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