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