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