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