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