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