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