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