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