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