Home | History | Annotate | Line # | Download | only in ic
dwc_eqos.c revision 1.2
      1 /* $NetBSD: dwc_eqos.c,v 1.2 2022/01/08 22:24:53 mrg Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2022 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * DesignWare Ethernet Quality-of-Service controller
     31  */
     32 
     33 #include "opt_net_mpsafe.h"
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: dwc_eqos.c,v 1.2 2022/01/08 22:24:53 mrg Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/bus.h>
     40 #include <sys/device.h>
     41 #include <sys/intr.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/mutex.h>
     45 #include <sys/callout.h>
     46 #include <sys/cprng.h>
     47 #include <sys/evcnt.h>
     48 
     49 #include <sys/rndsource.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_dl.h>
     53 #include <net/if_ether.h>
     54 #include <net/if_media.h>
     55 #include <net/bpf.h>
     56 
     57 #include <dev/mii/miivar.h>
     58 
     59 #include <dev/ic/dwc_eqos_reg.h>
     60 #include <dev/ic/dwc_eqos_var.h>
     61 
     62 CTASSERT(MCLBYTES == 2048);
     63 #ifdef EQOS_DEBUG
     64 #define	DPRINTF(...)	printf(##__VA_ARGS__)
     65 #else
     66 #define	DPRINTF(...)	((void)0)
     67 #endif
     68 
     69 #ifdef NET_MPSAFE
     70 #define	EQOS_MPSAFE		1
     71 #define	CALLOUT_FLAGS		CALLOUT_MPSAFE
     72 #else
     73 #define	CALLOUT_FLAGS		0
     74 #endif
     75 
     76 #define	DESC_BOUNDARY		(1ULL << 32)
     77 #define	DESC_ALIGN		sizeof(struct eqos_dma_desc)
     78 #define	TX_DESC_COUNT		EQOS_DMA_DESC_COUNT
     79 #define	TX_DESC_SIZE		(TX_DESC_COUNT * DESC_ALIGN)
     80 #define	RX_DESC_COUNT		EQOS_DMA_DESC_COUNT
     81 #define	RX_DESC_SIZE		(RX_DESC_COUNT * DESC_ALIGN)
     82 #define	MII_BUSY_RETRY		1000
     83 
     84 #define	DESC_OFF(n)		((n) * sizeof(struct eqos_dma_desc))
     85 #define	TX_SKIP(n, o)		(((n) + (o)) % TX_DESC_COUNT)
     86 #define	TX_NEXT(n)		TX_SKIP(n, 1)
     87 #define	RX_NEXT(n)		(((n) + 1) % RX_DESC_COUNT)
     88 
     89 #define	TX_MAX_SEGS		128
     90 
     91 #define	EQOS_LOCK(sc)			mutex_enter(&(sc)->sc_lock)
     92 #define	EQOS_UNLOCK(sc)			mutex_exit(&(sc)->sc_lock)
     93 #define	EQOS_ASSERT_LOCKED(sc)		KASSERT(mutex_owned(&(sc)->sc_lock))
     94 
     95 #define	EQOS_TXLOCK(sc)			mutex_enter(&(sc)->sc_txlock)
     96 #define	EQOS_TXUNLOCK(sc)		mutex_exit(&(sc)->sc_txlock)
     97 #define	EQOS_ASSERT_TXLOCKED(sc)	KASSERT(mutex_owned(&(sc)->sc_txlock))
     98 
     99 #define	EQOS_HW_FEATURE_ADDR64_32BIT(sc)				\
    100 	(((sc)->sc_hw_feature[1] & GMAC_MAC_HW_FEATURE1_ADDR64_MASK) ==	\
    101 	    GMAC_MAC_HW_FEATURE1_ADDR64_32BIT)
    102 
    103 
    104 #define	RD4(sc, reg)			\
    105 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    106 #define	WR4(sc, reg, val)		\
    107 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    108 
    109 #define STUB(...)				\
    110 	printf("%s: TODO\n", __func__);	\
    111 
    112 static int
    113 eqos_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
    114 {
    115 	struct eqos_softc *sc = device_private(dev);
    116 	uint32_t addr;
    117 	int retry;
    118 
    119 	addr = sc->sc_clock_range |
    120 	    (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
    121 	    (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
    122 	    GMAC_MAC_MDIO_ADDRESS_GOC_READ |
    123 	    GMAC_MAC_MDIO_ADDRESS_GB;
    124 	WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
    125 
    126 	delay(10000);
    127 
    128 	for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
    129 		addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
    130 		if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
    131 			*val = RD4(sc, GMAC_MAC_MDIO_DATA) & 0xFFFF;
    132 			break;
    133 		}
    134 		delay(10);
    135 	}
    136 	if (retry == 0) {
    137 		device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
    138 		    phy, reg);
    139 		return ETIMEDOUT;
    140 	}
    141 
    142 	return 0;
    143 }
    144 
    145 static int
    146 eqos_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
    147 {
    148 	struct eqos_softc *sc = device_private(dev);
    149 	uint32_t addr;
    150 	int retry;
    151 
    152 	WR4(sc, GMAC_MAC_MDIO_DATA, val);
    153 
    154 	addr = sc->sc_clock_range |
    155 	    (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
    156 	    (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
    157 	    GMAC_MAC_MDIO_ADDRESS_GOC_WRITE |
    158 	    GMAC_MAC_MDIO_ADDRESS_GB;
    159 	WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
    160 
    161 	delay(10000);
    162 
    163 	for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
    164 		addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
    165 		if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
    166 			break;
    167 		}
    168 		delay(10);
    169 	}
    170 	if (retry == 0) {
    171 		device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
    172 		    phy, reg);
    173 		return ETIMEDOUT;
    174 	}
    175 
    176 	return 0;
    177 }
    178 
    179 static void
    180 eqos_update_link(struct eqos_softc *sc)
    181 {
    182 	struct mii_data *mii = &sc->sc_mii;
    183 	uint64_t baudrate;
    184 	uint32_t conf;
    185 
    186 	baudrate = ifmedia_baudrate(mii->mii_media_active);
    187 
    188 	conf = RD4(sc, GMAC_MAC_CONFIGURATION);
    189 	switch (baudrate) {
    190 	case IF_Mbps(10):
    191 		conf |= GMAC_MAC_CONFIGURATION_PS;
    192 		conf &= ~GMAC_MAC_CONFIGURATION_FES;
    193 		break;
    194 	case IF_Mbps(100):
    195 		conf |= GMAC_MAC_CONFIGURATION_PS;
    196 		conf |= GMAC_MAC_CONFIGURATION_FES;
    197 		break;
    198 	case IF_Gbps(1):
    199 		conf &= ~GMAC_MAC_CONFIGURATION_PS;
    200 		conf &= ~GMAC_MAC_CONFIGURATION_FES;
    201 		break;
    202 	case IF_Mbps(2500ULL):
    203 		conf &= ~GMAC_MAC_CONFIGURATION_PS;
    204 		conf |= GMAC_MAC_CONFIGURATION_FES;
    205 		break;
    206 	}
    207 
    208 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
    209 		conf |= GMAC_MAC_CONFIGURATION_DM;
    210 	} else {
    211 		conf &= ~GMAC_MAC_CONFIGURATION_DM;
    212 	}
    213 
    214 	WR4(sc, GMAC_MAC_CONFIGURATION, conf);
    215 }
    216 
    217 static void
    218 eqos_mii_statchg(struct ifnet *ifp)
    219 {
    220 	struct eqos_softc * const sc = ifp->if_softc;
    221 
    222 	eqos_update_link(sc);
    223 }
    224 
    225 static void
    226 eqos_dma_sync(struct eqos_softc *sc, bus_dmamap_t map,
    227     u_int start, u_int end, u_int total, int flags)
    228 {
    229 	if (end > start) {
    230 		bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
    231 		    DESC_OFF(end) - DESC_OFF(start), flags);
    232 	} else {
    233 		bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
    234 		    DESC_OFF(total) - DESC_OFF(start), flags);
    235 		if (DESC_OFF(end) - DESC_OFF(0) > 0) {
    236 			bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(0),
    237 			    DESC_OFF(end) - DESC_OFF(0), flags);
    238 		}
    239 	}
    240 }
    241 
    242 static void
    243 eqos_setup_txdesc(struct eqos_softc *sc, int index, int flags,
    244     bus_addr_t paddr, u_int len, u_int total_len)
    245 {
    246 	uint32_t tdes2, tdes3;
    247 
    248 	if (paddr == 0 || len == 0) {
    249 		KASSERT(flags == 0);
    250 		tdes2 = 0;
    251 		tdes3 = 0;
    252 		--sc->sc_tx.queued;
    253 	} else {
    254 		tdes2 = (flags & EQOS_TDES3_LD) ? EQOS_TDES2_IOC : 0;
    255 		tdes3 = flags;
    256 		++sc->sc_tx.queued;
    257 	}
    258 
    259 	KASSERT(!EQOS_HW_FEATURE_ADDR64_32BIT(sc) || (paddr >> 32) == 0);
    260 
    261 	sc->sc_tx.desc_ring[index].tdes0 = htole32((uint32_t)paddr);
    262 	sc->sc_tx.desc_ring[index].tdes1 = htole32((uint32_t)(paddr >> 32));
    263 	sc->sc_tx.desc_ring[index].tdes2 = htole32(tdes2 | len);
    264 	sc->sc_tx.desc_ring[index].tdes3 = htole32(tdes3 | total_len);
    265 }
    266 
    267 static int
    268 eqos_setup_txbuf(struct eqos_softc *sc, int index, struct mbuf *m)
    269 {
    270 	bus_dma_segment_t *segs;
    271 	int error, nsegs, cur, i;
    272 	uint32_t flags;
    273 	bool nospace;
    274 
    275 	/* at least one descriptor free ? */
    276 	if (sc->sc_tx.queued >= TX_DESC_COUNT - 1)
    277 		return -1;
    278 
    279 	error = bus_dmamap_load_mbuf(sc->sc_dmat,
    280 	    sc->sc_tx.buf_map[index].map, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
    281 	if (error == EFBIG) {
    282 		device_printf(sc->sc_dev,
    283 		    "TX packet needs too many DMA segments, dropping...\n");
    284 		return -2;
    285 	}
    286 	if (error != 0) {
    287 		device_printf(sc->sc_dev,
    288 		    "TX packet cannot be mapped, retried...\n");
    289 		return 0;
    290 	}
    291 
    292 	segs = sc->sc_tx.buf_map[index].map->dm_segs;
    293 	nsegs = sc->sc_tx.buf_map[index].map->dm_nsegs;
    294 
    295 	nospace = sc->sc_tx.queued >= TX_DESC_COUNT - nsegs;
    296 	if (nospace) {
    297 		bus_dmamap_unload(sc->sc_dmat,
    298 		    sc->sc_tx.buf_map[index].map);
    299 		/* XXX coalesce and retry ? */
    300 		return -1;
    301 	}
    302 
    303 	bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.buf_map[index].map,
    304 	    0, sc->sc_tx.buf_map[index].map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    305 
    306 	/* stored in same index as loaded map */
    307 	sc->sc_tx.buf_map[index].mbuf = m;
    308 
    309 	flags = EQOS_TDES3_FD;
    310 
    311 	for (cur = index, i = 0; i < nsegs; i++) {
    312 		if (i == nsegs - 1)
    313 			flags |= EQOS_TDES3_LD;
    314 
    315 		eqos_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
    316 		    segs[i].ds_len, m->m_pkthdr.len);
    317 		flags &= ~EQOS_TDES3_FD;
    318 		cur = TX_NEXT(cur);
    319 
    320 		flags |= EQOS_TDES3_OWN;
    321 	}
    322 
    323 	/*
    324 	 * Defer setting OWN bit on the first descriptor until all
    325 	 * descriptors have been updated.
    326 	 */
    327 	membar_sync();
    328 	sc->sc_tx.desc_ring[index].tdes3 |= htole32(EQOS_TDES3_OWN);
    329 
    330 	return nsegs;
    331 }
    332 
    333 static void
    334 eqos_setup_rxdesc(struct eqos_softc *sc, int index, bus_addr_t paddr)
    335 {
    336 	sc->sc_rx.desc_ring[index].tdes0 = htole32((uint32_t)paddr);
    337 	sc->sc_rx.desc_ring[index].tdes1 = htole32((uint32_t)(paddr >> 32));
    338 	sc->sc_rx.desc_ring[index].tdes2 = htole32(0);
    339 	membar_sync();
    340 	sc->sc_rx.desc_ring[index].tdes3 =
    341 	    htole32(EQOS_TDES3_OWN | EQOS_TDES3_IOC | EQOS_TDES3_BUF1V);
    342 }
    343 
    344 static int
    345 eqos_setup_rxbuf(struct eqos_softc *sc, int index, struct mbuf *m)
    346 {
    347 	int error;
    348 
    349 	m_adj(m, ETHER_ALIGN);
    350 
    351 	error = bus_dmamap_load_mbuf(sc->sc_dmat,
    352 	    sc->sc_rx.buf_map[index].map, m, BUS_DMA_READ | BUS_DMA_NOWAIT);
    353 	if (error != 0)
    354 		return error;
    355 
    356 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
    357 	    0, sc->sc_rx.buf_map[index].map->dm_mapsize,
    358 	    BUS_DMASYNC_PREREAD);
    359 
    360 	sc->sc_rx.buf_map[index].mbuf = m;
    361 	eqos_setup_rxdesc(sc, index,
    362 	    sc->sc_rx.buf_map[index].map->dm_segs[0].ds_addr);
    363 
    364 	return 0;
    365 }
    366 
    367 static struct mbuf *
    368 eqos_alloc_mbufcl(struct eqos_softc *sc)
    369 {
    370 	struct mbuf *m;
    371 
    372 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
    373 	if (m != NULL)
    374 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
    375 
    376 	return m;
    377 }
    378 
    379 static void
    380 eqos_enable_intr(struct eqos_softc *sc)
    381 {
    382 	WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE,
    383 	    GMAC_DMA_CHAN0_INTR_ENABLE_NIE |
    384 	    GMAC_DMA_CHAN0_INTR_ENABLE_AIE |
    385 	    GMAC_DMA_CHAN0_INTR_ENABLE_FBE |
    386 	    GMAC_DMA_CHAN0_INTR_ENABLE_RIE |
    387 	    GMAC_DMA_CHAN0_INTR_ENABLE_TIE);
    388 }
    389 
    390 static void
    391 eqos_disable_intr(struct eqos_softc *sc)
    392 {
    393 	WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, 0);
    394 }
    395 
    396 static void
    397 eqos_tick(void *softc)
    398 {
    399 	struct eqos_softc *sc = softc;
    400 	struct mii_data *mii = &sc->sc_mii;
    401 #ifndef EQOS_MPSAFE
    402 	int s = splnet();
    403 #endif
    404 
    405 	EQOS_LOCK(sc);
    406 	mii_tick(mii);
    407 	callout_schedule(&sc->sc_stat_ch, hz);
    408 	EQOS_UNLOCK(sc);
    409 
    410 #ifndef EQOS_MPSAFE
    411 	splx(s);
    412 #endif
    413 }
    414 
    415 static uint32_t
    416 eqos_bitrev32(uint32_t x)
    417 {
    418 	x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
    419 	x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
    420 	x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
    421 	x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
    422 
    423 	return (x >> 16) | (x << 16);
    424 }
    425 
    426 static void
    427 eqos_setup_rxfilter(struct eqos_softc *sc)
    428 {
    429 	struct ethercom *ec = &sc->sc_ec;
    430 	struct ifnet *ifp = &ec->ec_if;
    431 	uint32_t pfil, crc, hashreg, hashbit, hash[2];
    432 	struct ether_multi *enm;
    433 	struct ether_multistep step;
    434 	const uint8_t *eaddr;
    435 	uint32_t val;
    436 
    437 	EQOS_ASSERT_LOCKED(sc);
    438 
    439 	pfil = RD4(sc, GMAC_MAC_PACKET_FILTER);
    440 	pfil &= ~(GMAC_MAC_PACKET_FILTER_PR |
    441 		  GMAC_MAC_PACKET_FILTER_PM |
    442 		  GMAC_MAC_PACKET_FILTER_HMC |
    443 		  GMAC_MAC_PACKET_FILTER_PCF_MASK);
    444 	hash[0] = hash[1] = ~0U;
    445 
    446 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    447 		pfil |= GMAC_MAC_PACKET_FILTER_PR |
    448 			GMAC_MAC_PACKET_FILTER_PCF_ALL;
    449 	} else if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
    450 		pfil |= GMAC_MAC_PACKET_FILTER_PM;
    451 	} else {
    452 		hash[0] = hash[1] = 0;
    453 		pfil |= GMAC_MAC_PACKET_FILTER_HMC;
    454 		ETHER_LOCK(ec);
    455 		ETHER_FIRST_MULTI(step, ec, enm);
    456 		while (enm != NULL) {
    457 			crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    458 			crc &= 0x7f;
    459 			crc = eqos_bitrev32(~crc) >> 26;
    460 			hashreg = (crc >> 5);
    461 			hashbit = (crc & 0x1f);
    462 			hash[hashreg] |= (1 << hashbit);
    463 			ETHER_NEXT_MULTI(step, enm);
    464 		}
    465 		ETHER_UNLOCK(ec);
    466 	}
    467 
    468 	/* Write our unicast address */
    469 	eaddr = CLLADDR(ifp->if_sadl);
    470 	val = eaddr[4] | (eaddr[5] << 8);
    471 	WR4(sc, GMAC_MAC_ADDRESS0_HIGH, val);
    472 	val = eaddr[0] | (eaddr[1] << 8) | (eaddr[2] << 16) |
    473 	    (eaddr[3] << 24);
    474 	WR4(sc, GMAC_MAC_ADDRESS0_LOW, val);
    475 
    476 	/* Multicast hash filters */
    477 	WR4(sc, GMAC_MAC_HASH_TABLE_REG0, hash[1]);
    478 	WR4(sc, GMAC_MAC_HASH_TABLE_REG1, hash[0]);
    479 
    480 	/* Packet filter config */
    481 	WR4(sc, GMAC_MAC_PACKET_FILTER, pfil);
    482 }
    483 
    484 static int
    485 eqos_reset(struct eqos_softc *sc)
    486 {
    487 	uint32_t val;
    488 	int retry;
    489 
    490 	WR4(sc, GMAC_DMA_MODE, GMAC_DMA_MODE_SWR);
    491 	for (retry = 2000; retry > 0; retry--) {
    492 		delay(1000);
    493 		val = RD4(sc, GMAC_DMA_MODE);
    494 		if ((val & GMAC_DMA_MODE_SWR) == 0) {
    495 			return 0;
    496 		}
    497 	}
    498 
    499 	device_printf(sc->sc_dev, "reset timeout!\n");
    500 	return ETIMEDOUT;
    501 }
    502 
    503 static void
    504 eqos_init_rings(struct eqos_softc *sc, int qid)
    505 {
    506 	sc->sc_tx.queued = 0;
    507 
    508 	WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR_HI,
    509 	    (uint32_t)(sc->sc_tx.desc_ring_paddr >> 32));
    510 	WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR,
    511 	    (uint32_t)sc->sc_tx.desc_ring_paddr);
    512 	WR4(sc, GMAC_DMA_CHAN0_TX_RING_LEN, TX_DESC_COUNT - 1);
    513 
    514 	WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR_HI,
    515 	    (uint32_t)(sc->sc_rx.desc_ring_paddr >> 32));
    516 	WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR,
    517 	    (uint32_t)sc->sc_rx.desc_ring_paddr);
    518 	WR4(sc, GMAC_DMA_CHAN0_RX_RING_LEN, RX_DESC_COUNT - 1);
    519 	WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
    520 	    (uint32_t)sc->sc_rx.desc_ring_paddr +
    521 	    DESC_OFF((sc->sc_rx.cur - 1) % RX_DESC_COUNT));
    522 }
    523 
    524 static int
    525 eqos_init_locked(struct eqos_softc *sc)
    526 {
    527 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    528 	struct mii_data *mii = &sc->sc_mii;
    529 	uint32_t val;
    530 
    531 	EQOS_ASSERT_LOCKED(sc);
    532 	EQOS_ASSERT_TXLOCKED(sc);
    533 
    534 	if ((ifp->if_flags & IFF_RUNNING) != 0)
    535 		return 0;
    536 
    537 	/* Setup TX/RX rings */
    538 	eqos_init_rings(sc, 0);
    539 
    540 	/* Setup RX filter */
    541 	eqos_setup_rxfilter(sc);
    542 
    543 	WR4(sc, GMAC_MAC_1US_TIC_COUNTER, (sc->sc_csr_clock / 1000000) - 1);
    544 
    545 	/* Enable transmit and receive DMA */
    546 	val = RD4(sc, GMAC_DMA_CHAN0_CONTROL);
    547 	val &= ~GMAC_DMA_CHAN0_CONTROL_DSL_MASK;
    548 	val |= ((DESC_ALIGN - 16) / 8) << GMAC_DMA_CHAN0_CONTROL_DSL_SHIFT;
    549 	val |= GMAC_DMA_CHAN0_CONTROL_PBLX8;
    550 	WR4(sc, GMAC_DMA_CHAN0_CONTROL, val);
    551 	val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
    552 	val |= GMAC_DMA_CHAN0_TX_CONTROL_OSP;
    553 	val |= GMAC_DMA_CHAN0_TX_CONTROL_START;
    554 	WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
    555 	val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
    556 	val &= ~GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_MASK;
    557 	val |= (MCLBYTES << GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_SHIFT);
    558 	val |= GMAC_DMA_CHAN0_RX_CONTROL_START;
    559 	WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
    560 
    561 	/* Configure operation modes */
    562 	WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE,
    563 	    GMAC_MTL_TXQ0_OPERATION_MODE_TSF |
    564 	    GMAC_MTL_TXQ0_OPERATION_MODE_TXQEN_EN);
    565 	WR4(sc, GMAC_MTL_RXQ0_OPERATION_MODE,
    566 	    GMAC_MTL_RXQ0_OPERATION_MODE_RSF |
    567 	    GMAC_MTL_RXQ0_OPERATION_MODE_FEP |
    568 	    GMAC_MTL_RXQ0_OPERATION_MODE_FUP);
    569 
    570 	/* Enable flow control */
    571 	val = RD4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL);
    572 	val |= 0xFFFFU << GMAC_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT;
    573 	val |= GMAC_MAC_Q0_TX_FLOW_CTRL_TFE;
    574 	WR4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL, val);
    575 	val = RD4(sc, GMAC_MAC_RX_FLOW_CTRL);
    576 	val |= GMAC_MAC_RX_FLOW_CTRL_RFE;
    577 	WR4(sc, GMAC_MAC_RX_FLOW_CTRL, val);
    578 
    579 	/* Enable transmitter and receiver */
    580 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    581 	val |= GMAC_MAC_CONFIGURATION_BE;
    582 	val |= GMAC_MAC_CONFIGURATION_JD;
    583 	val |= GMAC_MAC_CONFIGURATION_JE;
    584 	val |= GMAC_MAC_CONFIGURATION_DCRS;
    585 	val |= GMAC_MAC_CONFIGURATION_TE;
    586 	val |= GMAC_MAC_CONFIGURATION_RE;
    587 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    588 
    589 	/* Enable interrupts */
    590 	eqos_enable_intr(sc);
    591 
    592 	ifp->if_flags |= IFF_RUNNING;
    593 	ifp->if_flags &= ~IFF_OACTIVE;
    594 
    595 	mii_mediachg(mii);
    596 	callout_schedule(&sc->sc_stat_ch, hz);
    597 
    598 	return 0;
    599 }
    600 
    601 static int
    602 eqos_init(struct ifnet *ifp)
    603 {
    604 	struct eqos_softc *sc = ifp->if_softc;
    605 	int error;
    606 
    607 	EQOS_LOCK(sc);
    608 	EQOS_TXLOCK(sc);
    609 	error = eqos_init_locked(sc);
    610 	EQOS_TXUNLOCK(sc);
    611 	EQOS_UNLOCK(sc);
    612 
    613 	return error;
    614 }
    615 
    616 static void
    617 eqos_stop_locked(struct eqos_softc *sc, int disable)
    618 {
    619 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    620 	uint32_t val;
    621 	int retry;
    622 
    623 	EQOS_ASSERT_LOCKED(sc);
    624 
    625 	callout_stop(&sc->sc_stat_ch);
    626 
    627 	mii_down(&sc->sc_mii);
    628 
    629 	/* Disable receiver */
    630 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    631 	val &= ~GMAC_MAC_CONFIGURATION_RE;
    632 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    633 
    634 	/* Stop receive DMA */
    635 	val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
    636 	val &= ~GMAC_DMA_CHAN0_RX_CONTROL_START;
    637 	WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
    638 
    639 	/* Stop transmit DMA */
    640 	val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
    641 	val &= ~GMAC_DMA_CHAN0_TX_CONTROL_START;
    642 	WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
    643 
    644 	if (disable) {
    645 		/* Flush data in the TX FIFO */
    646 		val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
    647 		val |= GMAC_MTL_TXQ0_OPERATION_MODE_FTQ;
    648 		WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE, val);
    649 		/* Wait for flush to complete */
    650 		for (retry = 10000; retry > 0; retry--) {
    651 			val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
    652 			if ((val & GMAC_MTL_TXQ0_OPERATION_MODE_FTQ) == 0) {
    653 				break;
    654 			}
    655 			delay(1);
    656 		}
    657 		if (retry == 0) {
    658 			device_printf(sc->sc_dev,
    659 			    "timeout flushing TX queue\n");
    660 		}
    661 	}
    662 
    663 	/* Disable transmitter */
    664 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    665 	val &= ~GMAC_MAC_CONFIGURATION_TE;
    666 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    667 
    668 	/* Disable interrupts */
    669 	eqos_disable_intr(sc);
    670 
    671 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    672 }
    673 
    674 static void
    675 eqos_stop(struct ifnet *ifp, int disable)
    676 {
    677 	struct eqos_softc * const sc = ifp->if_softc;
    678 
    679 	EQOS_LOCK(sc);
    680 	eqos_stop_locked(sc, disable);
    681 	EQOS_UNLOCK(sc);
    682 }
    683 
    684 static void
    685 eqos_rxintr(struct eqos_softc *sc, int qid)
    686 {
    687 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    688 	int error, index, len, pkts = 0;
    689 	struct mbuf *m, *m0;
    690 	uint32_t tdes3;
    691 
    692 	for (index = sc->sc_rx.cur; ; index = RX_NEXT(index)) {
    693 		eqos_dma_sync(sc, sc->sc_rx.desc_map,
    694 		    index, index + 1, RX_DESC_COUNT,
    695 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    696 
    697 		tdes3 = le32toh(sc->sc_rx.desc_ring[index].tdes3);
    698 		if ((tdes3 & EQOS_TDES3_OWN) != 0) {
    699 			break;
    700 		}
    701 
    702 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
    703 		    0, sc->sc_rx.buf_map[index].map->dm_mapsize,
    704 		    BUS_DMASYNC_POSTREAD);
    705 		bus_dmamap_unload(sc->sc_dmat,
    706 		    sc->sc_rx.buf_map[index].map);
    707 
    708 		len = tdes3 & EQOS_TDES3_LENGTH_MASK;
    709 		if (len != 0) {
    710 			m = sc->sc_rx.buf_map[index].mbuf;
    711 			m_set_rcvif(m, ifp);
    712 			m->m_flags |= M_HASFCS;
    713 			m->m_pkthdr.len = len;
    714 			m->m_len = len;
    715 			m->m_nextpkt = NULL;
    716 
    717 			if_percpuq_enqueue(ifp->if_percpuq, m);
    718 			++pkts;
    719 		}
    720 
    721 		if ((m0 = eqos_alloc_mbufcl(sc)) != NULL) {
    722 			error = eqos_setup_rxbuf(sc, index, m0);
    723 			if (error != 0) {
    724 				/* XXX hole in RX ring */
    725 			}
    726 		} else {
    727 			if_statinc(ifp, if_ierrors);
    728 		}
    729 		eqos_dma_sync(sc, sc->sc_rx.desc_map,
    730 		    index, index + 1, RX_DESC_COUNT,
    731 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    732 
    733 		WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
    734 		    (uint32_t)sc->sc_rx.desc_ring_paddr +
    735 		    DESC_OFF(sc->sc_rx.cur));
    736 	}
    737 
    738 	sc->sc_rx.cur = index;
    739 
    740 	if (pkts != 0) {
    741 		rnd_add_uint32(&sc->sc_rndsource, pkts);
    742 	}
    743 }
    744 
    745 static void
    746 eqos_txintr(struct eqos_softc *sc, int qid)
    747 {
    748 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    749 	struct eqos_bufmap *bmap;
    750 	struct eqos_dma_desc *desc;
    751 	uint32_t tdes3;
    752 	int i, pkts = 0;
    753 
    754 	EQOS_ASSERT_LOCKED(sc);
    755 
    756 	for (i = sc->sc_tx.next; sc->sc_tx.queued > 0; i = TX_NEXT(i)) {
    757 		KASSERT(sc->sc_tx.queued > 0);
    758 		KASSERT(sc->sc_tx.queued <= TX_DESC_COUNT);
    759 		eqos_dma_sync(sc, sc->sc_tx.desc_map,
    760 		    i, i + 1, TX_DESC_COUNT,
    761 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    762 		desc = &sc->sc_tx.desc_ring[i];
    763 		tdes3 = le32toh(desc->tdes3);
    764 		if ((tdes3 & EQOS_TDES3_OWN) != 0) {
    765 			break;
    766 		}
    767 		bmap = &sc->sc_tx.buf_map[i];
    768 		if (bmap->mbuf != NULL) {
    769 			bus_dmamap_sync(sc->sc_dmat, bmap->map,
    770 			    0, bmap->map->dm_mapsize,
    771 			    BUS_DMASYNC_POSTWRITE);
    772 			bus_dmamap_unload(sc->sc_dmat, bmap->map);
    773 			m_freem(bmap->mbuf);
    774 			bmap->mbuf = NULL;
    775 			++pkts;
    776 		}
    777 
    778 		eqos_setup_txdesc(sc, i, 0, 0, 0, 0);
    779 		eqos_dma_sync(sc, sc->sc_tx.desc_map,
    780 		    i, i + 1, TX_DESC_COUNT,
    781 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    782 
    783 		ifp->if_flags &= ~IFF_OACTIVE;
    784 
    785 		/* Last descriptor in a packet contains DMA status */
    786 		if ((tdes3 & EQOS_TDES3_LD) != 0) {
    787 			if ((tdes3 & EQOS_TDES3_DE) != 0) {
    788 				device_printf(sc->sc_dev,
    789 				    "TX [%u] desc error: 0x%08x\n",
    790 				    i, tdes3);
    791 				if_statinc(ifp, if_oerrors);
    792 			} else if ((tdes3 & EQOS_TDES3_ES) != 0) {
    793 				device_printf(sc->sc_dev,
    794 				    "TX [%u] tx error: 0x%08x\n",
    795 				    i, tdes3);
    796 				if_statinc(ifp, if_oerrors);
    797 			} else {
    798 				if_statinc(ifp, if_opackets);
    799 			}
    800 		}
    801 
    802 	}
    803 
    804 	sc->sc_tx.next = i;
    805 
    806 	if (pkts != 0) {
    807 		rnd_add_uint32(&sc->sc_rndsource, pkts);
    808 	}
    809 }
    810 
    811 static void
    812 eqos_start_locked(struct eqos_softc *sc)
    813 {
    814 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    815 	struct mbuf *m;
    816 	int cnt, nsegs, start;
    817 
    818 	EQOS_ASSERT_TXLOCKED(sc);
    819 
    820 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    821 		return;
    822 
    823 	for (cnt = 0, start = sc->sc_tx.cur; ; cnt++) {
    824 		if (sc->sc_tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
    825 			ifp->if_flags |= IFF_OACTIVE;
    826 			break;
    827 		}
    828 
    829 		IFQ_POLL(&ifp->if_snd, m);
    830 		if (m == NULL) {
    831 			break;
    832 		}
    833 
    834 		nsegs = eqos_setup_txbuf(sc, sc->sc_tx.cur, m);
    835 		if (nsegs <= 0) {
    836 			if (nsegs == -1) {
    837 				ifp->if_flags |= IFF_OACTIVE;
    838 			} else if (nsegs == -2) {
    839 				IFQ_DEQUEUE(&ifp->if_snd, m);
    840 				m_freem(m);
    841 			}
    842 			break;
    843 		}
    844 
    845 		IFQ_DEQUEUE(&ifp->if_snd, m);
    846 		bpf_mtap(ifp, m, BPF_D_OUT);
    847 
    848 		sc->sc_tx.cur = TX_SKIP(sc->sc_tx.cur, nsegs);
    849 	}
    850 
    851 	if (cnt != 0) {
    852 		eqos_dma_sync(sc, sc->sc_tx.desc_map,
    853 		    start, sc->sc_tx.cur, TX_DESC_COUNT,
    854 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    855 
    856 		/* Start and run TX DMA */
    857 		WR4(sc, GMAC_DMA_CHAN0_TX_END_ADDR,
    858 		    (uint32_t)sc->sc_tx.desc_ring_paddr +
    859 		    DESC_OFF(sc->sc_tx.cur));
    860 	}
    861 }
    862 
    863 static void
    864 eqos_start(struct ifnet *ifp)
    865 {
    866 	struct eqos_softc *sc = ifp->if_softc;
    867 
    868 	EQOS_TXLOCK(sc);
    869 	eqos_start_locked(sc);
    870 	EQOS_TXUNLOCK(sc);
    871 }
    872 
    873 int
    874 eqos_intr(void *arg)
    875 {
    876 	struct eqos_softc *sc = arg;
    877 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    878 	uint32_t mac_status, mtl_status, dma_status, rx_tx_status;
    879 
    880 	sc->sc_ev_intr.ev_count++;
    881 
    882 	mac_status = RD4(sc, GMAC_MAC_INTERRUPT_STATUS);
    883 	mac_status &= RD4(sc, GMAC_MAC_INTERRUPT_ENABLE);
    884 
    885 	if (mac_status) {
    886 		sc->sc_ev_mac.ev_count++;
    887 #ifdef DEBUG_LOUD
    888 		device_printf(sc->sc_dev,
    889 		    "GMAC_MAC_INTERRUPT_STATUS = 0x%08X\n", mac_status);
    890 #endif
    891 	}
    892 
    893 	mtl_status = RD4(sc, GMAC_MTL_INTERRUPT_STATUS);
    894 	if (mtl_status) {
    895 		sc->sc_ev_mtl.ev_count++;
    896 #ifdef DEBUG_LOUD
    897 		device_printf(sc->sc_dev,
    898 		    "GMAC_MTL_INTERRUPT_STATUS = 0x%08X\n", mtl_status);
    899 #endif
    900 	}
    901 
    902 	dma_status = RD4(sc, GMAC_DMA_CHAN0_STATUS);
    903 	dma_status &= RD4(sc, GMAC_DMA_CHAN0_INTR_ENABLE);
    904 	if (dma_status) {
    905 		WR4(sc, GMAC_DMA_CHAN0_STATUS, dma_status);
    906 	}
    907 
    908 	EQOS_LOCK(sc);
    909 	if ((dma_status & GMAC_DMA_CHAN0_STATUS_RI) != 0) {
    910 		eqos_rxintr(sc, 0);
    911 		sc->sc_ev_rxintr.ev_count++;
    912 	}
    913 
    914 	if ((dma_status & GMAC_DMA_CHAN0_STATUS_TI) != 0) {
    915 		eqos_txintr(sc, 0);
    916 		if_schedule_deferred_start(ifp);
    917 		sc->sc_ev_txintr.ev_count++;
    918 	}
    919 	EQOS_UNLOCK(sc);
    920 
    921 #ifdef DEBUG_LOUD
    922 	if ((mac_status | mtl_status | dma_status) == 0) {
    923 		device_printf(sc->sc_dev, "spurious interrupt?!\n");
    924 	}
    925 #endif
    926 
    927 	rx_tx_status = RD4(sc, GMAC_MAC_RX_TX_STATUS);
    928 	if (rx_tx_status) {
    929 		sc->sc_ev_status.ev_count++;
    930 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_RWT) != 0)
    931 			sc->sc_ev_rwt.ev_count++;
    932 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXCOL) != 0)
    933 			sc->sc_ev_excol.ev_count++;
    934 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCOL) != 0)
    935 			sc->sc_ev_lcol.ev_count++;
    936 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXDEF) != 0)
    937 			sc->sc_ev_exdef.ev_count++;
    938 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCARR) != 0)
    939 			sc->sc_ev_lcarr.ev_count++;
    940 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_NCARR) != 0)
    941 			sc->sc_ev_ncarr.ev_count++;
    942 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_TJT) != 0)
    943 			sc->sc_ev_tjt.ev_count++;
    944 #ifdef DEBUG_LOUD
    945 		device_printf(sc->sc_dev, "GMAC_MAC_RX_TX_STATUS = 0x%08x\n",
    946 		    rx_tx_status);
    947 #endif
    948 	}
    949 
    950 	return 1;
    951 }
    952 
    953 static int
    954 eqos_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    955 {
    956 	struct eqos_softc *sc = ifp->if_softc;
    957 	int error, s;
    958 
    959 #ifndef EQOS_MPSAFE
    960 	s = splnet();
    961 #endif
    962 
    963 	switch (cmd) {
    964 	default:
    965 #ifdef EQOS_MPSAFE
    966 		s = splnet();
    967 #endif
    968 		error = ether_ioctl(ifp, cmd, data);
    969 #ifdef EQOS_MPSAFE
    970 		splx(s);
    971 #endif
    972 		if (error != ENETRESET)
    973 			break;
    974 
    975 		error = 0;
    976 
    977 		if (cmd == SIOCSIFCAP)
    978 			error = (*ifp->if_init)(ifp);
    979 		else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    980 			;
    981 		else if ((ifp->if_flags & IFF_RUNNING) != 0) {
    982 			EQOS_LOCK(sc);
    983 			eqos_setup_rxfilter(sc);
    984 			EQOS_UNLOCK(sc);
    985 		}
    986 		break;
    987 	}
    988 
    989 #ifndef EQOS_MPSAFE
    990 	splx(s);
    991 #endif
    992 
    993 	return error;
    994 }
    995 
    996 static void
    997 eqos_get_eaddr(struct eqos_softc *sc, uint8_t *eaddr)
    998 {
    999 	prop_dictionary_t prop = device_properties(sc->sc_dev);
   1000 	uint32_t maclo, machi;
   1001 	prop_data_t eaprop;
   1002 
   1003 	eaprop = prop_dictionary_get(prop, "mac-address");
   1004 	if (eaprop != NULL) {
   1005 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
   1006 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
   1007 		memcpy(eaddr, prop_data_value(eaprop),
   1008 		    ETHER_ADDR_LEN);
   1009 		return;
   1010 	}
   1011 
   1012 	maclo = htobe32(RD4(sc, GMAC_MAC_ADDRESS0_LOW));
   1013 	machi = htobe16(RD4(sc, GMAC_MAC_ADDRESS0_HIGH) & 0xFFFF);
   1014 
   1015 	if (maclo == 0xFFFFFFFF && machi == 0xFFFF) {
   1016 		/* Create one */
   1017 		maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
   1018 		machi = cprng_strong32() & 0xffff;
   1019 	}
   1020 
   1021 	eaddr[0] = maclo & 0xff;
   1022 	eaddr[1] = (maclo >> 8) & 0xff;
   1023 	eaddr[2] = (maclo >> 16) & 0xff;
   1024 	eaddr[3] = (maclo >> 24) & 0xff;
   1025 	eaddr[4] = machi & 0xff;
   1026 	eaddr[5] = (machi >> 8) & 0xff;
   1027 }
   1028 
   1029 static void
   1030 eqos_axi_configure(struct eqos_softc *sc)
   1031 {
   1032 	prop_dictionary_t prop = device_properties(sc->sc_dev);
   1033 	uint32_t val;
   1034 	u_int uival;
   1035 	bool bval;
   1036 
   1037 	val = RD4(sc, GMAC_DMA_SYSBUS_MODE);
   1038 	if (prop_dictionary_get_bool(prop, "snps,mixed-burst", &bval) && bval) {
   1039 		val |= GMAC_DMA_SYSBUS_MODE_MB;
   1040 	}
   1041 	if (prop_dictionary_get_bool(prop, "snps,fixed-burst", &bval) && bval) {
   1042 		val |= GMAC_DMA_SYSBUS_MODE_FB;
   1043 	}
   1044 	if (prop_dictionary_get_uint(prop, "snps,wr_osr_lmt", &uival)) {
   1045 		val &= ~GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_MASK;
   1046 		val |= uival << GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_SHIFT;
   1047 	}
   1048 	if (prop_dictionary_get_uint(prop, "snps,rd_osr_lmt", &uival)) {
   1049 		val &= ~GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK;
   1050 		val |= uival << GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT;
   1051 	}
   1052 
   1053 	if (!EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
   1054 		val |= GMAC_DMA_SYSBUS_MODE_EAME;
   1055 	}
   1056 
   1057 	/* XXX */
   1058 	val |= GMAC_DMA_SYSBUS_MODE_BLEN16;
   1059 	val |= GMAC_DMA_SYSBUS_MODE_BLEN8;
   1060 	val |= GMAC_DMA_SYSBUS_MODE_BLEN4;
   1061 
   1062 	WR4(sc, GMAC_DMA_SYSBUS_MODE, val);
   1063 }
   1064 
   1065 static int
   1066 eqos_setup_dma(struct eqos_softc *sc, int qid)
   1067 {
   1068 	struct mbuf *m;
   1069 	int error, nsegs, i;
   1070 
   1071 	/* Setup TX ring */
   1072 	error = bus_dmamap_create(sc->sc_dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE,
   1073 	    DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_tx.desc_map);
   1074 	if (error) {
   1075 		return error;
   1076 	}
   1077 	error = bus_dmamem_alloc(sc->sc_dmat, TX_DESC_SIZE, DESC_ALIGN,
   1078 	    DESC_BOUNDARY, &sc->sc_tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1079 	if (error) {
   1080 		return error;
   1081 	}
   1082 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_tx.desc_dmaseg, nsegs,
   1083 	    TX_DESC_SIZE, (void *)&sc->sc_tx.desc_ring, BUS_DMA_WAITOK);
   1084 	if (error) {
   1085 		return error;
   1086 	}
   1087 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_tx.desc_map,
   1088 	    sc->sc_tx.desc_ring, TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1089 	if (error) {
   1090 		return error;
   1091 	}
   1092 	sc->sc_tx.desc_ring_paddr = sc->sc_tx.desc_map->dm_segs[0].ds_addr;
   1093 
   1094 	memset(sc->sc_tx.desc_ring, 0, TX_DESC_SIZE);
   1095 	bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.desc_map, 0, TX_DESC_SIZE,
   1096 	    BUS_DMASYNC_PREWRITE);
   1097 
   1098 	sc->sc_tx.queued = TX_DESC_COUNT;
   1099 	for (i = 0; i < TX_DESC_COUNT; i++) {
   1100 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
   1101 		    TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
   1102 		    &sc->sc_tx.buf_map[i].map);
   1103 		if (error != 0) {
   1104 			device_printf(sc->sc_dev,
   1105 			    "cannot create TX buffer map\n");
   1106 			return error;
   1107 		}
   1108 		eqos_setup_txdesc(sc, i, 0, 0, 0, 0);
   1109 	}
   1110 
   1111 	/* Setup RX ring */
   1112 	error = bus_dmamap_create(sc->sc_dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE,
   1113 	    DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_rx.desc_map);
   1114 	if (error) {
   1115 		return error;
   1116 	}
   1117 	error = bus_dmamem_alloc(sc->sc_dmat, RX_DESC_SIZE, DESC_ALIGN,
   1118 	    DESC_BOUNDARY, &sc->sc_rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1119 	if (error) {
   1120 		return error;
   1121 	}
   1122 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_rx.desc_dmaseg, nsegs,
   1123 	    RX_DESC_SIZE, (void *)&sc->sc_rx.desc_ring, BUS_DMA_WAITOK);
   1124 	if (error) {
   1125 		return error;
   1126 	}
   1127 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_rx.desc_map,
   1128 	    sc->sc_rx.desc_ring, RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1129 	if (error) {
   1130 		return error;
   1131 	}
   1132 	sc->sc_rx.desc_ring_paddr = sc->sc_rx.desc_map->dm_segs[0].ds_addr;
   1133 
   1134 	memset(sc->sc_rx.desc_ring, 0, RX_DESC_SIZE);
   1135 
   1136 	for (i = 0; i < RX_DESC_COUNT; i++) {
   1137 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
   1138 		    RX_DESC_COUNT, MCLBYTES, 0, BUS_DMA_WAITOK,
   1139 		    &sc->sc_rx.buf_map[i].map);
   1140 		if (error != 0) {
   1141 			device_printf(sc->sc_dev,
   1142 			    "cannot create RX buffer map\n");
   1143 			return error;
   1144 		}
   1145 		if ((m = eqos_alloc_mbufcl(sc)) == NULL) {
   1146 			device_printf(sc->sc_dev, "cannot allocate RX mbuf\n");
   1147 			return ENOMEM;
   1148 		}
   1149 		error = eqos_setup_rxbuf(sc, i, m);
   1150 		if (error != 0) {
   1151 			device_printf(sc->sc_dev, "cannot create RX buffer\n");
   1152 			return error;
   1153 		}
   1154 	}
   1155 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.desc_map,
   1156 	    0, sc->sc_rx.desc_map->dm_mapsize,
   1157 	    BUS_DMASYNC_PREWRITE);
   1158 
   1159 	aprint_debug_dev(sc->sc_dev, "TX ring @ 0x%lX, RX ring @ 0x%lX\n",
   1160 	    sc->sc_tx.desc_ring_paddr, sc->sc_rx.desc_ring_paddr);
   1161 
   1162 	return 0;
   1163 }
   1164 
   1165 int
   1166 eqos_attach(struct eqos_softc *sc)
   1167 {
   1168 	struct mii_data *mii = &sc->sc_mii;
   1169 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1170 	uint8_t eaddr[ETHER_ADDR_LEN];
   1171 	u_int userver, snpsver;
   1172 	int mii_flags = 0;
   1173 	int error;
   1174 	int n;
   1175 
   1176 	const uint32_t ver = RD4(sc, GMAC_MAC_VERSION);
   1177 	userver = (ver & GMAC_MAC_VERSION_USERVER_MASK) >>
   1178 	    GMAC_MAC_VERSION_USERVER_SHIFT;
   1179 	snpsver = ver & GMAC_MAC_VERSION_SNPSVER_MASK;
   1180 
   1181 	if (snpsver != 0x51) {
   1182 		aprint_error(": EQOS version 0x%02xx not supported\n",
   1183 		    snpsver);
   1184 		return ENXIO;
   1185 	}
   1186 
   1187 	if (sc->sc_csr_clock < 20000000) {
   1188 		aprint_error(": CSR clock too low\n");
   1189 		return EINVAL;
   1190 	} else if (sc->sc_csr_clock < 35000000) {
   1191 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_20_35;
   1192 	} else if (sc->sc_csr_clock < 60000000) {
   1193 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_35_60;
   1194 	} else if (sc->sc_csr_clock < 100000000) {
   1195 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_60_100;
   1196 	} else if (sc->sc_csr_clock < 150000000) {
   1197 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_100_150;
   1198 	} else if (sc->sc_csr_clock < 250000000) {
   1199 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_150_250;
   1200 	} else if (sc->sc_csr_clock < 300000000) {
   1201 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_300_500;
   1202 	} else if (sc->sc_csr_clock < 800000000) {
   1203 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_500_800;
   1204 	} else {
   1205 		aprint_error(": CSR clock too high\n");
   1206 		return EINVAL;
   1207 	}
   1208 
   1209 	for (n = 0; n < 4; n++) {
   1210 		sc->sc_hw_feature[n] = RD4(sc, GMAC_MAC_HW_FEATURE(n));
   1211 	}
   1212 
   1213 	aprint_naive("\n");
   1214 	aprint_normal(": DesignWare EQOS ver 0x%02x (0x%02x)\n",
   1215 	    snpsver, userver);
   1216 	aprint_verbose_dev(sc->sc_dev, "hw features %08x %08x %08x %08x\n",
   1217 	    sc->sc_hw_feature[0], sc->sc_hw_feature[1],
   1218 	    sc->sc_hw_feature[2], sc->sc_hw_feature[3]);
   1219 
   1220 	if (EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
   1221 		bus_dma_tag_t ntag;
   1222 
   1223 		error = bus_dmatag_subregion(sc->sc_dmat, 0, UINT32_MAX,
   1224 		    &ntag, 0);
   1225 		if (error) {
   1226 			aprint_error_dev(sc->sc_dev,
   1227 			    "failed to restrict DMA: %d\n", error);
   1228 			return error;
   1229 		}
   1230 		aprint_verbose_dev(sc->sc_dev, "using 32-bit DMA\n");
   1231 		sc->sc_dmat = ntag;
   1232 	}
   1233 
   1234 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
   1235 	mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_NET);
   1236 	callout_init(&sc->sc_stat_ch, CALLOUT_FLAGS);
   1237 	callout_setfunc(&sc->sc_stat_ch, eqos_tick, sc);
   1238 
   1239 	eqos_get_eaddr(sc, eaddr);
   1240 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n", ether_sprintf(eaddr));
   1241 
   1242 	/* Soft reset EMAC core */
   1243 	error = eqos_reset(sc);
   1244 	if (error != 0) {
   1245 		return error;
   1246 	}
   1247 
   1248 	/* Configure AXI Bus mode parameters */
   1249 	eqos_axi_configure(sc);
   1250 
   1251 	/* Setup DMA descriptors */
   1252 	if (eqos_setup_dma(sc, 0) != 0) {
   1253 		aprint_error_dev(sc->sc_dev, "failed to setup DMA descriptors\n");
   1254 		return EINVAL;
   1255 	}
   1256 
   1257 	/* Setup ethernet interface */
   1258 	ifp->if_softc = sc;
   1259 	snprintf(ifp->if_xname, IFNAMSIZ, "%s", device_xname(sc->sc_dev));
   1260 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1261 #ifdef EQOS_MPSAFE
   1262 	ifp->if_extflags = IFEF_MPSAFE;
   1263 #endif
   1264 	ifp->if_start = eqos_start;
   1265 	ifp->if_ioctl = eqos_ioctl;
   1266 	ifp->if_init = eqos_init;
   1267 	ifp->if_stop = eqos_stop;
   1268 	ifp->if_capabilities = 0;
   1269 	ifp->if_capenable = ifp->if_capabilities;
   1270 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
   1271 	IFQ_SET_READY(&ifp->if_snd);
   1272 
   1273 	/* 802.1Q VLAN-sized frames are supported */
   1274 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
   1275 
   1276 	/* Attach MII driver */
   1277 	sc->sc_ec.ec_mii = mii;
   1278 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
   1279 	mii->mii_ifp = ifp;
   1280 	mii->mii_readreg = eqos_mii_readreg;
   1281 	mii->mii_writereg = eqos_mii_writereg;
   1282 	mii->mii_statchg = eqos_mii_statchg;
   1283 	mii_attach(sc->sc_dev, mii, 0xffffffff, sc->sc_phy_id, MII_OFFSET_ANY,
   1284 	    mii_flags);
   1285 
   1286 	if (LIST_EMPTY(&mii->mii_phys)) {
   1287 		aprint_error_dev(sc->sc_dev, "no PHY found!\n");
   1288 		return ENOENT;
   1289 	}
   1290 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1291 
   1292 	/* Master interrupt evcnt */
   1293 	evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
   1294 	    NULL, device_xname(sc->sc_dev), "interrupts");
   1295 
   1296 	/* Per-interrupt type, using main interrupt */
   1297 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
   1298 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxintr");
   1299 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
   1300 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "txintr");
   1301 	evcnt_attach_dynamic(&sc->sc_ev_mac, EVCNT_TYPE_INTR,
   1302 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "macstatus");
   1303 	evcnt_attach_dynamic(&sc->sc_ev_mtl, EVCNT_TYPE_INTR,
   1304 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "intrstatus");
   1305 	evcnt_attach_dynamic(&sc->sc_ev_status, EVCNT_TYPE_INTR,
   1306 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxtxstatus");
   1307 
   1308 	/* RX/TX Status specific type, using rxtxstatus interrupt */
   1309 	evcnt_attach_dynamic(&sc->sc_ev_rwt, EVCNT_TYPE_INTR,
   1310 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "rwt");
   1311 	evcnt_attach_dynamic(&sc->sc_ev_excol, EVCNT_TYPE_INTR,
   1312 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "excol");
   1313 	evcnt_attach_dynamic(&sc->sc_ev_lcol, EVCNT_TYPE_INTR,
   1314 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "lcol");
   1315 	evcnt_attach_dynamic(&sc->sc_ev_exdef, EVCNT_TYPE_INTR,
   1316 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "exdef");
   1317 	evcnt_attach_dynamic(&sc->sc_ev_lcarr, EVCNT_TYPE_INTR,
   1318 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "lcarr");
   1319 	evcnt_attach_dynamic(&sc->sc_ev_ncarr, EVCNT_TYPE_INTR,
   1320 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "ncarr");
   1321 	evcnt_attach_dynamic(&sc->sc_ev_tjt, EVCNT_TYPE_INTR,
   1322 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "tjt");
   1323 
   1324 	/* Attach interface */
   1325 	if_attach(ifp);
   1326 	if_deferred_start_init(ifp, NULL);
   1327 
   1328 	/* Attach ethernet interface */
   1329 	ether_ifattach(ifp, eaddr);
   1330 
   1331 	rnd_attach_source(&sc->sc_rndsource, ifp->if_xname, RND_TYPE_NET,
   1332 	    RND_FLAG_DEFAULT);
   1333 
   1334 	return 0;
   1335 }
   1336