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