Home | History | Annotate | Line # | Download | only in ic
dwc_eqos.c revision 1.32
      1 /* $NetBSD: dwc_eqos.c,v 1.32 2023/11/02 13:50:02 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.32 2023/11/02 13:50:02 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 (sc->sc_promisc) {
    513 		pfil |= GMAC_MAC_PACKET_FILTER_PR |
    514 			GMAC_MAC_PACKET_FILTER_PCF_ALL;
    515 	} else if (sc->sc_allmulti) {
    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 	sc->sc_promisc = ifp->if_flags & IFF_PROMISC;
    621 	sc->sc_allmulti = ifp->if_flags & IFF_ALLMULTI; /* XXX */
    622 	eqos_setup_rxfilter(sc);
    623 
    624 	WR4(sc, GMAC_MAC_1US_TIC_COUNTER, (sc->sc_csr_clock / 1000000) - 1);
    625 
    626 	/* Enable transmit and receive DMA */
    627 	val = RD4(sc, GMAC_DMA_CHAN0_CONTROL);
    628 	val &= ~GMAC_DMA_CHAN0_CONTROL_DSL_MASK;
    629 	val |= ((DESC_ALIGN - 16) / 8) << GMAC_DMA_CHAN0_CONTROL_DSL_SHIFT;
    630 	val |= GMAC_DMA_CHAN0_CONTROL_PBLX8;
    631 	WR4(sc, GMAC_DMA_CHAN0_CONTROL, val);
    632 	val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
    633 	val &= ~GMAC_DMA_CHAN0_TX_CONTROL_TXPBL_MASK;
    634 	val |= (sc->sc_dma_txpbl << GMAC_DMA_CHAN0_TX_CONTROL_TXPBL_SHIFT);
    635 	val |= GMAC_DMA_CHAN0_TX_CONTROL_OSP;
    636 	val |= GMAC_DMA_CHAN0_TX_CONTROL_START;
    637 	WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
    638 	val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
    639 	val &= ~(GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_MASK |
    640 	    GMAC_DMA_CHAN0_RX_CONTROL_RXPBL_MASK);
    641 	val |= (MCLBYTES << GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_SHIFT);
    642 	val |= (sc->sc_dma_rxpbl << GMAC_DMA_CHAN0_RX_CONTROL_RXPBL_SHIFT);
    643 	val |= GMAC_DMA_CHAN0_RX_CONTROL_START;
    644 	WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
    645 
    646 	/* Disable counters */
    647 	WR4(sc, GMAC_MMC_CONTROL,
    648 	    GMAC_MMC_CONTROL_CNTFREEZ |
    649 	    GMAC_MMC_CONTROL_CNTPRST |
    650 	    GMAC_MMC_CONTROL_CNTPRSTLVL);
    651 
    652 	/* Configure operation modes */
    653 	WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE,
    654 	    GMAC_MTL_TXQ0_OPERATION_MODE_TSF |
    655 	    GMAC_MTL_TXQ0_OPERATION_MODE_TXQEN_EN);
    656 	WR4(sc, GMAC_MTL_RXQ0_OPERATION_MODE,
    657 	    GMAC_MTL_RXQ0_OPERATION_MODE_RSF |
    658 	    GMAC_MTL_RXQ0_OPERATION_MODE_FEP |
    659 	    GMAC_MTL_RXQ0_OPERATION_MODE_FUP);
    660 
    661 	/*
    662 	 * TX/RX fifo size in hw_feature[1] are log2(n/128), and
    663 	 * TQS/RQS in TXQ0/RXQ0_OPERATION_MODE are n/256-1.
    664 	 */
    665 	tqs = (128 << __SHIFTOUT(sc->sc_hw_feature[1],
    666 	    GMAC_MAC_HW_FEATURE1_TXFIFOSIZE) / 256) - 1;
    667 	val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
    668 	val &= ~GMAC_MTL_TXQ0_OPERATION_MODE_TQS;
    669 	val |= __SHIFTIN(tqs, GMAC_MTL_TXQ0_OPERATION_MODE_TQS);
    670 	WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE, val);
    671 
    672 	rqs = (128 << __SHIFTOUT(sc->sc_hw_feature[1],
    673 	    GMAC_MAC_HW_FEATURE1_RXFIFOSIZE) / 256) - 1;
    674 	val = RD4(sc, GMAC_MTL_RXQ0_OPERATION_MODE);
    675 	val &= ~GMAC_MTL_RXQ0_OPERATION_MODE_RQS;
    676 	val |= __SHIFTIN(rqs, GMAC_MTL_RXQ0_OPERATION_MODE_RQS);
    677 	WR4(sc, GMAC_MTL_RXQ0_OPERATION_MODE, val);
    678 
    679 	/*
    680 	 * Disable flow control.
    681 	 * It'll be configured later from the negotiated result.
    682 	 */
    683 	WR4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL, 0);
    684 	WR4(sc, GMAC_MAC_RX_FLOW_CTRL, 0);
    685 
    686 	/* set RX queue mode. must be in DCB mode. */
    687 	val = __SHIFTIN(GMAC_RXQ_CTRL0_EN_DCB, GMAC_RXQ_CTRL0_EN_MASK);
    688 	WR4(sc, GMAC_RXQ_CTRL0, val);
    689 
    690 	/* Enable transmitter and receiver */
    691 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    692 	val |= GMAC_MAC_CONFIGURATION_BE;
    693 	val |= GMAC_MAC_CONFIGURATION_JD;
    694 	val |= GMAC_MAC_CONFIGURATION_JE;
    695 	val |= GMAC_MAC_CONFIGURATION_DCRS;
    696 	val |= GMAC_MAC_CONFIGURATION_TE;
    697 	val |= GMAC_MAC_CONFIGURATION_RE;
    698 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    699 
    700 	/* Enable interrupts */
    701 	eqos_enable_intr(sc);
    702 
    703 	EQOS_ASSERT_TXLOCKED(sc);
    704 	sc->sc_txrunning = true;
    705 
    706 	sc->sc_running = true;
    707 	ifp->if_flags |= IFF_RUNNING;
    708 
    709 	mii_mediachg(mii);
    710 	callout_schedule(&sc->sc_stat_ch, hz);
    711 
    712 	return 0;
    713 }
    714 
    715 static int
    716 eqos_init(struct ifnet *ifp)
    717 {
    718 	struct eqos_softc * const sc = ifp->if_softc;
    719 	int error;
    720 
    721 	EQOS_LOCK(sc);
    722 	EQOS_TXLOCK(sc);
    723 	error = eqos_init_locked(sc);
    724 	EQOS_TXUNLOCK(sc);
    725 	EQOS_UNLOCK(sc);
    726 
    727 	return error;
    728 }
    729 
    730 static void
    731 eqos_stop_locked(struct eqos_softc *sc, int disable)
    732 {
    733 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    734 	uint32_t val;
    735 	int retry;
    736 
    737 	EQOS_ASSERT_LOCKED(sc);
    738 
    739 	EQOS_TXLOCK(sc);
    740 	sc->sc_txrunning = false;
    741 	EQOS_TXUNLOCK(sc);
    742 
    743 	sc->sc_running = false;
    744 	callout_halt(&sc->sc_stat_ch, &sc->sc_lock);
    745 
    746 	mii_down(&sc->sc_mii);
    747 
    748 	/* Disable receiver */
    749 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    750 	val &= ~GMAC_MAC_CONFIGURATION_RE;
    751 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    752 
    753 	/* Stop receive DMA */
    754 	val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
    755 	val &= ~GMAC_DMA_CHAN0_RX_CONTROL_START;
    756 	WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
    757 
    758 	/* Stop transmit DMA */
    759 	val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
    760 	val &= ~GMAC_DMA_CHAN0_TX_CONTROL_START;
    761 	WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
    762 
    763 	if (disable) {
    764 		/* Flush data in the TX FIFO */
    765 		val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
    766 		val |= GMAC_MTL_TXQ0_OPERATION_MODE_FTQ;
    767 		WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE, val);
    768 		/* Wait for flush to complete */
    769 		for (retry = 10000; retry > 0; retry--) {
    770 			val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
    771 			if ((val & GMAC_MTL_TXQ0_OPERATION_MODE_FTQ) == 0) {
    772 				break;
    773 			}
    774 			delay(1);
    775 		}
    776 		if (retry == 0) {
    777 			device_printf(sc->sc_dev,
    778 			    "timeout flushing TX queue\n");
    779 		}
    780 	}
    781 
    782 	/* Disable transmitter */
    783 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    784 	val &= ~GMAC_MAC_CONFIGURATION_TE;
    785 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    786 
    787 	/* Disable interrupts */
    788 	eqos_disable_intr(sc);
    789 
    790 	ifp->if_flags &= ~IFF_RUNNING;
    791 }
    792 
    793 static void
    794 eqos_stop(struct ifnet *ifp, int disable)
    795 {
    796 	struct eqos_softc * const sc = ifp->if_softc;
    797 
    798 	EQOS_LOCK(sc);
    799 	eqos_stop_locked(sc, disable);
    800 	EQOS_UNLOCK(sc);
    801 }
    802 
    803 static void
    804 eqos_rxintr(struct eqos_softc *sc, int qid)
    805 {
    806 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    807 	int error, index, pkts = 0;
    808 	struct mbuf *m, *m0, *new_m, *mprev;
    809 	uint32_t tdes3;
    810 	bool discarding;
    811 
    812 	/* restore jumboframe context */
    813 	discarding = sc->sc_rx_discarding;
    814 	m0 = sc->sc_rx_receiving_m;
    815 	mprev = sc->sc_rx_receiving_m_last;
    816 
    817 	for (index = sc->sc_rx.cur; ; index = RX_NEXT(index)) {
    818 		eqos_dma_sync(sc, sc->sc_rx.desc_map,
    819 		    index, index + 1, RX_DESC_COUNT,
    820 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    821 
    822 		tdes3 = le32toh(sc->sc_rx.desc_ring[index].tdes3);
    823 		if ((tdes3 & EQOS_TDES3_RX_OWN) != 0) {
    824 			break;
    825 		}
    826 
    827 		/* now discarding untill the last packet */
    828 		if (discarding)
    829 			goto rx_next;
    830 
    831 		if ((tdes3 & EQOS_TDES3_RX_CTXT) != 0)
    832 			goto rx_next;	/* ignore receive context descriptor */
    833 
    834 		/* error packet? */
    835 		if ((tdes3 & (EQOS_TDES3_RX_CE | EQOS_TDES3_RX_RWT |
    836 		    EQOS_TDES3_RX_OE | EQOS_TDES3_RX_RE |
    837 		    EQOS_TDES3_RX_DE)) != 0) {
    838 #ifdef EQOS_DEBUG
    839 			char buf[128];
    840 			snprintb(buf, sizeof(buf),
    841 			    "\177\020"
    842 			    "b\x1e" "CTXT\0"	/* 30 */
    843 			    "b\x18" "CE\0"	/* 24 */
    844 			    "b\x17" "GP\0"	/* 23 */
    845 			    "b\x16" "WDT\0"	/* 22 */
    846 			    "b\x15" "OE\0"	/* 21 */
    847 			    "b\x14" "RE\0"	/* 20 */
    848 			    "b\x13" "DE\0"	/* 19 */
    849 			    "b\x0f" "ES\0"	/* 15 */
    850 			    "\0", tdes3);
    851 			DPRINTF(EDEB_NOTE,
    852 			    "rxdesc[%d].tdes3=%s\n", index, buf);
    853 #endif
    854 			if_statinc(ifp, if_ierrors);
    855 			if (m0 != NULL) {
    856 				m_freem(m0);
    857 				m0 = mprev = NULL;
    858 			}
    859 			discarding = true;
    860 			goto rx_next;
    861 		}
    862 
    863 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
    864 		    0, sc->sc_rx.buf_map[index].map->dm_mapsize,
    865 		    BUS_DMASYNC_POSTREAD);
    866 		m = sc->sc_rx.buf_map[index].mbuf;
    867 		new_m = eqos_alloc_mbufcl(sc);
    868 		if (new_m == NULL) {
    869 			/*
    870 			 * cannot allocate new mbuf. discard this received
    871 			 * packet, and reuse the mbuf for next.
    872 			 */
    873 			if_statinc(ifp, if_ierrors);
    874 			if (m0 != NULL) {
    875 				/* also discard the halfway jumbo packet */
    876 				m_freem(m0);
    877 				m0 = mprev = NULL;
    878 			}
    879 			discarding = true;
    880 			goto rx_next;
    881 		}
    882 		bus_dmamap_unload(sc->sc_dmat,
    883 		    sc->sc_rx.buf_map[index].map);
    884 		error = eqos_setup_rxbuf(sc, index, new_m);
    885 		if (error)
    886 			panic("%s: %s: unable to load RX mbuf. error=%d",
    887 			    device_xname(sc->sc_dev), __func__, error);
    888 
    889 		if (m0 == NULL) {
    890 			m0 = m;
    891 		} else {
    892 			if (m->m_flags & M_PKTHDR)
    893 				m_remove_pkthdr(m);
    894 			mprev->m_next = m;
    895 		}
    896 		mprev = m;
    897 
    898 		if ((tdes3 & EQOS_TDES3_RX_LD) == 0) {
    899 			/* to be continued in the next segment */
    900 			m->m_len = EQOS_RXDMA_SIZE;
    901 		} else {
    902 			/* last segment */
    903 			uint32_t totallen = tdes3 & EQOS_TDES3_RX_LENGTH_MASK;
    904 			uint32_t mlen = totallen % EQOS_RXDMA_SIZE;
    905 			if (mlen == 0)
    906 				mlen = EQOS_RXDMA_SIZE;
    907 			m->m_len = mlen;
    908 			m0->m_pkthdr.len = totallen;
    909 			m_set_rcvif(m0, ifp);
    910 			m0->m_flags |= M_HASFCS;
    911 			m0->m_nextpkt = NULL;
    912 			if_percpuq_enqueue(ifp->if_percpuq, m0);
    913 			m0 = mprev = NULL;
    914 
    915 			++pkts;
    916 		}
    917 
    918  rx_next:
    919 		if (discarding && (tdes3 & EQOS_TDES3_RX_LD) != 0)
    920 			discarding = false;
    921 
    922 		eqos_setup_rxdesc(sc, index,
    923 		    sc->sc_rx.buf_map[index].map->dm_segs[0].ds_addr);
    924 		eqos_dma_sync(sc, sc->sc_rx.desc_map,
    925 		    index, index + 1, RX_DESC_COUNT,
    926 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    927 
    928 		WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
    929 		    (uint32_t)sc->sc_rx.desc_ring_paddr +
    930 		    DESC_OFF(sc->sc_rx.cur));
    931 	}
    932 	/* save jumboframe context */
    933 	sc->sc_rx_discarding = discarding;
    934 	sc->sc_rx_receiving_m = m0;
    935 	sc->sc_rx_receiving_m_last = mprev;
    936 
    937 	DPRINTF(EDEB_RXRING, "sc_rx.cur %u -> %u\n",
    938 	    sc->sc_rx.cur, index);
    939 	sc->sc_rx.cur = index;
    940 
    941 	if (pkts != 0) {
    942 		rnd_add_uint32(&sc->sc_rndsource, pkts);
    943 	}
    944 }
    945 
    946 static void
    947 eqos_txintr(struct eqos_softc *sc, int qid)
    948 {
    949 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    950 	struct eqos_bufmap *bmap;
    951 	struct eqos_dma_desc *desc;
    952 	uint32_t tdes3;
    953 	int i, pkts = 0;
    954 
    955 	DPRINTF(EDEB_INTR, "qid: %u\n", qid);
    956 
    957 	EQOS_ASSERT_LOCKED(sc);
    958 	EQOS_ASSERT_TXLOCKED(sc);
    959 
    960 	for (i = sc->sc_tx.next; sc->sc_tx.queued > 0; i = TX_NEXT(i)) {
    961 		KASSERT(sc->sc_tx.queued > 0);
    962 		KASSERT(sc->sc_tx.queued <= TX_DESC_COUNT);
    963 		eqos_dma_sync(sc, sc->sc_tx.desc_map,
    964 		    i, i + 1, TX_DESC_COUNT,
    965 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    966 		desc = &sc->sc_tx.desc_ring[i];
    967 		tdes3 = le32toh(desc->tdes3);
    968 		if ((tdes3 & EQOS_TDES3_TX_OWN) != 0) {
    969 			break;
    970 		}
    971 		bmap = &sc->sc_tx.buf_map[i];
    972 		if (bmap->mbuf != NULL) {
    973 			bus_dmamap_sync(sc->sc_dmat, bmap->map,
    974 			    0, bmap->map->dm_mapsize,
    975 			    BUS_DMASYNC_POSTWRITE);
    976 			bus_dmamap_unload(sc->sc_dmat, bmap->map);
    977 			m_freem(bmap->mbuf);
    978 			bmap->mbuf = NULL;
    979 			++pkts;
    980 		}
    981 
    982 		eqos_setup_txdesc(sc, i, 0, 0, 0, 0);
    983 		eqos_dma_sync(sc, sc->sc_tx.desc_map,
    984 		    i, i + 1, TX_DESC_COUNT,
    985 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    986 
    987 		/* Last descriptor in a packet contains DMA status */
    988 		if ((tdes3 & EQOS_TDES3_TX_LD) != 0) {
    989 			if ((tdes3 & EQOS_TDES3_TX_DE) != 0) {
    990 				device_printf(sc->sc_dev,
    991 				    "TX [%u] desc error: 0x%08x\n",
    992 				    i, tdes3);
    993 				if_statinc(ifp, if_oerrors);
    994 			} else if ((tdes3 & EQOS_TDES3_TX_ES) != 0) {
    995 				device_printf(sc->sc_dev,
    996 				    "TX [%u] tx error: 0x%08x\n",
    997 				    i, tdes3);
    998 				if_statinc(ifp, if_oerrors);
    999 			} else {
   1000 				if_statinc(ifp, if_opackets);
   1001 			}
   1002 		}
   1003 
   1004 	}
   1005 
   1006 	sc->sc_tx.next = i;
   1007 
   1008 	if (pkts != 0) {
   1009 		rnd_add_uint32(&sc->sc_rndsource, pkts);
   1010 	}
   1011 }
   1012 
   1013 static void
   1014 eqos_start_locked(struct eqos_softc *sc)
   1015 {
   1016 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1017 	struct mbuf *m;
   1018 	int cnt, nsegs, start;
   1019 
   1020 	EQOS_ASSERT_TXLOCKED(sc);
   1021 
   1022 	if (!sc->sc_txrunning)
   1023 		return;
   1024 
   1025 	for (cnt = 0, start = sc->sc_tx.cur; ; cnt++) {
   1026 		if (sc->sc_tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
   1027 			DPRINTF(EDEB_TXRING, "%u sc_tx.queued, ring full\n",
   1028 			    sc->sc_tx.queued);
   1029 			break;
   1030 		}
   1031 
   1032 		IFQ_POLL(&ifp->if_snd, m);
   1033 		if (m == NULL)
   1034 			break;
   1035 
   1036 		nsegs = eqos_setup_txbuf(sc, sc->sc_tx.cur, m);
   1037 		if (nsegs <= 0) {
   1038 			DPRINTF(EDEB_TXRING, "eqos_setup_txbuf failed "
   1039 			    "with %d\n", nsegs);
   1040 			if (nsegs == -2) {
   1041 				IFQ_DEQUEUE(&ifp->if_snd, m);
   1042 				m_freem(m);
   1043 				continue;
   1044 			}
   1045 			break;
   1046 		}
   1047 
   1048 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1049 		bpf_mtap(ifp, m, BPF_D_OUT);
   1050 
   1051 		sc->sc_tx.cur = TX_SKIP(sc->sc_tx.cur, nsegs);
   1052 	}
   1053 
   1054 	DPRINTF(EDEB_TXRING, "tx loop -> cnt = %u, cur: %u, next: %u, "
   1055 	    "queued: %u\n", cnt, sc->sc_tx.cur, sc->sc_tx.next,
   1056 	    sc->sc_tx.queued);
   1057 
   1058 	if (cnt != 0) {
   1059 		eqos_dma_sync(sc, sc->sc_tx.desc_map,
   1060 		    start, sc->sc_tx.cur, TX_DESC_COUNT,
   1061 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1062 
   1063 		/* Start and run TX DMA */
   1064 		DPRINTF(EDEB_TXRING, "sending desc %u at %lx upto "
   1065 		    "%u-1 at %lx cur tx desc: %x cur tx buf: %x\n", start,
   1066 		    (uint32_t)sc->sc_tx.desc_ring_paddr + DESC_OFF(start),
   1067 		    sc->sc_tx.cur,
   1068 		    (uint32_t)sc->sc_tx.desc_ring_paddr +
   1069 		    DESC_OFF(sc->sc_tx.cur),
   1070 		    RD4(sc, GMAC_DMA_CHAN0_CUR_TX_DESC),
   1071 		    RD4(sc, GMAC_DMA_CHAN0_CUR_TX_BUF_ADDR));
   1072 		WR4(sc, GMAC_DMA_CHAN0_TX_END_ADDR,
   1073 		    (uint32_t)sc->sc_tx.desc_ring_paddr +
   1074 		    DESC_OFF(sc->sc_tx.cur));
   1075 	}
   1076 }
   1077 
   1078 static void
   1079 eqos_start(struct ifnet *ifp)
   1080 {
   1081 	struct eqos_softc * const sc = ifp->if_softc;
   1082 
   1083 	EQOS_TXLOCK(sc);
   1084 	eqos_start_locked(sc);
   1085 	EQOS_TXUNLOCK(sc);
   1086 }
   1087 
   1088 static void
   1089 eqos_intr_mtl(struct eqos_softc *sc, uint32_t mtl_status)
   1090 {
   1091 	uint32_t debug_data __unused = 0, ictrl = 0;
   1092 
   1093 	if (mtl_status == 0)
   1094 		return;
   1095 
   1096 	/* Drain the errors reported by MTL_INTERRUPT_STATUS */
   1097 	sc->sc_ev_mtl.ev_count++;
   1098 
   1099 	if ((mtl_status & GMAC_MTL_INTERRUPT_STATUS_DBGIS) != 0) {
   1100 		debug_data = RD4(sc, GMAC_MTL_FIFO_DEBUG_DATA);
   1101 		sc->sc_ev_mtl_debugdata.ev_count++;
   1102 	}
   1103 	if ((mtl_status & GMAC_MTL_INTERRUPT_STATUS_Q0IS) != 0) {
   1104 		uint32_t new_status = 0;
   1105 
   1106 		ictrl = RD4(sc, GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS);
   1107 		if ((ictrl & GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOVFIS) != 0) {
   1108 			new_status |= GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOVFIS;
   1109 			sc->sc_ev_mtl_rxovfis.ev_count++;
   1110 		}
   1111 		if ((ictrl & GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUNFIS) != 0) {
   1112 			new_status |= GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUNFIS;
   1113 			sc->sc_ev_mtl_txovfis.ev_count++;
   1114 		}
   1115 		if (new_status) {
   1116 			new_status |= (ictrl &
   1117 			    (GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOIE |
   1118 			     GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUIE));
   1119 			WR4(sc, GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS, new_status);
   1120 		}
   1121 	}
   1122 	DPRINTF(EDEB_INTR,
   1123 	    "GMAC_MTL_INTERRUPT_STATUS = 0x%08X, "
   1124 	    "GMAC_MTL_FIFO_DEBUG_DATA = 0x%08X, "
   1125 	    "GMAC_MTL_INTERRUPT_STATUS_Q0IS = 0x%08X\n",
   1126 	    mtl_status, debug_data, ictrl);
   1127 }
   1128 
   1129 int
   1130 eqos_intr(void *arg)
   1131 {
   1132 	struct eqos_softc * const sc = arg;
   1133 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1134 	uint32_t mac_status, mtl_status, dma_status, rx_tx_status;
   1135 
   1136 	sc->sc_ev_intr.ev_count++;
   1137 
   1138 	mac_status = RD4(sc, GMAC_MAC_INTERRUPT_STATUS);
   1139 	mac_status &= RD4(sc, GMAC_MAC_INTERRUPT_ENABLE);
   1140 
   1141 	if (mac_status) {
   1142 		sc->sc_ev_mac.ev_count++;
   1143 		DPRINTF(EDEB_INTR,
   1144 		    "GMAC_MAC_INTERRUPT_STATUS = 0x%08X\n", mac_status);
   1145 	}
   1146 
   1147 	mtl_status = RD4(sc, GMAC_MTL_INTERRUPT_STATUS);
   1148 	eqos_intr_mtl(sc, mtl_status);
   1149 
   1150 	dma_status = RD4(sc, GMAC_DMA_CHAN0_STATUS);
   1151 	dma_status &= RD4(sc, GMAC_DMA_CHAN0_INTR_ENABLE);
   1152 	if (dma_status) {
   1153 		WR4(sc, GMAC_DMA_CHAN0_STATUS, dma_status);
   1154 	}
   1155 
   1156 	EQOS_LOCK(sc);
   1157 	if ((dma_status & GMAC_DMA_CHAN0_STATUS_RI) != 0) {
   1158 		eqos_rxintr(sc, 0);
   1159 		sc->sc_ev_rxintr.ev_count++;
   1160 	}
   1161 
   1162 	if ((dma_status & GMAC_DMA_CHAN0_STATUS_TI) != 0) {
   1163 		EQOS_TXLOCK(sc);
   1164 		eqos_txintr(sc, 0);
   1165 		EQOS_TXUNLOCK(sc);
   1166 		if_schedule_deferred_start(ifp);
   1167 		sc->sc_ev_txintr.ev_count++;
   1168 	}
   1169 	EQOS_UNLOCK(sc);
   1170 
   1171 	if ((mac_status | mtl_status | dma_status) == 0) {
   1172 		DPRINTF(EDEB_NOTE, "spurious interrupt?!\n");
   1173 	}
   1174 
   1175 	rx_tx_status = RD4(sc, GMAC_MAC_RX_TX_STATUS);
   1176 	if (rx_tx_status) {
   1177 		sc->sc_ev_status.ev_count++;
   1178 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_RWT) != 0)
   1179 			sc->sc_ev_rwt.ev_count++;
   1180 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXCOL) != 0)
   1181 			sc->sc_ev_excol.ev_count++;
   1182 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCOL) != 0)
   1183 			sc->sc_ev_lcol.ev_count++;
   1184 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXDEF) != 0)
   1185 			sc->sc_ev_exdef.ev_count++;
   1186 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCARR) != 0)
   1187 			sc->sc_ev_lcarr.ev_count++;
   1188 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_NCARR) != 0)
   1189 			sc->sc_ev_ncarr.ev_count++;
   1190 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_TJT) != 0)
   1191 			sc->sc_ev_tjt.ev_count++;
   1192 
   1193 		DPRINTF(EDEB_INTR, "GMAC_MAC_RX_TX_STATUS = 0x%08x\n",
   1194 		    rx_tx_status);
   1195 	}
   1196 
   1197 	return 1;
   1198 }
   1199 
   1200 static int
   1201 eqos_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1202 {
   1203 	struct eqos_softc * const sc = ifp->if_softc;
   1204 	struct ifreq * const ifr = (struct ifreq *)data;
   1205 	int error, s;
   1206 
   1207 #ifndef EQOS_MPSAFE
   1208 	s = splnet();
   1209 #endif
   1210 
   1211 	switch (cmd) {
   1212 	case SIOCSIFMTU:
   1213 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > EQOS_MAX_MTU) {
   1214 			error = EINVAL;
   1215 		} else {
   1216 			ifp->if_mtu = ifr->ifr_mtu;
   1217 			error = 0;	/* no need ENETRESET */
   1218 		}
   1219 		break;
   1220 	default:
   1221 #ifdef EQOS_MPSAFE
   1222 		s = splnet();
   1223 #endif
   1224 		error = ether_ioctl(ifp, cmd, data);
   1225 #ifdef EQOS_MPSAFE
   1226 		splx(s);
   1227 #endif
   1228 		if (error != ENETRESET)
   1229 			break;
   1230 
   1231 		error = 0;
   1232 
   1233 		if (cmd == SIOCSIFCAP)
   1234 			error = (*ifp->if_init)(ifp);
   1235 		else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
   1236 			;
   1237 		else {
   1238 			EQOS_LOCK(sc);
   1239 			if (sc->sc_running)
   1240 				eqos_setup_rxfilter(sc);
   1241 			EQOS_UNLOCK(sc);
   1242 		}
   1243 		break;
   1244 	}
   1245 
   1246 #ifndef EQOS_MPSAFE
   1247 	splx(s);
   1248 #endif
   1249 
   1250 	return error;
   1251 }
   1252 
   1253 static void
   1254 eqos_get_eaddr(struct eqos_softc *sc, uint8_t *eaddr)
   1255 {
   1256 	prop_dictionary_t prop = device_properties(sc->sc_dev);
   1257 	uint32_t maclo, machi;
   1258 	prop_data_t eaprop;
   1259 
   1260 	eaprop = prop_dictionary_get(prop, "mac-address");
   1261 	if (eaprop != NULL) {
   1262 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
   1263 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
   1264 		memcpy(eaddr, prop_data_value(eaprop),
   1265 		    ETHER_ADDR_LEN);
   1266 		return;
   1267 	}
   1268 
   1269 	maclo = RD4(sc, GMAC_MAC_ADDRESS0_LOW);
   1270 	machi = RD4(sc, GMAC_MAC_ADDRESS0_HIGH) & 0xFFFF;
   1271 	if ((maclo & 0x00000001) != 0) {
   1272 		aprint_error_dev(sc->sc_dev,
   1273 		    "Wrong MAC address. Clear the multicast bit.\n");
   1274 		maclo &= ~0x00000001;
   1275 	}
   1276 
   1277 	if (maclo == 0xFFFFFFFF && machi == 0xFFFF) {
   1278 		/* Create one */
   1279 		maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
   1280 		machi = cprng_strong32() & 0xffff;
   1281 	}
   1282 
   1283 	eaddr[0] = maclo & 0xff;
   1284 	eaddr[1] = (maclo >> 8) & 0xff;
   1285 	eaddr[2] = (maclo >> 16) & 0xff;
   1286 	eaddr[3] = (maclo >> 24) & 0xff;
   1287 	eaddr[4] = machi & 0xff;
   1288 	eaddr[5] = (machi >> 8) & 0xff;
   1289 }
   1290 
   1291 static void
   1292 eqos_get_dma_pbl(struct eqos_softc *sc)
   1293 {
   1294 	prop_dictionary_t prop = device_properties(sc->sc_dev);
   1295 	uint32_t pbl;
   1296 
   1297 	/* Set default values. */
   1298 	sc->sc_dma_txpbl = sc->sc_dma_rxpbl = EQOS_DMA_PBL_DEFAULT;
   1299 
   1300 	/* Get values from props. */
   1301 	if (prop_dictionary_get_uint32(prop, "snps,pbl", &pbl) && pbl)
   1302 		sc->sc_dma_txpbl = sc->sc_dma_rxpbl = pbl;
   1303 	if (prop_dictionary_get_uint32(prop, "snps,txpbl", &pbl) && pbl)
   1304 		sc->sc_dma_txpbl = pbl;
   1305 	if (prop_dictionary_get_uint32(prop, "snps,rxpbl", &pbl) && pbl)
   1306 		sc->sc_dma_rxpbl = pbl;
   1307 }
   1308 
   1309 static void
   1310 eqos_axi_configure(struct eqos_softc *sc)
   1311 {
   1312 	prop_dictionary_t prop = device_properties(sc->sc_dev);
   1313 	uint32_t val;
   1314 	u_int uival;
   1315 	bool bval;
   1316 
   1317 	val = RD4(sc, GMAC_DMA_SYSBUS_MODE);
   1318 	if (prop_dictionary_get_bool(prop, "snps,mixed-burst", &bval) && bval) {
   1319 		val |= GMAC_DMA_SYSBUS_MODE_MB;
   1320 	}
   1321 	if (prop_dictionary_get_bool(prop, "snps,fixed-burst", &bval) && bval) {
   1322 		val |= GMAC_DMA_SYSBUS_MODE_FB;
   1323 	}
   1324 	if (prop_dictionary_get_uint(prop, "snps,wr_osr_lmt", &uival)) {
   1325 		val &= ~GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_MASK;
   1326 		val |= uival << GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_SHIFT;
   1327 	}
   1328 	if (prop_dictionary_get_uint(prop, "snps,rd_osr_lmt", &uival)) {
   1329 		val &= ~GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK;
   1330 		val |= uival << GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT;
   1331 	}
   1332 
   1333 	if (!EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
   1334 		val |= GMAC_DMA_SYSBUS_MODE_EAME;
   1335 	}
   1336 
   1337 	/* XXX */
   1338 	val |= GMAC_DMA_SYSBUS_MODE_BLEN16;
   1339 	val |= GMAC_DMA_SYSBUS_MODE_BLEN8;
   1340 	val |= GMAC_DMA_SYSBUS_MODE_BLEN4;
   1341 
   1342 	WR4(sc, GMAC_DMA_SYSBUS_MODE, val);
   1343 }
   1344 
   1345 static int
   1346 eqos_setup_dma(struct eqos_softc *sc, int qid)
   1347 {
   1348 	struct mbuf *m;
   1349 	int error, nsegs, i;
   1350 
   1351 	/* Set back pointer */
   1352 	sc->sc_tx.sc = sc;
   1353 	sc->sc_rx.sc = sc;
   1354 
   1355 	/* Setup TX ring */
   1356 	error = bus_dmamap_create(sc->sc_dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE,
   1357 	    DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_tx.desc_map);
   1358 	if (error) {
   1359 		return error;
   1360 	}
   1361 	error = bus_dmamem_alloc(sc->sc_dmat, TX_DESC_SIZE, DESC_ALIGN,
   1362 	    DESC_BOUNDARY, &sc->sc_tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1363 	if (error) {
   1364 		return error;
   1365 	}
   1366 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_tx.desc_dmaseg, nsegs,
   1367 	    TX_DESC_SIZE, (void *)&sc->sc_tx.desc_ring, BUS_DMA_WAITOK);
   1368 	if (error) {
   1369 		return error;
   1370 	}
   1371 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_tx.desc_map,
   1372 	    sc->sc_tx.desc_ring, TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1373 	if (error) {
   1374 		return error;
   1375 	}
   1376 	sc->sc_tx.desc_ring_paddr = sc->sc_tx.desc_map->dm_segs[0].ds_addr;
   1377 
   1378 	memset(sc->sc_tx.desc_ring, 0, TX_DESC_SIZE);
   1379 	bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.desc_map, 0, TX_DESC_SIZE,
   1380 	    BUS_DMASYNC_PREWRITE);
   1381 
   1382 	sc->sc_tx.queued = TX_DESC_COUNT;
   1383 	for (i = 0; i < TX_DESC_COUNT; i++) {
   1384 		error = bus_dmamap_create(sc->sc_dmat, EQOS_TXDMA_SIZE,
   1385 		    TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
   1386 		    &sc->sc_tx.buf_map[i].map);
   1387 		if (error != 0) {
   1388 			device_printf(sc->sc_dev,
   1389 			    "cannot create TX buffer map\n");
   1390 			return error;
   1391 		}
   1392 		EQOS_TXLOCK(sc);
   1393 		eqos_setup_txdesc(sc, i, 0, 0, 0, 0);
   1394 		EQOS_TXUNLOCK(sc);
   1395 	}
   1396 
   1397 	/* Setup RX ring */
   1398 	error = bus_dmamap_create(sc->sc_dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE,
   1399 	    DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_rx.desc_map);
   1400 	if (error) {
   1401 		return error;
   1402 	}
   1403 	error = bus_dmamem_alloc(sc->sc_dmat, RX_DESC_SIZE, DESC_ALIGN,
   1404 	    DESC_BOUNDARY, &sc->sc_rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1405 	if (error) {
   1406 		return error;
   1407 	}
   1408 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_rx.desc_dmaseg, nsegs,
   1409 	    RX_DESC_SIZE, (void *)&sc->sc_rx.desc_ring, BUS_DMA_WAITOK);
   1410 	if (error) {
   1411 		return error;
   1412 	}
   1413 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_rx.desc_map,
   1414 	    sc->sc_rx.desc_ring, RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1415 	if (error) {
   1416 		return error;
   1417 	}
   1418 	sc->sc_rx.desc_ring_paddr = sc->sc_rx.desc_map->dm_segs[0].ds_addr;
   1419 
   1420 	memset(sc->sc_rx.desc_ring, 0, RX_DESC_SIZE);
   1421 
   1422 	for (i = 0; i < RX_DESC_COUNT; i++) {
   1423 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
   1424 		    RX_DESC_COUNT, MCLBYTES, 0, BUS_DMA_WAITOK,
   1425 		    &sc->sc_rx.buf_map[i].map);
   1426 		if (error != 0) {
   1427 			device_printf(sc->sc_dev,
   1428 			    "cannot create RX buffer map\n");
   1429 			return error;
   1430 		}
   1431 		if ((m = eqos_alloc_mbufcl(sc)) == NULL) {
   1432 			device_printf(sc->sc_dev, "cannot allocate RX mbuf\n");
   1433 			return ENOMEM;
   1434 		}
   1435 		error = eqos_setup_rxbuf(sc, i, m);
   1436 		if (error != 0) {
   1437 			device_printf(sc->sc_dev, "cannot create RX buffer\n");
   1438 			return error;
   1439 		}
   1440 		eqos_setup_rxdesc(sc, i,
   1441 		    sc->sc_rx.buf_map[i].map->dm_segs[0].ds_addr);
   1442 	}
   1443 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.desc_map,
   1444 	    0, sc->sc_rx.desc_map->dm_mapsize,
   1445 	    BUS_DMASYNC_PREWRITE);
   1446 
   1447 	aprint_debug_dev(sc->sc_dev, "TX ring @ 0x%lX, RX ring @ 0x%lX\n",
   1448 	    sc->sc_tx.desc_ring_paddr, sc->sc_rx.desc_ring_paddr);
   1449 
   1450 	return 0;
   1451 }
   1452 
   1453 int
   1454 eqos_attach(struct eqos_softc *sc)
   1455 {
   1456 	struct mii_data * const mii = &sc->sc_mii;
   1457 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1458 	uint8_t eaddr[ETHER_ADDR_LEN];
   1459 	u_int userver, snpsver;
   1460 	int error;
   1461 	int n;
   1462 
   1463 #ifdef EQOS_DEBUG
   1464 	/* Load the default debug flags. */
   1465 	sc->sc_debug = eqos_debug;
   1466 #endif
   1467 
   1468 	const uint32_t ver = RD4(sc, GMAC_MAC_VERSION);
   1469 	userver = (ver & GMAC_MAC_VERSION_USERVER_MASK) >>
   1470 	    GMAC_MAC_VERSION_USERVER_SHIFT;
   1471 	snpsver = ver & GMAC_MAC_VERSION_SNPSVER_MASK;
   1472 
   1473 	if ((snpsver < 0x51) || (snpsver > 0x52)) {
   1474 		aprint_error(": EQOS version 0x%02xx not supported\n",
   1475 		    snpsver);
   1476 		return ENXIO;
   1477 	}
   1478 
   1479 	if (sc->sc_csr_clock < 20000000) {
   1480 		aprint_error(": CSR clock too low\n");
   1481 		return EINVAL;
   1482 	} else if (sc->sc_csr_clock < 35000000) {
   1483 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_20_35;
   1484 	} else if (sc->sc_csr_clock < 60000000) {
   1485 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_35_60;
   1486 	} else if (sc->sc_csr_clock < 100000000) {
   1487 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_60_100;
   1488 	} else if (sc->sc_csr_clock < 150000000) {
   1489 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_100_150;
   1490 	} else if (sc->sc_csr_clock < 250000000) {
   1491 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_150_250;
   1492 	} else if (sc->sc_csr_clock < 300000000) {
   1493 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_250_300;
   1494 	} else if (sc->sc_csr_clock < 500000000) {
   1495 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_300_500;
   1496 	} else if (sc->sc_csr_clock < 800000000) {
   1497 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_500_800;
   1498 	} else {
   1499 		aprint_error(": CSR clock too high\n");
   1500 		return EINVAL;
   1501 	}
   1502 
   1503 	for (n = 0; n < 4; n++) {
   1504 		sc->sc_hw_feature[n] = RD4(sc, GMAC_MAC_HW_FEATURE(n));
   1505 	}
   1506 
   1507 	aprint_naive("\n");
   1508 	aprint_normal(": DesignWare EQOS ver 0x%02x (0x%02x)\n",
   1509 	    snpsver, userver);
   1510 	aprint_verbose_dev(sc->sc_dev, "hw features %08x %08x %08x %08x\n",
   1511 	    sc->sc_hw_feature[0], sc->sc_hw_feature[1],
   1512 	    sc->sc_hw_feature[2], sc->sc_hw_feature[3]);
   1513 
   1514 	if (EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
   1515 		bus_dma_tag_t ntag;
   1516 
   1517 		error = bus_dmatag_subregion(sc->sc_dmat, 0, UINT32_MAX,
   1518 		    &ntag, 0);
   1519 		if (error) {
   1520 			aprint_error_dev(sc->sc_dev,
   1521 			    "failed to restrict DMA: %d\n", error);
   1522 			return error;
   1523 		}
   1524 		aprint_verbose_dev(sc->sc_dev, "using 32-bit DMA\n");
   1525 		sc->sc_dmat = ntag;
   1526 	}
   1527 
   1528 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
   1529 	mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_NET);
   1530 	callout_init(&sc->sc_stat_ch, CALLOUT_FLAGS);
   1531 	callout_setfunc(&sc->sc_stat_ch, eqos_tick, sc);
   1532 
   1533 	eqos_get_eaddr(sc, eaddr);
   1534 	aprint_normal_dev(sc->sc_dev,
   1535 	    "Ethernet address %s\n", ether_sprintf(eaddr));
   1536 
   1537 	/* Soft reset EMAC core */
   1538 	error = eqos_reset(sc);
   1539 	if (error != 0) {
   1540 		return error;
   1541 	}
   1542 
   1543 	/* Get DMA burst length */
   1544 	eqos_get_dma_pbl(sc);
   1545 
   1546 	/* Configure AXI Bus mode parameters */
   1547 	eqos_axi_configure(sc);
   1548 
   1549 	/* Setup DMA descriptors */
   1550 	if (eqos_setup_dma(sc, 0) != 0) {
   1551 		aprint_error_dev(sc->sc_dev,
   1552 		    "failed to setup DMA descriptors\n");
   1553 		return EINVAL;
   1554 	}
   1555 
   1556 	/* Setup ethernet interface */
   1557 	ifp->if_softc = sc;
   1558 	snprintf(ifp->if_xname, IFNAMSIZ, "%s", device_xname(sc->sc_dev));
   1559 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1560 #ifdef EQOS_MPSAFE
   1561 	ifp->if_extflags = IFEF_MPSAFE;
   1562 #endif
   1563 	ifp->if_start = eqos_start;
   1564 	ifp->if_ioctl = eqos_ioctl;
   1565 	ifp->if_init = eqos_init;
   1566 	ifp->if_stop = eqos_stop;
   1567 	ifp->if_capabilities = 0;
   1568 	ifp->if_capenable = ifp->if_capabilities;
   1569 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
   1570 	IFQ_SET_READY(&ifp->if_snd);
   1571 
   1572 	/* 802.1Q VLAN-sized frames, and jumbo frame are supported */
   1573 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
   1574 	sc->sc_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   1575 
   1576 	/* Attach MII driver */
   1577 	sc->sc_ec.ec_mii = mii;
   1578 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
   1579 	mii->mii_ifp = ifp;
   1580 	mii->mii_readreg = eqos_mii_readreg;
   1581 	mii->mii_writereg = eqos_mii_writereg;
   1582 	mii->mii_statchg = eqos_mii_statchg;
   1583 	mii_attach(sc->sc_dev, mii, 0xffffffff, sc->sc_phy_id, MII_OFFSET_ANY,
   1584 	    MIIF_DOPAUSE);
   1585 
   1586 	if (LIST_EMPTY(&mii->mii_phys)) {
   1587 		aprint_error_dev(sc->sc_dev, "no PHY found!\n");
   1588 		return ENOENT;
   1589 	}
   1590 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1591 
   1592 	/* Master interrupt evcnt */
   1593 	evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
   1594 	    NULL, device_xname(sc->sc_dev), "interrupts");
   1595 
   1596 	/* Per-interrupt type, using main interrupt */
   1597 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
   1598 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxintr");
   1599 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
   1600 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "txintr");
   1601 	evcnt_attach_dynamic(&sc->sc_ev_mac, EVCNT_TYPE_INTR,
   1602 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "macstatus");
   1603 	evcnt_attach_dynamic(&sc->sc_ev_mtl, EVCNT_TYPE_INTR,
   1604 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "intrstatus");
   1605 	evcnt_attach_dynamic(&sc->sc_ev_status, EVCNT_TYPE_INTR,
   1606 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxtxstatus");
   1607 
   1608 	/* MAC Status specific type, using macstatus interrupt */
   1609 	evcnt_attach_dynamic(&sc->sc_ev_mtl_debugdata, EVCNT_TYPE_INTR,
   1610 	    &sc->sc_ev_mtl, device_xname(sc->sc_dev), "debugdata");
   1611 	evcnt_attach_dynamic(&sc->sc_ev_mtl_rxovfis, EVCNT_TYPE_INTR,
   1612 	    &sc->sc_ev_mtl, device_xname(sc->sc_dev), "rxovfis");
   1613 	evcnt_attach_dynamic(&sc->sc_ev_mtl_txovfis, EVCNT_TYPE_INTR,
   1614 	    &sc->sc_ev_mtl, device_xname(sc->sc_dev), "txovfis");
   1615 
   1616 	/* RX/TX Status specific type, using rxtxstatus interrupt */
   1617 	evcnt_attach_dynamic(&sc->sc_ev_rwt, EVCNT_TYPE_INTR,
   1618 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "rwt");
   1619 	evcnt_attach_dynamic(&sc->sc_ev_excol, EVCNT_TYPE_INTR,
   1620 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "excol");
   1621 	evcnt_attach_dynamic(&sc->sc_ev_lcol, EVCNT_TYPE_INTR,
   1622 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "lcol");
   1623 	evcnt_attach_dynamic(&sc->sc_ev_exdef, EVCNT_TYPE_INTR,
   1624 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "exdef");
   1625 	evcnt_attach_dynamic(&sc->sc_ev_lcarr, EVCNT_TYPE_INTR,
   1626 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "lcarr");
   1627 	evcnt_attach_dynamic(&sc->sc_ev_ncarr, EVCNT_TYPE_INTR,
   1628 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "ncarr");
   1629 	evcnt_attach_dynamic(&sc->sc_ev_tjt, EVCNT_TYPE_INTR,
   1630 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "tjt");
   1631 
   1632 	/* Attach interface */
   1633 	if_attach(ifp);
   1634 	if_deferred_start_init(ifp, NULL);
   1635 
   1636 	/* Attach ethernet interface */
   1637 	ether_ifattach(ifp, eaddr);
   1638 
   1639 	eqos_init_sysctls(sc);
   1640 
   1641 	rnd_attach_source(&sc->sc_rndsource, ifp->if_xname, RND_TYPE_NET,
   1642 	    RND_FLAG_DEFAULT);
   1643 
   1644 	return 0;
   1645 }
   1646 
   1647 static void
   1648 eqos_init_sysctls(struct eqos_softc *sc)
   1649 {
   1650 	struct sysctllog **log;
   1651 	const struct sysctlnode *rnode, *qnode, *cnode;
   1652 	const char *dvname;
   1653 	int i, rv;
   1654 
   1655 	log = &sc->sc_sysctllog;
   1656 	dvname = device_xname(sc->sc_dev);
   1657 
   1658 	rv = sysctl_createv(log, 0, NULL, &rnode,
   1659 	    0, CTLTYPE_NODE, dvname,
   1660 	    SYSCTL_DESCR("eqos information and settings"),
   1661 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
   1662 	if (rv != 0)
   1663 		goto err;
   1664 
   1665 	for (i = 0; i < 1; i++) {
   1666 		struct eqos_ring *txr = &sc->sc_tx;
   1667 		struct eqos_ring *rxr = &sc->sc_rx;
   1668 		const unsigned char *name = "q0";
   1669 
   1670 		if (sysctl_createv(log, 0, &rnode, &qnode,
   1671 		    0, CTLTYPE_NODE,
   1672 		    name, SYSCTL_DESCR("Queue Name"),
   1673 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   1674 			break;
   1675 
   1676 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1677 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1678 		    "txs_cur", SYSCTL_DESCR("TX cur"),
   1679 		    NULL, 0, &txr->cur,
   1680 		    0, CTL_CREATE, CTL_EOL) != 0)
   1681 			break;
   1682 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1683 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1684 		    "txs_next", SYSCTL_DESCR("TX next"),
   1685 		    NULL, 0, &txr->next,
   1686 		    0, CTL_CREATE, CTL_EOL) != 0)
   1687 			break;
   1688 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1689 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1690 		    "txs_queued", SYSCTL_DESCR("TX queued"),
   1691 		    NULL, 0, &txr->queued,
   1692 		    0, CTL_CREATE, CTL_EOL) != 0)
   1693 			break;
   1694 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1695 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1696 		    "txr_cur", SYSCTL_DESCR("TX descriptor cur"),
   1697 		    eqos_sysctl_tx_cur_handler, 0, (void *)txr,
   1698 		    0, CTL_CREATE, CTL_EOL) != 0)
   1699 			break;
   1700 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1701 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1702 		    "txr_end", SYSCTL_DESCR("TX descriptor end"),
   1703 		    eqos_sysctl_tx_end_handler, 0, (void *)txr,
   1704 		    0, CTL_CREATE, CTL_EOL) != 0)
   1705 			break;
   1706 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1707 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1708 		    "rxs_cur", SYSCTL_DESCR("RX cur"),
   1709 		    NULL, 0, &rxr->cur,
   1710 		    0, CTL_CREATE, CTL_EOL) != 0)
   1711 			break;
   1712 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1713 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1714 		    "rxs_next", SYSCTL_DESCR("RX next"),
   1715 		    NULL, 0, &rxr->next,
   1716 		    0, CTL_CREATE, CTL_EOL) != 0)
   1717 			break;
   1718 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1719 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1720 		    "rxs_queued", SYSCTL_DESCR("RX queued"),
   1721 		    NULL, 0, &rxr->queued,
   1722 		    0, CTL_CREATE, CTL_EOL) != 0)
   1723 			break;
   1724 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1725 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1726 		    "rxr_cur", SYSCTL_DESCR("RX descriptor cur"),
   1727 		    eqos_sysctl_rx_cur_handler, 0, (void *)rxr,
   1728 		    0, CTL_CREATE, CTL_EOL) != 0)
   1729 			break;
   1730 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1731 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1732 		    "rxr_end", SYSCTL_DESCR("RX descriptor end"),
   1733 		    eqos_sysctl_rx_end_handler, 0, (void *)rxr,
   1734 		    0, CTL_CREATE, CTL_EOL) != 0)
   1735 			break;
   1736 	}
   1737 
   1738 #ifdef EQOS_DEBUG
   1739 	rv = sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   1740 	    CTLTYPE_INT, "debug_flags",
   1741 	    SYSCTL_DESCR(
   1742 		    "Debug flags:\n"	\
   1743 		    "\t0x01 NOTE\n"	\
   1744 		    "\t0x02 INTR\n"	\
   1745 		    "\t0x04 RX RING\n"	\
   1746 		    "\t0x08 TX RING\n"),
   1747 	    eqos_sysctl_debug_handler, 0, (void *)sc, 0, CTL_CREATE, CTL_EOL);
   1748 #endif
   1749 
   1750 	return;
   1751 
   1752 err:
   1753 	sc->sc_sysctllog = NULL;
   1754 	device_printf(sc->sc_dev, "%s: sysctl_createv failed, rv = %d\n",
   1755 	    __func__, rv);
   1756 }
   1757 
   1758 static int
   1759 eqos_sysctl_tx_cur_handler(SYSCTLFN_ARGS)
   1760 {
   1761 	struct sysctlnode node = *rnode;
   1762 	struct eqos_ring *txq = (struct eqos_ring *)node.sysctl_data;
   1763 	struct eqos_softc *sc = txq->sc;
   1764 	uint32_t reg, index;
   1765 
   1766 	reg = RD4(sc, GMAC_DMA_CHAN0_CUR_TX_DESC);
   1767 #if 0
   1768 	printf("head  = %08x\n", (uint32_t)sc->sc_tx.desc_ring_paddr);
   1769 	printf("cdesc = %08x\n", reg);
   1770 	printf("index = %zu\n",
   1771 	    (reg - (uint32_t)sc->sc_tx.desc_ring_paddr) /
   1772 	    sizeof(struct eqos_dma_desc));
   1773 #endif
   1774 	if (reg == 0)
   1775 		index = 0;
   1776 	else {
   1777 		index = (reg - (uint32_t)sc->sc_tx.desc_ring_paddr) /
   1778 		    sizeof(struct eqos_dma_desc);
   1779 	}
   1780 	node.sysctl_data = &index;
   1781 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1782 }
   1783 
   1784 static int
   1785 eqos_sysctl_tx_end_handler(SYSCTLFN_ARGS)
   1786 {
   1787 	struct sysctlnode node = *rnode;
   1788 	struct eqos_ring *txq = (struct eqos_ring *)node.sysctl_data;
   1789 	struct eqos_softc *sc = txq->sc;
   1790 	uint32_t reg, index;
   1791 
   1792 	reg = RD4(sc, GMAC_DMA_CHAN0_TX_END_ADDR);
   1793 	if (reg == 0)
   1794 		index = 0;
   1795 	else {
   1796 		index = (reg - (uint32_t)sc->sc_tx.desc_ring_paddr) /
   1797 		    sizeof(struct eqos_dma_desc);
   1798 	}
   1799 	node.sysctl_data = &index;
   1800 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1801 }
   1802 
   1803 static int
   1804 eqos_sysctl_rx_cur_handler(SYSCTLFN_ARGS)
   1805 {
   1806 	struct sysctlnode node = *rnode;
   1807 	struct eqos_ring *rxq = (struct eqos_ring *)node.sysctl_data;
   1808 	struct eqos_softc *sc = rxq->sc;
   1809 	uint32_t reg, index;
   1810 
   1811 	reg = RD4(sc, GMAC_DMA_CHAN0_CUR_RX_DESC);
   1812 	if (reg == 0)
   1813 		index = 0;
   1814 	else {
   1815 		index = (reg - (uint32_t)sc->sc_rx.desc_ring_paddr) /
   1816 		    sizeof(struct eqos_dma_desc);
   1817 	}
   1818 	node.sysctl_data = &index;
   1819 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1820 }
   1821 
   1822 static int
   1823 eqos_sysctl_rx_end_handler(SYSCTLFN_ARGS)
   1824 {
   1825 	struct sysctlnode node = *rnode;
   1826 	struct eqos_ring *rxq = (struct eqos_ring *)node.sysctl_data;
   1827 	struct eqos_softc *sc = rxq->sc;
   1828 	uint32_t reg, index;
   1829 
   1830 	reg = RD4(sc, GMAC_DMA_CHAN0_RX_END_ADDR);
   1831 	if (reg == 0)
   1832 		index = 0;
   1833 	else {
   1834 		index = (reg - (uint32_t)sc->sc_rx.desc_ring_paddr) /
   1835 		    sizeof(struct eqos_dma_desc);
   1836 	}
   1837 	node.sysctl_data = &index;
   1838 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1839 }
   1840 
   1841 #ifdef EQOS_DEBUG
   1842 static int
   1843 eqos_sysctl_debug_handler(SYSCTLFN_ARGS)
   1844 {
   1845 	struct sysctlnode node = *rnode;
   1846 	struct eqos_softc *sc = (struct eqos_softc *)node.sysctl_data;
   1847 	uint32_t dflags;
   1848 	int error;
   1849 
   1850 	dflags = sc->sc_debug;
   1851 	node.sysctl_data = &dflags;
   1852 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1853 
   1854 	if (error || newp == NULL)
   1855 		return error;
   1856 
   1857 	sc->sc_debug = dflags;
   1858 #if 0
   1859 	/* Addd debug code here if you want. */
   1860 #endif
   1861 
   1862 	return 0;
   1863 }
   1864 #endif
   1865