Home | History | Annotate | Line # | Download | only in marvell
if_mvgbe.c revision 1.32
      1 /*	$NetBSD: if_mvgbe.c,v 1.32 2012/11/09 09:04:19 msaitoh Exp $	*/
      2 /*
      3  * Copyright (c) 2007, 2008 KIYOHARA Takashi
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: if_mvgbe.c,v 1.32 2012/11/09 09:04:19 msaitoh Exp $");
     29 
     30 #include <sys/param.h>
     31 #include <sys/bus.h>
     32 #include <sys/callout.h>
     33 #include <sys/device.h>
     34 #include <sys/endian.h>
     35 #include <sys/errno.h>
     36 #include <sys/evcnt.h>
     37 #include <sys/kernel.h>
     38 #include <sys/kmem.h>
     39 #include <sys/mutex.h>
     40 #include <sys/sockio.h>
     41 #include <sys/sysctl.h>
     42 
     43 #include <dev/marvell/marvellreg.h>
     44 #include <dev/marvell/marvellvar.h>
     45 #include <dev/marvell/mvgbereg.h>
     46 
     47 #include <net/if.h>
     48 #include <net/if_ether.h>
     49 #include <net/if_media.h>
     50 
     51 #include <netinet/in.h>
     52 #include <netinet/in_systm.h>
     53 #include <netinet/ip.h>
     54 
     55 #include <net/bpf.h>
     56 #include <sys/rnd.h>
     57 
     58 #include <dev/mii/mii.h>
     59 #include <dev/mii/miivar.h>
     60 
     61 #include "locators.h"
     62 
     63 /* #define MVGBE_DEBUG 3 */
     64 #ifdef MVGBE_DEBUG
     65 #define DPRINTF(x)	if (mvgbe_debug) printf x
     66 #define DPRINTFN(n,x)	if (mvgbe_debug >= (n)) printf x
     67 int mvgbe_debug = MVGBE_DEBUG;
     68 #else
     69 #define DPRINTF(x)
     70 #define DPRINTFN(n,x)
     71 #endif
     72 
     73 
     74 #define MVGBE_READ(sc, reg) \
     75 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
     76 #define MVGBE_WRITE(sc, reg, val) \
     77 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
     78 #define MVGBE_READ_FILTER(sc, reg, val, c) \
     79 	bus_space_read_region_4((sc)->sc_iot, (sc)->sc_dafh, (reg), (val), (c))
     80 #define MVGBE_WRITE_FILTER(sc, reg, val, c) \
     81 	bus_space_write_region_4((sc)->sc_iot, (sc)->sc_dafh, (reg), (val), (c))
     82 
     83 #define MVGBE_TX_RING_CNT	256
     84 #define MVGBE_TX_RING_MSK	(MVGBE_TX_RING_CNT - 1)
     85 #define MVGBE_TX_RING_NEXT(x)	(((x) + 1) & MVGBE_TX_RING_MSK)
     86 #define MVGBE_RX_RING_CNT	256
     87 #define MVGBE_RX_RING_MSK	(MVGBE_RX_RING_CNT - 1)
     88 #define MVGBE_RX_RING_NEXT(x)	(((x) + 1) & MVGBE_RX_RING_MSK)
     89 
     90 CTASSERT(MVGBE_TX_RING_CNT > 1 && MVGBE_TX_RING_NEXT(MVGBE_TX_RING_CNT) ==
     91 	(MVGBE_TX_RING_CNT + 1) % MVGBE_TX_RING_CNT);
     92 CTASSERT(MVGBE_RX_RING_CNT > 1 && MVGBE_RX_RING_NEXT(MVGBE_RX_RING_CNT) ==
     93 	(MVGBE_RX_RING_CNT + 1) % MVGBE_RX_RING_CNT);
     94 
     95 #define MVGBE_JSLOTS		384	/* XXXX */
     96 #define MVGBE_JLEN \
     97     ((MVGBE_MRU + MVGBE_HWHEADER_SIZE + MVGBE_RXBUF_ALIGN - 1) & \
     98     ~MVGBE_RXBUF_MASK)
     99 #define MVGBE_NTXSEG		30
    100 #define MVGBE_JPAGESZ		PAGE_SIZE
    101 #define MVGBE_RESID \
    102     (MVGBE_JPAGESZ - (MVGBE_JLEN * MVGBE_JSLOTS) % MVGBE_JPAGESZ)
    103 #define MVGBE_JMEM \
    104     ((MVGBE_JLEN * MVGBE_JSLOTS) + MVGBE_RESID)
    105 
    106 #define MVGBE_TX_RING_ADDR(sc, i)		\
    107     ((sc)->sc_ring_map->dm_segs[0].ds_addr +	\
    108 			offsetof(struct mvgbe_ring_data, mvgbe_tx_ring[(i)]))
    109 
    110 #define MVGBE_RX_RING_ADDR(sc, i)		\
    111     ((sc)->sc_ring_map->dm_segs[0].ds_addr +	\
    112 			offsetof(struct mvgbe_ring_data, mvgbe_rx_ring[(i)]))
    113 
    114 #define MVGBE_CDOFF(x)		offsetof(struct mvgbe_ring_data, x)
    115 #define MVGBE_CDTXOFF(x)	MVGBE_CDOFF(mvgbe_tx_ring[(x)])
    116 #define MVGBE_CDRXOFF(x)	MVGBE_CDOFF(mvgbe_rx_ring[(x)])
    117 
    118 #define MVGBE_CDTXSYNC(sc, x, n, ops)					\
    119 do {									\
    120 	int __x, __n;							\
    121 	const int __descsize = sizeof(struct mvgbe_tx_desc);		\
    122 									\
    123 	__x = (x);							\
    124 	__n = (n);							\
    125 									\
    126 	/* If it will wrap around, sync to the end of the ring. */	\
    127 	if ((__x + __n) > MVGBE_TX_RING_CNT) {				\
    128 		bus_dmamap_sync((sc)->sc_dmat,				\
    129 		    (sc)->sc_ring_map, MVGBE_CDTXOFF(__x),		\
    130 		    __descsize * (MVGBE_TX_RING_CNT - __x), (ops));	\
    131 		__n -= (MVGBE_TX_RING_CNT - __x);			\
    132 		__x = 0;						\
    133 	}								\
    134 									\
    135 	/* Now sync whatever is left. */				\
    136 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_ring_map,		\
    137 	    MVGBE_CDTXOFF((__x)), __descsize * __n, (ops));		\
    138 } while (0 /*CONSTCOND*/)
    139 
    140 #define MVGBE_CDRXSYNC(sc, x, ops)					\
    141 do {									\
    142 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_ring_map,		\
    143 	    MVGBE_CDRXOFF((x)), sizeof(struct mvgbe_rx_desc), (ops));	\
    144 	} while (/*CONSTCOND*/0)
    145 
    146 #define MVGBE_IPGINTTX_DEFAULT	768
    147 #define MVGBE_IPGINTRX_DEFAULT	768
    148 
    149 #ifdef MVGBE_EVENT_COUNTERS
    150 #define	MVGBE_EVCNT_INCR(ev)		(ev)->ev_count++
    151 #define	MVGBE_EVCNT_ADD(ev, val)	(ev)->ev_count += (val)
    152 #else
    153 #define	MVGBE_EVCNT_INCR(ev)		/* nothing */
    154 #define	MVGBE_EVCNT_ADD(ev, val)	/* nothing */
    155 #endif
    156 
    157 struct mvgbe_jpool_entry {
    158 	int slot;
    159 	LIST_ENTRY(mvgbe_jpool_entry) jpool_entries;
    160 };
    161 
    162 struct mvgbe_chain {
    163 	void *mvgbe_desc;
    164 	struct mbuf *mvgbe_mbuf;
    165 	struct mvgbe_chain *mvgbe_next;
    166 };
    167 
    168 struct mvgbe_txmap_entry {
    169 	bus_dmamap_t dmamap;
    170 	SIMPLEQ_ENTRY(mvgbe_txmap_entry) link;
    171 };
    172 
    173 struct mvgbe_chain_data {
    174 	struct mvgbe_chain mvgbe_tx_chain[MVGBE_TX_RING_CNT];
    175 	struct mvgbe_txmap_entry *mvgbe_tx_map[MVGBE_TX_RING_CNT];
    176 	int mvgbe_tx_prod;
    177 	int mvgbe_tx_cons;
    178 	int mvgbe_tx_cnt;
    179 
    180 	struct mvgbe_chain mvgbe_rx_chain[MVGBE_RX_RING_CNT];
    181 	bus_dmamap_t mvgbe_rx_map[MVGBE_RX_RING_CNT];
    182 	bus_dmamap_t mvgbe_rx_jumbo_map;
    183 	int mvgbe_rx_prod;
    184 	int mvgbe_rx_cons;
    185 	int mvgbe_rx_cnt;
    186 
    187 	/* Stick the jumbo mem management stuff here too. */
    188 	void *mvgbe_jslots[MVGBE_JSLOTS];
    189 	void *mvgbe_jumbo_buf;
    190 };
    191 
    192 struct mvgbe_ring_data {
    193 	struct mvgbe_tx_desc mvgbe_tx_ring[MVGBE_TX_RING_CNT];
    194 	struct mvgbe_rx_desc mvgbe_rx_ring[MVGBE_RX_RING_CNT];
    195 };
    196 
    197 struct mvgbec_softc {
    198 	device_t sc_dev;
    199 
    200 	bus_space_tag_t sc_iot;
    201 	bus_space_handle_t sc_ioh;
    202 
    203 	kmutex_t sc_mtx;
    204 
    205 	int sc_flags;
    206 };
    207 
    208 struct mvgbe_softc {
    209 	device_t sc_dev;
    210 	int sc_port;
    211 
    212 	bus_space_tag_t sc_iot;
    213 	bus_space_handle_t sc_ioh;
    214 	bus_space_handle_t sc_dafh;	/* dest address filter handle */
    215 	bus_dma_tag_t sc_dmat;
    216 
    217 	struct ethercom sc_ethercom;
    218 	struct mii_data sc_mii;
    219 	u_int8_t sc_enaddr[ETHER_ADDR_LEN];	/* station addr */
    220 
    221 	callout_t sc_tick_ch;		/* tick callout */
    222 
    223 	struct mvgbe_chain_data sc_cdata;
    224 	struct mvgbe_ring_data *sc_rdata;
    225 	bus_dmamap_t sc_ring_map;
    226 	int sc_if_flags;
    227 	unsigned int sc_ipginttx;
    228 	unsigned int sc_ipgintrx;
    229 	int sc_wdogsoft;
    230 
    231 	LIST_HEAD(__mvgbe_jfreehead, mvgbe_jpool_entry) sc_jfree_listhead;
    232 	LIST_HEAD(__mvgbe_jinusehead, mvgbe_jpool_entry) sc_jinuse_listhead;
    233 	SIMPLEQ_HEAD(__mvgbe_txmaphead, mvgbe_txmap_entry) sc_txmap_head;
    234 
    235 	krndsource_t sc_rnd_source;
    236 	struct sysctllog *mvgbe_clog;
    237 #ifdef MVGBE_EVENT_COUNTERS
    238 	struct evcnt sc_ev_rxoverrun;
    239 	struct evcnt sc_ev_wdogsoft;
    240 #endif
    241 };
    242 
    243 
    244 /* Gigabit Ethernet Unit Global part functions */
    245 
    246 static int mvgbec_match(device_t, struct cfdata *, void *);
    247 static void mvgbec_attach(device_t, device_t, void *);
    248 
    249 static int mvgbec_print(void *, const char *);
    250 static int mvgbec_search(device_t, cfdata_t, const int *, void *);
    251 
    252 /* MII funcstions */
    253 static int mvgbec_miibus_readreg(device_t, int, int);
    254 static void mvgbec_miibus_writereg(device_t, int, int, int);
    255 static void mvgbec_miibus_statchg(struct ifnet *);
    256 
    257 static void mvgbec_wininit(struct mvgbec_softc *);
    258 
    259 /* Gigabit Ethernet Port part functions */
    260 
    261 static int mvgbe_match(device_t, struct cfdata *, void *);
    262 static void mvgbe_attach(device_t, device_t, void *);
    263 
    264 static void mvgbe_tick(void *);
    265 static int mvgbe_intr(void *);
    266 
    267 static void mvgbe_start(struct ifnet *);
    268 static int mvgbe_ioctl(struct ifnet *, u_long, void *);
    269 static int mvgbe_init(struct ifnet *);
    270 static void mvgbe_stop(struct ifnet *, int);
    271 static void mvgbe_watchdog(struct ifnet *);
    272 
    273 static int mvgbe_ifflags_cb(struct ethercom *);
    274 
    275 static int mvgbe_mediachange(struct ifnet *);
    276 static void mvgbe_mediastatus(struct ifnet *, struct ifmediareq *);
    277 
    278 static int mvgbe_init_rx_ring(struct mvgbe_softc *);
    279 static int mvgbe_init_tx_ring(struct mvgbe_softc *);
    280 static int mvgbe_newbuf(struct mvgbe_softc *, int, struct mbuf *, bus_dmamap_t);
    281 static int mvgbe_alloc_jumbo_mem(struct mvgbe_softc *);
    282 static void *mvgbe_jalloc(struct mvgbe_softc *);
    283 static void mvgbe_jfree(struct mbuf *, void *, size_t, void *);
    284 static int mvgbe_encap(struct mvgbe_softc *, struct mbuf *, uint32_t *);
    285 static void mvgbe_rxeof(struct mvgbe_softc *);
    286 static void mvgbe_txeof(struct mvgbe_softc *);
    287 static uint8_t mvgbe_crc8(const uint8_t *, size_t);
    288 static void mvgbe_filter_setup(struct mvgbe_softc *);
    289 #ifdef MVGBE_DEBUG
    290 static void mvgbe_dump_txdesc(struct mvgbe_tx_desc *, int);
    291 #endif
    292 static int mvgbe_ipginttx(struct mvgbec_softc *, struct mvgbe_softc *,
    293     unsigned int);
    294 static int mvgbe_ipgintrx(struct mvgbec_softc *, struct mvgbe_softc *,
    295     unsigned int);
    296 static void sysctl_mvgbe_init(struct mvgbe_softc *);
    297 static int mvgbe_sysctl_ipginttx(SYSCTLFN_PROTO);
    298 static int mvgbe_sysctl_ipgintrx(SYSCTLFN_PROTO);
    299 
    300 CFATTACH_DECL_NEW(mvgbec_gt, sizeof(struct mvgbec_softc),
    301     mvgbec_match, mvgbec_attach, NULL, NULL);
    302 CFATTACH_DECL_NEW(mvgbec_mbus, sizeof(struct mvgbec_softc),
    303     mvgbec_match, mvgbec_attach, NULL, NULL);
    304 
    305 CFATTACH_DECL_NEW(mvgbe, sizeof(struct mvgbe_softc),
    306     mvgbe_match, mvgbe_attach, NULL, NULL);
    307 
    308 device_t mvgbec0 = NULL;
    309 static int mvgbe_root_num;
    310 
    311 struct mvgbe_port {
    312 	int model;
    313 	int unit;
    314 	int ports;
    315 	int irqs[3];
    316 	int flags;
    317 #define FLAGS_FIX_TQTB	(1 << 0)
    318 #define FLAGS_FIX_MTU	(1 << 1)
    319 #define	FLAGS_IPG1	(1 << 2)
    320 #define	FLAGS_IPG2	(1 << 3)
    321 } mvgbe_ports[] = {
    322 	{ MARVELL_DISCOVERY_II,		0, 3, { 32, 33, 34 }, 0 },
    323 	{ MARVELL_DISCOVERY_III,	0, 3, { 32, 33, 34 }, 0 },
    324 #if 0
    325 	{ MARVELL_DISCOVERY_LT,		0, ?, { }, 0 },
    326 	{ MARVELL_DISCOVERY_V,		0, ?, { }, 0 },
    327 	{ MARVELL_DISCOVERY_VI,		0, ?, { }, 0 },
    328 #endif
    329 	{ MARVELL_ORION_1_88F5082,	0, 1, { 21 }, FLAGS_FIX_MTU },
    330 	{ MARVELL_ORION_1_88F5180N,	0, 1, { 21 }, FLAGS_FIX_MTU },
    331 	{ MARVELL_ORION_1_88F5181,	0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
    332 	{ MARVELL_ORION_1_88F5182,	0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
    333 	{ MARVELL_ORION_2_88F5281,	0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
    334 	{ MARVELL_ORION_1_88F6082,	0, 1, { 21 }, FLAGS_FIX_MTU },
    335 	{ MARVELL_ORION_1_88W8660,	0, 1, { 21 }, FLAGS_FIX_MTU },
    336 
    337 	{ MARVELL_KIRKWOOD_88F6180,	0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    338 	{ MARVELL_KIRKWOOD_88F6192,	0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    339 	{ MARVELL_KIRKWOOD_88F6192,	1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    340 	{ MARVELL_KIRKWOOD_88F6281,	0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    341 	{ MARVELL_KIRKWOOD_88F6281,	1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    342 	{ MARVELL_KIRKWOOD_88F6282,	0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    343 	{ MARVELL_KIRKWOOD_88F6282,	1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    344 
    345 	{ MARVELL_MV78XX0_MV78100,	0, 1, { 40 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    346 	{ MARVELL_MV78XX0_MV78100,	1, 1, { 44 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    347 	{ MARVELL_MV78XX0_MV78200,	0, 1, { 40 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    348 	{ MARVELL_MV78XX0_MV78200,	1, 1, { 44 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    349 	{ MARVELL_MV78XX0_MV78200,	2, 1, { 48 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    350 	{ MARVELL_MV78XX0_MV78200,	3, 1, { 52 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    351 };
    352 
    353 
    354 /* ARGSUSED */
    355 static int
    356 mvgbec_match(device_t parent, cfdata_t match, void *aux)
    357 {
    358 	struct marvell_attach_args *mva = aux;
    359 	int i;
    360 
    361 	if (strcmp(mva->mva_name, match->cf_name) != 0)
    362 		return 0;
    363 	if (mva->mva_offset == MVA_OFFSET_DEFAULT)
    364 		return 0;
    365 
    366 	for (i = 0; i < __arraycount(mvgbe_ports); i++)
    367 		if (mva->mva_model == mvgbe_ports[i].model) {
    368 			mva->mva_size = MVGBE_SIZE;
    369 			return 1;
    370 		}
    371 	return 0;
    372 }
    373 
    374 /* ARGSUSED */
    375 static void
    376 mvgbec_attach(device_t parent, device_t self, void *aux)
    377 {
    378 	struct mvgbec_softc *csc = device_private(self);
    379 	struct marvell_attach_args *mva = aux, gbea;
    380 	struct mvgbe_softc *port;
    381 	struct mii_softc *mii;
    382 	device_t child;
    383 	uint32_t phyaddr;
    384 	int i, j;
    385 
    386 	aprint_naive("\n");
    387 	aprint_normal(": Marvell Gigabit Ethernet Controller\n");
    388 
    389 	csc->sc_dev = self;
    390 	csc->sc_iot = mva->mva_iot;
    391 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
    392 	    mva->mva_size, &csc->sc_ioh)) {
    393 		aprint_error_dev(self, "Cannot map registers\n");
    394 		return;
    395 	}
    396 
    397 	if (mvgbec0 == NULL)
    398 		mvgbec0 = self;
    399 
    400 	phyaddr = 0;
    401 	MVGBE_WRITE(csc, MVGBE_PHYADDR, phyaddr);
    402 
    403 	mutex_init(&csc->sc_mtx, MUTEX_DEFAULT, IPL_NET);
    404 
    405 	/* Disable and clear Gigabit Ethernet Unit interrupts */
    406 	MVGBE_WRITE(csc, MVGBE_EUIM, 0);
    407 	MVGBE_WRITE(csc, MVGBE_EUIC, 0);
    408 
    409 	mvgbec_wininit(csc);
    410 
    411 	memset(&gbea, 0, sizeof(gbea));
    412 	for (i = 0; i < __arraycount(mvgbe_ports); i++) {
    413 		if (mvgbe_ports[i].model != mva->mva_model ||
    414 		    mvgbe_ports[i].unit != mva->mva_unit)
    415 			continue;
    416 
    417 		csc->sc_flags = mvgbe_ports[i].flags;
    418 
    419 		for (j = 0; j < mvgbe_ports[i].ports; j++) {
    420 			gbea.mva_name = "mvgbe";
    421 			gbea.mva_model = mva->mva_model;
    422 			gbea.mva_iot = csc->sc_iot;
    423 			gbea.mva_ioh = csc->sc_ioh;
    424 			gbea.mva_unit = j;
    425 			gbea.mva_dmat = mva->mva_dmat;
    426 			gbea.mva_irq = mvgbe_ports[i].irqs[j];
    427 			child = config_found_sm_loc(csc->sc_dev, "mvgbec", NULL,
    428 			    &gbea, mvgbec_print, mvgbec_search);
    429 			if (child) {
    430 				port = device_private(child);
    431 				mii  = LIST_FIRST(&port->sc_mii.mii_phys);
    432 				phyaddr |= MVGBE_PHYADDR_PHYAD(j, mii->mii_phy);
    433 			}
    434 		}
    435 		break;
    436 	}
    437 	MVGBE_WRITE(csc, MVGBE_PHYADDR, phyaddr);
    438 }
    439 
    440 static int
    441 mvgbec_print(void *aux, const char *pnp)
    442 {
    443 	struct marvell_attach_args *gbea = aux;
    444 
    445 	if (pnp)
    446 		aprint_normal("%s at %s port %d",
    447 		    gbea->mva_name, pnp, gbea->mva_unit);
    448 	else {
    449 		if (gbea->mva_unit != MVGBECCF_PORT_DEFAULT)
    450 			aprint_normal(" port %d", gbea->mva_unit);
    451 		if (gbea->mva_irq != MVGBECCF_IRQ_DEFAULT)
    452 			aprint_normal(" irq %d", gbea->mva_irq);
    453 	}
    454 	return UNCONF;
    455 }
    456 
    457 /* ARGSUSED */
    458 static int
    459 mvgbec_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    460 {
    461 	struct marvell_attach_args *gbea = aux;
    462 
    463 	if (cf->cf_loc[MVGBECCF_PORT] == gbea->mva_unit &&
    464 	    cf->cf_loc[MVGBECCF_IRQ] != MVGBECCF_IRQ_DEFAULT)
    465 		gbea->mva_irq = cf->cf_loc[MVGBECCF_IRQ];
    466 
    467 	return config_match(parent, cf, aux);
    468 }
    469 
    470 static int
    471 mvgbec_miibus_readreg(device_t dev, int phy, int reg)
    472 {
    473 	struct mvgbe_softc *sc = device_private(dev);
    474 	struct mvgbec_softc *csc;
    475 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    476 	uint32_t smi, val;
    477 	int i;
    478 
    479 	if (mvgbec0 == NULL) {
    480 		aprint_error_ifnet(ifp, "SMI mvgbec0 not found\n");
    481 		return -1;
    482 	}
    483 	csc = device_private(mvgbec0);
    484 
    485 	mutex_enter(&csc->sc_mtx);
    486 
    487 	for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
    488 		DELAY(1);
    489 		if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
    490 			break;
    491 	}
    492 	if (i == MVGBE_PHY_TIMEOUT) {
    493 		aprint_error_ifnet(ifp, "SMI busy timeout\n");
    494 		mutex_exit(&csc->sc_mtx);
    495 		return -1;
    496 	}
    497 
    498 	smi =
    499 	    MVGBE_SMI_PHYAD(phy) | MVGBE_SMI_REGAD(reg) | MVGBE_SMI_OPCODE_READ;
    500 	MVGBE_WRITE(csc, MVGBE_SMI, smi);
    501 
    502 	for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
    503 		DELAY(1);
    504 		smi = MVGBE_READ(csc, MVGBE_SMI);
    505 		if (smi & MVGBE_SMI_READVALID)
    506 			break;
    507 	}
    508 
    509 	mutex_exit(&csc->sc_mtx);
    510 
    511 	DPRINTFN(9, ("mvgbec_miibus_readreg: i=%d, timeout=%d\n",
    512 	    i, MVGBE_PHY_TIMEOUT));
    513 
    514 	val = smi & MVGBE_SMI_DATA_MASK;
    515 
    516 	DPRINTFN(9, ("mvgbec_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
    517 	    phy, reg, val));
    518 
    519 	return val;
    520 }
    521 
    522 static void
    523 mvgbec_miibus_writereg(device_t dev, int phy, int reg, int val)
    524 {
    525 	struct mvgbe_softc *sc = device_private(dev);
    526 	struct mvgbec_softc *csc;
    527 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    528 	uint32_t smi;
    529 	int i;
    530 
    531 	if (mvgbec0 == NULL) {
    532 		aprint_error_ifnet(ifp, "SMI mvgbec0 not found\n");
    533 		return;
    534 	}
    535 	csc = device_private(mvgbec0);
    536 
    537 	DPRINTFN(9, ("mvgbec_miibus_writereg phy=%d reg=%#x val=%#x\n",
    538 	     phy, reg, val));
    539 
    540 	mutex_enter(&csc->sc_mtx);
    541 
    542 	for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
    543 		DELAY(1);
    544 		if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
    545 			break;
    546 	}
    547 	if (i == MVGBE_PHY_TIMEOUT) {
    548 		aprint_error_ifnet(ifp, "SMI busy timeout\n");
    549 		mutex_exit(&csc->sc_mtx);
    550 		return;
    551 	}
    552 
    553 	smi = MVGBE_SMI_PHYAD(phy) | MVGBE_SMI_REGAD(reg) |
    554 	    MVGBE_SMI_OPCODE_WRITE | (val & MVGBE_SMI_DATA_MASK);
    555 	MVGBE_WRITE(csc, MVGBE_SMI, smi);
    556 
    557 	for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
    558 		DELAY(1);
    559 		if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
    560 			break;
    561 	}
    562 
    563 	mutex_exit(&csc->sc_mtx);
    564 
    565 	if (i == MVGBE_PHY_TIMEOUT)
    566 		aprint_error_ifnet(ifp, "phy write timed out\n");
    567 }
    568 
    569 static void
    570 mvgbec_miibus_statchg(struct ifnet *ifp)
    571 {
    572 
    573 	/* nothing to do */
    574 }
    575 
    576 
    577 static void
    578 mvgbec_wininit(struct mvgbec_softc *sc)
    579 {
    580 	device_t pdev = device_parent(sc->sc_dev);
    581 	uint64_t base;
    582 	uint32_t en, ac, size;
    583 	int window, target, attr, rv, i;
    584 	static int tags[] = {
    585 		MARVELL_TAG_SDRAM_CS0,
    586 		MARVELL_TAG_SDRAM_CS1,
    587 		MARVELL_TAG_SDRAM_CS2,
    588 		MARVELL_TAG_SDRAM_CS3,
    589 
    590 		MARVELL_TAG_UNDEFINED,
    591 	};
    592 
    593 	/* First disable all address decode windows */
    594 	en = MVGBE_BARE_EN_MASK;
    595 	MVGBE_WRITE(sc, MVGBE_BARE, en);
    596 
    597 	ac = 0;
    598 	for (window = 0, i = 0;
    599 	    tags[i] != MARVELL_TAG_UNDEFINED && window < MVGBE_NWINDOW; i++) {
    600 		rv = marvell_winparams_by_tag(pdev, tags[i],
    601 		    &target, &attr, &base, &size);
    602 		if (rv != 0 || size == 0)
    603 			continue;
    604 
    605 		if (base > 0xffffffffULL) {
    606 			if (window >= MVGBE_NREMAP) {
    607 				aprint_error_dev(sc->sc_dev,
    608 				    "can't remap window %d\n", window);
    609 				continue;
    610 			}
    611 			MVGBE_WRITE(sc, MVGBE_HA(window),
    612 			    (base >> 32) & 0xffffffff);
    613 		}
    614 
    615 		MVGBE_WRITE(sc, MVGBE_BASEADDR(window),
    616 		    MVGBE_BASEADDR_TARGET(target)	|
    617 		    MVGBE_BASEADDR_ATTR(attr)		|
    618 		    MVGBE_BASEADDR_BASE(base));
    619 		MVGBE_WRITE(sc, MVGBE_S(window), MVGBE_S_SIZE(size));
    620 
    621 		en &= ~(1 << window);
    622 		/* set full access (r/w) */
    623 		ac |= MVGBE_EPAP_EPAR(window, MVGBE_EPAP_AC_FA);
    624 		window++;
    625 	}
    626 	/* allow to access decode window */
    627 	MVGBE_WRITE(sc, MVGBE_EPAP, ac);
    628 
    629 	MVGBE_WRITE(sc, MVGBE_BARE, en);
    630 }
    631 
    632 
    633 /* ARGSUSED */
    634 static int
    635 mvgbe_match(device_t parent, cfdata_t match, void *aux)
    636 {
    637 	struct marvell_attach_args *mva = aux;
    638 	uint32_t pbase, maddrh, maddrl;
    639 
    640 	pbase = MVGBE_PORTR_BASE + mva->mva_unit * MVGBE_PORTR_SIZE;
    641 	maddrh =
    642 	    bus_space_read_4(mva->mva_iot, mva->mva_ioh, pbase + MVGBE_MACAH);
    643 	maddrl =
    644 	    bus_space_read_4(mva->mva_iot, mva->mva_ioh, pbase + MVGBE_MACAL);
    645 	if ((maddrh | maddrl) == 0)
    646 		return 0;
    647 
    648 	return 1;
    649 }
    650 
    651 /* ARGSUSED */
    652 static void
    653 mvgbe_attach(device_t parent, device_t self, void *aux)
    654 {
    655 	struct mvgbe_softc *sc = device_private(self);
    656 	struct marvell_attach_args *mva = aux;
    657 	struct mvgbe_txmap_entry *entry;
    658 	struct ifnet *ifp;
    659 	bus_dma_segment_t seg;
    660 	bus_dmamap_t dmamap;
    661 	int rseg, i;
    662 	uint32_t maddrh, maddrl;
    663 	void *kva;
    664 
    665 	aprint_naive("\n");
    666 	aprint_normal("\n");
    667 
    668 	sc->sc_dev = self;
    669 	sc->sc_port = mva->mva_unit;
    670 	sc->sc_iot = mva->mva_iot;
    671 	callout_init(&sc->sc_tick_ch, 0);
    672 	callout_setfunc(&sc->sc_tick_ch, mvgbe_tick, sc);
    673 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
    674 	    MVGBE_PORTR_BASE + mva->mva_unit * MVGBE_PORTR_SIZE,
    675 	    MVGBE_PORTR_SIZE, &sc->sc_ioh)) {
    676 		aprint_error_dev(self, "Cannot map registers\n");
    677 		return;
    678 	}
    679 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
    680 	    MVGBE_PORTDAFR_BASE + mva->mva_unit * MVGBE_PORTDAFR_SIZE,
    681 	    MVGBE_PORTDAFR_SIZE, &sc->sc_dafh)) {
    682 		aprint_error_dev(self,
    683 		    "Cannot map destination address filter registers\n");
    684 		return;
    685 	}
    686 	sc->sc_dmat = mva->mva_dmat;
    687 
    688 	maddrh = MVGBE_READ(sc, MVGBE_MACAH);
    689 	maddrl = MVGBE_READ(sc, MVGBE_MACAL);
    690 	sc->sc_enaddr[0] = maddrh >> 24;
    691 	sc->sc_enaddr[1] = maddrh >> 16;
    692 	sc->sc_enaddr[2] = maddrh >> 8;
    693 	sc->sc_enaddr[3] = maddrh >> 0;
    694 	sc->sc_enaddr[4] = maddrl >> 8;
    695 	sc->sc_enaddr[5] = maddrl >> 0;
    696 	aprint_normal_dev(self, "Ethernet address %s\n",
    697 	    ether_sprintf(sc->sc_enaddr));
    698 
    699 	/* clear all ethernet port interrupts */
    700 	MVGBE_WRITE(sc, MVGBE_IC, 0);
    701 	MVGBE_WRITE(sc, MVGBE_ICE, 0);
    702 
    703 	marvell_intr_establish(mva->mva_irq, IPL_NET, mvgbe_intr, sc);
    704 
    705 	/* Allocate the descriptor queues. */
    706 	if (bus_dmamem_alloc(sc->sc_dmat, sizeof(struct mvgbe_ring_data),
    707 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
    708 		aprint_error_dev(self, "can't alloc rx buffers\n");
    709 		return;
    710 	}
    711 	if (bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    712 	    sizeof(struct mvgbe_ring_data), &kva, BUS_DMA_NOWAIT)) {
    713 		aprint_error_dev(self, "can't map dma buffers (%lu bytes)\n",
    714 		    (u_long)sizeof(struct mvgbe_ring_data));
    715 		goto fail1;
    716 	}
    717 	if (bus_dmamap_create(sc->sc_dmat, sizeof(struct mvgbe_ring_data), 1,
    718 	    sizeof(struct mvgbe_ring_data), 0, BUS_DMA_NOWAIT,
    719 	    &sc->sc_ring_map)) {
    720 		aprint_error_dev(self, "can't create dma map\n");
    721 		goto fail2;
    722 	}
    723 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_ring_map, kva,
    724 	    sizeof(struct mvgbe_ring_data), NULL, BUS_DMA_NOWAIT)) {
    725 		aprint_error_dev(self, "can't load dma map\n");
    726 		goto fail3;
    727 	}
    728 	for (i = 0; i < MVGBE_RX_RING_CNT; i++)
    729 		sc->sc_cdata.mvgbe_rx_chain[i].mvgbe_mbuf = NULL;
    730 
    731 	SIMPLEQ_INIT(&sc->sc_txmap_head);
    732 	for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
    733 		sc->sc_cdata.mvgbe_tx_chain[i].mvgbe_mbuf = NULL;
    734 
    735 		if (bus_dmamap_create(sc->sc_dmat,
    736 		    MVGBE_JLEN, MVGBE_NTXSEG, MVGBE_JLEN, 0,
    737 		    BUS_DMA_NOWAIT, &dmamap)) {
    738 			aprint_error_dev(self, "Can't create TX dmamap\n");
    739 			goto fail4;
    740 		}
    741 
    742 		entry = kmem_alloc(sizeof(*entry), KM_SLEEP);
    743 		if (!entry) {
    744 			aprint_error_dev(self, "Can't alloc txmap entry\n");
    745 			bus_dmamap_destroy(sc->sc_dmat, dmamap);
    746 			goto fail4;
    747 		}
    748 		entry->dmamap = dmamap;
    749 		SIMPLEQ_INSERT_HEAD(&sc->sc_txmap_head, entry, link);
    750 	}
    751 
    752 	sc->sc_rdata = (struct mvgbe_ring_data *)kva;
    753 	memset(sc->sc_rdata, 0, sizeof(struct mvgbe_ring_data));
    754 
    755 	/*
    756 	 * We can support 802.1Q VLAN-sized frames and jumbo
    757 	 * Ethernet frames.
    758 	 */
    759 	sc->sc_ethercom.ec_capabilities |=
    760 	    ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
    761 
    762 	/* Try to allocate memory for jumbo buffers. */
    763 	if (mvgbe_alloc_jumbo_mem(sc)) {
    764 		aprint_error_dev(self, "jumbo buffer allocation failed\n");
    765 		goto fail4;
    766 	}
    767 
    768 	ifp = &sc->sc_ethercom.ec_if;
    769 	ifp->if_softc = sc;
    770 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    771 	ifp->if_start = mvgbe_start;
    772 	ifp->if_ioctl = mvgbe_ioctl;
    773 	ifp->if_init = mvgbe_init;
    774 	ifp->if_stop = mvgbe_stop;
    775 	ifp->if_watchdog = mvgbe_watchdog;
    776 	/*
    777 	 * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
    778 	 */
    779 	sc->sc_ethercom.ec_if.if_capabilities |=
    780 	    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    781 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    782 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
    783 	/*
    784 	 * But, IPv6 packets in the stream can cause incorrect TCPv4 Tx sums.
    785 	 */
    786 	sc->sc_ethercom.ec_if.if_capabilities &= ~IFCAP_CSUM_TCPv4_Tx;
    787 	IFQ_SET_MAXLEN(&ifp->if_snd, max(MVGBE_TX_RING_CNT - 1, IFQ_MAXLEN));
    788 	IFQ_SET_READY(&ifp->if_snd);
    789 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    790 
    791 	mvgbe_stop(ifp, 0);
    792 
    793 	/*
    794 	 * Do MII setup.
    795 	 */
    796 	sc->sc_mii.mii_ifp = ifp;
    797 	sc->sc_mii.mii_readreg = mvgbec_miibus_readreg;
    798 	sc->sc_mii.mii_writereg = mvgbec_miibus_writereg;
    799 	sc->sc_mii.mii_statchg = mvgbec_miibus_statchg;
    800 
    801 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    802 	ifmedia_init(&sc->sc_mii.mii_media, 0,
    803 	    mvgbe_mediachange, mvgbe_mediastatus);
    804 	mii_attach(self, &sc->sc_mii, 0xffffffff,
    805 	    MII_PHY_ANY, parent == mvgbec0 ? 0 : 1, 0);
    806 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    807 		aprint_error_dev(self, "no PHY found!\n");
    808 		ifmedia_add(&sc->sc_mii.mii_media,
    809 		    IFM_ETHER|IFM_MANUAL, 0, NULL);
    810 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
    811 	} else
    812 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    813 
    814 	/*
    815 	 * Call MI attach routines.
    816 	 */
    817 	if_attach(ifp);
    818 
    819 	ether_ifattach(ifp, sc->sc_enaddr);
    820 	ether_set_ifflags_cb(&sc->sc_ethercom, mvgbe_ifflags_cb);
    821 
    822 	sysctl_mvgbe_init(sc);
    823 #ifdef MVGBE_EVENT_COUNTERS
    824 	/* Attach event counters. */
    825 	evcnt_attach_dynamic(&sc->sc_ev_rxoverrun, EVCNT_TYPE_MISC,
    826 	    NULL, device_xname(sc->sc_dev), "rxoverrrun");
    827 	evcnt_attach_dynamic(&sc->sc_ev_wdogsoft, EVCNT_TYPE_MISC,
    828 	    NULL, device_xname(sc->sc_dev), "wdogsoft");
    829 #endif
    830 	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
    831 	    RND_TYPE_NET, 0);
    832 
    833 	return;
    834 
    835 fail4:
    836 	while ((entry = SIMPLEQ_FIRST(&sc->sc_txmap_head)) != NULL) {
    837 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txmap_head, link);
    838 		bus_dmamap_destroy(sc->sc_dmat, entry->dmamap);
    839 	}
    840 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ring_map);
    841 fail3:
    842 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ring_map);
    843 fail2:
    844 	bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(struct mvgbe_ring_data));
    845 fail1:
    846 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    847 	return;
    848 }
    849 
    850 static int
    851 mvgbe_ipginttx(struct mvgbec_softc *csc, struct mvgbe_softc *sc,
    852     unsigned int ipginttx)
    853 {
    854 	uint32_t reg;
    855 	reg = MVGBE_READ(sc, MVGBE_PTFUT);
    856 
    857 	if (csc->sc_flags & FLAGS_IPG2) {
    858 		if (ipginttx > MVGBE_PTFUT_IPGINTTX_V2_MAX)
    859 			return -1;
    860 		reg &= ~MVGBE_PTFUT_IPGINTTX_V2_MASK;
    861 		reg |= MVGBE_PTFUT_IPGINTTX_V2(ipginttx);
    862 	} else if (csc->sc_flags & FLAGS_IPG1) {
    863 		if (ipginttx > MVGBE_PTFUT_IPGINTTX_V1_MAX)
    864 			return -1;
    865 		reg &= ~MVGBE_PTFUT_IPGINTTX_V1_MASK;
    866 		reg |= MVGBE_PTFUT_IPGINTTX_V1(ipginttx);
    867 	}
    868 	MVGBE_WRITE(sc, MVGBE_PTFUT, reg);
    869 
    870 	return 0;
    871 }
    872 
    873 static int
    874 mvgbe_ipgintrx(struct mvgbec_softc *csc, struct mvgbe_softc *sc,
    875     unsigned int ipgintrx)
    876 {
    877 	uint32_t reg;
    878 	reg = MVGBE_READ(sc, MVGBE_SDC);
    879 
    880 	if (csc->sc_flags & FLAGS_IPG2) {
    881 		if (ipgintrx > MVGBE_SDC_IPGINTRX_V2_MAX)
    882 			return -1;
    883 		reg &= ~MVGBE_SDC_IPGINTRX_V2_MASK;
    884 		reg |= MVGBE_SDC_IPGINTRX_V2(ipgintrx);
    885 	} else if (csc->sc_flags & FLAGS_IPG1) {
    886 		if (ipgintrx > MVGBE_SDC_IPGINTRX_V1_MAX)
    887 			return -1;
    888 		reg &= ~MVGBE_SDC_IPGINTRX_V1_MASK;
    889 		reg |= MVGBE_SDC_IPGINTRX_V1(ipgintrx);
    890 	}
    891 	MVGBE_WRITE(sc, MVGBE_SDC, reg);
    892 
    893 	return 0;
    894 }
    895 
    896 static void
    897 mvgbe_tick(void *arg)
    898 {
    899 	struct mvgbe_softc *sc = arg;
    900 	struct mii_data *mii = &sc->sc_mii;
    901 	int s;
    902 
    903 	s = splnet();
    904 	mii_tick(mii);
    905 	/* Need more work */
    906 	MVGBE_EVCNT_ADD(&sc->sc_ev_rxoverrun, MVGBE_READ(sc, MVGBE_POFC));
    907 	splx(s);
    908 
    909 	callout_schedule(&sc->sc_tick_ch, hz);
    910 }
    911 
    912 static int
    913 mvgbe_intr(void *arg)
    914 {
    915 	struct mvgbe_softc *sc = arg;
    916 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    917 	uint32_t ic, ice, datum = 0;
    918 	int claimed = 0;
    919 
    920 	for (;;) {
    921 		ice = MVGBE_READ(sc, MVGBE_ICE);
    922 		ic = MVGBE_READ(sc, MVGBE_IC);
    923 
    924 		DPRINTFN(3, ("mvgbe_intr: ic=%#x, ice=%#x\n", ic, ice));
    925 		if (ic == 0 && ice == 0)
    926 			break;
    927 
    928 		datum = datum ^ ic ^ ice;
    929 
    930 		MVGBE_WRITE(sc, MVGBE_IC, ~ic);
    931 		MVGBE_WRITE(sc, MVGBE_ICE, ~ice);
    932 
    933 		claimed = 1;
    934 
    935 		if (!(ifp->if_flags & IFF_RUNNING))
    936 			break;
    937 
    938 		if (ice & MVGBE_ICE_LINKCHG) {
    939 			if (MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP) {
    940 				/* Enable port RX and TX. */
    941 				MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_ENQ(0));
    942 				MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
    943 			} else {
    944 				MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_DISQ(0));
    945 				MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_DISQ);
    946 			}
    947 
    948 			/* Notify link change event to mii layer */
    949 			mii_pollstat(&sc->sc_mii);
    950 		}
    951 
    952 		if (ic & (MVGBE_IC_RXBUF | MVGBE_IC_RXERROR))
    953 			mvgbe_rxeof(sc);
    954 
    955 		if (ice & (MVGBE_ICE_TXBUF | MVGBE_ICE_TXERR))
    956 			mvgbe_txeof(sc);
    957 	}
    958 
    959 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
    960 		mvgbe_start(ifp);
    961 
    962 	rnd_add_uint32(&sc->sc_rnd_source, datum);
    963 
    964 	return claimed;
    965 }
    966 
    967 static void
    968 mvgbe_start(struct ifnet *ifp)
    969 {
    970 	struct mvgbe_softc *sc = ifp->if_softc;
    971 	struct mbuf *m_head = NULL;
    972 	uint32_t idx = sc->sc_cdata.mvgbe_tx_prod;
    973 	int pkts = 0;
    974 
    975 	DPRINTFN(3, ("mvgbe_start (idx %d, tx_chain[idx] %p)\n", idx,
    976 	    sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf));
    977 
    978 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    979 		return;
    980 	/* If Link is DOWN, can't start TX */
    981 	if (!(MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP))
    982 		return;
    983 
    984 	while (sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf == NULL) {
    985 		IFQ_POLL(&ifp->if_snd, m_head);
    986 		if (m_head == NULL)
    987 			break;
    988 
    989 		/*
    990 		 * Pack the data into the transmit ring. If we
    991 		 * don't have room, set the OACTIVE flag and wait
    992 		 * for the NIC to drain the ring.
    993 		 */
    994 		if (mvgbe_encap(sc, m_head, &idx)) {
    995 			ifp->if_flags |= IFF_OACTIVE;
    996 			break;
    997 		}
    998 
    999 		/* now we are committed to transmit the packet */
   1000 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1001 		pkts++;
   1002 
   1003 		/*
   1004 		 * If there's a BPF listener, bounce a copy of this frame
   1005 		 * to him.
   1006 		 */
   1007 		bpf_mtap(ifp, m_head);
   1008 	}
   1009 	if (pkts == 0)
   1010 		return;
   1011 
   1012 	/* Transmit at Queue 0 */
   1013 	if (idx != sc->sc_cdata.mvgbe_tx_prod) {
   1014 		sc->sc_cdata.mvgbe_tx_prod = idx;
   1015 		MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
   1016 
   1017 		/*
   1018 		 * Set a timeout in case the chip goes out to lunch.
   1019 		 */
   1020 		ifp->if_timer = 1;
   1021 		sc->sc_wdogsoft = 1;
   1022 	}
   1023 }
   1024 
   1025 static int
   1026 mvgbe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1027 {
   1028 	struct mvgbe_softc *sc = ifp->if_softc;
   1029 	struct ifreq *ifr = data;
   1030 	int s, error = 0;
   1031 
   1032 	s = splnet();
   1033 
   1034 	switch (cmd) {
   1035 	case SIOCGIFMEDIA:
   1036 	case SIOCSIFMEDIA:
   1037 		DPRINTFN(2, ("mvgbe_ioctl MEDIA\n"));
   1038 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1039 		break;
   1040 	default:
   1041 		DPRINTFN(2, ("mvgbe_ioctl ETHER\n"));
   1042 		error = ether_ioctl(ifp, cmd, data);
   1043 		if (error == ENETRESET) {
   1044 			if (ifp->if_flags & IFF_RUNNING) {
   1045 				mvgbe_filter_setup(sc);
   1046 			}
   1047 			error = 0;
   1048 		}
   1049 		break;
   1050 	}
   1051 
   1052 	splx(s);
   1053 
   1054 	return error;
   1055 }
   1056 
   1057 static int
   1058 mvgbe_init(struct ifnet *ifp)
   1059 {
   1060 	struct mvgbe_softc *sc = ifp->if_softc;
   1061 	struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
   1062 	struct mii_data *mii = &sc->sc_mii;
   1063 	uint32_t reg;
   1064 	int i;
   1065 
   1066 	DPRINTFN(2, ("mvgbe_init\n"));
   1067 
   1068 	/* Cancel pending I/O and free all RX/TX buffers. */
   1069 	mvgbe_stop(ifp, 0);
   1070 
   1071 	/* clear all ethernet port interrupts */
   1072 	MVGBE_WRITE(sc, MVGBE_IC, 0);
   1073 	MVGBE_WRITE(sc, MVGBE_ICE, 0);
   1074 
   1075 	/* Init TX/RX descriptors */
   1076 	if (mvgbe_init_tx_ring(sc) == ENOBUFS) {
   1077 		aprint_error_ifnet(ifp,
   1078 		    "initialization failed: no memory for tx buffers\n");
   1079 		return ENOBUFS;
   1080 	}
   1081 	if (mvgbe_init_rx_ring(sc) == ENOBUFS) {
   1082 		aprint_error_ifnet(ifp,
   1083 		    "initialization failed: no memory for rx buffers\n");
   1084 		return ENOBUFS;
   1085 	}
   1086 
   1087 	if ((csc->sc_flags & FLAGS_IPG1) || (csc->sc_flags & FLAGS_IPG2)) {
   1088 		sc->sc_ipginttx = MVGBE_IPGINTTX_DEFAULT;
   1089 		sc->sc_ipgintrx = MVGBE_IPGINTRX_DEFAULT;
   1090 	}
   1091 	if (csc->sc_flags & FLAGS_FIX_MTU)
   1092 		MVGBE_WRITE(sc, MVGBE_MTU, 0);	/* hw reset value is wrong */
   1093 	MVGBE_WRITE(sc, MVGBE_PSC,
   1094 	    MVGBE_PSC_ANFC |			/* Enable Auto-Neg Flow Ctrl */
   1095 	    MVGBE_PSC_RESERVED |		/* Must be set to 1 */
   1096 	    MVGBE_PSC_FLFAIL |			/* Do NOT Force Link Fail */
   1097 	    MVGBE_PSC_MRU(MVGBE_PSC_MRU_9022) | /* we want 9k */
   1098 	    MVGBE_PSC_SETFULLDX);		/* Set_FullDx */
   1099 	/* XXXX: mvgbe(4) always use RGMII. */
   1100 	MVGBE_WRITE(sc, MVGBE_PSC1,
   1101 	    MVGBE_READ(sc, MVGBE_PSC1) | MVGBE_PSC1_RGMIIEN);
   1102 	/* XXXX: Also always Weighted Round-Robin Priority Mode */
   1103 	MVGBE_WRITE(sc, MVGBE_TQFPC, MVGBE_TQFPC_EN(0));
   1104 
   1105 	MVGBE_WRITE(sc, MVGBE_CRDP(0), MVGBE_RX_RING_ADDR(sc, 0));
   1106 	MVGBE_WRITE(sc, MVGBE_TCQDP, MVGBE_TX_RING_ADDR(sc, 0));
   1107 
   1108 	if (csc->sc_flags & FLAGS_FIX_TQTB) {
   1109 		/*
   1110 		 * Queue 0 (offset 0x72700) must be programmed to 0x3fffffff.
   1111 		 * And offset 0x72704 must be programmed to 0x03ffffff.
   1112 		 * Queue 1 through 7 must be programmed to 0x0.
   1113 		 */
   1114 		MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(0), 0x3fffffff);
   1115 		MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(0), 0x03ffffff);
   1116 		for (i = 1; i < 8; i++) {
   1117 			MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x0);
   1118 			MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0x0);
   1119 		}
   1120 	} else
   1121 		for (i = 1; i < 8; i++) {
   1122 			MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x3fffffff);
   1123 			MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0xffff7fff);
   1124 			MVGBE_WRITE(sc, MVGBE_TQAC(i), 0xfc0000ff);
   1125 		}
   1126 
   1127 	MVGBE_WRITE(sc, MVGBE_PXC, MVGBE_PXC_RXCS);
   1128 	MVGBE_WRITE(sc, MVGBE_PXCX, 0);
   1129 
   1130 	/* Set SDC register except IPGINT bits */
   1131 	MVGBE_WRITE(sc, MVGBE_SDC,
   1132 	    MVGBE_SDC_RXBSZ_16_64BITWORDS |
   1133 #if BYTE_ORDER == LITTLE_ENDIAN
   1134 	    MVGBE_SDC_BLMR |	/* Big/Little Endian Receive Mode: No swap */
   1135 	    MVGBE_SDC_BLMT |	/* Big/Little Endian Transmit Mode: No swap */
   1136 #endif
   1137 	    MVGBE_SDC_TXBSZ_16_64BITWORDS);
   1138 	/* And then set IPGINT bits */
   1139 	mvgbe_ipgintrx(csc, sc, sc->sc_ipgintrx);
   1140 
   1141 	/* Tx side */
   1142 	MVGBE_WRITE(sc, MVGBE_PTFUT, 0);
   1143 	mvgbe_ipginttx(csc, sc, sc->sc_ipginttx);
   1144 
   1145 	mvgbe_filter_setup(sc);
   1146 
   1147 	mii_mediachg(mii);
   1148 
   1149 	/* Enable port */
   1150 	reg = MVGBE_READ(sc, MVGBE_PSC);
   1151 	MVGBE_WRITE(sc, MVGBE_PSC, reg | MVGBE_PSC_PORTEN);
   1152 
   1153 	/* If Link is UP, Start RX and TX traffic */
   1154 	if (MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP) {
   1155 		/* Enable port RX/TX. */
   1156 		MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_ENQ(0));
   1157 		MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
   1158 	}
   1159 
   1160 	/* Enable interrupt masks */
   1161 	MVGBE_WRITE(sc, MVGBE_PIM,
   1162 	    MVGBE_IC_RXBUF |
   1163 	    MVGBE_IC_EXTEND |
   1164 	    MVGBE_IC_RXBUFQ_MASK |
   1165 	    MVGBE_IC_RXERROR |
   1166 	    MVGBE_IC_RXERRQ_MASK);
   1167 	MVGBE_WRITE(sc, MVGBE_PEIM,
   1168 	    MVGBE_ICE_TXBUF |
   1169 	    MVGBE_ICE_TXERR |
   1170 	    MVGBE_ICE_LINKCHG);
   1171 
   1172 	callout_schedule(&sc->sc_tick_ch, hz);
   1173 
   1174 	ifp->if_flags |= IFF_RUNNING;
   1175 	ifp->if_flags &= ~IFF_OACTIVE;
   1176 
   1177 	return 0;
   1178 }
   1179 
   1180 /* ARGSUSED */
   1181 static void
   1182 mvgbe_stop(struct ifnet *ifp, int disable)
   1183 {
   1184 	struct mvgbe_softc *sc = ifp->if_softc;
   1185 	struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
   1186 	struct mvgbe_chain_data *cdata = &sc->sc_cdata;
   1187 	uint32_t reg;
   1188 	int i, cnt;
   1189 
   1190 	DPRINTFN(2, ("mvgbe_stop\n"));
   1191 
   1192 	callout_stop(&sc->sc_tick_ch);
   1193 
   1194 	/* Stop Rx port activity. Check port Rx activity. */
   1195 	reg = MVGBE_READ(sc, MVGBE_RQC);
   1196 	if (reg & MVGBE_RQC_ENQ_MASK)
   1197 		/* Issue stop command for active channels only */
   1198 		MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_DISQ_DISABLE(reg));
   1199 
   1200 	/* Stop Tx port activity. Check port Tx activity. */
   1201 	if (MVGBE_READ(sc, MVGBE_TQC) & MVGBE_TQC_ENQ)
   1202 		MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_DISQ);
   1203 
   1204 	/* Force link down */
   1205 	reg = MVGBE_READ(sc, MVGBE_PSC);
   1206 	MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_FLFAIL);
   1207 
   1208 #define RX_DISABLE_TIMEOUT          0x1000000
   1209 #define TX_FIFO_EMPTY_TIMEOUT       0x1000000
   1210 	/* Wait for all Rx activity to terminate. */
   1211 	cnt = 0;
   1212 	do {
   1213 		if (cnt >= RX_DISABLE_TIMEOUT) {
   1214 			aprint_error_ifnet(ifp,
   1215 			    "timeout for RX stopped. rqc 0x%x\n", reg);
   1216 			break;
   1217 		}
   1218 		cnt++;
   1219 
   1220 		/*
   1221 		 * Check Receive Queue Command register that all Rx queues
   1222 		 * are stopped
   1223 		 */
   1224 		reg = MVGBE_READ(sc, MVGBE_RQC);
   1225 	} while (reg & 0xff);
   1226 
   1227 	/* Double check to verify that TX FIFO is empty */
   1228 	cnt = 0;
   1229 	while (1) {
   1230 		do {
   1231 			if (cnt >= TX_FIFO_EMPTY_TIMEOUT) {
   1232 				aprint_error_ifnet(ifp,
   1233 				    "timeout for TX FIFO empty. status 0x%x\n",
   1234 				    reg);
   1235 				break;
   1236 			}
   1237 			cnt++;
   1238 
   1239 			reg = MVGBE_READ(sc, MVGBE_PS);
   1240 		} while
   1241 		    (!(reg & MVGBE_PS_TXFIFOEMP) || reg & MVGBE_PS_TXINPROG);
   1242 
   1243 		if (cnt >= TX_FIFO_EMPTY_TIMEOUT)
   1244 			break;
   1245 
   1246 		/* Double check */
   1247 		reg = MVGBE_READ(sc, MVGBE_PS);
   1248 		if (reg & MVGBE_PS_TXFIFOEMP && !(reg & MVGBE_PS_TXINPROG))
   1249 			break;
   1250 		else
   1251 			aprint_error_ifnet(ifp,
   1252 			    "TX FIFO empty double check failed."
   1253 			    " %d loops, status 0x%x\n", cnt, reg);
   1254 	}
   1255 
   1256 	/* Reset the Enable bit in the Port Serial Control Register */
   1257 	reg = MVGBE_READ(sc, MVGBE_PSC);
   1258 	MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_PORTEN);
   1259 
   1260 	/*
   1261 	 * Disable and clear interrupts
   1262 	 * 0) controller interrupt
   1263 	 * 1) port interrupt cause
   1264 	 * 2) port interrupt mask
   1265 	 */
   1266 	MVGBE_WRITE(csc, MVGBE_EUIM, 0);
   1267 	MVGBE_WRITE(csc, MVGBE_EUIC, 0);
   1268 	MVGBE_WRITE(sc, MVGBE_IC, 0);
   1269 	MVGBE_WRITE(sc, MVGBE_ICE, 0);
   1270 	MVGBE_WRITE(sc, MVGBE_PIM, 0);
   1271 	MVGBE_WRITE(sc, MVGBE_PEIM, 0);
   1272 
   1273 	/* Free RX and TX mbufs still in the queues. */
   1274 	for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
   1275 		if (cdata->mvgbe_rx_chain[i].mvgbe_mbuf != NULL) {
   1276 			m_freem(cdata->mvgbe_rx_chain[i].mvgbe_mbuf);
   1277 			cdata->mvgbe_rx_chain[i].mvgbe_mbuf = NULL;
   1278 		}
   1279 	}
   1280 	for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
   1281 		if (cdata->mvgbe_tx_chain[i].mvgbe_mbuf != NULL) {
   1282 			m_freem(cdata->mvgbe_tx_chain[i].mvgbe_mbuf);
   1283 			cdata->mvgbe_tx_chain[i].mvgbe_mbuf = NULL;
   1284 		}
   1285 	}
   1286 
   1287 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1288 }
   1289 
   1290 static void
   1291 mvgbe_watchdog(struct ifnet *ifp)
   1292 {
   1293 	struct mvgbe_softc *sc = ifp->if_softc;
   1294 
   1295 	/*
   1296 	 * Reclaim first as there is a possibility of losing Tx completion
   1297 	 * interrupts.
   1298 	 */
   1299 	mvgbe_txeof(sc);
   1300 	if (sc->sc_cdata.mvgbe_tx_cnt != 0) {
   1301 		if (sc->sc_wdogsoft) {
   1302 			/*
   1303 			 * There is race condition between CPU and DMA
   1304 			 * engine. When DMA engine encounters queue end,
   1305 			 * it clears MVGBE_TQC_ENQ bit.
   1306 			 */
   1307 			MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
   1308 			ifp->if_timer = 5;
   1309 			sc->sc_wdogsoft = 0;
   1310 			MVGBE_EVCNT_INCR(&sc->sc_ev_wdogsoft);
   1311 		} else {
   1312 			aprint_error_ifnet(ifp, "watchdog timeout\n");
   1313 
   1314 			ifp->if_oerrors++;
   1315 
   1316 			mvgbe_init(ifp);
   1317 		}
   1318 	}
   1319 }
   1320 
   1321 static int
   1322 mvgbe_ifflags_cb(struct ethercom *ec)
   1323 {
   1324 	struct ifnet *ifp = &ec->ec_if;
   1325 	struct mvgbe_softc *sc = ifp->if_softc;
   1326 	int change = ifp->if_flags ^ sc->sc_if_flags;
   1327 
   1328 	if (change != 0)
   1329 		sc->sc_if_flags = ifp->if_flags;
   1330 
   1331 	if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
   1332 		return ENETRESET;
   1333 
   1334 	if ((change & IFF_PROMISC) != 0)
   1335 		mvgbe_filter_setup(sc);
   1336 
   1337 	return 0;
   1338 }
   1339 
   1340 /*
   1341  * Set media options.
   1342  */
   1343 static int
   1344 mvgbe_mediachange(struct ifnet *ifp)
   1345 {
   1346 	return ether_mediachange(ifp);
   1347 }
   1348 
   1349 /*
   1350  * Report current media status.
   1351  */
   1352 static void
   1353 mvgbe_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1354 {
   1355 	ether_mediastatus(ifp, ifmr);
   1356 }
   1357 
   1358 
   1359 static int
   1360 mvgbe_init_rx_ring(struct mvgbe_softc *sc)
   1361 {
   1362 	struct mvgbe_chain_data *cd = &sc->sc_cdata;
   1363 	struct mvgbe_ring_data *rd = sc->sc_rdata;
   1364 	int i;
   1365 
   1366 	memset(rd->mvgbe_rx_ring, 0,
   1367 	    sizeof(struct mvgbe_rx_desc) * MVGBE_RX_RING_CNT);
   1368 
   1369 	for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
   1370 		cd->mvgbe_rx_chain[i].mvgbe_desc =
   1371 		    &rd->mvgbe_rx_ring[i];
   1372 		if (i == MVGBE_RX_RING_CNT - 1) {
   1373 			cd->mvgbe_rx_chain[i].mvgbe_next =
   1374 			    &cd->mvgbe_rx_chain[0];
   1375 			rd->mvgbe_rx_ring[i].nextdescptr =
   1376 			    MVGBE_RX_RING_ADDR(sc, 0);
   1377 		} else {
   1378 			cd->mvgbe_rx_chain[i].mvgbe_next =
   1379 			    &cd->mvgbe_rx_chain[i + 1];
   1380 			rd->mvgbe_rx_ring[i].nextdescptr =
   1381 			    MVGBE_RX_RING_ADDR(sc, i + 1);
   1382 		}
   1383 	}
   1384 
   1385 	for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
   1386 		if (mvgbe_newbuf(sc, i, NULL,
   1387 		    sc->sc_cdata.mvgbe_rx_jumbo_map) == ENOBUFS) {
   1388 			aprint_error_ifnet(&sc->sc_ethercom.ec_if,
   1389 			    "failed alloc of %dth mbuf\n", i);
   1390 			return ENOBUFS;
   1391 		}
   1392 	}
   1393 	sc->sc_cdata.mvgbe_rx_prod = 0;
   1394 	sc->sc_cdata.mvgbe_rx_cons = 0;
   1395 
   1396 	return 0;
   1397 }
   1398 
   1399 static int
   1400 mvgbe_init_tx_ring(struct mvgbe_softc *sc)
   1401 {
   1402 	struct mvgbe_chain_data *cd = &sc->sc_cdata;
   1403 	struct mvgbe_ring_data *rd = sc->sc_rdata;
   1404 	int i;
   1405 
   1406 	memset(sc->sc_rdata->mvgbe_tx_ring, 0,
   1407 	    sizeof(struct mvgbe_tx_desc) * MVGBE_TX_RING_CNT);
   1408 
   1409 	for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
   1410 		cd->mvgbe_tx_chain[i].mvgbe_desc =
   1411 		    &rd->mvgbe_tx_ring[i];
   1412 		if (i == MVGBE_TX_RING_CNT - 1) {
   1413 			cd->mvgbe_tx_chain[i].mvgbe_next =
   1414 			    &cd->mvgbe_tx_chain[0];
   1415 			rd->mvgbe_tx_ring[i].nextdescptr =
   1416 			    MVGBE_TX_RING_ADDR(sc, 0);
   1417 		} else {
   1418 			cd->mvgbe_tx_chain[i].mvgbe_next =
   1419 			    &cd->mvgbe_tx_chain[i + 1];
   1420 			rd->mvgbe_tx_ring[i].nextdescptr =
   1421 			    MVGBE_TX_RING_ADDR(sc, i + 1);
   1422 		}
   1423 		rd->mvgbe_tx_ring[i].cmdsts = MVGBE_BUFFER_OWNED_BY_HOST;
   1424 	}
   1425 
   1426 	sc->sc_cdata.mvgbe_tx_prod = 0;
   1427 	sc->sc_cdata.mvgbe_tx_cons = 0;
   1428 	sc->sc_cdata.mvgbe_tx_cnt = 0;
   1429 
   1430 	MVGBE_CDTXSYNC(sc, 0, MVGBE_TX_RING_CNT,
   1431 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1432 
   1433 	return 0;
   1434 }
   1435 
   1436 static int
   1437 mvgbe_newbuf(struct mvgbe_softc *sc, int i, struct mbuf *m,
   1438 		bus_dmamap_t dmamap)
   1439 {
   1440 	struct mbuf *m_new = NULL;
   1441 	struct mvgbe_chain *c;
   1442 	struct mvgbe_rx_desc *r;
   1443 	int align;
   1444 	vaddr_t offset;
   1445 
   1446 	if (m == NULL) {
   1447 		void *buf = NULL;
   1448 
   1449 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1450 		if (m_new == NULL) {
   1451 			aprint_error_ifnet(&sc->sc_ethercom.ec_if,
   1452 			    "no memory for rx list -- packet dropped!\n");
   1453 			return ENOBUFS;
   1454 		}
   1455 
   1456 		/* Allocate the jumbo buffer */
   1457 		buf = mvgbe_jalloc(sc);
   1458 		if (buf == NULL) {
   1459 			m_freem(m_new);
   1460 			DPRINTFN(1, ("%s jumbo allocation failed -- packet "
   1461 			    "dropped!\n", sc->sc_ethercom.ec_if.if_xname));
   1462 			return ENOBUFS;
   1463 		}
   1464 
   1465 		/* Attach the buffer to the mbuf */
   1466 		m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
   1467 		MEXTADD(m_new, buf, MVGBE_JLEN, 0, mvgbe_jfree, sc);
   1468 	} else {
   1469 		/*
   1470 		 * We're re-using a previously allocated mbuf;
   1471 		 * be sure to re-init pointers and lengths to
   1472 		 * default values.
   1473 		 */
   1474 		m_new = m;
   1475 		m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
   1476 		m_new->m_data = m_new->m_ext.ext_buf;
   1477 	}
   1478 	align = (u_long)m_new->m_data & MVGBE_RXBUF_MASK;
   1479 	if (align != 0) {
   1480 		DPRINTFN(1,("align = %d\n", align));
   1481 		m_adj(m_new,  MVGBE_RXBUF_ALIGN - align);
   1482 	}
   1483 
   1484 	c = &sc->sc_cdata.mvgbe_rx_chain[i];
   1485 	r = c->mvgbe_desc;
   1486 	c->mvgbe_mbuf = m_new;
   1487 	offset = (vaddr_t)m_new->m_data - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf;
   1488 	r->bufptr = dmamap->dm_segs[0].ds_addr + offset;
   1489 	r->bufsize = MVGBE_JLEN & ~MVGBE_RXBUF_MASK;
   1490 	r->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_ENABLE_INTERRUPT;
   1491 
   1492 	/* Invalidate RX buffer */
   1493 	bus_dmamap_sync(sc->sc_dmat, dmamap, offset, r->bufsize,
   1494 	    BUS_DMASYNC_PREREAD);
   1495 
   1496 	MVGBE_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1497 
   1498 	return 0;
   1499 }
   1500 
   1501 /*
   1502  * Memory management for jumbo frames.
   1503  */
   1504 
   1505 static int
   1506 mvgbe_alloc_jumbo_mem(struct mvgbe_softc *sc)
   1507 {
   1508 	char *ptr, *kva;
   1509 	bus_dma_segment_t seg;
   1510 	int i, rseg, state, error;
   1511 	struct mvgbe_jpool_entry *entry;
   1512 
   1513 	state = error = 0;
   1514 
   1515 	/* Grab a big chunk o' storage. */
   1516 	if (bus_dmamem_alloc(sc->sc_dmat, MVGBE_JMEM, PAGE_SIZE, 0,
   1517 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
   1518 		aprint_error_dev(sc->sc_dev, "can't alloc rx buffers\n");
   1519 		return ENOBUFS;
   1520 	}
   1521 
   1522 	state = 1;
   1523 	if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, MVGBE_JMEM,
   1524 	    (void **)&kva, BUS_DMA_NOWAIT)) {
   1525 		aprint_error_dev(sc->sc_dev,
   1526 		    "can't map dma buffers (%d bytes)\n", MVGBE_JMEM);
   1527 		error = ENOBUFS;
   1528 		goto out;
   1529 	}
   1530 
   1531 	state = 2;
   1532 	if (bus_dmamap_create(sc->sc_dmat, MVGBE_JMEM, 1, MVGBE_JMEM, 0,
   1533 	    BUS_DMA_NOWAIT, &sc->sc_cdata.mvgbe_rx_jumbo_map)) {
   1534 		aprint_error_dev(sc->sc_dev, "can't create dma map\n");
   1535 		error = ENOBUFS;
   1536 		goto out;
   1537 	}
   1538 
   1539 	state = 3;
   1540 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_cdata.mvgbe_rx_jumbo_map,
   1541 	    kva, MVGBE_JMEM, NULL, BUS_DMA_NOWAIT)) {
   1542 		aprint_error_dev(sc->sc_dev, "can't load dma map\n");
   1543 		error = ENOBUFS;
   1544 		goto out;
   1545 	}
   1546 
   1547 	state = 4;
   1548 	sc->sc_cdata.mvgbe_jumbo_buf = (void *)kva;
   1549 	DPRINTFN(1,("mvgbe_jumbo_buf = %p\n", sc->sc_cdata.mvgbe_jumbo_buf));
   1550 
   1551 	LIST_INIT(&sc->sc_jfree_listhead);
   1552 	LIST_INIT(&sc->sc_jinuse_listhead);
   1553 
   1554 	/*
   1555 	 * Now divide it up into 9K pieces and save the addresses
   1556 	 * in an array.
   1557 	 */
   1558 	ptr = sc->sc_cdata.mvgbe_jumbo_buf;
   1559 	for (i = 0; i < MVGBE_JSLOTS; i++) {
   1560 		sc->sc_cdata.mvgbe_jslots[i] = ptr;
   1561 		ptr += MVGBE_JLEN;
   1562 		entry = kmem_alloc(sizeof(struct mvgbe_jpool_entry), KM_SLEEP);
   1563 		if (entry == NULL) {
   1564 			aprint_error_dev(sc->sc_dev,
   1565 			    "no memory for jumbo buffer queue!\n");
   1566 			error = ENOBUFS;
   1567 			goto out;
   1568 		}
   1569 		entry->slot = i;
   1570 		if (i)
   1571 			LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry,
   1572 			    jpool_entries);
   1573 		else
   1574 			LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry,
   1575 			    jpool_entries);
   1576 	}
   1577 out:
   1578 	if (error != 0) {
   1579 		switch (state) {
   1580 		case 4:
   1581 			bus_dmamap_unload(sc->sc_dmat,
   1582 			    sc->sc_cdata.mvgbe_rx_jumbo_map);
   1583 		case 3:
   1584 			bus_dmamap_destroy(sc->sc_dmat,
   1585 			    sc->sc_cdata.mvgbe_rx_jumbo_map);
   1586 		case 2:
   1587 			bus_dmamem_unmap(sc->sc_dmat, kva, MVGBE_JMEM);
   1588 		case 1:
   1589 			bus_dmamem_free(sc->sc_dmat, &seg, rseg);
   1590 			break;
   1591 		default:
   1592 			break;
   1593 		}
   1594 	}
   1595 
   1596 	return error;
   1597 }
   1598 
   1599 /*
   1600  * Allocate a jumbo buffer.
   1601  */
   1602 static void *
   1603 mvgbe_jalloc(struct mvgbe_softc *sc)
   1604 {
   1605 	struct mvgbe_jpool_entry *entry;
   1606 
   1607 	entry = LIST_FIRST(&sc->sc_jfree_listhead);
   1608 
   1609 	if (entry == NULL)
   1610 		return NULL;
   1611 
   1612 	LIST_REMOVE(entry, jpool_entries);
   1613 	LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry, jpool_entries);
   1614 	return sc->sc_cdata.mvgbe_jslots[entry->slot];
   1615 }
   1616 
   1617 /*
   1618  * Release a jumbo buffer.
   1619  */
   1620 static void
   1621 mvgbe_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
   1622 {
   1623 	struct mvgbe_jpool_entry *entry;
   1624 	struct mvgbe_softc *sc;
   1625 	int i, s;
   1626 
   1627 	/* Extract the softc struct pointer. */
   1628 	sc = (struct mvgbe_softc *)arg;
   1629 
   1630 	if (sc == NULL)
   1631 		panic("%s: can't find softc pointer!", __func__);
   1632 
   1633 	/* calculate the slot this buffer belongs to */
   1634 
   1635 	i = ((vaddr_t)buf - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf) / MVGBE_JLEN;
   1636 
   1637 	if ((i < 0) || (i >= MVGBE_JSLOTS))
   1638 		panic("%s: asked to free buffer that we don't manage!",
   1639 		    __func__);
   1640 
   1641 	s = splvm();
   1642 	entry = LIST_FIRST(&sc->sc_jinuse_listhead);
   1643 	if (entry == NULL)
   1644 		panic("%s: buffer not in use!", __func__);
   1645 	entry->slot = i;
   1646 	LIST_REMOVE(entry, jpool_entries);
   1647 	LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry, jpool_entries);
   1648 
   1649 	if (__predict_true(m != NULL))
   1650 		pool_cache_put(mb_cache, m);
   1651 	splx(s);
   1652 }
   1653 
   1654 static int
   1655 mvgbe_encap(struct mvgbe_softc *sc, struct mbuf *m_head,
   1656 	      uint32_t *txidx)
   1657 {
   1658 	struct mvgbe_tx_desc *f = NULL;
   1659 	struct mvgbe_txmap_entry *entry;
   1660 	bus_dma_segment_t *txseg;
   1661 	bus_dmamap_t txmap;
   1662 	uint32_t first, current, last, cmdsts = 0;
   1663 	int m_csumflags, i;
   1664 	bool needs_defrag = false;
   1665 
   1666 	DPRINTFN(3, ("mvgbe_encap\n"));
   1667 
   1668 	entry = SIMPLEQ_FIRST(&sc->sc_txmap_head);
   1669 	if (entry == NULL) {
   1670 		DPRINTFN(2, ("mvgbe_encap: no txmap available\n"));
   1671 		return ENOBUFS;
   1672 	}
   1673 	txmap = entry->dmamap;
   1674 
   1675 	first = current = last = *txidx;
   1676 
   1677 	/*
   1678 	 * Preserve m_pkthdr.csum_flags here since m_head might be
   1679 	 * updated by m_defrag()
   1680 	 */
   1681 	m_csumflags = m_head->m_pkthdr.csum_flags;
   1682 
   1683 do_defrag:
   1684 	if (__predict_false(needs_defrag == true)) {
   1685 		/* A small unaligned segment was detected. */
   1686 		struct mbuf *m_new;
   1687 		m_new = m_defrag(m_head, M_DONTWAIT);
   1688 		if (m_new == NULL)
   1689 			return EFBIG;
   1690 		m_head = m_new;
   1691 	}
   1692 
   1693 	/*
   1694 	 * Start packing the mbufs in this chain into
   1695 	 * the fragment pointers. Stop when we run out
   1696 	 * of fragments or hit the end of the mbuf chain.
   1697 	 */
   1698 	if (bus_dmamap_load_mbuf(sc->sc_dmat, txmap, m_head, BUS_DMA_NOWAIT)) {
   1699 		DPRINTFN(1, ("mvgbe_encap: dmamap failed\n"));
   1700 		return ENOBUFS;
   1701 	}
   1702 
   1703 	txseg = txmap->dm_segs;
   1704 
   1705 	if (__predict_true(needs_defrag == false)) {
   1706 		/*
   1707 		 * Detect rarely encountered DMA limitation.
   1708 		 */
   1709 		for (i = 0; i < txmap->dm_nsegs; i++) {
   1710 			if (((txseg[i].ds_addr & 7) != 0) &&
   1711 			    (txseg[i].ds_len <= 8) &&
   1712 			    (txseg[i].ds_len >= 1)
   1713 			    ) {
   1714 				txseg = NULL;
   1715 				bus_dmamap_unload(sc->sc_dmat, txmap);
   1716 				needs_defrag = true;
   1717 				goto do_defrag;
   1718 			}
   1719 		}
   1720 	}
   1721 
   1722 	/* Sync the DMA map. */
   1723 	bus_dmamap_sync(sc->sc_dmat, txmap, 0, txmap->dm_mapsize,
   1724 	    BUS_DMASYNC_PREWRITE);
   1725 
   1726 	if (sc->sc_cdata.mvgbe_tx_cnt + txmap->dm_nsegs >=
   1727 	    MVGBE_TX_RING_CNT) {
   1728 		DPRINTFN(2, ("mvgbe_encap: too few descriptors free\n"));
   1729 		bus_dmamap_unload(sc->sc_dmat, txmap);
   1730 		return ENOBUFS;
   1731 	}
   1732 
   1733 
   1734 	DPRINTFN(2, ("mvgbe_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
   1735 
   1736 	for (i = 0; i < txmap->dm_nsegs; i++) {
   1737 		f = &sc->sc_rdata->mvgbe_tx_ring[current];
   1738 		f->bufptr = txseg[i].ds_addr;
   1739 		f->bytecnt = txseg[i].ds_len;
   1740 		if (i != 0)
   1741 			f->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA;
   1742 		last = current;
   1743 		current = MVGBE_TX_RING_NEXT(current);
   1744 	}
   1745 
   1746 	if (m_csumflags & M_CSUM_IPv4)
   1747 		cmdsts |= MVGBE_TX_GENERATE_IP_CHKSUM;
   1748 	if (m_csumflags & M_CSUM_TCPv4)
   1749 		cmdsts |=
   1750 		    MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_TCP;
   1751 	if (m_csumflags & M_CSUM_UDPv4)
   1752 		cmdsts |=
   1753 		    MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_UDP;
   1754 	if (m_csumflags & (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
   1755 		const int iphdr_unitlen = sizeof(struct ip) / sizeof(uint32_t);
   1756 
   1757 		cmdsts |= MVGBE_TX_IP_NO_FRAG |
   1758 		    MVGBE_TX_IP_HEADER_LEN(iphdr_unitlen);	/* unit is 4B */
   1759 	}
   1760 	if (txmap->dm_nsegs == 1)
   1761 		f->cmdsts = cmdsts		|
   1762 		    MVGBE_TX_GENERATE_CRC	|
   1763 		    MVGBE_TX_ENABLE_INTERRUPT	|
   1764 		    MVGBE_TX_ZERO_PADDING	|
   1765 		    MVGBE_TX_FIRST_DESC		|
   1766 		    MVGBE_TX_LAST_DESC;
   1767 	else {
   1768 		f = &sc->sc_rdata->mvgbe_tx_ring[first];
   1769 		f->cmdsts = cmdsts		|
   1770 		    MVGBE_TX_GENERATE_CRC	|
   1771 		    MVGBE_TX_FIRST_DESC;
   1772 
   1773 		f = &sc->sc_rdata->mvgbe_tx_ring[last];
   1774 		f->cmdsts =
   1775 		    MVGBE_BUFFER_OWNED_BY_DMA	|
   1776 		    MVGBE_TX_ENABLE_INTERRUPT	|
   1777 		    MVGBE_TX_ZERO_PADDING	|
   1778 		    MVGBE_TX_LAST_DESC;
   1779 
   1780 		/* Sync descriptors except first */
   1781 		MVGBE_CDTXSYNC(sc,
   1782 		    (MVGBE_TX_RING_CNT - 1 == *txidx) ? 0 : (*txidx) + 1,
   1783 		    txmap->dm_nsegs - 1,
   1784 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1785 	}
   1786 
   1787 	sc->sc_cdata.mvgbe_tx_chain[last].mvgbe_mbuf = m_head;
   1788 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txmap_head, link);
   1789 	sc->sc_cdata.mvgbe_tx_map[last] = entry;
   1790 
   1791 	/* Finally, sync first descriptor */
   1792 	sc->sc_rdata->mvgbe_tx_ring[first].cmdsts |=
   1793 	    MVGBE_BUFFER_OWNED_BY_DMA;
   1794 	MVGBE_CDTXSYNC(sc, *txidx, 1,
   1795 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1796 
   1797 	sc->sc_cdata.mvgbe_tx_cnt += i;
   1798 	*txidx = current;
   1799 
   1800 	DPRINTFN(3, ("mvgbe_encap: completed successfully\n"));
   1801 
   1802 	return 0;
   1803 }
   1804 
   1805 static void
   1806 mvgbe_rxeof(struct mvgbe_softc *sc)
   1807 {
   1808 	struct mvgbe_chain_data *cdata = &sc->sc_cdata;
   1809 	struct mvgbe_rx_desc *cur_rx;
   1810 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1811 	struct mbuf *m;
   1812 	bus_dmamap_t dmamap;
   1813 	uint32_t rxstat;
   1814 	uint16_t bufsize;
   1815 	int idx, cur, total_len;
   1816 
   1817 	idx = sc->sc_cdata.mvgbe_rx_prod;
   1818 
   1819 	DPRINTFN(3, ("mvgbe_rxeof %d\n", idx));
   1820 
   1821 	for (;;) {
   1822 		cur = idx;
   1823 
   1824 		/* Sync the descriptor */
   1825 		MVGBE_CDRXSYNC(sc, idx,
   1826 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1827 
   1828 		cur_rx = &sc->sc_rdata->mvgbe_rx_ring[idx];
   1829 
   1830 		if ((cur_rx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
   1831 		    MVGBE_BUFFER_OWNED_BY_DMA) {
   1832 			/* Invalidate the descriptor -- it's not ready yet */
   1833 			MVGBE_CDRXSYNC(sc, idx, BUS_DMASYNC_PREREAD);
   1834 			sc->sc_cdata.mvgbe_rx_prod = idx;
   1835 			break;
   1836 		}
   1837 #ifdef DIAGNOSTIC
   1838 		if ((cur_rx->cmdsts &
   1839 		    (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC)) !=
   1840 		    (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC))
   1841 			panic(
   1842 			    "mvgbe_rxeof: buffer size is smaller than packet");
   1843 #endif
   1844 
   1845 		dmamap = sc->sc_cdata.mvgbe_rx_jumbo_map;
   1846 
   1847 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   1848 		    BUS_DMASYNC_POSTREAD);
   1849 
   1850 		m = cdata->mvgbe_rx_chain[idx].mvgbe_mbuf;
   1851 		cdata->mvgbe_rx_chain[idx].mvgbe_mbuf = NULL;
   1852 		total_len = cur_rx->bytecnt - ETHER_CRC_LEN;
   1853 		rxstat = cur_rx->cmdsts;
   1854 		bufsize = cur_rx->bufsize;
   1855 
   1856 		cdata->mvgbe_rx_map[idx] = NULL;
   1857 
   1858 		idx = MVGBE_RX_RING_NEXT(idx);
   1859 
   1860 		if (rxstat & MVGBE_ERROR_SUMMARY) {
   1861 #if 0
   1862 			int err = rxstat & MVGBE_RX_ERROR_CODE_MASK;
   1863 
   1864 			if (err == MVGBE_RX_CRC_ERROR)
   1865 				ifp->if_ierrors++;
   1866 			if (err == MVGBE_RX_OVERRUN_ERROR)
   1867 				ifp->if_ierrors++;
   1868 			if (err == MVGBE_RX_MAX_FRAME_LEN_ERROR)
   1869 				ifp->if_ierrors++;
   1870 			if (err == MVGBE_RX_RESOURCE_ERROR)
   1871 				ifp->if_ierrors++;
   1872 #else
   1873 			ifp->if_ierrors++;
   1874 #endif
   1875 			mvgbe_newbuf(sc, cur, m, dmamap);
   1876 			continue;
   1877 		}
   1878 
   1879 		if (rxstat & MVGBE_RX_IP_FRAME_TYPE) {
   1880 			int flgs = 0;
   1881 
   1882 			/* Check IPv4 header checksum */
   1883 			flgs |= M_CSUM_IPv4;
   1884 			if (!(rxstat & MVGBE_RX_IP_HEADER_OK))
   1885 				flgs |= M_CSUM_IPv4_BAD;
   1886 			else if ((bufsize & MVGBE_RX_IP_FRAGMENT) == 0) {
   1887 				/*
   1888 				 * Check TCPv4/UDPv4 checksum for
   1889 				 * non-fragmented packet only.
   1890 				 *
   1891 				 * It seemd that sometimes
   1892 				 * MVGBE_RX_L4_CHECKSUM_OK bit was set to 0
   1893 				 * even if the checksum is correct and the
   1894 				 * packet was not fragmented. So we don't set
   1895 				 * M_CSUM_TCP_UDP_BAD even if csum bit is 0.
   1896 				 */
   1897 
   1898 				if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
   1899 					MVGBE_RX_L4_TYPE_TCP) &&
   1900 				    ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
   1901 					flgs |= M_CSUM_TCPv4;
   1902 				else if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
   1903 					MVGBE_RX_L4_TYPE_UDP) &&
   1904 				    ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
   1905 					flgs |= M_CSUM_UDPv4;
   1906 			}
   1907 			m->m_pkthdr.csum_flags = flgs;
   1908 		}
   1909 
   1910 		/*
   1911 		 * Try to allocate a new jumbo buffer. If that
   1912 		 * fails, copy the packet to mbufs and put the
   1913 		 * jumbo buffer back in the ring so it can be
   1914 		 * re-used. If allocating mbufs fails, then we
   1915 		 * have to drop the packet.
   1916 		 */
   1917 		if (mvgbe_newbuf(sc, cur, NULL, dmamap) == ENOBUFS) {
   1918 			struct mbuf *m0;
   1919 
   1920 			m0 = m_devget(mtod(m, char *), total_len, 0, ifp, NULL);
   1921 			mvgbe_newbuf(sc, cur, m, dmamap);
   1922 			if (m0 == NULL) {
   1923 				aprint_error_ifnet(ifp,
   1924 				    "no receive buffers available --"
   1925 				    " packet dropped!\n");
   1926 				ifp->if_ierrors++;
   1927 				continue;
   1928 			}
   1929 			m = m0;
   1930 		} else {
   1931 			m->m_pkthdr.rcvif = ifp;
   1932 			m->m_pkthdr.len = m->m_len = total_len;
   1933 		}
   1934 
   1935 		/* Skip on first 2byte (HW header) */
   1936 		m_adj(m,  MVGBE_HWHEADER_SIZE);
   1937 
   1938 		ifp->if_ipackets++;
   1939 
   1940 		bpf_mtap(ifp, m);
   1941 
   1942 		/* pass it on. */
   1943 		(*ifp->if_input)(ifp, m);
   1944 	}
   1945 }
   1946 
   1947 static void
   1948 mvgbe_txeof(struct mvgbe_softc *sc)
   1949 {
   1950 	struct mvgbe_chain_data *cdata = &sc->sc_cdata;
   1951 	struct mvgbe_tx_desc *cur_tx;
   1952 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1953 	struct mvgbe_txmap_entry *entry;
   1954 	int idx;
   1955 
   1956 	DPRINTFN(3, ("mvgbe_txeof\n"));
   1957 
   1958 	/*
   1959 	 * Go through our tx ring and free mbufs for those
   1960 	 * frames that have been sent.
   1961 	 */
   1962 	idx = cdata->mvgbe_tx_cons;
   1963 	while (idx != cdata->mvgbe_tx_prod) {
   1964 		MVGBE_CDTXSYNC(sc, idx, 1,
   1965 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1966 
   1967 		cur_tx = &sc->sc_rdata->mvgbe_tx_ring[idx];
   1968 #ifdef MVGBE_DEBUG
   1969 		if (mvgbe_debug >= 3)
   1970 			mvgbe_dump_txdesc(cur_tx, idx);
   1971 #endif
   1972 		if ((cur_tx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
   1973 		    MVGBE_BUFFER_OWNED_BY_DMA) {
   1974 			MVGBE_CDTXSYNC(sc, idx, 1, BUS_DMASYNC_PREREAD);
   1975 			break;
   1976 		}
   1977 		if (cur_tx->cmdsts & MVGBE_TX_LAST_DESC)
   1978 			ifp->if_opackets++;
   1979 		if (cur_tx->cmdsts & MVGBE_ERROR_SUMMARY) {
   1980 			int err = cur_tx->cmdsts & MVGBE_TX_ERROR_CODE_MASK;
   1981 
   1982 			if (err == MVGBE_TX_LATE_COLLISION_ERROR)
   1983 				ifp->if_collisions++;
   1984 			if (err == MVGBE_TX_UNDERRUN_ERROR)
   1985 				ifp->if_oerrors++;
   1986 			if (err == MVGBE_TX_EXCESSIVE_COLLISION_ERRO)
   1987 				ifp->if_collisions++;
   1988 		}
   1989 		if (cdata->mvgbe_tx_chain[idx].mvgbe_mbuf != NULL) {
   1990 			entry = cdata->mvgbe_tx_map[idx];
   1991 
   1992 			m_freem(cdata->mvgbe_tx_chain[idx].mvgbe_mbuf);
   1993 			cdata->mvgbe_tx_chain[idx].mvgbe_mbuf = NULL;
   1994 
   1995 			bus_dmamap_sync(sc->sc_dmat, entry->dmamap, 0,
   1996 			    entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1997 
   1998 			bus_dmamap_unload(sc->sc_dmat, entry->dmamap);
   1999 			SIMPLEQ_INSERT_TAIL(&sc->sc_txmap_head, entry, link);
   2000 			cdata->mvgbe_tx_map[idx] = NULL;
   2001 		}
   2002 		cdata->mvgbe_tx_cnt--;
   2003 		idx = MVGBE_TX_RING_NEXT(idx);
   2004 	}
   2005 	if (cdata->mvgbe_tx_cnt == 0)
   2006 		ifp->if_timer = 0;
   2007 
   2008 	if (cdata->mvgbe_tx_cnt < MVGBE_TX_RING_CNT - 2)
   2009 		ifp->if_flags &= ~IFF_OACTIVE;
   2010 
   2011 	cdata->mvgbe_tx_cons = idx;
   2012 }
   2013 
   2014 static uint8_t
   2015 mvgbe_crc8(const uint8_t *data, size_t size)
   2016 {
   2017 	int bit;
   2018 	uint8_t byte;
   2019 	uint8_t crc = 0;
   2020 	const uint8_t poly = 0x07;
   2021 
   2022 	while(size--)
   2023 	  for (byte = *data++, bit = NBBY-1; bit >= 0; bit--)
   2024 	    crc = (crc << 1) ^ ((((crc >> 7) ^ (byte >> bit)) & 1) ? poly : 0);
   2025 
   2026 	return crc;
   2027 }
   2028 
   2029 CTASSERT(MVGBE_NDFSMT == MVGBE_NDFOMT);
   2030 
   2031 static void
   2032 mvgbe_filter_setup(struct mvgbe_softc *sc)
   2033 {
   2034 	struct ethercom *ec = &sc->sc_ethercom;
   2035 	struct ifnet *ifp= &sc->sc_ethercom.ec_if;
   2036 	struct ether_multi *enm;
   2037 	struct ether_multistep step;
   2038 	uint32_t dfut[MVGBE_NDFUT], dfsmt[MVGBE_NDFSMT], dfomt[MVGBE_NDFOMT];
   2039 	uint32_t pxc;
   2040 	int i;
   2041 	const uint8_t special[ETHER_ADDR_LEN] = {0x01,0x00,0x5e,0x00,0x00,0x00};
   2042 
   2043 	memset(dfut, 0, sizeof(dfut));
   2044 	memset(dfsmt, 0, sizeof(dfsmt));
   2045 	memset(dfomt, 0, sizeof(dfomt));
   2046 
   2047 	if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
   2048 		goto allmulti;
   2049 	}
   2050 
   2051 	ETHER_FIRST_MULTI(step, ec, enm);
   2052 	while (enm != NULL) {
   2053 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2054 			/* ranges are complex and somewhat rare */
   2055 			goto allmulti;
   2056 		}
   2057 		/* chip handles some IPv4 multicast specially */
   2058 		if (memcmp(enm->enm_addrlo, special, 5) == 0) {
   2059 			i = enm->enm_addrlo[5];
   2060 			dfsmt[i>>2] =
   2061 			    MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
   2062 		} else {
   2063 			i = mvgbe_crc8(enm->enm_addrlo, ETHER_ADDR_LEN);
   2064 			dfomt[i>>2] =
   2065 			    MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
   2066 		}
   2067 
   2068 		ETHER_NEXT_MULTI(step, enm);
   2069 	}
   2070 	goto set;
   2071 
   2072 allmulti:
   2073 	if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
   2074 		for (i = 0; i < MVGBE_NDFSMT; i++) {
   2075 			dfsmt[i] = dfomt[i] =
   2076 			    MVGBE_DF(0, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
   2077 			    MVGBE_DF(1, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
   2078 			    MVGBE_DF(2, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
   2079 			    MVGBE_DF(3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
   2080 		}
   2081 	}
   2082 
   2083 set:
   2084 	pxc = MVGBE_READ(sc, MVGBE_PXC);
   2085 	pxc &= ~MVGBE_PXC_UPM;
   2086 	pxc |= MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP;
   2087 	if (ifp->if_flags & IFF_BROADCAST) {
   2088 		pxc &= ~(MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP);
   2089 	}
   2090 	if (ifp->if_flags & IFF_PROMISC) {
   2091 		pxc |= MVGBE_PXC_UPM;
   2092 	}
   2093 	MVGBE_WRITE(sc, MVGBE_PXC, pxc);
   2094 
   2095 	/* Set Destination Address Filter Unicast Table */
   2096 	i = sc->sc_enaddr[5] & 0xf;		/* last nibble */
   2097 	dfut[i>>2] = MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
   2098 	MVGBE_WRITE_FILTER(sc, MVGBE_DFUT, dfut, MVGBE_NDFUT);
   2099 
   2100 	/* Set Destination Address Filter Multicast Tables */
   2101 	MVGBE_WRITE_FILTER(sc, MVGBE_DFSMT, dfsmt, MVGBE_NDFSMT);
   2102 	MVGBE_WRITE_FILTER(sc, MVGBE_DFOMT, dfomt, MVGBE_NDFOMT);
   2103 }
   2104 
   2105 #ifdef MVGBE_DEBUG
   2106 static void
   2107 mvgbe_dump_txdesc(struct mvgbe_tx_desc *desc, int idx)
   2108 {
   2109 #define DESC_PRINT(X)					\
   2110 	if (X)						\
   2111 		printf("txdesc[%d]." #X "=%#x\n", idx, X);
   2112 
   2113 #if BYTE_ORDER == BIG_ENDIAN
   2114        DESC_PRINT(desc->bytecnt);
   2115        DESC_PRINT(desc->l4ichk);
   2116        DESC_PRINT(desc->cmdsts);
   2117        DESC_PRINT(desc->nextdescptr);
   2118        DESC_PRINT(desc->bufptr);
   2119 #else	/* LITTLE_ENDIAN */
   2120        DESC_PRINT(desc->cmdsts);
   2121        DESC_PRINT(desc->l4ichk);
   2122        DESC_PRINT(desc->bytecnt);
   2123        DESC_PRINT(desc->bufptr);
   2124        DESC_PRINT(desc->nextdescptr);
   2125 #endif
   2126 #undef DESC_PRINT
   2127 }
   2128 #endif
   2129 
   2130 SYSCTL_SETUP(sysctl_mvgbe, "sysctl mvgbe subtree setup")
   2131 {
   2132 	int rc;
   2133 	const struct sysctlnode *node;
   2134 
   2135 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
   2136 	    0, CTLTYPE_NODE, "hw", NULL,
   2137 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
   2138 		goto err;
   2139 	}
   2140 
   2141 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   2142 	    0, CTLTYPE_NODE, "mvgbe",
   2143 	    SYSCTL_DESCR("mvgbe interface controls"),
   2144 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
   2145 		goto err;
   2146 	}
   2147 
   2148 	mvgbe_root_num = node->sysctl_num;
   2149 	return;
   2150 
   2151 err:
   2152 	aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
   2153 }
   2154 
   2155 static void
   2156 sysctl_mvgbe_init(struct mvgbe_softc *sc)
   2157 {
   2158 	const struct sysctlnode *node;
   2159 	int mvgbe_nodenum;
   2160 
   2161 	if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
   2162 		0, CTLTYPE_NODE, device_xname(sc->sc_dev),
   2163 		SYSCTL_DESCR("mvgbe per-controller controls"),
   2164 		NULL, 0, NULL, 0, CTL_HW, mvgbe_root_num, CTL_CREATE,
   2165 		CTL_EOL) != 0) {
   2166 		aprint_normal_dev(sc->sc_dev, "couldn't create sysctl node\n");
   2167 		return;
   2168 	}
   2169 	mvgbe_nodenum = node->sysctl_num;
   2170 
   2171 	/* interrupt moderation sysctls */
   2172 	if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
   2173 		CTLFLAG_READWRITE, CTLTYPE_INT, "ipginttx",
   2174 		SYSCTL_DESCR("mvgbe TX interrupt moderation timer"),
   2175 		mvgbe_sysctl_ipginttx, 0, (void *)sc,
   2176 		0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
   2177 		CTL_EOL) != 0) {
   2178 		aprint_normal_dev(sc->sc_dev,
   2179 		    "couldn't create ipginttx sysctl node\n");
   2180 	}
   2181 	if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
   2182 		CTLFLAG_READWRITE, CTLTYPE_INT, "ipgintrx",
   2183 		SYSCTL_DESCR("mvgbe RX interrupt moderation timer"),
   2184 		mvgbe_sysctl_ipgintrx, 0, (void *)sc,
   2185 		0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
   2186 		CTL_EOL) != 0) {
   2187 		aprint_normal_dev(sc->sc_dev,
   2188 		    "couldn't create ipginttx sysctl node\n");
   2189 	}
   2190 }
   2191 
   2192 static int
   2193 mvgbe_sysctl_ipginttx(SYSCTLFN_ARGS)
   2194 {
   2195 	int error;
   2196 	unsigned int t;
   2197 	struct sysctlnode node;
   2198 	struct mvgbec_softc *csc;
   2199 	struct mvgbe_softc *sc;
   2200 
   2201 	node = *rnode;
   2202 	sc = node.sysctl_data;
   2203 	csc = device_private(device_parent(sc->sc_dev));
   2204 	t = sc->sc_ipginttx;
   2205 	node.sysctl_data = &t;
   2206 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2207 	if (error || newp == NULL)
   2208 		return error;
   2209 
   2210 	if (mvgbe_ipginttx(csc, sc, t) < 0)
   2211 		return EINVAL;
   2212 	/*
   2213 	 * update the softc with sysctl-changed value, and mark
   2214 	 * for hardware update
   2215 	 */
   2216 	sc->sc_ipginttx = t;
   2217 
   2218 	return 0;
   2219 }
   2220 
   2221 static int
   2222 mvgbe_sysctl_ipgintrx(SYSCTLFN_ARGS)
   2223 {
   2224 	int error;
   2225 	unsigned int t;
   2226 	struct sysctlnode node;
   2227 	struct mvgbec_softc *csc;
   2228 	struct mvgbe_softc *sc;
   2229 
   2230 	node = *rnode;
   2231 	sc = node.sysctl_data;
   2232 	csc = device_private(device_parent(sc->sc_dev));
   2233 	t = sc->sc_ipgintrx;
   2234 	node.sysctl_data = &t;
   2235 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2236 	if (error || newp == NULL)
   2237 		return error;
   2238 
   2239 	if (mvgbe_ipgintrx(csc, sc, t) < 0)
   2240 		return EINVAL;
   2241 	/*
   2242 	 * update the softc with sysctl-changed value, and mark
   2243 	 * for hardware update
   2244 	 */
   2245 	sc->sc_ipgintrx = t;
   2246 
   2247 	return 0;
   2248 }
   2249