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