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