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