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