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