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