Home | History | Annotate | Line # | Download | only in ixgbe
ixgbe.c revision 1.90
      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.90 2017/06/23 04:36:48 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_1GB_FULL:
   2438 					bpsmsg = "1 Gbps";
   2439 					break;
   2440 				case IXGBE_LINK_SPEED_100_FULL:
   2441 					bpsmsg = "100 Mbps";
   2442 					break;
   2443 				default:
   2444 					bpsmsg = "unknown speed";
   2445 					break;
   2446 				}
   2447 				device_printf(dev,"Link is up %s %s \n",
   2448 				    bpsmsg, "Full Duplex");
   2449 			}
   2450 			adapter->link_active = TRUE;
   2451 			/* Update any Flow Control changes */
   2452 			ixgbe_fc_enable(&adapter->hw);
   2453 			/* Update DMA coalescing config */
   2454 			ixgbe_config_dmac(adapter);
   2455 			if_link_state_change(ifp, LINK_STATE_UP);
   2456 #ifdef PCI_IOV
   2457 			ixgbe_ping_all_vfs(adapter);
   2458 #endif
   2459 		}
   2460 	} else { /* Link down */
   2461 		if (adapter->link_active == TRUE) {
   2462 			if (bootverbose)
   2463 				device_printf(dev,"Link is Down\n");
   2464 			if_link_state_change(ifp, LINK_STATE_DOWN);
   2465 			adapter->link_active = FALSE;
   2466 #ifdef PCI_IOV
   2467 			ixgbe_ping_all_vfs(adapter);
   2468 #endif
   2469 		}
   2470 	}
   2471 
   2472 	return;
   2473 }
   2474 
   2475 
   2476 static void
   2477 ixgbe_ifstop(struct ifnet *ifp, int disable)
   2478 {
   2479 	struct adapter *adapter = ifp->if_softc;
   2480 
   2481 	IXGBE_CORE_LOCK(adapter);
   2482 	ixgbe_stop(adapter);
   2483 	IXGBE_CORE_UNLOCK(adapter);
   2484 }
   2485 
   2486 /*********************************************************************
   2487  *
   2488  *  This routine disables all traffic on the adapter by issuing a
   2489  *  global reset on the MAC and deallocates TX/RX buffers.
   2490  *
   2491  **********************************************************************/
   2492 
   2493 static void
   2494 ixgbe_stop(void *arg)
   2495 {
   2496 	struct ifnet   *ifp;
   2497 	struct adapter *adapter = arg;
   2498 	struct ixgbe_hw *hw = &adapter->hw;
   2499 	ifp = adapter->ifp;
   2500 
   2501 	KASSERT(mutex_owned(&adapter->core_mtx));
   2502 
   2503 	INIT_DEBUGOUT("ixgbe_stop: begin\n");
   2504 	ixgbe_disable_intr(adapter);
   2505 	callout_stop(&adapter->timer);
   2506 
   2507 	/* Let the stack know...*/
   2508 	ifp->if_flags &= ~IFF_RUNNING;
   2509 
   2510 	ixgbe_reset_hw(hw);
   2511 	hw->adapter_stopped = FALSE;
   2512 	ixgbe_stop_adapter(hw);
   2513 	if (hw->mac.type == ixgbe_mac_82599EB)
   2514 		ixgbe_stop_mac_link_on_d3_82599(hw);
   2515 	/* Turn off the laser - noop with no optics */
   2516 	ixgbe_disable_tx_laser(hw);
   2517 
   2518 	/* Update the stack */
   2519 	adapter->link_up = FALSE;
   2520 	ixgbe_update_link_status(adapter);
   2521 
   2522 	/* reprogram the RAR[0] in case user changed it. */
   2523 	ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
   2524 
   2525 	return;
   2526 }
   2527 
   2528 
   2529 /*********************************************************************
   2530  *
   2531  *  Determine hardware revision.
   2532  *
   2533  **********************************************************************/
   2534 static void
   2535 ixgbe_identify_hardware(struct adapter *adapter)
   2536 {
   2537 	pcitag_t tag;
   2538 	pci_chipset_tag_t pc;
   2539 	pcireg_t subid, id;
   2540 	struct ixgbe_hw *hw = &adapter->hw;
   2541 
   2542 	pc = adapter->osdep.pc;
   2543 	tag = adapter->osdep.tag;
   2544 
   2545 	id = pci_conf_read(pc, tag, PCI_ID_REG);
   2546 	subid = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG);
   2547 
   2548 	/* Save off the information about this board */
   2549 	hw->vendor_id = PCI_VENDOR(id);
   2550 	hw->device_id = PCI_PRODUCT(id);
   2551 	hw->revision_id =
   2552 	    PCI_REVISION(pci_conf_read(pc, tag, PCI_CLASS_REG));
   2553 	hw->subsystem_vendor_id = PCI_SUBSYS_VENDOR(subid);
   2554 	hw->subsystem_device_id = PCI_SUBSYS_ID(subid);
   2555 
   2556 	/*
   2557 	** Make sure BUSMASTER is set
   2558 	*/
   2559 	ixgbe_pci_enable_busmaster(pc, tag);
   2560 
   2561 	/* We need this here to set the num_segs below */
   2562 	ixgbe_set_mac_type(hw);
   2563 
   2564 	/* Pick up the 82599 settings */
   2565 	if (hw->mac.type != ixgbe_mac_82598EB) {
   2566 		hw->phy.smart_speed = ixgbe_smart_speed;
   2567 		adapter->num_segs = IXGBE_82599_SCATTER;
   2568 	} else
   2569 		adapter->num_segs = IXGBE_82598_SCATTER;
   2570 
   2571 	return;
   2572 }
   2573 
   2574 /*********************************************************************
   2575  *
   2576  *  Determine optic type
   2577  *
   2578  **********************************************************************/
   2579 static void
   2580 ixgbe_setup_optics(struct adapter *adapter)
   2581 {
   2582 	struct ixgbe_hw *hw = &adapter->hw;
   2583 	int		layer;
   2584 
   2585 	layer = adapter->phy_layer = ixgbe_get_supported_physical_layer(hw);
   2586 
   2587 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T) {
   2588 		adapter->optics = IFM_10G_T;
   2589 		return;
   2590 	}
   2591 
   2592 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T) {
   2593 		adapter->optics = IFM_1000_T;
   2594 		return;
   2595 	}
   2596 
   2597 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX) {
   2598 		adapter->optics = IFM_1000_SX;
   2599 		return;
   2600 	}
   2601 
   2602 	if (layer & (IXGBE_PHYSICAL_LAYER_10GBASE_LR |
   2603 	    IXGBE_PHYSICAL_LAYER_10GBASE_LRM)) {
   2604 		adapter->optics = IFM_10G_LR;
   2605 		return;
   2606 	}
   2607 
   2608 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
   2609 		adapter->optics = IFM_10G_SR;
   2610 		return;
   2611 	}
   2612 
   2613 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU) {
   2614 		adapter->optics = IFM_10G_TWINAX;
   2615 		return;
   2616 	}
   2617 
   2618 	if (layer & (IXGBE_PHYSICAL_LAYER_10GBASE_KX4 |
   2619 	    IXGBE_PHYSICAL_LAYER_10GBASE_CX4)) {
   2620 		adapter->optics = IFM_10G_CX4;
   2621 		return;
   2622 	}
   2623 
   2624 	/* If we get here just set the default */
   2625 	adapter->optics = IFM_ETHER | IFM_AUTO;
   2626 	return;
   2627 }
   2628 
   2629 /*********************************************************************
   2630  *
   2631  *  Setup the Legacy or MSI Interrupt handler
   2632  *
   2633  **********************************************************************/
   2634 static int
   2635 ixgbe_allocate_legacy(struct adapter *adapter,
   2636     const struct pci_attach_args *pa)
   2637 {
   2638 	device_t	dev = adapter->dev;
   2639 	struct		ix_queue *que = adapter->queues;
   2640 #ifndef IXGBE_LEGACY_TX
   2641 	struct tx_ring		*txr = adapter->tx_rings;
   2642 #endif
   2643 	int		counts[PCI_INTR_TYPE_SIZE];
   2644 	pci_intr_type_t intr_type, max_type;
   2645 	char intrbuf[PCI_INTRSTR_LEN];
   2646 	const char	*intrstr = NULL;
   2647 
   2648 	/* Allocation settings */
   2649 	max_type = PCI_INTR_TYPE_MSI;
   2650 	counts[PCI_INTR_TYPE_MSIX] = 0;
   2651 	counts[PCI_INTR_TYPE_MSI] = 1;
   2652 	counts[PCI_INTR_TYPE_INTX] = 1;
   2653 
   2654 alloc_retry:
   2655 	if (pci_intr_alloc(pa, &adapter->osdep.intrs, counts, max_type) != 0) {
   2656 		aprint_error_dev(dev, "couldn't alloc interrupt\n");
   2657 		return ENXIO;
   2658 	}
   2659 	adapter->osdep.nintrs = 1;
   2660 	intrstr = pci_intr_string(adapter->osdep.pc, adapter->osdep.intrs[0],
   2661 	    intrbuf, sizeof(intrbuf));
   2662 	adapter->osdep.ihs[0] = pci_intr_establish_xname(adapter->osdep.pc,
   2663 	    adapter->osdep.intrs[0], IPL_NET, ixgbe_legacy_irq, que,
   2664 	    device_xname(dev));
   2665 	if (adapter->osdep.ihs[0] == NULL) {
   2666 		intr_type = pci_intr_type(adapter->osdep.pc,
   2667 		    adapter->osdep.intrs[0]);
   2668 		aprint_error_dev(dev,"unable to establish %s\n",
   2669 		    (intr_type == PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
   2670 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   2671 		switch (intr_type) {
   2672 		case PCI_INTR_TYPE_MSI:
   2673 			/* The next try is for INTx: Disable MSI */
   2674 			max_type = PCI_INTR_TYPE_INTX;
   2675 			counts[PCI_INTR_TYPE_INTX] = 1;
   2676 			goto alloc_retry;
   2677 		case PCI_INTR_TYPE_INTX:
   2678 		default:
   2679 			/* See below */
   2680 			break;
   2681 		}
   2682 	}
   2683 	if (adapter->osdep.ihs[0] == NULL) {
   2684 		aprint_error_dev(dev,
   2685 		    "couldn't establish interrupt%s%s\n",
   2686 		    intrstr ? " at " : "", intrstr ? intrstr : "");
   2687 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   2688 		return ENXIO;
   2689 	}
   2690 	aprint_normal_dev(dev, "interrupting at %s\n", intrstr);
   2691 	/*
   2692 	 * Try allocating a fast interrupt and the associated deferred
   2693 	 * processing contexts.
   2694 	 */
   2695 #ifndef IXGBE_LEGACY_TX
   2696 	txr->txr_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2697 	    ixgbe_deferred_mq_start, txr);
   2698 #endif
   2699 	que->que_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2700 	    ixgbe_handle_que, que);
   2701 
   2702 	/* Tasklets for Link, SFP and Multispeed Fiber */
   2703 	adapter->link_si = softint_establish(SOFTINT_NET |IXGBE_SOFTINFT_FLAGS,
   2704 	    ixgbe_handle_link, adapter);
   2705 	adapter->mod_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2706 	    ixgbe_handle_mod, adapter);
   2707 	adapter->msf_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2708 	    ixgbe_handle_msf, adapter);
   2709 	adapter->phy_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2710 	    ixgbe_handle_phy, adapter);
   2711 
   2712 #ifdef IXGBE_FDIR
   2713 	adapter->fdir_si =
   2714 	    softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2715 		ixgbe_reinit_fdir, adapter);
   2716 #endif
   2717 	if (que->que_si == NULL ||
   2718 	    adapter->link_si == NULL ||
   2719 	    adapter->mod_si == NULL ||
   2720 #ifdef IXGBE_FDIR
   2721 	    adapter->fdir_si == NULL ||
   2722 #endif
   2723 	    adapter->msf_si == NULL) {
   2724 		aprint_error_dev(dev,
   2725 		    "could not establish software interrupts\n");
   2726 		return ENXIO;
   2727 	}
   2728 
   2729 	/* For simplicity in the handlers */
   2730 	adapter->active_queues = IXGBE_EIMS_ENABLE_MASK;
   2731 
   2732 	return (0);
   2733 }
   2734 
   2735 
   2736 /*********************************************************************
   2737  *
   2738  *  Setup MSIX Interrupt resources and handlers
   2739  *
   2740  **********************************************************************/
   2741 static int
   2742 ixgbe_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
   2743 {
   2744 	device_t        dev = adapter->dev;
   2745 	struct 		ix_queue *que = adapter->queues;
   2746 	struct  	tx_ring *txr = adapter->tx_rings;
   2747 	pci_chipset_tag_t pc;
   2748 	char		intrbuf[PCI_INTRSTR_LEN];
   2749 	char		intr_xname[32];
   2750 	const char	*intrstr = NULL;
   2751 	int 		error, vector = 0;
   2752 	int		cpu_id = 0;
   2753 	kcpuset_t	*affinity;
   2754 #ifdef	RSS
   2755 	cpuset_t	cpu_mask;
   2756 #endif
   2757 
   2758 	pc = adapter->osdep.pc;
   2759 #ifdef	RSS
   2760 	/*
   2761 	 * If we're doing RSS, the number of queues needs to
   2762 	 * match the number of RSS buckets that are configured.
   2763 	 *
   2764 	 * + If there's more queues than RSS buckets, we'll end
   2765 	 *   up with queues that get no traffic.
   2766 	 *
   2767 	 * + If there's more RSS buckets than queues, we'll end
   2768 	 *   up having multiple RSS buckets map to the same queue,
   2769 	 *   so there'll be some contention.
   2770 	 */
   2771 	if (adapter->num_queues != rss_getnumbuckets()) {
   2772 		device_printf(dev,
   2773 		    "%s: number of queues (%d) != number of RSS buckets (%d)"
   2774 		    "; performance will be impacted.\n",
   2775 		    __func__,
   2776 		    adapter->num_queues,
   2777 		    rss_getnumbuckets());
   2778 	}
   2779 #endif
   2780 
   2781 	adapter->osdep.nintrs = adapter->num_queues + 1;
   2782 	if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
   2783 	    adapter->osdep.nintrs) != 0) {
   2784 		aprint_error_dev(dev,
   2785 		    "failed to allocate MSI-X interrupt\n");
   2786 		return (ENXIO);
   2787 	}
   2788 
   2789 	kcpuset_create(&affinity, false);
   2790 	for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
   2791 		snprintf(intr_xname, sizeof(intr_xname), "%s TXRX%d",
   2792 		    device_xname(dev), i);
   2793 		intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
   2794 		    sizeof(intrbuf));
   2795 #ifdef IXGBE_MPSAFE
   2796 		pci_intr_setattr(pc, &adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
   2797 		    true);
   2798 #endif
   2799 		/* Set the handler function */
   2800 		que->res = adapter->osdep.ihs[i] = pci_intr_establish_xname(pc,
   2801 		    adapter->osdep.intrs[i], IPL_NET, ixgbe_msix_que, que,
   2802 		    intr_xname);
   2803 		if (que->res == NULL) {
   2804 			pci_intr_release(pc, adapter->osdep.intrs,
   2805 			    adapter->osdep.nintrs);
   2806 			aprint_error_dev(dev,
   2807 			    "Failed to register QUE handler\n");
   2808 			kcpuset_destroy(affinity);
   2809 			return ENXIO;
   2810 		}
   2811 		que->msix = vector;
   2812 		adapter->active_queues |= (u64)(1 << que->msix);
   2813 #ifdef	RSS
   2814 		/*
   2815 		 * The queue ID is used as the RSS layer bucket ID.
   2816 		 * We look up the queue ID -> RSS CPU ID and select
   2817 		 * that.
   2818 		 */
   2819 		cpu_id = rss_getcpu(i % rss_getnumbuckets());
   2820 #else
   2821 		/*
   2822 		 * Bind the msix vector, and thus the
   2823 		 * rings to the corresponding cpu.
   2824 		 *
   2825 		 * This just happens to match the default RSS round-robin
   2826 		 * bucket -> queue -> CPU allocation.
   2827 		 */
   2828 		if (adapter->num_queues > 1)
   2829 			cpu_id = i;
   2830 #endif
   2831 		/* Round-robin affinity */
   2832 		kcpuset_zero(affinity);
   2833 		kcpuset_set(affinity, cpu_id % ncpu);
   2834 		error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
   2835 		    NULL);
   2836 		aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
   2837 		    intrstr);
   2838 		if (error == 0) {
   2839 #if 1 /* def IXGBE_DEBUG */
   2840 #ifdef	RSS
   2841 			aprintf_normal(
   2842 			    ", bound RSS bucket %d to CPU %d",
   2843 			    i, cpu_id % ncpu);
   2844 #else
   2845 			aprint_normal(
   2846 			    ", bound queue %d to cpu %d",
   2847 			    i, cpu_id % ncpu);
   2848 #endif
   2849 #endif /* IXGBE_DEBUG */
   2850 		}
   2851 		aprint_normal("\n");
   2852 #ifndef IXGBE_LEGACY_TX
   2853 		txr->txr_si
   2854 		    = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2855 		    ixgbe_deferred_mq_start, txr);
   2856 #endif
   2857 		que->que_si
   2858 		    = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2859 			ixgbe_handle_que, que);
   2860 		if (que->que_si == NULL) {
   2861 			aprint_error_dev(dev,
   2862 			    "could not establish software interrupt\n");
   2863 		}
   2864 	}
   2865 
   2866 	/* and Link */
   2867 	cpu_id++;
   2868 	snprintf(intr_xname, sizeof(intr_xname), "%s link", device_xname(dev));
   2869 	intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
   2870 	    sizeof(intrbuf));
   2871 #ifdef IXGBE_MPSAFE
   2872 	pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
   2873 	    true);
   2874 #endif
   2875 	/* Set the link handler function */
   2876 	adapter->osdep.ihs[vector] = pci_intr_establish_xname(pc,
   2877 	    adapter->osdep.intrs[vector], IPL_NET, ixgbe_msix_link, adapter,
   2878 	    intr_xname);
   2879 	if (adapter->osdep.ihs[vector] == NULL) {
   2880 		adapter->res = NULL;
   2881 		aprint_error_dev(dev, "Failed to register LINK handler\n");
   2882 		kcpuset_destroy(affinity);
   2883 		return (ENXIO);
   2884 	}
   2885 	/* Round-robin affinity */
   2886 	kcpuset_zero(affinity);
   2887 	kcpuset_set(affinity, cpu_id % ncpu);
   2888 	error = interrupt_distribute(adapter->osdep.ihs[vector], affinity,NULL);
   2889 
   2890 	aprint_normal_dev(dev,
   2891 	    "for link, interrupting at %s", intrstr);
   2892 	if (error == 0)
   2893 		aprint_normal(", affinity to cpu %d\n", cpu_id % ncpu);
   2894 	else
   2895 		aprint_normal("\n");
   2896 
   2897 	adapter->vector = vector;
   2898 	/* Tasklets for Link, SFP and Multispeed Fiber */
   2899 	adapter->link_si = softint_establish(SOFTINT_NET |IXGBE_SOFTINFT_FLAGS,
   2900 	    ixgbe_handle_link, adapter);
   2901 	adapter->mod_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2902 	    ixgbe_handle_mod, adapter);
   2903 	adapter->msf_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2904 	    ixgbe_handle_msf, adapter);
   2905 #ifdef PCI_IOV
   2906 	TASK_INIT(&adapter->mbx_task, 0, ixgbe_handle_mbx, adapter);
   2907 #endif
   2908 	adapter->phy_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
   2909 		ixgbe_handle_phy, adapter);
   2910 #ifdef IXGBE_FDIR
   2911 	adapter->fdir_si = softint_establish(SOFTINT_NET | XGBE_SOFTINFT_FLAGS,
   2912 		ixgbe_reinit_fdir, adapter);
   2913 #endif
   2914 
   2915 	kcpuset_destroy(affinity);
   2916 	return (0);
   2917 }
   2918 
   2919 /*
   2920  * Setup Either MSI/X or MSI
   2921  */
   2922 static int
   2923 ixgbe_setup_msix(struct adapter *adapter)
   2924 {
   2925 	device_t dev = adapter->dev;
   2926 	int want, queues, msgs;
   2927 
   2928 	/* Override by tuneable */
   2929 	if (ixgbe_enable_msix == 0)
   2930 		goto msi;
   2931 
   2932 	/* First try MSI/X */
   2933 	msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
   2934 	msgs = MIN(msgs, IXG_MAX_NINTR);
   2935 	if (msgs < 2)
   2936 		goto msi;
   2937 
   2938 	adapter->msix_mem = (void *)1; /* XXX */
   2939 
   2940 	/* Figure out a reasonable auto config value */
   2941 	queues = (ncpu > (msgs - 1)) ? (msgs - 1) : ncpu;
   2942 
   2943 #ifdef	RSS
   2944 	/* If we're doing RSS, clamp at the number of RSS buckets */
   2945 	if (queues > rss_getnumbuckets())
   2946 		queues = rss_getnumbuckets();
   2947 #endif
   2948 
   2949 	if (ixgbe_num_queues != 0)
   2950 		queues = ixgbe_num_queues;
   2951 	/* Set max queues to 8 when autoconfiguring */
   2952 	else if ((ixgbe_num_queues == 0) && (queues > 8))
   2953 		queues = 8;
   2954 
   2955 	/* reflect correct sysctl value */
   2956 	ixgbe_num_queues = queues;
   2957 
   2958 	/*
   2959 	** Want one vector (RX/TX pair) per queue
   2960 	** plus an additional for Link.
   2961 	*/
   2962 	want = queues + 1;
   2963 	if (msgs >= want)
   2964 		msgs = want;
   2965 	else {
   2966                	aprint_error_dev(dev,
   2967 		    "MSIX Configuration Problem, "
   2968 		    "%d vectors but %d queues wanted!\n",
   2969 		    msgs, want);
   2970 		goto msi;
   2971 	}
   2972 	device_printf(dev,
   2973 	    "Using MSIX interrupts with %d vectors\n", msgs);
   2974 	adapter->num_queues = queues;
   2975 	return (msgs);
   2976 
   2977 	/*
   2978 	** If MSIX alloc failed or provided us with
   2979 	** less than needed, free and fall through to MSI
   2980 	*/
   2981 msi:
   2982        	msgs = pci_msi_count(adapter->osdep.pc, adapter->osdep.tag);
   2983 	adapter->msix_mem = NULL; /* XXX */
   2984        	msgs = 1;
   2985 	aprint_normal_dev(dev, "Using an MSI interrupt\n");
   2986 	return (msgs);
   2987 }
   2988 
   2989 
   2990 static int
   2991 ixgbe_allocate_pci_resources(struct adapter *adapter,
   2992     const struct pci_attach_args *pa)
   2993 {
   2994 	pcireg_t	memtype;
   2995 	device_t        dev = adapter->dev;
   2996 	bus_addr_t addr;
   2997 	int flags;
   2998 
   2999 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
   3000 	switch (memtype) {
   3001 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   3002 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   3003 		adapter->osdep.mem_bus_space_tag = pa->pa_memt;
   3004 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
   3005 	              memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
   3006 			goto map_err;
   3007 		if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
   3008 			aprint_normal_dev(dev, "clearing prefetchable bit\n");
   3009 			flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
   3010 		}
   3011 		if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
   3012 		     adapter->osdep.mem_size, flags,
   3013 		     &adapter->osdep.mem_bus_space_handle) != 0) {
   3014 map_err:
   3015 			adapter->osdep.mem_size = 0;
   3016 			aprint_error_dev(dev, "unable to map BAR0\n");
   3017 			return ENXIO;
   3018 		}
   3019 		break;
   3020 	default:
   3021 		aprint_error_dev(dev, "unexpected type on BAR0\n");
   3022 		return ENXIO;
   3023 	}
   3024 	adapter->hw.back = adapter;
   3025 
   3026 	/* Default to 1 queue if MSI-X setup fails */
   3027 	adapter->num_queues = 1;
   3028 
   3029 	/*
   3030 	** Now setup MSI or MSI-X, should
   3031 	** return us the number of supported
   3032 	** vectors. (Will be 1 for MSI)
   3033 	*/
   3034 	adapter->msix = ixgbe_setup_msix(adapter);
   3035 	return (0);
   3036 }
   3037 
   3038 static void
   3039 ixgbe_free_pci_resources(struct adapter * adapter)
   3040 {
   3041 	struct 		ix_queue *que = adapter->queues;
   3042 	int		rid;
   3043 
   3044 	/*
   3045 	**  Release all msix queue resources:
   3046 	*/
   3047 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   3048 		if (que->res != NULL)
   3049 			pci_intr_disestablish(adapter->osdep.pc,
   3050 			    adapter->osdep.ihs[i]);
   3051 	}
   3052 
   3053 
   3054 	/* Clean the Legacy or Link interrupt last */
   3055 	if (adapter->vector) /* we are doing MSIX */
   3056 		rid = adapter->vector;
   3057 	else
   3058 		rid = 0;
   3059 
   3060 	if (adapter->osdep.ihs[rid] != NULL) {
   3061 		pci_intr_disestablish(adapter->osdep.pc,
   3062 		    adapter->osdep.ihs[rid]);
   3063 		adapter->osdep.ihs[rid] = NULL;
   3064 	}
   3065 
   3066 	pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
   3067 	    adapter->osdep.nintrs);
   3068 
   3069 	if (adapter->osdep.mem_size != 0) {
   3070 		bus_space_unmap(adapter->osdep.mem_bus_space_tag,
   3071 		    adapter->osdep.mem_bus_space_handle,
   3072 		    adapter->osdep.mem_size);
   3073 	}
   3074 
   3075 	return;
   3076 }
   3077 
   3078 /*********************************************************************
   3079  *
   3080  *  Setup networking device structure and register an interface.
   3081  *
   3082  **********************************************************************/
   3083 static int
   3084 ixgbe_setup_interface(device_t dev, struct adapter *adapter)
   3085 {
   3086 	struct ethercom *ec = &adapter->osdep.ec;
   3087 	struct ifnet   *ifp;
   3088 
   3089 	INIT_DEBUGOUT("ixgbe_setup_interface: begin");
   3090 
   3091 	ifp = adapter->ifp = &ec->ec_if;
   3092 	strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
   3093 	ifp->if_baudrate = IF_Gbps(10);
   3094 	ifp->if_init = ixgbe_init;
   3095 	ifp->if_stop = ixgbe_ifstop;
   3096 	ifp->if_softc = adapter;
   3097 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   3098 #ifdef IXGBE_MPSAFE
   3099 	ifp->if_extflags = IFEF_START_MPSAFE;
   3100 #endif
   3101 	ifp->if_ioctl = ixgbe_ioctl;
   3102 #if __FreeBSD_version >= 1100045
   3103 	/* TSO parameters */
   3104 	ifp->if_hw_tsomax = 65518;
   3105 	ifp->if_hw_tsomaxsegcount = IXGBE_82599_SCATTER;
   3106 	ifp->if_hw_tsomaxsegsize = 2048;
   3107 #endif
   3108 #ifndef IXGBE_LEGACY_TX
   3109 	ifp->if_transmit = ixgbe_mq_start;
   3110 #endif
   3111 	ifp->if_start = ixgbe_start;
   3112 	IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
   3113 	IFQ_SET_READY(&ifp->if_snd);
   3114 
   3115 	if_initialize(ifp);
   3116 	adapter->ipq = if_percpuq_create(&adapter->osdep.ec.ec_if);
   3117 	ether_ifattach(ifp, adapter->hw.mac.addr);
   3118 	/*
   3119 	 * We use per TX queue softint, so if_deferred_start_init() isn't
   3120 	 * used.
   3121 	 */
   3122 	if_register(ifp);
   3123 	ether_set_ifflags_cb(ec, ixgbe_ifflags_cb);
   3124 
   3125 	adapter->max_frame_size =
   3126 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   3127 
   3128 	/*
   3129 	 * Tell the upper layer(s) we support long frames.
   3130 	 */
   3131 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
   3132 
   3133 	/* Set capability flags */
   3134 	ifp->if_capabilities |= IFCAP_RXCSUM
   3135 			     |  IFCAP_TXCSUM
   3136 			     |  IFCAP_TSOv4
   3137 			     |  IFCAP_TSOv6
   3138 			     |  IFCAP_LRO;
   3139 	ifp->if_capenable = 0;
   3140 
   3141 	ec->ec_capabilities |= ETHERCAP_VLAN_HWTAGGING
   3142 	    		    |  ETHERCAP_VLAN_HWCSUM
   3143 	    		    |  ETHERCAP_JUMBO_MTU
   3144 	    		    |  ETHERCAP_VLAN_MTU;
   3145 
   3146 	/* Enable the above capabilities by default */
   3147 	ec->ec_capenable = ec->ec_capabilities;
   3148 
   3149 	/*
   3150 	** Don't turn this on by default, if vlans are
   3151 	** created on another pseudo device (eg. lagg)
   3152 	** then vlan events are not passed thru, breaking
   3153 	** operation, but with HW FILTER off it works. If
   3154 	** using vlans directly on the ixgbe driver you can
   3155 	** enable this and get full hardware tag filtering.
   3156 	*/
   3157 	ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER;
   3158 
   3159 	/*
   3160 	 * Specify the media types supported by this adapter and register
   3161 	 * callbacks to update media and link information
   3162 	 */
   3163 	ifmedia_init(&adapter->media, IFM_IMASK, ixgbe_media_change,
   3164 		    ixgbe_media_status);
   3165 
   3166 	adapter->phy_layer = ixgbe_get_supported_physical_layer(&adapter->hw);
   3167 	ixgbe_add_media_types(adapter);
   3168 
   3169 	/* Set autoselect media by default */
   3170 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   3171 
   3172 	return (0);
   3173 }
   3174 
   3175 static void
   3176 ixgbe_add_media_types(struct adapter *adapter)
   3177 {
   3178 	struct ixgbe_hw *hw = &adapter->hw;
   3179 	device_t dev = adapter->dev;
   3180 	int layer;
   3181 
   3182 	layer = adapter->phy_layer;
   3183 
   3184 #define	ADD(mm, dd)							\
   3185 	ifmedia_add(&adapter->media, IFM_ETHER | (mm), (dd), NULL);
   3186 
   3187 	/* Media types with matching NetBSD media defines */
   3188 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T) {
   3189 		ADD(IFM_10G_T, 0);
   3190 		ADD(IFM_10G_T | IFM_FDX, 0);
   3191 	}
   3192 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T) {
   3193 		ADD(IFM_1000_T, 0);
   3194 		ADD(IFM_1000_T | IFM_FDX, 0);
   3195 	}
   3196 	if (layer & IXGBE_PHYSICAL_LAYER_100BASE_TX) {
   3197 		ADD(IFM_100_TX, 0);
   3198 		ADD(IFM_100_TX | IFM_FDX, 0);
   3199 	}
   3200 
   3201 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
   3202 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA) {
   3203 		ADD(IFM_10G_TWINAX, 0);
   3204 		ADD(IFM_10G_TWINAX | IFM_FDX, 0);
   3205 	}
   3206 
   3207 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR) {
   3208 		ADD(IFM_10G_LR, 0);
   3209 		ADD(IFM_10G_LR | IFM_FDX, 0);
   3210 		if (hw->phy.multispeed_fiber) {
   3211 			ADD(IFM_1000_LX, 0);
   3212 			ADD(IFM_1000_LX | IFM_FDX, 0);
   3213 		}
   3214 	}
   3215 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
   3216 		ADD(IFM_10G_SR, 0);
   3217 		ADD(IFM_10G_SR | IFM_FDX, 0);
   3218 		if (hw->phy.multispeed_fiber) {
   3219 			ADD(IFM_1000_SX, 0);
   3220 			ADD(IFM_1000_SX | IFM_FDX, 0);
   3221 		}
   3222 	} else if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX) {
   3223 		ADD(IFM_1000_SX, 0);
   3224 		ADD(IFM_1000_SX | IFM_FDX, 0);
   3225 	}
   3226 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4) {
   3227 		ADD(IFM_10G_CX4, 0);
   3228 		ADD(IFM_10G_CX4 | IFM_FDX, 0);
   3229 	}
   3230 
   3231 #ifdef IFM_ETH_XTYPE
   3232 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
   3233 		ADD(IFM_10G_KR, 0);
   3234 		ADD(IFM_10G_KR | IFM_FDX, 0);
   3235 	}
   3236 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
   3237 		ADD(AIFM_10G_KX4, 0);
   3238 		ADD(AIFM_10G_KX4 | IFM_FDX, 0);
   3239 	}
   3240 #else
   3241 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
   3242 		device_printf(dev, "Media supported: 10GbaseKR\n");
   3243 		device_printf(dev, "10GbaseKR mapped to 10GbaseSR\n");
   3244 		ADD(IFM_10G_SR, 0);
   3245 		ADD(IFM_10G_SR | IFM_FDX, 0);
   3246 	}
   3247 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
   3248 		device_printf(dev, "Media supported: 10GbaseKX4\n");
   3249 		device_printf(dev, "10GbaseKX4 mapped to 10GbaseCX4\n");
   3250 		ADD(IFM_10G_CX4, 0);
   3251 		ADD(IFM_10G_CX4 | IFM_FDX, 0);
   3252 	}
   3253 #endif
   3254 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX) {
   3255 		ADD(IFM_1000_KX, 0);
   3256 		ADD(IFM_1000_KX | IFM_FDX, 0);
   3257 	}
   3258 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_BX)
   3259 		device_printf(dev, "Media supported: 1000baseBX\n");
   3260 	/* XXX no ifmedia_set? */
   3261 
   3262 	if (hw->device_id == IXGBE_DEV_ID_82598AT) {
   3263 		ADD(IFM_1000_T | IFM_FDX, 0);
   3264 		ADD(IFM_1000_T, 0);
   3265 	}
   3266 
   3267 	ADD(IFM_AUTO, 0);
   3268 
   3269 #undef ADD
   3270 }
   3271 
   3272 static void
   3273 ixgbe_config_link(struct adapter *adapter)
   3274 {
   3275 	struct ixgbe_hw *hw = &adapter->hw;
   3276 	u32	autoneg, err = 0;
   3277 	bool	sfp, negotiate;
   3278 
   3279 	sfp = ixgbe_is_sfp(hw);
   3280 
   3281 	if (sfp) {
   3282 		kpreempt_disable();
   3283 		softint_schedule(adapter->mod_si);
   3284 		kpreempt_enable();
   3285 	} else {
   3286 		if (hw->mac.ops.check_link)
   3287 			err = ixgbe_check_link(hw, &adapter->link_speed,
   3288 			    &adapter->link_up, FALSE);
   3289 		if (err)
   3290 			goto out;
   3291 		autoneg = hw->phy.autoneg_advertised;
   3292 		if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
   3293                 	err  = hw->mac.ops.get_link_capabilities(hw,
   3294 			    &autoneg, &negotiate);
   3295 		else
   3296 			negotiate = 0;
   3297 		if (err)
   3298 			goto out;
   3299 		if (hw->mac.ops.setup_link)
   3300                 	err = hw->mac.ops.setup_link(hw,
   3301 			    autoneg, adapter->link_up);
   3302 	}
   3303 out:
   3304 	return;
   3305 }
   3306 
   3307 
   3308 /*********************************************************************
   3309  *
   3310  *  Enable transmit units.
   3311  *
   3312  **********************************************************************/
   3313 static void
   3314 ixgbe_initialize_transmit_units(struct adapter *adapter)
   3315 {
   3316 	struct tx_ring	*txr = adapter->tx_rings;
   3317 	struct ixgbe_hw	*hw = &adapter->hw;
   3318 
   3319 	/* Setup the Base and Length of the Tx Descriptor Ring */
   3320 	for (int i = 0; i < adapter->num_queues; i++, txr++) {
   3321 		u64	tdba = txr->txdma.dma_paddr;
   3322 		u32	txctrl = 0;
   3323 		int	j = txr->me;
   3324 
   3325 		IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
   3326 		       (tdba & 0x00000000ffffffffULL));
   3327 		IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
   3328 		IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j),
   3329 		    adapter->num_tx_desc * sizeof(union ixgbe_adv_tx_desc));
   3330 
   3331 		/* Setup the HW Tx Head and Tail descriptor pointers */
   3332 		IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
   3333 		IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
   3334 
   3335 		/* Cache the tail address */
   3336 		txr->tail = IXGBE_TDT(j);
   3337 
   3338 		/* Disable Head Writeback */
   3339 		/*
   3340 		 * Note: for X550 series devices, these registers are actually
   3341 		 * prefixed with TPH_ isntead of DCA_, but the addresses and
   3342 		 * fields remain the same.
   3343 		 */
   3344 		switch (hw->mac.type) {
   3345 		case ixgbe_mac_82598EB:
   3346 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
   3347 			break;
   3348 		default:
   3349 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(j));
   3350 			break;
   3351                 }
   3352 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
   3353 		switch (hw->mac.type) {
   3354 		case ixgbe_mac_82598EB:
   3355 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
   3356 			break;
   3357 		default:
   3358 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(j), txctrl);
   3359 			break;
   3360 		}
   3361 
   3362 	}
   3363 
   3364 	if (hw->mac.type != ixgbe_mac_82598EB) {
   3365 		u32 dmatxctl, rttdcs;
   3366 #ifdef PCI_IOV
   3367 		enum ixgbe_iov_mode mode = ixgbe_get_iov_mode(adapter);
   3368 #endif
   3369 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
   3370 		dmatxctl |= IXGBE_DMATXCTL_TE;
   3371 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
   3372 		/* Disable arbiter to set MTQC */
   3373 		rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
   3374 		rttdcs |= IXGBE_RTTDCS_ARBDIS;
   3375 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
   3376 #ifdef PCI_IOV
   3377 		IXGBE_WRITE_REG(hw, IXGBE_MTQC, ixgbe_get_mtqc(mode));
   3378 #else
   3379 		IXGBE_WRITE_REG(hw, IXGBE_MTQC, IXGBE_MTQC_64Q_1PB);
   3380 #endif
   3381 		rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
   3382 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
   3383 	}
   3384 
   3385 	return;
   3386 }
   3387 
   3388 static void
   3389 ixgbe_initialize_rss_mapping(struct adapter *adapter)
   3390 {
   3391 	struct ixgbe_hw	*hw = &adapter->hw;
   3392 	u32 reta = 0, mrqc, rss_key[10];
   3393 	int queue_id, table_size, index_mult;
   3394 #ifdef	RSS
   3395 	u32 rss_hash_config;
   3396 #endif
   3397 #ifdef PCI_IOV
   3398 	enum ixgbe_iov_mode mode;
   3399 #endif
   3400 
   3401 #ifdef	RSS
   3402 	/* Fetch the configured RSS key */
   3403 	rss_getkey((uint8_t *) &rss_key);
   3404 #else
   3405 	/* set up random bits */
   3406 	cprng_fast(&rss_key, sizeof(rss_key));
   3407 #endif
   3408 
   3409 	/* Set multiplier for RETA setup and table size based on MAC */
   3410 	index_mult = 0x1;
   3411 	table_size = 128;
   3412 	switch (adapter->hw.mac.type) {
   3413 	case ixgbe_mac_82598EB:
   3414 		index_mult = 0x11;
   3415 		break;
   3416 	case ixgbe_mac_X550:
   3417 	case ixgbe_mac_X550EM_x:
   3418 		table_size = 512;
   3419 		break;
   3420 	default:
   3421 		break;
   3422 	}
   3423 
   3424 	/* Set up the redirection table */
   3425 	for (int i = 0, j = 0; i < table_size; i++, j++) {
   3426 		if (j == adapter->num_queues) j = 0;
   3427 #ifdef	RSS
   3428 		/*
   3429 		 * Fetch the RSS bucket id for the given indirection entry.
   3430 		 * Cap it at the number of configured buckets (which is
   3431 		 * num_queues.)
   3432 		 */
   3433 		queue_id = rss_get_indirection_to_bucket(i);
   3434 		queue_id = queue_id % adapter->num_queues;
   3435 #else
   3436 		queue_id = (j * index_mult);
   3437 #endif
   3438 		/*
   3439 		 * The low 8 bits are for hash value (n+0);
   3440 		 * The next 8 bits are for hash value (n+1), etc.
   3441 		 */
   3442 		reta = reta >> 8;
   3443 		reta = reta | ( ((uint32_t) queue_id) << 24);
   3444 		if ((i & 3) == 3) {
   3445 			if (i < 128)
   3446 				IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
   3447 			else
   3448 				IXGBE_WRITE_REG(hw, IXGBE_ERETA((i >> 2) - 32), reta);
   3449 			reta = 0;
   3450 		}
   3451 	}
   3452 
   3453 	/* Now fill our hash function seeds */
   3454 	for (int i = 0; i < 10; i++)
   3455 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
   3456 
   3457 	/* Perform hash on these packet types */
   3458 #ifdef	RSS
   3459 	mrqc = IXGBE_MRQC_RSSEN;
   3460 	rss_hash_config = rss_gethashconfig();
   3461 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
   3462 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
   3463 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
   3464 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
   3465 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
   3466 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
   3467 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
   3468 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
   3469 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
   3470 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
   3471 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
   3472 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
   3473 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
   3474 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
   3475 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4_EX)
   3476 		device_printf(adapter->dev,
   3477 		    "%s: RSS_HASHTYPE_RSS_UDP_IPV4_EX defined, "
   3478 		    "but not supported\n", __func__);
   3479 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
   3480 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
   3481 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
   3482 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
   3483 #else
   3484 	/*
   3485 	 * Disable UDP - IP fragments aren't currently being handled
   3486 	 * and so we end up with a mix of 2-tuple and 4-tuple
   3487 	 * traffic.
   3488 	 */
   3489 	mrqc = IXGBE_MRQC_RSSEN
   3490 	     | IXGBE_MRQC_RSS_FIELD_IPV4
   3491 	     | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
   3492 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
   3493 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX
   3494 	     | IXGBE_MRQC_RSS_FIELD_IPV6
   3495 	     | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
   3496 	;
   3497 #endif /* RSS */
   3498 #ifdef PCI_IOV
   3499 	mode = ixgbe_get_iov_mode(adapter);
   3500 	mrqc |= ixgbe_get_mrqc(mode);
   3501 #endif
   3502 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
   3503 }
   3504 
   3505 
   3506 /*********************************************************************
   3507  *
   3508  *  Setup receive registers and features.
   3509  *
   3510  **********************************************************************/
   3511 #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
   3512 
   3513 #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
   3514 
   3515 static void
   3516 ixgbe_initialize_receive_units(struct adapter *adapter)
   3517 {
   3518 	int i;
   3519 	struct	rx_ring	*rxr = adapter->rx_rings;
   3520 	struct ixgbe_hw	*hw = &adapter->hw;
   3521 	struct ifnet   *ifp = adapter->ifp;
   3522 	u32		bufsz, fctrl, srrctl, rxcsum;
   3523 	u32		hlreg;
   3524 
   3525 	/*
   3526 	 * Make sure receives are disabled while
   3527 	 * setting up the descriptor ring
   3528 	 */
   3529 	ixgbe_disable_rx(hw);
   3530 
   3531 	/* Enable broadcasts */
   3532 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
   3533 	fctrl |= IXGBE_FCTRL_BAM;
   3534 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
   3535 		fctrl |= IXGBE_FCTRL_DPF;
   3536 		fctrl |= IXGBE_FCTRL_PMCF;
   3537 	}
   3538 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
   3539 
   3540 	/* Set for Jumbo Frames? */
   3541 	hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
   3542 	if (ifp->if_mtu > ETHERMTU)
   3543 		hlreg |= IXGBE_HLREG0_JUMBOEN;
   3544 	else
   3545 		hlreg &= ~IXGBE_HLREG0_JUMBOEN;
   3546 #ifdef DEV_NETMAP
   3547 	/* crcstrip is conditional in netmap (in RDRXCTL too ?) */
   3548 	if (ifp->if_capenable & IFCAP_NETMAP && !ix_crcstrip)
   3549 		hlreg &= ~IXGBE_HLREG0_RXCRCSTRP;
   3550 	else
   3551 		hlreg |= IXGBE_HLREG0_RXCRCSTRP;
   3552 #endif /* DEV_NETMAP */
   3553 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
   3554 
   3555 	bufsz = (adapter->rx_mbuf_sz +
   3556 	    BSIZEPKT_ROUNDUP) >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
   3557 
   3558 	for (i = 0; i < adapter->num_queues; i++, rxr++) {
   3559 		u64 rdba = rxr->rxdma.dma_paddr;
   3560 		int j = rxr->me;
   3561 		u32 tqsmreg, reg;
   3562 		int regnum = i / 4;	/* 1 register per 4 queues */
   3563 		int regshift = i % 4;	/* 4 bits per 1 queue */
   3564 
   3565 		/* Setup the Base and Length of the Rx Descriptor Ring */
   3566 		IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j),
   3567 			       (rdba & 0x00000000ffffffffULL));
   3568 		IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
   3569 		IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j),
   3570 		    adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
   3571 
   3572 		/* Set up the SRRCTL register */
   3573 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(j));
   3574 		srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
   3575 		srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
   3576 		srrctl |= bufsz;
   3577 		srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
   3578 
   3579 		/* Set RQSMR (Receive Queue Statistic Mapping) register */
   3580 		reg = IXGBE_READ_REG(hw, IXGBE_RQSMR(regnum));
   3581 		reg &= ~(0x000000ff << (regshift * 8));
   3582 		reg |= i << (regshift * 8);
   3583 		IXGBE_WRITE_REG(hw, IXGBE_RQSMR(regnum), reg);
   3584 
   3585 		/*
   3586 		 * Set RQSMR (Receive Queue Statistic Mapping) register.
   3587 		 * Register location for queue 0...7 are different between
   3588 		 * 82598 and newer.
   3589 		 */
   3590 		if (adapter->hw.mac.type == ixgbe_mac_82598EB)
   3591 			tqsmreg = IXGBE_TQSMR(regnum);
   3592 		else
   3593 			tqsmreg = IXGBE_TQSM(regnum);
   3594 		reg = IXGBE_READ_REG(hw, tqsmreg);
   3595 		reg &= ~(0x000000ff << (regshift * 8));
   3596 		reg |= i << (regshift * 8);
   3597 		IXGBE_WRITE_REG(hw, tqsmreg, reg);
   3598 
   3599 		/*
   3600 		 * Set DROP_EN iff we have no flow control and >1 queue.
   3601 		 * Note that srrctl was cleared shortly before during reset,
   3602 		 * so we do not need to clear the bit, but do it just in case
   3603 		 * this code is moved elsewhere.
   3604 		 */
   3605 		if (adapter->num_queues > 1 &&
   3606 		    adapter->hw.fc.requested_mode == ixgbe_fc_none) {
   3607 			srrctl |= IXGBE_SRRCTL_DROP_EN;
   3608 		} else {
   3609 			srrctl &= ~IXGBE_SRRCTL_DROP_EN;
   3610 		}
   3611 
   3612 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(j), srrctl);
   3613 
   3614 		/* Setup the HW Rx Head and Tail Descriptor Pointers */
   3615 		IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
   3616 		IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
   3617 
   3618 		/* Set the driver rx tail address */
   3619 		rxr->tail =  IXGBE_RDT(rxr->me);
   3620 	}
   3621 
   3622 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
   3623 		u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
   3624 			      IXGBE_PSRTYPE_UDPHDR |
   3625 			      IXGBE_PSRTYPE_IPV4HDR |
   3626 			      IXGBE_PSRTYPE_IPV6HDR;
   3627 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
   3628 	}
   3629 
   3630 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
   3631 
   3632 	ixgbe_initialize_rss_mapping(adapter);
   3633 
   3634 	if (adapter->num_queues > 1) {
   3635 		/* RSS and RX IPP Checksum are mutually exclusive */
   3636 		rxcsum |= IXGBE_RXCSUM_PCSD;
   3637 	}
   3638 
   3639 	if (ifp->if_capenable & IFCAP_RXCSUM)
   3640 		rxcsum |= IXGBE_RXCSUM_PCSD;
   3641 
   3642 	/* This is useful for calculating UDP/IP fragment checksums */
   3643 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
   3644 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
   3645 
   3646 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
   3647 
   3648 	return;
   3649 }
   3650 
   3651 
   3652 #if 0	/* XXX Badly need to overhaul vlan(4) on NetBSD. */
   3653 /*
   3654 ** This routine is run via an vlan config EVENT,
   3655 ** it enables us to use the HW Filter table since
   3656 ** we can get the vlan id. This just creates the
   3657 ** entry in the soft version of the VFTA, init will
   3658 ** repopulate the real table.
   3659 */
   3660 static void
   3661 ixgbe_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   3662 {
   3663 	struct adapter	*adapter = ifp->if_softc;
   3664 	u16		index, bit;
   3665 
   3666 	if (ifp->if_softc !=  arg)   /* Not our event */
   3667 		return;
   3668 
   3669 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   3670 		return;
   3671 
   3672 	IXGBE_CORE_LOCK(adapter);
   3673 	index = (vtag >> 5) & 0x7F;
   3674 	bit = vtag & 0x1F;
   3675 	adapter->shadow_vfta[index] |= (1 << bit);
   3676 	ixgbe_setup_vlan_hw_support(adapter);
   3677 	IXGBE_CORE_UNLOCK(adapter);
   3678 }
   3679 
   3680 /*
   3681 ** This routine is run via an vlan
   3682 ** unconfig EVENT, remove our entry
   3683 ** in the soft vfta.
   3684 */
   3685 static void
   3686 ixgbe_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   3687 {
   3688 	struct adapter	*adapter = ifp->if_softc;
   3689 	u16		index, bit;
   3690 
   3691 	if (ifp->if_softc !=  arg)
   3692 		return;
   3693 
   3694 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   3695 		return;
   3696 
   3697 	IXGBE_CORE_LOCK(adapter);
   3698 	index = (vtag >> 5) & 0x7F;
   3699 	bit = vtag & 0x1F;
   3700 	adapter->shadow_vfta[index] &= ~(1 << bit);
   3701 	/* Re-init to load the changes */
   3702 	ixgbe_setup_vlan_hw_support(adapter);
   3703 	IXGBE_CORE_UNLOCK(adapter);
   3704 }
   3705 #endif
   3706 
   3707 static void
   3708 ixgbe_setup_vlan_hw_support(struct adapter *adapter)
   3709 {
   3710 	struct ethercom *ec = &adapter->osdep.ec;
   3711 	struct ixgbe_hw *hw = &adapter->hw;
   3712 	struct rx_ring	*rxr;
   3713 	u32		ctrl;
   3714 
   3715 
   3716 	/*
   3717 	** We get here thru init_locked, meaning
   3718 	** a soft reset, this has already cleared
   3719 	** the VFTA and other state, so if there
   3720 	** have been no vlan's registered do nothing.
   3721 	*/
   3722 	if (!VLAN_ATTACHED(&adapter->osdep.ec))
   3723 		return;
   3724 
   3725 	/* Setup the queues for vlans */
   3726 	for (int i = 0; i < adapter->num_queues; i++) {
   3727 		rxr = &adapter->rx_rings[i];
   3728 		/* On 82599 the VLAN enable is per/queue in RXDCTL */
   3729 		if (hw->mac.type != ixgbe_mac_82598EB) {
   3730 			ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
   3731 			ctrl |= IXGBE_RXDCTL_VME;
   3732 			IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), ctrl);
   3733 		}
   3734 		rxr->vtag_strip = TRUE;
   3735 	}
   3736 
   3737 	if ((ec->ec_capenable & ETHERCAP_VLAN_HWFILTER) == 0)
   3738 		return;
   3739 	/*
   3740 	** A soft reset zero's out the VFTA, so
   3741 	** we need to repopulate it now.
   3742 	*/
   3743 	for (int i = 0; i < IXGBE_VFTA_SIZE; i++)
   3744 		if (adapter->shadow_vfta[i] != 0)
   3745 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(i),
   3746 			    adapter->shadow_vfta[i]);
   3747 
   3748 	ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
   3749 	/* Enable the Filter Table if enabled */
   3750 	if (ec->ec_capenable & ETHERCAP_VLAN_HWFILTER) {
   3751 		ctrl &= ~IXGBE_VLNCTRL_CFIEN;
   3752 		ctrl |= IXGBE_VLNCTRL_VFE;
   3753 	}
   3754 	if (hw->mac.type == ixgbe_mac_82598EB)
   3755 		ctrl |= IXGBE_VLNCTRL_VME;
   3756 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
   3757 }
   3758 
   3759 static void
   3760 ixgbe_enable_intr(struct adapter *adapter)
   3761 {
   3762 	struct ixgbe_hw	*hw = &adapter->hw;
   3763 	struct ix_queue	*que = adapter->queues;
   3764 	u32		mask, fwsm;
   3765 
   3766 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
   3767 	/* Enable Fan Failure detection */
   3768 	if (hw->device_id == IXGBE_DEV_ID_82598AT)
   3769 		    mask |= IXGBE_EIMS_GPI_SDP1;
   3770 
   3771 	switch (adapter->hw.mac.type) {
   3772 		case ixgbe_mac_82599EB:
   3773 			mask |= IXGBE_EIMS_ECC;
   3774 			/* Temperature sensor on some adapters */
   3775 			mask |= IXGBE_EIMS_GPI_SDP0;
   3776 			/* SFP+ (RX_LOS_N & MOD_ABS_N) */
   3777 			mask |= IXGBE_EIMS_GPI_SDP1;
   3778 			mask |= IXGBE_EIMS_GPI_SDP2;
   3779 #ifdef IXGBE_FDIR
   3780 			mask |= IXGBE_EIMS_FLOW_DIR;
   3781 #endif
   3782 #ifdef PCI_IOV
   3783 			mask |= IXGBE_EIMS_MAILBOX;
   3784 #endif
   3785 			break;
   3786 		case ixgbe_mac_X540:
   3787 			/* Detect if Thermal Sensor is enabled */
   3788 			fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
   3789 			if (fwsm & IXGBE_FWSM_TS_ENABLED)
   3790 				mask |= IXGBE_EIMS_TS;
   3791 			mask |= IXGBE_EIMS_ECC;
   3792 #ifdef IXGBE_FDIR
   3793 			mask |= IXGBE_EIMS_FLOW_DIR;
   3794 #endif
   3795 			break;
   3796 		case ixgbe_mac_X550:
   3797 		case ixgbe_mac_X550EM_x:
   3798 			/* MAC thermal sensor is automatically enabled */
   3799 			mask |= IXGBE_EIMS_TS;
   3800 			/* Some devices use SDP0 for important information */
   3801 			if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
   3802 			    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
   3803 				mask |= IXGBE_EIMS_GPI_SDP0_BY_MAC(hw);
   3804 			mask |= IXGBE_EIMS_ECC;
   3805 #ifdef IXGBE_FDIR
   3806 			mask |= IXGBE_EIMS_FLOW_DIR;
   3807 #endif
   3808 #ifdef PCI_IOV
   3809 			mask |= IXGBE_EIMS_MAILBOX;
   3810 #endif
   3811 		/* falls through */
   3812 		default:
   3813 			break;
   3814 	}
   3815 
   3816 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
   3817 
   3818 	/* With MSI-X we use auto clear */
   3819 	if (adapter->msix_mem) {
   3820 		mask = IXGBE_EIMS_ENABLE_MASK;
   3821 		/* Don't autoclear Link */
   3822 		mask &= ~IXGBE_EIMS_OTHER;
   3823 		mask &= ~IXGBE_EIMS_LSC;
   3824 #ifdef PCI_IOV
   3825 		mask &= ~IXGBE_EIMS_MAILBOX;
   3826 #endif
   3827 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
   3828 	}
   3829 
   3830 	/*
   3831 	** Now enable all queues, this is done separately to
   3832 	** allow for handling the extended (beyond 32) MSIX
   3833 	** vectors that can be used by 82599
   3834 	*/
   3835         for (int i = 0; i < adapter->num_queues; i++, que++)
   3836                 ixgbe_enable_queue(adapter, que->msix);
   3837 
   3838 	IXGBE_WRITE_FLUSH(hw);
   3839 
   3840 	return;
   3841 }
   3842 
   3843 static void
   3844 ixgbe_disable_intr(struct adapter *adapter)
   3845 {
   3846 	if (adapter->msix_mem)
   3847 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, 0);
   3848 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
   3849 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
   3850 	} else {
   3851 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, 0xFFFF0000);
   3852 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), ~0);
   3853 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), ~0);
   3854 	}
   3855 	IXGBE_WRITE_FLUSH(&adapter->hw);
   3856 	return;
   3857 }
   3858 
   3859 /*
   3860 ** Get the width and transaction speed of
   3861 ** the slot this adapter is plugged into.
   3862 */
   3863 static void
   3864 ixgbe_get_slot_info(struct adapter *adapter)
   3865 {
   3866 	device_t		dev = adapter->dev;
   3867 	struct ixgbe_hw		*hw = &adapter->hw;
   3868 	struct ixgbe_mac_info	*mac = &hw->mac;
   3869 	u16			link;
   3870 
   3871 	/* For most devices simply call the shared code routine */
   3872 	if (hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) {
   3873 		ixgbe_get_bus_info(hw);
   3874 		/* These devices don't use PCI-E */
   3875 		switch (hw->mac.type) {
   3876 		case ixgbe_mac_X550EM_x:
   3877 			return;
   3878 		default:
   3879 			goto display;
   3880 		}
   3881 	}
   3882 
   3883 	/*
   3884 	** For the Quad port adapter we need to parse back
   3885 	** up the PCI tree to find the speed of the expansion
   3886 	** slot into which this adapter is plugged. A bit more work.
   3887 	*/
   3888 	dev = device_parent(device_parent(dev));
   3889 #ifdef IXGBE_DEBUG
   3890 	device_printf(dev, "parent pcib = %x,%x,%x\n",
   3891 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
   3892 #endif
   3893 	dev = device_parent(device_parent(dev));
   3894 #ifdef IXGBE_DEBUG
   3895 	device_printf(dev, "slot pcib = %x,%x,%x\n",
   3896 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
   3897 #endif
   3898 	/* Now get the PCI Express Capabilities offset */
   3899 	/* ...and read the Link Status Register */
   3900 	link = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_LINK_STATUS);
   3901 	switch (link & IXGBE_PCI_LINK_WIDTH) {
   3902 	case IXGBE_PCI_LINK_WIDTH_1:
   3903 		hw->bus.width = ixgbe_bus_width_pcie_x1;
   3904 		break;
   3905 	case IXGBE_PCI_LINK_WIDTH_2:
   3906 		hw->bus.width = ixgbe_bus_width_pcie_x2;
   3907 		break;
   3908 	case IXGBE_PCI_LINK_WIDTH_4:
   3909 		hw->bus.width = ixgbe_bus_width_pcie_x4;
   3910 		break;
   3911 	case IXGBE_PCI_LINK_WIDTH_8:
   3912 		hw->bus.width = ixgbe_bus_width_pcie_x8;
   3913 		break;
   3914 	default:
   3915 		hw->bus.width = ixgbe_bus_width_unknown;
   3916 		break;
   3917 	}
   3918 
   3919 	switch (link & IXGBE_PCI_LINK_SPEED) {
   3920 	case IXGBE_PCI_LINK_SPEED_2500:
   3921 		hw->bus.speed = ixgbe_bus_speed_2500;
   3922 		break;
   3923 	case IXGBE_PCI_LINK_SPEED_5000:
   3924 		hw->bus.speed = ixgbe_bus_speed_5000;
   3925 		break;
   3926 	case IXGBE_PCI_LINK_SPEED_8000:
   3927 		hw->bus.speed = ixgbe_bus_speed_8000;
   3928 		break;
   3929 	default:
   3930 		hw->bus.speed = ixgbe_bus_speed_unknown;
   3931 		break;
   3932 	}
   3933 
   3934 	mac->ops.set_lan_id(hw);
   3935 
   3936 display:
   3937 	device_printf(dev,"PCI Express Bus: Speed %s Width %s\n",
   3938 	    ((hw->bus.speed == ixgbe_bus_speed_8000) ? "8.0GT/s":
   3939 	    (hw->bus.speed == ixgbe_bus_speed_5000) ? "5.0GT/s":
   3940 	    (hw->bus.speed == ixgbe_bus_speed_2500) ? "2.5GT/s":"Unknown"),
   3941 	    (hw->bus.width == ixgbe_bus_width_pcie_x8) ? "x8" :
   3942 	    (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "x4" :
   3943 	    (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "x1" :
   3944 	    ("Unknown"));
   3945 
   3946 	if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   3947 	    ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
   3948 	    (hw->bus.speed == ixgbe_bus_speed_2500))) {
   3949 		device_printf(dev, "PCI-Express bandwidth available"
   3950 		    " for this card\n     is not sufficient for"
   3951 		    " optimal performance.\n");
   3952 		device_printf(dev, "For optimal performance a x8 "
   3953 		    "PCIE, or x4 PCIE Gen2 slot is required.\n");
   3954         }
   3955 	if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   3956 	    ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
   3957 	    (hw->bus.speed < ixgbe_bus_speed_8000))) {
   3958 		device_printf(dev, "PCI-Express bandwidth available"
   3959 		    " for this card\n     is not sufficient for"
   3960 		    " optimal performance.\n");
   3961 		device_printf(dev, "For optimal performance a x8 "
   3962 		    "PCIE Gen3 slot is required.\n");
   3963         }
   3964 
   3965 	return;
   3966 }
   3967 
   3968 
   3969 /*
   3970 ** Setup the correct IVAR register for a particular MSIX interrupt
   3971 **   (yes this is all very magic and confusing :)
   3972 **  - entry is the register array entry
   3973 **  - vector is the MSIX vector for this queue
   3974 **  - type is RX/TX/MISC
   3975 */
   3976 static void
   3977 ixgbe_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
   3978 {
   3979 	struct ixgbe_hw *hw = &adapter->hw;
   3980 	u32 ivar, index;
   3981 
   3982 	vector |= IXGBE_IVAR_ALLOC_VAL;
   3983 
   3984 	switch (hw->mac.type) {
   3985 
   3986 	case ixgbe_mac_82598EB:
   3987 		if (type == -1)
   3988 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
   3989 		else
   3990 			entry += (type * 64);
   3991 		index = (entry >> 2) & 0x1F;
   3992 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
   3993 		ivar &= ~(0xFF << (8 * (entry & 0x3)));
   3994 		ivar |= (vector << (8 * (entry & 0x3)));
   3995 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
   3996 		break;
   3997 
   3998 	case ixgbe_mac_82599EB:
   3999 	case ixgbe_mac_X540:
   4000 	case ixgbe_mac_X550:
   4001 	case ixgbe_mac_X550EM_x:
   4002 		if (type == -1) { /* MISC IVAR */
   4003 			index = (entry & 1) * 8;
   4004 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
   4005 			ivar &= ~(0xFF << index);
   4006 			ivar |= (vector << index);
   4007 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
   4008 		} else {	/* RX/TX IVARS */
   4009 			index = (16 * (entry & 1)) + (8 * type);
   4010 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
   4011 			ivar &= ~(0xFF << index);
   4012 			ivar |= (vector << index);
   4013 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
   4014 		}
   4015 
   4016 	default:
   4017 		break;
   4018 	}
   4019 }
   4020 
   4021 static void
   4022 ixgbe_configure_ivars(struct adapter *adapter)
   4023 {
   4024 	struct  ix_queue	*que = adapter->queues;
   4025 	u32			newitr;
   4026 
   4027 	if (ixgbe_max_interrupt_rate > 0)
   4028 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
   4029 	else {
   4030 		/*
   4031 		** Disable DMA coalescing if interrupt moderation is
   4032 		** disabled.
   4033 		*/
   4034 		adapter->dmac = 0;
   4035 		newitr = 0;
   4036 	}
   4037 
   4038         for (int i = 0; i < adapter->num_queues; i++, que++) {
   4039 		struct rx_ring *rxr = &adapter->rx_rings[i];
   4040 		struct tx_ring *txr = &adapter->tx_rings[i];
   4041 		/* First the RX queue entry */
   4042                 ixgbe_set_ivar(adapter, rxr->me, que->msix, 0);
   4043 		/* ... and the TX */
   4044 		ixgbe_set_ivar(adapter, txr->me, que->msix, 1);
   4045 		/* Set an Initial EITR value */
   4046                 IXGBE_WRITE_REG(&adapter->hw,
   4047                     IXGBE_EITR(que->msix), newitr);
   4048 	}
   4049 
   4050 	/* For the Link interrupt */
   4051         ixgbe_set_ivar(adapter, 1, adapter->vector, -1);
   4052 }
   4053 
   4054 /*
   4055 ** ixgbe_sfp_probe - called in the local timer to
   4056 ** determine if a port had optics inserted.
   4057 */
   4058 static bool
   4059 ixgbe_sfp_probe(struct adapter *adapter)
   4060 {
   4061 	struct ixgbe_hw	*hw = &adapter->hw;
   4062 	device_t	dev = adapter->dev;
   4063 	bool		result = FALSE;
   4064 
   4065 	if ((hw->phy.type == ixgbe_phy_nl) &&
   4066 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
   4067 		s32 ret = hw->phy.ops.identify_sfp(hw);
   4068 		if (ret)
   4069                         goto out;
   4070 		ret = hw->phy.ops.reset(hw);
   4071 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   4072 			device_printf(dev,"Unsupported SFP+ module detected!");
   4073 			device_printf(dev, "Reload driver with supported module.\n");
   4074 			adapter->sfp_probe = FALSE;
   4075                         goto out;
   4076 		} else
   4077 			device_printf(dev, "SFP+ module detected!\n");
   4078 		/* We now have supported optics */
   4079 		adapter->sfp_probe = FALSE;
   4080 		/* Set the optics type so system reports correctly */
   4081 		ixgbe_setup_optics(adapter);
   4082 		result = TRUE;
   4083 	}
   4084 out:
   4085 	return (result);
   4086 }
   4087 
   4088 /*
   4089 ** Tasklet handler for MSIX Link interrupts
   4090 **  - do outside interrupt since it might sleep
   4091 */
   4092 static void
   4093 ixgbe_handle_link(void *context)
   4094 {
   4095 	struct adapter  *adapter = context;
   4096 	struct ixgbe_hw *hw = &adapter->hw;
   4097 
   4098 	ixgbe_check_link(hw,
   4099 	    &adapter->link_speed, &adapter->link_up, 0);
   4100 	ixgbe_update_link_status(adapter);
   4101 
   4102 	/* Re-enable link interrupts */
   4103 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_LSC);
   4104 }
   4105 
   4106 /*
   4107 ** Tasklet for handling SFP module interrupts
   4108 */
   4109 static void
   4110 ixgbe_handle_mod(void *context)
   4111 {
   4112 	struct adapter  *adapter = context;
   4113 	struct ixgbe_hw *hw = &adapter->hw;
   4114 	enum ixgbe_phy_type orig_type = hw->phy.type;
   4115 	device_t	dev = adapter->dev;
   4116 	u32 err;
   4117 
   4118 	IXGBE_CORE_LOCK(adapter);
   4119 
   4120 	/* Check to see if the PHY type changed */
   4121 	if (hw->phy.ops.identify) {
   4122 		hw->phy.type = ixgbe_phy_unknown;
   4123 		hw->phy.ops.identify(hw);
   4124 	}
   4125 
   4126 	if (hw->phy.type != orig_type) {
   4127 		device_printf(dev, "Detected phy_type %d\n", hw->phy.type);
   4128 
   4129 		if (hw->phy.type == ixgbe_phy_none) {
   4130 			hw->phy.sfp_type = ixgbe_sfp_type_unknown;
   4131 			goto out;
   4132 		}
   4133 
   4134 		/* Try to do the initialization that was skipped before */
   4135 		if (hw->phy.ops.init)
   4136 			hw->phy.ops.init(hw);
   4137 		if (hw->phy.ops.reset)
   4138 			hw->phy.ops.reset(hw);
   4139 	}
   4140 
   4141 	err = hw->phy.ops.identify_sfp(hw);
   4142 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   4143 		device_printf(dev,
   4144 		    "Unsupported SFP+ module type was detected.\n");
   4145 		goto out;
   4146 	}
   4147 
   4148 	err = hw->mac.ops.setup_sfp(hw);
   4149 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   4150 		device_printf(dev,
   4151 		    "Setup failure - unsupported SFP+ module type.\n");
   4152 		goto out;
   4153 	}
   4154 out:
   4155 	/* Update media type */
   4156 	switch (hw->mac.ops.get_media_type(hw)) {
   4157 		case ixgbe_media_type_fiber:
   4158 			adapter->optics = IFM_10G_SR;
   4159 			break;
   4160 		case ixgbe_media_type_copper:
   4161 			adapter->optics = IFM_10G_TWINAX;
   4162 			break;
   4163 		case ixgbe_media_type_cx4:
   4164 			adapter->optics = IFM_10G_CX4;
   4165 			break;
   4166 		default:
   4167 			adapter->optics = 0;
   4168 			break;
   4169 	}
   4170 
   4171 	/* Adjust media types shown in ifconfig */
   4172 	ifmedia_removeall(&adapter->media);
   4173 	/* get_supported_phy_layer will call hw->phy.ops.identify_sfp() */
   4174 	adapter->phy_layer = ixgbe_get_supported_physical_layer(hw);
   4175 	ixgbe_add_media_types(adapter);
   4176 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   4177 
   4178 	softint_schedule(adapter->msf_si);
   4179 	IXGBE_CORE_UNLOCK(adapter);
   4180 	return;
   4181 }
   4182 
   4183 
   4184 /*
   4185 ** Tasklet for handling MSF (multispeed fiber) interrupts
   4186 */
   4187 static void
   4188 ixgbe_handle_msf(void *context)
   4189 {
   4190 	struct adapter  *adapter = context;
   4191 	struct ixgbe_hw *hw = &adapter->hw;
   4192 	u32 autoneg;
   4193 	bool negotiate;
   4194 
   4195 	IXGBE_CORE_LOCK(adapter);
   4196 
   4197 	autoneg = hw->phy.autoneg_advertised;
   4198 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
   4199 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
   4200 	else
   4201 		negotiate = 0;
   4202 	if (hw->mac.ops.setup_link)
   4203 		hw->mac.ops.setup_link(hw, autoneg, TRUE);
   4204 
   4205 	IXGBE_CORE_UNLOCK(adapter);
   4206 	return;
   4207 }
   4208 
   4209 /*
   4210 ** Tasklet for handling interrupts from an external PHY
   4211 */
   4212 static void
   4213 ixgbe_handle_phy(void *context)
   4214 {
   4215 	struct adapter  *adapter = context;
   4216 	struct ixgbe_hw *hw = &adapter->hw;
   4217 	int error;
   4218 
   4219 	error = hw->phy.ops.handle_lasi(hw);
   4220 	if (error == IXGBE_ERR_OVERTEMP)
   4221 		device_printf(adapter->dev,
   4222 		    "CRITICAL: EXTERNAL PHY OVER TEMP!! "
   4223 		    " PHY will downshift to lower power state!\n");
   4224 	else if (error)
   4225 		device_printf(adapter->dev,
   4226 		    "Error handling LASI interrupt: %d\n",
   4227 		    error);
   4228 	return;
   4229 }
   4230 
   4231 #ifdef IXGBE_FDIR
   4232 /*
   4233 ** Tasklet for reinitializing the Flow Director filter table
   4234 */
   4235 static void
   4236 ixgbe_reinit_fdir(void *context)
   4237 {
   4238 	struct adapter  *adapter = context;
   4239 	struct ifnet   *ifp = adapter->ifp;
   4240 
   4241 	if (adapter->fdir_reinit != 1) /* Shouldn't happen */
   4242 		return;
   4243 	ixgbe_reinit_fdir_tables_82599(&adapter->hw);
   4244 	adapter->fdir_reinit = 0;
   4245 	/* re-enable flow director interrupts */
   4246 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_FLOW_DIR);
   4247 	/* Restart the interface */
   4248 	ifp->if_flags |= IFF_RUNNING;
   4249 	return;
   4250 }
   4251 #endif
   4252 
   4253 /*********************************************************************
   4254  *
   4255  *  Configure DMA Coalescing
   4256  *
   4257  **********************************************************************/
   4258 static void
   4259 ixgbe_config_dmac(struct adapter *adapter)
   4260 {
   4261 	struct ixgbe_hw *hw = &adapter->hw;
   4262 	struct ixgbe_dmac_config *dcfg = &hw->mac.dmac_config;
   4263 
   4264 	if (hw->mac.type < ixgbe_mac_X550 ||
   4265 	    !hw->mac.ops.dmac_config)
   4266 		return;
   4267 
   4268 	if (dcfg->watchdog_timer ^ adapter->dmac ||
   4269 	    dcfg->link_speed ^ adapter->link_speed) {
   4270 		dcfg->watchdog_timer = adapter->dmac;
   4271 		dcfg->fcoe_en = false;
   4272 		dcfg->link_speed = adapter->link_speed;
   4273 		dcfg->num_tcs = 1;
   4274 
   4275 		INIT_DEBUGOUT2("dmac settings: watchdog %d, link speed %d\n",
   4276 		    dcfg->watchdog_timer, dcfg->link_speed);
   4277 
   4278 		hw->mac.ops.dmac_config(hw);
   4279 	}
   4280 }
   4281 
   4282 /*
   4283  * Checks whether the adapter's ports are capable of
   4284  * Wake On LAN by reading the adapter's NVM.
   4285  *
   4286  * Sets each port's hw->wol_enabled value depending
   4287  * on the value read here.
   4288  */
   4289 static void
   4290 ixgbe_check_wol_support(struct adapter *adapter)
   4291 {
   4292 	struct ixgbe_hw *hw = &adapter->hw;
   4293 	u16 dev_caps = 0;
   4294 
   4295 	/* Find out WoL support for port */
   4296 	adapter->wol_support = hw->wol_enabled = 0;
   4297 	ixgbe_get_device_caps(hw, &dev_caps);
   4298 	if ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
   4299 	    ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0) &&
   4300 	      hw->bus.func == 0))
   4301 		adapter->wol_support = hw->wol_enabled = 1;
   4302 
   4303 	/* Save initial wake up filter configuration */
   4304 	adapter->wufc = IXGBE_READ_REG(hw, IXGBE_WUFC);
   4305 
   4306 	return;
   4307 }
   4308 
   4309 /*
   4310  * Prepare the adapter/port for LPLU and/or WoL
   4311  */
   4312 static int
   4313 ixgbe_setup_low_power_mode(struct adapter *adapter)
   4314 {
   4315 	struct ixgbe_hw *hw = &adapter->hw;
   4316 	device_t dev = adapter->dev;
   4317 	s32 error = 0;
   4318 
   4319 	KASSERT(mutex_owned(&adapter->core_mtx));
   4320 
   4321 	/* Limit power management flow to X550EM baseT */
   4322 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T
   4323 	    && hw->phy.ops.enter_lplu) {
   4324 		/* X550EM baseT adapters need a special LPLU flow */
   4325 		hw->phy.reset_disable = true;
   4326 		ixgbe_stop(adapter);
   4327 		error = hw->phy.ops.enter_lplu(hw);
   4328 		if (error)
   4329 			device_printf(dev,
   4330 			    "Error entering LPLU: %d\n", error);
   4331 		hw->phy.reset_disable = false;
   4332 	} else {
   4333 		/* Just stop for other adapters */
   4334 		ixgbe_stop(adapter);
   4335 	}
   4336 
   4337 	if (!hw->wol_enabled) {
   4338 		ixgbe_set_phy_power(hw, FALSE);
   4339 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
   4340 		IXGBE_WRITE_REG(hw, IXGBE_WUC, 0);
   4341 	} else {
   4342 		/* Turn off support for APM wakeup. (Using ACPI instead) */
   4343 		IXGBE_WRITE_REG(hw, IXGBE_GRC,
   4344 		    IXGBE_READ_REG(hw, IXGBE_GRC) & ~(u32)2);
   4345 
   4346 		/*
   4347 		 * Clear Wake Up Status register to prevent any previous wakeup
   4348 		 * events from waking us up immediately after we suspend.
   4349 		 */
   4350 		IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
   4351 
   4352 		/*
   4353 		 * Program the Wakeup Filter Control register with user filter
   4354 		 * settings
   4355 		 */
   4356 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, adapter->wufc);
   4357 
   4358 		/* Enable wakeups and power management in Wakeup Control */
   4359 		IXGBE_WRITE_REG(hw, IXGBE_WUC,
   4360 		    IXGBE_WUC_WKEN | IXGBE_WUC_PME_EN);
   4361 
   4362 	}
   4363 
   4364 	return error;
   4365 }
   4366 
   4367 /**********************************************************************
   4368  *
   4369  *  Update the board statistics counters.
   4370  *
   4371  **********************************************************************/
   4372 static void
   4373 ixgbe_update_stats_counters(struct adapter *adapter)
   4374 {
   4375 	struct ifnet   *ifp = adapter->ifp;
   4376 	struct ixgbe_hw *hw = &adapter->hw;
   4377 	u32 missed_rx = 0, bprc, lxon, lxoff, total;
   4378 	u64 total_missed_rx = 0;
   4379 	uint64_t crcerrs, rlec;
   4380 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
   4381 
   4382 	crcerrs = IXGBE_READ_REG(hw, IXGBE_CRCERRS);
   4383 	stats->crcerrs.ev_count += crcerrs;
   4384 	stats->illerrc.ev_count += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
   4385 	stats->errbc.ev_count += IXGBE_READ_REG(hw, IXGBE_ERRBC);
   4386 	stats->mspdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MSPDC);
   4387 	if (hw->mac.type == ixgbe_mac_X550)
   4388 		stats->mbsdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MBSDC);
   4389 
   4390 	for (int i = 0; i < __arraycount(stats->qprc); i++) {
   4391 		int j = i % adapter->num_queues;
   4392 		stats->qprc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
   4393 		stats->qptc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
   4394 		stats->qprdc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
   4395 	}
   4396 	for (int i = 0; i < __arraycount(stats->mpc); i++) {
   4397 		uint32_t mp;
   4398 		int j = i % adapter->num_queues;
   4399 
   4400 		mp = IXGBE_READ_REG(hw, IXGBE_MPC(i));
   4401 		/* global total per queue */
   4402 		stats->mpc[j].ev_count += mp;
   4403 		/* running comprehensive total for stats display */
   4404 		total_missed_rx += mp;
   4405 
   4406 		if (hw->mac.type == ixgbe_mac_82598EB)
   4407 			stats->rnbc[j].ev_count
   4408 			    += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
   4409 
   4410 	}
   4411 	stats->mpctotal.ev_count += total_missed_rx;
   4412 
   4413 	/* Document says M[LR]FC are valid when link is up and 10Gbps */
   4414 	if ((adapter->link_active == TRUE)
   4415 	    && (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL)) {
   4416 		stats->mlfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MLFC);
   4417 		stats->mrfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MRFC);
   4418 	}
   4419 	rlec = IXGBE_READ_REG(hw, IXGBE_RLEC);
   4420 	stats->rlec.ev_count += rlec;
   4421 
   4422 	/* Hardware workaround, gprc counts missed packets */
   4423 	stats->gprc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPRC) - missed_rx;
   4424 
   4425 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
   4426 	stats->lxontxc.ev_count += lxon;
   4427 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
   4428 	stats->lxofftxc.ev_count += lxoff;
   4429 	total = lxon + lxoff;
   4430 
   4431 	if (hw->mac.type != ixgbe_mac_82598EB) {
   4432 		stats->gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCL) +
   4433 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
   4434 		stats->gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
   4435 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32) - total * ETHER_MIN_LEN;
   4436 		stats->tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORL) +
   4437 		    ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
   4438 		stats->lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
   4439 		stats->lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
   4440 	} else {
   4441 		stats->lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
   4442 		stats->lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
   4443 		/* 82598 only has a counter in the high register */
   4444 		stats->gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCH);
   4445 		stats->gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCH) - total * ETHER_MIN_LEN;
   4446 		stats->tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORH);
   4447 	}
   4448 
   4449 	/*
   4450 	 * Workaround: mprc hardware is incorrectly counting
   4451 	 * broadcasts, so for now we subtract those.
   4452 	 */
   4453 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
   4454 	stats->bprc.ev_count += bprc;
   4455 	stats->mprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPRC) - ((hw->mac.type == ixgbe_mac_82598EB) ? bprc : 0);
   4456 
   4457 	stats->prc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC64);
   4458 	stats->prc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC127);
   4459 	stats->prc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC255);
   4460 	stats->prc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC511);
   4461 	stats->prc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1023);
   4462 	stats->prc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1522);
   4463 
   4464 	stats->gptc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPTC) - total;
   4465 	stats->mptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPTC) - total;
   4466 	stats->ptc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC64) - total;
   4467 
   4468 	stats->ruc.ev_count += IXGBE_READ_REG(hw, IXGBE_RUC);
   4469 	stats->rfc.ev_count += IXGBE_READ_REG(hw, IXGBE_RFC);
   4470 	stats->roc.ev_count += IXGBE_READ_REG(hw, IXGBE_ROC);
   4471 	stats->rjc.ev_count += IXGBE_READ_REG(hw, IXGBE_RJC);
   4472 	stats->mngprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
   4473 	stats->mngpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
   4474 	stats->mngptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
   4475 	stats->tpr.ev_count += IXGBE_READ_REG(hw, IXGBE_TPR);
   4476 	stats->tpt.ev_count += IXGBE_READ_REG(hw, IXGBE_TPT);
   4477 	stats->ptc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC127);
   4478 	stats->ptc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC255);
   4479 	stats->ptc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC511);
   4480 	stats->ptc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1023);
   4481 	stats->ptc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1522);
   4482 	stats->bptc.ev_count += IXGBE_READ_REG(hw, IXGBE_BPTC);
   4483 	stats->xec.ev_count += IXGBE_READ_REG(hw, IXGBE_XEC);
   4484 	stats->fccrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCCRC);
   4485 	stats->fclast.ev_count += IXGBE_READ_REG(hw, IXGBE_FCLAST);
   4486 	/* Only read FCOE on 82599 */
   4487 	if (hw->mac.type != ixgbe_mac_82598EB) {
   4488 		stats->fcoerpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
   4489 		stats->fcoeprc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
   4490 		stats->fcoeptc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
   4491 		stats->fcoedwrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
   4492 		stats->fcoedwtc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
   4493 	}
   4494 
   4495 	/* Fill out the OS statistics structure */
   4496 	/*
   4497 	 * NetBSD: Don't override if_{i|o}{packets|bytes|mcasts} with
   4498 	 * adapter->stats counters. It's required to make ifconfig -z
   4499 	 * (SOICZIFDATA) work.
   4500 	 */
   4501 	ifp->if_collisions = 0;
   4502 
   4503 	/* Rx Errors */
   4504 	ifp->if_iqdrops += total_missed_rx;
   4505 	ifp->if_ierrors += crcerrs + rlec;
   4506 }
   4507 
   4508 /** ixgbe_sysctl_tdh_handler - Handler function
   4509  *  Retrieves the TDH value from the hardware
   4510  */
   4511 static int
   4512 ixgbe_sysctl_tdh_handler(SYSCTLFN_ARGS)
   4513 {
   4514 	struct sysctlnode node = *rnode;
   4515 	uint32_t val;
   4516 	struct tx_ring *txr;
   4517 
   4518 	txr = (struct tx_ring *)node.sysctl_data;
   4519 	if (txr == NULL)
   4520 		return 0;
   4521 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDH(txr->me));
   4522 	node.sysctl_data = &val;
   4523 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   4524 }
   4525 
   4526 /** ixgbe_sysctl_tdt_handler - Handler function
   4527  *  Retrieves the TDT value from the hardware
   4528  */
   4529 static int
   4530 ixgbe_sysctl_tdt_handler(SYSCTLFN_ARGS)
   4531 {
   4532 	struct sysctlnode node = *rnode;
   4533 	uint32_t val;
   4534 	struct tx_ring *txr;
   4535 
   4536 	txr = (struct tx_ring *)node.sysctl_data;
   4537 	if (txr == NULL)
   4538 		return 0;
   4539 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDT(txr->me));
   4540 	node.sysctl_data = &val;
   4541 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   4542 }
   4543 
   4544 /** ixgbe_sysctl_rdh_handler - Handler function
   4545  *  Retrieves the RDH value from the hardware
   4546  */
   4547 static int
   4548 ixgbe_sysctl_rdh_handler(SYSCTLFN_ARGS)
   4549 {
   4550 	struct sysctlnode node = *rnode;
   4551 	uint32_t val;
   4552 	struct rx_ring *rxr;
   4553 
   4554 	rxr = (struct rx_ring *)node.sysctl_data;
   4555 	if (rxr == NULL)
   4556 		return 0;
   4557 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDH(rxr->me));
   4558 	node.sysctl_data = &val;
   4559 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   4560 }
   4561 
   4562 /** ixgbe_sysctl_rdt_handler - Handler function
   4563  *  Retrieves the RDT value from the hardware
   4564  */
   4565 static int
   4566 ixgbe_sysctl_rdt_handler(SYSCTLFN_ARGS)
   4567 {
   4568 	struct sysctlnode node = *rnode;
   4569 	uint32_t val;
   4570 	struct rx_ring *rxr;
   4571 
   4572 	rxr = (struct rx_ring *)node.sysctl_data;
   4573 	if (rxr == NULL)
   4574 		return 0;
   4575 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDT(rxr->me));
   4576 	node.sysctl_data = &val;
   4577 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   4578 }
   4579 
   4580 static int
   4581 ixgbe_sysctl_interrupt_rate_handler(SYSCTLFN_ARGS)
   4582 {
   4583 	struct sysctlnode node = *rnode;
   4584 	struct ix_queue *que;
   4585 	uint32_t reg, usec, rate;
   4586 	int error;
   4587 
   4588 	que = (struct ix_queue *)node.sysctl_data;
   4589 	if (que == NULL)
   4590 		return 0;
   4591 	reg = IXGBE_READ_REG(&que->adapter->hw, IXGBE_EITR(que->msix));
   4592 	usec = ((reg & 0x0FF8) >> 3);
   4593 	if (usec > 0)
   4594 		rate = 500000 / usec;
   4595 	else
   4596 		rate = 0;
   4597 	node.sysctl_data = &rate;
   4598 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4599 	if (error || newp == NULL)
   4600 		return error;
   4601 	reg &= ~0xfff; /* default, no limitation */
   4602 	ixgbe_max_interrupt_rate = 0;
   4603 	if (rate > 0 && rate < 500000) {
   4604 		if (rate < 1000)
   4605 			rate = 1000;
   4606 		ixgbe_max_interrupt_rate = rate;
   4607 		reg |= ((4000000/rate) & 0xff8 );
   4608 	}
   4609 	IXGBE_WRITE_REG(&que->adapter->hw, IXGBE_EITR(que->msix), reg);
   4610 	return 0;
   4611 }
   4612 
   4613 const struct sysctlnode *
   4614 ixgbe_sysctl_instance(struct adapter *adapter)
   4615 {
   4616 	const char *dvname;
   4617 	struct sysctllog **log;
   4618 	int rc;
   4619 	const struct sysctlnode *rnode;
   4620 
   4621 	if (adapter->sysctltop != NULL)
   4622 		return adapter->sysctltop;
   4623 
   4624 	log = &adapter->sysctllog;
   4625 	dvname = device_xname(adapter->dev);
   4626 
   4627 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
   4628 	    0, CTLTYPE_NODE, dvname,
   4629 	    SYSCTL_DESCR("ixgbe information and settings"),
   4630 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
   4631 		goto err;
   4632 
   4633 	return rnode;
   4634 err:
   4635 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
   4636 	return NULL;
   4637 }
   4638 
   4639 static void
   4640 ixgbe_add_device_sysctls(struct adapter *adapter)
   4641 {
   4642 	device_t dev = adapter->dev;
   4643 	struct ixgbe_hw *hw = &adapter->hw;
   4644 	struct sysctllog **log;
   4645 	const struct sysctlnode *rnode, *cnode;
   4646 
   4647 	log = &adapter->sysctllog;
   4648 
   4649 	if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   4650 		aprint_error_dev(dev, "could not create sysctl root\n");
   4651 		return;
   4652 	}
   4653 
   4654 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4655 	    CTLFLAG_READONLY, CTLTYPE_INT,
   4656 	    "num_rx_desc", SYSCTL_DESCR("Number of rx descriptors"),
   4657 	    NULL, 0, &adapter->num_rx_desc, 0, CTL_CREATE, CTL_EOL) != 0)
   4658 		aprint_error_dev(dev, "could not create sysctl\n");
   4659 
   4660 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4661 	    CTLFLAG_READONLY, CTLTYPE_INT,
   4662 	    "num_queues", SYSCTL_DESCR("Number of queues"),
   4663 	    NULL, 0, &adapter->num_queues, 0, CTL_CREATE, CTL_EOL) != 0)
   4664 		aprint_error_dev(dev, "could not create sysctl\n");
   4665 
   4666 	/* Sysctls for all devices */
   4667 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4668 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   4669 	    "fc", SYSCTL_DESCR(IXGBE_SYSCTL_DESC_SET_FC),
   4670 	    ixgbe_sysctl_flowcntl, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4671 		aprint_error_dev(dev, "could not create sysctl\n");
   4672 
   4673 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4674 	    CTLFLAG_READWRITE, CTLTYPE_BOOL,
   4675 	    "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
   4676 	    NULL, 0, &adapter->enable_aim, 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_INT,
   4681 	    "advertise_speed", SYSCTL_DESCR(IXGBE_SYSCTL_DESC_ADV_SPEED),
   4682 	    ixgbe_sysctl_advertise, 0, (void *)adapter, 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 	    "ts", SYSCTL_DESCR("Thermal Test"),
   4688 	    ixgbe_sysctl_thermal_test, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4689 		aprint_error_dev(dev, "could not create sysctl\n");
   4690 
   4691 #ifdef IXGBE_DEBUG
   4692 	/* testing sysctls (for all devices) */
   4693 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4694 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   4695 	    "power_state", SYSCTL_DESCR("PCI Power State"),
   4696 	    ixgbe_sysctl_power_state, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4697 		aprint_error_dev(dev, "could not create sysctl\n");
   4698 
   4699 	if (sysctl_createv(log, 0, &rnode, &cnode,
   4700 	    CTLFLAG_READONLY, CTLTYPE_STRING,
   4701 	    "print_rss_config", SYSCTL_DESCR("Prints RSS Configuration"),
   4702 	    ixgbe_sysctl_print_rss_config, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4703 		aprint_error_dev(dev, "could not create sysctl\n");
   4704 #endif
   4705 	/* for X550 series devices */
   4706 	if (hw->mac.type >= ixgbe_mac_X550)
   4707 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4708 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   4709 		    "dmac", SYSCTL_DESCR("DMA Coalesce"),
   4710 		    ixgbe_sysctl_dmac, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4711 			aprint_error_dev(dev, "could not create sysctl\n");
   4712 
   4713 	/* for X552 backplane devices */
   4714 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_KR) {
   4715 		const struct sysctlnode *eee_node;
   4716 
   4717 		if (sysctl_createv(log, 0, &rnode, &eee_node,
   4718 		    0, CTLTYPE_NODE,
   4719 		    "eee", SYSCTL_DESCR("Energy Efficient Ethernet sysctls"),
   4720 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0) {
   4721 			aprint_error_dev(dev, "could not create sysctl\n");
   4722 			return;
   4723 		}
   4724 
   4725 		if (sysctl_createv(log, 0, &eee_node, &cnode,
   4726 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   4727 		    "enable", SYSCTL_DESCR("Enable or Disable EEE"),
   4728 		    ixgbe_sysctl_eee_enable, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4729 			aprint_error_dev(dev, "could not create sysctl\n");
   4730 
   4731 		if (sysctl_createv(log, 0, &eee_node, &cnode,
   4732 		    CTLFLAG_READONLY, CTLTYPE_BOOL,
   4733 		    "negotiated", SYSCTL_DESCR("EEE negotiated on link"),
   4734 		    ixgbe_sysctl_eee_negotiated, 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 		    "tx_lpi_status", SYSCTL_DESCR("Whether or not TX link is in LPI state"),
   4740 		    ixgbe_sysctl_eee_tx_lpi_status, 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 		    "rx_lpi_status", SYSCTL_DESCR("Whether or not RX link is in LPI state"),
   4746 		    ixgbe_sysctl_eee_rx_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 		    "tx_lpi_delay", SYSCTL_DESCR("TX LPI entry delay in microseconds"),
   4752 		    ixgbe_sysctl_eee_tx_lpi_delay, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4753 			aprint_error_dev(dev, "could not create sysctl\n");
   4754 	}
   4755 
   4756 	/* for WoL-capable devices */
   4757 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
   4758 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4759 		    CTLFLAG_READWRITE, CTLTYPE_BOOL,
   4760 		    "wol_enable", SYSCTL_DESCR("Enable/Disable Wake on LAN"),
   4761 		    ixgbe_sysctl_wol_enable, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4762 			aprint_error_dev(dev, "could not create sysctl\n");
   4763 
   4764 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4765 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   4766 		    "wufc", SYSCTL_DESCR("Enable/Disable Wake Up Filters"),
   4767 		    ixgbe_sysctl_wufc, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4768 			aprint_error_dev(dev, "could not create sysctl\n");
   4769 	}
   4770 
   4771 	/* for X552/X557-AT devices */
   4772 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
   4773 		const struct sysctlnode *phy_node;
   4774 
   4775 		if (sysctl_createv(log, 0, &rnode, &phy_node,
   4776 		    0, CTLTYPE_NODE,
   4777 		    "phy", SYSCTL_DESCR("External PHY sysctls"),
   4778 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0) {
   4779 			aprint_error_dev(dev, "could not create sysctl\n");
   4780 			return;
   4781 		}
   4782 
   4783 		if (sysctl_createv(log, 0, &phy_node, &cnode,
   4784 		    CTLFLAG_READONLY, CTLTYPE_INT,
   4785 		    "temp", SYSCTL_DESCR("Current External PHY Temperature (Celsius)"),
   4786 		    ixgbe_sysctl_phy_temp, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4787 			aprint_error_dev(dev, "could not create sysctl\n");
   4788 
   4789 		if (sysctl_createv(log, 0, &phy_node, &cnode,
   4790 		    CTLFLAG_READONLY, CTLTYPE_INT,
   4791 		    "overtemp_occurred", SYSCTL_DESCR("External PHY High Temperature Event Occurred"),
   4792 		    ixgbe_sysctl_phy_overtemp_occurred, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
   4793 			aprint_error_dev(dev, "could not create sysctl\n");
   4794 	}
   4795 }
   4796 
   4797 /*
   4798  * Add sysctl variables, one per statistic, to the system.
   4799  */
   4800 static void
   4801 ixgbe_add_hw_stats(struct adapter *adapter)
   4802 {
   4803 	device_t dev = adapter->dev;
   4804 	const struct sysctlnode *rnode, *cnode;
   4805 	struct sysctllog **log = &adapter->sysctllog;
   4806 	struct tx_ring *txr = adapter->tx_rings;
   4807 	struct rx_ring *rxr = adapter->rx_rings;
   4808 	struct ixgbe_hw *hw = &adapter->hw;
   4809 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
   4810 	const char *xname = device_xname(dev);
   4811 
   4812 	/* Driver Statistics */
   4813 #if 0
   4814 	/* These counters are not updated by the software */
   4815 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_header_failed",
   4816 			CTLFLAG_RD, &adapter->mbuf_header_failed,
   4817 			"???");
   4818 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_packet_failed",
   4819 			CTLFLAG_RD, &adapter->mbuf_packet_failed,
   4820 			"???");
   4821 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "no_tx_map_avail",
   4822 			CTLFLAG_RD, &adapter->no_tx_map_avail,
   4823 			"???");
   4824 #endif
   4825 	evcnt_attach_dynamic(&adapter->handleq, EVCNT_TYPE_MISC,
   4826 	    NULL, xname, "Handled queue in softint");
   4827 	evcnt_attach_dynamic(&adapter->req, EVCNT_TYPE_MISC,
   4828 	    NULL, xname, "Requeued in softint");
   4829 	evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
   4830 	    NULL, xname, "Driver tx dma soft fail EFBIG");
   4831 	evcnt_attach_dynamic(&adapter->mbuf_defrag_failed, EVCNT_TYPE_MISC,
   4832 	    NULL, xname, "m_defrag() failed");
   4833 	evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
   4834 	    NULL, xname, "Driver tx dma hard fail EFBIG");
   4835 	evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
   4836 	    NULL, xname, "Driver tx dma hard fail EINVAL");
   4837 	evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
   4838 	    NULL, xname, "Driver tx dma hard fail other");
   4839 	evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
   4840 	    NULL, xname, "Driver tx dma soft fail EAGAIN");
   4841 	evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
   4842 	    NULL, xname, "Driver tx dma soft fail ENOMEM");
   4843 	evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
   4844 	    NULL, xname, "Watchdog timeouts");
   4845 	evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
   4846 	    NULL, xname, "TSO errors");
   4847 	evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_INTR,
   4848 	    NULL, xname, "Link MSIX IRQ Handled");
   4849 
   4850 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   4851 		snprintf(adapter->queues[i].evnamebuf,
   4852 		    sizeof(adapter->queues[i].evnamebuf), "%s q%d",
   4853 		    xname, i);
   4854 		snprintf(adapter->queues[i].namebuf,
   4855 		    sizeof(adapter->queues[i].namebuf), "q%d", i);
   4856 
   4857 		if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   4858 			aprint_error_dev(dev, "could not create sysctl root\n");
   4859 			break;
   4860 		}
   4861 
   4862 		if (sysctl_createv(log, 0, &rnode, &rnode,
   4863 		    0, CTLTYPE_NODE,
   4864 		    adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
   4865 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   4866 			break;
   4867 
   4868 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4869 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   4870 		    "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
   4871 		    ixgbe_sysctl_interrupt_rate_handler, 0,
   4872 		    (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
   4873 			break;
   4874 
   4875 #if 0 /* XXX msaitoh */
   4876 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4877 		    CTLFLAG_READONLY, CTLTYPE_QUAD,
   4878 		    "irqs", SYSCTL_DESCR("irqs on this queue"),
   4879 			NULL, 0, &(adapter->queues[i].irqs),
   4880 		    0, CTL_CREATE, CTL_EOL) != 0)
   4881 			break;
   4882 #endif
   4883 
   4884 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4885 		    CTLFLAG_READONLY, CTLTYPE_INT,
   4886 		    "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
   4887 		    ixgbe_sysctl_tdh_handler, 0, (void *)txr,
   4888 		    0, CTL_CREATE, CTL_EOL) != 0)
   4889 			break;
   4890 
   4891 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4892 		    CTLFLAG_READONLY, CTLTYPE_INT,
   4893 		    "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
   4894 		    ixgbe_sysctl_tdt_handler, 0, (void *)txr,
   4895 		    0, CTL_CREATE, CTL_EOL) != 0)
   4896 			break;
   4897 
   4898 		evcnt_attach_dynamic(&adapter->queues[i].irqs, EVCNT_TYPE_INTR,
   4899 		    NULL, adapter->queues[i].evnamebuf, "IRQs on queue");
   4900 		evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
   4901 		    NULL, adapter->queues[i].evnamebuf, "TSO");
   4902 		evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
   4903 		    NULL, adapter->queues[i].evnamebuf,
   4904 		    "Queue No Descriptor Available");
   4905 		evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
   4906 		    NULL, adapter->queues[i].evnamebuf,
   4907 		    "Queue Packets Transmitted");
   4908 #ifndef IXGBE_LEGACY_TX
   4909 		evcnt_attach_dynamic(&txr->pcq_drops, EVCNT_TYPE_MISC,
   4910 		    NULL, adapter->queues[i].evnamebuf,
   4911 		    "Packets dropped in pcq");
   4912 #endif
   4913 
   4914 #ifdef LRO
   4915 		struct lro_ctrl *lro = &rxr->lro;
   4916 #endif /* LRO */
   4917 
   4918 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4919 		    CTLFLAG_READONLY,
   4920 		    CTLTYPE_INT,
   4921 		    "rxd_head", SYSCTL_DESCR("Receive Descriptor Head"),
   4922 		    ixgbe_sysctl_rdh_handler, 0, (void *)rxr, 0,
   4923 		    CTL_CREATE, CTL_EOL) != 0)
   4924 			break;
   4925 
   4926 		if (sysctl_createv(log, 0, &rnode, &cnode,
   4927 		    CTLFLAG_READONLY,
   4928 		    CTLTYPE_INT,
   4929 		    "rxd_tail", SYSCTL_DESCR("Receive Descriptor Tail"),
   4930 		    ixgbe_sysctl_rdt_handler, 0, (void *)rxr, 0,
   4931 		    CTL_CREATE, CTL_EOL) != 0)
   4932 			break;
   4933 
   4934 		if (i < __arraycount(stats->mpc)) {
   4935 			evcnt_attach_dynamic(&stats->mpc[i],
   4936 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4937 			    "Missed Packet Count");
   4938 			if (hw->mac.type == ixgbe_mac_82598EB)
   4939 				evcnt_attach_dynamic(&stats->rnbc[i],
   4940 				    EVCNT_TYPE_MISC, NULL,
   4941 				    adapter->queues[i].evnamebuf,
   4942 				    "Receive No Buffers");
   4943 		}
   4944 		if (i < __arraycount(stats->pxontxc)) {
   4945 			evcnt_attach_dynamic(&stats->pxontxc[i],
   4946 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4947 			    "pxontxc");
   4948 			evcnt_attach_dynamic(&stats->pxonrxc[i],
   4949 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4950 			    "pxonrxc");
   4951 			evcnt_attach_dynamic(&stats->pxofftxc[i],
   4952 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4953 			    "pxofftxc");
   4954 			evcnt_attach_dynamic(&stats->pxoffrxc[i],
   4955 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4956 			    "pxoffrxc");
   4957 			evcnt_attach_dynamic(&stats->pxon2offc[i],
   4958 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4959 			    "pxon2offc");
   4960 		}
   4961 		if (i < __arraycount(stats->qprc)) {
   4962 			evcnt_attach_dynamic(&stats->qprc[i],
   4963 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4964 			    "qprc");
   4965 			evcnt_attach_dynamic(&stats->qptc[i],
   4966 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4967 			    "qptc");
   4968 			evcnt_attach_dynamic(&stats->qbrc[i],
   4969 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4970 			    "qbrc");
   4971 			evcnt_attach_dynamic(&stats->qbtc[i],
   4972 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4973 			    "qbtc");
   4974 			evcnt_attach_dynamic(&stats->qprdc[i],
   4975 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   4976 			    "qprdc");
   4977 		}
   4978 
   4979 		evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
   4980 		    NULL, adapter->queues[i].evnamebuf, "Queue Packets Received");
   4981 		evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
   4982 		    NULL, adapter->queues[i].evnamebuf, "Queue Bytes Received");
   4983 		evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
   4984 		    NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
   4985 		evcnt_attach_dynamic(&rxr->no_jmbuf, EVCNT_TYPE_MISC,
   4986 		    NULL, adapter->queues[i].evnamebuf, "Rx no jumbo mbuf");
   4987 		evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
   4988 		    NULL, adapter->queues[i].evnamebuf, "Rx discarded");
   4989 #ifdef LRO
   4990 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
   4991 				CTLFLAG_RD, &lro->lro_queued, 0,
   4992 				"LRO Queued");
   4993 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
   4994 				CTLFLAG_RD, &lro->lro_flushed, 0,
   4995 				"LRO Flushed");
   4996 #endif /* LRO */
   4997 	}
   4998 
   4999 	/* MAC stats get the own sub node */
   5000 
   5001 	snprintf(stats->namebuf,
   5002 	    sizeof(stats->namebuf), "%s MAC Statistics", xname);
   5003 
   5004 	evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
   5005 	    stats->namebuf, "rx csum offload - IP");
   5006 	evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
   5007 	    stats->namebuf, "rx csum offload - L4");
   5008 	evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
   5009 	    stats->namebuf, "rx csum offload - IP bad");
   5010 	evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
   5011 	    stats->namebuf, "rx csum offload - L4 bad");
   5012 	evcnt_attach_dynamic(&stats->intzero, EVCNT_TYPE_MISC, NULL,
   5013 	    stats->namebuf, "Interrupt conditions zero");
   5014 	evcnt_attach_dynamic(&stats->legint, EVCNT_TYPE_MISC, NULL,
   5015 	    stats->namebuf, "Legacy interrupts");
   5016 	evcnt_attach_dynamic(&stats->crcerrs, EVCNT_TYPE_MISC, NULL,
   5017 	    stats->namebuf, "CRC Errors");
   5018 	evcnt_attach_dynamic(&stats->illerrc, EVCNT_TYPE_MISC, NULL,
   5019 	    stats->namebuf, "Illegal Byte Errors");
   5020 	evcnt_attach_dynamic(&stats->errbc, EVCNT_TYPE_MISC, NULL,
   5021 	    stats->namebuf, "Byte Errors");
   5022 	evcnt_attach_dynamic(&stats->mspdc, EVCNT_TYPE_MISC, NULL,
   5023 	    stats->namebuf, "MAC Short Packets Discarded");
   5024 	if (hw->mac.type >= ixgbe_mac_X550)
   5025 		evcnt_attach_dynamic(&stats->mbsdc, EVCNT_TYPE_MISC, NULL,
   5026 		    stats->namebuf, "Bad SFD");
   5027 	evcnt_attach_dynamic(&stats->mpctotal, EVCNT_TYPE_MISC, NULL,
   5028 	    stats->namebuf, "Total Packets Missed");
   5029 	evcnt_attach_dynamic(&stats->mlfc, EVCNT_TYPE_MISC, NULL,
   5030 	    stats->namebuf, "MAC Local Faults");
   5031 	evcnt_attach_dynamic(&stats->mrfc, EVCNT_TYPE_MISC, NULL,
   5032 	    stats->namebuf, "MAC Remote Faults");
   5033 	evcnt_attach_dynamic(&stats->rlec, EVCNT_TYPE_MISC, NULL,
   5034 	    stats->namebuf, "Receive Length Errors");
   5035 	evcnt_attach_dynamic(&stats->lxontxc, EVCNT_TYPE_MISC, NULL,
   5036 	    stats->namebuf, "Link XON Transmitted");
   5037 	evcnt_attach_dynamic(&stats->lxonrxc, EVCNT_TYPE_MISC, NULL,
   5038 	    stats->namebuf, "Link XON Received");
   5039 	evcnt_attach_dynamic(&stats->lxofftxc, EVCNT_TYPE_MISC, NULL,
   5040 	    stats->namebuf, "Link XOFF Transmitted");
   5041 	evcnt_attach_dynamic(&stats->lxoffrxc, EVCNT_TYPE_MISC, NULL,
   5042 	    stats->namebuf, "Link XOFF Received");
   5043 
   5044 	/* Packet Reception Stats */
   5045 	evcnt_attach_dynamic(&stats->tor, EVCNT_TYPE_MISC, NULL,
   5046 	    stats->namebuf, "Total Octets Received");
   5047 	evcnt_attach_dynamic(&stats->gorc, EVCNT_TYPE_MISC, NULL,
   5048 	    stats->namebuf, "Good Octets Received");
   5049 	evcnt_attach_dynamic(&stats->tpr, EVCNT_TYPE_MISC, NULL,
   5050 	    stats->namebuf, "Total Packets Received");
   5051 	evcnt_attach_dynamic(&stats->gprc, EVCNT_TYPE_MISC, NULL,
   5052 	    stats->namebuf, "Good Packets Received");
   5053 	evcnt_attach_dynamic(&stats->mprc, EVCNT_TYPE_MISC, NULL,
   5054 	    stats->namebuf, "Multicast Packets Received");
   5055 	evcnt_attach_dynamic(&stats->bprc, EVCNT_TYPE_MISC, NULL,
   5056 	    stats->namebuf, "Broadcast Packets Received");
   5057 	evcnt_attach_dynamic(&stats->prc64, EVCNT_TYPE_MISC, NULL,
   5058 	    stats->namebuf, "64 byte frames received ");
   5059 	evcnt_attach_dynamic(&stats->prc127, EVCNT_TYPE_MISC, NULL,
   5060 	    stats->namebuf, "65-127 byte frames received");
   5061 	evcnt_attach_dynamic(&stats->prc255, EVCNT_TYPE_MISC, NULL,
   5062 	    stats->namebuf, "128-255 byte frames received");
   5063 	evcnt_attach_dynamic(&stats->prc511, EVCNT_TYPE_MISC, NULL,
   5064 	    stats->namebuf, "256-511 byte frames received");
   5065 	evcnt_attach_dynamic(&stats->prc1023, EVCNT_TYPE_MISC, NULL,
   5066 	    stats->namebuf, "512-1023 byte frames received");
   5067 	evcnt_attach_dynamic(&stats->prc1522, EVCNT_TYPE_MISC, NULL,
   5068 	    stats->namebuf, "1023-1522 byte frames received");
   5069 	evcnt_attach_dynamic(&stats->ruc, EVCNT_TYPE_MISC, NULL,
   5070 	    stats->namebuf, "Receive Undersized");
   5071 	evcnt_attach_dynamic(&stats->rfc, EVCNT_TYPE_MISC, NULL,
   5072 	    stats->namebuf, "Fragmented Packets Received ");
   5073 	evcnt_attach_dynamic(&stats->roc, EVCNT_TYPE_MISC, NULL,
   5074 	    stats->namebuf, "Oversized Packets Received");
   5075 	evcnt_attach_dynamic(&stats->rjc, EVCNT_TYPE_MISC, NULL,
   5076 	    stats->namebuf, "Received Jabber");
   5077 	evcnt_attach_dynamic(&stats->mngprc, EVCNT_TYPE_MISC, NULL,
   5078 	    stats->namebuf, "Management Packets Received");
   5079 	evcnt_attach_dynamic(&stats->mngpdc, EVCNT_TYPE_MISC, NULL,
   5080 	    stats->namebuf, "Management Packets Dropped");
   5081 	evcnt_attach_dynamic(&stats->xec, EVCNT_TYPE_MISC, NULL,
   5082 	    stats->namebuf, "Checksum Errors");
   5083 
   5084 	/* Packet Transmission Stats */
   5085 	evcnt_attach_dynamic(&stats->gotc, EVCNT_TYPE_MISC, NULL,
   5086 	    stats->namebuf, "Good Octets Transmitted");
   5087 	evcnt_attach_dynamic(&stats->tpt, EVCNT_TYPE_MISC, NULL,
   5088 	    stats->namebuf, "Total Packets Transmitted");
   5089 	evcnt_attach_dynamic(&stats->gptc, EVCNT_TYPE_MISC, NULL,
   5090 	    stats->namebuf, "Good Packets Transmitted");
   5091 	evcnt_attach_dynamic(&stats->bptc, EVCNT_TYPE_MISC, NULL,
   5092 	    stats->namebuf, "Broadcast Packets Transmitted");
   5093 	evcnt_attach_dynamic(&stats->mptc, EVCNT_TYPE_MISC, NULL,
   5094 	    stats->namebuf, "Multicast Packets Transmitted");
   5095 	evcnt_attach_dynamic(&stats->mngptc, EVCNT_TYPE_MISC, NULL,
   5096 	    stats->namebuf, "Management Packets Transmitted");
   5097 	evcnt_attach_dynamic(&stats->ptc64, EVCNT_TYPE_MISC, NULL,
   5098 	    stats->namebuf, "64 byte frames transmitted ");
   5099 	evcnt_attach_dynamic(&stats->ptc127, EVCNT_TYPE_MISC, NULL,
   5100 	    stats->namebuf, "65-127 byte frames transmitted");
   5101 	evcnt_attach_dynamic(&stats->ptc255, EVCNT_TYPE_MISC, NULL,
   5102 	    stats->namebuf, "128-255 byte frames transmitted");
   5103 	evcnt_attach_dynamic(&stats->ptc511, EVCNT_TYPE_MISC, NULL,
   5104 	    stats->namebuf, "256-511 byte frames transmitted");
   5105 	evcnt_attach_dynamic(&stats->ptc1023, EVCNT_TYPE_MISC, NULL,
   5106 	    stats->namebuf, "512-1023 byte frames transmitted");
   5107 	evcnt_attach_dynamic(&stats->ptc1522, EVCNT_TYPE_MISC, NULL,
   5108 	    stats->namebuf, "1024-1522 byte frames transmitted");
   5109 }
   5110 
   5111 static void
   5112 ixgbe_clear_evcnt(struct adapter *adapter)
   5113 {
   5114 	struct tx_ring *txr = adapter->tx_rings;
   5115 	struct rx_ring *rxr = adapter->rx_rings;
   5116 	struct ixgbe_hw *hw = &adapter->hw;
   5117 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
   5118 
   5119 	adapter->handleq.ev_count = 0;
   5120 	adapter->req.ev_count = 0;
   5121 	adapter->efbig_tx_dma_setup.ev_count = 0;
   5122 	adapter->mbuf_defrag_failed.ev_count = 0;
   5123 	adapter->efbig2_tx_dma_setup.ev_count = 0;
   5124 	adapter->einval_tx_dma_setup.ev_count = 0;
   5125 	adapter->other_tx_dma_setup.ev_count = 0;
   5126 	adapter->eagain_tx_dma_setup.ev_count = 0;
   5127 	adapter->enomem_tx_dma_setup.ev_count = 0;
   5128 	adapter->watchdog_events.ev_count = 0;
   5129 	adapter->tso_err.ev_count = 0;
   5130 	adapter->link_irq.ev_count = 0;
   5131 
   5132 	txr = adapter->tx_rings;
   5133 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   5134 		adapter->queues[i].irqs.ev_count = 0;
   5135 		txr->no_desc_avail.ev_count = 0;
   5136 		txr->total_packets.ev_count = 0;
   5137 		txr->tso_tx.ev_count = 0;
   5138 #ifndef IXGBE_LEGACY_TX
   5139 		txr->pcq_drops.ev_count = 0;
   5140 #endif
   5141 
   5142 		if (i < __arraycount(stats->mpc)) {
   5143 			stats->mpc[i].ev_count = 0;
   5144 			if (hw->mac.type == ixgbe_mac_82598EB)
   5145 				stats->rnbc[i].ev_count = 0;
   5146 		}
   5147 		if (i < __arraycount(stats->pxontxc)) {
   5148 			stats->pxontxc[i].ev_count = 0;
   5149 			stats->pxonrxc[i].ev_count = 0;
   5150 			stats->pxofftxc[i].ev_count = 0;
   5151 			stats->pxoffrxc[i].ev_count = 0;
   5152 			stats->pxon2offc[i].ev_count = 0;
   5153 		}
   5154 		if (i < __arraycount(stats->qprc)) {
   5155 			stats->qprc[i].ev_count = 0;
   5156 			stats->qptc[i].ev_count = 0;
   5157 			stats->qbrc[i].ev_count = 0;
   5158 			stats->qbtc[i].ev_count = 0;
   5159 			stats->qprdc[i].ev_count = 0;
   5160 		}
   5161 
   5162 		rxr->rx_packets.ev_count = 0;
   5163 		rxr->rx_bytes.ev_count = 0;
   5164 		rxr->rx_copies.ev_count = 0;
   5165 		rxr->no_jmbuf.ev_count = 0;
   5166 		rxr->rx_discarded.ev_count = 0;
   5167 	}
   5168 	stats->ipcs.ev_count = 0;
   5169 	stats->l4cs.ev_count = 0;
   5170 	stats->ipcs_bad.ev_count = 0;
   5171 	stats->l4cs_bad.ev_count = 0;
   5172 	stats->intzero.ev_count = 0;
   5173 	stats->legint.ev_count = 0;
   5174 	stats->crcerrs.ev_count = 0;
   5175 	stats->illerrc.ev_count = 0;
   5176 	stats->errbc.ev_count = 0;
   5177 	stats->mspdc.ev_count = 0;
   5178 	stats->mbsdc.ev_count = 0;
   5179 	stats->mpctotal.ev_count = 0;
   5180 	stats->mlfc.ev_count = 0;
   5181 	stats->mrfc.ev_count = 0;
   5182 	stats->rlec.ev_count = 0;
   5183 	stats->lxontxc.ev_count = 0;
   5184 	stats->lxonrxc.ev_count = 0;
   5185 	stats->lxofftxc.ev_count = 0;
   5186 	stats->lxoffrxc.ev_count = 0;
   5187 
   5188 	/* Packet Reception Stats */
   5189 	stats->tor.ev_count = 0;
   5190 	stats->gorc.ev_count = 0;
   5191 	stats->tpr.ev_count = 0;
   5192 	stats->gprc.ev_count = 0;
   5193 	stats->mprc.ev_count = 0;
   5194 	stats->bprc.ev_count = 0;
   5195 	stats->prc64.ev_count = 0;
   5196 	stats->prc127.ev_count = 0;
   5197 	stats->prc255.ev_count = 0;
   5198 	stats->prc511.ev_count = 0;
   5199 	stats->prc1023.ev_count = 0;
   5200 	stats->prc1522.ev_count = 0;
   5201 	stats->ruc.ev_count = 0;
   5202 	stats->rfc.ev_count = 0;
   5203 	stats->roc.ev_count = 0;
   5204 	stats->rjc.ev_count = 0;
   5205 	stats->mngprc.ev_count = 0;
   5206 	stats->mngpdc.ev_count = 0;
   5207 	stats->xec.ev_count = 0;
   5208 
   5209 	/* Packet Transmission Stats */
   5210 	stats->gotc.ev_count = 0;
   5211 	stats->tpt.ev_count = 0;
   5212 	stats->gptc.ev_count = 0;
   5213 	stats->bptc.ev_count = 0;
   5214 	stats->mptc.ev_count = 0;
   5215 	stats->mngptc.ev_count = 0;
   5216 	stats->ptc64.ev_count = 0;
   5217 	stats->ptc127.ev_count = 0;
   5218 	stats->ptc255.ev_count = 0;
   5219 	stats->ptc511.ev_count = 0;
   5220 	stats->ptc1023.ev_count = 0;
   5221 	stats->ptc1522.ev_count = 0;
   5222 }
   5223 
   5224 static void
   5225 ixgbe_set_sysctl_value(struct adapter *adapter, const char *name,
   5226     const char *description, int *limit, int value)
   5227 {
   5228 	device_t dev =  adapter->dev;
   5229 	struct sysctllog **log;
   5230 	const struct sysctlnode *rnode, *cnode;
   5231 
   5232 	log = &adapter->sysctllog;
   5233 	if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   5234 		aprint_error_dev(dev, "could not create sysctl root\n");
   5235 		return;
   5236 	}
   5237 	if (sysctl_createv(log, 0, &rnode, &cnode,
   5238 	    CTLFLAG_READWRITE, CTLTYPE_INT,
   5239 	    name, SYSCTL_DESCR(description),
   5240 		NULL, 0, limit, 0, CTL_CREATE, CTL_EOL) != 0)
   5241 		aprint_error_dev(dev, "could not create sysctl\n");
   5242 	*limit = value;
   5243 }
   5244 
   5245 /*
   5246 ** Set flow control using sysctl:
   5247 ** Flow control values:
   5248 ** 	0 - off
   5249 **	1 - rx pause
   5250 **	2 - tx pause
   5251 **	3 - full
   5252 */
   5253 static int
   5254 ixgbe_sysctl_flowcntl(SYSCTLFN_ARGS)
   5255 {
   5256 	int error, fc;
   5257 	struct sysctlnode node = *rnode;
   5258 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5259 
   5260 	fc = adapter->fc;
   5261 	node.sysctl_data = &fc;
   5262 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5263 	if (error != 0 || newp == NULL)
   5264 		return error;
   5265 
   5266 	/* Don't bother if it's not changed */
   5267 	if (adapter->fc == fc)
   5268 		return (0);
   5269 
   5270 	return ixgbe_set_flowcntl(adapter, fc);
   5271 }
   5272 
   5273 
   5274 static int
   5275 ixgbe_set_flowcntl(struct adapter *adapter, int fc)
   5276 {
   5277 
   5278 	switch (fc) {
   5279 		case ixgbe_fc_rx_pause:
   5280 		case ixgbe_fc_tx_pause:
   5281 		case ixgbe_fc_full:
   5282 			adapter->hw.fc.requested_mode = adapter->fc;
   5283 			if (adapter->num_queues > 1)
   5284 				ixgbe_disable_rx_drop(adapter);
   5285 			break;
   5286 		case ixgbe_fc_none:
   5287 			adapter->hw.fc.requested_mode = ixgbe_fc_none;
   5288 			if (adapter->num_queues > 1)
   5289 				ixgbe_enable_rx_drop(adapter);
   5290 			break;
   5291 		default:
   5292 			return (EINVAL);
   5293 	}
   5294 	adapter->fc = fc;
   5295 #if 0 /* XXX NetBSD */
   5296 	/* Don't autoneg if forcing a value */
   5297 	adapter->hw.fc.disable_fc_autoneg = TRUE;
   5298 #endif
   5299 	ixgbe_fc_enable(&adapter->hw);
   5300 	return (0);
   5301 }
   5302 
   5303 /*
   5304 ** Control advertised link speed:
   5305 **	Flags:
   5306 **	0x0 - Default (all capable link speed)
   5307 **	0x1 - advertise 100 Mb
   5308 **	0x2 - advertise 1G
   5309 **	0x4 - advertise 10G
   5310 */
   5311 static int
   5312 ixgbe_sysctl_advertise(SYSCTLFN_ARGS)
   5313 {
   5314 	struct sysctlnode	node = *rnode;
   5315 	int			error = 0, advertise;
   5316 	struct adapter		*adapter = (struct adapter *)node.sysctl_data;
   5317 
   5318 	advertise = adapter->advertise;
   5319 	node.sysctl_data = &advertise;
   5320 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5321 	if (error != 0 || newp == NULL)
   5322 		return error;
   5323 
   5324 	return ixgbe_set_advertise(adapter, advertise);
   5325 }
   5326 
   5327 static int
   5328 ixgbe_set_advertise(struct adapter *adapter, int advertise)
   5329 {
   5330 	device_t		dev;
   5331 	struct ixgbe_hw		*hw;
   5332 	ixgbe_link_speed	speed;
   5333 
   5334 	/* Checks to validate new value */
   5335 	if (adapter->advertise == advertise) /* no change */
   5336 		return (0);
   5337 
   5338 	hw = &adapter->hw;
   5339 	dev = adapter->dev;
   5340 
   5341 	/* No speed changes for backplane media */
   5342 	if (hw->phy.media_type == ixgbe_media_type_backplane)
   5343 		return (ENODEV);
   5344 
   5345 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
   5346 	    (hw->phy.multispeed_fiber))) {
   5347 		device_printf(dev,
   5348 		    "Advertised speed can only be set on copper or "
   5349 		    "multispeed fiber media types.\n");
   5350 		return (EINVAL);
   5351 	}
   5352 
   5353 	if (advertise < 0x0 || advertise > 0x7) {
   5354 		device_printf(dev,
   5355 		    "Invalid advertised speed; valid modes are 0x0 through 0x7\n");
   5356 		return (EINVAL);
   5357 	}
   5358 
   5359 	/* Set new value and report new advertised mode */
   5360 	speed = 0;
   5361 	if ((hw->mac.type != ixgbe_mac_X540)
   5362 	    && (hw->mac.type != ixgbe_mac_X550)) {
   5363 		if (advertise & 0x1) {
   5364 			device_printf(dev,
   5365 			    "Set Advertise: 100Mb on X540/X550 only\n");
   5366 			return (EINVAL);
   5367 		}
   5368 	} else if ((advertise & 0x1) || (advertise == 0))
   5369 		speed |= IXGBE_LINK_SPEED_100_FULL;
   5370 	if ((advertise & 0x2) || (advertise == 0))
   5371 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
   5372 	if ((advertise & 0x4) || (advertise == 0))
   5373 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
   5374 	adapter->advertise = advertise;
   5375 
   5376 	hw->mac.autotry_restart = TRUE;
   5377 	hw->mac.ops.setup_link(hw, speed, TRUE);
   5378 
   5379 	return 0;
   5380 }
   5381 
   5382 /*
   5383  * The following two sysctls are for X552/X557-AT devices;
   5384  * they deal with the external PHY used in them.
   5385  */
   5386 static int
   5387 ixgbe_sysctl_phy_temp(SYSCTLFN_ARGS)
   5388 {
   5389 	struct sysctlnode node = *rnode;
   5390 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   5391 	struct ixgbe_hw *hw = &adapter->hw;
   5392 	int val;
   5393 	u16 reg;
   5394 	int		error;
   5395 
   5396 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
   5397 		device_printf(adapter->dev,
   5398 		    "Device has no supported external thermal sensor.\n");
   5399 		return (ENODEV);
   5400 	}
   5401 
   5402 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_CURRENT_TEMP,
   5403 				      IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
   5404 				      &reg)) {
   5405 		device_printf(adapter->dev,
   5406 		    "Error reading from PHY's current temperature register\n");
   5407 		return (EAGAIN);
   5408 	}
   5409 
   5410 	node.sysctl_data = &val;
   5411 
   5412 	/* Shift temp for output */
   5413 	val = reg >> 8;
   5414 
   5415 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5416 	if ((error) || (newp == NULL))
   5417 		return (error);
   5418 
   5419 	return (0);
   5420 }
   5421 
   5422 /*
   5423  * Reports whether the current PHY temperature is over
   5424  * the overtemp threshold.
   5425  *  - This is reported directly from the PHY
   5426  */
   5427 static int
   5428 ixgbe_sysctl_phy_overtemp_occurred(SYSCTLFN_ARGS)
   5429 {
   5430 	struct sysctlnode node = *rnode;
   5431 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   5432 	struct ixgbe_hw *hw = &adapter->hw;
   5433 	int val, error;
   5434 	u16 reg;
   5435 
   5436 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
   5437 		device_printf(adapter->dev,
   5438 		    "Device has no supported external thermal sensor.\n");
   5439 		return (ENODEV);
   5440 	}
   5441 
   5442 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_OVERTEMP_STATUS,
   5443 				      IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
   5444 				      &reg)) {
   5445 		device_printf(adapter->dev,
   5446 		    "Error reading from PHY's temperature status register\n");
   5447 		return (EAGAIN);
   5448 	}
   5449 
   5450 	node.sysctl_data = &val;
   5451 
   5452 	/* Get occurrence bit */
   5453 	val = !!(reg & 0x4000);
   5454 
   5455 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5456 	if ((error) || (newp == NULL))
   5457 		return (error);
   5458 
   5459 	return (0);
   5460 }
   5461 
   5462 /*
   5463 ** Thermal Shutdown Trigger (internal MAC)
   5464 **   - Set this to 1 to cause an overtemp event to occur
   5465 */
   5466 static int
   5467 ixgbe_sysctl_thermal_test(SYSCTLFN_ARGS)
   5468 {
   5469 	struct sysctlnode node = *rnode;
   5470 	struct adapter	*adapter = (struct adapter *)node.sysctl_data;
   5471 	int		error, fire = 0;
   5472 	struct ixgbe_hw *hw;
   5473 
   5474 	hw = &adapter->hw;
   5475 
   5476 	node.sysctl_data = &fire;
   5477 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5478 	if ((error) || (newp == NULL))
   5479 		return (error);
   5480 
   5481 	if (fire) {
   5482 		u32 reg = IXGBE_READ_REG(hw, IXGBE_EICS);
   5483 		reg |= IXGBE_EICR_TS;
   5484 		IXGBE_WRITE_REG(hw, IXGBE_EICS, reg);
   5485 	}
   5486 
   5487 	return (0);
   5488 }
   5489 
   5490 /*
   5491 ** Manage DMA Coalescing.
   5492 ** Control values:
   5493 ** 	0/1 - off / on (use default value of 1000)
   5494 **
   5495 **	Legal timer values are:
   5496 **	50,100,250,500,1000,2000,5000,10000
   5497 **
   5498 **	Turning off interrupt moderation will also turn this off.
   5499 */
   5500 static int
   5501 ixgbe_sysctl_dmac(SYSCTLFN_ARGS)
   5502 {
   5503 	struct sysctlnode node = *rnode;
   5504 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5505 	struct ifnet *ifp = adapter->ifp;
   5506 	int		error;
   5507 	u16		oldval;
   5508 	int		newval;
   5509 
   5510 	oldval = adapter->dmac;
   5511 	newval = oldval;
   5512 	node.sysctl_data = &newval;
   5513 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5514 	if ((error) || (newp == NULL))
   5515 		return (error);
   5516 
   5517 	switch (newval) {
   5518 	case 0:
   5519 		/* Disabled */
   5520 		adapter->dmac = 0;
   5521 		break;
   5522 	case 1:
   5523 		/* Enable and use default */
   5524 		adapter->dmac = 1000;
   5525 		break;
   5526 	case 50:
   5527 	case 100:
   5528 	case 250:
   5529 	case 500:
   5530 	case 1000:
   5531 	case 2000:
   5532 	case 5000:
   5533 	case 10000:
   5534 		/* Legal values - allow */
   5535 		adapter->dmac = newval;
   5536 		break;
   5537 	default:
   5538 		/* Do nothing, illegal value */
   5539 		return (EINVAL);
   5540 	}
   5541 
   5542 	/* Re-initialize hardware if it's already running */
   5543 	if (ifp->if_flags & IFF_RUNNING)
   5544 		ixgbe_init(ifp);
   5545 
   5546 	return (0);
   5547 }
   5548 
   5549 #ifdef IXGBE_DEBUG
   5550 /**
   5551  * Sysctl to test power states
   5552  * Values:
   5553  *	0      - set device to D0
   5554  *	3      - set device to D3
   5555  *	(none) - get current device power state
   5556  */
   5557 static int
   5558 ixgbe_sysctl_power_state(SYSCTLFN_ARGS)
   5559 {
   5560 	struct sysctlnode node = *rnode;
   5561 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5562 	device_t dev =  adapter->dev;
   5563 	int curr_ps, new_ps, error = 0;
   5564 
   5565 #if notyet
   5566 	curr_ps = new_ps = pci_get_powerstate(dev);
   5567 
   5568 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5569 	if ((error) || (req->newp == NULL))
   5570 		return (error);
   5571 
   5572 	if (new_ps == curr_ps)
   5573 		return (0);
   5574 
   5575 	if (new_ps == 3 && curr_ps == 0)
   5576 		error = DEVICE_SUSPEND(dev);
   5577 	else if (new_ps == 0 && curr_ps == 3)
   5578 		error = DEVICE_RESUME(dev);
   5579 	else
   5580 		return (EINVAL);
   5581 
   5582 	device_printf(dev, "New state: %d\n", pci_get_powerstate(dev));
   5583 
   5584 	return (error);
   5585 #else
   5586 	return 0;
   5587 #endif
   5588 }
   5589 #endif
   5590 /*
   5591  * Sysctl to enable/disable the WoL capability, if supported by the adapter.
   5592  * Values:
   5593  *	0 - disabled
   5594  *	1 - enabled
   5595  */
   5596 static int
   5597 ixgbe_sysctl_wol_enable(SYSCTLFN_ARGS)
   5598 {
   5599 	struct sysctlnode node = *rnode;
   5600 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5601 	struct ixgbe_hw *hw = &adapter->hw;
   5602 	bool new_wol_enabled;
   5603 	int error = 0;
   5604 
   5605 	new_wol_enabled = hw->wol_enabled;
   5606 	node.sysctl_data = &new_wol_enabled;
   5607 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5608 	if ((error) || (newp == NULL))
   5609 		return (error);
   5610 	if (new_wol_enabled == hw->wol_enabled)
   5611 		return (0);
   5612 
   5613 	if (new_wol_enabled && !adapter->wol_support)
   5614 		return (ENODEV);
   5615 	else
   5616 		hw->wol_enabled = new_wol_enabled;
   5617 
   5618 	return (0);
   5619 }
   5620 
   5621 /*
   5622  * Sysctl to enable/disable the Energy Efficient Ethernet capability,
   5623  * if supported by the adapter.
   5624  * Values:
   5625  *	0 - disabled
   5626  *	1 - enabled
   5627  */
   5628 static int
   5629 ixgbe_sysctl_eee_enable(SYSCTLFN_ARGS)
   5630 {
   5631 	struct sysctlnode node = *rnode;
   5632 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5633 	struct ixgbe_hw *hw = &adapter->hw;
   5634 	struct ifnet *ifp = adapter->ifp;
   5635 	int new_eee_enabled, error = 0;
   5636 
   5637 	new_eee_enabled = adapter->eee_enabled;
   5638 	node.sysctl_data = &new_eee_enabled;
   5639 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5640 	if ((error) || (newp == NULL))
   5641 		return (error);
   5642 	new_eee_enabled = !!(new_eee_enabled);
   5643 	if (new_eee_enabled == adapter->eee_enabled)
   5644 		return (0);
   5645 
   5646 	if (new_eee_enabled > 0 && !hw->mac.ops.setup_eee)
   5647 		return (ENODEV);
   5648 	else
   5649 		adapter->eee_enabled = new_eee_enabled;
   5650 
   5651 	/* Re-initialize hardware if it's already running */
   5652 	if (ifp->if_flags & IFF_RUNNING)
   5653 		ixgbe_init(ifp);
   5654 
   5655 	return (0);
   5656 }
   5657 
   5658 /*
   5659  * Read-only sysctl indicating whether EEE support was negotiated
   5660  * on the link.
   5661  */
   5662 static int
   5663 ixgbe_sysctl_eee_negotiated(SYSCTLFN_ARGS)
   5664 {
   5665 	struct sysctlnode node = *rnode;
   5666 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5667 	struct ixgbe_hw *hw = &adapter->hw;
   5668 	bool status;
   5669 
   5670 	status = !!(IXGBE_READ_REG(hw, IXGBE_EEE_STAT) & IXGBE_EEE_STAT_NEG);
   5671 
   5672 	node.sysctl_data = &status;
   5673 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   5674 }
   5675 
   5676 /*
   5677  * Read-only sysctl indicating whether RX Link is in LPI state.
   5678  */
   5679 static int
   5680 ixgbe_sysctl_eee_rx_lpi_status(SYSCTLFN_ARGS)
   5681 {
   5682 	struct sysctlnode node = *rnode;
   5683 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5684 	struct ixgbe_hw *hw = &adapter->hw;
   5685 	bool status;
   5686 
   5687 	status = !!(IXGBE_READ_REG(hw, IXGBE_EEE_STAT) &
   5688 	    IXGBE_EEE_RX_LPI_STATUS);
   5689 
   5690 	node.sysctl_data = &status;
   5691 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   5692 }
   5693 
   5694 /*
   5695  * Read-only sysctl indicating whether TX Link is in LPI state.
   5696  */
   5697 static int
   5698 ixgbe_sysctl_eee_tx_lpi_status(SYSCTLFN_ARGS)
   5699 {
   5700 	struct sysctlnode node = *rnode;
   5701 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5702 	struct ixgbe_hw *hw = &adapter->hw;
   5703 	bool status;
   5704 
   5705 	status = !!(IXGBE_READ_REG(hw, IXGBE_EEE_STAT) &
   5706 	    IXGBE_EEE_TX_LPI_STATUS);
   5707 
   5708 	node.sysctl_data = &status;
   5709 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   5710 }
   5711 
   5712 /*
   5713  * Read-only sysctl indicating TX Link LPI delay
   5714  */
   5715 static int
   5716 ixgbe_sysctl_eee_tx_lpi_delay(SYSCTLFN_ARGS)
   5717 {
   5718 	struct sysctlnode node = *rnode;
   5719 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5720 	struct ixgbe_hw *hw = &adapter->hw;
   5721 	u32 reg;
   5722 
   5723 	reg = IXGBE_READ_REG(hw, IXGBE_EEE_SU);
   5724 
   5725 	reg >>= 26;
   5726 	node.sysctl_data = &reg;
   5727 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   5728 }
   5729 
   5730 /*
   5731  * Sysctl to enable/disable the types of packets that the
   5732  * adapter will wake up on upon receipt.
   5733  * WUFC - Wake Up Filter Control
   5734  * Flags:
   5735  *	0x1  - Link Status Change
   5736  *	0x2  - Magic Packet
   5737  *	0x4  - Direct Exact
   5738  *	0x8  - Directed Multicast
   5739  *	0x10 - Broadcast
   5740  *	0x20 - ARP/IPv4 Request Packet
   5741  *	0x40 - Direct IPv4 Packet
   5742  *	0x80 - Direct IPv6 Packet
   5743  *
   5744  * Setting another flag will cause the sysctl to return an
   5745  * error.
   5746  */
   5747 static int
   5748 ixgbe_sysctl_wufc(SYSCTLFN_ARGS)
   5749 {
   5750 	struct sysctlnode node = *rnode;
   5751 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5752 	int error = 0;
   5753 	u32 new_wufc;
   5754 
   5755 	new_wufc = adapter->wufc;
   5756 	node.sysctl_data = &new_wufc;
   5757 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5758 	if ((error) || (newp == NULL))
   5759 		return (error);
   5760 	if (new_wufc == adapter->wufc)
   5761 		return (0);
   5762 
   5763 	if (new_wufc & 0xffffff00)
   5764 		return (EINVAL);
   5765 	else {
   5766 		new_wufc &= 0xff;
   5767 		new_wufc |= (0xffffff & adapter->wufc);
   5768 		adapter->wufc = new_wufc;
   5769 	}
   5770 
   5771 	return (0);
   5772 }
   5773 
   5774 #ifdef IXGBE_DEBUG
   5775 static int
   5776 ixgbe_sysctl_print_rss_config(SYSCTLFN_ARGS)
   5777 {
   5778 	struct adapter *adapter = (struct adapter *)node.sysctl_data;
   5779 	struct ixgbe_hw *hw = &adapter->hw;
   5780 	device_t dev = adapter->dev;
   5781 	int error = 0, reta_size;
   5782 	struct sbuf *buf;
   5783 	u32 reg;
   5784 
   5785 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
   5786 	if (!buf) {
   5787 		device_printf(dev, "Could not allocate sbuf for output.\n");
   5788 		return (ENOMEM);
   5789 	}
   5790 
   5791 	// TODO: use sbufs to make a string to print out
   5792 	/* Set multiplier for RETA setup and table size based on MAC */
   5793 	switch (adapter->hw.mac.type) {
   5794 	case ixgbe_mac_X550:
   5795 	case ixgbe_mac_X550EM_x:
   5796 		reta_size = 128;
   5797 		break;
   5798 	default:
   5799 		reta_size = 32;
   5800 		break;
   5801 	}
   5802 
   5803 	/* Print out the redirection table */
   5804 	sbuf_cat(buf, "\n");
   5805 	for (int i = 0; i < reta_size; i++) {
   5806 		if (i < 32) {
   5807 			reg = IXGBE_READ_REG(hw, IXGBE_RETA(i));
   5808 			sbuf_printf(buf, "RETA(%2d): 0x%08x\n", i, reg);
   5809 		} else {
   5810 			reg = IXGBE_READ_REG(hw, IXGBE_ERETA(i - 32));
   5811 			sbuf_printf(buf, "ERETA(%2d): 0x%08x\n", i - 32, reg);
   5812 		}
   5813 	}
   5814 
   5815 	// TODO: print more config
   5816 
   5817 	error = sbuf_finish(buf);
   5818 	if (error)
   5819 		device_printf(dev, "Error finishing sbuf: %d\n", error);
   5820 
   5821 	sbuf_delete(buf);
   5822 	return (0);
   5823 }
   5824 #endif /* IXGBE_DEBUG */
   5825 
   5826 /*
   5827 ** Enable the hardware to drop packets when the buffer is
   5828 ** full. This is useful when multiqueue,so that no single
   5829 ** queue being full stalls the entire RX engine. We only
   5830 ** enable this when Multiqueue AND when Flow Control is
   5831 ** disabled.
   5832 */
   5833 static void
   5834 ixgbe_enable_rx_drop(struct adapter *adapter)
   5835 {
   5836         struct ixgbe_hw *hw = &adapter->hw;
   5837 
   5838 	for (int i = 0; i < adapter->num_queues; i++) {
   5839 		struct rx_ring *rxr = &adapter->rx_rings[i];
   5840         	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
   5841         	srrctl |= IXGBE_SRRCTL_DROP_EN;
   5842         	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
   5843 	}
   5844 #ifdef PCI_IOV
   5845 	/* enable drop for each vf */
   5846 	for (int i = 0; i < adapter->num_vfs; i++) {
   5847 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
   5848 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT) |
   5849 		    IXGBE_QDE_ENABLE));
   5850 	}
   5851 #endif
   5852 }
   5853 
   5854 static void
   5855 ixgbe_disable_rx_drop(struct adapter *adapter)
   5856 {
   5857         struct ixgbe_hw *hw = &adapter->hw;
   5858 
   5859 	for (int i = 0; i < adapter->num_queues; i++) {
   5860 		struct rx_ring *rxr = &adapter->rx_rings[i];
   5861         	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
   5862         	srrctl &= ~IXGBE_SRRCTL_DROP_EN;
   5863         	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
   5864 	}
   5865 #ifdef PCI_IOV
   5866 	/* disable drop for each vf */
   5867 	for (int i = 0; i < adapter->num_vfs; i++) {
   5868 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
   5869 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT)));
   5870 	}
   5871 #endif
   5872 }
   5873 
   5874 static void
   5875 ixgbe_rearm_queues(struct adapter *adapter, u64 queues)
   5876 {
   5877 	u32 mask;
   5878 
   5879 	switch (adapter->hw.mac.type) {
   5880 	case ixgbe_mac_82598EB:
   5881 		mask = (IXGBE_EIMS_RTX_QUEUE & queues);
   5882 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, mask);
   5883 		break;
   5884 	case ixgbe_mac_82599EB:
   5885 	case ixgbe_mac_X540:
   5886 	case ixgbe_mac_X550:
   5887 	case ixgbe_mac_X550EM_x:
   5888 		mask = (queues & 0xFFFFFFFF);
   5889 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(0), mask);
   5890 		mask = (queues >> 32);
   5891 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(1), mask);
   5892 		break;
   5893 	default:
   5894 		break;
   5895 	}
   5896 }
   5897 
   5898 #ifdef PCI_IOV
   5899 
   5900 /*
   5901 ** Support functions for SRIOV/VF management
   5902 */
   5903 
   5904 static void
   5905 ixgbe_ping_all_vfs(struct adapter *adapter)
   5906 {
   5907 	struct ixgbe_vf *vf;
   5908 
   5909 	for (int i = 0; i < adapter->num_vfs; i++) {
   5910 		vf = &adapter->vfs[i];
   5911 		if (vf->flags & IXGBE_VF_ACTIVE)
   5912 			ixgbe_send_vf_msg(adapter, vf, IXGBE_PF_CONTROL_MSG);
   5913 	}
   5914 }
   5915 
   5916 
   5917 static void
   5918 ixgbe_vf_set_default_vlan(struct adapter *adapter, struct ixgbe_vf *vf,
   5919     uint16_t tag)
   5920 {
   5921 	struct ixgbe_hw *hw;
   5922 	uint32_t vmolr, vmvir;
   5923 
   5924 	hw = &adapter->hw;
   5925 
   5926 	vf->vlan_tag = tag;
   5927 
   5928 	vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf->pool));
   5929 
   5930 	/* Do not receive packets that pass inexact filters. */
   5931 	vmolr &= ~(IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_ROPE);
   5932 
   5933 	/* Disable Multicast Promicuous Mode. */
   5934 	vmolr &= ~IXGBE_VMOLR_MPE;
   5935 
   5936 	/* Accept broadcasts. */
   5937 	vmolr |= IXGBE_VMOLR_BAM;
   5938 
   5939 	if (tag == 0) {
   5940 		/* Accept non-vlan tagged traffic. */
   5941 		//vmolr |= IXGBE_VMOLR_AUPE;
   5942 
   5943 		/* Allow VM to tag outgoing traffic; no default tag. */
   5944 		vmvir = 0;
   5945 	} else {
   5946 		/* Require vlan-tagged traffic. */
   5947 		vmolr &= ~IXGBE_VMOLR_AUPE;
   5948 
   5949 		/* Tag all traffic with provided vlan tag. */
   5950 		vmvir = (tag | IXGBE_VMVIR_VLANA_DEFAULT);
   5951 	}
   5952 	IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf->pool), vmolr);
   5953 	IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf->pool), vmvir);
   5954 }
   5955 
   5956 
   5957 static boolean_t
   5958 ixgbe_vf_frame_size_compatible(struct adapter *adapter, struct ixgbe_vf *vf)
   5959 {
   5960 
   5961 	/*
   5962 	 * Frame size compatibility between PF and VF is only a problem on
   5963 	 * 82599-based cards.  X540 and later support any combination of jumbo
   5964 	 * frames on PFs and VFs.
   5965 	 */
   5966 	if (adapter->hw.mac.type != ixgbe_mac_82599EB)
   5967 		return (TRUE);
   5968 
   5969 	switch (vf->api_ver) {
   5970 	case IXGBE_API_VER_1_0:
   5971 	case IXGBE_API_VER_UNKNOWN:
   5972 		/*
   5973 		 * On legacy (1.0 and older) VF versions, we don't support jumbo
   5974 		 * frames on either the PF or the VF.
   5975 		 */
   5976 		if (adapter->max_frame_size > ETHER_MAX_LEN ||
   5977 		    vf->max_frame_size > ETHER_MAX_LEN)
   5978 		    return (FALSE);
   5979 
   5980 		return (TRUE);
   5981 
   5982 		break;
   5983 	case IXGBE_API_VER_1_1:
   5984 	default:
   5985 		/*
   5986 		 * 1.1 or later VF versions always work if they aren't using
   5987 		 * jumbo frames.
   5988 		 */
   5989 		if (vf->max_frame_size <= ETHER_MAX_LEN)
   5990 			return (TRUE);
   5991 
   5992 		/*
   5993 		 * Jumbo frames only work with VFs if the PF is also using jumbo
   5994 		 * frames.
   5995 		 */
   5996 		if (adapter->max_frame_size <= ETHER_MAX_LEN)
   5997 			return (TRUE);
   5998 
   5999 		return (FALSE);
   6000 
   6001 	}
   6002 }
   6003 
   6004 
   6005 static void
   6006 ixgbe_process_vf_reset(struct adapter *adapter, struct ixgbe_vf *vf)
   6007 {
   6008 	ixgbe_vf_set_default_vlan(adapter, vf, vf->default_vlan);
   6009 
   6010 	// XXX clear multicast addresses
   6011 
   6012 	ixgbe_clear_rar(&adapter->hw, vf->rar_index);
   6013 
   6014 	vf->api_ver = IXGBE_API_VER_UNKNOWN;
   6015 }
   6016 
   6017 
   6018 static void
   6019 ixgbe_vf_enable_transmit(struct adapter *adapter, struct ixgbe_vf *vf)
   6020 {
   6021 	struct ixgbe_hw *hw;
   6022 	uint32_t vf_index, vfte;
   6023 
   6024 	hw = &adapter->hw;
   6025 
   6026 	vf_index = IXGBE_VF_INDEX(vf->pool);
   6027 	vfte = IXGBE_READ_REG(hw, IXGBE_VFTE(vf_index));
   6028 	vfte |= IXGBE_VF_BIT(vf->pool);
   6029 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(vf_index), vfte);
   6030 }
   6031 
   6032 
   6033 static void
   6034 ixgbe_vf_enable_receive(struct adapter *adapter, struct ixgbe_vf *vf)
   6035 {
   6036 	struct ixgbe_hw *hw;
   6037 	uint32_t vf_index, vfre;
   6038 
   6039 	hw = &adapter->hw;
   6040 
   6041 	vf_index = IXGBE_VF_INDEX(vf->pool);
   6042 	vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(vf_index));
   6043 	if (ixgbe_vf_frame_size_compatible(adapter, vf))
   6044 		vfre |= IXGBE_VF_BIT(vf->pool);
   6045 	else
   6046 		vfre &= ~IXGBE_VF_BIT(vf->pool);
   6047 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(vf_index), vfre);
   6048 }
   6049 
   6050 
   6051 static void
   6052 ixgbe_vf_reset_msg(struct adapter *adapter, struct ixgbe_vf *vf, uint32_t *msg)
   6053 {
   6054 	struct ixgbe_hw *hw;
   6055 	uint32_t ack;
   6056 	uint32_t resp[IXGBE_VF_PERMADDR_MSG_LEN];
   6057 
   6058 	hw = &adapter->hw;
   6059 
   6060 	ixgbe_process_vf_reset(adapter, vf);
   6061 
   6062 	if (ixgbe_validate_mac_addr(vf->ether_addr) == 0) {
   6063 		ixgbe_set_rar(&adapter->hw, vf->rar_index,
   6064 		    vf->ether_addr, vf->pool, TRUE);
   6065 		ack = IXGBE_VT_MSGTYPE_ACK;
   6066 	} else
   6067 		ack = IXGBE_VT_MSGTYPE_NACK;
   6068 
   6069 	ixgbe_vf_enable_transmit(adapter, vf);
   6070 	ixgbe_vf_enable_receive(adapter, vf);
   6071 
   6072 	vf->flags |= IXGBE_VF_CTS;
   6073 
   6074 	resp[0] = IXGBE_VF_RESET | ack | IXGBE_VT_MSGTYPE_CTS;
   6075 	bcopy(vf->ether_addr, &resp[1], ETHER_ADDR_LEN);
   6076 	resp[3] = hw->mac.mc_filter_type;
   6077 	ixgbe_write_mbx(hw, resp, IXGBE_VF_PERMADDR_MSG_LEN, vf->pool);
   6078 }
   6079 
   6080 
   6081 static void
   6082 ixgbe_vf_set_mac(struct adapter *adapter, struct ixgbe_vf *vf, uint32_t *msg)
   6083 {
   6084 	uint8_t *mac;
   6085 
   6086 	mac = (uint8_t*)&msg[1];
   6087 
   6088 	/* Check that the VF has permission to change the MAC address. */
   6089 	if (!(vf->flags & IXGBE_VF_CAP_MAC) && ixgbe_vf_mac_changed(vf, mac)) {
   6090 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   6091 		return;
   6092 	}
   6093 
   6094 	if (ixgbe_validate_mac_addr(mac) != 0) {
   6095 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   6096 		return;
   6097 	}
   6098 
   6099 	bcopy(mac, vf->ether_addr, ETHER_ADDR_LEN);
   6100 
   6101 	ixgbe_set_rar(&adapter->hw, vf->rar_index, vf->ether_addr,
   6102 	    vf->pool, TRUE);
   6103 
   6104 	ixgbe_send_vf_ack(adapter, vf, msg[0]);
   6105 }
   6106 
   6107 
   6108 /*
   6109 ** VF multicast addresses are set by using the appropriate bit in
   6110 ** 1 of 128 32 bit addresses (4096 possible).
   6111 */
   6112 static void
   6113 ixgbe_vf_set_mc_addr(struct adapter *adapter, struct ixgbe_vf *vf, u32 *msg)
   6114 {
   6115 	u16	*list = (u16*)&msg[1];
   6116 	int	entries;
   6117 	u32	vmolr, vec_bit, vec_reg, mta_reg;
   6118 
   6119 	entries = (msg[0] & IXGBE_VT_MSGINFO_MASK) >> IXGBE_VT_MSGINFO_SHIFT;
   6120 	entries = min(entries, IXGBE_MAX_VF_MC);
   6121 
   6122 	vmolr = IXGBE_READ_REG(&adapter->hw, IXGBE_VMOLR(vf->pool));
   6123 
   6124 	vf->num_mc_hashes = entries;
   6125 
   6126 	/* Set the appropriate MTA bit */
   6127 	for (int i = 0; i < entries; i++) {
   6128 		vf->mc_hash[i] = list[i];
   6129 		vec_reg = (vf->mc_hash[i] >> 5) & 0x7F;
   6130                 vec_bit = vf->mc_hash[i] & 0x1F;
   6131                 mta_reg = IXGBE_READ_REG(&adapter->hw, IXGBE_MTA(vec_reg));
   6132                 mta_reg |= (1 << vec_bit);
   6133                 IXGBE_WRITE_REG(&adapter->hw, IXGBE_MTA(vec_reg), mta_reg);
   6134         }
   6135 
   6136 	vmolr |= IXGBE_VMOLR_ROMPE;
   6137 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_VMOLR(vf->pool), vmolr);
   6138 	ixgbe_send_vf_ack(adapter, vf, msg[0]);
   6139 	return;
   6140 }
   6141 
   6142 
   6143 static void
   6144 ixgbe_vf_set_vlan(struct adapter *adapter, struct ixgbe_vf *vf, uint32_t *msg)
   6145 {
   6146 	struct ixgbe_hw *hw;
   6147 	int enable;
   6148 	uint16_t tag;
   6149 
   6150 	hw = &adapter->hw;
   6151 	enable = IXGBE_VT_MSGINFO(msg[0]);
   6152 	tag = msg[1] & IXGBE_VLVF_VLANID_MASK;
   6153 
   6154 	if (!(vf->flags & IXGBE_VF_CAP_VLAN)) {
   6155 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   6156 		return;
   6157 	}
   6158 
   6159 	/* It is illegal to enable vlan tag 0. */
   6160 	if (tag == 0 && enable != 0){
   6161 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   6162 		return;
   6163 	}
   6164 
   6165 	ixgbe_set_vfta(hw, tag, vf->pool, enable);
   6166 	ixgbe_send_vf_ack(adapter, vf, msg[0]);
   6167 }
   6168 
   6169 
   6170 static void
   6171 ixgbe_vf_set_lpe(struct adapter *adapter, struct ixgbe_vf *vf, uint32_t *msg)
   6172 {
   6173 	struct ixgbe_hw *hw;
   6174 	uint32_t vf_max_size, pf_max_size, mhadd;
   6175 
   6176 	hw = &adapter->hw;
   6177 	vf_max_size = msg[1];
   6178 
   6179 	if (vf_max_size < ETHER_CRC_LEN) {
   6180 		/* We intentionally ACK invalid LPE requests. */
   6181 		ixgbe_send_vf_ack(adapter, vf, msg[0]);
   6182 		return;
   6183 	}
   6184 
   6185 	vf_max_size -= ETHER_CRC_LEN;
   6186 
   6187 	if (vf_max_size > IXGBE_MAX_FRAME_SIZE) {
   6188 		/* We intentionally ACK invalid LPE requests. */
   6189 		ixgbe_send_vf_ack(adapter, vf, msg[0]);
   6190 		return;
   6191 	}
   6192 
   6193 	vf->max_frame_size = vf_max_size;
   6194 	ixgbe_update_max_frame(adapter, vf->max_frame_size);
   6195 
   6196 	/*
   6197 	 * We might have to disable reception to this VF if the frame size is
   6198 	 * not compatible with the config on the PF.
   6199 	 */
   6200 	ixgbe_vf_enable_receive(adapter, vf);
   6201 
   6202 	mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
   6203 	pf_max_size = (mhadd & IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
   6204 
   6205 	if (pf_max_size < adapter->max_frame_size) {
   6206 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
   6207 		mhadd |= adapter->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
   6208 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
   6209 	}
   6210 
   6211 	ixgbe_send_vf_ack(adapter, vf, msg[0]);
   6212 }
   6213 
   6214 
   6215 static void
   6216 ixgbe_vf_set_macvlan(struct adapter *adapter, struct ixgbe_vf *vf,
   6217     uint32_t *msg)
   6218 {
   6219 	//XXX implement this
   6220 	ixgbe_send_vf_nack(adapter, vf, msg[0]);
   6221 }
   6222 
   6223 
   6224 static void
   6225 ixgbe_vf_api_negotiate(struct adapter *adapter, struct ixgbe_vf *vf,
   6226     uint32_t *msg)
   6227 {
   6228 
   6229 	switch (msg[1]) {
   6230 	case IXGBE_API_VER_1_0:
   6231 	case IXGBE_API_VER_1_1:
   6232 		vf->api_ver = msg[1];
   6233 		ixgbe_send_vf_ack(adapter, vf, msg[0]);
   6234 		break;
   6235 	default:
   6236 		vf->api_ver = IXGBE_API_VER_UNKNOWN;
   6237 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   6238 		break;
   6239 	}
   6240 }
   6241 
   6242 
   6243 static void
   6244 ixgbe_vf_get_queues(struct adapter *adapter, struct ixgbe_vf *vf,
   6245     uint32_t *msg)
   6246 {
   6247 	struct ixgbe_hw *hw;
   6248 	uint32_t resp[IXGBE_VF_GET_QUEUES_RESP_LEN];
   6249 	int num_queues;
   6250 
   6251 	hw = &adapter->hw;
   6252 
   6253 	/* GET_QUEUES is not supported on pre-1.1 APIs. */
   6254 	switch (msg[0]) {
   6255 	case IXGBE_API_VER_1_0:
   6256 	case IXGBE_API_VER_UNKNOWN:
   6257 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   6258 		return;
   6259 	}
   6260 
   6261 	resp[0] = IXGBE_VF_GET_QUEUES | IXGBE_VT_MSGTYPE_ACK |
   6262 	    IXGBE_VT_MSGTYPE_CTS;
   6263 
   6264 	num_queues = ixgbe_vf_queues(ixgbe_get_iov_mode(adapter));
   6265 	resp[IXGBE_VF_TX_QUEUES] = num_queues;
   6266 	resp[IXGBE_VF_RX_QUEUES] = num_queues;
   6267 	resp[IXGBE_VF_TRANS_VLAN] = (vf->default_vlan != 0);
   6268 	resp[IXGBE_VF_DEF_QUEUE] = 0;
   6269 
   6270 	ixgbe_write_mbx(hw, resp, IXGBE_VF_GET_QUEUES_RESP_LEN, vf->pool);
   6271 }
   6272 
   6273 
   6274 static void
   6275 ixgbe_process_vf_msg(struct adapter *adapter, struct ixgbe_vf *vf)
   6276 {
   6277 	struct ixgbe_hw *hw;
   6278 	uint32_t msg[IXGBE_VFMAILBOX_SIZE];
   6279 	int error;
   6280 
   6281 	hw = &adapter->hw;
   6282 
   6283 	error = ixgbe_read_mbx(hw, msg, IXGBE_VFMAILBOX_SIZE, vf->pool);
   6284 
   6285 	if (error != 0)
   6286 		return;
   6287 
   6288 	CTR3(KTR_MALLOC, "%s: received msg %x from %d",
   6289 	    adapter->ifp->if_xname, msg[0], vf->pool);
   6290 	if (msg[0] == IXGBE_VF_RESET) {
   6291 		ixgbe_vf_reset_msg(adapter, vf, msg);
   6292 		return;
   6293 	}
   6294 
   6295 	if (!(vf->flags & IXGBE_VF_CTS)) {
   6296 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   6297 		return;
   6298 	}
   6299 
   6300 	switch (msg[0] & IXGBE_VT_MSG_MASK) {
   6301 	case IXGBE_VF_SET_MAC_ADDR:
   6302 		ixgbe_vf_set_mac(adapter, vf, msg);
   6303 		break;
   6304 	case IXGBE_VF_SET_MULTICAST:
   6305 		ixgbe_vf_set_mc_addr(adapter, vf, msg);
   6306 		break;
   6307 	case IXGBE_VF_SET_VLAN:
   6308 		ixgbe_vf_set_vlan(adapter, vf, msg);
   6309 		break;
   6310 	case IXGBE_VF_SET_LPE:
   6311 		ixgbe_vf_set_lpe(adapter, vf, msg);
   6312 		break;
   6313 	case IXGBE_VF_SET_MACVLAN:
   6314 		ixgbe_vf_set_macvlan(adapter, vf, msg);
   6315 		break;
   6316 	case IXGBE_VF_API_NEGOTIATE:
   6317 		ixgbe_vf_api_negotiate(adapter, vf, msg);
   6318 		break;
   6319 	case IXGBE_VF_GET_QUEUES:
   6320 		ixgbe_vf_get_queues(adapter, vf, msg);
   6321 		break;
   6322 	default:
   6323 		ixgbe_send_vf_nack(adapter, vf, msg[0]);
   6324 	}
   6325 }
   6326 
   6327 
   6328 /*
   6329  * Tasklet for handling VF -> PF mailbox messages.
   6330  */
   6331 static void
   6332 ixgbe_handle_mbx(void *context, int pending)
   6333 {
   6334 	struct adapter *adapter;
   6335 	struct ixgbe_hw *hw;
   6336 	struct ixgbe_vf *vf;
   6337 	int i;
   6338 
   6339 	adapter = context;
   6340 	hw = &adapter->hw;
   6341 
   6342 	IXGBE_CORE_LOCK(adapter);
   6343 	for (i = 0; i < adapter->num_vfs; i++) {
   6344 		vf = &adapter->vfs[i];
   6345 
   6346 		if (vf->flags & IXGBE_VF_ACTIVE) {
   6347 			if (ixgbe_check_for_rst(hw, vf->pool) == 0)
   6348 				ixgbe_process_vf_reset(adapter, vf);
   6349 
   6350 			if (ixgbe_check_for_msg(hw, vf->pool) == 0)
   6351 				ixgbe_process_vf_msg(adapter, vf);
   6352 
   6353 			if (ixgbe_check_for_ack(hw, vf->pool) == 0)
   6354 				ixgbe_process_vf_ack(adapter, vf);
   6355 		}
   6356 	}
   6357 	IXGBE_CORE_UNLOCK(adapter);
   6358 }
   6359 
   6360 
   6361 static int
   6362 ixgbe_init_iov(device_t dev, u16 num_vfs, const nvlist_t *config)
   6363 {
   6364 	struct adapter *adapter;
   6365 	enum ixgbe_iov_mode mode;
   6366 
   6367 	adapter = device_get_softc(dev);
   6368 	adapter->num_vfs = num_vfs;
   6369 	mode = ixgbe_get_iov_mode(adapter);
   6370 
   6371 	if (num_vfs > ixgbe_max_vfs(mode)) {
   6372 		adapter->num_vfs = 0;
   6373 		return (ENOSPC);
   6374 	}
   6375 
   6376 	IXGBE_CORE_LOCK(adapter);
   6377 
   6378 	adapter->vfs = malloc(sizeof(*adapter->vfs) * num_vfs, M_IXGBE,
   6379 	    M_NOWAIT | M_ZERO);
   6380 
   6381 	if (adapter->vfs == NULL) {
   6382 		adapter->num_vfs = 0;
   6383 		IXGBE_CORE_UNLOCK(adapter);
   6384 		return (ENOMEM);
   6385 	}
   6386 
   6387 	ixgbe_init_locked(adapter);
   6388 
   6389 	IXGBE_CORE_UNLOCK(adapter);
   6390 
   6391 	return (0);
   6392 }
   6393 
   6394 
   6395 static void
   6396 ixgbe_uninit_iov(device_t dev)
   6397 {
   6398 	struct ixgbe_hw *hw;
   6399 	struct adapter *adapter;
   6400 	uint32_t pf_reg, vf_reg;
   6401 
   6402 	adapter = device_get_softc(dev);
   6403 	hw = &adapter->hw;
   6404 
   6405 	IXGBE_CORE_LOCK(adapter);
   6406 
   6407 	/* Enable rx/tx for the PF and disable it for all VFs. */
   6408 	pf_reg = IXGBE_VF_INDEX(adapter->pool);
   6409 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(pf_reg),
   6410 	    IXGBE_VF_BIT(adapter->pool));
   6411 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(pf_reg),
   6412 	    IXGBE_VF_BIT(adapter->pool));
   6413 
   6414 	if (pf_reg == 0)
   6415 		vf_reg = 1;
   6416 	else
   6417 		vf_reg = 0;
   6418 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(vf_reg), 0);
   6419 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(vf_reg), 0);
   6420 
   6421 	IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, 0);
   6422 
   6423 	free(adapter->vfs, M_IXGBE);
   6424 	adapter->vfs = NULL;
   6425 	adapter->num_vfs = 0;
   6426 
   6427 	IXGBE_CORE_UNLOCK(adapter);
   6428 }
   6429 
   6430 
   6431 static void
   6432 ixgbe_initialize_iov(struct adapter *adapter)
   6433 {
   6434 	struct ixgbe_hw *hw = &adapter->hw;
   6435 	uint32_t mrqc, mtqc, vt_ctl, vf_reg, gcr_ext, gpie;
   6436 	enum ixgbe_iov_mode mode;
   6437 	int i;
   6438 
   6439 	mode = ixgbe_get_iov_mode(adapter);
   6440 	if (mode == IXGBE_NO_VM)
   6441 		return;
   6442 
   6443 	IXGBE_CORE_LOCK_ASSERT(adapter);
   6444 
   6445 	mrqc = IXGBE_READ_REG(hw, IXGBE_MRQC);
   6446 	mrqc &= ~IXGBE_MRQC_MRQE_MASK;
   6447 
   6448 	switch (mode) {
   6449 	case IXGBE_64_VM:
   6450 		mrqc |= IXGBE_MRQC_VMDQRSS64EN;
   6451 		break;
   6452 	case IXGBE_32_VM:
   6453 		mrqc |= IXGBE_MRQC_VMDQRSS32EN;
   6454 		break;
   6455 	default:
   6456 		panic("Unexpected SR-IOV mode %d", mode);
   6457 	}
   6458 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
   6459 
   6460 	mtqc = IXGBE_MTQC_VT_ENA;
   6461 	switch (mode) {
   6462 	case IXGBE_64_VM:
   6463 		mtqc |= IXGBE_MTQC_64VF;
   6464 		break;
   6465 	case IXGBE_32_VM:
   6466 		mtqc |= IXGBE_MTQC_32VF;
   6467 		break;
   6468 	default:
   6469 		panic("Unexpected SR-IOV mode %d", mode);
   6470 	}
   6471 	IXGBE_WRITE_REG(hw, IXGBE_MTQC, mtqc);
   6472 
   6473 
   6474 	gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
   6475 	gcr_ext |= IXGBE_GCR_EXT_MSIX_EN;
   6476 	gcr_ext &= ~IXGBE_GCR_EXT_VT_MODE_MASK;
   6477 	switch (mode) {
   6478 	case IXGBE_64_VM:
   6479 		gcr_ext |= IXGBE_GCR_EXT_VT_MODE_64;
   6480 		break;
   6481 	case IXGBE_32_VM:
   6482 		gcr_ext |= IXGBE_GCR_EXT_VT_MODE_32;
   6483 		break;
   6484 	default:
   6485 		panic("Unexpected SR-IOV mode %d", mode);
   6486 	}
   6487 	IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
   6488 
   6489 
   6490 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
   6491 	gcr_ext &= ~IXGBE_GPIE_VTMODE_MASK;
   6492 	switch (mode) {
   6493 	case IXGBE_64_VM:
   6494 		gpie |= IXGBE_GPIE_VTMODE_64;
   6495 		break;
   6496 	case IXGBE_32_VM:
   6497 		gpie |= IXGBE_GPIE_VTMODE_32;
   6498 		break;
   6499 	default:
   6500 		panic("Unexpected SR-IOV mode %d", mode);
   6501 	}
   6502 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
   6503 
   6504 	/* Enable rx/tx for the PF. */
   6505 	vf_reg = IXGBE_VF_INDEX(adapter->pool);
   6506 	IXGBE_WRITE_REG(hw, IXGBE_VFRE(vf_reg),
   6507 	    IXGBE_VF_BIT(adapter->pool));
   6508 	IXGBE_WRITE_REG(hw, IXGBE_VFTE(vf_reg),
   6509 	    IXGBE_VF_BIT(adapter->pool));
   6510 
   6511 	/* Allow VM-to-VM communication. */
   6512 	IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN);
   6513 
   6514 	vt_ctl = IXGBE_VT_CTL_VT_ENABLE | IXGBE_VT_CTL_REPLEN;
   6515 	vt_ctl |= (adapter->pool << IXGBE_VT_CTL_POOL_SHIFT);
   6516 	IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vt_ctl);
   6517 
   6518 	for (i = 0; i < adapter->num_vfs; i++)
   6519 		ixgbe_init_vf(adapter, &adapter->vfs[i]);
   6520 }
   6521 
   6522 
   6523 /*
   6524 ** Check the max frame setting of all active VF's
   6525 */
   6526 static void
   6527 ixgbe_recalculate_max_frame(struct adapter *adapter)
   6528 {
   6529 	struct ixgbe_vf *vf;
   6530 
   6531 	IXGBE_CORE_LOCK_ASSERT(adapter);
   6532 
   6533 	for (int i = 0; i < adapter->num_vfs; i++) {
   6534 		vf = &adapter->vfs[i];
   6535 		if (vf->flags & IXGBE_VF_ACTIVE)
   6536 			ixgbe_update_max_frame(adapter, vf->max_frame_size);
   6537 	}
   6538 }
   6539 
   6540 
   6541 static void
   6542 ixgbe_init_vf(struct adapter *adapter, struct ixgbe_vf *vf)
   6543 {
   6544 	struct ixgbe_hw *hw;
   6545 	uint32_t vf_index, pfmbimr;
   6546 
   6547 	IXGBE_CORE_LOCK_ASSERT(adapter);
   6548 
   6549 	hw = &adapter->hw;
   6550 
   6551 	if (!(vf->flags & IXGBE_VF_ACTIVE))
   6552 		return;
   6553 
   6554 	vf_index = IXGBE_VF_INDEX(vf->pool);
   6555 	pfmbimr = IXGBE_READ_REG(hw, IXGBE_PFMBIMR(vf_index));
   6556 	pfmbimr |= IXGBE_VF_BIT(vf->pool);
   6557 	IXGBE_WRITE_REG(hw, IXGBE_PFMBIMR(vf_index), pfmbimr);
   6558 
   6559 	ixgbe_vf_set_default_vlan(adapter, vf, vf->vlan_tag);
   6560 
   6561 	// XXX multicast addresses
   6562 
   6563 	if (ixgbe_validate_mac_addr(vf->ether_addr) == 0) {
   6564 		ixgbe_set_rar(&adapter->hw, vf->rar_index,
   6565 		    vf->ether_addr, vf->pool, TRUE);
   6566 	}
   6567 
   6568 	ixgbe_vf_enable_transmit(adapter, vf);
   6569 	ixgbe_vf_enable_receive(adapter, vf);
   6570 
   6571 	ixgbe_send_vf_msg(adapter, vf, IXGBE_PF_CONTROL_MSG);
   6572 }
   6573 
   6574 static int
   6575 ixgbe_add_vf(device_t dev, u16 vfnum, const nvlist_t *config)
   6576 {
   6577 	struct adapter *adapter;
   6578 	struct ixgbe_vf *vf;
   6579 	const void *mac;
   6580 
   6581 	adapter = device_get_softc(dev);
   6582 
   6583 	KASSERT(vfnum < adapter->num_vfs, ("VF index %d is out of range %d",
   6584 	    vfnum, adapter->num_vfs));
   6585 
   6586 	IXGBE_CORE_LOCK(adapter);
   6587 	vf = &adapter->vfs[vfnum];
   6588 	vf->pool= vfnum;
   6589 
   6590 	/* RAR[0] is used by the PF so use vfnum + 1 for VF RAR. */
   6591 	vf->rar_index = vfnum + 1;
   6592 	vf->default_vlan = 0;
   6593 	vf->max_frame_size = ETHER_MAX_LEN;
   6594 	ixgbe_update_max_frame(adapter, vf->max_frame_size);
   6595 
   6596 	if (nvlist_exists_binary(config, "mac-addr")) {
   6597 		mac = nvlist_get_binary(config, "mac-addr", NULL);
   6598 		bcopy(mac, vf->ether_addr, ETHER_ADDR_LEN);
   6599 		if (nvlist_get_bool(config, "allow-set-mac"))
   6600 			vf->flags |= IXGBE_VF_CAP_MAC;
   6601 	} else
   6602 		/*
   6603 		 * If the administrator has not specified a MAC address then
   6604 		 * we must allow the VF to choose one.
   6605 		 */
   6606 		vf->flags |= IXGBE_VF_CAP_MAC;
   6607 
   6608 	vf->flags = IXGBE_VF_ACTIVE;
   6609 
   6610 	ixgbe_init_vf(adapter, vf);
   6611 	IXGBE_CORE_UNLOCK(adapter);
   6612 
   6613 	return (0);
   6614 }
   6615 #endif /* PCI_IOV */
   6616