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