xref: /freebsd-13.1/sys/dev/ixgbe/if_ix.c (revision f3e20eb8)
1 /******************************************************************************
2 
3   Copyright (c) 2001-2017, Intel Corporation
4   All rights reserved.
5 
6   Redistribution and use in source and binary forms, with or without
7   modification, are permitted provided that the following conditions are met:
8 
9    1. Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11 
12    2. Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in the
14       documentation and/or other materials provided with the distribution.
15 
16    3. Neither the name of the Intel Corporation nor the names of its
17       contributors may be used to endorse or promote products derived from
18       this software without specific prior written permission.
19 
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31 
32 ******************************************************************************/
33 /*$FreeBSD$*/
34 
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 #include "opt_rss.h"
38 
39 #include "ixgbe.h"
40 #include "ixgbe_sriov.h"
41 #include "ifdi_if.h"
42 
43 #include <net/netmap.h>
44 #include <dev/netmap/netmap_kern.h>
45 
46 /************************************************************************
47  * Driver version
48  ************************************************************************/
49 char ixgbe_driver_version[] = "4.0.1-k";
50 
51 /************************************************************************
52  * PCI Device ID Table
53  *
54  *   Used by probe to select devices to load on
55  *   Last field stores an index into ixgbe_strings
56  *   Last entry must be all 0s
57  *
58  *   { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
59  ************************************************************************/
60 static pci_vendor_info_t ixgbe_vendor_info_array[] =
61 {
62   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_DUAL_PORT,  "Intel(R) 82598EB AF (Dual Fiber)"),
63   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_SINGLE_PORT,  "Intel(R) 82598EB AF (Fiber)"),
64   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_CX4,  "Intel(R) 82598EB AT (CX4)"),
65   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT,  "Intel(R) 82598EB AT"),
66   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT2,  "Intel(R) 82598EB AT2"),
67   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598,  "Intel(R) 82598"),
68   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_DA_DUAL_PORT,  "Intel(R) 82598EB AF DA (Dual Fiber)"),
69   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_CX4_DUAL_PORT,  "Intel(R) 82598EB AT (Dual CX4)"),
70   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_XF_LR,  "Intel(R) 82598EB AF (Dual Fiber LR)"),
71   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM,  "Intel(R) 82598EB AF (Dual Fiber SR)"),
72   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_SFP_LOM,  "Intel(R) 82598EB LOM"),
73   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4,  "Intel(R) X520 82599 (KX4)"),
74   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4_MEZZ,  "Intel(R) X520 82599 (KX4 Mezzanine)"),
75   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP,  "Intel(R) X520 82599ES (SFI/SFP+)"),
76   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_XAUI_LOM,  "Intel(R) X520 82599 (XAUI/BX4)"),
77   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_CX4,  "Intel(R) X520 82599 (Dual CX4)"),
78   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_T3_LOM,  "Intel(R) X520-T 82599 LOM"),
79   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_COMBO_BACKPLANE,  "Intel(R) X520 82599 (Combined Backplane)"),
80   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BACKPLANE_FCOE,  "Intel(R) X520 82599 (Backplane w/FCoE)"),
81   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF2,  "Intel(R) X520 82599 (Dual SFP+)"),
82   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_FCOE,  "Intel(R) X520 82599 (Dual SFP+ w/FCoE)"),
83   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599EN_SFP,  "Intel(R) X520-1 82599EN (SFP+)"),
84   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF_QP,  "Intel(R) X520-4 82599 (Quad SFP+)"),
85   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_QSFP_SF_QP,  "Intel(R) X520-Q1 82599 (QSFP+)"),
86   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T,  "Intel(R) X540-AT2"),
87   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T1,  "Intel(R) X540-T1"),
88   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T,  "Intel(R) X550-T2"),
89   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T1, "Intel(R) X550-T1"),
90   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KR,  "Intel(R) X552 (KR Backplane)"),
91   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KX4,  "Intel(R) X552 (KX4 Backplane)"),
92   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_10G_T,  "Intel(R) X552/X557-AT (10GBASE-T)"),
93   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_1G_T,  "Intel(R) X552 (1000BASE-T)"),
94   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_SFP, "Intel(R) X552 (SFP+)"),
95   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR, "Intel(R) X553 (KR Backplane)"),
96   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR_L, "Intel(R) X553 L (KR Backplane)"),
97   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP, "Intel(R) X553 (SFP+)"),
98   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP_N, "Intel(R) X553 N (SFP+)"),
99   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII, "Intel(R) X553 (1GbE SGMII)"),
100   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII_L, "Intel(R) X553 L (1GbE SGMII)"),
101   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_10G_T, "Intel(R) X553/X557-AT (10GBASE-T)"),
102   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T, "Intel(R) X553 (1GbE)"),
103   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T_L, "Intel(R) X553 L (1GbE)"),
104   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540_BYPASS, "Intel(R) X540-T2 (Bypass)"),
105   PVID(IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BYPASS, "Intel(R) X520 82599 (Bypass)"),
106 	/* required last entry */
107   PVID_END
108 };
109 
110 static void *ixgbe_register(device_t);
111 static int  ixgbe_if_attach_pre(if_ctx_t);
112 static int  ixgbe_if_attach_post(if_ctx_t);
113 static int  ixgbe_if_detach(if_ctx_t);
114 static int  ixgbe_if_shutdown(if_ctx_t);
115 static int  ixgbe_if_suspend(if_ctx_t);
116 static int  ixgbe_if_resume(if_ctx_t);
117 
118 static void ixgbe_if_stop(if_ctx_t);
119 void ixgbe_if_enable_intr(if_ctx_t);
120 static void ixgbe_if_disable_intr(if_ctx_t);
121 static void ixgbe_link_intr_enable(if_ctx_t);
122 static int  ixgbe_if_rx_queue_intr_enable(if_ctx_t, uint16_t);
123 static void ixgbe_if_media_status(if_ctx_t, struct ifmediareq *);
124 static int  ixgbe_if_media_change(if_ctx_t);
125 static int  ixgbe_if_msix_intr_assign(if_ctx_t, int);
126 static int  ixgbe_if_mtu_set(if_ctx_t, uint32_t);
127 static void ixgbe_if_crcstrip_set(if_ctx_t, int, int);
128 static void ixgbe_if_multi_set(if_ctx_t);
129 static int  ixgbe_if_promisc_set(if_ctx_t, int);
130 static int  ixgbe_if_tx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
131 static int  ixgbe_if_rx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
132 static void ixgbe_if_queues_free(if_ctx_t);
133 static void ixgbe_if_timer(if_ctx_t, uint16_t);
134 static void ixgbe_if_update_admin_status(if_ctx_t);
135 static void ixgbe_if_vlan_register(if_ctx_t, u16);
136 static void ixgbe_if_vlan_unregister(if_ctx_t, u16);
137 static int  ixgbe_if_i2c_req(if_ctx_t, struct ifi2creq *);
138 static bool ixgbe_if_needs_restart(if_ctx_t, enum iflib_restart_event);
139 int ixgbe_intr(void *);
140 
141 /************************************************************************
142  * Function prototypes
143  ************************************************************************/
144 static uint64_t ixgbe_if_get_counter(if_ctx_t, ift_counter);
145 
146 static void ixgbe_enable_queue(struct ixgbe_softc *, u32);
147 static void ixgbe_disable_queue(struct ixgbe_softc *, u32);
148 static void ixgbe_add_device_sysctls(if_ctx_t);
149 static int  ixgbe_allocate_pci_resources(if_ctx_t);
150 static int  ixgbe_setup_low_power_mode(if_ctx_t);
151 
152 static void ixgbe_config_dmac(struct ixgbe_softc *);
153 static void ixgbe_configure_ivars(struct ixgbe_softc *);
154 static void ixgbe_set_ivar(struct ixgbe_softc *, u8, u8, s8);
155 static u8   *ixgbe_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
156 static bool ixgbe_sfp_probe(if_ctx_t);
157 
158 static void ixgbe_free_pci_resources(if_ctx_t);
159 
160 static int  ixgbe_msix_link(void *);
161 static int  ixgbe_msix_que(void *);
162 static void ixgbe_initialize_rss_mapping(struct ixgbe_softc *);
163 static void ixgbe_initialize_receive_units(if_ctx_t);
164 static void ixgbe_initialize_transmit_units(if_ctx_t);
165 
166 static int  ixgbe_setup_interface(if_ctx_t);
167 static void ixgbe_init_device_features(struct ixgbe_softc *);
168 static void ixgbe_check_fan_failure(struct ixgbe_softc *, u32, bool);
169 static void ixgbe_sbuf_fw_version(struct ixgbe_hw *, struct sbuf *);
170 static void ixgbe_print_fw_version(if_ctx_t);
171 static void ixgbe_add_media_types(if_ctx_t);
172 static void ixgbe_update_stats_counters(struct ixgbe_softc *);
173 static void ixgbe_config_link(if_ctx_t);
174 static void ixgbe_get_slot_info(struct ixgbe_softc *);
175 static void ixgbe_check_wol_support(struct ixgbe_softc *);
176 static void ixgbe_enable_rx_drop(struct ixgbe_softc *);
177 static void ixgbe_disable_rx_drop(struct ixgbe_softc *);
178 
179 static void ixgbe_add_hw_stats(struct ixgbe_softc *);
180 static int  ixgbe_set_flowcntl(struct ixgbe_softc *, int);
181 static int  ixgbe_set_advertise(struct ixgbe_softc *, int);
182 static int  ixgbe_get_default_advertise(struct ixgbe_softc *);
183 static void ixgbe_setup_vlan_hw_support(if_ctx_t);
184 static void ixgbe_config_gpie(struct ixgbe_softc *);
185 static void ixgbe_config_delay_values(struct ixgbe_softc *);
186 
187 /* Sysctl handlers */
188 static int  ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS);
189 static int  ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS);
190 static int  ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS);
191 static int  ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS);
192 static int  ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS);
193 static int  ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS);
194 static int  ixgbe_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS);
195 #ifdef IXGBE_DEBUG
196 static int  ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS);
197 static int  ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS);
198 #endif
199 static int  ixgbe_sysctl_rdh_handler(SYSCTL_HANDLER_ARGS);
200 static int  ixgbe_sysctl_rdt_handler(SYSCTL_HANDLER_ARGS);
201 static int  ixgbe_sysctl_tdt_handler(SYSCTL_HANDLER_ARGS);
202 static int  ixgbe_sysctl_tdh_handler(SYSCTL_HANDLER_ARGS);
203 static int  ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS);
204 static int  ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS);
205 static int  ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS);
206 
207 /* Deferred interrupt tasklets */
208 static void ixgbe_handle_msf(void *);
209 static void ixgbe_handle_mod(void *);
210 static void ixgbe_handle_phy(void *);
211 
212 /************************************************************************
213  *  FreeBSD Device Interface Entry Points
214  ************************************************************************/
215 static device_method_t ix_methods[] = {
216 	/* Device interface */
217 	DEVMETHOD(device_register, ixgbe_register),
218 	DEVMETHOD(device_probe, iflib_device_probe),
219 	DEVMETHOD(device_attach, iflib_device_attach),
220 	DEVMETHOD(device_detach, iflib_device_detach),
221 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
222 	DEVMETHOD(device_suspend, iflib_device_suspend),
223 	DEVMETHOD(device_resume, iflib_device_resume),
224 #ifdef PCI_IOV
225 	DEVMETHOD(pci_iov_init, iflib_device_iov_init),
226 	DEVMETHOD(pci_iov_uninit, iflib_device_iov_uninit),
227 	DEVMETHOD(pci_iov_add_vf, iflib_device_iov_add_vf),
228 #endif /* PCI_IOV */
229 	DEVMETHOD_END
230 };
231 
232 static driver_t ix_driver = {
233 	"ix", ix_methods, sizeof(struct ixgbe_softc),
234 };
235 
236 devclass_t ix_devclass;
237 DRIVER_MODULE(ix, pci, ix_driver, ix_devclass, 0, 0);
238 IFLIB_PNP_INFO(pci, ix_driver, ixgbe_vendor_info_array);
239 MODULE_DEPEND(ix, pci, 1, 1, 1);
240 MODULE_DEPEND(ix, ether, 1, 1, 1);
241 MODULE_DEPEND(ix, iflib, 1, 1, 1);
242 
243 static device_method_t ixgbe_if_methods[] = {
244 	DEVMETHOD(ifdi_attach_pre, ixgbe_if_attach_pre),
245 	DEVMETHOD(ifdi_attach_post, ixgbe_if_attach_post),
246 	DEVMETHOD(ifdi_detach, ixgbe_if_detach),
247 	DEVMETHOD(ifdi_shutdown, ixgbe_if_shutdown),
248 	DEVMETHOD(ifdi_suspend, ixgbe_if_suspend),
249 	DEVMETHOD(ifdi_resume, ixgbe_if_resume),
250 	DEVMETHOD(ifdi_init, ixgbe_if_init),
251 	DEVMETHOD(ifdi_stop, ixgbe_if_stop),
252 	DEVMETHOD(ifdi_msix_intr_assign, ixgbe_if_msix_intr_assign),
253 	DEVMETHOD(ifdi_intr_enable, ixgbe_if_enable_intr),
254 	DEVMETHOD(ifdi_intr_disable, ixgbe_if_disable_intr),
255 	DEVMETHOD(ifdi_link_intr_enable, ixgbe_link_intr_enable),
256 	DEVMETHOD(ifdi_tx_queue_intr_enable, ixgbe_if_rx_queue_intr_enable),
257 	DEVMETHOD(ifdi_rx_queue_intr_enable, ixgbe_if_rx_queue_intr_enable),
258 	DEVMETHOD(ifdi_tx_queues_alloc, ixgbe_if_tx_queues_alloc),
259 	DEVMETHOD(ifdi_rx_queues_alloc, ixgbe_if_rx_queues_alloc),
260 	DEVMETHOD(ifdi_queues_free, ixgbe_if_queues_free),
261 	DEVMETHOD(ifdi_update_admin_status, ixgbe_if_update_admin_status),
262 	DEVMETHOD(ifdi_multi_set, ixgbe_if_multi_set),
263 	DEVMETHOD(ifdi_mtu_set, ixgbe_if_mtu_set),
264 	DEVMETHOD(ifdi_crcstrip_set, ixgbe_if_crcstrip_set),
265 	DEVMETHOD(ifdi_media_status, ixgbe_if_media_status),
266 	DEVMETHOD(ifdi_media_change, ixgbe_if_media_change),
267 	DEVMETHOD(ifdi_promisc_set, ixgbe_if_promisc_set),
268 	DEVMETHOD(ifdi_timer, ixgbe_if_timer),
269 	DEVMETHOD(ifdi_vlan_register, ixgbe_if_vlan_register),
270 	DEVMETHOD(ifdi_vlan_unregister, ixgbe_if_vlan_unregister),
271 	DEVMETHOD(ifdi_get_counter, ixgbe_if_get_counter),
272 	DEVMETHOD(ifdi_i2c_req, ixgbe_if_i2c_req),
273 	DEVMETHOD(ifdi_needs_restart, ixgbe_if_needs_restart),
274 #ifdef PCI_IOV
275 	DEVMETHOD(ifdi_iov_init, ixgbe_if_iov_init),
276 	DEVMETHOD(ifdi_iov_uninit, ixgbe_if_iov_uninit),
277 	DEVMETHOD(ifdi_iov_vf_add, ixgbe_if_iov_vf_add),
278 #endif /* PCI_IOV */
279 	DEVMETHOD_END
280 };
281 
282 /*
283  * TUNEABLE PARAMETERS:
284  */
285 
286 static SYSCTL_NODE(_hw, OID_AUTO, ix, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
287     "IXGBE driver parameters");
288 static driver_t ixgbe_if_driver = {
289   "ixgbe_if", ixgbe_if_methods, sizeof(struct ixgbe_softc)
290 };
291 
292 static int ixgbe_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
293 SYSCTL_INT(_hw_ix, OID_AUTO, max_interrupt_rate, CTLFLAG_RDTUN,
294     &ixgbe_max_interrupt_rate, 0, "Maximum interrupts per second");
295 
296 /* Flow control setting, default to full */
297 static int ixgbe_flow_control = ixgbe_fc_full;
298 SYSCTL_INT(_hw_ix, OID_AUTO, flow_control, CTLFLAG_RDTUN,
299     &ixgbe_flow_control, 0, "Default flow control used for all adapters");
300 
301 /* Advertise Speed, default to 0 (auto) */
302 static int ixgbe_advertise_speed = 0;
303 SYSCTL_INT(_hw_ix, OID_AUTO, advertise_speed, CTLFLAG_RDTUN,
304     &ixgbe_advertise_speed, 0, "Default advertised speed for all adapters");
305 
306 /*
307  * Smart speed setting, default to on
308  * this only works as a compile option
309  * right now as its during attach, set
310  * this to 'ixgbe_smart_speed_off' to
311  * disable.
312  */
313 static int ixgbe_smart_speed = ixgbe_smart_speed_on;
314 
315 /*
316  * MSI-X should be the default for best performance,
317  * but this allows it to be forced off for testing.
318  */
319 static int ixgbe_enable_msix = 1;
320 SYSCTL_INT(_hw_ix, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &ixgbe_enable_msix, 0,
321     "Enable MSI-X interrupts");
322 
323 /*
324  * Defining this on will allow the use
325  * of unsupported SFP+ modules, note that
326  * doing so you are on your own :)
327  */
328 static int allow_unsupported_sfp = false;
329 SYSCTL_INT(_hw_ix, OID_AUTO, unsupported_sfp, CTLFLAG_RDTUN,
330     &allow_unsupported_sfp, 0,
331     "Allow unsupported SFP modules...use at your own risk");
332 
333 /*
334  * Not sure if Flow Director is fully baked,
335  * so we'll default to turning it off.
336  */
337 static int ixgbe_enable_fdir = 0;
338 SYSCTL_INT(_hw_ix, OID_AUTO, enable_fdir, CTLFLAG_RDTUN, &ixgbe_enable_fdir, 0,
339     "Enable Flow Director");
340 
341 /* Receive-Side Scaling */
342 static int ixgbe_enable_rss = 1;
343 SYSCTL_INT(_hw_ix, OID_AUTO, enable_rss, CTLFLAG_RDTUN, &ixgbe_enable_rss, 0,
344     "Enable Receive-Side Scaling (RSS)");
345 
346 /*
347  * AIM: Adaptive Interrupt Moderation
348  * which means that the interrupt rate
349  * is varied over time based on the
350  * traffic for that interrupt vector
351  */
352 static int ixgbe_enable_aim = false;
353 SYSCTL_INT(_hw_ix, OID_AUTO, enable_aim, CTLFLAG_RWTUN, &ixgbe_enable_aim, 0,
354     "Enable adaptive interrupt moderation");
355 
356 #if 0
357 /* Keep running tab on them for sanity check */
358 static int ixgbe_total_ports;
359 #endif
360 
361 MALLOC_DEFINE(M_IXGBE, "ix", "ix driver allocations");
362 
363 /*
364  * For Flow Director: this is the number of TX packets we sample
365  * for the filter pool, this means every 20th packet will be probed.
366  *
367  * This feature can be disabled by setting this to 0.
368  */
369 static int atr_sample_rate = 20;
370 
371 extern struct if_txrx ixgbe_txrx;
372 
373 static struct if_shared_ctx ixgbe_sctx_init = {
374 	.isc_magic = IFLIB_MAGIC,
375 	.isc_q_align = PAGE_SIZE,/* max(DBA_ALIGN, PAGE_SIZE) */
376 	.isc_tx_maxsize = IXGBE_TSO_SIZE + sizeof(struct ether_vlan_header),
377 	.isc_tx_maxsegsize = PAGE_SIZE,
378 	.isc_tso_maxsize = IXGBE_TSO_SIZE + sizeof(struct ether_vlan_header),
379 	.isc_tso_maxsegsize = PAGE_SIZE,
380 	.isc_rx_maxsize = PAGE_SIZE*4,
381 	.isc_rx_nsegments = 1,
382 	.isc_rx_maxsegsize = PAGE_SIZE*4,
383 	.isc_nfl = 1,
384 	.isc_ntxqs = 1,
385 	.isc_nrxqs = 1,
386 
387 	.isc_admin_intrcnt = 1,
388 	.isc_vendor_info = ixgbe_vendor_info_array,
389 	.isc_driver_version = ixgbe_driver_version,
390 	.isc_driver = &ixgbe_if_driver,
391 	.isc_flags = IFLIB_TSO_INIT_IP,
392 
393 	.isc_nrxd_min = {MIN_RXD},
394 	.isc_ntxd_min = {MIN_TXD},
395 	.isc_nrxd_max = {MAX_RXD},
396 	.isc_ntxd_max = {MAX_TXD},
397 	.isc_nrxd_default = {DEFAULT_RXD},
398 	.isc_ntxd_default = {DEFAULT_TXD},
399 };
400 
401 /************************************************************************
402  * ixgbe_if_tx_queues_alloc
403  ************************************************************************/
404 static int
ixgbe_if_tx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int ntxqs,int ntxqsets)405 ixgbe_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
406     int ntxqs, int ntxqsets)
407 {
408 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
409 	if_softc_ctx_t     scctx = sc->shared;
410 	struct ix_tx_queue *que;
411 	int                i, j, error;
412 
413 	MPASS(sc->num_tx_queues > 0);
414 	MPASS(sc->num_tx_queues == ntxqsets);
415 	MPASS(ntxqs == 1);
416 
417 	/* Allocate queue structure memory */
418 	sc->tx_queues =
419 	    (struct ix_tx_queue *)malloc(sizeof(struct ix_tx_queue) * ntxqsets,
420 	                                 M_IXGBE, M_NOWAIT | M_ZERO);
421 	if (!sc->tx_queues) {
422 		device_printf(iflib_get_dev(ctx),
423 		    "Unable to allocate TX ring memory\n");
424 		return (ENOMEM);
425 	}
426 
427 	for (i = 0, que = sc->tx_queues; i < ntxqsets; i++, que++) {
428 		struct tx_ring *txr = &que->txr;
429 
430 		/* In case SR-IOV is enabled, align the index properly */
431 		txr->me = ixgbe_vf_que_index(sc->iov_mode, sc->pool,
432 		    i);
433 
434 		txr->sc = que->sc = sc;
435 
436 		/* Allocate report status array */
437 		txr->tx_rsq = (qidx_t *)malloc(sizeof(qidx_t) * scctx->isc_ntxd[0], M_IXGBE, M_NOWAIT | M_ZERO);
438 		if (txr->tx_rsq == NULL) {
439 			error = ENOMEM;
440 			goto fail;
441 		}
442 		for (j = 0; j < scctx->isc_ntxd[0]; j++)
443 			txr->tx_rsq[j] = QIDX_INVALID;
444 		/* get the virtual and physical address of the hardware queues */
445 		txr->tail = IXGBE_TDT(txr->me);
446 		txr->tx_base = (union ixgbe_adv_tx_desc *)vaddrs[i];
447 		txr->tx_paddr = paddrs[i];
448 
449 		txr->bytes = 0;
450 		txr->total_packets = 0;
451 
452 		/* Set the rate at which we sample packets */
453 		if (sc->feat_en & IXGBE_FEATURE_FDIR)
454 			txr->atr_sample = atr_sample_rate;
455 
456 	}
457 
458 	device_printf(iflib_get_dev(ctx), "allocated for %d queues\n",
459 	    sc->num_tx_queues);
460 
461 	return (0);
462 
463 fail:
464 	ixgbe_if_queues_free(ctx);
465 
466 	return (error);
467 } /* ixgbe_if_tx_queues_alloc */
468 
469 /************************************************************************
470  * ixgbe_if_rx_queues_alloc
471  ************************************************************************/
472 static int
ixgbe_if_rx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int nrxqs,int nrxqsets)473 ixgbe_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
474     int nrxqs, int nrxqsets)
475 {
476 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
477 	struct ix_rx_queue *que;
478 	int                i;
479 
480 	MPASS(sc->num_rx_queues > 0);
481 	MPASS(sc->num_rx_queues == nrxqsets);
482 	MPASS(nrxqs == 1);
483 
484 	/* Allocate queue structure memory */
485 	sc->rx_queues =
486 	    (struct ix_rx_queue *)malloc(sizeof(struct ix_rx_queue)*nrxqsets,
487 	                                 M_IXGBE, M_NOWAIT | M_ZERO);
488 	if (!sc->rx_queues) {
489 		device_printf(iflib_get_dev(ctx),
490 		    "Unable to allocate TX ring memory\n");
491 		return (ENOMEM);
492 	}
493 
494 	for (i = 0, que = sc->rx_queues; i < nrxqsets; i++, que++) {
495 		struct rx_ring *rxr = &que->rxr;
496 
497 		/* In case SR-IOV is enabled, align the index properly */
498 		rxr->me = ixgbe_vf_que_index(sc->iov_mode, sc->pool,
499 		    i);
500 
501 		rxr->sc = que->sc = sc;
502 
503 		/* get the virtual and physical address of the hw queues */
504 		rxr->tail = IXGBE_RDT(rxr->me);
505 		rxr->rx_base = (union ixgbe_adv_rx_desc *)vaddrs[i];
506 		rxr->rx_paddr = paddrs[i];
507 		rxr->bytes = 0;
508 		rxr->que = que;
509 	}
510 
511 	device_printf(iflib_get_dev(ctx), "allocated for %d rx queues\n",
512 	    sc->num_rx_queues);
513 
514 	return (0);
515 } /* ixgbe_if_rx_queues_alloc */
516 
517 /************************************************************************
518  * ixgbe_if_queues_free
519  ************************************************************************/
520 static void
ixgbe_if_queues_free(if_ctx_t ctx)521 ixgbe_if_queues_free(if_ctx_t ctx)
522 {
523 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
524 	struct ix_tx_queue *tx_que = sc->tx_queues;
525 	struct ix_rx_queue *rx_que = sc->rx_queues;
526 	int                i;
527 
528 	if (tx_que != NULL) {
529 		for (i = 0; i < sc->num_tx_queues; i++, tx_que++) {
530 			struct tx_ring *txr = &tx_que->txr;
531 			if (txr->tx_rsq == NULL)
532 				break;
533 
534 			free(txr->tx_rsq, M_IXGBE);
535 			txr->tx_rsq = NULL;
536 		}
537 
538 		free(sc->tx_queues, M_IXGBE);
539 		sc->tx_queues = NULL;
540 	}
541 	if (rx_que != NULL) {
542 		free(sc->rx_queues, M_IXGBE);
543 		sc->rx_queues = NULL;
544 	}
545 } /* ixgbe_if_queues_free */
546 
547 /************************************************************************
548  * ixgbe_initialize_rss_mapping
549  ************************************************************************/
550 static void
ixgbe_initialize_rss_mapping(struct ixgbe_softc * sc)551 ixgbe_initialize_rss_mapping(struct ixgbe_softc *sc)
552 {
553 	struct ixgbe_hw *hw = &sc->hw;
554 	u32             reta = 0, mrqc, rss_key[10];
555 	int             queue_id, table_size, index_mult;
556 	int             i, j;
557 	u32             rss_hash_config;
558 
559 	if (sc->feat_en & IXGBE_FEATURE_RSS) {
560 		/* Fetch the configured RSS key */
561 		rss_getkey((uint8_t *)&rss_key);
562 	} else {
563 		/* set up random bits */
564 		arc4rand(&rss_key, sizeof(rss_key), 0);
565 	}
566 
567 	/* Set multiplier for RETA setup and table size based on MAC */
568 	index_mult = 0x1;
569 	table_size = 128;
570 	switch (sc->hw.mac.type) {
571 	case ixgbe_mac_82598EB:
572 		index_mult = 0x11;
573 		break;
574 	case ixgbe_mac_X550:
575 	case ixgbe_mac_X550EM_x:
576 	case ixgbe_mac_X550EM_a:
577 		table_size = 512;
578 		break;
579 	default:
580 		break;
581 	}
582 
583 	/* Set up the redirection table */
584 	for (i = 0, j = 0; i < table_size; i++, j++) {
585 		if (j == sc->num_rx_queues)
586 			j = 0;
587 
588 		if (sc->feat_en & IXGBE_FEATURE_RSS) {
589 			/*
590 			 * Fetch the RSS bucket id for the given indirection
591 			 * entry. Cap it at the number of configured buckets
592 			 * (which is num_rx_queues.)
593 			 */
594 			queue_id = rss_get_indirection_to_bucket(i);
595 			queue_id = queue_id % sc->num_rx_queues;
596 		} else
597 			queue_id = (j * index_mult);
598 
599 		/*
600 		 * The low 8 bits are for hash value (n+0);
601 		 * The next 8 bits are for hash value (n+1), etc.
602 		 */
603 		reta = reta >> 8;
604 		reta = reta | (((uint32_t)queue_id) << 24);
605 		if ((i & 3) == 3) {
606 			if (i < 128)
607 				IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
608 			else
609 				IXGBE_WRITE_REG(hw, IXGBE_ERETA((i >> 2) - 32),
610 				    reta);
611 			reta = 0;
612 		}
613 	}
614 
615 	/* Now fill our hash function seeds */
616 	for (i = 0; i < 10; i++)
617 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
618 
619 	/* Perform hash on these packet types */
620 	if (sc->feat_en & IXGBE_FEATURE_RSS)
621 		rss_hash_config = rss_gethashconfig();
622 	else {
623 		/*
624 		 * Disable UDP - IP fragments aren't currently being handled
625 		 * and so we end up with a mix of 2-tuple and 4-tuple
626 		 * traffic.
627 		 */
628 		rss_hash_config = RSS_HASHTYPE_RSS_IPV4
629 		                | RSS_HASHTYPE_RSS_TCP_IPV4
630 		                | RSS_HASHTYPE_RSS_IPV6
631 		                | RSS_HASHTYPE_RSS_TCP_IPV6
632 		                | RSS_HASHTYPE_RSS_IPV6_EX
633 		                | RSS_HASHTYPE_RSS_TCP_IPV6_EX;
634 	}
635 
636 	mrqc = IXGBE_MRQC_RSSEN;
637 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
638 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
639 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
640 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
641 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
642 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
643 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
644 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
645 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
646 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
647 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
648 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
649 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
650 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
651 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
652 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
653 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
654 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
655 	mrqc |= ixgbe_get_mrqc(sc->iov_mode);
656 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
657 } /* ixgbe_initialize_rss_mapping */
658 
659 /************************************************************************
660  * ixgbe_initialize_receive_units - Setup receive registers and features.
661  ************************************************************************/
662 #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
663 
664 static void
ixgbe_initialize_receive_units(if_ctx_t ctx)665 ixgbe_initialize_receive_units(if_ctx_t ctx)
666 {
667 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
668 	if_softc_ctx_t     scctx = sc->shared;
669 	struct ixgbe_hw    *hw = &sc->hw;
670 	struct ifnet       *ifp = iflib_get_ifp(ctx);
671 	struct ix_rx_queue *que;
672 	int                i, j;
673 	u32                bufsz, fctrl, srrctl, rxcsum;
674 	u32                hlreg;
675 
676 	/*
677 	 * Make sure receives are disabled while
678 	 * setting up the descriptor ring
679 	 */
680 	ixgbe_disable_rx(hw);
681 
682 	/* Enable broadcasts */
683 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
684 	fctrl |= IXGBE_FCTRL_BAM;
685 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
686 		fctrl |= IXGBE_FCTRL_DPF;
687 		fctrl |= IXGBE_FCTRL_PMCF;
688 	}
689 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
690 
691 	/* Set for Jumbo Frames? */
692 	hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
693 	if (ifp->if_mtu > ETHERMTU)
694 		hlreg |= IXGBE_HLREG0_JUMBOEN;
695 	else
696 		hlreg &= ~IXGBE_HLREG0_JUMBOEN;
697 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
698 
699 	bufsz = (sc->rx_mbuf_sz + BSIZEPKT_ROUNDUP) >>
700 	    IXGBE_SRRCTL_BSIZEPKT_SHIFT;
701 
702 	/* Setup the Base and Length of the Rx Descriptor Ring */
703 	for (i = 0, que = sc->rx_queues; i < sc->num_rx_queues; i++, que++) {
704 		struct rx_ring *rxr = &que->rxr;
705 		u64            rdba = rxr->rx_paddr;
706 
707 		j = rxr->me;
708 
709 		/* Setup the Base and Length of the Rx Descriptor Ring */
710 		IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j),
711 		    (rdba & 0x00000000ffffffffULL));
712 		IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
713 		IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j),
714 		     scctx->isc_nrxd[0] * sizeof(union ixgbe_adv_rx_desc));
715 
716 		/* Set up the SRRCTL register */
717 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(j));
718 		srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
719 		srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
720 		srrctl |= bufsz;
721 		srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
722 
723 		/*
724 		 * Set DROP_EN iff we have no flow control and >1 queue.
725 		 * Note that srrctl was cleared shortly before during reset,
726 		 * so we do not need to clear the bit, but do it just in case
727 		 * this code is moved elsewhere.
728 		 */
729 		if (sc->num_rx_queues > 1 &&
730 		    sc->hw.fc.requested_mode == ixgbe_fc_none) {
731 			srrctl |= IXGBE_SRRCTL_DROP_EN;
732 		} else {
733 			srrctl &= ~IXGBE_SRRCTL_DROP_EN;
734 		}
735 
736 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(j), srrctl);
737 
738 		/* Setup the HW Rx Head and Tail Descriptor Pointers */
739 		IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
740 		IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
741 
742 		/* Set the driver rx tail address */
743 		rxr->tail =  IXGBE_RDT(rxr->me);
744 	}
745 
746 	if (sc->hw.mac.type != ixgbe_mac_82598EB) {
747 		u32 psrtype = IXGBE_PSRTYPE_TCPHDR
748 		            | IXGBE_PSRTYPE_UDPHDR
749 		            | IXGBE_PSRTYPE_IPV4HDR
750 		            | IXGBE_PSRTYPE_IPV6HDR;
751 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
752 	}
753 
754 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
755 
756 	ixgbe_initialize_rss_mapping(sc);
757 
758 	if (sc->num_rx_queues > 1) {
759 		/* RSS and RX IPP Checksum are mutually exclusive */
760 		rxcsum |= IXGBE_RXCSUM_PCSD;
761 	}
762 
763 	if (ifp->if_capenable & IFCAP_RXCSUM)
764 		rxcsum |= IXGBE_RXCSUM_PCSD;
765 
766 	/* This is useful for calculating UDP/IP fragment checksums */
767 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
768 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
769 
770 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
771 
772 } /* ixgbe_initialize_receive_units */
773 
774 /************************************************************************
775  * ixgbe_initialize_transmit_units - Enable transmit units.
776  ************************************************************************/
777 static void
ixgbe_initialize_transmit_units(if_ctx_t ctx)778 ixgbe_initialize_transmit_units(if_ctx_t ctx)
779 {
780 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
781 	struct ixgbe_hw    *hw = &sc->hw;
782 	if_softc_ctx_t     scctx = sc->shared;
783 	struct ix_tx_queue *que;
784 	int i;
785 
786 	/* Setup the Base and Length of the Tx Descriptor Ring */
787 	for (i = 0, que = sc->tx_queues; i < sc->num_tx_queues;
788 	    i++, que++) {
789 		struct tx_ring	   *txr = &que->txr;
790 		u64 tdba = txr->tx_paddr;
791 		u32 txctrl = 0;
792 		int j = txr->me;
793 
794 		IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
795 		    (tdba & 0x00000000ffffffffULL));
796 		IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
797 		IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j),
798 		    scctx->isc_ntxd[0] * sizeof(union ixgbe_adv_tx_desc));
799 
800 		/* Setup the HW Tx Head and Tail descriptor pointers */
801 		IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
802 		IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
803 
804 		/* Cache the tail address */
805 		txr->tail = IXGBE_TDT(txr->me);
806 
807 		txr->tx_rs_cidx = txr->tx_rs_pidx;
808 		txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
809 		for (int k = 0; k < scctx->isc_ntxd[0]; k++)
810 			txr->tx_rsq[k] = QIDX_INVALID;
811 
812 		/* Disable Head Writeback */
813 		/*
814 		 * Note: for X550 series devices, these registers are actually
815 		 * prefixed with TPH_ isntead of DCA_, but the addresses and
816 		 * fields remain the same.
817 		 */
818 		switch (hw->mac.type) {
819 		case ixgbe_mac_82598EB:
820 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
821 			break;
822 		default:
823 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(j));
824 			break;
825 		}
826 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
827 		switch (hw->mac.type) {
828 		case ixgbe_mac_82598EB:
829 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
830 			break;
831 		default:
832 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(j), txctrl);
833 			break;
834 		}
835 
836 	}
837 
838 	if (hw->mac.type != ixgbe_mac_82598EB) {
839 		u32 dmatxctl, rttdcs;
840 
841 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
842 		dmatxctl |= IXGBE_DMATXCTL_TE;
843 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
844 		/* Disable arbiter to set MTQC */
845 		rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
846 		rttdcs |= IXGBE_RTTDCS_ARBDIS;
847 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
848 		IXGBE_WRITE_REG(hw, IXGBE_MTQC,
849 		    ixgbe_get_mtqc(sc->iov_mode));
850 		rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
851 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
852 	}
853 
854 } /* ixgbe_initialize_transmit_units */
855 
856 /************************************************************************
857  * ixgbe_register
858  ************************************************************************/
859 static void *
ixgbe_register(device_t dev)860 ixgbe_register(device_t dev)
861 {
862 	return (&ixgbe_sctx_init);
863 } /* ixgbe_register */
864 
865 /************************************************************************
866  * ixgbe_if_attach_pre - Device initialization routine, part 1
867  *
868  *   Called when the driver is being loaded.
869  *   Identifies the type of hardware, initializes the hardware,
870  *   and initializes iflib structures.
871  *
872  *   return 0 on success, positive on failure
873  ************************************************************************/
874 static int
ixgbe_if_attach_pre(if_ctx_t ctx)875 ixgbe_if_attach_pre(if_ctx_t ctx)
876 {
877 	struct ixgbe_softc  *sc;
878 	device_t        dev;
879 	if_softc_ctx_t  scctx;
880 	struct ixgbe_hw *hw;
881 	int             error = 0;
882 	u32             ctrl_ext;
883 
884 	INIT_DEBUGOUT("ixgbe_attach: begin");
885 
886 	/* Allocate, clear, and link in our adapter structure */
887 	dev = iflib_get_dev(ctx);
888 	sc = iflib_get_softc(ctx);
889 	sc->hw.back = sc;
890 	sc->ctx = ctx;
891 	sc->dev = dev;
892 	scctx = sc->shared = iflib_get_softc_ctx(ctx);
893 	sc->media = iflib_get_media(ctx);
894 	hw = &sc->hw;
895 
896 	/* Determine hardware revision */
897 	hw->vendor_id = pci_get_vendor(dev);
898 	hw->device_id = pci_get_device(dev);
899 	hw->revision_id = pci_get_revid(dev);
900 	hw->subsystem_vendor_id = pci_get_subvendor(dev);
901 	hw->subsystem_device_id = pci_get_subdevice(dev);
902 
903 	/* Do base PCI setup - map BAR0 */
904 	if (ixgbe_allocate_pci_resources(ctx)) {
905 		device_printf(dev, "Allocation of PCI resources failed\n");
906 		return (ENXIO);
907 	}
908 
909 	/* let hardware know driver is loaded */
910 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
911 	ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
912 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
913 
914 	/*
915 	 * Initialize the shared code
916 	 */
917 	if (ixgbe_init_shared_code(hw) != 0) {
918 		device_printf(dev, "Unable to initialize the shared code\n");
919 		error = ENXIO;
920 		goto err_pci;
921 	}
922 
923 	if (hw->mbx.ops.init_params)
924 		hw->mbx.ops.init_params(hw);
925 
926 	hw->allow_unsupported_sfp = allow_unsupported_sfp;
927 
928 	if (hw->mac.type != ixgbe_mac_82598EB)
929 		hw->phy.smart_speed = ixgbe_smart_speed;
930 
931 	ixgbe_init_device_features(sc);
932 
933 	/* Enable WoL (if supported) */
934 	ixgbe_check_wol_support(sc);
935 
936 	/* Verify adapter fan is still functional (if applicable) */
937 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL) {
938 		u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
939 		ixgbe_check_fan_failure(sc, esdp, false);
940 	}
941 
942 	/* Ensure SW/FW semaphore is free */
943 	ixgbe_init_swfw_semaphore(hw);
944 
945 	/* Set an initial default flow control value */
946 	hw->fc.requested_mode = ixgbe_flow_control;
947 
948 	hw->phy.reset_if_overtemp = true;
949 	error = ixgbe_reset_hw(hw);
950 	hw->phy.reset_if_overtemp = false;
951 	if (error == IXGBE_ERR_SFP_NOT_PRESENT) {
952 		/*
953 		 * No optics in this port, set up
954 		 * so the timer routine will probe
955 		 * for later insertion.
956 		 */
957 		sc->sfp_probe = true;
958 		error = 0;
959 	} else if (error == IXGBE_ERR_SFP_NOT_SUPPORTED) {
960 		device_printf(dev, "Unsupported SFP+ module detected!\n");
961 		error = EIO;
962 		goto err_pci;
963 	} else if (error) {
964 		device_printf(dev, "Hardware initialization failed\n");
965 		error = EIO;
966 		goto err_pci;
967 	}
968 
969 	/* Make sure we have a good EEPROM before we read from it */
970 	if (ixgbe_validate_eeprom_checksum(&sc->hw, NULL) < 0) {
971 		device_printf(dev, "The EEPROM Checksum Is Not Valid\n");
972 		error = EIO;
973 		goto err_pci;
974 	}
975 
976 	error = ixgbe_start_hw(hw);
977 	switch (error) {
978 	case IXGBE_ERR_EEPROM_VERSION:
979 		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");
980 		break;
981 	case IXGBE_ERR_SFP_NOT_SUPPORTED:
982 		device_printf(dev, "Unsupported SFP+ Module\n");
983 		error = EIO;
984 		goto err_pci;
985 	case IXGBE_ERR_SFP_NOT_PRESENT:
986 		device_printf(dev, "No SFP+ Module found\n");
987 		/* falls thru */
988 	default:
989 		break;
990 	}
991 
992 	/* Most of the iflib initialization... */
993 
994 	iflib_set_mac(ctx, hw->mac.addr);
995 	switch (sc->hw.mac.type) {
996 	case ixgbe_mac_X550:
997 	case ixgbe_mac_X550EM_x:
998 	case ixgbe_mac_X550EM_a:
999 		scctx->isc_rss_table_size = 512;
1000 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 64;
1001 		break;
1002 	default:
1003 		scctx->isc_rss_table_size = 128;
1004 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 16;
1005 	}
1006 
1007 	/* Allow legacy interrupts */
1008 	ixgbe_txrx.ift_legacy_intr = ixgbe_intr;
1009 
1010 	scctx->isc_txqsizes[0] =
1011 	    roundup2(scctx->isc_ntxd[0] * sizeof(union ixgbe_adv_tx_desc) +
1012 	    sizeof(u32), DBA_ALIGN),
1013 	scctx->isc_rxqsizes[0] =
1014 	    roundup2(scctx->isc_nrxd[0] * sizeof(union ixgbe_adv_rx_desc),
1015 	    DBA_ALIGN);
1016 
1017 	/* XXX */
1018 	scctx->isc_tx_csum_flags = CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_TSO |
1019 	    CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_TSO;
1020 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
1021 		scctx->isc_tx_nsegments = IXGBE_82598_SCATTER;
1022 	} else {
1023 		scctx->isc_tx_csum_flags |= CSUM_SCTP |CSUM_IP6_SCTP;
1024 		scctx->isc_tx_nsegments = IXGBE_82599_SCATTER;
1025 	}
1026 
1027 	scctx->isc_msix_bar = pci_msix_table_bar(dev);
1028 
1029 	scctx->isc_tx_tso_segments_max = scctx->isc_tx_nsegments;
1030 	scctx->isc_tx_tso_size_max = IXGBE_TSO_SIZE;
1031 	scctx->isc_tx_tso_segsize_max = PAGE_SIZE;
1032 
1033 	scctx->isc_txrx = &ixgbe_txrx;
1034 
1035 	scctx->isc_capabilities = scctx->isc_capenable = IXGBE_CAPS;
1036 
1037 	return (0);
1038 
1039 err_pci:
1040 	ctrl_ext = IXGBE_READ_REG(&sc->hw, IXGBE_CTRL_EXT);
1041 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
1042 	IXGBE_WRITE_REG(&sc->hw, IXGBE_CTRL_EXT, ctrl_ext);
1043 	ixgbe_free_pci_resources(ctx);
1044 
1045 	return (error);
1046 } /* ixgbe_if_attach_pre */
1047 
1048  /*********************************************************************
1049  * ixgbe_if_attach_post - Device initialization routine, part 2
1050  *
1051  *   Called during driver load, but after interrupts and
1052  *   resources have been allocated and configured.
1053  *   Sets up some data structures not relevant to iflib.
1054  *
1055  *   return 0 on success, positive on failure
1056  *********************************************************************/
1057 static int
ixgbe_if_attach_post(if_ctx_t ctx)1058 ixgbe_if_attach_post(if_ctx_t ctx)
1059 {
1060 	device_t dev;
1061 	struct ixgbe_softc  *sc;
1062 	struct ixgbe_hw *hw;
1063 	int             error = 0;
1064 
1065 	dev = iflib_get_dev(ctx);
1066 	sc = iflib_get_softc(ctx);
1067 	hw = &sc->hw;
1068 
1069 
1070 	if (sc->intr_type == IFLIB_INTR_LEGACY &&
1071 		(sc->feat_cap & IXGBE_FEATURE_LEGACY_IRQ) == 0) {
1072 		device_printf(dev, "Device does not support legacy interrupts");
1073 		error = ENXIO;
1074 		goto err;
1075 	}
1076 
1077 	/* Allocate multicast array memory. */
1078 	sc->mta = malloc(sizeof(*sc->mta) *
1079 	                      MAX_NUM_MULTICAST_ADDRESSES, M_IXGBE, M_NOWAIT);
1080 	if (sc->mta == NULL) {
1081 		device_printf(dev, "Can not allocate multicast setup array\n");
1082 		error = ENOMEM;
1083 		goto err;
1084 	}
1085 
1086 	/* hw.ix defaults init */
1087 	ixgbe_set_advertise(sc, ixgbe_advertise_speed);
1088 
1089 	/* Enable the optics for 82599 SFP+ fiber */
1090 	ixgbe_enable_tx_laser(hw);
1091 
1092 	/* Enable power to the phy. */
1093 	ixgbe_set_phy_power(hw, true);
1094 
1095 	ixgbe_initialize_iov(sc);
1096 
1097 	error = ixgbe_setup_interface(ctx);
1098 	if (error) {
1099 		device_printf(dev, "Interface setup failed: %d\n", error);
1100 		goto err;
1101 	}
1102 
1103 	ixgbe_if_update_admin_status(ctx);
1104 
1105 	/* Initialize statistics */
1106 	ixgbe_update_stats_counters(sc);
1107 	ixgbe_add_hw_stats(sc);
1108 
1109 	/* Check PCIE slot type/speed/width */
1110 	ixgbe_get_slot_info(sc);
1111 
1112 	/*
1113 	 * Do time init and sysctl init here, but
1114 	 * only on the first port of a bypass sc.
1115 	 */
1116 	ixgbe_bypass_init(sc);
1117 
1118 	/* Display NVM and Option ROM versions */
1119 	ixgbe_print_fw_version(ctx);
1120 
1121 	/* Set an initial dmac value */
1122 	sc->dmac = 0;
1123 	/* Set initial advertised speeds (if applicable) */
1124 	sc->advertise = ixgbe_get_default_advertise(sc);
1125 
1126 	if (sc->feat_cap & IXGBE_FEATURE_SRIOV)
1127 		ixgbe_define_iov_schemas(dev, &error);
1128 
1129 	/* Add sysctls */
1130 	ixgbe_add_device_sysctls(ctx);
1131 
1132 	return (0);
1133 err:
1134 	return (error);
1135 } /* ixgbe_if_attach_post */
1136 
1137 /************************************************************************
1138  * ixgbe_check_wol_support
1139  *
1140  *   Checks whether the adapter's ports are capable of
1141  *   Wake On LAN by reading the adapter's NVM.
1142  *
1143  *   Sets each port's hw->wol_enabled value depending
1144  *   on the value read here.
1145  ************************************************************************/
1146 static void
ixgbe_check_wol_support(struct ixgbe_softc * sc)1147 ixgbe_check_wol_support(struct ixgbe_softc *sc)
1148 {
1149 	struct ixgbe_hw *hw = &sc->hw;
1150 	u16             dev_caps = 0;
1151 
1152 	/* Find out WoL support for port */
1153 	sc->wol_support = hw->wol_enabled = 0;
1154 	ixgbe_get_device_caps(hw, &dev_caps);
1155 	if ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
1156 	    ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0) &&
1157 	     hw->bus.func == 0))
1158 		sc->wol_support = hw->wol_enabled = 1;
1159 
1160 	/* Save initial wake up filter configuration */
1161 	sc->wufc = IXGBE_READ_REG(hw, IXGBE_WUFC);
1162 
1163 	return;
1164 } /* ixgbe_check_wol_support */
1165 
1166 /************************************************************************
1167  * ixgbe_setup_interface
1168  *
1169  *   Setup networking device structure and register an interface.
1170  ************************************************************************/
1171 static int
ixgbe_setup_interface(if_ctx_t ctx)1172 ixgbe_setup_interface(if_ctx_t ctx)
1173 {
1174 	struct ifnet   *ifp = iflib_get_ifp(ctx);
1175 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
1176 
1177 	INIT_DEBUGOUT("ixgbe_setup_interface: begin");
1178 
1179 	if_setbaudrate(ifp, IF_Gbps(10));
1180 
1181 	sc->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1182 
1183 	sc->phy_layer = ixgbe_get_supported_physical_layer(&sc->hw);
1184 
1185 	ixgbe_add_media_types(ctx);
1186 
1187 	/* Autoselect media by default */
1188 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
1189 
1190 	return (0);
1191 } /* ixgbe_setup_interface */
1192 
1193 /************************************************************************
1194  * ixgbe_if_get_counter
1195  ************************************************************************/
1196 static uint64_t
ixgbe_if_get_counter(if_ctx_t ctx,ift_counter cnt)1197 ixgbe_if_get_counter(if_ctx_t ctx, ift_counter cnt)
1198 {
1199 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
1200 	if_t           ifp = iflib_get_ifp(ctx);
1201 
1202 	switch (cnt) {
1203 	case IFCOUNTER_IPACKETS:
1204 		return (sc->ipackets);
1205 	case IFCOUNTER_OPACKETS:
1206 		return (sc->opackets);
1207 	case IFCOUNTER_IBYTES:
1208 		return (sc->ibytes);
1209 	case IFCOUNTER_OBYTES:
1210 		return (sc->obytes);
1211 	case IFCOUNTER_IMCASTS:
1212 		return (sc->imcasts);
1213 	case IFCOUNTER_OMCASTS:
1214 		return (sc->omcasts);
1215 	case IFCOUNTER_COLLISIONS:
1216 		return (0);
1217 	case IFCOUNTER_IQDROPS:
1218 		return (sc->iqdrops);
1219 	case IFCOUNTER_OQDROPS:
1220 		return (0);
1221 	case IFCOUNTER_IERRORS:
1222 		return (sc->ierrors);
1223 	default:
1224 		return (if_get_counter_default(ifp, cnt));
1225 	}
1226 } /* ixgbe_if_get_counter */
1227 
1228 /************************************************************************
1229  * ixgbe_if_i2c_req
1230  ************************************************************************/
1231 static int
ixgbe_if_i2c_req(if_ctx_t ctx,struct ifi2creq * req)1232 ixgbe_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req)
1233 {
1234 	struct ixgbe_softc		*sc = iflib_get_softc(ctx);
1235 	struct ixgbe_hw 	*hw = &sc->hw;
1236 	int 			i;
1237 
1238 
1239 	if (hw->phy.ops.read_i2c_byte == NULL)
1240 		return (ENXIO);
1241 	for (i = 0; i < req->len; i++)
1242 		hw->phy.ops.read_i2c_byte(hw, req->offset + i,
1243 		    req->dev_addr, &req->data[i]);
1244 	return (0);
1245 } /* ixgbe_if_i2c_req */
1246 
1247 /* ixgbe_if_needs_restart - Tell iflib when the driver needs to be reinitialized
1248  * @ctx: iflib context
1249  * @event: event code to check
1250  *
1251  * Defaults to returning true for unknown events.
1252  *
1253  * @returns true if iflib needs to reinit the interface
1254  */
1255 static bool
ixgbe_if_needs_restart(if_ctx_t ctx __unused,enum iflib_restart_event event)1256 ixgbe_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
1257 {
1258 	switch (event) {
1259 	case IFLIB_RESTART_VLAN_CONFIG:
1260 		return (false);
1261 	default:
1262 		return (true);
1263 	}
1264 }
1265 
1266 /************************************************************************
1267  * ixgbe_add_media_types
1268  ************************************************************************/
1269 static void
ixgbe_add_media_types(if_ctx_t ctx)1270 ixgbe_add_media_types(if_ctx_t ctx)
1271 {
1272 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
1273 	struct ixgbe_hw *hw = &sc->hw;
1274 	device_t        dev = iflib_get_dev(ctx);
1275 	u64             layer;
1276 
1277 	layer = sc->phy_layer = ixgbe_get_supported_physical_layer(hw);
1278 
1279 	/* Media types with matching FreeBSD media defines */
1280 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T)
1281 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_T, 0, NULL);
1282 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T)
1283 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
1284 	if (layer & IXGBE_PHYSICAL_LAYER_100BASE_TX)
1285 		ifmedia_add(sc->media, IFM_ETHER | IFM_100_TX, 0, NULL);
1286 	if (layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
1287 		ifmedia_add(sc->media, IFM_ETHER | IFM_10_T, 0, NULL);
1288 
1289 	if (hw->mac.type == ixgbe_mac_X550) {
1290 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_T, 0, NULL);
1291 		ifmedia_add(sc->media, IFM_ETHER | IFM_5000_T, 0, NULL);
1292 	}
1293 
1294 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
1295 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
1296 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_TWINAX, 0,
1297 		    NULL);
1298 
1299 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR) {
1300 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_LR, 0, NULL);
1301 		if (hw->phy.multispeed_fiber)
1302 			ifmedia_add(sc->media, IFM_ETHER | IFM_1000_LX, 0,
1303 			    NULL);
1304 	}
1305 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
1306 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
1307 		if (hw->phy.multispeed_fiber)
1308 			ifmedia_add(sc->media, IFM_ETHER | IFM_1000_SX, 0,
1309 			    NULL);
1310 	} else if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
1311 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_SX, 0, NULL);
1312 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
1313 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
1314 
1315 #ifdef IFM_ETH_XTYPE
1316 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
1317 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_KR, 0, NULL);
1318 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4)
1319 		ifmedia_add( sc->media, IFM_ETHER | IFM_10G_KX4, 0, NULL);
1320 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
1321 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_KX, 0, NULL);
1322 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX)
1323 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_KX, 0, NULL);
1324 #else
1325 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
1326 		device_printf(dev, "Media supported: 10GbaseKR\n");
1327 		device_printf(dev, "10GbaseKR mapped to 10GbaseSR\n");
1328 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_SR, 0, NULL);
1329 	}
1330 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
1331 		device_printf(dev, "Media supported: 10GbaseKX4\n");
1332 		device_printf(dev, "10GbaseKX4 mapped to 10GbaseCX4\n");
1333 		ifmedia_add(sc->media, IFM_ETHER | IFM_10G_CX4, 0, NULL);
1334 	}
1335 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX) {
1336 		device_printf(dev, "Media supported: 1000baseKX\n");
1337 		device_printf(dev, "1000baseKX mapped to 1000baseCX\n");
1338 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_CX, 0, NULL);
1339 	}
1340 	if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX) {
1341 		device_printf(dev, "Media supported: 2500baseKX\n");
1342 		device_printf(dev, "2500baseKX mapped to 2500baseSX\n");
1343 		ifmedia_add(sc->media, IFM_ETHER | IFM_2500_SX, 0, NULL);
1344 	}
1345 #endif
1346 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_BX)
1347 		device_printf(dev, "Media supported: 1000baseBX\n");
1348 
1349 	if (hw->device_id == IXGBE_DEV_ID_82598AT) {
1350 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T | IFM_FDX,
1351 		    0, NULL);
1352 		ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
1353 	}
1354 
1355 	ifmedia_add(sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1356 } /* ixgbe_add_media_types */
1357 
1358 /************************************************************************
1359  * ixgbe_is_sfp
1360  ************************************************************************/
1361 static inline bool
ixgbe_is_sfp(struct ixgbe_hw * hw)1362 ixgbe_is_sfp(struct ixgbe_hw *hw)
1363 {
1364 	switch (hw->mac.type) {
1365 	case ixgbe_mac_82598EB:
1366 		if (hw->phy.type == ixgbe_phy_nl)
1367 			return (true);
1368 		return (false);
1369 	case ixgbe_mac_82599EB:
1370 		switch (hw->mac.ops.get_media_type(hw)) {
1371 		case ixgbe_media_type_fiber:
1372 		case ixgbe_media_type_fiber_qsfp:
1373 			return (true);
1374 		default:
1375 			return (false);
1376 		}
1377 	case ixgbe_mac_X550EM_x:
1378 	case ixgbe_mac_X550EM_a:
1379 		if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_fiber)
1380 			return (true);
1381 		return (false);
1382 	default:
1383 		return (false);
1384 	}
1385 } /* ixgbe_is_sfp */
1386 
1387 /************************************************************************
1388  * ixgbe_config_link
1389  ************************************************************************/
1390 static void
ixgbe_config_link(if_ctx_t ctx)1391 ixgbe_config_link(if_ctx_t ctx)
1392 {
1393 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
1394 	struct ixgbe_hw *hw = &sc->hw;
1395 	u32             autoneg, err = 0;
1396 	bool            sfp, negotiate;
1397 
1398 	sfp = ixgbe_is_sfp(hw);
1399 
1400 	if (sfp) {
1401 		sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
1402 		iflib_admin_intr_deferred(ctx);
1403 	} else {
1404 		if (hw->mac.ops.check_link)
1405 			err = ixgbe_check_link(hw, &sc->link_speed,
1406 			    &sc->link_up, false);
1407 		if (err)
1408 			return;
1409 		autoneg = hw->phy.autoneg_advertised;
1410 		if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
1411 			err = hw->mac.ops.get_link_capabilities(hw, &autoneg,
1412 			    &negotiate);
1413 		if (err)
1414 			return;
1415 
1416 		if (hw->mac.type == ixgbe_mac_X550 &&
1417 		    hw->phy.autoneg_advertised == 0) {
1418 			/*
1419 			 * 2.5G and 5G autonegotiation speeds on X550
1420 			 * are disabled by default due to reported
1421 			 * interoperability issues with some switches.
1422 			 *
1423 			 * The second condition checks if any operations
1424 			 * involving setting autonegotiation speeds have
1425 			 * been performed prior to this ixgbe_config_link()
1426 			 * call.
1427 			 *
1428 			 * If hw->phy.autoneg_advertised does not
1429 			 * equal 0, this means that the user might have
1430 			 * set autonegotiation speeds via the sysctl
1431 			 * before bringing the interface up. In this
1432 			 * case, we should not disable 2.5G and 5G
1433 			 * since that speeds might be selected by the
1434 			 * user.
1435 			 *
1436 			 * Otherwise (i.e. if hw->phy.autoneg_advertised
1437 			 * is set to 0), it is the first time we set
1438 			 * autonegotiation preferences and the default
1439 			 * set of speeds should exclude 2.5G and 5G.
1440 			 */
1441 			autoneg &= ~(IXGBE_LINK_SPEED_2_5GB_FULL |
1442 			    IXGBE_LINK_SPEED_5GB_FULL);
1443 		}
1444 
1445 		if (hw->mac.ops.setup_link)
1446 			err = hw->mac.ops.setup_link(hw, autoneg,
1447 			    sc->link_up);
1448 	}
1449 } /* ixgbe_config_link */
1450 
1451 /************************************************************************
1452  * ixgbe_update_stats_counters - Update board statistics counters.
1453  ************************************************************************/
1454 static void
ixgbe_update_stats_counters(struct ixgbe_softc * sc)1455 ixgbe_update_stats_counters(struct ixgbe_softc *sc)
1456 {
1457 	struct ixgbe_hw       *hw = &sc->hw;
1458 	struct ixgbe_hw_stats *stats = &sc->stats.pf;
1459 	u32                   missed_rx = 0, bprc, lxon, lxoff, total;
1460 	u32                   lxoffrxc;
1461 	u64                   total_missed_rx = 0;
1462 
1463 	stats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
1464 	stats->illerrc += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
1465 	stats->errbc += IXGBE_READ_REG(hw, IXGBE_ERRBC);
1466 	stats->mspdc += IXGBE_READ_REG(hw, IXGBE_MSPDC);
1467 	stats->mpc[0] += IXGBE_READ_REG(hw, IXGBE_MPC(0));
1468 
1469 	for (int i = 0; i < 16; i++) {
1470 		stats->qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
1471 		stats->qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
1472 		stats->qprdc[i] += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
1473 	}
1474 	stats->mlfc += IXGBE_READ_REG(hw, IXGBE_MLFC);
1475 	stats->mrfc += IXGBE_READ_REG(hw, IXGBE_MRFC);
1476 	stats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
1477 
1478 	/* Hardware workaround, gprc counts missed packets */
1479 	stats->gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
1480 	stats->gprc -= missed_rx;
1481 
1482 	if (hw->mac.type != ixgbe_mac_82598EB) {
1483 		stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL) +
1484 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
1485 		stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
1486 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32);
1487 		stats->tor += IXGBE_READ_REG(hw, IXGBE_TORL) +
1488 		    ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
1489 		stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
1490 		lxoffrxc = IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
1491 		stats->lxoffrxc += lxoffrxc;
1492 	} else {
1493 		stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
1494 		lxoffrxc = IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
1495 		stats->lxoffrxc += lxoffrxc;
1496 		/* 82598 only has a counter in the high register */
1497 		stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
1498 		stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
1499 		stats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
1500 	}
1501 
1502 	/*
1503 	 * For watchdog management we need to know if we have been paused
1504 	 * during the last interval, so capture that here.
1505 	*/
1506 	if (lxoffrxc)
1507 		sc->shared->isc_pause_frames = 1;
1508 
1509 	/*
1510 	 * Workaround: mprc hardware is incorrectly counting
1511 	 * broadcasts, so for now we subtract those.
1512 	 */
1513 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
1514 	stats->bprc += bprc;
1515 	stats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
1516 	if (hw->mac.type == ixgbe_mac_82598EB)
1517 		stats->mprc -= bprc;
1518 
1519 	stats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
1520 	stats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
1521 	stats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
1522 	stats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
1523 	stats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
1524 	stats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
1525 
1526 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
1527 	stats->lxontxc += lxon;
1528 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
1529 	stats->lxofftxc += lxoff;
1530 	total = lxon + lxoff;
1531 
1532 	stats->gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
1533 	stats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
1534 	stats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
1535 	stats->gptc -= total;
1536 	stats->mptc -= total;
1537 	stats->ptc64 -= total;
1538 	stats->gotc -= total * ETHER_MIN_LEN;
1539 
1540 	stats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
1541 	stats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
1542 	stats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
1543 	stats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
1544 	stats->mngprc += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
1545 	stats->mngpdc += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
1546 	stats->mngptc += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
1547 	stats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
1548 	stats->tpt += IXGBE_READ_REG(hw, IXGBE_TPT);
1549 	stats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
1550 	stats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
1551 	stats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
1552 	stats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
1553 	stats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
1554 	stats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
1555 	stats->xec += IXGBE_READ_REG(hw, IXGBE_XEC);
1556 	stats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
1557 	stats->fclast += IXGBE_READ_REG(hw, IXGBE_FCLAST);
1558 	/* Only read FCOE on 82599 */
1559 	if (hw->mac.type != ixgbe_mac_82598EB) {
1560 		stats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
1561 		stats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
1562 		stats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
1563 		stats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
1564 		stats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
1565 	}
1566 
1567 	/* Fill out the OS statistics structure */
1568 	IXGBE_SET_IPACKETS(sc, stats->gprc);
1569 	IXGBE_SET_OPACKETS(sc, stats->gptc);
1570 	IXGBE_SET_IBYTES(sc, stats->gorc);
1571 	IXGBE_SET_OBYTES(sc, stats->gotc);
1572 	IXGBE_SET_IMCASTS(sc, stats->mprc);
1573 	IXGBE_SET_OMCASTS(sc, stats->mptc);
1574 	IXGBE_SET_COLLISIONS(sc, 0);
1575 	IXGBE_SET_IQDROPS(sc, total_missed_rx);
1576 
1577 	/*
1578 	 * Aggregate following types of errors as RX errors:
1579 	 * - CRC error count,
1580 	 * - illegal byte error count,
1581 	 * - checksum error count,
1582 	 * - missed packets count,
1583 	 * - length error count,
1584 	 * - undersized packets count,
1585 	 * - fragmented packets count,
1586 	 * - oversized packets count,
1587 	 * - jabber count.
1588 	 *
1589 	 * Ignore XEC errors for 82599 to workaround errata about
1590 	 * UDP frames with zero checksum.
1591 	 */
1592 	IXGBE_SET_IERRORS(sc, stats->crcerrs + stats->illerrc +
1593 	    (hw->mac.type != ixgbe_mac_82599EB ? stats->xec : 0) +
1594 	    stats->mpc[0] + stats->rlec + stats->ruc + stats->rfc + stats->roc +
1595 	    stats->rjc);
1596 } /* ixgbe_update_stats_counters */
1597 
1598 /************************************************************************
1599  * ixgbe_add_hw_stats
1600  *
1601  *   Add sysctl variables, one per statistic, to the system.
1602  ************************************************************************/
1603 static void
ixgbe_add_hw_stats(struct ixgbe_softc * sc)1604 ixgbe_add_hw_stats(struct ixgbe_softc *sc)
1605 {
1606 	device_t               dev = iflib_get_dev(sc->ctx);
1607 	struct ix_rx_queue     *rx_que;
1608 	struct ix_tx_queue     *tx_que;
1609 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
1610 	struct sysctl_oid      *tree = device_get_sysctl_tree(dev);
1611 	struct sysctl_oid_list *child = SYSCTL_CHILDREN(tree);
1612 	struct ixgbe_hw_stats  *stats = &sc->stats.pf;
1613 	struct sysctl_oid      *stat_node, *queue_node;
1614 	struct sysctl_oid_list *stat_list, *queue_list;
1615 	int                    i;
1616 
1617 #define QUEUE_NAME_LEN 32
1618 	char                   namebuf[QUEUE_NAME_LEN];
1619 
1620 	/* Driver Statistics */
1621 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dropped",
1622 	    CTLFLAG_RD, &sc->dropped_pkts, "Driver dropped packets");
1623 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "watchdog_events",
1624 	    CTLFLAG_RD, &sc->watchdog_events, "Watchdog timeouts");
1625 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "link_irq",
1626 	    CTLFLAG_RD, &sc->link_irq, "Link MSI-X IRQ Handled");
1627 
1628 	for (i = 0, tx_que = sc->tx_queues; i < sc->num_tx_queues; i++, tx_que++) {
1629 		struct tx_ring *txr = &tx_que->txr;
1630 		snprintf(namebuf, QUEUE_NAME_LEN, "queue%d", i);
1631 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
1632 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
1633 		queue_list = SYSCTL_CHILDREN(queue_node);
1634 
1635 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_head",
1636 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, txr, 0,
1637 		    ixgbe_sysctl_tdh_handler, "IU", "Transmit Descriptor Head");
1638 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "txd_tail",
1639 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, txr, 0,
1640 		    ixgbe_sysctl_tdt_handler, "IU", "Transmit Descriptor Tail");
1641 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tso_tx",
1642 		    CTLFLAG_RD, &txr->tso_tx, "TSO");
1643 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tx_packets",
1644 		    CTLFLAG_RD, &txr->total_packets,
1645 		    "Queue Packets Transmitted");
1646 	}
1647 
1648 	for (i = 0, rx_que = sc->rx_queues; i < sc->num_rx_queues; i++, rx_que++) {
1649 		struct rx_ring *rxr = &rx_que->rxr;
1650 		snprintf(namebuf, QUEUE_NAME_LEN, "queue%d", i);
1651 		queue_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
1652 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
1653 		queue_list = SYSCTL_CHILDREN(queue_node);
1654 
1655 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "interrupt_rate",
1656 		    CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
1657 		    &sc->rx_queues[i], 0,
1658 		    ixgbe_sysctl_interrupt_rate_handler, "IU",
1659 		    "Interrupt Rate");
1660 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "irqs",
1661 		    CTLFLAG_RD, &(sc->rx_queues[i].irqs),
1662 		    "irqs on this queue");
1663 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_head",
1664 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, rxr, 0,
1665 		    ixgbe_sysctl_rdh_handler, "IU", "Receive Descriptor Head");
1666 		SYSCTL_ADD_PROC(ctx, queue_list, OID_AUTO, "rxd_tail",
1667 		    CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, rxr, 0,
1668 		    ixgbe_sysctl_rdt_handler, "IU", "Receive Descriptor Tail");
1669 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_packets",
1670 		    CTLFLAG_RD, &rxr->rx_packets, "Queue Packets Received");
1671 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_bytes",
1672 		    CTLFLAG_RD, &rxr->rx_bytes, "Queue Bytes Received");
1673 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_copies",
1674 		    CTLFLAG_RD, &rxr->rx_copies, "Copied RX Frames");
1675 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "rx_discarded",
1676 		    CTLFLAG_RD, &rxr->rx_discarded, "Discarded RX packets");
1677 	}
1678 
1679 	/* MAC stats get their own sub node */
1680 
1681 	stat_node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "mac_stats",
1682 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "MAC Statistics");
1683 	stat_list = SYSCTL_CHILDREN(stat_node);
1684 
1685 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_errs",
1686 	    CTLFLAG_RD, &sc->ierrors, IXGBE_SYSCTL_DESC_RX_ERRS);
1687 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "crc_errs",
1688 	    CTLFLAG_RD, &stats->crcerrs, "CRC Errors");
1689 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "ill_errs",
1690 	    CTLFLAG_RD, &stats->illerrc, "Illegal Byte Errors");
1691 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "byte_errs",
1692 	    CTLFLAG_RD, &stats->errbc, "Byte Errors");
1693 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "short_discards",
1694 	    CTLFLAG_RD, &stats->mspdc, "MAC Short Packets Discarded");
1695 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "local_faults",
1696 	    CTLFLAG_RD, &stats->mlfc, "MAC Local Faults");
1697 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "remote_faults",
1698 	    CTLFLAG_RD, &stats->mrfc, "MAC Remote Faults");
1699 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rec_len_errs",
1700 	    CTLFLAG_RD, &stats->rlec, "Receive Length Errors");
1701 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_missed_packets",
1702 	    CTLFLAG_RD, &stats->mpc[0], "RX Missed Packet Count");
1703 
1704 	/* Flow Control stats */
1705 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_txd",
1706 	    CTLFLAG_RD, &stats->lxontxc, "Link XON Transmitted");
1707 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xon_recvd",
1708 	    CTLFLAG_RD, &stats->lxonrxc, "Link XON Received");
1709 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_txd",
1710 	    CTLFLAG_RD, &stats->lxofftxc, "Link XOFF Transmitted");
1711 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "xoff_recvd",
1712 	    CTLFLAG_RD, &stats->lxoffrxc, "Link XOFF Received");
1713 
1714 	/* Packet Reception Stats */
1715 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_octets_rcvd",
1716 	    CTLFLAG_RD, &stats->tor, "Total Octets Received");
1717 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_rcvd",
1718 	    CTLFLAG_RD, &stats->gorc, "Good Octets Received");
1719 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_rcvd",
1720 	    CTLFLAG_RD, &stats->tpr, "Total Packets Received");
1721 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_rcvd",
1722 	    CTLFLAG_RD, &stats->gprc, "Good Packets Received");
1723 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_rcvd",
1724 	    CTLFLAG_RD, &stats->mprc, "Multicast Packets Received");
1725 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_rcvd",
1726 	    CTLFLAG_RD, &stats->bprc, "Broadcast Packets Received");
1727 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_64",
1728 	    CTLFLAG_RD, &stats->prc64, "64 byte frames received ");
1729 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_65_127",
1730 	    CTLFLAG_RD, &stats->prc127, "65-127 byte frames received");
1731 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_128_255",
1732 	    CTLFLAG_RD, &stats->prc255, "128-255 byte frames received");
1733 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_256_511",
1734 	    CTLFLAG_RD, &stats->prc511, "256-511 byte frames received");
1735 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_512_1023",
1736 	    CTLFLAG_RD, &stats->prc1023, "512-1023 byte frames received");
1737 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "rx_frames_1024_1522",
1738 	    CTLFLAG_RD, &stats->prc1522, "1023-1522 byte frames received");
1739 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_undersized",
1740 	    CTLFLAG_RD, &stats->ruc, "Receive Undersized");
1741 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_fragmented",
1742 	    CTLFLAG_RD, &stats->rfc, "Fragmented Packets Received ");
1743 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_oversized",
1744 	    CTLFLAG_RD, &stats->roc, "Oversized Packets Received");
1745 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "recv_jabberd",
1746 	    CTLFLAG_RD, &stats->rjc, "Received Jabber");
1747 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_rcvd",
1748 	    CTLFLAG_RD, &stats->mngprc, "Management Packets Received");
1749 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_drpd",
1750 	    CTLFLAG_RD, &stats->mngptc, "Management Packets Dropped");
1751 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "checksum_errs",
1752 	    CTLFLAG_RD, &stats->xec, "Checksum Errors");
1753 
1754 	/* Packet Transmission Stats */
1755 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_octets_txd",
1756 	    CTLFLAG_RD, &stats->gotc, "Good Octets Transmitted");
1757 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "total_pkts_txd",
1758 	    CTLFLAG_RD, &stats->tpt, "Total Packets Transmitted");
1759 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "good_pkts_txd",
1760 	    CTLFLAG_RD, &stats->gptc, "Good Packets Transmitted");
1761 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "bcast_pkts_txd",
1762 	    CTLFLAG_RD, &stats->bptc, "Broadcast Packets Transmitted");
1763 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "mcast_pkts_txd",
1764 	    CTLFLAG_RD, &stats->mptc, "Multicast Packets Transmitted");
1765 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "management_pkts_txd",
1766 	    CTLFLAG_RD, &stats->mngptc, "Management Packets Transmitted");
1767 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_64",
1768 	    CTLFLAG_RD, &stats->ptc64, "64 byte frames transmitted ");
1769 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_65_127",
1770 	    CTLFLAG_RD, &stats->ptc127, "65-127 byte frames transmitted");
1771 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_128_255",
1772 	    CTLFLAG_RD, &stats->ptc255, "128-255 byte frames transmitted");
1773 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_256_511",
1774 	    CTLFLAG_RD, &stats->ptc511, "256-511 byte frames transmitted");
1775 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_512_1023",
1776 	    CTLFLAG_RD, &stats->ptc1023, "512-1023 byte frames transmitted");
1777 	SYSCTL_ADD_UQUAD(ctx, stat_list, OID_AUTO, "tx_frames_1024_1522",
1778 	    CTLFLAG_RD, &stats->ptc1522, "1024-1522 byte frames transmitted");
1779 } /* ixgbe_add_hw_stats */
1780 
1781 /************************************************************************
1782  * ixgbe_sysctl_tdh_handler - Transmit Descriptor Head handler function
1783  *
1784  *   Retrieves the TDH value from the hardware
1785  ************************************************************************/
1786 static int
ixgbe_sysctl_tdh_handler(SYSCTL_HANDLER_ARGS)1787 ixgbe_sysctl_tdh_handler(SYSCTL_HANDLER_ARGS)
1788 {
1789 	struct tx_ring *txr = ((struct tx_ring *)oidp->oid_arg1);
1790 	int            error;
1791 	unsigned int   val;
1792 
1793 	if (!txr)
1794 		return (0);
1795 
1796 	val = IXGBE_READ_REG(&txr->sc->hw, IXGBE_TDH(txr->me));
1797 	error = sysctl_handle_int(oidp, &val, 0, req);
1798 	if (error || !req->newptr)
1799 		return error;
1800 
1801 	return (0);
1802 } /* ixgbe_sysctl_tdh_handler */
1803 
1804 /************************************************************************
1805  * ixgbe_sysctl_tdt_handler - Transmit Descriptor Tail handler function
1806  *
1807  *   Retrieves the TDT value from the hardware
1808  ************************************************************************/
1809 static int
ixgbe_sysctl_tdt_handler(SYSCTL_HANDLER_ARGS)1810 ixgbe_sysctl_tdt_handler(SYSCTL_HANDLER_ARGS)
1811 {
1812 	struct tx_ring *txr = ((struct tx_ring *)oidp->oid_arg1);
1813 	int            error;
1814 	unsigned int   val;
1815 
1816 	if (!txr)
1817 		return (0);
1818 
1819 	val = IXGBE_READ_REG(&txr->sc->hw, IXGBE_TDT(txr->me));
1820 	error = sysctl_handle_int(oidp, &val, 0, req);
1821 	if (error || !req->newptr)
1822 		return error;
1823 
1824 	return (0);
1825 } /* ixgbe_sysctl_tdt_handler */
1826 
1827 /************************************************************************
1828  * ixgbe_sysctl_rdh_handler - Receive Descriptor Head handler function
1829  *
1830  *   Retrieves the RDH value from the hardware
1831  ************************************************************************/
1832 static int
ixgbe_sysctl_rdh_handler(SYSCTL_HANDLER_ARGS)1833 ixgbe_sysctl_rdh_handler(SYSCTL_HANDLER_ARGS)
1834 {
1835 	struct rx_ring *rxr = ((struct rx_ring *)oidp->oid_arg1);
1836 	int            error;
1837 	unsigned int   val;
1838 
1839 	if (!rxr)
1840 		return (0);
1841 
1842 	val = IXGBE_READ_REG(&rxr->sc->hw, IXGBE_RDH(rxr->me));
1843 	error = sysctl_handle_int(oidp, &val, 0, req);
1844 	if (error || !req->newptr)
1845 		return error;
1846 
1847 	return (0);
1848 } /* ixgbe_sysctl_rdh_handler */
1849 
1850 /************************************************************************
1851  * ixgbe_sysctl_rdt_handler - Receive Descriptor Tail handler function
1852  *
1853  *   Retrieves the RDT value from the hardware
1854  ************************************************************************/
1855 static int
ixgbe_sysctl_rdt_handler(SYSCTL_HANDLER_ARGS)1856 ixgbe_sysctl_rdt_handler(SYSCTL_HANDLER_ARGS)
1857 {
1858 	struct rx_ring *rxr = ((struct rx_ring *)oidp->oid_arg1);
1859 	int            error;
1860 	unsigned int   val;
1861 
1862 	if (!rxr)
1863 		return (0);
1864 
1865 	val = IXGBE_READ_REG(&rxr->sc->hw, IXGBE_RDT(rxr->me));
1866 	error = sysctl_handle_int(oidp, &val, 0, req);
1867 	if (error || !req->newptr)
1868 		return error;
1869 
1870 	return (0);
1871 } /* ixgbe_sysctl_rdt_handler */
1872 
1873 /************************************************************************
1874  * ixgbe_if_vlan_register
1875  *
1876  *   Run via vlan config EVENT, it enables us to use the
1877  *   HW Filter table since we can get the vlan id. This
1878  *   just creates the entry in the soft version of the
1879  *   VFTA, init will repopulate the real table.
1880  ************************************************************************/
1881 static void
ixgbe_if_vlan_register(if_ctx_t ctx,u16 vtag)1882 ixgbe_if_vlan_register(if_ctx_t ctx, u16 vtag)
1883 {
1884 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
1885 	u16            index, bit;
1886 
1887 	index = (vtag >> 5) & 0x7F;
1888 	bit = vtag & 0x1F;
1889 	sc->shadow_vfta[index] |= (1 << bit);
1890 	++sc->num_vlans;
1891 	ixgbe_setup_vlan_hw_support(ctx);
1892 } /* ixgbe_if_vlan_register */
1893 
1894 /************************************************************************
1895  * ixgbe_if_vlan_unregister
1896  *
1897  *   Run via vlan unconfig EVENT, remove our entry in the soft vfta.
1898  ************************************************************************/
1899 static void
ixgbe_if_vlan_unregister(if_ctx_t ctx,u16 vtag)1900 ixgbe_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
1901 {
1902 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
1903 	u16            index, bit;
1904 
1905 	index = (vtag >> 5) & 0x7F;
1906 	bit = vtag & 0x1F;
1907 	sc->shadow_vfta[index] &= ~(1 << bit);
1908 	--sc->num_vlans;
1909 	/* Re-init to load the changes */
1910 	ixgbe_setup_vlan_hw_support(ctx);
1911 } /* ixgbe_if_vlan_unregister */
1912 
1913 /************************************************************************
1914  * ixgbe_setup_vlan_hw_support
1915  ************************************************************************/
1916 static void
ixgbe_setup_vlan_hw_support(if_ctx_t ctx)1917 ixgbe_setup_vlan_hw_support(if_ctx_t ctx)
1918 {
1919 	struct ifnet	*ifp = iflib_get_ifp(ctx);
1920 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
1921 	struct ixgbe_hw *hw = &sc->hw;
1922 	struct rx_ring  *rxr;
1923 	int             i;
1924 	u32             ctrl;
1925 
1926 
1927 	/*
1928 	 * We get here thru init_locked, meaning
1929 	 * a soft reset, this has already cleared
1930 	 * the VFTA and other state, so if there
1931 	 * have been no vlan's registered do nothing.
1932 	 */
1933 	if (sc->num_vlans == 0)
1934 		return;
1935 
1936 	/* Setup the queues for vlans */
1937 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1938 		for (i = 0; i < sc->num_rx_queues; i++) {
1939 			rxr = &sc->rx_queues[i].rxr;
1940 			/* On 82599 the VLAN enable is per/queue in RXDCTL */
1941 			if (hw->mac.type != ixgbe_mac_82598EB) {
1942 				ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
1943 				ctrl |= IXGBE_RXDCTL_VME;
1944 				IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), ctrl);
1945 			}
1946 			rxr->vtag_strip = true;
1947 		}
1948 	}
1949 
1950 	if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
1951 		return;
1952 	/*
1953 	 * A soft reset zero's out the VFTA, so
1954 	 * we need to repopulate it now.
1955 	 */
1956 	for (i = 0; i < IXGBE_VFTA_SIZE; i++)
1957 		if (sc->shadow_vfta[i] != 0)
1958 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(i),
1959 			    sc->shadow_vfta[i]);
1960 
1961 	ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1962 	/* Enable the Filter Table if enabled */
1963 	if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
1964 		ctrl &= ~IXGBE_VLNCTRL_CFIEN;
1965 		ctrl |= IXGBE_VLNCTRL_VFE;
1966 	}
1967 	if (hw->mac.type == ixgbe_mac_82598EB)
1968 		ctrl |= IXGBE_VLNCTRL_VME;
1969 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
1970 } /* ixgbe_setup_vlan_hw_support */
1971 
1972 /************************************************************************
1973  * ixgbe_get_slot_info
1974  *
1975  *   Get the width and transaction speed of
1976  *   the slot this adapter is plugged into.
1977  ************************************************************************/
1978 static void
ixgbe_get_slot_info(struct ixgbe_softc * sc)1979 ixgbe_get_slot_info(struct ixgbe_softc *sc)
1980 {
1981 	device_t        dev = iflib_get_dev(sc->ctx);
1982 	struct ixgbe_hw *hw = &sc->hw;
1983 	int             bus_info_valid = true;
1984 	u32             offset;
1985 	u16             link;
1986 
1987 	/* Some devices are behind an internal bridge */
1988 	switch (hw->device_id) {
1989 	case IXGBE_DEV_ID_82599_SFP_SF_QP:
1990 	case IXGBE_DEV_ID_82599_QSFP_SF_QP:
1991 		goto get_parent_info;
1992 	default:
1993 		break;
1994 	}
1995 
1996 	ixgbe_get_bus_info(hw);
1997 
1998 	/*
1999 	 * Some devices don't use PCI-E, but there is no need
2000 	 * to display "Unknown" for bus speed and width.
2001 	 */
2002 	switch (hw->mac.type) {
2003 	case ixgbe_mac_X550EM_x:
2004 	case ixgbe_mac_X550EM_a:
2005 		return;
2006 	default:
2007 		goto display;
2008 	}
2009 
2010 get_parent_info:
2011 	/*
2012 	 * For the Quad port adapter we need to parse back
2013 	 * up the PCI tree to find the speed of the expansion
2014 	 * slot into which this adapter is plugged. A bit more work.
2015 	 */
2016 	dev = device_get_parent(device_get_parent(dev));
2017 #ifdef IXGBE_DEBUG
2018 	device_printf(dev, "parent pcib = %x,%x,%x\n", pci_get_bus(dev),
2019 	    pci_get_slot(dev), pci_get_function(dev));
2020 #endif
2021 	dev = device_get_parent(device_get_parent(dev));
2022 #ifdef IXGBE_DEBUG
2023 	device_printf(dev, "slot pcib = %x,%x,%x\n", pci_get_bus(dev),
2024 	    pci_get_slot(dev), pci_get_function(dev));
2025 #endif
2026 	/* Now get the PCI Express Capabilities offset */
2027 	if (pci_find_cap(dev, PCIY_EXPRESS, &offset)) {
2028 		/*
2029 		 * Hmm...can't get PCI-Express capabilities.
2030 		 * Falling back to default method.
2031 		 */
2032 		bus_info_valid = false;
2033 		ixgbe_get_bus_info(hw);
2034 		goto display;
2035 	}
2036 	/* ...and read the Link Status Register */
2037 	link = pci_read_config(dev, offset + PCIER_LINK_STA, 2);
2038 	ixgbe_set_pci_config_data_generic(hw, link);
2039 
2040 display:
2041 	device_printf(dev, "PCI Express Bus: Speed %s %s\n",
2042 	    ((hw->bus.speed == ixgbe_bus_speed_8000)    ? "8.0GT/s"  :
2043 	     (hw->bus.speed == ixgbe_bus_speed_5000)    ? "5.0GT/s"  :
2044 	     (hw->bus.speed == ixgbe_bus_speed_2500)    ? "2.5GT/s"  :
2045 	     "Unknown"),
2046 	    ((hw->bus.width == ixgbe_bus_width_pcie_x8) ? "Width x8" :
2047 	     (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "Width x4" :
2048 	     (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "Width x1" :
2049 	     "Unknown"));
2050 
2051 	if (bus_info_valid) {
2052 		if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
2053 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
2054 		    (hw->bus.speed == ixgbe_bus_speed_2500))) {
2055 			device_printf(dev, "PCI-Express bandwidth available for this card\n     is not sufficient for optimal performance.\n");
2056 			device_printf(dev, "For optimal performance a x8 PCIE, or x4 PCIE Gen2 slot is required.\n");
2057 		}
2058 		if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
2059 		    ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
2060 		    (hw->bus.speed < ixgbe_bus_speed_8000))) {
2061 			device_printf(dev, "PCI-Express bandwidth available for this card\n     is not sufficient for optimal performance.\n");
2062 			device_printf(dev, "For optimal performance a x8 PCIE Gen3 slot is required.\n");
2063 		}
2064 	} else
2065 		device_printf(dev, "Unable to determine slot speed/width. The speed/width reported are that of the internal switch.\n");
2066 
2067 	return;
2068 } /* ixgbe_get_slot_info */
2069 
2070 /************************************************************************
2071  * ixgbe_if_msix_intr_assign
2072  *
2073  *   Setup MSI-X Interrupt resources and handlers
2074  ************************************************************************/
2075 static int
ixgbe_if_msix_intr_assign(if_ctx_t ctx,int msix)2076 ixgbe_if_msix_intr_assign(if_ctx_t ctx, int msix)
2077 {
2078 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
2079 	struct ix_rx_queue *rx_que = sc->rx_queues;
2080 	struct ix_tx_queue *tx_que;
2081 	int                error, rid, vector = 0;
2082 	int                cpu_id = 0;
2083 	char               buf[16];
2084 
2085 	/* Admin Que is vector 0*/
2086 	rid = vector + 1;
2087 	for (int i = 0; i < sc->num_rx_queues; i++, vector++, rx_que++) {
2088 		rid = vector + 1;
2089 
2090 		snprintf(buf, sizeof(buf), "rxq%d", i);
2091 		error = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
2092 		    IFLIB_INTR_RXTX, ixgbe_msix_que, rx_que, rx_que->rxr.me, buf);
2093 
2094 		if (error) {
2095 			device_printf(iflib_get_dev(ctx),
2096 			    "Failed to allocate que int %d err: %d", i, error);
2097 			sc->num_rx_queues = i + 1;
2098 			goto fail;
2099 		}
2100 
2101 		rx_que->msix = vector;
2102 		if (sc->feat_en & IXGBE_FEATURE_RSS) {
2103 			/*
2104 			 * The queue ID is used as the RSS layer bucket ID.
2105 			 * We look up the queue ID -> RSS CPU ID and select
2106 			 * that.
2107 			 */
2108 			cpu_id = rss_getcpu(i % rss_getnumbuckets());
2109 		} else {
2110 			/*
2111 			 * Bind the MSI-X vector, and thus the
2112 			 * rings to the corresponding cpu.
2113 			 *
2114 			 * This just happens to match the default RSS
2115 			 * round-robin bucket -> queue -> CPU allocation.
2116 			 */
2117 			if (sc->num_rx_queues > 1)
2118 				cpu_id = i;
2119 		}
2120 
2121 	}
2122 	for (int i = 0; i < sc->num_tx_queues; i++) {
2123 		snprintf(buf, sizeof(buf), "txq%d", i);
2124 		tx_que = &sc->tx_queues[i];
2125 		tx_que->msix = i % sc->num_rx_queues;
2126 		iflib_softirq_alloc_generic(ctx,
2127 		    &sc->rx_queues[tx_que->msix].que_irq,
2128 		    IFLIB_INTR_TX, tx_que, tx_que->txr.me, buf);
2129 	}
2130 	rid = vector + 1;
2131 	error = iflib_irq_alloc_generic(ctx, &sc->irq, rid,
2132 	    IFLIB_INTR_ADMIN, ixgbe_msix_link, sc, 0, "aq");
2133 	if (error) {
2134 		device_printf(iflib_get_dev(ctx),
2135 		    "Failed to register admin handler");
2136 		return (error);
2137 	}
2138 
2139 	sc->vector = vector;
2140 
2141 	return (0);
2142 fail:
2143 	iflib_irq_free(ctx, &sc->irq);
2144 	rx_que = sc->rx_queues;
2145 	for (int i = 0; i < sc->num_rx_queues; i++, rx_que++)
2146 		iflib_irq_free(ctx, &rx_que->que_irq);
2147 
2148 	return (error);
2149 } /* ixgbe_if_msix_intr_assign */
2150 
2151 static inline void
ixgbe_perform_aim(struct ixgbe_softc * sc,struct ix_rx_queue * que)2152 ixgbe_perform_aim(struct ixgbe_softc *sc, struct ix_rx_queue *que)
2153 {
2154 	uint32_t newitr = 0;
2155 	struct rx_ring *rxr = &que->rxr;
2156 
2157 	/*
2158 	 * Do Adaptive Interrupt Moderation:
2159 	 *  - Write out last calculated setting
2160 	 *  - Calculate based on average size over
2161 	 *    the last interval.
2162 	 */
2163 	if (que->eitr_setting) {
2164 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EITR(que->msix),
2165 		    que->eitr_setting);
2166 	}
2167 
2168 	que->eitr_setting = 0;
2169 	/* Idle, do nothing */
2170 	if (rxr->bytes == 0) {
2171 		return;
2172 	}
2173 
2174 	if ((rxr->bytes) && (rxr->packets)) {
2175 		newitr = (rxr->bytes / rxr->packets);
2176 	}
2177 
2178 	newitr += 24; /* account for hardware frame, crc */
2179 	/* set an upper boundary */
2180 	newitr = min(newitr, 3000);
2181 
2182 	/* Be nice to the mid range */
2183 	if ((newitr > 300) && (newitr < 1200)) {
2184 		newitr = (newitr / 3);
2185 	} else {
2186 		newitr = (newitr / 2);
2187 	}
2188 
2189 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
2190 		newitr |= newitr << 16;
2191 	} else {
2192 		newitr |= IXGBE_EITR_CNT_WDIS;
2193 	}
2194 
2195 	/* save for next interrupt */
2196 	que->eitr_setting = newitr;
2197 
2198 	/* Reset state */
2199 	rxr->bytes = 0;
2200 	rxr->packets = 0;
2201 
2202 	return;
2203 }
2204 
2205 /*********************************************************************
2206  * ixgbe_msix_que - MSI-X Queue Interrupt Service routine
2207  **********************************************************************/
2208 static int
ixgbe_msix_que(void * arg)2209 ixgbe_msix_que(void *arg)
2210 {
2211 	struct ix_rx_queue *que = arg;
2212 	struct ixgbe_softc     *sc = que->sc;
2213 	struct ifnet       *ifp = iflib_get_ifp(que->sc->ctx);
2214 
2215 	/* Protect against spurious interrupts */
2216 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2217 		return (FILTER_HANDLED);
2218 
2219 	ixgbe_disable_queue(sc, que->msix);
2220 	++que->irqs;
2221 
2222 	/* Check for AIM */
2223 	if (sc->enable_aim) {
2224 		ixgbe_perform_aim(sc, que);
2225 	}
2226 
2227 	return (FILTER_SCHEDULE_THREAD);
2228 } /* ixgbe_msix_que */
2229 
2230 /************************************************************************
2231  * ixgbe_media_status - Media Ioctl callback
2232  *
2233  *   Called whenever the user queries the status of
2234  *   the interface using ifconfig.
2235  ************************************************************************/
2236 static void
ixgbe_if_media_status(if_ctx_t ctx,struct ifmediareq * ifmr)2237 ixgbe_if_media_status(if_ctx_t ctx, struct ifmediareq * ifmr)
2238 {
2239 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2240 	struct ixgbe_hw *hw = &sc->hw;
2241 	int             layer;
2242 
2243 	INIT_DEBUGOUT("ixgbe_if_media_status: begin");
2244 
2245 	ifmr->ifm_status = IFM_AVALID;
2246 	ifmr->ifm_active = IFM_ETHER;
2247 
2248 	if (!sc->link_active)
2249 		return;
2250 
2251 	ifmr->ifm_status |= IFM_ACTIVE;
2252 	layer = sc->phy_layer;
2253 
2254 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T ||
2255 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_T ||
2256 	    layer & IXGBE_PHYSICAL_LAYER_100BASE_TX ||
2257 	    layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
2258 		switch (sc->link_speed) {
2259 		case IXGBE_LINK_SPEED_10GB_FULL:
2260 			ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
2261 			break;
2262 		case IXGBE_LINK_SPEED_1GB_FULL:
2263 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
2264 			break;
2265 		case IXGBE_LINK_SPEED_100_FULL:
2266 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
2267 			break;
2268 		case IXGBE_LINK_SPEED_10_FULL:
2269 			ifmr->ifm_active |= IFM_10_T | IFM_FDX;
2270 			break;
2271 		}
2272 	if (hw->mac.type == ixgbe_mac_X550)
2273 		switch (sc->link_speed) {
2274 		case IXGBE_LINK_SPEED_5GB_FULL:
2275 			ifmr->ifm_active |= IFM_5000_T | IFM_FDX;
2276 			break;
2277 		case IXGBE_LINK_SPEED_2_5GB_FULL:
2278 			ifmr->ifm_active |= IFM_2500_T | IFM_FDX;
2279 			break;
2280 		}
2281 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
2282 	    layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
2283 		switch (sc->link_speed) {
2284 		case IXGBE_LINK_SPEED_10GB_FULL:
2285 			ifmr->ifm_active |= IFM_10G_TWINAX | IFM_FDX;
2286 			break;
2287 		}
2288 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR)
2289 		switch (sc->link_speed) {
2290 		case IXGBE_LINK_SPEED_10GB_FULL:
2291 			ifmr->ifm_active |= IFM_10G_LR | IFM_FDX;
2292 			break;
2293 		case IXGBE_LINK_SPEED_1GB_FULL:
2294 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
2295 			break;
2296 		}
2297 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LRM)
2298 		switch (sc->link_speed) {
2299 		case IXGBE_LINK_SPEED_10GB_FULL:
2300 			ifmr->ifm_active |= IFM_10G_LRM | IFM_FDX;
2301 			break;
2302 		case IXGBE_LINK_SPEED_1GB_FULL:
2303 			ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
2304 			break;
2305 		}
2306 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR ||
2307 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
2308 		switch (sc->link_speed) {
2309 		case IXGBE_LINK_SPEED_10GB_FULL:
2310 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
2311 			break;
2312 		case IXGBE_LINK_SPEED_1GB_FULL:
2313 			ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
2314 			break;
2315 		}
2316 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
2317 		switch (sc->link_speed) {
2318 		case IXGBE_LINK_SPEED_10GB_FULL:
2319 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
2320 			break;
2321 		}
2322 	/*
2323 	 * XXX: These need to use the proper media types once
2324 	 * they're added.
2325 	 */
2326 #ifndef IFM_ETH_XTYPE
2327 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
2328 		switch (sc->link_speed) {
2329 		case IXGBE_LINK_SPEED_10GB_FULL:
2330 			ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
2331 			break;
2332 		case IXGBE_LINK_SPEED_2_5GB_FULL:
2333 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
2334 			break;
2335 		case IXGBE_LINK_SPEED_1GB_FULL:
2336 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
2337 			break;
2338 		}
2339 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
2340 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
2341 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
2342 		switch (sc->link_speed) {
2343 		case IXGBE_LINK_SPEED_10GB_FULL:
2344 			ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
2345 			break;
2346 		case IXGBE_LINK_SPEED_2_5GB_FULL:
2347 			ifmr->ifm_active |= IFM_2500_SX | IFM_FDX;
2348 			break;
2349 		case IXGBE_LINK_SPEED_1GB_FULL:
2350 			ifmr->ifm_active |= IFM_1000_CX | IFM_FDX;
2351 			break;
2352 		}
2353 #else
2354 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
2355 		switch (sc->link_speed) {
2356 		case IXGBE_LINK_SPEED_10GB_FULL:
2357 			ifmr->ifm_active |= IFM_10G_KR | IFM_FDX;
2358 			break;
2359 		case IXGBE_LINK_SPEED_2_5GB_FULL:
2360 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
2361 			break;
2362 		case IXGBE_LINK_SPEED_1GB_FULL:
2363 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
2364 			break;
2365 		}
2366 	else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
2367 	    layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
2368 	    layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
2369 		switch (sc->link_speed) {
2370 		case IXGBE_LINK_SPEED_10GB_FULL:
2371 			ifmr->ifm_active |= IFM_10G_KX4 | IFM_FDX;
2372 			break;
2373 		case IXGBE_LINK_SPEED_2_5GB_FULL:
2374 			ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
2375 			break;
2376 		case IXGBE_LINK_SPEED_1GB_FULL:
2377 			ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
2378 			break;
2379 		}
2380 #endif
2381 
2382 	/* If nothing is recognized... */
2383 	if (IFM_SUBTYPE(ifmr->ifm_active) == 0)
2384 		ifmr->ifm_active |= IFM_UNKNOWN;
2385 
2386 	/* Display current flow control setting used on link */
2387 	if (hw->fc.current_mode == ixgbe_fc_rx_pause ||
2388 	    hw->fc.current_mode == ixgbe_fc_full)
2389 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
2390 	if (hw->fc.current_mode == ixgbe_fc_tx_pause ||
2391 	    hw->fc.current_mode == ixgbe_fc_full)
2392 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
2393 } /* ixgbe_media_status */
2394 
2395 /************************************************************************
2396  * ixgbe_media_change - Media Ioctl callback
2397  *
2398  *   Called when the user changes speed/duplex using
2399  *   media/mediopt option with ifconfig.
2400  ************************************************************************/
2401 static int
ixgbe_if_media_change(if_ctx_t ctx)2402 ixgbe_if_media_change(if_ctx_t ctx)
2403 {
2404 	struct ixgbe_softc   *sc = iflib_get_softc(ctx);
2405 	struct ifmedia   *ifm = iflib_get_media(ctx);
2406 	struct ixgbe_hw  *hw = &sc->hw;
2407 	ixgbe_link_speed speed = 0;
2408 
2409 	INIT_DEBUGOUT("ixgbe_if_media_change: begin");
2410 
2411 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2412 		return (EINVAL);
2413 
2414 	if (hw->phy.media_type == ixgbe_media_type_backplane)
2415 		return (EPERM);
2416 
2417 	/*
2418 	 * We don't actually need to check against the supported
2419 	 * media types of the adapter; ifmedia will take care of
2420 	 * that for us.
2421 	 */
2422 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
2423 	case IFM_AUTO:
2424 	case IFM_10G_T:
2425 		speed |= IXGBE_LINK_SPEED_100_FULL;
2426 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
2427 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
2428 		break;
2429 	case IFM_10G_LRM:
2430 	case IFM_10G_LR:
2431 #ifndef IFM_ETH_XTYPE
2432 	case IFM_10G_SR: /* KR, too */
2433 	case IFM_10G_CX4: /* KX4 */
2434 #else
2435 	case IFM_10G_KR:
2436 	case IFM_10G_KX4:
2437 #endif
2438 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
2439 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
2440 		break;
2441 #ifndef IFM_ETH_XTYPE
2442 	case IFM_1000_CX: /* KX */
2443 #else
2444 	case IFM_1000_KX:
2445 #endif
2446 	case IFM_1000_LX:
2447 	case IFM_1000_SX:
2448 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
2449 		break;
2450 	case IFM_1000_T:
2451 		speed |= IXGBE_LINK_SPEED_100_FULL;
2452 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
2453 		break;
2454 	case IFM_10G_TWINAX:
2455 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
2456 		break;
2457 	case IFM_5000_T:
2458 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
2459 		break;
2460 	case IFM_2500_T:
2461 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
2462 		break;
2463 	case IFM_100_TX:
2464 		speed |= IXGBE_LINK_SPEED_100_FULL;
2465 		break;
2466 	case IFM_10_T:
2467 		speed |= IXGBE_LINK_SPEED_10_FULL;
2468 		break;
2469 	default:
2470 		goto invalid;
2471 	}
2472 
2473 	hw->mac.autotry_restart = true;
2474 	hw->mac.ops.setup_link(hw, speed, true);
2475 	sc->advertise =
2476 	    ((speed & IXGBE_LINK_SPEED_10GB_FULL)  ? 0x4  : 0) |
2477 	    ((speed & IXGBE_LINK_SPEED_5GB_FULL)   ? 0x20 : 0) |
2478 	    ((speed & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
2479 	    ((speed & IXGBE_LINK_SPEED_1GB_FULL)   ? 0x2  : 0) |
2480 	    ((speed & IXGBE_LINK_SPEED_100_FULL)   ? 0x1  : 0) |
2481 	    ((speed & IXGBE_LINK_SPEED_10_FULL)    ? 0x8  : 0);
2482 
2483 	return (0);
2484 
2485 invalid:
2486 	device_printf(iflib_get_dev(ctx), "Invalid media type!\n");
2487 
2488 	return (EINVAL);
2489 } /* ixgbe_if_media_change */
2490 
2491 /************************************************************************
2492  * ixgbe_set_promisc
2493  ************************************************************************/
2494 static int
ixgbe_if_promisc_set(if_ctx_t ctx,int flags)2495 ixgbe_if_promisc_set(if_ctx_t ctx, int flags)
2496 {
2497 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2498 	struct ifnet   *ifp = iflib_get_ifp(ctx);
2499 	u32            rctl;
2500 	int            mcnt = 0;
2501 
2502 	rctl = IXGBE_READ_REG(&sc->hw, IXGBE_FCTRL);
2503 	rctl &= (~IXGBE_FCTRL_UPE);
2504 	if (ifp->if_flags & IFF_ALLMULTI)
2505 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
2506 	else {
2507 		mcnt = min(if_llmaddr_count(ifp), MAX_NUM_MULTICAST_ADDRESSES);
2508 	}
2509 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
2510 		rctl &= (~IXGBE_FCTRL_MPE);
2511 	IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
2512 
2513 	if (ifp->if_flags & IFF_PROMISC) {
2514 		rctl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
2515 		IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
2516 	} else if (ifp->if_flags & IFF_ALLMULTI) {
2517 		rctl |= IXGBE_FCTRL_MPE;
2518 		rctl &= ~IXGBE_FCTRL_UPE;
2519 		IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, rctl);
2520 	}
2521 	return (0);
2522 } /* ixgbe_if_promisc_set */
2523 
2524 /************************************************************************
2525  * ixgbe_msix_link - Link status change ISR (MSI/MSI-X)
2526  ************************************************************************/
2527 static int
ixgbe_msix_link(void * arg)2528 ixgbe_msix_link(void *arg)
2529 {
2530 	struct ixgbe_softc  *sc = arg;
2531 	struct ixgbe_hw *hw = &sc->hw;
2532 	u32             eicr, eicr_mask;
2533 	s32             retval;
2534 
2535 	++sc->link_irq;
2536 
2537 	/* Pause other interrupts */
2538 	IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_OTHER);
2539 
2540 	/* First get the cause */
2541 	eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
2542 	/* Be sure the queue bits are not cleared */
2543 	eicr &= ~IXGBE_EICR_RTX_QUEUE;
2544 	/* Clear interrupt with write */
2545 	IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
2546 
2547 	/* Link status change */
2548 	if (eicr & IXGBE_EICR_LSC) {
2549 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
2550 		sc->task_requests |= IXGBE_REQUEST_TASK_LSC;
2551 	}
2552 
2553 	if (sc->hw.mac.type != ixgbe_mac_82598EB) {
2554 		if ((sc->feat_en & IXGBE_FEATURE_FDIR) &&
2555 		    (eicr & IXGBE_EICR_FLOW_DIR)) {
2556 			/* This is probably overkill :) */
2557 			if (!atomic_cmpset_int(&sc->fdir_reinit, 0, 1))
2558 				return (FILTER_HANDLED);
2559 			/* Disable the interrupt */
2560 			IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_FLOW_DIR);
2561 			sc->task_requests |= IXGBE_REQUEST_TASK_FDIR;
2562 		} else
2563 			if (eicr & IXGBE_EICR_ECC) {
2564 				device_printf(iflib_get_dev(sc->ctx),
2565 				   "\nCRITICAL: ECC ERROR!! Please Reboot!!\n");
2566 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
2567 			}
2568 
2569 		/* Check for over temp condition */
2570 		if (sc->feat_en & IXGBE_FEATURE_TEMP_SENSOR) {
2571 			switch (sc->hw.mac.type) {
2572 			case ixgbe_mac_X550EM_a:
2573 				if (!(eicr & IXGBE_EICR_GPI_SDP0_X550EM_a))
2574 					break;
2575 				IXGBE_WRITE_REG(hw, IXGBE_EIMC,
2576 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
2577 				IXGBE_WRITE_REG(hw, IXGBE_EICR,
2578 				    IXGBE_EICR_GPI_SDP0_X550EM_a);
2579 				retval = hw->phy.ops.check_overtemp(hw);
2580 				if (retval != IXGBE_ERR_OVERTEMP)
2581 					break;
2582 				device_printf(iflib_get_dev(sc->ctx),
2583 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2584 				device_printf(iflib_get_dev(sc->ctx),
2585 				    "System shutdown required!\n");
2586 				break;
2587 			default:
2588 				if (!(eicr & IXGBE_EICR_TS))
2589 					break;
2590 				retval = hw->phy.ops.check_overtemp(hw);
2591 				if (retval != IXGBE_ERR_OVERTEMP)
2592 					break;
2593 				device_printf(iflib_get_dev(sc->ctx),
2594 				    "\nCRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
2595 				device_printf(iflib_get_dev(sc->ctx),
2596 				    "System shutdown required!\n");
2597 				IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
2598 				break;
2599 			}
2600 		}
2601 
2602 		/* Check for VF message */
2603 		if ((sc->feat_en & IXGBE_FEATURE_SRIOV) &&
2604 		    (eicr & IXGBE_EICR_MAILBOX))
2605 			sc->task_requests |= IXGBE_REQUEST_TASK_MBX;
2606 	}
2607 
2608 	if (ixgbe_is_sfp(hw)) {
2609 		/* Pluggable optics-related interrupt */
2610 		if (hw->mac.type >= ixgbe_mac_X540)
2611 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
2612 		else
2613 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
2614 
2615 		if (eicr & eicr_mask) {
2616 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
2617 			sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
2618 		}
2619 
2620 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
2621 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
2622 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
2623 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
2624 			sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
2625 		}
2626 	}
2627 
2628 	/* Check for fan failure */
2629 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL) {
2630 		ixgbe_check_fan_failure(sc, eicr, true);
2631 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
2632 	}
2633 
2634 	/* External PHY interrupt */
2635 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
2636 	    (eicr & IXGBE_EICR_GPI_SDP0_X540)) {
2637 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP0_X540);
2638 		sc->task_requests |= IXGBE_REQUEST_TASK_PHY;
2639 	}
2640 
2641 	return (sc->task_requests != 0) ? FILTER_SCHEDULE_THREAD : FILTER_HANDLED;
2642 } /* ixgbe_msix_link */
2643 
2644 /************************************************************************
2645  * ixgbe_sysctl_interrupt_rate_handler
2646  ************************************************************************/
2647 static int
ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)2648 ixgbe_sysctl_interrupt_rate_handler(SYSCTL_HANDLER_ARGS)
2649 {
2650 	struct ix_rx_queue *que = ((struct ix_rx_queue *)oidp->oid_arg1);
2651 	int                error;
2652 	unsigned int       reg, usec, rate;
2653 
2654 	reg = IXGBE_READ_REG(&que->sc->hw, IXGBE_EITR(que->msix));
2655 	usec = ((reg & 0x0FF8) >> 3);
2656 	if (usec > 0)
2657 		rate = 500000 / usec;
2658 	else
2659 		rate = 0;
2660 	error = sysctl_handle_int(oidp, &rate, 0, req);
2661 	if (error || !req->newptr)
2662 		return error;
2663 	reg &= ~0xfff; /* default, no limitation */
2664 	ixgbe_max_interrupt_rate = 0;
2665 	if (rate > 0 && rate < 500000) {
2666 		if (rate < 1000)
2667 			rate = 1000;
2668 		ixgbe_max_interrupt_rate = rate;
2669 		reg |= ((4000000/rate) & 0xff8);
2670 	}
2671 	IXGBE_WRITE_REG(&que->sc->hw, IXGBE_EITR(que->msix), reg);
2672 
2673 	return (0);
2674 } /* ixgbe_sysctl_interrupt_rate_handler */
2675 
2676 /************************************************************************
2677  * ixgbe_add_device_sysctls
2678  ************************************************************************/
2679 static void
ixgbe_add_device_sysctls(if_ctx_t ctx)2680 ixgbe_add_device_sysctls(if_ctx_t ctx)
2681 {
2682 	struct ixgbe_softc         *sc = iflib_get_softc(ctx);
2683 	device_t               dev = iflib_get_dev(ctx);
2684 	struct ixgbe_hw        *hw = &sc->hw;
2685 	struct sysctl_oid_list *child;
2686 	struct sysctl_ctx_list *ctx_list;
2687 
2688 	ctx_list = device_get_sysctl_ctx(dev);
2689 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
2690 
2691 	/* Sysctls for all devices */
2692 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fc",
2693 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2694 	    sc, 0, ixgbe_sysctl_flowcntl, "I",
2695 	    IXGBE_SYSCTL_DESC_SET_FC);
2696 
2697 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "advertise_speed",
2698 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2699 	    sc, 0, ixgbe_sysctl_advertise, "I",
2700 	    IXGBE_SYSCTL_DESC_ADV_SPEED);
2701 
2702 	sc->enable_aim = ixgbe_enable_aim;
2703 	SYSCTL_ADD_INT(ctx_list, child, OID_AUTO, "enable_aim", CTLFLAG_RW,
2704 	    &sc->enable_aim, 0, "Interrupt Moderation");
2705 
2706 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "fw_version",
2707 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
2708 	    ixgbe_sysctl_print_fw_version, "A", "Prints FW/NVM Versions");
2709 
2710 #ifdef IXGBE_DEBUG
2711 	/* testing sysctls (for all devices) */
2712 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "power_state",
2713 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2714 	    sc, 0, ixgbe_sysctl_power_state,
2715 	    "I", "PCI Power State");
2716 
2717 	SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "print_rss_config",
2718 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
2719 	    ixgbe_sysctl_print_rss_config, "A", "Prints RSS Configuration");
2720 #endif
2721 	/* for X550 series devices */
2722 	if (hw->mac.type >= ixgbe_mac_X550)
2723 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "dmac",
2724 		    CTLTYPE_U16 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2725 		    sc, 0, ixgbe_sysctl_dmac,
2726 		    "I", "DMA Coalesce");
2727 
2728 	/* for WoL-capable devices */
2729 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
2730 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wol_enable",
2731 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
2732 		    ixgbe_sysctl_wol_enable, "I", "Enable/Disable Wake on LAN");
2733 
2734 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "wufc",
2735 		    CTLTYPE_U32 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
2736 		    sc, 0, ixgbe_sysctl_wufc,
2737 		    "I", "Enable/Disable Wake Up Filters");
2738 	}
2739 
2740 	/* for X552/X557-AT devices */
2741 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
2742 		struct sysctl_oid *phy_node;
2743 		struct sysctl_oid_list *phy_list;
2744 
2745 		phy_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "phy",
2746 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "External PHY sysctls");
2747 		phy_list = SYSCTL_CHILDREN(phy_node);
2748 
2749 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO, "temp",
2750 		    CTLTYPE_U16 | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
2751 		    sc, 0, ixgbe_sysctl_phy_temp,
2752 		    "I", "Current External PHY Temperature (Celsius)");
2753 
2754 		SYSCTL_ADD_PROC(ctx_list, phy_list, OID_AUTO,
2755 		    "overtemp_occurred",
2756 		    CTLTYPE_U16 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
2757 		    ixgbe_sysctl_phy_overtemp_occurred, "I",
2758 		    "External PHY High Temperature Event Occurred");
2759 	}
2760 
2761 	if (sc->feat_cap & IXGBE_FEATURE_EEE) {
2762 		SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "eee_state",
2763 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
2764 		    ixgbe_sysctl_eee_state, "I", "EEE Power Save State");
2765 	}
2766 } /* ixgbe_add_device_sysctls */
2767 
2768 /************************************************************************
2769  * ixgbe_allocate_pci_resources
2770  ************************************************************************/
2771 static int
ixgbe_allocate_pci_resources(if_ctx_t ctx)2772 ixgbe_allocate_pci_resources(if_ctx_t ctx)
2773 {
2774 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2775 	device_t        dev = iflib_get_dev(ctx);
2776 	int             rid;
2777 
2778 	rid = PCIR_BAR(0);
2779 	sc->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2780 	    RF_ACTIVE);
2781 
2782 	if (!(sc->pci_mem)) {
2783 		device_printf(dev, "Unable to allocate bus resource: memory\n");
2784 		return (ENXIO);
2785 	}
2786 
2787 	/* Save bus_space values for READ/WRITE_REG macros */
2788 	sc->osdep.mem_bus_space_tag = rman_get_bustag(sc->pci_mem);
2789 	sc->osdep.mem_bus_space_handle =
2790 	    rman_get_bushandle(sc->pci_mem);
2791 	/* Set hw values for shared code */
2792 	sc->hw.hw_addr = (u8 *)&sc->osdep.mem_bus_space_handle;
2793 
2794 	return (0);
2795 } /* ixgbe_allocate_pci_resources */
2796 
2797 /************************************************************************
2798  * ixgbe_detach - Device removal routine
2799  *
2800  *   Called when the driver is being removed.
2801  *   Stops the adapter and deallocates all the resources
2802  *   that were allocated for driver operation.
2803  *
2804  *   return 0 on success, positive on failure
2805  ************************************************************************/
2806 static int
ixgbe_if_detach(if_ctx_t ctx)2807 ixgbe_if_detach(if_ctx_t ctx)
2808 {
2809 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2810 	device_t       dev = iflib_get_dev(ctx);
2811 	u32            ctrl_ext;
2812 
2813 	INIT_DEBUGOUT("ixgbe_detach: begin");
2814 
2815 	if (ixgbe_pci_iov_detach(dev) != 0) {
2816 		device_printf(dev, "SR-IOV in use; detach first.\n");
2817 		return (EBUSY);
2818 	}
2819 
2820 	ixgbe_setup_low_power_mode(ctx);
2821 
2822 	/* let hardware know driver is unloading */
2823 	ctrl_ext = IXGBE_READ_REG(&sc->hw, IXGBE_CTRL_EXT);
2824 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
2825 	IXGBE_WRITE_REG(&sc->hw, IXGBE_CTRL_EXT, ctrl_ext);
2826 
2827 	ixgbe_free_pci_resources(ctx);
2828 	free(sc->mta, M_IXGBE);
2829 
2830 	return (0);
2831 } /* ixgbe_if_detach */
2832 
2833 /************************************************************************
2834  * ixgbe_setup_low_power_mode - LPLU/WoL preparation
2835  *
2836  *   Prepare the adapter/port for LPLU and/or WoL
2837  ************************************************************************/
2838 static int
ixgbe_setup_low_power_mode(if_ctx_t ctx)2839 ixgbe_setup_low_power_mode(if_ctx_t ctx)
2840 {
2841 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2842 	struct ixgbe_hw *hw = &sc->hw;
2843 	device_t        dev = iflib_get_dev(ctx);
2844 	s32             error = 0;
2845 
2846 	if (!hw->wol_enabled)
2847 		ixgbe_set_phy_power(hw, false);
2848 
2849 	/* Limit power management flow to X550EM baseT */
2850 	if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T &&
2851 	    hw->phy.ops.enter_lplu) {
2852 		/* Turn off support for APM wakeup. (Using ACPI instead) */
2853 		IXGBE_WRITE_REG(hw, IXGBE_GRC,
2854 		    IXGBE_READ_REG(hw, IXGBE_GRC) & ~(u32)2);
2855 
2856 		/*
2857 		 * Clear Wake Up Status register to prevent any previous wakeup
2858 		 * events from waking us up immediately after we suspend.
2859 		 */
2860 		IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
2861 
2862 		/*
2863 		 * Program the Wakeup Filter Control register with user filter
2864 		 * settings
2865 		 */
2866 		IXGBE_WRITE_REG(hw, IXGBE_WUFC, sc->wufc);
2867 
2868 		/* Enable wakeups and power management in Wakeup Control */
2869 		IXGBE_WRITE_REG(hw, IXGBE_WUC,
2870 		    IXGBE_WUC_WKEN | IXGBE_WUC_PME_EN);
2871 
2872 		/* X550EM baseT adapters need a special LPLU flow */
2873 		hw->phy.reset_disable = true;
2874 		ixgbe_if_stop(ctx);
2875 		error = hw->phy.ops.enter_lplu(hw);
2876 		if (error)
2877 			device_printf(dev, "Error entering LPLU: %d\n", error);
2878 		hw->phy.reset_disable = false;
2879 	} else {
2880 		/* Just stop for other adapters */
2881 		ixgbe_if_stop(ctx);
2882 	}
2883 
2884 	return error;
2885 } /* ixgbe_setup_low_power_mode */
2886 
2887 /************************************************************************
2888  * ixgbe_shutdown - Shutdown entry point
2889  ************************************************************************/
2890 static int
ixgbe_if_shutdown(if_ctx_t ctx)2891 ixgbe_if_shutdown(if_ctx_t ctx)
2892 {
2893 	int error = 0;
2894 
2895 	INIT_DEBUGOUT("ixgbe_shutdown: begin");
2896 
2897 	error = ixgbe_setup_low_power_mode(ctx);
2898 
2899 	return (error);
2900 } /* ixgbe_if_shutdown */
2901 
2902 /************************************************************************
2903  * ixgbe_suspend
2904  *
2905  *   From D0 to D3
2906  ************************************************************************/
2907 static int
ixgbe_if_suspend(if_ctx_t ctx)2908 ixgbe_if_suspend(if_ctx_t ctx)
2909 {
2910 	int error = 0;
2911 
2912 	INIT_DEBUGOUT("ixgbe_suspend: begin");
2913 
2914 	error = ixgbe_setup_low_power_mode(ctx);
2915 
2916 	return (error);
2917 } /* ixgbe_if_suspend */
2918 
2919 /************************************************************************
2920  * ixgbe_resume
2921  *
2922  *   From D3 to D0
2923  ************************************************************************/
2924 static int
ixgbe_if_resume(if_ctx_t ctx)2925 ixgbe_if_resume(if_ctx_t ctx)
2926 {
2927 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
2928 	device_t        dev = iflib_get_dev(ctx);
2929 	struct ifnet    *ifp = iflib_get_ifp(ctx);
2930 	struct ixgbe_hw *hw = &sc->hw;
2931 	u32             wus;
2932 
2933 	INIT_DEBUGOUT("ixgbe_resume: begin");
2934 
2935 	/* Read & clear WUS register */
2936 	wus = IXGBE_READ_REG(hw, IXGBE_WUS);
2937 	if (wus)
2938 		device_printf(dev, "Woken up by (WUS): %#010x\n",
2939 		    IXGBE_READ_REG(hw, IXGBE_WUS));
2940 	IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
2941 	/* And clear WUFC until next low-power transition */
2942 	IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
2943 
2944 	/*
2945 	 * Required after D3->D0 transition;
2946 	 * will re-advertise all previous advertised speeds
2947 	 */
2948 	if (ifp->if_flags & IFF_UP)
2949 		ixgbe_if_init(ctx);
2950 
2951 	return (0);
2952 } /* ixgbe_if_resume */
2953 
2954 /************************************************************************
2955  * ixgbe_if_mtu_set - Ioctl mtu entry point
2956  *
2957  *   Return 0 on success, EINVAL on failure
2958  ************************************************************************/
2959 static int
ixgbe_if_mtu_set(if_ctx_t ctx,uint32_t mtu)2960 ixgbe_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
2961 {
2962 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2963 	int error = 0;
2964 
2965 	IOCTL_DEBUGOUT("ioctl: SIOCIFMTU (Set Interface MTU)");
2966 
2967 	if (mtu > IXGBE_MAX_MTU) {
2968 		error = EINVAL;
2969 	} else {
2970 		sc->max_frame_size = mtu + IXGBE_MTU_HDR;
2971 	}
2972 
2973 	return error;
2974 } /* ixgbe_if_mtu_set */
2975 
2976 /************************************************************************
2977  * ixgbe_if_crcstrip_set
2978  ************************************************************************/
2979 static void
ixgbe_if_crcstrip_set(if_ctx_t ctx,int onoff,int crcstrip)2980 ixgbe_if_crcstrip_set(if_ctx_t ctx, int onoff, int crcstrip)
2981 {
2982 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
2983 	struct ixgbe_hw *hw = &sc->hw;
2984 	/* crc stripping is set in two places:
2985 	 * IXGBE_HLREG0 (modified on init_locked and hw reset)
2986 	 * IXGBE_RDRXCTL (set by the original driver in
2987 	 *	ixgbe_setup_hw_rsc() called in init_locked.
2988 	 *	We disable the setting when netmap is compiled in).
2989 	 * We update the values here, but also in ixgbe.c because
2990 	 * init_locked sometimes is called outside our control.
2991 	 */
2992 	uint32_t hl, rxc;
2993 
2994 	hl = IXGBE_READ_REG(hw, IXGBE_HLREG0);
2995 	rxc = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
2996 #ifdef NETMAP
2997 	if (netmap_verbose)
2998 		D("%s read  HLREG 0x%x rxc 0x%x",
2999 			onoff ? "enter" : "exit", hl, rxc);
3000 #endif
3001 	/* hw requirements ... */
3002 	rxc &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
3003 	rxc |= IXGBE_RDRXCTL_RSCACKC;
3004 	if (onoff && !crcstrip) {
3005 		/* keep the crc. Fast rx */
3006 		hl &= ~IXGBE_HLREG0_RXCRCSTRP;
3007 		rxc &= ~IXGBE_RDRXCTL_CRCSTRIP;
3008 	} else {
3009 		/* reset default mode */
3010 		hl |= IXGBE_HLREG0_RXCRCSTRP;
3011 		rxc |= IXGBE_RDRXCTL_CRCSTRIP;
3012 	}
3013 #ifdef NETMAP
3014 	if (netmap_verbose)
3015 		D("%s write HLREG 0x%x rxc 0x%x",
3016 			onoff ? "enter" : "exit", hl, rxc);
3017 #endif
3018 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hl);
3019 	IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rxc);
3020 } /* ixgbe_if_crcstrip_set */
3021 
3022 /*********************************************************************
3023  * ixgbe_if_init - Init entry point
3024  *
3025  *   Used in two ways: It is used by the stack as an init
3026  *   entry point in network interface structure. It is also
3027  *   used by the driver as a hw/sw initialization routine to
3028  *   get to a consistent state.
3029  *
3030  *   Return 0 on success, positive on failure
3031  **********************************************************************/
3032 void
ixgbe_if_init(if_ctx_t ctx)3033 ixgbe_if_init(if_ctx_t ctx)
3034 {
3035 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3036 	struct ifnet       *ifp = iflib_get_ifp(ctx);
3037 	device_t           dev = iflib_get_dev(ctx);
3038 	struct ixgbe_hw *hw = &sc->hw;
3039 	struct ix_rx_queue *rx_que;
3040 	struct ix_tx_queue *tx_que;
3041 	u32             txdctl, mhadd;
3042 	u32             rxdctl, rxctrl;
3043 	u32             ctrl_ext;
3044 
3045 	int             i, j, err;
3046 
3047 	INIT_DEBUGOUT("ixgbe_if_init: begin");
3048 
3049 	/* Queue indices may change with IOV mode */
3050 	ixgbe_align_all_queue_indices(sc);
3051 
3052 	/* reprogram the RAR[0] in case user changed it. */
3053 	ixgbe_set_rar(hw, 0, hw->mac.addr, sc->pool, IXGBE_RAH_AV);
3054 
3055 	/* Get the latest mac address, User can use a LAA */
3056 	bcopy(IF_LLADDR(ifp), hw->mac.addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
3057 	ixgbe_set_rar(hw, 0, hw->mac.addr, sc->pool, 1);
3058 	hw->addr_ctrl.rar_used_count = 1;
3059 
3060 	ixgbe_init_hw(hw);
3061 
3062 	ixgbe_initialize_iov(sc);
3063 
3064 	ixgbe_initialize_transmit_units(ctx);
3065 
3066 	/* Setup Multicast table */
3067 	ixgbe_if_multi_set(ctx);
3068 
3069 	/* Determine the correct mbuf pool, based on frame size */
3070 	sc->rx_mbuf_sz = iflib_get_rx_mbuf_sz(ctx);
3071 
3072 	/* Configure RX settings */
3073 	ixgbe_initialize_receive_units(ctx);
3074 
3075 	/*
3076 	 * Initialize variable holding task enqueue requests
3077 	 * from MSI-X interrupts
3078 	 */
3079 	sc->task_requests = 0;
3080 
3081 	/* Enable SDP & MSI-X interrupts based on adapter */
3082 	ixgbe_config_gpie(sc);
3083 
3084 	/* Set MTU size */
3085 	if (ifp->if_mtu > ETHERMTU) {
3086 		/* aka IXGBE_MAXFRS on 82599 and newer */
3087 		mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
3088 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
3089 		mhadd |= sc->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
3090 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
3091 	}
3092 
3093 	/* Now enable all the queues */
3094 	for (i = 0, tx_que = sc->tx_queues; i < sc->num_tx_queues; i++, tx_que++) {
3095 		struct tx_ring *txr = &tx_que->txr;
3096 
3097 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(txr->me));
3098 		txdctl |= IXGBE_TXDCTL_ENABLE;
3099 		/* Set WTHRESH to 8, burst writeback */
3100 		txdctl |= (8 << 16);
3101 		/*
3102 		 * When the internal queue falls below PTHRESH (32),
3103 		 * start prefetching as long as there are at least
3104 		 * HTHRESH (1) buffers ready. The values are taken
3105 		 * from the Intel linux driver 3.8.21.
3106 		 * Prefetching enables tx line rate even with 1 queue.
3107 		 */
3108 		txdctl |= (32 << 0) | (1 << 8);
3109 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(txr->me), txdctl);
3110 	}
3111 
3112 	for (i = 0, rx_que = sc->rx_queues; i < sc->num_rx_queues; i++, rx_que++) {
3113 		struct rx_ring *rxr = &rx_que->rxr;
3114 
3115 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
3116 		if (hw->mac.type == ixgbe_mac_82598EB) {
3117 			/*
3118 			 * PTHRESH = 21
3119 			 * HTHRESH = 4
3120 			 * WTHRESH = 8
3121 			 */
3122 			rxdctl &= ~0x3FFFFF;
3123 			rxdctl |= 0x080420;
3124 		}
3125 		rxdctl |= IXGBE_RXDCTL_ENABLE;
3126 		IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), rxdctl);
3127 		for (j = 0; j < 10; j++) {
3128 			if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me)) &
3129 			    IXGBE_RXDCTL_ENABLE)
3130 				break;
3131 			else
3132 				msec_delay(1);
3133 		}
3134 		wmb();
3135 	}
3136 
3137 	/* Enable Receive engine */
3138 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
3139 	if (hw->mac.type == ixgbe_mac_82598EB)
3140 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
3141 	rxctrl |= IXGBE_RXCTRL_RXEN;
3142 	ixgbe_enable_rx_dma(hw, rxctrl);
3143 
3144 	/* Set up MSI/MSI-X routing */
3145 	if (ixgbe_enable_msix)  {
3146 		ixgbe_configure_ivars(sc);
3147 		/* Set up auto-mask */
3148 		if (hw->mac.type == ixgbe_mac_82598EB)
3149 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
3150 		else {
3151 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
3152 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
3153 		}
3154 	} else {  /* Simple settings for Legacy/MSI */
3155 		ixgbe_set_ivar(sc, 0, 0, 0);
3156 		ixgbe_set_ivar(sc, 0, 0, 1);
3157 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
3158 	}
3159 
3160 	ixgbe_init_fdir(sc);
3161 
3162 	/*
3163 	 * Check on any SFP devices that
3164 	 * need to be kick-started
3165 	 */
3166 	if (hw->phy.type == ixgbe_phy_none) {
3167 		err = hw->phy.ops.identify(hw);
3168 		if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
3169 			device_printf(dev,
3170 			    "Unsupported SFP+ module type was detected.\n");
3171 			return;
3172 		}
3173 	}
3174 
3175 	/* Set moderation on the Link interrupt */
3176 	IXGBE_WRITE_REG(hw, IXGBE_EITR(sc->vector), IXGBE_LINK_ITR);
3177 
3178 	/* Enable power to the phy. */
3179 	ixgbe_set_phy_power(hw, true);
3180 
3181 	/* Config/Enable Link */
3182 	ixgbe_config_link(ctx);
3183 
3184 	/* Hardware Packet Buffer & Flow Control setup */
3185 	ixgbe_config_delay_values(sc);
3186 
3187 	/* Initialize the FC settings */
3188 	ixgbe_start_hw(hw);
3189 
3190 	/* Set up VLAN support and filter */
3191 	ixgbe_setup_vlan_hw_support(ctx);
3192 
3193 	/* Setup DMA Coalescing */
3194 	ixgbe_config_dmac(sc);
3195 
3196 	/* And now turn on interrupts */
3197 	ixgbe_if_enable_intr(ctx);
3198 
3199 	/* Enable the use of the MBX by the VF's */
3200 	if (sc->feat_en & IXGBE_FEATURE_SRIOV) {
3201 		ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
3202 		ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
3203 		IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
3204 	}
3205 
3206 } /* ixgbe_init_locked */
3207 
3208 /************************************************************************
3209  * ixgbe_set_ivar
3210  *
3211  *   Setup the correct IVAR register for a particular MSI-X interrupt
3212  *     (yes this is all very magic and confusing :)
3213  *    - entry is the register array entry
3214  *    - vector is the MSI-X vector for this queue
3215  *    - type is RX/TX/MISC
3216  ************************************************************************/
3217 static void
ixgbe_set_ivar(struct ixgbe_softc * sc,u8 entry,u8 vector,s8 type)3218 ixgbe_set_ivar(struct ixgbe_softc *sc, u8 entry, u8 vector, s8 type)
3219 {
3220 	struct ixgbe_hw *hw = &sc->hw;
3221 	u32 ivar, index;
3222 
3223 	vector |= IXGBE_IVAR_ALLOC_VAL;
3224 
3225 	switch (hw->mac.type) {
3226 	case ixgbe_mac_82598EB:
3227 		if (type == -1)
3228 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
3229 		else
3230 			entry += (type * 64);
3231 		index = (entry >> 2) & 0x1F;
3232 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
3233 		ivar &= ~(0xFF << (8 * (entry & 0x3)));
3234 		ivar |= (vector << (8 * (entry & 0x3)));
3235 		IXGBE_WRITE_REG(&sc->hw, IXGBE_IVAR(index), ivar);
3236 		break;
3237 	case ixgbe_mac_82599EB:
3238 	case ixgbe_mac_X540:
3239 	case ixgbe_mac_X550:
3240 	case ixgbe_mac_X550EM_x:
3241 	case ixgbe_mac_X550EM_a:
3242 		if (type == -1) { /* MISC IVAR */
3243 			index = (entry & 1) * 8;
3244 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
3245 			ivar &= ~(0xFF << index);
3246 			ivar |= (vector << index);
3247 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
3248 		} else {          /* RX/TX IVARS */
3249 			index = (16 * (entry & 1)) + (8 * type);
3250 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
3251 			ivar &= ~(0xFF << index);
3252 			ivar |= (vector << index);
3253 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
3254 		}
3255 	default:
3256 		break;
3257 	}
3258 } /* ixgbe_set_ivar */
3259 
3260 /************************************************************************
3261  * ixgbe_configure_ivars
3262  ************************************************************************/
3263 static void
ixgbe_configure_ivars(struct ixgbe_softc * sc)3264 ixgbe_configure_ivars(struct ixgbe_softc *sc)
3265 {
3266 	struct ix_rx_queue *rx_que = sc->rx_queues;
3267 	struct ix_tx_queue *tx_que = sc->tx_queues;
3268 	u32                newitr;
3269 
3270 	if (ixgbe_max_interrupt_rate > 0)
3271 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
3272 	else {
3273 		/*
3274 		 * Disable DMA coalescing if interrupt moderation is
3275 		 * disabled.
3276 		 */
3277 		sc->dmac = 0;
3278 		newitr = 0;
3279 	}
3280 
3281 	for (int i = 0; i < sc->num_rx_queues; i++, rx_que++) {
3282 		struct rx_ring *rxr = &rx_que->rxr;
3283 
3284 		/* First the RX queue entry */
3285 		ixgbe_set_ivar(sc, rxr->me, rx_que->msix, 0);
3286 
3287 		/* Set an Initial EITR value */
3288 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EITR(rx_que->msix), newitr);
3289 	}
3290 	for (int i = 0; i < sc->num_tx_queues; i++, tx_que++) {
3291 		struct tx_ring *txr = &tx_que->txr;
3292 
3293 		/* ... and the TX */
3294 		ixgbe_set_ivar(sc, txr->me, tx_que->msix, 1);
3295 	}
3296 	/* For the Link interrupt */
3297 	ixgbe_set_ivar(sc, 1, sc->vector, -1);
3298 } /* ixgbe_configure_ivars */
3299 
3300 /************************************************************************
3301  * ixgbe_config_gpie
3302  ************************************************************************/
3303 static void
ixgbe_config_gpie(struct ixgbe_softc * sc)3304 ixgbe_config_gpie(struct ixgbe_softc *sc)
3305 {
3306 	struct ixgbe_hw *hw = &sc->hw;
3307 	u32             gpie;
3308 
3309 	gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
3310 
3311 	if (sc->intr_type == IFLIB_INTR_MSIX) {
3312 		/* Enable Enhanced MSI-X mode */
3313 		gpie |= IXGBE_GPIE_MSIX_MODE
3314 		     |  IXGBE_GPIE_EIAME
3315 		     |  IXGBE_GPIE_PBA_SUPPORT
3316 		     |  IXGBE_GPIE_OCD;
3317 	}
3318 
3319 	/* Fan Failure Interrupt */
3320 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL)
3321 		gpie |= IXGBE_SDP1_GPIEN;
3322 
3323 	/* Thermal Sensor Interrupt */
3324 	if (sc->feat_en & IXGBE_FEATURE_TEMP_SENSOR)
3325 		gpie |= IXGBE_SDP0_GPIEN_X540;
3326 
3327 	/* Link detection */
3328 	switch (hw->mac.type) {
3329 	case ixgbe_mac_82599EB:
3330 		gpie |= IXGBE_SDP1_GPIEN | IXGBE_SDP2_GPIEN;
3331 		break;
3332 	case ixgbe_mac_X550EM_x:
3333 	case ixgbe_mac_X550EM_a:
3334 		gpie |= IXGBE_SDP0_GPIEN_X540;
3335 		break;
3336 	default:
3337 		break;
3338 	}
3339 
3340 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
3341 
3342 } /* ixgbe_config_gpie */
3343 
3344 /************************************************************************
3345  * ixgbe_config_delay_values
3346  *
3347  *   Requires sc->max_frame_size to be set.
3348  ************************************************************************/
3349 static void
ixgbe_config_delay_values(struct ixgbe_softc * sc)3350 ixgbe_config_delay_values(struct ixgbe_softc *sc)
3351 {
3352 	struct ixgbe_hw *hw = &sc->hw;
3353 	u32             rxpb, frame, size, tmp;
3354 
3355 	frame = sc->max_frame_size;
3356 
3357 	/* Calculate High Water */
3358 	switch (hw->mac.type) {
3359 	case ixgbe_mac_X540:
3360 	case ixgbe_mac_X550:
3361 	case ixgbe_mac_X550EM_x:
3362 	case ixgbe_mac_X550EM_a:
3363 		tmp = IXGBE_DV_X540(frame, frame);
3364 		break;
3365 	default:
3366 		tmp = IXGBE_DV(frame, frame);
3367 		break;
3368 	}
3369 	size = IXGBE_BT2KB(tmp);
3370 	rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
3371 	hw->fc.high_water[0] = rxpb - size;
3372 
3373 	/* Now calculate Low Water */
3374 	switch (hw->mac.type) {
3375 	case ixgbe_mac_X540:
3376 	case ixgbe_mac_X550:
3377 	case ixgbe_mac_X550EM_x:
3378 	case ixgbe_mac_X550EM_a:
3379 		tmp = IXGBE_LOW_DV_X540(frame);
3380 		break;
3381 	default:
3382 		tmp = IXGBE_LOW_DV(frame);
3383 		break;
3384 	}
3385 	hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
3386 
3387 	hw->fc.pause_time = IXGBE_FC_PAUSE;
3388 	hw->fc.send_xon = true;
3389 } /* ixgbe_config_delay_values */
3390 
3391 /************************************************************************
3392  * ixgbe_set_multi - Multicast Update
3393  *
3394  *   Called whenever multicast address list is updated.
3395  ************************************************************************/
3396 static u_int
ixgbe_mc_filter_apply(void * arg,struct sockaddr_dl * sdl,u_int idx)3397 ixgbe_mc_filter_apply(void *arg, struct sockaddr_dl *sdl, u_int idx)
3398 {
3399 	struct ixgbe_softc *sc = arg;
3400 	struct ixgbe_mc_addr *mta = sc->mta;
3401 
3402 	if (idx == MAX_NUM_MULTICAST_ADDRESSES)
3403 		return (0);
3404 	bcopy(LLADDR(sdl), mta[idx].addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
3405 	mta[idx].vmdq = sc->pool;
3406 
3407 	return (1);
3408 } /* ixgbe_mc_filter_apply */
3409 
3410 static void
ixgbe_if_multi_set(if_ctx_t ctx)3411 ixgbe_if_multi_set(if_ctx_t ctx)
3412 {
3413 	struct ixgbe_softc       *sc = iflib_get_softc(ctx);
3414 	struct ixgbe_mc_addr *mta;
3415 	struct ifnet         *ifp = iflib_get_ifp(ctx);
3416 	u8                   *update_ptr;
3417 	u32                  fctrl;
3418 	u_int		     mcnt;
3419 
3420 	IOCTL_DEBUGOUT("ixgbe_if_multi_set: begin");
3421 
3422 	mta = sc->mta;
3423 	bzero(mta, sizeof(*mta) * MAX_NUM_MULTICAST_ADDRESSES);
3424 
3425 	mcnt = if_foreach_llmaddr(iflib_get_ifp(ctx), ixgbe_mc_filter_apply, sc);
3426 
3427 	fctrl = IXGBE_READ_REG(&sc->hw, IXGBE_FCTRL);
3428 
3429 	if (ifp->if_flags & IFF_PROMISC)
3430 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
3431 	else if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES ||
3432 	    ifp->if_flags & IFF_ALLMULTI) {
3433 		fctrl |= IXGBE_FCTRL_MPE;
3434 		fctrl &= ~IXGBE_FCTRL_UPE;
3435 	} else
3436 		fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
3437 
3438 	IXGBE_WRITE_REG(&sc->hw, IXGBE_FCTRL, fctrl);
3439 
3440 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES) {
3441 		update_ptr = (u8 *)mta;
3442 		ixgbe_update_mc_addr_list(&sc->hw, update_ptr, mcnt,
3443 		    ixgbe_mc_array_itr, true);
3444 	}
3445 
3446 } /* ixgbe_if_multi_set */
3447 
3448 /************************************************************************
3449  * ixgbe_mc_array_itr
3450  *
3451  *   An iterator function needed by the multicast shared code.
3452  *   It feeds the shared code routine the addresses in the
3453  *   array of ixgbe_set_multi() one by one.
3454  ************************************************************************/
3455 static u8 *
ixgbe_mc_array_itr(struct ixgbe_hw * hw,u8 ** update_ptr,u32 * vmdq)3456 ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
3457 {
3458 	struct ixgbe_mc_addr *mta;
3459 
3460 	mta = (struct ixgbe_mc_addr *)*update_ptr;
3461 	*vmdq = mta->vmdq;
3462 
3463 	*update_ptr = (u8*)(mta + 1);
3464 
3465 	return (mta->addr);
3466 } /* ixgbe_mc_array_itr */
3467 
3468 /************************************************************************
3469  * ixgbe_local_timer - Timer routine
3470  *
3471  *   Checks for link status, updates statistics,
3472  *   and runs the watchdog check.
3473  ************************************************************************/
3474 static void
ixgbe_if_timer(if_ctx_t ctx,uint16_t qid)3475 ixgbe_if_timer(if_ctx_t ctx, uint16_t qid)
3476 {
3477 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3478 
3479 	if (qid != 0)
3480 		return;
3481 
3482 	/* Check for pluggable optics */
3483 	if (sc->sfp_probe)
3484 		if (!ixgbe_sfp_probe(ctx))
3485 			return; /* Nothing to do */
3486 
3487 	ixgbe_check_link(&sc->hw, &sc->link_speed, &sc->link_up, 0);
3488 
3489 	/* Fire off the adminq task */
3490 	iflib_admin_intr_deferred(ctx);
3491 
3492 } /* ixgbe_if_timer */
3493 
3494 /************************************************************************
3495  * ixgbe_sfp_probe
3496  *
3497  *   Determine if a port had optics inserted.
3498  ************************************************************************/
3499 static bool
ixgbe_sfp_probe(if_ctx_t ctx)3500 ixgbe_sfp_probe(if_ctx_t ctx)
3501 {
3502 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3503 	struct ixgbe_hw *hw = &sc->hw;
3504 	device_t        dev = iflib_get_dev(ctx);
3505 	bool            result = false;
3506 
3507 	if ((hw->phy.type == ixgbe_phy_nl) &&
3508 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
3509 		s32 ret = hw->phy.ops.identify_sfp(hw);
3510 		if (ret)
3511 			goto out;
3512 		ret = hw->phy.ops.reset(hw);
3513 		sc->sfp_probe = false;
3514 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
3515 			device_printf(dev, "Unsupported SFP+ module detected!");
3516 			device_printf(dev,
3517 			    "Reload driver with supported module.\n");
3518 			goto out;
3519 		} else
3520 			device_printf(dev, "SFP+ module detected!\n");
3521 		/* We now have supported optics */
3522 		result = true;
3523 	}
3524 out:
3525 
3526 	return (result);
3527 } /* ixgbe_sfp_probe */
3528 
3529 /************************************************************************
3530  * ixgbe_handle_mod - Tasklet for SFP module interrupts
3531  ************************************************************************/
3532 static void
ixgbe_handle_mod(void * context)3533 ixgbe_handle_mod(void *context)
3534 {
3535 	if_ctx_t        ctx = context;
3536 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3537 	struct ixgbe_hw *hw = &sc->hw;
3538 	device_t        dev = iflib_get_dev(ctx);
3539 	u32             err, cage_full = 0;
3540 
3541 	if (sc->hw.need_crosstalk_fix) {
3542 		switch (hw->mac.type) {
3543 		case ixgbe_mac_82599EB:
3544 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
3545 			    IXGBE_ESDP_SDP2;
3546 			break;
3547 		case ixgbe_mac_X550EM_x:
3548 		case ixgbe_mac_X550EM_a:
3549 			cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
3550 			    IXGBE_ESDP_SDP0;
3551 			break;
3552 		default:
3553 			break;
3554 		}
3555 
3556 		if (!cage_full)
3557 			goto handle_mod_out;
3558 	}
3559 
3560 	err = hw->phy.ops.identify_sfp(hw);
3561 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
3562 		device_printf(dev,
3563 		    "Unsupported SFP+ module type was detected.\n");
3564 		goto handle_mod_out;
3565 	}
3566 
3567 	if (hw->mac.type == ixgbe_mac_82598EB)
3568 		err = hw->phy.ops.reset(hw);
3569 	else
3570 		err = hw->mac.ops.setup_sfp(hw);
3571 
3572 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
3573 		device_printf(dev,
3574 		    "Setup failure - unsupported SFP+ module type.\n");
3575 		goto handle_mod_out;
3576 	}
3577 	sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
3578 	return;
3579 
3580 handle_mod_out:
3581 	sc->task_requests &= ~(IXGBE_REQUEST_TASK_MSF);
3582 } /* ixgbe_handle_mod */
3583 
3584 
3585 /************************************************************************
3586  * ixgbe_handle_msf - Tasklet for MSF (multispeed fiber) interrupts
3587  ************************************************************************/
3588 static void
ixgbe_handle_msf(void * context)3589 ixgbe_handle_msf(void *context)
3590 {
3591 	if_ctx_t        ctx = context;
3592 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3593 	struct ixgbe_hw *hw = &sc->hw;
3594 	u32             autoneg;
3595 	bool            negotiate;
3596 
3597 	/* get_supported_phy_layer will call hw->phy.ops.identify_sfp() */
3598 	sc->phy_layer = ixgbe_get_supported_physical_layer(hw);
3599 
3600 	autoneg = hw->phy.autoneg_advertised;
3601 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
3602 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
3603 	if (hw->mac.ops.setup_link)
3604 		hw->mac.ops.setup_link(hw, autoneg, true);
3605 
3606 	/* Adjust media types shown in ifconfig */
3607 	ifmedia_removeall(sc->media);
3608 	ixgbe_add_media_types(sc->ctx);
3609 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
3610 } /* ixgbe_handle_msf */
3611 
3612 /************************************************************************
3613  * ixgbe_handle_phy - Tasklet for external PHY interrupts
3614  ************************************************************************/
3615 static void
ixgbe_handle_phy(void * context)3616 ixgbe_handle_phy(void *context)
3617 {
3618 	if_ctx_t        ctx = context;
3619 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3620 	struct ixgbe_hw *hw = &sc->hw;
3621 	int             error;
3622 
3623 	error = hw->phy.ops.handle_lasi(hw);
3624 	if (error == IXGBE_ERR_OVERTEMP)
3625 		device_printf(sc->dev, "CRITICAL: EXTERNAL PHY OVER TEMP!!  PHY will downshift to lower power state!\n");
3626 	else if (error)
3627 		device_printf(sc->dev,
3628 		    "Error handling LASI interrupt: %d\n", error);
3629 } /* ixgbe_handle_phy */
3630 
3631 /************************************************************************
3632  * ixgbe_if_stop - Stop the hardware
3633  *
3634  *   Disables all traffic on the adapter by issuing a
3635  *   global reset on the MAC and deallocates TX/RX buffers.
3636  ************************************************************************/
3637 static void
ixgbe_if_stop(if_ctx_t ctx)3638 ixgbe_if_stop(if_ctx_t ctx)
3639 {
3640 	struct ixgbe_softc  *sc = iflib_get_softc(ctx);
3641 	struct ixgbe_hw *hw = &sc->hw;
3642 
3643 	INIT_DEBUGOUT("ixgbe_if_stop: begin\n");
3644 
3645 	ixgbe_reset_hw(hw);
3646 	hw->adapter_stopped = false;
3647 	ixgbe_stop_adapter(hw);
3648 	if (hw->mac.type == ixgbe_mac_82599EB)
3649 		ixgbe_stop_mac_link_on_d3_82599(hw);
3650 	/* Turn off the laser - noop with no optics */
3651 	ixgbe_disable_tx_laser(hw);
3652 
3653 	/* Update the stack */
3654 	sc->link_up = false;
3655 	ixgbe_if_update_admin_status(ctx);
3656 
3657 	/* reprogram the RAR[0] in case user changed it. */
3658 	ixgbe_set_rar(&sc->hw, 0, sc->hw.mac.addr, 0, IXGBE_RAH_AV);
3659 
3660 	return;
3661 } /* ixgbe_if_stop */
3662 
3663 /************************************************************************
3664  * ixgbe_update_link_status - Update OS on link state
3665  *
3666  * Note: Only updates the OS on the cached link state.
3667  *       The real check of the hardware only happens with
3668  *       a link interrupt.
3669  ************************************************************************/
3670 static void
ixgbe_if_update_admin_status(if_ctx_t ctx)3671 ixgbe_if_update_admin_status(if_ctx_t ctx)
3672 {
3673 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3674 	device_t       dev = iflib_get_dev(ctx);
3675 
3676 	if (sc->link_up) {
3677 		if (sc->link_active == false) {
3678 			if (bootverbose)
3679 				device_printf(dev, "Link is up %d Gbps %s \n",
3680 				    ((sc->link_speed == 128) ? 10 : 1),
3681 				    "Full Duplex");
3682 			sc->link_active = true;
3683 			/* Update any Flow Control changes */
3684 			ixgbe_fc_enable(&sc->hw);
3685 			/* Update DMA coalescing config */
3686 			ixgbe_config_dmac(sc);
3687 			/* should actually be negotiated value */
3688 			iflib_link_state_change(ctx, LINK_STATE_UP, IF_Gbps(10));
3689 
3690 			if (sc->feat_en & IXGBE_FEATURE_SRIOV)
3691 				ixgbe_ping_all_vfs(sc);
3692 		}
3693 	} else { /* Link down */
3694 		if (sc->link_active == true) {
3695 			if (bootverbose)
3696 				device_printf(dev, "Link is Down\n");
3697 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
3698 			sc->link_active = false;
3699 			if (sc->feat_en & IXGBE_FEATURE_SRIOV)
3700 				ixgbe_ping_all_vfs(sc);
3701 		}
3702 	}
3703 
3704 	/* Handle task requests from msix_link() */
3705 	if (sc->task_requests & IXGBE_REQUEST_TASK_MOD)
3706 		ixgbe_handle_mod(ctx);
3707 	if (sc->task_requests & IXGBE_REQUEST_TASK_MSF)
3708 		ixgbe_handle_msf(ctx);
3709 	if (sc->task_requests & IXGBE_REQUEST_TASK_MBX)
3710 		ixgbe_handle_mbx(ctx);
3711 	if (sc->task_requests & IXGBE_REQUEST_TASK_FDIR)
3712 		ixgbe_reinit_fdir(ctx);
3713 	if (sc->task_requests & IXGBE_REQUEST_TASK_PHY)
3714 		ixgbe_handle_phy(ctx);
3715 	sc->task_requests = 0;
3716 
3717 	ixgbe_update_stats_counters(sc);
3718 } /* ixgbe_if_update_admin_status */
3719 
3720 /************************************************************************
3721  * ixgbe_config_dmac - Configure DMA Coalescing
3722  ************************************************************************/
3723 static void
ixgbe_config_dmac(struct ixgbe_softc * sc)3724 ixgbe_config_dmac(struct ixgbe_softc *sc)
3725 {
3726 	struct ixgbe_hw          *hw = &sc->hw;
3727 	struct ixgbe_dmac_config *dcfg = &hw->mac.dmac_config;
3728 
3729 	if (hw->mac.type < ixgbe_mac_X550 || !hw->mac.ops.dmac_config)
3730 		return;
3731 
3732 	if (dcfg->watchdog_timer ^ sc->dmac ||
3733 	    dcfg->link_speed ^ sc->link_speed) {
3734 		dcfg->watchdog_timer = sc->dmac;
3735 		dcfg->fcoe_en = false;
3736 		dcfg->link_speed = sc->link_speed;
3737 		dcfg->num_tcs = 1;
3738 
3739 		INIT_DEBUGOUT2("dmac settings: watchdog %d, link speed %d\n",
3740 		    dcfg->watchdog_timer, dcfg->link_speed);
3741 
3742 		hw->mac.ops.dmac_config(hw);
3743 	}
3744 } /* ixgbe_config_dmac */
3745 
3746 /************************************************************************
3747  * ixgbe_if_enable_intr
3748  ************************************************************************/
3749 void
ixgbe_if_enable_intr(if_ctx_t ctx)3750 ixgbe_if_enable_intr(if_ctx_t ctx)
3751 {
3752 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3753 	struct ixgbe_hw    *hw = &sc->hw;
3754 	struct ix_rx_queue *que = sc->rx_queues;
3755 	u32                mask, fwsm;
3756 
3757 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
3758 
3759 	switch (sc->hw.mac.type) {
3760 	case ixgbe_mac_82599EB:
3761 		mask |= IXGBE_EIMS_ECC;
3762 		/* Temperature sensor on some scs */
3763 		mask |= IXGBE_EIMS_GPI_SDP0;
3764 		/* SFP+ (RX_LOS_N & MOD_ABS_N) */
3765 		mask |= IXGBE_EIMS_GPI_SDP1;
3766 		mask |= IXGBE_EIMS_GPI_SDP2;
3767 		break;
3768 	case ixgbe_mac_X540:
3769 		/* Detect if Thermal Sensor is enabled */
3770 		fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
3771 		if (fwsm & IXGBE_FWSM_TS_ENABLED)
3772 			mask |= IXGBE_EIMS_TS;
3773 		mask |= IXGBE_EIMS_ECC;
3774 		break;
3775 	case ixgbe_mac_X550:
3776 		/* MAC thermal sensor is automatically enabled */
3777 		mask |= IXGBE_EIMS_TS;
3778 		mask |= IXGBE_EIMS_ECC;
3779 		break;
3780 	case ixgbe_mac_X550EM_x:
3781 	case ixgbe_mac_X550EM_a:
3782 		/* Some devices use SDP0 for important information */
3783 		if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
3784 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP ||
3785 		    hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP_N ||
3786 		    hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
3787 			mask |= IXGBE_EIMS_GPI_SDP0_BY_MAC(hw);
3788 		if (hw->phy.type == ixgbe_phy_x550em_ext_t)
3789 			mask |= IXGBE_EICR_GPI_SDP0_X540;
3790 		mask |= IXGBE_EIMS_ECC;
3791 		break;
3792 	default:
3793 		break;
3794 	}
3795 
3796 	/* Enable Fan Failure detection */
3797 	if (sc->feat_en & IXGBE_FEATURE_FAN_FAIL)
3798 		mask |= IXGBE_EIMS_GPI_SDP1;
3799 	/* Enable SR-IOV */
3800 	if (sc->feat_en & IXGBE_FEATURE_SRIOV)
3801 		mask |= IXGBE_EIMS_MAILBOX;
3802 	/* Enable Flow Director */
3803 	if (sc->feat_en & IXGBE_FEATURE_FDIR)
3804 		mask |= IXGBE_EIMS_FLOW_DIR;
3805 
3806 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
3807 
3808 	/* With MSI-X we use auto clear */
3809 	if (sc->intr_type == IFLIB_INTR_MSIX) {
3810 		mask = IXGBE_EIMS_ENABLE_MASK;
3811 		/* Don't autoclear Link */
3812 		mask &= ~IXGBE_EIMS_OTHER;
3813 		mask &= ~IXGBE_EIMS_LSC;
3814 		if (sc->feat_cap & IXGBE_FEATURE_SRIOV)
3815 			mask &= ~IXGBE_EIMS_MAILBOX;
3816 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
3817 	}
3818 
3819 	/*
3820 	 * Now enable all queues, this is done separately to
3821 	 * allow for handling the extended (beyond 32) MSI-X
3822 	 * vectors that can be used by 82599
3823 	 */
3824 	for (int i = 0; i < sc->num_rx_queues; i++, que++)
3825 		ixgbe_enable_queue(sc, que->msix);
3826 
3827 	IXGBE_WRITE_FLUSH(hw);
3828 
3829 } /* ixgbe_if_enable_intr */
3830 
3831 /************************************************************************
3832  * ixgbe_disable_intr
3833  ************************************************************************/
3834 static void
ixgbe_if_disable_intr(if_ctx_t ctx)3835 ixgbe_if_disable_intr(if_ctx_t ctx)
3836 {
3837 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3838 
3839 	if (sc->intr_type == IFLIB_INTR_MSIX)
3840 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIAC, 0);
3841 	if (sc->hw.mac.type == ixgbe_mac_82598EB) {
3842 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC, ~0);
3843 	} else {
3844 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC, 0xFFFF0000);
3845 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC_EX(0), ~0);
3846 		IXGBE_WRITE_REG(&sc->hw, IXGBE_EIMC_EX(1), ~0);
3847 	}
3848 	IXGBE_WRITE_FLUSH(&sc->hw);
3849 
3850 } /* ixgbe_if_disable_intr */
3851 
3852 /************************************************************************
3853  * ixgbe_link_intr_enable
3854  ************************************************************************/
3855 static void
ixgbe_link_intr_enable(if_ctx_t ctx)3856 ixgbe_link_intr_enable(if_ctx_t ctx)
3857 {
3858 	struct ixgbe_hw *hw = &((struct ixgbe_softc *)iflib_get_softc(ctx))->hw;
3859 
3860 	/* Re-enable other interrupts */
3861 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER | IXGBE_EIMS_LSC);
3862 } /* ixgbe_link_intr_enable */
3863 
3864 /************************************************************************
3865  * ixgbe_if_rx_queue_intr_enable
3866  ************************************************************************/
3867 static int
ixgbe_if_rx_queue_intr_enable(if_ctx_t ctx,uint16_t rxqid)3868 ixgbe_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
3869 {
3870 	struct ixgbe_softc     *sc = iflib_get_softc(ctx);
3871 	struct ix_rx_queue *que = &sc->rx_queues[rxqid];
3872 
3873 	ixgbe_enable_queue(sc, que->msix);
3874 
3875 	return (0);
3876 } /* ixgbe_if_rx_queue_intr_enable */
3877 
3878 /************************************************************************
3879  * ixgbe_enable_queue
3880  ************************************************************************/
3881 static void
ixgbe_enable_queue(struct ixgbe_softc * sc,u32 vector)3882 ixgbe_enable_queue(struct ixgbe_softc *sc, u32 vector)
3883 {
3884 	struct ixgbe_hw *hw = &sc->hw;
3885 	u64             queue = 1ULL << vector;
3886 	u32             mask;
3887 
3888 	if (hw->mac.type == ixgbe_mac_82598EB) {
3889 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3890 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
3891 	} else {
3892 		mask = (queue & 0xFFFFFFFF);
3893 		if (mask)
3894 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
3895 		mask = (queue >> 32);
3896 		if (mask)
3897 			IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
3898 	}
3899 } /* ixgbe_enable_queue */
3900 
3901 /************************************************************************
3902  * ixgbe_disable_queue
3903  ************************************************************************/
3904 static void
ixgbe_disable_queue(struct ixgbe_softc * sc,u32 vector)3905 ixgbe_disable_queue(struct ixgbe_softc *sc, u32 vector)
3906 {
3907 	struct ixgbe_hw *hw = &sc->hw;
3908 	u64             queue = 1ULL << vector;
3909 	u32             mask;
3910 
3911 	if (hw->mac.type == ixgbe_mac_82598EB) {
3912 		mask = (IXGBE_EIMS_RTX_QUEUE & queue);
3913 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
3914 	} else {
3915 		mask = (queue & 0xFFFFFFFF);
3916 		if (mask)
3917 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
3918 		mask = (queue >> 32);
3919 		if (mask)
3920 			IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
3921 	}
3922 } /* ixgbe_disable_queue */
3923 
3924 /************************************************************************
3925  * ixgbe_intr - Legacy Interrupt Service Routine
3926  ************************************************************************/
3927 int
ixgbe_intr(void * arg)3928 ixgbe_intr(void *arg)
3929 {
3930 	struct ixgbe_softc     *sc = arg;
3931 	struct ix_rx_queue *que = sc->rx_queues;
3932 	struct ixgbe_hw    *hw = &sc->hw;
3933 	if_ctx_t           ctx = sc->ctx;
3934 	u32                eicr, eicr_mask;
3935 
3936 	eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
3937 
3938 	++que->irqs;
3939 	if (eicr == 0) {
3940 		ixgbe_if_enable_intr(ctx);
3941 		return (FILTER_HANDLED);
3942 	}
3943 
3944 	/* Check for fan failure */
3945 	if ((hw->device_id == IXGBE_DEV_ID_82598AT) &&
3946 	    (eicr & IXGBE_EICR_GPI_SDP1)) {
3947 		device_printf(sc->dev,
3948 		    "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
3949 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3950 	}
3951 
3952 	/* Link status change */
3953 	if (eicr & IXGBE_EICR_LSC) {
3954 		IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
3955 		iflib_admin_intr_deferred(ctx);
3956 	}
3957 
3958 	if (ixgbe_is_sfp(hw)) {
3959 		/* Pluggable optics-related interrupt */
3960 		if (hw->mac.type >= ixgbe_mac_X540)
3961 			eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
3962 		else
3963 			eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
3964 
3965 		if (eicr & eicr_mask) {
3966 			IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
3967 			sc->task_requests |= IXGBE_REQUEST_TASK_MOD;
3968 		}
3969 
3970 		if ((hw->mac.type == ixgbe_mac_82599EB) &&
3971 		    (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
3972 			IXGBE_WRITE_REG(hw, IXGBE_EICR,
3973 			    IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3974 			sc->task_requests |= IXGBE_REQUEST_TASK_MSF;
3975 		}
3976 	}
3977 
3978 	/* External PHY interrupt */
3979 	if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
3980 	    (eicr & IXGBE_EICR_GPI_SDP0_X540))
3981 		sc->task_requests |= IXGBE_REQUEST_TASK_PHY;
3982 
3983 	return (FILTER_SCHEDULE_THREAD);
3984 } /* ixgbe_intr */
3985 
3986 /************************************************************************
3987  * ixgbe_free_pci_resources
3988  ************************************************************************/
3989 static void
ixgbe_free_pci_resources(if_ctx_t ctx)3990 ixgbe_free_pci_resources(if_ctx_t ctx)
3991 {
3992 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
3993 	struct         ix_rx_queue *que = sc->rx_queues;
3994 	device_t       dev = iflib_get_dev(ctx);
3995 
3996 	/* Release all MSI-X queue resources */
3997 	if (sc->intr_type == IFLIB_INTR_MSIX)
3998 		iflib_irq_free(ctx, &sc->irq);
3999 
4000 	if (que != NULL) {
4001 		for (int i = 0; i < sc->num_rx_queues; i++, que++) {
4002 			iflib_irq_free(ctx, &que->que_irq);
4003 		}
4004 	}
4005 
4006 	if (sc->pci_mem != NULL)
4007 		bus_release_resource(dev, SYS_RES_MEMORY,
4008 		    rman_get_rid(sc->pci_mem), sc->pci_mem);
4009 } /* ixgbe_free_pci_resources */
4010 
4011 /************************************************************************
4012  * ixgbe_sysctl_flowcntl
4013  *
4014  *   SYSCTL wrapper around setting Flow Control
4015  ************************************************************************/
4016 static int
ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS)4017 ixgbe_sysctl_flowcntl(SYSCTL_HANDLER_ARGS)
4018 {
4019 	struct ixgbe_softc *sc;
4020 	int            error, fc;
4021 
4022 	sc = (struct ixgbe_softc *)arg1;
4023 	fc = sc->hw.fc.current_mode;
4024 
4025 	error = sysctl_handle_int(oidp, &fc, 0, req);
4026 	if ((error) || (req->newptr == NULL))
4027 		return (error);
4028 
4029 	/* Don't bother if it's not changed */
4030 	if (fc == sc->hw.fc.current_mode)
4031 		return (0);
4032 
4033 	return ixgbe_set_flowcntl(sc, fc);
4034 } /* ixgbe_sysctl_flowcntl */
4035 
4036 /************************************************************************
4037  * ixgbe_set_flowcntl - Set flow control
4038  *
4039  *   Flow control values:
4040  *     0 - off
4041  *     1 - rx pause
4042  *     2 - tx pause
4043  *     3 - full
4044  ************************************************************************/
4045 static int
ixgbe_set_flowcntl(struct ixgbe_softc * sc,int fc)4046 ixgbe_set_flowcntl(struct ixgbe_softc *sc, int fc)
4047 {
4048 	switch (fc) {
4049 	case ixgbe_fc_rx_pause:
4050 	case ixgbe_fc_tx_pause:
4051 	case ixgbe_fc_full:
4052 		sc->hw.fc.requested_mode = fc;
4053 		if (sc->num_rx_queues > 1)
4054 			ixgbe_disable_rx_drop(sc);
4055 		break;
4056 	case ixgbe_fc_none:
4057 		sc->hw.fc.requested_mode = ixgbe_fc_none;
4058 		if (sc->num_rx_queues > 1)
4059 			ixgbe_enable_rx_drop(sc);
4060 		break;
4061 	default:
4062 		return (EINVAL);
4063 	}
4064 
4065 	/* Don't autoneg if forcing a value */
4066 	sc->hw.fc.disable_fc_autoneg = true;
4067 	ixgbe_fc_enable(&sc->hw);
4068 
4069 	return (0);
4070 } /* ixgbe_set_flowcntl */
4071 
4072 /************************************************************************
4073  * ixgbe_enable_rx_drop
4074  *
4075  *   Enable the hardware to drop packets when the buffer is
4076  *   full. This is useful with multiqueue, so that no single
4077  *   queue being full stalls the entire RX engine. We only
4078  *   enable this when Multiqueue is enabled AND Flow Control
4079  *   is disabled.
4080  ************************************************************************/
4081 static void
ixgbe_enable_rx_drop(struct ixgbe_softc * sc)4082 ixgbe_enable_rx_drop(struct ixgbe_softc *sc)
4083 {
4084 	struct ixgbe_hw *hw = &sc->hw;
4085 	struct rx_ring  *rxr;
4086 	u32             srrctl;
4087 
4088 	for (int i = 0; i < sc->num_rx_queues; i++) {
4089 		rxr = &sc->rx_queues[i].rxr;
4090 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
4091 		srrctl |= IXGBE_SRRCTL_DROP_EN;
4092 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
4093 	}
4094 
4095 	/* enable drop for each vf */
4096 	for (int i = 0; i < sc->num_vfs; i++) {
4097 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
4098 		                (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT) |
4099 		                IXGBE_QDE_ENABLE));
4100 	}
4101 } /* ixgbe_enable_rx_drop */
4102 
4103 /************************************************************************
4104  * ixgbe_disable_rx_drop
4105  ************************************************************************/
4106 static void
ixgbe_disable_rx_drop(struct ixgbe_softc * sc)4107 ixgbe_disable_rx_drop(struct ixgbe_softc *sc)
4108 {
4109 	struct ixgbe_hw *hw = &sc->hw;
4110 	struct rx_ring  *rxr;
4111 	u32             srrctl;
4112 
4113 	for (int i = 0; i < sc->num_rx_queues; i++) {
4114 		rxr = &sc->rx_queues[i].rxr;
4115 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
4116 		srrctl &= ~IXGBE_SRRCTL_DROP_EN;
4117 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
4118 	}
4119 
4120 	/* disable drop for each vf */
4121 	for (int i = 0; i < sc->num_vfs; i++) {
4122 		IXGBE_WRITE_REG(hw, IXGBE_QDE,
4123 		    (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT)));
4124 	}
4125 } /* ixgbe_disable_rx_drop */
4126 
4127 /************************************************************************
4128  * ixgbe_sysctl_advertise
4129  *
4130  *   SYSCTL wrapper around setting advertised speed
4131  ************************************************************************/
4132 static int
ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS)4133 ixgbe_sysctl_advertise(SYSCTL_HANDLER_ARGS)
4134 {
4135 	struct ixgbe_softc *sc;
4136 	int            error, advertise;
4137 
4138 	sc = (struct ixgbe_softc *)arg1;
4139 	advertise = sc->advertise;
4140 
4141 	error = sysctl_handle_int(oidp, &advertise, 0, req);
4142 	if ((error) || (req->newptr == NULL))
4143 		return (error);
4144 
4145 	return ixgbe_set_advertise(sc, advertise);
4146 } /* ixgbe_sysctl_advertise */
4147 
4148 /************************************************************************
4149  * ixgbe_set_advertise - Control advertised link speed
4150  *
4151  *   Flags:
4152  *     0x1  - advertise 100 Mb
4153  *     0x2  - advertise 1G
4154  *     0x4  - advertise 10G
4155  *     0x8  - advertise 10 Mb (yes, Mb)
4156  *     0x10 - advertise 2.5G (disabled by default)
4157  *     0x20 - advertise 5G (disabled by default)
4158  *
4159  ************************************************************************/
4160 static int
ixgbe_set_advertise(struct ixgbe_softc * sc,int advertise)4161 ixgbe_set_advertise(struct ixgbe_softc *sc, int advertise)
4162 {
4163 	device_t         dev = iflib_get_dev(sc->ctx);
4164 	struct ixgbe_hw  *hw;
4165 	ixgbe_link_speed speed = 0;
4166 	ixgbe_link_speed link_caps = 0;
4167 	s32              err = IXGBE_NOT_IMPLEMENTED;
4168 	bool             negotiate = false;
4169 
4170 	/* Checks to validate new value */
4171 	if (sc->advertise == advertise) /* no change */
4172 		return (0);
4173 
4174 	hw = &sc->hw;
4175 
4176 	/* No speed changes for backplane media */
4177 	if (hw->phy.media_type == ixgbe_media_type_backplane)
4178 		return (ENODEV);
4179 
4180 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
4181 	      (hw->phy.multispeed_fiber))) {
4182 		device_printf(dev, "Advertised speed can only be set on copper or multispeed fiber media types.\n");
4183 		return (EINVAL);
4184 	}
4185 
4186 	if (advertise < 0x1 || advertise > 0x3F) {
4187 		device_printf(dev, "Invalid advertised speed; valid modes are 0x1 through 0x3F\n");
4188 		return (EINVAL);
4189 	}
4190 
4191 	if (hw->mac.ops.get_link_capabilities) {
4192 		err = hw->mac.ops.get_link_capabilities(hw, &link_caps,
4193 		    &negotiate);
4194 		if (err != IXGBE_SUCCESS) {
4195 			device_printf(dev, "Unable to determine supported advertise speeds\n");
4196 			return (ENODEV);
4197 		}
4198 	}
4199 
4200 	/* Set new value and report new advertised mode */
4201 	if (advertise & 0x1) {
4202 		if (!(link_caps & IXGBE_LINK_SPEED_100_FULL)) {
4203 			device_printf(dev, "Interface does not support 100Mb advertised speed\n");
4204 			return (EINVAL);
4205 		}
4206 		speed |= IXGBE_LINK_SPEED_100_FULL;
4207 	}
4208 	if (advertise & 0x2) {
4209 		if (!(link_caps & IXGBE_LINK_SPEED_1GB_FULL)) {
4210 			device_printf(dev, "Interface does not support 1Gb advertised speed\n");
4211 			return (EINVAL);
4212 		}
4213 		speed |= IXGBE_LINK_SPEED_1GB_FULL;
4214 	}
4215 	if (advertise & 0x4) {
4216 		if (!(link_caps & IXGBE_LINK_SPEED_10GB_FULL)) {
4217 			device_printf(dev, "Interface does not support 10Gb advertised speed\n");
4218 			return (EINVAL);
4219 		}
4220 		speed |= IXGBE_LINK_SPEED_10GB_FULL;
4221 	}
4222 	if (advertise & 0x8) {
4223 		if (!(link_caps & IXGBE_LINK_SPEED_10_FULL)) {
4224 			device_printf(dev, "Interface does not support 10Mb advertised speed\n");
4225 			return (EINVAL);
4226 		}
4227 		speed |= IXGBE_LINK_SPEED_10_FULL;
4228 	}
4229 	if (advertise & 0x10) {
4230 		if (!(link_caps & IXGBE_LINK_SPEED_2_5GB_FULL)) {
4231 			device_printf(dev, "Interface does not support 2.5G advertised speed\n");
4232 			return (EINVAL);
4233 		}
4234 		speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
4235 	}
4236 	if (advertise & 0x20) {
4237 		if (!(link_caps & IXGBE_LINK_SPEED_5GB_FULL)) {
4238 			device_printf(dev, "Interface does not support 5G advertised speed\n");
4239 			return (EINVAL);
4240 		}
4241 		speed |= IXGBE_LINK_SPEED_5GB_FULL;
4242 	}
4243 
4244 	hw->mac.autotry_restart = true;
4245 	hw->mac.ops.setup_link(hw, speed, true);
4246 	sc->advertise = advertise;
4247 
4248 	return (0);
4249 } /* ixgbe_set_advertise */
4250 
4251 /************************************************************************
4252  * ixgbe_get_default_advertise - Get default advertised speed settings
4253  *
4254  *   Formatted for sysctl usage.
4255  *   Flags:
4256  *     0x1 - advertise 100 Mb
4257  *     0x2 - advertise 1G
4258  *     0x4 - advertise 10G
4259  *     0x8 - advertise 10 Mb (yes, Mb)
4260  *     0x10 - advertise 2.5G (disabled by default)
4261  *     0x20 - advertise 5G (disabled by default)
4262  ************************************************************************/
4263 static int
ixgbe_get_default_advertise(struct ixgbe_softc * sc)4264 ixgbe_get_default_advertise(struct ixgbe_softc *sc)
4265 {
4266 	struct ixgbe_hw  *hw = &sc->hw;
4267 	int              speed;
4268 	ixgbe_link_speed link_caps = 0;
4269 	s32              err;
4270 	bool             negotiate = false;
4271 
4272 	/*
4273 	 * Advertised speed means nothing unless it's copper or
4274 	 * multi-speed fiber
4275 	 */
4276 	if (!(hw->phy.media_type == ixgbe_media_type_copper) &&
4277 	    !(hw->phy.multispeed_fiber))
4278 		return (0);
4279 
4280 	err = hw->mac.ops.get_link_capabilities(hw, &link_caps, &negotiate);
4281 	if (err != IXGBE_SUCCESS)
4282 		return (0);
4283 
4284 	if (hw->mac.type == ixgbe_mac_X550) {
4285 		/*
4286 		 * 2.5G and 5G autonegotiation speeds on X550
4287 		 * are disabled by default due to reported
4288 		 * interoperability issues with some switches.
4289 		 */
4290 		link_caps &= ~(IXGBE_LINK_SPEED_2_5GB_FULL |
4291 		    IXGBE_LINK_SPEED_5GB_FULL);
4292 	}
4293 
4294 	speed =
4295 	    ((link_caps & IXGBE_LINK_SPEED_10GB_FULL)  ? 0x4  : 0) |
4296 	    ((link_caps & IXGBE_LINK_SPEED_5GB_FULL)   ? 0x20 : 0) |
4297 	    ((link_caps & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
4298 	    ((link_caps & IXGBE_LINK_SPEED_1GB_FULL)   ? 0x2  : 0) |
4299 	    ((link_caps & IXGBE_LINK_SPEED_100_FULL)   ? 0x1  : 0) |
4300 	    ((link_caps & IXGBE_LINK_SPEED_10_FULL)    ? 0x8  : 0);
4301 
4302 	return speed;
4303 } /* ixgbe_get_default_advertise */
4304 
4305 /************************************************************************
4306  * ixgbe_sysctl_dmac - Manage DMA Coalescing
4307  *
4308  *   Control values:
4309  *     0/1 - off / on (use default value of 1000)
4310  *
4311  *     Legal timer values are:
4312  *     50,100,250,500,1000,2000,5000,10000
4313  *
4314  *     Turning off interrupt moderation will also turn this off.
4315  ************************************************************************/
4316 static int
ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS)4317 ixgbe_sysctl_dmac(SYSCTL_HANDLER_ARGS)
4318 {
4319 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4320 	struct ifnet   *ifp = iflib_get_ifp(sc->ctx);
4321 	int            error;
4322 	u16            newval;
4323 
4324 	newval = sc->dmac;
4325 	error = sysctl_handle_16(oidp, &newval, 0, req);
4326 	if ((error) || (req->newptr == NULL))
4327 		return (error);
4328 
4329 	switch (newval) {
4330 	case 0:
4331 		/* Disabled */
4332 		sc->dmac = 0;
4333 		break;
4334 	case 1:
4335 		/* Enable and use default */
4336 		sc->dmac = 1000;
4337 		break;
4338 	case 50:
4339 	case 100:
4340 	case 250:
4341 	case 500:
4342 	case 1000:
4343 	case 2000:
4344 	case 5000:
4345 	case 10000:
4346 		/* Legal values - allow */
4347 		sc->dmac = newval;
4348 		break;
4349 	default:
4350 		/* Do nothing, illegal value */
4351 		return (EINVAL);
4352 	}
4353 
4354 	/* Re-initialize hardware if it's already running */
4355 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4356 		ifp->if_init(ifp);
4357 
4358 	return (0);
4359 } /* ixgbe_sysctl_dmac */
4360 
4361 #ifdef IXGBE_DEBUG
4362 /************************************************************************
4363  * ixgbe_sysctl_power_state
4364  *
4365  *   Sysctl to test power states
4366  *   Values:
4367  *     0      - set device to D0
4368  *     3      - set device to D3
4369  *     (none) - get current device power state
4370  ************************************************************************/
4371 static int
ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS)4372 ixgbe_sysctl_power_state(SYSCTL_HANDLER_ARGS)
4373 {
4374 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4375 	device_t       dev = sc->dev;
4376 	int            curr_ps, new_ps, error = 0;
4377 
4378 	curr_ps = new_ps = pci_get_powerstate(dev);
4379 
4380 	error = sysctl_handle_int(oidp, &new_ps, 0, req);
4381 	if ((error) || (req->newptr == NULL))
4382 		return (error);
4383 
4384 	if (new_ps == curr_ps)
4385 		return (0);
4386 
4387 	if (new_ps == 3 && curr_ps == 0)
4388 		error = DEVICE_SUSPEND(dev);
4389 	else if (new_ps == 0 && curr_ps == 3)
4390 		error = DEVICE_RESUME(dev);
4391 	else
4392 		return (EINVAL);
4393 
4394 	device_printf(dev, "New state: %d\n", pci_get_powerstate(dev));
4395 
4396 	return (error);
4397 } /* ixgbe_sysctl_power_state */
4398 #endif
4399 
4400 /************************************************************************
4401  * ixgbe_sysctl_wol_enable
4402  *
4403  *   Sysctl to enable/disable the WoL capability,
4404  *   if supported by the adapter.
4405  *
4406  *   Values:
4407  *     0 - disabled
4408  *     1 - enabled
4409  ************************************************************************/
4410 static int
ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS)4411 ixgbe_sysctl_wol_enable(SYSCTL_HANDLER_ARGS)
4412 {
4413 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4414 	struct ixgbe_hw *hw = &sc->hw;
4415 	int             new_wol_enabled;
4416 	int             error = 0;
4417 
4418 	new_wol_enabled = hw->wol_enabled;
4419 	error = sysctl_handle_int(oidp, &new_wol_enabled, 0, req);
4420 	if ((error) || (req->newptr == NULL))
4421 		return (error);
4422 	new_wol_enabled = !!(new_wol_enabled);
4423 	if (new_wol_enabled == hw->wol_enabled)
4424 		return (0);
4425 
4426 	if (new_wol_enabled > 0 && !sc->wol_support)
4427 		return (ENODEV);
4428 	else
4429 		hw->wol_enabled = new_wol_enabled;
4430 
4431 	return (0);
4432 } /* ixgbe_sysctl_wol_enable */
4433 
4434 /************************************************************************
4435  * ixgbe_sysctl_wufc - Wake Up Filter Control
4436  *
4437  *   Sysctl to enable/disable the types of packets that the
4438  *   adapter will wake up on upon receipt.
4439  *   Flags:
4440  *     0x1  - Link Status Change
4441  *     0x2  - Magic Packet
4442  *     0x4  - Direct Exact
4443  *     0x8  - Directed Multicast
4444  *     0x10 - Broadcast
4445  *     0x20 - ARP/IPv4 Request Packet
4446  *     0x40 - Direct IPv4 Packet
4447  *     0x80 - Direct IPv6 Packet
4448  *
4449  *   Settings not listed above will cause the sysctl to return an error.
4450  ************************************************************************/
4451 static int
ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS)4452 ixgbe_sysctl_wufc(SYSCTL_HANDLER_ARGS)
4453 {
4454 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4455 	int            error = 0;
4456 	u32            new_wufc;
4457 
4458 	new_wufc = sc->wufc;
4459 
4460 	error = sysctl_handle_32(oidp, &new_wufc, 0, req);
4461 	if ((error) || (req->newptr == NULL))
4462 		return (error);
4463 	if (new_wufc == sc->wufc)
4464 		return (0);
4465 
4466 	if (new_wufc & 0xffffff00)
4467 		return (EINVAL);
4468 
4469 	new_wufc &= 0xff;
4470 	new_wufc |= (0xffffff & sc->wufc);
4471 	sc->wufc = new_wufc;
4472 
4473 	return (0);
4474 } /* ixgbe_sysctl_wufc */
4475 
4476 #ifdef IXGBE_DEBUG
4477 /************************************************************************
4478  * ixgbe_sysctl_print_rss_config
4479  ************************************************************************/
4480 static int
ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS)4481 ixgbe_sysctl_print_rss_config(SYSCTL_HANDLER_ARGS)
4482 {
4483 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4484 	struct ixgbe_hw *hw = &sc->hw;
4485 	device_t        dev = sc->dev;
4486 	struct sbuf     *buf;
4487 	int             error = 0, reta_size;
4488 	u32             reg;
4489 
4490 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
4491 	if (!buf) {
4492 		device_printf(dev, "Could not allocate sbuf for output.\n");
4493 		return (ENOMEM);
4494 	}
4495 
4496 	// TODO: use sbufs to make a string to print out
4497 	/* Set multiplier for RETA setup and table size based on MAC */
4498 	switch (sc->hw.mac.type) {
4499 	case ixgbe_mac_X550:
4500 	case ixgbe_mac_X550EM_x:
4501 	case ixgbe_mac_X550EM_a:
4502 		reta_size = 128;
4503 		break;
4504 	default:
4505 		reta_size = 32;
4506 		break;
4507 	}
4508 
4509 	/* Print out the redirection table */
4510 	sbuf_cat(buf, "\n");
4511 	for (int i = 0; i < reta_size; i++) {
4512 		if (i < 32) {
4513 			reg = IXGBE_READ_REG(hw, IXGBE_RETA(i));
4514 			sbuf_printf(buf, "RETA(%2d): 0x%08x\n", i, reg);
4515 		} else {
4516 			reg = IXGBE_READ_REG(hw, IXGBE_ERETA(i - 32));
4517 			sbuf_printf(buf, "ERETA(%2d): 0x%08x\n", i - 32, reg);
4518 		}
4519 	}
4520 
4521 	// TODO: print more config
4522 
4523 	error = sbuf_finish(buf);
4524 	if (error)
4525 		device_printf(dev, "Error finishing sbuf: %d\n", error);
4526 
4527 	sbuf_delete(buf);
4528 
4529 	return (0);
4530 } /* ixgbe_sysctl_print_rss_config */
4531 #endif /* IXGBE_DEBUG */
4532 
4533 /************************************************************************
4534  * ixgbe_sysctl_phy_temp - Retrieve temperature of PHY
4535  *
4536  *   For X552/X557-AT devices using an external PHY
4537  ************************************************************************/
4538 static int
ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS)4539 ixgbe_sysctl_phy_temp(SYSCTL_HANDLER_ARGS)
4540 {
4541 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4542 	struct ixgbe_hw *hw = &sc->hw;
4543 	u16             reg;
4544 
4545 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4546 		device_printf(iflib_get_dev(sc->ctx),
4547 		    "Device has no supported external thermal sensor.\n");
4548 		return (ENODEV);
4549 	}
4550 
4551 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_CURRENT_TEMP,
4552 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4553 		device_printf(iflib_get_dev(sc->ctx),
4554 		    "Error reading from PHY's current temperature register\n");
4555 		return (EAGAIN);
4556 	}
4557 
4558 	/* Shift temp for output */
4559 	reg = reg >> 8;
4560 
4561 	return (sysctl_handle_16(oidp, NULL, reg, req));
4562 } /* ixgbe_sysctl_phy_temp */
4563 
4564 /************************************************************************
4565  * ixgbe_sysctl_phy_overtemp_occurred
4566  *
4567  *   Reports (directly from the PHY) whether the current PHY
4568  *   temperature is over the overtemp threshold.
4569  ************************************************************************/
4570 static int
ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS)4571 ixgbe_sysctl_phy_overtemp_occurred(SYSCTL_HANDLER_ARGS)
4572 {
4573 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4574 	struct ixgbe_hw *hw = &sc->hw;
4575 	u16             reg;
4576 
4577 	if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
4578 		device_printf(iflib_get_dev(sc->ctx),
4579 		    "Device has no supported external thermal sensor.\n");
4580 		return (ENODEV);
4581 	}
4582 
4583 	if (hw->phy.ops.read_reg(hw, IXGBE_PHY_OVERTEMP_STATUS,
4584 	    IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, &reg)) {
4585 		device_printf(iflib_get_dev(sc->ctx),
4586 		    "Error reading from PHY's temperature status register\n");
4587 		return (EAGAIN);
4588 	}
4589 
4590 	/* Get occurrence bit */
4591 	reg = !!(reg & 0x4000);
4592 
4593 	return (sysctl_handle_16(oidp, 0, reg, req));
4594 } /* ixgbe_sysctl_phy_overtemp_occurred */
4595 
4596 /************************************************************************
4597  * ixgbe_sysctl_eee_state
4598  *
4599  *   Sysctl to set EEE power saving feature
4600  *   Values:
4601  *     0      - disable EEE
4602  *     1      - enable EEE
4603  *     (none) - get current device EEE state
4604  ************************************************************************/
4605 static int
ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS)4606 ixgbe_sysctl_eee_state(SYSCTL_HANDLER_ARGS)
4607 {
4608 	struct ixgbe_softc *sc = (struct ixgbe_softc *)arg1;
4609 	device_t       dev = sc->dev;
4610 	struct ifnet   *ifp = iflib_get_ifp(sc->ctx);
4611 	int            curr_eee, new_eee, error = 0;
4612 	s32            retval;
4613 
4614 	curr_eee = new_eee = !!(sc->feat_en & IXGBE_FEATURE_EEE);
4615 
4616 	error = sysctl_handle_int(oidp, &new_eee, 0, req);
4617 	if ((error) || (req->newptr == NULL))
4618 		return (error);
4619 
4620 	/* Nothing to do */
4621 	if (new_eee == curr_eee)
4622 		return (0);
4623 
4624 	/* Not supported */
4625 	if (!(sc->feat_cap & IXGBE_FEATURE_EEE))
4626 		return (EINVAL);
4627 
4628 	/* Bounds checking */
4629 	if ((new_eee < 0) || (new_eee > 1))
4630 		return (EINVAL);
4631 
4632 	retval = ixgbe_setup_eee(&sc->hw, new_eee);
4633 	if (retval) {
4634 		device_printf(dev, "Error in EEE setup: 0x%08X\n", retval);
4635 		return (EINVAL);
4636 	}
4637 
4638 	/* Restart auto-neg */
4639 	ifp->if_init(ifp);
4640 
4641 	device_printf(dev, "New EEE state: %d\n", new_eee);
4642 
4643 	/* Cache new value */
4644 	if (new_eee)
4645 		sc->feat_en |= IXGBE_FEATURE_EEE;
4646 	else
4647 		sc->feat_en &= ~IXGBE_FEATURE_EEE;
4648 
4649 	return (error);
4650 } /* ixgbe_sysctl_eee_state */
4651 
4652 /************************************************************************
4653  * ixgbe_init_device_features
4654  ************************************************************************/
4655 static void
ixgbe_init_device_features(struct ixgbe_softc * sc)4656 ixgbe_init_device_features(struct ixgbe_softc *sc)
4657 {
4658 	sc->feat_cap = IXGBE_FEATURE_NETMAP
4659 	                  | IXGBE_FEATURE_RSS
4660 	                  | IXGBE_FEATURE_MSI
4661 	                  | IXGBE_FEATURE_MSIX
4662 	                  | IXGBE_FEATURE_LEGACY_IRQ;
4663 
4664 	/* Set capabilities first... */
4665 	switch (sc->hw.mac.type) {
4666 	case ixgbe_mac_82598EB:
4667 		if (sc->hw.device_id == IXGBE_DEV_ID_82598AT)
4668 			sc->feat_cap |= IXGBE_FEATURE_FAN_FAIL;
4669 		break;
4670 	case ixgbe_mac_X540:
4671 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4672 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4673 		if ((sc->hw.device_id == IXGBE_DEV_ID_X540_BYPASS) &&
4674 		    (sc->hw.bus.func == 0))
4675 			sc->feat_cap |= IXGBE_FEATURE_BYPASS;
4676 		break;
4677 	case ixgbe_mac_X550:
4678 		sc->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
4679 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4680 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4681 		break;
4682 	case ixgbe_mac_X550EM_x:
4683 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4684 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4685 		break;
4686 	case ixgbe_mac_X550EM_a:
4687 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4688 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4689 		sc->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
4690 		if ((sc->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T) ||
4691 		    (sc->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T_L)) {
4692 			sc->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
4693 			sc->feat_cap |= IXGBE_FEATURE_EEE;
4694 		}
4695 		break;
4696 	case ixgbe_mac_82599EB:
4697 		sc->feat_cap |= IXGBE_FEATURE_SRIOV;
4698 		sc->feat_cap |= IXGBE_FEATURE_FDIR;
4699 		if ((sc->hw.device_id == IXGBE_DEV_ID_82599_BYPASS) &&
4700 		    (sc->hw.bus.func == 0))
4701 			sc->feat_cap |= IXGBE_FEATURE_BYPASS;
4702 		if (sc->hw.device_id == IXGBE_DEV_ID_82599_QSFP_SF_QP)
4703 			sc->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
4704 		break;
4705 	default:
4706 		break;
4707 	}
4708 
4709 	/* Enabled by default... */
4710 	/* Fan failure detection */
4711 	if (sc->feat_cap & IXGBE_FEATURE_FAN_FAIL)
4712 		sc->feat_en |= IXGBE_FEATURE_FAN_FAIL;
4713 	/* Netmap */
4714 	if (sc->feat_cap & IXGBE_FEATURE_NETMAP)
4715 		sc->feat_en |= IXGBE_FEATURE_NETMAP;
4716 	/* EEE */
4717 	if (sc->feat_cap & IXGBE_FEATURE_EEE)
4718 		sc->feat_en |= IXGBE_FEATURE_EEE;
4719 	/* Thermal Sensor */
4720 	if (sc->feat_cap & IXGBE_FEATURE_TEMP_SENSOR)
4721 		sc->feat_en |= IXGBE_FEATURE_TEMP_SENSOR;
4722 
4723 	/* Enabled via global sysctl... */
4724 	/* Flow Director */
4725 	if (ixgbe_enable_fdir) {
4726 		if (sc->feat_cap & IXGBE_FEATURE_FDIR)
4727 			sc->feat_en |= IXGBE_FEATURE_FDIR;
4728 		else
4729 			device_printf(sc->dev, "Device does not support Flow Director. Leaving disabled.");
4730 	}
4731 	/*
4732 	 * Message Signal Interrupts - Extended (MSI-X)
4733 	 * Normal MSI is only enabled if MSI-X calls fail.
4734 	 */
4735 	if (!ixgbe_enable_msix)
4736 		sc->feat_cap &= ~IXGBE_FEATURE_MSIX;
4737 	/* Receive-Side Scaling (RSS) */
4738 	if ((sc->feat_cap & IXGBE_FEATURE_RSS) && ixgbe_enable_rss)
4739 		sc->feat_en |= IXGBE_FEATURE_RSS;
4740 
4741 	/* Disable features with unmet dependencies... */
4742 	/* No MSI-X */
4743 	if (!(sc->feat_cap & IXGBE_FEATURE_MSIX)) {
4744 		sc->feat_cap &= ~IXGBE_FEATURE_RSS;
4745 		sc->feat_cap &= ~IXGBE_FEATURE_SRIOV;
4746 		sc->feat_en &= ~IXGBE_FEATURE_RSS;
4747 		sc->feat_en &= ~IXGBE_FEATURE_SRIOV;
4748 	}
4749 } /* ixgbe_init_device_features */
4750 
4751 /************************************************************************
4752  * ixgbe_check_fan_failure
4753  ************************************************************************/
4754 static void
ixgbe_check_fan_failure(struct ixgbe_softc * sc,u32 reg,bool in_interrupt)4755 ixgbe_check_fan_failure(struct ixgbe_softc *sc, u32 reg, bool in_interrupt)
4756 {
4757 	u32 mask;
4758 
4759 	mask = (in_interrupt) ? IXGBE_EICR_GPI_SDP1_BY_MAC(&sc->hw) :
4760 	    IXGBE_ESDP_SDP1;
4761 
4762 	if (reg & mask)
4763 		device_printf(sc->dev, "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
4764 } /* ixgbe_check_fan_failure */
4765 
4766 /************************************************************************
4767  * ixgbe_sbuf_fw_version
4768  ************************************************************************/
4769 static void
ixgbe_sbuf_fw_version(struct ixgbe_hw * hw,struct sbuf * buf)4770 ixgbe_sbuf_fw_version(struct ixgbe_hw *hw, struct sbuf *buf)
4771 {
4772 	struct ixgbe_nvm_version nvm_ver = {0};
4773 	uint16_t phyfw = 0;
4774 	int status;
4775 	const char *space = "";
4776 
4777 	ixgbe_get_oem_prod_version(hw, &nvm_ver); /* OEM's NVM version */
4778 	ixgbe_get_orom_version(hw, &nvm_ver); /* Option ROM */
4779 	ixgbe_get_etk_id(hw, &nvm_ver); /* eTrack identifies a build in Intel's SCM */
4780 	status = ixgbe_get_phy_firmware_version(hw, &phyfw);
4781 
4782 	if (nvm_ver.oem_valid) {
4783 		sbuf_printf(buf, "NVM OEM V%d.%d R%d", nvm_ver.oem_major,
4784 		    nvm_ver.oem_minor, nvm_ver.oem_release);
4785 		space = " ";
4786 	}
4787 
4788 	if (nvm_ver.or_valid) {
4789 		sbuf_printf(buf, "%sOption ROM V%d-b%d-p%d",
4790 		    space, nvm_ver.or_major, nvm_ver.or_build, nvm_ver.or_patch);
4791 		space = " ";
4792 	}
4793 
4794 	if (nvm_ver.etk_id != ((NVM_VER_INVALID << NVM_ETK_SHIFT) |
4795 	    NVM_VER_INVALID)) {
4796 		sbuf_printf(buf, "%seTrack 0x%08x", space, nvm_ver.etk_id);
4797 		space = " ";
4798 	}
4799 
4800 	if (phyfw != 0 && status == IXGBE_SUCCESS)
4801 		sbuf_printf(buf, "%sPHY FW V%d", space, phyfw);
4802 } /* ixgbe_sbuf_fw_version */
4803 
4804 /************************************************************************
4805  * ixgbe_print_fw_version
4806  ************************************************************************/
4807 static void
ixgbe_print_fw_version(if_ctx_t ctx)4808 ixgbe_print_fw_version(if_ctx_t ctx)
4809 {
4810 	struct ixgbe_softc *sc = iflib_get_softc(ctx);
4811 	struct ixgbe_hw *hw = &sc->hw;
4812 	device_t dev = sc->dev;
4813 	struct sbuf *buf;
4814 	int error = 0;
4815 
4816 	buf = sbuf_new_auto();
4817 	if (!buf) {
4818 		device_printf(dev, "Could not allocate sbuf for output.\n");
4819 		return;
4820 	}
4821 
4822 	ixgbe_sbuf_fw_version(hw, buf);
4823 
4824 	error = sbuf_finish(buf);
4825 	if (error)
4826 		device_printf(dev, "Error finishing sbuf: %d\n", error);
4827 	else if (sbuf_len(buf))
4828 		device_printf(dev, "%s\n", sbuf_data(buf));
4829 
4830 	sbuf_delete(buf);
4831 } /* ixgbe_print_fw_version */
4832 
4833 /************************************************************************
4834  * ixgbe_sysctl_print_fw_version
4835  ************************************************************************/
4836 static int
ixgbe_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)4837 ixgbe_sysctl_print_fw_version(SYSCTL_HANDLER_ARGS)
4838 {
4839 	struct ixgbe_softc  *sc = (struct ixgbe_softc *)arg1;
4840 	struct ixgbe_hw *hw = &sc->hw;
4841 	device_t dev = sc->dev;
4842 	struct sbuf *buf;
4843 	int error = 0;
4844 
4845 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
4846 	if (!buf) {
4847 		device_printf(dev, "Could not allocate sbuf for output.\n");
4848 		return (ENOMEM);
4849 	}
4850 
4851 	ixgbe_sbuf_fw_version(hw, buf);
4852 
4853 	error = sbuf_finish(buf);
4854 	if (error)
4855 		device_printf(dev, "Error finishing sbuf: %d\n", error);
4856 
4857 	sbuf_delete(buf);
4858 
4859 	return (0);
4860 } /* ixgbe_sysctl_print_fw_version */
4861