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