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