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