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