dwc_eqos.c revision 1.45 1 /* $NetBSD: dwc_eqos.c,v 1.45 2026/05/30 09:50:35 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2022-2026 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 * TODO:
33 * Multiqueue support.
34 * Add watchdog timer.
35 * Add detach function.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: dwc_eqos.c,v 1.45 2026/05/30 09:50:35 jmcneill Exp $");
40
41 #include <sys/param.h>
42 #include <sys/bus.h>
43 #include <sys/device.h>
44 #include <sys/intr.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/mutex.h>
48 #include <sys/callout.h>
49 #include <sys/cprng.h>
50 #include <sys/evcnt.h>
51 #include <sys/sysctl.h>
52 #include <sys/kthread.h>
53 #include <sys/rndsource.h>
54
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_ether.h>
58 #include <net/if_media.h>
59 #include <net/bpf.h>
60
61 #include <dev/mii/miivar.h>
62
63 #include <dev/ic/dwc_eqos_reg.h>
64 #include <dev/ic/dwc_eqos_var.h>
65
66 #define EQOS_MAX_MTU 9000 /* up to 16364? but not tested */
67 #define EQOS_TXDMA_SIZE (EQOS_MAX_MTU + ETHER_HDR_LEN + ETHER_CRC_LEN)
68 #define EQOS_RXDMA_SIZE 2048 /* Fixed value by hardware */
69 CTASSERT(MCLBYTES >= EQOS_RXDMA_SIZE);
70
71 #ifdef EQOS_DEBUG
72 #define EDEB_NOTE (1U << 0)
73 #define EDEB_INTR (1U << 1)
74 #define EDEB_RXRING (1U << 2)
75 #define EDEB_TXRING (1U << 3)
76 unsigned int eqos_debug; /* Default value */
77 #define DPRINTF(FLAG, FORMAT, ...) \
78 if (sc->sc_debug & FLAG) \
79 device_printf(sc->sc_dev, "%s: " FORMAT, \
80 __func__, ##__VA_ARGS__)
81 #else
82 #define DPRINTF(FLAG, FORMAT, ...) ((void)0)
83 #endif
84
85 #define CALLOUT_FLAGS CALLOUT_MPSAFE
86
87 #define DESC_BOUNDARY ((sizeof(bus_size_t) > 4) ? (1ULL << 32) : 0)
88 #define DESC_ALIGN sizeof(struct eqos_dma_desc)
89 #define TX_DESC_COUNT EQOS_DMA_DESC_COUNT
90 #define TX_DESC_SIZE (TX_DESC_COUNT * DESC_ALIGN)
91 #define RX_DESC_COUNT EQOS_DMA_DESC_COUNT
92 #define RX_DESC_SIZE (RX_DESC_COUNT * DESC_ALIGN)
93 #define MII_BUSY_RETRY 1000
94
95 #define DESC_OFF(n) ((n) * sizeof(struct eqos_dma_desc))
96 #define TX_SKIP(n, o) (((n) + (o)) % TX_DESC_COUNT)
97 #define TX_NEXT(n) TX_SKIP(n, 1)
98 #define RX_NEXT(n) (((n) + 1) % RX_DESC_COUNT)
99
100 #define TX_MAX_SEGS 128
101
102 #define EQOS_LOCK(sc) mutex_enter(&(sc)->sc_lock)
103 #define EQOS_UNLOCK(sc) mutex_exit(&(sc)->sc_lock)
104 #define EQOS_ASSERT_LOCKED(sc) KASSERT(mutex_owned(&(sc)->sc_lock))
105
106 #define EQOS_TXLOCK(sc) mutex_enter(&(sc)->sc_txlock)
107 #define EQOS_TXUNLOCK(sc) mutex_exit(&(sc)->sc_txlock)
108 #define EQOS_ASSERT_TXLOCKED(sc) KASSERT(mutex_owned(&(sc)->sc_txlock))
109
110 #define EQOS_HW_FEATURE_ADDR64_32BIT(sc) \
111 (((sc)->sc_hw_feature[1] & GMAC_MAC_HW_FEATURE1_ADDR64_MASK) == \
112 GMAC_MAC_HW_FEATURE1_ADDR64_32BIT)
113
114
115 #define RD4(sc, reg) \
116 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
117 #define WR4(sc, reg, val) \
118 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
119
120 /*
121 * Request interrupt on completion for every "rx_batch" # packets.
122 */
123 int eqos_rx_batch = 16;
124
125 /*
126 * RX interrupt watchdog is triggered after "rx_wdt_count" * 256 system
127 * clock cycles.
128 */
129 int eqos_rx_wdt_count = 160;
130
131 static void eqos_init_sysctls(struct eqos_softc *);
132 static int eqos_sysctl_tx_cur_handler(SYSCTLFN_PROTO);
133 static int eqos_sysctl_tx_end_handler(SYSCTLFN_PROTO);
134 static int eqos_sysctl_rx_cur_handler(SYSCTLFN_PROTO);
135 static int eqos_sysctl_rx_end_handler(SYSCTLFN_PROTO);
136 #ifdef EQOS_DEBUG
137 static int eqos_sysctl_debug_handler(SYSCTLFN_PROTO);
138 #endif
139
140 static int
141 eqos_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
142 {
143 struct eqos_softc * const sc = device_private(dev);
144 uint32_t addr;
145 int retry;
146
147 addr = sc->sc_clock_range |
148 (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
149 (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
150 GMAC_MAC_MDIO_ADDRESS_GOC_READ | GMAC_MAC_MDIO_ADDRESS_GB;
151 WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
152
153 delay(10000);
154
155 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
156 addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
157 if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
158 *val = RD4(sc, GMAC_MAC_MDIO_DATA) & 0xFFFF;
159 break;
160 }
161 delay(10);
162 }
163 if (retry == 0) {
164 device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
165 phy, reg);
166 return ETIMEDOUT;
167 }
168
169 return 0;
170 }
171
172 static int
173 eqos_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
174 {
175 struct eqos_softc * const sc = device_private(dev);
176 uint32_t addr;
177 int retry;
178
179 WR4(sc, GMAC_MAC_MDIO_DATA, val);
180
181 addr = sc->sc_clock_range |
182 (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
183 (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
184 GMAC_MAC_MDIO_ADDRESS_GOC_WRITE | GMAC_MAC_MDIO_ADDRESS_GB;
185 WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
186
187 delay(10000);
188
189 for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
190 addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
191 if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
192 break;
193 }
194 delay(10);
195 }
196 if (retry == 0) {
197 device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
198 phy, reg);
199 return ETIMEDOUT;
200 }
201
202 return 0;
203 }
204
205 static void
206 eqos_update_link(struct eqos_softc *sc)
207 {
208 struct mii_data * const mii = &sc->sc_mii;
209 uint64_t baudrate;
210 uint32_t conf, flow;
211
212 baudrate = ifmedia_baudrate(mii->mii_media_active);
213
214 conf = RD4(sc, GMAC_MAC_CONFIGURATION);
215 switch (baudrate) {
216 case IF_Mbps(10):
217 conf |= GMAC_MAC_CONFIGURATION_PS;
218 conf &= ~GMAC_MAC_CONFIGURATION_FES;
219 break;
220 case IF_Mbps(100):
221 conf |= GMAC_MAC_CONFIGURATION_PS;
222 conf |= GMAC_MAC_CONFIGURATION_FES;
223 break;
224 case IF_Gbps(1):
225 conf &= ~GMAC_MAC_CONFIGURATION_PS;
226 conf &= ~GMAC_MAC_CONFIGURATION_FES;
227 break;
228 case IF_Mbps(2500ULL):
229 conf &= ~GMAC_MAC_CONFIGURATION_PS;
230 conf |= GMAC_MAC_CONFIGURATION_FES;
231 break;
232 }
233
234 /* Set duplex. */
235 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
236 conf |= GMAC_MAC_CONFIGURATION_DM;
237 } else {
238 conf &= ~GMAC_MAC_CONFIGURATION_DM;
239 }
240 WR4(sc, GMAC_MAC_CONFIGURATION, conf);
241
242 /* Set TX flow control. */
243 if (mii->mii_media_active & IFM_ETH_TXPAUSE) {
244 flow = GMAC_MAC_Q0_TX_FLOW_CTRL_TFE;
245 flow |= 0xFFFFU << GMAC_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT;
246 } else
247 flow = 0;
248 WR4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL, flow);
249
250 /* Set RX flow control. */
251 if (mii->mii_media_active & IFM_ETH_RXPAUSE)
252 flow = GMAC_MAC_RX_FLOW_CTRL_RFE;
253 else
254 flow = 0;
255 WR4(sc, GMAC_MAC_RX_FLOW_CTRL, flow);
256 }
257
258 static void
259 eqos_mii_statchg(struct ifnet *ifp)
260 {
261 struct eqos_softc * const sc = ifp->if_softc;
262
263 eqos_update_link(sc);
264 }
265
266 static void
267 eqos_dma_sync(struct eqos_softc *sc, bus_dmamap_t map,
268 u_int start, u_int end, u_int total, int flags)
269 {
270 if (end > start) {
271 bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
272 DESC_OFF(end) - DESC_OFF(start), flags);
273 } else {
274 bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
275 DESC_OFF(total) - DESC_OFF(start), flags);
276 if (end > 0) {
277 bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(0),
278 DESC_OFF(end) - DESC_OFF(0), flags);
279 }
280 }
281 }
282
283 static uint32_t
284 eqos_setup_txdesc(struct eqos_softc *sc, int index, int flags,
285 bus_addr_t paddr, u_int len, u_int total_len)
286 {
287 struct eqos_dma_desc desc;
288 uint32_t tdes2, tdes3;
289
290 DPRINTF(EDEB_TXRING, "preparing desc %u\n", index);
291
292 EQOS_ASSERT_TXLOCKED(sc);
293
294 if (paddr == 0 || len == 0) {
295 DPRINTF(EDEB_TXRING,
296 "tx for desc %u done!\n", index);
297 KASSERT(flags == 0);
298 tdes2 = 0;
299 tdes3 = 0;
300 --sc->sc_tx.queued;
301 } else {
302 tdes2 = 0;
303 tdes3 = flags;
304 ++sc->sc_tx.queued;
305 }
306
307 KASSERT(!EQOS_HW_FEATURE_ADDR64_32BIT(sc) ||
308 ((uint64_t)paddr >> 32) == 0);
309
310 desc.tdes0 = htole32((uint32_t)paddr);
311 desc.tdes1 = htole32((uint32_t)((uint64_t)paddr >> 32));
312 desc.tdes2 = htole32(tdes2 | len);
313 desc.tdes3 = htole32(tdes3 | total_len);
314
315 sc->sc_tx.desc_ring[index] = desc;
316
317 return tdes3 | total_len;
318 }
319
320 static int
321 eqos_setup_txbuf(struct eqos_softc *sc, int index, struct mbuf *m,
322 uint32_t *ptdes3)
323 {
324 bus_dma_segment_t *segs;
325 int error, nsegs, cur, i;
326 uint32_t flags, first_tdes3;
327 bool nospace;
328
329 DPRINTF(EDEB_TXRING, "preparing desc %u\n", index);
330
331 /* at least one descriptor free ? */
332 if (sc->sc_tx.queued >= TX_DESC_COUNT - 1)
333 return -1;
334
335 error = bus_dmamap_load_mbuf(sc->sc_dmat,
336 sc->sc_tx.buf_map[index].map, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
337 if (error == EFBIG) {
338 device_printf(sc->sc_dev,
339 "TX packet needs too many DMA segments, dropping...\n");
340 return -2;
341 }
342 if (error != 0) {
343 device_printf(sc->sc_dev,
344 "TX packet cannot be mapped, retried...\n");
345 return 0;
346 }
347
348 segs = sc->sc_tx.buf_map[index].map->dm_segs;
349 nsegs = sc->sc_tx.buf_map[index].map->dm_nsegs;
350
351 nospace = sc->sc_tx.queued >= TX_DESC_COUNT - nsegs;
352 if (nospace) {
353 bus_dmamap_unload(sc->sc_dmat,
354 sc->sc_tx.buf_map[index].map);
355 /* XXX coalesce and retry ? */
356 return -1;
357 }
358
359 bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.buf_map[index].map,
360 0, sc->sc_tx.buf_map[index].map->dm_mapsize, BUS_DMASYNC_PREWRITE);
361
362 /* stored in same index as loaded map */
363 sc->sc_tx.buf_map[index].mbuf = m;
364
365 flags = EQOS_TDES3_TX_FD | (ptdes3 == NULL ? EQOS_TDES3_TX_OWN : 0);
366 first_tdes3 = 0;
367
368 for (cur = index, i = 0; i < nsegs; i++) {
369 uint32_t tdes3;
370
371 if (i == nsegs - 1)
372 flags |= EQOS_TDES3_TX_LD;
373
374 tdes3 = eqos_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
375 segs[i].ds_len, m->m_pkthdr.len);
376 cur = TX_NEXT(cur);
377
378 if (i == 0) {
379 first_tdes3 = tdes3;
380 flags &= ~EQOS_TDES3_TX_FD;
381 }
382 flags |= EQOS_TDES3_TX_OWN;
383 }
384
385 if (ptdes3 != NULL) {
386 *ptdes3 = first_tdes3;
387 }
388
389 DPRINTF(EDEB_TXRING, "passing tx desc %u to hardware, cur: %u, "
390 "next: %u, queued: %u, owned: %u\n",
391 index, sc->sc_tx.cur, sc->sc_tx.next, sc->sc_tx.queued,
392 ptdes3 == NULL);
393
394 return nsegs;
395 }
396
397 static void
398 eqos_setup_rxdesc(struct eqos_softc *sc, int index, bus_addr_t paddr)
399 {
400 struct eqos_dma_desc desc;
401 uint32_t tdes3 = EQOS_TDES3_RX_OWN | EQOS_TDES3_RX_BUF1V;
402
403 if ((index % eqos_rx_batch) == 0) {
404 tdes3 |= EQOS_TDES3_RX_IOC;
405 }
406
407 DPRINTF(EDEB_RXRING, "preparing desc %u\n", index);
408
409 desc.tdes0 = htole32((uint32_t)paddr);
410 desc.tdes1 = htole32((uint32_t)((uint64_t)paddr >> 32));
411 desc.tdes2 = htole32(0);
412 desc.tdes3 = 0;
413
414 sc->sc_rx.desc_ring[index] = desc;
415 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.desc_map,
416 DESC_OFF(index), offsetof(struct eqos_dma_desc, tdes3),
417 BUS_DMASYNC_PREWRITE);
418 sc->sc_rx.desc_ring[index].tdes3 = htole32(tdes3);
419 }
420
421 static int
422 eqos_setup_rxbuf(struct eqos_softc *sc, int index, struct mbuf *m)
423 {
424 int error;
425
426 DPRINTF(EDEB_RXRING, "preparing desc %u\n", index);
427
428 #if MCLBYTES >= (EQOS_RXDMA_SIZE + ETHER_ALIGN)
429 m_adj(m, ETHER_ALIGN);
430 #endif
431
432 error = bus_dmamap_load_mbuf(sc->sc_dmat,
433 sc->sc_rx.buf_map[index].map, m, BUS_DMA_READ | BUS_DMA_NOWAIT);
434 if (error != 0)
435 return error;
436
437 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
438 0, sc->sc_rx.buf_map[index].map->dm_mapsize,
439 BUS_DMASYNC_PREREAD);
440
441 sc->sc_rx.buf_map[index].mbuf = m;
442
443 return 0;
444 }
445
446 static struct mbuf *
447 eqos_alloc_mbufcl(struct eqos_softc *sc)
448 {
449 struct mbuf *m;
450
451 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
452 if (m != NULL)
453 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
454
455 return m;
456 }
457
458 static void
459 eqos_enable_intr(struct eqos_softc *sc)
460 {
461
462 WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE,
463 GMAC_DMA_CHAN0_INTR_ENABLE_NIE |
464 GMAC_DMA_CHAN0_INTR_ENABLE_AIE |
465 GMAC_DMA_CHAN0_INTR_ENABLE_FBE |
466 GMAC_DMA_CHAN0_INTR_ENABLE_RIE);
467 }
468
469 static void
470 eqos_disable_intr(struct eqos_softc *sc)
471 {
472
473 WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, 0);
474 }
475
476 static void
477 eqos_tick(void *softc)
478 {
479 struct eqos_softc * const sc = softc;
480 struct mii_data * const mii = &sc->sc_mii;
481
482 EQOS_LOCK(sc);
483 mii_tick(mii);
484 if ((sc->sc_if_flags & IFF_RUNNING) != 0)
485 callout_schedule(&sc->sc_stat_ch, hz);
486 EQOS_UNLOCK(sc);
487 }
488
489 static uint32_t
490 eqos_bitrev32(uint32_t x)
491 {
492
493 x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
494 x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
495 x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
496 x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
497
498 return (x >> 16) | (x << 16);
499 }
500
501 static void
502 eqos_setup_rxfilter(struct eqos_softc *sc)
503 {
504 struct ethercom *ec = &sc->sc_ec;
505 struct ifnet * const ifp = &ec->ec_if;
506 uint32_t pfil, crc, hashreg, hashbit, hash[2];
507 struct ether_multi *enm;
508 struct ether_multistep step;
509 const uint8_t *eaddr;
510 uint32_t val;
511
512 EQOS_ASSERT_LOCKED(sc);
513
514 pfil = RD4(sc, GMAC_MAC_PACKET_FILTER);
515 pfil &= ~(GMAC_MAC_PACKET_FILTER_PR |
516 GMAC_MAC_PACKET_FILTER_PM |
517 GMAC_MAC_PACKET_FILTER_HMC |
518 GMAC_MAC_PACKET_FILTER_PCF_MASK);
519 hash[0] = hash[1] = ~0U;
520
521 ETHER_LOCK(ec);
522 if ((sc->sc_if_flags & IFF_PROMISC) != 0) {
523 ec->ec_flags |= ETHER_F_ALLMULTI;
524 pfil |= GMAC_MAC_PACKET_FILTER_PR |
525 GMAC_MAC_PACKET_FILTER_PCF_ALL;
526 } else {
527 pfil |= GMAC_MAC_PACKET_FILTER_HMC;
528 hash[0] = hash[1] = 0;
529 ec->ec_flags &= ~ETHER_F_ALLMULTI;
530 ETHER_FIRST_MULTI(step, ec, enm);
531 while (enm != NULL) {
532 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
533 ETHER_ADDR_LEN) != 0) {
534 ec->ec_flags |= ETHER_F_ALLMULTI;
535 pfil &= ~GMAC_MAC_PACKET_FILTER_HMC;
536 pfil |= GMAC_MAC_PACKET_FILTER_PM;
537 /*
538 * Shouldn't matter if we clear HMC but
539 * let's avoid using different values.
540 */
541 hash[0] = hash[1] = 0xffffffff;
542 break;
543 }
544 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
545 crc &= 0x7f;
546 crc = eqos_bitrev32(~crc) >> 26;
547 hashreg = (crc >> 5);
548 hashbit = (crc & 0x1f);
549 hash[hashreg] |= (1 << hashbit);
550 ETHER_NEXT_MULTI(step, enm);
551 }
552 }
553 ETHER_UNLOCK(ec);
554
555 /* Write our unicast address */
556 eaddr = CLLADDR(ifp->if_sadl);
557 val = eaddr[4] | (eaddr[5] << 8) | GMAC_MAC_ADDRESS0_HIGH_AE;
558 WR4(sc, GMAC_MAC_ADDRESS0_HIGH, val);
559 val = eaddr[0] | (eaddr[1] << 8) | (eaddr[2] << 16) |
560 (eaddr[3] << 24);
561 WR4(sc, GMAC_MAC_ADDRESS0_LOW, val);
562
563 /* Multicast hash filters */
564 WR4(sc, GMAC_MAC_HASH_TABLE_REG0, hash[0]);
565 WR4(sc, GMAC_MAC_HASH_TABLE_REG1, hash[1]);
566
567 DPRINTF(EDEB_NOTE, "writing new packet filter config "
568 "%08x, hash[1]=%08x, hash[0]=%08x\n", pfil, hash[1], hash[0]);
569 /* Packet filter config */
570 WR4(sc, GMAC_MAC_PACKET_FILTER, pfil);
571 }
572
573 static int
574 eqos_reset(struct eqos_softc *sc)
575 {
576 uint32_t val;
577 int retry;
578
579 WR4(sc, GMAC_DMA_MODE, GMAC_DMA_MODE_SWR);
580 for (retry = 2000; retry > 0; retry--) {
581 delay(1000);
582 val = RD4(sc, GMAC_DMA_MODE);
583 if ((val & GMAC_DMA_MODE_SWR) == 0) {
584 return 0;
585 }
586 }
587
588 device_printf(sc->sc_dev, "reset timeout!\n");
589 return ETIMEDOUT;
590 }
591
592 static void
593 eqos_init_rings(struct eqos_softc *sc, int qid)
594 {
595 sc->sc_tx.cur = sc->sc_tx.next = sc->sc_tx.queued = 0;
596
597 sc->sc_rx_discarding = false;
598 m_freem(sc->sc_rx_receiving_m);
599 sc->sc_rx_receiving_m = NULL;
600 sc->sc_rx_receiving_m_last = NULL;
601
602 WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR_HI,
603 (uint32_t)((uint64_t)sc->sc_tx.desc_ring_paddr >> 32));
604 WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR,
605 (uint32_t)sc->sc_tx.desc_ring_paddr);
606 WR4(sc, GMAC_DMA_CHAN0_TX_RING_LEN, TX_DESC_COUNT - 1);
607 DPRINTF(EDEB_TXRING, "tx ring paddr %lx with %u descriptors\n",
608 sc->sc_tx.desc_ring_paddr, TX_DESC_COUNT);
609
610 sc->sc_rx.cur = sc->sc_rx.next = sc->sc_rx.queued = 0;
611 WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR_HI,
612 (uint32_t)((uint64_t)sc->sc_rx.desc_ring_paddr >> 32));
613 WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR,
614 (uint32_t)sc->sc_rx.desc_ring_paddr);
615 WR4(sc, GMAC_DMA_CHAN0_RX_RING_LEN, RX_DESC_COUNT - 1);
616 WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
617 (uint32_t)sc->sc_rx.desc_ring_paddr +
618 DESC_OFF((sc->sc_rx.cur - 1) % RX_DESC_COUNT));
619 DPRINTF(EDEB_RXRING, "rx ring paddr %lx with %u descriptors\n",
620 sc->sc_rx.desc_ring_paddr, RX_DESC_COUNT);
621 }
622
623 static int
624 eqos_init_locked(struct eqos_softc *sc)
625 {
626 struct ifnet * const ifp = &sc->sc_ec.ec_if;
627 struct mii_data * const mii = &sc->sc_mii;
628 uint32_t val, tqs, rqs;
629
630 EQOS_ASSERT_LOCKED(sc);
631 EQOS_ASSERT_TXLOCKED(sc);
632
633 if ((ifp->if_flags & IFF_RUNNING) != 0)
634 return 0;
635
636 /* Setup TX/RX rings */
637 eqos_init_rings(sc, 0);
638
639 /* Setup RX filter */
640 sc->sc_if_flags = ifp->if_flags;
641 eqos_setup_rxfilter(sc);
642
643 WR4(sc, GMAC_MAC_1US_TIC_COUNTER, (sc->sc_csr_clock / 1000000) - 1);
644
645 /* RX interrupt watchdog timer */
646 WR4(sc, GMAC_DMA_CHAN0_RX_WATCHDOG,
647 GMAC_DMA_CHAN0_RX_WATCHDOG_RWTU_256 | eqos_rx_wdt_count);
648
649 /* Enable transmit and receive DMA */
650 val = RD4(sc, GMAC_DMA_CHAN0_CONTROL);
651 val &= ~GMAC_DMA_CHAN0_CONTROL_DSL_MASK;
652 val |= ((DESC_ALIGN - 16) / 8) << GMAC_DMA_CHAN0_CONTROL_DSL_SHIFT;
653 val |= GMAC_DMA_CHAN0_CONTROL_PBLX8;
654 WR4(sc, GMAC_DMA_CHAN0_CONTROL, val);
655 val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
656 val &= ~GMAC_DMA_CHAN0_TX_CONTROL_TXPBL_MASK;
657 val |= (sc->sc_dma_txpbl << GMAC_DMA_CHAN0_TX_CONTROL_TXPBL_SHIFT);
658 val |= GMAC_DMA_CHAN0_TX_CONTROL_OSP;
659 val |= GMAC_DMA_CHAN0_TX_CONTROL_START;
660 WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
661 val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
662 val &= ~(GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_MASK |
663 GMAC_DMA_CHAN0_RX_CONTROL_RXPBL_MASK);
664 val |= (MCLBYTES << GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_SHIFT);
665 val |= (sc->sc_dma_rxpbl << GMAC_DMA_CHAN0_RX_CONTROL_RXPBL_SHIFT);
666 val |= GMAC_DMA_CHAN0_RX_CONTROL_START;
667 WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
668
669 /* Disable counters */
670 WR4(sc, GMAC_MMC_CONTROL,
671 GMAC_MMC_CONTROL_CNTFREEZ |
672 GMAC_MMC_CONTROL_CNTPRST |
673 GMAC_MMC_CONTROL_CNTPRSTLVL);
674
675 /* Configure operation modes */
676 WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE,
677 GMAC_MTL_TXQ0_OPERATION_MODE_TSF |
678 GMAC_MTL_TXQ0_OPERATION_MODE_TXQEN_EN);
679 WR4(sc, GMAC_MTL_RXQ0_OPERATION_MODE,
680 GMAC_MTL_RXQ0_OPERATION_MODE_RSF |
681 GMAC_MTL_RXQ0_OPERATION_MODE_FEP |
682 GMAC_MTL_RXQ0_OPERATION_MODE_FUP);
683
684 /*
685 * TX/RX fifo size in hw_feature[1] are log2(n/128), and
686 * TQS/RQS in TXQ0/RXQ0_OPERATION_MODE are n/256-1.
687 */
688 tqs = (128 << __SHIFTOUT(sc->sc_hw_feature[1],
689 GMAC_MAC_HW_FEATURE1_TXFIFOSIZE) / 256) - 1;
690 val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
691 val &= ~GMAC_MTL_TXQ0_OPERATION_MODE_TQS;
692 val |= __SHIFTIN(tqs, GMAC_MTL_TXQ0_OPERATION_MODE_TQS);
693 WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE, val);
694
695 rqs = (128 << __SHIFTOUT(sc->sc_hw_feature[1],
696 GMAC_MAC_HW_FEATURE1_RXFIFOSIZE) / 256) - 1;
697 val = RD4(sc, GMAC_MTL_RXQ0_OPERATION_MODE);
698 val &= ~GMAC_MTL_RXQ0_OPERATION_MODE_RQS;
699 val |= __SHIFTIN(rqs, GMAC_MTL_RXQ0_OPERATION_MODE_RQS);
700 WR4(sc, GMAC_MTL_RXQ0_OPERATION_MODE, val);
701
702 /*
703 * Disable flow control.
704 * It'll be configured later from the negotiated result.
705 */
706 WR4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL, 0);
707 WR4(sc, GMAC_MAC_RX_FLOW_CTRL, 0);
708
709 /* set RX queue mode. must be in DCB mode. */
710 val = __SHIFTIN(GMAC_RXQ_CTRL0_EN_DCB, GMAC_RXQ_CTRL0_EN_MASK);
711 WR4(sc, GMAC_RXQ_CTRL0, val);
712
713 /* Enable transmitter and receiver */
714 val = RD4(sc, GMAC_MAC_CONFIGURATION);
715 val |= GMAC_MAC_CONFIGURATION_BE;
716 val |= GMAC_MAC_CONFIGURATION_JD;
717 val |= GMAC_MAC_CONFIGURATION_JE;
718 val |= GMAC_MAC_CONFIGURATION_DCRS;
719 val |= GMAC_MAC_CONFIGURATION_TE;
720 val |= GMAC_MAC_CONFIGURATION_RE;
721 WR4(sc, GMAC_MAC_CONFIGURATION, val);
722
723 /* Enable interrupts */
724 eqos_enable_intr(sc);
725
726 EQOS_ASSERT_TXLOCKED(sc);
727 sc->sc_txrunning = true;
728
729 ifp->if_flags |= IFF_RUNNING;
730 sc->sc_if_flags |= IFF_RUNNING;
731
732 mii_mediachg(mii);
733 callout_schedule(&sc->sc_stat_ch, hz);
734
735 return 0;
736 }
737
738 static int
739 eqos_init(struct ifnet *ifp)
740 {
741 struct eqos_softc * const sc = ifp->if_softc;
742 int error;
743
744 EQOS_LOCK(sc);
745 EQOS_TXLOCK(sc);
746 error = eqos_init_locked(sc);
747 EQOS_TXUNLOCK(sc);
748 EQOS_UNLOCK(sc);
749
750 return error;
751 }
752
753 static void
754 eqos_stop_locked(struct eqos_softc *sc, int disable)
755 {
756 struct ifnet * const ifp = &sc->sc_ec.ec_if;
757 uint32_t val;
758 int retry;
759
760 EQOS_ASSERT_LOCKED(sc);
761
762 EQOS_TXLOCK(sc);
763 sc->sc_txrunning = false;
764 EQOS_TXUNLOCK(sc);
765
766 callout_halt(&sc->sc_stat_ch, &sc->sc_lock);
767
768 mii_down(&sc->sc_mii);
769
770 /* Disable receiver */
771 val = RD4(sc, GMAC_MAC_CONFIGURATION);
772 val &= ~GMAC_MAC_CONFIGURATION_RE;
773 WR4(sc, GMAC_MAC_CONFIGURATION, val);
774
775 /* Stop receive DMA */
776 val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
777 val &= ~GMAC_DMA_CHAN0_RX_CONTROL_START;
778 WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
779
780 /* Stop transmit DMA */
781 val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
782 val &= ~GMAC_DMA_CHAN0_TX_CONTROL_START;
783 WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
784
785 if (disable) {
786 /* Flush data in the TX FIFO */
787 val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
788 val |= GMAC_MTL_TXQ0_OPERATION_MODE_FTQ;
789 WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE, val);
790 /* Wait for flush to complete */
791 for (retry = 10000; retry > 0; retry--) {
792 val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
793 if ((val & GMAC_MTL_TXQ0_OPERATION_MODE_FTQ) == 0) {
794 break;
795 }
796 delay(1);
797 }
798 if (retry == 0) {
799 device_printf(sc->sc_dev,
800 "timeout flushing TX queue\n");
801 }
802 }
803
804 /* Disable transmitter */
805 val = RD4(sc, GMAC_MAC_CONFIGURATION);
806 val &= ~GMAC_MAC_CONFIGURATION_TE;
807 WR4(sc, GMAC_MAC_CONFIGURATION, val);
808
809 /* Disable interrupts */
810 eqos_disable_intr(sc);
811
812 workqueue_wait(sc->sc_wq, &sc->sc_rxwork);
813
814 sc->sc_if_flags &= ~IFF_RUNNING;
815 ifp->if_flags &= ~IFF_RUNNING;
816 }
817
818 static void
819 eqos_stop(struct ifnet *ifp, int disable)
820 {
821 struct eqos_softc * const sc = ifp->if_softc;
822
823 EQOS_LOCK(sc);
824 eqos_stop_locked(sc, disable);
825 EQOS_UNLOCK(sc);
826 }
827
828 static void
829 eqos_rxintr(struct eqos_softc *sc, int qid)
830 {
831 struct ifnet * const ifp = &sc->sc_ec.ec_if;
832 int error, index, pkts = 0;
833 struct mbuf *m, *m0, *new_m, *mprev;
834 uint32_t tdes3;
835 bool discarding;
836
837 /* restore jumboframe context */
838 discarding = sc->sc_rx_discarding;
839 m0 = sc->sc_rx_receiving_m;
840 mprev = sc->sc_rx_receiving_m_last;
841
842 for (index = sc->sc_rx.cur; ; index = RX_NEXT(index)) {
843 eqos_dma_sync(sc, sc->sc_rx.desc_map,
844 index, index + 1, RX_DESC_COUNT,
845 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
846
847 tdes3 = le32toh(sc->sc_rx.desc_ring[index].tdes3);
848 if ((tdes3 & EQOS_TDES3_RX_OWN) != 0) {
849 break;
850 }
851
852 /* now discarding untill the last packet */
853 if (discarding)
854 goto rx_next;
855
856 if ((tdes3 & EQOS_TDES3_RX_CTXT) != 0)
857 goto rx_next; /* ignore receive context descriptor */
858
859 /* error packet? */
860 if ((tdes3 & (EQOS_TDES3_RX_CE | EQOS_TDES3_RX_RWT |
861 EQOS_TDES3_RX_OE | EQOS_TDES3_RX_RE |
862 EQOS_TDES3_RX_DE)) != 0) {
863 #ifdef EQOS_DEBUG
864 char buf[128];
865 snprintb(buf, sizeof(buf),
866 "\177\020"
867 "b\x1e" "CTXT\0" /* 30 */
868 "b\x18" "CE\0" /* 24 */
869 "b\x17" "GP\0" /* 23 */
870 "b\x16" "WDT\0" /* 22 */
871 "b\x15" "OE\0" /* 21 */
872 "b\x14" "RE\0" /* 20 */
873 "b\x13" "DE\0" /* 19 */
874 "b\x0f" "ES\0" /* 15 */
875 "\0", tdes3);
876 DPRINTF(EDEB_NOTE,
877 "rxdesc[%d].tdes3=%s\n", index, buf);
878 #endif
879 if_statinc(ifp, if_ierrors);
880 if (m0 != NULL) {
881 m_freem(m0);
882 m0 = mprev = NULL;
883 }
884 discarding = true;
885 goto rx_next;
886 }
887
888 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
889 0, sc->sc_rx.buf_map[index].map->dm_mapsize,
890 BUS_DMASYNC_POSTREAD);
891 m = sc->sc_rx.buf_map[index].mbuf;
892 new_m = eqos_alloc_mbufcl(sc);
893 if (new_m == NULL) {
894 /*
895 * cannot allocate new mbuf. discard this received
896 * packet, and reuse the mbuf for next.
897 */
898 if_statinc(ifp, if_ierrors);
899 if (m0 != NULL) {
900 /* also discard the halfway jumbo packet */
901 m_freem(m0);
902 m0 = mprev = NULL;
903 }
904 discarding = true;
905 goto rx_next;
906 }
907 bus_dmamap_unload(sc->sc_dmat,
908 sc->sc_rx.buf_map[index].map);
909 error = eqos_setup_rxbuf(sc, index, new_m);
910 if (error)
911 panic("%s: %s: unable to load RX mbuf. error=%d",
912 device_xname(sc->sc_dev), __func__, error);
913
914 if (m0 == NULL) {
915 m0 = m;
916 } else {
917 if (m->m_flags & M_PKTHDR)
918 m_remove_pkthdr(m);
919 mprev->m_next = m;
920 }
921 mprev = m;
922
923 if ((tdes3 & EQOS_TDES3_RX_LD) == 0) {
924 /* to be continued in the next segment */
925 m->m_len = EQOS_RXDMA_SIZE;
926 } else {
927 /* last segment */
928 uint32_t totallen = tdes3 & EQOS_TDES3_RX_LENGTH_MASK;
929 uint32_t mlen = totallen % EQOS_RXDMA_SIZE;
930 if (mlen == 0)
931 mlen = EQOS_RXDMA_SIZE;
932 m->m_len = mlen;
933 m0->m_pkthdr.len = totallen;
934 m_set_rcvif(m0, ifp);
935 m0->m_flags |= M_HASFCS;
936 m0->m_nextpkt = NULL;
937 if_percpuq_enqueue(ifp->if_percpuq, m0);
938 m0 = mprev = NULL;
939
940 ++pkts;
941 }
942
943 rx_next:
944 if (discarding && (tdes3 & EQOS_TDES3_RX_LD) != 0)
945 discarding = false;
946
947 eqos_setup_rxdesc(sc, index,
948 sc->sc_rx.buf_map[index].map->dm_segs[0].ds_addr);
949 eqos_dma_sync(sc, sc->sc_rx.desc_map,
950 index, index + 1, RX_DESC_COUNT,
951 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
952 }
953 /* save jumboframe context */
954 sc->sc_rx_discarding = discarding;
955 sc->sc_rx_receiving_m = m0;
956 sc->sc_rx_receiving_m_last = mprev;
957
958 DPRINTF(EDEB_RXRING, "sc_rx.cur %u -> %u\n",
959 sc->sc_rx.cur, index);
960 sc->sc_rx.cur = index;
961
962 WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
963 (uint32_t)sc->sc_rx.desc_ring_paddr +
964 DESC_OFF(sc->sc_rx.cur));
965
966 if (pkts != 0) {
967 rnd_add_uint32(&sc->sc_rndsource, pkts);
968 }
969 }
970
971 static void
972 eqos_txintr(struct eqos_softc *sc, int qid)
973 {
974 struct ifnet * const ifp = &sc->sc_ec.ec_if;
975 struct eqos_bufmap *bmap;
976 struct eqos_dma_desc *desc;
977 uint32_t tdes3;
978 int i, pkts = 0;
979
980 DPRINTF(EDEB_INTR, "qid: %u\n", qid);
981
982 EQOS_ASSERT_TXLOCKED(sc);
983
984 eqos_dma_sync(sc, sc->sc_tx.desc_map,
985 sc->sc_tx.next, sc->sc_tx.cur, TX_DESC_COUNT,
986 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
987
988 for (i = sc->sc_tx.next; sc->sc_tx.queued > 0; i = TX_NEXT(i)) {
989 KASSERT(sc->sc_tx.queued > 0);
990 KASSERT(sc->sc_tx.queued <= TX_DESC_COUNT);
991
992 desc = &sc->sc_tx.desc_ring[i];
993 tdes3 = le32toh(desc->tdes3);
994 if ((tdes3 & EQOS_TDES3_TX_OWN) != 0) {
995 break;
996 }
997 bmap = &sc->sc_tx.buf_map[i];
998 if (bmap->mbuf != NULL) {
999 bus_dmamap_sync(sc->sc_dmat, bmap->map,
1000 0, bmap->map->dm_mapsize,
1001 BUS_DMASYNC_POSTWRITE);
1002 bus_dmamap_unload(sc->sc_dmat, bmap->map);
1003 m_freem(bmap->mbuf);
1004 bmap->mbuf = NULL;
1005 ++pkts;
1006 }
1007
1008 --sc->sc_tx.queued;
1009
1010 /* Last descriptor in a packet contains DMA status */
1011 if ((tdes3 & EQOS_TDES3_TX_LD) != 0) {
1012 if ((tdes3 & EQOS_TDES3_TX_DE) != 0) {
1013 device_printf(sc->sc_dev,
1014 "TX [%u] desc error: 0x%08x\n",
1015 i, tdes3);
1016 if_statinc(ifp, if_oerrors);
1017 } else if ((tdes3 & EQOS_TDES3_TX_ES) != 0) {
1018 device_printf(sc->sc_dev,
1019 "TX [%u] tx error: 0x%08x\n",
1020 i, tdes3);
1021 if_statinc(ifp, if_oerrors);
1022 } else {
1023 if_statinc(ifp, if_opackets);
1024 }
1025 }
1026
1027 }
1028
1029 sc->sc_tx.next = i;
1030
1031 if (pkts != 0) {
1032 rnd_add_uint32(&sc->sc_rndsource, pkts);
1033 }
1034 }
1035
1036 static void
1037 eqos_start_locked(struct eqos_softc *sc)
1038 {
1039 struct ifnet * const ifp = &sc->sc_ec.ec_if;
1040 struct mbuf *m;
1041 int cnt, nsegs, start;
1042 uint32_t first_tdes3 = 0;
1043 uint32_t *ptdes3 = &first_tdes3;
1044
1045 EQOS_ASSERT_TXLOCKED(sc);
1046
1047 if (!sc->sc_txrunning)
1048 return;
1049
1050 for (cnt = 0, start = sc->sc_tx.cur; ; cnt++) {
1051 if (sc->sc_tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
1052 DPRINTF(EDEB_TXRING, "%u sc_tx.queued, ring full\n",
1053 sc->sc_tx.queued);
1054 break;
1055 }
1056
1057 IFQ_POLL(&ifp->if_snd, m);
1058 if (m == NULL)
1059 break;
1060
1061 nsegs = eqos_setup_txbuf(sc, sc->sc_tx.cur, m, ptdes3);
1062 if (nsegs <= 0) {
1063 DPRINTF(EDEB_TXRING, "eqos_setup_txbuf failed "
1064 "with %d\n", nsegs);
1065 if (nsegs == -2) {
1066 IFQ_DEQUEUE(&ifp->if_snd, m);
1067 m_freem(m);
1068 continue;
1069 }
1070 break;
1071 }
1072
1073 IFQ_DEQUEUE(&ifp->if_snd, m);
1074 bpf_mtap(ifp, m, BPF_D_OUT);
1075
1076 sc->sc_tx.cur = TX_SKIP(sc->sc_tx.cur, nsegs);
1077 ptdes3 = NULL;
1078 }
1079
1080 DPRINTF(EDEB_TXRING, "tx loop -> cnt = %u, cur: %u, next: %u, "
1081 "queued: %u\n", cnt, sc->sc_tx.cur, sc->sc_tx.next,
1082 sc->sc_tx.queued);
1083
1084 if (cnt != 0) {
1085 eqos_dma_sync(sc, sc->sc_tx.desc_map,
1086 start, sc->sc_tx.cur, TX_DESC_COUNT,
1087 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1088
1089 /*
1090 * Defer setting OWN bit on the first descriptor until all
1091 * descriptors have been updated. The hardware will not try to
1092 * process any descriptors past the first one still owned by
1093 * software (i.e., with the OWN bit clear).
1094 */
1095 sc->sc_tx.desc_ring[start].tdes3 =
1096 htole32(first_tdes3 | EQOS_TDES3_TX_OWN);
1097 bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.desc_map,
1098 DESC_OFF(start) + offsetof(struct eqos_dma_desc, tdes3),
1099 sizeof(uint32_t),
1100 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1101
1102 /* Start and run TX DMA */
1103 DPRINTF(EDEB_TXRING, "sending desc %u at %lx upto "
1104 "%u-1 at %lx cur tx desc: %x cur tx buf: %x\n", start,
1105 (uint32_t)sc->sc_tx.desc_ring_paddr + DESC_OFF(start),
1106 sc->sc_tx.cur,
1107 (uint32_t)sc->sc_tx.desc_ring_paddr +
1108 DESC_OFF(sc->sc_tx.cur),
1109 RD4(sc, GMAC_DMA_CHAN0_CUR_TX_DESC),
1110 RD4(sc, GMAC_DMA_CHAN0_CUR_TX_BUF_ADDR));
1111 WR4(sc, GMAC_DMA_CHAN0_TX_END_ADDR,
1112 (uint32_t)sc->sc_tx.desc_ring_paddr +
1113 DESC_OFF(sc->sc_tx.cur));
1114 }
1115 }
1116
1117 static void
1118 eqos_start(struct ifnet *ifp)
1119 {
1120 struct eqos_softc * const sc = ifp->if_softc;
1121
1122 EQOS_LOCK(sc);
1123 cv_signal(&sc->sc_startcv);
1124 EQOS_UNLOCK(sc);
1125 }
1126
1127 static void
1128 eqos_receive(struct work *wk, void *arg)
1129 {
1130 struct eqos_softc *sc = arg;
1131 uint32_t dma_intr_enable;
1132
1133 EQOS_LOCK(sc);
1134
1135 eqos_rxintr(sc, 0);
1136
1137 dma_intr_enable = RD4(sc, GMAC_DMA_CHAN0_INTR_ENABLE);
1138 dma_intr_enable |= GMAC_DMA_CHAN0_STATUS_RI;
1139 WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, dma_intr_enable);
1140
1141 EQOS_UNLOCK(sc);
1142 }
1143
1144 static void
1145 eqos_transmit(void *arg)
1146 {
1147 struct eqos_softc *sc = arg;
1148
1149 EQOS_TXLOCK(sc);
1150 for (;;) {
1151 cv_wait(&sc->sc_startcv, &sc->sc_txlock);
1152
1153 eqos_txintr(sc, 0);
1154 eqos_start_locked(sc);
1155 }
1156
1157 EQOS_TXUNLOCK(sc);
1158
1159 kthread_exit(0);
1160 }
1161
1162 static void
1163 eqos_intr_mtl(struct eqos_softc *sc, uint32_t mtl_status)
1164 {
1165 uint32_t debug_data __unused = 0, ictrl = 0;
1166
1167 if (mtl_status == 0)
1168 return;
1169
1170 /* Drain the errors reported by MTL_INTERRUPT_STATUS */
1171 sc->sc_ev_mtl.ev_count++;
1172
1173 if ((mtl_status & GMAC_MTL_INTERRUPT_STATUS_DBGIS) != 0) {
1174 debug_data = RD4(sc, GMAC_MTL_FIFO_DEBUG_DATA);
1175 sc->sc_ev_mtl_debugdata.ev_count++;
1176 }
1177 if ((mtl_status & GMAC_MTL_INTERRUPT_STATUS_Q0IS) != 0) {
1178 uint32_t new_status = 0;
1179
1180 ictrl = RD4(sc, GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS);
1181 if ((ictrl & GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOVFIS) != 0) {
1182 new_status |= GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOVFIS;
1183 sc->sc_ev_mtl_rxovfis.ev_count++;
1184 }
1185 if ((ictrl & GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUNFIS) != 0) {
1186 new_status |= GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUNFIS;
1187 sc->sc_ev_mtl_txovfis.ev_count++;
1188 }
1189 if (new_status) {
1190 new_status |= (ictrl &
1191 (GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOIE |
1192 GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUIE));
1193 WR4(sc, GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS, new_status);
1194 }
1195 }
1196 DPRINTF(EDEB_INTR,
1197 "GMAC_MTL_INTERRUPT_STATUS = 0x%08X, "
1198 "GMAC_MTL_FIFO_DEBUG_DATA = 0x%08X, "
1199 "GMAC_MTL_INTERRUPT_STATUS_Q0IS = 0x%08X\n",
1200 mtl_status, debug_data, ictrl);
1201 }
1202
1203 int
1204 eqos_intr(void *arg)
1205 {
1206 struct eqos_softc * const sc = arg;
1207 uint32_t mac_status, mtl_status, dma_status, dma_intr_enable,
1208 rx_tx_status;
1209
1210 EQOS_LOCK(sc);
1211
1212 sc->sc_ev_intr.ev_count++;
1213
1214 mac_status = RD4(sc, GMAC_MAC_INTERRUPT_STATUS);
1215 mac_status &= RD4(sc, GMAC_MAC_INTERRUPT_ENABLE);
1216
1217 if (mac_status) {
1218 sc->sc_ev_mac.ev_count++;
1219 DPRINTF(EDEB_INTR,
1220 "GMAC_MAC_INTERRUPT_STATUS = 0x%08X\n", mac_status);
1221 }
1222
1223 mtl_status = RD4(sc, GMAC_MTL_INTERRUPT_STATUS);
1224 eqos_intr_mtl(sc, mtl_status);
1225
1226 dma_intr_enable = RD4(sc, GMAC_DMA_CHAN0_INTR_ENABLE);
1227 dma_status = RD4(sc, GMAC_DMA_CHAN0_STATUS);
1228 dma_status &= dma_intr_enable;
1229 if (dma_status) {
1230 WR4(sc, GMAC_DMA_CHAN0_STATUS, dma_status);
1231 }
1232
1233 if ((dma_status & GMAC_DMA_CHAN0_STATUS_RI) != 0) {
1234 dma_intr_enable &= ~GMAC_DMA_CHAN0_STATUS_RI;
1235 WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, dma_intr_enable);
1236 workqueue_enqueue(sc->sc_wq, &sc->sc_rxwork, NULL);
1237 sc->sc_ev_rxintr.ev_count++;
1238 }
1239
1240 rx_tx_status = RD4(sc, GMAC_MAC_RX_TX_STATUS);
1241
1242 EQOS_UNLOCK(sc);
1243
1244 if ((mac_status | mtl_status | dma_status) == 0) {
1245 DPRINTF(EDEB_NOTE, "spurious interrupt?!\n");
1246 }
1247
1248 if (rx_tx_status) {
1249 sc->sc_ev_status.ev_count++;
1250 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_RWT) != 0)
1251 sc->sc_ev_rwt.ev_count++;
1252 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXCOL) != 0)
1253 sc->sc_ev_excol.ev_count++;
1254 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCOL) != 0)
1255 sc->sc_ev_lcol.ev_count++;
1256 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXDEF) != 0)
1257 sc->sc_ev_exdef.ev_count++;
1258 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCARR) != 0)
1259 sc->sc_ev_lcarr.ev_count++;
1260 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_NCARR) != 0)
1261 sc->sc_ev_ncarr.ev_count++;
1262 if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_TJT) != 0)
1263 sc->sc_ev_tjt.ev_count++;
1264
1265 DPRINTF(EDEB_INTR, "GMAC_MAC_RX_TX_STATUS = 1x%08x\n",
1266 rx_tx_status);
1267 }
1268
1269 return 1;
1270 }
1271
1272 static int
1273 eqos_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1274 {
1275 struct eqos_softc * const sc = ifp->if_softc;
1276 int error;
1277
1278 switch (cmd) {
1279 case SIOCADDMULTI:
1280 case SIOCDELMULTI:
1281 break;
1282 default:
1283 KASSERT(IFNET_LOCKED(ifp));
1284 }
1285
1286 switch (cmd) {
1287 case SIOCSIFMTU: {
1288 struct ifreq * const ifr = (struct ifreq *)data;
1289 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > EQOS_MAX_MTU) {
1290 error = EINVAL;
1291 } else {
1292 ifp->if_mtu = ifr->ifr_mtu;
1293 error = 0; /* no need ENETRESET */
1294 }
1295 break;
1296 }
1297 default: {
1298 const int s = splnet();
1299 error = ether_ioctl(ifp, cmd, data);
1300 splx(s);
1301
1302 if (error != ENETRESET)
1303 break;
1304
1305 error = 0;
1306
1307 if (cmd == SIOCSIFCAP)
1308 error = (*ifp->if_init)(ifp);
1309 else if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
1310 EQOS_LOCK(sc);
1311 if ((sc->sc_if_flags & IFF_RUNNING) != 0)
1312 eqos_setup_rxfilter(sc);
1313 EQOS_UNLOCK(sc);
1314 }
1315 break;
1316 }
1317 }
1318
1319 return error;
1320 }
1321
1322 static int
1323 eqos_ifflags_cb(struct ethercom *ec)
1324 {
1325 struct ifnet * const ifp = &ec->ec_if;
1326 struct eqos_softc * const sc = ifp->if_softc;
1327 int ret = 0;
1328
1329 KASSERT(IFNET_LOCKED(ifp));
1330 EQOS_LOCK(sc);
1331
1332 u_short change = ifp->if_flags ^ sc->sc_if_flags;
1333 sc->sc_if_flags = ifp->if_flags;
1334
1335 if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
1336 ret = ENETRESET;
1337 } else if ((change & IFF_PROMISC) != 0) {
1338 if ((sc->sc_if_flags & IFF_RUNNING) != 0)
1339 eqos_setup_rxfilter(sc);
1340 }
1341 EQOS_UNLOCK(sc);
1342
1343 return ret;
1344 }
1345
1346
1347 static void
1348 eqos_get_eaddr(struct eqos_softc *sc, uint8_t *eaddr)
1349 {
1350 uint32_t maclo, machi;
1351
1352 if (ether_getaddr(sc->sc_dev, eaddr)) {
1353 return;
1354 }
1355
1356 maclo = RD4(sc, GMAC_MAC_ADDRESS0_LOW);
1357 machi = RD4(sc, GMAC_MAC_ADDRESS0_HIGH) & 0xFFFF;
1358 if ((maclo & 0x00000001) != 0) {
1359 aprint_error_dev(sc->sc_dev,
1360 "Wrong MAC address. Clearing the multicast bit.\n");
1361 maclo &= ~0x00000001;
1362 }
1363
1364 if (maclo == 0xFFFFFFFF && machi == 0xFFFF) {
1365 /* Create one */
1366 maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
1367 machi = cprng_strong32() & 0xffff;
1368 }
1369
1370 eaddr[0] = maclo & 0xff;
1371 eaddr[1] = (maclo >> 8) & 0xff;
1372 eaddr[2] = (maclo >> 16) & 0xff;
1373 eaddr[3] = (maclo >> 24) & 0xff;
1374 eaddr[4] = machi & 0xff;
1375 eaddr[5] = (machi >> 8) & 0xff;
1376 }
1377
1378 static void
1379 eqos_get_dma_pbl(struct eqos_softc *sc)
1380 {
1381 prop_dictionary_t prop = device_properties(sc->sc_dev);
1382 uint32_t pbl;
1383
1384 /* Set default values. */
1385 sc->sc_dma_txpbl = sc->sc_dma_rxpbl = EQOS_DMA_PBL_DEFAULT;
1386
1387 /* Get values from props. */
1388 if (prop_dictionary_get_uint32(prop, "snps,pbl", &pbl) && pbl)
1389 sc->sc_dma_txpbl = sc->sc_dma_rxpbl = pbl;
1390 if (prop_dictionary_get_uint32(prop, "snps,txpbl", &pbl) && pbl)
1391 sc->sc_dma_txpbl = pbl;
1392 if (prop_dictionary_get_uint32(prop, "snps,rxpbl", &pbl) && pbl)
1393 sc->sc_dma_rxpbl = pbl;
1394 }
1395
1396 static void
1397 eqos_axi_configure(struct eqos_softc *sc)
1398 {
1399 uint32_t val;
1400 u_int uival;
1401
1402 val = RD4(sc, GMAC_DMA_SYSBUS_MODE);
1403
1404 /* XXX are MB and FB mutually-exclusive? XXX */
1405 if (device_getprop_bool(sc->sc_dev, "snps,mixed-burst")) {
1406 val |= GMAC_DMA_SYSBUS_MODE_MB;
1407 }
1408 if (device_getprop_bool(sc->sc_dev, "snps,fixed-burst")) {
1409 val |= GMAC_DMA_SYSBUS_MODE_FB;
1410 }
1411 if (device_getprop_uint(sc->sc_dev, "snps,wr_osr_lmt", &uival)) {
1412 val &= ~GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_MASK;
1413 val |= (uival << GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_SHIFT) &
1414 GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_MASK;
1415 }
1416 if (device_getprop_uint(sc->sc_dev, "snps,rd_osr_lmt", &uival)) {
1417 val &= ~GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK;
1418 val |= (uival << GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) &
1419 GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK;
1420 }
1421
1422 if (!EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
1423 val |= GMAC_DMA_SYSBUS_MODE_EAME;
1424 }
1425
1426 /* XXX snps,kbbe XXX */
1427 /* XXX snps,blen XXX */
1428
1429 /* XXX */
1430 val |= GMAC_DMA_SYSBUS_MODE_BLEN16;
1431 val |= GMAC_DMA_SYSBUS_MODE_BLEN8;
1432 val |= GMAC_DMA_SYSBUS_MODE_BLEN4;
1433
1434 WR4(sc, GMAC_DMA_SYSBUS_MODE, val);
1435 }
1436
1437 static int
1438 eqos_setup_dma(struct eqos_softc *sc, int qid)
1439 {
1440 struct mbuf *m;
1441 int error, nsegs, i;
1442
1443 /* Set back pointer */
1444 sc->sc_tx.sc = sc;
1445 sc->sc_rx.sc = sc;
1446
1447 /* Setup TX ring */
1448 error = bus_dmamap_create(sc->sc_dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE,
1449 DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_tx.desc_map);
1450 if (error) {
1451 return error;
1452 }
1453 error = bus_dmamem_alloc(sc->sc_dmat, TX_DESC_SIZE, DESC_ALIGN,
1454 DESC_BOUNDARY, &sc->sc_tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1455 if (error) {
1456 return error;
1457 }
1458 error = bus_dmamem_map(sc->sc_dmat, &sc->sc_tx.desc_dmaseg, nsegs,
1459 TX_DESC_SIZE, (void *)&sc->sc_tx.desc_ring,
1460 BUS_DMA_WAITOK | BUS_DMA_COHERENT);
1461 if (error) {
1462 return error;
1463 }
1464 error = bus_dmamap_load(sc->sc_dmat, sc->sc_tx.desc_map,
1465 sc->sc_tx.desc_ring, TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1466 if (error) {
1467 return error;
1468 }
1469 sc->sc_tx.desc_ring_paddr = sc->sc_tx.desc_map->dm_segs[0].ds_addr;
1470
1471 memset(sc->sc_tx.desc_ring, 0, TX_DESC_SIZE);
1472 bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.desc_map, 0, TX_DESC_SIZE,
1473 BUS_DMASYNC_PREWRITE);
1474
1475 sc->sc_tx.queued = TX_DESC_COUNT;
1476 for (i = 0; i < TX_DESC_COUNT; i++) {
1477 error = bus_dmamap_create(sc->sc_dmat, EQOS_TXDMA_SIZE,
1478 TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
1479 &sc->sc_tx.buf_map[i].map);
1480 if (error != 0) {
1481 device_printf(sc->sc_dev,
1482 "cannot create TX buffer map\n");
1483 return error;
1484 }
1485 EQOS_TXLOCK(sc);
1486 eqos_setup_txdesc(sc, i, 0, 0, 0, 0);
1487 EQOS_TXUNLOCK(sc);
1488 }
1489
1490 /* Setup RX ring */
1491 error = bus_dmamap_create(sc->sc_dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE,
1492 DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_rx.desc_map);
1493 if (error) {
1494 return error;
1495 }
1496 error = bus_dmamem_alloc(sc->sc_dmat, RX_DESC_SIZE, DESC_ALIGN,
1497 DESC_BOUNDARY, &sc->sc_rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
1498 if (error) {
1499 return error;
1500 }
1501 error = bus_dmamem_map(sc->sc_dmat, &sc->sc_rx.desc_dmaseg, nsegs,
1502 RX_DESC_SIZE, (void *)&sc->sc_rx.desc_ring,
1503 BUS_DMA_WAITOK | BUS_DMA_COHERENT);
1504 if (error) {
1505 return error;
1506 }
1507 error = bus_dmamap_load(sc->sc_dmat, sc->sc_rx.desc_map,
1508 sc->sc_rx.desc_ring, RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
1509 if (error) {
1510 return error;
1511 }
1512 sc->sc_rx.desc_ring_paddr = sc->sc_rx.desc_map->dm_segs[0].ds_addr;
1513
1514 memset(sc->sc_rx.desc_ring, 0, RX_DESC_SIZE);
1515
1516 for (i = 0; i < RX_DESC_COUNT; i++) {
1517 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
1518 RX_DESC_COUNT, MCLBYTES, 0, BUS_DMA_WAITOK,
1519 &sc->sc_rx.buf_map[i].map);
1520 if (error != 0) {
1521 device_printf(sc->sc_dev,
1522 "cannot create RX buffer map\n");
1523 return error;
1524 }
1525 if ((m = eqos_alloc_mbufcl(sc)) == NULL) {
1526 device_printf(sc->sc_dev, "cannot allocate RX mbuf\n");
1527 return ENOMEM;
1528 }
1529 error = eqos_setup_rxbuf(sc, i, m);
1530 if (error != 0) {
1531 device_printf(sc->sc_dev, "cannot create RX buffer\n");
1532 return error;
1533 }
1534 eqos_setup_rxdesc(sc, i,
1535 sc->sc_rx.buf_map[i].map->dm_segs[0].ds_addr);
1536 }
1537 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.desc_map,
1538 0, sc->sc_rx.desc_map->dm_mapsize,
1539 BUS_DMASYNC_PREWRITE);
1540
1541 aprint_debug_dev(sc->sc_dev, "TX ring @ 0x%lX, RX ring @ 0x%lX\n",
1542 sc->sc_tx.desc_ring_paddr, sc->sc_rx.desc_ring_paddr);
1543
1544 return 0;
1545 }
1546
1547 int
1548 eqos_attach(struct eqos_softc *sc)
1549 {
1550 struct mii_data * const mii = &sc->sc_mii;
1551 struct ifnet * const ifp = &sc->sc_ec.ec_if;
1552 uint8_t eaddr[ETHER_ADDR_LEN];
1553 u_int userver, snpsver;
1554 int error;
1555 int n;
1556
1557 #ifdef EQOS_DEBUG
1558 /* Load the default debug flags. */
1559 sc->sc_debug = eqos_debug;
1560 #endif
1561
1562 const uint32_t ver = RD4(sc, GMAC_MAC_VERSION);
1563 userver = (ver & GMAC_MAC_VERSION_USERVER_MASK) >>
1564 GMAC_MAC_VERSION_USERVER_SHIFT;
1565 snpsver = ver & GMAC_MAC_VERSION_SNPSVER_MASK;
1566
1567 if ((snpsver < 0x51) || (snpsver > 0x52)) {
1568 aprint_error(": EQOS version 0x%02x not supported\n",
1569 snpsver);
1570 return ENXIO;
1571 }
1572
1573 if (sc->sc_csr_clock < 20000000) {
1574 aprint_error(": CSR clock too low\n");
1575 return EINVAL;
1576 } else if (sc->sc_csr_clock < 35000000) {
1577 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_20_35;
1578 } else if (sc->sc_csr_clock < 60000000) {
1579 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_35_60;
1580 } else if (sc->sc_csr_clock < 100000000) {
1581 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_60_100;
1582 } else if (sc->sc_csr_clock < 150000000) {
1583 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_100_150;
1584 } else if (sc->sc_csr_clock < 250000000) {
1585 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_150_250;
1586 } else if (sc->sc_csr_clock < 300000000) {
1587 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_250_300;
1588 } else if (sc->sc_csr_clock < 500000000) {
1589 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_300_500;
1590 } else if (sc->sc_csr_clock < 800000000) {
1591 sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_500_800;
1592 } else {
1593 aprint_error(": CSR clock too high\n");
1594 return EINVAL;
1595 }
1596
1597 for (n = 0; n < 4; n++) {
1598 sc->sc_hw_feature[n] = RD4(sc, GMAC_MAC_HW_FEATURE(n));
1599 }
1600
1601 aprint_naive("\n");
1602 aprint_normal(": DesignWare EQOS ver 0x%02x (0x%02x)\n",
1603 snpsver, userver);
1604 aprint_verbose_dev(sc->sc_dev, "hw features %08x %08x %08x %08x\n",
1605 sc->sc_hw_feature[0], sc->sc_hw_feature[1],
1606 sc->sc_hw_feature[2], sc->sc_hw_feature[3]);
1607
1608 if (EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
1609 bus_dma_tag_t ntag;
1610
1611 error = bus_dmatag_subregion(sc->sc_dmat, 0, UINT32_MAX,
1612 &ntag, 0);
1613 if (error) {
1614 aprint_error_dev(sc->sc_dev,
1615 "failed to restrict DMA: %d\n", error);
1616 return error;
1617 }
1618 aprint_verbose_dev(sc->sc_dev, "using 32-bit DMA\n");
1619 sc->sc_dmat = ntag;
1620 }
1621
1622 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
1623 mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_NET);
1624 callout_init(&sc->sc_stat_ch, CALLOUT_MPSAFE);
1625 callout_setfunc(&sc->sc_stat_ch, eqos_tick, sc);
1626
1627 eqos_get_eaddr(sc, eaddr);
1628 aprint_normal_dev(sc->sc_dev,
1629 "Ethernet address %s\n", ether_sprintf(eaddr));
1630
1631 /* Soft reset EMAC core */
1632 error = eqos_reset(sc);
1633 if (error != 0) {
1634 return error;
1635 }
1636
1637 /* Get DMA burst length */
1638 eqos_get_dma_pbl(sc);
1639
1640 /* Configure AXI Bus mode parameters */
1641 eqos_axi_configure(sc);
1642
1643 /* Setup DMA descriptors */
1644 if (eqos_setup_dma(sc, 0) != 0) {
1645 aprint_error_dev(sc->sc_dev,
1646 "failed to setup DMA descriptors\n");
1647 return EINVAL;
1648 }
1649
1650 /* Receive workers */
1651 error = workqueue_create(&sc->sc_wq, device_xname(sc->sc_dev),
1652 eqos_receive, sc, PRI_SOFTNET, IPL_NET, WQ_MPSAFE | WQ_PERCPU);
1653 if (error != 0) {
1654 aprint_error_dev(sc->sc_dev,
1655 "failed to create receive workqueue: %d\n", error);
1656 return error;
1657 }
1658
1659 /* Transmit thread */
1660 cv_init(&sc->sc_startcv, "idle");
1661 error = kthread_create(PRI_SOFTNET, KTHREAD_MPSAFE, NULL,
1662 eqos_transmit, sc, &sc->sc_startlwp, "%s-tx",
1663 device_xname(sc->sc_dev));
1664 if (error != 0) {
1665 aprint_error_dev(sc->sc_dev,
1666 "failed to create transmit thread: %d\n", error);
1667 return error;
1668 }
1669
1670 /* Setup ethernet interface */
1671 ifp->if_softc = sc;
1672 snprintf(ifp->if_xname, IFNAMSIZ, "%s", device_xname(sc->sc_dev));
1673 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1674 ifp->if_extflags = IFEF_MPSAFE;
1675 ifp->if_start = eqos_start;
1676 ifp->if_ioctl = eqos_ioctl;
1677 ifp->if_init = eqos_init;
1678 ifp->if_stop = eqos_stop;
1679 ifp->if_capabilities = 0;
1680 ifp->if_capenable = ifp->if_capabilities;
1681 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
1682 IFQ_SET_READY(&ifp->if_snd);
1683
1684 /* 802.1Q VLAN-sized frames, and jumbo frame are supported */
1685 sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
1686 sc->sc_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
1687
1688 /* Attach MII driver */
1689 sc->sc_ec.ec_mii = mii;
1690 ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
1691 mii->mii_ifp = ifp;
1692 mii->mii_readreg = eqos_mii_readreg;
1693 mii->mii_writereg = eqos_mii_writereg;
1694 mii->mii_statchg = eqos_mii_statchg;
1695 mii_attach(sc->sc_dev, mii, 0xffffffff, sc->sc_phy_id, MII_OFFSET_ANY,
1696 MIIF_DOPAUSE);
1697
1698 if (LIST_EMPTY(&mii->mii_phys)) {
1699 aprint_error_dev(sc->sc_dev, "no PHY found!\n");
1700 return ENOENT;
1701 }
1702 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1703
1704 /* Master interrupt evcnt */
1705 evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
1706 NULL, device_xname(sc->sc_dev), "interrupts");
1707
1708 /* Per-interrupt type, using main interrupt */
1709 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
1710 &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxintr");
1711 evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
1712 &sc->sc_ev_intr, device_xname(sc->sc_dev), "txintr");
1713 evcnt_attach_dynamic(&sc->sc_ev_mac, EVCNT_TYPE_INTR,
1714 &sc->sc_ev_intr, device_xname(sc->sc_dev), "macstatus");
1715 evcnt_attach_dynamic(&sc->sc_ev_mtl, EVCNT_TYPE_INTR,
1716 &sc->sc_ev_intr, device_xname(sc->sc_dev), "intrstatus");
1717 evcnt_attach_dynamic(&sc->sc_ev_status, EVCNT_TYPE_INTR,
1718 &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxtxstatus");
1719
1720 /* MAC Status specific type, using macstatus interrupt */
1721 evcnt_attach_dynamic(&sc->sc_ev_mtl_debugdata, EVCNT_TYPE_INTR,
1722 &sc->sc_ev_mtl, device_xname(sc->sc_dev), "debugdata");
1723 evcnt_attach_dynamic(&sc->sc_ev_mtl_rxovfis, EVCNT_TYPE_INTR,
1724 &sc->sc_ev_mtl, device_xname(sc->sc_dev), "rxovfis");
1725 evcnt_attach_dynamic(&sc->sc_ev_mtl_txovfis, EVCNT_TYPE_INTR,
1726 &sc->sc_ev_mtl, device_xname(sc->sc_dev), "txovfis");
1727
1728 /* RX/TX Status specific type, using rxtxstatus interrupt */
1729 evcnt_attach_dynamic(&sc->sc_ev_rwt, EVCNT_TYPE_INTR,
1730 &sc->sc_ev_status, device_xname(sc->sc_dev), "rwt");
1731 evcnt_attach_dynamic(&sc->sc_ev_excol, EVCNT_TYPE_INTR,
1732 &sc->sc_ev_status, device_xname(sc->sc_dev), "excol");
1733 evcnt_attach_dynamic(&sc->sc_ev_lcol, EVCNT_TYPE_INTR,
1734 &sc->sc_ev_status, device_xname(sc->sc_dev), "lcol");
1735 evcnt_attach_dynamic(&sc->sc_ev_exdef, EVCNT_TYPE_INTR,
1736 &sc->sc_ev_status, device_xname(sc->sc_dev), "exdef");
1737 evcnt_attach_dynamic(&sc->sc_ev_lcarr, EVCNT_TYPE_INTR,
1738 &sc->sc_ev_status, device_xname(sc->sc_dev), "lcarr");
1739 evcnt_attach_dynamic(&sc->sc_ev_ncarr, EVCNT_TYPE_INTR,
1740 &sc->sc_ev_status, device_xname(sc->sc_dev), "ncarr");
1741 evcnt_attach_dynamic(&sc->sc_ev_tjt, EVCNT_TYPE_INTR,
1742 &sc->sc_ev_status, device_xname(sc->sc_dev), "tjt");
1743
1744 /* Attach interface */
1745 if_attach(ifp);
1746 if_deferred_start_init(ifp, NULL);
1747
1748 /* Attach ethernet interface */
1749 ether_ifattach(ifp, eaddr);
1750 ether_set_ifflags_cb(&sc->sc_ec, eqos_ifflags_cb);
1751
1752 eqos_init_sysctls(sc);
1753
1754 rnd_attach_source(&sc->sc_rndsource, ifp->if_xname, RND_TYPE_NET,
1755 RND_FLAG_DEFAULT);
1756
1757 return 0;
1758 }
1759
1760 static void
1761 eqos_init_sysctls(struct eqos_softc *sc)
1762 {
1763 struct sysctllog **log;
1764 const struct sysctlnode *rnode, *qnode, *cnode;
1765 const char *dvname;
1766 int i, rv;
1767
1768 log = &sc->sc_sysctllog;
1769 dvname = device_xname(sc->sc_dev);
1770
1771 rv = sysctl_createv(log, 0, NULL, &rnode,
1772 0, CTLTYPE_NODE, dvname,
1773 SYSCTL_DESCR("eqos information and settings"),
1774 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
1775 if (rv != 0)
1776 goto err;
1777
1778 for (i = 0; i < 1; i++) {
1779 struct eqos_ring *txr = &sc->sc_tx;
1780 struct eqos_ring *rxr = &sc->sc_rx;
1781 const unsigned char *name = "q0";
1782
1783 if (sysctl_createv(log, 0, &rnode, &qnode,
1784 0, CTLTYPE_NODE,
1785 name, SYSCTL_DESCR("Queue Name"),
1786 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
1787 break;
1788
1789 if (sysctl_createv(log, 0, &qnode, &cnode,
1790 CTLFLAG_READONLY, CTLTYPE_INT,
1791 "txs_cur", SYSCTL_DESCR("TX cur"),
1792 NULL, 0, &txr->cur,
1793 0, CTL_CREATE, CTL_EOL) != 0)
1794 break;
1795 if (sysctl_createv(log, 0, &qnode, &cnode,
1796 CTLFLAG_READONLY, CTLTYPE_INT,
1797 "txs_next", SYSCTL_DESCR("TX next"),
1798 NULL, 0, &txr->next,
1799 0, CTL_CREATE, CTL_EOL) != 0)
1800 break;
1801 if (sysctl_createv(log, 0, &qnode, &cnode,
1802 CTLFLAG_READONLY, CTLTYPE_INT,
1803 "txs_queued", SYSCTL_DESCR("TX queued"),
1804 NULL, 0, &txr->queued,
1805 0, CTL_CREATE, CTL_EOL) != 0)
1806 break;
1807 if (sysctl_createv(log, 0, &qnode, &cnode,
1808 CTLFLAG_READONLY, CTLTYPE_INT,
1809 "txr_cur", SYSCTL_DESCR("TX descriptor cur"),
1810 eqos_sysctl_tx_cur_handler, 0, (void *)txr,
1811 0, CTL_CREATE, CTL_EOL) != 0)
1812 break;
1813 if (sysctl_createv(log, 0, &qnode, &cnode,
1814 CTLFLAG_READONLY, CTLTYPE_INT,
1815 "txr_end", SYSCTL_DESCR("TX descriptor end"),
1816 eqos_sysctl_tx_end_handler, 0, (void *)txr,
1817 0, CTL_CREATE, CTL_EOL) != 0)
1818 break;
1819 if (sysctl_createv(log, 0, &qnode, &cnode,
1820 CTLFLAG_READONLY, CTLTYPE_INT,
1821 "rxs_cur", SYSCTL_DESCR("RX cur"),
1822 NULL, 0, &rxr->cur,
1823 0, CTL_CREATE, CTL_EOL) != 0)
1824 break;
1825 if (sysctl_createv(log, 0, &qnode, &cnode,
1826 CTLFLAG_READONLY, CTLTYPE_INT,
1827 "rxs_next", SYSCTL_DESCR("RX next"),
1828 NULL, 0, &rxr->next,
1829 0, CTL_CREATE, CTL_EOL) != 0)
1830 break;
1831 if (sysctl_createv(log, 0, &qnode, &cnode,
1832 CTLFLAG_READONLY, CTLTYPE_INT,
1833 "rxs_queued", SYSCTL_DESCR("RX queued"),
1834 NULL, 0, &rxr->queued,
1835 0, CTL_CREATE, CTL_EOL) != 0)
1836 break;
1837 if (sysctl_createv(log, 0, &qnode, &cnode,
1838 CTLFLAG_READONLY, CTLTYPE_INT,
1839 "rxr_cur", SYSCTL_DESCR("RX descriptor cur"),
1840 eqos_sysctl_rx_cur_handler, 0, (void *)rxr,
1841 0, CTL_CREATE, CTL_EOL) != 0)
1842 break;
1843 if (sysctl_createv(log, 0, &qnode, &cnode,
1844 CTLFLAG_READONLY, CTLTYPE_INT,
1845 "rxr_end", SYSCTL_DESCR("RX descriptor end"),
1846 eqos_sysctl_rx_end_handler, 0, (void *)rxr,
1847 0, CTL_CREATE, CTL_EOL) != 0)
1848 break;
1849 }
1850
1851 #ifdef EQOS_DEBUG
1852 rv = sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
1853 CTLTYPE_INT, "debug_flags",
1854 SYSCTL_DESCR(
1855 "Debug flags:\n" \
1856 "\t0x01 NOTE\n" \
1857 "\t0x02 INTR\n" \
1858 "\t0x04 RX RING\n" \
1859 "\t0x08 TX RING\n"),
1860 eqos_sysctl_debug_handler, 0, (void *)sc, 0, CTL_CREATE, CTL_EOL);
1861 #endif
1862
1863 return;
1864
1865 err:
1866 sc->sc_sysctllog = NULL;
1867 device_printf(sc->sc_dev, "%s: sysctl_createv failed, rv = %d\n",
1868 __func__, rv);
1869 }
1870
1871 static int
1872 eqos_sysctl_tx_cur_handler(SYSCTLFN_ARGS)
1873 {
1874 struct sysctlnode node = *rnode;
1875 struct eqos_ring *txq = (struct eqos_ring *)node.sysctl_data;
1876 struct eqos_softc *sc = txq->sc;
1877 uint32_t reg, index;
1878
1879 reg = RD4(sc, GMAC_DMA_CHAN0_CUR_TX_DESC);
1880 #if 0
1881 printf("head = %08x\n", (uint32_t)sc->sc_tx.desc_ring_paddr);
1882 printf("cdesc = %08x\n", reg);
1883 printf("index = %zu\n",
1884 (reg - (uint32_t)sc->sc_tx.desc_ring_paddr) /
1885 sizeof(struct eqos_dma_desc));
1886 #endif
1887 if (reg == 0)
1888 index = 0;
1889 else {
1890 index = (reg - (uint32_t)sc->sc_tx.desc_ring_paddr) /
1891 sizeof(struct eqos_dma_desc);
1892 }
1893 node.sysctl_data = &index;
1894 return sysctl_lookup(SYSCTLFN_CALL(&node));
1895 }
1896
1897 static int
1898 eqos_sysctl_tx_end_handler(SYSCTLFN_ARGS)
1899 {
1900 struct sysctlnode node = *rnode;
1901 struct eqos_ring *txq = (struct eqos_ring *)node.sysctl_data;
1902 struct eqos_softc *sc = txq->sc;
1903 uint32_t reg, index;
1904
1905 reg = RD4(sc, GMAC_DMA_CHAN0_TX_END_ADDR);
1906 if (reg == 0)
1907 index = 0;
1908 else {
1909 index = (reg - (uint32_t)sc->sc_tx.desc_ring_paddr) /
1910 sizeof(struct eqos_dma_desc);
1911 }
1912 node.sysctl_data = &index;
1913 return sysctl_lookup(SYSCTLFN_CALL(&node));
1914 }
1915
1916 static int
1917 eqos_sysctl_rx_cur_handler(SYSCTLFN_ARGS)
1918 {
1919 struct sysctlnode node = *rnode;
1920 struct eqos_ring *rxq = (struct eqos_ring *)node.sysctl_data;
1921 struct eqos_softc *sc = rxq->sc;
1922 uint32_t reg, index;
1923
1924 reg = RD4(sc, GMAC_DMA_CHAN0_CUR_RX_DESC);
1925 if (reg == 0)
1926 index = 0;
1927 else {
1928 index = (reg - (uint32_t)sc->sc_rx.desc_ring_paddr) /
1929 sizeof(struct eqos_dma_desc);
1930 }
1931 node.sysctl_data = &index;
1932 return sysctl_lookup(SYSCTLFN_CALL(&node));
1933 }
1934
1935 static int
1936 eqos_sysctl_rx_end_handler(SYSCTLFN_ARGS)
1937 {
1938 struct sysctlnode node = *rnode;
1939 struct eqos_ring *rxq = (struct eqos_ring *)node.sysctl_data;
1940 struct eqos_softc *sc = rxq->sc;
1941 uint32_t reg, index;
1942
1943 reg = RD4(sc, GMAC_DMA_CHAN0_RX_END_ADDR);
1944 if (reg == 0)
1945 index = 0;
1946 else {
1947 index = (reg - (uint32_t)sc->sc_rx.desc_ring_paddr) /
1948 sizeof(struct eqos_dma_desc);
1949 }
1950 node.sysctl_data = &index;
1951 return sysctl_lookup(SYSCTLFN_CALL(&node));
1952 }
1953
1954 #ifdef EQOS_DEBUG
1955 static int
1956 eqos_sysctl_debug_handler(SYSCTLFN_ARGS)
1957 {
1958 struct sysctlnode node = *rnode;
1959 struct eqos_softc *sc = (struct eqos_softc *)node.sysctl_data;
1960 uint32_t dflags;
1961 int error;
1962
1963 dflags = sc->sc_debug;
1964 node.sysctl_data = &dflags;
1965 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1966
1967 if (error || newp == NULL)
1968 return error;
1969
1970 sc->sc_debug = dflags;
1971 #if 0
1972 /* Addd debug code here if you want. */
1973 #endif
1974
1975 return 0;
1976 }
1977 #endif
1978