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