Home | History | Annotate | Line # | Download | only in ixgbe
ixv.c revision 1.132
      1 /*$NetBSD: ixv.c,v 1.132 2019/09/12 06:19:47 msaitoh Exp $*/
      2 
      3 /******************************************************************************
      4 
      5   Copyright (c) 2001-2017, Intel Corporation
      6   All rights reserved.
      7 
      8   Redistribution and use in source and binary forms, with or without
      9   modification, are permitted provided that the following conditions are met:
     10 
     11    1. Redistributions of source code must retain the above copyright notice,
     12       this list of conditions and the following disclaimer.
     13 
     14    2. Redistributions in binary form must reproduce the above copyright
     15       notice, this list of conditions and the following disclaimer in the
     16       documentation and/or other materials provided with the distribution.
     17 
     18    3. Neither the name of the Intel Corporation nor the names of its
     19       contributors may be used to endorse or promote products derived from
     20       this software without specific prior written permission.
     21 
     22   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     23   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     26   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32   POSSIBILITY OF SUCH DAMAGE.
     33 
     34 ******************************************************************************/
     35 /*$FreeBSD: head/sys/dev/ixgbe/if_ixv.c 331224 2018-03-19 20:55:05Z erj $*/
     36 
     37 #ifdef _KERNEL_OPT
     38 #include "opt_inet.h"
     39 #include "opt_inet6.h"
     40 #include "opt_net_mpsafe.h"
     41 #endif
     42 
     43 #include "ixgbe.h"
     44 #include "vlan.h"
     45 
     46 /************************************************************************
     47  * Driver version
     48  ************************************************************************/
     49 static const char ixv_driver_version[] = "2.0.1-k";
     50 /* XXX NetBSD: + 1.5.17 */
     51 
     52 /************************************************************************
     53  * PCI Device ID Table
     54  *
     55  *   Used by probe to select devices to load on
     56  *   Last field stores an index into ixv_strings
     57  *   Last entry must be all 0s
     58  *
     59  *   { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
     60  ************************************************************************/
     61 static const ixgbe_vendor_info_t ixv_vendor_info_array[] =
     62 {
     63 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_VF, 0, 0, 0},
     64 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540_VF, 0, 0, 0},
     65 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550_VF, 0, 0, 0},
     66 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_VF, 0, 0, 0},
     67 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_VF, 0, 0, 0},
     68 	/* required last entry */
     69 	{0, 0, 0, 0, 0}
     70 };
     71 
     72 /************************************************************************
     73  * Table of branding strings
     74  ************************************************************************/
     75 static const char *ixv_strings[] = {
     76 	"Intel(R) PRO/10GbE Virtual Function Network Driver"
     77 };
     78 
     79 /*********************************************************************
     80  *  Function prototypes
     81  *********************************************************************/
     82 static int	ixv_probe(device_t, cfdata_t, void *);
     83 static void	ixv_attach(device_t, device_t, void *);
     84 static int	ixv_detach(device_t, int);
     85 #if 0
     86 static int	ixv_shutdown(device_t);
     87 #endif
     88 static int	ixv_ifflags_cb(struct ethercom *);
     89 static int	ixv_ioctl(struct ifnet *, u_long, void *);
     90 static int	ixv_init(struct ifnet *);
     91 static void	ixv_init_locked(struct adapter *);
     92 static void	ixv_ifstop(struct ifnet *, int);
     93 static void	ixv_stop(void *);
     94 static void	ixv_init_device_features(struct adapter *);
     95 static void	ixv_media_status(struct ifnet *, struct ifmediareq *);
     96 static int	ixv_media_change(struct ifnet *);
     97 static int	ixv_allocate_pci_resources(struct adapter *,
     98 		    const struct pci_attach_args *);
     99 static int	ixv_allocate_msix(struct adapter *,
    100 		    const struct pci_attach_args *);
    101 static int	ixv_configure_interrupts(struct adapter *);
    102 static void	ixv_free_pci_resources(struct adapter *);
    103 static void	ixv_local_timer(void *);
    104 static void	ixv_local_timer_locked(void *);
    105 static int	ixv_setup_interface(device_t, struct adapter *);
    106 static int	ixv_negotiate_api(struct adapter *);
    107 
    108 static void	ixv_initialize_transmit_units(struct adapter *);
    109 static void	ixv_initialize_receive_units(struct adapter *);
    110 static void	ixv_initialize_rss_mapping(struct adapter *);
    111 static s32	ixv_check_link(struct adapter *);
    112 
    113 static void	ixv_enable_intr(struct adapter *);
    114 static void	ixv_disable_intr(struct adapter *);
    115 static void	ixv_set_multi(struct adapter *);
    116 static void	ixv_update_link_status(struct adapter *);
    117 static int	ixv_sysctl_debug(SYSCTLFN_PROTO);
    118 static void	ixv_set_ivar(struct adapter *, u8, u8, s8);
    119 static void	ixv_configure_ivars(struct adapter *);
    120 static u8 *	ixv_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
    121 static void	ixv_eitr_write(struct adapter *, uint32_t, uint32_t);
    122 
    123 static void	ixv_setup_vlan_tagging(struct adapter *);
    124 static int	ixv_setup_vlan_support(struct adapter *);
    125 static int	ixv_vlan_cb(struct ethercom *, uint16_t, bool);
    126 static int	ixv_register_vlan(struct adapter *, u16);
    127 static int	ixv_unregister_vlan(struct adapter *, u16);
    128 
    129 static void	ixv_add_device_sysctls(struct adapter *);
    130 static void	ixv_save_stats(struct adapter *);
    131 static void	ixv_init_stats(struct adapter *);
    132 static void	ixv_update_stats(struct adapter *);
    133 static void	ixv_add_stats_sysctls(struct adapter *);
    134 static void	ixv_clear_evcnt(struct adapter *);
    135 
    136 /* Sysctl handlers */
    137 static void	ixv_set_sysctl_value(struct adapter *, const char *,
    138 		    const char *, int *, int);
    139 static int	ixv_sysctl_interrupt_rate_handler(SYSCTLFN_PROTO);
    140 static int	ixv_sysctl_next_to_check_handler(SYSCTLFN_PROTO);
    141 static int	ixv_sysctl_rdh_handler(SYSCTLFN_PROTO);
    142 static int	ixv_sysctl_rdt_handler(SYSCTLFN_PROTO);
    143 static int	ixv_sysctl_tdt_handler(SYSCTLFN_PROTO);
    144 static int	ixv_sysctl_tdh_handler(SYSCTLFN_PROTO);
    145 
    146 /* The MSI-X Interrupt handlers */
    147 static int	ixv_msix_que(void *);
    148 static int	ixv_msix_mbx(void *);
    149 
    150 /* Deferred interrupt tasklets */
    151 static void	ixv_handle_que(void *);
    152 static void	ixv_handle_link(void *);
    153 
    154 /* Workqueue handler for deferred work */
    155 static void	ixv_handle_que_work(struct work *, void *);
    156 
    157 const struct sysctlnode *ixv_sysctl_instance(struct adapter *);
    158 static const ixgbe_vendor_info_t *ixv_lookup(const struct pci_attach_args *);
    159 
    160 /************************************************************************
    161  * FreeBSD Device Interface Entry Points
    162  ************************************************************************/
    163 CFATTACH_DECL3_NEW(ixv, sizeof(struct adapter),
    164     ixv_probe, ixv_attach, ixv_detach, NULL, NULL, NULL,
    165     DVF_DETACH_SHUTDOWN);
    166 
    167 #if 0
    168 static driver_t ixv_driver = {
    169 	"ixv", ixv_methods, sizeof(struct adapter),
    170 };
    171 
    172 devclass_t ixv_devclass;
    173 DRIVER_MODULE(ixv, pci, ixv_driver, ixv_devclass, 0, 0);
    174 MODULE_DEPEND(ixv, pci, 1, 1, 1);
    175 MODULE_DEPEND(ixv, ether, 1, 1, 1);
    176 #endif
    177 
    178 /*
    179  * TUNEABLE PARAMETERS:
    180  */
    181 
    182 /* Number of Queues - do not exceed MSI-X vectors - 1 */
    183 static int ixv_num_queues = 0;
    184 #define	TUNABLE_INT(__x, __y)
    185 TUNABLE_INT("hw.ixv.num_queues", &ixv_num_queues);
    186 
    187 /*
    188  * AIM: Adaptive Interrupt Moderation
    189  * which means that the interrupt rate
    190  * is varied over time based on the
    191  * traffic for that interrupt vector
    192  */
    193 static bool ixv_enable_aim = false;
    194 TUNABLE_INT("hw.ixv.enable_aim", &ixv_enable_aim);
    195 
    196 static int ixv_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
    197 TUNABLE_INT("hw.ixv.max_interrupt_rate", &ixv_max_interrupt_rate);
    198 
    199 /* How many packets rxeof tries to clean at a time */
    200 static int ixv_rx_process_limit = 256;
    201 TUNABLE_INT("hw.ixv.rx_process_limit", &ixv_rx_process_limit);
    202 
    203 /* How many packets txeof tries to clean at a time */
    204 static int ixv_tx_process_limit = 256;
    205 TUNABLE_INT("hw.ixv.tx_process_limit", &ixv_tx_process_limit);
    206 
    207 /* Which packet processing uses workqueue or softint */
    208 static bool ixv_txrx_workqueue = false;
    209 
    210 /*
    211  * Number of TX descriptors per ring,
    212  * setting higher than RX as this seems
    213  * the better performing choice.
    214  */
    215 static int ixv_txd = PERFORM_TXD;
    216 TUNABLE_INT("hw.ixv.txd", &ixv_txd);
    217 
    218 /* Number of RX descriptors per ring */
    219 static int ixv_rxd = PERFORM_RXD;
    220 TUNABLE_INT("hw.ixv.rxd", &ixv_rxd);
    221 
    222 /* Legacy Transmit (single queue) */
    223 static int ixv_enable_legacy_tx = 0;
    224 TUNABLE_INT("hw.ixv.enable_legacy_tx", &ixv_enable_legacy_tx);
    225 
    226 #ifdef NET_MPSAFE
    227 #define IXGBE_MPSAFE		1
    228 #define IXGBE_CALLOUT_FLAGS	CALLOUT_MPSAFE
    229 #define IXGBE_SOFTINFT_FLAGS	SOFTINT_MPSAFE
    230 #define IXGBE_WORKQUEUE_FLAGS	WQ_PERCPU | WQ_MPSAFE
    231 #else
    232 #define IXGBE_CALLOUT_FLAGS	0
    233 #define IXGBE_SOFTINFT_FLAGS	0
    234 #define IXGBE_WORKQUEUE_FLAGS	WQ_PERCPU
    235 #endif
    236 #define IXGBE_WORKQUEUE_PRI PRI_SOFTNET
    237 
    238 #if 0
    239 static int (*ixv_start_locked)(struct ifnet *, struct tx_ring *);
    240 static int (*ixv_ring_empty)(struct ifnet *, struct buf_ring *);
    241 #endif
    242 
    243 /************************************************************************
    244  * ixv_probe - Device identification routine
    245  *
    246  *   Determines if the driver should be loaded on
    247  *   adapter based on its PCI vendor/device ID.
    248  *
    249  *   return BUS_PROBE_DEFAULT on success, positive on failure
    250  ************************************************************************/
    251 static int
    252 ixv_probe(device_t dev, cfdata_t cf, void *aux)
    253 {
    254 #ifdef __HAVE_PCI_MSI_MSIX
    255 	const struct pci_attach_args *pa = aux;
    256 
    257 	return (ixv_lookup(pa) != NULL) ? 1 : 0;
    258 #else
    259 	return 0;
    260 #endif
    261 } /* ixv_probe */
    262 
    263 static const ixgbe_vendor_info_t *
    264 ixv_lookup(const struct pci_attach_args *pa)
    265 {
    266 	const ixgbe_vendor_info_t *ent;
    267 	pcireg_t subid;
    268 
    269 	INIT_DEBUGOUT("ixv_lookup: begin");
    270 
    271 	if (PCI_VENDOR(pa->pa_id) != IXGBE_INTEL_VENDOR_ID)
    272 		return NULL;
    273 
    274 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    275 
    276 	for (ent = ixv_vendor_info_array; ent->vendor_id != 0; ent++) {
    277 		if ((PCI_VENDOR(pa->pa_id) == ent->vendor_id) &&
    278 		    (PCI_PRODUCT(pa->pa_id) == ent->device_id) &&
    279 		    ((PCI_SUBSYS_VENDOR(subid) == ent->subvendor_id) ||
    280 		     (ent->subvendor_id == 0)) &&
    281 		    ((PCI_SUBSYS_ID(subid) == ent->subdevice_id) ||
    282 		     (ent->subdevice_id == 0))) {
    283 			return ent;
    284 		}
    285 	}
    286 
    287 	return NULL;
    288 }
    289 
    290 /************************************************************************
    291  * ixv_attach - Device initialization routine
    292  *
    293  *   Called when the driver is being loaded.
    294  *   Identifies the type of hardware, allocates all resources
    295  *   and initializes the hardware.
    296  *
    297  *   return 0 on success, positive on failure
    298  ************************************************************************/
    299 static void
    300 ixv_attach(device_t parent, device_t dev, void *aux)
    301 {
    302 	struct adapter *adapter;
    303 	struct ixgbe_hw *hw;
    304 	int		error = 0;
    305 	pcireg_t	id, subid;
    306 	const ixgbe_vendor_info_t *ent;
    307 	const struct pci_attach_args *pa = aux;
    308 	const char *apivstr;
    309 	const char *str;
    310 	char buf[256];
    311 
    312 	INIT_DEBUGOUT("ixv_attach: begin");
    313 
    314 	/*
    315 	 * Make sure BUSMASTER is set, on a VM under
    316 	 * KVM it may not be and will break things.
    317 	 */
    318 	ixgbe_pci_enable_busmaster(pa->pa_pc, pa->pa_tag);
    319 
    320 	/* Allocate, clear, and link in our adapter structure */
    321 	adapter = device_private(dev);
    322 	adapter->dev = dev;
    323 	adapter->hw.back = adapter;
    324 	hw = &adapter->hw;
    325 
    326 	adapter->init_locked = ixv_init_locked;
    327 	adapter->stop_locked = ixv_stop;
    328 
    329 	adapter->osdep.pc = pa->pa_pc;
    330 	adapter->osdep.tag = pa->pa_tag;
    331 	if (pci_dma64_available(pa))
    332 		adapter->osdep.dmat = pa->pa_dmat64;
    333 	else
    334 		adapter->osdep.dmat = pa->pa_dmat;
    335 	adapter->osdep.attached = false;
    336 
    337 	ent = ixv_lookup(pa);
    338 
    339 	KASSERT(ent != NULL);
    340 
    341 	aprint_normal(": %s, Version - %s\n",
    342 	    ixv_strings[ent->index], ixv_driver_version);
    343 
    344 	/* Core Lock Init*/
    345 	IXGBE_CORE_LOCK_INIT(adapter, device_xname(dev));
    346 
    347 	/* Do base PCI setup - map BAR0 */
    348 	if (ixv_allocate_pci_resources(adapter, pa)) {
    349 		aprint_error_dev(dev, "ixv_allocate_pci_resources() failed!\n");
    350 		error = ENXIO;
    351 		goto err_out;
    352 	}
    353 
    354 	/* SYSCTL APIs */
    355 	ixv_add_device_sysctls(adapter);
    356 
    357 	/* Set up the timer callout */
    358 	callout_init(&adapter->timer, IXGBE_CALLOUT_FLAGS);
    359 
    360 	/* Save off the information about this board */
    361 	id = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG);
    362 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    363 	hw->vendor_id = PCI_VENDOR(id);
    364 	hw->device_id = PCI_PRODUCT(id);
    365 	hw->revision_id =
    366 	    PCI_REVISION(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
    367 	hw->subsystem_vendor_id = PCI_SUBSYS_VENDOR(subid);
    368 	hw->subsystem_device_id = PCI_SUBSYS_ID(subid);
    369 
    370 	/* A subset of set_mac_type */
    371 	switch (hw->device_id) {
    372 	case IXGBE_DEV_ID_82599_VF:
    373 		hw->mac.type = ixgbe_mac_82599_vf;
    374 		str = "82599 VF";
    375 		break;
    376 	case IXGBE_DEV_ID_X540_VF:
    377 		hw->mac.type = ixgbe_mac_X540_vf;
    378 		str = "X540 VF";
    379 		break;
    380 	case IXGBE_DEV_ID_X550_VF:
    381 		hw->mac.type = ixgbe_mac_X550_vf;
    382 		str = "X550 VF";
    383 		break;
    384 	case IXGBE_DEV_ID_X550EM_X_VF:
    385 		hw->mac.type = ixgbe_mac_X550EM_x_vf;
    386 		str = "X550EM X VF";
    387 		break;
    388 	case IXGBE_DEV_ID_X550EM_A_VF:
    389 		hw->mac.type = ixgbe_mac_X550EM_a_vf;
    390 		str = "X550EM A VF";
    391 		break;
    392 	default:
    393 		/* Shouldn't get here since probe succeeded */
    394 		aprint_error_dev(dev, "Unknown device ID!\n");
    395 		error = ENXIO;
    396 		goto err_out;
    397 		break;
    398 	}
    399 	aprint_normal_dev(dev, "device %s\n", str);
    400 
    401 	ixv_init_device_features(adapter);
    402 
    403 	/* Initialize the shared code */
    404 	error = ixgbe_init_ops_vf(hw);
    405 	if (error) {
    406 		aprint_error_dev(dev, "ixgbe_init_ops_vf() failed!\n");
    407 		error = EIO;
    408 		goto err_out;
    409 	}
    410 
    411 	/* Setup the mailbox */
    412 	ixgbe_init_mbx_params_vf(hw);
    413 
    414 	/* Set the right number of segments */
    415 	adapter->num_segs = IXGBE_82599_SCATTER;
    416 
    417 	/* Reset mbox api to 1.0 */
    418 	error = hw->mac.ops.reset_hw(hw);
    419 	if (error == IXGBE_ERR_RESET_FAILED)
    420 		aprint_error_dev(dev, "...reset_hw() failure: Reset Failed!\n");
    421 	else if (error)
    422 		aprint_error_dev(dev, "...reset_hw() failed with error %d\n",
    423 		    error);
    424 	if (error) {
    425 		error = EIO;
    426 		goto err_out;
    427 	}
    428 
    429 	error = hw->mac.ops.init_hw(hw);
    430 	if (error) {
    431 		aprint_error_dev(dev, "...init_hw() failed!\n");
    432 		error = EIO;
    433 		goto err_out;
    434 	}
    435 
    436 	/* Negotiate mailbox API version */
    437 	error = ixv_negotiate_api(adapter);
    438 	if (error)
    439 		aprint_normal_dev(dev,
    440 		    "MBX API negotiation failed during attach!\n");
    441 	switch (hw->api_version) {
    442 	case ixgbe_mbox_api_10:
    443 		apivstr = "1.0";
    444 		break;
    445 	case ixgbe_mbox_api_20:
    446 		apivstr = "2.0";
    447 		break;
    448 	case ixgbe_mbox_api_11:
    449 		apivstr = "1.1";
    450 		break;
    451 	case ixgbe_mbox_api_12:
    452 		apivstr = "1.2";
    453 		break;
    454 	case ixgbe_mbox_api_13:
    455 		apivstr = "1.3";
    456 		break;
    457 	default:
    458 		apivstr = "unknown";
    459 		break;
    460 	}
    461 	aprint_normal_dev(dev, "Mailbox API %s\n", apivstr);
    462 
    463 	/* If no mac address was assigned, make a random one */
    464 	if (!ixv_check_ether_addr(hw->mac.addr)) {
    465 		u8 addr[ETHER_ADDR_LEN];
    466 		uint64_t rndval = cprng_strong64();
    467 
    468 		memcpy(addr, &rndval, sizeof(addr));
    469 		addr[0] &= 0xFE;
    470 		addr[0] |= 0x02;
    471 		bcopy(addr, hw->mac.addr, sizeof(addr));
    472 	}
    473 
    474 	/* Register for VLAN events */
    475 	ether_set_vlan_cb(&adapter->osdep.ec, ixv_vlan_cb);
    476 
    477 	/* Sysctls for limiting the amount of work done in the taskqueues */
    478 	ixv_set_sysctl_value(adapter, "rx_processing_limit",
    479 	    "max number of rx packets to process",
    480 	    &adapter->rx_process_limit, ixv_rx_process_limit);
    481 
    482 	ixv_set_sysctl_value(adapter, "tx_processing_limit",
    483 	    "max number of tx packets to process",
    484 	    &adapter->tx_process_limit, ixv_tx_process_limit);
    485 
    486 	/* Do descriptor calc and sanity checks */
    487 	if (((ixv_txd * sizeof(union ixgbe_adv_tx_desc)) % DBA_ALIGN) != 0 ||
    488 	    ixv_txd < MIN_TXD || ixv_txd > MAX_TXD) {
    489 		aprint_error_dev(dev, "TXD config issue, using default!\n");
    490 		adapter->num_tx_desc = DEFAULT_TXD;
    491 	} else
    492 		adapter->num_tx_desc = ixv_txd;
    493 
    494 	if (((ixv_rxd * sizeof(union ixgbe_adv_rx_desc)) % DBA_ALIGN) != 0 ||
    495 	    ixv_rxd < MIN_RXD || ixv_rxd > MAX_RXD) {
    496 		aprint_error_dev(dev, "RXD config issue, using default!\n");
    497 		adapter->num_rx_desc = DEFAULT_RXD;
    498 	} else
    499 		adapter->num_rx_desc = ixv_rxd;
    500 
    501 	/* Setup MSI-X */
    502 	error = ixv_configure_interrupts(adapter);
    503 	if (error)
    504 		goto err_out;
    505 
    506 	/* Allocate our TX/RX Queues */
    507 	if (ixgbe_allocate_queues(adapter)) {
    508 		aprint_error_dev(dev, "ixgbe_allocate_queues() failed!\n");
    509 		error = ENOMEM;
    510 		goto err_out;
    511 	}
    512 
    513 	/* hw.ix defaults init */
    514 	adapter->enable_aim = ixv_enable_aim;
    515 
    516 	adapter->txrx_use_workqueue = ixv_txrx_workqueue;
    517 
    518 	error = ixv_allocate_msix(adapter, pa);
    519 	if (error) {
    520 		aprint_error_dev(dev, "ixv_allocate_msix() failed!\n");
    521 		goto err_late;
    522 	}
    523 
    524 	/* Setup OS specific network interface */
    525 	error = ixv_setup_interface(dev, adapter);
    526 	if (error != 0) {
    527 		aprint_error_dev(dev, "ixv_setup_interface() failed!\n");
    528 		goto err_late;
    529 	}
    530 
    531 	/* Do the stats setup */
    532 	ixv_save_stats(adapter);
    533 	ixv_init_stats(adapter);
    534 	ixv_add_stats_sysctls(adapter);
    535 
    536 	if (adapter->feat_en & IXGBE_FEATURE_NETMAP)
    537 		ixgbe_netmap_attach(adapter);
    538 
    539 	snprintb(buf, sizeof(buf), IXGBE_FEATURE_FLAGS, adapter->feat_cap);
    540 	aprint_verbose_dev(dev, "feature cap %s\n", buf);
    541 	snprintb(buf, sizeof(buf), IXGBE_FEATURE_FLAGS, adapter->feat_en);
    542 	aprint_verbose_dev(dev, "feature ena %s\n", buf);
    543 
    544 	INIT_DEBUGOUT("ixv_attach: end");
    545 	adapter->osdep.attached = true;
    546 
    547 	return;
    548 
    549 err_late:
    550 	ixgbe_free_transmit_structures(adapter);
    551 	ixgbe_free_receive_structures(adapter);
    552 	free(adapter->queues, M_DEVBUF);
    553 err_out:
    554 	ixv_free_pci_resources(adapter);
    555 	IXGBE_CORE_LOCK_DESTROY(adapter);
    556 
    557 	return;
    558 } /* ixv_attach */
    559 
    560 /************************************************************************
    561  * ixv_detach - Device removal routine
    562  *
    563  *   Called when the driver is being removed.
    564  *   Stops the adapter and deallocates all the resources
    565  *   that were allocated for driver operation.
    566  *
    567  *   return 0 on success, positive on failure
    568  ************************************************************************/
    569 static int
    570 ixv_detach(device_t dev, int flags)
    571 {
    572 	struct adapter	*adapter = device_private(dev);
    573 	struct ixgbe_hw *hw = &adapter->hw;
    574 	struct ix_queue *que = adapter->queues;
    575 	struct tx_ring *txr = adapter->tx_rings;
    576 	struct rx_ring *rxr = adapter->rx_rings;
    577 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
    578 
    579 	INIT_DEBUGOUT("ixv_detach: begin");
    580 	if (adapter->osdep.attached == false)
    581 		return 0;
    582 
    583 	/* Stop the interface. Callouts are stopped in it. */
    584 	ixv_ifstop(adapter->ifp, 1);
    585 
    586 #if NVLAN > 0
    587 	/* Make sure VLANs are not using driver */
    588 	if (!VLAN_ATTACHED(&adapter->osdep.ec))
    589 		;	/* nothing to do: no VLANs */
    590 	else if ((flags & (DETACH_SHUTDOWN | DETACH_FORCE)) != 0)
    591 		vlan_ifdetach(adapter->ifp);
    592 	else {
    593 		aprint_error_dev(dev, "VLANs in use, detach first\n");
    594 		return EBUSY;
    595 	}
    596 #endif
    597 
    598 	for (int i = 0; i < adapter->num_queues; i++, que++, txr++) {
    599 		if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX))
    600 			softint_disestablish(txr->txr_si);
    601 		softint_disestablish(que->que_si);
    602 	}
    603 	if (adapter->txr_wq != NULL)
    604 		workqueue_destroy(adapter->txr_wq);
    605 	if (adapter->txr_wq_enqueued != NULL)
    606 		percpu_free(adapter->txr_wq_enqueued, sizeof(u_int));
    607 	if (adapter->que_wq != NULL)
    608 		workqueue_destroy(adapter->que_wq);
    609 
    610 	/* Drain the Mailbox(link) queue */
    611 	softint_disestablish(adapter->link_si);
    612 
    613 	ether_ifdetach(adapter->ifp);
    614 	callout_halt(&adapter->timer, NULL);
    615 
    616 	if (adapter->feat_en & IXGBE_FEATURE_NETMAP)
    617 		netmap_detach(adapter->ifp);
    618 
    619 	ixv_free_pci_resources(adapter);
    620 #if 0 /* XXX the NetBSD port is probably missing something here */
    621 	bus_generic_detach(dev);
    622 #endif
    623 	if_detach(adapter->ifp);
    624 	if_percpuq_destroy(adapter->ipq);
    625 
    626 	sysctl_teardown(&adapter->sysctllog);
    627 	evcnt_detach(&adapter->efbig_tx_dma_setup);
    628 	evcnt_detach(&adapter->mbuf_defrag_failed);
    629 	evcnt_detach(&adapter->efbig2_tx_dma_setup);
    630 	evcnt_detach(&adapter->einval_tx_dma_setup);
    631 	evcnt_detach(&adapter->other_tx_dma_setup);
    632 	evcnt_detach(&adapter->eagain_tx_dma_setup);
    633 	evcnt_detach(&adapter->enomem_tx_dma_setup);
    634 	evcnt_detach(&adapter->watchdog_events);
    635 	evcnt_detach(&adapter->tso_err);
    636 	evcnt_detach(&adapter->link_irq);
    637 
    638 	txr = adapter->tx_rings;
    639 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
    640 		evcnt_detach(&adapter->queues[i].irqs);
    641 		evcnt_detach(&adapter->queues[i].handleq);
    642 		evcnt_detach(&adapter->queues[i].req);
    643 		evcnt_detach(&txr->no_desc_avail);
    644 		evcnt_detach(&txr->total_packets);
    645 		evcnt_detach(&txr->tso_tx);
    646 #ifndef IXGBE_LEGACY_TX
    647 		evcnt_detach(&txr->pcq_drops);
    648 #endif
    649 
    650 		evcnt_detach(&rxr->rx_packets);
    651 		evcnt_detach(&rxr->rx_bytes);
    652 		evcnt_detach(&rxr->rx_copies);
    653 		evcnt_detach(&rxr->no_jmbuf);
    654 		evcnt_detach(&rxr->rx_discarded);
    655 	}
    656 	evcnt_detach(&stats->ipcs);
    657 	evcnt_detach(&stats->l4cs);
    658 	evcnt_detach(&stats->ipcs_bad);
    659 	evcnt_detach(&stats->l4cs_bad);
    660 
    661 	/* Packet Reception Stats */
    662 	evcnt_detach(&stats->vfgorc);
    663 	evcnt_detach(&stats->vfgprc);
    664 	evcnt_detach(&stats->vfmprc);
    665 
    666 	/* Packet Transmission Stats */
    667 	evcnt_detach(&stats->vfgotc);
    668 	evcnt_detach(&stats->vfgptc);
    669 
    670 	/* Mailbox Stats */
    671 	evcnt_detach(&hw->mbx.stats.msgs_tx);
    672 	evcnt_detach(&hw->mbx.stats.msgs_rx);
    673 	evcnt_detach(&hw->mbx.stats.acks);
    674 	evcnt_detach(&hw->mbx.stats.reqs);
    675 	evcnt_detach(&hw->mbx.stats.rsts);
    676 
    677 	ixgbe_free_transmit_structures(adapter);
    678 	ixgbe_free_receive_structures(adapter);
    679 	for (int i = 0; i < adapter->num_queues; i++) {
    680 		struct ix_queue *lque = &adapter->queues[i];
    681 		mutex_destroy(&lque->dc_mtx);
    682 	}
    683 	free(adapter->queues, M_DEVBUF);
    684 
    685 	IXGBE_CORE_LOCK_DESTROY(adapter);
    686 
    687 	return (0);
    688 } /* ixv_detach */
    689 
    690 /************************************************************************
    691  * ixv_init_locked - Init entry point
    692  *
    693  *   Used in two ways: It is used by the stack as an init entry
    694  *   point in network interface structure. It is also used
    695  *   by the driver as a hw/sw initialization routine to get
    696  *   to a consistent state.
    697  *
    698  *   return 0 on success, positive on failure
    699  ************************************************************************/
    700 static void
    701 ixv_init_locked(struct adapter *adapter)
    702 {
    703 	struct ifnet	*ifp = adapter->ifp;
    704 	device_t	dev = adapter->dev;
    705 	struct ixgbe_hw *hw = &adapter->hw;
    706 	struct ix_queue	*que;
    707 	int		error = 0;
    708 	uint32_t mask;
    709 	int i;
    710 
    711 	INIT_DEBUGOUT("ixv_init_locked: begin");
    712 	KASSERT(mutex_owned(&adapter->core_mtx));
    713 	hw->adapter_stopped = FALSE;
    714 	hw->mac.ops.stop_adapter(hw);
    715 	callout_stop(&adapter->timer);
    716 	for (i = 0, que = adapter->queues; i < adapter->num_queues; i++, que++)
    717 		que->disabled_count = 0;
    718 
    719 	/* reprogram the RAR[0] in case user changed it. */
    720 	hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
    721 
    722 	/* Get the latest mac address, User can use a LAA */
    723 	memcpy(hw->mac.addr, CLLADDR(ifp->if_sadl),
    724 	     IXGBE_ETH_LENGTH_OF_ADDRESS);
    725 	hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, 1);
    726 
    727 	/* Prepare transmit descriptors and buffers */
    728 	if (ixgbe_setup_transmit_structures(adapter)) {
    729 		aprint_error_dev(dev, "Could not setup transmit structures\n");
    730 		ixv_stop(adapter);
    731 		return;
    732 	}
    733 
    734 	/* Reset VF and renegotiate mailbox API version */
    735 	hw->mac.ops.reset_hw(hw);
    736 	hw->mac.ops.start_hw(hw);
    737 	error = ixv_negotiate_api(adapter);
    738 	if (error)
    739 		device_printf(dev,
    740 		    "Mailbox API negotiation failed in init_locked!\n");
    741 
    742 	ixv_initialize_transmit_units(adapter);
    743 
    744 	/* Setup Multicast table */
    745 	ixv_set_multi(adapter);
    746 
    747 	/*
    748 	 * Determine the correct mbuf pool
    749 	 * for doing jumbo/headersplit
    750 	 */
    751 	if (ifp->if_mtu > ETHERMTU)
    752 		adapter->rx_mbuf_sz = MJUMPAGESIZE;
    753 	else
    754 		adapter->rx_mbuf_sz = MCLBYTES;
    755 
    756 	/* Prepare receive descriptors and buffers */
    757 	if (ixgbe_setup_receive_structures(adapter)) {
    758 		device_printf(dev, "Could not setup receive structures\n");
    759 		ixv_stop(adapter);
    760 		return;
    761 	}
    762 
    763 	/* Configure RX settings */
    764 	ixv_initialize_receive_units(adapter);
    765 
    766 #if 0 /* XXX isn't it required? -- msaitoh  */
    767 	/* Set the various hardware offload abilities */
    768 	ifp->if_hwassist = 0;
    769 	if (ifp->if_capenable & IFCAP_TSO4)
    770 		ifp->if_hwassist |= CSUM_TSO;
    771 	if (ifp->if_capenable & IFCAP_TXCSUM) {
    772 		ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP);
    773 #if __FreeBSD_version >= 800000
    774 		ifp->if_hwassist |= CSUM_SCTP;
    775 #endif
    776 	}
    777 #endif
    778 
    779 	/* Set up VLAN offload and filter */
    780 	ixv_setup_vlan_support(adapter);
    781 
    782 	/* Set up MSI-X routing */
    783 	ixv_configure_ivars(adapter);
    784 
    785 	/* Set up auto-mask */
    786 	mask = (1 << adapter->vector);
    787 	for (i = 0, que = adapter->queues; i < adapter->num_queues; i++, que++)
    788 		mask |= (1 << que->msix);
    789 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, mask);
    790 
    791 	/* Set moderation on the Link interrupt */
    792 	ixv_eitr_write(adapter, adapter->vector, IXGBE_LINK_ITR);
    793 
    794 	/* Stats init */
    795 	ixv_init_stats(adapter);
    796 
    797 	/* Config/Enable Link */
    798 	hw->mac.get_link_status = TRUE;
    799 	hw->mac.ops.check_link(hw, &adapter->link_speed, &adapter->link_up,
    800 	    FALSE);
    801 
    802 	/* Start watchdog */
    803 	callout_reset(&adapter->timer, hz, ixv_local_timer, adapter);
    804 
    805 	/* And now turn on interrupts */
    806 	ixv_enable_intr(adapter);
    807 
    808 	/* Update saved flags. See ixgbe_ifflags_cb() */
    809 	adapter->if_flags = ifp->if_flags;
    810 	adapter->ec_capenable = adapter->osdep.ec.ec_capenable;
    811 
    812 	/* Now inform the stack we're ready */
    813 	ifp->if_flags |= IFF_RUNNING;
    814 	ifp->if_flags &= ~IFF_OACTIVE;
    815 
    816 	return;
    817 } /* ixv_init_locked */
    818 
    819 /************************************************************************
    820  * ixv_enable_queue
    821  ************************************************************************/
    822 static inline void
    823 ixv_enable_queue(struct adapter *adapter, u32 vector)
    824 {
    825 	struct ixgbe_hw *hw = &adapter->hw;
    826 	struct ix_queue *que = &adapter->queues[vector];
    827 	u32		queue = 1UL << vector;
    828 	u32		mask;
    829 
    830 	mutex_enter(&que->dc_mtx);
    831 	if (que->disabled_count > 0 && --que->disabled_count > 0)
    832 		goto out;
    833 
    834 	mask = (IXGBE_EIMS_RTX_QUEUE & queue);
    835 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
    836 out:
    837 	mutex_exit(&que->dc_mtx);
    838 } /* ixv_enable_queue */
    839 
    840 /************************************************************************
    841  * ixv_disable_queue
    842  ************************************************************************/
    843 static inline void
    844 ixv_disable_queue(struct adapter *adapter, u32 vector)
    845 {
    846 	struct ixgbe_hw *hw = &adapter->hw;
    847 	struct ix_queue *que = &adapter->queues[vector];
    848 	u32		queue = 1UL << vector;
    849 	u32		mask;
    850 
    851 	mutex_enter(&que->dc_mtx);
    852 	if (que->disabled_count++ > 0)
    853 		goto  out;
    854 
    855 	mask = (IXGBE_EIMS_RTX_QUEUE & queue);
    856 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, mask);
    857 out:
    858 	mutex_exit(&que->dc_mtx);
    859 } /* ixv_disable_queue */
    860 
    861 #if 0
    862 static inline void
    863 ixv_rearm_queues(struct adapter *adapter, u64 queues)
    864 {
    865 	u32 mask = (IXGBE_EIMS_RTX_QUEUE & queues);
    866 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEICS, mask);
    867 } /* ixv_rearm_queues */
    868 #endif
    869 
    870 
    871 /************************************************************************
    872  * ixv_msix_que - MSI-X Queue Interrupt Service routine
    873  ************************************************************************/
    874 static int
    875 ixv_msix_que(void *arg)
    876 {
    877 	struct ix_queue	*que = arg;
    878 	struct adapter	*adapter = que->adapter;
    879 	struct tx_ring	*txr = que->txr;
    880 	struct rx_ring	*rxr = que->rxr;
    881 	bool		more;
    882 	u32		newitr = 0;
    883 
    884 	ixv_disable_queue(adapter, que->msix);
    885 	++que->irqs.ev_count;
    886 
    887 #ifdef __NetBSD__
    888 	/* Don't run ixgbe_rxeof in interrupt context */
    889 	more = true;
    890 #else
    891 	more = ixgbe_rxeof(que);
    892 #endif
    893 
    894 	IXGBE_TX_LOCK(txr);
    895 	ixgbe_txeof(txr);
    896 	IXGBE_TX_UNLOCK(txr);
    897 
    898 	/* Do AIM now? */
    899 
    900 	if (adapter->enable_aim == false)
    901 		goto no_calc;
    902 	/*
    903 	 * Do Adaptive Interrupt Moderation:
    904 	 *  - Write out last calculated setting
    905 	 *  - Calculate based on average size over
    906 	 *    the last interval.
    907 	 */
    908 	if (que->eitr_setting)
    909 		ixv_eitr_write(adapter, que->msix, que->eitr_setting);
    910 
    911 	que->eitr_setting = 0;
    912 
    913 	/* Idle, do nothing */
    914 	if ((txr->bytes == 0) && (rxr->bytes == 0))
    915 		goto no_calc;
    916 
    917 	if ((txr->bytes) && (txr->packets))
    918 		newitr = txr->bytes/txr->packets;
    919 	if ((rxr->bytes) && (rxr->packets))
    920 		newitr = uimax(newitr, (rxr->bytes / rxr->packets));
    921 	newitr += 24; /* account for hardware frame, crc */
    922 
    923 	/* set an upper boundary */
    924 	newitr = uimin(newitr, 3000);
    925 
    926 	/* Be nice to the mid range */
    927 	if ((newitr > 300) && (newitr < 1200))
    928 		newitr = (newitr / 3);
    929 	else
    930 		newitr = (newitr / 2);
    931 
    932 	/*
    933 	 * When RSC is used, ITR interval must be larger than RSC_DELAY.
    934 	 * Currently, we use 2us for RSC_DELAY. The minimum value is always
    935 	 * greater than 2us on 100M (and 10M?(not documented)), but it's not
    936 	 * on 1G and higher.
    937 	 */
    938 	if ((adapter->link_speed != IXGBE_LINK_SPEED_100_FULL)
    939 	    && (adapter->link_speed != IXGBE_LINK_SPEED_10_FULL)) {
    940 		if (newitr < IXGBE_MIN_RSC_EITR_10G1G)
    941 			newitr = IXGBE_MIN_RSC_EITR_10G1G;
    942 	}
    943 
    944 	/* save for next interrupt */
    945 	que->eitr_setting = newitr;
    946 
    947 	/* Reset state */
    948 	txr->bytes = 0;
    949 	txr->packets = 0;
    950 	rxr->bytes = 0;
    951 	rxr->packets = 0;
    952 
    953 no_calc:
    954 	if (more)
    955 		softint_schedule(que->que_si);
    956 	else /* Re-enable this interrupt */
    957 		ixv_enable_queue(adapter, que->msix);
    958 
    959 	return 1;
    960 } /* ixv_msix_que */
    961 
    962 /************************************************************************
    963  * ixv_msix_mbx
    964  ************************************************************************/
    965 static int
    966 ixv_msix_mbx(void *arg)
    967 {
    968 	struct adapter	*adapter = arg;
    969 	struct ixgbe_hw *hw = &adapter->hw;
    970 
    971 	++adapter->link_irq.ev_count;
    972 	/* NetBSD: We use auto-clear, so it's not required to write VTEICR */
    973 
    974 	/* Link status change */
    975 	hw->mac.get_link_status = TRUE;
    976 	softint_schedule(adapter->link_si);
    977 
    978 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, (1 << adapter->vector));
    979 
    980 	return 1;
    981 } /* ixv_msix_mbx */
    982 
    983 static void
    984 ixv_eitr_write(struct adapter *adapter, uint32_t index, uint32_t itr)
    985 {
    986 
    987 	/*
    988 	 * Newer devices than 82598 have VF function, so this function is
    989 	 * simple.
    990 	 */
    991 	itr |= IXGBE_EITR_CNT_WDIS;
    992 
    993 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEITR(index), itr);
    994 }
    995 
    996 
    997 /************************************************************************
    998  * ixv_media_status - Media Ioctl callback
    999  *
   1000  *   Called whenever the user queries the status of
   1001  *   the interface using ifconfig.
   1002  ************************************************************************/
   1003 static void
   1004 ixv_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   1005 {
   1006 	struct adapter *adapter = ifp->if_softc;
   1007 
   1008 	INIT_DEBUGOUT("ixv_media_status: begin");
   1009 	IXGBE_CORE_LOCK(adapter);
   1010 	ixv_update_link_status(adapter);
   1011 
   1012 	ifmr->ifm_status = IFM_AVALID;
   1013 	ifmr->ifm_active = IFM_ETHER;
   1014 
   1015 	if (adapter->link_active != LINK_STATE_UP) {
   1016 		ifmr->ifm_active |= IFM_NONE;
   1017 		IXGBE_CORE_UNLOCK(adapter);
   1018 		return;
   1019 	}
   1020 
   1021 	ifmr->ifm_status |= IFM_ACTIVE;
   1022 
   1023 	switch (adapter->link_speed) {
   1024 		case IXGBE_LINK_SPEED_10GB_FULL:
   1025 			ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
   1026 			break;
   1027 		case IXGBE_LINK_SPEED_5GB_FULL:
   1028 			ifmr->ifm_active |= IFM_5000_T | IFM_FDX;
   1029 			break;
   1030 		case IXGBE_LINK_SPEED_2_5GB_FULL:
   1031 			ifmr->ifm_active |= IFM_2500_T | IFM_FDX;
   1032 			break;
   1033 		case IXGBE_LINK_SPEED_1GB_FULL:
   1034 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
   1035 			break;
   1036 		case IXGBE_LINK_SPEED_100_FULL:
   1037 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
   1038 			break;
   1039 		case IXGBE_LINK_SPEED_10_FULL:
   1040 			ifmr->ifm_active |= IFM_10_T | IFM_FDX;
   1041 			break;
   1042 	}
   1043 
   1044 	ifp->if_baudrate = ifmedia_baudrate(ifmr->ifm_active);
   1045 
   1046 	IXGBE_CORE_UNLOCK(adapter);
   1047 } /* ixv_media_status */
   1048 
   1049 /************************************************************************
   1050  * ixv_media_change - Media Ioctl callback
   1051  *
   1052  *   Called when the user changes speed/duplex using
   1053  *   media/mediopt option with ifconfig.
   1054  ************************************************************************/
   1055 static int
   1056 ixv_media_change(struct ifnet *ifp)
   1057 {
   1058 	struct adapter *adapter = ifp->if_softc;
   1059 	struct ifmedia *ifm = &adapter->media;
   1060 
   1061 	INIT_DEBUGOUT("ixv_media_change: begin");
   1062 
   1063 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   1064 		return (EINVAL);
   1065 
   1066 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
   1067 	case IFM_AUTO:
   1068 		break;
   1069 	default:
   1070 		device_printf(adapter->dev, "Only auto media type\n");
   1071 		return (EINVAL);
   1072 	}
   1073 
   1074 	return (0);
   1075 } /* ixv_media_change */
   1076 
   1077 
   1078 /************************************************************************
   1079  * ixv_negotiate_api
   1080  *
   1081  *   Negotiate the Mailbox API with the PF;
   1082  *   start with the most featured API first.
   1083  ************************************************************************/
   1084 static int
   1085 ixv_negotiate_api(struct adapter *adapter)
   1086 {
   1087 	struct ixgbe_hw *hw = &adapter->hw;
   1088 	int		mbx_api[] = { ixgbe_mbox_api_11,
   1089 				      ixgbe_mbox_api_10,
   1090 				      ixgbe_mbox_api_unknown };
   1091 	int		i = 0;
   1092 
   1093 	while (mbx_api[i] != ixgbe_mbox_api_unknown) {
   1094 		if (ixgbevf_negotiate_api_version(hw, mbx_api[i]) == 0)
   1095 			return (0);
   1096 		i++;
   1097 	}
   1098 
   1099 	return (EINVAL);
   1100 } /* ixv_negotiate_api */
   1101 
   1102 
   1103 /************************************************************************
   1104  * ixv_set_multi - Multicast Update
   1105  *
   1106  *   Called whenever multicast address list is updated.
   1107  ************************************************************************/
   1108 static void
   1109 ixv_set_multi(struct adapter *adapter)
   1110 {
   1111 	struct ether_multi *enm;
   1112 	struct ether_multistep step;
   1113 	struct ethercom *ec = &adapter->osdep.ec;
   1114 	u8	mta[MAX_NUM_MULTICAST_ADDRESSES * IXGBE_ETH_LENGTH_OF_ADDRESS];
   1115 	u8		   *update_ptr;
   1116 	int		   mcnt = 0;
   1117 
   1118 	KASSERT(mutex_owned(&adapter->core_mtx));
   1119 	IOCTL_DEBUGOUT("ixv_set_multi: begin");
   1120 
   1121 	ETHER_LOCK(ec);
   1122 	ETHER_FIRST_MULTI(step, ec, enm);
   1123 	while (enm != NULL) {
   1124 		bcopy(enm->enm_addrlo,
   1125 		    &mta[mcnt * IXGBE_ETH_LENGTH_OF_ADDRESS],
   1126 		    IXGBE_ETH_LENGTH_OF_ADDRESS);
   1127 		mcnt++;
   1128 		/* XXX This might be required --msaitoh */
   1129 		if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES)
   1130 			break;
   1131 		ETHER_NEXT_MULTI(step, enm);
   1132 	}
   1133 	ETHER_UNLOCK(ec);
   1134 
   1135 	update_ptr = mta;
   1136 
   1137 	adapter->hw.mac.ops.update_mc_addr_list(&adapter->hw, update_ptr, mcnt,
   1138 	    ixv_mc_array_itr, TRUE);
   1139 } /* ixv_set_multi */
   1140 
   1141 /************************************************************************
   1142  * ixv_mc_array_itr
   1143  *
   1144  *   An iterator function needed by the multicast shared code.
   1145  *   It feeds the shared code routine the addresses in the
   1146  *   array of ixv_set_multi() one by one.
   1147  ************************************************************************/
   1148 static u8 *
   1149 ixv_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
   1150 {
   1151 	u8 *addr = *update_ptr;
   1152 	u8 *newptr;
   1153 
   1154 	*vmdq = 0;
   1155 
   1156 	newptr = addr + IXGBE_ETH_LENGTH_OF_ADDRESS;
   1157 	*update_ptr = newptr;
   1158 
   1159 	return addr;
   1160 } /* ixv_mc_array_itr */
   1161 
   1162 /************************************************************************
   1163  * ixv_local_timer - Timer routine
   1164  *
   1165  *   Checks for link status, updates statistics,
   1166  *   and runs the watchdog check.
   1167  ************************************************************************/
   1168 static void
   1169 ixv_local_timer(void *arg)
   1170 {
   1171 	struct adapter *adapter = arg;
   1172 
   1173 	IXGBE_CORE_LOCK(adapter);
   1174 	ixv_local_timer_locked(adapter);
   1175 	IXGBE_CORE_UNLOCK(adapter);
   1176 }
   1177 
   1178 static void
   1179 ixv_local_timer_locked(void *arg)
   1180 {
   1181 	struct adapter	*adapter = arg;
   1182 	device_t	dev = adapter->dev;
   1183 	struct ix_queue	*que = adapter->queues;
   1184 	u64		queues = 0;
   1185 	u64		v0, v1, v2, v3, v4, v5, v6, v7;
   1186 	int		hung = 0;
   1187 	int		i;
   1188 
   1189 	KASSERT(mutex_owned(&adapter->core_mtx));
   1190 
   1191 	if (ixv_check_link(adapter)) {
   1192 		ixv_init_locked(adapter);
   1193 		return;
   1194 	}
   1195 
   1196 	/* Stats Update */
   1197 	ixv_update_stats(adapter);
   1198 
   1199 	/* Update some event counters */
   1200 	v0 = v1 = v2 = v3 = v4 = v5 = v6 = v7 = 0;
   1201 	que = adapter->queues;
   1202 	for (i = 0; i < adapter->num_queues; i++, que++) {
   1203 		struct tx_ring	*txr = que->txr;
   1204 
   1205 		v0 += txr->q_efbig_tx_dma_setup;
   1206 		v1 += txr->q_mbuf_defrag_failed;
   1207 		v2 += txr->q_efbig2_tx_dma_setup;
   1208 		v3 += txr->q_einval_tx_dma_setup;
   1209 		v4 += txr->q_other_tx_dma_setup;
   1210 		v5 += txr->q_eagain_tx_dma_setup;
   1211 		v6 += txr->q_enomem_tx_dma_setup;
   1212 		v7 += txr->q_tso_err;
   1213 	}
   1214 	adapter->efbig_tx_dma_setup.ev_count = v0;
   1215 	adapter->mbuf_defrag_failed.ev_count = v1;
   1216 	adapter->efbig2_tx_dma_setup.ev_count = v2;
   1217 	adapter->einval_tx_dma_setup.ev_count = v3;
   1218 	adapter->other_tx_dma_setup.ev_count = v4;
   1219 	adapter->eagain_tx_dma_setup.ev_count = v5;
   1220 	adapter->enomem_tx_dma_setup.ev_count = v6;
   1221 	adapter->tso_err.ev_count = v7;
   1222 
   1223 	/*
   1224 	 * Check the TX queues status
   1225 	 *	- mark hung queues so we don't schedule on them
   1226 	 *	- watchdog only if all queues show hung
   1227 	 */
   1228 	que = adapter->queues;
   1229 	for (i = 0; i < adapter->num_queues; i++, que++) {
   1230 		/* Keep track of queues with work for soft irq */
   1231 		if (que->txr->busy)
   1232 			queues |= ((u64)1 << que->me);
   1233 		/*
   1234 		 * Each time txeof runs without cleaning, but there
   1235 		 * are uncleaned descriptors it increments busy. If
   1236 		 * we get to the MAX we declare it hung.
   1237 		 */
   1238 		if (que->busy == IXGBE_QUEUE_HUNG) {
   1239 			++hung;
   1240 			/* Mark the queue as inactive */
   1241 			adapter->active_queues &= ~((u64)1 << que->me);
   1242 			continue;
   1243 		} else {
   1244 			/* Check if we've come back from hung */
   1245 			if ((adapter->active_queues & ((u64)1 << que->me)) == 0)
   1246 				adapter->active_queues |= ((u64)1 << que->me);
   1247 		}
   1248 		if (que->busy >= IXGBE_MAX_TX_BUSY) {
   1249 			device_printf(dev,
   1250 			    "Warning queue %d appears to be hung!\n", i);
   1251 			que->txr->busy = IXGBE_QUEUE_HUNG;
   1252 			++hung;
   1253 		}
   1254 	}
   1255 
   1256 	/* Only truly watchdog if all queues show hung */
   1257 	if (hung == adapter->num_queues)
   1258 		goto watchdog;
   1259 #if 0
   1260 	else if (queues != 0) { /* Force an IRQ on queues with work */
   1261 		ixv_rearm_queues(adapter, queues);
   1262 	}
   1263 #endif
   1264 
   1265 	callout_reset(&adapter->timer, hz, ixv_local_timer, adapter);
   1266 
   1267 	return;
   1268 
   1269 watchdog:
   1270 
   1271 	device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
   1272 	adapter->ifp->if_flags &= ~IFF_RUNNING;
   1273 	adapter->watchdog_events.ev_count++;
   1274 	ixv_init_locked(adapter);
   1275 } /* ixv_local_timer */
   1276 
   1277 /************************************************************************
   1278  * ixv_update_link_status - Update OS on link state
   1279  *
   1280  * Note: Only updates the OS on the cached link state.
   1281  *	 The real check of the hardware only happens with
   1282  *	 a link interrupt.
   1283  ************************************************************************/
   1284 static void
   1285 ixv_update_link_status(struct adapter *adapter)
   1286 {
   1287 	struct ifnet *ifp = adapter->ifp;
   1288 	device_t     dev = adapter->dev;
   1289 
   1290 	KASSERT(mutex_owned(&adapter->core_mtx));
   1291 
   1292 	if (adapter->link_up) {
   1293 		if (adapter->link_active != LINK_STATE_UP) {
   1294 			if (bootverbose) {
   1295 				const char *bpsmsg;
   1296 
   1297 				switch (adapter->link_speed) {
   1298 				case IXGBE_LINK_SPEED_10GB_FULL:
   1299 					bpsmsg = "10 Gbps";
   1300 					break;
   1301 				case IXGBE_LINK_SPEED_5GB_FULL:
   1302 					bpsmsg = "5 Gbps";
   1303 					break;
   1304 				case IXGBE_LINK_SPEED_2_5GB_FULL:
   1305 					bpsmsg = "2.5 Gbps";
   1306 					break;
   1307 				case IXGBE_LINK_SPEED_1GB_FULL:
   1308 					bpsmsg = "1 Gbps";
   1309 					break;
   1310 				case IXGBE_LINK_SPEED_100_FULL:
   1311 					bpsmsg = "100 Mbps";
   1312 					break;
   1313 				case IXGBE_LINK_SPEED_10_FULL:
   1314 					bpsmsg = "10 Mbps";
   1315 					break;
   1316 				default:
   1317 					bpsmsg = "unknown speed";
   1318 					break;
   1319 				}
   1320 				device_printf(dev, "Link is up %s %s \n",
   1321 				    bpsmsg, "Full Duplex");
   1322 			}
   1323 			adapter->link_active = LINK_STATE_UP;
   1324 			if_link_state_change(ifp, LINK_STATE_UP);
   1325 		}
   1326 	} else {
   1327 		/*
   1328 		 * Do it when link active changes to DOWN. i.e.
   1329 		 * a) LINK_STATE_UNKNOWN -> LINK_STATE_DOWN
   1330 		 * b) LINK_STATE_UP	 -> LINK_STATE_DOWN
   1331 		 */
   1332 		if (adapter->link_active != LINK_STATE_DOWN) {
   1333 			if (bootverbose)
   1334 				device_printf(dev, "Link is Down\n");
   1335 			if_link_state_change(ifp, LINK_STATE_DOWN);
   1336 			adapter->link_active = LINK_STATE_DOWN;
   1337 		}
   1338 	}
   1339 } /* ixv_update_link_status */
   1340 
   1341 
   1342 /************************************************************************
   1343  * ixv_stop - Stop the hardware
   1344  *
   1345  *   Disables all traffic on the adapter by issuing a
   1346  *   global reset on the MAC and deallocates TX/RX buffers.
   1347  ************************************************************************/
   1348 static void
   1349 ixv_ifstop(struct ifnet *ifp, int disable)
   1350 {
   1351 	struct adapter *adapter = ifp->if_softc;
   1352 
   1353 	IXGBE_CORE_LOCK(adapter);
   1354 	ixv_stop(adapter);
   1355 	IXGBE_CORE_UNLOCK(adapter);
   1356 }
   1357 
   1358 static void
   1359 ixv_stop(void *arg)
   1360 {
   1361 	struct ifnet	*ifp;
   1362 	struct adapter	*adapter = arg;
   1363 	struct ixgbe_hw *hw = &adapter->hw;
   1364 
   1365 	ifp = adapter->ifp;
   1366 
   1367 	KASSERT(mutex_owned(&adapter->core_mtx));
   1368 
   1369 	INIT_DEBUGOUT("ixv_stop: begin\n");
   1370 	ixv_disable_intr(adapter);
   1371 
   1372 	/* Tell the stack that the interface is no longer active */
   1373 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1374 
   1375 	hw->mac.ops.reset_hw(hw);
   1376 	adapter->hw.adapter_stopped = FALSE;
   1377 	hw->mac.ops.stop_adapter(hw);
   1378 	callout_stop(&adapter->timer);
   1379 
   1380 	/* reprogram the RAR[0] in case user changed it. */
   1381 	hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
   1382 
   1383 	return;
   1384 } /* ixv_stop */
   1385 
   1386 
   1387 /************************************************************************
   1388  * ixv_allocate_pci_resources
   1389  ************************************************************************/
   1390 static int
   1391 ixv_allocate_pci_resources(struct adapter *adapter,
   1392     const struct pci_attach_args *pa)
   1393 {
   1394 	pcireg_t	memtype, csr;
   1395 	device_t	dev = adapter->dev;
   1396 	bus_addr_t addr;
   1397 	int flags;
   1398 
   1399 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
   1400 	switch (memtype) {
   1401 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   1402 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   1403 		adapter->osdep.mem_bus_space_tag = pa->pa_memt;
   1404 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
   1405 		      memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
   1406 			goto map_err;
   1407 		if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
   1408 			aprint_normal_dev(dev, "clearing prefetchable bit\n");
   1409 			flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
   1410 		}
   1411 		if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
   1412 		     adapter->osdep.mem_size, flags,
   1413 		     &adapter->osdep.mem_bus_space_handle) != 0) {
   1414 map_err:
   1415 			adapter->osdep.mem_size = 0;
   1416 			aprint_error_dev(dev, "unable to map BAR0\n");
   1417 			return ENXIO;
   1418 		}
   1419 		/*
   1420 		 * Enable address decoding for memory range in case it's not
   1421 		 * set.
   1422 		 */
   1423 		csr = pci_conf_read(pa->pa_pc, pa->pa_tag,
   1424 		    PCI_COMMAND_STATUS_REG);
   1425 		csr |= PCI_COMMAND_MEM_ENABLE;
   1426 		pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
   1427 		    csr);
   1428 		break;
   1429 	default:
   1430 		aprint_error_dev(dev, "unexpected type on BAR0\n");
   1431 		return ENXIO;
   1432 	}
   1433 
   1434 	/* Pick up the tuneable queues */
   1435 	adapter->num_queues = ixv_num_queues;
   1436 
   1437 	return (0);
   1438 } /* ixv_allocate_pci_resources */
   1439 
   1440 /************************************************************************
   1441  * ixv_free_pci_resources
   1442  ************************************************************************/
   1443 static void
   1444 ixv_free_pci_resources(struct adapter * adapter)
   1445 {
   1446 	struct		ix_queue *que = adapter->queues;
   1447 	int		rid;
   1448 
   1449 	/*
   1450 	 *  Release all msix queue resources:
   1451 	 */
   1452 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   1453 		if (que->res != NULL)
   1454 			pci_intr_disestablish(adapter->osdep.pc,
   1455 			    adapter->osdep.ihs[i]);
   1456 	}
   1457 
   1458 
   1459 	/* Clean the Mailbox interrupt last */
   1460 	rid = adapter->vector;
   1461 
   1462 	if (adapter->osdep.ihs[rid] != NULL) {
   1463 		pci_intr_disestablish(adapter->osdep.pc,
   1464 		    adapter->osdep.ihs[rid]);
   1465 		adapter->osdep.ihs[rid] = NULL;
   1466 	}
   1467 
   1468 	pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
   1469 	    adapter->osdep.nintrs);
   1470 
   1471 	if (adapter->osdep.mem_size != 0) {
   1472 		bus_space_unmap(adapter->osdep.mem_bus_space_tag,
   1473 		    adapter->osdep.mem_bus_space_handle,
   1474 		    adapter->osdep.mem_size);
   1475 	}
   1476 
   1477 	return;
   1478 } /* ixv_free_pci_resources */
   1479 
   1480 /************************************************************************
   1481  * ixv_setup_interface
   1482  *
   1483  *   Setup networking device structure and register an interface.
   1484  ************************************************************************/
   1485 static int
   1486 ixv_setup_interface(device_t dev, struct adapter *adapter)
   1487 {
   1488 	struct ethercom *ec = &adapter->osdep.ec;
   1489 	struct ifnet   *ifp;
   1490 	int rv;
   1491 
   1492 	INIT_DEBUGOUT("ixv_setup_interface: begin");
   1493 
   1494 	ifp = adapter->ifp = &ec->ec_if;
   1495 	strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
   1496 	ifp->if_baudrate = IF_Gbps(10);
   1497 	ifp->if_init = ixv_init;
   1498 	ifp->if_stop = ixv_ifstop;
   1499 	ifp->if_softc = adapter;
   1500 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1501 #ifdef IXGBE_MPSAFE
   1502 	ifp->if_extflags = IFEF_MPSAFE;
   1503 #endif
   1504 	ifp->if_ioctl = ixv_ioctl;
   1505 	if (adapter->feat_en & IXGBE_FEATURE_LEGACY_TX) {
   1506 #if 0
   1507 		ixv_start_locked = ixgbe_legacy_start_locked;
   1508 #endif
   1509 	} else {
   1510 		ifp->if_transmit = ixgbe_mq_start;
   1511 #if 0
   1512 		ixv_start_locked = ixgbe_mq_start_locked;
   1513 #endif
   1514 	}
   1515 	ifp->if_start = ixgbe_legacy_start;
   1516 	IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
   1517 	IFQ_SET_READY(&ifp->if_snd);
   1518 
   1519 	rv = if_initialize(ifp);
   1520 	if (rv != 0) {
   1521 		aprint_error_dev(dev, "if_initialize failed(%d)\n", rv);
   1522 		return rv;
   1523 	}
   1524 	adapter->ipq = if_percpuq_create(&adapter->osdep.ec.ec_if);
   1525 	ether_ifattach(ifp, adapter->hw.mac.addr);
   1526 	/*
   1527 	 * We use per TX queue softint, so if_deferred_start_init() isn't
   1528 	 * used.
   1529 	 */
   1530 	ether_set_ifflags_cb(ec, ixv_ifflags_cb);
   1531 
   1532 	adapter->max_frame_size = ifp->if_mtu + IXGBE_MTU_HDR;
   1533 
   1534 	/*
   1535 	 * Tell the upper layer(s) we support long frames.
   1536 	 */
   1537 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
   1538 
   1539 	/* Set capability flags */
   1540 	ifp->if_capabilities |= IFCAP_HWCSUM
   1541 			     |	IFCAP_TSOv4
   1542 			     |	IFCAP_TSOv6;
   1543 	ifp->if_capenable = 0;
   1544 
   1545 	ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER
   1546 			    |  ETHERCAP_VLAN_HWTAGGING
   1547 			    |  ETHERCAP_VLAN_HWCSUM
   1548 			    |  ETHERCAP_JUMBO_MTU
   1549 			    |  ETHERCAP_VLAN_MTU;
   1550 
   1551 	/* Enable the above capabilities by default */
   1552 	ec->ec_capenable = ec->ec_capabilities;
   1553 
   1554 	/* Don't enable LRO by default */
   1555 #if 0
   1556 	/* NetBSD doesn't support LRO yet */
   1557 	ifp->if_capabilities |= IFCAP_LRO;
   1558 #endif
   1559 
   1560 	/*
   1561 	 * Specify the media types supported by this adapter and register
   1562 	 * callbacks to update media and link information
   1563 	 */
   1564 	ec->ec_ifmedia = &adapter->media;
   1565 	ifmedia_init(&adapter->media, IFM_IMASK, ixv_media_change,
   1566 	    ixv_media_status);
   1567 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
   1568 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   1569 
   1570 	if_register(ifp);
   1571 
   1572 	return 0;
   1573 } /* ixv_setup_interface */
   1574 
   1575 
   1576 /************************************************************************
   1577  * ixv_initialize_transmit_units - Enable transmit unit.
   1578  ************************************************************************/
   1579 static void
   1580 ixv_initialize_transmit_units(struct adapter *adapter)
   1581 {
   1582 	struct tx_ring	*txr = adapter->tx_rings;
   1583 	struct ixgbe_hw	*hw = &adapter->hw;
   1584 	int i;
   1585 
   1586 	for (i = 0; i < adapter->num_queues; i++, txr++) {
   1587 		u64 tdba = txr->txdma.dma_paddr;
   1588 		u32 txctrl, txdctl;
   1589 		int j = txr->me;
   1590 
   1591 		/* Set WTHRESH to 8, burst writeback */
   1592 		txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
   1593 		txdctl |= (8 << 16);
   1594 		IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
   1595 
   1596 		/* Set the HW Tx Head and Tail indices */
   1597 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_VFTDH(j), 0);
   1598 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_VFTDT(j), 0);
   1599 
   1600 		/* Set Tx Tail register */
   1601 		txr->tail = IXGBE_VFTDT(j);
   1602 
   1603 		txr->txr_no_space = false;
   1604 
   1605 		/* Set Ring parameters */
   1606 		IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
   1607 		    (tdba & 0x00000000ffffffffULL));
   1608 		IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
   1609 		IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j),
   1610 		    adapter->num_tx_desc * sizeof(struct ixgbe_legacy_tx_desc));
   1611 		txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
   1612 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
   1613 		IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
   1614 
   1615 		/* Now enable */
   1616 		txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
   1617 		txdctl |= IXGBE_TXDCTL_ENABLE;
   1618 		IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
   1619 	}
   1620 
   1621 	return;
   1622 } /* ixv_initialize_transmit_units */
   1623 
   1624 
   1625 /************************************************************************
   1626  * ixv_initialize_rss_mapping
   1627  ************************************************************************/
   1628 static void
   1629 ixv_initialize_rss_mapping(struct adapter *adapter)
   1630 {
   1631 	struct ixgbe_hw *hw = &adapter->hw;
   1632 	u32		reta = 0, mrqc, rss_key[10];
   1633 	int		queue_id;
   1634 	int		i, j;
   1635 	u32		rss_hash_config;
   1636 
   1637 	/* force use default RSS key. */
   1638 #ifdef __NetBSD__
   1639 	rss_getkey((uint8_t *) &rss_key);
   1640 #else
   1641 	if (adapter->feat_en & IXGBE_FEATURE_RSS) {
   1642 		/* Fetch the configured RSS key */
   1643 		rss_getkey((uint8_t *)&rss_key);
   1644 	} else {
   1645 		/* set up random bits */
   1646 		cprng_fast(&rss_key, sizeof(rss_key));
   1647 	}
   1648 #endif
   1649 
   1650 	/* Now fill out hash function seeds */
   1651 	for (i = 0; i < 10; i++)
   1652 		IXGBE_WRITE_REG(hw, IXGBE_VFRSSRK(i), rss_key[i]);
   1653 
   1654 	/* Set up the redirection table */
   1655 	for (i = 0, j = 0; i < 64; i++, j++) {
   1656 		if (j == adapter->num_queues)
   1657 			j = 0;
   1658 
   1659 		if (adapter->feat_en & IXGBE_FEATURE_RSS) {
   1660 			/*
   1661 			 * Fetch the RSS bucket id for the given indirection
   1662 			 * entry. Cap it at the number of configured buckets
   1663 			 * (which is num_queues.)
   1664 			 */
   1665 			queue_id = rss_get_indirection_to_bucket(i);
   1666 			queue_id = queue_id % adapter->num_queues;
   1667 		} else
   1668 			queue_id = j;
   1669 
   1670 		/*
   1671 		 * The low 8 bits are for hash value (n+0);
   1672 		 * The next 8 bits are for hash value (n+1), etc.
   1673 		 */
   1674 		reta >>= 8;
   1675 		reta |= ((uint32_t)queue_id) << 24;
   1676 		if ((i & 3) == 3) {
   1677 			IXGBE_WRITE_REG(hw, IXGBE_VFRETA(i >> 2), reta);
   1678 			reta = 0;
   1679 		}
   1680 	}
   1681 
   1682 	/* Perform hash on these packet types */
   1683 	if (adapter->feat_en & IXGBE_FEATURE_RSS)
   1684 		rss_hash_config = rss_gethashconfig();
   1685 	else {
   1686 		/*
   1687 		 * Disable UDP - IP fragments aren't currently being handled
   1688 		 * and so we end up with a mix of 2-tuple and 4-tuple
   1689 		 * traffic.
   1690 		 */
   1691 		rss_hash_config = RSS_HASHTYPE_RSS_IPV4
   1692 				| RSS_HASHTYPE_RSS_TCP_IPV4
   1693 				| RSS_HASHTYPE_RSS_IPV6
   1694 				| RSS_HASHTYPE_RSS_TCP_IPV6;
   1695 	}
   1696 
   1697 	mrqc = IXGBE_MRQC_RSSEN;
   1698 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
   1699 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
   1700 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
   1701 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
   1702 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
   1703 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
   1704 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
   1705 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
   1706 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
   1707 		device_printf(adapter->dev, "%s: RSS_HASHTYPE_RSS_IPV6_EX defined, but not supported\n",
   1708 		    __func__);
   1709 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
   1710 		device_printf(adapter->dev, "%s: RSS_HASHTYPE_RSS_TCP_IPV6_EX defined, but not supported\n",
   1711 		    __func__);
   1712 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
   1713 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
   1714 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
   1715 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
   1716 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
   1717 		device_printf(adapter->dev, "%s: RSS_HASHTYPE_RSS_UDP_IPV6_EX defined, but not supported\n",
   1718 		    __func__);
   1719 	IXGBE_WRITE_REG(hw, IXGBE_VFMRQC, mrqc);
   1720 } /* ixv_initialize_rss_mapping */
   1721 
   1722 
   1723 /************************************************************************
   1724  * ixv_initialize_receive_units - Setup receive registers and features.
   1725  ************************************************************************/
   1726 static void
   1727 ixv_initialize_receive_units(struct adapter *adapter)
   1728 {
   1729 	struct	rx_ring	*rxr = adapter->rx_rings;
   1730 	struct ixgbe_hw	*hw = &adapter->hw;
   1731 	struct ifnet	*ifp = adapter->ifp;
   1732 	u32		bufsz, psrtype;
   1733 
   1734 	if (ifp->if_mtu > ETHERMTU)
   1735 		bufsz = 4096 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
   1736 	else
   1737 		bufsz = 2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
   1738 
   1739 	psrtype = IXGBE_PSRTYPE_TCPHDR
   1740 		| IXGBE_PSRTYPE_UDPHDR
   1741 		| IXGBE_PSRTYPE_IPV4HDR
   1742 		| IXGBE_PSRTYPE_IPV6HDR
   1743 		| IXGBE_PSRTYPE_L2HDR;
   1744 
   1745 	if (adapter->num_queues > 1)
   1746 		psrtype |= 1 << 29;
   1747 
   1748 	IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
   1749 
   1750 	/* Tell PF our max_frame size */
   1751 	if (ixgbevf_rlpml_set_vf(hw, adapter->max_frame_size) != 0) {
   1752 		device_printf(adapter->dev, "There is a problem with the PF setup.  It is likely the receive unit for this VF will not function correctly.\n");
   1753 	}
   1754 
   1755 	for (int i = 0; i < adapter->num_queues; i++, rxr++) {
   1756 		u64 rdba = rxr->rxdma.dma_paddr;
   1757 		u32 reg, rxdctl;
   1758 		int j = rxr->me;
   1759 
   1760 		/* Disable the queue */
   1761 		rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
   1762 		rxdctl &= ~IXGBE_RXDCTL_ENABLE;
   1763 		IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
   1764 		for (int k = 0; k < 10; k++) {
   1765 			if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) &
   1766 			    IXGBE_RXDCTL_ENABLE)
   1767 				msec_delay(1);
   1768 			else
   1769 				break;
   1770 		}
   1771 		wmb();
   1772 		/* Setup the Base and Length of the Rx Descriptor Ring */
   1773 		IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
   1774 		    (rdba & 0x00000000ffffffffULL));
   1775 		IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
   1776 		IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j),
   1777 		    adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
   1778 
   1779 		/* Reset the ring indices */
   1780 		IXGBE_WRITE_REG(hw, IXGBE_VFRDH(rxr->me), 0);
   1781 		IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me), 0);
   1782 
   1783 		/* Set up the SRRCTL register */
   1784 		reg = IXGBE_READ_REG(hw, IXGBE_VFSRRCTL(j));
   1785 		reg &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
   1786 		reg &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
   1787 		reg |= bufsz;
   1788 		reg |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
   1789 		IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(j), reg);
   1790 
   1791 		/* Capture Rx Tail index */
   1792 		rxr->tail = IXGBE_VFRDT(rxr->me);
   1793 
   1794 		/* Do the queue enabling last */
   1795 		rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
   1796 		IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
   1797 		for (int k = 0; k < 10; k++) {
   1798 			if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) &
   1799 			    IXGBE_RXDCTL_ENABLE)
   1800 				break;
   1801 			msec_delay(1);
   1802 		}
   1803 		wmb();
   1804 
   1805 		/* Set the Tail Pointer */
   1806 #ifdef DEV_NETMAP
   1807 		/*
   1808 		 * In netmap mode, we must preserve the buffers made
   1809 		 * available to userspace before the if_init()
   1810 		 * (this is true by default on the TX side, because
   1811 		 * init makes all buffers available to userspace).
   1812 		 *
   1813 		 * netmap_reset() and the device specific routines
   1814 		 * (e.g. ixgbe_setup_receive_rings()) map these
   1815 		 * buffers at the end of the NIC ring, so here we
   1816 		 * must set the RDT (tail) register to make sure
   1817 		 * they are not overwritten.
   1818 		 *
   1819 		 * In this driver the NIC ring starts at RDH = 0,
   1820 		 * RDT points to the last slot available for reception (?),
   1821 		 * so RDT = num_rx_desc - 1 means the whole ring is available.
   1822 		 */
   1823 		if ((adapter->feat_en & IXGBE_FEATURE_NETMAP) &&
   1824 		    (ifp->if_capenable & IFCAP_NETMAP)) {
   1825 			struct netmap_adapter *na = NA(adapter->ifp);
   1826 			struct netmap_kring *kring = na->rx_rings[i];
   1827 			int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
   1828 
   1829 			IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me), t);
   1830 		} else
   1831 #endif /* DEV_NETMAP */
   1832 			IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me),
   1833 			    adapter->num_rx_desc - 1);
   1834 	}
   1835 
   1836 	ixv_initialize_rss_mapping(adapter);
   1837 } /* ixv_initialize_receive_units */
   1838 
   1839 /************************************************************************
   1840  * ixv_sysctl_tdh_handler - Transmit Descriptor Head handler function
   1841  *
   1842  *   Retrieves the TDH value from the hardware
   1843  ************************************************************************/
   1844 static int
   1845 ixv_sysctl_tdh_handler(SYSCTLFN_ARGS)
   1846 {
   1847 	struct sysctlnode node = *rnode;
   1848 	struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
   1849 	uint32_t val;
   1850 
   1851 	if (!txr)
   1852 		return (0);
   1853 
   1854 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_VFTDH(txr->me));
   1855 	node.sysctl_data = &val;
   1856 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1857 } /* ixv_sysctl_tdh_handler */
   1858 
   1859 /************************************************************************
   1860  * ixgbe_sysctl_tdt_handler - Transmit Descriptor Tail handler function
   1861  *
   1862  *   Retrieves the TDT value from the hardware
   1863  ************************************************************************/
   1864 static int
   1865 ixv_sysctl_tdt_handler(SYSCTLFN_ARGS)
   1866 {
   1867 	struct sysctlnode node = *rnode;
   1868 	struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
   1869 	uint32_t val;
   1870 
   1871 	if (!txr)
   1872 		return (0);
   1873 
   1874 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_VFTDT(txr->me));
   1875 	node.sysctl_data = &val;
   1876 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1877 } /* ixv_sysctl_tdt_handler */
   1878 
   1879 /************************************************************************
   1880  * ixv_sysctl_next_to_check_handler - Receive Descriptor next to check
   1881  * handler function
   1882  *
   1883  *   Retrieves the next_to_check value
   1884  ************************************************************************/
   1885 static int
   1886 ixv_sysctl_next_to_check_handler(SYSCTLFN_ARGS)
   1887 {
   1888 	struct sysctlnode node = *rnode;
   1889 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   1890 	uint32_t val;
   1891 
   1892 	if (!rxr)
   1893 		return (0);
   1894 
   1895 	val = rxr->next_to_check;
   1896 	node.sysctl_data = &val;
   1897 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1898 } /* ixv_sysctl_next_to_check_handler */
   1899 
   1900 /************************************************************************
   1901  * ixv_sysctl_rdh_handler - Receive Descriptor Head handler function
   1902  *
   1903  *   Retrieves the RDH value from the hardware
   1904  ************************************************************************/
   1905 static int
   1906 ixv_sysctl_rdh_handler(SYSCTLFN_ARGS)
   1907 {
   1908 	struct sysctlnode node = *rnode;
   1909 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   1910 	uint32_t val;
   1911 
   1912 	if (!rxr)
   1913 		return (0);
   1914 
   1915 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_VFRDH(rxr->me));
   1916 	node.sysctl_data = &val;
   1917 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1918 } /* ixv_sysctl_rdh_handler */
   1919 
   1920 /************************************************************************
   1921  * ixv_sysctl_rdt_handler - Receive Descriptor Tail handler function
   1922  *
   1923  *   Retrieves the RDT value from the hardware
   1924  ************************************************************************/
   1925 static int
   1926 ixv_sysctl_rdt_handler(SYSCTLFN_ARGS)
   1927 {
   1928 	struct sysctlnode node = *rnode;
   1929 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   1930 	uint32_t val;
   1931 
   1932 	if (!rxr)
   1933 		return (0);
   1934 
   1935 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_VFRDT(rxr->me));
   1936 	node.sysctl_data = &val;
   1937 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1938 } /* ixv_sysctl_rdt_handler */
   1939 
   1940 static void
   1941 ixv_setup_vlan_tagging(struct adapter *adapter)
   1942 {
   1943 	struct ethercom *ec = &adapter->osdep.ec;
   1944 	struct ixgbe_hw *hw = &adapter->hw;
   1945 	struct rx_ring	*rxr;
   1946 	u32		ctrl;
   1947 	int		i;
   1948 	bool		hwtagging;
   1949 
   1950 	/* Enable HW tagging only if any vlan is attached */
   1951 	hwtagging = (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING)
   1952 	    && VLAN_ATTACHED(ec);
   1953 
   1954 	/* Enable the queues */
   1955 	for (i = 0; i < adapter->num_queues; i++) {
   1956 		rxr = &adapter->rx_rings[i];
   1957 		ctrl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(rxr->me));
   1958 		if (hwtagging)
   1959 			ctrl |= IXGBE_RXDCTL_VME;
   1960 		else
   1961 			ctrl &= ~IXGBE_RXDCTL_VME;
   1962 		IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(rxr->me), ctrl);
   1963 		/*
   1964 		 * Let Rx path know that it needs to store VLAN tag
   1965 		 * as part of extra mbuf info.
   1966 		 */
   1967 		rxr->vtag_strip = hwtagging ? TRUE : FALSE;
   1968 	}
   1969 } /* ixv_setup_vlan_tagging */
   1970 
   1971 /************************************************************************
   1972  * ixv_setup_vlan_support
   1973  ************************************************************************/
   1974 static int
   1975 ixv_setup_vlan_support(struct adapter *adapter)
   1976 {
   1977 	struct ethercom *ec = &adapter->osdep.ec;
   1978 	struct ixgbe_hw *hw = &adapter->hw;
   1979 	u32		vid, vfta, retry;
   1980 	struct vlanid_list *vlanidp;
   1981 	int rv, error = 0;
   1982 
   1983 	/*
   1984 	 *  This function is called from both if_init and ifflags_cb()
   1985 	 * on NetBSD.
   1986 	 */
   1987 
   1988 	/*
   1989 	 * Part 1:
   1990 	 * Setup VLAN HW tagging
   1991 	 */
   1992 	ixv_setup_vlan_tagging(adapter);
   1993 
   1994 	if (!VLAN_ATTACHED(ec))
   1995 		return 0;
   1996 
   1997 	/*
   1998 	 * Part 2:
   1999 	 * Setup VLAN HW filter
   2000 	 */
   2001 	/* Cleanup shadow_vfta */
   2002 	for (int i = 0; i < IXGBE_VFTA_SIZE; i++)
   2003 		adapter->shadow_vfta[i] = 0;
   2004 	/* Generate shadow_vfta from ec_vids */
   2005 	ETHER_LOCK(ec);
   2006 	SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
   2007 		uint32_t idx;
   2008 
   2009 		idx = vlanidp->vid / 32;
   2010 		KASSERT(idx < IXGBE_VFTA_SIZE);
   2011 		adapter->shadow_vfta[idx] |= (u32)1 << (vlanidp->vid % 32);
   2012 	}
   2013 	ETHER_UNLOCK(ec);
   2014 
   2015 	/*
   2016 	 * A soft reset zero's out the VFTA, so
   2017 	 * we need to repopulate it now.
   2018 	 */
   2019 	for (int i = 0; i < IXGBE_VFTA_SIZE; i++) {
   2020 		if (adapter->shadow_vfta[i] == 0)
   2021 			continue;
   2022 		vfta = adapter->shadow_vfta[i];
   2023 		/*
   2024 		 * Reconstruct the vlan id's
   2025 		 * based on the bits set in each
   2026 		 * of the array ints.
   2027 		 */
   2028 		for (int j = 0; j < 32; j++) {
   2029 			retry = 0;
   2030 			if ((vfta & ((u32)1 << j)) == 0)
   2031 				continue;
   2032 			vid = (i * 32) + j;
   2033 
   2034 			/* Call the shared code mailbox routine */
   2035 			while ((rv = hw->mac.ops.set_vfta(hw, vid, 0, TRUE,
   2036 			    FALSE)) != 0) {
   2037 				if (++retry > 5) {
   2038 					device_printf(adapter->dev,
   2039 					    "%s: max retry exceeded\n",
   2040 						__func__);
   2041 					break;
   2042 				}
   2043 			}
   2044 			if (rv != 0) {
   2045 				device_printf(adapter->dev,
   2046 				    "failed to set vlan %d\n", vid);
   2047 				error = EACCES;
   2048 			}
   2049 		}
   2050 	}
   2051 	return error;
   2052 } /* ixv_setup_vlan_support */
   2053 
   2054 static int
   2055 ixv_vlan_cb(struct ethercom *ec, uint16_t vid, bool set)
   2056 {
   2057 	struct ifnet *ifp = &ec->ec_if;
   2058 	struct adapter *adapter = ifp->if_softc;
   2059 	int rv;
   2060 
   2061 	if (set)
   2062 		rv = ixv_register_vlan(adapter, vid);
   2063 	else
   2064 		rv = ixv_unregister_vlan(adapter, vid);
   2065 
   2066 	if (rv != 0)
   2067 		return rv;
   2068 
   2069 	/*
   2070 	 * Control VLAN HW tagging when ec_nvlan is changed from 1 to 0
   2071 	 * or 0 to 1.
   2072 	 */
   2073 	if ((set && (ec->ec_nvlans == 1)) || (!set && (ec->ec_nvlans == 0)))
   2074 		ixv_setup_vlan_tagging(adapter);
   2075 
   2076 	return rv;
   2077 }
   2078 
   2079 /************************************************************************
   2080  * ixv_register_vlan
   2081  *
   2082  *   Run via a vlan config EVENT, it enables us to use the
   2083  *   HW Filter table since we can get the vlan id. This just
   2084  *   creates the entry in the soft version of the VFTA, init
   2085  *   will repopulate the real table.
   2086  ************************************************************************/
   2087 static int
   2088 ixv_register_vlan(struct adapter *adapter, u16 vtag)
   2089 {
   2090 	struct ixgbe_hw *hw = &adapter->hw;
   2091 	u16		index, bit;
   2092 	int error;
   2093 
   2094 	if ((vtag == 0) || (vtag > 4095)) /* Invalid */
   2095 		return EINVAL;
   2096 	IXGBE_CORE_LOCK(adapter);
   2097 	index = (vtag >> 5) & 0x7F;
   2098 	bit = vtag & 0x1F;
   2099 	adapter->shadow_vfta[index] |= ((u32)1 << bit);
   2100 	error = hw->mac.ops.set_vfta(hw, vtag, 0, true, false);
   2101 	IXGBE_CORE_UNLOCK(adapter);
   2102 
   2103 	if (error != 0) {
   2104 		device_printf(adapter->dev, "failed to register vlan %hu\n",
   2105 		    vtag);
   2106 		error = EACCES;
   2107 	}
   2108 	return error;
   2109 } /* ixv_register_vlan */
   2110 
   2111 /************************************************************************
   2112  * ixv_unregister_vlan
   2113  *
   2114  *   Run via a vlan unconfig EVENT, remove our entry
   2115  *   in the soft vfta.
   2116  ************************************************************************/
   2117 static int
   2118 ixv_unregister_vlan(struct adapter *adapter, u16 vtag)
   2119 {
   2120 	struct ixgbe_hw *hw = &adapter->hw;
   2121 	u16		index, bit;
   2122 	int 		error;
   2123 
   2124 	if ((vtag == 0) || (vtag > 4095))  /* Invalid */
   2125 		return EINVAL;
   2126 
   2127 	IXGBE_CORE_LOCK(adapter);
   2128 	index = (vtag >> 5) & 0x7F;
   2129 	bit = vtag & 0x1F;
   2130 	adapter->shadow_vfta[index] &= ~((u32)1 << bit);
   2131 	error = hw->mac.ops.set_vfta(hw, vtag, 0, false, false);
   2132 	IXGBE_CORE_UNLOCK(adapter);
   2133 
   2134 	if (error != 0) {
   2135 		device_printf(adapter->dev, "failed to unregister vlan %hu\n",
   2136 		    vtag);
   2137 		error = EIO;
   2138 	}
   2139 	return error;
   2140 } /* ixv_unregister_vlan */
   2141 
   2142 /************************************************************************
   2143  * ixv_enable_intr
   2144  ************************************************************************/
   2145 static void
   2146 ixv_enable_intr(struct adapter *adapter)
   2147 {
   2148 	struct ixgbe_hw *hw = &adapter->hw;
   2149 	struct ix_queue *que = adapter->queues;
   2150 	u32		mask;
   2151 	int i;
   2152 
   2153 	/* For VTEIAC */
   2154 	mask = (1 << adapter->vector);
   2155 	for (i = 0; i < adapter->num_queues; i++, que++)
   2156 		mask |= (1 << que->msix);
   2157 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, mask);
   2158 
   2159 	/* For VTEIMS */
   2160 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, (1 << adapter->vector));
   2161 	que = adapter->queues;
   2162 	for (i = 0; i < adapter->num_queues; i++, que++)
   2163 		ixv_enable_queue(adapter, que->msix);
   2164 
   2165 	IXGBE_WRITE_FLUSH(hw);
   2166 } /* ixv_enable_intr */
   2167 
   2168 /************************************************************************
   2169  * ixv_disable_intr
   2170  ************************************************************************/
   2171 static void
   2172 ixv_disable_intr(struct adapter *adapter)
   2173 {
   2174 	struct ix_queue	*que = adapter->queues;
   2175 
   2176 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEIAC, 0);
   2177 
   2178 	/* disable interrupts other than queues */
   2179 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEIMC, adapter->vector);
   2180 
   2181 	for (int i = 0; i < adapter->num_queues; i++, que++)
   2182 		ixv_disable_queue(adapter, que->msix);
   2183 
   2184 	IXGBE_WRITE_FLUSH(&adapter->hw);
   2185 } /* ixv_disable_intr */
   2186 
   2187 /************************************************************************
   2188  * ixv_set_ivar
   2189  *
   2190  *   Setup the correct IVAR register for a particular MSI-X interrupt
   2191  *    - entry is the register array entry
   2192  *    - vector is the MSI-X vector for this queue
   2193  *    - type is RX/TX/MISC
   2194  ************************************************************************/
   2195 static void
   2196 ixv_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
   2197 {
   2198 	struct ixgbe_hw *hw = &adapter->hw;
   2199 	u32		ivar, index;
   2200 
   2201 	vector |= IXGBE_IVAR_ALLOC_VAL;
   2202 
   2203 	if (type == -1) { /* MISC IVAR */
   2204 		ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
   2205 		ivar &= ~0xFF;
   2206 		ivar |= vector;
   2207 		IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
   2208 	} else {	  /* RX/TX IVARS */
   2209 		index = (16 * (entry & 1)) + (8 * type);
   2210 		ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(entry >> 1));
   2211 		ivar &= ~(0xffUL << index);
   2212 		ivar |= ((u32)vector << index);
   2213 		IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(entry >> 1), ivar);
   2214 	}
   2215 } /* ixv_set_ivar */
   2216 
   2217 /************************************************************************
   2218  * ixv_configure_ivars
   2219  ************************************************************************/
   2220 static void
   2221 ixv_configure_ivars(struct adapter *adapter)
   2222 {
   2223 	struct ix_queue *que = adapter->queues;
   2224 
   2225 	/* XXX We should sync EITR value calculation with ixgbe.c? */
   2226 
   2227 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   2228 		/* First the RX queue entry */
   2229 		ixv_set_ivar(adapter, i, que->msix, 0);
   2230 		/* ... and the TX */
   2231 		ixv_set_ivar(adapter, i, que->msix, 1);
   2232 		/* Set an initial value in EITR */
   2233 		ixv_eitr_write(adapter, que->msix, IXGBE_EITR_DEFAULT);
   2234 	}
   2235 
   2236 	/* For the mailbox interrupt */
   2237 	ixv_set_ivar(adapter, 1, adapter->vector, -1);
   2238 } /* ixv_configure_ivars */
   2239 
   2240 
   2241 /************************************************************************
   2242  * ixv_save_stats
   2243  *
   2244  *   The VF stats registers never have a truly virgin
   2245  *   starting point, so this routine tries to make an
   2246  *   artificial one, marking ground zero on attach as
   2247  *   it were.
   2248  ************************************************************************/
   2249 static void
   2250 ixv_save_stats(struct adapter *adapter)
   2251 {
   2252 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
   2253 
   2254 	if (stats->vfgprc.ev_count || stats->vfgptc.ev_count) {
   2255 		stats->saved_reset_vfgprc +=
   2256 		    stats->vfgprc.ev_count - stats->base_vfgprc;
   2257 		stats->saved_reset_vfgptc +=
   2258 		    stats->vfgptc.ev_count - stats->base_vfgptc;
   2259 		stats->saved_reset_vfgorc +=
   2260 		    stats->vfgorc.ev_count - stats->base_vfgorc;
   2261 		stats->saved_reset_vfgotc +=
   2262 		    stats->vfgotc.ev_count - stats->base_vfgotc;
   2263 		stats->saved_reset_vfmprc +=
   2264 		    stats->vfmprc.ev_count - stats->base_vfmprc;
   2265 	}
   2266 } /* ixv_save_stats */
   2267 
   2268 /************************************************************************
   2269  * ixv_init_stats
   2270  ************************************************************************/
   2271 static void
   2272 ixv_init_stats(struct adapter *adapter)
   2273 {
   2274 	struct ixgbe_hw *hw = &adapter->hw;
   2275 
   2276 	adapter->stats.vf.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
   2277 	adapter->stats.vf.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
   2278 	adapter->stats.vf.last_vfgorc |=
   2279 	    (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
   2280 
   2281 	adapter->stats.vf.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
   2282 	adapter->stats.vf.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
   2283 	adapter->stats.vf.last_vfgotc |=
   2284 	    (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
   2285 
   2286 	adapter->stats.vf.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
   2287 
   2288 	adapter->stats.vf.base_vfgprc = adapter->stats.vf.last_vfgprc;
   2289 	adapter->stats.vf.base_vfgorc = adapter->stats.vf.last_vfgorc;
   2290 	adapter->stats.vf.base_vfgptc = adapter->stats.vf.last_vfgptc;
   2291 	adapter->stats.vf.base_vfgotc = adapter->stats.vf.last_vfgotc;
   2292 	adapter->stats.vf.base_vfmprc = adapter->stats.vf.last_vfmprc;
   2293 } /* ixv_init_stats */
   2294 
   2295 #define UPDATE_STAT_32(reg, last, count)		\
   2296 {							\
   2297 	u32 current = IXGBE_READ_REG(hw, (reg));	\
   2298 	if (current < (last))				\
   2299 		count.ev_count += 0x100000000LL;	\
   2300 	(last) = current;				\
   2301 	count.ev_count &= 0xFFFFFFFF00000000LL;		\
   2302 	count.ev_count |= current;			\
   2303 }
   2304 
   2305 #define UPDATE_STAT_36(lsb, msb, last, count)		\
   2306 {							\
   2307 	u64 cur_lsb = IXGBE_READ_REG(hw, (lsb));	\
   2308 	u64 cur_msb = IXGBE_READ_REG(hw, (msb));	\
   2309 	u64 current = ((cur_msb << 32) | cur_lsb);	\
   2310 	if (current < (last))				\
   2311 		count.ev_count += 0x1000000000LL;	\
   2312 	(last) = current;				\
   2313 	count.ev_count &= 0xFFFFFFF000000000LL;		\
   2314 	count.ev_count |= current;			\
   2315 }
   2316 
   2317 /************************************************************************
   2318  * ixv_update_stats - Update the board statistics counters.
   2319  ************************************************************************/
   2320 void
   2321 ixv_update_stats(struct adapter *adapter)
   2322 {
   2323 	struct ixgbe_hw *hw = &adapter->hw;
   2324 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
   2325 
   2326 	UPDATE_STAT_32(IXGBE_VFGPRC, stats->last_vfgprc, stats->vfgprc);
   2327 	UPDATE_STAT_32(IXGBE_VFGPTC, stats->last_vfgptc, stats->vfgptc);
   2328 	UPDATE_STAT_36(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB, stats->last_vfgorc,
   2329 	    stats->vfgorc);
   2330 	UPDATE_STAT_36(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB, stats->last_vfgotc,
   2331 	    stats->vfgotc);
   2332 	UPDATE_STAT_32(IXGBE_VFMPRC, stats->last_vfmprc, stats->vfmprc);
   2333 
   2334 	/* Fill out the OS statistics structure */
   2335 	/*
   2336 	 * NetBSD: Don't override if_{i|o}{packets|bytes|mcasts} with
   2337 	 * adapter->stats counters. It's required to make ifconfig -z
   2338 	 * (SOICZIFDATA) work.
   2339 	 */
   2340 } /* ixv_update_stats */
   2341 
   2342 /************************************************************************
   2343  * ixv_sysctl_interrupt_rate_handler
   2344  ************************************************************************/
   2345 static int
   2346 ixv_sysctl_interrupt_rate_handler(SYSCTLFN_ARGS)
   2347 {
   2348 	struct sysctlnode node = *rnode;
   2349 	struct ix_queue *que = (struct ix_queue *)node.sysctl_data;
   2350 	struct adapter	*adapter = que->adapter;
   2351 	uint32_t reg, usec, rate;
   2352 	int error;
   2353 
   2354 	if (que == NULL)
   2355 		return 0;
   2356 	reg = IXGBE_READ_REG(&que->adapter->hw, IXGBE_VTEITR(que->msix));
   2357 	usec = ((reg & 0x0FF8) >> 3);
   2358 	if (usec > 0)
   2359 		rate = 500000 / usec;
   2360 	else
   2361 		rate = 0;
   2362 	node.sysctl_data = &rate;
   2363 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2364 	if (error || newp == NULL)
   2365 		return error;
   2366 	reg &= ~0xfff; /* default, no limitation */
   2367 	if (rate > 0 && rate < 500000) {
   2368 		if (rate < 1000)
   2369 			rate = 1000;
   2370 		reg |= ((4000000/rate) & 0xff8);
   2371 		/*
   2372 		 * When RSC is used, ITR interval must be larger than
   2373 		 * RSC_DELAY. Currently, we use 2us for RSC_DELAY.
   2374 		 * The minimum value is always greater than 2us on 100M
   2375 		 * (and 10M?(not documented)), but it's not on 1G and higher.
   2376 		 */
   2377 		if ((adapter->link_speed != IXGBE_LINK_SPEED_100_FULL)
   2378 		    && (adapter->link_speed != IXGBE_LINK_SPEED_10_FULL)) {
   2379 			if ((adapter->num_queues > 1)
   2380 			    && (reg < IXGBE_MIN_RSC_EITR_10G1G))
   2381 				return EINVAL;
   2382 		}
   2383 		ixv_max_interrupt_rate = rate;
   2384 	} else
   2385 		ixv_max_interrupt_rate = 0;
   2386 	ixv_eitr_write(adapter, que->msix, reg);
   2387 
   2388 	return (0);
   2389 } /* ixv_sysctl_interrupt_rate_handler */
   2390 
   2391 const struct sysctlnode *
   2392 ixv_sysctl_instance(struct adapter *adapter)
   2393 {
   2394 	const char *dvname;
   2395 	struct sysctllog **log;
   2396 	int rc;
   2397 	const struct sysctlnode *rnode;
   2398 
   2399 	log = &adapter->sysctllog;
   2400 	dvname = device_xname(adapter->dev);
   2401 
   2402 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
   2403 	    0, CTLTYPE_NODE, dvname,
   2404 	    SYSCTL_DESCR("ixv information and settings"),
   2405 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
   2406 		goto err;
   2407 
   2408 	return rnode;
   2409 err:
   2410 	device_printf(adapter->dev,
   2411 	    "%s: sysctl_createv failed, rc = %d\n", __func__, rc);
   2412 	return NULL;
   2413 }
   2414 
   2415 static void
   2416 ixv_add_device_sysctls(struct adapter *adapter)
   2417 {
   2418 	struct sysctllog **log;
   2419 	const struct sysctlnode *rnode, *cnode;
   2420 	device_t dev;
   2421 
   2422 	dev = adapter->dev;
   2423 	log = &adapter->sysctllog;
   2424 
   2425 	if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
   2426 		aprint_error_dev(dev, "could not create sysctl root\n");
   2427 		return;
   2428 	}
   2429 
   2430 	if (sysctl_createv(log, 0, &rnode, &cnode,
   2431 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   2432 	    "debug", SYSCTL_DESCR("Debug Info"),
   2433 	    ixv_sysctl_debug, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   2434 		aprint_error_dev(dev, "could not create sysctl\n");
   2435 
   2436 	if (sysctl_createv(log, 0, &rnode, &cnode,
   2437 	    CTLFLAG_READWRITE, CTLTYPE_BOOL,
   2438 	    "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
   2439 	    NULL, 0, &adapter->enable_aim, 0, CTL_CREATE, CTL_EOL) != 0)
   2440 		aprint_error_dev(dev, "could not create sysctl\n");
   2441 
   2442 	if (sysctl_createv(log, 0, &rnode, &cnode,
   2443 	    CTLFLAG_READWRITE, CTLTYPE_BOOL,
   2444 	    "txrx_workqueue", SYSCTL_DESCR("Use workqueue for packet processing"),
   2445 		NULL, 0, &adapter->txrx_use_workqueue, 0, CTL_CREATE, CTL_EOL) != 0)
   2446 		aprint_error_dev(dev, "could not create sysctl\n");
   2447 }
   2448 
   2449 /************************************************************************
   2450  * ixv_add_stats_sysctls - Add statistic sysctls for the VF.
   2451  ************************************************************************/
   2452 static void
   2453 ixv_add_stats_sysctls(struct adapter *adapter)
   2454 {
   2455 	device_t		dev = adapter->dev;
   2456 	struct tx_ring		*txr = adapter->tx_rings;
   2457 	struct rx_ring		*rxr = adapter->rx_rings;
   2458 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
   2459 	struct ixgbe_hw *hw = &adapter->hw;
   2460 	const struct sysctlnode *rnode, *cnode;
   2461 	struct sysctllog **log = &adapter->sysctllog;
   2462 	const char *xname = device_xname(dev);
   2463 
   2464 	/* Driver Statistics */
   2465 	evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
   2466 	    NULL, xname, "Driver tx dma soft fail EFBIG");
   2467 	evcnt_attach_dynamic(&adapter->mbuf_defrag_failed, EVCNT_TYPE_MISC,
   2468 	    NULL, xname, "m_defrag() failed");
   2469 	evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
   2470 	    NULL, xname, "Driver tx dma hard fail EFBIG");
   2471 	evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
   2472 	    NULL, xname, "Driver tx dma hard fail EINVAL");
   2473 	evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
   2474 	    NULL, xname, "Driver tx dma hard fail other");
   2475 	evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
   2476 	    NULL, xname, "Driver tx dma soft fail EAGAIN");
   2477 	evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
   2478 	    NULL, xname, "Driver tx dma soft fail ENOMEM");
   2479 	evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
   2480 	    NULL, xname, "Watchdog timeouts");
   2481 	evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
   2482 	    NULL, xname, "TSO errors");
   2483 	evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_INTR,
   2484 	    NULL, xname, "Link MSI-X IRQ Handled");
   2485 
   2486 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   2487 		snprintf(adapter->queues[i].evnamebuf,
   2488 		    sizeof(adapter->queues[i].evnamebuf), "%s q%d",
   2489 		    xname, i);
   2490 		snprintf(adapter->queues[i].namebuf,
   2491 		    sizeof(adapter->queues[i].namebuf), "q%d", i);
   2492 
   2493 		if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
   2494 			aprint_error_dev(dev, "could not create sysctl root\n");
   2495 			break;
   2496 		}
   2497 
   2498 		if (sysctl_createv(log, 0, &rnode, &rnode,
   2499 		    0, CTLTYPE_NODE,
   2500 		    adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
   2501 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   2502 			break;
   2503 
   2504 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2505 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   2506 		    "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
   2507 		    ixv_sysctl_interrupt_rate_handler, 0,
   2508 		    (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
   2509 			break;
   2510 
   2511 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2512 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2513 		    "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
   2514 		    ixv_sysctl_tdh_handler, 0, (void *)txr,
   2515 		    0, CTL_CREATE, CTL_EOL) != 0)
   2516 			break;
   2517 
   2518 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2519 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2520 		    "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
   2521 		    ixv_sysctl_tdt_handler, 0, (void *)txr,
   2522 		    0, CTL_CREATE, CTL_EOL) != 0)
   2523 			break;
   2524 
   2525 		evcnt_attach_dynamic(&adapter->queues[i].irqs, EVCNT_TYPE_INTR,
   2526 		    NULL, adapter->queues[i].evnamebuf, "IRQs on queue");
   2527 		evcnt_attach_dynamic(&adapter->queues[i].handleq,
   2528 		    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   2529 		    "Handled queue in softint");
   2530 		evcnt_attach_dynamic(&adapter->queues[i].req, EVCNT_TYPE_MISC,
   2531 		    NULL, adapter->queues[i].evnamebuf, "Requeued in softint");
   2532 		evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
   2533 		    NULL, adapter->queues[i].evnamebuf, "TSO");
   2534 		evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
   2535 		    NULL, adapter->queues[i].evnamebuf,
   2536 		    "Queue No Descriptor Available");
   2537 		evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
   2538 		    NULL, adapter->queues[i].evnamebuf,
   2539 		    "Queue Packets Transmitted");
   2540 #ifndef IXGBE_LEGACY_TX
   2541 		evcnt_attach_dynamic(&txr->pcq_drops, EVCNT_TYPE_MISC,
   2542 		    NULL, adapter->queues[i].evnamebuf,
   2543 		    "Packets dropped in pcq");
   2544 #endif
   2545 
   2546 #ifdef LRO
   2547 		struct lro_ctrl *lro = &rxr->lro;
   2548 #endif /* LRO */
   2549 
   2550 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2551 		    CTLFLAG_READONLY,
   2552 		    CTLTYPE_INT,
   2553 		    "rxd_nxck", SYSCTL_DESCR("Receive Descriptor next to check"),
   2554 			ixv_sysctl_next_to_check_handler, 0, (void *)rxr, 0,
   2555 		    CTL_CREATE, CTL_EOL) != 0)
   2556 			break;
   2557 
   2558 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2559 		    CTLFLAG_READONLY,
   2560 		    CTLTYPE_INT,
   2561 		    "rxd_head", SYSCTL_DESCR("Receive Descriptor Head"),
   2562 		    ixv_sysctl_rdh_handler, 0, (void *)rxr, 0,
   2563 		    CTL_CREATE, CTL_EOL) != 0)
   2564 			break;
   2565 
   2566 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2567 		    CTLFLAG_READONLY,
   2568 		    CTLTYPE_INT,
   2569 		    "rxd_tail", SYSCTL_DESCR("Receive Descriptor Tail"),
   2570 		    ixv_sysctl_rdt_handler, 0, (void *)rxr, 0,
   2571 		    CTL_CREATE, CTL_EOL) != 0)
   2572 			break;
   2573 
   2574 		evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
   2575 		    NULL, adapter->queues[i].evnamebuf, "Queue Packets Received");
   2576 		evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
   2577 		    NULL, adapter->queues[i].evnamebuf, "Queue Bytes Received");
   2578 		evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
   2579 		    NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
   2580 		evcnt_attach_dynamic(&rxr->no_jmbuf, EVCNT_TYPE_MISC,
   2581 		    NULL, adapter->queues[i].evnamebuf, "Rx no jumbo mbuf");
   2582 		evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
   2583 		    NULL, adapter->queues[i].evnamebuf, "Rx discarded");
   2584 #ifdef LRO
   2585 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
   2586 				CTLFLAG_RD, &lro->lro_queued, 0,
   2587 				"LRO Queued");
   2588 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
   2589 				CTLFLAG_RD, &lro->lro_flushed, 0,
   2590 				"LRO Flushed");
   2591 #endif /* LRO */
   2592 	}
   2593 
   2594 	/* MAC stats get their own sub node */
   2595 
   2596 	snprintf(stats->namebuf,
   2597 	    sizeof(stats->namebuf), "%s MAC Statistics", xname);
   2598 
   2599 	evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
   2600 	    stats->namebuf, "rx csum offload - IP");
   2601 	evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
   2602 	    stats->namebuf, "rx csum offload - L4");
   2603 	evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
   2604 	    stats->namebuf, "rx csum offload - IP bad");
   2605 	evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
   2606 	    stats->namebuf, "rx csum offload - L4 bad");
   2607 
   2608 	/* Packet Reception Stats */
   2609 	evcnt_attach_dynamic(&stats->vfgprc, EVCNT_TYPE_MISC, NULL,
   2610 	    xname, "Good Packets Received");
   2611 	evcnt_attach_dynamic(&stats->vfgorc, EVCNT_TYPE_MISC, NULL,
   2612 	    xname, "Good Octets Received");
   2613 	evcnt_attach_dynamic(&stats->vfmprc, EVCNT_TYPE_MISC, NULL,
   2614 	    xname, "Multicast Packets Received");
   2615 	evcnt_attach_dynamic(&stats->vfgptc, EVCNT_TYPE_MISC, NULL,
   2616 	    xname, "Good Packets Transmitted");
   2617 	evcnt_attach_dynamic(&stats->vfgotc, EVCNT_TYPE_MISC, NULL,
   2618 	    xname, "Good Octets Transmitted");
   2619 
   2620 	/* Mailbox Stats */
   2621 	evcnt_attach_dynamic(&hw->mbx.stats.msgs_tx, EVCNT_TYPE_MISC, NULL,
   2622 	    xname, "message TXs");
   2623 	evcnt_attach_dynamic(&hw->mbx.stats.msgs_rx, EVCNT_TYPE_MISC, NULL,
   2624 	    xname, "message RXs");
   2625 	evcnt_attach_dynamic(&hw->mbx.stats.acks, EVCNT_TYPE_MISC, NULL,
   2626 	    xname, "ACKs");
   2627 	evcnt_attach_dynamic(&hw->mbx.stats.reqs, EVCNT_TYPE_MISC, NULL,
   2628 	    xname, "REQs");
   2629 	evcnt_attach_dynamic(&hw->mbx.stats.rsts, EVCNT_TYPE_MISC, NULL,
   2630 	    xname, "RSTs");
   2631 
   2632 } /* ixv_add_stats_sysctls */
   2633 
   2634 static void
   2635 ixv_clear_evcnt(struct adapter *adapter)
   2636 {
   2637 	struct tx_ring		*txr = adapter->tx_rings;
   2638 	struct rx_ring		*rxr = adapter->rx_rings;
   2639 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
   2640 	struct ixgbe_hw *hw = &adapter->hw;
   2641 	int i;
   2642 
   2643 	/* Driver Statistics */
   2644 	adapter->efbig_tx_dma_setup.ev_count = 0;
   2645 	adapter->mbuf_defrag_failed.ev_count = 0;
   2646 	adapter->efbig2_tx_dma_setup.ev_count = 0;
   2647 	adapter->einval_tx_dma_setup.ev_count = 0;
   2648 	adapter->other_tx_dma_setup.ev_count = 0;
   2649 	adapter->eagain_tx_dma_setup.ev_count = 0;
   2650 	adapter->enomem_tx_dma_setup.ev_count = 0;
   2651 	adapter->watchdog_events.ev_count = 0;
   2652 	adapter->tso_err.ev_count = 0;
   2653 	adapter->link_irq.ev_count = 0;
   2654 
   2655 	for (i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   2656 		adapter->queues[i].irqs.ev_count = 0;
   2657 		adapter->queues[i].handleq.ev_count = 0;
   2658 		adapter->queues[i].req.ev_count = 0;
   2659 		txr->tso_tx.ev_count = 0;
   2660 		txr->no_desc_avail.ev_count = 0;
   2661 		txr->total_packets.ev_count = 0;
   2662 #ifndef IXGBE_LEGACY_TX
   2663 		txr->pcq_drops.ev_count = 0;
   2664 #endif
   2665 		txr->q_efbig_tx_dma_setup = 0;
   2666 		txr->q_mbuf_defrag_failed = 0;
   2667 		txr->q_efbig2_tx_dma_setup = 0;
   2668 		txr->q_einval_tx_dma_setup = 0;
   2669 		txr->q_other_tx_dma_setup = 0;
   2670 		txr->q_eagain_tx_dma_setup = 0;
   2671 		txr->q_enomem_tx_dma_setup = 0;
   2672 		txr->q_tso_err = 0;
   2673 
   2674 		rxr->rx_packets.ev_count = 0;
   2675 		rxr->rx_bytes.ev_count = 0;
   2676 		rxr->rx_copies.ev_count = 0;
   2677 		rxr->no_jmbuf.ev_count = 0;
   2678 		rxr->rx_discarded.ev_count = 0;
   2679 	}
   2680 
   2681 	/* MAC stats get their own sub node */
   2682 
   2683 	stats->ipcs.ev_count = 0;
   2684 	stats->l4cs.ev_count = 0;
   2685 	stats->ipcs_bad.ev_count = 0;
   2686 	stats->l4cs_bad.ev_count = 0;
   2687 
   2688 	/* Packet Reception Stats */
   2689 	stats->vfgprc.ev_count = 0;
   2690 	stats->vfgorc.ev_count = 0;
   2691 	stats->vfmprc.ev_count = 0;
   2692 	stats->vfgptc.ev_count = 0;
   2693 	stats->vfgotc.ev_count = 0;
   2694 
   2695 	/* Mailbox Stats */
   2696 	hw->mbx.stats.msgs_tx.ev_count = 0;
   2697 	hw->mbx.stats.msgs_rx.ev_count = 0;
   2698 	hw->mbx.stats.acks.ev_count = 0;
   2699 	hw->mbx.stats.reqs.ev_count = 0;
   2700 	hw->mbx.stats.rsts.ev_count = 0;
   2701 
   2702 } /* ixv_clear_evcnt */
   2703 
   2704 /************************************************************************
   2705  * ixv_set_sysctl_value
   2706  ************************************************************************/
   2707 static void
   2708 ixv_set_sysctl_value(struct adapter *adapter, const char *name,
   2709 	const char *description, int *limit, int value)
   2710 {
   2711 	device_t dev =	adapter->dev;
   2712 	struct sysctllog **log;
   2713 	const struct sysctlnode *rnode, *cnode;
   2714 
   2715 	log = &adapter->sysctllog;
   2716 	if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
   2717 		aprint_error_dev(dev, "could not create sysctl root\n");
   2718 		return;
   2719 	}
   2720 	if (sysctl_createv(log, 0, &rnode, &cnode,
   2721 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   2722 	    name, SYSCTL_DESCR(description),
   2723 	    NULL, 0, limit, 0, CTL_CREATE, CTL_EOL) != 0)
   2724 		aprint_error_dev(dev, "could not create sysctl\n");
   2725 	*limit = value;
   2726 } /* ixv_set_sysctl_value */
   2727 
   2728 /************************************************************************
   2729  * ixv_print_debug_info
   2730  *
   2731  *   Called only when em_display_debug_stats is enabled.
   2732  *   Provides a way to take a look at important statistics
   2733  *   maintained by the driver and hardware.
   2734  ************************************************************************/
   2735 static void
   2736 ixv_print_debug_info(struct adapter *adapter)
   2737 {
   2738 	device_t	dev = adapter->dev;
   2739 	struct ix_queue *que = adapter->queues;
   2740 	struct rx_ring	*rxr;
   2741 	struct tx_ring	*txr;
   2742 #ifdef LRO
   2743 	struct lro_ctrl *lro;
   2744 #endif /* LRO */
   2745 
   2746 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   2747 		txr = que->txr;
   2748 		rxr = que->rxr;
   2749 #ifdef LRO
   2750 		lro = &rxr->lro;
   2751 #endif /* LRO */
   2752 		device_printf(dev, "QUE(%d) IRQs Handled: %lu\n",
   2753 		    que->msix, (long)que->irqs.ev_count);
   2754 		device_printf(dev, "RX(%d) Packets Received: %lld\n",
   2755 		    rxr->me, (long long)rxr->rx_packets.ev_count);
   2756 		device_printf(dev, "RX(%d) Bytes Received: %lu\n",
   2757 		    rxr->me, (long)rxr->rx_bytes.ev_count);
   2758 #ifdef LRO
   2759 		device_printf(dev, "RX(%d) LRO Queued= %ju\n",
   2760 		    rxr->me, (uintmax_t)lro->lro_queued);
   2761 		device_printf(dev, "RX(%d) LRO Flushed= %ju\n",
   2762 		    rxr->me, (uintmax_t)lro->lro_flushed);
   2763 #endif /* LRO */
   2764 		device_printf(dev, "TX(%d) Packets Sent: %lu\n",
   2765 		    txr->me, (long)txr->total_packets.ev_count);
   2766 		device_printf(dev, "TX(%d) NO Desc Avail: %lu\n",
   2767 		    txr->me, (long)txr->no_desc_avail.ev_count);
   2768 	}
   2769 
   2770 	device_printf(dev, "MBX IRQ Handled: %lu\n",
   2771 	    (long)adapter->link_irq.ev_count);
   2772 } /* ixv_print_debug_info */
   2773 
   2774 /************************************************************************
   2775  * ixv_sysctl_debug
   2776  ************************************************************************/
   2777 static int
   2778 ixv_sysctl_debug(SYSCTLFN_ARGS)
   2779 {
   2780 	struct sysctlnode node = *rnode;
   2781 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   2782 	int	       error, result;
   2783 
   2784 	node.sysctl_data = &result;
   2785 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2786 
   2787 	if (error || newp == NULL)
   2788 		return error;
   2789 
   2790 	if (result == 1)
   2791 		ixv_print_debug_info(adapter);
   2792 
   2793 	return 0;
   2794 } /* ixv_sysctl_debug */
   2795 
   2796 /************************************************************************
   2797  * ixv_init_device_features
   2798  ************************************************************************/
   2799 static void
   2800 ixv_init_device_features(struct adapter *adapter)
   2801 {
   2802 	adapter->feat_cap = IXGBE_FEATURE_NETMAP
   2803 			  | IXGBE_FEATURE_VF
   2804 			  | IXGBE_FEATURE_RSS
   2805 			  | IXGBE_FEATURE_LEGACY_TX;
   2806 
   2807 	/* A tad short on feature flags for VFs, atm. */
   2808 	switch (adapter->hw.mac.type) {
   2809 	case ixgbe_mac_82599_vf:
   2810 		break;
   2811 	case ixgbe_mac_X540_vf:
   2812 		break;
   2813 	case ixgbe_mac_X550_vf:
   2814 	case ixgbe_mac_X550EM_x_vf:
   2815 	case ixgbe_mac_X550EM_a_vf:
   2816 		adapter->feat_cap |= IXGBE_FEATURE_NEEDS_CTXD;
   2817 		break;
   2818 	default:
   2819 		break;
   2820 	}
   2821 
   2822 	/* Enabled by default... */
   2823 	/* Is a virtual function (VF) */
   2824 	if (adapter->feat_cap & IXGBE_FEATURE_VF)
   2825 		adapter->feat_en |= IXGBE_FEATURE_VF;
   2826 	/* Netmap */
   2827 	if (adapter->feat_cap & IXGBE_FEATURE_NETMAP)
   2828 		adapter->feat_en |= IXGBE_FEATURE_NETMAP;
   2829 	/* Receive-Side Scaling (RSS) */
   2830 	if (adapter->feat_cap & IXGBE_FEATURE_RSS)
   2831 		adapter->feat_en |= IXGBE_FEATURE_RSS;
   2832 	/* Needs advanced context descriptor regardless of offloads req'd */
   2833 	if (adapter->feat_cap & IXGBE_FEATURE_NEEDS_CTXD)
   2834 		adapter->feat_en |= IXGBE_FEATURE_NEEDS_CTXD;
   2835 
   2836 	/* Enabled via sysctl... */
   2837 	/* Legacy (single queue) transmit */
   2838 	if ((adapter->feat_cap & IXGBE_FEATURE_LEGACY_TX) &&
   2839 	    ixv_enable_legacy_tx)
   2840 		adapter->feat_en |= IXGBE_FEATURE_LEGACY_TX;
   2841 } /* ixv_init_device_features */
   2842 
   2843 /************************************************************************
   2844  * ixv_shutdown - Shutdown entry point
   2845  ************************************************************************/
   2846 #if 0 /* XXX NetBSD ought to register something like this through pmf(9) */
   2847 static int
   2848 ixv_shutdown(device_t dev)
   2849 {
   2850 	struct adapter *adapter = device_private(dev);
   2851 	IXGBE_CORE_LOCK(adapter);
   2852 	ixv_stop(adapter);
   2853 	IXGBE_CORE_UNLOCK(adapter);
   2854 
   2855 	return (0);
   2856 } /* ixv_shutdown */
   2857 #endif
   2858 
   2859 static int
   2860 ixv_ifflags_cb(struct ethercom *ec)
   2861 {
   2862 	struct ifnet *ifp = &ec->ec_if;
   2863 	struct adapter *adapter = ifp->if_softc;
   2864 	int change, rv = 0;
   2865 
   2866 	IXGBE_CORE_LOCK(adapter);
   2867 
   2868 	change = ifp->if_flags ^ adapter->if_flags;
   2869 	if (change != 0)
   2870 		adapter->if_flags = ifp->if_flags;
   2871 
   2872 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
   2873 		rv = ENETRESET;
   2874 		goto out;
   2875 	}
   2876 
   2877 	/* Check for ec_capenable. */
   2878 	change = ec->ec_capenable ^ adapter->ec_capenable;
   2879 	adapter->ec_capenable = ec->ec_capenable;
   2880 	if ((change & ~(ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING
   2881 	    | ETHERCAP_VLAN_HWFILTER)) != 0) {
   2882 		rv = ENETRESET;
   2883 		goto out;
   2884 	}
   2885 
   2886 	/*
   2887 	 * Special handling is not required for ETHERCAP_VLAN_MTU.
   2888 	 * PF's MAXFRS(MHADD) does not include the 4bytes of the VLAN header.
   2889 	 */
   2890 
   2891 	/* Set up VLAN support and filter */
   2892 	if ((change & (ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_HWFILTER)) != 0)
   2893 		rv = ixv_setup_vlan_support(adapter);
   2894 
   2895 out:
   2896 	IXGBE_CORE_UNLOCK(adapter);
   2897 
   2898 	return rv;
   2899 }
   2900 
   2901 
   2902 /************************************************************************
   2903  * ixv_ioctl - Ioctl entry point
   2904  *
   2905  *   Called when the user wants to configure the interface.
   2906  *
   2907  *   return 0 on success, positive on failure
   2908  ************************************************************************/
   2909 static int
   2910 ixv_ioctl(struct ifnet *ifp, u_long command, void *data)
   2911 {
   2912 	struct adapter	*adapter = ifp->if_softc;
   2913 	struct ifcapreq *ifcr = data;
   2914 	int		error = 0;
   2915 	int l4csum_en;
   2916 	const int l4csum = IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
   2917 	     IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
   2918 
   2919 	switch (command) {
   2920 	case SIOCSIFFLAGS:
   2921 		IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
   2922 		break;
   2923 	case SIOCADDMULTI: {
   2924 		struct ether_multi *enm;
   2925 		struct ether_multistep step;
   2926 		struct ethercom *ec = &adapter->osdep.ec;
   2927 		int mcnt = 0;
   2928 
   2929 		/*
   2930 		 * Check the number of multicast address. If it exceeds,
   2931 		 * return ENOSPC.
   2932 		 * Update this code when we support API 1.3.
   2933 		 */
   2934 		ETHER_LOCK(ec);
   2935 		ETHER_FIRST_MULTI(step, ec, enm);
   2936 		while (enm != NULL) {
   2937 			mcnt++;
   2938 
   2939 			/*
   2940 			 * This code is before adding, so one room is required
   2941 			 * at least.
   2942 			 */
   2943 			if (mcnt > (IXGBE_MAX_VF_MC - 1)) {
   2944 				device_printf(adapter->dev,
   2945 				    "number of Ethernet multicast addresses "
   2946 				    "exceeds the limit (%d)\n",
   2947 				    IXGBE_MAX_VF_MC);
   2948 				error = ENOSPC;
   2949 				break;
   2950 			}
   2951 			ETHER_NEXT_MULTI(step, enm);
   2952 		}
   2953 		ETHER_UNLOCK(ec);
   2954 		if (error)
   2955 			return error;
   2956 	}
   2957 		/*FALLTHROUGH*/
   2958 	case SIOCDELMULTI:
   2959 		IOCTL_DEBUGOUT("ioctl: SIOC(ADD|DEL)MULTI");
   2960 		break;
   2961 	case SIOCSIFMEDIA:
   2962 	case SIOCGIFMEDIA:
   2963 		IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
   2964 		break;
   2965 	case SIOCSIFCAP:
   2966 		IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
   2967 		break;
   2968 	case SIOCSIFMTU:
   2969 		IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
   2970 		break;
   2971 	case SIOCZIFDATA:
   2972 		IOCTL_DEBUGOUT("ioctl: SIOCZIFDATA (Zero counter)");
   2973 		ixv_update_stats(adapter);
   2974 		ixv_clear_evcnt(adapter);
   2975 		break;
   2976 	default:
   2977 		IOCTL_DEBUGOUT1("ioctl: UNKNOWN (0x%X)", (int)command);
   2978 		break;
   2979 	}
   2980 
   2981 	switch (command) {
   2982 	case SIOCSIFCAP:
   2983 		/* Layer-4 Rx checksum offload has to be turned on and
   2984 		 * off as a unit.
   2985 		 */
   2986 		l4csum_en = ifcr->ifcr_capenable & l4csum;
   2987 		if (l4csum_en != l4csum && l4csum_en != 0)
   2988 			return EINVAL;
   2989 		/*FALLTHROUGH*/
   2990 	case SIOCADDMULTI:
   2991 	case SIOCDELMULTI:
   2992 	case SIOCSIFFLAGS:
   2993 	case SIOCSIFMTU:
   2994 	default:
   2995 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
   2996 			return error;
   2997 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   2998 			;
   2999 		else if (command == SIOCSIFCAP || command == SIOCSIFMTU) {
   3000 			IXGBE_CORE_LOCK(adapter);
   3001 			ixv_init_locked(adapter);
   3002 			IXGBE_CORE_UNLOCK(adapter);
   3003 		} else if (command == SIOCADDMULTI || command == SIOCDELMULTI) {
   3004 			/*
   3005 			 * Multicast list has changed; set the hardware filter
   3006 			 * accordingly.
   3007 			 */
   3008 			IXGBE_CORE_LOCK(adapter);
   3009 			ixv_disable_intr(adapter);
   3010 			ixv_set_multi(adapter);
   3011 			ixv_enable_intr(adapter);
   3012 			IXGBE_CORE_UNLOCK(adapter);
   3013 		}
   3014 		return 0;
   3015 	}
   3016 } /* ixv_ioctl */
   3017 
   3018 /************************************************************************
   3019  * ixv_init
   3020  ************************************************************************/
   3021 static int
   3022 ixv_init(struct ifnet *ifp)
   3023 {
   3024 	struct adapter *adapter = ifp->if_softc;
   3025 
   3026 	IXGBE_CORE_LOCK(adapter);
   3027 	ixv_init_locked(adapter);
   3028 	IXGBE_CORE_UNLOCK(adapter);
   3029 
   3030 	return 0;
   3031 } /* ixv_init */
   3032 
   3033 /************************************************************************
   3034  * ixv_handle_que
   3035  ************************************************************************/
   3036 static void
   3037 ixv_handle_que(void *context)
   3038 {
   3039 	struct ix_queue *que = context;
   3040 	struct adapter	*adapter = que->adapter;
   3041 	struct tx_ring	*txr = que->txr;
   3042 	struct ifnet	*ifp = adapter->ifp;
   3043 	bool		more;
   3044 
   3045 	que->handleq.ev_count++;
   3046 
   3047 	if (ifp->if_flags & IFF_RUNNING) {
   3048 		more = ixgbe_rxeof(que);
   3049 		IXGBE_TX_LOCK(txr);
   3050 		more |= ixgbe_txeof(txr);
   3051 		if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX))
   3052 			if (!ixgbe_mq_ring_empty(ifp, txr->txr_interq))
   3053 				ixgbe_mq_start_locked(ifp, txr);
   3054 		/* Only for queue 0 */
   3055 		/* NetBSD still needs this for CBQ */
   3056 		if ((&adapter->queues[0] == que)
   3057 		    && (!ixgbe_legacy_ring_empty(ifp, NULL)))
   3058 			ixgbe_legacy_start_locked(ifp, txr);
   3059 		IXGBE_TX_UNLOCK(txr);
   3060 		if (more) {
   3061 			que->req.ev_count++;
   3062 			if (adapter->txrx_use_workqueue) {
   3063 				/*
   3064 				 * "enqueued flag" is not required here
   3065 				 * the same as ixg(4). See ixgbe_msix_que().
   3066 				 */
   3067 				workqueue_enqueue(adapter->que_wq,
   3068 				    &que->wq_cookie, curcpu());
   3069 			} else
   3070 				  softint_schedule(que->que_si);
   3071 			return;
   3072 		}
   3073 	}
   3074 
   3075 	/* Re-enable this interrupt */
   3076 	ixv_enable_queue(adapter, que->msix);
   3077 
   3078 	return;
   3079 } /* ixv_handle_que */
   3080 
   3081 /************************************************************************
   3082  * ixv_handle_que_work
   3083  ************************************************************************/
   3084 static void
   3085 ixv_handle_que_work(struct work *wk, void *context)
   3086 {
   3087 	struct ix_queue *que = container_of(wk, struct ix_queue, wq_cookie);
   3088 
   3089 	/*
   3090 	 * "enqueued flag" is not required here the same as ixg(4).
   3091 	 * See ixgbe_msix_que().
   3092 	 */
   3093 	ixv_handle_que(que);
   3094 }
   3095 
   3096 /************************************************************************
   3097  * ixv_allocate_msix - Setup MSI-X Interrupt resources and handlers
   3098  ************************************************************************/
   3099 static int
   3100 ixv_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
   3101 {
   3102 	device_t	dev = adapter->dev;
   3103 	struct ix_queue *que = adapter->queues;
   3104 	struct		tx_ring *txr = adapter->tx_rings;
   3105 	int		error, msix_ctrl, rid, vector = 0;
   3106 	pci_chipset_tag_t pc;
   3107 	pcitag_t	tag;
   3108 	char		intrbuf[PCI_INTRSTR_LEN];
   3109 	char		wqname[MAXCOMLEN];
   3110 	char		intr_xname[32];
   3111 	const char	*intrstr = NULL;
   3112 	kcpuset_t	*affinity;
   3113 	int		cpu_id = 0;
   3114 
   3115 	pc = adapter->osdep.pc;
   3116 	tag = adapter->osdep.tag;
   3117 
   3118 	adapter->osdep.nintrs = adapter->num_queues + 1;
   3119 	if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
   3120 	    adapter->osdep.nintrs) != 0) {
   3121 		aprint_error_dev(dev,
   3122 		    "failed to allocate MSI-X interrupt\n");
   3123 		return (ENXIO);
   3124 	}
   3125 
   3126 	kcpuset_create(&affinity, false);
   3127 	for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
   3128 		snprintf(intr_xname, sizeof(intr_xname), "%s TXRX%d",
   3129 		    device_xname(dev), i);
   3130 		intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
   3131 		    sizeof(intrbuf));
   3132 #ifdef IXGBE_MPSAFE
   3133 		pci_intr_setattr(pc, &adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
   3134 		    true);
   3135 #endif
   3136 		/* Set the handler function */
   3137 		que->res = adapter->osdep.ihs[i] = pci_intr_establish_xname(pc,
   3138 		    adapter->osdep.intrs[i], IPL_NET, ixv_msix_que, que,
   3139 		    intr_xname);
   3140 		if (que->res == NULL) {
   3141 			pci_intr_release(pc, adapter->osdep.intrs,
   3142 			    adapter->osdep.nintrs);
   3143 			aprint_error_dev(dev,
   3144 			    "Failed to register QUE handler\n");
   3145 			kcpuset_destroy(affinity);
   3146 			return (ENXIO);
   3147 		}
   3148 		que->msix = vector;
   3149 		adapter->active_queues |= (u64)(1 << que->msix);
   3150 
   3151 		cpu_id = i;
   3152 		/* Round-robin affinity */
   3153 		kcpuset_zero(affinity);
   3154 		kcpuset_set(affinity, cpu_id % ncpu);
   3155 		error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
   3156 		    NULL);
   3157 		aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
   3158 		    intrstr);
   3159 		if (error == 0)
   3160 			aprint_normal(", bound queue %d to cpu %d\n",
   3161 			    i, cpu_id % ncpu);
   3162 		else
   3163 			aprint_normal("\n");
   3164 
   3165 #ifndef IXGBE_LEGACY_TX
   3166 		txr->txr_si
   3167 		    = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   3168 			ixgbe_deferred_mq_start, txr);
   3169 #endif
   3170 		que->que_si
   3171 		    = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   3172 			ixv_handle_que, que);
   3173 		if (que->que_si == NULL) {
   3174 			aprint_error_dev(dev,
   3175 			    "could not establish software interrupt\n");
   3176 		}
   3177 	}
   3178 	snprintf(wqname, sizeof(wqname), "%sdeferTx", device_xname(dev));
   3179 	error = workqueue_create(&adapter->txr_wq, wqname,
   3180 	    ixgbe_deferred_mq_start_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
   3181 	    IXGBE_WORKQUEUE_FLAGS);
   3182 	if (error) {
   3183 		aprint_error_dev(dev, "couldn't create workqueue for deferred Tx\n");
   3184 	}
   3185 	adapter->txr_wq_enqueued = percpu_alloc(sizeof(u_int));
   3186 
   3187 	snprintf(wqname, sizeof(wqname), "%sTxRx", device_xname(dev));
   3188 	error = workqueue_create(&adapter->que_wq, wqname,
   3189 	    ixv_handle_que_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
   3190 	    IXGBE_WORKQUEUE_FLAGS);
   3191 	if (error) {
   3192 		aprint_error_dev(dev,
   3193 		    "couldn't create workqueue\n");
   3194 	}
   3195 
   3196 	/* and Mailbox */
   3197 	cpu_id++;
   3198 	snprintf(intr_xname, sizeof(intr_xname), "%s link", device_xname(dev));
   3199 	adapter->vector = vector;
   3200 	intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
   3201 	    sizeof(intrbuf));
   3202 #ifdef IXGBE_MPSAFE
   3203 	pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
   3204 	    true);
   3205 #endif
   3206 	/* Set the mbx handler function */
   3207 	adapter->osdep.ihs[vector] = pci_intr_establish_xname(pc,
   3208 	    adapter->osdep.intrs[vector], IPL_NET, ixv_msix_mbx, adapter,
   3209 	    intr_xname);
   3210 	if (adapter->osdep.ihs[vector] == NULL) {
   3211 		aprint_error_dev(dev, "Failed to register LINK handler\n");
   3212 		kcpuset_destroy(affinity);
   3213 		return (ENXIO);
   3214 	}
   3215 	/* Round-robin affinity */
   3216 	kcpuset_zero(affinity);
   3217 	kcpuset_set(affinity, cpu_id % ncpu);
   3218 	error = interrupt_distribute(adapter->osdep.ihs[vector],
   3219 	    affinity, NULL);
   3220 
   3221 	aprint_normal_dev(dev,
   3222 	    "for link, interrupting at %s", intrstr);
   3223 	if (error == 0)
   3224 		aprint_normal(", affinity to cpu %d\n", cpu_id % ncpu);
   3225 	else
   3226 		aprint_normal("\n");
   3227 
   3228 	/* Tasklets for Mailbox */
   3229 	adapter->link_si = softint_establish(SOFTINT_NET |IXGBE_SOFTINFT_FLAGS,
   3230 	    ixv_handle_link, adapter);
   3231 	/*
   3232 	 * Due to a broken design QEMU will fail to properly
   3233 	 * enable the guest for MSI-X unless the vectors in
   3234 	 * the table are all set up, so we must rewrite the
   3235 	 * ENABLE in the MSI-X control register again at this
   3236 	 * point to cause it to successfully initialize us.
   3237 	 */
   3238 	if (adapter->hw.mac.type == ixgbe_mac_82599_vf) {
   3239 		pci_get_capability(pc, tag, PCI_CAP_MSIX, &rid, NULL);
   3240 		rid += PCI_MSIX_CTL;
   3241 		msix_ctrl = pci_conf_read(pc, tag, rid);
   3242 		msix_ctrl |= PCI_MSIX_CTL_ENABLE;
   3243 		pci_conf_write(pc, tag, rid, msix_ctrl);
   3244 	}
   3245 
   3246 	kcpuset_destroy(affinity);
   3247 	return (0);
   3248 } /* ixv_allocate_msix */
   3249 
   3250 /************************************************************************
   3251  * ixv_configure_interrupts - Setup MSI-X resources
   3252  *
   3253  *   Note: The VF device MUST use MSI-X, there is no fallback.
   3254  ************************************************************************/
   3255 static int
   3256 ixv_configure_interrupts(struct adapter *adapter)
   3257 {
   3258 	device_t dev = adapter->dev;
   3259 	int want, queues, msgs;
   3260 
   3261 	/* Must have at least 2 MSI-X vectors */
   3262 	msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
   3263 	if (msgs < 2) {
   3264 		aprint_error_dev(dev, "MSIX config error\n");
   3265 		return (ENXIO);
   3266 	}
   3267 	msgs = MIN(msgs, IXG_MAX_NINTR);
   3268 
   3269 	/* Figure out a reasonable auto config value */
   3270 	queues = (ncpu > (msgs - 1)) ? (msgs - 1) : ncpu;
   3271 
   3272 	if (ixv_num_queues != 0)
   3273 		queues = ixv_num_queues;
   3274 	else if ((ixv_num_queues == 0) && (queues > IXGBE_VF_MAX_TX_QUEUES))
   3275 		queues = IXGBE_VF_MAX_TX_QUEUES;
   3276 
   3277 	/*
   3278 	 * Want vectors for the queues,
   3279 	 * plus an additional for mailbox.
   3280 	 */
   3281 	want = queues + 1;
   3282 	if (msgs >= want)
   3283 		msgs = want;
   3284 	else {
   3285 		aprint_error_dev(dev,
   3286 		    "MSI-X Configuration Problem, "
   3287 		    "%d vectors but %d queues wanted!\n",
   3288 		    msgs, want);
   3289 		return -1;
   3290 	}
   3291 
   3292 	adapter->msix_mem = (void *)1; /* XXX */
   3293 	aprint_normal_dev(dev,
   3294 	    "Using MSI-X interrupts with %d vectors\n", msgs);
   3295 	adapter->num_queues = queues;
   3296 
   3297 	return (0);
   3298 } /* ixv_configure_interrupts */
   3299 
   3300 
   3301 /************************************************************************
   3302  * ixv_handle_link - Tasklet handler for MSI-X MBX interrupts
   3303  *
   3304  *   Done outside of interrupt context since the driver might sleep
   3305  ************************************************************************/
   3306 static void
   3307 ixv_handle_link(void *context)
   3308 {
   3309 	struct adapter *adapter = context;
   3310 
   3311 	IXGBE_CORE_LOCK(adapter);
   3312 
   3313 	adapter->hw.mac.ops.check_link(&adapter->hw, &adapter->link_speed,
   3314 	    &adapter->link_up, FALSE);
   3315 	ixv_update_link_status(adapter);
   3316 
   3317 	IXGBE_CORE_UNLOCK(adapter);
   3318 } /* ixv_handle_link */
   3319 
   3320 /************************************************************************
   3321  * ixv_check_link - Used in the local timer to poll for link changes
   3322  ************************************************************************/
   3323 static s32
   3324 ixv_check_link(struct adapter *adapter)
   3325 {
   3326 	s32 error;
   3327 
   3328 	KASSERT(mutex_owned(&adapter->core_mtx));
   3329 
   3330 	adapter->hw.mac.get_link_status = TRUE;
   3331 
   3332 	error = adapter->hw.mac.ops.check_link(&adapter->hw,
   3333 	    &adapter->link_speed, &adapter->link_up, FALSE);
   3334 	ixv_update_link_status(adapter);
   3335 
   3336 	return error;
   3337 } /* ixv_check_link */
   3338