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