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