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