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