xref: /netbsd-src/sys/dev/pci/ixgbe/ixv.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /******************************************************************************
2 
3   Copyright (c) 2001-2015, Intel Corporation
4   All rights reserved.
5 
6   Redistribution and use in source and binary forms, with or without
7   modification, are permitted provided that the following conditions are met:
8 
9    1. Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11 
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    3. Neither the name of the Intel Corporation nor the names of its
17       contributors may be used to endorse or promote products derived from
18       this software without specific prior written permission.
19 
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31 
32 ******************************************************************************/
33 /*$FreeBSD: head/sys/dev/ixgbe/if_ixv.c 302384 2016-07-07 03:39:18Z sbruno $*/
34 /*$NetBSD: ixv.c,v 1.53 2017/02/13 10:37:37 msaitoh Exp $*/
35 
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 
39 #include "ixgbe.h"
40 #include "vlan.h"
41 
42 /*********************************************************************
43  *  Driver version
44  *********************************************************************/
45 char ixv_driver_version[] = "1.4.6-k";
46 
47 /*********************************************************************
48  *  PCI Device ID Table
49  *
50  *  Used by probe to select devices to load on
51  *  Last field stores an index into ixv_strings
52  *  Last entry must be all 0s
53  *
54  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
55  *********************************************************************/
56 
57 static ixgbe_vendor_info_t ixv_vendor_info_array[] =
58 {
59 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_VF, 0, 0, 0},
60 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540_VF, 0, 0, 0},
61 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550_VF, 0, 0, 0},
62 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_VF, 0, 0, 0},
63 	/* required last entry */
64 	{0, 0, 0, 0, 0}
65 };
66 
67 /*********************************************************************
68  *  Table of branding strings
69  *********************************************************************/
70 
71 static const char    *ixv_strings[] = {
72 	"Intel(R) PRO/10GbE Virtual Function Network Driver"
73 };
74 
75 /*********************************************************************
76  *  Function prototypes
77  *********************************************************************/
78 static int      ixv_probe(device_t, cfdata_t, void *);
79 static void	ixv_attach(device_t, device_t, void *);
80 static int      ixv_detach(device_t, int);
81 #if 0
82 static int      ixv_shutdown(device_t);
83 #endif
84 static int      ixv_ioctl(struct ifnet *, u_long, void *);
85 static int	ixv_init(struct ifnet *);
86 static void	ixv_init_locked(struct adapter *);
87 static void     ixv_stop(void *);
88 static void     ixv_media_status(struct ifnet *, struct ifmediareq *);
89 static int      ixv_media_change(struct ifnet *);
90 static void     ixv_identify_hardware(struct adapter *);
91 static int      ixv_allocate_pci_resources(struct adapter *,
92 		    const struct pci_attach_args *);
93 static int      ixv_allocate_msix(struct adapter *,
94 		    const struct pci_attach_args *);
95 static int	ixv_setup_msix(struct adapter *);
96 static void	ixv_free_pci_resources(struct adapter *);
97 static void     ixv_local_timer(void *);
98 static void     ixv_local_timer_locked(void *);
99 static void     ixv_setup_interface(device_t, struct adapter *);
100 static void     ixv_config_link(struct adapter *);
101 
102 static void     ixv_initialize_transmit_units(struct adapter *);
103 static void     ixv_initialize_receive_units(struct adapter *);
104 
105 static void     ixv_enable_intr(struct adapter *);
106 static void     ixv_disable_intr(struct adapter *);
107 static void     ixv_set_multi(struct adapter *);
108 static void     ixv_update_link_status(struct adapter *);
109 static int	ixv_sysctl_debug(SYSCTLFN_PROTO);
110 static void	ixv_set_ivar(struct adapter *, u8, u8, s8);
111 static void	ixv_configure_ivars(struct adapter *);
112 static u8 *	ixv_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
113 
114 static void	ixv_setup_vlan_support(struct adapter *);
115 #if 0
116 static void	ixv_register_vlan(void *, struct ifnet *, u16);
117 static void	ixv_unregister_vlan(void *, struct ifnet *, u16);
118 #endif
119 
120 static void	ixv_add_device_sysctls(struct adapter *);
121 static void	ixv_save_stats(struct adapter *);
122 static void	ixv_init_stats(struct adapter *);
123 static void	ixv_update_stats(struct adapter *);
124 static void	ixv_add_stats_sysctls(struct adapter *);
125 static void	ixv_set_sysctl_value(struct adapter *, const char *,
126 		    const char *, int *, int);
127 
128 /* The MSI/X Interrupt handlers */
129 static int	ixv_msix_que(void *);
130 static int	ixv_msix_mbx(void *);
131 
132 /* Deferred interrupt tasklets */
133 static void	ixv_handle_que(void *);
134 static void	ixv_handle_mbx(void *);
135 
136 const struct sysctlnode *ixv_sysctl_instance(struct adapter *);
137 static ixgbe_vendor_info_t *ixv_lookup(const struct pci_attach_args *);
138 
139 #ifdef DEV_NETMAP
140 /*
141  * This is defined in <dev/netmap/ixgbe_netmap.h>, which is included by
142  * if_ix.c.
143  */
144 extern void ixgbe_netmap_attach(struct adapter *adapter);
145 
146 #include <net/netmap.h>
147 #include <sys/selinfo.h>
148 #include <dev/netmap/netmap_kern.h>
149 #endif /* DEV_NETMAP */
150 
151 /*********************************************************************
152  *  FreeBSD Device Interface Entry Points
153  *********************************************************************/
154 
155 CFATTACH_DECL3_NEW(ixv, sizeof(struct adapter),
156     ixv_probe, ixv_attach, ixv_detach, NULL, NULL, NULL,
157     DVF_DETACH_SHUTDOWN);
158 
159 # if 0
160 static device_method_t ixv_methods[] = {
161 	/* Device interface */
162 	DEVMETHOD(device_probe, ixv_probe),
163 	DEVMETHOD(device_attach, ixv_attach),
164 	DEVMETHOD(device_detach, ixv_detach),
165 	DEVMETHOD(device_shutdown, ixv_shutdown),
166 	DEVMETHOD_END
167 };
168 #endif
169 
170 #if 0
171 static driver_t ixv_driver = {
172 	"ixv", ixv_methods, sizeof(struct adapter),
173 };
174 
175 devclass_t ixv_devclass;
176 DRIVER_MODULE(ixv, pci, ixv_driver, ixv_devclass, 0, 0);
177 MODULE_DEPEND(ixv, pci, 1, 1, 1);
178 MODULE_DEPEND(ixv, ether, 1, 1, 1);
179 #ifdef DEV_NETMAP
180 MODULE_DEPEND(ix, netmap, 1, 1, 1);
181 #endif /* DEV_NETMAP */
182 /* XXX depend on 'ix' ? */
183 #endif
184 
185 /*
186 ** TUNEABLE PARAMETERS:
187 */
188 
189 /* Number of Queues - do not exceed MSIX vectors - 1 */
190 static int ixv_num_queues = 0;
191 #define	TUNABLE_INT(__x, __y)
192 TUNABLE_INT("hw.ixv.num_queues", &ixv_num_queues);
193 
194 /*
195 ** AIM: Adaptive Interrupt Moderation
196 ** which means that the interrupt rate
197 ** is varied over time based on the
198 ** traffic for that interrupt vector
199 */
200 static bool ixv_enable_aim = false;
201 TUNABLE_INT("hw.ixv.enable_aim", &ixv_enable_aim);
202 
203 /* How many packets rxeof tries to clean at a time */
204 static int ixv_rx_process_limit = 256;
205 TUNABLE_INT("hw.ixv.rx_process_limit", &ixv_rx_process_limit);
206 
207 /* How many packets txeof tries to clean at a time */
208 static int ixv_tx_process_limit = 256;
209 TUNABLE_INT("hw.ixv.tx_process_limit", &ixv_tx_process_limit);
210 
211 /*
212 ** Number of TX descriptors per ring,
213 ** setting higher than RX as this seems
214 ** the better performing choice.
215 */
216 static int ixv_txd = DEFAULT_TXD;
217 TUNABLE_INT("hw.ixv.txd", &ixv_txd);
218 
219 /* Number of RX descriptors per ring */
220 static int ixv_rxd = DEFAULT_RXD;
221 TUNABLE_INT("hw.ixv.rxd", &ixv_rxd);
222 
223 /*
224 ** Shadow VFTA table, this is needed because
225 ** the real filter table gets cleared during
226 ** a soft reset and we need to repopulate it.
227 */
228 static u32 ixv_shadow_vfta[IXGBE_VFTA_SIZE];
229 
230 /*********************************************************************
231  *  Device identification routine
232  *
233  *  ixv_probe determines if the driver should be loaded on
234  *  adapter based on PCI vendor/device id of the adapter.
235  *
236  *  return 1 on success, 0 on failure
237  *********************************************************************/
238 
239 static int
240 ixv_probe(device_t dev, cfdata_t cf, void *aux)
241 {
242 #ifdef __HAVE_PCI_MSI_MSIX
243 	const struct pci_attach_args *pa = aux;
244 
245 	return (ixv_lookup(pa) != NULL) ? 1 : 0;
246 #else
247 	return 0;
248 #endif
249 }
250 
251 static ixgbe_vendor_info_t *
252 ixv_lookup(const struct pci_attach_args *pa)
253 {
254 	pcireg_t subid;
255 	ixgbe_vendor_info_t *ent;
256 
257 	INIT_DEBUGOUT("ixv_lookup: begin");
258 
259 	if (PCI_VENDOR(pa->pa_id) != IXGBE_INTEL_VENDOR_ID)
260 		return NULL;
261 
262 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
263 
264 	for (ent = ixv_vendor_info_array; ent->vendor_id != 0; ent++) {
265 		if ((PCI_VENDOR(pa->pa_id) == ent->vendor_id) &&
266 		    (PCI_PRODUCT(pa->pa_id) == ent->device_id) &&
267 
268 		    ((PCI_SUBSYS_VENDOR(subid) == ent->subvendor_id) ||
269 		     (ent->subvendor_id == 0)) &&
270 
271 		    ((PCI_SUBSYS_ID(subid) == ent->subdevice_id) ||
272 		     (ent->subdevice_id == 0))) {
273 			return ent;
274 		}
275 	}
276 	return NULL;
277 }
278 
279 
280 /*********************************************************************
281  *  Device initialization routine
282  *
283  *  The attach entry point is called when the driver is being loaded.
284  *  This routine identifies the type of hardware, allocates all resources
285  *  and initializes the hardware.
286  *
287  *  return 0 on success, positive on failure
288  *********************************************************************/
289 
290 static void
291 ixv_attach(device_t parent, device_t dev, void *aux)
292 {
293 	struct adapter *adapter;
294 	struct ixgbe_hw *hw;
295 	int             error = 0;
296 	ixgbe_vendor_info_t *ent;
297 	const struct pci_attach_args *pa = aux;
298 
299 	INIT_DEBUGOUT("ixv_attach: begin");
300 
301 	/* Allocate, clear, and link in our adapter structure */
302 	adapter = device_private(dev);
303 	adapter->dev = dev;
304 	hw = &adapter->hw;
305 
306 #ifdef DEV_NETMAP
307 	adapter->init_locked = ixv_init_locked;
308 	adapter->stop_locked = ixv_stop;
309 #endif
310 
311 	adapter->osdep.pc = pa->pa_pc;
312 	adapter->osdep.tag = pa->pa_tag;
313 	if (pci_dma64_available(pa))
314 		adapter->osdep.dmat = pa->pa_dmat64;
315 	else
316 		adapter->osdep.dmat = pa->pa_dmat;
317 	adapter->osdep.attached = false;
318 
319 	ent = ixv_lookup(pa);
320 
321 	KASSERT(ent != NULL);
322 
323 	aprint_normal(": %s, Version - %s\n",
324 	    ixv_strings[ent->index], ixv_driver_version);
325 
326 	/* Core Lock Init*/
327 	IXGBE_CORE_LOCK_INIT(adapter, device_xname(dev));
328 
329 	/* Set up the timer callout */
330 	callout_init(&adapter->timer, 0);
331 
332 	/* Determine hardware revision */
333 	ixv_identify_hardware(adapter);
334 
335 	/* Do base PCI setup - map BAR0 */
336 	if (ixv_allocate_pci_resources(adapter, pa)) {
337 		aprint_error_dev(dev, "ixv_allocate_pci_resources() failed!\n");
338 		error = ENXIO;
339 		goto err_out;
340 	}
341 
342 	/* Sysctls for limiting the amount of work done in the taskqueues */
343 	ixv_set_sysctl_value(adapter, "rx_processing_limit",
344 	    "max number of rx packets to process",
345 	    &adapter->rx_process_limit, ixv_rx_process_limit);
346 
347 	ixv_set_sysctl_value(adapter, "tx_processing_limit",
348 	    "max number of tx packets to process",
349 	    &adapter->tx_process_limit, ixv_tx_process_limit);
350 
351 	/* Do descriptor calc and sanity checks */
352 	if (((ixv_txd * sizeof(union ixgbe_adv_tx_desc)) % DBA_ALIGN) != 0 ||
353 	    ixv_txd < MIN_TXD || ixv_txd > MAX_TXD) {
354 		aprint_error_dev(dev, "TXD config issue, using default!\n");
355 		adapter->num_tx_desc = DEFAULT_TXD;
356 	} else
357 		adapter->num_tx_desc = ixv_txd;
358 
359 	if (((ixv_rxd * sizeof(union ixgbe_adv_rx_desc)) % DBA_ALIGN) != 0 ||
360 	    ixv_rxd < MIN_RXD || ixv_rxd > MAX_RXD) {
361 		aprint_error_dev(dev, "RXD config issue, using default!\n");
362 		adapter->num_rx_desc = DEFAULT_RXD;
363 	} else
364 		adapter->num_rx_desc = ixv_rxd;
365 
366 	/* Allocate our TX/RX Queues */
367 	if (ixgbe_allocate_queues(adapter)) {
368 		aprint_error_dev(dev, "ixgbe_allocate_queues() failed!\n");
369 		error = ENOMEM;
370 		goto err_out;
371 	}
372 
373 	/*
374 	** Initialize the shared code: its
375 	** at this point the mac type is set.
376 	*/
377 	error = ixgbe_init_shared_code(hw);
378 	if (error) {
379 		aprint_error_dev(dev, "ixgbe_init_shared_code() failed!\n");
380 		error = EIO;
381 		goto err_late;
382 	}
383 
384 	/* Setup the mailbox */
385 	ixgbe_init_mbx_params_vf(hw);
386 
387 	/* Reset mbox api to 1.0 */
388 	error = ixgbe_reset_hw(hw);
389 	if (error == IXGBE_ERR_RESET_FAILED)
390 		aprint_error_dev(dev, "ixgbe_reset_hw() failure: Reset Failed!\n");
391 	else if (error)
392 		aprint_error_dev(dev, "ixgbe_reset_hw() failed with error %d\n", error);
393 	if (error) {
394 		error = EIO;
395 		goto err_late;
396 	}
397 
398 	/* Negotiate mailbox API version */
399 	error = ixgbevf_negotiate_api_version(hw, ixgbe_mbox_api_11);
400 	if (error)
401 		aprint_debug_dev(dev,
402 		    "MBX API 1.1 negotiation failed! Error %d\n", error);
403 
404 	error = ixgbe_init_hw(hw);
405 	if (error) {
406 		aprint_error_dev(dev, "ixgbe_init_hw() failed!\n");
407 		error = EIO;
408 		goto err_late;
409 	}
410 
411 	error = ixv_allocate_msix(adapter, pa);
412 	if (error) {
413 		device_printf(dev, "ixv_allocate_msix() failed!\n");
414 		goto err_late;
415 	}
416 
417 	/* If no mac address was assigned, make a random one */
418 	if (!ixv_check_ether_addr(hw->mac.addr)) {
419 		u8 addr[ETHER_ADDR_LEN];
420 		uint64_t rndval = cprng_fast64();
421 
422 		memcpy(addr, &rndval, sizeof(addr));
423 		addr[0] &= 0xFE;
424 		addr[0] |= 0x02;
425 		bcopy(addr, hw->mac.addr, sizeof(addr));
426 	}
427 
428 	/* hw.ix defaults init */
429 	adapter->enable_aim = ixv_enable_aim;
430 
431 	/* Setup OS specific network interface */
432 	ixv_setup_interface(dev, adapter);
433 
434 	/* Do the stats setup */
435 	ixv_save_stats(adapter);
436 	ixv_init_stats(adapter);
437 
438 	/* Register for VLAN events */
439 #if 0 /* XXX delete after write? */
440 	adapter->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
441 	    ixv_register_vlan, adapter, EVENTHANDLER_PRI_FIRST);
442 	adapter->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
443 	    ixv_unregister_vlan, adapter, EVENTHANDLER_PRI_FIRST);
444 #endif
445 
446 	/* Add sysctls */
447 	ixv_add_device_sysctls(adapter);
448 	ixv_add_stats_sysctls(adapter);
449 
450 #ifdef DEV_NETMAP
451 	ixgbe_netmap_attach(adapter);
452 #endif /* DEV_NETMAP */
453 	INIT_DEBUGOUT("ixv_attach: end");
454 	adapter->osdep.attached = true;
455 	return;
456 
457 err_late:
458 	ixgbe_free_transmit_structures(adapter);
459 	ixgbe_free_receive_structures(adapter);
460 err_out:
461 	ixv_free_pci_resources(adapter);
462 	return;
463 
464 }
465 
466 /*********************************************************************
467  *  Device removal routine
468  *
469  *  The detach entry point is called when the driver is being removed.
470  *  This routine stops the adapter and deallocates all the resources
471  *  that were allocated for driver operation.
472  *
473  *  return 0 on success, positive on failure
474  *********************************************************************/
475 
476 static int
477 ixv_detach(device_t dev, int flags)
478 {
479 	struct adapter *adapter = device_private(dev);
480 	struct ix_queue *que = adapter->queues;
481 	struct tx_ring *txr = adapter->tx_rings;
482 	struct rx_ring *rxr = adapter->rx_rings;
483 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
484 
485 	INIT_DEBUGOUT("ixv_detach: begin");
486 	if (adapter->osdep.attached == false)
487 		return 0;
488 
489 #if NVLAN > 0
490 	/* Make sure VLANS are not using driver */
491 	if (!VLAN_ATTACHED(&adapter->osdep.ec))
492 		;	/* nothing to do: no VLANs */
493 	else if ((flags & (DETACH_SHUTDOWN|DETACH_FORCE)) != 0)
494 		vlan_ifdetach(adapter->ifp);
495 	else {
496 		aprint_error_dev(dev, "VLANs in use, detach first\n");
497 		return EBUSY;
498 	}
499 #endif
500 
501 	IXGBE_CORE_LOCK(adapter);
502 	ixv_stop(adapter);
503 	IXGBE_CORE_UNLOCK(adapter);
504 
505 	for (int i = 0; i < adapter->num_queues; i++, que++, txr++) {
506 #ifndef IXGBE_LEGACY_TX
507 		softint_disestablish(txr->txr_si);
508 #endif
509 		softint_disestablish(que->que_si);
510 	}
511 
512 	/* Drain the Mailbox(link) queue */
513 	softint_disestablish(adapter->link_si);
514 
515 	/* Unregister VLAN events */
516 #if 0 /* XXX msaitoh delete after write? */
517 	if (adapter->vlan_attach != NULL)
518 		EVENTHANDLER_DEREGISTER(vlan_config, adapter->vlan_attach);
519 	if (adapter->vlan_detach != NULL)
520 		EVENTHANDLER_DEREGISTER(vlan_unconfig, adapter->vlan_detach);
521 #endif
522 
523 	ether_ifdetach(adapter->ifp);
524 	callout_halt(&adapter->timer, NULL);
525 #ifdef DEV_NETMAP
526 	netmap_detach(adapter->ifp);
527 #endif /* DEV_NETMAP */
528 	ixv_free_pci_resources(adapter);
529 #if 0 /* XXX the NetBSD port is probably missing something here */
530 	bus_generic_detach(dev);
531 #endif
532 	if_detach(adapter->ifp);
533 	if_percpuq_destroy(adapter->ipq);
534 
535 	sysctl_teardown(&adapter->sysctllog);
536 	evcnt_detach(&adapter->handleq);
537 	evcnt_detach(&adapter->req);
538 	evcnt_detach(&adapter->efbig_tx_dma_setup);
539 	evcnt_detach(&adapter->mbuf_defrag_failed);
540 	evcnt_detach(&adapter->efbig2_tx_dma_setup);
541 	evcnt_detach(&adapter->einval_tx_dma_setup);
542 	evcnt_detach(&adapter->other_tx_dma_setup);
543 	evcnt_detach(&adapter->eagain_tx_dma_setup);
544 	evcnt_detach(&adapter->enomem_tx_dma_setup);
545 	evcnt_detach(&adapter->watchdog_events);
546 	evcnt_detach(&adapter->tso_err);
547 	evcnt_detach(&adapter->link_irq);
548 
549 	txr = adapter->tx_rings;
550 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
551 		evcnt_detach(&adapter->queues[i].irqs);
552 		evcnt_detach(&txr->no_desc_avail);
553 		evcnt_detach(&txr->total_packets);
554 		evcnt_detach(&txr->tso_tx);
555 #ifndef IXGBE_LEGACY_TX
556 		evcnt_detach(&txr->pcq_drops);
557 #endif
558 
559 		evcnt_detach(&rxr->rx_packets);
560 		evcnt_detach(&rxr->rx_bytes);
561 		evcnt_detach(&rxr->rx_copies);
562 		evcnt_detach(&rxr->no_jmbuf);
563 		evcnt_detach(&rxr->rx_discarded);
564 	}
565 	evcnt_detach(&stats->ipcs);
566 	evcnt_detach(&stats->l4cs);
567 	evcnt_detach(&stats->ipcs_bad);
568 	evcnt_detach(&stats->l4cs_bad);
569 
570 	/* Packet Reception Stats */
571 	evcnt_detach(&stats->vfgorc);
572 	evcnt_detach(&stats->vfgprc);
573 	evcnt_detach(&stats->vfmprc);
574 
575 	/* Packet Transmission Stats */
576 	evcnt_detach(&stats->vfgotc);
577 	evcnt_detach(&stats->vfgptc);
578 
579 	ixgbe_free_transmit_structures(adapter);
580 	ixgbe_free_receive_structures(adapter);
581 
582 	IXGBE_CORE_LOCK_DESTROY(adapter);
583 	return (0);
584 }
585 
586 /*********************************************************************
587  *
588  *  Shutdown entry point
589  *
590  **********************************************************************/
591 #if 0 /* XXX NetBSD ought to register something like this through pmf(9) */
592 static int
593 ixv_shutdown(device_t dev)
594 {
595 	struct adapter *adapter = device_private(dev);
596 	IXGBE_CORE_LOCK(adapter);
597 	ixv_stop(adapter);
598 	IXGBE_CORE_UNLOCK(adapter);
599 	return (0);
600 }
601 #endif
602 
603 static int
604 ixv_ifflags_cb(struct ethercom *ec)
605 {
606 	struct ifnet *ifp = &ec->ec_if;
607 	struct adapter *adapter = ifp->if_softc;
608 	int change = ifp->if_flags ^ adapter->if_flags, rc = 0;
609 
610 	IXGBE_CORE_LOCK(adapter);
611 
612 	if (change != 0)
613 		adapter->if_flags = ifp->if_flags;
614 
615 	if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
616 		rc = ENETRESET;
617 
618 	IXGBE_CORE_UNLOCK(adapter);
619 
620 	return rc;
621 }
622 
623 /*********************************************************************
624  *  Ioctl entry point
625  *
626  *  ixv_ioctl is called when the user wants to configure the
627  *  interface.
628  *
629  *  return 0 on success, positive on failure
630  **********************************************************************/
631 
632 static int
633 ixv_ioctl(struct ifnet * ifp, u_long command, void *data)
634 {
635 	struct adapter	*adapter = ifp->if_softc;
636 	struct ifcapreq *ifcr = data;
637 	struct ifreq	*ifr = (struct ifreq *) data;
638 	int             error = 0;
639 	int l4csum_en;
640 	const int l4csum = IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx|
641 	     IFCAP_CSUM_TCPv6_Rx|IFCAP_CSUM_UDPv6_Rx;
642 
643 	switch (command) {
644 	case SIOCSIFFLAGS:
645 		IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
646 		break;
647 	case SIOCADDMULTI:
648 	case SIOCDELMULTI:
649 		IOCTL_DEBUGOUT("ioctl: SIOC(ADD|DEL)MULTI");
650 		break;
651 	case SIOCSIFMEDIA:
652 	case SIOCGIFMEDIA:
653 		IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
654 		break;
655 	case SIOCSIFCAP:
656 		IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
657 		break;
658 	case SIOCSIFMTU:
659 		IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
660 		break;
661 	default:
662 		IOCTL_DEBUGOUT1("ioctl: UNKNOWN (0x%X)", (int)command);
663 		break;
664 	}
665 
666 	switch (command) {
667 	case SIOCSIFMEDIA:
668 	case SIOCGIFMEDIA:
669 		return ifmedia_ioctl(ifp, ifr, &adapter->media, command);
670 	case SIOCSIFCAP:
671 		/* Layer-4 Rx checksum offload has to be turned on and
672 		 * off as a unit.
673 		 */
674 		l4csum_en = ifcr->ifcr_capenable & l4csum;
675 		if (l4csum_en != l4csum && l4csum_en != 0)
676 			return EINVAL;
677 		/*FALLTHROUGH*/
678 	case SIOCADDMULTI:
679 	case SIOCDELMULTI:
680 	case SIOCSIFFLAGS:
681 	case SIOCSIFMTU:
682 	default:
683 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
684 			return error;
685 		if ((ifp->if_flags & IFF_RUNNING) == 0)
686 			;
687 		else if (command == SIOCSIFCAP || command == SIOCSIFMTU) {
688 			IXGBE_CORE_LOCK(adapter);
689 			ixv_init_locked(adapter);
690 			IXGBE_CORE_UNLOCK(adapter);
691 		} else if (command == SIOCADDMULTI || command == SIOCDELMULTI) {
692 			/*
693 			 * Multicast list has changed; set the hardware filter
694 			 * accordingly.
695 			 */
696 			IXGBE_CORE_LOCK(adapter);
697 			ixv_disable_intr(adapter);
698 			ixv_set_multi(adapter);
699 			ixv_enable_intr(adapter);
700 			IXGBE_CORE_UNLOCK(adapter);
701 		}
702 		return 0;
703 	}
704 }
705 
706 /*********************************************************************
707  *  Init entry point
708  *
709  *  This routine is used in two ways. It is used by the stack as
710  *  init entry point in network interface structure. It is also used
711  *  by the driver as a hw/sw initialization routine to get to a
712  *  consistent state.
713  *
714  *  return 0 on success, positive on failure
715  **********************************************************************/
716 #define IXGBE_MHADD_MFS_SHIFT 16
717 
718 static void
719 ixv_init_locked(struct adapter *adapter)
720 {
721 	struct ifnet	*ifp = adapter->ifp;
722 	device_t 	dev = adapter->dev;
723 	struct ixgbe_hw *hw = &adapter->hw;
724 	int error = 0;
725 
726 	INIT_DEBUGOUT("ixv_init_locked: begin");
727 	KASSERT(mutex_owned(&adapter->core_mtx));
728 	hw->adapter_stopped = FALSE;
729 	ixgbe_stop_adapter(hw);
730         callout_stop(&adapter->timer);
731 
732         /* reprogram the RAR[0] in case user changed it. */
733         ixgbe_set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
734 
735 	/* Get the latest mac address, User can use a LAA */
736 	memcpy(hw->mac.addr, CLLADDR(adapter->ifp->if_sadl),
737 	     IXGBE_ETH_LENGTH_OF_ADDRESS);
738         ixgbe_set_rar(hw, 0, hw->mac.addr, 0, 1);
739 	hw->addr_ctrl.rar_used_count = 1;
740 
741 	/* Prepare transmit descriptors and buffers */
742 	if (ixgbe_setup_transmit_structures(adapter)) {
743 		aprint_error_dev(dev, "Could not setup transmit structures\n");
744 		ixv_stop(adapter);
745 		return;
746 	}
747 
748 	/* Reset VF and renegotiate mailbox API version */
749 	ixgbe_reset_hw(hw);
750 	error = ixgbevf_negotiate_api_version(hw, ixgbe_mbox_api_11);
751 	if (error)
752 		device_printf(dev, "MBX API 1.1 negotiation failed! Error %d\n", error);
753 
754 	ixv_initialize_transmit_units(adapter);
755 
756 	/* Setup Multicast table */
757 	ixv_set_multi(adapter);
758 
759 	/*
760 	** Determine the correct mbuf pool
761 	** for doing jumbo/headersplit
762 	*/
763 	if (ifp->if_mtu > ETHERMTU)
764 		adapter->rx_mbuf_sz = MJUMPAGESIZE;
765 	else
766 		adapter->rx_mbuf_sz = MCLBYTES;
767 
768 	/* Prepare receive descriptors and buffers */
769 	if (ixgbe_setup_receive_structures(adapter)) {
770 		device_printf(dev, "Could not setup receive structures\n");
771 		ixv_stop(adapter);
772 		return;
773 	}
774 
775 	/* Configure RX settings */
776 	ixv_initialize_receive_units(adapter);
777 
778 #if 0 /* XXX isn't it required? -- msaitoh  */
779 	/* Set the various hardware offload abilities */
780 	ifp->if_hwassist = 0;
781 	if (ifp->if_capenable & IFCAP_TSO4)
782 		ifp->if_hwassist |= CSUM_TSO;
783 	if (ifp->if_capenable & IFCAP_TXCSUM) {
784 		ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP);
785 #if __FreeBSD_version >= 800000
786 		ifp->if_hwassist |= CSUM_SCTP;
787 #endif
788 	}
789 #endif
790 
791 	/* Set up VLAN offload and filter */
792 	ixv_setup_vlan_support(adapter);
793 
794 	/* Set up MSI/X routing */
795 	ixv_configure_ivars(adapter);
796 
797 	/* Set up auto-mask */
798 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, IXGBE_EICS_RTX_QUEUE);
799 
800         /* Set moderation on the Link interrupt */
801         IXGBE_WRITE_REG(hw, IXGBE_VTEITR(adapter->vector), IXGBE_LINK_ITR);
802 
803 	/* Stats init */
804 	ixv_init_stats(adapter);
805 
806 	/* Config/Enable Link */
807 	ixv_config_link(adapter);
808 	hw->mac.get_link_status = TRUE;
809 
810 	/* Start watchdog */
811 	callout_reset(&adapter->timer, hz, ixv_local_timer, adapter);
812 
813 	/* And now turn on interrupts */
814 	ixv_enable_intr(adapter);
815 
816 	/* Now inform the stack we're ready */
817 	ifp->if_flags |= IFF_RUNNING;
818 	ifp->if_flags &= ~IFF_OACTIVE;
819 
820 	return;
821 }
822 
823 static int
824 ixv_init(struct ifnet *ifp)
825 {
826 	struct adapter *adapter = ifp->if_softc;
827 
828 	IXGBE_CORE_LOCK(adapter);
829 	ixv_init_locked(adapter);
830 	IXGBE_CORE_UNLOCK(adapter);
831 	return 0;
832 }
833 
834 
835 /*
836 **
837 ** MSIX Interrupt Handlers and Tasklets
838 **
839 */
840 
841 static inline void
842 ixv_enable_queue(struct adapter *adapter, u32 vector)
843 {
844 	struct ixgbe_hw *hw = &adapter->hw;
845 	u32	queue = 1 << vector;
846 	u32	mask;
847 
848 	mask = (IXGBE_EIMS_RTX_QUEUE & queue);
849 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
850 }
851 
852 static inline void
853 ixv_disable_queue(struct adapter *adapter, u32 vector)
854 {
855 	struct ixgbe_hw *hw = &adapter->hw;
856 	u64	queue = (u64)(1 << vector);
857 	u32	mask;
858 
859 	mask = (IXGBE_EIMS_RTX_QUEUE & queue);
860 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, mask);
861 }
862 
863 static inline void
864 ixv_rearm_queues(struct adapter *adapter, u64 queues)
865 {
866 	u32 mask = (IXGBE_EIMS_RTX_QUEUE & queues);
867 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEICS, mask);
868 }
869 
870 
871 static void
872 ixv_handle_que(void *context)
873 {
874 	struct ix_queue *que = context;
875 	struct adapter  *adapter = que->adapter;
876 	struct tx_ring	*txr = que->txr;
877 	struct ifnet    *ifp = adapter->ifp;
878 	bool		more;
879 
880 	adapter->handleq.ev_count++;
881 
882 	if (ifp->if_flags & IFF_RUNNING) {
883 		more = ixgbe_rxeof(que);
884 		IXGBE_TX_LOCK(txr);
885 		ixgbe_txeof(txr);
886 #ifndef IXGBE_LEGACY_TX
887 		if (pcq_peek(txr->txr_interq) != NULL)
888 			ixgbe_mq_start_locked(ifp, txr);
889 #else
890 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
891 			ixgbe_start_locked(txr, ifp);
892 #endif
893 		IXGBE_TX_UNLOCK(txr);
894 		if (more) {
895 			adapter->req.ev_count++;
896 			softint_schedule(que->que_si);
897 			return;
898 		}
899 	}
900 
901 	/* Reenable this interrupt */
902 	ixv_enable_queue(adapter, que->msix);
903 	return;
904 }
905 
906 /*********************************************************************
907  *
908  *  MSI Queue Interrupt Service routine
909  *
910  **********************************************************************/
911 int
912 ixv_msix_que(void *arg)
913 {
914 	struct ix_queue	*que = arg;
915 	struct adapter  *adapter = que->adapter;
916 #ifdef IXGBE_LEGACY_TX
917 	struct ifnet    *ifp = adapter->ifp;
918 #endif
919 	struct tx_ring	*txr = que->txr;
920 	struct rx_ring	*rxr = que->rxr;
921 	bool		more;
922 	u32		newitr = 0;
923 
924 	ixv_disable_queue(adapter, que->msix);
925 	++que->irqs.ev_count;
926 
927 #ifdef __NetBSD__
928 	/* Don't run ixgbe_rxeof in interrupt context */
929 	more = true;
930 #else
931 	more = ixgbe_rxeof(que);
932 #endif
933 
934 	IXGBE_TX_LOCK(txr);
935 	ixgbe_txeof(txr);
936 	IXGBE_TX_UNLOCK(txr);
937 
938 	/* Do AIM now? */
939 
940 	if (adapter->enable_aim == false)
941 		goto no_calc;
942 	/*
943 	** Do Adaptive Interrupt Moderation:
944         **  - Write out last calculated setting
945 	**  - Calculate based on average size over
946 	**    the last interval.
947 	*/
948         if (que->eitr_setting)
949                 IXGBE_WRITE_REG(&adapter->hw,
950                     IXGBE_VTEITR(que->msix),
951 		    que->eitr_setting);
952 
953         que->eitr_setting = 0;
954 
955         /* Idle, do nothing */
956         if ((txr->bytes == 0) && (rxr->bytes == 0))
957                 goto no_calc;
958 
959 	if ((txr->bytes) && (txr->packets))
960                	newitr = txr->bytes/txr->packets;
961 	if ((rxr->bytes) && (rxr->packets))
962 		newitr = max(newitr,
963 		    (rxr->bytes / rxr->packets));
964 	newitr += 24; /* account for hardware frame, crc */
965 
966 	/* set an upper boundary */
967 	newitr = min(newitr, 3000);
968 
969 	/* Be nice to the mid range */
970 	if ((newitr > 300) && (newitr < 1200))
971 		newitr = (newitr / 3);
972 	else
973 		newitr = (newitr / 2);
974 
975 	newitr |= newitr << 16;
976 
977         /* save for next interrupt */
978         que->eitr_setting = newitr;
979 
980         /* Reset state */
981         txr->bytes = 0;
982         txr->packets = 0;
983         rxr->bytes = 0;
984         rxr->packets = 0;
985 
986 no_calc:
987 	if (more)
988 		softint_schedule(que->que_si);
989 	else /* Reenable this interrupt */
990 		ixv_enable_queue(adapter, que->msix);
991 	return 1;
992 }
993 
994 static int
995 ixv_msix_mbx(void *arg)
996 {
997 	struct adapter	*adapter = arg;
998 	struct ixgbe_hw *hw = &adapter->hw;
999 	u32		reg;
1000 
1001 	++adapter->link_irq.ev_count;
1002 
1003 	/* First get the cause */
1004 	reg = IXGBE_READ_REG(hw, IXGBE_VTEICS);
1005 	/* Clear interrupt with write */
1006 	IXGBE_WRITE_REG(hw, IXGBE_VTEICR, reg);
1007 
1008 	/* Link status change */
1009 	if (reg & IXGBE_EICR_LSC)
1010 		softint_schedule(adapter->link_si);
1011 
1012 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, IXGBE_EIMS_OTHER);
1013 	return 1;
1014 }
1015 
1016 /*********************************************************************
1017  *
1018  *  Media Ioctl callback
1019  *
1020  *  This routine is called whenever the user queries the status of
1021  *  the interface using ifconfig.
1022  *
1023  **********************************************************************/
1024 static void
1025 ixv_media_status(struct ifnet * ifp, struct ifmediareq * ifmr)
1026 {
1027 	struct adapter *adapter = ifp->if_softc;
1028 
1029 	INIT_DEBUGOUT("ixv_media_status: begin");
1030 	IXGBE_CORE_LOCK(adapter);
1031 	ixv_update_link_status(adapter);
1032 
1033 	ifmr->ifm_status = IFM_AVALID;
1034 	ifmr->ifm_active = IFM_ETHER;
1035 
1036 	if (!adapter->link_active) {
1037 		ifmr->ifm_active |= IFM_NONE;
1038 		IXGBE_CORE_UNLOCK(adapter);
1039 		return;
1040 	}
1041 
1042 	ifmr->ifm_status |= IFM_ACTIVE;
1043 
1044 	switch (adapter->link_speed) {
1045 		case IXGBE_LINK_SPEED_10GB_FULL:
1046 			ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
1047 			break;
1048 		case IXGBE_LINK_SPEED_1GB_FULL:
1049 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
1050 			break;
1051 		case IXGBE_LINK_SPEED_100_FULL:
1052 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
1053 			break;
1054 	}
1055 
1056 	IXGBE_CORE_UNLOCK(adapter);
1057 
1058 	return;
1059 }
1060 
1061 /*********************************************************************
1062  *
1063  *  Media Ioctl callback
1064  *
1065  *  This routine is called when the user changes speed/duplex using
1066  *  media/mediopt option with ifconfig.
1067  *
1068  **********************************************************************/
1069 static int
1070 ixv_media_change(struct ifnet * ifp)
1071 {
1072 	struct adapter *adapter = ifp->if_softc;
1073 	struct ifmedia *ifm = &adapter->media;
1074 
1075 	INIT_DEBUGOUT("ixv_media_change: begin");
1076 
1077 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1078 		return (EINVAL);
1079 
1080         switch (IFM_SUBTYPE(ifm->ifm_media)) {
1081         case IFM_AUTO:
1082                 break;
1083         default:
1084                 device_printf(adapter->dev, "Only auto media type\n");
1085 		return (EINVAL);
1086         }
1087 
1088 	return (0);
1089 }
1090 
1091 
1092 /*********************************************************************
1093  *  Multicast Update
1094  *
1095  *  This routine is called whenever multicast address list is updated.
1096  *
1097  **********************************************************************/
1098 #define IXGBE_RAR_ENTRIES 16
1099 
1100 static void
1101 ixv_set_multi(struct adapter *adapter)
1102 {
1103 	struct ether_multi *enm;
1104 	struct ether_multistep step;
1105 	u8	mta[MAX_NUM_MULTICAST_ADDRESSES * IXGBE_ETH_LENGTH_OF_ADDRESS];
1106 	u8	*update_ptr;
1107 	int	mcnt = 0;
1108 	struct ethercom *ec = &adapter->osdep.ec;
1109 
1110 	IOCTL_DEBUGOUT("ixv_set_multi: begin");
1111 
1112 	ETHER_FIRST_MULTI(step, ec, enm);
1113 	while (enm != NULL) {
1114 		bcopy(enm->enm_addrlo,
1115 		    &mta[mcnt * IXGBE_ETH_LENGTH_OF_ADDRESS],
1116 		    IXGBE_ETH_LENGTH_OF_ADDRESS);
1117 		mcnt++;
1118 		/* XXX This might be required --msaitoh */
1119 		if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES)
1120 			break;
1121 		ETHER_NEXT_MULTI(step, enm);
1122 	}
1123 
1124 	update_ptr = mta;
1125 
1126 	ixgbe_update_mc_addr_list(&adapter->hw,
1127 	    update_ptr, mcnt, ixv_mc_array_itr, TRUE);
1128 
1129 	return;
1130 }
1131 
1132 /*
1133  * This is an iterator function now needed by the multicast
1134  * shared code. It simply feeds the shared code routine the
1135  * addresses in the array of ixv_set_multi() one by one.
1136  */
1137 static u8 *
1138 ixv_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
1139 {
1140 	u8 *addr = *update_ptr;
1141 	u8 *newptr;
1142 	*vmdq = 0;
1143 
1144 	newptr = addr + IXGBE_ETH_LENGTH_OF_ADDRESS;
1145 	*update_ptr = newptr;
1146 	return addr;
1147 }
1148 
1149 /*********************************************************************
1150  *  Timer routine
1151  *
1152  *  This routine checks for link status,updates statistics,
1153  *  and runs the watchdog check.
1154  *
1155  **********************************************************************/
1156 
1157 static void
1158 ixv_local_timer(void *arg)
1159 {
1160 	struct adapter *adapter = arg;
1161 
1162 	IXGBE_CORE_LOCK(adapter);
1163 	ixv_local_timer_locked(adapter);
1164 	IXGBE_CORE_UNLOCK(adapter);
1165 }
1166 
1167 static void
1168 ixv_local_timer_locked(void *arg)
1169 {
1170 	struct adapter	*adapter = arg;
1171 	device_t	dev = adapter->dev;
1172 	struct ix_queue	*que = adapter->queues;
1173 	u64		queues = 0;
1174 	int		hung = 0;
1175 
1176 	KASSERT(mutex_owned(&adapter->core_mtx));
1177 
1178 	ixv_update_link_status(adapter);
1179 
1180 	/* Stats Update */
1181 	ixv_update_stats(adapter);
1182 
1183 	/*
1184 	** Check the TX queues status
1185 	**      - mark hung queues so we don't schedule on them
1186 	**      - watchdog only if all queues show hung
1187 	*/
1188 	for (int i = 0; i < adapter->num_queues; i++, que++) {
1189 		/* Keep track of queues with work for soft irq */
1190 		if (que->txr->busy)
1191 			queues |= ((u64)1 << que->me);
1192 		/*
1193 		** Each time txeof runs without cleaning, but there
1194 		** are uncleaned descriptors it increments busy. If
1195 		** we get to the MAX we declare it hung.
1196 		*/
1197 		if (que->busy == IXGBE_QUEUE_HUNG) {
1198 			++hung;
1199 			/* Mark the queue as inactive */
1200 			adapter->active_queues &= ~((u64)1 << que->me);
1201 			continue;
1202 		} else {
1203 			/* Check if we've come back from hung */
1204 			if ((adapter->active_queues & ((u64)1 << que->me)) == 0)
1205                                 adapter->active_queues |= ((u64)1 << que->me);
1206 		}
1207 		if (que->busy >= IXGBE_MAX_TX_BUSY) {
1208 			device_printf(dev,"Warning queue %d "
1209 			    "appears to be hung!\n", i);
1210 			que->txr->busy = IXGBE_QUEUE_HUNG;
1211 			++hung;
1212 		}
1213 
1214 	}
1215 
1216 	/* Only truly watchdog if all queues show hung */
1217 	if (hung == adapter->num_queues)
1218 		goto watchdog;
1219 	else if (queues != 0) { /* Force an IRQ on queues with work */
1220 		ixv_rearm_queues(adapter, queues);
1221 	}
1222 
1223 	callout_reset(&adapter->timer, hz, ixv_local_timer, adapter);
1224 	return;
1225 
1226 watchdog:
1227 	device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
1228 	adapter->ifp->if_flags &= ~IFF_RUNNING;
1229 	adapter->watchdog_events.ev_count++;
1230 	ixv_init_locked(adapter);
1231 }
1232 
1233 /*
1234 ** Note: this routine updates the OS on the link state
1235 **	the real check of the hardware only happens with
1236 **	a link interrupt.
1237 */
1238 static void
1239 ixv_update_link_status(struct adapter *adapter)
1240 {
1241 	struct ifnet	*ifp = adapter->ifp;
1242 	device_t dev = adapter->dev;
1243 
1244 	if (adapter->link_up){
1245 		if (adapter->link_active == FALSE) {
1246 			if (bootverbose) {
1247 				const char *bpsmsg;
1248 
1249 				switch (adapter->link_speed) {
1250 				case IXGBE_LINK_SPEED_10GB_FULL:
1251 					bpsmsg = "10 Gbps";
1252 					break;
1253 				case IXGBE_LINK_SPEED_1GB_FULL:
1254 					bpsmsg = "1 Gbps";
1255 					break;
1256 				case IXGBE_LINK_SPEED_100_FULL:
1257 					bpsmsg = "100 Mbps";
1258 					break;
1259 				default:
1260 					bpsmsg = "unknown speed";
1261 					break;
1262 				}
1263 				device_printf(dev,"Link is up %s %s \n",
1264 				    bpsmsg, "Full Duplex");
1265 			}
1266 			adapter->link_active = TRUE;
1267 			if_link_state_change(ifp, LINK_STATE_UP);
1268 		}
1269 	} else { /* Link down */
1270 		if (adapter->link_active == TRUE) {
1271 			if (bootverbose)
1272 				device_printf(dev,"Link is Down\n");
1273 			if_link_state_change(ifp, LINK_STATE_DOWN);
1274 			adapter->link_active = FALSE;
1275 		}
1276 	}
1277 
1278 	return;
1279 }
1280 
1281 
1282 static void
1283 ixv_ifstop(struct ifnet *ifp, int disable)
1284 {
1285 	struct adapter *adapter = ifp->if_softc;
1286 
1287 	IXGBE_CORE_LOCK(adapter);
1288 	ixv_stop(adapter);
1289 	IXGBE_CORE_UNLOCK(adapter);
1290 }
1291 
1292 /*********************************************************************
1293  *
1294  *  This routine disables all traffic on the adapter by issuing a
1295  *  global reset on the MAC and deallocates TX/RX buffers.
1296  *
1297  **********************************************************************/
1298 
1299 static void
1300 ixv_stop(void *arg)
1301 {
1302 	struct ifnet   *ifp;
1303 	struct adapter *adapter = arg;
1304 	struct ixgbe_hw *hw = &adapter->hw;
1305 	ifp = adapter->ifp;
1306 
1307 	KASSERT(mutex_owned(&adapter->core_mtx));
1308 
1309 	INIT_DEBUGOUT("ixv_stop: begin\n");
1310 	ixv_disable_intr(adapter);
1311 
1312 	/* Tell the stack that the interface is no longer active */
1313 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1314 
1315 	ixgbe_reset_hw(hw);
1316 	adapter->hw.adapter_stopped = FALSE;
1317 	ixgbe_stop_adapter(hw);
1318 	callout_stop(&adapter->timer);
1319 
1320 	/* reprogram the RAR[0] in case user changed it. */
1321 	ixgbe_set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
1322 
1323 	return;
1324 }
1325 
1326 
1327 /*********************************************************************
1328  *
1329  *  Determine hardware revision.
1330  *
1331  **********************************************************************/
1332 static void
1333 ixv_identify_hardware(struct adapter *adapter)
1334 {
1335 	pcitag_t tag;
1336 	pci_chipset_tag_t pc;
1337 	pcireg_t subid, id;
1338 	struct ixgbe_hw *hw = &adapter->hw;
1339 
1340 	pc = adapter->osdep.pc;
1341 	tag = adapter->osdep.tag;
1342 
1343 	/*
1344 	** Make sure BUSMASTER is set, on a VM under
1345 	** KVM it may not be and will break things.
1346 	*/
1347 	ixgbe_pci_enable_busmaster(pc, tag);
1348 
1349 	id = pci_conf_read(pc, tag, PCI_ID_REG);
1350 	subid = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG);
1351 
1352 	/* Save off the information about this board */
1353 	hw->vendor_id = PCI_VENDOR(id);
1354 	hw->device_id = PCI_PRODUCT(id);
1355 	hw->revision_id = PCI_REVISION(pci_conf_read(pc, tag, PCI_CLASS_REG));
1356 	hw->subsystem_vendor_id = PCI_SUBSYS_VENDOR(subid);
1357 	hw->subsystem_device_id = PCI_SUBSYS_ID(subid);
1358 
1359 	/* We need this to determine device-specific things */
1360 	ixgbe_set_mac_type(hw);
1361 
1362 	/* Set the right number of segments */
1363 	adapter->num_segs = IXGBE_82599_SCATTER;
1364 
1365 	return;
1366 }
1367 
1368 /*********************************************************************
1369  *
1370  *  Setup MSIX Interrupt resources and handlers
1371  *
1372  **********************************************************************/
1373 static int
1374 ixv_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
1375 {
1376 	device_t	dev = adapter->dev;
1377 	struct ix_queue *que = adapter->queues;
1378 	struct		tx_ring *txr = adapter->tx_rings;
1379 	int 		error, rid, vector = 0;
1380 	pci_chipset_tag_t pc;
1381 	pcitag_t	tag;
1382 	char		intrbuf[PCI_INTRSTR_LEN];
1383 	char		intr_xname[32];
1384 	const char	*intrstr = NULL;
1385 	kcpuset_t	*affinity;
1386 	int		cpu_id = 0;
1387 
1388 	pc = adapter->osdep.pc;
1389 	tag = adapter->osdep.tag;
1390 
1391 	adapter->osdep.nintrs = adapter->num_queues + 1;
1392 	if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
1393 	    adapter->osdep.nintrs) != 0) {
1394 		aprint_error_dev(dev,
1395 		    "failed to allocate MSI-X interrupt\n");
1396 		return (ENXIO);
1397 	}
1398 
1399 	kcpuset_create(&affinity, false);
1400 	for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
1401 		snprintf(intr_xname, sizeof(intr_xname), "%s TXRX%d",
1402 		    device_xname(dev), i);
1403 		intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
1404 		    sizeof(intrbuf));
1405 #ifdef IXV_MPSAFE
1406 		pci_intr_setattr(pc, adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
1407 		    true);
1408 #endif
1409 		/* Set the handler function */
1410 		que->res = adapter->osdep.ihs[i] = pci_intr_establish_xname(pc,
1411 		    adapter->osdep.intrs[i], IPL_NET, ixv_msix_que, que,
1412 		    intr_xname);
1413 		if (que->res == NULL) {
1414 			pci_intr_release(pc, adapter->osdep.intrs,
1415 			    adapter->osdep.nintrs);
1416 			aprint_error_dev(dev,
1417 			    "Failed to register QUE handler\n");
1418 			kcpuset_destroy(affinity);
1419 			return (ENXIO);
1420 		}
1421 		que->msix = vector;
1422         	adapter->active_queues |= (u64)(1 << que->msix);
1423 
1424 		cpu_id = i;
1425 		/* Round-robin affinity */
1426 		kcpuset_zero(affinity);
1427 		kcpuset_set(affinity, cpu_id % ncpu);
1428 		error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
1429 		    NULL);
1430 		aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
1431 		    intrstr);
1432 		if (error == 0)
1433 			aprint_normal(", bound queue %d to cpu %d\n",
1434 			    i, cpu_id % ncpu);
1435 		else
1436 			aprint_normal("\n");
1437 
1438 #ifndef IXGBE_LEGACY_TX
1439 		txr->txr_si = softint_establish(SOFTINT_NET,
1440 		    ixgbe_deferred_mq_start, txr);
1441 #endif
1442 		que->que_si = softint_establish(SOFTINT_NET, ixv_handle_que,
1443 		    que);
1444 		if (que->que_si == NULL) {
1445 			aprint_error_dev(dev,
1446 			    "could not establish software interrupt\n");
1447 		}
1448 	}
1449 
1450 	/* and Mailbox */
1451 	cpu_id++;
1452 	snprintf(intr_xname, sizeof(intr_xname), "%s link", device_xname(dev));
1453 	intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
1454 	    sizeof(intrbuf));
1455 #ifdef IXG_MPSAFE
1456 	pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
1457 	    true);
1458 #endif
1459 	/* Set the mbx handler function */
1460 	adapter->osdep.ihs[vector] = pci_intr_establish_xname(pc,
1461 	    adapter->osdep.intrs[vector], IPL_NET, ixv_msix_mbx, adapter,
1462 	    intr_xname);
1463 	if (adapter->osdep.ihs[vector] == NULL) {
1464 		adapter->res = NULL;
1465 		aprint_error_dev(dev, "Failed to register LINK handler\n");
1466 		kcpuset_destroy(affinity);
1467 		return (ENXIO);
1468 	}
1469 	/* Round-robin affinity */
1470 	kcpuset_zero(affinity);
1471 	kcpuset_set(affinity, cpu_id % ncpu);
1472 	error = interrupt_distribute(adapter->osdep.ihs[vector], affinity,NULL);
1473 
1474 	aprint_normal_dev(dev,
1475 	    "for link, interrupting at %s", intrstr);
1476 	if (error == 0)
1477 		aprint_normal(", affinity to cpu %d\n", cpu_id % ncpu);
1478 	else
1479 		aprint_normal("\n");
1480 
1481 	adapter->vector = vector;
1482 	/* Tasklets for Mailbox */
1483 	adapter->link_si = softint_establish(SOFTINT_NET, ixv_handle_mbx,
1484 	    adapter);
1485 	/*
1486 	** Due to a broken design QEMU will fail to properly
1487 	** enable the guest for MSIX unless the vectors in
1488 	** the table are all set up, so we must rewrite the
1489 	** ENABLE in the MSIX control register again at this
1490 	** point to cause it to successfully initialize us.
1491 	*/
1492 	if (adapter->hw.mac.type == ixgbe_mac_82599_vf) {
1493 		int msix_ctrl;
1494 		pci_get_capability(pc, tag, PCI_CAP_MSIX, &rid, NULL);
1495 		rid += PCI_MSIX_CTL;
1496 		msix_ctrl = pci_conf_read(pc, tag, rid);
1497 		msix_ctrl |= PCI_MSIX_CTL_ENABLE;
1498 		pci_conf_write(pc, tag, rid, msix_ctrl);
1499 	}
1500 
1501 	kcpuset_destroy(affinity);
1502 	return (0);
1503 }
1504 
1505 /*
1506  * Setup MSIX resources, note that the VF
1507  * device MUST use MSIX, there is no fallback.
1508  */
1509 static int
1510 ixv_setup_msix(struct adapter *adapter)
1511 {
1512 	device_t dev = adapter->dev;
1513 	int want, queues, msgs;
1514 
1515 	/* Must have at least 2 MSIX vectors */
1516 	msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
1517 	if (msgs < 2) {
1518 		aprint_error_dev(dev,"MSIX config error\n");
1519 		return (ENXIO);
1520 	}
1521 	msgs = MIN(msgs, IXG_MAX_NINTR);
1522 
1523 	/* Figure out a reasonable auto config value */
1524 	queues = (ncpu > (msgs - 1)) ? (msgs - 1) : ncpu;
1525 
1526 	if (ixv_num_queues != 0)
1527 		queues = ixv_num_queues;
1528 	else if ((ixv_num_queues == 0) && (queues > IXGBE_VF_MAX_TX_QUEUES))
1529 		queues = IXGBE_VF_MAX_TX_QUEUES;
1530 
1531 	/*
1532 	** Want vectors for the queues,
1533 	** plus an additional for mailbox.
1534 	*/
1535 	want = queues + 1;
1536 	if (msgs >= want)
1537 		msgs = want;
1538 	else {
1539                	aprint_error_dev(dev,
1540 		    "MSIX Configuration Problem, "
1541 		    "%d vectors but %d queues wanted!\n",
1542 		    msgs, want);
1543 		return -1;
1544 	}
1545 
1546 	adapter->msix_mem = (void *)1; /* XXX */
1547 	aprint_normal_dev(dev,
1548 	    "Using MSIX interrupts with %d vectors\n", msgs);
1549 	adapter->num_queues = queues;
1550 	return (msgs);
1551 }
1552 
1553 
1554 static int
1555 ixv_allocate_pci_resources(struct adapter *adapter,
1556     const struct pci_attach_args *pa)
1557 {
1558 	pcireg_t	memtype;
1559 	device_t        dev = adapter->dev;
1560 	bus_addr_t addr;
1561 	int flags;
1562 
1563 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
1564 	switch (memtype) {
1565 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
1566 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
1567 		adapter->osdep.mem_bus_space_tag = pa->pa_memt;
1568 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
1569 	              memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
1570 			goto map_err;
1571 		if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
1572 			aprint_normal_dev(dev, "clearing prefetchable bit\n");
1573 			flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
1574 		}
1575 		if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
1576 		     adapter->osdep.mem_size, flags,
1577 		     &adapter->osdep.mem_bus_space_handle) != 0) {
1578 map_err:
1579 			adapter->osdep.mem_size = 0;
1580 			aprint_error_dev(dev, "unable to map BAR0\n");
1581 			return ENXIO;
1582 		}
1583 		break;
1584 	default:
1585 		aprint_error_dev(dev, "unexpected type on BAR0\n");
1586 		return ENXIO;
1587 	}
1588 	adapter->hw.back = adapter;
1589 
1590 	/* Pick up the tuneable queues */
1591 	adapter->num_queues = ixv_num_queues;
1592 
1593 	/*
1594 	** Now setup MSI/X, should
1595 	** return us the number of
1596 	** configured vectors.
1597 	*/
1598 	adapter->msix = ixv_setup_msix(adapter);
1599 	if (adapter->msix == ENXIO)
1600 		return (ENXIO);
1601 	else
1602 		return (0);
1603 }
1604 
1605 static void
1606 ixv_free_pci_resources(struct adapter * adapter)
1607 {
1608 	struct 		ix_queue *que = adapter->queues;
1609 	int		rid;
1610 
1611 	/*
1612 	**  Release all msix queue resources:
1613 	*/
1614 	for (int i = 0; i < adapter->num_queues; i++, que++) {
1615 		if (que->res != NULL)
1616 			pci_intr_disestablish(adapter->osdep.pc,
1617 			    adapter->osdep.ihs[i]);
1618 	}
1619 
1620 
1621 	/* Clean the Link interrupt last */
1622 	rid = adapter->vector;
1623 
1624 	if (adapter->osdep.ihs[rid] != NULL) {
1625 		pci_intr_disestablish(adapter->osdep.pc,
1626 		    adapter->osdep.ihs[rid]);
1627 		adapter->osdep.ihs[rid] = NULL;
1628 	}
1629 
1630 	pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
1631 	    adapter->osdep.nintrs);
1632 
1633 	if (adapter->osdep.mem_size != 0) {
1634 		bus_space_unmap(adapter->osdep.mem_bus_space_tag,
1635 		    adapter->osdep.mem_bus_space_handle,
1636 		    adapter->osdep.mem_size);
1637 	}
1638 
1639 	return;
1640 }
1641 
1642 /*********************************************************************
1643  *
1644  *  Setup networking device structure and register an interface.
1645  *
1646  **********************************************************************/
1647 static void
1648 ixv_setup_interface(device_t dev, struct adapter *adapter)
1649 {
1650 	struct ethercom *ec = &adapter->osdep.ec;
1651 	struct ifnet   *ifp;
1652 
1653 	INIT_DEBUGOUT("ixv_setup_interface: begin");
1654 
1655 	ifp = adapter->ifp = &ec->ec_if;
1656 	strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
1657 	ifp->if_baudrate = IF_Gbps(10);
1658 	ifp->if_init = ixv_init;
1659 	ifp->if_stop = ixv_ifstop;
1660 	ifp->if_softc = adapter;
1661 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1662 	ifp->if_ioctl = ixv_ioctl;
1663 #ifndef IXGBE_LEGACY_TX
1664 	ifp->if_transmit = ixgbe_mq_start;
1665 #endif
1666 	ifp->if_start = ixgbe_start;
1667 	IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
1668 	IFQ_SET_READY(&ifp->if_snd);
1669 
1670 	if_initialize(ifp);
1671 	adapter->ipq = if_percpuq_create(&adapter->osdep.ec.ec_if);
1672 	ether_ifattach(ifp, adapter->hw.mac.addr);
1673 	/*
1674 	 * We use per TX queue softint, so if_deferred_start_init() isn't
1675 	 * used.
1676 	 */
1677 	if_register(ifp);
1678 	ether_set_ifflags_cb(ec, ixv_ifflags_cb);
1679 
1680 	adapter->max_frame_size =
1681 	    ifp->if_mtu + IXGBE_MTU_HDR_VLAN;
1682 
1683 	/*
1684 	 * Tell the upper layer(s) we support long frames.
1685 	 */
1686 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1687 
1688 	ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSOv4;
1689 	ifp->if_capenable = 0;
1690 
1691 	ec->ec_capabilities |= ETHERCAP_VLAN_HWCSUM;
1692 	ec->ec_capabilities |= ETHERCAP_JUMBO_MTU;
1693 	ec->ec_capabilities |= ETHERCAP_VLAN_HWTAGGING
1694 	    		| ETHERCAP_VLAN_MTU;
1695 	ec->ec_capenable = ec->ec_capabilities;
1696 
1697 	/* Don't enable LRO by default */
1698 	ifp->if_capabilities |= IFCAP_LRO;
1699 #if 0
1700 	ifp->if_capenable = ifp->if_capabilities;
1701 #endif
1702 
1703 	/*
1704 	** Dont turn this on by default, if vlans are
1705 	** created on another pseudo device (eg. lagg)
1706 	** then vlan events are not passed thru, breaking
1707 	** operation, but with HW FILTER off it works. If
1708 	** using vlans directly on the em driver you can
1709 	** enable this and get full hardware tag filtering.
1710 	*/
1711 	ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER;
1712 
1713 	/*
1714 	 * Specify the media types supported by this adapter and register
1715 	 * callbacks to update media and link information
1716 	 */
1717 	ifmedia_init(&adapter->media, IFM_IMASK, ixv_media_change,
1718 		     ixv_media_status);
1719 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1720 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
1721 
1722 	return;
1723 }
1724 
1725 static void
1726 ixv_config_link(struct adapter *adapter)
1727 {
1728 	struct ixgbe_hw *hw = &adapter->hw;
1729 
1730 	if (hw->mac.ops.check_link)
1731 		hw->mac.ops.check_link(hw, &adapter->link_speed,
1732 		    &adapter->link_up, FALSE);
1733 }
1734 
1735 
1736 /*********************************************************************
1737  *
1738  *  Enable transmit unit.
1739  *
1740  **********************************************************************/
1741 static void
1742 ixv_initialize_transmit_units(struct adapter *adapter)
1743 {
1744 	struct tx_ring	*txr = adapter->tx_rings;
1745 	struct ixgbe_hw	*hw = &adapter->hw;
1746 
1747 
1748 	for (int i = 0; i < adapter->num_queues; i++, txr++) {
1749 		u64	tdba = txr->txdma.dma_paddr;
1750 		u32	txctrl, txdctl;
1751 
1752 		/* Set WTHRESH to 8, burst writeback */
1753 		txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(i));
1754 		txdctl |= (8 << 16);
1755 		IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(i), txdctl);
1756 
1757 		/* Set the HW Tx Head and Tail indices */
1758 	    	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VFTDH(i), 0);
1759 	    	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VFTDT(i), 0);
1760 
1761 		/* Set Tx Tail register */
1762 		txr->tail = IXGBE_VFTDT(i);
1763 
1764 		/* Set Ring parameters */
1765 		IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(i),
1766 		       (tdba & 0x00000000ffffffffULL));
1767 		IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(i), (tdba >> 32));
1768 		IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(i),
1769 		    adapter->num_tx_desc *
1770 		    sizeof(struct ixgbe_legacy_tx_desc));
1771 		txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(i));
1772 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
1773 		IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(i), txctrl);
1774 
1775 		/* Now enable */
1776 		txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(i));
1777 		txdctl |= IXGBE_TXDCTL_ENABLE;
1778 		IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(i), txdctl);
1779 	}
1780 
1781 	return;
1782 }
1783 
1784 
1785 /*********************************************************************
1786  *
1787  *  Setup receive registers and features.
1788  *
1789  **********************************************************************/
1790 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
1791 
1792 static void
1793 ixv_initialize_receive_units(struct adapter *adapter)
1794 {
1795 	struct	rx_ring	*rxr = adapter->rx_rings;
1796 	struct ixgbe_hw	*hw = &adapter->hw;
1797 	struct ifnet	*ifp = adapter->ifp;
1798 	u32		bufsz, rxcsum, psrtype;
1799 
1800 	if (ifp->if_mtu > ETHERMTU)
1801 		bufsz = 4096 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1802 	else
1803 		bufsz = 2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
1804 
1805 	psrtype = IXGBE_PSRTYPE_TCPHDR | IXGBE_PSRTYPE_UDPHDR |
1806 	    IXGBE_PSRTYPE_IPV4HDR | IXGBE_PSRTYPE_IPV6HDR |
1807 	    IXGBE_PSRTYPE_L2HDR;
1808 
1809 	IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
1810 
1811 	/* Tell PF our max_frame size */
1812 	ixgbevf_rlpml_set_vf(hw, adapter->max_frame_size);
1813 
1814 	for (int i = 0; i < adapter->num_queues; i++, rxr++) {
1815 		u64 rdba = rxr->rxdma.dma_paddr;
1816 		u32 reg, rxdctl;
1817 
1818 		/* Disable the queue */
1819 		rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i));
1820 		rxdctl &= ~IXGBE_RXDCTL_ENABLE;
1821 		IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(i), rxdctl);
1822 		for (int j = 0; j < 10; j++) {
1823 			if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i)) &
1824 			    IXGBE_RXDCTL_ENABLE)
1825 				msec_delay(1);
1826 			else
1827 				break;
1828 		}
1829 		wmb();
1830 		/* Setup the Base and Length of the Rx Descriptor Ring */
1831 		IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(i),
1832 		    (rdba & 0x00000000ffffffffULL));
1833 		IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(i),
1834 		    (rdba >> 32));
1835 		IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(i),
1836 		    adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
1837 
1838 		/* Reset the ring indices */
1839 		IXGBE_WRITE_REG(hw, IXGBE_VFRDH(rxr->me), 0);
1840 		IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me), 0);
1841 
1842 		/* Set up the SRRCTL register */
1843 		reg = IXGBE_READ_REG(hw, IXGBE_VFSRRCTL(i));
1844 		reg &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
1845 		reg &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
1846 		reg |= bufsz;
1847 		reg |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
1848 		IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(i), reg);
1849 
1850 		/* Capture Rx Tail index */
1851 		rxr->tail = IXGBE_VFRDT(rxr->me);
1852 
1853 		/* Do the queue enabling last */
1854 		rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
1855 		IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(i), rxdctl);
1856 		for (int k = 0; k < 10; k++) {
1857 			if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i)) &
1858 			    IXGBE_RXDCTL_ENABLE)
1859 				break;
1860 			else
1861 				msec_delay(1);
1862 		}
1863 		wmb();
1864 
1865 		/* Set the Tail Pointer */
1866 #ifdef DEV_NETMAP
1867 		/*
1868 		 * In netmap mode, we must preserve the buffers made
1869 		 * available to userspace before the if_init()
1870 		 * (this is true by default on the TX side, because
1871 		 * init makes all buffers available to userspace).
1872 		 *
1873 		 * netmap_reset() and the device specific routines
1874 		 * (e.g. ixgbe_setup_receive_rings()) map these
1875 		 * buffers at the end of the NIC ring, so here we
1876 		 * must set the RDT (tail) register to make sure
1877 		 * they are not overwritten.
1878 		 *
1879 		 * In this driver the NIC ring starts at RDH = 0,
1880 		 * RDT points to the last slot available for reception (?),
1881 		 * so RDT = num_rx_desc - 1 means the whole ring is available.
1882 		 */
1883 		if (ifp->if_capenable & IFCAP_NETMAP) {
1884 			struct netmap_adapter *na = NA(adapter->ifp);
1885 			struct netmap_kring *kring = &na->rx_rings[i];
1886 			int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
1887 
1888 			IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me), t);
1889 		} else
1890 #endif /* DEV_NETMAP */
1891 			IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me),
1892 			    adapter->num_rx_desc - 1);
1893 	}
1894 
1895 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
1896 
1897 	if (ifp->if_capenable & IFCAP_RXCSUM)
1898 		rxcsum |= IXGBE_RXCSUM_PCSD;
1899 
1900 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
1901 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
1902 
1903 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
1904 
1905 	return;
1906 }
1907 
1908 static void
1909 ixv_setup_vlan_support(struct adapter *adapter)
1910 {
1911 	struct ixgbe_hw *hw = &adapter->hw;
1912 	u32		ctrl, vid, vfta, retry;
1913 	struct rx_ring	*rxr;
1914 
1915 	/*
1916 	** We get here thru init_locked, meaning
1917 	** a soft reset, this has already cleared
1918 	** the VFTA and other state, so if there
1919 	** have been no vlan's registered do nothing.
1920 	*/
1921 	if (!VLAN_ATTACHED(&adapter->osdep.ec))
1922 		return;
1923 
1924 	/* Enable the queues */
1925 	for (int i = 0; i < adapter->num_queues; i++) {
1926 		ctrl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i));
1927 		ctrl |= IXGBE_RXDCTL_VME;
1928 		IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(i), ctrl);
1929 		/*
1930 		 * Let Rx path know that it needs to store VLAN tag
1931 		 * as part of extra mbuf info.
1932 		 */
1933 		rxr = &adapter->rx_rings[i];
1934 		rxr->vtag_strip = TRUE;
1935 	}
1936 
1937 	/*
1938 	** A soft reset zero's out the VFTA, so
1939 	** we need to repopulate it now.
1940 	*/
1941 	for (int i = 0; i < IXGBE_VFTA_SIZE; i++) {
1942 		if (ixv_shadow_vfta[i] == 0)
1943 			continue;
1944 		vfta = ixv_shadow_vfta[i];
1945 		/*
1946 		** Reconstruct the vlan id's
1947 		** based on the bits set in each
1948 		** of the array ints.
1949 		*/
1950 		for (int j = 0; j < 32; j++) {
1951 			retry = 0;
1952 			if ((vfta & (1 << j)) == 0)
1953 				continue;
1954 			vid = (i * 32) + j;
1955 			/* Call the shared code mailbox routine */
1956 			while (ixgbe_set_vfta(hw, vid, 0, TRUE)) {
1957 				if (++retry > 5)
1958 					break;
1959 			}
1960 		}
1961 	}
1962 }
1963 
1964 #if 0	/* XXX Badly need to overhaul vlan(4) on NetBSD. */
1965 /*
1966 ** This routine is run via an vlan config EVENT,
1967 ** it enables us to use the HW Filter table since
1968 ** we can get the vlan id. This just creates the
1969 ** entry in the soft version of the VFTA, init will
1970 ** repopulate the real table.
1971 */
1972 static void
1973 ixv_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
1974 {
1975 	struct adapter	*adapter = ifp->if_softc;
1976 	u16		index, bit;
1977 
1978 	if (ifp->if_softc != arg) /* Not our event */
1979 		return;
1980 
1981 	if ((vtag == 0) || (vtag > 4095)) /* Invalid */
1982 		return;
1983 
1984 	IXGBE_CORE_LOCK(adapter);
1985 	index = (vtag >> 5) & 0x7F;
1986 	bit = vtag & 0x1F;
1987 	ixv_shadow_vfta[index] |= (1 << bit);
1988 	/* Re-init to load the changes */
1989 	ixv_init_locked(adapter);
1990 	IXGBE_CORE_UNLOCK(adapter);
1991 }
1992 
1993 /*
1994 ** This routine is run via an vlan
1995 ** unconfig EVENT, remove our entry
1996 ** in the soft vfta.
1997 */
1998 static void
1999 ixv_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
2000 {
2001 	struct adapter	*adapter = ifp->if_softc;
2002 	u16		index, bit;
2003 
2004 	if (ifp->if_softc !=  arg)
2005 		return;
2006 
2007 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
2008 		return;
2009 
2010 	IXGBE_CORE_LOCK(adapter);
2011 	index = (vtag >> 5) & 0x7F;
2012 	bit = vtag & 0x1F;
2013 	ixv_shadow_vfta[index] &= ~(1 << bit);
2014 	/* Re-init to load the changes */
2015 	ixv_init_locked(adapter);
2016 	IXGBE_CORE_UNLOCK(adapter);
2017 }
2018 #endif
2019 
2020 static void
2021 ixv_enable_intr(struct adapter *adapter)
2022 {
2023 	struct ixgbe_hw *hw = &adapter->hw;
2024 	struct ix_queue *que = adapter->queues;
2025 	u32 mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
2026 
2027 
2028 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
2029 
2030 	mask = IXGBE_EIMS_ENABLE_MASK;
2031 	mask &= ~(IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC);
2032 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, mask);
2033 
2034         for (int i = 0; i < adapter->num_queues; i++, que++)
2035 		ixv_enable_queue(adapter, que->msix);
2036 
2037 	IXGBE_WRITE_FLUSH(hw);
2038 
2039 	return;
2040 }
2041 
2042 static void
2043 ixv_disable_intr(struct adapter *adapter)
2044 {
2045 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEIAC, 0);
2046 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEIMC, ~0);
2047 	IXGBE_WRITE_FLUSH(&adapter->hw);
2048 	return;
2049 }
2050 
2051 /*
2052 ** Setup the correct IVAR register for a particular MSIX interrupt
2053 **  - entry is the register array entry
2054 **  - vector is the MSIX vector for this queue
2055 **  - type is RX/TX/MISC
2056 */
2057 static void
2058 ixv_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
2059 {
2060 	struct ixgbe_hw *hw = &adapter->hw;
2061 	u32 ivar, index;
2062 
2063 	vector |= IXGBE_IVAR_ALLOC_VAL;
2064 
2065 	if (type == -1) { /* MISC IVAR */
2066 		ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
2067 		ivar &= ~0xFF;
2068 		ivar |= vector;
2069 		IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
2070 	} else {	/* RX/TX IVARS */
2071 		index = (16 * (entry & 1)) + (8 * type);
2072 		ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(entry >> 1));
2073 		ivar &= ~(0xFF << index);
2074 		ivar |= (vector << index);
2075 		IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(entry >> 1), ivar);
2076 	}
2077 }
2078 
2079 static void
2080 ixv_configure_ivars(struct adapter *adapter)
2081 {
2082 	struct  ix_queue *que = adapter->queues;
2083 
2084         for (int i = 0; i < adapter->num_queues; i++, que++) {
2085 		/* First the RX queue entry */
2086                 ixv_set_ivar(adapter, i, que->msix, 0);
2087 		/* ... and the TX */
2088 		ixv_set_ivar(adapter, i, que->msix, 1);
2089 		/* Set an initial value in EITR */
2090                 IXGBE_WRITE_REG(&adapter->hw,
2091                     IXGBE_VTEITR(que->msix), IXV_EITR_DEFAULT);
2092 	}
2093 
2094 	/* For the mailbox interrupt */
2095         ixv_set_ivar(adapter, 1, adapter->vector, -1);
2096 }
2097 
2098 
2099 /*
2100 ** Tasklet handler for MSIX MBX interrupts
2101 **  - do outside interrupt since it might sleep
2102 */
2103 static void
2104 ixv_handle_mbx(void *context)
2105 {
2106 	struct adapter  *adapter = context;
2107 
2108 	ixgbe_check_link(&adapter->hw,
2109 	    &adapter->link_speed, &adapter->link_up, 0);
2110 	ixv_update_link_status(adapter);
2111 }
2112 
2113 /*
2114 ** The VF stats registers never have a truly virgin
2115 ** starting point, so this routine tries to make an
2116 ** artificial one, marking ground zero on attach as
2117 ** it were.
2118 */
2119 static void
2120 ixv_save_stats(struct adapter *adapter)
2121 {
2122 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
2123 
2124 	if (stats->vfgprc.ev_count || stats->vfgptc.ev_count) {
2125 		stats->saved_reset_vfgprc +=
2126 		    stats->vfgprc.ev_count - stats->base_vfgprc;
2127 		stats->saved_reset_vfgptc +=
2128 		    stats->vfgptc.ev_count - stats->base_vfgptc;
2129 		stats->saved_reset_vfgorc +=
2130 		    stats->vfgorc.ev_count - stats->base_vfgorc;
2131 		stats->saved_reset_vfgotc +=
2132 		    stats->vfgotc.ev_count - stats->base_vfgotc;
2133 		stats->saved_reset_vfmprc +=
2134 		    stats->vfmprc.ev_count - stats->base_vfmprc;
2135 	}
2136 }
2137 
2138 static void
2139 ixv_init_stats(struct adapter *adapter)
2140 {
2141 	struct ixgbe_hw *hw = &adapter->hw;
2142 
2143 	adapter->stats.vf.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
2144 	adapter->stats.vf.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
2145 	adapter->stats.vf.last_vfgorc |=
2146 	    (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
2147 
2148 	adapter->stats.vf.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
2149 	adapter->stats.vf.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
2150 	adapter->stats.vf.last_vfgotc |=
2151 	    (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
2152 
2153 	adapter->stats.vf.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
2154 
2155 	adapter->stats.vf.base_vfgprc = adapter->stats.vf.last_vfgprc;
2156 	adapter->stats.vf.base_vfgorc = adapter->stats.vf.last_vfgorc;
2157 	adapter->stats.vf.base_vfgptc = adapter->stats.vf.last_vfgptc;
2158 	adapter->stats.vf.base_vfgotc = adapter->stats.vf.last_vfgotc;
2159 	adapter->stats.vf.base_vfmprc = adapter->stats.vf.last_vfmprc;
2160 }
2161 
2162 #define UPDATE_STAT_32(reg, last, count)		\
2163 {							\
2164 	u32 current = IXGBE_READ_REG(hw, reg);		\
2165 	if (current < last)				\
2166 		count.ev_count += 0x100000000LL;	\
2167 	last = current;					\
2168 	count.ev_count &= 0xFFFFFFFF00000000LL;		\
2169 	count.ev_count |= current;			\
2170 }
2171 
2172 #define UPDATE_STAT_36(lsb, msb, last, count) 		\
2173 {							\
2174 	u64 cur_lsb = IXGBE_READ_REG(hw, lsb);		\
2175 	u64 cur_msb = IXGBE_READ_REG(hw, msb);		\
2176 	u64 current = ((cur_msb << 32) | cur_lsb);	\
2177 	if (current < last)				\
2178 		count.ev_count += 0x1000000000LL;	\
2179 	last = current;					\
2180 	count.ev_count &= 0xFFFFFFF000000000LL;		\
2181 	count.ev_count |= current;			\
2182 }
2183 
2184 /*
2185 ** ixv_update_stats - Update the board statistics counters.
2186 */
2187 void
2188 ixv_update_stats(struct adapter *adapter)
2189 {
2190         struct ixgbe_hw *hw = &adapter->hw;
2191 
2192         UPDATE_STAT_32(IXGBE_VFGPRC, adapter->stats.vf.last_vfgprc,
2193 	    adapter->stats.vf.vfgprc);
2194         UPDATE_STAT_32(IXGBE_VFGPTC, adapter->stats.vf.last_vfgptc,
2195 	    adapter->stats.vf.vfgptc);
2196         UPDATE_STAT_36(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2197 	    adapter->stats.vf.last_vfgorc, adapter->stats.vf.vfgorc);
2198         UPDATE_STAT_36(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2199 	    adapter->stats.vf.last_vfgotc, adapter->stats.vf.vfgotc);
2200         UPDATE_STAT_32(IXGBE_VFMPRC, adapter->stats.vf.last_vfmprc,
2201 	    adapter->stats.vf.vfmprc);
2202 }
2203 
2204 /**********************************************************************
2205  *
2206  *  This routine is called only when em_display_debug_stats is enabled.
2207  *  This routine provides a way to take a look at important statistics
2208  *  maintained by the driver and hardware.
2209  *
2210  **********************************************************************/
2211 static void
2212 ixv_print_debug_info(struct adapter *adapter)
2213 {
2214         device_t dev = adapter->dev;
2215         struct ixgbe_hw         *hw = &adapter->hw;
2216         struct ix_queue         *que = adapter->queues;
2217         struct rx_ring          *rxr;
2218         struct tx_ring          *txr;
2219 #ifdef LRO
2220         struct lro_ctrl         *lro;
2221 #endif /* LRO */
2222 
2223         device_printf(dev,"Error Byte Count = %u \n",
2224             IXGBE_READ_REG(hw, IXGBE_ERRBC));
2225 
2226         for (int i = 0; i < adapter->num_queues; i++, que++) {
2227                 txr = que->txr;
2228                 rxr = que->rxr;
2229 #ifdef LRO
2230                 lro = &rxr->lro;
2231 #endif /* LRO */
2232                 device_printf(dev,"QUE(%d) IRQs Handled: %lu\n",
2233                     que->msix, (long)que->irqs.ev_count);
2234                 device_printf(dev,"RX(%d) Packets Received: %lld\n",
2235                     rxr->me, (long long)rxr->rx_packets.ev_count);
2236                 device_printf(dev,"RX(%d) Bytes Received: %lu\n",
2237                     rxr->me, (long)rxr->rx_bytes.ev_count);
2238 #ifdef LRO
2239                 device_printf(dev,"RX(%d) LRO Queued= %lld\n",
2240                     rxr->me, (long long)lro->lro_queued);
2241                 device_printf(dev,"RX(%d) LRO Flushed= %lld\n",
2242                     rxr->me, (long long)lro->lro_flushed);
2243 #endif /* LRO */
2244                 device_printf(dev,"TX(%d) Packets Sent: %lu\n",
2245                     txr->me, (long)txr->total_packets.ev_count);
2246                 device_printf(dev,"TX(%d) NO Desc Avail: %lu\n",
2247                     txr->me, (long)txr->no_desc_avail.ev_count);
2248         }
2249 
2250         device_printf(dev,"MBX IRQ Handled: %lu\n",
2251             (long)adapter->link_irq.ev_count);
2252         return;
2253 }
2254 
2255 static int
2256 ixv_sysctl_debug(SYSCTLFN_ARGS)
2257 {
2258 	struct sysctlnode node;
2259 	int error, result;
2260 	struct adapter *adapter;
2261 
2262 	node = *rnode;
2263 	adapter = (struct adapter *)node.sysctl_data;
2264 	node.sysctl_data = &result;
2265 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2266 
2267 	if (error)
2268 		return error;
2269 
2270 	if (result == 1)
2271 		ixv_print_debug_info(adapter);
2272 
2273 	return 0;
2274 }
2275 
2276 const struct sysctlnode *
2277 ixv_sysctl_instance(struct adapter *adapter)
2278 {
2279 	const char *dvname;
2280 	struct sysctllog **log;
2281 	int rc;
2282 	const struct sysctlnode *rnode;
2283 
2284 	log = &adapter->sysctllog;
2285 	dvname = device_xname(adapter->dev);
2286 
2287 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
2288 	    0, CTLTYPE_NODE, dvname,
2289 	    SYSCTL_DESCR("ixv information and settings"),
2290 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
2291 		goto err;
2292 
2293 	return rnode;
2294 err:
2295 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
2296 	return NULL;
2297 }
2298 
2299 static void
2300 ixv_add_device_sysctls(struct adapter *adapter)
2301 {
2302 	struct sysctllog **log;
2303 	const struct sysctlnode *rnode, *cnode;
2304 	device_t dev;
2305 
2306 	dev = adapter->dev;
2307 	log = &adapter->sysctllog;
2308 
2309 	if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
2310 		aprint_error_dev(dev, "could not create sysctl root\n");
2311 		return;
2312 	}
2313 
2314 	if (sysctl_createv(log, 0, &rnode, &cnode,
2315 	    CTLFLAG_READWRITE, CTLTYPE_INT,
2316 	    "debug", SYSCTL_DESCR("Debug Info"),
2317 	    ixv_sysctl_debug, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
2318 		aprint_error_dev(dev, "could not create sysctl\n");
2319 
2320 	if (sysctl_createv(log, 0, &rnode, &cnode,
2321 	    CTLFLAG_READWRITE, CTLTYPE_BOOL,
2322 	    "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
2323 	    NULL, 0, &adapter->enable_aim, 0, CTL_CREATE, CTL_EOL) != 0)
2324 		aprint_error_dev(dev, "could not create sysctl\n");
2325 }
2326 
2327 /*
2328  * Add statistic sysctls for the VF.
2329  */
2330 static void
2331 ixv_add_stats_sysctls(struct adapter *adapter)
2332 {
2333 	device_t dev = adapter->dev;
2334 	const struct sysctlnode *rnode;
2335 	struct sysctllog **log = &adapter->sysctllog;
2336 	struct ix_queue *que = &adapter->queues[0];
2337 	struct tx_ring *txr = que->txr;
2338 	struct rx_ring *rxr = que->rxr;
2339 
2340 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
2341 	const char *xname = device_xname(dev);
2342 
2343 	/* Driver Statistics */
2344 	evcnt_attach_dynamic(&adapter->handleq, EVCNT_TYPE_MISC,
2345 	    NULL, xname, "Handled queue in softint");
2346 	evcnt_attach_dynamic(&adapter->req, EVCNT_TYPE_MISC,
2347 	    NULL, xname, "Requeued in softint");
2348 	evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
2349 	    NULL, xname, "Driver tx dma soft fail EFBIG");
2350 	evcnt_attach_dynamic(&adapter->mbuf_defrag_failed, EVCNT_TYPE_MISC,
2351 	    NULL, xname, "m_defrag() failed");
2352 	evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
2353 	    NULL, xname, "Driver tx dma hard fail EFBIG");
2354 	evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
2355 	    NULL, xname, "Driver tx dma hard fail EINVAL");
2356 	evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
2357 	    NULL, xname, "Driver tx dma hard fail other");
2358 	evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
2359 	    NULL, xname, "Driver tx dma soft fail EAGAIN");
2360 	evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
2361 	    NULL, xname, "Driver tx dma soft fail ENOMEM");
2362 	evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
2363 	    NULL, xname, "Watchdog timeouts");
2364 	evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
2365 	    NULL, xname, "TSO errors");
2366 	evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_INTR,
2367 	    NULL, xname, "Link MSIX IRQ Handled");
2368 
2369 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
2370 		snprintf(adapter->queues[i].evnamebuf,
2371 		    sizeof(adapter->queues[i].evnamebuf), "%s q%d",
2372 		    xname, i);
2373 		snprintf(adapter->queues[i].namebuf,
2374 		    sizeof(adapter->queues[i].namebuf), "q%d", i);
2375 
2376 		if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
2377 			aprint_error_dev(dev, "could not create sysctl root\n");
2378 			break;
2379 		}
2380 
2381 		if (sysctl_createv(log, 0, &rnode, &rnode,
2382 		    0, CTLTYPE_NODE,
2383 		    adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
2384 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
2385 			break;
2386 
2387 #if 0 /* not yet */
2388 		if (sysctl_createv(log, 0, &rnode, &cnode,
2389 		    CTLFLAG_READWRITE, CTLTYPE_INT,
2390 		    "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
2391 		    ixgbe_sysctl_interrupt_rate_handler, 0,
2392 		    (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
2393 			break;
2394 
2395 		if (sysctl_createv(log, 0, &rnode, &cnode,
2396 		    CTLFLAG_READONLY, CTLTYPE_QUAD,
2397 		    "irqs", SYSCTL_DESCR("irqs on this queue"),
2398 			NULL, 0, &(adapter->queues[i].irqs),
2399 		    0, CTL_CREATE, CTL_EOL) != 0)
2400 			break;
2401 
2402 		if (sysctl_createv(log, 0, &rnode, &cnode,
2403 		    CTLFLAG_READONLY, CTLTYPE_INT,
2404 		    "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
2405 		    ixgbe_sysctl_tdh_handler, 0, (void *)txr,
2406 		    0, CTL_CREATE, CTL_EOL) != 0)
2407 			break;
2408 
2409 		if (sysctl_createv(log, 0, &rnode, &cnode,
2410 		    CTLFLAG_READONLY, CTLTYPE_INT,
2411 		    "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
2412 		    ixgbe_sysctl_tdt_handler, 0, (void *)txr,
2413 		    0, CTL_CREATE, CTL_EOL) != 0)
2414 			break;
2415 #endif
2416 		evcnt_attach_dynamic(&adapter->queues[i].irqs, EVCNT_TYPE_INTR,
2417 		    NULL, adapter->queues[i].evnamebuf, "IRQs on queue");
2418 		evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
2419 		    NULL, adapter->queues[i].evnamebuf, "TSO");
2420 		evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
2421 		    NULL, adapter->queues[i].evnamebuf,
2422 		    "Queue No Descriptor Available");
2423 		evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
2424 		    NULL, adapter->queues[i].evnamebuf,
2425 		    "Queue Packets Transmitted");
2426 #ifndef IXGBE_LEGACY_TX
2427 		evcnt_attach_dynamic(&txr->pcq_drops, EVCNT_TYPE_MISC,
2428 		    NULL, adapter->queues[i].evnamebuf,
2429 		    "Packets dropped in pcq");
2430 #endif
2431 
2432 #ifdef LRO
2433 		struct lro_ctrl *lro = &rxr->lro;
2434 #endif /* LRO */
2435 
2436 #if 0 /* not yet */
2437 		if (sysctl_createv(log, 0, &rnode, &cnode,
2438 		    CTLFLAG_READONLY,
2439 		    CTLTYPE_INT,
2440 		    "rxd_head", SYSCTL_DESCR("Receive Descriptor Head"),
2441 		    ixgbe_sysctl_rdh_handler, 0, (void *)rxr, 0,
2442 		    CTL_CREATE, CTL_EOL) != 0)
2443 			break;
2444 
2445 		if (sysctl_createv(log, 0, &rnode, &cnode,
2446 		    CTLFLAG_READONLY,
2447 		    CTLTYPE_INT,
2448 		    "rxd_tail", SYSCTL_DESCR("Receive Descriptor Tail"),
2449 		    ixgbe_sysctl_rdt_handler, 0, (void *)rxr, 0,
2450 		    CTL_CREATE, CTL_EOL) != 0)
2451 			break;
2452 #endif
2453 
2454 		evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
2455 		    NULL, adapter->queues[i].evnamebuf, "Queue Packets Received");
2456 		evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
2457 		    NULL, adapter->queues[i].evnamebuf, "Queue Bytes Received");
2458 		evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
2459 		    NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
2460 		evcnt_attach_dynamic(&rxr->no_jmbuf, EVCNT_TYPE_MISC,
2461 		    NULL, adapter->queues[i].evnamebuf, "Rx no jumbo mbuf");
2462 		evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
2463 		    NULL, adapter->queues[i].evnamebuf, "Rx discarded");
2464 #ifdef LRO
2465 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
2466 				CTLFLAG_RD, &lro->lro_queued, 0,
2467 				"LRO Queued");
2468 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
2469 				CTLFLAG_RD, &lro->lro_flushed, 0,
2470 				"LRO Flushed");
2471 #endif /* LRO */
2472 	}
2473 
2474 	/* MAC stats get the own sub node */
2475 
2476 	snprintf(stats->namebuf,
2477 	    sizeof(stats->namebuf), "%s MAC Statistics", xname);
2478 
2479 	evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
2480 	    stats->namebuf, "rx csum offload - IP");
2481 	evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
2482 	    stats->namebuf, "rx csum offload - L4");
2483 	evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
2484 	    stats->namebuf, "rx csum offload - IP bad");
2485 	evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
2486 	    stats->namebuf, "rx csum offload - L4 bad");
2487 
2488 	/* Packet Reception Stats */
2489 	evcnt_attach_dynamic(&stats->vfgprc, EVCNT_TYPE_MISC, NULL,
2490 	    xname, "Good Packets Received");
2491 	evcnt_attach_dynamic(&stats->vfgorc, EVCNT_TYPE_MISC, NULL,
2492 	    xname, "Good Octets Received");
2493 	evcnt_attach_dynamic(&stats->vfmprc, EVCNT_TYPE_MISC, NULL,
2494 	    xname, "Multicast Packets Received");
2495 	evcnt_attach_dynamic(&stats->vfgptc, EVCNT_TYPE_MISC, NULL,
2496 	    xname, "Good Packets Transmitted");
2497 	evcnt_attach_dynamic(&stats->vfgotc, EVCNT_TYPE_MISC, NULL,
2498 	    xname, "Good Octets Transmitted");
2499 }
2500 
2501 static void
2502 ixv_set_sysctl_value(struct adapter *adapter, const char *name,
2503 	const char *description, int *limit, int value)
2504 {
2505 	device_t dev =  adapter->dev;
2506 	struct sysctllog **log;
2507 	const struct sysctlnode *rnode, *cnode;
2508 
2509 	log = &adapter->sysctllog;
2510 	if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
2511 		aprint_error_dev(dev, "could not create sysctl root\n");
2512 		return;
2513 	}
2514 	if (sysctl_createv(log, 0, &rnode, &cnode,
2515 	    CTLFLAG_READWRITE, CTLTYPE_INT,
2516 	    name, SYSCTL_DESCR(description),
2517 	    NULL, 0, limit, 0, CTL_CREATE, CTL_EOL) != 0)
2518 		aprint_error_dev(dev, "could not create sysctl\n");
2519 	*limit = value;
2520 }
2521