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