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