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