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