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