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