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