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