xref: /freebsd/sys/dev/ixgbe/if_ix.c (revision a0302c9231502bae8f43edbd5fb8d73132eb8da7)
1758cc3dcSJack F Vogel /******************************************************************************
2758cc3dcSJack F Vogel 
38eb6488eSEric Joyner   Copyright (c) 2001-2017, Intel Corporation
4758cc3dcSJack F Vogel   All rights reserved.
5758cc3dcSJack F Vogel 
6758cc3dcSJack F Vogel   Redistribution and use in source and binary forms, with or without
7758cc3dcSJack F Vogel   modification, are permitted provided that the following conditions are met:
8758cc3dcSJack F Vogel 
9758cc3dcSJack F Vogel    1. Redistributions of source code must retain the above copyright notice,
10758cc3dcSJack F Vogel       this list of conditions and the following disclaimer.
11758cc3dcSJack F Vogel 
12758cc3dcSJack F Vogel    2. Redistributions in binary form must reproduce the above copyright
13758cc3dcSJack F Vogel       notice, this list of conditions and the following disclaimer in the
14758cc3dcSJack F Vogel       documentation and/or other materials provided with the distribution.
15758cc3dcSJack F Vogel 
16758cc3dcSJack F Vogel    3. Neither the name of the Intel Corporation nor the names of its
17758cc3dcSJack F Vogel       contributors may be used to endorse or promote products derived from
18758cc3dcSJack F Vogel       this software without specific prior written permission.
19758cc3dcSJack F Vogel 
20758cc3dcSJack F Vogel   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21758cc3dcSJack F Vogel   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22758cc3dcSJack F Vogel   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23758cc3dcSJack F Vogel   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24758cc3dcSJack F Vogel   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25758cc3dcSJack F Vogel   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26758cc3dcSJack F Vogel   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27758cc3dcSJack F Vogel   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28758cc3dcSJack F Vogel   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29758cc3dcSJack F Vogel   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30758cc3dcSJack F Vogel   POSSIBILITY OF SUCH DAMAGE.
31758cc3dcSJack F Vogel 
32758cc3dcSJack F Vogel ******************************************************************************/
33758cc3dcSJack F Vogel /*$FreeBSD$*/
34758cc3dcSJack F Vogel 
35758cc3dcSJack F Vogel #include "opt_inet.h"
36758cc3dcSJack F Vogel #include "opt_inet6.h"
37758cc3dcSJack F Vogel #include "opt_rss.h"
38758cc3dcSJack F Vogel 
39758cc3dcSJack F Vogel #include "ixgbe.h"
40c19c7afeSEric Joyner #include "ixgbe_sriov.h"
41c19c7afeSEric Joyner #include "ifdi_if.h"
42c19c7afeSEric Joyner 
43c19c7afeSEric Joyner #include <net/netmap.h>
44c19c7afeSEric Joyner #include <dev/netmap/netmap_kern.h>
45758cc3dcSJack F Vogel 
468eb6488eSEric Joyner /************************************************************************
47758cc3dcSJack F Vogel  * Driver version
488eb6488eSEric Joyner  ************************************************************************/
497d48aa4cSEric Joyner char ixgbe_driver_version[] = "4.0.1-k";
50a9ca1c79SSean Bruno 
518eb6488eSEric Joyner /************************************************************************
52758cc3dcSJack F Vogel  * PCI Device ID Table
53758cc3dcSJack F Vogel  *
54758cc3dcSJack F Vogel  *   Used by probe to select devices to load on
55758cc3dcSJack F Vogel  *   Last field stores an index into ixgbe_strings
56758cc3dcSJack F Vogel  *   Last entry must be all 0s
57758cc3dcSJack F Vogel  *
58758cc3dcSJack F Vogel  *   { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
598eb6488eSEric Joyner  ************************************************************************/
60c19c7afeSEric Joyner static pci_vendor_info_t ixgbe_vendor_info_array[] =
61758cc3dcSJack F Vogel {
62fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_DUAL_PORT,  "Intel(R) 82598EB AF (Dual Fiber)"),
63fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_SINGLE_PORT,  "Intel(R) 82598EB AF (Fiber)"),
64fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_CX4,  "Intel(R) 82598EB AT (CX4)"),
65fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT,  "Intel(R) 82598EB AT"),
66fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT2,  "Intel(R) 82598EB AT2"),
67fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598,  "Intel(R) 82598"),
68fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_DA_DUAL_PORT,  "Intel(R) 82598EB AF DA (Dual Fiber)"),
69fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_CX4_DUAL_PORT,  "Intel(R) 82598EB AT (Dual CX4)"),
70fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_XF_LR,  "Intel(R) 82598EB AF (Dual Fiber LR)"),
71fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM,  "Intel(R) 82598EB AF (Dual Fiber SR)"),
72fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_SFP_LOM,  "Intel(R) 82598EB LOM"),
73fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4,  "Intel(R) X520 82599 (KX4)"),
74fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4_MEZZ,  "Intel(R) X520 82599 (KX4 Mezzanine)"),
75fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP,  "Intel(R) X520 82599ES (SFI/SFP+)"),
76fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_XAUI_LOM,  "Intel(R) X520 82599 (XAUI/BX4)"),
77fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_CX4,  "Intel(R) X520 82599 (Dual CX4)"),
78fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_T3_LOM,  "Intel(R) X520-T 82599 LOM"),
79fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_COMBO_BACKPLANE,  "Intel(R) X520 82599 (Combined Backplane)"),
80fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BACKPLANE_FCOE,  "Intel(R) X520 82599 (Backplane w/FCoE)"),
81fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF2,  "Intel(R) X520 82599 (Dual SFP+)"),
82fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_FCOE,  "Intel(R) X520 82599 (Dual SFP+ w/FCoE)"),
83fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599EN_SFP,  "Intel(R) X520-1 82599EN (SFP+)"),
84fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF_QP,  "Intel(R) X520-4 82599 (Quad SFP+)"),
85fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_QSFP_SF_QP,  "Intel(R) X520-Q1 82599 (QSFP+)"),
86fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T,  "Intel(R) X540-AT2"),
87fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T1,  "Intel(R) X540-T1"),
88fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T,  "Intel(R) X550-T2"),
89fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T1, "Intel(R) X550-T1"),
90fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KR,  "Intel(R) X552 (KR Backplane)"),
91fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KX4,  "Intel(R) X552 (KX4 Backplane)"),
92fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_10G_T,  "Intel(R) X552/X557-AT (10GBASE-T)"),
93fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_1G_T,  "Intel(R) X552 (1000BASE-T)"),
94fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_SFP, "Intel(R) X552 (SFP+)"),
95fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR, "Intel(R) X553 (KR Backplane)"),
96fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR_L, "Intel(R) X553 L (KR Backplane)"),
97fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP, "Intel(R) X553 (SFP+)"),
98fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP_N, "Intel(R) X553 N (SFP+)"),
99fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII, "Intel(R) X553 (1GbE SGMII)"),
100fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII_L, "Intel(R) X553 L (1GbE SGMII)"),
101fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_10G_T, "Intel(R) X553/X557-AT (10GBASE-T)"),
102fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T, "Intel(R) X553 (1GbE)"),
103fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T_L, "Intel(R) X553 L (1GbE)"),
104fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540_BYPASS, "Intel(R) X540-T2 (Bypass)"),
105fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BYPASS, "Intel(R) X520 82599 (Bypass)"),
106758cc3dcSJack F Vogel 	/* required last entry */
107c19c7afeSEric Joyner   PVID_END
108758cc3dcSJack F Vogel };
109758cc3dcSJack F Vogel 
110b1d5caf3SKevin Bowling static void *ixgbe_register(device_t);
111b1d5caf3SKevin Bowling static int  ixgbe_if_attach_pre(if_ctx_t);
112b1d5caf3SKevin Bowling static int  ixgbe_if_attach_post(if_ctx_t);
113b1d5caf3SKevin Bowling static int  ixgbe_if_detach(if_ctx_t);
114b1d5caf3SKevin Bowling static int  ixgbe_if_shutdown(if_ctx_t);
115b1d5caf3SKevin Bowling static int  ixgbe_if_suspend(if_ctx_t);
116b1d5caf3SKevin Bowling static int  ixgbe_if_resume(if_ctx_t);
117c19c7afeSEric Joyner 
118b1d5caf3SKevin Bowling static void ixgbe_if_stop(if_ctx_t);
119b1d5caf3SKevin Bowling void ixgbe_if_enable_intr(if_ctx_t);
120b1d5caf3SKevin Bowling static void ixgbe_if_disable_intr(if_ctx_t);
121b1d5caf3SKevin Bowling static void ixgbe_link_intr_enable(if_ctx_t);
122b1d5caf3SKevin Bowling static int  ixgbe_if_rx_queue_intr_enable(if_ctx_t, uint16_t);
123b1d5caf3SKevin Bowling static void ixgbe_if_media_status(if_ctx_t, struct ifmediareq *);
124b1d5caf3SKevin Bowling static int  ixgbe_if_media_change(if_ctx_t);
125c19c7afeSEric Joyner static int  ixgbe_if_msix_intr_assign(if_ctx_t, int);
126b1d5caf3SKevin Bowling static int  ixgbe_if_mtu_set(if_ctx_t, uint32_t);
127b1d5caf3SKevin Bowling static void ixgbe_if_crcstrip_set(if_ctx_t, int, int);
128b1d5caf3SKevin Bowling static void ixgbe_if_multi_set(if_ctx_t);
129b1d5caf3SKevin Bowling static int  ixgbe_if_promisc_set(if_ctx_t, int);
130b1d5caf3SKevin Bowling static int  ixgbe_if_tx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
131b1d5caf3SKevin Bowling static int  ixgbe_if_rx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
132b1d5caf3SKevin Bowling static void ixgbe_if_queues_free(if_ctx_t);
133b1d5caf3SKevin Bowling static void ixgbe_if_timer(if_ctx_t, uint16_t);
134b1d5caf3SKevin Bowling static void ixgbe_if_update_admin_status(if_ctx_t);
135b1d5caf3SKevin Bowling static void ixgbe_if_vlan_register(if_ctx_t, u16);
136b1d5caf3SKevin Bowling static void ixgbe_if_vlan_unregister(if_ctx_t, u16);
137b1d5caf3SKevin Bowling static int  ixgbe_if_i2c_req(if_ctx_t, struct ifi2creq *);
138b1d5caf3SKevin Bowling static bool ixgbe_if_needs_restart(if_ctx_t, enum iflib_restart_event);
139b1d5caf3SKevin Bowling int ixgbe_intr(void *);
140758cc3dcSJack F Vogel 
1418eb6488eSEric Joyner /************************************************************************
142758cc3dcSJack F Vogel  * Function prototypes
1438eb6488eSEric Joyner  ************************************************************************/
144c19c7afeSEric Joyner static uint64_t ixgbe_if_get_counter(if_ctx_t, ift_counter);
145758cc3dcSJack F Vogel 
146b1d5caf3SKevin Bowling static void ixgbe_enable_queue(struct ixgbe_softc *, u32);
147b1d5caf3SKevin Bowling static void ixgbe_disable_queue(struct ixgbe_softc *, u32);
148b1d5caf3SKevin Bowling static void ixgbe_add_device_sysctls(if_ctx_t);
149b1d5caf3SKevin Bowling static int  ixgbe_allocate_pci_resources(if_ctx_t);
150b1d5caf3SKevin Bowling static int  ixgbe_setup_low_power_mode(if_ctx_t);
151c19c7afeSEric Joyner 
152b1d5caf3SKevin Bowling static void ixgbe_config_dmac(struct ixgbe_softc *);
153b1d5caf3SKevin Bowling static void ixgbe_configure_ivars(struct ixgbe_softc *);
154b1d5caf3SKevin Bowling static void ixgbe_set_ivar(struct ixgbe_softc *, u8, u8, s8);
155c19c7afeSEric Joyner static u8   *ixgbe_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
156b1d5caf3SKevin Bowling static bool ixgbe_sfp_probe(if_ctx_t);
157c19c7afeSEric Joyner 
158b1d5caf3SKevin Bowling static void ixgbe_free_pci_resources(if_ctx_t);
159c19c7afeSEric Joyner 
160b1d5caf3SKevin Bowling static int  ixgbe_msix_link(void *);
161b1d5caf3SKevin Bowling static int  ixgbe_msix_que(void *);
162b1d5caf3SKevin Bowling static void ixgbe_initialize_rss_mapping(struct ixgbe_softc *);
163b1d5caf3SKevin Bowling static void ixgbe_initialize_receive_units(if_ctx_t);
164b1d5caf3SKevin Bowling static void ixgbe_initialize_transmit_units(if_ctx_t);
165c19c7afeSEric Joyner 
166b1d5caf3SKevin Bowling static int  ixgbe_setup_interface(if_ctx_t);
167b1d5caf3SKevin Bowling static void ixgbe_init_device_features(struct ixgbe_softc *);
168b1d5caf3SKevin Bowling static void ixgbe_check_fan_failure(struct ixgbe_softc *, u32, bool);
1697660e4eaSKevin Bowling static void ixgbe_sbuf_fw_version(struct ixgbe_hw *, struct sbuf *);
170b1d5caf3SKevin Bowling static void ixgbe_print_fw_version(if_ctx_t);
171b1d5caf3SKevin Bowling static void ixgbe_add_media_types(if_ctx_t);
172b1d5caf3SKevin Bowling static void ixgbe_update_stats_counters(struct ixgbe_softc *);
173b1d5caf3SKevin Bowling static void ixgbe_config_link(if_ctx_t);
174b1d5caf3SKevin Bowling static void ixgbe_get_slot_info(struct ixgbe_softc *);
175b1d5caf3SKevin Bowling static void ixgbe_check_wol_support(struct ixgbe_softc *);
176b1d5caf3SKevin Bowling static void ixgbe_enable_rx_drop(struct ixgbe_softc *);
177b1d5caf3SKevin Bowling static void ixgbe_disable_rx_drop(struct ixgbe_softc *);
178758cc3dcSJack F Vogel 
179b1d5caf3SKevin Bowling static void ixgbe_add_hw_stats(struct ixgbe_softc *);
180b1d5caf3SKevin Bowling static int  ixgbe_set_flowcntl(struct ixgbe_softc *, int);
181b1d5caf3SKevin Bowling static int  ixgbe_set_advertise(struct ixgbe_softc *, int);
182d381c807SPiotr Pietruszewski static int  ixgbe_get_default_advertise(struct ixgbe_softc *);
183b1d5caf3SKevin Bowling static void ixgbe_setup_vlan_hw_support(if_ctx_t);
184b1d5caf3SKevin Bowling static void ixgbe_config_gpie(struct ixgbe_softc *);
185b1d5caf3SKevin Bowling static void ixgbe_config_delay_values(struct ixgbe_softc *);
1866f37f232SEric Joyner 
1876f37f232SEric Joyner /* Sysctl handlers */
188f2c4db54SSteven Hartland static int  ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS);
189f2c4db54SSteven Hartland static int  ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS);
1908eb6488eSEric Joyner static int  ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS);
1916f37f232SEric Joyner static int  ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS);
1926f37f232SEric Joyner static int  ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS);
1936f37f232SEric Joyner static int  ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS);
1947660e4eaSKevin Bowling static int  ixgbe_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS);
195a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
196a9ca1c79SSean Bruno static int  ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS);
197a9ca1c79SSean Bruno static int  ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS);
198a9ca1c79SSean Bruno #endif
1998eb6488eSEric Joyner static int  ixgbe_sysctl_rdh_handler(SYSCTL_HANDLER_ARGS);
2008eb6488eSEric Joyner static int  ixgbe_sysctl_rdt_handler(SYSCTL_HANDLER_ARGS);
2018eb6488eSEric Joyner static int  ixgbe_sysctl_tdt_handler(SYSCTL_HANDLER_ARGS);
2028eb6488eSEric Joyner static int  ixgbe_sysctl_tdh_handler(SYSCTL_HANDLER_ARGS);
2038eb6488eSEric Joyner static int  ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS);
2046f37f232SEric Joyner static int  ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS);
2056f37f232SEric Joyner static int  ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS);
206758cc3dcSJack F Vogel 
207758cc3dcSJack F Vogel /* Deferred interrupt tasklets */
208c19c7afeSEric Joyner static void ixgbe_handle_msf(void *);
209c19c7afeSEric Joyner static void ixgbe_handle_mod(void *);
210c19c7afeSEric Joyner static void ixgbe_handle_phy(void *);
211758cc3dcSJack F Vogel 
2128eb6488eSEric Joyner /************************************************************************
213758cc3dcSJack F Vogel  *  FreeBSD Device Interface Entry Points
2148eb6488eSEric Joyner  ************************************************************************/
215a1edda90SAdrian Chadd static device_method_t ix_methods[] = {
216758cc3dcSJack F Vogel 	/* Device interface */
217c19c7afeSEric Joyner 	DEVMETHOD(device_register, ixgbe_register),
218c19c7afeSEric Joyner 	DEVMETHOD(device_probe, iflib_device_probe),
219c19c7afeSEric Joyner 	DEVMETHOD(device_attach, iflib_device_attach),
220c19c7afeSEric Joyner 	DEVMETHOD(device_detach, iflib_device_detach),
221c19c7afeSEric Joyner 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
222c19c7afeSEric Joyner 	DEVMETHOD(device_suspend, iflib_device_suspend),
223c19c7afeSEric Joyner 	DEVMETHOD(device_resume, iflib_device_resume),
22448056c88SJack F Vogel #ifdef PCI_IOV
225c19c7afeSEric Joyner 	DEVMETHOD(pci_iov_init, iflib_device_iov_init),
226c19c7afeSEric Joyner 	DEVMETHOD(pci_iov_uninit, iflib_device_iov_uninit),
227c19c7afeSEric Joyner 	DEVMETHOD(pci_iov_add_vf, iflib_device_iov_add_vf),
22848056c88SJack F Vogel #endif /* PCI_IOV */
229758cc3dcSJack F Vogel 	DEVMETHOD_END
230758cc3dcSJack F Vogel };
231758cc3dcSJack F Vogel 
232a1edda90SAdrian Chadd static driver_t ix_driver = {
233b1d5caf3SKevin Bowling 	"ix", ix_methods, sizeof(struct ixgbe_softc),
234758cc3dcSJack F Vogel };
235758cc3dcSJack F Vogel 
236964c2b3aSJohn Baldwin DRIVER_MODULE(ix, pci, ix_driver, 0, 0);
237937b0f25SYuri Pankov IFLIB_PNP_INFO(pci, ix_driver, ixgbe_vendor_info_array);
238a1edda90SAdrian Chadd MODULE_DEPEND(ix, pci, 1, 1, 1);
239a1edda90SAdrian Chadd MODULE_DEPEND(ix, ether, 1, 1, 1);
240c19c7afeSEric Joyner MODULE_DEPEND(ix, iflib, 1, 1, 1);
241c19c7afeSEric Joyner 
242c19c7afeSEric Joyner static device_method_t ixgbe_if_methods[] = {
243c19c7afeSEric Joyner 	DEVMETHOD(ifdi_attach_pre, ixgbe_if_attach_pre),
244c19c7afeSEric Joyner 	DEVMETHOD(ifdi_attach_post, ixgbe_if_attach_post),
245c19c7afeSEric Joyner 	DEVMETHOD(ifdi_detach, ixgbe_if_detach),
246c19c7afeSEric Joyner 	DEVMETHOD(ifdi_shutdown, ixgbe_if_shutdown),
247c19c7afeSEric Joyner 	DEVMETHOD(ifdi_suspend, ixgbe_if_suspend),
248c19c7afeSEric Joyner 	DEVMETHOD(ifdi_resume, ixgbe_if_resume),
249c19c7afeSEric Joyner 	DEVMETHOD(ifdi_init, ixgbe_if_init),
250c19c7afeSEric Joyner 	DEVMETHOD(ifdi_stop, ixgbe_if_stop),
251c19c7afeSEric Joyner 	DEVMETHOD(ifdi_msix_intr_assign, ixgbe_if_msix_intr_assign),
252c19c7afeSEric Joyner 	DEVMETHOD(ifdi_intr_enable, ixgbe_if_enable_intr),
253c19c7afeSEric Joyner 	DEVMETHOD(ifdi_intr_disable, ixgbe_if_disable_intr),
254b2c1e8e6SEric Joyner 	DEVMETHOD(ifdi_link_intr_enable, ixgbe_link_intr_enable),
255c19c7afeSEric Joyner 	DEVMETHOD(ifdi_tx_queue_intr_enable, ixgbe_if_rx_queue_intr_enable),
256c19c7afeSEric Joyner 	DEVMETHOD(ifdi_rx_queue_intr_enable, ixgbe_if_rx_queue_intr_enable),
257c19c7afeSEric Joyner 	DEVMETHOD(ifdi_tx_queues_alloc, ixgbe_if_tx_queues_alloc),
258c19c7afeSEric Joyner 	DEVMETHOD(ifdi_rx_queues_alloc, ixgbe_if_rx_queues_alloc),
259c19c7afeSEric Joyner 	DEVMETHOD(ifdi_queues_free, ixgbe_if_queues_free),
260c19c7afeSEric Joyner 	DEVMETHOD(ifdi_update_admin_status, ixgbe_if_update_admin_status),
261c19c7afeSEric Joyner 	DEVMETHOD(ifdi_multi_set, ixgbe_if_multi_set),
262c19c7afeSEric Joyner 	DEVMETHOD(ifdi_mtu_set, ixgbe_if_mtu_set),
263c19c7afeSEric Joyner 	DEVMETHOD(ifdi_crcstrip_set, ixgbe_if_crcstrip_set),
264c19c7afeSEric Joyner 	DEVMETHOD(ifdi_media_status, ixgbe_if_media_status),
265c19c7afeSEric Joyner 	DEVMETHOD(ifdi_media_change, ixgbe_if_media_change),
266c19c7afeSEric Joyner 	DEVMETHOD(ifdi_promisc_set, ixgbe_if_promisc_set),
267c19c7afeSEric Joyner 	DEVMETHOD(ifdi_timer, ixgbe_if_timer),
268c19c7afeSEric Joyner 	DEVMETHOD(ifdi_vlan_register, ixgbe_if_vlan_register),
269c19c7afeSEric Joyner 	DEVMETHOD(ifdi_vlan_unregister, ixgbe_if_vlan_unregister),
270c19c7afeSEric Joyner 	DEVMETHOD(ifdi_get_counter, ixgbe_if_get_counter),
271bca38080SAndrew Gallatin 	DEVMETHOD(ifdi_i2c_req, ixgbe_if_i2c_req),
272cf150917SEric Joyner 	DEVMETHOD(ifdi_needs_restart, ixgbe_if_needs_restart),
273c19c7afeSEric Joyner #ifdef PCI_IOV
274c19c7afeSEric Joyner 	DEVMETHOD(ifdi_iov_init, ixgbe_if_iov_init),
275c19c7afeSEric Joyner 	DEVMETHOD(ifdi_iov_uninit, ixgbe_if_iov_uninit),
276c19c7afeSEric Joyner 	DEVMETHOD(ifdi_iov_vf_add, ixgbe_if_iov_vf_add),
277c19c7afeSEric Joyner #endif /* PCI_IOV */
278c19c7afeSEric Joyner 	DEVMETHOD_END
279c19c7afeSEric Joyner };
280758cc3dcSJack F Vogel 
281758cc3dcSJack F Vogel /*
2828eb6488eSEric Joyner  * TUNEABLE PARAMETERS:
283758cc3dcSJack F Vogel  */
284758cc3dcSJack F Vogel 
28520b91f0aSPawel Biernacki static SYSCTL_NODE(_hw, OID_AUTO, ix, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
28620b91f0aSPawel Biernacki     "IXGBE driver parameters");
287c19c7afeSEric Joyner static driver_t ixgbe_if_driver = {
288b1d5caf3SKevin Bowling   "ixgbe_if", ixgbe_if_methods, sizeof(struct ixgbe_softc)
289c19c7afeSEric Joyner };
290758cc3dcSJack F Vogel 
291758cc3dcSJack F Vogel static int ixgbe_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
292758cc3dcSJack F Vogel SYSCTL_INT(_hw_ix, OID_AUTO, max_interrupt_rate, CTLFLAG_RDTUN,
293758cc3dcSJack F Vogel     &ixgbe_max_interrupt_rate, 0, "Maximum interrupts per second");
294758cc3dcSJack F Vogel 
295f2c4db54SSteven Hartland /* Flow control setting, default to full */
296f2c4db54SSteven Hartland static int ixgbe_flow_control = ixgbe_fc_full;
297f2c4db54SSteven Hartland SYSCTL_INT(_hw_ix, OID_AUTO, flow_control, CTLFLAG_RDTUN,
298f2c4db54SSteven Hartland     &ixgbe_flow_control, 0, "Default flow control used for all adapters");
299f2c4db54SSteven Hartland 
300f2c4db54SSteven Hartland /* Advertise Speed, default to 0 (auto) */
301f2c4db54SSteven Hartland static int ixgbe_advertise_speed = 0;
302f2c4db54SSteven Hartland SYSCTL_INT(_hw_ix, OID_AUTO, advertise_speed, CTLFLAG_RDTUN,
303f2c4db54SSteven Hartland     &ixgbe_advertise_speed, 0, "Default advertised speed for all adapters");
304f2c4db54SSteven Hartland 
305758cc3dcSJack F Vogel /*
3068eb6488eSEric Joyner  * Smart speed setting, default to on
3078eb6488eSEric Joyner  * this only works as a compile option
3088eb6488eSEric Joyner  * right now as its during attach, set
3098eb6488eSEric Joyner  * this to 'ixgbe_smart_speed_off' to
3108eb6488eSEric Joyner  * disable.
311758cc3dcSJack F Vogel  */
312758cc3dcSJack F Vogel static int ixgbe_smart_speed = ixgbe_smart_speed_on;
313758cc3dcSJack F Vogel 
314758cc3dcSJack F Vogel /*
3158eb6488eSEric Joyner  * MSI-X should be the default for best performance,
316758cc3dcSJack F Vogel  * but this allows it to be forced off for testing.
317758cc3dcSJack F Vogel  */
318758cc3dcSJack F Vogel static int ixgbe_enable_msix = 1;
319758cc3dcSJack F Vogel SYSCTL_INT(_hw_ix, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &ixgbe_enable_msix, 0,
320758cc3dcSJack F Vogel     "Enable MSI-X interrupts");
321758cc3dcSJack F Vogel 
322758cc3dcSJack F Vogel /*
3238eb6488eSEric Joyner  * Defining this on will allow the use
3248eb6488eSEric Joyner  * of unsupported SFP+ modules, note that
3258eb6488eSEric Joyner  * doing so you are on your own :)
326758cc3dcSJack F Vogel  */
32779b36ec9SKevin Bowling static int allow_unsupported_sfp = false;
328fb6aa95dSSean Bruno SYSCTL_INT(_hw_ix, OID_AUTO, unsupported_sfp, CTLFLAG_RDTUN,
3298eb6488eSEric Joyner     &allow_unsupported_sfp, 0,
3308eb6488eSEric Joyner     "Allow unsupported SFP modules...use at your own risk");
3318eb6488eSEric Joyner 
3328eb6488eSEric Joyner /*
3338eb6488eSEric Joyner  * Not sure if Flow Director is fully baked,
3348eb6488eSEric Joyner  * so we'll default to turning it off.
3358eb6488eSEric Joyner  */
3368eb6488eSEric Joyner static int ixgbe_enable_fdir = 0;
3378eb6488eSEric Joyner SYSCTL_INT(_hw_ix, OID_AUTO, enable_fdir, CTLFLAG_RDTUN, &ixgbe_enable_fdir, 0,
3388eb6488eSEric Joyner     "Enable Flow Director");
3398eb6488eSEric Joyner 
3408eb6488eSEric Joyner /* Receive-Side Scaling */
3418eb6488eSEric Joyner static int ixgbe_enable_rss = 1;
3428eb6488eSEric Joyner SYSCTL_INT(_hw_ix, OID_AUTO, enable_rss, CTLFLAG_RDTUN, &ixgbe_enable_rss, 0,
3438eb6488eSEric Joyner     "Enable Receive-Side Scaling (RSS)");
344758cc3dcSJack F Vogel 
34564881da4SSai Rajesh Tallamraju /*
34664881da4SSai Rajesh Tallamraju  * AIM: Adaptive Interrupt Moderation
34764881da4SSai Rajesh Tallamraju  * which means that the interrupt rate
34864881da4SSai Rajesh Tallamraju  * is varied over time based on the
34964881da4SSai Rajesh Tallamraju  * traffic for that interrupt vector
35064881da4SSai Rajesh Tallamraju  */
35179b36ec9SKevin Bowling static int ixgbe_enable_aim = false;
35264881da4SSai Rajesh Tallamraju SYSCTL_INT(_hw_ix, OID_AUTO, enable_aim, CTLFLAG_RWTUN, &ixgbe_enable_aim, 0,
35364881da4SSai Rajesh Tallamraju     "Enable adaptive interrupt moderation");
35464881da4SSai Rajesh Tallamraju 
355c19c7afeSEric Joyner #if 0
356758cc3dcSJack F Vogel /* Keep running tab on them for sanity check */
357758cc3dcSJack F Vogel static int ixgbe_total_ports;
358c19c7afeSEric Joyner #endif
359758cc3dcSJack F Vogel 
3608eb6488eSEric Joyner MALLOC_DEFINE(M_IXGBE, "ix", "ix driver allocations");
361758cc3dcSJack F Vogel 
362c19c7afeSEric Joyner /*
363c19c7afeSEric Joyner  * For Flow Director: this is the number of TX packets we sample
364c19c7afeSEric Joyner  * for the filter pool, this means every 20th packet will be probed.
365c19c7afeSEric Joyner  *
366c19c7afeSEric Joyner  * This feature can be disabled by setting this to 0.
367c19c7afeSEric Joyner  */
368c19c7afeSEric Joyner static int atr_sample_rate = 20;
369c19c7afeSEric Joyner 
370c19c7afeSEric Joyner extern struct if_txrx ixgbe_txrx;
371c19c7afeSEric Joyner 
372c19c7afeSEric Joyner static struct if_shared_ctx ixgbe_sctx_init = {
373c19c7afeSEric Joyner 	.isc_magic = IFLIB_MAGIC,
374c19c7afeSEric Joyner 	.isc_q_align = PAGE_SIZE,/* max(DBA_ALIGN, PAGE_SIZE) */
3757f87c040SMarius Strobl 	.isc_tx_maxsize = IXGBE_TSO_SIZE + sizeof(struct ether_vlan_header),
376c19c7afeSEric Joyner 	.isc_tx_maxsegsize = PAGE_SIZE,
3777f87c040SMarius Strobl 	.isc_tso_maxsize = IXGBE_TSO_SIZE + sizeof(struct ether_vlan_header),
3787f87c040SMarius Strobl 	.isc_tso_maxsegsize = PAGE_SIZE,
379c19c7afeSEric Joyner 	.isc_rx_maxsize = PAGE_SIZE*4,
380c19c7afeSEric Joyner 	.isc_rx_nsegments = 1,
381c19c7afeSEric Joyner 	.isc_rx_maxsegsize = PAGE_SIZE*4,
382c19c7afeSEric Joyner 	.isc_nfl = 1,
383c19c7afeSEric Joyner 	.isc_ntxqs = 1,
384c19c7afeSEric Joyner 	.isc_nrxqs = 1,
385c19c7afeSEric Joyner 
386c19c7afeSEric Joyner 	.isc_admin_intrcnt = 1,
387c19c7afeSEric Joyner 	.isc_vendor_info = ixgbe_vendor_info_array,
388c19c7afeSEric Joyner 	.isc_driver_version = ixgbe_driver_version,
389c19c7afeSEric Joyner 	.isc_driver = &ixgbe_if_driver,
3907aad1f4eSEric Joyner 	.isc_flags = IFLIB_TSO_INIT_IP,
391c19c7afeSEric Joyner 
392c19c7afeSEric Joyner 	.isc_nrxd_min = {MIN_RXD},
393c19c7afeSEric Joyner 	.isc_ntxd_min = {MIN_TXD},
394c19c7afeSEric Joyner 	.isc_nrxd_max = {MAX_RXD},
395c19c7afeSEric Joyner 	.isc_ntxd_max = {MAX_TXD},
396c19c7afeSEric Joyner 	.isc_nrxd_default = {DEFAULT_RXD},
397c19c7afeSEric Joyner 	.isc_ntxd_default = {DEFAULT_TXD},
398c19c7afeSEric Joyner };
399c19c7afeSEric Joyner 
400c19c7afeSEric Joyner /************************************************************************
401c19c7afeSEric Joyner  * ixgbe_if_tx_queues_alloc
402c19c7afeSEric Joyner  ************************************************************************/
403c19c7afeSEric Joyner static int
404c19c7afeSEric Joyner ixgbe_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
405c19c7afeSEric Joyner     int ntxqs, int ntxqsets)
406c19c7afeSEric Joyner {
407b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
408b1d5caf3SKevin Bowling 	if_softc_ctx_t     scctx = sc->shared;
409c19c7afeSEric Joyner 	struct ix_tx_queue *que;
410c19c7afeSEric Joyner 	int                i, j, error;
411c19c7afeSEric Joyner 
412b1d5caf3SKevin Bowling 	MPASS(sc->num_tx_queues > 0);
413b1d5caf3SKevin Bowling 	MPASS(sc->num_tx_queues == ntxqsets);
414c19c7afeSEric Joyner 	MPASS(ntxqs == 1);
415c19c7afeSEric Joyner 
416c19c7afeSEric Joyner 	/* Allocate queue structure memory */
417b1d5caf3SKevin Bowling 	sc->tx_queues =
418c19c7afeSEric Joyner 	    (struct ix_tx_queue *)malloc(sizeof(struct ix_tx_queue) * ntxqsets,
419c19c7afeSEric Joyner 	                                 M_IXGBE, M_NOWAIT | M_ZERO);
420b1d5caf3SKevin Bowling 	if (!sc->tx_queues) {
421c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
422c19c7afeSEric Joyner 		    "Unable to allocate TX ring memory\n");
423c19c7afeSEric Joyner 		return (ENOMEM);
424c19c7afeSEric Joyner 	}
425c19c7afeSEric Joyner 
426b1d5caf3SKevin Bowling 	for (i = 0, que = sc->tx_queues; i < ntxqsets; i++, que++) {
427c19c7afeSEric Joyner 		struct tx_ring *txr = &que->txr;
428c19c7afeSEric Joyner 
429c19c7afeSEric Joyner 		/* In case SR-IOV is enabled, align the index properly */
430b1d5caf3SKevin Bowling 		txr->me = ixgbe_vf_que_index(sc->iov_mode, sc->pool,
431c19c7afeSEric Joyner 		    i);
432c19c7afeSEric Joyner 
433b1d5caf3SKevin Bowling 		txr->sc = que->sc = sc;
434c19c7afeSEric Joyner 
435c19c7afeSEric Joyner 		/* Allocate report status array */
436c19c7afeSEric Joyner 		txr->tx_rsq = (qidx_t *)malloc(sizeof(qidx_t) * scctx->isc_ntxd[0], M_IXGBE, M_NOWAIT | M_ZERO);
437c19c7afeSEric Joyner 		if (txr->tx_rsq == NULL) {
438c19c7afeSEric Joyner 			error = ENOMEM;
439c19c7afeSEric Joyner 			goto fail;
440c19c7afeSEric Joyner 		}
441c19c7afeSEric Joyner 		for (j = 0; j < scctx->isc_ntxd[0]; j++)
442c19c7afeSEric Joyner 			txr->tx_rsq[j] = QIDX_INVALID;
443c19c7afeSEric Joyner 		/* get the virtual and physical address of the hardware queues */
444c19c7afeSEric Joyner 		txr->tail = IXGBE_TDT(txr->me);
445c19c7afeSEric Joyner 		txr->tx_base = (union ixgbe_adv_tx_desc *)vaddrs[i];
446c19c7afeSEric Joyner 		txr->tx_paddr = paddrs[i];
447c19c7afeSEric Joyner 
448c19c7afeSEric Joyner 		txr->bytes = 0;
449c19c7afeSEric Joyner 		txr->total_packets = 0;
450c19c7afeSEric Joyner 
451c19c7afeSEric Joyner 		/* Set the rate at which we sample packets */
452b1d5caf3SKevin Bowling 		if (sc->feat_en & IXGBE_FEATURE_FDIR)
453c19c7afeSEric Joyner 			txr->atr_sample = atr_sample_rate;
454c19c7afeSEric Joyner 
455c19c7afeSEric Joyner 	}
456c19c7afeSEric Joyner 
457c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "allocated for %d queues\n",
458b1d5caf3SKevin Bowling 	    sc->num_tx_queues);
459c19c7afeSEric Joyner 
460c19c7afeSEric Joyner 	return (0);
461c19c7afeSEric Joyner 
462c19c7afeSEric Joyner fail:
463c19c7afeSEric Joyner 	ixgbe_if_queues_free(ctx);
464c19c7afeSEric Joyner 
465c19c7afeSEric Joyner 	return (error);
466c19c7afeSEric Joyner } /* ixgbe_if_tx_queues_alloc */
467c19c7afeSEric Joyner 
468c19c7afeSEric Joyner /************************************************************************
469c19c7afeSEric Joyner  * ixgbe_if_rx_queues_alloc
470c19c7afeSEric Joyner  ************************************************************************/
471c19c7afeSEric Joyner static int
472c19c7afeSEric Joyner ixgbe_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
473c19c7afeSEric Joyner     int nrxqs, int nrxqsets)
474c19c7afeSEric Joyner {
475b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
476c19c7afeSEric Joyner 	struct ix_rx_queue *que;
477c19c7afeSEric Joyner 	int                i;
478c19c7afeSEric Joyner 
479b1d5caf3SKevin Bowling 	MPASS(sc->num_rx_queues > 0);
480b1d5caf3SKevin Bowling 	MPASS(sc->num_rx_queues == nrxqsets);
481c19c7afeSEric Joyner 	MPASS(nrxqs == 1);
482c19c7afeSEric Joyner 
483c19c7afeSEric Joyner 	/* Allocate queue structure memory */
484b1d5caf3SKevin Bowling 	sc->rx_queues =
485c19c7afeSEric Joyner 	    (struct ix_rx_queue *)malloc(sizeof(struct ix_rx_queue)*nrxqsets,
486c19c7afeSEric Joyner 	                                 M_IXGBE, M_NOWAIT | M_ZERO);
487b1d5caf3SKevin Bowling 	if (!sc->rx_queues) {
488c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
489c19c7afeSEric Joyner 		    "Unable to allocate TX ring memory\n");
490c19c7afeSEric Joyner 		return (ENOMEM);
491c19c7afeSEric Joyner 	}
492c19c7afeSEric Joyner 
493b1d5caf3SKevin Bowling 	for (i = 0, que = sc->rx_queues; i < nrxqsets; i++, que++) {
494c19c7afeSEric Joyner 		struct rx_ring *rxr = &que->rxr;
495c19c7afeSEric Joyner 
496c19c7afeSEric Joyner 		/* In case SR-IOV is enabled, align the index properly */
497b1d5caf3SKevin Bowling 		rxr->me = ixgbe_vf_que_index(sc->iov_mode, sc->pool,
498c19c7afeSEric Joyner 		    i);
499c19c7afeSEric Joyner 
500b1d5caf3SKevin Bowling 		rxr->sc = que->sc = sc;
501c19c7afeSEric Joyner 
502c19c7afeSEric Joyner 		/* get the virtual and physical address of the hw queues */
503c19c7afeSEric Joyner 		rxr->tail = IXGBE_RDT(rxr->me);
504c19c7afeSEric Joyner 		rxr->rx_base = (union ixgbe_adv_rx_desc *)vaddrs[i];
505c19c7afeSEric Joyner 		rxr->rx_paddr = paddrs[i];
506c19c7afeSEric Joyner 		rxr->bytes = 0;
507c19c7afeSEric Joyner 		rxr->que = que;
508c19c7afeSEric Joyner 	}
509c19c7afeSEric Joyner 
510c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "allocated for %d rx queues\n",
511b1d5caf3SKevin Bowling 	    sc->num_rx_queues);
512c19c7afeSEric Joyner 
513c19c7afeSEric Joyner 	return (0);
514c19c7afeSEric Joyner } /* ixgbe_if_rx_queues_alloc */
515c19c7afeSEric Joyner 
516c19c7afeSEric Joyner /************************************************************************
517c19c7afeSEric Joyner  * ixgbe_if_queues_free
518c19c7afeSEric Joyner  ************************************************************************/
519c19c7afeSEric Joyner static void
520c19c7afeSEric Joyner ixgbe_if_queues_free(if_ctx_t ctx)
521c19c7afeSEric Joyner {
522b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
523b1d5caf3SKevin Bowling 	struct ix_tx_queue *tx_que = sc->tx_queues;
524b1d5caf3SKevin Bowling 	struct ix_rx_queue *rx_que = sc->rx_queues;
525c19c7afeSEric Joyner 	int                i;
526c19c7afeSEric Joyner 
527c19c7afeSEric Joyner 	if (tx_que != NULL) {
528b1d5caf3SKevin Bowling 		for (i = 0; i < sc->num_tx_queues; i++, tx_que++) {
529c19c7afeSEric Joyner 			struct tx_ring *txr = &tx_que->txr;
530c19c7afeSEric Joyner 			if (txr->tx_rsq == NULL)
531c19c7afeSEric Joyner 				break;
532c19c7afeSEric Joyner 
533c19c7afeSEric Joyner 			free(txr->tx_rsq, M_IXGBE);
534c19c7afeSEric Joyner 			txr->tx_rsq = NULL;
535c19c7afeSEric Joyner 		}
536c19c7afeSEric Joyner 
537b1d5caf3SKevin Bowling 		free(sc->tx_queues, M_IXGBE);
538b1d5caf3SKevin Bowling 		sc->tx_queues = NULL;
539c19c7afeSEric Joyner 	}
540c19c7afeSEric Joyner 	if (rx_que != NULL) {
541b1d5caf3SKevin Bowling 		free(sc->rx_queues, M_IXGBE);
542b1d5caf3SKevin Bowling 		sc->rx_queues = NULL;
543c19c7afeSEric Joyner 	}
544c19c7afeSEric Joyner } /* ixgbe_if_queues_free */
545c19c7afeSEric Joyner 
5468eb6488eSEric Joyner /************************************************************************
5478eb6488eSEric Joyner  * ixgbe_initialize_rss_mapping
5488eb6488eSEric Joyner  ************************************************************************/
5498eb6488eSEric Joyner static void
550b1d5caf3SKevin Bowling ixgbe_initialize_rss_mapping(struct ixgbe_softc *sc)
551758cc3dcSJack F Vogel {
552b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
5538eb6488eSEric Joyner 	u32             reta = 0, mrqc, rss_key[10];
5548eb6488eSEric Joyner 	int             queue_id, table_size, index_mult;
5558eb6488eSEric Joyner 	int             i, j;
5568eb6488eSEric Joyner 	u32             rss_hash_config;
5576f37f232SEric Joyner 
558b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_RSS) {
5598eb6488eSEric Joyner 		/* Fetch the configured RSS key */
5608eb6488eSEric Joyner 		rss_getkey((uint8_t *)&rss_key);
561758cc3dcSJack F Vogel 	} else {
5628eb6488eSEric Joyner 		/* set up random bits */
5638eb6488eSEric Joyner 		arc4rand(&rss_key, sizeof(rss_key), 0);
564758cc3dcSJack F Vogel 	}
565758cc3dcSJack F Vogel 
5668eb6488eSEric Joyner 	/* Set multiplier for RETA setup and table size based on MAC */
5678eb6488eSEric Joyner 	index_mult = 0x1;
5688eb6488eSEric Joyner 	table_size = 128;
569b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
5708eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
5718eb6488eSEric Joyner 		index_mult = 0x11;
572758cc3dcSJack F Vogel 		break;
5738eb6488eSEric Joyner 	case ixgbe_mac_X550:
5748eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
5758eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
5768eb6488eSEric Joyner 		table_size = 512;
5778eb6488eSEric Joyner 		break;
578758cc3dcSJack F Vogel 	default:
579758cc3dcSJack F Vogel 		break;
580758cc3dcSJack F Vogel 	}
581758cc3dcSJack F Vogel 
5828eb6488eSEric Joyner 	/* Set up the redirection table */
5838eb6488eSEric Joyner 	for (i = 0, j = 0; i < table_size; i++, j++) {
584b1d5caf3SKevin Bowling 		if (j == sc->num_rx_queues)
5858eb6488eSEric Joyner 			j = 0;
586758cc3dcSJack F Vogel 
587b1d5caf3SKevin Bowling 		if (sc->feat_en & IXGBE_FEATURE_RSS) {
588a9ca1c79SSean Bruno 			/*
5898eb6488eSEric Joyner 			 * Fetch the RSS bucket id for the given indirection
5908eb6488eSEric Joyner 			 * entry. Cap it at the number of configured buckets
591c19c7afeSEric Joyner 			 * (which is num_rx_queues.)
592a9ca1c79SSean Bruno 			 */
5938eb6488eSEric Joyner 			queue_id = rss_get_indirection_to_bucket(i);
594b1d5caf3SKevin Bowling 			queue_id = queue_id % sc->num_rx_queues;
595758cc3dcSJack F Vogel 		} else
5968eb6488eSEric Joyner 			queue_id = (j * index_mult);
5978eb6488eSEric Joyner 
5988eb6488eSEric Joyner 		/*
5998eb6488eSEric Joyner 		 * The low 8 bits are for hash value (n+0);
6008eb6488eSEric Joyner 		 * The next 8 bits are for hash value (n+1), etc.
6018eb6488eSEric Joyner 		 */
6028eb6488eSEric Joyner 		reta = reta >> 8;
6038eb6488eSEric Joyner 		reta = reta | (((uint32_t)queue_id) << 24);
6048eb6488eSEric Joyner 		if ((i & 3) == 3) {
6058eb6488eSEric Joyner 			if (i < 128)
6068eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
6078eb6488eSEric Joyner 			else
6088eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_ERETA((i >> 2) - 32),
6098eb6488eSEric Joyner 				    reta);
6108eb6488eSEric Joyner 			reta = 0;
6118eb6488eSEric Joyner 		}
612758cc3dcSJack F Vogel 	}
613758cc3dcSJack F Vogel 
6148eb6488eSEric Joyner 	/* Now fill our hash function seeds */
6158eb6488eSEric Joyner 	for (i = 0; i < 10; i++)
6168eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
617758cc3dcSJack F Vogel 
6188eb6488eSEric Joyner 	/* Perform hash on these packet types */
619b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_RSS)
6208eb6488eSEric Joyner 		rss_hash_config = rss_gethashconfig();
621758cc3dcSJack F Vogel 	else {
622758cc3dcSJack F Vogel 		/*
6238eb6488eSEric Joyner 		 * Disable UDP - IP fragments aren't currently being handled
6248eb6488eSEric Joyner 		 * and so we end up with a mix of 2-tuple and 4-tuple
6258eb6488eSEric Joyner 		 * traffic.
626758cc3dcSJack F Vogel 		 */
6278eb6488eSEric Joyner 		rss_hash_config = RSS_HASHTYPE_RSS_IPV4
6288eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_TCP_IPV4
6298eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_IPV6
6308eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_TCP_IPV6
6318eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_IPV6_EX
6328eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_TCP_IPV6_EX;
633758cc3dcSJack F Vogel 	}
634758cc3dcSJack F Vogel 
6358eb6488eSEric Joyner 	mrqc = IXGBE_MRQC_RSSEN;
6368eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
6378eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
6388eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
6398eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
6408eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
6418eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
6428eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
6438eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
6448eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
6458eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
6468eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
6478eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
6488eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
6498eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
6508eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
6518eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
6528eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
6538eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
654b1d5caf3SKevin Bowling 	mrqc |= ixgbe_get_mrqc(sc->iov_mode);
6558eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
6568eb6488eSEric Joyner } /* ixgbe_initialize_rss_mapping */
657758cc3dcSJack F Vogel 
6588eb6488eSEric Joyner /************************************************************************
6598eb6488eSEric Joyner  * ixgbe_initialize_receive_units - Setup receive registers and features.
6608eb6488eSEric Joyner  ************************************************************************/
6618eb6488eSEric Joyner #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
662758cc3dcSJack F Vogel 
663758cc3dcSJack F Vogel static void
664c19c7afeSEric Joyner ixgbe_initialize_receive_units(if_ctx_t ctx)
665758cc3dcSJack F Vogel {
666b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
667b1d5caf3SKevin Bowling 	if_softc_ctx_t     scctx = sc->shared;
668b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
669ff06a8dbSJustin Hibbits 	if_t               ifp = iflib_get_ifp(ctx);
670c19c7afeSEric Joyner 	struct ix_rx_queue *que;
6718eb6488eSEric Joyner 	int                i, j;
6728eb6488eSEric Joyner 	u32                bufsz, fctrl, srrctl, rxcsum;
6738eb6488eSEric Joyner 	u32                hlreg;
67448056c88SJack F Vogel 
67548056c88SJack F Vogel 	/*
6768eb6488eSEric Joyner 	 * Make sure receives are disabled while
6778eb6488eSEric Joyner 	 * setting up the descriptor ring
67848056c88SJack F Vogel 	 */
6798eb6488eSEric Joyner 	ixgbe_disable_rx(hw);
6808eb6488eSEric Joyner 
6818eb6488eSEric Joyner 	/* Enable broadcasts */
6828eb6488eSEric Joyner 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
6838eb6488eSEric Joyner 	fctrl |= IXGBE_FCTRL_BAM;
684b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
6858eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_DPF;
6868eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_PMCF;
68748056c88SJack F Vogel 	}
6888eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
6898eb6488eSEric Joyner 
6908eb6488eSEric Joyner 	/* Set for Jumbo Frames? */
6918eb6488eSEric Joyner 	hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
692ff06a8dbSJustin Hibbits 	if (if_getmtu(ifp) > ETHERMTU)
6938eb6488eSEric Joyner 		hlreg |= IXGBE_HLREG0_JUMBOEN;
6948eb6488eSEric Joyner 	else
6958eb6488eSEric Joyner 		hlreg &= ~IXGBE_HLREG0_JUMBOEN;
6968eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
6978eb6488eSEric Joyner 
698b1d5caf3SKevin Bowling 	bufsz = (sc->rx_mbuf_sz + BSIZEPKT_ROUNDUP) >>
6998eb6488eSEric Joyner 	    IXGBE_SRRCTL_BSIZEPKT_SHIFT;
7008eb6488eSEric Joyner 
701c19c7afeSEric Joyner 	/* Setup the Base and Length of the Rx Descriptor Ring */
702b1d5caf3SKevin Bowling 	for (i = 0, que = sc->rx_queues; i < sc->num_rx_queues; i++, que++) {
703c19c7afeSEric Joyner 		struct rx_ring *rxr = &que->rxr;
704c19c7afeSEric Joyner 		u64            rdba = rxr->rx_paddr;
705c19c7afeSEric Joyner 
7068eb6488eSEric Joyner 		j = rxr->me;
7078eb6488eSEric Joyner 
7088eb6488eSEric Joyner 		/* Setup the Base and Length of the Rx Descriptor Ring */
7098eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j),
7108eb6488eSEric Joyner 		    (rdba & 0x00000000ffffffffULL));
7118eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
7128eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j),
713c19c7afeSEric Joyner 		     scctx->isc_nrxd[0] * sizeof(union ixgbe_adv_rx_desc));
7148eb6488eSEric Joyner 
7158eb6488eSEric Joyner 		/* Set up the SRRCTL register */
7168eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(j));
7178eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
7188eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
7198eb6488eSEric Joyner 		srrctl |= bufsz;
7208eb6488eSEric Joyner 		srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
72148056c88SJack F Vogel 
72248056c88SJack F Vogel 		/*
7238eb6488eSEric Joyner 		 * Set DROP_EN iff we have no flow control and >1 queue.
7248eb6488eSEric Joyner 		 * Note that srrctl was cleared shortly before during reset,
7258eb6488eSEric Joyner 		 * so we do not need to clear the bit, but do it just in case
7268eb6488eSEric Joyner 		 * this code is moved elsewhere.
72748056c88SJack F Vogel 		 */
728b1d5caf3SKevin Bowling 		if (sc->num_rx_queues > 1 &&
729b1d5caf3SKevin Bowling 		    sc->hw.fc.requested_mode == ixgbe_fc_none) {
7308eb6488eSEric Joyner 			srrctl |= IXGBE_SRRCTL_DROP_EN;
731758cc3dcSJack F Vogel 		} else {
7328eb6488eSEric Joyner 			srrctl &= ~IXGBE_SRRCTL_DROP_EN;
733758cc3dcSJack F Vogel 		}
734758cc3dcSJack F Vogel 
7358eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(j), srrctl);
736758cc3dcSJack F Vogel 
7378eb6488eSEric Joyner 		/* Setup the HW Rx Head and Tail Descriptor Pointers */
7388eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
7398eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
740758cc3dcSJack F Vogel 
7418eb6488eSEric Joyner 		/* Set the driver rx tail address */
7428eb6488eSEric Joyner 		rxr->tail =  IXGBE_RDT(rxr->me);
743a9ca1c79SSean Bruno 	}
744758cc3dcSJack F Vogel 
745b1d5caf3SKevin Bowling 	if (sc->hw.mac.type != ixgbe_mac_82598EB) {
7468eb6488eSEric Joyner 		u32 psrtype = IXGBE_PSRTYPE_TCPHDR
7478eb6488eSEric Joyner 		            | IXGBE_PSRTYPE_UDPHDR
7488eb6488eSEric Joyner 		            | IXGBE_PSRTYPE_IPV4HDR
7498eb6488eSEric Joyner 		            | IXGBE_PSRTYPE_IPV6HDR;
7508eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
7516f37f232SEric Joyner 	}
7526f37f232SEric Joyner 
7538eb6488eSEric Joyner 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
7548eb6488eSEric Joyner 
755b1d5caf3SKevin Bowling 	ixgbe_initialize_rss_mapping(sc);
7568eb6488eSEric Joyner 
757156424fcSPrzemyslaw Lewandowski 	if (sc->feat_en & IXGBE_FEATURE_RSS) {
7588eb6488eSEric Joyner 		/* RSS and RX IPP Checksum are mutually exclusive */
7598eb6488eSEric Joyner 		rxcsum |= IXGBE_RXCSUM_PCSD;
7606f37f232SEric Joyner 	}
7616f37f232SEric Joyner 
762ff06a8dbSJustin Hibbits 	if (if_getcapenable(ifp) & IFCAP_RXCSUM)
7638eb6488eSEric Joyner 		rxcsum |= IXGBE_RXCSUM_PCSD;
764758cc3dcSJack F Vogel 
7658eb6488eSEric Joyner 	/* This is useful for calculating UDP/IP fragment checksums */
7668eb6488eSEric Joyner 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
7678eb6488eSEric Joyner 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
768758cc3dcSJack F Vogel 
7698eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
770758cc3dcSJack F Vogel 
7718eb6488eSEric Joyner } /* ixgbe_initialize_receive_units */
772758cc3dcSJack F Vogel 
7738eb6488eSEric Joyner /************************************************************************
7748eb6488eSEric Joyner  * ixgbe_initialize_transmit_units - Enable transmit units.
7758eb6488eSEric Joyner  ************************************************************************/
776758cc3dcSJack F Vogel static void
777c19c7afeSEric Joyner ixgbe_initialize_transmit_units(if_ctx_t ctx)
778758cc3dcSJack F Vogel {
779b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
780b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
781b1d5caf3SKevin Bowling 	if_softc_ctx_t     scctx = sc->shared;
782c19c7afeSEric Joyner 	struct ix_tx_queue *que;
783c19c7afeSEric Joyner 	int i;
784758cc3dcSJack F Vogel 
785758cc3dcSJack F Vogel 	/* Setup the Base and Length of the Tx Descriptor Ring */
786b1d5caf3SKevin Bowling 	for (i = 0, que = sc->tx_queues; i < sc->num_tx_queues;
787c19c7afeSEric Joyner 	    i++, que++) {
788c19c7afeSEric Joyner 		struct tx_ring	   *txr = &que->txr;
789c19c7afeSEric Joyner 		u64 tdba = txr->tx_paddr;
790758cc3dcSJack F Vogel 		u32 txctrl = 0;
79148056c88SJack F Vogel 		int j = txr->me;
792758cc3dcSJack F Vogel 
79348056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
794758cc3dcSJack F Vogel 		    (tdba & 0x00000000ffffffffULL));
79548056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
79648056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j),
797c19c7afeSEric Joyner 		    scctx->isc_ntxd[0] * sizeof(union ixgbe_adv_tx_desc));
798758cc3dcSJack F Vogel 
799758cc3dcSJack F Vogel 		/* Setup the HW Tx Head and Tail descriptor pointers */
80048056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
80148056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
802758cc3dcSJack F Vogel 
803758cc3dcSJack F Vogel 		/* Cache the tail address */
8042dc2d580SEric Joyner 		txr->tail = IXGBE_TDT(txr->me);
8052dc2d580SEric Joyner 
806088a0b27SEric Joyner 		txr->tx_rs_cidx = txr->tx_rs_pidx;
807088a0b27SEric Joyner 		txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
808c19c7afeSEric Joyner 		for (int k = 0; k < scctx->isc_ntxd[0]; k++)
809c19c7afeSEric Joyner 			txr->tx_rsq[k] = QIDX_INVALID;
810758cc3dcSJack F Vogel 
811758cc3dcSJack F Vogel 		/* Disable Head Writeback */
812a9ca1c79SSean Bruno 		/*
813a9ca1c79SSean Bruno 		 * Note: for X550 series devices, these registers are actually
814a9ca1c79SSean Bruno 		 * prefixed with TPH_ isntead of DCA_, but the addresses and
815a9ca1c79SSean Bruno 		 * fields remain the same.
816a9ca1c79SSean Bruno 		 */
817758cc3dcSJack F Vogel 		switch (hw->mac.type) {
818758cc3dcSJack F Vogel 		case ixgbe_mac_82598EB:
81948056c88SJack F Vogel 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
820758cc3dcSJack F Vogel 			break;
821758cc3dcSJack F Vogel 		default:
82248056c88SJack F Vogel 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(j));
823758cc3dcSJack F Vogel 			break;
824758cc3dcSJack F Vogel 		}
825758cc3dcSJack F Vogel 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
826758cc3dcSJack F Vogel 		switch (hw->mac.type) {
827758cc3dcSJack F Vogel 		case ixgbe_mac_82598EB:
82848056c88SJack F Vogel 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
829758cc3dcSJack F Vogel 			break;
830758cc3dcSJack F Vogel 		default:
83148056c88SJack F Vogel 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(j), txctrl);
832758cc3dcSJack F Vogel 			break;
833758cc3dcSJack F Vogel 		}
834758cc3dcSJack F Vogel 
835758cc3dcSJack F Vogel 	}
836758cc3dcSJack F Vogel 
837758cc3dcSJack F Vogel 	if (hw->mac.type != ixgbe_mac_82598EB) {
838758cc3dcSJack F Vogel 		u32 dmatxctl, rttdcs;
8398eb6488eSEric Joyner 
840758cc3dcSJack F Vogel 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
841758cc3dcSJack F Vogel 		dmatxctl |= IXGBE_DMATXCTL_TE;
842758cc3dcSJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
843758cc3dcSJack F Vogel 		/* Disable arbiter to set MTQC */
844758cc3dcSJack F Vogel 		rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
845758cc3dcSJack F Vogel 		rttdcs |= IXGBE_RTTDCS_ARBDIS;
846758cc3dcSJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
8478eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_MTQC,
848b1d5caf3SKevin Bowling 		    ixgbe_get_mtqc(sc->iov_mode));
849758cc3dcSJack F Vogel 		rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
850758cc3dcSJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
851758cc3dcSJack F Vogel 	}
852758cc3dcSJack F Vogel 
8538eb6488eSEric Joyner } /* ixgbe_initialize_transmit_units */
854758cc3dcSJack F Vogel 
8558eb6488eSEric Joyner /************************************************************************
856c19c7afeSEric Joyner  * ixgbe_register
857c19c7afeSEric Joyner  ************************************************************************/
858c19c7afeSEric Joyner static void *
859c19c7afeSEric Joyner ixgbe_register(device_t dev)
860c19c7afeSEric Joyner {
861ffe3def9SMark Johnston 	return (&ixgbe_sctx_init);
862c19c7afeSEric Joyner } /* ixgbe_register */
863c19c7afeSEric Joyner 
864c19c7afeSEric Joyner /************************************************************************
865c19c7afeSEric Joyner  * ixgbe_if_attach_pre - Device initialization routine, part 1
866758cc3dcSJack F Vogel  *
8678eb6488eSEric Joyner  *   Called when the driver is being loaded.
868c19c7afeSEric Joyner  *   Identifies the type of hardware, initializes the hardware,
869c19c7afeSEric Joyner  *   and initializes iflib structures.
870758cc3dcSJack F Vogel  *
8718eb6488eSEric Joyner  *   return 0 on success, positive on failure
8728eb6488eSEric Joyner  ************************************************************************/
8738eb6488eSEric Joyner static int
874c19c7afeSEric Joyner ixgbe_if_attach_pre(if_ctx_t ctx)
875758cc3dcSJack F Vogel {
876b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc;
877c19c7afeSEric Joyner 	device_t        dev;
878c19c7afeSEric Joyner 	if_softc_ctx_t  scctx;
8798eb6488eSEric Joyner 	struct ixgbe_hw *hw;
8808eb6488eSEric Joyner 	int             error = 0;
8818eb6488eSEric Joyner 	u32             ctrl_ext;
8828eb6488eSEric Joyner 
8838eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_attach: begin");
8848eb6488eSEric Joyner 
8858eb6488eSEric Joyner 	/* Allocate, clear, and link in our adapter structure */
886c19c7afeSEric Joyner 	dev = iflib_get_dev(ctx);
887b1d5caf3SKevin Bowling 	sc = iflib_get_softc(ctx);
888b1d5caf3SKevin Bowling 	sc->hw.back = sc;
889b1d5caf3SKevin Bowling 	sc->ctx = ctx;
890b1d5caf3SKevin Bowling 	sc->dev = dev;
891b1d5caf3SKevin Bowling 	scctx = sc->shared = iflib_get_softc_ctx(ctx);
892b1d5caf3SKevin Bowling 	sc->media = iflib_get_media(ctx);
893b1d5caf3SKevin Bowling 	hw = &sc->hw;
8948eb6488eSEric Joyner 
8958eb6488eSEric Joyner 	/* Determine hardware revision */
8968eb6488eSEric Joyner 	hw->vendor_id = pci_get_vendor(dev);
8978eb6488eSEric Joyner 	hw->device_id = pci_get_device(dev);
8988eb6488eSEric Joyner 	hw->revision_id = pci_get_revid(dev);
8998eb6488eSEric Joyner 	hw->subsystem_vendor_id = pci_get_subvendor(dev);
9008eb6488eSEric Joyner 	hw->subsystem_device_id = pci_get_subdevice(dev);
901758cc3dcSJack F Vogel 
9028eb6488eSEric Joyner 	/* Do base PCI setup - map BAR0 */
903c19c7afeSEric Joyner 	if (ixgbe_allocate_pci_resources(ctx)) {
9048eb6488eSEric Joyner 		device_printf(dev, "Allocation of PCI resources failed\n");
905c19c7afeSEric Joyner 		return (ENXIO);
9066f37f232SEric Joyner 	}
907758cc3dcSJack F Vogel 
9088eb6488eSEric Joyner 	/* let hardware know driver is loaded */
9098eb6488eSEric Joyner 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
9108eb6488eSEric Joyner 	ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
9118eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
912758cc3dcSJack F Vogel 
913758cc3dcSJack F Vogel 	/*
9148eb6488eSEric Joyner 	 * Initialize the shared code
915758cc3dcSJack F Vogel 	 */
916c19c7afeSEric Joyner 	if (ixgbe_init_shared_code(hw) != 0) {
9178eb6488eSEric Joyner 		device_printf(dev, "Unable to initialize the shared code\n");
9188eb6488eSEric Joyner 		error = ENXIO;
919c19c7afeSEric Joyner 		goto err_pci;
92030126537SJack F Vogel 	}
921758cc3dcSJack F Vogel 
9228eb6488eSEric Joyner 	if (hw->mbx.ops.init_params)
9238eb6488eSEric Joyner 		hw->mbx.ops.init_params(hw);
924758cc3dcSJack F Vogel 
9258eb6488eSEric Joyner 	hw->allow_unsupported_sfp = allow_unsupported_sfp;
926758cc3dcSJack F Vogel 
927c19c7afeSEric Joyner 	if (hw->mac.type != ixgbe_mac_82598EB)
9288eb6488eSEric Joyner 		hw->phy.smart_speed = ixgbe_smart_speed;
9298eb6488eSEric Joyner 
930b1d5caf3SKevin Bowling 	ixgbe_init_device_features(sc);
9318eb6488eSEric Joyner 
9328eb6488eSEric Joyner 	/* Enable WoL (if supported) */
933b1d5caf3SKevin Bowling 	ixgbe_check_wol_support(sc);
9348eb6488eSEric Joyner 
9358eb6488eSEric Joyner 	/* Verify adapter fan is still functional (if applicable) */
936b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL) {
9378eb6488eSEric Joyner 		u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
938b1d5caf3SKevin Bowling 		ixgbe_check_fan_failure(sc, esdp, false);
9398eb6488eSEric Joyner 	}
9408eb6488eSEric Joyner 
9418eb6488eSEric Joyner 	/* Ensure SW/FW semaphore is free */
9428eb6488eSEric Joyner 	ixgbe_init_swfw_semaphore(hw);
9438eb6488eSEric Joyner 
9448eb6488eSEric Joyner 	/* Set an initial default flow control value */
9458eb6488eSEric Joyner 	hw->fc.requested_mode = ixgbe_flow_control;
9468eb6488eSEric Joyner 
94779b36ec9SKevin Bowling 	hw->phy.reset_if_overtemp = true;
9488eb6488eSEric Joyner 	error = ixgbe_reset_hw(hw);
94979b36ec9SKevin Bowling 	hw->phy.reset_if_overtemp = false;
9508eb6488eSEric Joyner 	if (error == IXGBE_ERR_SFP_NOT_PRESENT) {
951758cc3dcSJack F Vogel 		/*
9528eb6488eSEric Joyner 		 * No optics in this port, set up
9538eb6488eSEric Joyner 		 * so the timer routine will probe
9548eb6488eSEric Joyner 		 * for later insertion.
955758cc3dcSJack F Vogel 		 */
956b1d5caf3SKevin Bowling 		sc->sfp_probe = true;
957c19c7afeSEric Joyner 		error = 0;
9588eb6488eSEric Joyner 	} else if (error == IXGBE_ERR_SFP_NOT_SUPPORTED) {
9598eb6488eSEric Joyner 		device_printf(dev, "Unsupported SFP+ module detected!\n");
9608eb6488eSEric Joyner 		error = EIO;
961c19c7afeSEric Joyner 		goto err_pci;
9628eb6488eSEric Joyner 	} else if (error) {
9638eb6488eSEric Joyner 		device_printf(dev, "Hardware initialization failed\n");
9648eb6488eSEric Joyner 		error = EIO;
965c19c7afeSEric Joyner 		goto err_pci;
96697f9586eSSean Bruno 	}
96797f9586eSSean Bruno 
9688eb6488eSEric Joyner 	/* Make sure we have a good EEPROM before we read from it */
969b1d5caf3SKevin Bowling 	if (ixgbe_validate_eeprom_checksum(&sc->hw, NULL) < 0) {
9708eb6488eSEric Joyner 		device_printf(dev, "The EEPROM Checksum Is Not Valid\n");
9718eb6488eSEric Joyner 		error = EIO;
972c19c7afeSEric Joyner 		goto err_pci;
97397f9586eSSean Bruno 	}
97497f9586eSSean Bruno 
9758eb6488eSEric Joyner 	error = ixgbe_start_hw(hw);
9768eb6488eSEric Joyner 	switch (error) {
9778eb6488eSEric Joyner 	case IXGBE_ERR_EEPROM_VERSION:
9788eb6488eSEric Joyner 		device_printf(dev, "This device is a pre-production adapter/LOM.  Please be aware there may be issues associated with your hardware.\nIf you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
97997f9586eSSean Bruno 		break;
9808eb6488eSEric Joyner 	case IXGBE_ERR_SFP_NOT_SUPPORTED:
9818eb6488eSEric Joyner 		device_printf(dev, "Unsupported SFP+ Module\n");
9828eb6488eSEric Joyner 		error = EIO;
983c19c7afeSEric Joyner 		goto err_pci;
9848eb6488eSEric Joyner 	case IXGBE_ERR_SFP_NOT_PRESENT:
9858eb6488eSEric Joyner 		device_printf(dev, "No SFP+ Module found\n");
9868eb6488eSEric Joyner 		/* falls thru */
98797f9586eSSean Bruno 	default:
98897f9586eSSean Bruno 		break;
98997f9586eSSean Bruno 	}
99097f9586eSSean Bruno 
991c19c7afeSEric Joyner 	/* Most of the iflib initialization... */
992c19c7afeSEric Joyner 
993c19c7afeSEric Joyner 	iflib_set_mac(ctx, hw->mac.addr);
994b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
995c19c7afeSEric Joyner 	case ixgbe_mac_X550:
996c19c7afeSEric Joyner 	case ixgbe_mac_X550EM_x:
997c19c7afeSEric Joyner 	case ixgbe_mac_X550EM_a:
998c19c7afeSEric Joyner 		scctx->isc_rss_table_size = 512;
999c19c7afeSEric Joyner 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 64;
1000c19c7afeSEric Joyner 		break;
1001c19c7afeSEric Joyner 	default:
1002c19c7afeSEric Joyner 		scctx->isc_rss_table_size = 128;
1003c19c7afeSEric Joyner 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 16;
1004c19c7afeSEric Joyner 	}
1005c19c7afeSEric Joyner 
1006c19c7afeSEric Joyner 	/* Allow legacy interrupts */
1007c19c7afeSEric Joyner 	ixgbe_txrx.ift_legacy_intr = ixgbe_intr;
1008c19c7afeSEric Joyner 
1009c19c7afeSEric Joyner 	scctx->isc_txqsizes[0] =
1010c19c7afeSEric Joyner 	    roundup2(scctx->isc_ntxd[0] * sizeof(union ixgbe_adv_tx_desc) +
1011c19c7afeSEric Joyner 	    sizeof(u32), DBA_ALIGN),
1012c19c7afeSEric Joyner 	scctx->isc_rxqsizes[0] =
1013c19c7afeSEric Joyner 	    roundup2(scctx->isc_nrxd[0] * sizeof(union ixgbe_adv_rx_desc),
1014c19c7afeSEric Joyner 	    DBA_ALIGN);
1015c19c7afeSEric Joyner 
1016c19c7afeSEric Joyner 	/* XXX */
1017c19c7afeSEric Joyner 	scctx->isc_tx_csum_flags = CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_TSO |
1018c19c7afeSEric Joyner 	    CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_TSO;
1019b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
1020c19c7afeSEric Joyner 		scctx->isc_tx_nsegments = IXGBE_82598_SCATTER;
1021c19c7afeSEric Joyner 	} else {
1022c19c7afeSEric Joyner 		scctx->isc_tx_csum_flags |= CSUM_SCTP |CSUM_IP6_SCTP;
1023c19c7afeSEric Joyner 		scctx->isc_tx_nsegments = IXGBE_82599_SCATTER;
1024c19c7afeSEric Joyner 	}
1025749597dcSEric Joyner 
1026749597dcSEric Joyner 	scctx->isc_msix_bar = pci_msix_table_bar(dev);
1027749597dcSEric Joyner 
1028c19c7afeSEric Joyner 	scctx->isc_tx_tso_segments_max = scctx->isc_tx_nsegments;
1029c19c7afeSEric Joyner 	scctx->isc_tx_tso_size_max = IXGBE_TSO_SIZE;
1030c19c7afeSEric Joyner 	scctx->isc_tx_tso_segsize_max = PAGE_SIZE;
1031c19c7afeSEric Joyner 
1032c19c7afeSEric Joyner 	scctx->isc_txrx = &ixgbe_txrx;
1033c19c7afeSEric Joyner 
10347f87c040SMarius Strobl 	scctx->isc_capabilities = scctx->isc_capenable = IXGBE_CAPS;
1035c19c7afeSEric Joyner 
1036c19c7afeSEric Joyner 	return (0);
1037c19c7afeSEric Joyner 
1038c19c7afeSEric Joyner err_pci:
1039b1d5caf3SKevin Bowling 	ctrl_ext = IXGBE_READ_REG(&sc->hw, IXGBE_CTRL_EXT);
1040c19c7afeSEric Joyner 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
1041b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_CTRL_EXT, ctrl_ext);
1042c19c7afeSEric Joyner 	ixgbe_free_pci_resources(ctx);
1043c19c7afeSEric Joyner 
1044c19c7afeSEric Joyner 	return (error);
1045c19c7afeSEric Joyner } /* ixgbe_if_attach_pre */
1046c19c7afeSEric Joyner 
1047c19c7afeSEric Joyner  /*********************************************************************
1048c19c7afeSEric Joyner  * ixgbe_if_attach_post - Device initialization routine, part 2
1049c19c7afeSEric Joyner  *
1050c19c7afeSEric Joyner  *   Called during driver load, but after interrupts and
1051c19c7afeSEric Joyner  *   resources have been allocated and configured.
1052c19c7afeSEric Joyner  *   Sets up some data structures not relevant to iflib.
1053c19c7afeSEric Joyner  *
1054c19c7afeSEric Joyner  *   return 0 on success, positive on failure
1055c19c7afeSEric Joyner  *********************************************************************/
1056c19c7afeSEric Joyner static int
1057c19c7afeSEric Joyner ixgbe_if_attach_post(if_ctx_t ctx)
1058c19c7afeSEric Joyner {
1059c19c7afeSEric Joyner 	device_t dev;
1060b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc;
1061c19c7afeSEric Joyner 	struct ixgbe_hw *hw;
1062c19c7afeSEric Joyner 	int             error = 0;
1063c19c7afeSEric Joyner 
1064c19c7afeSEric Joyner 	dev = iflib_get_dev(ctx);
1065b1d5caf3SKevin Bowling 	sc = iflib_get_softc(ctx);
1066b1d5caf3SKevin Bowling 	hw = &sc->hw;
1067c19c7afeSEric Joyner 
1068c19c7afeSEric Joyner 
1069b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_LEGACY &&
1070b1d5caf3SKevin Bowling 		(sc->feat_cap & IXGBE_FEATURE_LEGACY_IRQ) == 0) {
1071c19c7afeSEric Joyner 		device_printf(dev, "Device does not support legacy interrupts");
1072c19c7afeSEric Joyner 		error = ENXIO;
1073c19c7afeSEric Joyner 		goto err;
1074c19c7afeSEric Joyner 	}
1075c19c7afeSEric Joyner 
1076c19c7afeSEric Joyner 	/* Allocate multicast array memory. */
1077b1d5caf3SKevin Bowling 	sc->mta = malloc(sizeof(*sc->mta) *
1078c19c7afeSEric Joyner 	                      MAX_NUM_MULTICAST_ADDRESSES, M_IXGBE, M_NOWAIT);
1079b1d5caf3SKevin Bowling 	if (sc->mta == NULL) {
1080c19c7afeSEric Joyner 		device_printf(dev, "Can not allocate multicast setup array\n");
1081c19c7afeSEric Joyner 		error = ENOMEM;
1082c19c7afeSEric Joyner 		goto err;
1083c19c7afeSEric Joyner 	}
1084c19c7afeSEric Joyner 
1085c19c7afeSEric Joyner 	/* hw.ix defaults init */
1086b1d5caf3SKevin Bowling 	ixgbe_set_advertise(sc, ixgbe_advertise_speed);
1087c19c7afeSEric Joyner 
10888eb6488eSEric Joyner 	/* Enable the optics for 82599 SFP+ fiber */
10898eb6488eSEric Joyner 	ixgbe_enable_tx_laser(hw);
1090758cc3dcSJack F Vogel 
10918eb6488eSEric Joyner 	/* Enable power to the phy. */
109279b36ec9SKevin Bowling 	ixgbe_set_phy_power(hw, true);
10938eb6488eSEric Joyner 
1094b1d5caf3SKevin Bowling 	ixgbe_initialize_iov(sc);
1095c19c7afeSEric Joyner 
1096c19c7afeSEric Joyner 	error = ixgbe_setup_interface(ctx);
1097c19c7afeSEric Joyner 	if (error) {
1098c19c7afeSEric Joyner 		device_printf(dev, "Interface setup failed: %d\n", error);
1099c19c7afeSEric Joyner 		goto err;
1100c19c7afeSEric Joyner 	}
1101c19c7afeSEric Joyner 
1102c19c7afeSEric Joyner 	ixgbe_if_update_admin_status(ctx);
1103c19c7afeSEric Joyner 
11048eb6488eSEric Joyner 	/* Initialize statistics */
1105b1d5caf3SKevin Bowling 	ixgbe_update_stats_counters(sc);
1106b1d5caf3SKevin Bowling 	ixgbe_add_hw_stats(sc);
11078eb6488eSEric Joyner 
11088eb6488eSEric Joyner 	/* Check PCIE slot type/speed/width */
1109b1d5caf3SKevin Bowling 	ixgbe_get_slot_info(sc);
1110758cc3dcSJack F Vogel 
1111758cc3dcSJack F Vogel 	/*
11128eb6488eSEric Joyner 	 * Do time init and sysctl init here, but
1113b1d5caf3SKevin Bowling 	 * only on the first port of a bypass sc.
1114758cc3dcSJack F Vogel 	 */
1115b1d5caf3SKevin Bowling 	ixgbe_bypass_init(sc);
1116758cc3dcSJack F Vogel 
11177660e4eaSKevin Bowling 	/* Display NVM and Option ROM versions */
11187660e4eaSKevin Bowling 	ixgbe_print_fw_version(ctx);
11197660e4eaSKevin Bowling 
11208eb6488eSEric Joyner 	/* Set an initial dmac value */
1121b1d5caf3SKevin Bowling 	sc->dmac = 0;
11228eb6488eSEric Joyner 	/* Set initial advertised speeds (if applicable) */
1123d381c807SPiotr Pietruszewski 	sc->advertise = ixgbe_get_default_advertise(sc);
1124758cc3dcSJack F Vogel 
1125b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_SRIOV)
11268eb6488eSEric Joyner 		ixgbe_define_iov_schemas(dev, &error);
1127758cc3dcSJack F Vogel 
11288eb6488eSEric Joyner 	/* Add sysctls */
1129c19c7afeSEric Joyner 	ixgbe_add_device_sysctls(ctx);
1130758cc3dcSJack F Vogel 
11318eb6488eSEric Joyner 	return (0);
1132c19c7afeSEric Joyner err:
11338eb6488eSEric Joyner 	return (error);
1134c19c7afeSEric Joyner } /* ixgbe_if_attach_post */
11358eb6488eSEric Joyner 
11368eb6488eSEric Joyner /************************************************************************
11378eb6488eSEric Joyner  * ixgbe_check_wol_support
11386f37f232SEric Joyner  *
11396f37f232SEric Joyner  *   Checks whether the adapter's ports are capable of
11406f37f232SEric Joyner  *   Wake On LAN by reading the adapter's NVM.
11416f37f232SEric Joyner  *
11426f37f232SEric Joyner  *   Sets each port's hw->wol_enabled value depending
11436f37f232SEric Joyner  *   on the value read here.
11448eb6488eSEric Joyner  ************************************************************************/
11456f37f232SEric Joyner static void
1146b1d5caf3SKevin Bowling ixgbe_check_wol_support(struct ixgbe_softc *sc)
11476f37f232SEric Joyner {
1148b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
11496f37f232SEric Joyner 	u16             dev_caps = 0;
11506f37f232SEric Joyner 
11516f37f232SEric Joyner 	/* Find out WoL support for port */
1152b1d5caf3SKevin Bowling 	sc->wol_support = hw->wol_enabled = 0;
11536f37f232SEric Joyner 	ixgbe_get_device_caps(hw, &dev_caps);
11546f37f232SEric Joyner 	if ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
11556f37f232SEric Joyner 	    ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0) &&
11566f37f232SEric Joyner 	     hw->bus.func == 0))
1157b1d5caf3SKevin Bowling 		sc->wol_support = hw->wol_enabled = 1;
11586f37f232SEric Joyner 
11596f37f232SEric Joyner 	/* Save initial wake up filter configuration */
1160b1d5caf3SKevin Bowling 	sc->wufc = IXGBE_READ_REG(hw, IXGBE_WUFC);
11616f37f232SEric Joyner 
11626f37f232SEric Joyner 	return;
11638eb6488eSEric Joyner } /* ixgbe_check_wol_support */
11646f37f232SEric Joyner 
11658eb6488eSEric Joyner /************************************************************************
11668eb6488eSEric Joyner  * ixgbe_setup_interface
11678eb6488eSEric Joyner  *
11688eb6488eSEric Joyner  *   Setup networking device structure and register an interface.
11698eb6488eSEric Joyner  ************************************************************************/
11706f37f232SEric Joyner static int
1171c19c7afeSEric Joyner ixgbe_setup_interface(if_ctx_t ctx)
11726f37f232SEric Joyner {
1173ff06a8dbSJustin Hibbits 	if_t               ifp = iflib_get_ifp(ctx);
1174b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
11756f37f232SEric Joyner 
11768eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_setup_interface: begin");
11776f37f232SEric Joyner 
1178c19c7afeSEric Joyner 	if_setbaudrate(ifp, IF_Gbps(10));
11796f37f232SEric Joyner 
1180ff06a8dbSJustin Hibbits 	sc->max_frame_size = if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN;
1181758cc3dcSJack F Vogel 
1182b1d5caf3SKevin Bowling 	sc->phy_layer = ixgbe_get_supported_physical_layer(&sc->hw);
11838eb6488eSEric Joyner 
1184c19c7afeSEric Joyner 	ixgbe_add_media_types(ctx);
1185c19c7afeSEric Joyner 
1186c19c7afeSEric Joyner 	/* Autoselect media by default */
1187b1d5caf3SKevin Bowling 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
11888eb6488eSEric Joyner 
11898eb6488eSEric Joyner 	return (0);
11908eb6488eSEric Joyner } /* ixgbe_setup_interface */
1191758cc3dcSJack F Vogel 
11928eb6488eSEric Joyner /************************************************************************
1193c19c7afeSEric Joyner  * ixgbe_if_get_counter
11948eb6488eSEric Joyner  ************************************************************************/
1195758cc3dcSJack F Vogel static uint64_t
1196c19c7afeSEric Joyner ixgbe_if_get_counter(if_ctx_t ctx, ift_counter cnt)
1197758cc3dcSJack F Vogel {
1198b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
1199c19c7afeSEric Joyner 	if_t           ifp = iflib_get_ifp(ctx);
1200758cc3dcSJack F Vogel 
1201758cc3dcSJack F Vogel 	switch (cnt) {
1202758cc3dcSJack F Vogel 	case IFCOUNTER_IPACKETS:
1203b1d5caf3SKevin Bowling 		return (sc->ipackets);
1204758cc3dcSJack F Vogel 	case IFCOUNTER_OPACKETS:
1205b1d5caf3SKevin Bowling 		return (sc->opackets);
1206758cc3dcSJack F Vogel 	case IFCOUNTER_IBYTES:
1207b1d5caf3SKevin Bowling 		return (sc->ibytes);
1208758cc3dcSJack F Vogel 	case IFCOUNTER_OBYTES:
1209b1d5caf3SKevin Bowling 		return (sc->obytes);
1210758cc3dcSJack F Vogel 	case IFCOUNTER_IMCASTS:
1211b1d5caf3SKevin Bowling 		return (sc->imcasts);
1212758cc3dcSJack F Vogel 	case IFCOUNTER_OMCASTS:
1213b1d5caf3SKevin Bowling 		return (sc->omcasts);
1214758cc3dcSJack F Vogel 	case IFCOUNTER_COLLISIONS:
1215758cc3dcSJack F Vogel 		return (0);
1216758cc3dcSJack F Vogel 	case IFCOUNTER_IQDROPS:
1217b1d5caf3SKevin Bowling 		return (sc->iqdrops);
1218625d12c6SJohn Baldwin 	case IFCOUNTER_OQDROPS:
1219c19c7afeSEric Joyner 		return (0);
1220758cc3dcSJack F Vogel 	case IFCOUNTER_IERRORS:
1221b1d5caf3SKevin Bowling 		return (sc->ierrors);
1222758cc3dcSJack F Vogel 	default:
1223758cc3dcSJack F Vogel 		return (if_get_counter_default(ifp, cnt));
1224758cc3dcSJack F Vogel 	}
1225c19c7afeSEric Joyner } /* ixgbe_if_get_counter */
1226758cc3dcSJack F Vogel 
12278eb6488eSEric Joyner /************************************************************************
1228bca38080SAndrew Gallatin  * ixgbe_if_i2c_req
1229bca38080SAndrew Gallatin  ************************************************************************/
1230bca38080SAndrew Gallatin static int
1231bca38080SAndrew Gallatin ixgbe_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req)
1232bca38080SAndrew Gallatin {
1233b1d5caf3SKevin Bowling 	struct ixgbe_softc		*sc = iflib_get_softc(ctx);
1234b1d5caf3SKevin Bowling 	struct ixgbe_hw 	*hw = &sc->hw;
1235bca38080SAndrew Gallatin 	int 			i;
1236bca38080SAndrew Gallatin 
1237bca38080SAndrew Gallatin 
1238bca38080SAndrew Gallatin 	if (hw->phy.ops.read_i2c_byte == NULL)
1239bca38080SAndrew Gallatin 		return (ENXIO);
1240bca38080SAndrew Gallatin 	for (i = 0; i < req->len; i++)
1241bca38080SAndrew Gallatin 		hw->phy.ops.read_i2c_byte(hw, req->offset + i,
1242bca38080SAndrew Gallatin 		    req->dev_addr, &req->data[i]);
1243bca38080SAndrew Gallatin 	return (0);
1244bca38080SAndrew Gallatin } /* ixgbe_if_i2c_req */
1245bca38080SAndrew Gallatin 
1246cf150917SEric Joyner /* ixgbe_if_needs_restart - Tell iflib when the driver needs to be reinitialized
1247cf150917SEric Joyner  * @ctx: iflib context
1248cf150917SEric Joyner  * @event: event code to check
1249cf150917SEric Joyner  *
1250cf150917SEric Joyner  * Defaults to returning true for unknown events.
1251cf150917SEric Joyner  *
1252cf150917SEric Joyner  * @returns true if iflib needs to reinit the interface
1253cf150917SEric Joyner  */
1254cf150917SEric Joyner static bool
1255cf150917SEric Joyner ixgbe_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
1256cf150917SEric Joyner {
1257cf150917SEric Joyner 	switch (event) {
1258cf150917SEric Joyner 	case IFLIB_RESTART_VLAN_CONFIG:
1259cf150917SEric Joyner 		return (false);
1260cf150917SEric Joyner 	default:
1261cf150917SEric Joyner 		return (true);
1262cf150917SEric Joyner 	}
1263cf150917SEric Joyner }
1264cf150917SEric Joyner 
1265bca38080SAndrew Gallatin /************************************************************************
12668eb6488eSEric Joyner  * ixgbe_add_media_types
12678eb6488eSEric Joyner  ************************************************************************/
12688eb6488eSEric Joyner static void
1269c19c7afeSEric Joyner ixgbe_add_media_types(if_ctx_t ctx)
12708eb6488eSEric Joyner {
1271b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
1272b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
1273c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
12748eb6488eSEric Joyner 	u64             layer;
12758eb6488eSEric Joyner 
1276b1d5caf3SKevin Bowling 	layer = sc->phy_layer = ixgbe_get_supported_physical_layer(hw);
12778eb6488eSEric Joyner 
12788eb6488eSEric Joyner 	/* Media types with matching FreeBSD media defines */
12798eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T)
1280b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_T, 0, NULL);
12818eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T)
1282b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
12838eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_100BASE_TX)
1284b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_100_TX, 0, NULL);
12858eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
1286b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10_T, 0, NULL);
12878eb6488eSEric Joyner 
1288d381c807SPiotr Pietruszewski 	if (hw->mac.type == ixgbe_mac_X550) {
1289d381c807SPiotr Pietruszewski 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_T, 0, NULL);
1290d381c807SPiotr Pietruszewski 		ifmedia_add(sc->media, IFM_ETHER | IFM_5000_T, 0, NULL);
1291d381c807SPiotr Pietruszewski 	}
1292d381c807SPiotr Pietruszewski 
12938eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
12948eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
1295b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_TWINAX, 0,
12968eb6488eSEric Joyner 		    NULL);
12978eb6488eSEric Joyner 
12988eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR) {
1299b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_LR, 0, NULL);
13008eb6488eSEric Joyner 		if (hw->phy.multispeed_fiber)
1301b1d5caf3SKevin Bowling 			ifmedia_add(sc->media, IFM_ETHER | IFM_1000_LX, 0,
13028eb6488eSEric Joyner 			    NULL);
13038eb6488eSEric Joyner 	}
13048eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
1305b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
13068eb6488eSEric Joyner 		if (hw->phy.multispeed_fiber)
1307b1d5caf3SKevin Bowling 			ifmedia_add(sc->media, IFM_ETHER | IFM_1000_SX, 0,
13088eb6488eSEric Joyner 			    NULL);
13098eb6488eSEric Joyner 	} else if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
1310b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_SX, 0, NULL);
13118eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
1312b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
13138eb6488eSEric Joyner 
13148eb6488eSEric Joyner #ifdef IFM_ETH_XTYPE
13158eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
1316b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_KR, 0, NULL);
13178eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4)
1318b1d5caf3SKevin Bowling 		ifmedia_add( sc->media, IFM_ETHER | IFM_10G_KX4, 0, NULL);
13198eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
1320b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_KX, 0, NULL);
13218eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX)
1322b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_KX, 0, NULL);
13238eb6488eSEric Joyner #else
13248eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
13258eb6488eSEric Joyner 		device_printf(dev, "Media supported: 10GbaseKR\n");
13268eb6488eSEric Joyner 		device_printf(dev, "10GbaseKR mapped to 10GbaseSR\n");
1327b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
13288eb6488eSEric Joyner 	}
13298eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
13308eb6488eSEric Joyner 		device_printf(dev, "Media supported: 10GbaseKX4\n");
13318eb6488eSEric Joyner 		device_printf(dev, "10GbaseKX4 mapped to 10GbaseCX4\n");
1332b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
13338eb6488eSEric Joyner 	}
13348eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX) {
13358eb6488eSEric Joyner 		device_printf(dev, "Media supported: 1000baseKX\n");
13368eb6488eSEric Joyner 		device_printf(dev, "1000baseKX mapped to 1000baseCX\n");
1337b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_CX, 0, NULL);
13388eb6488eSEric Joyner 	}
13398eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX) {
13408eb6488eSEric Joyner 		device_printf(dev, "Media supported: 2500baseKX\n");
13418eb6488eSEric Joyner 		device_printf(dev, "2500baseKX mapped to 2500baseSX\n");
1342b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_SX, 0, NULL);
13438eb6488eSEric Joyner 	}
13448eb6488eSEric Joyner #endif
13458eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_BX)
13468eb6488eSEric Joyner 		device_printf(dev, "Media supported: 1000baseBX\n");
13478eb6488eSEric Joyner 
13488eb6488eSEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_82598AT) {
1349b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T | IFM_FDX,
13508eb6488eSEric Joyner 		    0, NULL);
1351b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
13528eb6488eSEric Joyner 	}
13538eb6488eSEric Joyner 
1354b1d5caf3SKevin Bowling 	ifmedia_add(sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
13558eb6488eSEric Joyner } /* ixgbe_add_media_types */
13568eb6488eSEric Joyner 
13578eb6488eSEric Joyner /************************************************************************
13588eb6488eSEric Joyner  * ixgbe_is_sfp
13598eb6488eSEric Joyner  ************************************************************************/
13608eb6488eSEric Joyner static inline bool
13618eb6488eSEric Joyner ixgbe_is_sfp(struct ixgbe_hw *hw)
13628eb6488eSEric Joyner {
13638eb6488eSEric Joyner 	switch (hw->mac.type) {
13648eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
13658eb6488eSEric Joyner 		if (hw->phy.type == ixgbe_phy_nl)
136679b36ec9SKevin Bowling 			return (true);
136779b36ec9SKevin Bowling 		return (false);
13688eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
13698eb6488eSEric Joyner 		switch (hw->mac.ops.get_media_type(hw)) {
13708eb6488eSEric Joyner 		case ixgbe_media_type_fiber:
13718eb6488eSEric Joyner 		case ixgbe_media_type_fiber_qsfp:
137279b36ec9SKevin Bowling 			return (true);
13738eb6488eSEric Joyner 		default:
137479b36ec9SKevin Bowling 			return (false);
13758eb6488eSEric Joyner 		}
13768eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
13778eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
13788eb6488eSEric Joyner 		if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_fiber)
137979b36ec9SKevin Bowling 			return (true);
138079b36ec9SKevin Bowling 		return (false);
13818eb6488eSEric Joyner 	default:
138279b36ec9SKevin Bowling 		return (false);
13838eb6488eSEric Joyner 	}
13848eb6488eSEric Joyner } /* ixgbe_is_sfp */
13858eb6488eSEric Joyner 
13868eb6488eSEric Joyner /************************************************************************
13878eb6488eSEric Joyner  * ixgbe_config_link
13888eb6488eSEric Joyner  ************************************************************************/
13898eb6488eSEric Joyner static void
1390b2c1e8e6SEric Joyner ixgbe_config_link(if_ctx_t ctx)
13918eb6488eSEric Joyner {
1392b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
1393b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
13948eb6488eSEric Joyner 	u32             autoneg, err = 0;
13958eb6488eSEric Joyner 	bool            sfp, negotiate;
13968eb6488eSEric Joyner 
13978eb6488eSEric Joyner 	sfp = ixgbe_is_sfp(hw);
13988eb6488eSEric Joyner 
13998eb6488eSEric Joyner 	if (sfp) {
1400b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
1401b2c1e8e6SEric Joyner 		iflib_admin_intr_deferred(ctx);
14028eb6488eSEric Joyner 	} else {
14038eb6488eSEric Joyner 		if (hw->mac.ops.check_link)
1404b1d5caf3SKevin Bowling 			err = ixgbe_check_link(hw, &sc->link_speed,
1405b1d5caf3SKevin Bowling 			    &sc->link_up, false);
14068eb6488eSEric Joyner 		if (err)
1407c19c7afeSEric Joyner 			return;
14088eb6488eSEric Joyner 		autoneg = hw->phy.autoneg_advertised;
14098eb6488eSEric Joyner 		if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
14108eb6488eSEric Joyner 			err = hw->mac.ops.get_link_capabilities(hw, &autoneg,
14118eb6488eSEric Joyner 			    &negotiate);
14128eb6488eSEric Joyner 		if (err)
1413c19c7afeSEric Joyner 			return;
1414d381c807SPiotr Pietruszewski 
1415d381c807SPiotr Pietruszewski 		if (hw->mac.type == ixgbe_mac_X550 &&
1416d381c807SPiotr Pietruszewski 		    hw->phy.autoneg_advertised == 0) {
1417d381c807SPiotr Pietruszewski 			/*
1418d381c807SPiotr Pietruszewski 			 * 2.5G and 5G autonegotiation speeds on X550
1419d381c807SPiotr Pietruszewski 			 * are disabled by default due to reported
1420d381c807SPiotr Pietruszewski 			 * interoperability issues with some switches.
1421d381c807SPiotr Pietruszewski 			 *
1422d381c807SPiotr Pietruszewski 			 * The second condition checks if any operations
1423d381c807SPiotr Pietruszewski 			 * involving setting autonegotiation speeds have
1424d381c807SPiotr Pietruszewski 			 * been performed prior to this ixgbe_config_link()
1425d381c807SPiotr Pietruszewski 			 * call.
1426d381c807SPiotr Pietruszewski 			 *
1427d381c807SPiotr Pietruszewski 			 * If hw->phy.autoneg_advertised does not
1428d381c807SPiotr Pietruszewski 			 * equal 0, this means that the user might have
1429d381c807SPiotr Pietruszewski 			 * set autonegotiation speeds via the sysctl
1430d381c807SPiotr Pietruszewski 			 * before bringing the interface up. In this
1431d381c807SPiotr Pietruszewski 			 * case, we should not disable 2.5G and 5G
1432d381c807SPiotr Pietruszewski 			 * since that speeds might be selected by the
1433d381c807SPiotr Pietruszewski 			 * user.
1434d381c807SPiotr Pietruszewski 			 *
1435d381c807SPiotr Pietruszewski 			 * Otherwise (i.e. if hw->phy.autoneg_advertised
1436d381c807SPiotr Pietruszewski 			 * is set to 0), it is the first time we set
1437d381c807SPiotr Pietruszewski 			 * autonegotiation preferences and the default
1438d381c807SPiotr Pietruszewski 			 * set of speeds should exclude 2.5G and 5G.
1439d381c807SPiotr Pietruszewski 			 */
1440d381c807SPiotr Pietruszewski 			autoneg &= ~(IXGBE_LINK_SPEED_2_5GB_FULL |
1441d381c807SPiotr Pietruszewski 			    IXGBE_LINK_SPEED_5GB_FULL);
1442d381c807SPiotr Pietruszewski 		}
1443d381c807SPiotr Pietruszewski 
14448eb6488eSEric Joyner 		if (hw->mac.ops.setup_link)
14458eb6488eSEric Joyner 			err = hw->mac.ops.setup_link(hw, autoneg,
1446b1d5caf3SKevin Bowling 			    sc->link_up);
14478eb6488eSEric Joyner 	}
14488eb6488eSEric Joyner } /* ixgbe_config_link */
14498eb6488eSEric Joyner 
14508eb6488eSEric Joyner /************************************************************************
14518eb6488eSEric Joyner  * ixgbe_update_stats_counters - Update board statistics counters.
14528eb6488eSEric Joyner  ************************************************************************/
14538eb6488eSEric Joyner static void
1454b1d5caf3SKevin Bowling ixgbe_update_stats_counters(struct ixgbe_softc *sc)
14558eb6488eSEric Joyner {
1456b1d5caf3SKevin Bowling 	struct ixgbe_hw       *hw = &sc->hw;
1457b1d5caf3SKevin Bowling 	struct ixgbe_hw_stats *stats = &sc->stats.pf;
14588eb6488eSEric Joyner 	u32                   missed_rx = 0, bprc, lxon, lxoff, total;
1459e37d3dc1SEric Joyner 	u32                   lxoffrxc;
14608eb6488eSEric Joyner 	u64                   total_missed_rx = 0;
14618eb6488eSEric Joyner 
14628eb6488eSEric Joyner 	stats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
14638eb6488eSEric Joyner 	stats->illerrc += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
14648eb6488eSEric Joyner 	stats->errbc += IXGBE_READ_REG(hw, IXGBE_ERRBC);
14658eb6488eSEric Joyner 	stats->mspdc += IXGBE_READ_REG(hw, IXGBE_MSPDC);
14668eb6488eSEric Joyner 	stats->mpc[0] += IXGBE_READ_REG(hw, IXGBE_MPC(0));
14678eb6488eSEric Joyner 
14688eb6488eSEric Joyner 	for (int i = 0; i < 16; i++) {
14698eb6488eSEric Joyner 		stats->qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
14708eb6488eSEric Joyner 		stats->qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
14718eb6488eSEric Joyner 		stats->qprdc[i] += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
14728eb6488eSEric Joyner 	}
14738eb6488eSEric Joyner 	stats->mlfc += IXGBE_READ_REG(hw, IXGBE_MLFC);
14748eb6488eSEric Joyner 	stats->mrfc += IXGBE_READ_REG(hw, IXGBE_MRFC);
14758eb6488eSEric Joyner 	stats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
14768eb6488eSEric Joyner 
14778eb6488eSEric Joyner 	/* Hardware workaround, gprc counts missed packets */
14788eb6488eSEric Joyner 	stats->gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
14798eb6488eSEric Joyner 	stats->gprc -= missed_rx;
14808eb6488eSEric Joyner 
14818eb6488eSEric Joyner 	if (hw->mac.type != ixgbe_mac_82598EB) {
14828eb6488eSEric Joyner 		stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL) +
14838eb6488eSEric Joyner 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
14848eb6488eSEric Joyner 		stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
14858eb6488eSEric Joyner 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32);
14868eb6488eSEric Joyner 		stats->tor += IXGBE_READ_REG(hw, IXGBE_TORL) +
14878eb6488eSEric Joyner 		    ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
14888eb6488eSEric Joyner 		stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
1489e37d3dc1SEric Joyner 		lxoffrxc = IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
1490e37d3dc1SEric Joyner 		stats->lxoffrxc += lxoffrxc;
14918eb6488eSEric Joyner 	} else {
14928eb6488eSEric Joyner 		stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
1493e37d3dc1SEric Joyner 		lxoffrxc = IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
1494e37d3dc1SEric Joyner 		stats->lxoffrxc += lxoffrxc;
14958eb6488eSEric Joyner 		/* 82598 only has a counter in the high register */
14968eb6488eSEric Joyner 		stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
14978eb6488eSEric Joyner 		stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
14988eb6488eSEric Joyner 		stats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
14998eb6488eSEric Joyner 	}
15008eb6488eSEric Joyner 
15018eb6488eSEric Joyner 	/*
1502e37d3dc1SEric Joyner 	 * For watchdog management we need to know if we have been paused
1503e37d3dc1SEric Joyner 	 * during the last interval, so capture that here.
1504e37d3dc1SEric Joyner 	*/
1505e37d3dc1SEric Joyner 	if (lxoffrxc)
1506b1d5caf3SKevin Bowling 		sc->shared->isc_pause_frames = 1;
1507e37d3dc1SEric Joyner 
1508e37d3dc1SEric Joyner 	/*
15098eb6488eSEric Joyner 	 * Workaround: mprc hardware is incorrectly counting
15108eb6488eSEric Joyner 	 * broadcasts, so for now we subtract those.
1511758cc3dcSJack F Vogel 	 */
15128eb6488eSEric Joyner 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
15138eb6488eSEric Joyner 	stats->bprc += bprc;
15148eb6488eSEric Joyner 	stats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
15158eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
15168eb6488eSEric Joyner 		stats->mprc -= bprc;
15178eb6488eSEric Joyner 
15188eb6488eSEric Joyner 	stats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
15198eb6488eSEric Joyner 	stats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
15208eb6488eSEric Joyner 	stats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
15218eb6488eSEric Joyner 	stats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
15228eb6488eSEric Joyner 	stats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
15238eb6488eSEric Joyner 	stats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
15248eb6488eSEric Joyner 
15258eb6488eSEric Joyner 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
15268eb6488eSEric Joyner 	stats->lxontxc += lxon;
15278eb6488eSEric Joyner 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
15288eb6488eSEric Joyner 	stats->lxofftxc += lxoff;
15298eb6488eSEric Joyner 	total = lxon + lxoff;
15308eb6488eSEric Joyner 
15318eb6488eSEric Joyner 	stats->gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
15328eb6488eSEric Joyner 	stats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
15338eb6488eSEric Joyner 	stats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
15348eb6488eSEric Joyner 	stats->gptc -= total;
15358eb6488eSEric Joyner 	stats->mptc -= total;
15368eb6488eSEric Joyner 	stats->ptc64 -= total;
15378eb6488eSEric Joyner 	stats->gotc -= total * ETHER_MIN_LEN;
15388eb6488eSEric Joyner 
15398eb6488eSEric Joyner 	stats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
15408eb6488eSEric Joyner 	stats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
15418eb6488eSEric Joyner 	stats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
15428eb6488eSEric Joyner 	stats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
15438eb6488eSEric Joyner 	stats->mngprc += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
15448eb6488eSEric Joyner 	stats->mngpdc += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
15458eb6488eSEric Joyner 	stats->mngptc += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
15468eb6488eSEric Joyner 	stats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
15478eb6488eSEric Joyner 	stats->tpt += IXGBE_READ_REG(hw, IXGBE_TPT);
15488eb6488eSEric Joyner 	stats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
15498eb6488eSEric Joyner 	stats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
15508eb6488eSEric Joyner 	stats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
15518eb6488eSEric Joyner 	stats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
15528eb6488eSEric Joyner 	stats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
15538eb6488eSEric Joyner 	stats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
15548eb6488eSEric Joyner 	stats->xec += IXGBE_READ_REG(hw, IXGBE_XEC);
15558eb6488eSEric Joyner 	stats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
15568eb6488eSEric Joyner 	stats->fclast += IXGBE_READ_REG(hw, IXGBE_FCLAST);
15578eb6488eSEric Joyner 	/* Only read FCOE on 82599 */
15588eb6488eSEric Joyner 	if (hw->mac.type != ixgbe_mac_82598EB) {
15598eb6488eSEric Joyner 		stats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
15608eb6488eSEric Joyner 		stats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
15618eb6488eSEric Joyner 		stats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
15628eb6488eSEric Joyner 		stats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
15638eb6488eSEric Joyner 		stats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
15648eb6488eSEric Joyner 	}
15658eb6488eSEric Joyner 
15668eb6488eSEric Joyner 	/* Fill out the OS statistics structure */
1567b1d5caf3SKevin Bowling 	IXGBE_SET_IPACKETS(sc, stats->gprc);
1568b1d5caf3SKevin Bowling 	IXGBE_SET_OPACKETS(sc, stats->gptc);
1569b1d5caf3SKevin Bowling 	IXGBE_SET_IBYTES(sc, stats->gorc);
1570b1d5caf3SKevin Bowling 	IXGBE_SET_OBYTES(sc, stats->gotc);
1571b1d5caf3SKevin Bowling 	IXGBE_SET_IMCASTS(sc, stats->mprc);
1572b1d5caf3SKevin Bowling 	IXGBE_SET_OMCASTS(sc, stats->mptc);
1573b1d5caf3SKevin Bowling 	IXGBE_SET_COLLISIONS(sc, 0);
1574b1d5caf3SKevin Bowling 	IXGBE_SET_IQDROPS(sc, total_missed_rx);
1575afb1aa4eSPiotr Pietruszewski 
1576afb1aa4eSPiotr Pietruszewski 	/*
1577afb1aa4eSPiotr Pietruszewski 	 * Aggregate following types of errors as RX errors:
1578afb1aa4eSPiotr Pietruszewski 	 * - CRC error count,
1579afb1aa4eSPiotr Pietruszewski 	 * - illegal byte error count,
1580afb1aa4eSPiotr Pietruszewski 	 * - missed packets count,
1581afb1aa4eSPiotr Pietruszewski 	 * - length error count,
1582afb1aa4eSPiotr Pietruszewski 	 * - undersized packets count,
1583afb1aa4eSPiotr Pietruszewski 	 * - fragmented packets count,
1584afb1aa4eSPiotr Pietruszewski 	 * - oversized packets count,
1585afb1aa4eSPiotr Pietruszewski 	 * - jabber count.
1586afb1aa4eSPiotr Pietruszewski 	 */
15878526120aSAndrey V. Elsukov 	IXGBE_SET_IERRORS(sc, stats->crcerrs + stats->illerrc +
1588afb1aa4eSPiotr Pietruszewski 	    stats->mpc[0] + stats->rlec + stats->ruc + stats->rfc + stats->roc +
1589afb1aa4eSPiotr Pietruszewski 	    stats->rjc);
15908eb6488eSEric Joyner } /* ixgbe_update_stats_counters */
15918eb6488eSEric Joyner 
15928eb6488eSEric Joyner /************************************************************************
15938eb6488eSEric Joyner  * ixgbe_add_hw_stats
15948eb6488eSEric Joyner  *
15958eb6488eSEric Joyner  *   Add sysctl variables, one per statistic, to the system.
15968eb6488eSEric Joyner  ************************************************************************/
15978eb6488eSEric Joyner static void
1598b1d5caf3SKevin Bowling ixgbe_add_hw_stats(struct ixgbe_softc *sc)
15998eb6488eSEric Joyner {
1600b1d5caf3SKevin Bowling 	device_t               dev = iflib_get_dev(sc->ctx);
1601c19c7afeSEric Joyner 	struct ix_rx_queue     *rx_que;
1602c19c7afeSEric Joyner 	struct ix_tx_queue     *tx_que;
16038eb6488eSEric Joyner 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
16048eb6488eSEric Joyner 	struct sysctl_oid      *tree = device_get_sysctl_tree(dev);
16058eb6488eSEric Joyner 	struct sysctl_oid_list *child = SYSCTL_CHILDREN(tree);
1606b1d5caf3SKevin Bowling 	struct ixgbe_hw_stats  *stats = &sc->stats.pf;
16078eb6488eSEric Joyner 	struct sysctl_oid      *stat_node, *queue_node;
16088eb6488eSEric Joyner 	struct sysctl_oid_list *stat_list, *queue_list;
1609c19c7afeSEric Joyner 	int                    i;
16108eb6488eSEric Joyner 
16118eb6488eSEric Joyner #define QUEUE_NAME_LEN 32
16128eb6488eSEric Joyner 	char                   namebuf[QUEUE_NAME_LEN];
16138eb6488eSEric Joyner 
16148eb6488eSEric Joyner 	/* Driver Statistics */
16158eb6488eSEric Joyner 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dropped",
1616b1d5caf3SKevin Bowling 	    CTLFLAG_RD, &sc->dropped_pkts, "Driver dropped packets");
16178eb6488eSEric Joyner 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "watchdog_events",
1618b1d5caf3SKevin Bowling 	    CTLFLAG_RD, &sc->watchdog_events, "Watchdog timeouts");
16198eb6488eSEric Joyner 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "link_irq",
1620b1d5caf3SKevin Bowling 	    CTLFLAG_RD, &sc->link_irq, "Link MSI-X IRQ Handled");
16218eb6488eSEric Joyner 
1622b1d5caf3SKevin Bowling 	for (i = 0, tx_que = sc->tx_queues; i < sc->num_tx_queues; i++, tx_que++) {
1623c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
16248eb6488eSEric Joyner 		snprintf(namebuf, QUEUE_NAME_LEN, "queue%d", i);
16258eb6488eSEric Joyner 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
162620b91f0aSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
16278eb6488eSEric Joyner 		queue_list = SYSCTL_CHILDREN(queue_node);
16288eb6488eSEric Joyner 
16298eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_head",
163020b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, txr, 0,
16318eb6488eSEric Joyner 		    ixgbe_sysctl_tdh_handler, "IU", "Transmit Descriptor Head");
16328eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_tail",
163320b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, txr, 0,
16348eb6488eSEric Joyner 		    ixgbe_sysctl_tdt_handler, "IU", "Transmit Descriptor Tail");
16358eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tso_tx",
16368eb6488eSEric Joyner 		    CTLFLAG_RD, &txr->tso_tx, "TSO");
16378eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tx_packets",
16388eb6488eSEric Joyner 		    CTLFLAG_RD, &txr->total_packets,
16398eb6488eSEric Joyner 		    "Queue Packets Transmitted");
16408eb6488eSEric Joyner 	}
16418eb6488eSEric Joyner 
1642b1d5caf3SKevin Bowling 	for (i = 0, rx_que = sc->rx_queues; i < sc->num_rx_queues; i++, rx_que++) {
1643c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
16448eb6488eSEric Joyner 		snprintf(namebuf, QUEUE_NAME_LEN, "queue%d", i);
16458eb6488eSEric Joyner 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
164620b91f0aSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
16478eb6488eSEric Joyner 		queue_list = SYSCTL_CHILDREN(queue_node);
16488eb6488eSEric Joyner 
1649c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "interrupt_rate",
165020b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1651b1d5caf3SKevin Bowling 		    &sc->rx_queues[i], 0,
1652c19c7afeSEric Joyner 		    ixgbe_sysctl_interrupt_rate_handler, "IU",
1653c19c7afeSEric Joyner 		    "Interrupt Rate");
1654c19c7afeSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "irqs",
1655b1d5caf3SKevin Bowling 		    CTLFLAG_RD, &(sc->rx_queues[i].irqs),
1656c19c7afeSEric Joyner 		    "irqs on this queue");
16578eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_head",
165820b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, rxr, 0,
16598eb6488eSEric Joyner 		    ixgbe_sysctl_rdh_handler, "IU", "Receive Descriptor Head");
16608eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_tail",
166120b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, rxr, 0,
16628eb6488eSEric Joyner 		    ixgbe_sysctl_rdt_handler, "IU", "Receive Descriptor Tail");
16638eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_packets",
16648eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_packets, "Queue Packets Received");
16658eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_bytes",
16668eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_bytes, "Queue Bytes Received");
16678eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_copies",
16688eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_copies, "Copied RX Frames");
16698eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_discarded",
16708eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_discarded, "Discarded RX packets");
16718eb6488eSEric Joyner 	}
16728eb6488eSEric Joyner 
16738eb6488eSEric Joyner 	/* MAC stats get their own sub node */
16748eb6488eSEric Joyner 
16758eb6488eSEric Joyner 	stat_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "mac_stats",
167620b91f0aSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "MAC Statistics");
16778eb6488eSEric Joyner 	stat_list = SYSCTL_CHILDREN(stat_node);
16788eb6488eSEric Joyner 
1679afb1aa4eSPiotr Pietruszewski 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_errs",
1680b1d5caf3SKevin Bowling 	    CTLFLAG_RD, &sc->ierrors, IXGBE_SYSCTL_DESC_RX_ERRS);
16818eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "crc_errs",
16828eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->crcerrs, "CRC Errors");
16838eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "ill_errs",
16848eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->illerrc, "Illegal Byte Errors");
16858eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "byte_errs",
16868eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->errbc, "Byte Errors");
16878eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "short_discards",
16888eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mspdc, "MAC Short Packets Discarded");
16898eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "local_faults",
16908eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mlfc, "MAC Local Faults");
16918eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "remote_faults",
16928eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mrfc, "MAC Remote Faults");
16938eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rec_len_errs",
16948eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->rlec, "Receive Length Errors");
16958eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_missed_packets",
16968eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mpc[0], "RX Missed Packet Count");
16978eb6488eSEric Joyner 
16988eb6488eSEric Joyner 	/* Flow Control stats */
16998eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_txd",
17008eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxontxc, "Link XON Transmitted");
17018eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_recvd",
17028eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxonrxc, "Link XON Received");
17038eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_txd",
17048eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxofftxc, "Link XOFF Transmitted");
17058eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_recvd",
17068eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxoffrxc, "Link XOFF Received");
17078eb6488eSEric Joyner 
17088eb6488eSEric Joyner 	/* Packet Reception Stats */
17098eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_octets_rcvd",
17108eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->tor, "Total Octets Received");
17118eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_rcvd",
17128eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gorc, "Good Octets Received");
17138eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_rcvd",
17148eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->tpr, "Total Packets Received");
17158eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_rcvd",
17168eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gprc, "Good Packets Received");
17178eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_rcvd",
17188eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mprc, "Multicast Packets Received");
17198eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_rcvd",
17208eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->bprc, "Broadcast Packets Received");
17218eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_64",
17228eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc64, "64 byte frames received ");
17238eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_65_127",
17248eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc127, "65-127 byte frames received");
17258eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_128_255",
17268eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc255, "128-255 byte frames received");
17278eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_256_511",
17288eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc511, "256-511 byte frames received");
17298eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_512_1023",
17308eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc1023, "512-1023 byte frames received");
17318eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_1024_1522",
17328eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc1522, "1023-1522 byte frames received");
17338eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_undersized",
17348eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ruc, "Receive Undersized");
17358eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_fragmented",
17368eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->rfc, "Fragmented Packets Received ");
17378eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_oversized",
17388eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->roc, "Oversized Packets Received");
17398eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_jabberd",
17408eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->rjc, "Received Jabber");
17418eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_rcvd",
17428eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mngprc, "Management Packets Received");
17438eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_drpd",
17448eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mngptc, "Management Packets Dropped");
17458eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "checksum_errs",
17468eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->xec, "Checksum Errors");
17478eb6488eSEric Joyner 
17488eb6488eSEric Joyner 	/* Packet Transmission Stats */
17498eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_txd",
17508eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gotc, "Good Octets Transmitted");
17518eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_txd",
17528eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->tpt, "Total Packets Transmitted");
17538eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_txd",
17548eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gptc, "Good Packets Transmitted");
17558eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_txd",
17568eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->bptc, "Broadcast Packets Transmitted");
17578eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_txd",
17588eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mptc, "Multicast Packets Transmitted");
17598eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_txd",
17608eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mngptc, "Management Packets Transmitted");
17618eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_64",
17628eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc64, "64 byte frames transmitted ");
17638eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_65_127",
17648eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc127, "65-127 byte frames transmitted");
17658eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_128_255",
17668eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc255, "128-255 byte frames transmitted");
17678eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_256_511",
17688eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc511, "256-511 byte frames transmitted");
17698eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_512_1023",
17708eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc1023, "512-1023 byte frames transmitted");
17718eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_1024_1522",
17728eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc1522, "1024-1522 byte frames transmitted");
17738eb6488eSEric Joyner } /* ixgbe_add_hw_stats */
17748eb6488eSEric Joyner 
17758eb6488eSEric Joyner /************************************************************************
17768eb6488eSEric Joyner  * ixgbe_sysctl_tdh_handler - Transmit Descriptor Head handler function
17778eb6488eSEric Joyner  *
17788eb6488eSEric Joyner  *   Retrieves the TDH value from the hardware
17798eb6488eSEric Joyner  ************************************************************************/
1780758cc3dcSJack F Vogel static int
1781758cc3dcSJack F Vogel ixgbe_sysctl_tdh_handler(SYSCTL_HANDLER_ARGS)
1782758cc3dcSJack F Vogel {
1783758cc3dcSJack F Vogel 	struct tx_ring *txr = ((struct tx_ring *)oidp->oid_arg1);
17848eb6488eSEric Joyner 	int            error;
17858eb6488eSEric Joyner 	unsigned int   val;
1786758cc3dcSJack F Vogel 
17878eb6488eSEric Joyner 	if (!txr)
17888eb6488eSEric Joyner 		return (0);
17898eb6488eSEric Joyner 
1790b1d5caf3SKevin Bowling 	val = IXGBE_READ_REG(&txr->sc->hw, IXGBE_TDH(txr->me));
1791758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1792758cc3dcSJack F Vogel 	if (error || !req->newptr)
1793758cc3dcSJack F Vogel 		return error;
1794758cc3dcSJack F Vogel 
17958eb6488eSEric Joyner 	return (0);
17968eb6488eSEric Joyner } /* ixgbe_sysctl_tdh_handler */
17978eb6488eSEric Joyner 
17988eb6488eSEric Joyner /************************************************************************
17998eb6488eSEric Joyner  * ixgbe_sysctl_tdt_handler - Transmit Descriptor Tail handler function
18008eb6488eSEric Joyner  *
1801758cc3dcSJack F Vogel  *   Retrieves the TDT value from the hardware
18028eb6488eSEric Joyner  ************************************************************************/
1803758cc3dcSJack F Vogel static int
1804758cc3dcSJack F Vogel ixgbe_sysctl_tdt_handler(SYSCTL_HANDLER_ARGS)
1805758cc3dcSJack F Vogel {
1806758cc3dcSJack F Vogel 	struct tx_ring *txr = ((struct tx_ring *)oidp->oid_arg1);
18078eb6488eSEric Joyner 	int            error;
18088eb6488eSEric Joyner 	unsigned int   val;
1809758cc3dcSJack F Vogel 
18108eb6488eSEric Joyner 	if (!txr)
18118eb6488eSEric Joyner 		return (0);
18128eb6488eSEric Joyner 
1813b1d5caf3SKevin Bowling 	val = IXGBE_READ_REG(&txr->sc->hw, IXGBE_TDT(txr->me));
1814758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1815758cc3dcSJack F Vogel 	if (error || !req->newptr)
1816758cc3dcSJack F Vogel 		return error;
1817758cc3dcSJack F Vogel 
18188eb6488eSEric Joyner 	return (0);
18198eb6488eSEric Joyner } /* ixgbe_sysctl_tdt_handler */
18208eb6488eSEric Joyner 
18218eb6488eSEric Joyner /************************************************************************
18228eb6488eSEric Joyner  * ixgbe_sysctl_rdh_handler - Receive Descriptor Head handler function
18238eb6488eSEric Joyner  *
1824758cc3dcSJack F Vogel  *   Retrieves the RDH value from the hardware
18258eb6488eSEric Joyner  ************************************************************************/
1826758cc3dcSJack F Vogel static int
1827758cc3dcSJack F Vogel ixgbe_sysctl_rdh_handler(SYSCTL_HANDLER_ARGS)
1828758cc3dcSJack F Vogel {
1829758cc3dcSJack F Vogel 	struct rx_ring *rxr = ((struct rx_ring *)oidp->oid_arg1);
18308eb6488eSEric Joyner 	int            error;
18318eb6488eSEric Joyner 	unsigned int   val;
1832758cc3dcSJack F Vogel 
18338eb6488eSEric Joyner 	if (!rxr)
18348eb6488eSEric Joyner 		return (0);
18358eb6488eSEric Joyner 
1836b1d5caf3SKevin Bowling 	val = IXGBE_READ_REG(&rxr->sc->hw, IXGBE_RDH(rxr->me));
1837758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1838758cc3dcSJack F Vogel 	if (error || !req->newptr)
1839758cc3dcSJack F Vogel 		return error;
1840758cc3dcSJack F Vogel 
18418eb6488eSEric Joyner 	return (0);
18428eb6488eSEric Joyner } /* ixgbe_sysctl_rdh_handler */
18438eb6488eSEric Joyner 
18448eb6488eSEric Joyner /************************************************************************
18458eb6488eSEric Joyner  * ixgbe_sysctl_rdt_handler - Receive Descriptor Tail handler function
18468eb6488eSEric Joyner  *
1847758cc3dcSJack F Vogel  *   Retrieves the RDT value from the hardware
18488eb6488eSEric Joyner  ************************************************************************/
1849758cc3dcSJack F Vogel static int
1850758cc3dcSJack F Vogel ixgbe_sysctl_rdt_handler(SYSCTL_HANDLER_ARGS)
1851758cc3dcSJack F Vogel {
1852758cc3dcSJack F Vogel 	struct rx_ring *rxr = ((struct rx_ring *)oidp->oid_arg1);
18538eb6488eSEric Joyner 	int            error;
18548eb6488eSEric Joyner 	unsigned int   val;
1855758cc3dcSJack F Vogel 
18568eb6488eSEric Joyner 	if (!rxr)
18578eb6488eSEric Joyner 		return (0);
18588eb6488eSEric Joyner 
1859b1d5caf3SKevin Bowling 	val = IXGBE_READ_REG(&rxr->sc->hw, IXGBE_RDT(rxr->me));
1860758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1861758cc3dcSJack F Vogel 	if (error || !req->newptr)
1862758cc3dcSJack F Vogel 		return error;
18638eb6488eSEric Joyner 
18648eb6488eSEric Joyner 	return (0);
18658eb6488eSEric Joyner } /* ixgbe_sysctl_rdt_handler */
18668eb6488eSEric Joyner 
18678eb6488eSEric Joyner /************************************************************************
1868c19c7afeSEric Joyner  * ixgbe_if_vlan_register
18698eb6488eSEric Joyner  *
18708eb6488eSEric Joyner  *   Run via vlan config EVENT, it enables us to use the
18718eb6488eSEric Joyner  *   HW Filter table since we can get the vlan id. This
18728eb6488eSEric Joyner  *   just creates the entry in the soft version of the
18738eb6488eSEric Joyner  *   VFTA, init will repopulate the real table.
18748eb6488eSEric Joyner  ************************************************************************/
18758eb6488eSEric Joyner static void
1876c19c7afeSEric Joyner ixgbe_if_vlan_register(if_ctx_t ctx, u16 vtag)
18778eb6488eSEric Joyner {
1878b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
18798eb6488eSEric Joyner 	u16            index, bit;
18808eb6488eSEric Joyner 
18818eb6488eSEric Joyner 	index = (vtag >> 5) & 0x7F;
18828eb6488eSEric Joyner 	bit = vtag & 0x1F;
1883b1d5caf3SKevin Bowling 	sc->shadow_vfta[index] |= (1 << bit);
1884b1d5caf3SKevin Bowling 	++sc->num_vlans;
1885c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
1886c19c7afeSEric Joyner } /* ixgbe_if_vlan_register */
18878eb6488eSEric Joyner 
18888eb6488eSEric Joyner /************************************************************************
1889c19c7afeSEric Joyner  * ixgbe_if_vlan_unregister
18908eb6488eSEric Joyner  *
18918eb6488eSEric Joyner  *   Run via vlan unconfig EVENT, remove our entry in the soft vfta.
18928eb6488eSEric Joyner  ************************************************************************/
18938eb6488eSEric Joyner static void
1894c19c7afeSEric Joyner ixgbe_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
18958eb6488eSEric Joyner {
1896b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
18978eb6488eSEric Joyner 	u16            index, bit;
18988eb6488eSEric Joyner 
18998eb6488eSEric Joyner 	index = (vtag >> 5) & 0x7F;
19008eb6488eSEric Joyner 	bit = vtag & 0x1F;
1901b1d5caf3SKevin Bowling 	sc->shadow_vfta[index] &= ~(1 << bit);
1902b1d5caf3SKevin Bowling 	--sc->num_vlans;
19038eb6488eSEric Joyner 	/* Re-init to load the changes */
1904c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
1905c19c7afeSEric Joyner } /* ixgbe_if_vlan_unregister */
19068eb6488eSEric Joyner 
19078eb6488eSEric Joyner /************************************************************************
19088eb6488eSEric Joyner  * ixgbe_setup_vlan_hw_support
19098eb6488eSEric Joyner  ************************************************************************/
19108eb6488eSEric Joyner static void
1911c19c7afeSEric Joyner ixgbe_setup_vlan_hw_support(if_ctx_t ctx)
19128eb6488eSEric Joyner {
1913ff06a8dbSJustin Hibbits 	if_t            ifp = iflib_get_ifp(ctx);
1914b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
1915b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
19168eb6488eSEric Joyner 	struct rx_ring  *rxr;
19178eb6488eSEric Joyner 	int             i;
19188eb6488eSEric Joyner 	u32             ctrl;
19198eb6488eSEric Joyner 
19208eb6488eSEric Joyner 
19218eb6488eSEric Joyner 	/*
19228eb6488eSEric Joyner 	 * We get here thru init_locked, meaning
19238eb6488eSEric Joyner 	 * a soft reset, this has already cleared
19248eb6488eSEric Joyner 	 * the VFTA and other state, so if there
19258eb6488eSEric Joyner 	 * have been no vlan's registered do nothing.
19268eb6488eSEric Joyner 	 */
1927ff06a8dbSJustin Hibbits 	if (sc->num_vlans == 0 || (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0) {
1928e7abb897SKristof Provost 		/* Clear the vlan hw flag */
1929e7abb897SKristof Provost 		for (i = 0; i < sc->num_rx_queues; i++) {
1930e7abb897SKristof Provost 			rxr = &sc->rx_queues[i].rxr;
1931e7abb897SKristof Provost 			/* On 82599 the VLAN enable is per/queue in RXDCTL */
1932e7abb897SKristof Provost 			if (hw->mac.type != ixgbe_mac_82598EB) {
1933e7abb897SKristof Provost 				ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
1934e7abb897SKristof Provost 				ctrl &= ~IXGBE_RXDCTL_VME;
1935e7abb897SKristof Provost 				IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), ctrl);
1936e7abb897SKristof Provost 			}
1937e7abb897SKristof Provost 			rxr->vtag_strip = false;
1938e7abb897SKristof Provost 		}
1939e7abb897SKristof Provost 		ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1940e7abb897SKristof Provost 		/* Enable the Filter Table if enabled */
1941e7abb897SKristof Provost 		ctrl |= IXGBE_VLNCTRL_CFIEN;
1942e7abb897SKristof Provost 		ctrl &= ~IXGBE_VLNCTRL_VFE;
1943e7abb897SKristof Provost 		if (hw->mac.type == ixgbe_mac_82598EB)
1944e7abb897SKristof Provost 			ctrl &= ~IXGBE_VLNCTRL_VME;
1945e7abb897SKristof Provost 		IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
19468eb6488eSEric Joyner 		return;
1947e7abb897SKristof Provost 	}
19488eb6488eSEric Joyner 
19498eb6488eSEric Joyner 	/* Setup the queues for vlans */
1950ff06a8dbSJustin Hibbits 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) {
1951b1d5caf3SKevin Bowling 		for (i = 0; i < sc->num_rx_queues; i++) {
1952b1d5caf3SKevin Bowling 			rxr = &sc->rx_queues[i].rxr;
19538eb6488eSEric Joyner 			/* On 82599 the VLAN enable is per/queue in RXDCTL */
19548eb6488eSEric Joyner 			if (hw->mac.type != ixgbe_mac_82598EB) {
19558eb6488eSEric Joyner 				ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
19568eb6488eSEric Joyner 				ctrl |= IXGBE_RXDCTL_VME;
19578eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), ctrl);
19588eb6488eSEric Joyner 			}
195979b36ec9SKevin Bowling 			rxr->vtag_strip = true;
1960758cc3dcSJack F Vogel 		}
19619c92bb71SSean Bruno 	}
1962758cc3dcSJack F Vogel 
1963ff06a8dbSJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) == 0)
19648eb6488eSEric Joyner 		return;
19658eb6488eSEric Joyner 	/*
19668eb6488eSEric Joyner 	 * A soft reset zero's out the VFTA, so
19678eb6488eSEric Joyner 	 * we need to repopulate it now.
19688eb6488eSEric Joyner 	 */
19698eb6488eSEric Joyner 	for (i = 0; i < IXGBE_VFTA_SIZE; i++)
1970b1d5caf3SKevin Bowling 		if (sc->shadow_vfta[i] != 0)
19718eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(i),
1972b1d5caf3SKevin Bowling 			    sc->shadow_vfta[i]);
19738eb6488eSEric Joyner 
19748eb6488eSEric Joyner 	ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
19758eb6488eSEric Joyner 	/* Enable the Filter Table if enabled */
1976ff06a8dbSJustin Hibbits 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) {
19778eb6488eSEric Joyner 		ctrl &= ~IXGBE_VLNCTRL_CFIEN;
19788eb6488eSEric Joyner 		ctrl |= IXGBE_VLNCTRL_VFE;
19798eb6488eSEric Joyner 	}
19808eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
19818eb6488eSEric Joyner 		ctrl |= IXGBE_VLNCTRL_VME;
19828eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
19838eb6488eSEric Joyner } /* ixgbe_setup_vlan_hw_support */
19848eb6488eSEric Joyner 
19858eb6488eSEric Joyner /************************************************************************
19868eb6488eSEric Joyner  * ixgbe_get_slot_info
19878eb6488eSEric Joyner  *
19888eb6488eSEric Joyner  *   Get the width and transaction speed of
19898eb6488eSEric Joyner  *   the slot this adapter is plugged into.
19908eb6488eSEric Joyner  ************************************************************************/
19918eb6488eSEric Joyner static void
1992b1d5caf3SKevin Bowling ixgbe_get_slot_info(struct ixgbe_softc *sc)
19938eb6488eSEric Joyner {
1994b1d5caf3SKevin Bowling 	device_t        dev = iflib_get_dev(sc->ctx);
1995b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
199679b36ec9SKevin Bowling 	int             bus_info_valid = true;
19978eb6488eSEric Joyner 	u32             offset;
19988eb6488eSEric Joyner 	u16             link;
19998eb6488eSEric Joyner 
20008eb6488eSEric Joyner 	/* Some devices are behind an internal bridge */
20018eb6488eSEric Joyner 	switch (hw->device_id) {
20028eb6488eSEric Joyner 	case IXGBE_DEV_ID_82599_SFP_SF_QP:
20038eb6488eSEric Joyner 	case IXGBE_DEV_ID_82599_QSFP_SF_QP:
20048eb6488eSEric Joyner 		goto get_parent_info;
20058eb6488eSEric Joyner 	default:
20068eb6488eSEric Joyner 		break;
20078eb6488eSEric Joyner 	}
20088eb6488eSEric Joyner 
20098eb6488eSEric Joyner 	ixgbe_get_bus_info(hw);
20108eb6488eSEric Joyner 
20118eb6488eSEric Joyner 	/*
20128eb6488eSEric Joyner 	 * Some devices don't use PCI-E, but there is no need
20138eb6488eSEric Joyner 	 * to display "Unknown" for bus speed and width.
20148eb6488eSEric Joyner 	 */
20158eb6488eSEric Joyner 	switch (hw->mac.type) {
20168eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
20178eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
20188eb6488eSEric Joyner 		return;
20198eb6488eSEric Joyner 	default:
20208eb6488eSEric Joyner 		goto display;
20218eb6488eSEric Joyner 	}
20228eb6488eSEric Joyner 
20238eb6488eSEric Joyner get_parent_info:
20248eb6488eSEric Joyner 	/*
20258eb6488eSEric Joyner 	 * For the Quad port adapter we need to parse back
20268eb6488eSEric Joyner 	 * up the PCI tree to find the speed of the expansion
20278eb6488eSEric Joyner 	 * slot into which this adapter is plugged. A bit more work.
20288eb6488eSEric Joyner 	 */
20298eb6488eSEric Joyner 	dev = device_get_parent(device_get_parent(dev));
20308eb6488eSEric Joyner #ifdef IXGBE_DEBUG
20318eb6488eSEric Joyner 	device_printf(dev, "parent pcib = %x,%x,%x\n", pci_get_bus(dev),
20328eb6488eSEric Joyner 	    pci_get_slot(dev), pci_get_function(dev));
20338eb6488eSEric Joyner #endif
20348eb6488eSEric Joyner 	dev = device_get_parent(device_get_parent(dev));
20358eb6488eSEric Joyner #ifdef IXGBE_DEBUG
20368eb6488eSEric Joyner 	device_printf(dev, "slot pcib = %x,%x,%x\n", pci_get_bus(dev),
20378eb6488eSEric Joyner 	    pci_get_slot(dev), pci_get_function(dev));
20388eb6488eSEric Joyner #endif
20398eb6488eSEric Joyner 	/* Now get the PCI Express Capabilities offset */
20408eb6488eSEric Joyner 	if (pci_find_cap(dev, PCIY_EXPRESS, &offset)) {
20418eb6488eSEric Joyner 		/*
20428eb6488eSEric Joyner 		 * Hmm...can't get PCI-Express capabilities.
20438eb6488eSEric Joyner 		 * Falling back to default method.
20448eb6488eSEric Joyner 		 */
204579b36ec9SKevin Bowling 		bus_info_valid = false;
20468eb6488eSEric Joyner 		ixgbe_get_bus_info(hw);
20478eb6488eSEric Joyner 		goto display;
20488eb6488eSEric Joyner 	}
20498eb6488eSEric Joyner 	/* ...and read the Link Status Register */
20508eb6488eSEric Joyner 	link = pci_read_config(dev, offset + PCIER_LINK_STA, 2);
20518eb6488eSEric Joyner 	ixgbe_set_pci_config_data_generic(hw, link);
20528eb6488eSEric Joyner 
20538eb6488eSEric Joyner display:
20548eb6488eSEric Joyner 	device_printf(dev, "PCI Express Bus: Speed %s %s\n",
20558eb6488eSEric Joyner 	    ((hw->bus.speed == ixgbe_bus_speed_8000)    ? "8.0GT/s"  :
20568eb6488eSEric Joyner 	     (hw->bus.speed == ixgbe_bus_speed_5000)    ? "5.0GT/s"  :
20578eb6488eSEric Joyner 	     (hw->bus.speed == ixgbe_bus_speed_2500)    ? "2.5GT/s"  :
20588eb6488eSEric Joyner 	     "Unknown"),
20598eb6488eSEric Joyner 	    ((hw->bus.width == ixgbe_bus_width_pcie_x8) ? "Width x8" :
20608eb6488eSEric Joyner 	     (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "Width x4" :
20618eb6488eSEric Joyner 	     (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "Width x1" :
20628eb6488eSEric Joyner 	     "Unknown"));
20638eb6488eSEric Joyner 
20648eb6488eSEric Joyner 	if (bus_info_valid) {
20658eb6488eSEric Joyner 		if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
20668eb6488eSEric Joyner 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
20678eb6488eSEric Joyner 		    (hw->bus.speed == ixgbe_bus_speed_2500))) {
20688eb6488eSEric Joyner 			device_printf(dev, "PCI-Express bandwidth available for this card\n     is not sufficient for optimal performance.\n");
20698eb6488eSEric Joyner 			device_printf(dev, "For optimal performance a x8 PCIE, or x4 PCIE Gen2 slot is required.\n");
20708eb6488eSEric Joyner 		}
20718eb6488eSEric Joyner 		if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
20728eb6488eSEric Joyner 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
20738eb6488eSEric Joyner 		    (hw->bus.speed < ixgbe_bus_speed_8000))) {
20748eb6488eSEric Joyner 			device_printf(dev, "PCI-Express bandwidth available for this card\n     is not sufficient for optimal performance.\n");
20758eb6488eSEric Joyner 			device_printf(dev, "For optimal performance a x8 PCIE Gen3 slot is required.\n");
20768eb6488eSEric Joyner 		}
20778eb6488eSEric Joyner 	} else
20788eb6488eSEric Joyner 		device_printf(dev, "Unable to determine slot speed/width. The speed/width reported are that of the internal switch.\n");
20798eb6488eSEric Joyner 
20808eb6488eSEric Joyner 	return;
20818eb6488eSEric Joyner } /* ixgbe_get_slot_info */
20828eb6488eSEric Joyner 
20838eb6488eSEric Joyner /************************************************************************
2084c19c7afeSEric Joyner  * ixgbe_if_msix_intr_assign
2085c19c7afeSEric Joyner  *
2086c19c7afeSEric Joyner  *   Setup MSI-X Interrupt resources and handlers
20878eb6488eSEric Joyner  ************************************************************************/
2088c19c7afeSEric Joyner static int
2089c19c7afeSEric Joyner ixgbe_if_msix_intr_assign(if_ctx_t ctx, int msix)
20908eb6488eSEric Joyner {
2091b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
2092b1d5caf3SKevin Bowling 	struct ix_rx_queue *rx_que = sc->rx_queues;
2093c19c7afeSEric Joyner 	struct ix_tx_queue *tx_que;
2094c19c7afeSEric Joyner 	int                error, rid, vector = 0;
2095c19c7afeSEric Joyner 	char               buf[16];
20968eb6488eSEric Joyner 
2097c19c7afeSEric Joyner 	/* Admin Que is vector 0*/
2098c19c7afeSEric Joyner 	rid = vector + 1;
2099b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, vector++, rx_que++) {
2100c19c7afeSEric Joyner 		rid = vector + 1;
2101c19c7afeSEric Joyner 
2102c19c7afeSEric Joyner 		snprintf(buf, sizeof(buf), "rxq%d", i);
2103c19c7afeSEric Joyner 		error = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
210481be6552SMatt Macy 		    IFLIB_INTR_RXTX, ixgbe_msix_que, rx_que, rx_que->rxr.me, buf);
2105c19c7afeSEric Joyner 
2106c19c7afeSEric Joyner 		if (error) {
2107c19c7afeSEric Joyner 			device_printf(iflib_get_dev(ctx),
2108c19c7afeSEric Joyner 			    "Failed to allocate que int %d err: %d", i, error);
2109b1d5caf3SKevin Bowling 			sc->num_rx_queues = i + 1;
2110c19c7afeSEric Joyner 			goto fail;
21118eb6488eSEric Joyner 		}
21128eb6488eSEric Joyner 
2113c19c7afeSEric Joyner 		rx_que->msix = vector;
2114c19c7afeSEric Joyner 	}
2115b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_tx_queues; i++) {
2116c19c7afeSEric Joyner 		snprintf(buf, sizeof(buf), "txq%d", i);
2117b1d5caf3SKevin Bowling 		tx_que = &sc->tx_queues[i];
2118b1d5caf3SKevin Bowling 		tx_que->msix = i % sc->num_rx_queues;
2119c19c7afeSEric Joyner 		iflib_softirq_alloc_generic(ctx,
2120b1d5caf3SKevin Bowling 		    &sc->rx_queues[tx_que->msix].que_irq,
2121c19c7afeSEric Joyner 		    IFLIB_INTR_TX, tx_que, tx_que->txr.me, buf);
2122c19c7afeSEric Joyner 	}
2123c19c7afeSEric Joyner 	rid = vector + 1;
2124b1d5caf3SKevin Bowling 	error = iflib_irq_alloc_generic(ctx, &sc->irq, rid,
2125b1d5caf3SKevin Bowling 	    IFLIB_INTR_ADMIN, ixgbe_msix_link, sc, 0, "aq");
2126c19c7afeSEric Joyner 	if (error) {
2127c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
2128c19c7afeSEric Joyner 		    "Failed to register admin handler");
2129c19c7afeSEric Joyner 		return (error);
2130c19c7afeSEric Joyner 	}
2131c19c7afeSEric Joyner 
2132b1d5caf3SKevin Bowling 	sc->vector = vector;
2133c19c7afeSEric Joyner 
2134c19c7afeSEric Joyner 	return (0);
2135c19c7afeSEric Joyner fail:
2136b1d5caf3SKevin Bowling 	iflib_irq_free(ctx, &sc->irq);
2137b1d5caf3SKevin Bowling 	rx_que = sc->rx_queues;
2138b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, rx_que++)
2139c19c7afeSEric Joyner 		iflib_irq_free(ctx, &rx_que->que_irq);
2140c19c7afeSEric Joyner 
2141c19c7afeSEric Joyner 	return (error);
2142c19c7afeSEric Joyner } /* ixgbe_if_msix_intr_assign */
2143c19c7afeSEric Joyner 
214464881da4SSai Rajesh Tallamraju static inline void
2145b1d5caf3SKevin Bowling ixgbe_perform_aim(struct ixgbe_softc *sc, struct ix_rx_queue *que)
214664881da4SSai Rajesh Tallamraju {
214764881da4SSai Rajesh Tallamraju 	uint32_t newitr = 0;
214864881da4SSai Rajesh Tallamraju 	struct rx_ring *rxr = &que->rxr;
214964881da4SSai Rajesh Tallamraju 
215064881da4SSai Rajesh Tallamraju 	/*
215164881da4SSai Rajesh Tallamraju 	 * Do Adaptive Interrupt Moderation:
215264881da4SSai Rajesh Tallamraju 	 *  - Write out last calculated setting
215364881da4SSai Rajesh Tallamraju 	 *  - Calculate based on average size over
215464881da4SSai Rajesh Tallamraju 	 *    the last interval.
215564881da4SSai Rajesh Tallamraju 	 */
215664881da4SSai Rajesh Tallamraju 	if (que->eitr_setting) {
2157b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EITR(que->msix),
215864881da4SSai Rajesh Tallamraju 		    que->eitr_setting);
215964881da4SSai Rajesh Tallamraju 	}
216064881da4SSai Rajesh Tallamraju 
216164881da4SSai Rajesh Tallamraju 	que->eitr_setting = 0;
216264881da4SSai Rajesh Tallamraju 	/* Idle, do nothing */
216364881da4SSai Rajesh Tallamraju 	if (rxr->bytes == 0) {
216464881da4SSai Rajesh Tallamraju 		return;
216564881da4SSai Rajesh Tallamraju 	}
216664881da4SSai Rajesh Tallamraju 
216764881da4SSai Rajesh Tallamraju 	if ((rxr->bytes) && (rxr->packets)) {
216864881da4SSai Rajesh Tallamraju 		newitr = (rxr->bytes / rxr->packets);
216964881da4SSai Rajesh Tallamraju 	}
217064881da4SSai Rajesh Tallamraju 
217164881da4SSai Rajesh Tallamraju 	newitr += 24; /* account for hardware frame, crc */
217264881da4SSai Rajesh Tallamraju 	/* set an upper boundary */
217364881da4SSai Rajesh Tallamraju 	newitr = min(newitr, 3000);
217464881da4SSai Rajesh Tallamraju 
217564881da4SSai Rajesh Tallamraju 	/* Be nice to the mid range */
217664881da4SSai Rajesh Tallamraju 	if ((newitr > 300) && (newitr < 1200)) {
217764881da4SSai Rajesh Tallamraju 		newitr = (newitr / 3);
217864881da4SSai Rajesh Tallamraju 	} else {
217964881da4SSai Rajesh Tallamraju 		newitr = (newitr / 2);
218064881da4SSai Rajesh Tallamraju 	}
218164881da4SSai Rajesh Tallamraju 
2182b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
218364881da4SSai Rajesh Tallamraju 		newitr |= newitr << 16;
218464881da4SSai Rajesh Tallamraju 	} else {
218564881da4SSai Rajesh Tallamraju 		newitr |= IXGBE_EITR_CNT_WDIS;
218664881da4SSai Rajesh Tallamraju 	}
218764881da4SSai Rajesh Tallamraju 
218864881da4SSai Rajesh Tallamraju 	/* save for next interrupt */
218964881da4SSai Rajesh Tallamraju 	que->eitr_setting = newitr;
219064881da4SSai Rajesh Tallamraju 
219164881da4SSai Rajesh Tallamraju 	/* Reset state */
219264881da4SSai Rajesh Tallamraju 	rxr->bytes = 0;
219364881da4SSai Rajesh Tallamraju 	rxr->packets = 0;
219464881da4SSai Rajesh Tallamraju 
219564881da4SSai Rajesh Tallamraju 	return;
219664881da4SSai Rajesh Tallamraju }
219764881da4SSai Rajesh Tallamraju 
2198c19c7afeSEric Joyner /*********************************************************************
21998eb6488eSEric Joyner  * ixgbe_msix_que - MSI-X Queue Interrupt Service routine
2200c19c7afeSEric Joyner  **********************************************************************/
2201c19c7afeSEric Joyner static int
22028eb6488eSEric Joyner ixgbe_msix_que(void *arg)
22038eb6488eSEric Joyner {
2204c19c7afeSEric Joyner 	struct ix_rx_queue *que = arg;
2205b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = que->sc;
2206ff06a8dbSJustin Hibbits 	if_t               ifp = iflib_get_ifp(que->sc->ctx);
22078eb6488eSEric Joyner 
22088eb6488eSEric Joyner 	/* Protect against spurious interrupts */
2209ff06a8dbSJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
22106c351041SEric Joyner 		return (FILTER_HANDLED);
22118eb6488eSEric Joyner 
2212b1d5caf3SKevin Bowling 	ixgbe_disable_queue(sc, que->msix);
22138eb6488eSEric Joyner 	++que->irqs;
22148eb6488eSEric Joyner 
221564881da4SSai Rajesh Tallamraju 	/* Check for AIM */
2216b1d5caf3SKevin Bowling 	if (sc->enable_aim) {
2217b1d5caf3SKevin Bowling 		ixgbe_perform_aim(sc, que);
221864881da4SSai Rajesh Tallamraju 	}
221964881da4SSai Rajesh Tallamraju 
2220c19c7afeSEric Joyner 	return (FILTER_SCHEDULE_THREAD);
22218eb6488eSEric Joyner } /* ixgbe_msix_que */
22228eb6488eSEric Joyner 
22238eb6488eSEric Joyner /************************************************************************
22248eb6488eSEric Joyner  * ixgbe_media_status - Media Ioctl callback
22258eb6488eSEric Joyner  *
22268eb6488eSEric Joyner  *   Called whenever the user queries the status of
22278eb6488eSEric Joyner  *   the interface using ifconfig.
22288eb6488eSEric Joyner  ************************************************************************/
22298eb6488eSEric Joyner static void
2230c19c7afeSEric Joyner ixgbe_if_media_status(if_ctx_t ctx, struct ifmediareq * ifmr)
22318eb6488eSEric Joyner {
2232b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2233b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
22348eb6488eSEric Joyner 	int             layer;
22358eb6488eSEric Joyner 
2236c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_media_status: begin");
2237c19c7afeSEric Joyner 
22388eb6488eSEric Joyner 	ifmr->ifm_status = IFM_AVALID;
22398eb6488eSEric Joyner 	ifmr->ifm_active = IFM_ETHER;
22408eb6488eSEric Joyner 
2241b1d5caf3SKevin Bowling 	if (!sc->link_active)
22428eb6488eSEric Joyner 		return;
22438eb6488eSEric Joyner 
22448eb6488eSEric Joyner 	ifmr->ifm_status |= IFM_ACTIVE;
2245b1d5caf3SKevin Bowling 	layer = sc->phy_layer;
22468eb6488eSEric Joyner 
22478eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T ||
22488eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_T ||
22498eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_100BASE_TX ||
22508eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
2251b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22528eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22538eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
22548eb6488eSEric Joyner 			break;
22558eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22568eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
22578eb6488eSEric Joyner 			break;
22588eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_100_FULL:
22598eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
22608eb6488eSEric Joyner 			break;
22618eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10_FULL:
22628eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10_T | IFM_FDX;
22638eb6488eSEric Joyner 			break;
22648eb6488eSEric Joyner 		}
2265d381c807SPiotr Pietruszewski 	if (hw->mac.type == ixgbe_mac_X550)
2266d381c807SPiotr Pietruszewski 		switch (sc->link_speed) {
2267d381c807SPiotr Pietruszewski 		case IXGBE_LINK_SPEED_5GB_FULL:
2268d381c807SPiotr Pietruszewski 			ifmr->ifm_active |= IFM_5000_T | IFM_FDX;
2269d381c807SPiotr Pietruszewski 			break;
2270d381c807SPiotr Pietruszewski 		case IXGBE_LINK_SPEED_2_5GB_FULL:
2271d381c807SPiotr Pietruszewski 			ifmr->ifm_active |= IFM_2500_T | IFM_FDX;
2272d381c807SPiotr Pietruszewski 			break;
2273d381c807SPiotr Pietruszewski 		}
22748eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
22758eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
2276b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22778eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22788eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_TWINAX | IFM_FDX;
22798eb6488eSEric Joyner 			break;
22808eb6488eSEric Joyner 		}
22818eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR)
2282b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22838eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22848eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_LR | IFM_FDX;
22858eb6488eSEric Joyner 			break;
22868eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22878eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
22888eb6488eSEric Joyner 			break;
22898eb6488eSEric Joyner 		}
22908eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LRM)
2291b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22928eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22938eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_LRM | IFM_FDX;
22948eb6488eSEric Joyner 			break;
22958eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22968eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
22978eb6488eSEric Joyner 			break;
22988eb6488eSEric Joyner 		}
22998eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR ||
23008eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
2301b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23028eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23038eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
23048eb6488eSEric Joyner 			break;
23058eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23068eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
23078eb6488eSEric Joyner 			break;
23088eb6488eSEric Joyner 		}
23098eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
2310b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23118eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23128eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
23138eb6488eSEric Joyner 			break;
23148eb6488eSEric Joyner 		}
23158eb6488eSEric Joyner 	/*
23168eb6488eSEric Joyner 	 * XXX: These need to use the proper media types once
23178eb6488eSEric Joyner 	 * they're added.
23188eb6488eSEric Joyner 	 */
23198eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
23208eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
2321b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23228eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23238eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
23248eb6488eSEric Joyner 			break;
23258eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23268eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
23278eb6488eSEric Joyner 			break;
23288eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23298eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
23308eb6488eSEric Joyner 			break;
23318eb6488eSEric Joyner 		}
23328eb6488eSEric Joyner 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
23338eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
23348eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
2335b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23368eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23378eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
23388eb6488eSEric Joyner 			break;
23398eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23408eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
23418eb6488eSEric Joyner 			break;
23428eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23438eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
23448eb6488eSEric Joyner 			break;
23458eb6488eSEric Joyner 		}
23468eb6488eSEric Joyner #else
23478eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
2348b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23498eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23508eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_KR | IFM_FDX;
23518eb6488eSEric Joyner 			break;
23528eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23538eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
23548eb6488eSEric Joyner 			break;
23558eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23568eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
23578eb6488eSEric Joyner 			break;
23588eb6488eSEric Joyner 		}
23598eb6488eSEric Joyner 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
23608eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
23618eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
2362b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23638eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23648eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_KX4 | IFM_FDX;
23658eb6488eSEric Joyner 			break;
23668eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23678eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
23688eb6488eSEric Joyner 			break;
23698eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23708eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
23718eb6488eSEric Joyner 			break;
23728eb6488eSEric Joyner 		}
23738eb6488eSEric Joyner #endif
23748eb6488eSEric Joyner 
23758eb6488eSEric Joyner 	/* If nothing is recognized... */
23768eb6488eSEric Joyner 	if (IFM_SUBTYPE(ifmr->ifm_active) == 0)
23778eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_UNKNOWN;
23788eb6488eSEric Joyner 
23798eb6488eSEric Joyner 	/* Display current flow control setting used on link */
23808eb6488eSEric Joyner 	if (hw->fc.current_mode == ixgbe_fc_rx_pause ||
23818eb6488eSEric Joyner 	    hw->fc.current_mode == ixgbe_fc_full)
23828eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
23838eb6488eSEric Joyner 	if (hw->fc.current_mode == ixgbe_fc_tx_pause ||
23848eb6488eSEric Joyner 	    hw->fc.current_mode == ixgbe_fc_full)
23858eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
23868eb6488eSEric Joyner } /* ixgbe_media_status */
23878eb6488eSEric Joyner 
23888eb6488eSEric Joyner /************************************************************************
23898eb6488eSEric Joyner  * ixgbe_media_change - Media Ioctl callback
23908eb6488eSEric Joyner  *
23918eb6488eSEric Joyner  *   Called when the user changes speed/duplex using
23928eb6488eSEric Joyner  *   media/mediopt option with ifconfig.
23938eb6488eSEric Joyner  ************************************************************************/
23948eb6488eSEric Joyner static int
2395c19c7afeSEric Joyner ixgbe_if_media_change(if_ctx_t ctx)
23968eb6488eSEric Joyner {
2397b1d5caf3SKevin Bowling 	struct ixgbe_softc   *sc = iflib_get_softc(ctx);
2398c19c7afeSEric Joyner 	struct ifmedia   *ifm = iflib_get_media(ctx);
2399b1d5caf3SKevin Bowling 	struct ixgbe_hw  *hw = &sc->hw;
24008eb6488eSEric Joyner 	ixgbe_link_speed speed = 0;
24018eb6488eSEric Joyner 
2402c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_media_change: begin");
24038eb6488eSEric Joyner 
24048eb6488eSEric Joyner 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
24058eb6488eSEric Joyner 		return (EINVAL);
24068eb6488eSEric Joyner 
24078eb6488eSEric Joyner 	if (hw->phy.media_type == ixgbe_media_type_backplane)
2408c19c7afeSEric Joyner 		return (EPERM);
24098eb6488eSEric Joyner 
24108eb6488eSEric Joyner 	/*
24118eb6488eSEric Joyner 	 * We don't actually need to check against the supported
24128eb6488eSEric Joyner 	 * media types of the adapter; ifmedia will take care of
24138eb6488eSEric Joyner 	 * that for us.
24148eb6488eSEric Joyner 	 */
24158eb6488eSEric Joyner 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
24168eb6488eSEric Joyner 	case IFM_AUTO:
24178eb6488eSEric Joyner 	case IFM_10G_T:
24188eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
24198eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24208eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
24218eb6488eSEric Joyner 		break;
24228eb6488eSEric Joyner 	case IFM_10G_LRM:
24238eb6488eSEric Joyner 	case IFM_10G_LR:
24248eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
24258eb6488eSEric Joyner 	case IFM_10G_SR: /* KR, too */
24268eb6488eSEric Joyner 	case IFM_10G_CX4: /* KX4 */
24278eb6488eSEric Joyner #else
24288eb6488eSEric Joyner 	case IFM_10G_KR:
24298eb6488eSEric Joyner 	case IFM_10G_KX4:
24308eb6488eSEric Joyner #endif
24318eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24328eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
24338eb6488eSEric Joyner 		break;
24348eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
24358eb6488eSEric Joyner 	case IFM_1000_CX: /* KX */
24368eb6488eSEric Joyner #else
24378eb6488eSEric Joyner 	case IFM_1000_KX:
24388eb6488eSEric Joyner #endif
24398eb6488eSEric Joyner 	case IFM_1000_LX:
24408eb6488eSEric Joyner 	case IFM_1000_SX:
24418eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24428eb6488eSEric Joyner 		break;
24438eb6488eSEric Joyner 	case IFM_1000_T:
24448eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
24458eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24468eb6488eSEric Joyner 		break;
24478eb6488eSEric Joyner 	case IFM_10G_TWINAX:
24488eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
24498eb6488eSEric Joyner 		break;
2450d381c807SPiotr Pietruszewski 	case IFM_5000_T:
2451d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
2452d381c807SPiotr Pietruszewski 		break;
2453d381c807SPiotr Pietruszewski 	case IFM_2500_T:
2454d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
2455d381c807SPiotr Pietruszewski 		break;
24568eb6488eSEric Joyner 	case IFM_100_TX:
24578eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
24588eb6488eSEric Joyner 		break;
24598eb6488eSEric Joyner 	case IFM_10_T:
24608eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10_FULL;
24618eb6488eSEric Joyner 		break;
24628eb6488eSEric Joyner 	default:
24638eb6488eSEric Joyner 		goto invalid;
24648eb6488eSEric Joyner 	}
24658eb6488eSEric Joyner 
246679b36ec9SKevin Bowling 	hw->mac.autotry_restart = true;
246779b36ec9SKevin Bowling 	hw->mac.ops.setup_link(hw, speed, true);
2468b1d5caf3SKevin Bowling 	sc->advertise =
2469d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_10GB_FULL)  ? 0x4  : 0) |
2470d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_5GB_FULL)   ? 0x20 : 0) |
2471d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
2472d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_1GB_FULL)   ? 0x2  : 0) |
2473d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_100_FULL)   ? 0x1  : 0) |
2474d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_10_FULL)    ? 0x8  : 0);
24758eb6488eSEric Joyner 
24768eb6488eSEric Joyner 	return (0);
24778eb6488eSEric Joyner 
24788eb6488eSEric Joyner invalid:
2479c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "Invalid media type!\n");
24808eb6488eSEric Joyner 
24818eb6488eSEric Joyner 	return (EINVAL);
2482c19c7afeSEric Joyner } /* ixgbe_if_media_change */
24838eb6488eSEric Joyner 
24848eb6488eSEric Joyner /************************************************************************
24858eb6488eSEric Joyner  * ixgbe_set_promisc
24868eb6488eSEric Joyner  ************************************************************************/
2487c19c7afeSEric Joyner static int
2488c19c7afeSEric Joyner ixgbe_if_promisc_set(if_ctx_t ctx, int flags)
24898eb6488eSEric Joyner {
2490b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2491ff06a8dbSJustin Hibbits 	if_t           ifp = iflib_get_ifp(ctx);
24928eb6488eSEric Joyner 	u32            rctl;
2493c19c7afeSEric Joyner 	int            mcnt = 0;
24948eb6488eSEric Joyner 
2495b1d5caf3SKevin Bowling 	rctl = IXGBE_READ_REG(&sc->hw, IXGBE_FCTRL);
24968eb6488eSEric Joyner 	rctl &= (~IXGBE_FCTRL_UPE);
2497ff06a8dbSJustin Hibbits 	if (if_getflags(ifp) & IFF_ALLMULTI)
24988eb6488eSEric Joyner 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
24998eb6488eSEric Joyner 	else {
2500ba76aa63SGleb Smirnoff 		mcnt = min(if_llmaddr_count(ifp), MAX_NUM_MULTICAST_ADDRESSES);
25018eb6488eSEric Joyner 	}
25028eb6488eSEric Joyner 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
25038eb6488eSEric Joyner 		rctl &= (~IXGBE_FCTRL_MPE);
2504b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
25058eb6488eSEric Joyner 
2506ff06a8dbSJustin Hibbits 	if (if_getflags(ifp) & IFF_PROMISC) {
25078eb6488eSEric Joyner 		rctl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
2508b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
2509ff06a8dbSJustin Hibbits 	} else if (if_getflags(ifp) & IFF_ALLMULTI) {
25108eb6488eSEric Joyner 		rctl |= IXGBE_FCTRL_MPE;
25118eb6488eSEric Joyner 		rctl &= ~IXGBE_FCTRL_UPE;
2512b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
25138eb6488eSEric Joyner 	}
2514c19c7afeSEric Joyner 	return (0);
2515c19c7afeSEric Joyner } /* ixgbe_if_promisc_set */
25168eb6488eSEric Joyner 
25178eb6488eSEric Joyner /************************************************************************
25188eb6488eSEric Joyner  * ixgbe_msix_link - Link status change ISR (MSI/MSI-X)
25198eb6488eSEric Joyner  ************************************************************************/
2520c19c7afeSEric Joyner static int
25218eb6488eSEric Joyner ixgbe_msix_link(void *arg)
25228eb6488eSEric Joyner {
2523b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = arg;
2524b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
25258eb6488eSEric Joyner 	u32             eicr, eicr_mask;
25268eb6488eSEric Joyner 	s32             retval;
25278eb6488eSEric Joyner 
2528b1d5caf3SKevin Bowling 	++sc->link_irq;
25298eb6488eSEric Joyner 
25308eb6488eSEric Joyner 	/* Pause other interrupts */
25318eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_OTHER);
25328eb6488eSEric Joyner 
25338eb6488eSEric Joyner 	/* First get the cause */
25348eb6488eSEric Joyner 	eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
25358eb6488eSEric Joyner 	/* Be sure the queue bits are not cleared */
25368eb6488eSEric Joyner 	eicr &= ~IXGBE_EICR_RTX_QUEUE;
25378eb6488eSEric Joyner 	/* Clear interrupt with write */
25388eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
25398eb6488eSEric Joyner 
25408eb6488eSEric Joyner 	/* Link status change */
25418eb6488eSEric Joyner 	if (eicr & IXGBE_EICR_LSC) {
25428eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
2543b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_LSC;
25448eb6488eSEric Joyner 	}
25458eb6488eSEric Joyner 
2546b1d5caf3SKevin Bowling 	if (sc->hw.mac.type != ixgbe_mac_82598EB) {
2547b1d5caf3SKevin Bowling 		if ((sc->feat_en & IXGBE_FEATURE_FDIR) &&
25488eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_FLOW_DIR)) {
25498eb6488eSEric Joyner 			/* This is probably overkill :) */
2550b1d5caf3SKevin Bowling 			if (!atomic_cmpset_int(&sc->fdir_reinit, 0, 1))
2551c19c7afeSEric Joyner 				return (FILTER_HANDLED);
25528eb6488eSEric Joyner 			/* Disable the interrupt */
2553c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_FLOW_DIR);
2554b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_FDIR;
2555c19c7afeSEric Joyner 		} else
25568eb6488eSEric Joyner 			if (eicr & IXGBE_EICR_ECC) {
2557b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
25584f1d91e4SNeel Chauhan 				   "Received ECC Err, initiating reset\n");
25594f1d91e4SNeel Chauhan 				hw->mac.flags |= ~IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
25604f1d91e4SNeel Chauhan 				ixgbe_reset_hw(hw);
25618eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
25628eb6488eSEric Joyner 			}
25638eb6488eSEric Joyner 
25648eb6488eSEric Joyner 		/* Check for over temp condition */
2565b1d5caf3SKevin Bowling 		if (sc->feat_en & IXGBE_FEATURE_TEMP_SENSOR) {
2566b1d5caf3SKevin Bowling 			switch (sc->hw.mac.type) {
25678eb6488eSEric Joyner 			case ixgbe_mac_X550EM_a:
25688eb6488eSEric Joyner 				if (!(eicr & IXGBE_EICR_GPI_SDP0_X550EM_a))
25698eb6488eSEric Joyner 					break;
25708eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EIMC,
25718eb6488eSEric Joyner 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
25728eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR,
25738eb6488eSEric Joyner 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
25748eb6488eSEric Joyner 				retval = hw->phy.ops.check_overtemp(hw);
25758eb6488eSEric Joyner 				if (retval != IXGBE_ERR_OVERTEMP)
25768eb6488eSEric Joyner 					break;
2577b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2578c19c7afeSEric Joyner 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2579b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2580c19c7afeSEric Joyner 				    "System shutdown required!\n");
25818eb6488eSEric Joyner 				break;
25828eb6488eSEric Joyner 			default:
25838eb6488eSEric Joyner 				if (!(eicr & IXGBE_EICR_TS))
25848eb6488eSEric Joyner 					break;
25858eb6488eSEric Joyner 				retval = hw->phy.ops.check_overtemp(hw);
25868eb6488eSEric Joyner 				if (retval != IXGBE_ERR_OVERTEMP)
25878eb6488eSEric Joyner 					break;
2588b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2589c19c7afeSEric Joyner 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2590b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2591c19c7afeSEric Joyner 				    "System shutdown required!\n");
25928eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
25938eb6488eSEric Joyner 				break;
25948eb6488eSEric Joyner 			}
25958eb6488eSEric Joyner 		}
25968eb6488eSEric Joyner 
25978eb6488eSEric Joyner 		/* Check for VF message */
2598b1d5caf3SKevin Bowling 		if ((sc->feat_en & IXGBE_FEATURE_SRIOV) &&
25998eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_MAILBOX))
2600b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MBX;
26018eb6488eSEric Joyner 	}
26028eb6488eSEric Joyner 
26038eb6488eSEric Joyner 	if (ixgbe_is_sfp(hw)) {
26048eb6488eSEric Joyner 		/* Pluggable optics-related interrupt */
26058eb6488eSEric Joyner 		if (hw->mac.type >= ixgbe_mac_X540)
26068eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
26078eb6488eSEric Joyner 		else
26088eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
26098eb6488eSEric Joyner 
26108eb6488eSEric Joyner 		if (eicr & eicr_mask) {
26118eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
2612b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
26138eb6488eSEric Joyner 		}
26148eb6488eSEric Joyner 
26158eb6488eSEric Joyner 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
26168eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
26178eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
26188eb6488eSEric Joyner 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
2619b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
26208eb6488eSEric Joyner 		}
26218eb6488eSEric Joyner 	}
26228eb6488eSEric Joyner 
26238eb6488eSEric Joyner 	/* Check for fan failure */
2624b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL) {
2625b1d5caf3SKevin Bowling 		ixgbe_check_fan_failure(sc, eicr, true);
26268eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
26278eb6488eSEric Joyner 	}
26288eb6488eSEric Joyner 
26298eb6488eSEric Joyner 	/* External PHY interrupt */
26308eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
26318eb6488eSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP0_X540)) {
26328eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP0_X540);
2633b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_PHY;
26348eb6488eSEric Joyner 	}
26358eb6488eSEric Joyner 
2636b1d5caf3SKevin Bowling 	return (sc->task_requests != 0) ? FILTER_SCHEDULE_THREAD : FILTER_HANDLED;
26378eb6488eSEric Joyner } /* ixgbe_msix_link */
26388eb6488eSEric Joyner 
26398eb6488eSEric Joyner /************************************************************************
26408eb6488eSEric Joyner  * ixgbe_sysctl_interrupt_rate_handler
26418eb6488eSEric Joyner  ************************************************************************/
2642758cc3dcSJack F Vogel static int
2643758cc3dcSJack F Vogel ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)
2644758cc3dcSJack F Vogel {
2645c19c7afeSEric Joyner 	struct ix_rx_queue *que = ((struct ix_rx_queue *)oidp->oid_arg1);
26468eb6488eSEric Joyner 	int                error;
2647758cc3dcSJack F Vogel 	unsigned int       reg, usec, rate;
2648758cc3dcSJack F Vogel 
2649b1d5caf3SKevin Bowling 	reg = IXGBE_READ_REG(&que->sc->hw, IXGBE_EITR(que->msix));
2650758cc3dcSJack F Vogel 	usec = ((reg & 0x0FF8) >> 3);
2651758cc3dcSJack F Vogel 	if (usec > 0)
2652758cc3dcSJack F Vogel 		rate = 500000 / usec;
2653758cc3dcSJack F Vogel 	else
2654758cc3dcSJack F Vogel 		rate = 0;
2655758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &rate, 0, req);
2656758cc3dcSJack F Vogel 	if (error || !req->newptr)
2657758cc3dcSJack F Vogel 		return error;
2658758cc3dcSJack F Vogel 	reg &= ~0xfff; /* default, no limitation */
2659758cc3dcSJack F Vogel 	ixgbe_max_interrupt_rate = 0;
2660758cc3dcSJack F Vogel 	if (rate > 0 && rate < 500000) {
2661758cc3dcSJack F Vogel 		if (rate < 1000)
2662758cc3dcSJack F Vogel 			rate = 1000;
2663758cc3dcSJack F Vogel 		ixgbe_max_interrupt_rate = rate;
2664758cc3dcSJack F Vogel 		reg |= ((4000000/rate) & 0xff8);
2665758cc3dcSJack F Vogel 	}
2666b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&que->sc->hw, IXGBE_EITR(que->msix), reg);
2667758cc3dcSJack F Vogel 
26688eb6488eSEric Joyner 	return (0);
26698eb6488eSEric Joyner } /* ixgbe_sysctl_interrupt_rate_handler */
26708eb6488eSEric Joyner 
26718eb6488eSEric Joyner /************************************************************************
26728eb6488eSEric Joyner  * ixgbe_add_device_sysctls
26738eb6488eSEric Joyner  ************************************************************************/
26746f37f232SEric Joyner static void
2675c19c7afeSEric Joyner ixgbe_add_device_sysctls(if_ctx_t ctx)
26766f37f232SEric Joyner {
2677b1d5caf3SKevin Bowling 	struct ixgbe_softc         *sc = iflib_get_softc(ctx);
2678c19c7afeSEric Joyner 	device_t               dev = iflib_get_dev(ctx);
2679b1d5caf3SKevin Bowling 	struct ixgbe_hw        *hw = &sc->hw;
26806f37f232SEric Joyner 	struct sysctl_oid_list *child;
2681c19c7afeSEric Joyner 	struct sysctl_ctx_list *ctx_list;
26826f37f232SEric Joyner 
2683c19c7afeSEric Joyner 	ctx_list = device_get_sysctl_ctx(dev);
26846f37f232SEric Joyner 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
26856f37f232SEric Joyner 
26866f37f232SEric Joyner 	/* Sysctls for all devices */
2687c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fc",
268820b91f0aSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2689b1d5caf3SKevin Bowling 	    sc, 0, ixgbe_sysctl_flowcntl, "I",
2690c19c7afeSEric Joyner 	    IXGBE_SYSCTL_DESC_SET_FC);
26916f37f232SEric Joyner 
2692c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "advertise_speed",
269320b91f0aSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2694b1d5caf3SKevin Bowling 	    sc, 0, ixgbe_sysctl_advertise, "I",
26958eb6488eSEric Joyner 	    IXGBE_SYSCTL_DESC_ADV_SPEED);
26966f37f232SEric Joyner 
2697b1d5caf3SKevin Bowling 	sc->enable_aim = ixgbe_enable_aim;
269864881da4SSai Rajesh Tallamraju 	SYSCTL_ADD_INT(ctx_list, child, OID_AUTO, "enable_aim", CTLFLAG_RW,
2699b1d5caf3SKevin Bowling 	    &sc->enable_aim, 0, "Interrupt Moderation");
270064881da4SSai Rajesh Tallamraju 
27017660e4eaSKevin Bowling 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fw_version",
2702b1d5caf3SKevin Bowling 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
27037660e4eaSKevin Bowling 	    ixgbe_sysctl_print_fw_version, "A", "Prints FW/NVM Versions");
27047660e4eaSKevin Bowling 
2705a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
2706a9ca1c79SSean Bruno 	/* testing sysctls (for all devices) */
2707c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "power_state",
270820b91f0aSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2709b1d5caf3SKevin Bowling 	    sc, 0, ixgbe_sysctl_power_state,
27108eb6488eSEric Joyner 	    "I", "PCI Power State");
2711a9ca1c79SSean Bruno 
2712c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "print_rss_config",
2713b1d5caf3SKevin Bowling 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
2714a9ca1c79SSean Bruno 	    ixgbe_sysctl_print_rss_config, "A", "Prints RSS Configuration");
2715a9ca1c79SSean Bruno #endif
2716a9ca1c79SSean Bruno 	/* for X550 series devices */
27176f37f232SEric Joyner 	if (hw->mac.type >= ixgbe_mac_X550)
2718c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "dmac",
271920b91f0aSPawel Biernacki 		    CTLTYPE_U16 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2720b1d5caf3SKevin Bowling 		    sc, 0, ixgbe_sysctl_dmac,
27218eb6488eSEric Joyner 		    "I", "DMA Coalesce");
27226f37f232SEric Joyner 
2723a9ca1c79SSean Bruno 	/* for WoL-capable devices */
2724a9ca1c79SSean Bruno 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
2725c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wol_enable",
2726b1d5caf3SKevin Bowling 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
27278eb6488eSEric Joyner 		    ixgbe_sysctl_wol_enable, "I", "Enable/Disable Wake on LAN");
27286f37f232SEric Joyner 
2729c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wufc",
273020b91f0aSPawel Biernacki 		    CTLTYPE_U32 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2731b1d5caf3SKevin Bowling 		    sc, 0, ixgbe_sysctl_wufc,
27328eb6488eSEric Joyner 		    "I", "Enable/Disable Wake Up Filters");
27336f37f232SEric Joyner 	}
27346f37f232SEric Joyner 
2735a9ca1c79SSean Bruno 	/* for X552/X557-AT devices */
27366f37f232SEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
27376f37f232SEric Joyner 		struct sysctl_oid *phy_node;
27386f37f232SEric Joyner 		struct sysctl_oid_list *phy_list;
27396f37f232SEric Joyner 
2740c19c7afeSEric Joyner 		phy_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "phy",
274120b91f0aSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "External PHY sysctls");
27426f37f232SEric Joyner 		phy_list = SYSCTL_CHILDREN(phy_node);
27436f37f232SEric Joyner 
2744c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO, "temp",
274520b91f0aSPawel Biernacki 		    CTLTYPE_U16 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
2746b1d5caf3SKevin Bowling 		    sc, 0, ixgbe_sysctl_phy_temp,
27478eb6488eSEric Joyner 		    "I", "Current External PHY Temperature (Celsius)");
27486f37f232SEric Joyner 
2749c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO,
275020b91f0aSPawel Biernacki 		    "overtemp_occurred",
2751b1d5caf3SKevin Bowling 		    CTLTYPE_U16 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
27526f37f232SEric Joyner 		    ixgbe_sysctl_phy_overtemp_occurred, "I",
27536f37f232SEric Joyner 		    "External PHY High Temperature Event Occurred");
27546f37f232SEric Joyner 	}
27558eb6488eSEric Joyner 
2756b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_EEE) {
2757c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "eee_state",
2758b1d5caf3SKevin Bowling 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
27598eb6488eSEric Joyner 		    ixgbe_sysctl_eee_state, "I", "EEE Power Save State");
27608eb6488eSEric Joyner 	}
27618eb6488eSEric Joyner } /* ixgbe_add_device_sysctls */
27628eb6488eSEric Joyner 
27638eb6488eSEric Joyner /************************************************************************
27648eb6488eSEric Joyner  * ixgbe_allocate_pci_resources
27658eb6488eSEric Joyner  ************************************************************************/
27668eb6488eSEric Joyner static int
2767c19c7afeSEric Joyner ixgbe_allocate_pci_resources(if_ctx_t ctx)
27688eb6488eSEric Joyner {
2769b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2770c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
27718eb6488eSEric Joyner 	int             rid;
27728eb6488eSEric Joyner 
27738eb6488eSEric Joyner 	rid = PCIR_BAR(0);
2774b1d5caf3SKevin Bowling 	sc->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
27758eb6488eSEric Joyner 	    RF_ACTIVE);
27768eb6488eSEric Joyner 
2777b1d5caf3SKevin Bowling 	if (!(sc->pci_mem)) {
27788eb6488eSEric Joyner 		device_printf(dev, "Unable to allocate bus resource: memory\n");
27798eb6488eSEric Joyner 		return (ENXIO);
27808eb6488eSEric Joyner 	}
27818eb6488eSEric Joyner 
27828eb6488eSEric Joyner 	/* Save bus_space values for READ/WRITE_REG macros */
2783b1d5caf3SKevin Bowling 	sc->osdep.mem_bus_space_tag = rman_get_bustag(sc->pci_mem);
2784b1d5caf3SKevin Bowling 	sc->osdep.mem_bus_space_handle =
2785b1d5caf3SKevin Bowling 	    rman_get_bushandle(sc->pci_mem);
27868eb6488eSEric Joyner 	/* Set hw values for shared code */
2787b1d5caf3SKevin Bowling 	sc->hw.hw_addr = (u8 *)&sc->osdep.mem_bus_space_handle;
27888eb6488eSEric Joyner 
27898eb6488eSEric Joyner 	return (0);
27908eb6488eSEric Joyner } /* ixgbe_allocate_pci_resources */
27918eb6488eSEric Joyner 
27928eb6488eSEric Joyner /************************************************************************
27938eb6488eSEric Joyner  * ixgbe_detach - Device removal routine
27948eb6488eSEric Joyner  *
27958eb6488eSEric Joyner  *   Called when the driver is being removed.
27968eb6488eSEric Joyner  *   Stops the adapter and deallocates all the resources
27978eb6488eSEric Joyner  *   that were allocated for driver operation.
27988eb6488eSEric Joyner  *
27998eb6488eSEric Joyner  *   return 0 on success, positive on failure
28008eb6488eSEric Joyner  ************************************************************************/
28018eb6488eSEric Joyner static int
2802c19c7afeSEric Joyner ixgbe_if_detach(if_ctx_t ctx)
28038eb6488eSEric Joyner {
2804b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2805c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
28068eb6488eSEric Joyner 	u32            ctrl_ext;
28078eb6488eSEric Joyner 
28088eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_detach: begin");
28098eb6488eSEric Joyner 
28108eb6488eSEric Joyner 	if (ixgbe_pci_iov_detach(dev) != 0) {
28118eb6488eSEric Joyner 		device_printf(dev, "SR-IOV in use; detach first.\n");
28128eb6488eSEric Joyner 		return (EBUSY);
28138eb6488eSEric Joyner 	}
28148eb6488eSEric Joyner 
2815c19c7afeSEric Joyner 	ixgbe_setup_low_power_mode(ctx);
28168eb6488eSEric Joyner 
28178eb6488eSEric Joyner 	/* let hardware know driver is unloading */
2818b1d5caf3SKevin Bowling 	ctrl_ext = IXGBE_READ_REG(&sc->hw, IXGBE_CTRL_EXT);
28198eb6488eSEric Joyner 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
2820b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_CTRL_EXT, ctrl_ext);
28218eb6488eSEric Joyner 
2822c19c7afeSEric Joyner 	ixgbe_free_pci_resources(ctx);
2823b1d5caf3SKevin Bowling 	free(sc->mta, M_IXGBE);
28248eb6488eSEric Joyner 
28258eb6488eSEric Joyner 	return (0);
2826c19c7afeSEric Joyner } /* ixgbe_if_detach */
28278eb6488eSEric Joyner 
28288eb6488eSEric Joyner /************************************************************************
28298eb6488eSEric Joyner  * ixgbe_setup_low_power_mode - LPLU/WoL preparation
28308eb6488eSEric Joyner  *
28318eb6488eSEric Joyner  *   Prepare the adapter/port for LPLU and/or WoL
28328eb6488eSEric Joyner  ************************************************************************/
28338eb6488eSEric Joyner static int
2834c19c7afeSEric Joyner ixgbe_setup_low_power_mode(if_ctx_t ctx)
28358eb6488eSEric Joyner {
2836b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2837b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
2838c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
28398eb6488eSEric Joyner 	s32             error = 0;
28408eb6488eSEric Joyner 
2841c19c7afeSEric Joyner 	if (!hw->wol_enabled)
284279b36ec9SKevin Bowling 		ixgbe_set_phy_power(hw, false);
28438eb6488eSEric Joyner 
28448eb6488eSEric Joyner 	/* Limit power management flow to X550EM baseT */
28458eb6488eSEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T &&
28468eb6488eSEric Joyner 	    hw->phy.ops.enter_lplu) {
28478eb6488eSEric Joyner 		/* Turn off support for APM wakeup. (Using ACPI instead) */
28488eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_GRC,
28498eb6488eSEric Joyner 		    IXGBE_READ_REG(hw, IXGBE_GRC) & ~(u32)2);
28508eb6488eSEric Joyner 
28518eb6488eSEric Joyner 		/*
28528eb6488eSEric Joyner 		 * Clear Wake Up Status register to prevent any previous wakeup
28538eb6488eSEric Joyner 		 * events from waking us up immediately after we suspend.
28548eb6488eSEric Joyner 		 */
28558eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
28568eb6488eSEric Joyner 
28578eb6488eSEric Joyner 		/*
28588eb6488eSEric Joyner 		 * Program the Wakeup Filter Control register with user filter
28598eb6488eSEric Joyner 		 * settings
28608eb6488eSEric Joyner 		 */
2861b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, sc->wufc);
28628eb6488eSEric Joyner 
28638eb6488eSEric Joyner 		/* Enable wakeups and power management in Wakeup Control */
28648eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_WUC,
28658eb6488eSEric Joyner 		    IXGBE_WUC_WKEN | IXGBE_WUC_PME_EN);
28668eb6488eSEric Joyner 
28678eb6488eSEric Joyner 		/* X550EM baseT adapters need a special LPLU flow */
286879b36ec9SKevin Bowling 		hw->phy.reset_disable = true;
2869c19c7afeSEric Joyner 		ixgbe_if_stop(ctx);
28708eb6488eSEric Joyner 		error = hw->phy.ops.enter_lplu(hw);
28718eb6488eSEric Joyner 		if (error)
28728eb6488eSEric Joyner 			device_printf(dev, "Error entering LPLU: %d\n", error);
287379b36ec9SKevin Bowling 		hw->phy.reset_disable = false;
28748eb6488eSEric Joyner 	} else {
28758eb6488eSEric Joyner 		/* Just stop for other adapters */
2876c19c7afeSEric Joyner 		ixgbe_if_stop(ctx);
28778eb6488eSEric Joyner 	}
28788eb6488eSEric Joyner 
28798eb6488eSEric Joyner 	return error;
28808eb6488eSEric Joyner } /* ixgbe_setup_low_power_mode */
28818eb6488eSEric Joyner 
28828eb6488eSEric Joyner /************************************************************************
28838eb6488eSEric Joyner  * ixgbe_shutdown - Shutdown entry point
28848eb6488eSEric Joyner  ************************************************************************/
28858eb6488eSEric Joyner static int
2886c19c7afeSEric Joyner ixgbe_if_shutdown(if_ctx_t ctx)
28878eb6488eSEric Joyner {
28888eb6488eSEric Joyner 	int error = 0;
28898eb6488eSEric Joyner 
28908eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_shutdown: begin");
28918eb6488eSEric Joyner 
2892c19c7afeSEric Joyner 	error = ixgbe_setup_low_power_mode(ctx);
28938eb6488eSEric Joyner 
28948eb6488eSEric Joyner 	return (error);
2895c19c7afeSEric Joyner } /* ixgbe_if_shutdown */
28968eb6488eSEric Joyner 
28978eb6488eSEric Joyner /************************************************************************
28988eb6488eSEric Joyner  * ixgbe_suspend
28998eb6488eSEric Joyner  *
29008eb6488eSEric Joyner  *   From D0 to D3
29018eb6488eSEric Joyner  ************************************************************************/
29028eb6488eSEric Joyner static int
2903c19c7afeSEric Joyner ixgbe_if_suspend(if_ctx_t ctx)
29048eb6488eSEric Joyner {
29058eb6488eSEric Joyner 	int error = 0;
29068eb6488eSEric Joyner 
29078eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_suspend: begin");
29088eb6488eSEric Joyner 
2909c19c7afeSEric Joyner 	error = ixgbe_setup_low_power_mode(ctx);
29108eb6488eSEric Joyner 
29118eb6488eSEric Joyner 	return (error);
2912c19c7afeSEric Joyner } /* ixgbe_if_suspend */
29138eb6488eSEric Joyner 
29148eb6488eSEric Joyner /************************************************************************
29158eb6488eSEric Joyner  * ixgbe_resume
29168eb6488eSEric Joyner  *
29178eb6488eSEric Joyner  *   From D3 to D0
29188eb6488eSEric Joyner  ************************************************************************/
29198eb6488eSEric Joyner static int
2920c19c7afeSEric Joyner ixgbe_if_resume(if_ctx_t ctx)
29218eb6488eSEric Joyner {
2922b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2923c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
2924ff06a8dbSJustin Hibbits 	if_t            ifp = iflib_get_ifp(ctx);
2925b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
29268eb6488eSEric Joyner 	u32             wus;
29278eb6488eSEric Joyner 
29288eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_resume: begin");
29298eb6488eSEric Joyner 
29308eb6488eSEric Joyner 	/* Read & clear WUS register */
29318eb6488eSEric Joyner 	wus = IXGBE_READ_REG(hw, IXGBE_WUS);
29328eb6488eSEric Joyner 	if (wus)
29338eb6488eSEric Joyner 		device_printf(dev, "Woken up by (WUS): %#010x\n",
29348eb6488eSEric Joyner 		    IXGBE_READ_REG(hw, IXGBE_WUS));
29358eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
29368eb6488eSEric Joyner 	/* And clear WUFC until next low-power transition */
29378eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
29388eb6488eSEric Joyner 
29398eb6488eSEric Joyner 	/*
29408eb6488eSEric Joyner 	 * Required after D3->D0 transition;
29418eb6488eSEric Joyner 	 * will re-advertise all previous advertised speeds
29428eb6488eSEric Joyner 	 */
2943ff06a8dbSJustin Hibbits 	if (if_getflags(ifp) & IFF_UP)
2944c19c7afeSEric Joyner 		ixgbe_if_init(ctx);
29458eb6488eSEric Joyner 
29468eb6488eSEric Joyner 	return (0);
2947c19c7afeSEric Joyner } /* ixgbe_if_resume */
29488eb6488eSEric Joyner 
29498eb6488eSEric Joyner /************************************************************************
2950c19c7afeSEric Joyner  * ixgbe_if_mtu_set - Ioctl mtu entry point
29518eb6488eSEric Joyner  *
2952c19c7afeSEric Joyner  *   Return 0 on success, EINVAL on failure
2953c19c7afeSEric Joyner  ************************************************************************/
2954c19c7afeSEric Joyner static int
2955c19c7afeSEric Joyner ixgbe_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
2956c19c7afeSEric Joyner {
2957b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2958c19c7afeSEric Joyner 	int error = 0;
2959c19c7afeSEric Joyner 
2960c19c7afeSEric Joyner 	IOCTL_DEBUGOUT("ioctl: SIOCIFMTU (Set Interface MTU)");
2961c19c7afeSEric Joyner 
2962c19c7afeSEric Joyner 	if (mtu > IXGBE_MAX_MTU) {
2963c19c7afeSEric Joyner 		error = EINVAL;
2964c19c7afeSEric Joyner 	} else {
2965b1d5caf3SKevin Bowling 		sc->max_frame_size = mtu + IXGBE_MTU_HDR;
2966c19c7afeSEric Joyner 	}
2967c19c7afeSEric Joyner 
2968c19c7afeSEric Joyner 	return error;
2969c19c7afeSEric Joyner } /* ixgbe_if_mtu_set */
2970c19c7afeSEric Joyner 
2971c19c7afeSEric Joyner /************************************************************************
2972c19c7afeSEric Joyner  * ixgbe_if_crcstrip_set
29738eb6488eSEric Joyner  ************************************************************************/
29748eb6488eSEric Joyner static void
2975c19c7afeSEric Joyner ixgbe_if_crcstrip_set(if_ctx_t ctx, int onoff, int crcstrip)
29768eb6488eSEric Joyner {
2977b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2978c19c7afeSEric Joyner 	struct ixgbe_hw *hw = &sc->hw;
2979c19c7afeSEric Joyner 	/* crc stripping is set in two places:
2980c19c7afeSEric Joyner 	 * IXGBE_HLREG0 (modified on init_locked and hw reset)
2981c19c7afeSEric Joyner 	 * IXGBE_RDRXCTL (set by the original driver in
2982c19c7afeSEric Joyner 	 *	ixgbe_setup_hw_rsc() called in init_locked.
2983c19c7afeSEric Joyner 	 *	We disable the setting when netmap is compiled in).
2984c19c7afeSEric Joyner 	 * We update the values here, but also in ixgbe.c because
2985c19c7afeSEric Joyner 	 * init_locked sometimes is called outside our control.
2986c19c7afeSEric Joyner 	 */
2987c19c7afeSEric Joyner 	uint32_t hl, rxc;
29888eb6488eSEric Joyner 
2989c19c7afeSEric Joyner 	hl = IXGBE_READ_REG(hw, IXGBE_HLREG0);
2990c19c7afeSEric Joyner 	rxc = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
2991c19c7afeSEric Joyner #ifdef NETMAP
2992c19c7afeSEric Joyner 	if (netmap_verbose)
2993c19c7afeSEric Joyner 		D("%s read  HLREG 0x%x rxc 0x%x",
2994c19c7afeSEric Joyner 			onoff ? "enter" : "exit", hl, rxc);
29958eb6488eSEric Joyner #endif
2996c19c7afeSEric Joyner 	/* hw requirements ... */
2997c19c7afeSEric Joyner 	rxc &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
2998c19c7afeSEric Joyner 	rxc |= IXGBE_RDRXCTL_RSCACKC;
2999c19c7afeSEric Joyner 	if (onoff && !crcstrip) {
3000c19c7afeSEric Joyner 		/* keep the crc. Fast rx */
3001c19c7afeSEric Joyner 		hl &= ~IXGBE_HLREG0_RXCRCSTRP;
3002c19c7afeSEric Joyner 		rxc &= ~IXGBE_RDRXCTL_CRCSTRIP;
3003c19c7afeSEric Joyner 	} else {
3004c19c7afeSEric Joyner 		/* reset default mode */
3005c19c7afeSEric Joyner 		hl |= IXGBE_HLREG0_RXCRCSTRP;
3006c19c7afeSEric Joyner 		rxc |= IXGBE_RDRXCTL_CRCSTRIP;
3007c19c7afeSEric Joyner 	}
3008c19c7afeSEric Joyner #ifdef NETMAP
3009c19c7afeSEric Joyner 	if (netmap_verbose)
3010c19c7afeSEric Joyner 		D("%s write HLREG 0x%x rxc 0x%x",
3011c19c7afeSEric Joyner 			onoff ? "enter" : "exit", hl, rxc);
3012c19c7afeSEric Joyner #endif
3013c19c7afeSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hl);
3014c19c7afeSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rxc);
3015c19c7afeSEric Joyner } /* ixgbe_if_crcstrip_set */
30168eb6488eSEric Joyner 
3017c19c7afeSEric Joyner /*********************************************************************
3018c19c7afeSEric Joyner  * ixgbe_if_init - Init entry point
30198eb6488eSEric Joyner  *
30208eb6488eSEric Joyner  *   Used in two ways: It is used by the stack as an init
30218eb6488eSEric Joyner  *   entry point in network interface structure. It is also
30228eb6488eSEric Joyner  *   used by the driver as a hw/sw initialization routine to
30238eb6488eSEric Joyner  *   get to a consistent state.
30248eb6488eSEric Joyner  *
3025c19c7afeSEric Joyner  *   Return 0 on success, positive on failure
3026c19c7afeSEric Joyner  **********************************************************************/
30278eb6488eSEric Joyner void
3028c19c7afeSEric Joyner ixgbe_if_init(if_ctx_t ctx)
30298eb6488eSEric Joyner {
3030b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3031ff06a8dbSJustin Hibbits 	if_t               ifp = iflib_get_ifp(ctx);
3032c19c7afeSEric Joyner 	device_t           dev = iflib_get_dev(ctx);
3033b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
3034c19c7afeSEric Joyner 	struct ix_rx_queue *rx_que;
3035c19c7afeSEric Joyner 	struct ix_tx_queue *tx_que;
30368eb6488eSEric Joyner 	u32             txdctl, mhadd;
30378eb6488eSEric Joyner 	u32             rxdctl, rxctrl;
30388eb6488eSEric Joyner 	u32             ctrl_ext;
30398eb6488eSEric Joyner 
3040c19c7afeSEric Joyner 	int             i, j, err;
30418eb6488eSEric Joyner 
3042c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_init: begin");
30438eb6488eSEric Joyner 
30448eb6488eSEric Joyner 	/* Queue indices may change with IOV mode */
3045b1d5caf3SKevin Bowling 	ixgbe_align_all_queue_indices(sc);
30468eb6488eSEric Joyner 
30478eb6488eSEric Joyner 	/* reprogram the RAR[0] in case user changed it. */
3048b1d5caf3SKevin Bowling 	ixgbe_set_rar(hw, 0, hw->mac.addr, sc->pool, IXGBE_RAH_AV);
30498eb6488eSEric Joyner 
30508eb6488eSEric Joyner 	/* Get the latest mac address, User can use a LAA */
3051ff06a8dbSJustin Hibbits 	bcopy(if_getlladdr(ifp), hw->mac.addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
3052b1d5caf3SKevin Bowling 	ixgbe_set_rar(hw, 0, hw->mac.addr, sc->pool, 1);
30538eb6488eSEric Joyner 	hw->addr_ctrl.rar_used_count = 1;
30548eb6488eSEric Joyner 
30558eb6488eSEric Joyner 	ixgbe_init_hw(hw);
3056c19c7afeSEric Joyner 
3057b1d5caf3SKevin Bowling 	ixgbe_initialize_iov(sc);
3058c19c7afeSEric Joyner 
3059c19c7afeSEric Joyner 	ixgbe_initialize_transmit_units(ctx);
30608eb6488eSEric Joyner 
30618eb6488eSEric Joyner 	/* Setup Multicast table */
3062c19c7afeSEric Joyner 	ixgbe_if_multi_set(ctx);
30638eb6488eSEric Joyner 
30648eb6488eSEric Joyner 	/* Determine the correct mbuf pool, based on frame size */
3065b1d5caf3SKevin Bowling 	sc->rx_mbuf_sz = iflib_get_rx_mbuf_sz(ctx);
30668eb6488eSEric Joyner 
30678eb6488eSEric Joyner 	/* Configure RX settings */
3068c19c7afeSEric Joyner 	ixgbe_initialize_receive_units(ctx);
30698eb6488eSEric Joyner 
3070b2c1e8e6SEric Joyner 	/*
3071b2c1e8e6SEric Joyner 	 * Initialize variable holding task enqueue requests
3072b2c1e8e6SEric Joyner 	 * from MSI-X interrupts
3073b2c1e8e6SEric Joyner 	 */
3074b1d5caf3SKevin Bowling 	sc->task_requests = 0;
3075b2c1e8e6SEric Joyner 
30768eb6488eSEric Joyner 	/* Enable SDP & MSI-X interrupts based on adapter */
3077b1d5caf3SKevin Bowling 	ixgbe_config_gpie(sc);
30788eb6488eSEric Joyner 
30798eb6488eSEric Joyner 	/* Set MTU size */
3080ff06a8dbSJustin Hibbits 	if (if_getmtu(ifp) > ETHERMTU) {
30818eb6488eSEric Joyner 		/* aka IXGBE_MAXFRS on 82599 and newer */
30828eb6488eSEric Joyner 		mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
30838eb6488eSEric Joyner 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
3084b1d5caf3SKevin Bowling 		mhadd |= sc->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
30858eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
30868eb6488eSEric Joyner 	}
30878eb6488eSEric Joyner 
30888eb6488eSEric Joyner 	/* Now enable all the queues */
3089b1d5caf3SKevin Bowling 	for (i = 0, tx_que = sc->tx_queues; i < sc->num_tx_queues; i++, tx_que++) {
3090c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
3091c19c7afeSEric Joyner 
30928eb6488eSEric Joyner 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(txr->me));
30938eb6488eSEric Joyner 		txdctl |= IXGBE_TXDCTL_ENABLE;
30948eb6488eSEric Joyner 		/* Set WTHRESH to 8, burst writeback */
30958eb6488eSEric Joyner 		txdctl |= (8 << 16);
30968eb6488eSEric Joyner 		/*
30978eb6488eSEric Joyner 		 * When the internal queue falls below PTHRESH (32),
30988eb6488eSEric Joyner 		 * start prefetching as long as there are at least
30998eb6488eSEric Joyner 		 * HTHRESH (1) buffers ready. The values are taken
31008eb6488eSEric Joyner 		 * from the Intel linux driver 3.8.21.
31018eb6488eSEric Joyner 		 * Prefetching enables tx line rate even with 1 queue.
31028eb6488eSEric Joyner 		 */
31038eb6488eSEric Joyner 		txdctl |= (32 << 0) | (1 << 8);
31048eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(txr->me), txdctl);
31058eb6488eSEric Joyner 	}
31068eb6488eSEric Joyner 
3107b1d5caf3SKevin Bowling 	for (i = 0, rx_que = sc->rx_queues; i < sc->num_rx_queues; i++, rx_que++) {
3108c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
3109c19c7afeSEric Joyner 
31108eb6488eSEric Joyner 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
31118eb6488eSEric Joyner 		if (hw->mac.type == ixgbe_mac_82598EB) {
31128eb6488eSEric Joyner 			/*
31138eb6488eSEric Joyner 			 * PTHRESH = 21
31148eb6488eSEric Joyner 			 * HTHRESH = 4
31158eb6488eSEric Joyner 			 * WTHRESH = 8
31168eb6488eSEric Joyner 			 */
31178eb6488eSEric Joyner 			rxdctl &= ~0x3FFFFF;
31188eb6488eSEric Joyner 			rxdctl |= 0x080420;
31198eb6488eSEric Joyner 		}
31208eb6488eSEric Joyner 		rxdctl |= IXGBE_RXDCTL_ENABLE;
31218eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), rxdctl);
3122c19c7afeSEric Joyner 		for (j = 0; j < 10; j++) {
31238eb6488eSEric Joyner 			if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me)) &
31248eb6488eSEric Joyner 			    IXGBE_RXDCTL_ENABLE)
31258eb6488eSEric Joyner 				break;
31268eb6488eSEric Joyner 			else
31278eb6488eSEric Joyner 				msec_delay(1);
31288eb6488eSEric Joyner 		}
31298eb6488eSEric Joyner 		wmb();
31308eb6488eSEric Joyner 	}
31318eb6488eSEric Joyner 
31328eb6488eSEric Joyner 	/* Enable Receive engine */
31338eb6488eSEric Joyner 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
31348eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
31358eb6488eSEric Joyner 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
31368eb6488eSEric Joyner 	rxctrl |= IXGBE_RXCTRL_RXEN;
31378eb6488eSEric Joyner 	ixgbe_enable_rx_dma(hw, rxctrl);
31388eb6488eSEric Joyner 
3139c19c7afeSEric Joyner 	/* Set up MSI/MSI-X routing */
3140c19c7afeSEric Joyner 	if (ixgbe_enable_msix)  {
3141b1d5caf3SKevin Bowling 		ixgbe_configure_ivars(sc);
31428eb6488eSEric Joyner 		/* Set up auto-mask */
31438eb6488eSEric Joyner 		if (hw->mac.type == ixgbe_mac_82598EB)
31448eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
31458eb6488eSEric Joyner 		else {
31468eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
31478eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
31488eb6488eSEric Joyner 		}
31498eb6488eSEric Joyner 	} else {  /* Simple settings for Legacy/MSI */
3150b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, 0, 0, 0);
3151b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, 0, 0, 1);
31528eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
31538eb6488eSEric Joyner 	}
31548eb6488eSEric Joyner 
3155b1d5caf3SKevin Bowling 	ixgbe_init_fdir(sc);
31568eb6488eSEric Joyner 
31578eb6488eSEric Joyner 	/*
31588eb6488eSEric Joyner 	 * Check on any SFP devices that
31598eb6488eSEric Joyner 	 * need to be kick-started
31608eb6488eSEric Joyner 	 */
31618eb6488eSEric Joyner 	if (hw->phy.type == ixgbe_phy_none) {
31628eb6488eSEric Joyner 		err = hw->phy.ops.identify(hw);
31638eb6488eSEric Joyner 		if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
31648eb6488eSEric Joyner 			device_printf(dev,
31658eb6488eSEric Joyner 			    "Unsupported SFP+ module type was detected.\n");
31668eb6488eSEric Joyner 			return;
31678eb6488eSEric Joyner 		}
31688eb6488eSEric Joyner 	}
31698eb6488eSEric Joyner 
31708eb6488eSEric Joyner 	/* Set moderation on the Link interrupt */
3171b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(hw, IXGBE_EITR(sc->vector), IXGBE_LINK_ITR);
31728eb6488eSEric Joyner 
3173c19c7afeSEric Joyner 	/* Enable power to the phy. */
317479b36ec9SKevin Bowling 	ixgbe_set_phy_power(hw, true);
3175c19c7afeSEric Joyner 
31768eb6488eSEric Joyner 	/* Config/Enable Link */
3177b2c1e8e6SEric Joyner 	ixgbe_config_link(ctx);
31788eb6488eSEric Joyner 
31798eb6488eSEric Joyner 	/* Hardware Packet Buffer & Flow Control setup */
3180b1d5caf3SKevin Bowling 	ixgbe_config_delay_values(sc);
31818eb6488eSEric Joyner 
31828eb6488eSEric Joyner 	/* Initialize the FC settings */
31838eb6488eSEric Joyner 	ixgbe_start_hw(hw);
31848eb6488eSEric Joyner 
31858eb6488eSEric Joyner 	/* Set up VLAN support and filter */
3186c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
31878eb6488eSEric Joyner 
31888eb6488eSEric Joyner 	/* Setup DMA Coalescing */
3189b1d5caf3SKevin Bowling 	ixgbe_config_dmac(sc);
31908eb6488eSEric Joyner 
31918eb6488eSEric Joyner 	/* And now turn on interrupts */
3192c19c7afeSEric Joyner 	ixgbe_if_enable_intr(ctx);
31938eb6488eSEric Joyner 
31948eb6488eSEric Joyner 	/* Enable the use of the MBX by the VF's */
3195b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_SRIOV) {
31968eb6488eSEric Joyner 		ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
31978eb6488eSEric Joyner 		ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
31988eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
31998eb6488eSEric Joyner 	}
32008eb6488eSEric Joyner 
32018eb6488eSEric Joyner } /* ixgbe_init_locked */
32028eb6488eSEric Joyner 
32038eb6488eSEric Joyner /************************************************************************
32048eb6488eSEric Joyner  * ixgbe_set_ivar
32058eb6488eSEric Joyner  *
32068eb6488eSEric Joyner  *   Setup the correct IVAR register for a particular MSI-X interrupt
32078eb6488eSEric Joyner  *     (yes this is all very magic and confusing :)
32088eb6488eSEric Joyner  *    - entry is the register array entry
32098eb6488eSEric Joyner  *    - vector is the MSI-X vector for this queue
32108eb6488eSEric Joyner  *    - type is RX/TX/MISC
32118eb6488eSEric Joyner  ************************************************************************/
32128eb6488eSEric Joyner static void
3213b1d5caf3SKevin Bowling ixgbe_set_ivar(struct ixgbe_softc *sc, u8 entry, u8 vector, s8 type)
32148eb6488eSEric Joyner {
3215b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
32168eb6488eSEric Joyner 	u32 ivar, index;
32178eb6488eSEric Joyner 
32188eb6488eSEric Joyner 	vector |= IXGBE_IVAR_ALLOC_VAL;
32198eb6488eSEric Joyner 
32208eb6488eSEric Joyner 	switch (hw->mac.type) {
32218eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
32228eb6488eSEric Joyner 		if (type == -1)
32238eb6488eSEric Joyner 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
32248eb6488eSEric Joyner 		else
32258eb6488eSEric Joyner 			entry += (type * 64);
32268eb6488eSEric Joyner 		index = (entry >> 2) & 0x1F;
32278eb6488eSEric Joyner 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
32288eb6488eSEric Joyner 		ivar &= ~(0xFF << (8 * (entry & 0x3)));
32298eb6488eSEric Joyner 		ivar |= (vector << (8 * (entry & 0x3)));
3230b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_IVAR(index), ivar);
32318eb6488eSEric Joyner 		break;
32328eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
32338eb6488eSEric Joyner 	case ixgbe_mac_X540:
32348eb6488eSEric Joyner 	case ixgbe_mac_X550:
32358eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
32368eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
32378eb6488eSEric Joyner 		if (type == -1) { /* MISC IVAR */
32388eb6488eSEric Joyner 			index = (entry & 1) * 8;
32398eb6488eSEric Joyner 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
32408eb6488eSEric Joyner 			ivar &= ~(0xFF << index);
32418eb6488eSEric Joyner 			ivar |= (vector << index);
32428eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
32438eb6488eSEric Joyner 		} else {          /* RX/TX IVARS */
32448eb6488eSEric Joyner 			index = (16 * (entry & 1)) + (8 * type);
32458eb6488eSEric Joyner 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
32468eb6488eSEric Joyner 			ivar &= ~(0xFF << index);
32478eb6488eSEric Joyner 			ivar |= (vector << index);
32488eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
32498eb6488eSEric Joyner 		}
32508eb6488eSEric Joyner 	default:
32518eb6488eSEric Joyner 		break;
32528eb6488eSEric Joyner 	}
32538eb6488eSEric Joyner } /* ixgbe_set_ivar */
32548eb6488eSEric Joyner 
32558eb6488eSEric Joyner /************************************************************************
32568eb6488eSEric Joyner  * ixgbe_configure_ivars
32578eb6488eSEric Joyner  ************************************************************************/
32588eb6488eSEric Joyner static void
3259b1d5caf3SKevin Bowling ixgbe_configure_ivars(struct ixgbe_softc *sc)
32608eb6488eSEric Joyner {
3261b1d5caf3SKevin Bowling 	struct ix_rx_queue *rx_que = sc->rx_queues;
3262b1d5caf3SKevin Bowling 	struct ix_tx_queue *tx_que = sc->tx_queues;
32638eb6488eSEric Joyner 	u32                newitr;
32648eb6488eSEric Joyner 
32658eb6488eSEric Joyner 	if (ixgbe_max_interrupt_rate > 0)
32668eb6488eSEric Joyner 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
32678eb6488eSEric Joyner 	else {
32688eb6488eSEric Joyner 		/*
32698eb6488eSEric Joyner 		 * Disable DMA coalescing if interrupt moderation is
32708eb6488eSEric Joyner 		 * disabled.
32718eb6488eSEric Joyner 		 */
3272b1d5caf3SKevin Bowling 		sc->dmac = 0;
32738eb6488eSEric Joyner 		newitr = 0;
32748eb6488eSEric Joyner 	}
32758eb6488eSEric Joyner 
3276b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, rx_que++) {
3277c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
32788eb6488eSEric Joyner 
3279c19c7afeSEric Joyner 		/* First the RX queue entry */
3280b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, rxr->me, rx_que->msix, 0);
3281c19c7afeSEric Joyner 
3282c19c7afeSEric Joyner 		/* Set an Initial EITR value */
3283b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EITR(rx_que->msix), newitr);
3284c19c7afeSEric Joyner 	}
3285b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_tx_queues; i++, tx_que++) {
3286c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
3287c19c7afeSEric Joyner 
3288c19c7afeSEric Joyner 		/* ... and the TX */
3289b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, txr->me, tx_que->msix, 1);
3290c19c7afeSEric Joyner 	}
32918eb6488eSEric Joyner 	/* For the Link interrupt */
3292b1d5caf3SKevin Bowling 	ixgbe_set_ivar(sc, 1, sc->vector, -1);
32938eb6488eSEric Joyner } /* ixgbe_configure_ivars */
32948eb6488eSEric Joyner 
32958eb6488eSEric Joyner /************************************************************************
32968eb6488eSEric Joyner  * ixgbe_config_gpie
32978eb6488eSEric Joyner  ************************************************************************/
32988eb6488eSEric Joyner static void
3299b1d5caf3SKevin Bowling ixgbe_config_gpie(struct ixgbe_softc *sc)
33008eb6488eSEric Joyner {
3301b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
33028eb6488eSEric Joyner 	u32             gpie;
33038eb6488eSEric Joyner 
33048eb6488eSEric Joyner 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
33058eb6488eSEric Joyner 
3306b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX) {
33078eb6488eSEric Joyner 		/* Enable Enhanced MSI-X mode */
33088eb6488eSEric Joyner 		gpie |= IXGBE_GPIE_MSIX_MODE
33098eb6488eSEric Joyner 		     |  IXGBE_GPIE_EIAME
33108eb6488eSEric Joyner 		     |  IXGBE_GPIE_PBA_SUPPORT
33118eb6488eSEric Joyner 		     |  IXGBE_GPIE_OCD;
33128eb6488eSEric Joyner 	}
33138eb6488eSEric Joyner 
33148eb6488eSEric Joyner 	/* Fan Failure Interrupt */
3315b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL)
33168eb6488eSEric Joyner 		gpie |= IXGBE_SDP1_GPIEN;
33178eb6488eSEric Joyner 
33188eb6488eSEric Joyner 	/* Thermal Sensor Interrupt */
3319b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_TEMP_SENSOR)
33208eb6488eSEric Joyner 		gpie |= IXGBE_SDP0_GPIEN_X540;
33218eb6488eSEric Joyner 
33228eb6488eSEric Joyner 	/* Link detection */
33238eb6488eSEric Joyner 	switch (hw->mac.type) {
33248eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
33258eb6488eSEric Joyner 		gpie |= IXGBE_SDP1_GPIEN | IXGBE_SDP2_GPIEN;
33268eb6488eSEric Joyner 		break;
33278eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
33288eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
33298eb6488eSEric Joyner 		gpie |= IXGBE_SDP0_GPIEN_X540;
33308eb6488eSEric Joyner 		break;
33318eb6488eSEric Joyner 	default:
33328eb6488eSEric Joyner 		break;
33338eb6488eSEric Joyner 	}
33348eb6488eSEric Joyner 
33358eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
33368eb6488eSEric Joyner 
33378eb6488eSEric Joyner } /* ixgbe_config_gpie */
33388eb6488eSEric Joyner 
33398eb6488eSEric Joyner /************************************************************************
33408eb6488eSEric Joyner  * ixgbe_config_delay_values
33418eb6488eSEric Joyner  *
3342b1d5caf3SKevin Bowling  *   Requires sc->max_frame_size to be set.
33438eb6488eSEric Joyner  ************************************************************************/
33448eb6488eSEric Joyner static void
3345b1d5caf3SKevin Bowling ixgbe_config_delay_values(struct ixgbe_softc *sc)
33468eb6488eSEric Joyner {
3347b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
33488eb6488eSEric Joyner 	u32             rxpb, frame, size, tmp;
33498eb6488eSEric Joyner 
3350b1d5caf3SKevin Bowling 	frame = sc->max_frame_size;
33518eb6488eSEric Joyner 
33528eb6488eSEric Joyner 	/* Calculate High Water */
33538eb6488eSEric Joyner 	switch (hw->mac.type) {
33548eb6488eSEric Joyner 	case ixgbe_mac_X540:
33558eb6488eSEric Joyner 	case ixgbe_mac_X550:
33568eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
33578eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
33588eb6488eSEric Joyner 		tmp = IXGBE_DV_X540(frame, frame);
33598eb6488eSEric Joyner 		break;
33608eb6488eSEric Joyner 	default:
33618eb6488eSEric Joyner 		tmp = IXGBE_DV(frame, frame);
33628eb6488eSEric Joyner 		break;
33638eb6488eSEric Joyner 	}
33648eb6488eSEric Joyner 	size = IXGBE_BT2KB(tmp);
33658eb6488eSEric Joyner 	rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
33668eb6488eSEric Joyner 	hw->fc.high_water[0] = rxpb - size;
33678eb6488eSEric Joyner 
33688eb6488eSEric Joyner 	/* Now calculate Low Water */
33698eb6488eSEric Joyner 	switch (hw->mac.type) {
33708eb6488eSEric Joyner 	case ixgbe_mac_X540:
33718eb6488eSEric Joyner 	case ixgbe_mac_X550:
33728eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
33738eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
33748eb6488eSEric Joyner 		tmp = IXGBE_LOW_DV_X540(frame);
33758eb6488eSEric Joyner 		break;
33768eb6488eSEric Joyner 	default:
33778eb6488eSEric Joyner 		tmp = IXGBE_LOW_DV(frame);
33788eb6488eSEric Joyner 		break;
33798eb6488eSEric Joyner 	}
33808eb6488eSEric Joyner 	hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
33818eb6488eSEric Joyner 
33828eb6488eSEric Joyner 	hw->fc.pause_time = IXGBE_FC_PAUSE;
338379b36ec9SKevin Bowling 	hw->fc.send_xon = true;
33848eb6488eSEric Joyner } /* ixgbe_config_delay_values */
33858eb6488eSEric Joyner 
33868eb6488eSEric Joyner /************************************************************************
33878eb6488eSEric Joyner  * ixgbe_set_multi - Multicast Update
33888eb6488eSEric Joyner  *
33898eb6488eSEric Joyner  *   Called whenever multicast address list is updated.
33908eb6488eSEric Joyner  ************************************************************************/
3391ba76aa63SGleb Smirnoff static u_int
339221afed4bSKevin Bowling ixgbe_mc_filter_apply(void *arg, struct sockaddr_dl *sdl, u_int idx)
33938eb6488eSEric Joyner {
3394b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = arg;
3395b1d5caf3SKevin Bowling 	struct ixgbe_mc_addr *mta = sc->mta;
3396c19c7afeSEric Joyner 
339721afed4bSKevin Bowling 	if (idx == MAX_NUM_MULTICAST_ADDRESSES)
3398c19c7afeSEric Joyner 		return (0);
339921afed4bSKevin Bowling 	bcopy(LLADDR(sdl), mta[idx].addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
3400b1d5caf3SKevin Bowling 	mta[idx].vmdq = sc->pool;
3401c19c7afeSEric Joyner 
3402c19c7afeSEric Joyner 	return (1);
3403c19c7afeSEric Joyner } /* ixgbe_mc_filter_apply */
3404c19c7afeSEric Joyner 
3405c19c7afeSEric Joyner static void
3406c19c7afeSEric Joyner ixgbe_if_multi_set(if_ctx_t ctx)
3407c19c7afeSEric Joyner {
3408b1d5caf3SKevin Bowling 	struct ixgbe_softc       *sc = iflib_get_softc(ctx);
34098eb6488eSEric Joyner 	struct ixgbe_mc_addr *mta;
3410ff06a8dbSJustin Hibbits 	if_t                  ifp = iflib_get_ifp(ctx);
34118eb6488eSEric Joyner 	u8                   *update_ptr;
34128eb6488eSEric Joyner 	u32                  fctrl;
3413ba76aa63SGleb Smirnoff 	u_int		     mcnt;
34148eb6488eSEric Joyner 
3415c19c7afeSEric Joyner 	IOCTL_DEBUGOUT("ixgbe_if_multi_set: begin");
34168eb6488eSEric Joyner 
3417b1d5caf3SKevin Bowling 	mta = sc->mta;
34188eb6488eSEric Joyner 	bzero(mta, sizeof(*mta) * MAX_NUM_MULTICAST_ADDRESSES);
34198eb6488eSEric Joyner 
3420b1d5caf3SKevin Bowling 	mcnt = if_foreach_llmaddr(iflib_get_ifp(ctx), ixgbe_mc_filter_apply, sc);
34218eb6488eSEric Joyner 
3422395cc55dSKevin Bowling 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES) {
3423395cc55dSKevin Bowling 		update_ptr = (u8 *)mta;
3424395cc55dSKevin Bowling 		ixgbe_update_mc_addr_list(&sc->hw, update_ptr, mcnt,
3425395cc55dSKevin Bowling 		    ixgbe_mc_array_itr, true);
3426395cc55dSKevin Bowling 	}
3427395cc55dSKevin Bowling 
3428b1d5caf3SKevin Bowling 	fctrl = IXGBE_READ_REG(&sc->hw, IXGBE_FCTRL);
3429deecaa14SKevin Bowling 
3430ff06a8dbSJustin Hibbits 	if (if_getflags(ifp) & IFF_PROMISC)
34318eb6488eSEric Joyner 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
34328eb6488eSEric Joyner 	else if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES ||
3433ff06a8dbSJustin Hibbits 	    if_getflags(ifp) & IFF_ALLMULTI) {
34348eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_MPE;
34358eb6488eSEric Joyner 		fctrl &= ~IXGBE_FCTRL_UPE;
34368eb6488eSEric Joyner 	} else
34378eb6488eSEric Joyner 		fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
34388eb6488eSEric Joyner 
3439b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, fctrl);
3440c19c7afeSEric Joyner } /* ixgbe_if_multi_set */
34418eb6488eSEric Joyner 
34428eb6488eSEric Joyner /************************************************************************
34438eb6488eSEric Joyner  * ixgbe_mc_array_itr
34448eb6488eSEric Joyner  *
34458eb6488eSEric Joyner  *   An iterator function needed by the multicast shared code.
34468eb6488eSEric Joyner  *   It feeds the shared code routine the addresses in the
34478eb6488eSEric Joyner  *   array of ixgbe_set_multi() one by one.
34488eb6488eSEric Joyner  ************************************************************************/
34498eb6488eSEric Joyner static u8 *
34508eb6488eSEric Joyner ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
34518eb6488eSEric Joyner {
34528eb6488eSEric Joyner 	struct ixgbe_mc_addr *mta;
34538eb6488eSEric Joyner 
34548eb6488eSEric Joyner 	mta = (struct ixgbe_mc_addr *)*update_ptr;
34558eb6488eSEric Joyner 	*vmdq = mta->vmdq;
34568eb6488eSEric Joyner 
34578eb6488eSEric Joyner 	*update_ptr = (u8*)(mta + 1);
34588eb6488eSEric Joyner 
34598eb6488eSEric Joyner 	return (mta->addr);
34608eb6488eSEric Joyner } /* ixgbe_mc_array_itr */
34618eb6488eSEric Joyner 
34628eb6488eSEric Joyner /************************************************************************
34638eb6488eSEric Joyner  * ixgbe_local_timer - Timer routine
34648eb6488eSEric Joyner  *
34658eb6488eSEric Joyner  *   Checks for link status, updates statistics,
34668eb6488eSEric Joyner  *   and runs the watchdog check.
34678eb6488eSEric Joyner  ************************************************************************/
34688eb6488eSEric Joyner static void
3469c19c7afeSEric Joyner ixgbe_if_timer(if_ctx_t ctx, uint16_t qid)
34708eb6488eSEric Joyner {
3471b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
34728eb6488eSEric Joyner 
3473c19c7afeSEric Joyner 	if (qid != 0)
3474c19c7afeSEric Joyner 		return;
34758eb6488eSEric Joyner 
34768eb6488eSEric Joyner 	/* Check for pluggable optics */
3477b1d5caf3SKevin Bowling 	if (sc->sfp_probe)
3478c19c7afeSEric Joyner 		if (!ixgbe_sfp_probe(ctx))
3479c19c7afeSEric Joyner 			return; /* Nothing to do */
34808eb6488eSEric Joyner 
3481b1d5caf3SKevin Bowling 	ixgbe_check_link(&sc->hw, &sc->link_speed, &sc->link_up, 0);
34828eb6488eSEric Joyner 
3483c19c7afeSEric Joyner 	/* Fire off the adminq task */
3484c19c7afeSEric Joyner 	iflib_admin_intr_deferred(ctx);
34858eb6488eSEric Joyner 
3486c19c7afeSEric Joyner } /* ixgbe_if_timer */
34878eb6488eSEric Joyner 
34888eb6488eSEric Joyner /************************************************************************
34898eb6488eSEric Joyner  * ixgbe_sfp_probe
34908eb6488eSEric Joyner  *
34918eb6488eSEric Joyner  *   Determine if a port had optics inserted.
34928eb6488eSEric Joyner  ************************************************************************/
34938eb6488eSEric Joyner static bool
3494c19c7afeSEric Joyner ixgbe_sfp_probe(if_ctx_t ctx)
34958eb6488eSEric Joyner {
3496b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3497b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
3498c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
349979b36ec9SKevin Bowling 	bool            result = false;
35008eb6488eSEric Joyner 
35018eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_nl) &&
35028eb6488eSEric Joyner 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
35038eb6488eSEric Joyner 		s32 ret = hw->phy.ops.identify_sfp(hw);
35048eb6488eSEric Joyner 		if (ret)
35058eb6488eSEric Joyner 			goto out;
35068eb6488eSEric Joyner 		ret = hw->phy.ops.reset(hw);
3507b1d5caf3SKevin Bowling 		sc->sfp_probe = false;
35088eb6488eSEric Joyner 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
35098eb6488eSEric Joyner 			device_printf(dev, "Unsupported SFP+ module detected!");
35108eb6488eSEric Joyner 			device_printf(dev,
35118eb6488eSEric Joyner 			    "Reload driver with supported module.\n");
35128eb6488eSEric Joyner 			goto out;
35138eb6488eSEric Joyner 		} else
35148eb6488eSEric Joyner 			device_printf(dev, "SFP+ module detected!\n");
35158eb6488eSEric Joyner 		/* We now have supported optics */
351679b36ec9SKevin Bowling 		result = true;
35178eb6488eSEric Joyner 	}
35188eb6488eSEric Joyner out:
35198eb6488eSEric Joyner 
35208eb6488eSEric Joyner 	return (result);
35218eb6488eSEric Joyner } /* ixgbe_sfp_probe */
35228eb6488eSEric Joyner 
35238eb6488eSEric Joyner /************************************************************************
35248eb6488eSEric Joyner  * ixgbe_handle_mod - Tasklet for SFP module interrupts
35258eb6488eSEric Joyner  ************************************************************************/
35268eb6488eSEric Joyner static void
3527c19c7afeSEric Joyner ixgbe_handle_mod(void *context)
35288eb6488eSEric Joyner {
3529c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3530b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3531b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
3532c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
35338eb6488eSEric Joyner 	u32             err, cage_full = 0;
35348eb6488eSEric Joyner 
3535b1d5caf3SKevin Bowling 	if (sc->hw.need_crosstalk_fix) {
35368eb6488eSEric Joyner 		switch (hw->mac.type) {
35378eb6488eSEric Joyner 		case ixgbe_mac_82599EB:
35388eb6488eSEric Joyner 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
35398eb6488eSEric Joyner 			    IXGBE_ESDP_SDP2;
35408eb6488eSEric Joyner 			break;
35418eb6488eSEric Joyner 		case ixgbe_mac_X550EM_x:
35428eb6488eSEric Joyner 		case ixgbe_mac_X550EM_a:
35438eb6488eSEric Joyner 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
35448eb6488eSEric Joyner 			    IXGBE_ESDP_SDP0;
35458eb6488eSEric Joyner 			break;
35468eb6488eSEric Joyner 		default:
35478eb6488eSEric Joyner 			break;
35488eb6488eSEric Joyner 		}
35498eb6488eSEric Joyner 
35508eb6488eSEric Joyner 		if (!cage_full)
3551c19c7afeSEric Joyner 			goto handle_mod_out;
35528eb6488eSEric Joyner 	}
35538eb6488eSEric Joyner 
35548eb6488eSEric Joyner 	err = hw->phy.ops.identify_sfp(hw);
35558eb6488eSEric Joyner 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
35568eb6488eSEric Joyner 		device_printf(dev,
35578eb6488eSEric Joyner 		    "Unsupported SFP+ module type was detected.\n");
3558c19c7afeSEric Joyner 		goto handle_mod_out;
35598eb6488eSEric Joyner 	}
35608eb6488eSEric Joyner 
3561c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
3562c19c7afeSEric Joyner 		err = hw->phy.ops.reset(hw);
3563c19c7afeSEric Joyner 	else
35648eb6488eSEric Joyner 		err = hw->mac.ops.setup_sfp(hw);
3565c19c7afeSEric Joyner 
35668eb6488eSEric Joyner 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
35678eb6488eSEric Joyner 		device_printf(dev,
35688eb6488eSEric Joyner 		    "Setup failure - unsupported SFP+ module type.\n");
3569c19c7afeSEric Joyner 		goto handle_mod_out;
35708eb6488eSEric Joyner 	}
3571b1d5caf3SKevin Bowling 	sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
3572c19c7afeSEric Joyner 	return;
3573c19c7afeSEric Joyner 
3574c19c7afeSEric Joyner handle_mod_out:
3575b1d5caf3SKevin Bowling 	sc->task_requests &= ~(IXGBE_REQUEST_TASK_MSF);
35768eb6488eSEric Joyner } /* ixgbe_handle_mod */
35778eb6488eSEric Joyner 
35788eb6488eSEric Joyner 
35798eb6488eSEric Joyner /************************************************************************
35808eb6488eSEric Joyner  * ixgbe_handle_msf - Tasklet for MSF (multispeed fiber) interrupts
35818eb6488eSEric Joyner  ************************************************************************/
35828eb6488eSEric Joyner static void
3583c19c7afeSEric Joyner ixgbe_handle_msf(void *context)
35848eb6488eSEric Joyner {
3585c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3586b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3587b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
35888eb6488eSEric Joyner 	u32             autoneg;
35898eb6488eSEric Joyner 	bool            negotiate;
35908eb6488eSEric Joyner 
35918eb6488eSEric Joyner 	/* get_supported_phy_layer will call hw->phy.ops.identify_sfp() */
3592b1d5caf3SKevin Bowling 	sc->phy_layer = ixgbe_get_supported_physical_layer(hw);
35938eb6488eSEric Joyner 
35948eb6488eSEric Joyner 	autoneg = hw->phy.autoneg_advertised;
35958eb6488eSEric Joyner 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
35968eb6488eSEric Joyner 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
35978eb6488eSEric Joyner 	if (hw->mac.ops.setup_link)
359879b36ec9SKevin Bowling 		hw->mac.ops.setup_link(hw, autoneg, true);
35998eb6488eSEric Joyner 
36008eb6488eSEric Joyner 	/* Adjust media types shown in ifconfig */
3601b1d5caf3SKevin Bowling 	ifmedia_removeall(sc->media);
3602b1d5caf3SKevin Bowling 	ixgbe_add_media_types(sc->ctx);
3603b1d5caf3SKevin Bowling 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
36048eb6488eSEric Joyner } /* ixgbe_handle_msf */
36058eb6488eSEric Joyner 
36068eb6488eSEric Joyner /************************************************************************
36078eb6488eSEric Joyner  * ixgbe_handle_phy - Tasklet for external PHY interrupts
36088eb6488eSEric Joyner  ************************************************************************/
36098eb6488eSEric Joyner static void
3610c19c7afeSEric Joyner ixgbe_handle_phy(void *context)
36118eb6488eSEric Joyner {
3612c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3613b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3614b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
36158eb6488eSEric Joyner 	int             error;
36168eb6488eSEric Joyner 
36178eb6488eSEric Joyner 	error = hw->phy.ops.handle_lasi(hw);
36188eb6488eSEric Joyner 	if (error == IXGBE_ERR_OVERTEMP)
3619b1d5caf3SKevin Bowling 		device_printf(sc->dev, "CRITICAL: EXTERNAL PHY OVER TEMP!!  PHY will downshift to lower power state!\n");
36208eb6488eSEric Joyner 	else if (error)
3621b1d5caf3SKevin Bowling 		device_printf(sc->dev,
36228eb6488eSEric Joyner 		    "Error handling LASI interrupt: %d\n", error);
36238eb6488eSEric Joyner } /* ixgbe_handle_phy */
36248eb6488eSEric Joyner 
36258eb6488eSEric Joyner /************************************************************************
3626c19c7afeSEric Joyner  * ixgbe_if_stop - Stop the hardware
36278eb6488eSEric Joyner  *
36288eb6488eSEric Joyner  *   Disables all traffic on the adapter by issuing a
36298eb6488eSEric Joyner  *   global reset on the MAC and deallocates TX/RX buffers.
36308eb6488eSEric Joyner  ************************************************************************/
36318eb6488eSEric Joyner static void
3632c19c7afeSEric Joyner ixgbe_if_stop(if_ctx_t ctx)
36338eb6488eSEric Joyner {
3634b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3635b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
36368eb6488eSEric Joyner 
3637c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_stop: begin\n");
36388eb6488eSEric Joyner 
36398eb6488eSEric Joyner 	ixgbe_reset_hw(hw);
364079b36ec9SKevin Bowling 	hw->adapter_stopped = false;
36418eb6488eSEric Joyner 	ixgbe_stop_adapter(hw);
36428eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82599EB)
36438eb6488eSEric Joyner 		ixgbe_stop_mac_link_on_d3_82599(hw);
36448eb6488eSEric Joyner 	/* Turn off the laser - noop with no optics */
36458eb6488eSEric Joyner 	ixgbe_disable_tx_laser(hw);
36468eb6488eSEric Joyner 
36478eb6488eSEric Joyner 	/* Update the stack */
3648b1d5caf3SKevin Bowling 	sc->link_up = false;
3649c19c7afeSEric Joyner 	ixgbe_if_update_admin_status(ctx);
36508eb6488eSEric Joyner 
36518eb6488eSEric Joyner 	/* reprogram the RAR[0] in case user changed it. */
3652b1d5caf3SKevin Bowling 	ixgbe_set_rar(&sc->hw, 0, sc->hw.mac.addr, 0, IXGBE_RAH_AV);
36538eb6488eSEric Joyner 
36548eb6488eSEric Joyner 	return;
3655c19c7afeSEric Joyner } /* ixgbe_if_stop */
36568eb6488eSEric Joyner 
36578eb6488eSEric Joyner /************************************************************************
36588eb6488eSEric Joyner  * ixgbe_update_link_status - Update OS on link state
36598eb6488eSEric Joyner  *
36608eb6488eSEric Joyner  * Note: Only updates the OS on the cached link state.
36618eb6488eSEric Joyner  *       The real check of the hardware only happens with
36628eb6488eSEric Joyner  *       a link interrupt.
36638eb6488eSEric Joyner  ************************************************************************/
36648eb6488eSEric Joyner static void
3665c19c7afeSEric Joyner ixgbe_if_update_admin_status(if_ctx_t ctx)
36668eb6488eSEric Joyner {
3667b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3668c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
36698eb6488eSEric Joyner 
3670b1d5caf3SKevin Bowling 	if (sc->link_up) {
3671b1d5caf3SKevin Bowling 		if (sc->link_active == false) {
36728eb6488eSEric Joyner 			if (bootverbose)
36738eb6488eSEric Joyner 				device_printf(dev, "Link is up %d Gbps %s \n",
3674b1d5caf3SKevin Bowling 				    ((sc->link_speed == 128) ? 10 : 1),
36758eb6488eSEric Joyner 				    "Full Duplex");
3676b1d5caf3SKevin Bowling 			sc->link_active = true;
36778eb6488eSEric Joyner 			/* Update any Flow Control changes */
3678b1d5caf3SKevin Bowling 			ixgbe_fc_enable(&sc->hw);
36798eb6488eSEric Joyner 			/* Update DMA coalescing config */
3680b1d5caf3SKevin Bowling 			ixgbe_config_dmac(sc);
3681c19c7afeSEric Joyner 			/* should actually be negotiated value */
3682*a0302c92SPiotr Pietruszewski 			iflib_link_state_change(ctx, LINK_STATE_UP,
3683*a0302c92SPiotr Pietruszewski 			    ixgbe_link_speed_to_baudrate(adapter->link_speed));
3684c19c7afeSEric Joyner 
3685b1d5caf3SKevin Bowling 			if (sc->feat_en & IXGBE_FEATURE_SRIOV)
3686b1d5caf3SKevin Bowling 				ixgbe_ping_all_vfs(sc);
36878eb6488eSEric Joyner 		}
36888eb6488eSEric Joyner 	} else { /* Link down */
3689b1d5caf3SKevin Bowling 		if (sc->link_active == true) {
36908eb6488eSEric Joyner 			if (bootverbose)
36918eb6488eSEric Joyner 				device_printf(dev, "Link is Down\n");
3692c19c7afeSEric Joyner 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
3693b1d5caf3SKevin Bowling 			sc->link_active = false;
3694b1d5caf3SKevin Bowling 			if (sc->feat_en & IXGBE_FEATURE_SRIOV)
3695b1d5caf3SKevin Bowling 				ixgbe_ping_all_vfs(sc);
36968eb6488eSEric Joyner 		}
36978eb6488eSEric Joyner 	}
36988eb6488eSEric Joyner 
3699b2c1e8e6SEric Joyner 	/* Handle task requests from msix_link() */
3700b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_MOD)
3701b2c1e8e6SEric Joyner 		ixgbe_handle_mod(ctx);
3702b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_MSF)
3703b2c1e8e6SEric Joyner 		ixgbe_handle_msf(ctx);
3704b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_MBX)
3705b2c1e8e6SEric Joyner 		ixgbe_handle_mbx(ctx);
3706b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_FDIR)
3707b2c1e8e6SEric Joyner 		ixgbe_reinit_fdir(ctx);
3708b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_PHY)
3709b2c1e8e6SEric Joyner 		ixgbe_handle_phy(ctx);
3710b1d5caf3SKevin Bowling 	sc->task_requests = 0;
3711c19c7afeSEric Joyner 
3712b1d5caf3SKevin Bowling 	ixgbe_update_stats_counters(sc);
3713c19c7afeSEric Joyner } /* ixgbe_if_update_admin_status */
37148eb6488eSEric Joyner 
37158eb6488eSEric Joyner /************************************************************************
37168eb6488eSEric Joyner  * ixgbe_config_dmac - Configure DMA Coalescing
37178eb6488eSEric Joyner  ************************************************************************/
37188eb6488eSEric Joyner static void
3719b1d5caf3SKevin Bowling ixgbe_config_dmac(struct ixgbe_softc *sc)
37208eb6488eSEric Joyner {
3721b1d5caf3SKevin Bowling 	struct ixgbe_hw          *hw = &sc->hw;
37228eb6488eSEric Joyner 	struct ixgbe_dmac_config *dcfg = &hw->mac.dmac_config;
37238eb6488eSEric Joyner 
37248eb6488eSEric Joyner 	if (hw->mac.type < ixgbe_mac_X550 || !hw->mac.ops.dmac_config)
37258eb6488eSEric Joyner 		return;
37268eb6488eSEric Joyner 
3727b1d5caf3SKevin Bowling 	if (dcfg->watchdog_timer ^ sc->dmac ||
3728b1d5caf3SKevin Bowling 	    dcfg->link_speed ^ sc->link_speed) {
3729b1d5caf3SKevin Bowling 		dcfg->watchdog_timer = sc->dmac;
373079b36ec9SKevin Bowling 		dcfg->fcoe_en = false;
3731b1d5caf3SKevin Bowling 		dcfg->link_speed = sc->link_speed;
37328eb6488eSEric Joyner 		dcfg->num_tcs = 1;
37338eb6488eSEric Joyner 
37348eb6488eSEric Joyner 		INIT_DEBUGOUT2("dmac settings: watchdog %d, link speed %d\n",
37358eb6488eSEric Joyner 		    dcfg->watchdog_timer, dcfg->link_speed);
37368eb6488eSEric Joyner 
37378eb6488eSEric Joyner 		hw->mac.ops.dmac_config(hw);
37388eb6488eSEric Joyner 	}
37398eb6488eSEric Joyner } /* ixgbe_config_dmac */
37408eb6488eSEric Joyner 
37418eb6488eSEric Joyner /************************************************************************
3742c19c7afeSEric Joyner  * ixgbe_if_enable_intr
37438eb6488eSEric Joyner  ************************************************************************/
3744c19c7afeSEric Joyner void
3745c19c7afeSEric Joyner ixgbe_if_enable_intr(if_ctx_t ctx)
37468eb6488eSEric Joyner {
3747b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3748b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
3749b1d5caf3SKevin Bowling 	struct ix_rx_queue *que = sc->rx_queues;
37508eb6488eSEric Joyner 	u32                mask, fwsm;
37518eb6488eSEric Joyner 
37528eb6488eSEric Joyner 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
37538eb6488eSEric Joyner 
3754b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
37558eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
37568eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
3757b1d5caf3SKevin Bowling 		/* Temperature sensor on some scs */
37588eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP0;
37598eb6488eSEric Joyner 		/* SFP+ (RX_LOS_N & MOD_ABS_N) */
37608eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP1;
37618eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP2;
37628eb6488eSEric Joyner 		break;
37638eb6488eSEric Joyner 	case ixgbe_mac_X540:
37648eb6488eSEric Joyner 		/* Detect if Thermal Sensor is enabled */
37658eb6488eSEric Joyner 		fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
37668eb6488eSEric Joyner 		if (fwsm & IXGBE_FWSM_TS_ENABLED)
37678eb6488eSEric Joyner 			mask |= IXGBE_EIMS_TS;
37688eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
37698eb6488eSEric Joyner 		break;
37708eb6488eSEric Joyner 	case ixgbe_mac_X550:
37718eb6488eSEric Joyner 		/* MAC thermal sensor is automatically enabled */
37728eb6488eSEric Joyner 		mask |= IXGBE_EIMS_TS;
37738eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
37748eb6488eSEric Joyner 		break;
37758eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
37768eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
37778eb6488eSEric Joyner 		/* Some devices use SDP0 for important information */
37788eb6488eSEric Joyner 		if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
37798eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP ||
37808eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP_N ||
37818eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
37828eb6488eSEric Joyner 			mask |= IXGBE_EIMS_GPI_SDP0_BY_MAC(hw);
37838eb6488eSEric Joyner 		if (hw->phy.type == ixgbe_phy_x550em_ext_t)
37848eb6488eSEric Joyner 			mask |= IXGBE_EICR_GPI_SDP0_X540;
37858eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
37868eb6488eSEric Joyner 		break;
37878eb6488eSEric Joyner 	default:
37888eb6488eSEric Joyner 		break;
37898eb6488eSEric Joyner 	}
37908eb6488eSEric Joyner 
37918eb6488eSEric Joyner 	/* Enable Fan Failure detection */
3792b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL)
37938eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP1;
37948eb6488eSEric Joyner 	/* Enable SR-IOV */
3795b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_SRIOV)
37968eb6488eSEric Joyner 		mask |= IXGBE_EIMS_MAILBOX;
37978eb6488eSEric Joyner 	/* Enable Flow Director */
3798b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FDIR)
37998eb6488eSEric Joyner 		mask |= IXGBE_EIMS_FLOW_DIR;
38008eb6488eSEric Joyner 
38018eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
38028eb6488eSEric Joyner 
38038eb6488eSEric Joyner 	/* With MSI-X we use auto clear */
3804b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX) {
38058eb6488eSEric Joyner 		mask = IXGBE_EIMS_ENABLE_MASK;
38068eb6488eSEric Joyner 		/* Don't autoclear Link */
38078eb6488eSEric Joyner 		mask &= ~IXGBE_EIMS_OTHER;
38088eb6488eSEric Joyner 		mask &= ~IXGBE_EIMS_LSC;
3809b1d5caf3SKevin Bowling 		if (sc->feat_cap & IXGBE_FEATURE_SRIOV)
38108eb6488eSEric Joyner 			mask &= ~IXGBE_EIMS_MAILBOX;
38118eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
38126f37f232SEric Joyner 	}
38136f37f232SEric Joyner 
3814758cc3dcSJack F Vogel 	/*
38158eb6488eSEric Joyner 	 * Now enable all queues, this is done separately to
38168eb6488eSEric Joyner 	 * allow for handling the extended (beyond 32) MSI-X
38178eb6488eSEric Joyner 	 * vectors that can be used by 82599
3818758cc3dcSJack F Vogel 	 */
3819b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, que++)
3820b1d5caf3SKevin Bowling 		ixgbe_enable_queue(sc, que->msix);
38218eb6488eSEric Joyner 
38228eb6488eSEric Joyner 	IXGBE_WRITE_FLUSH(hw);
38238eb6488eSEric Joyner 
3824c19c7afeSEric Joyner } /* ixgbe_if_enable_intr */
38258eb6488eSEric Joyner 
38268eb6488eSEric Joyner /************************************************************************
38278eb6488eSEric Joyner  * ixgbe_disable_intr
38288eb6488eSEric Joyner  ************************************************************************/
3829758cc3dcSJack F Vogel static void
3830c19c7afeSEric Joyner ixgbe_if_disable_intr(if_ctx_t ctx)
3831758cc3dcSJack F Vogel {
3832b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3833c19c7afeSEric Joyner 
3834b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX)
3835b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIAC, 0);
3836b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
3837b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC, ~0);
38388eb6488eSEric Joyner 	} else {
3839b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC, 0xFFFF0000);
3840b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC_EX(0), ~0);
3841b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC_EX(1), ~0);
38428eb6488eSEric Joyner 	}
3843b1d5caf3SKevin Bowling 	IXGBE_WRITE_FLUSH(&sc->hw);
3844758cc3dcSJack F Vogel 
3845c19c7afeSEric Joyner } /* ixgbe_if_disable_intr */
38468eb6488eSEric Joyner 
38478eb6488eSEric Joyner /************************************************************************
3848b2c1e8e6SEric Joyner  * ixgbe_link_intr_enable
3849b2c1e8e6SEric Joyner  ************************************************************************/
3850b2c1e8e6SEric Joyner static void
3851b2c1e8e6SEric Joyner ixgbe_link_intr_enable(if_ctx_t ctx)
3852b2c1e8e6SEric Joyner {
3853b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &((struct ixgbe_softc *)iflib_get_softc(ctx))->hw;
3854b2c1e8e6SEric Joyner 
3855b2c1e8e6SEric Joyner 	/* Re-enable other interrupts */
3856b2c1e8e6SEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC);
3857b2c1e8e6SEric Joyner } /* ixgbe_link_intr_enable */
3858b2c1e8e6SEric Joyner 
3859b2c1e8e6SEric Joyner /************************************************************************
3860c19c7afeSEric Joyner  * ixgbe_if_rx_queue_intr_enable
3861c19c7afeSEric Joyner  ************************************************************************/
3862c19c7afeSEric Joyner static int
3863c19c7afeSEric Joyner ixgbe_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
3864c19c7afeSEric Joyner {
3865b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3866b1d5caf3SKevin Bowling 	struct ix_rx_queue *que = &sc->rx_queues[rxqid];
3867c19c7afeSEric Joyner 
3868b1d5caf3SKevin Bowling 	ixgbe_enable_queue(sc, que->msix);
3869c19c7afeSEric Joyner 
3870c19c7afeSEric Joyner 	return (0);
3871c19c7afeSEric Joyner } /* ixgbe_if_rx_queue_intr_enable */
3872c19c7afeSEric Joyner 
3873c19c7afeSEric Joyner /************************************************************************
3874c19c7afeSEric Joyner  * ixgbe_enable_queue
38758eb6488eSEric Joyner  ************************************************************************/
38768eb6488eSEric Joyner static void
3877b1d5caf3SKevin Bowling ixgbe_enable_queue(struct ixgbe_softc *sc, u32 vector)
38788eb6488eSEric Joyner {
3879b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
38802dc2d580SEric Joyner 	u64             queue = 1ULL << vector;
3881c19c7afeSEric Joyner 	u32             mask;
3882758cc3dcSJack F Vogel 
3883c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB) {
3884c19c7afeSEric Joyner 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3885c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
3886c19c7afeSEric Joyner 	} else {
3887c19c7afeSEric Joyner 		mask = (queue & 0xFFFFFFFF);
3888c19c7afeSEric Joyner 		if (mask)
3889c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
3890c19c7afeSEric Joyner 		mask = (queue >> 32);
3891c19c7afeSEric Joyner 		if (mask)
3892c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
3893c19c7afeSEric Joyner 	}
3894c19c7afeSEric Joyner } /* ixgbe_enable_queue */
3895c19c7afeSEric Joyner 
3896c19c7afeSEric Joyner /************************************************************************
3897c19c7afeSEric Joyner  * ixgbe_disable_queue
3898c19c7afeSEric Joyner  ************************************************************************/
3899c19c7afeSEric Joyner static void
3900b1d5caf3SKevin Bowling ixgbe_disable_queue(struct ixgbe_softc *sc, u32 vector)
3901c19c7afeSEric Joyner {
3902b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
39032dc2d580SEric Joyner 	u64             queue = 1ULL << vector;
3904c19c7afeSEric Joyner 	u32             mask;
3905c19c7afeSEric Joyner 
3906c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB) {
3907c19c7afeSEric Joyner 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3908c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
3909c19c7afeSEric Joyner 	} else {
3910c19c7afeSEric Joyner 		mask = (queue & 0xFFFFFFFF);
3911c19c7afeSEric Joyner 		if (mask)
3912c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
3913c19c7afeSEric Joyner 		mask = (queue >> 32);
3914c19c7afeSEric Joyner 		if (mask)
3915c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
3916c19c7afeSEric Joyner 	}
3917c19c7afeSEric Joyner } /* ixgbe_disable_queue */
3918c19c7afeSEric Joyner 
3919c19c7afeSEric Joyner /************************************************************************
3920c19c7afeSEric Joyner  * ixgbe_intr - Legacy Interrupt Service Routine
3921c19c7afeSEric Joyner  ************************************************************************/
3922c19c7afeSEric Joyner int
3923c19c7afeSEric Joyner ixgbe_intr(void *arg)
3924c19c7afeSEric Joyner {
3925b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = arg;
3926b1d5caf3SKevin Bowling 	struct ix_rx_queue *que = sc->rx_queues;
3927b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
3928b1d5caf3SKevin Bowling 	if_ctx_t           ctx = sc->ctx;
3929c19c7afeSEric Joyner 	u32                eicr, eicr_mask;
3930758cc3dcSJack F Vogel 
39318eb6488eSEric Joyner 	eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
3932758cc3dcSJack F Vogel 
39338eb6488eSEric Joyner 	++que->irqs;
39348eb6488eSEric Joyner 	if (eicr == 0) {
3935c19c7afeSEric Joyner 		ixgbe_if_enable_intr(ctx);
3936c19c7afeSEric Joyner 		return (FILTER_HANDLED);
3937758cc3dcSJack F Vogel 	}
3938758cc3dcSJack F Vogel 
39398eb6488eSEric Joyner 	/* Check for fan failure */
3940c19c7afeSEric Joyner 	if ((hw->device_id == IXGBE_DEV_ID_82598AT) &&
3941c19c7afeSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP1)) {
3942b1d5caf3SKevin Bowling 		device_printf(sc->dev,
3943c19c7afeSEric Joyner 		    "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
39448eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3945758cc3dcSJack F Vogel 	}
3946758cc3dcSJack F Vogel 
39478eb6488eSEric Joyner 	/* Link status change */
3948c19c7afeSEric Joyner 	if (eicr & IXGBE_EICR_LSC) {
3949c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
3950c19c7afeSEric Joyner 		iflib_admin_intr_deferred(ctx);
3951c19c7afeSEric Joyner 	}
39528eb6488eSEric Joyner 
39538eb6488eSEric Joyner 	if (ixgbe_is_sfp(hw)) {
39548eb6488eSEric Joyner 		/* Pluggable optics-related interrupt */
39558eb6488eSEric Joyner 		if (hw->mac.type >= ixgbe_mac_X540)
39568eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
39578eb6488eSEric Joyner 		else
39588eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
39598eb6488eSEric Joyner 
39608eb6488eSEric Joyner 		if (eicr & eicr_mask) {
39618eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
3962b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
39638eb6488eSEric Joyner 		}
39648eb6488eSEric Joyner 
39658eb6488eSEric Joyner 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
39668eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
39678eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
39688eb6488eSEric Joyner 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3969b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
39708eb6488eSEric Joyner 		}
39718eb6488eSEric Joyner 	}
39728eb6488eSEric Joyner 
39738eb6488eSEric Joyner 	/* External PHY interrupt */
39748eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
39758eb6488eSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP0_X540))
3976b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_PHY;
39778eb6488eSEric Joyner 
3978c19c7afeSEric Joyner 	return (FILTER_SCHEDULE_THREAD);
3979c19c7afeSEric Joyner } /* ixgbe_intr */
39808eb6488eSEric Joyner 
39818eb6488eSEric Joyner /************************************************************************
39828eb6488eSEric Joyner  * ixgbe_free_pci_resources
39838eb6488eSEric Joyner  ************************************************************************/
39848eb6488eSEric Joyner static void
3985c19c7afeSEric Joyner ixgbe_free_pci_resources(if_ctx_t ctx)
39868eb6488eSEric Joyner {
3987b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3988b1d5caf3SKevin Bowling 	struct         ix_rx_queue *que = sc->rx_queues;
3989c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
39908eb6488eSEric Joyner 
3991b97de13aSMarius Strobl 	/* Release all MSI-X queue resources */
3992b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX)
3993b1d5caf3SKevin Bowling 		iflib_irq_free(ctx, &sc->irq);
3994c19c7afeSEric Joyner 
3995c19c7afeSEric Joyner 	if (que != NULL) {
3996b1d5caf3SKevin Bowling 		for (int i = 0; i < sc->num_rx_queues; i++, que++) {
3997c19c7afeSEric Joyner 			iflib_irq_free(ctx, &que->que_irq);
3998c19c7afeSEric Joyner 		}
3999c19c7afeSEric Joyner 	}
40008eb6488eSEric Joyner 
4001b1d5caf3SKevin Bowling 	if (sc->pci_mem != NULL)
4002c19c7afeSEric Joyner 		bus_release_resource(dev, SYS_RES_MEMORY,
4003b1d5caf3SKevin Bowling 		    rman_get_rid(sc->pci_mem), sc->pci_mem);
40048eb6488eSEric Joyner } /* ixgbe_free_pci_resources */
40058eb6488eSEric Joyner 
40068eb6488eSEric Joyner /************************************************************************
40078eb6488eSEric Joyner  * ixgbe_sysctl_flowcntl
40088eb6488eSEric Joyner  *
40098eb6488eSEric Joyner  *   SYSCTL wrapper around setting Flow Control
40108eb6488eSEric Joyner  ************************************************************************/
4011758cc3dcSJack F Vogel static int
4012f2c4db54SSteven Hartland ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS)
4013758cc3dcSJack F Vogel {
4014b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc;
40158eb6488eSEric Joyner 	int            error, fc;
4016758cc3dcSJack F Vogel 
4017b1d5caf3SKevin Bowling 	sc = (struct ixgbe_softc *)arg1;
4018b1d5caf3SKevin Bowling 	fc = sc->hw.fc.current_mode;
4019f2c4db54SSteven Hartland 
4020f2c4db54SSteven Hartland 	error = sysctl_handle_int(oidp, &fc, 0, req);
4021758cc3dcSJack F Vogel 	if ((error) || (req->newptr == NULL))
4022758cc3dcSJack F Vogel 		return (error);
4023758cc3dcSJack F Vogel 
4024758cc3dcSJack F Vogel 	/* Don't bother if it's not changed */
4025b1d5caf3SKevin Bowling 	if (fc == sc->hw.fc.current_mode)
4026758cc3dcSJack F Vogel 		return (0);
4027758cc3dcSJack F Vogel 
4028b1d5caf3SKevin Bowling 	return ixgbe_set_flowcntl(sc, fc);
40298eb6488eSEric Joyner } /* ixgbe_sysctl_flowcntl */
4030f2c4db54SSteven Hartland 
40318eb6488eSEric Joyner /************************************************************************
40328eb6488eSEric Joyner  * ixgbe_set_flowcntl - Set flow control
40338eb6488eSEric Joyner  *
40348eb6488eSEric Joyner  *   Flow control values:
40358eb6488eSEric Joyner  *     0 - off
40368eb6488eSEric Joyner  *     1 - rx pause
40378eb6488eSEric Joyner  *     2 - tx pause
40388eb6488eSEric Joyner  *     3 - full
40398eb6488eSEric Joyner  ************************************************************************/
4040f2c4db54SSteven Hartland static int
4041b1d5caf3SKevin Bowling ixgbe_set_flowcntl(struct ixgbe_softc *sc, int fc)
4042f2c4db54SSteven Hartland {
4043f2c4db54SSteven Hartland 	switch (fc) {
4044758cc3dcSJack F Vogel 	case ixgbe_fc_rx_pause:
4045758cc3dcSJack F Vogel 	case ixgbe_fc_tx_pause:
4046758cc3dcSJack F Vogel 	case ixgbe_fc_full:
4047b1d5caf3SKevin Bowling 		sc->hw.fc.requested_mode = fc;
4048b1d5caf3SKevin Bowling 		if (sc->num_rx_queues > 1)
4049b1d5caf3SKevin Bowling 			ixgbe_disable_rx_drop(sc);
4050758cc3dcSJack F Vogel 		break;
4051758cc3dcSJack F Vogel 	case ixgbe_fc_none:
4052b1d5caf3SKevin Bowling 		sc->hw.fc.requested_mode = ixgbe_fc_none;
4053b1d5caf3SKevin Bowling 		if (sc->num_rx_queues > 1)
4054b1d5caf3SKevin Bowling 			ixgbe_enable_rx_drop(sc);
4055758cc3dcSJack F Vogel 		break;
4056758cc3dcSJack F Vogel 	default:
4057758cc3dcSJack F Vogel 		return (EINVAL);
4058758cc3dcSJack F Vogel 	}
40598eb6488eSEric Joyner 
4060758cc3dcSJack F Vogel 	/* Don't autoneg if forcing a value */
4061b1d5caf3SKevin Bowling 	sc->hw.fc.disable_fc_autoneg = true;
4062b1d5caf3SKevin Bowling 	ixgbe_fc_enable(&sc->hw);
40638eb6488eSEric Joyner 
4064f2c4db54SSteven Hartland 	return (0);
40658eb6488eSEric Joyner } /* ixgbe_set_flowcntl */
40668eb6488eSEric Joyner 
40678eb6488eSEric Joyner /************************************************************************
40688eb6488eSEric Joyner  * ixgbe_enable_rx_drop
40698eb6488eSEric Joyner  *
40708eb6488eSEric Joyner  *   Enable the hardware to drop packets when the buffer is
40718eb6488eSEric Joyner  *   full. This is useful with multiqueue, so that no single
40728eb6488eSEric Joyner  *   queue being full stalls the entire RX engine. We only
40738eb6488eSEric Joyner  *   enable this when Multiqueue is enabled AND Flow Control
40748eb6488eSEric Joyner  *   is disabled.
40758eb6488eSEric Joyner  ************************************************************************/
40768eb6488eSEric Joyner static void
4077b1d5caf3SKevin Bowling ixgbe_enable_rx_drop(struct ixgbe_softc *sc)
40788eb6488eSEric Joyner {
4079b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
40808eb6488eSEric Joyner 	struct rx_ring  *rxr;
40818eb6488eSEric Joyner 	u32             srrctl;
40828eb6488eSEric Joyner 
4083b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++) {
4084b1d5caf3SKevin Bowling 		rxr = &sc->rx_queues[i].rxr;
40858eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
40868eb6488eSEric Joyner 		srrctl |= IXGBE_SRRCTL_DROP_EN;
40878eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
4088758cc3dcSJack F Vogel 	}
4089758cc3dcSJack F Vogel 
40908eb6488eSEric Joyner 	/* enable drop for each vf */
4091b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_vfs; i++) {
40928eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
40938eb6488eSEric Joyner 		                (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT) |
40948eb6488eSEric Joyner 		                IXGBE_QDE_ENABLE));
40958eb6488eSEric Joyner 	}
40968eb6488eSEric Joyner } /* ixgbe_enable_rx_drop */
40978eb6488eSEric Joyner 
40988eb6488eSEric Joyner /************************************************************************
40998eb6488eSEric Joyner  * ixgbe_disable_rx_drop
41008eb6488eSEric Joyner  ************************************************************************/
41018eb6488eSEric Joyner static void
4102b1d5caf3SKevin Bowling ixgbe_disable_rx_drop(struct ixgbe_softc *sc)
41038eb6488eSEric Joyner {
4104b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
41058eb6488eSEric Joyner 	struct rx_ring  *rxr;
41068eb6488eSEric Joyner 	u32             srrctl;
41078eb6488eSEric Joyner 
4108b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++) {
4109b1d5caf3SKevin Bowling 		rxr = &sc->rx_queues[i].rxr;
41108eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
41118eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_DROP_EN;
41128eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
41138eb6488eSEric Joyner 	}
41148eb6488eSEric Joyner 
41158eb6488eSEric Joyner 	/* disable drop for each vf */
4116b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_vfs; i++) {
41178eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
41188eb6488eSEric Joyner 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT)));
41198eb6488eSEric Joyner 	}
41208eb6488eSEric Joyner } /* ixgbe_disable_rx_drop */
41218eb6488eSEric Joyner 
41228eb6488eSEric Joyner /************************************************************************
41238eb6488eSEric Joyner  * ixgbe_sysctl_advertise
41248eb6488eSEric Joyner  *
41258eb6488eSEric Joyner  *   SYSCTL wrapper around setting advertised speed
41268eb6488eSEric Joyner  ************************************************************************/
4127758cc3dcSJack F Vogel static int
4128f2c4db54SSteven Hartland ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS)
4129758cc3dcSJack F Vogel {
4130b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc;
41318eb6488eSEric Joyner 	int            error, advertise;
4132758cc3dcSJack F Vogel 
4133b1d5caf3SKevin Bowling 	sc = (struct ixgbe_softc *)arg1;
4134b1d5caf3SKevin Bowling 	advertise = sc->advertise;
4135758cc3dcSJack F Vogel 
4136f2c4db54SSteven Hartland 	error = sysctl_handle_int(oidp, &advertise, 0, req);
4137758cc3dcSJack F Vogel 	if ((error) || (req->newptr == NULL))
4138758cc3dcSJack F Vogel 		return (error);
4139758cc3dcSJack F Vogel 
4140b1d5caf3SKevin Bowling 	return ixgbe_set_advertise(sc, advertise);
41418eb6488eSEric Joyner } /* ixgbe_sysctl_advertise */
4142f2c4db54SSteven Hartland 
41438eb6488eSEric Joyner /************************************************************************
41448eb6488eSEric Joyner  * ixgbe_set_advertise - Control advertised link speed
41458eb6488eSEric Joyner  *
41468eb6488eSEric Joyner  *   Flags:
41478eb6488eSEric Joyner  *     0x1  - advertise 100 Mb
41488eb6488eSEric Joyner  *     0x2  - advertise 1G
41498eb6488eSEric Joyner  *     0x4  - advertise 10G
41508eb6488eSEric Joyner  *     0x8  - advertise 10 Mb (yes, Mb)
4151d381c807SPiotr Pietruszewski  *     0x10 - advertise 2.5G (disabled by default)
4152d381c807SPiotr Pietruszewski  *     0x20 - advertise 5G (disabled by default)
4153d381c807SPiotr Pietruszewski  *
41548eb6488eSEric Joyner  ************************************************************************/
4155f2c4db54SSteven Hartland static int
4156b1d5caf3SKevin Bowling ixgbe_set_advertise(struct ixgbe_softc *sc, int advertise)
4157f2c4db54SSteven Hartland {
4158b1d5caf3SKevin Bowling 	device_t         dev = iflib_get_dev(sc->ctx);
4159f2c4db54SSteven Hartland 	struct ixgbe_hw  *hw;
41608eb6488eSEric Joyner 	ixgbe_link_speed speed = 0;
41618eb6488eSEric Joyner 	ixgbe_link_speed link_caps = 0;
41628eb6488eSEric Joyner 	s32              err = IXGBE_NOT_IMPLEMENTED;
416379b36ec9SKevin Bowling 	bool             negotiate = false;
4164f2c4db54SSteven Hartland 
4165d775d23aSSteven Hartland 	/* Checks to validate new value */
4166b1d5caf3SKevin Bowling 	if (sc->advertise == advertise) /* no change */
4167d775d23aSSteven Hartland 		return (0);
4168d775d23aSSteven Hartland 
4169b1d5caf3SKevin Bowling 	hw = &sc->hw;
4170f2c4db54SSteven Hartland 
4171a9ca1c79SSean Bruno 	/* No speed changes for backplane media */
4172a9ca1c79SSean Bruno 	if (hw->phy.media_type == ixgbe_media_type_backplane)
4173a9ca1c79SSean Bruno 		return (ENODEV);
4174a9ca1c79SSean Bruno 
4175758cc3dcSJack F Vogel 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
4176758cc3dcSJack F Vogel 	      (hw->phy.multispeed_fiber))) {
41778eb6488eSEric Joyner 		device_printf(dev, "Advertised speed can only be set on copper or multispeed fiber media types.\n");
4178758cc3dcSJack F Vogel 		return (EINVAL);
4179758cc3dcSJack F Vogel 	}
4180758cc3dcSJack F Vogel 
4181d381c807SPiotr Pietruszewski 	if (advertise < 0x1 || advertise > 0x3F) {
4182d381c807SPiotr Pietruszewski 		device_printf(dev, "Invalid advertised speed; valid modes are 0x1 through 0x3F\n");
4183758cc3dcSJack F Vogel 		return (EINVAL);
4184758cc3dcSJack F Vogel 	}
4185758cc3dcSJack F Vogel 
41868eb6488eSEric Joyner 	if (hw->mac.ops.get_link_capabilities) {
41878eb6488eSEric Joyner 		err = hw->mac.ops.get_link_capabilities(hw, &link_caps,
41888eb6488eSEric Joyner 		    &negotiate);
41898eb6488eSEric Joyner 		if (err != IXGBE_SUCCESS) {
41908eb6488eSEric Joyner 			device_printf(dev, "Unable to determine supported advertise speeds\n");
41918eb6488eSEric Joyner 			return (ENODEV);
41928eb6488eSEric Joyner 		}
4193758cc3dcSJack F Vogel 	}
4194758cc3dcSJack F Vogel 
4195758cc3dcSJack F Vogel 	/* Set new value and report new advertised mode */
41968eb6488eSEric Joyner 	if (advertise & 0x1) {
41978eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_100_FULL)) {
41988eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 100Mb advertised speed\n");
41998eb6488eSEric Joyner 			return (EINVAL);
42008eb6488eSEric Joyner 		}
4201758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_100_FULL;
42028eb6488eSEric Joyner 	}
42038eb6488eSEric Joyner 	if (advertise & 0x2) {
42048eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_1GB_FULL)) {
42058eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 1Gb advertised speed\n");
42068eb6488eSEric Joyner 			return (EINVAL);
42078eb6488eSEric Joyner 		}
4208758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
42098eb6488eSEric Joyner 	}
42108eb6488eSEric Joyner 	if (advertise & 0x4) {
42118eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_10GB_FULL)) {
42128eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 10Gb advertised speed\n");
42138eb6488eSEric Joyner 			return (EINVAL);
42148eb6488eSEric Joyner 		}
4215758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
42168eb6488eSEric Joyner 	}
42178eb6488eSEric Joyner 	if (advertise & 0x8) {
42188eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_10_FULL)) {
42198eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 10Mb advertised speed\n");
42208eb6488eSEric Joyner 			return (EINVAL);
42218eb6488eSEric Joyner 		}
42228eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10_FULL;
42238eb6488eSEric Joyner 	}
4224d381c807SPiotr Pietruszewski 	if (advertise & 0x10) {
4225d381c807SPiotr Pietruszewski 		if (!(link_caps & IXGBE_LINK_SPEED_2_5GB_FULL)) {
4226d381c807SPiotr Pietruszewski 			device_printf(dev, "Interface does not support 2.5G advertised speed\n");
4227d381c807SPiotr Pietruszewski 			return (EINVAL);
4228d381c807SPiotr Pietruszewski 		}
4229d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
4230d381c807SPiotr Pietruszewski 	}
4231d381c807SPiotr Pietruszewski 	if (advertise & 0x20) {
4232d381c807SPiotr Pietruszewski 		if (!(link_caps & IXGBE_LINK_SPEED_5GB_FULL)) {
4233d381c807SPiotr Pietruszewski 			device_printf(dev, "Interface does not support 5G advertised speed\n");
4234d381c807SPiotr Pietruszewski 			return (EINVAL);
4235d381c807SPiotr Pietruszewski 		}
4236d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
4237d381c807SPiotr Pietruszewski 	}
4238758cc3dcSJack F Vogel 
423979b36ec9SKevin Bowling 	hw->mac.autotry_restart = true;
424079b36ec9SKevin Bowling 	hw->mac.ops.setup_link(hw, speed, true);
4241b1d5caf3SKevin Bowling 	sc->advertise = advertise;
4242758cc3dcSJack F Vogel 
4243f2c4db54SSteven Hartland 	return (0);
42448eb6488eSEric Joyner } /* ixgbe_set_advertise */
42458eb6488eSEric Joyner 
42468eb6488eSEric Joyner /************************************************************************
4247d381c807SPiotr Pietruszewski  * ixgbe_get_default_advertise - Get default advertised speed settings
42488eb6488eSEric Joyner  *
42498eb6488eSEric Joyner  *   Formatted for sysctl usage.
42508eb6488eSEric Joyner  *   Flags:
42518eb6488eSEric Joyner  *     0x1 - advertise 100 Mb
42528eb6488eSEric Joyner  *     0x2 - advertise 1G
42538eb6488eSEric Joyner  *     0x4 - advertise 10G
42548eb6488eSEric Joyner  *     0x8 - advertise 10 Mb (yes, Mb)
4255d381c807SPiotr Pietruszewski  *     0x10 - advertise 2.5G (disabled by default)
4256d381c807SPiotr Pietruszewski  *     0x20 - advertise 5G (disabled by default)
42578eb6488eSEric Joyner  ************************************************************************/
42588eb6488eSEric Joyner static int
4259d381c807SPiotr Pietruszewski ixgbe_get_default_advertise(struct ixgbe_softc *sc)
42608eb6488eSEric Joyner {
4261b1d5caf3SKevin Bowling 	struct ixgbe_hw  *hw = &sc->hw;
42628eb6488eSEric Joyner 	int              speed;
42638eb6488eSEric Joyner 	ixgbe_link_speed link_caps = 0;
42648eb6488eSEric Joyner 	s32              err;
426579b36ec9SKevin Bowling 	bool             negotiate = false;
4266758cc3dcSJack F Vogel 
4267758cc3dcSJack F Vogel 	/*
42688eb6488eSEric Joyner 	 * Advertised speed means nothing unless it's copper or
42698eb6488eSEric Joyner 	 * multi-speed fiber
4270758cc3dcSJack F Vogel 	 */
42718eb6488eSEric Joyner 	if (!(hw->phy.media_type == ixgbe_media_type_copper) &&
42728eb6488eSEric Joyner 	    !(hw->phy.multispeed_fiber))
4273758cc3dcSJack F Vogel 		return (0);
4274758cc3dcSJack F Vogel 
42758eb6488eSEric Joyner 	err = hw->mac.ops.get_link_capabilities(hw, &link_caps, &negotiate);
42768eb6488eSEric Joyner 	if (err != IXGBE_SUCCESS)
42778eb6488eSEric Joyner 		return (0);
42788eb6488eSEric Joyner 
4279d381c807SPiotr Pietruszewski 	if (hw->mac.type == ixgbe_mac_X550) {
4280d381c807SPiotr Pietruszewski 		/*
4281d381c807SPiotr Pietruszewski 		 * 2.5G and 5G autonegotiation speeds on X550
4282d381c807SPiotr Pietruszewski 		 * are disabled by default due to reported
4283d381c807SPiotr Pietruszewski 		 * interoperability issues with some switches.
4284d381c807SPiotr Pietruszewski 		 */
4285d381c807SPiotr Pietruszewski 		link_caps &= ~(IXGBE_LINK_SPEED_2_5GB_FULL |
4286d381c807SPiotr Pietruszewski 		    IXGBE_LINK_SPEED_5GB_FULL);
4287d381c807SPiotr Pietruszewski 	}
4288d381c807SPiotr Pietruszewski 
42898eb6488eSEric Joyner 	speed =
4290d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_10GB_FULL)  ? 0x4  : 0) |
4291d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_5GB_FULL)   ? 0x20 : 0) |
4292d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
4293d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_1GB_FULL)   ? 0x2  : 0) |
4294d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_100_FULL)   ? 0x1  : 0) |
4295d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_10_FULL)    ? 0x8  : 0);
42968eb6488eSEric Joyner 
42978eb6488eSEric Joyner 	return speed;
4298d381c807SPiotr Pietruszewski } /* ixgbe_get_default_advertise */
42998eb6488eSEric Joyner 
43008eb6488eSEric Joyner /************************************************************************
43018eb6488eSEric Joyner  * ixgbe_sysctl_dmac - Manage DMA Coalescing
43028eb6488eSEric Joyner  *
43038eb6488eSEric Joyner  *   Control values:
43048eb6488eSEric Joyner  *     0/1 - off / on (use default value of 1000)
43058eb6488eSEric Joyner  *
43068eb6488eSEric Joyner  *     Legal timer values are:
43078eb6488eSEric Joyner  *     50,100,250,500,1000,2000,5000,10000
43088eb6488eSEric Joyner  *
43098eb6488eSEric Joyner  *     Turning off interrupt moderation will also turn this off.
43108eb6488eSEric Joyner  ************************************************************************/
43116f37f232SEric Joyner static int
43126f37f232SEric Joyner ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS)
43136f37f232SEric Joyner {
4314b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4315ff06a8dbSJustin Hibbits 	if_t           ifp = iflib_get_ifp(sc->ctx);
43166f37f232SEric Joyner 	int            error;
4317c19c7afeSEric Joyner 	u16            newval;
43186f37f232SEric Joyner 
4319b1d5caf3SKevin Bowling 	newval = sc->dmac;
4320c19c7afeSEric Joyner 	error = sysctl_handle_16(oidp, &newval, 0, req);
43216f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
43226f37f232SEric Joyner 		return (error);
43236f37f232SEric Joyner 
4324a9ca1c79SSean Bruno 	switch (newval) {
43256f37f232SEric Joyner 	case 0:
43266f37f232SEric Joyner 		/* Disabled */
4327b1d5caf3SKevin Bowling 		sc->dmac = 0;
43286f37f232SEric Joyner 		break;
4329a9ca1c79SSean Bruno 	case 1:
4330a9ca1c79SSean Bruno 		/* Enable and use default */
4331b1d5caf3SKevin Bowling 		sc->dmac = 1000;
43326f37f232SEric Joyner 		break;
43336f37f232SEric Joyner 	case 50:
43346f37f232SEric Joyner 	case 100:
43356f37f232SEric Joyner 	case 250:
43366f37f232SEric Joyner 	case 500:
43376f37f232SEric Joyner 	case 1000:
43386f37f232SEric Joyner 	case 2000:
43396f37f232SEric Joyner 	case 5000:
43406f37f232SEric Joyner 	case 10000:
43416f37f232SEric Joyner 		/* Legal values - allow */
4342b1d5caf3SKevin Bowling 		sc->dmac = newval;
43436f37f232SEric Joyner 		break;
43446f37f232SEric Joyner 	default:
43456f37f232SEric Joyner 		/* Do nothing, illegal value */
43466f37f232SEric Joyner 		return (EINVAL);
43476f37f232SEric Joyner 	}
43486f37f232SEric Joyner 
43496f37f232SEric Joyner 	/* Re-initialize hardware if it's already running */
4350ff06a8dbSJustin Hibbits 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
4351ff06a8dbSJustin Hibbits 		if_init(ifp, ifp);
43526f37f232SEric Joyner 
43536f37f232SEric Joyner 	return (0);
43548eb6488eSEric Joyner } /* ixgbe_sysctl_dmac */
43556f37f232SEric Joyner 
4356a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
43578eb6488eSEric Joyner /************************************************************************
43588eb6488eSEric Joyner  * ixgbe_sysctl_power_state
43598eb6488eSEric Joyner  *
4360a9ca1c79SSean Bruno  *   Sysctl to test power states
4361a9ca1c79SSean Bruno  *   Values:
4362a9ca1c79SSean Bruno  *     0      - set device to D0
4363a9ca1c79SSean Bruno  *     3      - set device to D3
4364a9ca1c79SSean Bruno  *     (none) - get current device power state
43658eb6488eSEric Joyner  ************************************************************************/
4366a9ca1c79SSean Bruno static int
4367a9ca1c79SSean Bruno ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS)
4368a9ca1c79SSean Bruno {
4369b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4370b1d5caf3SKevin Bowling 	device_t       dev = sc->dev;
4371a9ca1c79SSean Bruno 	int            curr_ps, new_ps, error = 0;
4372a9ca1c79SSean Bruno 
4373a9ca1c79SSean Bruno 	curr_ps = new_ps = pci_get_powerstate(dev);
4374a9ca1c79SSean Bruno 
4375a9ca1c79SSean Bruno 	error = sysctl_handle_int(oidp, &new_ps, 0, req);
4376a9ca1c79SSean Bruno 	if ((error) || (req->newptr == NULL))
4377a9ca1c79SSean Bruno 		return (error);
4378a9ca1c79SSean Bruno 
4379a9ca1c79SSean Bruno 	if (new_ps == curr_ps)
4380a9ca1c79SSean Bruno 		return (0);
4381a9ca1c79SSean Bruno 
4382a9ca1c79SSean Bruno 	if (new_ps == 3 && curr_ps == 0)
4383a9ca1c79SSean Bruno 		error = DEVICE_SUSPEND(dev);
4384a9ca1c79SSean Bruno 	else if (new_ps == 0 && curr_ps == 3)
4385a9ca1c79SSean Bruno 		error = DEVICE_RESUME(dev);
4386a9ca1c79SSean Bruno 	else
4387a9ca1c79SSean Bruno 		return (EINVAL);
4388a9ca1c79SSean Bruno 
4389a9ca1c79SSean Bruno 	device_printf(dev, "New state: %d\n", pci_get_powerstate(dev));
4390a9ca1c79SSean Bruno 
4391a9ca1c79SSean Bruno 	return (error);
43928eb6488eSEric Joyner } /* ixgbe_sysctl_power_state */
4393a9ca1c79SSean Bruno #endif
43948eb6488eSEric Joyner 
43958eb6488eSEric Joyner /************************************************************************
43968eb6488eSEric Joyner  * ixgbe_sysctl_wol_enable
43978eb6488eSEric Joyner  *
43988eb6488eSEric Joyner  *   Sysctl to enable/disable the WoL capability,
43998eb6488eSEric Joyner  *   if supported by the adapter.
44008eb6488eSEric Joyner  *
44016f37f232SEric Joyner  *   Values:
44026f37f232SEric Joyner  *     0 - disabled
44036f37f232SEric Joyner  *     1 - enabled
44048eb6488eSEric Joyner  ************************************************************************/
44056f37f232SEric Joyner static int
44066f37f232SEric Joyner ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS)
44076f37f232SEric Joyner {
4408b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4409b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
44106f37f232SEric Joyner 	int             new_wol_enabled;
44116f37f232SEric Joyner 	int             error = 0;
44126f37f232SEric Joyner 
44136f37f232SEric Joyner 	new_wol_enabled = hw->wol_enabled;
44146f37f232SEric Joyner 	error = sysctl_handle_int(oidp, &new_wol_enabled, 0, req);
44156f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
44166f37f232SEric Joyner 		return (error);
4417a9ca1c79SSean Bruno 	new_wol_enabled = !!(new_wol_enabled);
44186f37f232SEric Joyner 	if (new_wol_enabled == hw->wol_enabled)
44196f37f232SEric Joyner 		return (0);
44206f37f232SEric Joyner 
4421b1d5caf3SKevin Bowling 	if (new_wol_enabled > 0 && !sc->wol_support)
44226f37f232SEric Joyner 		return (ENODEV);
44236f37f232SEric Joyner 	else
4424a9ca1c79SSean Bruno 		hw->wol_enabled = new_wol_enabled;
44256f37f232SEric Joyner 
44266f37f232SEric Joyner 	return (0);
44278eb6488eSEric Joyner } /* ixgbe_sysctl_wol_enable */
44286f37f232SEric Joyner 
44298eb6488eSEric Joyner /************************************************************************
44308eb6488eSEric Joyner  * ixgbe_sysctl_wufc - Wake Up Filter Control
44318eb6488eSEric Joyner  *
44326f37f232SEric Joyner  *   Sysctl to enable/disable the types of packets that the
44336f37f232SEric Joyner  *   adapter will wake up on upon receipt.
44346f37f232SEric Joyner  *   Flags:
44356f37f232SEric Joyner  *     0x1  - Link Status Change
44366f37f232SEric Joyner  *     0x2  - Magic Packet
44376f37f232SEric Joyner  *     0x4  - Direct Exact
44386f37f232SEric Joyner  *     0x8  - Directed Multicast
44396f37f232SEric Joyner  *     0x10 - Broadcast
44406f37f232SEric Joyner  *     0x20 - ARP/IPv4 Request Packet
44416f37f232SEric Joyner  *     0x40 - Direct IPv4 Packet
44426f37f232SEric Joyner  *     0x80 - Direct IPv6 Packet
44436f37f232SEric Joyner  *
44448eb6488eSEric Joyner  *   Settings not listed above will cause the sysctl to return an error.
44458eb6488eSEric Joyner  ************************************************************************/
44466f37f232SEric Joyner static int
44476f37f232SEric Joyner ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS)
44486f37f232SEric Joyner {
4449b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
44506f37f232SEric Joyner 	int            error = 0;
44516f37f232SEric Joyner 	u32            new_wufc;
44526f37f232SEric Joyner 
4453b1d5caf3SKevin Bowling 	new_wufc = sc->wufc;
44546f37f232SEric Joyner 
4455c19c7afeSEric Joyner 	error = sysctl_handle_32(oidp, &new_wufc, 0, req);
44566f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
44576f37f232SEric Joyner 		return (error);
4458b1d5caf3SKevin Bowling 	if (new_wufc == sc->wufc)
44596f37f232SEric Joyner 		return (0);
44606f37f232SEric Joyner 
44616f37f232SEric Joyner 	if (new_wufc & 0xffffff00)
44626f37f232SEric Joyner 		return (EINVAL);
44638eb6488eSEric Joyner 
44646f37f232SEric Joyner 	new_wufc &= 0xff;
4465b1d5caf3SKevin Bowling 	new_wufc |= (0xffffff & sc->wufc);
4466b1d5caf3SKevin Bowling 	sc->wufc = new_wufc;
44676f37f232SEric Joyner 
44686f37f232SEric Joyner 	return (0);
44698eb6488eSEric Joyner } /* ixgbe_sysctl_wufc */
44706f37f232SEric Joyner 
4471a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
44728eb6488eSEric Joyner /************************************************************************
44738eb6488eSEric Joyner  * ixgbe_sysctl_print_rss_config
44748eb6488eSEric Joyner  ************************************************************************/
4475a9ca1c79SSean Bruno static int
4476a9ca1c79SSean Bruno ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS)
4477a9ca1c79SSean Bruno {
4478b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4479b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
4480b1d5caf3SKevin Bowling 	device_t        dev = sc->dev;
4481a9ca1c79SSean Bruno 	struct sbuf     *buf;
44828eb6488eSEric Joyner 	int             error = 0, reta_size;
4483a9ca1c79SSean Bruno 	u32             reg;
4484a9ca1c79SSean Bruno 
4485a9ca1c79SSean Bruno 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
4486a9ca1c79SSean Bruno 	if (!buf) {
4487a9ca1c79SSean Bruno 		device_printf(dev, "Could not allocate sbuf for output.\n");
4488a9ca1c79SSean Bruno 		return (ENOMEM);
4489a9ca1c79SSean Bruno 	}
4490a9ca1c79SSean Bruno 
4491a9ca1c79SSean Bruno 	// TODO: use sbufs to make a string to print out
4492a9ca1c79SSean Bruno 	/* Set multiplier for RETA setup and table size based on MAC */
4493b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
4494a9ca1c79SSean Bruno 	case ixgbe_mac_X550:
4495a9ca1c79SSean Bruno 	case ixgbe_mac_X550EM_x:
44968eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
4497a9ca1c79SSean Bruno 		reta_size = 128;
4498a9ca1c79SSean Bruno 		break;
4499a9ca1c79SSean Bruno 	default:
4500a9ca1c79SSean Bruno 		reta_size = 32;
4501a9ca1c79SSean Bruno 		break;
4502a9ca1c79SSean Bruno 	}
4503a9ca1c79SSean Bruno 
4504a9ca1c79SSean Bruno 	/* Print out the redirection table */
4505a9ca1c79SSean Bruno 	sbuf_cat(buf, "\n");
4506a9ca1c79SSean Bruno 	for (int i = 0; i < reta_size; i++) {
4507a9ca1c79SSean Bruno 		if (i < 32) {
4508a9ca1c79SSean Bruno 			reg = IXGBE_READ_REG(hw, IXGBE_RETA(i));
4509a9ca1c79SSean Bruno 			sbuf_printf(buf, "RETA(%2d): 0x%08x\n", i, reg);
4510a9ca1c79SSean Bruno 		} else {
4511a9ca1c79SSean Bruno 			reg = IXGBE_READ_REG(hw, IXGBE_ERETA(i - 32));
4512a9ca1c79SSean Bruno 			sbuf_printf(buf, "ERETA(%2d): 0x%08x\n", i - 32, reg);
4513a9ca1c79SSean Bruno 		}
4514a9ca1c79SSean Bruno 	}
4515a9ca1c79SSean Bruno 
4516a9ca1c79SSean Bruno 	// TODO: print more config
4517a9ca1c79SSean Bruno 
4518a9ca1c79SSean Bruno 	error = sbuf_finish(buf);
4519a9ca1c79SSean Bruno 	if (error)
4520a9ca1c79SSean Bruno 		device_printf(dev, "Error finishing sbuf: %d\n", error);
4521a9ca1c79SSean Bruno 
4522a9ca1c79SSean Bruno 	sbuf_delete(buf);
45238eb6488eSEric Joyner 
4524a9ca1c79SSean Bruno 	return (0);
45258eb6488eSEric Joyner } /* ixgbe_sysctl_print_rss_config */
4526a9ca1c79SSean Bruno #endif /* IXGBE_DEBUG */
4527a9ca1c79SSean Bruno 
45288eb6488eSEric Joyner /************************************************************************
45298eb6488eSEric Joyner  * ixgbe_sysctl_phy_temp - Retrieve temperature of PHY
45308eb6488eSEric Joyner  *
45318eb6488eSEric Joyner  *   For X552/X557-AT devices using an external PHY
45328eb6488eSEric Joyner  ************************************************************************/
45338eb6488eSEric Joyner static int
45348eb6488eSEric Joyner ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS)
45358eb6488eSEric Joyner {
4536b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4537b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
45388eb6488eSEric Joyner 	u16             reg;
45398eb6488eSEric Joyner 
45408eb6488eSEric Joyner 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4541b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45428eb6488eSEric Joyner 		    "Device has no supported external thermal sensor.\n");
45438eb6488eSEric Joyner 		return (ENODEV);
45448eb6488eSEric Joyner 	}
45458eb6488eSEric Joyner 
45468eb6488eSEric Joyner 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_CURRENT_TEMP,
45478eb6488eSEric Joyner 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4548b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45498eb6488eSEric Joyner 		    "Error reading from PHY's current temperature register\n");
45508eb6488eSEric Joyner 		return (EAGAIN);
45518eb6488eSEric Joyner 	}
45528eb6488eSEric Joyner 
45538eb6488eSEric Joyner 	/* Shift temp for output */
45548eb6488eSEric Joyner 	reg = reg >> 8;
45558eb6488eSEric Joyner 
4556c19c7afeSEric Joyner 	return (sysctl_handle_16(oidp, NULL, reg, req));
45578eb6488eSEric Joyner } /* ixgbe_sysctl_phy_temp */
45588eb6488eSEric Joyner 
45598eb6488eSEric Joyner /************************************************************************
45608eb6488eSEric Joyner  * ixgbe_sysctl_phy_overtemp_occurred
45618eb6488eSEric Joyner  *
45628eb6488eSEric Joyner  *   Reports (directly from the PHY) whether the current PHY
45638eb6488eSEric Joyner  *   temperature is over the overtemp threshold.
45648eb6488eSEric Joyner  ************************************************************************/
45658eb6488eSEric Joyner static int
45668eb6488eSEric Joyner ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS)
45678eb6488eSEric Joyner {
4568b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4569b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
45708eb6488eSEric Joyner 	u16             reg;
45718eb6488eSEric Joyner 
45728eb6488eSEric Joyner 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4573b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45748eb6488eSEric Joyner 		    "Device has no supported external thermal sensor.\n");
45758eb6488eSEric Joyner 		return (ENODEV);
45768eb6488eSEric Joyner 	}
45778eb6488eSEric Joyner 
45788eb6488eSEric Joyner 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_OVERTEMP_STATUS,
45798eb6488eSEric Joyner 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4580b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45818eb6488eSEric Joyner 		    "Error reading from PHY's temperature status register\n");
45828eb6488eSEric Joyner 		return (EAGAIN);
45838eb6488eSEric Joyner 	}
45848eb6488eSEric Joyner 
45858eb6488eSEric Joyner 	/* Get occurrence bit */
45868eb6488eSEric Joyner 	reg = !!(reg & 0x4000);
45878eb6488eSEric Joyner 
4588c19c7afeSEric Joyner 	return (sysctl_handle_16(oidp, 0, reg, req));
45898eb6488eSEric Joyner } /* ixgbe_sysctl_phy_overtemp_occurred */
45908eb6488eSEric Joyner 
45918eb6488eSEric Joyner /************************************************************************
45928eb6488eSEric Joyner  * ixgbe_sysctl_eee_state
45938eb6488eSEric Joyner  *
45948eb6488eSEric Joyner  *   Sysctl to set EEE power saving feature
45958eb6488eSEric Joyner  *   Values:
45968eb6488eSEric Joyner  *     0      - disable EEE
45978eb6488eSEric Joyner  *     1      - enable EEE
45988eb6488eSEric Joyner  *     (none) - get current device EEE state
45998eb6488eSEric Joyner  ************************************************************************/
46008eb6488eSEric Joyner static int
46018eb6488eSEric Joyner ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS)
46028eb6488eSEric Joyner {
4603b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4604b1d5caf3SKevin Bowling 	device_t       dev = sc->dev;
4605ff06a8dbSJustin Hibbits 	if_t           ifp = iflib_get_ifp(sc->ctx);
46068eb6488eSEric Joyner 	int            curr_eee, new_eee, error = 0;
46078eb6488eSEric Joyner 	s32            retval;
46088eb6488eSEric Joyner 
4609b1d5caf3SKevin Bowling 	curr_eee = new_eee = !!(sc->feat_en & IXGBE_FEATURE_EEE);
46108eb6488eSEric Joyner 
46118eb6488eSEric Joyner 	error = sysctl_handle_int(oidp, &new_eee, 0, req);
46128eb6488eSEric Joyner 	if ((error) || (req->newptr == NULL))
46138eb6488eSEric Joyner 		return (error);
46148eb6488eSEric Joyner 
46158eb6488eSEric Joyner 	/* Nothing to do */
46168eb6488eSEric Joyner 	if (new_eee == curr_eee)
46178eb6488eSEric Joyner 		return (0);
46188eb6488eSEric Joyner 
46198eb6488eSEric Joyner 	/* Not supported */
4620b1d5caf3SKevin Bowling 	if (!(sc->feat_cap & IXGBE_FEATURE_EEE))
46218eb6488eSEric Joyner 		return (EINVAL);
46228eb6488eSEric Joyner 
46238eb6488eSEric Joyner 	/* Bounds checking */
46248eb6488eSEric Joyner 	if ((new_eee < 0) || (new_eee > 1))
46258eb6488eSEric Joyner 		return (EINVAL);
46268eb6488eSEric Joyner 
4627b1d5caf3SKevin Bowling 	retval = ixgbe_setup_eee(&sc->hw, new_eee);
46288eb6488eSEric Joyner 	if (retval) {
46298eb6488eSEric Joyner 		device_printf(dev, "Error in EEE setup: 0x%08X\n", retval);
46308eb6488eSEric Joyner 		return (EINVAL);
46318eb6488eSEric Joyner 	}
46328eb6488eSEric Joyner 
46338eb6488eSEric Joyner 	/* Restart auto-neg */
4634ff06a8dbSJustin Hibbits 	if_init(ifp, ifp);
46358eb6488eSEric Joyner 
46368eb6488eSEric Joyner 	device_printf(dev, "New EEE state: %d\n", new_eee);
46378eb6488eSEric Joyner 
46388eb6488eSEric Joyner 	/* Cache new value */
46398eb6488eSEric Joyner 	if (new_eee)
4640b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_EEE;
46418eb6488eSEric Joyner 	else
4642b1d5caf3SKevin Bowling 		sc->feat_en &= ~IXGBE_FEATURE_EEE;
46438eb6488eSEric Joyner 
46448eb6488eSEric Joyner 	return (error);
46458eb6488eSEric Joyner } /* ixgbe_sysctl_eee_state */
46468eb6488eSEric Joyner 
46478eb6488eSEric Joyner /************************************************************************
46488eb6488eSEric Joyner  * ixgbe_init_device_features
46498eb6488eSEric Joyner  ************************************************************************/
46508eb6488eSEric Joyner static void
4651b1d5caf3SKevin Bowling ixgbe_init_device_features(struct ixgbe_softc *sc)
46528eb6488eSEric Joyner {
4653b1d5caf3SKevin Bowling 	sc->feat_cap = IXGBE_FEATURE_NETMAP
46548eb6488eSEric Joyner 	                  | IXGBE_FEATURE_RSS
46558eb6488eSEric Joyner 	                  | IXGBE_FEATURE_MSI
46568eb6488eSEric Joyner 	                  | IXGBE_FEATURE_MSIX
4657c19c7afeSEric Joyner 	                  | IXGBE_FEATURE_LEGACY_IRQ;
46588eb6488eSEric Joyner 
46598eb6488eSEric Joyner 	/* Set capabilities first... */
4660b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
46618eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
4662b1d5caf3SKevin Bowling 		if (sc->hw.device_id == IXGBE_DEV_ID_82598AT)
4663b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_FAN_FAIL;
46648eb6488eSEric Joyner 		break;
46658eb6488eSEric Joyner 	case ixgbe_mac_X540:
4666b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4667b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4668b1d5caf3SKevin Bowling 		if ((sc->hw.device_id == IXGBE_DEV_ID_X540_BYPASS) &&
4669b1d5caf3SKevin Bowling 		    (sc->hw.bus.func == 0))
4670b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_BYPASS;
46718eb6488eSEric Joyner 		break;
46728eb6488eSEric Joyner 	case ixgbe_mac_X550:
4673b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
4674b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4675b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
46768eb6488eSEric Joyner 		break;
46778eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
4678b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4679b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
46808eb6488eSEric Joyner 		break;
46818eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
4682b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4683b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4684b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
4685b1d5caf3SKevin Bowling 		if ((sc->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T) ||
4686b1d5caf3SKevin Bowling 		    (sc->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T_L)) {
4687b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
4688b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_EEE;
46898eb6488eSEric Joyner 		}
46908eb6488eSEric Joyner 		break;
46918eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
4692b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4693b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4694b1d5caf3SKevin Bowling 		if ((sc->hw.device_id == IXGBE_DEV_ID_82599_BYPASS) &&
4695b1d5caf3SKevin Bowling 		    (sc->hw.bus.func == 0))
4696b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_BYPASS;
4697b1d5caf3SKevin Bowling 		if (sc->hw.device_id == IXGBE_DEV_ID_82599_QSFP_SF_QP)
4698b1d5caf3SKevin Bowling 			sc->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
46998eb6488eSEric Joyner 		break;
47008eb6488eSEric Joyner 	default:
47018eb6488eSEric Joyner 		break;
47028eb6488eSEric Joyner 	}
47038eb6488eSEric Joyner 
47048eb6488eSEric Joyner 	/* Enabled by default... */
47058eb6488eSEric Joyner 	/* Fan failure detection */
4706b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_FAN_FAIL)
4707b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_FAN_FAIL;
47088eb6488eSEric Joyner 	/* Netmap */
4709b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_NETMAP)
4710b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_NETMAP;
47118eb6488eSEric Joyner 	/* EEE */
4712b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_EEE)
4713b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_EEE;
47148eb6488eSEric Joyner 	/* Thermal Sensor */
4715b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_TEMP_SENSOR)
4716b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_TEMP_SENSOR;
47178eb6488eSEric Joyner 
47188eb6488eSEric Joyner 	/* Enabled via global sysctl... */
47198eb6488eSEric Joyner 	/* Flow Director */
47208eb6488eSEric Joyner 	if (ixgbe_enable_fdir) {
4721b1d5caf3SKevin Bowling 		if (sc->feat_cap & IXGBE_FEATURE_FDIR)
4722b1d5caf3SKevin Bowling 			sc->feat_en |= IXGBE_FEATURE_FDIR;
47238eb6488eSEric Joyner 		else
4724b1d5caf3SKevin Bowling 			device_printf(sc->dev, "Device does not support Flow Director. Leaving disabled.");
47258eb6488eSEric Joyner 	}
47266f37f232SEric Joyner 	/*
47278eb6488eSEric Joyner 	 * Message Signal Interrupts - Extended (MSI-X)
47288eb6488eSEric Joyner 	 * Normal MSI is only enabled if MSI-X calls fail.
4729758cc3dcSJack F Vogel 	 */
47308eb6488eSEric Joyner 	if (!ixgbe_enable_msix)
4731b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_MSIX;
47328eb6488eSEric Joyner 	/* Receive-Side Scaling (RSS) */
4733b1d5caf3SKevin Bowling 	if ((sc->feat_cap & IXGBE_FEATURE_RSS) && ixgbe_enable_rss)
4734b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_RSS;
47358eb6488eSEric Joyner 
47368eb6488eSEric Joyner 	/* Disable features with unmet dependencies... */
47378eb6488eSEric Joyner 	/* No MSI-X */
4738b1d5caf3SKevin Bowling 	if (!(sc->feat_cap & IXGBE_FEATURE_MSIX)) {
4739b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_RSS;
4740b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_SRIOV;
4741b1d5caf3SKevin Bowling 		sc->feat_en &= ~IXGBE_FEATURE_RSS;
4742b1d5caf3SKevin Bowling 		sc->feat_en &= ~IXGBE_FEATURE_SRIOV;
47438eb6488eSEric Joyner 	}
47448eb6488eSEric Joyner } /* ixgbe_init_device_features */
47458eb6488eSEric Joyner 
47468eb6488eSEric Joyner /************************************************************************
47478eb6488eSEric Joyner  * ixgbe_check_fan_failure
47488eb6488eSEric Joyner  ************************************************************************/
47498eb6488eSEric Joyner static void
4750b1d5caf3SKevin Bowling ixgbe_check_fan_failure(struct ixgbe_softc *sc, u32 reg, bool in_interrupt)
47518eb6488eSEric Joyner {
47528eb6488eSEric Joyner 	u32 mask;
47538eb6488eSEric Joyner 
4754b1d5caf3SKevin Bowling 	mask = (in_interrupt) ? IXGBE_EICR_GPI_SDP1_BY_MAC(&sc->hw) :
47558eb6488eSEric Joyner 	    IXGBE_ESDP_SDP1;
47568eb6488eSEric Joyner 
47578eb6488eSEric Joyner 	if (reg & mask)
4758b1d5caf3SKevin Bowling 		device_printf(sc->dev, "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
47598eb6488eSEric Joyner } /* ixgbe_check_fan_failure */
47607660e4eaSKevin Bowling 
47617660e4eaSKevin Bowling /************************************************************************
47627660e4eaSKevin Bowling  * ixgbe_sbuf_fw_version
47637660e4eaSKevin Bowling  ************************************************************************/
47647660e4eaSKevin Bowling static void
47657660e4eaSKevin Bowling ixgbe_sbuf_fw_version(struct ixgbe_hw *hw, struct sbuf *buf)
47667660e4eaSKevin Bowling {
47677660e4eaSKevin Bowling 	struct ixgbe_nvm_version nvm_ver = {0};
47687660e4eaSKevin Bowling 	uint16_t phyfw = 0;
47697660e4eaSKevin Bowling 	int status;
47705de5419bSKevin Bowling 	const char *space = "";
47717660e4eaSKevin Bowling 
47727660e4eaSKevin Bowling 	ixgbe_get_oem_prod_version(hw, &nvm_ver); /* OEM's NVM version */
47737660e4eaSKevin Bowling 	ixgbe_get_orom_version(hw, &nvm_ver); /* Option ROM */
47747660e4eaSKevin Bowling 	ixgbe_get_etk_id(hw, &nvm_ver); /* eTrack identifies a build in Intel's SCM */
47757660e4eaSKevin Bowling 	status = ixgbe_get_phy_firmware_version(hw, &phyfw);
47767660e4eaSKevin Bowling 
47775de5419bSKevin Bowling 	if (nvm_ver.oem_valid) {
47787660e4eaSKevin Bowling 		sbuf_printf(buf, "NVM OEM V%d.%d R%d", nvm_ver.oem_major,
47797660e4eaSKevin Bowling 		    nvm_ver.oem_minor, nvm_ver.oem_release);
47805de5419bSKevin Bowling 		space = " ";
47815de5419bSKevin Bowling 	}
47827660e4eaSKevin Bowling 
47835de5419bSKevin Bowling 	if (nvm_ver.or_valid) {
47845de5419bSKevin Bowling 		sbuf_printf(buf, "%sOption ROM V%d-b%d-p%d",
47855de5419bSKevin Bowling 		    space, nvm_ver.or_major, nvm_ver.or_build, nvm_ver.or_patch);
47865de5419bSKevin Bowling 		space = " ";
47875de5419bSKevin Bowling 	}
47887660e4eaSKevin Bowling 
47895de5419bSKevin Bowling 	if (nvm_ver.etk_id != ((NVM_VER_INVALID << NVM_ETK_SHIFT) |
47905de5419bSKevin Bowling 	    NVM_VER_INVALID)) {
47915de5419bSKevin Bowling 		sbuf_printf(buf, "%seTrack 0x%08x", space, nvm_ver.etk_id);
47925de5419bSKevin Bowling 		space = " ";
47935de5419bSKevin Bowling 	}
47947660e4eaSKevin Bowling 
47957660e4eaSKevin Bowling 	if (phyfw != 0 && status == IXGBE_SUCCESS)
47965de5419bSKevin Bowling 		sbuf_printf(buf, "%sPHY FW V%d", space, phyfw);
47977660e4eaSKevin Bowling } /* ixgbe_sbuf_fw_version */
47987660e4eaSKevin Bowling 
47997660e4eaSKevin Bowling /************************************************************************
48007660e4eaSKevin Bowling  * ixgbe_print_fw_version
48017660e4eaSKevin Bowling  ************************************************************************/
48027660e4eaSKevin Bowling static void
48037660e4eaSKevin Bowling ixgbe_print_fw_version(if_ctx_t ctx)
48047660e4eaSKevin Bowling {
4805b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
4806b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
4807b1d5caf3SKevin Bowling 	device_t dev = sc->dev;
48087660e4eaSKevin Bowling 	struct sbuf *buf;
48097660e4eaSKevin Bowling 	int error = 0;
48107660e4eaSKevin Bowling 
48117660e4eaSKevin Bowling 	buf = sbuf_new_auto();
48127660e4eaSKevin Bowling 	if (!buf) {
48137660e4eaSKevin Bowling 		device_printf(dev, "Could not allocate sbuf for output.\n");
48147660e4eaSKevin Bowling 		return;
48157660e4eaSKevin Bowling 	}
48167660e4eaSKevin Bowling 
48177660e4eaSKevin Bowling 	ixgbe_sbuf_fw_version(hw, buf);
48187660e4eaSKevin Bowling 
48197660e4eaSKevin Bowling 	error = sbuf_finish(buf);
48207660e4eaSKevin Bowling 	if (error)
48217660e4eaSKevin Bowling 		device_printf(dev, "Error finishing sbuf: %d\n", error);
48227660e4eaSKevin Bowling 	else if (sbuf_len(buf))
48237660e4eaSKevin Bowling 		device_printf(dev, "%s\n", sbuf_data(buf));
48247660e4eaSKevin Bowling 
48257660e4eaSKevin Bowling 	sbuf_delete(buf);
48267660e4eaSKevin Bowling } /* ixgbe_print_fw_version */
48277660e4eaSKevin Bowling 
48287660e4eaSKevin Bowling /************************************************************************
48297660e4eaSKevin Bowling  * ixgbe_sysctl_print_fw_version
48307660e4eaSKevin Bowling  ************************************************************************/
48317660e4eaSKevin Bowling static int
48327660e4eaSKevin Bowling ixgbe_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)
48337660e4eaSKevin Bowling {
4834b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4835b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
4836b1d5caf3SKevin Bowling 	device_t dev = sc->dev;
48377660e4eaSKevin Bowling 	struct sbuf *buf;
48387660e4eaSKevin Bowling 	int error = 0;
48397660e4eaSKevin Bowling 
48407660e4eaSKevin Bowling 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
48417660e4eaSKevin Bowling 	if (!buf) {
48427660e4eaSKevin Bowling 		device_printf(dev, "Could not allocate sbuf for output.\n");
48437660e4eaSKevin Bowling 		return (ENOMEM);
48447660e4eaSKevin Bowling 	}
48457660e4eaSKevin Bowling 
48467660e4eaSKevin Bowling 	ixgbe_sbuf_fw_version(hw, buf);
48477660e4eaSKevin Bowling 
48487660e4eaSKevin Bowling 	error = sbuf_finish(buf);
48497660e4eaSKevin Bowling 	if (error)
48507660e4eaSKevin Bowling 		device_printf(dev, "Error finishing sbuf: %d\n", error);
48517660e4eaSKevin Bowling 
48527660e4eaSKevin Bowling 	sbuf_delete(buf);
48537660e4eaSKevin Bowling 
48547660e4eaSKevin Bowling 	return (0);
48557660e4eaSKevin Bowling } /* ixgbe_sysctl_print_fw_version */
4856