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