xref: /freebsd/sys/dev/ixgbe/if_ix.c (revision b97de13ae097269d8eec91ffc7145ddd3ce03660)
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 
36758cc3dcSJack F Vogel #include "opt_inet.h"
37758cc3dcSJack F Vogel #include "opt_inet6.h"
38758cc3dcSJack F Vogel #include "opt_rss.h"
39758cc3dcSJack F Vogel 
40758cc3dcSJack F Vogel #include "ixgbe.h"
41c19c7afeSEric Joyner #include "ixgbe_sriov.h"
42c19c7afeSEric Joyner #include "ifdi_if.h"
43c19c7afeSEric Joyner 
44c19c7afeSEric Joyner #include <net/netmap.h>
45c19c7afeSEric Joyner #include <dev/netmap/netmap_kern.h>
46758cc3dcSJack F Vogel 
478eb6488eSEric Joyner /************************************************************************
48758cc3dcSJack F Vogel  * Driver version
498eb6488eSEric Joyner  ************************************************************************/
507d48aa4cSEric Joyner char ixgbe_driver_version[] = "4.0.1-k";
51a9ca1c79SSean Bruno 
52758cc3dcSJack F Vogel 
538eb6488eSEric Joyner /************************************************************************
54758cc3dcSJack F Vogel  * PCI Device ID Table
55758cc3dcSJack F Vogel  *
56758cc3dcSJack F Vogel  *   Used by probe to select devices to load on
57758cc3dcSJack F Vogel  *   Last field stores an index into ixgbe_strings
58758cc3dcSJack F Vogel  *   Last entry must be all 0s
59758cc3dcSJack F Vogel  *
60758cc3dcSJack F Vogel  *   { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
618eb6488eSEric Joyner  ************************************************************************/
62c19c7afeSEric Joyner static pci_vendor_info_t ixgbe_vendor_info_array[] =
63758cc3dcSJack F Vogel {
64c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_DUAL_PORT,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
65c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_SINGLE_PORT,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
66c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_CX4,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
67c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
68c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT2,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
69c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
70c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_DA_DUAL_PORT,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
71c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_CX4_DUAL_PORT,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
72c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_XF_LR,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
73c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
74c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_SFP_LOM,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
75c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
76c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4_MEZZ,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
77c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
78c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_XAUI_LOM,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
79c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_CX4,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
80c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_T3_LOM,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
81c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_COMBO_BACKPLANE,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
82c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BACKPLANE_FCOE,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
83c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF2,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
84c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_FCOE,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
85c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599EN_SFP,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
86c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF_QP,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
87c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_QSFP_SF_QP,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
88c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
89c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T1,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
90c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
91c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T1, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
92c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KR,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
93c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KX4,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
94c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_10G_T,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
957d48aa4cSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_1G_T,  "Intel(R) PRO/10GbE PCI-Express Network Driver"),
96c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_SFP, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
97c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
98c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR_L, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
99c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
100c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP_N, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
101c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
102c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII_L, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
103c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_10G_T, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
104c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
105c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T_L, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
106c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540_BYPASS, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
107c19c7afeSEric Joyner   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BYPASS, "Intel(R) PRO/10GbE PCI-Express Network Driver"),
108758cc3dcSJack F Vogel 	/* required last entry */
109c19c7afeSEric Joyner   PVID_END
110758cc3dcSJack F Vogel };
111758cc3dcSJack F Vogel 
112c19c7afeSEric Joyner static void *ixgbe_register(device_t dev);
113c19c7afeSEric Joyner static int  ixgbe_if_attach_pre(if_ctx_t ctx);
114c19c7afeSEric Joyner static int  ixgbe_if_attach_post(if_ctx_t ctx);
115c19c7afeSEric Joyner static int  ixgbe_if_detach(if_ctx_t ctx);
116c19c7afeSEric Joyner static int  ixgbe_if_shutdown(if_ctx_t ctx);
117c19c7afeSEric Joyner static int  ixgbe_if_suspend(if_ctx_t ctx);
118c19c7afeSEric Joyner static int  ixgbe_if_resume(if_ctx_t ctx);
119c19c7afeSEric Joyner 
120c19c7afeSEric Joyner static void ixgbe_if_stop(if_ctx_t ctx);
121c19c7afeSEric Joyner void ixgbe_if_enable_intr(if_ctx_t ctx);
122c19c7afeSEric Joyner static void ixgbe_if_disable_intr(if_ctx_t ctx);
123c19c7afeSEric Joyner static int  ixgbe_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t qid);
124c19c7afeSEric Joyner static void ixgbe_if_media_status(if_ctx_t ctx, struct ifmediareq * ifmr);
125c19c7afeSEric Joyner static int  ixgbe_if_media_change(if_ctx_t ctx);
126c19c7afeSEric Joyner static int  ixgbe_if_msix_intr_assign(if_ctx_t, int);
127c19c7afeSEric Joyner static int  ixgbe_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
128c19c7afeSEric Joyner static void ixgbe_if_crcstrip_set(if_ctx_t ctx, int onoff, int strip);
129c19c7afeSEric Joyner static void ixgbe_if_multi_set(if_ctx_t ctx);
130c19c7afeSEric Joyner static int  ixgbe_if_promisc_set(if_ctx_t ctx, int flags);
131c19c7afeSEric Joyner static int  ixgbe_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
132c19c7afeSEric Joyner                                      uint64_t *paddrs, int nrxqs, int nrxqsets);
133c19c7afeSEric Joyner static int  ixgbe_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
134c19c7afeSEric Joyner                                      uint64_t *paddrs, int nrxqs, int nrxqsets);
135c19c7afeSEric Joyner static void ixgbe_if_queues_free(if_ctx_t ctx);
136c19c7afeSEric Joyner static void ixgbe_if_timer(if_ctx_t ctx, uint16_t);
137c19c7afeSEric Joyner static void ixgbe_if_update_admin_status(if_ctx_t ctx);
138c19c7afeSEric Joyner static void ixgbe_if_vlan_register(if_ctx_t ctx, u16 vtag);
139c19c7afeSEric Joyner static void ixgbe_if_vlan_unregister(if_ctx_t ctx, u16 vtag);
140bca38080SAndrew Gallatin static int  ixgbe_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req);
141c19c7afeSEric Joyner int ixgbe_intr(void *arg);
142758cc3dcSJack F Vogel 
1438eb6488eSEric Joyner /************************************************************************
144758cc3dcSJack F Vogel  * Function prototypes
1458eb6488eSEric Joyner  ************************************************************************/
146758cc3dcSJack F Vogel #if __FreeBSD_version >= 1100036
147c19c7afeSEric Joyner static uint64_t ixgbe_if_get_counter(if_ctx_t, ift_counter);
148758cc3dcSJack F Vogel #endif
149758cc3dcSJack F Vogel 
150c19c7afeSEric Joyner static void ixgbe_enable_queue(struct adapter *adapter, u32 vector);
151c19c7afeSEric Joyner static void ixgbe_disable_queue(struct adapter *adapter, u32 vector);
152c19c7afeSEric Joyner static void ixgbe_add_device_sysctls(if_ctx_t ctx);
153c19c7afeSEric Joyner static int  ixgbe_allocate_pci_resources(if_ctx_t ctx);
154c19c7afeSEric Joyner static int  ixgbe_setup_low_power_mode(if_ctx_t ctx);
155c19c7afeSEric Joyner 
156c19c7afeSEric Joyner static void ixgbe_config_dmac(struct adapter *adapter);
157c19c7afeSEric Joyner static void ixgbe_configure_ivars(struct adapter *adapter);
158c19c7afeSEric Joyner static void ixgbe_set_ivar(struct adapter *adapter, u8 entry, u8 vector,
159c19c7afeSEric Joyner                            s8 type);
160c19c7afeSEric Joyner static u8   *ixgbe_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
161c19c7afeSEric Joyner static bool ixgbe_sfp_probe(if_ctx_t ctx);
162c19c7afeSEric Joyner 
163c19c7afeSEric Joyner static void ixgbe_free_pci_resources(if_ctx_t ctx);
164c19c7afeSEric Joyner 
165c19c7afeSEric Joyner static int  ixgbe_msix_link(void *arg);
166c19c7afeSEric Joyner static int  ixgbe_msix_que(void *arg);
167c19c7afeSEric Joyner static void ixgbe_initialize_rss_mapping(struct adapter *adapter);
168c19c7afeSEric Joyner static void ixgbe_initialize_receive_units(if_ctx_t ctx);
169c19c7afeSEric Joyner static void ixgbe_initialize_transmit_units(if_ctx_t ctx);
170c19c7afeSEric Joyner 
171c19c7afeSEric Joyner static int  ixgbe_setup_interface(if_ctx_t ctx);
172c19c7afeSEric Joyner static void ixgbe_init_device_features(struct adapter *adapter);
173c19c7afeSEric Joyner static void ixgbe_check_fan_failure(struct adapter *, u32, bool);
174c19c7afeSEric Joyner static void ixgbe_add_media_types(if_ctx_t ctx);
175c19c7afeSEric Joyner static void ixgbe_update_stats_counters(struct adapter *adapter);
176c19c7afeSEric Joyner static void ixgbe_config_link(struct adapter *adapter);
177c19c7afeSEric Joyner static void ixgbe_get_slot_info(struct adapter *);
178c19c7afeSEric Joyner static void ixgbe_check_wol_support(struct adapter *adapter);
179758cc3dcSJack F Vogel static void ixgbe_enable_rx_drop(struct adapter *);
180758cc3dcSJack F Vogel static void ixgbe_disable_rx_drop(struct adapter *);
181758cc3dcSJack F Vogel 
182c19c7afeSEric Joyner static void ixgbe_add_hw_stats(struct adapter *adapter);
183f2c4db54SSteven Hartland static int  ixgbe_set_flowcntl(struct adapter *, int);
184f2c4db54SSteven Hartland static int  ixgbe_set_advertise(struct adapter *, int);
1858eb6488eSEric Joyner static int  ixgbe_get_advertise(struct adapter *);
186c19c7afeSEric Joyner static void ixgbe_setup_vlan_hw_support(if_ctx_t ctx);
187c19c7afeSEric Joyner static void ixgbe_config_gpie(struct adapter *adapter);
188c19c7afeSEric Joyner static void ixgbe_config_delay_values(struct adapter *adapter);
1896f37f232SEric Joyner 
1906f37f232SEric Joyner /* Sysctl handlers */
191f2c4db54SSteven Hartland static int  ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS);
192f2c4db54SSteven Hartland static int  ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS);
1938eb6488eSEric Joyner static int  ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS);
1946f37f232SEric Joyner static int  ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS);
1956f37f232SEric Joyner static int  ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS);
1966f37f232SEric Joyner static int  ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS);
197a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
198a9ca1c79SSean Bruno static int  ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS);
199a9ca1c79SSean Bruno static int  ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS);
200a9ca1c79SSean Bruno #endif
2018eb6488eSEric Joyner static int  ixgbe_sysctl_rdh_handler(SYSCTL_HANDLER_ARGS);
2028eb6488eSEric Joyner static int  ixgbe_sysctl_rdt_handler(SYSCTL_HANDLER_ARGS);
2038eb6488eSEric Joyner static int  ixgbe_sysctl_tdt_handler(SYSCTL_HANDLER_ARGS);
2048eb6488eSEric Joyner static int  ixgbe_sysctl_tdh_handler(SYSCTL_HANDLER_ARGS);
2058eb6488eSEric Joyner static int  ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS);
2066f37f232SEric Joyner static int  ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS);
2076f37f232SEric Joyner static int  ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS);
208758cc3dcSJack F Vogel 
209758cc3dcSJack F Vogel /* Deferred interrupt tasklets */
210c19c7afeSEric Joyner static void ixgbe_handle_msf(void *);
211c19c7afeSEric Joyner static void ixgbe_handle_mod(void *);
212c19c7afeSEric Joyner static void ixgbe_handle_phy(void *);
213758cc3dcSJack F Vogel 
2148eb6488eSEric Joyner /************************************************************************
215758cc3dcSJack F Vogel  *  FreeBSD Device Interface Entry Points
2168eb6488eSEric Joyner  ************************************************************************/
217a1edda90SAdrian Chadd static device_method_t ix_methods[] = {
218758cc3dcSJack F Vogel 	/* Device interface */
219c19c7afeSEric Joyner 	DEVMETHOD(device_register, ixgbe_register),
220c19c7afeSEric Joyner 	DEVMETHOD(device_probe, iflib_device_probe),
221c19c7afeSEric Joyner 	DEVMETHOD(device_attach, iflib_device_attach),
222c19c7afeSEric Joyner 	DEVMETHOD(device_detach, iflib_device_detach),
223c19c7afeSEric Joyner 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
224c19c7afeSEric Joyner 	DEVMETHOD(device_suspend, iflib_device_suspend),
225c19c7afeSEric Joyner 	DEVMETHOD(device_resume, iflib_device_resume),
22648056c88SJack F Vogel #ifdef PCI_IOV
227c19c7afeSEric Joyner 	DEVMETHOD(pci_iov_init, iflib_device_iov_init),
228c19c7afeSEric Joyner 	DEVMETHOD(pci_iov_uninit, iflib_device_iov_uninit),
229c19c7afeSEric Joyner 	DEVMETHOD(pci_iov_add_vf, iflib_device_iov_add_vf),
23048056c88SJack F Vogel #endif /* PCI_IOV */
231758cc3dcSJack F Vogel 	DEVMETHOD_END
232758cc3dcSJack F Vogel };
233758cc3dcSJack F Vogel 
234a1edda90SAdrian Chadd static driver_t ix_driver = {
235a1edda90SAdrian Chadd 	"ix", ix_methods, sizeof(struct adapter),
236758cc3dcSJack F Vogel };
237758cc3dcSJack F Vogel 
238a1edda90SAdrian Chadd devclass_t ix_devclass;
239a1edda90SAdrian Chadd DRIVER_MODULE(ix, pci, ix_driver, ix_devclass, 0, 0);
240937b0f25SYuri Pankov IFLIB_PNP_INFO(pci, ix_driver, ixgbe_vendor_info_array);
241a1edda90SAdrian Chadd MODULE_DEPEND(ix, pci, 1, 1, 1);
242a1edda90SAdrian Chadd MODULE_DEPEND(ix, ether, 1, 1, 1);
243c19c7afeSEric Joyner MODULE_DEPEND(ix, iflib, 1, 1, 1);
244c19c7afeSEric Joyner 
245c19c7afeSEric Joyner static device_method_t ixgbe_if_methods[] = {
246c19c7afeSEric Joyner 	DEVMETHOD(ifdi_attach_pre, ixgbe_if_attach_pre),
247c19c7afeSEric Joyner 	DEVMETHOD(ifdi_attach_post, ixgbe_if_attach_post),
248c19c7afeSEric Joyner 	DEVMETHOD(ifdi_detach, ixgbe_if_detach),
249c19c7afeSEric Joyner 	DEVMETHOD(ifdi_shutdown, ixgbe_if_shutdown),
250c19c7afeSEric Joyner 	DEVMETHOD(ifdi_suspend, ixgbe_if_suspend),
251c19c7afeSEric Joyner 	DEVMETHOD(ifdi_resume, ixgbe_if_resume),
252c19c7afeSEric Joyner 	DEVMETHOD(ifdi_init, ixgbe_if_init),
253c19c7afeSEric Joyner 	DEVMETHOD(ifdi_stop, ixgbe_if_stop),
254c19c7afeSEric Joyner 	DEVMETHOD(ifdi_msix_intr_assign, ixgbe_if_msix_intr_assign),
255c19c7afeSEric Joyner 	DEVMETHOD(ifdi_intr_enable, ixgbe_if_enable_intr),
256c19c7afeSEric Joyner 	DEVMETHOD(ifdi_intr_disable, ixgbe_if_disable_intr),
257c19c7afeSEric Joyner 	DEVMETHOD(ifdi_tx_queue_intr_enable, ixgbe_if_rx_queue_intr_enable),
258c19c7afeSEric Joyner 	DEVMETHOD(ifdi_rx_queue_intr_enable, ixgbe_if_rx_queue_intr_enable),
259c19c7afeSEric Joyner 	DEVMETHOD(ifdi_tx_queues_alloc, ixgbe_if_tx_queues_alloc),
260c19c7afeSEric Joyner 	DEVMETHOD(ifdi_rx_queues_alloc, ixgbe_if_rx_queues_alloc),
261c19c7afeSEric Joyner 	DEVMETHOD(ifdi_queues_free, ixgbe_if_queues_free),
262c19c7afeSEric Joyner 	DEVMETHOD(ifdi_update_admin_status, ixgbe_if_update_admin_status),
263c19c7afeSEric Joyner 	DEVMETHOD(ifdi_multi_set, ixgbe_if_multi_set),
264c19c7afeSEric Joyner 	DEVMETHOD(ifdi_mtu_set, ixgbe_if_mtu_set),
265c19c7afeSEric Joyner 	DEVMETHOD(ifdi_crcstrip_set, ixgbe_if_crcstrip_set),
266c19c7afeSEric Joyner 	DEVMETHOD(ifdi_media_status, ixgbe_if_media_status),
267c19c7afeSEric Joyner 	DEVMETHOD(ifdi_media_change, ixgbe_if_media_change),
268c19c7afeSEric Joyner 	DEVMETHOD(ifdi_promisc_set, ixgbe_if_promisc_set),
269c19c7afeSEric Joyner 	DEVMETHOD(ifdi_timer, ixgbe_if_timer),
270c19c7afeSEric Joyner 	DEVMETHOD(ifdi_vlan_register, ixgbe_if_vlan_register),
271c19c7afeSEric Joyner 	DEVMETHOD(ifdi_vlan_unregister, ixgbe_if_vlan_unregister),
272c19c7afeSEric Joyner 	DEVMETHOD(ifdi_get_counter, ixgbe_if_get_counter),
273bca38080SAndrew Gallatin 	DEVMETHOD(ifdi_i2c_req, ixgbe_if_i2c_req),
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 
2868eb6488eSEric Joyner static SYSCTL_NODE(_hw, OID_AUTO, ix, CTLFLAG_RD, 0, "IXGBE driver parameters");
287c19c7afeSEric Joyner static driver_t ixgbe_if_driver = {
288c19c7afeSEric Joyner   "ixgbe_if", ixgbe_if_methods, sizeof(struct adapter)
289c19c7afeSEric Joyner };
290758cc3dcSJack F Vogel 
291758cc3dcSJack F Vogel static int ixgbe_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
292758cc3dcSJack F Vogel SYSCTL_INT(_hw_ix, OID_AUTO, max_interrupt_rate, CTLFLAG_RDTUN,
293758cc3dcSJack F Vogel     &ixgbe_max_interrupt_rate, 0, "Maximum interrupts per second");
294758cc3dcSJack F Vogel 
295f2c4db54SSteven Hartland /* Flow control setting, default to full */
296f2c4db54SSteven Hartland static int ixgbe_flow_control = ixgbe_fc_full;
297f2c4db54SSteven Hartland SYSCTL_INT(_hw_ix, OID_AUTO, flow_control, CTLFLAG_RDTUN,
298f2c4db54SSteven Hartland     &ixgbe_flow_control, 0, "Default flow control used for all adapters");
299f2c4db54SSteven Hartland 
300f2c4db54SSteven Hartland /* Advertise Speed, default to 0 (auto) */
301f2c4db54SSteven Hartland static int ixgbe_advertise_speed = 0;
302f2c4db54SSteven Hartland SYSCTL_INT(_hw_ix, OID_AUTO, advertise_speed, CTLFLAG_RDTUN,
303f2c4db54SSteven Hartland     &ixgbe_advertise_speed, 0, "Default advertised speed for all adapters");
304f2c4db54SSteven Hartland 
305758cc3dcSJack F Vogel /*
3068eb6488eSEric Joyner  * Smart speed setting, default to on
3078eb6488eSEric Joyner  * this only works as a compile option
3088eb6488eSEric Joyner  * right now as its during attach, set
3098eb6488eSEric Joyner  * this to 'ixgbe_smart_speed_off' to
3108eb6488eSEric Joyner  * disable.
311758cc3dcSJack F Vogel  */
312758cc3dcSJack F Vogel static int ixgbe_smart_speed = ixgbe_smart_speed_on;
313758cc3dcSJack F Vogel 
314758cc3dcSJack F Vogel /*
3158eb6488eSEric Joyner  * MSI-X should be the default for best performance,
316758cc3dcSJack F Vogel  * but this allows it to be forced off for testing.
317758cc3dcSJack F Vogel  */
318758cc3dcSJack F Vogel static int ixgbe_enable_msix = 1;
319758cc3dcSJack F Vogel SYSCTL_INT(_hw_ix, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &ixgbe_enable_msix, 0,
320758cc3dcSJack F Vogel     "Enable MSI-X interrupts");
321758cc3dcSJack F Vogel 
322758cc3dcSJack F Vogel /*
3238eb6488eSEric Joyner  * Defining this on will allow the use
3248eb6488eSEric Joyner  * of unsupported SFP+ modules, note that
3258eb6488eSEric Joyner  * doing so you are on your own :)
326758cc3dcSJack F Vogel  */
327758cc3dcSJack F Vogel static int allow_unsupported_sfp = FALSE;
328fb6aa95dSSean Bruno SYSCTL_INT(_hw_ix, OID_AUTO, unsupported_sfp, CTLFLAG_RDTUN,
3298eb6488eSEric Joyner     &allow_unsupported_sfp, 0,
3308eb6488eSEric Joyner     "Allow unsupported SFP modules...use at your own risk");
3318eb6488eSEric Joyner 
3328eb6488eSEric Joyner /*
3338eb6488eSEric Joyner  * Not sure if Flow Director is fully baked,
3348eb6488eSEric Joyner  * so we'll default to turning it off.
3358eb6488eSEric Joyner  */
3368eb6488eSEric Joyner static int ixgbe_enable_fdir = 0;
3378eb6488eSEric Joyner SYSCTL_INT(_hw_ix, OID_AUTO, enable_fdir, CTLFLAG_RDTUN, &ixgbe_enable_fdir, 0,
3388eb6488eSEric Joyner     "Enable Flow Director");
3398eb6488eSEric Joyner 
3408eb6488eSEric Joyner /* Receive-Side Scaling */
3418eb6488eSEric Joyner static int ixgbe_enable_rss = 1;
3428eb6488eSEric Joyner SYSCTL_INT(_hw_ix, OID_AUTO, enable_rss, CTLFLAG_RDTUN, &ixgbe_enable_rss, 0,
3438eb6488eSEric Joyner     "Enable Receive-Side Scaling (RSS)");
344758cc3dcSJack F Vogel 
345c19c7afeSEric Joyner #if 0
346758cc3dcSJack F Vogel /* Keep running tab on them for sanity check */
347758cc3dcSJack F Vogel static int ixgbe_total_ports;
348c19c7afeSEric Joyner #endif
349758cc3dcSJack F Vogel 
3508eb6488eSEric Joyner MALLOC_DEFINE(M_IXGBE, "ix", "ix driver allocations");
351758cc3dcSJack F Vogel 
352c19c7afeSEric Joyner /*
353c19c7afeSEric Joyner  * For Flow Director: this is the number of TX packets we sample
354c19c7afeSEric Joyner  * for the filter pool, this means every 20th packet will be probed.
355c19c7afeSEric Joyner  *
356c19c7afeSEric Joyner  * This feature can be disabled by setting this to 0.
357c19c7afeSEric Joyner  */
358c19c7afeSEric Joyner static int atr_sample_rate = 20;
359c19c7afeSEric Joyner 
360c19c7afeSEric Joyner extern struct if_txrx ixgbe_txrx;
361c19c7afeSEric Joyner 
362c19c7afeSEric Joyner static struct if_shared_ctx ixgbe_sctx_init = {
363c19c7afeSEric Joyner 	.isc_magic = IFLIB_MAGIC,
364c19c7afeSEric Joyner 	.isc_q_align = PAGE_SIZE,/* max(DBA_ALIGN, PAGE_SIZE) */
3657f87c040SMarius Strobl 	.isc_tx_maxsize = IXGBE_TSO_SIZE + sizeof(struct ether_vlan_header),
366c19c7afeSEric Joyner 	.isc_tx_maxsegsize = PAGE_SIZE,
3677f87c040SMarius Strobl 	.isc_tso_maxsize = IXGBE_TSO_SIZE + sizeof(struct ether_vlan_header),
3687f87c040SMarius Strobl 	.isc_tso_maxsegsize = PAGE_SIZE,
369c19c7afeSEric Joyner 	.isc_rx_maxsize = PAGE_SIZE*4,
370c19c7afeSEric Joyner 	.isc_rx_nsegments = 1,
371c19c7afeSEric Joyner 	.isc_rx_maxsegsize = PAGE_SIZE*4,
372c19c7afeSEric Joyner 	.isc_nfl = 1,
373c19c7afeSEric Joyner 	.isc_ntxqs = 1,
374c19c7afeSEric Joyner 	.isc_nrxqs = 1,
375c19c7afeSEric Joyner 
376c19c7afeSEric Joyner 	.isc_admin_intrcnt = 1,
377c19c7afeSEric Joyner 	.isc_vendor_info = ixgbe_vendor_info_array,
378c19c7afeSEric Joyner 	.isc_driver_version = ixgbe_driver_version,
379c19c7afeSEric Joyner 	.isc_driver = &ixgbe_if_driver,
380c19c7afeSEric Joyner 
381c19c7afeSEric Joyner 	.isc_nrxd_min = {MIN_RXD},
382c19c7afeSEric Joyner 	.isc_ntxd_min = {MIN_TXD},
383c19c7afeSEric Joyner 	.isc_nrxd_max = {MAX_RXD},
384c19c7afeSEric Joyner 	.isc_ntxd_max = {MAX_TXD},
385c19c7afeSEric Joyner 	.isc_nrxd_default = {DEFAULT_RXD},
386c19c7afeSEric Joyner 	.isc_ntxd_default = {DEFAULT_TXD},
387c19c7afeSEric Joyner };
388c19c7afeSEric Joyner 
389c19c7afeSEric Joyner if_shared_ctx_t ixgbe_sctx = &ixgbe_sctx_init;
390c19c7afeSEric Joyner 
391c19c7afeSEric Joyner /************************************************************************
392c19c7afeSEric Joyner  * ixgbe_if_tx_queues_alloc
393c19c7afeSEric Joyner  ************************************************************************/
394c19c7afeSEric Joyner static int
395c19c7afeSEric Joyner ixgbe_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
396c19c7afeSEric Joyner                          int ntxqs, int ntxqsets)
397c19c7afeSEric Joyner {
398c19c7afeSEric Joyner 	struct adapter     *adapter = iflib_get_softc(ctx);
399c19c7afeSEric Joyner 	if_softc_ctx_t     scctx = adapter->shared;
400c19c7afeSEric Joyner 	struct ix_tx_queue *que;
401c19c7afeSEric Joyner 	int                i, j, error;
402c19c7afeSEric Joyner 
403c19c7afeSEric Joyner 	MPASS(adapter->num_tx_queues > 0);
404c19c7afeSEric Joyner 	MPASS(adapter->num_tx_queues == ntxqsets);
405c19c7afeSEric Joyner 	MPASS(ntxqs == 1);
406c19c7afeSEric Joyner 
407c19c7afeSEric Joyner 	/* Allocate queue structure memory */
408c19c7afeSEric Joyner 	adapter->tx_queues =
409c19c7afeSEric Joyner 	    (struct ix_tx_queue *)malloc(sizeof(struct ix_tx_queue) * ntxqsets,
410c19c7afeSEric Joyner 	                                 M_IXGBE, M_NOWAIT | M_ZERO);
411c19c7afeSEric Joyner 	if (!adapter->tx_queues) {
412c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
413c19c7afeSEric Joyner 		    "Unable to allocate TX ring memory\n");
414c19c7afeSEric Joyner 		return (ENOMEM);
415c19c7afeSEric Joyner 	}
416c19c7afeSEric Joyner 
417c19c7afeSEric Joyner 	for (i = 0, que = adapter->tx_queues; i < ntxqsets; i++, que++) {
418c19c7afeSEric Joyner 		struct tx_ring *txr = &que->txr;
419c19c7afeSEric Joyner 
420c19c7afeSEric Joyner 		/* In case SR-IOV is enabled, align the index properly */
421c19c7afeSEric Joyner 		txr->me = ixgbe_vf_que_index(adapter->iov_mode, adapter->pool,
422c19c7afeSEric Joyner 		    i);
423c19c7afeSEric Joyner 
424c19c7afeSEric Joyner 		txr->adapter = que->adapter = adapter;
425c19c7afeSEric Joyner 		adapter->active_queues |= (u64)1 << txr->me;
426c19c7afeSEric Joyner 
427c19c7afeSEric Joyner 		/* Allocate report status array */
428c19c7afeSEric Joyner 		txr->tx_rsq = (qidx_t *)malloc(sizeof(qidx_t) * scctx->isc_ntxd[0], M_IXGBE, M_NOWAIT | M_ZERO);
429c19c7afeSEric Joyner 		if (txr->tx_rsq == NULL) {
430c19c7afeSEric Joyner 			error = ENOMEM;
431c19c7afeSEric Joyner 			goto fail;
432c19c7afeSEric Joyner 		}
433c19c7afeSEric Joyner 		for (j = 0; j < scctx->isc_ntxd[0]; j++)
434c19c7afeSEric Joyner 			txr->tx_rsq[j] = QIDX_INVALID;
435c19c7afeSEric Joyner 		/* get the virtual and physical address of the hardware queues */
436c19c7afeSEric Joyner 		txr->tail = IXGBE_TDT(txr->me);
437c19c7afeSEric Joyner 		txr->tx_base = (union ixgbe_adv_tx_desc *)vaddrs[i];
438c19c7afeSEric Joyner 		txr->tx_paddr = paddrs[i];
439c19c7afeSEric Joyner 
440c19c7afeSEric Joyner 		txr->bytes = 0;
441c19c7afeSEric Joyner 		txr->total_packets = 0;
442c19c7afeSEric Joyner 
443c19c7afeSEric Joyner 		/* Set the rate at which we sample packets */
444c19c7afeSEric Joyner 		if (adapter->feat_en & IXGBE_FEATURE_FDIR)
445c19c7afeSEric Joyner 			txr->atr_sample = atr_sample_rate;
446c19c7afeSEric Joyner 
447c19c7afeSEric Joyner 	}
448c19c7afeSEric Joyner 
449c19c7afeSEric Joyner 	iflib_config_gtask_init(ctx, &adapter->mod_task, ixgbe_handle_mod,
450c19c7afeSEric Joyner 	    "mod_task");
451c19c7afeSEric Joyner 	iflib_config_gtask_init(ctx, &adapter->msf_task, ixgbe_handle_msf,
452c19c7afeSEric Joyner 	    "msf_task");
453c19c7afeSEric Joyner 	iflib_config_gtask_init(ctx, &adapter->phy_task, ixgbe_handle_phy,
454c19c7afeSEric Joyner 	    "phy_task");
455c19c7afeSEric Joyner 	if (adapter->feat_cap & IXGBE_FEATURE_SRIOV)
456c19c7afeSEric Joyner 		iflib_config_gtask_init(ctx, &adapter->mbx_task,
457c19c7afeSEric Joyner 		    ixgbe_handle_mbx, "mbx_task");
458c19c7afeSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_FDIR)
459c19c7afeSEric Joyner 		iflib_config_gtask_init(ctx, &adapter->fdir_task,
460c19c7afeSEric Joyner 		    ixgbe_reinit_fdir, "fdir_task");
461c19c7afeSEric Joyner 
462c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "allocated for %d queues\n",
463c19c7afeSEric Joyner 	    adapter->num_tx_queues);
464c19c7afeSEric Joyner 
465c19c7afeSEric Joyner 	return (0);
466c19c7afeSEric Joyner 
467c19c7afeSEric Joyner fail:
468c19c7afeSEric Joyner 	ixgbe_if_queues_free(ctx);
469c19c7afeSEric Joyner 
470c19c7afeSEric Joyner 	return (error);
471c19c7afeSEric Joyner } /* ixgbe_if_tx_queues_alloc */
472c19c7afeSEric Joyner 
473c19c7afeSEric Joyner /************************************************************************
474c19c7afeSEric Joyner  * ixgbe_if_rx_queues_alloc
475c19c7afeSEric Joyner  ************************************************************************/
476c19c7afeSEric Joyner static int
477c19c7afeSEric Joyner ixgbe_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
478c19c7afeSEric Joyner                          int nrxqs, int nrxqsets)
479c19c7afeSEric Joyner {
480c19c7afeSEric Joyner 	struct adapter     *adapter = iflib_get_softc(ctx);
481c19c7afeSEric Joyner 	struct ix_rx_queue *que;
482c19c7afeSEric Joyner 	int                i;
483c19c7afeSEric Joyner 
484c19c7afeSEric Joyner 	MPASS(adapter->num_rx_queues > 0);
485c19c7afeSEric Joyner 	MPASS(adapter->num_rx_queues == nrxqsets);
486c19c7afeSEric Joyner 	MPASS(nrxqs == 1);
487c19c7afeSEric Joyner 
488c19c7afeSEric Joyner 	/* Allocate queue structure memory */
489c19c7afeSEric Joyner 	adapter->rx_queues =
490c19c7afeSEric Joyner 	    (struct ix_rx_queue *)malloc(sizeof(struct ix_rx_queue)*nrxqsets,
491c19c7afeSEric Joyner 	                                 M_IXGBE, M_NOWAIT | M_ZERO);
492c19c7afeSEric Joyner 	if (!adapter->rx_queues) {
493c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
494c19c7afeSEric Joyner 		    "Unable to allocate TX ring memory\n");
495c19c7afeSEric Joyner 		return (ENOMEM);
496c19c7afeSEric Joyner 	}
497c19c7afeSEric Joyner 
498c19c7afeSEric Joyner 	for (i = 0, que = adapter->rx_queues; i < nrxqsets; i++, que++) {
499c19c7afeSEric Joyner 		struct rx_ring *rxr = &que->rxr;
500c19c7afeSEric Joyner 
501c19c7afeSEric Joyner 		/* In case SR-IOV is enabled, align the index properly */
502c19c7afeSEric Joyner 		rxr->me = ixgbe_vf_que_index(adapter->iov_mode, adapter->pool,
503c19c7afeSEric Joyner 		    i);
504c19c7afeSEric Joyner 
505c19c7afeSEric Joyner 		rxr->adapter = que->adapter = adapter;
506c19c7afeSEric Joyner 
507c19c7afeSEric Joyner 		/* get the virtual and physical address of the hw queues */
508c19c7afeSEric Joyner 		rxr->tail = IXGBE_RDT(rxr->me);
509c19c7afeSEric Joyner 		rxr->rx_base = (union ixgbe_adv_rx_desc *)vaddrs[i];
510c19c7afeSEric Joyner 		rxr->rx_paddr = paddrs[i];
511c19c7afeSEric Joyner 		rxr->bytes = 0;
512c19c7afeSEric Joyner 		rxr->que = que;
513c19c7afeSEric Joyner 	}
514c19c7afeSEric Joyner 
515c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "allocated for %d rx queues\n",
516c19c7afeSEric Joyner 	    adapter->num_rx_queues);
517c19c7afeSEric Joyner 
518c19c7afeSEric Joyner 	return (0);
519c19c7afeSEric Joyner } /* ixgbe_if_rx_queues_alloc */
520c19c7afeSEric Joyner 
521c19c7afeSEric Joyner /************************************************************************
522c19c7afeSEric Joyner  * ixgbe_if_queues_free
523c19c7afeSEric Joyner  ************************************************************************/
524c19c7afeSEric Joyner static void
525c19c7afeSEric Joyner ixgbe_if_queues_free(if_ctx_t ctx)
526c19c7afeSEric Joyner {
527c19c7afeSEric Joyner 	struct adapter     *adapter = iflib_get_softc(ctx);
528c19c7afeSEric Joyner 	struct ix_tx_queue *tx_que = adapter->tx_queues;
529c19c7afeSEric Joyner 	struct ix_rx_queue *rx_que = adapter->rx_queues;
530c19c7afeSEric Joyner 	int                i;
531c19c7afeSEric Joyner 
532c19c7afeSEric Joyner 	if (tx_que != NULL) {
533c19c7afeSEric Joyner 		for (i = 0; i < adapter->num_tx_queues; i++, tx_que++) {
534c19c7afeSEric Joyner 			struct tx_ring *txr = &tx_que->txr;
535c19c7afeSEric Joyner 			if (txr->tx_rsq == NULL)
536c19c7afeSEric Joyner 				break;
537c19c7afeSEric Joyner 
538c19c7afeSEric Joyner 			free(txr->tx_rsq, M_IXGBE);
539c19c7afeSEric Joyner 			txr->tx_rsq = NULL;
540c19c7afeSEric Joyner 		}
541c19c7afeSEric Joyner 
542c19c7afeSEric Joyner 		free(adapter->tx_queues, M_IXGBE);
543c19c7afeSEric Joyner 		adapter->tx_queues = NULL;
544c19c7afeSEric Joyner 	}
545c19c7afeSEric Joyner 	if (rx_que != NULL) {
546c19c7afeSEric Joyner 		free(adapter->rx_queues, M_IXGBE);
547c19c7afeSEric Joyner 		adapter->rx_queues = NULL;
548c19c7afeSEric Joyner 	}
549c19c7afeSEric Joyner } /* ixgbe_if_queues_free */
550c19c7afeSEric Joyner 
5518eb6488eSEric Joyner /************************************************************************
5528eb6488eSEric Joyner  * ixgbe_initialize_rss_mapping
5538eb6488eSEric Joyner  ************************************************************************/
5548eb6488eSEric Joyner static void
5558eb6488eSEric Joyner ixgbe_initialize_rss_mapping(struct adapter *adapter)
556758cc3dcSJack F Vogel {
5576f37f232SEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
5588eb6488eSEric Joyner 	u32             reta = 0, mrqc, rss_key[10];
5598eb6488eSEric Joyner 	int             queue_id, table_size, index_mult;
5608eb6488eSEric Joyner 	int             i, j;
5618eb6488eSEric Joyner 	u32             rss_hash_config;
5626f37f232SEric Joyner 
5638eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_RSS) {
5648eb6488eSEric Joyner 		/* Fetch the configured RSS key */
5658eb6488eSEric Joyner 		rss_getkey((uint8_t *)&rss_key);
566758cc3dcSJack F Vogel 	} else {
5678eb6488eSEric Joyner 		/* set up random bits */
5688eb6488eSEric Joyner 		arc4rand(&rss_key, sizeof(rss_key), 0);
569758cc3dcSJack F Vogel 	}
570758cc3dcSJack F Vogel 
5718eb6488eSEric Joyner 	/* Set multiplier for RETA setup and table size based on MAC */
5728eb6488eSEric Joyner 	index_mult = 0x1;
5738eb6488eSEric Joyner 	table_size = 128;
5748eb6488eSEric Joyner 	switch (adapter->hw.mac.type) {
5758eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
5768eb6488eSEric Joyner 		index_mult = 0x11;
577758cc3dcSJack F Vogel 		break;
5788eb6488eSEric Joyner 	case ixgbe_mac_X550:
5798eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
5808eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
5818eb6488eSEric Joyner 		table_size = 512;
5828eb6488eSEric Joyner 		break;
583758cc3dcSJack F Vogel 	default:
584758cc3dcSJack F Vogel 		break;
585758cc3dcSJack F Vogel 	}
586758cc3dcSJack F Vogel 
5878eb6488eSEric Joyner 	/* Set up the redirection table */
5888eb6488eSEric Joyner 	for (i = 0, j = 0; i < table_size; i++, j++) {
589c19c7afeSEric Joyner 		if (j == adapter->num_rx_queues)
5908eb6488eSEric Joyner 			j = 0;
591758cc3dcSJack F Vogel 
5928eb6488eSEric Joyner 		if (adapter->feat_en & IXGBE_FEATURE_RSS) {
593a9ca1c79SSean Bruno 			/*
5948eb6488eSEric Joyner 			 * Fetch the RSS bucket id for the given indirection
5958eb6488eSEric Joyner 			 * entry. Cap it at the number of configured buckets
596c19c7afeSEric Joyner 			 * (which is num_rx_queues.)
597a9ca1c79SSean Bruno 			 */
5988eb6488eSEric Joyner 			queue_id = rss_get_indirection_to_bucket(i);
599c19c7afeSEric Joyner 			queue_id = queue_id % adapter->num_rx_queues;
600758cc3dcSJack F Vogel 		} else
6018eb6488eSEric Joyner 			queue_id = (j * index_mult);
6028eb6488eSEric Joyner 
6038eb6488eSEric Joyner 		/*
6048eb6488eSEric Joyner 		 * The low 8 bits are for hash value (n+0);
6058eb6488eSEric Joyner 		 * The next 8 bits are for hash value (n+1), etc.
6068eb6488eSEric Joyner 		 */
6078eb6488eSEric Joyner 		reta = reta >> 8;
6088eb6488eSEric Joyner 		reta = reta | (((uint32_t)queue_id) << 24);
6098eb6488eSEric Joyner 		if ((i & 3) == 3) {
6108eb6488eSEric Joyner 			if (i < 128)
6118eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
6128eb6488eSEric Joyner 			else
6138eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_ERETA((i >> 2) - 32),
6148eb6488eSEric Joyner 				    reta);
6158eb6488eSEric Joyner 			reta = 0;
6168eb6488eSEric Joyner 		}
617758cc3dcSJack F Vogel 	}
618758cc3dcSJack F Vogel 
6198eb6488eSEric Joyner 	/* Now fill our hash function seeds */
6208eb6488eSEric Joyner 	for (i = 0; i < 10; i++)
6218eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
622758cc3dcSJack F Vogel 
6238eb6488eSEric Joyner 	/* Perform hash on these packet types */
6248eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_RSS)
6258eb6488eSEric Joyner 		rss_hash_config = rss_gethashconfig();
626758cc3dcSJack F Vogel 	else {
627758cc3dcSJack F Vogel 		/*
6288eb6488eSEric Joyner 		 * Disable UDP - IP fragments aren't currently being handled
6298eb6488eSEric Joyner 		 * and so we end up with a mix of 2-tuple and 4-tuple
6308eb6488eSEric Joyner 		 * traffic.
631758cc3dcSJack F Vogel 		 */
6328eb6488eSEric Joyner 		rss_hash_config = RSS_HASHTYPE_RSS_IPV4
6338eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_TCP_IPV4
6348eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_IPV6
6358eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_TCP_IPV6
6368eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_IPV6_EX
6378eb6488eSEric Joyner 		                | RSS_HASHTYPE_RSS_TCP_IPV6_EX;
638758cc3dcSJack F Vogel 	}
639758cc3dcSJack F Vogel 
6408eb6488eSEric Joyner 	mrqc = IXGBE_MRQC_RSSEN;
6418eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
6428eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
6438eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
6448eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
6458eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
6468eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
6478eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
6488eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
6498eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
6508eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
6518eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
6528eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
6538eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
6548eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
6558eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
6568eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
6578eb6488eSEric Joyner 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
6588eb6488eSEric Joyner 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
6598eb6488eSEric Joyner 	mrqc |= ixgbe_get_mrqc(adapter->iov_mode);
6608eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
6618eb6488eSEric Joyner } /* ixgbe_initialize_rss_mapping */
662758cc3dcSJack F Vogel 
6638eb6488eSEric Joyner /************************************************************************
6648eb6488eSEric Joyner  * ixgbe_initialize_receive_units - Setup receive registers and features.
6658eb6488eSEric Joyner  ************************************************************************/
6668eb6488eSEric Joyner #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
667758cc3dcSJack F Vogel 
668758cc3dcSJack F Vogel static void
669c19c7afeSEric Joyner ixgbe_initialize_receive_units(if_ctx_t ctx)
670758cc3dcSJack F Vogel {
671c19c7afeSEric Joyner 	struct adapter     *adapter = iflib_get_softc(ctx);
672c19c7afeSEric Joyner 	if_softc_ctx_t     scctx = adapter->shared;
67348056c88SJack F Vogel 	struct ixgbe_hw    *hw = &adapter->hw;
674c19c7afeSEric Joyner 	struct ifnet       *ifp = iflib_get_ifp(ctx);
675c19c7afeSEric Joyner 	struct ix_rx_queue *que;
6768eb6488eSEric Joyner 	int                i, j;
6778eb6488eSEric Joyner 	u32                bufsz, fctrl, srrctl, rxcsum;
6788eb6488eSEric Joyner 	u32                hlreg;
67948056c88SJack F Vogel 
68048056c88SJack F Vogel 	/*
6818eb6488eSEric Joyner 	 * Make sure receives are disabled while
6828eb6488eSEric Joyner 	 * setting up the descriptor ring
68348056c88SJack F Vogel 	 */
6848eb6488eSEric Joyner 	ixgbe_disable_rx(hw);
6858eb6488eSEric Joyner 
6868eb6488eSEric Joyner 	/* Enable broadcasts */
6878eb6488eSEric Joyner 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
6888eb6488eSEric Joyner 	fctrl |= IXGBE_FCTRL_BAM;
6898eb6488eSEric Joyner 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
6908eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_DPF;
6918eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_PMCF;
69248056c88SJack F Vogel 	}
6938eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
6948eb6488eSEric Joyner 
6958eb6488eSEric Joyner 	/* Set for Jumbo Frames? */
6968eb6488eSEric Joyner 	hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
6978eb6488eSEric Joyner 	if (ifp->if_mtu > ETHERMTU)
6988eb6488eSEric Joyner 		hlreg |= IXGBE_HLREG0_JUMBOEN;
6998eb6488eSEric Joyner 	else
7008eb6488eSEric Joyner 		hlreg &= ~IXGBE_HLREG0_JUMBOEN;
7018eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
7028eb6488eSEric Joyner 
7038eb6488eSEric Joyner 	bufsz = (adapter->rx_mbuf_sz + BSIZEPKT_ROUNDUP) >>
7048eb6488eSEric Joyner 	    IXGBE_SRRCTL_BSIZEPKT_SHIFT;
7058eb6488eSEric Joyner 
706c19c7afeSEric Joyner 	/* Setup the Base and Length of the Rx Descriptor Ring */
707c19c7afeSEric Joyner 	for (i = 0, que = adapter->rx_queues; i < adapter->num_rx_queues; i++, que++) {
708c19c7afeSEric Joyner 		struct rx_ring *rxr = &que->rxr;
709c19c7afeSEric Joyner 		u64            rdba = rxr->rx_paddr;
710c19c7afeSEric Joyner 
7118eb6488eSEric Joyner 		j = rxr->me;
7128eb6488eSEric Joyner 
7138eb6488eSEric Joyner 		/* Setup the Base and Length of the Rx Descriptor Ring */
7148eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j),
7158eb6488eSEric Joyner 		    (rdba & 0x00000000ffffffffULL));
7168eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
7178eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j),
718c19c7afeSEric Joyner 		     scctx->isc_nrxd[0] * sizeof(union ixgbe_adv_rx_desc));
7198eb6488eSEric Joyner 
7208eb6488eSEric Joyner 		/* Set up the SRRCTL register */
7218eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(j));
7228eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
7238eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
7248eb6488eSEric Joyner 		srrctl |= bufsz;
7258eb6488eSEric Joyner 		srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
72648056c88SJack F Vogel 
72748056c88SJack F Vogel 		/*
7288eb6488eSEric Joyner 		 * Set DROP_EN iff we have no flow control and >1 queue.
7298eb6488eSEric Joyner 		 * Note that srrctl was cleared shortly before during reset,
7308eb6488eSEric Joyner 		 * so we do not need to clear the bit, but do it just in case
7318eb6488eSEric Joyner 		 * this code is moved elsewhere.
73248056c88SJack F Vogel 		 */
733c19c7afeSEric Joyner 		if (adapter->num_rx_queues > 1 &&
7348eb6488eSEric Joyner 		    adapter->hw.fc.requested_mode == ixgbe_fc_none) {
7358eb6488eSEric Joyner 			srrctl |= IXGBE_SRRCTL_DROP_EN;
736758cc3dcSJack F Vogel 		} else {
7378eb6488eSEric Joyner 			srrctl &= ~IXGBE_SRRCTL_DROP_EN;
738758cc3dcSJack F Vogel 		}
739758cc3dcSJack F Vogel 
7408eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(j), srrctl);
741758cc3dcSJack F Vogel 
7428eb6488eSEric Joyner 		/* Setup the HW Rx Head and Tail Descriptor Pointers */
7438eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
7448eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
745758cc3dcSJack F Vogel 
7468eb6488eSEric Joyner 		/* Set the driver rx tail address */
7478eb6488eSEric Joyner 		rxr->tail =  IXGBE_RDT(rxr->me);
748a9ca1c79SSean Bruno 	}
749758cc3dcSJack F Vogel 
750758cc3dcSJack F Vogel 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
7518eb6488eSEric Joyner 		u32 psrtype = IXGBE_PSRTYPE_TCPHDR
7528eb6488eSEric Joyner 		            | IXGBE_PSRTYPE_UDPHDR
7538eb6488eSEric Joyner 		            | IXGBE_PSRTYPE_IPV4HDR
7548eb6488eSEric Joyner 		            | IXGBE_PSRTYPE_IPV6HDR;
7558eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
7566f37f232SEric Joyner 	}
7576f37f232SEric Joyner 
7588eb6488eSEric Joyner 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
7598eb6488eSEric Joyner 
7608eb6488eSEric Joyner 	ixgbe_initialize_rss_mapping(adapter);
7618eb6488eSEric Joyner 
762c19c7afeSEric Joyner 	if (adapter->num_rx_queues > 1) {
7638eb6488eSEric Joyner 		/* RSS and RX IPP Checksum are mutually exclusive */
7648eb6488eSEric Joyner 		rxcsum |= IXGBE_RXCSUM_PCSD;
7656f37f232SEric Joyner 	}
7666f37f232SEric Joyner 
7678eb6488eSEric Joyner 	if (ifp->if_capenable & IFCAP_RXCSUM)
7688eb6488eSEric Joyner 		rxcsum |= IXGBE_RXCSUM_PCSD;
769758cc3dcSJack F Vogel 
7708eb6488eSEric Joyner 	/* This is useful for calculating UDP/IP fragment checksums */
7718eb6488eSEric Joyner 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
7728eb6488eSEric Joyner 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
773758cc3dcSJack F Vogel 
7748eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
775758cc3dcSJack F Vogel 
7768eb6488eSEric Joyner } /* ixgbe_initialize_receive_units */
777758cc3dcSJack F Vogel 
7788eb6488eSEric Joyner /************************************************************************
7798eb6488eSEric Joyner  * ixgbe_initialize_transmit_units - Enable transmit units.
7808eb6488eSEric Joyner  ************************************************************************/
781758cc3dcSJack F Vogel static void
782c19c7afeSEric Joyner ixgbe_initialize_transmit_units(if_ctx_t ctx)
783758cc3dcSJack F Vogel {
784c19c7afeSEric Joyner 	struct adapter     *adapter = iflib_get_softc(ctx);
785758cc3dcSJack F Vogel 	struct ixgbe_hw    *hw = &adapter->hw;
786c19c7afeSEric Joyner 	if_softc_ctx_t     scctx = adapter->shared;
787c19c7afeSEric Joyner 	struct ix_tx_queue *que;
788c19c7afeSEric Joyner 	int i;
789758cc3dcSJack F Vogel 
790758cc3dcSJack F Vogel 	/* Setup the Base and Length of the Tx Descriptor Ring */
791c19c7afeSEric Joyner 	for (i = 0, que = adapter->tx_queues; i < adapter->num_tx_queues;
792c19c7afeSEric Joyner 	    i++, que++) {
793c19c7afeSEric Joyner 		struct tx_ring	   *txr = &que->txr;
794c19c7afeSEric Joyner 		u64 tdba = txr->tx_paddr;
795758cc3dcSJack F Vogel 		u32 txctrl = 0;
79648056c88SJack F Vogel 		int j = txr->me;
797758cc3dcSJack F Vogel 
79848056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
799758cc3dcSJack F Vogel 		    (tdba & 0x00000000ffffffffULL));
80048056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
80148056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j),
802c19c7afeSEric Joyner 		    scctx->isc_ntxd[0] * sizeof(union ixgbe_adv_tx_desc));
803758cc3dcSJack F Vogel 
804758cc3dcSJack F Vogel 		/* Setup the HW Tx Head and Tail descriptor pointers */
80548056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
80648056c88SJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
807758cc3dcSJack F Vogel 
808758cc3dcSJack F Vogel 		/* Cache the tail address */
809088a0b27SEric Joyner 		txr->tx_rs_cidx = txr->tx_rs_pidx;
810088a0b27SEric Joyner 		txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
811c19c7afeSEric Joyner 		for (int k = 0; k < scctx->isc_ntxd[0]; k++)
812c19c7afeSEric Joyner 			txr->tx_rsq[k] = QIDX_INVALID;
813758cc3dcSJack F Vogel 
814758cc3dcSJack F Vogel 		/* Disable Head Writeback */
815a9ca1c79SSean Bruno 		/*
816a9ca1c79SSean Bruno 		 * Note: for X550 series devices, these registers are actually
817a9ca1c79SSean Bruno 		 * prefixed with TPH_ isntead of DCA_, but the addresses and
818a9ca1c79SSean Bruno 		 * fields remain the same.
819a9ca1c79SSean Bruno 		 */
820758cc3dcSJack F Vogel 		switch (hw->mac.type) {
821758cc3dcSJack F Vogel 		case ixgbe_mac_82598EB:
82248056c88SJack F Vogel 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
823758cc3dcSJack F Vogel 			break;
824758cc3dcSJack F Vogel 		default:
82548056c88SJack F Vogel 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(j));
826758cc3dcSJack F Vogel 			break;
827758cc3dcSJack F Vogel 		}
828758cc3dcSJack F Vogel 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
829758cc3dcSJack F Vogel 		switch (hw->mac.type) {
830758cc3dcSJack F Vogel 		case ixgbe_mac_82598EB:
83148056c88SJack F Vogel 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
832758cc3dcSJack F Vogel 			break;
833758cc3dcSJack F Vogel 		default:
83448056c88SJack F Vogel 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(j), txctrl);
835758cc3dcSJack F Vogel 			break;
836758cc3dcSJack F Vogel 		}
837758cc3dcSJack F Vogel 
838758cc3dcSJack F Vogel 	}
839758cc3dcSJack F Vogel 
840758cc3dcSJack F Vogel 	if (hw->mac.type != ixgbe_mac_82598EB) {
841758cc3dcSJack F Vogel 		u32 dmatxctl, rttdcs;
8428eb6488eSEric Joyner 
843758cc3dcSJack F Vogel 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
844758cc3dcSJack F Vogel 		dmatxctl |= IXGBE_DMATXCTL_TE;
845758cc3dcSJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
846758cc3dcSJack F Vogel 		/* Disable arbiter to set MTQC */
847758cc3dcSJack F Vogel 		rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
848758cc3dcSJack F Vogel 		rttdcs |= IXGBE_RTTDCS_ARBDIS;
849758cc3dcSJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
8508eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_MTQC,
8518eb6488eSEric Joyner 		    ixgbe_get_mtqc(adapter->iov_mode));
852758cc3dcSJack F Vogel 		rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
853758cc3dcSJack F Vogel 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
854758cc3dcSJack F Vogel 	}
855758cc3dcSJack F Vogel 
8568eb6488eSEric Joyner } /* ixgbe_initialize_transmit_units */
857758cc3dcSJack F Vogel 
8588eb6488eSEric Joyner /************************************************************************
859c19c7afeSEric Joyner  * ixgbe_register
860c19c7afeSEric Joyner  ************************************************************************/
861c19c7afeSEric Joyner static void *
862c19c7afeSEric Joyner ixgbe_register(device_t dev)
863c19c7afeSEric Joyner {
864c19c7afeSEric Joyner 	return (ixgbe_sctx);
865c19c7afeSEric Joyner } /* ixgbe_register */
866c19c7afeSEric Joyner 
867c19c7afeSEric Joyner /************************************************************************
868c19c7afeSEric Joyner  * ixgbe_if_attach_pre - Device initialization routine, part 1
869758cc3dcSJack F Vogel  *
8708eb6488eSEric Joyner  *   Called when the driver is being loaded.
871c19c7afeSEric Joyner  *   Identifies the type of hardware, initializes the hardware,
872c19c7afeSEric Joyner  *   and initializes iflib structures.
873758cc3dcSJack F Vogel  *
8748eb6488eSEric Joyner  *   return 0 on success, positive on failure
8758eb6488eSEric Joyner  ************************************************************************/
8768eb6488eSEric Joyner static int
877c19c7afeSEric Joyner ixgbe_if_attach_pre(if_ctx_t ctx)
878758cc3dcSJack F Vogel {
8798eb6488eSEric Joyner 	struct adapter  *adapter;
880c19c7afeSEric Joyner 	device_t        dev;
881c19c7afeSEric Joyner 	if_softc_ctx_t  scctx;
8828eb6488eSEric Joyner 	struct ixgbe_hw *hw;
8838eb6488eSEric Joyner 	int             error = 0;
8848eb6488eSEric Joyner 	u32             ctrl_ext;
8858eb6488eSEric Joyner 
8868eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_attach: begin");
8878eb6488eSEric Joyner 
8888eb6488eSEric Joyner 	/* Allocate, clear, and link in our adapter structure */
889c19c7afeSEric Joyner 	dev = iflib_get_dev(ctx);
890c19c7afeSEric Joyner 	adapter = iflib_get_softc(ctx);
8918eb6488eSEric Joyner 	adapter->hw.back = adapter;
892c19c7afeSEric Joyner 	adapter->ctx = ctx;
8938eb6488eSEric Joyner 	adapter->dev = dev;
894c19c7afeSEric Joyner 	scctx = adapter->shared = iflib_get_softc_ctx(ctx);
895c19c7afeSEric Joyner 	adapter->media = iflib_get_media(ctx);
8968eb6488eSEric Joyner 	hw = &adapter->hw;
8978eb6488eSEric Joyner 
8988eb6488eSEric Joyner 	/* Determine hardware revision */
8998eb6488eSEric Joyner 	hw->vendor_id = pci_get_vendor(dev);
9008eb6488eSEric Joyner 	hw->device_id = pci_get_device(dev);
9018eb6488eSEric Joyner 	hw->revision_id = pci_get_revid(dev);
9028eb6488eSEric Joyner 	hw->subsystem_vendor_id = pci_get_subvendor(dev);
9038eb6488eSEric Joyner 	hw->subsystem_device_id = pci_get_subdevice(dev);
904758cc3dcSJack F Vogel 
9058eb6488eSEric Joyner 	/* Do base PCI setup - map BAR0 */
906c19c7afeSEric Joyner 	if (ixgbe_allocate_pci_resources(ctx)) {
9078eb6488eSEric Joyner 		device_printf(dev, "Allocation of PCI resources failed\n");
908c19c7afeSEric Joyner 		return (ENXIO);
9096f37f232SEric Joyner 	}
910758cc3dcSJack F Vogel 
9118eb6488eSEric Joyner 	/* let hardware know driver is loaded */
9128eb6488eSEric Joyner 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
9138eb6488eSEric Joyner 	ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
9148eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
915758cc3dcSJack F Vogel 
916758cc3dcSJack F Vogel 	/*
9178eb6488eSEric Joyner 	 * Initialize the shared code
918758cc3dcSJack F Vogel 	 */
919c19c7afeSEric Joyner 	if (ixgbe_init_shared_code(hw) != 0) {
9208eb6488eSEric Joyner 		device_printf(dev, "Unable to initialize the shared code\n");
9218eb6488eSEric Joyner 		error = ENXIO;
922c19c7afeSEric Joyner 		goto err_pci;
92330126537SJack F Vogel 	}
924758cc3dcSJack F Vogel 
9258eb6488eSEric Joyner 	if (hw->mbx.ops.init_params)
9268eb6488eSEric Joyner 		hw->mbx.ops.init_params(hw);
927758cc3dcSJack F Vogel 
9288eb6488eSEric Joyner 	hw->allow_unsupported_sfp = allow_unsupported_sfp;
929758cc3dcSJack F Vogel 
930c19c7afeSEric Joyner 	if (hw->mac.type != ixgbe_mac_82598EB)
9318eb6488eSEric Joyner 		hw->phy.smart_speed = ixgbe_smart_speed;
9328eb6488eSEric Joyner 
9338eb6488eSEric Joyner 	ixgbe_init_device_features(adapter);
9348eb6488eSEric Joyner 
9358eb6488eSEric Joyner 	/* Enable WoL (if supported) */
9368eb6488eSEric Joyner 	ixgbe_check_wol_support(adapter);
9378eb6488eSEric Joyner 
9388eb6488eSEric Joyner 	/* Verify adapter fan is still functional (if applicable) */
9398eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL) {
9408eb6488eSEric Joyner 		u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
9418eb6488eSEric Joyner 		ixgbe_check_fan_failure(adapter, esdp, FALSE);
9428eb6488eSEric Joyner 	}
9438eb6488eSEric Joyner 
9448eb6488eSEric Joyner 	/* Ensure SW/FW semaphore is free */
9458eb6488eSEric Joyner 	ixgbe_init_swfw_semaphore(hw);
9468eb6488eSEric Joyner 
9478eb6488eSEric Joyner 	/* Set an initial default flow control value */
9488eb6488eSEric Joyner 	hw->fc.requested_mode = ixgbe_flow_control;
9498eb6488eSEric Joyner 
9508eb6488eSEric Joyner 	hw->phy.reset_if_overtemp = TRUE;
9518eb6488eSEric Joyner 	error = ixgbe_reset_hw(hw);
9528eb6488eSEric Joyner 	hw->phy.reset_if_overtemp = FALSE;
9538eb6488eSEric Joyner 	if (error == IXGBE_ERR_SFP_NOT_PRESENT) {
954758cc3dcSJack F Vogel 		/*
9558eb6488eSEric Joyner 		 * No optics in this port, set up
9568eb6488eSEric Joyner 		 * so the timer routine will probe
9578eb6488eSEric Joyner 		 * for later insertion.
958758cc3dcSJack F Vogel 		 */
9598eb6488eSEric Joyner 		adapter->sfp_probe = TRUE;
960c19c7afeSEric Joyner 		error = 0;
9618eb6488eSEric Joyner 	} else if (error == IXGBE_ERR_SFP_NOT_SUPPORTED) {
9628eb6488eSEric Joyner 		device_printf(dev, "Unsupported SFP+ module detected!\n");
9638eb6488eSEric Joyner 		error = EIO;
964c19c7afeSEric Joyner 		goto err_pci;
9658eb6488eSEric Joyner 	} else if (error) {
9668eb6488eSEric Joyner 		device_printf(dev, "Hardware initialization failed\n");
9678eb6488eSEric Joyner 		error = EIO;
968c19c7afeSEric Joyner 		goto err_pci;
96997f9586eSSean Bruno 	}
97097f9586eSSean Bruno 
9718eb6488eSEric Joyner 	/* Make sure we have a good EEPROM before we read from it */
9728eb6488eSEric Joyner 	if (ixgbe_validate_eeprom_checksum(&adapter->hw, NULL) < 0) {
9738eb6488eSEric Joyner 		device_printf(dev, "The EEPROM Checksum Is Not Valid\n");
9748eb6488eSEric Joyner 		error = EIO;
975c19c7afeSEric Joyner 		goto err_pci;
97697f9586eSSean Bruno 	}
97797f9586eSSean Bruno 
9788eb6488eSEric Joyner 	error = ixgbe_start_hw(hw);
9798eb6488eSEric Joyner 	switch (error) {
9808eb6488eSEric Joyner 	case IXGBE_ERR_EEPROM_VERSION:
9818eb6488eSEric 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");
98297f9586eSSean Bruno 		break;
9838eb6488eSEric Joyner 	case IXGBE_ERR_SFP_NOT_SUPPORTED:
9848eb6488eSEric Joyner 		device_printf(dev, "Unsupported SFP+ Module\n");
9858eb6488eSEric Joyner 		error = EIO;
986c19c7afeSEric Joyner 		goto err_pci;
9878eb6488eSEric Joyner 	case IXGBE_ERR_SFP_NOT_PRESENT:
9888eb6488eSEric Joyner 		device_printf(dev, "No SFP+ Module found\n");
9898eb6488eSEric Joyner 		/* falls thru */
99097f9586eSSean Bruno 	default:
99197f9586eSSean Bruno 		break;
99297f9586eSSean Bruno 	}
99397f9586eSSean Bruno 
994c19c7afeSEric Joyner 	/* Most of the iflib initialization... */
995c19c7afeSEric Joyner 
996c19c7afeSEric Joyner 	iflib_set_mac(ctx, hw->mac.addr);
997c19c7afeSEric Joyner 	switch (adapter->hw.mac.type) {
998c19c7afeSEric Joyner 	case ixgbe_mac_X550:
999c19c7afeSEric Joyner 	case ixgbe_mac_X550EM_x:
1000c19c7afeSEric Joyner 	case ixgbe_mac_X550EM_a:
1001c19c7afeSEric Joyner 		scctx->isc_rss_table_size = 512;
1002c19c7afeSEric Joyner 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 64;
1003c19c7afeSEric Joyner 		break;
1004c19c7afeSEric Joyner 	default:
1005c19c7afeSEric Joyner 		scctx->isc_rss_table_size = 128;
1006c19c7afeSEric Joyner 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 16;
1007c19c7afeSEric Joyner 	}
1008c19c7afeSEric Joyner 
1009c19c7afeSEric Joyner 	/* Allow legacy interrupts */
1010c19c7afeSEric Joyner 	ixgbe_txrx.ift_legacy_intr = ixgbe_intr;
1011c19c7afeSEric Joyner 
1012c19c7afeSEric Joyner 	scctx->isc_txqsizes[0] =
1013c19c7afeSEric Joyner 	    roundup2(scctx->isc_ntxd[0] * sizeof(union ixgbe_adv_tx_desc) +
1014c19c7afeSEric Joyner 	    sizeof(u32), DBA_ALIGN),
1015c19c7afeSEric Joyner 	scctx->isc_rxqsizes[0] =
1016c19c7afeSEric Joyner 	    roundup2(scctx->isc_nrxd[0] * sizeof(union ixgbe_adv_rx_desc),
1017c19c7afeSEric Joyner 	    DBA_ALIGN);
1018c19c7afeSEric Joyner 
1019c19c7afeSEric Joyner 	/* XXX */
1020c19c7afeSEric Joyner 	scctx->isc_tx_csum_flags = CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_TSO |
1021c19c7afeSEric Joyner 	    CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_TSO;
1022c19c7afeSEric Joyner 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
1023c19c7afeSEric Joyner 		scctx->isc_tx_nsegments = IXGBE_82598_SCATTER;
1024c19c7afeSEric Joyner 		scctx->isc_msix_bar = PCIR_BAR(MSIX_82598_BAR);
1025c19c7afeSEric Joyner 	} else {
1026c19c7afeSEric Joyner 		scctx->isc_tx_csum_flags |= CSUM_SCTP |CSUM_IP6_SCTP;
1027c19c7afeSEric Joyner 		scctx->isc_tx_nsegments = IXGBE_82599_SCATTER;
1028c19c7afeSEric Joyner 		scctx->isc_msix_bar = PCIR_BAR(MSIX_82599_BAR);
1029c19c7afeSEric Joyner 	}
1030c19c7afeSEric Joyner 	scctx->isc_tx_tso_segments_max = scctx->isc_tx_nsegments;
1031c19c7afeSEric Joyner 	scctx->isc_tx_tso_size_max = IXGBE_TSO_SIZE;
1032c19c7afeSEric Joyner 	scctx->isc_tx_tso_segsize_max = PAGE_SIZE;
1033c19c7afeSEric Joyner 
1034c19c7afeSEric Joyner 	scctx->isc_txrx = &ixgbe_txrx;
1035c19c7afeSEric Joyner 
10367f87c040SMarius Strobl 	scctx->isc_capabilities = scctx->isc_capenable = IXGBE_CAPS;
1037c19c7afeSEric Joyner 
1038c19c7afeSEric Joyner 	return (0);
1039c19c7afeSEric Joyner 
1040c19c7afeSEric Joyner err_pci:
1041c19c7afeSEric Joyner 	ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
1042c19c7afeSEric Joyner 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
1043c19c7afeSEric Joyner 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT, ctrl_ext);
1044c19c7afeSEric Joyner 	ixgbe_free_pci_resources(ctx);
1045c19c7afeSEric Joyner 
1046c19c7afeSEric Joyner 	return (error);
1047c19c7afeSEric Joyner } /* ixgbe_if_attach_pre */
1048c19c7afeSEric Joyner 
1049c19c7afeSEric Joyner  /*********************************************************************
1050c19c7afeSEric Joyner  * ixgbe_if_attach_post - Device initialization routine, part 2
1051c19c7afeSEric Joyner  *
1052c19c7afeSEric Joyner  *   Called during driver load, but after interrupts and
1053c19c7afeSEric Joyner  *   resources have been allocated and configured.
1054c19c7afeSEric Joyner  *   Sets up some data structures not relevant to iflib.
1055c19c7afeSEric Joyner  *
1056c19c7afeSEric Joyner  *   return 0 on success, positive on failure
1057c19c7afeSEric Joyner  *********************************************************************/
1058c19c7afeSEric Joyner static int
1059c19c7afeSEric Joyner ixgbe_if_attach_post(if_ctx_t ctx)
1060c19c7afeSEric Joyner {
1061c19c7afeSEric Joyner 	device_t dev;
1062c19c7afeSEric Joyner 	struct adapter  *adapter;
1063c19c7afeSEric Joyner 	struct ixgbe_hw *hw;
1064c19c7afeSEric Joyner 	int             error = 0;
1065c19c7afeSEric Joyner 
1066c19c7afeSEric Joyner 	dev = iflib_get_dev(ctx);
1067c19c7afeSEric Joyner 	adapter = iflib_get_softc(ctx);
1068c19c7afeSEric Joyner 	hw = &adapter->hw;
1069c19c7afeSEric Joyner 
1070c19c7afeSEric Joyner 
1071c19c7afeSEric Joyner 	if (adapter->intr_type == IFLIB_INTR_LEGACY &&
1072c19c7afeSEric Joyner 		(adapter->feat_cap & IXGBE_FEATURE_LEGACY_IRQ) == 0) {
1073c19c7afeSEric Joyner 		device_printf(dev, "Device does not support legacy interrupts");
1074c19c7afeSEric Joyner 		error = ENXIO;
1075c19c7afeSEric Joyner 		goto err;
1076c19c7afeSEric Joyner 	}
1077c19c7afeSEric Joyner 
1078c19c7afeSEric Joyner 	/* Allocate multicast array memory. */
1079c19c7afeSEric Joyner 	adapter->mta = malloc(sizeof(*adapter->mta) *
1080c19c7afeSEric Joyner 	                      MAX_NUM_MULTICAST_ADDRESSES, M_IXGBE, M_NOWAIT);
1081c19c7afeSEric Joyner 	if (adapter->mta == NULL) {
1082c19c7afeSEric Joyner 		device_printf(dev, "Can not allocate multicast setup array\n");
1083c19c7afeSEric Joyner 		error = ENOMEM;
1084c19c7afeSEric Joyner 		goto err;
1085c19c7afeSEric Joyner 	}
1086c19c7afeSEric Joyner 
1087c19c7afeSEric Joyner 	/* hw.ix defaults init */
1088c19c7afeSEric Joyner 	ixgbe_set_advertise(adapter, ixgbe_advertise_speed);
1089c19c7afeSEric Joyner 
10908eb6488eSEric Joyner 	/* Enable the optics for 82599 SFP+ fiber */
10918eb6488eSEric Joyner 	ixgbe_enable_tx_laser(hw);
1092758cc3dcSJack F Vogel 
10938eb6488eSEric Joyner 	/* Enable power to the phy. */
10948eb6488eSEric Joyner 	ixgbe_set_phy_power(hw, TRUE);
10958eb6488eSEric Joyner 
1096c19c7afeSEric Joyner 	ixgbe_initialize_iov(adapter);
1097c19c7afeSEric Joyner 
1098c19c7afeSEric Joyner 	error = ixgbe_setup_interface(ctx);
1099c19c7afeSEric Joyner 	if (error) {
1100c19c7afeSEric Joyner 		device_printf(dev, "Interface setup failed: %d\n", error);
1101c19c7afeSEric Joyner 		goto err;
1102c19c7afeSEric Joyner 	}
1103c19c7afeSEric Joyner 
1104c19c7afeSEric Joyner 	ixgbe_if_update_admin_status(ctx);
1105c19c7afeSEric Joyner 
11068eb6488eSEric Joyner 	/* Initialize statistics */
11078eb6488eSEric Joyner 	ixgbe_update_stats_counters(adapter);
1108c19c7afeSEric Joyner 	ixgbe_add_hw_stats(adapter);
11098eb6488eSEric Joyner 
11108eb6488eSEric Joyner 	/* Check PCIE slot type/speed/width */
11118eb6488eSEric Joyner 	ixgbe_get_slot_info(adapter);
1112758cc3dcSJack F Vogel 
1113758cc3dcSJack F Vogel 	/*
11148eb6488eSEric Joyner 	 * Do time init and sysctl init here, but
11158eb6488eSEric Joyner 	 * only on the first port of a bypass adapter.
1116758cc3dcSJack F Vogel 	 */
11178eb6488eSEric Joyner 	ixgbe_bypass_init(adapter);
1118758cc3dcSJack F Vogel 
11198eb6488eSEric Joyner 	/* Set an initial dmac value */
11208eb6488eSEric Joyner 	adapter->dmac = 0;
11218eb6488eSEric Joyner 	/* Set initial advertised speeds (if applicable) */
11228eb6488eSEric Joyner 	adapter->advertise = ixgbe_get_advertise(adapter);
1123758cc3dcSJack F Vogel 
11248eb6488eSEric Joyner 	if (adapter->feat_cap & IXGBE_FEATURE_SRIOV)
11258eb6488eSEric Joyner 		ixgbe_define_iov_schemas(dev, &error);
1126758cc3dcSJack F Vogel 
11278eb6488eSEric Joyner 	/* Add sysctls */
1128c19c7afeSEric Joyner 	ixgbe_add_device_sysctls(ctx);
1129758cc3dcSJack F Vogel 
11308eb6488eSEric Joyner 	return (0);
1131c19c7afeSEric Joyner err:
11328eb6488eSEric Joyner 	return (error);
1133c19c7afeSEric Joyner } /* ixgbe_if_attach_post */
11348eb6488eSEric Joyner 
11358eb6488eSEric Joyner /************************************************************************
11368eb6488eSEric Joyner  * ixgbe_check_wol_support
11376f37f232SEric Joyner  *
11386f37f232SEric Joyner  *   Checks whether the adapter's ports are capable of
11396f37f232SEric Joyner  *   Wake On LAN by reading the adapter's NVM.
11406f37f232SEric Joyner  *
11416f37f232SEric Joyner  *   Sets each port's hw->wol_enabled value depending
11426f37f232SEric Joyner  *   on the value read here.
11438eb6488eSEric Joyner  ************************************************************************/
11446f37f232SEric Joyner static void
11456f37f232SEric Joyner ixgbe_check_wol_support(struct adapter *adapter)
11466f37f232SEric Joyner {
11476f37f232SEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
11486f37f232SEric Joyner 	u16             dev_caps = 0;
11496f37f232SEric Joyner 
11506f37f232SEric Joyner 	/* Find out WoL support for port */
11516f37f232SEric Joyner 	adapter->wol_support = hw->wol_enabled = 0;
11526f37f232SEric Joyner 	ixgbe_get_device_caps(hw, &dev_caps);
11536f37f232SEric Joyner 	if ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
11546f37f232SEric Joyner 	    ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0) &&
11556f37f232SEric Joyner 	     hw->bus.func == 0))
11566f37f232SEric Joyner 		adapter->wol_support = hw->wol_enabled = 1;
11576f37f232SEric Joyner 
11586f37f232SEric Joyner 	/* Save initial wake up filter configuration */
11596f37f232SEric Joyner 	adapter->wufc = IXGBE_READ_REG(hw, IXGBE_WUFC);
11606f37f232SEric Joyner 
11616f37f232SEric Joyner 	return;
11628eb6488eSEric Joyner } /* ixgbe_check_wol_support */
11636f37f232SEric Joyner 
11648eb6488eSEric Joyner /************************************************************************
11658eb6488eSEric Joyner  * ixgbe_setup_interface
11668eb6488eSEric Joyner  *
11678eb6488eSEric Joyner  *   Setup networking device structure and register an interface.
11688eb6488eSEric Joyner  ************************************************************************/
11696f37f232SEric Joyner static int
1170c19c7afeSEric Joyner ixgbe_setup_interface(if_ctx_t ctx)
11716f37f232SEric Joyner {
1172c19c7afeSEric Joyner 	struct ifnet   *ifp = iflib_get_ifp(ctx);
1173c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
11746f37f232SEric Joyner 
11758eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_setup_interface: begin");
11766f37f232SEric Joyner 
1177c19c7afeSEric Joyner 	if_setbaudrate(ifp, IF_Gbps(10));
11786f37f232SEric Joyner 
11798eb6488eSEric Joyner 	adapter->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1180758cc3dcSJack F Vogel 
11818eb6488eSEric Joyner 	adapter->phy_layer = ixgbe_get_supported_physical_layer(&adapter->hw);
11828eb6488eSEric Joyner 
1183c19c7afeSEric Joyner 	ixgbe_add_media_types(ctx);
1184c19c7afeSEric Joyner 
1185c19c7afeSEric Joyner 	/* Autoselect media by default */
1186c19c7afeSEric Joyner 	ifmedia_set(adapter->media, IFM_ETHER | IFM_AUTO);
11878eb6488eSEric Joyner 
11888eb6488eSEric Joyner 	return (0);
11898eb6488eSEric Joyner } /* ixgbe_setup_interface */
1190758cc3dcSJack F Vogel 
11918eb6488eSEric Joyner /************************************************************************
1192c19c7afeSEric Joyner  * ixgbe_if_get_counter
11938eb6488eSEric Joyner  ************************************************************************/
1194758cc3dcSJack F Vogel static uint64_t
1195c19c7afeSEric Joyner ixgbe_if_get_counter(if_ctx_t ctx, ift_counter cnt)
1196758cc3dcSJack F Vogel {
1197c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
1198c19c7afeSEric Joyner 	if_t           ifp = iflib_get_ifp(ctx);
1199758cc3dcSJack F Vogel 
1200758cc3dcSJack F Vogel 	switch (cnt) {
1201758cc3dcSJack F Vogel 	case IFCOUNTER_IPACKETS:
1202758cc3dcSJack F Vogel 		return (adapter->ipackets);
1203758cc3dcSJack F Vogel 	case IFCOUNTER_OPACKETS:
1204758cc3dcSJack F Vogel 		return (adapter->opackets);
1205758cc3dcSJack F Vogel 	case IFCOUNTER_IBYTES:
1206758cc3dcSJack F Vogel 		return (adapter->ibytes);
1207758cc3dcSJack F Vogel 	case IFCOUNTER_OBYTES:
1208758cc3dcSJack F Vogel 		return (adapter->obytes);
1209758cc3dcSJack F Vogel 	case IFCOUNTER_IMCASTS:
1210758cc3dcSJack F Vogel 		return (adapter->imcasts);
1211758cc3dcSJack F Vogel 	case IFCOUNTER_OMCASTS:
1212758cc3dcSJack F Vogel 		return (adapter->omcasts);
1213758cc3dcSJack F Vogel 	case IFCOUNTER_COLLISIONS:
1214758cc3dcSJack F Vogel 		return (0);
1215758cc3dcSJack F Vogel 	case IFCOUNTER_IQDROPS:
1216758cc3dcSJack F Vogel 		return (adapter->iqdrops);
1217625d12c6SJohn Baldwin 	case IFCOUNTER_OQDROPS:
1218c19c7afeSEric Joyner 		return (0);
1219758cc3dcSJack F Vogel 	case IFCOUNTER_IERRORS:
1220758cc3dcSJack F Vogel 		return (adapter->ierrors);
1221758cc3dcSJack F Vogel 	default:
1222758cc3dcSJack F Vogel 		return (if_get_counter_default(ifp, cnt));
1223758cc3dcSJack F Vogel 	}
1224c19c7afeSEric Joyner } /* ixgbe_if_get_counter */
1225758cc3dcSJack F Vogel 
12268eb6488eSEric Joyner /************************************************************************
1227bca38080SAndrew Gallatin  * ixgbe_if_i2c_req
1228bca38080SAndrew Gallatin  ************************************************************************/
1229bca38080SAndrew Gallatin static int
1230bca38080SAndrew Gallatin ixgbe_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req)
1231bca38080SAndrew Gallatin {
1232bca38080SAndrew Gallatin 	struct adapter		*adapter = iflib_get_softc(ctx);
1233bca38080SAndrew Gallatin 	struct ixgbe_hw 	*hw = &adapter->hw;
1234bca38080SAndrew Gallatin 	int 			i;
1235bca38080SAndrew Gallatin 
1236bca38080SAndrew Gallatin 
1237bca38080SAndrew Gallatin 	if (hw->phy.ops.read_i2c_byte == NULL)
1238bca38080SAndrew Gallatin 		return (ENXIO);
1239bca38080SAndrew Gallatin 	for (i = 0; i < req->len; i++)
1240bca38080SAndrew Gallatin 		hw->phy.ops.read_i2c_byte(hw, req->offset + i,
1241bca38080SAndrew Gallatin 		    req->dev_addr, &req->data[i]);
1242bca38080SAndrew Gallatin 	return (0);
1243bca38080SAndrew Gallatin } /* ixgbe_if_i2c_req */
1244bca38080SAndrew Gallatin 
1245bca38080SAndrew Gallatin /************************************************************************
12468eb6488eSEric Joyner  * ixgbe_add_media_types
12478eb6488eSEric Joyner  ************************************************************************/
12488eb6488eSEric Joyner static void
1249c19c7afeSEric Joyner ixgbe_add_media_types(if_ctx_t ctx)
12508eb6488eSEric Joyner {
1251c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
12528eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
1253c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
12548eb6488eSEric Joyner 	u64             layer;
12558eb6488eSEric Joyner 
1256c19c7afeSEric Joyner 	layer = adapter->phy_layer = ixgbe_get_supported_physical_layer(hw);
12578eb6488eSEric Joyner 
12588eb6488eSEric Joyner 	/* Media types with matching FreeBSD media defines */
12598eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T)
1260c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_10G_T, 0, NULL);
12618eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T)
1262c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_1000_T, 0, NULL);
12638eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_100BASE_TX)
1264c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_100_TX, 0, NULL);
12658eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
1266c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_10_T, 0, NULL);
12678eb6488eSEric Joyner 
12688eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
12698eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
1270c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_10G_TWINAX, 0,
12718eb6488eSEric Joyner 		    NULL);
12728eb6488eSEric Joyner 
12738eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR) {
1274c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_10G_LR, 0, NULL);
12758eb6488eSEric Joyner 		if (hw->phy.multispeed_fiber)
1276c19c7afeSEric Joyner 			ifmedia_add(adapter->media, IFM_ETHER | IFM_1000_LX, 0,
12778eb6488eSEric Joyner 			    NULL);
12788eb6488eSEric Joyner 	}
12798eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
1280c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
12818eb6488eSEric Joyner 		if (hw->phy.multispeed_fiber)
1282c19c7afeSEric Joyner 			ifmedia_add(adapter->media, IFM_ETHER | IFM_1000_SX, 0,
12838eb6488eSEric Joyner 			    NULL);
12848eb6488eSEric Joyner 	} else if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
1285c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_1000_SX, 0, NULL);
12868eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
1287c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
12888eb6488eSEric Joyner 
12898eb6488eSEric Joyner #ifdef IFM_ETH_XTYPE
12908eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
1291c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_10G_KR, 0, NULL);
12928eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4)
1293c19c7afeSEric Joyner 		ifmedia_add( adapter->media, IFM_ETHER | IFM_10G_KX4, 0, NULL);
12948eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
1295c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_1000_KX, 0, NULL);
12968eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX)
1297c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_2500_KX, 0, NULL);
12988eb6488eSEric Joyner #else
12998eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
13008eb6488eSEric Joyner 		device_printf(dev, "Media supported: 10GbaseKR\n");
13018eb6488eSEric Joyner 		device_printf(dev, "10GbaseKR mapped to 10GbaseSR\n");
1302c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
13038eb6488eSEric Joyner 	}
13048eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
13058eb6488eSEric Joyner 		device_printf(dev, "Media supported: 10GbaseKX4\n");
13068eb6488eSEric Joyner 		device_printf(dev, "10GbaseKX4 mapped to 10GbaseCX4\n");
1307c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
13088eb6488eSEric Joyner 	}
13098eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX) {
13108eb6488eSEric Joyner 		device_printf(dev, "Media supported: 1000baseKX\n");
13118eb6488eSEric Joyner 		device_printf(dev, "1000baseKX mapped to 1000baseCX\n");
1312c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_1000_CX, 0, NULL);
13138eb6488eSEric Joyner 	}
13148eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX) {
13158eb6488eSEric Joyner 		device_printf(dev, "Media supported: 2500baseKX\n");
13168eb6488eSEric Joyner 		device_printf(dev, "2500baseKX mapped to 2500baseSX\n");
1317c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_2500_SX, 0, NULL);
13188eb6488eSEric Joyner 	}
13198eb6488eSEric Joyner #endif
13208eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_BX)
13218eb6488eSEric Joyner 		device_printf(dev, "Media supported: 1000baseBX\n");
13228eb6488eSEric Joyner 
13238eb6488eSEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_82598AT) {
1324c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_1000_T | IFM_FDX,
13258eb6488eSEric Joyner 		    0, NULL);
1326c19c7afeSEric Joyner 		ifmedia_add(adapter->media, IFM_ETHER | IFM_1000_T, 0, NULL);
13278eb6488eSEric Joyner 	}
13288eb6488eSEric Joyner 
1329c19c7afeSEric Joyner 	ifmedia_add(adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
13308eb6488eSEric Joyner } /* ixgbe_add_media_types */
13318eb6488eSEric Joyner 
13328eb6488eSEric Joyner /************************************************************************
13338eb6488eSEric Joyner  * ixgbe_is_sfp
13348eb6488eSEric Joyner  ************************************************************************/
13358eb6488eSEric Joyner static inline bool
13368eb6488eSEric Joyner ixgbe_is_sfp(struct ixgbe_hw *hw)
13378eb6488eSEric Joyner {
13388eb6488eSEric Joyner 	switch (hw->mac.type) {
13398eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
13408eb6488eSEric Joyner 		if (hw->phy.type == ixgbe_phy_nl)
1341c19c7afeSEric Joyner 			return (TRUE);
1342c19c7afeSEric Joyner 		return (FALSE);
13438eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
13448eb6488eSEric Joyner 		switch (hw->mac.ops.get_media_type(hw)) {
13458eb6488eSEric Joyner 		case ixgbe_media_type_fiber:
13468eb6488eSEric Joyner 		case ixgbe_media_type_fiber_qsfp:
1347c19c7afeSEric Joyner 			return (TRUE);
13488eb6488eSEric Joyner 		default:
1349c19c7afeSEric Joyner 			return (FALSE);
13508eb6488eSEric Joyner 		}
13518eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
13528eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
13538eb6488eSEric Joyner 		if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_fiber)
1354c19c7afeSEric Joyner 			return (TRUE);
1355c19c7afeSEric Joyner 		return (FALSE);
13568eb6488eSEric Joyner 	default:
1357c19c7afeSEric Joyner 		return (FALSE);
13588eb6488eSEric Joyner 	}
13598eb6488eSEric Joyner } /* ixgbe_is_sfp */
13608eb6488eSEric Joyner 
13618eb6488eSEric Joyner /************************************************************************
13628eb6488eSEric Joyner  * ixgbe_config_link
13638eb6488eSEric Joyner  ************************************************************************/
13648eb6488eSEric Joyner static void
13658eb6488eSEric Joyner ixgbe_config_link(struct adapter *adapter)
13668eb6488eSEric Joyner {
13678eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
13688eb6488eSEric Joyner 	u32             autoneg, err = 0;
13698eb6488eSEric Joyner 	bool            sfp, negotiate;
13708eb6488eSEric Joyner 
13718eb6488eSEric Joyner 	sfp = ixgbe_is_sfp(hw);
13728eb6488eSEric Joyner 
13738eb6488eSEric Joyner 	if (sfp) {
1374c19c7afeSEric Joyner 		GROUPTASK_ENQUEUE(&adapter->mod_task);
13758eb6488eSEric Joyner 	} else {
13768eb6488eSEric Joyner 		if (hw->mac.ops.check_link)
13778eb6488eSEric Joyner 			err = ixgbe_check_link(hw, &adapter->link_speed,
13788eb6488eSEric Joyner 			    &adapter->link_up, FALSE);
13798eb6488eSEric Joyner 		if (err)
1380c19c7afeSEric Joyner 			return;
13818eb6488eSEric Joyner 		autoneg = hw->phy.autoneg_advertised;
13828eb6488eSEric Joyner 		if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
13838eb6488eSEric Joyner 			err = hw->mac.ops.get_link_capabilities(hw, &autoneg,
13848eb6488eSEric Joyner 			    &negotiate);
13858eb6488eSEric Joyner 		if (err)
1386c19c7afeSEric Joyner 			return;
13878eb6488eSEric Joyner 		if (hw->mac.ops.setup_link)
13888eb6488eSEric Joyner 			err = hw->mac.ops.setup_link(hw, autoneg,
13898eb6488eSEric Joyner 			    adapter->link_up);
13908eb6488eSEric Joyner 	}
13918eb6488eSEric Joyner 
13928eb6488eSEric Joyner } /* ixgbe_config_link */
13938eb6488eSEric Joyner 
13948eb6488eSEric Joyner /************************************************************************
13958eb6488eSEric Joyner  * ixgbe_update_stats_counters - Update board statistics counters.
13968eb6488eSEric Joyner  ************************************************************************/
13978eb6488eSEric Joyner static void
13988eb6488eSEric Joyner ixgbe_update_stats_counters(struct adapter *adapter)
13998eb6488eSEric Joyner {
14008eb6488eSEric Joyner 	struct ixgbe_hw       *hw = &adapter->hw;
14018eb6488eSEric Joyner 	struct ixgbe_hw_stats *stats = &adapter->stats.pf;
14028eb6488eSEric Joyner 	u32                   missed_rx = 0, bprc, lxon, lxoff, total;
14038eb6488eSEric Joyner 	u64                   total_missed_rx = 0;
14048eb6488eSEric Joyner 
14058eb6488eSEric Joyner 	stats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
14068eb6488eSEric Joyner 	stats->illerrc += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
14078eb6488eSEric Joyner 	stats->errbc += IXGBE_READ_REG(hw, IXGBE_ERRBC);
14088eb6488eSEric Joyner 	stats->mspdc += IXGBE_READ_REG(hw, IXGBE_MSPDC);
14098eb6488eSEric Joyner 	stats->mpc[0] += IXGBE_READ_REG(hw, IXGBE_MPC(0));
14108eb6488eSEric Joyner 
14118eb6488eSEric Joyner 	for (int i = 0; i < 16; i++) {
14128eb6488eSEric Joyner 		stats->qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
14138eb6488eSEric Joyner 		stats->qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
14148eb6488eSEric Joyner 		stats->qprdc[i] += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
14158eb6488eSEric Joyner 	}
14168eb6488eSEric Joyner 	stats->mlfc += IXGBE_READ_REG(hw, IXGBE_MLFC);
14178eb6488eSEric Joyner 	stats->mrfc += IXGBE_READ_REG(hw, IXGBE_MRFC);
14188eb6488eSEric Joyner 	stats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
14198eb6488eSEric Joyner 
14208eb6488eSEric Joyner 	/* Hardware workaround, gprc counts missed packets */
14218eb6488eSEric Joyner 	stats->gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
14228eb6488eSEric Joyner 	stats->gprc -= missed_rx;
14238eb6488eSEric Joyner 
14248eb6488eSEric Joyner 	if (hw->mac.type != ixgbe_mac_82598EB) {
14258eb6488eSEric Joyner 		stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL) +
14268eb6488eSEric Joyner 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
14278eb6488eSEric Joyner 		stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
14288eb6488eSEric Joyner 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32);
14298eb6488eSEric Joyner 		stats->tor += IXGBE_READ_REG(hw, IXGBE_TORL) +
14308eb6488eSEric Joyner 		    ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
14318eb6488eSEric Joyner 		stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
14328eb6488eSEric Joyner 		stats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
14338eb6488eSEric Joyner 	} else {
14348eb6488eSEric Joyner 		stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
14358eb6488eSEric Joyner 		stats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
14368eb6488eSEric Joyner 		/* 82598 only has a counter in the high register */
14378eb6488eSEric Joyner 		stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
14388eb6488eSEric Joyner 		stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
14398eb6488eSEric Joyner 		stats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
14408eb6488eSEric Joyner 	}
14418eb6488eSEric Joyner 
14428eb6488eSEric Joyner 	/*
14438eb6488eSEric Joyner 	 * Workaround: mprc hardware is incorrectly counting
14448eb6488eSEric Joyner 	 * broadcasts, so for now we subtract those.
1445758cc3dcSJack F Vogel 	 */
14468eb6488eSEric Joyner 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
14478eb6488eSEric Joyner 	stats->bprc += bprc;
14488eb6488eSEric Joyner 	stats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
14498eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
14508eb6488eSEric Joyner 		stats->mprc -= bprc;
14518eb6488eSEric Joyner 
14528eb6488eSEric Joyner 	stats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
14538eb6488eSEric Joyner 	stats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
14548eb6488eSEric Joyner 	stats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
14558eb6488eSEric Joyner 	stats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
14568eb6488eSEric Joyner 	stats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
14578eb6488eSEric Joyner 	stats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
14588eb6488eSEric Joyner 
14598eb6488eSEric Joyner 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
14608eb6488eSEric Joyner 	stats->lxontxc += lxon;
14618eb6488eSEric Joyner 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
14628eb6488eSEric Joyner 	stats->lxofftxc += lxoff;
14638eb6488eSEric Joyner 	total = lxon + lxoff;
14648eb6488eSEric Joyner 
14658eb6488eSEric Joyner 	stats->gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
14668eb6488eSEric Joyner 	stats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
14678eb6488eSEric Joyner 	stats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
14688eb6488eSEric Joyner 	stats->gptc -= total;
14698eb6488eSEric Joyner 	stats->mptc -= total;
14708eb6488eSEric Joyner 	stats->ptc64 -= total;
14718eb6488eSEric Joyner 	stats->gotc -= total * ETHER_MIN_LEN;
14728eb6488eSEric Joyner 
14738eb6488eSEric Joyner 	stats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
14748eb6488eSEric Joyner 	stats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
14758eb6488eSEric Joyner 	stats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
14768eb6488eSEric Joyner 	stats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
14778eb6488eSEric Joyner 	stats->mngprc += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
14788eb6488eSEric Joyner 	stats->mngpdc += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
14798eb6488eSEric Joyner 	stats->mngptc += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
14808eb6488eSEric Joyner 	stats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
14818eb6488eSEric Joyner 	stats->tpt += IXGBE_READ_REG(hw, IXGBE_TPT);
14828eb6488eSEric Joyner 	stats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
14838eb6488eSEric Joyner 	stats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
14848eb6488eSEric Joyner 	stats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
14858eb6488eSEric Joyner 	stats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
14868eb6488eSEric Joyner 	stats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
14878eb6488eSEric Joyner 	stats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
14888eb6488eSEric Joyner 	stats->xec += IXGBE_READ_REG(hw, IXGBE_XEC);
14898eb6488eSEric Joyner 	stats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
14908eb6488eSEric Joyner 	stats->fclast += IXGBE_READ_REG(hw, IXGBE_FCLAST);
14918eb6488eSEric Joyner 	/* Only read FCOE on 82599 */
14928eb6488eSEric Joyner 	if (hw->mac.type != ixgbe_mac_82598EB) {
14938eb6488eSEric Joyner 		stats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
14948eb6488eSEric Joyner 		stats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
14958eb6488eSEric Joyner 		stats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
14968eb6488eSEric Joyner 		stats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
14978eb6488eSEric Joyner 		stats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
14988eb6488eSEric Joyner 	}
14998eb6488eSEric Joyner 
15008eb6488eSEric Joyner 	/* Fill out the OS statistics structure */
15018eb6488eSEric Joyner 	IXGBE_SET_IPACKETS(adapter, stats->gprc);
15028eb6488eSEric Joyner 	IXGBE_SET_OPACKETS(adapter, stats->gptc);
15038eb6488eSEric Joyner 	IXGBE_SET_IBYTES(adapter, stats->gorc);
15048eb6488eSEric Joyner 	IXGBE_SET_OBYTES(adapter, stats->gotc);
15058eb6488eSEric Joyner 	IXGBE_SET_IMCASTS(adapter, stats->mprc);
15068eb6488eSEric Joyner 	IXGBE_SET_OMCASTS(adapter, stats->mptc);
15078eb6488eSEric Joyner 	IXGBE_SET_COLLISIONS(adapter, 0);
15088eb6488eSEric Joyner 	IXGBE_SET_IQDROPS(adapter, total_missed_rx);
15098eb6488eSEric Joyner 	IXGBE_SET_IERRORS(adapter, stats->crcerrs + stats->rlec);
15108eb6488eSEric Joyner } /* ixgbe_update_stats_counters */
15118eb6488eSEric Joyner 
15128eb6488eSEric Joyner /************************************************************************
15138eb6488eSEric Joyner  * ixgbe_add_hw_stats
15148eb6488eSEric Joyner  *
15158eb6488eSEric Joyner  *   Add sysctl variables, one per statistic, to the system.
15168eb6488eSEric Joyner  ************************************************************************/
15178eb6488eSEric Joyner static void
15188eb6488eSEric Joyner ixgbe_add_hw_stats(struct adapter *adapter)
15198eb6488eSEric Joyner {
1520c19c7afeSEric Joyner 	device_t               dev = iflib_get_dev(adapter->ctx);
1521c19c7afeSEric Joyner 	struct ix_rx_queue     *rx_que;
1522c19c7afeSEric Joyner 	struct ix_tx_queue     *tx_que;
15238eb6488eSEric Joyner 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
15248eb6488eSEric Joyner 	struct sysctl_oid      *tree = device_get_sysctl_tree(dev);
15258eb6488eSEric Joyner 	struct sysctl_oid_list *child = SYSCTL_CHILDREN(tree);
15268eb6488eSEric Joyner 	struct ixgbe_hw_stats  *stats = &adapter->stats.pf;
15278eb6488eSEric Joyner 	struct sysctl_oid      *stat_node, *queue_node;
15288eb6488eSEric Joyner 	struct sysctl_oid_list *stat_list, *queue_list;
1529c19c7afeSEric Joyner 	int                    i;
15308eb6488eSEric Joyner 
15318eb6488eSEric Joyner #define QUEUE_NAME_LEN 32
15328eb6488eSEric Joyner 	char                   namebuf[QUEUE_NAME_LEN];
15338eb6488eSEric Joyner 
15348eb6488eSEric Joyner 	/* Driver Statistics */
15358eb6488eSEric Joyner 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dropped",
15368eb6488eSEric Joyner 	    CTLFLAG_RD, &adapter->dropped_pkts, "Driver dropped packets");
15378eb6488eSEric Joyner 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "watchdog_events",
15388eb6488eSEric Joyner 	    CTLFLAG_RD, &adapter->watchdog_events, "Watchdog timeouts");
15398eb6488eSEric Joyner 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "link_irq",
15408eb6488eSEric Joyner 	    CTLFLAG_RD, &adapter->link_irq, "Link MSI-X IRQ Handled");
15418eb6488eSEric Joyner 
1542c19c7afeSEric Joyner 	for (i = 0, tx_que = adapter->tx_queues; i < adapter->num_tx_queues; i++, tx_que++) {
1543c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
15448eb6488eSEric Joyner 		snprintf(namebuf, QUEUE_NAME_LEN, "queue%d", i);
15458eb6488eSEric Joyner 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
15468eb6488eSEric Joyner 		    CTLFLAG_RD, NULL, "Queue Name");
15478eb6488eSEric Joyner 		queue_list = SYSCTL_CHILDREN(queue_node);
15488eb6488eSEric Joyner 
15498eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_head",
15508eb6488eSEric Joyner 		    CTLTYPE_UINT | CTLFLAG_RD, txr, sizeof(txr),
15518eb6488eSEric Joyner 		    ixgbe_sysctl_tdh_handler, "IU", "Transmit Descriptor Head");
15528eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_tail",
15538eb6488eSEric Joyner 		    CTLTYPE_UINT | CTLFLAG_RD, txr, sizeof(txr),
15548eb6488eSEric Joyner 		    ixgbe_sysctl_tdt_handler, "IU", "Transmit Descriptor Tail");
15558eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tso_tx",
15568eb6488eSEric Joyner 		    CTLFLAG_RD, &txr->tso_tx, "TSO");
15578eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tx_packets",
15588eb6488eSEric Joyner 		    CTLFLAG_RD, &txr->total_packets,
15598eb6488eSEric Joyner 		    "Queue Packets Transmitted");
15608eb6488eSEric Joyner 	}
15618eb6488eSEric Joyner 
1562c19c7afeSEric Joyner 	for (i = 0, rx_que = adapter->rx_queues; i < adapter->num_rx_queues; i++, rx_que++) {
1563c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
15648eb6488eSEric Joyner 		snprintf(namebuf, QUEUE_NAME_LEN, "queue%d", i);
15658eb6488eSEric Joyner 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
15668eb6488eSEric Joyner 		    CTLFLAG_RD, NULL, "Queue Name");
15678eb6488eSEric Joyner 		queue_list = SYSCTL_CHILDREN(queue_node);
15688eb6488eSEric Joyner 
1569c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "interrupt_rate",
1570c19c7afeSEric Joyner 		    CTLTYPE_UINT | CTLFLAG_RW, &adapter->rx_queues[i],
1571c19c7afeSEric Joyner 		    sizeof(&adapter->rx_queues[i]),
1572c19c7afeSEric Joyner 		    ixgbe_sysctl_interrupt_rate_handler, "IU",
1573c19c7afeSEric Joyner 		    "Interrupt Rate");
1574c19c7afeSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "irqs",
1575c19c7afeSEric Joyner 		    CTLFLAG_RD, &(adapter->rx_queues[i].irqs),
1576c19c7afeSEric Joyner 		    "irqs on this queue");
15778eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_head",
15788eb6488eSEric Joyner 		    CTLTYPE_UINT | CTLFLAG_RD, rxr, sizeof(rxr),
15798eb6488eSEric Joyner 		    ixgbe_sysctl_rdh_handler, "IU", "Receive Descriptor Head");
15808eb6488eSEric Joyner 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_tail",
15818eb6488eSEric Joyner 		    CTLTYPE_UINT | CTLFLAG_RD, rxr, sizeof(rxr),
15828eb6488eSEric Joyner 		    ixgbe_sysctl_rdt_handler, "IU", "Receive Descriptor Tail");
15838eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_packets",
15848eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_packets, "Queue Packets Received");
15858eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_bytes",
15868eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_bytes, "Queue Bytes Received");
15878eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_copies",
15888eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_copies, "Copied RX Frames");
15898eb6488eSEric Joyner 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_discarded",
15908eb6488eSEric Joyner 		    CTLFLAG_RD, &rxr->rx_discarded, "Discarded RX packets");
15918eb6488eSEric Joyner 	}
15928eb6488eSEric Joyner 
15938eb6488eSEric Joyner 	/* MAC stats get their own sub node */
15948eb6488eSEric Joyner 
15958eb6488eSEric Joyner 	stat_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "mac_stats",
15968eb6488eSEric Joyner 	    CTLFLAG_RD, NULL, "MAC Statistics");
15978eb6488eSEric Joyner 	stat_list = SYSCTL_CHILDREN(stat_node);
15988eb6488eSEric Joyner 
15998eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "crc_errs",
16008eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->crcerrs, "CRC Errors");
16018eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "ill_errs",
16028eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->illerrc, "Illegal Byte Errors");
16038eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "byte_errs",
16048eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->errbc, "Byte Errors");
16058eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "short_discards",
16068eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mspdc, "MAC Short Packets Discarded");
16078eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "local_faults",
16088eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mlfc, "MAC Local Faults");
16098eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "remote_faults",
16108eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mrfc, "MAC Remote Faults");
16118eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rec_len_errs",
16128eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->rlec, "Receive Length Errors");
16138eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_missed_packets",
16148eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mpc[0], "RX Missed Packet Count");
16158eb6488eSEric Joyner 
16168eb6488eSEric Joyner 	/* Flow Control stats */
16178eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_txd",
16188eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxontxc, "Link XON Transmitted");
16198eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_recvd",
16208eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxonrxc, "Link XON Received");
16218eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_txd",
16228eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxofftxc, "Link XOFF Transmitted");
16238eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_recvd",
16248eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->lxoffrxc, "Link XOFF Received");
16258eb6488eSEric Joyner 
16268eb6488eSEric Joyner 	/* Packet Reception Stats */
16278eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_octets_rcvd",
16288eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->tor, "Total Octets Received");
16298eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_rcvd",
16308eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gorc, "Good Octets Received");
16318eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_rcvd",
16328eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->tpr, "Total Packets Received");
16338eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_rcvd",
16348eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gprc, "Good Packets Received");
16358eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_rcvd",
16368eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mprc, "Multicast Packets Received");
16378eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_rcvd",
16388eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->bprc, "Broadcast Packets Received");
16398eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_64",
16408eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc64, "64 byte frames received ");
16418eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_65_127",
16428eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc127, "65-127 byte frames received");
16438eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_128_255",
16448eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc255, "128-255 byte frames received");
16458eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_256_511",
16468eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc511, "256-511 byte frames received");
16478eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_512_1023",
16488eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc1023, "512-1023 byte frames received");
16498eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_1024_1522",
16508eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->prc1522, "1023-1522 byte frames received");
16518eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_undersized",
16528eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ruc, "Receive Undersized");
16538eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_fragmented",
16548eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->rfc, "Fragmented Packets Received ");
16558eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_oversized",
16568eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->roc, "Oversized Packets Received");
16578eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_jabberd",
16588eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->rjc, "Received Jabber");
16598eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_rcvd",
16608eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mngprc, "Management Packets Received");
16618eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_drpd",
16628eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mngptc, "Management Packets Dropped");
16638eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "checksum_errs",
16648eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->xec, "Checksum Errors");
16658eb6488eSEric Joyner 
16668eb6488eSEric Joyner 	/* Packet Transmission Stats */
16678eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_txd",
16688eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gotc, "Good Octets Transmitted");
16698eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_txd",
16708eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->tpt, "Total Packets Transmitted");
16718eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_txd",
16728eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->gptc, "Good Packets Transmitted");
16738eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_txd",
16748eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->bptc, "Broadcast Packets Transmitted");
16758eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_txd",
16768eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mptc, "Multicast Packets Transmitted");
16778eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_txd",
16788eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->mngptc, "Management Packets Transmitted");
16798eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_64",
16808eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc64, "64 byte frames transmitted ");
16818eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_65_127",
16828eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc127, "65-127 byte frames transmitted");
16838eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_128_255",
16848eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc255, "128-255 byte frames transmitted");
16858eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_256_511",
16868eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc511, "256-511 byte frames transmitted");
16878eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_512_1023",
16888eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc1023, "512-1023 byte frames transmitted");
16898eb6488eSEric Joyner 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_1024_1522",
16908eb6488eSEric Joyner 	    CTLFLAG_RD, &stats->ptc1522, "1024-1522 byte frames transmitted");
16918eb6488eSEric Joyner } /* ixgbe_add_hw_stats */
16928eb6488eSEric Joyner 
16938eb6488eSEric Joyner /************************************************************************
16948eb6488eSEric Joyner  * ixgbe_sysctl_tdh_handler - Transmit Descriptor Head handler function
16958eb6488eSEric Joyner  *
16968eb6488eSEric Joyner  *   Retrieves the TDH value from the hardware
16978eb6488eSEric Joyner  ************************************************************************/
1698758cc3dcSJack F Vogel static int
1699758cc3dcSJack F Vogel ixgbe_sysctl_tdh_handler(SYSCTL_HANDLER_ARGS)
1700758cc3dcSJack F Vogel {
1701758cc3dcSJack F Vogel 	struct tx_ring *txr = ((struct tx_ring *)oidp->oid_arg1);
17028eb6488eSEric Joyner 	int            error;
17038eb6488eSEric Joyner 	unsigned int   val;
1704758cc3dcSJack F Vogel 
17058eb6488eSEric Joyner 	if (!txr)
17068eb6488eSEric Joyner 		return (0);
17078eb6488eSEric Joyner 
17088eb6488eSEric Joyner 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDH(txr->me));
1709758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1710758cc3dcSJack F Vogel 	if (error || !req->newptr)
1711758cc3dcSJack F Vogel 		return error;
1712758cc3dcSJack F Vogel 
17138eb6488eSEric Joyner 	return (0);
17148eb6488eSEric Joyner } /* ixgbe_sysctl_tdh_handler */
17158eb6488eSEric Joyner 
17168eb6488eSEric Joyner /************************************************************************
17178eb6488eSEric Joyner  * ixgbe_sysctl_tdt_handler - Transmit Descriptor Tail handler function
17188eb6488eSEric Joyner  *
1719758cc3dcSJack F Vogel  *   Retrieves the TDT value from the hardware
17208eb6488eSEric Joyner  ************************************************************************/
1721758cc3dcSJack F Vogel static int
1722758cc3dcSJack F Vogel ixgbe_sysctl_tdt_handler(SYSCTL_HANDLER_ARGS)
1723758cc3dcSJack F Vogel {
1724758cc3dcSJack F Vogel 	struct tx_ring *txr = ((struct tx_ring *)oidp->oid_arg1);
17258eb6488eSEric Joyner 	int            error;
17268eb6488eSEric Joyner 	unsigned int   val;
1727758cc3dcSJack F Vogel 
17288eb6488eSEric Joyner 	if (!txr)
17298eb6488eSEric Joyner 		return (0);
17308eb6488eSEric Joyner 
17318eb6488eSEric Joyner 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDT(txr->me));
1732758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1733758cc3dcSJack F Vogel 	if (error || !req->newptr)
1734758cc3dcSJack F Vogel 		return error;
1735758cc3dcSJack F Vogel 
17368eb6488eSEric Joyner 	return (0);
17378eb6488eSEric Joyner } /* ixgbe_sysctl_tdt_handler */
17388eb6488eSEric Joyner 
17398eb6488eSEric Joyner /************************************************************************
17408eb6488eSEric Joyner  * ixgbe_sysctl_rdh_handler - Receive Descriptor Head handler function
17418eb6488eSEric Joyner  *
1742758cc3dcSJack F Vogel  *   Retrieves the RDH value from the hardware
17438eb6488eSEric Joyner  ************************************************************************/
1744758cc3dcSJack F Vogel static int
1745758cc3dcSJack F Vogel ixgbe_sysctl_rdh_handler(SYSCTL_HANDLER_ARGS)
1746758cc3dcSJack F Vogel {
1747758cc3dcSJack F Vogel 	struct rx_ring *rxr = ((struct rx_ring *)oidp->oid_arg1);
17488eb6488eSEric Joyner 	int            error;
17498eb6488eSEric Joyner 	unsigned int   val;
1750758cc3dcSJack F Vogel 
17518eb6488eSEric Joyner 	if (!rxr)
17528eb6488eSEric Joyner 		return (0);
17538eb6488eSEric Joyner 
17548eb6488eSEric Joyner 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDH(rxr->me));
1755758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1756758cc3dcSJack F Vogel 	if (error || !req->newptr)
1757758cc3dcSJack F Vogel 		return error;
1758758cc3dcSJack F Vogel 
17598eb6488eSEric Joyner 	return (0);
17608eb6488eSEric Joyner } /* ixgbe_sysctl_rdh_handler */
17618eb6488eSEric Joyner 
17628eb6488eSEric Joyner /************************************************************************
17638eb6488eSEric Joyner  * ixgbe_sysctl_rdt_handler - Receive Descriptor Tail handler function
17648eb6488eSEric Joyner  *
1765758cc3dcSJack F Vogel  *   Retrieves the RDT value from the hardware
17668eb6488eSEric Joyner  ************************************************************************/
1767758cc3dcSJack F Vogel static int
1768758cc3dcSJack F Vogel ixgbe_sysctl_rdt_handler(SYSCTL_HANDLER_ARGS)
1769758cc3dcSJack F Vogel {
1770758cc3dcSJack F Vogel 	struct rx_ring *rxr = ((struct rx_ring *)oidp->oid_arg1);
17718eb6488eSEric Joyner 	int            error;
17728eb6488eSEric Joyner 	unsigned int   val;
1773758cc3dcSJack F Vogel 
17748eb6488eSEric Joyner 	if (!rxr)
17758eb6488eSEric Joyner 		return (0);
17768eb6488eSEric Joyner 
17778eb6488eSEric Joyner 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDT(rxr->me));
1778758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &val, 0, req);
1779758cc3dcSJack F Vogel 	if (error || !req->newptr)
1780758cc3dcSJack F Vogel 		return error;
17818eb6488eSEric Joyner 
17828eb6488eSEric Joyner 	return (0);
17838eb6488eSEric Joyner } /* ixgbe_sysctl_rdt_handler */
17848eb6488eSEric Joyner 
17858eb6488eSEric Joyner /************************************************************************
1786c19c7afeSEric Joyner  * ixgbe_if_vlan_register
17878eb6488eSEric Joyner  *
17888eb6488eSEric Joyner  *   Run via vlan config EVENT, it enables us to use the
17898eb6488eSEric Joyner  *   HW Filter table since we can get the vlan id. This
17908eb6488eSEric Joyner  *   just creates the entry in the soft version of the
17918eb6488eSEric Joyner  *   VFTA, init will repopulate the real table.
17928eb6488eSEric Joyner  ************************************************************************/
17938eb6488eSEric Joyner static void
1794c19c7afeSEric Joyner ixgbe_if_vlan_register(if_ctx_t ctx, u16 vtag)
17958eb6488eSEric Joyner {
1796c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
17978eb6488eSEric Joyner 	u16            index, bit;
17988eb6488eSEric Joyner 
17998eb6488eSEric Joyner 	index = (vtag >> 5) & 0x7F;
18008eb6488eSEric Joyner 	bit = vtag & 0x1F;
18018eb6488eSEric Joyner 	adapter->shadow_vfta[index] |= (1 << bit);
18028eb6488eSEric Joyner 	++adapter->num_vlans;
1803c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
1804c19c7afeSEric Joyner } /* ixgbe_if_vlan_register */
18058eb6488eSEric Joyner 
18068eb6488eSEric Joyner /************************************************************************
1807c19c7afeSEric Joyner  * ixgbe_if_vlan_unregister
18088eb6488eSEric Joyner  *
18098eb6488eSEric Joyner  *   Run via vlan unconfig EVENT, remove our entry in the soft vfta.
18108eb6488eSEric Joyner  ************************************************************************/
18118eb6488eSEric Joyner static void
1812c19c7afeSEric Joyner ixgbe_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
18138eb6488eSEric Joyner {
1814c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
18158eb6488eSEric Joyner 	u16            index, bit;
18168eb6488eSEric Joyner 
18178eb6488eSEric Joyner 	index = (vtag >> 5) & 0x7F;
18188eb6488eSEric Joyner 	bit = vtag & 0x1F;
18198eb6488eSEric Joyner 	adapter->shadow_vfta[index] &= ~(1 << bit);
18208eb6488eSEric Joyner 	--adapter->num_vlans;
18218eb6488eSEric Joyner 	/* Re-init to load the changes */
1822c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
1823c19c7afeSEric Joyner } /* ixgbe_if_vlan_unregister */
18248eb6488eSEric Joyner 
18258eb6488eSEric Joyner /************************************************************************
18268eb6488eSEric Joyner  * ixgbe_setup_vlan_hw_support
18278eb6488eSEric Joyner  ************************************************************************/
18288eb6488eSEric Joyner static void
1829c19c7afeSEric Joyner ixgbe_setup_vlan_hw_support(if_ctx_t ctx)
18308eb6488eSEric Joyner {
1831c19c7afeSEric Joyner 	struct ifnet	*ifp = iflib_get_ifp(ctx);
1832c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
18338eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
18348eb6488eSEric Joyner 	struct rx_ring  *rxr;
18358eb6488eSEric Joyner 	int             i;
18368eb6488eSEric Joyner 	u32             ctrl;
18378eb6488eSEric Joyner 
18388eb6488eSEric Joyner 
18398eb6488eSEric Joyner 	/*
18408eb6488eSEric Joyner 	 * We get here thru init_locked, meaning
18418eb6488eSEric Joyner 	 * a soft reset, this has already cleared
18428eb6488eSEric Joyner 	 * the VFTA and other state, so if there
18438eb6488eSEric Joyner 	 * have been no vlan's registered do nothing.
18448eb6488eSEric Joyner 	 */
18458eb6488eSEric Joyner 	if (adapter->num_vlans == 0)
18468eb6488eSEric Joyner 		return;
18478eb6488eSEric Joyner 
18488eb6488eSEric Joyner 	/* Setup the queues for vlans */
18499c92bb71SSean Bruno 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1850c19c7afeSEric Joyner 		for (i = 0; i < adapter->num_rx_queues; i++) {
1851c19c7afeSEric Joyner 			rxr = &adapter->rx_queues[i].rxr;
18528eb6488eSEric Joyner 			/* On 82599 the VLAN enable is per/queue in RXDCTL */
18538eb6488eSEric Joyner 			if (hw->mac.type != ixgbe_mac_82598EB) {
18548eb6488eSEric Joyner 				ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
18558eb6488eSEric Joyner 				ctrl |= IXGBE_RXDCTL_VME;
18568eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), ctrl);
18578eb6488eSEric Joyner 			}
18588eb6488eSEric Joyner 			rxr->vtag_strip = TRUE;
1859758cc3dcSJack F Vogel 		}
18609c92bb71SSean Bruno 	}
1861758cc3dcSJack F Vogel 
18628eb6488eSEric Joyner 	if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
18638eb6488eSEric Joyner 		return;
18648eb6488eSEric Joyner 	/*
18658eb6488eSEric Joyner 	 * A soft reset zero's out the VFTA, so
18668eb6488eSEric Joyner 	 * we need to repopulate it now.
18678eb6488eSEric Joyner 	 */
18688eb6488eSEric Joyner 	for (i = 0; i < IXGBE_VFTA_SIZE; i++)
18698eb6488eSEric Joyner 		if (adapter->shadow_vfta[i] != 0)
18708eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(i),
18718eb6488eSEric Joyner 			    adapter->shadow_vfta[i]);
18728eb6488eSEric Joyner 
18738eb6488eSEric Joyner 	ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
18748eb6488eSEric Joyner 	/* Enable the Filter Table if enabled */
18758eb6488eSEric Joyner 	if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
18768eb6488eSEric Joyner 		ctrl &= ~IXGBE_VLNCTRL_CFIEN;
18778eb6488eSEric Joyner 		ctrl |= IXGBE_VLNCTRL_VFE;
18788eb6488eSEric Joyner 	}
18798eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
18808eb6488eSEric Joyner 		ctrl |= IXGBE_VLNCTRL_VME;
18818eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
18828eb6488eSEric Joyner } /* ixgbe_setup_vlan_hw_support */
18838eb6488eSEric Joyner 
18848eb6488eSEric Joyner /************************************************************************
18858eb6488eSEric Joyner  * ixgbe_get_slot_info
18868eb6488eSEric Joyner  *
18878eb6488eSEric Joyner  *   Get the width and transaction speed of
18888eb6488eSEric Joyner  *   the slot this adapter is plugged into.
18898eb6488eSEric Joyner  ************************************************************************/
18908eb6488eSEric Joyner static void
18918eb6488eSEric Joyner ixgbe_get_slot_info(struct adapter *adapter)
18928eb6488eSEric Joyner {
1893c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(adapter->ctx);
18948eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
1895c19c7afeSEric Joyner 	int             bus_info_valid = TRUE;
18968eb6488eSEric Joyner 	u32             offset;
18978eb6488eSEric Joyner 	u16             link;
18988eb6488eSEric Joyner 
18998eb6488eSEric Joyner 	/* Some devices are behind an internal bridge */
19008eb6488eSEric Joyner 	switch (hw->device_id) {
19018eb6488eSEric Joyner 	case IXGBE_DEV_ID_82599_SFP_SF_QP:
19028eb6488eSEric Joyner 	case IXGBE_DEV_ID_82599_QSFP_SF_QP:
19038eb6488eSEric Joyner 		goto get_parent_info;
19048eb6488eSEric Joyner 	default:
19058eb6488eSEric Joyner 		break;
19068eb6488eSEric Joyner 	}
19078eb6488eSEric Joyner 
19088eb6488eSEric Joyner 	ixgbe_get_bus_info(hw);
19098eb6488eSEric Joyner 
19108eb6488eSEric Joyner 	/*
19118eb6488eSEric Joyner 	 * Some devices don't use PCI-E, but there is no need
19128eb6488eSEric Joyner 	 * to display "Unknown" for bus speed and width.
19138eb6488eSEric Joyner 	 */
19148eb6488eSEric Joyner 	switch (hw->mac.type) {
19158eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
19168eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
19178eb6488eSEric Joyner 		return;
19188eb6488eSEric Joyner 	default:
19198eb6488eSEric Joyner 		goto display;
19208eb6488eSEric Joyner 	}
19218eb6488eSEric Joyner 
19228eb6488eSEric Joyner get_parent_info:
19238eb6488eSEric Joyner 	/*
19248eb6488eSEric Joyner 	 * For the Quad port adapter we need to parse back
19258eb6488eSEric Joyner 	 * up the PCI tree to find the speed of the expansion
19268eb6488eSEric Joyner 	 * slot into which this adapter is plugged. A bit more work.
19278eb6488eSEric Joyner 	 */
19288eb6488eSEric Joyner 	dev = device_get_parent(device_get_parent(dev));
19298eb6488eSEric Joyner #ifdef IXGBE_DEBUG
19308eb6488eSEric Joyner 	device_printf(dev, "parent pcib = %x,%x,%x\n", pci_get_bus(dev),
19318eb6488eSEric Joyner 	    pci_get_slot(dev), pci_get_function(dev));
19328eb6488eSEric Joyner #endif
19338eb6488eSEric Joyner 	dev = device_get_parent(device_get_parent(dev));
19348eb6488eSEric Joyner #ifdef IXGBE_DEBUG
19358eb6488eSEric Joyner 	device_printf(dev, "slot pcib = %x,%x,%x\n", pci_get_bus(dev),
19368eb6488eSEric Joyner 	    pci_get_slot(dev), pci_get_function(dev));
19378eb6488eSEric Joyner #endif
19388eb6488eSEric Joyner 	/* Now get the PCI Express Capabilities offset */
19398eb6488eSEric Joyner 	if (pci_find_cap(dev, PCIY_EXPRESS, &offset)) {
19408eb6488eSEric Joyner 		/*
19418eb6488eSEric Joyner 		 * Hmm...can't get PCI-Express capabilities.
19428eb6488eSEric Joyner 		 * Falling back to default method.
19438eb6488eSEric Joyner 		 */
19448eb6488eSEric Joyner 		bus_info_valid = FALSE;
19458eb6488eSEric Joyner 		ixgbe_get_bus_info(hw);
19468eb6488eSEric Joyner 		goto display;
19478eb6488eSEric Joyner 	}
19488eb6488eSEric Joyner 	/* ...and read the Link Status Register */
19498eb6488eSEric Joyner 	link = pci_read_config(dev, offset + PCIER_LINK_STA, 2);
19508eb6488eSEric Joyner 	ixgbe_set_pci_config_data_generic(hw, link);
19518eb6488eSEric Joyner 
19528eb6488eSEric Joyner display:
19538eb6488eSEric Joyner 	device_printf(dev, "PCI Express Bus: Speed %s %s\n",
19548eb6488eSEric Joyner 	    ((hw->bus.speed == ixgbe_bus_speed_8000)    ? "8.0GT/s"  :
19558eb6488eSEric Joyner 	     (hw->bus.speed == ixgbe_bus_speed_5000)    ? "5.0GT/s"  :
19568eb6488eSEric Joyner 	     (hw->bus.speed == ixgbe_bus_speed_2500)    ? "2.5GT/s"  :
19578eb6488eSEric Joyner 	     "Unknown"),
19588eb6488eSEric Joyner 	    ((hw->bus.width == ixgbe_bus_width_pcie_x8) ? "Width x8" :
19598eb6488eSEric Joyner 	     (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "Width x4" :
19608eb6488eSEric Joyner 	     (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "Width x1" :
19618eb6488eSEric Joyner 	     "Unknown"));
19628eb6488eSEric Joyner 
19638eb6488eSEric Joyner 	if (bus_info_valid) {
19648eb6488eSEric Joyner 		if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
19658eb6488eSEric Joyner 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
19668eb6488eSEric Joyner 		    (hw->bus.speed == ixgbe_bus_speed_2500))) {
19678eb6488eSEric Joyner 			device_printf(dev, "PCI-Express bandwidth available for this card\n     is not sufficient for optimal performance.\n");
19688eb6488eSEric Joyner 			device_printf(dev, "For optimal performance a x8 PCIE, or x4 PCIE Gen2 slot is required.\n");
19698eb6488eSEric Joyner 		}
19708eb6488eSEric Joyner 		if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
19718eb6488eSEric Joyner 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
19728eb6488eSEric Joyner 		    (hw->bus.speed < ixgbe_bus_speed_8000))) {
19738eb6488eSEric Joyner 			device_printf(dev, "PCI-Express bandwidth available for this card\n     is not sufficient for optimal performance.\n");
19748eb6488eSEric Joyner 			device_printf(dev, "For optimal performance a x8 PCIE Gen3 slot is required.\n");
19758eb6488eSEric Joyner 		}
19768eb6488eSEric Joyner 	} else
19778eb6488eSEric Joyner 		device_printf(dev, "Unable to determine slot speed/width. The speed/width reported are that of the internal switch.\n");
19788eb6488eSEric Joyner 
19798eb6488eSEric Joyner 	return;
19808eb6488eSEric Joyner } /* ixgbe_get_slot_info */
19818eb6488eSEric Joyner 
19828eb6488eSEric Joyner /************************************************************************
1983c19c7afeSEric Joyner  * ixgbe_if_msix_intr_assign
1984c19c7afeSEric Joyner  *
1985c19c7afeSEric Joyner  *   Setup MSI-X Interrupt resources and handlers
19868eb6488eSEric Joyner  ************************************************************************/
1987c19c7afeSEric Joyner static int
1988c19c7afeSEric Joyner ixgbe_if_msix_intr_assign(if_ctx_t ctx, int msix)
19898eb6488eSEric Joyner {
1990c19c7afeSEric Joyner 	struct adapter     *adapter = iflib_get_softc(ctx);
1991c19c7afeSEric Joyner 	struct ix_rx_queue *rx_que = adapter->rx_queues;
1992c19c7afeSEric Joyner 	struct ix_tx_queue *tx_que;
1993c19c7afeSEric Joyner 	int                error, rid, vector = 0;
1994c19c7afeSEric Joyner 	int                cpu_id = 0;
1995c19c7afeSEric Joyner 	char               buf[16];
19968eb6488eSEric Joyner 
1997c19c7afeSEric Joyner 	/* Admin Que is vector 0*/
1998c19c7afeSEric Joyner 	rid = vector + 1;
1999c19c7afeSEric Joyner 	for (int i = 0; i < adapter->num_rx_queues; i++, vector++, rx_que++) {
2000c19c7afeSEric Joyner 		rid = vector + 1;
2001c19c7afeSEric Joyner 
2002c19c7afeSEric Joyner 		snprintf(buf, sizeof(buf), "rxq%d", i);
2003c19c7afeSEric Joyner 		error = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
2004c19c7afeSEric Joyner 		    IFLIB_INTR_RX, ixgbe_msix_que, rx_que, rx_que->rxr.me, buf);
2005c19c7afeSEric Joyner 
2006c19c7afeSEric Joyner 		if (error) {
2007c19c7afeSEric Joyner 			device_printf(iflib_get_dev(ctx),
2008c19c7afeSEric Joyner 			    "Failed to allocate que int %d err: %d", i, error);
2009c19c7afeSEric Joyner 			adapter->num_rx_queues = i + 1;
2010c19c7afeSEric Joyner 			goto fail;
20118eb6488eSEric Joyner 		}
20128eb6488eSEric Joyner 
2013c19c7afeSEric Joyner 		rx_que->msix = vector;
2014c19c7afeSEric Joyner 		adapter->active_queues |= (u64)(1 << rx_que->msix);
2015c19c7afeSEric Joyner 		if (adapter->feat_en & IXGBE_FEATURE_RSS) {
2016c19c7afeSEric Joyner 			/*
2017c19c7afeSEric Joyner 			 * The queue ID is used as the RSS layer bucket ID.
2018c19c7afeSEric Joyner 			 * We look up the queue ID -> RSS CPU ID and select
2019c19c7afeSEric Joyner 			 * that.
2020c19c7afeSEric Joyner 			 */
2021c19c7afeSEric Joyner 			cpu_id = rss_getcpu(i % rss_getnumbuckets());
20228eb6488eSEric Joyner 		} else {
2023c19c7afeSEric Joyner 			/*
2024*b97de13aSMarius Strobl 			 * Bind the MSI-X vector, and thus the
2025c19c7afeSEric Joyner 			 * rings to the corresponding cpu.
2026c19c7afeSEric Joyner 			 *
2027c19c7afeSEric Joyner 			 * This just happens to match the default RSS
2028c19c7afeSEric Joyner 			 * round-robin bucket -> queue -> CPU allocation.
2029c19c7afeSEric Joyner 			 */
2030c19c7afeSEric Joyner 			if (adapter->num_rx_queues > 1)
2031c19c7afeSEric Joyner 				cpu_id = i;
20328eb6488eSEric Joyner 		}
20338eb6488eSEric Joyner 
2034c19c7afeSEric Joyner 	}
2035c19c7afeSEric Joyner 	for (int i = 0; i < adapter->num_tx_queues; i++) {
2036c19c7afeSEric Joyner 		snprintf(buf, sizeof(buf), "txq%d", i);
2037c19c7afeSEric Joyner 		tx_que = &adapter->tx_queues[i];
2038c19c7afeSEric Joyner 		tx_que->msix = i % adapter->num_rx_queues;
2039c19c7afeSEric Joyner 		iflib_softirq_alloc_generic(ctx,
2040c19c7afeSEric Joyner 		    &adapter->rx_queues[tx_que->msix].que_irq,
2041c19c7afeSEric Joyner 		    IFLIB_INTR_TX, tx_que, tx_que->txr.me, buf);
2042c19c7afeSEric Joyner 	}
2043c19c7afeSEric Joyner 	rid = vector + 1;
2044c19c7afeSEric Joyner 	error = iflib_irq_alloc_generic(ctx, &adapter->irq, rid,
2045c19c7afeSEric Joyner 	    IFLIB_INTR_ADMIN, ixgbe_msix_link, adapter, 0, "aq");
2046c19c7afeSEric Joyner 	if (error) {
2047c19c7afeSEric Joyner 		device_printf(iflib_get_dev(ctx),
2048c19c7afeSEric Joyner 		    "Failed to register admin handler");
2049c19c7afeSEric Joyner 		return (error);
2050c19c7afeSEric Joyner 	}
2051c19c7afeSEric Joyner 
2052c19c7afeSEric Joyner 	adapter->vector = vector;
2053c19c7afeSEric Joyner 
2054c19c7afeSEric Joyner 	return (0);
2055c19c7afeSEric Joyner fail:
2056c19c7afeSEric Joyner 	iflib_irq_free(ctx, &adapter->irq);
2057c19c7afeSEric Joyner 	rx_que = adapter->rx_queues;
2058c19c7afeSEric Joyner 	for (int i = 0; i < adapter->num_rx_queues; i++, rx_que++)
2059c19c7afeSEric Joyner 		iflib_irq_free(ctx, &rx_que->que_irq);
2060c19c7afeSEric Joyner 
2061c19c7afeSEric Joyner 	return (error);
2062c19c7afeSEric Joyner } /* ixgbe_if_msix_intr_assign */
2063c19c7afeSEric Joyner 
2064c19c7afeSEric Joyner /*********************************************************************
20658eb6488eSEric Joyner  * ixgbe_msix_que - MSI-X Queue Interrupt Service routine
2066c19c7afeSEric Joyner  **********************************************************************/
2067c19c7afeSEric Joyner static int
20688eb6488eSEric Joyner ixgbe_msix_que(void *arg)
20698eb6488eSEric Joyner {
2070c19c7afeSEric Joyner 	struct ix_rx_queue *que = arg;
20718eb6488eSEric Joyner 	struct adapter     *adapter = que->adapter;
2072c19c7afeSEric Joyner 	struct ifnet       *ifp = iflib_get_ifp(que->adapter->ctx);
20738eb6488eSEric Joyner 
20748eb6488eSEric Joyner 	/* Protect against spurious interrupts */
20758eb6488eSEric Joyner 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2076c19c7afeSEric Joyner 		return 0;
20778eb6488eSEric Joyner 
20788eb6488eSEric Joyner 	ixgbe_disable_queue(adapter, que->msix);
20798eb6488eSEric Joyner 	++que->irqs;
20808eb6488eSEric Joyner 
2081c19c7afeSEric Joyner 	return (FILTER_SCHEDULE_THREAD);
20828eb6488eSEric Joyner } /* ixgbe_msix_que */
20838eb6488eSEric Joyner 
20848eb6488eSEric Joyner /************************************************************************
20858eb6488eSEric Joyner  * ixgbe_media_status - Media Ioctl callback
20868eb6488eSEric Joyner  *
20878eb6488eSEric Joyner  *   Called whenever the user queries the status of
20888eb6488eSEric Joyner  *   the interface using ifconfig.
20898eb6488eSEric Joyner  ************************************************************************/
20908eb6488eSEric Joyner static void
2091c19c7afeSEric Joyner ixgbe_if_media_status(if_ctx_t ctx, struct ifmediareq * ifmr)
20928eb6488eSEric Joyner {
2093c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
20948eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
20958eb6488eSEric Joyner 	int             layer;
20968eb6488eSEric Joyner 
2097c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_media_status: begin");
2098c19c7afeSEric Joyner 
2099c19c7afeSEric Joyner 	iflib_admin_intr_deferred(ctx);
21008eb6488eSEric Joyner 
21018eb6488eSEric Joyner 	ifmr->ifm_status = IFM_AVALID;
21028eb6488eSEric Joyner 	ifmr->ifm_active = IFM_ETHER;
21038eb6488eSEric Joyner 
2104c19c7afeSEric Joyner 	if (!adapter->link_active)
21058eb6488eSEric Joyner 		return;
21068eb6488eSEric Joyner 
21078eb6488eSEric Joyner 	ifmr->ifm_status |= IFM_ACTIVE;
21088eb6488eSEric Joyner 	layer = adapter->phy_layer;
21098eb6488eSEric Joyner 
21108eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T ||
21118eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_T ||
21128eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_100BASE_TX ||
21138eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
21148eb6488eSEric Joyner 		switch (adapter->link_speed) {
21158eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
21168eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
21178eb6488eSEric Joyner 			break;
21188eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
21198eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
21208eb6488eSEric Joyner 			break;
21218eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_100_FULL:
21228eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
21238eb6488eSEric Joyner 			break;
21248eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10_FULL:
21258eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10_T | IFM_FDX;
21268eb6488eSEric Joyner 			break;
21278eb6488eSEric Joyner 		}
21288eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
21298eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
21308eb6488eSEric Joyner 		switch (adapter->link_speed) {
21318eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
21328eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_TWINAX | IFM_FDX;
21338eb6488eSEric Joyner 			break;
21348eb6488eSEric Joyner 		}
21358eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR)
21368eb6488eSEric Joyner 		switch (adapter->link_speed) {
21378eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
21388eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_LR | IFM_FDX;
21398eb6488eSEric Joyner 			break;
21408eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
21418eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
21428eb6488eSEric Joyner 			break;
21438eb6488eSEric Joyner 		}
21448eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LRM)
21458eb6488eSEric Joyner 		switch (adapter->link_speed) {
21468eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
21478eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_LRM | IFM_FDX;
21488eb6488eSEric Joyner 			break;
21498eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
21508eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
21518eb6488eSEric Joyner 			break;
21528eb6488eSEric Joyner 		}
21538eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR ||
21548eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
21558eb6488eSEric Joyner 		switch (adapter->link_speed) {
21568eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
21578eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
21588eb6488eSEric Joyner 			break;
21598eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
21608eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
21618eb6488eSEric Joyner 			break;
21628eb6488eSEric Joyner 		}
21638eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
21648eb6488eSEric Joyner 		switch (adapter->link_speed) {
21658eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
21668eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
21678eb6488eSEric Joyner 			break;
21688eb6488eSEric Joyner 		}
21698eb6488eSEric Joyner 	/*
21708eb6488eSEric Joyner 	 * XXX: These need to use the proper media types once
21718eb6488eSEric Joyner 	 * they're added.
21728eb6488eSEric Joyner 	 */
21738eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
21748eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
21758eb6488eSEric Joyner 		switch (adapter->link_speed) {
21768eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
21778eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
21788eb6488eSEric Joyner 			break;
21798eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
21808eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
21818eb6488eSEric Joyner 			break;
21828eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
21838eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
21848eb6488eSEric Joyner 			break;
21858eb6488eSEric Joyner 		}
21868eb6488eSEric Joyner 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
21878eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
21888eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
21898eb6488eSEric Joyner 		switch (adapter->link_speed) {
21908eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
21918eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
21928eb6488eSEric Joyner 			break;
21938eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
21948eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
21958eb6488eSEric Joyner 			break;
21968eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
21978eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
21988eb6488eSEric Joyner 			break;
21998eb6488eSEric Joyner 		}
22008eb6488eSEric Joyner #else
22018eb6488eSEric Joyner 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
22028eb6488eSEric Joyner 		switch (adapter->link_speed) {
22038eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22048eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_KR | IFM_FDX;
22058eb6488eSEric Joyner 			break;
22068eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
22078eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
22088eb6488eSEric Joyner 			break;
22098eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22108eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
22118eb6488eSEric Joyner 			break;
22128eb6488eSEric Joyner 		}
22138eb6488eSEric Joyner 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
22148eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
22158eb6488eSEric Joyner 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
22168eb6488eSEric Joyner 		switch (adapter->link_speed) {
22178eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_10GB_FULL:
22188eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_10G_KX4 | IFM_FDX;
22198eb6488eSEric Joyner 			break;
22208eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_2_5GB_FULL:
22218eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
22228eb6488eSEric Joyner 			break;
22238eb6488eSEric Joyner 		case IXGBE_LINK_SPEED_1GB_FULL:
22248eb6488eSEric Joyner 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
22258eb6488eSEric Joyner 			break;
22268eb6488eSEric Joyner 		}
22278eb6488eSEric Joyner #endif
22288eb6488eSEric Joyner 
22298eb6488eSEric Joyner 	/* If nothing is recognized... */
22308eb6488eSEric Joyner 	if (IFM_SUBTYPE(ifmr->ifm_active) == 0)
22318eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_UNKNOWN;
22328eb6488eSEric Joyner 
22338eb6488eSEric Joyner 	/* Display current flow control setting used on link */
22348eb6488eSEric Joyner 	if (hw->fc.current_mode == ixgbe_fc_rx_pause ||
22358eb6488eSEric Joyner 	    hw->fc.current_mode == ixgbe_fc_full)
22368eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
22378eb6488eSEric Joyner 	if (hw->fc.current_mode == ixgbe_fc_tx_pause ||
22388eb6488eSEric Joyner 	    hw->fc.current_mode == ixgbe_fc_full)
22398eb6488eSEric Joyner 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
22408eb6488eSEric Joyner } /* ixgbe_media_status */
22418eb6488eSEric Joyner 
22428eb6488eSEric Joyner /************************************************************************
22438eb6488eSEric Joyner  * ixgbe_media_change - Media Ioctl callback
22448eb6488eSEric Joyner  *
22458eb6488eSEric Joyner  *   Called when the user changes speed/duplex using
22468eb6488eSEric Joyner  *   media/mediopt option with ifconfig.
22478eb6488eSEric Joyner  ************************************************************************/
22488eb6488eSEric Joyner static int
2249c19c7afeSEric Joyner ixgbe_if_media_change(if_ctx_t ctx)
22508eb6488eSEric Joyner {
2251c19c7afeSEric Joyner 	struct adapter   *adapter = iflib_get_softc(ctx);
2252c19c7afeSEric Joyner 	struct ifmedia   *ifm = iflib_get_media(ctx);
22538eb6488eSEric Joyner 	struct ixgbe_hw  *hw = &adapter->hw;
22548eb6488eSEric Joyner 	ixgbe_link_speed speed = 0;
22558eb6488eSEric Joyner 
2256c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_media_change: begin");
22578eb6488eSEric Joyner 
22588eb6488eSEric Joyner 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
22598eb6488eSEric Joyner 		return (EINVAL);
22608eb6488eSEric Joyner 
22618eb6488eSEric Joyner 	if (hw->phy.media_type == ixgbe_media_type_backplane)
2262c19c7afeSEric Joyner 		return (EPERM);
22638eb6488eSEric Joyner 
22648eb6488eSEric Joyner 	/*
22658eb6488eSEric Joyner 	 * We don't actually need to check against the supported
22668eb6488eSEric Joyner 	 * media types of the adapter; ifmedia will take care of
22678eb6488eSEric Joyner 	 * that for us.
22688eb6488eSEric Joyner 	 */
22698eb6488eSEric Joyner 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
22708eb6488eSEric Joyner 	case IFM_AUTO:
22718eb6488eSEric Joyner 	case IFM_10G_T:
22728eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
22738eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
22748eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
22758eb6488eSEric Joyner 		break;
22768eb6488eSEric Joyner 	case IFM_10G_LRM:
22778eb6488eSEric Joyner 	case IFM_10G_LR:
22788eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
22798eb6488eSEric Joyner 	case IFM_10G_SR: /* KR, too */
22808eb6488eSEric Joyner 	case IFM_10G_CX4: /* KX4 */
22818eb6488eSEric Joyner #else
22828eb6488eSEric Joyner 	case IFM_10G_KR:
22838eb6488eSEric Joyner 	case IFM_10G_KX4:
22848eb6488eSEric Joyner #endif
22858eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
22868eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
22878eb6488eSEric Joyner 		break;
22888eb6488eSEric Joyner #ifndef IFM_ETH_XTYPE
22898eb6488eSEric Joyner 	case IFM_1000_CX: /* KX */
22908eb6488eSEric Joyner #else
22918eb6488eSEric Joyner 	case IFM_1000_KX:
22928eb6488eSEric Joyner #endif
22938eb6488eSEric Joyner 	case IFM_1000_LX:
22948eb6488eSEric Joyner 	case IFM_1000_SX:
22958eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
22968eb6488eSEric Joyner 		break;
22978eb6488eSEric Joyner 	case IFM_1000_T:
22988eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
22998eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
23008eb6488eSEric Joyner 		break;
23018eb6488eSEric Joyner 	case IFM_10G_TWINAX:
23028eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
23038eb6488eSEric Joyner 		break;
23048eb6488eSEric Joyner 	case IFM_100_TX:
23058eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_100_FULL;
23068eb6488eSEric Joyner 		break;
23078eb6488eSEric Joyner 	case IFM_10_T:
23088eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10_FULL;
23098eb6488eSEric Joyner 		break;
23108eb6488eSEric Joyner 	default:
23118eb6488eSEric Joyner 		goto invalid;
23128eb6488eSEric Joyner 	}
23138eb6488eSEric Joyner 
23148eb6488eSEric Joyner 	hw->mac.autotry_restart = TRUE;
23158eb6488eSEric Joyner 	hw->mac.ops.setup_link(hw, speed, TRUE);
23168eb6488eSEric Joyner 	adapter->advertise =
23178eb6488eSEric Joyner 	    ((speed & IXGBE_LINK_SPEED_10GB_FULL) ? 4 : 0) |
23188eb6488eSEric Joyner 	    ((speed & IXGBE_LINK_SPEED_1GB_FULL)  ? 2 : 0) |
23198eb6488eSEric Joyner 	    ((speed & IXGBE_LINK_SPEED_100_FULL)  ? 1 : 0) |
23208eb6488eSEric Joyner 	    ((speed & IXGBE_LINK_SPEED_10_FULL)   ? 8 : 0);
23218eb6488eSEric Joyner 
23228eb6488eSEric Joyner 	return (0);
23238eb6488eSEric Joyner 
23248eb6488eSEric Joyner invalid:
2325c19c7afeSEric Joyner 	device_printf(iflib_get_dev(ctx), "Invalid media type!\n");
23268eb6488eSEric Joyner 
23278eb6488eSEric Joyner 	return (EINVAL);
2328c19c7afeSEric Joyner } /* ixgbe_if_media_change */
23298eb6488eSEric Joyner 
23308eb6488eSEric Joyner /************************************************************************
23318eb6488eSEric Joyner  * ixgbe_set_promisc
23328eb6488eSEric Joyner  ************************************************************************/
2333c19c7afeSEric Joyner static int
2334c19c7afeSEric Joyner ixgbe_if_promisc_set(if_ctx_t ctx, int flags)
23358eb6488eSEric Joyner {
2336c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
2337c19c7afeSEric Joyner 	struct ifnet   *ifp = iflib_get_ifp(ctx);
23388eb6488eSEric Joyner 	u32            rctl;
2339c19c7afeSEric Joyner 	int            mcnt = 0;
23408eb6488eSEric Joyner 
23418eb6488eSEric Joyner 	rctl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
23428eb6488eSEric Joyner 	rctl &= (~IXGBE_FCTRL_UPE);
23438eb6488eSEric Joyner 	if (ifp->if_flags & IFF_ALLMULTI)
23448eb6488eSEric Joyner 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
23458eb6488eSEric Joyner 	else {
2346c19c7afeSEric Joyner 		mcnt = if_multiaddr_count(ifp, MAX_NUM_MULTICAST_ADDRESSES);
23478eb6488eSEric Joyner 	}
23488eb6488eSEric Joyner 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
23498eb6488eSEric Joyner 		rctl &= (~IXGBE_FCTRL_MPE);
23508eb6488eSEric Joyner 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, rctl);
23518eb6488eSEric Joyner 
23528eb6488eSEric Joyner 	if (ifp->if_flags & IFF_PROMISC) {
23538eb6488eSEric Joyner 		rctl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
23548eb6488eSEric Joyner 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, rctl);
23558eb6488eSEric Joyner 	} else if (ifp->if_flags & IFF_ALLMULTI) {
23568eb6488eSEric Joyner 		rctl |= IXGBE_FCTRL_MPE;
23578eb6488eSEric Joyner 		rctl &= ~IXGBE_FCTRL_UPE;
23588eb6488eSEric Joyner 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, rctl);
23598eb6488eSEric Joyner 	}
2360c19c7afeSEric Joyner 	return (0);
2361c19c7afeSEric Joyner } /* ixgbe_if_promisc_set */
23628eb6488eSEric Joyner 
23638eb6488eSEric Joyner /************************************************************************
23648eb6488eSEric Joyner  * ixgbe_msix_link - Link status change ISR (MSI/MSI-X)
23658eb6488eSEric Joyner  ************************************************************************/
2366c19c7afeSEric Joyner static int
23678eb6488eSEric Joyner ixgbe_msix_link(void *arg)
23688eb6488eSEric Joyner {
23698eb6488eSEric Joyner 	struct adapter  *adapter = arg;
23708eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
23718eb6488eSEric Joyner 	u32             eicr, eicr_mask;
23728eb6488eSEric Joyner 	s32             retval;
23738eb6488eSEric Joyner 
23748eb6488eSEric Joyner 	++adapter->link_irq;
23758eb6488eSEric Joyner 
23768eb6488eSEric Joyner 	/* Pause other interrupts */
23778eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_OTHER);
23788eb6488eSEric Joyner 
23798eb6488eSEric Joyner 	/* First get the cause */
23808eb6488eSEric Joyner 	eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
23818eb6488eSEric Joyner 	/* Be sure the queue bits are not cleared */
23828eb6488eSEric Joyner 	eicr &= ~IXGBE_EICR_RTX_QUEUE;
23838eb6488eSEric Joyner 	/* Clear interrupt with write */
23848eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
23858eb6488eSEric Joyner 
23868eb6488eSEric Joyner 	/* Link status change */
23878eb6488eSEric Joyner 	if (eicr & IXGBE_EICR_LSC) {
23888eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
2389c19c7afeSEric Joyner 		iflib_admin_intr_deferred(adapter->ctx);
23908eb6488eSEric Joyner 	}
23918eb6488eSEric Joyner 
23928eb6488eSEric Joyner 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
23938eb6488eSEric Joyner 		if ((adapter->feat_en & IXGBE_FEATURE_FDIR) &&
23948eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_FLOW_DIR)) {
23958eb6488eSEric Joyner 			/* This is probably overkill :) */
23968eb6488eSEric Joyner 			if (!atomic_cmpset_int(&adapter->fdir_reinit, 0, 1))
2397c19c7afeSEric Joyner 				return (FILTER_HANDLED);
23988eb6488eSEric Joyner 			/* Disable the interrupt */
2399c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_FLOW_DIR);
2400c19c7afeSEric Joyner 			GROUPTASK_ENQUEUE(&adapter->fdir_task);
2401c19c7afeSEric Joyner 		} else
24028eb6488eSEric Joyner 			if (eicr & IXGBE_EICR_ECC) {
2403c19c7afeSEric Joyner 				device_printf(iflib_get_dev(adapter->ctx),
2404c19c7afeSEric Joyner 				   "\nCRITICAL: ECC ERROR!! Please Reboot!!\n");
24058eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
24068eb6488eSEric Joyner 			}
24078eb6488eSEric Joyner 
24088eb6488eSEric Joyner 		/* Check for over temp condition */
24098eb6488eSEric Joyner 		if (adapter->feat_en & IXGBE_FEATURE_TEMP_SENSOR) {
24108eb6488eSEric Joyner 			switch (adapter->hw.mac.type) {
24118eb6488eSEric Joyner 			case ixgbe_mac_X550EM_a:
24128eb6488eSEric Joyner 				if (!(eicr & IXGBE_EICR_GPI_SDP0_X550EM_a))
24138eb6488eSEric Joyner 					break;
24148eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EIMC,
24158eb6488eSEric Joyner 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
24168eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR,
24178eb6488eSEric Joyner 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
24188eb6488eSEric Joyner 				retval = hw->phy.ops.check_overtemp(hw);
24198eb6488eSEric Joyner 				if (retval != IXGBE_ERR_OVERTEMP)
24208eb6488eSEric Joyner 					break;
2421c19c7afeSEric Joyner 				device_printf(iflib_get_dev(adapter->ctx),
2422c19c7afeSEric Joyner 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2423c19c7afeSEric Joyner 				device_printf(iflib_get_dev(adapter->ctx),
2424c19c7afeSEric Joyner 				    "System shutdown required!\n");
24258eb6488eSEric Joyner 				break;
24268eb6488eSEric Joyner 			default:
24278eb6488eSEric Joyner 				if (!(eicr & IXGBE_EICR_TS))
24288eb6488eSEric Joyner 					break;
24298eb6488eSEric Joyner 				retval = hw->phy.ops.check_overtemp(hw);
24308eb6488eSEric Joyner 				if (retval != IXGBE_ERR_OVERTEMP)
24318eb6488eSEric Joyner 					break;
2432c19c7afeSEric Joyner 				device_printf(iflib_get_dev(adapter->ctx),
2433c19c7afeSEric Joyner 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2434c19c7afeSEric Joyner 				device_printf(iflib_get_dev(adapter->ctx),
2435c19c7afeSEric Joyner 				    "System shutdown required!\n");
24368eb6488eSEric Joyner 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
24378eb6488eSEric Joyner 				break;
24388eb6488eSEric Joyner 			}
24398eb6488eSEric Joyner 		}
24408eb6488eSEric Joyner 
24418eb6488eSEric Joyner 		/* Check for VF message */
24428eb6488eSEric Joyner 		if ((adapter->feat_en & IXGBE_FEATURE_SRIOV) &&
24438eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_MAILBOX))
2444c19c7afeSEric Joyner 			GROUPTASK_ENQUEUE(&adapter->mbx_task);
24458eb6488eSEric Joyner 	}
24468eb6488eSEric Joyner 
24478eb6488eSEric Joyner 	if (ixgbe_is_sfp(hw)) {
24488eb6488eSEric Joyner 		/* Pluggable optics-related interrupt */
24498eb6488eSEric Joyner 		if (hw->mac.type >= ixgbe_mac_X540)
24508eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
24518eb6488eSEric Joyner 		else
24528eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
24538eb6488eSEric Joyner 
24548eb6488eSEric Joyner 		if (eicr & eicr_mask) {
24558eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
2456c19c7afeSEric Joyner 			if (atomic_cmpset_acq_int(&adapter->sfp_reinit, 0, 1))
2457c19c7afeSEric Joyner 				GROUPTASK_ENQUEUE(&adapter->mod_task);
24588eb6488eSEric Joyner 		}
24598eb6488eSEric Joyner 
24608eb6488eSEric Joyner 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
24618eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
24628eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
24638eb6488eSEric Joyner 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
2464c19c7afeSEric Joyner 			if (atomic_cmpset_acq_int(&adapter->sfp_reinit, 0, 1))
2465c19c7afeSEric Joyner 				GROUPTASK_ENQUEUE(&adapter->msf_task);
24668eb6488eSEric Joyner 		}
24678eb6488eSEric Joyner 	}
24688eb6488eSEric Joyner 
24698eb6488eSEric Joyner 	/* Check for fan failure */
24708eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL) {
24718eb6488eSEric Joyner 		ixgbe_check_fan_failure(adapter, eicr, TRUE);
24728eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
24738eb6488eSEric Joyner 	}
24748eb6488eSEric Joyner 
24758eb6488eSEric Joyner 	/* External PHY interrupt */
24768eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
24778eb6488eSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP0_X540)) {
24788eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP0_X540);
2479c19c7afeSEric Joyner 		GROUPTASK_ENQUEUE(&adapter->phy_task);
24808eb6488eSEric Joyner 	}
24818eb6488eSEric Joyner 
24828eb6488eSEric Joyner 	/* Re-enable other interrupts */
24838eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
2484c19c7afeSEric Joyner 
2485c19c7afeSEric Joyner 	return (FILTER_HANDLED);
24868eb6488eSEric Joyner } /* ixgbe_msix_link */
24878eb6488eSEric Joyner 
24888eb6488eSEric Joyner /************************************************************************
24898eb6488eSEric Joyner  * ixgbe_sysctl_interrupt_rate_handler
24908eb6488eSEric Joyner  ************************************************************************/
2491758cc3dcSJack F Vogel static int
2492758cc3dcSJack F Vogel ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)
2493758cc3dcSJack F Vogel {
2494c19c7afeSEric Joyner 	struct ix_rx_queue *que = ((struct ix_rx_queue *)oidp->oid_arg1);
24958eb6488eSEric Joyner 	int                error;
2496758cc3dcSJack F Vogel 	unsigned int       reg, usec, rate;
2497758cc3dcSJack F Vogel 
2498758cc3dcSJack F Vogel 	reg = IXGBE_READ_REG(&que->adapter->hw, IXGBE_EITR(que->msix));
2499758cc3dcSJack F Vogel 	usec = ((reg & 0x0FF8) >> 3);
2500758cc3dcSJack F Vogel 	if (usec > 0)
2501758cc3dcSJack F Vogel 		rate = 500000 / usec;
2502758cc3dcSJack F Vogel 	else
2503758cc3dcSJack F Vogel 		rate = 0;
2504758cc3dcSJack F Vogel 	error = sysctl_handle_int(oidp, &rate, 0, req);
2505758cc3dcSJack F Vogel 	if (error || !req->newptr)
2506758cc3dcSJack F Vogel 		return error;
2507758cc3dcSJack F Vogel 	reg &= ~0xfff; /* default, no limitation */
2508758cc3dcSJack F Vogel 	ixgbe_max_interrupt_rate = 0;
2509758cc3dcSJack F Vogel 	if (rate > 0 && rate < 500000) {
2510758cc3dcSJack F Vogel 		if (rate < 1000)
2511758cc3dcSJack F Vogel 			rate = 1000;
2512758cc3dcSJack F Vogel 		ixgbe_max_interrupt_rate = rate;
2513758cc3dcSJack F Vogel 		reg |= ((4000000/rate) & 0xff8);
2514758cc3dcSJack F Vogel 	}
2515758cc3dcSJack F Vogel 	IXGBE_WRITE_REG(&que->adapter->hw, IXGBE_EITR(que->msix), reg);
2516758cc3dcSJack F Vogel 
25178eb6488eSEric Joyner 	return (0);
25188eb6488eSEric Joyner } /* ixgbe_sysctl_interrupt_rate_handler */
25198eb6488eSEric Joyner 
25208eb6488eSEric Joyner /************************************************************************
25218eb6488eSEric Joyner  * ixgbe_add_device_sysctls
25228eb6488eSEric Joyner  ************************************************************************/
25236f37f232SEric Joyner static void
2524c19c7afeSEric Joyner ixgbe_add_device_sysctls(if_ctx_t ctx)
25256f37f232SEric Joyner {
2526c19c7afeSEric Joyner 	struct adapter         *adapter = iflib_get_softc(ctx);
2527c19c7afeSEric Joyner 	device_t               dev = iflib_get_dev(ctx);
25286f37f232SEric Joyner 	struct ixgbe_hw        *hw = &adapter->hw;
25296f37f232SEric Joyner 	struct sysctl_oid_list *child;
2530c19c7afeSEric Joyner 	struct sysctl_ctx_list *ctx_list;
25316f37f232SEric Joyner 
2532c19c7afeSEric Joyner 	ctx_list = device_get_sysctl_ctx(dev);
25336f37f232SEric Joyner 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
25346f37f232SEric Joyner 
25356f37f232SEric Joyner 	/* Sysctls for all devices */
2536c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fc",
2537c19c7afeSEric Joyner 	    CTLTYPE_INT | CTLFLAG_RW, adapter, 0, ixgbe_sysctl_flowcntl, "I",
2538c19c7afeSEric Joyner 	    IXGBE_SYSCTL_DESC_SET_FC);
25396f37f232SEric Joyner 
2540c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "advertise_speed",
25418eb6488eSEric Joyner 	    CTLTYPE_INT | CTLFLAG_RW, adapter, 0, ixgbe_sysctl_advertise, "I",
25428eb6488eSEric Joyner 	    IXGBE_SYSCTL_DESC_ADV_SPEED);
25436f37f232SEric Joyner 
2544a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
2545a9ca1c79SSean Bruno 	/* testing sysctls (for all devices) */
2546c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "power_state",
25478eb6488eSEric Joyner 	    CTLTYPE_INT | CTLFLAG_RW, adapter, 0, ixgbe_sysctl_power_state,
25488eb6488eSEric Joyner 	    "I", "PCI Power State");
2549a9ca1c79SSean Bruno 
2550c19c7afeSEric Joyner 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "print_rss_config",
2551a9ca1c79SSean Bruno 	    CTLTYPE_STRING | CTLFLAG_RD, adapter, 0,
2552a9ca1c79SSean Bruno 	    ixgbe_sysctl_print_rss_config, "A", "Prints RSS Configuration");
2553a9ca1c79SSean Bruno #endif
2554a9ca1c79SSean Bruno 	/* for X550 series devices */
25556f37f232SEric Joyner 	if (hw->mac.type >= ixgbe_mac_X550)
2556c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "dmac",
2557c19c7afeSEric Joyner 		    CTLTYPE_U16 | CTLFLAG_RW, adapter, 0, ixgbe_sysctl_dmac,
25588eb6488eSEric Joyner 		    "I", "DMA Coalesce");
25596f37f232SEric Joyner 
2560a9ca1c79SSean Bruno 	/* for WoL-capable devices */
2561a9ca1c79SSean Bruno 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
2562c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wol_enable",
25636f37f232SEric Joyner 		    CTLTYPE_INT | CTLFLAG_RW, adapter, 0,
25648eb6488eSEric Joyner 		    ixgbe_sysctl_wol_enable, "I", "Enable/Disable Wake on LAN");
25656f37f232SEric Joyner 
2566c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wufc",
2567c19c7afeSEric Joyner 		    CTLTYPE_U32 | CTLFLAG_RW, adapter, 0, ixgbe_sysctl_wufc,
25688eb6488eSEric Joyner 		    "I", "Enable/Disable Wake Up Filters");
25696f37f232SEric Joyner 	}
25706f37f232SEric Joyner 
2571a9ca1c79SSean Bruno 	/* for X552/X557-AT devices */
25726f37f232SEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
25736f37f232SEric Joyner 		struct sysctl_oid *phy_node;
25746f37f232SEric Joyner 		struct sysctl_oid_list *phy_list;
25756f37f232SEric Joyner 
2576c19c7afeSEric Joyner 		phy_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "phy",
25778eb6488eSEric Joyner 		    CTLFLAG_RD, NULL, "External PHY sysctls");
25786f37f232SEric Joyner 		phy_list = SYSCTL_CHILDREN(phy_node);
25796f37f232SEric Joyner 
2580c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO, "temp",
2581c19c7afeSEric Joyner 		    CTLTYPE_U16 | CTLFLAG_RD, adapter, 0, ixgbe_sysctl_phy_temp,
25828eb6488eSEric Joyner 		    "I", "Current External PHY Temperature (Celsius)");
25836f37f232SEric Joyner 
2584c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO,
2585c19c7afeSEric Joyner 		    "overtemp_occurred", CTLTYPE_U16 | CTLFLAG_RD, adapter, 0,
25866f37f232SEric Joyner 		    ixgbe_sysctl_phy_overtemp_occurred, "I",
25876f37f232SEric Joyner 		    "External PHY High Temperature Event Occurred");
25886f37f232SEric Joyner 	}
25898eb6488eSEric Joyner 
25908eb6488eSEric Joyner 	if (adapter->feat_cap & IXGBE_FEATURE_EEE) {
2591c19c7afeSEric Joyner 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "eee_state",
25928eb6488eSEric Joyner 		    CTLTYPE_INT | CTLFLAG_RW, adapter, 0,
25938eb6488eSEric Joyner 		    ixgbe_sysctl_eee_state, "I", "EEE Power Save State");
25948eb6488eSEric Joyner 	}
25958eb6488eSEric Joyner } /* ixgbe_add_device_sysctls */
25968eb6488eSEric Joyner 
25978eb6488eSEric Joyner /************************************************************************
25988eb6488eSEric Joyner  * ixgbe_allocate_pci_resources
25998eb6488eSEric Joyner  ************************************************************************/
26008eb6488eSEric Joyner static int
2601c19c7afeSEric Joyner ixgbe_allocate_pci_resources(if_ctx_t ctx)
26028eb6488eSEric Joyner {
2603c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
2604c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
26058eb6488eSEric Joyner 	int             rid;
26068eb6488eSEric Joyner 
26078eb6488eSEric Joyner 	rid = PCIR_BAR(0);
26088eb6488eSEric Joyner 	adapter->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
26098eb6488eSEric Joyner 	    RF_ACTIVE);
26108eb6488eSEric Joyner 
26118eb6488eSEric Joyner 	if (!(adapter->pci_mem)) {
26128eb6488eSEric Joyner 		device_printf(dev, "Unable to allocate bus resource: memory\n");
26138eb6488eSEric Joyner 		return (ENXIO);
26148eb6488eSEric Joyner 	}
26158eb6488eSEric Joyner 
26168eb6488eSEric Joyner 	/* Save bus_space values for READ/WRITE_REG macros */
26178eb6488eSEric Joyner 	adapter->osdep.mem_bus_space_tag = rman_get_bustag(adapter->pci_mem);
26188eb6488eSEric Joyner 	adapter->osdep.mem_bus_space_handle =
26198eb6488eSEric Joyner 	    rman_get_bushandle(adapter->pci_mem);
26208eb6488eSEric Joyner 	/* Set hw values for shared code */
26218eb6488eSEric Joyner 	adapter->hw.hw_addr = (u8 *)&adapter->osdep.mem_bus_space_handle;
26228eb6488eSEric Joyner 
26238eb6488eSEric Joyner 	return (0);
26248eb6488eSEric Joyner } /* ixgbe_allocate_pci_resources */
26258eb6488eSEric Joyner 
26268eb6488eSEric Joyner /************************************************************************
26278eb6488eSEric Joyner  * ixgbe_detach - Device removal routine
26288eb6488eSEric Joyner  *
26298eb6488eSEric Joyner  *   Called when the driver is being removed.
26308eb6488eSEric Joyner  *   Stops the adapter and deallocates all the resources
26318eb6488eSEric Joyner  *   that were allocated for driver operation.
26328eb6488eSEric Joyner  *
26338eb6488eSEric Joyner  *   return 0 on success, positive on failure
26348eb6488eSEric Joyner  ************************************************************************/
26358eb6488eSEric Joyner static int
2636c19c7afeSEric Joyner ixgbe_if_detach(if_ctx_t ctx)
26378eb6488eSEric Joyner {
2638c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
2639c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
26408eb6488eSEric Joyner 	u32            ctrl_ext;
26418eb6488eSEric Joyner 
26428eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_detach: begin");
26438eb6488eSEric Joyner 
26448eb6488eSEric Joyner 	if (ixgbe_pci_iov_detach(dev) != 0) {
26458eb6488eSEric Joyner 		device_printf(dev, "SR-IOV in use; detach first.\n");
26468eb6488eSEric Joyner 		return (EBUSY);
26478eb6488eSEric Joyner 	}
26488eb6488eSEric Joyner 
2649c19c7afeSEric Joyner 	iflib_config_gtask_deinit(&adapter->mod_task);
2650c19c7afeSEric Joyner 	iflib_config_gtask_deinit(&adapter->msf_task);
2651c19c7afeSEric Joyner 	iflib_config_gtask_deinit(&adapter->phy_task);
26528eb6488eSEric Joyner 	if (adapter->feat_cap & IXGBE_FEATURE_SRIOV)
2653c19c7afeSEric Joyner 		iflib_config_gtask_deinit(&adapter->mbx_task);
2654c19c7afeSEric Joyner 
2655c19c7afeSEric Joyner 	ixgbe_setup_low_power_mode(ctx);
26568eb6488eSEric Joyner 
26578eb6488eSEric Joyner 	/* let hardware know driver is unloading */
26588eb6488eSEric Joyner 	ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
26598eb6488eSEric Joyner 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
26608eb6488eSEric Joyner 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT, ctrl_ext);
26618eb6488eSEric Joyner 
2662c19c7afeSEric Joyner 	ixgbe_free_pci_resources(ctx);
26638eb6488eSEric Joyner 	free(adapter->mta, M_IXGBE);
26648eb6488eSEric Joyner 
26658eb6488eSEric Joyner 	return (0);
2666c19c7afeSEric Joyner } /* ixgbe_if_detach */
26678eb6488eSEric Joyner 
26688eb6488eSEric Joyner /************************************************************************
26698eb6488eSEric Joyner  * ixgbe_setup_low_power_mode - LPLU/WoL preparation
26708eb6488eSEric Joyner  *
26718eb6488eSEric Joyner  *   Prepare the adapter/port for LPLU and/or WoL
26728eb6488eSEric Joyner  ************************************************************************/
26738eb6488eSEric Joyner static int
2674c19c7afeSEric Joyner ixgbe_setup_low_power_mode(if_ctx_t ctx)
26758eb6488eSEric Joyner {
2676c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
26778eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
2678c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
26798eb6488eSEric Joyner 	s32             error = 0;
26808eb6488eSEric Joyner 
2681c19c7afeSEric Joyner 	if (!hw->wol_enabled)
2682c19c7afeSEric Joyner 		ixgbe_set_phy_power(hw, FALSE);
26838eb6488eSEric Joyner 
26848eb6488eSEric Joyner 	/* Limit power management flow to X550EM baseT */
26858eb6488eSEric Joyner 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T &&
26868eb6488eSEric Joyner 	    hw->phy.ops.enter_lplu) {
26878eb6488eSEric Joyner 		/* Turn off support for APM wakeup. (Using ACPI instead) */
26888eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_GRC,
26898eb6488eSEric Joyner 		    IXGBE_READ_REG(hw, IXGBE_GRC) & ~(u32)2);
26908eb6488eSEric Joyner 
26918eb6488eSEric Joyner 		/*
26928eb6488eSEric Joyner 		 * Clear Wake Up Status register to prevent any previous wakeup
26938eb6488eSEric Joyner 		 * events from waking us up immediately after we suspend.
26948eb6488eSEric Joyner 		 */
26958eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
26968eb6488eSEric Joyner 
26978eb6488eSEric Joyner 		/*
26988eb6488eSEric Joyner 		 * Program the Wakeup Filter Control register with user filter
26998eb6488eSEric Joyner 		 * settings
27008eb6488eSEric Joyner 		 */
27018eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, adapter->wufc);
27028eb6488eSEric Joyner 
27038eb6488eSEric Joyner 		/* Enable wakeups and power management in Wakeup Control */
27048eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_WUC,
27058eb6488eSEric Joyner 		    IXGBE_WUC_WKEN | IXGBE_WUC_PME_EN);
27068eb6488eSEric Joyner 
27078eb6488eSEric Joyner 		/* X550EM baseT adapters need a special LPLU flow */
2708c19c7afeSEric Joyner 		hw->phy.reset_disable = TRUE;
2709c19c7afeSEric Joyner 		ixgbe_if_stop(ctx);
27108eb6488eSEric Joyner 		error = hw->phy.ops.enter_lplu(hw);
27118eb6488eSEric Joyner 		if (error)
27128eb6488eSEric Joyner 			device_printf(dev, "Error entering LPLU: %d\n", error);
2713c19c7afeSEric Joyner 		hw->phy.reset_disable = FALSE;
27148eb6488eSEric Joyner 	} else {
27158eb6488eSEric Joyner 		/* Just stop for other adapters */
2716c19c7afeSEric Joyner 		ixgbe_if_stop(ctx);
27178eb6488eSEric Joyner 	}
27188eb6488eSEric Joyner 
27198eb6488eSEric Joyner 	return error;
27208eb6488eSEric Joyner } /* ixgbe_setup_low_power_mode */
27218eb6488eSEric Joyner 
27228eb6488eSEric Joyner /************************************************************************
27238eb6488eSEric Joyner  * ixgbe_shutdown - Shutdown entry point
27248eb6488eSEric Joyner  ************************************************************************/
27258eb6488eSEric Joyner static int
2726c19c7afeSEric Joyner ixgbe_if_shutdown(if_ctx_t ctx)
27278eb6488eSEric Joyner {
27288eb6488eSEric Joyner 	int error = 0;
27298eb6488eSEric Joyner 
27308eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_shutdown: begin");
27318eb6488eSEric Joyner 
2732c19c7afeSEric Joyner 	error = ixgbe_setup_low_power_mode(ctx);
27338eb6488eSEric Joyner 
27348eb6488eSEric Joyner 	return (error);
2735c19c7afeSEric Joyner } /* ixgbe_if_shutdown */
27368eb6488eSEric Joyner 
27378eb6488eSEric Joyner /************************************************************************
27388eb6488eSEric Joyner  * ixgbe_suspend
27398eb6488eSEric Joyner  *
27408eb6488eSEric Joyner  *   From D0 to D3
27418eb6488eSEric Joyner  ************************************************************************/
27428eb6488eSEric Joyner static int
2743c19c7afeSEric Joyner ixgbe_if_suspend(if_ctx_t ctx)
27448eb6488eSEric Joyner {
27458eb6488eSEric Joyner 	int error = 0;
27468eb6488eSEric Joyner 
27478eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_suspend: begin");
27488eb6488eSEric Joyner 
2749c19c7afeSEric Joyner 	error = ixgbe_setup_low_power_mode(ctx);
27508eb6488eSEric Joyner 
27518eb6488eSEric Joyner 	return (error);
2752c19c7afeSEric Joyner } /* ixgbe_if_suspend */
27538eb6488eSEric Joyner 
27548eb6488eSEric Joyner /************************************************************************
27558eb6488eSEric Joyner  * ixgbe_resume
27568eb6488eSEric Joyner  *
27578eb6488eSEric Joyner  *   From D3 to D0
27588eb6488eSEric Joyner  ************************************************************************/
27598eb6488eSEric Joyner static int
2760c19c7afeSEric Joyner ixgbe_if_resume(if_ctx_t ctx)
27618eb6488eSEric Joyner {
2762c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
2763c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
2764c19c7afeSEric Joyner 	struct ifnet    *ifp = iflib_get_ifp(ctx);
27658eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
27668eb6488eSEric Joyner 	u32             wus;
27678eb6488eSEric Joyner 
27688eb6488eSEric Joyner 	INIT_DEBUGOUT("ixgbe_resume: begin");
27698eb6488eSEric Joyner 
27708eb6488eSEric Joyner 	/* Read & clear WUS register */
27718eb6488eSEric Joyner 	wus = IXGBE_READ_REG(hw, IXGBE_WUS);
27728eb6488eSEric Joyner 	if (wus)
27738eb6488eSEric Joyner 		device_printf(dev, "Woken up by (WUS): %#010x\n",
27748eb6488eSEric Joyner 		    IXGBE_READ_REG(hw, IXGBE_WUS));
27758eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
27768eb6488eSEric Joyner 	/* And clear WUFC until next low-power transition */
27778eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
27788eb6488eSEric Joyner 
27798eb6488eSEric Joyner 	/*
27808eb6488eSEric Joyner 	 * Required after D3->D0 transition;
27818eb6488eSEric Joyner 	 * will re-advertise all previous advertised speeds
27828eb6488eSEric Joyner 	 */
27838eb6488eSEric Joyner 	if (ifp->if_flags & IFF_UP)
2784c19c7afeSEric Joyner 		ixgbe_if_init(ctx);
27858eb6488eSEric Joyner 
27868eb6488eSEric Joyner 	return (0);
2787c19c7afeSEric Joyner } /* ixgbe_if_resume */
27888eb6488eSEric Joyner 
27898eb6488eSEric Joyner /************************************************************************
2790c19c7afeSEric Joyner  * ixgbe_if_mtu_set - Ioctl mtu entry point
27918eb6488eSEric Joyner  *
2792c19c7afeSEric Joyner  *   Return 0 on success, EINVAL on failure
2793c19c7afeSEric Joyner  ************************************************************************/
2794c19c7afeSEric Joyner static int
2795c19c7afeSEric Joyner ixgbe_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
2796c19c7afeSEric Joyner {
2797c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
2798c19c7afeSEric Joyner 	int error = 0;
2799c19c7afeSEric Joyner 
2800c19c7afeSEric Joyner 	IOCTL_DEBUGOUT("ioctl: SIOCIFMTU (Set Interface MTU)");
2801c19c7afeSEric Joyner 
2802c19c7afeSEric Joyner 	if (mtu > IXGBE_MAX_MTU) {
2803c19c7afeSEric Joyner 		error = EINVAL;
2804c19c7afeSEric Joyner 	} else {
2805c19c7afeSEric Joyner 		adapter->max_frame_size = mtu + IXGBE_MTU_HDR;
2806c19c7afeSEric Joyner 	}
2807c19c7afeSEric Joyner 
2808c19c7afeSEric Joyner 	return error;
2809c19c7afeSEric Joyner } /* ixgbe_if_mtu_set */
2810c19c7afeSEric Joyner 
2811c19c7afeSEric Joyner /************************************************************************
2812c19c7afeSEric Joyner  * ixgbe_if_crcstrip_set
28138eb6488eSEric Joyner  ************************************************************************/
28148eb6488eSEric Joyner static void
2815c19c7afeSEric Joyner ixgbe_if_crcstrip_set(if_ctx_t ctx, int onoff, int crcstrip)
28168eb6488eSEric Joyner {
2817c19c7afeSEric Joyner 	struct adapter *sc = iflib_get_softc(ctx);
2818c19c7afeSEric Joyner 	struct ixgbe_hw *hw = &sc->hw;
2819c19c7afeSEric Joyner 	/* crc stripping is set in two places:
2820c19c7afeSEric Joyner 	 * IXGBE_HLREG0 (modified on init_locked and hw reset)
2821c19c7afeSEric Joyner 	 * IXGBE_RDRXCTL (set by the original driver in
2822c19c7afeSEric Joyner 	 *	ixgbe_setup_hw_rsc() called in init_locked.
2823c19c7afeSEric Joyner 	 *	We disable the setting when netmap is compiled in).
2824c19c7afeSEric Joyner 	 * We update the values here, but also in ixgbe.c because
2825c19c7afeSEric Joyner 	 * init_locked sometimes is called outside our control.
2826c19c7afeSEric Joyner 	 */
2827c19c7afeSEric Joyner 	uint32_t hl, rxc;
28288eb6488eSEric Joyner 
2829c19c7afeSEric Joyner 	hl = IXGBE_READ_REG(hw, IXGBE_HLREG0);
2830c19c7afeSEric Joyner 	rxc = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
2831c19c7afeSEric Joyner #ifdef NETMAP
2832c19c7afeSEric Joyner 	if (netmap_verbose)
2833c19c7afeSEric Joyner 		D("%s read  HLREG 0x%x rxc 0x%x",
2834c19c7afeSEric Joyner 			onoff ? "enter" : "exit", hl, rxc);
28358eb6488eSEric Joyner #endif
2836c19c7afeSEric Joyner 	/* hw requirements ... */
2837c19c7afeSEric Joyner 	rxc &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
2838c19c7afeSEric Joyner 	rxc |= IXGBE_RDRXCTL_RSCACKC;
2839c19c7afeSEric Joyner 	if (onoff && !crcstrip) {
2840c19c7afeSEric Joyner 		/* keep the crc. Fast rx */
2841c19c7afeSEric Joyner 		hl &= ~IXGBE_HLREG0_RXCRCSTRP;
2842c19c7afeSEric Joyner 		rxc &= ~IXGBE_RDRXCTL_CRCSTRIP;
2843c19c7afeSEric Joyner 	} else {
2844c19c7afeSEric Joyner 		/* reset default mode */
2845c19c7afeSEric Joyner 		hl |= IXGBE_HLREG0_RXCRCSTRP;
2846c19c7afeSEric Joyner 		rxc |= IXGBE_RDRXCTL_CRCSTRIP;
2847c19c7afeSEric Joyner 	}
2848c19c7afeSEric Joyner #ifdef NETMAP
2849c19c7afeSEric Joyner 	if (netmap_verbose)
2850c19c7afeSEric Joyner 		D("%s write HLREG 0x%x rxc 0x%x",
2851c19c7afeSEric Joyner 			onoff ? "enter" : "exit", hl, rxc);
2852c19c7afeSEric Joyner #endif
2853c19c7afeSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hl);
2854c19c7afeSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rxc);
2855c19c7afeSEric Joyner } /* ixgbe_if_crcstrip_set */
28568eb6488eSEric Joyner 
2857c19c7afeSEric Joyner /*********************************************************************
2858c19c7afeSEric Joyner  * ixgbe_if_init - Init entry point
28598eb6488eSEric Joyner  *
28608eb6488eSEric Joyner  *   Used in two ways: It is used by the stack as an init
28618eb6488eSEric Joyner  *   entry point in network interface structure. It is also
28628eb6488eSEric Joyner  *   used by the driver as a hw/sw initialization routine to
28638eb6488eSEric Joyner  *   get to a consistent state.
28648eb6488eSEric Joyner  *
2865c19c7afeSEric Joyner  *   Return 0 on success, positive on failure
2866c19c7afeSEric Joyner  **********************************************************************/
28678eb6488eSEric Joyner void
2868c19c7afeSEric Joyner ixgbe_if_init(if_ctx_t ctx)
28698eb6488eSEric Joyner {
2870c19c7afeSEric Joyner 	struct adapter     *adapter = iflib_get_softc(ctx);
2871c19c7afeSEric Joyner 	struct ifnet       *ifp = iflib_get_ifp(ctx);
2872c19c7afeSEric Joyner 	device_t           dev = iflib_get_dev(ctx);
28738eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
2874c19c7afeSEric Joyner 	struct ix_rx_queue *rx_que;
2875c19c7afeSEric Joyner 	struct ix_tx_queue *tx_que;
28768eb6488eSEric Joyner 	u32             txdctl, mhadd;
28778eb6488eSEric Joyner 	u32             rxdctl, rxctrl;
28788eb6488eSEric Joyner 	u32             ctrl_ext;
28798eb6488eSEric Joyner 
2880c19c7afeSEric Joyner 	int             i, j, err;
28818eb6488eSEric Joyner 
2882c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_init: begin");
28838eb6488eSEric Joyner 
28848eb6488eSEric Joyner 	/* Queue indices may change with IOV mode */
28858eb6488eSEric Joyner 	ixgbe_align_all_queue_indices(adapter);
28868eb6488eSEric Joyner 
28878eb6488eSEric Joyner 	/* reprogram the RAR[0] in case user changed it. */
28888eb6488eSEric Joyner 	ixgbe_set_rar(hw, 0, hw->mac.addr, adapter->pool, IXGBE_RAH_AV);
28898eb6488eSEric Joyner 
28908eb6488eSEric Joyner 	/* Get the latest mac address, User can use a LAA */
28918eb6488eSEric Joyner 	bcopy(IF_LLADDR(ifp), hw->mac.addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
28928eb6488eSEric Joyner 	ixgbe_set_rar(hw, 0, hw->mac.addr, adapter->pool, 1);
28938eb6488eSEric Joyner 	hw->addr_ctrl.rar_used_count = 1;
28948eb6488eSEric Joyner 
28958eb6488eSEric Joyner 	ixgbe_init_hw(hw);
2896c19c7afeSEric Joyner 
28978eb6488eSEric Joyner 	ixgbe_initialize_iov(adapter);
2898c19c7afeSEric Joyner 
2899c19c7afeSEric Joyner 	ixgbe_initialize_transmit_units(ctx);
29008eb6488eSEric Joyner 
29018eb6488eSEric Joyner 	/* Setup Multicast table */
2902c19c7afeSEric Joyner 	ixgbe_if_multi_set(ctx);
29038eb6488eSEric Joyner 
29048eb6488eSEric Joyner 	/* Determine the correct mbuf pool, based on frame size */
29058eb6488eSEric Joyner 	if (adapter->max_frame_size <= MCLBYTES)
29068eb6488eSEric Joyner 		adapter->rx_mbuf_sz = MCLBYTES;
29078eb6488eSEric Joyner 	else
29088eb6488eSEric Joyner 		adapter->rx_mbuf_sz = MJUMPAGESIZE;
29098eb6488eSEric Joyner 
29108eb6488eSEric Joyner 	/* Configure RX settings */
2911c19c7afeSEric Joyner 	ixgbe_initialize_receive_units(ctx);
29128eb6488eSEric Joyner 
29138eb6488eSEric Joyner 	/* Enable SDP & MSI-X interrupts based on adapter */
29148eb6488eSEric Joyner 	ixgbe_config_gpie(adapter);
29158eb6488eSEric Joyner 
29168eb6488eSEric Joyner 	/* Set MTU size */
29178eb6488eSEric Joyner 	if (ifp->if_mtu > ETHERMTU) {
29188eb6488eSEric Joyner 		/* aka IXGBE_MAXFRS on 82599 and newer */
29198eb6488eSEric Joyner 		mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
29208eb6488eSEric Joyner 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
29218eb6488eSEric Joyner 		mhadd |= adapter->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
29228eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
29238eb6488eSEric Joyner 	}
29248eb6488eSEric Joyner 
29258eb6488eSEric Joyner 	/* Now enable all the queues */
2926c19c7afeSEric Joyner 	for (i = 0, tx_que = adapter->tx_queues; i < adapter->num_tx_queues; i++, tx_que++) {
2927c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
2928c19c7afeSEric Joyner 
29298eb6488eSEric Joyner 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(txr->me));
29308eb6488eSEric Joyner 		txdctl |= IXGBE_TXDCTL_ENABLE;
29318eb6488eSEric Joyner 		/* Set WTHRESH to 8, burst writeback */
29328eb6488eSEric Joyner 		txdctl |= (8 << 16);
29338eb6488eSEric Joyner 		/*
29348eb6488eSEric Joyner 		 * When the internal queue falls below PTHRESH (32),
29358eb6488eSEric Joyner 		 * start prefetching as long as there are at least
29368eb6488eSEric Joyner 		 * HTHRESH (1) buffers ready. The values are taken
29378eb6488eSEric Joyner 		 * from the Intel linux driver 3.8.21.
29388eb6488eSEric Joyner 		 * Prefetching enables tx line rate even with 1 queue.
29398eb6488eSEric Joyner 		 */
29408eb6488eSEric Joyner 		txdctl |= (32 << 0) | (1 << 8);
29418eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(txr->me), txdctl);
29428eb6488eSEric Joyner 	}
29438eb6488eSEric Joyner 
2944c19c7afeSEric Joyner 	for (i = 0, rx_que = adapter->rx_queues; i < adapter->num_rx_queues; i++, rx_que++) {
2945c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
2946c19c7afeSEric Joyner 
29478eb6488eSEric Joyner 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
29488eb6488eSEric Joyner 		if (hw->mac.type == ixgbe_mac_82598EB) {
29498eb6488eSEric Joyner 			/*
29508eb6488eSEric Joyner 			 * PTHRESH = 21
29518eb6488eSEric Joyner 			 * HTHRESH = 4
29528eb6488eSEric Joyner 			 * WTHRESH = 8
29538eb6488eSEric Joyner 			 */
29548eb6488eSEric Joyner 			rxdctl &= ~0x3FFFFF;
29558eb6488eSEric Joyner 			rxdctl |= 0x080420;
29568eb6488eSEric Joyner 		}
29578eb6488eSEric Joyner 		rxdctl |= IXGBE_RXDCTL_ENABLE;
29588eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), rxdctl);
2959c19c7afeSEric Joyner 		for (j = 0; j < 10; j++) {
29608eb6488eSEric Joyner 			if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me)) &
29618eb6488eSEric Joyner 			    IXGBE_RXDCTL_ENABLE)
29628eb6488eSEric Joyner 				break;
29638eb6488eSEric Joyner 			else
29648eb6488eSEric Joyner 				msec_delay(1);
29658eb6488eSEric Joyner 		}
29668eb6488eSEric Joyner 		wmb();
29678eb6488eSEric Joyner 	}
29688eb6488eSEric Joyner 
29698eb6488eSEric Joyner 	/* Enable Receive engine */
29708eb6488eSEric Joyner 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
29718eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
29728eb6488eSEric Joyner 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
29738eb6488eSEric Joyner 	rxctrl |= IXGBE_RXCTRL_RXEN;
29748eb6488eSEric Joyner 	ixgbe_enable_rx_dma(hw, rxctrl);
29758eb6488eSEric Joyner 
2976c19c7afeSEric Joyner 	/* Set up MSI/MSI-X routing */
2977c19c7afeSEric Joyner 	if (ixgbe_enable_msix)  {
29788eb6488eSEric Joyner 		ixgbe_configure_ivars(adapter);
29798eb6488eSEric Joyner 		/* Set up auto-mask */
29808eb6488eSEric Joyner 		if (hw->mac.type == ixgbe_mac_82598EB)
29818eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
29828eb6488eSEric Joyner 		else {
29838eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
29848eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
29858eb6488eSEric Joyner 		}
29868eb6488eSEric Joyner 	} else {  /* Simple settings for Legacy/MSI */
29878eb6488eSEric Joyner 		ixgbe_set_ivar(adapter, 0, 0, 0);
29888eb6488eSEric Joyner 		ixgbe_set_ivar(adapter, 0, 0, 1);
29898eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
29908eb6488eSEric Joyner 	}
29918eb6488eSEric Joyner 
29928eb6488eSEric Joyner 	ixgbe_init_fdir(adapter);
29938eb6488eSEric Joyner 
29948eb6488eSEric Joyner 	/*
29958eb6488eSEric Joyner 	 * Check on any SFP devices that
29968eb6488eSEric Joyner 	 * need to be kick-started
29978eb6488eSEric Joyner 	 */
29988eb6488eSEric Joyner 	if (hw->phy.type == ixgbe_phy_none) {
29998eb6488eSEric Joyner 		err = hw->phy.ops.identify(hw);
30008eb6488eSEric Joyner 		if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
30018eb6488eSEric Joyner 			device_printf(dev,
30028eb6488eSEric Joyner 			    "Unsupported SFP+ module type was detected.\n");
30038eb6488eSEric Joyner 			return;
30048eb6488eSEric Joyner 		}
30058eb6488eSEric Joyner 	}
30068eb6488eSEric Joyner 
30078eb6488eSEric Joyner 	/* Set moderation on the Link interrupt */
30088eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EITR(adapter->vector), IXGBE_LINK_ITR);
30098eb6488eSEric Joyner 
3010c19c7afeSEric Joyner 	/* Enable power to the phy. */
3011c19c7afeSEric Joyner 	ixgbe_set_phy_power(hw, TRUE);
3012c19c7afeSEric Joyner 
30138eb6488eSEric Joyner 	/* Config/Enable Link */
30148eb6488eSEric Joyner 	ixgbe_config_link(adapter);
30158eb6488eSEric Joyner 
30168eb6488eSEric Joyner 	/* Hardware Packet Buffer & Flow Control setup */
30178eb6488eSEric Joyner 	ixgbe_config_delay_values(adapter);
30188eb6488eSEric Joyner 
30198eb6488eSEric Joyner 	/* Initialize the FC settings */
30208eb6488eSEric Joyner 	ixgbe_start_hw(hw);
30218eb6488eSEric Joyner 
30228eb6488eSEric Joyner 	/* Set up VLAN support and filter */
3023c19c7afeSEric Joyner 	ixgbe_setup_vlan_hw_support(ctx);
30248eb6488eSEric Joyner 
30258eb6488eSEric Joyner 	/* Setup DMA Coalescing */
30268eb6488eSEric Joyner 	ixgbe_config_dmac(adapter);
30278eb6488eSEric Joyner 
30288eb6488eSEric Joyner 	/* And now turn on interrupts */
3029c19c7afeSEric Joyner 	ixgbe_if_enable_intr(ctx);
30308eb6488eSEric Joyner 
30318eb6488eSEric Joyner 	/* Enable the use of the MBX by the VF's */
30328eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_SRIOV) {
30338eb6488eSEric Joyner 		ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
30348eb6488eSEric Joyner 		ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
30358eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
30368eb6488eSEric Joyner 	}
30378eb6488eSEric Joyner 
30388eb6488eSEric Joyner } /* ixgbe_init_locked */
30398eb6488eSEric Joyner 
30408eb6488eSEric Joyner /************************************************************************
30418eb6488eSEric Joyner  * ixgbe_set_ivar
30428eb6488eSEric Joyner  *
30438eb6488eSEric Joyner  *   Setup the correct IVAR register for a particular MSI-X interrupt
30448eb6488eSEric Joyner  *     (yes this is all very magic and confusing :)
30458eb6488eSEric Joyner  *    - entry is the register array entry
30468eb6488eSEric Joyner  *    - vector is the MSI-X vector for this queue
30478eb6488eSEric Joyner  *    - type is RX/TX/MISC
30488eb6488eSEric Joyner  ************************************************************************/
30498eb6488eSEric Joyner static void
30508eb6488eSEric Joyner ixgbe_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
30518eb6488eSEric Joyner {
30528eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
30538eb6488eSEric Joyner 	u32 ivar, index;
30548eb6488eSEric Joyner 
30558eb6488eSEric Joyner 	vector |= IXGBE_IVAR_ALLOC_VAL;
30568eb6488eSEric Joyner 
30578eb6488eSEric Joyner 	switch (hw->mac.type) {
30588eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
30598eb6488eSEric Joyner 		if (type == -1)
30608eb6488eSEric Joyner 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
30618eb6488eSEric Joyner 		else
30628eb6488eSEric Joyner 			entry += (type * 64);
30638eb6488eSEric Joyner 		index = (entry >> 2) & 0x1F;
30648eb6488eSEric Joyner 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
30658eb6488eSEric Joyner 		ivar &= ~(0xFF << (8 * (entry & 0x3)));
30668eb6488eSEric Joyner 		ivar |= (vector << (8 * (entry & 0x3)));
30678eb6488eSEric Joyner 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
30688eb6488eSEric Joyner 		break;
30698eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
30708eb6488eSEric Joyner 	case ixgbe_mac_X540:
30718eb6488eSEric Joyner 	case ixgbe_mac_X550:
30728eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
30738eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
30748eb6488eSEric Joyner 		if (type == -1) { /* MISC IVAR */
30758eb6488eSEric Joyner 			index = (entry & 1) * 8;
30768eb6488eSEric Joyner 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
30778eb6488eSEric Joyner 			ivar &= ~(0xFF << index);
30788eb6488eSEric Joyner 			ivar |= (vector << index);
30798eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
30808eb6488eSEric Joyner 		} else {          /* RX/TX IVARS */
30818eb6488eSEric Joyner 			index = (16 * (entry & 1)) + (8 * type);
30828eb6488eSEric Joyner 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
30838eb6488eSEric Joyner 			ivar &= ~(0xFF << index);
30848eb6488eSEric Joyner 			ivar |= (vector << index);
30858eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
30868eb6488eSEric Joyner 		}
30878eb6488eSEric Joyner 	default:
30888eb6488eSEric Joyner 		break;
30898eb6488eSEric Joyner 	}
30908eb6488eSEric Joyner } /* ixgbe_set_ivar */
30918eb6488eSEric Joyner 
30928eb6488eSEric Joyner /************************************************************************
30938eb6488eSEric Joyner  * ixgbe_configure_ivars
30948eb6488eSEric Joyner  ************************************************************************/
30958eb6488eSEric Joyner static void
30968eb6488eSEric Joyner ixgbe_configure_ivars(struct adapter *adapter)
30978eb6488eSEric Joyner {
3098c19c7afeSEric Joyner 	struct ix_rx_queue *rx_que = adapter->rx_queues;
3099c19c7afeSEric Joyner 	struct ix_tx_queue *tx_que = adapter->tx_queues;
31008eb6488eSEric Joyner 	u32                newitr;
31018eb6488eSEric Joyner 
31028eb6488eSEric Joyner 	if (ixgbe_max_interrupt_rate > 0)
31038eb6488eSEric Joyner 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
31048eb6488eSEric Joyner 	else {
31058eb6488eSEric Joyner 		/*
31068eb6488eSEric Joyner 		 * Disable DMA coalescing if interrupt moderation is
31078eb6488eSEric Joyner 		 * disabled.
31088eb6488eSEric Joyner 		 */
31098eb6488eSEric Joyner 		adapter->dmac = 0;
31108eb6488eSEric Joyner 		newitr = 0;
31118eb6488eSEric Joyner 	}
31128eb6488eSEric Joyner 
3113c19c7afeSEric Joyner 	for (int i = 0; i < adapter->num_rx_queues; i++, rx_que++) {
3114c19c7afeSEric Joyner 		struct rx_ring *rxr = &rx_que->rxr;
31158eb6488eSEric Joyner 
3116c19c7afeSEric Joyner 		/* First the RX queue entry */
3117c19c7afeSEric Joyner 		ixgbe_set_ivar(adapter, rxr->me, rx_que->msix, 0);
3118c19c7afeSEric Joyner 
3119c19c7afeSEric Joyner 		/* Set an Initial EITR value */
3120c19c7afeSEric Joyner 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(rx_que->msix), newitr);
3121c19c7afeSEric Joyner 	}
3122c19c7afeSEric Joyner 	for (int i = 0; i < adapter->num_tx_queues; i++, tx_que++) {
3123c19c7afeSEric Joyner 		struct tx_ring *txr = &tx_que->txr;
3124c19c7afeSEric Joyner 
3125c19c7afeSEric Joyner 		/* ... and the TX */
3126c19c7afeSEric Joyner 		ixgbe_set_ivar(adapter, txr->me, tx_que->msix, 1);
3127c19c7afeSEric Joyner 	}
31288eb6488eSEric Joyner 	/* For the Link interrupt */
31298eb6488eSEric Joyner 	ixgbe_set_ivar(adapter, 1, adapter->vector, -1);
31308eb6488eSEric Joyner } /* ixgbe_configure_ivars */
31318eb6488eSEric Joyner 
31328eb6488eSEric Joyner /************************************************************************
31338eb6488eSEric Joyner  * ixgbe_config_gpie
31348eb6488eSEric Joyner  ************************************************************************/
31358eb6488eSEric Joyner static void
31368eb6488eSEric Joyner ixgbe_config_gpie(struct adapter *adapter)
31378eb6488eSEric Joyner {
31388eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
31398eb6488eSEric Joyner 	u32             gpie;
31408eb6488eSEric Joyner 
31418eb6488eSEric Joyner 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
31428eb6488eSEric Joyner 
3143c19c7afeSEric Joyner 	if (adapter->intr_type == IFLIB_INTR_MSIX) {
31448eb6488eSEric Joyner 		/* Enable Enhanced MSI-X mode */
31458eb6488eSEric Joyner 		gpie |= IXGBE_GPIE_MSIX_MODE
31468eb6488eSEric Joyner 		     |  IXGBE_GPIE_EIAME
31478eb6488eSEric Joyner 		     |  IXGBE_GPIE_PBA_SUPPORT
31488eb6488eSEric Joyner 		     |  IXGBE_GPIE_OCD;
31498eb6488eSEric Joyner 	}
31508eb6488eSEric Joyner 
31518eb6488eSEric Joyner 	/* Fan Failure Interrupt */
31528eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL)
31538eb6488eSEric Joyner 		gpie |= IXGBE_SDP1_GPIEN;
31548eb6488eSEric Joyner 
31558eb6488eSEric Joyner 	/* Thermal Sensor Interrupt */
31568eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_TEMP_SENSOR)
31578eb6488eSEric Joyner 		gpie |= IXGBE_SDP0_GPIEN_X540;
31588eb6488eSEric Joyner 
31598eb6488eSEric Joyner 	/* Link detection */
31608eb6488eSEric Joyner 	switch (hw->mac.type) {
31618eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
31628eb6488eSEric Joyner 		gpie |= IXGBE_SDP1_GPIEN | IXGBE_SDP2_GPIEN;
31638eb6488eSEric Joyner 		break;
31648eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
31658eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
31668eb6488eSEric Joyner 		gpie |= IXGBE_SDP0_GPIEN_X540;
31678eb6488eSEric Joyner 		break;
31688eb6488eSEric Joyner 	default:
31698eb6488eSEric Joyner 		break;
31708eb6488eSEric Joyner 	}
31718eb6488eSEric Joyner 
31728eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
31738eb6488eSEric Joyner 
31748eb6488eSEric Joyner } /* ixgbe_config_gpie */
31758eb6488eSEric Joyner 
31768eb6488eSEric Joyner /************************************************************************
31778eb6488eSEric Joyner  * ixgbe_config_delay_values
31788eb6488eSEric Joyner  *
31798eb6488eSEric Joyner  *   Requires adapter->max_frame_size to be set.
31808eb6488eSEric Joyner  ************************************************************************/
31818eb6488eSEric Joyner static void
31828eb6488eSEric Joyner ixgbe_config_delay_values(struct adapter *adapter)
31838eb6488eSEric Joyner {
31848eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
31858eb6488eSEric Joyner 	u32             rxpb, frame, size, tmp;
31868eb6488eSEric Joyner 
31878eb6488eSEric Joyner 	frame = adapter->max_frame_size;
31888eb6488eSEric Joyner 
31898eb6488eSEric Joyner 	/* Calculate High Water */
31908eb6488eSEric Joyner 	switch (hw->mac.type) {
31918eb6488eSEric Joyner 	case ixgbe_mac_X540:
31928eb6488eSEric Joyner 	case ixgbe_mac_X550:
31938eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
31948eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
31958eb6488eSEric Joyner 		tmp = IXGBE_DV_X540(frame, frame);
31968eb6488eSEric Joyner 		break;
31978eb6488eSEric Joyner 	default:
31988eb6488eSEric Joyner 		tmp = IXGBE_DV(frame, frame);
31998eb6488eSEric Joyner 		break;
32008eb6488eSEric Joyner 	}
32018eb6488eSEric Joyner 	size = IXGBE_BT2KB(tmp);
32028eb6488eSEric Joyner 	rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
32038eb6488eSEric Joyner 	hw->fc.high_water[0] = rxpb - size;
32048eb6488eSEric Joyner 
32058eb6488eSEric Joyner 	/* Now calculate Low Water */
32068eb6488eSEric Joyner 	switch (hw->mac.type) {
32078eb6488eSEric Joyner 	case ixgbe_mac_X540:
32088eb6488eSEric Joyner 	case ixgbe_mac_X550:
32098eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
32108eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
32118eb6488eSEric Joyner 		tmp = IXGBE_LOW_DV_X540(frame);
32128eb6488eSEric Joyner 		break;
32138eb6488eSEric Joyner 	default:
32148eb6488eSEric Joyner 		tmp = IXGBE_LOW_DV(frame);
32158eb6488eSEric Joyner 		break;
32168eb6488eSEric Joyner 	}
32178eb6488eSEric Joyner 	hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
32188eb6488eSEric Joyner 
32198eb6488eSEric Joyner 	hw->fc.pause_time = IXGBE_FC_PAUSE;
32208eb6488eSEric Joyner 	hw->fc.send_xon = TRUE;
32218eb6488eSEric Joyner } /* ixgbe_config_delay_values */
32228eb6488eSEric Joyner 
32238eb6488eSEric Joyner /************************************************************************
32248eb6488eSEric Joyner  * ixgbe_set_multi - Multicast Update
32258eb6488eSEric Joyner  *
32268eb6488eSEric Joyner  *   Called whenever multicast address list is updated.
32278eb6488eSEric Joyner  ************************************************************************/
3228c19c7afeSEric Joyner static int
3229c19c7afeSEric Joyner ixgbe_mc_filter_apply(void *arg, struct ifmultiaddr *ifma, int count)
32308eb6488eSEric Joyner {
3231c19c7afeSEric Joyner 	struct adapter *adapter = arg;
3232c19c7afeSEric Joyner 	struct ixgbe_mc_addr *mta = adapter->mta;
3233c19c7afeSEric Joyner 
3234c19c7afeSEric Joyner 	if (ifma->ifma_addr->sa_family != AF_LINK)
3235c19c7afeSEric Joyner 		return (0);
3236c19c7afeSEric Joyner 	if (count == MAX_NUM_MULTICAST_ADDRESSES)
3237c19c7afeSEric Joyner 		return (0);
3238c19c7afeSEric Joyner 	bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3239c19c7afeSEric Joyner 	    mta[count].addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
3240c19c7afeSEric Joyner 	mta[count].vmdq = adapter->pool;
3241c19c7afeSEric Joyner 
3242c19c7afeSEric Joyner 	return (1);
3243c19c7afeSEric Joyner } /* ixgbe_mc_filter_apply */
3244c19c7afeSEric Joyner 
3245c19c7afeSEric Joyner static void
3246c19c7afeSEric Joyner ixgbe_if_multi_set(if_ctx_t ctx)
3247c19c7afeSEric Joyner {
3248c19c7afeSEric Joyner 	struct adapter       *adapter = iflib_get_softc(ctx);
32498eb6488eSEric Joyner 	struct ixgbe_mc_addr *mta;
3250c19c7afeSEric Joyner 	struct ifnet         *ifp = iflib_get_ifp(ctx);
32518eb6488eSEric Joyner 	u8                   *update_ptr;
32528eb6488eSEric Joyner 	int                  mcnt = 0;
32538eb6488eSEric Joyner 	u32                  fctrl;
32548eb6488eSEric Joyner 
3255c19c7afeSEric Joyner 	IOCTL_DEBUGOUT("ixgbe_if_multi_set: begin");
32568eb6488eSEric Joyner 
32578eb6488eSEric Joyner 	mta = adapter->mta;
32588eb6488eSEric Joyner 	bzero(mta, sizeof(*mta) * MAX_NUM_MULTICAST_ADDRESSES);
32598eb6488eSEric Joyner 
3260c19c7afeSEric Joyner 	mcnt = if_multi_apply(iflib_get_ifp(ctx), ixgbe_mc_filter_apply, adapter);
32618eb6488eSEric Joyner 
32628eb6488eSEric Joyner 	fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
32638eb6488eSEric Joyner 	fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
32648eb6488eSEric Joyner 	if (ifp->if_flags & IFF_PROMISC)
32658eb6488eSEric Joyner 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
32668eb6488eSEric Joyner 	else if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES ||
32678eb6488eSEric Joyner 	    ifp->if_flags & IFF_ALLMULTI) {
32688eb6488eSEric Joyner 		fctrl |= IXGBE_FCTRL_MPE;
32698eb6488eSEric Joyner 		fctrl &= ~IXGBE_FCTRL_UPE;
32708eb6488eSEric Joyner 	} else
32718eb6488eSEric Joyner 		fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
32728eb6488eSEric Joyner 
32738eb6488eSEric Joyner 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
32748eb6488eSEric Joyner 
32758eb6488eSEric Joyner 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES) {
32768eb6488eSEric Joyner 		update_ptr = (u8 *)mta;
32778eb6488eSEric Joyner 		ixgbe_update_mc_addr_list(&adapter->hw, update_ptr, mcnt,
32788eb6488eSEric Joyner 		    ixgbe_mc_array_itr, TRUE);
32798eb6488eSEric Joyner 	}
32808eb6488eSEric Joyner 
3281c19c7afeSEric Joyner } /* ixgbe_if_multi_set */
32828eb6488eSEric Joyner 
32838eb6488eSEric Joyner /************************************************************************
32848eb6488eSEric Joyner  * ixgbe_mc_array_itr
32858eb6488eSEric Joyner  *
32868eb6488eSEric Joyner  *   An iterator function needed by the multicast shared code.
32878eb6488eSEric Joyner  *   It feeds the shared code routine the addresses in the
32888eb6488eSEric Joyner  *   array of ixgbe_set_multi() one by one.
32898eb6488eSEric Joyner  ************************************************************************/
32908eb6488eSEric Joyner static u8 *
32918eb6488eSEric Joyner ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
32928eb6488eSEric Joyner {
32938eb6488eSEric Joyner 	struct ixgbe_mc_addr *mta;
32948eb6488eSEric Joyner 
32958eb6488eSEric Joyner 	mta = (struct ixgbe_mc_addr *)*update_ptr;
32968eb6488eSEric Joyner 	*vmdq = mta->vmdq;
32978eb6488eSEric Joyner 
32988eb6488eSEric Joyner 	*update_ptr = (u8*)(mta + 1);
32998eb6488eSEric Joyner 
33008eb6488eSEric Joyner 	return (mta->addr);
33018eb6488eSEric Joyner } /* ixgbe_mc_array_itr */
33028eb6488eSEric Joyner 
33038eb6488eSEric Joyner /************************************************************************
33048eb6488eSEric Joyner  * ixgbe_local_timer - Timer routine
33058eb6488eSEric Joyner  *
33068eb6488eSEric Joyner  *   Checks for link status, updates statistics,
33078eb6488eSEric Joyner  *   and runs the watchdog check.
33088eb6488eSEric Joyner  ************************************************************************/
33098eb6488eSEric Joyner static void
3310c19c7afeSEric Joyner ixgbe_if_timer(if_ctx_t ctx, uint16_t qid)
33118eb6488eSEric Joyner {
3312c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
33138eb6488eSEric Joyner 
3314c19c7afeSEric Joyner 	if (qid != 0)
3315c19c7afeSEric Joyner 		return;
33168eb6488eSEric Joyner 
33178eb6488eSEric Joyner 	/* Check for pluggable optics */
33188eb6488eSEric Joyner 	if (adapter->sfp_probe)
3319c19c7afeSEric Joyner 		if (!ixgbe_sfp_probe(ctx))
3320c19c7afeSEric Joyner 			return; /* Nothing to do */
33218eb6488eSEric Joyner 
3322c19c7afeSEric Joyner 	ixgbe_check_link(&adapter->hw, &adapter->link_speed,
3323c19c7afeSEric Joyner 	    &adapter->link_up, 0);
33248eb6488eSEric Joyner 
3325c19c7afeSEric Joyner 	/* Fire off the adminq task */
3326c19c7afeSEric Joyner 	iflib_admin_intr_deferred(ctx);
33278eb6488eSEric Joyner 
3328c19c7afeSEric Joyner } /* ixgbe_if_timer */
33298eb6488eSEric Joyner 
33308eb6488eSEric Joyner /************************************************************************
33318eb6488eSEric Joyner  * ixgbe_sfp_probe
33328eb6488eSEric Joyner  *
33338eb6488eSEric Joyner  *   Determine if a port had optics inserted.
33348eb6488eSEric Joyner  ************************************************************************/
33358eb6488eSEric Joyner static bool
3336c19c7afeSEric Joyner ixgbe_sfp_probe(if_ctx_t ctx)
33378eb6488eSEric Joyner {
3338c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
33398eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
3340c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
33418eb6488eSEric Joyner 	bool            result = FALSE;
33428eb6488eSEric Joyner 
33438eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_nl) &&
33448eb6488eSEric Joyner 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
33458eb6488eSEric Joyner 		s32 ret = hw->phy.ops.identify_sfp(hw);
33468eb6488eSEric Joyner 		if (ret)
33478eb6488eSEric Joyner 			goto out;
33488eb6488eSEric Joyner 		ret = hw->phy.ops.reset(hw);
33498eb6488eSEric Joyner 		adapter->sfp_probe = FALSE;
33508eb6488eSEric Joyner 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
33518eb6488eSEric Joyner 			device_printf(dev, "Unsupported SFP+ module detected!");
33528eb6488eSEric Joyner 			device_printf(dev,
33538eb6488eSEric Joyner 			    "Reload driver with supported module.\n");
33548eb6488eSEric Joyner 			goto out;
33558eb6488eSEric Joyner 		} else
33568eb6488eSEric Joyner 			device_printf(dev, "SFP+ module detected!\n");
33578eb6488eSEric Joyner 		/* We now have supported optics */
33588eb6488eSEric Joyner 		result = TRUE;
33598eb6488eSEric Joyner 	}
33608eb6488eSEric Joyner out:
33618eb6488eSEric Joyner 
33628eb6488eSEric Joyner 	return (result);
33638eb6488eSEric Joyner } /* ixgbe_sfp_probe */
33648eb6488eSEric Joyner 
33658eb6488eSEric Joyner /************************************************************************
33668eb6488eSEric Joyner  * ixgbe_handle_mod - Tasklet for SFP module interrupts
33678eb6488eSEric Joyner  ************************************************************************/
33688eb6488eSEric Joyner static void
3369c19c7afeSEric Joyner ixgbe_handle_mod(void *context)
33708eb6488eSEric Joyner {
3371c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3372c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
33738eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
3374c19c7afeSEric Joyner 	device_t        dev = iflib_get_dev(ctx);
33758eb6488eSEric Joyner 	u32             err, cage_full = 0;
33768eb6488eSEric Joyner 
3377c19c7afeSEric Joyner 	adapter->sfp_reinit = 1;
33788eb6488eSEric Joyner 	if (adapter->hw.need_crosstalk_fix) {
33798eb6488eSEric Joyner 		switch (hw->mac.type) {
33808eb6488eSEric Joyner 		case ixgbe_mac_82599EB:
33818eb6488eSEric Joyner 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
33828eb6488eSEric Joyner 			    IXGBE_ESDP_SDP2;
33838eb6488eSEric Joyner 			break;
33848eb6488eSEric Joyner 		case ixgbe_mac_X550EM_x:
33858eb6488eSEric Joyner 		case ixgbe_mac_X550EM_a:
33868eb6488eSEric Joyner 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
33878eb6488eSEric Joyner 			    IXGBE_ESDP_SDP0;
33888eb6488eSEric Joyner 			break;
33898eb6488eSEric Joyner 		default:
33908eb6488eSEric Joyner 			break;
33918eb6488eSEric Joyner 		}
33928eb6488eSEric Joyner 
33938eb6488eSEric Joyner 		if (!cage_full)
3394c19c7afeSEric Joyner 			goto handle_mod_out;
33958eb6488eSEric Joyner 	}
33968eb6488eSEric Joyner 
33978eb6488eSEric Joyner 	err = hw->phy.ops.identify_sfp(hw);
33988eb6488eSEric Joyner 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
33998eb6488eSEric Joyner 		device_printf(dev,
34008eb6488eSEric Joyner 		    "Unsupported SFP+ module type was detected.\n");
3401c19c7afeSEric Joyner 		goto handle_mod_out;
34028eb6488eSEric Joyner 	}
34038eb6488eSEric Joyner 
3404c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB)
3405c19c7afeSEric Joyner 		err = hw->phy.ops.reset(hw);
3406c19c7afeSEric Joyner 	else
34078eb6488eSEric Joyner 		err = hw->mac.ops.setup_sfp(hw);
3408c19c7afeSEric Joyner 
34098eb6488eSEric Joyner 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
34108eb6488eSEric Joyner 		device_printf(dev,
34118eb6488eSEric Joyner 		    "Setup failure - unsupported SFP+ module type.\n");
3412c19c7afeSEric Joyner 		goto handle_mod_out;
34138eb6488eSEric Joyner 	}
3414c19c7afeSEric Joyner 	GROUPTASK_ENQUEUE(&adapter->msf_task);
3415c19c7afeSEric Joyner 	return;
3416c19c7afeSEric Joyner 
3417c19c7afeSEric Joyner handle_mod_out:
3418c19c7afeSEric Joyner 	adapter->sfp_reinit = 0;
34198eb6488eSEric Joyner } /* ixgbe_handle_mod */
34208eb6488eSEric Joyner 
34218eb6488eSEric Joyner 
34228eb6488eSEric Joyner /************************************************************************
34238eb6488eSEric Joyner  * ixgbe_handle_msf - Tasklet for MSF (multispeed fiber) interrupts
34248eb6488eSEric Joyner  ************************************************************************/
34258eb6488eSEric Joyner static void
3426c19c7afeSEric Joyner ixgbe_handle_msf(void *context)
34278eb6488eSEric Joyner {
3428c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3429c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
34308eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
34318eb6488eSEric Joyner 	u32             autoneg;
34328eb6488eSEric Joyner 	bool            negotiate;
34338eb6488eSEric Joyner 
3434c19c7afeSEric Joyner 	if (adapter->sfp_reinit != 1)
3435c19c7afeSEric Joyner 		return;
3436c19c7afeSEric Joyner 
34378eb6488eSEric Joyner 	/* get_supported_phy_layer will call hw->phy.ops.identify_sfp() */
34388eb6488eSEric Joyner 	adapter->phy_layer = ixgbe_get_supported_physical_layer(hw);
34398eb6488eSEric Joyner 
34408eb6488eSEric Joyner 	autoneg = hw->phy.autoneg_advertised;
34418eb6488eSEric Joyner 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
34428eb6488eSEric Joyner 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
34438eb6488eSEric Joyner 	if (hw->mac.ops.setup_link)
34448eb6488eSEric Joyner 		hw->mac.ops.setup_link(hw, autoneg, TRUE);
34458eb6488eSEric Joyner 
34468eb6488eSEric Joyner 	/* Adjust media types shown in ifconfig */
3447c19c7afeSEric Joyner 	ifmedia_removeall(adapter->media);
3448c19c7afeSEric Joyner 	ixgbe_add_media_types(adapter->ctx);
3449c19c7afeSEric Joyner 	ifmedia_set(adapter->media, IFM_ETHER | IFM_AUTO);
3450c19c7afeSEric Joyner 
3451c19c7afeSEric Joyner 	adapter->sfp_reinit = 0;
34528eb6488eSEric Joyner } /* ixgbe_handle_msf */
34538eb6488eSEric Joyner 
34548eb6488eSEric Joyner /************************************************************************
34558eb6488eSEric Joyner  * ixgbe_handle_phy - Tasklet for external PHY interrupts
34568eb6488eSEric Joyner  ************************************************************************/
34578eb6488eSEric Joyner static void
3458c19c7afeSEric Joyner ixgbe_handle_phy(void *context)
34598eb6488eSEric Joyner {
3460c19c7afeSEric Joyner 	if_ctx_t        ctx = context;
3461c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
34628eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
34638eb6488eSEric Joyner 	int             error;
34648eb6488eSEric Joyner 
34658eb6488eSEric Joyner 	error = hw->phy.ops.handle_lasi(hw);
34668eb6488eSEric Joyner 	if (error == IXGBE_ERR_OVERTEMP)
34678eb6488eSEric Joyner 		device_printf(adapter->dev, "CRITICAL: EXTERNAL PHY OVER TEMP!!  PHY will downshift to lower power state!\n");
34688eb6488eSEric Joyner 	else if (error)
34698eb6488eSEric Joyner 		device_printf(adapter->dev,
34708eb6488eSEric Joyner 		    "Error handling LASI interrupt: %d\n", error);
34718eb6488eSEric Joyner } /* ixgbe_handle_phy */
34728eb6488eSEric Joyner 
34738eb6488eSEric Joyner /************************************************************************
3474c19c7afeSEric Joyner  * ixgbe_if_stop - Stop the hardware
34758eb6488eSEric Joyner  *
34768eb6488eSEric Joyner  *   Disables all traffic on the adapter by issuing a
34778eb6488eSEric Joyner  *   global reset on the MAC and deallocates TX/RX buffers.
34788eb6488eSEric Joyner  ************************************************************************/
34798eb6488eSEric Joyner static void
3480c19c7afeSEric Joyner ixgbe_if_stop(if_ctx_t ctx)
34818eb6488eSEric Joyner {
3482c19c7afeSEric Joyner 	struct adapter  *adapter = iflib_get_softc(ctx);
34838eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
34848eb6488eSEric Joyner 
3485c19c7afeSEric Joyner 	INIT_DEBUGOUT("ixgbe_if_stop: begin\n");
34868eb6488eSEric Joyner 
34878eb6488eSEric Joyner 	ixgbe_reset_hw(hw);
34888eb6488eSEric Joyner 	hw->adapter_stopped = FALSE;
34898eb6488eSEric Joyner 	ixgbe_stop_adapter(hw);
34908eb6488eSEric Joyner 	if (hw->mac.type == ixgbe_mac_82599EB)
34918eb6488eSEric Joyner 		ixgbe_stop_mac_link_on_d3_82599(hw);
34928eb6488eSEric Joyner 	/* Turn off the laser - noop with no optics */
34938eb6488eSEric Joyner 	ixgbe_disable_tx_laser(hw);
34948eb6488eSEric Joyner 
34958eb6488eSEric Joyner 	/* Update the stack */
34968eb6488eSEric Joyner 	adapter->link_up = FALSE;
3497c19c7afeSEric Joyner 	ixgbe_if_update_admin_status(ctx);
34988eb6488eSEric Joyner 
34998eb6488eSEric Joyner 	/* reprogram the RAR[0] in case user changed it. */
35008eb6488eSEric Joyner 	ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
35018eb6488eSEric Joyner 
35028eb6488eSEric Joyner 	return;
3503c19c7afeSEric Joyner } /* ixgbe_if_stop */
35048eb6488eSEric Joyner 
35058eb6488eSEric Joyner /************************************************************************
35068eb6488eSEric Joyner  * ixgbe_update_link_status - Update OS on link state
35078eb6488eSEric Joyner  *
35088eb6488eSEric Joyner  * Note: Only updates the OS on the cached link state.
35098eb6488eSEric Joyner  *       The real check of the hardware only happens with
35108eb6488eSEric Joyner  *       a link interrupt.
35118eb6488eSEric Joyner  ************************************************************************/
35128eb6488eSEric Joyner static void
3513c19c7afeSEric Joyner ixgbe_if_update_admin_status(if_ctx_t ctx)
35148eb6488eSEric Joyner {
3515c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
3516c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
35178eb6488eSEric Joyner 
35188eb6488eSEric Joyner 	if (adapter->link_up) {
35198eb6488eSEric Joyner 		if (adapter->link_active == FALSE) {
35208eb6488eSEric Joyner 			if (bootverbose)
35218eb6488eSEric Joyner 				device_printf(dev, "Link is up %d Gbps %s \n",
35228eb6488eSEric Joyner 				    ((adapter->link_speed == 128) ? 10 : 1),
35238eb6488eSEric Joyner 				    "Full Duplex");
35248eb6488eSEric Joyner 			adapter->link_active = TRUE;
35258eb6488eSEric Joyner 			/* Update any Flow Control changes */
35268eb6488eSEric Joyner 			ixgbe_fc_enable(&adapter->hw);
35278eb6488eSEric Joyner 			/* Update DMA coalescing config */
35288eb6488eSEric Joyner 			ixgbe_config_dmac(adapter);
3529c19c7afeSEric Joyner 			/* should actually be negotiated value */
3530c19c7afeSEric Joyner 			iflib_link_state_change(ctx, LINK_STATE_UP, IF_Gbps(10));
3531c19c7afeSEric Joyner 
35328eb6488eSEric Joyner 			if (adapter->feat_en & IXGBE_FEATURE_SRIOV)
35338eb6488eSEric Joyner 				ixgbe_ping_all_vfs(adapter);
35348eb6488eSEric Joyner 		}
35358eb6488eSEric Joyner 	} else { /* Link down */
35368eb6488eSEric Joyner 		if (adapter->link_active == TRUE) {
35378eb6488eSEric Joyner 			if (bootverbose)
35388eb6488eSEric Joyner 				device_printf(dev, "Link is Down\n");
3539c19c7afeSEric Joyner 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
35408eb6488eSEric Joyner 			adapter->link_active = FALSE;
35418eb6488eSEric Joyner 			if (adapter->feat_en & IXGBE_FEATURE_SRIOV)
35428eb6488eSEric Joyner 				ixgbe_ping_all_vfs(adapter);
35438eb6488eSEric Joyner 		}
35448eb6488eSEric Joyner 	}
35458eb6488eSEric Joyner 
3546c19c7afeSEric Joyner 	ixgbe_update_stats_counters(adapter);
3547c19c7afeSEric Joyner 
3548c19c7afeSEric Joyner 	/* Re-enable link interrupts */
3549c19c7afeSEric Joyner        IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_LSC);
3550c19c7afeSEric Joyner } /* ixgbe_if_update_admin_status */
35518eb6488eSEric Joyner 
35528eb6488eSEric Joyner /************************************************************************
35538eb6488eSEric Joyner  * ixgbe_config_dmac - Configure DMA Coalescing
35548eb6488eSEric Joyner  ************************************************************************/
35558eb6488eSEric Joyner static void
35568eb6488eSEric Joyner ixgbe_config_dmac(struct adapter *adapter)
35578eb6488eSEric Joyner {
35588eb6488eSEric Joyner 	struct ixgbe_hw          *hw = &adapter->hw;
35598eb6488eSEric Joyner 	struct ixgbe_dmac_config *dcfg = &hw->mac.dmac_config;
35608eb6488eSEric Joyner 
35618eb6488eSEric Joyner 	if (hw->mac.type < ixgbe_mac_X550 || !hw->mac.ops.dmac_config)
35628eb6488eSEric Joyner 		return;
35638eb6488eSEric Joyner 
35648eb6488eSEric Joyner 	if (dcfg->watchdog_timer ^ adapter->dmac ||
35658eb6488eSEric Joyner 	    dcfg->link_speed ^ adapter->link_speed) {
35668eb6488eSEric Joyner 		dcfg->watchdog_timer = adapter->dmac;
3567c19c7afeSEric Joyner 		dcfg->fcoe_en = FALSE;
35688eb6488eSEric Joyner 		dcfg->link_speed = adapter->link_speed;
35698eb6488eSEric Joyner 		dcfg->num_tcs = 1;
35708eb6488eSEric Joyner 
35718eb6488eSEric Joyner 		INIT_DEBUGOUT2("dmac settings: watchdog %d, link speed %d\n",
35728eb6488eSEric Joyner 		    dcfg->watchdog_timer, dcfg->link_speed);
35738eb6488eSEric Joyner 
35748eb6488eSEric Joyner 		hw->mac.ops.dmac_config(hw);
35758eb6488eSEric Joyner 	}
35768eb6488eSEric Joyner } /* ixgbe_config_dmac */
35778eb6488eSEric Joyner 
35788eb6488eSEric Joyner /************************************************************************
3579c19c7afeSEric Joyner  * ixgbe_if_enable_intr
35808eb6488eSEric Joyner  ************************************************************************/
3581c19c7afeSEric Joyner void
3582c19c7afeSEric Joyner ixgbe_if_enable_intr(if_ctx_t ctx)
35838eb6488eSEric Joyner {
3584c19c7afeSEric Joyner 	struct adapter     *adapter = iflib_get_softc(ctx);
35858eb6488eSEric Joyner 	struct ixgbe_hw    *hw = &adapter->hw;
3586c19c7afeSEric Joyner 	struct ix_rx_queue *que = adapter->rx_queues;
35878eb6488eSEric Joyner 	u32                mask, fwsm;
35888eb6488eSEric Joyner 
35898eb6488eSEric Joyner 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
35908eb6488eSEric Joyner 
35918eb6488eSEric Joyner 	switch (adapter->hw.mac.type) {
35928eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
35938eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
35948eb6488eSEric Joyner 		/* Temperature sensor on some adapters */
35958eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP0;
35968eb6488eSEric Joyner 		/* SFP+ (RX_LOS_N & MOD_ABS_N) */
35978eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP1;
35988eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP2;
35998eb6488eSEric Joyner 		break;
36008eb6488eSEric Joyner 	case ixgbe_mac_X540:
36018eb6488eSEric Joyner 		/* Detect if Thermal Sensor is enabled */
36028eb6488eSEric Joyner 		fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
36038eb6488eSEric Joyner 		if (fwsm & IXGBE_FWSM_TS_ENABLED)
36048eb6488eSEric Joyner 			mask |= IXGBE_EIMS_TS;
36058eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
36068eb6488eSEric Joyner 		break;
36078eb6488eSEric Joyner 	case ixgbe_mac_X550:
36088eb6488eSEric Joyner 		/* MAC thermal sensor is automatically enabled */
36098eb6488eSEric Joyner 		mask |= IXGBE_EIMS_TS;
36108eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
36118eb6488eSEric Joyner 		break;
36128eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
36138eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
36148eb6488eSEric Joyner 		/* Some devices use SDP0 for important information */
36158eb6488eSEric Joyner 		if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
36168eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP ||
36178eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP_N ||
36188eb6488eSEric Joyner 		    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
36198eb6488eSEric Joyner 			mask |= IXGBE_EIMS_GPI_SDP0_BY_MAC(hw);
36208eb6488eSEric Joyner 		if (hw->phy.type == ixgbe_phy_x550em_ext_t)
36218eb6488eSEric Joyner 			mask |= IXGBE_EICR_GPI_SDP0_X540;
36228eb6488eSEric Joyner 		mask |= IXGBE_EIMS_ECC;
36238eb6488eSEric Joyner 		break;
36248eb6488eSEric Joyner 	default:
36258eb6488eSEric Joyner 		break;
36268eb6488eSEric Joyner 	}
36278eb6488eSEric Joyner 
36288eb6488eSEric Joyner 	/* Enable Fan Failure detection */
36298eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL)
36308eb6488eSEric Joyner 		mask |= IXGBE_EIMS_GPI_SDP1;
36318eb6488eSEric Joyner 	/* Enable SR-IOV */
36328eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_SRIOV)
36338eb6488eSEric Joyner 		mask |= IXGBE_EIMS_MAILBOX;
36348eb6488eSEric Joyner 	/* Enable Flow Director */
36358eb6488eSEric Joyner 	if (adapter->feat_en & IXGBE_FEATURE_FDIR)
36368eb6488eSEric Joyner 		mask |= IXGBE_EIMS_FLOW_DIR;
36378eb6488eSEric Joyner 
36388eb6488eSEric Joyner 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
36398eb6488eSEric Joyner 
36408eb6488eSEric Joyner 	/* With MSI-X we use auto clear */
3641c19c7afeSEric Joyner 	if (adapter->intr_type == IFLIB_INTR_MSIX) {
36428eb6488eSEric Joyner 		mask = IXGBE_EIMS_ENABLE_MASK;
36438eb6488eSEric Joyner 		/* Don't autoclear Link */
36448eb6488eSEric Joyner 		mask &= ~IXGBE_EIMS_OTHER;
36458eb6488eSEric Joyner 		mask &= ~IXGBE_EIMS_LSC;
36468eb6488eSEric Joyner 		if (adapter->feat_cap & IXGBE_FEATURE_SRIOV)
36478eb6488eSEric Joyner 			mask &= ~IXGBE_EIMS_MAILBOX;
36488eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
36496f37f232SEric Joyner 	}
36506f37f232SEric Joyner 
3651758cc3dcSJack F Vogel 	/*
36528eb6488eSEric Joyner 	 * Now enable all queues, this is done separately to
36538eb6488eSEric Joyner 	 * allow for handling the extended (beyond 32) MSI-X
36548eb6488eSEric Joyner 	 * vectors that can be used by 82599
3655758cc3dcSJack F Vogel 	 */
3656c19c7afeSEric Joyner 	for (int i = 0; i < adapter->num_rx_queues; i++, que++)
36578eb6488eSEric Joyner 		ixgbe_enable_queue(adapter, que->msix);
36588eb6488eSEric Joyner 
36598eb6488eSEric Joyner 	IXGBE_WRITE_FLUSH(hw);
36608eb6488eSEric Joyner 
3661c19c7afeSEric Joyner } /* ixgbe_if_enable_intr */
36628eb6488eSEric Joyner 
36638eb6488eSEric Joyner /************************************************************************
36648eb6488eSEric Joyner  * ixgbe_disable_intr
36658eb6488eSEric Joyner  ************************************************************************/
3666758cc3dcSJack F Vogel static void
3667c19c7afeSEric Joyner ixgbe_if_disable_intr(if_ctx_t ctx)
3668758cc3dcSJack F Vogel {
3669c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
3670c19c7afeSEric Joyner 
3671c19c7afeSEric Joyner 	if (adapter->intr_type == IFLIB_INTR_MSIX)
36728eb6488eSEric Joyner 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, 0);
36738eb6488eSEric Joyner 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
36748eb6488eSEric Joyner 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
36758eb6488eSEric Joyner 	} else {
36768eb6488eSEric Joyner 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, 0xFFFF0000);
36778eb6488eSEric Joyner 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), ~0);
36788eb6488eSEric Joyner 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), ~0);
36798eb6488eSEric Joyner 	}
36808eb6488eSEric Joyner 	IXGBE_WRITE_FLUSH(&adapter->hw);
3681758cc3dcSJack F Vogel 
3682c19c7afeSEric Joyner } /* ixgbe_if_disable_intr */
36838eb6488eSEric Joyner 
36848eb6488eSEric Joyner /************************************************************************
3685c19c7afeSEric Joyner  * ixgbe_if_rx_queue_intr_enable
3686c19c7afeSEric Joyner  ************************************************************************/
3687c19c7afeSEric Joyner static int
3688c19c7afeSEric Joyner ixgbe_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
3689c19c7afeSEric Joyner {
3690c19c7afeSEric Joyner 	struct adapter     *adapter = iflib_get_softc(ctx);
3691c19c7afeSEric Joyner 	struct ix_rx_queue *que = &adapter->rx_queues[rxqid];
3692c19c7afeSEric Joyner 
3693c19c7afeSEric Joyner 	ixgbe_enable_queue(adapter, que->rxr.me);
3694c19c7afeSEric Joyner 
3695c19c7afeSEric Joyner 	return (0);
3696c19c7afeSEric Joyner } /* ixgbe_if_rx_queue_intr_enable */
3697c19c7afeSEric Joyner 
3698c19c7afeSEric Joyner /************************************************************************
3699c19c7afeSEric Joyner  * ixgbe_enable_queue
37008eb6488eSEric Joyner  ************************************************************************/
37018eb6488eSEric Joyner static void
3702c19c7afeSEric Joyner ixgbe_enable_queue(struct adapter *adapter, u32 vector)
37038eb6488eSEric Joyner {
37048eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
3705c19c7afeSEric Joyner 	u64             queue = (u64)(1 << vector);
3706c19c7afeSEric Joyner 	u32             mask;
3707758cc3dcSJack F Vogel 
3708c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB) {
3709c19c7afeSEric Joyner 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3710c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
3711c19c7afeSEric Joyner 	} else {
3712c19c7afeSEric Joyner 		mask = (queue & 0xFFFFFFFF);
3713c19c7afeSEric Joyner 		if (mask)
3714c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
3715c19c7afeSEric Joyner 		mask = (queue >> 32);
3716c19c7afeSEric Joyner 		if (mask)
3717c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
3718c19c7afeSEric Joyner 	}
3719c19c7afeSEric Joyner } /* ixgbe_enable_queue */
3720c19c7afeSEric Joyner 
3721c19c7afeSEric Joyner /************************************************************************
3722c19c7afeSEric Joyner  * ixgbe_disable_queue
3723c19c7afeSEric Joyner  ************************************************************************/
3724c19c7afeSEric Joyner static void
3725c19c7afeSEric Joyner ixgbe_disable_queue(struct adapter *adapter, u32 vector)
3726c19c7afeSEric Joyner {
3727c19c7afeSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
3728c19c7afeSEric Joyner 	u64             queue = (u64)(1 << vector);
3729c19c7afeSEric Joyner 	u32             mask;
3730c19c7afeSEric Joyner 
3731c19c7afeSEric Joyner 	if (hw->mac.type == ixgbe_mac_82598EB) {
3732c19c7afeSEric Joyner 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3733c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
3734c19c7afeSEric Joyner 	} else {
3735c19c7afeSEric Joyner 		mask = (queue & 0xFFFFFFFF);
3736c19c7afeSEric Joyner 		if (mask)
3737c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
3738c19c7afeSEric Joyner 		mask = (queue >> 32);
3739c19c7afeSEric Joyner 		if (mask)
3740c19c7afeSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
3741c19c7afeSEric Joyner 	}
3742c19c7afeSEric Joyner } /* ixgbe_disable_queue */
3743c19c7afeSEric Joyner 
3744c19c7afeSEric Joyner /************************************************************************
3745c19c7afeSEric Joyner  * ixgbe_intr - Legacy Interrupt Service Routine
3746c19c7afeSEric Joyner  ************************************************************************/
3747c19c7afeSEric Joyner int
3748c19c7afeSEric Joyner ixgbe_intr(void *arg)
3749c19c7afeSEric Joyner {
3750c19c7afeSEric Joyner 	struct adapter     *adapter = arg;
3751c19c7afeSEric Joyner 	struct ix_rx_queue *que = adapter->rx_queues;
3752c19c7afeSEric Joyner 	struct ixgbe_hw    *hw = &adapter->hw;
3753c19c7afeSEric Joyner 	if_ctx_t           ctx = adapter->ctx;
3754c19c7afeSEric Joyner 	u32                eicr, eicr_mask;
3755758cc3dcSJack F Vogel 
37568eb6488eSEric Joyner 	eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
3757758cc3dcSJack F Vogel 
37588eb6488eSEric Joyner 	++que->irqs;
37598eb6488eSEric Joyner 	if (eicr == 0) {
3760c19c7afeSEric Joyner 		ixgbe_if_enable_intr(ctx);
3761c19c7afeSEric Joyner 		return (FILTER_HANDLED);
3762758cc3dcSJack F Vogel 	}
3763758cc3dcSJack F Vogel 
37648eb6488eSEric Joyner 	/* Check for fan failure */
3765c19c7afeSEric Joyner 	if ((hw->device_id == IXGBE_DEV_ID_82598AT) &&
3766c19c7afeSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP1)) {
3767c19c7afeSEric Joyner 		device_printf(adapter->dev,
3768c19c7afeSEric Joyner 		    "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
37698eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3770758cc3dcSJack F Vogel 	}
3771758cc3dcSJack F Vogel 
37728eb6488eSEric Joyner 	/* Link status change */
3773c19c7afeSEric Joyner 	if (eicr & IXGBE_EICR_LSC) {
3774c19c7afeSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
3775c19c7afeSEric Joyner 		iflib_admin_intr_deferred(ctx);
3776c19c7afeSEric Joyner 	}
37778eb6488eSEric Joyner 
37788eb6488eSEric Joyner 	if (ixgbe_is_sfp(hw)) {
37798eb6488eSEric Joyner 		/* Pluggable optics-related interrupt */
37808eb6488eSEric Joyner 		if (hw->mac.type >= ixgbe_mac_X540)
37818eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
37828eb6488eSEric Joyner 		else
37838eb6488eSEric Joyner 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
37848eb6488eSEric Joyner 
37858eb6488eSEric Joyner 		if (eicr & eicr_mask) {
37868eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
3787c19c7afeSEric Joyner 			GROUPTASK_ENQUEUE(&adapter->mod_task);
37888eb6488eSEric Joyner 		}
37898eb6488eSEric Joyner 
37908eb6488eSEric Joyner 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
37918eb6488eSEric Joyner 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
37928eb6488eSEric Joyner 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
37938eb6488eSEric Joyner 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3794c19c7afeSEric Joyner 			if (atomic_cmpset_acq_int(&adapter->sfp_reinit, 0, 1))
3795c19c7afeSEric Joyner 				GROUPTASK_ENQUEUE(&adapter->msf_task);
37968eb6488eSEric Joyner 		}
37978eb6488eSEric Joyner 	}
37988eb6488eSEric Joyner 
37998eb6488eSEric Joyner 	/* External PHY interrupt */
38008eb6488eSEric Joyner 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
38018eb6488eSEric Joyner 	    (eicr & IXGBE_EICR_GPI_SDP0_X540))
3802c19c7afeSEric Joyner 		GROUPTASK_ENQUEUE(&adapter->phy_task);
38038eb6488eSEric Joyner 
3804c19c7afeSEric Joyner 	return (FILTER_SCHEDULE_THREAD);
3805c19c7afeSEric Joyner } /* ixgbe_intr */
38068eb6488eSEric Joyner 
38078eb6488eSEric Joyner /************************************************************************
38088eb6488eSEric Joyner  * ixgbe_free_pci_resources
38098eb6488eSEric Joyner  ************************************************************************/
38108eb6488eSEric Joyner static void
3811c19c7afeSEric Joyner ixgbe_free_pci_resources(if_ctx_t ctx)
38128eb6488eSEric Joyner {
3813c19c7afeSEric Joyner 	struct adapter *adapter = iflib_get_softc(ctx);
3814c19c7afeSEric Joyner 	struct         ix_rx_queue *que = adapter->rx_queues;
3815c19c7afeSEric Joyner 	device_t       dev = iflib_get_dev(ctx);
38168eb6488eSEric Joyner 
3817*b97de13aSMarius Strobl 	/* Release all MSI-X queue resources */
3818c19c7afeSEric Joyner 	if (adapter->intr_type == IFLIB_INTR_MSIX)
3819c19c7afeSEric Joyner 		iflib_irq_free(ctx, &adapter->irq);
3820c19c7afeSEric Joyner 
3821c19c7afeSEric Joyner 	if (que != NULL) {
3822c19c7afeSEric Joyner 		for (int i = 0; i < adapter->num_rx_queues; i++, que++) {
3823c19c7afeSEric Joyner 			iflib_irq_free(ctx, &que->que_irq);
3824c19c7afeSEric Joyner 		}
3825c19c7afeSEric Joyner 	}
38268eb6488eSEric Joyner 
38278eb6488eSEric Joyner 	if (adapter->pci_mem != NULL)
3828c19c7afeSEric Joyner 		bus_release_resource(dev, SYS_RES_MEMORY,
3829*b97de13aSMarius Strobl 		    rman_get_rid(adapter->pci_mem), adapter->pci_mem);
38308eb6488eSEric Joyner } /* ixgbe_free_pci_resources */
38318eb6488eSEric Joyner 
38328eb6488eSEric Joyner /************************************************************************
38338eb6488eSEric Joyner  * ixgbe_sysctl_flowcntl
38348eb6488eSEric Joyner  *
38358eb6488eSEric Joyner  *   SYSCTL wrapper around setting Flow Control
38368eb6488eSEric Joyner  ************************************************************************/
3837758cc3dcSJack F Vogel static int
3838f2c4db54SSteven Hartland ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS)
3839758cc3dcSJack F Vogel {
3840f2c4db54SSteven Hartland 	struct adapter *adapter;
38418eb6488eSEric Joyner 	int            error, fc;
3842758cc3dcSJack F Vogel 
3843f2c4db54SSteven Hartland 	adapter = (struct adapter *)arg1;
38448eb6488eSEric Joyner 	fc = adapter->hw.fc.current_mode;
3845f2c4db54SSteven Hartland 
3846f2c4db54SSteven Hartland 	error = sysctl_handle_int(oidp, &fc, 0, req);
3847758cc3dcSJack F Vogel 	if ((error) || (req->newptr == NULL))
3848758cc3dcSJack F Vogel 		return (error);
3849758cc3dcSJack F Vogel 
3850758cc3dcSJack F Vogel 	/* Don't bother if it's not changed */
38518eb6488eSEric Joyner 	if (fc == adapter->hw.fc.current_mode)
3852758cc3dcSJack F Vogel 		return (0);
3853758cc3dcSJack F Vogel 
3854f2c4db54SSteven Hartland 	return ixgbe_set_flowcntl(adapter, fc);
38558eb6488eSEric Joyner } /* ixgbe_sysctl_flowcntl */
3856f2c4db54SSteven Hartland 
38578eb6488eSEric Joyner /************************************************************************
38588eb6488eSEric Joyner  * ixgbe_set_flowcntl - Set flow control
38598eb6488eSEric Joyner  *
38608eb6488eSEric Joyner  *   Flow control values:
38618eb6488eSEric Joyner  *     0 - off
38628eb6488eSEric Joyner  *     1 - rx pause
38638eb6488eSEric Joyner  *     2 - tx pause
38648eb6488eSEric Joyner  *     3 - full
38658eb6488eSEric Joyner  ************************************************************************/
3866f2c4db54SSteven Hartland static int
3867f2c4db54SSteven Hartland ixgbe_set_flowcntl(struct adapter *adapter, int fc)
3868f2c4db54SSteven Hartland {
3869f2c4db54SSteven Hartland 	switch (fc) {
3870758cc3dcSJack F Vogel 	case ixgbe_fc_rx_pause:
3871758cc3dcSJack F Vogel 	case ixgbe_fc_tx_pause:
3872758cc3dcSJack F Vogel 	case ixgbe_fc_full:
38738eb6488eSEric Joyner 		adapter->hw.fc.requested_mode = fc;
3874c19c7afeSEric Joyner 		if (adapter->num_rx_queues > 1)
3875758cc3dcSJack F Vogel 			ixgbe_disable_rx_drop(adapter);
3876758cc3dcSJack F Vogel 		break;
3877758cc3dcSJack F Vogel 	case ixgbe_fc_none:
3878758cc3dcSJack F Vogel 		adapter->hw.fc.requested_mode = ixgbe_fc_none;
3879c19c7afeSEric Joyner 		if (adapter->num_rx_queues > 1)
3880758cc3dcSJack F Vogel 			ixgbe_enable_rx_drop(adapter);
3881758cc3dcSJack F Vogel 		break;
3882758cc3dcSJack F Vogel 	default:
3883758cc3dcSJack F Vogel 		return (EINVAL);
3884758cc3dcSJack F Vogel 	}
38858eb6488eSEric Joyner 
3886758cc3dcSJack F Vogel 	/* Don't autoneg if forcing a value */
3887758cc3dcSJack F Vogel 	adapter->hw.fc.disable_fc_autoneg = TRUE;
3888758cc3dcSJack F Vogel 	ixgbe_fc_enable(&adapter->hw);
38898eb6488eSEric Joyner 
3890f2c4db54SSteven Hartland 	return (0);
38918eb6488eSEric Joyner } /* ixgbe_set_flowcntl */
38928eb6488eSEric Joyner 
38938eb6488eSEric Joyner /************************************************************************
38948eb6488eSEric Joyner  * ixgbe_enable_rx_drop
38958eb6488eSEric Joyner  *
38968eb6488eSEric Joyner  *   Enable the hardware to drop packets when the buffer is
38978eb6488eSEric Joyner  *   full. This is useful with multiqueue, so that no single
38988eb6488eSEric Joyner  *   queue being full stalls the entire RX engine. We only
38998eb6488eSEric Joyner  *   enable this when Multiqueue is enabled AND Flow Control
39008eb6488eSEric Joyner  *   is disabled.
39018eb6488eSEric Joyner  ************************************************************************/
39028eb6488eSEric Joyner static void
39038eb6488eSEric Joyner ixgbe_enable_rx_drop(struct adapter *adapter)
39048eb6488eSEric Joyner {
39058eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
39068eb6488eSEric Joyner 	struct rx_ring  *rxr;
39078eb6488eSEric Joyner 	u32             srrctl;
39088eb6488eSEric Joyner 
3909c19c7afeSEric Joyner 	for (int i = 0; i < adapter->num_rx_queues; i++) {
3910c19c7afeSEric Joyner 		rxr = &adapter->rx_queues[i].rxr;
39118eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
39128eb6488eSEric Joyner 		srrctl |= IXGBE_SRRCTL_DROP_EN;
39138eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
3914758cc3dcSJack F Vogel 	}
3915758cc3dcSJack F Vogel 
39168eb6488eSEric Joyner 	/* enable drop for each vf */
39178eb6488eSEric Joyner 	for (int i = 0; i < adapter->num_vfs; i++) {
39188eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
39198eb6488eSEric Joyner 		                (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT) |
39208eb6488eSEric Joyner 		                IXGBE_QDE_ENABLE));
39218eb6488eSEric Joyner 	}
39228eb6488eSEric Joyner } /* ixgbe_enable_rx_drop */
39238eb6488eSEric Joyner 
39248eb6488eSEric Joyner /************************************************************************
39258eb6488eSEric Joyner  * ixgbe_disable_rx_drop
39268eb6488eSEric Joyner  ************************************************************************/
39278eb6488eSEric Joyner static void
39288eb6488eSEric Joyner ixgbe_disable_rx_drop(struct adapter *adapter)
39298eb6488eSEric Joyner {
39308eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
39318eb6488eSEric Joyner 	struct rx_ring  *rxr;
39328eb6488eSEric Joyner 	u32             srrctl;
39338eb6488eSEric Joyner 
3934c19c7afeSEric Joyner 	for (int i = 0; i < adapter->num_rx_queues; i++) {
3935c19c7afeSEric Joyner 		rxr = &adapter->rx_queues[i].rxr;
39368eb6488eSEric Joyner 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
39378eb6488eSEric Joyner 		srrctl &= ~IXGBE_SRRCTL_DROP_EN;
39388eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
39398eb6488eSEric Joyner 	}
39408eb6488eSEric Joyner 
39418eb6488eSEric Joyner 	/* disable drop for each vf */
39428eb6488eSEric Joyner 	for (int i = 0; i < adapter->num_vfs; i++) {
39438eb6488eSEric Joyner 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
39448eb6488eSEric Joyner 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT)));
39458eb6488eSEric Joyner 	}
39468eb6488eSEric Joyner } /* ixgbe_disable_rx_drop */
39478eb6488eSEric Joyner 
39488eb6488eSEric Joyner /************************************************************************
39498eb6488eSEric Joyner  * ixgbe_sysctl_advertise
39508eb6488eSEric Joyner  *
39518eb6488eSEric Joyner  *   SYSCTL wrapper around setting advertised speed
39528eb6488eSEric Joyner  ************************************************************************/
3953758cc3dcSJack F Vogel static int
3954f2c4db54SSteven Hartland ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS)
3955758cc3dcSJack F Vogel {
3956758cc3dcSJack F Vogel 	struct adapter *adapter;
39578eb6488eSEric Joyner 	int            error, advertise;
3958758cc3dcSJack F Vogel 
3959758cc3dcSJack F Vogel 	adapter = (struct adapter *)arg1;
3960f2c4db54SSteven Hartland 	advertise = adapter->advertise;
3961758cc3dcSJack F Vogel 
3962f2c4db54SSteven Hartland 	error = sysctl_handle_int(oidp, &advertise, 0, req);
3963758cc3dcSJack F Vogel 	if ((error) || (req->newptr == NULL))
3964758cc3dcSJack F Vogel 		return (error);
3965758cc3dcSJack F Vogel 
3966f2c4db54SSteven Hartland 	return ixgbe_set_advertise(adapter, advertise);
39678eb6488eSEric Joyner } /* ixgbe_sysctl_advertise */
3968f2c4db54SSteven Hartland 
39698eb6488eSEric Joyner /************************************************************************
39708eb6488eSEric Joyner  * ixgbe_set_advertise - Control advertised link speed
39718eb6488eSEric Joyner  *
39728eb6488eSEric Joyner  *   Flags:
39738eb6488eSEric Joyner  *     0x1 - advertise 100 Mb
39748eb6488eSEric Joyner  *     0x2 - advertise 1G
39758eb6488eSEric Joyner  *     0x4 - advertise 10G
39768eb6488eSEric Joyner  *     0x8 - advertise 10 Mb (yes, Mb)
39778eb6488eSEric Joyner  ************************************************************************/
3978f2c4db54SSteven Hartland static int
3979f2c4db54SSteven Hartland ixgbe_set_advertise(struct adapter *adapter, int advertise)
3980f2c4db54SSteven Hartland {
3981c19c7afeSEric Joyner 	device_t         dev = iflib_get_dev(adapter->ctx);
3982f2c4db54SSteven Hartland 	struct ixgbe_hw  *hw;
39838eb6488eSEric Joyner 	ixgbe_link_speed speed = 0;
39848eb6488eSEric Joyner 	ixgbe_link_speed link_caps = 0;
39858eb6488eSEric Joyner 	s32              err = IXGBE_NOT_IMPLEMENTED;
39868eb6488eSEric Joyner 	bool             negotiate = FALSE;
3987f2c4db54SSteven Hartland 
3988d775d23aSSteven Hartland 	/* Checks to validate new value */
3989d775d23aSSteven Hartland 	if (adapter->advertise == advertise) /* no change */
3990d775d23aSSteven Hartland 		return (0);
3991d775d23aSSteven Hartland 
39928eb6488eSEric Joyner 	hw = &adapter->hw;
3993f2c4db54SSteven Hartland 
3994a9ca1c79SSean Bruno 	/* No speed changes for backplane media */
3995a9ca1c79SSean Bruno 	if (hw->phy.media_type == ixgbe_media_type_backplane)
3996a9ca1c79SSean Bruno 		return (ENODEV);
3997a9ca1c79SSean Bruno 
3998758cc3dcSJack F Vogel 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
3999758cc3dcSJack F Vogel 	      (hw->phy.multispeed_fiber))) {
40008eb6488eSEric Joyner 		device_printf(dev, "Advertised speed can only be set on copper or multispeed fiber media types.\n");
4001758cc3dcSJack F Vogel 		return (EINVAL);
4002758cc3dcSJack F Vogel 	}
4003758cc3dcSJack F Vogel 
40048eb6488eSEric Joyner 	if (advertise < 0x1 || advertise > 0xF) {
40058eb6488eSEric Joyner 		device_printf(dev, "Invalid advertised speed; valid modes are 0x1 through 0xF\n");
4006758cc3dcSJack F Vogel 		return (EINVAL);
4007758cc3dcSJack F Vogel 	}
4008758cc3dcSJack F Vogel 
40098eb6488eSEric Joyner 	if (hw->mac.ops.get_link_capabilities) {
40108eb6488eSEric Joyner 		err = hw->mac.ops.get_link_capabilities(hw, &link_caps,
40118eb6488eSEric Joyner 		    &negotiate);
40128eb6488eSEric Joyner 		if (err != IXGBE_SUCCESS) {
40138eb6488eSEric Joyner 			device_printf(dev, "Unable to determine supported advertise speeds\n");
40148eb6488eSEric Joyner 			return (ENODEV);
40158eb6488eSEric Joyner 		}
4016758cc3dcSJack F Vogel 	}
4017758cc3dcSJack F Vogel 
4018758cc3dcSJack F Vogel 	/* Set new value and report new advertised mode */
40198eb6488eSEric Joyner 	if (advertise & 0x1) {
40208eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_100_FULL)) {
40218eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 100Mb advertised speed\n");
40228eb6488eSEric Joyner 			return (EINVAL);
40238eb6488eSEric Joyner 		}
4024758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_100_FULL;
40258eb6488eSEric Joyner 	}
40268eb6488eSEric Joyner 	if (advertise & 0x2) {
40278eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_1GB_FULL)) {
40288eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 1Gb advertised speed\n");
40298eb6488eSEric Joyner 			return (EINVAL);
40308eb6488eSEric Joyner 		}
4031758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
40328eb6488eSEric Joyner 	}
40338eb6488eSEric Joyner 	if (advertise & 0x4) {
40348eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_10GB_FULL)) {
40358eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 10Gb advertised speed\n");
40368eb6488eSEric Joyner 			return (EINVAL);
40378eb6488eSEric Joyner 		}
4038758cc3dcSJack F Vogel 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
40398eb6488eSEric Joyner 	}
40408eb6488eSEric Joyner 	if (advertise & 0x8) {
40418eb6488eSEric Joyner 		if (!(link_caps & IXGBE_LINK_SPEED_10_FULL)) {
40428eb6488eSEric Joyner 			device_printf(dev, "Interface does not support 10Mb advertised speed\n");
40438eb6488eSEric Joyner 			return (EINVAL);
40448eb6488eSEric Joyner 		}
40458eb6488eSEric Joyner 		speed |= IXGBE_LINK_SPEED_10_FULL;
40468eb6488eSEric Joyner 	}
4047758cc3dcSJack F Vogel 
4048758cc3dcSJack F Vogel 	hw->mac.autotry_restart = TRUE;
4049758cc3dcSJack F Vogel 	hw->mac.ops.setup_link(hw, speed, TRUE);
40508eb6488eSEric Joyner 	adapter->advertise = advertise;
4051758cc3dcSJack F Vogel 
4052f2c4db54SSteven Hartland 	return (0);
40538eb6488eSEric Joyner } /* ixgbe_set_advertise */
40548eb6488eSEric Joyner 
40558eb6488eSEric Joyner /************************************************************************
40568eb6488eSEric Joyner  * ixgbe_get_advertise - Get current advertised speed settings
40578eb6488eSEric Joyner  *
40588eb6488eSEric Joyner  *   Formatted for sysctl usage.
40598eb6488eSEric Joyner  *   Flags:
40608eb6488eSEric Joyner  *     0x1 - advertise 100 Mb
40618eb6488eSEric Joyner  *     0x2 - advertise 1G
40628eb6488eSEric Joyner  *     0x4 - advertise 10G
40638eb6488eSEric Joyner  *     0x8 - advertise 10 Mb (yes, Mb)
40648eb6488eSEric Joyner  ************************************************************************/
40658eb6488eSEric Joyner static int
40668eb6488eSEric Joyner ixgbe_get_advertise(struct adapter *adapter)
40678eb6488eSEric Joyner {
40688eb6488eSEric Joyner 	struct ixgbe_hw  *hw = &adapter->hw;
40698eb6488eSEric Joyner 	int              speed;
40708eb6488eSEric Joyner 	ixgbe_link_speed link_caps = 0;
40718eb6488eSEric Joyner 	s32              err;
40728eb6488eSEric Joyner 	bool             negotiate = FALSE;
4073758cc3dcSJack F Vogel 
4074758cc3dcSJack F Vogel 	/*
40758eb6488eSEric Joyner 	 * Advertised speed means nothing unless it's copper or
40768eb6488eSEric Joyner 	 * multi-speed fiber
4077758cc3dcSJack F Vogel 	 */
40788eb6488eSEric Joyner 	if (!(hw->phy.media_type == ixgbe_media_type_copper) &&
40798eb6488eSEric Joyner 	    !(hw->phy.multispeed_fiber))
4080758cc3dcSJack F Vogel 		return (0);
4081758cc3dcSJack F Vogel 
40828eb6488eSEric Joyner 	err = hw->mac.ops.get_link_capabilities(hw, &link_caps, &negotiate);
40838eb6488eSEric Joyner 	if (err != IXGBE_SUCCESS)
40848eb6488eSEric Joyner 		return (0);
40858eb6488eSEric Joyner 
40868eb6488eSEric Joyner 	speed =
40878eb6488eSEric Joyner 	    ((link_caps & IXGBE_LINK_SPEED_10GB_FULL) ? 4 : 0) |
40888eb6488eSEric Joyner 	    ((link_caps & IXGBE_LINK_SPEED_1GB_FULL)  ? 2 : 0) |
40898eb6488eSEric Joyner 	    ((link_caps & IXGBE_LINK_SPEED_100_FULL)  ? 1 : 0) |
40908eb6488eSEric Joyner 	    ((link_caps & IXGBE_LINK_SPEED_10_FULL)   ? 8 : 0);
40918eb6488eSEric Joyner 
40928eb6488eSEric Joyner 	return speed;
40938eb6488eSEric Joyner } /* ixgbe_get_advertise */
40948eb6488eSEric Joyner 
40958eb6488eSEric Joyner /************************************************************************
40968eb6488eSEric Joyner  * ixgbe_sysctl_dmac - Manage DMA Coalescing
40978eb6488eSEric Joyner  *
40988eb6488eSEric Joyner  *   Control values:
40998eb6488eSEric Joyner  *     0/1 - off / on (use default value of 1000)
41008eb6488eSEric Joyner  *
41018eb6488eSEric Joyner  *     Legal timer values are:
41028eb6488eSEric Joyner  *     50,100,250,500,1000,2000,5000,10000
41038eb6488eSEric Joyner  *
41048eb6488eSEric Joyner  *     Turning off interrupt moderation will also turn this off.
41058eb6488eSEric Joyner  ************************************************************************/
41066f37f232SEric Joyner static int
41076f37f232SEric Joyner ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS)
41086f37f232SEric Joyner {
41096f37f232SEric Joyner 	struct adapter *adapter = (struct adapter *)arg1;
4110c19c7afeSEric Joyner 	struct ifnet   *ifp = iflib_get_ifp(adapter->ctx);
41116f37f232SEric Joyner 	int            error;
4112c19c7afeSEric Joyner 	u16            newval;
41136f37f232SEric Joyner 
4114a9ca1c79SSean Bruno 	newval = adapter->dmac;
4115c19c7afeSEric Joyner 	error = sysctl_handle_16(oidp, &newval, 0, req);
41166f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
41176f37f232SEric Joyner 		return (error);
41186f37f232SEric Joyner 
4119a9ca1c79SSean Bruno 	switch (newval) {
41206f37f232SEric Joyner 	case 0:
41216f37f232SEric Joyner 		/* Disabled */
4122a9ca1c79SSean Bruno 		adapter->dmac = 0;
41236f37f232SEric Joyner 		break;
4124a9ca1c79SSean Bruno 	case 1:
4125a9ca1c79SSean Bruno 		/* Enable and use default */
41266f37f232SEric Joyner 		adapter->dmac = 1000;
41276f37f232SEric Joyner 		break;
41286f37f232SEric Joyner 	case 50:
41296f37f232SEric Joyner 	case 100:
41306f37f232SEric Joyner 	case 250:
41316f37f232SEric Joyner 	case 500:
41326f37f232SEric Joyner 	case 1000:
41336f37f232SEric Joyner 	case 2000:
41346f37f232SEric Joyner 	case 5000:
41356f37f232SEric Joyner 	case 10000:
41366f37f232SEric Joyner 		/* Legal values - allow */
4137a9ca1c79SSean Bruno 		adapter->dmac = newval;
41386f37f232SEric Joyner 		break;
41396f37f232SEric Joyner 	default:
41406f37f232SEric Joyner 		/* Do nothing, illegal value */
41416f37f232SEric Joyner 		return (EINVAL);
41426f37f232SEric Joyner 	}
41436f37f232SEric Joyner 
41446f37f232SEric Joyner 	/* Re-initialize hardware if it's already running */
41456f37f232SEric Joyner 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4146c19c7afeSEric Joyner 		ifp->if_init(ifp);
41476f37f232SEric Joyner 
41486f37f232SEric Joyner 	return (0);
41498eb6488eSEric Joyner } /* ixgbe_sysctl_dmac */
41506f37f232SEric Joyner 
4151a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
41528eb6488eSEric Joyner /************************************************************************
41538eb6488eSEric Joyner  * ixgbe_sysctl_power_state
41548eb6488eSEric Joyner  *
4155a9ca1c79SSean Bruno  *   Sysctl to test power states
4156a9ca1c79SSean Bruno  *   Values:
4157a9ca1c79SSean Bruno  *     0      - set device to D0
4158a9ca1c79SSean Bruno  *     3      - set device to D3
4159a9ca1c79SSean Bruno  *     (none) - get current device power state
41608eb6488eSEric Joyner  ************************************************************************/
4161a9ca1c79SSean Bruno static int
4162a9ca1c79SSean Bruno ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS)
4163a9ca1c79SSean Bruno {
4164a9ca1c79SSean Bruno 	struct adapter *adapter = (struct adapter *)arg1;
4165a9ca1c79SSean Bruno 	device_t       dev = adapter->dev;
4166a9ca1c79SSean Bruno 	int            curr_ps, new_ps, error = 0;
4167a9ca1c79SSean Bruno 
4168a9ca1c79SSean Bruno 	curr_ps = new_ps = pci_get_powerstate(dev);
4169a9ca1c79SSean Bruno 
4170a9ca1c79SSean Bruno 	error = sysctl_handle_int(oidp, &new_ps, 0, req);
4171a9ca1c79SSean Bruno 	if ((error) || (req->newptr == NULL))
4172a9ca1c79SSean Bruno 		return (error);
4173a9ca1c79SSean Bruno 
4174a9ca1c79SSean Bruno 	if (new_ps == curr_ps)
4175a9ca1c79SSean Bruno 		return (0);
4176a9ca1c79SSean Bruno 
4177a9ca1c79SSean Bruno 	if (new_ps == 3 && curr_ps == 0)
4178a9ca1c79SSean Bruno 		error = DEVICE_SUSPEND(dev);
4179a9ca1c79SSean Bruno 	else if (new_ps == 0 && curr_ps == 3)
4180a9ca1c79SSean Bruno 		error = DEVICE_RESUME(dev);
4181a9ca1c79SSean Bruno 	else
4182a9ca1c79SSean Bruno 		return (EINVAL);
4183a9ca1c79SSean Bruno 
4184a9ca1c79SSean Bruno 	device_printf(dev, "New state: %d\n", pci_get_powerstate(dev));
4185a9ca1c79SSean Bruno 
4186a9ca1c79SSean Bruno 	return (error);
41878eb6488eSEric Joyner } /* ixgbe_sysctl_power_state */
4188a9ca1c79SSean Bruno #endif
41898eb6488eSEric Joyner 
41908eb6488eSEric Joyner /************************************************************************
41918eb6488eSEric Joyner  * ixgbe_sysctl_wol_enable
41928eb6488eSEric Joyner  *
41938eb6488eSEric Joyner  *   Sysctl to enable/disable the WoL capability,
41948eb6488eSEric Joyner  *   if supported by the adapter.
41958eb6488eSEric Joyner  *
41966f37f232SEric Joyner  *   Values:
41976f37f232SEric Joyner  *     0 - disabled
41986f37f232SEric Joyner  *     1 - enabled
41998eb6488eSEric Joyner  ************************************************************************/
42006f37f232SEric Joyner static int
42016f37f232SEric Joyner ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS)
42026f37f232SEric Joyner {
42036f37f232SEric Joyner 	struct adapter  *adapter = (struct adapter *)arg1;
42046f37f232SEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
42056f37f232SEric Joyner 	int             new_wol_enabled;
42066f37f232SEric Joyner 	int             error = 0;
42076f37f232SEric Joyner 
42086f37f232SEric Joyner 	new_wol_enabled = hw->wol_enabled;
42096f37f232SEric Joyner 	error = sysctl_handle_int(oidp, &new_wol_enabled, 0, req);
42106f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
42116f37f232SEric Joyner 		return (error);
4212a9ca1c79SSean Bruno 	new_wol_enabled = !!(new_wol_enabled);
42136f37f232SEric Joyner 	if (new_wol_enabled == hw->wol_enabled)
42146f37f232SEric Joyner 		return (0);
42156f37f232SEric Joyner 
42166f37f232SEric Joyner 	if (new_wol_enabled > 0 && !adapter->wol_support)
42176f37f232SEric Joyner 		return (ENODEV);
42186f37f232SEric Joyner 	else
4219a9ca1c79SSean Bruno 		hw->wol_enabled = new_wol_enabled;
42206f37f232SEric Joyner 
42216f37f232SEric Joyner 	return (0);
42228eb6488eSEric Joyner } /* ixgbe_sysctl_wol_enable */
42236f37f232SEric Joyner 
42248eb6488eSEric Joyner /************************************************************************
42258eb6488eSEric Joyner  * ixgbe_sysctl_wufc - Wake Up Filter Control
42268eb6488eSEric Joyner  *
42276f37f232SEric Joyner  *   Sysctl to enable/disable the types of packets that the
42286f37f232SEric Joyner  *   adapter will wake up on upon receipt.
42296f37f232SEric Joyner  *   Flags:
42306f37f232SEric Joyner  *     0x1  - Link Status Change
42316f37f232SEric Joyner  *     0x2  - Magic Packet
42326f37f232SEric Joyner  *     0x4  - Direct Exact
42336f37f232SEric Joyner  *     0x8  - Directed Multicast
42346f37f232SEric Joyner  *     0x10 - Broadcast
42356f37f232SEric Joyner  *     0x20 - ARP/IPv4 Request Packet
42366f37f232SEric Joyner  *     0x40 - Direct IPv4 Packet
42376f37f232SEric Joyner  *     0x80 - Direct IPv6 Packet
42386f37f232SEric Joyner  *
42398eb6488eSEric Joyner  *   Settings not listed above will cause the sysctl to return an error.
42408eb6488eSEric Joyner  ************************************************************************/
42416f37f232SEric Joyner static int
42426f37f232SEric Joyner ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS)
42436f37f232SEric Joyner {
42446f37f232SEric Joyner 	struct adapter *adapter = (struct adapter *)arg1;
42456f37f232SEric Joyner 	int            error = 0;
42466f37f232SEric Joyner 	u32            new_wufc;
42476f37f232SEric Joyner 
42486f37f232SEric Joyner 	new_wufc = adapter->wufc;
42496f37f232SEric Joyner 
4250c19c7afeSEric Joyner 	error = sysctl_handle_32(oidp, &new_wufc, 0, req);
42516f37f232SEric Joyner 	if ((error) || (req->newptr == NULL))
42526f37f232SEric Joyner 		return (error);
42536f37f232SEric Joyner 	if (new_wufc == adapter->wufc)
42546f37f232SEric Joyner 		return (0);
42556f37f232SEric Joyner 
42566f37f232SEric Joyner 	if (new_wufc & 0xffffff00)
42576f37f232SEric Joyner 		return (EINVAL);
42588eb6488eSEric Joyner 
42596f37f232SEric Joyner 	new_wufc &= 0xff;
42606f37f232SEric Joyner 	new_wufc |= (0xffffff & adapter->wufc);
42616f37f232SEric Joyner 	adapter->wufc = new_wufc;
42626f37f232SEric Joyner 
42636f37f232SEric Joyner 	return (0);
42648eb6488eSEric Joyner } /* ixgbe_sysctl_wufc */
42656f37f232SEric Joyner 
4266a9ca1c79SSean Bruno #ifdef IXGBE_DEBUG
42678eb6488eSEric Joyner /************************************************************************
42688eb6488eSEric Joyner  * ixgbe_sysctl_print_rss_config
42698eb6488eSEric Joyner  ************************************************************************/
4270a9ca1c79SSean Bruno static int
4271a9ca1c79SSean Bruno ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS)
4272a9ca1c79SSean Bruno {
4273a9ca1c79SSean Bruno 	struct adapter  *adapter = (struct adapter *)arg1;
4274a9ca1c79SSean Bruno 	struct ixgbe_hw *hw = &adapter->hw;
4275a9ca1c79SSean Bruno 	device_t        dev = adapter->dev;
4276a9ca1c79SSean Bruno 	struct sbuf     *buf;
42778eb6488eSEric Joyner 	int             error = 0, reta_size;
4278a9ca1c79SSean Bruno 	u32             reg;
4279a9ca1c79SSean Bruno 
4280a9ca1c79SSean Bruno 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
4281a9ca1c79SSean Bruno 	if (!buf) {
4282a9ca1c79SSean Bruno 		device_printf(dev, "Could not allocate sbuf for output.\n");
4283a9ca1c79SSean Bruno 		return (ENOMEM);
4284a9ca1c79SSean Bruno 	}
4285a9ca1c79SSean Bruno 
4286a9ca1c79SSean Bruno 	// TODO: use sbufs to make a string to print out
4287a9ca1c79SSean Bruno 	/* Set multiplier for RETA setup and table size based on MAC */
4288a9ca1c79SSean Bruno 	switch (adapter->hw.mac.type) {
4289a9ca1c79SSean Bruno 	case ixgbe_mac_X550:
4290a9ca1c79SSean Bruno 	case ixgbe_mac_X550EM_x:
42918eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
4292a9ca1c79SSean Bruno 		reta_size = 128;
4293a9ca1c79SSean Bruno 		break;
4294a9ca1c79SSean Bruno 	default:
4295a9ca1c79SSean Bruno 		reta_size = 32;
4296a9ca1c79SSean Bruno 		break;
4297a9ca1c79SSean Bruno 	}
4298a9ca1c79SSean Bruno 
4299a9ca1c79SSean Bruno 	/* Print out the redirection table */
4300a9ca1c79SSean Bruno 	sbuf_cat(buf, "\n");
4301a9ca1c79SSean Bruno 	for (int i = 0; i < reta_size; i++) {
4302a9ca1c79SSean Bruno 		if (i < 32) {
4303a9ca1c79SSean Bruno 			reg = IXGBE_READ_REG(hw, IXGBE_RETA(i));
4304a9ca1c79SSean Bruno 			sbuf_printf(buf, "RETA(%2d): 0x%08x\n", i, reg);
4305a9ca1c79SSean Bruno 		} else {
4306a9ca1c79SSean Bruno 			reg = IXGBE_READ_REG(hw, IXGBE_ERETA(i - 32));
4307a9ca1c79SSean Bruno 			sbuf_printf(buf, "ERETA(%2d): 0x%08x\n", i - 32, reg);
4308a9ca1c79SSean Bruno 		}
4309a9ca1c79SSean Bruno 	}
4310a9ca1c79SSean Bruno 
4311a9ca1c79SSean Bruno 	// TODO: print more config
4312a9ca1c79SSean Bruno 
4313a9ca1c79SSean Bruno 	error = sbuf_finish(buf);
4314a9ca1c79SSean Bruno 	if (error)
4315a9ca1c79SSean Bruno 		device_printf(dev, "Error finishing sbuf: %d\n", error);
4316a9ca1c79SSean Bruno 
4317a9ca1c79SSean Bruno 	sbuf_delete(buf);
43188eb6488eSEric Joyner 
4319a9ca1c79SSean Bruno 	return (0);
43208eb6488eSEric Joyner } /* ixgbe_sysctl_print_rss_config */
4321a9ca1c79SSean Bruno #endif /* IXGBE_DEBUG */
4322a9ca1c79SSean Bruno 
43238eb6488eSEric Joyner /************************************************************************
43248eb6488eSEric Joyner  * ixgbe_sysctl_phy_temp - Retrieve temperature of PHY
43258eb6488eSEric Joyner  *
43268eb6488eSEric Joyner  *   For X552/X557-AT devices using an external PHY
43278eb6488eSEric Joyner  ************************************************************************/
43288eb6488eSEric Joyner static int
43298eb6488eSEric Joyner ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS)
43308eb6488eSEric Joyner {
43318eb6488eSEric Joyner 	struct adapter  *adapter = (struct adapter *)arg1;
43328eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
43338eb6488eSEric Joyner 	u16             reg;
43348eb6488eSEric Joyner 
43358eb6488eSEric Joyner 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4336c19c7afeSEric Joyner 		device_printf(iflib_get_dev(adapter->ctx),
43378eb6488eSEric Joyner 		    "Device has no supported external thermal sensor.\n");
43388eb6488eSEric Joyner 		return (ENODEV);
43398eb6488eSEric Joyner 	}
43408eb6488eSEric Joyner 
43418eb6488eSEric Joyner 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_CURRENT_TEMP,
43428eb6488eSEric Joyner 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4343c19c7afeSEric Joyner 		device_printf(iflib_get_dev(adapter->ctx),
43448eb6488eSEric Joyner 		    "Error reading from PHY's current temperature register\n");
43458eb6488eSEric Joyner 		return (EAGAIN);
43468eb6488eSEric Joyner 	}
43478eb6488eSEric Joyner 
43488eb6488eSEric Joyner 	/* Shift temp for output */
43498eb6488eSEric Joyner 	reg = reg >> 8;
43508eb6488eSEric Joyner 
4351c19c7afeSEric Joyner 	return (sysctl_handle_16(oidp, NULL, reg, req));
43528eb6488eSEric Joyner } /* ixgbe_sysctl_phy_temp */
43538eb6488eSEric Joyner 
43548eb6488eSEric Joyner /************************************************************************
43558eb6488eSEric Joyner  * ixgbe_sysctl_phy_overtemp_occurred
43568eb6488eSEric Joyner  *
43578eb6488eSEric Joyner  *   Reports (directly from the PHY) whether the current PHY
43588eb6488eSEric Joyner  *   temperature is over the overtemp threshold.
43598eb6488eSEric Joyner  ************************************************************************/
43608eb6488eSEric Joyner static int
43618eb6488eSEric Joyner ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS)
43628eb6488eSEric Joyner {
43638eb6488eSEric Joyner 	struct adapter  *adapter = (struct adapter *)arg1;
43648eb6488eSEric Joyner 	struct ixgbe_hw *hw = &adapter->hw;
43658eb6488eSEric Joyner 	u16             reg;
43668eb6488eSEric Joyner 
43678eb6488eSEric Joyner 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4368c19c7afeSEric Joyner 		device_printf(iflib_get_dev(adapter->ctx),
43698eb6488eSEric Joyner 		    "Device has no supported external thermal sensor.\n");
43708eb6488eSEric Joyner 		return (ENODEV);
43718eb6488eSEric Joyner 	}
43728eb6488eSEric Joyner 
43738eb6488eSEric Joyner 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_OVERTEMP_STATUS,
43748eb6488eSEric Joyner 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4375c19c7afeSEric Joyner 		device_printf(iflib_get_dev(adapter->ctx),
43768eb6488eSEric Joyner 		    "Error reading from PHY's temperature status register\n");
43778eb6488eSEric Joyner 		return (EAGAIN);
43788eb6488eSEric Joyner 	}
43798eb6488eSEric Joyner 
43808eb6488eSEric Joyner 	/* Get occurrence bit */
43818eb6488eSEric Joyner 	reg = !!(reg & 0x4000);
43828eb6488eSEric Joyner 
4383c19c7afeSEric Joyner 	return (sysctl_handle_16(oidp, 0, reg, req));
43848eb6488eSEric Joyner } /* ixgbe_sysctl_phy_overtemp_occurred */
43858eb6488eSEric Joyner 
43868eb6488eSEric Joyner /************************************************************************
43878eb6488eSEric Joyner  * ixgbe_sysctl_eee_state
43888eb6488eSEric Joyner  *
43898eb6488eSEric Joyner  *   Sysctl to set EEE power saving feature
43908eb6488eSEric Joyner  *   Values:
43918eb6488eSEric Joyner  *     0      - disable EEE
43928eb6488eSEric Joyner  *     1      - enable EEE
43938eb6488eSEric Joyner  *     (none) - get current device EEE state
43948eb6488eSEric Joyner  ************************************************************************/
43958eb6488eSEric Joyner static int
43968eb6488eSEric Joyner ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS)
43978eb6488eSEric Joyner {
43988eb6488eSEric Joyner 	struct adapter *adapter = (struct adapter *)arg1;
43998eb6488eSEric Joyner 	device_t       dev = adapter->dev;
4400c19c7afeSEric Joyner 	struct ifnet   *ifp = iflib_get_ifp(adapter->ctx);
44018eb6488eSEric Joyner 	int            curr_eee, new_eee, error = 0;
44028eb6488eSEric Joyner 	s32            retval;
44038eb6488eSEric Joyner 
44048eb6488eSEric Joyner 	curr_eee = new_eee = !!(adapter->feat_en & IXGBE_FEATURE_EEE);
44058eb6488eSEric Joyner 
44068eb6488eSEric Joyner 	error = sysctl_handle_int(oidp, &new_eee, 0, req);
44078eb6488eSEric Joyner 	if ((error) || (req->newptr == NULL))
44088eb6488eSEric Joyner 		return (error);
44098eb6488eSEric Joyner 
44108eb6488eSEric Joyner 	/* Nothing to do */
44118eb6488eSEric Joyner 	if (new_eee == curr_eee)
44128eb6488eSEric Joyner 		return (0);
44138eb6488eSEric Joyner 
44148eb6488eSEric Joyner 	/* Not supported */
44158eb6488eSEric Joyner 	if (!(adapter->feat_cap & IXGBE_FEATURE_EEE))
44168eb6488eSEric Joyner 		return (EINVAL);
44178eb6488eSEric Joyner 
44188eb6488eSEric Joyner 	/* Bounds checking */
44198eb6488eSEric Joyner 	if ((new_eee < 0) || (new_eee > 1))
44208eb6488eSEric Joyner 		return (EINVAL);
44218eb6488eSEric Joyner 
44228eb6488eSEric Joyner 	retval = adapter->hw.mac.ops.setup_eee(&adapter->hw, new_eee);
44238eb6488eSEric Joyner 	if (retval) {
44248eb6488eSEric Joyner 		device_printf(dev, "Error in EEE setup: 0x%08X\n", retval);
44258eb6488eSEric Joyner 		return (EINVAL);
44268eb6488eSEric Joyner 	}
44278eb6488eSEric Joyner 
44288eb6488eSEric Joyner 	/* Restart auto-neg */
4429c19c7afeSEric Joyner 	ifp->if_init(ifp);
44308eb6488eSEric Joyner 
44318eb6488eSEric Joyner 	device_printf(dev, "New EEE state: %d\n", new_eee);
44328eb6488eSEric Joyner 
44338eb6488eSEric Joyner 	/* Cache new value */
44348eb6488eSEric Joyner 	if (new_eee)
44358eb6488eSEric Joyner 		adapter->feat_en |= IXGBE_FEATURE_EEE;
44368eb6488eSEric Joyner 	else
44378eb6488eSEric Joyner 		adapter->feat_en &= ~IXGBE_FEATURE_EEE;
44388eb6488eSEric Joyner 
44398eb6488eSEric Joyner 	return (error);
44408eb6488eSEric Joyner } /* ixgbe_sysctl_eee_state */
44418eb6488eSEric Joyner 
44428eb6488eSEric Joyner /************************************************************************
44438eb6488eSEric Joyner  * ixgbe_init_device_features
44448eb6488eSEric Joyner  ************************************************************************/
44458eb6488eSEric Joyner static void
44468eb6488eSEric Joyner ixgbe_init_device_features(struct adapter *adapter)
44478eb6488eSEric Joyner {
44488eb6488eSEric Joyner 	adapter->feat_cap = IXGBE_FEATURE_NETMAP
44498eb6488eSEric Joyner 	                  | IXGBE_FEATURE_RSS
44508eb6488eSEric Joyner 	                  | IXGBE_FEATURE_MSI
44518eb6488eSEric Joyner 	                  | IXGBE_FEATURE_MSIX
4452c19c7afeSEric Joyner 	                  | IXGBE_FEATURE_LEGACY_IRQ;
44538eb6488eSEric Joyner 
44548eb6488eSEric Joyner 	/* Set capabilities first... */
44558eb6488eSEric Joyner 	switch (adapter->hw.mac.type) {
44568eb6488eSEric Joyner 	case ixgbe_mac_82598EB:
44578eb6488eSEric Joyner 		if (adapter->hw.device_id == IXGBE_DEV_ID_82598AT)
44588eb6488eSEric Joyner 			adapter->feat_cap |= IXGBE_FEATURE_FAN_FAIL;
44598eb6488eSEric Joyner 		break;
44608eb6488eSEric Joyner 	case ixgbe_mac_X540:
44618eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
44628eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
44638eb6488eSEric Joyner 		if ((adapter->hw.device_id == IXGBE_DEV_ID_X540_BYPASS) &&
44648eb6488eSEric Joyner 		    (adapter->hw.bus.func == 0))
44658eb6488eSEric Joyner 			adapter->feat_cap |= IXGBE_FEATURE_BYPASS;
44668eb6488eSEric Joyner 		break;
44678eb6488eSEric Joyner 	case ixgbe_mac_X550:
44688eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
44698eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
44708eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
44718eb6488eSEric Joyner 		break;
44728eb6488eSEric Joyner 	case ixgbe_mac_X550EM_x:
44738eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
44748eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
44758eb6488eSEric Joyner 		if (adapter->hw.device_id == IXGBE_DEV_ID_X550EM_X_KR)
44768eb6488eSEric Joyner 			adapter->feat_cap |= IXGBE_FEATURE_EEE;
44778eb6488eSEric Joyner 		break;
44788eb6488eSEric Joyner 	case ixgbe_mac_X550EM_a:
44798eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
44808eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
44818eb6488eSEric Joyner 		adapter->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
44828eb6488eSEric Joyner 		if ((adapter->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T) ||
44838eb6488eSEric Joyner 		    (adapter->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T_L)) {
44848eb6488eSEric Joyner 			adapter->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
44858eb6488eSEric Joyner 			adapter->feat_cap |= IXGBE_FEATURE_EEE;
44868eb6488eSEric Joyner 		}
44878eb6488eSEric Joyner 		break;
44888eb6488eSEric Joyner 	case ixgbe_mac_82599EB:
44898eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
44908eb6488eSEric Joyner 		adapter->feat_cap |= IXGBE_FEATURE_FDIR;
44918eb6488eSEric Joyner 		if ((adapter->hw.device_id == IXGBE_DEV_ID_82599_BYPASS) &&
44928eb6488eSEric Joyner 		    (adapter->hw.bus.func == 0))
44938eb6488eSEric Joyner 			adapter->feat_cap |= IXGBE_FEATURE_BYPASS;
44948eb6488eSEric Joyner 		if (adapter->hw.device_id == IXGBE_DEV_ID_82599_QSFP_SF_QP)
44958eb6488eSEric Joyner 			adapter->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
44968eb6488eSEric Joyner 		break;
44978eb6488eSEric Joyner 	default:
44988eb6488eSEric Joyner 		break;
44998eb6488eSEric Joyner 	}
45008eb6488eSEric Joyner 
45018eb6488eSEric Joyner 	/* Enabled by default... */
45028eb6488eSEric Joyner 	/* Fan failure detection */
45038eb6488eSEric Joyner 	if (adapter->feat_cap & IXGBE_FEATURE_FAN_FAIL)
45048eb6488eSEric Joyner 		adapter->feat_en |= IXGBE_FEATURE_FAN_FAIL;
45058eb6488eSEric Joyner 	/* Netmap */
45068eb6488eSEric Joyner 	if (adapter->feat_cap & IXGBE_FEATURE_NETMAP)
45078eb6488eSEric Joyner 		adapter->feat_en |= IXGBE_FEATURE_NETMAP;
45088eb6488eSEric Joyner 	/* EEE */
45098eb6488eSEric Joyner 	if (adapter->feat_cap & IXGBE_FEATURE_EEE)
45108eb6488eSEric Joyner 		adapter->feat_en |= IXGBE_FEATURE_EEE;
45118eb6488eSEric Joyner 	/* Thermal Sensor */
45128eb6488eSEric Joyner 	if (adapter->feat_cap & IXGBE_FEATURE_TEMP_SENSOR)
45138eb6488eSEric Joyner 		adapter->feat_en |= IXGBE_FEATURE_TEMP_SENSOR;
45148eb6488eSEric Joyner 
45158eb6488eSEric Joyner 	/* Enabled via global sysctl... */
45168eb6488eSEric Joyner 	/* Flow Director */
45178eb6488eSEric Joyner 	if (ixgbe_enable_fdir) {
45188eb6488eSEric Joyner 		if (adapter->feat_cap & IXGBE_FEATURE_FDIR)
45198eb6488eSEric Joyner 			adapter->feat_en |= IXGBE_FEATURE_FDIR;
45208eb6488eSEric Joyner 		else
45218eb6488eSEric Joyner 			device_printf(adapter->dev, "Device does not support Flow Director. Leaving disabled.");
45228eb6488eSEric Joyner 	}
45236f37f232SEric Joyner 	/*
45248eb6488eSEric Joyner 	 * Message Signal Interrupts - Extended (MSI-X)
45258eb6488eSEric Joyner 	 * Normal MSI is only enabled if MSI-X calls fail.
4526758cc3dcSJack F Vogel 	 */
45278eb6488eSEric Joyner 	if (!ixgbe_enable_msix)
45288eb6488eSEric Joyner 		adapter->feat_cap &= ~IXGBE_FEATURE_MSIX;
45298eb6488eSEric Joyner 	/* Receive-Side Scaling (RSS) */
45308eb6488eSEric Joyner 	if ((adapter->feat_cap & IXGBE_FEATURE_RSS) && ixgbe_enable_rss)
45318eb6488eSEric Joyner 		adapter->feat_en |= IXGBE_FEATURE_RSS;
45328eb6488eSEric Joyner 
45338eb6488eSEric Joyner 	/* Disable features with unmet dependencies... */
45348eb6488eSEric Joyner 	/* No MSI-X */
45358eb6488eSEric Joyner 	if (!(adapter->feat_cap & IXGBE_FEATURE_MSIX)) {
45368eb6488eSEric Joyner 		adapter->feat_cap &= ~IXGBE_FEATURE_RSS;
45378eb6488eSEric Joyner 		adapter->feat_cap &= ~IXGBE_FEATURE_SRIOV;
45388eb6488eSEric Joyner 		adapter->feat_en &= ~IXGBE_FEATURE_RSS;
45398eb6488eSEric Joyner 		adapter->feat_en &= ~IXGBE_FEATURE_SRIOV;
45408eb6488eSEric Joyner 	}
45418eb6488eSEric Joyner } /* ixgbe_init_device_features */
45428eb6488eSEric Joyner 
45438eb6488eSEric Joyner /************************************************************************
45448eb6488eSEric Joyner  * ixgbe_check_fan_failure
45458eb6488eSEric Joyner  ************************************************************************/
45468eb6488eSEric Joyner static void
45478eb6488eSEric Joyner ixgbe_check_fan_failure(struct adapter *adapter, u32 reg, bool in_interrupt)
45488eb6488eSEric Joyner {
45498eb6488eSEric Joyner 	u32 mask;
45508eb6488eSEric Joyner 
45518eb6488eSEric Joyner 	mask = (in_interrupt) ? IXGBE_EICR_GPI_SDP1_BY_MAC(&adapter->hw) :
45528eb6488eSEric Joyner 	    IXGBE_ESDP_SDP1;
45538eb6488eSEric Joyner 
45548eb6488eSEric Joyner 	if (reg & mask)
45558eb6488eSEric Joyner 		device_printf(adapter->dev, "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
45568eb6488eSEric Joyner } /* ixgbe_check_fan_failure */
4557