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