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