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