xref: /freebsd/sys/dev/ixgbe/if_ix.c (revision d381c807510de2ebb453a563540bd17e344a2aab)
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);
182*d381c807SPiotr 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 
236a1edda90SAdrian Chadd devclass_t ix_devclass;
237a1edda90SAdrian Chadd DRIVER_MODULE(ix, pci, ix_driver, ix_devclass, 0, 0);
238937b0f25SYuri Pankov IFLIB_PNP_INFO(pci, ix_driver, ixgbe_vendor_info_array);
239a1edda90SAdrian Chadd MODULE_DEPEND(ix, pci, 1, 1, 1);
240a1edda90SAdrian Chadd MODULE_DEPEND(ix, ether, 1, 1, 1);
241c19c7afeSEric Joyner MODULE_DEPEND(ix, iflib, 1, 1, 1);
242c19c7afeSEric Joyner 
243c19c7afeSEric Joyner static device_method_t ixgbe_if_methods[] = {
244c19c7afeSEric Joyner 	DEVMETHOD(ifdi_attach_pre, ixgbe_if_attach_pre),
245c19c7afeSEric Joyner 	DEVMETHOD(ifdi_attach_post, ixgbe_if_attach_post),
246c19c7afeSEric Joyner 	DEVMETHOD(ifdi_detach, ixgbe_if_detach),
247c19c7afeSEric Joyner 	DEVMETHOD(ifdi_shutdown, ixgbe_if_shutdown),
248c19c7afeSEric Joyner 	DEVMETHOD(ifdi_suspend, ixgbe_if_suspend),
249c19c7afeSEric Joyner 	DEVMETHOD(ifdi_resume, ixgbe_if_resume),
250c19c7afeSEric Joyner 	DEVMETHOD(ifdi_init, ixgbe_if_init),
251c19c7afeSEric Joyner 	DEVMETHOD(ifdi_stop, ixgbe_if_stop),
252c19c7afeSEric Joyner 	DEVMETHOD(ifdi_msix_intr_assign, ixgbe_if_msix_intr_assign),
253c19c7afeSEric Joyner 	DEVMETHOD(ifdi_intr_enable, ixgbe_if_enable_intr),
254c19c7afeSEric Joyner 	DEVMETHOD(ifdi_intr_disable, ixgbe_if_disable_intr),
255b2c1e8e6SEric Joyner 	DEVMETHOD(ifdi_link_intr_enable, ixgbe_link_intr_enable),
256c19c7afeSEric Joyner 	DEVMETHOD(ifdi_tx_queue_intr_enable, ixgbe_if_rx_queue_intr_enable),
257c19c7afeSEric Joyner 	DEVMETHOD(ifdi_rx_queue_intr_enable, ixgbe_if_rx_queue_intr_enable),
258c19c7afeSEric Joyner 	DEVMETHOD(ifdi_tx_queues_alloc, ixgbe_if_tx_queues_alloc),
259c19c7afeSEric Joyner 	DEVMETHOD(ifdi_rx_queues_alloc, ixgbe_if_rx_queues_alloc),
260c19c7afeSEric Joyner 	DEVMETHOD(ifdi_queues_free, ixgbe_if_queues_free),
261c19c7afeSEric Joyner 	DEVMETHOD(ifdi_update_admin_status, ixgbe_if_update_admin_status),
262c19c7afeSEric Joyner 	DEVMETHOD(ifdi_multi_set, ixgbe_if_multi_set),
263c19c7afeSEric Joyner 	DEVMETHOD(ifdi_mtu_set, ixgbe_if_mtu_set),
264c19c7afeSEric Joyner 	DEVMETHOD(ifdi_crcstrip_set, ixgbe_if_crcstrip_set),
265c19c7afeSEric Joyner 	DEVMETHOD(ifdi_media_status, ixgbe_if_media_status),
266c19c7afeSEric Joyner 	DEVMETHOD(ifdi_media_change, ixgbe_if_media_change),
267c19c7afeSEric Joyner 	DEVMETHOD(ifdi_promisc_set, ixgbe_if_promisc_set),
268c19c7afeSEric Joyner 	DEVMETHOD(ifdi_timer, ixgbe_if_timer),
269c19c7afeSEric Joyner 	DEVMETHOD(ifdi_vlan_register, ixgbe_if_vlan_register),
270c19c7afeSEric Joyner 	DEVMETHOD(ifdi_vlan_unregister, ixgbe_if_vlan_unregister),
271c19c7afeSEric Joyner 	DEVMETHOD(ifdi_get_counter, ixgbe_if_get_counter),
272bca38080SAndrew Gallatin 	DEVMETHOD(ifdi_i2c_req, ixgbe_if_i2c_req),
273cf150917SEric Joyner 	DEVMETHOD(ifdi_needs_restart, ixgbe_if_needs_restart),
274c19c7afeSEric Joyner #ifdef PCI_IOV
275c19c7afeSEric Joyner 	DEVMETHOD(ifdi_iov_init, ixgbe_if_iov_init),
276c19c7afeSEric Joyner 	DEVMETHOD(ifdi_iov_uninit, ixgbe_if_iov_uninit),
277c19c7afeSEric Joyner 	DEVMETHOD(ifdi_iov_vf_add, ixgbe_if_iov_vf_add),
278c19c7afeSEric Joyner #endif /* PCI_IOV */
279c19c7afeSEric Joyner 	DEVMETHOD_END
280c19c7afeSEric Joyner };
281758cc3dcSJack F Vogel 
282758cc3dcSJack F Vogel /*
2838eb6488eSEric Joyner  * TUNEABLE PARAMETERS:
284758cc3dcSJack F Vogel  */
285758cc3dcSJack F Vogel 
28620b91f0aSPawel Biernacki static SYSCTL_NODE(_hw, OID_AUTO, ix, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
28720b91f0aSPawel Biernacki     "IXGBE driver parameters");
288c19c7afeSEric Joyner static driver_t ixgbe_if_driver = {
289b1d5caf3SKevin Bowling   "ixgbe_if", ixgbe_if_methods, sizeof(struct ixgbe_softc)
290c19c7afeSEric Joyner };
291758cc3dcSJack F Vogel 
292758cc3dcSJack F Vogel static int ixgbe_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
293758cc3dcSJack F Vogel SYSCTL_INT(_hw_ix, OID_AUTO, max_interrupt_rate, CTLFLAG_RDTUN,
294758cc3dcSJack F Vogel     &ixgbe_max_interrupt_rate, 0, "Maximum interrupts per second");
295758cc3dcSJack F Vogel 
296f2c4db54SSteven Hartland /* Flow control setting, default to full */
297f2c4db54SSteven Hartland static int ixgbe_flow_control = ixgbe_fc_full;
298f2c4db54SSteven Hartland SYSCTL_INT(_hw_ix, OID_AUTO, flow_control, CTLFLAG_RDTUN,
299f2c4db54SSteven Hartland     &ixgbe_flow_control, 0, "Default flow control used for all adapters");
300f2c4db54SSteven Hartland 
301f2c4db54SSteven Hartland /* Advertise Speed, default to 0 (auto) */
302f2c4db54SSteven Hartland static int ixgbe_advertise_speed = 0;
303f2c4db54SSteven Hartland SYSCTL_INT(_hw_ix, OID_AUTO, advertise_speed, CTLFLAG_RDTUN,
304f2c4db54SSteven Hartland     &ixgbe_advertise_speed, 0, "Default advertised speed for all adapters");
305f2c4db54SSteven Hartland 
306758cc3dcSJack F Vogel /*
3078eb6488eSEric Joyner  * Smart speed setting, default to on
3088eb6488eSEric Joyner  * this only works as a compile option
3098eb6488eSEric Joyner  * right now as its during attach, set
3108eb6488eSEric Joyner  * this to 'ixgbe_smart_speed_off' to
3118eb6488eSEric Joyner  * disable.
312758cc3dcSJack F Vogel  */
313758cc3dcSJack F Vogel static int ixgbe_smart_speed = ixgbe_smart_speed_on;
314758cc3dcSJack F Vogel 
315758cc3dcSJack F Vogel /*
3168eb6488eSEric Joyner  * MSI-X should be the default for best performance,
317758cc3dcSJack F Vogel  * but this allows it to be forced off for testing.
318758cc3dcSJack F Vogel  */
319758cc3dcSJack F Vogel static int ixgbe_enable_msix = 1;
320758cc3dcSJack F Vogel SYSCTL_INT(_hw_ix, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &ixgbe_enable_msix, 0,
321758cc3dcSJack F Vogel     "Enable MSI-X interrupts");
322758cc3dcSJack F Vogel 
323758cc3dcSJack F Vogel /*
3248eb6488eSEric Joyner  * Defining this on will allow the use
3258eb6488eSEric Joyner  * of unsupported SFP+ modules, note that
3268eb6488eSEric Joyner  * doing so you are on your own :)
327758cc3dcSJack F Vogel  */
32879b36ec9SKevin Bowling static int allow_unsupported_sfp = false;
329fb6aa95dSSean Bruno SYSCTL_INT(_hw_ix, OID_AUTO, unsupported_sfp, CTLFLAG_RDTUN,
3308eb6488eSEric Joyner     &allow_unsupported_sfp, 0,
3318eb6488eSEric Joyner     "Allow unsupported SFP modules...use at your own risk");
3328eb6488eSEric Joyner 
3338eb6488eSEric Joyner /*
3348eb6488eSEric Joyner  * Not sure if Flow Director is fully baked,
3358eb6488eSEric Joyner  * so we'll default to turning it off.
3368eb6488eSEric Joyner  */
3378eb6488eSEric Joyner static int ixgbe_enable_fdir = 0;
3388eb6488eSEric Joyner SYSCTL_INT(_hw_ix, OID_AUTO, enable_fdir, CTLFLAG_RDTUN, &ixgbe_enable_fdir, 0,
3398eb6488eSEric Joyner     "Enable Flow Director");
3408eb6488eSEric Joyner 
3418eb6488eSEric Joyner /* Receive-Side Scaling */
3428eb6488eSEric Joyner static int ixgbe_enable_rss = 1;
3438eb6488eSEric Joyner SYSCTL_INT(_hw_ix, OID_AUTO, enable_rss, CTLFLAG_RDTUN, &ixgbe_enable_rss, 0,
3448eb6488eSEric Joyner     "Enable Receive-Side Scaling (RSS)");
345758cc3dcSJack F Vogel 
34664881da4SSai Rajesh Tallamraju /*
34764881da4SSai Rajesh Tallamraju  * AIM: Adaptive Interrupt Moderation
34864881da4SSai Rajesh Tallamraju  * which means that the interrupt rate
34964881da4SSai Rajesh Tallamraju  * is varied over time based on the
35064881da4SSai Rajesh Tallamraju  * traffic for that interrupt vector
35164881da4SSai Rajesh Tallamraju  */
35279b36ec9SKevin Bowling static int ixgbe_enable_aim = false;
35364881da4SSai Rajesh Tallamraju SYSCTL_INT(_hw_ix, OID_AUTO, enable_aim, CTLFLAG_RWTUN, &ixgbe_enable_aim, 0,
35464881da4SSai Rajesh Tallamraju     "Enable adaptive interrupt moderation");
35564881da4SSai Rajesh Tallamraju 
356c19c7afeSEric Joyner #if 0
357758cc3dcSJack F Vogel /* Keep running tab on them for sanity check */
358758cc3dcSJack F Vogel static int ixgbe_total_ports;
359c19c7afeSEric Joyner #endif
360758cc3dcSJack F Vogel 
3618eb6488eSEric Joyner MALLOC_DEFINE(M_IXGBE, "ix", "ix driver allocations");
362758cc3dcSJack F Vogel 
363c19c7afeSEric Joyner /*
364c19c7afeSEric Joyner  * For Flow Director: this is the number of TX packets we sample
365c19c7afeSEric Joyner  * for the filter pool, this means every 20th packet will be probed.
366c19c7afeSEric Joyner  *
367c19c7afeSEric Joyner  * This feature can be disabled by setting this to 0.
368c19c7afeSEric Joyner  */
369c19c7afeSEric Joyner static int atr_sample_rate = 20;
370c19c7afeSEric Joyner 
371c19c7afeSEric Joyner extern struct if_txrx ixgbe_txrx;
372c19c7afeSEric Joyner 
373c19c7afeSEric Joyner static struct if_shared_ctx ixgbe_sctx_init = {
374c19c7afeSEric Joyner 	.isc_magic = IFLIB_MAGIC,
375c19c7afeSEric Joyner 	.isc_q_align = PAGE_SIZE,/* max(DBA_ALIGN, PAGE_SIZE) */
3767f87c040SMarius Strobl 	.isc_tx_maxsize = IXGBE_TSO_SIZE + sizeof(struct ether_vlan_header),
377c19c7afeSEric Joyner 	.isc_tx_maxsegsize = PAGE_SIZE,
3787f87c040SMarius Strobl 	.isc_tso_maxsize = IXGBE_TSO_SIZE + sizeof(struct ether_vlan_header),
3797f87c040SMarius Strobl 	.isc_tso_maxsegsize = PAGE_SIZE,
380c19c7afeSEric Joyner 	.isc_rx_maxsize = PAGE_SIZE*4,
381c19c7afeSEric Joyner 	.isc_rx_nsegments = 1,
382c19c7afeSEric Joyner 	.isc_rx_maxsegsize = PAGE_SIZE*4,
383c19c7afeSEric Joyner 	.isc_nfl = 1,
384c19c7afeSEric Joyner 	.isc_ntxqs = 1,
385c19c7afeSEric Joyner 	.isc_nrxqs = 1,
386c19c7afeSEric Joyner 
387c19c7afeSEric Joyner 	.isc_admin_intrcnt = 1,
388c19c7afeSEric Joyner 	.isc_vendor_info = ixgbe_vendor_info_array,
389c19c7afeSEric Joyner 	.isc_driver_version = ixgbe_driver_version,
390c19c7afeSEric Joyner 	.isc_driver = &ixgbe_if_driver,
3917aad1f4eSEric Joyner 	.isc_flags = IFLIB_TSO_INIT_IP,
392c19c7afeSEric Joyner 
393c19c7afeSEric Joyner 	.isc_nrxd_min = {MIN_RXD},
394c19c7afeSEric Joyner 	.isc_ntxd_min = {MIN_TXD},
395c19c7afeSEric Joyner 	.isc_nrxd_max = {MAX_RXD},
396c19c7afeSEric Joyner 	.isc_ntxd_max = {MAX_TXD},
397c19c7afeSEric Joyner 	.isc_nrxd_default = {DEFAULT_RXD},
398c19c7afeSEric Joyner 	.isc_ntxd_default = {DEFAULT_TXD},
399c19c7afeSEric Joyner };
400c19c7afeSEric Joyner 
401c19c7afeSEric Joyner /************************************************************************
402c19c7afeSEric Joyner  * ixgbe_if_tx_queues_alloc
403c19c7afeSEric Joyner  ************************************************************************/
404c19c7afeSEric Joyner static int
405c19c7afeSEric Joyner ixgbe_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
406c19c7afeSEric Joyner     int ntxqs, int ntxqsets)
407c19c7afeSEric Joyner {
408b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
409b1d5caf3SKevin Bowling 	if_softc_ctx_t     scctx = sc->shared;
410c19c7afeSEric Joyner 	struct ix_tx_queue *que;
411c19c7afeSEric Joyner 	int                i, j, error;
412c19c7afeSEric Joyner 
413b1d5caf3SKevin Bowling 	MPASS(sc->num_tx_queues > 0);
414b1d5caf3SKevin Bowling 	MPASS(sc->num_tx_queues == ntxqsets);
415c19c7afeSEric Joyner 	MPASS(ntxqs == 1);
416c19c7afeSEric Joyner 
417c19c7afeSEric Joyner 	/* Allocate queue structure memory */
418b1d5caf3SKevin Bowling 	sc->tx_queues =
419c19c7afeSEric Joyner 	    (struct ix_tx_queue *)malloc(sizeof(struct ix_tx_queue) * ntxqsets,
420c19c7afeSEric Joyner 	                                 M_IXGBE, M_NOWAIT | M_ZERO);
421b1d5caf3SKevin Bowling 	if (!sc->tx_queues) {
422c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
423c19c7afeSEric Joyner 		    "Unable to allocate TX ring memory\n");
424c19c7afeSEric Joyner 		return (ENOMEM);
425c19c7afeSEric Joyner 	}
426c19c7afeSEric Joyner 
427b1d5caf3SKevin Bowling 	for (i = 0, que = sc->tx_queues; i < ntxqsets; i++, que++) {
428c19c7afeSEric Joyner 		struct tx_ring *txr = &que->txr;
429c19c7afeSEric Joyner 
430c19c7afeSEric Joyner 		/* In case SR-IOV is enabled, align the index properly */
431b1d5caf3SKevin Bowling 		txr->me = ixgbe_vf_que_index(sc->iov_mode, sc->pool,
432c19c7afeSEric Joyner 		    i);
433c19c7afeSEric Joyner 
434b1d5caf3SKevin Bowling 		txr->sc = que->sc = sc;
435c19c7afeSEric Joyner 
436c19c7afeSEric Joyner 		/* Allocate report status array */
437c19c7afeSEric Joyner 		txr->tx_rsq = (qidx_t *)malloc(sizeof(qidx_t) * scctx->isc_ntxd[0], M_IXGBE, M_NOWAIT | M_ZERO);
438c19c7afeSEric Joyner 		if (txr->tx_rsq == NULL) {
439c19c7afeSEric Joyner 			error = ENOMEM;
440c19c7afeSEric Joyner 			goto fail;
441c19c7afeSEric Joyner 		}
442c19c7afeSEric Joyner 		for (j = 0; j < scctx->isc_ntxd[0]; j++)
443c19c7afeSEric Joyner 			txr->tx_rsq[j] = QIDX_INVALID;
444c19c7afeSEric Joyner 		/* get the virtual and physical address of the hardware queues */
445c19c7afeSEric Joyner 		txr->tail = IXGBE_TDT(txr->me);
446c19c7afeSEric Joyner 		txr->tx_base = (union ixgbe_adv_tx_desc *)vaddrs[i];
447c19c7afeSEric Joyner 		txr->tx_paddr = paddrs[i];
448c19c7afeSEric Joyner 
449c19c7afeSEric Joyner 		txr->bytes = 0;
450c19c7afeSEric Joyner 		txr->total_packets = 0;
451c19c7afeSEric Joyner 
452c19c7afeSEric Joyner 		/* Set the rate at which we sample packets */
453b1d5caf3SKevin Bowling 		if (sc->feat_en & IXGBE_FEATURE_FDIR)
454c19c7afeSEric Joyner 			txr->atr_sample = atr_sample_rate;
455c19c7afeSEric Joyner 
456c19c7afeSEric Joyner 	}
457c19c7afeSEric Joyner 
458c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "allocated for %d queues\n",
459b1d5caf3SKevin Bowling 	    sc->num_tx_queues);
460c19c7afeSEric Joyner 
461c19c7afeSEric Joyner 	return (0);
462c19c7afeSEric Joyner 
463c19c7afeSEric Joyner fail:
464c19c7afeSEric Joyner 	ixgbe_if_queues_free(ctx);
465c19c7afeSEric Joyner 
466c19c7afeSEric Joyner 	return (error);
467c19c7afeSEric Joyner } /* ixgbe_if_tx_queues_alloc */
468c19c7afeSEric Joyner 
469c19c7afeSEric Joyner /************************************************************************
470c19c7afeSEric Joyner  * ixgbe_if_rx_queues_alloc
471c19c7afeSEric Joyner  ************************************************************************/
472c19c7afeSEric Joyner static int
473c19c7afeSEric Joyner ixgbe_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
474c19c7afeSEric Joyner     int nrxqs, int nrxqsets)
475c19c7afeSEric Joyner {
476b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
477c19c7afeSEric Joyner 	struct ix_rx_queue *que;
478c19c7afeSEric Joyner 	int                i;
479c19c7afeSEric Joyner 
480b1d5caf3SKevin Bowling 	MPASS(sc->num_rx_queues > 0);
481b1d5caf3SKevin Bowling 	MPASS(sc->num_rx_queues == nrxqsets);
482c19c7afeSEric Joyner 	MPASS(nrxqs == 1);
483c19c7afeSEric Joyner 
484c19c7afeSEric Joyner 	/* Allocate queue structure memory */
485b1d5caf3SKevin Bowling 	sc->rx_queues =
486c19c7afeSEric Joyner 	    (struct ix_rx_queue *)malloc(sizeof(struct ix_rx_queue)*nrxqsets,
487c19c7afeSEric Joyner 	                                 M_IXGBE, M_NOWAIT | M_ZERO);
488b1d5caf3SKevin Bowling 	if (!sc->rx_queues) {
489c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
490c19c7afeSEric Joyner 		    "Unable to allocate TX ring memory\n");
491c19c7afeSEric Joyner 		return (ENOMEM);
492c19c7afeSEric Joyner 	}
493c19c7afeSEric Joyner 
494b1d5caf3SKevin Bowling 	for (i = 0, que = sc->rx_queues; i < nrxqsets; i++, que++) {
495c19c7afeSEric Joyner 		struct rx_ring *rxr = &que->rxr;
496c19c7afeSEric Joyner 
497c19c7afeSEric Joyner 		/* In case SR-IOV is enabled, align the index properly */
498b1d5caf3SKevin Bowling 		rxr->me = ixgbe_vf_que_index(sc->iov_mode, sc->pool,
499c19c7afeSEric Joyner 		    i);
500c19c7afeSEric Joyner 
501b1d5caf3SKevin Bowling 		rxr->sc = que->sc = sc;
502c19c7afeSEric Joyner 
503c19c7afeSEric Joyner 		/* get the virtual and physical address of the hw queues */
504c19c7afeSEric Joyner 		rxr->tail = IXGBE_RDT(rxr->me);
505c19c7afeSEric Joyner 		rxr->rx_base = (union ixgbe_adv_rx_desc *)vaddrs[i];
506c19c7afeSEric Joyner 		rxr->rx_paddr = paddrs[i];
507c19c7afeSEric Joyner 		rxr->bytes = 0;
508c19c7afeSEric Joyner 		rxr->que = que;
509c19c7afeSEric Joyner 	}
510c19c7afeSEric Joyner 
511c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "allocated for %d rx queues\n",
512b1d5caf3SKevin Bowling 	    sc->num_rx_queues);
513c19c7afeSEric Joyner 
514c19c7afeSEric Joyner 	return (0);
515c19c7afeSEric Joyner } /* ixgbe_if_rx_queues_alloc */
516c19c7afeSEric Joyner 
517c19c7afeSEric Joyner /************************************************************************
518c19c7afeSEric Joyner  * ixgbe_if_queues_free
519c19c7afeSEric Joyner  ************************************************************************/
520c19c7afeSEric Joyner static void
521c19c7afeSEric Joyner ixgbe_if_queues_free(if_ctx_t ctx)
522c19c7afeSEric Joyner {
523b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
524b1d5caf3SKevin Bowling 	struct ix_tx_queue *tx_que = sc->tx_queues;
525b1d5caf3SKevin Bowling 	struct ix_rx_queue *rx_que = sc->rx_queues;
526c19c7afeSEric Joyner 	int                i;
527c19c7afeSEric Joyner 
528c19c7afeSEric Joyner 	if (tx_que != NULL) {
529b1d5caf3SKevin Bowling 		for (i = 0; i < sc->num_tx_queues; i++, tx_que++) {
530c19c7afeSEric Joyner 			struct tx_ring *txr = &tx_que->txr;
531c19c7afeSEric Joyner 			if (txr->tx_rsq == NULL)
532c19c7afeSEric Joyner 				break;
533c19c7afeSEric Joyner 
534c19c7afeSEric Joyner 			free(txr->tx_rsq, M_IXGBE);
535c19c7afeSEric Joyner 			txr->tx_rsq = NULL;
536c19c7afeSEric Joyner 		}
537c19c7afeSEric Joyner 
538b1d5caf3SKevin Bowling 		free(sc->tx_queues, M_IXGBE);
539b1d5caf3SKevin Bowling 		sc->tx_queues = NULL;
540c19c7afeSEric Joyner 	}
541c19c7afeSEric Joyner 	if (rx_que != NULL) {
542b1d5caf3SKevin Bowling 		free(sc->rx_queues, M_IXGBE);
543b1d5caf3SKevin Bowling 		sc->rx_queues = NULL;
544c19c7afeSEric Joyner 	}
545c19c7afeSEric Joyner } /* ixgbe_if_queues_free */
546c19c7afeSEric Joyner 
5478eb6488eSEric Joyner /************************************************************************
5488eb6488eSEric Joyner  * ixgbe_initialize_rss_mapping
5498eb6488eSEric Joyner  ************************************************************************/
5508eb6488eSEric Joyner static void
551b1d5caf3SKevin Bowling ixgbe_initialize_rss_mapping(struct ixgbe_softc *sc)
552758cc3dcSJack F Vogel {
553b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
5548eb6488eSEric Joyner 	u32             reta = 0, mrqc, rss_key[10];
5558eb6488eSEric Joyner 	int             queue_id, table_size, index_mult;
5568eb6488eSEric Joyner 	int             i, j;
5578eb6488eSEric Joyner 	u32             rss_hash_config;
5586f37f232SEric Joyner 
559b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_RSS) {
5608eb6488eSEric Joyner 		/* Fetch the configured RSS key */
5618eb6488eSEric Joyner 		rss_getkey((uint8_t *)&rss_key);
562758cc3dcSJack F Vogel 	} else {
5638eb6488eSEric Joyner 		/* set up random bits */
5648eb6488eSEric Joyner 		arc4rand(&rss_key, sizeof(rss_key), 0);
565758cc3dcSJack F Vogel 	}
566758cc3dcSJack F Vogel 
5678eb6488eSEric Joyner 	/* Set multiplier for RETA setup and table size based on MAC */
5688eb6488eSEric Joyner 	index_mult = 0x1;
5698eb6488eSEric Joyner 	table_size = 128;
570b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
5718eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
5728eb6488eSEric Joyner 		index_mult = 0x11;
573758cc3dcSJack F Vogel 		break;
5748eb6488eSEric Joyner 	case ixgbe_mac_X550:
5758eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
5768eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
5778eb6488eSEric Joyner 		table_size = 512;
5788eb6488eSEric Joyner 		break;
579758cc3dcSJack F Vogel 	default:
580758cc3dcSJack F Vogel 		break;
581758cc3dcSJack F Vogel 	}
582758cc3dcSJack F Vogel 
5838eb6488eSEric Joyner 	/* Set up the redirection table */
5848eb6488eSEric Joyner 	for (i = 0, j = 0; i < table_size; i++, j++) {
585b1d5caf3SKevin Bowling 		if (j == sc->num_rx_queues)
5868eb6488eSEric Joyner 			j = 0;
587758cc3dcSJack F Vogel 
588b1d5caf3SKevin Bowling 		if (sc->feat_en & IXGBE_FEATURE_RSS) {
589a9ca1c79SSean Bruno 			/*
5908eb6488eSEric Joyner 			 * Fetch the RSS bucket id for the given indirection
5918eb6488eSEric Joyner 			 * entry. Cap it at the number of configured buckets
592c19c7afeSEric Joyner 			 * (which is num_rx_queues.)
593a9ca1c79SSean Bruno 			 */
5948eb6488eSEric Joyner 			queue_id = rss_get_indirection_to_bucket(i);
595b1d5caf3SKevin Bowling 			queue_id = queue_id % sc->num_rx_queues;
596758cc3dcSJack F Vogel 		} else
5978eb6488eSEric Joyner 			queue_id = (j * index_mult);
5988eb6488eSEric Joyner 
5998eb6488eSEric Joyner 		/*
6008eb6488eSEric Joyner 		 * The low 8 bits are for hash value (n+0);
6018eb6488eSEric Joyner 		 * The next 8 bits are for hash value (n+1), etc.
6028eb6488eSEric Joyner 		 */
6038eb6488eSEric Joyner 		reta = reta >> 8;
6048eb6488eSEric Joyner 		reta = reta | (((uint32_t)queue_id) << 24);
6058eb6488eSEric Joyner 		if ((i & 3) == 3) {
6068eb6488eSEric Joyner 			if (i < 128)
6078eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
6088eb6488eSEric Joyner 			else
6098eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_ERETA((i >> 2) - 32),
6108eb6488eSEric Joyner 				    reta);
6118eb6488eSEric Joyner 			reta = 0;
6128eb6488eSEric Joyner 		}
613758cc3dcSJack F Vogel 	}
614758cc3dcSJack F Vogel 
6158eb6488eSEric Joyner 	/* Now fill our hash function seeds */
6168eb6488eSEric Joyner 	for (i = 0; i < 10; i++)
6178eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
618758cc3dcSJack F Vogel 
6198eb6488eSEric Joyner 	/* Perform hash on these packet types */
620b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_RSS)
6218eb6488eSEric Joyner 		rss_hash_config = rss_gethashconfig();
622758cc3dcSJack F Vogel 	else {
623758cc3dcSJack F Vogel 		/*
6248eb6488eSEric Joyner 		 * Disable UDP - IP fragments aren't currently being handled
6258eb6488eSEric Joyner 		 * and so we end up with a mix of 2-tuple and 4-tuple
6268eb6488eSEric Joyner 		 * traffic.
627758cc3dcSJack F Vogel 		 */
6288eb6488eSEric Joyner 		rss_hash_config = RSS_HASHTYPE_RSS_IPV4
6298eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_TCP_IPV4
6308eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_IPV6
6318eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_TCP_IPV6
6328eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_IPV6_EX
6338eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_TCP_IPV6_EX;
634758cc3dcSJack F Vogel 	}
635758cc3dcSJack F Vogel 
6368eb6488eSEric Joyner 	mrqc = IXGBE_MRQC_RSSEN;
6378eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
6388eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
6398eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
6408eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
6418eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
6428eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
6438eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
6448eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
6458eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
6468eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
6478eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
6488eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
6498eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
6508eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
6518eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
6528eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
6538eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
6548eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
655b1d5caf3SKevin Bowling 	mrqc |= ixgbe_get_mrqc(sc->iov_mode);
6568eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
6578eb6488eSEric Joyner } /* ixgbe_initialize_rss_mapping */
658758cc3dcSJack F Vogel 
6598eb6488eSEric Joyner /************************************************************************
6608eb6488eSEric Joyner  * ixgbe_initialize_receive_units - Setup receive registers and features.
6618eb6488eSEric Joyner  ************************************************************************/
6628eb6488eSEric Joyner #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
663758cc3dcSJack F Vogel 
664758cc3dcSJack F Vogel static void
665c19c7afeSEric Joyner ixgbe_initialize_receive_units(if_ctx_t ctx)
666758cc3dcSJack F Vogel {
667b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
668b1d5caf3SKevin Bowling 	if_softc_ctx_t     scctx = sc->shared;
669b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
670c19c7afeSEric Joyner 	struct ifnet       *ifp = iflib_get_ifp(ctx);
671c19c7afeSEric Joyner 	struct ix_rx_queue *que;
6728eb6488eSEric Joyner 	int                i, j;
6738eb6488eSEric Joyner 	u32                bufsz, fctrl, srrctl, rxcsum;
6748eb6488eSEric Joyner 	u32                hlreg;
67548056c88SJack F Vogel 
67648056c88SJack F Vogel 	/*
6778eb6488eSEric Joyner 	 * Make sure receives are disabled while
6788eb6488eSEric Joyner 	 * setting up the descriptor ring
67948056c88SJack F Vogel 	 */
6808eb6488eSEric Joyner 	ixgbe_disable_rx(hw);
6818eb6488eSEric Joyner 
6828eb6488eSEric Joyner 	/* Enable broadcasts */
6838eb6488eSEric Joyner 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
6848eb6488eSEric Joyner 	fctrl |= IXGBE_FCTRL_BAM;
685b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
6868eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_DPF;
6878eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_PMCF;
68848056c88SJack F Vogel 	}
6898eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
6908eb6488eSEric Joyner 
6918eb6488eSEric Joyner 	/* Set for Jumbo Frames? */
6928eb6488eSEric Joyner 	hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
6938eb6488eSEric Joyner 	if (ifp->if_mtu > ETHERMTU)
6948eb6488eSEric Joyner 		hlreg |= IXGBE_HLREG0_JUMBOEN;
6958eb6488eSEric Joyner 	else
6968eb6488eSEric Joyner 		hlreg &= ~IXGBE_HLREG0_JUMBOEN;
6978eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
6988eb6488eSEric Joyner 
699b1d5caf3SKevin Bowling 	bufsz = (sc->rx_mbuf_sz + BSIZEPKT_ROUNDUP) >>
7008eb6488eSEric Joyner 	    IXGBE_SRRCTL_BSIZEPKT_SHIFT;
7018eb6488eSEric Joyner 
702c19c7afeSEric Joyner 	/* Setup the Base and Length of the Rx Descriptor Ring */
703b1d5caf3SKevin Bowling 	for (i = 0, que = sc->rx_queues; i < sc->num_rx_queues; i++, que++) {
704c19c7afeSEric Joyner 		struct rx_ring *rxr = &que->rxr;
705c19c7afeSEric Joyner 		u64            rdba = rxr->rx_paddr;
706c19c7afeSEric Joyner 
7078eb6488eSEric Joyner 		j = rxr->me;
7088eb6488eSEric Joyner 
7098eb6488eSEric Joyner 		/* Setup the Base and Length of the Rx Descriptor Ring */
7108eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j),
7118eb6488eSEric Joyner 		    (rdba & 0x00000000ffffffffULL));
7128eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
7138eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j),
714c19c7afeSEric Joyner 		     scctx->isc_nrxd[0] * sizeof(union ixgbe_adv_rx_desc));
7158eb6488eSEric Joyner 
7168eb6488eSEric Joyner 		/* Set up the SRRCTL register */
7178eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(j));
7188eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
7198eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
7208eb6488eSEric Joyner 		srrctl |= bufsz;
7218eb6488eSEric Joyner 		srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
72248056c88SJack F Vogel 
72348056c88SJack F Vogel 		/*
7248eb6488eSEric Joyner 		 * Set DROP_EN iff we have no flow control and >1 queue.
7258eb6488eSEric Joyner 		 * Note that srrctl was cleared shortly before during reset,
7268eb6488eSEric Joyner 		 * so we do not need to clear the bit, but do it just in case
7278eb6488eSEric Joyner 		 * this code is moved elsewhere.
72848056c88SJack F Vogel 		 */
729b1d5caf3SKevin Bowling 		if (sc->num_rx_queues > 1 &&
730b1d5caf3SKevin Bowling 		    sc->hw.fc.requested_mode == ixgbe_fc_none) {
7318eb6488eSEric Joyner 			srrctl |= IXGBE_SRRCTL_DROP_EN;
732758cc3dcSJack F Vogel 		} else {
7338eb6488eSEric Joyner 			srrctl &= ~IXGBE_SRRCTL_DROP_EN;
734758cc3dcSJack F Vogel 		}
735758cc3dcSJack F Vogel 
7368eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(j), srrctl);
737758cc3dcSJack F Vogel 
7388eb6488eSEric Joyner 		/* Setup the HW Rx Head and Tail Descriptor Pointers */
7398eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
7408eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
741758cc3dcSJack F Vogel 
7428eb6488eSEric Joyner 		/* Set the driver rx tail address */
7438eb6488eSEric Joyner 		rxr->tail =  IXGBE_RDT(rxr->me);
744a9ca1c79SSean Bruno 	}
745758cc3dcSJack F Vogel 
746b1d5caf3SKevin Bowling 	if (sc->hw.mac.type != ixgbe_mac_82598EB) {
7478eb6488eSEric Joyner 		u32 psrtype = IXGBE_PSRTYPE_TCPHDR
7488eb6488eSEric Joyner 		            | IXGBE_PSRTYPE_UDPHDR
7498eb6488eSEric Joyner 		            | IXGBE_PSRTYPE_IPV4HDR
7508eb6488eSEric Joyner 		            | IXGBE_PSRTYPE_IPV6HDR;
7518eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
7526f37f232SEric Joyner 	}
7536f37f232SEric Joyner 
7548eb6488eSEric Joyner 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
7558eb6488eSEric Joyner 
756b1d5caf3SKevin Bowling 	ixgbe_initialize_rss_mapping(sc);
7578eb6488eSEric Joyner 
758b1d5caf3SKevin Bowling 	if (sc->num_rx_queues > 1) {
7598eb6488eSEric Joyner 		/* RSS and RX IPP Checksum are mutually exclusive */
7608eb6488eSEric Joyner 		rxcsum |= IXGBE_RXCSUM_PCSD;
7616f37f232SEric Joyner 	}
7626f37f232SEric Joyner 
7638eb6488eSEric Joyner 	if (ifp->if_capenable & IFCAP_RXCSUM)
7648eb6488eSEric Joyner 		rxcsum |= IXGBE_RXCSUM_PCSD;
765758cc3dcSJack F Vogel 
7668eb6488eSEric Joyner 	/* This is useful for calculating UDP/IP fragment checksums */
7678eb6488eSEric Joyner 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
7688eb6488eSEric Joyner 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
769758cc3dcSJack F Vogel 
7708eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
771758cc3dcSJack F Vogel 
7728eb6488eSEric Joyner } /* ixgbe_initialize_receive_units */
773758cc3dcSJack F Vogel 
7748eb6488eSEric Joyner /************************************************************************
7758eb6488eSEric Joyner  * ixgbe_initialize_transmit_units - Enable transmit units.
7768eb6488eSEric Joyner  ************************************************************************/
777758cc3dcSJack F Vogel static void
778c19c7afeSEric Joyner ixgbe_initialize_transmit_units(if_ctx_t ctx)
779758cc3dcSJack F Vogel {
780b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
781b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
782b1d5caf3SKevin Bowling 	if_softc_ctx_t     scctx = sc->shared;
783c19c7afeSEric Joyner 	struct ix_tx_queue *que;
784c19c7afeSEric Joyner 	int i;
785758cc3dcSJack F Vogel 
786758cc3dcSJack F Vogel 	/* Setup the Base and Length of the Tx Descriptor Ring */
787b1d5caf3SKevin Bowling 	for (i = 0, que = sc->tx_queues; i < sc->num_tx_queues;
788c19c7afeSEric Joyner 	    i++, que++) {
789c19c7afeSEric Joyner 		struct tx_ring	   *txr = &que->txr;
790c19c7afeSEric Joyner 		u64 tdba = txr->tx_paddr;
791758cc3dcSJack F Vogel 		u32 txctrl = 0;
79248056c88SJack F Vogel 		int j = txr->me;
793758cc3dcSJack F Vogel 
79448056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
795758cc3dcSJack F Vogel 		    (tdba & 0x00000000ffffffffULL));
79648056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
79748056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j),
798c19c7afeSEric Joyner 		    scctx->isc_ntxd[0] * sizeof(union ixgbe_adv_tx_desc));
799758cc3dcSJack F Vogel 
800758cc3dcSJack F Vogel 		/* Setup the HW Tx Head and Tail descriptor pointers */
80148056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
80248056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
803758cc3dcSJack F Vogel 
804758cc3dcSJack F Vogel 		/* Cache the tail address */
8052dc2d580SEric Joyner 		txr->tail = IXGBE_TDT(txr->me);
8062dc2d580SEric Joyner 
807088a0b27SEric Joyner 		txr->tx_rs_cidx = txr->tx_rs_pidx;
808088a0b27SEric Joyner 		txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
809c19c7afeSEric Joyner 		for (int k = 0; k < scctx->isc_ntxd[0]; k++)
810c19c7afeSEric Joyner 			txr->tx_rsq[k] = QIDX_INVALID;
811758cc3dcSJack F Vogel 
812758cc3dcSJack F Vogel 		/* Disable Head Writeback */
813a9ca1c79SSean Bruno 		/*
814a9ca1c79SSean Bruno 		 * Note: for X550 series devices, these registers are actually
815a9ca1c79SSean Bruno 		 * prefixed with TPH_ isntead of DCA_, but the addresses and
816a9ca1c79SSean Bruno 		 * fields remain the same.
817a9ca1c79SSean Bruno 		 */
818758cc3dcSJack F Vogel 		switch (hw->mac.type) {
819758cc3dcSJack F Vogel 		case ixgbe_mac_82598EB:
82048056c88SJack F Vogel 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
821758cc3dcSJack F Vogel 			break;
822758cc3dcSJack F Vogel 		default:
82348056c88SJack F Vogel 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(j));
824758cc3dcSJack F Vogel 			break;
825758cc3dcSJack F Vogel 		}
826758cc3dcSJack F Vogel 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
827758cc3dcSJack F Vogel 		switch (hw->mac.type) {
828758cc3dcSJack F Vogel 		case ixgbe_mac_82598EB:
82948056c88SJack F Vogel 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
830758cc3dcSJack F Vogel 			break;
831758cc3dcSJack F Vogel 		default:
83248056c88SJack F Vogel 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(j), txctrl);
833758cc3dcSJack F Vogel 			break;
834758cc3dcSJack F Vogel 		}
835758cc3dcSJack F Vogel 
836758cc3dcSJack F Vogel 	}
837758cc3dcSJack F Vogel 
838758cc3dcSJack F Vogel 	if (hw->mac.type != ixgbe_mac_82598EB) {
839758cc3dcSJack F Vogel 		u32 dmatxctl, rttdcs;
8408eb6488eSEric Joyner 
841758cc3dcSJack F Vogel 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
842758cc3dcSJack F Vogel 		dmatxctl |= IXGBE_DMATXCTL_TE;
843758cc3dcSJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
844758cc3dcSJack F Vogel 		/* Disable arbiter to set MTQC */
845758cc3dcSJack F Vogel 		rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
846758cc3dcSJack F Vogel 		rttdcs |= IXGBE_RTTDCS_ARBDIS;
847758cc3dcSJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
8488eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_MTQC,
849b1d5caf3SKevin Bowling 		    ixgbe_get_mtqc(sc->iov_mode));
850758cc3dcSJack F Vogel 		rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
851758cc3dcSJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
852758cc3dcSJack F Vogel 	}
853758cc3dcSJack F Vogel 
8548eb6488eSEric Joyner } /* ixgbe_initialize_transmit_units */
855758cc3dcSJack F Vogel 
8568eb6488eSEric Joyner /************************************************************************
857c19c7afeSEric Joyner  * ixgbe_register
858c19c7afeSEric Joyner  ************************************************************************/
859c19c7afeSEric Joyner static void *
860c19c7afeSEric Joyner ixgbe_register(device_t dev)
861c19c7afeSEric Joyner {
862ffe3def9SMark Johnston 	return (&ixgbe_sctx_init);
863c19c7afeSEric Joyner } /* ixgbe_register */
864c19c7afeSEric Joyner 
865c19c7afeSEric Joyner /************************************************************************
866c19c7afeSEric Joyner  * ixgbe_if_attach_pre - Device initialization routine, part 1
867758cc3dcSJack F Vogel  *
8688eb6488eSEric Joyner  *   Called when the driver is being loaded.
869c19c7afeSEric Joyner  *   Identifies the type of hardware, initializes the hardware,
870c19c7afeSEric Joyner  *   and initializes iflib structures.
871758cc3dcSJack F Vogel  *
8728eb6488eSEric Joyner  *   return 0 on success, positive on failure
8738eb6488eSEric Joyner  ************************************************************************/
8748eb6488eSEric Joyner static int
875c19c7afeSEric Joyner ixgbe_if_attach_pre(if_ctx_t ctx)
876758cc3dcSJack F Vogel {
877b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc;
878c19c7afeSEric Joyner 	device_t        dev;
879c19c7afeSEric Joyner 	if_softc_ctx_t  scctx;
8808eb6488eSEric Joyner 	struct ixgbe_hw *hw;
8818eb6488eSEric Joyner 	int             error = 0;
8828eb6488eSEric Joyner 	u32             ctrl_ext;
8838eb6488eSEric Joyner 
8848eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_attach: begin");
8858eb6488eSEric Joyner 
8868eb6488eSEric Joyner 	/* Allocate, clear, and link in our adapter structure */
887c19c7afeSEric Joyner 	dev = iflib_get_dev(ctx);
888b1d5caf3SKevin Bowling 	sc = iflib_get_softc(ctx);
889b1d5caf3SKevin Bowling 	sc->hw.back = sc;
890b1d5caf3SKevin Bowling 	sc->ctx = ctx;
891b1d5caf3SKevin Bowling 	sc->dev = dev;
892b1d5caf3SKevin Bowling 	scctx = sc->shared = iflib_get_softc_ctx(ctx);
893b1d5caf3SKevin Bowling 	sc->media = iflib_get_media(ctx);
894b1d5caf3SKevin Bowling 	hw = &sc->hw;
8958eb6488eSEric Joyner 
8968eb6488eSEric Joyner 	/* Determine hardware revision */
8978eb6488eSEric Joyner 	hw->vendor_id = pci_get_vendor(dev);
8988eb6488eSEric Joyner 	hw->device_id = pci_get_device(dev);
8998eb6488eSEric Joyner 	hw->revision_id = pci_get_revid(dev);
9008eb6488eSEric Joyner 	hw->subsystem_vendor_id = pci_get_subvendor(dev);
9018eb6488eSEric Joyner 	hw->subsystem_device_id = pci_get_subdevice(dev);
902758cc3dcSJack F Vogel 
9038eb6488eSEric Joyner 	/* Do base PCI setup - map BAR0 */
904c19c7afeSEric Joyner 	if (ixgbe_allocate_pci_resources(ctx)) {
9058eb6488eSEric Joyner 		device_printf(dev, "Allocation of PCI resources failed\n");
906c19c7afeSEric Joyner 		return (ENXIO);
9076f37f232SEric Joyner 	}
908758cc3dcSJack F Vogel 
9098eb6488eSEric Joyner 	/* let hardware know driver is loaded */
9108eb6488eSEric Joyner 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
9118eb6488eSEric Joyner 	ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
9128eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
913758cc3dcSJack F Vogel 
914758cc3dcSJack F Vogel 	/*
9158eb6488eSEric Joyner 	 * Initialize the shared code
916758cc3dcSJack F Vogel 	 */
917c19c7afeSEric Joyner 	if (ixgbe_init_shared_code(hw) != 0) {
9188eb6488eSEric Joyner 		device_printf(dev, "Unable to initialize the shared code\n");
9198eb6488eSEric Joyner 		error = ENXIO;
920c19c7afeSEric Joyner 		goto err_pci;
92130126537SJack F Vogel 	}
922758cc3dcSJack F Vogel 
9238eb6488eSEric Joyner 	if (hw->mbx.ops.init_params)
9248eb6488eSEric Joyner 		hw->mbx.ops.init_params(hw);
925758cc3dcSJack F Vogel 
9268eb6488eSEric Joyner 	hw->allow_unsupported_sfp = allow_unsupported_sfp;
927758cc3dcSJack F Vogel 
928c19c7afeSEric Joyner 	if (hw->mac.type != ixgbe_mac_82598EB)
9298eb6488eSEric Joyner 		hw->phy.smart_speed = ixgbe_smart_speed;
9308eb6488eSEric Joyner 
931b1d5caf3SKevin Bowling 	ixgbe_init_device_features(sc);
9328eb6488eSEric Joyner 
9338eb6488eSEric Joyner 	/* Enable WoL (if supported) */
934b1d5caf3SKevin Bowling 	ixgbe_check_wol_support(sc);
9358eb6488eSEric Joyner 
9368eb6488eSEric Joyner 	/* Verify adapter fan is still functional (if applicable) */
937b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL) {
9388eb6488eSEric Joyner 		u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
939b1d5caf3SKevin Bowling 		ixgbe_check_fan_failure(sc, esdp, false);
9408eb6488eSEric Joyner 	}
9418eb6488eSEric Joyner 
9428eb6488eSEric Joyner 	/* Ensure SW/FW semaphore is free */
9438eb6488eSEric Joyner 	ixgbe_init_swfw_semaphore(hw);
9448eb6488eSEric Joyner 
9458eb6488eSEric Joyner 	/* Set an initial default flow control value */
9468eb6488eSEric Joyner 	hw->fc.requested_mode = ixgbe_flow_control;
9478eb6488eSEric Joyner 
94879b36ec9SKevin Bowling 	hw->phy.reset_if_overtemp = true;
9498eb6488eSEric Joyner 	error = ixgbe_reset_hw(hw);
95079b36ec9SKevin Bowling 	hw->phy.reset_if_overtemp = false;
9518eb6488eSEric Joyner 	if (error == IXGBE_ERR_SFP_NOT_PRESENT) {
952758cc3dcSJack F Vogel 		/*
9538eb6488eSEric Joyner 		 * No optics in this port, set up
9548eb6488eSEric Joyner 		 * so the timer routine will probe
9558eb6488eSEric Joyner 		 * for later insertion.
956758cc3dcSJack F Vogel 		 */
957b1d5caf3SKevin Bowling 		sc->sfp_probe = true;
958c19c7afeSEric Joyner 		error = 0;
9598eb6488eSEric Joyner 	} else if (error == IXGBE_ERR_SFP_NOT_SUPPORTED) {
9608eb6488eSEric Joyner 		device_printf(dev, "Unsupported SFP+ module detected!\n");
9618eb6488eSEric Joyner 		error = EIO;
962c19c7afeSEric Joyner 		goto err_pci;
9638eb6488eSEric Joyner 	} else if (error) {
9648eb6488eSEric Joyner 		device_printf(dev, "Hardware initialization failed\n");
9658eb6488eSEric Joyner 		error = EIO;
966c19c7afeSEric Joyner 		goto err_pci;
96797f9586eSSean Bruno 	}
96897f9586eSSean Bruno 
9698eb6488eSEric Joyner 	/* Make sure we have a good EEPROM before we read from it */
970b1d5caf3SKevin Bowling 	if (ixgbe_validate_eeprom_checksum(&sc->hw, NULL) < 0) {
9718eb6488eSEric Joyner 		device_printf(dev, "The EEPROM Checksum Is Not Valid\n");
9728eb6488eSEric Joyner 		error = EIO;
973c19c7afeSEric Joyner 		goto err_pci;
97497f9586eSSean Bruno 	}
97597f9586eSSean Bruno 
9768eb6488eSEric Joyner 	error = ixgbe_start_hw(hw);
9778eb6488eSEric Joyner 	switch (error) {
9788eb6488eSEric Joyner 	case IXGBE_ERR_EEPROM_VERSION:
9798eb6488eSEric 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");
98097f9586eSSean Bruno 		break;
9818eb6488eSEric Joyner 	case IXGBE_ERR_SFP_NOT_SUPPORTED:
9828eb6488eSEric Joyner 		device_printf(dev, "Unsupported SFP+ Module\n");
9838eb6488eSEric Joyner 		error = EIO;
984c19c7afeSEric Joyner 		goto err_pci;
9858eb6488eSEric Joyner 	case IXGBE_ERR_SFP_NOT_PRESENT:
9868eb6488eSEric Joyner 		device_printf(dev, "No SFP+ Module found\n");
9878eb6488eSEric Joyner 		/* falls thru */
98897f9586eSSean Bruno 	default:
98997f9586eSSean Bruno 		break;
99097f9586eSSean Bruno 	}
99197f9586eSSean Bruno 
992c19c7afeSEric Joyner 	/* Most of the iflib initialization... */
993c19c7afeSEric Joyner 
994c19c7afeSEric Joyner 	iflib_set_mac(ctx, hw->mac.addr);
995b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
996c19c7afeSEric Joyner 	case ixgbe_mac_X550:
997c19c7afeSEric Joyner 	case ixgbe_mac_X550EM_x:
998c19c7afeSEric Joyner 	case ixgbe_mac_X550EM_a:
999c19c7afeSEric Joyner 		scctx->isc_rss_table_size = 512;
1000c19c7afeSEric Joyner 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 64;
1001c19c7afeSEric Joyner 		break;
1002c19c7afeSEric Joyner 	default:
1003c19c7afeSEric Joyner 		scctx->isc_rss_table_size = 128;
1004c19c7afeSEric Joyner 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 16;
1005c19c7afeSEric Joyner 	}
1006c19c7afeSEric Joyner 
1007c19c7afeSEric Joyner 	/* Allow legacy interrupts */
1008c19c7afeSEric Joyner 	ixgbe_txrx.ift_legacy_intr = ixgbe_intr;
1009c19c7afeSEric Joyner 
1010c19c7afeSEric Joyner 	scctx->isc_txqsizes[0] =
1011c19c7afeSEric Joyner 	    roundup2(scctx->isc_ntxd[0] * sizeof(union ixgbe_adv_tx_desc) +
1012c19c7afeSEric Joyner 	    sizeof(u32), DBA_ALIGN),
1013c19c7afeSEric Joyner 	scctx->isc_rxqsizes[0] =
1014c19c7afeSEric Joyner 	    roundup2(scctx->isc_nrxd[0] * sizeof(union ixgbe_adv_rx_desc),
1015c19c7afeSEric Joyner 	    DBA_ALIGN);
1016c19c7afeSEric Joyner 
1017c19c7afeSEric Joyner 	/* XXX */
1018c19c7afeSEric Joyner 	scctx->isc_tx_csum_flags = CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_TSO |
1019c19c7afeSEric Joyner 	    CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_TSO;
1020b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
1021c19c7afeSEric Joyner 		scctx->isc_tx_nsegments = IXGBE_82598_SCATTER;
1022c19c7afeSEric Joyner 	} else {
1023c19c7afeSEric Joyner 		scctx->isc_tx_csum_flags |= CSUM_SCTP |CSUM_IP6_SCTP;
1024c19c7afeSEric Joyner 		scctx->isc_tx_nsegments = IXGBE_82599_SCATTER;
1025c19c7afeSEric Joyner 	}
1026749597dcSEric Joyner 
1027749597dcSEric Joyner 	scctx->isc_msix_bar = pci_msix_table_bar(dev);
1028749597dcSEric Joyner 
1029c19c7afeSEric Joyner 	scctx->isc_tx_tso_segments_max = scctx->isc_tx_nsegments;
1030c19c7afeSEric Joyner 	scctx->isc_tx_tso_size_max = IXGBE_TSO_SIZE;
1031c19c7afeSEric Joyner 	scctx->isc_tx_tso_segsize_max = PAGE_SIZE;
1032c19c7afeSEric Joyner 
1033c19c7afeSEric Joyner 	scctx->isc_txrx = &ixgbe_txrx;
1034c19c7afeSEric Joyner 
10357f87c040SMarius Strobl 	scctx->isc_capabilities = scctx->isc_capenable = IXGBE_CAPS;
1036c19c7afeSEric Joyner 
1037c19c7afeSEric Joyner 	return (0);
1038c19c7afeSEric Joyner 
1039c19c7afeSEric Joyner err_pci:
1040b1d5caf3SKevin Bowling 	ctrl_ext = IXGBE_READ_REG(&sc->hw, IXGBE_CTRL_EXT);
1041c19c7afeSEric Joyner 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
1042b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_CTRL_EXT, ctrl_ext);
1043c19c7afeSEric Joyner 	ixgbe_free_pci_resources(ctx);
1044c19c7afeSEric Joyner 
1045c19c7afeSEric Joyner 	return (error);
1046c19c7afeSEric Joyner } /* ixgbe_if_attach_pre */
1047c19c7afeSEric Joyner 
1048c19c7afeSEric Joyner  /*********************************************************************
1049c19c7afeSEric Joyner  * ixgbe_if_attach_post - Device initialization routine, part 2
1050c19c7afeSEric Joyner  *
1051c19c7afeSEric Joyner  *   Called during driver load, but after interrupts and
1052c19c7afeSEric Joyner  *   resources have been allocated and configured.
1053c19c7afeSEric Joyner  *   Sets up some data structures not relevant to iflib.
1054c19c7afeSEric Joyner  *
1055c19c7afeSEric Joyner  *   return 0 on success, positive on failure
1056c19c7afeSEric Joyner  *********************************************************************/
1057c19c7afeSEric Joyner static int
1058c19c7afeSEric Joyner ixgbe_if_attach_post(if_ctx_t ctx)
1059c19c7afeSEric Joyner {
1060c19c7afeSEric Joyner 	device_t dev;
1061b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc;
1062c19c7afeSEric Joyner 	struct ixgbe_hw *hw;
1063c19c7afeSEric Joyner 	int             error = 0;
1064c19c7afeSEric Joyner 
1065c19c7afeSEric Joyner 	dev = iflib_get_dev(ctx);
1066b1d5caf3SKevin Bowling 	sc = iflib_get_softc(ctx);
1067b1d5caf3SKevin Bowling 	hw = &sc->hw;
1068c19c7afeSEric Joyner 
1069c19c7afeSEric Joyner 
1070b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_LEGACY &&
1071b1d5caf3SKevin Bowling 		(sc->feat_cap & IXGBE_FEATURE_LEGACY_IRQ) == 0) {
1072c19c7afeSEric Joyner 		device_printf(dev, "Device does not support legacy interrupts");
1073c19c7afeSEric Joyner 		error = ENXIO;
1074c19c7afeSEric Joyner 		goto err;
1075c19c7afeSEric Joyner 	}
1076c19c7afeSEric Joyner 
1077c19c7afeSEric Joyner 	/* Allocate multicast array memory. */
1078b1d5caf3SKevin Bowling 	sc->mta = malloc(sizeof(*sc->mta) *
1079c19c7afeSEric Joyner 	                      MAX_NUM_MULTICAST_ADDRESSES, M_IXGBE, M_NOWAIT);
1080b1d5caf3SKevin Bowling 	if (sc->mta == NULL) {
1081c19c7afeSEric Joyner 		device_printf(dev, "Can not allocate multicast setup array\n");
1082c19c7afeSEric Joyner 		error = ENOMEM;
1083c19c7afeSEric Joyner 		goto err;
1084c19c7afeSEric Joyner 	}
1085c19c7afeSEric Joyner 
1086c19c7afeSEric Joyner 	/* hw.ix defaults init */
1087b1d5caf3SKevin Bowling 	ixgbe_set_advertise(sc, ixgbe_advertise_speed);
1088c19c7afeSEric Joyner 
10898eb6488eSEric Joyner 	/* Enable the optics for 82599 SFP+ fiber */
10908eb6488eSEric Joyner 	ixgbe_enable_tx_laser(hw);
1091758cc3dcSJack F Vogel 
10928eb6488eSEric Joyner 	/* Enable power to the phy. */
109379b36ec9SKevin Bowling 	ixgbe_set_phy_power(hw, true);
10948eb6488eSEric Joyner 
1095b1d5caf3SKevin Bowling 	ixgbe_initialize_iov(sc);
1096c19c7afeSEric Joyner 
1097c19c7afeSEric Joyner 	error = ixgbe_setup_interface(ctx);
1098c19c7afeSEric Joyner 	if (error) {
1099c19c7afeSEric Joyner 		device_printf(dev, "Interface setup failed: %d\n", error);
1100c19c7afeSEric Joyner 		goto err;
1101c19c7afeSEric Joyner 	}
1102c19c7afeSEric Joyner 
1103c19c7afeSEric Joyner 	ixgbe_if_update_admin_status(ctx);
1104c19c7afeSEric Joyner 
11058eb6488eSEric Joyner 	/* Initialize statistics */
1106b1d5caf3SKevin Bowling 	ixgbe_update_stats_counters(sc);
1107b1d5caf3SKevin Bowling 	ixgbe_add_hw_stats(sc);
11088eb6488eSEric Joyner 
11098eb6488eSEric Joyner 	/* Check PCIE slot type/speed/width */
1110b1d5caf3SKevin Bowling 	ixgbe_get_slot_info(sc);
1111758cc3dcSJack F Vogel 
1112758cc3dcSJack F Vogel 	/*
11138eb6488eSEric Joyner 	 * Do time init and sysctl init here, but
1114b1d5caf3SKevin Bowling 	 * only on the first port of a bypass sc.
1115758cc3dcSJack F Vogel 	 */
1116b1d5caf3SKevin Bowling 	ixgbe_bypass_init(sc);
1117758cc3dcSJack F Vogel 
11187660e4eaSKevin Bowling 	/* Display NVM and Option ROM versions */
11197660e4eaSKevin Bowling 	ixgbe_print_fw_version(ctx);
11207660e4eaSKevin Bowling 
11218eb6488eSEric Joyner 	/* Set an initial dmac value */
1122b1d5caf3SKevin Bowling 	sc->dmac = 0;
11238eb6488eSEric Joyner 	/* Set initial advertised speeds (if applicable) */
1124*d381c807SPiotr Pietruszewski 	sc->advertise = ixgbe_get_default_advertise(sc);
1125758cc3dcSJack F Vogel 
1126b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_SRIOV)
11278eb6488eSEric Joyner 		ixgbe_define_iov_schemas(dev, &error);
1128758cc3dcSJack F Vogel 
11298eb6488eSEric Joyner 	/* Add sysctls */
1130c19c7afeSEric Joyner 	ixgbe_add_device_sysctls(ctx);
1131758cc3dcSJack F Vogel 
11328eb6488eSEric Joyner 	return (0);
1133c19c7afeSEric Joyner err:
11348eb6488eSEric Joyner 	return (error);
1135c19c7afeSEric Joyner } /* ixgbe_if_attach_post */
11368eb6488eSEric Joyner 
11378eb6488eSEric Joyner /************************************************************************
11388eb6488eSEric Joyner  * ixgbe_check_wol_support
11396f37f232SEric Joyner  *
11406f37f232SEric Joyner  *   Checks whether the adapter's ports are capable of
11416f37f232SEric Joyner  *   Wake On LAN by reading the adapter's NVM.
11426f37f232SEric Joyner  *
11436f37f232SEric Joyner  *   Sets each port's hw->wol_enabled value depending
11446f37f232SEric Joyner  *   on the value read here.
11458eb6488eSEric Joyner  ************************************************************************/
11466f37f232SEric Joyner static void
1147b1d5caf3SKevin Bowling ixgbe_check_wol_support(struct ixgbe_softc *sc)
11486f37f232SEric Joyner {
1149b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
11506f37f232SEric Joyner 	u16             dev_caps = 0;
11516f37f232SEric Joyner 
11526f37f232SEric Joyner 	/* Find out WoL support for port */
1153b1d5caf3SKevin Bowling 	sc->wol_support = hw->wol_enabled = 0;
11546f37f232SEric Joyner 	ixgbe_get_device_caps(hw, &dev_caps);
11556f37f232SEric Joyner 	if ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
11566f37f232SEric Joyner 	    ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0) &&
11576f37f232SEric Joyner 	     hw->bus.func == 0))
1158b1d5caf3SKevin Bowling 		sc->wol_support = hw->wol_enabled = 1;
11596f37f232SEric Joyner 
11606f37f232SEric Joyner 	/* Save initial wake up filter configuration */
1161b1d5caf3SKevin Bowling 	sc->wufc = IXGBE_READ_REG(hw, IXGBE_WUFC);
11626f37f232SEric Joyner 
11636f37f232SEric Joyner 	return;
11648eb6488eSEric Joyner } /* ixgbe_check_wol_support */
11656f37f232SEric Joyner 
11668eb6488eSEric Joyner /************************************************************************
11678eb6488eSEric Joyner  * ixgbe_setup_interface
11688eb6488eSEric Joyner  *
11698eb6488eSEric Joyner  *   Setup networking device structure and register an interface.
11708eb6488eSEric Joyner  ************************************************************************/
11716f37f232SEric Joyner static int
1172c19c7afeSEric Joyner ixgbe_setup_interface(if_ctx_t ctx)
11736f37f232SEric Joyner {
1174c19c7afeSEric Joyner 	struct ifnet   *ifp = iflib_get_ifp(ctx);
1175b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
11766f37f232SEric Joyner 
11778eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_setup_interface: begin");
11786f37f232SEric Joyner 
1179c19c7afeSEric Joyner 	if_setbaudrate(ifp, IF_Gbps(10));
11806f37f232SEric Joyner 
1181b1d5caf3SKevin Bowling 	sc->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1182758cc3dcSJack F Vogel 
1183b1d5caf3SKevin Bowling 	sc->phy_layer = ixgbe_get_supported_physical_layer(&sc->hw);
11848eb6488eSEric Joyner 
1185c19c7afeSEric Joyner 	ixgbe_add_media_types(ctx);
1186c19c7afeSEric Joyner 
1187c19c7afeSEric Joyner 	/* Autoselect media by default */
1188b1d5caf3SKevin Bowling 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
11898eb6488eSEric Joyner 
11908eb6488eSEric Joyner 	return (0);
11918eb6488eSEric Joyner } /* ixgbe_setup_interface */
1192758cc3dcSJack F Vogel 
11938eb6488eSEric Joyner /************************************************************************
1194c19c7afeSEric Joyner  * ixgbe_if_get_counter
11958eb6488eSEric Joyner  ************************************************************************/
1196758cc3dcSJack F Vogel static uint64_t
1197c19c7afeSEric Joyner ixgbe_if_get_counter(if_ctx_t ctx, ift_counter cnt)
1198758cc3dcSJack F Vogel {
1199b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
1200c19c7afeSEric Joyner 	if_t           ifp = iflib_get_ifp(ctx);
1201758cc3dcSJack F Vogel 
1202758cc3dcSJack F Vogel 	switch (cnt) {
1203758cc3dcSJack F Vogel 	case IFCOUNTER_IPACKETS:
1204b1d5caf3SKevin Bowling 		return (sc->ipackets);
1205758cc3dcSJack F Vogel 	case IFCOUNTER_OPACKETS:
1206b1d5caf3SKevin Bowling 		return (sc->opackets);
1207758cc3dcSJack F Vogel 	case IFCOUNTER_IBYTES:
1208b1d5caf3SKevin Bowling 		return (sc->ibytes);
1209758cc3dcSJack F Vogel 	case IFCOUNTER_OBYTES:
1210b1d5caf3SKevin Bowling 		return (sc->obytes);
1211758cc3dcSJack F Vogel 	case IFCOUNTER_IMCASTS:
1212b1d5caf3SKevin Bowling 		return (sc->imcasts);
1213758cc3dcSJack F Vogel 	case IFCOUNTER_OMCASTS:
1214b1d5caf3SKevin Bowling 		return (sc->omcasts);
1215758cc3dcSJack F Vogel 	case IFCOUNTER_COLLISIONS:
1216758cc3dcSJack F Vogel 		return (0);
1217758cc3dcSJack F Vogel 	case IFCOUNTER_IQDROPS:
1218b1d5caf3SKevin Bowling 		return (sc->iqdrops);
1219625d12c6SJohn Baldwin 	case IFCOUNTER_OQDROPS:
1220c19c7afeSEric Joyner 		return (0);
1221758cc3dcSJack F Vogel 	case IFCOUNTER_IERRORS:
1222b1d5caf3SKevin Bowling 		return (sc->ierrors);
1223758cc3dcSJack F Vogel 	default:
1224758cc3dcSJack F Vogel 		return (if_get_counter_default(ifp, cnt));
1225758cc3dcSJack F Vogel 	}
1226c19c7afeSEric Joyner } /* ixgbe_if_get_counter */
1227758cc3dcSJack F Vogel 
12288eb6488eSEric Joyner /************************************************************************
1229bca38080SAndrew Gallatin  * ixgbe_if_i2c_req
1230bca38080SAndrew Gallatin  ************************************************************************/
1231bca38080SAndrew Gallatin static int
1232bca38080SAndrew Gallatin ixgbe_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req)
1233bca38080SAndrew Gallatin {
1234b1d5caf3SKevin Bowling 	struct ixgbe_softc		*sc = iflib_get_softc(ctx);
1235b1d5caf3SKevin Bowling 	struct ixgbe_hw 	*hw = &sc->hw;
1236bca38080SAndrew Gallatin 	int 			i;
1237bca38080SAndrew Gallatin 
1238bca38080SAndrew Gallatin 
1239bca38080SAndrew Gallatin 	if (hw->phy.ops.read_i2c_byte == NULL)
1240bca38080SAndrew Gallatin 		return (ENXIO);
1241bca38080SAndrew Gallatin 	for (i = 0; i < req->len; i++)
1242bca38080SAndrew Gallatin 		hw->phy.ops.read_i2c_byte(hw, req->offset + i,
1243bca38080SAndrew Gallatin 		    req->dev_addr, &req->data[i]);
1244bca38080SAndrew Gallatin 	return (0);
1245bca38080SAndrew Gallatin } /* ixgbe_if_i2c_req */
1246bca38080SAndrew Gallatin 
1247cf150917SEric Joyner /* ixgbe_if_needs_restart - Tell iflib when the driver needs to be reinitialized
1248cf150917SEric Joyner  * @ctx: iflib context
1249cf150917SEric Joyner  * @event: event code to check
1250cf150917SEric Joyner  *
1251cf150917SEric Joyner  * Defaults to returning true for unknown events.
1252cf150917SEric Joyner  *
1253cf150917SEric Joyner  * @returns true if iflib needs to reinit the interface
1254cf150917SEric Joyner  */
1255cf150917SEric Joyner static bool
1256cf150917SEric Joyner ixgbe_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
1257cf150917SEric Joyner {
1258cf150917SEric Joyner 	switch (event) {
1259cf150917SEric Joyner 	case IFLIB_RESTART_VLAN_CONFIG:
1260cf150917SEric Joyner 		return (false);
1261cf150917SEric Joyner 	default:
1262cf150917SEric Joyner 		return (true);
1263cf150917SEric Joyner 	}
1264cf150917SEric Joyner }
1265cf150917SEric Joyner 
1266bca38080SAndrew Gallatin /************************************************************************
12678eb6488eSEric Joyner  * ixgbe_add_media_types
12688eb6488eSEric Joyner  ************************************************************************/
12698eb6488eSEric Joyner static void
1270c19c7afeSEric Joyner ixgbe_add_media_types(if_ctx_t ctx)
12718eb6488eSEric Joyner {
1272b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
1273b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
1274c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
12758eb6488eSEric Joyner 	u64             layer;
12768eb6488eSEric Joyner 
1277b1d5caf3SKevin Bowling 	layer = sc->phy_layer = ixgbe_get_supported_physical_layer(hw);
12788eb6488eSEric Joyner 
12798eb6488eSEric Joyner 	/* Media types with matching FreeBSD media defines */
12808eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T)
1281b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_T, 0, NULL);
12828eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T)
1283b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
12848eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_100BASE_TX)
1285b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_100_TX, 0, NULL);
12868eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
1287b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10_T, 0, NULL);
12888eb6488eSEric Joyner 
1289*d381c807SPiotr Pietruszewski 	if (hw->mac.type == ixgbe_mac_X550) {
1290*d381c807SPiotr Pietruszewski 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_T, 0, NULL);
1291*d381c807SPiotr Pietruszewski 		ifmedia_add(sc->media, IFM_ETHER | IFM_5000_T, 0, NULL);
1292*d381c807SPiotr Pietruszewski 	}
1293*d381c807SPiotr Pietruszewski 
12948eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
12958eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
1296b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_TWINAX, 0,
12978eb6488eSEric Joyner 		    NULL);
12988eb6488eSEric Joyner 
12998eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR) {
1300b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_LR, 0, NULL);
13018eb6488eSEric Joyner 		if (hw->phy.multispeed_fiber)
1302b1d5caf3SKevin Bowling 			ifmedia_add(sc->media, IFM_ETHER | IFM_1000_LX, 0,
13038eb6488eSEric Joyner 			    NULL);
13048eb6488eSEric Joyner 	}
13058eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
1306b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
13078eb6488eSEric Joyner 		if (hw->phy.multispeed_fiber)
1308b1d5caf3SKevin Bowling 			ifmedia_add(sc->media, IFM_ETHER | IFM_1000_SX, 0,
13098eb6488eSEric Joyner 			    NULL);
13108eb6488eSEric Joyner 	} else if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
1311b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_SX, 0, NULL);
13128eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
1313b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
13148eb6488eSEric Joyner 
13158eb6488eSEric Joyner #ifdef IFM_ETH_XTYPE
13168eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
1317b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_KR, 0, NULL);
13188eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4)
1319b1d5caf3SKevin Bowling 		ifmedia_add( sc->media, IFM_ETHER | IFM_10G_KX4, 0, NULL);
13208eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
1321b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_KX, 0, NULL);
13228eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX)
1323b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_KX, 0, NULL);
13248eb6488eSEric Joyner #else
13258eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
13268eb6488eSEric Joyner 		device_printf(dev, "Media supported: 10GbaseKR\n");
13278eb6488eSEric Joyner 		device_printf(dev, "10GbaseKR mapped to 10GbaseSR\n");
1328b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
13298eb6488eSEric Joyner 	}
13308eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
13318eb6488eSEric Joyner 		device_printf(dev, "Media supported: 10GbaseKX4\n");
13328eb6488eSEric Joyner 		device_printf(dev, "10GbaseKX4 mapped to 10GbaseCX4\n");
1333b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
13348eb6488eSEric Joyner 	}
13358eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX) {
13368eb6488eSEric Joyner 		device_printf(dev, "Media supported: 1000baseKX\n");
13378eb6488eSEric Joyner 		device_printf(dev, "1000baseKX mapped to 1000baseCX\n");
1338b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_CX, 0, NULL);
13398eb6488eSEric Joyner 	}
13408eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX) {
13418eb6488eSEric Joyner 		device_printf(dev, "Media supported: 2500baseKX\n");
13428eb6488eSEric Joyner 		device_printf(dev, "2500baseKX mapped to 2500baseSX\n");
1343b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_SX, 0, NULL);
13448eb6488eSEric Joyner 	}
13458eb6488eSEric Joyner #endif
13468eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_BX)
13478eb6488eSEric Joyner 		device_printf(dev, "Media supported: 1000baseBX\n");
13488eb6488eSEric Joyner 
13498eb6488eSEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_82598AT) {
1350b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T | IFM_FDX,
13518eb6488eSEric Joyner 		    0, NULL);
1352b1d5caf3SKevin Bowling 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
13538eb6488eSEric Joyner 	}
13548eb6488eSEric Joyner 
1355b1d5caf3SKevin Bowling 	ifmedia_add(sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
13568eb6488eSEric Joyner } /* ixgbe_add_media_types */
13578eb6488eSEric Joyner 
13588eb6488eSEric Joyner /************************************************************************
13598eb6488eSEric Joyner  * ixgbe_is_sfp
13608eb6488eSEric Joyner  ************************************************************************/
13618eb6488eSEric Joyner static inline bool
13628eb6488eSEric Joyner ixgbe_is_sfp(struct ixgbe_hw *hw)
13638eb6488eSEric Joyner {
13648eb6488eSEric Joyner 	switch (hw->mac.type) {
13658eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
13668eb6488eSEric Joyner 		if (hw->phy.type == ixgbe_phy_nl)
136779b36ec9SKevin Bowling 			return (true);
136879b36ec9SKevin Bowling 		return (false);
13698eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
13708eb6488eSEric Joyner 		switch (hw->mac.ops.get_media_type(hw)) {
13718eb6488eSEric Joyner 		case ixgbe_media_type_fiber:
13728eb6488eSEric Joyner 		case ixgbe_media_type_fiber_qsfp:
137379b36ec9SKevin Bowling 			return (true);
13748eb6488eSEric Joyner 		default:
137579b36ec9SKevin Bowling 			return (false);
13768eb6488eSEric Joyner 		}
13778eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
13788eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
13798eb6488eSEric Joyner 		if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_fiber)
138079b36ec9SKevin Bowling 			return (true);
138179b36ec9SKevin Bowling 		return (false);
13828eb6488eSEric Joyner 	default:
138379b36ec9SKevin Bowling 		return (false);
13848eb6488eSEric Joyner 	}
13858eb6488eSEric Joyner } /* ixgbe_is_sfp */
13868eb6488eSEric Joyner 
13878eb6488eSEric Joyner /************************************************************************
13888eb6488eSEric Joyner  * ixgbe_config_link
13898eb6488eSEric Joyner  ************************************************************************/
13908eb6488eSEric Joyner static void
1391b2c1e8e6SEric Joyner ixgbe_config_link(if_ctx_t ctx)
13928eb6488eSEric Joyner {
1393b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
1394b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
13958eb6488eSEric Joyner 	u32             autoneg, err = 0;
13968eb6488eSEric Joyner 	bool            sfp, negotiate;
13978eb6488eSEric Joyner 
13988eb6488eSEric Joyner 	sfp = ixgbe_is_sfp(hw);
13998eb6488eSEric Joyner 
14008eb6488eSEric Joyner 	if (sfp) {
1401b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
1402b2c1e8e6SEric Joyner 		iflib_admin_intr_deferred(ctx);
14038eb6488eSEric Joyner 	} else {
14048eb6488eSEric Joyner 		if (hw->mac.ops.check_link)
1405b1d5caf3SKevin Bowling 			err = ixgbe_check_link(hw, &sc->link_speed,
1406b1d5caf3SKevin Bowling 			    &sc->link_up, false);
14078eb6488eSEric Joyner 		if (err)
1408c19c7afeSEric Joyner 			return;
14098eb6488eSEric Joyner 		autoneg = hw->phy.autoneg_advertised;
14108eb6488eSEric Joyner 		if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
14118eb6488eSEric Joyner 			err = hw->mac.ops.get_link_capabilities(hw, &autoneg,
14128eb6488eSEric Joyner 			    &negotiate);
14138eb6488eSEric Joyner 		if (err)
1414c19c7afeSEric Joyner 			return;
1415*d381c807SPiotr Pietruszewski 
1416*d381c807SPiotr Pietruszewski 		if (hw->mac.type == ixgbe_mac_X550 &&
1417*d381c807SPiotr Pietruszewski 		    hw->phy.autoneg_advertised == 0) {
1418*d381c807SPiotr Pietruszewski 			/*
1419*d381c807SPiotr Pietruszewski 			 * 2.5G and 5G autonegotiation speeds on X550
1420*d381c807SPiotr Pietruszewski 			 * are disabled by default due to reported
1421*d381c807SPiotr Pietruszewski 			 * interoperability issues with some switches.
1422*d381c807SPiotr Pietruszewski 			 *
1423*d381c807SPiotr Pietruszewski 			 * The second condition checks if any operations
1424*d381c807SPiotr Pietruszewski 			 * involving setting autonegotiation speeds have
1425*d381c807SPiotr Pietruszewski 			 * been performed prior to this ixgbe_config_link()
1426*d381c807SPiotr Pietruszewski 			 * call.
1427*d381c807SPiotr Pietruszewski 			 *
1428*d381c807SPiotr Pietruszewski 			 * If hw->phy.autoneg_advertised does not
1429*d381c807SPiotr Pietruszewski 			 * equal 0, this means that the user might have
1430*d381c807SPiotr Pietruszewski 			 * set autonegotiation speeds via the sysctl
1431*d381c807SPiotr Pietruszewski 			 * before bringing the interface up. In this
1432*d381c807SPiotr Pietruszewski 			 * case, we should not disable 2.5G and 5G
1433*d381c807SPiotr Pietruszewski 			 * since that speeds might be selected by the
1434*d381c807SPiotr Pietruszewski 			 * user.
1435*d381c807SPiotr Pietruszewski 			 *
1436*d381c807SPiotr Pietruszewski 			 * Otherwise (i.e. if hw->phy.autoneg_advertised
1437*d381c807SPiotr Pietruszewski 			 * is set to 0), it is the first time we set
1438*d381c807SPiotr Pietruszewski 			 * autonegotiation preferences and the default
1439*d381c807SPiotr Pietruszewski 			 * set of speeds should exclude 2.5G and 5G.
1440*d381c807SPiotr Pietruszewski 			 */
1441*d381c807SPiotr Pietruszewski 			autoneg &= ~(IXGBE_LINK_SPEED_2_5GB_FULL |
1442*d381c807SPiotr Pietruszewski 			    IXGBE_LINK_SPEED_5GB_FULL);
1443*d381c807SPiotr Pietruszewski 		}
1444*d381c807SPiotr Pietruszewski 
14458eb6488eSEric Joyner 		if (hw->mac.ops.setup_link)
14468eb6488eSEric Joyner 			err = hw->mac.ops.setup_link(hw, autoneg,
1447b1d5caf3SKevin Bowling 			    sc->link_up);
14488eb6488eSEric Joyner 	}
14498eb6488eSEric Joyner } /* ixgbe_config_link */
14508eb6488eSEric Joyner 
14518eb6488eSEric Joyner /************************************************************************
14528eb6488eSEric Joyner  * ixgbe_update_stats_counters - Update board statistics counters.
14538eb6488eSEric Joyner  ************************************************************************/
14548eb6488eSEric Joyner static void
1455b1d5caf3SKevin Bowling ixgbe_update_stats_counters(struct ixgbe_softc *sc)
14568eb6488eSEric Joyner {
1457b1d5caf3SKevin Bowling 	struct ixgbe_hw       *hw = &sc->hw;
1458b1d5caf3SKevin Bowling 	struct ixgbe_hw_stats *stats = &sc->stats.pf;
14598eb6488eSEric Joyner 	u32                   missed_rx = 0, bprc, lxon, lxoff, total;
1460e37d3dc1SEric Joyner 	u32                   lxoffrxc;
14618eb6488eSEric Joyner 	u64                   total_missed_rx = 0;
14628eb6488eSEric Joyner 
14638eb6488eSEric Joyner 	stats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
14648eb6488eSEric Joyner 	stats->illerrc += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
14658eb6488eSEric Joyner 	stats->errbc += IXGBE_READ_REG(hw, IXGBE_ERRBC);
14668eb6488eSEric Joyner 	stats->mspdc += IXGBE_READ_REG(hw, IXGBE_MSPDC);
14678eb6488eSEric Joyner 	stats->mpc[0] += IXGBE_READ_REG(hw, IXGBE_MPC(0));
14688eb6488eSEric Joyner 
14698eb6488eSEric Joyner 	for (int i = 0; i < 16; i++) {
14708eb6488eSEric Joyner 		stats->qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
14718eb6488eSEric Joyner 		stats->qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
14728eb6488eSEric Joyner 		stats->qprdc[i] += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
14738eb6488eSEric Joyner 	}
14748eb6488eSEric Joyner 	stats->mlfc += IXGBE_READ_REG(hw, IXGBE_MLFC);
14758eb6488eSEric Joyner 	stats->mrfc += IXGBE_READ_REG(hw, IXGBE_MRFC);
14768eb6488eSEric Joyner 	stats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
14778eb6488eSEric Joyner 
14788eb6488eSEric Joyner 	/* Hardware workaround, gprc counts missed packets */
14798eb6488eSEric Joyner 	stats->gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
14808eb6488eSEric Joyner 	stats->gprc -= missed_rx;
14818eb6488eSEric Joyner 
14828eb6488eSEric Joyner 	if (hw->mac.type != ixgbe_mac_82598EB) {
14838eb6488eSEric Joyner 		stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL) +
14848eb6488eSEric Joyner 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
14858eb6488eSEric Joyner 		stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
14868eb6488eSEric Joyner 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32);
14878eb6488eSEric Joyner 		stats->tor += IXGBE_READ_REG(hw, IXGBE_TORL) +
14888eb6488eSEric Joyner 		    ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
14898eb6488eSEric Joyner 		stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
1490e37d3dc1SEric Joyner 		lxoffrxc = IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
1491e37d3dc1SEric Joyner 		stats->lxoffrxc += lxoffrxc;
14928eb6488eSEric Joyner 	} else {
14938eb6488eSEric Joyner 		stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
1494e37d3dc1SEric Joyner 		lxoffrxc = IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
1495e37d3dc1SEric Joyner 		stats->lxoffrxc += lxoffrxc;
14968eb6488eSEric Joyner 		/* 82598 only has a counter in the high register */
14978eb6488eSEric Joyner 		stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
14988eb6488eSEric Joyner 		stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
14998eb6488eSEric Joyner 		stats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
15008eb6488eSEric Joyner 	}
15018eb6488eSEric Joyner 
15028eb6488eSEric Joyner 	/*
1503e37d3dc1SEric Joyner 	 * For watchdog management we need to know if we have been paused
1504e37d3dc1SEric Joyner 	 * during the last interval, so capture that here.
1505e37d3dc1SEric Joyner 	*/
1506e37d3dc1SEric Joyner 	if (lxoffrxc)
1507b1d5caf3SKevin Bowling 		sc->shared->isc_pause_frames = 1;
1508e37d3dc1SEric Joyner 
1509e37d3dc1SEric Joyner 	/*
15108eb6488eSEric Joyner 	 * Workaround: mprc hardware is incorrectly counting
15118eb6488eSEric Joyner 	 * broadcasts, so for now we subtract those.
1512758cc3dcSJack F Vogel 	 */
15138eb6488eSEric Joyner 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
15148eb6488eSEric Joyner 	stats->bprc += bprc;
15158eb6488eSEric Joyner 	stats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
15168eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
15178eb6488eSEric Joyner 		stats->mprc -= bprc;
15188eb6488eSEric Joyner 
15198eb6488eSEric Joyner 	stats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
15208eb6488eSEric Joyner 	stats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
15218eb6488eSEric Joyner 	stats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
15228eb6488eSEric Joyner 	stats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
15238eb6488eSEric Joyner 	stats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
15248eb6488eSEric Joyner 	stats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
15258eb6488eSEric Joyner 
15268eb6488eSEric Joyner 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
15278eb6488eSEric Joyner 	stats->lxontxc += lxon;
15288eb6488eSEric Joyner 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
15298eb6488eSEric Joyner 	stats->lxofftxc += lxoff;
15308eb6488eSEric Joyner 	total = lxon + lxoff;
15318eb6488eSEric Joyner 
15328eb6488eSEric Joyner 	stats->gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
15338eb6488eSEric Joyner 	stats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
15348eb6488eSEric Joyner 	stats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
15358eb6488eSEric Joyner 	stats->gptc -= total;
15368eb6488eSEric Joyner 	stats->mptc -= total;
15378eb6488eSEric Joyner 	stats->ptc64 -= total;
15388eb6488eSEric Joyner 	stats->gotc -= total * ETHER_MIN_LEN;
15398eb6488eSEric Joyner 
15408eb6488eSEric Joyner 	stats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
15418eb6488eSEric Joyner 	stats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
15428eb6488eSEric Joyner 	stats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
15438eb6488eSEric Joyner 	stats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
15448eb6488eSEric Joyner 	stats->mngprc += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
15458eb6488eSEric Joyner 	stats->mngpdc += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
15468eb6488eSEric Joyner 	stats->mngptc += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
15478eb6488eSEric Joyner 	stats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
15488eb6488eSEric Joyner 	stats->tpt += IXGBE_READ_REG(hw, IXGBE_TPT);
15498eb6488eSEric Joyner 	stats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
15508eb6488eSEric Joyner 	stats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
15518eb6488eSEric Joyner 	stats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
15528eb6488eSEric Joyner 	stats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
15538eb6488eSEric Joyner 	stats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
15548eb6488eSEric Joyner 	stats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
15558eb6488eSEric Joyner 	stats->xec += IXGBE_READ_REG(hw, IXGBE_XEC);
15568eb6488eSEric Joyner 	stats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
15578eb6488eSEric Joyner 	stats->fclast += IXGBE_READ_REG(hw, IXGBE_FCLAST);
15588eb6488eSEric Joyner 	/* Only read FCOE on 82599 */
15598eb6488eSEric Joyner 	if (hw->mac.type != ixgbe_mac_82598EB) {
15608eb6488eSEric Joyner 		stats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
15618eb6488eSEric Joyner 		stats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
15628eb6488eSEric Joyner 		stats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
15638eb6488eSEric Joyner 		stats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
15648eb6488eSEric Joyner 		stats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
15658eb6488eSEric Joyner 	}
15668eb6488eSEric Joyner 
15678eb6488eSEric Joyner 	/* Fill out the OS statistics structure */
1568b1d5caf3SKevin Bowling 	IXGBE_SET_IPACKETS(sc, stats->gprc);
1569b1d5caf3SKevin Bowling 	IXGBE_SET_OPACKETS(sc, stats->gptc);
1570b1d5caf3SKevin Bowling 	IXGBE_SET_IBYTES(sc, stats->gorc);
1571b1d5caf3SKevin Bowling 	IXGBE_SET_OBYTES(sc, stats->gotc);
1572b1d5caf3SKevin Bowling 	IXGBE_SET_IMCASTS(sc, stats->mprc);
1573b1d5caf3SKevin Bowling 	IXGBE_SET_OMCASTS(sc, stats->mptc);
1574b1d5caf3SKevin Bowling 	IXGBE_SET_COLLISIONS(sc, 0);
1575b1d5caf3SKevin Bowling 	IXGBE_SET_IQDROPS(sc, total_missed_rx);
1576afb1aa4eSPiotr Pietruszewski 
1577afb1aa4eSPiotr Pietruszewski 	/*
1578afb1aa4eSPiotr Pietruszewski 	 * Aggregate following types of errors as RX errors:
1579afb1aa4eSPiotr Pietruszewski 	 * - CRC error count,
1580afb1aa4eSPiotr Pietruszewski 	 * - illegal byte error count,
1581afb1aa4eSPiotr Pietruszewski 	 * - checksum error count,
1582afb1aa4eSPiotr Pietruszewski 	 * - missed packets count,
1583afb1aa4eSPiotr Pietruszewski 	 * - length error count,
1584afb1aa4eSPiotr Pietruszewski 	 * - undersized packets count,
1585afb1aa4eSPiotr Pietruszewski 	 * - fragmented packets count,
1586afb1aa4eSPiotr Pietruszewski 	 * - oversized packets count,
1587afb1aa4eSPiotr Pietruszewski 	 * - jabber count.
1588afb1aa4eSPiotr Pietruszewski 	 */
1589b1d5caf3SKevin Bowling 	IXGBE_SET_IERRORS(sc, stats->crcerrs + stats->illerrc + stats->xec +
1590afb1aa4eSPiotr Pietruszewski 	    stats->mpc[0] + stats->rlec + stats->ruc + stats->rfc + stats->roc +
1591afb1aa4eSPiotr Pietruszewski 	    stats->rjc);
15928eb6488eSEric Joyner } /* ixgbe_update_stats_counters */
15938eb6488eSEric Joyner 
15948eb6488eSEric Joyner /************************************************************************
15958eb6488eSEric Joyner  * ixgbe_add_hw_stats
15968eb6488eSEric Joyner  *
15978eb6488eSEric Joyner  *   Add sysctl variables, one per statistic, to the system.
15988eb6488eSEric Joyner  ************************************************************************/
15998eb6488eSEric Joyner static void
1600b1d5caf3SKevin Bowling ixgbe_add_hw_stats(struct ixgbe_softc *sc)
16018eb6488eSEric Joyner {
1602b1d5caf3SKevin Bowling 	device_t               dev = iflib_get_dev(sc->ctx);
1603c19c7afeSEric Joyner 	struct ix_rx_queue     *rx_que;
1604c19c7afeSEric Joyner 	struct ix_tx_queue     *tx_que;
16058eb6488eSEric Joyner 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
16068eb6488eSEric Joyner 	struct sysctl_oid      *tree = device_get_sysctl_tree(dev);
16078eb6488eSEric Joyner 	struct sysctl_oid_list *child = SYSCTL_CHILDREN(tree);
1608b1d5caf3SKevin Bowling 	struct ixgbe_hw_stats  *stats = &sc->stats.pf;
16098eb6488eSEric Joyner 	struct sysctl_oid      *stat_node, *queue_node;
16108eb6488eSEric Joyner 	struct sysctl_oid_list *stat_list, *queue_list;
1611c19c7afeSEric Joyner 	int                    i;
16128eb6488eSEric Joyner 
16138eb6488eSEric Joyner #define QUEUE_NAME_LEN 32
16148eb6488eSEric Joyner 	char                   namebuf[QUEUE_NAME_LEN];
16158eb6488eSEric Joyner 
16168eb6488eSEric Joyner 	/* Driver Statistics */
16178eb6488eSEric Joyner 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dropped",
1618b1d5caf3SKevin Bowling 	    CTLFLAG_RD, &sc->dropped_pkts, "Driver dropped packets");
16198eb6488eSEric Joyner 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "watchdog_events",
1620b1d5caf3SKevin Bowling 	    CTLFLAG_RD, &sc->watchdog_events, "Watchdog timeouts");
16218eb6488eSEric Joyner 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "link_irq",
1622b1d5caf3SKevin Bowling 	    CTLFLAG_RD, &sc->link_irq, "Link MSI-X IRQ Handled");
16238eb6488eSEric Joyner 
1624b1d5caf3SKevin Bowling 	for (i = 0, tx_que = sc->tx_queues; i < sc->num_tx_queues; i++, tx_que++) {
1625c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
16268eb6488eSEric Joyner 		snprintf(namebuf, QUEUE_NAME_LEN, "queue%d", i);
16278eb6488eSEric Joyner 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
162820b91f0aSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
16298eb6488eSEric Joyner 		queue_list = SYSCTL_CHILDREN(queue_node);
16308eb6488eSEric Joyner 
16318eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_head",
163220b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, txr, 0,
16338eb6488eSEric Joyner 		    ixgbe_sysctl_tdh_handler, "IU", "Transmit Descriptor Head");
16348eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_tail",
163520b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, txr, 0,
16368eb6488eSEric Joyner 		    ixgbe_sysctl_tdt_handler, "IU", "Transmit Descriptor Tail");
16378eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tso_tx",
16388eb6488eSEric Joyner 		    CTLFLAG_RD, &txr->tso_tx, "TSO");
16398eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tx_packets",
16408eb6488eSEric Joyner 		    CTLFLAG_RD, &txr->total_packets,
16418eb6488eSEric Joyner 		    "Queue Packets Transmitted");
16428eb6488eSEric Joyner 	}
16438eb6488eSEric Joyner 
1644b1d5caf3SKevin Bowling 	for (i = 0, rx_que = sc->rx_queues; i < sc->num_rx_queues; i++, rx_que++) {
1645c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
16468eb6488eSEric Joyner 		snprintf(namebuf, QUEUE_NAME_LEN, "queue%d", i);
16478eb6488eSEric Joyner 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
164820b91f0aSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
16498eb6488eSEric Joyner 		queue_list = SYSCTL_CHILDREN(queue_node);
16508eb6488eSEric Joyner 
1651c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "interrupt_rate",
165220b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1653b1d5caf3SKevin Bowling 		    &sc->rx_queues[i], 0,
1654c19c7afeSEric Joyner 		    ixgbe_sysctl_interrupt_rate_handler, "IU",
1655c19c7afeSEric Joyner 		    "Interrupt Rate");
1656c19c7afeSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "irqs",
1657b1d5caf3SKevin Bowling 		    CTLFLAG_RD, &(sc->rx_queues[i].irqs),
1658c19c7afeSEric Joyner 		    "irqs on this queue");
16598eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_head",
166020b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, rxr, 0,
16618eb6488eSEric Joyner 		    ixgbe_sysctl_rdh_handler, "IU", "Receive Descriptor Head");
16628eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_tail",
166320b91f0aSPawel Biernacki 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, rxr, 0,
16648eb6488eSEric Joyner 		    ixgbe_sysctl_rdt_handler, "IU", "Receive Descriptor Tail");
16658eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_packets",
16668eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_packets, "Queue Packets Received");
16678eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_bytes",
16688eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_bytes, "Queue Bytes Received");
16698eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_copies",
16708eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_copies, "Copied RX Frames");
16718eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_discarded",
16728eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_discarded, "Discarded RX packets");
16738eb6488eSEric Joyner 	}
16748eb6488eSEric Joyner 
16758eb6488eSEric Joyner 	/* MAC stats get their own sub node */
16768eb6488eSEric Joyner 
16778eb6488eSEric Joyner 	stat_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "mac_stats",
167820b91f0aSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "MAC Statistics");
16798eb6488eSEric Joyner 	stat_list = SYSCTL_CHILDREN(stat_node);
16808eb6488eSEric Joyner 
1681afb1aa4eSPiotr Pietruszewski 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_errs",
1682b1d5caf3SKevin Bowling 	    CTLFLAG_RD, &sc->ierrors, IXGBE_SYSCTL_DESC_RX_ERRS);
16838eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "crc_errs",
16848eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->crcerrs, "CRC Errors");
16858eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "ill_errs",
16868eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->illerrc, "Illegal Byte Errors");
16878eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "byte_errs",
16888eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->errbc, "Byte Errors");
16898eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "short_discards",
16908eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mspdc, "MAC Short Packets Discarded");
16918eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "local_faults",
16928eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mlfc, "MAC Local Faults");
16938eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "remote_faults",
16948eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mrfc, "MAC Remote Faults");
16958eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rec_len_errs",
16968eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->rlec, "Receive Length Errors");
16978eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_missed_packets",
16988eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mpc[0], "RX Missed Packet Count");
16998eb6488eSEric Joyner 
17008eb6488eSEric Joyner 	/* Flow Control stats */
17018eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_txd",
17028eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxontxc, "Link XON Transmitted");
17038eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_recvd",
17048eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxonrxc, "Link XON Received");
17058eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_txd",
17068eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxofftxc, "Link XOFF Transmitted");
17078eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_recvd",
17088eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxoffrxc, "Link XOFF Received");
17098eb6488eSEric Joyner 
17108eb6488eSEric Joyner 	/* Packet Reception Stats */
17118eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_octets_rcvd",
17128eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->tor, "Total Octets Received");
17138eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_rcvd",
17148eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gorc, "Good Octets Received");
17158eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_rcvd",
17168eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->tpr, "Total Packets Received");
17178eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_rcvd",
17188eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gprc, "Good Packets Received");
17198eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_rcvd",
17208eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mprc, "Multicast Packets Received");
17218eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_rcvd",
17228eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->bprc, "Broadcast Packets Received");
17238eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_64",
17248eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc64, "64 byte frames received ");
17258eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_65_127",
17268eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc127, "65-127 byte frames received");
17278eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_128_255",
17288eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc255, "128-255 byte frames received");
17298eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_256_511",
17308eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc511, "256-511 byte frames received");
17318eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_512_1023",
17328eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc1023, "512-1023 byte frames received");
17338eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_1024_1522",
17348eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc1522, "1023-1522 byte frames received");
17358eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_undersized",
17368eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ruc, "Receive Undersized");
17378eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_fragmented",
17388eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->rfc, "Fragmented Packets Received ");
17398eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_oversized",
17408eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->roc, "Oversized Packets Received");
17418eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_jabberd",
17428eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->rjc, "Received Jabber");
17438eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_rcvd",
17448eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mngprc, "Management Packets Received");
17458eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_drpd",
17468eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mngptc, "Management Packets Dropped");
17478eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "checksum_errs",
17488eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->xec, "Checksum Errors");
17498eb6488eSEric Joyner 
17508eb6488eSEric Joyner 	/* Packet Transmission Stats */
17518eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_txd",
17528eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gotc, "Good Octets Transmitted");
17538eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_txd",
17548eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->tpt, "Total Packets Transmitted");
17558eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_txd",
17568eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gptc, "Good Packets Transmitted");
17578eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_txd",
17588eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->bptc, "Broadcast Packets Transmitted");
17598eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_txd",
17608eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mptc, "Multicast Packets Transmitted");
17618eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_txd",
17628eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mngptc, "Management Packets Transmitted");
17638eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_64",
17648eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc64, "64 byte frames transmitted ");
17658eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_65_127",
17668eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc127, "65-127 byte frames transmitted");
17678eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_128_255",
17688eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc255, "128-255 byte frames transmitted");
17698eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_256_511",
17708eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc511, "256-511 byte frames transmitted");
17718eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_512_1023",
17728eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc1023, "512-1023 byte frames transmitted");
17738eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_1024_1522",
17748eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc1522, "1024-1522 byte frames transmitted");
17758eb6488eSEric Joyner } /* ixgbe_add_hw_stats */
17768eb6488eSEric Joyner 
17778eb6488eSEric Joyner /************************************************************************
17788eb6488eSEric Joyner  * ixgbe_sysctl_tdh_handler - Transmit Descriptor Head handler function
17798eb6488eSEric Joyner  *
17808eb6488eSEric Joyner  *   Retrieves the TDH value from the hardware
17818eb6488eSEric Joyner  ************************************************************************/
1782758cc3dcSJack F Vogel static int
1783758cc3dcSJack F Vogel ixgbe_sysctl_tdh_handler(SYSCTL_HANDLER_ARGS)
1784758cc3dcSJack F Vogel {
1785758cc3dcSJack F Vogel 	struct tx_ring *txr = ((struct tx_ring *)oidp->oid_arg1);
17868eb6488eSEric Joyner 	int            error;
17878eb6488eSEric Joyner 	unsigned int   val;
1788758cc3dcSJack F Vogel 
17898eb6488eSEric Joyner 	if (!txr)
17908eb6488eSEric Joyner 		return (0);
17918eb6488eSEric Joyner 
1792b1d5caf3SKevin Bowling 	val = IXGBE_READ_REG(&txr->sc->hw, IXGBE_TDH(txr->me));
1793758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1794758cc3dcSJack F Vogel 	if (error || !req->newptr)
1795758cc3dcSJack F Vogel 		return error;
1796758cc3dcSJack F Vogel 
17978eb6488eSEric Joyner 	return (0);
17988eb6488eSEric Joyner } /* ixgbe_sysctl_tdh_handler */
17998eb6488eSEric Joyner 
18008eb6488eSEric Joyner /************************************************************************
18018eb6488eSEric Joyner  * ixgbe_sysctl_tdt_handler - Transmit Descriptor Tail handler function
18028eb6488eSEric Joyner  *
1803758cc3dcSJack F Vogel  *   Retrieves the TDT value from the hardware
18048eb6488eSEric Joyner  ************************************************************************/
1805758cc3dcSJack F Vogel static int
1806758cc3dcSJack F Vogel ixgbe_sysctl_tdt_handler(SYSCTL_HANDLER_ARGS)
1807758cc3dcSJack F Vogel {
1808758cc3dcSJack F Vogel 	struct tx_ring *txr = ((struct tx_ring *)oidp->oid_arg1);
18098eb6488eSEric Joyner 	int            error;
18108eb6488eSEric Joyner 	unsigned int   val;
1811758cc3dcSJack F Vogel 
18128eb6488eSEric Joyner 	if (!txr)
18138eb6488eSEric Joyner 		return (0);
18148eb6488eSEric Joyner 
1815b1d5caf3SKevin Bowling 	val = IXGBE_READ_REG(&txr->sc->hw, IXGBE_TDT(txr->me));
1816758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1817758cc3dcSJack F Vogel 	if (error || !req->newptr)
1818758cc3dcSJack F Vogel 		return error;
1819758cc3dcSJack F Vogel 
18208eb6488eSEric Joyner 	return (0);
18218eb6488eSEric Joyner } /* ixgbe_sysctl_tdt_handler */
18228eb6488eSEric Joyner 
18238eb6488eSEric Joyner /************************************************************************
18248eb6488eSEric Joyner  * ixgbe_sysctl_rdh_handler - Receive Descriptor Head handler function
18258eb6488eSEric Joyner  *
1826758cc3dcSJack F Vogel  *   Retrieves the RDH value from the hardware
18278eb6488eSEric Joyner  ************************************************************************/
1828758cc3dcSJack F Vogel static int
1829758cc3dcSJack F Vogel ixgbe_sysctl_rdh_handler(SYSCTL_HANDLER_ARGS)
1830758cc3dcSJack F Vogel {
1831758cc3dcSJack F Vogel 	struct rx_ring *rxr = ((struct rx_ring *)oidp->oid_arg1);
18328eb6488eSEric Joyner 	int            error;
18338eb6488eSEric Joyner 	unsigned int   val;
1834758cc3dcSJack F Vogel 
18358eb6488eSEric Joyner 	if (!rxr)
18368eb6488eSEric Joyner 		return (0);
18378eb6488eSEric Joyner 
1838b1d5caf3SKevin Bowling 	val = IXGBE_READ_REG(&rxr->sc->hw, IXGBE_RDH(rxr->me));
1839758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1840758cc3dcSJack F Vogel 	if (error || !req->newptr)
1841758cc3dcSJack F Vogel 		return error;
1842758cc3dcSJack F Vogel 
18438eb6488eSEric Joyner 	return (0);
18448eb6488eSEric Joyner } /* ixgbe_sysctl_rdh_handler */
18458eb6488eSEric Joyner 
18468eb6488eSEric Joyner /************************************************************************
18478eb6488eSEric Joyner  * ixgbe_sysctl_rdt_handler - Receive Descriptor Tail handler function
18488eb6488eSEric Joyner  *
1849758cc3dcSJack F Vogel  *   Retrieves the RDT value from the hardware
18508eb6488eSEric Joyner  ************************************************************************/
1851758cc3dcSJack F Vogel static int
1852758cc3dcSJack F Vogel ixgbe_sysctl_rdt_handler(SYSCTL_HANDLER_ARGS)
1853758cc3dcSJack F Vogel {
1854758cc3dcSJack F Vogel 	struct rx_ring *rxr = ((struct rx_ring *)oidp->oid_arg1);
18558eb6488eSEric Joyner 	int            error;
18568eb6488eSEric Joyner 	unsigned int   val;
1857758cc3dcSJack F Vogel 
18588eb6488eSEric Joyner 	if (!rxr)
18598eb6488eSEric Joyner 		return (0);
18608eb6488eSEric Joyner 
1861b1d5caf3SKevin Bowling 	val = IXGBE_READ_REG(&rxr->sc->hw, IXGBE_RDT(rxr->me));
1862758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1863758cc3dcSJack F Vogel 	if (error || !req->newptr)
1864758cc3dcSJack F Vogel 		return error;
18658eb6488eSEric Joyner 
18668eb6488eSEric Joyner 	return (0);
18678eb6488eSEric Joyner } /* ixgbe_sysctl_rdt_handler */
18688eb6488eSEric Joyner 
18698eb6488eSEric Joyner /************************************************************************
1870c19c7afeSEric Joyner  * ixgbe_if_vlan_register
18718eb6488eSEric Joyner  *
18728eb6488eSEric Joyner  *   Run via vlan config EVENT, it enables us to use the
18738eb6488eSEric Joyner  *   HW Filter table since we can get the vlan id. This
18748eb6488eSEric Joyner  *   just creates the entry in the soft version of the
18758eb6488eSEric Joyner  *   VFTA, init will repopulate the real table.
18768eb6488eSEric Joyner  ************************************************************************/
18778eb6488eSEric Joyner static void
1878c19c7afeSEric Joyner ixgbe_if_vlan_register(if_ctx_t ctx, u16 vtag)
18798eb6488eSEric Joyner {
1880b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
18818eb6488eSEric Joyner 	u16            index, bit;
18828eb6488eSEric Joyner 
18838eb6488eSEric Joyner 	index = (vtag >> 5) & 0x7F;
18848eb6488eSEric Joyner 	bit = vtag & 0x1F;
1885b1d5caf3SKevin Bowling 	sc->shadow_vfta[index] |= (1 << bit);
1886b1d5caf3SKevin Bowling 	++sc->num_vlans;
1887c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
1888c19c7afeSEric Joyner } /* ixgbe_if_vlan_register */
18898eb6488eSEric Joyner 
18908eb6488eSEric Joyner /************************************************************************
1891c19c7afeSEric Joyner  * ixgbe_if_vlan_unregister
18928eb6488eSEric Joyner  *
18938eb6488eSEric Joyner  *   Run via vlan unconfig EVENT, remove our entry in the soft vfta.
18948eb6488eSEric Joyner  ************************************************************************/
18958eb6488eSEric Joyner static void
1896c19c7afeSEric Joyner ixgbe_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
18978eb6488eSEric Joyner {
1898b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
18998eb6488eSEric Joyner 	u16            index, bit;
19008eb6488eSEric Joyner 
19018eb6488eSEric Joyner 	index = (vtag >> 5) & 0x7F;
19028eb6488eSEric Joyner 	bit = vtag & 0x1F;
1903b1d5caf3SKevin Bowling 	sc->shadow_vfta[index] &= ~(1 << bit);
1904b1d5caf3SKevin Bowling 	--sc->num_vlans;
19058eb6488eSEric Joyner 	/* Re-init to load the changes */
1906c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
1907c19c7afeSEric Joyner } /* ixgbe_if_vlan_unregister */
19088eb6488eSEric Joyner 
19098eb6488eSEric Joyner /************************************************************************
19108eb6488eSEric Joyner  * ixgbe_setup_vlan_hw_support
19118eb6488eSEric Joyner  ************************************************************************/
19128eb6488eSEric Joyner static void
1913c19c7afeSEric Joyner ixgbe_setup_vlan_hw_support(if_ctx_t ctx)
19148eb6488eSEric Joyner {
1915c19c7afeSEric Joyner 	struct ifnet	*ifp = iflib_get_ifp(ctx);
1916b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
1917b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
19188eb6488eSEric Joyner 	struct rx_ring  *rxr;
19198eb6488eSEric Joyner 	int             i;
19208eb6488eSEric Joyner 	u32             ctrl;
19218eb6488eSEric Joyner 
19228eb6488eSEric Joyner 
19238eb6488eSEric Joyner 	/*
19248eb6488eSEric Joyner 	 * We get here thru init_locked, meaning
19258eb6488eSEric Joyner 	 * a soft reset, this has already cleared
19268eb6488eSEric Joyner 	 * the VFTA and other state, so if there
19278eb6488eSEric Joyner 	 * have been no vlan's registered do nothing.
19288eb6488eSEric Joyner 	 */
1929b1d5caf3SKevin Bowling 	if (sc->num_vlans == 0)
19308eb6488eSEric Joyner 		return;
19318eb6488eSEric Joyner 
19328eb6488eSEric Joyner 	/* Setup the queues for vlans */
19339c92bb71SSean Bruno 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1934b1d5caf3SKevin Bowling 		for (i = 0; i < sc->num_rx_queues; i++) {
1935b1d5caf3SKevin Bowling 			rxr = &sc->rx_queues[i].rxr;
19368eb6488eSEric Joyner 			/* On 82599 the VLAN enable is per/queue in RXDCTL */
19378eb6488eSEric Joyner 			if (hw->mac.type != ixgbe_mac_82598EB) {
19388eb6488eSEric Joyner 				ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
19398eb6488eSEric Joyner 				ctrl |= IXGBE_RXDCTL_VME;
19408eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), ctrl);
19418eb6488eSEric Joyner 			}
194279b36ec9SKevin Bowling 			rxr->vtag_strip = true;
1943758cc3dcSJack F Vogel 		}
19449c92bb71SSean Bruno 	}
1945758cc3dcSJack F Vogel 
19468eb6488eSEric Joyner 	if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
19478eb6488eSEric Joyner 		return;
19488eb6488eSEric Joyner 	/*
19498eb6488eSEric Joyner 	 * A soft reset zero's out the VFTA, so
19508eb6488eSEric Joyner 	 * we need to repopulate it now.
19518eb6488eSEric Joyner 	 */
19528eb6488eSEric Joyner 	for (i = 0; i < IXGBE_VFTA_SIZE; i++)
1953b1d5caf3SKevin Bowling 		if (sc->shadow_vfta[i] != 0)
19548eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(i),
1955b1d5caf3SKevin Bowling 			    sc->shadow_vfta[i]);
19568eb6488eSEric Joyner 
19578eb6488eSEric Joyner 	ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
19588eb6488eSEric Joyner 	/* Enable the Filter Table if enabled */
19598eb6488eSEric Joyner 	if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
19608eb6488eSEric Joyner 		ctrl &= ~IXGBE_VLNCTRL_CFIEN;
19618eb6488eSEric Joyner 		ctrl |= IXGBE_VLNCTRL_VFE;
19628eb6488eSEric Joyner 	}
19638eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
19648eb6488eSEric Joyner 		ctrl |= IXGBE_VLNCTRL_VME;
19658eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
19668eb6488eSEric Joyner } /* ixgbe_setup_vlan_hw_support */
19678eb6488eSEric Joyner 
19688eb6488eSEric Joyner /************************************************************************
19698eb6488eSEric Joyner  * ixgbe_get_slot_info
19708eb6488eSEric Joyner  *
19718eb6488eSEric Joyner  *   Get the width and transaction speed of
19728eb6488eSEric Joyner  *   the slot this adapter is plugged into.
19738eb6488eSEric Joyner  ************************************************************************/
19748eb6488eSEric Joyner static void
1975b1d5caf3SKevin Bowling ixgbe_get_slot_info(struct ixgbe_softc *sc)
19768eb6488eSEric Joyner {
1977b1d5caf3SKevin Bowling 	device_t        dev = iflib_get_dev(sc->ctx);
1978b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
197979b36ec9SKevin Bowling 	int             bus_info_valid = true;
19808eb6488eSEric Joyner 	u32             offset;
19818eb6488eSEric Joyner 	u16             link;
19828eb6488eSEric Joyner 
19838eb6488eSEric Joyner 	/* Some devices are behind an internal bridge */
19848eb6488eSEric Joyner 	switch (hw->device_id) {
19858eb6488eSEric Joyner 	case IXGBE_DEV_ID_82599_SFP_SF_QP:
19868eb6488eSEric Joyner 	case IXGBE_DEV_ID_82599_QSFP_SF_QP:
19878eb6488eSEric Joyner 		goto get_parent_info;
19888eb6488eSEric Joyner 	default:
19898eb6488eSEric Joyner 		break;
19908eb6488eSEric Joyner 	}
19918eb6488eSEric Joyner 
19928eb6488eSEric Joyner 	ixgbe_get_bus_info(hw);
19938eb6488eSEric Joyner 
19948eb6488eSEric Joyner 	/*
19958eb6488eSEric Joyner 	 * Some devices don't use PCI-E, but there is no need
19968eb6488eSEric Joyner 	 * to display "Unknown" for bus speed and width.
19978eb6488eSEric Joyner 	 */
19988eb6488eSEric Joyner 	switch (hw->mac.type) {
19998eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
20008eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
20018eb6488eSEric Joyner 		return;
20028eb6488eSEric Joyner 	default:
20038eb6488eSEric Joyner 		goto display;
20048eb6488eSEric Joyner 	}
20058eb6488eSEric Joyner 
20068eb6488eSEric Joyner get_parent_info:
20078eb6488eSEric Joyner 	/*
20088eb6488eSEric Joyner 	 * For the Quad port adapter we need to parse back
20098eb6488eSEric Joyner 	 * up the PCI tree to find the speed of the expansion
20108eb6488eSEric Joyner 	 * slot into which this adapter is plugged. A bit more work.
20118eb6488eSEric Joyner 	 */
20128eb6488eSEric Joyner 	dev = device_get_parent(device_get_parent(dev));
20138eb6488eSEric Joyner #ifdef IXGBE_DEBUG
20148eb6488eSEric Joyner 	device_printf(dev, "parent pcib = %x,%x,%x\n", pci_get_bus(dev),
20158eb6488eSEric Joyner 	    pci_get_slot(dev), pci_get_function(dev));
20168eb6488eSEric Joyner #endif
20178eb6488eSEric Joyner 	dev = device_get_parent(device_get_parent(dev));
20188eb6488eSEric Joyner #ifdef IXGBE_DEBUG
20198eb6488eSEric Joyner 	device_printf(dev, "slot pcib = %x,%x,%x\n", pci_get_bus(dev),
20208eb6488eSEric Joyner 	    pci_get_slot(dev), pci_get_function(dev));
20218eb6488eSEric Joyner #endif
20228eb6488eSEric Joyner 	/* Now get the PCI Express Capabilities offset */
20238eb6488eSEric Joyner 	if (pci_find_cap(dev, PCIY_EXPRESS, &offset)) {
20248eb6488eSEric Joyner 		/*
20258eb6488eSEric Joyner 		 * Hmm...can't get PCI-Express capabilities.
20268eb6488eSEric Joyner 		 * Falling back to default method.
20278eb6488eSEric Joyner 		 */
202879b36ec9SKevin Bowling 		bus_info_valid = false;
20298eb6488eSEric Joyner 		ixgbe_get_bus_info(hw);
20308eb6488eSEric Joyner 		goto display;
20318eb6488eSEric Joyner 	}
20328eb6488eSEric Joyner 	/* ...and read the Link Status Register */
20338eb6488eSEric Joyner 	link = pci_read_config(dev, offset + PCIER_LINK_STA, 2);
20348eb6488eSEric Joyner 	ixgbe_set_pci_config_data_generic(hw, link);
20358eb6488eSEric Joyner 
20368eb6488eSEric Joyner display:
20378eb6488eSEric Joyner 	device_printf(dev, "PCI Express Bus: Speed %s %s\n",
20388eb6488eSEric Joyner 	    ((hw->bus.speed == ixgbe_bus_speed_8000)    ? "8.0GT/s"  :
20398eb6488eSEric Joyner 	     (hw->bus.speed == ixgbe_bus_speed_5000)    ? "5.0GT/s"  :
20408eb6488eSEric Joyner 	     (hw->bus.speed == ixgbe_bus_speed_2500)    ? "2.5GT/s"  :
20418eb6488eSEric Joyner 	     "Unknown"),
20428eb6488eSEric Joyner 	    ((hw->bus.width == ixgbe_bus_width_pcie_x8) ? "Width x8" :
20438eb6488eSEric Joyner 	     (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "Width x4" :
20448eb6488eSEric Joyner 	     (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "Width x1" :
20458eb6488eSEric Joyner 	     "Unknown"));
20468eb6488eSEric Joyner 
20478eb6488eSEric Joyner 	if (bus_info_valid) {
20488eb6488eSEric Joyner 		if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
20498eb6488eSEric Joyner 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
20508eb6488eSEric Joyner 		    (hw->bus.speed == ixgbe_bus_speed_2500))) {
20518eb6488eSEric Joyner 			device_printf(dev, "PCI-Express bandwidth available for this card\n     is not sufficient for optimal performance.\n");
20528eb6488eSEric Joyner 			device_printf(dev, "For optimal performance a x8 PCIE, or x4 PCIE Gen2 slot is required.\n");
20538eb6488eSEric Joyner 		}
20548eb6488eSEric Joyner 		if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
20558eb6488eSEric Joyner 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
20568eb6488eSEric Joyner 		    (hw->bus.speed < ixgbe_bus_speed_8000))) {
20578eb6488eSEric Joyner 			device_printf(dev, "PCI-Express bandwidth available for this card\n     is not sufficient for optimal performance.\n");
20588eb6488eSEric Joyner 			device_printf(dev, "For optimal performance a x8 PCIE Gen3 slot is required.\n");
20598eb6488eSEric Joyner 		}
20608eb6488eSEric Joyner 	} else
20618eb6488eSEric Joyner 		device_printf(dev, "Unable to determine slot speed/width. The speed/width reported are that of the internal switch.\n");
20628eb6488eSEric Joyner 
20638eb6488eSEric Joyner 	return;
20648eb6488eSEric Joyner } /* ixgbe_get_slot_info */
20658eb6488eSEric Joyner 
20668eb6488eSEric Joyner /************************************************************************
2067c19c7afeSEric Joyner  * ixgbe_if_msix_intr_assign
2068c19c7afeSEric Joyner  *
2069c19c7afeSEric Joyner  *   Setup MSI-X Interrupt resources and handlers
20708eb6488eSEric Joyner  ************************************************************************/
2071c19c7afeSEric Joyner static int
2072c19c7afeSEric Joyner ixgbe_if_msix_intr_assign(if_ctx_t ctx, int msix)
20738eb6488eSEric Joyner {
2074b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
2075b1d5caf3SKevin Bowling 	struct ix_rx_queue *rx_que = sc->rx_queues;
2076c19c7afeSEric Joyner 	struct ix_tx_queue *tx_que;
2077c19c7afeSEric Joyner 	int                error, rid, vector = 0;
2078c19c7afeSEric Joyner 	int                cpu_id = 0;
2079c19c7afeSEric Joyner 	char               buf[16];
20808eb6488eSEric Joyner 
2081c19c7afeSEric Joyner 	/* Admin Que is vector 0*/
2082c19c7afeSEric Joyner 	rid = vector + 1;
2083b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, vector++, rx_que++) {
2084c19c7afeSEric Joyner 		rid = vector + 1;
2085c19c7afeSEric Joyner 
2086c19c7afeSEric Joyner 		snprintf(buf, sizeof(buf), "rxq%d", i);
2087c19c7afeSEric Joyner 		error = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
208881be6552SMatt Macy 		    IFLIB_INTR_RXTX, ixgbe_msix_que, rx_que, rx_que->rxr.me, buf);
2089c19c7afeSEric Joyner 
2090c19c7afeSEric Joyner 		if (error) {
2091c19c7afeSEric Joyner 			device_printf(iflib_get_dev(ctx),
2092c19c7afeSEric Joyner 			    "Failed to allocate que int %d err: %d", i, error);
2093b1d5caf3SKevin Bowling 			sc->num_rx_queues = i + 1;
2094c19c7afeSEric Joyner 			goto fail;
20958eb6488eSEric Joyner 		}
20968eb6488eSEric Joyner 
2097c19c7afeSEric Joyner 		rx_que->msix = vector;
2098b1d5caf3SKevin Bowling 		if (sc->feat_en & IXGBE_FEATURE_RSS) {
2099c19c7afeSEric Joyner 			/*
2100c19c7afeSEric Joyner 			 * The queue ID is used as the RSS layer bucket ID.
2101c19c7afeSEric Joyner 			 * We look up the queue ID -> RSS CPU ID and select
2102c19c7afeSEric Joyner 			 * that.
2103c19c7afeSEric Joyner 			 */
2104c19c7afeSEric Joyner 			cpu_id = rss_getcpu(i % rss_getnumbuckets());
21058eb6488eSEric Joyner 		} else {
2106c19c7afeSEric Joyner 			/*
2107b97de13aSMarius Strobl 			 * Bind the MSI-X vector, and thus the
2108c19c7afeSEric Joyner 			 * rings to the corresponding cpu.
2109c19c7afeSEric Joyner 			 *
2110c19c7afeSEric Joyner 			 * This just happens to match the default RSS
2111c19c7afeSEric Joyner 			 * round-robin bucket -> queue -> CPU allocation.
2112c19c7afeSEric Joyner 			 */
2113b1d5caf3SKevin Bowling 			if (sc->num_rx_queues > 1)
2114c19c7afeSEric Joyner 				cpu_id = i;
21158eb6488eSEric Joyner 		}
21168eb6488eSEric Joyner 
2117c19c7afeSEric Joyner 	}
2118b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_tx_queues; i++) {
2119c19c7afeSEric Joyner 		snprintf(buf, sizeof(buf), "txq%d", i);
2120b1d5caf3SKevin Bowling 		tx_que = &sc->tx_queues[i];
2121b1d5caf3SKevin Bowling 		tx_que->msix = i % sc->num_rx_queues;
2122c19c7afeSEric Joyner 		iflib_softirq_alloc_generic(ctx,
2123b1d5caf3SKevin Bowling 		    &sc->rx_queues[tx_que->msix].que_irq,
2124c19c7afeSEric Joyner 		    IFLIB_INTR_TX, tx_que, tx_que->txr.me, buf);
2125c19c7afeSEric Joyner 	}
2126c19c7afeSEric Joyner 	rid = vector + 1;
2127b1d5caf3SKevin Bowling 	error = iflib_irq_alloc_generic(ctx, &sc->irq, rid,
2128b1d5caf3SKevin Bowling 	    IFLIB_INTR_ADMIN, ixgbe_msix_link, sc, 0, "aq");
2129c19c7afeSEric Joyner 	if (error) {
2130c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
2131c19c7afeSEric Joyner 		    "Failed to register admin handler");
2132c19c7afeSEric Joyner 		return (error);
2133c19c7afeSEric Joyner 	}
2134c19c7afeSEric Joyner 
2135b1d5caf3SKevin Bowling 	sc->vector = vector;
2136c19c7afeSEric Joyner 
2137c19c7afeSEric Joyner 	return (0);
2138c19c7afeSEric Joyner fail:
2139b1d5caf3SKevin Bowling 	iflib_irq_free(ctx, &sc->irq);
2140b1d5caf3SKevin Bowling 	rx_que = sc->rx_queues;
2141b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, rx_que++)
2142c19c7afeSEric Joyner 		iflib_irq_free(ctx, &rx_que->que_irq);
2143c19c7afeSEric Joyner 
2144c19c7afeSEric Joyner 	return (error);
2145c19c7afeSEric Joyner } /* ixgbe_if_msix_intr_assign */
2146c19c7afeSEric Joyner 
214764881da4SSai Rajesh Tallamraju static inline void
2148b1d5caf3SKevin Bowling ixgbe_perform_aim(struct ixgbe_softc *sc, struct ix_rx_queue *que)
214964881da4SSai Rajesh Tallamraju {
215064881da4SSai Rajesh Tallamraju 	uint32_t newitr = 0;
215164881da4SSai Rajesh Tallamraju 	struct rx_ring *rxr = &que->rxr;
215264881da4SSai Rajesh Tallamraju 
215364881da4SSai Rajesh Tallamraju 	/*
215464881da4SSai Rajesh Tallamraju 	 * Do Adaptive Interrupt Moderation:
215564881da4SSai Rajesh Tallamraju 	 *  - Write out last calculated setting
215664881da4SSai Rajesh Tallamraju 	 *  - Calculate based on average size over
215764881da4SSai Rajesh Tallamraju 	 *    the last interval.
215864881da4SSai Rajesh Tallamraju 	 */
215964881da4SSai Rajesh Tallamraju 	if (que->eitr_setting) {
2160b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EITR(que->msix),
216164881da4SSai Rajesh Tallamraju 		    que->eitr_setting);
216264881da4SSai Rajesh Tallamraju 	}
216364881da4SSai Rajesh Tallamraju 
216464881da4SSai Rajesh Tallamraju 	que->eitr_setting = 0;
216564881da4SSai Rajesh Tallamraju 	/* Idle, do nothing */
216664881da4SSai Rajesh Tallamraju 	if (rxr->bytes == 0) {
216764881da4SSai Rajesh Tallamraju 		return;
216864881da4SSai Rajesh Tallamraju 	}
216964881da4SSai Rajesh Tallamraju 
217064881da4SSai Rajesh Tallamraju 	if ((rxr->bytes) && (rxr->packets)) {
217164881da4SSai Rajesh Tallamraju 		newitr = (rxr->bytes / rxr->packets);
217264881da4SSai Rajesh Tallamraju 	}
217364881da4SSai Rajesh Tallamraju 
217464881da4SSai Rajesh Tallamraju 	newitr += 24; /* account for hardware frame, crc */
217564881da4SSai Rajesh Tallamraju 	/* set an upper boundary */
217664881da4SSai Rajesh Tallamraju 	newitr = min(newitr, 3000);
217764881da4SSai Rajesh Tallamraju 
217864881da4SSai Rajesh Tallamraju 	/* Be nice to the mid range */
217964881da4SSai Rajesh Tallamraju 	if ((newitr > 300) && (newitr < 1200)) {
218064881da4SSai Rajesh Tallamraju 		newitr = (newitr / 3);
218164881da4SSai Rajesh Tallamraju 	} else {
218264881da4SSai Rajesh Tallamraju 		newitr = (newitr / 2);
218364881da4SSai Rajesh Tallamraju 	}
218464881da4SSai Rajesh Tallamraju 
2185b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
218664881da4SSai Rajesh Tallamraju 		newitr |= newitr << 16;
218764881da4SSai Rajesh Tallamraju 	} else {
218864881da4SSai Rajesh Tallamraju 		newitr |= IXGBE_EITR_CNT_WDIS;
218964881da4SSai Rajesh Tallamraju 	}
219064881da4SSai Rajesh Tallamraju 
219164881da4SSai Rajesh Tallamraju 	/* save for next interrupt */
219264881da4SSai Rajesh Tallamraju 	que->eitr_setting = newitr;
219364881da4SSai Rajesh Tallamraju 
219464881da4SSai Rajesh Tallamraju 	/* Reset state */
219564881da4SSai Rajesh Tallamraju 	rxr->bytes = 0;
219664881da4SSai Rajesh Tallamraju 	rxr->packets = 0;
219764881da4SSai Rajesh Tallamraju 
219864881da4SSai Rajesh Tallamraju 	return;
219964881da4SSai Rajesh Tallamraju }
220064881da4SSai Rajesh Tallamraju 
2201c19c7afeSEric Joyner /*********************************************************************
22028eb6488eSEric Joyner  * ixgbe_msix_que - MSI-X Queue Interrupt Service routine
2203c19c7afeSEric Joyner  **********************************************************************/
2204c19c7afeSEric Joyner static int
22058eb6488eSEric Joyner ixgbe_msix_que(void *arg)
22068eb6488eSEric Joyner {
2207c19c7afeSEric Joyner 	struct ix_rx_queue *que = arg;
2208b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = que->sc;
2209b1d5caf3SKevin Bowling 	struct ifnet       *ifp = iflib_get_ifp(que->sc->ctx);
22108eb6488eSEric Joyner 
22118eb6488eSEric Joyner 	/* Protect against spurious interrupts */
22128eb6488eSEric Joyner 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
22136c351041SEric Joyner 		return (FILTER_HANDLED);
22148eb6488eSEric Joyner 
2215b1d5caf3SKevin Bowling 	ixgbe_disable_queue(sc, que->msix);
22168eb6488eSEric Joyner 	++que->irqs;
22178eb6488eSEric Joyner 
221864881da4SSai Rajesh Tallamraju 	/* Check for AIM */
2219b1d5caf3SKevin Bowling 	if (sc->enable_aim) {
2220b1d5caf3SKevin Bowling 		ixgbe_perform_aim(sc, que);
222164881da4SSai Rajesh Tallamraju 	}
222264881da4SSai Rajesh Tallamraju 
2223c19c7afeSEric Joyner 	return (FILTER_SCHEDULE_THREAD);
22248eb6488eSEric Joyner } /* ixgbe_msix_que */
22258eb6488eSEric Joyner 
22268eb6488eSEric Joyner /************************************************************************
22278eb6488eSEric Joyner  * ixgbe_media_status - Media Ioctl callback
22288eb6488eSEric Joyner  *
22298eb6488eSEric Joyner  *   Called whenever the user queries the status of
22308eb6488eSEric Joyner  *   the interface using ifconfig.
22318eb6488eSEric Joyner  ************************************************************************/
22328eb6488eSEric Joyner static void
2233c19c7afeSEric Joyner ixgbe_if_media_status(if_ctx_t ctx, struct ifmediareq * ifmr)
22348eb6488eSEric Joyner {
2235b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2236b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
22378eb6488eSEric Joyner 	int             layer;
22388eb6488eSEric Joyner 
2239c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_media_status: begin");
2240c19c7afeSEric Joyner 
22418eb6488eSEric Joyner 	ifmr->ifm_status = IFM_AVALID;
22428eb6488eSEric Joyner 	ifmr->ifm_active = IFM_ETHER;
22438eb6488eSEric Joyner 
2244b1d5caf3SKevin Bowling 	if (!sc->link_active)
22458eb6488eSEric Joyner 		return;
22468eb6488eSEric Joyner 
22478eb6488eSEric Joyner 	ifmr->ifm_status |= IFM_ACTIVE;
2248b1d5caf3SKevin Bowling 	layer = sc->phy_layer;
22498eb6488eSEric Joyner 
22508eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T ||
22518eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_T ||
22528eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_100BASE_TX ||
22538eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
2254b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22558eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22568eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
22578eb6488eSEric Joyner 			break;
22588eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22598eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
22608eb6488eSEric Joyner 			break;
22618eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_100_FULL:
22628eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
22638eb6488eSEric Joyner 			break;
22648eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10_FULL:
22658eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10_T | IFM_FDX;
22668eb6488eSEric Joyner 			break;
22678eb6488eSEric Joyner 		}
2268*d381c807SPiotr Pietruszewski 	if (hw->mac.type == ixgbe_mac_X550)
2269*d381c807SPiotr Pietruszewski 		switch (sc->link_speed) {
2270*d381c807SPiotr Pietruszewski 		case IXGBE_LINK_SPEED_5GB_FULL:
2271*d381c807SPiotr Pietruszewski 			ifmr->ifm_active |= IFM_5000_T | IFM_FDX;
2272*d381c807SPiotr Pietruszewski 			break;
2273*d381c807SPiotr Pietruszewski 		case IXGBE_LINK_SPEED_2_5GB_FULL:
2274*d381c807SPiotr Pietruszewski 			ifmr->ifm_active |= IFM_2500_T | IFM_FDX;
2275*d381c807SPiotr Pietruszewski 			break;
2276*d381c807SPiotr Pietruszewski 		}
22778eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
22788eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
2279b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22808eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22818eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_TWINAX | IFM_FDX;
22828eb6488eSEric Joyner 			break;
22838eb6488eSEric Joyner 		}
22848eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR)
2285b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22868eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22878eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_LR | IFM_FDX;
22888eb6488eSEric Joyner 			break;
22898eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22908eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
22918eb6488eSEric Joyner 			break;
22928eb6488eSEric Joyner 		}
22938eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LRM)
2294b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22958eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22968eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_LRM | IFM_FDX;
22978eb6488eSEric Joyner 			break;
22988eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22998eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
23008eb6488eSEric Joyner 			break;
23018eb6488eSEric Joyner 		}
23028eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR ||
23038eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
2304b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23058eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23068eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
23078eb6488eSEric Joyner 			break;
23088eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23098eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
23108eb6488eSEric Joyner 			break;
23118eb6488eSEric Joyner 		}
23128eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
2313b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23148eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23158eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
23168eb6488eSEric Joyner 			break;
23178eb6488eSEric Joyner 		}
23188eb6488eSEric Joyner 	/*
23198eb6488eSEric Joyner 	 * XXX: These need to use the proper media types once
23208eb6488eSEric Joyner 	 * they're added.
23218eb6488eSEric Joyner 	 */
23228eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
23238eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
2324b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23258eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23268eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
23278eb6488eSEric Joyner 			break;
23288eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23298eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
23308eb6488eSEric Joyner 			break;
23318eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23328eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
23338eb6488eSEric Joyner 			break;
23348eb6488eSEric Joyner 		}
23358eb6488eSEric Joyner 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
23368eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
23378eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
2338b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23398eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23408eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
23418eb6488eSEric Joyner 			break;
23428eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23438eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
23448eb6488eSEric Joyner 			break;
23458eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23468eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
23478eb6488eSEric Joyner 			break;
23488eb6488eSEric Joyner 		}
23498eb6488eSEric Joyner #else
23508eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
2351b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23528eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23538eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_KR | IFM_FDX;
23548eb6488eSEric Joyner 			break;
23558eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23568eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
23578eb6488eSEric Joyner 			break;
23588eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23598eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
23608eb6488eSEric Joyner 			break;
23618eb6488eSEric Joyner 		}
23628eb6488eSEric Joyner 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
23638eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
23648eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
2365b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23668eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23678eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_KX4 | IFM_FDX;
23688eb6488eSEric Joyner 			break;
23698eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23708eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
23718eb6488eSEric Joyner 			break;
23728eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23738eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
23748eb6488eSEric Joyner 			break;
23758eb6488eSEric Joyner 		}
23768eb6488eSEric Joyner #endif
23778eb6488eSEric Joyner 
23788eb6488eSEric Joyner 	/* If nothing is recognized... */
23798eb6488eSEric Joyner 	if (IFM_SUBTYPE(ifmr->ifm_active) == 0)
23808eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_UNKNOWN;
23818eb6488eSEric Joyner 
23828eb6488eSEric Joyner 	/* Display current flow control setting used on link */
23838eb6488eSEric Joyner 	if (hw->fc.current_mode == ixgbe_fc_rx_pause ||
23848eb6488eSEric Joyner 	    hw->fc.current_mode == ixgbe_fc_full)
23858eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
23868eb6488eSEric Joyner 	if (hw->fc.current_mode == ixgbe_fc_tx_pause ||
23878eb6488eSEric Joyner 	    hw->fc.current_mode == ixgbe_fc_full)
23888eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
23898eb6488eSEric Joyner } /* ixgbe_media_status */
23908eb6488eSEric Joyner 
23918eb6488eSEric Joyner /************************************************************************
23928eb6488eSEric Joyner  * ixgbe_media_change - Media Ioctl callback
23938eb6488eSEric Joyner  *
23948eb6488eSEric Joyner  *   Called when the user changes speed/duplex using
23958eb6488eSEric Joyner  *   media/mediopt option with ifconfig.
23968eb6488eSEric Joyner  ************************************************************************/
23978eb6488eSEric Joyner static int
2398c19c7afeSEric Joyner ixgbe_if_media_change(if_ctx_t ctx)
23998eb6488eSEric Joyner {
2400b1d5caf3SKevin Bowling 	struct ixgbe_softc   *sc = iflib_get_softc(ctx);
2401c19c7afeSEric Joyner 	struct ifmedia   *ifm = iflib_get_media(ctx);
2402b1d5caf3SKevin Bowling 	struct ixgbe_hw  *hw = &sc->hw;
24038eb6488eSEric Joyner 	ixgbe_link_speed speed = 0;
24048eb6488eSEric Joyner 
2405c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_media_change: begin");
24068eb6488eSEric Joyner 
24078eb6488eSEric Joyner 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
24088eb6488eSEric Joyner 		return (EINVAL);
24098eb6488eSEric Joyner 
24108eb6488eSEric Joyner 	if (hw->phy.media_type == ixgbe_media_type_backplane)
2411c19c7afeSEric Joyner 		return (EPERM);
24128eb6488eSEric Joyner 
24138eb6488eSEric Joyner 	/*
24148eb6488eSEric Joyner 	 * We don't actually need to check against the supported
24158eb6488eSEric Joyner 	 * media types of the adapter; ifmedia will take care of
24168eb6488eSEric Joyner 	 * that for us.
24178eb6488eSEric Joyner 	 */
24188eb6488eSEric Joyner 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
24198eb6488eSEric Joyner 	case IFM_AUTO:
24208eb6488eSEric Joyner 	case IFM_10G_T:
24218eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
24228eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24238eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
24248eb6488eSEric Joyner 		break;
24258eb6488eSEric Joyner 	case IFM_10G_LRM:
24268eb6488eSEric Joyner 	case IFM_10G_LR:
24278eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
24288eb6488eSEric Joyner 	case IFM_10G_SR: /* KR, too */
24298eb6488eSEric Joyner 	case IFM_10G_CX4: /* KX4 */
24308eb6488eSEric Joyner #else
24318eb6488eSEric Joyner 	case IFM_10G_KR:
24328eb6488eSEric Joyner 	case IFM_10G_KX4:
24338eb6488eSEric Joyner #endif
24348eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24358eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
24368eb6488eSEric Joyner 		break;
24378eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
24388eb6488eSEric Joyner 	case IFM_1000_CX: /* KX */
24398eb6488eSEric Joyner #else
24408eb6488eSEric Joyner 	case IFM_1000_KX:
24418eb6488eSEric Joyner #endif
24428eb6488eSEric Joyner 	case IFM_1000_LX:
24438eb6488eSEric Joyner 	case IFM_1000_SX:
24448eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24458eb6488eSEric Joyner 		break;
24468eb6488eSEric Joyner 	case IFM_1000_T:
24478eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
24488eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24498eb6488eSEric Joyner 		break;
24508eb6488eSEric Joyner 	case IFM_10G_TWINAX:
24518eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
24528eb6488eSEric Joyner 		break;
2453*d381c807SPiotr Pietruszewski 	case IFM_5000_T:
2454*d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
2455*d381c807SPiotr Pietruszewski 		break;
2456*d381c807SPiotr Pietruszewski 	case IFM_2500_T:
2457*d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
2458*d381c807SPiotr Pietruszewski 		break;
24598eb6488eSEric Joyner 	case IFM_100_TX:
24608eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
24618eb6488eSEric Joyner 		break;
24628eb6488eSEric Joyner 	case IFM_10_T:
24638eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10_FULL;
24648eb6488eSEric Joyner 		break;
24658eb6488eSEric Joyner 	default:
24668eb6488eSEric Joyner 		goto invalid;
24678eb6488eSEric Joyner 	}
24688eb6488eSEric Joyner 
246979b36ec9SKevin Bowling 	hw->mac.autotry_restart = true;
247079b36ec9SKevin Bowling 	hw->mac.ops.setup_link(hw, speed, true);
2471b1d5caf3SKevin Bowling 	sc->advertise =
2472*d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_10GB_FULL)  ? 0x4  : 0) |
2473*d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_5GB_FULL)   ? 0x20 : 0) |
2474*d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
2475*d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_1GB_FULL)   ? 0x2  : 0) |
2476*d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_100_FULL)   ? 0x1  : 0) |
2477*d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_10_FULL)    ? 0x8  : 0);
24788eb6488eSEric Joyner 
24798eb6488eSEric Joyner 	return (0);
24808eb6488eSEric Joyner 
24818eb6488eSEric Joyner invalid:
2482c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "Invalid media type!\n");
24838eb6488eSEric Joyner 
24848eb6488eSEric Joyner 	return (EINVAL);
2485c19c7afeSEric Joyner } /* ixgbe_if_media_change */
24868eb6488eSEric Joyner 
24878eb6488eSEric Joyner /************************************************************************
24888eb6488eSEric Joyner  * ixgbe_set_promisc
24898eb6488eSEric Joyner  ************************************************************************/
2490c19c7afeSEric Joyner static int
2491c19c7afeSEric Joyner ixgbe_if_promisc_set(if_ctx_t ctx, int flags)
24928eb6488eSEric Joyner {
2493b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2494c19c7afeSEric Joyner 	struct ifnet   *ifp = iflib_get_ifp(ctx);
24958eb6488eSEric Joyner 	u32            rctl;
2496c19c7afeSEric Joyner 	int            mcnt = 0;
24978eb6488eSEric Joyner 
2498b1d5caf3SKevin Bowling 	rctl = IXGBE_READ_REG(&sc->hw, IXGBE_FCTRL);
24998eb6488eSEric Joyner 	rctl &= (~IXGBE_FCTRL_UPE);
25008eb6488eSEric Joyner 	if (ifp->if_flags & IFF_ALLMULTI)
25018eb6488eSEric Joyner 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
25028eb6488eSEric Joyner 	else {
2503ba76aa63SGleb Smirnoff 		mcnt = min(if_llmaddr_count(ifp), MAX_NUM_MULTICAST_ADDRESSES);
25048eb6488eSEric Joyner 	}
25058eb6488eSEric Joyner 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
25068eb6488eSEric Joyner 		rctl &= (~IXGBE_FCTRL_MPE);
2507b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
25088eb6488eSEric Joyner 
25098eb6488eSEric Joyner 	if (ifp->if_flags & IFF_PROMISC) {
25108eb6488eSEric Joyner 		rctl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
2511b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
25128eb6488eSEric Joyner 	} else if (ifp->if_flags & IFF_ALLMULTI) {
25138eb6488eSEric Joyner 		rctl |= IXGBE_FCTRL_MPE;
25148eb6488eSEric Joyner 		rctl &= ~IXGBE_FCTRL_UPE;
2515b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
25168eb6488eSEric Joyner 	}
2517c19c7afeSEric Joyner 	return (0);
2518c19c7afeSEric Joyner } /* ixgbe_if_promisc_set */
25198eb6488eSEric Joyner 
25208eb6488eSEric Joyner /************************************************************************
25218eb6488eSEric Joyner  * ixgbe_msix_link - Link status change ISR (MSI/MSI-X)
25228eb6488eSEric Joyner  ************************************************************************/
2523c19c7afeSEric Joyner static int
25248eb6488eSEric Joyner ixgbe_msix_link(void *arg)
25258eb6488eSEric Joyner {
2526b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = arg;
2527b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
25288eb6488eSEric Joyner 	u32             eicr, eicr_mask;
25298eb6488eSEric Joyner 	s32             retval;
25308eb6488eSEric Joyner 
2531b1d5caf3SKevin Bowling 	++sc->link_irq;
25328eb6488eSEric Joyner 
25338eb6488eSEric Joyner 	/* Pause other interrupts */
25348eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_OTHER);
25358eb6488eSEric Joyner 
25368eb6488eSEric Joyner 	/* First get the cause */
25378eb6488eSEric Joyner 	eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
25388eb6488eSEric Joyner 	/* Be sure the queue bits are not cleared */
25398eb6488eSEric Joyner 	eicr &= ~IXGBE_EICR_RTX_QUEUE;
25408eb6488eSEric Joyner 	/* Clear interrupt with write */
25418eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
25428eb6488eSEric Joyner 
25438eb6488eSEric Joyner 	/* Link status change */
25448eb6488eSEric Joyner 	if (eicr & IXGBE_EICR_LSC) {
25458eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
2546b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_LSC;
25478eb6488eSEric Joyner 	}
25488eb6488eSEric Joyner 
2549b1d5caf3SKevin Bowling 	if (sc->hw.mac.type != ixgbe_mac_82598EB) {
2550b1d5caf3SKevin Bowling 		if ((sc->feat_en & IXGBE_FEATURE_FDIR) &&
25518eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_FLOW_DIR)) {
25528eb6488eSEric Joyner 			/* This is probably overkill :) */
2553b1d5caf3SKevin Bowling 			if (!atomic_cmpset_int(&sc->fdir_reinit, 0, 1))
2554c19c7afeSEric Joyner 				return (FILTER_HANDLED);
25558eb6488eSEric Joyner 			/* Disable the interrupt */
2556c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_FLOW_DIR);
2557b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_FDIR;
2558c19c7afeSEric Joyner 		} else
25598eb6488eSEric Joyner 			if (eicr & IXGBE_EICR_ECC) {
2560b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2561c19c7afeSEric Joyner 				   "\nCRITICAL: ECC ERROR!! Please Reboot!!\n");
25628eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
25638eb6488eSEric Joyner 			}
25648eb6488eSEric Joyner 
25658eb6488eSEric Joyner 		/* Check for over temp condition */
2566b1d5caf3SKevin Bowling 		if (sc->feat_en & IXGBE_FEATURE_TEMP_SENSOR) {
2567b1d5caf3SKevin Bowling 			switch (sc->hw.mac.type) {
25688eb6488eSEric Joyner 			case ixgbe_mac_X550EM_a:
25698eb6488eSEric Joyner 				if (!(eicr & IXGBE_EICR_GPI_SDP0_X550EM_a))
25708eb6488eSEric Joyner 					break;
25718eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EIMC,
25728eb6488eSEric Joyner 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
25738eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR,
25748eb6488eSEric Joyner 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
25758eb6488eSEric Joyner 				retval = hw->phy.ops.check_overtemp(hw);
25768eb6488eSEric Joyner 				if (retval != IXGBE_ERR_OVERTEMP)
25778eb6488eSEric Joyner 					break;
2578b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2579c19c7afeSEric Joyner 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2580b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2581c19c7afeSEric Joyner 				    "System shutdown required!\n");
25828eb6488eSEric Joyner 				break;
25838eb6488eSEric Joyner 			default:
25848eb6488eSEric Joyner 				if (!(eicr & IXGBE_EICR_TS))
25858eb6488eSEric Joyner 					break;
25868eb6488eSEric Joyner 				retval = hw->phy.ops.check_overtemp(hw);
25878eb6488eSEric Joyner 				if (retval != IXGBE_ERR_OVERTEMP)
25888eb6488eSEric Joyner 					break;
2589b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2590c19c7afeSEric Joyner 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2591b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2592c19c7afeSEric Joyner 				    "System shutdown required!\n");
25938eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
25948eb6488eSEric Joyner 				break;
25958eb6488eSEric Joyner 			}
25968eb6488eSEric Joyner 		}
25978eb6488eSEric Joyner 
25988eb6488eSEric Joyner 		/* Check for VF message */
2599b1d5caf3SKevin Bowling 		if ((sc->feat_en & IXGBE_FEATURE_SRIOV) &&
26008eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_MAILBOX))
2601b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MBX;
26028eb6488eSEric Joyner 	}
26038eb6488eSEric Joyner 
26048eb6488eSEric Joyner 	if (ixgbe_is_sfp(hw)) {
26058eb6488eSEric Joyner 		/* Pluggable optics-related interrupt */
26068eb6488eSEric Joyner 		if (hw->mac.type >= ixgbe_mac_X540)
26078eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
26088eb6488eSEric Joyner 		else
26098eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
26108eb6488eSEric Joyner 
26118eb6488eSEric Joyner 		if (eicr & eicr_mask) {
26128eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
2613b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
26148eb6488eSEric Joyner 		}
26158eb6488eSEric Joyner 
26168eb6488eSEric Joyner 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
26178eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
26188eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
26198eb6488eSEric Joyner 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
2620b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
26218eb6488eSEric Joyner 		}
26228eb6488eSEric Joyner 	}
26238eb6488eSEric Joyner 
26248eb6488eSEric Joyner 	/* Check for fan failure */
2625b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL) {
2626b1d5caf3SKevin Bowling 		ixgbe_check_fan_failure(sc, eicr, true);
26278eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
26288eb6488eSEric Joyner 	}
26298eb6488eSEric Joyner 
26308eb6488eSEric Joyner 	/* External PHY interrupt */
26318eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
26328eb6488eSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP0_X540)) {
26338eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP0_X540);
2634b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_PHY;
26358eb6488eSEric Joyner 	}
26368eb6488eSEric Joyner 
2637b1d5caf3SKevin Bowling 	return (sc->task_requests != 0) ? FILTER_SCHEDULE_THREAD : FILTER_HANDLED;
26388eb6488eSEric Joyner } /* ixgbe_msix_link */
26398eb6488eSEric Joyner 
26408eb6488eSEric Joyner /************************************************************************
26418eb6488eSEric Joyner  * ixgbe_sysctl_interrupt_rate_handler
26428eb6488eSEric Joyner  ************************************************************************/
2643758cc3dcSJack F Vogel static int
2644758cc3dcSJack F Vogel ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)
2645758cc3dcSJack F Vogel {
2646c19c7afeSEric Joyner 	struct ix_rx_queue *que = ((struct ix_rx_queue *)oidp->oid_arg1);
26478eb6488eSEric Joyner 	int                error;
2648758cc3dcSJack F Vogel 	unsigned int       reg, usec, rate;
2649758cc3dcSJack F Vogel 
2650b1d5caf3SKevin Bowling 	reg = IXGBE_READ_REG(&que->sc->hw, IXGBE_EITR(que->msix));
2651758cc3dcSJack F Vogel 	usec = ((reg & 0x0FF8) >> 3);
2652758cc3dcSJack F Vogel 	if (usec > 0)
2653758cc3dcSJack F Vogel 		rate = 500000 / usec;
2654758cc3dcSJack F Vogel 	else
2655758cc3dcSJack F Vogel 		rate = 0;
2656758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &rate, 0, req);
2657758cc3dcSJack F Vogel 	if (error || !req->newptr)
2658758cc3dcSJack F Vogel 		return error;
2659758cc3dcSJack F Vogel 	reg &= ~0xfff; /* default, no limitation */
2660758cc3dcSJack F Vogel 	ixgbe_max_interrupt_rate = 0;
2661758cc3dcSJack F Vogel 	if (rate > 0 && rate < 500000) {
2662758cc3dcSJack F Vogel 		if (rate < 1000)
2663758cc3dcSJack F Vogel 			rate = 1000;
2664758cc3dcSJack F Vogel 		ixgbe_max_interrupt_rate = rate;
2665758cc3dcSJack F Vogel 		reg |= ((4000000/rate) & 0xff8);
2666758cc3dcSJack F Vogel 	}
2667b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&que->sc->hw, IXGBE_EITR(que->msix), reg);
2668758cc3dcSJack F Vogel 
26698eb6488eSEric Joyner 	return (0);
26708eb6488eSEric Joyner } /* ixgbe_sysctl_interrupt_rate_handler */
26718eb6488eSEric Joyner 
26728eb6488eSEric Joyner /************************************************************************
26738eb6488eSEric Joyner  * ixgbe_add_device_sysctls
26748eb6488eSEric Joyner  ************************************************************************/
26756f37f232SEric Joyner static void
2676c19c7afeSEric Joyner ixgbe_add_device_sysctls(if_ctx_t ctx)
26776f37f232SEric Joyner {
2678b1d5caf3SKevin Bowling 	struct ixgbe_softc         *sc = iflib_get_softc(ctx);
2679c19c7afeSEric Joyner 	device_t               dev = iflib_get_dev(ctx);
2680b1d5caf3SKevin Bowling 	struct ixgbe_hw        *hw = &sc->hw;
26816f37f232SEric Joyner 	struct sysctl_oid_list *child;
2682c19c7afeSEric Joyner 	struct sysctl_ctx_list *ctx_list;
26836f37f232SEric Joyner 
2684c19c7afeSEric Joyner 	ctx_list = device_get_sysctl_ctx(dev);
26856f37f232SEric Joyner 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
26866f37f232SEric Joyner 
26876f37f232SEric Joyner 	/* Sysctls for all devices */
2688c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fc",
268920b91f0aSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2690b1d5caf3SKevin Bowling 	    sc, 0, ixgbe_sysctl_flowcntl, "I",
2691c19c7afeSEric Joyner 	    IXGBE_SYSCTL_DESC_SET_FC);
26926f37f232SEric Joyner 
2693c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "advertise_speed",
269420b91f0aSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2695b1d5caf3SKevin Bowling 	    sc, 0, ixgbe_sysctl_advertise, "I",
26968eb6488eSEric Joyner 	    IXGBE_SYSCTL_DESC_ADV_SPEED);
26976f37f232SEric Joyner 
2698b1d5caf3SKevin Bowling 	sc->enable_aim = ixgbe_enable_aim;
269964881da4SSai Rajesh Tallamraju 	SYSCTL_ADD_INT(ctx_list, child, OID_AUTO, "enable_aim", CTLFLAG_RW,
2700b1d5caf3SKevin Bowling 	    &sc->enable_aim, 0, "Interrupt Moderation");
270164881da4SSai Rajesh Tallamraju 
27027660e4eaSKevin Bowling 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fw_version",
2703b1d5caf3SKevin Bowling 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
27047660e4eaSKevin Bowling 	    ixgbe_sysctl_print_fw_version, "A", "Prints FW/NVM Versions");
27057660e4eaSKevin Bowling 
2706a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
2707a9ca1c79SSean Bruno 	/* testing sysctls (for all devices) */
2708c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "power_state",
270920b91f0aSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2710b1d5caf3SKevin Bowling 	    sc, 0, ixgbe_sysctl_power_state,
27118eb6488eSEric Joyner 	    "I", "PCI Power State");
2712a9ca1c79SSean Bruno 
2713c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "print_rss_config",
2714b1d5caf3SKevin Bowling 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
2715a9ca1c79SSean Bruno 	    ixgbe_sysctl_print_rss_config, "A", "Prints RSS Configuration");
2716a9ca1c79SSean Bruno #endif
2717a9ca1c79SSean Bruno 	/* for X550 series devices */
27186f37f232SEric Joyner 	if (hw->mac.type >= ixgbe_mac_X550)
2719c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "dmac",
272020b91f0aSPawel Biernacki 		    CTLTYPE_U16 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2721b1d5caf3SKevin Bowling 		    sc, 0, ixgbe_sysctl_dmac,
27228eb6488eSEric Joyner 		    "I", "DMA Coalesce");
27236f37f232SEric Joyner 
2724a9ca1c79SSean Bruno 	/* for WoL-capable devices */
2725a9ca1c79SSean Bruno 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
2726c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wol_enable",
2727b1d5caf3SKevin Bowling 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
27288eb6488eSEric Joyner 		    ixgbe_sysctl_wol_enable, "I", "Enable/Disable Wake on LAN");
27296f37f232SEric Joyner 
2730c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wufc",
273120b91f0aSPawel Biernacki 		    CTLTYPE_U32 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2732b1d5caf3SKevin Bowling 		    sc, 0, ixgbe_sysctl_wufc,
27338eb6488eSEric Joyner 		    "I", "Enable/Disable Wake Up Filters");
27346f37f232SEric Joyner 	}
27356f37f232SEric Joyner 
2736a9ca1c79SSean Bruno 	/* for X552/X557-AT devices */
27376f37f232SEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
27386f37f232SEric Joyner 		struct sysctl_oid *phy_node;
27396f37f232SEric Joyner 		struct sysctl_oid_list *phy_list;
27406f37f232SEric Joyner 
2741c19c7afeSEric Joyner 		phy_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "phy",
274220b91f0aSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "External PHY sysctls");
27436f37f232SEric Joyner 		phy_list = SYSCTL_CHILDREN(phy_node);
27446f37f232SEric Joyner 
2745c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO, "temp",
274620b91f0aSPawel Biernacki 		    CTLTYPE_U16 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
2747b1d5caf3SKevin Bowling 		    sc, 0, ixgbe_sysctl_phy_temp,
27488eb6488eSEric Joyner 		    "I", "Current External PHY Temperature (Celsius)");
27496f37f232SEric Joyner 
2750c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO,
275120b91f0aSPawel Biernacki 		    "overtemp_occurred",
2752b1d5caf3SKevin Bowling 		    CTLTYPE_U16 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
27536f37f232SEric Joyner 		    ixgbe_sysctl_phy_overtemp_occurred, "I",
27546f37f232SEric Joyner 		    "External PHY High Temperature Event Occurred");
27556f37f232SEric Joyner 	}
27568eb6488eSEric Joyner 
2757b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_EEE) {
2758c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "eee_state",
2759b1d5caf3SKevin Bowling 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
27608eb6488eSEric Joyner 		    ixgbe_sysctl_eee_state, "I", "EEE Power Save State");
27618eb6488eSEric Joyner 	}
27628eb6488eSEric Joyner } /* ixgbe_add_device_sysctls */
27638eb6488eSEric Joyner 
27648eb6488eSEric Joyner /************************************************************************
27658eb6488eSEric Joyner  * ixgbe_allocate_pci_resources
27668eb6488eSEric Joyner  ************************************************************************/
27678eb6488eSEric Joyner static int
2768c19c7afeSEric Joyner ixgbe_allocate_pci_resources(if_ctx_t ctx)
27698eb6488eSEric Joyner {
2770b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2771c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
27728eb6488eSEric Joyner 	int             rid;
27738eb6488eSEric Joyner 
27748eb6488eSEric Joyner 	rid = PCIR_BAR(0);
2775b1d5caf3SKevin Bowling 	sc->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
27768eb6488eSEric Joyner 	    RF_ACTIVE);
27778eb6488eSEric Joyner 
2778b1d5caf3SKevin Bowling 	if (!(sc->pci_mem)) {
27798eb6488eSEric Joyner 		device_printf(dev, "Unable to allocate bus resource: memory\n");
27808eb6488eSEric Joyner 		return (ENXIO);
27818eb6488eSEric Joyner 	}
27828eb6488eSEric Joyner 
27838eb6488eSEric Joyner 	/* Save bus_space values for READ/WRITE_REG macros */
2784b1d5caf3SKevin Bowling 	sc->osdep.mem_bus_space_tag = rman_get_bustag(sc->pci_mem);
2785b1d5caf3SKevin Bowling 	sc->osdep.mem_bus_space_handle =
2786b1d5caf3SKevin Bowling 	    rman_get_bushandle(sc->pci_mem);
27878eb6488eSEric Joyner 	/* Set hw values for shared code */
2788b1d5caf3SKevin Bowling 	sc->hw.hw_addr = (u8 *)&sc->osdep.mem_bus_space_handle;
27898eb6488eSEric Joyner 
27908eb6488eSEric Joyner 	return (0);
27918eb6488eSEric Joyner } /* ixgbe_allocate_pci_resources */
27928eb6488eSEric Joyner 
27938eb6488eSEric Joyner /************************************************************************
27948eb6488eSEric Joyner  * ixgbe_detach - Device removal routine
27958eb6488eSEric Joyner  *
27968eb6488eSEric Joyner  *   Called when the driver is being removed.
27978eb6488eSEric Joyner  *   Stops the adapter and deallocates all the resources
27988eb6488eSEric Joyner  *   that were allocated for driver operation.
27998eb6488eSEric Joyner  *
28008eb6488eSEric Joyner  *   return 0 on success, positive on failure
28018eb6488eSEric Joyner  ************************************************************************/
28028eb6488eSEric Joyner static int
2803c19c7afeSEric Joyner ixgbe_if_detach(if_ctx_t ctx)
28048eb6488eSEric Joyner {
2805b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2806c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
28078eb6488eSEric Joyner 	u32            ctrl_ext;
28088eb6488eSEric Joyner 
28098eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_detach: begin");
28108eb6488eSEric Joyner 
28118eb6488eSEric Joyner 	if (ixgbe_pci_iov_detach(dev) != 0) {
28128eb6488eSEric Joyner 		device_printf(dev, "SR-IOV in use; detach first.\n");
28138eb6488eSEric Joyner 		return (EBUSY);
28148eb6488eSEric Joyner 	}
28158eb6488eSEric Joyner 
2816c19c7afeSEric Joyner 	ixgbe_setup_low_power_mode(ctx);
28178eb6488eSEric Joyner 
28188eb6488eSEric Joyner 	/* let hardware know driver is unloading */
2819b1d5caf3SKevin Bowling 	ctrl_ext = IXGBE_READ_REG(&sc->hw, IXGBE_CTRL_EXT);
28208eb6488eSEric Joyner 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
2821b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_CTRL_EXT, ctrl_ext);
28228eb6488eSEric Joyner 
2823c19c7afeSEric Joyner 	ixgbe_free_pci_resources(ctx);
2824b1d5caf3SKevin Bowling 	free(sc->mta, M_IXGBE);
28258eb6488eSEric Joyner 
28268eb6488eSEric Joyner 	return (0);
2827c19c7afeSEric Joyner } /* ixgbe_if_detach */
28288eb6488eSEric Joyner 
28298eb6488eSEric Joyner /************************************************************************
28308eb6488eSEric Joyner  * ixgbe_setup_low_power_mode - LPLU/WoL preparation
28318eb6488eSEric Joyner  *
28328eb6488eSEric Joyner  *   Prepare the adapter/port for LPLU and/or WoL
28338eb6488eSEric Joyner  ************************************************************************/
28348eb6488eSEric Joyner static int
2835c19c7afeSEric Joyner ixgbe_setup_low_power_mode(if_ctx_t ctx)
28368eb6488eSEric Joyner {
2837b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2838b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
2839c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
28408eb6488eSEric Joyner 	s32             error = 0;
28418eb6488eSEric Joyner 
2842c19c7afeSEric Joyner 	if (!hw->wol_enabled)
284379b36ec9SKevin Bowling 		ixgbe_set_phy_power(hw, false);
28448eb6488eSEric Joyner 
28458eb6488eSEric Joyner 	/* Limit power management flow to X550EM baseT */
28468eb6488eSEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T &&
28478eb6488eSEric Joyner 	    hw->phy.ops.enter_lplu) {
28488eb6488eSEric Joyner 		/* Turn off support for APM wakeup. (Using ACPI instead) */
28498eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_GRC,
28508eb6488eSEric Joyner 		    IXGBE_READ_REG(hw, IXGBE_GRC) & ~(u32)2);
28518eb6488eSEric Joyner 
28528eb6488eSEric Joyner 		/*
28538eb6488eSEric Joyner 		 * Clear Wake Up Status register to prevent any previous wakeup
28548eb6488eSEric Joyner 		 * events from waking us up immediately after we suspend.
28558eb6488eSEric Joyner 		 */
28568eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
28578eb6488eSEric Joyner 
28588eb6488eSEric Joyner 		/*
28598eb6488eSEric Joyner 		 * Program the Wakeup Filter Control register with user filter
28608eb6488eSEric Joyner 		 * settings
28618eb6488eSEric Joyner 		 */
2862b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, sc->wufc);
28638eb6488eSEric Joyner 
28648eb6488eSEric Joyner 		/* Enable wakeups and power management in Wakeup Control */
28658eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_WUC,
28668eb6488eSEric Joyner 		    IXGBE_WUC_WKEN | IXGBE_WUC_PME_EN);
28678eb6488eSEric Joyner 
28688eb6488eSEric Joyner 		/* X550EM baseT adapters need a special LPLU flow */
286979b36ec9SKevin Bowling 		hw->phy.reset_disable = true;
2870c19c7afeSEric Joyner 		ixgbe_if_stop(ctx);
28718eb6488eSEric Joyner 		error = hw->phy.ops.enter_lplu(hw);
28728eb6488eSEric Joyner 		if (error)
28738eb6488eSEric Joyner 			device_printf(dev, "Error entering LPLU: %d\n", error);
287479b36ec9SKevin Bowling 		hw->phy.reset_disable = false;
28758eb6488eSEric Joyner 	} else {
28768eb6488eSEric Joyner 		/* Just stop for other adapters */
2877c19c7afeSEric Joyner 		ixgbe_if_stop(ctx);
28788eb6488eSEric Joyner 	}
28798eb6488eSEric Joyner 
28808eb6488eSEric Joyner 	return error;
28818eb6488eSEric Joyner } /* ixgbe_setup_low_power_mode */
28828eb6488eSEric Joyner 
28838eb6488eSEric Joyner /************************************************************************
28848eb6488eSEric Joyner  * ixgbe_shutdown - Shutdown entry point
28858eb6488eSEric Joyner  ************************************************************************/
28868eb6488eSEric Joyner static int
2887c19c7afeSEric Joyner ixgbe_if_shutdown(if_ctx_t ctx)
28888eb6488eSEric Joyner {
28898eb6488eSEric Joyner 	int error = 0;
28908eb6488eSEric Joyner 
28918eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_shutdown: begin");
28928eb6488eSEric Joyner 
2893c19c7afeSEric Joyner 	error = ixgbe_setup_low_power_mode(ctx);
28948eb6488eSEric Joyner 
28958eb6488eSEric Joyner 	return (error);
2896c19c7afeSEric Joyner } /* ixgbe_if_shutdown */
28978eb6488eSEric Joyner 
28988eb6488eSEric Joyner /************************************************************************
28998eb6488eSEric Joyner  * ixgbe_suspend
29008eb6488eSEric Joyner  *
29018eb6488eSEric Joyner  *   From D0 to D3
29028eb6488eSEric Joyner  ************************************************************************/
29038eb6488eSEric Joyner static int
2904c19c7afeSEric Joyner ixgbe_if_suspend(if_ctx_t ctx)
29058eb6488eSEric Joyner {
29068eb6488eSEric Joyner 	int error = 0;
29078eb6488eSEric Joyner 
29088eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_suspend: begin");
29098eb6488eSEric Joyner 
2910c19c7afeSEric Joyner 	error = ixgbe_setup_low_power_mode(ctx);
29118eb6488eSEric Joyner 
29128eb6488eSEric Joyner 	return (error);
2913c19c7afeSEric Joyner } /* ixgbe_if_suspend */
29148eb6488eSEric Joyner 
29158eb6488eSEric Joyner /************************************************************************
29168eb6488eSEric Joyner  * ixgbe_resume
29178eb6488eSEric Joyner  *
29188eb6488eSEric Joyner  *   From D3 to D0
29198eb6488eSEric Joyner  ************************************************************************/
29208eb6488eSEric Joyner static int
2921c19c7afeSEric Joyner ixgbe_if_resume(if_ctx_t ctx)
29228eb6488eSEric Joyner {
2923b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2924c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
2925c19c7afeSEric Joyner 	struct ifnet    *ifp = iflib_get_ifp(ctx);
2926b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
29278eb6488eSEric Joyner 	u32             wus;
29288eb6488eSEric Joyner 
29298eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_resume: begin");
29308eb6488eSEric Joyner 
29318eb6488eSEric Joyner 	/* Read & clear WUS register */
29328eb6488eSEric Joyner 	wus = IXGBE_READ_REG(hw, IXGBE_WUS);
29338eb6488eSEric Joyner 	if (wus)
29348eb6488eSEric Joyner 		device_printf(dev, "Woken up by (WUS): %#010x\n",
29358eb6488eSEric Joyner 		    IXGBE_READ_REG(hw, IXGBE_WUS));
29368eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
29378eb6488eSEric Joyner 	/* And clear WUFC until next low-power transition */
29388eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
29398eb6488eSEric Joyner 
29408eb6488eSEric Joyner 	/*
29418eb6488eSEric Joyner 	 * Required after D3->D0 transition;
29428eb6488eSEric Joyner 	 * will re-advertise all previous advertised speeds
29438eb6488eSEric Joyner 	 */
29448eb6488eSEric Joyner 	if (ifp->if_flags & IFF_UP)
2945c19c7afeSEric Joyner 		ixgbe_if_init(ctx);
29468eb6488eSEric Joyner 
29478eb6488eSEric Joyner 	return (0);
2948c19c7afeSEric Joyner } /* ixgbe_if_resume */
29498eb6488eSEric Joyner 
29508eb6488eSEric Joyner /************************************************************************
2951c19c7afeSEric Joyner  * ixgbe_if_mtu_set - Ioctl mtu entry point
29528eb6488eSEric Joyner  *
2953c19c7afeSEric Joyner  *   Return 0 on success, EINVAL on failure
2954c19c7afeSEric Joyner  ************************************************************************/
2955c19c7afeSEric Joyner static int
2956c19c7afeSEric Joyner ixgbe_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
2957c19c7afeSEric Joyner {
2958b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2959c19c7afeSEric Joyner 	int error = 0;
2960c19c7afeSEric Joyner 
2961c19c7afeSEric Joyner 	IOCTL_DEBUGOUT("ioctl: SIOCIFMTU (Set Interface MTU)");
2962c19c7afeSEric Joyner 
2963c19c7afeSEric Joyner 	if (mtu > IXGBE_MAX_MTU) {
2964c19c7afeSEric Joyner 		error = EINVAL;
2965c19c7afeSEric Joyner 	} else {
2966b1d5caf3SKevin Bowling 		sc->max_frame_size = mtu + IXGBE_MTU_HDR;
2967c19c7afeSEric Joyner 	}
2968c19c7afeSEric Joyner 
2969c19c7afeSEric Joyner 	return error;
2970c19c7afeSEric Joyner } /* ixgbe_if_mtu_set */
2971c19c7afeSEric Joyner 
2972c19c7afeSEric Joyner /************************************************************************
2973c19c7afeSEric Joyner  * ixgbe_if_crcstrip_set
29748eb6488eSEric Joyner  ************************************************************************/
29758eb6488eSEric Joyner static void
2976c19c7afeSEric Joyner ixgbe_if_crcstrip_set(if_ctx_t ctx, int onoff, int crcstrip)
29778eb6488eSEric Joyner {
2978b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2979c19c7afeSEric Joyner 	struct ixgbe_hw *hw = &sc->hw;
2980c19c7afeSEric Joyner 	/* crc stripping is set in two places:
2981c19c7afeSEric Joyner 	 * IXGBE_HLREG0 (modified on init_locked and hw reset)
2982c19c7afeSEric Joyner 	 * IXGBE_RDRXCTL (set by the original driver in
2983c19c7afeSEric Joyner 	 *	ixgbe_setup_hw_rsc() called in init_locked.
2984c19c7afeSEric Joyner 	 *	We disable the setting when netmap is compiled in).
2985c19c7afeSEric Joyner 	 * We update the values here, but also in ixgbe.c because
2986c19c7afeSEric Joyner 	 * init_locked sometimes is called outside our control.
2987c19c7afeSEric Joyner 	 */
2988c19c7afeSEric Joyner 	uint32_t hl, rxc;
29898eb6488eSEric Joyner 
2990c19c7afeSEric Joyner 	hl = IXGBE_READ_REG(hw, IXGBE_HLREG0);
2991c19c7afeSEric Joyner 	rxc = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
2992c19c7afeSEric Joyner #ifdef NETMAP
2993c19c7afeSEric Joyner 	if (netmap_verbose)
2994c19c7afeSEric Joyner 		D("%s read  HLREG 0x%x rxc 0x%x",
2995c19c7afeSEric Joyner 			onoff ? "enter" : "exit", hl, rxc);
29968eb6488eSEric Joyner #endif
2997c19c7afeSEric Joyner 	/* hw requirements ... */
2998c19c7afeSEric Joyner 	rxc &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
2999c19c7afeSEric Joyner 	rxc |= IXGBE_RDRXCTL_RSCACKC;
3000c19c7afeSEric Joyner 	if (onoff && !crcstrip) {
3001c19c7afeSEric Joyner 		/* keep the crc. Fast rx */
3002c19c7afeSEric Joyner 		hl &= ~IXGBE_HLREG0_RXCRCSTRP;
3003c19c7afeSEric Joyner 		rxc &= ~IXGBE_RDRXCTL_CRCSTRIP;
3004c19c7afeSEric Joyner 	} else {
3005c19c7afeSEric Joyner 		/* reset default mode */
3006c19c7afeSEric Joyner 		hl |= IXGBE_HLREG0_RXCRCSTRP;
3007c19c7afeSEric Joyner 		rxc |= IXGBE_RDRXCTL_CRCSTRIP;
3008c19c7afeSEric Joyner 	}
3009c19c7afeSEric Joyner #ifdef NETMAP
3010c19c7afeSEric Joyner 	if (netmap_verbose)
3011c19c7afeSEric Joyner 		D("%s write HLREG 0x%x rxc 0x%x",
3012c19c7afeSEric Joyner 			onoff ? "enter" : "exit", hl, rxc);
3013c19c7afeSEric Joyner #endif
3014c19c7afeSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hl);
3015c19c7afeSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rxc);
3016c19c7afeSEric Joyner } /* ixgbe_if_crcstrip_set */
30178eb6488eSEric Joyner 
3018c19c7afeSEric Joyner /*********************************************************************
3019c19c7afeSEric Joyner  * ixgbe_if_init - Init entry point
30208eb6488eSEric Joyner  *
30218eb6488eSEric Joyner  *   Used in two ways: It is used by the stack as an init
30228eb6488eSEric Joyner  *   entry point in network interface structure. It is also
30238eb6488eSEric Joyner  *   used by the driver as a hw/sw initialization routine to
30248eb6488eSEric Joyner  *   get to a consistent state.
30258eb6488eSEric Joyner  *
3026c19c7afeSEric Joyner  *   Return 0 on success, positive on failure
3027c19c7afeSEric Joyner  **********************************************************************/
30288eb6488eSEric Joyner void
3029c19c7afeSEric Joyner ixgbe_if_init(if_ctx_t ctx)
30308eb6488eSEric Joyner {
3031b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3032c19c7afeSEric Joyner 	struct ifnet       *ifp = iflib_get_ifp(ctx);
3033c19c7afeSEric Joyner 	device_t           dev = iflib_get_dev(ctx);
3034b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
3035c19c7afeSEric Joyner 	struct ix_rx_queue *rx_que;
3036c19c7afeSEric Joyner 	struct ix_tx_queue *tx_que;
30378eb6488eSEric Joyner 	u32             txdctl, mhadd;
30388eb6488eSEric Joyner 	u32             rxdctl, rxctrl;
30398eb6488eSEric Joyner 	u32             ctrl_ext;
30408eb6488eSEric Joyner 
3041c19c7afeSEric Joyner 	int             i, j, err;
30428eb6488eSEric Joyner 
3043c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_init: begin");
30448eb6488eSEric Joyner 
30458eb6488eSEric Joyner 	/* Queue indices may change with IOV mode */
3046b1d5caf3SKevin Bowling 	ixgbe_align_all_queue_indices(sc);
30478eb6488eSEric Joyner 
30488eb6488eSEric Joyner 	/* reprogram the RAR[0] in case user changed it. */
3049b1d5caf3SKevin Bowling 	ixgbe_set_rar(hw, 0, hw->mac.addr, sc->pool, IXGBE_RAH_AV);
30508eb6488eSEric Joyner 
30518eb6488eSEric Joyner 	/* Get the latest mac address, User can use a LAA */
30528eb6488eSEric Joyner 	bcopy(IF_LLADDR(ifp), hw->mac.addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
3053b1d5caf3SKevin Bowling 	ixgbe_set_rar(hw, 0, hw->mac.addr, sc->pool, 1);
30548eb6488eSEric Joyner 	hw->addr_ctrl.rar_used_count = 1;
30558eb6488eSEric Joyner 
30568eb6488eSEric Joyner 	ixgbe_init_hw(hw);
3057c19c7afeSEric Joyner 
3058b1d5caf3SKevin Bowling 	ixgbe_initialize_iov(sc);
3059c19c7afeSEric Joyner 
3060c19c7afeSEric Joyner 	ixgbe_initialize_transmit_units(ctx);
30618eb6488eSEric Joyner 
30628eb6488eSEric Joyner 	/* Setup Multicast table */
3063c19c7afeSEric Joyner 	ixgbe_if_multi_set(ctx);
30648eb6488eSEric Joyner 
30658eb6488eSEric Joyner 	/* Determine the correct mbuf pool, based on frame size */
3066b1d5caf3SKevin Bowling 	sc->rx_mbuf_sz = iflib_get_rx_mbuf_sz(ctx);
30678eb6488eSEric Joyner 
30688eb6488eSEric Joyner 	/* Configure RX settings */
3069c19c7afeSEric Joyner 	ixgbe_initialize_receive_units(ctx);
30708eb6488eSEric Joyner 
3071b2c1e8e6SEric Joyner 	/*
3072b2c1e8e6SEric Joyner 	 * Initialize variable holding task enqueue requests
3073b2c1e8e6SEric Joyner 	 * from MSI-X interrupts
3074b2c1e8e6SEric Joyner 	 */
3075b1d5caf3SKevin Bowling 	sc->task_requests = 0;
3076b2c1e8e6SEric Joyner 
30778eb6488eSEric Joyner 	/* Enable SDP & MSI-X interrupts based on adapter */
3078b1d5caf3SKevin Bowling 	ixgbe_config_gpie(sc);
30798eb6488eSEric Joyner 
30808eb6488eSEric Joyner 	/* Set MTU size */
30818eb6488eSEric Joyner 	if (ifp->if_mtu > ETHERMTU) {
30828eb6488eSEric Joyner 		/* aka IXGBE_MAXFRS on 82599 and newer */
30838eb6488eSEric Joyner 		mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
30848eb6488eSEric Joyner 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
3085b1d5caf3SKevin Bowling 		mhadd |= sc->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
30868eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
30878eb6488eSEric Joyner 	}
30888eb6488eSEric Joyner 
30898eb6488eSEric Joyner 	/* Now enable all the queues */
3090b1d5caf3SKevin Bowling 	for (i = 0, tx_que = sc->tx_queues; i < sc->num_tx_queues; i++, tx_que++) {
3091c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
3092c19c7afeSEric Joyner 
30938eb6488eSEric Joyner 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(txr->me));
30948eb6488eSEric Joyner 		txdctl |= IXGBE_TXDCTL_ENABLE;
30958eb6488eSEric Joyner 		/* Set WTHRESH to 8, burst writeback */
30968eb6488eSEric Joyner 		txdctl |= (8 << 16);
30978eb6488eSEric Joyner 		/*
30988eb6488eSEric Joyner 		 * When the internal queue falls below PTHRESH (32),
30998eb6488eSEric Joyner 		 * start prefetching as long as there are at least
31008eb6488eSEric Joyner 		 * HTHRESH (1) buffers ready. The values are taken
31018eb6488eSEric Joyner 		 * from the Intel linux driver 3.8.21.
31028eb6488eSEric Joyner 		 * Prefetching enables tx line rate even with 1 queue.
31038eb6488eSEric Joyner 		 */
31048eb6488eSEric Joyner 		txdctl |= (32 << 0) | (1 << 8);
31058eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(txr->me), txdctl);
31068eb6488eSEric Joyner 	}
31078eb6488eSEric Joyner 
3108b1d5caf3SKevin Bowling 	for (i = 0, rx_que = sc->rx_queues; i < sc->num_rx_queues; i++, rx_que++) {
3109c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
3110c19c7afeSEric Joyner 
31118eb6488eSEric Joyner 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
31128eb6488eSEric Joyner 		if (hw->mac.type == ixgbe_mac_82598EB) {
31138eb6488eSEric Joyner 			/*
31148eb6488eSEric Joyner 			 * PTHRESH = 21
31158eb6488eSEric Joyner 			 * HTHRESH = 4
31168eb6488eSEric Joyner 			 * WTHRESH = 8
31178eb6488eSEric Joyner 			 */
31188eb6488eSEric Joyner 			rxdctl &= ~0x3FFFFF;
31198eb6488eSEric Joyner 			rxdctl |= 0x080420;
31208eb6488eSEric Joyner 		}
31218eb6488eSEric Joyner 		rxdctl |= IXGBE_RXDCTL_ENABLE;
31228eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), rxdctl);
3123c19c7afeSEric Joyner 		for (j = 0; j < 10; j++) {
31248eb6488eSEric Joyner 			if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me)) &
31258eb6488eSEric Joyner 			    IXGBE_RXDCTL_ENABLE)
31268eb6488eSEric Joyner 				break;
31278eb6488eSEric Joyner 			else
31288eb6488eSEric Joyner 				msec_delay(1);
31298eb6488eSEric Joyner 		}
31308eb6488eSEric Joyner 		wmb();
31318eb6488eSEric Joyner 	}
31328eb6488eSEric Joyner 
31338eb6488eSEric Joyner 	/* Enable Receive engine */
31348eb6488eSEric Joyner 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
31358eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
31368eb6488eSEric Joyner 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
31378eb6488eSEric Joyner 	rxctrl |= IXGBE_RXCTRL_RXEN;
31388eb6488eSEric Joyner 	ixgbe_enable_rx_dma(hw, rxctrl);
31398eb6488eSEric Joyner 
3140c19c7afeSEric Joyner 	/* Set up MSI/MSI-X routing */
3141c19c7afeSEric Joyner 	if (ixgbe_enable_msix)  {
3142b1d5caf3SKevin Bowling 		ixgbe_configure_ivars(sc);
31438eb6488eSEric Joyner 		/* Set up auto-mask */
31448eb6488eSEric Joyner 		if (hw->mac.type == ixgbe_mac_82598EB)
31458eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
31468eb6488eSEric Joyner 		else {
31478eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
31488eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
31498eb6488eSEric Joyner 		}
31508eb6488eSEric Joyner 	} else {  /* Simple settings for Legacy/MSI */
3151b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, 0, 0, 0);
3152b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, 0, 0, 1);
31538eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
31548eb6488eSEric Joyner 	}
31558eb6488eSEric Joyner 
3156b1d5caf3SKevin Bowling 	ixgbe_init_fdir(sc);
31578eb6488eSEric Joyner 
31588eb6488eSEric Joyner 	/*
31598eb6488eSEric Joyner 	 * Check on any SFP devices that
31608eb6488eSEric Joyner 	 * need to be kick-started
31618eb6488eSEric Joyner 	 */
31628eb6488eSEric Joyner 	if (hw->phy.type == ixgbe_phy_none) {
31638eb6488eSEric Joyner 		err = hw->phy.ops.identify(hw);
31648eb6488eSEric Joyner 		if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
31658eb6488eSEric Joyner 			device_printf(dev,
31668eb6488eSEric Joyner 			    "Unsupported SFP+ module type was detected.\n");
31678eb6488eSEric Joyner 			return;
31688eb6488eSEric Joyner 		}
31698eb6488eSEric Joyner 	}
31708eb6488eSEric Joyner 
31718eb6488eSEric Joyner 	/* Set moderation on the Link interrupt */
3172b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(hw, IXGBE_EITR(sc->vector), IXGBE_LINK_ITR);
31738eb6488eSEric Joyner 
3174c19c7afeSEric Joyner 	/* Enable power to the phy. */
317579b36ec9SKevin Bowling 	ixgbe_set_phy_power(hw, true);
3176c19c7afeSEric Joyner 
31778eb6488eSEric Joyner 	/* Config/Enable Link */
3178b2c1e8e6SEric Joyner 	ixgbe_config_link(ctx);
31798eb6488eSEric Joyner 
31808eb6488eSEric Joyner 	/* Hardware Packet Buffer & Flow Control setup */
3181b1d5caf3SKevin Bowling 	ixgbe_config_delay_values(sc);
31828eb6488eSEric Joyner 
31838eb6488eSEric Joyner 	/* Initialize the FC settings */
31848eb6488eSEric Joyner 	ixgbe_start_hw(hw);
31858eb6488eSEric Joyner 
31868eb6488eSEric Joyner 	/* Set up VLAN support and filter */
3187c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
31888eb6488eSEric Joyner 
31898eb6488eSEric Joyner 	/* Setup DMA Coalescing */
3190b1d5caf3SKevin Bowling 	ixgbe_config_dmac(sc);
31918eb6488eSEric Joyner 
31928eb6488eSEric Joyner 	/* And now turn on interrupts */
3193c19c7afeSEric Joyner 	ixgbe_if_enable_intr(ctx);
31948eb6488eSEric Joyner 
31958eb6488eSEric Joyner 	/* Enable the use of the MBX by the VF's */
3196b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_SRIOV) {
31978eb6488eSEric Joyner 		ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
31988eb6488eSEric Joyner 		ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
31998eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
32008eb6488eSEric Joyner 	}
32018eb6488eSEric Joyner 
32028eb6488eSEric Joyner } /* ixgbe_init_locked */
32038eb6488eSEric Joyner 
32048eb6488eSEric Joyner /************************************************************************
32058eb6488eSEric Joyner  * ixgbe_set_ivar
32068eb6488eSEric Joyner  *
32078eb6488eSEric Joyner  *   Setup the correct IVAR register for a particular MSI-X interrupt
32088eb6488eSEric Joyner  *     (yes this is all very magic and confusing :)
32098eb6488eSEric Joyner  *    - entry is the register array entry
32108eb6488eSEric Joyner  *    - vector is the MSI-X vector for this queue
32118eb6488eSEric Joyner  *    - type is RX/TX/MISC
32128eb6488eSEric Joyner  ************************************************************************/
32138eb6488eSEric Joyner static void
3214b1d5caf3SKevin Bowling ixgbe_set_ivar(struct ixgbe_softc *sc, u8 entry, u8 vector, s8 type)
32158eb6488eSEric Joyner {
3216b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
32178eb6488eSEric Joyner 	u32 ivar, index;
32188eb6488eSEric Joyner 
32198eb6488eSEric Joyner 	vector |= IXGBE_IVAR_ALLOC_VAL;
32208eb6488eSEric Joyner 
32218eb6488eSEric Joyner 	switch (hw->mac.type) {
32228eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
32238eb6488eSEric Joyner 		if (type == -1)
32248eb6488eSEric Joyner 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
32258eb6488eSEric Joyner 		else
32268eb6488eSEric Joyner 			entry += (type * 64);
32278eb6488eSEric Joyner 		index = (entry >> 2) & 0x1F;
32288eb6488eSEric Joyner 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
32298eb6488eSEric Joyner 		ivar &= ~(0xFF << (8 * (entry & 0x3)));
32308eb6488eSEric Joyner 		ivar |= (vector << (8 * (entry & 0x3)));
3231b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_IVAR(index), ivar);
32328eb6488eSEric Joyner 		break;
32338eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
32348eb6488eSEric Joyner 	case ixgbe_mac_X540:
32358eb6488eSEric Joyner 	case ixgbe_mac_X550:
32368eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
32378eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
32388eb6488eSEric Joyner 		if (type == -1) { /* MISC IVAR */
32398eb6488eSEric Joyner 			index = (entry & 1) * 8;
32408eb6488eSEric Joyner 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
32418eb6488eSEric Joyner 			ivar &= ~(0xFF << index);
32428eb6488eSEric Joyner 			ivar |= (vector << index);
32438eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
32448eb6488eSEric Joyner 		} else {          /* RX/TX IVARS */
32458eb6488eSEric Joyner 			index = (16 * (entry & 1)) + (8 * type);
32468eb6488eSEric Joyner 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
32478eb6488eSEric Joyner 			ivar &= ~(0xFF << index);
32488eb6488eSEric Joyner 			ivar |= (vector << index);
32498eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
32508eb6488eSEric Joyner 		}
32518eb6488eSEric Joyner 	default:
32528eb6488eSEric Joyner 		break;
32538eb6488eSEric Joyner 	}
32548eb6488eSEric Joyner } /* ixgbe_set_ivar */
32558eb6488eSEric Joyner 
32568eb6488eSEric Joyner /************************************************************************
32578eb6488eSEric Joyner  * ixgbe_configure_ivars
32588eb6488eSEric Joyner  ************************************************************************/
32598eb6488eSEric Joyner static void
3260b1d5caf3SKevin Bowling ixgbe_configure_ivars(struct ixgbe_softc *sc)
32618eb6488eSEric Joyner {
3262b1d5caf3SKevin Bowling 	struct ix_rx_queue *rx_que = sc->rx_queues;
3263b1d5caf3SKevin Bowling 	struct ix_tx_queue *tx_que = sc->tx_queues;
32648eb6488eSEric Joyner 	u32                newitr;
32658eb6488eSEric Joyner 
32668eb6488eSEric Joyner 	if (ixgbe_max_interrupt_rate > 0)
32678eb6488eSEric Joyner 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
32688eb6488eSEric Joyner 	else {
32698eb6488eSEric Joyner 		/*
32708eb6488eSEric Joyner 		 * Disable DMA coalescing if interrupt moderation is
32718eb6488eSEric Joyner 		 * disabled.
32728eb6488eSEric Joyner 		 */
3273b1d5caf3SKevin Bowling 		sc->dmac = 0;
32748eb6488eSEric Joyner 		newitr = 0;
32758eb6488eSEric Joyner 	}
32768eb6488eSEric Joyner 
3277b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, rx_que++) {
3278c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
32798eb6488eSEric Joyner 
3280c19c7afeSEric Joyner 		/* First the RX queue entry */
3281b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, rxr->me, rx_que->msix, 0);
3282c19c7afeSEric Joyner 
3283c19c7afeSEric Joyner 		/* Set an Initial EITR value */
3284b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EITR(rx_que->msix), newitr);
3285c19c7afeSEric Joyner 	}
3286b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_tx_queues; i++, tx_que++) {
3287c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
3288c19c7afeSEric Joyner 
3289c19c7afeSEric Joyner 		/* ... and the TX */
3290b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, txr->me, tx_que->msix, 1);
3291c19c7afeSEric Joyner 	}
32928eb6488eSEric Joyner 	/* For the Link interrupt */
3293b1d5caf3SKevin Bowling 	ixgbe_set_ivar(sc, 1, sc->vector, -1);
32948eb6488eSEric Joyner } /* ixgbe_configure_ivars */
32958eb6488eSEric Joyner 
32968eb6488eSEric Joyner /************************************************************************
32978eb6488eSEric Joyner  * ixgbe_config_gpie
32988eb6488eSEric Joyner  ************************************************************************/
32998eb6488eSEric Joyner static void
3300b1d5caf3SKevin Bowling ixgbe_config_gpie(struct ixgbe_softc *sc)
33018eb6488eSEric Joyner {
3302b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
33038eb6488eSEric Joyner 	u32             gpie;
33048eb6488eSEric Joyner 
33058eb6488eSEric Joyner 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
33068eb6488eSEric Joyner 
3307b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX) {
33088eb6488eSEric Joyner 		/* Enable Enhanced MSI-X mode */
33098eb6488eSEric Joyner 		gpie |= IXGBE_GPIE_MSIX_MODE
33108eb6488eSEric Joyner 		     |  IXGBE_GPIE_EIAME
33118eb6488eSEric Joyner 		     |  IXGBE_GPIE_PBA_SUPPORT
33128eb6488eSEric Joyner 		     |  IXGBE_GPIE_OCD;
33138eb6488eSEric Joyner 	}
33148eb6488eSEric Joyner 
33158eb6488eSEric Joyner 	/* Fan Failure Interrupt */
3316b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL)
33178eb6488eSEric Joyner 		gpie |= IXGBE_SDP1_GPIEN;
33188eb6488eSEric Joyner 
33198eb6488eSEric Joyner 	/* Thermal Sensor Interrupt */
3320b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_TEMP_SENSOR)
33218eb6488eSEric Joyner 		gpie |= IXGBE_SDP0_GPIEN_X540;
33228eb6488eSEric Joyner 
33238eb6488eSEric Joyner 	/* Link detection */
33248eb6488eSEric Joyner 	switch (hw->mac.type) {
33258eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
33268eb6488eSEric Joyner 		gpie |= IXGBE_SDP1_GPIEN | IXGBE_SDP2_GPIEN;
33278eb6488eSEric Joyner 		break;
33288eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
33298eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
33308eb6488eSEric Joyner 		gpie |= IXGBE_SDP0_GPIEN_X540;
33318eb6488eSEric Joyner 		break;
33328eb6488eSEric Joyner 	default:
33338eb6488eSEric Joyner 		break;
33348eb6488eSEric Joyner 	}
33358eb6488eSEric Joyner 
33368eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
33378eb6488eSEric Joyner 
33388eb6488eSEric Joyner } /* ixgbe_config_gpie */
33398eb6488eSEric Joyner 
33408eb6488eSEric Joyner /************************************************************************
33418eb6488eSEric Joyner  * ixgbe_config_delay_values
33428eb6488eSEric Joyner  *
3343b1d5caf3SKevin Bowling  *   Requires sc->max_frame_size to be set.
33448eb6488eSEric Joyner  ************************************************************************/
33458eb6488eSEric Joyner static void
3346b1d5caf3SKevin Bowling ixgbe_config_delay_values(struct ixgbe_softc *sc)
33478eb6488eSEric Joyner {
3348b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
33498eb6488eSEric Joyner 	u32             rxpb, frame, size, tmp;
33508eb6488eSEric Joyner 
3351b1d5caf3SKevin Bowling 	frame = sc->max_frame_size;
33528eb6488eSEric Joyner 
33538eb6488eSEric Joyner 	/* Calculate High Water */
33548eb6488eSEric Joyner 	switch (hw->mac.type) {
33558eb6488eSEric Joyner 	case ixgbe_mac_X540:
33568eb6488eSEric Joyner 	case ixgbe_mac_X550:
33578eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
33588eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
33598eb6488eSEric Joyner 		tmp = IXGBE_DV_X540(frame, frame);
33608eb6488eSEric Joyner 		break;
33618eb6488eSEric Joyner 	default:
33628eb6488eSEric Joyner 		tmp = IXGBE_DV(frame, frame);
33638eb6488eSEric Joyner 		break;
33648eb6488eSEric Joyner 	}
33658eb6488eSEric Joyner 	size = IXGBE_BT2KB(tmp);
33668eb6488eSEric Joyner 	rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
33678eb6488eSEric Joyner 	hw->fc.high_water[0] = rxpb - size;
33688eb6488eSEric Joyner 
33698eb6488eSEric Joyner 	/* Now calculate Low Water */
33708eb6488eSEric Joyner 	switch (hw->mac.type) {
33718eb6488eSEric Joyner 	case ixgbe_mac_X540:
33728eb6488eSEric Joyner 	case ixgbe_mac_X550:
33738eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
33748eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
33758eb6488eSEric Joyner 		tmp = IXGBE_LOW_DV_X540(frame);
33768eb6488eSEric Joyner 		break;
33778eb6488eSEric Joyner 	default:
33788eb6488eSEric Joyner 		tmp = IXGBE_LOW_DV(frame);
33798eb6488eSEric Joyner 		break;
33808eb6488eSEric Joyner 	}
33818eb6488eSEric Joyner 	hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
33828eb6488eSEric Joyner 
33838eb6488eSEric Joyner 	hw->fc.pause_time = IXGBE_FC_PAUSE;
338479b36ec9SKevin Bowling 	hw->fc.send_xon = true;
33858eb6488eSEric Joyner } /* ixgbe_config_delay_values */
33868eb6488eSEric Joyner 
33878eb6488eSEric Joyner /************************************************************************
33888eb6488eSEric Joyner  * ixgbe_set_multi - Multicast Update
33898eb6488eSEric Joyner  *
33908eb6488eSEric Joyner  *   Called whenever multicast address list is updated.
33918eb6488eSEric Joyner  ************************************************************************/
3392ba76aa63SGleb Smirnoff static u_int
339321afed4bSKevin Bowling ixgbe_mc_filter_apply(void *arg, struct sockaddr_dl *sdl, u_int idx)
33948eb6488eSEric Joyner {
3395b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = arg;
3396b1d5caf3SKevin Bowling 	struct ixgbe_mc_addr *mta = sc->mta;
3397c19c7afeSEric Joyner 
339821afed4bSKevin Bowling 	if (idx == MAX_NUM_MULTICAST_ADDRESSES)
3399c19c7afeSEric Joyner 		return (0);
340021afed4bSKevin Bowling 	bcopy(LLADDR(sdl), mta[idx].addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
3401b1d5caf3SKevin Bowling 	mta[idx].vmdq = sc->pool;
3402c19c7afeSEric Joyner 
3403c19c7afeSEric Joyner 	return (1);
3404c19c7afeSEric Joyner } /* ixgbe_mc_filter_apply */
3405c19c7afeSEric Joyner 
3406c19c7afeSEric Joyner static void
3407c19c7afeSEric Joyner ixgbe_if_multi_set(if_ctx_t ctx)
3408c19c7afeSEric Joyner {
3409b1d5caf3SKevin Bowling 	struct ixgbe_softc       *sc = iflib_get_softc(ctx);
34108eb6488eSEric Joyner 	struct ixgbe_mc_addr *mta;
3411c19c7afeSEric Joyner 	struct ifnet         *ifp = iflib_get_ifp(ctx);
34128eb6488eSEric Joyner 	u8                   *update_ptr;
34138eb6488eSEric Joyner 	u32                  fctrl;
3414ba76aa63SGleb Smirnoff 	u_int		     mcnt;
34158eb6488eSEric Joyner 
3416c19c7afeSEric Joyner 	IOCTL_DEBUGOUT("ixgbe_if_multi_set: begin");
34178eb6488eSEric Joyner 
3418b1d5caf3SKevin Bowling 	mta = sc->mta;
34198eb6488eSEric Joyner 	bzero(mta, sizeof(*mta) * MAX_NUM_MULTICAST_ADDRESSES);
34208eb6488eSEric Joyner 
3421b1d5caf3SKevin Bowling 	mcnt = if_foreach_llmaddr(iflib_get_ifp(ctx), ixgbe_mc_filter_apply, sc);
34228eb6488eSEric Joyner 
3423b1d5caf3SKevin Bowling 	fctrl = IXGBE_READ_REG(&sc->hw, IXGBE_FCTRL);
3424deecaa14SKevin Bowling 
34258eb6488eSEric Joyner 	if (ifp->if_flags & IFF_PROMISC)
34268eb6488eSEric Joyner 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
34278eb6488eSEric Joyner 	else if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES ||
34288eb6488eSEric Joyner 	    ifp->if_flags & IFF_ALLMULTI) {
34298eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_MPE;
34308eb6488eSEric Joyner 		fctrl &= ~IXGBE_FCTRL_UPE;
34318eb6488eSEric Joyner 	} else
34328eb6488eSEric Joyner 		fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
34338eb6488eSEric Joyner 
3434b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, fctrl);
34358eb6488eSEric Joyner 
34368eb6488eSEric Joyner 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES) {
34378eb6488eSEric Joyner 		update_ptr = (u8 *)mta;
3438b1d5caf3SKevin Bowling 		ixgbe_update_mc_addr_list(&sc->hw, update_ptr, mcnt,
343979b36ec9SKevin Bowling 		    ixgbe_mc_array_itr, true);
34408eb6488eSEric Joyner 	}
34418eb6488eSEric Joyner 
3442c19c7afeSEric Joyner } /* ixgbe_if_multi_set */
34438eb6488eSEric Joyner 
34448eb6488eSEric Joyner /************************************************************************
34458eb6488eSEric Joyner  * ixgbe_mc_array_itr
34468eb6488eSEric Joyner  *
34478eb6488eSEric Joyner  *   An iterator function needed by the multicast shared code.
34488eb6488eSEric Joyner  *   It feeds the shared code routine the addresses in the
34498eb6488eSEric Joyner  *   array of ixgbe_set_multi() one by one.
34508eb6488eSEric Joyner  ************************************************************************/
34518eb6488eSEric Joyner static u8 *
34528eb6488eSEric Joyner ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
34538eb6488eSEric Joyner {
34548eb6488eSEric Joyner 	struct ixgbe_mc_addr *mta;
34558eb6488eSEric Joyner 
34568eb6488eSEric Joyner 	mta = (struct ixgbe_mc_addr *)*update_ptr;
34578eb6488eSEric Joyner 	*vmdq = mta->vmdq;
34588eb6488eSEric Joyner 
34598eb6488eSEric Joyner 	*update_ptr = (u8*)(mta + 1);
34608eb6488eSEric Joyner 
34618eb6488eSEric Joyner 	return (mta->addr);
34628eb6488eSEric Joyner } /* ixgbe_mc_array_itr */
34638eb6488eSEric Joyner 
34648eb6488eSEric Joyner /************************************************************************
34658eb6488eSEric Joyner  * ixgbe_local_timer - Timer routine
34668eb6488eSEric Joyner  *
34678eb6488eSEric Joyner  *   Checks for link status, updates statistics,
34688eb6488eSEric Joyner  *   and runs the watchdog check.
34698eb6488eSEric Joyner  ************************************************************************/
34708eb6488eSEric Joyner static void
3471c19c7afeSEric Joyner ixgbe_if_timer(if_ctx_t ctx, uint16_t qid)
34728eb6488eSEric Joyner {
3473b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
34748eb6488eSEric Joyner 
3475c19c7afeSEric Joyner 	if (qid != 0)
3476c19c7afeSEric Joyner 		return;
34778eb6488eSEric Joyner 
34788eb6488eSEric Joyner 	/* Check for pluggable optics */
3479b1d5caf3SKevin Bowling 	if (sc->sfp_probe)
3480c19c7afeSEric Joyner 		if (!ixgbe_sfp_probe(ctx))
3481c19c7afeSEric Joyner 			return; /* Nothing to do */
34828eb6488eSEric Joyner 
3483b1d5caf3SKevin Bowling 	ixgbe_check_link(&sc->hw, &sc->link_speed, &sc->link_up, 0);
34848eb6488eSEric Joyner 
3485c19c7afeSEric Joyner 	/* Fire off the adminq task */
3486c19c7afeSEric Joyner 	iflib_admin_intr_deferred(ctx);
34878eb6488eSEric Joyner 
3488c19c7afeSEric Joyner } /* ixgbe_if_timer */
34898eb6488eSEric Joyner 
34908eb6488eSEric Joyner /************************************************************************
34918eb6488eSEric Joyner  * ixgbe_sfp_probe
34928eb6488eSEric Joyner  *
34938eb6488eSEric Joyner  *   Determine if a port had optics inserted.
34948eb6488eSEric Joyner  ************************************************************************/
34958eb6488eSEric Joyner static bool
3496c19c7afeSEric Joyner ixgbe_sfp_probe(if_ctx_t ctx)
34978eb6488eSEric Joyner {
3498b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3499b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
3500c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
350179b36ec9SKevin Bowling 	bool            result = false;
35028eb6488eSEric Joyner 
35038eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_nl) &&
35048eb6488eSEric Joyner 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
35058eb6488eSEric Joyner 		s32 ret = hw->phy.ops.identify_sfp(hw);
35068eb6488eSEric Joyner 		if (ret)
35078eb6488eSEric Joyner 			goto out;
35088eb6488eSEric Joyner 		ret = hw->phy.ops.reset(hw);
3509b1d5caf3SKevin Bowling 		sc->sfp_probe = false;
35108eb6488eSEric Joyner 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
35118eb6488eSEric Joyner 			device_printf(dev, "Unsupported SFP+ module detected!");
35128eb6488eSEric Joyner 			device_printf(dev,
35138eb6488eSEric Joyner 			    "Reload driver with supported module.\n");
35148eb6488eSEric Joyner 			goto out;
35158eb6488eSEric Joyner 		} else
35168eb6488eSEric Joyner 			device_printf(dev, "SFP+ module detected!\n");
35178eb6488eSEric Joyner 		/* We now have supported optics */
351879b36ec9SKevin Bowling 		result = true;
35198eb6488eSEric Joyner 	}
35208eb6488eSEric Joyner out:
35218eb6488eSEric Joyner 
35228eb6488eSEric Joyner 	return (result);
35238eb6488eSEric Joyner } /* ixgbe_sfp_probe */
35248eb6488eSEric Joyner 
35258eb6488eSEric Joyner /************************************************************************
35268eb6488eSEric Joyner  * ixgbe_handle_mod - Tasklet for SFP module interrupts
35278eb6488eSEric Joyner  ************************************************************************/
35288eb6488eSEric Joyner static void
3529c19c7afeSEric Joyner ixgbe_handle_mod(void *context)
35308eb6488eSEric Joyner {
3531c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3532b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3533b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
3534c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
35358eb6488eSEric Joyner 	u32             err, cage_full = 0;
35368eb6488eSEric Joyner 
3537b1d5caf3SKevin Bowling 	if (sc->hw.need_crosstalk_fix) {
35388eb6488eSEric Joyner 		switch (hw->mac.type) {
35398eb6488eSEric Joyner 		case ixgbe_mac_82599EB:
35408eb6488eSEric Joyner 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
35418eb6488eSEric Joyner 			    IXGBE_ESDP_SDP2;
35428eb6488eSEric Joyner 			break;
35438eb6488eSEric Joyner 		case ixgbe_mac_X550EM_x:
35448eb6488eSEric Joyner 		case ixgbe_mac_X550EM_a:
35458eb6488eSEric Joyner 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
35468eb6488eSEric Joyner 			    IXGBE_ESDP_SDP0;
35478eb6488eSEric Joyner 			break;
35488eb6488eSEric Joyner 		default:
35498eb6488eSEric Joyner 			break;
35508eb6488eSEric Joyner 		}
35518eb6488eSEric Joyner 
35528eb6488eSEric Joyner 		if (!cage_full)
3553c19c7afeSEric Joyner 			goto handle_mod_out;
35548eb6488eSEric Joyner 	}
35558eb6488eSEric Joyner 
35568eb6488eSEric Joyner 	err = hw->phy.ops.identify_sfp(hw);
35578eb6488eSEric Joyner 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
35588eb6488eSEric Joyner 		device_printf(dev,
35598eb6488eSEric Joyner 		    "Unsupported SFP+ module type was detected.\n");
3560c19c7afeSEric Joyner 		goto handle_mod_out;
35618eb6488eSEric Joyner 	}
35628eb6488eSEric Joyner 
3563c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
3564c19c7afeSEric Joyner 		err = hw->phy.ops.reset(hw);
3565c19c7afeSEric Joyner 	else
35668eb6488eSEric Joyner 		err = hw->mac.ops.setup_sfp(hw);
3567c19c7afeSEric Joyner 
35688eb6488eSEric Joyner 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
35698eb6488eSEric Joyner 		device_printf(dev,
35708eb6488eSEric Joyner 		    "Setup failure - unsupported SFP+ module type.\n");
3571c19c7afeSEric Joyner 		goto handle_mod_out;
35728eb6488eSEric Joyner 	}
3573b1d5caf3SKevin Bowling 	sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
3574c19c7afeSEric Joyner 	return;
3575c19c7afeSEric Joyner 
3576c19c7afeSEric Joyner handle_mod_out:
3577b1d5caf3SKevin Bowling 	sc->task_requests &= ~(IXGBE_REQUEST_TASK_MSF);
35788eb6488eSEric Joyner } /* ixgbe_handle_mod */
35798eb6488eSEric Joyner 
35808eb6488eSEric Joyner 
35818eb6488eSEric Joyner /************************************************************************
35828eb6488eSEric Joyner  * ixgbe_handle_msf - Tasklet for MSF (multispeed fiber) interrupts
35838eb6488eSEric Joyner  ************************************************************************/
35848eb6488eSEric Joyner static void
3585c19c7afeSEric Joyner ixgbe_handle_msf(void *context)
35868eb6488eSEric Joyner {
3587c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3588b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3589b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
35908eb6488eSEric Joyner 	u32             autoneg;
35918eb6488eSEric Joyner 	bool            negotiate;
35928eb6488eSEric Joyner 
35938eb6488eSEric Joyner 	/* get_supported_phy_layer will call hw->phy.ops.identify_sfp() */
3594b1d5caf3SKevin Bowling 	sc->phy_layer = ixgbe_get_supported_physical_layer(hw);
35958eb6488eSEric Joyner 
35968eb6488eSEric Joyner 	autoneg = hw->phy.autoneg_advertised;
35978eb6488eSEric Joyner 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
35988eb6488eSEric Joyner 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
35998eb6488eSEric Joyner 	if (hw->mac.ops.setup_link)
360079b36ec9SKevin Bowling 		hw->mac.ops.setup_link(hw, autoneg, true);
36018eb6488eSEric Joyner 
36028eb6488eSEric Joyner 	/* Adjust media types shown in ifconfig */
3603b1d5caf3SKevin Bowling 	ifmedia_removeall(sc->media);
3604b1d5caf3SKevin Bowling 	ixgbe_add_media_types(sc->ctx);
3605b1d5caf3SKevin Bowling 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
36068eb6488eSEric Joyner } /* ixgbe_handle_msf */
36078eb6488eSEric Joyner 
36088eb6488eSEric Joyner /************************************************************************
36098eb6488eSEric Joyner  * ixgbe_handle_phy - Tasklet for external PHY interrupts
36108eb6488eSEric Joyner  ************************************************************************/
36118eb6488eSEric Joyner static void
3612c19c7afeSEric Joyner ixgbe_handle_phy(void *context)
36138eb6488eSEric Joyner {
3614c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3615b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3616b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
36178eb6488eSEric Joyner 	int             error;
36188eb6488eSEric Joyner 
36198eb6488eSEric Joyner 	error = hw->phy.ops.handle_lasi(hw);
36208eb6488eSEric Joyner 	if (error == IXGBE_ERR_OVERTEMP)
3621b1d5caf3SKevin Bowling 		device_printf(sc->dev, "CRITICAL: EXTERNAL PHY OVER TEMP!!  PHY will downshift to lower power state!\n");
36228eb6488eSEric Joyner 	else if (error)
3623b1d5caf3SKevin Bowling 		device_printf(sc->dev,
36248eb6488eSEric Joyner 		    "Error handling LASI interrupt: %d\n", error);
36258eb6488eSEric Joyner } /* ixgbe_handle_phy */
36268eb6488eSEric Joyner 
36278eb6488eSEric Joyner /************************************************************************
3628c19c7afeSEric Joyner  * ixgbe_if_stop - Stop the hardware
36298eb6488eSEric Joyner  *
36308eb6488eSEric Joyner  *   Disables all traffic on the adapter by issuing a
36318eb6488eSEric Joyner  *   global reset on the MAC and deallocates TX/RX buffers.
36328eb6488eSEric Joyner  ************************************************************************/
36338eb6488eSEric Joyner static void
3634c19c7afeSEric Joyner ixgbe_if_stop(if_ctx_t ctx)
36358eb6488eSEric Joyner {
3636b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3637b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
36388eb6488eSEric Joyner 
3639c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_stop: begin\n");
36408eb6488eSEric Joyner 
36418eb6488eSEric Joyner 	ixgbe_reset_hw(hw);
364279b36ec9SKevin Bowling 	hw->adapter_stopped = false;
36438eb6488eSEric Joyner 	ixgbe_stop_adapter(hw);
36448eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82599EB)
36458eb6488eSEric Joyner 		ixgbe_stop_mac_link_on_d3_82599(hw);
36468eb6488eSEric Joyner 	/* Turn off the laser - noop with no optics */
36478eb6488eSEric Joyner 	ixgbe_disable_tx_laser(hw);
36488eb6488eSEric Joyner 
36498eb6488eSEric Joyner 	/* Update the stack */
3650b1d5caf3SKevin Bowling 	sc->link_up = false;
3651c19c7afeSEric Joyner 	ixgbe_if_update_admin_status(ctx);
36528eb6488eSEric Joyner 
36538eb6488eSEric Joyner 	/* reprogram the RAR[0] in case user changed it. */
3654b1d5caf3SKevin Bowling 	ixgbe_set_rar(&sc->hw, 0, sc->hw.mac.addr, 0, IXGBE_RAH_AV);
36558eb6488eSEric Joyner 
36568eb6488eSEric Joyner 	return;
3657c19c7afeSEric Joyner } /* ixgbe_if_stop */
36588eb6488eSEric Joyner 
36598eb6488eSEric Joyner /************************************************************************
36608eb6488eSEric Joyner  * ixgbe_update_link_status - Update OS on link state
36618eb6488eSEric Joyner  *
36628eb6488eSEric Joyner  * Note: Only updates the OS on the cached link state.
36638eb6488eSEric Joyner  *       The real check of the hardware only happens with
36648eb6488eSEric Joyner  *       a link interrupt.
36658eb6488eSEric Joyner  ************************************************************************/
36668eb6488eSEric Joyner static void
3667c19c7afeSEric Joyner ixgbe_if_update_admin_status(if_ctx_t ctx)
36688eb6488eSEric Joyner {
3669b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3670c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
36718eb6488eSEric Joyner 
3672b1d5caf3SKevin Bowling 	if (sc->link_up) {
3673b1d5caf3SKevin Bowling 		if (sc->link_active == false) {
36748eb6488eSEric Joyner 			if (bootverbose)
36758eb6488eSEric Joyner 				device_printf(dev, "Link is up %d Gbps %s \n",
3676b1d5caf3SKevin Bowling 				    ((sc->link_speed == 128) ? 10 : 1),
36778eb6488eSEric Joyner 				    "Full Duplex");
3678b1d5caf3SKevin Bowling 			sc->link_active = true;
36798eb6488eSEric Joyner 			/* Update any Flow Control changes */
3680b1d5caf3SKevin Bowling 			ixgbe_fc_enable(&sc->hw);
36818eb6488eSEric Joyner 			/* Update DMA coalescing config */
3682b1d5caf3SKevin Bowling 			ixgbe_config_dmac(sc);
3683c19c7afeSEric Joyner 			/* should actually be negotiated value */
3684c19c7afeSEric Joyner 			iflib_link_state_change(ctx, LINK_STATE_UP, IF_Gbps(10));
3685c19c7afeSEric Joyner 
3686b1d5caf3SKevin Bowling 			if (sc->feat_en & IXGBE_FEATURE_SRIOV)
3687b1d5caf3SKevin Bowling 				ixgbe_ping_all_vfs(sc);
36888eb6488eSEric Joyner 		}
36898eb6488eSEric Joyner 	} else { /* Link down */
3690b1d5caf3SKevin Bowling 		if (sc->link_active == true) {
36918eb6488eSEric Joyner 			if (bootverbose)
36928eb6488eSEric Joyner 				device_printf(dev, "Link is Down\n");
3693c19c7afeSEric Joyner 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
3694b1d5caf3SKevin Bowling 			sc->link_active = false;
3695b1d5caf3SKevin Bowling 			if (sc->feat_en & IXGBE_FEATURE_SRIOV)
3696b1d5caf3SKevin Bowling 				ixgbe_ping_all_vfs(sc);
36978eb6488eSEric Joyner 		}
36988eb6488eSEric Joyner 	}
36998eb6488eSEric Joyner 
3700b2c1e8e6SEric Joyner 	/* Handle task requests from msix_link() */
3701b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_MOD)
3702b2c1e8e6SEric Joyner 		ixgbe_handle_mod(ctx);
3703b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_MSF)
3704b2c1e8e6SEric Joyner 		ixgbe_handle_msf(ctx);
3705b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_MBX)
3706b2c1e8e6SEric Joyner 		ixgbe_handle_mbx(ctx);
3707b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_FDIR)
3708b2c1e8e6SEric Joyner 		ixgbe_reinit_fdir(ctx);
3709b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_PHY)
3710b2c1e8e6SEric Joyner 		ixgbe_handle_phy(ctx);
3711b1d5caf3SKevin Bowling 	sc->task_requests = 0;
3712c19c7afeSEric Joyner 
3713b1d5caf3SKevin Bowling 	ixgbe_update_stats_counters(sc);
3714c19c7afeSEric Joyner } /* ixgbe_if_update_admin_status */
37158eb6488eSEric Joyner 
37168eb6488eSEric Joyner /************************************************************************
37178eb6488eSEric Joyner  * ixgbe_config_dmac - Configure DMA Coalescing
37188eb6488eSEric Joyner  ************************************************************************/
37198eb6488eSEric Joyner static void
3720b1d5caf3SKevin Bowling ixgbe_config_dmac(struct ixgbe_softc *sc)
37218eb6488eSEric Joyner {
3722b1d5caf3SKevin Bowling 	struct ixgbe_hw          *hw = &sc->hw;
37238eb6488eSEric Joyner 	struct ixgbe_dmac_config *dcfg = &hw->mac.dmac_config;
37248eb6488eSEric Joyner 
37258eb6488eSEric Joyner 	if (hw->mac.type < ixgbe_mac_X550 || !hw->mac.ops.dmac_config)
37268eb6488eSEric Joyner 		return;
37278eb6488eSEric Joyner 
3728b1d5caf3SKevin Bowling 	if (dcfg->watchdog_timer ^ sc->dmac ||
3729b1d5caf3SKevin Bowling 	    dcfg->link_speed ^ sc->link_speed) {
3730b1d5caf3SKevin Bowling 		dcfg->watchdog_timer = sc->dmac;
373179b36ec9SKevin Bowling 		dcfg->fcoe_en = false;
3732b1d5caf3SKevin Bowling 		dcfg->link_speed = sc->link_speed;
37338eb6488eSEric Joyner 		dcfg->num_tcs = 1;
37348eb6488eSEric Joyner 
37358eb6488eSEric Joyner 		INIT_DEBUGOUT2("dmac settings: watchdog %d, link speed %d\n",
37368eb6488eSEric Joyner 		    dcfg->watchdog_timer, dcfg->link_speed);
37378eb6488eSEric Joyner 
37388eb6488eSEric Joyner 		hw->mac.ops.dmac_config(hw);
37398eb6488eSEric Joyner 	}
37408eb6488eSEric Joyner } /* ixgbe_config_dmac */
37418eb6488eSEric Joyner 
37428eb6488eSEric Joyner /************************************************************************
3743c19c7afeSEric Joyner  * ixgbe_if_enable_intr
37448eb6488eSEric Joyner  ************************************************************************/
3745c19c7afeSEric Joyner void
3746c19c7afeSEric Joyner ixgbe_if_enable_intr(if_ctx_t ctx)
37478eb6488eSEric Joyner {
3748b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3749b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
3750b1d5caf3SKevin Bowling 	struct ix_rx_queue *que = sc->rx_queues;
37518eb6488eSEric Joyner 	u32                mask, fwsm;
37528eb6488eSEric Joyner 
37538eb6488eSEric Joyner 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
37548eb6488eSEric Joyner 
3755b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
37568eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
37578eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
3758b1d5caf3SKevin Bowling 		/* Temperature sensor on some scs */
37598eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP0;
37608eb6488eSEric Joyner 		/* SFP+ (RX_LOS_N & MOD_ABS_N) */
37618eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP1;
37628eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP2;
37638eb6488eSEric Joyner 		break;
37648eb6488eSEric Joyner 	case ixgbe_mac_X540:
37658eb6488eSEric Joyner 		/* Detect if Thermal Sensor is enabled */
37668eb6488eSEric Joyner 		fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
37678eb6488eSEric Joyner 		if (fwsm & IXGBE_FWSM_TS_ENABLED)
37688eb6488eSEric Joyner 			mask |= IXGBE_EIMS_TS;
37698eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
37708eb6488eSEric Joyner 		break;
37718eb6488eSEric Joyner 	case ixgbe_mac_X550:
37728eb6488eSEric Joyner 		/* MAC thermal sensor is automatically enabled */
37738eb6488eSEric Joyner 		mask |= IXGBE_EIMS_TS;
37748eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
37758eb6488eSEric Joyner 		break;
37768eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
37778eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
37788eb6488eSEric Joyner 		/* Some devices use SDP0 for important information */
37798eb6488eSEric Joyner 		if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
37808eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP ||
37818eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP_N ||
37828eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
37838eb6488eSEric Joyner 			mask |= IXGBE_EIMS_GPI_SDP0_BY_MAC(hw);
37848eb6488eSEric Joyner 		if (hw->phy.type == ixgbe_phy_x550em_ext_t)
37858eb6488eSEric Joyner 			mask |= IXGBE_EICR_GPI_SDP0_X540;
37868eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
37878eb6488eSEric Joyner 		break;
37888eb6488eSEric Joyner 	default:
37898eb6488eSEric Joyner 		break;
37908eb6488eSEric Joyner 	}
37918eb6488eSEric Joyner 
37928eb6488eSEric Joyner 	/* Enable Fan Failure detection */
3793b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL)
37948eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP1;
37958eb6488eSEric Joyner 	/* Enable SR-IOV */
3796b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_SRIOV)
37978eb6488eSEric Joyner 		mask |= IXGBE_EIMS_MAILBOX;
37988eb6488eSEric Joyner 	/* Enable Flow Director */
3799b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FDIR)
38008eb6488eSEric Joyner 		mask |= IXGBE_EIMS_FLOW_DIR;
38018eb6488eSEric Joyner 
38028eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
38038eb6488eSEric Joyner 
38048eb6488eSEric Joyner 	/* With MSI-X we use auto clear */
3805b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX) {
38068eb6488eSEric Joyner 		mask = IXGBE_EIMS_ENABLE_MASK;
38078eb6488eSEric Joyner 		/* Don't autoclear Link */
38088eb6488eSEric Joyner 		mask &= ~IXGBE_EIMS_OTHER;
38098eb6488eSEric Joyner 		mask &= ~IXGBE_EIMS_LSC;
3810b1d5caf3SKevin Bowling 		if (sc->feat_cap & IXGBE_FEATURE_SRIOV)
38118eb6488eSEric Joyner 			mask &= ~IXGBE_EIMS_MAILBOX;
38128eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
38136f37f232SEric Joyner 	}
38146f37f232SEric Joyner 
3815758cc3dcSJack F Vogel 	/*
38168eb6488eSEric Joyner 	 * Now enable all queues, this is done separately to
38178eb6488eSEric Joyner 	 * allow for handling the extended (beyond 32) MSI-X
38188eb6488eSEric Joyner 	 * vectors that can be used by 82599
3819758cc3dcSJack F Vogel 	 */
3820b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, que++)
3821b1d5caf3SKevin Bowling 		ixgbe_enable_queue(sc, que->msix);
38228eb6488eSEric Joyner 
38238eb6488eSEric Joyner 	IXGBE_WRITE_FLUSH(hw);
38248eb6488eSEric Joyner 
3825c19c7afeSEric Joyner } /* ixgbe_if_enable_intr */
38268eb6488eSEric Joyner 
38278eb6488eSEric Joyner /************************************************************************
38288eb6488eSEric Joyner  * ixgbe_disable_intr
38298eb6488eSEric Joyner  ************************************************************************/
3830758cc3dcSJack F Vogel static void
3831c19c7afeSEric Joyner ixgbe_if_disable_intr(if_ctx_t ctx)
3832758cc3dcSJack F Vogel {
3833b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3834c19c7afeSEric Joyner 
3835b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX)
3836b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIAC, 0);
3837b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
3838b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC, ~0);
38398eb6488eSEric Joyner 	} else {
3840b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC, 0xFFFF0000);
3841b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC_EX(0), ~0);
3842b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC_EX(1), ~0);
38438eb6488eSEric Joyner 	}
3844b1d5caf3SKevin Bowling 	IXGBE_WRITE_FLUSH(&sc->hw);
3845758cc3dcSJack F Vogel 
3846c19c7afeSEric Joyner } /* ixgbe_if_disable_intr */
38478eb6488eSEric Joyner 
38488eb6488eSEric Joyner /************************************************************************
3849b2c1e8e6SEric Joyner  * ixgbe_link_intr_enable
3850b2c1e8e6SEric Joyner  ************************************************************************/
3851b2c1e8e6SEric Joyner static void
3852b2c1e8e6SEric Joyner ixgbe_link_intr_enable(if_ctx_t ctx)
3853b2c1e8e6SEric Joyner {
3854b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &((struct ixgbe_softc *)iflib_get_softc(ctx))->hw;
3855b2c1e8e6SEric Joyner 
3856b2c1e8e6SEric Joyner 	/* Re-enable other interrupts */
3857b2c1e8e6SEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC);
3858b2c1e8e6SEric Joyner } /* ixgbe_link_intr_enable */
3859b2c1e8e6SEric Joyner 
3860b2c1e8e6SEric Joyner /************************************************************************
3861c19c7afeSEric Joyner  * ixgbe_if_rx_queue_intr_enable
3862c19c7afeSEric Joyner  ************************************************************************/
3863c19c7afeSEric Joyner static int
3864c19c7afeSEric Joyner ixgbe_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
3865c19c7afeSEric Joyner {
3866b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3867b1d5caf3SKevin Bowling 	struct ix_rx_queue *que = &sc->rx_queues[rxqid];
3868c19c7afeSEric Joyner 
3869b1d5caf3SKevin Bowling 	ixgbe_enable_queue(sc, que->msix);
3870c19c7afeSEric Joyner 
3871c19c7afeSEric Joyner 	return (0);
3872c19c7afeSEric Joyner } /* ixgbe_if_rx_queue_intr_enable */
3873c19c7afeSEric Joyner 
3874c19c7afeSEric Joyner /************************************************************************
3875c19c7afeSEric Joyner  * ixgbe_enable_queue
38768eb6488eSEric Joyner  ************************************************************************/
38778eb6488eSEric Joyner static void
3878b1d5caf3SKevin Bowling ixgbe_enable_queue(struct ixgbe_softc *sc, u32 vector)
38798eb6488eSEric Joyner {
3880b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
38812dc2d580SEric Joyner 	u64             queue = 1ULL << vector;
3882c19c7afeSEric Joyner 	u32             mask;
3883758cc3dcSJack F Vogel 
3884c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB) {
3885c19c7afeSEric Joyner 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3886c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
3887c19c7afeSEric Joyner 	} else {
3888c19c7afeSEric Joyner 		mask = (queue & 0xFFFFFFFF);
3889c19c7afeSEric Joyner 		if (mask)
3890c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
3891c19c7afeSEric Joyner 		mask = (queue >> 32);
3892c19c7afeSEric Joyner 		if (mask)
3893c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
3894c19c7afeSEric Joyner 	}
3895c19c7afeSEric Joyner } /* ixgbe_enable_queue */
3896c19c7afeSEric Joyner 
3897c19c7afeSEric Joyner /************************************************************************
3898c19c7afeSEric Joyner  * ixgbe_disable_queue
3899c19c7afeSEric Joyner  ************************************************************************/
3900c19c7afeSEric Joyner static void
3901b1d5caf3SKevin Bowling ixgbe_disable_queue(struct ixgbe_softc *sc, u32 vector)
3902c19c7afeSEric Joyner {
3903b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
39042dc2d580SEric Joyner 	u64             queue = 1ULL << vector;
3905c19c7afeSEric Joyner 	u32             mask;
3906c19c7afeSEric Joyner 
3907c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB) {
3908c19c7afeSEric Joyner 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3909c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
3910c19c7afeSEric Joyner 	} else {
3911c19c7afeSEric Joyner 		mask = (queue & 0xFFFFFFFF);
3912c19c7afeSEric Joyner 		if (mask)
3913c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
3914c19c7afeSEric Joyner 		mask = (queue >> 32);
3915c19c7afeSEric Joyner 		if (mask)
3916c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
3917c19c7afeSEric Joyner 	}
3918c19c7afeSEric Joyner } /* ixgbe_disable_queue */
3919c19c7afeSEric Joyner 
3920c19c7afeSEric Joyner /************************************************************************
3921c19c7afeSEric Joyner  * ixgbe_intr - Legacy Interrupt Service Routine
3922c19c7afeSEric Joyner  ************************************************************************/
3923c19c7afeSEric Joyner int
3924c19c7afeSEric Joyner ixgbe_intr(void *arg)
3925c19c7afeSEric Joyner {
3926b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = arg;
3927b1d5caf3SKevin Bowling 	struct ix_rx_queue *que = sc->rx_queues;
3928b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
3929b1d5caf3SKevin Bowling 	if_ctx_t           ctx = sc->ctx;
3930c19c7afeSEric Joyner 	u32                eicr, eicr_mask;
3931758cc3dcSJack F Vogel 
39328eb6488eSEric Joyner 	eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
3933758cc3dcSJack F Vogel 
39348eb6488eSEric Joyner 	++que->irqs;
39358eb6488eSEric Joyner 	if (eicr == 0) {
3936c19c7afeSEric Joyner 		ixgbe_if_enable_intr(ctx);
3937c19c7afeSEric Joyner 		return (FILTER_HANDLED);
3938758cc3dcSJack F Vogel 	}
3939758cc3dcSJack F Vogel 
39408eb6488eSEric Joyner 	/* Check for fan failure */
3941c19c7afeSEric Joyner 	if ((hw->device_id == IXGBE_DEV_ID_82598AT) &&
3942c19c7afeSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP1)) {
3943b1d5caf3SKevin Bowling 		device_printf(sc->dev,
3944c19c7afeSEric Joyner 		    "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
39458eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3946758cc3dcSJack F Vogel 	}
3947758cc3dcSJack F Vogel 
39488eb6488eSEric Joyner 	/* Link status change */
3949c19c7afeSEric Joyner 	if (eicr & IXGBE_EICR_LSC) {
3950c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
3951c19c7afeSEric Joyner 		iflib_admin_intr_deferred(ctx);
3952c19c7afeSEric Joyner 	}
39538eb6488eSEric Joyner 
39548eb6488eSEric Joyner 	if (ixgbe_is_sfp(hw)) {
39558eb6488eSEric Joyner 		/* Pluggable optics-related interrupt */
39568eb6488eSEric Joyner 		if (hw->mac.type >= ixgbe_mac_X540)
39578eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
39588eb6488eSEric Joyner 		else
39598eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
39608eb6488eSEric Joyner 
39618eb6488eSEric Joyner 		if (eicr & eicr_mask) {
39628eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
3963b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
39648eb6488eSEric Joyner 		}
39658eb6488eSEric Joyner 
39668eb6488eSEric Joyner 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
39678eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
39688eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
39698eb6488eSEric Joyner 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3970b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
39718eb6488eSEric Joyner 		}
39728eb6488eSEric Joyner 	}
39738eb6488eSEric Joyner 
39748eb6488eSEric Joyner 	/* External PHY interrupt */
39758eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
39768eb6488eSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP0_X540))
3977b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_PHY;
39788eb6488eSEric Joyner 
3979c19c7afeSEric Joyner 	return (FILTER_SCHEDULE_THREAD);
3980c19c7afeSEric Joyner } /* ixgbe_intr */
39818eb6488eSEric Joyner 
39828eb6488eSEric Joyner /************************************************************************
39838eb6488eSEric Joyner  * ixgbe_free_pci_resources
39848eb6488eSEric Joyner  ************************************************************************/
39858eb6488eSEric Joyner static void
3986c19c7afeSEric Joyner ixgbe_free_pci_resources(if_ctx_t ctx)
39878eb6488eSEric Joyner {
3988b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3989b1d5caf3SKevin Bowling 	struct         ix_rx_queue *que = sc->rx_queues;
3990c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
39918eb6488eSEric Joyner 
3992b97de13aSMarius Strobl 	/* Release all MSI-X queue resources */
3993b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX)
3994b1d5caf3SKevin Bowling 		iflib_irq_free(ctx, &sc->irq);
3995c19c7afeSEric Joyner 
3996c19c7afeSEric Joyner 	if (que != NULL) {
3997b1d5caf3SKevin Bowling 		for (int i = 0; i < sc->num_rx_queues; i++, que++) {
3998c19c7afeSEric Joyner 			iflib_irq_free(ctx, &que->que_irq);
3999c19c7afeSEric Joyner 		}
4000c19c7afeSEric Joyner 	}
40018eb6488eSEric Joyner 
4002b1d5caf3SKevin Bowling 	if (sc->pci_mem != NULL)
4003c19c7afeSEric Joyner 		bus_release_resource(dev, SYS_RES_MEMORY,
4004b1d5caf3SKevin Bowling 		    rman_get_rid(sc->pci_mem), sc->pci_mem);
40058eb6488eSEric Joyner } /* ixgbe_free_pci_resources */
40068eb6488eSEric Joyner 
40078eb6488eSEric Joyner /************************************************************************
40088eb6488eSEric Joyner  * ixgbe_sysctl_flowcntl
40098eb6488eSEric Joyner  *
40108eb6488eSEric Joyner  *   SYSCTL wrapper around setting Flow Control
40118eb6488eSEric Joyner  ************************************************************************/
4012758cc3dcSJack F Vogel static int
4013f2c4db54SSteven Hartland ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS)
4014758cc3dcSJack F Vogel {
4015b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc;
40168eb6488eSEric Joyner 	int            error, fc;
4017758cc3dcSJack F Vogel 
4018b1d5caf3SKevin Bowling 	sc = (struct ixgbe_softc *)arg1;
4019b1d5caf3SKevin Bowling 	fc = sc->hw.fc.current_mode;
4020f2c4db54SSteven Hartland 
4021f2c4db54SSteven Hartland 	error = sysctl_handle_int(oidp, &fc, 0, req);
4022758cc3dcSJack F Vogel 	if ((error) || (req->newptr == NULL))
4023758cc3dcSJack F Vogel 		return (error);
4024758cc3dcSJack F Vogel 
4025758cc3dcSJack F Vogel 	/* Don't bother if it's not changed */
4026b1d5caf3SKevin Bowling 	if (fc == sc->hw.fc.current_mode)
4027758cc3dcSJack F Vogel 		return (0);
4028758cc3dcSJack F Vogel 
4029b1d5caf3SKevin Bowling 	return ixgbe_set_flowcntl(sc, fc);
40308eb6488eSEric Joyner } /* ixgbe_sysctl_flowcntl */
4031f2c4db54SSteven Hartland 
40328eb6488eSEric Joyner /************************************************************************
40338eb6488eSEric Joyner  * ixgbe_set_flowcntl - Set flow control
40348eb6488eSEric Joyner  *
40358eb6488eSEric Joyner  *   Flow control values:
40368eb6488eSEric Joyner  *     0 - off
40378eb6488eSEric Joyner  *     1 - rx pause
40388eb6488eSEric Joyner  *     2 - tx pause
40398eb6488eSEric Joyner  *     3 - full
40408eb6488eSEric Joyner  ************************************************************************/
4041f2c4db54SSteven Hartland static int
4042b1d5caf3SKevin Bowling ixgbe_set_flowcntl(struct ixgbe_softc *sc, int fc)
4043f2c4db54SSteven Hartland {
4044f2c4db54SSteven Hartland 	switch (fc) {
4045758cc3dcSJack F Vogel 	case ixgbe_fc_rx_pause:
4046758cc3dcSJack F Vogel 	case ixgbe_fc_tx_pause:
4047758cc3dcSJack F Vogel 	case ixgbe_fc_full:
4048b1d5caf3SKevin Bowling 		sc->hw.fc.requested_mode = fc;
4049b1d5caf3SKevin Bowling 		if (sc->num_rx_queues > 1)
4050b1d5caf3SKevin Bowling 			ixgbe_disable_rx_drop(sc);
4051758cc3dcSJack F Vogel 		break;
4052758cc3dcSJack F Vogel 	case ixgbe_fc_none:
4053b1d5caf3SKevin Bowling 		sc->hw.fc.requested_mode = ixgbe_fc_none;
4054b1d5caf3SKevin Bowling 		if (sc->num_rx_queues > 1)
4055b1d5caf3SKevin Bowling 			ixgbe_enable_rx_drop(sc);
4056758cc3dcSJack F Vogel 		break;
4057758cc3dcSJack F Vogel 	default:
4058758cc3dcSJack F Vogel 		return (EINVAL);
4059758cc3dcSJack F Vogel 	}
40608eb6488eSEric Joyner 
4061758cc3dcSJack F Vogel 	/* Don't autoneg if forcing a value */
4062b1d5caf3SKevin Bowling 	sc->hw.fc.disable_fc_autoneg = true;
4063b1d5caf3SKevin Bowling 	ixgbe_fc_enable(&sc->hw);
40648eb6488eSEric Joyner 
4065f2c4db54SSteven Hartland 	return (0);
40668eb6488eSEric Joyner } /* ixgbe_set_flowcntl */
40678eb6488eSEric Joyner 
40688eb6488eSEric Joyner /************************************************************************
40698eb6488eSEric Joyner  * ixgbe_enable_rx_drop
40708eb6488eSEric Joyner  *
40718eb6488eSEric Joyner  *   Enable the hardware to drop packets when the buffer is
40728eb6488eSEric Joyner  *   full. This is useful with multiqueue, so that no single
40738eb6488eSEric Joyner  *   queue being full stalls the entire RX engine. We only
40748eb6488eSEric Joyner  *   enable this when Multiqueue is enabled AND Flow Control
40758eb6488eSEric Joyner  *   is disabled.
40768eb6488eSEric Joyner  ************************************************************************/
40778eb6488eSEric Joyner static void
4078b1d5caf3SKevin Bowling ixgbe_enable_rx_drop(struct ixgbe_softc *sc)
40798eb6488eSEric Joyner {
4080b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
40818eb6488eSEric Joyner 	struct rx_ring  *rxr;
40828eb6488eSEric Joyner 	u32             srrctl;
40838eb6488eSEric Joyner 
4084b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++) {
4085b1d5caf3SKevin Bowling 		rxr = &sc->rx_queues[i].rxr;
40868eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
40878eb6488eSEric Joyner 		srrctl |= IXGBE_SRRCTL_DROP_EN;
40888eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
4089758cc3dcSJack F Vogel 	}
4090758cc3dcSJack F Vogel 
40918eb6488eSEric Joyner 	/* enable drop for each vf */
4092b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_vfs; i++) {
40938eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
40948eb6488eSEric Joyner 		                (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT) |
40958eb6488eSEric Joyner 		                IXGBE_QDE_ENABLE));
40968eb6488eSEric Joyner 	}
40978eb6488eSEric Joyner } /* ixgbe_enable_rx_drop */
40988eb6488eSEric Joyner 
40998eb6488eSEric Joyner /************************************************************************
41008eb6488eSEric Joyner  * ixgbe_disable_rx_drop
41018eb6488eSEric Joyner  ************************************************************************/
41028eb6488eSEric Joyner static void
4103b1d5caf3SKevin Bowling ixgbe_disable_rx_drop(struct ixgbe_softc *sc)
41048eb6488eSEric Joyner {
4105b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
41068eb6488eSEric Joyner 	struct rx_ring  *rxr;
41078eb6488eSEric Joyner 	u32             srrctl;
41088eb6488eSEric Joyner 
4109b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++) {
4110b1d5caf3SKevin Bowling 		rxr = &sc->rx_queues[i].rxr;
41118eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
41128eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_DROP_EN;
41138eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
41148eb6488eSEric Joyner 	}
41158eb6488eSEric Joyner 
41168eb6488eSEric Joyner 	/* disable drop for each vf */
4117b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_vfs; i++) {
41188eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
41198eb6488eSEric Joyner 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT)));
41208eb6488eSEric Joyner 	}
41218eb6488eSEric Joyner } /* ixgbe_disable_rx_drop */
41228eb6488eSEric Joyner 
41238eb6488eSEric Joyner /************************************************************************
41248eb6488eSEric Joyner  * ixgbe_sysctl_advertise
41258eb6488eSEric Joyner  *
41268eb6488eSEric Joyner  *   SYSCTL wrapper around setting advertised speed
41278eb6488eSEric Joyner  ************************************************************************/
4128758cc3dcSJack F Vogel static int
4129f2c4db54SSteven Hartland ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS)
4130758cc3dcSJack F Vogel {
4131b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc;
41328eb6488eSEric Joyner 	int            error, advertise;
4133758cc3dcSJack F Vogel 
4134b1d5caf3SKevin Bowling 	sc = (struct ixgbe_softc *)arg1;
4135b1d5caf3SKevin Bowling 	advertise = sc->advertise;
4136758cc3dcSJack F Vogel 
4137f2c4db54SSteven Hartland 	error = sysctl_handle_int(oidp, &advertise, 0, req);
4138758cc3dcSJack F Vogel 	if ((error) || (req->newptr == NULL))
4139758cc3dcSJack F Vogel 		return (error);
4140758cc3dcSJack F Vogel 
4141b1d5caf3SKevin Bowling 	return ixgbe_set_advertise(sc, advertise);
41428eb6488eSEric Joyner } /* ixgbe_sysctl_advertise */
4143f2c4db54SSteven Hartland 
41448eb6488eSEric Joyner /************************************************************************
41458eb6488eSEric Joyner  * ixgbe_set_advertise - Control advertised link speed
41468eb6488eSEric Joyner  *
41478eb6488eSEric Joyner  *   Flags:
41488eb6488eSEric Joyner  *     0x1  - advertise 100 Mb
41498eb6488eSEric Joyner  *     0x2  - advertise 1G
41508eb6488eSEric Joyner  *     0x4  - advertise 10G
41518eb6488eSEric Joyner  *     0x8  - advertise 10 Mb (yes, Mb)
4152*d381c807SPiotr Pietruszewski  *     0x10 - advertise 2.5G (disabled by default)
4153*d381c807SPiotr Pietruszewski  *     0x20 - advertise 5G (disabled by default)
4154*d381c807SPiotr Pietruszewski  *
41558eb6488eSEric Joyner  ************************************************************************/
4156f2c4db54SSteven Hartland static int
4157b1d5caf3SKevin Bowling ixgbe_set_advertise(struct ixgbe_softc *sc, int advertise)
4158f2c4db54SSteven Hartland {
4159b1d5caf3SKevin Bowling 	device_t         dev = iflib_get_dev(sc->ctx);
4160f2c4db54SSteven Hartland 	struct ixgbe_hw  *hw;
41618eb6488eSEric Joyner 	ixgbe_link_speed speed = 0;
41628eb6488eSEric Joyner 	ixgbe_link_speed link_caps = 0;
41638eb6488eSEric Joyner 	s32              err = IXGBE_NOT_IMPLEMENTED;
416479b36ec9SKevin Bowling 	bool             negotiate = false;
4165f2c4db54SSteven Hartland 
4166d775d23aSSteven Hartland 	/* Checks to validate new value */
4167b1d5caf3SKevin Bowling 	if (sc->advertise == advertise) /* no change */
4168d775d23aSSteven Hartland 		return (0);
4169d775d23aSSteven Hartland 
4170b1d5caf3SKevin Bowling 	hw = &sc->hw;
4171f2c4db54SSteven Hartland 
4172a9ca1c79SSean Bruno 	/* No speed changes for backplane media */
4173a9ca1c79SSean Bruno 	if (hw->phy.media_type == ixgbe_media_type_backplane)
4174a9ca1c79SSean Bruno 		return (ENODEV);
4175a9ca1c79SSean Bruno 
4176758cc3dcSJack F Vogel 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
4177758cc3dcSJack F Vogel 	      (hw->phy.multispeed_fiber))) {
41788eb6488eSEric Joyner 		device_printf(dev, "Advertised speed can only be set on copper or multispeed fiber media types.\n");
4179758cc3dcSJack F Vogel 		return (EINVAL);
4180758cc3dcSJack F Vogel 	}
4181758cc3dcSJack F Vogel 
4182*d381c807SPiotr Pietruszewski 	if (advertise < 0x1 || advertise > 0x3F) {
4183*d381c807SPiotr Pietruszewski 		device_printf(dev, "Invalid advertised speed; valid modes are 0x1 through 0x3F\n");
4184758cc3dcSJack F Vogel 		return (EINVAL);
4185758cc3dcSJack F Vogel 	}
4186758cc3dcSJack F Vogel 
41878eb6488eSEric Joyner 	if (hw->mac.ops.get_link_capabilities) {
41888eb6488eSEric Joyner 		err = hw->mac.ops.get_link_capabilities(hw, &link_caps,
41898eb6488eSEric Joyner 		    &negotiate);
41908eb6488eSEric Joyner 		if (err != IXGBE_SUCCESS) {
41918eb6488eSEric Joyner 			device_printf(dev, "Unable to determine supported advertise speeds\n");
41928eb6488eSEric Joyner 			return (ENODEV);
41938eb6488eSEric Joyner 		}
4194758cc3dcSJack F Vogel 	}
4195758cc3dcSJack F Vogel 
4196758cc3dcSJack F Vogel 	/* Set new value and report new advertised mode */
41978eb6488eSEric Joyner 	if (advertise & 0x1) {
41988eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_100_FULL)) {
41998eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 100Mb advertised speed\n");
42008eb6488eSEric Joyner 			return (EINVAL);
42018eb6488eSEric Joyner 		}
4202758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_100_FULL;
42038eb6488eSEric Joyner 	}
42048eb6488eSEric Joyner 	if (advertise & 0x2) {
42058eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_1GB_FULL)) {
42068eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 1Gb advertised speed\n");
42078eb6488eSEric Joyner 			return (EINVAL);
42088eb6488eSEric Joyner 		}
4209758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
42108eb6488eSEric Joyner 	}
42118eb6488eSEric Joyner 	if (advertise & 0x4) {
42128eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_10GB_FULL)) {
42138eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 10Gb advertised speed\n");
42148eb6488eSEric Joyner 			return (EINVAL);
42158eb6488eSEric Joyner 		}
4216758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
42178eb6488eSEric Joyner 	}
42188eb6488eSEric Joyner 	if (advertise & 0x8) {
42198eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_10_FULL)) {
42208eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 10Mb advertised speed\n");
42218eb6488eSEric Joyner 			return (EINVAL);
42228eb6488eSEric Joyner 		}
42238eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10_FULL;
42248eb6488eSEric Joyner 	}
4225*d381c807SPiotr Pietruszewski 	if (advertise & 0x10) {
4226*d381c807SPiotr Pietruszewski 		if (!(link_caps & IXGBE_LINK_SPEED_2_5GB_FULL)) {
4227*d381c807SPiotr Pietruszewski 			device_printf(dev, "Interface does not support 2.5G advertised speed\n");
4228*d381c807SPiotr Pietruszewski 			return (EINVAL);
4229*d381c807SPiotr Pietruszewski 		}
4230*d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
4231*d381c807SPiotr Pietruszewski 	}
4232*d381c807SPiotr Pietruszewski 	if (advertise & 0x20) {
4233*d381c807SPiotr Pietruszewski 		if (!(link_caps & IXGBE_LINK_SPEED_5GB_FULL)) {
4234*d381c807SPiotr Pietruszewski 			device_printf(dev, "Interface does not support 5G advertised speed\n");
4235*d381c807SPiotr Pietruszewski 			return (EINVAL);
4236*d381c807SPiotr Pietruszewski 		}
4237*d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
4238*d381c807SPiotr Pietruszewski 	}
4239758cc3dcSJack F Vogel 
424079b36ec9SKevin Bowling 	hw->mac.autotry_restart = true;
424179b36ec9SKevin Bowling 	hw->mac.ops.setup_link(hw, speed, true);
4242b1d5caf3SKevin Bowling 	sc->advertise = advertise;
4243758cc3dcSJack F Vogel 
4244f2c4db54SSteven Hartland 	return (0);
42458eb6488eSEric Joyner } /* ixgbe_set_advertise */
42468eb6488eSEric Joyner 
42478eb6488eSEric Joyner /************************************************************************
4248*d381c807SPiotr Pietruszewski  * ixgbe_get_default_advertise - Get default advertised speed settings
42498eb6488eSEric Joyner  *
42508eb6488eSEric Joyner  *   Formatted for sysctl usage.
42518eb6488eSEric Joyner  *   Flags:
42528eb6488eSEric Joyner  *     0x1 - advertise 100 Mb
42538eb6488eSEric Joyner  *     0x2 - advertise 1G
42548eb6488eSEric Joyner  *     0x4 - advertise 10G
42558eb6488eSEric Joyner  *     0x8 - advertise 10 Mb (yes, Mb)
4256*d381c807SPiotr Pietruszewski  *     0x10 - advertise 2.5G (disabled by default)
4257*d381c807SPiotr Pietruszewski  *     0x20 - advertise 5G (disabled by default)
42588eb6488eSEric Joyner  ************************************************************************/
42598eb6488eSEric Joyner static int
4260*d381c807SPiotr Pietruszewski ixgbe_get_default_advertise(struct ixgbe_softc *sc)
42618eb6488eSEric Joyner {
4262b1d5caf3SKevin Bowling 	struct ixgbe_hw  *hw = &sc->hw;
42638eb6488eSEric Joyner 	int              speed;
42648eb6488eSEric Joyner 	ixgbe_link_speed link_caps = 0;
42658eb6488eSEric Joyner 	s32              err;
426679b36ec9SKevin Bowling 	bool             negotiate = false;
4267758cc3dcSJack F Vogel 
4268758cc3dcSJack F Vogel 	/*
42698eb6488eSEric Joyner 	 * Advertised speed means nothing unless it's copper or
42708eb6488eSEric Joyner 	 * multi-speed fiber
4271758cc3dcSJack F Vogel 	 */
42728eb6488eSEric Joyner 	if (!(hw->phy.media_type == ixgbe_media_type_copper) &&
42738eb6488eSEric Joyner 	    !(hw->phy.multispeed_fiber))
4274758cc3dcSJack F Vogel 		return (0);
4275758cc3dcSJack F Vogel 
42768eb6488eSEric Joyner 	err = hw->mac.ops.get_link_capabilities(hw, &link_caps, &negotiate);
42778eb6488eSEric Joyner 	if (err != IXGBE_SUCCESS)
42788eb6488eSEric Joyner 		return (0);
42798eb6488eSEric Joyner 
4280*d381c807SPiotr Pietruszewski 	if (hw->mac.type == ixgbe_mac_X550) {
4281*d381c807SPiotr Pietruszewski 		/*
4282*d381c807SPiotr Pietruszewski 		 * 2.5G and 5G autonegotiation speeds on X550
4283*d381c807SPiotr Pietruszewski 		 * are disabled by default due to reported
4284*d381c807SPiotr Pietruszewski 		 * interoperability issues with some switches.
4285*d381c807SPiotr Pietruszewski 		 */
4286*d381c807SPiotr Pietruszewski 		link_caps &= ~(IXGBE_LINK_SPEED_2_5GB_FULL |
4287*d381c807SPiotr Pietruszewski 		    IXGBE_LINK_SPEED_5GB_FULL);
4288*d381c807SPiotr Pietruszewski 	}
4289*d381c807SPiotr Pietruszewski 
42908eb6488eSEric Joyner 	speed =
4291*d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_10GB_FULL)  ? 0x4  : 0) |
4292*d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_5GB_FULL)   ? 0x20 : 0) |
4293*d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
4294*d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_1GB_FULL)   ? 0x2  : 0) |
4295*d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_100_FULL)   ? 0x1  : 0) |
4296*d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_10_FULL)    ? 0x8  : 0);
42978eb6488eSEric Joyner 
42988eb6488eSEric Joyner 	return speed;
4299*d381c807SPiotr Pietruszewski } /* ixgbe_get_default_advertise */
43008eb6488eSEric Joyner 
43018eb6488eSEric Joyner /************************************************************************
43028eb6488eSEric Joyner  * ixgbe_sysctl_dmac - Manage DMA Coalescing
43038eb6488eSEric Joyner  *
43048eb6488eSEric Joyner  *   Control values:
43058eb6488eSEric Joyner  *     0/1 - off / on (use default value of 1000)
43068eb6488eSEric Joyner  *
43078eb6488eSEric Joyner  *     Legal timer values are:
43088eb6488eSEric Joyner  *     50,100,250,500,1000,2000,5000,10000
43098eb6488eSEric Joyner  *
43108eb6488eSEric Joyner  *     Turning off interrupt moderation will also turn this off.
43118eb6488eSEric Joyner  ************************************************************************/
43126f37f232SEric Joyner static int
43136f37f232SEric Joyner ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS)
43146f37f232SEric Joyner {
4315b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4316b1d5caf3SKevin Bowling 	struct ifnet   *ifp = iflib_get_ifp(sc->ctx);
43176f37f232SEric Joyner 	int            error;
4318c19c7afeSEric Joyner 	u16            newval;
43196f37f232SEric Joyner 
4320b1d5caf3SKevin Bowling 	newval = sc->dmac;
4321c19c7afeSEric Joyner 	error = sysctl_handle_16(oidp, &newval, 0, req);
43226f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
43236f37f232SEric Joyner 		return (error);
43246f37f232SEric Joyner 
4325a9ca1c79SSean Bruno 	switch (newval) {
43266f37f232SEric Joyner 	case 0:
43276f37f232SEric Joyner 		/* Disabled */
4328b1d5caf3SKevin Bowling 		sc->dmac = 0;
43296f37f232SEric Joyner 		break;
4330a9ca1c79SSean Bruno 	case 1:
4331a9ca1c79SSean Bruno 		/* Enable and use default */
4332b1d5caf3SKevin Bowling 		sc->dmac = 1000;
43336f37f232SEric Joyner 		break;
43346f37f232SEric Joyner 	case 50:
43356f37f232SEric Joyner 	case 100:
43366f37f232SEric Joyner 	case 250:
43376f37f232SEric Joyner 	case 500:
43386f37f232SEric Joyner 	case 1000:
43396f37f232SEric Joyner 	case 2000:
43406f37f232SEric Joyner 	case 5000:
43416f37f232SEric Joyner 	case 10000:
43426f37f232SEric Joyner 		/* Legal values - allow */
4343b1d5caf3SKevin Bowling 		sc->dmac = newval;
43446f37f232SEric Joyner 		break;
43456f37f232SEric Joyner 	default:
43466f37f232SEric Joyner 		/* Do nothing, illegal value */
43476f37f232SEric Joyner 		return (EINVAL);
43486f37f232SEric Joyner 	}
43496f37f232SEric Joyner 
43506f37f232SEric Joyner 	/* Re-initialize hardware if it's already running */
43516f37f232SEric Joyner 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4352c19c7afeSEric Joyner 		ifp->if_init(ifp);
43536f37f232SEric Joyner 
43546f37f232SEric Joyner 	return (0);
43558eb6488eSEric Joyner } /* ixgbe_sysctl_dmac */
43566f37f232SEric Joyner 
4357a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
43588eb6488eSEric Joyner /************************************************************************
43598eb6488eSEric Joyner  * ixgbe_sysctl_power_state
43608eb6488eSEric Joyner  *
4361a9ca1c79SSean Bruno  *   Sysctl to test power states
4362a9ca1c79SSean Bruno  *   Values:
4363a9ca1c79SSean Bruno  *     0      - set device to D0
4364a9ca1c79SSean Bruno  *     3      - set device to D3
4365a9ca1c79SSean Bruno  *     (none) - get current device power state
43668eb6488eSEric Joyner  ************************************************************************/
4367a9ca1c79SSean Bruno static int
4368a9ca1c79SSean Bruno ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS)
4369a9ca1c79SSean Bruno {
4370b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4371b1d5caf3SKevin Bowling 	device_t       dev = sc->dev;
4372a9ca1c79SSean Bruno 	int            curr_ps, new_ps, error = 0;
4373a9ca1c79SSean Bruno 
4374a9ca1c79SSean Bruno 	curr_ps = new_ps = pci_get_powerstate(dev);
4375a9ca1c79SSean Bruno 
4376a9ca1c79SSean Bruno 	error = sysctl_handle_int(oidp, &new_ps, 0, req);
4377a9ca1c79SSean Bruno 	if ((error) || (req->newptr == NULL))
4378a9ca1c79SSean Bruno 		return (error);
4379a9ca1c79SSean Bruno 
4380a9ca1c79SSean Bruno 	if (new_ps == curr_ps)
4381a9ca1c79SSean Bruno 		return (0);
4382a9ca1c79SSean Bruno 
4383a9ca1c79SSean Bruno 	if (new_ps == 3 && curr_ps == 0)
4384a9ca1c79SSean Bruno 		error = DEVICE_SUSPEND(dev);
4385a9ca1c79SSean Bruno 	else if (new_ps == 0 && curr_ps == 3)
4386a9ca1c79SSean Bruno 		error = DEVICE_RESUME(dev);
4387a9ca1c79SSean Bruno 	else
4388a9ca1c79SSean Bruno 		return (EINVAL);
4389a9ca1c79SSean Bruno 
4390a9ca1c79SSean Bruno 	device_printf(dev, "New state: %d\n", pci_get_powerstate(dev));
4391a9ca1c79SSean Bruno 
4392a9ca1c79SSean Bruno 	return (error);
43938eb6488eSEric Joyner } /* ixgbe_sysctl_power_state */
4394a9ca1c79SSean Bruno #endif
43958eb6488eSEric Joyner 
43968eb6488eSEric Joyner /************************************************************************
43978eb6488eSEric Joyner  * ixgbe_sysctl_wol_enable
43988eb6488eSEric Joyner  *
43998eb6488eSEric Joyner  *   Sysctl to enable/disable the WoL capability,
44008eb6488eSEric Joyner  *   if supported by the adapter.
44018eb6488eSEric Joyner  *
44026f37f232SEric Joyner  *   Values:
44036f37f232SEric Joyner  *     0 - disabled
44046f37f232SEric Joyner  *     1 - enabled
44058eb6488eSEric Joyner  ************************************************************************/
44066f37f232SEric Joyner static int
44076f37f232SEric Joyner ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS)
44086f37f232SEric Joyner {
4409b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4410b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
44116f37f232SEric Joyner 	int             new_wol_enabled;
44126f37f232SEric Joyner 	int             error = 0;
44136f37f232SEric Joyner 
44146f37f232SEric Joyner 	new_wol_enabled = hw->wol_enabled;
44156f37f232SEric Joyner 	error = sysctl_handle_int(oidp, &new_wol_enabled, 0, req);
44166f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
44176f37f232SEric Joyner 		return (error);
4418a9ca1c79SSean Bruno 	new_wol_enabled = !!(new_wol_enabled);
44196f37f232SEric Joyner 	if (new_wol_enabled == hw->wol_enabled)
44206f37f232SEric Joyner 		return (0);
44216f37f232SEric Joyner 
4422b1d5caf3SKevin Bowling 	if (new_wol_enabled > 0 && !sc->wol_support)
44236f37f232SEric Joyner 		return (ENODEV);
44246f37f232SEric Joyner 	else
4425a9ca1c79SSean Bruno 		hw->wol_enabled = new_wol_enabled;
44266f37f232SEric Joyner 
44276f37f232SEric Joyner 	return (0);
44288eb6488eSEric Joyner } /* ixgbe_sysctl_wol_enable */
44296f37f232SEric Joyner 
44308eb6488eSEric Joyner /************************************************************************
44318eb6488eSEric Joyner  * ixgbe_sysctl_wufc - Wake Up Filter Control
44328eb6488eSEric Joyner  *
44336f37f232SEric Joyner  *   Sysctl to enable/disable the types of packets that the
44346f37f232SEric Joyner  *   adapter will wake up on upon receipt.
44356f37f232SEric Joyner  *   Flags:
44366f37f232SEric Joyner  *     0x1  - Link Status Change
44376f37f232SEric Joyner  *     0x2  - Magic Packet
44386f37f232SEric Joyner  *     0x4  - Direct Exact
44396f37f232SEric Joyner  *     0x8  - Directed Multicast
44406f37f232SEric Joyner  *     0x10 - Broadcast
44416f37f232SEric Joyner  *     0x20 - ARP/IPv4 Request Packet
44426f37f232SEric Joyner  *     0x40 - Direct IPv4 Packet
44436f37f232SEric Joyner  *     0x80 - Direct IPv6 Packet
44446f37f232SEric Joyner  *
44458eb6488eSEric Joyner  *   Settings not listed above will cause the sysctl to return an error.
44468eb6488eSEric Joyner  ************************************************************************/
44476f37f232SEric Joyner static int
44486f37f232SEric Joyner ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS)
44496f37f232SEric Joyner {
4450b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
44516f37f232SEric Joyner 	int            error = 0;
44526f37f232SEric Joyner 	u32            new_wufc;
44536f37f232SEric Joyner 
4454b1d5caf3SKevin Bowling 	new_wufc = sc->wufc;
44556f37f232SEric Joyner 
4456c19c7afeSEric Joyner 	error = sysctl_handle_32(oidp, &new_wufc, 0, req);
44576f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
44586f37f232SEric Joyner 		return (error);
4459b1d5caf3SKevin Bowling 	if (new_wufc == sc->wufc)
44606f37f232SEric Joyner 		return (0);
44616f37f232SEric Joyner 
44626f37f232SEric Joyner 	if (new_wufc & 0xffffff00)
44636f37f232SEric Joyner 		return (EINVAL);
44648eb6488eSEric Joyner 
44656f37f232SEric Joyner 	new_wufc &= 0xff;
4466b1d5caf3SKevin Bowling 	new_wufc |= (0xffffff & sc->wufc);
4467b1d5caf3SKevin Bowling 	sc->wufc = new_wufc;
44686f37f232SEric Joyner 
44696f37f232SEric Joyner 	return (0);
44708eb6488eSEric Joyner } /* ixgbe_sysctl_wufc */
44716f37f232SEric Joyner 
4472a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
44738eb6488eSEric Joyner /************************************************************************
44748eb6488eSEric Joyner  * ixgbe_sysctl_print_rss_config
44758eb6488eSEric Joyner  ************************************************************************/
4476a9ca1c79SSean Bruno static int
4477a9ca1c79SSean Bruno ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS)
4478a9ca1c79SSean Bruno {
4479b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4480b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
4481b1d5caf3SKevin Bowling 	device_t        dev = sc->dev;
4482a9ca1c79SSean Bruno 	struct sbuf     *buf;
44838eb6488eSEric Joyner 	int             error = 0, reta_size;
4484a9ca1c79SSean Bruno 	u32             reg;
4485a9ca1c79SSean Bruno 
4486a9ca1c79SSean Bruno 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
4487a9ca1c79SSean Bruno 	if (!buf) {
4488a9ca1c79SSean Bruno 		device_printf(dev, "Could not allocate sbuf for output.\n");
4489a9ca1c79SSean Bruno 		return (ENOMEM);
4490a9ca1c79SSean Bruno 	}
4491a9ca1c79SSean Bruno 
4492a9ca1c79SSean Bruno 	// TODO: use sbufs to make a string to print out
4493a9ca1c79SSean Bruno 	/* Set multiplier for RETA setup and table size based on MAC */
4494b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
4495a9ca1c79SSean Bruno 	case ixgbe_mac_X550:
4496a9ca1c79SSean Bruno 	case ixgbe_mac_X550EM_x:
44978eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
4498a9ca1c79SSean Bruno 		reta_size = 128;
4499a9ca1c79SSean Bruno 		break;
4500a9ca1c79SSean Bruno 	default:
4501a9ca1c79SSean Bruno 		reta_size = 32;
4502a9ca1c79SSean Bruno 		break;
4503a9ca1c79SSean Bruno 	}
4504a9ca1c79SSean Bruno 
4505a9ca1c79SSean Bruno 	/* Print out the redirection table */
4506a9ca1c79SSean Bruno 	sbuf_cat(buf, "\n");
4507a9ca1c79SSean Bruno 	for (int i = 0; i < reta_size; i++) {
4508a9ca1c79SSean Bruno 		if (i < 32) {
4509a9ca1c79SSean Bruno 			reg = IXGBE_READ_REG(hw, IXGBE_RETA(i));
4510a9ca1c79SSean Bruno 			sbuf_printf(buf, "RETA(%2d): 0x%08x\n", i, reg);
4511a9ca1c79SSean Bruno 		} else {
4512a9ca1c79SSean Bruno 			reg = IXGBE_READ_REG(hw, IXGBE_ERETA(i - 32));
4513a9ca1c79SSean Bruno 			sbuf_printf(buf, "ERETA(%2d): 0x%08x\n", i - 32, reg);
4514a9ca1c79SSean Bruno 		}
4515a9ca1c79SSean Bruno 	}
4516a9ca1c79SSean Bruno 
4517a9ca1c79SSean Bruno 	// TODO: print more config
4518a9ca1c79SSean Bruno 
4519a9ca1c79SSean Bruno 	error = sbuf_finish(buf);
4520a9ca1c79SSean Bruno 	if (error)
4521a9ca1c79SSean Bruno 		device_printf(dev, "Error finishing sbuf: %d\n", error);
4522a9ca1c79SSean Bruno 
4523a9ca1c79SSean Bruno 	sbuf_delete(buf);
45248eb6488eSEric Joyner 
4525a9ca1c79SSean Bruno 	return (0);
45268eb6488eSEric Joyner } /* ixgbe_sysctl_print_rss_config */
4527a9ca1c79SSean Bruno #endif /* IXGBE_DEBUG */
4528a9ca1c79SSean Bruno 
45298eb6488eSEric Joyner /************************************************************************
45308eb6488eSEric Joyner  * ixgbe_sysctl_phy_temp - Retrieve temperature of PHY
45318eb6488eSEric Joyner  *
45328eb6488eSEric Joyner  *   For X552/X557-AT devices using an external PHY
45338eb6488eSEric Joyner  ************************************************************************/
45348eb6488eSEric Joyner static int
45358eb6488eSEric Joyner ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS)
45368eb6488eSEric Joyner {
4537b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4538b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
45398eb6488eSEric Joyner 	u16             reg;
45408eb6488eSEric Joyner 
45418eb6488eSEric Joyner 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4542b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45438eb6488eSEric Joyner 		    "Device has no supported external thermal sensor.\n");
45448eb6488eSEric Joyner 		return (ENODEV);
45458eb6488eSEric Joyner 	}
45468eb6488eSEric Joyner 
45478eb6488eSEric Joyner 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_CURRENT_TEMP,
45488eb6488eSEric Joyner 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4549b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45508eb6488eSEric Joyner 		    "Error reading from PHY's current temperature register\n");
45518eb6488eSEric Joyner 		return (EAGAIN);
45528eb6488eSEric Joyner 	}
45538eb6488eSEric Joyner 
45548eb6488eSEric Joyner 	/* Shift temp for output */
45558eb6488eSEric Joyner 	reg = reg >> 8;
45568eb6488eSEric Joyner 
4557c19c7afeSEric Joyner 	return (sysctl_handle_16(oidp, NULL, reg, req));
45588eb6488eSEric Joyner } /* ixgbe_sysctl_phy_temp */
45598eb6488eSEric Joyner 
45608eb6488eSEric Joyner /************************************************************************
45618eb6488eSEric Joyner  * ixgbe_sysctl_phy_overtemp_occurred
45628eb6488eSEric Joyner  *
45638eb6488eSEric Joyner  *   Reports (directly from the PHY) whether the current PHY
45648eb6488eSEric Joyner  *   temperature is over the overtemp threshold.
45658eb6488eSEric Joyner  ************************************************************************/
45668eb6488eSEric Joyner static int
45678eb6488eSEric Joyner ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS)
45688eb6488eSEric Joyner {
4569b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4570b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
45718eb6488eSEric Joyner 	u16             reg;
45728eb6488eSEric Joyner 
45738eb6488eSEric Joyner 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4574b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45758eb6488eSEric Joyner 		    "Device has no supported external thermal sensor.\n");
45768eb6488eSEric Joyner 		return (ENODEV);
45778eb6488eSEric Joyner 	}
45788eb6488eSEric Joyner 
45798eb6488eSEric Joyner 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_OVERTEMP_STATUS,
45808eb6488eSEric Joyner 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4581b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45828eb6488eSEric Joyner 		    "Error reading from PHY's temperature status register\n");
45838eb6488eSEric Joyner 		return (EAGAIN);
45848eb6488eSEric Joyner 	}
45858eb6488eSEric Joyner 
45868eb6488eSEric Joyner 	/* Get occurrence bit */
45878eb6488eSEric Joyner 	reg = !!(reg & 0x4000);
45888eb6488eSEric Joyner 
4589c19c7afeSEric Joyner 	return (sysctl_handle_16(oidp, 0, reg, req));
45908eb6488eSEric Joyner } /* ixgbe_sysctl_phy_overtemp_occurred */
45918eb6488eSEric Joyner 
45928eb6488eSEric Joyner /************************************************************************
45938eb6488eSEric Joyner  * ixgbe_sysctl_eee_state
45948eb6488eSEric Joyner  *
45958eb6488eSEric Joyner  *   Sysctl to set EEE power saving feature
45968eb6488eSEric Joyner  *   Values:
45978eb6488eSEric Joyner  *     0      - disable EEE
45988eb6488eSEric Joyner  *     1      - enable EEE
45998eb6488eSEric Joyner  *     (none) - get current device EEE state
46008eb6488eSEric Joyner  ************************************************************************/
46018eb6488eSEric Joyner static int
46028eb6488eSEric Joyner ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS)
46038eb6488eSEric Joyner {
4604b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4605b1d5caf3SKevin Bowling 	device_t       dev = sc->dev;
4606b1d5caf3SKevin Bowling 	struct ifnet   *ifp = iflib_get_ifp(sc->ctx);
46078eb6488eSEric Joyner 	int            curr_eee, new_eee, error = 0;
46088eb6488eSEric Joyner 	s32            retval;
46098eb6488eSEric Joyner 
4610b1d5caf3SKevin Bowling 	curr_eee = new_eee = !!(sc->feat_en & IXGBE_FEATURE_EEE);
46118eb6488eSEric Joyner 
46128eb6488eSEric Joyner 	error = sysctl_handle_int(oidp, &new_eee, 0, req);
46138eb6488eSEric Joyner 	if ((error) || (req->newptr == NULL))
46148eb6488eSEric Joyner 		return (error);
46158eb6488eSEric Joyner 
46168eb6488eSEric Joyner 	/* Nothing to do */
46178eb6488eSEric Joyner 	if (new_eee == curr_eee)
46188eb6488eSEric Joyner 		return (0);
46198eb6488eSEric Joyner 
46208eb6488eSEric Joyner 	/* Not supported */
4621b1d5caf3SKevin Bowling 	if (!(sc->feat_cap & IXGBE_FEATURE_EEE))
46228eb6488eSEric Joyner 		return (EINVAL);
46238eb6488eSEric Joyner 
46248eb6488eSEric Joyner 	/* Bounds checking */
46258eb6488eSEric Joyner 	if ((new_eee < 0) || (new_eee > 1))
46268eb6488eSEric Joyner 		return (EINVAL);
46278eb6488eSEric Joyner 
4628b1d5caf3SKevin Bowling 	retval = ixgbe_setup_eee(&sc->hw, new_eee);
46298eb6488eSEric Joyner 	if (retval) {
46308eb6488eSEric Joyner 		device_printf(dev, "Error in EEE setup: 0x%08X\n", retval);
46318eb6488eSEric Joyner 		return (EINVAL);
46328eb6488eSEric Joyner 	}
46338eb6488eSEric Joyner 
46348eb6488eSEric Joyner 	/* Restart auto-neg */
4635c19c7afeSEric Joyner 	ifp->if_init(ifp);
46368eb6488eSEric Joyner 
46378eb6488eSEric Joyner 	device_printf(dev, "New EEE state: %d\n", new_eee);
46388eb6488eSEric Joyner 
46398eb6488eSEric Joyner 	/* Cache new value */
46408eb6488eSEric Joyner 	if (new_eee)
4641b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_EEE;
46428eb6488eSEric Joyner 	else
4643b1d5caf3SKevin Bowling 		sc->feat_en &= ~IXGBE_FEATURE_EEE;
46448eb6488eSEric Joyner 
46458eb6488eSEric Joyner 	return (error);
46468eb6488eSEric Joyner } /* ixgbe_sysctl_eee_state */
46478eb6488eSEric Joyner 
46488eb6488eSEric Joyner /************************************************************************
46498eb6488eSEric Joyner  * ixgbe_init_device_features
46508eb6488eSEric Joyner  ************************************************************************/
46518eb6488eSEric Joyner static void
4652b1d5caf3SKevin Bowling ixgbe_init_device_features(struct ixgbe_softc *sc)
46538eb6488eSEric Joyner {
4654b1d5caf3SKevin Bowling 	sc->feat_cap = IXGBE_FEATURE_NETMAP
46558eb6488eSEric Joyner 	                  | IXGBE_FEATURE_RSS
46568eb6488eSEric Joyner 	                  | IXGBE_FEATURE_MSI
46578eb6488eSEric Joyner 	                  | IXGBE_FEATURE_MSIX
4658c19c7afeSEric Joyner 	                  | IXGBE_FEATURE_LEGACY_IRQ;
46598eb6488eSEric Joyner 
46608eb6488eSEric Joyner 	/* Set capabilities first... */
4661b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
46628eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
4663b1d5caf3SKevin Bowling 		if (sc->hw.device_id == IXGBE_DEV_ID_82598AT)
4664b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_FAN_FAIL;
46658eb6488eSEric Joyner 		break;
46668eb6488eSEric Joyner 	case ixgbe_mac_X540:
4667b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4668b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4669b1d5caf3SKevin Bowling 		if ((sc->hw.device_id == IXGBE_DEV_ID_X540_BYPASS) &&
4670b1d5caf3SKevin Bowling 		    (sc->hw.bus.func == 0))
4671b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_BYPASS;
46728eb6488eSEric Joyner 		break;
46738eb6488eSEric Joyner 	case ixgbe_mac_X550:
4674b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
4675b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4676b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
46778eb6488eSEric Joyner 		break;
46788eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
4679b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4680b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
46818eb6488eSEric Joyner 		break;
46828eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
4683b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4684b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4685b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
4686b1d5caf3SKevin Bowling 		if ((sc->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T) ||
4687b1d5caf3SKevin Bowling 		    (sc->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T_L)) {
4688b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
4689b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_EEE;
46908eb6488eSEric Joyner 		}
46918eb6488eSEric Joyner 		break;
46928eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
4693b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4694b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4695b1d5caf3SKevin Bowling 		if ((sc->hw.device_id == IXGBE_DEV_ID_82599_BYPASS) &&
4696b1d5caf3SKevin Bowling 		    (sc->hw.bus.func == 0))
4697b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_BYPASS;
4698b1d5caf3SKevin Bowling 		if (sc->hw.device_id == IXGBE_DEV_ID_82599_QSFP_SF_QP)
4699b1d5caf3SKevin Bowling 			sc->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
47008eb6488eSEric Joyner 		break;
47018eb6488eSEric Joyner 	default:
47028eb6488eSEric Joyner 		break;
47038eb6488eSEric Joyner 	}
47048eb6488eSEric Joyner 
47058eb6488eSEric Joyner 	/* Enabled by default... */
47068eb6488eSEric Joyner 	/* Fan failure detection */
4707b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_FAN_FAIL)
4708b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_FAN_FAIL;
47098eb6488eSEric Joyner 	/* Netmap */
4710b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_NETMAP)
4711b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_NETMAP;
47128eb6488eSEric Joyner 	/* EEE */
4713b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_EEE)
4714b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_EEE;
47158eb6488eSEric Joyner 	/* Thermal Sensor */
4716b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_TEMP_SENSOR)
4717b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_TEMP_SENSOR;
47188eb6488eSEric Joyner 
47198eb6488eSEric Joyner 	/* Enabled via global sysctl... */
47208eb6488eSEric Joyner 	/* Flow Director */
47218eb6488eSEric Joyner 	if (ixgbe_enable_fdir) {
4722b1d5caf3SKevin Bowling 		if (sc->feat_cap & IXGBE_FEATURE_FDIR)
4723b1d5caf3SKevin Bowling 			sc->feat_en |= IXGBE_FEATURE_FDIR;
47248eb6488eSEric Joyner 		else
4725b1d5caf3SKevin Bowling 			device_printf(sc->dev, "Device does not support Flow Director. Leaving disabled.");
47268eb6488eSEric Joyner 	}
47276f37f232SEric Joyner 	/*
47288eb6488eSEric Joyner 	 * Message Signal Interrupts - Extended (MSI-X)
47298eb6488eSEric Joyner 	 * Normal MSI is only enabled if MSI-X calls fail.
4730758cc3dcSJack F Vogel 	 */
47318eb6488eSEric Joyner 	if (!ixgbe_enable_msix)
4732b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_MSIX;
47338eb6488eSEric Joyner 	/* Receive-Side Scaling (RSS) */
4734b1d5caf3SKevin Bowling 	if ((sc->feat_cap & IXGBE_FEATURE_RSS) && ixgbe_enable_rss)
4735b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_RSS;
47368eb6488eSEric Joyner 
47378eb6488eSEric Joyner 	/* Disable features with unmet dependencies... */
47388eb6488eSEric Joyner 	/* No MSI-X */
4739b1d5caf3SKevin Bowling 	if (!(sc->feat_cap & IXGBE_FEATURE_MSIX)) {
4740b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_RSS;
4741b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_SRIOV;
4742b1d5caf3SKevin Bowling 		sc->feat_en &= ~IXGBE_FEATURE_RSS;
4743b1d5caf3SKevin Bowling 		sc->feat_en &= ~IXGBE_FEATURE_SRIOV;
47448eb6488eSEric Joyner 	}
47458eb6488eSEric Joyner } /* ixgbe_init_device_features */
47468eb6488eSEric Joyner 
47478eb6488eSEric Joyner /************************************************************************
47488eb6488eSEric Joyner  * ixgbe_check_fan_failure
47498eb6488eSEric Joyner  ************************************************************************/
47508eb6488eSEric Joyner static void
4751b1d5caf3SKevin Bowling ixgbe_check_fan_failure(struct ixgbe_softc *sc, u32 reg, bool in_interrupt)
47528eb6488eSEric Joyner {
47538eb6488eSEric Joyner 	u32 mask;
47548eb6488eSEric Joyner 
4755b1d5caf3SKevin Bowling 	mask = (in_interrupt) ? IXGBE_EICR_GPI_SDP1_BY_MAC(&sc->hw) :
47568eb6488eSEric Joyner 	    IXGBE_ESDP_SDP1;
47578eb6488eSEric Joyner 
47588eb6488eSEric Joyner 	if (reg & mask)
4759b1d5caf3SKevin Bowling 		device_printf(sc->dev, "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
47608eb6488eSEric Joyner } /* ixgbe_check_fan_failure */
47617660e4eaSKevin Bowling 
47627660e4eaSKevin Bowling /************************************************************************
47637660e4eaSKevin Bowling  * ixgbe_sbuf_fw_version
47647660e4eaSKevin Bowling  ************************************************************************/
47657660e4eaSKevin Bowling static void
47667660e4eaSKevin Bowling ixgbe_sbuf_fw_version(struct ixgbe_hw *hw, struct sbuf *buf)
47677660e4eaSKevin Bowling {
47687660e4eaSKevin Bowling 	struct ixgbe_nvm_version nvm_ver = {0};
47697660e4eaSKevin Bowling 	uint16_t phyfw = 0;
47707660e4eaSKevin Bowling 	int status;
47715de5419bSKevin Bowling 	const char *space = "";
47727660e4eaSKevin Bowling 
47737660e4eaSKevin Bowling 	ixgbe_get_oem_prod_version(hw, &nvm_ver); /* OEM's NVM version */
47747660e4eaSKevin Bowling 	ixgbe_get_orom_version(hw, &nvm_ver); /* Option ROM */
47757660e4eaSKevin Bowling 	ixgbe_get_etk_id(hw, &nvm_ver); /* eTrack identifies a build in Intel's SCM */
47767660e4eaSKevin Bowling 	status = ixgbe_get_phy_firmware_version(hw, &phyfw);
47777660e4eaSKevin Bowling 
47785de5419bSKevin Bowling 	if (nvm_ver.oem_valid) {
47797660e4eaSKevin Bowling 		sbuf_printf(buf, "NVM OEM V%d.%d R%d", nvm_ver.oem_major,
47807660e4eaSKevin Bowling 		    nvm_ver.oem_minor, nvm_ver.oem_release);
47815de5419bSKevin Bowling 		space = " ";
47825de5419bSKevin Bowling 	}
47837660e4eaSKevin Bowling 
47845de5419bSKevin Bowling 	if (nvm_ver.or_valid) {
47855de5419bSKevin Bowling 		sbuf_printf(buf, "%sOption ROM V%d-b%d-p%d",
47865de5419bSKevin Bowling 		    space, nvm_ver.or_major, nvm_ver.or_build, nvm_ver.or_patch);
47875de5419bSKevin Bowling 		space = " ";
47885de5419bSKevin Bowling 	}
47897660e4eaSKevin Bowling 
47905de5419bSKevin Bowling 	if (nvm_ver.etk_id != ((NVM_VER_INVALID << NVM_ETK_SHIFT) |
47915de5419bSKevin Bowling 	    NVM_VER_INVALID)) {
47925de5419bSKevin Bowling 		sbuf_printf(buf, "%seTrack 0x%08x", space, nvm_ver.etk_id);
47935de5419bSKevin Bowling 		space = " ";
47945de5419bSKevin Bowling 	}
47957660e4eaSKevin Bowling 
47967660e4eaSKevin Bowling 	if (phyfw != 0 && status == IXGBE_SUCCESS)
47975de5419bSKevin Bowling 		sbuf_printf(buf, "%sPHY FW V%d", space, phyfw);
47987660e4eaSKevin Bowling } /* ixgbe_sbuf_fw_version */
47997660e4eaSKevin Bowling 
48007660e4eaSKevin Bowling /************************************************************************
48017660e4eaSKevin Bowling  * ixgbe_print_fw_version
48027660e4eaSKevin Bowling  ************************************************************************/
48037660e4eaSKevin Bowling static void
48047660e4eaSKevin Bowling ixgbe_print_fw_version(if_ctx_t ctx)
48057660e4eaSKevin Bowling {
4806b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
4807b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
4808b1d5caf3SKevin Bowling 	device_t dev = sc->dev;
48097660e4eaSKevin Bowling 	struct sbuf *buf;
48107660e4eaSKevin Bowling 	int error = 0;
48117660e4eaSKevin Bowling 
48127660e4eaSKevin Bowling 	buf = sbuf_new_auto();
48137660e4eaSKevin Bowling 	if (!buf) {
48147660e4eaSKevin Bowling 		device_printf(dev, "Could not allocate sbuf for output.\n");
48157660e4eaSKevin Bowling 		return;
48167660e4eaSKevin Bowling 	}
48177660e4eaSKevin Bowling 
48187660e4eaSKevin Bowling 	ixgbe_sbuf_fw_version(hw, buf);
48197660e4eaSKevin Bowling 
48207660e4eaSKevin Bowling 	error = sbuf_finish(buf);
48217660e4eaSKevin Bowling 	if (error)
48227660e4eaSKevin Bowling 		device_printf(dev, "Error finishing sbuf: %d\n", error);
48237660e4eaSKevin Bowling 	else if (sbuf_len(buf))
48247660e4eaSKevin Bowling 		device_printf(dev, "%s\n", sbuf_data(buf));
48257660e4eaSKevin Bowling 
48267660e4eaSKevin Bowling 	sbuf_delete(buf);
48277660e4eaSKevin Bowling } /* ixgbe_print_fw_version */
48287660e4eaSKevin Bowling 
48297660e4eaSKevin Bowling /************************************************************************
48307660e4eaSKevin Bowling  * ixgbe_sysctl_print_fw_version
48317660e4eaSKevin Bowling  ************************************************************************/
48327660e4eaSKevin Bowling static int
48337660e4eaSKevin Bowling ixgbe_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)
48347660e4eaSKevin Bowling {
4835b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4836b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
4837b1d5caf3SKevin Bowling 	device_t dev = sc->dev;
48387660e4eaSKevin Bowling 	struct sbuf *buf;
48397660e4eaSKevin Bowling 	int error = 0;
48407660e4eaSKevin Bowling 
48417660e4eaSKevin Bowling 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
48427660e4eaSKevin Bowling 	if (!buf) {
48437660e4eaSKevin Bowling 		device_printf(dev, "Could not allocate sbuf for output.\n");
48447660e4eaSKevin Bowling 		return (ENOMEM);
48457660e4eaSKevin Bowling 	}
48467660e4eaSKevin Bowling 
48477660e4eaSKevin Bowling 	ixgbe_sbuf_fw_version(hw, buf);
48487660e4eaSKevin Bowling 
48497660e4eaSKevin Bowling 	error = sbuf_finish(buf);
48507660e4eaSKevin Bowling 	if (error)
48517660e4eaSKevin Bowling 		device_printf(dev, "Error finishing sbuf: %d\n", error);
48527660e4eaSKevin Bowling 
48537660e4eaSKevin Bowling 	sbuf_delete(buf);
48547660e4eaSKevin Bowling 
48557660e4eaSKevin Bowling 	return (0);
48567660e4eaSKevin Bowling } /* ixgbe_sysctl_print_fw_version */
4857