xref: /freebsd/sys/dev/neta/if_mvneta.c (revision efe014e6b177ba1b0d70f7df751f5066d4eafe9b)
1 /*
2  * Copyright (c) 2017 Stormshield.
3  * Copyright (c) 2017 Semihalf.
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 
28 #include "opt_platform.h"
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/endian.h>
35 #include <sys/mbuf.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/smp.h>
43 #include <sys/taskqueue.h>
44 #ifdef MVNETA_KTR
45 #include <sys/ktr.h>
46 #endif
47 
48 #include <net/ethernet.h>
49 #include <net/bpf.h>
50 #include <net/if.h>
51 #include <net/if_arp.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 #include <net/if_vlan_var.h>
56 
57 #include <netinet/in_systm.h>
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
60 #include <netinet/tcp_lro.h>
61 
62 #include <sys/sockio.h>
63 #include <sys/bus.h>
64 #include <machine/bus.h>
65 #include <sys/rman.h>
66 #include <machine/resource.h>
67 
68 #include <dev/mii/mii.h>
69 #include <dev/mii/miivar.h>
70 
71 #include <dev/ofw/openfirm.h>
72 #include <dev/ofw/ofw_bus.h>
73 #include <dev/ofw/ofw_bus_subr.h>
74 
75 #include <dev/mdio/mdio.h>
76 
77 #include <arm/mv/mvvar.h>
78 
79 #if !defined(__aarch64__)
80 #include <arm/mv/mvreg.h>
81 #include <arm/mv/mvwin.h>
82 #endif
83 
84 #include "if_mvnetareg.h"
85 #include "if_mvnetavar.h"
86 
87 #include "miibus_if.h"
88 #include "mdio_if.h"
89 
90 #ifdef MVNETA_DEBUG
91 #define	STATIC /* nothing */
92 #else
93 #define	STATIC static
94 #endif
95 
96 #define	DASSERT(x) KASSERT((x), (#x))
97 
98 #define	A3700_TCLK_250MHZ		250000000
99 
100 /* Device Register Initialization */
101 STATIC int mvneta_initreg(struct ifnet *);
102 
103 /* Descriptor Ring Control for each of queues */
104 STATIC int mvneta_ring_alloc_rx_queue(struct mvneta_softc *, int);
105 STATIC int mvneta_ring_alloc_tx_queue(struct mvneta_softc *, int);
106 STATIC void mvneta_ring_dealloc_rx_queue(struct mvneta_softc *, int);
107 STATIC void mvneta_ring_dealloc_tx_queue(struct mvneta_softc *, int);
108 STATIC int mvneta_ring_init_rx_queue(struct mvneta_softc *, int);
109 STATIC int mvneta_ring_init_tx_queue(struct mvneta_softc *, int);
110 STATIC void mvneta_ring_flush_rx_queue(struct mvneta_softc *, int);
111 STATIC void mvneta_ring_flush_tx_queue(struct mvneta_softc *, int);
112 STATIC void mvneta_dmamap_cb(void *, bus_dma_segment_t *, int, int);
113 STATIC int mvneta_dma_create(struct mvneta_softc *);
114 
115 /* Rx/Tx Queue Control */
116 STATIC int mvneta_rx_queue_init(struct ifnet *, int);
117 STATIC int mvneta_tx_queue_init(struct ifnet *, int);
118 STATIC int mvneta_rx_queue_enable(struct ifnet *, int);
119 STATIC int mvneta_tx_queue_enable(struct ifnet *, int);
120 STATIC void mvneta_rx_lockq(struct mvneta_softc *, int);
121 STATIC void mvneta_rx_unlockq(struct mvneta_softc *, int);
122 STATIC void mvneta_tx_lockq(struct mvneta_softc *, int);
123 STATIC void mvneta_tx_unlockq(struct mvneta_softc *, int);
124 
125 /* Interrupt Handlers */
126 STATIC void mvneta_disable_intr(struct mvneta_softc *);
127 STATIC void mvneta_enable_intr(struct mvneta_softc *);
128 STATIC void mvneta_rxtxth_intr(void *);
129 STATIC int mvneta_misc_intr(struct mvneta_softc *);
130 STATIC void mvneta_tick(void *);
131 /* struct ifnet and mii callbacks*/
132 STATIC int mvneta_xmitfast_locked(struct mvneta_softc *, int, struct mbuf **);
133 STATIC int mvneta_xmit_locked(struct mvneta_softc *, int);
134 #ifdef MVNETA_MULTIQUEUE
135 STATIC int mvneta_transmit(struct ifnet *, struct mbuf *);
136 #else /* !MVNETA_MULTIQUEUE */
137 STATIC void mvneta_start(struct ifnet *);
138 #endif
139 STATIC void mvneta_qflush(struct ifnet *);
140 STATIC void mvneta_tx_task(void *, int);
141 STATIC int mvneta_ioctl(struct ifnet *, u_long, caddr_t);
142 STATIC void mvneta_init(void *);
143 STATIC void mvneta_init_locked(void *);
144 STATIC void mvneta_stop(struct mvneta_softc *);
145 STATIC void mvneta_stop_locked(struct mvneta_softc *);
146 STATIC int mvneta_mediachange(struct ifnet *);
147 STATIC void mvneta_mediastatus(struct ifnet *, struct ifmediareq *);
148 STATIC void mvneta_portup(struct mvneta_softc *);
149 STATIC void mvneta_portdown(struct mvneta_softc *);
150 
151 /* Link State Notify */
152 STATIC void mvneta_update_autoneg(struct mvneta_softc *, int);
153 STATIC int mvneta_update_media(struct mvneta_softc *, int);
154 STATIC void mvneta_adjust_link(struct mvneta_softc *);
155 STATIC void mvneta_update_eee(struct mvneta_softc *);
156 STATIC void mvneta_update_fc(struct mvneta_softc *);
157 STATIC void mvneta_link_isr(struct mvneta_softc *);
158 STATIC void mvneta_linkupdate(struct mvneta_softc *, boolean_t);
159 STATIC void mvneta_linkup(struct mvneta_softc *);
160 STATIC void mvneta_linkdown(struct mvneta_softc *);
161 STATIC void mvneta_linkreset(struct mvneta_softc *);
162 
163 /* Tx Subroutines */
164 STATIC int mvneta_tx_queue(struct mvneta_softc *, struct mbuf **, int);
165 STATIC void mvneta_tx_set_csumflag(struct ifnet *,
166     struct mvneta_tx_desc *, struct mbuf *);
167 STATIC void mvneta_tx_queue_complete(struct mvneta_softc *, int);
168 STATIC void mvneta_tx_drain(struct mvneta_softc *);
169 
170 /* Rx Subroutines */
171 STATIC int mvneta_rx(struct mvneta_softc *, int, int);
172 STATIC void mvneta_rx_queue(struct mvneta_softc *, int, int);
173 STATIC void mvneta_rx_queue_refill(struct mvneta_softc *, int);
174 STATIC void mvneta_rx_set_csumflag(struct ifnet *,
175     struct mvneta_rx_desc *, struct mbuf *);
176 STATIC void mvneta_rx_buf_free(struct mvneta_softc *, struct mvneta_buf *);
177 
178 /* MAC address filter */
179 STATIC void mvneta_filter_setup(struct mvneta_softc *);
180 
181 /* sysctl(9) */
182 STATIC int sysctl_read_mib(SYSCTL_HANDLER_ARGS);
183 STATIC int sysctl_clear_mib(SYSCTL_HANDLER_ARGS);
184 STATIC int sysctl_set_queue_rxthtime(SYSCTL_HANDLER_ARGS);
185 STATIC void sysctl_mvneta_init(struct mvneta_softc *);
186 
187 /* MIB */
188 STATIC void mvneta_clear_mib(struct mvneta_softc *);
189 STATIC uint64_t mvneta_read_mib(struct mvneta_softc *, int);
190 STATIC void mvneta_update_mib(struct mvneta_softc *);
191 
192 /* Switch */
193 STATIC boolean_t mvneta_find_ethernet_prop_switch(phandle_t, phandle_t);
194 STATIC boolean_t mvneta_has_switch(device_t);
195 
196 #define	mvneta_sc_lock(sc) mtx_lock(&sc->mtx)
197 #define	mvneta_sc_unlock(sc) mtx_unlock(&sc->mtx)
198 
199 STATIC struct mtx mii_mutex;
200 STATIC int mii_init = 0;
201 
202 /* Device */
203 STATIC int mvneta_detach(device_t);
204 /* MII */
205 STATIC int mvneta_miibus_readreg(device_t, int, int);
206 STATIC int mvneta_miibus_writereg(device_t, int, int, int);
207 
208 /* Clock */
209 STATIC uint32_t mvneta_get_clk(void);
210 
211 static device_method_t mvneta_methods[] = {
212 	/* Device interface */
213 	DEVMETHOD(device_detach,	mvneta_detach),
214 	/* MII interface */
215 	DEVMETHOD(miibus_readreg,       mvneta_miibus_readreg),
216 	DEVMETHOD(miibus_writereg,      mvneta_miibus_writereg),
217 	/* MDIO interface */
218 	DEVMETHOD(mdio_readreg,		mvneta_miibus_readreg),
219 	DEVMETHOD(mdio_writereg,	mvneta_miibus_writereg),
220 
221 	/* End */
222 	DEVMETHOD_END
223 };
224 
225 DEFINE_CLASS_0(mvneta, mvneta_driver, mvneta_methods, sizeof(struct mvneta_softc));
226 
227 DRIVER_MODULE(miibus, mvneta, miibus_driver, miibus_devclass, 0, 0);
228 DRIVER_MODULE(mdio, mvneta, mdio_driver, mdio_devclass, 0, 0);
229 MODULE_DEPEND(mvneta, mdio, 1, 1, 1);
230 MODULE_DEPEND(mvneta, ether, 1, 1, 1);
231 MODULE_DEPEND(mvneta, miibus, 1, 1, 1);
232 MODULE_DEPEND(mvneta, mvxpbm, 1, 1, 1);
233 
234 /*
235  * List of MIB register and names
236  */
237 enum mvneta_mib_idx
238 {
239 	MVNETA_MIB_RX_GOOD_OCT_IDX,
240 	MVNETA_MIB_RX_BAD_OCT_IDX,
241 	MVNETA_MIB_TX_MAC_TRNS_ERR_IDX,
242 	MVNETA_MIB_RX_GOOD_FRAME_IDX,
243 	MVNETA_MIB_RX_BAD_FRAME_IDX,
244 	MVNETA_MIB_RX_BCAST_FRAME_IDX,
245 	MVNETA_MIB_RX_MCAST_FRAME_IDX,
246 	MVNETA_MIB_RX_FRAME64_OCT_IDX,
247 	MVNETA_MIB_RX_FRAME127_OCT_IDX,
248 	MVNETA_MIB_RX_FRAME255_OCT_IDX,
249 	MVNETA_MIB_RX_FRAME511_OCT_IDX,
250 	MVNETA_MIB_RX_FRAME1023_OCT_IDX,
251 	MVNETA_MIB_RX_FRAMEMAX_OCT_IDX,
252 	MVNETA_MIB_TX_GOOD_OCT_IDX,
253 	MVNETA_MIB_TX_GOOD_FRAME_IDX,
254 	MVNETA_MIB_TX_EXCES_COL_IDX,
255 	MVNETA_MIB_TX_MCAST_FRAME_IDX,
256 	MVNETA_MIB_TX_BCAST_FRAME_IDX,
257 	MVNETA_MIB_TX_MAC_CTL_ERR_IDX,
258 	MVNETA_MIB_FC_SENT_IDX,
259 	MVNETA_MIB_FC_GOOD_IDX,
260 	MVNETA_MIB_FC_BAD_IDX,
261 	MVNETA_MIB_PKT_UNDERSIZE_IDX,
262 	MVNETA_MIB_PKT_FRAGMENT_IDX,
263 	MVNETA_MIB_PKT_OVERSIZE_IDX,
264 	MVNETA_MIB_PKT_JABBER_IDX,
265 	MVNETA_MIB_MAC_RX_ERR_IDX,
266 	MVNETA_MIB_MAC_CRC_ERR_IDX,
267 	MVNETA_MIB_MAC_COL_IDX,
268 	MVNETA_MIB_MAC_LATE_COL_IDX,
269 };
270 
271 STATIC struct mvneta_mib_def {
272 	uint32_t regnum;
273 	int reg64;
274 	const char *sysctl_name;
275 	const char *desc;
276 } mvneta_mib_list[] = {
277 	[MVNETA_MIB_RX_GOOD_OCT_IDX] = {MVNETA_MIB_RX_GOOD_OCT, 1,
278 	    "rx_good_oct", "Good Octets Rx"},
279 	[MVNETA_MIB_RX_BAD_OCT_IDX] = {MVNETA_MIB_RX_BAD_OCT, 0,
280 	    "rx_bad_oct", "Bad  Octets Rx"},
281 	[MVNETA_MIB_TX_MAC_TRNS_ERR_IDX] = {MVNETA_MIB_TX_MAC_TRNS_ERR, 0,
282 	    "tx_mac_err", "MAC Transmit Error"},
283 	[MVNETA_MIB_RX_GOOD_FRAME_IDX] = {MVNETA_MIB_RX_GOOD_FRAME, 0,
284 	    "rx_good_frame", "Good Frames Rx"},
285 	[MVNETA_MIB_RX_BAD_FRAME_IDX] = {MVNETA_MIB_RX_BAD_FRAME, 0,
286 	    "rx_bad_frame", "Bad Frames Rx"},
287 	[MVNETA_MIB_RX_BCAST_FRAME_IDX] = {MVNETA_MIB_RX_BCAST_FRAME, 0,
288 	    "rx_bcast_frame", "Broadcast Frames Rx"},
289 	[MVNETA_MIB_RX_MCAST_FRAME_IDX] = {MVNETA_MIB_RX_MCAST_FRAME, 0,
290 	    "rx_mcast_frame", "Multicast Frames Rx"},
291 	[MVNETA_MIB_RX_FRAME64_OCT_IDX] = {MVNETA_MIB_RX_FRAME64_OCT, 0,
292 	    "rx_frame_1_64", "Frame Size    1 -   64"},
293 	[MVNETA_MIB_RX_FRAME127_OCT_IDX] = {MVNETA_MIB_RX_FRAME127_OCT, 0,
294 	    "rx_frame_65_127", "Frame Size   65 -  127"},
295 	[MVNETA_MIB_RX_FRAME255_OCT_IDX] = {MVNETA_MIB_RX_FRAME255_OCT, 0,
296 	    "rx_frame_128_255", "Frame Size  128 -  255"},
297 	[MVNETA_MIB_RX_FRAME511_OCT_IDX] = {MVNETA_MIB_RX_FRAME511_OCT, 0,
298 	    "rx_frame_256_511", "Frame Size  256 -  511"},
299 	[MVNETA_MIB_RX_FRAME1023_OCT_IDX] = {MVNETA_MIB_RX_FRAME1023_OCT, 0,
300 	    "rx_frame_512_1023", "Frame Size  512 - 1023"},
301 	[MVNETA_MIB_RX_FRAMEMAX_OCT_IDX] = {MVNETA_MIB_RX_FRAMEMAX_OCT, 0,
302 	    "rx_fame_1024_max", "Frame Size 1024 -  Max"},
303 	[MVNETA_MIB_TX_GOOD_OCT_IDX] = {MVNETA_MIB_TX_GOOD_OCT, 1,
304 	    "tx_good_oct", "Good Octets Tx"},
305 	[MVNETA_MIB_TX_GOOD_FRAME_IDX] = {MVNETA_MIB_TX_GOOD_FRAME, 0,
306 	    "tx_good_frame", "Good Frames Tx"},
307 	[MVNETA_MIB_TX_EXCES_COL_IDX] = {MVNETA_MIB_TX_EXCES_COL, 0,
308 	    "tx_exces_collision", "Excessive Collision"},
309 	[MVNETA_MIB_TX_MCAST_FRAME_IDX] = {MVNETA_MIB_TX_MCAST_FRAME, 0,
310 	    "tx_mcast_frame", "Multicast Frames Tx"},
311 	[MVNETA_MIB_TX_BCAST_FRAME_IDX] = {MVNETA_MIB_TX_BCAST_FRAME, 0,
312 	    "tx_bcast_frame", "Broadcast Frames Tx"},
313 	[MVNETA_MIB_TX_MAC_CTL_ERR_IDX] = {MVNETA_MIB_TX_MAC_CTL_ERR, 0,
314 	    "tx_mac_ctl_err", "Unknown MAC Control"},
315 	[MVNETA_MIB_FC_SENT_IDX] = {MVNETA_MIB_FC_SENT, 0,
316 	    "fc_tx", "Flow Control Tx"},
317 	[MVNETA_MIB_FC_GOOD_IDX] = {MVNETA_MIB_FC_GOOD, 0,
318 	    "fc_rx_good", "Good Flow Control Rx"},
319 	[MVNETA_MIB_FC_BAD_IDX] = {MVNETA_MIB_FC_BAD, 0,
320 	    "fc_rx_bad", "Bad Flow Control Rx"},
321 	[MVNETA_MIB_PKT_UNDERSIZE_IDX] = {MVNETA_MIB_PKT_UNDERSIZE, 0,
322 	    "pkt_undersize", "Undersized Packets Rx"},
323 	[MVNETA_MIB_PKT_FRAGMENT_IDX] = {MVNETA_MIB_PKT_FRAGMENT, 0,
324 	    "pkt_fragment", "Fragmented Packets Rx"},
325 	[MVNETA_MIB_PKT_OVERSIZE_IDX] = {MVNETA_MIB_PKT_OVERSIZE, 0,
326 	    "pkt_oversize", "Oversized Packets Rx"},
327 	[MVNETA_MIB_PKT_JABBER_IDX] = {MVNETA_MIB_PKT_JABBER, 0,
328 	    "pkt_jabber", "Jabber Packets Rx"},
329 	[MVNETA_MIB_MAC_RX_ERR_IDX] = {MVNETA_MIB_MAC_RX_ERR, 0,
330 	    "mac_rx_err", "MAC Rx Errors"},
331 	[MVNETA_MIB_MAC_CRC_ERR_IDX] = {MVNETA_MIB_MAC_CRC_ERR, 0,
332 	    "mac_crc_err", "MAC CRC Errors"},
333 	[MVNETA_MIB_MAC_COL_IDX] = {MVNETA_MIB_MAC_COL, 0,
334 	    "mac_collision", "MAC Collision"},
335 	[MVNETA_MIB_MAC_LATE_COL_IDX] = {MVNETA_MIB_MAC_LATE_COL, 0,
336 	    "mac_late_collision", "MAC Late Collision"},
337 };
338 
339 static struct resource_spec res_spec[] = {
340 	{ SYS_RES_MEMORY, 0, RF_ACTIVE },
341 	{ SYS_RES_IRQ, 0, RF_ACTIVE },
342 	{ -1, 0}
343 };
344 
345 static struct {
346 	driver_intr_t *handler;
347 	char * description;
348 } mvneta_intrs[] = {
349 	{ mvneta_rxtxth_intr, "MVNETA aggregated interrupt" },
350 };
351 
352 STATIC uint32_t
353 mvneta_get_clk()
354 {
355 #if defined(__aarch64__)
356 	return (A3700_TCLK_250MHZ);
357 #else
358 	return (get_tclk());
359 #endif
360 }
361 
362 static int
363 mvneta_set_mac_address(struct mvneta_softc *sc, uint8_t *addr)
364 {
365 	unsigned int mac_h;
366 	unsigned int mac_l;
367 
368 	mac_l = (addr[4] << 8) | (addr[5]);
369 	mac_h = (addr[0] << 24) | (addr[1] << 16) |
370 	    (addr[2] << 8) | (addr[3] << 0);
371 
372 	MVNETA_WRITE(sc, MVNETA_MACAL, mac_l);
373 	MVNETA_WRITE(sc, MVNETA_MACAH, mac_h);
374 	return (0);
375 }
376 
377 static int
378 mvneta_get_mac_address(struct mvneta_softc *sc, uint8_t *addr)
379 {
380 	uint32_t mac_l, mac_h;
381 
382 #ifdef FDT
383 	if (mvneta_fdt_mac_address(sc, addr) == 0)
384 		return (0);
385 #endif
386 	/*
387 	 * Fall back -- use the currently programmed address.
388 	 */
389 	mac_l = MVNETA_READ(sc, MVNETA_MACAL);
390 	mac_h = MVNETA_READ(sc, MVNETA_MACAH);
391 	if (mac_l == 0 && mac_h == 0) {
392 		/*
393 		 * Generate pseudo-random MAC.
394 		 * Set lower part to random number | unit number.
395 		 */
396 		mac_l = arc4random() & ~0xff;
397 		mac_l |= device_get_unit(sc->dev) & 0xff;
398 		mac_h = arc4random();
399 		mac_h &= ~(3 << 24);	/* Clear multicast and LAA bits */
400 		if (bootverbose) {
401 			device_printf(sc->dev,
402 			    "Could not acquire MAC address. "
403 			    "Using randomized one.\n");
404 		}
405 	}
406 
407 	addr[0] = (mac_h & 0xff000000) >> 24;
408 	addr[1] = (mac_h & 0x00ff0000) >> 16;
409 	addr[2] = (mac_h & 0x0000ff00) >> 8;
410 	addr[3] = (mac_h & 0x000000ff);
411 	addr[4] = (mac_l & 0x0000ff00) >> 8;
412 	addr[5] = (mac_l & 0x000000ff);
413 	return (0);
414 }
415 
416 STATIC boolean_t
417 mvneta_find_ethernet_prop_switch(phandle_t ethernet, phandle_t node)
418 {
419 	boolean_t ret;
420 	phandle_t child, switch_eth_handle, switch_eth;
421 
422 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
423 		if (OF_getencprop(child, "ethernet", (void*)&switch_eth_handle,
424 		    sizeof(switch_eth_handle)) > 0) {
425 			if (switch_eth_handle > 0) {
426 				switch_eth = OF_node_from_xref(
427 				    switch_eth_handle);
428 
429 				if (switch_eth == ethernet)
430 					return (true);
431 			}
432 		}
433 
434 		ret = mvneta_find_ethernet_prop_switch(ethernet, child);
435 		if (ret != 0)
436 			return (ret);
437 	}
438 
439 	return (false);
440 }
441 
442 STATIC boolean_t
443 mvneta_has_switch(device_t self)
444 {
445 	phandle_t node;
446 
447 	node = ofw_bus_get_node(self);
448 
449 	return mvneta_find_ethernet_prop_switch(node, OF_finddevice("/"));
450 }
451 
452 STATIC int
453 mvneta_dma_create(struct mvneta_softc *sc)
454 {
455 	size_t maxsize, maxsegsz;
456 	size_t q;
457 	int error;
458 
459 	/*
460 	 * Create Tx DMA
461 	 */
462 	maxsize = maxsegsz = sizeof(struct mvneta_tx_desc) * MVNETA_TX_RING_CNT;
463 
464 	error = bus_dma_tag_create(
465 	    bus_get_dma_tag(sc->dev),		/* parent */
466 	    16, 0,                              /* alignment, boundary */
467 	    BUS_SPACE_MAXADDR_32BIT,            /* lowaddr */
468 	    BUS_SPACE_MAXADDR,                  /* highaddr */
469 	    NULL, NULL,                         /* filtfunc, filtfuncarg */
470 	    maxsize,				/* maxsize */
471 	    1,					/* nsegments */
472 	    maxsegsz,				/* maxsegsz */
473 	    0,					/* flags */
474 	    NULL, NULL,				/* lockfunc, lockfuncarg */
475 	    &sc->tx_dtag);			/* dmat */
476 	if (error != 0) {
477 		device_printf(sc->dev,
478 		    "Failed to create DMA tag for Tx descriptors.\n");
479 		goto fail;
480 	}
481 	error = bus_dma_tag_create(
482 	    bus_get_dma_tag(sc->dev),		/* parent */
483 	    1, 0,				/* alignment, boundary */
484 	    BUS_SPACE_MAXADDR_32BIT,		/* lowaddr */
485 	    BUS_SPACE_MAXADDR,			/* highaddr */
486 	    NULL, NULL,				/* filtfunc, filtfuncarg */
487 	    MVNETA_MAX_FRAME,			/* maxsize */
488 	    MVNETA_TX_SEGLIMIT,			/* nsegments */
489 	    MVNETA_MAX_FRAME,			/* maxsegsz */
490 	    BUS_DMA_ALLOCNOW,			/* flags */
491 	    NULL, NULL,				/* lockfunc, lockfuncarg */
492 	    &sc->txmbuf_dtag);
493 	if (error != 0) {
494 		device_printf(sc->dev,
495 		    "Failed to create DMA tag for Tx mbufs.\n");
496 		goto fail;
497 	}
498 
499 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) {
500 		error = mvneta_ring_alloc_tx_queue(sc, q);
501 		if (error != 0) {
502 			device_printf(sc->dev,
503 			    "Failed to allocate DMA safe memory for TxQ: %zu\n", q);
504 			goto fail;
505 		}
506 	}
507 
508 	/*
509 	 * Create Rx DMA.
510 	 */
511 	/* Create tag for Rx descripors */
512 	error = bus_dma_tag_create(
513 	    bus_get_dma_tag(sc->dev),		/* parent */
514 	    32, 0,                              /* alignment, boundary */
515 	    BUS_SPACE_MAXADDR_32BIT,            /* lowaddr */
516 	    BUS_SPACE_MAXADDR,                  /* highaddr */
517 	    NULL, NULL,                         /* filtfunc, filtfuncarg */
518 	    sizeof(struct mvneta_rx_desc) * MVNETA_RX_RING_CNT, /* maxsize */
519 	    1,					/* nsegments */
520 	    sizeof(struct mvneta_rx_desc) * MVNETA_RX_RING_CNT, /* maxsegsz */
521 	    0,					/* flags */
522 	    NULL, NULL,				/* lockfunc, lockfuncarg */
523 	    &sc->rx_dtag);			/* dmat */
524 	if (error != 0) {
525 		device_printf(sc->dev,
526 		    "Failed to create DMA tag for Rx descriptors.\n");
527 		goto fail;
528 	}
529 
530 	/* Create tag for Rx buffers */
531 	error = bus_dma_tag_create(
532 	    bus_get_dma_tag(sc->dev),		/* parent */
533 	    32, 0,				/* alignment, boundary */
534 	    BUS_SPACE_MAXADDR_32BIT,		/* lowaddr */
535 	    BUS_SPACE_MAXADDR,			/* highaddr */
536 	    NULL, NULL,				/* filtfunc, filtfuncarg */
537 	    MVNETA_MAX_FRAME, 1,		/* maxsize, nsegments */
538 	    MVNETA_MAX_FRAME,			/* maxsegsz */
539 	    0,					/* flags */
540 	    NULL, NULL,				/* lockfunc, lockfuncarg */
541 	    &sc->rxbuf_dtag);			/* dmat */
542 	if (error != 0) {
543 		device_printf(sc->dev,
544 		    "Failed to create DMA tag for Rx buffers.\n");
545 		goto fail;
546 	}
547 
548 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
549 		if (mvneta_ring_alloc_rx_queue(sc, q) != 0) {
550 			device_printf(sc->dev,
551 			    "Failed to allocate DMA safe memory for RxQ: %zu\n", q);
552 			goto fail;
553 		}
554 	}
555 
556 	return (0);
557 fail:
558 	mvneta_detach(sc->dev);
559 
560 	return (error);
561 }
562 
563 /* ARGSUSED */
564 int
565 mvneta_attach(device_t self)
566 {
567 	struct mvneta_softc *sc;
568 	struct ifnet *ifp;
569 	device_t child;
570 	int ifm_target;
571 	int q, error;
572 #if !defined(__aarch64__)
573 	uint32_t reg;
574 #endif
575 
576 	sc = device_get_softc(self);
577 	sc->dev = self;
578 
579 	mtx_init(&sc->mtx, "mvneta_sc", NULL, MTX_DEF);
580 
581 	error = bus_alloc_resources(self, res_spec, sc->res);
582 	if (error) {
583 		device_printf(self, "could not allocate resources\n");
584 		return (ENXIO);
585 	}
586 
587 	sc->version = MVNETA_READ(sc, MVNETA_PV);
588 	device_printf(self, "version is %x\n", sc->version);
589 	callout_init(&sc->tick_ch, 0);
590 
591 	/*
592 	 * make sure DMA engines are in reset state
593 	 */
594 	MVNETA_WRITE(sc, MVNETA_PRXINIT, 0x00000001);
595 	MVNETA_WRITE(sc, MVNETA_PTXINIT, 0x00000001);
596 
597 #if !defined(__aarch64__)
598 	/*
599 	 * Disable port snoop for buffers and descriptors
600 	 * to avoid L2 caching of both without DRAM copy.
601 	 * Obtain coherency settings from the first MBUS
602 	 * window attribute.
603 	 */
604 	if ((MVNETA_READ(sc, MV_WIN_NETA_BASE(0)) & IO_WIN_COH_ATTR_MASK) == 0) {
605 		reg = MVNETA_READ(sc, MVNETA_PSNPCFG);
606 		reg &= ~MVNETA_PSNPCFG_DESCSNP_MASK;
607 		reg &= ~MVNETA_PSNPCFG_BUFSNP_MASK;
608 		MVNETA_WRITE(sc, MVNETA_PSNPCFG, reg);
609 	}
610 #endif
611 
612 	/*
613 	 * MAC address
614 	 */
615 	if (mvneta_get_mac_address(sc, sc->enaddr)) {
616 		device_printf(self, "no mac address.\n");
617 		return (ENXIO);
618 	}
619 	mvneta_set_mac_address(sc, sc->enaddr);
620 
621 	mvneta_disable_intr(sc);
622 
623 	/* Allocate network interface */
624 	ifp = sc->ifp = if_alloc(IFT_ETHER);
625 	if (ifp == NULL) {
626 		device_printf(self, "if_alloc() failed\n");
627 		mvneta_detach(self);
628 		return (ENOMEM);
629 	}
630 	if_initname(ifp, device_get_name(self), device_get_unit(self));
631 
632 	/*
633 	 * We can support 802.1Q VLAN-sized frames and jumbo
634 	 * Ethernet frames.
635 	 */
636 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU;
637 
638 	ifp->if_softc = sc;
639 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
640 #ifdef MVNETA_MULTIQUEUE
641 	ifp->if_transmit = mvneta_transmit;
642 	ifp->if_qflush = mvneta_qflush;
643 #else /* !MVNETA_MULTIQUEUE */
644 	ifp->if_start = mvneta_start;
645 	ifp->if_snd.ifq_drv_maxlen = MVNETA_TX_RING_CNT - 1;
646 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
647 	IFQ_SET_READY(&ifp->if_snd);
648 #endif
649 	ifp->if_init = mvneta_init;
650 	ifp->if_ioctl = mvneta_ioctl;
651 
652 	/*
653 	 * We can do IPv4/TCPv4/UDPv4/TCPv6/UDPv6 checksums in hardware.
654 	 */
655 	ifp->if_capabilities |= IFCAP_HWCSUM;
656 
657 	/*
658 	 * As VLAN hardware tagging is not supported
659 	 * but is necessary to perform VLAN hardware checksums,
660 	 * it is done in the driver
661 	 */
662 	ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
663 
664 	/*
665 	 * Currently IPv6 HW checksum is broken, so make sure it is disabled.
666 	 */
667 	ifp->if_capabilities &= ~IFCAP_HWCSUM_IPV6;
668 	ifp->if_capenable = ifp->if_capabilities;
669 
670 	/*
671 	 * Disabled option(s):
672 	 * - Support for Large Receive Offload
673 	 */
674 	ifp->if_capabilities |= IFCAP_LRO;
675 
676 	ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
677 
678 	sc->rx_frame_size = MCLBYTES; /* ether_ifattach() always sets normal mtu */
679 
680 	/*
681 	 * Device DMA Buffer allocation.
682 	 * Handles resource deallocation in case of failure.
683 	 */
684 	error = mvneta_dma_create(sc);
685 	if (error != 0) {
686 		mvneta_detach(self);
687 		return (error);
688 	}
689 
690 	/* Initialize queues */
691 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) {
692 		error = mvneta_ring_init_tx_queue(sc, q);
693 		if (error != 0) {
694 			mvneta_detach(self);
695 			return (error);
696 		}
697 	}
698 
699 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
700 		error = mvneta_ring_init_rx_queue(sc, q);
701 		if (error != 0) {
702 			mvneta_detach(self);
703 			return (error);
704 		}
705 	}
706 
707 	ether_ifattach(ifp, sc->enaddr);
708 
709 	/*
710 	 * Enable DMA engines and Initialize Device Registers.
711 	 */
712 	MVNETA_WRITE(sc, MVNETA_PRXINIT, 0x00000000);
713 	MVNETA_WRITE(sc, MVNETA_PTXINIT, 0x00000000);
714 	MVNETA_WRITE(sc, MVNETA_PACC, MVNETA_PACC_ACCELERATIONMODE_EDM);
715 	mvneta_sc_lock(sc);
716 	mvneta_filter_setup(sc);
717 	mvneta_sc_unlock(sc);
718 	mvneta_initreg(ifp);
719 
720 	/*
721 	 * Now MAC is working, setup MII.
722 	 */
723 	if (mii_init == 0) {
724 		/*
725 		 * MII bus is shared by all MACs and all PHYs in SoC.
726 		 * serializing the bus access should be safe.
727 		 */
728 		mtx_init(&mii_mutex, "mvneta_mii", NULL, MTX_DEF);
729 		mii_init = 1;
730 	}
731 
732 	/* Attach PHY(s) */
733 	if ((sc->phy_addr != MII_PHY_ANY) && (!sc->use_inband_status)) {
734 		error = mii_attach(self, &sc->miibus, ifp, mvneta_mediachange,
735 		    mvneta_mediastatus, BMSR_DEFCAPMASK, sc->phy_addr,
736 		    MII_OFFSET_ANY, 0);
737 		if (error != 0) {
738 			if (bootverbose) {
739 				device_printf(self,
740 				    "MII attach failed, error: %d\n", error);
741 			}
742 			ether_ifdetach(sc->ifp);
743 			mvneta_detach(self);
744 			return (error);
745 		}
746 		sc->mii = device_get_softc(sc->miibus);
747 		sc->phy_attached = 1;
748 
749 		/* Disable auto-negotiation in MAC - rely on PHY layer */
750 		mvneta_update_autoneg(sc, FALSE);
751 	} else if (sc->use_inband_status == TRUE) {
752 		/* In-band link status */
753 		ifmedia_init(&sc->mvneta_ifmedia, 0, mvneta_mediachange,
754 		    mvneta_mediastatus);
755 
756 		/* Configure media */
757 		ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_1000_T | IFM_FDX,
758 		    0, NULL);
759 		ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_100_TX, 0, NULL);
760 		ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_100_TX | IFM_FDX,
761 		    0, NULL);
762 		ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_10_T, 0, NULL);
763 		ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_10_T | IFM_FDX,
764 		    0, NULL);
765 		ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
766 		ifmedia_set(&sc->mvneta_ifmedia, IFM_ETHER | IFM_AUTO);
767 
768 		/* Enable auto-negotiation */
769 		mvneta_update_autoneg(sc, TRUE);
770 
771 		mvneta_sc_lock(sc);
772 		if (MVNETA_IS_LINKUP(sc))
773 			mvneta_linkup(sc);
774 		else
775 			mvneta_linkdown(sc);
776 		mvneta_sc_unlock(sc);
777 
778 	} else {
779 		/* Fixed-link, use predefined values */
780 		mvneta_update_autoneg(sc, FALSE);
781 		ifmedia_init(&sc->mvneta_ifmedia, 0, mvneta_mediachange,
782 		    mvneta_mediastatus);
783 
784 		ifm_target = IFM_ETHER;
785 		switch (sc->phy_speed) {
786 		case 2500:
787 			if (sc->phy_mode != MVNETA_PHY_SGMII &&
788 			    sc->phy_mode != MVNETA_PHY_QSGMII) {
789 				device_printf(self,
790 				    "2.5G speed can work only in (Q)SGMII mode\n");
791 				ether_ifdetach(sc->ifp);
792 				mvneta_detach(self);
793 				return (ENXIO);
794 			}
795 			ifm_target |= IFM_2500_T;
796 			break;
797 		case 1000:
798 			ifm_target |= IFM_1000_T;
799 			break;
800 		case 100:
801 			ifm_target |= IFM_100_TX;
802 			break;
803 		case 10:
804 			ifm_target |= IFM_10_T;
805 			break;
806 		default:
807 			ether_ifdetach(sc->ifp);
808 			mvneta_detach(self);
809 			return (ENXIO);
810 		}
811 
812 		if (sc->phy_fdx)
813 			ifm_target |= IFM_FDX;
814 		else
815 			ifm_target |= IFM_HDX;
816 
817 		ifmedia_add(&sc->mvneta_ifmedia, ifm_target, 0, NULL);
818 		ifmedia_set(&sc->mvneta_ifmedia, ifm_target);
819 		if_link_state_change(sc->ifp, LINK_STATE_UP);
820 
821 		if (mvneta_has_switch(self)) {
822 			if (bootverbose)
823 				device_printf(self, "This device is attached to a switch\n");
824 			child = device_add_child(sc->dev, "mdio", -1);
825 			if (child == NULL) {
826 				ether_ifdetach(sc->ifp);
827 				mvneta_detach(self);
828 				return (ENXIO);
829 			}
830 			bus_generic_attach(sc->dev);
831 			bus_generic_attach(child);
832 		}
833 
834 		/* Configure MAC media */
835 		mvneta_update_media(sc, ifm_target);
836 	}
837 
838 	sysctl_mvneta_init(sc);
839 
840 	callout_reset(&sc->tick_ch, 0, mvneta_tick, sc);
841 
842 	error = bus_setup_intr(self, sc->res[1],
843 	    INTR_TYPE_NET | INTR_MPSAFE, NULL, mvneta_intrs[0].handler, sc,
844 	    &sc->ih_cookie[0]);
845 	if (error) {
846 		device_printf(self, "could not setup %s\n",
847 		    mvneta_intrs[0].description);
848 		ether_ifdetach(sc->ifp);
849 		mvneta_detach(self);
850 		return (error);
851 	}
852 
853 	return (0);
854 }
855 
856 STATIC int
857 mvneta_detach(device_t dev)
858 {
859 	struct mvneta_softc *sc;
860 	int q;
861 
862 	sc = device_get_softc(dev);
863 
864 	mvneta_stop(sc);
865 	/* Detach network interface */
866 	if (sc->ifp)
867 		if_free(sc->ifp);
868 
869 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++)
870 		mvneta_ring_dealloc_rx_queue(sc, q);
871 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++)
872 		mvneta_ring_dealloc_tx_queue(sc, q);
873 
874 	if (sc->tx_dtag != NULL)
875 		bus_dma_tag_destroy(sc->tx_dtag);
876 	if (sc->rx_dtag != NULL)
877 		bus_dma_tag_destroy(sc->rx_dtag);
878 	if (sc->txmbuf_dtag != NULL)
879 		bus_dma_tag_destroy(sc->txmbuf_dtag);
880 	if (sc->rxbuf_dtag != NULL)
881 		bus_dma_tag_destroy(sc->rxbuf_dtag);
882 
883 	bus_release_resources(dev, res_spec, sc->res);
884 	return (0);
885 }
886 
887 /*
888  * MII
889  */
890 STATIC int
891 mvneta_miibus_readreg(device_t dev, int phy, int reg)
892 {
893 	struct mvneta_softc *sc;
894 	struct ifnet *ifp;
895 	uint32_t smi, val;
896 	int i;
897 
898 	sc = device_get_softc(dev);
899 	ifp = sc->ifp;
900 
901 	mtx_lock(&mii_mutex);
902 
903 	for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) {
904 		if ((MVNETA_READ(sc, MVNETA_SMI) & MVNETA_SMI_BUSY) == 0)
905 			break;
906 		DELAY(1);
907 	}
908 	if (i == MVNETA_PHY_TIMEOUT) {
909 		if_printf(ifp, "SMI busy timeout\n");
910 		mtx_unlock(&mii_mutex);
911 		return (-1);
912 	}
913 
914 	smi = MVNETA_SMI_PHYAD(phy) |
915 	    MVNETA_SMI_REGAD(reg) | MVNETA_SMI_OPCODE_READ;
916 	MVNETA_WRITE(sc, MVNETA_SMI, smi);
917 
918 	for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) {
919 		if ((MVNETA_READ(sc, MVNETA_SMI) & MVNETA_SMI_BUSY) == 0)
920 			break;
921 		DELAY(1);
922 	}
923 
924 	if (i == MVNETA_PHY_TIMEOUT) {
925 		if_printf(ifp, "SMI busy timeout\n");
926 		mtx_unlock(&mii_mutex);
927 		return (-1);
928 	}
929 	for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) {
930 		smi = MVNETA_READ(sc, MVNETA_SMI);
931 		if (smi & MVNETA_SMI_READVALID)
932 			break;
933 		DELAY(1);
934 	}
935 
936 	if (i == MVNETA_PHY_TIMEOUT) {
937 		if_printf(ifp, "SMI busy timeout\n");
938 		mtx_unlock(&mii_mutex);
939 		return (-1);
940 	}
941 
942 	mtx_unlock(&mii_mutex);
943 
944 #ifdef MVNETA_KTR
945 	CTR3(KTR_SPARE2, "%s i=%d, timeout=%d\n", ifp->if_xname, i,
946 	    MVNETA_PHY_TIMEOUT);
947 #endif
948 
949 	val = smi & MVNETA_SMI_DATA_MASK;
950 
951 #ifdef MVNETA_KTR
952 	CTR4(KTR_SPARE2, "%s phy=%d, reg=%#x, val=%#x\n", ifp->if_xname, phy,
953 	    reg, val);
954 #endif
955 	return (val);
956 }
957 
958 STATIC int
959 mvneta_miibus_writereg(device_t dev, int phy, int reg, int val)
960 {
961 	struct mvneta_softc *sc;
962 	struct ifnet *ifp;
963 	uint32_t smi;
964 	int i;
965 
966 	sc = device_get_softc(dev);
967 	ifp = sc->ifp;
968 #ifdef MVNETA_KTR
969 	CTR4(KTR_SPARE2, "%s phy=%d, reg=%#x, val=%#x\n", ifp->if_xname,
970 	    phy, reg, val);
971 #endif
972 
973 	mtx_lock(&mii_mutex);
974 
975 	for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) {
976 		if ((MVNETA_READ(sc, MVNETA_SMI) & MVNETA_SMI_BUSY) == 0)
977 			break;
978 		DELAY(1);
979 	}
980 	if (i == MVNETA_PHY_TIMEOUT) {
981 		if_printf(ifp, "SMI busy timeout\n");
982 		mtx_unlock(&mii_mutex);
983 		return (0);
984 	}
985 
986 	smi = MVNETA_SMI_PHYAD(phy) | MVNETA_SMI_REGAD(reg) |
987 	    MVNETA_SMI_OPCODE_WRITE | (val & MVNETA_SMI_DATA_MASK);
988 	MVNETA_WRITE(sc, MVNETA_SMI, smi);
989 
990 	for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) {
991 		if ((MVNETA_READ(sc, MVNETA_SMI) & MVNETA_SMI_BUSY) == 0)
992 			break;
993 		DELAY(1);
994 	}
995 
996 	mtx_unlock(&mii_mutex);
997 
998 	if (i == MVNETA_PHY_TIMEOUT)
999 		if_printf(ifp, "phy write timed out\n");
1000 
1001 	return (0);
1002 }
1003 
1004 STATIC void
1005 mvneta_portup(struct mvneta_softc *sc)
1006 {
1007 	int q;
1008 
1009 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
1010 		mvneta_rx_lockq(sc, q);
1011 		mvneta_rx_queue_enable(sc->ifp, q);
1012 		mvneta_rx_unlockq(sc, q);
1013 	}
1014 
1015 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) {
1016 		mvneta_tx_lockq(sc, q);
1017 		mvneta_tx_queue_enable(sc->ifp, q);
1018 		mvneta_tx_unlockq(sc, q);
1019 	}
1020 
1021 }
1022 
1023 STATIC void
1024 mvneta_portdown(struct mvneta_softc *sc)
1025 {
1026 	struct mvneta_rx_ring *rx;
1027 	struct mvneta_tx_ring *tx;
1028 	int q, cnt;
1029 	uint32_t reg;
1030 
1031 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
1032 		rx = MVNETA_RX_RING(sc, q);
1033 		mvneta_rx_lockq(sc, q);
1034 		rx->queue_status = MVNETA_QUEUE_DISABLED;
1035 		mvneta_rx_unlockq(sc, q);
1036 	}
1037 
1038 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) {
1039 		tx = MVNETA_TX_RING(sc, q);
1040 		mvneta_tx_lockq(sc, q);
1041 		tx->queue_status = MVNETA_QUEUE_DISABLED;
1042 		mvneta_tx_unlockq(sc, q);
1043 	}
1044 
1045 	/* Wait for all Rx activity to terminate. */
1046 	reg = MVNETA_READ(sc, MVNETA_RQC) & MVNETA_RQC_EN_MASK;
1047 	reg = MVNETA_RQC_DIS(reg);
1048 	MVNETA_WRITE(sc, MVNETA_RQC, reg);
1049 	cnt = 0;
1050 	do {
1051 		if (cnt >= RX_DISABLE_TIMEOUT) {
1052 			if_printf(sc->ifp,
1053 			    "timeout for RX stopped. rqc 0x%x\n", reg);
1054 			break;
1055 		}
1056 		cnt++;
1057 		reg = MVNETA_READ(sc, MVNETA_RQC);
1058 	} while ((reg & MVNETA_RQC_EN_MASK) != 0);
1059 
1060 	/* Wait for all Tx activity to terminate. */
1061 	reg  = MVNETA_READ(sc, MVNETA_PIE);
1062 	reg &= ~MVNETA_PIE_TXPKTINTRPTENB_MASK;
1063 	MVNETA_WRITE(sc, MVNETA_PIE, reg);
1064 
1065 	reg  = MVNETA_READ(sc, MVNETA_PRXTXTIM);
1066 	reg &= ~MVNETA_PRXTXTI_TBTCQ_MASK;
1067 	MVNETA_WRITE(sc, MVNETA_PRXTXTIM, reg);
1068 
1069 	reg = MVNETA_READ(sc, MVNETA_TQC) & MVNETA_TQC_EN_MASK;
1070 	reg = MVNETA_TQC_DIS(reg);
1071 	MVNETA_WRITE(sc, MVNETA_TQC, reg);
1072 	cnt = 0;
1073 	do {
1074 		if (cnt >= TX_DISABLE_TIMEOUT) {
1075 			if_printf(sc->ifp,
1076 			    "timeout for TX stopped. tqc 0x%x\n", reg);
1077 			break;
1078 		}
1079 		cnt++;
1080 		reg = MVNETA_READ(sc, MVNETA_TQC);
1081 	} while ((reg & MVNETA_TQC_EN_MASK) != 0);
1082 
1083 	/* Wait for all Tx FIFO is empty */
1084 	cnt = 0;
1085 	do {
1086 		if (cnt >= TX_FIFO_EMPTY_TIMEOUT) {
1087 			if_printf(sc->ifp,
1088 			    "timeout for TX FIFO drained. ps0 0x%x\n", reg);
1089 			break;
1090 		}
1091 		cnt++;
1092 		reg = MVNETA_READ(sc, MVNETA_PS0);
1093 	} while (((reg & MVNETA_PS0_TXFIFOEMP) == 0) &&
1094 	    ((reg & MVNETA_PS0_TXINPROG) != 0));
1095 }
1096 
1097 /*
1098  * Device Register Initialization
1099  *  reset device registers to device driver default value.
1100  *  the device is not enabled here.
1101  */
1102 STATIC int
1103 mvneta_initreg(struct ifnet *ifp)
1104 {
1105 	struct mvneta_softc *sc;
1106 	int q;
1107 	uint32_t reg;
1108 
1109 	sc = ifp->if_softc;
1110 #ifdef MVNETA_KTR
1111 	CTR1(KTR_SPARE2, "%s initializing device register", ifp->if_xname);
1112 #endif
1113 
1114 	/* Disable Legacy WRR, Disable EJP, Release from reset. */
1115 	MVNETA_WRITE(sc, MVNETA_TQC_1, 0);
1116 	/* Enable mbus retry. */
1117 	MVNETA_WRITE(sc, MVNETA_MBUS_CONF, MVNETA_MBUS_RETRY_EN);
1118 
1119 	/* Init TX/RX Queue Registers */
1120 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
1121 		mvneta_rx_lockq(sc, q);
1122 		if (mvneta_rx_queue_init(ifp, q) != 0) {
1123 			device_printf(sc->dev,
1124 			    "initialization failed: cannot initialize queue\n");
1125 			mvneta_rx_unlockq(sc, q);
1126 			return (ENOBUFS);
1127 		}
1128 		mvneta_rx_unlockq(sc, q);
1129 	}
1130 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) {
1131 		mvneta_tx_lockq(sc, q);
1132 		if (mvneta_tx_queue_init(ifp, q) != 0) {
1133 			device_printf(sc->dev,
1134 			    "initialization failed: cannot initialize queue\n");
1135 			mvneta_tx_unlockq(sc, q);
1136 			return (ENOBUFS);
1137 		}
1138 		mvneta_tx_unlockq(sc, q);
1139 	}
1140 
1141 	/*
1142 	 * Ethernet Unit Control - disable automatic PHY management by HW.
1143 	 * In case the port uses SMI-controlled PHY, poll its status with
1144 	 * mii_tick() and update MAC settings accordingly.
1145 	 */
1146 	reg = MVNETA_READ(sc, MVNETA_EUC);
1147 	reg &= ~MVNETA_EUC_POLLING;
1148 	MVNETA_WRITE(sc, MVNETA_EUC, reg);
1149 
1150 	/* EEE: Low Power Idle */
1151 	reg  = MVNETA_LPIC0_LILIMIT(MVNETA_LPI_LI);
1152 	reg |= MVNETA_LPIC0_TSLIMIT(MVNETA_LPI_TS);
1153 	MVNETA_WRITE(sc, MVNETA_LPIC0, reg);
1154 
1155 	reg  = MVNETA_LPIC1_TWLIMIT(MVNETA_LPI_TW);
1156 	MVNETA_WRITE(sc, MVNETA_LPIC1, reg);
1157 
1158 	reg = MVNETA_LPIC2_MUSTSET;
1159 	MVNETA_WRITE(sc, MVNETA_LPIC2, reg);
1160 
1161 	/* Port MAC Control set 0 */
1162 	reg  = MVNETA_PMACC0_MUSTSET;	/* must write 0x1 */
1163 	reg &= ~MVNETA_PMACC0_PORTEN;	/* port is still disabled */
1164 	reg |= MVNETA_PMACC0_FRAMESIZELIMIT(ifp->if_mtu + MVNETA_ETHER_SIZE);
1165 	MVNETA_WRITE(sc, MVNETA_PMACC0, reg);
1166 
1167 	/* Port MAC Control set 2 */
1168 	reg = MVNETA_READ(sc, MVNETA_PMACC2);
1169 	switch (sc->phy_mode) {
1170 	case MVNETA_PHY_QSGMII:
1171 		reg |= (MVNETA_PMACC2_PCSEN | MVNETA_PMACC2_RGMIIEN);
1172 		MVNETA_WRITE(sc, MVNETA_PSERDESCFG, MVNETA_PSERDESCFG_QSGMII);
1173 		break;
1174 	case MVNETA_PHY_SGMII:
1175 		reg |= (MVNETA_PMACC2_PCSEN | MVNETA_PMACC2_RGMIIEN);
1176 		MVNETA_WRITE(sc, MVNETA_PSERDESCFG, MVNETA_PSERDESCFG_SGMII);
1177 		break;
1178 	case MVNETA_PHY_RGMII:
1179 	case MVNETA_PHY_RGMII_ID:
1180 		reg |= MVNETA_PMACC2_RGMIIEN;
1181 		break;
1182 	}
1183 	reg |= MVNETA_PMACC2_MUSTSET;
1184 	reg &= ~MVNETA_PMACC2_PORTMACRESET;
1185 	MVNETA_WRITE(sc, MVNETA_PMACC2, reg);
1186 
1187 	/* Port Configuration Extended: enable Tx CRC generation */
1188 	reg = MVNETA_READ(sc, MVNETA_PXCX);
1189 	reg &= ~MVNETA_PXCX_TXCRCDIS;
1190 	MVNETA_WRITE(sc, MVNETA_PXCX, reg);
1191 
1192 	/* clear MIB counter registers(clear by read) */
1193 	mvneta_sc_lock(sc);
1194 	mvneta_clear_mib(sc);
1195 	mvneta_sc_unlock(sc);
1196 
1197 	/* Set SDC register except IPGINT bits */
1198 	reg  = MVNETA_SDC_RXBSZ_16_64BITWORDS;
1199 	reg |= MVNETA_SDC_TXBSZ_16_64BITWORDS;
1200 	reg |= MVNETA_SDC_BLMR;
1201 	reg |= MVNETA_SDC_BLMT;
1202 	MVNETA_WRITE(sc, MVNETA_SDC, reg);
1203 
1204 	return (0);
1205 }
1206 
1207 STATIC void
1208 mvneta_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nseg, int error)
1209 {
1210 
1211 	if (error != 0)
1212 		return;
1213 	*(bus_addr_t *)arg = segs->ds_addr;
1214 }
1215 
1216 STATIC int
1217 mvneta_ring_alloc_rx_queue(struct mvneta_softc *sc, int q)
1218 {
1219 	struct mvneta_rx_ring *rx;
1220 	struct mvneta_buf *rxbuf;
1221 	bus_dmamap_t dmap;
1222 	int i, error;
1223 
1224 	if (q >= MVNETA_RX_QNUM_MAX)
1225 		return (EINVAL);
1226 
1227 	rx = MVNETA_RX_RING(sc, q);
1228 	mtx_init(&rx->ring_mtx, "mvneta_rx", NULL, MTX_DEF);
1229 	/* Allocate DMA memory for Rx descriptors */
1230 	error = bus_dmamem_alloc(sc->rx_dtag,
1231 	    (void**)&(rx->desc),
1232 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1233 	    &rx->desc_map);
1234 	if (error != 0 || rx->desc == NULL)
1235 		goto fail;
1236 	error = bus_dmamap_load(sc->rx_dtag, rx->desc_map,
1237 	    rx->desc,
1238 	    sizeof(struct mvneta_rx_desc) * MVNETA_RX_RING_CNT,
1239 	    mvneta_dmamap_cb, &rx->desc_pa, BUS_DMA_NOWAIT);
1240 	if (error != 0)
1241 		goto fail;
1242 
1243 	for (i = 0; i < MVNETA_RX_RING_CNT; i++) {
1244 		error = bus_dmamap_create(sc->rxbuf_dtag, 0, &dmap);
1245 		if (error != 0) {
1246 			device_printf(sc->dev,
1247 			    "Failed to create DMA map for Rx buffer num: %d\n", i);
1248 			goto fail;
1249 		}
1250 		rxbuf = &rx->rxbuf[i];
1251 		rxbuf->dmap = dmap;
1252 		rxbuf->m = NULL;
1253 	}
1254 
1255 	return (0);
1256 fail:
1257 	mvneta_ring_dealloc_rx_queue(sc, q);
1258 	device_printf(sc->dev, "DMA Ring buffer allocation failure.\n");
1259 	return (error);
1260 }
1261 
1262 STATIC int
1263 mvneta_ring_alloc_tx_queue(struct mvneta_softc *sc, int q)
1264 {
1265 	struct mvneta_tx_ring *tx;
1266 	int error;
1267 
1268 	if (q >= MVNETA_TX_QNUM_MAX)
1269 		return (EINVAL);
1270 	tx = MVNETA_TX_RING(sc, q);
1271 	mtx_init(&tx->ring_mtx, "mvneta_tx", NULL, MTX_DEF);
1272 	error = bus_dmamem_alloc(sc->tx_dtag,
1273 	    (void**)&(tx->desc),
1274 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1275 	    &tx->desc_map);
1276 	if (error != 0 || tx->desc == NULL)
1277 		goto fail;
1278 	error = bus_dmamap_load(sc->tx_dtag, tx->desc_map,
1279 	    tx->desc,
1280 	    sizeof(struct mvneta_tx_desc) * MVNETA_TX_RING_CNT,
1281 	    mvneta_dmamap_cb, &tx->desc_pa, BUS_DMA_NOWAIT);
1282 	if (error != 0)
1283 		goto fail;
1284 
1285 #ifdef MVNETA_MULTIQUEUE
1286 	tx->br = buf_ring_alloc(MVNETA_BUFRING_SIZE, M_DEVBUF, M_NOWAIT,
1287 	    &tx->ring_mtx);
1288 	if (tx->br == NULL) {
1289 		device_printf(sc->dev,
1290 		    "Could not setup buffer ring for TxQ(%d)\n", q);
1291 		error = ENOMEM;
1292 		goto fail;
1293 	}
1294 #endif
1295 
1296 	return (0);
1297 fail:
1298 	mvneta_ring_dealloc_tx_queue(sc, q);
1299 	device_printf(sc->dev, "DMA Ring buffer allocation failure.\n");
1300 	return (error);
1301 }
1302 
1303 STATIC void
1304 mvneta_ring_dealloc_tx_queue(struct mvneta_softc *sc, int q)
1305 {
1306 	struct mvneta_tx_ring *tx;
1307 	struct mvneta_buf *txbuf;
1308 	void *kva;
1309 	int error;
1310 	int i;
1311 
1312 	if (q >= MVNETA_TX_QNUM_MAX)
1313 		return;
1314 	tx = MVNETA_TX_RING(sc, q);
1315 
1316 	if (tx->taskq != NULL) {
1317 		/* Remove task */
1318 		while (taskqueue_cancel(tx->taskq, &tx->task, NULL) != 0)
1319 			taskqueue_drain(tx->taskq, &tx->task);
1320 	}
1321 #ifdef MVNETA_MULTIQUEUE
1322 	if (tx->br != NULL)
1323 		drbr_free(tx->br, M_DEVBUF);
1324 #endif
1325 
1326 	if (sc->txmbuf_dtag != NULL) {
1327 		if (mtx_name(&tx->ring_mtx) != NULL) {
1328 			/*
1329 			 * It is assumed that maps are being loaded after mutex
1330 			 * is initialized. Therefore we can skip unloading maps
1331 			 * when mutex is empty.
1332 			 */
1333 			mvneta_tx_lockq(sc, q);
1334 			mvneta_ring_flush_tx_queue(sc, q);
1335 			mvneta_tx_unlockq(sc, q);
1336 		}
1337 		for (i = 0; i < MVNETA_TX_RING_CNT; i++) {
1338 			txbuf = &tx->txbuf[i];
1339 			if (txbuf->dmap != NULL) {
1340 				error = bus_dmamap_destroy(sc->txmbuf_dtag,
1341 				    txbuf->dmap);
1342 				if (error != 0) {
1343 					panic("%s: map busy for Tx descriptor (Q%d, %d)",
1344 					    __func__, q, i);
1345 				}
1346 			}
1347 		}
1348 	}
1349 
1350 	if (tx->desc_pa != 0)
1351 		bus_dmamap_unload(sc->tx_dtag, tx->desc_map);
1352 
1353 	kva = (void *)tx->desc;
1354 	if (kva != NULL)
1355 		bus_dmamem_free(sc->tx_dtag, tx->desc, tx->desc_map);
1356 
1357 	if (mtx_name(&tx->ring_mtx) != NULL)
1358 		mtx_destroy(&tx->ring_mtx);
1359 
1360 	memset(tx, 0, sizeof(*tx));
1361 }
1362 
1363 STATIC void
1364 mvneta_ring_dealloc_rx_queue(struct mvneta_softc *sc, int q)
1365 {
1366 	struct mvneta_rx_ring *rx;
1367 	struct lro_ctrl	*lro;
1368 	void *kva;
1369 
1370 	if (q >= MVNETA_RX_QNUM_MAX)
1371 		return;
1372 
1373 	rx = MVNETA_RX_RING(sc, q);
1374 
1375 	mvneta_ring_flush_rx_queue(sc, q);
1376 
1377 	if (rx->desc_pa != 0)
1378 		bus_dmamap_unload(sc->rx_dtag, rx->desc_map);
1379 
1380 	kva = (void *)rx->desc;
1381 	if (kva != NULL)
1382 		bus_dmamem_free(sc->rx_dtag, rx->desc, rx->desc_map);
1383 
1384 	lro = &rx->lro;
1385 	tcp_lro_free(lro);
1386 
1387 	if (mtx_name(&rx->ring_mtx) != NULL)
1388 		mtx_destroy(&rx->ring_mtx);
1389 
1390 	memset(rx, 0, sizeof(*rx));
1391 }
1392 
1393 STATIC int
1394 mvneta_ring_init_rx_queue(struct mvneta_softc *sc, int q)
1395 {
1396 	struct mvneta_rx_ring *rx;
1397 	struct lro_ctrl	*lro;
1398 	int error;
1399 
1400 	if (q >= MVNETA_RX_QNUM_MAX)
1401 		return (0);
1402 
1403 	rx = MVNETA_RX_RING(sc, q);
1404 	rx->dma = rx->cpu = 0;
1405 	rx->queue_th_received = MVNETA_RXTH_COUNT;
1406 	rx->queue_th_time = (mvneta_get_clk() / 1000) / 10; /* 0.1 [ms] */
1407 
1408 	/* Initialize LRO */
1409 	rx->lro_enabled = FALSE;
1410 	if ((sc->ifp->if_capenable & IFCAP_LRO) != 0) {
1411 		lro = &rx->lro;
1412 		error = tcp_lro_init(lro);
1413 		if (error != 0)
1414 			device_printf(sc->dev, "LRO Initialization failed!\n");
1415 		else {
1416 			rx->lro_enabled = TRUE;
1417 			lro->ifp = sc->ifp;
1418 		}
1419 	}
1420 
1421 	return (0);
1422 }
1423 
1424 STATIC int
1425 mvneta_ring_init_tx_queue(struct mvneta_softc *sc, int q)
1426 {
1427 	struct mvneta_tx_ring *tx;
1428 	struct mvneta_buf *txbuf;
1429 	int i, error;
1430 
1431 	if (q >= MVNETA_TX_QNUM_MAX)
1432 		return (0);
1433 
1434 	tx = MVNETA_TX_RING(sc, q);
1435 
1436 	/* Tx handle */
1437 	for (i = 0; i < MVNETA_TX_RING_CNT; i++) {
1438 		txbuf = &tx->txbuf[i];
1439 		txbuf->m = NULL;
1440 		/* Tx handle needs DMA map for busdma_load_mbuf() */
1441 		error = bus_dmamap_create(sc->txmbuf_dtag, 0,
1442 		    &txbuf->dmap);
1443 		if (error != 0) {
1444 			device_printf(sc->dev,
1445 			    "can't create dma map (tx ring %d)\n", i);
1446 			return (error);
1447 		}
1448 	}
1449 	tx->dma = tx->cpu = 0;
1450 	tx->used = 0;
1451 	tx->drv_error = 0;
1452 	tx->queue_status = MVNETA_QUEUE_DISABLED;
1453 	tx->queue_hung = FALSE;
1454 
1455 	tx->ifp = sc->ifp;
1456 	tx->qidx = q;
1457 	TASK_INIT(&tx->task, 0, mvneta_tx_task, tx);
1458 	tx->taskq = taskqueue_create_fast("mvneta_tx_taskq", M_WAITOK,
1459 	    taskqueue_thread_enqueue, &tx->taskq);
1460 	taskqueue_start_threads(&tx->taskq, 1, PI_NET, "%s: tx_taskq(%d)",
1461 	    device_get_nameunit(sc->dev), q);
1462 
1463 	return (0);
1464 }
1465 
1466 STATIC void
1467 mvneta_ring_flush_tx_queue(struct mvneta_softc *sc, int q)
1468 {
1469 	struct mvneta_tx_ring *tx;
1470 	struct mvneta_buf *txbuf;
1471 	int i;
1472 
1473 	tx = MVNETA_TX_RING(sc, q);
1474 	KASSERT_TX_MTX(sc, q);
1475 
1476 	/* Tx handle */
1477 	for (i = 0; i < MVNETA_TX_RING_CNT; i++) {
1478 		txbuf = &tx->txbuf[i];
1479 		bus_dmamap_unload(sc->txmbuf_dtag, txbuf->dmap);
1480 		if (txbuf->m != NULL) {
1481 			m_freem(txbuf->m);
1482 			txbuf->m = NULL;
1483 		}
1484 	}
1485 	tx->dma = tx->cpu = 0;
1486 	tx->used = 0;
1487 }
1488 
1489 STATIC void
1490 mvneta_ring_flush_rx_queue(struct mvneta_softc *sc, int q)
1491 {
1492 	struct mvneta_rx_ring *rx;
1493 	struct mvneta_buf *rxbuf;
1494 	int i;
1495 
1496 	rx = MVNETA_RX_RING(sc, q);
1497 	KASSERT_RX_MTX(sc, q);
1498 
1499 	/* Rx handle */
1500 	for (i = 0; i < MVNETA_RX_RING_CNT; i++) {
1501 		rxbuf = &rx->rxbuf[i];
1502 		mvneta_rx_buf_free(sc, rxbuf);
1503 	}
1504 	rx->dma = rx->cpu = 0;
1505 }
1506 
1507 /*
1508  * Rx/Tx Queue Control
1509  */
1510 STATIC int
1511 mvneta_rx_queue_init(struct ifnet *ifp, int q)
1512 {
1513 	struct mvneta_softc *sc;
1514 	struct mvneta_rx_ring *rx;
1515 	uint32_t reg;
1516 
1517 	sc = ifp->if_softc;
1518 	KASSERT_RX_MTX(sc, q);
1519 	rx =  MVNETA_RX_RING(sc, q);
1520 	DASSERT(rx->desc_pa != 0);
1521 
1522 	/* descriptor address */
1523 	MVNETA_WRITE(sc, MVNETA_PRXDQA(q), rx->desc_pa);
1524 
1525 	/* Rx buffer size and descriptor ring size */
1526 	reg  = MVNETA_PRXDQS_BUFFERSIZE(sc->rx_frame_size >> 3);
1527 	reg |= MVNETA_PRXDQS_DESCRIPTORSQUEUESIZE(MVNETA_RX_RING_CNT);
1528 	MVNETA_WRITE(sc, MVNETA_PRXDQS(q), reg);
1529 #ifdef MVNETA_KTR
1530 	CTR3(KTR_SPARE2, "%s PRXDQS(%d): %#x", ifp->if_xname, q,
1531 	    MVNETA_READ(sc, MVNETA_PRXDQS(q)));
1532 #endif
1533 	/* Rx packet offset address */
1534 	reg = MVNETA_PRXC_PACKETOFFSET(MVNETA_PACKET_OFFSET >> 3);
1535 	MVNETA_WRITE(sc, MVNETA_PRXC(q), reg);
1536 #ifdef MVNETA_KTR
1537 	CTR3(KTR_SPARE2, "%s PRXC(%d): %#x", ifp->if_xname, q,
1538 	    MVNETA_READ(sc, MVNETA_PRXC(q)));
1539 #endif
1540 
1541 	/* if DMA is not working, register is not updated */
1542 	DASSERT(MVNETA_READ(sc, MVNETA_PRXDQA(q)) == rx->desc_pa);
1543 	return (0);
1544 }
1545 
1546 STATIC int
1547 mvneta_tx_queue_init(struct ifnet *ifp, int q)
1548 {
1549 	struct mvneta_softc *sc;
1550 	struct mvneta_tx_ring *tx;
1551 	uint32_t reg;
1552 
1553 	sc = ifp->if_softc;
1554 	KASSERT_TX_MTX(sc, q);
1555 	tx = MVNETA_TX_RING(sc, q);
1556 	DASSERT(tx->desc_pa != 0);
1557 
1558 	/* descriptor address */
1559 	MVNETA_WRITE(sc, MVNETA_PTXDQA(q), tx->desc_pa);
1560 
1561 	/* descriptor ring size */
1562 	reg = MVNETA_PTXDQS_DQS(MVNETA_TX_RING_CNT);
1563 	MVNETA_WRITE(sc, MVNETA_PTXDQS(q), reg);
1564 
1565 	/* if DMA is not working, register is not updated */
1566 	DASSERT(MVNETA_READ(sc, MVNETA_PTXDQA(q)) == tx->desc_pa);
1567 	return (0);
1568 }
1569 
1570 STATIC int
1571 mvneta_rx_queue_enable(struct ifnet *ifp, int q)
1572 {
1573 	struct mvneta_softc *sc;
1574 	struct mvneta_rx_ring *rx;
1575 	uint32_t reg;
1576 
1577 	sc = ifp->if_softc;
1578 	rx = MVNETA_RX_RING(sc, q);
1579 	KASSERT_RX_MTX(sc, q);
1580 
1581 	/* Set Rx interrupt threshold */
1582 	reg  = MVNETA_PRXDQTH_ODT(rx->queue_th_received);
1583 	MVNETA_WRITE(sc, MVNETA_PRXDQTH(q), reg);
1584 
1585 	reg  = MVNETA_PRXITTH_RITT(rx->queue_th_time);
1586 	MVNETA_WRITE(sc, MVNETA_PRXITTH(q), reg);
1587 
1588 	/* Unmask RXTX_TH Intr. */
1589 	reg = MVNETA_READ(sc, MVNETA_PRXTXTIM);
1590 	reg |= MVNETA_PRXTXTI_RBICTAPQ(q); /* Rx Buffer Interrupt Coalese */
1591 	MVNETA_WRITE(sc, MVNETA_PRXTXTIM, reg);
1592 
1593 	/* Enable Rx queue */
1594 	reg = MVNETA_READ(sc, MVNETA_RQC) & MVNETA_RQC_EN_MASK;
1595 	reg |= MVNETA_RQC_ENQ(q);
1596 	MVNETA_WRITE(sc, MVNETA_RQC, reg);
1597 
1598 	rx->queue_status = MVNETA_QUEUE_WORKING;
1599 	return (0);
1600 }
1601 
1602 STATIC int
1603 mvneta_tx_queue_enable(struct ifnet *ifp, int q)
1604 {
1605 	struct mvneta_softc *sc;
1606 	struct mvneta_tx_ring *tx;
1607 
1608 	sc = ifp->if_softc;
1609 	tx = MVNETA_TX_RING(sc, q);
1610 	KASSERT_TX_MTX(sc, q);
1611 
1612 	/* Enable Tx queue */
1613 	MVNETA_WRITE(sc, MVNETA_TQC, MVNETA_TQC_ENQ(q));
1614 
1615 	tx->queue_status = MVNETA_QUEUE_IDLE;
1616 	tx->queue_hung = FALSE;
1617 	return (0);
1618 }
1619 
1620 STATIC __inline void
1621 mvneta_rx_lockq(struct mvneta_softc *sc, int q)
1622 {
1623 
1624 	DASSERT(q >= 0);
1625 	DASSERT(q < MVNETA_RX_QNUM_MAX);
1626 	mtx_lock(&sc->rx_ring[q].ring_mtx);
1627 }
1628 
1629 STATIC __inline void
1630 mvneta_rx_unlockq(struct mvneta_softc *sc, int q)
1631 {
1632 
1633 	DASSERT(q >= 0);
1634 	DASSERT(q < MVNETA_RX_QNUM_MAX);
1635 	mtx_unlock(&sc->rx_ring[q].ring_mtx);
1636 }
1637 
1638 STATIC __inline int __unused
1639 mvneta_tx_trylockq(struct mvneta_softc *sc, int q)
1640 {
1641 
1642 	DASSERT(q >= 0);
1643 	DASSERT(q < MVNETA_TX_QNUM_MAX);
1644 	return (mtx_trylock(&sc->tx_ring[q].ring_mtx));
1645 }
1646 
1647 STATIC __inline void
1648 mvneta_tx_lockq(struct mvneta_softc *sc, int q)
1649 {
1650 
1651 	DASSERT(q >= 0);
1652 	DASSERT(q < MVNETA_TX_QNUM_MAX);
1653 	mtx_lock(&sc->tx_ring[q].ring_mtx);
1654 }
1655 
1656 STATIC __inline void
1657 mvneta_tx_unlockq(struct mvneta_softc *sc, int q)
1658 {
1659 
1660 	DASSERT(q >= 0);
1661 	DASSERT(q < MVNETA_TX_QNUM_MAX);
1662 	mtx_unlock(&sc->tx_ring[q].ring_mtx);
1663 }
1664 
1665 /*
1666  * Interrupt Handlers
1667  */
1668 STATIC void
1669 mvneta_disable_intr(struct mvneta_softc *sc)
1670 {
1671 
1672 	MVNETA_WRITE(sc, MVNETA_EUIM, 0);
1673 	MVNETA_WRITE(sc, MVNETA_EUIC, 0);
1674 	MVNETA_WRITE(sc, MVNETA_PRXTXTIM, 0);
1675 	MVNETA_WRITE(sc, MVNETA_PRXTXTIC, 0);
1676 	MVNETA_WRITE(sc, MVNETA_PRXTXIM, 0);
1677 	MVNETA_WRITE(sc, MVNETA_PRXTXIC, 0);
1678 	MVNETA_WRITE(sc, MVNETA_PMIM, 0);
1679 	MVNETA_WRITE(sc, MVNETA_PMIC, 0);
1680 	MVNETA_WRITE(sc, MVNETA_PIE, 0);
1681 }
1682 
1683 STATIC void
1684 mvneta_enable_intr(struct mvneta_softc *sc)
1685 {
1686 	uint32_t reg;
1687 
1688 	/* Enable Summary Bit to check all interrupt cause. */
1689 	reg = MVNETA_READ(sc, MVNETA_PRXTXTIM);
1690 	reg |= MVNETA_PRXTXTI_PMISCICSUMMARY;
1691 	MVNETA_WRITE(sc, MVNETA_PRXTXTIM, reg);
1692 
1693 	if (sc->use_inband_status) {
1694 		/* Enable Port MISC Intr. (via RXTX_TH_Summary bit) */
1695 		MVNETA_WRITE(sc, MVNETA_PMIM, MVNETA_PMI_PHYSTATUSCHNG |
1696 		    MVNETA_PMI_LINKCHANGE | MVNETA_PMI_PSCSYNCCHANGE);
1697 	}
1698 
1699 	/* Enable All Queue Interrupt */
1700 	reg  = MVNETA_READ(sc, MVNETA_PIE);
1701 	reg |= MVNETA_PIE_RXPKTINTRPTENB_MASK;
1702 	reg |= MVNETA_PIE_TXPKTINTRPTENB_MASK;
1703 	MVNETA_WRITE(sc, MVNETA_PIE, reg);
1704 }
1705 
1706 STATIC void
1707 mvneta_rxtxth_intr(void *arg)
1708 {
1709 	struct mvneta_softc *sc;
1710 	struct ifnet *ifp;
1711 	uint32_t ic, queues;
1712 
1713 	sc = arg;
1714 	ifp = sc->ifp;
1715 #ifdef MVNETA_KTR
1716 	CTR1(KTR_SPARE2, "%s got RXTX_TH_Intr", ifp->if_xname);
1717 #endif
1718 	ic = MVNETA_READ(sc, MVNETA_PRXTXTIC);
1719 	if (ic == 0)
1720 		return;
1721 	MVNETA_WRITE(sc, MVNETA_PRXTXTIC, ~ic);
1722 
1723 	/* Ack maintance interrupt first */
1724 	if (__predict_false((ic & MVNETA_PRXTXTI_PMISCICSUMMARY) &&
1725 	    sc->use_inband_status)) {
1726 		mvneta_sc_lock(sc);
1727 		mvneta_misc_intr(sc);
1728 		mvneta_sc_unlock(sc);
1729 	}
1730 	if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING)))
1731 		return;
1732 	/* RxTxTH interrupt */
1733 	queues = MVNETA_PRXTXTI_GET_RBICTAPQ(ic);
1734 	if (__predict_true(queues)) {
1735 #ifdef MVNETA_KTR
1736 		CTR1(KTR_SPARE2, "%s got PRXTXTIC: +RXEOF", ifp->if_xname);
1737 #endif
1738 		/* At the moment the driver support only one RX queue. */
1739 		DASSERT(MVNETA_IS_QUEUE_SET(queues, 0));
1740 		mvneta_rx(sc, 0, 0);
1741 	}
1742 }
1743 
1744 STATIC int
1745 mvneta_misc_intr(struct mvneta_softc *sc)
1746 {
1747 	uint32_t ic;
1748 	int claimed = 0;
1749 
1750 #ifdef MVNETA_KTR
1751 	CTR1(KTR_SPARE2, "%s got MISC_INTR", sc->ifp->if_xname);
1752 #endif
1753 	KASSERT_SC_MTX(sc);
1754 
1755 	for (;;) {
1756 		ic = MVNETA_READ(sc, MVNETA_PMIC);
1757 		ic &= MVNETA_READ(sc, MVNETA_PMIM);
1758 		if (ic == 0)
1759 			break;
1760 		MVNETA_WRITE(sc, MVNETA_PMIC, ~ic);
1761 		claimed = 1;
1762 
1763 		if (ic & (MVNETA_PMI_PHYSTATUSCHNG |
1764 		    MVNETA_PMI_LINKCHANGE | MVNETA_PMI_PSCSYNCCHANGE))
1765 			mvneta_link_isr(sc);
1766 	}
1767 	return (claimed);
1768 }
1769 
1770 STATIC void
1771 mvneta_tick(void *arg)
1772 {
1773 	struct mvneta_softc *sc;
1774 	struct mvneta_tx_ring *tx;
1775 	struct mvneta_rx_ring *rx;
1776 	int q;
1777 	uint32_t fc_prev, fc_curr;
1778 
1779 	sc = arg;
1780 
1781 	/*
1782 	 * This is done before mib update to get the right stats
1783 	 * for this tick.
1784 	 */
1785 	mvneta_tx_drain(sc);
1786 
1787 	/* Extract previous flow-control frame received counter. */
1788 	fc_prev = sc->sysctl_mib[MVNETA_MIB_FC_GOOD_IDX].counter;
1789 	/* Read mib registers (clear by read). */
1790 	mvneta_update_mib(sc);
1791 	/* Extract current flow-control frame received counter. */
1792 	fc_curr = sc->sysctl_mib[MVNETA_MIB_FC_GOOD_IDX].counter;
1793 
1794 
1795 	if (sc->phy_attached && sc->ifp->if_flags & IFF_UP) {
1796 		mvneta_sc_lock(sc);
1797 		mii_tick(sc->mii);
1798 
1799 		/* Adjust MAC settings */
1800 		mvneta_adjust_link(sc);
1801 		mvneta_sc_unlock(sc);
1802 	}
1803 
1804 	/*
1805 	 * We were unable to refill the rx queue and left the rx func, leaving
1806 	 * the ring without mbuf and no way to call the refill func.
1807 	 */
1808 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
1809 		rx = MVNETA_RX_RING(sc, q);
1810 		if (rx->needs_refill == TRUE) {
1811 			mvneta_rx_lockq(sc, q);
1812 			mvneta_rx_queue_refill(sc, q);
1813 			mvneta_rx_unlockq(sc, q);
1814 		}
1815 	}
1816 
1817 	/*
1818 	 * Watchdog:
1819 	 * - check if queue is mark as hung.
1820 	 * - ignore hung status if we received some pause frame
1821 	 *   as hardware may have paused packet transmit.
1822 	 */
1823 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) {
1824 		/*
1825 		 * We should take queue lock, but as we only read
1826 		 * queue status we can do it without lock, we may
1827 		 * only missdetect queue status for one tick.
1828 		 */
1829 		tx = MVNETA_TX_RING(sc, q);
1830 
1831 		if (tx->queue_hung && (fc_curr - fc_prev) == 0)
1832 			goto timeout;
1833 	}
1834 
1835 	callout_schedule(&sc->tick_ch, hz);
1836 	return;
1837 
1838 timeout:
1839 	if_printf(sc->ifp, "watchdog timeout\n");
1840 
1841 	mvneta_sc_lock(sc);
1842 	sc->counter_watchdog++;
1843 	sc->counter_watchdog_mib++;
1844 	/* Trigger reinitialize sequence. */
1845 	mvneta_stop_locked(sc);
1846 	mvneta_init_locked(sc);
1847 	mvneta_sc_unlock(sc);
1848 }
1849 
1850 STATIC void
1851 mvneta_qflush(struct ifnet *ifp)
1852 {
1853 #ifdef MVNETA_MULTIQUEUE
1854 	struct mvneta_softc *sc;
1855 	struct mvneta_tx_ring *tx;
1856 	struct mbuf *m;
1857 	size_t q;
1858 
1859 	sc = ifp->if_softc;
1860 
1861 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) {
1862 		tx = MVNETA_TX_RING(sc, q);
1863 		mvneta_tx_lockq(sc, q);
1864 		while ((m = buf_ring_dequeue_sc(tx->br)) != NULL)
1865 			m_freem(m);
1866 		mvneta_tx_unlockq(sc, q);
1867 	}
1868 #endif
1869 	if_qflush(ifp);
1870 }
1871 
1872 STATIC void
1873 mvneta_tx_task(void *arg, int pending)
1874 {
1875 	struct mvneta_softc *sc;
1876 	struct mvneta_tx_ring *tx;
1877 	struct ifnet *ifp;
1878 	int error;
1879 
1880 	tx = arg;
1881 	ifp = tx->ifp;
1882 	sc = ifp->if_softc;
1883 
1884 	mvneta_tx_lockq(sc, tx->qidx);
1885 	error = mvneta_xmit_locked(sc, tx->qidx);
1886 	mvneta_tx_unlockq(sc, tx->qidx);
1887 
1888 	/* Try again */
1889 	if (__predict_false(error != 0 && error != ENETDOWN)) {
1890 		pause("mvneta_tx_task_sleep", 1);
1891 		taskqueue_enqueue(tx->taskq, &tx->task);
1892 	}
1893 }
1894 
1895 STATIC int
1896 mvneta_xmitfast_locked(struct mvneta_softc *sc, int q, struct mbuf **m)
1897 {
1898 	struct mvneta_tx_ring *tx;
1899 	struct ifnet *ifp;
1900 	int error;
1901 
1902 	KASSERT_TX_MTX(sc, q);
1903 	tx = MVNETA_TX_RING(sc, q);
1904 	error = 0;
1905 
1906 	ifp = sc->ifp;
1907 
1908 	/* Dont enqueue packet if the queue is disabled. */
1909 	if (__predict_false(tx->queue_status == MVNETA_QUEUE_DISABLED)) {
1910 		m_freem(*m);
1911 		*m = NULL;
1912 		return (ENETDOWN);
1913 	}
1914 
1915 	/* Reclaim mbuf if above threshold. */
1916 	if (__predict_true(tx->used > MVNETA_TX_RECLAIM_COUNT))
1917 		mvneta_tx_queue_complete(sc, q);
1918 
1919 	/* Do not call transmit path if queue is already too full. */
1920 	if (__predict_false(tx->used >
1921 	    MVNETA_TX_RING_CNT - MVNETA_TX_SEGLIMIT))
1922 		return (ENOBUFS);
1923 
1924 	error = mvneta_tx_queue(sc, m, q);
1925 	if (__predict_false(error != 0))
1926 		return (error);
1927 
1928 	/* Send a copy of the frame to the BPF listener */
1929 	ETHER_BPF_MTAP(ifp, *m);
1930 
1931 	/* Set watchdog on */
1932 	tx->watchdog_time = ticks;
1933 	tx->queue_status = MVNETA_QUEUE_WORKING;
1934 
1935 	return (error);
1936 }
1937 
1938 #ifdef MVNETA_MULTIQUEUE
1939 STATIC int
1940 mvneta_transmit(struct ifnet *ifp, struct mbuf *m)
1941 {
1942 	struct mvneta_softc *sc;
1943 	struct mvneta_tx_ring *tx;
1944 	int error;
1945 	int q;
1946 
1947 	sc = ifp->if_softc;
1948 
1949 	/* Use default queue if there is no flow id as thread can migrate. */
1950 	if (__predict_true(M_HASHTYPE_GET(m) != M_HASHTYPE_NONE))
1951 		q = m->m_pkthdr.flowid % MVNETA_TX_QNUM_MAX;
1952 	else
1953 		q = 0;
1954 
1955 	tx = MVNETA_TX_RING(sc, q);
1956 
1957 	/* If buf_ring is full start transmit immediatly. */
1958 	if (buf_ring_full(tx->br)) {
1959 		mvneta_tx_lockq(sc, q);
1960 		mvneta_xmit_locked(sc, q);
1961 		mvneta_tx_unlockq(sc, q);
1962 	}
1963 
1964 	/*
1965 	 * If the buf_ring is empty we will not reorder packets.
1966 	 * If the lock is available transmit without using buf_ring.
1967 	 */
1968 	if (buf_ring_empty(tx->br) && mvneta_tx_trylockq(sc, q) != 0) {
1969 		error = mvneta_xmitfast_locked(sc, q, &m);
1970 		mvneta_tx_unlockq(sc, q);
1971 		if (__predict_true(error == 0))
1972 			return (0);
1973 
1974 		/* Transmit can fail in fastpath. */
1975 		if (__predict_false(m == NULL))
1976 			return (error);
1977 	}
1978 
1979 	/* Enqueue then schedule taskqueue. */
1980 	error = drbr_enqueue(ifp, tx->br, m);
1981 	if (__predict_false(error != 0))
1982 		return (error);
1983 
1984 	taskqueue_enqueue(tx->taskq, &tx->task);
1985 	return (0);
1986 }
1987 
1988 STATIC int
1989 mvneta_xmit_locked(struct mvneta_softc *sc, int q)
1990 {
1991 	struct ifnet *ifp;
1992 	struct mvneta_tx_ring *tx;
1993 	struct mbuf *m;
1994 	int error;
1995 
1996 	KASSERT_TX_MTX(sc, q);
1997 	ifp = sc->ifp;
1998 	tx = MVNETA_TX_RING(sc, q);
1999 	error = 0;
2000 
2001 	while ((m = drbr_peek(ifp, tx->br)) != NULL) {
2002 		error = mvneta_xmitfast_locked(sc, q, &m);
2003 		if (__predict_false(error != 0)) {
2004 			if (m != NULL)
2005 				drbr_putback(ifp, tx->br, m);
2006 			else
2007 				drbr_advance(ifp, tx->br);
2008 			break;
2009 		}
2010 		drbr_advance(ifp, tx->br);
2011 	}
2012 
2013 	return (error);
2014 }
2015 #else /* !MVNETA_MULTIQUEUE */
2016 STATIC void
2017 mvneta_start(struct ifnet *ifp)
2018 {
2019 	struct mvneta_softc *sc;
2020 	struct mvneta_tx_ring *tx;
2021 	int error;
2022 
2023 	sc = ifp->if_softc;
2024 	tx = MVNETA_TX_RING(sc, 0);
2025 
2026 	mvneta_tx_lockq(sc, 0);
2027 	error = mvneta_xmit_locked(sc, 0);
2028 	mvneta_tx_unlockq(sc, 0);
2029 	/* Handle retransmit in the background taskq. */
2030 	if (__predict_false(error != 0 && error != ENETDOWN))
2031 		taskqueue_enqueue(tx->taskq, &tx->task);
2032 }
2033 
2034 STATIC int
2035 mvneta_xmit_locked(struct mvneta_softc *sc, int q)
2036 {
2037 	struct ifnet *ifp;
2038 	struct mvneta_tx_ring *tx;
2039 	struct mbuf *m;
2040 	int error;
2041 
2042 	KASSERT_TX_MTX(sc, q);
2043 	ifp = sc->ifp;
2044 	tx = MVNETA_TX_RING(sc, 0);
2045 	error = 0;
2046 
2047 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
2048 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2049 		if (m == NULL)
2050 			break;
2051 
2052 		error = mvneta_xmitfast_locked(sc, q, &m);
2053 		if (__predict_false(error != 0)) {
2054 			if (m != NULL)
2055 				IFQ_DRV_PREPEND(&ifp->if_snd, m);
2056 			break;
2057 		}
2058 	}
2059 
2060 	return (error);
2061 }
2062 #endif
2063 
2064 STATIC int
2065 mvneta_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2066 {
2067 	struct mvneta_softc *sc;
2068 	struct mvneta_rx_ring *rx;
2069 	struct ifreq *ifr;
2070 	int error, mask;
2071 	uint32_t flags;
2072 	int q;
2073 
2074 	error = 0;
2075 	sc = ifp->if_softc;
2076 	ifr = (struct ifreq *)data;
2077 	switch (cmd) {
2078 	case SIOCSIFFLAGS:
2079 		mvneta_sc_lock(sc);
2080 		if (ifp->if_flags & IFF_UP) {
2081 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2082 				flags = ifp->if_flags ^ sc->mvneta_if_flags;
2083 
2084 				if (flags != 0)
2085 					sc->mvneta_if_flags = ifp->if_flags;
2086 
2087 				if ((flags & IFF_PROMISC) != 0)
2088 					mvneta_filter_setup(sc);
2089 			} else {
2090 				mvneta_init_locked(sc);
2091 				sc->mvneta_if_flags = ifp->if_flags;
2092 				if (sc->phy_attached)
2093 					mii_mediachg(sc->mii);
2094 				mvneta_sc_unlock(sc);
2095 				break;
2096 			}
2097 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2098 			mvneta_stop_locked(sc);
2099 
2100 		sc->mvneta_if_flags = ifp->if_flags;
2101 		mvneta_sc_unlock(sc);
2102 		break;
2103 	case SIOCSIFCAP:
2104 		if (ifp->if_mtu > sc->tx_csum_limit &&
2105 		    ifr->ifr_reqcap & IFCAP_TXCSUM)
2106 			ifr->ifr_reqcap &= ~IFCAP_TXCSUM;
2107 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
2108 		if (mask & IFCAP_HWCSUM) {
2109 			ifp->if_capenable &= ~IFCAP_HWCSUM;
2110 			ifp->if_capenable |= IFCAP_HWCSUM & ifr->ifr_reqcap;
2111 			if (ifp->if_capenable & IFCAP_TXCSUM)
2112 				ifp->if_hwassist = CSUM_IP | CSUM_TCP |
2113 				    CSUM_UDP;
2114 			else
2115 				ifp->if_hwassist = 0;
2116 		}
2117 		if (mask & IFCAP_LRO) {
2118 			mvneta_sc_lock(sc);
2119 			ifp->if_capenable ^= IFCAP_LRO;
2120 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2121 				for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
2122 					rx = MVNETA_RX_RING(sc, q);
2123 					rx->lro_enabled = !rx->lro_enabled;
2124 				}
2125 			}
2126 			mvneta_sc_unlock(sc);
2127 		}
2128 		VLAN_CAPABILITIES(ifp);
2129 		break;
2130 	case SIOCSIFMEDIA:
2131 		if ((IFM_SUBTYPE(ifr->ifr_media) == IFM_1000_T ||
2132 		    IFM_SUBTYPE(ifr->ifr_media) == IFM_2500_T) &&
2133 		    (ifr->ifr_media & IFM_FDX) == 0) {
2134 			device_printf(sc->dev,
2135 			    "%s half-duplex unsupported\n",
2136 			    IFM_SUBTYPE(ifr->ifr_media) == IFM_1000_T ?
2137 			    "1000Base-T" :
2138 			    "2500Base-T");
2139 			error = EINVAL;
2140 			break;
2141 		}
2142 	case SIOCGIFMEDIA: /* FALLTHROUGH */
2143 	case SIOCGIFXMEDIA:
2144 		if (!sc->phy_attached)
2145 			error = ifmedia_ioctl(ifp, ifr, &sc->mvneta_ifmedia,
2146 			    cmd);
2147 		else
2148 			error = ifmedia_ioctl(ifp, ifr, &sc->mii->mii_media,
2149 			    cmd);
2150 		break;
2151 	case SIOCSIFMTU:
2152 		if (ifr->ifr_mtu < 68 || ifr->ifr_mtu > MVNETA_MAX_FRAME -
2153 		    MVNETA_ETHER_SIZE) {
2154 			error = EINVAL;
2155 		} else {
2156 			ifp->if_mtu = ifr->ifr_mtu;
2157 			mvneta_sc_lock(sc);
2158 			if (ifp->if_mtu + MVNETA_ETHER_SIZE <= MCLBYTES) {
2159 				sc->rx_frame_size = MCLBYTES;
2160 			} else {
2161 				sc->rx_frame_size = MJUM9BYTES;
2162 			}
2163 			if (ifp->if_mtu > sc->tx_csum_limit) {
2164 				ifp->if_capenable &= ~IFCAP_TXCSUM;
2165 				ifp->if_hwassist = 0;
2166 			} else {
2167 				ifp->if_capenable |= IFCAP_TXCSUM;
2168 				ifp->if_hwassist = CSUM_IP | CSUM_TCP |
2169 					CSUM_UDP;
2170 			}
2171 			/*
2172 			 * Reinitialize RX queues.
2173 			 * We need to update RX descriptor size.
2174 			 */
2175 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2176 				mvneta_stop_locked(sc);
2177 
2178 			for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
2179 				mvneta_rx_lockq(sc, q);
2180 				if (mvneta_rx_queue_init(ifp, q) != 0) {
2181 					device_printf(sc->dev,
2182 					    "initialization failed:"
2183 					    " cannot initialize queue\n");
2184 					mvneta_rx_unlockq(sc, q);
2185 					error = ENOBUFS;
2186 					break;
2187 				}
2188 				mvneta_rx_unlockq(sc, q);
2189 			}
2190 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2191 				mvneta_init_locked(sc);
2192 
2193 			mvneta_sc_unlock(sc);
2194                 }
2195                 break;
2196 
2197 	default:
2198 		error = ether_ioctl(ifp, cmd, data);
2199 		break;
2200 	}
2201 
2202 	return (error);
2203 }
2204 
2205 STATIC void
2206 mvneta_init_locked(void *arg)
2207 {
2208 	struct mvneta_softc *sc;
2209 	struct ifnet *ifp;
2210 	uint32_t reg;
2211 	int q, cpu;
2212 
2213 	sc = arg;
2214 	ifp = sc->ifp;
2215 
2216 	if (!device_is_attached(sc->dev) ||
2217 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2218 		return;
2219 
2220 	mvneta_disable_intr(sc);
2221 	callout_stop(&sc->tick_ch);
2222 
2223 	/* Get the latest mac address */
2224 	bcopy(IF_LLADDR(ifp), sc->enaddr, ETHER_ADDR_LEN);
2225 	mvneta_set_mac_address(sc, sc->enaddr);
2226 	mvneta_filter_setup(sc);
2227 
2228 	/* Start DMA Engine */
2229 	MVNETA_WRITE(sc, MVNETA_PRXINIT, 0x00000000);
2230 	MVNETA_WRITE(sc, MVNETA_PTXINIT, 0x00000000);
2231 	MVNETA_WRITE(sc, MVNETA_PACC, MVNETA_PACC_ACCELERATIONMODE_EDM);
2232 
2233 	/* Enable port */
2234 	reg  = MVNETA_READ(sc, MVNETA_PMACC0);
2235 	reg |= MVNETA_PMACC0_PORTEN;
2236 	reg &= ~MVNETA_PMACC0_FRAMESIZELIMIT_MASK;
2237 	reg |= MVNETA_PMACC0_FRAMESIZELIMIT(ifp->if_mtu + MVNETA_ETHER_SIZE);
2238 	MVNETA_WRITE(sc, MVNETA_PMACC0, reg);
2239 
2240 	/* Allow access to each TXQ/RXQ from both CPU's */
2241 	for (cpu = 0; cpu < mp_ncpus; ++cpu)
2242 		MVNETA_WRITE(sc, MVNETA_PCP2Q(cpu),
2243 		    MVNETA_PCP2Q_TXQEN_MASK | MVNETA_PCP2Q_RXQEN_MASK);
2244 
2245 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
2246 		mvneta_rx_lockq(sc, q);
2247 		mvneta_rx_queue_refill(sc, q);
2248 		mvneta_rx_unlockq(sc, q);
2249 	}
2250 
2251 	if (!sc->phy_attached)
2252 		mvneta_linkup(sc);
2253 
2254 	/* Enable interrupt */
2255 	mvneta_enable_intr(sc);
2256 
2257 	/* Set Counter */
2258 	callout_schedule(&sc->tick_ch, hz);
2259 
2260 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2261 }
2262 
2263 STATIC void
2264 mvneta_init(void *arg)
2265 {
2266 	struct mvneta_softc *sc;
2267 
2268 	sc = arg;
2269 	mvneta_sc_lock(sc);
2270 	mvneta_init_locked(sc);
2271 	if (sc->phy_attached)
2272 		mii_mediachg(sc->mii);
2273 	mvneta_sc_unlock(sc);
2274 }
2275 
2276 /* ARGSUSED */
2277 STATIC void
2278 mvneta_stop_locked(struct mvneta_softc *sc)
2279 {
2280 	struct ifnet *ifp;
2281 	struct mvneta_rx_ring *rx;
2282 	struct mvneta_tx_ring *tx;
2283 	uint32_t reg;
2284 	int q;
2285 
2286 	ifp = sc->ifp;
2287 	if (ifp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2288 		return;
2289 
2290 	mvneta_disable_intr(sc);
2291 
2292 	callout_stop(&sc->tick_ch);
2293 
2294 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2295 
2296 	/* Link down */
2297 	if (sc->linkup == TRUE)
2298 		mvneta_linkdown(sc);
2299 
2300 	/* Reset the MAC Port Enable bit */
2301 	reg = MVNETA_READ(sc, MVNETA_PMACC0);
2302 	reg &= ~MVNETA_PMACC0_PORTEN;
2303 	MVNETA_WRITE(sc, MVNETA_PMACC0, reg);
2304 
2305 	/* Disable each of queue */
2306 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
2307 		rx = MVNETA_RX_RING(sc, q);
2308 
2309 		mvneta_rx_lockq(sc, q);
2310 		mvneta_ring_flush_rx_queue(sc, q);
2311 		mvneta_rx_unlockq(sc, q);
2312 	}
2313 
2314 	/*
2315 	 * Hold Reset state of DMA Engine
2316 	 * (must write 0x0 to restart it)
2317 	 */
2318 	MVNETA_WRITE(sc, MVNETA_PRXINIT, 0x00000001);
2319 	MVNETA_WRITE(sc, MVNETA_PTXINIT, 0x00000001);
2320 
2321 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) {
2322 		tx = MVNETA_TX_RING(sc, q);
2323 
2324 		mvneta_tx_lockq(sc, q);
2325 		mvneta_ring_flush_tx_queue(sc, q);
2326 		mvneta_tx_unlockq(sc, q);
2327 	}
2328 }
2329 
2330 STATIC void
2331 mvneta_stop(struct mvneta_softc *sc)
2332 {
2333 
2334 	mvneta_sc_lock(sc);
2335 	mvneta_stop_locked(sc);
2336 	mvneta_sc_unlock(sc);
2337 }
2338 
2339 STATIC int
2340 mvneta_mediachange(struct ifnet *ifp)
2341 {
2342 	struct mvneta_softc *sc;
2343 
2344 	sc = ifp->if_softc;
2345 
2346 	if (!sc->phy_attached && !sc->use_inband_status) {
2347 		/* We shouldn't be here */
2348 		if_printf(ifp, "Cannot change media in fixed-link mode!\n");
2349 		return (0);
2350 	}
2351 
2352 	if (sc->use_inband_status) {
2353 		mvneta_update_media(sc, sc->mvneta_ifmedia.ifm_media);
2354 		return (0);
2355 	}
2356 
2357 	mvneta_sc_lock(sc);
2358 
2359 	/* Update PHY */
2360 	mii_mediachg(sc->mii);
2361 
2362 	mvneta_sc_unlock(sc);
2363 
2364 	return (0);
2365 }
2366 
2367 STATIC void
2368 mvneta_get_media(struct mvneta_softc *sc, struct ifmediareq *ifmr)
2369 {
2370 	uint32_t psr;
2371 
2372 	psr = MVNETA_READ(sc, MVNETA_PSR);
2373 
2374 	/* Speed */
2375 	if (psr & MVNETA_PSR_GMIISPEED)
2376 		ifmr->ifm_active = IFM_ETHER_SUBTYPE_SET(IFM_1000_T);
2377 	else if (psr & MVNETA_PSR_MIISPEED)
2378 		ifmr->ifm_active = IFM_ETHER_SUBTYPE_SET(IFM_100_TX);
2379 	else if (psr & MVNETA_PSR_LINKUP)
2380 		ifmr->ifm_active = IFM_ETHER_SUBTYPE_SET(IFM_10_T);
2381 
2382 	/* Duplex */
2383 	if (psr & MVNETA_PSR_FULLDX)
2384 		ifmr->ifm_active |= IFM_FDX;
2385 
2386 	/* Link */
2387 	ifmr->ifm_status = IFM_AVALID;
2388 	if (psr & MVNETA_PSR_LINKUP)
2389 		ifmr->ifm_status |= IFM_ACTIVE;
2390 }
2391 
2392 STATIC void
2393 mvneta_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2394 {
2395 	struct mvneta_softc *sc;
2396 	struct mii_data *mii;
2397 
2398 	sc = ifp->if_softc;
2399 
2400 	if (!sc->phy_attached && !sc->use_inband_status) {
2401 		ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
2402 		return;
2403 	}
2404 
2405 	mvneta_sc_lock(sc);
2406 
2407 	if (sc->use_inband_status) {
2408 		mvneta_get_media(sc, ifmr);
2409 		mvneta_sc_unlock(sc);
2410 		return;
2411 	}
2412 
2413 	mii = sc->mii;
2414 	mii_pollstat(mii);
2415 
2416 	ifmr->ifm_active = mii->mii_media_active;
2417 	ifmr->ifm_status = mii->mii_media_status;
2418 
2419 	mvneta_sc_unlock(sc);
2420 }
2421 
2422 /*
2423  * Link State Notify
2424  */
2425 STATIC void
2426 mvneta_update_autoneg(struct mvneta_softc *sc, int enable)
2427 {
2428 	int reg;
2429 
2430 	if (enable) {
2431 		reg = MVNETA_READ(sc, MVNETA_PANC);
2432 		reg &= ~(MVNETA_PANC_FORCELINKFAIL | MVNETA_PANC_FORCELINKPASS |
2433 		    MVNETA_PANC_ANFCEN);
2434 		reg |= MVNETA_PANC_ANDUPLEXEN | MVNETA_PANC_ANSPEEDEN |
2435 		    MVNETA_PANC_INBANDANEN;
2436 		MVNETA_WRITE(sc, MVNETA_PANC, reg);
2437 
2438 		reg = MVNETA_READ(sc, MVNETA_PMACC2);
2439 		reg |= MVNETA_PMACC2_INBANDANMODE;
2440 		MVNETA_WRITE(sc, MVNETA_PMACC2, reg);
2441 
2442 		reg = MVNETA_READ(sc, MVNETA_PSOMSCD);
2443 		reg |= MVNETA_PSOMSCD_ENABLE;
2444 		MVNETA_WRITE(sc, MVNETA_PSOMSCD, reg);
2445 	} else {
2446 		reg = MVNETA_READ(sc, MVNETA_PANC);
2447 		reg &= ~(MVNETA_PANC_FORCELINKFAIL | MVNETA_PANC_FORCELINKPASS |
2448 		    MVNETA_PANC_ANDUPLEXEN | MVNETA_PANC_ANSPEEDEN |
2449 		    MVNETA_PANC_INBANDANEN);
2450 		MVNETA_WRITE(sc, MVNETA_PANC, reg);
2451 
2452 		reg = MVNETA_READ(sc, MVNETA_PMACC2);
2453 		reg &= ~MVNETA_PMACC2_INBANDANMODE;
2454 		MVNETA_WRITE(sc, MVNETA_PMACC2, reg);
2455 
2456 		reg = MVNETA_READ(sc, MVNETA_PSOMSCD);
2457 		reg &= ~MVNETA_PSOMSCD_ENABLE;
2458 		MVNETA_WRITE(sc, MVNETA_PSOMSCD, reg);
2459 	}
2460 }
2461 
2462 STATIC int
2463 mvneta_update_media(struct mvneta_softc *sc, int media)
2464 {
2465 	int reg, err;
2466 	boolean_t running;
2467 
2468 	err = 0;
2469 
2470 	mvneta_sc_lock(sc);
2471 
2472 	mvneta_linkreset(sc);
2473 
2474 	running = (sc->ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
2475 	if (running)
2476 		mvneta_stop_locked(sc);
2477 
2478 	sc->autoneg = (IFM_SUBTYPE(media) == IFM_AUTO);
2479 
2480 	if (sc->use_inband_status)
2481 		mvneta_update_autoneg(sc, IFM_SUBTYPE(media) == IFM_AUTO);
2482 
2483 	mvneta_update_eee(sc);
2484 	mvneta_update_fc(sc);
2485 
2486 	if (IFM_SUBTYPE(media) != IFM_AUTO) {
2487 		reg = MVNETA_READ(sc, MVNETA_PANC);
2488 		reg &= ~(MVNETA_PANC_SETGMIISPEED |
2489 		    MVNETA_PANC_SETMIISPEED |
2490 		    MVNETA_PANC_SETFULLDX);
2491 		if (IFM_SUBTYPE(media) == IFM_1000_T ||
2492 		    IFM_SUBTYPE(media) == IFM_2500_T) {
2493 			if ((media & IFM_FDX) == 0) {
2494 				device_printf(sc->dev,
2495 				    "%s half-duplex unsupported\n",
2496 				    IFM_SUBTYPE(media) == IFM_1000_T ?
2497 				    "1000Base-T" :
2498 				    "2500Base-T");
2499 				err = EINVAL;
2500 				goto out;
2501 			}
2502 			reg |= MVNETA_PANC_SETGMIISPEED;
2503 		} else if (IFM_SUBTYPE(media) == IFM_100_TX)
2504 			reg |= MVNETA_PANC_SETMIISPEED;
2505 
2506 		if (media & IFM_FDX)
2507 			reg |= MVNETA_PANC_SETFULLDX;
2508 
2509 		MVNETA_WRITE(sc, MVNETA_PANC, reg);
2510 	}
2511 out:
2512 	if (running)
2513 		mvneta_init_locked(sc);
2514 	mvneta_sc_unlock(sc);
2515 	return (err);
2516 }
2517 
2518 STATIC void
2519 mvneta_adjust_link(struct mvneta_softc *sc)
2520 {
2521 	boolean_t phy_linkup;
2522 	int reg;
2523 
2524 	/* Update eee/fc */
2525 	mvneta_update_eee(sc);
2526 	mvneta_update_fc(sc);
2527 
2528 	/* Check for link change */
2529 	phy_linkup = (sc->mii->mii_media_status &
2530 	    (IFM_AVALID | IFM_ACTIVE)) == (IFM_AVALID | IFM_ACTIVE);
2531 
2532 	if (sc->linkup != phy_linkup)
2533 		mvneta_linkupdate(sc, phy_linkup);
2534 
2535 	/* Don't update media on disabled link */
2536 	if (!phy_linkup)
2537 		return;
2538 
2539 	/* Check for media type change */
2540 	if (sc->mvneta_media != sc->mii->mii_media_active) {
2541 		sc->mvneta_media = sc->mii->mii_media_active;
2542 
2543 		reg = MVNETA_READ(sc, MVNETA_PANC);
2544 		reg &= ~(MVNETA_PANC_SETGMIISPEED |
2545 		    MVNETA_PANC_SETMIISPEED |
2546 		    MVNETA_PANC_SETFULLDX);
2547 		if (IFM_SUBTYPE(sc->mvneta_media) == IFM_1000_T ||
2548 		    IFM_SUBTYPE(sc->mvneta_media) == IFM_2500_T) {
2549 			reg |= MVNETA_PANC_SETGMIISPEED;
2550 		} else if (IFM_SUBTYPE(sc->mvneta_media) == IFM_100_TX)
2551 			reg |= MVNETA_PANC_SETMIISPEED;
2552 
2553 		if (sc->mvneta_media & IFM_FDX)
2554 			reg |= MVNETA_PANC_SETFULLDX;
2555 
2556 		MVNETA_WRITE(sc, MVNETA_PANC, reg);
2557 	}
2558 }
2559 
2560 STATIC void
2561 mvneta_link_isr(struct mvneta_softc *sc)
2562 {
2563 	int linkup;
2564 
2565 	KASSERT_SC_MTX(sc);
2566 
2567 	linkup = MVNETA_IS_LINKUP(sc) ? TRUE : FALSE;
2568 	if (sc->linkup == linkup)
2569 		return;
2570 
2571 	if (linkup == TRUE)
2572 		mvneta_linkup(sc);
2573 	else
2574 		mvneta_linkdown(sc);
2575 
2576 #ifdef DEBUG
2577 	log(LOG_DEBUG,
2578 	    "%s: link %s\n", device_xname(sc->dev), linkup ? "up" : "down");
2579 #endif
2580 }
2581 
2582 STATIC void
2583 mvneta_linkupdate(struct mvneta_softc *sc, boolean_t linkup)
2584 {
2585 
2586 	KASSERT_SC_MTX(sc);
2587 
2588 	if (linkup == TRUE)
2589 		mvneta_linkup(sc);
2590 	else
2591 		mvneta_linkdown(sc);
2592 
2593 #ifdef DEBUG
2594 	log(LOG_DEBUG,
2595 	    "%s: link %s\n", device_xname(sc->dev), linkup ? "up" : "down");
2596 #endif
2597 }
2598 
2599 STATIC void
2600 mvneta_update_eee(struct mvneta_softc *sc)
2601 {
2602 	uint32_t reg;
2603 
2604 	KASSERT_SC_MTX(sc);
2605 
2606 	/* set EEE parameters */
2607 	reg = MVNETA_READ(sc, MVNETA_LPIC1);
2608 	if (sc->cf_lpi)
2609 		reg |= MVNETA_LPIC1_LPIRE;
2610 	else
2611 		reg &= ~MVNETA_LPIC1_LPIRE;
2612 	MVNETA_WRITE(sc, MVNETA_LPIC1, reg);
2613 }
2614 
2615 STATIC void
2616 mvneta_update_fc(struct mvneta_softc *sc)
2617 {
2618 	uint32_t reg;
2619 
2620 	KASSERT_SC_MTX(sc);
2621 
2622 	reg  = MVNETA_READ(sc, MVNETA_PANC);
2623 	if (sc->cf_fc) {
2624 		/* Flow control negotiation */
2625 		reg |= MVNETA_PANC_PAUSEADV;
2626 		reg |= MVNETA_PANC_ANFCEN;
2627 	} else {
2628 		/* Disable flow control negotiation */
2629 		reg &= ~MVNETA_PANC_PAUSEADV;
2630 		reg &= ~MVNETA_PANC_ANFCEN;
2631 	}
2632 
2633 	MVNETA_WRITE(sc, MVNETA_PANC, reg);
2634 }
2635 
2636 STATIC void
2637 mvneta_linkup(struct mvneta_softc *sc)
2638 {
2639 	uint32_t reg;
2640 
2641 	KASSERT_SC_MTX(sc);
2642 
2643 	if (!sc->use_inband_status) {
2644 		reg  = MVNETA_READ(sc, MVNETA_PANC);
2645 		reg |= MVNETA_PANC_FORCELINKPASS;
2646 		reg &= ~MVNETA_PANC_FORCELINKFAIL;
2647 		MVNETA_WRITE(sc, MVNETA_PANC, reg);
2648 	}
2649 
2650 	mvneta_qflush(sc->ifp);
2651 	mvneta_portup(sc);
2652 	sc->linkup = TRUE;
2653 	if_link_state_change(sc->ifp, LINK_STATE_UP);
2654 }
2655 
2656 STATIC void
2657 mvneta_linkdown(struct mvneta_softc *sc)
2658 {
2659 	uint32_t reg;
2660 
2661 	KASSERT_SC_MTX(sc);
2662 
2663 	if (!sc->use_inband_status) {
2664 		reg  = MVNETA_READ(sc, MVNETA_PANC);
2665 		reg &= ~MVNETA_PANC_FORCELINKPASS;
2666 		reg |= MVNETA_PANC_FORCELINKFAIL;
2667 		MVNETA_WRITE(sc, MVNETA_PANC, reg);
2668 	}
2669 
2670 	mvneta_portdown(sc);
2671 	mvneta_qflush(sc->ifp);
2672 	sc->linkup = FALSE;
2673 	if_link_state_change(sc->ifp, LINK_STATE_DOWN);
2674 }
2675 
2676 STATIC void
2677 mvneta_linkreset(struct mvneta_softc *sc)
2678 {
2679 	struct mii_softc *mii;
2680 
2681 	if (sc->phy_attached) {
2682 		/* Force reset PHY */
2683 		mii = LIST_FIRST(&sc->mii->mii_phys);
2684 		if (mii)
2685 			mii_phy_reset(mii);
2686 	}
2687 }
2688 
2689 /*
2690  * Tx Subroutines
2691  */
2692 STATIC int
2693 mvneta_tx_queue(struct mvneta_softc *sc, struct mbuf **mbufp, int q)
2694 {
2695 	struct ifnet *ifp;
2696 	bus_dma_segment_t txsegs[MVNETA_TX_SEGLIMIT];
2697 	struct mbuf *mtmp, *mbuf;
2698 	struct mvneta_tx_ring *tx;
2699 	struct mvneta_buf *txbuf;
2700 	struct mvneta_tx_desc *t;
2701 	uint32_t ptxsu;
2702 	int start, used, error, i, txnsegs;
2703 
2704 	mbuf = *mbufp;
2705 	tx = MVNETA_TX_RING(sc, q);
2706 	DASSERT(tx->used >= 0);
2707 	DASSERT(tx->used <= MVNETA_TX_RING_CNT);
2708 	t = NULL;
2709 	ifp = sc->ifp;
2710 
2711 	if (__predict_false(mbuf->m_flags & M_VLANTAG)) {
2712 		mbuf = ether_vlanencap(mbuf, mbuf->m_pkthdr.ether_vtag);
2713 		if (mbuf == NULL) {
2714 			tx->drv_error++;
2715 			*mbufp = NULL;
2716 			return (ENOBUFS);
2717 		}
2718 		mbuf->m_flags &= ~M_VLANTAG;
2719 		*mbufp = mbuf;
2720 	}
2721 
2722 	if (__predict_false(mbuf->m_next != NULL &&
2723 	    (mbuf->m_pkthdr.csum_flags &
2724 	    (CSUM_IP | CSUM_TCP | CSUM_UDP)) != 0)) {
2725 		if (M_WRITABLE(mbuf) == 0) {
2726 			mtmp = m_dup(mbuf, M_NOWAIT);
2727 			m_freem(mbuf);
2728 			if (mtmp == NULL) {
2729 				tx->drv_error++;
2730 				*mbufp = NULL;
2731 				return (ENOBUFS);
2732 			}
2733 			*mbufp = mbuf = mtmp;
2734 		}
2735 	}
2736 
2737 	/* load mbuf using dmamap of 1st descriptor */
2738 	txbuf = &tx->txbuf[tx->cpu];
2739 	error = bus_dmamap_load_mbuf_sg(sc->txmbuf_dtag,
2740 	    txbuf->dmap, mbuf, txsegs, &txnsegs,
2741 	    BUS_DMA_NOWAIT);
2742 	if (__predict_false(error != 0)) {
2743 #ifdef MVNETA_KTR
2744 		CTR3(KTR_SPARE2, "%s:%u bus_dmamap_load_mbuf_sg error=%d", ifp->if_xname, q, error);
2745 #endif
2746 		/* This is the only recoverable error (except EFBIG). */
2747 		if (error != ENOMEM) {
2748 			tx->drv_error++;
2749 			m_freem(mbuf);
2750 			*mbufp = NULL;
2751 			return (ENOBUFS);
2752 		}
2753 		return (error);
2754 	}
2755 
2756 	if (__predict_false(txnsegs <= 0
2757 	    || (txnsegs + tx->used) > MVNETA_TX_RING_CNT)) {
2758 		/* we have no enough descriptors or mbuf is broken */
2759 #ifdef MVNETA_KTR
2760 		CTR3(KTR_SPARE2, "%s:%u not enough descriptors txnsegs=%d",
2761 		    ifp->if_xname, q, txnsegs);
2762 #endif
2763 		bus_dmamap_unload(sc->txmbuf_dtag, txbuf->dmap);
2764 		return (ENOBUFS);
2765 	}
2766 	DASSERT(txbuf->m == NULL);
2767 
2768 	/* remember mbuf using 1st descriptor */
2769 	txbuf->m = mbuf;
2770 	bus_dmamap_sync(sc->txmbuf_dtag, txbuf->dmap,
2771 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2772 
2773 	/* load to tx descriptors */
2774 	start = tx->cpu;
2775 	used = 0;
2776 	for (i = 0; i < txnsegs; i++) {
2777 		t = &tx->desc[tx->cpu];
2778 		t->command = 0;
2779 		t->l4ichk = 0;
2780 		t->flags = 0;
2781 		if (__predict_true(i == 0)) {
2782 			/* 1st descriptor */
2783 			t->command |= MVNETA_TX_CMD_W_PACKET_OFFSET(0);
2784 			t->command |= MVNETA_TX_CMD_F;
2785 			mvneta_tx_set_csumflag(ifp, t, mbuf);
2786 		}
2787 		t->bufptr_pa = txsegs[i].ds_addr;
2788 		t->bytecnt = txsegs[i].ds_len;
2789 		tx->cpu = tx_counter_adv(tx->cpu, 1);
2790 
2791 		tx->used++;
2792 		used++;
2793 	}
2794 	/* t is last descriptor here */
2795 	DASSERT(t != NULL);
2796 	t->command |= MVNETA_TX_CMD_L|MVNETA_TX_CMD_PADDING;
2797 
2798 	bus_dmamap_sync(sc->tx_dtag, tx->desc_map,
2799 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2800 
2801 	while (__predict_false(used > 255)) {
2802 		ptxsu = MVNETA_PTXSU_NOWD(255);
2803 		MVNETA_WRITE(sc, MVNETA_PTXSU(q), ptxsu);
2804 		used -= 255;
2805 	}
2806 	if (__predict_true(used > 0)) {
2807 		ptxsu = MVNETA_PTXSU_NOWD(used);
2808 		MVNETA_WRITE(sc, MVNETA_PTXSU(q), ptxsu);
2809 	}
2810 	return (0);
2811 }
2812 
2813 STATIC void
2814 mvneta_tx_set_csumflag(struct ifnet *ifp,
2815     struct mvneta_tx_desc *t, struct mbuf *m)
2816 {
2817 	struct ether_header *eh;
2818 	int csum_flags;
2819 	uint32_t iphl, ipoff;
2820 	struct ip *ip;
2821 
2822 	iphl = ipoff = 0;
2823 	csum_flags = ifp->if_hwassist & m->m_pkthdr.csum_flags;
2824 	eh = mtod(m, struct ether_header *);
2825 
2826 	switch (ntohs(eh->ether_type)) {
2827 	case ETHERTYPE_IP:
2828 		ipoff = ETHER_HDR_LEN;
2829 		break;
2830 	case ETHERTYPE_VLAN:
2831 		ipoff = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
2832 		break;
2833 	default:
2834 		csum_flags = 0;
2835 	}
2836 
2837 	if (__predict_true(csum_flags & (CSUM_IP|CSUM_IP_TCP|CSUM_IP_UDP))) {
2838 		ip = (struct ip *)(m->m_data + ipoff);
2839 		iphl = ip->ip_hl<<2;
2840 		t->command |= MVNETA_TX_CMD_L3_IP4;
2841 	} else {
2842 		t->command |= MVNETA_TX_CMD_L4_CHECKSUM_NONE;
2843 		return;
2844 	}
2845 
2846 
2847 	/* L3 */
2848 	if (csum_flags & CSUM_IP) {
2849 		t->command |= MVNETA_TX_CMD_IP4_CHECKSUM;
2850 	}
2851 
2852 	/* L4 */
2853 	if (csum_flags & CSUM_IP_TCP) {
2854 		t->command |= MVNETA_TX_CMD_L4_CHECKSUM_NOFRAG;
2855 		t->command |= MVNETA_TX_CMD_L4_TCP;
2856 	} else if (csum_flags & CSUM_IP_UDP) {
2857 		t->command |= MVNETA_TX_CMD_L4_CHECKSUM_NOFRAG;
2858 		t->command |= MVNETA_TX_CMD_L4_UDP;
2859 	} else
2860 		t->command |= MVNETA_TX_CMD_L4_CHECKSUM_NONE;
2861 
2862 	t->l4ichk = 0;
2863 	t->command |= MVNETA_TX_CMD_IP_HEADER_LEN(iphl >> 2);
2864 	t->command |= MVNETA_TX_CMD_L3_OFFSET(ipoff);
2865 }
2866 
2867 STATIC void
2868 mvneta_tx_queue_complete(struct mvneta_softc *sc, int q)
2869 {
2870 	struct mvneta_tx_ring *tx;
2871 	struct mvneta_buf *txbuf;
2872 	struct mvneta_tx_desc *t;
2873 	uint32_t ptxs, ptxsu, ndesc;
2874 	int i;
2875 
2876 	KASSERT_TX_MTX(sc, q);
2877 
2878 	tx = MVNETA_TX_RING(sc, q);
2879 	if (__predict_false(tx->queue_status == MVNETA_QUEUE_DISABLED))
2880 		return;
2881 
2882 	ptxs = MVNETA_READ(sc, MVNETA_PTXS(q));
2883 	ndesc = MVNETA_PTXS_GET_TBC(ptxs);
2884 
2885 	if (__predict_false(ndesc == 0)) {
2886 		if (tx->used == 0)
2887 			tx->queue_status = MVNETA_QUEUE_IDLE;
2888 		else if (tx->queue_status == MVNETA_QUEUE_WORKING &&
2889 		    ((ticks - tx->watchdog_time) > MVNETA_WATCHDOG))
2890 			tx->queue_hung = TRUE;
2891 		return;
2892 	}
2893 
2894 #ifdef MVNETA_KTR
2895 	CTR3(KTR_SPARE2, "%s:%u tx_complete begin ndesc=%u",
2896 	    sc->ifp->if_xname, q, ndesc);
2897 #endif
2898 
2899 	bus_dmamap_sync(sc->tx_dtag, tx->desc_map,
2900 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2901 
2902 	for (i = 0; i < ndesc; i++) {
2903 		t = &tx->desc[tx->dma];
2904 #ifdef MVNETA_KTR
2905 		if (t->flags & MVNETA_TX_F_ES)
2906 			CTR3(KTR_SPARE2, "%s tx error queue %d desc %d",
2907 			    sc->ifp->if_xname, q, tx->dma);
2908 #endif
2909 		txbuf = &tx->txbuf[tx->dma];
2910 		if (__predict_true(txbuf->m != NULL)) {
2911 			DASSERT((t->command & MVNETA_TX_CMD_F) != 0);
2912 			bus_dmamap_unload(sc->txmbuf_dtag, txbuf->dmap);
2913 			m_freem(txbuf->m);
2914 			txbuf->m = NULL;
2915 		}
2916 		else
2917 			DASSERT((t->flags & MVNETA_TX_CMD_F) == 0);
2918 		tx->dma = tx_counter_adv(tx->dma, 1);
2919 		tx->used--;
2920 	}
2921 	DASSERT(tx->used >= 0);
2922 	DASSERT(tx->used <= MVNETA_TX_RING_CNT);
2923 	while (__predict_false(ndesc > 255)) {
2924 		ptxsu = MVNETA_PTXSU_NORB(255);
2925 		MVNETA_WRITE(sc, MVNETA_PTXSU(q), ptxsu);
2926 		ndesc -= 255;
2927 	}
2928 	if (__predict_true(ndesc > 0)) {
2929 		ptxsu = MVNETA_PTXSU_NORB(ndesc);
2930 		MVNETA_WRITE(sc, MVNETA_PTXSU(q), ptxsu);
2931 	}
2932 #ifdef MVNETA_KTR
2933 	CTR5(KTR_SPARE2, "%s:%u tx_complete tx_cpu=%d tx_dma=%d tx_used=%d",
2934 	    sc->ifp->if_xname, q, tx->cpu, tx->dma, tx->used);
2935 #endif
2936 
2937 	tx->watchdog_time = ticks;
2938 
2939 	if (tx->used == 0)
2940 		tx->queue_status = MVNETA_QUEUE_IDLE;
2941 }
2942 
2943 /*
2944  * Do a final TX complete when TX is idle.
2945  */
2946 STATIC void
2947 mvneta_tx_drain(struct mvneta_softc *sc)
2948 {
2949 	struct mvneta_tx_ring *tx;
2950 	int q;
2951 
2952 	/*
2953 	 * Handle trailing mbuf on TX queue.
2954 	 * Check is done lockess to avoid TX path contention.
2955 	 */
2956 	for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) {
2957 		tx = MVNETA_TX_RING(sc, q);
2958 		if ((ticks - tx->watchdog_time) > MVNETA_WATCHDOG_TXCOMP &&
2959 		    tx->used > 0) {
2960 			mvneta_tx_lockq(sc, q);
2961 			mvneta_tx_queue_complete(sc, q);
2962 			mvneta_tx_unlockq(sc, q);
2963 		}
2964 	}
2965 }
2966 
2967 /*
2968  * Rx Subroutines
2969  */
2970 STATIC int
2971 mvneta_rx(struct mvneta_softc *sc, int q, int count)
2972 {
2973 	uint32_t prxs, npkt;
2974 	int more;
2975 
2976 	more = 0;
2977 	mvneta_rx_lockq(sc, q);
2978 	prxs = MVNETA_READ(sc, MVNETA_PRXS(q));
2979 	npkt = MVNETA_PRXS_GET_ODC(prxs);
2980 	if (__predict_false(npkt == 0))
2981 		goto out;
2982 
2983 	if (count > 0 && npkt > count) {
2984 		more = 1;
2985 		npkt = count;
2986 	}
2987 	mvneta_rx_queue(sc, q, npkt);
2988 out:
2989 	mvneta_rx_unlockq(sc, q);
2990 	return more;
2991 }
2992 
2993 /*
2994  * Helper routine for updating PRXSU register of a given queue.
2995  * Handles number of processed descriptors bigger than maximum acceptable value.
2996  */
2997 STATIC __inline void
2998 mvneta_prxsu_update(struct mvneta_softc *sc, int q, int processed)
2999 {
3000 	uint32_t prxsu;
3001 
3002 	while (__predict_false(processed > 255)) {
3003 		prxsu = MVNETA_PRXSU_NOOFPROCESSEDDESCRIPTORS(255);
3004 		MVNETA_WRITE(sc, MVNETA_PRXSU(q), prxsu);
3005 		processed -= 255;
3006 	}
3007 	prxsu = MVNETA_PRXSU_NOOFPROCESSEDDESCRIPTORS(processed);
3008 	MVNETA_WRITE(sc, MVNETA_PRXSU(q), prxsu);
3009 }
3010 
3011 static __inline void
3012 mvneta_prefetch(void *p)
3013 {
3014 
3015 	__builtin_prefetch(p);
3016 }
3017 
3018 STATIC void
3019 mvneta_rx_queue(struct mvneta_softc *sc, int q, int npkt)
3020 {
3021 	struct ifnet *ifp;
3022 	struct mvneta_rx_ring *rx;
3023 	struct mvneta_rx_desc *r;
3024 	struct mvneta_buf *rxbuf;
3025 	struct mbuf *m;
3026 	struct lro_ctrl *lro;
3027 	struct lro_entry *queued;
3028 	void *pktbuf;
3029 	int i, pktlen, processed, ndma;
3030 
3031 	KASSERT_RX_MTX(sc, q);
3032 
3033 	ifp = sc->ifp;
3034 	rx = MVNETA_RX_RING(sc, q);
3035 	processed = 0;
3036 
3037 	if (__predict_false(rx->queue_status == MVNETA_QUEUE_DISABLED))
3038 		return;
3039 
3040 	bus_dmamap_sync(sc->rx_dtag, rx->desc_map,
3041 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3042 
3043 	for (i = 0; i < npkt; i++) {
3044 		/* Prefetch next desc, rxbuf. */
3045 		ndma = rx_counter_adv(rx->dma, 1);
3046 		mvneta_prefetch(&rx->desc[ndma]);
3047 		mvneta_prefetch(&rx->rxbuf[ndma]);
3048 
3049 		/* get descriptor and packet */
3050 		r = &rx->desc[rx->dma];
3051 		rxbuf = &rx->rxbuf[rx->dma];
3052 		m = rxbuf->m;
3053 		rxbuf->m = NULL;
3054 		DASSERT(m != NULL);
3055 		bus_dmamap_sync(sc->rxbuf_dtag, rxbuf->dmap,
3056 		    BUS_DMASYNC_POSTREAD);
3057 		bus_dmamap_unload(sc->rxbuf_dtag, rxbuf->dmap);
3058 		/* Prefetch mbuf header. */
3059 		mvneta_prefetch(m);
3060 
3061 		processed++;
3062 		/* Drop desc with error status or not in a single buffer. */
3063 		DASSERT((r->status & (MVNETA_RX_F|MVNETA_RX_L)) ==
3064 		    (MVNETA_RX_F|MVNETA_RX_L));
3065 		if (__predict_false((r->status & MVNETA_RX_ES) ||
3066 		    (r->status & (MVNETA_RX_F|MVNETA_RX_L)) !=
3067 		    (MVNETA_RX_F|MVNETA_RX_L)))
3068 			goto rx_error;
3069 
3070 		/*
3071 		 * [ OFF | MH | PKT | CRC ]
3072 		 * bytecnt cover MH, PKT, CRC
3073 		 */
3074 		pktlen = r->bytecnt - ETHER_CRC_LEN - MVNETA_HWHEADER_SIZE;
3075 		pktbuf = (uint8_t *)rx->rxbuf_virt_addr[rx->dma] + MVNETA_PACKET_OFFSET +
3076                     MVNETA_HWHEADER_SIZE;
3077 
3078 		/* Prefetch mbuf data. */
3079 		mvneta_prefetch(pktbuf);
3080 
3081 		/* Write value to mbuf (avoid read). */
3082 		m->m_data = pktbuf;
3083 		m->m_len = m->m_pkthdr.len = pktlen;
3084 		m->m_pkthdr.rcvif = ifp;
3085 		mvneta_rx_set_csumflag(ifp, r, m);
3086 
3087 		/* Increase rx_dma before releasing the lock. */
3088 		rx->dma = ndma;
3089 
3090 		if (__predict_false(rx->lro_enabled &&
3091 		    ((r->status & MVNETA_RX_L3_IP) != 0) &&
3092 		    ((r->status & MVNETA_RX_L4_MASK) == MVNETA_RX_L4_TCP) &&
3093 		    (m->m_pkthdr.csum_flags &
3094 		    (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) ==
3095 		    (CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) {
3096 			if (rx->lro.lro_cnt != 0) {
3097 				if (tcp_lro_rx(&rx->lro, m, 0) == 0)
3098 					goto rx_done;
3099 			}
3100 		}
3101 
3102 		mvneta_rx_unlockq(sc, q);
3103 		(*ifp->if_input)(ifp, m);
3104 		mvneta_rx_lockq(sc, q);
3105 		/*
3106 		 * Check whether this queue has been disabled in the
3107 		 * meantime. If yes, then clear LRO and exit.
3108 		 */
3109 		if(__predict_false(rx->queue_status == MVNETA_QUEUE_DISABLED))
3110 			goto rx_lro;
3111 rx_done:
3112 		/* Refresh receive ring to avoid stall and minimize jitter. */
3113 		if (processed >= MVNETA_RX_REFILL_COUNT) {
3114 			mvneta_prxsu_update(sc, q, processed);
3115 			mvneta_rx_queue_refill(sc, q);
3116 			processed = 0;
3117 		}
3118 		continue;
3119 rx_error:
3120 		m_freem(m);
3121 		rx->dma = ndma;
3122 		/* Refresh receive ring to avoid stall and minimize jitter. */
3123 		if (processed >= MVNETA_RX_REFILL_COUNT) {
3124 			mvneta_prxsu_update(sc, q, processed);
3125 			mvneta_rx_queue_refill(sc, q);
3126 			processed = 0;
3127 		}
3128 	}
3129 #ifdef MVNETA_KTR
3130 	CTR3(KTR_SPARE2, "%s:%u %u packets received", ifp->if_xname, q, npkt);
3131 #endif
3132 	/* DMA status update */
3133 	mvneta_prxsu_update(sc, q, processed);
3134 	/* Refill the rest of buffers if there are any to refill */
3135 	mvneta_rx_queue_refill(sc, q);
3136 
3137 rx_lro:
3138 	/*
3139 	 * Flush any outstanding LRO work
3140 	 */
3141 	lro = &rx->lro;
3142 	while (__predict_false((queued = LIST_FIRST(&lro->lro_active)) != NULL)) {
3143 		LIST_REMOVE(LIST_FIRST((&lro->lro_active)), next);
3144 		tcp_lro_flush(lro, queued);
3145 	}
3146 }
3147 
3148 STATIC void
3149 mvneta_rx_buf_free(struct mvneta_softc *sc, struct mvneta_buf *rxbuf)
3150 {
3151 
3152 	bus_dmamap_unload(sc->rxbuf_dtag, rxbuf->dmap);
3153 	/* This will remove all data at once */
3154 	m_freem(rxbuf->m);
3155 }
3156 
3157 STATIC void
3158 mvneta_rx_queue_refill(struct mvneta_softc *sc, int q)
3159 {
3160 	struct mvneta_rx_ring *rx;
3161 	struct mvneta_rx_desc *r;
3162 	struct mvneta_buf *rxbuf;
3163 	bus_dma_segment_t segs;
3164 	struct mbuf *m;
3165 	uint32_t prxs, prxsu, ndesc;
3166 	int npkt, refill, nsegs, error;
3167 
3168 	KASSERT_RX_MTX(sc, q);
3169 
3170 	rx = MVNETA_RX_RING(sc, q);
3171 	prxs = MVNETA_READ(sc, MVNETA_PRXS(q));
3172 	ndesc = MVNETA_PRXS_GET_NODC(prxs) + MVNETA_PRXS_GET_ODC(prxs);
3173 	refill = MVNETA_RX_RING_CNT - ndesc;
3174 #ifdef MVNETA_KTR
3175 	CTR3(KTR_SPARE2, "%s:%u refill %u packets", sc->ifp->if_xname, q,
3176 	    refill);
3177 #endif
3178 	if (__predict_false(refill <= 0))
3179 		return;
3180 
3181 	for (npkt = 0; npkt < refill; npkt++) {
3182 		rxbuf = &rx->rxbuf[rx->cpu];
3183 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, sc->rx_frame_size);
3184 		if (__predict_false(m == NULL)) {
3185 			error = ENOBUFS;
3186 			break;
3187 		}
3188 		m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
3189 
3190 		error = bus_dmamap_load_mbuf_sg(sc->rxbuf_dtag, rxbuf->dmap,
3191 		    m, &segs, &nsegs, BUS_DMA_NOWAIT);
3192 		if (__predict_false(error != 0 || nsegs != 1)) {
3193 			KASSERT(1, ("Failed to load Rx mbuf DMA map"));
3194 			m_freem(m);
3195 			break;
3196 		}
3197 
3198 		/* Add the packet to the ring */
3199 		rxbuf->m = m;
3200 		r = &rx->desc[rx->cpu];
3201 		r->bufptr_pa = segs.ds_addr;
3202 		rx->rxbuf_virt_addr[rx->cpu] = m->m_data;
3203 
3204 		rx->cpu = rx_counter_adv(rx->cpu, 1);
3205 	}
3206 	if (npkt == 0) {
3207 		if (refill == MVNETA_RX_RING_CNT)
3208 			rx->needs_refill = TRUE;
3209 		return;
3210 	}
3211 
3212 	rx->needs_refill = FALSE;
3213 	bus_dmamap_sync(sc->rx_dtag, rx->desc_map, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3214 
3215 	while (__predict_false(npkt > 255)) {
3216 		prxsu = MVNETA_PRXSU_NOOFNEWDESCRIPTORS(255);
3217 		MVNETA_WRITE(sc, MVNETA_PRXSU(q), prxsu);
3218 		npkt -= 255;
3219 	}
3220 	if (__predict_true(npkt > 0)) {
3221 		prxsu = MVNETA_PRXSU_NOOFNEWDESCRIPTORS(npkt);
3222 		MVNETA_WRITE(sc, MVNETA_PRXSU(q), prxsu);
3223 	}
3224 }
3225 
3226 STATIC __inline void
3227 mvneta_rx_set_csumflag(struct ifnet *ifp,
3228     struct mvneta_rx_desc *r, struct mbuf *m)
3229 {
3230 	uint32_t csum_flags;
3231 
3232 	csum_flags = 0;
3233 	if (__predict_false((r->status &
3234 	    (MVNETA_RX_IP_HEADER_OK|MVNETA_RX_L3_IP)) == 0))
3235 		return; /* not a IP packet */
3236 
3237 	/* L3 */
3238 	if (__predict_true((r->status & MVNETA_RX_IP_HEADER_OK) ==
3239 	    MVNETA_RX_IP_HEADER_OK))
3240 		csum_flags |= CSUM_L3_CALC|CSUM_L3_VALID;
3241 
3242 	if (__predict_true((r->status & (MVNETA_RX_IP_HEADER_OK|MVNETA_RX_L3_IP)) ==
3243 	    (MVNETA_RX_IP_HEADER_OK|MVNETA_RX_L3_IP))) {
3244 		/* L4 */
3245 		switch (r->status & MVNETA_RX_L4_MASK) {
3246 		case MVNETA_RX_L4_TCP:
3247 		case MVNETA_RX_L4_UDP:
3248 			csum_flags |= CSUM_L4_CALC;
3249 			if (__predict_true((r->status &
3250 			    MVNETA_RX_L4_CHECKSUM_OK) == MVNETA_RX_L4_CHECKSUM_OK)) {
3251 				csum_flags |= CSUM_L4_VALID;
3252 				m->m_pkthdr.csum_data = htons(0xffff);
3253 			}
3254 			break;
3255 		case MVNETA_RX_L4_OTH:
3256 		default:
3257 			break;
3258 		}
3259 	}
3260 	m->m_pkthdr.csum_flags = csum_flags;
3261 }
3262 
3263 /*
3264  * MAC address filter
3265  */
3266 STATIC void
3267 mvneta_filter_setup(struct mvneta_softc *sc)
3268 {
3269 	struct ifnet *ifp;
3270 	uint32_t dfut[MVNETA_NDFUT], dfsmt[MVNETA_NDFSMT], dfomt[MVNETA_NDFOMT];
3271 	uint32_t pxc;
3272 	int i;
3273 
3274 	KASSERT_SC_MTX(sc);
3275 
3276 	memset(dfut, 0, sizeof(dfut));
3277 	memset(dfsmt, 0, sizeof(dfsmt));
3278 	memset(dfomt, 0, sizeof(dfomt));
3279 
3280 	ifp = sc->ifp;
3281 	ifp->if_flags |= IFF_ALLMULTI;
3282 	if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
3283 		for (i = 0; i < MVNETA_NDFSMT; i++) {
3284 			dfsmt[i] = dfomt[i] =
3285 			    MVNETA_DF(0, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) |
3286 			    MVNETA_DF(1, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) |
3287 			    MVNETA_DF(2, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) |
3288 			    MVNETA_DF(3, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS);
3289 		}
3290 	}
3291 
3292 	pxc = MVNETA_READ(sc, MVNETA_PXC);
3293 	pxc &= ~(MVNETA_PXC_UPM | MVNETA_PXC_RXQ_MASK | MVNETA_PXC_RXQARP_MASK |
3294 	    MVNETA_PXC_TCPQ_MASK | MVNETA_PXC_UDPQ_MASK | MVNETA_PXC_BPDUQ_MASK);
3295 	pxc |= MVNETA_PXC_RXQ(MVNETA_RX_QNUM_MAX-1);
3296 	pxc |= MVNETA_PXC_RXQARP(MVNETA_RX_QNUM_MAX-1);
3297 	pxc |= MVNETA_PXC_TCPQ(MVNETA_RX_QNUM_MAX-1);
3298 	pxc |= MVNETA_PXC_UDPQ(MVNETA_RX_QNUM_MAX-1);
3299 	pxc |= MVNETA_PXC_BPDUQ(MVNETA_RX_QNUM_MAX-1);
3300 	pxc |= MVNETA_PXC_RB | MVNETA_PXC_RBIP | MVNETA_PXC_RBARP;
3301 	if (ifp->if_flags & IFF_BROADCAST) {
3302 		pxc &= ~(MVNETA_PXC_RB | MVNETA_PXC_RBIP | MVNETA_PXC_RBARP);
3303 	}
3304 	if (ifp->if_flags & IFF_PROMISC) {
3305 		pxc |= MVNETA_PXC_UPM;
3306 	}
3307 	MVNETA_WRITE(sc, MVNETA_PXC, pxc);
3308 
3309 	/* Set Destination Address Filter Unicast Table */
3310 	if (ifp->if_flags & IFF_PROMISC) {
3311 		/* pass all unicast addresses */
3312 		for (i = 0; i < MVNETA_NDFUT; i++) {
3313 			dfut[i] =
3314 			    MVNETA_DF(0, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) |
3315 			    MVNETA_DF(1, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) |
3316 			    MVNETA_DF(2, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) |
3317 			    MVNETA_DF(3, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS);
3318 		}
3319 	} else {
3320 		i = sc->enaddr[5] & 0xf;		/* last nibble */
3321 		dfut[i>>2] = MVNETA_DF(i&3, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS);
3322 	}
3323 	MVNETA_WRITE_REGION(sc, MVNETA_DFUT(0), dfut, MVNETA_NDFUT);
3324 
3325 	/* Set Destination Address Filter Multicast Tables */
3326 	MVNETA_WRITE_REGION(sc, MVNETA_DFSMT(0), dfsmt, MVNETA_NDFSMT);
3327 	MVNETA_WRITE_REGION(sc, MVNETA_DFOMT(0), dfomt, MVNETA_NDFOMT);
3328 }
3329 
3330 /*
3331  * sysctl(9)
3332  */
3333 STATIC int
3334 sysctl_read_mib(SYSCTL_HANDLER_ARGS)
3335 {
3336 	struct mvneta_sysctl_mib *arg;
3337 	struct mvneta_softc *sc;
3338 	uint64_t val;
3339 
3340 	arg = (struct mvneta_sysctl_mib *)arg1;
3341 	if (arg == NULL)
3342 		return (EINVAL);
3343 
3344 	sc = arg->sc;
3345 	if (sc == NULL)
3346 		return (EINVAL);
3347 	if (arg->index < 0 || arg->index > MVNETA_PORTMIB_NOCOUNTER)
3348 		return (EINVAL);
3349 
3350 	mvneta_sc_lock(sc);
3351 	val = arg->counter;
3352 	mvneta_sc_unlock(sc);
3353 	return sysctl_handle_64(oidp, &val, 0, req);
3354 }
3355 
3356 
3357 STATIC int
3358 sysctl_clear_mib(SYSCTL_HANDLER_ARGS)
3359 {
3360 	struct mvneta_softc *sc;
3361 	int err, val;
3362 
3363 	val = 0;
3364 	sc = (struct mvneta_softc *)arg1;
3365 	if (sc == NULL)
3366 		return (EINVAL);
3367 
3368 	err = sysctl_handle_int(oidp, &val, 0, req);
3369 	if (err != 0)
3370 		return (err);
3371 
3372 	if (val < 0 || val > 1)
3373 		return (EINVAL);
3374 
3375 	if (val == 1) {
3376 		mvneta_sc_lock(sc);
3377 		mvneta_clear_mib(sc);
3378 		mvneta_sc_unlock(sc);
3379 	}
3380 
3381 	return (0);
3382 }
3383 
3384 STATIC int
3385 sysctl_set_queue_rxthtime(SYSCTL_HANDLER_ARGS)
3386 {
3387 	struct mvneta_sysctl_queue *arg;
3388 	struct mvneta_rx_ring *rx;
3389 	struct mvneta_softc *sc;
3390 	uint32_t reg, time_mvtclk;
3391 	int err, time_us;
3392 
3393 	rx = NULL;
3394 	arg = (struct mvneta_sysctl_queue *)arg1;
3395 	if (arg == NULL)
3396 		return (EINVAL);
3397 	if (arg->queue < 0 || arg->queue > MVNETA_RX_RING_CNT)
3398 		return (EINVAL);
3399 	if (arg->rxtx != MVNETA_SYSCTL_RX)
3400 		return (EINVAL);
3401 
3402 	sc = arg->sc;
3403 	if (sc == NULL)
3404 		return (EINVAL);
3405 
3406 	/* read queue length */
3407 	mvneta_sc_lock(sc);
3408 	mvneta_rx_lockq(sc, arg->queue);
3409 	rx = MVNETA_RX_RING(sc, arg->queue);
3410 	time_mvtclk = rx->queue_th_time;
3411 	time_us = ((uint64_t)time_mvtclk * 1000ULL * 1000ULL) / mvneta_get_clk();
3412 	mvneta_rx_unlockq(sc, arg->queue);
3413 	mvneta_sc_unlock(sc);
3414 
3415 	err = sysctl_handle_int(oidp, &time_us, 0, req);
3416 	if (err != 0)
3417 		return (err);
3418 
3419 	mvneta_sc_lock(sc);
3420 	mvneta_rx_lockq(sc, arg->queue);
3421 
3422 	/* update queue length (0[sec] - 1[sec]) */
3423 	if (time_us < 0 || time_us > (1000 * 1000)) {
3424 		mvneta_rx_unlockq(sc, arg->queue);
3425 		mvneta_sc_unlock(sc);
3426 		return (EINVAL);
3427 	}
3428 	time_mvtclk =
3429 	    (uint64_t)mvneta_get_clk() * (uint64_t)time_us / (1000ULL * 1000ULL);
3430 	rx->queue_th_time = time_mvtclk;
3431 	reg = MVNETA_PRXITTH_RITT(rx->queue_th_time);
3432 	MVNETA_WRITE(sc, MVNETA_PRXITTH(arg->queue), reg);
3433 	mvneta_rx_unlockq(sc, arg->queue);
3434 	mvneta_sc_unlock(sc);
3435 
3436 	return (0);
3437 }
3438 
3439 STATIC void
3440 sysctl_mvneta_init(struct mvneta_softc *sc)
3441 {
3442 	struct sysctl_ctx_list *ctx;
3443 	struct sysctl_oid_list *children;
3444 	struct sysctl_oid_list *rxchildren;
3445 	struct sysctl_oid_list *qchildren, *mchildren;
3446 	struct sysctl_oid *tree;
3447 	int i, q;
3448 	struct mvneta_sysctl_queue *rxarg;
3449 #define	MVNETA_SYSCTL_NAME(num) "queue" # num
3450 	static const char *sysctl_queue_names[] = {
3451 		MVNETA_SYSCTL_NAME(0), MVNETA_SYSCTL_NAME(1),
3452 		MVNETA_SYSCTL_NAME(2), MVNETA_SYSCTL_NAME(3),
3453 		MVNETA_SYSCTL_NAME(4), MVNETA_SYSCTL_NAME(5),
3454 		MVNETA_SYSCTL_NAME(6), MVNETA_SYSCTL_NAME(7),
3455 	};
3456 #undef MVNETA_SYSCTL_NAME
3457 
3458 #ifndef NO_SYSCTL_DESCR
3459 #define	MVNETA_SYSCTL_DESCR(num) "configuration parameters for queue " # num
3460 	static const char *sysctl_queue_descrs[] = {
3461 		MVNETA_SYSCTL_DESCR(0), MVNETA_SYSCTL_DESCR(1),
3462 		MVNETA_SYSCTL_DESCR(2), MVNETA_SYSCTL_DESCR(3),
3463 		MVNETA_SYSCTL_DESCR(4), MVNETA_SYSCTL_DESCR(5),
3464 		MVNETA_SYSCTL_DESCR(6), MVNETA_SYSCTL_DESCR(7),
3465 	};
3466 #undef MVNETA_SYSCTL_DESCR
3467 #endif
3468 
3469 
3470 	ctx = device_get_sysctl_ctx(sc->dev);
3471 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
3472 
3473 	tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rx",
3474 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "NETA RX");
3475 	rxchildren = SYSCTL_CHILDREN(tree);
3476 	tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "mib",
3477 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "NETA MIB");
3478 	mchildren = SYSCTL_CHILDREN(tree);
3479 
3480 
3481 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "flow_control",
3482 	    CTLFLAG_RW, &sc->cf_fc, 0, "flow control");
3483 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpi",
3484 	    CTLFLAG_RW, &sc->cf_lpi, 0, "Low Power Idle");
3485 
3486 	/*
3487 	 * MIB access
3488 	 */
3489 	/* dev.mvneta.[unit].mib.<mibs> */
3490 	for (i = 0; i < MVNETA_PORTMIB_NOCOUNTER; i++) {
3491 		struct mvneta_sysctl_mib *mib_arg = &sc->sysctl_mib[i];
3492 
3493 		mib_arg->sc = sc;
3494 		mib_arg->index = i;
3495 		SYSCTL_ADD_PROC(ctx, mchildren, OID_AUTO,
3496 		    mvneta_mib_list[i].sysctl_name,
3497 		    CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
3498 		    (void *)mib_arg, 0, sysctl_read_mib, "I",
3499 		    mvneta_mib_list[i].desc);
3500 	}
3501 	SYSCTL_ADD_UQUAD(ctx, mchildren, OID_AUTO, "rx_discard",
3502 	    CTLFLAG_RD, &sc->counter_pdfc, "Port Rx Discard Frame Counter");
3503 	SYSCTL_ADD_UQUAD(ctx, mchildren, OID_AUTO, "overrun",
3504 	    CTLFLAG_RD, &sc->counter_pofc, "Port Overrun Frame Counter");
3505 	SYSCTL_ADD_UINT(ctx, mchildren, OID_AUTO, "watchdog",
3506 	    CTLFLAG_RD, &sc->counter_watchdog, 0, "TX Watchdog Counter");
3507 
3508 	SYSCTL_ADD_PROC(ctx, mchildren, OID_AUTO, "reset",
3509 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
3510 	    (void *)sc, 0, sysctl_clear_mib, "I", "Reset MIB counters");
3511 
3512 	for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) {
3513 		rxarg = &sc->sysctl_rx_queue[q];
3514 
3515 		rxarg->sc = sc;
3516 		rxarg->queue = q;
3517 		rxarg->rxtx = MVNETA_SYSCTL_RX;
3518 
3519 		/* hw.mvneta.mvneta[unit].rx.[queue] */
3520 		tree = SYSCTL_ADD_NODE(ctx, rxchildren, OID_AUTO,
3521 		    sysctl_queue_names[q], CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3522 		    sysctl_queue_descrs[q]);
3523 		qchildren = SYSCTL_CHILDREN(tree);
3524 
3525 		/* hw.mvneta.mvneta[unit].rx.[queue].threshold_timer_us */
3526 		SYSCTL_ADD_PROC(ctx, qchildren, OID_AUTO, "threshold_timer_us",
3527 		    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, rxarg, 0,
3528 		    sysctl_set_queue_rxthtime, "I",
3529 		    "interrupt coalescing threshold timer [us]");
3530 	}
3531 }
3532 
3533 /*
3534  * MIB
3535  */
3536 STATIC uint64_t
3537 mvneta_read_mib(struct mvneta_softc *sc, int index)
3538 {
3539 	struct mvneta_mib_def *mib;
3540 	uint64_t val;
3541 
3542 	mib = &mvneta_mib_list[index];
3543 	val = MVNETA_READ_MIB(sc, mib->regnum);
3544 	if (mib->reg64)
3545 		val |= (uint64_t)MVNETA_READ_MIB(sc, mib->regnum + 4) << 32;
3546 	return (val);
3547 }
3548 
3549 STATIC void
3550 mvneta_clear_mib(struct mvneta_softc *sc)
3551 {
3552 	int i;
3553 
3554 	KASSERT_SC_MTX(sc);
3555 
3556 	for (i = 0; i < nitems(mvneta_mib_list); i++) {
3557 		(void)mvneta_read_mib(sc, i);
3558 		sc->sysctl_mib[i].counter = 0;
3559 	}
3560 	MVNETA_READ(sc, MVNETA_PDFC);
3561 	sc->counter_pdfc = 0;
3562 	MVNETA_READ(sc, MVNETA_POFC);
3563 	sc->counter_pofc = 0;
3564 	sc->counter_watchdog = 0;
3565 }
3566 
3567 STATIC void
3568 mvneta_update_mib(struct mvneta_softc *sc)
3569 {
3570 	struct mvneta_tx_ring *tx;
3571 	int i;
3572 	uint64_t val;
3573 	uint32_t reg;
3574 
3575 	for (i = 0; i < nitems(mvneta_mib_list); i++) {
3576 
3577 		val = mvneta_read_mib(sc, i);
3578 		if (val == 0)
3579 			continue;
3580 
3581 		sc->sysctl_mib[i].counter += val;
3582 		switch (mvneta_mib_list[i].regnum) {
3583 			case MVNETA_MIB_RX_GOOD_OCT:
3584 				if_inc_counter(sc->ifp, IFCOUNTER_IBYTES, val);
3585 				break;
3586 			case MVNETA_MIB_RX_BAD_FRAME:
3587 				if_inc_counter(sc->ifp, IFCOUNTER_IERRORS, val);
3588 				break;
3589 			case MVNETA_MIB_RX_GOOD_FRAME:
3590 				if_inc_counter(sc->ifp, IFCOUNTER_IPACKETS, val);
3591 				break;
3592 			case MVNETA_MIB_RX_MCAST_FRAME:
3593 				if_inc_counter(sc->ifp, IFCOUNTER_IMCASTS, val);
3594 				break;
3595 			case MVNETA_MIB_TX_GOOD_OCT:
3596 				if_inc_counter(sc->ifp, IFCOUNTER_OBYTES, val);
3597 				break;
3598 			case MVNETA_MIB_TX_GOOD_FRAME:
3599 				if_inc_counter(sc->ifp, IFCOUNTER_OPACKETS, val);
3600 				break;
3601 			case MVNETA_MIB_TX_MCAST_FRAME:
3602 				if_inc_counter(sc->ifp, IFCOUNTER_OMCASTS, val);
3603 				break;
3604 			case MVNETA_MIB_MAC_COL:
3605 				if_inc_counter(sc->ifp, IFCOUNTER_COLLISIONS, val);
3606 				break;
3607 			case MVNETA_MIB_TX_MAC_TRNS_ERR:
3608 			case MVNETA_MIB_TX_EXCES_COL:
3609 			case MVNETA_MIB_MAC_LATE_COL:
3610 				if_inc_counter(sc->ifp, IFCOUNTER_OERRORS, val);
3611 				break;
3612 		}
3613 	}
3614 
3615 	reg = MVNETA_READ(sc, MVNETA_PDFC);
3616 	sc->counter_pdfc += reg;
3617 	if_inc_counter(sc->ifp, IFCOUNTER_IQDROPS, reg);
3618 	reg = MVNETA_READ(sc, MVNETA_POFC);
3619 	sc->counter_pofc += reg;
3620 	if_inc_counter(sc->ifp, IFCOUNTER_IQDROPS, reg);
3621 
3622 	/* TX watchdog. */
3623 	if (sc->counter_watchdog_mib > 0) {
3624 		if_inc_counter(sc->ifp, IFCOUNTER_OERRORS, sc->counter_watchdog_mib);
3625 		sc->counter_watchdog_mib = 0;
3626 	}
3627 	/*
3628 	 * TX driver errors:
3629 	 * We do not take queue locks to not disrupt TX path.
3630 	 * We may only miss one drv error which will be fixed at
3631 	 * next mib update. We may also clear counter when TX path
3632 	 * is incrementing it but we only do it if counter was not zero
3633 	 * thus we may only loose one error.
3634 	 */
3635 	for (i = 0; i < MVNETA_TX_QNUM_MAX; i++) {
3636 		tx = MVNETA_TX_RING(sc, i);
3637 
3638 		if (tx->drv_error > 0) {
3639 			if_inc_counter(sc->ifp, IFCOUNTER_OERRORS, tx->drv_error);
3640 			tx->drv_error = 0;
3641 		}
3642 	}
3643 }
3644