Home | History | Annotate | Line # | Download | only in ixgbe
ixv.c revision 1.134
      1 /*$NetBSD: ixv.c,v 1.134 2019/09/12 12:25:46 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_13,
   1089 				      ixgbe_mbox_api_12,
   1090 				      ixgbe_mbox_api_11,
   1091 				      ixgbe_mbox_api_10,
   1092 				      ixgbe_mbox_api_unknown };
   1093 	int		i = 0;
   1094 
   1095 	while (mbx_api[i] != ixgbe_mbox_api_unknown) {
   1096 		if (ixgbevf_negotiate_api_version(hw, mbx_api[i]) == 0)
   1097 			return (0);
   1098 		i++;
   1099 	}
   1100 
   1101 	return (EINVAL);
   1102 } /* ixv_negotiate_api */
   1103 
   1104 
   1105 /************************************************************************
   1106  * ixv_set_multi - Multicast Update
   1107  *
   1108  *   Called whenever multicast address list is updated.
   1109  ************************************************************************/
   1110 static void
   1111 ixv_set_multi(struct adapter *adapter)
   1112 {
   1113 	struct ixgbe_hw *hw = &adapter->hw;
   1114 	struct ether_multi *enm;
   1115 	struct ether_multistep step;
   1116 	struct ethercom *ec = &adapter->osdep.ec;
   1117 	u8	mta[IXGBE_MAX_VF_MC * IXGBE_ETH_LENGTH_OF_ADDRESS];
   1118 	u8		   *update_ptr;
   1119 	int		   mcnt = 0;
   1120 	bool overflow = false;
   1121 	bool allmulti = false;
   1122 	int error;
   1123 
   1124 	KASSERT(mutex_owned(&adapter->core_mtx));
   1125 	IOCTL_DEBUGOUT("ixv_set_multi: begin");
   1126 
   1127 	ETHER_LOCK(ec);
   1128 	ETHER_FIRST_MULTI(step, ec, enm);
   1129 	while (enm != NULL) {
   1130 		if (mcnt >= IXGBE_MAX_VF_MC) {
   1131 			overflow = true;
   1132 			break;
   1133 		}
   1134 		bcopy(enm->enm_addrlo,
   1135 		    &mta[mcnt * IXGBE_ETH_LENGTH_OF_ADDRESS],
   1136 		    IXGBE_ETH_LENGTH_OF_ADDRESS);
   1137 		mcnt++;
   1138 		ETHER_NEXT_MULTI(step, enm);
   1139 	}
   1140 	ETHER_UNLOCK(ec);
   1141 
   1142 	if (overflow) {
   1143 		error = hw->mac.ops.update_xcast_mode(hw,
   1144 		    IXGBEVF_XCAST_MODE_ALLMULTI);
   1145 		if (error == IXGBE_ERR_NOT_TRUSTED) {
   1146 			device_printf(adapter->dev,
   1147 			    "this interface is not trusted\n");
   1148 			error = ENOSPC;
   1149 		} else if (error) {
   1150 			device_printf(adapter->dev,
   1151 			    "number of Ethernet multicast addresses "
   1152 			    "exceeds the limit (%d). error = %d\n",
   1153 			    IXGBE_MAX_VF_MC, error);
   1154 			error = ENOSPC;
   1155 		} else {
   1156 			allmulti = true;
   1157 			ec->ec_flags |= ETHER_F_ALLMULTI;
   1158 		}
   1159 	}
   1160 
   1161 	if (!allmulti) {
   1162 		error = hw->mac.ops.update_xcast_mode(hw,
   1163 		    IXGBEVF_XCAST_MODE_MULTI);
   1164 		if (error) {
   1165 			device_printf(adapter->dev,
   1166 			    "failed to set Ethernet multicast address "
   1167 			    "operation to normal. error = %d\n", error);
   1168 		}
   1169 		ec->ec_flags &= ~ETHER_F_ALLMULTI;
   1170 	}
   1171 
   1172 	update_ptr = mta;
   1173 
   1174 	adapter->hw.mac.ops.update_mc_addr_list(&adapter->hw, update_ptr, mcnt,
   1175 	    ixv_mc_array_itr, TRUE);
   1176 } /* ixv_set_multi */
   1177 
   1178 /************************************************************************
   1179  * ixv_mc_array_itr
   1180  *
   1181  *   An iterator function needed by the multicast shared code.
   1182  *   It feeds the shared code routine the addresses in the
   1183  *   array of ixv_set_multi() one by one.
   1184  ************************************************************************/
   1185 static u8 *
   1186 ixv_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
   1187 {
   1188 	u8 *addr = *update_ptr;
   1189 	u8 *newptr;
   1190 
   1191 	*vmdq = 0;
   1192 
   1193 	newptr = addr + IXGBE_ETH_LENGTH_OF_ADDRESS;
   1194 	*update_ptr = newptr;
   1195 
   1196 	return addr;
   1197 } /* ixv_mc_array_itr */
   1198 
   1199 /************************************************************************
   1200  * ixv_local_timer - Timer routine
   1201  *
   1202  *   Checks for link status, updates statistics,
   1203  *   and runs the watchdog check.
   1204  ************************************************************************/
   1205 static void
   1206 ixv_local_timer(void *arg)
   1207 {
   1208 	struct adapter *adapter = arg;
   1209 
   1210 	IXGBE_CORE_LOCK(adapter);
   1211 	ixv_local_timer_locked(adapter);
   1212 	IXGBE_CORE_UNLOCK(adapter);
   1213 }
   1214 
   1215 static void
   1216 ixv_local_timer_locked(void *arg)
   1217 {
   1218 	struct adapter	*adapter = arg;
   1219 	device_t	dev = adapter->dev;
   1220 	struct ix_queue	*que = adapter->queues;
   1221 	u64		queues = 0;
   1222 	u64		v0, v1, v2, v3, v4, v5, v6, v7;
   1223 	int		hung = 0;
   1224 	int		i;
   1225 
   1226 	KASSERT(mutex_owned(&adapter->core_mtx));
   1227 
   1228 	if (ixv_check_link(adapter)) {
   1229 		ixv_init_locked(adapter);
   1230 		return;
   1231 	}
   1232 
   1233 	/* Stats Update */
   1234 	ixv_update_stats(adapter);
   1235 
   1236 	/* Update some event counters */
   1237 	v0 = v1 = v2 = v3 = v4 = v5 = v6 = v7 = 0;
   1238 	que = adapter->queues;
   1239 	for (i = 0; i < adapter->num_queues; i++, que++) {
   1240 		struct tx_ring	*txr = que->txr;
   1241 
   1242 		v0 += txr->q_efbig_tx_dma_setup;
   1243 		v1 += txr->q_mbuf_defrag_failed;
   1244 		v2 += txr->q_efbig2_tx_dma_setup;
   1245 		v3 += txr->q_einval_tx_dma_setup;
   1246 		v4 += txr->q_other_tx_dma_setup;
   1247 		v5 += txr->q_eagain_tx_dma_setup;
   1248 		v6 += txr->q_enomem_tx_dma_setup;
   1249 		v7 += txr->q_tso_err;
   1250 	}
   1251 	adapter->efbig_tx_dma_setup.ev_count = v0;
   1252 	adapter->mbuf_defrag_failed.ev_count = v1;
   1253 	adapter->efbig2_tx_dma_setup.ev_count = v2;
   1254 	adapter->einval_tx_dma_setup.ev_count = v3;
   1255 	adapter->other_tx_dma_setup.ev_count = v4;
   1256 	adapter->eagain_tx_dma_setup.ev_count = v5;
   1257 	adapter->enomem_tx_dma_setup.ev_count = v6;
   1258 	adapter->tso_err.ev_count = v7;
   1259 
   1260 	/*
   1261 	 * Check the TX queues status
   1262 	 *	- mark hung queues so we don't schedule on them
   1263 	 *	- watchdog only if all queues show hung
   1264 	 */
   1265 	que = adapter->queues;
   1266 	for (i = 0; i < adapter->num_queues; i++, que++) {
   1267 		/* Keep track of queues with work for soft irq */
   1268 		if (que->txr->busy)
   1269 			queues |= ((u64)1 << que->me);
   1270 		/*
   1271 		 * Each time txeof runs without cleaning, but there
   1272 		 * are uncleaned descriptors it increments busy. If
   1273 		 * we get to the MAX we declare it hung.
   1274 		 */
   1275 		if (que->busy == IXGBE_QUEUE_HUNG) {
   1276 			++hung;
   1277 			/* Mark the queue as inactive */
   1278 			adapter->active_queues &= ~((u64)1 << que->me);
   1279 			continue;
   1280 		} else {
   1281 			/* Check if we've come back from hung */
   1282 			if ((adapter->active_queues & ((u64)1 << que->me)) == 0)
   1283 				adapter->active_queues |= ((u64)1 << que->me);
   1284 		}
   1285 		if (que->busy >= IXGBE_MAX_TX_BUSY) {
   1286 			device_printf(dev,
   1287 			    "Warning queue %d appears to be hung!\n", i);
   1288 			que->txr->busy = IXGBE_QUEUE_HUNG;
   1289 			++hung;
   1290 		}
   1291 	}
   1292 
   1293 	/* Only truly watchdog if all queues show hung */
   1294 	if (hung == adapter->num_queues)
   1295 		goto watchdog;
   1296 #if 0
   1297 	else if (queues != 0) { /* Force an IRQ on queues with work */
   1298 		ixv_rearm_queues(adapter, queues);
   1299 	}
   1300 #endif
   1301 
   1302 	callout_reset(&adapter->timer, hz, ixv_local_timer, adapter);
   1303 
   1304 	return;
   1305 
   1306 watchdog:
   1307 
   1308 	device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
   1309 	adapter->ifp->if_flags &= ~IFF_RUNNING;
   1310 	adapter->watchdog_events.ev_count++;
   1311 	ixv_init_locked(adapter);
   1312 } /* ixv_local_timer */
   1313 
   1314 /************************************************************************
   1315  * ixv_update_link_status - Update OS on link state
   1316  *
   1317  * Note: Only updates the OS on the cached link state.
   1318  *	 The real check of the hardware only happens with
   1319  *	 a link interrupt.
   1320  ************************************************************************/
   1321 static void
   1322 ixv_update_link_status(struct adapter *adapter)
   1323 {
   1324 	struct ifnet *ifp = adapter->ifp;
   1325 	device_t     dev = adapter->dev;
   1326 
   1327 	KASSERT(mutex_owned(&adapter->core_mtx));
   1328 
   1329 	if (adapter->link_up) {
   1330 		if (adapter->link_active != LINK_STATE_UP) {
   1331 			if (bootverbose) {
   1332 				const char *bpsmsg;
   1333 
   1334 				switch (adapter->link_speed) {
   1335 				case IXGBE_LINK_SPEED_10GB_FULL:
   1336 					bpsmsg = "10 Gbps";
   1337 					break;
   1338 				case IXGBE_LINK_SPEED_5GB_FULL:
   1339 					bpsmsg = "5 Gbps";
   1340 					break;
   1341 				case IXGBE_LINK_SPEED_2_5GB_FULL:
   1342 					bpsmsg = "2.5 Gbps";
   1343 					break;
   1344 				case IXGBE_LINK_SPEED_1GB_FULL:
   1345 					bpsmsg = "1 Gbps";
   1346 					break;
   1347 				case IXGBE_LINK_SPEED_100_FULL:
   1348 					bpsmsg = "100 Mbps";
   1349 					break;
   1350 				case IXGBE_LINK_SPEED_10_FULL:
   1351 					bpsmsg = "10 Mbps";
   1352 					break;
   1353 				default:
   1354 					bpsmsg = "unknown speed";
   1355 					break;
   1356 				}
   1357 				device_printf(dev, "Link is up %s %s \n",
   1358 				    bpsmsg, "Full Duplex");
   1359 			}
   1360 			adapter->link_active = LINK_STATE_UP;
   1361 			if_link_state_change(ifp, LINK_STATE_UP);
   1362 		}
   1363 	} else {
   1364 		/*
   1365 		 * Do it when link active changes to DOWN. i.e.
   1366 		 * a) LINK_STATE_UNKNOWN -> LINK_STATE_DOWN
   1367 		 * b) LINK_STATE_UP	 -> LINK_STATE_DOWN
   1368 		 */
   1369 		if (adapter->link_active != LINK_STATE_DOWN) {
   1370 			if (bootverbose)
   1371 				device_printf(dev, "Link is Down\n");
   1372 			if_link_state_change(ifp, LINK_STATE_DOWN);
   1373 			adapter->link_active = LINK_STATE_DOWN;
   1374 		}
   1375 	}
   1376 } /* ixv_update_link_status */
   1377 
   1378 
   1379 /************************************************************************
   1380  * ixv_stop - Stop the hardware
   1381  *
   1382  *   Disables all traffic on the adapter by issuing a
   1383  *   global reset on the MAC and deallocates TX/RX buffers.
   1384  ************************************************************************/
   1385 static void
   1386 ixv_ifstop(struct ifnet *ifp, int disable)
   1387 {
   1388 	struct adapter *adapter = ifp->if_softc;
   1389 
   1390 	IXGBE_CORE_LOCK(adapter);
   1391 	ixv_stop(adapter);
   1392 	IXGBE_CORE_UNLOCK(adapter);
   1393 }
   1394 
   1395 static void
   1396 ixv_stop(void *arg)
   1397 {
   1398 	struct ifnet	*ifp;
   1399 	struct adapter	*adapter = arg;
   1400 	struct ixgbe_hw *hw = &adapter->hw;
   1401 
   1402 	ifp = adapter->ifp;
   1403 
   1404 	KASSERT(mutex_owned(&adapter->core_mtx));
   1405 
   1406 	INIT_DEBUGOUT("ixv_stop: begin\n");
   1407 	ixv_disable_intr(adapter);
   1408 
   1409 	/* Tell the stack that the interface is no longer active */
   1410 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1411 
   1412 	hw->mac.ops.reset_hw(hw);
   1413 	adapter->hw.adapter_stopped = FALSE;
   1414 	hw->mac.ops.stop_adapter(hw);
   1415 	callout_stop(&adapter->timer);
   1416 
   1417 	/* reprogram the RAR[0] in case user changed it. */
   1418 	hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
   1419 
   1420 	return;
   1421 } /* ixv_stop */
   1422 
   1423 
   1424 /************************************************************************
   1425  * ixv_allocate_pci_resources
   1426  ************************************************************************/
   1427 static int
   1428 ixv_allocate_pci_resources(struct adapter *adapter,
   1429     const struct pci_attach_args *pa)
   1430 {
   1431 	pcireg_t	memtype, csr;
   1432 	device_t	dev = adapter->dev;
   1433 	bus_addr_t addr;
   1434 	int flags;
   1435 
   1436 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
   1437 	switch (memtype) {
   1438 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   1439 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   1440 		adapter->osdep.mem_bus_space_tag = pa->pa_memt;
   1441 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
   1442 		      memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
   1443 			goto map_err;
   1444 		if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
   1445 			aprint_normal_dev(dev, "clearing prefetchable bit\n");
   1446 			flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
   1447 		}
   1448 		if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
   1449 		     adapter->osdep.mem_size, flags,
   1450 		     &adapter->osdep.mem_bus_space_handle) != 0) {
   1451 map_err:
   1452 			adapter->osdep.mem_size = 0;
   1453 			aprint_error_dev(dev, "unable to map BAR0\n");
   1454 			return ENXIO;
   1455 		}
   1456 		/*
   1457 		 * Enable address decoding for memory range in case it's not
   1458 		 * set.
   1459 		 */
   1460 		csr = pci_conf_read(pa->pa_pc, pa->pa_tag,
   1461 		    PCI_COMMAND_STATUS_REG);
   1462 		csr |= PCI_COMMAND_MEM_ENABLE;
   1463 		pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
   1464 		    csr);
   1465 		break;
   1466 	default:
   1467 		aprint_error_dev(dev, "unexpected type on BAR0\n");
   1468 		return ENXIO;
   1469 	}
   1470 
   1471 	/* Pick up the tuneable queues */
   1472 	adapter->num_queues = ixv_num_queues;
   1473 
   1474 	return (0);
   1475 } /* ixv_allocate_pci_resources */
   1476 
   1477 /************************************************************************
   1478  * ixv_free_pci_resources
   1479  ************************************************************************/
   1480 static void
   1481 ixv_free_pci_resources(struct adapter * adapter)
   1482 {
   1483 	struct		ix_queue *que = adapter->queues;
   1484 	int		rid;
   1485 
   1486 	/*
   1487 	 *  Release all msix queue resources:
   1488 	 */
   1489 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   1490 		if (que->res != NULL)
   1491 			pci_intr_disestablish(adapter->osdep.pc,
   1492 			    adapter->osdep.ihs[i]);
   1493 	}
   1494 
   1495 
   1496 	/* Clean the Mailbox interrupt last */
   1497 	rid = adapter->vector;
   1498 
   1499 	if (adapter->osdep.ihs[rid] != NULL) {
   1500 		pci_intr_disestablish(adapter->osdep.pc,
   1501 		    adapter->osdep.ihs[rid]);
   1502 		adapter->osdep.ihs[rid] = NULL;
   1503 	}
   1504 
   1505 	pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
   1506 	    adapter->osdep.nintrs);
   1507 
   1508 	if (adapter->osdep.mem_size != 0) {
   1509 		bus_space_unmap(adapter->osdep.mem_bus_space_tag,
   1510 		    adapter->osdep.mem_bus_space_handle,
   1511 		    adapter->osdep.mem_size);
   1512 	}
   1513 
   1514 	return;
   1515 } /* ixv_free_pci_resources */
   1516 
   1517 /************************************************************************
   1518  * ixv_setup_interface
   1519  *
   1520  *   Setup networking device structure and register an interface.
   1521  ************************************************************************/
   1522 static int
   1523 ixv_setup_interface(device_t dev, struct adapter *adapter)
   1524 {
   1525 	struct ethercom *ec = &adapter->osdep.ec;
   1526 	struct ifnet   *ifp;
   1527 	int rv;
   1528 
   1529 	INIT_DEBUGOUT("ixv_setup_interface: begin");
   1530 
   1531 	ifp = adapter->ifp = &ec->ec_if;
   1532 	strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
   1533 	ifp->if_baudrate = IF_Gbps(10);
   1534 	ifp->if_init = ixv_init;
   1535 	ifp->if_stop = ixv_ifstop;
   1536 	ifp->if_softc = adapter;
   1537 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1538 #ifdef IXGBE_MPSAFE
   1539 	ifp->if_extflags = IFEF_MPSAFE;
   1540 #endif
   1541 	ifp->if_ioctl = ixv_ioctl;
   1542 	if (adapter->feat_en & IXGBE_FEATURE_LEGACY_TX) {
   1543 #if 0
   1544 		ixv_start_locked = ixgbe_legacy_start_locked;
   1545 #endif
   1546 	} else {
   1547 		ifp->if_transmit = ixgbe_mq_start;
   1548 #if 0
   1549 		ixv_start_locked = ixgbe_mq_start_locked;
   1550 #endif
   1551 	}
   1552 	ifp->if_start = ixgbe_legacy_start;
   1553 	IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
   1554 	IFQ_SET_READY(&ifp->if_snd);
   1555 
   1556 	rv = if_initialize(ifp);
   1557 	if (rv != 0) {
   1558 		aprint_error_dev(dev, "if_initialize failed(%d)\n", rv);
   1559 		return rv;
   1560 	}
   1561 	adapter->ipq = if_percpuq_create(&adapter->osdep.ec.ec_if);
   1562 	ether_ifattach(ifp, adapter->hw.mac.addr);
   1563 	/*
   1564 	 * We use per TX queue softint, so if_deferred_start_init() isn't
   1565 	 * used.
   1566 	 */
   1567 	ether_set_ifflags_cb(ec, ixv_ifflags_cb);
   1568 
   1569 	adapter->max_frame_size = ifp->if_mtu + IXGBE_MTU_HDR;
   1570 
   1571 	/*
   1572 	 * Tell the upper layer(s) we support long frames.
   1573 	 */
   1574 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
   1575 
   1576 	/* Set capability flags */
   1577 	ifp->if_capabilities |= IFCAP_HWCSUM
   1578 			     |	IFCAP_TSOv4
   1579 			     |	IFCAP_TSOv6;
   1580 	ifp->if_capenable = 0;
   1581 
   1582 	ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER
   1583 			    |  ETHERCAP_VLAN_HWTAGGING
   1584 			    |  ETHERCAP_VLAN_HWCSUM
   1585 			    |  ETHERCAP_JUMBO_MTU
   1586 			    |  ETHERCAP_VLAN_MTU;
   1587 
   1588 	/* Enable the above capabilities by default */
   1589 	ec->ec_capenable = ec->ec_capabilities;
   1590 
   1591 	/* Don't enable LRO by default */
   1592 #if 0
   1593 	/* NetBSD doesn't support LRO yet */
   1594 	ifp->if_capabilities |= IFCAP_LRO;
   1595 #endif
   1596 
   1597 	/*
   1598 	 * Specify the media types supported by this adapter and register
   1599 	 * callbacks to update media and link information
   1600 	 */
   1601 	ec->ec_ifmedia = &adapter->media;
   1602 	ifmedia_init(&adapter->media, IFM_IMASK, ixv_media_change,
   1603 	    ixv_media_status);
   1604 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
   1605 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   1606 
   1607 	if_register(ifp);
   1608 
   1609 	return 0;
   1610 } /* ixv_setup_interface */
   1611 
   1612 
   1613 /************************************************************************
   1614  * ixv_initialize_transmit_units - Enable transmit unit.
   1615  ************************************************************************/
   1616 static void
   1617 ixv_initialize_transmit_units(struct adapter *adapter)
   1618 {
   1619 	struct tx_ring	*txr = adapter->tx_rings;
   1620 	struct ixgbe_hw	*hw = &adapter->hw;
   1621 	int i;
   1622 
   1623 	for (i = 0; i < adapter->num_queues; i++, txr++) {
   1624 		u64 tdba = txr->txdma.dma_paddr;
   1625 		u32 txctrl, txdctl;
   1626 		int j = txr->me;
   1627 
   1628 		/* Set WTHRESH to 8, burst writeback */
   1629 		txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
   1630 		txdctl |= (8 << 16);
   1631 		IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
   1632 
   1633 		/* Set the HW Tx Head and Tail indices */
   1634 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_VFTDH(j), 0);
   1635 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_VFTDT(j), 0);
   1636 
   1637 		/* Set Tx Tail register */
   1638 		txr->tail = IXGBE_VFTDT(j);
   1639 
   1640 		txr->txr_no_space = false;
   1641 
   1642 		/* Set Ring parameters */
   1643 		IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
   1644 		    (tdba & 0x00000000ffffffffULL));
   1645 		IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
   1646 		IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j),
   1647 		    adapter->num_tx_desc * sizeof(struct ixgbe_legacy_tx_desc));
   1648 		txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
   1649 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
   1650 		IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
   1651 
   1652 		/* Now enable */
   1653 		txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
   1654 		txdctl |= IXGBE_TXDCTL_ENABLE;
   1655 		IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
   1656 	}
   1657 
   1658 	return;
   1659 } /* ixv_initialize_transmit_units */
   1660 
   1661 
   1662 /************************************************************************
   1663  * ixv_initialize_rss_mapping
   1664  ************************************************************************/
   1665 static void
   1666 ixv_initialize_rss_mapping(struct adapter *adapter)
   1667 {
   1668 	struct ixgbe_hw *hw = &adapter->hw;
   1669 	u32		reta = 0, mrqc, rss_key[10];
   1670 	int		queue_id;
   1671 	int		i, j;
   1672 	u32		rss_hash_config;
   1673 
   1674 	/* force use default RSS key. */
   1675 #ifdef __NetBSD__
   1676 	rss_getkey((uint8_t *) &rss_key);
   1677 #else
   1678 	if (adapter->feat_en & IXGBE_FEATURE_RSS) {
   1679 		/* Fetch the configured RSS key */
   1680 		rss_getkey((uint8_t *)&rss_key);
   1681 	} else {
   1682 		/* set up random bits */
   1683 		cprng_fast(&rss_key, sizeof(rss_key));
   1684 	}
   1685 #endif
   1686 
   1687 	/* Now fill out hash function seeds */
   1688 	for (i = 0; i < 10; i++)
   1689 		IXGBE_WRITE_REG(hw, IXGBE_VFRSSRK(i), rss_key[i]);
   1690 
   1691 	/* Set up the redirection table */
   1692 	for (i = 0, j = 0; i < 64; i++, j++) {
   1693 		if (j == adapter->num_queues)
   1694 			j = 0;
   1695 
   1696 		if (adapter->feat_en & IXGBE_FEATURE_RSS) {
   1697 			/*
   1698 			 * Fetch the RSS bucket id for the given indirection
   1699 			 * entry. Cap it at the number of configured buckets
   1700 			 * (which is num_queues.)
   1701 			 */
   1702 			queue_id = rss_get_indirection_to_bucket(i);
   1703 			queue_id = queue_id % adapter->num_queues;
   1704 		} else
   1705 			queue_id = j;
   1706 
   1707 		/*
   1708 		 * The low 8 bits are for hash value (n+0);
   1709 		 * The next 8 bits are for hash value (n+1), etc.
   1710 		 */
   1711 		reta >>= 8;
   1712 		reta |= ((uint32_t)queue_id) << 24;
   1713 		if ((i & 3) == 3) {
   1714 			IXGBE_WRITE_REG(hw, IXGBE_VFRETA(i >> 2), reta);
   1715 			reta = 0;
   1716 		}
   1717 	}
   1718 
   1719 	/* Perform hash on these packet types */
   1720 	if (adapter->feat_en & IXGBE_FEATURE_RSS)
   1721 		rss_hash_config = rss_gethashconfig();
   1722 	else {
   1723 		/*
   1724 		 * Disable UDP - IP fragments aren't currently being handled
   1725 		 * and so we end up with a mix of 2-tuple and 4-tuple
   1726 		 * traffic.
   1727 		 */
   1728 		rss_hash_config = RSS_HASHTYPE_RSS_IPV4
   1729 				| RSS_HASHTYPE_RSS_TCP_IPV4
   1730 				| RSS_HASHTYPE_RSS_IPV6
   1731 				| RSS_HASHTYPE_RSS_TCP_IPV6;
   1732 	}
   1733 
   1734 	mrqc = IXGBE_MRQC_RSSEN;
   1735 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
   1736 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
   1737 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
   1738 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
   1739 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
   1740 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
   1741 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
   1742 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
   1743 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
   1744 		device_printf(adapter->dev, "%s: RSS_HASHTYPE_RSS_IPV6_EX defined, but not supported\n",
   1745 		    __func__);
   1746 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
   1747 		device_printf(adapter->dev, "%s: RSS_HASHTYPE_RSS_TCP_IPV6_EX defined, but not supported\n",
   1748 		    __func__);
   1749 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
   1750 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
   1751 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
   1752 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
   1753 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
   1754 		device_printf(adapter->dev, "%s: RSS_HASHTYPE_RSS_UDP_IPV6_EX defined, but not supported\n",
   1755 		    __func__);
   1756 	IXGBE_WRITE_REG(hw, IXGBE_VFMRQC, mrqc);
   1757 } /* ixv_initialize_rss_mapping */
   1758 
   1759 
   1760 /************************************************************************
   1761  * ixv_initialize_receive_units - Setup receive registers and features.
   1762  ************************************************************************/
   1763 static void
   1764 ixv_initialize_receive_units(struct adapter *adapter)
   1765 {
   1766 	struct	rx_ring	*rxr = adapter->rx_rings;
   1767 	struct ixgbe_hw	*hw = &adapter->hw;
   1768 	struct ifnet	*ifp = adapter->ifp;
   1769 	u32		bufsz, psrtype;
   1770 
   1771 	if (ifp->if_mtu > ETHERMTU)
   1772 		bufsz = 4096 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
   1773 	else
   1774 		bufsz = 2048 >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
   1775 
   1776 	psrtype = IXGBE_PSRTYPE_TCPHDR
   1777 		| IXGBE_PSRTYPE_UDPHDR
   1778 		| IXGBE_PSRTYPE_IPV4HDR
   1779 		| IXGBE_PSRTYPE_IPV6HDR
   1780 		| IXGBE_PSRTYPE_L2HDR;
   1781 
   1782 	if (adapter->num_queues > 1)
   1783 		psrtype |= 1 << 29;
   1784 
   1785 	IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, psrtype);
   1786 
   1787 	/* Tell PF our max_frame size */
   1788 	if (ixgbevf_rlpml_set_vf(hw, adapter->max_frame_size) != 0) {
   1789 		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");
   1790 	}
   1791 
   1792 	for (int i = 0; i < adapter->num_queues; i++, rxr++) {
   1793 		u64 rdba = rxr->rxdma.dma_paddr;
   1794 		u32 reg, rxdctl;
   1795 		int j = rxr->me;
   1796 
   1797 		/* Disable the queue */
   1798 		rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
   1799 		rxdctl &= ~IXGBE_RXDCTL_ENABLE;
   1800 		IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
   1801 		for (int k = 0; k < 10; k++) {
   1802 			if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) &
   1803 			    IXGBE_RXDCTL_ENABLE)
   1804 				msec_delay(1);
   1805 			else
   1806 				break;
   1807 		}
   1808 		wmb();
   1809 		/* Setup the Base and Length of the Rx Descriptor Ring */
   1810 		IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
   1811 		    (rdba & 0x00000000ffffffffULL));
   1812 		IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
   1813 		IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j),
   1814 		    adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
   1815 
   1816 		/* Reset the ring indices */
   1817 		IXGBE_WRITE_REG(hw, IXGBE_VFRDH(rxr->me), 0);
   1818 		IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me), 0);
   1819 
   1820 		/* Set up the SRRCTL register */
   1821 		reg = IXGBE_READ_REG(hw, IXGBE_VFSRRCTL(j));
   1822 		reg &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
   1823 		reg &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
   1824 		reg |= bufsz;
   1825 		reg |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
   1826 		IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(j), reg);
   1827 
   1828 		/* Capture Rx Tail index */
   1829 		rxr->tail = IXGBE_VFRDT(rxr->me);
   1830 
   1831 		/* Do the queue enabling last */
   1832 		rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
   1833 		IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
   1834 		for (int k = 0; k < 10; k++) {
   1835 			if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) &
   1836 			    IXGBE_RXDCTL_ENABLE)
   1837 				break;
   1838 			msec_delay(1);
   1839 		}
   1840 		wmb();
   1841 
   1842 		/* Set the Tail Pointer */
   1843 #ifdef DEV_NETMAP
   1844 		/*
   1845 		 * In netmap mode, we must preserve the buffers made
   1846 		 * available to userspace before the if_init()
   1847 		 * (this is true by default on the TX side, because
   1848 		 * init makes all buffers available to userspace).
   1849 		 *
   1850 		 * netmap_reset() and the device specific routines
   1851 		 * (e.g. ixgbe_setup_receive_rings()) map these
   1852 		 * buffers at the end of the NIC ring, so here we
   1853 		 * must set the RDT (tail) register to make sure
   1854 		 * they are not overwritten.
   1855 		 *
   1856 		 * In this driver the NIC ring starts at RDH = 0,
   1857 		 * RDT points to the last slot available for reception (?),
   1858 		 * so RDT = num_rx_desc - 1 means the whole ring is available.
   1859 		 */
   1860 		if ((adapter->feat_en & IXGBE_FEATURE_NETMAP) &&
   1861 		    (ifp->if_capenable & IFCAP_NETMAP)) {
   1862 			struct netmap_adapter *na = NA(adapter->ifp);
   1863 			struct netmap_kring *kring = na->rx_rings[i];
   1864 			int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
   1865 
   1866 			IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me), t);
   1867 		} else
   1868 #endif /* DEV_NETMAP */
   1869 			IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rxr->me),
   1870 			    adapter->num_rx_desc - 1);
   1871 	}
   1872 
   1873 	ixv_initialize_rss_mapping(adapter);
   1874 } /* ixv_initialize_receive_units */
   1875 
   1876 /************************************************************************
   1877  * ixv_sysctl_tdh_handler - Transmit Descriptor Head handler function
   1878  *
   1879  *   Retrieves the TDH value from the hardware
   1880  ************************************************************************/
   1881 static int
   1882 ixv_sysctl_tdh_handler(SYSCTLFN_ARGS)
   1883 {
   1884 	struct sysctlnode node = *rnode;
   1885 	struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
   1886 	uint32_t val;
   1887 
   1888 	if (!txr)
   1889 		return (0);
   1890 
   1891 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_VFTDH(txr->me));
   1892 	node.sysctl_data = &val;
   1893 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1894 } /* ixv_sysctl_tdh_handler */
   1895 
   1896 /************************************************************************
   1897  * ixgbe_sysctl_tdt_handler - Transmit Descriptor Tail handler function
   1898  *
   1899  *   Retrieves the TDT value from the hardware
   1900  ************************************************************************/
   1901 static int
   1902 ixv_sysctl_tdt_handler(SYSCTLFN_ARGS)
   1903 {
   1904 	struct sysctlnode node = *rnode;
   1905 	struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
   1906 	uint32_t val;
   1907 
   1908 	if (!txr)
   1909 		return (0);
   1910 
   1911 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_VFTDT(txr->me));
   1912 	node.sysctl_data = &val;
   1913 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1914 } /* ixv_sysctl_tdt_handler */
   1915 
   1916 /************************************************************************
   1917  * ixv_sysctl_next_to_check_handler - Receive Descriptor next to check
   1918  * handler function
   1919  *
   1920  *   Retrieves the next_to_check value
   1921  ************************************************************************/
   1922 static int
   1923 ixv_sysctl_next_to_check_handler(SYSCTLFN_ARGS)
   1924 {
   1925 	struct sysctlnode node = *rnode;
   1926 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   1927 	uint32_t val;
   1928 
   1929 	if (!rxr)
   1930 		return (0);
   1931 
   1932 	val = rxr->next_to_check;
   1933 	node.sysctl_data = &val;
   1934 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1935 } /* ixv_sysctl_next_to_check_handler */
   1936 
   1937 /************************************************************************
   1938  * ixv_sysctl_rdh_handler - Receive Descriptor Head handler function
   1939  *
   1940  *   Retrieves the RDH value from the hardware
   1941  ************************************************************************/
   1942 static int
   1943 ixv_sysctl_rdh_handler(SYSCTLFN_ARGS)
   1944 {
   1945 	struct sysctlnode node = *rnode;
   1946 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   1947 	uint32_t val;
   1948 
   1949 	if (!rxr)
   1950 		return (0);
   1951 
   1952 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_VFRDH(rxr->me));
   1953 	node.sysctl_data = &val;
   1954 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1955 } /* ixv_sysctl_rdh_handler */
   1956 
   1957 /************************************************************************
   1958  * ixv_sysctl_rdt_handler - Receive Descriptor Tail handler function
   1959  *
   1960  *   Retrieves the RDT value from the hardware
   1961  ************************************************************************/
   1962 static int
   1963 ixv_sysctl_rdt_handler(SYSCTLFN_ARGS)
   1964 {
   1965 	struct sysctlnode node = *rnode;
   1966 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   1967 	uint32_t val;
   1968 
   1969 	if (!rxr)
   1970 		return (0);
   1971 
   1972 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_VFRDT(rxr->me));
   1973 	node.sysctl_data = &val;
   1974 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1975 } /* ixv_sysctl_rdt_handler */
   1976 
   1977 static void
   1978 ixv_setup_vlan_tagging(struct adapter *adapter)
   1979 {
   1980 	struct ethercom *ec = &adapter->osdep.ec;
   1981 	struct ixgbe_hw *hw = &adapter->hw;
   1982 	struct rx_ring	*rxr;
   1983 	u32		ctrl;
   1984 	int		i;
   1985 	bool		hwtagging;
   1986 
   1987 	/* Enable HW tagging only if any vlan is attached */
   1988 	hwtagging = (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING)
   1989 	    && VLAN_ATTACHED(ec);
   1990 
   1991 	/* Enable the queues */
   1992 	for (i = 0; i < adapter->num_queues; i++) {
   1993 		rxr = &adapter->rx_rings[i];
   1994 		ctrl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(rxr->me));
   1995 		if (hwtagging)
   1996 			ctrl |= IXGBE_RXDCTL_VME;
   1997 		else
   1998 			ctrl &= ~IXGBE_RXDCTL_VME;
   1999 		IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(rxr->me), ctrl);
   2000 		/*
   2001 		 * Let Rx path know that it needs to store VLAN tag
   2002 		 * as part of extra mbuf info.
   2003 		 */
   2004 		rxr->vtag_strip = hwtagging ? TRUE : FALSE;
   2005 	}
   2006 } /* ixv_setup_vlan_tagging */
   2007 
   2008 /************************************************************************
   2009  * ixv_setup_vlan_support
   2010  ************************************************************************/
   2011 static int
   2012 ixv_setup_vlan_support(struct adapter *adapter)
   2013 {
   2014 	struct ethercom *ec = &adapter->osdep.ec;
   2015 	struct ixgbe_hw *hw = &adapter->hw;
   2016 	u32		vid, vfta, retry;
   2017 	struct vlanid_list *vlanidp;
   2018 	int rv, error = 0;
   2019 
   2020 	/*
   2021 	 *  This function is called from both if_init and ifflags_cb()
   2022 	 * on NetBSD.
   2023 	 */
   2024 
   2025 	/*
   2026 	 * Part 1:
   2027 	 * Setup VLAN HW tagging
   2028 	 */
   2029 	ixv_setup_vlan_tagging(adapter);
   2030 
   2031 	if (!VLAN_ATTACHED(ec))
   2032 		return 0;
   2033 
   2034 	/*
   2035 	 * Part 2:
   2036 	 * Setup VLAN HW filter
   2037 	 */
   2038 	/* Cleanup shadow_vfta */
   2039 	for (int i = 0; i < IXGBE_VFTA_SIZE; i++)
   2040 		adapter->shadow_vfta[i] = 0;
   2041 	/* Generate shadow_vfta from ec_vids */
   2042 	ETHER_LOCK(ec);
   2043 	SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
   2044 		uint32_t idx;
   2045 
   2046 		idx = vlanidp->vid / 32;
   2047 		KASSERT(idx < IXGBE_VFTA_SIZE);
   2048 		adapter->shadow_vfta[idx] |= (u32)1 << (vlanidp->vid % 32);
   2049 	}
   2050 	ETHER_UNLOCK(ec);
   2051 
   2052 	/*
   2053 	 * A soft reset zero's out the VFTA, so
   2054 	 * we need to repopulate it now.
   2055 	 */
   2056 	for (int i = 0; i < IXGBE_VFTA_SIZE; i++) {
   2057 		if (adapter->shadow_vfta[i] == 0)
   2058 			continue;
   2059 		vfta = adapter->shadow_vfta[i];
   2060 		/*
   2061 		 * Reconstruct the vlan id's
   2062 		 * based on the bits set in each
   2063 		 * of the array ints.
   2064 		 */
   2065 		for (int j = 0; j < 32; j++) {
   2066 			retry = 0;
   2067 			if ((vfta & ((u32)1 << j)) == 0)
   2068 				continue;
   2069 			vid = (i * 32) + j;
   2070 
   2071 			/* Call the shared code mailbox routine */
   2072 			while ((rv = hw->mac.ops.set_vfta(hw, vid, 0, TRUE,
   2073 			    FALSE)) != 0) {
   2074 				if (++retry > 5) {
   2075 					device_printf(adapter->dev,
   2076 					    "%s: max retry exceeded\n",
   2077 						__func__);
   2078 					break;
   2079 				}
   2080 			}
   2081 			if (rv != 0) {
   2082 				device_printf(adapter->dev,
   2083 				    "failed to set vlan %d\n", vid);
   2084 				error = EACCES;
   2085 			}
   2086 		}
   2087 	}
   2088 	return error;
   2089 } /* ixv_setup_vlan_support */
   2090 
   2091 static int
   2092 ixv_vlan_cb(struct ethercom *ec, uint16_t vid, bool set)
   2093 {
   2094 	struct ifnet *ifp = &ec->ec_if;
   2095 	struct adapter *adapter = ifp->if_softc;
   2096 	int rv;
   2097 
   2098 	if (set)
   2099 		rv = ixv_register_vlan(adapter, vid);
   2100 	else
   2101 		rv = ixv_unregister_vlan(adapter, vid);
   2102 
   2103 	if (rv != 0)
   2104 		return rv;
   2105 
   2106 	/*
   2107 	 * Control VLAN HW tagging when ec_nvlan is changed from 1 to 0
   2108 	 * or 0 to 1.
   2109 	 */
   2110 	if ((set && (ec->ec_nvlans == 1)) || (!set && (ec->ec_nvlans == 0)))
   2111 		ixv_setup_vlan_tagging(adapter);
   2112 
   2113 	return rv;
   2114 }
   2115 
   2116 /************************************************************************
   2117  * ixv_register_vlan
   2118  *
   2119  *   Run via a vlan config EVENT, it enables us to use the
   2120  *   HW Filter table since we can get the vlan id. This just
   2121  *   creates the entry in the soft version of the VFTA, init
   2122  *   will repopulate the real table.
   2123  ************************************************************************/
   2124 static int
   2125 ixv_register_vlan(struct adapter *adapter, u16 vtag)
   2126 {
   2127 	struct ixgbe_hw *hw = &adapter->hw;
   2128 	u16		index, bit;
   2129 	int error;
   2130 
   2131 	if ((vtag == 0) || (vtag > 4095)) /* Invalid */
   2132 		return EINVAL;
   2133 	IXGBE_CORE_LOCK(adapter);
   2134 	index = (vtag >> 5) & 0x7F;
   2135 	bit = vtag & 0x1F;
   2136 	adapter->shadow_vfta[index] |= ((u32)1 << bit);
   2137 	error = hw->mac.ops.set_vfta(hw, vtag, 0, true, false);
   2138 	IXGBE_CORE_UNLOCK(adapter);
   2139 
   2140 	if (error != 0) {
   2141 		device_printf(adapter->dev, "failed to register vlan %hu\n",
   2142 		    vtag);
   2143 		error = EACCES;
   2144 	}
   2145 	return error;
   2146 } /* ixv_register_vlan */
   2147 
   2148 /************************************************************************
   2149  * ixv_unregister_vlan
   2150  *
   2151  *   Run via a vlan unconfig EVENT, remove our entry
   2152  *   in the soft vfta.
   2153  ************************************************************************/
   2154 static int
   2155 ixv_unregister_vlan(struct adapter *adapter, u16 vtag)
   2156 {
   2157 	struct ixgbe_hw *hw = &adapter->hw;
   2158 	u16		index, bit;
   2159 	int 		error;
   2160 
   2161 	if ((vtag == 0) || (vtag > 4095))  /* Invalid */
   2162 		return EINVAL;
   2163 
   2164 	IXGBE_CORE_LOCK(adapter);
   2165 	index = (vtag >> 5) & 0x7F;
   2166 	bit = vtag & 0x1F;
   2167 	adapter->shadow_vfta[index] &= ~((u32)1 << bit);
   2168 	error = hw->mac.ops.set_vfta(hw, vtag, 0, false, false);
   2169 	IXGBE_CORE_UNLOCK(adapter);
   2170 
   2171 	if (error != 0) {
   2172 		device_printf(adapter->dev, "failed to unregister vlan %hu\n",
   2173 		    vtag);
   2174 		error = EIO;
   2175 	}
   2176 	return error;
   2177 } /* ixv_unregister_vlan */
   2178 
   2179 /************************************************************************
   2180  * ixv_enable_intr
   2181  ************************************************************************/
   2182 static void
   2183 ixv_enable_intr(struct adapter *adapter)
   2184 {
   2185 	struct ixgbe_hw *hw = &adapter->hw;
   2186 	struct ix_queue *que = adapter->queues;
   2187 	u32		mask;
   2188 	int i;
   2189 
   2190 	/* For VTEIAC */
   2191 	mask = (1 << adapter->vector);
   2192 	for (i = 0; i < adapter->num_queues; i++, que++)
   2193 		mask |= (1 << que->msix);
   2194 	IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, mask);
   2195 
   2196 	/* For VTEIMS */
   2197 	IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, (1 << adapter->vector));
   2198 	que = adapter->queues;
   2199 	for (i = 0; i < adapter->num_queues; i++, que++)
   2200 		ixv_enable_queue(adapter, que->msix);
   2201 
   2202 	IXGBE_WRITE_FLUSH(hw);
   2203 } /* ixv_enable_intr */
   2204 
   2205 /************************************************************************
   2206  * ixv_disable_intr
   2207  ************************************************************************/
   2208 static void
   2209 ixv_disable_intr(struct adapter *adapter)
   2210 {
   2211 	struct ix_queue	*que = adapter->queues;
   2212 
   2213 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEIAC, 0);
   2214 
   2215 	/* disable interrupts other than queues */
   2216 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VTEIMC, adapter->vector);
   2217 
   2218 	for (int i = 0; i < adapter->num_queues; i++, que++)
   2219 		ixv_disable_queue(adapter, que->msix);
   2220 
   2221 	IXGBE_WRITE_FLUSH(&adapter->hw);
   2222 } /* ixv_disable_intr */
   2223 
   2224 /************************************************************************
   2225  * ixv_set_ivar
   2226  *
   2227  *   Setup the correct IVAR register for a particular MSI-X interrupt
   2228  *    - entry is the register array entry
   2229  *    - vector is the MSI-X vector for this queue
   2230  *    - type is RX/TX/MISC
   2231  ************************************************************************/
   2232 static void
   2233 ixv_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
   2234 {
   2235 	struct ixgbe_hw *hw = &adapter->hw;
   2236 	u32		ivar, index;
   2237 
   2238 	vector |= IXGBE_IVAR_ALLOC_VAL;
   2239 
   2240 	if (type == -1) { /* MISC IVAR */
   2241 		ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
   2242 		ivar &= ~0xFF;
   2243 		ivar |= vector;
   2244 		IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
   2245 	} else {	  /* RX/TX IVARS */
   2246 		index = (16 * (entry & 1)) + (8 * type);
   2247 		ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(entry >> 1));
   2248 		ivar &= ~(0xffUL << index);
   2249 		ivar |= ((u32)vector << index);
   2250 		IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(entry >> 1), ivar);
   2251 	}
   2252 } /* ixv_set_ivar */
   2253 
   2254 /************************************************************************
   2255  * ixv_configure_ivars
   2256  ************************************************************************/
   2257 static void
   2258 ixv_configure_ivars(struct adapter *adapter)
   2259 {
   2260 	struct ix_queue *que = adapter->queues;
   2261 
   2262 	/* XXX We should sync EITR value calculation with ixgbe.c? */
   2263 
   2264 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   2265 		/* First the RX queue entry */
   2266 		ixv_set_ivar(adapter, i, que->msix, 0);
   2267 		/* ... and the TX */
   2268 		ixv_set_ivar(adapter, i, que->msix, 1);
   2269 		/* Set an initial value in EITR */
   2270 		ixv_eitr_write(adapter, que->msix, IXGBE_EITR_DEFAULT);
   2271 	}
   2272 
   2273 	/* For the mailbox interrupt */
   2274 	ixv_set_ivar(adapter, 1, adapter->vector, -1);
   2275 } /* ixv_configure_ivars */
   2276 
   2277 
   2278 /************************************************************************
   2279  * ixv_save_stats
   2280  *
   2281  *   The VF stats registers never have a truly virgin
   2282  *   starting point, so this routine tries to make an
   2283  *   artificial one, marking ground zero on attach as
   2284  *   it were.
   2285  ************************************************************************/
   2286 static void
   2287 ixv_save_stats(struct adapter *adapter)
   2288 {
   2289 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
   2290 
   2291 	if (stats->vfgprc.ev_count || stats->vfgptc.ev_count) {
   2292 		stats->saved_reset_vfgprc +=
   2293 		    stats->vfgprc.ev_count - stats->base_vfgprc;
   2294 		stats->saved_reset_vfgptc +=
   2295 		    stats->vfgptc.ev_count - stats->base_vfgptc;
   2296 		stats->saved_reset_vfgorc +=
   2297 		    stats->vfgorc.ev_count - stats->base_vfgorc;
   2298 		stats->saved_reset_vfgotc +=
   2299 		    stats->vfgotc.ev_count - stats->base_vfgotc;
   2300 		stats->saved_reset_vfmprc +=
   2301 		    stats->vfmprc.ev_count - stats->base_vfmprc;
   2302 	}
   2303 } /* ixv_save_stats */
   2304 
   2305 /************************************************************************
   2306  * ixv_init_stats
   2307  ************************************************************************/
   2308 static void
   2309 ixv_init_stats(struct adapter *adapter)
   2310 {
   2311 	struct ixgbe_hw *hw = &adapter->hw;
   2312 
   2313 	adapter->stats.vf.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
   2314 	adapter->stats.vf.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
   2315 	adapter->stats.vf.last_vfgorc |=
   2316 	    (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
   2317 
   2318 	adapter->stats.vf.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
   2319 	adapter->stats.vf.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
   2320 	adapter->stats.vf.last_vfgotc |=
   2321 	    (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
   2322 
   2323 	adapter->stats.vf.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
   2324 
   2325 	adapter->stats.vf.base_vfgprc = adapter->stats.vf.last_vfgprc;
   2326 	adapter->stats.vf.base_vfgorc = adapter->stats.vf.last_vfgorc;
   2327 	adapter->stats.vf.base_vfgptc = adapter->stats.vf.last_vfgptc;
   2328 	adapter->stats.vf.base_vfgotc = adapter->stats.vf.last_vfgotc;
   2329 	adapter->stats.vf.base_vfmprc = adapter->stats.vf.last_vfmprc;
   2330 } /* ixv_init_stats */
   2331 
   2332 #define UPDATE_STAT_32(reg, last, count)		\
   2333 {							\
   2334 	u32 current = IXGBE_READ_REG(hw, (reg));	\
   2335 	if (current < (last))				\
   2336 		count.ev_count += 0x100000000LL;	\
   2337 	(last) = current;				\
   2338 	count.ev_count &= 0xFFFFFFFF00000000LL;		\
   2339 	count.ev_count |= current;			\
   2340 }
   2341 
   2342 #define UPDATE_STAT_36(lsb, msb, last, count)		\
   2343 {							\
   2344 	u64 cur_lsb = IXGBE_READ_REG(hw, (lsb));	\
   2345 	u64 cur_msb = IXGBE_READ_REG(hw, (msb));	\
   2346 	u64 current = ((cur_msb << 32) | cur_lsb);	\
   2347 	if (current < (last))				\
   2348 		count.ev_count += 0x1000000000LL;	\
   2349 	(last) = current;				\
   2350 	count.ev_count &= 0xFFFFFFF000000000LL;		\
   2351 	count.ev_count |= current;			\
   2352 }
   2353 
   2354 /************************************************************************
   2355  * ixv_update_stats - Update the board statistics counters.
   2356  ************************************************************************/
   2357 void
   2358 ixv_update_stats(struct adapter *adapter)
   2359 {
   2360 	struct ixgbe_hw *hw = &adapter->hw;
   2361 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
   2362 
   2363 	UPDATE_STAT_32(IXGBE_VFGPRC, stats->last_vfgprc, stats->vfgprc);
   2364 	UPDATE_STAT_32(IXGBE_VFGPTC, stats->last_vfgptc, stats->vfgptc);
   2365 	UPDATE_STAT_36(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB, stats->last_vfgorc,
   2366 	    stats->vfgorc);
   2367 	UPDATE_STAT_36(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB, stats->last_vfgotc,
   2368 	    stats->vfgotc);
   2369 	UPDATE_STAT_32(IXGBE_VFMPRC, stats->last_vfmprc, stats->vfmprc);
   2370 
   2371 	/* Fill out the OS statistics structure */
   2372 	/*
   2373 	 * NetBSD: Don't override if_{i|o}{packets|bytes|mcasts} with
   2374 	 * adapter->stats counters. It's required to make ifconfig -z
   2375 	 * (SOICZIFDATA) work.
   2376 	 */
   2377 } /* ixv_update_stats */
   2378 
   2379 /************************************************************************
   2380  * ixv_sysctl_interrupt_rate_handler
   2381  ************************************************************************/
   2382 static int
   2383 ixv_sysctl_interrupt_rate_handler(SYSCTLFN_ARGS)
   2384 {
   2385 	struct sysctlnode node = *rnode;
   2386 	struct ix_queue *que = (struct ix_queue *)node.sysctl_data;
   2387 	struct adapter	*adapter = que->adapter;
   2388 	uint32_t reg, usec, rate;
   2389 	int error;
   2390 
   2391 	if (que == NULL)
   2392 		return 0;
   2393 	reg = IXGBE_READ_REG(&que->adapter->hw, IXGBE_VTEITR(que->msix));
   2394 	usec = ((reg & 0x0FF8) >> 3);
   2395 	if (usec > 0)
   2396 		rate = 500000 / usec;
   2397 	else
   2398 		rate = 0;
   2399 	node.sysctl_data = &rate;
   2400 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2401 	if (error || newp == NULL)
   2402 		return error;
   2403 	reg &= ~0xfff; /* default, no limitation */
   2404 	if (rate > 0 && rate < 500000) {
   2405 		if (rate < 1000)
   2406 			rate = 1000;
   2407 		reg |= ((4000000/rate) & 0xff8);
   2408 		/*
   2409 		 * When RSC is used, ITR interval must be larger than
   2410 		 * RSC_DELAY. Currently, we use 2us for RSC_DELAY.
   2411 		 * The minimum value is always greater than 2us on 100M
   2412 		 * (and 10M?(not documented)), but it's not on 1G and higher.
   2413 		 */
   2414 		if ((adapter->link_speed != IXGBE_LINK_SPEED_100_FULL)
   2415 		    && (adapter->link_speed != IXGBE_LINK_SPEED_10_FULL)) {
   2416 			if ((adapter->num_queues > 1)
   2417 			    && (reg < IXGBE_MIN_RSC_EITR_10G1G))
   2418 				return EINVAL;
   2419 		}
   2420 		ixv_max_interrupt_rate = rate;
   2421 	} else
   2422 		ixv_max_interrupt_rate = 0;
   2423 	ixv_eitr_write(adapter, que->msix, reg);
   2424 
   2425 	return (0);
   2426 } /* ixv_sysctl_interrupt_rate_handler */
   2427 
   2428 const struct sysctlnode *
   2429 ixv_sysctl_instance(struct adapter *adapter)
   2430 {
   2431 	const char *dvname;
   2432 	struct sysctllog **log;
   2433 	int rc;
   2434 	const struct sysctlnode *rnode;
   2435 
   2436 	log = &adapter->sysctllog;
   2437 	dvname = device_xname(adapter->dev);
   2438 
   2439 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
   2440 	    0, CTLTYPE_NODE, dvname,
   2441 	    SYSCTL_DESCR("ixv information and settings"),
   2442 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
   2443 		goto err;
   2444 
   2445 	return rnode;
   2446 err:
   2447 	device_printf(adapter->dev,
   2448 	    "%s: sysctl_createv failed, rc = %d\n", __func__, rc);
   2449 	return NULL;
   2450 }
   2451 
   2452 static void
   2453 ixv_add_device_sysctls(struct adapter *adapter)
   2454 {
   2455 	struct sysctllog **log;
   2456 	const struct sysctlnode *rnode, *cnode;
   2457 	device_t dev;
   2458 
   2459 	dev = adapter->dev;
   2460 	log = &adapter->sysctllog;
   2461 
   2462 	if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
   2463 		aprint_error_dev(dev, "could not create sysctl root\n");
   2464 		return;
   2465 	}
   2466 
   2467 	if (sysctl_createv(log, 0, &rnode, &cnode,
   2468 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   2469 	    "debug", SYSCTL_DESCR("Debug Info"),
   2470 	    ixv_sysctl_debug, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   2471 		aprint_error_dev(dev, "could not create sysctl\n");
   2472 
   2473 	if (sysctl_createv(log, 0, &rnode, &cnode,
   2474 	    CTLFLAG_READWRITE, CTLTYPE_BOOL,
   2475 	    "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
   2476 	    NULL, 0, &adapter->enable_aim, 0, CTL_CREATE, CTL_EOL) != 0)
   2477 		aprint_error_dev(dev, "could not create sysctl\n");
   2478 
   2479 	if (sysctl_createv(log, 0, &rnode, &cnode,
   2480 	    CTLFLAG_READWRITE, CTLTYPE_BOOL,
   2481 	    "txrx_workqueue", SYSCTL_DESCR("Use workqueue for packet processing"),
   2482 		NULL, 0, &adapter->txrx_use_workqueue, 0, CTL_CREATE, CTL_EOL) != 0)
   2483 		aprint_error_dev(dev, "could not create sysctl\n");
   2484 }
   2485 
   2486 /************************************************************************
   2487  * ixv_add_stats_sysctls - Add statistic sysctls for the VF.
   2488  ************************************************************************/
   2489 static void
   2490 ixv_add_stats_sysctls(struct adapter *adapter)
   2491 {
   2492 	device_t		dev = adapter->dev;
   2493 	struct tx_ring		*txr = adapter->tx_rings;
   2494 	struct rx_ring		*rxr = adapter->rx_rings;
   2495 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
   2496 	struct ixgbe_hw *hw = &adapter->hw;
   2497 	const struct sysctlnode *rnode, *cnode;
   2498 	struct sysctllog **log = &adapter->sysctllog;
   2499 	const char *xname = device_xname(dev);
   2500 
   2501 	/* Driver Statistics */
   2502 	evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
   2503 	    NULL, xname, "Driver tx dma soft fail EFBIG");
   2504 	evcnt_attach_dynamic(&adapter->mbuf_defrag_failed, EVCNT_TYPE_MISC,
   2505 	    NULL, xname, "m_defrag() failed");
   2506 	evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
   2507 	    NULL, xname, "Driver tx dma hard fail EFBIG");
   2508 	evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
   2509 	    NULL, xname, "Driver tx dma hard fail EINVAL");
   2510 	evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
   2511 	    NULL, xname, "Driver tx dma hard fail other");
   2512 	evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
   2513 	    NULL, xname, "Driver tx dma soft fail EAGAIN");
   2514 	evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
   2515 	    NULL, xname, "Driver tx dma soft fail ENOMEM");
   2516 	evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
   2517 	    NULL, xname, "Watchdog timeouts");
   2518 	evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
   2519 	    NULL, xname, "TSO errors");
   2520 	evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_INTR,
   2521 	    NULL, xname, "Link MSI-X IRQ Handled");
   2522 
   2523 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   2524 		snprintf(adapter->queues[i].evnamebuf,
   2525 		    sizeof(adapter->queues[i].evnamebuf), "%s q%d",
   2526 		    xname, i);
   2527 		snprintf(adapter->queues[i].namebuf,
   2528 		    sizeof(adapter->queues[i].namebuf), "q%d", i);
   2529 
   2530 		if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
   2531 			aprint_error_dev(dev, "could not create sysctl root\n");
   2532 			break;
   2533 		}
   2534 
   2535 		if (sysctl_createv(log, 0, &rnode, &rnode,
   2536 		    0, CTLTYPE_NODE,
   2537 		    adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
   2538 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   2539 			break;
   2540 
   2541 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2542 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   2543 		    "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
   2544 		    ixv_sysctl_interrupt_rate_handler, 0,
   2545 		    (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
   2546 			break;
   2547 
   2548 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2549 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2550 		    "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
   2551 		    ixv_sysctl_tdh_handler, 0, (void *)txr,
   2552 		    0, CTL_CREATE, CTL_EOL) != 0)
   2553 			break;
   2554 
   2555 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2556 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2557 		    "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
   2558 		    ixv_sysctl_tdt_handler, 0, (void *)txr,
   2559 		    0, CTL_CREATE, CTL_EOL) != 0)
   2560 			break;
   2561 
   2562 		evcnt_attach_dynamic(&adapter->queues[i].irqs, EVCNT_TYPE_INTR,
   2563 		    NULL, adapter->queues[i].evnamebuf, "IRQs on queue");
   2564 		evcnt_attach_dynamic(&adapter->queues[i].handleq,
   2565 		    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   2566 		    "Handled queue in softint");
   2567 		evcnt_attach_dynamic(&adapter->queues[i].req, EVCNT_TYPE_MISC,
   2568 		    NULL, adapter->queues[i].evnamebuf, "Requeued in softint");
   2569 		evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
   2570 		    NULL, adapter->queues[i].evnamebuf, "TSO");
   2571 		evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
   2572 		    NULL, adapter->queues[i].evnamebuf,
   2573 		    "Queue No Descriptor Available");
   2574 		evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
   2575 		    NULL, adapter->queues[i].evnamebuf,
   2576 		    "Queue Packets Transmitted");
   2577 #ifndef IXGBE_LEGACY_TX
   2578 		evcnt_attach_dynamic(&txr->pcq_drops, EVCNT_TYPE_MISC,
   2579 		    NULL, adapter->queues[i].evnamebuf,
   2580 		    "Packets dropped in pcq");
   2581 #endif
   2582 
   2583 #ifdef LRO
   2584 		struct lro_ctrl *lro = &rxr->lro;
   2585 #endif /* LRO */
   2586 
   2587 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2588 		    CTLFLAG_READONLY,
   2589 		    CTLTYPE_INT,
   2590 		    "rxd_nxck", SYSCTL_DESCR("Receive Descriptor next to check"),
   2591 			ixv_sysctl_next_to_check_handler, 0, (void *)rxr, 0,
   2592 		    CTL_CREATE, CTL_EOL) != 0)
   2593 			break;
   2594 
   2595 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2596 		    CTLFLAG_READONLY,
   2597 		    CTLTYPE_INT,
   2598 		    "rxd_head", SYSCTL_DESCR("Receive Descriptor Head"),
   2599 		    ixv_sysctl_rdh_handler, 0, (void *)rxr, 0,
   2600 		    CTL_CREATE, CTL_EOL) != 0)
   2601 			break;
   2602 
   2603 		if (sysctl_createv(log, 0, &rnode, &cnode,
   2604 		    CTLFLAG_READONLY,
   2605 		    CTLTYPE_INT,
   2606 		    "rxd_tail", SYSCTL_DESCR("Receive Descriptor Tail"),
   2607 		    ixv_sysctl_rdt_handler, 0, (void *)rxr, 0,
   2608 		    CTL_CREATE, CTL_EOL) != 0)
   2609 			break;
   2610 
   2611 		evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
   2612 		    NULL, adapter->queues[i].evnamebuf, "Queue Packets Received");
   2613 		evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
   2614 		    NULL, adapter->queues[i].evnamebuf, "Queue Bytes Received");
   2615 		evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
   2616 		    NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
   2617 		evcnt_attach_dynamic(&rxr->no_jmbuf, EVCNT_TYPE_MISC,
   2618 		    NULL, adapter->queues[i].evnamebuf, "Rx no jumbo mbuf");
   2619 		evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
   2620 		    NULL, adapter->queues[i].evnamebuf, "Rx discarded");
   2621 #ifdef LRO
   2622 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
   2623 				CTLFLAG_RD, &lro->lro_queued, 0,
   2624 				"LRO Queued");
   2625 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
   2626 				CTLFLAG_RD, &lro->lro_flushed, 0,
   2627 				"LRO Flushed");
   2628 #endif /* LRO */
   2629 	}
   2630 
   2631 	/* MAC stats get their own sub node */
   2632 
   2633 	snprintf(stats->namebuf,
   2634 	    sizeof(stats->namebuf), "%s MAC Statistics", xname);
   2635 
   2636 	evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
   2637 	    stats->namebuf, "rx csum offload - IP");
   2638 	evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
   2639 	    stats->namebuf, "rx csum offload - L4");
   2640 	evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
   2641 	    stats->namebuf, "rx csum offload - IP bad");
   2642 	evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
   2643 	    stats->namebuf, "rx csum offload - L4 bad");
   2644 
   2645 	/* Packet Reception Stats */
   2646 	evcnt_attach_dynamic(&stats->vfgprc, EVCNT_TYPE_MISC, NULL,
   2647 	    xname, "Good Packets Received");
   2648 	evcnt_attach_dynamic(&stats->vfgorc, EVCNT_TYPE_MISC, NULL,
   2649 	    xname, "Good Octets Received");
   2650 	evcnt_attach_dynamic(&stats->vfmprc, EVCNT_TYPE_MISC, NULL,
   2651 	    xname, "Multicast Packets Received");
   2652 	evcnt_attach_dynamic(&stats->vfgptc, EVCNT_TYPE_MISC, NULL,
   2653 	    xname, "Good Packets Transmitted");
   2654 	evcnt_attach_dynamic(&stats->vfgotc, EVCNT_TYPE_MISC, NULL,
   2655 	    xname, "Good Octets Transmitted");
   2656 
   2657 	/* Mailbox Stats */
   2658 	evcnt_attach_dynamic(&hw->mbx.stats.msgs_tx, EVCNT_TYPE_MISC, NULL,
   2659 	    xname, "message TXs");
   2660 	evcnt_attach_dynamic(&hw->mbx.stats.msgs_rx, EVCNT_TYPE_MISC, NULL,
   2661 	    xname, "message RXs");
   2662 	evcnt_attach_dynamic(&hw->mbx.stats.acks, EVCNT_TYPE_MISC, NULL,
   2663 	    xname, "ACKs");
   2664 	evcnt_attach_dynamic(&hw->mbx.stats.reqs, EVCNT_TYPE_MISC, NULL,
   2665 	    xname, "REQs");
   2666 	evcnt_attach_dynamic(&hw->mbx.stats.rsts, EVCNT_TYPE_MISC, NULL,
   2667 	    xname, "RSTs");
   2668 
   2669 } /* ixv_add_stats_sysctls */
   2670 
   2671 static void
   2672 ixv_clear_evcnt(struct adapter *adapter)
   2673 {
   2674 	struct tx_ring		*txr = adapter->tx_rings;
   2675 	struct rx_ring		*rxr = adapter->rx_rings;
   2676 	struct ixgbevf_hw_stats *stats = &adapter->stats.vf;
   2677 	struct ixgbe_hw *hw = &adapter->hw;
   2678 	int i;
   2679 
   2680 	/* Driver Statistics */
   2681 	adapter->efbig_tx_dma_setup.ev_count = 0;
   2682 	adapter->mbuf_defrag_failed.ev_count = 0;
   2683 	adapter->efbig2_tx_dma_setup.ev_count = 0;
   2684 	adapter->einval_tx_dma_setup.ev_count = 0;
   2685 	adapter->other_tx_dma_setup.ev_count = 0;
   2686 	adapter->eagain_tx_dma_setup.ev_count = 0;
   2687 	adapter->enomem_tx_dma_setup.ev_count = 0;
   2688 	adapter->watchdog_events.ev_count = 0;
   2689 	adapter->tso_err.ev_count = 0;
   2690 	adapter->link_irq.ev_count = 0;
   2691 
   2692 	for (i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   2693 		adapter->queues[i].irqs.ev_count = 0;
   2694 		adapter->queues[i].handleq.ev_count = 0;
   2695 		adapter->queues[i].req.ev_count = 0;
   2696 		txr->tso_tx.ev_count = 0;
   2697 		txr->no_desc_avail.ev_count = 0;
   2698 		txr->total_packets.ev_count = 0;
   2699 #ifndef IXGBE_LEGACY_TX
   2700 		txr->pcq_drops.ev_count = 0;
   2701 #endif
   2702 		txr->q_efbig_tx_dma_setup = 0;
   2703 		txr->q_mbuf_defrag_failed = 0;
   2704 		txr->q_efbig2_tx_dma_setup = 0;
   2705 		txr->q_einval_tx_dma_setup = 0;
   2706 		txr->q_other_tx_dma_setup = 0;
   2707 		txr->q_eagain_tx_dma_setup = 0;
   2708 		txr->q_enomem_tx_dma_setup = 0;
   2709 		txr->q_tso_err = 0;
   2710 
   2711 		rxr->rx_packets.ev_count = 0;
   2712 		rxr->rx_bytes.ev_count = 0;
   2713 		rxr->rx_copies.ev_count = 0;
   2714 		rxr->no_jmbuf.ev_count = 0;
   2715 		rxr->rx_discarded.ev_count = 0;
   2716 	}
   2717 
   2718 	/* MAC stats get their own sub node */
   2719 
   2720 	stats->ipcs.ev_count = 0;
   2721 	stats->l4cs.ev_count = 0;
   2722 	stats->ipcs_bad.ev_count = 0;
   2723 	stats->l4cs_bad.ev_count = 0;
   2724 
   2725 	/* Packet Reception Stats */
   2726 	stats->vfgprc.ev_count = 0;
   2727 	stats->vfgorc.ev_count = 0;
   2728 	stats->vfmprc.ev_count = 0;
   2729 	stats->vfgptc.ev_count = 0;
   2730 	stats->vfgotc.ev_count = 0;
   2731 
   2732 	/* Mailbox Stats */
   2733 	hw->mbx.stats.msgs_tx.ev_count = 0;
   2734 	hw->mbx.stats.msgs_rx.ev_count = 0;
   2735 	hw->mbx.stats.acks.ev_count = 0;
   2736 	hw->mbx.stats.reqs.ev_count = 0;
   2737 	hw->mbx.stats.rsts.ev_count = 0;
   2738 
   2739 } /* ixv_clear_evcnt */
   2740 
   2741 /************************************************************************
   2742  * ixv_set_sysctl_value
   2743  ************************************************************************/
   2744 static void
   2745 ixv_set_sysctl_value(struct adapter *adapter, const char *name,
   2746 	const char *description, int *limit, int value)
   2747 {
   2748 	device_t dev =	adapter->dev;
   2749 	struct sysctllog **log;
   2750 	const struct sysctlnode *rnode, *cnode;
   2751 
   2752 	log = &adapter->sysctllog;
   2753 	if ((rnode = ixv_sysctl_instance(adapter)) == NULL) {
   2754 		aprint_error_dev(dev, "could not create sysctl root\n");
   2755 		return;
   2756 	}
   2757 	if (sysctl_createv(log, 0, &rnode, &cnode,
   2758 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   2759 	    name, SYSCTL_DESCR(description),
   2760 	    NULL, 0, limit, 0, CTL_CREATE, CTL_EOL) != 0)
   2761 		aprint_error_dev(dev, "could not create sysctl\n");
   2762 	*limit = value;
   2763 } /* ixv_set_sysctl_value */
   2764 
   2765 /************************************************************************
   2766  * ixv_print_debug_info
   2767  *
   2768  *   Called only when em_display_debug_stats is enabled.
   2769  *   Provides a way to take a look at important statistics
   2770  *   maintained by the driver and hardware.
   2771  ************************************************************************/
   2772 static void
   2773 ixv_print_debug_info(struct adapter *adapter)
   2774 {
   2775 	device_t	dev = adapter->dev;
   2776 	struct ix_queue *que = adapter->queues;
   2777 	struct rx_ring	*rxr;
   2778 	struct tx_ring	*txr;
   2779 #ifdef LRO
   2780 	struct lro_ctrl *lro;
   2781 #endif /* LRO */
   2782 
   2783 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   2784 		txr = que->txr;
   2785 		rxr = que->rxr;
   2786 #ifdef LRO
   2787 		lro = &rxr->lro;
   2788 #endif /* LRO */
   2789 		device_printf(dev, "QUE(%d) IRQs Handled: %lu\n",
   2790 		    que->msix, (long)que->irqs.ev_count);
   2791 		device_printf(dev, "RX(%d) Packets Received: %lld\n",
   2792 		    rxr->me, (long long)rxr->rx_packets.ev_count);
   2793 		device_printf(dev, "RX(%d) Bytes Received: %lu\n",
   2794 		    rxr->me, (long)rxr->rx_bytes.ev_count);
   2795 #ifdef LRO
   2796 		device_printf(dev, "RX(%d) LRO Queued= %ju\n",
   2797 		    rxr->me, (uintmax_t)lro->lro_queued);
   2798 		device_printf(dev, "RX(%d) LRO Flushed= %ju\n",
   2799 		    rxr->me, (uintmax_t)lro->lro_flushed);
   2800 #endif /* LRO */
   2801 		device_printf(dev, "TX(%d) Packets Sent: %lu\n",
   2802 		    txr->me, (long)txr->total_packets.ev_count);
   2803 		device_printf(dev, "TX(%d) NO Desc Avail: %lu\n",
   2804 		    txr->me, (long)txr->no_desc_avail.ev_count);
   2805 	}
   2806 
   2807 	device_printf(dev, "MBX IRQ Handled: %lu\n",
   2808 	    (long)adapter->link_irq.ev_count);
   2809 } /* ixv_print_debug_info */
   2810 
   2811 /************************************************************************
   2812  * ixv_sysctl_debug
   2813  ************************************************************************/
   2814 static int
   2815 ixv_sysctl_debug(SYSCTLFN_ARGS)
   2816 {
   2817 	struct sysctlnode node = *rnode;
   2818 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   2819 	int	       error, result;
   2820 
   2821 	node.sysctl_data = &result;
   2822 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2823 
   2824 	if (error || newp == NULL)
   2825 		return error;
   2826 
   2827 	if (result == 1)
   2828 		ixv_print_debug_info(adapter);
   2829 
   2830 	return 0;
   2831 } /* ixv_sysctl_debug */
   2832 
   2833 /************************************************************************
   2834  * ixv_init_device_features
   2835  ************************************************************************/
   2836 static void
   2837 ixv_init_device_features(struct adapter *adapter)
   2838 {
   2839 	adapter->feat_cap = IXGBE_FEATURE_NETMAP
   2840 			  | IXGBE_FEATURE_VF
   2841 			  | IXGBE_FEATURE_RSS
   2842 			  | IXGBE_FEATURE_LEGACY_TX;
   2843 
   2844 	/* A tad short on feature flags for VFs, atm. */
   2845 	switch (adapter->hw.mac.type) {
   2846 	case ixgbe_mac_82599_vf:
   2847 		break;
   2848 	case ixgbe_mac_X540_vf:
   2849 		break;
   2850 	case ixgbe_mac_X550_vf:
   2851 	case ixgbe_mac_X550EM_x_vf:
   2852 	case ixgbe_mac_X550EM_a_vf:
   2853 		adapter->feat_cap |= IXGBE_FEATURE_NEEDS_CTXD;
   2854 		break;
   2855 	default:
   2856 		break;
   2857 	}
   2858 
   2859 	/* Enabled by default... */
   2860 	/* Is a virtual function (VF) */
   2861 	if (adapter->feat_cap & IXGBE_FEATURE_VF)
   2862 		adapter->feat_en |= IXGBE_FEATURE_VF;
   2863 	/* Netmap */
   2864 	if (adapter->feat_cap & IXGBE_FEATURE_NETMAP)
   2865 		adapter->feat_en |= IXGBE_FEATURE_NETMAP;
   2866 	/* Receive-Side Scaling (RSS) */
   2867 	if (adapter->feat_cap & IXGBE_FEATURE_RSS)
   2868 		adapter->feat_en |= IXGBE_FEATURE_RSS;
   2869 	/* Needs advanced context descriptor regardless of offloads req'd */
   2870 	if (adapter->feat_cap & IXGBE_FEATURE_NEEDS_CTXD)
   2871 		adapter->feat_en |= IXGBE_FEATURE_NEEDS_CTXD;
   2872 
   2873 	/* Enabled via sysctl... */
   2874 	/* Legacy (single queue) transmit */
   2875 	if ((adapter->feat_cap & IXGBE_FEATURE_LEGACY_TX) &&
   2876 	    ixv_enable_legacy_tx)
   2877 		adapter->feat_en |= IXGBE_FEATURE_LEGACY_TX;
   2878 } /* ixv_init_device_features */
   2879 
   2880 /************************************************************************
   2881  * ixv_shutdown - Shutdown entry point
   2882  ************************************************************************/
   2883 #if 0 /* XXX NetBSD ought to register something like this through pmf(9) */
   2884 static int
   2885 ixv_shutdown(device_t dev)
   2886 {
   2887 	struct adapter *adapter = device_private(dev);
   2888 	IXGBE_CORE_LOCK(adapter);
   2889 	ixv_stop(adapter);
   2890 	IXGBE_CORE_UNLOCK(adapter);
   2891 
   2892 	return (0);
   2893 } /* ixv_shutdown */
   2894 #endif
   2895 
   2896 static int
   2897 ixv_ifflags_cb(struct ethercom *ec)
   2898 {
   2899 	struct ifnet *ifp = &ec->ec_if;
   2900 	struct adapter *adapter = ifp->if_softc;
   2901 	int change, rv = 0;
   2902 
   2903 	IXGBE_CORE_LOCK(adapter);
   2904 
   2905 	change = ifp->if_flags ^ adapter->if_flags;
   2906 	if (change != 0)
   2907 		adapter->if_flags = ifp->if_flags;
   2908 
   2909 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
   2910 		rv = ENETRESET;
   2911 		goto out;
   2912 	}
   2913 
   2914 	/* Check for ec_capenable. */
   2915 	change = ec->ec_capenable ^ adapter->ec_capenable;
   2916 	adapter->ec_capenable = ec->ec_capenable;
   2917 	if ((change & ~(ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING
   2918 	    | ETHERCAP_VLAN_HWFILTER)) != 0) {
   2919 		rv = ENETRESET;
   2920 		goto out;
   2921 	}
   2922 
   2923 	/*
   2924 	 * Special handling is not required for ETHERCAP_VLAN_MTU.
   2925 	 * PF's MAXFRS(MHADD) does not include the 4bytes of the VLAN header.
   2926 	 */
   2927 
   2928 	/* Set up VLAN support and filter */
   2929 	if ((change & (ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_HWFILTER)) != 0)
   2930 		rv = ixv_setup_vlan_support(adapter);
   2931 
   2932 out:
   2933 	IXGBE_CORE_UNLOCK(adapter);
   2934 
   2935 	return rv;
   2936 }
   2937 
   2938 
   2939 /************************************************************************
   2940  * ixv_ioctl - Ioctl entry point
   2941  *
   2942  *   Called when the user wants to configure the interface.
   2943  *
   2944  *   return 0 on success, positive on failure
   2945  ************************************************************************/
   2946 static int
   2947 ixv_ioctl(struct ifnet *ifp, u_long command, void *data)
   2948 {
   2949 	struct adapter	*adapter = ifp->if_softc;
   2950 	struct ixgbe_hw *hw = &adapter->hw;
   2951 	struct ifcapreq *ifcr = data;
   2952 	int		error;
   2953 	int l4csum_en;
   2954 	const int l4csum = IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
   2955 	     IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
   2956 
   2957 	switch (command) {
   2958 	case SIOCSIFFLAGS:
   2959 		IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
   2960 		break;
   2961 	case SIOCADDMULTI: {
   2962 		struct ether_multi *enm;
   2963 		struct ether_multistep step;
   2964 		struct ethercom *ec = &adapter->osdep.ec;
   2965 		bool overflow = false;
   2966 		int mcnt = 0;
   2967 
   2968 		/*
   2969 		 * Check the number of multicast address. If it exceeds,
   2970 		 * return ENOSPC.
   2971 		 * Update this code when we support API 1.3.
   2972 		 */
   2973 		ETHER_LOCK(ec);
   2974 		ETHER_FIRST_MULTI(step, ec, enm);
   2975 		while (enm != NULL) {
   2976 			mcnt++;
   2977 
   2978 			/*
   2979 			 * This code is before adding, so one room is required
   2980 			 * at least.
   2981 			 */
   2982 			if (mcnt > (IXGBE_MAX_VF_MC - 1)) {
   2983 				overflow = true;
   2984 				break;
   2985 			}
   2986 			ETHER_NEXT_MULTI(step, enm);
   2987 		}
   2988 		ETHER_UNLOCK(ec);
   2989 		error = 0;
   2990 		if (overflow && ((ec->ec_flags & ETHER_F_ALLMULTI) == 0)) {
   2991 			error = hw->mac.ops.update_xcast_mode(hw,
   2992 			    IXGBEVF_XCAST_MODE_ALLMULTI);
   2993 			if (error == IXGBE_ERR_NOT_TRUSTED) {
   2994 				device_printf(adapter->dev,
   2995 				    "this interface is not trusted\n");
   2996 				error = ENOSPC;
   2997 			} else if (error) {
   2998 				device_printf(adapter->dev,
   2999 				    "number of Ethernet multicast addresses "
   3000 				    "exceeds the limit (%d). error = %d\n",
   3001 				    IXGBE_MAX_VF_MC, error);
   3002 				error = ENOSPC;
   3003 			} else
   3004 				ec->ec_flags |= ETHER_F_ALLMULTI;
   3005 		}
   3006 		if (error)
   3007 			return error;
   3008 	}
   3009 		/*FALLTHROUGH*/
   3010 	case SIOCDELMULTI:
   3011 		IOCTL_DEBUGOUT("ioctl: SIOC(ADD|DEL)MULTI");
   3012 		break;
   3013 	case SIOCSIFMEDIA:
   3014 	case SIOCGIFMEDIA:
   3015 		IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
   3016 		break;
   3017 	case SIOCSIFCAP:
   3018 		IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
   3019 		break;
   3020 	case SIOCSIFMTU:
   3021 		IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
   3022 		break;
   3023 	case SIOCZIFDATA:
   3024 		IOCTL_DEBUGOUT("ioctl: SIOCZIFDATA (Zero counter)");
   3025 		ixv_update_stats(adapter);
   3026 		ixv_clear_evcnt(adapter);
   3027 		break;
   3028 	default:
   3029 		IOCTL_DEBUGOUT1("ioctl: UNKNOWN (0x%X)", (int)command);
   3030 		break;
   3031 	}
   3032 
   3033 	switch (command) {
   3034 	case SIOCSIFCAP:
   3035 		/* Layer-4 Rx checksum offload has to be turned on and
   3036 		 * off as a unit.
   3037 		 */
   3038 		l4csum_en = ifcr->ifcr_capenable & l4csum;
   3039 		if (l4csum_en != l4csum && l4csum_en != 0)
   3040 			return EINVAL;
   3041 		/*FALLTHROUGH*/
   3042 	case SIOCADDMULTI:
   3043 	case SIOCDELMULTI:
   3044 	case SIOCSIFFLAGS:
   3045 	case SIOCSIFMTU:
   3046 	default:
   3047 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
   3048 			return error;
   3049 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   3050 			;
   3051 		else if (command == SIOCSIFCAP || command == SIOCSIFMTU) {
   3052 			IXGBE_CORE_LOCK(adapter);
   3053 			ixv_init_locked(adapter);
   3054 			IXGBE_CORE_UNLOCK(adapter);
   3055 		} else if (command == SIOCADDMULTI || command == SIOCDELMULTI) {
   3056 			/*
   3057 			 * Multicast list has changed; set the hardware filter
   3058 			 * accordingly.
   3059 			 */
   3060 			IXGBE_CORE_LOCK(adapter);
   3061 			ixv_disable_intr(adapter);
   3062 			ixv_set_multi(adapter);
   3063 			ixv_enable_intr(adapter);
   3064 			IXGBE_CORE_UNLOCK(adapter);
   3065 		}
   3066 		return 0;
   3067 	}
   3068 } /* ixv_ioctl */
   3069 
   3070 /************************************************************************
   3071  * ixv_init
   3072  ************************************************************************/
   3073 static int
   3074 ixv_init(struct ifnet *ifp)
   3075 {
   3076 	struct adapter *adapter = ifp->if_softc;
   3077 
   3078 	IXGBE_CORE_LOCK(adapter);
   3079 	ixv_init_locked(adapter);
   3080 	IXGBE_CORE_UNLOCK(adapter);
   3081 
   3082 	return 0;
   3083 } /* ixv_init */
   3084 
   3085 /************************************************************************
   3086  * ixv_handle_que
   3087  ************************************************************************/
   3088 static void
   3089 ixv_handle_que(void *context)
   3090 {
   3091 	struct ix_queue *que = context;
   3092 	struct adapter	*adapter = que->adapter;
   3093 	struct tx_ring	*txr = que->txr;
   3094 	struct ifnet	*ifp = adapter->ifp;
   3095 	bool		more;
   3096 
   3097 	que->handleq.ev_count++;
   3098 
   3099 	if (ifp->if_flags & IFF_RUNNING) {
   3100 		more = ixgbe_rxeof(que);
   3101 		IXGBE_TX_LOCK(txr);
   3102 		more |= ixgbe_txeof(txr);
   3103 		if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX))
   3104 			if (!ixgbe_mq_ring_empty(ifp, txr->txr_interq))
   3105 				ixgbe_mq_start_locked(ifp, txr);
   3106 		/* Only for queue 0 */
   3107 		/* NetBSD still needs this for CBQ */
   3108 		if ((&adapter->queues[0] == que)
   3109 		    && (!ixgbe_legacy_ring_empty(ifp, NULL)))
   3110 			ixgbe_legacy_start_locked(ifp, txr);
   3111 		IXGBE_TX_UNLOCK(txr);
   3112 		if (more) {
   3113 			que->req.ev_count++;
   3114 			if (adapter->txrx_use_workqueue) {
   3115 				/*
   3116 				 * "enqueued flag" is not required here
   3117 				 * the same as ixg(4). See ixgbe_msix_que().
   3118 				 */
   3119 				workqueue_enqueue(adapter->que_wq,
   3120 				    &que->wq_cookie, curcpu());
   3121 			} else
   3122 				  softint_schedule(que->que_si);
   3123 			return;
   3124 		}
   3125 	}
   3126 
   3127 	/* Re-enable this interrupt */
   3128 	ixv_enable_queue(adapter, que->msix);
   3129 
   3130 	return;
   3131 } /* ixv_handle_que */
   3132 
   3133 /************************************************************************
   3134  * ixv_handle_que_work
   3135  ************************************************************************/
   3136 static void
   3137 ixv_handle_que_work(struct work *wk, void *context)
   3138 {
   3139 	struct ix_queue *que = container_of(wk, struct ix_queue, wq_cookie);
   3140 
   3141 	/*
   3142 	 * "enqueued flag" is not required here the same as ixg(4).
   3143 	 * See ixgbe_msix_que().
   3144 	 */
   3145 	ixv_handle_que(que);
   3146 }
   3147 
   3148 /************************************************************************
   3149  * ixv_allocate_msix - Setup MSI-X Interrupt resources and handlers
   3150  ************************************************************************/
   3151 static int
   3152 ixv_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
   3153 {
   3154 	device_t	dev = adapter->dev;
   3155 	struct ix_queue *que = adapter->queues;
   3156 	struct		tx_ring *txr = adapter->tx_rings;
   3157 	int		error, msix_ctrl, rid, vector = 0;
   3158 	pci_chipset_tag_t pc;
   3159 	pcitag_t	tag;
   3160 	char		intrbuf[PCI_INTRSTR_LEN];
   3161 	char		wqname[MAXCOMLEN];
   3162 	char		intr_xname[32];
   3163 	const char	*intrstr = NULL;
   3164 	kcpuset_t	*affinity;
   3165 	int		cpu_id = 0;
   3166 
   3167 	pc = adapter->osdep.pc;
   3168 	tag = adapter->osdep.tag;
   3169 
   3170 	adapter->osdep.nintrs = adapter->num_queues + 1;
   3171 	if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
   3172 	    adapter->osdep.nintrs) != 0) {
   3173 		aprint_error_dev(dev,
   3174 		    "failed to allocate MSI-X interrupt\n");
   3175 		return (ENXIO);
   3176 	}
   3177 
   3178 	kcpuset_create(&affinity, false);
   3179 	for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
   3180 		snprintf(intr_xname, sizeof(intr_xname), "%s TXRX%d",
   3181 		    device_xname(dev), i);
   3182 		intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
   3183 		    sizeof(intrbuf));
   3184 #ifdef IXGBE_MPSAFE
   3185 		pci_intr_setattr(pc, &adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
   3186 		    true);
   3187 #endif
   3188 		/* Set the handler function */
   3189 		que->res = adapter->osdep.ihs[i] = pci_intr_establish_xname(pc,
   3190 		    adapter->osdep.intrs[i], IPL_NET, ixv_msix_que, que,
   3191 		    intr_xname);
   3192 		if (que->res == NULL) {
   3193 			pci_intr_release(pc, adapter->osdep.intrs,
   3194 			    adapter->osdep.nintrs);
   3195 			aprint_error_dev(dev,
   3196 			    "Failed to register QUE handler\n");
   3197 			kcpuset_destroy(affinity);
   3198 			return (ENXIO);
   3199 		}
   3200 		que->msix = vector;
   3201 		adapter->active_queues |= (u64)(1 << que->msix);
   3202 
   3203 		cpu_id = i;
   3204 		/* Round-robin affinity */
   3205 		kcpuset_zero(affinity);
   3206 		kcpuset_set(affinity, cpu_id % ncpu);
   3207 		error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
   3208 		    NULL);
   3209 		aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
   3210 		    intrstr);
   3211 		if (error == 0)
   3212 			aprint_normal(", bound queue %d to cpu %d\n",
   3213 			    i, cpu_id % ncpu);
   3214 		else
   3215 			aprint_normal("\n");
   3216 
   3217 #ifndef IXGBE_LEGACY_TX
   3218 		txr->txr_si
   3219 		    = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   3220 			ixgbe_deferred_mq_start, txr);
   3221 #endif
   3222 		que->que_si
   3223 		    = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   3224 			ixv_handle_que, que);
   3225 		if (que->que_si == NULL) {
   3226 			aprint_error_dev(dev,
   3227 			    "could not establish software interrupt\n");
   3228 		}
   3229 	}
   3230 	snprintf(wqname, sizeof(wqname), "%sdeferTx", device_xname(dev));
   3231 	error = workqueue_create(&adapter->txr_wq, wqname,
   3232 	    ixgbe_deferred_mq_start_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
   3233 	    IXGBE_WORKQUEUE_FLAGS);
   3234 	if (error) {
   3235 		aprint_error_dev(dev, "couldn't create workqueue for deferred Tx\n");
   3236 	}
   3237 	adapter->txr_wq_enqueued = percpu_alloc(sizeof(u_int));
   3238 
   3239 	snprintf(wqname, sizeof(wqname), "%sTxRx", device_xname(dev));
   3240 	error = workqueue_create(&adapter->que_wq, wqname,
   3241 	    ixv_handle_que_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
   3242 	    IXGBE_WORKQUEUE_FLAGS);
   3243 	if (error) {
   3244 		aprint_error_dev(dev,
   3245 		    "couldn't create workqueue\n");
   3246 	}
   3247 
   3248 	/* and Mailbox */
   3249 	cpu_id++;
   3250 	snprintf(intr_xname, sizeof(intr_xname), "%s link", device_xname(dev));
   3251 	adapter->vector = vector;
   3252 	intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
   3253 	    sizeof(intrbuf));
   3254 #ifdef IXGBE_MPSAFE
   3255 	pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
   3256 	    true);
   3257 #endif
   3258 	/* Set the mbx handler function */
   3259 	adapter->osdep.ihs[vector] = pci_intr_establish_xname(pc,
   3260 	    adapter->osdep.intrs[vector], IPL_NET, ixv_msix_mbx, adapter,
   3261 	    intr_xname);
   3262 	if (adapter->osdep.ihs[vector] == NULL) {
   3263 		aprint_error_dev(dev, "Failed to register LINK handler\n");
   3264 		kcpuset_destroy(affinity);
   3265 		return (ENXIO);
   3266 	}
   3267 	/* Round-robin affinity */
   3268 	kcpuset_zero(affinity);
   3269 	kcpuset_set(affinity, cpu_id % ncpu);
   3270 	error = interrupt_distribute(adapter->osdep.ihs[vector],
   3271 	    affinity, NULL);
   3272 
   3273 	aprint_normal_dev(dev,
   3274 	    "for link, interrupting at %s", intrstr);
   3275 	if (error == 0)
   3276 		aprint_normal(", affinity to cpu %d\n", cpu_id % ncpu);
   3277 	else
   3278 		aprint_normal("\n");
   3279 
   3280 	/* Tasklets for Mailbox */
   3281 	adapter->link_si = softint_establish(SOFTINT_NET |IXGBE_SOFTINFT_FLAGS,
   3282 	    ixv_handle_link, adapter);
   3283 	/*
   3284 	 * Due to a broken design QEMU will fail to properly
   3285 	 * enable the guest for MSI-X unless the vectors in
   3286 	 * the table are all set up, so we must rewrite the
   3287 	 * ENABLE in the MSI-X control register again at this
   3288 	 * point to cause it to successfully initialize us.
   3289 	 */
   3290 	if (adapter->hw.mac.type == ixgbe_mac_82599_vf) {
   3291 		pci_get_capability(pc, tag, PCI_CAP_MSIX, &rid, NULL);
   3292 		rid += PCI_MSIX_CTL;
   3293 		msix_ctrl = pci_conf_read(pc, tag, rid);
   3294 		msix_ctrl |= PCI_MSIX_CTL_ENABLE;
   3295 		pci_conf_write(pc, tag, rid, msix_ctrl);
   3296 	}
   3297 
   3298 	kcpuset_destroy(affinity);
   3299 	return (0);
   3300 } /* ixv_allocate_msix */
   3301 
   3302 /************************************************************************
   3303  * ixv_configure_interrupts - Setup MSI-X resources
   3304  *
   3305  *   Note: The VF device MUST use MSI-X, there is no fallback.
   3306  ************************************************************************/
   3307 static int
   3308 ixv_configure_interrupts(struct adapter *adapter)
   3309 {
   3310 	device_t dev = adapter->dev;
   3311 	int want, queues, msgs;
   3312 
   3313 	/* Must have at least 2 MSI-X vectors */
   3314 	msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
   3315 	if (msgs < 2) {
   3316 		aprint_error_dev(dev, "MSIX config error\n");
   3317 		return (ENXIO);
   3318 	}
   3319 	msgs = MIN(msgs, IXG_MAX_NINTR);
   3320 
   3321 	/* Figure out a reasonable auto config value */
   3322 	queues = (ncpu > (msgs - 1)) ? (msgs - 1) : ncpu;
   3323 
   3324 	if (ixv_num_queues != 0)
   3325 		queues = ixv_num_queues;
   3326 	else if ((ixv_num_queues == 0) && (queues > IXGBE_VF_MAX_TX_QUEUES))
   3327 		queues = IXGBE_VF_MAX_TX_QUEUES;
   3328 
   3329 	/*
   3330 	 * Want vectors for the queues,
   3331 	 * plus an additional for mailbox.
   3332 	 */
   3333 	want = queues + 1;
   3334 	if (msgs >= want)
   3335 		msgs = want;
   3336 	else {
   3337 		aprint_error_dev(dev,
   3338 		    "MSI-X Configuration Problem, "
   3339 		    "%d vectors but %d queues wanted!\n",
   3340 		    msgs, want);
   3341 		return -1;
   3342 	}
   3343 
   3344 	adapter->msix_mem = (void *)1; /* XXX */
   3345 	aprint_normal_dev(dev,
   3346 	    "Using MSI-X interrupts with %d vectors\n", msgs);
   3347 	adapter->num_queues = queues;
   3348 
   3349 	return (0);
   3350 } /* ixv_configure_interrupts */
   3351 
   3352 
   3353 /************************************************************************
   3354  * ixv_handle_link - Tasklet handler for MSI-X MBX interrupts
   3355  *
   3356  *   Done outside of interrupt context since the driver might sleep
   3357  ************************************************************************/
   3358 static void
   3359 ixv_handle_link(void *context)
   3360 {
   3361 	struct adapter *adapter = context;
   3362 
   3363 	IXGBE_CORE_LOCK(adapter);
   3364 
   3365 	adapter->hw.mac.ops.check_link(&adapter->hw, &adapter->link_speed,
   3366 	    &adapter->link_up, FALSE);
   3367 	ixv_update_link_status(adapter);
   3368 
   3369 	IXGBE_CORE_UNLOCK(adapter);
   3370 } /* ixv_handle_link */
   3371 
   3372 /************************************************************************
   3373  * ixv_check_link - Used in the local timer to poll for link changes
   3374  ************************************************************************/
   3375 static s32
   3376 ixv_check_link(struct adapter *adapter)
   3377 {
   3378 	s32 error;
   3379 
   3380 	KASSERT(mutex_owned(&adapter->core_mtx));
   3381 
   3382 	adapter->hw.mac.get_link_status = TRUE;
   3383 
   3384 	error = adapter->hw.mac.ops.check_link(&adapter->hw,
   3385 	    &adapter->link_speed, &adapter->link_up, FALSE);
   3386 	ixv_update_link_status(adapter);
   3387 
   3388 	return error;
   3389 } /* ixv_check_link */
   3390