Home | History | Annotate | Line # | Download | only in ixgbe
ixgbe.c revision 1.46
      1 /******************************************************************************
      2 
      3   Copyright (c) 2001-2015, Intel Corporation
      4   All rights reserved.
      5 
      6   Redistribution and use in source and binary forms, with or without
      7   modification, are permitted provided that the following conditions are met:
      8 
      9    1. Redistributions of source code must retain the above copyright notice,
     10       this list of conditions and the following disclaimer.
     11 
     12    2. Redistributions in binary form must reproduce the above copyright
     13       notice, this list of conditions and the following disclaimer in the
     14       documentation and/or other materials provided with the distribution.
     15 
     16    3. Neither the name of the Intel Corporation nor the names of its
     17       contributors may be used to endorse or promote products derived from
     18       this software without specific prior written permission.
     19 
     20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   POSSIBILITY OF SUCH DAMAGE.
     31 
     32 ******************************************************************************/
     33 /*
     34  * Copyright (c) 2011 The NetBSD Foundation, Inc.
     35  * All rights reserved.
     36  *
     37  * This code is derived from software contributed to The NetBSD Foundation
     38  * by Coyote Point Systems, Inc.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     59  * POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 /*$FreeBSD: head/sys/dev/ixgbe/if_ix.c 285590 2015-07-15 00:35:50Z pkelsey $*/
     62 /*$NetBSD: ixgbe.c,v 1.46 2016/12/02 10:24:31 msaitoh Exp $*/
     63 
     64 #include "opt_inet.h"
     65 #include "opt_inet6.h"
     66 
     67 #include "ixgbe.h"
     68 #include "vlan.h"
     69 
     70 #include <sys/cprng.h>
     71 
     72 /*********************************************************************
     73  *  Set this to one to display debug statistics
     74  *********************************************************************/
     75 int             ixgbe_display_debug_stats = 0;
     76 
     77 /*********************************************************************
     78  *  Driver version
     79  *********************************************************************/
     80 char ixgbe_driver_version[] = "3.1.0";
     81 
     82 /*********************************************************************
     83  *  PCI Device ID Table
     84  *
     85  *  Used by probe to select devices to load on
     86  *  Last field stores an index into ixgbe_strings
     87  *  Last entry must be all 0s
     88  *
     89  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
     90  *********************************************************************/
     91 
     92 static ixgbe_vendor_info_t ixgbe_vendor_info_array[] =
     93 {
     94 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_DUAL_PORT, 0, 0, 0},
     95 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_SINGLE_PORT, 0, 0, 0},
     96 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_CX4, 0, 0, 0},
     97 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT, 0, 0, 0},
     98 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT2, 0, 0, 0},
     99 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598, 0, 0, 0},
    100 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_DA_DUAL_PORT, 0, 0, 0},
    101 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_CX4_DUAL_PORT, 0, 0, 0},
    102 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_XF_LR, 0, 0, 0},
    103 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM, 0, 0, 0},
    104 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_SFP_LOM, 0, 0, 0},
    105 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4, 0, 0, 0},
    106 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4_MEZZ, 0, 0, 0},
    107 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP, 0, 0, 0},
    108 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_XAUI_LOM, 0, 0, 0},
    109 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_CX4, 0, 0, 0},
    110 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_T3_LOM, 0, 0, 0},
    111 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_COMBO_BACKPLANE, 0, 0, 0},
    112 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BACKPLANE_FCOE, 0, 0, 0},
    113 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF2, 0, 0, 0},
    114 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_FCOE, 0, 0, 0},
    115 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599EN_SFP, 0, 0, 0},
    116 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF_QP, 0, 0, 0},
    117 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_QSFP_SF_QP, 0, 0, 0},
    118 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T, 0, 0, 0},
    119 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T1, 0, 0, 0},
    120 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T, 0, 0, 0},
    121 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KR, 0, 0, 0},
    122 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KX4, 0, 0, 0},
    123 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_10G_T, 0, 0, 0},
    124 	/* required last entry */
    125 	{0, 0, 0, 0, 0}
    126 };
    127 
    128 /*********************************************************************
    129  *  Table of branding strings
    130  *********************************************************************/
    131 
    132 static const char    *ixgbe_strings[] = {
    133 	"Intel(R) PRO/10GbE PCI-Express Network Driver"
    134 };
    135 
    136 /*********************************************************************
    137  *  Function prototypes
    138  *********************************************************************/
    139 static int      ixgbe_probe(device_t, cfdata_t, void *);
    140 static void     ixgbe_attach(device_t, device_t, void *);
    141 static int      ixgbe_detach(device_t, int);
    142 #if 0
    143 static int      ixgbe_shutdown(device_t);
    144 #endif
    145 static bool	ixgbe_suspend(device_t, const pmf_qual_t *);
    146 static bool	ixgbe_resume(device_t, const pmf_qual_t *);
    147 static int      ixgbe_ioctl(struct ifnet *, u_long, void *);
    148 static void	ixgbe_ifstop(struct ifnet *, int);
    149 static int	ixgbe_init(struct ifnet *);
    150 static void	ixgbe_init_locked(struct adapter *);
    151 static void     ixgbe_stop(void *);
    152 static void	ixgbe_add_media_types(struct adapter *);
    153 static void     ixgbe_media_status(struct ifnet *, struct ifmediareq *);
    154 static int      ixgbe_media_change(struct ifnet *);
    155 static void     ixgbe_identify_hardware(struct adapter *);
    156 static int      ixgbe_allocate_pci_resources(struct adapter *,
    157 		    const struct pci_attach_args *);
    158 static void	ixgbe_get_slot_info(struct ixgbe_hw *);
    159 static int      ixgbe_allocate_msix(struct adapter *,
    160 		    const struct pci_attach_args *);
    161 static int      ixgbe_allocate_legacy(struct adapter *,
    162 		    const struct pci_attach_args *);
    163 static int	ixgbe_setup_msix(struct adapter *);
    164 static void	ixgbe_free_pci_resources(struct adapter *);
    165 static void	ixgbe_local_timer(void *);
    166 static int	ixgbe_setup_interface(device_t, struct adapter *);
    167 static void	ixgbe_config_gpie(struct adapter *);
    168 static void	ixgbe_config_dmac(struct adapter *);
    169 static void	ixgbe_config_delay_values(struct adapter *);
    170 static void	ixgbe_config_link(struct adapter *);
    171 static void	ixgbe_check_eee_support(struct adapter *);
    172 static void	ixgbe_check_wol_support(struct adapter *);
    173 static int	ixgbe_setup_low_power_mode(struct adapter *);
    174 static void	ixgbe_rearm_queues(struct adapter *, u64);
    175 
    176 static void     ixgbe_initialize_transmit_units(struct adapter *);
    177 static void     ixgbe_initialize_receive_units(struct adapter *);
    178 static void	ixgbe_enable_rx_drop(struct adapter *);
    179 static void	ixgbe_disable_rx_drop(struct adapter *);
    180 
    181 static void     ixgbe_enable_intr(struct adapter *);
    182 static void     ixgbe_disable_intr(struct adapter *);
    183 static void     ixgbe_update_stats_counters(struct adapter *);
    184 static void     ixgbe_set_promisc(struct adapter *);
    185 static void     ixgbe_set_multi(struct adapter *);
    186 static void     ixgbe_update_link_status(struct adapter *);
    187 static void	ixgbe_set_ivar(struct adapter *, u8, u8, s8);
    188 static void	ixgbe_configure_ivars(struct adapter *);
    189 static u8 *	ixgbe_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
    190 
    191 static void	ixgbe_setup_vlan_hw_support(struct adapter *);
    192 #if 0
    193 static void	ixgbe_register_vlan(void *, struct ifnet *, u16);
    194 static void	ixgbe_unregister_vlan(void *, struct ifnet *, u16);
    195 #endif
    196 
    197 static void	ixgbe_add_device_sysctls(struct adapter *);
    198 static void     ixgbe_add_hw_stats(struct adapter *);
    199 
    200 /* Sysctl handlers */
    201 static int	ixgbe_set_flowcntl(SYSCTLFN_PROTO);
    202 static int	ixgbe_set_advertise(SYSCTLFN_PROTO);
    203 static int	ixgbe_sysctl_thermal_test(SYSCTLFN_PROTO);
    204 static int	ixgbe_sysctl_dmac(SYSCTLFN_PROTO);
    205 static int	ixgbe_sysctl_phy_temp(SYSCTLFN_PROTO);
    206 static int	ixgbe_sysctl_phy_overtemp_occurred(SYSCTLFN_PROTO);
    207 static int	ixgbe_sysctl_wol_enable(SYSCTLFN_PROTO);
    208 static int	ixgbe_sysctl_wufc(SYSCTLFN_PROTO);
    209 static int	ixgbe_sysctl_eee_enable(SYSCTLFN_PROTO);
    210 static int	ixgbe_sysctl_eee_negotiated(SYSCTLFN_PROTO);
    211 static int	ixgbe_sysctl_eee_rx_lpi_status(SYSCTLFN_PROTO);
    212 static int	ixgbe_sysctl_eee_tx_lpi_status(SYSCTLFN_PROTO);
    213 
    214 /* Support for pluggable optic modules */
    215 static bool	ixgbe_sfp_probe(struct adapter *);
    216 static void	ixgbe_setup_optics(struct adapter *);
    217 
    218 /* Legacy (single vector interrupt handler */
    219 static int	ixgbe_legacy_irq(void *);
    220 
    221 /* The MSI/X Interrupt handlers */
    222 static int	ixgbe_msix_que(void *);
    223 static int	ixgbe_msix_link(void *);
    224 
    225 /* Software interrupts for deferred work */
    226 static void	ixgbe_handle_que(void *);
    227 static void	ixgbe_handle_link(void *);
    228 static void	ixgbe_handle_msf(void *);
    229 static void	ixgbe_handle_mod(void *);
    230 static void	ixgbe_handle_phy(void *);
    231 
    232 const struct sysctlnode *ixgbe_sysctl_instance(struct adapter *);
    233 static ixgbe_vendor_info_t *ixgbe_lookup(const struct pci_attach_args *);
    234 
    235 #ifdef IXGBE_FDIR
    236 static void	ixgbe_reinit_fdir(void *, int);
    237 #endif
    238 
    239 #ifdef PCI_IOV
    240 static void	ixgbe_ping_all_vfs(struct adapter *);
    241 static void	ixgbe_handle_mbx(void *, int);
    242 static int	ixgbe_init_iov(device_t, u16, const nvlist_t *);
    243 static void	ixgbe_uninit_iov(device_t);
    244 static int	ixgbe_add_vf(device_t, u16, const nvlist_t *);
    245 static void	ixgbe_initialize_iov(struct adapter *);
    246 static void	ixgbe_recalculate_max_frame(struct adapter *);
    247 static void	ixgbe_init_vf(struct adapter *, struct ixgbe_vf *);
    248 #endif /* PCI_IOV */
    249 
    250 
    251 /*********************************************************************
    252  *  FreeBSD Device Interface Entry Points
    253  *********************************************************************/
    254 
    255 CFATTACH_DECL3_NEW(ixg, sizeof(struct adapter),
    256     ixgbe_probe, ixgbe_attach, ixgbe_detach, NULL, NULL, NULL,
    257     DVF_DETACH_SHUTDOWN);
    258 
    259 #if 0
    260 devclass_t ix_devclass;
    261 DRIVER_MODULE(ix, pci, ix_driver, ix_devclass, 0, 0);
    262 
    263 MODULE_DEPEND(ix, pci, 1, 1, 1);
    264 MODULE_DEPEND(ix, ether, 1, 1, 1);
    265 #ifdef DEV_NETMAP
    266 MODULE_DEPEND(ix, netmap, 1, 1, 1);
    267 #endif /* DEV_NETMAP */
    268 #endif
    269 
    270 /*
    271 ** TUNEABLE PARAMETERS:
    272 */
    273 
    274 /*
    275 ** AIM: Adaptive Interrupt Moderation
    276 ** which means that the interrupt rate
    277 ** is varied over time based on the
    278 ** traffic for that interrupt vector
    279 */
    280 static int ixgbe_enable_aim = TRUE;
    281 #define SYSCTL_INT(__x, __y)
    282 SYSCTL_INT("hw.ixgbe.enable_aim", &ixgbe_enable_aim);
    283 
    284 static int ixgbe_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
    285 SYSCTL_INT("hw.ixgbe.max_interrupt_rate", &ixgbe_max_interrupt_rate);
    286 
    287 /* How many packets rxeof tries to clean at a time */
    288 static int ixgbe_rx_process_limit = 256;
    289 SYSCTL_INT("hw.ixgbe.rx_process_limit", &ixgbe_rx_process_limit);
    290 
    291 /* How many packets txeof tries to clean at a time */
    292 static int ixgbe_tx_process_limit = 256;
    293 SYSCTL_INT("hw.ixgbe.tx_process_limit", &ixgbe_tx_process_limit);
    294 
    295 /*
    296 ** Smart speed setting, default to on
    297 ** this only works as a compile option
    298 ** right now as its during attach, set
    299 ** this to 'ixgbe_smart_speed_off' to
    300 ** disable.
    301 */
    302 static int ixgbe_smart_speed = ixgbe_smart_speed_on;
    303 
    304 /*
    305  * MSIX should be the default for best performance,
    306  * but this allows it to be forced off for testing.
    307  */
    308 static int ixgbe_enable_msix = 1;
    309 SYSCTL_INT("hw.ixgbe.enable_msix", &ixgbe_enable_msix);
    310 
    311 /*
    312  * Number of Queues, can be set to 0,
    313  * it then autoconfigures based on the
    314  * number of cpus with a max of 8. This
    315  * can be overriden manually here.
    316  */
    317 static int ixgbe_num_queues = 1;
    318 SYSCTL_INT("hw.ixgbe.num_queues", &ixgbe_num_queues);
    319 
    320 /*
    321 ** Number of TX descriptors per ring,
    322 ** setting higher than RX as this seems
    323 ** the better performing choice.
    324 */
    325 static int ixgbe_txd = PERFORM_TXD;
    326 SYSCTL_INT("hw.ixgbe.txd", &ixgbe_txd);
    327 
    328 /* Number of RX descriptors per ring */
    329 static int ixgbe_rxd = PERFORM_RXD;
    330 SYSCTL_INT("hw.ixgbe.rxd", &ixgbe_rxd);
    331 
    332 /*
    333 ** Defining this on will allow the use
    334 ** of unsupported SFP+ modules, note that
    335 ** doing so you are on your own :)
    336 */
    337 static int allow_unsupported_sfp = false;
    338 SYSCTL_INT("hw.ix.unsupported_sfp", &allow_unsupported_sfp);
    339 
    340 /* Keep running tab on them for sanity check */
    341 static int ixgbe_total_ports;
    342 
    343 #ifdef IXGBE_FDIR
    344 /*
    345 ** Flow Director actually 'steals'
    346 ** part of the packet buffer as its
    347 ** filter pool, this variable controls
    348 ** how much it uses:
    349 **  0 = 64K, 1 = 128K, 2 = 256K
    350 */
    351 static int fdir_pballoc = 1;
    352 #endif
    353 
    354 #ifdef DEV_NETMAP
    355 /*
    356  * The #ifdef DEV_NETMAP / #endif blocks in this file are meant to
    357  * be a reference on how to implement netmap support in a driver.
    358  * Additional comments are in ixgbe_netmap.h .
    359  *
    360  * <dev/netmap/ixgbe_netmap.h> contains functions for netmap support
    361  * that extend the standard driver.
    362  */
    363 #include <dev/netmap/ixgbe_netmap.h>
    364 #endif /* DEV_NETMAP */
    365 
    366 /*********************************************************************
    367  *  Device identification routine
    368  *
    369  *  ixgbe_probe determines if the driver should be loaded on
    370  *  adapter based on PCI vendor/device id of the adapter.
    371  *
    372  *  return 1 on success, 0 on failure
    373  *********************************************************************/
    374 
    375 static int
    376 ixgbe_probe(device_t dev, cfdata_t cf, void *aux)
    377 {
    378 	const struct pci_attach_args *pa = aux;
    379 
    380 	return (ixgbe_lookup(pa) != NULL) ? 1 : 0;
    381 }
    382 
    383 static ixgbe_vendor_info_t *
    384 ixgbe_lookup(const struct pci_attach_args *pa)
    385 {
    386 	pcireg_t subid;
    387 	ixgbe_vendor_info_t *ent;
    388 
    389 	INIT_DEBUGOUT("ixgbe_probe: begin");
    390 
    391 	if (PCI_VENDOR(pa->pa_id) != IXGBE_INTEL_VENDOR_ID)
    392 		return NULL;
    393 
    394 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    395 
    396 	for (ent = ixgbe_vendor_info_array; ent->vendor_id != 0; ent++) {
    397 		if (PCI_VENDOR(pa->pa_id) == ent->vendor_id &&
    398 		    PCI_PRODUCT(pa->pa_id) == ent->device_id &&
    399 
    400 		    (PCI_SUBSYS_VENDOR(subid) == ent->subvendor_id ||
    401 		     ent->subvendor_id == 0) &&
    402 
    403 		    (PCI_SUBSYS_ID(subid) == ent->subdevice_id ||
    404 		     ent->subdevice_id == 0)) {
    405 			++ixgbe_total_ports;
    406 			return ent;
    407 		}
    408 	}
    409 	return NULL;
    410 }
    411 
    412 
    413 /*********************************************************************
    414  *  Device initialization routine
    415  *
    416  *  The attach entry point is called when the driver is being loaded.
    417  *  This routine identifies the type of hardware, allocates all resources
    418  *  and initializes the hardware.
    419  *
    420  *  return 0 on success, positive on failure
    421  *********************************************************************/
    422 
    423 static void
    424 ixgbe_attach(device_t parent, device_t dev, void *aux)
    425 {
    426 	struct adapter *adapter;
    427 	struct ixgbe_hw *hw;
    428 	int             error = -1;
    429 	u16		csum;
    430 	u32		ctrl_ext;
    431 	ixgbe_vendor_info_t *ent;
    432 	struct pci_attach_args *pa = aux;
    433 
    434 	INIT_DEBUGOUT("ixgbe_attach: begin");
    435 
    436 	/* Allocate, clear, and link in our adapter structure */
    437 	adapter = device_private(dev);
    438 	adapter->dev = adapter->osdep.dev = dev;
    439 	hw = &adapter->hw;
    440 	adapter->osdep.pc = pa->pa_pc;
    441 	adapter->osdep.tag = pa->pa_tag;
    442 	adapter->osdep.dmat = pa->pa_dmat;
    443 	adapter->osdep.attached = false;
    444 
    445 	ent = ixgbe_lookup(pa);
    446 
    447 	KASSERT(ent != NULL);
    448 
    449 	aprint_normal(": %s, Version - %s\n",
    450 	    ixgbe_strings[ent->index], ixgbe_driver_version);
    451 
    452 	/* Core Lock Init*/
    453 	IXGBE_CORE_LOCK_INIT(adapter, device_xname(dev));
    454 
    455 	/* Set up the timer callout */
    456 	callout_init(&adapter->timer, 0);
    457 
    458 	/* Determine hardware revision */
    459 	ixgbe_identify_hardware(adapter);
    460 
    461 	/* Do base PCI setup - map BAR0 */
    462 	if (ixgbe_allocate_pci_resources(adapter, pa)) {
    463 		aprint_error_dev(dev, "Allocation of PCI resources failed\n");
    464 		error = ENXIO;
    465 		goto err_out;
    466 	}
    467 
    468 	/* Do descriptor calc and sanity checks */
    469 	if (((ixgbe_txd * sizeof(union ixgbe_adv_tx_desc)) % DBA_ALIGN) != 0 ||
    470 	    ixgbe_txd < MIN_TXD || ixgbe_txd > MAX_TXD) {
    471 		aprint_error_dev(dev, "TXD config issue, using default!\n");
    472 		adapter->num_tx_desc = DEFAULT_TXD;
    473 	} else
    474 		adapter->num_tx_desc = ixgbe_txd;
    475 
    476 	/*
    477 	** With many RX rings it is easy to exceed the
    478 	** system mbuf allocation. Tuning nmbclusters
    479 	** can alleviate this.
    480 	*/
    481 	if (nmbclusters > 0) {
    482 		int s;
    483 		s = (ixgbe_rxd * adapter->num_queues) * ixgbe_total_ports;
    484 		if (s > nmbclusters) {
    485 			aprint_error_dev(dev, "RX Descriptors exceed "
    486 			    "system mbuf max, using default instead!\n");
    487 			ixgbe_rxd = DEFAULT_RXD;
    488 		}
    489 	}
    490 
    491 	if (((ixgbe_rxd * sizeof(union ixgbe_adv_rx_desc)) % DBA_ALIGN) != 0 ||
    492 	    ixgbe_rxd < MIN_RXD || ixgbe_rxd > MAX_RXD) {
    493 		aprint_error_dev(dev, "RXD config issue, using default!\n");
    494 		adapter->num_rx_desc = DEFAULT_RXD;
    495 	} else
    496 		adapter->num_rx_desc = ixgbe_rxd;
    497 
    498 	/* Allocate our TX/RX Queues */
    499 	if (ixgbe_allocate_queues(adapter)) {
    500 		error = ENOMEM;
    501 		goto err_out;
    502 	}
    503 
    504 	/* Allocate multicast array memory. */
    505 	adapter->mta = malloc(sizeof(*adapter->mta) *
    506 	    MAX_NUM_MULTICAST_ADDRESSES, M_DEVBUF, M_NOWAIT);
    507 	if (adapter->mta == NULL) {
    508 		aprint_error_dev(dev, "Cannot allocate multicast setup array\n");
    509 		error = ENOMEM;
    510 		goto err_late;
    511 	}
    512 
    513 	/* Initialize the shared code */
    514 	hw->allow_unsupported_sfp = allow_unsupported_sfp;
    515 	error = ixgbe_init_shared_code(hw);
    516 	if (error == IXGBE_ERR_SFP_NOT_PRESENT) {
    517 		/*
    518 		** No optics in this port, set up
    519 		** so the timer routine will probe
    520 		** for later insertion.
    521 		*/
    522 		adapter->sfp_probe = TRUE;
    523 		error = 0;
    524 	} else if (error == IXGBE_ERR_SFP_NOT_SUPPORTED) {
    525 		aprint_error_dev(dev,"Unsupported SFP+ module detected!\n");
    526 		error = EIO;
    527 		goto err_late;
    528 	} else if (error) {
    529 		aprint_error_dev(dev,"Unable to initialize the shared code\n");
    530 		error = EIO;
    531 		goto err_late;
    532 	}
    533 
    534 	/* Make sure we have a good EEPROM before we read from it */
    535 	if (ixgbe_validate_eeprom_checksum(&adapter->hw, &csum) < 0) {
    536 		aprint_error_dev(dev,"The EEPROM Checksum Is Not Valid\n");
    537 		error = EIO;
    538 		goto err_late;
    539 	}
    540 
    541 	error = ixgbe_init_hw(hw);
    542 	switch (error) {
    543 	case IXGBE_ERR_EEPROM_VERSION:
    544 		aprint_error_dev(dev, "This device is a pre-production adapter/"
    545 		    "LOM.  Please be aware there may be issues associated "
    546 		    "with your hardware.\n If you are experiencing problems "
    547 		    "please contact your Intel or hardware representative "
    548 		    "who provided you with this hardware.\n");
    549 		break;
    550 	case IXGBE_ERR_SFP_NOT_SUPPORTED:
    551 		aprint_error_dev(dev,"Unsupported SFP+ Module\n");
    552 		error = EIO;
    553 		aprint_error_dev(dev,"Hardware Initialization Failure\n");
    554 		goto err_late;
    555 	case IXGBE_ERR_SFP_NOT_PRESENT:
    556 		device_printf(dev,"No SFP+ Module found\n");
    557 		/* falls thru */
    558 	default:
    559 		break;
    560 	}
    561 
    562 	/* Detect and set physical type */
    563 	ixgbe_setup_optics(adapter);
    564 
    565 	error = -1;
    566 	if ((adapter->msix > 1) && (ixgbe_enable_msix))
    567 		error = ixgbe_allocate_msix(adapter, pa);
    568 	if (error != 0)
    569 		error = ixgbe_allocate_legacy(adapter, pa);
    570 	if (error)
    571 		goto err_late;
    572 
    573 	/* Setup OS specific network interface */
    574 	if (ixgbe_setup_interface(dev, adapter) != 0)
    575 		goto err_late;
    576 
    577 	/* Initialize statistics */
    578 	ixgbe_update_stats_counters(adapter);
    579 
    580         /* Check PCIE slot type/speed/width */
    581 	ixgbe_get_slot_info(hw);
    582 
    583 
    584 	/* Set an initial default flow control value */
    585 	adapter->fc = ixgbe_fc_full;
    586 
    587 #ifdef PCI_IOV
    588 	if ((hw->mac.type != ixgbe_mac_82598EB) && (adapter->msix > 1)) {
    589 		nvlist_t *pf_schema, *vf_schema;
    590 
    591 		hw->mbx.ops.init_params(hw);
    592 		pf_schema = pci_iov_schema_alloc_node();
    593 		vf_schema = pci_iov_schema_alloc_node();
    594 		pci_iov_schema_add_unicast_mac(vf_schema, "mac-addr", 0, NULL);
    595 		pci_iov_schema_add_bool(vf_schema, "mac-anti-spoof",
    596 		    IOV_SCHEMA_HASDEFAULT, TRUE);
    597 		pci_iov_schema_add_bool(vf_schema, "allow-set-mac",
    598 		    IOV_SCHEMA_HASDEFAULT, FALSE);
    599 		pci_iov_schema_add_bool(vf_schema, "allow-promisc",
    600 		    IOV_SCHEMA_HASDEFAULT, FALSE);
    601 		error = pci_iov_attach(dev, pf_schema, vf_schema);
    602 		if (error != 0) {
    603 			device_printf(dev,
    604 			    "Error %d setting up SR-IOV\n", error);
    605 		}
    606 	}
    607 #endif /* PCI_IOV */
    608 
    609 	/* Check for certain supported features */
    610 	ixgbe_check_wol_support(adapter);
    611 	ixgbe_check_eee_support(adapter);
    612 
    613 	/* Add sysctls */
    614 	ixgbe_add_device_sysctls(adapter);
    615 	ixgbe_add_hw_stats(adapter);
    616 
    617 	/* let hardware know driver is loaded */
    618 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
    619 	ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
    620 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
    621 
    622 #ifdef DEV_NETMAP
    623 	ixgbe_netmap_attach(adapter);
    624 #endif /* DEV_NETMAP */
    625 
    626 	if (pmf_device_register(dev, ixgbe_suspend, ixgbe_resume))
    627 		pmf_class_network_register(dev, adapter->ifp);
    628 	else
    629 		aprint_error_dev(dev, "couldn't establish power handler\n");
    630 
    631 	INIT_DEBUGOUT("ixgbe_attach: end");
    632 	adapter->osdep.attached = true;
    633 	return;
    634 
    635 err_late:
    636 	ixgbe_free_transmit_structures(adapter);
    637 	ixgbe_free_receive_structures(adapter);
    638 err_out:
    639 	if (adapter->ifp != NULL)
    640 		if_free(adapter->ifp);
    641 	ixgbe_free_pci_resources(adapter);
    642 	if (adapter->mta != NULL)
    643 		free(adapter->mta, M_DEVBUF);
    644 	return;
    645 }
    646 
    647 /*********************************************************************
    648  *  Device removal routine
    649  *
    650  *  The detach entry point is called when the driver is being removed.
    651  *  This routine stops the adapter and deallocates all the resources
    652  *  that were allocated for driver operation.
    653  *
    654  *  return 0 on success, positive on failure
    655  *********************************************************************/
    656 
    657 static int
    658 ixgbe_detach(device_t dev, int flags)
    659 {
    660 	struct adapter *adapter = device_private(dev);
    661 	struct rx_ring *rxr = adapter->rx_rings;
    662 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
    663 	struct ix_queue *que = adapter->queues;
    664 	struct tx_ring *txr = adapter->tx_rings;
    665 	u32	ctrl_ext;
    666 
    667 	INIT_DEBUGOUT("ixgbe_detach: begin");
    668 	if (adapter->osdep.attached == false)
    669 		return 0;
    670 
    671 #if NVLAN > 0
    672 	/* Make sure VLANs are not using driver */
    673 	if (!VLAN_ATTACHED(&adapter->osdep.ec))
    674 		;	/* nothing to do: no VLANs */
    675 	else if ((flags & (DETACH_SHUTDOWN|DETACH_FORCE)) != 0)
    676 		vlan_ifdetach(adapter->ifp);
    677 	else {
    678 		aprint_error_dev(dev, "VLANs in use\n");
    679 		return EBUSY;
    680 	}
    681 #endif
    682 
    683 #ifdef PCI_IOV
    684 	if (pci_iov_detach(dev) != 0) {
    685 		device_printf(dev, "SR-IOV in use; detach first.\n");
    686 		return (EBUSY);
    687 	}
    688 #endif /* PCI_IOV */
    689 
    690 	/* Stop the adapter */
    691 	IXGBE_CORE_LOCK(adapter);
    692 	ixgbe_setup_low_power_mode(adapter);
    693 	IXGBE_CORE_UNLOCK(adapter);
    694 
    695 	for (int i = 0; i < adapter->num_queues; i++, que++, txr++) {
    696 #ifndef IXGBE_LEGACY_TX
    697 		softint_disestablish(txr->txq_si);
    698 #endif
    699 		softint_disestablish(que->que_si);
    700 	}
    701 
    702 	/* Drain the Link queue */
    703 	softint_disestablish(adapter->link_si);
    704 	softint_disestablish(adapter->mod_si);
    705 	softint_disestablish(adapter->msf_si);
    706 #ifdef PCI_IOV
    707 	softint_disestablish(adapter->mbx_si);
    708 #endif
    709 	softint_disestablish(adapter->phy_si);
    710 #ifdef IXGBE_FDIR
    711 	softint_disestablish(adapter->fdir_si);
    712 #endif
    713 
    714 	/* let hardware know driver is unloading */
    715 	ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
    716 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
    717 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT, ctrl_ext);
    718 
    719 	ether_ifdetach(adapter->ifp);
    720 	callout_halt(&adapter->timer, NULL);
    721 #ifdef DEV_NETMAP
    722 	netmap_detach(adapter->ifp);
    723 #endif /* DEV_NETMAP */
    724 	ixgbe_free_pci_resources(adapter);
    725 #if 0	/* XXX the NetBSD port is probably missing something here */
    726 	bus_generic_detach(dev);
    727 #endif
    728 	if_detach(adapter->ifp);
    729 
    730 	sysctl_teardown(&adapter->sysctllog);
    731 	evcnt_detach(&adapter->handleq);
    732 	evcnt_detach(&adapter->req);
    733 	evcnt_detach(&adapter->morerx);
    734 	evcnt_detach(&adapter->moretx);
    735 	evcnt_detach(&adapter->txloops);
    736 	evcnt_detach(&adapter->efbig_tx_dma_setup);
    737 	evcnt_detach(&adapter->m_defrag_failed);
    738 	evcnt_detach(&adapter->efbig2_tx_dma_setup);
    739 	evcnt_detach(&adapter->einval_tx_dma_setup);
    740 	evcnt_detach(&adapter->other_tx_dma_setup);
    741 	evcnt_detach(&adapter->eagain_tx_dma_setup);
    742 	evcnt_detach(&adapter->enomem_tx_dma_setup);
    743 	evcnt_detach(&adapter->watchdog_events);
    744 	evcnt_detach(&adapter->tso_err);
    745 	evcnt_detach(&adapter->link_irq);
    746 
    747 	txr = adapter->tx_rings;
    748 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
    749 		evcnt_detach(&txr->no_desc_avail);
    750 		evcnt_detach(&txr->total_packets);
    751 		evcnt_detach(&txr->tso_tx);
    752 
    753 		if (i < __arraycount(adapter->stats.pf.mpc)) {
    754 			evcnt_detach(&adapter->stats.pf.mpc[i]);
    755 		}
    756 		if (i < __arraycount(adapter->stats.pf.pxontxc)) {
    757 			evcnt_detach(&adapter->stats.pf.pxontxc[i]);
    758 			evcnt_detach(&adapter->stats.pf.pxonrxc[i]);
    759 			evcnt_detach(&adapter->stats.pf.pxofftxc[i]);
    760 			evcnt_detach(&adapter->stats.pf.pxoffrxc[i]);
    761 			evcnt_detach(&adapter->stats.pf.pxon2offc[i]);
    762 		}
    763 		if (i < __arraycount(adapter->stats.pf.qprc)) {
    764 			evcnt_detach(&adapter->stats.pf.qprc[i]);
    765 			evcnt_detach(&adapter->stats.pf.qptc[i]);
    766 			evcnt_detach(&adapter->stats.pf.qbrc[i]);
    767 			evcnt_detach(&adapter->stats.pf.qbtc[i]);
    768 			evcnt_detach(&adapter->stats.pf.qprdc[i]);
    769 		}
    770 
    771 		evcnt_detach(&rxr->rx_packets);
    772 		evcnt_detach(&rxr->rx_bytes);
    773 		evcnt_detach(&rxr->rx_copies);
    774 		evcnt_detach(&rxr->no_jmbuf);
    775 		evcnt_detach(&rxr->rx_discarded);
    776 		evcnt_detach(&rxr->rx_irq);
    777 	}
    778 	evcnt_detach(&stats->ipcs);
    779 	evcnt_detach(&stats->l4cs);
    780 	evcnt_detach(&stats->ipcs_bad);
    781 	evcnt_detach(&stats->l4cs_bad);
    782 	evcnt_detach(&stats->intzero);
    783 	evcnt_detach(&stats->legint);
    784 	evcnt_detach(&stats->crcerrs);
    785 	evcnt_detach(&stats->illerrc);
    786 	evcnt_detach(&stats->errbc);
    787 	evcnt_detach(&stats->mspdc);
    788 	evcnt_detach(&stats->mlfc);
    789 	evcnt_detach(&stats->mrfc);
    790 	evcnt_detach(&stats->rlec);
    791 	evcnt_detach(&stats->lxontxc);
    792 	evcnt_detach(&stats->lxonrxc);
    793 	evcnt_detach(&stats->lxofftxc);
    794 	evcnt_detach(&stats->lxoffrxc);
    795 
    796 	/* Packet Reception Stats */
    797 	evcnt_detach(&stats->tor);
    798 	evcnt_detach(&stats->gorc);
    799 	evcnt_detach(&stats->tpr);
    800 	evcnt_detach(&stats->gprc);
    801 	evcnt_detach(&stats->mprc);
    802 	evcnt_detach(&stats->bprc);
    803 	evcnt_detach(&stats->prc64);
    804 	evcnt_detach(&stats->prc127);
    805 	evcnt_detach(&stats->prc255);
    806 	evcnt_detach(&stats->prc511);
    807 	evcnt_detach(&stats->prc1023);
    808 	evcnt_detach(&stats->prc1522);
    809 	evcnt_detach(&stats->ruc);
    810 	evcnt_detach(&stats->rfc);
    811 	evcnt_detach(&stats->roc);
    812 	evcnt_detach(&stats->rjc);
    813 	evcnt_detach(&stats->mngprc);
    814 	evcnt_detach(&stats->xec);
    815 
    816 	/* Packet Transmission Stats */
    817 	evcnt_detach(&stats->gotc);
    818 	evcnt_detach(&stats->tpt);
    819 	evcnt_detach(&stats->gptc);
    820 	evcnt_detach(&stats->bptc);
    821 	evcnt_detach(&stats->mptc);
    822 	evcnt_detach(&stats->mngptc);
    823 	evcnt_detach(&stats->ptc64);
    824 	evcnt_detach(&stats->ptc127);
    825 	evcnt_detach(&stats->ptc255);
    826 	evcnt_detach(&stats->ptc511);
    827 	evcnt_detach(&stats->ptc1023);
    828 	evcnt_detach(&stats->ptc1522);
    829 
    830 	ixgbe_free_transmit_structures(adapter);
    831 	ixgbe_free_receive_structures(adapter);
    832 	free(adapter->mta, M_DEVBUF);
    833 
    834 	IXGBE_CORE_LOCK_DESTROY(adapter);
    835 	return (0);
    836 }
    837 
    838 /*********************************************************************
    839  *
    840  *  Shutdown entry point
    841  *
    842  **********************************************************************/
    843 
    844 #if 0 /* XXX NetBSD ought to register something like this through pmf(9) */
    845 static int
    846 ixgbe_shutdown(device_t dev)
    847 {
    848 	struct adapter *adapter = device_private(dev);
    849 	int error = 0;
    850 
    851 	INIT_DEBUGOUT("ixgbe_shutdown: begin");
    852 
    853 	IXGBE_CORE_LOCK(adapter);
    854 	error = ixgbe_setup_low_power_mode(adapter);
    855 	IXGBE_CORE_UNLOCK(adapter);
    856 
    857 	return (error);
    858 }
    859 #endif
    860 
    861 /**
    862  * Methods for going from:
    863  * D0 -> D3: ixgbe_suspend
    864  * D3 -> D0: ixgbe_resume
    865  */
    866 static bool
    867 ixgbe_suspend(device_t dev, const pmf_qual_t *qual)
    868 {
    869 	struct adapter *adapter = device_private(dev);
    870 	int error = 0;
    871 
    872 	INIT_DEBUGOUT("ixgbe_suspend: begin");
    873 
    874 	IXGBE_CORE_LOCK(adapter);
    875 
    876 	error = ixgbe_setup_low_power_mode(adapter);
    877 
    878 #if 0 /* XXX */
    879 	/* Save state and power down */
    880 	pci_save_state(dev);
    881 	pci_set_powerstate(dev, PCI_POWERSTATE_D3);
    882 #endif
    883 
    884 	IXGBE_CORE_UNLOCK(adapter);
    885 
    886 	return (error);
    887 }
    888 
    889 static bool
    890 ixgbe_resume(device_t dev, const pmf_qual_t *qual)
    891 {
    892 	struct adapter *adapter = device_private(dev);
    893 	struct ifnet *ifp = adapter->ifp;
    894 	struct ixgbe_hw *hw = &adapter->hw;
    895 	u32 wus;
    896 
    897 	INIT_DEBUGOUT("ixgbe_resume: begin");
    898 
    899 	IXGBE_CORE_LOCK(adapter);
    900 
    901 #if 0 /* XXX */
    902 	pci_set_powerstate(dev, PCI_POWERSTATE_D0);
    903 	pci_restore_state(dev);
    904 #endif
    905 
    906 	/* Read & clear WUS register */
    907 	wus = IXGBE_READ_REG(hw, IXGBE_WUS);
    908 	if (wus)
    909 		device_printf(dev, "Woken up by (WUS): %#010x\n",
    910 		    IXGBE_READ_REG(hw, IXGBE_WUS));
    911 	IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
    912 	/* And clear WUFC until next low-power transition */
    913 	IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
    914 
    915 	/*
    916 	 * Required after D3->D0 transition;
    917 	 * will re-advertise all previous advertised speeds
    918 	 */
    919 	if (ifp->if_flags & IFF_UP)
    920 		ixgbe_init_locked(adapter);
    921 
    922 	IXGBE_CORE_UNLOCK(adapter);
    923 
    924 	INIT_DEBUGOUT("ixgbe_resume: end");
    925 	return true;
    926 }
    927 
    928 static int
    929 ixgbe_ifflags_cb(struct ethercom *ec)
    930 {
    931 	struct ifnet *ifp = &ec->ec_if;
    932 	struct adapter *adapter = ifp->if_softc;
    933 	int change = ifp->if_flags ^ adapter->if_flags, rc = 0;
    934 
    935 	IXGBE_CORE_LOCK(adapter);
    936 
    937 	if (change != 0)
    938 		adapter->if_flags = ifp->if_flags;
    939 
    940 	if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
    941 		rc = ENETRESET;
    942 	else if ((change & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
    943 		ixgbe_set_promisc(adapter);
    944 
    945 	/* Set up VLAN support and filter */
    946 	ixgbe_setup_vlan_hw_support(adapter);
    947 
    948 	IXGBE_CORE_UNLOCK(adapter);
    949 
    950 	return rc;
    951 }
    952 
    953 /*********************************************************************
    954  *  Ioctl entry point
    955  *
    956  *  ixgbe_ioctl is called when the user wants to configure the
    957  *  interface.
    958  *
    959  *  return 0 on success, positive on failure
    960  **********************************************************************/
    961 
    962 static int
    963 ixgbe_ioctl(struct ifnet * ifp, u_long command, void *data)
    964 {
    965 	struct adapter	*adapter = ifp->if_softc;
    966 	struct ifcapreq *ifcr = data;
    967 	struct ifreq	*ifr = data;
    968 	int             error = 0;
    969 	int l4csum_en;
    970 	const int l4csum = IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx|
    971 	     IFCAP_CSUM_TCPv6_Rx|IFCAP_CSUM_UDPv6_Rx;
    972 
    973 	switch (command) {
    974 	case SIOCSIFFLAGS:
    975 		IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
    976 		break;
    977 	case SIOCADDMULTI:
    978 	case SIOCDELMULTI:
    979 		IOCTL_DEBUGOUT("ioctl: SIOC(ADD|DEL)MULTI");
    980 		break;
    981 	case SIOCSIFMEDIA:
    982 	case SIOCGIFMEDIA:
    983 		IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
    984 		break;
    985 	case SIOCSIFCAP:
    986 		IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
    987 		break;
    988 	case SIOCSIFMTU:
    989 		IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
    990 		break;
    991 	default:
    992 		IOCTL_DEBUGOUT1("ioctl: UNKNOWN (0x%X)\n", (int)command);
    993 		break;
    994 	}
    995 
    996 	switch (command) {
    997 	case SIOCSIFMEDIA:
    998 	case SIOCGIFMEDIA:
    999 		return ifmedia_ioctl(ifp, ifr, &adapter->media, command);
   1000 	case SIOCGI2C:
   1001 	{
   1002 		struct ixgbe_hw *hw = &adapter->hw;
   1003 		struct ixgbe_i2c_req	i2c;
   1004 		IOCTL_DEBUGOUT("ioctl: SIOCGI2C (Get I2C Data)");
   1005 		error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
   1006 		if (error != 0)
   1007 			break;
   1008 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
   1009 			error = EINVAL;
   1010 			break;
   1011 		}
   1012 		if (i2c.len > sizeof(i2c.data)) {
   1013 			error = EINVAL;
   1014 			break;
   1015 		}
   1016 
   1017 		hw->phy.ops.read_i2c_byte(hw, i2c.offset,
   1018 		    i2c.dev_addr, i2c.data);
   1019 		error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
   1020 		break;
   1021 	}
   1022 	case SIOCSIFCAP:
   1023 		/* Layer-4 Rx checksum offload has to be turned on and
   1024 		 * off as a unit.
   1025 		 */
   1026 		l4csum_en = ifcr->ifcr_capenable & l4csum;
   1027 		if (l4csum_en != l4csum && l4csum_en != 0)
   1028 			return EINVAL;
   1029 		/*FALLTHROUGH*/
   1030 	case SIOCADDMULTI:
   1031 	case SIOCDELMULTI:
   1032 	case SIOCSIFFLAGS:
   1033 	case SIOCSIFMTU:
   1034 	default:
   1035 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
   1036 			return error;
   1037 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   1038 			;
   1039 		else if (command == SIOCSIFCAP || command == SIOCSIFMTU) {
   1040 			IXGBE_CORE_LOCK(adapter);
   1041 			ixgbe_init_locked(adapter);
   1042 #ifdef PCI_IOV
   1043 			ixgbe_recalculate_max_frame(adapter);
   1044 #endif
   1045 			IXGBE_CORE_UNLOCK(adapter);
   1046 		} else if (command == SIOCADDMULTI || command == SIOCDELMULTI) {
   1047 			/*
   1048 			 * Multicast list has changed; set the hardware filter
   1049 			 * accordingly.
   1050 			 */
   1051 			IXGBE_CORE_LOCK(adapter);
   1052 			ixgbe_disable_intr(adapter);
   1053 			ixgbe_set_multi(adapter);
   1054 			ixgbe_enable_intr(adapter);
   1055 			IXGBE_CORE_UNLOCK(adapter);
   1056 		}
   1057 		return 0;
   1058 	}
   1059 
   1060 	return error;
   1061 }
   1062 
   1063 /*********************************************************************
   1064  *  Init entry point
   1065  *
   1066  *  This routine is used in two ways. It is used by the stack as
   1067  *  init entry point in network interface structure. It is also used
   1068  *  by the driver as a hw/sw initialization routine to get to a
   1069  *  consistent state.
   1070  *
   1071  *  return 0 on success, positive on failure
   1072  **********************************************************************/
   1073 #define IXGBE_MHADD_MFS_SHIFT 16
   1074 
   1075 static void
   1076 ixgbe_init_locked(struct adapter *adapter)
   1077 {
   1078 	struct ifnet   *ifp = adapter->ifp;
   1079 	device_t 	dev = adapter->dev;
   1080 	struct ixgbe_hw *hw = &adapter->hw;
   1081 	struct tx_ring  *txr;
   1082 	struct rx_ring  *rxr;
   1083 	u32		txdctl, mhadd;
   1084 	u32		rxdctl, rxctrl;
   1085 #ifdef PCI_IOV
   1086 	enum ixgbe_iov_mode mode;
   1087 #endif
   1088 
   1089 	/* XXX check IFF_UP and IFF_RUNNING, power-saving state! */
   1090 
   1091 	KASSERT(mutex_owned(&adapter->core_mtx));
   1092 	INIT_DEBUGOUT("ixgbe_init_locked: begin");
   1093 
   1094 	hw->adapter_stopped = FALSE;
   1095 	ixgbe_stop_adapter(hw);
   1096         callout_stop(&adapter->timer);
   1097 
   1098 	/* XXX I moved this here from the SIOCSIFMTU case in ixgbe_ioctl(). */
   1099 	adapter->max_frame_size =
   1100 		ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   1101 
   1102 #ifdef PCI_IOV
   1103 	mode = ixgbe_get_iov_mode(adapter);
   1104 	adapter->pool = ixgbe_max_vfs(mode);
   1105 	/* Queue indices may change with IOV mode */
   1106 	for (int i = 0; i < adapter->num_queues; i++) {
   1107 		adapter->rx_rings[i].me = ixgbe_pf_que_index(mode, i);
   1108 		adapter->tx_rings[i].me = ixgbe_pf_que_index(mode, i);
   1109 	}
   1110 #endif
   1111         /* reprogram the RAR[0] in case user changed it. */
   1112 	ixgbe_set_rar(hw, 0, hw->mac.addr, adapter->pool, IXGBE_RAH_AV);
   1113 
   1114 	/* Get the latest mac address, User can use a LAA */
   1115 	memcpy(hw->mac.addr, CLLADDR(ifp->if_sadl),
   1116 	    IXGBE_ETH_LENGTH_OF_ADDRESS);
   1117 	ixgbe_set_rar(hw, 0, hw->mac.addr, adapter->pool, 1);
   1118 	hw->addr_ctrl.rar_used_count = 1;
   1119 
   1120 	/* Prepare transmit descriptors and buffers */
   1121 	if (ixgbe_setup_transmit_structures(adapter)) {
   1122 		device_printf(dev, "Could not setup transmit structures\n");
   1123 		ixgbe_stop(adapter);
   1124 		return;
   1125 	}
   1126 
   1127 	ixgbe_init_hw(hw);
   1128 #ifdef PCI_IOV
   1129 	ixgbe_initialize_iov(adapter);
   1130 #endif
   1131 	ixgbe_initialize_transmit_units(adapter);
   1132 
   1133 	/* Setup Multicast table */
   1134 	ixgbe_set_multi(adapter);
   1135 
   1136 	/*
   1137 	** Determine the correct mbuf pool
   1138 	** for doing jumbo frames
   1139 	*/
   1140 	if (adapter->max_frame_size <= MCLBYTES)
   1141 		adapter->rx_mbuf_sz = MCLBYTES;
   1142 	else
   1143 		adapter->rx_mbuf_sz = MJUMPAGESIZE;
   1144 
   1145 	/* Prepare receive descriptors and buffers */
   1146 	if (ixgbe_setup_receive_structures(adapter)) {
   1147 		device_printf(dev, "Could not setup receive structures\n");
   1148 		ixgbe_stop(adapter);
   1149 		return;
   1150 	}
   1151 
   1152 	/* Configure RX settings */
   1153 	ixgbe_initialize_receive_units(adapter);
   1154 
   1155 	/* Enable SDP & MSIX interrupts based on adapter */
   1156 	ixgbe_config_gpie(adapter);
   1157 
   1158 	/* Set MTU size */
   1159 	if (ifp->if_mtu > ETHERMTU) {
   1160 		/* aka IXGBE_MAXFRS on 82599 and newer */
   1161 		mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
   1162 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
   1163 		mhadd |= adapter->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
   1164 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
   1165 	}
   1166 
   1167 	/* Now enable all the queues */
   1168 	for (int i = 0; i < adapter->num_queues; i++) {
   1169 		txr = &adapter->tx_rings[i];
   1170 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(txr->me));
   1171 		txdctl |= IXGBE_TXDCTL_ENABLE;
   1172 		/* Set WTHRESH to 8, burst writeback */
   1173 		txdctl |= (8 << 16);
   1174 		/*
   1175 		 * When the internal queue falls below PTHRESH (32),
   1176 		 * start prefetching as long as there are at least
   1177 		 * HTHRESH (1) buffers ready. The values are taken
   1178 		 * from the Intel linux driver 3.8.21.
   1179 		 * Prefetching enables tx line rate even with 1 queue.
   1180 		 */
   1181 		txdctl |= (32 << 0) | (1 << 8);
   1182 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(txr->me), txdctl);
   1183 	}
   1184 
   1185 	for (int i = 0, j = 0; i < adapter->num_queues; i++) {
   1186 		rxr = &adapter->rx_rings[i];
   1187 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
   1188 		if (hw->mac.type == ixgbe_mac_82598EB) {
   1189 			/*
   1190 			** PTHRESH = 21
   1191 			** HTHRESH = 4
   1192 			** WTHRESH = 8
   1193 			*/
   1194 			rxdctl &= ~0x3FFFFF;
   1195 			rxdctl |= 0x080420;
   1196 		}
   1197 		rxdctl |= IXGBE_RXDCTL_ENABLE;
   1198 		IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), rxdctl);
   1199 		for (; j < 10; j++) {
   1200 			if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me)) &
   1201 			    IXGBE_RXDCTL_ENABLE)
   1202 				break;
   1203 			else
   1204 				msec_delay(1);
   1205 		}
   1206 		wmb();
   1207 #ifdef DEV_NETMAP
   1208 		/*
   1209 		 * In netmap mode, we must preserve the buffers made
   1210 		 * available to userspace before the if_init()
   1211 		 * (this is true by default on the TX side, because
   1212 		 * init makes all buffers available to userspace).
   1213 		 *
   1214 		 * netmap_reset() and the device specific routines
   1215 		 * (e.g. ixgbe_setup_receive_rings()) map these
   1216 		 * buffers at the end of the NIC ring, so here we
   1217 		 * must set the RDT (tail) register to make sure
   1218 		 * they are not overwritten.
   1219 		 *
   1220 		 * In this driver the NIC ring starts at RDH = 0,
   1221 		 * RDT points to the last slot available for reception (?),
   1222 		 * so RDT = num_rx_desc - 1 means the whole ring is available.
   1223 		 */
   1224 		if (ifp->if_capenable & IFCAP_NETMAP) {
   1225 			struct netmap_adapter *na = NA(adapter->ifp);
   1226 			struct netmap_kring *kring = &na->rx_rings[i];
   1227 			int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
   1228 
   1229 			IXGBE_WRITE_REG(hw, IXGBE_RDT(rxr->me), t);
   1230 		} else
   1231 #endif /* DEV_NETMAP */
   1232 		IXGBE_WRITE_REG(hw, IXGBE_RDT(rxr->me), adapter->num_rx_desc - 1);
   1233 	}
   1234 
   1235 	/* Enable Receive engine */
   1236 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
   1237 	if (hw->mac.type == ixgbe_mac_82598EB)
   1238 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
   1239 	rxctrl |= IXGBE_RXCTRL_RXEN;
   1240 	ixgbe_enable_rx_dma(hw, rxctrl);
   1241 
   1242 	callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
   1243 
   1244 	/* Set up MSI/X routing */
   1245 	if (ixgbe_enable_msix)  {
   1246 		ixgbe_configure_ivars(adapter);
   1247 		/* Set up auto-mask */
   1248 		if (hw->mac.type == ixgbe_mac_82598EB)
   1249 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
   1250 		else {
   1251 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
   1252 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
   1253 		}
   1254 	} else {  /* Simple settings for Legacy/MSI */
   1255                 ixgbe_set_ivar(adapter, 0, 0, 0);
   1256                 ixgbe_set_ivar(adapter, 0, 0, 1);
   1257 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
   1258 	}
   1259 
   1260 #ifdef IXGBE_FDIR
   1261 	/* Init Flow director */
   1262 	if (hw->mac.type != ixgbe_mac_82598EB) {
   1263 		u32 hdrm = 32 << fdir_pballoc;
   1264 
   1265 		hw->mac.ops.setup_rxpba(hw, 0, hdrm, PBA_STRATEGY_EQUAL);
   1266 		ixgbe_init_fdir_signature_82599(&adapter->hw, fdir_pballoc);
   1267 	}
   1268 #endif
   1269 
   1270 	/*
   1271 	 * Check on any SFP devices that
   1272 	 * need to be kick-started
   1273 	 */
   1274 	if (hw->phy.type == ixgbe_phy_none) {
   1275 		int err = hw->phy.ops.identify(hw);
   1276 		if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   1277                 	device_printf(dev,
   1278 			    "Unsupported SFP+ module type was detected.\n");
   1279 			return;
   1280         	}
   1281 	}
   1282 
   1283 	/* Set moderation on the Link interrupt */
   1284 	IXGBE_WRITE_REG(hw, IXGBE_EITR(adapter->vector), IXGBE_LINK_ITR);
   1285 
   1286 	/* Configure Energy Efficient Ethernet for supported devices */
   1287 	ixgbe_setup_eee(hw, adapter->eee_enabled);
   1288 
   1289 	/* Config/Enable Link */
   1290 	ixgbe_config_link(adapter);
   1291 
   1292 	/* Hardware Packet Buffer & Flow Control setup */
   1293 	ixgbe_config_delay_values(adapter);
   1294 
   1295 	/* Initialize the FC settings */
   1296 	ixgbe_start_hw(hw);
   1297 
   1298 	/* Set up VLAN support and filter */
   1299 	ixgbe_setup_vlan_hw_support(adapter);
   1300 
   1301 	/* Setup DMA Coalescing */
   1302 	ixgbe_config_dmac(adapter);
   1303 
   1304 	/* And now turn on interrupts */
   1305 	ixgbe_enable_intr(adapter);
   1306 
   1307 #ifdef PCI_IOV
   1308 	/* Enable the use of the MBX by the VF's */
   1309 	{
   1310 		u32 reg = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
   1311 		reg |= IXGBE_CTRL_EXT_PFRSTD;
   1312 		IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, reg);
   1313 	}
   1314 #endif
   1315 
   1316 	/* Now inform the stack we're ready */
   1317 	ifp->if_flags |= IFF_RUNNING;
   1318 
   1319 	return;
   1320 }
   1321 
   1322 static int
   1323 ixgbe_init(struct ifnet *ifp)
   1324 {
   1325 	struct adapter *adapter = ifp->if_softc;
   1326 
   1327 	IXGBE_CORE_LOCK(adapter);
   1328 	ixgbe_init_locked(adapter);
   1329 	IXGBE_CORE_UNLOCK(adapter);
   1330 	return 0;	/* XXX ixgbe_init_locked cannot fail?  really? */
   1331 }
   1332 
   1333 static void
   1334 ixgbe_config_gpie(struct adapter *adapter)
   1335 {
   1336 	struct ixgbe_hw *hw = &adapter->hw;
   1337 	u32 gpie;
   1338 
   1339 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
   1340 
   1341 	/* Fan Failure Interrupt */
   1342 	if (hw->device_id == IXGBE_DEV_ID_82598AT)
   1343 		gpie |= IXGBE_SDP1_GPIEN;
   1344 
   1345 	/*
   1346 	 * Module detection (SDP2)
   1347 	 * Media ready (SDP1)
   1348 	 */
   1349 	if (hw->mac.type == ixgbe_mac_82599EB) {
   1350 		gpie |= IXGBE_SDP2_GPIEN;
   1351 		if (hw->device_id != IXGBE_DEV_ID_82599_QSFP_SF_QP)
   1352 			gpie |= IXGBE_SDP1_GPIEN;
   1353 	}
   1354 
   1355 	/*
   1356 	 * Thermal Failure Detection (X540)
   1357 	 * Link Detection (X557)
   1358 	 */
   1359 	if (hw->mac.type == ixgbe_mac_X540 ||
   1360 	    hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
   1361 	    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
   1362 		gpie |= IXGBE_SDP0_GPIEN_X540;
   1363 
   1364 	if (adapter->msix > 1) {
   1365 		/* Enable Enhanced MSIX mode */
   1366 		gpie |= IXGBE_GPIE_MSIX_MODE;
   1367 		gpie |= IXGBE_GPIE_EIAME | IXGBE_GPIE_PBA_SUPPORT |
   1368 		    IXGBE_GPIE_OCD;
   1369 	}
   1370 
   1371 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
   1372 	return;
   1373 }
   1374 
   1375 /*
   1376  * Requires adapter->max_frame_size to be set.
   1377  */
   1378 static void
   1379 ixgbe_config_delay_values(struct adapter *adapter)
   1380 {
   1381 	struct ixgbe_hw *hw = &adapter->hw;
   1382 	u32 rxpb, frame, size, tmp;
   1383 
   1384 	frame = adapter->max_frame_size;
   1385 
   1386 	/* Calculate High Water */
   1387 	switch (hw->mac.type) {
   1388 	case ixgbe_mac_X540:
   1389 	case ixgbe_mac_X550:
   1390 	case ixgbe_mac_X550EM_x:
   1391 		tmp = IXGBE_DV_X540(frame, frame);
   1392 		break;
   1393 	default:
   1394 		tmp = IXGBE_DV(frame, frame);
   1395 		break;
   1396 	}
   1397 	size = IXGBE_BT2KB(tmp);
   1398 	rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
   1399 	hw->fc.high_water[0] = rxpb - size;
   1400 
   1401 	/* Now calculate Low Water */
   1402 	switch (hw->mac.type) {
   1403 	case ixgbe_mac_X540:
   1404 	case ixgbe_mac_X550:
   1405 	case ixgbe_mac_X550EM_x:
   1406 		tmp = IXGBE_LOW_DV_X540(frame);
   1407 		break;
   1408 	default:
   1409 		tmp = IXGBE_LOW_DV(frame);
   1410 		break;
   1411 	}
   1412 	hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
   1413 
   1414 	hw->fc.requested_mode = adapter->fc;
   1415 	hw->fc.pause_time = IXGBE_FC_PAUSE;
   1416 	hw->fc.send_xon = TRUE;
   1417 }
   1418 
   1419 /*
   1420 **
   1421 ** MSIX Interrupt Handlers and Tasklets
   1422 **
   1423 */
   1424 
   1425 static inline void
   1426 ixgbe_enable_queue(struct adapter *adapter, u32 vector)
   1427 {
   1428 	struct ixgbe_hw *hw = &adapter->hw;
   1429 	u64	queue = (u64)(1ULL << vector);
   1430 	u32	mask;
   1431 
   1432 	if (hw->mac.type == ixgbe_mac_82598EB) {
   1433                 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
   1434                 IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
   1435 	} else {
   1436                 mask = (queue & 0xFFFFFFFF);
   1437                 if (mask)
   1438                         IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
   1439                 mask = (queue >> 32);
   1440                 if (mask)
   1441                         IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
   1442 	}
   1443 }
   1444 
   1445 __unused static inline void
   1446 ixgbe_disable_queue(struct adapter *adapter, u32 vector)
   1447 {
   1448 	struct ixgbe_hw *hw = &adapter->hw;
   1449 	u64	queue = (u64)(1ULL << vector);
   1450 	u32	mask;
   1451 
   1452 	if (hw->mac.type == ixgbe_mac_82598EB) {
   1453                 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
   1454                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
   1455 	} else {
   1456                 mask = (queue & 0xFFFFFFFF);
   1457                 if (mask)
   1458                         IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
   1459                 mask = (queue >> 32);
   1460                 if (mask)
   1461                         IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
   1462 	}
   1463 }
   1464 
   1465 static void
   1466 ixgbe_handle_que(void *context)
   1467 {
   1468 	struct ix_queue *que = context;
   1469 	struct adapter  *adapter = que->adapter;
   1470 	struct tx_ring  *txr = que->txr;
   1471 	struct ifnet    *ifp = adapter->ifp;
   1472 
   1473 	adapter->handleq.ev_count++;
   1474 
   1475 	if (ifp->if_flags & IFF_RUNNING) {
   1476 		ixgbe_rxeof(que);
   1477 		IXGBE_TX_LOCK(txr);
   1478 		ixgbe_txeof(txr);
   1479 #ifndef IXGBE_LEGACY_TX
   1480 		if (!drbr_empty(ifp, txr->br))
   1481 			ixgbe_mq_start_locked(ifp, txr);
   1482 #else
   1483 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1484 			ixgbe_start_locked(txr, ifp);
   1485 #endif
   1486 		IXGBE_TX_UNLOCK(txr);
   1487 	}
   1488 
   1489 	/* Reenable this interrupt */
   1490 	if (que->res != NULL)
   1491 		ixgbe_enable_queue(adapter, que->msix);
   1492 	else
   1493 		ixgbe_enable_intr(adapter);
   1494 	return;
   1495 }
   1496 
   1497 
   1498 /*********************************************************************
   1499  *
   1500  *  Legacy Interrupt Service routine
   1501  *
   1502  **********************************************************************/
   1503 
   1504 static int
   1505 ixgbe_legacy_irq(void *arg)
   1506 {
   1507 	struct ix_queue *que = arg;
   1508 	struct adapter	*adapter = que->adapter;
   1509 	struct ixgbe_hw	*hw = &adapter->hw;
   1510 	struct ifnet    *ifp = adapter->ifp;
   1511 	struct 		tx_ring *txr = adapter->tx_rings;
   1512 	bool		more = false;
   1513 	u32       	reg_eicr;
   1514 
   1515 
   1516 	reg_eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
   1517 
   1518 	adapter->stats.pf.legint.ev_count++;
   1519 	++que->irqs.ev_count;
   1520 	if (reg_eicr == 0) {
   1521 		adapter->stats.pf.intzero.ev_count++;
   1522 		if ((ifp->if_flags & IFF_UP) != 0)
   1523 			ixgbe_enable_intr(adapter);
   1524 		return 0;
   1525 	}
   1526 
   1527 	if ((ifp->if_flags & IFF_RUNNING) != 0) {
   1528 #ifdef __NetBSD__
   1529 		/* Don't run ixgbe_rxeof in interrupt context */
   1530 		more = true;
   1531 #else
   1532 		more = ixgbe_rxeof(que);
   1533 #endif
   1534 
   1535 		IXGBE_TX_LOCK(txr);
   1536 		ixgbe_txeof(txr);
   1537 #ifdef IXGBE_LEGACY_TX
   1538 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1539 			ixgbe_start_locked(txr, ifp);
   1540 #else
   1541 		if (!drbr_empty(ifp, txr->br))
   1542 			ixgbe_mq_start_locked(ifp, txr);
   1543 #endif
   1544 		IXGBE_TX_UNLOCK(txr);
   1545 	}
   1546 
   1547 	/* Check for fan failure */
   1548 	if ((hw->device_id == IXGBE_DEV_ID_82598AT) &&
   1549 	    (reg_eicr & IXGBE_EICR_GPI_SDP1)) {
   1550                 device_printf(adapter->dev, "\nCRITICAL: FAN FAILURE!! "
   1551 		    "REPLACE IMMEDIATELY!!\n");
   1552 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
   1553 	}
   1554 
   1555 	/* Link status change */
   1556 	if (reg_eicr & IXGBE_EICR_LSC)
   1557 		softint_schedule(adapter->link_si);
   1558 
   1559 	/* External PHY interrupt */
   1560 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T &&
   1561 	    (reg_eicr & IXGBE_EICR_GPI_SDP0_X540))
   1562 		softint_schedule(adapter->phy_si);
   1563 
   1564 	if (more)
   1565 #ifndef IXGBE_LEGACY_TX
   1566 		softint_schedule(txr->txq_si);
   1567 #else
   1568 		softint_schedule(que->que_si);
   1569 #endif
   1570 	else
   1571 		ixgbe_enable_intr(adapter);
   1572 	return 1;
   1573 }
   1574 
   1575 
   1576 /*********************************************************************
   1577  *
   1578  *  MSIX Queue Interrupt Service routine
   1579  *
   1580  **********************************************************************/
   1581 static int
   1582 ixgbe_msix_que(void *arg)
   1583 {
   1584 	struct ix_queue	*que = arg;
   1585 	struct adapter  *adapter = que->adapter;
   1586 	struct ifnet    *ifp = adapter->ifp;
   1587 	struct tx_ring	*txr = que->txr;
   1588 	struct rx_ring	*rxr = que->rxr;
   1589 	bool		more;
   1590 	u32		newitr = 0;
   1591 
   1592 
   1593 	/* Protect against spurious interrupts */
   1594 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1595 		return 0;
   1596 
   1597 	ixgbe_disable_queue(adapter, que->msix);
   1598 	++que->irqs.ev_count;
   1599 
   1600 #ifdef __NetBSD__
   1601 	/* Don't run ixgbe_rxeof in interrupt context */
   1602 	more = true;
   1603 #else
   1604 	more = ixgbe_rxeof(que);
   1605 #endif
   1606 
   1607 	IXGBE_TX_LOCK(txr);
   1608 	ixgbe_txeof(txr);
   1609 #ifdef IXGBE_LEGACY_TX
   1610 	if (!IFQ_IS_EMPTY(&adapter->ifp->if_snd))
   1611 		ixgbe_start_locked(txr, ifp);
   1612 #else
   1613 	if (!drbr_empty(ifp, txr->br))
   1614 		ixgbe_mq_start_locked(ifp, txr);
   1615 #endif
   1616 	IXGBE_TX_UNLOCK(txr);
   1617 
   1618 	/* Do AIM now? */
   1619 
   1620 	if (ixgbe_enable_aim == FALSE)
   1621 		goto no_calc;
   1622 	/*
   1623 	** Do Adaptive Interrupt Moderation:
   1624         **  - Write out last calculated setting
   1625 	**  - Calculate based on average size over
   1626 	**    the last interval.
   1627 	*/
   1628         if (que->eitr_setting)
   1629                 IXGBE_WRITE_REG(&adapter->hw,
   1630                     IXGBE_EITR(que->msix), que->eitr_setting);
   1631 
   1632         que->eitr_setting = 0;
   1633 
   1634         /* Idle, do nothing */
   1635         if ((txr->bytes == 0) && (rxr->bytes == 0))
   1636                 goto no_calc;
   1637 
   1638 	if ((txr->bytes) && (txr->packets))
   1639                	newitr = txr->bytes/txr->packets;
   1640 	if ((rxr->bytes) && (rxr->packets))
   1641 		newitr = max(newitr,
   1642 		    (rxr->bytes / rxr->packets));
   1643 	newitr += 24; /* account for hardware frame, crc */
   1644 
   1645 	/* set an upper boundary */
   1646 	newitr = min(newitr, 3000);
   1647 
   1648 	/* Be nice to the mid range */
   1649 	if ((newitr > 300) && (newitr < 1200))
   1650 		newitr = (newitr / 3);
   1651 	else
   1652 		newitr = (newitr / 2);
   1653 
   1654         if (adapter->hw.mac.type == ixgbe_mac_82598EB)
   1655                 newitr |= newitr << 16;
   1656         else
   1657                 newitr |= IXGBE_EITR_CNT_WDIS;
   1658 
   1659         /* save for next interrupt */
   1660         que->eitr_setting = newitr;
   1661 
   1662         /* Reset state */
   1663         txr->bytes = 0;
   1664         txr->packets = 0;
   1665         rxr->bytes = 0;
   1666         rxr->packets = 0;
   1667 
   1668 no_calc:
   1669 	if (more)
   1670 		softint_schedule(que->que_si);
   1671 	else
   1672 		ixgbe_enable_queue(adapter, que->msix);
   1673 	return 1;
   1674 }
   1675 
   1676 
   1677 static int
   1678 ixgbe_msix_link(void *arg)
   1679 {
   1680 	struct adapter	*adapter = arg;
   1681 	struct ixgbe_hw *hw = &adapter->hw;
   1682 	u32		reg_eicr, mod_mask;
   1683 
   1684 	++adapter->link_irq.ev_count;
   1685 
   1686 	/* First get the cause */
   1687 	reg_eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
   1688 	/* Be sure the queue bits are not cleared */
   1689 	reg_eicr &= ~IXGBE_EICR_RTX_QUEUE;
   1690 	/* Clear interrupt with write */
   1691 	IXGBE_WRITE_REG(hw, IXGBE_EICR, reg_eicr);
   1692 
   1693 	/* Link status change */
   1694 	if (reg_eicr & IXGBE_EICR_LSC)
   1695 		softint_schedule(adapter->link_si);
   1696 
   1697 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
   1698 #ifdef IXGBE_FDIR
   1699 		if (reg_eicr & IXGBE_EICR_FLOW_DIR) {
   1700 			/* This is probably overkill :) */
   1701 			if (!atomic_cmpset_int(&adapter->fdir_reinit, 0, 1))
   1702 				return 1;
   1703                 	/* Disable the interrupt */
   1704 			IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_FLOW_DIR);
   1705 			softint_schedule(adapter->fdir_si);
   1706 		} else
   1707 #endif
   1708 		if (reg_eicr & IXGBE_EICR_ECC) {
   1709                 	device_printf(adapter->dev, "\nCRITICAL: ECC ERROR!! "
   1710 			    "Please Reboot!!\n");
   1711 			IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
   1712 		}
   1713 
   1714 		/* Check for over temp condition */
   1715 		if (reg_eicr & IXGBE_EICR_TS) {
   1716 			device_printf(adapter->dev, "\nCRITICAL: OVER TEMP!! "
   1717 			    "PHY IS SHUT DOWN!!\n");
   1718 			device_printf(adapter->dev, "System shutdown required!\n");
   1719 			IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
   1720 		}
   1721 #ifdef PCI_IOV
   1722 		if (reg_eicr & IXGBE_EICR_MAILBOX)
   1723 			taskqueue_enqueue(adapter->tq, &adapter->mbx_task);
   1724 #endif
   1725 	}
   1726 
   1727 	/* Pluggable optics-related interrupt */
   1728 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP)
   1729 		mod_mask = IXGBE_EICR_GPI_SDP0_X540;
   1730 	else
   1731 		mod_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
   1732 
   1733 	if (ixgbe_is_sfp(hw)) {
   1734 		if (reg_eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw)) {
   1735 			IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
   1736 			softint_schedule(adapter->msf_si);
   1737 		} else if (reg_eicr & mod_mask) {
   1738 			IXGBE_WRITE_REG(hw, IXGBE_EICR, mod_mask);
   1739 			softint_schedule(adapter->mod_si);
   1740 		}
   1741 	}
   1742 
   1743 	/* Check for fan failure */
   1744 	if ((hw->device_id == IXGBE_DEV_ID_82598AT) &&
   1745 	    (reg_eicr & IXGBE_EICR_GPI_SDP1)) {
   1746 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
   1747                 device_printf(adapter->dev, "\nCRITICAL: FAN FAILURE!! "
   1748 		    "REPLACE IMMEDIATELY!!\n");
   1749 	}
   1750 
   1751 	/* External PHY interrupt */
   1752 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T &&
   1753 	    (reg_eicr & IXGBE_EICR_GPI_SDP0_X540)) {
   1754 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP0_X540);
   1755 		softint_schedule(adapter->phy_si);
   1756  	}
   1757 
   1758 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
   1759 	return 1;
   1760 }
   1761 
   1762 /*********************************************************************
   1763  *
   1764  *  Media Ioctl callback
   1765  *
   1766  *  This routine is called whenever the user queries the status of
   1767  *  the interface using ifconfig.
   1768  *
   1769  **********************************************************************/
   1770 static void
   1771 ixgbe_media_status(struct ifnet * ifp, struct ifmediareq * ifmr)
   1772 {
   1773 	struct adapter *adapter = ifp->if_softc;
   1774 	struct ixgbe_hw *hw = &adapter->hw;
   1775 	int layer;
   1776 
   1777 	INIT_DEBUGOUT("ixgbe_media_status: begin");
   1778 	IXGBE_CORE_LOCK(adapter);
   1779 	ixgbe_update_link_status(adapter);
   1780 
   1781 	ifmr->ifm_status = IFM_AVALID;
   1782 	ifmr->ifm_active = IFM_ETHER;
   1783 
   1784 	if (!adapter->link_active) {
   1785 		IXGBE_CORE_UNLOCK(adapter);
   1786 		return;
   1787 	}
   1788 
   1789 	ifmr->ifm_status |= IFM_ACTIVE;
   1790 	layer = adapter->phy_layer;
   1791 
   1792 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T ||
   1793 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_T ||
   1794 	    layer & IXGBE_PHYSICAL_LAYER_100BASE_TX)
   1795 		switch (adapter->link_speed) {
   1796 		case IXGBE_LINK_SPEED_10GB_FULL:
   1797 			ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
   1798 			break;
   1799 		case IXGBE_LINK_SPEED_1GB_FULL:
   1800 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
   1801 			break;
   1802 		case IXGBE_LINK_SPEED_100_FULL:
   1803 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
   1804 			break;
   1805 		}
   1806 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
   1807 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
   1808 		switch (adapter->link_speed) {
   1809 		case IXGBE_LINK_SPEED_10GB_FULL:
   1810 			ifmr->ifm_active |= IFM_10G_TWINAX | IFM_FDX;
   1811 			break;
   1812 		}
   1813 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR)
   1814 		switch (adapter->link_speed) {
   1815 		case IXGBE_LINK_SPEED_10GB_FULL:
   1816 			ifmr->ifm_active |= IFM_10G_LR | IFM_FDX;
   1817 			break;
   1818 		case IXGBE_LINK_SPEED_1GB_FULL:
   1819 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
   1820 			break;
   1821 		}
   1822 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LRM)
   1823 		switch (adapter->link_speed) {
   1824 		case IXGBE_LINK_SPEED_10GB_FULL:
   1825 			ifmr->ifm_active |= IFM_10G_LRM | IFM_FDX;
   1826 			break;
   1827 		case IXGBE_LINK_SPEED_1GB_FULL:
   1828 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
   1829 			break;
   1830 		}
   1831 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR ||
   1832 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
   1833 		switch (adapter->link_speed) {
   1834 		case IXGBE_LINK_SPEED_10GB_FULL:
   1835 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
   1836 			break;
   1837 		case IXGBE_LINK_SPEED_1GB_FULL:
   1838 			ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
   1839 			break;
   1840 		}
   1841 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
   1842 		switch (adapter->link_speed) {
   1843 		case IXGBE_LINK_SPEED_10GB_FULL:
   1844 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
   1845 			break;
   1846 		}
   1847 	/*
   1848 	** XXX: These need to use the proper media types once
   1849 	** they're added.
   1850 	*/
   1851 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
   1852 		switch (adapter->link_speed) {
   1853 		case IXGBE_LINK_SPEED_10GB_FULL:
   1854 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
   1855 			break;
   1856 		case IXGBE_LINK_SPEED_2_5GB_FULL:
   1857 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
   1858 			break;
   1859 		case IXGBE_LINK_SPEED_1GB_FULL:
   1860 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
   1861 			break;
   1862 		}
   1863 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4
   1864 	    || layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
   1865 		switch (adapter->link_speed) {
   1866 		case IXGBE_LINK_SPEED_10GB_FULL:
   1867 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
   1868 			break;
   1869 		case IXGBE_LINK_SPEED_2_5GB_FULL:
   1870 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
   1871 			break;
   1872 		case IXGBE_LINK_SPEED_1GB_FULL:
   1873 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
   1874 			break;
   1875 		}
   1876 
   1877 	/* If nothing is recognized... */
   1878 #if 0
   1879 	if (IFM_SUBTYPE(ifmr->ifm_active) == 0)
   1880 		ifmr->ifm_active |= IFM_UNKNOWN;
   1881 #endif
   1882 
   1883 	/* Display current flow control setting used on link */
   1884 	if (hw->fc.current_mode == ixgbe_fc_rx_pause ||
   1885 	    hw->fc.current_mode == ixgbe_fc_full)
   1886 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
   1887 	if (hw->fc.current_mode == ixgbe_fc_tx_pause ||
   1888 	    hw->fc.current_mode == ixgbe_fc_full)
   1889 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
   1890 
   1891 	IXGBE_CORE_UNLOCK(adapter);
   1892 
   1893 	return;
   1894 }
   1895 
   1896 /*********************************************************************
   1897  *
   1898  *  Media Ioctl callback
   1899  *
   1900  *  This routine is called when the user changes speed/duplex using
   1901  *  media/mediopt option with ifconfig.
   1902  *
   1903  **********************************************************************/
   1904 static int
   1905 ixgbe_media_change(struct ifnet * ifp)
   1906 {
   1907 	struct adapter *adapter = ifp->if_softc;
   1908 	struct ifmedia *ifm = &adapter->media;
   1909 	struct ixgbe_hw *hw = &adapter->hw;
   1910 	ixgbe_link_speed speed = 0;
   1911 
   1912 	INIT_DEBUGOUT("ixgbe_media_change: begin");
   1913 
   1914 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   1915 		return (EINVAL);
   1916 
   1917 	if (hw->phy.media_type == ixgbe_media_type_backplane)
   1918 		return (EPERM);
   1919 
   1920 	/*
   1921 	** We don't actually need to check against the supported
   1922 	** media types of the adapter; ifmedia will take care of
   1923 	** that for us.
   1924 	*/
   1925 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
   1926 		case IFM_AUTO:
   1927 		case IFM_10G_T:
   1928 			speed |= IXGBE_LINK_SPEED_100_FULL;
   1929 		case IFM_10G_LRM:
   1930 		case IFM_10G_SR: /* KR, too */
   1931 		case IFM_10G_LR:
   1932 		case IFM_10G_CX4: /* KX4 */
   1933 			speed |= IXGBE_LINK_SPEED_1GB_FULL;
   1934 		case IFM_10G_TWINAX:
   1935 			speed |= IXGBE_LINK_SPEED_10GB_FULL;
   1936 			break;
   1937 		case IFM_1000_T:
   1938 			speed |= IXGBE_LINK_SPEED_100_FULL;
   1939 		case IFM_1000_LX:
   1940 		case IFM_1000_SX:
   1941 		case IFM_1000_CX: /* KX */
   1942 			speed |= IXGBE_LINK_SPEED_1GB_FULL;
   1943 			break;
   1944 		case IFM_100_TX:
   1945 			speed |= IXGBE_LINK_SPEED_100_FULL;
   1946 			break;
   1947 		default:
   1948 			goto invalid;
   1949 	}
   1950 
   1951 	hw->mac.autotry_restart = TRUE;
   1952 	hw->mac.ops.setup_link(hw, speed, TRUE);
   1953 	adapter->advertise =
   1954 		((speed & IXGBE_LINK_SPEED_10GB_FULL) << 2) |
   1955 		((speed & IXGBE_LINK_SPEED_1GB_FULL) << 1) |
   1956 		((speed & IXGBE_LINK_SPEED_100_FULL) << 0);
   1957 
   1958 	return (0);
   1959 
   1960 invalid:
   1961 	device_printf(adapter->dev, "Invalid media type!\n");
   1962 	return (EINVAL);
   1963 }
   1964 
   1965 static void
   1966 ixgbe_set_promisc(struct adapter *adapter)
   1967 {
   1968 	struct ether_multi *enm;
   1969 	struct ether_multistep step;
   1970 	u_int32_t       reg_rctl;
   1971 	struct ethercom *ec = &adapter->osdep.ec;
   1972 	struct ifnet   *ifp = adapter->ifp;
   1973 	int		mcnt = 0;
   1974 
   1975 	reg_rctl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
   1976 	reg_rctl &= (~IXGBE_FCTRL_UPE);
   1977 	if (ifp->if_flags & IFF_ALLMULTI)
   1978 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
   1979 	else {
   1980 		ETHER_FIRST_MULTI(step, ec, enm);
   1981 		while (enm != NULL) {
   1982 			if (mcnt == MAX_NUM_MULTICAST_ADDRESSES)
   1983 				break;
   1984 			mcnt++;
   1985 			ETHER_NEXT_MULTI(step, enm);
   1986 		}
   1987 	}
   1988 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
   1989 		reg_rctl &= (~IXGBE_FCTRL_MPE);
   1990 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, reg_rctl);
   1991 
   1992 	if (ifp->if_flags & IFF_PROMISC) {
   1993 		reg_rctl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   1994 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, reg_rctl);
   1995 	} else if (ifp->if_flags & IFF_ALLMULTI) {
   1996 		reg_rctl |= IXGBE_FCTRL_MPE;
   1997 		reg_rctl &= ~IXGBE_FCTRL_UPE;
   1998 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, reg_rctl);
   1999 	}
   2000 	return;
   2001 }
   2002 
   2003 
   2004 /*********************************************************************
   2005  *  Multicast Update
   2006  *
   2007  *  This routine is called whenever multicast address list is updated.
   2008  *
   2009  **********************************************************************/
   2010 #define IXGBE_RAR_ENTRIES 16
   2011 
   2012 static void
   2013 ixgbe_set_multi(struct adapter *adapter)
   2014 {
   2015 	u32			fctrl;
   2016 	u8			*update_ptr;
   2017 	struct ixgbe_mc_addr	*mta;
   2018 	int			mcnt = 0;
   2019 	struct ifnet		*ifp = adapter->ifp;
   2020 	struct ethercom		*ec = &adapter->osdep.ec;
   2021 	struct ether_multi	*enm;
   2022 	struct ether_multistep	step;
   2023 
   2024 	IOCTL_DEBUGOUT("ixgbe_set_multi: begin");
   2025 
   2026 	mta = adapter->mta;
   2027 	bzero(mta, sizeof(*mta) * MAX_NUM_MULTICAST_ADDRESSES);
   2028 
   2029 	ifp->if_flags &= ~IFF_ALLMULTI;
   2030 	ETHER_FIRST_MULTI(step, ec, enm);
   2031 	while (enm != NULL) {
   2032 		if ((mcnt == MAX_NUM_MULTICAST_ADDRESSES) ||
   2033 		    (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   2034 			ETHER_ADDR_LEN) != 0)) {
   2035 			ifp->if_flags |= IFF_ALLMULTI;
   2036 			break;
   2037 		}
   2038 		bcopy(enm->enm_addrlo,
   2039 		    mta[mcnt].addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
   2040 		mta[mcnt].vmdq = adapter->pool;
   2041 		mcnt++;
   2042 		ETHER_NEXT_MULTI(step, enm);
   2043 	}
   2044 
   2045 	fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
   2046 	fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   2047 	if (ifp->if_flags & IFF_PROMISC)
   2048 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   2049 	else if (ifp->if_flags & IFF_ALLMULTI) {
   2050 		fctrl |= IXGBE_FCTRL_MPE;
   2051 	}
   2052 
   2053 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
   2054 
   2055 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES) {
   2056 		update_ptr = (u8 *)mta;
   2057 		ixgbe_update_mc_addr_list(&adapter->hw,
   2058 		    update_ptr, mcnt, ixgbe_mc_array_itr, TRUE);
   2059 	}
   2060 
   2061 	return;
   2062 }
   2063 
   2064 /*
   2065  * This is an iterator function now needed by the multicast
   2066  * shared code. It simply feeds the shared code routine the
   2067  * addresses in the array of ixgbe_set_multi() one by one.
   2068  */
   2069 static u8 *
   2070 ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
   2071 {
   2072 	struct ixgbe_mc_addr *mta;
   2073 
   2074 	mta = (struct ixgbe_mc_addr *)*update_ptr;
   2075 	*vmdq = mta->vmdq;
   2076 
   2077 	*update_ptr = (u8*)(mta + 1);;
   2078 	return (mta->addr);
   2079 }
   2080 
   2081 
   2082 /*********************************************************************
   2083  *  Timer routine
   2084  *
   2085  *  This routine checks for link status,updates statistics,
   2086  *  and runs the watchdog check.
   2087  *
   2088  **********************************************************************/
   2089 
   2090 static void
   2091 ixgbe_local_timer1(void *arg)
   2092 {
   2093 	struct adapter	*adapter = arg;
   2094 	device_t	dev = adapter->dev;
   2095 	struct ix_queue *que = adapter->queues;
   2096 	u64		queues = 0;
   2097 	int		hung = 0;
   2098 
   2099 	KASSERT(mutex_owned(&adapter->core_mtx));
   2100 
   2101 	/* Check for pluggable optics */
   2102 	if (adapter->sfp_probe)
   2103 		if (!ixgbe_sfp_probe(adapter))
   2104 			goto out; /* Nothing to do */
   2105 
   2106 	ixgbe_update_link_status(adapter);
   2107 	ixgbe_update_stats_counters(adapter);
   2108 
   2109 	/*
   2110 	** Check the TX queues status
   2111 	**	- mark hung queues so we don't schedule on them
   2112 	**      - watchdog only if all queues show hung
   2113 	*/
   2114 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   2115 		/* Keep track of queues with work for soft irq */
   2116 		if (que->txr->busy)
   2117 			queues |= ((u64)1 << que->me);
   2118 		/*
   2119 		** Each time txeof runs without cleaning, but there
   2120 		** are uncleaned descriptors it increments busy. If
   2121 		** we get to the MAX we declare it hung.
   2122 		*/
   2123 		if (que->busy == IXGBE_QUEUE_HUNG) {
   2124 			++hung;
   2125 			/* Mark the queue as inactive */
   2126 			adapter->active_queues &= ~((u64)1 << que->me);
   2127 			continue;
   2128 		} else {
   2129 			/* Check if we've come back from hung */
   2130 			if ((adapter->active_queues & ((u64)1 << que->me)) == 0)
   2131                                 adapter->active_queues |= ((u64)1 << que->me);
   2132 		}
   2133 		if (que->busy >= IXGBE_MAX_TX_BUSY) {
   2134 			device_printf(dev,"Warning queue %d "
   2135 			    "appears to be hung!\n", i);
   2136 			que->txr->busy = IXGBE_QUEUE_HUNG;
   2137 			++hung;
   2138 		}
   2139 
   2140 	}
   2141 	/* Only truely watchdog if all queues show hung */
   2142 	if (hung == adapter->num_queues)
   2143 		goto watchdog;
   2144 	else if (queues != 0) { /* Force an IRQ on queues with work */
   2145 		ixgbe_rearm_queues(adapter, queues);
   2146 	}
   2147 
   2148 out:
   2149 	callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
   2150 	return;
   2151 
   2152 watchdog:
   2153 	device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
   2154 	adapter->ifp->if_flags &= ~IFF_RUNNING;
   2155 	adapter->watchdog_events.ev_count++;
   2156 	ixgbe_init_locked(adapter);
   2157 }
   2158 
   2159 static void
   2160 ixgbe_local_timer(void *arg)
   2161 {
   2162 	struct adapter *adapter = arg;
   2163 
   2164 	IXGBE_CORE_LOCK(adapter);
   2165 	ixgbe_local_timer1(adapter);
   2166 	IXGBE_CORE_UNLOCK(adapter);
   2167 }
   2168 
   2169 
   2170 /*
   2171 ** Note: this routine updates the OS on the link state
   2172 **	the real check of the hardware only happens with
   2173 **	a link interrupt.
   2174 */
   2175 static void
   2176 ixgbe_update_link_status(struct adapter *adapter)
   2177 {
   2178 	struct ifnet	*ifp = adapter->ifp;
   2179 	device_t dev = adapter->dev;
   2180 
   2181 	if (adapter->link_up){
   2182 		if (adapter->link_active == FALSE) {
   2183 			if (bootverbose)
   2184 				device_printf(dev,"Link is up %d Gbps %s \n",
   2185 				    ((adapter->link_speed == 128)? 10:1),
   2186 				    "Full Duplex");
   2187 			adapter->link_active = TRUE;
   2188 			/* Update any Flow Control changes */
   2189 			ixgbe_fc_enable(&adapter->hw);
   2190 			/* Update DMA coalescing config */
   2191 			ixgbe_config_dmac(adapter);
   2192 			if_link_state_change(ifp, LINK_STATE_UP);
   2193 #ifdef PCI_IOV
   2194 			ixgbe_ping_all_vfs(adapter);
   2195 #endif
   2196 		}
   2197 	} else { /* Link down */
   2198 		if (adapter->link_active == TRUE) {
   2199 			if (bootverbose)
   2200 				device_printf(dev,"Link is Down\n");
   2201 			if_link_state_change(ifp, LINK_STATE_DOWN);
   2202 			adapter->link_active = FALSE;
   2203 #ifdef PCI_IOV
   2204 			ixgbe_ping_all_vfs(adapter);
   2205 #endif
   2206 		}
   2207 	}
   2208 
   2209 	return;
   2210 }
   2211 
   2212 
   2213 static void
   2214 ixgbe_ifstop(struct ifnet *ifp, int disable)
   2215 {
   2216 	struct adapter *adapter = ifp->if_softc;
   2217 
   2218 	IXGBE_CORE_LOCK(adapter);
   2219 	ixgbe_stop(adapter);
   2220 	IXGBE_CORE_UNLOCK(adapter);
   2221 }
   2222 
   2223 /*********************************************************************
   2224  *
   2225  *  This routine disables all traffic on the adapter by issuing a
   2226  *  global reset on the MAC and deallocates TX/RX buffers.
   2227  *
   2228  **********************************************************************/
   2229 
   2230 static void
   2231 ixgbe_stop(void *arg)
   2232 {
   2233 	struct ifnet   *ifp;
   2234 	struct adapter *adapter = arg;
   2235 	struct ixgbe_hw *hw = &adapter->hw;
   2236 	ifp = adapter->ifp;
   2237 
   2238 	KASSERT(mutex_owned(&adapter->core_mtx));
   2239 
   2240 	INIT_DEBUGOUT("ixgbe_stop: begin\n");
   2241 	ixgbe_disable_intr(adapter);
   2242 	callout_stop(&adapter->timer);
   2243 
   2244 	/* Let the stack know...*/
   2245 	ifp->if_flags &= ~IFF_RUNNING;
   2246 
   2247 	ixgbe_reset_hw(hw);
   2248 	hw->adapter_stopped = FALSE;
   2249 	ixgbe_stop_adapter(hw);
   2250 	if (hw->mac.type == ixgbe_mac_82599EB)
   2251 		ixgbe_stop_mac_link_on_d3_82599(hw);
   2252 	/* Turn off the laser - noop with no optics */
   2253 	ixgbe_disable_tx_laser(hw);
   2254 
   2255 	/* Update the stack */
   2256 	adapter->link_up = FALSE;
   2257 	ixgbe_update_link_status(adapter);
   2258 
   2259 	/* reprogram the RAR[0] in case user changed it. */
   2260 	ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
   2261 
   2262 	return;
   2263 }
   2264 
   2265 
   2266 /*********************************************************************
   2267  *
   2268  *  Determine hardware revision.
   2269  *
   2270  **********************************************************************/
   2271 static void
   2272 ixgbe_identify_hardware(struct adapter *adapter)
   2273 {
   2274 	pcitag_t tag;
   2275 	pci_chipset_tag_t pc;
   2276 	pcireg_t subid, id;
   2277 	struct ixgbe_hw *hw = &adapter->hw;
   2278 
   2279 	pc = adapter->osdep.pc;
   2280 	tag = adapter->osdep.tag;
   2281 
   2282 	id = pci_conf_read(pc, tag, PCI_ID_REG);
   2283 	subid = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG);
   2284 
   2285 	/* Save off the information about this board */
   2286 	hw->vendor_id = PCI_VENDOR(id);
   2287 	hw->device_id = PCI_PRODUCT(id);
   2288 	hw->revision_id =
   2289 	    PCI_REVISION(pci_conf_read(pc, tag, PCI_CLASS_REG));
   2290 	hw->subsystem_vendor_id = PCI_SUBSYS_VENDOR(subid);
   2291 	hw->subsystem_device_id = PCI_SUBSYS_ID(subid);
   2292 
   2293 	/*
   2294 	** Make sure BUSMASTER is set
   2295 	*/
   2296 	ixgbe_pci_enable_busmaster(pc, tag);
   2297 
   2298 	/* We need this here to set the num_segs below */
   2299 	ixgbe_set_mac_type(hw);
   2300 
   2301 	/* Pick up the 82599 settings */
   2302 	if (hw->mac.type != ixgbe_mac_82598EB) {
   2303 		hw->phy.smart_speed = ixgbe_smart_speed;
   2304 		adapter->num_segs = IXGBE_82599_SCATTER;
   2305 	} else
   2306 		adapter->num_segs = IXGBE_82598_SCATTER;
   2307 
   2308 	return;
   2309 }
   2310 
   2311 /*********************************************************************
   2312  *
   2313  *  Determine optic type
   2314  *
   2315  **********************************************************************/
   2316 static void
   2317 ixgbe_setup_optics(struct adapter *adapter)
   2318 {
   2319 	struct ixgbe_hw *hw = &adapter->hw;
   2320 	int		layer;
   2321 
   2322 	layer = adapter->phy_layer = ixgbe_get_supported_physical_layer(hw);
   2323 
   2324 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T) {
   2325 		adapter->optics = IFM_10G_T;
   2326 		return;
   2327 	}
   2328 
   2329 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T) {
   2330 		adapter->optics = IFM_1000_T;
   2331 		return;
   2332 	}
   2333 
   2334 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX) {
   2335 		adapter->optics = IFM_1000_SX;
   2336 		return;
   2337 	}
   2338 
   2339 	if (layer & (IXGBE_PHYSICAL_LAYER_10GBASE_LR |
   2340 	    IXGBE_PHYSICAL_LAYER_10GBASE_LRM)) {
   2341 		adapter->optics = IFM_10G_LR;
   2342 		return;
   2343 	}
   2344 
   2345 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
   2346 		adapter->optics = IFM_10G_SR;
   2347 		return;
   2348 	}
   2349 
   2350 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU) {
   2351 		adapter->optics = IFM_10G_TWINAX;
   2352 		return;
   2353 	}
   2354 
   2355 	if (layer & (IXGBE_PHYSICAL_LAYER_10GBASE_KX4 |
   2356 	    IXGBE_PHYSICAL_LAYER_10GBASE_CX4)) {
   2357 		adapter->optics = IFM_10G_CX4;
   2358 		return;
   2359 	}
   2360 
   2361 	/* If we get here just set the default */
   2362 	adapter->optics = IFM_ETHER | IFM_AUTO;
   2363 	return;
   2364 }
   2365 
   2366 /*********************************************************************
   2367  *
   2368  *  Setup the Legacy or MSI Interrupt handler
   2369  *
   2370  **********************************************************************/
   2371 static int
   2372 ixgbe_allocate_legacy(struct adapter *adapter,
   2373     const struct pci_attach_args *pa)
   2374 {
   2375 	device_t	dev = adapter->dev;
   2376 	struct		ix_queue *que = adapter->queues;
   2377 #ifndef IXGBE_LEGACY_TX
   2378 	struct tx_ring		*txr = adapter->tx_rings;
   2379 #endif
   2380 	int		counts[PCI_INTR_TYPE_SIZE];
   2381 	pci_intr_type_t intr_type, max_type;
   2382 	char intrbuf[PCI_INTRSTR_LEN];
   2383 	const char	*intrstr = NULL;
   2384 
   2385 	/* Allocation settings */
   2386 	max_type = PCI_INTR_TYPE_MSI;
   2387 	counts[PCI_INTR_TYPE_MSIX] = 0;
   2388 	counts[PCI_INTR_TYPE_MSI] = 1;
   2389 	counts[PCI_INTR_TYPE_INTX] = 1;
   2390 
   2391 alloc_retry:
   2392 	if (pci_intr_alloc(pa, &adapter->osdep.intrs, counts, max_type) != 0) {
   2393 		aprint_error_dev(dev, "couldn't alloc interrupt\n");
   2394 		return ENXIO;
   2395 	}
   2396 	adapter->osdep.nintrs = 1;
   2397 	intrstr = pci_intr_string(adapter->osdep.pc, adapter->osdep.intrs[0],
   2398 	    intrbuf, sizeof(intrbuf));
   2399 	adapter->osdep.ihs[0] = pci_intr_establish_xname(adapter->osdep.pc,
   2400 	    adapter->osdep.intrs[0], IPL_NET, ixgbe_legacy_irq, que,
   2401 	    device_xname(dev));
   2402 	if (adapter->osdep.ihs[0] == NULL) {
   2403 		intr_type = pci_intr_type(adapter->osdep.pc,
   2404 		    adapter->osdep.intrs[0]);
   2405 		aprint_error_dev(dev,"unable to establish %s\n",
   2406 		    (intr_type == PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
   2407 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   2408 		switch (intr_type) {
   2409 		case PCI_INTR_TYPE_MSI:
   2410 			/* The next try is for INTx: Disable MSI */
   2411 			max_type = PCI_INTR_TYPE_INTX;
   2412 			counts[PCI_INTR_TYPE_INTX] = 1;
   2413 			goto alloc_retry;
   2414 		case PCI_INTR_TYPE_INTX:
   2415 		default:
   2416 			/* See below */
   2417 			break;
   2418 		}
   2419 	}
   2420 	if (adapter->osdep.ihs[0] == NULL) {
   2421 		aprint_error_dev(dev,
   2422 		    "couldn't establish interrupt%s%s\n",
   2423 		    intrstr ? " at " : "", intrstr ? intrstr : "");
   2424 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   2425 		return ENXIO;
   2426 	}
   2427 	aprint_normal_dev(dev, "interrupting at %s\n", intrstr);
   2428 	/*
   2429 	 * Try allocating a fast interrupt and the associated deferred
   2430 	 * processing contexts.
   2431 	 */
   2432 #ifndef IXGBE_LEGACY_TX
   2433 	txr->txq_si = softint_establish(SOFTINT_NET, ixgbe_deferred_mq_start,
   2434 	    txr);
   2435 #endif
   2436 	que->que_si = softint_establish(SOFTINT_NET, ixgbe_handle_que, que);
   2437 
   2438 	/* Tasklets for Link, SFP and Multispeed Fiber */
   2439 	adapter->link_si =
   2440 	    softint_establish(SOFTINT_NET, ixgbe_handle_link, adapter);
   2441 	adapter->mod_si =
   2442 	    softint_establish(SOFTINT_NET, ixgbe_handle_mod, adapter);
   2443 	adapter->msf_si =
   2444 	    softint_establish(SOFTINT_NET, ixgbe_handle_msf, adapter);
   2445 	adapter->phy_si =
   2446 	    softint_establish(SOFTINT_NET, ixgbe_handle_phy, adapter);
   2447 
   2448 #ifdef IXGBE_FDIR
   2449 	adapter->fdir_si =
   2450 	    softint_establish(SOFTINT_NET, ixgbe_reinit_fdir, adapter);
   2451 #endif
   2452 	if (que->que_si == NULL ||
   2453 	    adapter->link_si == NULL ||
   2454 	    adapter->mod_si == NULL ||
   2455 #ifdef IXGBE_FDIR
   2456 	    adapter->fdir_si == NULL ||
   2457 #endif
   2458 	    adapter->msf_si == NULL) {
   2459 		aprint_error_dev(dev,
   2460 		    "could not establish software interrupts\n");
   2461 		return ENXIO;
   2462 	}
   2463 
   2464 	/* For simplicity in the handlers */
   2465 	adapter->active_queues = IXGBE_EIMS_ENABLE_MASK;
   2466 
   2467 	return (0);
   2468 }
   2469 
   2470 
   2471 /*********************************************************************
   2472  *
   2473  *  Setup MSIX Interrupt resources and handlers
   2474  *
   2475  **********************************************************************/
   2476 static int
   2477 ixgbe_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
   2478 {
   2479 	device_t        dev = adapter->dev;
   2480 	struct 		ix_queue *que = adapter->queues;
   2481 	struct  	tx_ring *txr = adapter->tx_rings;
   2482 	pci_chipset_tag_t pc;
   2483 	char		intrbuf[PCI_INTRSTR_LEN];
   2484 	char		intr_xname[32];
   2485 	const char	*intrstr = NULL;
   2486 	int 		error, vector = 0;
   2487 	int		cpu_id = 0;
   2488 	kcpuset_t	*affinity;
   2489 #ifdef	RSS
   2490 	cpuset_t	cpu_mask;
   2491 #endif
   2492 
   2493 	pc = adapter->osdep.pc;
   2494 #ifdef	RSS
   2495 	/*
   2496 	 * If we're doing RSS, the number of queues needs to
   2497 	 * match the number of RSS buckets that are configured.
   2498 	 *
   2499 	 * + If there's more queues than RSS buckets, we'll end
   2500 	 *   up with queues that get no traffic.
   2501 	 *
   2502 	 * + If there's more RSS buckets than queues, we'll end
   2503 	 *   up having multiple RSS buckets map to the same queue,
   2504 	 *   so there'll be some contention.
   2505 	 */
   2506 	if (adapter->num_queues != rss_getnumbuckets()) {
   2507 		device_printf(dev,
   2508 		    "%s: number of queues (%d) != number of RSS buckets (%d)"
   2509 		    "; performance will be impacted.\n",
   2510 		    __func__,
   2511 		    adapter->num_queues,
   2512 		    rss_getnumbuckets());
   2513 	}
   2514 #endif
   2515 
   2516 	adapter->osdep.nintrs = adapter->num_queues + 1;
   2517 	if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
   2518 	    adapter->osdep.nintrs) != 0) {
   2519 		aprint_error_dev(dev,
   2520 		    "failed to allocate MSI-X interrupt\n");
   2521 		return (ENXIO);
   2522 	}
   2523 
   2524 	kcpuset_create(&affinity, false);
   2525 	for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
   2526 		snprintf(intr_xname, sizeof(intr_xname), "%s TX/RX",
   2527 		    device_xname(dev));
   2528 		intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
   2529 		    sizeof(intrbuf));
   2530 #ifdef IXG_MPSAFE
   2531 		pci_intr_setattr(pc, adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
   2532 		    true);
   2533 #endif
   2534 		/* Set the handler function */
   2535 		que->res = adapter->osdep.ihs[i] = pci_intr_establish_xname(pc,
   2536 		    adapter->osdep.intrs[i], IPL_NET, ixgbe_msix_que, que,
   2537 		    intr_xname);
   2538 		if (que->res == NULL) {
   2539 			pci_intr_release(pc, adapter->osdep.intrs,
   2540 			    adapter->osdep.nintrs);
   2541 			aprint_error_dev(dev,
   2542 			    "Failed to register QUE handler\n");
   2543 			kcpuset_destroy(affinity);
   2544 			return ENXIO;
   2545 		}
   2546 		que->msix = vector;
   2547 		adapter->active_queues |= (u64)(1 << que->msix);
   2548 #ifdef	RSS
   2549 		/*
   2550 		 * The queue ID is used as the RSS layer bucket ID.
   2551 		 * We look up the queue ID -> RSS CPU ID and select
   2552 		 * that.
   2553 		 */
   2554 		cpu_id = rss_getcpu(i % rss_getnumbuckets());
   2555 #else
   2556 		/*
   2557 		 * Bind the msix vector, and thus the
   2558 		 * rings to the corresponding cpu.
   2559 		 *
   2560 		 * This just happens to match the default RSS round-robin
   2561 		 * bucket -> queue -> CPU allocation.
   2562 		 */
   2563 		if (adapter->num_queues > 1)
   2564 			cpu_id = i;
   2565 #endif
   2566 		/* Round-robin affinity */
   2567 		kcpuset_zero(affinity);
   2568 		kcpuset_set(affinity, cpu_id % ncpu);
   2569 		error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
   2570 		    NULL);
   2571 		aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
   2572 		    intrstr);
   2573 		if (error == 0) {
   2574 #ifdef IXGBE_DEBUG
   2575 #ifdef	RSS
   2576 			aprintf_normal(
   2577 			    ", bound RSS bucket %d to CPU %d\n",
   2578 			    i, cpu_id);
   2579 #else
   2580 			aprint_normal(
   2581 			    ", bound queue %d to cpu %d\n",
   2582 			    i, cpu_id);
   2583 #endif
   2584 #endif /* IXGBE_DEBUG */
   2585 		} else
   2586 			aprint_normal("\n");
   2587 #ifndef IXGBE_LEGACY_TX
   2588 		txr->txq_si = softint_establish(SOFTINT_NET,
   2589 		    ixgbe_deferred_mq_start, txr);
   2590 #endif
   2591 		que->que_si = softint_establish(SOFTINT_NET, ixgbe_handle_que,
   2592 		    que);
   2593 		if (que->que_si == NULL) {
   2594 			aprint_error_dev(dev,
   2595 			    "could not establish software interrupt\n");
   2596 		}
   2597 	}
   2598 
   2599 	/* and Link */
   2600 	cpu_id++;
   2601 	snprintf(intr_xname, sizeof(intr_xname), "%s link", device_xname(dev));
   2602 	intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
   2603 	    sizeof(intrbuf));
   2604 #ifdef IXG_MPSAFE
   2605 	pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
   2606 	    true);
   2607 #endif
   2608 	/* Set the link handler function */
   2609 	adapter->osdep.ihs[vector] = pci_intr_establish_xname(pc,
   2610 	    adapter->osdep.intrs[vector], IPL_NET, ixgbe_msix_link, adapter,
   2611 	    intr_xname);
   2612 	if (adapter->osdep.ihs[vector] == NULL) {
   2613 		adapter->res = NULL;
   2614 		aprint_error_dev(dev, "Failed to register LINK handler\n");
   2615 		kcpuset_destroy(affinity);
   2616 		return (ENXIO);
   2617 	}
   2618 	/* Round-robin affinity */
   2619 	kcpuset_zero(affinity);
   2620 	kcpuset_set(affinity, cpu_id % ncpu);
   2621 	error = interrupt_distribute(adapter->osdep.ihs[vector], affinity,NULL);
   2622 
   2623 	aprint_normal_dev(dev,
   2624 	    "for link, interrupting at %s", intrstr);
   2625 	if (error == 0)
   2626 		aprint_normal(", affinity to cpu %d\n", cpu_id);
   2627 	else
   2628 		aprint_normal("\n");
   2629 
   2630 	adapter->vector = vector;
   2631 	/* Tasklets for Link, SFP and Multispeed Fiber */
   2632 	adapter->link_si =
   2633 	    softint_establish(SOFTINT_NET, ixgbe_handle_link, adapter);
   2634 	adapter->mod_si =
   2635 	    softint_establish(SOFTINT_NET, ixgbe_handle_mod, adapter);
   2636 	adapter->msf_si =
   2637 	    softint_establish(SOFTINT_NET, ixgbe_handle_msf, adapter);
   2638 #ifdef PCI_IOV
   2639 	TASK_INIT(&adapter->mbx_task, 0, ixgbe_handle_mbx, adapter);
   2640 #endif
   2641 	adapter->phy_si =
   2642 	    softint_establish(SOFTINT_NET, ixgbe_handle_phy, adapter);
   2643 #ifdef IXGBE_FDIR
   2644 	adapter->fdir_si =
   2645 	    softint_establish(SOFTINT_NET, ixgbe_reinit_fdir, adapter);
   2646 #endif
   2647 
   2648 	kcpuset_destroy(affinity);
   2649 	return (0);
   2650 }
   2651 
   2652 /*
   2653  * Setup Either MSI/X or MSI
   2654  */
   2655 static int
   2656 ixgbe_setup_msix(struct adapter *adapter)
   2657 {
   2658 	device_t dev = adapter->dev;
   2659 	int want, queues, msgs;
   2660 
   2661 	/* Override by tuneable */
   2662 	if (ixgbe_enable_msix == 0)
   2663 		goto msi;
   2664 
   2665 	/* First try MSI/X */
   2666 	msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
   2667 	if (msgs < IXG_MSIX_NINTR)
   2668 		goto msi;
   2669 
   2670 	adapter->msix_mem = (void *)1; /* XXX */
   2671 
   2672 	/* Figure out a reasonable auto config value */
   2673 	queues = (ncpu > (msgs-1)) ? (msgs-1) : ncpu;
   2674 
   2675 #ifdef	RSS
   2676 	/* If we're doing RSS, clamp at the number of RSS buckets */
   2677 	if (queues > rss_getnumbuckets())
   2678 		queues = rss_getnumbuckets();
   2679 #endif
   2680 
   2681 	if (ixgbe_num_queues != 0)
   2682 		queues = ixgbe_num_queues;
   2683 
   2684 	/* reflect correct sysctl value */
   2685 	ixgbe_num_queues = queues;
   2686 
   2687 	/*
   2688 	** Want one vector (RX/TX pair) per queue
   2689 	** plus an additional for Link.
   2690 	*/
   2691 	want = queues + 1;
   2692 	if (msgs >= want)
   2693 		msgs = want;
   2694 	else {
   2695                	aprint_error_dev(dev,
   2696 		    "MSIX Configuration Problem, "
   2697 		    "%d vectors but %d queues wanted!\n",
   2698 		    msgs, want);
   2699 		goto msi;
   2700 	}
   2701 	device_printf(dev,
   2702 	    "Using MSIX interrupts with %d vectors\n", msgs);
   2703 	adapter->num_queues = queues;
   2704 	return (msgs);
   2705 
   2706 	/*
   2707 	** If MSIX alloc failed or provided us with
   2708 	** less than needed, free and fall through to MSI
   2709 	*/
   2710 msi:
   2711        	msgs = pci_msi_count(adapter->osdep.pc, adapter->osdep.tag);
   2712 	adapter->msix_mem = NULL; /* XXX */
   2713        	msgs = 1;
   2714 	aprint_normal_dev(dev,"Using an MSI interrupt\n");
   2715 	return (msgs);
   2716 }
   2717 
   2718 
   2719 static int
   2720 ixgbe_allocate_pci_resources(struct adapter *adapter,
   2721     const struct pci_attach_args *pa)
   2722 {
   2723 	pcireg_t	memtype;
   2724 	device_t        dev = adapter->dev;
   2725 	bus_addr_t addr;
   2726 	int flags;
   2727 
   2728 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
   2729 	switch (memtype) {
   2730 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   2731 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   2732 		adapter->osdep.mem_bus_space_tag = pa->pa_memt;
   2733 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
   2734 	              memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
   2735 			goto map_err;
   2736 		if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
   2737 			aprint_normal_dev(dev, "clearing prefetchable bit\n");
   2738 			flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
   2739 		}
   2740 		if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
   2741 		     adapter->osdep.mem_size, flags,
   2742 		     &adapter->osdep.mem_bus_space_handle) != 0) {
   2743 map_err:
   2744 			adapter->osdep.mem_size = 0;
   2745 			aprint_error_dev(dev, "unable to map BAR0\n");
   2746 			return ENXIO;
   2747 		}
   2748 		break;
   2749 	default:
   2750 		aprint_error_dev(dev, "unexpected type on BAR0\n");
   2751 		return ENXIO;
   2752 	}
   2753 
   2754 	/* Legacy defaults */
   2755 	adapter->num_queues = 1;
   2756 	adapter->hw.back = &adapter->osdep;
   2757 
   2758 	/*
   2759 	** Now setup MSI or MSI/X, should
   2760 	** return us the number of supported
   2761 	** vectors. (Will be 1 for MSI)
   2762 	*/
   2763 	adapter->msix = ixgbe_setup_msix(adapter);
   2764 	return (0);
   2765 }
   2766 
   2767 static void
   2768 ixgbe_free_pci_resources(struct adapter * adapter)
   2769 {
   2770 	struct 		ix_queue *que = adapter->queues;
   2771 	int		rid;
   2772 
   2773 	/*
   2774 	**  Release all msix queue resources:
   2775 	*/
   2776 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   2777 		if (que->res != NULL)
   2778 			pci_intr_disestablish(adapter->osdep.pc,
   2779 			    adapter->osdep.ihs[i]);
   2780 	}
   2781 
   2782 
   2783 	/* Clean the Legacy or Link interrupt last */
   2784 	if (adapter->vector) /* we are doing MSIX */
   2785 		rid = adapter->vector;
   2786 	else
   2787 		rid = 0;
   2788 
   2789 	if (adapter->osdep.ihs[rid] != NULL) {
   2790 		pci_intr_disestablish(adapter->osdep.pc,
   2791 		    adapter->osdep.ihs[rid]);
   2792 		adapter->osdep.ihs[rid] = NULL;
   2793 	}
   2794 
   2795 	pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
   2796 	    adapter->osdep.nintrs);
   2797 
   2798 	if (adapter->osdep.mem_size != 0) {
   2799 		bus_space_unmap(adapter->osdep.mem_bus_space_tag,
   2800 		    adapter->osdep.mem_bus_space_handle,
   2801 		    adapter->osdep.mem_size);
   2802 	}
   2803 
   2804 	return;
   2805 }
   2806 
   2807 /*********************************************************************
   2808  *
   2809  *  Setup networking device structure and register an interface.
   2810  *
   2811  **********************************************************************/
   2812 static int
   2813 ixgbe_setup_interface(device_t dev, struct adapter *adapter)
   2814 {
   2815 	struct ethercom *ec = &adapter->osdep.ec;
   2816 	struct ifnet   *ifp;
   2817 
   2818 	INIT_DEBUGOUT("ixgbe_setup_interface: begin");
   2819 
   2820 	ifp = adapter->ifp = &ec->ec_if;
   2821 	strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
   2822 	ifp->if_baudrate = IF_Gbps(10);
   2823 	ifp->if_init = ixgbe_init;
   2824 	ifp->if_stop = ixgbe_ifstop;
   2825 	ifp->if_softc = adapter;
   2826 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   2827 	ifp->if_ioctl = ixgbe_ioctl;
   2828 #if __FreeBSD_version >= 1100045
   2829 	/* TSO parameters */
   2830 	ifp->if_hw_tsomax = 65518;
   2831 	ifp->if_hw_tsomaxsegcount = IXGBE_82599_SCATTER;
   2832 	ifp->if_hw_tsomaxsegsize = 2048;
   2833 #endif
   2834 #ifndef IXGBE_LEGACY_TX
   2835 	ifp->if_transmit = ixgbe_mq_start;
   2836 	ifp->if_qflush = ixgbe_qflush;
   2837 #else
   2838 	ifp->if_start = ixgbe_start;
   2839 	IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
   2840 #if 0
   2841 	ifp->if_snd.ifq_drv_maxlen = adapter->num_tx_desc - 2;
   2842 #endif
   2843 	IFQ_SET_READY(&ifp->if_snd);
   2844 #endif
   2845 
   2846 	if_initialize(ifp);
   2847 	ether_ifattach(ifp, adapter->hw.mac.addr);
   2848 	if_register(ifp);
   2849 	ether_set_ifflags_cb(ec, ixgbe_ifflags_cb);
   2850 
   2851 	adapter->max_frame_size =
   2852 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   2853 
   2854 	/*
   2855 	 * Tell the upper layer(s) we support long frames.
   2856 	 */
   2857 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
   2858 
   2859 	ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSOv4 | IFCAP_TSOv6;
   2860 	ifp->if_capenable = 0;
   2861 
   2862 	ec->ec_capabilities |= ETHERCAP_VLAN_HWCSUM;
   2863 	ec->ec_capabilities |= ETHERCAP_JUMBO_MTU;
   2864 	ifp->if_capabilities |= IFCAP_LRO;
   2865 	ec->ec_capabilities |= ETHERCAP_VLAN_HWTAGGING
   2866 	    		    | ETHERCAP_VLAN_MTU;
   2867 	ec->ec_capenable = ec->ec_capabilities;
   2868 
   2869 	/*
   2870 	** Don't turn this on by default, if vlans are
   2871 	** created on another pseudo device (eg. lagg)
   2872 	** then vlan events are not passed thru, breaking
   2873 	** operation, but with HW FILTER off it works. If
   2874 	** using vlans directly on the ixgbe driver you can
   2875 	** enable this and get full hardware tag filtering.
   2876 	*/
   2877 	ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER;
   2878 
   2879 	/*
   2880 	 * Specify the media types supported by this adapter and register
   2881 	 * callbacks to update media and link information
   2882 	 */
   2883 	ifmedia_init(&adapter->media, IFM_IMASK, ixgbe_media_change,
   2884 		    ixgbe_media_status);
   2885 
   2886 	ixgbe_add_media_types(adapter);
   2887 
   2888 	/* Autoselect media by default */
   2889 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   2890 
   2891 	return (0);
   2892 }
   2893 
   2894 static void
   2895 ixgbe_add_media_types(struct adapter *adapter)
   2896 {
   2897 	struct ixgbe_hw *hw = &adapter->hw;
   2898 	device_t dev = adapter->dev;
   2899 	int layer;
   2900 
   2901 	layer = adapter->phy_layer = ixgbe_get_supported_physical_layer(hw);
   2902 
   2903 	/* Media types with matching NetBSD media defines */
   2904 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T)
   2905 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_10G_T, 0, NULL);
   2906 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T)
   2907 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_T, 0, NULL);
   2908 	if (layer & IXGBE_PHYSICAL_LAYER_100BASE_TX)
   2909 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX, 0, NULL);
   2910 
   2911 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
   2912 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
   2913 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_10G_TWINAX, 0, NULL);
   2914 
   2915 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR)
   2916 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_10G_LR, 0, NULL);
   2917 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR)
   2918 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
   2919 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
   2920 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
   2921 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
   2922 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX, 0, NULL);
   2923 
   2924 	/*
   2925 	** Other (no matching NetBSD media type):
   2926 	** To workaround this, we'll assign these completely
   2927 	** inappropriate media types.
   2928 	*/
   2929 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
   2930 		device_printf(dev, "Media supported: 10GbaseKR\n");
   2931 		device_printf(dev, "10GbaseKR mapped to 10GbaseSR\n");
   2932 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
   2933 	}
   2934 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
   2935 		device_printf(dev, "Media supported: 10GbaseKX4\n");
   2936 		device_printf(dev, "10GbaseKX4 mapped to 10GbaseCX4\n");
   2937 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
   2938 	}
   2939 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX) {
   2940 		device_printf(dev, "Media supported: 1000baseKX\n");
   2941 		device_printf(dev, "1000baseKX mapped to 1000baseCX\n");
   2942 		ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_CX, 0, NULL);
   2943 	}
   2944 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_BX) {
   2945 		/* Someday, someone will care about you... */
   2946 		device_printf(dev, "Media supported: 1000baseBX\n");
   2947 	}
   2948 
   2949 	if (hw->device_id == IXGBE_DEV_ID_82598AT) {
   2950 		ifmedia_add(&adapter->media,
   2951 		    IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
   2952 		ifmedia_add(&adapter->media,
   2953 		    IFM_ETHER | IFM_1000_T, 0, NULL);
   2954 	}
   2955 
   2956 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
   2957 }
   2958 
   2959 static void
   2960 ixgbe_config_link(struct adapter *adapter)
   2961 {
   2962 	struct ixgbe_hw *hw = &adapter->hw;
   2963 	u32	autoneg, err = 0;
   2964 	bool	sfp, negotiate;
   2965 
   2966 	sfp = ixgbe_is_sfp(hw);
   2967 
   2968 	if (sfp) {
   2969 		void *ip;
   2970 
   2971 		if (hw->phy.multispeed_fiber) {
   2972 			hw->mac.ops.setup_sfp(hw);
   2973 			ixgbe_enable_tx_laser(hw);
   2974 			ip = adapter->msf_si;
   2975 		} else {
   2976 			ip = adapter->mod_si;
   2977 		}
   2978 
   2979 		kpreempt_disable();
   2980 		softint_schedule(ip);
   2981 		kpreempt_enable();
   2982 	} else {
   2983 		if (hw->mac.ops.check_link)
   2984 			err = ixgbe_check_link(hw, &adapter->link_speed,
   2985 			    &adapter->link_up, FALSE);
   2986 		if (err)
   2987 			goto out;
   2988 		autoneg = hw->phy.autoneg_advertised;
   2989 		if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
   2990                 	err  = hw->mac.ops.get_link_capabilities(hw,
   2991 			    &autoneg, &negotiate);
   2992 		else
   2993 			negotiate = 0;
   2994 		if (err)
   2995 			goto out;
   2996 		if (hw->mac.ops.setup_link)
   2997                 	err = hw->mac.ops.setup_link(hw,
   2998 			    autoneg, adapter->link_up);
   2999 	}
   3000 out:
   3001 	return;
   3002 }
   3003 
   3004 
   3005 /*********************************************************************
   3006  *
   3007  *  Enable transmit units.
   3008  *
   3009  **********************************************************************/
   3010 static void
   3011 ixgbe_initialize_transmit_units(struct adapter *adapter)
   3012 {
   3013 	struct tx_ring	*txr = adapter->tx_rings;
   3014 	struct ixgbe_hw	*hw = &adapter->hw;
   3015 
   3016 	/* Setup the Base and Length of the Tx Descriptor Ring */
   3017 
   3018 	for (int i = 0; i < adapter->num_queues; i++, txr++) {
   3019 		u64	tdba = txr->txdma.dma_paddr;
   3020 		u32	txctrl = 0;
   3021 		int	j = txr->me;
   3022 
   3023 		IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
   3024 		       (tdba & 0x00000000ffffffffULL));
   3025 		IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
   3026 		IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j),
   3027 		    adapter->num_tx_desc * sizeof(union ixgbe_adv_tx_desc));
   3028 
   3029 		/* Setup the HW Tx Head and Tail descriptor pointers */
   3030 		IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
   3031 		IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
   3032 
   3033 		/* Cache the tail address */
   3034 		txr->tail = IXGBE_TDT(j);
   3035 
   3036 		/* Set the processing limit */
   3037 		txr->process_limit = ixgbe_tx_process_limit;
   3038 
   3039 		/* Disable Head Writeback */
   3040 		switch (hw->mac.type) {
   3041 		case ixgbe_mac_82598EB:
   3042 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
   3043 			break;
   3044 		case ixgbe_mac_82599EB:
   3045 		case ixgbe_mac_X540:
   3046 		default:
   3047 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(j));
   3048 			break;
   3049                 }
   3050 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
   3051 		switch (hw->mac.type) {
   3052 		case ixgbe_mac_82598EB:
   3053 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
   3054 			break;
   3055 		case ixgbe_mac_82599EB:
   3056 		case ixgbe_mac_X540:
   3057 		default:
   3058 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(j), txctrl);
   3059 			break;
   3060 		}
   3061 
   3062 	}
   3063 
   3064 	if (hw->mac.type != ixgbe_mac_82598EB) {
   3065 		u32 dmatxctl, rttdcs;
   3066 #ifdef PCI_IOV
   3067 		enum ixgbe_iov_mode mode = ixgbe_get_iov_mode(adapter);
   3068 #endif
   3069 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
   3070 		dmatxctl |= IXGBE_DMATXCTL_TE;
   3071 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
   3072 		/* Disable arbiter to set MTQC */
   3073 		rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
   3074 		rttdcs |= IXGBE_RTTDCS_ARBDIS;
   3075 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
   3076 #ifdef PCI_IOV
   3077 		IXGBE_WRITE_REG(hw, IXGBE_MTQC, ixgbe_get_mtqc(mode));
   3078 #else
   3079 		IXGBE_WRITE_REG(hw, IXGBE_MTQC, IXGBE_MTQC_64Q_1PB);
   3080 #endif
   3081 		rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
   3082 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
   3083 	}
   3084 
   3085 	return;
   3086 }
   3087 
   3088 static void
   3089 ixgbe_initialise_rss_mapping(struct adapter *adapter)
   3090 {
   3091 	struct ixgbe_hw	*hw = &adapter->hw;
   3092 	u32 reta = 0, mrqc, rss_key[10];
   3093 	int queue_id, table_size, index_mult;
   3094 #ifdef	RSS
   3095 	u32 rss_hash_config;
   3096 #endif
   3097 #ifdef PCI_IOV
   3098 	enum ixgbe_iov_mode mode;
   3099 #endif
   3100 
   3101 #ifdef	RSS
   3102 	/* Fetch the configured RSS key */
   3103 	rss_getkey((uint8_t *) &rss_key);
   3104 #else
   3105 	/* set up random bits */
   3106 	cprng_fast(&rss_key, sizeof(rss_key));
   3107 #endif
   3108 
   3109 	/* Set multiplier for RETA setup and table size based on MAC */
   3110 	index_mult = 0x1;
   3111 	table_size = 128;
   3112 	switch (adapter->hw.mac.type) {
   3113 	case ixgbe_mac_82598EB:
   3114 		index_mult = 0x11;
   3115 		break;
   3116 	case ixgbe_mac_X550:
   3117 	case ixgbe_mac_X550EM_x:
   3118 		table_size = 512;
   3119 		break;
   3120 	default:
   3121 		break;
   3122 	}
   3123 
   3124 	/* Set up the redirection table */
   3125 	for (int i = 0, j = 0; i < table_size; i++, j++) {
   3126 		if (j == adapter->num_queues) j = 0;
   3127 #ifdef	RSS
   3128 		/*
   3129 		 * Fetch the RSS bucket id for the given indirection entry.
   3130 		 * Cap it at the number of configured buckets (which is
   3131 		 * num_queues.)
   3132 		 */
   3133 		queue_id = rss_get_indirection_to_bucket(i);
   3134 		queue_id = queue_id % adapter->num_queues;
   3135 #else
   3136 		queue_id = (j * index_mult);
   3137 #endif
   3138 		/*
   3139 		 * The low 8 bits are for hash value (n+0);
   3140 		 * The next 8 bits are for hash value (n+1), etc.
   3141 		 */
   3142 		reta = reta >> 8;
   3143 		reta = reta | ( ((uint32_t) queue_id) << 24);
   3144 		if ((i & 3) == 3) {
   3145 			if (i < 128)
   3146 				IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
   3147 			else
   3148 				IXGBE_WRITE_REG(hw, IXGBE_ERETA((i >> 2) - 32), reta);
   3149 			reta = 0;
   3150 		}
   3151 	}
   3152 
   3153 	/* Now fill our hash function seeds */
   3154 	for (int i = 0; i < 10; i++)
   3155 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
   3156 
   3157 	/* Perform hash on these packet types */
   3158 #ifdef	RSS
   3159 	mrqc = IXGBE_MRQC_RSSEN;
   3160 	rss_hash_config = rss_gethashconfig();
   3161 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
   3162 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
   3163 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
   3164 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
   3165 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
   3166 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
   3167 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
   3168 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
   3169 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
   3170 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
   3171 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
   3172 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
   3173 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
   3174 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
   3175 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4_EX)
   3176 		device_printf(adapter->dev,
   3177 		    "%s: RSS_HASHTYPE_RSS_UDP_IPV4_EX defined, "
   3178 		    "but not supported\n", __func__);
   3179 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
   3180 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
   3181 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
   3182 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
   3183 #else
   3184 	/*
   3185 	 * Disable UDP - IP fragments aren't currently being handled
   3186 	 * and so we end up with a mix of 2-tuple and 4-tuple
   3187 	 * traffic.
   3188 	 */
   3189 	mrqc = IXGBE_MRQC_RSSEN
   3190 	     | IXGBE_MRQC_RSS_FIELD_IPV4
   3191 	     | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
   3192 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
   3193 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX
   3194 	     | IXGBE_MRQC_RSS_FIELD_IPV6
   3195 	     | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
   3196 	;
   3197 #endif /* RSS */
   3198 #ifdef PCI_IOV
   3199 	mode = ixgbe_get_iov_mode(adapter);
   3200 	mrqc |= ixgbe_get_mrqc(mode);
   3201 #endif
   3202 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
   3203 }
   3204 
   3205 
   3206 /*********************************************************************
   3207  *
   3208  *  Setup receive registers and features.
   3209  *
   3210  **********************************************************************/
   3211 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
   3212 
   3213 #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
   3214 
   3215 static void
   3216 ixgbe_initialize_receive_units(struct adapter *adapter)
   3217 {
   3218 	int i;
   3219 	struct	rx_ring	*rxr = adapter->rx_rings;
   3220 	struct ixgbe_hw	*hw = &adapter->hw;
   3221 	struct ifnet   *ifp = adapter->ifp;
   3222 	u32		bufsz, fctrl, srrctl, rxcsum;
   3223 	u32		hlreg;
   3224 
   3225 
   3226 	/*
   3227 	 * Make sure receives are disabled while
   3228 	 * setting up the descriptor ring
   3229 	 */
   3230 	ixgbe_disable_rx(hw);
   3231 
   3232 	/* Enable broadcasts */
   3233 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
   3234 	fctrl |= IXGBE_FCTRL_BAM;
   3235 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
   3236 		fctrl |= IXGBE_FCTRL_DPF;
   3237 		fctrl |= IXGBE_FCTRL_PMCF;
   3238 	}
   3239 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
   3240 
   3241 	/* Set for Jumbo Frames? */
   3242 	hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
   3243 	if (ifp->if_mtu > ETHERMTU)
   3244 		hlreg |= IXGBE_HLREG0_JUMBOEN;
   3245 	else
   3246 		hlreg &= ~IXGBE_HLREG0_JUMBOEN;
   3247 #ifdef DEV_NETMAP
   3248 	/* crcstrip is conditional in netmap (in RDRXCTL too ?) */
   3249 	if (ifp->if_capenable & IFCAP_NETMAP && !ix_crcstrip)
   3250 		hlreg &= ~IXGBE_HLREG0_RXCRCSTRP;
   3251 	else
   3252 		hlreg |= IXGBE_HLREG0_RXCRCSTRP;
   3253 #endif /* DEV_NETMAP */
   3254 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
   3255 
   3256 	bufsz = (adapter->rx_mbuf_sz +
   3257 	    BSIZEPKT_ROUNDUP) >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
   3258 
   3259 	for (i = 0; i < adapter->num_queues; i++, rxr++) {
   3260 		u64 rdba = rxr->rxdma.dma_paddr;
   3261 		int j = rxr->me;
   3262 
   3263 		/* Setup the Base and Length of the Rx Descriptor Ring */
   3264 		IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j),
   3265 			       (rdba & 0x00000000ffffffffULL));
   3266 		IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
   3267 		IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j),
   3268 		    adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
   3269 
   3270 		/* Set up the SRRCTL register */
   3271 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(j));
   3272 		srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
   3273 		srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
   3274 		srrctl |= bufsz;
   3275 		srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
   3276 
   3277 		/*
   3278 		 * Set DROP_EN iff we have no flow control and >1 queue.
   3279 		 * Note that srrctl was cleared shortly before during reset,
   3280 		 * so we do not need to clear the bit, but do it just in case
   3281 		 * this code is moved elsewhere.
   3282 		 */
   3283 		if (adapter->num_queues > 1 &&
   3284 		    adapter->hw.fc.requested_mode == ixgbe_fc_none) {
   3285 			srrctl |= IXGBE_SRRCTL_DROP_EN;
   3286 		} else {
   3287 			srrctl &= ~IXGBE_SRRCTL_DROP_EN;
   3288 		}
   3289 
   3290 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(j), srrctl);
   3291 
   3292 		/* Setup the HW Rx Head and Tail Descriptor Pointers */
   3293 		IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
   3294 		IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
   3295 
   3296 		/* Set the processing limit */
   3297 		rxr->process_limit = ixgbe_rx_process_limit;
   3298 
   3299 		/* Set the driver rx tail address */
   3300 		rxr->tail =  IXGBE_RDT(rxr->me);
   3301 	}
   3302 
   3303 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
   3304 		u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
   3305 			      IXGBE_PSRTYPE_UDPHDR |
   3306 			      IXGBE_PSRTYPE_IPV4HDR |
   3307 			      IXGBE_PSRTYPE_IPV6HDR;
   3308 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
   3309 	}
   3310 
   3311 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
   3312 
   3313 	ixgbe_initialise_rss_mapping(adapter);
   3314 
   3315 	if (adapter->num_queues > 1) {
   3316 		/* RSS and RX IPP Checksum are mutually exclusive */
   3317 		rxcsum |= IXGBE_RXCSUM_PCSD;
   3318 	}
   3319 
   3320 	if (ifp->if_capenable & IFCAP_RXCSUM)
   3321 		rxcsum |= IXGBE_RXCSUM_PCSD;
   3322 
   3323 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
   3324 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
   3325 
   3326 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
   3327 
   3328 	return;
   3329 }
   3330 
   3331 
   3332 #if 0	/* XXX Badly need to overhaul vlan(4) on NetBSD. */
   3333 /*
   3334 ** This routine is run via an vlan config EVENT,
   3335 ** it enables us to use the HW Filter table since
   3336 ** we can get the vlan id. This just creates the
   3337 ** entry in the soft version of the VFTA, init will
   3338 ** repopulate the real table.
   3339 */
   3340 static void
   3341 ixgbe_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   3342 {
   3343 	struct adapter	*adapter = ifp->if_softc;
   3344 	u16		index, bit;
   3345 
   3346 	if (ifp->if_softc !=  arg)   /* Not our event */
   3347 		return;
   3348 
   3349 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   3350 		return;
   3351 
   3352 	IXGBE_CORE_LOCK(adapter);
   3353 	index = (vtag >> 5) & 0x7F;
   3354 	bit = vtag & 0x1F;
   3355 	adapter->shadow_vfta[index] |= (1 << bit);
   3356 	ixgbe_setup_vlan_hw_support(adapter);
   3357 	IXGBE_CORE_UNLOCK(adapter);
   3358 }
   3359 
   3360 /*
   3361 ** This routine is run via an vlan
   3362 ** unconfig EVENT, remove our entry
   3363 ** in the soft vfta.
   3364 */
   3365 static void
   3366 ixgbe_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   3367 {
   3368 	struct adapter	*adapter = ifp->if_softc;
   3369 	u16		index, bit;
   3370 
   3371 	if (ifp->if_softc !=  arg)
   3372 		return;
   3373 
   3374 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   3375 		return;
   3376 
   3377 	IXGBE_CORE_LOCK(adapter);
   3378 	index = (vtag >> 5) & 0x7F;
   3379 	bit = vtag & 0x1F;
   3380 	adapter->shadow_vfta[index] &= ~(1 << bit);
   3381 	/* Re-init to load the changes */
   3382 	ixgbe_setup_vlan_hw_support(adapter);
   3383 	IXGBE_CORE_UNLOCK(adapter);
   3384 }
   3385 #endif
   3386 
   3387 static void
   3388 ixgbe_setup_vlan_hw_support(struct adapter *adapter)
   3389 {
   3390 	struct ethercom *ec = &adapter->osdep.ec;
   3391 	struct ixgbe_hw *hw = &adapter->hw;
   3392 	struct rx_ring	*rxr;
   3393 	u32		ctrl;
   3394 
   3395 
   3396 	/*
   3397 	** We get here thru init_locked, meaning
   3398 	** a soft reset, this has already cleared
   3399 	** the VFTA and other state, so if there
   3400 	** have been no vlan's registered do nothing.
   3401 	*/
   3402 	if (!VLAN_ATTACHED(&adapter->osdep.ec))
   3403 		return;
   3404 
   3405 	/* Setup the queues for vlans */
   3406 	for (int i = 0; i < adapter->num_queues; i++) {
   3407 		rxr = &adapter->rx_rings[i];
   3408 		/* On 82599 the VLAN enable is per/queue in RXDCTL */
   3409 		if (hw->mac.type != ixgbe_mac_82598EB) {
   3410 			ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
   3411 			ctrl |= IXGBE_RXDCTL_VME;
   3412 			IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), ctrl);
   3413 		}
   3414 		rxr->vtag_strip = TRUE;
   3415 	}
   3416 
   3417 	if ((ec->ec_capenable & ETHERCAP_VLAN_HWFILTER) == 0)
   3418 		return;
   3419 	/*
   3420 	** A soft reset zero's out the VFTA, so
   3421 	** we need to repopulate it now.
   3422 	*/
   3423 	for (int i = 0; i < IXGBE_VFTA_SIZE; i++)
   3424 		if (adapter->shadow_vfta[i] != 0)
   3425 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(i),
   3426 			    adapter->shadow_vfta[i]);
   3427 
   3428 	ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
   3429 	/* Enable the Filter Table if enabled */
   3430 	if (ec->ec_capenable & ETHERCAP_VLAN_HWFILTER) {
   3431 		ctrl &= ~IXGBE_VLNCTRL_CFIEN;
   3432 		ctrl |= IXGBE_VLNCTRL_VFE;
   3433 	}
   3434 	if (hw->mac.type == ixgbe_mac_82598EB)
   3435 		ctrl |= IXGBE_VLNCTRL_VME;
   3436 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
   3437 }
   3438 
   3439 static void
   3440 ixgbe_enable_intr(struct adapter *adapter)
   3441 {
   3442 	struct ixgbe_hw	*hw = &adapter->hw;
   3443 	struct ix_queue	*que = adapter->queues;
   3444 	u32		mask, fwsm;
   3445 
   3446 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
   3447 	/* Enable Fan Failure detection */
   3448 	if (hw->device_id == IXGBE_DEV_ID_82598AT)
   3449 		    mask |= IXGBE_EIMS_GPI_SDP1;
   3450 
   3451 	switch (adapter->hw.mac.type) {
   3452 		case ixgbe_mac_82599EB:
   3453 			mask |= IXGBE_EIMS_ECC;
   3454 			/* Temperature sensor on some adapters */
   3455 			mask |= IXGBE_EIMS_GPI_SDP0;
   3456 			/* SFP+ (RX_LOS_N & MOD_ABS_N) */
   3457 			mask |= IXGBE_EIMS_GPI_SDP1;
   3458 			mask |= IXGBE_EIMS_GPI_SDP2;
   3459 #ifdef IXGBE_FDIR
   3460 			mask |= IXGBE_EIMS_FLOW_DIR;
   3461 #endif
   3462 #ifdef PCI_IOV
   3463 			mask |= IXGBE_EIMS_MAILBOX;
   3464 #endif
   3465 			break;
   3466 		case ixgbe_mac_X540:
   3467 			/* Detect if Thermal Sensor is enabled */
   3468 			fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
   3469 			if (fwsm & IXGBE_FWSM_TS_ENABLED)
   3470 				mask |= IXGBE_EIMS_TS;
   3471 			mask |= IXGBE_EIMS_ECC;
   3472 #ifdef IXGBE_FDIR
   3473 			mask |= IXGBE_EIMS_FLOW_DIR;
   3474 #endif
   3475 			break;
   3476 		case ixgbe_mac_X550:
   3477 		case ixgbe_mac_X550EM_x:
   3478 			/* MAC thermal sensor is automatically enabled */
   3479 			mask |= IXGBE_EIMS_TS;
   3480 			/* Some devices use SDP0 for important information */
   3481 			if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
   3482 			    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
   3483 				mask |= IXGBE_EIMS_GPI_SDP0_BY_MAC(hw);
   3484 			mask |= IXGBE_EIMS_ECC;
   3485 #ifdef IXGBE_FDIR
   3486 			mask |= IXGBE_EIMS_FLOW_DIR;
   3487 #endif
   3488 #ifdef PCI_IOV
   3489 			mask |= IXGBE_EIMS_MAILBOX;
   3490 #endif
   3491 		/* falls through */
   3492 		default:
   3493 			break;
   3494 	}
   3495 
   3496 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
   3497 
   3498 	/* With MSI-X we use auto clear */
   3499 	if (adapter->msix_mem) {
   3500 		mask = IXGBE_EIMS_ENABLE_MASK;
   3501 		/* Don't autoclear Link */
   3502 		mask &= ~IXGBE_EIMS_OTHER;
   3503 		mask &= ~IXGBE_EIMS_LSC;
   3504 #ifdef PCI_IOV
   3505 		mask &= ~IXGBE_EIMS_MAILBOX;
   3506 #endif
   3507 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
   3508 	}
   3509 
   3510 	/*
   3511 	** Now enable all queues, this is done separately to
   3512 	** allow for handling the extended (beyond 32) MSIX
   3513 	** vectors that can be used by 82599
   3514 	*/
   3515         for (int i = 0; i < adapter->num_queues; i++, que++)
   3516                 ixgbe_enable_queue(adapter, que->msix);
   3517 
   3518 	IXGBE_WRITE_FLUSH(hw);
   3519 
   3520 	return;
   3521 }
   3522 
   3523 static void
   3524 ixgbe_disable_intr(struct adapter *adapter)
   3525 {
   3526 	if (adapter->msix_mem)
   3527 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, 0);
   3528 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
   3529 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
   3530 	} else {
   3531 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, 0xFFFF0000);
   3532 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), ~0);
   3533 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), ~0);
   3534 	}
   3535 	IXGBE_WRITE_FLUSH(&adapter->hw);
   3536 	return;
   3537 }
   3538 
   3539 /*
   3540 ** Get the width and transaction speed of
   3541 ** the slot this adapter is plugged into.
   3542 */
   3543 static void
   3544 ixgbe_get_slot_info(struct ixgbe_hw *hw)
   3545 {
   3546 	device_t		dev = ((struct ixgbe_osdep *)hw->back)->dev;
   3547 	struct ixgbe_mac_info	*mac = &hw->mac;
   3548 	u16			link;
   3549 
   3550 	/* For most devices simply call the shared code routine */
   3551 	if (hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) {
   3552 		ixgbe_get_bus_info(hw);
   3553 		/* These devices don't use PCI-E */
   3554 		switch (hw->mac.type) {
   3555 		case ixgbe_mac_X550EM_x:
   3556 			return;
   3557 		default:
   3558 			goto display;
   3559 		}
   3560 	}
   3561 
   3562 	/*
   3563 	** For the Quad port adapter we need to parse back
   3564 	** up the PCI tree to find the speed of the expansion
   3565 	** slot into which this adapter is plugged. A bit more work.
   3566 	*/
   3567 	dev = device_parent(device_parent(dev));
   3568 #ifdef IXGBE_DEBUG
   3569 	device_printf(dev, "parent pcib = %x,%x,%x\n",
   3570 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
   3571 #endif
   3572 	dev = device_parent(device_parent(dev));
   3573 #ifdef IXGBE_DEBUG
   3574 	device_printf(dev, "slot pcib = %x,%x,%x\n",
   3575 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
   3576 #endif
   3577 	/* Now get the PCI Express Capabilities offset */
   3578 	/* ...and read the Link Status Register */
   3579 	link = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_LINK_STATUS);
   3580 	switch (link & IXGBE_PCI_LINK_WIDTH) {
   3581 	case IXGBE_PCI_LINK_WIDTH_1:
   3582 		hw->bus.width = ixgbe_bus_width_pcie_x1;
   3583 		break;
   3584 	case IXGBE_PCI_LINK_WIDTH_2:
   3585 		hw->bus.width = ixgbe_bus_width_pcie_x2;
   3586 		break;
   3587 	case IXGBE_PCI_LINK_WIDTH_4:
   3588 		hw->bus.width = ixgbe_bus_width_pcie_x4;
   3589 		break;
   3590 	case IXGBE_PCI_LINK_WIDTH_8:
   3591 		hw->bus.width = ixgbe_bus_width_pcie_x8;
   3592 		break;
   3593 	default:
   3594 		hw->bus.width = ixgbe_bus_width_unknown;
   3595 		break;
   3596 	}
   3597 
   3598 	switch (link & IXGBE_PCI_LINK_SPEED) {
   3599 	case IXGBE_PCI_LINK_SPEED_2500:
   3600 		hw->bus.speed = ixgbe_bus_speed_2500;
   3601 		break;
   3602 	case IXGBE_PCI_LINK_SPEED_5000:
   3603 		hw->bus.speed = ixgbe_bus_speed_5000;
   3604 		break;
   3605 	case IXGBE_PCI_LINK_SPEED_8000:
   3606 		hw->bus.speed = ixgbe_bus_speed_8000;
   3607 		break;
   3608 	default:
   3609 		hw->bus.speed = ixgbe_bus_speed_unknown;
   3610 		break;
   3611 	}
   3612 
   3613 	mac->ops.set_lan_id(hw);
   3614 
   3615 display:
   3616 	device_printf(dev,"PCI Express Bus: Speed %s %s\n",
   3617 	    ((hw->bus.speed == ixgbe_bus_speed_8000) ? "8.0GT/s":
   3618 	    (hw->bus.speed == ixgbe_bus_speed_5000) ? "5.0GT/s":
   3619 	    (hw->bus.speed == ixgbe_bus_speed_2500) ? "2.5GT/s":"Unknown"),
   3620 	    (hw->bus.width == ixgbe_bus_width_pcie_x8) ? "Width x8" :
   3621 	    (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "Width x4" :
   3622 	    (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "Width x1" :
   3623 	    ("Unknown"));
   3624 
   3625 	if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   3626 	    ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
   3627 	    (hw->bus.speed == ixgbe_bus_speed_2500))) {
   3628 		device_printf(dev, "PCI-Express bandwidth available"
   3629 		    " for this card\n     is not sufficient for"
   3630 		    " optimal performance.\n");
   3631 		device_printf(dev, "For optimal performance a x8 "
   3632 		    "PCIE, or x4 PCIE Gen2 slot is required.\n");
   3633         }
   3634 	if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   3635 	    ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
   3636 	    (hw->bus.speed < ixgbe_bus_speed_8000))) {
   3637 		device_printf(dev, "PCI-Express bandwidth available"
   3638 		    " for this card\n     is not sufficient for"
   3639 		    " optimal performance.\n");
   3640 		device_printf(dev, "For optimal performance a x8 "
   3641 		    "PCIE Gen3 slot is required.\n");
   3642         }
   3643 
   3644 	return;
   3645 }
   3646 
   3647 
   3648 /*
   3649 ** Setup the correct IVAR register for a particular MSIX interrupt
   3650 **   (yes this is all very magic and confusing :)
   3651 **  - entry is the register array entry
   3652 **  - vector is the MSIX vector for this queue
   3653 **  - type is RX/TX/MISC
   3654 */
   3655 static void
   3656 ixgbe_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
   3657 {
   3658 	struct ixgbe_hw *hw = &adapter->hw;
   3659 	u32 ivar, index;
   3660 
   3661 	vector |= IXGBE_IVAR_ALLOC_VAL;
   3662 
   3663 	switch (hw->mac.type) {
   3664 
   3665 	case ixgbe_mac_82598EB:
   3666 		if (type == -1)
   3667 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
   3668 		else
   3669 			entry += (type * 64);
   3670 		index = (entry >> 2) & 0x1F;
   3671 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
   3672 		ivar &= ~(0xFF << (8 * (entry & 0x3)));
   3673 		ivar |= (vector << (8 * (entry & 0x3)));
   3674 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
   3675 		break;
   3676 
   3677 	case ixgbe_mac_82599EB:
   3678 	case ixgbe_mac_X540:
   3679 	case ixgbe_mac_X550:
   3680 	case ixgbe_mac_X550EM_x:
   3681 		if (type == -1) { /* MISC IVAR */
   3682 			index = (entry & 1) * 8;
   3683 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
   3684 			ivar &= ~(0xFF << index);
   3685 			ivar |= (vector << index);
   3686 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
   3687 		} else {	/* RX/TX IVARS */
   3688 			index = (16 * (entry & 1)) + (8 * type);
   3689 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
   3690 			ivar &= ~(0xFF << index);
   3691 			ivar |= (vector << index);
   3692 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
   3693 		}
   3694 
   3695 	default:
   3696 		break;
   3697 	}
   3698 }
   3699 
   3700 static void
   3701 ixgbe_configure_ivars(struct adapter *adapter)
   3702 {
   3703 	struct  ix_queue	*que = adapter->queues;
   3704 	u32			newitr;
   3705 
   3706 	if (ixgbe_max_interrupt_rate > 0)
   3707 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
   3708 	else {
   3709 		/*
   3710 		** Disable DMA coalescing if interrupt moderation is
   3711 		** disabled.
   3712 		*/
   3713 		adapter->dmac = 0;
   3714 		newitr = 0;
   3715 	}
   3716 
   3717         for (int i = 0; i < adapter->num_queues; i++, que++) {
   3718 		struct rx_ring *rxr = &adapter->rx_rings[i];
   3719 		struct tx_ring *txr = &adapter->tx_rings[i];
   3720 		/* First the RX queue entry */
   3721                 ixgbe_set_ivar(adapter, rxr->me, que->msix, 0);
   3722 		/* ... and the TX */
   3723 		ixgbe_set_ivar(adapter, txr->me, que->msix, 1);
   3724 		/* Set an Initial EITR value */
   3725                 IXGBE_WRITE_REG(&adapter->hw,
   3726                     IXGBE_EITR(que->msix), newitr);
   3727 	}
   3728 
   3729 	/* For the Link interrupt */
   3730         ixgbe_set_ivar(adapter, 1, adapter->vector, -1);
   3731 }
   3732 
   3733 /*
   3734 ** ixgbe_sfp_probe - called in the local timer to
   3735 ** determine if a port had optics inserted.
   3736 */
   3737 static bool
   3738 ixgbe_sfp_probe(struct adapter *adapter)
   3739 {
   3740 	struct ixgbe_hw	*hw = &adapter->hw;
   3741 	device_t	dev = adapter->dev;
   3742 	bool		result = FALSE;
   3743 
   3744 	if ((hw->phy.type == ixgbe_phy_nl) &&
   3745 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
   3746 		s32 ret = hw->phy.ops.identify_sfp(hw);
   3747 		if (ret)
   3748                         goto out;
   3749 		ret = hw->phy.ops.reset(hw);
   3750 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   3751 			device_printf(dev,"Unsupported SFP+ module detected!");
   3752 			device_printf(dev, "Reload driver with supported module.\n");
   3753 			adapter->sfp_probe = FALSE;
   3754                         goto out;
   3755 		} else
   3756 			device_printf(dev,"SFP+ module detected!\n");
   3757 		/* We now have supported optics */
   3758 		adapter->sfp_probe = FALSE;
   3759 		/* Set the optics type so system reports correctly */
   3760 		ixgbe_setup_optics(adapter);
   3761 		result = TRUE;
   3762 	}
   3763 out:
   3764 	return (result);
   3765 }
   3766 
   3767 /*
   3768 ** Tasklet handler for MSIX Link interrupts
   3769 **  - do outside interrupt since it might sleep
   3770 */
   3771 static void
   3772 ixgbe_handle_link(void *context)
   3773 {
   3774 	struct adapter  *adapter = context;
   3775 
   3776 	ixgbe_check_link(&adapter->hw,
   3777 	    &adapter->link_speed, &adapter->link_up, 0);
   3778 	ixgbe_update_link_status(adapter);
   3779 }
   3780 
   3781 /*
   3782 ** Tasklet for handling SFP module interrupts
   3783 */
   3784 static void
   3785 ixgbe_handle_mod(void *context)
   3786 {
   3787 	struct adapter  *adapter = context;
   3788 	struct ixgbe_hw *hw = &adapter->hw;
   3789 	device_t	dev = adapter->dev;
   3790 	u32 err;
   3791 
   3792 	err = hw->phy.ops.identify_sfp(hw);
   3793 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   3794 		device_printf(dev,
   3795 		    "Unsupported SFP+ module type was detected.\n");
   3796 		return;
   3797 	}
   3798 
   3799 	err = hw->mac.ops.setup_sfp(hw);
   3800 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   3801 		device_printf(dev,
   3802 		    "Setup failure - unsupported SFP+ module type.\n");
   3803 		return;
   3804 	}
   3805 	softint_schedule(adapter->msf_si);
   3806 	return;
   3807 }
   3808 
   3809 
   3810 /*
   3811 ** Tasklet for handling MSF (multispeed fiber) interrupts
   3812 */
   3813 static void
   3814 ixgbe_handle_msf(void *context)
   3815 {
   3816 	struct adapter  *adapter = context;
   3817 	struct ixgbe_hw *hw = &adapter->hw;
   3818 	u32 autoneg;
   3819 	bool negotiate;
   3820 	int err;
   3821 
   3822 	err = hw->phy.ops.identify_sfp(hw);
   3823 	if (!err) {
   3824 		ixgbe_setup_optics(adapter);
   3825 		INIT_DEBUGOUT1("ixgbe_sfp_probe: flags: %X\n", adapter->optics);
   3826 	}
   3827 
   3828 	autoneg = hw->phy.autoneg_advertised;
   3829 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
   3830 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
   3831 	else
   3832 		negotiate = 0;
   3833 	if (hw->mac.ops.setup_link)
   3834 		hw->mac.ops.setup_link(hw, autoneg, TRUE);
   3835 
   3836 	ifmedia_removeall(&adapter->media);
   3837 	ixgbe_add_media_types(adapter);
   3838 	return;
   3839 }
   3840 
   3841 /*
   3842 ** Tasklet for handling interrupts from an external PHY
   3843 */
   3844 static void
   3845 ixgbe_handle_phy(void *context)
   3846 {
   3847 	struct adapter  *adapter = context;
   3848 	struct ixgbe_hw *hw = &adapter->hw;
   3849 	int error;
   3850 
   3851 	error = hw->phy.ops.handle_lasi(hw);
   3852 	if (error == IXGBE_ERR_OVERTEMP)
   3853 		device_printf(adapter->dev,
   3854 		    "CRITICAL: EXTERNAL PHY OVER TEMP!! "
   3855 		    " PHY will downshift to lower power state!\n");
   3856 	else if (error)
   3857 		device_printf(adapter->dev,
   3858 		    "Error handling LASI interrupt: %d\n",
   3859 		    error);
   3860 	return;
   3861 }
   3862 
   3863 #ifdef IXGBE_FDIR
   3864 /*
   3865 ** Tasklet for reinitializing the Flow Director filter table
   3866 */
   3867 static void
   3868 ixgbe_reinit_fdir(void *context)
   3869 {
   3870 	struct adapter  *adapter = context;
   3871 	struct ifnet   *ifp = adapter->ifp;
   3872 
   3873 	if (adapter->fdir_reinit != 1) /* Shouldn't happen */
   3874 		return;
   3875 	ixgbe_reinit_fdir_tables_82599(&adapter->hw);
   3876 	adapter->fdir_reinit = 0;
   3877 	/* re-enable flow director interrupts */
   3878 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_FLOW_DIR);
   3879 	/* Restart the interface */
   3880 	ifp->if_flags |= IFF_RUNNING;
   3881 	return;
   3882 }
   3883 #endif
   3884 
   3885 /*********************************************************************
   3886  *
   3887  *  Configure DMA Coalescing
   3888  *
   3889  **********************************************************************/
   3890 static void
   3891 ixgbe_config_dmac(struct adapter *adapter)
   3892 {
   3893 	struct ixgbe_hw *hw = &adapter->hw;
   3894 	struct ixgbe_dmac_config *dcfg = &hw->mac.dmac_config;
   3895 
   3896 	if (hw->mac.type < ixgbe_mac_X550 ||
   3897 	    !hw->mac.ops.dmac_config)
   3898 		return;
   3899 
   3900 	if (dcfg->watchdog_timer ^ adapter->dmac ||
   3901 	    dcfg->link_speed ^ adapter->link_speed) {
   3902 		dcfg->watchdog_timer = adapter->dmac;
   3903 		dcfg->fcoe_en = false;
   3904 		dcfg->link_speed = adapter->link_speed;
   3905 		dcfg->num_tcs = 1;
   3906 
   3907 		INIT_DEBUGOUT2("dmac settings: watchdog %d, link speed %d\n",
   3908 		    dcfg->watchdog_timer, dcfg->link_speed);
   3909 
   3910 		hw->mac.ops.dmac_config(hw);
   3911 	}
   3912 }
   3913 
   3914 /*
   3915  * Checks whether the adapter supports Energy Efficient Ethernet
   3916  * or not, based on device ID.
   3917  */
   3918 static void
   3919 ixgbe_check_eee_support(struct adapter *adapter)
   3920 {
   3921 	struct ixgbe_hw *hw = &adapter->hw;
   3922 
   3923 	adapter->eee_enabled = !!(hw->mac.ops.setup_eee);
   3924 }
   3925 
   3926 /*
   3927  * Checks whether the adapter's ports are capable of
   3928  * Wake On LAN by reading the adapter's NVM.
   3929  *
   3930  * Sets each port's hw->wol_enabled value depending
   3931  * on the value read here.
   3932  */
   3933 static void
   3934 ixgbe_check_wol_support(struct adapter *adapter)
   3935 {
   3936 	struct ixgbe_hw *hw = &adapter->hw;
   3937 	u16 dev_caps = 0;
   3938 
   3939 	/* Find out WoL support for port */
   3940 	adapter->wol_support = hw->wol_enabled = 0;
   3941 	ixgbe_get_device_caps(hw, &dev_caps);
   3942 	if ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
   3943 	    ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0) &&
   3944 	        hw->bus.func == 0))
   3945 	    adapter->wol_support = hw->wol_enabled = 1;
   3946 
   3947 	/* Save initial wake up filter configuration */
   3948 	adapter->wufc = IXGBE_READ_REG(hw, IXGBE_WUFC);
   3949 
   3950 	return;
   3951 }
   3952 
   3953 /*
   3954  * Prepare the adapter/port for LPLU and/or WoL
   3955  */
   3956 static int
   3957 ixgbe_setup_low_power_mode(struct adapter *adapter)
   3958 {
   3959 	struct ixgbe_hw *hw = &adapter->hw;
   3960 	device_t dev = adapter->dev;
   3961 	s32 error = 0;
   3962 
   3963 	KASSERT(mutex_owned(&adapter->core_mtx));
   3964 
   3965 	/* Limit power management flow to X550EM baseT */
   3966 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T
   3967 	    && hw->phy.ops.enter_lplu) {
   3968 		/* Turn off support for APM wakeup. (Using ACPI instead) */
   3969 		IXGBE_WRITE_REG(hw, IXGBE_GRC,
   3970 		    IXGBE_READ_REG(hw, IXGBE_GRC) & ~(u32)2);
   3971 
   3972 		/*
   3973 		 * Clear Wake Up Status register to prevent any previous wakeup
   3974 		 * events from waking us up immediately after we suspend.
   3975 		 */
   3976 		IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
   3977 
   3978 		/*
   3979 		 * Program the Wakeup Filter Control register with user filter
   3980 		 * settings
   3981 		 */
   3982 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, adapter->wufc);
   3983 
   3984 		/* Enable wakeups and power management in Wakeup Control */
   3985 		IXGBE_WRITE_REG(hw, IXGBE_WUC,
   3986 		    IXGBE_WUC_WKEN | IXGBE_WUC_PME_EN);
   3987 
   3988 		/* X550EM baseT adapters need a special LPLU flow */
   3989 		hw->phy.reset_disable = true;
   3990 		ixgbe_stop(adapter);
   3991 		error = hw->phy.ops.enter_lplu(hw);
   3992 		if (error)
   3993 			device_printf(dev,
   3994 			    "Error entering LPLU: %d\n", error);
   3995 		hw->phy.reset_disable = false;
   3996 	} else {
   3997 		/* Just stop for other adapters */
   3998 		ixgbe_stop(adapter);
   3999 	}
   4000 
   4001 	return error;
   4002 }
   4003 
   4004 /**********************************************************************
   4005  *
   4006  *  Update the board statistics counters.
   4007  *
   4008  **********************************************************************/
   4009 static void
   4010 ixgbe_update_stats_counters(struct adapter *adapter)
   4011 {
   4012 	struct ifnet   *ifp = adapter->ifp;
   4013 	struct ixgbe_hw *hw = &adapter->hw;
   4014 	u32 missed_rx = 0, bprc, lxon, lxoff, total;
   4015 	u64 total_missed_rx = 0;
   4016 	uint64_t crcerrs, rlec;
   4017 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
   4018 
   4019 	crcerrs = IXGBE_READ_REG(hw, IXGBE_CRCERRS);
   4020 	stats->crcerrs.ev_count += crcerrs;
   4021 	stats->illerrc.ev_count += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
   4022 	stats->errbc.ev_count += IXGBE_READ_REG(hw, IXGBE_ERRBC);
   4023 	stats->mspdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MSPDC);
   4024 
   4025 	for (int i = 0; i < __arraycount(stats->qprc); i++) {
   4026 		int j = i % adapter->num_queues;
   4027 		stats->qprc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
   4028 		stats->qptc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
   4029 		stats->qprdc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
   4030 	}
   4031 	stats->mlfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MLFC);
   4032 	stats->mrfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MRFC);
   4033 	rlec = IXGBE_READ_REG(hw, IXGBE_RLEC);
   4034 	stats->rlec.ev_count += rlec;
   4035 
   4036 	/* Hardware workaround, gprc counts missed packets */
   4037 	stats->gprc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPRC) - missed_rx;
   4038 
   4039 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
   4040 	stats->lxontxc.ev_count += lxon;
   4041 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
   4042 	stats->lxofftxc.ev_count += lxoff;
   4043 	total = lxon + lxoff;
   4044 
   4045 	if (hw->mac.type != ixgbe_mac_82598EB) {
   4046 		stats->gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCL) +
   4047 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
   4048 		stats->gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
   4049 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32) - total * ETHER_MIN_LEN;
   4050 		stats->tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORL) +
   4051 		    ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
   4052 		stats->lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
   4053 		stats->lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
   4054 	} else {
   4055 		stats->lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
   4056 		stats->lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
   4057 		/* 82598 only has a counter in the high register */
   4058 		stats->gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCH);
   4059 		stats->gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCH) - total * ETHER_MIN_LEN;
   4060 		stats->tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORH);
   4061 	}
   4062 
   4063 	/*
   4064 	 * Workaround: mprc hardware is incorrectly counting
   4065 	 * broadcasts, so for now we subtract those.
   4066 	 */
   4067 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
   4068 	stats->bprc.ev_count += bprc;
   4069 	stats->mprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPRC) - ((hw->mac.type == ixgbe_mac_82598EB) ? bprc : 0);
   4070 
   4071 	stats->prc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC64);
   4072 	stats->prc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC127);
   4073 	stats->prc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC255);
   4074 	stats->prc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC511);
   4075 	stats->prc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1023);
   4076 	stats->prc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1522);
   4077 
   4078 	stats->gptc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPTC) - total;
   4079 	stats->mptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPTC) - total;
   4080 	stats->ptc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC64) - total;
   4081 
   4082 	stats->ruc.ev_count += IXGBE_READ_REG(hw, IXGBE_RUC);
   4083 	stats->rfc.ev_count += IXGBE_READ_REG(hw, IXGBE_RFC);
   4084 	stats->roc.ev_count += IXGBE_READ_REG(hw, IXGBE_ROC);
   4085 	stats->rjc.ev_count += IXGBE_READ_REG(hw, IXGBE_RJC);
   4086 	stats->mngprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
   4087 	stats->mngpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
   4088 	stats->mngptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
   4089 	stats->tpr.ev_count += IXGBE_READ_REG(hw, IXGBE_TPR);
   4090 	stats->tpt.ev_count += IXGBE_READ_REG(hw, IXGBE_TPT);
   4091 	stats->ptc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC127);
   4092 	stats->ptc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC255);
   4093 	stats->ptc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC511);
   4094 	stats->ptc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1023);
   4095 	stats->ptc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1522);
   4096 	stats->bptc.ev_count += IXGBE_READ_REG(hw, IXGBE_BPTC);
   4097 	stats->xec.ev_count += IXGBE_READ_REG(hw, IXGBE_XEC);
   4098 	stats->fccrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCCRC);
   4099 	stats->fclast.ev_count += IXGBE_READ_REG(hw, IXGBE_FCLAST);
   4100 	/* Only read FCOE on 82599 */
   4101 	if (hw->mac.type != ixgbe_mac_82598EB) {
   4102 		stats->fcoerpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
   4103 		stats->fcoeprc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
   4104 		stats->fcoeptc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
   4105 		stats->fcoedwrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
   4106 		stats->fcoedwtc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
   4107 	}
   4108 
   4109 	/* Fill out the OS statistics structure */
   4110 	/*
   4111 	 * NetBSD: Don't override if_{i|o}{packets|bytes|mcasts} with
   4112 	 * adapter->stats counters. It's required to make ifconfig -z
   4113 	 * (SOICZIFDATA) work.
   4114 	 */
   4115 	ifp->if_collisions = 0;
   4116 
   4117 	/* Rx Errors */
   4118 	ifp->if_iqdrops += total_missed_rx;
   4119 	ifp->if_ierrors += crcerrs + rlec;
   4120 }
   4121 
   4122 /** ixgbe_sysctl_tdh_handler - Handler function
   4123  *  Retrieves the TDH value from the hardware
   4124  */
   4125 static int
   4126 ixgbe_sysctl_tdh_handler(SYSCTLFN_ARGS)
   4127 {
   4128 	struct sysctlnode node = *rnode;
   4129 	uint32_t val;
   4130 	struct tx_ring *txr;
   4131 
   4132 	txr = (struct tx_ring *)node.sysctl_data;
   4133 	if (txr == NULL)
   4134 		return 0;
   4135 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDH(txr->me));
   4136 	node.sysctl_data = &val;
   4137 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   4138 }
   4139 
   4140 /** ixgbe_sysctl_tdt_handler - Handler function
   4141  *  Retrieves the TDT value from the hardware
   4142  */
   4143 static int
   4144 ixgbe_sysctl_tdt_handler(SYSCTLFN_ARGS)
   4145 {
   4146 	struct sysctlnode node = *rnode;
   4147 	uint32_t val;
   4148 	struct tx_ring *txr;
   4149 
   4150 	txr = (struct tx_ring *)node.sysctl_data;
   4151 	if (txr == NULL)
   4152 		return 0;
   4153 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDT(txr->me));
   4154 	node.sysctl_data = &val;
   4155 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   4156 }
   4157 
   4158 /** ixgbe_sysctl_rdh_handler - Handler function
   4159  *  Retrieves the RDH value from the hardware
   4160  */
   4161 static int
   4162 ixgbe_sysctl_rdh_handler(SYSCTLFN_ARGS)
   4163 {
   4164 	struct sysctlnode node = *rnode;
   4165 	uint32_t val;
   4166 	struct rx_ring *rxr;
   4167 
   4168 	rxr = (struct rx_ring *)node.sysctl_data;
   4169 	if (rxr == NULL)
   4170 		return 0;
   4171 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDH(rxr->me));
   4172 	node.sysctl_data = &val;
   4173 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   4174 }
   4175 
   4176 /** ixgbe_sysctl_rdt_handler - Handler function
   4177  *  Retrieves the RDT value from the hardware
   4178  */
   4179 static int
   4180 ixgbe_sysctl_rdt_handler(SYSCTLFN_ARGS)
   4181 {
   4182 	struct sysctlnode node = *rnode;
   4183 	uint32_t val;
   4184 	struct rx_ring *rxr;
   4185 
   4186 	rxr = (struct rx_ring *)node.sysctl_data;
   4187 	if (rxr == NULL)
   4188 		return 0;
   4189 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDT(rxr->me));
   4190 	node.sysctl_data = &val;
   4191 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   4192 }
   4193 
   4194 static int
   4195 ixgbe_sysctl_interrupt_rate_handler(SYSCTLFN_ARGS)
   4196 {
   4197 	struct sysctlnode node = *rnode;
   4198 	struct ix_queue *que;
   4199 	uint32_t reg, usec, rate;
   4200 	int error;
   4201 
   4202 	que = (struct ix_queue *)node.sysctl_data;
   4203 	if (que == NULL)
   4204 		return 0;
   4205 	reg = IXGBE_READ_REG(&que->adapter->hw, IXGBE_EITR(que->msix));
   4206 	usec = ((reg & 0x0FF8) >> 3);
   4207 	if (usec > 0)
   4208 		rate = 500000 / usec;
   4209 	else
   4210 		rate = 0;
   4211 	node.sysctl_data = &rate;
   4212 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4213 	if (error)
   4214 		return error;
   4215 	reg &= ~0xfff; /* default, no limitation */
   4216 	ixgbe_max_interrupt_rate = 0;
   4217 	if (rate > 0 && rate < 500000) {
   4218 		if (rate < 1000)
   4219 			rate = 1000;
   4220 		ixgbe_max_interrupt_rate = rate;
   4221 		reg |= ((4000000/rate) & 0xff8 );
   4222 	}
   4223 	IXGBE_WRITE_REG(&que->adapter->hw, IXGBE_EITR(que->msix), reg);
   4224 	return 0;
   4225 }
   4226 
   4227 const struct sysctlnode *
   4228 ixgbe_sysctl_instance(struct adapter *adapter)
   4229 {
   4230 	const char *dvname;
   4231 	struct sysctllog **log;
   4232 	int rc;
   4233 	const struct sysctlnode *rnode;
   4234 
   4235 	log = &adapter->sysctllog;
   4236 	dvname = device_xname(adapter->dev);
   4237 
   4238 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
   4239 	    0, CTLTYPE_NODE, dvname,
   4240 	    SYSCTL_DESCR("ixgbe information and settings"),
   4241 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
   4242 		goto err;
   4243 
   4244 	return rnode;
   4245 err:
   4246 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
   4247 	return NULL;
   4248 }
   4249 
   4250 static void
   4251 ixgbe_add_device_sysctls(struct adapter *adapter)
   4252 {
   4253 	device_t dev = adapter->dev;
   4254 	struct ixgbe_hw *hw = &adapter->hw;
   4255 	struct sysctllog **log;
   4256 	const struct sysctlnode *rnode, *cnode;
   4257 
   4258 	log = &adapter->sysctllog;
   4259 
   4260 	if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   4261 		aprint_error_dev(dev, "could not create sysctl root\n");
   4262 		return;
   4263 	}
   4264 
   4265 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4266 	    CTLFLAG_READONLY, CTLTYPE_INT,
   4267 	    "num_rx_desc", SYSCTL_DESCR("Number of rx descriptors"),
   4268 	    NULL, 0, &adapter->num_rx_desc, 0, CTL_CREATE, CTL_EOL) != 0)
   4269 		aprint_error_dev(dev, "could not create sysctl\n");
   4270 
   4271 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4272 	    CTLFLAG_READONLY, CTLTYPE_INT,
   4273 	    "num_queues", SYSCTL_DESCR("Number of queues"),
   4274 	    NULL, 0, &adapter->num_queues, 0, CTL_CREATE, CTL_EOL) != 0)
   4275 		aprint_error_dev(dev, "could not create sysctl\n");
   4276 
   4277 	/* Sysctls for all devices */
   4278 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4279 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   4280 	    "fc", SYSCTL_DESCR(IXGBE_SYSCTL_DESC_SET_FC),
   4281 	    ixgbe_set_flowcntl, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4282 		aprint_error_dev(dev, "could not create sysctl\n");
   4283 
   4284 	/* XXX This is an *instance* sysctl controlling a *global* variable.
   4285 	 * XXX It's that way in the FreeBSD driver that this derives from.
   4286 	 */
   4287 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4288 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   4289 	    "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
   4290 	    NULL, 0, &ixgbe_enable_aim, 0, CTL_CREATE, CTL_EOL) != 0)
   4291 		aprint_error_dev(dev, "could not create sysctl\n");
   4292 
   4293 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4294 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   4295 	    "advertise_speed", SYSCTL_DESCR(IXGBE_SYSCTL_DESC_ADV_SPEED),
   4296 	    ixgbe_set_advertise, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4297 		aprint_error_dev(dev, "could not create sysctl\n");
   4298 
   4299 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4300 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   4301 	    "ts", SYSCTL_DESCR("Thermal Test"),
   4302 	    ixgbe_sysctl_thermal_test, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4303 		aprint_error_dev(dev, "could not create sysctl\n");
   4304 
   4305 	/* for X550 devices */
   4306 	if (hw->mac.type >= ixgbe_mac_X550)
   4307 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4308 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   4309 		    "dmac", SYSCTL_DESCR("DMA Coalesce"),
   4310 		    ixgbe_sysctl_dmac, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4311 			aprint_error_dev(dev, "could not create sysctl\n");
   4312 
   4313 	/* for X550T and X550EM backplane devices */
   4314 	if (hw->mac.ops.setup_eee) {
   4315 		const struct sysctlnode *eee_node;
   4316 
   4317 		if (sysctl_createv(log, 0, &rnode, &eee_node,
   4318 		    0, CTLTYPE_NODE,
   4319 		    "eee", SYSCTL_DESCR("Energy Efficient Ethernet sysctls"),
   4320 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0) {
   4321 			aprint_error_dev(dev, "could not create sysctl\n");
   4322 			return;
   4323 		}
   4324 
   4325 		if (sysctl_createv(log, 0, &eee_node, &cnode,
   4326 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   4327 		    "enable", SYSCTL_DESCR("Enable or Disable EEE"),
   4328 		    ixgbe_sysctl_eee_enable, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4329 			aprint_error_dev(dev, "could not create sysctl\n");
   4330 
   4331 		if (sysctl_createv(log, 0, &eee_node, &cnode,
   4332 		    CTLFLAG_READONLY, CTLTYPE_BOOL,
   4333 		    "negotiated", SYSCTL_DESCR("EEE negotiated on link"),
   4334 		    ixgbe_sysctl_eee_negotiated, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4335 			aprint_error_dev(dev, "could not create sysctl\n");
   4336 
   4337 		if (sysctl_createv(log, 0, &eee_node, &cnode,
   4338 		    CTLFLAG_READONLY, CTLTYPE_BOOL,
   4339 		    "tx_lpi_status", SYSCTL_DESCR("Whether or not TX link is in LPI state"),
   4340 		    ixgbe_sysctl_eee_tx_lpi_status, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4341 			aprint_error_dev(dev, "could not create sysctl\n");
   4342 
   4343 		if (sysctl_createv(log, 0, &eee_node, &cnode,
   4344 		    CTLFLAG_READONLY, CTLTYPE_BOOL,
   4345 		    "rx_lpi_status", SYSCTL_DESCR("Whether or not RX link is in LPI state"),
   4346 		    ixgbe_sysctl_eee_rx_lpi_status, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4347 			aprint_error_dev(dev, "could not create sysctl\n");
   4348 
   4349 	}
   4350 
   4351 	/* for certain 10GBaseT devices */
   4352 	if (hw->device_id == IXGBE_DEV_ID_X550T ||
   4353 	    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
   4354 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4355 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   4356 		    "wol_enable", SYSCTL_DESCR("Enable/Disable Wake on LAN"),
   4357 		    ixgbe_sysctl_wol_enable, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4358 			aprint_error_dev(dev, "could not create sysctl\n");
   4359 
   4360 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4361 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   4362 		    "wufc", SYSCTL_DESCR("Enable/Disable Wake Up Filters"),
   4363 		    ixgbe_sysctl_wufc, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4364 			aprint_error_dev(dev, "could not create sysctl\n");
   4365 	}
   4366 
   4367 	/* for X550EM 10GBaseT devices */
   4368 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
   4369 		const struct sysctlnode *phy_node;
   4370 
   4371 		if (sysctl_createv(log, 0, &rnode, &phy_node,
   4372 		    0, CTLTYPE_NODE,
   4373 		    "phy", SYSCTL_DESCR("External PHY sysctls"),
   4374 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0) {
   4375 			aprint_error_dev(dev, "could not create sysctl\n");
   4376 			return;
   4377 		}
   4378 
   4379 		if (sysctl_createv(log, 0, &phy_node, &cnode,
   4380 		    CTLFLAG_READONLY, CTLTYPE_INT,
   4381 		    "temp", SYSCTL_DESCR("Current External PHY Temperature (Celsius)"),
   4382 		    ixgbe_sysctl_phy_temp, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4383 			aprint_error_dev(dev, "could not create sysctl\n");
   4384 
   4385 		if (sysctl_createv(log, 0, &phy_node, &cnode,
   4386 		    CTLFLAG_READONLY, CTLTYPE_INT,
   4387 		    "overtemp_occurred", SYSCTL_DESCR("External PHY High Temperature Event Occurred"),
   4388 		    ixgbe_sysctl_phy_overtemp_occurred, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4389 			aprint_error_dev(dev, "could not create sysctl\n");
   4390 	}
   4391 }
   4392 
   4393 /*
   4394  * Add sysctl variables, one per statistic, to the system.
   4395  */
   4396 static void
   4397 ixgbe_add_hw_stats(struct adapter *adapter)
   4398 {
   4399 	device_t dev = adapter->dev;
   4400 	const struct sysctlnode *rnode, *cnode;
   4401 	struct sysctllog **log = &adapter->sysctllog;
   4402 	struct tx_ring *txr = adapter->tx_rings;
   4403 	struct rx_ring *rxr = adapter->rx_rings;
   4404 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
   4405 
   4406 	/* Driver Statistics */
   4407 #if 0
   4408 	/* These counters are not updated by the software */
   4409 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dropped",
   4410 			CTLFLAG_RD, &adapter->dropped_pkts,
   4411 			"Driver dropped packets");
   4412 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_header_failed",
   4413 			CTLFLAG_RD, &adapter->mbuf_header_failed,
   4414 			"???");
   4415 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_packet_failed",
   4416 			CTLFLAG_RD, &adapter->mbuf_packet_failed,
   4417 			"???");
   4418 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "no_tx_map_avail",
   4419 			CTLFLAG_RD, &adapter->no_tx_map_avail,
   4420 			"???");
   4421 #endif
   4422 	evcnt_attach_dynamic(&adapter->handleq, EVCNT_TYPE_MISC,
   4423 	    NULL, device_xname(dev), "Handled queue in softint");
   4424 	evcnt_attach_dynamic(&adapter->req, EVCNT_TYPE_MISC,
   4425 	    NULL, device_xname(dev), "Requeued in softint");
   4426 	evcnt_attach_dynamic(&adapter->morerx, EVCNT_TYPE_MISC,
   4427 	    NULL, device_xname(dev), "Interrupt handler more rx");
   4428 	evcnt_attach_dynamic(&adapter->moretx, EVCNT_TYPE_MISC,
   4429 	    NULL, device_xname(dev), "Interrupt handler more tx");
   4430 	evcnt_attach_dynamic(&adapter->txloops, EVCNT_TYPE_MISC,
   4431 	    NULL, device_xname(dev), "Interrupt handler tx loops");
   4432 	evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
   4433 	    NULL, device_xname(dev), "Driver tx dma soft fail EFBIG");
   4434 	evcnt_attach_dynamic(&adapter->m_defrag_failed, EVCNT_TYPE_MISC,
   4435 	    NULL, device_xname(dev), "m_defrag() failed");
   4436 	evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
   4437 	    NULL, device_xname(dev), "Driver tx dma hard fail EFBIG");
   4438 	evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
   4439 	    NULL, device_xname(dev), "Driver tx dma hard fail EINVAL");
   4440 	evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
   4441 	    NULL, device_xname(dev), "Driver tx dma hard fail other");
   4442 	evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
   4443 	    NULL, device_xname(dev), "Driver tx dma soft fail EAGAIN");
   4444 	evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
   4445 	    NULL, device_xname(dev), "Driver tx dma soft fail ENOMEM");
   4446 	evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
   4447 	    NULL, device_xname(dev), "Watchdog timeouts");
   4448 	evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
   4449 	    NULL, device_xname(dev), "TSO errors");
   4450 	evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_MISC,
   4451 	    NULL, device_xname(dev), "Link MSIX IRQ Handled");
   4452 
   4453 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   4454 		snprintf(adapter->queues[i].evnamebuf,
   4455 		    sizeof(adapter->queues[i].evnamebuf), "%s queue%d",
   4456 		    device_xname(dev), i);
   4457 		snprintf(adapter->queues[i].namebuf,
   4458 		    sizeof(adapter->queues[i].namebuf), "queue%d", i);
   4459 
   4460 		if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   4461 			aprint_error_dev(dev, "could not create sysctl root\n");
   4462 			break;
   4463 		}
   4464 
   4465 		if (sysctl_createv(log, 0, &rnode, &rnode,
   4466 		    0, CTLTYPE_NODE,
   4467 		    adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
   4468 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   4469 			break;
   4470 
   4471 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4472 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   4473 		    "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
   4474 		    ixgbe_sysctl_interrupt_rate_handler, 0,
   4475 		    (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
   4476 			break;
   4477 
   4478 #if 0 /* XXX msaitoh */
   4479 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4480 		    CTLFLAG_READONLY, CTLTYPE_QUAD,
   4481 		    "irqs", SYSCTL_DESCR("irqs on this queue"),
   4482 			NULL, 0, &(adapter->queues[i].irqs),
   4483 		    0, CTL_CREATE, CTL_EOL) != 0)
   4484 			break;
   4485 #endif
   4486 
   4487 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4488 		    CTLFLAG_READONLY, CTLTYPE_INT,
   4489 		    "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
   4490 		    ixgbe_sysctl_tdh_handler, 0, (void *)txr,
   4491 		    0, CTL_CREATE, CTL_EOL) != 0)
   4492 			break;
   4493 
   4494 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4495 		    CTLFLAG_READONLY, CTLTYPE_INT,
   4496 		    "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
   4497 		    ixgbe_sysctl_tdt_handler, 0, (void *)txr,
   4498 		    0, CTL_CREATE, CTL_EOL) != 0)
   4499 			break;
   4500 
   4501 		evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
   4502 		    NULL, device_xname(dev), "TSO");
   4503 		evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
   4504 		    NULL, adapter->queues[i].evnamebuf,
   4505 		    "Queue No Descriptor Available");
   4506 		evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
   4507 		    NULL, adapter->queues[i].evnamebuf,
   4508 		    "Queue Packets Transmitted");
   4509 #ifndef IXGBE_LEGACY_TX
   4510 		evcnt_attach_dynamic(&txr->br->br_drops, EVCNT_TYPE_MISC,
   4511 		    NULL, adapter->queues[i].evnamebuf,
   4512 		    "Packets dropped in buf_ring");
   4513 #endif
   4514 
   4515 #ifdef LRO
   4516 		struct lro_ctrl *lro = &rxr->lro;
   4517 #endif /* LRO */
   4518 
   4519 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4520 		    CTLFLAG_READONLY,
   4521 		    CTLTYPE_INT,
   4522 		    "rxd_head", SYSCTL_DESCR("Receive Descriptor Head"),
   4523 		    ixgbe_sysctl_rdh_handler, 0, (void *)rxr, 0,
   4524 		    CTL_CREATE, CTL_EOL) != 0)
   4525 			break;
   4526 
   4527 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4528 		    CTLFLAG_READONLY,
   4529 		    CTLTYPE_INT,
   4530 		    "rxd_tail", SYSCTL_DESCR("Receive Descriptor Tail"),
   4531 		    ixgbe_sysctl_rdt_handler, 0, (void *)rxr, 0,
   4532 		    CTL_CREATE, CTL_EOL) != 0)
   4533 			break;
   4534 
   4535 		if (i < __arraycount(stats->mpc)) {
   4536 			evcnt_attach_dynamic(&stats->mpc[i],
   4537 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4538 			    "Missed Packet Count");
   4539 		}
   4540 		if (i < __arraycount(stats->pxontxc)) {
   4541 			evcnt_attach_dynamic(&stats->pxontxc[i],
   4542 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4543 			    "pxontxc");
   4544 			evcnt_attach_dynamic(&stats->pxonrxc[i],
   4545 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4546 			    "pxonrxc");
   4547 			evcnt_attach_dynamic(&stats->pxofftxc[i],
   4548 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4549 			    "pxofftxc");
   4550 			evcnt_attach_dynamic(&stats->pxoffrxc[i],
   4551 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4552 			    "pxoffrxc");
   4553 			evcnt_attach_dynamic(&stats->pxon2offc[i],
   4554 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4555 			    "pxon2offc");
   4556 		}
   4557 		if (i < __arraycount(stats->qprc)) {
   4558 			evcnt_attach_dynamic(&stats->qprc[i],
   4559 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4560 			    "qprc");
   4561 			evcnt_attach_dynamic(&stats->qptc[i],
   4562 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4563 			    "qptc");
   4564 			evcnt_attach_dynamic(&stats->qbrc[i],
   4565 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4566 			    "qbrc");
   4567 			evcnt_attach_dynamic(&stats->qbtc[i],
   4568 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4569 			    "qbtc");
   4570 			evcnt_attach_dynamic(&stats->qprdc[i],
   4571 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4572 			    "qprdc");
   4573 		}
   4574 
   4575 		evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
   4576 		    NULL, adapter->queues[i].evnamebuf, "Queue Packets Received");
   4577 		evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
   4578 		    NULL, adapter->queues[i].evnamebuf, "Queue Bytes Received");
   4579 		evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
   4580 		    NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
   4581 		evcnt_attach_dynamic(&rxr->no_jmbuf, EVCNT_TYPE_MISC,
   4582 		    NULL, adapter->queues[i].evnamebuf, "Rx no jumbo mbuf");
   4583 		evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
   4584 		    NULL, adapter->queues[i].evnamebuf, "Rx discarded");
   4585 		evcnt_attach_dynamic(&rxr->rx_irq, EVCNT_TYPE_MISC,
   4586 		    NULL, adapter->queues[i].evnamebuf, "Rx interrupts");
   4587 #ifdef LRO
   4588 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
   4589 				CTLFLAG_RD, &lro->lro_queued, 0,
   4590 				"LRO Queued");
   4591 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
   4592 				CTLFLAG_RD, &lro->lro_flushed, 0,
   4593 				"LRO Flushed");
   4594 #endif /* LRO */
   4595 	}
   4596 
   4597 	/* MAC stats get the own sub node */
   4598 
   4599 
   4600 	snprintf(stats->namebuf,
   4601 	    sizeof(stats->namebuf), "%s MAC Statistics", device_xname(dev));
   4602 
   4603 	evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
   4604 	    stats->namebuf, "rx csum offload - IP");
   4605 	evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
   4606 	    stats->namebuf, "rx csum offload - L4");
   4607 	evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
   4608 	    stats->namebuf, "rx csum offload - IP bad");
   4609 	evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
   4610 	    stats->namebuf, "rx csum offload - L4 bad");
   4611 	evcnt_attach_dynamic(&stats->intzero, EVCNT_TYPE_MISC, NULL,
   4612 	    stats->namebuf, "Interrupt conditions zero");
   4613 	evcnt_attach_dynamic(&stats->legint, EVCNT_TYPE_MISC, NULL,
   4614 	    stats->namebuf, "Legacy interrupts");
   4615 	evcnt_attach_dynamic(&stats->crcerrs, EVCNT_TYPE_MISC, NULL,
   4616 	    stats->namebuf, "CRC Errors");
   4617 	evcnt_attach_dynamic(&stats->illerrc, EVCNT_TYPE_MISC, NULL,
   4618 	    stats->namebuf, "Illegal Byte Errors");
   4619 	evcnt_attach_dynamic(&stats->errbc, EVCNT_TYPE_MISC, NULL,
   4620 	    stats->namebuf, "Byte Errors");
   4621 	evcnt_attach_dynamic(&stats->mspdc, EVCNT_TYPE_MISC, NULL,
   4622 	    stats->namebuf, "MAC Short Packets Discarded");
   4623 	evcnt_attach_dynamic(&stats->mlfc, EVCNT_TYPE_MISC, NULL,
   4624 	    stats->namebuf, "MAC Local Faults");
   4625 	evcnt_attach_dynamic(&stats->mrfc, EVCNT_TYPE_MISC, NULL,
   4626 	    stats->namebuf, "MAC Remote Faults");
   4627 	evcnt_attach_dynamic(&stats->rlec, EVCNT_TYPE_MISC, NULL,
   4628 	    stats->namebuf, "Receive Length Errors");
   4629 	evcnt_attach_dynamic(&stats->lxontxc, EVCNT_TYPE_MISC, NULL,
   4630 	    stats->namebuf, "Link XON Transmitted");
   4631 	evcnt_attach_dynamic(&stats->lxonrxc, EVCNT_TYPE_MISC, NULL,
   4632 	    stats->namebuf, "Link XON Received");
   4633 	evcnt_attach_dynamic(&stats->lxofftxc, EVCNT_TYPE_MISC, NULL,
   4634 	    stats->namebuf, "Link XOFF Transmitted");
   4635 	evcnt_attach_dynamic(&stats->lxoffrxc, EVCNT_TYPE_MISC, NULL,
   4636 	    stats->namebuf, "Link XOFF Received");
   4637 
   4638 	/* Packet Reception Stats */
   4639 	evcnt_attach_dynamic(&stats->tor, EVCNT_TYPE_MISC, NULL,
   4640 	    stats->namebuf, "Total Octets Received");
   4641 	evcnt_attach_dynamic(&stats->gorc, EVCNT_TYPE_MISC, NULL,
   4642 	    stats->namebuf, "Good Octets Received");
   4643 	evcnt_attach_dynamic(&stats->tpr, EVCNT_TYPE_MISC, NULL,
   4644 	    stats->namebuf, "Total Packets Received");
   4645 	evcnt_attach_dynamic(&stats->gprc, EVCNT_TYPE_MISC, NULL,
   4646 	    stats->namebuf, "Good Packets Received");
   4647 	evcnt_attach_dynamic(&stats->mprc, EVCNT_TYPE_MISC, NULL,
   4648 	    stats->namebuf, "Multicast Packets Received");
   4649 	evcnt_attach_dynamic(&stats->bprc, EVCNT_TYPE_MISC, NULL,
   4650 	    stats->namebuf, "Broadcast Packets Received");
   4651 	evcnt_attach_dynamic(&stats->prc64, EVCNT_TYPE_MISC, NULL,
   4652 	    stats->namebuf, "64 byte frames received ");
   4653 	evcnt_attach_dynamic(&stats->prc127, EVCNT_TYPE_MISC, NULL,
   4654 	    stats->namebuf, "65-127 byte frames received");
   4655 	evcnt_attach_dynamic(&stats->prc255, EVCNT_TYPE_MISC, NULL,
   4656 	    stats->namebuf, "128-255 byte frames received");
   4657 	evcnt_attach_dynamic(&stats->prc511, EVCNT_TYPE_MISC, NULL,
   4658 	    stats->namebuf, "256-511 byte frames received");
   4659 	evcnt_attach_dynamic(&stats->prc1023, EVCNT_TYPE_MISC, NULL,
   4660 	    stats->namebuf, "512-1023 byte frames received");
   4661 	evcnt_attach_dynamic(&stats->prc1522, EVCNT_TYPE_MISC, NULL,
   4662 	    stats->namebuf, "1023-1522 byte frames received");
   4663 	evcnt_attach_dynamic(&stats->ruc, EVCNT_TYPE_MISC, NULL,
   4664 	    stats->namebuf, "Receive Undersized");
   4665 	evcnt_attach_dynamic(&stats->rfc, EVCNT_TYPE_MISC, NULL,
   4666 	    stats->namebuf, "Fragmented Packets Received ");
   4667 	evcnt_attach_dynamic(&stats->roc, EVCNT_TYPE_MISC, NULL,
   4668 	    stats->namebuf, "Oversized Packets Received");
   4669 	evcnt_attach_dynamic(&stats->rjc, EVCNT_TYPE_MISC, NULL,
   4670 	    stats->namebuf, "Received Jabber");
   4671 	evcnt_attach_dynamic(&stats->mngprc, EVCNT_TYPE_MISC, NULL,
   4672 	    stats->namebuf, "Management Packets Received");
   4673 	evcnt_attach_dynamic(&stats->xec, EVCNT_TYPE_MISC, NULL,
   4674 	    stats->namebuf, "Checksum Errors");
   4675 
   4676 	/* Packet Transmission Stats */
   4677 	evcnt_attach_dynamic(&stats->gotc, EVCNT_TYPE_MISC, NULL,
   4678 	    stats->namebuf, "Good Octets Transmitted");
   4679 	evcnt_attach_dynamic(&stats->tpt, EVCNT_TYPE_MISC, NULL,
   4680 	    stats->namebuf, "Total Packets Transmitted");
   4681 	evcnt_attach_dynamic(&stats->gptc, EVCNT_TYPE_MISC, NULL,
   4682 	    stats->namebuf, "Good Packets Transmitted");
   4683 	evcnt_attach_dynamic(&stats->bptc, EVCNT_TYPE_MISC, NULL,
   4684 	    stats->namebuf, "Broadcast Packets Transmitted");
   4685 	evcnt_attach_dynamic(&stats->mptc, EVCNT_TYPE_MISC, NULL,
   4686 	    stats->namebuf, "Multicast Packets Transmitted");
   4687 	evcnt_attach_dynamic(&stats->mngptc, EVCNT_TYPE_MISC, NULL,
   4688 	    stats->namebuf, "Management Packets Transmitted");
   4689 	evcnt_attach_dynamic(&stats->ptc64, EVCNT_TYPE_MISC, NULL,
   4690 	    stats->namebuf, "64 byte frames transmitted ");
   4691 	evcnt_attach_dynamic(&stats->ptc127, EVCNT_TYPE_MISC, NULL,
   4692 	    stats->namebuf, "65-127 byte frames transmitted");
   4693 	evcnt_attach_dynamic(&stats->ptc255, EVCNT_TYPE_MISC, NULL,
   4694 	    stats->namebuf, "128-255 byte frames transmitted");
   4695 	evcnt_attach_dynamic(&stats->ptc511, EVCNT_TYPE_MISC, NULL,
   4696 	    stats->namebuf, "256-511 byte frames transmitted");
   4697 	evcnt_attach_dynamic(&stats->ptc1023, EVCNT_TYPE_MISC, NULL,
   4698 	    stats->namebuf, "512-1023 byte frames transmitted");
   4699 	evcnt_attach_dynamic(&stats->ptc1522, EVCNT_TYPE_MISC, NULL,
   4700 	    stats->namebuf, "1024-1522 byte frames transmitted");
   4701 }
   4702 
   4703 /*
   4704 ** Set flow control using sysctl:
   4705 ** Flow control values:
   4706 ** 	0 - off
   4707 **	1 - rx pause
   4708 **	2 - tx pause
   4709 **	3 - full
   4710 */
   4711 static int
   4712 ixgbe_set_flowcntl(SYSCTLFN_ARGS)
   4713 {
   4714 	struct sysctlnode node = *rnode;
   4715 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   4716 	int error, last;
   4717 
   4718 	node.sysctl_data = &adapter->fc;
   4719 	last = adapter->fc;
   4720 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4721 	if (error != 0 || newp == NULL)
   4722 		return error;
   4723 
   4724 	/* Don't bother if it's not changed */
   4725 	if (adapter->fc == last)
   4726 		return (0);
   4727 
   4728 	switch (adapter->fc) {
   4729 		case ixgbe_fc_rx_pause:
   4730 		case ixgbe_fc_tx_pause:
   4731 		case ixgbe_fc_full:
   4732 			adapter->hw.fc.requested_mode = adapter->fc;
   4733 			if (adapter->num_queues > 1)
   4734 				ixgbe_disable_rx_drop(adapter);
   4735 			break;
   4736 		case ixgbe_fc_none:
   4737 			adapter->hw.fc.requested_mode = ixgbe_fc_none;
   4738 			if (adapter->num_queues > 1)
   4739 				ixgbe_enable_rx_drop(adapter);
   4740 			break;
   4741 		default:
   4742 			adapter->fc = last;
   4743 			return (EINVAL);
   4744 	}
   4745 	/* Don't autoneg if forcing a value */
   4746 	adapter->hw.fc.disable_fc_autoneg = TRUE;
   4747 	ixgbe_fc_enable(&adapter->hw);
   4748 	return 0;
   4749 }
   4750 
   4751 /*
   4752 ** Control advertised link speed:
   4753 **	Flags:
   4754 **	0x1 - advertise 100 Mb
   4755 **	0x2 - advertise 1G
   4756 **	0x4 - advertise 10G
   4757 */
   4758 static int
   4759 ixgbe_set_advertise(SYSCTLFN_ARGS)
   4760 {
   4761 	struct sysctlnode	node = *rnode;
   4762 	int			old, error = 0, requested;
   4763 	struct adapter		*adapter = (struct adapter *)node.sysctl_data;
   4764 	device_t		dev;
   4765 	struct ixgbe_hw		*hw;
   4766 	ixgbe_link_speed	speed = 0;
   4767 
   4768 	dev = adapter->dev;
   4769 	hw = &adapter->hw;
   4770 
   4771 	old = requested = adapter->advertise;
   4772 	node.sysctl_data = &requested;
   4773 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4774 	if (error != 0 || newp == NULL)
   4775 		return error;
   4776 
   4777 	/* Checks to validate new value */
   4778 	if (requested == old) /* no change */
   4779 		return (0);
   4780 
   4781 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
   4782 	    (hw->phy.multispeed_fiber))) {
   4783 		device_printf(dev,
   4784 		    "Advertised speed can only be set on copper or "
   4785 		    "multispeed fiber media types.\n");
   4786 		return (EINVAL);
   4787 	}
   4788 
   4789 	if (requested < 0x1 || requested > 0x7) {
   4790 		device_printf(dev,
   4791 		    "Invalid advertised speed; valid modes are 0x1 through 0x7\n");
   4792 		return (EINVAL);
   4793 	}
   4794 
   4795 	if ((requested & 0x1)
   4796 	    && (hw->mac.type != ixgbe_mac_X540)
   4797 	    && (hw->mac.type != ixgbe_mac_X550)) {
   4798 		device_printf(dev, "Set Advertise: 100Mb on X540/X550 only\n");
   4799 		return (EINVAL);
   4800 	}
   4801 
   4802 	adapter->advertise = requested;
   4803 
   4804 	/* Set new value and report new advertised mode */
   4805 	if (requested & 0x1)
   4806 		speed |= IXGBE_LINK_SPEED_100_FULL;
   4807 	if (requested & 0x2)
   4808 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
   4809 	if (requested & 0x4)
   4810 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
   4811 
   4812 	hw->mac.autotry_restart = TRUE;
   4813 	hw->mac.ops.setup_link(hw, speed, TRUE);
   4814 	adapter->advertise = requested;
   4815 
   4816 	return 0;
   4817 }
   4818 
   4819 /*
   4820  * The following two sysctls are for X550 BaseT devices;
   4821  * they deal with the external PHY used in them.
   4822  */
   4823 static int
   4824 ixgbe_sysctl_phy_temp(SYSCTLFN_ARGS)
   4825 {
   4826 	struct sysctlnode node = *rnode;
   4827 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   4828 	struct ixgbe_hw *hw = &adapter->hw;
   4829 	int val;
   4830 	u16 reg;
   4831 	int		error;
   4832 
   4833 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
   4834 		device_printf(adapter->dev,
   4835 		    "Device has no supported external thermal sensor.\n");
   4836 		return (ENODEV);
   4837 	}
   4838 
   4839 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_CURRENT_TEMP,
   4840 				      IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
   4841 				      &reg)) {
   4842 		device_printf(adapter->dev,
   4843 		    "Error reading from PHY's current temperature register\n");
   4844 		return (EAGAIN);
   4845 	}
   4846 
   4847 	node.sysctl_data = &val;
   4848 
   4849 	/* Shift temp for output */
   4850 	val = reg >> 8;
   4851 
   4852 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4853 	if ((error) || (newp == NULL))
   4854 		return (error);
   4855 
   4856 	return (0);
   4857 }
   4858 
   4859 /*
   4860  * Reports whether the current PHY temperature is over
   4861  * the overtemp threshold.
   4862  *  - This is reported directly from the PHY
   4863  */
   4864 static int
   4865 ixgbe_sysctl_phy_overtemp_occurred(SYSCTLFN_ARGS)
   4866 {
   4867 	struct sysctlnode node = *rnode;
   4868 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   4869 	struct ixgbe_hw *hw = &adapter->hw;
   4870 	int val, error;
   4871 	u16 reg;
   4872 
   4873 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
   4874 		device_printf(adapter->dev,
   4875 		    "Device has no supported external thermal sensor.\n");
   4876 		return (ENODEV);
   4877 	}
   4878 
   4879 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_OVERTEMP_STATUS,
   4880 				      IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
   4881 				      &reg)) {
   4882 		device_printf(adapter->dev,
   4883 		    "Error reading from PHY's temperature status register\n");
   4884 		return (EAGAIN);
   4885 	}
   4886 
   4887 	node.sysctl_data = &val;
   4888 
   4889 	/* Get occurrence bit */
   4890 	val = !!(reg & 0x4000);
   4891 
   4892 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4893 	if ((error) || (newp == NULL))
   4894 		return (error);
   4895 
   4896 	return (0);
   4897 }
   4898 
   4899 /*
   4900 ** Thermal Shutdown Trigger (internal MAC)
   4901 **   - Set this to 1 to cause an overtemp event to occur
   4902 */
   4903 static int
   4904 ixgbe_sysctl_thermal_test(SYSCTLFN_ARGS)
   4905 {
   4906 	struct sysctlnode node = *rnode;
   4907 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   4908 	int		error, fire = 0;
   4909 	struct ixgbe_hw *hw;
   4910 
   4911 	hw = &adapter->hw;
   4912 
   4913 	node.sysctl_data = &fire;
   4914 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4915 	if ((error) || (newp == NULL))
   4916 		return (error);
   4917 
   4918 	if (fire) {
   4919 		u32 reg = IXGBE_READ_REG(hw, IXGBE_EICS);
   4920 		reg |= IXGBE_EICR_TS;
   4921 		IXGBE_WRITE_REG(hw, IXGBE_EICS, reg);
   4922 	}
   4923 
   4924 	return (0);
   4925 }
   4926 
   4927 /*
   4928 ** Manage DMA Coalescing.
   4929 ** Control values:
   4930 ** 	0/1 - off / on (use default value of 1000)
   4931 **
   4932 **	Legal timer values are:
   4933 **	50,100,250,500,1000,2000,5000,10000
   4934 **
   4935 **	Turning off interrupt moderation will also turn this off.
   4936 */
   4937 static int
   4938 ixgbe_sysctl_dmac(SYSCTLFN_ARGS)
   4939 {
   4940 	struct sysctlnode node = *rnode;
   4941 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   4942 	struct ixgbe_hw *hw = &adapter->hw;
   4943 	struct ifnet *ifp = adapter->ifp;
   4944 	int		error;
   4945 	u16		oldval;
   4946 	int		buffer;
   4947 
   4948 	oldval = adapter->dmac;
   4949 	buffer = oldval;
   4950 	node.sysctl_data = &buffer;
   4951 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4952 	if ((error) || (newp == NULL))
   4953 		return (error);
   4954 
   4955 	switch (hw->mac.type) {
   4956 	case ixgbe_mac_X550:
   4957 	case ixgbe_mac_X550EM_x:
   4958 		break;
   4959 	default:
   4960 		device_printf(adapter->dev,
   4961 		    "DMA Coalescing is only supported on X550 devices\n");
   4962 		return (ENODEV);
   4963 	}
   4964 
   4965 	switch (adapter->dmac) {
   4966 	case 0:
   4967 		/* Disabled */
   4968 		break;
   4969 	case 1: /* Enable and use default */
   4970 		adapter->dmac = 1000;
   4971 		break;
   4972 	case 50:
   4973 	case 100:
   4974 	case 250:
   4975 	case 500:
   4976 	case 1000:
   4977 	case 2000:
   4978 	case 5000:
   4979 	case 10000:
   4980 		/* Legal values - allow */
   4981 		break;
   4982 	default:
   4983 		/* Do nothing, illegal value */
   4984 		adapter->dmac = oldval;
   4985 		return (EINVAL);
   4986 	}
   4987 
   4988 	/* Re-initialize hardware if it's already running */
   4989 	if (ifp->if_flags & IFF_RUNNING)
   4990 		ixgbe_init(ifp);
   4991 
   4992 	return (0);
   4993 }
   4994 
   4995 /*
   4996  * Sysctl to enable/disable the WoL capability, if supported by the adapter.
   4997  * Values:
   4998  *	0 - disabled
   4999  *	1 - enabled
   5000  */
   5001 static int
   5002 ixgbe_sysctl_wol_enable(SYSCTLFN_ARGS)
   5003 {
   5004 	struct sysctlnode node = *rnode;
   5005 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5006 	struct ixgbe_hw *hw = &adapter->hw;
   5007 	int new_wol_enabled;
   5008 	int error = 0;
   5009 
   5010 	new_wol_enabled = hw->wol_enabled;
   5011 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5012 	if ((error) || (newp == NULL))
   5013 		return (error);
   5014 	if (new_wol_enabled == hw->wol_enabled)
   5015 		return (0);
   5016 
   5017 	if (new_wol_enabled > 0 && !adapter->wol_support)
   5018 		return (ENODEV);
   5019 	else
   5020 		hw->wol_enabled = !!(new_wol_enabled);
   5021 
   5022 	return (0);
   5023 }
   5024 
   5025 /*
   5026  * Sysctl to enable/disable the Energy Efficient Ethernet capability,
   5027  * if supported by the adapter.
   5028  * Values:
   5029  *	0 - disabled
   5030  *	1 - enabled
   5031  */
   5032 static int
   5033 ixgbe_sysctl_eee_enable(SYSCTLFN_ARGS)
   5034 {
   5035 	struct sysctlnode node = *rnode;
   5036 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5037 	struct ixgbe_hw *hw = &adapter->hw;
   5038 	struct ifnet *ifp = adapter->ifp;
   5039 	int new_eee_enabled, error = 0;
   5040 
   5041 	new_eee_enabled = adapter->eee_enabled;
   5042 	node.sysctl_data = &new_eee_enabled;
   5043 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5044 	if ((error) || (newp == NULL))
   5045 		return (error);
   5046 	if (new_eee_enabled == adapter->eee_enabled)
   5047 		return (0);
   5048 
   5049 	if (new_eee_enabled > 0 && !hw->mac.ops.setup_eee)
   5050 		return (ENODEV);
   5051 	else
   5052 		adapter->eee_enabled = !!(new_eee_enabled);
   5053 
   5054 	/* Re-initialize hardware if it's already running */
   5055 	if (ifp->if_flags & IFF_RUNNING)
   5056 		ixgbe_init(ifp);
   5057 
   5058 	return (0);
   5059 }
   5060 
   5061 /*
   5062  * Read-only sysctl indicating whether EEE support was negotiated
   5063  * on the link.
   5064  */
   5065 static int
   5066 ixgbe_sysctl_eee_negotiated(SYSCTLFN_ARGS)
   5067 {
   5068 	struct sysctlnode node = *rnode;
   5069 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5070 	struct ixgbe_hw *hw = &adapter->hw;
   5071 	bool status;
   5072 
   5073 	status = !!(IXGBE_READ_REG(hw, IXGBE_EEE_STAT) & IXGBE_EEE_STAT_NEG);
   5074 
   5075 	node.sysctl_data = &status;
   5076 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   5077 }
   5078 
   5079 /*
   5080  * Read-only sysctl indicating whether RX Link is in LPI state.
   5081  */
   5082 static int
   5083 ixgbe_sysctl_eee_rx_lpi_status(SYSCTLFN_ARGS)
   5084 {
   5085 	struct sysctlnode node = *rnode;
   5086 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5087 	struct ixgbe_hw *hw = &adapter->hw;
   5088 	bool status;
   5089 
   5090 	status = !!(IXGBE_READ_REG(hw, IXGBE_EEE_STAT) &
   5091 	    IXGBE_EEE_RX_LPI_STATUS);
   5092 
   5093 	node.sysctl_data = &status;
   5094 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   5095 }
   5096 
   5097 /*
   5098  * Read-only sysctl indicating whether TX Link is in LPI state.
   5099  */
   5100 static int
   5101 ixgbe_sysctl_eee_tx_lpi_status(SYSCTLFN_ARGS)
   5102 {
   5103 	struct sysctlnode node = *rnode;
   5104 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5105 	struct ixgbe_hw *hw = &adapter->hw;
   5106 	bool status;
   5107 
   5108 	status = !!(IXGBE_READ_REG(hw, IXGBE_EEE_STAT) &
   5109 	    IXGBE_EEE_TX_LPI_STATUS);
   5110 
   5111 	node.sysctl_data = &status;
   5112 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   5113 }
   5114 
   5115 /*
   5116  * Sysctl to enable/disable the types of packets that the
   5117  * adapter will wake up on upon receipt.
   5118  * WUFC - Wake Up Filter Control
   5119  * Flags:
   5120  *	0x1  - Link Status Change
   5121  *	0x2  - Magic Packet
   5122  *	0x4  - Direct Exact
   5123  *	0x8  - Directed Multicast
   5124  *	0x10 - Broadcast
   5125  *	0x20 - ARP/IPv4 Request Packet
   5126  *	0x40 - Direct IPv4 Packet
   5127  *	0x80 - Direct IPv6 Packet
   5128  *
   5129  * Setting another flag will cause the sysctl to return an
   5130  * error.
   5131  */
   5132 static int
   5133 ixgbe_sysctl_wufc(SYSCTLFN_ARGS)
   5134 {
   5135 	struct sysctlnode node = *rnode;
   5136 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5137 	int error = 0;
   5138 	u32 new_wufc;
   5139 
   5140 	new_wufc = adapter->wufc;
   5141 
   5142 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5143 	if ((error) || (newp == NULL))
   5144 		return (error);
   5145 	if (new_wufc == adapter->wufc)
   5146 		return (0);
   5147 
   5148 	if (new_wufc & 0xffffff00)
   5149 		return (EINVAL);
   5150 	else {
   5151 		new_wufc &= 0xff;
   5152 		new_wufc |= (0xffffff & adapter->wufc);
   5153 		adapter->wufc = new_wufc;
   5154 	}
   5155 
   5156 	return (0);
   5157 }
   5158 
   5159 /*
   5160 ** Enable the hardware to drop packets when the buffer is
   5161 ** full. This is useful when multiqueue,so that no single
   5162 ** queue being full stalls the entire RX engine. We only
   5163 ** enable this when Multiqueue AND when Flow Control is
   5164 ** disabled.
   5165 */
   5166 static void
   5167 ixgbe_enable_rx_drop(struct adapter *adapter)
   5168 {
   5169         struct ixgbe_hw *hw = &adapter->hw;
   5170 
   5171 	for (int i = 0; i < adapter->num_queues; i++) {
   5172 		struct rx_ring *rxr = &adapter->rx_rings[i];
   5173         	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
   5174         	srrctl |= IXGBE_SRRCTL_DROP_EN;
   5175         	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
   5176 	}
   5177 #ifdef PCI_IOV
   5178 	/* enable drop for each vf */
   5179 	for (int i = 0; i < adapter->num_vfs; i++) {
   5180 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
   5181 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT) |
   5182 		    IXGBE_QDE_ENABLE));
   5183 	}
   5184 #endif
   5185 }
   5186 
   5187 static void
   5188 ixgbe_disable_rx_drop(struct adapter *adapter)
   5189 {
   5190         struct ixgbe_hw *hw = &adapter->hw;
   5191 
   5192 	for (int i = 0; i < adapter->num_queues; i++) {
   5193 		struct rx_ring *rxr = &adapter->rx_rings[i];
   5194         	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
   5195         	srrctl &= ~IXGBE_SRRCTL_DROP_EN;
   5196         	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
   5197 	}
   5198 #ifdef PCI_IOV
   5199 	/* disable drop for each vf */
   5200 	for (int i = 0; i < adapter->num_vfs; i++) {
   5201 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
   5202 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT)));
   5203 	}
   5204 #endif
   5205 }
   5206 
   5207 #ifdef PCI_IOV
   5208 
   5209 /*
   5210 ** Support functions for SRIOV/VF management
   5211 */
   5212 
   5213 static void
   5214 ixgbe_ping_all_vfs(struct adapter *adapter)
   5215 {
   5216 	struct ixgbe_vf *vf;
   5217 
   5218 	for (int i = 0; i < adapter->num_vfs; i++) {
   5219 		vf = &adapter->vfs[i];
   5220 		if (vf->flags & IXGBE_VF_ACTIVE)
   5221 			ixgbe_send_vf_msg(adapter, vf, IXGBE_PF_CONTROL_MSG);
   5222 	}
   5223 }
   5224 
   5225 
   5226 static void
   5227 ixgbe_vf_set_default_vlan(struct adapter *adapter, struct ixgbe_vf *vf,
   5228     uint16_t tag)
   5229 {
   5230 	struct ixgbe_hw *hw;
   5231 	uint32_t vmolr, vmvir;
   5232 
   5233 	hw = &adapter->hw;
   5234 
   5235 	vf->vlan_tag = tag;
   5236 
   5237 	vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf->pool));
   5238 
   5239 	/* Do not receive packets that pass inexact filters. */
   5240 	vmolr &= ~(IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_ROPE);
   5241 
   5242 	/* Disable Multicast Promicuous Mode. */
   5243 	vmolr &= ~IXGBE_VMOLR_MPE;
   5244 
   5245 	/* Accept broadcasts. */
   5246 	vmolr |= IXGBE_VMOLR_BAM;
   5247 
   5248 	if (tag == 0) {
   5249 		/* Accept non-vlan tagged traffic. */
   5250 		//vmolr |= IXGBE_VMOLR_AUPE;
   5251 
   5252 		/* Allow VM to tag outgoing traffic; no default tag. */
   5253 		vmvir = 0;
   5254 	} else {
   5255 		/* Require vlan-tagged traffic. */
   5256 		vmolr &= ~IXGBE_VMOLR_AUPE;
   5257 
   5258 		/* Tag all traffic with provided vlan tag. */
   5259 		vmvir = (tag | IXGBE_VMVIR_VLANA_DEFAULT);
   5260 	}
   5261 	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf->pool), vmolr);
   5262 	IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf->pool), vmvir);
   5263 }
   5264 
   5265 
   5266 static boolean_t
   5267 ixgbe_vf_frame_size_compatible(struct adapter *adapter, struct ixgbe_vf *vf)
   5268 {
   5269 
   5270 	/*
   5271 	 * Frame size compatibility between PF and VF is only a problem on
   5272 	 * 82599-based cards.  X540 and later support any combination of jumbo
   5273 	 * frames on PFs and VFs.
   5274 	 */
   5275 	if (adapter->hw.mac.type != ixgbe_mac_82599EB)
   5276 		return (TRUE);
   5277 
   5278 	switch (vf->api_ver) {
   5279 	case IXGBE_API_VER_1_0:
   5280 	case IXGBE_API_VER_UNKNOWN:
   5281 		/*
   5282 		 * On legacy (1.0 and older) VF versions, we don't support jumbo
   5283 		 * frames on either the PF or the VF.
   5284 		 */
   5285 		if (adapter->max_frame_size > ETHER_MAX_LEN ||
   5286 		    vf->max_frame_size > ETHER_MAX_LEN)
   5287 		    return (FALSE);
   5288 
   5289 		return (TRUE);
   5290 
   5291 		break;
   5292 	case IXGBE_API_VER_1_1:
   5293 	default:
   5294 		/*
   5295 		 * 1.1 or later VF versions always work if they aren't using
   5296 		 * jumbo frames.
   5297 		 */
   5298 		if (vf->max_frame_size <= ETHER_MAX_LEN)
   5299 			return (TRUE);
   5300 
   5301 		/*
   5302 		 * Jumbo frames only work with VFs if the PF is also using jumbo
   5303 		 * frames.
   5304 		 */
   5305 		if (adapter->max_frame_size <= ETHER_MAX_LEN)
   5306 			return (TRUE);
   5307 
   5308 		return (FALSE);
   5309 
   5310 	}
   5311 }
   5312 
   5313 
   5314 static void
   5315 ixgbe_process_vf_reset(struct adapter *adapter, struct ixgbe_vf *vf)
   5316 {
   5317 	ixgbe_vf_set_default_vlan(adapter, vf, vf->default_vlan);
   5318 
   5319 	// XXX clear multicast addresses
   5320 
   5321 	ixgbe_clear_rar(&adapter->hw, vf->rar_index);
   5322 
   5323 	vf->api_ver = IXGBE_API_VER_UNKNOWN;
   5324 }
   5325 
   5326 
   5327 static void
   5328 ixgbe_vf_enable_transmit(struct adapter *adapter, struct ixgbe_vf *vf)
   5329 {
   5330 	struct ixgbe_hw *hw;
   5331 	uint32_t vf_index, vfte;
   5332 
   5333 	hw = &adapter->hw;
   5334 
   5335 	vf_index = IXGBE_VF_INDEX(vf->pool);
   5336 	vfte = IXGBE_READ_REG(hw, IXGBE_VFTE(vf_index));
   5337 	vfte |= IXGBE_VF_BIT(vf->pool);
   5338 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(vf_index), vfte);
   5339 }
   5340 
   5341 
   5342 static void
   5343 ixgbe_vf_enable_receive(struct adapter *adapter, struct ixgbe_vf *vf)
   5344 {
   5345 	struct ixgbe_hw *hw;
   5346 	uint32_t vf_index, vfre;
   5347 
   5348 	hw = &adapter->hw;
   5349 
   5350 	vf_index = IXGBE_VF_INDEX(vf->pool);
   5351 	vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(vf_index));
   5352 	if (ixgbe_vf_frame_size_compatible(adapter, vf))
   5353 		vfre |= IXGBE_VF_BIT(vf->pool);
   5354 	else
   5355 		vfre &= ~IXGBE_VF_BIT(vf->pool);
   5356 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(vf_index), vfre);
   5357 }
   5358 
   5359 
   5360 static void
   5361 ixgbe_vf_reset_msg(struct adapter *adapter, struct ixgbe_vf *vf, uint32_t *msg)
   5362 {
   5363 	struct ixgbe_hw *hw;
   5364 	uint32_t ack;
   5365 	uint32_t resp[IXGBE_VF_PERMADDR_MSG_LEN];
   5366 
   5367 	hw = &adapter->hw;
   5368 
   5369 	ixgbe_process_vf_reset(adapter, vf);
   5370 
   5371 	if (ixgbe_validate_mac_addr(vf->ether_addr) == 0) {
   5372 		ixgbe_set_rar(&adapter->hw, vf->rar_index,
   5373 		    vf->ether_addr, vf->pool, TRUE);
   5374 		ack = IXGBE_VT_MSGTYPE_ACK;
   5375 	} else
   5376 		ack = IXGBE_VT_MSGTYPE_NACK;
   5377 
   5378 	ixgbe_vf_enable_transmit(adapter, vf);
   5379 	ixgbe_vf_enable_receive(adapter, vf);
   5380 
   5381 	vf->flags |= IXGBE_VF_CTS;
   5382 
   5383 	resp[0] = IXGBE_VF_RESET | ack | IXGBE_VT_MSGTYPE_CTS;
   5384 	bcopy(vf->ether_addr, &resp[1], ETHER_ADDR_LEN);
   5385 	resp[3] = hw->mac.mc_filter_type;
   5386 	ixgbe_write_mbx(hw, resp, IXGBE_VF_PERMADDR_MSG_LEN, vf->pool);
   5387 }
   5388 
   5389 
   5390 static void
   5391 ixgbe_vf_set_mac(struct adapter *adapter, struct ixgbe_vf *vf, uint32_t *msg)
   5392 {
   5393 	uint8_t *mac;
   5394 
   5395 	mac = (uint8_t*)&msg[1];
   5396 
   5397 	/* Check that the VF has permission to change the MAC address. */
   5398 	if (!(vf->flags & IXGBE_VF_CAP_MAC) && ixgbe_vf_mac_changed(vf, mac)) {
   5399 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   5400 		return;
   5401 	}
   5402 
   5403 	if (ixgbe_validate_mac_addr(mac) != 0) {
   5404 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   5405 		return;
   5406 	}
   5407 
   5408 	bcopy(mac, vf->ether_addr, ETHER_ADDR_LEN);
   5409 
   5410 	ixgbe_set_rar(&adapter->hw, vf->rar_index, vf->ether_addr,
   5411 	    vf->pool, TRUE);
   5412 
   5413 	ixgbe_send_vf_ack(adapter, vf, msg[0]);
   5414 }
   5415 
   5416 
   5417 /*
   5418 ** VF multicast addresses are set by using the appropriate bit in
   5419 ** 1 of 128 32 bit addresses (4096 possible).
   5420 */
   5421 static void
   5422 ixgbe_vf_set_mc_addr(struct adapter *adapter, struct ixgbe_vf *vf, u32 *msg)
   5423 {
   5424 	u16	*list = (u16*)&msg[1];
   5425 	int	entries;
   5426 	u32	vmolr, vec_bit, vec_reg, mta_reg;
   5427 
   5428 	entries = (msg[0] & IXGBE_VT_MSGINFO_MASK) >> IXGBE_VT_MSGINFO_SHIFT;
   5429 	entries = min(entries, IXGBE_MAX_VF_MC);
   5430 
   5431 	vmolr = IXGBE_READ_REG(&adapter->hw, IXGBE_VMOLR(vf->pool));
   5432 
   5433 	vf->num_mc_hashes = entries;
   5434 
   5435 	/* Set the appropriate MTA bit */
   5436 	for (int i = 0; i < entries; i++) {
   5437 		vf->mc_hash[i] = list[i];
   5438 		vec_reg = (vf->mc_hash[i] >> 5) & 0x7F;
   5439                 vec_bit = vf->mc_hash[i] & 0x1F;
   5440                 mta_reg = IXGBE_READ_REG(&adapter->hw, IXGBE_MTA(vec_reg));
   5441                 mta_reg |= (1 << vec_bit);
   5442                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_MTA(vec_reg), mta_reg);
   5443         }
   5444 
   5445 	vmolr |= IXGBE_VMOLR_ROMPE;
   5446 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VMOLR(vf->pool), vmolr);
   5447 	ixgbe_send_vf_ack(adapter, vf, msg[0]);
   5448 	return;
   5449 }
   5450 
   5451 
   5452 static void
   5453 ixgbe_vf_set_vlan(struct adapter *adapter, struct ixgbe_vf *vf, uint32_t *msg)
   5454 {
   5455 	struct ixgbe_hw *hw;
   5456 	int enable;
   5457 	uint16_t tag;
   5458 
   5459 	hw = &adapter->hw;
   5460 	enable = IXGBE_VT_MSGINFO(msg[0]);
   5461 	tag = msg[1] & IXGBE_VLVF_VLANID_MASK;
   5462 
   5463 	if (!(vf->flags & IXGBE_VF_CAP_VLAN)) {
   5464 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   5465 		return;
   5466 	}
   5467 
   5468 	/* It is illegal to enable vlan tag 0. */
   5469 	if (tag == 0 && enable != 0){
   5470 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   5471 		return;
   5472 	}
   5473 
   5474 	ixgbe_set_vfta(hw, tag, vf->pool, enable);
   5475 	ixgbe_send_vf_ack(adapter, vf, msg[0]);
   5476 }
   5477 
   5478 
   5479 static void
   5480 ixgbe_vf_set_lpe(struct adapter *adapter, struct ixgbe_vf *vf, uint32_t *msg)
   5481 {
   5482 	struct ixgbe_hw *hw;
   5483 	uint32_t vf_max_size, pf_max_size, mhadd;
   5484 
   5485 	hw = &adapter->hw;
   5486 	vf_max_size = msg[1];
   5487 
   5488 	if (vf_max_size < ETHER_CRC_LEN) {
   5489 		/* We intentionally ACK invalid LPE requests. */
   5490 		ixgbe_send_vf_ack(adapter, vf, msg[0]);
   5491 		return;
   5492 	}
   5493 
   5494 	vf_max_size -= ETHER_CRC_LEN;
   5495 
   5496 	if (vf_max_size > IXGBE_MAX_FRAME_SIZE) {
   5497 		/* We intentionally ACK invalid LPE requests. */
   5498 		ixgbe_send_vf_ack(adapter, vf, msg[0]);
   5499 		return;
   5500 	}
   5501 
   5502 	vf->max_frame_size = vf_max_size;
   5503 	ixgbe_update_max_frame(adapter, vf->max_frame_size);
   5504 
   5505 	/*
   5506 	 * We might have to disable reception to this VF if the frame size is
   5507 	 * not compatible with the config on the PF.
   5508 	 */
   5509 	ixgbe_vf_enable_receive(adapter, vf);
   5510 
   5511 	mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
   5512 	pf_max_size = (mhadd & IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
   5513 
   5514 	if (pf_max_size < adapter->max_frame_size) {
   5515 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
   5516 		mhadd |= adapter->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
   5517 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
   5518 	}
   5519 
   5520 	ixgbe_send_vf_ack(adapter, vf, msg[0]);
   5521 }
   5522 
   5523 
   5524 static void
   5525 ixgbe_vf_set_macvlan(struct adapter *adapter, struct ixgbe_vf *vf,
   5526     uint32_t *msg)
   5527 {
   5528 	//XXX implement this
   5529 	ixgbe_send_vf_nack(adapter, vf, msg[0]);
   5530 }
   5531 
   5532 
   5533 static void
   5534 ixgbe_vf_api_negotiate(struct adapter *adapter, struct ixgbe_vf *vf,
   5535     uint32_t *msg)
   5536 {
   5537 
   5538 	switch (msg[1]) {
   5539 	case IXGBE_API_VER_1_0:
   5540 	case IXGBE_API_VER_1_1:
   5541 		vf->api_ver = msg[1];
   5542 		ixgbe_send_vf_ack(adapter, vf, msg[0]);
   5543 		break;
   5544 	default:
   5545 		vf->api_ver = IXGBE_API_VER_UNKNOWN;
   5546 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   5547 		break;
   5548 	}
   5549 }
   5550 
   5551 
   5552 static void
   5553 ixgbe_vf_get_queues(struct adapter *adapter, struct ixgbe_vf *vf,
   5554     uint32_t *msg)
   5555 {
   5556 	struct ixgbe_hw *hw;
   5557 	uint32_t resp[IXGBE_VF_GET_QUEUES_RESP_LEN];
   5558 	int num_queues;
   5559 
   5560 	hw = &adapter->hw;
   5561 
   5562 	/* GET_QUEUES is not supported on pre-1.1 APIs. */
   5563 	switch (msg[0]) {
   5564 	case IXGBE_API_VER_1_0:
   5565 	case IXGBE_API_VER_UNKNOWN:
   5566 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   5567 		return;
   5568 	}
   5569 
   5570 	resp[0] = IXGBE_VF_GET_QUEUES | IXGBE_VT_MSGTYPE_ACK |
   5571 	    IXGBE_VT_MSGTYPE_CTS;
   5572 
   5573 	num_queues = ixgbe_vf_queues(ixgbe_get_iov_mode(adapter));
   5574 	resp[IXGBE_VF_TX_QUEUES] = num_queues;
   5575 	resp[IXGBE_VF_RX_QUEUES] = num_queues;
   5576 	resp[IXGBE_VF_TRANS_VLAN] = (vf->default_vlan != 0);
   5577 	resp[IXGBE_VF_DEF_QUEUE] = 0;
   5578 
   5579 	ixgbe_write_mbx(hw, resp, IXGBE_VF_GET_QUEUES_RESP_LEN, vf->pool);
   5580 }
   5581 
   5582 
   5583 static void
   5584 ixgbe_process_vf_msg(struct adapter *adapter, struct ixgbe_vf *vf)
   5585 {
   5586 	struct ixgbe_hw *hw;
   5587 	uint32_t msg[IXGBE_VFMAILBOX_SIZE];
   5588 	int error;
   5589 
   5590 	hw = &adapter->hw;
   5591 
   5592 	error = ixgbe_read_mbx(hw, msg, IXGBE_VFMAILBOX_SIZE, vf->pool);
   5593 
   5594 	if (error != 0)
   5595 		return;
   5596 
   5597 	CTR3(KTR_MALLOC, "%s: received msg %x from %d",
   5598 	    adapter->ifp->if_xname, msg[0], vf->pool);
   5599 	if (msg[0] == IXGBE_VF_RESET) {
   5600 		ixgbe_vf_reset_msg(adapter, vf, msg);
   5601 		return;
   5602 	}
   5603 
   5604 	if (!(vf->flags & IXGBE_VF_CTS)) {
   5605 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   5606 		return;
   5607 	}
   5608 
   5609 	switch (msg[0] & IXGBE_VT_MSG_MASK) {
   5610 	case IXGBE_VF_SET_MAC_ADDR:
   5611 		ixgbe_vf_set_mac(adapter, vf, msg);
   5612 		break;
   5613 	case IXGBE_VF_SET_MULTICAST:
   5614 		ixgbe_vf_set_mc_addr(adapter, vf, msg);
   5615 		break;
   5616 	case IXGBE_VF_SET_VLAN:
   5617 		ixgbe_vf_set_vlan(adapter, vf, msg);
   5618 		break;
   5619 	case IXGBE_VF_SET_LPE:
   5620 		ixgbe_vf_set_lpe(adapter, vf, msg);
   5621 		break;
   5622 	case IXGBE_VF_SET_MACVLAN:
   5623 		ixgbe_vf_set_macvlan(adapter, vf, msg);
   5624 		break;
   5625 	case IXGBE_VF_API_NEGOTIATE:
   5626 		ixgbe_vf_api_negotiate(adapter, vf, msg);
   5627 		break;
   5628 	case IXGBE_VF_GET_QUEUES:
   5629 		ixgbe_vf_get_queues(adapter, vf, msg);
   5630 		break;
   5631 	default:
   5632 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   5633 	}
   5634 }
   5635 
   5636 
   5637 /*
   5638  * Tasklet for handling VF -> PF mailbox messages.
   5639  */
   5640 static void
   5641 ixgbe_handle_mbx(void *context, int pending)
   5642 {
   5643 	struct adapter *adapter;
   5644 	struct ixgbe_hw *hw;
   5645 	struct ixgbe_vf *vf;
   5646 	int i;
   5647 
   5648 	adapter = context;
   5649 	hw = &adapter->hw;
   5650 
   5651 	IXGBE_CORE_LOCK(adapter);
   5652 	for (i = 0; i < adapter->num_vfs; i++) {
   5653 		vf = &adapter->vfs[i];
   5654 
   5655 		if (vf->flags & IXGBE_VF_ACTIVE) {
   5656 			if (ixgbe_check_for_rst(hw, vf->pool) == 0)
   5657 				ixgbe_process_vf_reset(adapter, vf);
   5658 
   5659 			if (ixgbe_check_for_msg(hw, vf->pool) == 0)
   5660 				ixgbe_process_vf_msg(adapter, vf);
   5661 
   5662 			if (ixgbe_check_for_ack(hw, vf->pool) == 0)
   5663 				ixgbe_process_vf_ack(adapter, vf);
   5664 		}
   5665 	}
   5666 	IXGBE_CORE_UNLOCK(adapter);
   5667 }
   5668 
   5669 
   5670 static int
   5671 ixgbe_init_iov(device_t dev, u16 num_vfs, const nvlist_t *config)
   5672 {
   5673 	struct adapter *adapter;
   5674 	enum ixgbe_iov_mode mode;
   5675 
   5676 	adapter = device_get_softc(dev);
   5677 	adapter->num_vfs = num_vfs;
   5678 	mode = ixgbe_get_iov_mode(adapter);
   5679 
   5680 	if (num_vfs > ixgbe_max_vfs(mode)) {
   5681 		adapter->num_vfs = 0;
   5682 		return (ENOSPC);
   5683 	}
   5684 
   5685 	IXGBE_CORE_LOCK(adapter);
   5686 
   5687 	adapter->vfs = malloc(sizeof(*adapter->vfs) * num_vfs, M_IXGBE,
   5688 	    M_NOWAIT | M_ZERO);
   5689 
   5690 	if (adapter->vfs == NULL) {
   5691 		adapter->num_vfs = 0;
   5692 		IXGBE_CORE_UNLOCK(adapter);
   5693 		return (ENOMEM);
   5694 	}
   5695 
   5696 	ixgbe_init_locked(adapter);
   5697 
   5698 	IXGBE_CORE_UNLOCK(adapter);
   5699 
   5700 	return (0);
   5701 }
   5702 
   5703 
   5704 static void
   5705 ixgbe_uninit_iov(device_t dev)
   5706 {
   5707 	struct ixgbe_hw *hw;
   5708 	struct adapter *adapter;
   5709 	uint32_t pf_reg, vf_reg;
   5710 
   5711 	adapter = device_get_softc(dev);
   5712 	hw = &adapter->hw;
   5713 
   5714 	IXGBE_CORE_LOCK(adapter);
   5715 
   5716 	/* Enable rx/tx for the PF and disable it for all VFs. */
   5717 	pf_reg = IXGBE_VF_INDEX(adapter->pool);
   5718 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(pf_reg),
   5719 	    IXGBE_VF_BIT(adapter->pool));
   5720 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(pf_reg),
   5721 	    IXGBE_VF_BIT(adapter->pool));
   5722 
   5723 	if (pf_reg == 0)
   5724 		vf_reg = 1;
   5725 	else
   5726 		vf_reg = 0;
   5727 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(vf_reg), 0);
   5728 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(vf_reg), 0);
   5729 
   5730 	IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, 0);
   5731 
   5732 	free(adapter->vfs, M_IXGBE);
   5733 	adapter->vfs = NULL;
   5734 	adapter->num_vfs = 0;
   5735 
   5736 	IXGBE_CORE_UNLOCK(adapter);
   5737 }
   5738 
   5739 
   5740 static void
   5741 ixgbe_initialize_iov(struct adapter *adapter)
   5742 {
   5743 	struct ixgbe_hw *hw = &adapter->hw;
   5744 	uint32_t mrqc, mtqc, vt_ctl, vf_reg, gcr_ext, gpie;
   5745 	enum ixgbe_iov_mode mode;
   5746 	int i;
   5747 
   5748 	mode = ixgbe_get_iov_mode(adapter);
   5749 	if (mode == IXGBE_NO_VM)
   5750 		return;
   5751 
   5752 	IXGBE_CORE_LOCK_ASSERT(adapter);
   5753 
   5754 	mrqc = IXGBE_READ_REG(hw, IXGBE_MRQC);
   5755 	mrqc &= ~IXGBE_MRQC_MRQE_MASK;
   5756 
   5757 	switch (mode) {
   5758 	case IXGBE_64_VM:
   5759 		mrqc |= IXGBE_MRQC_VMDQRSS64EN;
   5760 		break;
   5761 	case IXGBE_32_VM:
   5762 		mrqc |= IXGBE_MRQC_VMDQRSS32EN;
   5763 		break;
   5764 	default:
   5765 		panic("Unexpected SR-IOV mode %d", mode);
   5766 	}
   5767 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
   5768 
   5769 	mtqc = IXGBE_MTQC_VT_ENA;
   5770 	switch (mode) {
   5771 	case IXGBE_64_VM:
   5772 		mtqc |= IXGBE_MTQC_64VF;
   5773 		break;
   5774 	case IXGBE_32_VM:
   5775 		mtqc |= IXGBE_MTQC_32VF;
   5776 		break;
   5777 	default:
   5778 		panic("Unexpected SR-IOV mode %d", mode);
   5779 	}
   5780 	IXGBE_WRITE_REG(hw, IXGBE_MTQC, mtqc);
   5781 
   5782 
   5783 	gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
   5784 	gcr_ext |= IXGBE_GCR_EXT_MSIX_EN;
   5785 	gcr_ext &= ~IXGBE_GCR_EXT_VT_MODE_MASK;
   5786 	switch (mode) {
   5787 	case IXGBE_64_VM:
   5788 		gcr_ext |= IXGBE_GCR_EXT_VT_MODE_64;
   5789 		break;
   5790 	case IXGBE_32_VM:
   5791 		gcr_ext |= IXGBE_GCR_EXT_VT_MODE_32;
   5792 		break;
   5793 	default:
   5794 		panic("Unexpected SR-IOV mode %d", mode);
   5795 	}
   5796 	IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
   5797 
   5798 
   5799 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
   5800 	gcr_ext &= ~IXGBE_GPIE_VTMODE_MASK;
   5801 	switch (mode) {
   5802 	case IXGBE_64_VM:
   5803 		gpie |= IXGBE_GPIE_VTMODE_64;
   5804 		break;
   5805 	case IXGBE_32_VM:
   5806 		gpie |= IXGBE_GPIE_VTMODE_32;
   5807 		break;
   5808 	default:
   5809 		panic("Unexpected SR-IOV mode %d", mode);
   5810 	}
   5811 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
   5812 
   5813 	/* Enable rx/tx for the PF. */
   5814 	vf_reg = IXGBE_VF_INDEX(adapter->pool);
   5815 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(vf_reg),
   5816 	    IXGBE_VF_BIT(adapter->pool));
   5817 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(vf_reg),
   5818 	    IXGBE_VF_BIT(adapter->pool));
   5819 
   5820 	/* Allow VM-to-VM communication. */
   5821 	IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
   5822 
   5823 	vt_ctl = IXGBE_VT_CTL_VT_ENABLE | IXGBE_VT_CTL_REPLEN;
   5824 	vt_ctl |= (adapter->pool << IXGBE_VT_CTL_POOL_SHIFT);
   5825 	IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vt_ctl);
   5826 
   5827 	for (i = 0; i < adapter->num_vfs; i++)
   5828 		ixgbe_init_vf(adapter, &adapter->vfs[i]);
   5829 }
   5830 
   5831 
   5832 /*
   5833 ** Check the max frame setting of all active VF's
   5834 */
   5835 static void
   5836 ixgbe_recalculate_max_frame(struct adapter *adapter)
   5837 {
   5838 	struct ixgbe_vf *vf;
   5839 
   5840 	IXGBE_CORE_LOCK_ASSERT(adapter);
   5841 
   5842 	for (int i = 0; i < adapter->num_vfs; i++) {
   5843 		vf = &adapter->vfs[i];
   5844 		if (vf->flags & IXGBE_VF_ACTIVE)
   5845 			ixgbe_update_max_frame(adapter, vf->max_frame_size);
   5846 	}
   5847 }
   5848 
   5849 
   5850 static void
   5851 ixgbe_init_vf(struct adapter *adapter, struct ixgbe_vf *vf)
   5852 {
   5853 	struct ixgbe_hw *hw;
   5854 	uint32_t vf_index, pfmbimr;
   5855 
   5856 	IXGBE_CORE_LOCK_ASSERT(adapter);
   5857 
   5858 	hw = &adapter->hw;
   5859 
   5860 	if (!(vf->flags & IXGBE_VF_ACTIVE))
   5861 		return;
   5862 
   5863 	vf_index = IXGBE_VF_INDEX(vf->pool);
   5864 	pfmbimr = IXGBE_READ_REG(hw, IXGBE_PFMBIMR(vf_index));
   5865 	pfmbimr |= IXGBE_VF_BIT(vf->pool);
   5866 	IXGBE_WRITE_REG(hw, IXGBE_PFMBIMR(vf_index), pfmbimr);
   5867 
   5868 	ixgbe_vf_set_default_vlan(adapter, vf, vf->vlan_tag);
   5869 
   5870 	// XXX multicast addresses
   5871 
   5872 	if (ixgbe_validate_mac_addr(vf->ether_addr) == 0) {
   5873 		ixgbe_set_rar(&adapter->hw, vf->rar_index,
   5874 		    vf->ether_addr, vf->pool, TRUE);
   5875 	}
   5876 
   5877 	ixgbe_vf_enable_transmit(adapter, vf);
   5878 	ixgbe_vf_enable_receive(adapter, vf);
   5879 
   5880 	ixgbe_send_vf_msg(adapter, vf, IXGBE_PF_CONTROL_MSG);
   5881 }
   5882 
   5883 static int
   5884 ixgbe_add_vf(device_t dev, u16 vfnum, const nvlist_t *config)
   5885 {
   5886 	struct adapter *adapter;
   5887 	struct ixgbe_vf *vf;
   5888 	const void *mac;
   5889 
   5890 	adapter = device_get_softc(dev);
   5891 
   5892 	KASSERT(vfnum < adapter->num_vfs, ("VF index %d is out of range %d",
   5893 	    vfnum, adapter->num_vfs));
   5894 
   5895 	IXGBE_CORE_LOCK(adapter);
   5896 	vf = &adapter->vfs[vfnum];
   5897 	vf->pool= vfnum;
   5898 
   5899 	/* RAR[0] is used by the PF so use vfnum + 1 for VF RAR. */
   5900 	vf->rar_index = vfnum + 1;
   5901 	vf->default_vlan = 0;
   5902 	vf->max_frame_size = ETHER_MAX_LEN;
   5903 	ixgbe_update_max_frame(adapter, vf->max_frame_size);
   5904 
   5905 	if (nvlist_exists_binary(config, "mac-addr")) {
   5906 		mac = nvlist_get_binary(config, "mac-addr", NULL);
   5907 		bcopy(mac, vf->ether_addr, ETHER_ADDR_LEN);
   5908 		if (nvlist_get_bool(config, "allow-set-mac"))
   5909 			vf->flags |= IXGBE_VF_CAP_MAC;
   5910 	} else
   5911 		/*
   5912 		 * If the administrator has not specified a MAC address then
   5913 		 * we must allow the VF to choose one.
   5914 		 */
   5915 		vf->flags |= IXGBE_VF_CAP_MAC;
   5916 
   5917 	vf->flags = IXGBE_VF_ACTIVE;
   5918 
   5919 	ixgbe_init_vf(adapter, vf);
   5920 	IXGBE_CORE_UNLOCK(adapter);
   5921 
   5922 	return (0);
   5923 }
   5924 #endif /* PCI_IOV */
   5925 static void
   5926 ixgbe_rearm_queues(struct adapter *adapter, u64 queues)
   5927 {
   5928 	u32 mask;
   5929 
   5930 	switch (adapter->hw.mac.type) {
   5931 	case ixgbe_mac_82598EB:
   5932 		mask = (IXGBE_EIMS_RTX_QUEUE & queues);
   5933 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, mask);
   5934 		break;
   5935 	case ixgbe_mac_82599EB:
   5936 	case ixgbe_mac_X540:
   5937 	case ixgbe_mac_X550:
   5938 	case ixgbe_mac_X550EM_x:
   5939 		mask = (queues & 0xFFFFFFFF);
   5940 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(0), mask);
   5941 		mask = (queues >> 32);
   5942 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(1), mask);
   5943 		break;
   5944 	default:
   5945 		break;
   5946 	}
   5947 }
   5948