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