Home | History | Annotate | Line # | Download | only in ixgbe
ixgbe.c revision 1.88.2.44
      1 /* $NetBSD: ixgbe.c,v 1.88.2.44 2021/09/15 16:38:00 martin Exp $ */
      2 
      3 /******************************************************************************
      4 
      5   Copyright (c) 2001-2017, Intel Corporation
      6   All rights reserved.
      7 
      8   Redistribution and use in source and binary forms, with or without
      9   modification, are permitted provided that the following conditions are met:
     10 
     11    1. Redistributions of source code must retain the above copyright notice,
     12       this list of conditions and the following disclaimer.
     13 
     14    2. Redistributions in binary form must reproduce the above copyright
     15       notice, this list of conditions and the following disclaimer in the
     16       documentation and/or other materials provided with the distribution.
     17 
     18    3. Neither the name of the Intel Corporation nor the names of its
     19       contributors may be used to endorse or promote products derived from
     20       this software without specific prior written permission.
     21 
     22   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     23   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     26   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32   POSSIBILITY OF SUCH DAMAGE.
     33 
     34 ******************************************************************************/
     35 /*$FreeBSD: head/sys/dev/ixgbe/if_ix.c 331224 2018-03-19 20:55:05Z erj $*/
     36 
     37 /*
     38  * Copyright (c) 2011 The NetBSD Foundation, Inc.
     39  * All rights reserved.
     40  *
     41  * This code is derived from software contributed to The NetBSD Foundation
     42  * by Coyote Point Systems, Inc.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     54  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63  * POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 #include <sys/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: ixgbe.c,v 1.88.2.44 2021/09/15 16:38:00 martin Exp $");
     68 
     69 #ifdef _KERNEL_OPT
     70 #include "opt_inet.h"
     71 #include "opt_inet6.h"
     72 #include "opt_net_mpsafe.h"
     73 #endif
     74 
     75 #include "ixgbe.h"
     76 #include "ixgbe_sriov.h"
     77 #include "vlan.h"
     78 
     79 #include <sys/cprng.h>
     80 #include <dev/mii/mii.h>
     81 #include <dev/mii/miivar.h>
     82 
     83 /************************************************************************
     84  * Driver version
     85  ************************************************************************/
     86 static const char ixgbe_driver_version[] = "4.0.1-k";
     87 /* XXX NetBSD: + 3.3.10 */
     88 
     89 /************************************************************************
     90  * PCI Device ID Table
     91  *
     92  *   Used by probe to select devices to load on
     93  *   Last field stores an index into ixgbe_strings
     94  *   Last entry must be all 0s
     95  *
     96  *   { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
     97  ************************************************************************/
     98 static const ixgbe_vendor_info_t ixgbe_vendor_info_array[] =
     99 {
    100 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_DUAL_PORT, 0, 0, 0},
    101 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_SINGLE_PORT, 0, 0, 0},
    102 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_CX4, 0, 0, 0},
    103 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT, 0, 0, 0},
    104 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT2, 0, 0, 0},
    105 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598, 0, 0, 0},
    106 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_BX, 0, 0, 0},
    107 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_DA_DUAL_PORT, 0, 0, 0},
    108 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_CX4_DUAL_PORT, 0, 0, 0},
    109 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_XF_LR, 0, 0, 0},
    110 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM, 0, 0, 0},
    111 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_SFP_LOM, 0, 0, 0},
    112 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KR, 0, 0, 0},
    113 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4, 0, 0, 0},
    114 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4_MEZZ, 0, 0, 0},
    115 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP, 0, 0, 0},
    116 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_EM, 0, 0, 0},
    117 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_XAUI_LOM, 0, 0, 0},
    118 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_CX4, 0, 0, 0},
    119 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_T3_LOM, 0, 0, 0},
    120 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_COMBO_BACKPLANE, 0, 0, 0},
    121 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BACKPLANE_FCOE, 0, 0, 0},
    122 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF2, 0, 0, 0},
    123 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_FCOE, 0, 0, 0},
    124 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599EN_SFP, 0, 0, 0},
    125 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF_QP, 0, 0, 0},
    126 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_QSFP_SF_QP, 0, 0, 0},
    127 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T, 0, 0, 0},
    128 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T1, 0, 0, 0},
    129 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T, 0, 0, 0},
    130 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T1, 0, 0, 0},
    131 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KR, 0, 0, 0},
    132 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KX4, 0, 0, 0},
    133 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_10G_T, 0, 0, 0},
    134 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_1G_T, 0, 0, 0},
    135 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_SFP, 0, 0, 0},
    136 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_XFI, 0, 0, 0},
    137 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR, 0, 0, 0},
    138 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR_L, 0, 0, 0},
    139 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_QSFP, 0, 0, 0},
    140 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_QSFP_N, 0, 0, 0},
    141 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP, 0, 0, 0},
    142 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP_N, 0, 0, 0},
    143 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII, 0, 0, 0},
    144 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII_L, 0, 0, 0},
    145 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_10G_T, 0, 0, 0},
    146 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T, 0, 0, 0},
    147 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T_L, 0, 0, 0},
    148 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540_BYPASS, 0, 0, 0},
    149 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BYPASS, 0, 0, 0},
    150 	/* required last entry */
    151 	{0, 0, 0, 0, 0}
    152 };
    153 
    154 /************************************************************************
    155  * Table of branding strings
    156  ************************************************************************/
    157 static const char    *ixgbe_strings[] = {
    158 	"Intel(R) PRO/10GbE PCI-Express Network Driver"
    159 };
    160 
    161 /************************************************************************
    162  * Function prototypes
    163  ************************************************************************/
    164 static int	ixgbe_probe(device_t, cfdata_t, void *);
    165 static void	ixgbe_attach(device_t, device_t, void *);
    166 static int	ixgbe_detach(device_t, int);
    167 #if 0
    168 static int	ixgbe_shutdown(device_t);
    169 #endif
    170 static bool	ixgbe_suspend(device_t, const pmf_qual_t *);
    171 static bool	ixgbe_resume(device_t, const pmf_qual_t *);
    172 static int	ixgbe_ifflags_cb(struct ethercom *);
    173 static int	ixgbe_ioctl(struct ifnet *, u_long, void *);
    174 static int	ixgbe_init(struct ifnet *);
    175 static void	ixgbe_init_locked(struct adapter *);
    176 static void	ixgbe_ifstop(struct ifnet *, int);
    177 static void	ixgbe_stop_locked(void *);
    178 static void	ixgbe_init_device_features(struct adapter *);
    179 static void	ixgbe_check_fan_failure(struct adapter *, u32, bool);
    180 static void	ixgbe_add_media_types(struct adapter *);
    181 static void	ixgbe_media_status(struct ifnet *, struct ifmediareq *);
    182 static int	ixgbe_media_change(struct ifnet *);
    183 static int	ixgbe_allocate_pci_resources(struct adapter *,
    184 		    const struct pci_attach_args *);
    185 static void	ixgbe_free_softint(struct adapter *);
    186 static void	ixgbe_get_slot_info(struct adapter *);
    187 static int	ixgbe_allocate_msix(struct adapter *,
    188 		    const struct pci_attach_args *);
    189 static int	ixgbe_allocate_legacy(struct adapter *,
    190 		    const struct pci_attach_args *);
    191 static int	ixgbe_configure_interrupts(struct adapter *);
    192 static void	ixgbe_free_pciintr_resources(struct adapter *);
    193 static void	ixgbe_free_pci_resources(struct adapter *);
    194 static void	ixgbe_local_timer(void *);
    195 static void	ixgbe_local_timer1(void *);
    196 static void	ixgbe_recovery_mode_timer(void *);
    197 static int	ixgbe_setup_interface(device_t, struct adapter *);
    198 static void	ixgbe_config_gpie(struct adapter *);
    199 static void	ixgbe_config_dmac(struct adapter *);
    200 static void	ixgbe_config_delay_values(struct adapter *);
    201 static void	ixgbe_config_link(struct adapter *);
    202 static void	ixgbe_check_wol_support(struct adapter *);
    203 static int	ixgbe_setup_low_power_mode(struct adapter *);
    204 static void	ixgbe_rearm_queues(struct adapter *, u64);
    205 
    206 static void	ixgbe_initialize_transmit_units(struct adapter *);
    207 static void	ixgbe_initialize_receive_units(struct adapter *);
    208 static void	ixgbe_enable_rx_drop(struct adapter *);
    209 static void	ixgbe_disable_rx_drop(struct adapter *);
    210 static void	ixgbe_initialize_rss_mapping(struct adapter *);
    211 
    212 static void	ixgbe_enable_intr(struct adapter *);
    213 static void	ixgbe_disable_intr(struct adapter *);
    214 static void	ixgbe_update_stats_counters(struct adapter *);
    215 static void	ixgbe_set_rxfilter(struct adapter *);
    216 static void	ixgbe_update_link_status(struct adapter *);
    217 static void	ixgbe_set_ivar(struct adapter *, u8, u8, s8);
    218 static void	ixgbe_configure_ivars(struct adapter *);
    219 static u8 *	ixgbe_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
    220 static void	ixgbe_eitr_write(struct adapter *, uint32_t, uint32_t);
    221 
    222 static void	ixgbe_setup_vlan_hw_support(struct adapter *);
    223 #if 0
    224 static void	ixgbe_register_vlan(void *, struct ifnet *, u16);
    225 static void	ixgbe_unregister_vlan(void *, struct ifnet *, u16);
    226 #endif
    227 
    228 static void	ixgbe_add_device_sysctls(struct adapter *);
    229 static void	ixgbe_add_hw_stats(struct adapter *);
    230 static void	ixgbe_clear_evcnt(struct adapter *);
    231 static int	ixgbe_set_flowcntl(struct adapter *, int);
    232 static int	ixgbe_set_advertise(struct adapter *, int);
    233 static int	ixgbe_get_advertise(struct adapter *);
    234 
    235 /* Sysctl handlers */
    236 static void	ixgbe_set_sysctl_value(struct adapter *, const char *,
    237 		     const char *, int *, int);
    238 static int	ixgbe_sysctl_flowcntl(SYSCTLFN_PROTO);
    239 static int	ixgbe_sysctl_advertise(SYSCTLFN_PROTO);
    240 static int	ixgbe_sysctl_interrupt_rate_handler(SYSCTLFN_PROTO);
    241 static int	ixgbe_sysctl_dmac(SYSCTLFN_PROTO);
    242 static int	ixgbe_sysctl_phy_temp(SYSCTLFN_PROTO);
    243 static int	ixgbe_sysctl_phy_overtemp_occurred(SYSCTLFN_PROTO);
    244 #ifdef IXGBE_DEBUG
    245 static int	ixgbe_sysctl_power_state(SYSCTLFN_PROTO);
    246 static int	ixgbe_sysctl_print_rss_config(SYSCTLFN_PROTO);
    247 #endif
    248 static int	ixgbe_sysctl_next_to_check_handler(SYSCTLFN_PROTO);
    249 static int	ixgbe_sysctl_next_to_refresh_handler(SYSCTLFN_PROTO);
    250 static int	ixgbe_sysctl_rdh_handler(SYSCTLFN_PROTO);
    251 static int	ixgbe_sysctl_rdt_handler(SYSCTLFN_PROTO);
    252 static int	ixgbe_sysctl_tdt_handler(SYSCTLFN_PROTO);
    253 static int	ixgbe_sysctl_tdh_handler(SYSCTLFN_PROTO);
    254 static int	ixgbe_sysctl_eee_state(SYSCTLFN_PROTO);
    255 static int	ixgbe_sysctl_debug(SYSCTLFN_PROTO);
    256 static int	ixgbe_sysctl_rx_copy_len(SYSCTLFN_PROTO);
    257 static int	ixgbe_sysctl_wol_enable(SYSCTLFN_PROTO);
    258 static int	ixgbe_sysctl_wufc(SYSCTLFN_PROTO);
    259 
    260 /* Support for pluggable optic modules */
    261 static bool	ixgbe_sfp_probe(struct adapter *);
    262 
    263 /* Legacy (single vector) interrupt handler */
    264 static int	ixgbe_legacy_irq(void *);
    265 
    266 /* The MSI/MSI-X Interrupt handlers */
    267 static int	ixgbe_msix_que(void *);
    268 static int	ixgbe_msix_link(void *);
    269 
    270 /* Software interrupts for deferred work */
    271 static void	ixgbe_handle_que(void *);
    272 static void	ixgbe_handle_link(void *);
    273 static void	ixgbe_handle_msf(void *);
    274 static void	ixgbe_handle_mod(void *);
    275 static void	ixgbe_handle_phy(void *);
    276 
    277 /* Workqueue handler for deferred work */
    278 static void	ixgbe_handle_que_work(struct work *, void *);
    279 
    280 static const ixgbe_vendor_info_t *ixgbe_lookup(const struct pci_attach_args *);
    281 
    282 /************************************************************************
    283  *  NetBSD Device Interface Entry Points
    284  ************************************************************************/
    285 CFATTACH_DECL3_NEW(ixg, sizeof(struct adapter),
    286     ixgbe_probe, ixgbe_attach, ixgbe_detach, NULL, NULL, NULL,
    287     DVF_DETACH_SHUTDOWN);
    288 
    289 #if 0
    290 devclass_t ix_devclass;
    291 DRIVER_MODULE(ix, pci, ix_driver, ix_devclass, 0, 0);
    292 
    293 MODULE_DEPEND(ix, pci, 1, 1, 1);
    294 MODULE_DEPEND(ix, ether, 1, 1, 1);
    295 #ifdef DEV_NETMAP
    296 MODULE_DEPEND(ix, netmap, 1, 1, 1);
    297 #endif
    298 #endif
    299 
    300 /*
    301  * TUNEABLE PARAMETERS:
    302  */
    303 
    304 /*
    305  * AIM: Adaptive Interrupt Moderation
    306  * which means that the interrupt rate
    307  * is varied over time based on the
    308  * traffic for that interrupt vector
    309  */
    310 static bool ixgbe_enable_aim = true;
    311 #define SYSCTL_INT(_a1, _a2, _a3, _a4, _a5, _a6, _a7)
    312 SYSCTL_INT(_hw_ix, OID_AUTO, enable_aim, CTLFLAG_RDTUN, &ixgbe_enable_aim, 0,
    313     "Enable adaptive interrupt moderation");
    314 
    315 static int ixgbe_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
    316 SYSCTL_INT(_hw_ix, OID_AUTO, max_interrupt_rate, CTLFLAG_RDTUN,
    317     &ixgbe_max_interrupt_rate, 0, "Maximum interrupts per second");
    318 
    319 /* How many packets rxeof tries to clean at a time */
    320 static int ixgbe_rx_process_limit = 256;
    321 SYSCTL_INT(_hw_ix, OID_AUTO, rx_process_limit, CTLFLAG_RDTUN,
    322     &ixgbe_rx_process_limit, 0, "Maximum number of received packets to process at a time, -1 means unlimited");
    323 
    324 /* How many packets txeof tries to clean at a time */
    325 static int ixgbe_tx_process_limit = 256;
    326 SYSCTL_INT(_hw_ix, OID_AUTO, tx_process_limit, CTLFLAG_RDTUN,
    327     &ixgbe_tx_process_limit, 0,
    328     "Maximum number of sent packets to process at a time, -1 means unlimited");
    329 
    330 /* Flow control setting, default to full */
    331 static int ixgbe_flow_control = ixgbe_fc_full;
    332 SYSCTL_INT(_hw_ix, OID_AUTO, flow_control, CTLFLAG_RDTUN,
    333     &ixgbe_flow_control, 0, "Default flow control used for all adapters");
    334 
    335 /* Which packet processing uses workqueue or softint */
    336 static bool ixgbe_txrx_workqueue = false;
    337 
    338 /*
    339  * Smart speed setting, default to on
    340  * this only works as a compile option
    341  * right now as its during attach, set
    342  * this to 'ixgbe_smart_speed_off' to
    343  * disable.
    344  */
    345 static int ixgbe_smart_speed = ixgbe_smart_speed_on;
    346 
    347 /*
    348  * MSI-X should be the default for best performance,
    349  * but this allows it to be forced off for testing.
    350  */
    351 static int ixgbe_enable_msix = 1;
    352 SYSCTL_INT(_hw_ix, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &ixgbe_enable_msix, 0,
    353     "Enable MSI-X interrupts");
    354 
    355 /*
    356  * Number of Queues, can be set to 0,
    357  * it then autoconfigures based on the
    358  * number of cpus with a max of 8. This
    359  * can be overridden manually here.
    360  */
    361 static int ixgbe_num_queues = 0;
    362 SYSCTL_INT(_hw_ix, OID_AUTO, num_queues, CTLFLAG_RDTUN, &ixgbe_num_queues, 0,
    363     "Number of queues to configure, 0 indicates autoconfigure");
    364 
    365 /*
    366  * Number of TX descriptors per ring,
    367  * setting higher than RX as this seems
    368  * the better performing choice.
    369  */
    370 static int ixgbe_txd = PERFORM_TXD;
    371 SYSCTL_INT(_hw_ix, OID_AUTO, txd, CTLFLAG_RDTUN, &ixgbe_txd, 0,
    372     "Number of transmit descriptors per queue");
    373 
    374 /* Number of RX descriptors per ring */
    375 static int ixgbe_rxd = PERFORM_RXD;
    376 SYSCTL_INT(_hw_ix, OID_AUTO, rxd, CTLFLAG_RDTUN, &ixgbe_rxd, 0,
    377     "Number of receive descriptors per queue");
    378 
    379 /*
    380  * Defining this on will allow the use
    381  * of unsupported SFP+ modules, note that
    382  * doing so you are on your own :)
    383  */
    384 static int allow_unsupported_sfp = false;
    385 #define TUNABLE_INT(__x, __y)
    386 TUNABLE_INT("hw.ix.unsupported_sfp", &allow_unsupported_sfp);
    387 
    388 /*
    389  * Not sure if Flow Director is fully baked,
    390  * so we'll default to turning it off.
    391  */
    392 static int ixgbe_enable_fdir = 0;
    393 SYSCTL_INT(_hw_ix, OID_AUTO, enable_fdir, CTLFLAG_RDTUN, &ixgbe_enable_fdir, 0,
    394     "Enable Flow Director");
    395 
    396 /* Legacy Transmit (single queue) */
    397 static int ixgbe_enable_legacy_tx = 0;
    398 SYSCTL_INT(_hw_ix, OID_AUTO, enable_legacy_tx, CTLFLAG_RDTUN,
    399     &ixgbe_enable_legacy_tx, 0, "Enable Legacy TX flow");
    400 
    401 /* Receive-Side Scaling */
    402 static int ixgbe_enable_rss = 1;
    403 SYSCTL_INT(_hw_ix, OID_AUTO, enable_rss, CTLFLAG_RDTUN, &ixgbe_enable_rss, 0,
    404     "Enable Receive-Side Scaling (RSS)");
    405 
    406 #if 0
    407 static int (*ixgbe_start_locked)(struct ifnet *, struct tx_ring *);
    408 static int (*ixgbe_ring_empty)(struct ifnet *, pcq_t *);
    409 #endif
    410 
    411 #ifdef NET_MPSAFE
    412 #define IXGBE_MPSAFE		1
    413 #define IXGBE_CALLOUT_FLAGS	CALLOUT_MPSAFE
    414 #define IXGBE_SOFTINT_FLAGS	SOFTINT_MPSAFE
    415 #define IXGBE_WORKQUEUE_FLAGS	WQ_PERCPU | WQ_MPSAFE
    416 #else
    417 #define IXGBE_CALLOUT_FLAGS	0
    418 #define IXGBE_SOFTINT_FLAGS	0
    419 #define IXGBE_WORKQUEUE_FLAGS	WQ_PERCPU
    420 #endif
    421 #define IXGBE_WORKQUEUE_PRI PRI_SOFTNET
    422 
    423 /************************************************************************
    424  * ixgbe_initialize_rss_mapping
    425  ************************************************************************/
    426 static void
    427 ixgbe_initialize_rss_mapping(struct adapter *adapter)
    428 {
    429 	struct ixgbe_hw	*hw = &adapter->hw;
    430 	u32		reta = 0, mrqc, rss_key[10];
    431 	int		queue_id, table_size, index_mult;
    432 	int		i, j;
    433 	u32		rss_hash_config;
    434 
    435 	/* force use default RSS key. */
    436 #ifdef __NetBSD__
    437 	rss_getkey((uint8_t *) &rss_key);
    438 #else
    439 	if (adapter->feat_en & IXGBE_FEATURE_RSS) {
    440 		/* Fetch the configured RSS key */
    441 		rss_getkey((uint8_t *) &rss_key);
    442 	} else {
    443 		/* set up random bits */
    444 		cprng_fast(&rss_key, sizeof(rss_key));
    445 	}
    446 #endif
    447 
    448 	/* Set multiplier for RETA setup and table size based on MAC */
    449 	index_mult = 0x1;
    450 	table_size = 128;
    451 	switch (adapter->hw.mac.type) {
    452 	case ixgbe_mac_82598EB:
    453 		index_mult = 0x11;
    454 		break;
    455 	case ixgbe_mac_X550:
    456 	case ixgbe_mac_X550EM_x:
    457 	case ixgbe_mac_X550EM_a:
    458 		table_size = 512;
    459 		break;
    460 	default:
    461 		break;
    462 	}
    463 
    464 	/* Set up the redirection table */
    465 	for (i = 0, j = 0; i < table_size; i++, j++) {
    466 		if (j == adapter->num_queues)
    467 			j = 0;
    468 
    469 		if (adapter->feat_en & IXGBE_FEATURE_RSS) {
    470 			/*
    471 			 * Fetch the RSS bucket id for the given indirection
    472 			 * entry. Cap it at the number of configured buckets
    473 			 * (which is num_queues.)
    474 			 */
    475 			queue_id = rss_get_indirection_to_bucket(i);
    476 			queue_id = queue_id % adapter->num_queues;
    477 		} else
    478 			queue_id = (j * index_mult);
    479 
    480 		/*
    481 		 * The low 8 bits are for hash value (n+0);
    482 		 * The next 8 bits are for hash value (n+1), etc.
    483 		 */
    484 		reta = reta >> 8;
    485 		reta = reta | (((uint32_t) queue_id) << 24);
    486 		if ((i & 3) == 3) {
    487 			if (i < 128)
    488 				IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
    489 			else
    490 				IXGBE_WRITE_REG(hw, IXGBE_ERETA((i >> 2) - 32),
    491 				    reta);
    492 			reta = 0;
    493 		}
    494 	}
    495 
    496 	/* Now fill our hash function seeds */
    497 	for (i = 0; i < 10; i++)
    498 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
    499 
    500 	/* Perform hash on these packet types */
    501 	if (adapter->feat_en & IXGBE_FEATURE_RSS)
    502 		rss_hash_config = rss_gethashconfig();
    503 	else {
    504 		/*
    505 		 * Disable UDP - IP fragments aren't currently being handled
    506 		 * and so we end up with a mix of 2-tuple and 4-tuple
    507 		 * traffic.
    508 		 */
    509 		rss_hash_config = RSS_HASHTYPE_RSS_IPV4
    510 				| RSS_HASHTYPE_RSS_TCP_IPV4
    511 				| RSS_HASHTYPE_RSS_IPV6
    512 				| RSS_HASHTYPE_RSS_TCP_IPV6
    513 				| RSS_HASHTYPE_RSS_IPV6_EX
    514 				| RSS_HASHTYPE_RSS_TCP_IPV6_EX;
    515 	}
    516 
    517 	mrqc = IXGBE_MRQC_RSSEN;
    518 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
    519 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
    520 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
    521 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
    522 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
    523 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
    524 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
    525 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
    526 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
    527 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
    528 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
    529 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
    530 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
    531 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
    532 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
    533 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
    534 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
    535 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
    536 	mrqc |= ixgbe_get_mrqc(adapter->iov_mode);
    537 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
    538 } /* ixgbe_initialize_rss_mapping */
    539 
    540 /************************************************************************
    541  * ixgbe_initialize_receive_units - Setup receive registers and features.
    542  ************************************************************************/
    543 #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
    544 
    545 static void
    546 ixgbe_initialize_receive_units(struct adapter *adapter)
    547 {
    548 	struct	rx_ring	*rxr = adapter->rx_rings;
    549 	struct ixgbe_hw	*hw = &adapter->hw;
    550 	struct ifnet	*ifp = adapter->ifp;
    551 	int		i, j;
    552 	u32		bufsz, fctrl, srrctl, rxcsum;
    553 	u32		hlreg;
    554 
    555 	/*
    556 	 * Make sure receives are disabled while
    557 	 * setting up the descriptor ring
    558 	 */
    559 	ixgbe_disable_rx(hw);
    560 
    561 	/* Enable broadcasts */
    562 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
    563 	fctrl |= IXGBE_FCTRL_BAM;
    564 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
    565 		fctrl |= IXGBE_FCTRL_DPF;
    566 		fctrl |= IXGBE_FCTRL_PMCF;
    567 	}
    568 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
    569 
    570 	/* Set for Jumbo Frames? */
    571 	hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
    572 	if (ifp->if_mtu > ETHERMTU)
    573 		hlreg |= IXGBE_HLREG0_JUMBOEN;
    574 	else
    575 		hlreg &= ~IXGBE_HLREG0_JUMBOEN;
    576 
    577 #ifdef DEV_NETMAP
    578 	/* CRC stripping is conditional in Netmap */
    579 	if ((adapter->feat_en & IXGBE_FEATURE_NETMAP) &&
    580 	    (ifp->if_capenable & IFCAP_NETMAP) &&
    581 	    !ix_crcstrip)
    582 		hlreg &= ~IXGBE_HLREG0_RXCRCSTRP;
    583 	else
    584 #endif /* DEV_NETMAP */
    585 		hlreg |= IXGBE_HLREG0_RXCRCSTRP;
    586 
    587 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
    588 
    589 	bufsz = (adapter->rx_mbuf_sz + BSIZEPKT_ROUNDUP) >>
    590 	    IXGBE_SRRCTL_BSIZEPKT_SHIFT;
    591 
    592 	for (i = 0; i < adapter->num_queues; i++, rxr++) {
    593 		u64 rdba = rxr->rxdma.dma_paddr;
    594 		u32 reg;
    595 		int regnum = i / 4;	/* 1 register per 4 queues */
    596 		int regshift = i % 4;	/* 4 bits per 1 queue */
    597 		j = rxr->me;
    598 
    599 		/* Setup the Base and Length of the Rx Descriptor Ring */
    600 		IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j),
    601 		    (rdba & 0x00000000ffffffffULL));
    602 		IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
    603 		IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j),
    604 		    adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
    605 
    606 		/* Set up the SRRCTL register */
    607 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(j));
    608 		srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
    609 		srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
    610 		srrctl |= bufsz;
    611 		srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
    612 
    613 		/* Set RQSMR (Receive Queue Statistic Mapping) register */
    614 		reg = IXGBE_READ_REG(hw, IXGBE_RQSMR(regnum));
    615 		reg &= ~(0x000000ffUL << (regshift * 8));
    616 		reg |= i << (regshift * 8);
    617 		IXGBE_WRITE_REG(hw, IXGBE_RQSMR(regnum), reg);
    618 
    619 		/*
    620 		 * Set DROP_EN iff we have no flow control and >1 queue.
    621 		 * Note that srrctl was cleared shortly before during reset,
    622 		 * so we do not need to clear the bit, but do it just in case
    623 		 * this code is moved elsewhere.
    624 		 */
    625 		if (adapter->num_queues > 1 &&
    626 		    adapter->hw.fc.requested_mode == ixgbe_fc_none) {
    627 			srrctl |= IXGBE_SRRCTL_DROP_EN;
    628 		} else {
    629 			srrctl &= ~IXGBE_SRRCTL_DROP_EN;
    630 		}
    631 
    632 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(j), srrctl);
    633 
    634 		/* Setup the HW Rx Head and Tail Descriptor Pointers */
    635 		IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
    636 		IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
    637 
    638 		/* Set the driver rx tail address */
    639 		rxr->tail =  IXGBE_RDT(rxr->me);
    640 	}
    641 
    642 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
    643 		u32 psrtype = IXGBE_PSRTYPE_TCPHDR
    644 			    | IXGBE_PSRTYPE_UDPHDR
    645 			    | IXGBE_PSRTYPE_IPV4HDR
    646 			    | IXGBE_PSRTYPE_IPV6HDR;
    647 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
    648 	}
    649 
    650 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
    651 
    652 	ixgbe_initialize_rss_mapping(adapter);
    653 
    654 	if (adapter->num_queues > 1) {
    655 		/* RSS and RX IPP Checksum are mutually exclusive */
    656 		rxcsum |= IXGBE_RXCSUM_PCSD;
    657 	}
    658 
    659 	if (ifp->if_capenable & IFCAP_RXCSUM)
    660 		rxcsum |= IXGBE_RXCSUM_PCSD;
    661 
    662 	/* This is useful for calculating UDP/IP fragment checksums */
    663 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
    664 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
    665 
    666 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
    667 
    668 } /* ixgbe_initialize_receive_units */
    669 
    670 /************************************************************************
    671  * ixgbe_initialize_transmit_units - Enable transmit units.
    672  ************************************************************************/
    673 static void
    674 ixgbe_initialize_transmit_units(struct adapter *adapter)
    675 {
    676 	struct tx_ring	*txr = adapter->tx_rings;
    677 	struct ixgbe_hw	*hw = &adapter->hw;
    678 	int i;
    679 
    680 	INIT_DEBUGOUT("ixgbe_initialize_transmit_units");
    681 
    682 	/* Setup the Base and Length of the Tx Descriptor Ring */
    683 	for (i = 0; i < adapter->num_queues; i++, txr++) {
    684 		u64 tdba = txr->txdma.dma_paddr;
    685 		u32 txctrl = 0;
    686 		u32 tqsmreg, reg;
    687 		int regnum = i / 4;	/* 1 register per 4 queues */
    688 		int regshift = i % 4;	/* 4 bits per 1 queue */
    689 		int j = txr->me;
    690 
    691 		IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
    692 		    (tdba & 0x00000000ffffffffULL));
    693 		IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
    694 		IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j),
    695 		    adapter->num_tx_desc * sizeof(union ixgbe_adv_tx_desc));
    696 
    697 		/*
    698 		 * Set TQSMR (Transmit Queue Statistic Mapping) register.
    699 		 * Register location is different between 82598 and others.
    700 		 */
    701 		if (adapter->hw.mac.type == ixgbe_mac_82598EB)
    702 			tqsmreg = IXGBE_TQSMR(regnum);
    703 		else
    704 			tqsmreg = IXGBE_TQSM(regnum);
    705 		reg = IXGBE_READ_REG(hw, tqsmreg);
    706 		reg &= ~(0x000000ffUL << (regshift * 8));
    707 		reg |= i << (regshift * 8);
    708 		IXGBE_WRITE_REG(hw, tqsmreg, reg);
    709 
    710 		/* Setup the HW Tx Head and Tail descriptor pointers */
    711 		IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
    712 		IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
    713 
    714 		/* Cache the tail address */
    715 		txr->tail = IXGBE_TDT(j);
    716 
    717 		txr->txr_no_space = false;
    718 
    719 		/* Disable Head Writeback */
    720 		/*
    721 		 * Note: for X550 series devices, these registers are actually
    722 		 * prefixed with TPH_ isntead of DCA_, but the addresses and
    723 		 * fields remain the same.
    724 		 */
    725 		switch (hw->mac.type) {
    726 		case ixgbe_mac_82598EB:
    727 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
    728 			break;
    729 		default:
    730 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(j));
    731 			break;
    732 		}
    733 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
    734 		switch (hw->mac.type) {
    735 		case ixgbe_mac_82598EB:
    736 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
    737 			break;
    738 		default:
    739 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(j), txctrl);
    740 			break;
    741 		}
    742 
    743 	}
    744 
    745 	if (hw->mac.type != ixgbe_mac_82598EB) {
    746 		u32 dmatxctl, rttdcs;
    747 
    748 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
    749 		dmatxctl |= IXGBE_DMATXCTL_TE;
    750 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
    751 		/* Disable arbiter to set MTQC */
    752 		rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
    753 		rttdcs |= IXGBE_RTTDCS_ARBDIS;
    754 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
    755 		IXGBE_WRITE_REG(hw, IXGBE_MTQC,
    756 		    ixgbe_get_mtqc(adapter->iov_mode));
    757 		rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
    758 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
    759 	}
    760 
    761 	return;
    762 } /* ixgbe_initialize_transmit_units */
    763 
    764 /************************************************************************
    765  * ixgbe_attach - Device initialization routine
    766  *
    767  *   Called when the driver is being loaded.
    768  *   Identifies the type of hardware, allocates all resources
    769  *   and initializes the hardware.
    770  *
    771  *   return 0 on success, positive on failure
    772  ************************************************************************/
    773 static void
    774 ixgbe_attach(device_t parent, device_t dev, void *aux)
    775 {
    776 	struct adapter	*adapter;
    777 	struct ixgbe_hw *hw;
    778 	int		error = -1;
    779 	u32		ctrl_ext;
    780 	u16		high, low, nvmreg;
    781 	pcireg_t	id, subid;
    782 	const ixgbe_vendor_info_t *ent;
    783 	struct pci_attach_args *pa = aux;
    784 	bool unsupported_sfp = false;
    785 	const char *str;
    786 	char buf[256];
    787 
    788 	INIT_DEBUGOUT("ixgbe_attach: begin");
    789 
    790 	/* Allocate, clear, and link in our adapter structure */
    791 	adapter = device_private(dev);
    792 	adapter->hw.back = adapter;
    793 	adapter->dev = dev;
    794 	hw = &adapter->hw;
    795 	adapter->osdep.pc = pa->pa_pc;
    796 	adapter->osdep.tag = pa->pa_tag;
    797 	if (pci_dma64_available(pa))
    798 		adapter->osdep.dmat = pa->pa_dmat64;
    799 	else
    800 		adapter->osdep.dmat = pa->pa_dmat;
    801 	adapter->osdep.attached = false;
    802 
    803 	ent = ixgbe_lookup(pa);
    804 
    805 	KASSERT(ent != NULL);
    806 
    807 	aprint_normal(": %s, Version - %s\n",
    808 	    ixgbe_strings[ent->index], ixgbe_driver_version);
    809 
    810 	/* Core Lock Init*/
    811 	IXGBE_CORE_LOCK_INIT(adapter, device_xname(dev));
    812 
    813 	/* Set up the timer callout */
    814 	callout_init(&adapter->timer, IXGBE_CALLOUT_FLAGS);
    815 
    816 	/* Determine hardware revision */
    817 	id = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG);
    818 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    819 
    820 	hw->vendor_id = PCI_VENDOR(id);
    821 	hw->device_id = PCI_PRODUCT(id);
    822 	hw->revision_id =
    823 	    PCI_REVISION(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
    824 	hw->subsystem_vendor_id = PCI_SUBSYS_VENDOR(subid);
    825 	hw->subsystem_device_id = PCI_SUBSYS_ID(subid);
    826 
    827 	/*
    828 	 * Make sure BUSMASTER is set
    829 	 */
    830 	ixgbe_pci_enable_busmaster(pa->pa_pc, pa->pa_tag);
    831 
    832 	/* Do base PCI setup - map BAR0 */
    833 	if (ixgbe_allocate_pci_resources(adapter, pa)) {
    834 		aprint_error_dev(dev, "Allocation of PCI resources failed\n");
    835 		error = ENXIO;
    836 		goto err_out;
    837 	}
    838 
    839 	/* let hardware know driver is loaded */
    840 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
    841 	ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
    842 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
    843 
    844 	/*
    845 	 * Initialize the shared code
    846 	 */
    847 	if (ixgbe_init_shared_code(hw) != 0) {
    848 		aprint_error_dev(dev, "Unable to initialize the shared code\n");
    849 		error = ENXIO;
    850 		goto err_out;
    851 	}
    852 
    853 	switch (hw->mac.type) {
    854 	case ixgbe_mac_82598EB:
    855 		str = "82598EB";
    856 		break;
    857 	case ixgbe_mac_82599EB:
    858 		str = "82599EB";
    859 		break;
    860 	case ixgbe_mac_X540:
    861 		str = "X540";
    862 		break;
    863 	case ixgbe_mac_X550:
    864 		str = "X550";
    865 		break;
    866 	case ixgbe_mac_X550EM_x:
    867 		str = "X550EM X";
    868 		break;
    869 	case ixgbe_mac_X550EM_a:
    870 		str = "X550EM A";
    871 		break;
    872 	default:
    873 		str = "Unknown";
    874 		break;
    875 	}
    876 	aprint_normal_dev(dev, "device %s\n", str);
    877 
    878 	if (hw->mbx.ops.init_params)
    879 		hw->mbx.ops.init_params(hw);
    880 
    881 	hw->allow_unsupported_sfp = allow_unsupported_sfp;
    882 
    883 	/* Pick up the 82599 settings */
    884 	if (hw->mac.type != ixgbe_mac_82598EB) {
    885 		hw->phy.smart_speed = ixgbe_smart_speed;
    886 		adapter->num_segs = IXGBE_82599_SCATTER;
    887 	} else
    888 		adapter->num_segs = IXGBE_82598_SCATTER;
    889 
    890 	/* Ensure SW/FW semaphore is free */
    891 	ixgbe_init_swfw_semaphore(hw);
    892 
    893 	hw->mac.ops.set_lan_id(hw);
    894 	ixgbe_init_device_features(adapter);
    895 
    896 	if (ixgbe_configure_interrupts(adapter)) {
    897 		error = ENXIO;
    898 		goto err_out;
    899 	}
    900 
    901 	/* Allocate multicast array memory. */
    902 	adapter->mta = malloc(sizeof(*adapter->mta) *
    903 	    MAX_NUM_MULTICAST_ADDRESSES, M_DEVBUF, M_NOWAIT);
    904 	if (adapter->mta == NULL) {
    905 		aprint_error_dev(dev, "Cannot allocate multicast setup array\n");
    906 		error = ENOMEM;
    907 		goto err_out;
    908 	}
    909 
    910 	/* Enable WoL (if supported) */
    911 	ixgbe_check_wol_support(adapter);
    912 
    913 	/* Verify adapter fan is still functional (if applicable) */
    914 	if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL) {
    915 		u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
    916 		ixgbe_check_fan_failure(adapter, esdp, FALSE);
    917 	}
    918 
    919 	/* Set an initial default flow control value */
    920 	hw->fc.requested_mode = ixgbe_flow_control;
    921 
    922 	/* Sysctls for limiting the amount of work done in the taskqueues */
    923 	ixgbe_set_sysctl_value(adapter, "rx_processing_limit",
    924 	    "max number of rx packets to process",
    925 	    &adapter->rx_process_limit, ixgbe_rx_process_limit);
    926 
    927 	ixgbe_set_sysctl_value(adapter, "tx_processing_limit",
    928 	    "max number of tx packets to process",
    929 	    &adapter->tx_process_limit, ixgbe_tx_process_limit);
    930 
    931 	/* Do descriptor calc and sanity checks */
    932 	if (((ixgbe_txd * sizeof(union ixgbe_adv_tx_desc)) % DBA_ALIGN) != 0 ||
    933 	    ixgbe_txd < MIN_TXD || ixgbe_txd > MAX_TXD) {
    934 		aprint_error_dev(dev, "TXD config issue, using default!\n");
    935 		adapter->num_tx_desc = DEFAULT_TXD;
    936 	} else
    937 		adapter->num_tx_desc = ixgbe_txd;
    938 
    939 	if (((ixgbe_rxd * sizeof(union ixgbe_adv_rx_desc)) % DBA_ALIGN) != 0 ||
    940 	    ixgbe_rxd < MIN_RXD || ixgbe_rxd > MAX_RXD) {
    941 		aprint_error_dev(dev, "RXD config issue, using default!\n");
    942 		adapter->num_rx_desc = DEFAULT_RXD;
    943 	} else
    944 		adapter->num_rx_desc = ixgbe_rxd;
    945 
    946 	/* Set default high limit of copying mbuf in rxeof */
    947 	adapter->rx_copy_len = IXGBE_RX_COPY_LEN_MAX;
    948 
    949 	/* Allocate our TX/RX Queues */
    950 	if (ixgbe_allocate_queues(adapter)) {
    951 		error = ENOMEM;
    952 		goto err_out;
    953 	}
    954 
    955 	hw->phy.reset_if_overtemp = TRUE;
    956 	error = ixgbe_reset_hw(hw);
    957 	hw->phy.reset_if_overtemp = FALSE;
    958 	if (error == IXGBE_ERR_SFP_NOT_PRESENT) {
    959 		/*
    960 		 * No optics in this port, set up
    961 		 * so the timer routine will probe
    962 		 * for later insertion.
    963 		 */
    964 		adapter->sfp_probe = TRUE;
    965 		error = IXGBE_SUCCESS;
    966 	} else if (error == IXGBE_ERR_SFP_NOT_SUPPORTED) {
    967 		aprint_error_dev(dev, "Unsupported SFP+ module detected!\n");
    968 		unsupported_sfp = true;
    969 		error = IXGBE_SUCCESS;
    970 	} else if (error) {
    971 		aprint_error_dev(dev,
    972 		    "Hardware initialization failed(error = %d)\n", error);
    973 		error = EIO;
    974 		goto err_late;
    975 	}
    976 
    977 	/* Make sure we have a good EEPROM before we read from it */
    978 	if (ixgbe_validate_eeprom_checksum(&adapter->hw, NULL) < 0) {
    979 		aprint_error_dev(dev, "The EEPROM Checksum Is Not Valid\n");
    980 		error = EIO;
    981 		goto err_late;
    982 	}
    983 
    984 	aprint_normal("%s:", device_xname(dev));
    985 	/* NVM Image Version */
    986 	high = low = 0;
    987 	switch (hw->mac.type) {
    988 	case ixgbe_mac_X540:
    989 	case ixgbe_mac_X550EM_a:
    990 		hw->eeprom.ops.read(hw, IXGBE_NVM_IMAGE_VER, &nvmreg);
    991 		if (nvmreg == 0xffff)
    992 			break;
    993 		high = (nvmreg >> 12) & 0x0f;
    994 		low = (nvmreg >> 4) & 0xff;
    995 		id = nvmreg & 0x0f;
    996 		aprint_normal(" NVM Image Version %u.", high);
    997 		if (hw->mac.type == ixgbe_mac_X540)
    998 			str = "%x";
    999 		else
   1000 			str = "%02x";
   1001 		aprint_normal(str, low);
   1002 		aprint_normal(" ID 0x%x,", id);
   1003 		break;
   1004 	case ixgbe_mac_X550EM_x:
   1005 	case ixgbe_mac_X550:
   1006 		hw->eeprom.ops.read(hw, IXGBE_NVM_IMAGE_VER, &nvmreg);
   1007 		if (nvmreg == 0xffff)
   1008 			break;
   1009 		high = (nvmreg >> 12) & 0x0f;
   1010 		low = nvmreg & 0xff;
   1011 		aprint_normal(" NVM Image Version %u.%02x,", high, low);
   1012 		break;
   1013 	default:
   1014 		break;
   1015 	}
   1016 	hw->eeprom.nvm_image_ver_high = high;
   1017 	hw->eeprom.nvm_image_ver_low = low;
   1018 
   1019 	/* PHY firmware revision */
   1020 	switch (hw->mac.type) {
   1021 	case ixgbe_mac_X540:
   1022 	case ixgbe_mac_X550:
   1023 		hw->eeprom.ops.read(hw, IXGBE_PHYFW_REV, &nvmreg);
   1024 		if (nvmreg == 0xffff)
   1025 			break;
   1026 		high = (nvmreg >> 12) & 0x0f;
   1027 		low = (nvmreg >> 4) & 0xff;
   1028 		id = nvmreg & 0x000f;
   1029 		aprint_normal(" PHY FW Revision %u.", high);
   1030 		if (hw->mac.type == ixgbe_mac_X540)
   1031 			str = "%x";
   1032 		else
   1033 			str = "%02x";
   1034 		aprint_normal(str, low);
   1035 		aprint_normal(" ID 0x%x,", id);
   1036 		break;
   1037 	default:
   1038 		break;
   1039 	}
   1040 
   1041 	/* NVM Map version & OEM NVM Image version */
   1042 	switch (hw->mac.type) {
   1043 	case ixgbe_mac_X550:
   1044 	case ixgbe_mac_X550EM_x:
   1045 	case ixgbe_mac_X550EM_a:
   1046 		hw->eeprom.ops.read(hw, IXGBE_NVM_MAP_VER, &nvmreg);
   1047 		if (nvmreg != 0xffff) {
   1048 			high = (nvmreg >> 12) & 0x0f;
   1049 			low = nvmreg & 0x00ff;
   1050 			aprint_normal(" NVM Map version %u.%02x,", high, low);
   1051 		}
   1052 		hw->eeprom.ops.read(hw, IXGBE_OEM_NVM_IMAGE_VER, &nvmreg);
   1053 		if (nvmreg != 0xffff) {
   1054 			high = (nvmreg >> 12) & 0x0f;
   1055 			low = nvmreg & 0x00ff;
   1056 			aprint_verbose(" OEM NVM Image version %u.%02x,", high,
   1057 			    low);
   1058 		}
   1059 		break;
   1060 	default:
   1061 		break;
   1062 	}
   1063 
   1064 	/* Print the ETrackID */
   1065 	hw->eeprom.ops.read(hw, IXGBE_ETRACKID_H, &high);
   1066 	hw->eeprom.ops.read(hw, IXGBE_ETRACKID_L, &low);
   1067 	aprint_normal(" ETrackID %08x\n", ((uint32_t)high << 16) | low);
   1068 
   1069 	if (adapter->feat_en & IXGBE_FEATURE_MSIX) {
   1070 		error = ixgbe_allocate_msix(adapter, pa);
   1071 		if (error) {
   1072 			/* Free allocated queue structures first */
   1073 			ixgbe_free_queues(adapter);
   1074 
   1075 			/* Fallback to legacy interrupt */
   1076 			adapter->feat_en &= ~IXGBE_FEATURE_MSIX;
   1077 			if (adapter->feat_cap & IXGBE_FEATURE_MSI)
   1078 				adapter->feat_en |= IXGBE_FEATURE_MSI;
   1079 			adapter->num_queues = 1;
   1080 
   1081 			/* Allocate our TX/RX Queues again */
   1082 			if (ixgbe_allocate_queues(adapter)) {
   1083 				error = ENOMEM;
   1084 				goto err_out;
   1085 			}
   1086 		}
   1087 	}
   1088 	/* Recovery mode */
   1089 	switch (adapter->hw.mac.type) {
   1090 	case ixgbe_mac_X550:
   1091 	case ixgbe_mac_X550EM_x:
   1092 	case ixgbe_mac_X550EM_a:
   1093 		/* >= 2.00 */
   1094 		if (hw->eeprom.nvm_image_ver_high >= 2) {
   1095 			adapter->feat_cap |= IXGBE_FEATURE_RECOVERY_MODE;
   1096 			adapter->feat_en |= IXGBE_FEATURE_RECOVERY_MODE;
   1097 		}
   1098 		break;
   1099 	default:
   1100 		break;
   1101 	}
   1102 
   1103 	if ((adapter->feat_en & IXGBE_FEATURE_MSIX) == 0)
   1104 		error = ixgbe_allocate_legacy(adapter, pa);
   1105 	if (error)
   1106 		goto err_late;
   1107 
   1108 	/* Tasklets for Link, SFP, Multispeed Fiber and Flow Director */
   1109 	adapter->link_si = softint_establish(SOFTINT_NET |IXGBE_SOFTINT_FLAGS,
   1110 	    ixgbe_handle_link, adapter);
   1111 	adapter->mod_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINT_FLAGS,
   1112 	    ixgbe_handle_mod, adapter);
   1113 	adapter->msf_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINT_FLAGS,
   1114 	    ixgbe_handle_msf, adapter);
   1115 	adapter->phy_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINT_FLAGS,
   1116 	    ixgbe_handle_phy, adapter);
   1117 	if (adapter->feat_en & IXGBE_FEATURE_FDIR)
   1118 		adapter->fdir_si =
   1119 		    softint_establish(SOFTINT_NET | IXGBE_SOFTINT_FLAGS,
   1120 			ixgbe_reinit_fdir, adapter);
   1121 	if ((adapter->link_si == NULL) || (adapter->mod_si == NULL)
   1122 	    || (adapter->msf_si == NULL) || (adapter->phy_si == NULL)
   1123 	    || ((adapter->feat_en & IXGBE_FEATURE_FDIR)
   1124 		&& (adapter->fdir_si == NULL))) {
   1125 		aprint_error_dev(dev,
   1126 		    "could not establish software interrupts ()\n");
   1127 		goto err_out;
   1128 	}
   1129 
   1130 	error = ixgbe_start_hw(hw);
   1131 	switch (error) {
   1132 	case IXGBE_ERR_EEPROM_VERSION:
   1133 		aprint_error_dev(dev, "This device is a pre-production adapter/"
   1134 		    "LOM.  Please be aware there may be issues associated "
   1135 		    "with your hardware.\nIf you are experiencing problems "
   1136 		    "please contact your Intel or hardware representative "
   1137 		    "who provided you with this hardware.\n");
   1138 		break;
   1139 	default:
   1140 		break;
   1141 	}
   1142 
   1143 	/* Setup OS specific network interface */
   1144 	if (ixgbe_setup_interface(dev, adapter) != 0)
   1145 		goto err_late;
   1146 
   1147 	/*
   1148 	 *  Print PHY ID only for copper PHY. On device which has SFP(+) cage
   1149 	 * and a module is inserted, phy.id is not MII PHY id but SFF 8024 ID.
   1150 	 */
   1151 	if (hw->phy.media_type == ixgbe_media_type_copper) {
   1152 		uint16_t id1, id2;
   1153 		int oui, model, rev;
   1154 		const char *descr;
   1155 
   1156 		id1 = hw->phy.id >> 16;
   1157 		id2 = hw->phy.id & 0xffff;
   1158 		oui = MII_OUI(id1, id2);
   1159 		model = MII_MODEL(id2);
   1160 		rev = MII_REV(id2);
   1161 		if ((descr = mii_get_descr(oui, model)) != NULL)
   1162 			aprint_normal_dev(dev,
   1163 			    "PHY: %s (OUI 0x%06x, model 0x%04x), rev. %d\n",
   1164 			    descr, oui, model, rev);
   1165 		else
   1166 			aprint_normal_dev(dev,
   1167 			    "PHY OUI 0x%06x, model 0x%04x, rev. %d\n",
   1168 			    oui, model, rev);
   1169 	}
   1170 
   1171 	/* Enable EEE power saving */
   1172 	if (adapter->feat_cap & IXGBE_FEATURE_EEE)
   1173 		hw->mac.ops.setup_eee(hw,
   1174 		    adapter->feat_en & IXGBE_FEATURE_EEE);
   1175 
   1176 	/* Enable power to the phy. */
   1177 	if (!unsupported_sfp) {
   1178 		/* Enable the optics for 82599 SFP+ fiber */
   1179 		ixgbe_enable_tx_laser(hw);
   1180 
   1181 		/*
   1182 		 * XXX Currently, ixgbe_set_phy_power() supports only copper
   1183 		 * PHY, so it's not required to test with !unsupported_sfp.
   1184 		 */
   1185 		ixgbe_set_phy_power(hw, TRUE);
   1186 	}
   1187 
   1188 	/* Initialize statistics */
   1189 	ixgbe_update_stats_counters(adapter);
   1190 
   1191 	/* Check PCIE slot type/speed/width */
   1192 	ixgbe_get_slot_info(adapter);
   1193 
   1194 	/*
   1195 	 * Do time init and sysctl init here, but
   1196 	 * only on the first port of a bypass adapter.
   1197 	 */
   1198 	ixgbe_bypass_init(adapter);
   1199 
   1200 	/* Set an initial dmac value */
   1201 	adapter->dmac = 0;
   1202 	/* Set initial advertised speeds (if applicable) */
   1203 	adapter->advertise = ixgbe_get_advertise(adapter);
   1204 
   1205 	if (adapter->feat_cap & IXGBE_FEATURE_SRIOV)
   1206 		ixgbe_define_iov_schemas(dev, &error);
   1207 
   1208 	/* Add sysctls */
   1209 	ixgbe_add_device_sysctls(adapter);
   1210 	ixgbe_add_hw_stats(adapter);
   1211 
   1212 	/* For Netmap */
   1213 	adapter->init_locked = ixgbe_init_locked;
   1214 	adapter->stop_locked = ixgbe_stop_locked;
   1215 
   1216 	if (adapter->feat_en & IXGBE_FEATURE_NETMAP)
   1217 		ixgbe_netmap_attach(adapter);
   1218 
   1219 	snprintb(buf, sizeof(buf), IXGBE_FEATURE_FLAGS, adapter->feat_cap);
   1220 	aprint_verbose_dev(dev, "feature cap %s\n", buf);
   1221 	snprintb(buf, sizeof(buf), IXGBE_FEATURE_FLAGS, adapter->feat_en);
   1222 	aprint_verbose_dev(dev, "feature ena %s\n", buf);
   1223 
   1224 	if (pmf_device_register(dev, ixgbe_suspend, ixgbe_resume))
   1225 		pmf_class_network_register(dev, adapter->ifp);
   1226 	else
   1227 		aprint_error_dev(dev, "couldn't establish power handler\n");
   1228 
   1229 	/* Init recovery mode timer and state variable */
   1230 	if (adapter->feat_en & IXGBE_FEATURE_RECOVERY_MODE) {
   1231 		adapter->recovery_mode = 0;
   1232 
   1233 		/* Set up the timer callout */
   1234 		callout_init(&adapter->recovery_mode_timer,
   1235 		    IXGBE_CALLOUT_FLAGS);
   1236 
   1237 		/* Start the task */
   1238 		callout_reset(&adapter->recovery_mode_timer, hz,
   1239 		    ixgbe_recovery_mode_timer, adapter);
   1240 	}
   1241 
   1242 	INIT_DEBUGOUT("ixgbe_attach: end");
   1243 	adapter->osdep.attached = true;
   1244 
   1245 	return;
   1246 
   1247 err_late:
   1248 	ixgbe_free_queues(adapter);
   1249 err_out:
   1250 	ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
   1251 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
   1252 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT, ctrl_ext);
   1253 	ixgbe_free_softint(adapter);
   1254 	ixgbe_free_pci_resources(adapter);
   1255 	if (adapter->mta != NULL)
   1256 		free(adapter->mta, M_DEVBUF);
   1257 	IXGBE_CORE_LOCK_DESTROY(adapter);
   1258 
   1259 	return;
   1260 } /* ixgbe_attach */
   1261 
   1262 /************************************************************************
   1263  * ixgbe_check_wol_support
   1264  *
   1265  *   Checks whether the adapter's ports are capable of
   1266  *   Wake On LAN by reading the adapter's NVM.
   1267  *
   1268  *   Sets each port's hw->wol_enabled value depending
   1269  *   on the value read here.
   1270  ************************************************************************/
   1271 static void
   1272 ixgbe_check_wol_support(struct adapter *adapter)
   1273 {
   1274 	struct ixgbe_hw *hw = &adapter->hw;
   1275 	u16		dev_caps = 0;
   1276 
   1277 	/* Find out WoL support for port */
   1278 	adapter->wol_support = hw->wol_enabled = 0;
   1279 	ixgbe_get_device_caps(hw, &dev_caps);
   1280 	if ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
   1281 	    ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0) &&
   1282 	     hw->bus.func == 0))
   1283 		adapter->wol_support = hw->wol_enabled = 1;
   1284 
   1285 	/* Save initial wake up filter configuration */
   1286 	adapter->wufc = IXGBE_READ_REG(hw, IXGBE_WUFC);
   1287 
   1288 	return;
   1289 } /* ixgbe_check_wol_support */
   1290 
   1291 /************************************************************************
   1292  * ixgbe_setup_interface
   1293  *
   1294  *   Setup networking device structure and register an interface.
   1295  ************************************************************************/
   1296 static int
   1297 ixgbe_setup_interface(device_t dev, struct adapter *adapter)
   1298 {
   1299 	struct ethercom *ec = &adapter->osdep.ec;
   1300 	struct ifnet   *ifp;
   1301 	int rv;
   1302 
   1303 	INIT_DEBUGOUT("ixgbe_setup_interface: begin");
   1304 
   1305 	ifp = adapter->ifp = &ec->ec_if;
   1306 	strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
   1307 	ifp->if_baudrate = IF_Gbps(10);
   1308 	ifp->if_init = ixgbe_init;
   1309 	ifp->if_stop = ixgbe_ifstop;
   1310 	ifp->if_softc = adapter;
   1311 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1312 #ifdef IXGBE_MPSAFE
   1313 	ifp->if_extflags = IFEF_MPSAFE;
   1314 #endif
   1315 	ifp->if_ioctl = ixgbe_ioctl;
   1316 #if __FreeBSD_version >= 1100045
   1317 	/* TSO parameters */
   1318 	ifp->if_hw_tsomax = 65518;
   1319 	ifp->if_hw_tsomaxsegcount = IXGBE_82599_SCATTER;
   1320 	ifp->if_hw_tsomaxsegsize = 2048;
   1321 #endif
   1322 	if (adapter->feat_en & IXGBE_FEATURE_LEGACY_TX) {
   1323 #if 0
   1324 		ixgbe_start_locked = ixgbe_legacy_start_locked;
   1325 #endif
   1326 	} else {
   1327 		ifp->if_transmit = ixgbe_mq_start;
   1328 #if 0
   1329 		ixgbe_start_locked = ixgbe_mq_start_locked;
   1330 #endif
   1331 	}
   1332 	ifp->if_start = ixgbe_legacy_start;
   1333 	IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
   1334 	IFQ_SET_READY(&ifp->if_snd);
   1335 
   1336 	rv = if_initialize(ifp);
   1337 	if (rv != 0) {
   1338 		aprint_error_dev(dev, "if_initialize failed(%d)\n", rv);
   1339 		return rv;
   1340 	}
   1341 	adapter->ipq = if_percpuq_create(&adapter->osdep.ec.ec_if);
   1342 	ether_ifattach(ifp, adapter->hw.mac.addr);
   1343 	aprint_normal_dev(dev, "Ethernet address %s\n",
   1344 	    ether_sprintf(adapter->hw.mac.addr));
   1345 	/*
   1346 	 * We use per TX queue softint, so if_deferred_start_init() isn't
   1347 	 * used.
   1348 	 */
   1349 	ether_set_ifflags_cb(ec, ixgbe_ifflags_cb);
   1350 
   1351 	adapter->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   1352 
   1353 	/*
   1354 	 * Tell the upper layer(s) we support long frames.
   1355 	 */
   1356 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
   1357 
   1358 	/* Set capability flags */
   1359 	ifp->if_capabilities |= IFCAP_RXCSUM
   1360 			     |	IFCAP_TXCSUM
   1361 			     |	IFCAP_TSOv4
   1362 			     |	IFCAP_TSOv6;
   1363 	ifp->if_capenable = 0;
   1364 
   1365 	ec->ec_capabilities |= ETHERCAP_VLAN_HWTAGGING
   1366 			    |  ETHERCAP_VLAN_HWCSUM
   1367 			    |  ETHERCAP_JUMBO_MTU
   1368 			    |  ETHERCAP_VLAN_MTU;
   1369 
   1370 	/* Enable the above capabilities by default */
   1371 	ec->ec_capenable = ec->ec_capabilities;
   1372 
   1373 	/*
   1374 	 * Don't turn this on by default, if vlans are
   1375 	 * created on another pseudo device (eg. lagg)
   1376 	 * then vlan events are not passed thru, breaking
   1377 	 * operation, but with HW FILTER off it works. If
   1378 	 * using vlans directly on the ixgbe driver you can
   1379 	 * enable this and get full hardware tag filtering.
   1380 	 */
   1381 	ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER;
   1382 
   1383 	/*
   1384 	 * Specify the media types supported by this adapter and register
   1385 	 * callbacks to update media and link information
   1386 	 */
   1387 	ifmedia_init(&adapter->media, IFM_IMASK, ixgbe_media_change,
   1388 	    ixgbe_media_status);
   1389 
   1390 	adapter->phy_layer = ixgbe_get_supported_physical_layer(&adapter->hw);
   1391 	ixgbe_add_media_types(adapter);
   1392 
   1393 	/* Set autoselect media by default */
   1394 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   1395 
   1396 	if_register(ifp);
   1397 
   1398 	return (0);
   1399 } /* ixgbe_setup_interface */
   1400 
   1401 /************************************************************************
   1402  * ixgbe_add_media_types
   1403  ************************************************************************/
   1404 static void
   1405 ixgbe_add_media_types(struct adapter *adapter)
   1406 {
   1407 	struct ixgbe_hw *hw = &adapter->hw;
   1408 	device_t	dev = adapter->dev;
   1409 	u64		layer;
   1410 
   1411 	layer = adapter->phy_layer;
   1412 
   1413 #define	ADD(mm, dd)							\
   1414 	ifmedia_add(&adapter->media, IFM_ETHER | (mm), (dd), NULL);
   1415 
   1416 	ADD(IFM_NONE, 0);
   1417 
   1418 	/* Media types with matching NetBSD media defines */
   1419 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T) {
   1420 		ADD(IFM_10G_T | IFM_FDX, 0);
   1421 	}
   1422 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T) {
   1423 		ADD(IFM_1000_T | IFM_FDX, 0);
   1424 	}
   1425 	if (layer & IXGBE_PHYSICAL_LAYER_100BASE_TX) {
   1426 		ADD(IFM_100_TX | IFM_FDX, 0);
   1427 	}
   1428 	if (layer & IXGBE_PHYSICAL_LAYER_10BASE_T) {
   1429 		ADD(IFM_10_T | IFM_FDX, 0);
   1430 	}
   1431 
   1432 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
   1433 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA) {
   1434 		ADD(IFM_10G_TWINAX | IFM_FDX, 0);
   1435 	}
   1436 
   1437 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR) {
   1438 		ADD(IFM_10G_LR | IFM_FDX, 0);
   1439 		if (hw->phy.multispeed_fiber) {
   1440 			ADD(IFM_1000_LX | IFM_FDX, 0);
   1441 		}
   1442 	}
   1443 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
   1444 		ADD(IFM_10G_SR | IFM_FDX, 0);
   1445 		if (hw->phy.multispeed_fiber) {
   1446 			ADD(IFM_1000_SX | IFM_FDX, 0);
   1447 		}
   1448 	} else if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX) {
   1449 		ADD(IFM_1000_SX | IFM_FDX, 0);
   1450 	}
   1451 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4) {
   1452 		ADD(IFM_10G_CX4 | IFM_FDX, 0);
   1453 	}
   1454 
   1455 #ifdef IFM_ETH_XTYPE
   1456 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
   1457 		ADD(IFM_10G_KR | IFM_FDX, 0);
   1458 	}
   1459 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
   1460 		ADD(IFM_10G_KX4 | IFM_FDX, 0);
   1461 	}
   1462 #else
   1463 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
   1464 		device_printf(dev, "Media supported: 10GbaseKR\n");
   1465 		device_printf(dev, "10GbaseKR mapped to 10GbaseSR\n");
   1466 		ADD(IFM_10G_SR | IFM_FDX, 0);
   1467 	}
   1468 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
   1469 		device_printf(dev, "Media supported: 10GbaseKX4\n");
   1470 		device_printf(dev, "10GbaseKX4 mapped to 10GbaseCX4\n");
   1471 		ADD(IFM_10G_CX4 | IFM_FDX, 0);
   1472 	}
   1473 #endif
   1474 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX) {
   1475 		ADD(IFM_1000_KX | IFM_FDX, 0);
   1476 	}
   1477 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX) {
   1478 		ADD(IFM_2500_KX | IFM_FDX, 0);
   1479 	}
   1480 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_T) {
   1481 		ADD(IFM_2500_T | IFM_FDX, 0);
   1482 	}
   1483 	if (layer & IXGBE_PHYSICAL_LAYER_5GBASE_T) {
   1484 		ADD(IFM_5000_T | IFM_FDX, 0);
   1485 	}
   1486 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_BX)
   1487 		ADD(IFM_1000_LX | IFM_FDX, 0); /* IFM_1000_BX */
   1488 	/* XXX no ifmedia_set? */
   1489 
   1490 	ADD(IFM_AUTO, 0);
   1491 
   1492 #undef ADD
   1493 } /* ixgbe_add_media_types */
   1494 
   1495 /************************************************************************
   1496  * ixgbe_is_sfp
   1497  ************************************************************************/
   1498 static inline bool
   1499 ixgbe_is_sfp(struct ixgbe_hw *hw)
   1500 {
   1501 	switch (hw->mac.type) {
   1502 	case ixgbe_mac_82598EB:
   1503 		if (hw->phy.type == ixgbe_phy_nl)
   1504 			return (TRUE);
   1505 		return (FALSE);
   1506 	case ixgbe_mac_82599EB:
   1507 	case ixgbe_mac_X550EM_x:
   1508 	case ixgbe_mac_X550EM_a:
   1509 		switch (hw->mac.ops.get_media_type(hw)) {
   1510 		case ixgbe_media_type_fiber:
   1511 		case ixgbe_media_type_fiber_qsfp:
   1512 			return (TRUE);
   1513 		default:
   1514 			return (FALSE);
   1515 		}
   1516 	default:
   1517 		return (FALSE);
   1518 	}
   1519 } /* ixgbe_is_sfp */
   1520 
   1521 /************************************************************************
   1522  * ixgbe_config_link
   1523  ************************************************************************/
   1524 static void
   1525 ixgbe_config_link(struct adapter *adapter)
   1526 {
   1527 	struct ixgbe_hw *hw = &adapter->hw;
   1528 	u32		autoneg, err = 0;
   1529 	bool		sfp, negotiate = false;
   1530 
   1531 	sfp = ixgbe_is_sfp(hw);
   1532 
   1533 	if (sfp) {
   1534 		if (hw->phy.multispeed_fiber) {
   1535 			ixgbe_enable_tx_laser(hw);
   1536 			kpreempt_disable();
   1537 			softint_schedule(adapter->msf_si);
   1538 			kpreempt_enable();
   1539 		}
   1540 		kpreempt_disable();
   1541 		softint_schedule(adapter->mod_si);
   1542 		kpreempt_enable();
   1543 	} else {
   1544 		struct ifmedia	*ifm = &adapter->media;
   1545 
   1546 		if (hw->mac.ops.check_link)
   1547 			err = ixgbe_check_link(hw, &adapter->link_speed,
   1548 			    &adapter->link_up, FALSE);
   1549 		if (err)
   1550 			return;
   1551 
   1552 		/*
   1553 		 * Check if it's the first call. If it's the first call,
   1554 		 * get value for auto negotiation.
   1555 		 */
   1556 		autoneg = hw->phy.autoneg_advertised;
   1557 		if ((IFM_SUBTYPE(ifm->ifm_cur->ifm_media) != IFM_NONE)
   1558 		    && ((!autoneg) && (hw->mac.ops.get_link_capabilities)))
   1559 			err = hw->mac.ops.get_link_capabilities(hw, &autoneg,
   1560 			    &negotiate);
   1561 		if (err)
   1562 			return;
   1563 		if (hw->mac.ops.setup_link)
   1564 			err = hw->mac.ops.setup_link(hw, autoneg,
   1565 			    adapter->link_up);
   1566 	}
   1567 
   1568 } /* ixgbe_config_link */
   1569 
   1570 /************************************************************************
   1571  * ixgbe_update_stats_counters - Update board statistics counters.
   1572  ************************************************************************/
   1573 static void
   1574 ixgbe_update_stats_counters(struct adapter *adapter)
   1575 {
   1576 	struct ifnet	      *ifp = adapter->ifp;
   1577 	struct ixgbe_hw	      *hw = &adapter->hw;
   1578 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
   1579 	u32		      missed_rx = 0, bprc, lxon, lxoff, total;
   1580 	u64		      total_missed_rx = 0;
   1581 	uint64_t	      crcerrs, rlec;
   1582 	unsigned int	      queue_counters;
   1583 	int		      i;
   1584 
   1585 	crcerrs = IXGBE_READ_REG(hw, IXGBE_CRCERRS);
   1586 	stats->crcerrs.ev_count += crcerrs;
   1587 	stats->illerrc.ev_count += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
   1588 	stats->errbc.ev_count += IXGBE_READ_REG(hw, IXGBE_ERRBC);
   1589 	stats->mspdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MSPDC);
   1590 	if (hw->mac.type >= ixgbe_mac_X550)
   1591 		stats->mbsdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MBSDC);
   1592 
   1593 	/* 16 registers exist */
   1594 	queue_counters = min(__arraycount(stats->qprc), adapter->num_queues);
   1595 	for (i = 0; i < queue_counters; i++) {
   1596 		stats->qprc[i].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
   1597 		stats->qptc[i].ev_count += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
   1598 		if (hw->mac.type >= ixgbe_mac_82599EB) {
   1599 			stats->qprdc[i].ev_count
   1600 			    += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
   1601 		}
   1602 	}
   1603 
   1604 	/* 8 registers exist */
   1605 	for (i = 0; i < IXGBE_TC_COUNTER_NUM; i++) {
   1606 		uint32_t mp;
   1607 
   1608 		/* MPC */
   1609 		mp = IXGBE_READ_REG(hw, IXGBE_MPC(i));
   1610 		/* global total per queue */
   1611 		stats->mpc[i].ev_count += mp;
   1612 		/* running comprehensive total for stats display */
   1613 		total_missed_rx += mp;
   1614 
   1615 		if (hw->mac.type == ixgbe_mac_82598EB)
   1616 			stats->rnbc[i].ev_count
   1617 			    += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
   1618 
   1619 		stats->pxontxc[i].ev_count
   1620 		    += IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
   1621 		stats->pxofftxc[i].ev_count
   1622 		    += IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
   1623 		if (hw->mac.type >= ixgbe_mac_82599EB) {
   1624 			stats->pxonrxc[i].ev_count
   1625 			    += IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
   1626 			stats->pxoffrxc[i].ev_count
   1627 			    += IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
   1628 			stats->pxon2offc[i].ev_count
   1629 			    += IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
   1630 		} else {
   1631 			stats->pxonrxc[i].ev_count
   1632 			    += IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
   1633 			stats->pxoffrxc[i].ev_count
   1634 			    += IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
   1635 		}
   1636 	}
   1637 	stats->mpctotal.ev_count += total_missed_rx;
   1638 
   1639 	/* Document says M[LR]FC are valid when link is up and 10Gbps */
   1640 	if ((adapter->link_active == LINK_STATE_UP)
   1641 	    && (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL)) {
   1642 		stats->mlfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MLFC);
   1643 		stats->mrfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MRFC);
   1644 	}
   1645 	rlec = IXGBE_READ_REG(hw, IXGBE_RLEC);
   1646 	stats->rlec.ev_count += rlec;
   1647 
   1648 	/* Hardware workaround, gprc counts missed packets */
   1649 	stats->gprc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPRC) - missed_rx;
   1650 
   1651 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
   1652 	stats->lxontxc.ev_count += lxon;
   1653 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
   1654 	stats->lxofftxc.ev_count += lxoff;
   1655 	total = lxon + lxoff;
   1656 
   1657 	if (hw->mac.type != ixgbe_mac_82598EB) {
   1658 		stats->gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCL) +
   1659 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
   1660 		stats->gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
   1661 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32)
   1662 		    - total * ETHER_MIN_LEN;
   1663 		stats->tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORL) +
   1664 		    ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
   1665 		stats->lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
   1666 		stats->lxoffrxc.ev_count
   1667 		    += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
   1668 	} else {
   1669 		stats->lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
   1670 		stats->lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
   1671 		/* 82598 only has a counter in the high register */
   1672 		stats->gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCH);
   1673 		stats->gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCH)
   1674 		    - total * ETHER_MIN_LEN;
   1675 		stats->tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORH);
   1676 	}
   1677 
   1678 	/*
   1679 	 * Workaround: mprc hardware is incorrectly counting
   1680 	 * broadcasts, so for now we subtract those.
   1681 	 */
   1682 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
   1683 	stats->bprc.ev_count += bprc;
   1684 	stats->mprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPRC)
   1685 	    - ((hw->mac.type == ixgbe_mac_82598EB) ? bprc : 0);
   1686 
   1687 	stats->prc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC64);
   1688 	stats->prc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC127);
   1689 	stats->prc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC255);
   1690 	stats->prc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC511);
   1691 	stats->prc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1023);
   1692 	stats->prc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1522);
   1693 
   1694 	stats->gptc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPTC) - total;
   1695 	stats->mptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPTC) - total;
   1696 	stats->ptc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC64) - total;
   1697 
   1698 	stats->ruc.ev_count += IXGBE_READ_REG(hw, IXGBE_RUC);
   1699 	stats->rfc.ev_count += IXGBE_READ_REG(hw, IXGBE_RFC);
   1700 	stats->roc.ev_count += IXGBE_READ_REG(hw, IXGBE_ROC);
   1701 	stats->rjc.ev_count += IXGBE_READ_REG(hw, IXGBE_RJC);
   1702 	stats->mngprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
   1703 	stats->mngpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
   1704 	stats->mngptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
   1705 	stats->tpr.ev_count += IXGBE_READ_REG(hw, IXGBE_TPR);
   1706 	stats->tpt.ev_count += IXGBE_READ_REG(hw, IXGBE_TPT);
   1707 	stats->ptc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC127);
   1708 	stats->ptc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC255);
   1709 	stats->ptc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC511);
   1710 	stats->ptc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1023);
   1711 	stats->ptc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1522);
   1712 	stats->bptc.ev_count += IXGBE_READ_REG(hw, IXGBE_BPTC);
   1713 	stats->xec.ev_count += IXGBE_READ_REG(hw, IXGBE_XEC);
   1714 	stats->fccrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCCRC);
   1715 	stats->fclast.ev_count += IXGBE_READ_REG(hw, IXGBE_FCLAST);
   1716 	/* Only read FCOE on 82599 */
   1717 	if (hw->mac.type != ixgbe_mac_82598EB) {
   1718 		stats->fcoerpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
   1719 		stats->fcoeprc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
   1720 		stats->fcoeptc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
   1721 		stats->fcoedwrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
   1722 		stats->fcoedwtc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
   1723 	}
   1724 
   1725 	/* Fill out the OS statistics structure */
   1726 	/*
   1727 	 * NetBSD: Don't override if_{i|o}{packets|bytes|mcasts} with
   1728 	 * adapter->stats counters. It's required to make ifconfig -z
   1729 	 * (SOICZIFDATA) work.
   1730 	 */
   1731 	ifp->if_collisions = 0;
   1732 
   1733 	/* Rx Errors */
   1734 	ifp->if_iqdrops += total_missed_rx;
   1735 	ifp->if_ierrors += crcerrs + rlec;
   1736 } /* ixgbe_update_stats_counters */
   1737 
   1738 /************************************************************************
   1739  * ixgbe_add_hw_stats
   1740  *
   1741  *   Add sysctl variables, one per statistic, to the system.
   1742  ************************************************************************/
   1743 static void
   1744 ixgbe_add_hw_stats(struct adapter *adapter)
   1745 {
   1746 	device_t dev = adapter->dev;
   1747 	const struct sysctlnode *rnode, *cnode;
   1748 	struct sysctllog **log = &adapter->sysctllog;
   1749 	struct tx_ring *txr = adapter->tx_rings;
   1750 	struct rx_ring *rxr = adapter->rx_rings;
   1751 	struct ixgbe_hw *hw = &adapter->hw;
   1752 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
   1753 	const char *xname = device_xname(dev);
   1754 	int i;
   1755 
   1756 	/* Driver Statistics */
   1757 	evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
   1758 	    NULL, xname, "Driver tx dma soft fail EFBIG");
   1759 	evcnt_attach_dynamic(&adapter->mbuf_defrag_failed, EVCNT_TYPE_MISC,
   1760 	    NULL, xname, "m_defrag() failed");
   1761 	evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
   1762 	    NULL, xname, "Driver tx dma hard fail EFBIG");
   1763 	evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
   1764 	    NULL, xname, "Driver tx dma hard fail EINVAL");
   1765 	evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
   1766 	    NULL, xname, "Driver tx dma hard fail other");
   1767 	evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
   1768 	    NULL, xname, "Driver tx dma soft fail EAGAIN");
   1769 	evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
   1770 	    NULL, xname, "Driver tx dma soft fail ENOMEM");
   1771 	evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
   1772 	    NULL, xname, "Watchdog timeouts");
   1773 	evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
   1774 	    NULL, xname, "TSO errors");
   1775 	evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_INTR,
   1776 	    NULL, xname, "Link MSI-X IRQ Handled");
   1777 	evcnt_attach_dynamic(&adapter->link_sicount, EVCNT_TYPE_INTR,
   1778 	    NULL, xname, "Link softint");
   1779 	evcnt_attach_dynamic(&adapter->mod_sicount, EVCNT_TYPE_INTR,
   1780 	    NULL, xname, "module softint");
   1781 	evcnt_attach_dynamic(&adapter->msf_sicount, EVCNT_TYPE_INTR,
   1782 	    NULL, xname, "multimode softint");
   1783 	evcnt_attach_dynamic(&adapter->phy_sicount, EVCNT_TYPE_INTR,
   1784 	    NULL, xname, "external PHY softint");
   1785 
   1786 	/* Max number of traffic class is 8 */
   1787 	KASSERT(IXGBE_DCB_MAX_TRAFFIC_CLASS == 8);
   1788 	for (i = 0; i < IXGBE_TC_COUNTER_NUM; i++) {
   1789 		snprintf(adapter->tcs[i].evnamebuf,
   1790 		    sizeof(adapter->tcs[i].evnamebuf), "%s tc%d",
   1791 		    xname, i);
   1792 		if (i < __arraycount(stats->mpc)) {
   1793 			evcnt_attach_dynamic(&stats->mpc[i],
   1794 			    EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
   1795 			    "RX Missed Packet Count");
   1796 			if (hw->mac.type == ixgbe_mac_82598EB)
   1797 				evcnt_attach_dynamic(&stats->rnbc[i],
   1798 				    EVCNT_TYPE_MISC, NULL,
   1799 				    adapter->tcs[i].evnamebuf,
   1800 				    "Receive No Buffers");
   1801 		}
   1802 		if (i < __arraycount(stats->pxontxc)) {
   1803 			evcnt_attach_dynamic(&stats->pxontxc[i],
   1804 			    EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
   1805 			    "pxontxc");
   1806 			evcnt_attach_dynamic(&stats->pxonrxc[i],
   1807 			    EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
   1808 			    "pxonrxc");
   1809 			evcnt_attach_dynamic(&stats->pxofftxc[i],
   1810 			    EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
   1811 			    "pxofftxc");
   1812 			evcnt_attach_dynamic(&stats->pxoffrxc[i],
   1813 			    EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
   1814 			    "pxoffrxc");
   1815 			if (hw->mac.type >= ixgbe_mac_82599EB)
   1816 				evcnt_attach_dynamic(&stats->pxon2offc[i],
   1817 				    EVCNT_TYPE_MISC, NULL,
   1818 				    adapter->tcs[i].evnamebuf,
   1819 			    "pxon2offc");
   1820 		}
   1821 	}
   1822 
   1823 	for (i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   1824 #ifdef LRO
   1825 		struct lro_ctrl *lro = &rxr->lro;
   1826 #endif /* LRO */
   1827 
   1828 		snprintf(adapter->queues[i].evnamebuf,
   1829 		    sizeof(adapter->queues[i].evnamebuf), "%s q%d",
   1830 		    xname, i);
   1831 		snprintf(adapter->queues[i].namebuf,
   1832 		    sizeof(adapter->queues[i].namebuf), "q%d", i);
   1833 
   1834 		if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   1835 			aprint_error_dev(dev, "could not create sysctl root\n");
   1836 			break;
   1837 		}
   1838 
   1839 		if (sysctl_createv(log, 0, &rnode, &rnode,
   1840 		    0, CTLTYPE_NODE,
   1841 		    adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
   1842 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   1843 			break;
   1844 
   1845 		if (sysctl_createv(log, 0, &rnode, &cnode,
   1846 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   1847 		    "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
   1848 		    ixgbe_sysctl_interrupt_rate_handler, 0,
   1849 		    (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
   1850 			break;
   1851 
   1852 		if (sysctl_createv(log, 0, &rnode, &cnode,
   1853 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1854 		    "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
   1855 		    ixgbe_sysctl_tdh_handler, 0, (void *)txr,
   1856 		    0, CTL_CREATE, CTL_EOL) != 0)
   1857 			break;
   1858 
   1859 		if (sysctl_createv(log, 0, &rnode, &cnode,
   1860 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1861 		    "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
   1862 		    ixgbe_sysctl_tdt_handler, 0, (void *)txr,
   1863 		    0, CTL_CREATE, CTL_EOL) != 0)
   1864 			break;
   1865 
   1866 		evcnt_attach_dynamic(&adapter->queues[i].irqs, EVCNT_TYPE_INTR,
   1867 		    NULL, adapter->queues[i].evnamebuf, "IRQs on queue");
   1868 		evcnt_attach_dynamic(&adapter->queues[i].handleq,
   1869 		    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   1870 		    "Handled queue in softint");
   1871 		evcnt_attach_dynamic(&adapter->queues[i].req, EVCNT_TYPE_MISC,
   1872 		    NULL, adapter->queues[i].evnamebuf, "Requeued in softint");
   1873 		evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
   1874 		    NULL, adapter->queues[i].evnamebuf, "TSO");
   1875 		evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
   1876 		    NULL, adapter->queues[i].evnamebuf,
   1877 		    "TX Queue No Descriptor Available");
   1878 		evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
   1879 		    NULL, adapter->queues[i].evnamebuf,
   1880 		    "Queue Packets Transmitted");
   1881 #ifndef IXGBE_LEGACY_TX
   1882 		evcnt_attach_dynamic(&txr->pcq_drops, EVCNT_TYPE_MISC,
   1883 		    NULL, adapter->queues[i].evnamebuf,
   1884 		    "Packets dropped in pcq");
   1885 #endif
   1886 
   1887 		if (sysctl_createv(log, 0, &rnode, &cnode,
   1888 		    CTLFLAG_READONLY, CTLTYPE_INT, "rxd_nxck",
   1889 		    SYSCTL_DESCR("Receive Descriptor next to check"),
   1890 		    ixgbe_sysctl_next_to_check_handler, 0, (void *)rxr, 0,
   1891 		    CTL_CREATE, CTL_EOL) != 0)
   1892 			break;
   1893 
   1894 		if (sysctl_createv(log, 0, &rnode, &cnode,
   1895 		    CTLFLAG_READONLY, CTLTYPE_INT, "rxd_nxrf",
   1896 		    SYSCTL_DESCR("Receive Descriptor next to refresh"),
   1897 		    ixgbe_sysctl_next_to_refresh_handler, 0, (void *)rxr, 0,
   1898 		    CTL_CREATE, CTL_EOL) != 0)
   1899 			break;
   1900 
   1901 		if (sysctl_createv(log, 0, &rnode, &cnode,
   1902 		    CTLFLAG_READONLY, CTLTYPE_INT, "rxd_head",
   1903 		    SYSCTL_DESCR("Receive Descriptor Head"),
   1904 		    ixgbe_sysctl_rdh_handler, 0, (void *)rxr, 0,
   1905 		    CTL_CREATE, CTL_EOL) != 0)
   1906 			break;
   1907 
   1908 		if (sysctl_createv(log, 0, &rnode, &cnode,
   1909 		    CTLFLAG_READONLY, CTLTYPE_INT, "rxd_tail",
   1910 		    SYSCTL_DESCR("Receive Descriptor Tail"),
   1911 		    ixgbe_sysctl_rdt_handler, 0, (void *)rxr, 0,
   1912 		    CTL_CREATE, CTL_EOL) != 0)
   1913 			break;
   1914 
   1915 		if (i < __arraycount(stats->qprc)) {
   1916 			evcnt_attach_dynamic(&stats->qprc[i], EVCNT_TYPE_MISC,
   1917 			    NULL, adapter->queues[i].evnamebuf, "qprc");
   1918 			evcnt_attach_dynamic(&stats->qptc[i], EVCNT_TYPE_MISC,
   1919 			    NULL, adapter->queues[i].evnamebuf, "qptc");
   1920 			evcnt_attach_dynamic(&stats->qbrc[i], EVCNT_TYPE_MISC,
   1921 			    NULL, adapter->queues[i].evnamebuf, "qbrc");
   1922 			evcnt_attach_dynamic(&stats->qbtc[i], EVCNT_TYPE_MISC,
   1923 			    NULL, adapter->queues[i].evnamebuf, "qbtc");
   1924 			if (hw->mac.type >= ixgbe_mac_82599EB)
   1925 				evcnt_attach_dynamic(&stats->qprdc[i],
   1926 				    EVCNT_TYPE_MISC, NULL,
   1927 				    adapter->queues[i].evnamebuf, "qprdc");
   1928 		}
   1929 
   1930 		evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
   1931 		    NULL, adapter->queues[i].evnamebuf,
   1932 		    "Queue Packets Received");
   1933 		evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
   1934 		    NULL, adapter->queues[i].evnamebuf,
   1935 		    "Queue Bytes Received");
   1936 		evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
   1937 		    NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
   1938 		evcnt_attach_dynamic(&rxr->no_mbuf, EVCNT_TYPE_MISC,
   1939 		    NULL, adapter->queues[i].evnamebuf, "Rx no mbuf");
   1940 		evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
   1941 		    NULL, adapter->queues[i].evnamebuf, "Rx discarded");
   1942 #ifdef LRO
   1943 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
   1944 				CTLFLAG_RD, &lro->lro_queued, 0,
   1945 				"LRO Queued");
   1946 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
   1947 				CTLFLAG_RD, &lro->lro_flushed, 0,
   1948 				"LRO Flushed");
   1949 #endif /* LRO */
   1950 	}
   1951 
   1952 	/* MAC stats get their own sub node */
   1953 
   1954 	snprintf(stats->namebuf,
   1955 	    sizeof(stats->namebuf), "%s MAC Statistics", xname);
   1956 
   1957 	evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
   1958 	    stats->namebuf, "rx csum offload - IP");
   1959 	evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
   1960 	    stats->namebuf, "rx csum offload - L4");
   1961 	evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
   1962 	    stats->namebuf, "rx csum offload - IP bad");
   1963 	evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
   1964 	    stats->namebuf, "rx csum offload - L4 bad");
   1965 	evcnt_attach_dynamic(&stats->intzero, EVCNT_TYPE_MISC, NULL,
   1966 	    stats->namebuf, "Interrupt conditions zero");
   1967 	evcnt_attach_dynamic(&stats->legint, EVCNT_TYPE_MISC, NULL,
   1968 	    stats->namebuf, "Legacy interrupts");
   1969 
   1970 	evcnt_attach_dynamic(&stats->crcerrs, EVCNT_TYPE_MISC, NULL,
   1971 	    stats->namebuf, "CRC Errors");
   1972 	evcnt_attach_dynamic(&stats->illerrc, EVCNT_TYPE_MISC, NULL,
   1973 	    stats->namebuf, "Illegal Byte Errors");
   1974 	evcnt_attach_dynamic(&stats->errbc, EVCNT_TYPE_MISC, NULL,
   1975 	    stats->namebuf, "Byte Errors");
   1976 	evcnt_attach_dynamic(&stats->mspdc, EVCNT_TYPE_MISC, NULL,
   1977 	    stats->namebuf, "MAC Short Packets Discarded");
   1978 	if (hw->mac.type >= ixgbe_mac_X550)
   1979 		evcnt_attach_dynamic(&stats->mbsdc, EVCNT_TYPE_MISC, NULL,
   1980 		    stats->namebuf, "Bad SFD");
   1981 	evcnt_attach_dynamic(&stats->mpctotal, EVCNT_TYPE_MISC, NULL,
   1982 	    stats->namebuf, "Total Packets Missed");
   1983 	evcnt_attach_dynamic(&stats->mlfc, EVCNT_TYPE_MISC, NULL,
   1984 	    stats->namebuf, "MAC Local Faults");
   1985 	evcnt_attach_dynamic(&stats->mrfc, EVCNT_TYPE_MISC, NULL,
   1986 	    stats->namebuf, "MAC Remote Faults");
   1987 	evcnt_attach_dynamic(&stats->rlec, EVCNT_TYPE_MISC, NULL,
   1988 	    stats->namebuf, "Receive Length Errors");
   1989 	evcnt_attach_dynamic(&stats->lxontxc, EVCNT_TYPE_MISC, NULL,
   1990 	    stats->namebuf, "Link XON Transmitted");
   1991 	evcnt_attach_dynamic(&stats->lxonrxc, EVCNT_TYPE_MISC, NULL,
   1992 	    stats->namebuf, "Link XON Received");
   1993 	evcnt_attach_dynamic(&stats->lxofftxc, EVCNT_TYPE_MISC, NULL,
   1994 	    stats->namebuf, "Link XOFF Transmitted");
   1995 	evcnt_attach_dynamic(&stats->lxoffrxc, EVCNT_TYPE_MISC, NULL,
   1996 	    stats->namebuf, "Link XOFF Received");
   1997 
   1998 	/* Packet Reception Stats */
   1999 	evcnt_attach_dynamic(&stats->tor, EVCNT_TYPE_MISC, NULL,
   2000 	    stats->namebuf, "Total Octets Received");
   2001 	evcnt_attach_dynamic(&stats->gorc, EVCNT_TYPE_MISC, NULL,
   2002 	    stats->namebuf, "Good Octets Received");
   2003 	evcnt_attach_dynamic(&stats->tpr, EVCNT_TYPE_MISC, NULL,
   2004 	    stats->namebuf, "Total Packets Received");
   2005 	evcnt_attach_dynamic(&stats->gprc, EVCNT_TYPE_MISC, NULL,
   2006 	    stats->namebuf, "Good Packets Received");
   2007 	evcnt_attach_dynamic(&stats->mprc, EVCNT_TYPE_MISC, NULL,
   2008 	    stats->namebuf, "Multicast Packets Received");
   2009 	evcnt_attach_dynamic(&stats->bprc, EVCNT_TYPE_MISC, NULL,
   2010 	    stats->namebuf, "Broadcast Packets Received");
   2011 	evcnt_attach_dynamic(&stats->prc64, EVCNT_TYPE_MISC, NULL,
   2012 	    stats->namebuf, "64 byte frames received ");
   2013 	evcnt_attach_dynamic(&stats->prc127, EVCNT_TYPE_MISC, NULL,
   2014 	    stats->namebuf, "65-127 byte frames received");
   2015 	evcnt_attach_dynamic(&stats->prc255, EVCNT_TYPE_MISC, NULL,
   2016 	    stats->namebuf, "128-255 byte frames received");
   2017 	evcnt_attach_dynamic(&stats->prc511, EVCNT_TYPE_MISC, NULL,
   2018 	    stats->namebuf, "256-511 byte frames received");
   2019 	evcnt_attach_dynamic(&stats->prc1023, EVCNT_TYPE_MISC, NULL,
   2020 	    stats->namebuf, "512-1023 byte frames received");
   2021 	evcnt_attach_dynamic(&stats->prc1522, EVCNT_TYPE_MISC, NULL,
   2022 	    stats->namebuf, "1023-1522 byte frames received");
   2023 	evcnt_attach_dynamic(&stats->ruc, EVCNT_TYPE_MISC, NULL,
   2024 	    stats->namebuf, "Receive Undersized");
   2025 	evcnt_attach_dynamic(&stats->rfc, EVCNT_TYPE_MISC, NULL,
   2026 	    stats->namebuf, "Fragmented Packets Received ");
   2027 	evcnt_attach_dynamic(&stats->roc, EVCNT_TYPE_MISC, NULL,
   2028 	    stats->namebuf, "Oversized Packets Received");
   2029 	evcnt_attach_dynamic(&stats->rjc, EVCNT_TYPE_MISC, NULL,
   2030 	    stats->namebuf, "Received Jabber");
   2031 	evcnt_attach_dynamic(&stats->mngprc, EVCNT_TYPE_MISC, NULL,
   2032 	    stats->namebuf, "Management Packets Received");
   2033 	evcnt_attach_dynamic(&stats->mngpdc, EVCNT_TYPE_MISC, NULL,
   2034 	    stats->namebuf, "Management Packets Dropped");
   2035 	evcnt_attach_dynamic(&stats->xec, EVCNT_TYPE_MISC, NULL,
   2036 	    stats->namebuf, "Checksum Errors");
   2037 
   2038 	/* Packet Transmission Stats */
   2039 	evcnt_attach_dynamic(&stats->gotc, EVCNT_TYPE_MISC, NULL,
   2040 	    stats->namebuf, "Good Octets Transmitted");
   2041 	evcnt_attach_dynamic(&stats->tpt, EVCNT_TYPE_MISC, NULL,
   2042 	    stats->namebuf, "Total Packets Transmitted");
   2043 	evcnt_attach_dynamic(&stats->gptc, EVCNT_TYPE_MISC, NULL,
   2044 	    stats->namebuf, "Good Packets Transmitted");
   2045 	evcnt_attach_dynamic(&stats->bptc, EVCNT_TYPE_MISC, NULL,
   2046 	    stats->namebuf, "Broadcast Packets Transmitted");
   2047 	evcnt_attach_dynamic(&stats->mptc, EVCNT_TYPE_MISC, NULL,
   2048 	    stats->namebuf, "Multicast Packets Transmitted");
   2049 	evcnt_attach_dynamic(&stats->mngptc, EVCNT_TYPE_MISC, NULL,
   2050 	    stats->namebuf, "Management Packets Transmitted");
   2051 	evcnt_attach_dynamic(&stats->ptc64, EVCNT_TYPE_MISC, NULL,
   2052 	    stats->namebuf, "64 byte frames transmitted ");
   2053 	evcnt_attach_dynamic(&stats->ptc127, EVCNT_TYPE_MISC, NULL,
   2054 	    stats->namebuf, "65-127 byte frames transmitted");
   2055 	evcnt_attach_dynamic(&stats->ptc255, EVCNT_TYPE_MISC, NULL,
   2056 	    stats->namebuf, "128-255 byte frames transmitted");
   2057 	evcnt_attach_dynamic(&stats->ptc511, EVCNT_TYPE_MISC, NULL,
   2058 	    stats->namebuf, "256-511 byte frames transmitted");
   2059 	evcnt_attach_dynamic(&stats->ptc1023, EVCNT_TYPE_MISC, NULL,
   2060 	    stats->namebuf, "512-1023 byte frames transmitted");
   2061 	evcnt_attach_dynamic(&stats->ptc1522, EVCNT_TYPE_MISC, NULL,
   2062 	    stats->namebuf, "1024-1522 byte frames transmitted");
   2063 } /* ixgbe_add_hw_stats */
   2064 
   2065 static void
   2066 ixgbe_clear_evcnt(struct adapter *adapter)
   2067 {
   2068 	struct tx_ring *txr = adapter->tx_rings;
   2069 	struct rx_ring *rxr = adapter->rx_rings;
   2070 	struct ixgbe_hw *hw = &adapter->hw;
   2071 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
   2072 	int i;
   2073 
   2074 	adapter->efbig_tx_dma_setup.ev_count = 0;
   2075 	adapter->mbuf_defrag_failed.ev_count = 0;
   2076 	adapter->efbig2_tx_dma_setup.ev_count = 0;
   2077 	adapter->einval_tx_dma_setup.ev_count = 0;
   2078 	adapter->other_tx_dma_setup.ev_count = 0;
   2079 	adapter->eagain_tx_dma_setup.ev_count = 0;
   2080 	adapter->enomem_tx_dma_setup.ev_count = 0;
   2081 	adapter->tso_err.ev_count = 0;
   2082 	adapter->watchdog_events.ev_count = 0;
   2083 	adapter->link_irq.ev_count = 0;
   2084 	adapter->link_sicount.ev_count = 0;
   2085 	adapter->mod_sicount.ev_count = 0;
   2086 	adapter->msf_sicount.ev_count = 0;
   2087 	adapter->phy_sicount.ev_count = 0;
   2088 
   2089 	for (i = 0; i < IXGBE_TC_COUNTER_NUM; i++) {
   2090 		if (i < __arraycount(stats->mpc)) {
   2091 			stats->mpc[i].ev_count = 0;
   2092 			if (hw->mac.type == ixgbe_mac_82598EB)
   2093 				stats->rnbc[i].ev_count = 0;
   2094 		}
   2095 		if (i < __arraycount(stats->pxontxc)) {
   2096 			stats->pxontxc[i].ev_count = 0;
   2097 			stats->pxonrxc[i].ev_count = 0;
   2098 			stats->pxofftxc[i].ev_count = 0;
   2099 			stats->pxoffrxc[i].ev_count = 0;
   2100 			if (hw->mac.type >= ixgbe_mac_82599EB)
   2101 				stats->pxon2offc[i].ev_count = 0;
   2102 		}
   2103 	}
   2104 
   2105 	txr = adapter->tx_rings;
   2106 	for (i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   2107 		adapter->queues[i].irqs.ev_count = 0;
   2108 		adapter->queues[i].handleq.ev_count = 0;
   2109 		adapter->queues[i].req.ev_count = 0;
   2110 		txr->no_desc_avail.ev_count = 0;
   2111 		txr->total_packets.ev_count = 0;
   2112 		txr->tso_tx.ev_count = 0;
   2113 #ifndef IXGBE_LEGACY_TX
   2114 		txr->pcq_drops.ev_count = 0;
   2115 #endif
   2116 		txr->q_efbig_tx_dma_setup = 0;
   2117 		txr->q_mbuf_defrag_failed = 0;
   2118 		txr->q_efbig2_tx_dma_setup = 0;
   2119 		txr->q_einval_tx_dma_setup = 0;
   2120 		txr->q_other_tx_dma_setup = 0;
   2121 		txr->q_eagain_tx_dma_setup = 0;
   2122 		txr->q_enomem_tx_dma_setup = 0;
   2123 		txr->q_tso_err = 0;
   2124 
   2125 		if (i < __arraycount(stats->qprc)) {
   2126 			stats->qprc[i].ev_count = 0;
   2127 			stats->qptc[i].ev_count = 0;
   2128 			stats->qbrc[i].ev_count = 0;
   2129 			stats->qbtc[i].ev_count = 0;
   2130 			if (hw->mac.type >= ixgbe_mac_82599EB)
   2131 				stats->qprdc[i].ev_count = 0;
   2132 		}
   2133 
   2134 		rxr->rx_packets.ev_count = 0;
   2135 		rxr->rx_bytes.ev_count = 0;
   2136 		rxr->rx_copies.ev_count = 0;
   2137 		rxr->no_mbuf.ev_count = 0;
   2138 		rxr->rx_discarded.ev_count = 0;
   2139 	}
   2140 	stats->ipcs.ev_count = 0;
   2141 	stats->l4cs.ev_count = 0;
   2142 	stats->ipcs_bad.ev_count = 0;
   2143 	stats->l4cs_bad.ev_count = 0;
   2144 	stats->intzero.ev_count = 0;
   2145 	stats->legint.ev_count = 0;
   2146 	stats->crcerrs.ev_count = 0;
   2147 	stats->illerrc.ev_count = 0;
   2148 	stats->errbc.ev_count = 0;
   2149 	stats->mspdc.ev_count = 0;
   2150 	if (hw->mac.type >= ixgbe_mac_X550)
   2151 		stats->mbsdc.ev_count = 0;
   2152 	stats->mpctotal.ev_count = 0;
   2153 	stats->mlfc.ev_count = 0;
   2154 	stats->mrfc.ev_count = 0;
   2155 	stats->rlec.ev_count = 0;
   2156 	stats->lxontxc.ev_count = 0;
   2157 	stats->lxonrxc.ev_count = 0;
   2158 	stats->lxofftxc.ev_count = 0;
   2159 	stats->lxoffrxc.ev_count = 0;
   2160 
   2161 	/* Packet Reception Stats */
   2162 	stats->tor.ev_count = 0;
   2163 	stats->gorc.ev_count = 0;
   2164 	stats->tpr.ev_count = 0;
   2165 	stats->gprc.ev_count = 0;
   2166 	stats->mprc.ev_count = 0;
   2167 	stats->bprc.ev_count = 0;
   2168 	stats->prc64.ev_count = 0;
   2169 	stats->prc127.ev_count = 0;
   2170 	stats->prc255.ev_count = 0;
   2171 	stats->prc511.ev_count = 0;
   2172 	stats->prc1023.ev_count = 0;
   2173 	stats->prc1522.ev_count = 0;
   2174 	stats->ruc.ev_count = 0;
   2175 	stats->rfc.ev_count = 0;
   2176 	stats->roc.ev_count = 0;
   2177 	stats->rjc.ev_count = 0;
   2178 	stats->mngprc.ev_count = 0;
   2179 	stats->mngpdc.ev_count = 0;
   2180 	stats->xec.ev_count = 0;
   2181 
   2182 	/* Packet Transmission Stats */
   2183 	stats->gotc.ev_count = 0;
   2184 	stats->tpt.ev_count = 0;
   2185 	stats->gptc.ev_count = 0;
   2186 	stats->bptc.ev_count = 0;
   2187 	stats->mptc.ev_count = 0;
   2188 	stats->mngptc.ev_count = 0;
   2189 	stats->ptc64.ev_count = 0;
   2190 	stats->ptc127.ev_count = 0;
   2191 	stats->ptc255.ev_count = 0;
   2192 	stats->ptc511.ev_count = 0;
   2193 	stats->ptc1023.ev_count = 0;
   2194 	stats->ptc1522.ev_count = 0;
   2195 }
   2196 
   2197 /************************************************************************
   2198  * ixgbe_sysctl_tdh_handler - Transmit Descriptor Head handler function
   2199  *
   2200  *   Retrieves the TDH value from the hardware
   2201  ************************************************************************/
   2202 static int
   2203 ixgbe_sysctl_tdh_handler(SYSCTLFN_ARGS)
   2204 {
   2205 	struct sysctlnode node = *rnode;
   2206 	struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
   2207 	struct adapter *adapter;
   2208 	uint32_t val;
   2209 
   2210 	if (!txr)
   2211 		return (0);
   2212 
   2213 	adapter = txr->adapter;
   2214 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   2215 		return (EPERM);
   2216 
   2217 	val = IXGBE_READ_REG(&adapter->hw, IXGBE_TDH(txr->me));
   2218 	node.sysctl_data = &val;
   2219 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2220 } /* ixgbe_sysctl_tdh_handler */
   2221 
   2222 /************************************************************************
   2223  * ixgbe_sysctl_tdt_handler - Transmit Descriptor Tail handler function
   2224  *
   2225  *   Retrieves the TDT value from the hardware
   2226  ************************************************************************/
   2227 static int
   2228 ixgbe_sysctl_tdt_handler(SYSCTLFN_ARGS)
   2229 {
   2230 	struct sysctlnode node = *rnode;
   2231 	struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
   2232 	struct adapter *adapter;
   2233 	uint32_t val;
   2234 
   2235 	if (!txr)
   2236 		return (0);
   2237 
   2238 	adapter = txr->adapter;
   2239 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   2240 		return (EPERM);
   2241 
   2242 	val = IXGBE_READ_REG(&adapter->hw, IXGBE_TDT(txr->me));
   2243 	node.sysctl_data = &val;
   2244 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2245 } /* ixgbe_sysctl_tdt_handler */
   2246 
   2247 /************************************************************************
   2248  * ixgbe_sysctl_next_to_check_handler - Receive Descriptor next to check
   2249  * handler function
   2250  *
   2251  *   Retrieves the next_to_check value
   2252  ************************************************************************/
   2253 static int
   2254 ixgbe_sysctl_next_to_check_handler(SYSCTLFN_ARGS)
   2255 {
   2256 	struct sysctlnode node = *rnode;
   2257 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   2258 	struct adapter *adapter;
   2259 	uint32_t val;
   2260 
   2261 	if (!rxr)
   2262 		return (0);
   2263 
   2264 	adapter = rxr->adapter;
   2265 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   2266 		return (EPERM);
   2267 
   2268 	val = rxr->next_to_check;
   2269 	node.sysctl_data = &val;
   2270 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2271 } /* ixgbe_sysctl_next_to_check_handler */
   2272 
   2273 /************************************************************************
   2274  * ixgbe_sysctl_next_to_refresh_handler - Receive Descriptor next to check
   2275  * handler function
   2276  *
   2277  *   Retrieves the next_to_refresh value
   2278  ************************************************************************/
   2279 static int
   2280 ixgbe_sysctl_next_to_refresh_handler(SYSCTLFN_ARGS)
   2281 {
   2282 	struct sysctlnode node = *rnode;
   2283 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   2284 	struct adapter *adapter;
   2285 	uint32_t val;
   2286 
   2287 	if (!rxr)
   2288 		return (0);
   2289 
   2290 	adapter = rxr->adapter;
   2291 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   2292 		return (EPERM);
   2293 
   2294 	val = rxr->next_to_refresh;
   2295 	node.sysctl_data = &val;
   2296 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2297 } /* ixgbe_sysctl_next_to_refresh_handler */
   2298 
   2299 /************************************************************************
   2300  * ixgbe_sysctl_rdh_handler - Receive Descriptor Head handler function
   2301  *
   2302  *   Retrieves the RDH value from the hardware
   2303  ************************************************************************/
   2304 static int
   2305 ixgbe_sysctl_rdh_handler(SYSCTLFN_ARGS)
   2306 {
   2307 	struct sysctlnode node = *rnode;
   2308 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   2309 	struct adapter *adapter;
   2310 	uint32_t val;
   2311 
   2312 	if (!rxr)
   2313 		return (0);
   2314 
   2315 	adapter = rxr->adapter;
   2316 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   2317 		return (EPERM);
   2318 
   2319 	val = IXGBE_READ_REG(&adapter->hw, IXGBE_RDH(rxr->me));
   2320 	node.sysctl_data = &val;
   2321 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2322 } /* ixgbe_sysctl_rdh_handler */
   2323 
   2324 /************************************************************************
   2325  * ixgbe_sysctl_rdt_handler - Receive Descriptor Tail handler function
   2326  *
   2327  *   Retrieves the RDT value from the hardware
   2328  ************************************************************************/
   2329 static int
   2330 ixgbe_sysctl_rdt_handler(SYSCTLFN_ARGS)
   2331 {
   2332 	struct sysctlnode node = *rnode;
   2333 	struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
   2334 	struct adapter *adapter;
   2335 	uint32_t val;
   2336 
   2337 	if (!rxr)
   2338 		return (0);
   2339 
   2340 	adapter = rxr->adapter;
   2341 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   2342 		return (EPERM);
   2343 
   2344 	val = IXGBE_READ_REG(&adapter->hw, IXGBE_RDT(rxr->me));
   2345 	node.sysctl_data = &val;
   2346 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2347 } /* ixgbe_sysctl_rdt_handler */
   2348 
   2349 #if 0	/* XXX Badly need to overhaul vlan(4) on NetBSD. */
   2350 /************************************************************************
   2351  * ixgbe_register_vlan
   2352  *
   2353  *   Run via vlan config EVENT, it enables us to use the
   2354  *   HW Filter table since we can get the vlan id. This
   2355  *   just creates the entry in the soft version of the
   2356  *   VFTA, init will repopulate the real table.
   2357  ************************************************************************/
   2358 static void
   2359 ixgbe_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   2360 {
   2361 	struct adapter	*adapter = ifp->if_softc;
   2362 	u16		index, bit;
   2363 
   2364 	if (ifp->if_softc != arg)   /* Not our event */
   2365 		return;
   2366 
   2367 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   2368 		return;
   2369 
   2370 	IXGBE_CORE_LOCK(adapter);
   2371 	index = (vtag >> 5) & 0x7F;
   2372 	bit = vtag & 0x1F;
   2373 	adapter->shadow_vfta[index] |= ((u32)1 << bit);
   2374 	ixgbe_setup_vlan_hw_support(adapter);
   2375 	IXGBE_CORE_UNLOCK(adapter);
   2376 } /* ixgbe_register_vlan */
   2377 
   2378 /************************************************************************
   2379  * ixgbe_unregister_vlan
   2380  *
   2381  *   Run via vlan unconfig EVENT, remove our entry in the soft vfta.
   2382  ************************************************************************/
   2383 static void
   2384 ixgbe_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   2385 {
   2386 	struct adapter	*adapter = ifp->if_softc;
   2387 	u16		index, bit;
   2388 
   2389 	if (ifp->if_softc != arg)
   2390 		return;
   2391 
   2392 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   2393 		return;
   2394 
   2395 	IXGBE_CORE_LOCK(adapter);
   2396 	index = (vtag >> 5) & 0x7F;
   2397 	bit = vtag & 0x1F;
   2398 	adapter->shadow_vfta[index] &= ~((u32)1 << bit);
   2399 	/* Re-init to load the changes */
   2400 	ixgbe_setup_vlan_hw_support(adapter);
   2401 	IXGBE_CORE_UNLOCK(adapter);
   2402 } /* ixgbe_unregister_vlan */
   2403 #endif
   2404 
   2405 static void
   2406 ixgbe_setup_vlan_hw_support(struct adapter *adapter)
   2407 {
   2408 	struct ethercom *ec = &adapter->osdep.ec;
   2409 	struct ixgbe_hw *hw = &adapter->hw;
   2410 	struct rx_ring	*rxr;
   2411 	int		i;
   2412 	u32		ctrl;
   2413 	bool		hwtagging;
   2414 
   2415 	/*
   2416 	 *  This function is called from both if_init and ifflags_cb()
   2417 	 * on NetBSD.
   2418 	 */
   2419 
   2420 	/* Enable HW tagging only if any vlan is attached */
   2421 	hwtagging = (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING)
   2422 	    && VLAN_ATTACHED(ec);
   2423 
   2424 	/* Setup the queues for vlans */
   2425 	for (i = 0; i < adapter->num_queues; i++) {
   2426 		rxr = &adapter->rx_rings[i];
   2427 		/*
   2428 		 * On 82599 and later, the VLAN enable is per/queue in RXDCTL.
   2429 		 */
   2430 		if (hw->mac.type != ixgbe_mac_82598EB) {
   2431 			ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
   2432 			if (hwtagging)
   2433 				ctrl |= IXGBE_RXDCTL_VME;
   2434 			else
   2435 				ctrl &= ~IXGBE_RXDCTL_VME;
   2436 			IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), ctrl);
   2437 		}
   2438 		rxr->vtag_strip = hwtagging ? TRUE : FALSE;
   2439 	}
   2440 
   2441 	/*
   2442 	 * A soft reset zero's out the VFTA, so
   2443 	 * we need to repopulate it now.
   2444 	 */
   2445 	for (i = 0; i < IXGBE_VFTA_SIZE; i++)
   2446 		if (adapter->shadow_vfta[i] != 0)
   2447 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(i),
   2448 			    adapter->shadow_vfta[i]);
   2449 
   2450 	ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
   2451 	/* Enable the Filter Table if enabled */
   2452 	if (ec->ec_capenable & ETHERCAP_VLAN_HWFILTER)
   2453 		ctrl |= IXGBE_VLNCTRL_VFE;
   2454 	else
   2455 		ctrl &= ~IXGBE_VLNCTRL_VFE;
   2456 	/* VLAN hw tagging for 82598 */
   2457 	if (hw->mac.type == ixgbe_mac_82598EB) {
   2458 		if (hwtagging)
   2459 			ctrl |= IXGBE_VLNCTRL_VME;
   2460 		else
   2461 			ctrl &= ~IXGBE_VLNCTRL_VME;
   2462 	}
   2463 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
   2464 } /* ixgbe_setup_vlan_hw_support */
   2465 
   2466 /************************************************************************
   2467  * ixgbe_get_slot_info
   2468  *
   2469  *   Get the width and transaction speed of
   2470  *   the slot this adapter is plugged into.
   2471  ************************************************************************/
   2472 static void
   2473 ixgbe_get_slot_info(struct adapter *adapter)
   2474 {
   2475 	device_t		dev = adapter->dev;
   2476 	struct ixgbe_hw		*hw = &adapter->hw;
   2477 	u32		      offset;
   2478 	u16			link;
   2479 	int		      bus_info_valid = TRUE;
   2480 
   2481 	/* Some devices are behind an internal bridge */
   2482 	switch (hw->device_id) {
   2483 	case IXGBE_DEV_ID_82599_SFP_SF_QP:
   2484 	case IXGBE_DEV_ID_82599_QSFP_SF_QP:
   2485 		goto get_parent_info;
   2486 	default:
   2487 		break;
   2488 	}
   2489 
   2490 	ixgbe_get_bus_info(hw);
   2491 
   2492 	/*
   2493 	 * Some devices don't use PCI-E, but there is no need
   2494 	 * to display "Unknown" for bus speed and width.
   2495 	 */
   2496 	switch (hw->mac.type) {
   2497 	case ixgbe_mac_X550EM_x:
   2498 	case ixgbe_mac_X550EM_a:
   2499 		return;
   2500 	default:
   2501 		goto display;
   2502 	}
   2503 
   2504 get_parent_info:
   2505 	/*
   2506 	 * For the Quad port adapter we need to parse back
   2507 	 * up the PCI tree to find the speed of the expansion
   2508 	 * slot into which this adapter is plugged. A bit more work.
   2509 	 */
   2510 	dev = device_parent(device_parent(dev));
   2511 #if 0
   2512 #ifdef IXGBE_DEBUG
   2513 	device_printf(dev, "parent pcib = %x,%x,%x\n", pci_get_bus(dev),
   2514 	    pci_get_slot(dev), pci_get_function(dev));
   2515 #endif
   2516 	dev = device_parent(device_parent(dev));
   2517 #ifdef IXGBE_DEBUG
   2518 	device_printf(dev, "slot pcib = %x,%x,%x\n", pci_get_bus(dev),
   2519 	    pci_get_slot(dev), pci_get_function(dev));
   2520 #endif
   2521 #endif
   2522 	/* Now get the PCI Express Capabilities offset */
   2523 	if (pci_get_capability(adapter->osdep.pc, adapter->osdep.tag,
   2524 	    PCI_CAP_PCIEXPRESS, &offset, NULL)) {
   2525 		/*
   2526 		 * Hmm...can't get PCI-Express capabilities.
   2527 		 * Falling back to default method.
   2528 		 */
   2529 		bus_info_valid = FALSE;
   2530 		ixgbe_get_bus_info(hw);
   2531 		goto display;
   2532 	}
   2533 	/* ...and read the Link Status Register */
   2534 	link = pci_conf_read(adapter->osdep.pc, adapter->osdep.tag,
   2535 	    offset + PCIE_LCSR) >> 16;
   2536 	ixgbe_set_pci_config_data_generic(hw, link);
   2537 
   2538 display:
   2539 	device_printf(dev, "PCI Express Bus: Speed %s Width %s\n",
   2540 	    ((hw->bus.speed == ixgbe_bus_speed_8000)	? "8.0GT/s" :
   2541 	     (hw->bus.speed == ixgbe_bus_speed_5000)	? "5.0GT/s" :
   2542 	     (hw->bus.speed == ixgbe_bus_speed_2500)	? "2.5GT/s" :
   2543 	     "Unknown"),
   2544 	    ((hw->bus.width == ixgbe_bus_width_pcie_x8) ? "x8" :
   2545 	     (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "x4" :
   2546 	     (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "x1" :
   2547 	     "Unknown"));
   2548 
   2549 	if (bus_info_valid) {
   2550 		if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   2551 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
   2552 			(hw->bus.speed == ixgbe_bus_speed_2500))) {
   2553 			device_printf(dev, "PCI-Express bandwidth available"
   2554 			    " for this card\n     is not sufficient for"
   2555 			    " optimal performance.\n");
   2556 			device_printf(dev, "For optimal performance a x8 "
   2557 			    "PCIE, or x4 PCIE Gen2 slot is required.\n");
   2558 		}
   2559 		if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   2560 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
   2561 			(hw->bus.speed < ixgbe_bus_speed_8000))) {
   2562 			device_printf(dev, "PCI-Express bandwidth available"
   2563 			    " for this card\n     is not sufficient for"
   2564 			    " optimal performance.\n");
   2565 			device_printf(dev, "For optimal performance a x8 "
   2566 			    "PCIE Gen3 slot is required.\n");
   2567 		}
   2568 	} else
   2569 		device_printf(dev, "Unable to determine slot speed/width. The speed/width reported are that of the internal switch.\n");
   2570 
   2571 	return;
   2572 } /* ixgbe_get_slot_info */
   2573 
   2574 /************************************************************************
   2575  * ixgbe_enable_queue - MSI-X Interrupt Handlers and Tasklets
   2576  ************************************************************************/
   2577 static inline void
   2578 ixgbe_enable_queue(struct adapter *adapter, u32 vector)
   2579 {
   2580 	struct ixgbe_hw *hw = &adapter->hw;
   2581 	struct ix_queue *que = &adapter->queues[vector];
   2582 	u64		queue = 1ULL << vector;
   2583 	u32		mask;
   2584 
   2585 	mutex_enter(&que->dc_mtx);
   2586 	if (que->disabled_count > 0 && --que->disabled_count > 0)
   2587 		goto out;
   2588 
   2589 	if (hw->mac.type == ixgbe_mac_82598EB) {
   2590 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
   2591 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
   2592 	} else {
   2593 		mask = (queue & 0xFFFFFFFF);
   2594 		if (mask)
   2595 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
   2596 		mask = (queue >> 32);
   2597 		if (mask)
   2598 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
   2599 	}
   2600 out:
   2601 	mutex_exit(&que->dc_mtx);
   2602 } /* ixgbe_enable_queue */
   2603 
   2604 /************************************************************************
   2605  * ixgbe_disable_queue_internal
   2606  ************************************************************************/
   2607 static inline void
   2608 ixgbe_disable_queue_internal(struct adapter *adapter, u32 vector, bool nestok)
   2609 {
   2610 	struct ixgbe_hw *hw = &adapter->hw;
   2611 	struct ix_queue *que = &adapter->queues[vector];
   2612 	u64		queue = 1ULL << vector;
   2613 	u32		mask;
   2614 
   2615 	mutex_enter(&que->dc_mtx);
   2616 
   2617 	if (que->disabled_count > 0) {
   2618 		if (nestok)
   2619 			que->disabled_count++;
   2620 		goto out;
   2621 	}
   2622 	que->disabled_count++;
   2623 
   2624 	if (hw->mac.type == ixgbe_mac_82598EB) {
   2625 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
   2626 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
   2627 	} else {
   2628 		mask = (queue & 0xFFFFFFFF);
   2629 		if (mask)
   2630 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
   2631 		mask = (queue >> 32);
   2632 		if (mask)
   2633 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
   2634 	}
   2635 out:
   2636 	mutex_exit(&que->dc_mtx);
   2637 } /* ixgbe_disable_queue_internal */
   2638 
   2639 /************************************************************************
   2640  * ixgbe_disable_queue
   2641  ************************************************************************/
   2642 static inline void
   2643 ixgbe_disable_queue(struct adapter *adapter, u32 vector)
   2644 {
   2645 
   2646 	ixgbe_disable_queue_internal(adapter, vector, true);
   2647 } /* ixgbe_disable_queue */
   2648 
   2649 /************************************************************************
   2650  * ixgbe_sched_handle_que - schedule deferred packet processing
   2651  ************************************************************************/
   2652 static inline void
   2653 ixgbe_sched_handle_que(struct adapter *adapter, struct ix_queue *que)
   2654 {
   2655 
   2656 	if (que->txrx_use_workqueue) {
   2657 		/*
   2658 		 * adapter->que_wq is bound to each CPU instead of
   2659 		 * each NIC queue to reduce workqueue kthread. As we
   2660 		 * should consider about interrupt affinity in this
   2661 		 * function, the workqueue kthread must be WQ_PERCPU.
   2662 		 * If create WQ_PERCPU workqueue kthread for each NIC
   2663 		 * queue, that number of created workqueue kthread is
   2664 		 * (number of used NIC queue) * (number of CPUs) =
   2665 		 * (number of CPUs) ^ 2 most often.
   2666 		 *
   2667 		 * The same NIC queue's interrupts are avoided by
   2668 		 * masking the queue's interrupt. And different
   2669 		 * NIC queue's interrupts use different struct work
   2670 		 * (que->wq_cookie). So, "enqueued flag" to avoid
   2671 		 * twice workqueue_enqueue() is not required .
   2672 		 */
   2673 		workqueue_enqueue(adapter->que_wq, &que->wq_cookie, curcpu());
   2674 	} else {
   2675 		softint_schedule(que->que_si);
   2676 	}
   2677 }
   2678 
   2679 /************************************************************************
   2680  * ixgbe_msix_que - MSI-X Queue Interrupt Service routine
   2681  ************************************************************************/
   2682 static int
   2683 ixgbe_msix_que(void *arg)
   2684 {
   2685 	struct ix_queue	*que = arg;
   2686 	struct adapter	*adapter = que->adapter;
   2687 	struct ifnet	*ifp = adapter->ifp;
   2688 	struct tx_ring	*txr = que->txr;
   2689 	struct rx_ring	*rxr = que->rxr;
   2690 	bool		more;
   2691 	u32		newitr = 0;
   2692 
   2693 	/* Protect against spurious interrupts */
   2694 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   2695 		return 0;
   2696 
   2697 	ixgbe_disable_queue(adapter, que->msix);
   2698 	++que->irqs.ev_count;
   2699 
   2700 	/*
   2701 	 * Don't change "que->txrx_use_workqueue" from this point to avoid
   2702 	 * flip-flopping softint/workqueue mode in one deferred processing.
   2703 	 */
   2704 	que->txrx_use_workqueue = adapter->txrx_use_workqueue;
   2705 
   2706 #ifdef __NetBSD__
   2707 	/* Don't run ixgbe_rxeof in interrupt context */
   2708 	more = true;
   2709 #else
   2710 	more = ixgbe_rxeof(que);
   2711 #endif
   2712 
   2713 	IXGBE_TX_LOCK(txr);
   2714 	ixgbe_txeof(txr);
   2715 	IXGBE_TX_UNLOCK(txr);
   2716 
   2717 	/* Do AIM now? */
   2718 
   2719 	if (adapter->enable_aim == false)
   2720 		goto no_calc;
   2721 	/*
   2722 	 * Do Adaptive Interrupt Moderation:
   2723 	 *  - Write out last calculated setting
   2724 	 *  - Calculate based on average size over
   2725 	 *    the last interval.
   2726 	 */
   2727 	if (que->eitr_setting)
   2728 		ixgbe_eitr_write(adapter, que->msix, que->eitr_setting);
   2729 
   2730 	que->eitr_setting = 0;
   2731 
   2732 	/* Idle, do nothing */
   2733 	if ((txr->bytes == 0) && (rxr->bytes == 0))
   2734 		goto no_calc;
   2735 
   2736 	if ((txr->bytes) && (txr->packets))
   2737 		newitr = txr->bytes/txr->packets;
   2738 	if ((rxr->bytes) && (rxr->packets))
   2739 		newitr = max(newitr, (rxr->bytes / rxr->packets));
   2740 	newitr += 24; /* account for hardware frame, crc */
   2741 
   2742 	/* set an upper boundary */
   2743 	newitr = min(newitr, 3000);
   2744 
   2745 	/* Be nice to the mid range */
   2746 	if ((newitr > 300) && (newitr < 1200))
   2747 		newitr = (newitr / 3);
   2748 	else
   2749 		newitr = (newitr / 2);
   2750 
   2751 	/*
   2752 	 * When RSC is used, ITR interval must be larger than RSC_DELAY.
   2753 	 * Currently, we use 2us for RSC_DELAY. The minimum value is always
   2754 	 * greater than 2us on 100M (and 10M?(not documented)), but it's not
   2755 	 * on 1G and higher.
   2756 	 */
   2757 	if ((adapter->link_speed != IXGBE_LINK_SPEED_100_FULL)
   2758 	    && (adapter->link_speed != IXGBE_LINK_SPEED_10_FULL)) {
   2759 		if (newitr < IXGBE_MIN_RSC_EITR_10G1G)
   2760 			newitr = IXGBE_MIN_RSC_EITR_10G1G;
   2761 	}
   2762 
   2763 	/* save for next interrupt */
   2764 	que->eitr_setting = newitr;
   2765 
   2766 	/* Reset state */
   2767 	txr->bytes = 0;
   2768 	txr->packets = 0;
   2769 	rxr->bytes = 0;
   2770 	rxr->packets = 0;
   2771 
   2772 no_calc:
   2773 	if (more)
   2774 		ixgbe_sched_handle_que(adapter, que);
   2775 	else
   2776 		ixgbe_enable_queue(adapter, que->msix);
   2777 
   2778 	return 1;
   2779 } /* ixgbe_msix_que */
   2780 
   2781 /************************************************************************
   2782  * ixgbe_media_status - Media Ioctl callback
   2783  *
   2784  *   Called whenever the user queries the status of
   2785  *   the interface using ifconfig.
   2786  ************************************************************************/
   2787 static void
   2788 ixgbe_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   2789 {
   2790 	struct adapter *adapter = ifp->if_softc;
   2791 	struct ixgbe_hw *hw = &adapter->hw;
   2792 	int layer;
   2793 
   2794 	INIT_DEBUGOUT("ixgbe_media_status: begin");
   2795 	IXGBE_CORE_LOCK(adapter);
   2796 	ixgbe_update_link_status(adapter);
   2797 
   2798 	ifmr->ifm_status = IFM_AVALID;
   2799 	ifmr->ifm_active = IFM_ETHER;
   2800 
   2801 	if (adapter->link_active != LINK_STATE_UP) {
   2802 		ifmr->ifm_active |= IFM_NONE;
   2803 		IXGBE_CORE_UNLOCK(adapter);
   2804 		return;
   2805 	}
   2806 
   2807 	ifmr->ifm_status |= IFM_ACTIVE;
   2808 	layer = adapter->phy_layer;
   2809 
   2810 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T ||
   2811 	    layer & IXGBE_PHYSICAL_LAYER_5GBASE_T ||
   2812 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_T ||
   2813 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_T ||
   2814 	    layer & IXGBE_PHYSICAL_LAYER_100BASE_TX ||
   2815 	    layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
   2816 		switch (adapter->link_speed) {
   2817 		case IXGBE_LINK_SPEED_10GB_FULL:
   2818 			ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
   2819 			break;
   2820 		case IXGBE_LINK_SPEED_5GB_FULL:
   2821 			ifmr->ifm_active |= IFM_5000_T | IFM_FDX;
   2822 			break;
   2823 		case IXGBE_LINK_SPEED_2_5GB_FULL:
   2824 			ifmr->ifm_active |= IFM_2500_T | IFM_FDX;
   2825 			break;
   2826 		case IXGBE_LINK_SPEED_1GB_FULL:
   2827 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
   2828 			break;
   2829 		case IXGBE_LINK_SPEED_100_FULL:
   2830 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
   2831 			break;
   2832 		case IXGBE_LINK_SPEED_10_FULL:
   2833 			ifmr->ifm_active |= IFM_10_T | IFM_FDX;
   2834 			break;
   2835 		}
   2836 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
   2837 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
   2838 		switch (adapter->link_speed) {
   2839 		case IXGBE_LINK_SPEED_10GB_FULL:
   2840 			ifmr->ifm_active |= IFM_10G_TWINAX | IFM_FDX;
   2841 			break;
   2842 		}
   2843 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR)
   2844 		switch (adapter->link_speed) {
   2845 		case IXGBE_LINK_SPEED_10GB_FULL:
   2846 			ifmr->ifm_active |= IFM_10G_LR | IFM_FDX;
   2847 			break;
   2848 		case IXGBE_LINK_SPEED_1GB_FULL:
   2849 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
   2850 			break;
   2851 		}
   2852 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LRM)
   2853 		switch (adapter->link_speed) {
   2854 		case IXGBE_LINK_SPEED_10GB_FULL:
   2855 			ifmr->ifm_active |= IFM_10G_LRM | IFM_FDX;
   2856 			break;
   2857 		case IXGBE_LINK_SPEED_1GB_FULL:
   2858 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
   2859 			break;
   2860 		}
   2861 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR ||
   2862 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
   2863 		switch (adapter->link_speed) {
   2864 		case IXGBE_LINK_SPEED_10GB_FULL:
   2865 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
   2866 			break;
   2867 		case IXGBE_LINK_SPEED_1GB_FULL:
   2868 			ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
   2869 			break;
   2870 		}
   2871 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
   2872 		switch (adapter->link_speed) {
   2873 		case IXGBE_LINK_SPEED_10GB_FULL:
   2874 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
   2875 			break;
   2876 		}
   2877 	/*
   2878 	 * XXX: These need to use the proper media types once
   2879 	 * they're added.
   2880 	 */
   2881 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
   2882 		switch (adapter->link_speed) {
   2883 		case IXGBE_LINK_SPEED_10GB_FULL:
   2884 #ifndef IFM_ETH_XTYPE
   2885 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
   2886 #else
   2887 			ifmr->ifm_active |= IFM_10G_KR | IFM_FDX;
   2888 #endif
   2889 			break;
   2890 		case IXGBE_LINK_SPEED_2_5GB_FULL:
   2891 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
   2892 			break;
   2893 		case IXGBE_LINK_SPEED_1GB_FULL:
   2894 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
   2895 			break;
   2896 		}
   2897 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
   2898 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
   2899 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
   2900 		switch (adapter->link_speed) {
   2901 		case IXGBE_LINK_SPEED_10GB_FULL:
   2902 #ifndef IFM_ETH_XTYPE
   2903 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
   2904 #else
   2905 			ifmr->ifm_active |= IFM_10G_KX4 | IFM_FDX;
   2906 #endif
   2907 			break;
   2908 		case IXGBE_LINK_SPEED_2_5GB_FULL:
   2909 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
   2910 			break;
   2911 		case IXGBE_LINK_SPEED_1GB_FULL:
   2912 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
   2913 			break;
   2914 		}
   2915 
   2916 	/* If nothing is recognized... */
   2917 #if 0
   2918 	if (IFM_SUBTYPE(ifmr->ifm_active) == 0)
   2919 		ifmr->ifm_active |= IFM_UNKNOWN;
   2920 #endif
   2921 
   2922 	ifp->if_baudrate = ifmedia_baudrate(ifmr->ifm_active);
   2923 
   2924 	/* Display current flow control setting used on link */
   2925 	if (hw->fc.current_mode == ixgbe_fc_rx_pause ||
   2926 	    hw->fc.current_mode == ixgbe_fc_full)
   2927 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
   2928 	if (hw->fc.current_mode == ixgbe_fc_tx_pause ||
   2929 	    hw->fc.current_mode == ixgbe_fc_full)
   2930 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
   2931 
   2932 	IXGBE_CORE_UNLOCK(adapter);
   2933 
   2934 	return;
   2935 } /* ixgbe_media_status */
   2936 
   2937 /************************************************************************
   2938  * ixgbe_media_change - Media Ioctl callback
   2939  *
   2940  *   Called when the user changes speed/duplex using
   2941  *   media/mediopt option with ifconfig.
   2942  ************************************************************************/
   2943 static int
   2944 ixgbe_media_change(struct ifnet *ifp)
   2945 {
   2946 	struct adapter	 *adapter = ifp->if_softc;
   2947 	struct ifmedia	 *ifm = &adapter->media;
   2948 	struct ixgbe_hw	 *hw = &adapter->hw;
   2949 	ixgbe_link_speed speed = 0;
   2950 	ixgbe_link_speed link_caps = 0;
   2951 	bool negotiate = false;
   2952 	s32 err = IXGBE_NOT_IMPLEMENTED;
   2953 
   2954 	INIT_DEBUGOUT("ixgbe_media_change: begin");
   2955 
   2956 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   2957 		return (EINVAL);
   2958 
   2959 	if (hw->phy.media_type == ixgbe_media_type_backplane)
   2960 		return (EPERM);
   2961 
   2962 	IXGBE_CORE_LOCK(adapter);
   2963 	/*
   2964 	 * We don't actually need to check against the supported
   2965 	 * media types of the adapter; ifmedia will take care of
   2966 	 * that for us.
   2967 	 */
   2968 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
   2969 	case IFM_AUTO:
   2970 		err = hw->mac.ops.get_link_capabilities(hw, &link_caps,
   2971 		    &negotiate);
   2972 		if (err != IXGBE_SUCCESS) {
   2973 			device_printf(adapter->dev, "Unable to determine "
   2974 			    "supported advertise speeds\n");
   2975 			IXGBE_CORE_UNLOCK(adapter);
   2976 			return (ENODEV);
   2977 		}
   2978 		speed |= link_caps;
   2979 		break;
   2980 	case IFM_10G_T:
   2981 	case IFM_10G_LRM:
   2982 	case IFM_10G_LR:
   2983 	case IFM_10G_TWINAX:
   2984 	case IFM_10G_SR:
   2985 	case IFM_10G_CX4:
   2986 #ifdef IFM_ETH_XTYPE
   2987 	case IFM_10G_KR:
   2988 	case IFM_10G_KX4:
   2989 #endif
   2990 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
   2991 		break;
   2992 	case IFM_5000_T:
   2993 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
   2994 		break;
   2995 	case IFM_2500_T:
   2996 	case IFM_2500_KX:
   2997 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
   2998 		break;
   2999 	case IFM_1000_T:
   3000 	case IFM_1000_LX:
   3001 	case IFM_1000_SX:
   3002 	case IFM_1000_KX:
   3003 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
   3004 		break;
   3005 	case IFM_100_TX:
   3006 		speed |= IXGBE_LINK_SPEED_100_FULL;
   3007 		break;
   3008 	case IFM_10_T:
   3009 		speed |= IXGBE_LINK_SPEED_10_FULL;
   3010 		break;
   3011 	case IFM_NONE:
   3012 		break;
   3013 	default:
   3014 		goto invalid;
   3015 	}
   3016 
   3017 	hw->mac.autotry_restart = TRUE;
   3018 	hw->mac.ops.setup_link(hw, speed, TRUE);
   3019 	adapter->advertise = 0;
   3020 	if (IFM_SUBTYPE(ifm->ifm_media) != IFM_AUTO) {
   3021 		if ((speed & IXGBE_LINK_SPEED_10GB_FULL) != 0)
   3022 			adapter->advertise |= 1 << 2;
   3023 		if ((speed & IXGBE_LINK_SPEED_1GB_FULL) != 0)
   3024 			adapter->advertise |= 1 << 1;
   3025 		if ((speed & IXGBE_LINK_SPEED_100_FULL) != 0)
   3026 			adapter->advertise |= 1 << 0;
   3027 		if ((speed & IXGBE_LINK_SPEED_10_FULL) != 0)
   3028 			adapter->advertise |= 1 << 3;
   3029 		if ((speed & IXGBE_LINK_SPEED_2_5GB_FULL) != 0)
   3030 			adapter->advertise |= 1 << 4;
   3031 		if ((speed & IXGBE_LINK_SPEED_5GB_FULL) != 0)
   3032 			adapter->advertise |= 1 << 5;
   3033 	}
   3034 
   3035 	IXGBE_CORE_UNLOCK(adapter);
   3036 	return (0);
   3037 
   3038 invalid:
   3039 	device_printf(adapter->dev, "Invalid media type!\n");
   3040 	IXGBE_CORE_UNLOCK(adapter);
   3041 
   3042 	return (EINVAL);
   3043 } /* ixgbe_media_change */
   3044 
   3045 /************************************************************************
   3046  * ixgbe_msix_link - Link status change ISR (MSI/MSI-X)
   3047  ************************************************************************/
   3048 static int
   3049 ixgbe_msix_link(void *arg)
   3050 {
   3051 	struct adapter	*adapter = arg;
   3052 	struct ixgbe_hw *hw = &adapter->hw;
   3053 	u32		eicr, eicr_mask;
   3054 	s32		retval;
   3055 
   3056 	++adapter->link_irq.ev_count;
   3057 
   3058 	/* Pause other interrupts */
   3059 	IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_OTHER);
   3060 
   3061 	/* First get the cause */
   3062 	/*
   3063 	 * The specifications of 82598, 82599, X540 and X550 say EICS register
   3064 	 * is write only. However, Linux says it is a workaround for silicon
   3065 	 * errata to read EICS instead of EICR to get interrupt cause. It seems
   3066 	 * there is a problem about read clear mechanism for EICR register.
   3067 	 */
   3068 	eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
   3069 	/* Be sure the queue bits are not cleared */
   3070 	eicr &= ~IXGBE_EICR_RTX_QUEUE;
   3071 	/* Clear interrupt with write */
   3072 	IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
   3073 
   3074 	if (ixgbe_is_sfp(hw)) {
   3075 		/* Pluggable optics-related interrupt */
   3076 		if (hw->mac.type >= ixgbe_mac_X540)
   3077 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
   3078 		else
   3079 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
   3080 
   3081 		/*
   3082 		 *  An interrupt might not arrive when a module is inserted.
   3083 		 * When an link status change interrupt occurred and the driver
   3084 		 * still regard SFP as unplugged, issue the module softint
   3085 		 * and then issue LSC interrupt.
   3086 		 */
   3087 		if ((eicr & eicr_mask)
   3088 		    || ((hw->phy.sfp_type == ixgbe_sfp_type_not_present)
   3089 			&& (eicr & IXGBE_EICR_LSC))) {
   3090 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
   3091 			softint_schedule(adapter->mod_si);
   3092 		}
   3093 
   3094 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
   3095 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
   3096 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
   3097 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
   3098 			softint_schedule(adapter->msf_si);
   3099 		}
   3100 	}
   3101 
   3102 	/* Link status change */
   3103 	if (eicr & IXGBE_EICR_LSC) {
   3104 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
   3105 		softint_schedule(adapter->link_si);
   3106 	}
   3107 
   3108 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
   3109 		if ((adapter->feat_en & IXGBE_FEATURE_FDIR) &&
   3110 		    (eicr & IXGBE_EICR_FLOW_DIR)) {
   3111 			/* This is probably overkill :) */
   3112 			if (!atomic_cas_uint(&adapter->fdir_reinit, 0, 1))
   3113 				return 1;
   3114 			/* Disable the interrupt */
   3115 			IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_FLOW_DIR);
   3116 			softint_schedule(adapter->fdir_si);
   3117 		}
   3118 
   3119 		if (eicr & IXGBE_EICR_ECC) {
   3120 			device_printf(adapter->dev,
   3121 			    "CRITICAL: ECC ERROR!! Please Reboot!!\n");
   3122 			IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
   3123 		}
   3124 
   3125 		/* Check for over temp condition */
   3126 		if (adapter->feat_en & IXGBE_FEATURE_TEMP_SENSOR) {
   3127 			switch (adapter->hw.mac.type) {
   3128 			case ixgbe_mac_X550EM_a:
   3129 				if (!(eicr & IXGBE_EICR_GPI_SDP0_X550EM_a))
   3130 					break;
   3131 				IXGBE_WRITE_REG(hw, IXGBE_EIMC,
   3132 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
   3133 				IXGBE_WRITE_REG(hw, IXGBE_EICR,
   3134 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
   3135 				retval = hw->phy.ops.check_overtemp(hw);
   3136 				if (retval != IXGBE_ERR_OVERTEMP)
   3137 					break;
   3138 				device_printf(adapter->dev,
   3139 				    "CRITICAL: OVER TEMP!! "
   3140 				    "PHY IS SHUT DOWN!!\n");
   3141 				device_printf(adapter->dev,
   3142 				    "System shutdown required!\n");
   3143 				break;
   3144 			default:
   3145 				if (!(eicr & IXGBE_EICR_TS))
   3146 					break;
   3147 				retval = hw->phy.ops.check_overtemp(hw);
   3148 				if (retval != IXGBE_ERR_OVERTEMP)
   3149 					break;
   3150 				device_printf(adapter->dev, "CRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
   3151 				device_printf(adapter->dev, "System shutdown required!\n");
   3152 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
   3153 				break;
   3154 			}
   3155 		}
   3156 
   3157 		/* Check for VF message */
   3158 		if ((adapter->feat_en & IXGBE_FEATURE_SRIOV) &&
   3159 		    (eicr & IXGBE_EICR_MAILBOX))
   3160 			softint_schedule(adapter->mbx_si);
   3161 	}
   3162 
   3163 	/* Check for fan failure */
   3164 	if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL) {
   3165 		ixgbe_check_fan_failure(adapter, eicr, TRUE);
   3166 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
   3167 	}
   3168 
   3169 	/* External PHY interrupt */
   3170 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
   3171 	    (eicr & IXGBE_EICR_GPI_SDP0_X540)) {
   3172 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP0_X540);
   3173 		softint_schedule(adapter->phy_si);
   3174 	}
   3175 
   3176 	/* Re-enable other interrupts */
   3177 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
   3178 	return 1;
   3179 } /* ixgbe_msix_link */
   3180 
   3181 static void
   3182 ixgbe_eitr_write(struct adapter *adapter, uint32_t index, uint32_t itr)
   3183 {
   3184 
   3185 	if (adapter->hw.mac.type == ixgbe_mac_82598EB)
   3186 		itr |= itr << 16;
   3187 	else
   3188 		itr |= IXGBE_EITR_CNT_WDIS;
   3189 
   3190 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(index), itr);
   3191 }
   3192 
   3193 
   3194 /************************************************************************
   3195  * ixgbe_sysctl_interrupt_rate_handler
   3196  ************************************************************************/
   3197 static int
   3198 ixgbe_sysctl_interrupt_rate_handler(SYSCTLFN_ARGS)
   3199 {
   3200 	struct sysctlnode node = *rnode;
   3201 	struct ix_queue *que = (struct ix_queue *)node.sysctl_data;
   3202 	struct adapter	*adapter;
   3203 	uint32_t reg, usec, rate;
   3204 	int error;
   3205 
   3206 	if (que == NULL)
   3207 		return 0;
   3208 
   3209 	adapter = que->adapter;
   3210 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   3211 		return (EPERM);
   3212 
   3213 	reg = IXGBE_READ_REG(&adapter->hw, IXGBE_EITR(que->msix));
   3214 	usec = ((reg & 0x0FF8) >> 3);
   3215 	if (usec > 0)
   3216 		rate = 500000 / usec;
   3217 	else
   3218 		rate = 0;
   3219 	node.sysctl_data = &rate;
   3220 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   3221 	if (error || newp == NULL)
   3222 		return error;
   3223 	reg &= ~0xfff; /* default, no limitation */
   3224 	if (rate > 0 && rate < 500000) {
   3225 		if (rate < 1000)
   3226 			rate = 1000;
   3227 		reg |= ((4000000 / rate) & 0xff8);
   3228 		/*
   3229 		 * When RSC is used, ITR interval must be larger than
   3230 		 * RSC_DELAY. Currently, we use 2us for RSC_DELAY.
   3231 		 * The minimum value is always greater than 2us on 100M
   3232 		 * (and 10M?(not documented)), but it's not on 1G and higher.
   3233 		 */
   3234 		if ((adapter->link_speed != IXGBE_LINK_SPEED_100_FULL)
   3235 		    && (adapter->link_speed != IXGBE_LINK_SPEED_10_FULL)) {
   3236 			if ((adapter->num_queues > 1)
   3237 			    && (reg < IXGBE_MIN_RSC_EITR_10G1G))
   3238 				return EINVAL;
   3239 		}
   3240 		ixgbe_max_interrupt_rate = rate;
   3241 	} else
   3242 		ixgbe_max_interrupt_rate = 0;
   3243 	ixgbe_eitr_write(adapter, que->msix, reg);
   3244 
   3245 	return (0);
   3246 } /* ixgbe_sysctl_interrupt_rate_handler */
   3247 
   3248 const struct sysctlnode *
   3249 ixgbe_sysctl_instance(struct adapter *adapter)
   3250 {
   3251 	const char *dvname;
   3252 	struct sysctllog **log;
   3253 	int rc;
   3254 	const struct sysctlnode *rnode;
   3255 
   3256 	if (adapter->sysctltop != NULL)
   3257 		return adapter->sysctltop;
   3258 
   3259 	log = &adapter->sysctllog;
   3260 	dvname = device_xname(adapter->dev);
   3261 
   3262 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
   3263 	    0, CTLTYPE_NODE, dvname,
   3264 	    SYSCTL_DESCR("ixgbe information and settings"),
   3265 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
   3266 		goto err;
   3267 
   3268 	return rnode;
   3269 err:
   3270 	device_printf(adapter->dev,
   3271 	    "%s: sysctl_createv failed, rc = %d\n", __func__, rc);
   3272 	return NULL;
   3273 }
   3274 
   3275 /************************************************************************
   3276  * ixgbe_add_device_sysctls
   3277  ************************************************************************/
   3278 static void
   3279 ixgbe_add_device_sysctls(struct adapter *adapter)
   3280 {
   3281 	device_t	       dev = adapter->dev;
   3282 	struct ixgbe_hw	       *hw = &adapter->hw;
   3283 	struct sysctllog **log;
   3284 	const struct sysctlnode *rnode, *cnode;
   3285 
   3286 	log = &adapter->sysctllog;
   3287 
   3288 	if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   3289 		aprint_error_dev(dev, "could not create sysctl root\n");
   3290 		return;
   3291 	}
   3292 
   3293 	if (sysctl_createv(log, 0, &rnode, &cnode,
   3294 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   3295 	    "debug", SYSCTL_DESCR("Debug Info"),
   3296 	    ixgbe_sysctl_debug, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL)
   3297 	    != 0)
   3298 		aprint_error_dev(dev, "could not create sysctl\n");
   3299 
   3300 	if (sysctl_createv(log, 0, &rnode, &cnode,
   3301 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   3302 	    "rx_copy_len", SYSCTL_DESCR("RX Copy Length"),
   3303 	    ixgbe_sysctl_rx_copy_len, 0,
   3304 	    (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   3305 		aprint_error_dev(dev, "could not create sysctl\n");
   3306 
   3307 	if (sysctl_createv(log, 0, &rnode, &cnode,
   3308 	    CTLFLAG_READONLY, CTLTYPE_INT,
   3309 	    "num_rx_desc", SYSCTL_DESCR("Number of rx descriptors"),
   3310 	    NULL, 0, &adapter->num_rx_desc, 0, CTL_CREATE, CTL_EOL) != 0)
   3311 		aprint_error_dev(dev, "could not create sysctl\n");
   3312 
   3313 	if (sysctl_createv(log, 0, &rnode, &cnode,
   3314 	    CTLFLAG_READONLY, CTLTYPE_INT,
   3315 	    "num_queues", SYSCTL_DESCR("Number of queues"),
   3316 	    NULL, 0, &adapter->num_queues, 0, CTL_CREATE, CTL_EOL) != 0)
   3317 		aprint_error_dev(dev, "could not create sysctl\n");
   3318 
   3319 	/* Sysctls for all devices */
   3320 	if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   3321 	    CTLTYPE_INT, "fc", SYSCTL_DESCR(IXGBE_SYSCTL_DESC_SET_FC),
   3322 	    ixgbe_sysctl_flowcntl, 0, (void *)adapter, 0, CTL_CREATE,
   3323 	    CTL_EOL) != 0)
   3324 		aprint_error_dev(dev, "could not create sysctl\n");
   3325 
   3326 	adapter->enable_aim = ixgbe_enable_aim;
   3327 	if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   3328 	    CTLTYPE_BOOL, "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
   3329 	    NULL, 0, &adapter->enable_aim, 0, CTL_CREATE, CTL_EOL) != 0)
   3330 		aprint_error_dev(dev, "could not create sysctl\n");
   3331 
   3332 	if (sysctl_createv(log, 0, &rnode, &cnode,
   3333 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   3334 	    "advertise_speed", SYSCTL_DESCR(IXGBE_SYSCTL_DESC_ADV_SPEED),
   3335 	    ixgbe_sysctl_advertise, 0, (void *)adapter, 0, CTL_CREATE,
   3336 	    CTL_EOL) != 0)
   3337 		aprint_error_dev(dev, "could not create sysctl\n");
   3338 
   3339 	/*
   3340 	 * If each "que->txrx_use_workqueue" is changed in sysctl handler,
   3341 	 * it causesflip-flopping softint/workqueue mode in one deferred
   3342 	 * processing. Therefore, preempt_disable()/preempt_enable() are
   3343 	 * required in ixgbe_sched_handle_que() to avoid
   3344 	 * KASSERT(ixgbe_sched_handle_que()) in softint_schedule().
   3345 	 * I think changing "que->txrx_use_workqueue" in interrupt handler
   3346 	 * is lighter than doing preempt_disable()/preempt_enable() in every
   3347 	 * ixgbe_sched_handle_que().
   3348 	 */
   3349 	adapter->txrx_use_workqueue = ixgbe_txrx_workqueue;
   3350 	if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   3351 	    CTLTYPE_BOOL, "txrx_workqueue",
   3352 	    SYSCTL_DESCR("Use workqueue for packet processing"),
   3353 	    NULL, 0, &adapter->txrx_use_workqueue, 0, CTL_CREATE,
   3354 	    CTL_EOL) != 0)
   3355 		aprint_error_dev(dev, "could not create sysctl\n");
   3356 
   3357 #ifdef IXGBE_DEBUG
   3358 	/* testing sysctls (for all devices) */
   3359 	if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   3360 	    CTLTYPE_INT, "power_state", SYSCTL_DESCR("PCI Power State"),
   3361 	    ixgbe_sysctl_power_state, 0, (void *)adapter, 0, CTL_CREATE,
   3362 	    CTL_EOL) != 0)
   3363 		aprint_error_dev(dev, "could not create sysctl\n");
   3364 
   3365 	if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READONLY,
   3366 	    CTLTYPE_STRING, "print_rss_config",
   3367 	    SYSCTL_DESCR("Prints RSS Configuration"),
   3368 	    ixgbe_sysctl_print_rss_config, 0, (void *)adapter, 0, CTL_CREATE,
   3369 	    CTL_EOL) != 0)
   3370 		aprint_error_dev(dev, "could not create sysctl\n");
   3371 #endif
   3372 	/* for X550 series devices */
   3373 	if (hw->mac.type >= ixgbe_mac_X550)
   3374 		if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   3375 		    CTLTYPE_INT, "dmac", SYSCTL_DESCR("DMA Coalesce"),
   3376 		    ixgbe_sysctl_dmac, 0, (void *)adapter, 0, CTL_CREATE,
   3377 		    CTL_EOL) != 0)
   3378 			aprint_error_dev(dev, "could not create sysctl\n");
   3379 
   3380 	/* for WoL-capable devices */
   3381 	if (adapter->wol_support) {
   3382 		if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   3383 		    CTLTYPE_BOOL, "wol_enable",
   3384 		    SYSCTL_DESCR("Enable/Disable Wake on LAN"),
   3385 		    ixgbe_sysctl_wol_enable, 0, (void *)adapter, 0, CTL_CREATE,
   3386 		    CTL_EOL) != 0)
   3387 			aprint_error_dev(dev, "could not create sysctl\n");
   3388 
   3389 		if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   3390 		    CTLTYPE_INT, "wufc",
   3391 		    SYSCTL_DESCR("Enable/Disable Wake Up Filters"),
   3392 		    ixgbe_sysctl_wufc, 0, (void *)adapter, 0, CTL_CREATE,
   3393 		    CTL_EOL) != 0)
   3394 			aprint_error_dev(dev, "could not create sysctl\n");
   3395 	}
   3396 
   3397 	/* for X552/X557-AT devices */
   3398 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
   3399 		const struct sysctlnode *phy_node;
   3400 
   3401 		if (sysctl_createv(log, 0, &rnode, &phy_node, 0, CTLTYPE_NODE,
   3402 		    "phy", SYSCTL_DESCR("External PHY sysctls"),
   3403 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0) {
   3404 			aprint_error_dev(dev, "could not create sysctl\n");
   3405 			return;
   3406 		}
   3407 
   3408 		if (sysctl_createv(log, 0, &phy_node, &cnode, CTLFLAG_READONLY,
   3409 		    CTLTYPE_INT, "temp",
   3410 		    SYSCTL_DESCR("Current External PHY Temperature (Celsius)"),
   3411 		    ixgbe_sysctl_phy_temp, 0, (void *)adapter, 0, CTL_CREATE,
   3412 		    CTL_EOL) != 0)
   3413 			aprint_error_dev(dev, "could not create sysctl\n");
   3414 
   3415 		if (sysctl_createv(log, 0, &phy_node, &cnode, CTLFLAG_READONLY,
   3416 		    CTLTYPE_INT, "overtemp_occurred",
   3417 		    SYSCTL_DESCR(
   3418 			    "External PHY High Temperature Event Occurred"),
   3419 		    ixgbe_sysctl_phy_overtemp_occurred, 0, (void *)adapter, 0,
   3420 		    CTL_CREATE, CTL_EOL) != 0)
   3421 			aprint_error_dev(dev, "could not create sysctl\n");
   3422 	}
   3423 
   3424 	if ((hw->mac.type == ixgbe_mac_X550EM_a)
   3425 	    && (hw->phy.type == ixgbe_phy_fw))
   3426 		if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   3427 		    CTLTYPE_BOOL, "force_10_100_autonego",
   3428 		    SYSCTL_DESCR("Force autonego on 10M and 100M"),
   3429 		    NULL, 0, &hw->phy.force_10_100_autonego, 0,
   3430 		    CTL_CREATE, CTL_EOL) != 0)
   3431 			aprint_error_dev(dev, "could not create sysctl\n");
   3432 
   3433 	if (adapter->feat_cap & IXGBE_FEATURE_EEE) {
   3434 		if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   3435 		    CTLTYPE_INT, "eee_state",
   3436 		    SYSCTL_DESCR("EEE Power Save State"),
   3437 		    ixgbe_sysctl_eee_state, 0, (void *)adapter, 0, CTL_CREATE,
   3438 		    CTL_EOL) != 0)
   3439 			aprint_error_dev(dev, "could not create sysctl\n");
   3440 	}
   3441 } /* ixgbe_add_device_sysctls */
   3442 
   3443 /************************************************************************
   3444  * ixgbe_allocate_pci_resources
   3445  ************************************************************************/
   3446 static int
   3447 ixgbe_allocate_pci_resources(struct adapter *adapter,
   3448     const struct pci_attach_args *pa)
   3449 {
   3450 	pcireg_t	memtype, csr;
   3451 	device_t dev = adapter->dev;
   3452 	bus_addr_t addr;
   3453 	int flags;
   3454 
   3455 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
   3456 	switch (memtype) {
   3457 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   3458 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   3459 		adapter->osdep.mem_bus_space_tag = pa->pa_memt;
   3460 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
   3461 		      memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
   3462 			goto map_err;
   3463 		if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
   3464 			aprint_normal_dev(dev, "clearing prefetchable bit\n");
   3465 			flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
   3466 		}
   3467 		if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
   3468 		     adapter->osdep.mem_size, flags,
   3469 		     &adapter->osdep.mem_bus_space_handle) != 0) {
   3470 map_err:
   3471 			adapter->osdep.mem_size = 0;
   3472 			aprint_error_dev(dev, "unable to map BAR0\n");
   3473 			return ENXIO;
   3474 		}
   3475 		/*
   3476 		 * Enable address decoding for memory range in case BIOS or
   3477 		 * UEFI don't set it.
   3478 		 */
   3479 		csr = pci_conf_read(pa->pa_pc, pa->pa_tag,
   3480 		    PCI_COMMAND_STATUS_REG);
   3481 		csr |= PCI_COMMAND_MEM_ENABLE;
   3482 		pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
   3483 		    csr);
   3484 		break;
   3485 	default:
   3486 		aprint_error_dev(dev, "unexpected type on BAR0\n");
   3487 		return ENXIO;
   3488 	}
   3489 
   3490 	return (0);
   3491 } /* ixgbe_allocate_pci_resources */
   3492 
   3493 static void
   3494 ixgbe_free_softint(struct adapter *adapter)
   3495 {
   3496 	struct ix_queue *que = adapter->queues;
   3497 	struct tx_ring *txr = adapter->tx_rings;
   3498 	int i;
   3499 
   3500 	for (i = 0; i < adapter->num_queues; i++, que++, txr++) {
   3501 		if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX)) {
   3502 			if (txr->txr_si != NULL)
   3503 				softint_disestablish(txr->txr_si);
   3504 		}
   3505 		if (que->que_si != NULL)
   3506 			softint_disestablish(que->que_si);
   3507 	}
   3508 	if (adapter->txr_wq != NULL)
   3509 		workqueue_destroy(adapter->txr_wq);
   3510 	if (adapter->txr_wq_enqueued != NULL)
   3511 		percpu_free(adapter->txr_wq_enqueued, sizeof(u_int));
   3512 	if (adapter->que_wq != NULL)
   3513 		workqueue_destroy(adapter->que_wq);
   3514 
   3515 	/* Drain the Link queue */
   3516 	if (adapter->link_si != NULL) {
   3517 		softint_disestablish(adapter->link_si);
   3518 		adapter->link_si = NULL;
   3519 	}
   3520 	if (adapter->mod_si != NULL) {
   3521 		softint_disestablish(adapter->mod_si);
   3522 		adapter->mod_si = NULL;
   3523 	}
   3524 	if (adapter->msf_si != NULL) {
   3525 		softint_disestablish(adapter->msf_si);
   3526 		adapter->msf_si = NULL;
   3527 	}
   3528 	if (adapter->phy_si != NULL) {
   3529 		softint_disestablish(adapter->phy_si);
   3530 		adapter->phy_si = NULL;
   3531 	}
   3532 	if (adapter->feat_en & IXGBE_FEATURE_FDIR) {
   3533 		if (adapter->fdir_si != NULL) {
   3534 			softint_disestablish(adapter->fdir_si);
   3535 			adapter->fdir_si = NULL;
   3536 		}
   3537 	}
   3538 	if (adapter->feat_cap & IXGBE_FEATURE_SRIOV) {
   3539 		if (adapter->mbx_si != NULL) {
   3540 			softint_disestablish(adapter->mbx_si);
   3541 			adapter->mbx_si = NULL;
   3542 		}
   3543 	}
   3544 } /* ixgbe_free_softint */
   3545 
   3546 /************************************************************************
   3547  * ixgbe_detach - Device removal routine
   3548  *
   3549  *   Called when the driver is being removed.
   3550  *   Stops the adapter and deallocates all the resources
   3551  *   that were allocated for driver operation.
   3552  *
   3553  *   return 0 on success, positive on failure
   3554  ************************************************************************/
   3555 static int
   3556 ixgbe_detach(device_t dev, int flags)
   3557 {
   3558 	struct adapter *adapter = device_private(dev);
   3559 	struct rx_ring *rxr = adapter->rx_rings;
   3560 	struct tx_ring *txr = adapter->tx_rings;
   3561 	struct ixgbe_hw *hw = &adapter->hw;
   3562 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
   3563 	u32	ctrl_ext;
   3564 	int i;
   3565 
   3566 	INIT_DEBUGOUT("ixgbe_detach: begin");
   3567 	if (adapter->osdep.attached == false)
   3568 		return 0;
   3569 
   3570 	if (ixgbe_pci_iov_detach(dev) != 0) {
   3571 		device_printf(dev, "SR-IOV in use; detach first.\n");
   3572 		return (EBUSY);
   3573 	}
   3574 
   3575 	/*
   3576 	 * Stop the interface. ixgbe_setup_low_power_mode() calls
   3577 	 * ixgbe_stop_locked(), so it's not required to call ixgbe_stop_locked()
   3578 	 * directly.
   3579 	 */
   3580 	IXGBE_CORE_LOCK(adapter);
   3581 	ixgbe_setup_low_power_mode(adapter);
   3582 	IXGBE_CORE_UNLOCK(adapter);
   3583 #if NVLAN > 0
   3584 	/* Make sure VLANs are not using driver */
   3585 	if (!VLAN_ATTACHED(&adapter->osdep.ec))
   3586 		;	/* nothing to do: no VLANs */
   3587 	else if ((flags & (DETACH_SHUTDOWN | DETACH_FORCE)) != 0)
   3588 		vlan_ifdetach(adapter->ifp);
   3589 	else {
   3590 		aprint_error_dev(dev, "VLANs in use, detach first\n");
   3591 		return (EBUSY);
   3592 	}
   3593 #endif
   3594 
   3595 	pmf_device_deregister(dev);
   3596 
   3597 	ether_ifdetach(adapter->ifp);
   3598 
   3599 	ixgbe_free_softint(adapter);
   3600 
   3601 	/* let hardware know driver is unloading */
   3602 	ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
   3603 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
   3604 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT, ctrl_ext);
   3605 
   3606 	callout_halt(&adapter->timer, NULL);
   3607 	if (adapter->feat_en & IXGBE_FEATURE_RECOVERY_MODE)
   3608 		callout_halt(&adapter->recovery_mode_timer, NULL);
   3609 
   3610 	if (adapter->feat_en & IXGBE_FEATURE_NETMAP)
   3611 		netmap_detach(adapter->ifp);
   3612 
   3613 	ixgbe_free_pci_resources(adapter);
   3614 #if 0	/* XXX the NetBSD port is probably missing something here */
   3615 	bus_generic_detach(dev);
   3616 #endif
   3617 	if_detach(adapter->ifp);
   3618 	if_percpuq_destroy(adapter->ipq);
   3619 
   3620 	sysctl_teardown(&adapter->sysctllog);
   3621 	evcnt_detach(&adapter->efbig_tx_dma_setup);
   3622 	evcnt_detach(&adapter->mbuf_defrag_failed);
   3623 	evcnt_detach(&adapter->efbig2_tx_dma_setup);
   3624 	evcnt_detach(&adapter->einval_tx_dma_setup);
   3625 	evcnt_detach(&adapter->other_tx_dma_setup);
   3626 	evcnt_detach(&adapter->eagain_tx_dma_setup);
   3627 	evcnt_detach(&adapter->enomem_tx_dma_setup);
   3628 	evcnt_detach(&adapter->watchdog_events);
   3629 	evcnt_detach(&adapter->tso_err);
   3630 	evcnt_detach(&adapter->link_irq);
   3631 	evcnt_detach(&adapter->link_sicount);
   3632 	evcnt_detach(&adapter->mod_sicount);
   3633 	evcnt_detach(&adapter->msf_sicount);
   3634 	evcnt_detach(&adapter->phy_sicount);
   3635 
   3636 	for (i = 0; i < IXGBE_TC_COUNTER_NUM; i++) {
   3637 		if (i < __arraycount(stats->mpc)) {
   3638 			evcnt_detach(&stats->mpc[i]);
   3639 			if (hw->mac.type == ixgbe_mac_82598EB)
   3640 				evcnt_detach(&stats->rnbc[i]);
   3641 		}
   3642 		if (i < __arraycount(stats->pxontxc)) {
   3643 			evcnt_detach(&stats->pxontxc[i]);
   3644 			evcnt_detach(&stats->pxonrxc[i]);
   3645 			evcnt_detach(&stats->pxofftxc[i]);
   3646 			evcnt_detach(&stats->pxoffrxc[i]);
   3647 			if (hw->mac.type >= ixgbe_mac_82599EB)
   3648 				evcnt_detach(&stats->pxon2offc[i]);
   3649 		}
   3650 	}
   3651 
   3652 	txr = adapter->tx_rings;
   3653 	for (i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   3654 		evcnt_detach(&adapter->queues[i].irqs);
   3655 		evcnt_detach(&adapter->queues[i].handleq);
   3656 		evcnt_detach(&adapter->queues[i].req);
   3657 		evcnt_detach(&txr->no_desc_avail);
   3658 		evcnt_detach(&txr->total_packets);
   3659 		evcnt_detach(&txr->tso_tx);
   3660 #ifndef IXGBE_LEGACY_TX
   3661 		evcnt_detach(&txr->pcq_drops);
   3662 #endif
   3663 
   3664 		if (i < __arraycount(stats->qprc)) {
   3665 			evcnt_detach(&stats->qprc[i]);
   3666 			evcnt_detach(&stats->qptc[i]);
   3667 			evcnt_detach(&stats->qbrc[i]);
   3668 			evcnt_detach(&stats->qbtc[i]);
   3669 			if (hw->mac.type >= ixgbe_mac_82599EB)
   3670 				evcnt_detach(&stats->qprdc[i]);
   3671 		}
   3672 
   3673 		evcnt_detach(&rxr->rx_packets);
   3674 		evcnt_detach(&rxr->rx_bytes);
   3675 		evcnt_detach(&rxr->rx_copies);
   3676 		evcnt_detach(&rxr->no_mbuf);
   3677 		evcnt_detach(&rxr->rx_discarded);
   3678 	}
   3679 	evcnt_detach(&stats->ipcs);
   3680 	evcnt_detach(&stats->l4cs);
   3681 	evcnt_detach(&stats->ipcs_bad);
   3682 	evcnt_detach(&stats->l4cs_bad);
   3683 	evcnt_detach(&stats->intzero);
   3684 	evcnt_detach(&stats->legint);
   3685 	evcnt_detach(&stats->crcerrs);
   3686 	evcnt_detach(&stats->illerrc);
   3687 	evcnt_detach(&stats->errbc);
   3688 	evcnt_detach(&stats->mspdc);
   3689 	if (hw->mac.type >= ixgbe_mac_X550)
   3690 		evcnt_detach(&stats->mbsdc);
   3691 	evcnt_detach(&stats->mpctotal);
   3692 	evcnt_detach(&stats->mlfc);
   3693 	evcnt_detach(&stats->mrfc);
   3694 	evcnt_detach(&stats->rlec);
   3695 	evcnt_detach(&stats->lxontxc);
   3696 	evcnt_detach(&stats->lxonrxc);
   3697 	evcnt_detach(&stats->lxofftxc);
   3698 	evcnt_detach(&stats->lxoffrxc);
   3699 
   3700 	/* Packet Reception Stats */
   3701 	evcnt_detach(&stats->tor);
   3702 	evcnt_detach(&stats->gorc);
   3703 	evcnt_detach(&stats->tpr);
   3704 	evcnt_detach(&stats->gprc);
   3705 	evcnt_detach(&stats->mprc);
   3706 	evcnt_detach(&stats->bprc);
   3707 	evcnt_detach(&stats->prc64);
   3708 	evcnt_detach(&stats->prc127);
   3709 	evcnt_detach(&stats->prc255);
   3710 	evcnt_detach(&stats->prc511);
   3711 	evcnt_detach(&stats->prc1023);
   3712 	evcnt_detach(&stats->prc1522);
   3713 	evcnt_detach(&stats->ruc);
   3714 	evcnt_detach(&stats->rfc);
   3715 	evcnt_detach(&stats->roc);
   3716 	evcnt_detach(&stats->rjc);
   3717 	evcnt_detach(&stats->mngprc);
   3718 	evcnt_detach(&stats->mngpdc);
   3719 	evcnt_detach(&stats->xec);
   3720 
   3721 	/* Packet Transmission Stats */
   3722 	evcnt_detach(&stats->gotc);
   3723 	evcnt_detach(&stats->tpt);
   3724 	evcnt_detach(&stats->gptc);
   3725 	evcnt_detach(&stats->bptc);
   3726 	evcnt_detach(&stats->mptc);
   3727 	evcnt_detach(&stats->mngptc);
   3728 	evcnt_detach(&stats->ptc64);
   3729 	evcnt_detach(&stats->ptc127);
   3730 	evcnt_detach(&stats->ptc255);
   3731 	evcnt_detach(&stats->ptc511);
   3732 	evcnt_detach(&stats->ptc1023);
   3733 	evcnt_detach(&stats->ptc1522);
   3734 
   3735 	ixgbe_free_queues(adapter);
   3736 	free(adapter->mta, M_DEVBUF);
   3737 
   3738 	IXGBE_CORE_LOCK_DESTROY(adapter);
   3739 
   3740 	return (0);
   3741 } /* ixgbe_detach */
   3742 
   3743 /************************************************************************
   3744  * ixgbe_setup_low_power_mode - LPLU/WoL preparation
   3745  *
   3746  *   Prepare the adapter/port for LPLU and/or WoL
   3747  ************************************************************************/
   3748 static int
   3749 ixgbe_setup_low_power_mode(struct adapter *adapter)
   3750 {
   3751 	struct ixgbe_hw *hw = &adapter->hw;
   3752 	device_t	dev = adapter->dev;
   3753 	s32		error = 0;
   3754 
   3755 	KASSERT(mutex_owned(&adapter->core_mtx));
   3756 
   3757 	/* Limit power management flow to X550EM baseT */
   3758 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T &&
   3759 	    hw->phy.ops.enter_lplu) {
   3760 		/* X550EM baseT adapters need a special LPLU flow */
   3761 		hw->phy.reset_disable = true;
   3762 		ixgbe_stop_locked(adapter);
   3763 		error = hw->phy.ops.enter_lplu(hw);
   3764 		if (error)
   3765 			device_printf(dev,
   3766 			    "Error entering LPLU: %d\n", error);
   3767 		hw->phy.reset_disable = false;
   3768 	} else {
   3769 		/* Just stop for other adapters */
   3770 		ixgbe_stop_locked(adapter);
   3771 	}
   3772 
   3773 	if (!hw->wol_enabled) {
   3774 		ixgbe_set_phy_power(hw, FALSE);
   3775 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
   3776 		IXGBE_WRITE_REG(hw, IXGBE_WUC, 0);
   3777 	} else {
   3778 		/* Turn off support for APM wakeup. (Using ACPI instead) */
   3779 		IXGBE_WRITE_REG(hw, IXGBE_GRC_BY_MAC(hw),
   3780 		    IXGBE_READ_REG(hw, IXGBE_GRC_BY_MAC(hw)) & ~(u32)2);
   3781 
   3782 		/*
   3783 		 * Clear Wake Up Status register to prevent any previous wakeup
   3784 		 * events from waking us up immediately after we suspend.
   3785 		 */
   3786 		IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
   3787 
   3788 		/*
   3789 		 * Program the Wakeup Filter Control register with user filter
   3790 		 * settings
   3791 		 */
   3792 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, adapter->wufc);
   3793 
   3794 		/* Enable wakeups and power management in Wakeup Control */
   3795 		IXGBE_WRITE_REG(hw, IXGBE_WUC,
   3796 		    IXGBE_WUC_WKEN | IXGBE_WUC_PME_EN);
   3797 
   3798 	}
   3799 
   3800 	return error;
   3801 } /* ixgbe_setup_low_power_mode */
   3802 
   3803 /************************************************************************
   3804  * ixgbe_shutdown - Shutdown entry point
   3805  ************************************************************************/
   3806 #if 0 /* XXX NetBSD ought to register something like this through pmf(9) */
   3807 static int
   3808 ixgbe_shutdown(device_t dev)
   3809 {
   3810 	struct adapter *adapter = device_private(dev);
   3811 	int error = 0;
   3812 
   3813 	INIT_DEBUGOUT("ixgbe_shutdown: begin");
   3814 
   3815 	IXGBE_CORE_LOCK(adapter);
   3816 	error = ixgbe_setup_low_power_mode(adapter);
   3817 	IXGBE_CORE_UNLOCK(adapter);
   3818 
   3819 	return (error);
   3820 } /* ixgbe_shutdown */
   3821 #endif
   3822 
   3823 /************************************************************************
   3824  * ixgbe_suspend
   3825  *
   3826  *   From D0 to D3
   3827  ************************************************************************/
   3828 static bool
   3829 ixgbe_suspend(device_t dev, const pmf_qual_t *qual)
   3830 {
   3831 	struct adapter *adapter = device_private(dev);
   3832 	int	       error = 0;
   3833 
   3834 	INIT_DEBUGOUT("ixgbe_suspend: begin");
   3835 
   3836 	IXGBE_CORE_LOCK(adapter);
   3837 
   3838 	error = ixgbe_setup_low_power_mode(adapter);
   3839 
   3840 	IXGBE_CORE_UNLOCK(adapter);
   3841 
   3842 	return (error);
   3843 } /* ixgbe_suspend */
   3844 
   3845 /************************************************************************
   3846  * ixgbe_resume
   3847  *
   3848  *   From D3 to D0
   3849  ************************************************************************/
   3850 static bool
   3851 ixgbe_resume(device_t dev, const pmf_qual_t *qual)
   3852 {
   3853 	struct adapter	*adapter = device_private(dev);
   3854 	struct ifnet	*ifp = adapter->ifp;
   3855 	struct ixgbe_hw *hw = &adapter->hw;
   3856 	u32		wus;
   3857 
   3858 	INIT_DEBUGOUT("ixgbe_resume: begin");
   3859 
   3860 	IXGBE_CORE_LOCK(adapter);
   3861 
   3862 	/* Read & clear WUS register */
   3863 	wus = IXGBE_READ_REG(hw, IXGBE_WUS);
   3864 	if (wus)
   3865 		device_printf(dev, "Woken up by (WUS): %#010x\n",
   3866 		    IXGBE_READ_REG(hw, IXGBE_WUS));
   3867 	IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
   3868 	/* And clear WUFC until next low-power transition */
   3869 	IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
   3870 
   3871 	/*
   3872 	 * Required after D3->D0 transition;
   3873 	 * will re-advertise all previous advertised speeds
   3874 	 */
   3875 	if (ifp->if_flags & IFF_UP)
   3876 		ixgbe_init_locked(adapter);
   3877 
   3878 	IXGBE_CORE_UNLOCK(adapter);
   3879 
   3880 	return true;
   3881 } /* ixgbe_resume */
   3882 
   3883 /*
   3884  * Set the various hardware offload abilities.
   3885  *
   3886  * This takes the ifnet's if_capenable flags (e.g. set by the user using
   3887  * ifconfig) and indicates to the OS via the ifnet's if_hwassist field what
   3888  * mbuf offload flags the driver will understand.
   3889  */
   3890 static void
   3891 ixgbe_set_if_hwassist(struct adapter *adapter)
   3892 {
   3893 	/* XXX */
   3894 }
   3895 
   3896 /************************************************************************
   3897  * ixgbe_init_locked - Init entry point
   3898  *
   3899  *   Used in two ways: It is used by the stack as an init
   3900  *   entry point in network interface structure. It is also
   3901  *   used by the driver as a hw/sw initialization routine to
   3902  *   get to a consistent state.
   3903  *
   3904  *   return 0 on success, positive on failure
   3905  ************************************************************************/
   3906 static void
   3907 ixgbe_init_locked(struct adapter *adapter)
   3908 {
   3909 	struct ifnet   *ifp = adapter->ifp;
   3910 	device_t	dev = adapter->dev;
   3911 	struct ixgbe_hw *hw = &adapter->hw;
   3912 	struct ix_queue *que;
   3913 	struct tx_ring	*txr;
   3914 	struct rx_ring	*rxr;
   3915 	u32		txdctl, mhadd;
   3916 	u32		rxdctl, rxctrl;
   3917 	u32		ctrl_ext;
   3918 	bool		unsupported_sfp = false;
   3919 	int		i, j, error;
   3920 
   3921 	/* XXX check IFF_UP and IFF_RUNNING, power-saving state! */
   3922 
   3923 	KASSERT(mutex_owned(&adapter->core_mtx));
   3924 	INIT_DEBUGOUT("ixgbe_init_locked: begin");
   3925 
   3926 	hw->need_unsupported_sfp_recovery = false;
   3927 	hw->adapter_stopped = FALSE;
   3928 	ixgbe_stop_adapter(hw);
   3929 	callout_stop(&adapter->timer);
   3930 	for (i = 0, que = adapter->queues; i < adapter->num_queues; i++, que++)
   3931 		que->disabled_count = 0;
   3932 
   3933 	/* XXX I moved this here from the SIOCSIFMTU case in ixgbe_ioctl(). */
   3934 	adapter->max_frame_size =
   3935 		ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   3936 
   3937 	/* Queue indices may change with IOV mode */
   3938 	ixgbe_align_all_queue_indices(adapter);
   3939 
   3940 	/* reprogram the RAR[0] in case user changed it. */
   3941 	ixgbe_set_rar(hw, 0, hw->mac.addr, adapter->pool, IXGBE_RAH_AV);
   3942 
   3943 	/* Get the latest mac address, User can use a LAA */
   3944 	memcpy(hw->mac.addr, CLLADDR(ifp->if_sadl),
   3945 	    IXGBE_ETH_LENGTH_OF_ADDRESS);
   3946 	ixgbe_set_rar(hw, 0, hw->mac.addr, adapter->pool, 1);
   3947 	hw->addr_ctrl.rar_used_count = 1;
   3948 
   3949 	/* Set hardware offload abilities from ifnet flags */
   3950 	ixgbe_set_if_hwassist(adapter);
   3951 
   3952 	/* Prepare transmit descriptors and buffers */
   3953 	if (ixgbe_setup_transmit_structures(adapter)) {
   3954 		device_printf(dev, "Could not setup transmit structures\n");
   3955 		ixgbe_stop_locked(adapter);
   3956 		return;
   3957 	}
   3958 
   3959 	ixgbe_init_hw(hw);
   3960 
   3961 	ixgbe_initialize_iov(adapter);
   3962 
   3963 	ixgbe_initialize_transmit_units(adapter);
   3964 
   3965 	/* Setup Multicast table */
   3966 	ixgbe_set_rxfilter(adapter);
   3967 
   3968 	/* Use fixed buffer size, even for jumbo frames */
   3969 	adapter->rx_mbuf_sz = MCLBYTES;
   3970 
   3971 	/* Prepare receive descriptors and buffers */
   3972 	error = ixgbe_setup_receive_structures(adapter);
   3973 	if (error) {
   3974 		device_printf(dev,
   3975 		    "Could not setup receive structures (err = %d)\n", error);
   3976 		ixgbe_stop_locked(adapter);
   3977 		return;
   3978 	}
   3979 
   3980 	/* Configure RX settings */
   3981 	ixgbe_initialize_receive_units(adapter);
   3982 
   3983 	/* Enable SDP & MSI-X interrupts based on adapter */
   3984 	ixgbe_config_gpie(adapter);
   3985 
   3986 	/* Set MTU size */
   3987 	if (ifp->if_mtu > ETHERMTU) {
   3988 		/* aka IXGBE_MAXFRS on 82599 and newer */
   3989 		mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
   3990 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
   3991 		mhadd |= adapter->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
   3992 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
   3993 	}
   3994 
   3995 	/* Now enable all the queues */
   3996 	for (i = 0; i < adapter->num_queues; i++) {
   3997 		txr = &adapter->tx_rings[i];
   3998 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(txr->me));
   3999 		txdctl |= IXGBE_TXDCTL_ENABLE;
   4000 		/* Set WTHRESH to 8, burst writeback */
   4001 		txdctl |= (8 << 16);
   4002 		/*
   4003 		 * When the internal queue falls below PTHRESH (32),
   4004 		 * start prefetching as long as there are at least
   4005 		 * HTHRESH (1) buffers ready. The values are taken
   4006 		 * from the Intel linux driver 3.8.21.
   4007 		 * Prefetching enables tx line rate even with 1 queue.
   4008 		 */
   4009 		txdctl |= (32 << 0) | (1 << 8);
   4010 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(txr->me), txdctl);
   4011 	}
   4012 
   4013 	for (i = 0; i < adapter->num_queues; i++) {
   4014 		rxr = &adapter->rx_rings[i];
   4015 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
   4016 		if (hw->mac.type == ixgbe_mac_82598EB) {
   4017 			/*
   4018 			 * PTHRESH = 21
   4019 			 * HTHRESH = 4
   4020 			 * WTHRESH = 8
   4021 			 */
   4022 			rxdctl &= ~0x3FFFFF;
   4023 			rxdctl |= 0x080420;
   4024 		}
   4025 		rxdctl |= IXGBE_RXDCTL_ENABLE;
   4026 		IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), rxdctl);
   4027 		for (j = 0; j < 10; j++) {
   4028 			if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me)) &
   4029 			    IXGBE_RXDCTL_ENABLE)
   4030 				break;
   4031 			else
   4032 				msec_delay(1);
   4033 		}
   4034 		wmb();
   4035 
   4036 		/*
   4037 		 * In netmap mode, we must preserve the buffers made
   4038 		 * available to userspace before the if_init()
   4039 		 * (this is true by default on the TX side, because
   4040 		 * init makes all buffers available to userspace).
   4041 		 *
   4042 		 * netmap_reset() and the device specific routines
   4043 		 * (e.g. ixgbe_setup_receive_rings()) map these
   4044 		 * buffers at the end of the NIC ring, so here we
   4045 		 * must set the RDT (tail) register to make sure
   4046 		 * they are not overwritten.
   4047 		 *
   4048 		 * In this driver the NIC ring starts at RDH = 0,
   4049 		 * RDT points to the last slot available for reception (?),
   4050 		 * so RDT = num_rx_desc - 1 means the whole ring is available.
   4051 		 */
   4052 #ifdef DEV_NETMAP
   4053 		if ((adapter->feat_en & IXGBE_FEATURE_NETMAP) &&
   4054 		    (ifp->if_capenable & IFCAP_NETMAP)) {
   4055 			struct netmap_adapter *na = NA(adapter->ifp);
   4056 			struct netmap_kring *kring = na->rx_rings[i];
   4057 			int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
   4058 
   4059 			IXGBE_WRITE_REG(hw, IXGBE_RDT(rxr->me), t);
   4060 		} else
   4061 #endif /* DEV_NETMAP */
   4062 			IXGBE_WRITE_REG(hw, IXGBE_RDT(rxr->me),
   4063 			    adapter->num_rx_desc - 1);
   4064 	}
   4065 
   4066 	/* Enable Receive engine */
   4067 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
   4068 	if (hw->mac.type == ixgbe_mac_82598EB)
   4069 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
   4070 	rxctrl |= IXGBE_RXCTRL_RXEN;
   4071 	ixgbe_enable_rx_dma(hw, rxctrl);
   4072 
   4073 	callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
   4074 
   4075 	/* Set up MSI/MSI-X routing */
   4076 	if (adapter->feat_en & IXGBE_FEATURE_MSIX) {
   4077 		ixgbe_configure_ivars(adapter);
   4078 		/* Set up auto-mask */
   4079 		if (hw->mac.type == ixgbe_mac_82598EB)
   4080 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
   4081 		else {
   4082 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
   4083 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
   4084 		}
   4085 	} else {  /* Simple settings for Legacy/MSI */
   4086 		ixgbe_set_ivar(adapter, 0, 0, 0);
   4087 		ixgbe_set_ivar(adapter, 0, 0, 1);
   4088 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
   4089 	}
   4090 
   4091 	ixgbe_init_fdir(adapter);
   4092 
   4093 	/*
   4094 	 * Check on any SFP devices that
   4095 	 * need to be kick-started
   4096 	 */
   4097 	if (hw->phy.type == ixgbe_phy_none) {
   4098 		error = hw->phy.ops.identify(hw);
   4099 		if (error == IXGBE_ERR_SFP_NOT_SUPPORTED)
   4100 			unsupported_sfp = true;
   4101 	} else if (hw->phy.type == ixgbe_phy_sfp_unsupported)
   4102 		unsupported_sfp = true;
   4103 
   4104 	if (unsupported_sfp)
   4105 		device_printf(dev,
   4106 		    "Unsupported SFP+ module type was detected.\n");
   4107 
   4108 	/* Set moderation on the Link interrupt */
   4109 	ixgbe_eitr_write(adapter, adapter->vector, IXGBE_LINK_ITR);
   4110 
   4111 	/* Enable EEE power saving */
   4112 	if (adapter->feat_cap & IXGBE_FEATURE_EEE)
   4113 		hw->mac.ops.setup_eee(hw,
   4114 		    adapter->feat_en & IXGBE_FEATURE_EEE);
   4115 
   4116 	/* Enable power to the phy. */
   4117 	if (!unsupported_sfp) {
   4118 		ixgbe_set_phy_power(hw, TRUE);
   4119 
   4120 		/* Config/Enable Link */
   4121 		ixgbe_config_link(adapter);
   4122 	}
   4123 
   4124 	/* Hardware Packet Buffer & Flow Control setup */
   4125 	ixgbe_config_delay_values(adapter);
   4126 
   4127 	/* Initialize the FC settings */
   4128 	ixgbe_start_hw(hw);
   4129 
   4130 	/* Set up VLAN support and filter */
   4131 	ixgbe_setup_vlan_hw_support(adapter);
   4132 
   4133 	/* Setup DMA Coalescing */
   4134 	ixgbe_config_dmac(adapter);
   4135 
   4136 	/* And now turn on interrupts */
   4137 	ixgbe_enable_intr(adapter);
   4138 
   4139 	/* Enable the use of the MBX by the VF's */
   4140 	if (adapter->feat_en & IXGBE_FEATURE_SRIOV) {
   4141 		ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
   4142 		ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
   4143 		IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
   4144 	}
   4145 
   4146 	/* Update saved flags. See ixgbe_ifflags_cb() */
   4147 	adapter->if_flags = ifp->if_flags;
   4148 
   4149 	/* Now inform the stack we're ready */
   4150 	ifp->if_flags |= IFF_RUNNING;
   4151 
   4152 	return;
   4153 } /* ixgbe_init_locked */
   4154 
   4155 /************************************************************************
   4156  * ixgbe_init
   4157  ************************************************************************/
   4158 static int
   4159 ixgbe_init(struct ifnet *ifp)
   4160 {
   4161 	struct adapter *adapter = ifp->if_softc;
   4162 
   4163 	IXGBE_CORE_LOCK(adapter);
   4164 	ixgbe_init_locked(adapter);
   4165 	IXGBE_CORE_UNLOCK(adapter);
   4166 
   4167 	return 0;	/* XXX ixgbe_init_locked cannot fail?  really? */
   4168 } /* ixgbe_init */
   4169 
   4170 /************************************************************************
   4171  * ixgbe_set_ivar
   4172  *
   4173  *   Setup the correct IVAR register for a particular MSI-X interrupt
   4174  *     (yes this is all very magic and confusing :)
   4175  *    - entry is the register array entry
   4176  *    - vector is the MSI-X vector for this queue
   4177  *    - type is RX/TX/MISC
   4178  ************************************************************************/
   4179 static void
   4180 ixgbe_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
   4181 {
   4182 	struct ixgbe_hw *hw = &adapter->hw;
   4183 	u32 ivar, index;
   4184 
   4185 	vector |= IXGBE_IVAR_ALLOC_VAL;
   4186 
   4187 	switch (hw->mac.type) {
   4188 	case ixgbe_mac_82598EB:
   4189 		if (type == -1)
   4190 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
   4191 		else
   4192 			entry += (type * 64);
   4193 		index = (entry >> 2) & 0x1F;
   4194 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
   4195 		ivar &= ~(0xffUL << (8 * (entry & 0x3)));
   4196 		ivar |= ((u32)vector << (8 * (entry & 0x3)));
   4197 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
   4198 		break;
   4199 	case ixgbe_mac_82599EB:
   4200 	case ixgbe_mac_X540:
   4201 	case ixgbe_mac_X550:
   4202 	case ixgbe_mac_X550EM_x:
   4203 	case ixgbe_mac_X550EM_a:
   4204 		if (type == -1) { /* MISC IVAR */
   4205 			index = (entry & 1) * 8;
   4206 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
   4207 			ivar &= ~(0xffUL << index);
   4208 			ivar |= ((u32)vector << index);
   4209 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
   4210 		} else {	/* RX/TX IVARS */
   4211 			index = (16 * (entry & 1)) + (8 * type);
   4212 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
   4213 			ivar &= ~(0xffUL << index);
   4214 			ivar |= ((u32)vector << index);
   4215 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
   4216 		}
   4217 		break;
   4218 	default:
   4219 		break;
   4220 	}
   4221 } /* ixgbe_set_ivar */
   4222 
   4223 /************************************************************************
   4224  * ixgbe_configure_ivars
   4225  ************************************************************************/
   4226 static void
   4227 ixgbe_configure_ivars(struct adapter *adapter)
   4228 {
   4229 	struct ix_queue *que = adapter->queues;
   4230 	u32		newitr;
   4231 
   4232 	if (ixgbe_max_interrupt_rate > 0)
   4233 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
   4234 	else {
   4235 		/*
   4236 		 * Disable DMA coalescing if interrupt moderation is
   4237 		 * disabled.
   4238 		 */
   4239 		adapter->dmac = 0;
   4240 		newitr = 0;
   4241 	}
   4242 
   4243 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   4244 		struct rx_ring *rxr = &adapter->rx_rings[i];
   4245 		struct tx_ring *txr = &adapter->tx_rings[i];
   4246 		/* First the RX queue entry */
   4247 		ixgbe_set_ivar(adapter, rxr->me, que->msix, 0);
   4248 		/* ... and the TX */
   4249 		ixgbe_set_ivar(adapter, txr->me, que->msix, 1);
   4250 		/* Set an Initial EITR value */
   4251 		ixgbe_eitr_write(adapter, que->msix, newitr);
   4252 		/*
   4253 		 * To eliminate influence of the previous state.
   4254 		 * At this point, Tx/Rx interrupt handler
   4255 		 * (ixgbe_msix_que()) cannot be called, so  both
   4256 		 * IXGBE_TX_LOCK and IXGBE_RX_LOCK are not required.
   4257 		 */
   4258 		que->eitr_setting = 0;
   4259 	}
   4260 
   4261 	/* For the Link interrupt */
   4262 	ixgbe_set_ivar(adapter, 1, adapter->vector, -1);
   4263 } /* ixgbe_configure_ivars */
   4264 
   4265 /************************************************************************
   4266  * ixgbe_config_gpie
   4267  ************************************************************************/
   4268 static void
   4269 ixgbe_config_gpie(struct adapter *adapter)
   4270 {
   4271 	struct ixgbe_hw *hw = &adapter->hw;
   4272 	u32		gpie;
   4273 
   4274 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
   4275 
   4276 	if (adapter->feat_en & IXGBE_FEATURE_MSIX) {
   4277 		/* Enable Enhanced MSI-X mode */
   4278 		gpie |= IXGBE_GPIE_MSIX_MODE
   4279 		     |	IXGBE_GPIE_EIAME
   4280 		     |	IXGBE_GPIE_PBA_SUPPORT
   4281 		     |	IXGBE_GPIE_OCD;
   4282 	}
   4283 
   4284 	/* Fan Failure Interrupt */
   4285 	if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL)
   4286 		gpie |= IXGBE_SDP1_GPIEN;
   4287 
   4288 	/* Thermal Sensor Interrupt */
   4289 	if (adapter->feat_en & IXGBE_FEATURE_TEMP_SENSOR)
   4290 		gpie |= IXGBE_SDP0_GPIEN_X540;
   4291 
   4292 	/* Link detection */
   4293 	switch (hw->mac.type) {
   4294 	case ixgbe_mac_82599EB:
   4295 		gpie |= IXGBE_SDP1_GPIEN | IXGBE_SDP2_GPIEN;
   4296 		break;
   4297 	case ixgbe_mac_X550EM_x:
   4298 	case ixgbe_mac_X550EM_a:
   4299 		gpie |= IXGBE_SDP0_GPIEN_X540;
   4300 		break;
   4301 	default:
   4302 		break;
   4303 	}
   4304 
   4305 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
   4306 
   4307 } /* ixgbe_config_gpie */
   4308 
   4309 /************************************************************************
   4310  * ixgbe_config_delay_values
   4311  *
   4312  *   Requires adapter->max_frame_size to be set.
   4313  ************************************************************************/
   4314 static void
   4315 ixgbe_config_delay_values(struct adapter *adapter)
   4316 {
   4317 	struct ixgbe_hw *hw = &adapter->hw;
   4318 	u32		rxpb, frame, size, tmp;
   4319 
   4320 	frame = adapter->max_frame_size;
   4321 
   4322 	/* Calculate High Water */
   4323 	switch (hw->mac.type) {
   4324 	case ixgbe_mac_X540:
   4325 	case ixgbe_mac_X550:
   4326 	case ixgbe_mac_X550EM_x:
   4327 	case ixgbe_mac_X550EM_a:
   4328 		tmp = IXGBE_DV_X540(frame, frame);
   4329 		break;
   4330 	default:
   4331 		tmp = IXGBE_DV(frame, frame);
   4332 		break;
   4333 	}
   4334 	size = IXGBE_BT2KB(tmp);
   4335 	rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
   4336 	hw->fc.high_water[0] = rxpb - size;
   4337 
   4338 	/* Now calculate Low Water */
   4339 	switch (hw->mac.type) {
   4340 	case ixgbe_mac_X540:
   4341 	case ixgbe_mac_X550:
   4342 	case ixgbe_mac_X550EM_x:
   4343 	case ixgbe_mac_X550EM_a:
   4344 		tmp = IXGBE_LOW_DV_X540(frame);
   4345 		break;
   4346 	default:
   4347 		tmp = IXGBE_LOW_DV(frame);
   4348 		break;
   4349 	}
   4350 	hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
   4351 
   4352 	hw->fc.pause_time = IXGBE_FC_PAUSE;
   4353 	hw->fc.send_xon = TRUE;
   4354 } /* ixgbe_config_delay_values */
   4355 
   4356 /************************************************************************
   4357  * ixgbe_set_rxfilter - Multicast Update
   4358  *
   4359  *   Called whenever multicast address list is updated.
   4360  ************************************************************************/
   4361 static void
   4362 ixgbe_set_rxfilter(struct adapter *adapter)
   4363 {
   4364 	struct ixgbe_mc_addr	*mta;
   4365 	struct ifnet		*ifp = adapter->ifp;
   4366 	u8			*update_ptr;
   4367 	int			mcnt = 0;
   4368 	u32			fctrl;
   4369 	struct ethercom		*ec = &adapter->osdep.ec;
   4370 	struct ether_multi	*enm;
   4371 	struct ether_multistep	step;
   4372 
   4373 	KASSERT(mutex_owned(&adapter->core_mtx));
   4374 	IOCTL_DEBUGOUT("ixgbe_set_rxfilter: begin");
   4375 
   4376 	mta = adapter->mta;
   4377 	bzero(mta, sizeof(*mta) * MAX_NUM_MULTICAST_ADDRESSES);
   4378 
   4379 	ifp->if_flags &= ~IFF_ALLMULTI;
   4380 	ETHER_LOCK(ec);
   4381 	ETHER_FIRST_MULTI(step, ec, enm);
   4382 	while (enm != NULL) {
   4383 		if ((mcnt == MAX_NUM_MULTICAST_ADDRESSES) ||
   4384 		    (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   4385 			ETHER_ADDR_LEN) != 0)) {
   4386 			ifp->if_flags |= IFF_ALLMULTI;
   4387 			break;
   4388 		}
   4389 		bcopy(enm->enm_addrlo,
   4390 		    mta[mcnt].addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
   4391 		mta[mcnt].vmdq = adapter->pool;
   4392 		mcnt++;
   4393 		ETHER_NEXT_MULTI(step, enm);
   4394 	}
   4395 	ETHER_UNLOCK(ec);
   4396 
   4397 	fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
   4398 	if (ifp->if_flags & IFF_PROMISC)
   4399 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   4400 	else if (ifp->if_flags & IFF_ALLMULTI) {
   4401 		fctrl |= IXGBE_FCTRL_MPE;
   4402 		fctrl &= ~IXGBE_FCTRL_UPE;
   4403 	} else
   4404 		fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   4405 
   4406 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
   4407 
   4408 	if (mcnt <= MAX_NUM_MULTICAST_ADDRESSES) {
   4409 		update_ptr = (u8 *)mta;
   4410 		ixgbe_update_mc_addr_list(&adapter->hw, update_ptr, mcnt,
   4411 		    ixgbe_mc_array_itr, TRUE);
   4412 	}
   4413 } /* ixgbe_set_filter */
   4414 
   4415 /************************************************************************
   4416  * ixgbe_mc_array_itr
   4417  *
   4418  *   An iterator function needed by the multicast shared code.
   4419  *   It feeds the shared code routine the addresses in the
   4420  *   array of ixgbe_set_rxfilter() one by one.
   4421  ************************************************************************/
   4422 static u8 *
   4423 ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
   4424 {
   4425 	struct ixgbe_mc_addr *mta;
   4426 
   4427 	mta = (struct ixgbe_mc_addr *)*update_ptr;
   4428 	*vmdq = mta->vmdq;
   4429 
   4430 	*update_ptr = (u8*)(mta + 1);
   4431 
   4432 	return (mta->addr);
   4433 } /* ixgbe_mc_array_itr */
   4434 
   4435 /************************************************************************
   4436  * ixgbe_local_timer - Timer routine
   4437  *
   4438  *   Checks for link status, updates statistics,
   4439  *   and runs the watchdog check.
   4440  ************************************************************************/
   4441 static void
   4442 ixgbe_local_timer(void *arg)
   4443 {
   4444 	struct adapter *adapter = arg;
   4445 
   4446 	IXGBE_CORE_LOCK(adapter);
   4447 	ixgbe_local_timer1(adapter);
   4448 	IXGBE_CORE_UNLOCK(adapter);
   4449 }
   4450 
   4451 static void
   4452 ixgbe_local_timer1(void *arg)
   4453 {
   4454 	struct adapter	*adapter = arg;
   4455 	device_t	dev = adapter->dev;
   4456 	struct ix_queue	*que = adapter->queues;
   4457 	u64		queues = 0;
   4458 	u64		v0, v1, v2, v3, v4, v5, v6, v7;
   4459 	int		hung = 0;
   4460 	int		i;
   4461 
   4462 	KASSERT(mutex_owned(&adapter->core_mtx));
   4463 
   4464 	/* Check for pluggable optics */
   4465 	if (adapter->sfp_probe)
   4466 		if (!ixgbe_sfp_probe(adapter))
   4467 			goto out; /* Nothing to do */
   4468 
   4469 	ixgbe_update_link_status(adapter);
   4470 	ixgbe_update_stats_counters(adapter);
   4471 
   4472 	/* Update some event counters */
   4473 	v0 = v1 = v2 = v3 = v4 = v5 = v6 = v7 = 0;
   4474 	que = adapter->queues;
   4475 	for (i = 0; i < adapter->num_queues; i++, que++) {
   4476 		struct tx_ring	*txr = que->txr;
   4477 
   4478 		v0 += txr->q_efbig_tx_dma_setup;
   4479 		v1 += txr->q_mbuf_defrag_failed;
   4480 		v2 += txr->q_efbig2_tx_dma_setup;
   4481 		v3 += txr->q_einval_tx_dma_setup;
   4482 		v4 += txr->q_other_tx_dma_setup;
   4483 		v5 += txr->q_eagain_tx_dma_setup;
   4484 		v6 += txr->q_enomem_tx_dma_setup;
   4485 		v7 += txr->q_tso_err;
   4486 	}
   4487 	adapter->efbig_tx_dma_setup.ev_count = v0;
   4488 	adapter->mbuf_defrag_failed.ev_count = v1;
   4489 	adapter->efbig2_tx_dma_setup.ev_count = v2;
   4490 	adapter->einval_tx_dma_setup.ev_count = v3;
   4491 	adapter->other_tx_dma_setup.ev_count = v4;
   4492 	adapter->eagain_tx_dma_setup.ev_count = v5;
   4493 	adapter->enomem_tx_dma_setup.ev_count = v6;
   4494 	adapter->tso_err.ev_count = v7;
   4495 
   4496 	/*
   4497 	 * Check the TX queues status
   4498 	 *	- mark hung queues so we don't schedule on them
   4499 	 *	- watchdog only if all queues show hung
   4500 	 */
   4501 	que = adapter->queues;
   4502 	for (i = 0; i < adapter->num_queues; i++, que++) {
   4503 		/* Keep track of queues with work for soft irq */
   4504 		if (que->txr->busy)
   4505 			queues |= 1ULL << que->me;
   4506 		/*
   4507 		 * Each time txeof runs without cleaning, but there
   4508 		 * are uncleaned descriptors it increments busy. If
   4509 		 * we get to the MAX we declare it hung.
   4510 		 */
   4511 		if (que->busy == IXGBE_QUEUE_HUNG) {
   4512 			++hung;
   4513 			/* Mark the queue as inactive */
   4514 			adapter->active_queues &= ~(1ULL << que->me);
   4515 			continue;
   4516 		} else {
   4517 			/* Check if we've come back from hung */
   4518 			if ((adapter->active_queues & (1ULL << que->me)) == 0)
   4519 				adapter->active_queues |= 1ULL << que->me;
   4520 		}
   4521 		if (que->busy >= IXGBE_MAX_TX_BUSY) {
   4522 			device_printf(dev,
   4523 			    "Warning queue %d appears to be hung!\n", i);
   4524 			que->txr->busy = IXGBE_QUEUE_HUNG;
   4525 			++hung;
   4526 		}
   4527 	}
   4528 
   4529 	/* Only truly watchdog if all queues show hung */
   4530 	if (hung == adapter->num_queues)
   4531 		goto watchdog;
   4532 #if 0 /* XXX Avoid unexpectedly disabling interrupt forever (PR#53294) */
   4533 	else if (queues != 0) { /* Force an IRQ on queues with work */
   4534 		que = adapter->queues;
   4535 		for (i = 0; i < adapter->num_queues; i++, que++) {
   4536 			mutex_enter(&que->dc_mtx);
   4537 			if (que->disabled_count == 0)
   4538 				ixgbe_rearm_queues(adapter,
   4539 				    queues & ((u64)1 << i));
   4540 			mutex_exit(&que->dc_mtx);
   4541 		}
   4542 	}
   4543 #endif
   4544 
   4545 out:
   4546 	callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
   4547 	return;
   4548 
   4549 watchdog:
   4550 	device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
   4551 	adapter->ifp->if_flags &= ~IFF_RUNNING;
   4552 	adapter->watchdog_events.ev_count++;
   4553 	ixgbe_init_locked(adapter);
   4554 } /* ixgbe_local_timer */
   4555 
   4556 /************************************************************************
   4557  * ixgbe_recovery_mode_timer - Recovery mode timer routine
   4558  ************************************************************************/
   4559 static void
   4560 ixgbe_recovery_mode_timer(void *arg)
   4561 {
   4562 	struct adapter *adapter = arg;
   4563 	struct ixgbe_hw *hw = &adapter->hw;
   4564 
   4565 	IXGBE_CORE_LOCK(adapter);
   4566 	if (ixgbe_fw_recovery_mode(hw)) {
   4567 		if (atomic_cas_uint(&adapter->recovery_mode, 0, 1)) {
   4568 			/* Firmware error detected, entering recovery mode */
   4569 			device_printf(adapter->dev, "Firmware recovery mode detected. Limiting functionality. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
   4570 
   4571 			if (hw->adapter_stopped == FALSE)
   4572 				ixgbe_stop_locked(adapter);
   4573 		}
   4574 	} else
   4575 		atomic_cas_uint(&adapter->recovery_mode, 1, 0);
   4576 
   4577 	callout_reset(&adapter->recovery_mode_timer, hz,
   4578 	    ixgbe_recovery_mode_timer, adapter);
   4579 	IXGBE_CORE_UNLOCK(adapter);
   4580 } /* ixgbe_recovery_mode_timer */
   4581 
   4582 /************************************************************************
   4583  * ixgbe_sfp_probe
   4584  *
   4585  *   Determine if a port had optics inserted.
   4586  ************************************************************************/
   4587 static bool
   4588 ixgbe_sfp_probe(struct adapter *adapter)
   4589 {
   4590 	struct ixgbe_hw	*hw = &adapter->hw;
   4591 	device_t	dev = adapter->dev;
   4592 	bool		result = FALSE;
   4593 
   4594 	if ((hw->phy.type == ixgbe_phy_nl) &&
   4595 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
   4596 		s32 ret = hw->phy.ops.identify_sfp(hw);
   4597 		if (ret)
   4598 			goto out;
   4599 		ret = hw->phy.ops.reset(hw);
   4600 		adapter->sfp_probe = FALSE;
   4601 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   4602 			device_printf(dev,"Unsupported SFP+ module detected!");
   4603 			device_printf(dev,
   4604 			    "Reload driver with supported module.\n");
   4605 			goto out;
   4606 		} else
   4607 			device_printf(dev, "SFP+ module detected!\n");
   4608 		/* We now have supported optics */
   4609 		result = TRUE;
   4610 	}
   4611 out:
   4612 
   4613 	return (result);
   4614 } /* ixgbe_sfp_probe */
   4615 
   4616 /************************************************************************
   4617  * ixgbe_handle_mod - Tasklet for SFP module interrupts
   4618  ************************************************************************/
   4619 static void
   4620 ixgbe_handle_mod(void *context)
   4621 {
   4622 	struct adapter	*adapter = context;
   4623 	struct ixgbe_hw *hw = &adapter->hw;
   4624 	device_t	dev = adapter->dev;
   4625 	u32		err, cage_full = 0;
   4626 
   4627 	IXGBE_CORE_LOCK(adapter);
   4628 	++adapter->mod_sicount.ev_count;
   4629 	if (adapter->hw.need_crosstalk_fix) {
   4630 		switch (hw->mac.type) {
   4631 		case ixgbe_mac_82599EB:
   4632 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
   4633 			    IXGBE_ESDP_SDP2;
   4634 			break;
   4635 		case ixgbe_mac_X550EM_x:
   4636 		case ixgbe_mac_X550EM_a:
   4637 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
   4638 			    IXGBE_ESDP_SDP0;
   4639 			break;
   4640 		default:
   4641 			break;
   4642 		}
   4643 
   4644 		if (!cage_full)
   4645 			goto out;
   4646 	}
   4647 
   4648 	err = hw->phy.ops.identify_sfp(hw);
   4649 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   4650 		device_printf(dev,
   4651 		    "Unsupported SFP+ module type was detected.\n");
   4652 		goto out;
   4653 	}
   4654 
   4655 	if (hw->need_unsupported_sfp_recovery) {
   4656 		device_printf(dev, "Recovering from unsupported SFP\n");
   4657 		/*
   4658 		 *  We could recover the status by calling setup_sfp(),
   4659 		 * setup_link() and some others. It's complex and might not
   4660 		 * work correctly on some unknown cases. To avoid such type of
   4661 		 * problem, call ixgbe_init_locked(). It's simple and safe
   4662 		 * approach.
   4663 		 */
   4664 		ixgbe_init_locked(adapter);
   4665 	} else {
   4666 		if (hw->mac.type == ixgbe_mac_82598EB)
   4667 			err = hw->phy.ops.reset(hw);
   4668 		else {
   4669 			err = hw->mac.ops.setup_sfp(hw);
   4670 			hw->phy.sfp_setup_needed = FALSE;
   4671 		}
   4672 		if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   4673 			device_printf(dev,
   4674 			    "Setup failure - unsupported SFP+ module type.\n");
   4675 			goto out;
   4676 		}
   4677 	}
   4678 	softint_schedule(adapter->msf_si);
   4679 out:
   4680 	IXGBE_CORE_UNLOCK(adapter);
   4681 } /* ixgbe_handle_mod */
   4682 
   4683 
   4684 /************************************************************************
   4685  * ixgbe_handle_msf - Tasklet for MSF (multispeed fiber) interrupts
   4686  ************************************************************************/
   4687 static void
   4688 ixgbe_handle_msf(void *context)
   4689 {
   4690 	struct adapter	*adapter = context;
   4691 	struct ixgbe_hw *hw = &adapter->hw;
   4692 	u32		autoneg;
   4693 	bool		negotiate;
   4694 
   4695 	IXGBE_CORE_LOCK(adapter);
   4696 	++adapter->msf_sicount.ev_count;
   4697 	/* get_supported_phy_layer will call hw->phy.ops.identify_sfp() */
   4698 	adapter->phy_layer = ixgbe_get_supported_physical_layer(hw);
   4699 
   4700 	autoneg = hw->phy.autoneg_advertised;
   4701 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
   4702 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
   4703 	else
   4704 		negotiate = 0;
   4705 	if (hw->mac.ops.setup_link)
   4706 		hw->mac.ops.setup_link(hw, autoneg, TRUE);
   4707 
   4708 	/* Adjust media types shown in ifconfig */
   4709 	ifmedia_removeall(&adapter->media);
   4710 	ixgbe_add_media_types(adapter);
   4711 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   4712 	IXGBE_CORE_UNLOCK(adapter);
   4713 } /* ixgbe_handle_msf */
   4714 
   4715 /************************************************************************
   4716  * ixgbe_handle_phy - Tasklet for external PHY interrupts
   4717  ************************************************************************/
   4718 static void
   4719 ixgbe_handle_phy(void *context)
   4720 {
   4721 	struct adapter	*adapter = context;
   4722 	struct ixgbe_hw *hw = &adapter->hw;
   4723 	int error;
   4724 
   4725 	++adapter->phy_sicount.ev_count;
   4726 	error = hw->phy.ops.handle_lasi(hw);
   4727 	if (error == IXGBE_ERR_OVERTEMP)
   4728 		device_printf(adapter->dev,
   4729 		    "CRITICAL: EXTERNAL PHY OVER TEMP!! "
   4730 		    " PHY will downshift to lower power state!\n");
   4731 	else if (error)
   4732 		device_printf(adapter->dev,
   4733 		    "Error handling LASI interrupt: %d\n", error);
   4734 } /* ixgbe_handle_phy */
   4735 
   4736 static void
   4737 ixgbe_ifstop(struct ifnet *ifp, int disable)
   4738 {
   4739 	struct adapter *adapter = ifp->if_softc;
   4740 
   4741 	IXGBE_CORE_LOCK(adapter);
   4742 	ixgbe_stop_locked(adapter);
   4743 	IXGBE_CORE_UNLOCK(adapter);
   4744 }
   4745 
   4746 /************************************************************************
   4747  * ixgbe_stop_locked - Stop the hardware
   4748  *
   4749  *   Disables all traffic on the adapter by issuing a
   4750  *   global reset on the MAC and deallocates TX/RX buffers.
   4751  ************************************************************************/
   4752 static void
   4753 ixgbe_stop_locked(void *arg)
   4754 {
   4755 	struct ifnet	*ifp;
   4756 	struct adapter	*adapter = arg;
   4757 	struct ixgbe_hw *hw = &adapter->hw;
   4758 
   4759 	ifp = adapter->ifp;
   4760 
   4761 	KASSERT(mutex_owned(&adapter->core_mtx));
   4762 
   4763 	INIT_DEBUGOUT("ixgbe_stop_locked: begin\n");
   4764 	ixgbe_disable_intr(adapter);
   4765 	callout_stop(&adapter->timer);
   4766 
   4767 	/* Let the stack know...*/
   4768 	ifp->if_flags &= ~IFF_RUNNING;
   4769 
   4770 	ixgbe_reset_hw(hw);
   4771 	hw->adapter_stopped = FALSE;
   4772 	ixgbe_stop_adapter(hw);
   4773 	if (hw->mac.type == ixgbe_mac_82599EB)
   4774 		ixgbe_stop_mac_link_on_d3_82599(hw);
   4775 	/* Turn off the laser - noop with no optics */
   4776 	ixgbe_disable_tx_laser(hw);
   4777 
   4778 	/* Update the stack */
   4779 	adapter->link_up = FALSE;
   4780 	ixgbe_update_link_status(adapter);
   4781 
   4782 	/* reprogram the RAR[0] in case user changed it. */
   4783 	ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
   4784 
   4785 	return;
   4786 } /* ixgbe_stop_locked */
   4787 
   4788 /************************************************************************
   4789  * ixgbe_update_link_status - Update OS on link state
   4790  *
   4791  * Note: Only updates the OS on the cached link state.
   4792  *	 The real check of the hardware only happens with
   4793  *	 a link interrupt.
   4794  ************************************************************************/
   4795 static void
   4796 ixgbe_update_link_status(struct adapter *adapter)
   4797 {
   4798 	struct ifnet	*ifp = adapter->ifp;
   4799 	device_t	dev = adapter->dev;
   4800 	struct ixgbe_hw *hw = &adapter->hw;
   4801 
   4802 	KASSERT(mutex_owned(&adapter->core_mtx));
   4803 
   4804 	if (adapter->link_up) {
   4805 		if (adapter->link_active != LINK_STATE_UP) {
   4806 			/*
   4807 			 * To eliminate influence of the previous state
   4808 			 * in the same way as ixgbe_init_locked().
   4809 			 */
   4810 			struct ix_queue	*que = adapter->queues;
   4811 			for (int i = 0; i < adapter->num_queues; i++, que++)
   4812 				que->eitr_setting = 0;
   4813 
   4814 			if (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL){
   4815 				/*
   4816 				 *  Discard count for both MAC Local Fault and
   4817 				 * Remote Fault because those registers are
   4818 				 * valid only when the link speed is up and
   4819 				 * 10Gbps.
   4820 				 */
   4821 				IXGBE_READ_REG(hw, IXGBE_MLFC);
   4822 				IXGBE_READ_REG(hw, IXGBE_MRFC);
   4823 			}
   4824 
   4825 			if (bootverbose) {
   4826 				const char *bpsmsg;
   4827 
   4828 				switch (adapter->link_speed) {
   4829 				case IXGBE_LINK_SPEED_10GB_FULL:
   4830 					bpsmsg = "10 Gbps";
   4831 					break;
   4832 				case IXGBE_LINK_SPEED_5GB_FULL:
   4833 					bpsmsg = "5 Gbps";
   4834 					break;
   4835 				case IXGBE_LINK_SPEED_2_5GB_FULL:
   4836 					bpsmsg = "2.5 Gbps";
   4837 					break;
   4838 				case IXGBE_LINK_SPEED_1GB_FULL:
   4839 					bpsmsg = "1 Gbps";
   4840 					break;
   4841 				case IXGBE_LINK_SPEED_100_FULL:
   4842 					bpsmsg = "100 Mbps";
   4843 					break;
   4844 				case IXGBE_LINK_SPEED_10_FULL:
   4845 					bpsmsg = "10 Mbps";
   4846 					break;
   4847 				default:
   4848 					bpsmsg = "unknown speed";
   4849 					break;
   4850 				}
   4851 				device_printf(dev, "Link is up %s %s \n",
   4852 				    bpsmsg, "Full Duplex");
   4853 			}
   4854 			adapter->link_active = LINK_STATE_UP;
   4855 			/* Update any Flow Control changes */
   4856 			ixgbe_fc_enable(&adapter->hw);
   4857 			/* Update DMA coalescing config */
   4858 			ixgbe_config_dmac(adapter);
   4859 			if_link_state_change(ifp, LINK_STATE_UP);
   4860 
   4861 			if (adapter->feat_en & IXGBE_FEATURE_SRIOV)
   4862 				ixgbe_ping_all_vfs(adapter);
   4863 		}
   4864 	} else {
   4865 		/*
   4866 		 * Do it when link active changes to DOWN. i.e.
   4867 		 * a) LINK_STATE_UNKNOWN -> LINK_STATE_DOWN
   4868 		 * b) LINK_STATE_UP	 -> LINK_STATE_DOWN
   4869 		 */
   4870 		if (adapter->link_active != LINK_STATE_DOWN) {
   4871 			if (bootverbose)
   4872 				device_printf(dev, "Link is Down\n");
   4873 			if_link_state_change(ifp, LINK_STATE_DOWN);
   4874 			adapter->link_active = LINK_STATE_DOWN;
   4875 			if (adapter->feat_en & IXGBE_FEATURE_SRIOV)
   4876 				ixgbe_ping_all_vfs(adapter);
   4877 			ixgbe_drain_all(adapter);
   4878 		}
   4879 	}
   4880 } /* ixgbe_update_link_status */
   4881 
   4882 /************************************************************************
   4883  * ixgbe_config_dmac - Configure DMA Coalescing
   4884  ************************************************************************/
   4885 static void
   4886 ixgbe_config_dmac(struct adapter *adapter)
   4887 {
   4888 	struct ixgbe_hw *hw = &adapter->hw;
   4889 	struct ixgbe_dmac_config *dcfg = &hw->mac.dmac_config;
   4890 
   4891 	if (hw->mac.type < ixgbe_mac_X550 || !hw->mac.ops.dmac_config)
   4892 		return;
   4893 
   4894 	if (dcfg->watchdog_timer ^ adapter->dmac ||
   4895 	    dcfg->link_speed ^ adapter->link_speed) {
   4896 		dcfg->watchdog_timer = adapter->dmac;
   4897 		dcfg->fcoe_en = false;
   4898 		dcfg->link_speed = adapter->link_speed;
   4899 		dcfg->num_tcs = 1;
   4900 
   4901 		INIT_DEBUGOUT2("dmac settings: watchdog %d, link speed %d\n",
   4902 		    dcfg->watchdog_timer, dcfg->link_speed);
   4903 
   4904 		hw->mac.ops.dmac_config(hw);
   4905 	}
   4906 } /* ixgbe_config_dmac */
   4907 
   4908 /************************************************************************
   4909  * ixgbe_enable_intr
   4910  ************************************************************************/
   4911 static void
   4912 ixgbe_enable_intr(struct adapter *adapter)
   4913 {
   4914 	struct ixgbe_hw	*hw = &adapter->hw;
   4915 	struct ix_queue	*que = adapter->queues;
   4916 	u32		mask, fwsm;
   4917 
   4918 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
   4919 
   4920 	switch (adapter->hw.mac.type) {
   4921 	case ixgbe_mac_82599EB:
   4922 		mask |= IXGBE_EIMS_ECC;
   4923 		/* Temperature sensor on some adapters */
   4924 		mask |= IXGBE_EIMS_GPI_SDP0;
   4925 		/* SFP+ (RX_LOS_N & MOD_ABS_N) */
   4926 		mask |= IXGBE_EIMS_GPI_SDP1;
   4927 		mask |= IXGBE_EIMS_GPI_SDP2;
   4928 		break;
   4929 	case ixgbe_mac_X540:
   4930 		/* Detect if Thermal Sensor is enabled */
   4931 		fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
   4932 		if (fwsm & IXGBE_FWSM_TS_ENABLED)
   4933 			mask |= IXGBE_EIMS_TS;
   4934 		mask |= IXGBE_EIMS_ECC;
   4935 		break;
   4936 	case ixgbe_mac_X550:
   4937 		/* MAC thermal sensor is automatically enabled */
   4938 		mask |= IXGBE_EIMS_TS;
   4939 		mask |= IXGBE_EIMS_ECC;
   4940 		break;
   4941 	case ixgbe_mac_X550EM_x:
   4942 	case ixgbe_mac_X550EM_a:
   4943 		/* Some devices use SDP0 for important information */
   4944 		if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
   4945 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP ||
   4946 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP_N ||
   4947 		    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
   4948 			mask |= IXGBE_EIMS_GPI_SDP0_BY_MAC(hw);
   4949 		if (hw->phy.type == ixgbe_phy_x550em_ext_t)
   4950 			mask |= IXGBE_EICR_GPI_SDP0_X540;
   4951 		mask |= IXGBE_EIMS_ECC;
   4952 		break;
   4953 	default:
   4954 		break;
   4955 	}
   4956 
   4957 	/* Enable Fan Failure detection */
   4958 	if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL)
   4959 		mask |= IXGBE_EIMS_GPI_SDP1;
   4960 	/* Enable SR-IOV */
   4961 	if (adapter->feat_en & IXGBE_FEATURE_SRIOV)
   4962 		mask |= IXGBE_EIMS_MAILBOX;
   4963 	/* Enable Flow Director */
   4964 	if (adapter->feat_en & IXGBE_FEATURE_FDIR)
   4965 		mask |= IXGBE_EIMS_FLOW_DIR;
   4966 
   4967 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
   4968 
   4969 	/* With MSI-X we use auto clear */
   4970 	if (adapter->msix_mem) {
   4971 		mask = IXGBE_EIMS_ENABLE_MASK;
   4972 		/* Don't autoclear Link */
   4973 		mask &= ~IXGBE_EIMS_OTHER;
   4974 		mask &= ~IXGBE_EIMS_LSC;
   4975 		if (adapter->feat_cap & IXGBE_FEATURE_SRIOV)
   4976 			mask &= ~IXGBE_EIMS_MAILBOX;
   4977 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
   4978 	}
   4979 
   4980 	/*
   4981 	 * Now enable all queues, this is done separately to
   4982 	 * allow for handling the extended (beyond 32) MSI-X
   4983 	 * vectors that can be used by 82599
   4984 	 */
   4985 	for (int i = 0; i < adapter->num_queues; i++, que++)
   4986 		ixgbe_enable_queue(adapter, que->msix);
   4987 
   4988 	IXGBE_WRITE_FLUSH(hw);
   4989 
   4990 } /* ixgbe_enable_intr */
   4991 
   4992 /************************************************************************
   4993  * ixgbe_disable_intr_internal
   4994  ************************************************************************/
   4995 static void
   4996 ixgbe_disable_intr_internal(struct adapter *adapter, bool nestok)
   4997 {
   4998 	struct ix_queue	*que = adapter->queues;
   4999 
   5000 	/* disable interrupts other than queues */
   5001 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~IXGBE_EIMC_RTX_QUEUE);
   5002 
   5003 	if (adapter->msix_mem)
   5004 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, 0);
   5005 
   5006 	for (int i = 0; i < adapter->num_queues; i++, que++)
   5007 		ixgbe_disable_queue_internal(adapter, que->msix, nestok);
   5008 
   5009 	IXGBE_WRITE_FLUSH(&adapter->hw);
   5010 
   5011 } /* ixgbe_do_disable_intr_internal */
   5012 
   5013 /************************************************************************
   5014  * ixgbe_disable_intr
   5015  ************************************************************************/
   5016 static void
   5017 ixgbe_disable_intr(struct adapter *adapter)
   5018 {
   5019 
   5020 	ixgbe_disable_intr_internal(adapter, true);
   5021 } /* ixgbe_disable_intr */
   5022 
   5023 /************************************************************************
   5024  * ixgbe_ensure_disabled_intr
   5025  ************************************************************************/
   5026 void
   5027 ixgbe_ensure_disabled_intr(struct adapter *adapter)
   5028 {
   5029 
   5030 	ixgbe_disable_intr_internal(adapter, false);
   5031 } /* ixgbe_ensure_disabled_intr */
   5032 
   5033 /************************************************************************
   5034  * ixgbe_legacy_irq - Legacy Interrupt Service routine
   5035  ************************************************************************/
   5036 static int
   5037 ixgbe_legacy_irq(void *arg)
   5038 {
   5039 	struct ix_queue *que = arg;
   5040 	struct adapter	*adapter = que->adapter;
   5041 	struct ixgbe_hw	*hw = &adapter->hw;
   5042 	struct ifnet	*ifp = adapter->ifp;
   5043 	struct		tx_ring *txr = adapter->tx_rings;
   5044 	bool		more = false;
   5045 	u32		eicr, eicr_mask;
   5046 
   5047 	/* Silicon errata #26 on 82598 */
   5048 	IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
   5049 
   5050 	eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
   5051 
   5052 	adapter->stats.pf.legint.ev_count++;
   5053 	++que->irqs.ev_count;
   5054 	if (eicr == 0) {
   5055 		adapter->stats.pf.intzero.ev_count++;
   5056 		if ((ifp->if_flags & IFF_UP) != 0)
   5057 			ixgbe_enable_intr(adapter);
   5058 		return 0;
   5059 	}
   5060 
   5061 	if ((ifp->if_flags & IFF_RUNNING) != 0) {
   5062 		/*
   5063 		 * The same as ixgbe_msix_que() about "que->txrx_use_workqueue".
   5064 		 */
   5065 		que->txrx_use_workqueue = adapter->txrx_use_workqueue;
   5066 
   5067 #ifdef __NetBSD__
   5068 		/* Don't run ixgbe_rxeof in interrupt context */
   5069 		more = true;
   5070 #else
   5071 		more = ixgbe_rxeof(que);
   5072 #endif
   5073 
   5074 		IXGBE_TX_LOCK(txr);
   5075 		ixgbe_txeof(txr);
   5076 #ifdef notyet
   5077 		if (!ixgbe_ring_empty(ifp, txr->br))
   5078 			ixgbe_start_locked(ifp, txr);
   5079 #endif
   5080 		IXGBE_TX_UNLOCK(txr);
   5081 	}
   5082 
   5083 	/* Check for fan failure */
   5084 	if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL) {
   5085 		ixgbe_check_fan_failure(adapter, eicr, true);
   5086 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
   5087 	}
   5088 
   5089 	/* Link status change */
   5090 	if (eicr & IXGBE_EICR_LSC)
   5091 		softint_schedule(adapter->link_si);
   5092 
   5093 	if (ixgbe_is_sfp(hw)) {
   5094 		/* Pluggable optics-related interrupt */
   5095 		if (hw->mac.type >= ixgbe_mac_X540)
   5096 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
   5097 		else
   5098 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
   5099 
   5100 		if (eicr & eicr_mask) {
   5101 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
   5102 			softint_schedule(adapter->mod_si);
   5103 		}
   5104 
   5105 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
   5106 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
   5107 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
   5108 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
   5109 			softint_schedule(adapter->msf_si);
   5110 		}
   5111 	}
   5112 
   5113 	/* External PHY interrupt */
   5114 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
   5115 	    (eicr & IXGBE_EICR_GPI_SDP0_X540))
   5116 		softint_schedule(adapter->phy_si);
   5117 
   5118 	if (more) {
   5119 		que->req.ev_count++;
   5120 		ixgbe_sched_handle_que(adapter, que);
   5121 	} else
   5122 		ixgbe_enable_intr(adapter);
   5123 
   5124 	return 1;
   5125 } /* ixgbe_legacy_irq */
   5126 
   5127 /************************************************************************
   5128  * ixgbe_free_pciintr_resources
   5129  ************************************************************************/
   5130 static void
   5131 ixgbe_free_pciintr_resources(struct adapter *adapter)
   5132 {
   5133 	struct ix_queue *que = adapter->queues;
   5134 	int		rid;
   5135 
   5136 	/*
   5137 	 * Release all msix queue resources:
   5138 	 */
   5139 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   5140 		if (que->res != NULL) {
   5141 			pci_intr_disestablish(adapter->osdep.pc,
   5142 			    adapter->osdep.ihs[i]);
   5143 			adapter->osdep.ihs[i] = NULL;
   5144 		}
   5145 	}
   5146 
   5147 	/* Clean the Legacy or Link interrupt last */
   5148 	if (adapter->vector) /* we are doing MSIX */
   5149 		rid = adapter->vector;
   5150 	else
   5151 		rid = 0;
   5152 
   5153 	if (adapter->osdep.ihs[rid] != NULL) {
   5154 		pci_intr_disestablish(adapter->osdep.pc,
   5155 		    adapter->osdep.ihs[rid]);
   5156 		adapter->osdep.ihs[rid] = NULL;
   5157 	}
   5158 
   5159 	if (adapter->osdep.intrs != NULL) {
   5160 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
   5161 		    adapter->osdep.nintrs);
   5162 		adapter->osdep.intrs = NULL;
   5163 	}
   5164 } /* ixgbe_free_pciintr_resources */
   5165 
   5166 /************************************************************************
   5167  * ixgbe_free_pci_resources
   5168  ************************************************************************/
   5169 static void
   5170 ixgbe_free_pci_resources(struct adapter *adapter)
   5171 {
   5172 
   5173 	ixgbe_free_pciintr_resources(adapter);
   5174 
   5175 	if (adapter->osdep.mem_size != 0) {
   5176 		bus_space_unmap(adapter->osdep.mem_bus_space_tag,
   5177 		    adapter->osdep.mem_bus_space_handle,
   5178 		    adapter->osdep.mem_size);
   5179 	}
   5180 
   5181 } /* ixgbe_free_pci_resources */
   5182 
   5183 /************************************************************************
   5184  * ixgbe_set_sysctl_value
   5185  ************************************************************************/
   5186 static void
   5187 ixgbe_set_sysctl_value(struct adapter *adapter, const char *name,
   5188     const char *description, int *limit, int value)
   5189 {
   5190 	device_t dev =	adapter->dev;
   5191 	struct sysctllog **log;
   5192 	const struct sysctlnode *rnode, *cnode;
   5193 
   5194 	/*
   5195 	 * It's not required to check recovery mode because this function never
   5196 	 * touches hardware.
   5197 	 */
   5198 
   5199 	log = &adapter->sysctllog;
   5200 	if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   5201 		aprint_error_dev(dev, "could not create sysctl root\n");
   5202 		return;
   5203 	}
   5204 	if (sysctl_createv(log, 0, &rnode, &cnode,
   5205 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   5206 	    name, SYSCTL_DESCR(description),
   5207 		NULL, 0, limit, 0, CTL_CREATE, CTL_EOL) != 0)
   5208 		aprint_error_dev(dev, "could not create sysctl\n");
   5209 	*limit = value;
   5210 } /* ixgbe_set_sysctl_value */
   5211 
   5212 /************************************************************************
   5213  * ixgbe_sysctl_flowcntl
   5214  *
   5215  *   SYSCTL wrapper around setting Flow Control
   5216  ************************************************************************/
   5217 static int
   5218 ixgbe_sysctl_flowcntl(SYSCTLFN_ARGS)
   5219 {
   5220 	struct sysctlnode node = *rnode;
   5221 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5222 	int error, fc;
   5223 
   5224 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   5225 		return (EPERM);
   5226 
   5227 	fc = adapter->hw.fc.current_mode;
   5228 	node.sysctl_data = &fc;
   5229 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5230 	if (error != 0 || newp == NULL)
   5231 		return error;
   5232 
   5233 	/* Don't bother if it's not changed */
   5234 	if (fc == adapter->hw.fc.current_mode)
   5235 		return (0);
   5236 
   5237 	return ixgbe_set_flowcntl(adapter, fc);
   5238 } /* ixgbe_sysctl_flowcntl */
   5239 
   5240 /************************************************************************
   5241  * ixgbe_set_flowcntl - Set flow control
   5242  *
   5243  *   Flow control values:
   5244  *     0 - off
   5245  *     1 - rx pause
   5246  *     2 - tx pause
   5247  *     3 - full
   5248  ************************************************************************/
   5249 static int
   5250 ixgbe_set_flowcntl(struct adapter *adapter, int fc)
   5251 {
   5252 	switch (fc) {
   5253 		case ixgbe_fc_rx_pause:
   5254 		case ixgbe_fc_tx_pause:
   5255 		case ixgbe_fc_full:
   5256 			adapter->hw.fc.requested_mode = fc;
   5257 			if (adapter->num_queues > 1)
   5258 				ixgbe_disable_rx_drop(adapter);
   5259 			break;
   5260 		case ixgbe_fc_none:
   5261 			adapter->hw.fc.requested_mode = ixgbe_fc_none;
   5262 			if (adapter->num_queues > 1)
   5263 				ixgbe_enable_rx_drop(adapter);
   5264 			break;
   5265 		default:
   5266 			return (EINVAL);
   5267 	}
   5268 
   5269 #if 0 /* XXX NetBSD */
   5270 	/* Don't autoneg if forcing a value */
   5271 	adapter->hw.fc.disable_fc_autoneg = TRUE;
   5272 #endif
   5273 	ixgbe_fc_enable(&adapter->hw);
   5274 
   5275 	return (0);
   5276 } /* ixgbe_set_flowcntl */
   5277 
   5278 /************************************************************************
   5279  * ixgbe_enable_rx_drop
   5280  *
   5281  *   Enable the hardware to drop packets when the buffer is
   5282  *   full. This is useful with multiqueue, so that no single
   5283  *   queue being full stalls the entire RX engine. We only
   5284  *   enable this when Multiqueue is enabled AND Flow Control
   5285  *   is disabled.
   5286  ************************************************************************/
   5287 static void
   5288 ixgbe_enable_rx_drop(struct adapter *adapter)
   5289 {
   5290 	struct ixgbe_hw *hw = &adapter->hw;
   5291 	struct rx_ring	*rxr;
   5292 	u32		srrctl;
   5293 
   5294 	for (int i = 0; i < adapter->num_queues; i++) {
   5295 		rxr = &adapter->rx_rings[i];
   5296 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
   5297 		srrctl |= IXGBE_SRRCTL_DROP_EN;
   5298 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
   5299 	}
   5300 
   5301 	/* enable drop for each vf */
   5302 	for (int i = 0; i < adapter->num_vfs; i++) {
   5303 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
   5304 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT) |
   5305 		    IXGBE_QDE_ENABLE));
   5306 	}
   5307 } /* ixgbe_enable_rx_drop */
   5308 
   5309 /************************************************************************
   5310  * ixgbe_disable_rx_drop
   5311  ************************************************************************/
   5312 static void
   5313 ixgbe_disable_rx_drop(struct adapter *adapter)
   5314 {
   5315 	struct ixgbe_hw *hw = &adapter->hw;
   5316 	struct rx_ring	*rxr;
   5317 	u32		srrctl;
   5318 
   5319 	for (int i = 0; i < adapter->num_queues; i++) {
   5320 		rxr = &adapter->rx_rings[i];
   5321 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
   5322 		srrctl &= ~IXGBE_SRRCTL_DROP_EN;
   5323 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
   5324 	}
   5325 
   5326 	/* disable drop for each vf */
   5327 	for (int i = 0; i < adapter->num_vfs; i++) {
   5328 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
   5329 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT)));
   5330 	}
   5331 } /* ixgbe_disable_rx_drop */
   5332 
   5333 /************************************************************************
   5334  * ixgbe_sysctl_advertise
   5335  *
   5336  *   SYSCTL wrapper around setting advertised speed
   5337  ************************************************************************/
   5338 static int
   5339 ixgbe_sysctl_advertise(SYSCTLFN_ARGS)
   5340 {
   5341 	struct sysctlnode node = *rnode;
   5342 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5343 	int	       error = 0, advertise;
   5344 
   5345 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   5346 		return (EPERM);
   5347 
   5348 	advertise = adapter->advertise;
   5349 	node.sysctl_data = &advertise;
   5350 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5351 	if (error != 0 || newp == NULL)
   5352 		return error;
   5353 
   5354 	return ixgbe_set_advertise(adapter, advertise);
   5355 } /* ixgbe_sysctl_advertise */
   5356 
   5357 /************************************************************************
   5358  * ixgbe_set_advertise - Control advertised link speed
   5359  *
   5360  *   Flags:
   5361  *     0x00 - Default (all capable link speed)
   5362  *     0x01 - advertise 100 Mb
   5363  *     0x02 - advertise 1G
   5364  *     0x04 - advertise 10G
   5365  *     0x08 - advertise 10 Mb
   5366  *     0x10 - advertise 2.5G
   5367  *     0x20 - advertise 5G
   5368  ************************************************************************/
   5369 static int
   5370 ixgbe_set_advertise(struct adapter *adapter, int advertise)
   5371 {
   5372 	device_t	 dev;
   5373 	struct ixgbe_hw	 *hw;
   5374 	ixgbe_link_speed speed = 0;
   5375 	ixgbe_link_speed link_caps = 0;
   5376 	s32		 err = IXGBE_NOT_IMPLEMENTED;
   5377 	bool		 negotiate = FALSE;
   5378 
   5379 	/* Checks to validate new value */
   5380 	if (adapter->advertise == advertise) /* no change */
   5381 		return (0);
   5382 
   5383 	dev = adapter->dev;
   5384 	hw = &adapter->hw;
   5385 
   5386 	/* No speed changes for backplane media */
   5387 	if (hw->phy.media_type == ixgbe_media_type_backplane)
   5388 		return (ENODEV);
   5389 
   5390 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
   5391 	    (hw->phy.multispeed_fiber))) {
   5392 		device_printf(dev,
   5393 		    "Advertised speed can only be set on copper or "
   5394 		    "multispeed fiber media types.\n");
   5395 		return (EINVAL);
   5396 	}
   5397 
   5398 	if (advertise < 0x0 || advertise > 0x3f) {
   5399 		device_printf(dev, "Invalid advertised speed; valid modes are 0x0 through 0x3f\n");
   5400 		return (EINVAL);
   5401 	}
   5402 
   5403 	if (hw->mac.ops.get_link_capabilities) {
   5404 		err = hw->mac.ops.get_link_capabilities(hw, &link_caps,
   5405 		    &negotiate);
   5406 		if (err != IXGBE_SUCCESS) {
   5407 			device_printf(dev, "Unable to determine supported advertise speeds\n");
   5408 			return (ENODEV);
   5409 		}
   5410 	}
   5411 
   5412 	/* Set new value and report new advertised mode */
   5413 	if (advertise & 0x1) {
   5414 		if (!(link_caps & IXGBE_LINK_SPEED_100_FULL)) {
   5415 			device_printf(dev, "Interface does not support 100Mb advertised speed\n");
   5416 			return (EINVAL);
   5417 		}
   5418 		speed |= IXGBE_LINK_SPEED_100_FULL;
   5419 	}
   5420 	if (advertise & 0x2) {
   5421 		if (!(link_caps & IXGBE_LINK_SPEED_1GB_FULL)) {
   5422 			device_printf(dev, "Interface does not support 1Gb advertised speed\n");
   5423 			return (EINVAL);
   5424 		}
   5425 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
   5426 	}
   5427 	if (advertise & 0x4) {
   5428 		if (!(link_caps & IXGBE_LINK_SPEED_10GB_FULL)) {
   5429 			device_printf(dev, "Interface does not support 10Gb advertised speed\n");
   5430 			return (EINVAL);
   5431 		}
   5432 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
   5433 	}
   5434 	if (advertise & 0x8) {
   5435 		if (!(link_caps & IXGBE_LINK_SPEED_10_FULL)) {
   5436 			device_printf(dev, "Interface does not support 10Mb advertised speed\n");
   5437 			return (EINVAL);
   5438 		}
   5439 		speed |= IXGBE_LINK_SPEED_10_FULL;
   5440 	}
   5441 	if (advertise & 0x10) {
   5442 		if (!(link_caps & IXGBE_LINK_SPEED_2_5GB_FULL)) {
   5443 			device_printf(dev, "Interface does not support 2.5Gb advertised speed\n");
   5444 			return (EINVAL);
   5445 		}
   5446 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
   5447 	}
   5448 	if (advertise & 0x20) {
   5449 		if (!(link_caps & IXGBE_LINK_SPEED_5GB_FULL)) {
   5450 			device_printf(dev, "Interface does not support 5Gb advertised speed\n");
   5451 			return (EINVAL);
   5452 		}
   5453 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
   5454 	}
   5455 	if (advertise == 0)
   5456 		speed = link_caps; /* All capable link speed */
   5457 
   5458 	hw->mac.autotry_restart = TRUE;
   5459 	hw->mac.ops.setup_link(hw, speed, TRUE);
   5460 	adapter->advertise = advertise;
   5461 
   5462 	return (0);
   5463 } /* ixgbe_set_advertise */
   5464 
   5465 /************************************************************************
   5466  * ixgbe_get_advertise - Get current advertised speed settings
   5467  *
   5468  *   Formatted for sysctl usage.
   5469  *   Flags:
   5470  *     0x01 - advertise 100 Mb
   5471  *     0x02 - advertise 1G
   5472  *     0x04 - advertise 10G
   5473  *     0x08 - advertise 10 Mb (yes, Mb)
   5474  *     0x10 - advertise 2.5G
   5475  *     0x20 - advertise 5G
   5476  ************************************************************************/
   5477 static int
   5478 ixgbe_get_advertise(struct adapter *adapter)
   5479 {
   5480 	struct ixgbe_hw	 *hw = &adapter->hw;
   5481 	int		 speed;
   5482 	ixgbe_link_speed link_caps = 0;
   5483 	s32		 err;
   5484 	bool		 negotiate = FALSE;
   5485 
   5486 	/*
   5487 	 * Advertised speed means nothing unless it's copper or
   5488 	 * multi-speed fiber
   5489 	 */
   5490 	if (!(hw->phy.media_type == ixgbe_media_type_copper) &&
   5491 	    !(hw->phy.multispeed_fiber))
   5492 		return (0);
   5493 
   5494 	err = hw->mac.ops.get_link_capabilities(hw, &link_caps, &negotiate);
   5495 	if (err != IXGBE_SUCCESS)
   5496 		return (0);
   5497 
   5498 	speed =
   5499 	    ((link_caps & IXGBE_LINK_SPEED_10GB_FULL)  ? 0x04 : 0) |
   5500 	    ((link_caps & IXGBE_LINK_SPEED_1GB_FULL)   ? 0x02 : 0) |
   5501 	    ((link_caps & IXGBE_LINK_SPEED_100_FULL)   ? 0x01 : 0) |
   5502 	    ((link_caps & IXGBE_LINK_SPEED_10_FULL)    ? 0x08 : 0) |
   5503 	    ((link_caps & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
   5504 	    ((link_caps & IXGBE_LINK_SPEED_5GB_FULL)   ? 0x20 : 0);
   5505 
   5506 	return speed;
   5507 } /* ixgbe_get_advertise */
   5508 
   5509 /************************************************************************
   5510  * ixgbe_sysctl_dmac - Manage DMA Coalescing
   5511  *
   5512  *   Control values:
   5513  *     0/1 - off / on (use default value of 1000)
   5514  *
   5515  *     Legal timer values are:
   5516  *     50,100,250,500,1000,2000,5000,10000
   5517  *
   5518  *     Turning off interrupt moderation will also turn this off.
   5519  ************************************************************************/
   5520 static int
   5521 ixgbe_sysctl_dmac(SYSCTLFN_ARGS)
   5522 {
   5523 	struct sysctlnode node = *rnode;
   5524 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5525 	struct ifnet   *ifp = adapter->ifp;
   5526 	int	       error;
   5527 	int	       newval;
   5528 
   5529 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   5530 		return (EPERM);
   5531 
   5532 	newval = adapter->dmac;
   5533 	node.sysctl_data = &newval;
   5534 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5535 	if ((error) || (newp == NULL))
   5536 		return (error);
   5537 
   5538 	switch (newval) {
   5539 	case 0:
   5540 		/* Disabled */
   5541 		adapter->dmac = 0;
   5542 		break;
   5543 	case 1:
   5544 		/* Enable and use default */
   5545 		adapter->dmac = 1000;
   5546 		break;
   5547 	case 50:
   5548 	case 100:
   5549 	case 250:
   5550 	case 500:
   5551 	case 1000:
   5552 	case 2000:
   5553 	case 5000:
   5554 	case 10000:
   5555 		/* Legal values - allow */
   5556 		adapter->dmac = newval;
   5557 		break;
   5558 	default:
   5559 		/* Do nothing, illegal value */
   5560 		return (EINVAL);
   5561 	}
   5562 
   5563 	/* Re-initialize hardware if it's already running */
   5564 	if (ifp->if_flags & IFF_RUNNING)
   5565 		ifp->if_init(ifp);
   5566 
   5567 	return (0);
   5568 }
   5569 
   5570 #ifdef IXGBE_DEBUG
   5571 /************************************************************************
   5572  * ixgbe_sysctl_power_state
   5573  *
   5574  *   Sysctl to test power states
   5575  *   Values:
   5576  *     0      - set device to D0
   5577  *     3      - set device to D3
   5578  *     (none) - get current device power state
   5579  ************************************************************************/
   5580 static int
   5581 ixgbe_sysctl_power_state(SYSCTLFN_ARGS)
   5582 {
   5583 #ifdef notyet
   5584 	struct sysctlnode node = *rnode;
   5585 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5586 	device_t       dev =  adapter->dev;
   5587 	int	       curr_ps, new_ps, error = 0;
   5588 
   5589 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   5590 		return (EPERM);
   5591 
   5592 	curr_ps = new_ps = pci_get_powerstate(dev);
   5593 
   5594 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5595 	if ((error) || (req->newp == NULL))
   5596 		return (error);
   5597 
   5598 	if (new_ps == curr_ps)
   5599 		return (0);
   5600 
   5601 	if (new_ps == 3 && curr_ps == 0)
   5602 		error = DEVICE_SUSPEND(dev);
   5603 	else if (new_ps == 0 && curr_ps == 3)
   5604 		error = DEVICE_RESUME(dev);
   5605 	else
   5606 		return (EINVAL);
   5607 
   5608 	device_printf(dev, "New state: %d\n", pci_get_powerstate(dev));
   5609 
   5610 	return (error);
   5611 #else
   5612 	return 0;
   5613 #endif
   5614 } /* ixgbe_sysctl_power_state */
   5615 #endif
   5616 
   5617 /************************************************************************
   5618  * ixgbe_sysctl_wol_enable
   5619  *
   5620  *   Sysctl to enable/disable the WoL capability,
   5621  *   if supported by the adapter.
   5622  *
   5623  *   Values:
   5624  *     0 - disabled
   5625  *     1 - enabled
   5626  ************************************************************************/
   5627 static int
   5628 ixgbe_sysctl_wol_enable(SYSCTLFN_ARGS)
   5629 {
   5630 	struct sysctlnode node = *rnode;
   5631 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   5632 	struct ixgbe_hw *hw = &adapter->hw;
   5633 	bool		new_wol_enabled;
   5634 	int		error = 0;
   5635 
   5636 	/*
   5637 	 * It's not required to check recovery mode because this function never
   5638 	 * touches hardware.
   5639 	 */
   5640 	new_wol_enabled = hw->wol_enabled;
   5641 	node.sysctl_data = &new_wol_enabled;
   5642 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5643 	if ((error) || (newp == NULL))
   5644 		return (error);
   5645 	if (new_wol_enabled == hw->wol_enabled)
   5646 		return (0);
   5647 
   5648 	if (new_wol_enabled && !adapter->wol_support)
   5649 		return (ENODEV);
   5650 	else
   5651 		hw->wol_enabled = new_wol_enabled;
   5652 
   5653 	return (0);
   5654 } /* ixgbe_sysctl_wol_enable */
   5655 
   5656 /************************************************************************
   5657  * ixgbe_sysctl_wufc - Wake Up Filter Control
   5658  *
   5659  *   Sysctl to enable/disable the types of packets that the
   5660  *   adapter will wake up on upon receipt.
   5661  *   Flags:
   5662  *     0x1  - Link Status Change
   5663  *     0x2  - Magic Packet
   5664  *     0x4  - Direct Exact
   5665  *     0x8  - Directed Multicast
   5666  *     0x10 - Broadcast
   5667  *     0x20 - ARP/IPv4 Request Packet
   5668  *     0x40 - Direct IPv4 Packet
   5669  *     0x80 - Direct IPv6 Packet
   5670  *
   5671  *   Settings not listed above will cause the sysctl to return an error.
   5672  ************************************************************************/
   5673 static int
   5674 ixgbe_sysctl_wufc(SYSCTLFN_ARGS)
   5675 {
   5676 	struct sysctlnode node = *rnode;
   5677 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5678 	int error = 0;
   5679 	u32 new_wufc;
   5680 
   5681 	/*
   5682 	 * It's not required to check recovery mode because this function never
   5683 	 * touches hardware.
   5684 	 */
   5685 	new_wufc = adapter->wufc;
   5686 	node.sysctl_data = &new_wufc;
   5687 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5688 	if ((error) || (newp == NULL))
   5689 		return (error);
   5690 	if (new_wufc == adapter->wufc)
   5691 		return (0);
   5692 
   5693 	if (new_wufc & 0xffffff00)
   5694 		return (EINVAL);
   5695 
   5696 	new_wufc &= 0xff;
   5697 	new_wufc |= (0xffffff & adapter->wufc);
   5698 	adapter->wufc = new_wufc;
   5699 
   5700 	return (0);
   5701 } /* ixgbe_sysctl_wufc */
   5702 
   5703 #ifdef IXGBE_DEBUG
   5704 /************************************************************************
   5705  * ixgbe_sysctl_print_rss_config
   5706  ************************************************************************/
   5707 static int
   5708 ixgbe_sysctl_print_rss_config(SYSCTLFN_ARGS)
   5709 {
   5710 #ifdef notyet
   5711 	struct sysctlnode node = *rnode;
   5712 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   5713 	struct ixgbe_hw *hw = &adapter->hw;
   5714 	device_t	dev = adapter->dev;
   5715 	struct sbuf	*buf;
   5716 	int		error = 0, reta_size;
   5717 	u32		reg;
   5718 
   5719 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   5720 		return (EPERM);
   5721 
   5722 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
   5723 	if (!buf) {
   5724 		device_printf(dev, "Could not allocate sbuf for output.\n");
   5725 		return (ENOMEM);
   5726 	}
   5727 
   5728 	// TODO: use sbufs to make a string to print out
   5729 	/* Set multiplier for RETA setup and table size based on MAC */
   5730 	switch (adapter->hw.mac.type) {
   5731 	case ixgbe_mac_X550:
   5732 	case ixgbe_mac_X550EM_x:
   5733 	case ixgbe_mac_X550EM_a:
   5734 		reta_size = 128;
   5735 		break;
   5736 	default:
   5737 		reta_size = 32;
   5738 		break;
   5739 	}
   5740 
   5741 	/* Print out the redirection table */
   5742 	sbuf_cat(buf, "\n");
   5743 	for (int i = 0; i < reta_size; i++) {
   5744 		if (i < 32) {
   5745 			reg = IXGBE_READ_REG(hw, IXGBE_RETA(i));
   5746 			sbuf_printf(buf, "RETA(%2d): 0x%08x\n", i, reg);
   5747 		} else {
   5748 			reg = IXGBE_READ_REG(hw, IXGBE_ERETA(i - 32));
   5749 			sbuf_printf(buf, "ERETA(%2d): 0x%08x\n", i - 32, reg);
   5750 		}
   5751 	}
   5752 
   5753 	// TODO: print more config
   5754 
   5755 	error = sbuf_finish(buf);
   5756 	if (error)
   5757 		device_printf(dev, "Error finishing sbuf: %d\n", error);
   5758 
   5759 	sbuf_delete(buf);
   5760 #endif
   5761 	return (0);
   5762 } /* ixgbe_sysctl_print_rss_config */
   5763 #endif /* IXGBE_DEBUG */
   5764 
   5765 /************************************************************************
   5766  * ixgbe_sysctl_phy_temp - Retrieve temperature of PHY
   5767  *
   5768  *   For X552/X557-AT devices using an external PHY
   5769  ************************************************************************/
   5770 static int
   5771 ixgbe_sysctl_phy_temp(SYSCTLFN_ARGS)
   5772 {
   5773 	struct sysctlnode node = *rnode;
   5774 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   5775 	struct ixgbe_hw *hw = &adapter->hw;
   5776 	int val;
   5777 	u16 reg;
   5778 	int		error;
   5779 
   5780 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   5781 		return (EPERM);
   5782 
   5783 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
   5784 		device_printf(adapter->dev,
   5785 		    "Device has no supported external thermal sensor.\n");
   5786 		return (ENODEV);
   5787 	}
   5788 
   5789 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_CURRENT_TEMP,
   5790 		IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
   5791 		device_printf(adapter->dev,
   5792 		    "Error reading from PHY's current temperature register\n");
   5793 		return (EAGAIN);
   5794 	}
   5795 
   5796 	node.sysctl_data = &val;
   5797 
   5798 	/* Shift temp for output */
   5799 	val = reg >> 8;
   5800 
   5801 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5802 	if ((error) || (newp == NULL))
   5803 		return (error);
   5804 
   5805 	return (0);
   5806 } /* ixgbe_sysctl_phy_temp */
   5807 
   5808 /************************************************************************
   5809  * ixgbe_sysctl_phy_overtemp_occurred
   5810  *
   5811  *   Reports (directly from the PHY) whether the current PHY
   5812  *   temperature is over the overtemp threshold.
   5813  ************************************************************************/
   5814 static int
   5815 ixgbe_sysctl_phy_overtemp_occurred(SYSCTLFN_ARGS)
   5816 {
   5817 	struct sysctlnode node = *rnode;
   5818 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   5819 	struct ixgbe_hw *hw = &adapter->hw;
   5820 	int val, error;
   5821 	u16 reg;
   5822 
   5823 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   5824 		return (EPERM);
   5825 
   5826 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
   5827 		device_printf(adapter->dev,
   5828 		    "Device has no supported external thermal sensor.\n");
   5829 		return (ENODEV);
   5830 	}
   5831 
   5832 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_OVERTEMP_STATUS,
   5833 		IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
   5834 		device_printf(adapter->dev,
   5835 		    "Error reading from PHY's temperature status register\n");
   5836 		return (EAGAIN);
   5837 	}
   5838 
   5839 	node.sysctl_data = &val;
   5840 
   5841 	/* Get occurrence bit */
   5842 	val = !!(reg & 0x4000);
   5843 
   5844 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5845 	if ((error) || (newp == NULL))
   5846 		return (error);
   5847 
   5848 	return (0);
   5849 } /* ixgbe_sysctl_phy_overtemp_occurred */
   5850 
   5851 /************************************************************************
   5852  * ixgbe_sysctl_eee_state
   5853  *
   5854  *   Sysctl to set EEE power saving feature
   5855  *   Values:
   5856  *     0      - disable EEE
   5857  *     1      - enable EEE
   5858  *     (none) - get current device EEE state
   5859  ************************************************************************/
   5860 static int
   5861 ixgbe_sysctl_eee_state(SYSCTLFN_ARGS)
   5862 {
   5863 	struct sysctlnode node = *rnode;
   5864 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5865 	struct ifnet   *ifp = adapter->ifp;
   5866 	device_t       dev = adapter->dev;
   5867 	int	       curr_eee, new_eee, error = 0;
   5868 	s32	       retval;
   5869 
   5870 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   5871 		return (EPERM);
   5872 
   5873 	curr_eee = new_eee = !!(adapter->feat_en & IXGBE_FEATURE_EEE);
   5874 	node.sysctl_data = &new_eee;
   5875 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5876 	if ((error) || (newp == NULL))
   5877 		return (error);
   5878 
   5879 	/* Nothing to do */
   5880 	if (new_eee == curr_eee)
   5881 		return (0);
   5882 
   5883 	/* Not supported */
   5884 	if (!(adapter->feat_cap & IXGBE_FEATURE_EEE))
   5885 		return (EINVAL);
   5886 
   5887 	/* Bounds checking */
   5888 	if ((new_eee < 0) || (new_eee > 1))
   5889 		return (EINVAL);
   5890 
   5891 	retval = ixgbe_setup_eee(&adapter->hw, new_eee);
   5892 	if (retval) {
   5893 		device_printf(dev, "Error in EEE setup: 0x%08X\n", retval);
   5894 		return (EINVAL);
   5895 	}
   5896 
   5897 	/* Restart auto-neg */
   5898 	ifp->if_init(ifp);
   5899 
   5900 	device_printf(dev, "New EEE state: %d\n", new_eee);
   5901 
   5902 	/* Cache new value */
   5903 	if (new_eee)
   5904 		adapter->feat_en |= IXGBE_FEATURE_EEE;
   5905 	else
   5906 		adapter->feat_en &= ~IXGBE_FEATURE_EEE;
   5907 
   5908 	return (error);
   5909 } /* ixgbe_sysctl_eee_state */
   5910 
   5911 #define PRINTQS(adapter, regname)					\
   5912 	do {								\
   5913 		struct ixgbe_hw	*_hw = &(adapter)->hw;			\
   5914 		int _i;							\
   5915 									\
   5916 		printf("%s: %s", device_xname((adapter)->dev), #regname); \
   5917 		for (_i = 0; _i < (adapter)->num_queues; _i++) {	\
   5918 			printf((_i == 0) ? "\t" : " ");			\
   5919 			printf("%08x", IXGBE_READ_REG(_hw,		\
   5920 				IXGBE_##regname(_i)));			\
   5921 		}							\
   5922 		printf("\n");						\
   5923 	} while (0)
   5924 
   5925 /************************************************************************
   5926  * ixgbe_print_debug_info
   5927  *
   5928  *   Called only when em_display_debug_stats is enabled.
   5929  *   Provides a way to take a look at important statistics
   5930  *   maintained by the driver and hardware.
   5931  ************************************************************************/
   5932 static void
   5933 ixgbe_print_debug_info(struct adapter *adapter)
   5934 {
   5935 	device_t	dev = adapter->dev;
   5936 	struct ixgbe_hw *hw = &adapter->hw;
   5937 	int table_size;
   5938 	int i;
   5939 
   5940 	switch (adapter->hw.mac.type) {
   5941 	case ixgbe_mac_X550:
   5942 	case ixgbe_mac_X550EM_x:
   5943 	case ixgbe_mac_X550EM_a:
   5944 		table_size = 128;
   5945 		break;
   5946 	default:
   5947 		table_size = 32;
   5948 		break;
   5949 	}
   5950 
   5951 	device_printf(dev, "[E]RETA:\n");
   5952 	for (i = 0; i < table_size; i++) {
   5953 		if (i < 32)
   5954 			printf("%02x: %08x\n", i, IXGBE_READ_REG(hw,
   5955 				IXGBE_RETA(i)));
   5956 		else
   5957 			printf("%02x: %08x\n", i, IXGBE_READ_REG(hw,
   5958 				IXGBE_ERETA(i - 32)));
   5959 	}
   5960 
   5961 	device_printf(dev, "queue:");
   5962 	for (i = 0; i < adapter->num_queues; i++) {
   5963 		printf((i == 0) ? "\t" : " ");
   5964 		printf("%8d", i);
   5965 	}
   5966 	printf("\n");
   5967 	PRINTQS(adapter, RDBAL);
   5968 	PRINTQS(adapter, RDBAH);
   5969 	PRINTQS(adapter, RDLEN);
   5970 	PRINTQS(adapter, SRRCTL);
   5971 	PRINTQS(adapter, RDH);
   5972 	PRINTQS(adapter, RDT);
   5973 	PRINTQS(adapter, RXDCTL);
   5974 
   5975 	device_printf(dev, "RQSMR:");
   5976 	for (i = 0; i < adapter->num_queues / 4; i++) {
   5977 		printf((i == 0) ? "\t" : " ");
   5978 		printf("%08x", IXGBE_READ_REG(hw, IXGBE_RQSMR(i)));
   5979 	}
   5980 	printf("\n");
   5981 
   5982 	device_printf(dev, "disabled_count:");
   5983 	for (i = 0; i < adapter->num_queues; i++) {
   5984 		printf((i == 0) ? "\t" : " ");
   5985 		printf("%8d", adapter->queues[i].disabled_count);
   5986 	}
   5987 	printf("\n");
   5988 
   5989 	device_printf(dev, "EIMS:\t%08x\n", IXGBE_READ_REG(hw, IXGBE_EIMS));
   5990 	if (hw->mac.type != ixgbe_mac_82598EB) {
   5991 		device_printf(dev, "EIMS_EX(0):\t%08x\n",
   5992 			      IXGBE_READ_REG(hw, IXGBE_EIMS_EX(0)));
   5993 		device_printf(dev, "EIMS_EX(1):\t%08x\n",
   5994 			      IXGBE_READ_REG(hw, IXGBE_EIMS_EX(1)));
   5995 	}
   5996 } /* ixgbe_print_debug_info */
   5997 
   5998 /************************************************************************
   5999  * ixgbe_sysctl_debug
   6000  ************************************************************************/
   6001 static int
   6002 ixgbe_sysctl_debug(SYSCTLFN_ARGS)
   6003 {
   6004 	struct sysctlnode node = *rnode;
   6005 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   6006 	int	       error, result = 0;
   6007 
   6008 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   6009 		return (EPERM);
   6010 
   6011 	node.sysctl_data = &result;
   6012 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   6013 
   6014 	if (error || newp == NULL)
   6015 		return error;
   6016 
   6017 	if (result == 1)
   6018 		ixgbe_print_debug_info(adapter);
   6019 
   6020 	return 0;
   6021 } /* ixgbe_sysctl_debug */
   6022 
   6023 /************************************************************************
   6024  * ixgbe_sysctl_rx_copy_len
   6025  ************************************************************************/
   6026 static int
   6027 ixgbe_sysctl_rx_copy_len(SYSCTLFN_ARGS)
   6028 {
   6029 	struct sysctlnode node = *rnode;
   6030 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   6031 	int error;
   6032 	int result = adapter->rx_copy_len;
   6033 
   6034 	node.sysctl_data = &result;
   6035 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   6036 
   6037 	if (error || newp == NULL)
   6038 		return error;
   6039 
   6040 	if ((result < 0) || (result > IXGBE_RX_COPY_LEN_MAX))
   6041 		return EINVAL;
   6042 
   6043 	adapter->rx_copy_len = result;
   6044 
   6045 	return 0;
   6046 } /* ixgbe_sysctl_rx_copy_len */
   6047 
   6048 /************************************************************************
   6049  * ixgbe_init_device_features
   6050  ************************************************************************/
   6051 static void
   6052 ixgbe_init_device_features(struct adapter *adapter)
   6053 {
   6054 	adapter->feat_cap = IXGBE_FEATURE_NETMAP
   6055 			  | IXGBE_FEATURE_RSS
   6056 			  | IXGBE_FEATURE_MSI
   6057 			  | IXGBE_FEATURE_MSIX
   6058 			  | IXGBE_FEATURE_LEGACY_IRQ
   6059 			  | IXGBE_FEATURE_LEGACY_TX;
   6060 
   6061 	/* Set capabilities first... */
   6062 	switch (adapter->hw.mac.type) {
   6063 	case ixgbe_mac_82598EB:
   6064 		if (adapter->hw.device_id == IXGBE_DEV_ID_82598AT)
   6065 			adapter->feat_cap |= IXGBE_FEATURE_FAN_FAIL;
   6066 		break;
   6067 	case ixgbe_mac_X540:
   6068 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
   6069 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
   6070 		if ((adapter->hw.device_id == IXGBE_DEV_ID_X540_BYPASS) &&
   6071 		    (adapter->hw.bus.func == 0))
   6072 			adapter->feat_cap |= IXGBE_FEATURE_BYPASS;
   6073 		break;
   6074 	case ixgbe_mac_X550:
   6075 		/*
   6076 		 * IXGBE_FEATURE_RECOVERY_MODE will be set after reading
   6077 		 * NVM Image version.
   6078 		 */
   6079 		adapter->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
   6080 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
   6081 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
   6082 		break;
   6083 	case ixgbe_mac_X550EM_x:
   6084 		/*
   6085 		 * IXGBE_FEATURE_RECOVERY_MODE will be set after reading
   6086 		 * NVM Image version.
   6087 		 */
   6088 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
   6089 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
   6090 		break;
   6091 	case ixgbe_mac_X550EM_a:
   6092 		/*
   6093 		 * IXGBE_FEATURE_RECOVERY_MODE will be set after reading
   6094 		 * NVM Image version.
   6095 		 */
   6096 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
   6097 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
   6098 		adapter->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
   6099 		if ((adapter->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T) ||
   6100 		    (adapter->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T_L)) {
   6101 			adapter->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
   6102 			adapter->feat_cap |= IXGBE_FEATURE_EEE;
   6103 		}
   6104 		break;
   6105 	case ixgbe_mac_82599EB:
   6106 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
   6107 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
   6108 		if ((adapter->hw.device_id == IXGBE_DEV_ID_82599_BYPASS) &&
   6109 		    (adapter->hw.bus.func == 0))
   6110 			adapter->feat_cap |= IXGBE_FEATURE_BYPASS;
   6111 		if (adapter->hw.device_id == IXGBE_DEV_ID_82599_QSFP_SF_QP)
   6112 			adapter->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
   6113 		break;
   6114 	default:
   6115 		break;
   6116 	}
   6117 
   6118 	/* Enabled by default... */
   6119 	/* Fan failure detection */
   6120 	if (adapter->feat_cap & IXGBE_FEATURE_FAN_FAIL)
   6121 		adapter->feat_en |= IXGBE_FEATURE_FAN_FAIL;
   6122 	/* Netmap */
   6123 	if (adapter->feat_cap & IXGBE_FEATURE_NETMAP)
   6124 		adapter->feat_en |= IXGBE_FEATURE_NETMAP;
   6125 	/* EEE */
   6126 	if (adapter->feat_cap & IXGBE_FEATURE_EEE)
   6127 		adapter->feat_en |= IXGBE_FEATURE_EEE;
   6128 	/* Thermal Sensor */
   6129 	if (adapter->feat_cap & IXGBE_FEATURE_TEMP_SENSOR)
   6130 		adapter->feat_en |= IXGBE_FEATURE_TEMP_SENSOR;
   6131 	/*
   6132 	 * Recovery mode:
   6133 	 * NetBSD: IXGBE_FEATURE_RECOVERY_MODE will be controlled after reading
   6134 	 * NVM Image version.
   6135 	 */
   6136 
   6137 	/* Enabled via global sysctl... */
   6138 	/* Flow Director */
   6139 	if (ixgbe_enable_fdir) {
   6140 		if (adapter->feat_cap & IXGBE_FEATURE_FDIR)
   6141 			adapter->feat_en |= IXGBE_FEATURE_FDIR;
   6142 		else
   6143 			device_printf(adapter->dev, "Device does not support Flow Director. Leaving disabled.");
   6144 	}
   6145 	/* Legacy (single queue) transmit */
   6146 	if ((adapter->feat_cap & IXGBE_FEATURE_LEGACY_TX) &&
   6147 	    ixgbe_enable_legacy_tx)
   6148 		adapter->feat_en |= IXGBE_FEATURE_LEGACY_TX;
   6149 	/*
   6150 	 * Message Signal Interrupts - Extended (MSI-X)
   6151 	 * Normal MSI is only enabled if MSI-X calls fail.
   6152 	 */
   6153 	if (!ixgbe_enable_msix)
   6154 		adapter->feat_cap &= ~IXGBE_FEATURE_MSIX;
   6155 	/* Receive-Side Scaling (RSS) */
   6156 	if ((adapter->feat_cap & IXGBE_FEATURE_RSS) && ixgbe_enable_rss)
   6157 		adapter->feat_en |= IXGBE_FEATURE_RSS;
   6158 
   6159 	/* Disable features with unmet dependencies... */
   6160 	/* No MSI-X */
   6161 	if (!(adapter->feat_cap & IXGBE_FEATURE_MSIX)) {
   6162 		adapter->feat_cap &= ~IXGBE_FEATURE_RSS;
   6163 		adapter->feat_cap &= ~IXGBE_FEATURE_SRIOV;
   6164 		adapter->feat_en &= ~IXGBE_FEATURE_RSS;
   6165 		adapter->feat_en &= ~IXGBE_FEATURE_SRIOV;
   6166 	}
   6167 } /* ixgbe_init_device_features */
   6168 
   6169 /************************************************************************
   6170  * ixgbe_probe - Device identification routine
   6171  *
   6172  *   Determines if the driver should be loaded on
   6173  *   adapter based on its PCI vendor/device ID.
   6174  *
   6175  *   return BUS_PROBE_DEFAULT on success, positive on failure
   6176  ************************************************************************/
   6177 static int
   6178 ixgbe_probe(device_t dev, cfdata_t cf, void *aux)
   6179 {
   6180 	const struct pci_attach_args *pa = aux;
   6181 
   6182 	return (ixgbe_lookup(pa) != NULL) ? 1 : 0;
   6183 }
   6184 
   6185 static const ixgbe_vendor_info_t *
   6186 ixgbe_lookup(const struct pci_attach_args *pa)
   6187 {
   6188 	const ixgbe_vendor_info_t *ent;
   6189 	pcireg_t subid;
   6190 
   6191 	INIT_DEBUGOUT("ixgbe_lookup: begin");
   6192 
   6193 	if (PCI_VENDOR(pa->pa_id) != IXGBE_INTEL_VENDOR_ID)
   6194 		return NULL;
   6195 
   6196 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
   6197 
   6198 	for (ent = ixgbe_vendor_info_array; ent->vendor_id != 0; ent++) {
   6199 		if ((PCI_VENDOR(pa->pa_id) == ent->vendor_id) &&
   6200 		    (PCI_PRODUCT(pa->pa_id) == ent->device_id) &&
   6201 		    ((PCI_SUBSYS_VENDOR(subid) == ent->subvendor_id) ||
   6202 			(ent->subvendor_id == 0)) &&
   6203 		    ((PCI_SUBSYS_ID(subid) == ent->subdevice_id) ||
   6204 			(ent->subdevice_id == 0))) {
   6205 			return ent;
   6206 		}
   6207 	}
   6208 	return NULL;
   6209 }
   6210 
   6211 static int
   6212 ixgbe_ifflags_cb(struct ethercom *ec)
   6213 {
   6214 	struct ifnet *ifp = &ec->ec_if;
   6215 	struct adapter *adapter = ifp->if_softc;
   6216 	u_short change;
   6217 	int rv = 0;
   6218 
   6219 	IXGBE_CORE_LOCK(adapter);
   6220 
   6221 	change = ifp->if_flags ^ adapter->if_flags;
   6222 	if (change != 0)
   6223 		adapter->if_flags = ifp->if_flags;
   6224 
   6225 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
   6226 		rv = ENETRESET;
   6227 		goto out;
   6228 	} else if ((change & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
   6229 		ixgbe_set_rxfilter(adapter);
   6230 
   6231 	/* Set up VLAN support and filter */
   6232 	ixgbe_setup_vlan_hw_support(adapter);
   6233 
   6234 out:
   6235 	IXGBE_CORE_UNLOCK(adapter);
   6236 
   6237 	return rv;
   6238 }
   6239 
   6240 /************************************************************************
   6241  * ixgbe_ioctl - Ioctl entry point
   6242  *
   6243  *   Called when the user wants to configure the interface.
   6244  *
   6245  *   return 0 on success, positive on failure
   6246  ************************************************************************/
   6247 static int
   6248 ixgbe_ioctl(struct ifnet *ifp, u_long command, void *data)
   6249 {
   6250 	struct adapter	*adapter = ifp->if_softc;
   6251 	struct ixgbe_hw *hw = &adapter->hw;
   6252 	struct ifcapreq *ifcr = data;
   6253 	struct ifreq	*ifr = data;
   6254 	int		error = 0;
   6255 	int l4csum_en;
   6256 	const int l4csum = IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
   6257 	     IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
   6258 
   6259 	if (ixgbe_fw_recovery_mode_swflag(adapter))
   6260 		return (EPERM);
   6261 
   6262 	switch (command) {
   6263 	case SIOCSIFFLAGS:
   6264 		IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
   6265 		break;
   6266 	case SIOCADDMULTI:
   6267 	case SIOCDELMULTI:
   6268 		IOCTL_DEBUGOUT("ioctl: SIOC(ADD|DEL)MULTI");
   6269 		break;
   6270 	case SIOCSIFMEDIA:
   6271 	case SIOCGIFMEDIA:
   6272 		IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
   6273 		break;
   6274 	case SIOCSIFCAP:
   6275 		IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
   6276 		break;
   6277 	case SIOCSIFMTU:
   6278 		IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
   6279 		break;
   6280 #ifdef __NetBSD__
   6281 	case SIOCINITIFADDR:
   6282 		IOCTL_DEBUGOUT("ioctl: SIOCINITIFADDR");
   6283 		break;
   6284 	case SIOCGIFFLAGS:
   6285 		IOCTL_DEBUGOUT("ioctl: SIOCGIFFLAGS");
   6286 		break;
   6287 	case SIOCGIFAFLAG_IN:
   6288 		IOCTL_DEBUGOUT("ioctl: SIOCGIFAFLAG_IN");
   6289 		break;
   6290 	case SIOCGIFADDR:
   6291 		IOCTL_DEBUGOUT("ioctl: SIOCGIFADDR");
   6292 		break;
   6293 	case SIOCGIFMTU:
   6294 		IOCTL_DEBUGOUT("ioctl: SIOCGIFMTU (Get Interface MTU)");
   6295 		break;
   6296 	case SIOCGIFCAP:
   6297 		IOCTL_DEBUGOUT("ioctl: SIOCGIFCAP (Get IF cap)");
   6298 		break;
   6299 	case SIOCGETHERCAP:
   6300 		IOCTL_DEBUGOUT("ioctl: SIOCGETHERCAP (Get ethercap)");
   6301 		break;
   6302 	case SIOCGLIFADDR:
   6303 		IOCTL_DEBUGOUT("ioctl: SIOCGLIFADDR (Get Interface addr)");
   6304 		break;
   6305 	case SIOCZIFDATA:
   6306 		IOCTL_DEBUGOUT("ioctl: SIOCZIFDATA (Zero counter)");
   6307 		hw->mac.ops.clear_hw_cntrs(hw);
   6308 		ixgbe_clear_evcnt(adapter);
   6309 		break;
   6310 	case SIOCAIFADDR:
   6311 		IOCTL_DEBUGOUT("ioctl: SIOCAIFADDR (add/chg IF alias)");
   6312 		break;
   6313 #endif
   6314 	default:
   6315 		IOCTL_DEBUGOUT1("ioctl: UNKNOWN (0x%X)", (int)command);
   6316 		break;
   6317 	}
   6318 
   6319 	switch (command) {
   6320 	case SIOCSIFMEDIA:
   6321 	case SIOCGIFMEDIA:
   6322 		return ifmedia_ioctl(ifp, ifr, &adapter->media, command);
   6323 	case SIOCGI2C:
   6324 	{
   6325 		struct ixgbe_i2c_req	i2c;
   6326 
   6327 		IOCTL_DEBUGOUT("ioctl: SIOCGI2C (Get I2C Data)");
   6328 		error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
   6329 		if (error != 0)
   6330 			break;
   6331 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
   6332 			error = EINVAL;
   6333 			break;
   6334 		}
   6335 		if (i2c.len > sizeof(i2c.data)) {
   6336 			error = EINVAL;
   6337 			break;
   6338 		}
   6339 
   6340 		hw->phy.ops.read_i2c_byte(hw, i2c.offset,
   6341 		    i2c.dev_addr, i2c.data);
   6342 		error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
   6343 		break;
   6344 	}
   6345 	case SIOCSIFCAP:
   6346 		/* Layer-4 Rx checksum offload has to be turned on and
   6347 		 * off as a unit.
   6348 		 */
   6349 		l4csum_en = ifcr->ifcr_capenable & l4csum;
   6350 		if (l4csum_en != l4csum && l4csum_en != 0)
   6351 			return EINVAL;
   6352 		/*FALLTHROUGH*/
   6353 	case SIOCADDMULTI:
   6354 	case SIOCDELMULTI:
   6355 	case SIOCSIFFLAGS:
   6356 	case SIOCSIFMTU:
   6357 	default:
   6358 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
   6359 			return error;
   6360 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   6361 			;
   6362 		else if (command == SIOCSIFCAP || command == SIOCSIFMTU) {
   6363 			IXGBE_CORE_LOCK(adapter);
   6364 			if ((ifp->if_flags & IFF_RUNNING) != 0)
   6365 				ixgbe_init_locked(adapter);
   6366 			ixgbe_recalculate_max_frame(adapter);
   6367 			IXGBE_CORE_UNLOCK(adapter);
   6368 		} else if (command == SIOCADDMULTI || command == SIOCDELMULTI) {
   6369 			/*
   6370 			 * Multicast list has changed; set the hardware filter
   6371 			 * accordingly.
   6372 			 */
   6373 			IXGBE_CORE_LOCK(adapter);
   6374 			ixgbe_disable_intr(adapter);
   6375 			ixgbe_set_rxfilter(adapter);
   6376 			ixgbe_enable_intr(adapter);
   6377 			IXGBE_CORE_UNLOCK(adapter);
   6378 		}
   6379 		return 0;
   6380 	}
   6381 
   6382 	return error;
   6383 } /* ixgbe_ioctl */
   6384 
   6385 /************************************************************************
   6386  * ixgbe_check_fan_failure
   6387  ************************************************************************/
   6388 static void
   6389 ixgbe_check_fan_failure(struct adapter *adapter, u32 reg, bool in_interrupt)
   6390 {
   6391 	u32 mask;
   6392 
   6393 	mask = (in_interrupt) ? IXGBE_EICR_GPI_SDP1_BY_MAC(&adapter->hw) :
   6394 	    IXGBE_ESDP_SDP1;
   6395 
   6396 	if (reg & mask)
   6397 		device_printf(adapter->dev,
   6398 		    "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
   6399 } /* ixgbe_check_fan_failure */
   6400 
   6401 /************************************************************************
   6402  * ixgbe_handle_que
   6403  ************************************************************************/
   6404 static void
   6405 ixgbe_handle_que(void *context)
   6406 {
   6407 	struct ix_queue *que = context;
   6408 	struct adapter	*adapter = que->adapter;
   6409 	struct tx_ring	*txr = que->txr;
   6410 	struct ifnet	*ifp = adapter->ifp;
   6411 	bool		more = false;
   6412 
   6413 	que->handleq.ev_count++;
   6414 
   6415 	if (ifp->if_flags & IFF_RUNNING) {
   6416 		more = ixgbe_rxeof(que);
   6417 		IXGBE_TX_LOCK(txr);
   6418 		more |= ixgbe_txeof(txr);
   6419 		if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX))
   6420 			if (!ixgbe_mq_ring_empty(ifp, txr->txr_interq))
   6421 				ixgbe_mq_start_locked(ifp, txr);
   6422 		/* Only for queue 0 */
   6423 		/* NetBSD still needs this for CBQ */
   6424 		if ((&adapter->queues[0] == que)
   6425 		    && (!ixgbe_legacy_ring_empty(ifp, NULL)))
   6426 			ixgbe_legacy_start_locked(ifp, txr);
   6427 		IXGBE_TX_UNLOCK(txr);
   6428 	}
   6429 
   6430 	if (more) {
   6431 		que->req.ev_count++;
   6432 		ixgbe_sched_handle_que(adapter, que);
   6433 	} else if (que->res != NULL) {
   6434 		/* Re-enable this interrupt */
   6435 		ixgbe_enable_queue(adapter, que->msix);
   6436 	} else
   6437 		ixgbe_enable_intr(adapter);
   6438 
   6439 	return;
   6440 } /* ixgbe_handle_que */
   6441 
   6442 /************************************************************************
   6443  * ixgbe_handle_que_work
   6444  ************************************************************************/
   6445 static void
   6446 ixgbe_handle_que_work(struct work *wk, void *context)
   6447 {
   6448 	struct ix_queue *que = container_of(wk, struct ix_queue, wq_cookie);
   6449 
   6450 	/*
   6451 	 * "enqueued flag" is not required here.
   6452 	 * See ixgbe_msix_que().
   6453 	 */
   6454 	ixgbe_handle_que(que);
   6455 }
   6456 
   6457 /************************************************************************
   6458  * ixgbe_allocate_legacy - Setup the Legacy or MSI Interrupt handler
   6459  ************************************************************************/
   6460 static int
   6461 ixgbe_allocate_legacy(struct adapter *adapter,
   6462     const struct pci_attach_args *pa)
   6463 {
   6464 	device_t	dev = adapter->dev;
   6465 	struct ix_queue *que = adapter->queues;
   6466 	struct tx_ring	*txr = adapter->tx_rings;
   6467 	int		counts[PCI_INTR_TYPE_SIZE];
   6468 	pci_intr_type_t intr_type, max_type;
   6469 	char		intrbuf[PCI_INTRSTR_LEN];
   6470 	char		wqname[MAXCOMLEN];
   6471 	const char	*intrstr = NULL;
   6472 	int defertx_error = 0, error;
   6473 
   6474 	/* We allocate a single interrupt resource */
   6475 	max_type = PCI_INTR_TYPE_MSI;
   6476 	counts[PCI_INTR_TYPE_MSIX] = 0;
   6477 	counts[PCI_INTR_TYPE_MSI] =
   6478 	    (adapter->feat_en & IXGBE_FEATURE_MSI) ? 1 : 0;
   6479 	/* Check not feat_en but feat_cap to fallback to INTx */
   6480 	counts[PCI_INTR_TYPE_INTX] =
   6481 	    (adapter->feat_cap & IXGBE_FEATURE_LEGACY_IRQ) ? 1 : 0;
   6482 
   6483 alloc_retry:
   6484 	if (pci_intr_alloc(pa, &adapter->osdep.intrs, counts, max_type) != 0) {
   6485 		aprint_error_dev(dev, "couldn't alloc interrupt\n");
   6486 		return ENXIO;
   6487 	}
   6488 	adapter->osdep.nintrs = 1;
   6489 	intrstr = pci_intr_string(adapter->osdep.pc, adapter->osdep.intrs[0],
   6490 	    intrbuf, sizeof(intrbuf));
   6491 	adapter->osdep.ihs[0] = pci_intr_establish_xname(adapter->osdep.pc,
   6492 	    adapter->osdep.intrs[0], IPL_NET, ixgbe_legacy_irq, que,
   6493 	    device_xname(dev));
   6494 	intr_type = pci_intr_type(adapter->osdep.pc, adapter->osdep.intrs[0]);
   6495 	if (adapter->osdep.ihs[0] == NULL) {
   6496 		aprint_error_dev(dev,"unable to establish %s\n",
   6497 		    (intr_type == PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
   6498 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   6499 		adapter->osdep.intrs = NULL;
   6500 		switch (intr_type) {
   6501 		case PCI_INTR_TYPE_MSI:
   6502 			/* The next try is for INTx: Disable MSI */
   6503 			max_type = PCI_INTR_TYPE_INTX;
   6504 			counts[PCI_INTR_TYPE_INTX] = 1;
   6505 			adapter->feat_en &= ~IXGBE_FEATURE_MSI;
   6506 			if (adapter->feat_cap & IXGBE_FEATURE_LEGACY_IRQ) {
   6507 				adapter->feat_en |= IXGBE_FEATURE_LEGACY_IRQ;
   6508 				goto alloc_retry;
   6509 			} else
   6510 				break;
   6511 		case PCI_INTR_TYPE_INTX:
   6512 		default:
   6513 			/* See below */
   6514 			break;
   6515 		}
   6516 	}
   6517 	if (intr_type == PCI_INTR_TYPE_INTX) {
   6518 		adapter->feat_en &= ~IXGBE_FEATURE_MSI;
   6519 		adapter->feat_en |= IXGBE_FEATURE_LEGACY_IRQ;
   6520 	}
   6521 	if (adapter->osdep.ihs[0] == NULL) {
   6522 		aprint_error_dev(dev,
   6523 		    "couldn't establish interrupt%s%s\n",
   6524 		    intrstr ? " at " : "", intrstr ? intrstr : "");
   6525 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   6526 		adapter->osdep.intrs = NULL;
   6527 		return ENXIO;
   6528 	}
   6529 	aprint_normal_dev(dev, "interrupting at %s\n", intrstr);
   6530 	/*
   6531 	 * Try allocating a fast interrupt and the associated deferred
   6532 	 * processing contexts.
   6533 	 */
   6534 	if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX)) {
   6535 		txr->txr_si =
   6536 		    softint_establish(SOFTINT_NET | IXGBE_SOFTINT_FLAGS,
   6537 			ixgbe_deferred_mq_start, txr);
   6538 
   6539 		snprintf(wqname, sizeof(wqname), "%sdeferTx",
   6540 		    device_xname(dev));
   6541 		defertx_error = workqueue_create(&adapter->txr_wq, wqname,
   6542 		    ixgbe_deferred_mq_start_work, adapter, IXGBE_WORKQUEUE_PRI,
   6543 		    IPL_NET, IXGBE_WORKQUEUE_FLAGS);
   6544 		adapter->txr_wq_enqueued = percpu_alloc(sizeof(u_int));
   6545 	}
   6546 	que->que_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINT_FLAGS,
   6547 	    ixgbe_handle_que, que);
   6548 	snprintf(wqname, sizeof(wqname), "%sTxRx", device_xname(dev));
   6549 	error = workqueue_create(&adapter->que_wq, wqname,
   6550 	    ixgbe_handle_que_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
   6551 	    IXGBE_WORKQUEUE_FLAGS);
   6552 
   6553 	if ((!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX)
   6554 		&& ((txr->txr_si == NULL) || defertx_error != 0))
   6555 	    || (que->que_si == NULL) || error != 0) {
   6556 		aprint_error_dev(dev,
   6557 		    "could not establish software interrupts\n");
   6558 
   6559 		return ENXIO;
   6560 	}
   6561 	/* For simplicity in the handlers */
   6562 	adapter->active_queues = IXGBE_EIMS_ENABLE_MASK;
   6563 
   6564 	return (0);
   6565 } /* ixgbe_allocate_legacy */
   6566 
   6567 /************************************************************************
   6568  * ixgbe_allocate_msix - Setup MSI-X Interrupt resources and handlers
   6569  ************************************************************************/
   6570 static int
   6571 ixgbe_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
   6572 {
   6573 	device_t	dev = adapter->dev;
   6574 	struct		ix_queue *que = adapter->queues;
   6575 	struct		tx_ring *txr = adapter->tx_rings;
   6576 	pci_chipset_tag_t pc;
   6577 	char		intrbuf[PCI_INTRSTR_LEN];
   6578 	char		intr_xname[32];
   6579 	char		wqname[MAXCOMLEN];
   6580 	const char	*intrstr = NULL;
   6581 	int		error, vector = 0;
   6582 	int		cpu_id = 0;
   6583 	kcpuset_t	*affinity;
   6584 #ifdef RSS
   6585 	unsigned int	rss_buckets = 0;
   6586 	kcpuset_t	cpu_mask;
   6587 #endif
   6588 
   6589 	pc = adapter->osdep.pc;
   6590 #ifdef	RSS
   6591 	/*
   6592 	 * If we're doing RSS, the number of queues needs to
   6593 	 * match the number of RSS buckets that are configured.
   6594 	 *
   6595 	 * + If there's more queues than RSS buckets, we'll end
   6596 	 *   up with queues that get no traffic.
   6597 	 *
   6598 	 * + If there's more RSS buckets than queues, we'll end
   6599 	 *   up having multiple RSS buckets map to the same queue,
   6600 	 *   so there'll be some contention.
   6601 	 */
   6602 	rss_buckets = rss_getnumbuckets();
   6603 	if ((adapter->feat_en & IXGBE_FEATURE_RSS) &&
   6604 	    (adapter->num_queues != rss_buckets)) {
   6605 		device_printf(dev,
   6606 		    "%s: number of queues (%d) != number of RSS buckets (%d)"
   6607 		    "; performance will be impacted.\n",
   6608 		    __func__, adapter->num_queues, rss_buckets);
   6609 	}
   6610 #endif
   6611 
   6612 	adapter->osdep.nintrs = adapter->num_queues + 1;
   6613 	if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
   6614 	    adapter->osdep.nintrs) != 0) {
   6615 		aprint_error_dev(dev,
   6616 		    "failed to allocate MSI-X interrupt\n");
   6617 		return (ENXIO);
   6618 	}
   6619 
   6620 	kcpuset_create(&affinity, false);
   6621 	for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
   6622 		snprintf(intr_xname, sizeof(intr_xname), "%s TXRX%d",
   6623 		    device_xname(dev), i);
   6624 		intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
   6625 		    sizeof(intrbuf));
   6626 #ifdef IXGBE_MPSAFE
   6627 		pci_intr_setattr(pc, &adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
   6628 		    true);
   6629 #endif
   6630 		/* Set the handler function */
   6631 		que->res = adapter->osdep.ihs[i] = pci_intr_establish_xname(pc,
   6632 		    adapter->osdep.intrs[i], IPL_NET, ixgbe_msix_que, que,
   6633 		    intr_xname);
   6634 		if (que->res == NULL) {
   6635 			aprint_error_dev(dev,
   6636 			    "Failed to register QUE handler\n");
   6637 			error = ENXIO;
   6638 			goto err_out;
   6639 		}
   6640 		que->msix = vector;
   6641 		adapter->active_queues |= 1ULL << que->msix;
   6642 
   6643 		if (adapter->feat_en & IXGBE_FEATURE_RSS) {
   6644 #ifdef	RSS
   6645 			/*
   6646 			 * The queue ID is used as the RSS layer bucket ID.
   6647 			 * We look up the queue ID -> RSS CPU ID and select
   6648 			 * that.
   6649 			 */
   6650 			cpu_id = rss_getcpu(i % rss_getnumbuckets());
   6651 			CPU_SETOF(cpu_id, &cpu_mask);
   6652 #endif
   6653 		} else {
   6654 			/*
   6655 			 * Bind the MSI-X vector, and thus the
   6656 			 * rings to the corresponding CPU.
   6657 			 *
   6658 			 * This just happens to match the default RSS
   6659 			 * round-robin bucket -> queue -> CPU allocation.
   6660 			 */
   6661 			if (adapter->num_queues > 1)
   6662 				cpu_id = i;
   6663 		}
   6664 		/* Round-robin affinity */
   6665 		kcpuset_zero(affinity);
   6666 		kcpuset_set(affinity, cpu_id % ncpu);
   6667 		error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
   6668 		    NULL);
   6669 		aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
   6670 		    intrstr);
   6671 		if (error == 0) {
   6672 #if 1 /* def IXGBE_DEBUG */
   6673 #ifdef	RSS
   6674 			aprintf_normal(", bound RSS bucket %d to CPU %d", i,
   6675 			    cpu_id % ncpu);
   6676 #else
   6677 			aprint_normal(", bound queue %d to cpu %d", i,
   6678 			    cpu_id % ncpu);
   6679 #endif
   6680 #endif /* IXGBE_DEBUG */
   6681 		}
   6682 		aprint_normal("\n");
   6683 
   6684 		if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX)) {
   6685 			txr->txr_si = softint_establish(
   6686 				SOFTINT_NET | IXGBE_SOFTINT_FLAGS,
   6687 				ixgbe_deferred_mq_start, txr);
   6688 			if (txr->txr_si == NULL) {
   6689 				aprint_error_dev(dev,
   6690 				    "couldn't establish software interrupt\n");
   6691 				error = ENXIO;
   6692 				goto err_out;
   6693 			}
   6694 		}
   6695 		que->que_si
   6696 		    = softint_establish(SOFTINT_NET | IXGBE_SOFTINT_FLAGS,
   6697 			ixgbe_handle_que, que);
   6698 		if (que->que_si == NULL) {
   6699 			aprint_error_dev(dev,
   6700 			    "couldn't establish software interrupt\n");
   6701 			error = ENXIO;
   6702 			goto err_out;
   6703 		}
   6704 	}
   6705 	snprintf(wqname, sizeof(wqname), "%sdeferTx", device_xname(dev));
   6706 	error = workqueue_create(&adapter->txr_wq, wqname,
   6707 	    ixgbe_deferred_mq_start_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
   6708 	    IXGBE_WORKQUEUE_FLAGS);
   6709 	if (error) {
   6710 		aprint_error_dev(dev,
   6711 		    "couldn't create workqueue for deferred Tx\n");
   6712 		goto err_out;
   6713 	}
   6714 	adapter->txr_wq_enqueued = percpu_alloc(sizeof(u_int));
   6715 
   6716 	snprintf(wqname, sizeof(wqname), "%sTxRx", device_xname(dev));
   6717 	error = workqueue_create(&adapter->que_wq, wqname,
   6718 	    ixgbe_handle_que_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
   6719 	    IXGBE_WORKQUEUE_FLAGS);
   6720 	if (error) {
   6721 		aprint_error_dev(dev, "couldn't create workqueue for Tx/Rx\n");
   6722 		goto err_out;
   6723 	}
   6724 
   6725 	/* and Link */
   6726 	cpu_id++;
   6727 	snprintf(intr_xname, sizeof(intr_xname), "%s link", device_xname(dev));
   6728 	adapter->vector = vector;
   6729 	intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
   6730 	    sizeof(intrbuf));
   6731 #ifdef IXGBE_MPSAFE
   6732 	pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
   6733 	    true);
   6734 #endif
   6735 	/* Set the link handler function */
   6736 	adapter->osdep.ihs[vector] = pci_intr_establish_xname(pc,
   6737 	    adapter->osdep.intrs[vector], IPL_NET, ixgbe_msix_link, adapter,
   6738 	    intr_xname);
   6739 	if (adapter->osdep.ihs[vector] == NULL) {
   6740 		aprint_error_dev(dev, "Failed to register LINK handler\n");
   6741 		error = ENXIO;
   6742 		goto err_out;
   6743 	}
   6744 	/* Round-robin affinity */
   6745 	kcpuset_zero(affinity);
   6746 	kcpuset_set(affinity, cpu_id % ncpu);
   6747 	error = interrupt_distribute(adapter->osdep.ihs[vector], affinity,
   6748 	    NULL);
   6749 
   6750 	aprint_normal_dev(dev,
   6751 	    "for link, interrupting at %s", intrstr);
   6752 	if (error == 0)
   6753 		aprint_normal(", affinity to cpu %d\n", cpu_id % ncpu);
   6754 	else
   6755 		aprint_normal("\n");
   6756 
   6757 	if (adapter->feat_cap & IXGBE_FEATURE_SRIOV) {
   6758 		adapter->mbx_si =
   6759 		    softint_establish(SOFTINT_NET | IXGBE_SOFTINT_FLAGS,
   6760 			ixgbe_handle_mbx, adapter);
   6761 		if (adapter->mbx_si == NULL) {
   6762 			aprint_error_dev(dev,
   6763 			    "could not establish software interrupts\n");
   6764 
   6765 			error = ENXIO;
   6766 			goto err_out;
   6767 		}
   6768 	}
   6769 
   6770 	kcpuset_destroy(affinity);
   6771 	aprint_normal_dev(dev,
   6772 	    "Using MSI-X interrupts with %d vectors\n", vector + 1);
   6773 
   6774 	return (0);
   6775 
   6776 err_out:
   6777 	kcpuset_destroy(affinity);
   6778 	ixgbe_free_softint(adapter);
   6779 	ixgbe_free_pciintr_resources(adapter);
   6780 	return (error);
   6781 } /* ixgbe_allocate_msix */
   6782 
   6783 /************************************************************************
   6784  * ixgbe_configure_interrupts
   6785  *
   6786  *   Setup MSI-X, MSI, or legacy interrupts (in that order).
   6787  *   This will also depend on user settings.
   6788  ************************************************************************/
   6789 static int
   6790 ixgbe_configure_interrupts(struct adapter *adapter)
   6791 {
   6792 	device_t dev = adapter->dev;
   6793 	struct ixgbe_mac_info *mac = &adapter->hw.mac;
   6794 	int want, queues, msgs;
   6795 
   6796 	/* Default to 1 queue if MSI-X setup fails */
   6797 	adapter->num_queues = 1;
   6798 
   6799 	/* Override by tuneable */
   6800 	if (!(adapter->feat_cap & IXGBE_FEATURE_MSIX))
   6801 		goto msi;
   6802 
   6803 	/*
   6804 	 *  NetBSD only: Use single vector MSI when number of CPU is 1 to save
   6805 	 * interrupt slot.
   6806 	 */
   6807 	if (ncpu == 1)
   6808 		goto msi;
   6809 
   6810 	/* First try MSI-X */
   6811 	msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
   6812 	msgs = MIN(msgs, IXG_MAX_NINTR);
   6813 	if (msgs < 2)
   6814 		goto msi;
   6815 
   6816 	adapter->msix_mem = (void *)1; /* XXX */
   6817 
   6818 	/* Figure out a reasonable auto config value */
   6819 	queues = (ncpu > (msgs - 1)) ? (msgs - 1) : ncpu;
   6820 
   6821 #ifdef	RSS
   6822 	/* If we're doing RSS, clamp at the number of RSS buckets */
   6823 	if (adapter->feat_en & IXGBE_FEATURE_RSS)
   6824 		queues = min(queues, rss_getnumbuckets());
   6825 #endif
   6826 	if (ixgbe_num_queues > queues) {
   6827 		aprint_error_dev(adapter->dev, "ixgbe_num_queues (%d) is too large, using reduced amount (%d).\n", ixgbe_num_queues, queues);
   6828 		ixgbe_num_queues = queues;
   6829 	}
   6830 
   6831 	if (ixgbe_num_queues != 0)
   6832 		queues = ixgbe_num_queues;
   6833 	else
   6834 		queues = min(queues,
   6835 		    min(mac->max_tx_queues, mac->max_rx_queues));
   6836 
   6837 	/* reflect correct sysctl value */
   6838 	ixgbe_num_queues = queues;
   6839 
   6840 	/*
   6841 	 * Want one vector (RX/TX pair) per queue
   6842 	 * plus an additional for Link.
   6843 	 */
   6844 	want = queues + 1;
   6845 	if (msgs >= want)
   6846 		msgs = want;
   6847 	else {
   6848 		aprint_error_dev(dev, "MSI-X Configuration Problem, "
   6849 		    "%d vectors but %d queues wanted!\n",
   6850 		    msgs, want);
   6851 		goto msi;
   6852 	}
   6853 	adapter->num_queues = queues;
   6854 	adapter->feat_en |= IXGBE_FEATURE_MSIX;
   6855 	return (0);
   6856 
   6857 	/*
   6858 	 * MSI-X allocation failed or provided us with
   6859 	 * less vectors than needed. Free MSI-X resources
   6860 	 * and we'll try enabling MSI.
   6861 	 */
   6862 msi:
   6863 	/* Without MSI-X, some features are no longer supported */
   6864 	adapter->feat_cap &= ~IXGBE_FEATURE_RSS;
   6865 	adapter->feat_en  &= ~IXGBE_FEATURE_RSS;
   6866 	adapter->feat_cap &= ~IXGBE_FEATURE_SRIOV;
   6867 	adapter->feat_en  &= ~IXGBE_FEATURE_SRIOV;
   6868 
   6869 	msgs = pci_msi_count(adapter->osdep.pc, adapter->osdep.tag);
   6870 	adapter->msix_mem = NULL; /* XXX */
   6871 	if (msgs > 1)
   6872 		msgs = 1;
   6873 	if (msgs != 0) {
   6874 		msgs = 1;
   6875 		adapter->feat_en |= IXGBE_FEATURE_MSI;
   6876 		return (0);
   6877 	}
   6878 
   6879 	if (!(adapter->feat_cap & IXGBE_FEATURE_LEGACY_IRQ)) {
   6880 		aprint_error_dev(dev,
   6881 		    "Device does not support legacy interrupts.\n");
   6882 		return 1;
   6883 	}
   6884 
   6885 	adapter->feat_en |= IXGBE_FEATURE_LEGACY_IRQ;
   6886 
   6887 	return (0);
   6888 } /* ixgbe_configure_interrupts */
   6889 
   6890 
   6891 /************************************************************************
   6892  * ixgbe_handle_link - Tasklet for MSI-X Link interrupts
   6893  *
   6894  *   Done outside of interrupt context since the driver might sleep
   6895  ************************************************************************/
   6896 static void
   6897 ixgbe_handle_link(void *context)
   6898 {
   6899 	struct adapter	*adapter = context;
   6900 	struct ixgbe_hw *hw = &adapter->hw;
   6901 
   6902 	IXGBE_CORE_LOCK(adapter);
   6903 	++adapter->link_sicount.ev_count;
   6904 	ixgbe_check_link(hw, &adapter->link_speed, &adapter->link_up, 0);
   6905 	ixgbe_update_link_status(adapter);
   6906 
   6907 	/* Re-enable link interrupts */
   6908 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_LSC);
   6909 
   6910 	IXGBE_CORE_UNLOCK(adapter);
   6911 } /* ixgbe_handle_link */
   6912 
   6913 /************************************************************************
   6914  * ixgbe_rearm_queues
   6915  ************************************************************************/
   6916 static __inline void
   6917 ixgbe_rearm_queues(struct adapter *adapter, u64 queues)
   6918 {
   6919 	u32 mask;
   6920 
   6921 	switch (adapter->hw.mac.type) {
   6922 	case ixgbe_mac_82598EB:
   6923 		mask = (IXGBE_EIMS_RTX_QUEUE & queues);
   6924 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, mask);
   6925 		break;
   6926 	case ixgbe_mac_82599EB:
   6927 	case ixgbe_mac_X540:
   6928 	case ixgbe_mac_X550:
   6929 	case ixgbe_mac_X550EM_x:
   6930 	case ixgbe_mac_X550EM_a:
   6931 		mask = (queues & 0xFFFFFFFF);
   6932 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(0), mask);
   6933 		mask = (queues >> 32);
   6934 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(1), mask);
   6935 		break;
   6936 	default:
   6937 		break;
   6938 	}
   6939 } /* ixgbe_rearm_queues */
   6940