xref: /freebsd/sys/dev/al_eth/al_eth.c (revision a3557ef0)
1 /*-
2  * Copyright (c) 2015,2016 Annapurna Labs Ltd. and affiliates
3  * All rights reserved.
4  *
5  * Developed by Semihalf.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/kthread.h>
37 #include <sys/lock.h>
38 #include <sys/mbuf.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/rman.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/sysctl.h>
45 #include <sys/taskqueue.h>
46 
47 #include <machine/atomic.h>
48 
49 #include "opt_inet.h"
50 #include "opt_inet6.h"
51 
52 #include <net/ethernet.h>
53 #include <net/if.h>
54 #include <net/if_var.h>
55 #include <net/if_arp.h>
56 #include <net/if_dl.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
59 #include <netinet/in.h>
60 #include <net/if_vlan_var.h>
61 #include <netinet/tcp.h>
62 #include <netinet/tcp_lro.h>
63 
64 #ifdef INET
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
69 #endif
70 
71 #ifdef INET6
72 #include <netinet/ip6.h>
73 #endif
74 
75 #include <sys/sockio.h>
76 
77 #include <dev/pci/pcireg.h>
78 #include <dev/pci/pcivar.h>
79 
80 #include <dev/mii/mii.h>
81 #include <dev/mii/miivar.h>
82 
83 #include <al_hal_common.h>
84 #include <al_hal_plat_services.h>
85 #include <al_hal_udma_config.h>
86 #include <al_hal_udma_iofic.h>
87 #include <al_hal_udma_debug.h>
88 #include <al_hal_eth.h>
89 
90 #include "al_eth.h"
91 #include "al_init_eth_lm.h"
92 #include "arm/annapurna/alpine/alpine_serdes.h"
93 
94 #include "miibus_if.h"
95 
96 #define	device_printf_dbg(fmt, ...) do {				\
97 	if (AL_DBG_LEVEL >= AL_DBG_LEVEL_DBG) { AL_DBG_LOCK();		\
98 	    device_printf(fmt, __VA_ARGS__); AL_DBG_UNLOCK();}		\
99 	} while (0)
100 
101 MALLOC_DEFINE(M_IFAL, "if_al_malloc", "All allocated data for AL ETH driver");
102 
103 /* move out to some pci header file */
104 #define	PCI_VENDOR_ID_ANNAPURNA_LABS	0x1c36
105 #define	PCI_DEVICE_ID_AL_ETH		0x0001
106 #define	PCI_DEVICE_ID_AL_ETH_ADVANCED	0x0002
107 #define	PCI_DEVICE_ID_AL_ETH_NIC	0x0003
108 #define	PCI_DEVICE_ID_AL_ETH_FPGA_NIC	0x0030
109 #define	PCI_DEVICE_ID_AL_CRYPTO		0x0011
110 #define	PCI_DEVICE_ID_AL_CRYPTO_VF	0x8011
111 #define	PCI_DEVICE_ID_AL_RAID_DMA	0x0021
112 #define	PCI_DEVICE_ID_AL_RAID_DMA_VF	0x8021
113 #define	PCI_DEVICE_ID_AL_USB		0x0041
114 
115 #define	MAC_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
116 #define	MAC_ADDR(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
117 
118 #define	AL_ETH_MAC_TABLE_UNICAST_IDX_BASE	0
119 #define	AL_ETH_MAC_TABLE_UNICAST_MAX_COUNT	4
120 #define	AL_ETH_MAC_TABLE_ALL_MULTICAST_IDX	(AL_ETH_MAC_TABLE_UNICAST_IDX_BASE + \
121 						 AL_ETH_MAC_TABLE_UNICAST_MAX_COUNT)
122 
123 #define	AL_ETH_MAC_TABLE_DROP_IDX		(AL_ETH_FWD_MAC_NUM - 1)
124 #define	AL_ETH_MAC_TABLE_BROADCAST_IDX		(AL_ETH_MAC_TABLE_DROP_IDX - 1)
125 
126 #define	AL_ETH_THASH_UDMA_SHIFT		0
127 #define	AL_ETH_THASH_UDMA_MASK		(0xF << AL_ETH_THASH_UDMA_SHIFT)
128 
129 #define	AL_ETH_THASH_Q_SHIFT		4
130 #define	AL_ETH_THASH_Q_MASK		(0x3 << AL_ETH_THASH_Q_SHIFT)
131 
132 /* the following defines should be moved to hal */
133 #define	AL_ETH_FSM_ENTRY_IPV4_TCP		0
134 #define	AL_ETH_FSM_ENTRY_IPV4_UDP		1
135 #define	AL_ETH_FSM_ENTRY_IPV6_TCP		2
136 #define	AL_ETH_FSM_ENTRY_IPV6_UDP		3
137 #define	AL_ETH_FSM_ENTRY_IPV6_NO_UDP_TCP	4
138 #define	AL_ETH_FSM_ENTRY_IPV4_NO_UDP_TCP	5
139 
140 /* FSM DATA format */
141 #define	AL_ETH_FSM_DATA_OUTER_2_TUPLE	0
142 #define	AL_ETH_FSM_DATA_OUTER_4_TUPLE	1
143 #define	AL_ETH_FSM_DATA_INNER_2_TUPLE	2
144 #define	AL_ETH_FSM_DATA_INNER_4_TUPLE	3
145 
146 #define	AL_ETH_FSM_DATA_HASH_SEL	(1 << 2)
147 
148 #define	AL_ETH_FSM_DATA_DEFAULT_Q	0
149 #define	AL_ETH_FSM_DATA_DEFAULT_UDMA	0
150 
151 #define	AL_BR_SIZE	512
152 #define	AL_TSO_SIZE	65500
153 #define	AL_DEFAULT_MTU	1500
154 
155 #define	CSUM_OFFLOAD		(CSUM_IP|CSUM_TCP|CSUM_UDP|CSUM_SCTP)
156 
157 #define	AL_IP_ALIGNMENT_OFFSET	2
158 
159 #define	SFP_I2C_ADDR		0x50
160 
161 #define	AL_MASK_GROUP_A_INT	0x7
162 #define	AL_MASK_GROUP_B_INT	0xF
163 #define	AL_MASK_GROUP_C_INT	0xF
164 #define	AL_MASK_GROUP_D_INT	0xFFFFFFFF
165 
166 #define	AL_REG_OFFSET_FORWARD_INTR	(0x1800000 + 0x1210)
167 #define	AL_EN_FORWARD_INTR	0x1FFFF
168 #define	AL_DIS_FORWARD_INTR	0
169 
170 #define	AL_M2S_MASK_INIT	0x480
171 #define	AL_S2M_MASK_INIT	0x1E0
172 #define	AL_M2S_S2M_MASK_NOT_INT	(0x3f << 25)
173 
174 #define	AL_10BASE_T_SPEED	10
175 #define	AL_100BASE_TX_SPEED	100
176 #define	AL_1000BASE_T_SPEED	1000
177 
178 static devclass_t al_devclass;
179 
180 #define	AL_RX_LOCK_INIT(_sc)	mtx_init(&((_sc)->if_rx_lock), "ALRXL", "ALRXL", MTX_DEF)
181 #define	AL_RX_LOCK(_sc)		mtx_lock(&((_sc)->if_rx_lock))
182 #define	AL_RX_UNLOCK(_sc)	mtx_unlock(&((_sc)->if_rx_lock))
183 
184 /* helper functions */
185 static int al_is_device_supported(device_t);
186 
187 static void al_eth_init_rings(struct al_eth_adapter *);
188 static void al_eth_flow_ctrl_disable(struct al_eth_adapter *);
189 int al_eth_fpga_read_pci_config(void *, int, uint32_t *);
190 int al_eth_fpga_write_pci_config(void *, int, uint32_t);
191 int al_eth_read_pci_config(void *, int, uint32_t *);
192 int al_eth_write_pci_config(void *, int, uint32_t);
193 void al_eth_irq_config(uint32_t *, uint32_t);
194 void al_eth_forward_int_config(uint32_t *, uint32_t);
195 static void al_eth_start_xmit(void *, int);
196 static void al_eth_rx_recv_work(void *, int);
197 static int al_eth_up(struct al_eth_adapter *);
198 static void al_eth_down(struct al_eth_adapter *);
199 static void al_eth_interrupts_unmask(struct al_eth_adapter *);
200 static void al_eth_interrupts_mask(struct al_eth_adapter *);
201 static int al_eth_check_mtu(struct al_eth_adapter *, int);
202 static uint64_t al_get_counter(struct ifnet *, ift_counter);
203 static void al_eth_req_rx_buff_size(struct al_eth_adapter *, int);
204 static int al_eth_board_params_init(struct al_eth_adapter *);
205 static int al_media_update(struct ifnet *);
206 static void al_media_status(struct ifnet *, struct ifmediareq *);
207 static int al_eth_function_reset(struct al_eth_adapter *);
208 static int al_eth_hw_init_adapter(struct al_eth_adapter *);
209 static void al_eth_serdes_init(struct al_eth_adapter *);
210 static void al_eth_lm_config(struct al_eth_adapter *);
211 static int al_eth_hw_init(struct al_eth_adapter *);
212 
213 static void al_tick_stats(void *);
214 
215 /* ifnet entry points */
216 static void al_init(void *);
217 static int al_mq_start(struct ifnet *, struct mbuf *);
218 static void al_qflush(struct ifnet *);
219 static int al_ioctl(struct ifnet * ifp, u_long, caddr_t);
220 
221 /* bus entry points */
222 static int al_probe(device_t);
223 static int al_attach(device_t);
224 static int al_detach(device_t);
225 static int al_shutdown(device_t);
226 
227 /* mii bus support routines */
228 static int al_miibus_readreg(device_t, int, int);
229 static int al_miibus_writereg(device_t, int, int, int);
230 static void al_miibus_statchg(device_t);
231 static void al_miibus_linkchg(device_t);
232 
233 struct al_eth_adapter* g_adapters[16];
234 uint32_t g_adapters_count;
235 
236 /* flag for napi-like mbuf processing, controlled from sysctl */
237 static int napi = 0;
238 
239 static device_method_t al_methods[] = {
240 	/* Device interface */
241 	DEVMETHOD(device_probe,		al_probe),
242 	DEVMETHOD(device_attach,	al_attach),
243 	DEVMETHOD(device_detach,	al_detach),
244 	DEVMETHOD(device_shutdown,	al_shutdown),
245 
246 	DEVMETHOD(miibus_readreg,	al_miibus_readreg),
247 	DEVMETHOD(miibus_writereg,	al_miibus_writereg),
248 	DEVMETHOD(miibus_statchg,	al_miibus_statchg),
249 	DEVMETHOD(miibus_linkchg,	al_miibus_linkchg),
250 	{ 0, 0 }
251 };
252 
253 static driver_t al_driver = {
254 	"al",
255 	al_methods,
256 	sizeof(struct al_eth_adapter),
257 };
258 
259 DRIVER_MODULE(al, pci, al_driver, al_devclass, 0, 0);
260 DRIVER_MODULE(miibus, al, miibus_driver, miibus_devclass, 0, 0);
261 
262 static int
263 al_probe(device_t dev)
264 {
265 	if ((al_is_device_supported(dev)) != 0) {
266 		device_set_desc(dev, "al");
267 		return (BUS_PROBE_DEFAULT);
268 	}
269 	return (ENXIO);
270 }
271 
272 static int
273 al_attach(device_t dev)
274 {
275 	struct al_eth_adapter *adapter;
276 	struct sysctl_oid_list *child;
277 	struct sysctl_ctx_list *ctx;
278 	struct sysctl_oid *tree;
279 	struct ifnet *ifp;
280 	uint32_t dev_id;
281 	uint32_t rev_id;
282 	int bar_udma;
283 	int bar_mac;
284 	int bar_ec;
285 	int err;
286 
287 	err = 0;
288 	ifp = NULL;
289 	dev_id = rev_id = 0;
290 	ctx = device_get_sysctl_ctx(dev);
291 	tree = SYSCTL_PARENT(device_get_sysctl_tree(dev));
292 	child = SYSCTL_CHILDREN(tree);
293 
294 	if (g_adapters_count == 0) {
295 		SYSCTL_ADD_INT(ctx, child, OID_AUTO, "napi",
296 		    CTLFLAG_RW, &napi, 0, "Use pseudo-napi mechanism");
297 	}
298 	adapter = device_get_softc(dev);
299 	adapter->dev = dev;
300 	adapter->board_type = ALPINE_INTEGRATED;
301 	snprintf(adapter->name, AL_ETH_NAME_MAX_LEN, "%s",
302 	    device_get_nameunit(dev));
303 	AL_RX_LOCK_INIT(adapter);
304 
305 	g_adapters[g_adapters_count] = adapter;
306 
307 	bar_udma = PCIR_BAR(AL_ETH_UDMA_BAR);
308 	adapter->udma_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
309 	    &bar_udma, RF_ACTIVE);
310 	if (adapter->udma_res == NULL) {
311 		device_printf(adapter->dev,
312 		    "could not allocate memory resources for DMA.\n");
313 		err = ENOMEM;
314 		goto err_res_dma;
315 	}
316 	adapter->udma_base = al_bus_dma_to_va(rman_get_bustag(adapter->udma_res),
317 	    rman_get_bushandle(adapter->udma_res));
318 	bar_mac = PCIR_BAR(AL_ETH_MAC_BAR);
319 	adapter->mac_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
320 	    &bar_mac, RF_ACTIVE);
321 	if (adapter->mac_res == NULL) {
322 		device_printf(adapter->dev,
323 		    "could not allocate memory resources for MAC.\n");
324 		err = ENOMEM;
325 		goto err_res_mac;
326 	}
327 	adapter->mac_base = al_bus_dma_to_va(rman_get_bustag(adapter->mac_res),
328 	    rman_get_bushandle(adapter->mac_res));
329 
330 	bar_ec = PCIR_BAR(AL_ETH_EC_BAR);
331 	adapter->ec_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &bar_ec,
332 	    RF_ACTIVE);
333 	if (adapter->ec_res == NULL) {
334 		device_printf(adapter->dev,
335 		    "could not allocate memory resources for EC.\n");
336 		err = ENOMEM;
337 		goto err_res_ec;
338 	}
339 	adapter->ec_base = al_bus_dma_to_va(rman_get_bustag(adapter->ec_res),
340 	    rman_get_bushandle(adapter->ec_res));
341 
342 	adapter->netdev = ifp = if_alloc(IFT_ETHER);
343 
344 	adapter->netdev->if_link_state = LINK_STATE_DOWN;
345 
346 	ifp->if_softc = adapter;
347 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
348 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
349 	ifp->if_flags = ifp->if_drv_flags;
350 	ifp->if_flags |= IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_ALLMULTI;
351 	ifp->if_transmit = al_mq_start;
352 	ifp->if_qflush = al_qflush;
353 	ifp->if_ioctl = al_ioctl;
354 	ifp->if_init = al_init;
355 	ifp->if_get_counter = al_get_counter;
356 	ifp->if_mtu = AL_DEFAULT_MTU;
357 
358 	adapter->if_flags = ifp->if_flags;
359 
360 	ifp->if_capabilities = ifp->if_capenable = 0;
361 
362 	ifp->if_capabilities |= IFCAP_HWCSUM |
363 	    IFCAP_HWCSUM_IPV6 | IFCAP_TSO |
364 	    IFCAP_LRO | IFCAP_JUMBO_MTU;
365 
366 	ifp->if_capenable = ifp->if_capabilities;
367 
368 	adapter->id_number = g_adapters_count;
369 
370 	if (adapter->board_type == ALPINE_INTEGRATED) {
371 		dev_id = pci_get_device(adapter->dev);
372 		rev_id = pci_get_revid(adapter->dev);
373 	} else {
374 		al_eth_fpga_read_pci_config(adapter->internal_pcie_base,
375 		    PCIR_DEVICE, &dev_id);
376 		al_eth_fpga_read_pci_config(adapter->internal_pcie_base,
377 		    PCIR_REVID, &rev_id);
378 	}
379 
380 	adapter->dev_id = dev_id;
381 	adapter->rev_id = rev_id;
382 
383 	/* set default ring sizes */
384 	adapter->tx_ring_count = AL_ETH_DEFAULT_TX_SW_DESCS;
385 	adapter->tx_descs_count = AL_ETH_DEFAULT_TX_HW_DESCS;
386 	adapter->rx_ring_count = AL_ETH_DEFAULT_RX_DESCS;
387 	adapter->rx_descs_count = AL_ETH_DEFAULT_RX_DESCS;
388 
389 	adapter->num_tx_queues = AL_ETH_NUM_QUEUES;
390 	adapter->num_rx_queues = AL_ETH_NUM_QUEUES;
391 
392 	adapter->small_copy_len	= AL_ETH_DEFAULT_SMALL_PACKET_LEN;
393 	adapter->link_poll_interval = AL_ETH_DEFAULT_LINK_POLL_INTERVAL;
394 	adapter->max_rx_buff_alloc_size = AL_ETH_DEFAULT_MAX_RX_BUFF_ALLOC_SIZE;
395 
396 	al_eth_req_rx_buff_size(adapter, adapter->netdev->if_mtu);
397 
398 	adapter->link_config.force_1000_base_x = AL_ETH_DEFAULT_FORCE_1000_BASEX;
399 
400 	err = al_eth_board_params_init(adapter);
401 	if (err != 0)
402 		goto err;
403 
404 	if (adapter->mac_mode == AL_ETH_MAC_MODE_10GbE_Serial) {
405 		ifmedia_init(&adapter->media, IFM_IMASK,
406 		    al_media_update, al_media_status);
407 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_LX, 0, NULL);
408 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_10G_LR, 0, NULL);
409 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
410 		ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
411 	}
412 
413 	al_eth_function_reset(adapter);
414 
415 	err = al_eth_hw_init_adapter(adapter);
416 	if (err != 0)
417 		goto err;
418 
419 	al_eth_init_rings(adapter);
420 	g_adapters_count++;
421 
422 	al_eth_lm_config(adapter);
423 	mtx_init(&adapter->stats_mtx, "AlStatsMtx", NULL, MTX_DEF);
424 	mtx_init(&adapter->wd_mtx, "AlWdMtx", NULL, MTX_DEF);
425 	callout_init_mtx(&adapter->stats_callout, &adapter->stats_mtx, 0);
426 	callout_init_mtx(&adapter->wd_callout, &adapter->wd_mtx, 0);
427 
428 	ether_ifattach(ifp, adapter->mac_addr);
429 	ifp->if_mtu = AL_DEFAULT_MTU;
430 
431 	if (adapter->mac_mode == AL_ETH_MAC_MODE_RGMII) {
432 		al_eth_hw_init(adapter);
433 
434 		/* Attach PHY(s) */
435 		err = mii_attach(adapter->dev, &adapter->miibus, adapter->netdev,
436 		    al_media_update, al_media_status, BMSR_DEFCAPMASK, 0,
437 		    MII_OFFSET_ANY, 0);
438 		if (err != 0) {
439 			device_printf(adapter->dev, "attaching PHYs failed\n");
440 			return (err);
441 		}
442 
443 		adapter->mii = device_get_softc(adapter->miibus);
444 	}
445 
446 	return (err);
447 
448 err:
449 	bus_release_resource(dev, SYS_RES_MEMORY, bar_ec, adapter->ec_res);
450 err_res_ec:
451 	bus_release_resource(dev, SYS_RES_MEMORY, bar_mac, adapter->mac_res);
452 err_res_mac:
453 	bus_release_resource(dev, SYS_RES_MEMORY, bar_udma, adapter->udma_res);
454 err_res_dma:
455 	return (err);
456 }
457 
458 static int
459 al_detach(device_t dev)
460 {
461 	struct al_eth_adapter *adapter;
462 
463 	adapter = device_get_softc(dev);
464 	ether_ifdetach(adapter->netdev);
465 
466 	mtx_destroy(&adapter->stats_mtx);
467 	mtx_destroy(&adapter->wd_mtx);
468 
469 	al_eth_down(adapter);
470 
471 	bus_release_resource(dev, SYS_RES_IRQ,    0, adapter->irq_res);
472 	bus_release_resource(dev, SYS_RES_MEMORY, 0, adapter->ec_res);
473 	bus_release_resource(dev, SYS_RES_MEMORY, 0, adapter->mac_res);
474 	bus_release_resource(dev, SYS_RES_MEMORY, 0, adapter->udma_res);
475 
476 	return (0);
477 }
478 
479 int
480 al_eth_fpga_read_pci_config(void *handle, int where, uint32_t *val)
481 {
482 
483 	/* handle is the base address of the adapter */
484 	*val = al_reg_read32((void*)((u_long)handle + where));
485 
486 	return (0);
487 }
488 
489 int
490 al_eth_fpga_write_pci_config(void *handle, int where, uint32_t val)
491 {
492 
493 	/* handle is the base address of the adapter */
494 	al_reg_write32((void*)((u_long)handle + where), val);
495 	return (0);
496 }
497 
498 int
499 al_eth_read_pci_config(void *handle, int where, uint32_t *val)
500 {
501 
502 	/* handle is a pci_dev */
503 	*val = pci_read_config((device_t)handle, where, sizeof(*val));
504 	return (0);
505 }
506 
507 int
508 al_eth_write_pci_config(void *handle, int where, uint32_t val)
509 {
510 
511 	/* handle is a pci_dev */
512 	pci_write_config((device_t)handle, where, val, sizeof(val));
513 	return (0);
514 }
515 
516 void
517 al_eth_irq_config(uint32_t *offset, uint32_t value)
518 {
519 
520 	al_reg_write32_relaxed(offset, value);
521 }
522 
523 void
524 al_eth_forward_int_config(uint32_t *offset, uint32_t value)
525 {
526 
527 	al_reg_write32(offset, value);
528 }
529 
530 static void
531 al_eth_serdes_init(struct al_eth_adapter *adapter)
532 {
533 	void __iomem	*serdes_base;
534 
535 	adapter->serdes_init = false;
536 
537 	serdes_base = alpine_serdes_resource_get(adapter->serdes_grp);
538 	if (serdes_base == NULL) {
539 		device_printf(adapter->dev, "serdes_base get failed!\n");
540 		return;
541 	}
542 
543 	serdes_base = al_bus_dma_to_va(serdes_tag, serdes_base);
544 
545 	al_serdes_handle_grp_init(serdes_base, adapter->serdes_grp,
546 	    &adapter->serdes_obj);
547 
548 	adapter->serdes_init = true;
549 }
550 
551 static void
552 al_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
553 {
554 	bus_addr_t *paddr;
555 
556 	paddr = arg;
557 	*paddr = segs->ds_addr;
558 }
559 
560 static int
561 al_dma_alloc_coherent(struct device *dev, bus_dma_tag_t *tag, bus_dmamap_t *map,
562     bus_addr_t *baddr, void **vaddr, uint32_t size)
563 {
564 	int ret;
565 	uint32_t maxsize = ((size - 1)/PAGE_SIZE + 1) * PAGE_SIZE;
566 
567 	ret = bus_dma_tag_create(bus_get_dma_tag(dev), 8, 0,
568 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
569 	    maxsize, 1, maxsize, BUS_DMA_COHERENT, NULL, NULL, tag);
570 	if (ret != 0) {
571 		device_printf(dev,
572 		    "failed to create bus tag, ret = %d\n", ret);
573 		return (ret);
574 	}
575 
576 	ret = bus_dmamem_alloc(*tag, vaddr,
577 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, map);
578 	if (ret != 0) {
579 		device_printf(dev,
580 		    "failed to allocate dmamem, ret = %d\n", ret);
581 		return (ret);
582 	}
583 
584 	ret = bus_dmamap_load(*tag, *map, *vaddr,
585 	    size, al_dma_map_addr, baddr, 0);
586 	if (ret != 0) {
587 		device_printf(dev,
588 		    "failed to allocate bus_dmamap_load, ret = %d\n", ret);
589 		return (ret);
590 	}
591 
592 	return (0);
593 }
594 
595 static void
596 al_dma_free_coherent(bus_dma_tag_t tag, bus_dmamap_t map, void *vaddr)
597 {
598 
599 	bus_dmamap_unload(tag, map);
600 	bus_dmamem_free(tag, vaddr, map);
601 	bus_dma_tag_destroy(tag);
602 }
603 
604 static void
605 al_eth_mac_table_unicast_add(struct al_eth_adapter *adapter,
606     uint8_t idx, uint8_t udma_mask)
607 {
608 	struct al_eth_fwd_mac_table_entry entry = { { 0 } };
609 
610 	memcpy(entry.addr, adapter->mac_addr, sizeof(adapter->mac_addr));
611 
612 	memset(entry.mask, 0xff, sizeof(entry.mask));
613 	entry.rx_valid = true;
614 	entry.tx_valid = false;
615 	entry.udma_mask = udma_mask;
616 	entry.filter = false;
617 
618 	device_printf_dbg(adapter->dev,
619 	    "%s: [%d]: addr "MAC_ADDR_STR" mask "MAC_ADDR_STR"\n",
620 	    __func__, idx, MAC_ADDR(entry.addr), MAC_ADDR(entry.mask));
621 
622 	al_eth_fwd_mac_table_set(&adapter->hal_adapter, idx, &entry);
623 }
624 
625 static void
626 al_eth_mac_table_all_multicast_add(struct al_eth_adapter *adapter, uint8_t idx,
627     uint8_t udma_mask)
628 {
629 	struct al_eth_fwd_mac_table_entry entry = { { 0 } };
630 
631 	memset(entry.addr, 0x00, sizeof(entry.addr));
632 	memset(entry.mask, 0x00, sizeof(entry.mask));
633 	entry.mask[0] |= 1;
634 	entry.addr[0] |= 1;
635 
636 	entry.rx_valid = true;
637 	entry.tx_valid = false;
638 	entry.udma_mask = udma_mask;
639 	entry.filter = false;
640 
641 	device_printf_dbg(adapter->dev,
642 	    "%s: [%d]: addr "MAC_ADDR_STR" mask "MAC_ADDR_STR"\n",
643 	    __func__, idx, MAC_ADDR(entry.addr), MAC_ADDR(entry.mask));
644 
645 	al_eth_fwd_mac_table_set(&adapter->hal_adapter, idx, &entry);
646 }
647 
648 static void
649 al_eth_mac_table_broadcast_add(struct al_eth_adapter *adapter,
650     uint8_t idx, uint8_t udma_mask)
651 {
652 	struct al_eth_fwd_mac_table_entry entry = { { 0 } };
653 
654 	memset(entry.addr, 0xff, sizeof(entry.addr));
655 	memset(entry.mask, 0xff, sizeof(entry.mask));
656 
657 	entry.rx_valid = true;
658 	entry.tx_valid = false;
659 	entry.udma_mask = udma_mask;
660 	entry.filter = false;
661 
662 	device_printf_dbg(adapter->dev,
663 	    "%s: [%d]: addr "MAC_ADDR_STR" mask "MAC_ADDR_STR"\n",
664 	    __func__, idx, MAC_ADDR(entry.addr), MAC_ADDR(entry.mask));
665 
666 	al_eth_fwd_mac_table_set(&adapter->hal_adapter, idx, &entry);
667 }
668 
669 static void
670 al_eth_mac_table_promiscuous_set(struct al_eth_adapter *adapter,
671     boolean_t promiscuous)
672 {
673 	struct al_eth_fwd_mac_table_entry entry = { { 0 } };
674 
675 	memset(entry.addr, 0x00, sizeof(entry.addr));
676 	memset(entry.mask, 0x00, sizeof(entry.mask));
677 
678 	entry.rx_valid = true;
679 	entry.tx_valid = false;
680 	entry.udma_mask = (promiscuous) ? 1 : 0;
681 	entry.filter = (promiscuous) ? false : true;
682 
683 	device_printf_dbg(adapter->dev, "%s: %s promiscuous mode\n",
684 	    __func__, (promiscuous) ? "enter" : "exit");
685 
686 	al_eth_fwd_mac_table_set(&adapter->hal_adapter,
687 	    AL_ETH_MAC_TABLE_DROP_IDX, &entry);
688 }
689 
690 static void
691 al_eth_set_thash_table_entry(struct al_eth_adapter *adapter, uint8_t idx,
692     uint8_t udma, uint32_t queue)
693 {
694 
695 	if (udma != 0)
696 		panic("only UDMA0 is supporter");
697 
698 	if (queue >= AL_ETH_NUM_QUEUES)
699 		panic("invalid queue number");
700 
701 	al_eth_thash_table_set(&adapter->hal_adapter, idx, udma, queue);
702 }
703 
704 /* init FSM, no tunneling supported yet, if packet is tcp/udp over ipv4/ipv6, use 4 tuple hash */
705 static void
706 al_eth_fsm_table_init(struct al_eth_adapter *adapter)
707 {
708 	uint32_t val;
709 	int i;
710 
711 	for (i = 0; i < AL_ETH_RX_FSM_TABLE_SIZE; i++) {
712 		uint8_t outer_type = AL_ETH_FSM_ENTRY_OUTER(i);
713 		switch (outer_type) {
714 		case AL_ETH_FSM_ENTRY_IPV4_TCP:
715 		case AL_ETH_FSM_ENTRY_IPV4_UDP:
716 		case AL_ETH_FSM_ENTRY_IPV6_TCP:
717 		case AL_ETH_FSM_ENTRY_IPV6_UDP:
718 			val = AL_ETH_FSM_DATA_OUTER_4_TUPLE |
719 			    AL_ETH_FSM_DATA_HASH_SEL;
720 			break;
721 		case AL_ETH_FSM_ENTRY_IPV6_NO_UDP_TCP:
722 		case AL_ETH_FSM_ENTRY_IPV4_NO_UDP_TCP:
723 			val = AL_ETH_FSM_DATA_OUTER_2_TUPLE |
724 			    AL_ETH_FSM_DATA_HASH_SEL;
725 			break;
726 		default:
727 			val = AL_ETH_FSM_DATA_DEFAULT_Q |
728 			    AL_ETH_FSM_DATA_DEFAULT_UDMA;
729 		}
730 		al_eth_fsm_table_set(&adapter->hal_adapter, i, val);
731 	}
732 }
733 
734 static void
735 al_eth_mac_table_entry_clear(struct al_eth_adapter *adapter,
736     uint8_t idx)
737 {
738 	struct al_eth_fwd_mac_table_entry entry = { { 0 } };
739 
740 	device_printf_dbg(adapter->dev, "%s: clear entry %d\n", __func__, idx);
741 
742 	al_eth_fwd_mac_table_set(&adapter->hal_adapter, idx, &entry);
743 }
744 
745 static int
746 al_eth_hw_init_adapter(struct al_eth_adapter *adapter)
747 {
748 	struct al_eth_adapter_params *params = &adapter->eth_hal_params;
749 	int rc;
750 
751 	/* params->dev_id = adapter->dev_id; */
752 	params->rev_id = adapter->rev_id;
753 	params->udma_id = 0;
754 	params->enable_rx_parser = 1; /* enable rx epe parser*/
755 	params->udma_regs_base = adapter->udma_base; /* UDMA register base address */
756 	params->ec_regs_base = adapter->ec_base; /* Ethernet controller registers base address */
757 	params->mac_regs_base = adapter->mac_base; /* Ethernet MAC registers base address */
758 	params->name = adapter->name;
759 	params->serdes_lane = adapter->serdes_lane;
760 
761 	rc = al_eth_adapter_init(&adapter->hal_adapter, params);
762 	if (rc != 0)
763 		device_printf(adapter->dev, "%s failed at hal init!\n",
764 		    __func__);
765 
766 	if ((adapter->board_type == ALPINE_NIC) ||
767 	    (adapter->board_type == ALPINE_FPGA_NIC)) {
768 		/* in pcie NIC mode, force eth UDMA to access PCIE0 using the vmid */
769 		struct al_udma_gen_tgtid_conf conf;
770 		int i;
771 		for (i = 0; i < DMA_MAX_Q; i++) {
772 			conf.tx_q_conf[i].queue_en = AL_TRUE;
773 			conf.tx_q_conf[i].desc_en = AL_FALSE;
774 			conf.tx_q_conf[i].tgtid = 0x100; /* for access from PCIE0 */
775 			conf.rx_q_conf[i].queue_en = AL_TRUE;
776 			conf.rx_q_conf[i].desc_en = AL_FALSE;
777 			conf.rx_q_conf[i].tgtid = 0x100; /* for access from PCIE0 */
778 		}
779 		al_udma_gen_tgtid_conf_set(adapter->udma_base, &conf);
780 	}
781 
782 	return (rc);
783 }
784 
785 static void
786 al_eth_lm_config(struct al_eth_adapter *adapter)
787 {
788 	struct al_eth_lm_init_params params = {0};
789 
790 	params.adapter = &adapter->hal_adapter;
791 	params.serdes_obj = &adapter->serdes_obj;
792 	params.lane = adapter->serdes_lane;
793 	params.sfp_detection = adapter->sfp_detection_needed;
794 	if (adapter->sfp_detection_needed == true) {
795 		params.sfp_bus_id = adapter->i2c_adapter_id;
796 		params.sfp_i2c_addr = SFP_I2C_ADDR;
797 	}
798 
799 	if (adapter->sfp_detection_needed == false) {
800 		switch (adapter->mac_mode) {
801 		case AL_ETH_MAC_MODE_10GbE_Serial:
802 			if ((adapter->lt_en != 0) && (adapter->an_en != 0))
803 				params.default_mode = AL_ETH_LM_MODE_10G_DA;
804 			else
805 				params.default_mode = AL_ETH_LM_MODE_10G_OPTIC;
806 			break;
807 		case AL_ETH_MAC_MODE_SGMII:
808 			params.default_mode = AL_ETH_LM_MODE_1G;
809 			break;
810 		default:
811 			params.default_mode = AL_ETH_LM_MODE_10G_DA;
812 		}
813 	} else
814 		params.default_mode = AL_ETH_LM_MODE_10G_DA;
815 
816 	params.link_training = adapter->lt_en;
817 	params.rx_equal = true;
818 	params.static_values = !adapter->dont_override_serdes;
819 	params.i2c_context = adapter;
820 	params.kr_fec_enable = false;
821 
822 	params.retimer_exist = adapter->retimer.exist;
823 	params.retimer_bus_id = adapter->retimer.bus_id;
824 	params.retimer_i2c_addr = adapter->retimer.i2c_addr;
825 	params.retimer_channel = adapter->retimer.channel;
826 
827 	al_eth_lm_init(&adapter->lm_context, &params);
828 }
829 
830 static int
831 al_eth_board_params_init(struct al_eth_adapter *adapter)
832 {
833 
834 	if (adapter->board_type == ALPINE_NIC) {
835 		adapter->mac_mode = AL_ETH_MAC_MODE_10GbE_Serial;
836 		adapter->sfp_detection_needed = false;
837 		adapter->phy_exist = false;
838 		adapter->an_en = false;
839 		adapter->lt_en = false;
840 		adapter->ref_clk_freq = AL_ETH_REF_FREQ_375_MHZ;
841 		adapter->mdio_freq = AL_ETH_DEFAULT_MDIO_FREQ_KHZ;
842 	} else if (adapter->board_type == ALPINE_FPGA_NIC) {
843 		adapter->mac_mode = AL_ETH_MAC_MODE_SGMII;
844 		adapter->sfp_detection_needed = false;
845 		adapter->phy_exist = false;
846 		adapter->an_en = false;
847 		adapter->lt_en = false;
848 		adapter->ref_clk_freq = AL_ETH_REF_FREQ_375_MHZ;
849 		adapter->mdio_freq = AL_ETH_DEFAULT_MDIO_FREQ_KHZ;
850 	} else {
851 		struct al_eth_board_params params;
852 		int rc;
853 
854 		adapter->auto_speed = false;
855 
856 		rc = al_eth_board_params_get(adapter->mac_base, &params);
857 		if (rc != 0) {
858 			device_printf(adapter->dev,
859 			    "board info not available\n");
860 			return (-1);
861 		}
862 
863 		adapter->phy_exist = params.phy_exist == TRUE;
864 		adapter->phy_addr = params.phy_mdio_addr;
865 		adapter->an_en = params.autoneg_enable;
866 		adapter->lt_en = params.kr_lt_enable;
867 		adapter->serdes_grp = params.serdes_grp;
868 		adapter->serdes_lane = params.serdes_lane;
869 		adapter->sfp_detection_needed = params.sfp_plus_module_exist;
870 		adapter->i2c_adapter_id = params.i2c_adapter_id;
871 		adapter->ref_clk_freq = params.ref_clk_freq;
872 		adapter->dont_override_serdes = params.dont_override_serdes;
873 		adapter->link_config.active_duplex = !params.half_duplex;
874 		adapter->link_config.autoneg = !params.an_disable;
875 		adapter->link_config.force_1000_base_x = params.force_1000_base_x;
876 		adapter->retimer.exist = params.retimer_exist;
877 		adapter->retimer.bus_id = params.retimer_bus_id;
878 		adapter->retimer.i2c_addr = params.retimer_i2c_addr;
879 		adapter->retimer.channel = params.retimer_channel;
880 
881 		switch (params.speed) {
882 		default:
883 			device_printf(adapter->dev,
884 			    "%s: invalid speed (%d)\n", __func__, params.speed);
885 		case AL_ETH_BOARD_1G_SPEED_1000M:
886 			adapter->link_config.active_speed = 1000;
887 			break;
888 		case AL_ETH_BOARD_1G_SPEED_100M:
889 			adapter->link_config.active_speed = 100;
890 			break;
891 		case AL_ETH_BOARD_1G_SPEED_10M:
892 			adapter->link_config.active_speed = 10;
893 			break;
894 		}
895 
896 		switch (params.mdio_freq) {
897 		default:
898 			device_printf(adapter->dev,
899 			    "%s: invalid mdio freq (%d)\n", __func__,
900 			    params.mdio_freq);
901 		case AL_ETH_BOARD_MDIO_FREQ_2_5_MHZ:
902 			adapter->mdio_freq = AL_ETH_DEFAULT_MDIO_FREQ_KHZ;
903 			break;
904 		case AL_ETH_BOARD_MDIO_FREQ_1_MHZ:
905 			adapter->mdio_freq = AL_ETH_MDIO_FREQ_1000_KHZ;
906 			break;
907 		}
908 
909 		switch (params.media_type) {
910 		case AL_ETH_BOARD_MEDIA_TYPE_RGMII:
911 			if (params.sfp_plus_module_exist == TRUE)
912 				/* Backward compatibility */
913 				adapter->mac_mode = AL_ETH_MAC_MODE_SGMII;
914 			else
915 				adapter->mac_mode = AL_ETH_MAC_MODE_RGMII;
916 
917 			adapter->use_lm = false;
918 			break;
919 		case AL_ETH_BOARD_MEDIA_TYPE_SGMII:
920 			adapter->mac_mode = AL_ETH_MAC_MODE_SGMII;
921 			adapter->use_lm = true;
922 			break;
923 		case AL_ETH_BOARD_MEDIA_TYPE_10GBASE_SR:
924 			adapter->mac_mode = AL_ETH_MAC_MODE_10GbE_Serial;
925 			adapter->use_lm = true;
926 			break;
927 		case AL_ETH_BOARD_MEDIA_TYPE_AUTO_DETECT:
928 			adapter->sfp_detection_needed = TRUE;
929 			adapter->auto_speed = false;
930 			adapter->use_lm = true;
931 			break;
932 		case AL_ETH_BOARD_MEDIA_TYPE_AUTO_DETECT_AUTO_SPEED:
933 			adapter->sfp_detection_needed = TRUE;
934 			adapter->auto_speed = true;
935 			adapter->mac_mode_set = false;
936 			adapter->use_lm = true;
937 
938 			adapter->mac_mode = AL_ETH_MAC_MODE_10GbE_Serial;
939 			break;
940 		default:
941 			device_printf(adapter->dev,
942 			    "%s: unsupported media type %d\n",
943 			    __func__, params.media_type);
944 			return (-1);
945 		}
946 
947 		device_printf(adapter->dev,
948 		    "Board info: phy exist %s. phy addr %d. mdio freq %u Khz. "
949 		    "SFP connected %s. media %d\n",
950 		    params.phy_exist == TRUE ? "Yes" : "No",
951 		    params.phy_mdio_addr, adapter->mdio_freq,
952 		    params.sfp_plus_module_exist == TRUE ? "Yes" : "No",
953 		    params.media_type);
954 	}
955 
956 	al_eth_mac_addr_read(adapter->ec_base, 0, adapter->mac_addr);
957 
958 	return (0);
959 }
960 
961 static int
962 al_eth_function_reset(struct al_eth_adapter *adapter)
963 {
964 	struct al_eth_board_params params;
965 	int rc;
966 
967 	/* save board params so we restore it after reset */
968 	al_eth_board_params_get(adapter->mac_base, &params);
969 	al_eth_mac_addr_read(adapter->ec_base, 0, adapter->mac_addr);
970 	if (adapter->board_type == ALPINE_INTEGRATED)
971 		rc = al_eth_flr_rmn(&al_eth_read_pci_config,
972 		    &al_eth_write_pci_config,
973 		    adapter->dev, adapter->mac_base);
974 	else
975 		rc = al_eth_flr_rmn(&al_eth_fpga_read_pci_config,
976 		    &al_eth_fpga_write_pci_config,
977 		    adapter->internal_pcie_base, adapter->mac_base);
978 
979 	/* restore params */
980 	al_eth_board_params_set(adapter->mac_base, &params);
981 	al_eth_mac_addr_store(adapter->ec_base, 0, adapter->mac_addr);
982 
983 	return (rc);
984 }
985 
986 static void
987 al_eth_init_rings(struct al_eth_adapter *adapter)
988 {
989 	int i;
990 
991 	for (i = 0; i < adapter->num_tx_queues; i++) {
992 		struct al_eth_ring *ring = &adapter->tx_ring[i];
993 
994 		ring->ring_id = i;
995 		ring->dev = adapter->dev;
996 		ring->adapter = adapter;
997 		ring->netdev = adapter->netdev;
998 		al_udma_q_handle_get(&adapter->hal_adapter.tx_udma, i,
999 		    &ring->dma_q);
1000 		ring->sw_count = adapter->tx_ring_count;
1001 		ring->hw_count = adapter->tx_descs_count;
1002 		ring->unmask_reg_offset = al_udma_iofic_unmask_offset_get((struct unit_regs *)adapter->udma_base, AL_UDMA_IOFIC_LEVEL_PRIMARY, AL_INT_GROUP_C);
1003 		ring->unmask_val = ~(1 << i);
1004 	}
1005 
1006 	for (i = 0; i < adapter->num_rx_queues; i++) {
1007 		struct al_eth_ring *ring = &adapter->rx_ring[i];
1008 
1009 		ring->ring_id = i;
1010 		ring->dev = adapter->dev;
1011 		ring->adapter = adapter;
1012 		ring->netdev = adapter->netdev;
1013 		al_udma_q_handle_get(&adapter->hal_adapter.rx_udma, i, &ring->dma_q);
1014 		ring->sw_count = adapter->rx_ring_count;
1015 		ring->hw_count = adapter->rx_descs_count;
1016 		ring->unmask_reg_offset = al_udma_iofic_unmask_offset_get(
1017 		    (struct unit_regs *)adapter->udma_base,
1018 		    AL_UDMA_IOFIC_LEVEL_PRIMARY, AL_INT_GROUP_B);
1019 		ring->unmask_val = ~(1 << i);
1020 	}
1021 }
1022 
1023 static void
1024 al_init_locked(void *arg)
1025 {
1026 	struct al_eth_adapter *adapter = arg;
1027 	if_t ifp = adapter->netdev;
1028 	int rc = 0;
1029 
1030 	al_eth_down(adapter);
1031 	rc = al_eth_up(adapter);
1032 
1033 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1034 	if (rc == 0)
1035 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1036 }
1037 
1038 static void
1039 al_init(void *arg)
1040 {
1041 	struct al_eth_adapter *adapter = arg;
1042 
1043 	al_init_locked(adapter);
1044 }
1045 
1046 static inline int
1047 al_eth_alloc_rx_buf(struct al_eth_adapter *adapter,
1048     struct al_eth_ring *rx_ring,
1049     struct al_eth_rx_buffer *rx_info)
1050 {
1051 	struct al_buf *al_buf;
1052 	bus_dma_segment_t segs[2];
1053 	int error;
1054 	int nsegs;
1055 
1056 	if (rx_info->m != NULL)
1057 		return (0);
1058 
1059 	rx_info->data_size = adapter->rx_mbuf_sz;
1060 
1061 	AL_RX_LOCK(adapter);
1062 
1063 	/* Get mbuf using UMA allocator */
1064 	rx_info->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
1065 	    rx_info->data_size);
1066 	AL_RX_UNLOCK(adapter);
1067 
1068 	if (rx_info->m == NULL)
1069 		return (ENOMEM);
1070 
1071 	rx_info->m->m_pkthdr.len = rx_info->m->m_len = adapter->rx_mbuf_sz;
1072 
1073 	/* Map packets for DMA */
1074 	error = bus_dmamap_load_mbuf_sg(rx_ring->dma_buf_tag, rx_info->dma_map,
1075 	    rx_info->m, segs, &nsegs, BUS_DMA_NOWAIT);
1076 	if (__predict_false(error)) {
1077 		device_printf(rx_ring->dev, "failed to map mbuf, error = %d\n",
1078 		    error);
1079 		m_freem(rx_info->m);
1080 		rx_info->m = NULL;
1081 		return (EFAULT);
1082 	}
1083 
1084 	al_buf = &rx_info->al_buf;
1085 	al_buf->addr = segs[0].ds_addr + AL_IP_ALIGNMENT_OFFSET;
1086 	al_buf->len = rx_info->data_size - AL_IP_ALIGNMENT_OFFSET;
1087 
1088 	return (0);
1089 }
1090 
1091 static int
1092 al_eth_refill_rx_bufs(struct al_eth_adapter *adapter, unsigned int qid,
1093     unsigned int num)
1094 {
1095 	struct al_eth_ring *rx_ring = &adapter->rx_ring[qid];
1096 	uint16_t next_to_use;
1097 	unsigned int i;
1098 
1099 	next_to_use = rx_ring->next_to_use;
1100 
1101 	for (i = 0; i < num; i++) {
1102 		int rc;
1103 		struct al_eth_rx_buffer *rx_info =
1104 		    &rx_ring->rx_buffer_info[next_to_use];
1105 
1106 		if (__predict_false(al_eth_alloc_rx_buf(adapter,
1107 		    rx_ring, rx_info) < 0)) {
1108 			device_printf(adapter->dev,
1109 			    "failed to alloc buffer for rx queue %d\n", qid);
1110 			break;
1111 		}
1112 
1113 		rc = al_eth_rx_buffer_add(rx_ring->dma_q,
1114 		    &rx_info->al_buf, AL_ETH_RX_FLAGS_INT, NULL);
1115 		if (__predict_false(rc)) {
1116 			device_printf(adapter->dev,
1117 			    "failed to add buffer for rx queue %d\n", qid);
1118 			break;
1119 		}
1120 
1121 		next_to_use = AL_ETH_RX_RING_IDX_NEXT(rx_ring, next_to_use);
1122 	}
1123 
1124 	if (__predict_false(i < num))
1125 		device_printf(adapter->dev,
1126 		    "refilled rx queue %d with %d pages only - available %d\n",
1127 		    qid, i, al_udma_available_get(rx_ring->dma_q));
1128 
1129 	if (__predict_true(i))
1130 		al_eth_rx_buffer_action(rx_ring->dma_q, i);
1131 
1132 	rx_ring->next_to_use = next_to_use;
1133 
1134 	return (i);
1135 }
1136 
1137 /*
1138  * al_eth_refill_all_rx_bufs - allocate all queues Rx buffers
1139  * @adapter: board private structure
1140  */
1141 static void
1142 al_eth_refill_all_rx_bufs(struct al_eth_adapter *adapter)
1143 {
1144 	int i;
1145 
1146 	for (i = 0; i < adapter->num_rx_queues; i++)
1147 		al_eth_refill_rx_bufs(adapter, i, AL_ETH_DEFAULT_RX_DESCS - 1);
1148 }
1149 
1150 static void
1151 al_eth_tx_do_cleanup(struct al_eth_ring *tx_ring)
1152 {
1153 	unsigned int total_done;
1154 	uint16_t next_to_clean;
1155 	int qid = tx_ring->ring_id;
1156 
1157 	total_done = al_eth_comp_tx_get(tx_ring->dma_q);
1158 	device_printf_dbg(tx_ring->dev,
1159 	    "tx_poll: q %d total completed descs %x\n", qid, total_done);
1160 	next_to_clean = tx_ring->next_to_clean;
1161 
1162 	while (total_done != 0) {
1163 		struct al_eth_tx_buffer *tx_info;
1164 		struct mbuf *mbuf;
1165 
1166 		tx_info = &tx_ring->tx_buffer_info[next_to_clean];
1167 		/* stop if not all descriptors of the packet are completed */
1168 		if (tx_info->tx_descs > total_done)
1169 			break;
1170 
1171 		mbuf = tx_info->m;
1172 
1173 		tx_info->m = NULL;
1174 
1175 		device_printf_dbg(tx_ring->dev,
1176 		    "tx_poll: q %d mbuf %p completed\n", qid, mbuf);
1177 
1178 		/* map is no longer required */
1179 		bus_dmamap_unload(tx_ring->dma_buf_tag, tx_info->dma_map);
1180 
1181 		m_freem(mbuf);
1182 		total_done -= tx_info->tx_descs;
1183 		next_to_clean = AL_ETH_TX_RING_IDX_NEXT(tx_ring, next_to_clean);
1184 	}
1185 
1186 	tx_ring->next_to_clean = next_to_clean;
1187 
1188 	device_printf_dbg(tx_ring->dev, "tx_poll: q %d done next to clean %x\n",
1189 	    qid, next_to_clean);
1190 
1191 	/*
1192 	 * need to make the rings circular update visible to
1193 	 * al_eth_start_xmit() before checking for netif_queue_stopped().
1194 	 */
1195 	al_smp_data_memory_barrier();
1196 }
1197 
1198 static void
1199 al_eth_tx_csum(struct al_eth_ring *tx_ring, struct al_eth_tx_buffer *tx_info,
1200     struct al_eth_pkt *hal_pkt, struct mbuf *m)
1201 {
1202 	uint32_t mss = m->m_pkthdr.tso_segsz;
1203 	struct ether_vlan_header *eh;
1204 	uint16_t etype;
1205 #ifdef INET
1206 	struct ip *ip;
1207 #endif
1208 #ifdef INET6
1209 	struct ip6_hdr *ip6;
1210 #endif
1211 	struct tcphdr *th = NULL;
1212 	int	ehdrlen, ip_hlen = 0;
1213 	uint8_t	ipproto = 0;
1214 	uint32_t offload = 0;
1215 
1216 	if (mss != 0)
1217 		offload = 1;
1218 
1219 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0)
1220 		offload = 1;
1221 
1222 	if ((m->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
1223 		offload = 1;
1224 
1225 	if (offload != 0) {
1226 		struct al_eth_meta_data *meta = &tx_ring->hal_meta;
1227 
1228 		if (mss != 0)
1229 			hal_pkt->flags |= (AL_ETH_TX_FLAGS_TSO |
1230 			    AL_ETH_TX_FLAGS_L4_CSUM);
1231 		else
1232 			hal_pkt->flags |= (AL_ETH_TX_FLAGS_L4_CSUM |
1233 			    AL_ETH_TX_FLAGS_L4_PARTIAL_CSUM);
1234 
1235 		/*
1236 		 * Determine where frame payload starts.
1237 		 * Jump over vlan headers if already present,
1238 		 * helpful for QinQ too.
1239 		 */
1240 		eh = mtod(m, struct ether_vlan_header *);
1241 		if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
1242 			etype = ntohs(eh->evl_proto);
1243 			ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
1244 		} else {
1245 			etype = ntohs(eh->evl_encap_proto);
1246 			ehdrlen = ETHER_HDR_LEN;
1247 		}
1248 
1249 		switch (etype) {
1250 #ifdef INET
1251 		case ETHERTYPE_IP:
1252 			ip = (struct ip *)(m->m_data + ehdrlen);
1253 			ip_hlen = ip->ip_hl << 2;
1254 			ipproto = ip->ip_p;
1255 			hal_pkt->l3_proto_idx = AL_ETH_PROTO_ID_IPv4;
1256 			th = (struct tcphdr *)((caddr_t)ip + ip_hlen);
1257 			if (mss != 0)
1258 				hal_pkt->flags |= AL_ETH_TX_FLAGS_IPV4_L3_CSUM;
1259 			if (ipproto == IPPROTO_TCP)
1260 				hal_pkt->l4_proto_idx = AL_ETH_PROTO_ID_TCP;
1261 			else
1262 				hal_pkt->l4_proto_idx = AL_ETH_PROTO_ID_UDP;
1263 			break;
1264 #endif /* INET */
1265 #ifdef INET6
1266 		case ETHERTYPE_IPV6:
1267 			ip6 = (struct ip6_hdr *)(m->m_data + ehdrlen);
1268 			hal_pkt->l3_proto_idx = AL_ETH_PROTO_ID_IPv6;
1269 			ip_hlen = sizeof(struct ip6_hdr);
1270 			th = (struct tcphdr *)((caddr_t)ip6 + ip_hlen);
1271 			ipproto = ip6->ip6_nxt;
1272 			if (ipproto == IPPROTO_TCP)
1273 				hal_pkt->l4_proto_idx = AL_ETH_PROTO_ID_TCP;
1274 			else
1275 				hal_pkt->l4_proto_idx = AL_ETH_PROTO_ID_UDP;
1276 			break;
1277 #endif /* INET6 */
1278 		default:
1279 			break;
1280 		}
1281 
1282 		meta->words_valid = 4;
1283 		meta->l3_header_len = ip_hlen;
1284 		meta->l3_header_offset = ehdrlen;
1285 		if (th != NULL)
1286 			meta->l4_header_len = th->th_off; /* this param needed only for TSO */
1287 		meta->mss_idx_sel = 0;			/* check how to select MSS */
1288 		meta->mss_val = mss;
1289 		hal_pkt->meta = meta;
1290 	} else
1291 		hal_pkt->meta = NULL;
1292 }
1293 
1294 #define	XMIT_QUEUE_TIMEOUT	100
1295 
1296 static void
1297 al_eth_xmit_mbuf(struct al_eth_ring *tx_ring, struct mbuf *m)
1298 {
1299 	struct al_eth_tx_buffer *tx_info;
1300 	int error;
1301 	int nsegs, a;
1302 	uint16_t next_to_use;
1303 	bus_dma_segment_t segs[AL_ETH_PKT_MAX_BUFS + 1];
1304 	struct al_eth_pkt *hal_pkt;
1305 	struct al_buf *al_buf;
1306 	boolean_t remap;
1307 
1308 	/* Check if queue is ready */
1309 	if (unlikely(tx_ring->stall) != 0) {
1310 		for (a = 0; a < XMIT_QUEUE_TIMEOUT; a++) {
1311 			if (al_udma_available_get(tx_ring->dma_q) >=
1312 			    (AL_ETH_DEFAULT_TX_HW_DESCS -
1313 			    AL_ETH_TX_WAKEUP_THRESH)) {
1314 				tx_ring->stall = 0;
1315 				break;
1316 			}
1317 			pause("stall", 1);
1318 		}
1319 		if (a == XMIT_QUEUE_TIMEOUT) {
1320 			device_printf(tx_ring->dev,
1321 			    "timeout waiting for queue %d ready!\n",
1322 			    tx_ring->ring_id);
1323 			return;
1324 		} else {
1325 			device_printf_dbg(tx_ring->dev,
1326 			    "queue %d is ready!\n", tx_ring->ring_id);
1327 		}
1328 	}
1329 
1330 	next_to_use = tx_ring->next_to_use;
1331 	tx_info = &tx_ring->tx_buffer_info[next_to_use];
1332 	tx_info->m = m;
1333 	hal_pkt = &tx_info->hal_pkt;
1334 
1335 	if (m == NULL) {
1336 		device_printf(tx_ring->dev, "mbuf is NULL\n");
1337 		return;
1338 	}
1339 
1340 	remap = TRUE;
1341 	/* Map packets for DMA */
1342 retry:
1343 	error = bus_dmamap_load_mbuf_sg(tx_ring->dma_buf_tag, tx_info->dma_map,
1344 	    m, segs, &nsegs, BUS_DMA_NOWAIT);
1345 	if (__predict_false(error)) {
1346 		struct mbuf *m_new;
1347 
1348 		if (error == EFBIG) {
1349 			/* Try it again? - one try */
1350 			if (remap == TRUE) {
1351 				remap = FALSE;
1352 				m_new = m_defrag(m, M_NOWAIT);
1353 				if (m_new == NULL) {
1354 					device_printf(tx_ring->dev,
1355 					    "failed to defrag mbuf\n");
1356 					goto exit;
1357 				}
1358 				m = m_new;
1359 				goto retry;
1360 			} else {
1361 				device_printf(tx_ring->dev,
1362 				    "failed to map mbuf, error %d\n", error);
1363 				goto exit;
1364 			}
1365 		} else {
1366 			device_printf(tx_ring->dev,
1367 			    "failed to map mbuf, error %d\n", error);
1368 			goto exit;
1369 		}
1370 	}
1371 
1372 	/* set flags and meta data */
1373 	hal_pkt->flags = AL_ETH_TX_FLAGS_INT;
1374 	al_eth_tx_csum(tx_ring, tx_info, hal_pkt, m);
1375 
1376 	al_buf = hal_pkt->bufs;
1377 	for (a = 0; a < nsegs; a++) {
1378 		al_buf->addr = segs[a].ds_addr;
1379 		al_buf->len = segs[a].ds_len;
1380 
1381 		al_buf++;
1382 	}
1383 
1384 	hal_pkt->num_of_bufs = nsegs;
1385 
1386 	/* prepare the packet's descriptors to dma engine */
1387 	tx_info->tx_descs = al_eth_tx_pkt_prepare(tx_ring->dma_q, hal_pkt);
1388 
1389 	if (tx_info->tx_descs == 0)
1390 		goto exit;
1391 
1392 	/*
1393 	 * stop the queue when no more space available, the packet can have up
1394 	 * to AL_ETH_PKT_MAX_BUFS + 1 buffers and a meta descriptor
1395 	 */
1396 	if (unlikely(al_udma_available_get(tx_ring->dma_q) <
1397 	    (AL_ETH_PKT_MAX_BUFS + 2))) {
1398 		tx_ring->stall = 1;
1399 		device_printf_dbg(tx_ring->dev, "stall, stopping queue %d...\n",
1400 		    tx_ring->ring_id);
1401 		al_data_memory_barrier();
1402 	}
1403 
1404 	tx_ring->next_to_use = AL_ETH_TX_RING_IDX_NEXT(tx_ring, next_to_use);
1405 
1406 	/* trigger the dma engine */
1407 	al_eth_tx_dma_action(tx_ring->dma_q, tx_info->tx_descs);
1408 	return;
1409 
1410 exit:
1411 	m_freem(m);
1412 }
1413 
1414 static void
1415 al_eth_tx_cmpl_work(void *arg, int pending)
1416 {
1417 	struct al_eth_ring *tx_ring = arg;
1418 
1419 	if (napi != 0) {
1420 		tx_ring->cmpl_is_running = 1;
1421 		al_data_memory_barrier();
1422 	}
1423 
1424 	al_eth_tx_do_cleanup(tx_ring);
1425 
1426 	if (napi != 0) {
1427 		tx_ring->cmpl_is_running = 0;
1428 		al_data_memory_barrier();
1429 	}
1430 	/* all work done, enable IRQs */
1431 	al_eth_irq_config(tx_ring->unmask_reg_offset, tx_ring->unmask_val);
1432 }
1433 
1434 static int
1435 al_eth_tx_cmlp_irq_filter(void *arg)
1436 {
1437 	struct al_eth_ring *tx_ring = arg;
1438 
1439 	/* Interrupt should be auto-masked upon arrival */
1440 
1441 	device_printf_dbg(tx_ring->dev, "%s for ring ID = %d\n", __func__,
1442 	    tx_ring->ring_id);
1443 
1444 	/*
1445 	 * For napi, if work is not running, schedule it. Always schedule
1446 	 * for casual (non-napi) packet handling.
1447 	 */
1448 	if ((napi == 0) || (napi && tx_ring->cmpl_is_running == 0))
1449 		taskqueue_enqueue(tx_ring->cmpl_tq, &tx_ring->cmpl_task);
1450 
1451 	/* Do not run bottom half */
1452 	return (FILTER_HANDLED);
1453 }
1454 
1455 static int
1456 al_eth_rx_recv_irq_filter(void *arg)
1457 {
1458 	struct al_eth_ring *rx_ring = arg;
1459 
1460 	/* Interrupt should be auto-masked upon arrival */
1461 
1462 	device_printf_dbg(rx_ring->dev, "%s for ring ID = %d\n", __func__,
1463 	    rx_ring->ring_id);
1464 
1465 	/*
1466 	 * For napi, if work is not running, schedule it. Always schedule
1467 	 * for casual (non-napi) packet handling.
1468 	 */
1469 	if ((napi == 0) || (napi && rx_ring->enqueue_is_running == 0))
1470 		taskqueue_enqueue(rx_ring->enqueue_tq, &rx_ring->enqueue_task);
1471 
1472 	/* Do not run bottom half */
1473 	return (FILTER_HANDLED);
1474 }
1475 
1476 /*
1477  * al_eth_rx_checksum - indicate in mbuf if hw indicated a good cksum
1478  * @adapter: structure containing adapter specific data
1479  * @hal_pkt: HAL structure for the packet
1480  * @mbuf: mbuf currently being received and modified
1481  */
1482 static inline void
1483 al_eth_rx_checksum(struct al_eth_adapter *adapter,
1484     struct al_eth_pkt *hal_pkt, struct mbuf *mbuf)
1485 {
1486 
1487 	/* if IPv4 and error */
1488 	if (unlikely((adapter->netdev->if_capenable & IFCAP_RXCSUM) &&
1489 	    (hal_pkt->l3_proto_idx == AL_ETH_PROTO_ID_IPv4) &&
1490 	    (hal_pkt->flags & AL_ETH_RX_FLAGS_L3_CSUM_ERR))) {
1491 		device_printf(adapter->dev,"rx ipv4 header checksum error\n");
1492 		return;
1493 	}
1494 
1495 	/* if IPv6 and error */
1496 	if (unlikely((adapter->netdev->if_capenable & IFCAP_RXCSUM_IPV6) &&
1497 	    (hal_pkt->l3_proto_idx == AL_ETH_PROTO_ID_IPv6) &&
1498 	    (hal_pkt->flags & AL_ETH_RX_FLAGS_L3_CSUM_ERR))) {
1499 		device_printf(adapter->dev,"rx ipv6 header checksum error\n");
1500 		return;
1501 	}
1502 
1503 	/* if TCP/UDP */
1504 	if (likely((hal_pkt->l4_proto_idx == AL_ETH_PROTO_ID_TCP) ||
1505 	   (hal_pkt->l4_proto_idx == AL_ETH_PROTO_ID_UDP))) {
1506 		if (unlikely(hal_pkt->flags & AL_ETH_RX_FLAGS_L4_CSUM_ERR)) {
1507 			device_printf_dbg(adapter->dev, "rx L4 checksum error\n");
1508 
1509 			/* TCP/UDP checksum error */
1510 			mbuf->m_pkthdr.csum_flags = 0;
1511 		} else {
1512 			device_printf_dbg(adapter->dev, "rx checksum correct\n");
1513 
1514 			/* IP Checksum Good */
1515 			mbuf->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
1516 			mbuf->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1517 		}
1518 	}
1519 }
1520 
1521 static struct mbuf*
1522 al_eth_rx_mbuf(struct al_eth_adapter *adapter,
1523     struct al_eth_ring *rx_ring, struct al_eth_pkt *hal_pkt,
1524     unsigned int descs, uint16_t *next_to_clean)
1525 {
1526 	struct mbuf *mbuf;
1527 	struct al_eth_rx_buffer *rx_info =
1528 	    &rx_ring->rx_buffer_info[*next_to_clean];
1529 	unsigned int len;
1530 
1531 	len = hal_pkt->bufs[0].len;
1532 	device_printf_dbg(adapter->dev, "rx_info %p data %p\n", rx_info,
1533 	   rx_info->m);
1534 
1535 	if (rx_info->m == NULL) {
1536 		*next_to_clean = AL_ETH_RX_RING_IDX_NEXT(rx_ring,
1537 		    *next_to_clean);
1538 		return (NULL);
1539 	}
1540 
1541 	mbuf = rx_info->m;
1542 	mbuf->m_pkthdr.len = len;
1543 	mbuf->m_len = len;
1544 	mbuf->m_pkthdr.rcvif = rx_ring->netdev;
1545 	mbuf->m_flags |= M_PKTHDR;
1546 
1547 	if (len <= adapter->small_copy_len) {
1548 		struct mbuf *smbuf;
1549 		device_printf_dbg(adapter->dev, "rx small packet. len %d\n", len);
1550 
1551 		AL_RX_LOCK(adapter);
1552 		smbuf = m_gethdr(M_NOWAIT, MT_DATA);
1553 		AL_RX_UNLOCK(adapter);
1554 		if (__predict_false(smbuf == NULL)) {
1555 			device_printf(adapter->dev, "smbuf is NULL\n");
1556 			return (NULL);
1557 		}
1558 
1559 		smbuf->m_data = smbuf->m_data + AL_IP_ALIGNMENT_OFFSET;
1560 		memcpy(smbuf->m_data, mbuf->m_data + AL_IP_ALIGNMENT_OFFSET, len);
1561 
1562 		smbuf->m_len = len;
1563 		smbuf->m_pkthdr.rcvif = rx_ring->netdev;
1564 
1565 		/* first desc of a non-ps chain */
1566 		smbuf->m_flags |= M_PKTHDR;
1567 		smbuf->m_pkthdr.len = smbuf->m_len;
1568 
1569 		*next_to_clean = AL_ETH_RX_RING_IDX_NEXT(rx_ring,
1570 		    *next_to_clean);
1571 
1572 		return (smbuf);
1573 	}
1574 	mbuf->m_data = mbuf->m_data + AL_IP_ALIGNMENT_OFFSET;
1575 
1576 	/* Unmap the buffer */
1577 	bus_dmamap_unload(rx_ring->dma_buf_tag, rx_info->dma_map);
1578 
1579 	rx_info->m = NULL;
1580 	*next_to_clean = AL_ETH_RX_RING_IDX_NEXT(rx_ring, *next_to_clean);
1581 
1582 	return (mbuf);
1583 }
1584 
1585 static void
1586 al_eth_rx_recv_work(void *arg, int pending)
1587 {
1588 	struct al_eth_ring *rx_ring = arg;
1589 	struct mbuf *mbuf;
1590 	struct lro_entry *queued;
1591 	unsigned int qid = rx_ring->ring_id;
1592 	struct al_eth_pkt *hal_pkt = &rx_ring->hal_pkt;
1593 	uint16_t next_to_clean = rx_ring->next_to_clean;
1594 	uint32_t refill_required;
1595 	uint32_t refill_actual;
1596 	uint32_t do_if_input;
1597 
1598 	if (napi != 0) {
1599 		rx_ring->enqueue_is_running = 1;
1600 		al_data_memory_barrier();
1601 	}
1602 
1603 	do {
1604 		unsigned int descs;
1605 
1606 		descs = al_eth_pkt_rx(rx_ring->dma_q, hal_pkt);
1607 		if (unlikely(descs == 0))
1608 			break;
1609 
1610 		device_printf_dbg(rx_ring->dev, "rx_poll: q %d got packet "
1611 		    "from hal. descs %d\n", qid, descs);
1612 		device_printf_dbg(rx_ring->dev, "rx_poll: q %d flags %x. "
1613 		    "l3 proto %d l4 proto %d\n", qid, hal_pkt->flags,
1614 		    hal_pkt->l3_proto_idx, hal_pkt->l4_proto_idx);
1615 
1616 		/* ignore if detected dma or eth controller errors */
1617 		if ((hal_pkt->flags & (AL_ETH_RX_ERROR |
1618 		    AL_UDMA_CDESC_ERROR)) != 0) {
1619 			device_printf(rx_ring->dev, "receive packet with error. "
1620 			    "flags = 0x%x\n", hal_pkt->flags);
1621 			next_to_clean = AL_ETH_RX_RING_IDX_ADD(rx_ring,
1622 			    next_to_clean, descs);
1623 			continue;
1624 		}
1625 
1626 		/* allocate mbuf and fill it */
1627 		mbuf = al_eth_rx_mbuf(rx_ring->adapter, rx_ring, hal_pkt, descs,
1628 		    &next_to_clean);
1629 
1630 		/* exit if we failed to retrieve a buffer */
1631 		if (unlikely(mbuf == NULL)) {
1632 			next_to_clean = AL_ETH_RX_RING_IDX_ADD(rx_ring,
1633 			    next_to_clean, descs);
1634 			break;
1635 		}
1636 
1637 		if (__predict_true(rx_ring->netdev->if_capenable & IFCAP_RXCSUM ||
1638 		    rx_ring->netdev->if_capenable & IFCAP_RXCSUM_IPV6)) {
1639 			al_eth_rx_checksum(rx_ring->adapter, hal_pkt, mbuf);
1640 		}
1641 
1642 		mbuf->m_pkthdr.flowid = qid;
1643 		M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE);
1644 
1645 		/*
1646 		 * LRO is only for IP/TCP packets and TCP checksum of the packet
1647 		 * should be computed by hardware.
1648 		 */
1649 		do_if_input = 1;
1650 		if ((rx_ring->lro_enabled != 0) &&
1651 		    ((mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0) &&
1652 		    hal_pkt->l4_proto_idx == AL_ETH_PROTO_ID_TCP) {
1653 			/*
1654 			 * Send to the stack if:
1655 			 *  - LRO not enabled, or
1656 			 *  - no LRO resources, or
1657 			 *  - lro enqueue fails
1658 			 */
1659 			if (rx_ring->lro.lro_cnt != 0) {
1660 				if (tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0)
1661 					do_if_input = 0;
1662 			}
1663 		}
1664 
1665 		if (do_if_input)
1666 			(*rx_ring->netdev->if_input)(rx_ring->netdev, mbuf);
1667 
1668 	} while (1);
1669 
1670 	rx_ring->next_to_clean = next_to_clean;
1671 
1672 	refill_required = al_udma_available_get(rx_ring->dma_q);
1673 	refill_actual = al_eth_refill_rx_bufs(rx_ring->adapter, qid,
1674 	    refill_required);
1675 
1676 	if (unlikely(refill_actual < refill_required)) {
1677 		device_printf_dbg(rx_ring->dev,
1678 		    "%s: not filling rx queue %d\n", __func__, qid);
1679 	}
1680 
1681 	while (((queued = LIST_FIRST(&rx_ring->lro.lro_active)) != NULL)) {
1682 		LIST_REMOVE(queued, next);
1683 		tcp_lro_flush(&rx_ring->lro, queued);
1684 	}
1685 
1686 	if (napi != 0) {
1687 		rx_ring->enqueue_is_running = 0;
1688 		al_data_memory_barrier();
1689 	}
1690 	/* unmask irq */
1691 	al_eth_irq_config(rx_ring->unmask_reg_offset, rx_ring->unmask_val);
1692 }
1693 
1694 static void
1695 al_eth_start_xmit(void *arg, int pending)
1696 {
1697 	struct al_eth_ring *tx_ring = arg;
1698 	struct mbuf *mbuf;
1699 
1700 	if (napi != 0) {
1701 		tx_ring->enqueue_is_running = 1;
1702 		al_data_memory_barrier();
1703 	}
1704 
1705 	while (1) {
1706 		mtx_lock(&tx_ring->br_mtx);
1707 		mbuf = drbr_dequeue(NULL, tx_ring->br);
1708 		mtx_unlock(&tx_ring->br_mtx);
1709 
1710 		if (mbuf == NULL)
1711 			break;
1712 
1713 		al_eth_xmit_mbuf(tx_ring, mbuf);
1714 	}
1715 
1716 	if (napi != 0) {
1717 		tx_ring->enqueue_is_running = 0;
1718 		al_data_memory_barrier();
1719 		while (1) {
1720 			mtx_lock(&tx_ring->br_mtx);
1721 			mbuf = drbr_dequeue(NULL, tx_ring->br);
1722 			mtx_unlock(&tx_ring->br_mtx);
1723 			if (mbuf == NULL)
1724 				break;
1725 			al_eth_xmit_mbuf(tx_ring, mbuf);
1726 		}
1727 	}
1728 }
1729 
1730 static int
1731 al_mq_start(struct ifnet *ifp, struct mbuf *m)
1732 {
1733 	struct al_eth_adapter *adapter = ifp->if_softc;
1734 	struct al_eth_ring *tx_ring;
1735 	int i;
1736 	int ret;
1737 
1738 	/* Which queue to use */
1739 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
1740 		i = m->m_pkthdr.flowid % adapter->num_tx_queues;
1741 	else
1742 		i = curcpu % adapter->num_tx_queues;
1743 
1744 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
1745 	    IFF_DRV_RUNNING) {
1746 		return (EFAULT);
1747 	}
1748 
1749 	tx_ring = &adapter->tx_ring[i];
1750 
1751 	device_printf_dbg(adapter->dev, "dgb start() - assuming link is active, "
1752 	    "sending packet to queue %d\n", i);
1753 
1754 	ret = drbr_enqueue(ifp, tx_ring->br, m);
1755 
1756 	/*
1757 	 * For napi, if work is not running, schedule it. Always schedule
1758 	 * for casual (non-napi) packet handling.
1759 	 */
1760 	if ((napi == 0) || ((napi != 0) && (tx_ring->enqueue_is_running == 0)))
1761 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
1762 
1763 	return (ret);
1764 }
1765 
1766 static void
1767 al_qflush(struct ifnet * ifp)
1768 {
1769 
1770 	/* unused */
1771 }
1772 
1773 static inline void
1774 al_eth_flow_ctrl_init(struct al_eth_adapter *adapter)
1775 {
1776 	uint8_t default_flow_ctrl;
1777 
1778 	default_flow_ctrl = AL_ETH_FLOW_CTRL_TX_PAUSE;
1779 	default_flow_ctrl |= AL_ETH_FLOW_CTRL_RX_PAUSE;
1780 
1781 	adapter->link_config.flow_ctrl_supported = default_flow_ctrl;
1782 }
1783 
1784 static int
1785 al_eth_flow_ctrl_config(struct al_eth_adapter *adapter)
1786 {
1787 	struct al_eth_flow_control_params *flow_ctrl_params;
1788 	uint8_t active = adapter->link_config.flow_ctrl_active;
1789 	int i;
1790 
1791 	flow_ctrl_params = &adapter->flow_ctrl_params;
1792 
1793 	flow_ctrl_params->type = AL_ETH_FLOW_CONTROL_TYPE_LINK_PAUSE;
1794 	flow_ctrl_params->obay_enable =
1795 	    ((active & AL_ETH_FLOW_CTRL_RX_PAUSE) != 0);
1796 	flow_ctrl_params->gen_enable =
1797 	    ((active & AL_ETH_FLOW_CTRL_TX_PAUSE) != 0);
1798 
1799 	flow_ctrl_params->rx_fifo_th_high = AL_ETH_FLOW_CTRL_RX_FIFO_TH_HIGH;
1800 	flow_ctrl_params->rx_fifo_th_low = AL_ETH_FLOW_CTRL_RX_FIFO_TH_LOW;
1801 	flow_ctrl_params->quanta = AL_ETH_FLOW_CTRL_QUANTA;
1802 	flow_ctrl_params->quanta_th = AL_ETH_FLOW_CTRL_QUANTA_TH;
1803 
1804 	/* map priority to queue index, queue id = priority/2 */
1805 	for (i = 0; i < AL_ETH_FWD_PRIO_TABLE_NUM; i++)
1806 		flow_ctrl_params->prio_q_map[0][i] =  1 << (i >> 1);
1807 
1808 	al_eth_flow_control_config(&adapter->hal_adapter, flow_ctrl_params);
1809 
1810 	return (0);
1811 }
1812 
1813 static void
1814 al_eth_flow_ctrl_enable(struct al_eth_adapter *adapter)
1815 {
1816 
1817 	/*
1818 	 * change the active configuration to the default / force by ethtool
1819 	 * and call to configure
1820 	 */
1821 	adapter->link_config.flow_ctrl_active =
1822 	    adapter->link_config.flow_ctrl_supported;
1823 
1824 	al_eth_flow_ctrl_config(adapter);
1825 }
1826 
1827 static void
1828 al_eth_flow_ctrl_disable(struct al_eth_adapter *adapter)
1829 {
1830 
1831 	adapter->link_config.flow_ctrl_active = 0;
1832 	al_eth_flow_ctrl_config(adapter);
1833 }
1834 
1835 static int
1836 al_eth_hw_init(struct al_eth_adapter *adapter)
1837 {
1838 	int rc;
1839 
1840 	rc = al_eth_hw_init_adapter(adapter);
1841 	if (rc != 0)
1842 		return (rc);
1843 
1844 	rc = al_eth_mac_config(&adapter->hal_adapter, adapter->mac_mode);
1845 	if (rc < 0) {
1846 		device_printf(adapter->dev, "%s failed to configure mac!\n",
1847 		    __func__);
1848 		return (rc);
1849 	}
1850 
1851 	if ((adapter->mac_mode == AL_ETH_MAC_MODE_SGMII) ||
1852 	    (adapter->mac_mode == AL_ETH_MAC_MODE_RGMII &&
1853 	     adapter->phy_exist == FALSE)) {
1854 		rc = al_eth_mac_link_config(&adapter->hal_adapter,
1855 		    adapter->link_config.force_1000_base_x,
1856 		    adapter->link_config.autoneg,
1857 		    adapter->link_config.active_speed,
1858 		    adapter->link_config.active_duplex);
1859 		if (rc != 0) {
1860 			device_printf(adapter->dev,
1861 			    "%s failed to configure link parameters!\n",
1862 			    __func__);
1863 			return (rc);
1864 		}
1865 	}
1866 
1867 	rc = al_eth_mdio_config(&adapter->hal_adapter,
1868 	    AL_ETH_MDIO_TYPE_CLAUSE_22, TRUE /* shared_mdio_if */,
1869 	    adapter->ref_clk_freq, adapter->mdio_freq);
1870 	if (rc != 0) {
1871 		device_printf(adapter->dev, "%s failed at mdio config!\n",
1872 		    __func__);
1873 		return (rc);
1874 	}
1875 
1876 	al_eth_flow_ctrl_init(adapter);
1877 
1878 	return (rc);
1879 }
1880 
1881 static int
1882 al_eth_hw_stop(struct al_eth_adapter *adapter)
1883 {
1884 
1885 	al_eth_mac_stop(&adapter->hal_adapter);
1886 
1887 	/*
1888 	 * wait till pending rx packets written and UDMA becomes idle,
1889 	 * the MAC has ~10KB fifo, 10us should be enought time for the
1890 	 * UDMA to write to the memory
1891 	 */
1892 	DELAY(10);
1893 
1894 	al_eth_adapter_stop(&adapter->hal_adapter);
1895 
1896 	adapter->flags |= AL_ETH_FLAG_RESET_REQUESTED;
1897 
1898 	/* disable flow ctrl to avoid pause packets*/
1899 	al_eth_flow_ctrl_disable(adapter);
1900 
1901 	return (0);
1902 }
1903 
1904 /*
1905  * al_eth_intr_intx_all - Legacy Interrupt Handler for all interrupts
1906  * @irq: interrupt number
1907  * @data: pointer to a network interface device structure
1908  */
1909 static int
1910 al_eth_intr_intx_all(void *data)
1911 {
1912 	struct al_eth_adapter *adapter = data;
1913 
1914 	struct unit_regs __iomem *regs_base =
1915 	    (struct unit_regs __iomem *)adapter->udma_base;
1916 	uint32_t reg;
1917 
1918 	reg = al_udma_iofic_read_cause(regs_base, AL_UDMA_IOFIC_LEVEL_PRIMARY,
1919 	    AL_INT_GROUP_A);
1920 	if (likely(reg))
1921 		device_printf_dbg(adapter->dev, "%s group A cause %x\n",
1922 		    __func__, reg);
1923 
1924 	if (unlikely(reg & AL_INT_GROUP_A_GROUP_D_SUM)) {
1925 		struct al_iofic_grp_ctrl __iomem *sec_ints_base;
1926 		uint32_t cause_d =  al_udma_iofic_read_cause(regs_base,
1927 		    AL_UDMA_IOFIC_LEVEL_PRIMARY, AL_INT_GROUP_D);
1928 
1929 		sec_ints_base =
1930 		    &regs_base->gen.interrupt_regs.secondary_iofic_ctrl[0];
1931 		if (cause_d != 0) {
1932 			device_printf_dbg(adapter->dev,
1933 			    "got interrupt from group D. cause %x\n", cause_d);
1934 
1935 			cause_d = al_iofic_read_cause(sec_ints_base,
1936 			    AL_INT_GROUP_A);
1937 			device_printf(adapter->dev,
1938 			    "secondary A cause %x\n", cause_d);
1939 
1940 			cause_d = al_iofic_read_cause(sec_ints_base,
1941 			    AL_INT_GROUP_B);
1942 
1943 			device_printf_dbg(adapter->dev,
1944 			    "secondary B cause %x\n", cause_d);
1945 		}
1946 	}
1947 	if ((reg & AL_INT_GROUP_A_GROUP_B_SUM) != 0 ) {
1948 		uint32_t cause_b = al_udma_iofic_read_cause(regs_base,
1949 		    AL_UDMA_IOFIC_LEVEL_PRIMARY, AL_INT_GROUP_B);
1950 		int qid;
1951 		device_printf_dbg(adapter->dev, "secondary B cause %x\n",
1952 		    cause_b);
1953 		for (qid = 0; qid < adapter->num_rx_queues; qid++) {
1954 			if (cause_b & (1 << qid)) {
1955 				/* mask */
1956 				al_udma_iofic_mask(
1957 				    (struct unit_regs __iomem *)adapter->udma_base,
1958 				    AL_UDMA_IOFIC_LEVEL_PRIMARY,
1959 				    AL_INT_GROUP_B, 1 << qid);
1960 			}
1961 		}
1962 	}
1963 	if ((reg & AL_INT_GROUP_A_GROUP_C_SUM) != 0) {
1964 		uint32_t cause_c = al_udma_iofic_read_cause(regs_base,
1965 		    AL_UDMA_IOFIC_LEVEL_PRIMARY, AL_INT_GROUP_C);
1966 		int qid;
1967 		device_printf_dbg(adapter->dev, "secondary C cause %x\n", cause_c);
1968 		for (qid = 0; qid < adapter->num_tx_queues; qid++) {
1969 			if ((cause_c & (1 << qid)) != 0) {
1970 				al_udma_iofic_mask(
1971 				    (struct unit_regs __iomem *)adapter->udma_base,
1972 				    AL_UDMA_IOFIC_LEVEL_PRIMARY,
1973 				    AL_INT_GROUP_C, 1 << qid);
1974 			}
1975 		}
1976 	}
1977 
1978 	al_eth_tx_cmlp_irq_filter(adapter->tx_ring);
1979 
1980 	return (0);
1981 }
1982 
1983 static int
1984 al_eth_intr_msix_all(void *data)
1985 {
1986 	struct al_eth_adapter *adapter = data;
1987 
1988 	device_printf_dbg(adapter->dev, "%s\n", __func__);
1989 	return (0);
1990 }
1991 
1992 static int
1993 al_eth_intr_msix_mgmt(void *data)
1994 {
1995 	struct al_eth_adapter *adapter = data;
1996 
1997 	device_printf_dbg(adapter->dev, "%s\n", __func__);
1998 	return (0);
1999 }
2000 
2001 static int
2002 al_eth_enable_msix(struct al_eth_adapter *adapter)
2003 {
2004 	int i, msix_vecs, rc, count;
2005 
2006 	device_printf_dbg(adapter->dev, "%s\n", __func__);
2007 	msix_vecs = 1 + adapter->num_rx_queues + adapter->num_tx_queues;
2008 
2009 	device_printf_dbg(adapter->dev,
2010 	    "Try to enable MSIX, vector numbers = %d\n", msix_vecs);
2011 
2012 	adapter->msix_entries = malloc(msix_vecs*sizeof(*adapter->msix_entries),
2013 	    M_IFAL, M_ZERO | M_WAITOK);
2014 
2015 	if (adapter->msix_entries == NULL) {
2016 		device_printf_dbg(adapter->dev, "failed to allocate"
2017 		    " msix_entries %d\n", msix_vecs);
2018 		rc = ENOMEM;
2019 		goto exit;
2020 	}
2021 
2022 	/* management vector (GROUP_A) @2*/
2023 	adapter->msix_entries[AL_ETH_MGMT_IRQ_IDX].entry = 2;
2024 	adapter->msix_entries[AL_ETH_MGMT_IRQ_IDX].vector = 0;
2025 
2026 	/* rx queues start @3 */
2027 	for (i = 0; i < adapter->num_rx_queues; i++) {
2028 		int irq_idx = AL_ETH_RXQ_IRQ_IDX(adapter, i);
2029 
2030 		adapter->msix_entries[irq_idx].entry = 3 + i;
2031 		adapter->msix_entries[irq_idx].vector = 0;
2032 	}
2033 	/* tx queues start @7 */
2034 	for (i = 0; i < adapter->num_tx_queues; i++) {
2035 		int irq_idx = AL_ETH_TXQ_IRQ_IDX(adapter, i);
2036 
2037 		adapter->msix_entries[irq_idx].entry = 3 +
2038 		    AL_ETH_MAX_HW_QUEUES + i;
2039 		adapter->msix_entries[irq_idx].vector = 0;
2040 	}
2041 
2042 	count = msix_vecs + 2; /* entries start from 2 */
2043 	rc = pci_alloc_msix(adapter->dev, &count);
2044 
2045 	if (rc != 0) {
2046 		device_printf_dbg(adapter->dev, "failed to allocate MSIX "
2047 		    "vectors %d\n", msix_vecs+2);
2048 		device_printf_dbg(adapter->dev, "ret = %d\n", rc);
2049 		goto msix_entries_exit;
2050 	}
2051 
2052 	if (count != msix_vecs + 2) {
2053 		device_printf_dbg(adapter->dev, "failed to allocate all MSIX "
2054 		    "vectors %d, allocated %d\n", msix_vecs+2, count);
2055 		rc = ENOSPC;
2056 		goto msix_entries_exit;
2057 	}
2058 
2059 	for (i = 0; i < msix_vecs; i++)
2060 	    adapter->msix_entries[i].vector = 2 + 1 + i;
2061 
2062 	device_printf_dbg(adapter->dev, "successfully enabled MSIX,"
2063 	    " vectors %d\n", msix_vecs);
2064 
2065 	adapter->msix_vecs = msix_vecs;
2066 	adapter->flags |= AL_ETH_FLAG_MSIX_ENABLED;
2067 	goto exit;
2068 
2069 msix_entries_exit:
2070 	adapter->msix_vecs = 0;
2071 	free(adapter->msix_entries, M_IFAL);
2072 	adapter->msix_entries = NULL;
2073 
2074 exit:
2075 	return (rc);
2076 }
2077 
2078 static int
2079 al_eth_setup_int_mode(struct al_eth_adapter *adapter)
2080 {
2081 	int i, rc;
2082 
2083 	rc = al_eth_enable_msix(adapter);
2084 	if (rc != 0) {
2085 		device_printf(adapter->dev, "Failed to enable MSIX mode.\n");
2086 		return (rc);
2087 	}
2088 
2089 	adapter->irq_vecs = max(1, adapter->msix_vecs);
2090 	/* single INTX mode */
2091 	if (adapter->msix_vecs == 0) {
2092 		snprintf(adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].name,
2093 		    AL_ETH_IRQNAME_SIZE, "al-eth-intx-all@pci:%s",
2094 		    device_get_name(adapter->dev));
2095 		adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].handler =
2096 		    al_eth_intr_intx_all;
2097 		/* IRQ vector will be resolved from device resources */
2098 		adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].vector = 0;
2099 		adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].data = adapter;
2100 
2101 		device_printf(adapter->dev, "%s and vector %d \n", __func__,
2102 		    adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].vector);
2103 
2104 		return (0);
2105 	}
2106 	/* single MSI-X mode */
2107 	if (adapter->msix_vecs == 1) {
2108 		snprintf(adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].name,
2109 		    AL_ETH_IRQNAME_SIZE, "al-eth-msix-all@pci:%s",
2110 		    device_get_name(adapter->dev));
2111 		adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].handler =
2112 		    al_eth_intr_msix_all;
2113 		adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].vector =
2114 		    adapter->msix_entries[AL_ETH_MGMT_IRQ_IDX].vector;
2115 		adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].data = adapter;
2116 
2117 		return (0);
2118 	}
2119 	/* MSI-X per queue */
2120 	snprintf(adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].name, AL_ETH_IRQNAME_SIZE,
2121 	    "al-eth-msix-mgmt@pci:%s", device_get_name(adapter->dev));
2122 	adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].handler = al_eth_intr_msix_mgmt;
2123 
2124 	adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].data = adapter;
2125 	adapter->irq_tbl[AL_ETH_MGMT_IRQ_IDX].vector =
2126 	    adapter->msix_entries[AL_ETH_MGMT_IRQ_IDX].vector;
2127 
2128 	for (i = 0; i < adapter->num_rx_queues; i++) {
2129 		int irq_idx = AL_ETH_RXQ_IRQ_IDX(adapter, i);
2130 
2131 		snprintf(adapter->irq_tbl[irq_idx].name, AL_ETH_IRQNAME_SIZE,
2132 		    "al-eth-rx-comp-%d@pci:%s", i,
2133 		    device_get_name(adapter->dev));
2134 		adapter->irq_tbl[irq_idx].handler = al_eth_rx_recv_irq_filter;
2135 		adapter->irq_tbl[irq_idx].data = &adapter->rx_ring[i];
2136 		adapter->irq_tbl[irq_idx].vector =
2137 		    adapter->msix_entries[irq_idx].vector;
2138 	}
2139 
2140 	for (i = 0; i < adapter->num_tx_queues; i++) {
2141 		int irq_idx = AL_ETH_TXQ_IRQ_IDX(adapter, i);
2142 
2143 		snprintf(adapter->irq_tbl[irq_idx].name,
2144 		    AL_ETH_IRQNAME_SIZE, "al-eth-tx-comp-%d@pci:%s", i,
2145 		    device_get_name(adapter->dev));
2146 		adapter->irq_tbl[irq_idx].handler = al_eth_tx_cmlp_irq_filter;
2147 		adapter->irq_tbl[irq_idx].data = &adapter->tx_ring[i];
2148 		adapter->irq_tbl[irq_idx].vector =
2149 		    adapter->msix_entries[irq_idx].vector;
2150 	}
2151 
2152 	return (0);
2153 }
2154 
2155 static void
2156 __al_eth_free_irq(struct al_eth_adapter *adapter)
2157 {
2158 	struct al_eth_irq *irq;
2159 	int i, rc;
2160 
2161 	for (i = 0; i < adapter->irq_vecs; i++) {
2162 		irq = &adapter->irq_tbl[i];
2163 		if (irq->requested != 0) {
2164 			device_printf_dbg(adapter->dev, "tear down irq: %d\n",
2165 			    irq->vector);
2166 			rc = bus_teardown_intr(adapter->dev, irq->res,
2167 			    irq->cookie);
2168 			if (rc != 0)
2169 				device_printf(adapter->dev, "failed to tear "
2170 				    "down irq: %d\n", irq->vector);
2171 
2172 		}
2173 		irq->requested = 0;
2174 	}
2175 }
2176 
2177 static void
2178 al_eth_free_irq(struct al_eth_adapter *adapter)
2179 {
2180 	struct al_eth_irq *irq;
2181 	int i, rc;
2182 #ifdef CONFIG_RFS_ACCEL
2183 	if (adapter->msix_vecs >= 1) {
2184 		free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
2185 		adapter->netdev->rx_cpu_rmap = NULL;
2186 	}
2187 #endif
2188 
2189 	__al_eth_free_irq(adapter);
2190 
2191 	for (i = 0; i < adapter->irq_vecs; i++) {
2192 		irq = &adapter->irq_tbl[i];
2193 		if (irq->res == NULL)
2194 			continue;
2195 		device_printf_dbg(adapter->dev, "release resource irq: %d\n",
2196 		    irq->vector);
2197 		rc = bus_release_resource(adapter->dev, SYS_RES_IRQ, irq->vector,
2198 		    irq->res);
2199 		irq->res = NULL;
2200 		if (rc != 0)
2201 			device_printf(adapter->dev, "dev has no parent while "
2202 			    "releasing res for irq: %d\n", irq->vector);
2203 	}
2204 
2205 	pci_release_msi(adapter->dev);
2206 
2207 	adapter->flags &= ~AL_ETH_FLAG_MSIX_ENABLED;
2208 
2209 	adapter->msix_vecs = 0;
2210 	free(adapter->msix_entries, M_IFAL);
2211 	adapter->msix_entries = NULL;
2212 }
2213 
2214 static int
2215 al_eth_request_irq(struct al_eth_adapter *adapter)
2216 {
2217 	unsigned long flags;
2218 	struct al_eth_irq *irq;
2219 	int rc = 0, i, v;
2220 
2221 	if ((adapter->flags & AL_ETH_FLAG_MSIX_ENABLED) != 0)
2222 		flags = RF_ACTIVE;
2223 	else
2224 		flags = RF_ACTIVE | RF_SHAREABLE;
2225 
2226 	for (i = 0; i < adapter->irq_vecs; i++) {
2227 		irq = &adapter->irq_tbl[i];
2228 
2229 		if (irq->requested != 0)
2230 			continue;
2231 
2232 		irq->res = bus_alloc_resource_any(adapter->dev, SYS_RES_IRQ,
2233 		    &irq->vector, flags);
2234 		if (irq->res == NULL) {
2235 			device_printf(adapter->dev, "could not allocate "
2236 			    "irq vector=%d\n", irq->vector);
2237 			rc = ENXIO;
2238 			goto exit_res;
2239 		}
2240 
2241 		if ((rc = bus_setup_intr(adapter->dev, irq->res,
2242 		    INTR_TYPE_NET | INTR_MPSAFE, irq->handler,
2243 		    NULL, irq->data, &irq->cookie)) != 0) {
2244 			device_printf(adapter->dev, "failed to register "
2245 			    "interrupt handler for irq %ju: %d\n",
2246 			    (uintmax_t)rman_get_start(irq->res), rc);
2247 			goto exit_intr;
2248 		}
2249 		irq->requested = 1;
2250 	}
2251 	goto exit;
2252 
2253 exit_intr:
2254 	v = i - 1; /* -1 because we omit the operation that failed */
2255 	while (v-- >= 0) {
2256 		int bti;
2257 		irq = &adapter->irq_tbl[v];
2258 		bti = bus_teardown_intr(adapter->dev, irq->res, irq->cookie);
2259 		if (bti != 0) {
2260 			device_printf(adapter->dev, "failed to tear "
2261 			    "down irq: %d\n", irq->vector);
2262 		}
2263 
2264 		irq->requested = 0;
2265 		device_printf_dbg(adapter->dev, "exit_intr: releasing irq %d\n",
2266 		    irq->vector);
2267 	}
2268 
2269 exit_res:
2270 	v = i - 1; /* -1 because we omit the operation that failed */
2271 	while (v-- >= 0) {
2272 		int brr;
2273 		irq = &adapter->irq_tbl[v];
2274 		device_printf_dbg(adapter->dev, "exit_res: releasing resource"
2275 		    " for irq %d\n", irq->vector);
2276 		brr = bus_release_resource(adapter->dev, SYS_RES_IRQ,
2277 		    irq->vector, irq->res);
2278 		if (brr != 0)
2279 			device_printf(adapter->dev, "dev has no parent while "
2280 			    "releasing res for irq: %d\n", irq->vector);
2281 		irq->res = NULL;
2282 	}
2283 
2284 exit:
2285 	return (rc);
2286 }
2287 
2288 /**
2289  * al_eth_setup_tx_resources - allocate Tx resources (Descriptors)
2290  * @adapter: network interface device structure
2291  * @qid: queue index
2292  *
2293  * Return 0 on success, negative on failure
2294  **/
2295 static int
2296 al_eth_setup_tx_resources(struct al_eth_adapter *adapter, int qid)
2297 {
2298 	struct al_eth_ring *tx_ring = &adapter->tx_ring[qid];
2299 	struct device *dev = tx_ring->dev;
2300 	struct al_udma_q_params *q_params = &tx_ring->q_params;
2301 	int size;
2302 	int ret;
2303 
2304 	if (adapter->up)
2305 		return (0);
2306 
2307 	size = sizeof(struct al_eth_tx_buffer) * tx_ring->sw_count;
2308 
2309 	tx_ring->tx_buffer_info = malloc(size, M_IFAL, M_ZERO | M_WAITOK);
2310 	if (tx_ring->tx_buffer_info == NULL)
2311 		return (ENOMEM);
2312 
2313 	tx_ring->descs_size = tx_ring->hw_count * sizeof(union al_udma_desc);
2314 	q_params->size = tx_ring->hw_count;
2315 
2316 	ret = al_dma_alloc_coherent(dev, &q_params->desc_phy_base_tag,
2317 	    (bus_dmamap_t *)&q_params->desc_phy_base_map,
2318 	    (bus_addr_t *)&q_params->desc_phy_base,
2319 	    (void**)&q_params->desc_base, tx_ring->descs_size);
2320 	if (ret != 0) {
2321 		device_printf(dev, "failed to al_dma_alloc_coherent,"
2322 		    " ret = %d\n", ret);
2323 		return (ENOMEM);
2324 	}
2325 
2326 	if (q_params->desc_base == NULL)
2327 		return (ENOMEM);
2328 
2329 	device_printf_dbg(dev, "Initializing ring queues %d\n", qid);
2330 
2331 	/* Allocate Ring Queue */
2332 	mtx_init(&tx_ring->br_mtx, "AlRingMtx", NULL, MTX_DEF);
2333 	tx_ring->br = buf_ring_alloc(AL_BR_SIZE, M_DEVBUF, M_WAITOK,
2334 	    &tx_ring->br_mtx);
2335 	if (tx_ring->br == NULL) {
2336 		device_printf(dev, "Critical Failure setting up buf ring\n");
2337 		return (ENOMEM);
2338 	}
2339 
2340 	/* Allocate taskqueues */
2341 	TASK_INIT(&tx_ring->enqueue_task, 0, al_eth_start_xmit, tx_ring);
2342 	tx_ring->enqueue_tq = taskqueue_create_fast("al_tx_enque", M_NOWAIT,
2343 	    taskqueue_thread_enqueue, &tx_ring->enqueue_tq);
2344 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, PI_NET, "%s txeq",
2345 	    device_get_nameunit(adapter->dev));
2346 	TASK_INIT(&tx_ring->cmpl_task, 0, al_eth_tx_cmpl_work, tx_ring);
2347 	tx_ring->cmpl_tq = taskqueue_create_fast("al_tx_cmpl", M_NOWAIT,
2348 	    taskqueue_thread_enqueue, &tx_ring->cmpl_tq);
2349 	taskqueue_start_threads(&tx_ring->cmpl_tq, 1, PI_REALTIME, "%s txcq",
2350 	    device_get_nameunit(adapter->dev));
2351 
2352 	/* Setup DMA descriptor areas. */
2353 	ret = bus_dma_tag_create(bus_get_dma_tag(dev),
2354 	    1, 0,			/* alignment, bounds */
2355 	    BUS_SPACE_MAXADDR,		/* lowaddr */
2356 	    BUS_SPACE_MAXADDR,		/* highaddr */
2357 	    NULL, NULL,			/* filter, filterarg */
2358 	    AL_TSO_SIZE,		/* maxsize */
2359 	    AL_ETH_PKT_MAX_BUFS,	/* nsegments */
2360 	    PAGE_SIZE,			/* maxsegsize */
2361 	    0,				/* flags */
2362 	    NULL,			/* lockfunc */
2363 	    NULL,			/* lockfuncarg */
2364 	    &tx_ring->dma_buf_tag);
2365 
2366 	if (ret != 0) {
2367 		device_printf(dev,"Unable to allocate dma_buf_tag, ret = %d\n",
2368 		    ret);
2369 		return (ret);
2370 	}
2371 
2372 	for (size = 0; size < tx_ring->sw_count; size++) {
2373 		ret = bus_dmamap_create(tx_ring->dma_buf_tag, 0,
2374 		    &tx_ring->tx_buffer_info[size].dma_map);
2375 		if (ret != 0) {
2376 			device_printf(dev, "Unable to map DMA TX "
2377 			    "buffer memory [iter=%d]\n", size);
2378 			return (ret);
2379 		}
2380 	}
2381 
2382 	/* completion queue not used for tx */
2383 	q_params->cdesc_base = NULL;
2384 	/* size in bytes of the udma completion ring descriptor */
2385 	q_params->cdesc_size = 8;
2386 	tx_ring->next_to_use = 0;
2387 	tx_ring->next_to_clean = 0;
2388 
2389 	return (0);
2390 }
2391 
2392 /*
2393  * al_eth_free_tx_resources - Free Tx Resources per Queue
2394  * @adapter: network interface device structure
2395  * @qid: queue index
2396  *
2397  * Free all transmit software resources
2398  */
2399 static void
2400 al_eth_free_tx_resources(struct al_eth_adapter *adapter, int qid)
2401 {
2402 	struct al_eth_ring *tx_ring = &adapter->tx_ring[qid];
2403 	struct al_udma_q_params *q_params = &tx_ring->q_params;
2404 	int size;
2405 
2406 	/* At this point interrupts' handlers must be deactivated */
2407 	while (taskqueue_cancel(tx_ring->cmpl_tq, &tx_ring->cmpl_task, NULL))
2408 		taskqueue_drain(tx_ring->cmpl_tq, &tx_ring->cmpl_task);
2409 
2410 	taskqueue_free(tx_ring->cmpl_tq);
2411 	while (taskqueue_cancel(tx_ring->enqueue_tq,
2412 	    &tx_ring->enqueue_task, NULL)) {
2413 		taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
2414 	}
2415 
2416 	taskqueue_free(tx_ring->enqueue_tq);
2417 
2418 	if (tx_ring->br != NULL) {
2419 		drbr_flush(adapter->netdev, tx_ring->br);
2420 		buf_ring_free(tx_ring->br, M_DEVBUF);
2421 	}
2422 
2423 	for (size = 0; size < tx_ring->sw_count; size++) {
2424 		m_freem(tx_ring->tx_buffer_info[size].m);
2425 		tx_ring->tx_buffer_info[size].m = NULL;
2426 
2427 		bus_dmamap_unload(tx_ring->dma_buf_tag,
2428 		    tx_ring->tx_buffer_info[size].dma_map);
2429 		bus_dmamap_destroy(tx_ring->dma_buf_tag,
2430 		    tx_ring->tx_buffer_info[size].dma_map);
2431 	}
2432 	bus_dma_tag_destroy(tx_ring->dma_buf_tag);
2433 
2434 	free(tx_ring->tx_buffer_info, M_IFAL);
2435 	tx_ring->tx_buffer_info = NULL;
2436 
2437 	mtx_destroy(&tx_ring->br_mtx);
2438 
2439 	/* if not set, then don't free */
2440 	if (q_params->desc_base == NULL)
2441 		return;
2442 
2443 	al_dma_free_coherent(q_params->desc_phy_base_tag,
2444 	    q_params->desc_phy_base_map, q_params->desc_base);
2445 
2446 	q_params->desc_base = NULL;
2447 }
2448 
2449 /*
2450  * al_eth_free_all_tx_resources - Free Tx Resources for All Queues
2451  * @adapter: board private structure
2452  *
2453  * Free all transmit software resources
2454  */
2455 static void
2456 al_eth_free_all_tx_resources(struct al_eth_adapter *adapter)
2457 {
2458 	int i;
2459 
2460 	for (i = 0; i < adapter->num_tx_queues; i++)
2461 		if (adapter->tx_ring[i].q_params.desc_base)
2462 			al_eth_free_tx_resources(adapter, i);
2463 }
2464 
2465 /*
2466  * al_eth_setup_rx_resources - allocate Rx resources (Descriptors)
2467  * @adapter: network interface device structure
2468  * @qid: queue index
2469  *
2470  * Returns 0 on success, negative on failure
2471  */
2472 static int
2473 al_eth_setup_rx_resources(struct al_eth_adapter *adapter, unsigned int qid)
2474 {
2475 	struct al_eth_ring *rx_ring = &adapter->rx_ring[qid];
2476 	struct device *dev = rx_ring->dev;
2477 	struct al_udma_q_params *q_params = &rx_ring->q_params;
2478 	int size;
2479 	int ret;
2480 
2481 	size = sizeof(struct al_eth_rx_buffer) * rx_ring->sw_count;
2482 
2483 	/* alloc extra element so in rx path we can always prefetch rx_info + 1 */
2484 	size += 1;
2485 
2486 	rx_ring->rx_buffer_info = malloc(size, M_IFAL, M_ZERO | M_WAITOK);
2487 	if (rx_ring->rx_buffer_info == NULL)
2488 		return (ENOMEM);
2489 
2490 	rx_ring->descs_size = rx_ring->hw_count * sizeof(union al_udma_desc);
2491 	q_params->size = rx_ring->hw_count;
2492 
2493 	ret = al_dma_alloc_coherent(dev, &q_params->desc_phy_base_tag,
2494 	    &q_params->desc_phy_base_map,
2495 	    (bus_addr_t *)&q_params->desc_phy_base,
2496 	    (void**)&q_params->desc_base, rx_ring->descs_size);
2497 
2498 	if ((q_params->desc_base == NULL) || (ret != 0))
2499 		return (ENOMEM);
2500 
2501 	/* size in bytes of the udma completion ring descriptor */
2502 	q_params->cdesc_size = 16;
2503 	rx_ring->cdescs_size = rx_ring->hw_count * q_params->cdesc_size;
2504 	ret = al_dma_alloc_coherent(dev, &q_params->cdesc_phy_base_tag,
2505 	    &q_params->cdesc_phy_base_map,
2506 	    (bus_addr_t *)&q_params->cdesc_phy_base,
2507 	    (void**)&q_params->cdesc_base, rx_ring->cdescs_size);
2508 
2509 	if ((q_params->cdesc_base == NULL) || (ret != 0))
2510 		return (ENOMEM);
2511 
2512 	/* Allocate taskqueues */
2513 	NET_TASK_INIT(&rx_ring->enqueue_task, 0, al_eth_rx_recv_work, rx_ring);
2514 	rx_ring->enqueue_tq = taskqueue_create_fast("al_rx_enque", M_NOWAIT,
2515 	    taskqueue_thread_enqueue, &rx_ring->enqueue_tq);
2516 	taskqueue_start_threads(&rx_ring->enqueue_tq, 1, PI_NET, "%s rxeq",
2517 	    device_get_nameunit(adapter->dev));
2518 
2519 	/* Setup DMA descriptor areas. */
2520 	ret = bus_dma_tag_create(bus_get_dma_tag(dev),
2521 	    1, 0,			/* alignment, bounds */
2522 	    BUS_SPACE_MAXADDR,		/* lowaddr */
2523 	    BUS_SPACE_MAXADDR,		/* highaddr */
2524 	    NULL, NULL,			/* filter, filterarg */
2525 	    AL_TSO_SIZE,		/* maxsize */
2526 	    1,				/* nsegments */
2527 	    AL_TSO_SIZE,		/* maxsegsize */
2528 	    0,				/* flags */
2529 	    NULL,			/* lockfunc */
2530 	    NULL,			/* lockfuncarg */
2531 	    &rx_ring->dma_buf_tag);
2532 
2533 	if (ret != 0) {
2534 		device_printf(dev,"Unable to allocate RX dma_buf_tag\n");
2535 		return (ret);
2536 	}
2537 
2538 	for (size = 0; size < rx_ring->sw_count; size++) {
2539 		ret = bus_dmamap_create(rx_ring->dma_buf_tag, 0,
2540 		    &rx_ring->rx_buffer_info[size].dma_map);
2541 		if (ret != 0) {
2542 			device_printf(dev,"Unable to map DMA RX buffer memory\n");
2543 			return (ret);
2544 		}
2545 	}
2546 
2547 	/* Zero out the descriptor ring */
2548 	memset(q_params->cdesc_base, 0, rx_ring->cdescs_size);
2549 
2550 	/* Create LRO for the ring */
2551 	if ((adapter->netdev->if_capenable & IFCAP_LRO) != 0) {
2552 		int err = tcp_lro_init(&rx_ring->lro);
2553 		if (err != 0) {
2554 			device_printf(adapter->dev,
2555 			    "LRO[%d] Initialization failed!\n", qid);
2556 		} else {
2557 			device_printf_dbg(adapter->dev,
2558 			    "RX Soft LRO[%d] Initialized\n", qid);
2559 			rx_ring->lro_enabled = TRUE;
2560 			rx_ring->lro.ifp = adapter->netdev;
2561 		}
2562 	}
2563 
2564 	rx_ring->next_to_clean = 0;
2565 	rx_ring->next_to_use = 0;
2566 
2567 	return (0);
2568 }
2569 
2570 /*
2571  * al_eth_free_rx_resources - Free Rx Resources
2572  * @adapter: network interface device structure
2573  * @qid: queue index
2574  *
2575  * Free all receive software resources
2576  */
2577 static void
2578 al_eth_free_rx_resources(struct al_eth_adapter *adapter, unsigned int qid)
2579 {
2580 	struct al_eth_ring *rx_ring = &adapter->rx_ring[qid];
2581 	struct al_udma_q_params *q_params = &rx_ring->q_params;
2582 	int size;
2583 
2584 	/* At this point interrupts' handlers must be deactivated */
2585 	while (taskqueue_cancel(rx_ring->enqueue_tq,
2586 	    &rx_ring->enqueue_task, NULL)) {
2587 		taskqueue_drain(rx_ring->enqueue_tq, &rx_ring->enqueue_task);
2588 	}
2589 
2590 	taskqueue_free(rx_ring->enqueue_tq);
2591 
2592 	for (size = 0; size < rx_ring->sw_count; size++) {
2593 		m_freem(rx_ring->rx_buffer_info[size].m);
2594 		rx_ring->rx_buffer_info[size].m = NULL;
2595 		bus_dmamap_unload(rx_ring->dma_buf_tag,
2596 		    rx_ring->rx_buffer_info[size].dma_map);
2597 		bus_dmamap_destroy(rx_ring->dma_buf_tag,
2598 		    rx_ring->rx_buffer_info[size].dma_map);
2599 	}
2600 	bus_dma_tag_destroy(rx_ring->dma_buf_tag);
2601 
2602 	free(rx_ring->rx_buffer_info, M_IFAL);
2603 	rx_ring->rx_buffer_info = NULL;
2604 
2605 	/* if not set, then don't free */
2606 	if (q_params->desc_base == NULL)
2607 		return;
2608 
2609 	al_dma_free_coherent(q_params->desc_phy_base_tag,
2610 	    q_params->desc_phy_base_map, q_params->desc_base);
2611 
2612 	q_params->desc_base = NULL;
2613 
2614 	/* if not set, then don't free */
2615 	if (q_params->cdesc_base == NULL)
2616 		return;
2617 
2618 	al_dma_free_coherent(q_params->cdesc_phy_base_tag,
2619 	    q_params->cdesc_phy_base_map, q_params->cdesc_base);
2620 
2621 	q_params->cdesc_phy_base = 0;
2622 
2623 	/* Free LRO resources */
2624 	tcp_lro_free(&rx_ring->lro);
2625 }
2626 
2627 /*
2628  * al_eth_free_all_rx_resources - Free Rx Resources for All Queues
2629  * @adapter: board private structure
2630  *
2631  * Free all receive software resources
2632  */
2633 static void
2634 al_eth_free_all_rx_resources(struct al_eth_adapter *adapter)
2635 {
2636 	int i;
2637 
2638 	for (i = 0; i < adapter->num_rx_queues; i++)
2639 		if (adapter->rx_ring[i].q_params.desc_base != 0)
2640 			al_eth_free_rx_resources(adapter, i);
2641 }
2642 
2643 /*
2644  * al_eth_setup_all_rx_resources - allocate all queues Rx resources
2645  * @adapter: board private structure
2646  *
2647  * Return 0 on success, negative on failure
2648  */
2649 static int
2650 al_eth_setup_all_rx_resources(struct al_eth_adapter *adapter)
2651 {
2652 	int i, rc = 0;
2653 
2654 	for (i = 0; i < adapter->num_rx_queues; i++) {
2655 		rc = al_eth_setup_rx_resources(adapter, i);
2656 		if (rc == 0)
2657 			continue;
2658 
2659 		device_printf(adapter->dev, "Allocation for Rx Queue %u failed\n", i);
2660 		goto err_setup_rx;
2661 	}
2662 	return (0);
2663 
2664 err_setup_rx:
2665 	/* rewind the index freeing the rings as we go */
2666 	while (i--)
2667 		al_eth_free_rx_resources(adapter, i);
2668 	return (rc);
2669 }
2670 
2671 /*
2672  * al_eth_setup_all_tx_resources - allocate all queues Tx resources
2673  * @adapter: private structure
2674  *
2675  * Return 0 on success, negative on failure
2676  */
2677 static int
2678 al_eth_setup_all_tx_resources(struct al_eth_adapter *adapter)
2679 {
2680 	int i, rc = 0;
2681 
2682 	for (i = 0; i < adapter->num_tx_queues; i++) {
2683 		rc = al_eth_setup_tx_resources(adapter, i);
2684 		if (rc == 0)
2685 			continue;
2686 
2687 		device_printf(adapter->dev,
2688 		    "Allocation for Tx Queue %u failed\n", i);
2689 		goto err_setup_tx;
2690 	}
2691 
2692 	return (0);
2693 
2694 err_setup_tx:
2695 	/* rewind the index freeing the rings as we go */
2696 	while (i--)
2697 		al_eth_free_tx_resources(adapter, i);
2698 
2699 	return (rc);
2700 }
2701 
2702 static void
2703 al_eth_disable_int_sync(struct al_eth_adapter *adapter)
2704 {
2705 
2706 	/* disable forwarding interrupts from eth through pci end point */
2707 	if ((adapter->board_type == ALPINE_FPGA_NIC) ||
2708 	    (adapter->board_type == ALPINE_NIC)) {
2709 		al_eth_forward_int_config((uint32_t*)adapter->internal_pcie_base +
2710 		    AL_REG_OFFSET_FORWARD_INTR, AL_DIS_FORWARD_INTR);
2711 	}
2712 
2713 	/* mask hw interrupts */
2714 	al_eth_interrupts_mask(adapter);
2715 }
2716 
2717 static void
2718 al_eth_interrupts_unmask(struct al_eth_adapter *adapter)
2719 {
2720 	uint32_t group_a_mask = AL_INT_GROUP_A_GROUP_D_SUM; /* enable group D summery */
2721 	uint32_t group_b_mask = (1 << adapter->num_rx_queues) - 1;/* bit per Rx q*/
2722 	uint32_t group_c_mask = (1 << adapter->num_tx_queues) - 1;/* bit per Tx q*/
2723 	uint32_t group_d_mask = 3 << 8;
2724 	struct unit_regs __iomem *regs_base =
2725 	    (struct unit_regs __iomem *)adapter->udma_base;
2726 
2727 	if (adapter->int_mode == AL_IOFIC_MODE_LEGACY)
2728 		group_a_mask |= AL_INT_GROUP_A_GROUP_B_SUM |
2729 		    AL_INT_GROUP_A_GROUP_C_SUM |
2730 		    AL_INT_GROUP_A_GROUP_D_SUM;
2731 
2732 	al_udma_iofic_unmask(regs_base, AL_UDMA_IOFIC_LEVEL_PRIMARY,
2733 	    AL_INT_GROUP_A, group_a_mask);
2734 	al_udma_iofic_unmask(regs_base, AL_UDMA_IOFIC_LEVEL_PRIMARY,
2735 	    AL_INT_GROUP_B, group_b_mask);
2736 	al_udma_iofic_unmask(regs_base, AL_UDMA_IOFIC_LEVEL_PRIMARY,
2737 	    AL_INT_GROUP_C, group_c_mask);
2738 	al_udma_iofic_unmask(regs_base, AL_UDMA_IOFIC_LEVEL_PRIMARY,
2739 	    AL_INT_GROUP_D, group_d_mask);
2740 }
2741 
2742 static void
2743 al_eth_interrupts_mask(struct al_eth_adapter *adapter)
2744 {
2745 	struct unit_regs __iomem *regs_base =
2746 	    (struct unit_regs __iomem *)adapter->udma_base;
2747 
2748 	/* mask all interrupts */
2749 	al_udma_iofic_mask(regs_base, AL_UDMA_IOFIC_LEVEL_PRIMARY,
2750 	    AL_INT_GROUP_A, AL_MASK_GROUP_A_INT);
2751 	al_udma_iofic_mask(regs_base, AL_UDMA_IOFIC_LEVEL_PRIMARY,
2752 	    AL_INT_GROUP_B, AL_MASK_GROUP_B_INT);
2753 	al_udma_iofic_mask(regs_base, AL_UDMA_IOFIC_LEVEL_PRIMARY,
2754 	    AL_INT_GROUP_C, AL_MASK_GROUP_C_INT);
2755 	al_udma_iofic_mask(regs_base, AL_UDMA_IOFIC_LEVEL_PRIMARY,
2756 	    AL_INT_GROUP_D, AL_MASK_GROUP_D_INT);
2757 }
2758 
2759 static int
2760 al_eth_configure_int_mode(struct al_eth_adapter *adapter)
2761 {
2762 	enum al_iofic_mode int_mode;
2763 	uint32_t m2s_errors_disable = AL_M2S_MASK_INIT;
2764 	uint32_t m2s_aborts_disable = AL_M2S_MASK_INIT;
2765 	uint32_t s2m_errors_disable = AL_S2M_MASK_INIT;
2766 	uint32_t s2m_aborts_disable = AL_S2M_MASK_INIT;
2767 
2768 	/* single INTX mode */
2769 	if (adapter->msix_vecs == 0)
2770 		int_mode = AL_IOFIC_MODE_LEGACY;
2771 	else if (adapter->msix_vecs > 1)
2772 		int_mode = AL_IOFIC_MODE_MSIX_PER_Q;
2773 	else {
2774 		device_printf(adapter->dev,
2775 		    "udma doesn't support single MSI-X mode yet.\n");
2776 		return (EIO);
2777 	}
2778 
2779 	if (adapter->board_type != ALPINE_INTEGRATED) {
2780 		m2s_errors_disable |= AL_M2S_S2M_MASK_NOT_INT;
2781 		m2s_errors_disable |= AL_M2S_S2M_MASK_NOT_INT;
2782 		s2m_aborts_disable |= AL_M2S_S2M_MASK_NOT_INT;
2783 		s2m_aborts_disable |= AL_M2S_S2M_MASK_NOT_INT;
2784 	}
2785 
2786 	if (al_udma_iofic_config((struct unit_regs __iomem *)adapter->udma_base,
2787 	    int_mode, m2s_errors_disable, m2s_aborts_disable,
2788 	    s2m_errors_disable, s2m_aborts_disable)) {
2789 		device_printf(adapter->dev,
2790 		    "al_udma_unit_int_config failed!.\n");
2791 		return (EIO);
2792 	}
2793 	adapter->int_mode = int_mode;
2794 	device_printf_dbg(adapter->dev, "using %s interrupt mode\n",
2795 	    int_mode == AL_IOFIC_MODE_LEGACY ? "INTx" :
2796 	    int_mode == AL_IOFIC_MODE_MSIX_PER_Q ? "MSI-X per Queue" : "Unknown");
2797 	/* set interrupt moderation resolution to 15us */
2798 	al_iofic_moder_res_config(&((struct unit_regs *)(adapter->udma_base))->gen.interrupt_regs.main_iofic, AL_INT_GROUP_B, 15);
2799 	al_iofic_moder_res_config(&((struct unit_regs *)(adapter->udma_base))->gen.interrupt_regs.main_iofic, AL_INT_GROUP_C, 15);
2800 	/* by default interrupt coalescing is disabled */
2801 	adapter->tx_usecs = 0;
2802 	adapter->rx_usecs = 0;
2803 
2804 	return (0);
2805 }
2806 
2807 /*
2808  * ethtool_rxfh_indir_default - get default value for RX flow hash indirection
2809  * @index: Index in RX flow hash indirection table
2810  * @n_rx_rings: Number of RX rings to use
2811  *
2812  * This function provides the default policy for RX flow hash indirection.
2813  */
2814 static inline uint32_t
2815 ethtool_rxfh_indir_default(uint32_t index, uint32_t n_rx_rings)
2816 {
2817 
2818 	return (index % n_rx_rings);
2819 }
2820 
2821 static void*
2822 al_eth_update_stats(struct al_eth_adapter *adapter)
2823 {
2824 	struct al_eth_mac_stats *mac_stats = &adapter->mac_stats;
2825 
2826 	if (adapter->up == 0)
2827 		return (NULL);
2828 
2829 	al_eth_mac_stats_get(&adapter->hal_adapter, mac_stats);
2830 
2831 	return (NULL);
2832 }
2833 
2834 static uint64_t
2835 al_get_counter(struct ifnet *ifp, ift_counter cnt)
2836 {
2837 	struct al_eth_adapter *adapter;
2838 	struct al_eth_mac_stats *mac_stats;
2839 	uint64_t rv;
2840 
2841 	adapter = if_getsoftc(ifp);
2842 	mac_stats = &adapter->mac_stats;
2843 
2844 	switch (cnt) {
2845 	case IFCOUNTER_IPACKETS:
2846 		return (mac_stats->aFramesReceivedOK); /* including pause frames */
2847 	case IFCOUNTER_OPACKETS:
2848 		return (mac_stats->aFramesTransmittedOK);
2849 	case IFCOUNTER_IBYTES:
2850 		return (mac_stats->aOctetsReceivedOK);
2851 	case IFCOUNTER_OBYTES:
2852 		return (mac_stats->aOctetsTransmittedOK);
2853 	case IFCOUNTER_IMCASTS:
2854 		return (mac_stats->ifInMulticastPkts);
2855 	case IFCOUNTER_OMCASTS:
2856 		return (mac_stats->ifOutMulticastPkts);
2857 	case IFCOUNTER_COLLISIONS:
2858 		return (0);
2859 	case IFCOUNTER_IQDROPS:
2860 		return (mac_stats->etherStatsDropEvents);
2861 	case IFCOUNTER_IERRORS:
2862 		rv = mac_stats->ifInErrors +
2863 		    mac_stats->etherStatsUndersizePkts + /* good but short */
2864 		    mac_stats->etherStatsFragments + /* short and bad*/
2865 		    mac_stats->etherStatsJabbers + /* with crc errors */
2866 		    mac_stats->etherStatsOversizePkts +
2867 		    mac_stats->aFrameCheckSequenceErrors +
2868 		    mac_stats->aAlignmentErrors;
2869 		return (rv);
2870 	case IFCOUNTER_OERRORS:
2871 		return (mac_stats->ifOutErrors);
2872 	default:
2873 		return (if_get_counter_default(ifp, cnt));
2874 	}
2875 }
2876 
2877 static u_int
2878 al_count_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
2879 {
2880 	unsigned char *mac;
2881 
2882 	mac = LLADDR(sdl);
2883 	/* default mc address inside mac address */
2884 	if (mac[3] != 0 && mac[4] != 0 && mac[5] != 1)
2885 		return (1);
2886 	else
2887 		return (0);
2888 }
2889 
2890 static u_int
2891 al_program_addr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
2892 {
2893 	struct al_eth_adapter *adapter = arg;
2894 
2895 	al_eth_mac_table_unicast_add(adapter,
2896 	    AL_ETH_MAC_TABLE_UNICAST_IDX_BASE + 1 + cnt, 1);
2897 
2898 	return (1);
2899 }
2900 
2901 /*
2902  *  Unicast, Multicast and Promiscuous mode set
2903  *
2904  *  The set_rx_mode entry point is called whenever the unicast or multicast
2905  *  address lists or the network interface flags are updated.  This routine is
2906  *  responsible for configuring the hardware for proper unicast, multicast,
2907  *  promiscuous mode, and all-multi behavior.
2908  */
2909 static void
2910 al_eth_set_rx_mode(struct al_eth_adapter *adapter)
2911 {
2912 	struct ifnet *ifp = adapter->netdev;
2913 	int mc, uc;
2914 	uint8_t i;
2915 
2916 	/* XXXGL: why generic count won't work? */
2917 	mc = if_foreach_llmaddr(ifp, al_count_maddr, NULL);
2918 	uc = if_lladdr_count(ifp);
2919 
2920 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
2921 		al_eth_mac_table_promiscuous_set(adapter, true);
2922 	} else {
2923 		if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
2924 			/* This interface is in all-multicasts mode (used by multicast routers). */
2925 			al_eth_mac_table_all_multicast_add(adapter,
2926 			    AL_ETH_MAC_TABLE_ALL_MULTICAST_IDX, 1);
2927 		} else {
2928 			if (mc == 0) {
2929 				al_eth_mac_table_entry_clear(adapter,
2930 				    AL_ETH_MAC_TABLE_ALL_MULTICAST_IDX);
2931 			} else {
2932 				al_eth_mac_table_all_multicast_add(adapter,
2933 				    AL_ETH_MAC_TABLE_ALL_MULTICAST_IDX, 1);
2934 			}
2935 		}
2936 		if (uc != 0) {
2937 			i = AL_ETH_MAC_TABLE_UNICAST_IDX_BASE + 1;
2938 			if (uc > AL_ETH_MAC_TABLE_UNICAST_MAX_COUNT) {
2939 				/*
2940 				 * In this case there are more addresses then
2941 				 * entries in the mac table - set promiscuous
2942 				 */
2943 				al_eth_mac_table_promiscuous_set(adapter, true);
2944 				return;
2945 			}
2946 
2947 			/* clear the last configuration */
2948 			while (i < (AL_ETH_MAC_TABLE_UNICAST_IDX_BASE +
2949 				    AL_ETH_MAC_TABLE_UNICAST_MAX_COUNT)) {
2950 				al_eth_mac_table_entry_clear(adapter, i);
2951 				i++;
2952 			}
2953 
2954 			/* set new addresses */
2955 			if_foreach_lladdr(ifp, al_program_addr, adapter);
2956 		}
2957 		al_eth_mac_table_promiscuous_set(adapter, false);
2958 	}
2959 }
2960 
2961 static void
2962 al_eth_config_rx_fwd(struct al_eth_adapter *adapter)
2963 {
2964 	struct al_eth_fwd_ctrl_table_entry entry;
2965 	int i;
2966 
2967 	/* let priority be equal to pbits */
2968 	for (i = 0; i < AL_ETH_FWD_PBITS_TABLE_NUM; i++)
2969 		al_eth_fwd_pbits_table_set(&adapter->hal_adapter, i, i);
2970 
2971 	/* map priority to queue index, queue id = priority/2 */
2972 	for (i = 0; i < AL_ETH_FWD_PRIO_TABLE_NUM; i++)
2973 		al_eth_fwd_priority_table_set(&adapter->hal_adapter, i, i >> 1);
2974 
2975 	entry.prio_sel = AL_ETH_CTRL_TABLE_PRIO_SEL_VAL_0;
2976 	entry.queue_sel_1 = AL_ETH_CTRL_TABLE_QUEUE_SEL_1_THASH_TABLE;
2977 	entry.queue_sel_2 = AL_ETH_CTRL_TABLE_QUEUE_SEL_2_NO_PRIO;
2978 	entry.udma_sel = AL_ETH_CTRL_TABLE_UDMA_SEL_MAC_TABLE;
2979 	entry.filter = FALSE;
2980 
2981 	al_eth_ctrl_table_def_set(&adapter->hal_adapter, FALSE, &entry);
2982 
2983 	/*
2984 	 * By default set the mac table to forward all unicast packets to our
2985 	 * MAC address and all broadcast. all the rest will be dropped.
2986 	 */
2987 	al_eth_mac_table_unicast_add(adapter, AL_ETH_MAC_TABLE_UNICAST_IDX_BASE,
2988 	    1);
2989 	al_eth_mac_table_broadcast_add(adapter, AL_ETH_MAC_TABLE_BROADCAST_IDX, 1);
2990 	al_eth_mac_table_promiscuous_set(adapter, false);
2991 
2992 	/* set toeplitz hash keys */
2993 	for (i = 0; i < sizeof(adapter->toeplitz_hash_key); i++)
2994 		*((uint8_t*)adapter->toeplitz_hash_key + i) = (uint8_t)random();
2995 
2996 	for (i = 0; i < AL_ETH_RX_HASH_KEY_NUM; i++)
2997 		al_eth_hash_key_set(&adapter->hal_adapter, i,
2998 		    htonl(adapter->toeplitz_hash_key[i]));
2999 
3000 	for (i = 0; i < AL_ETH_RX_RSS_TABLE_SIZE; i++) {
3001 		adapter->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i,
3002 		    AL_ETH_NUM_QUEUES);
3003 		al_eth_set_thash_table_entry(adapter, i, 0,
3004 		    adapter->rss_ind_tbl[i]);
3005 	}
3006 
3007 	al_eth_fsm_table_init(adapter);
3008 }
3009 
3010 static void
3011 al_eth_req_rx_buff_size(struct al_eth_adapter *adapter, int size)
3012 {
3013 
3014 	/*
3015 	* Determine the correct mbuf pool
3016 	* for doing jumbo frames
3017 	* Try from the smallest up to maximum supported
3018 	*/
3019 	adapter->rx_mbuf_sz = MCLBYTES;
3020 	if (size > 2048) {
3021 		if (adapter->max_rx_buff_alloc_size > 2048)
3022 			adapter->rx_mbuf_sz = MJUMPAGESIZE;
3023 		else
3024 			return;
3025 	}
3026 	if (size > 4096) {
3027 		if (adapter->max_rx_buff_alloc_size > 4096)
3028 			adapter->rx_mbuf_sz = MJUM9BYTES;
3029 		else
3030 			return;
3031 	}
3032 	if (size > 9216) {
3033 		if (adapter->max_rx_buff_alloc_size > 9216)
3034 			adapter->rx_mbuf_sz = MJUM16BYTES;
3035 		else
3036 			return;
3037 	}
3038 }
3039 
3040 static int
3041 al_eth_change_mtu(struct al_eth_adapter *adapter, int new_mtu)
3042 {
3043 	int max_frame = new_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
3044 	    ETHER_VLAN_ENCAP_LEN;
3045 
3046 	al_eth_req_rx_buff_size(adapter, new_mtu);
3047 
3048 	device_printf_dbg(adapter->dev, "set MTU to %d\n", new_mtu);
3049 	al_eth_rx_pkt_limit_config(&adapter->hal_adapter,
3050 	    AL_ETH_MIN_FRAME_LEN, max_frame);
3051 
3052 	al_eth_tso_mss_config(&adapter->hal_adapter, 0, new_mtu - 100);
3053 
3054 	return (0);
3055 }
3056 
3057 static int
3058 al_eth_check_mtu(struct al_eth_adapter *adapter, int new_mtu)
3059 {
3060 	int max_frame = new_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
3061 
3062 	if ((new_mtu < AL_ETH_MIN_FRAME_LEN) ||
3063 	    (max_frame > AL_ETH_MAX_FRAME_LEN)) {
3064 		return (EINVAL);
3065 	}
3066 
3067 	return (0);
3068 }
3069 
3070 static int
3071 al_eth_udma_queue_enable(struct al_eth_adapter *adapter, enum al_udma_type type,
3072     int qid)
3073 {
3074 	int rc = 0;
3075 	char *name = (type == UDMA_TX) ? "Tx" : "Rx";
3076 	struct al_udma_q_params *q_params;
3077 
3078 	if (type == UDMA_TX)
3079 		q_params = &adapter->tx_ring[qid].q_params;
3080 	else
3081 		q_params = &adapter->rx_ring[qid].q_params;
3082 
3083 	rc = al_eth_queue_config(&adapter->hal_adapter, type, qid, q_params);
3084 	if (rc < 0) {
3085 		device_printf(adapter->dev, "config %s queue %u failed\n", name,
3086 		    qid);
3087 		return (rc);
3088 	}
3089 	return (rc);
3090 }
3091 
3092 static int
3093 al_eth_udma_queues_enable_all(struct al_eth_adapter *adapter)
3094 {
3095 	int i;
3096 
3097 	for (i = 0; i < adapter->num_tx_queues; i++)
3098 		al_eth_udma_queue_enable(adapter, UDMA_TX, i);
3099 
3100 	for (i = 0; i < adapter->num_rx_queues; i++)
3101 		al_eth_udma_queue_enable(adapter, UDMA_RX, i);
3102 
3103 	return (0);
3104 }
3105 
3106 static void
3107 al_eth_up_complete(struct al_eth_adapter *adapter)
3108 {
3109 
3110 	al_eth_configure_int_mode(adapter);
3111 	al_eth_config_rx_fwd(adapter);
3112 	al_eth_change_mtu(adapter, adapter->netdev->if_mtu);
3113 	al_eth_udma_queues_enable_all(adapter);
3114 	al_eth_refill_all_rx_bufs(adapter);
3115 	al_eth_interrupts_unmask(adapter);
3116 
3117 	/* enable forwarding interrupts from eth through pci end point */
3118 	if ((adapter->board_type == ALPINE_FPGA_NIC) ||
3119 	    (adapter->board_type == ALPINE_NIC)) {
3120 		al_eth_forward_int_config((uint32_t*)adapter->internal_pcie_base +
3121 		    AL_REG_OFFSET_FORWARD_INTR, AL_EN_FORWARD_INTR);
3122 	}
3123 
3124 	al_eth_flow_ctrl_enable(adapter);
3125 
3126 	mtx_lock(&adapter->stats_mtx);
3127 	callout_reset(&adapter->stats_callout, hz, al_tick_stats, (void*)adapter);
3128 	mtx_unlock(&adapter->stats_mtx);
3129 
3130 	al_eth_mac_start(&adapter->hal_adapter);
3131 }
3132 
3133 static int
3134 al_media_update(struct ifnet *ifp)
3135 {
3136 	struct al_eth_adapter *adapter = ifp->if_softc;
3137 
3138 	if ((ifp->if_flags & IFF_UP) != 0)
3139 		mii_mediachg(adapter->mii);
3140 
3141 	return (0);
3142 }
3143 
3144 static void
3145 al_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
3146 {
3147 	struct al_eth_adapter *sc = ifp->if_softc;
3148 	struct mii_data *mii;
3149 
3150 	if (sc->mii == NULL) {
3151 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
3152 		ifmr->ifm_status = 0;
3153 
3154 		return;
3155 	}
3156 
3157 	mii = sc->mii;
3158 	mii_pollstat(mii);
3159 
3160 	ifmr->ifm_active = mii->mii_media_active;
3161 	ifmr->ifm_status = mii->mii_media_status;
3162 }
3163 
3164 static void
3165 al_tick(void *arg)
3166 {
3167 	struct al_eth_adapter *adapter = arg;
3168 
3169 	mii_tick(adapter->mii);
3170 
3171 	/* Schedule another timeout one second from now */
3172 	callout_schedule(&adapter->wd_callout, hz);
3173 }
3174 
3175 static void
3176 al_tick_stats(void *arg)
3177 {
3178 	struct al_eth_adapter *adapter = arg;
3179 
3180 	al_eth_update_stats(adapter);
3181 
3182 	callout_schedule(&adapter->stats_callout, hz);
3183 }
3184 
3185 static int
3186 al_eth_up(struct al_eth_adapter *adapter)
3187 {
3188 	struct ifnet *ifp = adapter->netdev;
3189 	int rc;
3190 
3191 	if (adapter->up)
3192 		return (0);
3193 
3194 	if ((adapter->flags & AL_ETH_FLAG_RESET_REQUESTED) != 0) {
3195 		al_eth_function_reset(adapter);
3196 		adapter->flags &= ~AL_ETH_FLAG_RESET_REQUESTED;
3197 	}
3198 
3199 	ifp->if_hwassist = 0;
3200 	if ((ifp->if_capenable & IFCAP_TSO) != 0)
3201 		ifp->if_hwassist |= CSUM_TSO;
3202 	if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
3203 		ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP);
3204 	if ((ifp->if_capenable & IFCAP_TXCSUM_IPV6) != 0)
3205 		ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
3206 
3207 	al_eth_serdes_init(adapter);
3208 
3209 	rc = al_eth_hw_init(adapter);
3210 	if (rc != 0)
3211 		goto err_hw_init_open;
3212 
3213 	rc = al_eth_setup_int_mode(adapter);
3214 	if (rc != 0) {
3215 		device_printf(adapter->dev,
3216 		    "%s failed at setup interrupt mode!\n", __func__);
3217 		goto err_setup_int;
3218 	}
3219 
3220 	/* allocate transmit descriptors */
3221 	rc = al_eth_setup_all_tx_resources(adapter);
3222 	if (rc != 0)
3223 		goto err_setup_tx;
3224 
3225 	/* allocate receive descriptors */
3226 	rc = al_eth_setup_all_rx_resources(adapter);
3227 	if (rc != 0)
3228 		goto err_setup_rx;
3229 
3230 	rc = al_eth_request_irq(adapter);
3231 	if (rc != 0)
3232 		goto err_req_irq;
3233 
3234 	al_eth_up_complete(adapter);
3235 
3236 	adapter->up = true;
3237 
3238 	if (adapter->mac_mode == AL_ETH_MAC_MODE_10GbE_Serial)
3239 		adapter->netdev->if_link_state = LINK_STATE_UP;
3240 
3241 	if (adapter->mac_mode == AL_ETH_MAC_MODE_RGMII) {
3242 		mii_mediachg(adapter->mii);
3243 
3244 		/* Schedule watchdog timeout */
3245 		mtx_lock(&adapter->wd_mtx);
3246 		callout_reset(&adapter->wd_callout, hz, al_tick, adapter);
3247 		mtx_unlock(&adapter->wd_mtx);
3248 
3249 		mii_pollstat(adapter->mii);
3250 	}
3251 
3252 	return (rc);
3253 
3254 err_req_irq:
3255 	al_eth_free_all_rx_resources(adapter);
3256 err_setup_rx:
3257 	al_eth_free_all_tx_resources(adapter);
3258 err_setup_tx:
3259 	al_eth_free_irq(adapter);
3260 err_setup_int:
3261 	al_eth_hw_stop(adapter);
3262 err_hw_init_open:
3263 	al_eth_function_reset(adapter);
3264 
3265 	return (rc);
3266 }
3267 
3268 static int
3269 al_shutdown(device_t dev)
3270 {
3271 	struct al_eth_adapter *adapter = device_get_softc(dev);
3272 
3273 	al_eth_down(adapter);
3274 
3275 	return (0);
3276 }
3277 
3278 static void
3279 al_eth_down(struct al_eth_adapter *adapter)
3280 {
3281 
3282 	device_printf_dbg(adapter->dev, "al_eth_down: begin\n");
3283 
3284 	adapter->up = false;
3285 
3286 	mtx_lock(&adapter->wd_mtx);
3287 	callout_stop(&adapter->wd_callout);
3288 	mtx_unlock(&adapter->wd_mtx);
3289 
3290 	al_eth_disable_int_sync(adapter);
3291 
3292 	mtx_lock(&adapter->stats_mtx);
3293 	callout_stop(&adapter->stats_callout);
3294 	mtx_unlock(&adapter->stats_mtx);
3295 
3296 	al_eth_free_irq(adapter);
3297 	al_eth_hw_stop(adapter);
3298 
3299 	al_eth_free_all_tx_resources(adapter);
3300 	al_eth_free_all_rx_resources(adapter);
3301 }
3302 
3303 static int
3304 al_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3305 {
3306 	struct al_eth_adapter	*adapter = ifp->if_softc;
3307 	struct ifreq		*ifr = (struct ifreq *)data;
3308 	int			error = 0;
3309 
3310 	switch (command) {
3311 	case SIOCSIFMTU:
3312 	{
3313 		error = al_eth_check_mtu(adapter, ifr->ifr_mtu);
3314 		if (error != 0) {
3315 			device_printf(adapter->dev, "ioctl wrong mtu %u\n",
3316 			    adapter->netdev->if_mtu);
3317 			break;
3318 		}
3319 
3320 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3321 		adapter->netdev->if_mtu = ifr->ifr_mtu;
3322 		al_init(adapter);
3323 		break;
3324 	}
3325 	case SIOCSIFFLAGS:
3326 		if ((ifp->if_flags & IFF_UP) != 0) {
3327 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3328 				if (((ifp->if_flags ^ adapter->if_flags) &
3329 				    (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
3330 					device_printf_dbg(adapter->dev,
3331 					    "ioctl promisc/allmulti\n");
3332 					al_eth_set_rx_mode(adapter);
3333 				}
3334 			} else {
3335 				error = al_eth_up(adapter);
3336 				if (error == 0)
3337 					ifp->if_drv_flags |= IFF_DRV_RUNNING;
3338 			}
3339 		} else {
3340 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3341 				al_eth_down(adapter);
3342 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3343 			}
3344 		}
3345 
3346 		adapter->if_flags = ifp->if_flags;
3347 		break;
3348 
3349 	case SIOCADDMULTI:
3350 	case SIOCDELMULTI:
3351 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3352 			device_printf_dbg(adapter->dev,
3353 			    "ioctl add/del multi before\n");
3354 			al_eth_set_rx_mode(adapter);
3355 #ifdef DEVICE_POLLING
3356 			if ((ifp->if_capenable & IFCAP_POLLING) == 0)
3357 #endif
3358 		}
3359 		break;
3360 	case SIOCSIFMEDIA:
3361 	case SIOCGIFMEDIA:
3362 		if (adapter->mii != NULL)
3363 			error = ifmedia_ioctl(ifp, ifr,
3364 			    &adapter->mii->mii_media, command);
3365 		else
3366 			error = ifmedia_ioctl(ifp, ifr,
3367 			    &adapter->media, command);
3368 		break;
3369 	case SIOCSIFCAP:
3370 	    {
3371 		int mask, reinit;
3372 
3373 		reinit = 0;
3374 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3375 #ifdef DEVICE_POLLING
3376 		if ((mask & IFCAP_POLLING) != 0) {
3377 			if ((ifr->ifr_reqcap & IFCAP_POLLING) != 0) {
3378 				if (error != 0)
3379 					return (error);
3380 				ifp->if_capenable |= IFCAP_POLLING;
3381 			} else {
3382 				error = ether_poll_deregister(ifp);
3383 				/* Enable interrupt even in error case */
3384 				ifp->if_capenable &= ~IFCAP_POLLING;
3385 			}
3386 		}
3387 #endif
3388 		if ((mask & IFCAP_HWCSUM) != 0) {
3389 			/* apply to both rx and tx */
3390 			ifp->if_capenable ^= IFCAP_HWCSUM;
3391 			reinit = 1;
3392 		}
3393 		if ((mask & IFCAP_HWCSUM_IPV6) != 0) {
3394 			ifp->if_capenable ^= IFCAP_HWCSUM_IPV6;
3395 			reinit = 1;
3396 		}
3397 		if ((mask & IFCAP_TSO) != 0) {
3398 			ifp->if_capenable ^= IFCAP_TSO;
3399 			reinit = 1;
3400 		}
3401 		if ((mask & IFCAP_LRO) != 0) {
3402 			ifp->if_capenable ^= IFCAP_LRO;
3403 		}
3404 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0) {
3405 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3406 			reinit = 1;
3407 		}
3408 		if ((mask & IFCAP_VLAN_HWFILTER) != 0) {
3409 			ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
3410 			reinit = 1;
3411 		}
3412 		if ((mask & IFCAP_VLAN_HWTSO) != 0) {
3413 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3414 			reinit = 1;
3415 		}
3416 		if ((reinit != 0) &&
3417 		    ((ifp->if_drv_flags & IFF_DRV_RUNNING)) != 0)
3418 		{
3419 			al_init(adapter);
3420 		}
3421 		break;
3422 	    }
3423 
3424 	default:
3425 		error = ether_ioctl(ifp, command, data);
3426 		break;
3427 	}
3428 
3429 	return (error);
3430 }
3431 
3432 static int
3433 al_is_device_supported(device_t dev)
3434 {
3435 	uint16_t pci_vendor_id = pci_get_vendor(dev);
3436 	uint16_t pci_device_id = pci_get_device(dev);
3437 
3438 	return (pci_vendor_id == PCI_VENDOR_ID_ANNAPURNA_LABS &&
3439 	    (pci_device_id == PCI_DEVICE_ID_AL_ETH ||
3440 	    pci_device_id == PCI_DEVICE_ID_AL_ETH_ADVANCED ||
3441 	    pci_device_id == PCI_DEVICE_ID_AL_ETH_NIC ||
3442 	    pci_device_id == PCI_DEVICE_ID_AL_ETH_FPGA_NIC));
3443 }
3444 
3445 /* Time in mSec to keep trying to read / write from MDIO in case of error */
3446 #define	MDIO_TIMEOUT_MSEC	100
3447 #define	MDIO_PAUSE_MSEC		10
3448 
3449 static int
3450 al_miibus_readreg(device_t dev, int phy, int reg)
3451 {
3452 	struct al_eth_adapter *adapter = device_get_softc(dev);
3453 	uint16_t value = 0;
3454 	int rc;
3455 	int timeout = MDIO_TIMEOUT_MSEC;
3456 
3457 	while (timeout > 0) {
3458 		rc = al_eth_mdio_read(&adapter->hal_adapter, adapter->phy_addr,
3459 		    -1, reg, &value);
3460 
3461 		if (rc == 0)
3462 			return (value);
3463 
3464 		device_printf_dbg(adapter->dev,
3465 		    "mdio read failed. try again in 10 msec\n");
3466 
3467 		timeout -= MDIO_PAUSE_MSEC;
3468 		pause("readred pause", MDIO_PAUSE_MSEC);
3469 	}
3470 
3471 	if (rc != 0)
3472 		device_printf(adapter->dev, "MDIO read failed on timeout\n");
3473 
3474 	return (value);
3475 }
3476 
3477 static int
3478 al_miibus_writereg(device_t dev, int phy, int reg, int value)
3479 {
3480 	struct al_eth_adapter *adapter = device_get_softc(dev);
3481 	int rc;
3482 	int timeout = MDIO_TIMEOUT_MSEC;
3483 
3484 	while (timeout > 0) {
3485 		rc = al_eth_mdio_write(&adapter->hal_adapter, adapter->phy_addr,
3486 		    -1, reg, value);
3487 
3488 		if (rc == 0)
3489 			return (0);
3490 
3491 		device_printf(adapter->dev,
3492 		    "mdio write failed. try again in 10 msec\n");
3493 
3494 		timeout -= MDIO_PAUSE_MSEC;
3495 		pause("miibus writereg", MDIO_PAUSE_MSEC);
3496 	}
3497 
3498 	if (rc != 0)
3499 		device_printf(adapter->dev, "MDIO write failed on timeout\n");
3500 
3501 	return (rc);
3502 }
3503 
3504 static void
3505 al_miibus_statchg(device_t dev)
3506 {
3507 	struct al_eth_adapter *adapter = device_get_softc(dev);
3508 
3509 	device_printf_dbg(adapter->dev,
3510 	    "al_miibus_statchg: state has changed!\n");
3511 	device_printf_dbg(adapter->dev,
3512 	    "al_miibus_statchg: active = 0x%x status = 0x%x\n",
3513 	    adapter->mii->mii_media_active, adapter->mii->mii_media_status);
3514 
3515 	if (adapter->up == 0)
3516 		return;
3517 
3518 	if ((adapter->mii->mii_media_status & IFM_AVALID) != 0) {
3519 		if (adapter->mii->mii_media_status & IFM_ACTIVE) {
3520 			device_printf(adapter->dev, "link is UP\n");
3521 			adapter->netdev->if_link_state = LINK_STATE_UP;
3522 		} else {
3523 			device_printf(adapter->dev, "link is DOWN\n");
3524 			adapter->netdev->if_link_state = LINK_STATE_DOWN;
3525 		}
3526 	}
3527 }
3528 
3529 static void
3530 al_miibus_linkchg(device_t dev)
3531 {
3532 	struct al_eth_adapter *adapter = device_get_softc(dev);
3533 	uint8_t duplex = 0;
3534 	uint8_t speed = 0;
3535 
3536 	if (adapter->mii == NULL)
3537 		return;
3538 
3539 	if ((adapter->netdev->if_flags & IFF_UP) == 0)
3540 		return;
3541 
3542 	/* Ignore link changes when link is not ready */
3543 	if ((adapter->mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) !=
3544 	    (IFM_AVALID | IFM_ACTIVE)) {
3545 		return;
3546 	}
3547 
3548 	if ((adapter->mii->mii_media_active & IFM_FDX) != 0)
3549 		duplex = 1;
3550 
3551 	speed = IFM_SUBTYPE(adapter->mii->mii_media_active);
3552 
3553 	if (speed == IFM_10_T) {
3554 		al_eth_mac_link_config(&adapter->hal_adapter, 0, 1,
3555 		    AL_10BASE_T_SPEED, duplex);
3556 		return;
3557 	}
3558 
3559 	if (speed == IFM_100_TX) {
3560 		al_eth_mac_link_config(&adapter->hal_adapter, 0, 1,
3561 		    AL_100BASE_TX_SPEED, duplex);
3562 		return;
3563 	}
3564 
3565 	if (speed == IFM_1000_T) {
3566 		al_eth_mac_link_config(&adapter->hal_adapter, 0, 1,
3567 		    AL_1000BASE_T_SPEED, duplex);
3568 		return;
3569 	}
3570 
3571 	device_printf(adapter->dev, "ERROR: unknown MII media active 0x%08x\n",
3572 	    adapter->mii->mii_media_active);
3573 }
3574