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