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