xref: /freebsd/sys/dev/ixgbe/if_ix.c (revision 395cc55d896654b8f75071e71e856b22aed87da5)
1758cc3dcSJack F Vogel /******************************************************************************
2758cc3dcSJack F Vogel 
38eb6488eSEric Joyner   Copyright (c) 2001-2017, Intel Corporation
4758cc3dcSJack F Vogel   All rights reserved.
5758cc3dcSJack F Vogel 
6758cc3dcSJack F Vogel   Redistribution and use in source and binary forms, with or without
7758cc3dcSJack F Vogel   modification, are permitted provided that the following conditions are met:
8758cc3dcSJack F Vogel 
9758cc3dcSJack F Vogel    1. Redistributions of source code must retain the above copyright notice,
10758cc3dcSJack F Vogel       this list of conditions and the following disclaimer.
11758cc3dcSJack F Vogel 
12758cc3dcSJack F Vogel    2. Redistributions in binary form must reproduce the above copyright
13758cc3dcSJack F Vogel       notice, this list of conditions and the following disclaimer in the
14758cc3dcSJack F Vogel       documentation and/or other materials provided with the distribution.
15758cc3dcSJack F Vogel 
16758cc3dcSJack F Vogel    3. Neither the name of the Intel Corporation nor the names of its
17758cc3dcSJack F Vogel       contributors may be used to endorse or promote products derived from
18758cc3dcSJack F Vogel       this software without specific prior written permission.
19758cc3dcSJack F Vogel 
20758cc3dcSJack F Vogel   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21758cc3dcSJack F Vogel   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22758cc3dcSJack F Vogel   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23758cc3dcSJack F Vogel   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24758cc3dcSJack F Vogel   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25758cc3dcSJack F Vogel   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26758cc3dcSJack F Vogel   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27758cc3dcSJack F Vogel   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28758cc3dcSJack F Vogel   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29758cc3dcSJack F Vogel   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30758cc3dcSJack F Vogel   POSSIBILITY OF SUCH DAMAGE.
31758cc3dcSJack F Vogel 
32758cc3dcSJack F Vogel ******************************************************************************/
33758cc3dcSJack F Vogel /*$FreeBSD$*/
34758cc3dcSJack F Vogel 
35758cc3dcSJack F Vogel #include "opt_inet.h"
36758cc3dcSJack F Vogel #include "opt_inet6.h"
37758cc3dcSJack F Vogel #include "opt_rss.h"
38758cc3dcSJack F Vogel 
39758cc3dcSJack F Vogel #include "ixgbe.h"
40c19c7afeSEric Joyner #include "ixgbe_sriov.h"
41c19c7afeSEric Joyner #include "ifdi_if.h"
42c19c7afeSEric Joyner 
43c19c7afeSEric Joyner #include <net/netmap.h>
44c19c7afeSEric Joyner #include <dev/netmap/netmap_kern.h>
45758cc3dcSJack F Vogel 
468eb6488eSEric Joyner /************************************************************************
47758cc3dcSJack F Vogel  * Driver version
488eb6488eSEric Joyner  ************************************************************************/
497d48aa4cSEric Joyner char ixgbe_driver_version[] = "4.0.1-k";
50a9ca1c79SSean Bruno 
518eb6488eSEric Joyner /************************************************************************
52758cc3dcSJack F Vogel  * PCI Device ID Table
53758cc3dcSJack F Vogel  *
54758cc3dcSJack F Vogel  *   Used by probe to select devices to load on
55758cc3dcSJack F Vogel  *   Last field stores an index into ixgbe_strings
56758cc3dcSJack F Vogel  *   Last entry must be all 0s
57758cc3dcSJack F Vogel  *
58758cc3dcSJack F Vogel  *   { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
598eb6488eSEric Joyner  ************************************************************************/
60c19c7afeSEric Joyner static pci_vendor_info_t ixgbe_vendor_info_array[] =
61758cc3dcSJack F Vogel {
62fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_DUAL_PORT,  "Intel(R) 82598EB AF (Dual Fiber)"),
63fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_SINGLE_PORT,  "Intel(R) 82598EB AF (Fiber)"),
64fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_CX4,  "Intel(R) 82598EB AT (CX4)"),
65fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT,  "Intel(R) 82598EB AT"),
66fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT2,  "Intel(R) 82598EB AT2"),
67fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598,  "Intel(R) 82598"),
68fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_DA_DUAL_PORT,  "Intel(R) 82598EB AF DA (Dual Fiber)"),
69fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_CX4_DUAL_PORT,  "Intel(R) 82598EB AT (Dual CX4)"),
70fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_XF_LR,  "Intel(R) 82598EB AF (Dual Fiber LR)"),
71fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM,  "Intel(R) 82598EB AF (Dual Fiber SR)"),
72fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_SFP_LOM,  "Intel(R) 82598EB LOM"),
73fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4,  "Intel(R) X520 82599 (KX4)"),
74fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4_MEZZ,  "Intel(R) X520 82599 (KX4 Mezzanine)"),
75fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP,  "Intel(R) X520 82599ES (SFI/SFP+)"),
76fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_XAUI_LOM,  "Intel(R) X520 82599 (XAUI/BX4)"),
77fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_CX4,  "Intel(R) X520 82599 (Dual CX4)"),
78fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_T3_LOM,  "Intel(R) X520-T 82599 LOM"),
79fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_COMBO_BACKPLANE,  "Intel(R) X520 82599 (Combined Backplane)"),
80fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BACKPLANE_FCOE,  "Intel(R) X520 82599 (Backplane w/FCoE)"),
81fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF2,  "Intel(R) X520 82599 (Dual SFP+)"),
82fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_FCOE,  "Intel(R) X520 82599 (Dual SFP+ w/FCoE)"),
83fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599EN_SFP,  "Intel(R) X520-1 82599EN (SFP+)"),
84fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF_QP,  "Intel(R) X520-4 82599 (Quad SFP+)"),
85fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_QSFP_SF_QP,  "Intel(R) X520-Q1 82599 (QSFP+)"),
86fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T,  "Intel(R) X540-AT2"),
87fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T1,  "Intel(R) X540-T1"),
88fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T,  "Intel(R) X550-T2"),
89fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T1, "Intel(R) X550-T1"),
90fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KR,  "Intel(R) X552 (KR Backplane)"),
91fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KX4,  "Intel(R) X552 (KX4 Backplane)"),
92fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_10G_T,  "Intel(R) X552/X557-AT (10GBASE-T)"),
93fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_1G_T,  "Intel(R) X552 (1000BASE-T)"),
94fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_SFP, "Intel(R) X552 (SFP+)"),
95fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR, "Intel(R) X553 (KR Backplane)"),
96fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR_L, "Intel(R) X553 L (KR Backplane)"),
97fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP, "Intel(R) X553 (SFP+)"),
98fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP_N, "Intel(R) X553 N (SFP+)"),
99fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII, "Intel(R) X553 (1GbE SGMII)"),
100fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII_L, "Intel(R) X553 L (1GbE SGMII)"),
101fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_10G_T, "Intel(R) X553/X557-AT (10GBASE-T)"),
102fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T, "Intel(R) X553 (1GbE)"),
103fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T_L, "Intel(R) X553 L (1GbE)"),
104fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540_BYPASS, "Intel(R) X540-T2 (Bypass)"),
105fdbcd35aSKevin Bowling   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BYPASS, "Intel(R) X520 82599 (Bypass)"),
106758cc3dcSJack F Vogel 	/* required last entry */
107c19c7afeSEric Joyner   PVID_END
108758cc3dcSJack F Vogel };
109758cc3dcSJack F Vogel 
110b1d5caf3SKevin Bowling static void *ixgbe_register(device_t);
111b1d5caf3SKevin Bowling static int  ixgbe_if_attach_pre(if_ctx_t);
112b1d5caf3SKevin Bowling static int  ixgbe_if_attach_post(if_ctx_t);
113b1d5caf3SKevin Bowling static int  ixgbe_if_detach(if_ctx_t);
114b1d5caf3SKevin Bowling static int  ixgbe_if_shutdown(if_ctx_t);
115b1d5caf3SKevin Bowling static int  ixgbe_if_suspend(if_ctx_t);
116b1d5caf3SKevin Bowling static int  ixgbe_if_resume(if_ctx_t);
117c19c7afeSEric Joyner 
118b1d5caf3SKevin Bowling static void ixgbe_if_stop(if_ctx_t);
119b1d5caf3SKevin Bowling void ixgbe_if_enable_intr(if_ctx_t);
120b1d5caf3SKevin Bowling static void ixgbe_if_disable_intr(if_ctx_t);
121b1d5caf3SKevin Bowling static void ixgbe_link_intr_enable(if_ctx_t);
122b1d5caf3SKevin Bowling static int  ixgbe_if_rx_queue_intr_enable(if_ctx_t, uint16_t);
123b1d5caf3SKevin Bowling static void ixgbe_if_media_status(if_ctx_t, struct ifmediareq *);
124b1d5caf3SKevin Bowling static int  ixgbe_if_media_change(if_ctx_t);
125c19c7afeSEric Joyner static int  ixgbe_if_msix_intr_assign(if_ctx_t, int);
126b1d5caf3SKevin Bowling static int  ixgbe_if_mtu_set(if_ctx_t, uint32_t);
127b1d5caf3SKevin Bowling static void ixgbe_if_crcstrip_set(if_ctx_t, int, int);
128b1d5caf3SKevin Bowling static void ixgbe_if_multi_set(if_ctx_t);
129b1d5caf3SKevin Bowling static int  ixgbe_if_promisc_set(if_ctx_t, int);
130b1d5caf3SKevin Bowling static int  ixgbe_if_tx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
131b1d5caf3SKevin Bowling static int  ixgbe_if_rx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
132b1d5caf3SKevin Bowling static void ixgbe_if_queues_free(if_ctx_t);
133b1d5caf3SKevin Bowling static void ixgbe_if_timer(if_ctx_t, uint16_t);
134b1d5caf3SKevin Bowling static void ixgbe_if_update_admin_status(if_ctx_t);
135b1d5caf3SKevin Bowling static void ixgbe_if_vlan_register(if_ctx_t, u16);
136b1d5caf3SKevin Bowling static void ixgbe_if_vlan_unregister(if_ctx_t, u16);
137b1d5caf3SKevin Bowling static int  ixgbe_if_i2c_req(if_ctx_t, struct ifi2creq *);
138b1d5caf3SKevin Bowling static bool ixgbe_if_needs_restart(if_ctx_t, enum iflib_restart_event);
139b1d5caf3SKevin Bowling int ixgbe_intr(void *);
140758cc3dcSJack F Vogel 
1418eb6488eSEric Joyner /************************************************************************
142758cc3dcSJack F Vogel  * Function prototypes
1438eb6488eSEric Joyner  ************************************************************************/
144c19c7afeSEric Joyner static uint64_t ixgbe_if_get_counter(if_ctx_t, ift_counter);
145758cc3dcSJack F Vogel 
146b1d5caf3SKevin Bowling static void ixgbe_enable_queue(struct ixgbe_softc *, u32);
147b1d5caf3SKevin Bowling static void ixgbe_disable_queue(struct ixgbe_softc *, u32);
148b1d5caf3SKevin Bowling static void ixgbe_add_device_sysctls(if_ctx_t);
149b1d5caf3SKevin Bowling static int  ixgbe_allocate_pci_resources(if_ctx_t);
150b1d5caf3SKevin Bowling static int  ixgbe_setup_low_power_mode(if_ctx_t);
151c19c7afeSEric Joyner 
152b1d5caf3SKevin Bowling static void ixgbe_config_dmac(struct ixgbe_softc *);
153b1d5caf3SKevin Bowling static void ixgbe_configure_ivars(struct ixgbe_softc *);
154b1d5caf3SKevin Bowling static void ixgbe_set_ivar(struct ixgbe_softc *, u8, u8, s8);
155c19c7afeSEric Joyner static u8   *ixgbe_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
156b1d5caf3SKevin Bowling static bool ixgbe_sfp_probe(if_ctx_t);
157c19c7afeSEric Joyner 
158b1d5caf3SKevin Bowling static void ixgbe_free_pci_resources(if_ctx_t);
159c19c7afeSEric Joyner 
160b1d5caf3SKevin Bowling static int  ixgbe_msix_link(void *);
161b1d5caf3SKevin Bowling static int  ixgbe_msix_que(void *);
162b1d5caf3SKevin Bowling static void ixgbe_initialize_rss_mapping(struct ixgbe_softc *);
163b1d5caf3SKevin Bowling static void ixgbe_initialize_receive_units(if_ctx_t);
164b1d5caf3SKevin Bowling static void ixgbe_initialize_transmit_units(if_ctx_t);
165c19c7afeSEric Joyner 
166b1d5caf3SKevin Bowling static int  ixgbe_setup_interface(if_ctx_t);
167b1d5caf3SKevin Bowling static void ixgbe_init_device_features(struct ixgbe_softc *);
168b1d5caf3SKevin Bowling static void ixgbe_check_fan_failure(struct ixgbe_softc *, u32, bool);
1697660e4eaSKevin Bowling static void ixgbe_sbuf_fw_version(struct ixgbe_hw *, struct sbuf *);
170b1d5caf3SKevin Bowling static void ixgbe_print_fw_version(if_ctx_t);
171b1d5caf3SKevin Bowling static void ixgbe_add_media_types(if_ctx_t);
172b1d5caf3SKevin Bowling static void ixgbe_update_stats_counters(struct ixgbe_softc *);
173b1d5caf3SKevin Bowling static void ixgbe_config_link(if_ctx_t);
174b1d5caf3SKevin Bowling static void ixgbe_get_slot_info(struct ixgbe_softc *);
175b1d5caf3SKevin Bowling static void ixgbe_check_wol_support(struct ixgbe_softc *);
176b1d5caf3SKevin Bowling static void ixgbe_enable_rx_drop(struct ixgbe_softc *);
177b1d5caf3SKevin Bowling static void ixgbe_disable_rx_drop(struct ixgbe_softc *);
178758cc3dcSJack F Vogel 
179b1d5caf3SKevin Bowling static void ixgbe_add_hw_stats(struct ixgbe_softc *);
180b1d5caf3SKevin Bowling static int  ixgbe_set_flowcntl(struct ixgbe_softc *, int);
181b1d5caf3SKevin Bowling static int  ixgbe_set_advertise(struct ixgbe_softc *, int);
182d381c807SPiotr Pietruszewski static int  ixgbe_get_default_advertise(struct ixgbe_softc *);
183b1d5caf3SKevin Bowling static void ixgbe_setup_vlan_hw_support(if_ctx_t);
184b1d5caf3SKevin Bowling static void ixgbe_config_gpie(struct ixgbe_softc *);
185b1d5caf3SKevin Bowling static void ixgbe_config_delay_values(struct ixgbe_softc *);
1866f37f232SEric Joyner 
1876f37f232SEric Joyner /* Sysctl handlers */
188f2c4db54SSteven Hartland static int  ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS);
189f2c4db54SSteven Hartland static int  ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS);
1908eb6488eSEric Joyner static int  ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS);
1916f37f232SEric Joyner static int  ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS);
1926f37f232SEric Joyner static int  ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS);
1936f37f232SEric Joyner static int  ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS);
1947660e4eaSKevin Bowling static int  ixgbe_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS);
195a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
196a9ca1c79SSean Bruno static int  ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS);
197a9ca1c79SSean Bruno static int  ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS);
198a9ca1c79SSean Bruno #endif
1998eb6488eSEric Joyner static int  ixgbe_sysctl_rdh_handler(SYSCTL_HANDLER_ARGS);
2008eb6488eSEric Joyner static int  ixgbe_sysctl_rdt_handler(SYSCTL_HANDLER_ARGS);
2018eb6488eSEric Joyner static int  ixgbe_sysctl_tdt_handler(SYSCTL_HANDLER_ARGS);
2028eb6488eSEric Joyner static int  ixgbe_sysctl_tdh_handler(SYSCTL_HANDLER_ARGS);
2038eb6488eSEric Joyner static int  ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS);
2046f37f232SEric Joyner static int  ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS);
2056f37f232SEric Joyner static int  ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS);
206758cc3dcSJack F Vogel 
207758cc3dcSJack F Vogel /* Deferred interrupt tasklets */
208c19c7afeSEric Joyner static void ixgbe_handle_msf(void *);
209c19c7afeSEric Joyner static void ixgbe_handle_mod(void *);
210c19c7afeSEric Joyner static void ixgbe_handle_phy(void *);
211758cc3dcSJack F Vogel 
2128eb6488eSEric Joyner /************************************************************************
213758cc3dcSJack F Vogel  *  FreeBSD Device Interface Entry Points
2148eb6488eSEric Joyner  ************************************************************************/
215a1edda90SAdrian Chadd static device_method_t ix_methods[] = {
216758cc3dcSJack F Vogel 	/* Device interface */
217c19c7afeSEric Joyner 	DEVMETHOD(device_register, ixgbe_register),
218c19c7afeSEric Joyner 	DEVMETHOD(device_probe, iflib_device_probe),
219c19c7afeSEric Joyner 	DEVMETHOD(device_attach, iflib_device_attach),
220c19c7afeSEric Joyner 	DEVMETHOD(device_detach, iflib_device_detach),
221c19c7afeSEric Joyner 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
222c19c7afeSEric Joyner 	DEVMETHOD(device_suspend, iflib_device_suspend),
223c19c7afeSEric Joyner 	DEVMETHOD(device_resume, iflib_device_resume),
22448056c88SJack F Vogel #ifdef PCI_IOV
225c19c7afeSEric Joyner 	DEVMETHOD(pci_iov_init, iflib_device_iov_init),
226c19c7afeSEric Joyner 	DEVMETHOD(pci_iov_uninit, iflib_device_iov_uninit),
227c19c7afeSEric Joyner 	DEVMETHOD(pci_iov_add_vf, iflib_device_iov_add_vf),
22848056c88SJack F Vogel #endif /* PCI_IOV */
229758cc3dcSJack F Vogel 	DEVMETHOD_END
230758cc3dcSJack F Vogel };
231758cc3dcSJack F Vogel 
232a1edda90SAdrian Chadd static driver_t ix_driver = {
233b1d5caf3SKevin Bowling 	"ix", ix_methods, sizeof(struct ixgbe_softc),
234758cc3dcSJack F Vogel };
235758cc3dcSJack F Vogel 
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) */
1124d381c807SPiotr 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 
1289d381c807SPiotr Pietruszewski 	if (hw->mac.type == ixgbe_mac_X550) {
1290d381c807SPiotr Pietruszewski 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_T, 0, NULL);
1291d381c807SPiotr Pietruszewski 		ifmedia_add(sc->media, IFM_ETHER | IFM_5000_T, 0, NULL);
1292d381c807SPiotr Pietruszewski 	}
1293d381c807SPiotr 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;
1415d381c807SPiotr Pietruszewski 
1416d381c807SPiotr Pietruszewski 		if (hw->mac.type == ixgbe_mac_X550 &&
1417d381c807SPiotr Pietruszewski 		    hw->phy.autoneg_advertised == 0) {
1418d381c807SPiotr Pietruszewski 			/*
1419d381c807SPiotr Pietruszewski 			 * 2.5G and 5G autonegotiation speeds on X550
1420d381c807SPiotr Pietruszewski 			 * are disabled by default due to reported
1421d381c807SPiotr Pietruszewski 			 * interoperability issues with some switches.
1422d381c807SPiotr Pietruszewski 			 *
1423d381c807SPiotr Pietruszewski 			 * The second condition checks if any operations
1424d381c807SPiotr Pietruszewski 			 * involving setting autonegotiation speeds have
1425d381c807SPiotr Pietruszewski 			 * been performed prior to this ixgbe_config_link()
1426d381c807SPiotr Pietruszewski 			 * call.
1427d381c807SPiotr Pietruszewski 			 *
1428d381c807SPiotr Pietruszewski 			 * If hw->phy.autoneg_advertised does not
1429d381c807SPiotr Pietruszewski 			 * equal 0, this means that the user might have
1430d381c807SPiotr Pietruszewski 			 * set autonegotiation speeds via the sysctl
1431d381c807SPiotr Pietruszewski 			 * before bringing the interface up. In this
1432d381c807SPiotr Pietruszewski 			 * case, we should not disable 2.5G and 5G
1433d381c807SPiotr Pietruszewski 			 * since that speeds might be selected by the
1434d381c807SPiotr Pietruszewski 			 * user.
1435d381c807SPiotr Pietruszewski 			 *
1436d381c807SPiotr Pietruszewski 			 * Otherwise (i.e. if hw->phy.autoneg_advertised
1437d381c807SPiotr Pietruszewski 			 * is set to 0), it is the first time we set
1438d381c807SPiotr Pietruszewski 			 * autonegotiation preferences and the default
1439d381c807SPiotr Pietruszewski 			 * set of speeds should exclude 2.5G and 5G.
1440d381c807SPiotr Pietruszewski 			 */
1441d381c807SPiotr Pietruszewski 			autoneg &= ~(IXGBE_LINK_SPEED_2_5GB_FULL |
1442d381c807SPiotr Pietruszewski 			    IXGBE_LINK_SPEED_5GB_FULL);
1443d381c807SPiotr Pietruszewski 		}
1444d381c807SPiotr 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 	char               buf[16];
20798eb6488eSEric Joyner 
2080c19c7afeSEric Joyner 	/* Admin Que is vector 0*/
2081c19c7afeSEric Joyner 	rid = vector + 1;
2082b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, vector++, rx_que++) {
2083c19c7afeSEric Joyner 		rid = vector + 1;
2084c19c7afeSEric Joyner 
2085c19c7afeSEric Joyner 		snprintf(buf, sizeof(buf), "rxq%d", i);
2086c19c7afeSEric Joyner 		error = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
208781be6552SMatt Macy 		    IFLIB_INTR_RXTX, ixgbe_msix_que, rx_que, rx_que->rxr.me, buf);
2088c19c7afeSEric Joyner 
2089c19c7afeSEric Joyner 		if (error) {
2090c19c7afeSEric Joyner 			device_printf(iflib_get_dev(ctx),
2091c19c7afeSEric Joyner 			    "Failed to allocate que int %d err: %d", i, error);
2092b1d5caf3SKevin Bowling 			sc->num_rx_queues = i + 1;
2093c19c7afeSEric Joyner 			goto fail;
20948eb6488eSEric Joyner 		}
20958eb6488eSEric Joyner 
2096c19c7afeSEric Joyner 		rx_que->msix = vector;
2097c19c7afeSEric Joyner 	}
2098b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_tx_queues; i++) {
2099c19c7afeSEric Joyner 		snprintf(buf, sizeof(buf), "txq%d", i);
2100b1d5caf3SKevin Bowling 		tx_que = &sc->tx_queues[i];
2101b1d5caf3SKevin Bowling 		tx_que->msix = i % sc->num_rx_queues;
2102c19c7afeSEric Joyner 		iflib_softirq_alloc_generic(ctx,
2103b1d5caf3SKevin Bowling 		    &sc->rx_queues[tx_que->msix].que_irq,
2104c19c7afeSEric Joyner 		    IFLIB_INTR_TX, tx_que, tx_que->txr.me, buf);
2105c19c7afeSEric Joyner 	}
2106c19c7afeSEric Joyner 	rid = vector + 1;
2107b1d5caf3SKevin Bowling 	error = iflib_irq_alloc_generic(ctx, &sc->irq, rid,
2108b1d5caf3SKevin Bowling 	    IFLIB_INTR_ADMIN, ixgbe_msix_link, sc, 0, "aq");
2109c19c7afeSEric Joyner 	if (error) {
2110c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
2111c19c7afeSEric Joyner 		    "Failed to register admin handler");
2112c19c7afeSEric Joyner 		return (error);
2113c19c7afeSEric Joyner 	}
2114c19c7afeSEric Joyner 
2115b1d5caf3SKevin Bowling 	sc->vector = vector;
2116c19c7afeSEric Joyner 
2117c19c7afeSEric Joyner 	return (0);
2118c19c7afeSEric Joyner fail:
2119b1d5caf3SKevin Bowling 	iflib_irq_free(ctx, &sc->irq);
2120b1d5caf3SKevin Bowling 	rx_que = sc->rx_queues;
2121b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, rx_que++)
2122c19c7afeSEric Joyner 		iflib_irq_free(ctx, &rx_que->que_irq);
2123c19c7afeSEric Joyner 
2124c19c7afeSEric Joyner 	return (error);
2125c19c7afeSEric Joyner } /* ixgbe_if_msix_intr_assign */
2126c19c7afeSEric Joyner 
212764881da4SSai Rajesh Tallamraju static inline void
2128b1d5caf3SKevin Bowling ixgbe_perform_aim(struct ixgbe_softc *sc, struct ix_rx_queue *que)
212964881da4SSai Rajesh Tallamraju {
213064881da4SSai Rajesh Tallamraju 	uint32_t newitr = 0;
213164881da4SSai Rajesh Tallamraju 	struct rx_ring *rxr = &que->rxr;
213264881da4SSai Rajesh Tallamraju 
213364881da4SSai Rajesh Tallamraju 	/*
213464881da4SSai Rajesh Tallamraju 	 * Do Adaptive Interrupt Moderation:
213564881da4SSai Rajesh Tallamraju 	 *  - Write out last calculated setting
213664881da4SSai Rajesh Tallamraju 	 *  - Calculate based on average size over
213764881da4SSai Rajesh Tallamraju 	 *    the last interval.
213864881da4SSai Rajesh Tallamraju 	 */
213964881da4SSai Rajesh Tallamraju 	if (que->eitr_setting) {
2140b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EITR(que->msix),
214164881da4SSai Rajesh Tallamraju 		    que->eitr_setting);
214264881da4SSai Rajesh Tallamraju 	}
214364881da4SSai Rajesh Tallamraju 
214464881da4SSai Rajesh Tallamraju 	que->eitr_setting = 0;
214564881da4SSai Rajesh Tallamraju 	/* Idle, do nothing */
214664881da4SSai Rajesh Tallamraju 	if (rxr->bytes == 0) {
214764881da4SSai Rajesh Tallamraju 		return;
214864881da4SSai Rajesh Tallamraju 	}
214964881da4SSai Rajesh Tallamraju 
215064881da4SSai Rajesh Tallamraju 	if ((rxr->bytes) && (rxr->packets)) {
215164881da4SSai Rajesh Tallamraju 		newitr = (rxr->bytes / rxr->packets);
215264881da4SSai Rajesh Tallamraju 	}
215364881da4SSai Rajesh Tallamraju 
215464881da4SSai Rajesh Tallamraju 	newitr += 24; /* account for hardware frame, crc */
215564881da4SSai Rajesh Tallamraju 	/* set an upper boundary */
215664881da4SSai Rajesh Tallamraju 	newitr = min(newitr, 3000);
215764881da4SSai Rajesh Tallamraju 
215864881da4SSai Rajesh Tallamraju 	/* Be nice to the mid range */
215964881da4SSai Rajesh Tallamraju 	if ((newitr > 300) && (newitr < 1200)) {
216064881da4SSai Rajesh Tallamraju 		newitr = (newitr / 3);
216164881da4SSai Rajesh Tallamraju 	} else {
216264881da4SSai Rajesh Tallamraju 		newitr = (newitr / 2);
216364881da4SSai Rajesh Tallamraju 	}
216464881da4SSai Rajesh Tallamraju 
2165b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
216664881da4SSai Rajesh Tallamraju 		newitr |= newitr << 16;
216764881da4SSai Rajesh Tallamraju 	} else {
216864881da4SSai Rajesh Tallamraju 		newitr |= IXGBE_EITR_CNT_WDIS;
216964881da4SSai Rajesh Tallamraju 	}
217064881da4SSai Rajesh Tallamraju 
217164881da4SSai Rajesh Tallamraju 	/* save for next interrupt */
217264881da4SSai Rajesh Tallamraju 	que->eitr_setting = newitr;
217364881da4SSai Rajesh Tallamraju 
217464881da4SSai Rajesh Tallamraju 	/* Reset state */
217564881da4SSai Rajesh Tallamraju 	rxr->bytes = 0;
217664881da4SSai Rajesh Tallamraju 	rxr->packets = 0;
217764881da4SSai Rajesh Tallamraju 
217864881da4SSai Rajesh Tallamraju 	return;
217964881da4SSai Rajesh Tallamraju }
218064881da4SSai Rajesh Tallamraju 
2181c19c7afeSEric Joyner /*********************************************************************
21828eb6488eSEric Joyner  * ixgbe_msix_que - MSI-X Queue Interrupt Service routine
2183c19c7afeSEric Joyner  **********************************************************************/
2184c19c7afeSEric Joyner static int
21858eb6488eSEric Joyner ixgbe_msix_que(void *arg)
21868eb6488eSEric Joyner {
2187c19c7afeSEric Joyner 	struct ix_rx_queue *que = arg;
2188b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = que->sc;
2189b1d5caf3SKevin Bowling 	struct ifnet       *ifp = iflib_get_ifp(que->sc->ctx);
21908eb6488eSEric Joyner 
21918eb6488eSEric Joyner 	/* Protect against spurious interrupts */
21928eb6488eSEric Joyner 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
21936c351041SEric Joyner 		return (FILTER_HANDLED);
21948eb6488eSEric Joyner 
2195b1d5caf3SKevin Bowling 	ixgbe_disable_queue(sc, que->msix);
21968eb6488eSEric Joyner 	++que->irqs;
21978eb6488eSEric Joyner 
219864881da4SSai Rajesh Tallamraju 	/* Check for AIM */
2199b1d5caf3SKevin Bowling 	if (sc->enable_aim) {
2200b1d5caf3SKevin Bowling 		ixgbe_perform_aim(sc, que);
220164881da4SSai Rajesh Tallamraju 	}
220264881da4SSai Rajesh Tallamraju 
2203c19c7afeSEric Joyner 	return (FILTER_SCHEDULE_THREAD);
22048eb6488eSEric Joyner } /* ixgbe_msix_que */
22058eb6488eSEric Joyner 
22068eb6488eSEric Joyner /************************************************************************
22078eb6488eSEric Joyner  * ixgbe_media_status - Media Ioctl callback
22088eb6488eSEric Joyner  *
22098eb6488eSEric Joyner  *   Called whenever the user queries the status of
22108eb6488eSEric Joyner  *   the interface using ifconfig.
22118eb6488eSEric Joyner  ************************************************************************/
22128eb6488eSEric Joyner static void
2213c19c7afeSEric Joyner ixgbe_if_media_status(if_ctx_t ctx, struct ifmediareq * ifmr)
22148eb6488eSEric Joyner {
2215b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2216b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
22178eb6488eSEric Joyner 	int             layer;
22188eb6488eSEric Joyner 
2219c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_media_status: begin");
2220c19c7afeSEric Joyner 
22218eb6488eSEric Joyner 	ifmr->ifm_status = IFM_AVALID;
22228eb6488eSEric Joyner 	ifmr->ifm_active = IFM_ETHER;
22238eb6488eSEric Joyner 
2224b1d5caf3SKevin Bowling 	if (!sc->link_active)
22258eb6488eSEric Joyner 		return;
22268eb6488eSEric Joyner 
22278eb6488eSEric Joyner 	ifmr->ifm_status |= IFM_ACTIVE;
2228b1d5caf3SKevin Bowling 	layer = sc->phy_layer;
22298eb6488eSEric Joyner 
22308eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T ||
22318eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_T ||
22328eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_100BASE_TX ||
22338eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
2234b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22358eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22368eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
22378eb6488eSEric Joyner 			break;
22388eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22398eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
22408eb6488eSEric Joyner 			break;
22418eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_100_FULL:
22428eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
22438eb6488eSEric Joyner 			break;
22448eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10_FULL:
22458eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10_T | IFM_FDX;
22468eb6488eSEric Joyner 			break;
22478eb6488eSEric Joyner 		}
2248d381c807SPiotr Pietruszewski 	if (hw->mac.type == ixgbe_mac_X550)
2249d381c807SPiotr Pietruszewski 		switch (sc->link_speed) {
2250d381c807SPiotr Pietruszewski 		case IXGBE_LINK_SPEED_5GB_FULL:
2251d381c807SPiotr Pietruszewski 			ifmr->ifm_active |= IFM_5000_T | IFM_FDX;
2252d381c807SPiotr Pietruszewski 			break;
2253d381c807SPiotr Pietruszewski 		case IXGBE_LINK_SPEED_2_5GB_FULL:
2254d381c807SPiotr Pietruszewski 			ifmr->ifm_active |= IFM_2500_T | IFM_FDX;
2255d381c807SPiotr Pietruszewski 			break;
2256d381c807SPiotr Pietruszewski 		}
22578eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
22588eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
2259b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22608eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22618eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_TWINAX | IFM_FDX;
22628eb6488eSEric Joyner 			break;
22638eb6488eSEric Joyner 		}
22648eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR)
2265b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22668eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22678eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_LR | IFM_FDX;
22688eb6488eSEric Joyner 			break;
22698eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22708eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
22718eb6488eSEric Joyner 			break;
22728eb6488eSEric Joyner 		}
22738eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LRM)
2274b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22758eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22768eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_LRM | IFM_FDX;
22778eb6488eSEric Joyner 			break;
22788eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22798eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
22808eb6488eSEric Joyner 			break;
22818eb6488eSEric Joyner 		}
22828eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR ||
22838eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
2284b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22858eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22868eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
22878eb6488eSEric Joyner 			break;
22888eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22898eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
22908eb6488eSEric Joyner 			break;
22918eb6488eSEric Joyner 		}
22928eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
2293b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
22948eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22958eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
22968eb6488eSEric Joyner 			break;
22978eb6488eSEric Joyner 		}
22988eb6488eSEric Joyner 	/*
22998eb6488eSEric Joyner 	 * XXX: These need to use the proper media types once
23008eb6488eSEric Joyner 	 * they're added.
23018eb6488eSEric Joyner 	 */
23028eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
23038eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
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_2_5GB_FULL:
23098eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
23108eb6488eSEric Joyner 			break;
23118eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23128eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
23138eb6488eSEric Joyner 			break;
23148eb6488eSEric Joyner 		}
23158eb6488eSEric Joyner 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
23168eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
23178eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
2318b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23198eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23208eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
23218eb6488eSEric Joyner 			break;
23228eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23238eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
23248eb6488eSEric Joyner 			break;
23258eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23268eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
23278eb6488eSEric Joyner 			break;
23288eb6488eSEric Joyner 		}
23298eb6488eSEric Joyner #else
23308eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
2331b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23328eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23338eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_KR | IFM_FDX;
23348eb6488eSEric Joyner 			break;
23358eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23368eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
23378eb6488eSEric Joyner 			break;
23388eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23398eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
23408eb6488eSEric Joyner 			break;
23418eb6488eSEric Joyner 		}
23428eb6488eSEric Joyner 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
23438eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
23448eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
2345b1d5caf3SKevin Bowling 		switch (sc->link_speed) {
23468eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
23478eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_KX4 | IFM_FDX;
23488eb6488eSEric Joyner 			break;
23498eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
23508eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
23518eb6488eSEric Joyner 			break;
23528eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
23538eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
23548eb6488eSEric Joyner 			break;
23558eb6488eSEric Joyner 		}
23568eb6488eSEric Joyner #endif
23578eb6488eSEric Joyner 
23588eb6488eSEric Joyner 	/* If nothing is recognized... */
23598eb6488eSEric Joyner 	if (IFM_SUBTYPE(ifmr->ifm_active) == 0)
23608eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_UNKNOWN;
23618eb6488eSEric Joyner 
23628eb6488eSEric Joyner 	/* Display current flow control setting used on link */
23638eb6488eSEric Joyner 	if (hw->fc.current_mode == ixgbe_fc_rx_pause ||
23648eb6488eSEric Joyner 	    hw->fc.current_mode == ixgbe_fc_full)
23658eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
23668eb6488eSEric Joyner 	if (hw->fc.current_mode == ixgbe_fc_tx_pause ||
23678eb6488eSEric Joyner 	    hw->fc.current_mode == ixgbe_fc_full)
23688eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
23698eb6488eSEric Joyner } /* ixgbe_media_status */
23708eb6488eSEric Joyner 
23718eb6488eSEric Joyner /************************************************************************
23728eb6488eSEric Joyner  * ixgbe_media_change - Media Ioctl callback
23738eb6488eSEric Joyner  *
23748eb6488eSEric Joyner  *   Called when the user changes speed/duplex using
23758eb6488eSEric Joyner  *   media/mediopt option with ifconfig.
23768eb6488eSEric Joyner  ************************************************************************/
23778eb6488eSEric Joyner static int
2378c19c7afeSEric Joyner ixgbe_if_media_change(if_ctx_t ctx)
23798eb6488eSEric Joyner {
2380b1d5caf3SKevin Bowling 	struct ixgbe_softc   *sc = iflib_get_softc(ctx);
2381c19c7afeSEric Joyner 	struct ifmedia   *ifm = iflib_get_media(ctx);
2382b1d5caf3SKevin Bowling 	struct ixgbe_hw  *hw = &sc->hw;
23838eb6488eSEric Joyner 	ixgbe_link_speed speed = 0;
23848eb6488eSEric Joyner 
2385c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_media_change: begin");
23868eb6488eSEric Joyner 
23878eb6488eSEric Joyner 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
23888eb6488eSEric Joyner 		return (EINVAL);
23898eb6488eSEric Joyner 
23908eb6488eSEric Joyner 	if (hw->phy.media_type == ixgbe_media_type_backplane)
2391c19c7afeSEric Joyner 		return (EPERM);
23928eb6488eSEric Joyner 
23938eb6488eSEric Joyner 	/*
23948eb6488eSEric Joyner 	 * We don't actually need to check against the supported
23958eb6488eSEric Joyner 	 * media types of the adapter; ifmedia will take care of
23968eb6488eSEric Joyner 	 * that for us.
23978eb6488eSEric Joyner 	 */
23988eb6488eSEric Joyner 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
23998eb6488eSEric Joyner 	case IFM_AUTO:
24008eb6488eSEric Joyner 	case IFM_10G_T:
24018eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
24028eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24038eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
24048eb6488eSEric Joyner 		break;
24058eb6488eSEric Joyner 	case IFM_10G_LRM:
24068eb6488eSEric Joyner 	case IFM_10G_LR:
24078eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
24088eb6488eSEric Joyner 	case IFM_10G_SR: /* KR, too */
24098eb6488eSEric Joyner 	case IFM_10G_CX4: /* KX4 */
24108eb6488eSEric Joyner #else
24118eb6488eSEric Joyner 	case IFM_10G_KR:
24128eb6488eSEric Joyner 	case IFM_10G_KX4:
24138eb6488eSEric Joyner #endif
24148eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24158eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
24168eb6488eSEric Joyner 		break;
24178eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
24188eb6488eSEric Joyner 	case IFM_1000_CX: /* KX */
24198eb6488eSEric Joyner #else
24208eb6488eSEric Joyner 	case IFM_1000_KX:
24218eb6488eSEric Joyner #endif
24228eb6488eSEric Joyner 	case IFM_1000_LX:
24238eb6488eSEric Joyner 	case IFM_1000_SX:
24248eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24258eb6488eSEric Joyner 		break;
24268eb6488eSEric Joyner 	case IFM_1000_T:
24278eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
24288eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
24298eb6488eSEric Joyner 		break;
24308eb6488eSEric Joyner 	case IFM_10G_TWINAX:
24318eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
24328eb6488eSEric Joyner 		break;
2433d381c807SPiotr Pietruszewski 	case IFM_5000_T:
2434d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
2435d381c807SPiotr Pietruszewski 		break;
2436d381c807SPiotr Pietruszewski 	case IFM_2500_T:
2437d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
2438d381c807SPiotr Pietruszewski 		break;
24398eb6488eSEric Joyner 	case IFM_100_TX:
24408eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
24418eb6488eSEric Joyner 		break;
24428eb6488eSEric Joyner 	case IFM_10_T:
24438eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10_FULL;
24448eb6488eSEric Joyner 		break;
24458eb6488eSEric Joyner 	default:
24468eb6488eSEric Joyner 		goto invalid;
24478eb6488eSEric Joyner 	}
24488eb6488eSEric Joyner 
244979b36ec9SKevin Bowling 	hw->mac.autotry_restart = true;
245079b36ec9SKevin Bowling 	hw->mac.ops.setup_link(hw, speed, true);
2451b1d5caf3SKevin Bowling 	sc->advertise =
2452d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_10GB_FULL)  ? 0x4  : 0) |
2453d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_5GB_FULL)   ? 0x20 : 0) |
2454d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
2455d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_1GB_FULL)   ? 0x2  : 0) |
2456d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_100_FULL)   ? 0x1  : 0) |
2457d381c807SPiotr Pietruszewski 	    ((speed & IXGBE_LINK_SPEED_10_FULL)    ? 0x8  : 0);
24588eb6488eSEric Joyner 
24598eb6488eSEric Joyner 	return (0);
24608eb6488eSEric Joyner 
24618eb6488eSEric Joyner invalid:
2462c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "Invalid media type!\n");
24638eb6488eSEric Joyner 
24648eb6488eSEric Joyner 	return (EINVAL);
2465c19c7afeSEric Joyner } /* ixgbe_if_media_change */
24668eb6488eSEric Joyner 
24678eb6488eSEric Joyner /************************************************************************
24688eb6488eSEric Joyner  * ixgbe_set_promisc
24698eb6488eSEric Joyner  ************************************************************************/
2470c19c7afeSEric Joyner static int
2471c19c7afeSEric Joyner ixgbe_if_promisc_set(if_ctx_t ctx, int flags)
24728eb6488eSEric Joyner {
2473b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2474c19c7afeSEric Joyner 	struct ifnet   *ifp = iflib_get_ifp(ctx);
24758eb6488eSEric Joyner 	u32            rctl;
2476c19c7afeSEric Joyner 	int            mcnt = 0;
24778eb6488eSEric Joyner 
2478b1d5caf3SKevin Bowling 	rctl = IXGBE_READ_REG(&sc->hw, IXGBE_FCTRL);
24798eb6488eSEric Joyner 	rctl &= (~IXGBE_FCTRL_UPE);
24808eb6488eSEric Joyner 	if (ifp->if_flags & IFF_ALLMULTI)
24818eb6488eSEric Joyner 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
24828eb6488eSEric Joyner 	else {
2483ba76aa63SGleb Smirnoff 		mcnt = min(if_llmaddr_count(ifp), MAX_NUM_MULTICAST_ADDRESSES);
24848eb6488eSEric Joyner 	}
24858eb6488eSEric Joyner 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
24868eb6488eSEric Joyner 		rctl &= (~IXGBE_FCTRL_MPE);
2487b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
24888eb6488eSEric Joyner 
24898eb6488eSEric Joyner 	if (ifp->if_flags & IFF_PROMISC) {
24908eb6488eSEric Joyner 		rctl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
2491b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
24928eb6488eSEric Joyner 	} else if (ifp->if_flags & IFF_ALLMULTI) {
24938eb6488eSEric Joyner 		rctl |= IXGBE_FCTRL_MPE;
24948eb6488eSEric Joyner 		rctl &= ~IXGBE_FCTRL_UPE;
2495b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
24968eb6488eSEric Joyner 	}
2497c19c7afeSEric Joyner 	return (0);
2498c19c7afeSEric Joyner } /* ixgbe_if_promisc_set */
24998eb6488eSEric Joyner 
25008eb6488eSEric Joyner /************************************************************************
25018eb6488eSEric Joyner  * ixgbe_msix_link - Link status change ISR (MSI/MSI-X)
25028eb6488eSEric Joyner  ************************************************************************/
2503c19c7afeSEric Joyner static int
25048eb6488eSEric Joyner ixgbe_msix_link(void *arg)
25058eb6488eSEric Joyner {
2506b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = arg;
2507b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
25088eb6488eSEric Joyner 	u32             eicr, eicr_mask;
25098eb6488eSEric Joyner 	s32             retval;
25108eb6488eSEric Joyner 
2511b1d5caf3SKevin Bowling 	++sc->link_irq;
25128eb6488eSEric Joyner 
25138eb6488eSEric Joyner 	/* Pause other interrupts */
25148eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_OTHER);
25158eb6488eSEric Joyner 
25168eb6488eSEric Joyner 	/* First get the cause */
25178eb6488eSEric Joyner 	eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
25188eb6488eSEric Joyner 	/* Be sure the queue bits are not cleared */
25198eb6488eSEric Joyner 	eicr &= ~IXGBE_EICR_RTX_QUEUE;
25208eb6488eSEric Joyner 	/* Clear interrupt with write */
25218eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
25228eb6488eSEric Joyner 
25238eb6488eSEric Joyner 	/* Link status change */
25248eb6488eSEric Joyner 	if (eicr & IXGBE_EICR_LSC) {
25258eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
2526b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_LSC;
25278eb6488eSEric Joyner 	}
25288eb6488eSEric Joyner 
2529b1d5caf3SKevin Bowling 	if (sc->hw.mac.type != ixgbe_mac_82598EB) {
2530b1d5caf3SKevin Bowling 		if ((sc->feat_en & IXGBE_FEATURE_FDIR) &&
25318eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_FLOW_DIR)) {
25328eb6488eSEric Joyner 			/* This is probably overkill :) */
2533b1d5caf3SKevin Bowling 			if (!atomic_cmpset_int(&sc->fdir_reinit, 0, 1))
2534c19c7afeSEric Joyner 				return (FILTER_HANDLED);
25358eb6488eSEric Joyner 			/* Disable the interrupt */
2536c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_FLOW_DIR);
2537b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_FDIR;
2538c19c7afeSEric Joyner 		} else
25398eb6488eSEric Joyner 			if (eicr & IXGBE_EICR_ECC) {
2540b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2541c19c7afeSEric Joyner 				   "\nCRITICAL: ECC ERROR!! Please Reboot!!\n");
25428eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
25438eb6488eSEric Joyner 			}
25448eb6488eSEric Joyner 
25458eb6488eSEric Joyner 		/* Check for over temp condition */
2546b1d5caf3SKevin Bowling 		if (sc->feat_en & IXGBE_FEATURE_TEMP_SENSOR) {
2547b1d5caf3SKevin Bowling 			switch (sc->hw.mac.type) {
25488eb6488eSEric Joyner 			case ixgbe_mac_X550EM_a:
25498eb6488eSEric Joyner 				if (!(eicr & IXGBE_EICR_GPI_SDP0_X550EM_a))
25508eb6488eSEric Joyner 					break;
25518eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EIMC,
25528eb6488eSEric Joyner 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
25538eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR,
25548eb6488eSEric Joyner 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
25558eb6488eSEric Joyner 				retval = hw->phy.ops.check_overtemp(hw);
25568eb6488eSEric Joyner 				if (retval != IXGBE_ERR_OVERTEMP)
25578eb6488eSEric Joyner 					break;
2558b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2559c19c7afeSEric Joyner 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2560b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2561c19c7afeSEric Joyner 				    "System shutdown required!\n");
25628eb6488eSEric Joyner 				break;
25638eb6488eSEric Joyner 			default:
25648eb6488eSEric Joyner 				if (!(eicr & IXGBE_EICR_TS))
25658eb6488eSEric Joyner 					break;
25668eb6488eSEric Joyner 				retval = hw->phy.ops.check_overtemp(hw);
25678eb6488eSEric Joyner 				if (retval != IXGBE_ERR_OVERTEMP)
25688eb6488eSEric Joyner 					break;
2569b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2570c19c7afeSEric Joyner 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2571b1d5caf3SKevin Bowling 				device_printf(iflib_get_dev(sc->ctx),
2572c19c7afeSEric Joyner 				    "System shutdown required!\n");
25738eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
25748eb6488eSEric Joyner 				break;
25758eb6488eSEric Joyner 			}
25768eb6488eSEric Joyner 		}
25778eb6488eSEric Joyner 
25788eb6488eSEric Joyner 		/* Check for VF message */
2579b1d5caf3SKevin Bowling 		if ((sc->feat_en & IXGBE_FEATURE_SRIOV) &&
25808eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_MAILBOX))
2581b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MBX;
25828eb6488eSEric Joyner 	}
25838eb6488eSEric Joyner 
25848eb6488eSEric Joyner 	if (ixgbe_is_sfp(hw)) {
25858eb6488eSEric Joyner 		/* Pluggable optics-related interrupt */
25868eb6488eSEric Joyner 		if (hw->mac.type >= ixgbe_mac_X540)
25878eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
25888eb6488eSEric Joyner 		else
25898eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
25908eb6488eSEric Joyner 
25918eb6488eSEric Joyner 		if (eicr & eicr_mask) {
25928eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
2593b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
25948eb6488eSEric Joyner 		}
25958eb6488eSEric Joyner 
25968eb6488eSEric Joyner 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
25978eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
25988eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
25998eb6488eSEric Joyner 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
2600b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
26018eb6488eSEric Joyner 		}
26028eb6488eSEric Joyner 	}
26038eb6488eSEric Joyner 
26048eb6488eSEric Joyner 	/* Check for fan failure */
2605b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL) {
2606b1d5caf3SKevin Bowling 		ixgbe_check_fan_failure(sc, eicr, true);
26078eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
26088eb6488eSEric Joyner 	}
26098eb6488eSEric Joyner 
26108eb6488eSEric Joyner 	/* External PHY interrupt */
26118eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
26128eb6488eSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP0_X540)) {
26138eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP0_X540);
2614b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_PHY;
26158eb6488eSEric Joyner 	}
26168eb6488eSEric Joyner 
2617b1d5caf3SKevin Bowling 	return (sc->task_requests != 0) ? FILTER_SCHEDULE_THREAD : FILTER_HANDLED;
26188eb6488eSEric Joyner } /* ixgbe_msix_link */
26198eb6488eSEric Joyner 
26208eb6488eSEric Joyner /************************************************************************
26218eb6488eSEric Joyner  * ixgbe_sysctl_interrupt_rate_handler
26228eb6488eSEric Joyner  ************************************************************************/
2623758cc3dcSJack F Vogel static int
2624758cc3dcSJack F Vogel ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)
2625758cc3dcSJack F Vogel {
2626c19c7afeSEric Joyner 	struct ix_rx_queue *que = ((struct ix_rx_queue *)oidp->oid_arg1);
26278eb6488eSEric Joyner 	int                error;
2628758cc3dcSJack F Vogel 	unsigned int       reg, usec, rate;
2629758cc3dcSJack F Vogel 
2630b1d5caf3SKevin Bowling 	reg = IXGBE_READ_REG(&que->sc->hw, IXGBE_EITR(que->msix));
2631758cc3dcSJack F Vogel 	usec = ((reg & 0x0FF8) >> 3);
2632758cc3dcSJack F Vogel 	if (usec > 0)
2633758cc3dcSJack F Vogel 		rate = 500000 / usec;
2634758cc3dcSJack F Vogel 	else
2635758cc3dcSJack F Vogel 		rate = 0;
2636758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &rate, 0, req);
2637758cc3dcSJack F Vogel 	if (error || !req->newptr)
2638758cc3dcSJack F Vogel 		return error;
2639758cc3dcSJack F Vogel 	reg &= ~0xfff; /* default, no limitation */
2640758cc3dcSJack F Vogel 	ixgbe_max_interrupt_rate = 0;
2641758cc3dcSJack F Vogel 	if (rate > 0 && rate < 500000) {
2642758cc3dcSJack F Vogel 		if (rate < 1000)
2643758cc3dcSJack F Vogel 			rate = 1000;
2644758cc3dcSJack F Vogel 		ixgbe_max_interrupt_rate = rate;
2645758cc3dcSJack F Vogel 		reg |= ((4000000/rate) & 0xff8);
2646758cc3dcSJack F Vogel 	}
2647b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&que->sc->hw, IXGBE_EITR(que->msix), reg);
2648758cc3dcSJack F Vogel 
26498eb6488eSEric Joyner 	return (0);
26508eb6488eSEric Joyner } /* ixgbe_sysctl_interrupt_rate_handler */
26518eb6488eSEric Joyner 
26528eb6488eSEric Joyner /************************************************************************
26538eb6488eSEric Joyner  * ixgbe_add_device_sysctls
26548eb6488eSEric Joyner  ************************************************************************/
26556f37f232SEric Joyner static void
2656c19c7afeSEric Joyner ixgbe_add_device_sysctls(if_ctx_t ctx)
26576f37f232SEric Joyner {
2658b1d5caf3SKevin Bowling 	struct ixgbe_softc         *sc = iflib_get_softc(ctx);
2659c19c7afeSEric Joyner 	device_t               dev = iflib_get_dev(ctx);
2660b1d5caf3SKevin Bowling 	struct ixgbe_hw        *hw = &sc->hw;
26616f37f232SEric Joyner 	struct sysctl_oid_list *child;
2662c19c7afeSEric Joyner 	struct sysctl_ctx_list *ctx_list;
26636f37f232SEric Joyner 
2664c19c7afeSEric Joyner 	ctx_list = device_get_sysctl_ctx(dev);
26656f37f232SEric Joyner 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
26666f37f232SEric Joyner 
26676f37f232SEric Joyner 	/* Sysctls for all devices */
2668c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fc",
266920b91f0aSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2670b1d5caf3SKevin Bowling 	    sc, 0, ixgbe_sysctl_flowcntl, "I",
2671c19c7afeSEric Joyner 	    IXGBE_SYSCTL_DESC_SET_FC);
26726f37f232SEric Joyner 
2673c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "advertise_speed",
267420b91f0aSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2675b1d5caf3SKevin Bowling 	    sc, 0, ixgbe_sysctl_advertise, "I",
26768eb6488eSEric Joyner 	    IXGBE_SYSCTL_DESC_ADV_SPEED);
26776f37f232SEric Joyner 
2678b1d5caf3SKevin Bowling 	sc->enable_aim = ixgbe_enable_aim;
267964881da4SSai Rajesh Tallamraju 	SYSCTL_ADD_INT(ctx_list, child, OID_AUTO, "enable_aim", CTLFLAG_RW,
2680b1d5caf3SKevin Bowling 	    &sc->enable_aim, 0, "Interrupt Moderation");
268164881da4SSai Rajesh Tallamraju 
26827660e4eaSKevin Bowling 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fw_version",
2683b1d5caf3SKevin Bowling 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
26847660e4eaSKevin Bowling 	    ixgbe_sysctl_print_fw_version, "A", "Prints FW/NVM Versions");
26857660e4eaSKevin Bowling 
2686a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
2687a9ca1c79SSean Bruno 	/* testing sysctls (for all devices) */
2688c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "power_state",
268920b91f0aSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2690b1d5caf3SKevin Bowling 	    sc, 0, ixgbe_sysctl_power_state,
26918eb6488eSEric Joyner 	    "I", "PCI Power State");
2692a9ca1c79SSean Bruno 
2693c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "print_rss_config",
2694b1d5caf3SKevin Bowling 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
2695a9ca1c79SSean Bruno 	    ixgbe_sysctl_print_rss_config, "A", "Prints RSS Configuration");
2696a9ca1c79SSean Bruno #endif
2697a9ca1c79SSean Bruno 	/* for X550 series devices */
26986f37f232SEric Joyner 	if (hw->mac.type >= ixgbe_mac_X550)
2699c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "dmac",
270020b91f0aSPawel Biernacki 		    CTLTYPE_U16 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2701b1d5caf3SKevin Bowling 		    sc, 0, ixgbe_sysctl_dmac,
27028eb6488eSEric Joyner 		    "I", "DMA Coalesce");
27036f37f232SEric Joyner 
2704a9ca1c79SSean Bruno 	/* for WoL-capable devices */
2705a9ca1c79SSean Bruno 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
2706c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wol_enable",
2707b1d5caf3SKevin Bowling 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
27088eb6488eSEric Joyner 		    ixgbe_sysctl_wol_enable, "I", "Enable/Disable Wake on LAN");
27096f37f232SEric Joyner 
2710c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wufc",
271120b91f0aSPawel Biernacki 		    CTLTYPE_U32 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2712b1d5caf3SKevin Bowling 		    sc, 0, ixgbe_sysctl_wufc,
27138eb6488eSEric Joyner 		    "I", "Enable/Disable Wake Up Filters");
27146f37f232SEric Joyner 	}
27156f37f232SEric Joyner 
2716a9ca1c79SSean Bruno 	/* for X552/X557-AT devices */
27176f37f232SEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
27186f37f232SEric Joyner 		struct sysctl_oid *phy_node;
27196f37f232SEric Joyner 		struct sysctl_oid_list *phy_list;
27206f37f232SEric Joyner 
2721c19c7afeSEric Joyner 		phy_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "phy",
272220b91f0aSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "External PHY sysctls");
27236f37f232SEric Joyner 		phy_list = SYSCTL_CHILDREN(phy_node);
27246f37f232SEric Joyner 
2725c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO, "temp",
272620b91f0aSPawel Biernacki 		    CTLTYPE_U16 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
2727b1d5caf3SKevin Bowling 		    sc, 0, ixgbe_sysctl_phy_temp,
27288eb6488eSEric Joyner 		    "I", "Current External PHY Temperature (Celsius)");
27296f37f232SEric Joyner 
2730c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO,
273120b91f0aSPawel Biernacki 		    "overtemp_occurred",
2732b1d5caf3SKevin Bowling 		    CTLTYPE_U16 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
27336f37f232SEric Joyner 		    ixgbe_sysctl_phy_overtemp_occurred, "I",
27346f37f232SEric Joyner 		    "External PHY High Temperature Event Occurred");
27356f37f232SEric Joyner 	}
27368eb6488eSEric Joyner 
2737b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_EEE) {
2738c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "eee_state",
2739b1d5caf3SKevin Bowling 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
27408eb6488eSEric Joyner 		    ixgbe_sysctl_eee_state, "I", "EEE Power Save State");
27418eb6488eSEric Joyner 	}
27428eb6488eSEric Joyner } /* ixgbe_add_device_sysctls */
27438eb6488eSEric Joyner 
27448eb6488eSEric Joyner /************************************************************************
27458eb6488eSEric Joyner  * ixgbe_allocate_pci_resources
27468eb6488eSEric Joyner  ************************************************************************/
27478eb6488eSEric Joyner static int
2748c19c7afeSEric Joyner ixgbe_allocate_pci_resources(if_ctx_t ctx)
27498eb6488eSEric Joyner {
2750b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2751c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
27528eb6488eSEric Joyner 	int             rid;
27538eb6488eSEric Joyner 
27548eb6488eSEric Joyner 	rid = PCIR_BAR(0);
2755b1d5caf3SKevin Bowling 	sc->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
27568eb6488eSEric Joyner 	    RF_ACTIVE);
27578eb6488eSEric Joyner 
2758b1d5caf3SKevin Bowling 	if (!(sc->pci_mem)) {
27598eb6488eSEric Joyner 		device_printf(dev, "Unable to allocate bus resource: memory\n");
27608eb6488eSEric Joyner 		return (ENXIO);
27618eb6488eSEric Joyner 	}
27628eb6488eSEric Joyner 
27638eb6488eSEric Joyner 	/* Save bus_space values for READ/WRITE_REG macros */
2764b1d5caf3SKevin Bowling 	sc->osdep.mem_bus_space_tag = rman_get_bustag(sc->pci_mem);
2765b1d5caf3SKevin Bowling 	sc->osdep.mem_bus_space_handle =
2766b1d5caf3SKevin Bowling 	    rman_get_bushandle(sc->pci_mem);
27678eb6488eSEric Joyner 	/* Set hw values for shared code */
2768b1d5caf3SKevin Bowling 	sc->hw.hw_addr = (u8 *)&sc->osdep.mem_bus_space_handle;
27698eb6488eSEric Joyner 
27708eb6488eSEric Joyner 	return (0);
27718eb6488eSEric Joyner } /* ixgbe_allocate_pci_resources */
27728eb6488eSEric Joyner 
27738eb6488eSEric Joyner /************************************************************************
27748eb6488eSEric Joyner  * ixgbe_detach - Device removal routine
27758eb6488eSEric Joyner  *
27768eb6488eSEric Joyner  *   Called when the driver is being removed.
27778eb6488eSEric Joyner  *   Stops the adapter and deallocates all the resources
27788eb6488eSEric Joyner  *   that were allocated for driver operation.
27798eb6488eSEric Joyner  *
27808eb6488eSEric Joyner  *   return 0 on success, positive on failure
27818eb6488eSEric Joyner  ************************************************************************/
27828eb6488eSEric Joyner static int
2783c19c7afeSEric Joyner ixgbe_if_detach(if_ctx_t ctx)
27848eb6488eSEric Joyner {
2785b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2786c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
27878eb6488eSEric Joyner 	u32            ctrl_ext;
27888eb6488eSEric Joyner 
27898eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_detach: begin");
27908eb6488eSEric Joyner 
27918eb6488eSEric Joyner 	if (ixgbe_pci_iov_detach(dev) != 0) {
27928eb6488eSEric Joyner 		device_printf(dev, "SR-IOV in use; detach first.\n");
27938eb6488eSEric Joyner 		return (EBUSY);
27948eb6488eSEric Joyner 	}
27958eb6488eSEric Joyner 
2796c19c7afeSEric Joyner 	ixgbe_setup_low_power_mode(ctx);
27978eb6488eSEric Joyner 
27988eb6488eSEric Joyner 	/* let hardware know driver is unloading */
2799b1d5caf3SKevin Bowling 	ctrl_ext = IXGBE_READ_REG(&sc->hw, IXGBE_CTRL_EXT);
28008eb6488eSEric Joyner 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
2801b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_CTRL_EXT, ctrl_ext);
28028eb6488eSEric Joyner 
2803c19c7afeSEric Joyner 	ixgbe_free_pci_resources(ctx);
2804b1d5caf3SKevin Bowling 	free(sc->mta, M_IXGBE);
28058eb6488eSEric Joyner 
28068eb6488eSEric Joyner 	return (0);
2807c19c7afeSEric Joyner } /* ixgbe_if_detach */
28088eb6488eSEric Joyner 
28098eb6488eSEric Joyner /************************************************************************
28108eb6488eSEric Joyner  * ixgbe_setup_low_power_mode - LPLU/WoL preparation
28118eb6488eSEric Joyner  *
28128eb6488eSEric Joyner  *   Prepare the adapter/port for LPLU and/or WoL
28138eb6488eSEric Joyner  ************************************************************************/
28148eb6488eSEric Joyner static int
2815c19c7afeSEric Joyner ixgbe_setup_low_power_mode(if_ctx_t ctx)
28168eb6488eSEric Joyner {
2817b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2818b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
2819c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
28208eb6488eSEric Joyner 	s32             error = 0;
28218eb6488eSEric Joyner 
2822c19c7afeSEric Joyner 	if (!hw->wol_enabled)
282379b36ec9SKevin Bowling 		ixgbe_set_phy_power(hw, false);
28248eb6488eSEric Joyner 
28258eb6488eSEric Joyner 	/* Limit power management flow to X550EM baseT */
28268eb6488eSEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T &&
28278eb6488eSEric Joyner 	    hw->phy.ops.enter_lplu) {
28288eb6488eSEric Joyner 		/* Turn off support for APM wakeup. (Using ACPI instead) */
28298eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_GRC,
28308eb6488eSEric Joyner 		    IXGBE_READ_REG(hw, IXGBE_GRC) & ~(u32)2);
28318eb6488eSEric Joyner 
28328eb6488eSEric Joyner 		/*
28338eb6488eSEric Joyner 		 * Clear Wake Up Status register to prevent any previous wakeup
28348eb6488eSEric Joyner 		 * events from waking us up immediately after we suspend.
28358eb6488eSEric Joyner 		 */
28368eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
28378eb6488eSEric Joyner 
28388eb6488eSEric Joyner 		/*
28398eb6488eSEric Joyner 		 * Program the Wakeup Filter Control register with user filter
28408eb6488eSEric Joyner 		 * settings
28418eb6488eSEric Joyner 		 */
2842b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, sc->wufc);
28438eb6488eSEric Joyner 
28448eb6488eSEric Joyner 		/* Enable wakeups and power management in Wakeup Control */
28458eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_WUC,
28468eb6488eSEric Joyner 		    IXGBE_WUC_WKEN | IXGBE_WUC_PME_EN);
28478eb6488eSEric Joyner 
28488eb6488eSEric Joyner 		/* X550EM baseT adapters need a special LPLU flow */
284979b36ec9SKevin Bowling 		hw->phy.reset_disable = true;
2850c19c7afeSEric Joyner 		ixgbe_if_stop(ctx);
28518eb6488eSEric Joyner 		error = hw->phy.ops.enter_lplu(hw);
28528eb6488eSEric Joyner 		if (error)
28538eb6488eSEric Joyner 			device_printf(dev, "Error entering LPLU: %d\n", error);
285479b36ec9SKevin Bowling 		hw->phy.reset_disable = false;
28558eb6488eSEric Joyner 	} else {
28568eb6488eSEric Joyner 		/* Just stop for other adapters */
2857c19c7afeSEric Joyner 		ixgbe_if_stop(ctx);
28588eb6488eSEric Joyner 	}
28598eb6488eSEric Joyner 
28608eb6488eSEric Joyner 	return error;
28618eb6488eSEric Joyner } /* ixgbe_setup_low_power_mode */
28628eb6488eSEric Joyner 
28638eb6488eSEric Joyner /************************************************************************
28648eb6488eSEric Joyner  * ixgbe_shutdown - Shutdown entry point
28658eb6488eSEric Joyner  ************************************************************************/
28668eb6488eSEric Joyner static int
2867c19c7afeSEric Joyner ixgbe_if_shutdown(if_ctx_t ctx)
28688eb6488eSEric Joyner {
28698eb6488eSEric Joyner 	int error = 0;
28708eb6488eSEric Joyner 
28718eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_shutdown: begin");
28728eb6488eSEric Joyner 
2873c19c7afeSEric Joyner 	error = ixgbe_setup_low_power_mode(ctx);
28748eb6488eSEric Joyner 
28758eb6488eSEric Joyner 	return (error);
2876c19c7afeSEric Joyner } /* ixgbe_if_shutdown */
28778eb6488eSEric Joyner 
28788eb6488eSEric Joyner /************************************************************************
28798eb6488eSEric Joyner  * ixgbe_suspend
28808eb6488eSEric Joyner  *
28818eb6488eSEric Joyner  *   From D0 to D3
28828eb6488eSEric Joyner  ************************************************************************/
28838eb6488eSEric Joyner static int
2884c19c7afeSEric Joyner ixgbe_if_suspend(if_ctx_t ctx)
28858eb6488eSEric Joyner {
28868eb6488eSEric Joyner 	int error = 0;
28878eb6488eSEric Joyner 
28888eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_suspend: begin");
28898eb6488eSEric Joyner 
2890c19c7afeSEric Joyner 	error = ixgbe_setup_low_power_mode(ctx);
28918eb6488eSEric Joyner 
28928eb6488eSEric Joyner 	return (error);
2893c19c7afeSEric Joyner } /* ixgbe_if_suspend */
28948eb6488eSEric Joyner 
28958eb6488eSEric Joyner /************************************************************************
28968eb6488eSEric Joyner  * ixgbe_resume
28978eb6488eSEric Joyner  *
28988eb6488eSEric Joyner  *   From D3 to D0
28998eb6488eSEric Joyner  ************************************************************************/
29008eb6488eSEric Joyner static int
2901c19c7afeSEric Joyner ixgbe_if_resume(if_ctx_t ctx)
29028eb6488eSEric Joyner {
2903b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2904c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
2905c19c7afeSEric Joyner 	struct ifnet    *ifp = iflib_get_ifp(ctx);
2906b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
29078eb6488eSEric Joyner 	u32             wus;
29088eb6488eSEric Joyner 
29098eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_resume: begin");
29108eb6488eSEric Joyner 
29118eb6488eSEric Joyner 	/* Read & clear WUS register */
29128eb6488eSEric Joyner 	wus = IXGBE_READ_REG(hw, IXGBE_WUS);
29138eb6488eSEric Joyner 	if (wus)
29148eb6488eSEric Joyner 		device_printf(dev, "Woken up by (WUS): %#010x\n",
29158eb6488eSEric Joyner 		    IXGBE_READ_REG(hw, IXGBE_WUS));
29168eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
29178eb6488eSEric Joyner 	/* And clear WUFC until next low-power transition */
29188eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
29198eb6488eSEric Joyner 
29208eb6488eSEric Joyner 	/*
29218eb6488eSEric Joyner 	 * Required after D3->D0 transition;
29228eb6488eSEric Joyner 	 * will re-advertise all previous advertised speeds
29238eb6488eSEric Joyner 	 */
29248eb6488eSEric Joyner 	if (ifp->if_flags & IFF_UP)
2925c19c7afeSEric Joyner 		ixgbe_if_init(ctx);
29268eb6488eSEric Joyner 
29278eb6488eSEric Joyner 	return (0);
2928c19c7afeSEric Joyner } /* ixgbe_if_resume */
29298eb6488eSEric Joyner 
29308eb6488eSEric Joyner /************************************************************************
2931c19c7afeSEric Joyner  * ixgbe_if_mtu_set - Ioctl mtu entry point
29328eb6488eSEric Joyner  *
2933c19c7afeSEric Joyner  *   Return 0 on success, EINVAL on failure
2934c19c7afeSEric Joyner  ************************************************************************/
2935c19c7afeSEric Joyner static int
2936c19c7afeSEric Joyner ixgbe_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
2937c19c7afeSEric Joyner {
2938b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2939c19c7afeSEric Joyner 	int error = 0;
2940c19c7afeSEric Joyner 
2941c19c7afeSEric Joyner 	IOCTL_DEBUGOUT("ioctl: SIOCIFMTU (Set Interface MTU)");
2942c19c7afeSEric Joyner 
2943c19c7afeSEric Joyner 	if (mtu > IXGBE_MAX_MTU) {
2944c19c7afeSEric Joyner 		error = EINVAL;
2945c19c7afeSEric Joyner 	} else {
2946b1d5caf3SKevin Bowling 		sc->max_frame_size = mtu + IXGBE_MTU_HDR;
2947c19c7afeSEric Joyner 	}
2948c19c7afeSEric Joyner 
2949c19c7afeSEric Joyner 	return error;
2950c19c7afeSEric Joyner } /* ixgbe_if_mtu_set */
2951c19c7afeSEric Joyner 
2952c19c7afeSEric Joyner /************************************************************************
2953c19c7afeSEric Joyner  * ixgbe_if_crcstrip_set
29548eb6488eSEric Joyner  ************************************************************************/
29558eb6488eSEric Joyner static void
2956c19c7afeSEric Joyner ixgbe_if_crcstrip_set(if_ctx_t ctx, int onoff, int crcstrip)
29578eb6488eSEric Joyner {
2958b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2959c19c7afeSEric Joyner 	struct ixgbe_hw *hw = &sc->hw;
2960c19c7afeSEric Joyner 	/* crc stripping is set in two places:
2961c19c7afeSEric Joyner 	 * IXGBE_HLREG0 (modified on init_locked and hw reset)
2962c19c7afeSEric Joyner 	 * IXGBE_RDRXCTL (set by the original driver in
2963c19c7afeSEric Joyner 	 *	ixgbe_setup_hw_rsc() called in init_locked.
2964c19c7afeSEric Joyner 	 *	We disable the setting when netmap is compiled in).
2965c19c7afeSEric Joyner 	 * We update the values here, but also in ixgbe.c because
2966c19c7afeSEric Joyner 	 * init_locked sometimes is called outside our control.
2967c19c7afeSEric Joyner 	 */
2968c19c7afeSEric Joyner 	uint32_t hl, rxc;
29698eb6488eSEric Joyner 
2970c19c7afeSEric Joyner 	hl = IXGBE_READ_REG(hw, IXGBE_HLREG0);
2971c19c7afeSEric Joyner 	rxc = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
2972c19c7afeSEric Joyner #ifdef NETMAP
2973c19c7afeSEric Joyner 	if (netmap_verbose)
2974c19c7afeSEric Joyner 		D("%s read  HLREG 0x%x rxc 0x%x",
2975c19c7afeSEric Joyner 			onoff ? "enter" : "exit", hl, rxc);
29768eb6488eSEric Joyner #endif
2977c19c7afeSEric Joyner 	/* hw requirements ... */
2978c19c7afeSEric Joyner 	rxc &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
2979c19c7afeSEric Joyner 	rxc |= IXGBE_RDRXCTL_RSCACKC;
2980c19c7afeSEric Joyner 	if (onoff && !crcstrip) {
2981c19c7afeSEric Joyner 		/* keep the crc. Fast rx */
2982c19c7afeSEric Joyner 		hl &= ~IXGBE_HLREG0_RXCRCSTRP;
2983c19c7afeSEric Joyner 		rxc &= ~IXGBE_RDRXCTL_CRCSTRIP;
2984c19c7afeSEric Joyner 	} else {
2985c19c7afeSEric Joyner 		/* reset default mode */
2986c19c7afeSEric Joyner 		hl |= IXGBE_HLREG0_RXCRCSTRP;
2987c19c7afeSEric Joyner 		rxc |= IXGBE_RDRXCTL_CRCSTRIP;
2988c19c7afeSEric Joyner 	}
2989c19c7afeSEric Joyner #ifdef NETMAP
2990c19c7afeSEric Joyner 	if (netmap_verbose)
2991c19c7afeSEric Joyner 		D("%s write HLREG 0x%x rxc 0x%x",
2992c19c7afeSEric Joyner 			onoff ? "enter" : "exit", hl, rxc);
2993c19c7afeSEric Joyner #endif
2994c19c7afeSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hl);
2995c19c7afeSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rxc);
2996c19c7afeSEric Joyner } /* ixgbe_if_crcstrip_set */
29978eb6488eSEric Joyner 
2998c19c7afeSEric Joyner /*********************************************************************
2999c19c7afeSEric Joyner  * ixgbe_if_init - Init entry point
30008eb6488eSEric Joyner  *
30018eb6488eSEric Joyner  *   Used in two ways: It is used by the stack as an init
30028eb6488eSEric Joyner  *   entry point in network interface structure. It is also
30038eb6488eSEric Joyner  *   used by the driver as a hw/sw initialization routine to
30048eb6488eSEric Joyner  *   get to a consistent state.
30058eb6488eSEric Joyner  *
3006c19c7afeSEric Joyner  *   Return 0 on success, positive on failure
3007c19c7afeSEric Joyner  **********************************************************************/
30088eb6488eSEric Joyner void
3009c19c7afeSEric Joyner ixgbe_if_init(if_ctx_t ctx)
30108eb6488eSEric Joyner {
3011b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3012c19c7afeSEric Joyner 	struct ifnet       *ifp = iflib_get_ifp(ctx);
3013c19c7afeSEric Joyner 	device_t           dev = iflib_get_dev(ctx);
3014b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
3015c19c7afeSEric Joyner 	struct ix_rx_queue *rx_que;
3016c19c7afeSEric Joyner 	struct ix_tx_queue *tx_que;
30178eb6488eSEric Joyner 	u32             txdctl, mhadd;
30188eb6488eSEric Joyner 	u32             rxdctl, rxctrl;
30198eb6488eSEric Joyner 	u32             ctrl_ext;
30208eb6488eSEric Joyner 
3021c19c7afeSEric Joyner 	int             i, j, err;
30228eb6488eSEric Joyner 
3023c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_init: begin");
30248eb6488eSEric Joyner 
30258eb6488eSEric Joyner 	/* Queue indices may change with IOV mode */
3026b1d5caf3SKevin Bowling 	ixgbe_align_all_queue_indices(sc);
30278eb6488eSEric Joyner 
30288eb6488eSEric Joyner 	/* reprogram the RAR[0] in case user changed it. */
3029b1d5caf3SKevin Bowling 	ixgbe_set_rar(hw, 0, hw->mac.addr, sc->pool, IXGBE_RAH_AV);
30308eb6488eSEric Joyner 
30318eb6488eSEric Joyner 	/* Get the latest mac address, User can use a LAA */
30328eb6488eSEric Joyner 	bcopy(IF_LLADDR(ifp), hw->mac.addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
3033b1d5caf3SKevin Bowling 	ixgbe_set_rar(hw, 0, hw->mac.addr, sc->pool, 1);
30348eb6488eSEric Joyner 	hw->addr_ctrl.rar_used_count = 1;
30358eb6488eSEric Joyner 
30368eb6488eSEric Joyner 	ixgbe_init_hw(hw);
3037c19c7afeSEric Joyner 
3038b1d5caf3SKevin Bowling 	ixgbe_initialize_iov(sc);
3039c19c7afeSEric Joyner 
3040c19c7afeSEric Joyner 	ixgbe_initialize_transmit_units(ctx);
30418eb6488eSEric Joyner 
30428eb6488eSEric Joyner 	/* Setup Multicast table */
3043c19c7afeSEric Joyner 	ixgbe_if_multi_set(ctx);
30448eb6488eSEric Joyner 
30458eb6488eSEric Joyner 	/* Determine the correct mbuf pool, based on frame size */
3046b1d5caf3SKevin Bowling 	sc->rx_mbuf_sz = iflib_get_rx_mbuf_sz(ctx);
30478eb6488eSEric Joyner 
30488eb6488eSEric Joyner 	/* Configure RX settings */
3049c19c7afeSEric Joyner 	ixgbe_initialize_receive_units(ctx);
30508eb6488eSEric Joyner 
3051b2c1e8e6SEric Joyner 	/*
3052b2c1e8e6SEric Joyner 	 * Initialize variable holding task enqueue requests
3053b2c1e8e6SEric Joyner 	 * from MSI-X interrupts
3054b2c1e8e6SEric Joyner 	 */
3055b1d5caf3SKevin Bowling 	sc->task_requests = 0;
3056b2c1e8e6SEric Joyner 
30578eb6488eSEric Joyner 	/* Enable SDP & MSI-X interrupts based on adapter */
3058b1d5caf3SKevin Bowling 	ixgbe_config_gpie(sc);
30598eb6488eSEric Joyner 
30608eb6488eSEric Joyner 	/* Set MTU size */
30618eb6488eSEric Joyner 	if (ifp->if_mtu > ETHERMTU) {
30628eb6488eSEric Joyner 		/* aka IXGBE_MAXFRS on 82599 and newer */
30638eb6488eSEric Joyner 		mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
30648eb6488eSEric Joyner 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
3065b1d5caf3SKevin Bowling 		mhadd |= sc->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
30668eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
30678eb6488eSEric Joyner 	}
30688eb6488eSEric Joyner 
30698eb6488eSEric Joyner 	/* Now enable all the queues */
3070b1d5caf3SKevin Bowling 	for (i = 0, tx_que = sc->tx_queues; i < sc->num_tx_queues; i++, tx_que++) {
3071c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
3072c19c7afeSEric Joyner 
30738eb6488eSEric Joyner 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(txr->me));
30748eb6488eSEric Joyner 		txdctl |= IXGBE_TXDCTL_ENABLE;
30758eb6488eSEric Joyner 		/* Set WTHRESH to 8, burst writeback */
30768eb6488eSEric Joyner 		txdctl |= (8 << 16);
30778eb6488eSEric Joyner 		/*
30788eb6488eSEric Joyner 		 * When the internal queue falls below PTHRESH (32),
30798eb6488eSEric Joyner 		 * start prefetching as long as there are at least
30808eb6488eSEric Joyner 		 * HTHRESH (1) buffers ready. The values are taken
30818eb6488eSEric Joyner 		 * from the Intel linux driver 3.8.21.
30828eb6488eSEric Joyner 		 * Prefetching enables tx line rate even with 1 queue.
30838eb6488eSEric Joyner 		 */
30848eb6488eSEric Joyner 		txdctl |= (32 << 0) | (1 << 8);
30858eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(txr->me), txdctl);
30868eb6488eSEric Joyner 	}
30878eb6488eSEric Joyner 
3088b1d5caf3SKevin Bowling 	for (i = 0, rx_que = sc->rx_queues; i < sc->num_rx_queues; i++, rx_que++) {
3089c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
3090c19c7afeSEric Joyner 
30918eb6488eSEric Joyner 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
30928eb6488eSEric Joyner 		if (hw->mac.type == ixgbe_mac_82598EB) {
30938eb6488eSEric Joyner 			/*
30948eb6488eSEric Joyner 			 * PTHRESH = 21
30958eb6488eSEric Joyner 			 * HTHRESH = 4
30968eb6488eSEric Joyner 			 * WTHRESH = 8
30978eb6488eSEric Joyner 			 */
30988eb6488eSEric Joyner 			rxdctl &= ~0x3FFFFF;
30998eb6488eSEric Joyner 			rxdctl |= 0x080420;
31008eb6488eSEric Joyner 		}
31018eb6488eSEric Joyner 		rxdctl |= IXGBE_RXDCTL_ENABLE;
31028eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), rxdctl);
3103c19c7afeSEric Joyner 		for (j = 0; j < 10; j++) {
31048eb6488eSEric Joyner 			if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me)) &
31058eb6488eSEric Joyner 			    IXGBE_RXDCTL_ENABLE)
31068eb6488eSEric Joyner 				break;
31078eb6488eSEric Joyner 			else
31088eb6488eSEric Joyner 				msec_delay(1);
31098eb6488eSEric Joyner 		}
31108eb6488eSEric Joyner 		wmb();
31118eb6488eSEric Joyner 	}
31128eb6488eSEric Joyner 
31138eb6488eSEric Joyner 	/* Enable Receive engine */
31148eb6488eSEric Joyner 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
31158eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
31168eb6488eSEric Joyner 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
31178eb6488eSEric Joyner 	rxctrl |= IXGBE_RXCTRL_RXEN;
31188eb6488eSEric Joyner 	ixgbe_enable_rx_dma(hw, rxctrl);
31198eb6488eSEric Joyner 
3120c19c7afeSEric Joyner 	/* Set up MSI/MSI-X routing */
3121c19c7afeSEric Joyner 	if (ixgbe_enable_msix)  {
3122b1d5caf3SKevin Bowling 		ixgbe_configure_ivars(sc);
31238eb6488eSEric Joyner 		/* Set up auto-mask */
31248eb6488eSEric Joyner 		if (hw->mac.type == ixgbe_mac_82598EB)
31258eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
31268eb6488eSEric Joyner 		else {
31278eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
31288eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
31298eb6488eSEric Joyner 		}
31308eb6488eSEric Joyner 	} else {  /* Simple settings for Legacy/MSI */
3131b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, 0, 0, 0);
3132b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, 0, 0, 1);
31338eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
31348eb6488eSEric Joyner 	}
31358eb6488eSEric Joyner 
3136b1d5caf3SKevin Bowling 	ixgbe_init_fdir(sc);
31378eb6488eSEric Joyner 
31388eb6488eSEric Joyner 	/*
31398eb6488eSEric Joyner 	 * Check on any SFP devices that
31408eb6488eSEric Joyner 	 * need to be kick-started
31418eb6488eSEric Joyner 	 */
31428eb6488eSEric Joyner 	if (hw->phy.type == ixgbe_phy_none) {
31438eb6488eSEric Joyner 		err = hw->phy.ops.identify(hw);
31448eb6488eSEric Joyner 		if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
31458eb6488eSEric Joyner 			device_printf(dev,
31468eb6488eSEric Joyner 			    "Unsupported SFP+ module type was detected.\n");
31478eb6488eSEric Joyner 			return;
31488eb6488eSEric Joyner 		}
31498eb6488eSEric Joyner 	}
31508eb6488eSEric Joyner 
31518eb6488eSEric Joyner 	/* Set moderation on the Link interrupt */
3152b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(hw, IXGBE_EITR(sc->vector), IXGBE_LINK_ITR);
31538eb6488eSEric Joyner 
3154c19c7afeSEric Joyner 	/* Enable power to the phy. */
315579b36ec9SKevin Bowling 	ixgbe_set_phy_power(hw, true);
3156c19c7afeSEric Joyner 
31578eb6488eSEric Joyner 	/* Config/Enable Link */
3158b2c1e8e6SEric Joyner 	ixgbe_config_link(ctx);
31598eb6488eSEric Joyner 
31608eb6488eSEric Joyner 	/* Hardware Packet Buffer & Flow Control setup */
3161b1d5caf3SKevin Bowling 	ixgbe_config_delay_values(sc);
31628eb6488eSEric Joyner 
31638eb6488eSEric Joyner 	/* Initialize the FC settings */
31648eb6488eSEric Joyner 	ixgbe_start_hw(hw);
31658eb6488eSEric Joyner 
31668eb6488eSEric Joyner 	/* Set up VLAN support and filter */
3167c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
31688eb6488eSEric Joyner 
31698eb6488eSEric Joyner 	/* Setup DMA Coalescing */
3170b1d5caf3SKevin Bowling 	ixgbe_config_dmac(sc);
31718eb6488eSEric Joyner 
31728eb6488eSEric Joyner 	/* And now turn on interrupts */
3173c19c7afeSEric Joyner 	ixgbe_if_enable_intr(ctx);
31748eb6488eSEric Joyner 
31758eb6488eSEric Joyner 	/* Enable the use of the MBX by the VF's */
3176b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_SRIOV) {
31778eb6488eSEric Joyner 		ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
31788eb6488eSEric Joyner 		ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
31798eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
31808eb6488eSEric Joyner 	}
31818eb6488eSEric Joyner 
31828eb6488eSEric Joyner } /* ixgbe_init_locked */
31838eb6488eSEric Joyner 
31848eb6488eSEric Joyner /************************************************************************
31858eb6488eSEric Joyner  * ixgbe_set_ivar
31868eb6488eSEric Joyner  *
31878eb6488eSEric Joyner  *   Setup the correct IVAR register for a particular MSI-X interrupt
31888eb6488eSEric Joyner  *     (yes this is all very magic and confusing :)
31898eb6488eSEric Joyner  *    - entry is the register array entry
31908eb6488eSEric Joyner  *    - vector is the MSI-X vector for this queue
31918eb6488eSEric Joyner  *    - type is RX/TX/MISC
31928eb6488eSEric Joyner  ************************************************************************/
31938eb6488eSEric Joyner static void
3194b1d5caf3SKevin Bowling ixgbe_set_ivar(struct ixgbe_softc *sc, u8 entry, u8 vector, s8 type)
31958eb6488eSEric Joyner {
3196b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
31978eb6488eSEric Joyner 	u32 ivar, index;
31988eb6488eSEric Joyner 
31998eb6488eSEric Joyner 	vector |= IXGBE_IVAR_ALLOC_VAL;
32008eb6488eSEric Joyner 
32018eb6488eSEric Joyner 	switch (hw->mac.type) {
32028eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
32038eb6488eSEric Joyner 		if (type == -1)
32048eb6488eSEric Joyner 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
32058eb6488eSEric Joyner 		else
32068eb6488eSEric Joyner 			entry += (type * 64);
32078eb6488eSEric Joyner 		index = (entry >> 2) & 0x1F;
32088eb6488eSEric Joyner 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
32098eb6488eSEric Joyner 		ivar &= ~(0xFF << (8 * (entry & 0x3)));
32108eb6488eSEric Joyner 		ivar |= (vector << (8 * (entry & 0x3)));
3211b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_IVAR(index), ivar);
32128eb6488eSEric Joyner 		break;
32138eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
32148eb6488eSEric Joyner 	case ixgbe_mac_X540:
32158eb6488eSEric Joyner 	case ixgbe_mac_X550:
32168eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
32178eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
32188eb6488eSEric Joyner 		if (type == -1) { /* MISC IVAR */
32198eb6488eSEric Joyner 			index = (entry & 1) * 8;
32208eb6488eSEric Joyner 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
32218eb6488eSEric Joyner 			ivar &= ~(0xFF << index);
32228eb6488eSEric Joyner 			ivar |= (vector << index);
32238eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
32248eb6488eSEric Joyner 		} else {          /* RX/TX IVARS */
32258eb6488eSEric Joyner 			index = (16 * (entry & 1)) + (8 * type);
32268eb6488eSEric Joyner 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
32278eb6488eSEric Joyner 			ivar &= ~(0xFF << index);
32288eb6488eSEric Joyner 			ivar |= (vector << index);
32298eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
32308eb6488eSEric Joyner 		}
32318eb6488eSEric Joyner 	default:
32328eb6488eSEric Joyner 		break;
32338eb6488eSEric Joyner 	}
32348eb6488eSEric Joyner } /* ixgbe_set_ivar */
32358eb6488eSEric Joyner 
32368eb6488eSEric Joyner /************************************************************************
32378eb6488eSEric Joyner  * ixgbe_configure_ivars
32388eb6488eSEric Joyner  ************************************************************************/
32398eb6488eSEric Joyner static void
3240b1d5caf3SKevin Bowling ixgbe_configure_ivars(struct ixgbe_softc *sc)
32418eb6488eSEric Joyner {
3242b1d5caf3SKevin Bowling 	struct ix_rx_queue *rx_que = sc->rx_queues;
3243b1d5caf3SKevin Bowling 	struct ix_tx_queue *tx_que = sc->tx_queues;
32448eb6488eSEric Joyner 	u32                newitr;
32458eb6488eSEric Joyner 
32468eb6488eSEric Joyner 	if (ixgbe_max_interrupt_rate > 0)
32478eb6488eSEric Joyner 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
32488eb6488eSEric Joyner 	else {
32498eb6488eSEric Joyner 		/*
32508eb6488eSEric Joyner 		 * Disable DMA coalescing if interrupt moderation is
32518eb6488eSEric Joyner 		 * disabled.
32528eb6488eSEric Joyner 		 */
3253b1d5caf3SKevin Bowling 		sc->dmac = 0;
32548eb6488eSEric Joyner 		newitr = 0;
32558eb6488eSEric Joyner 	}
32568eb6488eSEric Joyner 
3257b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, rx_que++) {
3258c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
32598eb6488eSEric Joyner 
3260c19c7afeSEric Joyner 		/* First the RX queue entry */
3261b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, rxr->me, rx_que->msix, 0);
3262c19c7afeSEric Joyner 
3263c19c7afeSEric Joyner 		/* Set an Initial EITR value */
3264b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EITR(rx_que->msix), newitr);
3265c19c7afeSEric Joyner 	}
3266b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_tx_queues; i++, tx_que++) {
3267c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
3268c19c7afeSEric Joyner 
3269c19c7afeSEric Joyner 		/* ... and the TX */
3270b1d5caf3SKevin Bowling 		ixgbe_set_ivar(sc, txr->me, tx_que->msix, 1);
3271c19c7afeSEric Joyner 	}
32728eb6488eSEric Joyner 	/* For the Link interrupt */
3273b1d5caf3SKevin Bowling 	ixgbe_set_ivar(sc, 1, sc->vector, -1);
32748eb6488eSEric Joyner } /* ixgbe_configure_ivars */
32758eb6488eSEric Joyner 
32768eb6488eSEric Joyner /************************************************************************
32778eb6488eSEric Joyner  * ixgbe_config_gpie
32788eb6488eSEric Joyner  ************************************************************************/
32798eb6488eSEric Joyner static void
3280b1d5caf3SKevin Bowling ixgbe_config_gpie(struct ixgbe_softc *sc)
32818eb6488eSEric Joyner {
3282b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
32838eb6488eSEric Joyner 	u32             gpie;
32848eb6488eSEric Joyner 
32858eb6488eSEric Joyner 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
32868eb6488eSEric Joyner 
3287b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX) {
32888eb6488eSEric Joyner 		/* Enable Enhanced MSI-X mode */
32898eb6488eSEric Joyner 		gpie |= IXGBE_GPIE_MSIX_MODE
32908eb6488eSEric Joyner 		     |  IXGBE_GPIE_EIAME
32918eb6488eSEric Joyner 		     |  IXGBE_GPIE_PBA_SUPPORT
32928eb6488eSEric Joyner 		     |  IXGBE_GPIE_OCD;
32938eb6488eSEric Joyner 	}
32948eb6488eSEric Joyner 
32958eb6488eSEric Joyner 	/* Fan Failure Interrupt */
3296b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL)
32978eb6488eSEric Joyner 		gpie |= IXGBE_SDP1_GPIEN;
32988eb6488eSEric Joyner 
32998eb6488eSEric Joyner 	/* Thermal Sensor Interrupt */
3300b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_TEMP_SENSOR)
33018eb6488eSEric Joyner 		gpie |= IXGBE_SDP0_GPIEN_X540;
33028eb6488eSEric Joyner 
33038eb6488eSEric Joyner 	/* Link detection */
33048eb6488eSEric Joyner 	switch (hw->mac.type) {
33058eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
33068eb6488eSEric Joyner 		gpie |= IXGBE_SDP1_GPIEN | IXGBE_SDP2_GPIEN;
33078eb6488eSEric Joyner 		break;
33088eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
33098eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
33108eb6488eSEric Joyner 		gpie |= IXGBE_SDP0_GPIEN_X540;
33118eb6488eSEric Joyner 		break;
33128eb6488eSEric Joyner 	default:
33138eb6488eSEric Joyner 		break;
33148eb6488eSEric Joyner 	}
33158eb6488eSEric Joyner 
33168eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
33178eb6488eSEric Joyner 
33188eb6488eSEric Joyner } /* ixgbe_config_gpie */
33198eb6488eSEric Joyner 
33208eb6488eSEric Joyner /************************************************************************
33218eb6488eSEric Joyner  * ixgbe_config_delay_values
33228eb6488eSEric Joyner  *
3323b1d5caf3SKevin Bowling  *   Requires sc->max_frame_size to be set.
33248eb6488eSEric Joyner  ************************************************************************/
33258eb6488eSEric Joyner static void
3326b1d5caf3SKevin Bowling ixgbe_config_delay_values(struct ixgbe_softc *sc)
33278eb6488eSEric Joyner {
3328b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
33298eb6488eSEric Joyner 	u32             rxpb, frame, size, tmp;
33308eb6488eSEric Joyner 
3331b1d5caf3SKevin Bowling 	frame = sc->max_frame_size;
33328eb6488eSEric Joyner 
33338eb6488eSEric Joyner 	/* Calculate High Water */
33348eb6488eSEric Joyner 	switch (hw->mac.type) {
33358eb6488eSEric Joyner 	case ixgbe_mac_X540:
33368eb6488eSEric Joyner 	case ixgbe_mac_X550:
33378eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
33388eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
33398eb6488eSEric Joyner 		tmp = IXGBE_DV_X540(frame, frame);
33408eb6488eSEric Joyner 		break;
33418eb6488eSEric Joyner 	default:
33428eb6488eSEric Joyner 		tmp = IXGBE_DV(frame, frame);
33438eb6488eSEric Joyner 		break;
33448eb6488eSEric Joyner 	}
33458eb6488eSEric Joyner 	size = IXGBE_BT2KB(tmp);
33468eb6488eSEric Joyner 	rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
33478eb6488eSEric Joyner 	hw->fc.high_water[0] = rxpb - size;
33488eb6488eSEric Joyner 
33498eb6488eSEric Joyner 	/* Now calculate Low Water */
33508eb6488eSEric Joyner 	switch (hw->mac.type) {
33518eb6488eSEric Joyner 	case ixgbe_mac_X540:
33528eb6488eSEric Joyner 	case ixgbe_mac_X550:
33538eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
33548eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
33558eb6488eSEric Joyner 		tmp = IXGBE_LOW_DV_X540(frame);
33568eb6488eSEric Joyner 		break;
33578eb6488eSEric Joyner 	default:
33588eb6488eSEric Joyner 		tmp = IXGBE_LOW_DV(frame);
33598eb6488eSEric Joyner 		break;
33608eb6488eSEric Joyner 	}
33618eb6488eSEric Joyner 	hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
33628eb6488eSEric Joyner 
33638eb6488eSEric Joyner 	hw->fc.pause_time = IXGBE_FC_PAUSE;
336479b36ec9SKevin Bowling 	hw->fc.send_xon = true;
33658eb6488eSEric Joyner } /* ixgbe_config_delay_values */
33668eb6488eSEric Joyner 
33678eb6488eSEric Joyner /************************************************************************
33688eb6488eSEric Joyner  * ixgbe_set_multi - Multicast Update
33698eb6488eSEric Joyner  *
33708eb6488eSEric Joyner  *   Called whenever multicast address list is updated.
33718eb6488eSEric Joyner  ************************************************************************/
3372ba76aa63SGleb Smirnoff static u_int
337321afed4bSKevin Bowling ixgbe_mc_filter_apply(void *arg, struct sockaddr_dl *sdl, u_int idx)
33748eb6488eSEric Joyner {
3375b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = arg;
3376b1d5caf3SKevin Bowling 	struct ixgbe_mc_addr *mta = sc->mta;
3377c19c7afeSEric Joyner 
337821afed4bSKevin Bowling 	if (idx == MAX_NUM_MULTICAST_ADDRESSES)
3379c19c7afeSEric Joyner 		return (0);
338021afed4bSKevin Bowling 	bcopy(LLADDR(sdl), mta[idx].addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
3381b1d5caf3SKevin Bowling 	mta[idx].vmdq = sc->pool;
3382c19c7afeSEric Joyner 
3383c19c7afeSEric Joyner 	return (1);
3384c19c7afeSEric Joyner } /* ixgbe_mc_filter_apply */
3385c19c7afeSEric Joyner 
3386c19c7afeSEric Joyner static void
3387c19c7afeSEric Joyner ixgbe_if_multi_set(if_ctx_t ctx)
3388c19c7afeSEric Joyner {
3389b1d5caf3SKevin Bowling 	struct ixgbe_softc       *sc = iflib_get_softc(ctx);
33908eb6488eSEric Joyner 	struct ixgbe_mc_addr *mta;
3391c19c7afeSEric Joyner 	struct ifnet         *ifp = iflib_get_ifp(ctx);
33928eb6488eSEric Joyner 	u8                   *update_ptr;
33938eb6488eSEric Joyner 	u32                  fctrl;
3394ba76aa63SGleb Smirnoff 	u_int		     mcnt;
33958eb6488eSEric Joyner 
3396c19c7afeSEric Joyner 	IOCTL_DEBUGOUT("ixgbe_if_multi_set: begin");
33978eb6488eSEric Joyner 
3398b1d5caf3SKevin Bowling 	mta = sc->mta;
33998eb6488eSEric Joyner 	bzero(mta, sizeof(*mta) * MAX_NUM_MULTICAST_ADDRESSES);
34008eb6488eSEric Joyner 
3401b1d5caf3SKevin Bowling 	mcnt = if_foreach_llmaddr(iflib_get_ifp(ctx), ixgbe_mc_filter_apply, sc);
34028eb6488eSEric Joyner 
3403*395cc55dSKevin Bowling 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES) {
3404*395cc55dSKevin Bowling 		update_ptr = (u8 *)mta;
3405*395cc55dSKevin Bowling 		ixgbe_update_mc_addr_list(&sc->hw, update_ptr, mcnt,
3406*395cc55dSKevin Bowling 		    ixgbe_mc_array_itr, true);
3407*395cc55dSKevin Bowling 	}
3408*395cc55dSKevin Bowling 
3409b1d5caf3SKevin Bowling 	fctrl = IXGBE_READ_REG(&sc->hw, IXGBE_FCTRL);
3410deecaa14SKevin Bowling 
34118eb6488eSEric Joyner 	if (ifp->if_flags & IFF_PROMISC)
34128eb6488eSEric Joyner 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
34138eb6488eSEric Joyner 	else if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES ||
34148eb6488eSEric Joyner 	    ifp->if_flags & IFF_ALLMULTI) {
34158eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_MPE;
34168eb6488eSEric Joyner 		fctrl &= ~IXGBE_FCTRL_UPE;
34178eb6488eSEric Joyner 	} else
34188eb6488eSEric Joyner 		fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
34198eb6488eSEric Joyner 
3420b1d5caf3SKevin Bowling 	IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, fctrl);
3421c19c7afeSEric Joyner } /* ixgbe_if_multi_set */
34228eb6488eSEric Joyner 
34238eb6488eSEric Joyner /************************************************************************
34248eb6488eSEric Joyner  * ixgbe_mc_array_itr
34258eb6488eSEric Joyner  *
34268eb6488eSEric Joyner  *   An iterator function needed by the multicast shared code.
34278eb6488eSEric Joyner  *   It feeds the shared code routine the addresses in the
34288eb6488eSEric Joyner  *   array of ixgbe_set_multi() one by one.
34298eb6488eSEric Joyner  ************************************************************************/
34308eb6488eSEric Joyner static u8 *
34318eb6488eSEric Joyner ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
34328eb6488eSEric Joyner {
34338eb6488eSEric Joyner 	struct ixgbe_mc_addr *mta;
34348eb6488eSEric Joyner 
34358eb6488eSEric Joyner 	mta = (struct ixgbe_mc_addr *)*update_ptr;
34368eb6488eSEric Joyner 	*vmdq = mta->vmdq;
34378eb6488eSEric Joyner 
34388eb6488eSEric Joyner 	*update_ptr = (u8*)(mta + 1);
34398eb6488eSEric Joyner 
34408eb6488eSEric Joyner 	return (mta->addr);
34418eb6488eSEric Joyner } /* ixgbe_mc_array_itr */
34428eb6488eSEric Joyner 
34438eb6488eSEric Joyner /************************************************************************
34448eb6488eSEric Joyner  * ixgbe_local_timer - Timer routine
34458eb6488eSEric Joyner  *
34468eb6488eSEric Joyner  *   Checks for link status, updates statistics,
34478eb6488eSEric Joyner  *   and runs the watchdog check.
34488eb6488eSEric Joyner  ************************************************************************/
34498eb6488eSEric Joyner static void
3450c19c7afeSEric Joyner ixgbe_if_timer(if_ctx_t ctx, uint16_t qid)
34518eb6488eSEric Joyner {
3452b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
34538eb6488eSEric Joyner 
3454c19c7afeSEric Joyner 	if (qid != 0)
3455c19c7afeSEric Joyner 		return;
34568eb6488eSEric Joyner 
34578eb6488eSEric Joyner 	/* Check for pluggable optics */
3458b1d5caf3SKevin Bowling 	if (sc->sfp_probe)
3459c19c7afeSEric Joyner 		if (!ixgbe_sfp_probe(ctx))
3460c19c7afeSEric Joyner 			return; /* Nothing to do */
34618eb6488eSEric Joyner 
3462b1d5caf3SKevin Bowling 	ixgbe_check_link(&sc->hw, &sc->link_speed, &sc->link_up, 0);
34638eb6488eSEric Joyner 
3464c19c7afeSEric Joyner 	/* Fire off the adminq task */
3465c19c7afeSEric Joyner 	iflib_admin_intr_deferred(ctx);
34668eb6488eSEric Joyner 
3467c19c7afeSEric Joyner } /* ixgbe_if_timer */
34688eb6488eSEric Joyner 
34698eb6488eSEric Joyner /************************************************************************
34708eb6488eSEric Joyner  * ixgbe_sfp_probe
34718eb6488eSEric Joyner  *
34728eb6488eSEric Joyner  *   Determine if a port had optics inserted.
34738eb6488eSEric Joyner  ************************************************************************/
34748eb6488eSEric Joyner static bool
3475c19c7afeSEric Joyner ixgbe_sfp_probe(if_ctx_t ctx)
34768eb6488eSEric Joyner {
3477b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3478b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
3479c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
348079b36ec9SKevin Bowling 	bool            result = false;
34818eb6488eSEric Joyner 
34828eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_nl) &&
34838eb6488eSEric Joyner 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
34848eb6488eSEric Joyner 		s32 ret = hw->phy.ops.identify_sfp(hw);
34858eb6488eSEric Joyner 		if (ret)
34868eb6488eSEric Joyner 			goto out;
34878eb6488eSEric Joyner 		ret = hw->phy.ops.reset(hw);
3488b1d5caf3SKevin Bowling 		sc->sfp_probe = false;
34898eb6488eSEric Joyner 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
34908eb6488eSEric Joyner 			device_printf(dev, "Unsupported SFP+ module detected!");
34918eb6488eSEric Joyner 			device_printf(dev,
34928eb6488eSEric Joyner 			    "Reload driver with supported module.\n");
34938eb6488eSEric Joyner 			goto out;
34948eb6488eSEric Joyner 		} else
34958eb6488eSEric Joyner 			device_printf(dev, "SFP+ module detected!\n");
34968eb6488eSEric Joyner 		/* We now have supported optics */
349779b36ec9SKevin Bowling 		result = true;
34988eb6488eSEric Joyner 	}
34998eb6488eSEric Joyner out:
35008eb6488eSEric Joyner 
35018eb6488eSEric Joyner 	return (result);
35028eb6488eSEric Joyner } /* ixgbe_sfp_probe */
35038eb6488eSEric Joyner 
35048eb6488eSEric Joyner /************************************************************************
35058eb6488eSEric Joyner  * ixgbe_handle_mod - Tasklet for SFP module interrupts
35068eb6488eSEric Joyner  ************************************************************************/
35078eb6488eSEric Joyner static void
3508c19c7afeSEric Joyner ixgbe_handle_mod(void *context)
35098eb6488eSEric Joyner {
3510c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3511b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3512b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
3513c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
35148eb6488eSEric Joyner 	u32             err, cage_full = 0;
35158eb6488eSEric Joyner 
3516b1d5caf3SKevin Bowling 	if (sc->hw.need_crosstalk_fix) {
35178eb6488eSEric Joyner 		switch (hw->mac.type) {
35188eb6488eSEric Joyner 		case ixgbe_mac_82599EB:
35198eb6488eSEric Joyner 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
35208eb6488eSEric Joyner 			    IXGBE_ESDP_SDP2;
35218eb6488eSEric Joyner 			break;
35228eb6488eSEric Joyner 		case ixgbe_mac_X550EM_x:
35238eb6488eSEric Joyner 		case ixgbe_mac_X550EM_a:
35248eb6488eSEric Joyner 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
35258eb6488eSEric Joyner 			    IXGBE_ESDP_SDP0;
35268eb6488eSEric Joyner 			break;
35278eb6488eSEric Joyner 		default:
35288eb6488eSEric Joyner 			break;
35298eb6488eSEric Joyner 		}
35308eb6488eSEric Joyner 
35318eb6488eSEric Joyner 		if (!cage_full)
3532c19c7afeSEric Joyner 			goto handle_mod_out;
35338eb6488eSEric Joyner 	}
35348eb6488eSEric Joyner 
35358eb6488eSEric Joyner 	err = hw->phy.ops.identify_sfp(hw);
35368eb6488eSEric Joyner 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
35378eb6488eSEric Joyner 		device_printf(dev,
35388eb6488eSEric Joyner 		    "Unsupported SFP+ module type was detected.\n");
3539c19c7afeSEric Joyner 		goto handle_mod_out;
35408eb6488eSEric Joyner 	}
35418eb6488eSEric Joyner 
3542c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
3543c19c7afeSEric Joyner 		err = hw->phy.ops.reset(hw);
3544c19c7afeSEric Joyner 	else
35458eb6488eSEric Joyner 		err = hw->mac.ops.setup_sfp(hw);
3546c19c7afeSEric Joyner 
35478eb6488eSEric Joyner 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
35488eb6488eSEric Joyner 		device_printf(dev,
35498eb6488eSEric Joyner 		    "Setup failure - unsupported SFP+ module type.\n");
3550c19c7afeSEric Joyner 		goto handle_mod_out;
35518eb6488eSEric Joyner 	}
3552b1d5caf3SKevin Bowling 	sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
3553c19c7afeSEric Joyner 	return;
3554c19c7afeSEric Joyner 
3555c19c7afeSEric Joyner handle_mod_out:
3556b1d5caf3SKevin Bowling 	sc->task_requests &= ~(IXGBE_REQUEST_TASK_MSF);
35578eb6488eSEric Joyner } /* ixgbe_handle_mod */
35588eb6488eSEric Joyner 
35598eb6488eSEric Joyner 
35608eb6488eSEric Joyner /************************************************************************
35618eb6488eSEric Joyner  * ixgbe_handle_msf - Tasklet for MSF (multispeed fiber) interrupts
35628eb6488eSEric Joyner  ************************************************************************/
35638eb6488eSEric Joyner static void
3564c19c7afeSEric Joyner ixgbe_handle_msf(void *context)
35658eb6488eSEric Joyner {
3566c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3567b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3568b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
35698eb6488eSEric Joyner 	u32             autoneg;
35708eb6488eSEric Joyner 	bool            negotiate;
35718eb6488eSEric Joyner 
35728eb6488eSEric Joyner 	/* get_supported_phy_layer will call hw->phy.ops.identify_sfp() */
3573b1d5caf3SKevin Bowling 	sc->phy_layer = ixgbe_get_supported_physical_layer(hw);
35748eb6488eSEric Joyner 
35758eb6488eSEric Joyner 	autoneg = hw->phy.autoneg_advertised;
35768eb6488eSEric Joyner 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
35778eb6488eSEric Joyner 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
35788eb6488eSEric Joyner 	if (hw->mac.ops.setup_link)
357979b36ec9SKevin Bowling 		hw->mac.ops.setup_link(hw, autoneg, true);
35808eb6488eSEric Joyner 
35818eb6488eSEric Joyner 	/* Adjust media types shown in ifconfig */
3582b1d5caf3SKevin Bowling 	ifmedia_removeall(sc->media);
3583b1d5caf3SKevin Bowling 	ixgbe_add_media_types(sc->ctx);
3584b1d5caf3SKevin Bowling 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
35858eb6488eSEric Joyner } /* ixgbe_handle_msf */
35868eb6488eSEric Joyner 
35878eb6488eSEric Joyner /************************************************************************
35888eb6488eSEric Joyner  * ixgbe_handle_phy - Tasklet for external PHY interrupts
35898eb6488eSEric Joyner  ************************************************************************/
35908eb6488eSEric Joyner static void
3591c19c7afeSEric Joyner ixgbe_handle_phy(void *context)
35928eb6488eSEric Joyner {
3593c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3594b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3595b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
35968eb6488eSEric Joyner 	int             error;
35978eb6488eSEric Joyner 
35988eb6488eSEric Joyner 	error = hw->phy.ops.handle_lasi(hw);
35998eb6488eSEric Joyner 	if (error == IXGBE_ERR_OVERTEMP)
3600b1d5caf3SKevin Bowling 		device_printf(sc->dev, "CRITICAL: EXTERNAL PHY OVER TEMP!!  PHY will downshift to lower power state!\n");
36018eb6488eSEric Joyner 	else if (error)
3602b1d5caf3SKevin Bowling 		device_printf(sc->dev,
36038eb6488eSEric Joyner 		    "Error handling LASI interrupt: %d\n", error);
36048eb6488eSEric Joyner } /* ixgbe_handle_phy */
36058eb6488eSEric Joyner 
36068eb6488eSEric Joyner /************************************************************************
3607c19c7afeSEric Joyner  * ixgbe_if_stop - Stop the hardware
36088eb6488eSEric Joyner  *
36098eb6488eSEric Joyner  *   Disables all traffic on the adapter by issuing a
36108eb6488eSEric Joyner  *   global reset on the MAC and deallocates TX/RX buffers.
36118eb6488eSEric Joyner  ************************************************************************/
36128eb6488eSEric Joyner static void
3613c19c7afeSEric Joyner ixgbe_if_stop(if_ctx_t ctx)
36148eb6488eSEric Joyner {
3615b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3616b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
36178eb6488eSEric Joyner 
3618c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_stop: begin\n");
36198eb6488eSEric Joyner 
36208eb6488eSEric Joyner 	ixgbe_reset_hw(hw);
362179b36ec9SKevin Bowling 	hw->adapter_stopped = false;
36228eb6488eSEric Joyner 	ixgbe_stop_adapter(hw);
36238eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82599EB)
36248eb6488eSEric Joyner 		ixgbe_stop_mac_link_on_d3_82599(hw);
36258eb6488eSEric Joyner 	/* Turn off the laser - noop with no optics */
36268eb6488eSEric Joyner 	ixgbe_disable_tx_laser(hw);
36278eb6488eSEric Joyner 
36288eb6488eSEric Joyner 	/* Update the stack */
3629b1d5caf3SKevin Bowling 	sc->link_up = false;
3630c19c7afeSEric Joyner 	ixgbe_if_update_admin_status(ctx);
36318eb6488eSEric Joyner 
36328eb6488eSEric Joyner 	/* reprogram the RAR[0] in case user changed it. */
3633b1d5caf3SKevin Bowling 	ixgbe_set_rar(&sc->hw, 0, sc->hw.mac.addr, 0, IXGBE_RAH_AV);
36348eb6488eSEric Joyner 
36358eb6488eSEric Joyner 	return;
3636c19c7afeSEric Joyner } /* ixgbe_if_stop */
36378eb6488eSEric Joyner 
36388eb6488eSEric Joyner /************************************************************************
36398eb6488eSEric Joyner  * ixgbe_update_link_status - Update OS on link state
36408eb6488eSEric Joyner  *
36418eb6488eSEric Joyner  * Note: Only updates the OS on the cached link state.
36428eb6488eSEric Joyner  *       The real check of the hardware only happens with
36438eb6488eSEric Joyner  *       a link interrupt.
36448eb6488eSEric Joyner  ************************************************************************/
36458eb6488eSEric Joyner static void
3646c19c7afeSEric Joyner ixgbe_if_update_admin_status(if_ctx_t ctx)
36478eb6488eSEric Joyner {
3648b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3649c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
36508eb6488eSEric Joyner 
3651b1d5caf3SKevin Bowling 	if (sc->link_up) {
3652b1d5caf3SKevin Bowling 		if (sc->link_active == false) {
36538eb6488eSEric Joyner 			if (bootverbose)
36548eb6488eSEric Joyner 				device_printf(dev, "Link is up %d Gbps %s \n",
3655b1d5caf3SKevin Bowling 				    ((sc->link_speed == 128) ? 10 : 1),
36568eb6488eSEric Joyner 				    "Full Duplex");
3657b1d5caf3SKevin Bowling 			sc->link_active = true;
36588eb6488eSEric Joyner 			/* Update any Flow Control changes */
3659b1d5caf3SKevin Bowling 			ixgbe_fc_enable(&sc->hw);
36608eb6488eSEric Joyner 			/* Update DMA coalescing config */
3661b1d5caf3SKevin Bowling 			ixgbe_config_dmac(sc);
3662c19c7afeSEric Joyner 			/* should actually be negotiated value */
3663c19c7afeSEric Joyner 			iflib_link_state_change(ctx, LINK_STATE_UP, IF_Gbps(10));
3664c19c7afeSEric Joyner 
3665b1d5caf3SKevin Bowling 			if (sc->feat_en & IXGBE_FEATURE_SRIOV)
3666b1d5caf3SKevin Bowling 				ixgbe_ping_all_vfs(sc);
36678eb6488eSEric Joyner 		}
36688eb6488eSEric Joyner 	} else { /* Link down */
3669b1d5caf3SKevin Bowling 		if (sc->link_active == true) {
36708eb6488eSEric Joyner 			if (bootverbose)
36718eb6488eSEric Joyner 				device_printf(dev, "Link is Down\n");
3672c19c7afeSEric Joyner 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
3673b1d5caf3SKevin Bowling 			sc->link_active = false;
3674b1d5caf3SKevin Bowling 			if (sc->feat_en & IXGBE_FEATURE_SRIOV)
3675b1d5caf3SKevin Bowling 				ixgbe_ping_all_vfs(sc);
36768eb6488eSEric Joyner 		}
36778eb6488eSEric Joyner 	}
36788eb6488eSEric Joyner 
3679b2c1e8e6SEric Joyner 	/* Handle task requests from msix_link() */
3680b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_MOD)
3681b2c1e8e6SEric Joyner 		ixgbe_handle_mod(ctx);
3682b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_MSF)
3683b2c1e8e6SEric Joyner 		ixgbe_handle_msf(ctx);
3684b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_MBX)
3685b2c1e8e6SEric Joyner 		ixgbe_handle_mbx(ctx);
3686b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_FDIR)
3687b2c1e8e6SEric Joyner 		ixgbe_reinit_fdir(ctx);
3688b1d5caf3SKevin Bowling 	if (sc->task_requests & IXGBE_REQUEST_TASK_PHY)
3689b2c1e8e6SEric Joyner 		ixgbe_handle_phy(ctx);
3690b1d5caf3SKevin Bowling 	sc->task_requests = 0;
3691c19c7afeSEric Joyner 
3692b1d5caf3SKevin Bowling 	ixgbe_update_stats_counters(sc);
3693c19c7afeSEric Joyner } /* ixgbe_if_update_admin_status */
36948eb6488eSEric Joyner 
36958eb6488eSEric Joyner /************************************************************************
36968eb6488eSEric Joyner  * ixgbe_config_dmac - Configure DMA Coalescing
36978eb6488eSEric Joyner  ************************************************************************/
36988eb6488eSEric Joyner static void
3699b1d5caf3SKevin Bowling ixgbe_config_dmac(struct ixgbe_softc *sc)
37008eb6488eSEric Joyner {
3701b1d5caf3SKevin Bowling 	struct ixgbe_hw          *hw = &sc->hw;
37028eb6488eSEric Joyner 	struct ixgbe_dmac_config *dcfg = &hw->mac.dmac_config;
37038eb6488eSEric Joyner 
37048eb6488eSEric Joyner 	if (hw->mac.type < ixgbe_mac_X550 || !hw->mac.ops.dmac_config)
37058eb6488eSEric Joyner 		return;
37068eb6488eSEric Joyner 
3707b1d5caf3SKevin Bowling 	if (dcfg->watchdog_timer ^ sc->dmac ||
3708b1d5caf3SKevin Bowling 	    dcfg->link_speed ^ sc->link_speed) {
3709b1d5caf3SKevin Bowling 		dcfg->watchdog_timer = sc->dmac;
371079b36ec9SKevin Bowling 		dcfg->fcoe_en = false;
3711b1d5caf3SKevin Bowling 		dcfg->link_speed = sc->link_speed;
37128eb6488eSEric Joyner 		dcfg->num_tcs = 1;
37138eb6488eSEric Joyner 
37148eb6488eSEric Joyner 		INIT_DEBUGOUT2("dmac settings: watchdog %d, link speed %d\n",
37158eb6488eSEric Joyner 		    dcfg->watchdog_timer, dcfg->link_speed);
37168eb6488eSEric Joyner 
37178eb6488eSEric Joyner 		hw->mac.ops.dmac_config(hw);
37188eb6488eSEric Joyner 	}
37198eb6488eSEric Joyner } /* ixgbe_config_dmac */
37208eb6488eSEric Joyner 
37218eb6488eSEric Joyner /************************************************************************
3722c19c7afeSEric Joyner  * ixgbe_if_enable_intr
37238eb6488eSEric Joyner  ************************************************************************/
3724c19c7afeSEric Joyner void
3725c19c7afeSEric Joyner ixgbe_if_enable_intr(if_ctx_t ctx)
37268eb6488eSEric Joyner {
3727b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3728b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
3729b1d5caf3SKevin Bowling 	struct ix_rx_queue *que = sc->rx_queues;
37308eb6488eSEric Joyner 	u32                mask, fwsm;
37318eb6488eSEric Joyner 
37328eb6488eSEric Joyner 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
37338eb6488eSEric Joyner 
3734b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
37358eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
37368eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
3737b1d5caf3SKevin Bowling 		/* Temperature sensor on some scs */
37388eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP0;
37398eb6488eSEric Joyner 		/* SFP+ (RX_LOS_N & MOD_ABS_N) */
37408eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP1;
37418eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP2;
37428eb6488eSEric Joyner 		break;
37438eb6488eSEric Joyner 	case ixgbe_mac_X540:
37448eb6488eSEric Joyner 		/* Detect if Thermal Sensor is enabled */
37458eb6488eSEric Joyner 		fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
37468eb6488eSEric Joyner 		if (fwsm & IXGBE_FWSM_TS_ENABLED)
37478eb6488eSEric Joyner 			mask |= IXGBE_EIMS_TS;
37488eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
37498eb6488eSEric Joyner 		break;
37508eb6488eSEric Joyner 	case ixgbe_mac_X550:
37518eb6488eSEric Joyner 		/* MAC thermal sensor is automatically enabled */
37528eb6488eSEric Joyner 		mask |= IXGBE_EIMS_TS;
37538eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
37548eb6488eSEric Joyner 		break;
37558eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
37568eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
37578eb6488eSEric Joyner 		/* Some devices use SDP0 for important information */
37588eb6488eSEric Joyner 		if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
37598eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP ||
37608eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP_N ||
37618eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
37628eb6488eSEric Joyner 			mask |= IXGBE_EIMS_GPI_SDP0_BY_MAC(hw);
37638eb6488eSEric Joyner 		if (hw->phy.type == ixgbe_phy_x550em_ext_t)
37648eb6488eSEric Joyner 			mask |= IXGBE_EICR_GPI_SDP0_X540;
37658eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
37668eb6488eSEric Joyner 		break;
37678eb6488eSEric Joyner 	default:
37688eb6488eSEric Joyner 		break;
37698eb6488eSEric Joyner 	}
37708eb6488eSEric Joyner 
37718eb6488eSEric Joyner 	/* Enable Fan Failure detection */
3772b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL)
37738eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP1;
37748eb6488eSEric Joyner 	/* Enable SR-IOV */
3775b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_SRIOV)
37768eb6488eSEric Joyner 		mask |= IXGBE_EIMS_MAILBOX;
37778eb6488eSEric Joyner 	/* Enable Flow Director */
3778b1d5caf3SKevin Bowling 	if (sc->feat_en & IXGBE_FEATURE_FDIR)
37798eb6488eSEric Joyner 		mask |= IXGBE_EIMS_FLOW_DIR;
37808eb6488eSEric Joyner 
37818eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
37828eb6488eSEric Joyner 
37838eb6488eSEric Joyner 	/* With MSI-X we use auto clear */
3784b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX) {
37858eb6488eSEric Joyner 		mask = IXGBE_EIMS_ENABLE_MASK;
37868eb6488eSEric Joyner 		/* Don't autoclear Link */
37878eb6488eSEric Joyner 		mask &= ~IXGBE_EIMS_OTHER;
37888eb6488eSEric Joyner 		mask &= ~IXGBE_EIMS_LSC;
3789b1d5caf3SKevin Bowling 		if (sc->feat_cap & IXGBE_FEATURE_SRIOV)
37908eb6488eSEric Joyner 			mask &= ~IXGBE_EIMS_MAILBOX;
37918eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
37926f37f232SEric Joyner 	}
37936f37f232SEric Joyner 
3794758cc3dcSJack F Vogel 	/*
37958eb6488eSEric Joyner 	 * Now enable all queues, this is done separately to
37968eb6488eSEric Joyner 	 * allow for handling the extended (beyond 32) MSI-X
37978eb6488eSEric Joyner 	 * vectors that can be used by 82599
3798758cc3dcSJack F Vogel 	 */
3799b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++, que++)
3800b1d5caf3SKevin Bowling 		ixgbe_enable_queue(sc, que->msix);
38018eb6488eSEric Joyner 
38028eb6488eSEric Joyner 	IXGBE_WRITE_FLUSH(hw);
38038eb6488eSEric Joyner 
3804c19c7afeSEric Joyner } /* ixgbe_if_enable_intr */
38058eb6488eSEric Joyner 
38068eb6488eSEric Joyner /************************************************************************
38078eb6488eSEric Joyner  * ixgbe_disable_intr
38088eb6488eSEric Joyner  ************************************************************************/
3809758cc3dcSJack F Vogel static void
3810c19c7afeSEric Joyner ixgbe_if_disable_intr(if_ctx_t ctx)
3811758cc3dcSJack F Vogel {
3812b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3813c19c7afeSEric Joyner 
3814b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX)
3815b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIAC, 0);
3816b1d5caf3SKevin Bowling 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
3817b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC, ~0);
38188eb6488eSEric Joyner 	} else {
3819b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC, 0xFFFF0000);
3820b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC_EX(0), ~0);
3821b1d5caf3SKevin Bowling 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC_EX(1), ~0);
38228eb6488eSEric Joyner 	}
3823b1d5caf3SKevin Bowling 	IXGBE_WRITE_FLUSH(&sc->hw);
3824758cc3dcSJack F Vogel 
3825c19c7afeSEric Joyner } /* ixgbe_if_disable_intr */
38268eb6488eSEric Joyner 
38278eb6488eSEric Joyner /************************************************************************
3828b2c1e8e6SEric Joyner  * ixgbe_link_intr_enable
3829b2c1e8e6SEric Joyner  ************************************************************************/
3830b2c1e8e6SEric Joyner static void
3831b2c1e8e6SEric Joyner ixgbe_link_intr_enable(if_ctx_t ctx)
3832b2c1e8e6SEric Joyner {
3833b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &((struct ixgbe_softc *)iflib_get_softc(ctx))->hw;
3834b2c1e8e6SEric Joyner 
3835b2c1e8e6SEric Joyner 	/* Re-enable other interrupts */
3836b2c1e8e6SEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC);
3837b2c1e8e6SEric Joyner } /* ixgbe_link_intr_enable */
3838b2c1e8e6SEric Joyner 
3839b2c1e8e6SEric Joyner /************************************************************************
3840c19c7afeSEric Joyner  * ixgbe_if_rx_queue_intr_enable
3841c19c7afeSEric Joyner  ************************************************************************/
3842c19c7afeSEric Joyner static int
3843c19c7afeSEric Joyner ixgbe_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
3844c19c7afeSEric Joyner {
3845b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3846b1d5caf3SKevin Bowling 	struct ix_rx_queue *que = &sc->rx_queues[rxqid];
3847c19c7afeSEric Joyner 
3848b1d5caf3SKevin Bowling 	ixgbe_enable_queue(sc, que->msix);
3849c19c7afeSEric Joyner 
3850c19c7afeSEric Joyner 	return (0);
3851c19c7afeSEric Joyner } /* ixgbe_if_rx_queue_intr_enable */
3852c19c7afeSEric Joyner 
3853c19c7afeSEric Joyner /************************************************************************
3854c19c7afeSEric Joyner  * ixgbe_enable_queue
38558eb6488eSEric Joyner  ************************************************************************/
38568eb6488eSEric Joyner static void
3857b1d5caf3SKevin Bowling ixgbe_enable_queue(struct ixgbe_softc *sc, u32 vector)
38588eb6488eSEric Joyner {
3859b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
38602dc2d580SEric Joyner 	u64             queue = 1ULL << vector;
3861c19c7afeSEric Joyner 	u32             mask;
3862758cc3dcSJack F Vogel 
3863c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB) {
3864c19c7afeSEric Joyner 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3865c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
3866c19c7afeSEric Joyner 	} else {
3867c19c7afeSEric Joyner 		mask = (queue & 0xFFFFFFFF);
3868c19c7afeSEric Joyner 		if (mask)
3869c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
3870c19c7afeSEric Joyner 		mask = (queue >> 32);
3871c19c7afeSEric Joyner 		if (mask)
3872c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
3873c19c7afeSEric Joyner 	}
3874c19c7afeSEric Joyner } /* ixgbe_enable_queue */
3875c19c7afeSEric Joyner 
3876c19c7afeSEric Joyner /************************************************************************
3877c19c7afeSEric Joyner  * ixgbe_disable_queue
3878c19c7afeSEric Joyner  ************************************************************************/
3879c19c7afeSEric Joyner static void
3880b1d5caf3SKevin Bowling ixgbe_disable_queue(struct ixgbe_softc *sc, u32 vector)
3881c19c7afeSEric Joyner {
3882b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
38832dc2d580SEric Joyner 	u64             queue = 1ULL << vector;
3884c19c7afeSEric Joyner 	u32             mask;
3885c19c7afeSEric Joyner 
3886c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB) {
3887c19c7afeSEric Joyner 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3888c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
3889c19c7afeSEric Joyner 	} else {
3890c19c7afeSEric Joyner 		mask = (queue & 0xFFFFFFFF);
3891c19c7afeSEric Joyner 		if (mask)
3892c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
3893c19c7afeSEric Joyner 		mask = (queue >> 32);
3894c19c7afeSEric Joyner 		if (mask)
3895c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
3896c19c7afeSEric Joyner 	}
3897c19c7afeSEric Joyner } /* ixgbe_disable_queue */
3898c19c7afeSEric Joyner 
3899c19c7afeSEric Joyner /************************************************************************
3900c19c7afeSEric Joyner  * ixgbe_intr - Legacy Interrupt Service Routine
3901c19c7afeSEric Joyner  ************************************************************************/
3902c19c7afeSEric Joyner int
3903c19c7afeSEric Joyner ixgbe_intr(void *arg)
3904c19c7afeSEric Joyner {
3905b1d5caf3SKevin Bowling 	struct ixgbe_softc     *sc = arg;
3906b1d5caf3SKevin Bowling 	struct ix_rx_queue *que = sc->rx_queues;
3907b1d5caf3SKevin Bowling 	struct ixgbe_hw    *hw = &sc->hw;
3908b1d5caf3SKevin Bowling 	if_ctx_t           ctx = sc->ctx;
3909c19c7afeSEric Joyner 	u32                eicr, eicr_mask;
3910758cc3dcSJack F Vogel 
39118eb6488eSEric Joyner 	eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
3912758cc3dcSJack F Vogel 
39138eb6488eSEric Joyner 	++que->irqs;
39148eb6488eSEric Joyner 	if (eicr == 0) {
3915c19c7afeSEric Joyner 		ixgbe_if_enable_intr(ctx);
3916c19c7afeSEric Joyner 		return (FILTER_HANDLED);
3917758cc3dcSJack F Vogel 	}
3918758cc3dcSJack F Vogel 
39198eb6488eSEric Joyner 	/* Check for fan failure */
3920c19c7afeSEric Joyner 	if ((hw->device_id == IXGBE_DEV_ID_82598AT) &&
3921c19c7afeSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP1)) {
3922b1d5caf3SKevin Bowling 		device_printf(sc->dev,
3923c19c7afeSEric Joyner 		    "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
39248eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3925758cc3dcSJack F Vogel 	}
3926758cc3dcSJack F Vogel 
39278eb6488eSEric Joyner 	/* Link status change */
3928c19c7afeSEric Joyner 	if (eicr & IXGBE_EICR_LSC) {
3929c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
3930c19c7afeSEric Joyner 		iflib_admin_intr_deferred(ctx);
3931c19c7afeSEric Joyner 	}
39328eb6488eSEric Joyner 
39338eb6488eSEric Joyner 	if (ixgbe_is_sfp(hw)) {
39348eb6488eSEric Joyner 		/* Pluggable optics-related interrupt */
39358eb6488eSEric Joyner 		if (hw->mac.type >= ixgbe_mac_X540)
39368eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
39378eb6488eSEric Joyner 		else
39388eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
39398eb6488eSEric Joyner 
39408eb6488eSEric Joyner 		if (eicr & eicr_mask) {
39418eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
3942b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
39438eb6488eSEric Joyner 		}
39448eb6488eSEric Joyner 
39458eb6488eSEric Joyner 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
39468eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
39478eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
39488eb6488eSEric Joyner 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3949b1d5caf3SKevin Bowling 			sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
39508eb6488eSEric Joyner 		}
39518eb6488eSEric Joyner 	}
39528eb6488eSEric Joyner 
39538eb6488eSEric Joyner 	/* External PHY interrupt */
39548eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
39558eb6488eSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP0_X540))
3956b1d5caf3SKevin Bowling 		sc->task_requests |= IXGBE_REQUEST_TASK_PHY;
39578eb6488eSEric Joyner 
3958c19c7afeSEric Joyner 	return (FILTER_SCHEDULE_THREAD);
3959c19c7afeSEric Joyner } /* ixgbe_intr */
39608eb6488eSEric Joyner 
39618eb6488eSEric Joyner /************************************************************************
39628eb6488eSEric Joyner  * ixgbe_free_pci_resources
39638eb6488eSEric Joyner  ************************************************************************/
39648eb6488eSEric Joyner static void
3965c19c7afeSEric Joyner ixgbe_free_pci_resources(if_ctx_t ctx)
39668eb6488eSEric Joyner {
3967b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3968b1d5caf3SKevin Bowling 	struct         ix_rx_queue *que = sc->rx_queues;
3969c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
39708eb6488eSEric Joyner 
3971b97de13aSMarius Strobl 	/* Release all MSI-X queue resources */
3972b1d5caf3SKevin Bowling 	if (sc->intr_type == IFLIB_INTR_MSIX)
3973b1d5caf3SKevin Bowling 		iflib_irq_free(ctx, &sc->irq);
3974c19c7afeSEric Joyner 
3975c19c7afeSEric Joyner 	if (que != NULL) {
3976b1d5caf3SKevin Bowling 		for (int i = 0; i < sc->num_rx_queues; i++, que++) {
3977c19c7afeSEric Joyner 			iflib_irq_free(ctx, &que->que_irq);
3978c19c7afeSEric Joyner 		}
3979c19c7afeSEric Joyner 	}
39808eb6488eSEric Joyner 
3981b1d5caf3SKevin Bowling 	if (sc->pci_mem != NULL)
3982c19c7afeSEric Joyner 		bus_release_resource(dev, SYS_RES_MEMORY,
3983b1d5caf3SKevin Bowling 		    rman_get_rid(sc->pci_mem), sc->pci_mem);
39848eb6488eSEric Joyner } /* ixgbe_free_pci_resources */
39858eb6488eSEric Joyner 
39868eb6488eSEric Joyner /************************************************************************
39878eb6488eSEric Joyner  * ixgbe_sysctl_flowcntl
39888eb6488eSEric Joyner  *
39898eb6488eSEric Joyner  *   SYSCTL wrapper around setting Flow Control
39908eb6488eSEric Joyner  ************************************************************************/
3991758cc3dcSJack F Vogel static int
3992f2c4db54SSteven Hartland ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS)
3993758cc3dcSJack F Vogel {
3994b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc;
39958eb6488eSEric Joyner 	int            error, fc;
3996758cc3dcSJack F Vogel 
3997b1d5caf3SKevin Bowling 	sc = (struct ixgbe_softc *)arg1;
3998b1d5caf3SKevin Bowling 	fc = sc->hw.fc.current_mode;
3999f2c4db54SSteven Hartland 
4000f2c4db54SSteven Hartland 	error = sysctl_handle_int(oidp, &fc, 0, req);
4001758cc3dcSJack F Vogel 	if ((error) || (req->newptr == NULL))
4002758cc3dcSJack F Vogel 		return (error);
4003758cc3dcSJack F Vogel 
4004758cc3dcSJack F Vogel 	/* Don't bother if it's not changed */
4005b1d5caf3SKevin Bowling 	if (fc == sc->hw.fc.current_mode)
4006758cc3dcSJack F Vogel 		return (0);
4007758cc3dcSJack F Vogel 
4008b1d5caf3SKevin Bowling 	return ixgbe_set_flowcntl(sc, fc);
40098eb6488eSEric Joyner } /* ixgbe_sysctl_flowcntl */
4010f2c4db54SSteven Hartland 
40118eb6488eSEric Joyner /************************************************************************
40128eb6488eSEric Joyner  * ixgbe_set_flowcntl - Set flow control
40138eb6488eSEric Joyner  *
40148eb6488eSEric Joyner  *   Flow control values:
40158eb6488eSEric Joyner  *     0 - off
40168eb6488eSEric Joyner  *     1 - rx pause
40178eb6488eSEric Joyner  *     2 - tx pause
40188eb6488eSEric Joyner  *     3 - full
40198eb6488eSEric Joyner  ************************************************************************/
4020f2c4db54SSteven Hartland static int
4021b1d5caf3SKevin Bowling ixgbe_set_flowcntl(struct ixgbe_softc *sc, int fc)
4022f2c4db54SSteven Hartland {
4023f2c4db54SSteven Hartland 	switch (fc) {
4024758cc3dcSJack F Vogel 	case ixgbe_fc_rx_pause:
4025758cc3dcSJack F Vogel 	case ixgbe_fc_tx_pause:
4026758cc3dcSJack F Vogel 	case ixgbe_fc_full:
4027b1d5caf3SKevin Bowling 		sc->hw.fc.requested_mode = fc;
4028b1d5caf3SKevin Bowling 		if (sc->num_rx_queues > 1)
4029b1d5caf3SKevin Bowling 			ixgbe_disable_rx_drop(sc);
4030758cc3dcSJack F Vogel 		break;
4031758cc3dcSJack F Vogel 	case ixgbe_fc_none:
4032b1d5caf3SKevin Bowling 		sc->hw.fc.requested_mode = ixgbe_fc_none;
4033b1d5caf3SKevin Bowling 		if (sc->num_rx_queues > 1)
4034b1d5caf3SKevin Bowling 			ixgbe_enable_rx_drop(sc);
4035758cc3dcSJack F Vogel 		break;
4036758cc3dcSJack F Vogel 	default:
4037758cc3dcSJack F Vogel 		return (EINVAL);
4038758cc3dcSJack F Vogel 	}
40398eb6488eSEric Joyner 
4040758cc3dcSJack F Vogel 	/* Don't autoneg if forcing a value */
4041b1d5caf3SKevin Bowling 	sc->hw.fc.disable_fc_autoneg = true;
4042b1d5caf3SKevin Bowling 	ixgbe_fc_enable(&sc->hw);
40438eb6488eSEric Joyner 
4044f2c4db54SSteven Hartland 	return (0);
40458eb6488eSEric Joyner } /* ixgbe_set_flowcntl */
40468eb6488eSEric Joyner 
40478eb6488eSEric Joyner /************************************************************************
40488eb6488eSEric Joyner  * ixgbe_enable_rx_drop
40498eb6488eSEric Joyner  *
40508eb6488eSEric Joyner  *   Enable the hardware to drop packets when the buffer is
40518eb6488eSEric Joyner  *   full. This is useful with multiqueue, so that no single
40528eb6488eSEric Joyner  *   queue being full stalls the entire RX engine. We only
40538eb6488eSEric Joyner  *   enable this when Multiqueue is enabled AND Flow Control
40548eb6488eSEric Joyner  *   is disabled.
40558eb6488eSEric Joyner  ************************************************************************/
40568eb6488eSEric Joyner static void
4057b1d5caf3SKevin Bowling ixgbe_enable_rx_drop(struct ixgbe_softc *sc)
40588eb6488eSEric Joyner {
4059b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
40608eb6488eSEric Joyner 	struct rx_ring  *rxr;
40618eb6488eSEric Joyner 	u32             srrctl;
40628eb6488eSEric Joyner 
4063b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++) {
4064b1d5caf3SKevin Bowling 		rxr = &sc->rx_queues[i].rxr;
40658eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
40668eb6488eSEric Joyner 		srrctl |= IXGBE_SRRCTL_DROP_EN;
40678eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
4068758cc3dcSJack F Vogel 	}
4069758cc3dcSJack F Vogel 
40708eb6488eSEric Joyner 	/* enable drop for each vf */
4071b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_vfs; i++) {
40728eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
40738eb6488eSEric Joyner 		                (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT) |
40748eb6488eSEric Joyner 		                IXGBE_QDE_ENABLE));
40758eb6488eSEric Joyner 	}
40768eb6488eSEric Joyner } /* ixgbe_enable_rx_drop */
40778eb6488eSEric Joyner 
40788eb6488eSEric Joyner /************************************************************************
40798eb6488eSEric Joyner  * ixgbe_disable_rx_drop
40808eb6488eSEric Joyner  ************************************************************************/
40818eb6488eSEric Joyner static void
4082b1d5caf3SKevin Bowling ixgbe_disable_rx_drop(struct ixgbe_softc *sc)
40838eb6488eSEric Joyner {
4084b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
40858eb6488eSEric Joyner 	struct rx_ring  *rxr;
40868eb6488eSEric Joyner 	u32             srrctl;
40878eb6488eSEric Joyner 
4088b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_rx_queues; i++) {
4089b1d5caf3SKevin Bowling 		rxr = &sc->rx_queues[i].rxr;
40908eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
40918eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_DROP_EN;
40928eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
40938eb6488eSEric Joyner 	}
40948eb6488eSEric Joyner 
40958eb6488eSEric Joyner 	/* disable drop for each vf */
4096b1d5caf3SKevin Bowling 	for (int i = 0; i < sc->num_vfs; i++) {
40978eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
40988eb6488eSEric Joyner 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT)));
40998eb6488eSEric Joyner 	}
41008eb6488eSEric Joyner } /* ixgbe_disable_rx_drop */
41018eb6488eSEric Joyner 
41028eb6488eSEric Joyner /************************************************************************
41038eb6488eSEric Joyner  * ixgbe_sysctl_advertise
41048eb6488eSEric Joyner  *
41058eb6488eSEric Joyner  *   SYSCTL wrapper around setting advertised speed
41068eb6488eSEric Joyner  ************************************************************************/
4107758cc3dcSJack F Vogel static int
4108f2c4db54SSteven Hartland ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS)
4109758cc3dcSJack F Vogel {
4110b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc;
41118eb6488eSEric Joyner 	int            error, advertise;
4112758cc3dcSJack F Vogel 
4113b1d5caf3SKevin Bowling 	sc = (struct ixgbe_softc *)arg1;
4114b1d5caf3SKevin Bowling 	advertise = sc->advertise;
4115758cc3dcSJack F Vogel 
4116f2c4db54SSteven Hartland 	error = sysctl_handle_int(oidp, &advertise, 0, req);
4117758cc3dcSJack F Vogel 	if ((error) || (req->newptr == NULL))
4118758cc3dcSJack F Vogel 		return (error);
4119758cc3dcSJack F Vogel 
4120b1d5caf3SKevin Bowling 	return ixgbe_set_advertise(sc, advertise);
41218eb6488eSEric Joyner } /* ixgbe_sysctl_advertise */
4122f2c4db54SSteven Hartland 
41238eb6488eSEric Joyner /************************************************************************
41248eb6488eSEric Joyner  * ixgbe_set_advertise - Control advertised link speed
41258eb6488eSEric Joyner  *
41268eb6488eSEric Joyner  *   Flags:
41278eb6488eSEric Joyner  *     0x1  - advertise 100 Mb
41288eb6488eSEric Joyner  *     0x2  - advertise 1G
41298eb6488eSEric Joyner  *     0x4  - advertise 10G
41308eb6488eSEric Joyner  *     0x8  - advertise 10 Mb (yes, Mb)
4131d381c807SPiotr Pietruszewski  *     0x10 - advertise 2.5G (disabled by default)
4132d381c807SPiotr Pietruszewski  *     0x20 - advertise 5G (disabled by default)
4133d381c807SPiotr Pietruszewski  *
41348eb6488eSEric Joyner  ************************************************************************/
4135f2c4db54SSteven Hartland static int
4136b1d5caf3SKevin Bowling ixgbe_set_advertise(struct ixgbe_softc *sc, int advertise)
4137f2c4db54SSteven Hartland {
4138b1d5caf3SKevin Bowling 	device_t         dev = iflib_get_dev(sc->ctx);
4139f2c4db54SSteven Hartland 	struct ixgbe_hw  *hw;
41408eb6488eSEric Joyner 	ixgbe_link_speed speed = 0;
41418eb6488eSEric Joyner 	ixgbe_link_speed link_caps = 0;
41428eb6488eSEric Joyner 	s32              err = IXGBE_NOT_IMPLEMENTED;
414379b36ec9SKevin Bowling 	bool             negotiate = false;
4144f2c4db54SSteven Hartland 
4145d775d23aSSteven Hartland 	/* Checks to validate new value */
4146b1d5caf3SKevin Bowling 	if (sc->advertise == advertise) /* no change */
4147d775d23aSSteven Hartland 		return (0);
4148d775d23aSSteven Hartland 
4149b1d5caf3SKevin Bowling 	hw = &sc->hw;
4150f2c4db54SSteven Hartland 
4151a9ca1c79SSean Bruno 	/* No speed changes for backplane media */
4152a9ca1c79SSean Bruno 	if (hw->phy.media_type == ixgbe_media_type_backplane)
4153a9ca1c79SSean Bruno 		return (ENODEV);
4154a9ca1c79SSean Bruno 
4155758cc3dcSJack F Vogel 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
4156758cc3dcSJack F Vogel 	      (hw->phy.multispeed_fiber))) {
41578eb6488eSEric Joyner 		device_printf(dev, "Advertised speed can only be set on copper or multispeed fiber media types.\n");
4158758cc3dcSJack F Vogel 		return (EINVAL);
4159758cc3dcSJack F Vogel 	}
4160758cc3dcSJack F Vogel 
4161d381c807SPiotr Pietruszewski 	if (advertise < 0x1 || advertise > 0x3F) {
4162d381c807SPiotr Pietruszewski 		device_printf(dev, "Invalid advertised speed; valid modes are 0x1 through 0x3F\n");
4163758cc3dcSJack F Vogel 		return (EINVAL);
4164758cc3dcSJack F Vogel 	}
4165758cc3dcSJack F Vogel 
41668eb6488eSEric Joyner 	if (hw->mac.ops.get_link_capabilities) {
41678eb6488eSEric Joyner 		err = hw->mac.ops.get_link_capabilities(hw, &link_caps,
41688eb6488eSEric Joyner 		    &negotiate);
41698eb6488eSEric Joyner 		if (err != IXGBE_SUCCESS) {
41708eb6488eSEric Joyner 			device_printf(dev, "Unable to determine supported advertise speeds\n");
41718eb6488eSEric Joyner 			return (ENODEV);
41728eb6488eSEric Joyner 		}
4173758cc3dcSJack F Vogel 	}
4174758cc3dcSJack F Vogel 
4175758cc3dcSJack F Vogel 	/* Set new value and report new advertised mode */
41768eb6488eSEric Joyner 	if (advertise & 0x1) {
41778eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_100_FULL)) {
41788eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 100Mb advertised speed\n");
41798eb6488eSEric Joyner 			return (EINVAL);
41808eb6488eSEric Joyner 		}
4181758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_100_FULL;
41828eb6488eSEric Joyner 	}
41838eb6488eSEric Joyner 	if (advertise & 0x2) {
41848eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_1GB_FULL)) {
41858eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 1Gb advertised speed\n");
41868eb6488eSEric Joyner 			return (EINVAL);
41878eb6488eSEric Joyner 		}
4188758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
41898eb6488eSEric Joyner 	}
41908eb6488eSEric Joyner 	if (advertise & 0x4) {
41918eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_10GB_FULL)) {
41928eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 10Gb advertised speed\n");
41938eb6488eSEric Joyner 			return (EINVAL);
41948eb6488eSEric Joyner 		}
4195758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
41968eb6488eSEric Joyner 	}
41978eb6488eSEric Joyner 	if (advertise & 0x8) {
41988eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_10_FULL)) {
41998eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 10Mb advertised speed\n");
42008eb6488eSEric Joyner 			return (EINVAL);
42018eb6488eSEric Joyner 		}
42028eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10_FULL;
42038eb6488eSEric Joyner 	}
4204d381c807SPiotr Pietruszewski 	if (advertise & 0x10) {
4205d381c807SPiotr Pietruszewski 		if (!(link_caps & IXGBE_LINK_SPEED_2_5GB_FULL)) {
4206d381c807SPiotr Pietruszewski 			device_printf(dev, "Interface does not support 2.5G advertised speed\n");
4207d381c807SPiotr Pietruszewski 			return (EINVAL);
4208d381c807SPiotr Pietruszewski 		}
4209d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
4210d381c807SPiotr Pietruszewski 	}
4211d381c807SPiotr Pietruszewski 	if (advertise & 0x20) {
4212d381c807SPiotr Pietruszewski 		if (!(link_caps & IXGBE_LINK_SPEED_5GB_FULL)) {
4213d381c807SPiotr Pietruszewski 			device_printf(dev, "Interface does not support 5G advertised speed\n");
4214d381c807SPiotr Pietruszewski 			return (EINVAL);
4215d381c807SPiotr Pietruszewski 		}
4216d381c807SPiotr Pietruszewski 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
4217d381c807SPiotr Pietruszewski 	}
4218758cc3dcSJack F Vogel 
421979b36ec9SKevin Bowling 	hw->mac.autotry_restart = true;
422079b36ec9SKevin Bowling 	hw->mac.ops.setup_link(hw, speed, true);
4221b1d5caf3SKevin Bowling 	sc->advertise = advertise;
4222758cc3dcSJack F Vogel 
4223f2c4db54SSteven Hartland 	return (0);
42248eb6488eSEric Joyner } /* ixgbe_set_advertise */
42258eb6488eSEric Joyner 
42268eb6488eSEric Joyner /************************************************************************
4227d381c807SPiotr Pietruszewski  * ixgbe_get_default_advertise - Get default advertised speed settings
42288eb6488eSEric Joyner  *
42298eb6488eSEric Joyner  *   Formatted for sysctl usage.
42308eb6488eSEric Joyner  *   Flags:
42318eb6488eSEric Joyner  *     0x1 - advertise 100 Mb
42328eb6488eSEric Joyner  *     0x2 - advertise 1G
42338eb6488eSEric Joyner  *     0x4 - advertise 10G
42348eb6488eSEric Joyner  *     0x8 - advertise 10 Mb (yes, Mb)
4235d381c807SPiotr Pietruszewski  *     0x10 - advertise 2.5G (disabled by default)
4236d381c807SPiotr Pietruszewski  *     0x20 - advertise 5G (disabled by default)
42378eb6488eSEric Joyner  ************************************************************************/
42388eb6488eSEric Joyner static int
4239d381c807SPiotr Pietruszewski ixgbe_get_default_advertise(struct ixgbe_softc *sc)
42408eb6488eSEric Joyner {
4241b1d5caf3SKevin Bowling 	struct ixgbe_hw  *hw = &sc->hw;
42428eb6488eSEric Joyner 	int              speed;
42438eb6488eSEric Joyner 	ixgbe_link_speed link_caps = 0;
42448eb6488eSEric Joyner 	s32              err;
424579b36ec9SKevin Bowling 	bool             negotiate = false;
4246758cc3dcSJack F Vogel 
4247758cc3dcSJack F Vogel 	/*
42488eb6488eSEric Joyner 	 * Advertised speed means nothing unless it's copper or
42498eb6488eSEric Joyner 	 * multi-speed fiber
4250758cc3dcSJack F Vogel 	 */
42518eb6488eSEric Joyner 	if (!(hw->phy.media_type == ixgbe_media_type_copper) &&
42528eb6488eSEric Joyner 	    !(hw->phy.multispeed_fiber))
4253758cc3dcSJack F Vogel 		return (0);
4254758cc3dcSJack F Vogel 
42558eb6488eSEric Joyner 	err = hw->mac.ops.get_link_capabilities(hw, &link_caps, &negotiate);
42568eb6488eSEric Joyner 	if (err != IXGBE_SUCCESS)
42578eb6488eSEric Joyner 		return (0);
42588eb6488eSEric Joyner 
4259d381c807SPiotr Pietruszewski 	if (hw->mac.type == ixgbe_mac_X550) {
4260d381c807SPiotr Pietruszewski 		/*
4261d381c807SPiotr Pietruszewski 		 * 2.5G and 5G autonegotiation speeds on X550
4262d381c807SPiotr Pietruszewski 		 * are disabled by default due to reported
4263d381c807SPiotr Pietruszewski 		 * interoperability issues with some switches.
4264d381c807SPiotr Pietruszewski 		 */
4265d381c807SPiotr Pietruszewski 		link_caps &= ~(IXGBE_LINK_SPEED_2_5GB_FULL |
4266d381c807SPiotr Pietruszewski 		    IXGBE_LINK_SPEED_5GB_FULL);
4267d381c807SPiotr Pietruszewski 	}
4268d381c807SPiotr Pietruszewski 
42698eb6488eSEric Joyner 	speed =
4270d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_10GB_FULL)  ? 0x4  : 0) |
4271d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_5GB_FULL)   ? 0x20 : 0) |
4272d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
4273d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_1GB_FULL)   ? 0x2  : 0) |
4274d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_100_FULL)   ? 0x1  : 0) |
4275d381c807SPiotr Pietruszewski 	    ((link_caps & IXGBE_LINK_SPEED_10_FULL)    ? 0x8  : 0);
42768eb6488eSEric Joyner 
42778eb6488eSEric Joyner 	return speed;
4278d381c807SPiotr Pietruszewski } /* ixgbe_get_default_advertise */
42798eb6488eSEric Joyner 
42808eb6488eSEric Joyner /************************************************************************
42818eb6488eSEric Joyner  * ixgbe_sysctl_dmac - Manage DMA Coalescing
42828eb6488eSEric Joyner  *
42838eb6488eSEric Joyner  *   Control values:
42848eb6488eSEric Joyner  *     0/1 - off / on (use default value of 1000)
42858eb6488eSEric Joyner  *
42868eb6488eSEric Joyner  *     Legal timer values are:
42878eb6488eSEric Joyner  *     50,100,250,500,1000,2000,5000,10000
42888eb6488eSEric Joyner  *
42898eb6488eSEric Joyner  *     Turning off interrupt moderation will also turn this off.
42908eb6488eSEric Joyner  ************************************************************************/
42916f37f232SEric Joyner static int
42926f37f232SEric Joyner ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS)
42936f37f232SEric Joyner {
4294b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4295b1d5caf3SKevin Bowling 	struct ifnet   *ifp = iflib_get_ifp(sc->ctx);
42966f37f232SEric Joyner 	int            error;
4297c19c7afeSEric Joyner 	u16            newval;
42986f37f232SEric Joyner 
4299b1d5caf3SKevin Bowling 	newval = sc->dmac;
4300c19c7afeSEric Joyner 	error = sysctl_handle_16(oidp, &newval, 0, req);
43016f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
43026f37f232SEric Joyner 		return (error);
43036f37f232SEric Joyner 
4304a9ca1c79SSean Bruno 	switch (newval) {
43056f37f232SEric Joyner 	case 0:
43066f37f232SEric Joyner 		/* Disabled */
4307b1d5caf3SKevin Bowling 		sc->dmac = 0;
43086f37f232SEric Joyner 		break;
4309a9ca1c79SSean Bruno 	case 1:
4310a9ca1c79SSean Bruno 		/* Enable and use default */
4311b1d5caf3SKevin Bowling 		sc->dmac = 1000;
43126f37f232SEric Joyner 		break;
43136f37f232SEric Joyner 	case 50:
43146f37f232SEric Joyner 	case 100:
43156f37f232SEric Joyner 	case 250:
43166f37f232SEric Joyner 	case 500:
43176f37f232SEric Joyner 	case 1000:
43186f37f232SEric Joyner 	case 2000:
43196f37f232SEric Joyner 	case 5000:
43206f37f232SEric Joyner 	case 10000:
43216f37f232SEric Joyner 		/* Legal values - allow */
4322b1d5caf3SKevin Bowling 		sc->dmac = newval;
43236f37f232SEric Joyner 		break;
43246f37f232SEric Joyner 	default:
43256f37f232SEric Joyner 		/* Do nothing, illegal value */
43266f37f232SEric Joyner 		return (EINVAL);
43276f37f232SEric Joyner 	}
43286f37f232SEric Joyner 
43296f37f232SEric Joyner 	/* Re-initialize hardware if it's already running */
43306f37f232SEric Joyner 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4331c19c7afeSEric Joyner 		ifp->if_init(ifp);
43326f37f232SEric Joyner 
43336f37f232SEric Joyner 	return (0);
43348eb6488eSEric Joyner } /* ixgbe_sysctl_dmac */
43356f37f232SEric Joyner 
4336a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
43378eb6488eSEric Joyner /************************************************************************
43388eb6488eSEric Joyner  * ixgbe_sysctl_power_state
43398eb6488eSEric Joyner  *
4340a9ca1c79SSean Bruno  *   Sysctl to test power states
4341a9ca1c79SSean Bruno  *   Values:
4342a9ca1c79SSean Bruno  *     0      - set device to D0
4343a9ca1c79SSean Bruno  *     3      - set device to D3
4344a9ca1c79SSean Bruno  *     (none) - get current device power state
43458eb6488eSEric Joyner  ************************************************************************/
4346a9ca1c79SSean Bruno static int
4347a9ca1c79SSean Bruno ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS)
4348a9ca1c79SSean Bruno {
4349b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4350b1d5caf3SKevin Bowling 	device_t       dev = sc->dev;
4351a9ca1c79SSean Bruno 	int            curr_ps, new_ps, error = 0;
4352a9ca1c79SSean Bruno 
4353a9ca1c79SSean Bruno 	curr_ps = new_ps = pci_get_powerstate(dev);
4354a9ca1c79SSean Bruno 
4355a9ca1c79SSean Bruno 	error = sysctl_handle_int(oidp, &new_ps, 0, req);
4356a9ca1c79SSean Bruno 	if ((error) || (req->newptr == NULL))
4357a9ca1c79SSean Bruno 		return (error);
4358a9ca1c79SSean Bruno 
4359a9ca1c79SSean Bruno 	if (new_ps == curr_ps)
4360a9ca1c79SSean Bruno 		return (0);
4361a9ca1c79SSean Bruno 
4362a9ca1c79SSean Bruno 	if (new_ps == 3 && curr_ps == 0)
4363a9ca1c79SSean Bruno 		error = DEVICE_SUSPEND(dev);
4364a9ca1c79SSean Bruno 	else if (new_ps == 0 && curr_ps == 3)
4365a9ca1c79SSean Bruno 		error = DEVICE_RESUME(dev);
4366a9ca1c79SSean Bruno 	else
4367a9ca1c79SSean Bruno 		return (EINVAL);
4368a9ca1c79SSean Bruno 
4369a9ca1c79SSean Bruno 	device_printf(dev, "New state: %d\n", pci_get_powerstate(dev));
4370a9ca1c79SSean Bruno 
4371a9ca1c79SSean Bruno 	return (error);
43728eb6488eSEric Joyner } /* ixgbe_sysctl_power_state */
4373a9ca1c79SSean Bruno #endif
43748eb6488eSEric Joyner 
43758eb6488eSEric Joyner /************************************************************************
43768eb6488eSEric Joyner  * ixgbe_sysctl_wol_enable
43778eb6488eSEric Joyner  *
43788eb6488eSEric Joyner  *   Sysctl to enable/disable the WoL capability,
43798eb6488eSEric Joyner  *   if supported by the adapter.
43808eb6488eSEric Joyner  *
43816f37f232SEric Joyner  *   Values:
43826f37f232SEric Joyner  *     0 - disabled
43836f37f232SEric Joyner  *     1 - enabled
43848eb6488eSEric Joyner  ************************************************************************/
43856f37f232SEric Joyner static int
43866f37f232SEric Joyner ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS)
43876f37f232SEric Joyner {
4388b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4389b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
43906f37f232SEric Joyner 	int             new_wol_enabled;
43916f37f232SEric Joyner 	int             error = 0;
43926f37f232SEric Joyner 
43936f37f232SEric Joyner 	new_wol_enabled = hw->wol_enabled;
43946f37f232SEric Joyner 	error = sysctl_handle_int(oidp, &new_wol_enabled, 0, req);
43956f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
43966f37f232SEric Joyner 		return (error);
4397a9ca1c79SSean Bruno 	new_wol_enabled = !!(new_wol_enabled);
43986f37f232SEric Joyner 	if (new_wol_enabled == hw->wol_enabled)
43996f37f232SEric Joyner 		return (0);
44006f37f232SEric Joyner 
4401b1d5caf3SKevin Bowling 	if (new_wol_enabled > 0 && !sc->wol_support)
44026f37f232SEric Joyner 		return (ENODEV);
44036f37f232SEric Joyner 	else
4404a9ca1c79SSean Bruno 		hw->wol_enabled = new_wol_enabled;
44056f37f232SEric Joyner 
44066f37f232SEric Joyner 	return (0);
44078eb6488eSEric Joyner } /* ixgbe_sysctl_wol_enable */
44086f37f232SEric Joyner 
44098eb6488eSEric Joyner /************************************************************************
44108eb6488eSEric Joyner  * ixgbe_sysctl_wufc - Wake Up Filter Control
44118eb6488eSEric Joyner  *
44126f37f232SEric Joyner  *   Sysctl to enable/disable the types of packets that the
44136f37f232SEric Joyner  *   adapter will wake up on upon receipt.
44146f37f232SEric Joyner  *   Flags:
44156f37f232SEric Joyner  *     0x1  - Link Status Change
44166f37f232SEric Joyner  *     0x2  - Magic Packet
44176f37f232SEric Joyner  *     0x4  - Direct Exact
44186f37f232SEric Joyner  *     0x8  - Directed Multicast
44196f37f232SEric Joyner  *     0x10 - Broadcast
44206f37f232SEric Joyner  *     0x20 - ARP/IPv4 Request Packet
44216f37f232SEric Joyner  *     0x40 - Direct IPv4 Packet
44226f37f232SEric Joyner  *     0x80 - Direct IPv6 Packet
44236f37f232SEric Joyner  *
44248eb6488eSEric Joyner  *   Settings not listed above will cause the sysctl to return an error.
44258eb6488eSEric Joyner  ************************************************************************/
44266f37f232SEric Joyner static int
44276f37f232SEric Joyner ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS)
44286f37f232SEric Joyner {
4429b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
44306f37f232SEric Joyner 	int            error = 0;
44316f37f232SEric Joyner 	u32            new_wufc;
44326f37f232SEric Joyner 
4433b1d5caf3SKevin Bowling 	new_wufc = sc->wufc;
44346f37f232SEric Joyner 
4435c19c7afeSEric Joyner 	error = sysctl_handle_32(oidp, &new_wufc, 0, req);
44366f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
44376f37f232SEric Joyner 		return (error);
4438b1d5caf3SKevin Bowling 	if (new_wufc == sc->wufc)
44396f37f232SEric Joyner 		return (0);
44406f37f232SEric Joyner 
44416f37f232SEric Joyner 	if (new_wufc & 0xffffff00)
44426f37f232SEric Joyner 		return (EINVAL);
44438eb6488eSEric Joyner 
44446f37f232SEric Joyner 	new_wufc &= 0xff;
4445b1d5caf3SKevin Bowling 	new_wufc |= (0xffffff & sc->wufc);
4446b1d5caf3SKevin Bowling 	sc->wufc = new_wufc;
44476f37f232SEric Joyner 
44486f37f232SEric Joyner 	return (0);
44498eb6488eSEric Joyner } /* ixgbe_sysctl_wufc */
44506f37f232SEric Joyner 
4451a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
44528eb6488eSEric Joyner /************************************************************************
44538eb6488eSEric Joyner  * ixgbe_sysctl_print_rss_config
44548eb6488eSEric Joyner  ************************************************************************/
4455a9ca1c79SSean Bruno static int
4456a9ca1c79SSean Bruno ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS)
4457a9ca1c79SSean Bruno {
4458b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4459b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
4460b1d5caf3SKevin Bowling 	device_t        dev = sc->dev;
4461a9ca1c79SSean Bruno 	struct sbuf     *buf;
44628eb6488eSEric Joyner 	int             error = 0, reta_size;
4463a9ca1c79SSean Bruno 	u32             reg;
4464a9ca1c79SSean Bruno 
4465a9ca1c79SSean Bruno 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
4466a9ca1c79SSean Bruno 	if (!buf) {
4467a9ca1c79SSean Bruno 		device_printf(dev, "Could not allocate sbuf for output.\n");
4468a9ca1c79SSean Bruno 		return (ENOMEM);
4469a9ca1c79SSean Bruno 	}
4470a9ca1c79SSean Bruno 
4471a9ca1c79SSean Bruno 	// TODO: use sbufs to make a string to print out
4472a9ca1c79SSean Bruno 	/* Set multiplier for RETA setup and table size based on MAC */
4473b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
4474a9ca1c79SSean Bruno 	case ixgbe_mac_X550:
4475a9ca1c79SSean Bruno 	case ixgbe_mac_X550EM_x:
44768eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
4477a9ca1c79SSean Bruno 		reta_size = 128;
4478a9ca1c79SSean Bruno 		break;
4479a9ca1c79SSean Bruno 	default:
4480a9ca1c79SSean Bruno 		reta_size = 32;
4481a9ca1c79SSean Bruno 		break;
4482a9ca1c79SSean Bruno 	}
4483a9ca1c79SSean Bruno 
4484a9ca1c79SSean Bruno 	/* Print out the redirection table */
4485a9ca1c79SSean Bruno 	sbuf_cat(buf, "\n");
4486a9ca1c79SSean Bruno 	for (int i = 0; i < reta_size; i++) {
4487a9ca1c79SSean Bruno 		if (i < 32) {
4488a9ca1c79SSean Bruno 			reg = IXGBE_READ_REG(hw, IXGBE_RETA(i));
4489a9ca1c79SSean Bruno 			sbuf_printf(buf, "RETA(%2d): 0x%08x\n", i, reg);
4490a9ca1c79SSean Bruno 		} else {
4491a9ca1c79SSean Bruno 			reg = IXGBE_READ_REG(hw, IXGBE_ERETA(i - 32));
4492a9ca1c79SSean Bruno 			sbuf_printf(buf, "ERETA(%2d): 0x%08x\n", i - 32, reg);
4493a9ca1c79SSean Bruno 		}
4494a9ca1c79SSean Bruno 	}
4495a9ca1c79SSean Bruno 
4496a9ca1c79SSean Bruno 	// TODO: print more config
4497a9ca1c79SSean Bruno 
4498a9ca1c79SSean Bruno 	error = sbuf_finish(buf);
4499a9ca1c79SSean Bruno 	if (error)
4500a9ca1c79SSean Bruno 		device_printf(dev, "Error finishing sbuf: %d\n", error);
4501a9ca1c79SSean Bruno 
4502a9ca1c79SSean Bruno 	sbuf_delete(buf);
45038eb6488eSEric Joyner 
4504a9ca1c79SSean Bruno 	return (0);
45058eb6488eSEric Joyner } /* ixgbe_sysctl_print_rss_config */
4506a9ca1c79SSean Bruno #endif /* IXGBE_DEBUG */
4507a9ca1c79SSean Bruno 
45088eb6488eSEric Joyner /************************************************************************
45098eb6488eSEric Joyner  * ixgbe_sysctl_phy_temp - Retrieve temperature of PHY
45108eb6488eSEric Joyner  *
45118eb6488eSEric Joyner  *   For X552/X557-AT devices using an external PHY
45128eb6488eSEric Joyner  ************************************************************************/
45138eb6488eSEric Joyner static int
45148eb6488eSEric Joyner ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS)
45158eb6488eSEric Joyner {
4516b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4517b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
45188eb6488eSEric Joyner 	u16             reg;
45198eb6488eSEric Joyner 
45208eb6488eSEric Joyner 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4521b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45228eb6488eSEric Joyner 		    "Device has no supported external thermal sensor.\n");
45238eb6488eSEric Joyner 		return (ENODEV);
45248eb6488eSEric Joyner 	}
45258eb6488eSEric Joyner 
45268eb6488eSEric Joyner 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_CURRENT_TEMP,
45278eb6488eSEric Joyner 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4528b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45298eb6488eSEric Joyner 		    "Error reading from PHY's current temperature register\n");
45308eb6488eSEric Joyner 		return (EAGAIN);
45318eb6488eSEric Joyner 	}
45328eb6488eSEric Joyner 
45338eb6488eSEric Joyner 	/* Shift temp for output */
45348eb6488eSEric Joyner 	reg = reg >> 8;
45358eb6488eSEric Joyner 
4536c19c7afeSEric Joyner 	return (sysctl_handle_16(oidp, NULL, reg, req));
45378eb6488eSEric Joyner } /* ixgbe_sysctl_phy_temp */
45388eb6488eSEric Joyner 
45398eb6488eSEric Joyner /************************************************************************
45408eb6488eSEric Joyner  * ixgbe_sysctl_phy_overtemp_occurred
45418eb6488eSEric Joyner  *
45428eb6488eSEric Joyner  *   Reports (directly from the PHY) whether the current PHY
45438eb6488eSEric Joyner  *   temperature is over the overtemp threshold.
45448eb6488eSEric Joyner  ************************************************************************/
45458eb6488eSEric Joyner static int
45468eb6488eSEric Joyner ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS)
45478eb6488eSEric Joyner {
4548b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4549b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
45508eb6488eSEric Joyner 	u16             reg;
45518eb6488eSEric Joyner 
45528eb6488eSEric Joyner 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4553b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45548eb6488eSEric Joyner 		    "Device has no supported external thermal sensor.\n");
45558eb6488eSEric Joyner 		return (ENODEV);
45568eb6488eSEric Joyner 	}
45578eb6488eSEric Joyner 
45588eb6488eSEric Joyner 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_OVERTEMP_STATUS,
45598eb6488eSEric Joyner 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4560b1d5caf3SKevin Bowling 		device_printf(iflib_get_dev(sc->ctx),
45618eb6488eSEric Joyner 		    "Error reading from PHY's temperature status register\n");
45628eb6488eSEric Joyner 		return (EAGAIN);
45638eb6488eSEric Joyner 	}
45648eb6488eSEric Joyner 
45658eb6488eSEric Joyner 	/* Get occurrence bit */
45668eb6488eSEric Joyner 	reg = !!(reg & 0x4000);
45678eb6488eSEric Joyner 
4568c19c7afeSEric Joyner 	return (sysctl_handle_16(oidp, 0, reg, req));
45698eb6488eSEric Joyner } /* ixgbe_sysctl_phy_overtemp_occurred */
45708eb6488eSEric Joyner 
45718eb6488eSEric Joyner /************************************************************************
45728eb6488eSEric Joyner  * ixgbe_sysctl_eee_state
45738eb6488eSEric Joyner  *
45748eb6488eSEric Joyner  *   Sysctl to set EEE power saving feature
45758eb6488eSEric Joyner  *   Values:
45768eb6488eSEric Joyner  *     0      - disable EEE
45778eb6488eSEric Joyner  *     1      - enable EEE
45788eb6488eSEric Joyner  *     (none) - get current device EEE state
45798eb6488eSEric Joyner  ************************************************************************/
45808eb6488eSEric Joyner static int
45818eb6488eSEric Joyner ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS)
45828eb6488eSEric Joyner {
4583b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4584b1d5caf3SKevin Bowling 	device_t       dev = sc->dev;
4585b1d5caf3SKevin Bowling 	struct ifnet   *ifp = iflib_get_ifp(sc->ctx);
45868eb6488eSEric Joyner 	int            curr_eee, new_eee, error = 0;
45878eb6488eSEric Joyner 	s32            retval;
45888eb6488eSEric Joyner 
4589b1d5caf3SKevin Bowling 	curr_eee = new_eee = !!(sc->feat_en & IXGBE_FEATURE_EEE);
45908eb6488eSEric Joyner 
45918eb6488eSEric Joyner 	error = sysctl_handle_int(oidp, &new_eee, 0, req);
45928eb6488eSEric Joyner 	if ((error) || (req->newptr == NULL))
45938eb6488eSEric Joyner 		return (error);
45948eb6488eSEric Joyner 
45958eb6488eSEric Joyner 	/* Nothing to do */
45968eb6488eSEric Joyner 	if (new_eee == curr_eee)
45978eb6488eSEric Joyner 		return (0);
45988eb6488eSEric Joyner 
45998eb6488eSEric Joyner 	/* Not supported */
4600b1d5caf3SKevin Bowling 	if (!(sc->feat_cap & IXGBE_FEATURE_EEE))
46018eb6488eSEric Joyner 		return (EINVAL);
46028eb6488eSEric Joyner 
46038eb6488eSEric Joyner 	/* Bounds checking */
46048eb6488eSEric Joyner 	if ((new_eee < 0) || (new_eee > 1))
46058eb6488eSEric Joyner 		return (EINVAL);
46068eb6488eSEric Joyner 
4607b1d5caf3SKevin Bowling 	retval = ixgbe_setup_eee(&sc->hw, new_eee);
46088eb6488eSEric Joyner 	if (retval) {
46098eb6488eSEric Joyner 		device_printf(dev, "Error in EEE setup: 0x%08X\n", retval);
46108eb6488eSEric Joyner 		return (EINVAL);
46118eb6488eSEric Joyner 	}
46128eb6488eSEric Joyner 
46138eb6488eSEric Joyner 	/* Restart auto-neg */
4614c19c7afeSEric Joyner 	ifp->if_init(ifp);
46158eb6488eSEric Joyner 
46168eb6488eSEric Joyner 	device_printf(dev, "New EEE state: %d\n", new_eee);
46178eb6488eSEric Joyner 
46188eb6488eSEric Joyner 	/* Cache new value */
46198eb6488eSEric Joyner 	if (new_eee)
4620b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_EEE;
46218eb6488eSEric Joyner 	else
4622b1d5caf3SKevin Bowling 		sc->feat_en &= ~IXGBE_FEATURE_EEE;
46238eb6488eSEric Joyner 
46248eb6488eSEric Joyner 	return (error);
46258eb6488eSEric Joyner } /* ixgbe_sysctl_eee_state */
46268eb6488eSEric Joyner 
46278eb6488eSEric Joyner /************************************************************************
46288eb6488eSEric Joyner  * ixgbe_init_device_features
46298eb6488eSEric Joyner  ************************************************************************/
46308eb6488eSEric Joyner static void
4631b1d5caf3SKevin Bowling ixgbe_init_device_features(struct ixgbe_softc *sc)
46328eb6488eSEric Joyner {
4633b1d5caf3SKevin Bowling 	sc->feat_cap = IXGBE_FEATURE_NETMAP
46348eb6488eSEric Joyner 	                  | IXGBE_FEATURE_RSS
46358eb6488eSEric Joyner 	                  | IXGBE_FEATURE_MSI
46368eb6488eSEric Joyner 	                  | IXGBE_FEATURE_MSIX
4637c19c7afeSEric Joyner 	                  | IXGBE_FEATURE_LEGACY_IRQ;
46388eb6488eSEric Joyner 
46398eb6488eSEric Joyner 	/* Set capabilities first... */
4640b1d5caf3SKevin Bowling 	switch (sc->hw.mac.type) {
46418eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
4642b1d5caf3SKevin Bowling 		if (sc->hw.device_id == IXGBE_DEV_ID_82598AT)
4643b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_FAN_FAIL;
46448eb6488eSEric Joyner 		break;
46458eb6488eSEric Joyner 	case ixgbe_mac_X540:
4646b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4647b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4648b1d5caf3SKevin Bowling 		if ((sc->hw.device_id == IXGBE_DEV_ID_X540_BYPASS) &&
4649b1d5caf3SKevin Bowling 		    (sc->hw.bus.func == 0))
4650b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_BYPASS;
46518eb6488eSEric Joyner 		break;
46528eb6488eSEric Joyner 	case ixgbe_mac_X550:
4653b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
4654b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4655b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
46568eb6488eSEric Joyner 		break;
46578eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
4658b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4659b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
46608eb6488eSEric Joyner 		break;
46618eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
4662b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4663b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4664b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
4665b1d5caf3SKevin Bowling 		if ((sc->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T) ||
4666b1d5caf3SKevin Bowling 		    (sc->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T_L)) {
4667b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
4668b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_EEE;
46698eb6488eSEric Joyner 		}
46708eb6488eSEric Joyner 		break;
46718eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
4672b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4673b1d5caf3SKevin Bowling 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4674b1d5caf3SKevin Bowling 		if ((sc->hw.device_id == IXGBE_DEV_ID_82599_BYPASS) &&
4675b1d5caf3SKevin Bowling 		    (sc->hw.bus.func == 0))
4676b1d5caf3SKevin Bowling 			sc->feat_cap |= IXGBE_FEATURE_BYPASS;
4677b1d5caf3SKevin Bowling 		if (sc->hw.device_id == IXGBE_DEV_ID_82599_QSFP_SF_QP)
4678b1d5caf3SKevin Bowling 			sc->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
46798eb6488eSEric Joyner 		break;
46808eb6488eSEric Joyner 	default:
46818eb6488eSEric Joyner 		break;
46828eb6488eSEric Joyner 	}
46838eb6488eSEric Joyner 
46848eb6488eSEric Joyner 	/* Enabled by default... */
46858eb6488eSEric Joyner 	/* Fan failure detection */
4686b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_FAN_FAIL)
4687b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_FAN_FAIL;
46888eb6488eSEric Joyner 	/* Netmap */
4689b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_NETMAP)
4690b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_NETMAP;
46918eb6488eSEric Joyner 	/* EEE */
4692b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_EEE)
4693b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_EEE;
46948eb6488eSEric Joyner 	/* Thermal Sensor */
4695b1d5caf3SKevin Bowling 	if (sc->feat_cap & IXGBE_FEATURE_TEMP_SENSOR)
4696b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_TEMP_SENSOR;
46978eb6488eSEric Joyner 
46988eb6488eSEric Joyner 	/* Enabled via global sysctl... */
46998eb6488eSEric Joyner 	/* Flow Director */
47008eb6488eSEric Joyner 	if (ixgbe_enable_fdir) {
4701b1d5caf3SKevin Bowling 		if (sc->feat_cap & IXGBE_FEATURE_FDIR)
4702b1d5caf3SKevin Bowling 			sc->feat_en |= IXGBE_FEATURE_FDIR;
47038eb6488eSEric Joyner 		else
4704b1d5caf3SKevin Bowling 			device_printf(sc->dev, "Device does not support Flow Director. Leaving disabled.");
47058eb6488eSEric Joyner 	}
47066f37f232SEric Joyner 	/*
47078eb6488eSEric Joyner 	 * Message Signal Interrupts - Extended (MSI-X)
47088eb6488eSEric Joyner 	 * Normal MSI is only enabled if MSI-X calls fail.
4709758cc3dcSJack F Vogel 	 */
47108eb6488eSEric Joyner 	if (!ixgbe_enable_msix)
4711b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_MSIX;
47128eb6488eSEric Joyner 	/* Receive-Side Scaling (RSS) */
4713b1d5caf3SKevin Bowling 	if ((sc->feat_cap & IXGBE_FEATURE_RSS) && ixgbe_enable_rss)
4714b1d5caf3SKevin Bowling 		sc->feat_en |= IXGBE_FEATURE_RSS;
47158eb6488eSEric Joyner 
47168eb6488eSEric Joyner 	/* Disable features with unmet dependencies... */
47178eb6488eSEric Joyner 	/* No MSI-X */
4718b1d5caf3SKevin Bowling 	if (!(sc->feat_cap & IXGBE_FEATURE_MSIX)) {
4719b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_RSS;
4720b1d5caf3SKevin Bowling 		sc->feat_cap &= ~IXGBE_FEATURE_SRIOV;
4721b1d5caf3SKevin Bowling 		sc->feat_en &= ~IXGBE_FEATURE_RSS;
4722b1d5caf3SKevin Bowling 		sc->feat_en &= ~IXGBE_FEATURE_SRIOV;
47238eb6488eSEric Joyner 	}
47248eb6488eSEric Joyner } /* ixgbe_init_device_features */
47258eb6488eSEric Joyner 
47268eb6488eSEric Joyner /************************************************************************
47278eb6488eSEric Joyner  * ixgbe_check_fan_failure
47288eb6488eSEric Joyner  ************************************************************************/
47298eb6488eSEric Joyner static void
4730b1d5caf3SKevin Bowling ixgbe_check_fan_failure(struct ixgbe_softc *sc, u32 reg, bool in_interrupt)
47318eb6488eSEric Joyner {
47328eb6488eSEric Joyner 	u32 mask;
47338eb6488eSEric Joyner 
4734b1d5caf3SKevin Bowling 	mask = (in_interrupt) ? IXGBE_EICR_GPI_SDP1_BY_MAC(&sc->hw) :
47358eb6488eSEric Joyner 	    IXGBE_ESDP_SDP1;
47368eb6488eSEric Joyner 
47378eb6488eSEric Joyner 	if (reg & mask)
4738b1d5caf3SKevin Bowling 		device_printf(sc->dev, "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
47398eb6488eSEric Joyner } /* ixgbe_check_fan_failure */
47407660e4eaSKevin Bowling 
47417660e4eaSKevin Bowling /************************************************************************
47427660e4eaSKevin Bowling  * ixgbe_sbuf_fw_version
47437660e4eaSKevin Bowling  ************************************************************************/
47447660e4eaSKevin Bowling static void
47457660e4eaSKevin Bowling ixgbe_sbuf_fw_version(struct ixgbe_hw *hw, struct sbuf *buf)
47467660e4eaSKevin Bowling {
47477660e4eaSKevin Bowling 	struct ixgbe_nvm_version nvm_ver = {0};
47487660e4eaSKevin Bowling 	uint16_t phyfw = 0;
47497660e4eaSKevin Bowling 	int status;
47505de5419bSKevin Bowling 	const char *space = "";
47517660e4eaSKevin Bowling 
47527660e4eaSKevin Bowling 	ixgbe_get_oem_prod_version(hw, &nvm_ver); /* OEM's NVM version */
47537660e4eaSKevin Bowling 	ixgbe_get_orom_version(hw, &nvm_ver); /* Option ROM */
47547660e4eaSKevin Bowling 	ixgbe_get_etk_id(hw, &nvm_ver); /* eTrack identifies a build in Intel's SCM */
47557660e4eaSKevin Bowling 	status = ixgbe_get_phy_firmware_version(hw, &phyfw);
47567660e4eaSKevin Bowling 
47575de5419bSKevin Bowling 	if (nvm_ver.oem_valid) {
47587660e4eaSKevin Bowling 		sbuf_printf(buf, "NVM OEM V%d.%d R%d", nvm_ver.oem_major,
47597660e4eaSKevin Bowling 		    nvm_ver.oem_minor, nvm_ver.oem_release);
47605de5419bSKevin Bowling 		space = " ";
47615de5419bSKevin Bowling 	}
47627660e4eaSKevin Bowling 
47635de5419bSKevin Bowling 	if (nvm_ver.or_valid) {
47645de5419bSKevin Bowling 		sbuf_printf(buf, "%sOption ROM V%d-b%d-p%d",
47655de5419bSKevin Bowling 		    space, nvm_ver.or_major, nvm_ver.or_build, nvm_ver.or_patch);
47665de5419bSKevin Bowling 		space = " ";
47675de5419bSKevin Bowling 	}
47687660e4eaSKevin Bowling 
47695de5419bSKevin Bowling 	if (nvm_ver.etk_id != ((NVM_VER_INVALID << NVM_ETK_SHIFT) |
47705de5419bSKevin Bowling 	    NVM_VER_INVALID)) {
47715de5419bSKevin Bowling 		sbuf_printf(buf, "%seTrack 0x%08x", space, nvm_ver.etk_id);
47725de5419bSKevin Bowling 		space = " ";
47735de5419bSKevin Bowling 	}
47747660e4eaSKevin Bowling 
47757660e4eaSKevin Bowling 	if (phyfw != 0 && status == IXGBE_SUCCESS)
47765de5419bSKevin Bowling 		sbuf_printf(buf, "%sPHY FW V%d", space, phyfw);
47777660e4eaSKevin Bowling } /* ixgbe_sbuf_fw_version */
47787660e4eaSKevin Bowling 
47797660e4eaSKevin Bowling /************************************************************************
47807660e4eaSKevin Bowling  * ixgbe_print_fw_version
47817660e4eaSKevin Bowling  ************************************************************************/
47827660e4eaSKevin Bowling static void
47837660e4eaSKevin Bowling ixgbe_print_fw_version(if_ctx_t ctx)
47847660e4eaSKevin Bowling {
4785b1d5caf3SKevin Bowling 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
4786b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
4787b1d5caf3SKevin Bowling 	device_t dev = sc->dev;
47887660e4eaSKevin Bowling 	struct sbuf *buf;
47897660e4eaSKevin Bowling 	int error = 0;
47907660e4eaSKevin Bowling 
47917660e4eaSKevin Bowling 	buf = sbuf_new_auto();
47927660e4eaSKevin Bowling 	if (!buf) {
47937660e4eaSKevin Bowling 		device_printf(dev, "Could not allocate sbuf for output.\n");
47947660e4eaSKevin Bowling 		return;
47957660e4eaSKevin Bowling 	}
47967660e4eaSKevin Bowling 
47977660e4eaSKevin Bowling 	ixgbe_sbuf_fw_version(hw, buf);
47987660e4eaSKevin Bowling 
47997660e4eaSKevin Bowling 	error = sbuf_finish(buf);
48007660e4eaSKevin Bowling 	if (error)
48017660e4eaSKevin Bowling 		device_printf(dev, "Error finishing sbuf: %d\n", error);
48027660e4eaSKevin Bowling 	else if (sbuf_len(buf))
48037660e4eaSKevin Bowling 		device_printf(dev, "%s\n", sbuf_data(buf));
48047660e4eaSKevin Bowling 
48057660e4eaSKevin Bowling 	sbuf_delete(buf);
48067660e4eaSKevin Bowling } /* ixgbe_print_fw_version */
48077660e4eaSKevin Bowling 
48087660e4eaSKevin Bowling /************************************************************************
48097660e4eaSKevin Bowling  * ixgbe_sysctl_print_fw_version
48107660e4eaSKevin Bowling  ************************************************************************/
48117660e4eaSKevin Bowling static int
48127660e4eaSKevin Bowling ixgbe_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)
48137660e4eaSKevin Bowling {
4814b1d5caf3SKevin Bowling 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4815b1d5caf3SKevin Bowling 	struct ixgbe_hw *hw = &sc->hw;
4816b1d5caf3SKevin Bowling 	device_t dev = sc->dev;
48177660e4eaSKevin Bowling 	struct sbuf *buf;
48187660e4eaSKevin Bowling 	int error = 0;
48197660e4eaSKevin Bowling 
48207660e4eaSKevin Bowling 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
48217660e4eaSKevin Bowling 	if (!buf) {
48227660e4eaSKevin Bowling 		device_printf(dev, "Could not allocate sbuf for output.\n");
48237660e4eaSKevin Bowling 		return (ENOMEM);
48247660e4eaSKevin Bowling 	}
48257660e4eaSKevin Bowling 
48267660e4eaSKevin Bowling 	ixgbe_sbuf_fw_version(hw, buf);
48277660e4eaSKevin Bowling 
48287660e4eaSKevin Bowling 	error = sbuf_finish(buf);
48297660e4eaSKevin Bowling 	if (error)
48307660e4eaSKevin Bowling 		device_printf(dev, "Error finishing sbuf: %d\n", error);
48317660e4eaSKevin Bowling 
48327660e4eaSKevin Bowling 	sbuf_delete(buf);
48337660e4eaSKevin Bowling 
48347660e4eaSKevin Bowling 	return (0);
48357660e4eaSKevin Bowling } /* ixgbe_sysctl_print_fw_version */
4836