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