dwc_eqos.c revision 1.7 1 /* $NetBSD: dwc_eqos.c,v 1.7 2022/07/20 18:48:41 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2022 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * DesignWare Ethernet Quality-of-Service controller
31 */
32
33 #include "opt_net_mpsafe.h"
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: dwc_eqos.c,v 1.7 2022/07/20 18:48:41 martin Exp $");
37
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/device.h>
41 #include <sys/intr.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/mutex.h>
45 #include <sys/callout.h>
46 #include <sys/cprng.h>
47 #include <sys/evcnt.h>
48
49 #include <sys/rndsource.h>
50
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_ether.h>
54 #include <net/if_media.h>
55 #include <net/bpf.h>
56
57 #include <dev/mii/miivar.h>
58
59 #include <dev/ic/dwc_eqos_reg.h>
60 #include <dev/ic/dwc_eqos_var.h>
61
62 CTASSERT(MCLBYTES == 2048);
63 #ifdef EQOS_DEBUG
64 #define DPRINTF(...) printf(##__VA_ARGS__)
65 #else
66 #define DPRINTF(...) ((void)0)
67 #endif
68
69 #ifdef NET_MPSAFE
70 #define EQOS_MPSAFE 1
71 #define CALLOUT_FLAGS CALLOUT_MPSAFE
72 #else
73 #define CALLOUT_FLAGS 0
74 #endif
75
76 #define DESC_BOUNDARY (1ULL << 32)
77 #define DESC_ALIGN sizeof(struct eqos_dma_desc)
78 #define TX_DESC_COUNT EQOS_DMA_DESC_COUNT
79 #define TX_DESC_SIZE (TX_DESC_COUNT * DESC_ALIGN)
80 #define RX_DESC_COUNT EQOS_DMA_DESC_COUNT
81 #define RX_DESC_SIZE (RX_DESC_COUNT * DESC_ALIGN)
82 #define MII_BUSY_RETRY 1000
83
84 #define DESC_OFF(n) ((n) * sizeof(struct eqos_dma_desc))
85 #define TX_SKIP(n, o) (((n) + (o)) % TX_DESC_COUNT)
86 #define TX_NEXT(n) TX_SKIP(n, 1)
87 #define RX_NEXT(n) (((n) + 1) % RX_DESC_COUNT)
88
89 #define TX_MAX_SEGS 128
90
91 #define EQOS_LOCK(sc) mutex_enter(&(sc)->sc_lock)
92 #define EQOS_UNLOCK(sc) mutex_exit(&(sc)->sc_lock)
93 #define EQOS_ASSERT_LOCKED(sc) KASSERT(mutex_owned(&(sc)->sc_lock))
94
95 #define EQOS_TXLOCK(sc) mutex_enter(&(sc)->sc_txlock)
96 #define EQOS_TXUNLOCK(sc) mutex_exit(&(sc)->sc_txlock)
97 #define EQOS_ASSERT_TXLOCKED(sc) KASSERT(mutex_owned(&(sc)->sc_txlock))
98
99 #define EQOS_HW_FEATURE_ADDR64_32BIT(sc) \
100 (((sc)->sc_hw_feature[1] & GMAC_MAC_HW_FEATURE1_ADDR64_MASK) == \
101 GMAC_MAC_HW_FEATURE1_ADDR64_32BIT)
102
103
104 #define RD4(sc, reg) \
105 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
106 #define WR4(sc, reg, val) \
107 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
108
109 #define STUB(...) \
110 printf("%s: TODO\n", __func__); \
111
112 static int
113 eqos_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
114 {
115 struct eqos_softc *sc = device_private(dev);
116 uint32_t addr;
117 int retry;
118
119 addr = sc->sc_clock_range |
120 (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
121 (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
122 GMAC_MAC_MDIO_ADDRESS_GOC_READ |
123 GMAC_MAC_MDIO_ADDRESS_GB;
124 WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
125
126 delay(10000);
127
128 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
129 addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
130 if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
131 *val = RD4(sc, GMAC_MAC_MDIO_DATA) & 0xFFFF;
132 break;
133 }
134 delay(10);
135 }
136 if (retry == 0) {
137 device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
138 phy, reg);
139 return ETIMEDOUT;
140 }
141
142 return 0;
143 }
144
145 static int
146 eqos_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
147 {
148 struct eqos_softc *sc = device_private(dev);
149 uint32_t addr;
150 int retry;
151
152 WR4(sc, GMAC_MAC_MDIO_DATA, val);
153
154 addr = sc->sc_clock_range |
155 (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
156 (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
157 GMAC_MAC_MDIO_ADDRESS_GOC_WRITE |
158 GMAC_MAC_MDIO_ADDRESS_GB;
159 WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
160
161 delay(10000);
162
163 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
164 addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
165 if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
166 break;
167 }
168 delay(10);
169 }
170 if (retry == 0) {
171 device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
172 phy, reg);
173 return ETIMEDOUT;
174 }
175
176 return 0;
177 }
178
179 static void
180 eqos_update_link(struct eqos_softc *sc)
181 {
182 struct mii_data *mii = &sc->sc_mii;
183 uint64_t baudrate;
184 uint32_t conf;
185
186 baudrate = ifmedia_baudrate(mii->mii_media_active);
187
188 conf = RD4(sc, GMAC_MAC_CONFIGURATION);
189 switch (baudrate) {
190 case IF_Mbps(10):
191 conf |= GMAC_MAC_CONFIGURATION_PS;
192 conf &= ~GMAC_MAC_CONFIGURATION_FES;
193 break;
194 case IF_Mbps(100):
195 conf |= GMAC_MAC_CONFIGURATION_PS;
196 conf |= GMAC_MAC_CONFIGURATION_FES;
197 break;
198 case IF_Gbps(1):
199 conf &= ~GMAC_MAC_CONFIGURATION_PS;
200 conf &= ~GMAC_MAC_CONFIGURATION_FES;
201 break;
202 case IF_Mbps(2500ULL):
203 conf &= ~GMAC_MAC_CONFIGURATION_PS;
204 conf |= GMAC_MAC_CONFIGURATION_FES;
205 break;
206 }
207
208 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
209 conf |= GMAC_MAC_CONFIGURATION_DM;
210 } else {
211 conf &= ~GMAC_MAC_CONFIGURATION_DM;
212 }
213
214 WR4(sc, GMAC_MAC_CONFIGURATION, conf);
215 }
216
217 static void
218 eqos_mii_statchg(struct ifnet *ifp)
219 {
220 struct eqos_softc * const sc = ifp->if_softc;
221
222 eqos_update_link(sc);
223 }
224
225 static void
226 eqos_dma_sync(struct eqos_softc *sc, bus_dmamap_t map,
227 u_int start, u_int end, u_int total, int flags)
228 {
229 if (end > start) {
230 bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
231 DESC_OFF(end) - DESC_OFF(start), flags);
232 } else {
233 bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
234 DESC_OFF(total) - DESC_OFF(start), flags);
235 if (DESC_OFF(end) - DESC_OFF(0) > 0) {
236 bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(0),
237 DESC_OFF(end) - DESC_OFF(0), flags);
238 }
239 }
240 }
241
242 static void
243 eqos_setup_txdesc(struct eqos_softc *sc, int index, int flags,
244 bus_addr_t paddr, u_int len, u_int total_len)
245 {
246 uint32_t tdes2, tdes3;
247
248 if (paddr == 0 || len == 0) {
249 KASSERT(flags == 0);
250 tdes2 = 0;
251 tdes3 = 0;
252 --sc->sc_tx.queued;
253 } else {
254 tdes2 = (flags & EQOS_TDES3_LD) ? EQOS_TDES2_IOC : 0;
255 tdes3 = flags;
256 ++sc->sc_tx.queued;
257 }
258
259 KASSERT(!EQOS_HW_FEATURE_ADDR64_32BIT(sc) || (paddr >> 32) == 0);
260
261 sc->sc_tx.desc_ring[index].tdes0 = htole32((uint32_t)paddr);
262 sc->sc_tx.desc_ring[index].tdes1 = htole32((uint32_t)(paddr >> 32));
263 sc->sc_tx.desc_ring[index].tdes2 = htole32(tdes2 | len);
264 sc->sc_tx.desc_ring[index].tdes3 = htole32(tdes3 | total_len);
265 }
266
267 static int
268 eqos_setup_txbuf(struct eqos_softc *sc, int index, struct mbuf *m)
269 {
270 bus_dma_segment_t *segs;
271 int error, nsegs, cur, i;
272 uint32_t flags;
273 bool nospace;
274
275 /* at least one descriptor free ? */
276 if (sc->sc_tx.queued >= TX_DESC_COUNT - 1)
277 return -1;
278
279 error = bus_dmamap_load_mbuf(sc->sc_dmat,
280 sc->sc_tx.buf_map[index].map, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
281 if (error == EFBIG) {
282 device_printf(sc->sc_dev,
283 "TX packet needs too many DMA segments, dropping...\n");
284 return -2;
285 }
286 if (error != 0) {
287 device_printf(sc->sc_dev,
288 "TX packet cannot be mapped, retried...\n");
289 return 0;
290 }
291
292 segs = sc->sc_tx.buf_map[index].map->dm_segs;
293 nsegs = sc->sc_tx.buf_map[index].map->dm_nsegs;
294
295 nospace = sc->sc_tx.queued >= TX_DESC_COUNT - nsegs;
296 if (nospace) {
297 bus_dmamap_unload(sc->sc_dmat,
298 sc->sc_tx.buf_map[index].map);
299 /* XXX coalesce and retry ? */
300 return -1;
301 }
302
303 bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.buf_map[index].map,
304 0, sc->sc_tx.buf_map[index].map->dm_mapsize, BUS_DMASYNC_PREWRITE);
305
306 /* stored in same index as loaded map */
307 sc->sc_tx.buf_map[index].mbuf = m;
308
309 flags = EQOS_TDES3_FD;
310
311 for (cur = index, i = 0; i < nsegs; i++) {
312 if (i == nsegs - 1)
313 flags |= EQOS_TDES3_LD;
314
315 eqos_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
316 segs[i].ds_len, m->m_pkthdr.len);
317 flags &= ~EQOS_TDES3_FD;
318 cur = TX_NEXT(cur);
319
320 flags |= EQOS_TDES3_OWN;
321 }
322
323 /*
324 * Defer setting OWN bit on the first descriptor until all
325 * descriptors have been updated. The hardware will not try to
326 * process any descriptors past the first one still owned by
327 * software (i.e., with the OWN bit clear).
328 */
329 bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.desc_map,
330 DESC_OFF(index), offsetof(struct eqos_dma_desc, tdes3),
331 BUS_DMASYNC_PREWRITE);
332 sc->sc_tx.desc_ring[index].tdes3 |= htole32(EQOS_TDES3_OWN);
333
334 return nsegs;
335 }
336
337 static void
338 eqos_setup_rxdesc(struct eqos_softc *sc, int index, bus_addr_t paddr)
339 {
340
341 sc->sc_rx.desc_ring[index].tdes0 = htole32((uint32_t)paddr);
342 sc->sc_rx.desc_ring[index].tdes1 = htole32((uint32_t)(paddr >> 32));
343 sc->sc_rx.desc_ring[index].tdes2 = htole32(0);
344 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.desc_map,
345 DESC_OFF(index), offsetof(struct eqos_dma_desc, tdes3),
346 BUS_DMASYNC_PREWRITE);
347 sc->sc_rx.desc_ring[index].tdes3 =
348 htole32(EQOS_TDES3_OWN | EQOS_TDES3_IOC | EQOS_TDES3_BUF1V);
349 }
350
351 static int
352 eqos_setup_rxbuf(struct eqos_softc *sc, int index, struct mbuf *m)
353 {
354 int error;
355
356 m_adj(m, ETHER_ALIGN);
357
358 error = bus_dmamap_load_mbuf(sc->sc_dmat,
359 sc->sc_rx.buf_map[index].map, m, BUS_DMA_READ | BUS_DMA_NOWAIT);
360 if (error != 0)
361 return error;
362
363 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
364 0, sc->sc_rx.buf_map[index].map->dm_mapsize,
365 BUS_DMASYNC_PREREAD);
366
367 sc->sc_rx.buf_map[index].mbuf = m;
368 eqos_setup_rxdesc(sc, index,
369 sc->sc_rx.buf_map[index].map->dm_segs[0].ds_addr);
370
371 return 0;
372 }
373
374 static struct mbuf *
375 eqos_alloc_mbufcl(struct eqos_softc *sc)
376 {
377 struct mbuf *m;
378
379 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
380 if (m != NULL)
381 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
382
383 return m;
384 }
385
386 static void
387 eqos_enable_intr(struct eqos_softc *sc)
388 {
389 WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE,
390 GMAC_DMA_CHAN0_INTR_ENABLE_NIE |
391 GMAC_DMA_CHAN0_INTR_ENABLE_AIE |
392 GMAC_DMA_CHAN0_INTR_ENABLE_FBE |
393 GMAC_DMA_CHAN0_INTR_ENABLE_RIE |
394 GMAC_DMA_CHAN0_INTR_ENABLE_TIE);
395 }
396
397 static void
398 eqos_disable_intr(struct eqos_softc *sc)
399 {
400 WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, 0);
401 }
402
403 static void
404 eqos_tick(void *softc)
405 {
406 struct eqos_softc *sc = softc;
407 struct mii_data *mii = &sc->sc_mii;
408 #ifndef EQOS_MPSAFE
409 int s = splnet();
410 #endif
411
412 EQOS_LOCK(sc);
413 mii_tick(mii);
414 callout_schedule(&sc->sc_stat_ch, hz);
415 EQOS_UNLOCK(sc);
416
417 #ifndef EQOS_MPSAFE
418 splx(s);
419 #endif
420 }
421
422 static uint32_t
423 eqos_bitrev32(uint32_t x)
424 {
425 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
426 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
427 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
428 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
429
430 return (x >> 16) | (x << 16);
431 }
432
433 static void
434 eqos_setup_rxfilter(struct eqos_softc *sc)
435 {
436 struct ethercom *ec = &sc->sc_ec;
437 struct ifnet *ifp = &ec->ec_if;
438 uint32_t pfil, crc, hashreg, hashbit, hash[2];
439 struct ether_multi *enm;
440 struct ether_multistep step;
441 const uint8_t *eaddr;
442 uint32_t val;
443
444 EQOS_ASSERT_LOCKED(sc);
445
446 pfil = RD4(sc, GMAC_MAC_PACKET_FILTER);
447 pfil &= ~(GMAC_MAC_PACKET_FILTER_PR |
448 GMAC_MAC_PACKET_FILTER_PM |
449 GMAC_MAC_PACKET_FILTER_HMC |
450 GMAC_MAC_PACKET_FILTER_PCF_MASK);
451 hash[0] = hash[1] = ~0U;
452
453 if ((ifp->if_flags & IFF_PROMISC) != 0) {
454 pfil |= GMAC_MAC_PACKET_FILTER_PR |
455 GMAC_MAC_PACKET_FILTER_PCF_ALL;
456 } else if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
457 pfil |= GMAC_MAC_PACKET_FILTER_PM;
458 } else {
459 hash[0] = hash[1] = 0;
460 pfil |= GMAC_MAC_PACKET_FILTER_HMC;
461 ETHER_LOCK(ec);
462 ETHER_FIRST_MULTI(step, ec, enm);
463 while (enm != NULL) {
464 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
465 crc &= 0x7f;
466 crc = eqos_bitrev32(~crc) >> 26;
467 hashreg = (crc >> 5);
468 hashbit = (crc & 0x1f);
469 hash[hashreg] |= (1 << hashbit);
470 ETHER_NEXT_MULTI(step, enm);
471 }
472 ETHER_UNLOCK(ec);
473 }
474
475 /* Write our unicast address */
476 eaddr = CLLADDR(ifp->if_sadl);
477 val = eaddr[4] | (eaddr[5] << 8);
478 WR4(sc, GMAC_MAC_ADDRESS0_HIGH, val);
479 val = eaddr[0] | (eaddr[1] << 8) | (eaddr[2] << 16) |
480 (eaddr[3] << 24);
481 WR4(sc, GMAC_MAC_ADDRESS0_LOW, val);
482
483 /* Multicast hash filters */
484 WR4(sc, GMAC_MAC_HASH_TABLE_REG0, hash[1]);
485 WR4(sc, GMAC_MAC_HASH_TABLE_REG1, hash[0]);
486
487 /* Packet filter config */
488 WR4(sc, GMAC_MAC_PACKET_FILTER, pfil);
489 }
490
491 static int
492 eqos_reset(struct eqos_softc *sc)
493 {
494 uint32_t val;
495 int retry;
496
497 WR4(sc, GMAC_DMA_MODE, GMAC_DMA_MODE_SWR);
498 for (retry = 2000; retry > 0; retry--) {
499 delay(1000);
500 val = RD4(sc, GMAC_DMA_MODE);
501 if ((val & GMAC_DMA_MODE_SWR) == 0) {
502 return 0;
503 }
504 }
505
506 device_printf(sc->sc_dev, "reset timeout!\n");
507 return ETIMEDOUT;
508 }
509
510 static void
511 eqos_init_rings(struct eqos_softc *sc, int qid)
512 {
513 /*
514 * We reset the rings paddr, this implicitly resets
515 * the hardwares current descriptor pointer.
516 * Adjust our internal state accordingly.
517 */
518 sc->sc_tx.cur = sc->sc_tx.next = sc->sc_tx.queued = 0;
519 sc->sc_rx.cur = sc->sc_rx.next = sc->sc_rx.queued = 0;
520
521 WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR_HI,
522 (uint32_t)(sc->sc_tx.desc_ring_paddr >> 32));
523 WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR,
524 (uint32_t)sc->sc_tx.desc_ring_paddr);
525 WR4(sc, GMAC_DMA_CHAN0_TX_RING_LEN, TX_DESC_COUNT - 1);
526
527 WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR_HI,
528 (uint32_t)(sc->sc_rx.desc_ring_paddr >> 32));
529 WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR,
530 (uint32_t)sc->sc_rx.desc_ring_paddr);
531 WR4(sc, GMAC_DMA_CHAN0_RX_RING_LEN, RX_DESC_COUNT - 1);
532 WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
533 (uint32_t)sc->sc_rx.desc_ring_paddr +
534 DESC_OFF((sc->sc_rx.cur - 1) % RX_DESC_COUNT));
535 }
536
537 static int
538 eqos_init_locked(struct eqos_softc *sc)
539 {
540 struct ifnet *ifp = &sc->sc_ec.ec_if;
541 struct mii_data *mii = &sc->sc_mii;
542 uint32_t val;
543
544 EQOS_ASSERT_LOCKED(sc);
545 EQOS_ASSERT_TXLOCKED(sc);
546
547 if ((ifp->if_flags & IFF_RUNNING) != 0)
548 return 0;
549
550 /* Setup TX/RX rings */
551 eqos_init_rings(sc, 0);
552
553 /* Setup RX filter */
554 eqos_setup_rxfilter(sc);
555
556 WR4(sc, GMAC_MAC_1US_TIC_COUNTER, (sc->sc_csr_clock / 1000000) - 1);
557
558 /* Enable transmit and receive DMA */
559 val = RD4(sc, GMAC_DMA_CHAN0_CONTROL);
560 val &= ~GMAC_DMA_CHAN0_CONTROL_DSL_MASK;
561 val |= ((DESC_ALIGN - 16) / 8) << GMAC_DMA_CHAN0_CONTROL_DSL_SHIFT;
562 val |= GMAC_DMA_CHAN0_CONTROL_PBLX8;
563 WR4(sc, GMAC_DMA_CHAN0_CONTROL, val);
564 val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
565 val |= GMAC_DMA_CHAN0_TX_CONTROL_OSP;
566 val |= GMAC_DMA_CHAN0_TX_CONTROL_START;
567 WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
568 val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
569 val &= ~GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_MASK;
570 val |= (MCLBYTES << GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_SHIFT);
571 val |= GMAC_DMA_CHAN0_RX_CONTROL_START;
572 WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
573
574 /* Disable counters */
575 WR4(sc, GMAC_MMC_CONTROL,
576 GMAC_MMC_CONTROL_CNTFREEZ |
577 GMAC_MMC_CONTROL_CNTPRST |
578 GMAC_MMC_CONTROL_CNTPRSTLVL);
579
580 /* Configure operation modes */
581 WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE,
582 GMAC_MTL_TXQ0_OPERATION_MODE_TSF |
583 GMAC_MTL_TXQ0_OPERATION_MODE_TXQEN_EN);
584 WR4(sc, GMAC_MTL_RXQ0_OPERATION_MODE,
585 GMAC_MTL_RXQ0_OPERATION_MODE_RSF |
586 GMAC_MTL_RXQ0_OPERATION_MODE_FEP |
587 GMAC_MTL_RXQ0_OPERATION_MODE_FUP);
588
589 /* Enable flow control */
590 val = RD4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL);
591 val |= 0xFFFFU << GMAC_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT;
592 val |= GMAC_MAC_Q0_TX_FLOW_CTRL_TFE;
593 WR4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL, val);
594 val = RD4(sc, GMAC_MAC_RX_FLOW_CTRL);
595 val |= GMAC_MAC_RX_FLOW_CTRL_RFE;
596 WR4(sc, GMAC_MAC_RX_FLOW_CTRL, val);
597
598 /* Enable transmitter and receiver */
599 val = RD4(sc, GMAC_MAC_CONFIGURATION);
600 val |= GMAC_MAC_CONFIGURATION_BE;
601 val |= GMAC_MAC_CONFIGURATION_JD;
602 val |= GMAC_MAC_CONFIGURATION_JE;
603 val |= GMAC_MAC_CONFIGURATION_DCRS;
604 val |= GMAC_MAC_CONFIGURATION_TE;
605 val |= GMAC_MAC_CONFIGURATION_RE;
606 WR4(sc, GMAC_MAC_CONFIGURATION, val);
607
608 /* Enable interrupts */
609 eqos_enable_intr(sc);
610
611 ifp->if_flags |= IFF_RUNNING;
612 ifp->if_flags &= ~IFF_OACTIVE;
613
614 mii_mediachg(mii);
615 callout_schedule(&sc->sc_stat_ch, hz);
616
617 return 0;
618 }
619
620 static int
621 eqos_init(struct ifnet *ifp)
622 {
623 struct eqos_softc *sc = ifp->if_softc;
624 int error;
625
626 EQOS_LOCK(sc);
627 EQOS_TXLOCK(sc);
628 error = eqos_init_locked(sc);
629 EQOS_TXUNLOCK(sc);
630 EQOS_UNLOCK(sc);
631
632 return error;
633 }
634
635 static void
636 eqos_stop_locked(struct eqos_softc *sc, int disable)
637 {
638 struct ifnet *ifp = &sc->sc_ec.ec_if;
639 uint32_t val;
640 int retry;
641
642 EQOS_ASSERT_LOCKED(sc);
643
644 callout_stop(&sc->sc_stat_ch);
645
646 mii_down(&sc->sc_mii);
647
648 /* Disable receiver */
649 val = RD4(sc, GMAC_MAC_CONFIGURATION);
650 val &= ~GMAC_MAC_CONFIGURATION_RE;
651 WR4(sc, GMAC_MAC_CONFIGURATION, val);
652
653 /* Stop receive DMA */
654 val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
655 val &= ~GMAC_DMA_CHAN0_RX_CONTROL_START;
656 WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
657
658 /* Stop transmit DMA */
659 val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
660 val &= ~GMAC_DMA_CHAN0_TX_CONTROL_START;
661 WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
662
663 if (disable) {
664 /* Flush data in the TX FIFO */
665 val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
666 val |= GMAC_MTL_TXQ0_OPERATION_MODE_FTQ;
667 WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE, val);
668 /* Wait for flush to complete */
669 for (retry = 10000; retry > 0; retry--) {
670 val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
671 if ((val & GMAC_MTL_TXQ0_OPERATION_MODE_FTQ) == 0) {
672 break;
673 }
674 delay(1);
675 }
676 if (retry == 0) {
677 device_printf(sc->sc_dev,
678 "timeout flushing TX queue\n");
679 }
680 }
681
682 /* Disable transmitter */
683 val = RD4(sc, GMAC_MAC_CONFIGURATION);
684 val &= ~GMAC_MAC_CONFIGURATION_TE;
685 WR4(sc, GMAC_MAC_CONFIGURATION, val);
686
687 /* Disable interrupts */
688 eqos_disable_intr(sc);
689
690 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
691 }
692
693 static void
694 eqos_stop(struct ifnet *ifp, int disable)
695 {
696 struct eqos_softc * const sc = ifp->if_softc;
697
698 EQOS_LOCK(sc);
699 eqos_stop_locked(sc, disable);
700 EQOS_UNLOCK(sc);
701 }
702
703 static void
704 eqos_rxintr(struct eqos_softc *sc, int qid)
705 {
706 struct ifnet *ifp = &sc->sc_ec.ec_if;
707 int error, index, len, pkts = 0;
708 struct mbuf *m, *m0;
709 uint32_t tdes3;
710
711 for (index = sc->sc_rx.cur; ; index = RX_NEXT(index)) {
712 eqos_dma_sync(sc, sc->sc_rx.desc_map,
713 index, index + 1, RX_DESC_COUNT,
714 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
715
716 tdes3 = le32toh(sc->sc_rx.desc_ring[index].tdes3);
717 if ((tdes3 & EQOS_TDES3_OWN) != 0) {
718 break;
719 }
720
721 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
722 0, sc->sc_rx.buf_map[index].map->dm_mapsize,
723 BUS_DMASYNC_POSTREAD);
724 bus_dmamap_unload(sc->sc_dmat,
725 sc->sc_rx.buf_map[index].map);
726
727 len = tdes3 & EQOS_TDES3_LENGTH_MASK;
728 if (len != 0) {
729 m = sc->sc_rx.buf_map[index].mbuf;
730 m_set_rcvif(m, ifp);
731 m->m_flags |= M_HASFCS;
732 m->m_pkthdr.len = len;
733 m->m_len = len;
734 m->m_nextpkt = NULL;
735
736 if_percpuq_enqueue(ifp->if_percpuq, m);
737 ++pkts;
738 }
739
740 if ((m0 = eqos_alloc_mbufcl(sc)) != NULL) {
741 error = eqos_setup_rxbuf(sc, index, m0);
742 if (error != 0) {
743 /* XXX hole in RX ring */
744 }
745 } else {
746 if_statinc(ifp, if_ierrors);
747 }
748 eqos_dma_sync(sc, sc->sc_rx.desc_map,
749 index, index + 1, RX_DESC_COUNT,
750 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
751
752 WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
753 (uint32_t)sc->sc_rx.desc_ring_paddr +
754 DESC_OFF(sc->sc_rx.cur));
755 }
756
757 sc->sc_rx.cur = index;
758
759 if (pkts != 0) {
760 rnd_add_uint32(&sc->sc_rndsource, pkts);
761 }
762 }
763
764 static void
765 eqos_txintr(struct eqos_softc *sc, int qid)
766 {
767 struct ifnet *ifp = &sc->sc_ec.ec_if;
768 struct eqos_bufmap *bmap;
769 struct eqos_dma_desc *desc;
770 uint32_t tdes3;
771 int i, pkts = 0;
772
773 EQOS_ASSERT_LOCKED(sc);
774
775 for (i = sc->sc_tx.next; sc->sc_tx.queued > 0; i = TX_NEXT(i)) {
776 KASSERT(sc->sc_tx.queued > 0);
777 KASSERT(sc->sc_tx.queued <= TX_DESC_COUNT);
778 eqos_dma_sync(sc, sc->sc_tx.desc_map,
779 i, i + 1, TX_DESC_COUNT,
780 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
781 desc = &sc->sc_tx.desc_ring[i];
782 tdes3 = le32toh(desc->tdes3);
783 if ((tdes3 & EQOS_TDES3_OWN) != 0) {
784 break;
785 }
786 bmap = &sc->sc_tx.buf_map[i];
787 if (bmap->mbuf != NULL) {
788 bus_dmamap_sync(sc->sc_dmat, bmap->map,
789 0, bmap->map->dm_mapsize,
790 BUS_DMASYNC_POSTWRITE);
791 bus_dmamap_unload(sc->sc_dmat, bmap->map);
792 m_freem(bmap->mbuf);
793 bmap->mbuf = NULL;
794 ++pkts;
795 }
796
797 eqos_setup_txdesc(sc, i, 0, 0, 0, 0);
798 eqos_dma_sync(sc, sc->sc_tx.desc_map,
799 i, i + 1, TX_DESC_COUNT,
800 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
801
802 ifp->if_flags &= ~IFF_OACTIVE;
803
804 /* Last descriptor in a packet contains DMA status */
805 if ((tdes3 & EQOS_TDES3_LD) != 0) {
806 if ((tdes3 & EQOS_TDES3_DE) != 0) {
807 device_printf(sc->sc_dev,
808 "TX [%u] desc error: 0x%08x\n",
809 i, tdes3);
810 if_statinc(ifp, if_oerrors);
811 } else if ((tdes3 & EQOS_TDES3_ES) != 0) {
812 device_printf(sc->sc_dev,
813 "TX [%u] tx error: 0x%08x\n",
814 i, tdes3);
815 if_statinc(ifp, if_oerrors);
816 } else {
817 if_statinc(ifp, if_opackets);
818 }
819 }
820
821 }
822
823 sc->sc_tx.next = i;
824
825 if (pkts != 0) {
826 rnd_add_uint32(&sc->sc_rndsource, pkts);
827 }
828 }
829
830 static void
831 eqos_start_locked(struct eqos_softc *sc)
832 {
833 struct ifnet *ifp = &sc->sc_ec.ec_if;
834 struct mbuf *m;
835 int cnt, nsegs, start;
836
837 EQOS_ASSERT_TXLOCKED(sc);
838
839 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
840 return;
841
842 for (cnt = 0, start = sc->sc_tx.cur; ; cnt++) {
843 if (sc->sc_tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
844 ifp->if_flags |= IFF_OACTIVE;
845 break;
846 }
847
848 IFQ_POLL(&ifp->if_snd, m);
849 if (m == NULL) {
850 break;
851 }
852
853 nsegs = eqos_setup_txbuf(sc, sc->sc_tx.cur, m);
854 if (nsegs <= 0) {
855 if (nsegs == -1) {
856 ifp->if_flags |= IFF_OACTIVE;
857 } else if (nsegs == -2) {
858 IFQ_DEQUEUE(&ifp->if_snd, m);
859 m_freem(m);
860 }
861 break;
862 }
863
864 IFQ_DEQUEUE(&ifp->if_snd, m);
865 bpf_mtap(ifp, m, BPF_D_OUT);
866
867 sc->sc_tx.cur = TX_SKIP(sc->sc_tx.cur, nsegs);
868 }
869
870 if (cnt != 0) {
871 eqos_dma_sync(sc, sc->sc_tx.desc_map,
872 start, sc->sc_tx.cur, TX_DESC_COUNT,
873 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
874
875 /* Start and run TX DMA */
876 WR4(sc, GMAC_DMA_CHAN0_TX_END_ADDR,
877 (uint32_t)sc->sc_tx.desc_ring_paddr +
878 DESC_OFF(sc->sc_tx.cur));
879 }
880 }
881
882 static void
883 eqos_start(struct ifnet *ifp)
884 {
885 struct eqos_softc *sc = ifp->if_softc;
886
887 EQOS_TXLOCK(sc);
888 eqos_start_locked(sc);
889 EQOS_TXUNLOCK(sc);
890 }
891
892 static void
893 eqos_intr_mtl(struct eqos_softc *sc, uint32_t mtl_status)
894 {
895 uint32_t debug_data __unused = 0, ictrl = 0;
896
897 if (mtl_status == 0)
898 return;
899
900 /* Drain the errors reported by MTL_INTERRUPT_STATUS */
901 sc->sc_ev_mtl.ev_count++;
902
903 if ((mtl_status & GMAC_MTL_INTERRUPT_STATUS_DBGIS) != 0) {
904 debug_data = RD4(sc, GMAC_MTL_FIFO_DEBUG_DATA);
905 sc->sc_ev_mtl_debugdata.ev_count++;
906 }
907 if ((mtl_status & GMAC_MTL_INTERRUPT_STATUS_Q0IS) != 0) {
908 uint32_t new_status = 0;
909
910 ictrl = RD4(sc, GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS);
911 if ((ictrl & GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOVFIS) != 0) {
912 new_status |= GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOVFIS;
913 sc->sc_ev_mtl_rxovfis.ev_count++;
914 }
915 if ((ictrl & GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUNFIS) != 0) {
916 new_status |= GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUNFIS;
917 sc->sc_ev_mtl_txovfis.ev_count++;
918 }
919 if (new_status) {
920 new_status |= (ictrl &
921 (GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOIE|
922 GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUIE));
923 WR4(sc, GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS, new_status);
924 }
925 }
926 #ifdef DEBUG_LOUD
927 device_printf(sc->sc_dev,
928 "GMAC_MTL_INTERRUPT_STATUS = 0x%08X, "
929 "GMAC_MTL_FIFO_DEBUG_DATA = 0x%08X, "
930 "GMAC_MTL_INTERRUPT_STATUS_Q0IS = 0x%08X\n",
931 mtl_status, debug_data, ictrl);
932 #endif
933 }
934
935 int
936 eqos_intr(void *arg)
937 {
938 struct eqos_softc *sc = arg;
939 struct ifnet *ifp = &sc->sc_ec.ec_if;
940 uint32_t mac_status, mtl_status, dma_status, rx_tx_status;
941
942 sc->sc_ev_intr.ev_count++;
943
944 mac_status = RD4(sc, GMAC_MAC_INTERRUPT_STATUS);
945 mac_status &= RD4(sc, GMAC_MAC_INTERRUPT_ENABLE);
946
947 if (mac_status) {
948 sc->sc_ev_mac.ev_count++;
949 #ifdef DEBUG_LOUD
950 device_printf(sc->sc_dev,
951 "GMAC_MAC_INTERRUPT_STATUS = 0x%08X\n", mac_status);
952 #endif
953 }
954
955 mtl_status = RD4(sc, GMAC_MTL_INTERRUPT_STATUS);
956 eqos_intr_mtl(sc, mtl_status);
957
958 dma_status = RD4(sc, GMAC_DMA_CHAN0_STATUS);
959 dma_status &= RD4(sc, GMAC_DMA_CHAN0_INTR_ENABLE);
960 if (dma_status) {
961 WR4(sc, GMAC_DMA_CHAN0_STATUS, dma_status);
962 }
963
964 EQOS_LOCK(sc);
965 if ((dma_status & GMAC_DMA_CHAN0_STATUS_RI) != 0) {
966 eqos_rxintr(sc, 0);
967 sc->sc_ev_rxintr.ev_count++;
968 }
969
970 if ((dma_status & GMAC_DMA_CHAN0_STATUS_TI) != 0) {
971 eqos_txintr(sc, 0);
972 if_schedule_deferred_start(ifp);
973 sc->sc_ev_txintr.ev_count++;
974 }
975 EQOS_UNLOCK(sc);
976
977 #ifdef DEBUG_LOUD
978 if ((mac_status | mtl_status | dma_status) == 0) {
979 device_printf(sc->sc_dev, "spurious interrupt?!\n");
980 }
981 #endif
982
983 rx_tx_status = RD4(sc, GMAC_MAC_RX_TX_STATUS);
984 if (rx_tx_status) {
985 sc->sc_ev_status.ev_count++;
986 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_RWT) != 0)
987 sc->sc_ev_rwt.ev_count++;
988 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXCOL) != 0)
989 sc->sc_ev_excol.ev_count++;
990 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCOL) != 0)
991 sc->sc_ev_lcol.ev_count++;
992 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXDEF) != 0)
993 sc->sc_ev_exdef.ev_count++;
994 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCARR) != 0)
995 sc->sc_ev_lcarr.ev_count++;
996 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_NCARR) != 0)
997 sc->sc_ev_ncarr.ev_count++;
998 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_TJT) != 0)
999 sc->sc_ev_tjt.ev_count++;
1000 #ifdef DEBUG_LOUD
1001 device_printf(sc->sc_dev, "GMAC_MAC_RX_TX_STATUS = 0x%08x\n",
1002 rx_tx_status);
1003 #endif
1004 }
1005
1006 return 1;
1007 }
1008
1009 static int
1010 eqos_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1011 {
1012 struct eqos_softc *sc = ifp->if_softc;
1013 int error, s;
1014
1015 #ifndef EQOS_MPSAFE
1016 s = splnet();
1017 #endif
1018
1019 switch (cmd) {
1020 default:
1021 #ifdef EQOS_MPSAFE
1022 s = splnet();
1023 #endif
1024 error = ether_ioctl(ifp, cmd, data);
1025 #ifdef EQOS_MPSAFE
1026 splx(s);
1027 #endif
1028 if (error != ENETRESET)
1029 break;
1030
1031 error = 0;
1032
1033 if (cmd == SIOCSIFCAP)
1034 error = (*ifp->if_init)(ifp);
1035 else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
1036 ;
1037 else if ((ifp->if_flags & IFF_RUNNING) != 0) {
1038 EQOS_LOCK(sc);
1039 eqos_setup_rxfilter(sc);
1040 EQOS_UNLOCK(sc);
1041 }
1042 break;
1043 }
1044
1045 #ifndef EQOS_MPSAFE
1046 splx(s);
1047 #endif
1048
1049 return error;
1050 }
1051
1052 static void
1053 eqos_get_eaddr(struct eqos_softc *sc, uint8_t *eaddr)
1054 {
1055 prop_dictionary_t prop = device_properties(sc->sc_dev);
1056 uint32_t maclo, machi;
1057 prop_data_t eaprop;
1058
1059 eaprop = prop_dictionary_get(prop, "mac-address");
1060 if (eaprop != NULL) {
1061 KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
1062 KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
1063 memcpy(eaddr, prop_data_value(eaprop),
1064 ETHER_ADDR_LEN);
1065 return;
1066 }
1067
1068 maclo = htobe32(RD4(sc, GMAC_MAC_ADDRESS0_LOW));
1069 machi = htobe16(RD4(sc, GMAC_MAC_ADDRESS0_HIGH) & 0xFFFF);
1070
1071 if (maclo == 0xFFFFFFFF && machi == 0xFFFF) {
1072 /* Create one */
1073 maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
1074 machi = cprng_strong32() & 0xffff;
1075 }
1076
1077 eaddr[0] = maclo & 0xff;
1078 eaddr[1] = (maclo >> 8) & 0xff;
1079 eaddr[2] = (maclo >> 16) & 0xff;
1080 eaddr[3] = (maclo >> 24) & 0xff;
1081 eaddr[4] = machi & 0xff;
1082 eaddr[5] = (machi >> 8) & 0xff;
1083 }
1084
1085 static void
1086 eqos_axi_configure(struct eqos_softc *sc)
1087 {
1088 prop_dictionary_t prop = device_properties(sc->sc_dev);
1089 uint32_t val;
1090 u_int uival;
1091 bool bval;
1092
1093 val = RD4(sc, GMAC_DMA_SYSBUS_MODE);
1094 if (prop_dictionary_get_bool(prop, "snps,mixed-burst", &bval) && bval) {
1095 val |= GMAC_DMA_SYSBUS_MODE_MB;
1096 }
1097 if (prop_dictionary_get_bool(prop, "snps,fixed-burst", &bval) && bval) {
1098 val |= GMAC_DMA_SYSBUS_MODE_FB;
1099 }
1100 if (prop_dictionary_get_uint(prop, "snps,wr_osr_lmt", &uival)) {
1101 val &= ~GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_MASK;
1102 val |= uival << GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_SHIFT;
1103 }
1104 if (prop_dictionary_get_uint(prop, "snps,rd_osr_lmt", &uival)) {
1105 val &= ~GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK;
1106 val |= uival << GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT;
1107 }
1108
1109 if (!EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
1110 val |= GMAC_DMA_SYSBUS_MODE_EAME;
1111 }
1112
1113 /* XXX */
1114 val |= GMAC_DMA_SYSBUS_MODE_BLEN16;
1115 val |= GMAC_DMA_SYSBUS_MODE_BLEN8;
1116 val |= GMAC_DMA_SYSBUS_MODE_BLEN4;
1117
1118 WR4(sc, GMAC_DMA_SYSBUS_MODE, val);
1119 }
1120
1121 static int
1122 eqos_setup_dma(struct eqos_softc *sc, int qid)
1123 {
1124 struct mbuf *m;
1125 int error, nsegs, i;
1126
1127 /* Setup TX ring */
1128 error = bus_dmamap_create(sc->sc_dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE,
1129 DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_tx.desc_map);
1130 if (error) {
1131 return error;
1132 }
1133 error = bus_dmamem_alloc(sc->sc_dmat, TX_DESC_SIZE, DESC_ALIGN,
1134 DESC_BOUNDARY, &sc->sc_tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1135 if (error) {
1136 return error;
1137 }
1138 error = bus_dmamem_map(sc->sc_dmat, &sc->sc_tx.desc_dmaseg, nsegs,
1139 TX_DESC_SIZE, (void *)&sc->sc_tx.desc_ring, BUS_DMA_WAITOK);
1140 if (error) {
1141 return error;
1142 }
1143 error = bus_dmamap_load(sc->sc_dmat, sc->sc_tx.desc_map,
1144 sc->sc_tx.desc_ring, TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1145 if (error) {
1146 return error;
1147 }
1148 sc->sc_tx.desc_ring_paddr = sc->sc_tx.desc_map->dm_segs[0].ds_addr;
1149
1150 memset(sc->sc_tx.desc_ring, 0, TX_DESC_SIZE);
1151 bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.desc_map, 0, TX_DESC_SIZE,
1152 BUS_DMASYNC_PREWRITE);
1153
1154 sc->sc_tx.queued = TX_DESC_COUNT;
1155 for (i = 0; i < TX_DESC_COUNT; i++) {
1156 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
1157 TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
1158 &sc->sc_tx.buf_map[i].map);
1159 if (error != 0) {
1160 device_printf(sc->sc_dev,
1161 "cannot create TX buffer map\n");
1162 return error;
1163 }
1164 eqos_setup_txdesc(sc, i, 0, 0, 0, 0);
1165 }
1166
1167 /* Setup RX ring */
1168 error = bus_dmamap_create(sc->sc_dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE,
1169 DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_rx.desc_map);
1170 if (error) {
1171 return error;
1172 }
1173 error = bus_dmamem_alloc(sc->sc_dmat, RX_DESC_SIZE, DESC_ALIGN,
1174 DESC_BOUNDARY, &sc->sc_rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1175 if (error) {
1176 return error;
1177 }
1178 error = bus_dmamem_map(sc->sc_dmat, &sc->sc_rx.desc_dmaseg, nsegs,
1179 RX_DESC_SIZE, (void *)&sc->sc_rx.desc_ring, BUS_DMA_WAITOK);
1180 if (error) {
1181 return error;
1182 }
1183 error = bus_dmamap_load(sc->sc_dmat, sc->sc_rx.desc_map,
1184 sc->sc_rx.desc_ring, RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1185 if (error) {
1186 return error;
1187 }
1188 sc->sc_rx.desc_ring_paddr = sc->sc_rx.desc_map->dm_segs[0].ds_addr;
1189
1190 memset(sc->sc_rx.desc_ring, 0, RX_DESC_SIZE);
1191
1192 for (i = 0; i < RX_DESC_COUNT; i++) {
1193 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
1194 RX_DESC_COUNT, MCLBYTES, 0, BUS_DMA_WAITOK,
1195 &sc->sc_rx.buf_map[i].map);
1196 if (error != 0) {
1197 device_printf(sc->sc_dev,
1198 "cannot create RX buffer map\n");
1199 return error;
1200 }
1201 if ((m = eqos_alloc_mbufcl(sc)) == NULL) {
1202 device_printf(sc->sc_dev, "cannot allocate RX mbuf\n");
1203 return ENOMEM;
1204 }
1205 error = eqos_setup_rxbuf(sc, i, m);
1206 if (error != 0) {
1207 device_printf(sc->sc_dev, "cannot create RX buffer\n");
1208 return error;
1209 }
1210 }
1211 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.desc_map,
1212 0, sc->sc_rx.desc_map->dm_mapsize,
1213 BUS_DMASYNC_PREWRITE);
1214
1215 aprint_debug_dev(sc->sc_dev, "TX ring @ 0x%lX, RX ring @ 0x%lX\n",
1216 sc->sc_tx.desc_ring_paddr, sc->sc_rx.desc_ring_paddr);
1217
1218 return 0;
1219 }
1220
1221 int
1222 eqos_attach(struct eqos_softc *sc)
1223 {
1224 struct mii_data *mii = &sc->sc_mii;
1225 struct ifnet *ifp = &sc->sc_ec.ec_if;
1226 uint8_t eaddr[ETHER_ADDR_LEN];
1227 u_int userver, snpsver;
1228 int mii_flags = 0;
1229 int error;
1230 int n;
1231
1232 const uint32_t ver = RD4(sc, GMAC_MAC_VERSION);
1233 userver = (ver & GMAC_MAC_VERSION_USERVER_MASK) >>
1234 GMAC_MAC_VERSION_USERVER_SHIFT;
1235 snpsver = ver & GMAC_MAC_VERSION_SNPSVER_MASK;
1236
1237 if (snpsver != 0x51) {
1238 aprint_error(": EQOS version 0x%02xx not supported\n",
1239 snpsver);
1240 return ENXIO;
1241 }
1242
1243 if (sc->sc_csr_clock < 20000000) {
1244 aprint_error(": CSR clock too low\n");
1245 return EINVAL;
1246 } else if (sc->sc_csr_clock < 35000000) {
1247 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_20_35;
1248 } else if (sc->sc_csr_clock < 60000000) {
1249 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_35_60;
1250 } else if (sc->sc_csr_clock < 100000000) {
1251 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_60_100;
1252 } else if (sc->sc_csr_clock < 150000000) {
1253 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_100_150;
1254 } else if (sc->sc_csr_clock < 250000000) {
1255 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_150_250;
1256 } else if (sc->sc_csr_clock < 300000000) {
1257 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_300_500;
1258 } else if (sc->sc_csr_clock < 800000000) {
1259 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_500_800;
1260 } else {
1261 aprint_error(": CSR clock too high\n");
1262 return EINVAL;
1263 }
1264
1265 for (n = 0; n < 4; n++) {
1266 sc->sc_hw_feature[n] = RD4(sc, GMAC_MAC_HW_FEATURE(n));
1267 }
1268
1269 aprint_naive("\n");
1270 aprint_normal(": DesignWare EQOS ver 0x%02x (0x%02x)\n",
1271 snpsver, userver);
1272 aprint_verbose_dev(sc->sc_dev, "hw features %08x %08x %08x %08x\n",
1273 sc->sc_hw_feature[0], sc->sc_hw_feature[1],
1274 sc->sc_hw_feature[2], sc->sc_hw_feature[3]);
1275
1276 if (EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
1277 bus_dma_tag_t ntag;
1278
1279 error = bus_dmatag_subregion(sc->sc_dmat, 0, UINT32_MAX,
1280 &ntag, 0);
1281 if (error) {
1282 aprint_error_dev(sc->sc_dev,
1283 "failed to restrict DMA: %d\n", error);
1284 return error;
1285 }
1286 aprint_verbose_dev(sc->sc_dev, "using 32-bit DMA\n");
1287 sc->sc_dmat = ntag;
1288 }
1289
1290 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
1291 mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_NET);
1292 callout_init(&sc->sc_stat_ch, CALLOUT_FLAGS);
1293 callout_setfunc(&sc->sc_stat_ch, eqos_tick, sc);
1294
1295 eqos_get_eaddr(sc, eaddr);
1296 aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n", ether_sprintf(eaddr));
1297
1298 /* Soft reset EMAC core */
1299 error = eqos_reset(sc);
1300 if (error != 0) {
1301 return error;
1302 }
1303
1304 /* Configure AXI Bus mode parameters */
1305 eqos_axi_configure(sc);
1306
1307 /* Setup DMA descriptors */
1308 if (eqos_setup_dma(sc, 0) != 0) {
1309 aprint_error_dev(sc->sc_dev, "failed to setup DMA descriptors\n");
1310 return EINVAL;
1311 }
1312
1313 /* Setup ethernet interface */
1314 ifp->if_softc = sc;
1315 snprintf(ifp->if_xname, IFNAMSIZ, "%s", device_xname(sc->sc_dev));
1316 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1317 #ifdef EQOS_MPSAFE
1318 ifp->if_extflags = IFEF_MPSAFE;
1319 #endif
1320 ifp->if_start = eqos_start;
1321 ifp->if_ioctl = eqos_ioctl;
1322 ifp->if_init = eqos_init;
1323 ifp->if_stop = eqos_stop;
1324 ifp->if_capabilities = 0;
1325 ifp->if_capenable = ifp->if_capabilities;
1326 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
1327 IFQ_SET_READY(&ifp->if_snd);
1328
1329 /* 802.1Q VLAN-sized frames are supported */
1330 sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
1331
1332 /* Attach MII driver */
1333 sc->sc_ec.ec_mii = mii;
1334 ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
1335 mii->mii_ifp = ifp;
1336 mii->mii_readreg = eqos_mii_readreg;
1337 mii->mii_writereg = eqos_mii_writereg;
1338 mii->mii_statchg = eqos_mii_statchg;
1339 mii_attach(sc->sc_dev, mii, 0xffffffff, sc->sc_phy_id, MII_OFFSET_ANY,
1340 mii_flags);
1341
1342 if (LIST_EMPTY(&mii->mii_phys)) {
1343 aprint_error_dev(sc->sc_dev, "no PHY found!\n");
1344 return ENOENT;
1345 }
1346 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1347
1348 /* Master interrupt evcnt */
1349 evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
1350 NULL, device_xname(sc->sc_dev), "interrupts");
1351
1352 /* Per-interrupt type, using main interrupt */
1353 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
1354 &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxintr");
1355 evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
1356 &sc->sc_ev_intr, device_xname(sc->sc_dev), "txintr");
1357 evcnt_attach_dynamic(&sc->sc_ev_mac, EVCNT_TYPE_INTR,
1358 &sc->sc_ev_intr, device_xname(sc->sc_dev), "macstatus");
1359 evcnt_attach_dynamic(&sc->sc_ev_mtl, EVCNT_TYPE_INTR,
1360 &sc->sc_ev_intr, device_xname(sc->sc_dev), "intrstatus");
1361 evcnt_attach_dynamic(&sc->sc_ev_status, EVCNT_TYPE_INTR,
1362 &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxtxstatus");
1363
1364 /* MAC Status specific type, using macstatus interrupt */
1365 evcnt_attach_dynamic(&sc->sc_ev_mtl_debugdata, EVCNT_TYPE_INTR,
1366 &sc->sc_ev_mtl, device_xname(sc->sc_dev), "debugdata");
1367 evcnt_attach_dynamic(&sc->sc_ev_mtl_rxovfis, EVCNT_TYPE_INTR,
1368 &sc->sc_ev_mtl, device_xname(sc->sc_dev), "rxovfis");
1369 evcnt_attach_dynamic(&sc->sc_ev_mtl_txovfis, EVCNT_TYPE_INTR,
1370 &sc->sc_ev_mtl, device_xname(sc->sc_dev), "txovfis");
1371
1372 /* RX/TX Status specific type, using rxtxstatus interrupt */
1373 evcnt_attach_dynamic(&sc->sc_ev_rwt, EVCNT_TYPE_INTR,
1374 &sc->sc_ev_status, device_xname(sc->sc_dev), "rwt");
1375 evcnt_attach_dynamic(&sc->sc_ev_excol, EVCNT_TYPE_INTR,
1376 &sc->sc_ev_status, device_xname(sc->sc_dev), "excol");
1377 evcnt_attach_dynamic(&sc->sc_ev_lcol, EVCNT_TYPE_INTR,
1378 &sc->sc_ev_status, device_xname(sc->sc_dev), "lcol");
1379 evcnt_attach_dynamic(&sc->sc_ev_exdef, EVCNT_TYPE_INTR,
1380 &sc->sc_ev_status, device_xname(sc->sc_dev), "exdef");
1381 evcnt_attach_dynamic(&sc->sc_ev_lcarr, EVCNT_TYPE_INTR,
1382 &sc->sc_ev_status, device_xname(sc->sc_dev), "lcarr");
1383 evcnt_attach_dynamic(&sc->sc_ev_ncarr, EVCNT_TYPE_INTR,
1384 &sc->sc_ev_status, device_xname(sc->sc_dev), "ncarr");
1385 evcnt_attach_dynamic(&sc->sc_ev_tjt, EVCNT_TYPE_INTR,
1386 &sc->sc_ev_status, device_xname(sc->sc_dev), "tjt");
1387
1388 /* Attach interface */
1389 if_attach(ifp);
1390 if_deferred_start_init(ifp, NULL);
1391
1392 /* Attach ethernet interface */
1393 ether_ifattach(ifp, eaddr);
1394
1395 rnd_attach_source(&sc->sc_rndsource, ifp->if_xname, RND_TYPE_NET,
1396 RND_FLAG_DEFAULT);
1397
1398 return 0;
1399 }
1400