ixgbe.c revision 1.222 1 /* $NetBSD: ixgbe.c,v 1.222 2020/02/01 12:55:22 thorpej Exp $ */
2
3 /******************************************************************************
4
5 Copyright (c) 2001-2017, Intel Corporation
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 1. Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13
14 2. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17
18 3. Neither the name of the Intel Corporation nor the names of its
19 contributors may be used to endorse or promote products derived from
20 this software without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 POSSIBILITY OF SUCH DAMAGE.
33
34 ******************************************************************************/
35 /*$FreeBSD: head/sys/dev/ixgbe/if_ix.c 331224 2018-03-19 20:55:05Z erj $*/
36
37 /*
38 * Copyright (c) 2011 The NetBSD Foundation, Inc.
39 * All rights reserved.
40 *
41 * This code is derived from software contributed to The NetBSD Foundation
42 * by Coyote Point Systems, Inc.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 * POSSIBILITY OF SUCH DAMAGE.
64 */
65
66 #ifdef _KERNEL_OPT
67 #include "opt_inet.h"
68 #include "opt_inet6.h"
69 #include "opt_net_mpsafe.h"
70 #endif
71
72 #include "ixgbe.h"
73 #include "ixgbe_sriov.h"
74 #include "vlan.h"
75
76 #include <sys/cprng.h>
77 #include <dev/mii/mii.h>
78 #include <dev/mii/miivar.h>
79
80 /************************************************************************
81 * Driver version
82 ************************************************************************/
83 static const char ixgbe_driver_version[] = "4.0.1-k";
84 /* XXX NetBSD: + 3.3.10 */
85
86 /************************************************************************
87 * PCI Device ID Table
88 *
89 * Used by probe to select devices to load on
90 * Last field stores an index into ixgbe_strings
91 * Last entry must be all 0s
92 *
93 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
94 ************************************************************************/
95 static const ixgbe_vendor_info_t ixgbe_vendor_info_array[] =
96 {
97 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_DUAL_PORT, 0, 0, 0},
98 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_SINGLE_PORT, 0, 0, 0},
99 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_CX4, 0, 0, 0},
100 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT, 0, 0, 0},
101 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT2, 0, 0, 0},
102 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598, 0, 0, 0},
103 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_BX, 0, 0, 0},
104 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_DA_DUAL_PORT, 0, 0, 0},
105 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_CX4_DUAL_PORT, 0, 0, 0},
106 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_XF_LR, 0, 0, 0},
107 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM, 0, 0, 0},
108 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_SFP_LOM, 0, 0, 0},
109 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KR, 0, 0, 0},
110 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4, 0, 0, 0},
111 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4_MEZZ, 0, 0, 0},
112 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP, 0, 0, 0},
113 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_EM, 0, 0, 0},
114 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_XAUI_LOM, 0, 0, 0},
115 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_CX4, 0, 0, 0},
116 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_T3_LOM, 0, 0, 0},
117 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_COMBO_BACKPLANE, 0, 0, 0},
118 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BACKPLANE_FCOE, 0, 0, 0},
119 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF2, 0, 0, 0},
120 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_FCOE, 0, 0, 0},
121 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599EN_SFP, 0, 0, 0},
122 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF_QP, 0, 0, 0},
123 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_QSFP_SF_QP, 0, 0, 0},
124 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T, 0, 0, 0},
125 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T1, 0, 0, 0},
126 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T, 0, 0, 0},
127 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550T1, 0, 0, 0},
128 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KR, 0, 0, 0},
129 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_KX4, 0, 0, 0},
130 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_10G_T, 0, 0, 0},
131 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_1G_T, 0, 0, 0},
132 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_SFP, 0, 0, 0},
133 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_X_XFI, 0, 0, 0},
134 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR, 0, 0, 0},
135 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_KR_L, 0, 0, 0},
136 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_QSFP, 0, 0, 0},
137 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_QSFP_N, 0, 0, 0},
138 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP, 0, 0, 0},
139 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SFP_N, 0, 0, 0},
140 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII, 0, 0, 0},
141 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_SGMII_L, 0, 0, 0},
142 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_10G_T, 0, 0, 0},
143 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T, 0, 0, 0},
144 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X550EM_A_1G_T_L, 0, 0, 0},
145 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540_BYPASS, 0, 0, 0},
146 {IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BYPASS, 0, 0, 0},
147 /* required last entry */
148 {0, 0, 0, 0, 0}
149 };
150
151 /************************************************************************
152 * Table of branding strings
153 ************************************************************************/
154 static const char *ixgbe_strings[] = {
155 "Intel(R) PRO/10GbE PCI-Express Network Driver"
156 };
157
158 /************************************************************************
159 * Function prototypes
160 ************************************************************************/
161 static int ixgbe_probe(device_t, cfdata_t, void *);
162 static void ixgbe_attach(device_t, device_t, void *);
163 static int ixgbe_detach(device_t, int);
164 #if 0
165 static int ixgbe_shutdown(device_t);
166 #endif
167 static bool ixgbe_suspend(device_t, const pmf_qual_t *);
168 static bool ixgbe_resume(device_t, const pmf_qual_t *);
169 static int ixgbe_ifflags_cb(struct ethercom *);
170 static int ixgbe_ioctl(struct ifnet *, u_long, void *);
171 static void ixgbe_ifstop(struct ifnet *, int);
172 static int ixgbe_init(struct ifnet *);
173 static void ixgbe_init_locked(struct adapter *);
174 static void ixgbe_stop(void *);
175 static void ixgbe_init_device_features(struct adapter *);
176 static void ixgbe_check_fan_failure(struct adapter *, u32, bool);
177 static void ixgbe_add_media_types(struct adapter *);
178 static void ixgbe_media_status(struct ifnet *, struct ifmediareq *);
179 static int ixgbe_media_change(struct ifnet *);
180 static int ixgbe_allocate_pci_resources(struct adapter *,
181 const struct pci_attach_args *);
182 static void ixgbe_free_softint(struct adapter *);
183 static void ixgbe_get_slot_info(struct adapter *);
184 static int ixgbe_allocate_msix(struct adapter *,
185 const struct pci_attach_args *);
186 static int ixgbe_allocate_legacy(struct adapter *,
187 const struct pci_attach_args *);
188 static int ixgbe_configure_interrupts(struct adapter *);
189 static void ixgbe_free_pciintr_resources(struct adapter *);
190 static void ixgbe_free_pci_resources(struct adapter *);
191 static void ixgbe_local_timer(void *);
192 static void ixgbe_local_timer1(void *);
193 static void ixgbe_recovery_mode_timer(void *);
194 static int ixgbe_setup_interface(device_t, struct adapter *);
195 static void ixgbe_config_gpie(struct adapter *);
196 static void ixgbe_config_dmac(struct adapter *);
197 static void ixgbe_config_delay_values(struct adapter *);
198 static void ixgbe_config_link(struct adapter *);
199 static void ixgbe_check_wol_support(struct adapter *);
200 static int ixgbe_setup_low_power_mode(struct adapter *);
201 #if 0
202 static void ixgbe_rearm_queues(struct adapter *, u64);
203 #endif
204
205 static void ixgbe_initialize_transmit_units(struct adapter *);
206 static void ixgbe_initialize_receive_units(struct adapter *);
207 static void ixgbe_enable_rx_drop(struct adapter *);
208 static void ixgbe_disable_rx_drop(struct adapter *);
209 static void ixgbe_initialize_rss_mapping(struct adapter *);
210
211 static void ixgbe_enable_intr(struct adapter *);
212 static void ixgbe_disable_intr(struct adapter *);
213 static void ixgbe_update_stats_counters(struct adapter *);
214 static void ixgbe_set_rxfilter(struct adapter *);
215 static void ixgbe_update_link_status(struct adapter *);
216 static void ixgbe_set_ivar(struct adapter *, u8, u8, s8);
217 static void ixgbe_configure_ivars(struct adapter *);
218 static u8 * ixgbe_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
219 static void ixgbe_eitr_write(struct adapter *, uint32_t, uint32_t);
220
221 static void ixgbe_setup_vlan_hw_tagging(struct adapter *);
222 static void ixgbe_setup_vlan_hw_support(struct adapter *);
223 static int ixgbe_vlan_cb(struct ethercom *, uint16_t, bool);
224 static int ixgbe_register_vlan(struct adapter *, u16);
225 static int ixgbe_unregister_vlan(struct adapter *, u16);
226
227 static void ixgbe_add_device_sysctls(struct adapter *);
228 static void ixgbe_add_hw_stats(struct adapter *);
229 static void ixgbe_clear_evcnt(struct adapter *);
230 static int ixgbe_set_flowcntl(struct adapter *, int);
231 static int ixgbe_set_advertise(struct adapter *, int);
232 static int ixgbe_get_advertise(struct adapter *);
233
234 /* Sysctl handlers */
235 static void ixgbe_set_sysctl_value(struct adapter *, const char *,
236 const char *, int *, int);
237 static int ixgbe_sysctl_flowcntl(SYSCTLFN_PROTO);
238 static int ixgbe_sysctl_advertise(SYSCTLFN_PROTO);
239 static int ixgbe_sysctl_interrupt_rate_handler(SYSCTLFN_PROTO);
240 static int ixgbe_sysctl_dmac(SYSCTLFN_PROTO);
241 static int ixgbe_sysctl_phy_temp(SYSCTLFN_PROTO);
242 static int ixgbe_sysctl_phy_overtemp_occurred(SYSCTLFN_PROTO);
243 #ifdef IXGBE_DEBUG
244 static int ixgbe_sysctl_power_state(SYSCTLFN_PROTO);
245 static int ixgbe_sysctl_print_rss_config(SYSCTLFN_PROTO);
246 #endif
247 static int ixgbe_sysctl_next_to_check_handler(SYSCTLFN_PROTO);
248 static int ixgbe_sysctl_rdh_handler(SYSCTLFN_PROTO);
249 static int ixgbe_sysctl_rdt_handler(SYSCTLFN_PROTO);
250 static int ixgbe_sysctl_tdt_handler(SYSCTLFN_PROTO);
251 static int ixgbe_sysctl_tdh_handler(SYSCTLFN_PROTO);
252 static int ixgbe_sysctl_eee_state(SYSCTLFN_PROTO);
253 static int ixgbe_sysctl_debug(SYSCTLFN_PROTO);
254 static int ixgbe_sysctl_wol_enable(SYSCTLFN_PROTO);
255 static int ixgbe_sysctl_wufc(SYSCTLFN_PROTO);
256
257 /* Support for pluggable optic modules */
258 static bool ixgbe_sfp_probe(struct adapter *);
259
260 /* Legacy (single vector) interrupt handler */
261 static int ixgbe_legacy_irq(void *);
262
263 /* The MSI/MSI-X Interrupt handlers */
264 static int ixgbe_msix_que(void *);
265 static int ixgbe_msix_link(void *);
266
267 /* Software interrupts for deferred work */
268 static void ixgbe_handle_que(void *);
269 static void ixgbe_handle_link(void *);
270 static void ixgbe_handle_msf(void *);
271 static void ixgbe_handle_mod(void *);
272 static void ixgbe_handle_phy(void *);
273
274 /* Workqueue handler for deferred work */
275 static void ixgbe_handle_que_work(struct work *, void *);
276
277 static const ixgbe_vendor_info_t *ixgbe_lookup(const struct pci_attach_args *);
278
279 /************************************************************************
280 * NetBSD Device Interface Entry Points
281 ************************************************************************/
282 CFATTACH_DECL3_NEW(ixg, sizeof(struct adapter),
283 ixgbe_probe, ixgbe_attach, ixgbe_detach, NULL, NULL, NULL,
284 DVF_DETACH_SHUTDOWN);
285
286 #if 0
287 devclass_t ix_devclass;
288 DRIVER_MODULE(ix, pci, ix_driver, ix_devclass, 0, 0);
289
290 MODULE_DEPEND(ix, pci, 1, 1, 1);
291 MODULE_DEPEND(ix, ether, 1, 1, 1);
292 #ifdef DEV_NETMAP
293 MODULE_DEPEND(ix, netmap, 1, 1, 1);
294 #endif
295 #endif
296
297 /*
298 * TUNEABLE PARAMETERS:
299 */
300
301 /*
302 * AIM: Adaptive Interrupt Moderation
303 * which means that the interrupt rate
304 * is varied over time based on the
305 * traffic for that interrupt vector
306 */
307 static bool ixgbe_enable_aim = true;
308 #define SYSCTL_INT(_a1, _a2, _a3, _a4, _a5, _a6, _a7)
309 SYSCTL_INT(_hw_ix, OID_AUTO, enable_aim, CTLFLAG_RDTUN, &ixgbe_enable_aim, 0,
310 "Enable adaptive interrupt moderation");
311
312 static int ixgbe_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
313 SYSCTL_INT(_hw_ix, OID_AUTO, max_interrupt_rate, CTLFLAG_RDTUN,
314 &ixgbe_max_interrupt_rate, 0, "Maximum interrupts per second");
315
316 /* How many packets rxeof tries to clean at a time */
317 static int ixgbe_rx_process_limit = 256;
318 SYSCTL_INT(_hw_ix, OID_AUTO, rx_process_limit, CTLFLAG_RDTUN,
319 &ixgbe_rx_process_limit, 0, "Maximum number of received packets to process at a time, -1 means unlimited");
320
321 /* How many packets txeof tries to clean at a time */
322 static int ixgbe_tx_process_limit = 256;
323 SYSCTL_INT(_hw_ix, OID_AUTO, tx_process_limit, CTLFLAG_RDTUN,
324 &ixgbe_tx_process_limit, 0,
325 "Maximum number of sent packets to process at a time, -1 means unlimited");
326
327 /* Flow control setting, default to full */
328 static int ixgbe_flow_control = ixgbe_fc_full;
329 SYSCTL_INT(_hw_ix, OID_AUTO, flow_control, CTLFLAG_RDTUN,
330 &ixgbe_flow_control, 0, "Default flow control used for all adapters");
331
332 /* Which packet processing uses workqueue or softint */
333 static bool ixgbe_txrx_workqueue = false;
334
335 /*
336 * Smart speed setting, default to on
337 * this only works as a compile option
338 * right now as its during attach, set
339 * this to 'ixgbe_smart_speed_off' to
340 * disable.
341 */
342 static int ixgbe_smart_speed = ixgbe_smart_speed_on;
343
344 /*
345 * MSI-X should be the default for best performance,
346 * but this allows it to be forced off for testing.
347 */
348 static int ixgbe_enable_msix = 1;
349 SYSCTL_INT(_hw_ix, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &ixgbe_enable_msix, 0,
350 "Enable MSI-X interrupts");
351
352 /*
353 * Number of Queues, can be set to 0,
354 * it then autoconfigures based on the
355 * number of cpus with a max of 8. This
356 * can be overridden manually here.
357 */
358 static int ixgbe_num_queues = 0;
359 SYSCTL_INT(_hw_ix, OID_AUTO, num_queues, CTLFLAG_RDTUN, &ixgbe_num_queues, 0,
360 "Number of queues to configure, 0 indicates autoconfigure");
361
362 /*
363 * Number of TX descriptors per ring,
364 * setting higher than RX as this seems
365 * the better performing choice.
366 */
367 static int ixgbe_txd = PERFORM_TXD;
368 SYSCTL_INT(_hw_ix, OID_AUTO, txd, CTLFLAG_RDTUN, &ixgbe_txd, 0,
369 "Number of transmit descriptors per queue");
370
371 /* Number of RX descriptors per ring */
372 static int ixgbe_rxd = PERFORM_RXD;
373 SYSCTL_INT(_hw_ix, OID_AUTO, rxd, CTLFLAG_RDTUN, &ixgbe_rxd, 0,
374 "Number of receive descriptors per queue");
375
376 /*
377 * Defining this on will allow the use
378 * of unsupported SFP+ modules, note that
379 * doing so you are on your own :)
380 */
381 static int allow_unsupported_sfp = false;
382 #define TUNABLE_INT(__x, __y)
383 TUNABLE_INT("hw.ix.unsupported_sfp", &allow_unsupported_sfp);
384
385 /*
386 * Not sure if Flow Director is fully baked,
387 * so we'll default to turning it off.
388 */
389 static int ixgbe_enable_fdir = 0;
390 SYSCTL_INT(_hw_ix, OID_AUTO, enable_fdir, CTLFLAG_RDTUN, &ixgbe_enable_fdir, 0,
391 "Enable Flow Director");
392
393 /* Legacy Transmit (single queue) */
394 static int ixgbe_enable_legacy_tx = 0;
395 SYSCTL_INT(_hw_ix, OID_AUTO, enable_legacy_tx, CTLFLAG_RDTUN,
396 &ixgbe_enable_legacy_tx, 0, "Enable Legacy TX flow");
397
398 /* Receive-Side Scaling */
399 static int ixgbe_enable_rss = 1;
400 SYSCTL_INT(_hw_ix, OID_AUTO, enable_rss, CTLFLAG_RDTUN, &ixgbe_enable_rss, 0,
401 "Enable Receive-Side Scaling (RSS)");
402
403 #if 0
404 static int (*ixgbe_start_locked)(struct ifnet *, struct tx_ring *);
405 static int (*ixgbe_ring_empty)(struct ifnet *, pcq_t *);
406 #endif
407
408 #ifdef NET_MPSAFE
409 #define IXGBE_MPSAFE 1
410 #define IXGBE_CALLOUT_FLAGS CALLOUT_MPSAFE
411 #define IXGBE_SOFTINFT_FLAGS SOFTINT_MPSAFE
412 #define IXGBE_WORKQUEUE_FLAGS WQ_PERCPU | WQ_MPSAFE
413 #else
414 #define IXGBE_CALLOUT_FLAGS 0
415 #define IXGBE_SOFTINFT_FLAGS 0
416 #define IXGBE_WORKQUEUE_FLAGS WQ_PERCPU
417 #endif
418 #define IXGBE_WORKQUEUE_PRI PRI_SOFTNET
419
420 /************************************************************************
421 * ixgbe_initialize_rss_mapping
422 ************************************************************************/
423 static void
424 ixgbe_initialize_rss_mapping(struct adapter *adapter)
425 {
426 struct ixgbe_hw *hw = &adapter->hw;
427 u32 reta = 0, mrqc, rss_key[10];
428 int queue_id, table_size, index_mult;
429 int i, j;
430 u32 rss_hash_config;
431
432 /* force use default RSS key. */
433 #ifdef __NetBSD__
434 rss_getkey((uint8_t *) &rss_key);
435 #else
436 if (adapter->feat_en & IXGBE_FEATURE_RSS) {
437 /* Fetch the configured RSS key */
438 rss_getkey((uint8_t *) &rss_key);
439 } else {
440 /* set up random bits */
441 cprng_fast(&rss_key, sizeof(rss_key));
442 }
443 #endif
444
445 /* Set multiplier for RETA setup and table size based on MAC */
446 index_mult = 0x1;
447 table_size = 128;
448 switch (adapter->hw.mac.type) {
449 case ixgbe_mac_82598EB:
450 index_mult = 0x11;
451 break;
452 case ixgbe_mac_X550:
453 case ixgbe_mac_X550EM_x:
454 case ixgbe_mac_X550EM_a:
455 table_size = 512;
456 break;
457 default:
458 break;
459 }
460
461 /* Set up the redirection table */
462 for (i = 0, j = 0; i < table_size; i++, j++) {
463 if (j == adapter->num_queues)
464 j = 0;
465
466 if (adapter->feat_en & IXGBE_FEATURE_RSS) {
467 /*
468 * Fetch the RSS bucket id for the given indirection
469 * entry. Cap it at the number of configured buckets
470 * (which is num_queues.)
471 */
472 queue_id = rss_get_indirection_to_bucket(i);
473 queue_id = queue_id % adapter->num_queues;
474 } else
475 queue_id = (j * index_mult);
476
477 /*
478 * The low 8 bits are for hash value (n+0);
479 * The next 8 bits are for hash value (n+1), etc.
480 */
481 reta = reta >> 8;
482 reta = reta | (((uint32_t) queue_id) << 24);
483 if ((i & 3) == 3) {
484 if (i < 128)
485 IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
486 else
487 IXGBE_WRITE_REG(hw, IXGBE_ERETA((i >> 2) - 32),
488 reta);
489 reta = 0;
490 }
491 }
492
493 /* Now fill our hash function seeds */
494 for (i = 0; i < 10; i++)
495 IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
496
497 /* Perform hash on these packet types */
498 if (adapter->feat_en & IXGBE_FEATURE_RSS)
499 rss_hash_config = rss_gethashconfig();
500 else {
501 /*
502 * Disable UDP - IP fragments aren't currently being handled
503 * and so we end up with a mix of 2-tuple and 4-tuple
504 * traffic.
505 */
506 rss_hash_config = RSS_HASHTYPE_RSS_IPV4
507 | RSS_HASHTYPE_RSS_TCP_IPV4
508 | RSS_HASHTYPE_RSS_IPV6
509 | RSS_HASHTYPE_RSS_TCP_IPV6
510 | RSS_HASHTYPE_RSS_IPV6_EX
511 | RSS_HASHTYPE_RSS_TCP_IPV6_EX;
512 }
513
514 mrqc = IXGBE_MRQC_RSSEN;
515 if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
516 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
517 if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
518 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
519 if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
520 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
521 if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
522 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
523 if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
524 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
525 if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
526 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
527 if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
528 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
529 if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
530 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
531 if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
532 mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
533 mrqc |= ixgbe_get_mrqc(adapter->iov_mode);
534 IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
535 } /* ixgbe_initialize_rss_mapping */
536
537 /************************************************************************
538 * ixgbe_initialize_receive_units - Setup receive registers and features.
539 ************************************************************************/
540 #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
541
542 static void
543 ixgbe_initialize_receive_units(struct adapter *adapter)
544 {
545 struct rx_ring *rxr = adapter->rx_rings;
546 struct ixgbe_hw *hw = &adapter->hw;
547 struct ifnet *ifp = adapter->ifp;
548 int i, j;
549 u32 bufsz, fctrl, srrctl, rxcsum;
550 u32 hlreg;
551
552 /*
553 * Make sure receives are disabled while
554 * setting up the descriptor ring
555 */
556 ixgbe_disable_rx(hw);
557
558 /* Enable broadcasts */
559 fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
560 fctrl |= IXGBE_FCTRL_BAM;
561 if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
562 fctrl |= IXGBE_FCTRL_DPF;
563 fctrl |= IXGBE_FCTRL_PMCF;
564 }
565 IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
566
567 /* Set for Jumbo Frames? */
568 hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
569 if (ifp->if_mtu > ETHERMTU)
570 hlreg |= IXGBE_HLREG0_JUMBOEN;
571 else
572 hlreg &= ~IXGBE_HLREG0_JUMBOEN;
573
574 #ifdef DEV_NETMAP
575 /* CRC stripping is conditional in Netmap */
576 if ((adapter->feat_en & IXGBE_FEATURE_NETMAP) &&
577 (ifp->if_capenable & IFCAP_NETMAP) &&
578 !ix_crcstrip)
579 hlreg &= ~IXGBE_HLREG0_RXCRCSTRP;
580 else
581 #endif /* DEV_NETMAP */
582 hlreg |= IXGBE_HLREG0_RXCRCSTRP;
583
584 IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
585
586 bufsz = (adapter->rx_mbuf_sz + BSIZEPKT_ROUNDUP) >>
587 IXGBE_SRRCTL_BSIZEPKT_SHIFT;
588
589 for (i = 0; i < adapter->num_queues; i++, rxr++) {
590 u64 rdba = rxr->rxdma.dma_paddr;
591 u32 reg;
592 int regnum = i / 4; /* 1 register per 4 queues */
593 int regshift = i % 4; /* 4 bits per 1 queue */
594 j = rxr->me;
595
596 /* Setup the Base and Length of the Rx Descriptor Ring */
597 IXGBE_WRITE_REG(hw, IXGBE_RDBAL(j),
598 (rdba & 0x00000000ffffffffULL));
599 IXGBE_WRITE_REG(hw, IXGBE_RDBAH(j), (rdba >> 32));
600 IXGBE_WRITE_REG(hw, IXGBE_RDLEN(j),
601 adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
602
603 /* Set up the SRRCTL register */
604 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(j));
605 srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
606 srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
607 srrctl |= bufsz;
608 srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
609
610 /* Set RQSMR (Receive Queue Statistic Mapping) register */
611 reg = IXGBE_READ_REG(hw, IXGBE_RQSMR(regnum));
612 reg &= ~(0x000000ffUL << (regshift * 8));
613 reg |= i << (regshift * 8);
614 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(regnum), reg);
615
616 /*
617 * Set DROP_EN iff we have no flow control and >1 queue.
618 * Note that srrctl was cleared shortly before during reset,
619 * so we do not need to clear the bit, but do it just in case
620 * this code is moved elsewhere.
621 */
622 if (adapter->num_queues > 1 &&
623 adapter->hw.fc.requested_mode == ixgbe_fc_none) {
624 srrctl |= IXGBE_SRRCTL_DROP_EN;
625 } else {
626 srrctl &= ~IXGBE_SRRCTL_DROP_EN;
627 }
628
629 IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(j), srrctl);
630
631 /* Setup the HW Rx Head and Tail Descriptor Pointers */
632 IXGBE_WRITE_REG(hw, IXGBE_RDH(j), 0);
633 IXGBE_WRITE_REG(hw, IXGBE_RDT(j), 0);
634
635 /* Set the driver rx tail address */
636 rxr->tail = IXGBE_RDT(rxr->me);
637 }
638
639 if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
640 u32 psrtype = IXGBE_PSRTYPE_TCPHDR
641 | IXGBE_PSRTYPE_UDPHDR
642 | IXGBE_PSRTYPE_IPV4HDR
643 | IXGBE_PSRTYPE_IPV6HDR;
644 IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
645 }
646
647 rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
648
649 ixgbe_initialize_rss_mapping(adapter);
650
651 if (adapter->num_queues > 1) {
652 /* RSS and RX IPP Checksum are mutually exclusive */
653 rxcsum |= IXGBE_RXCSUM_PCSD;
654 }
655
656 if (ifp->if_capenable & IFCAP_RXCSUM)
657 rxcsum |= IXGBE_RXCSUM_PCSD;
658
659 /* This is useful for calculating UDP/IP fragment checksums */
660 if (!(rxcsum & IXGBE_RXCSUM_PCSD))
661 rxcsum |= IXGBE_RXCSUM_IPPCSE;
662
663 IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
664
665 } /* ixgbe_initialize_receive_units */
666
667 /************************************************************************
668 * ixgbe_initialize_transmit_units - Enable transmit units.
669 ************************************************************************/
670 static void
671 ixgbe_initialize_transmit_units(struct adapter *adapter)
672 {
673 struct tx_ring *txr = adapter->tx_rings;
674 struct ixgbe_hw *hw = &adapter->hw;
675 int i;
676
677 /* Setup the Base and Length of the Tx Descriptor Ring */
678 for (i = 0; i < adapter->num_queues; i++, txr++) {
679 u64 tdba = txr->txdma.dma_paddr;
680 u32 txctrl = 0;
681 u32 tqsmreg, reg;
682 int regnum = i / 4; /* 1 register per 4 queues */
683 int regshift = i % 4; /* 4 bits per 1 queue */
684 int j = txr->me;
685
686 IXGBE_WRITE_REG(hw, IXGBE_TDBAL(j),
687 (tdba & 0x00000000ffffffffULL));
688 IXGBE_WRITE_REG(hw, IXGBE_TDBAH(j), (tdba >> 32));
689 IXGBE_WRITE_REG(hw, IXGBE_TDLEN(j),
690 adapter->num_tx_desc * sizeof(union ixgbe_adv_tx_desc));
691
692 /*
693 * Set TQSMR (Transmit Queue Statistic Mapping) register.
694 * Register location is different between 82598 and others.
695 */
696 if (adapter->hw.mac.type == ixgbe_mac_82598EB)
697 tqsmreg = IXGBE_TQSMR(regnum);
698 else
699 tqsmreg = IXGBE_TQSM(regnum);
700 reg = IXGBE_READ_REG(hw, tqsmreg);
701 reg &= ~(0x000000ffUL << (regshift * 8));
702 reg |= i << (regshift * 8);
703 IXGBE_WRITE_REG(hw, tqsmreg, reg);
704
705 /* Setup the HW Tx Head and Tail descriptor pointers */
706 IXGBE_WRITE_REG(hw, IXGBE_TDH(j), 0);
707 IXGBE_WRITE_REG(hw, IXGBE_TDT(j), 0);
708
709 /* Cache the tail address */
710 txr->tail = IXGBE_TDT(j);
711
712 txr->txr_no_space = false;
713
714 /* Disable Head Writeback */
715 /*
716 * Note: for X550 series devices, these registers are actually
717 * prefixed with TPH_ isntead of DCA_, but the addresses and
718 * fields remain the same.
719 */
720 switch (hw->mac.type) {
721 case ixgbe_mac_82598EB:
722 txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(j));
723 break;
724 default:
725 txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(j));
726 break;
727 }
728 txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
729 switch (hw->mac.type) {
730 case ixgbe_mac_82598EB:
731 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(j), txctrl);
732 break;
733 default:
734 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(j), txctrl);
735 break;
736 }
737
738 }
739
740 if (hw->mac.type != ixgbe_mac_82598EB) {
741 u32 dmatxctl, rttdcs;
742
743 dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
744 dmatxctl |= IXGBE_DMATXCTL_TE;
745 IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
746 /* Disable arbiter to set MTQC */
747 rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
748 rttdcs |= IXGBE_RTTDCS_ARBDIS;
749 IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
750 IXGBE_WRITE_REG(hw, IXGBE_MTQC,
751 ixgbe_get_mtqc(adapter->iov_mode));
752 rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
753 IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
754 }
755
756 return;
757 } /* ixgbe_initialize_transmit_units */
758
759 /************************************************************************
760 * ixgbe_attach - Device initialization routine
761 *
762 * Called when the driver is being loaded.
763 * Identifies the type of hardware, allocates all resources
764 * and initializes the hardware.
765 *
766 * return 0 on success, positive on failure
767 ************************************************************************/
768 static void
769 ixgbe_attach(device_t parent, device_t dev, void *aux)
770 {
771 struct adapter *adapter;
772 struct ixgbe_hw *hw;
773 int error = -1;
774 u32 ctrl_ext;
775 u16 high, low, nvmreg;
776 pcireg_t id, subid;
777 const ixgbe_vendor_info_t *ent;
778 struct pci_attach_args *pa = aux;
779 bool unsupported_sfp = false;
780 const char *str;
781 char buf[256];
782
783 INIT_DEBUGOUT("ixgbe_attach: begin");
784
785 /* Allocate, clear, and link in our adapter structure */
786 adapter = device_private(dev);
787 adapter->hw.back = adapter;
788 adapter->dev = dev;
789 hw = &adapter->hw;
790 adapter->osdep.pc = pa->pa_pc;
791 adapter->osdep.tag = pa->pa_tag;
792 if (pci_dma64_available(pa))
793 adapter->osdep.dmat = pa->pa_dmat64;
794 else
795 adapter->osdep.dmat = pa->pa_dmat;
796 adapter->osdep.attached = false;
797
798 ent = ixgbe_lookup(pa);
799
800 KASSERT(ent != NULL);
801
802 aprint_normal(": %s, Version - %s\n",
803 ixgbe_strings[ent->index], ixgbe_driver_version);
804
805 /* Core Lock Init*/
806 IXGBE_CORE_LOCK_INIT(adapter, device_xname(dev));
807
808 /* Set up the timer callout */
809 callout_init(&adapter->timer, IXGBE_CALLOUT_FLAGS);
810
811 /* Determine hardware revision */
812 id = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG);
813 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
814
815 hw->vendor_id = PCI_VENDOR(id);
816 hw->device_id = PCI_PRODUCT(id);
817 hw->revision_id =
818 PCI_REVISION(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
819 hw->subsystem_vendor_id = PCI_SUBSYS_VENDOR(subid);
820 hw->subsystem_device_id = PCI_SUBSYS_ID(subid);
821
822 /*
823 * Make sure BUSMASTER is set
824 */
825 ixgbe_pci_enable_busmaster(pa->pa_pc, pa->pa_tag);
826
827 /* Do base PCI setup - map BAR0 */
828 if (ixgbe_allocate_pci_resources(adapter, pa)) {
829 aprint_error_dev(dev, "Allocation of PCI resources failed\n");
830 error = ENXIO;
831 goto err_out;
832 }
833
834 /* let hardware know driver is loaded */
835 ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
836 ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
837 IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
838
839 /*
840 * Initialize the shared code
841 */
842 if (ixgbe_init_shared_code(hw) != 0) {
843 aprint_error_dev(dev, "Unable to initialize the shared code\n");
844 error = ENXIO;
845 goto err_out;
846 }
847
848 switch (hw->mac.type) {
849 case ixgbe_mac_82598EB:
850 str = "82598EB";
851 break;
852 case ixgbe_mac_82599EB:
853 str = "82599EB";
854 break;
855 case ixgbe_mac_X540:
856 str = "X540";
857 break;
858 case ixgbe_mac_X550:
859 str = "X550";
860 break;
861 case ixgbe_mac_X550EM_x:
862 str = "X550EM";
863 break;
864 case ixgbe_mac_X550EM_a:
865 str = "X550EM A";
866 break;
867 default:
868 str = "Unknown";
869 break;
870 }
871 aprint_normal_dev(dev, "device %s\n", str);
872
873 if (hw->mbx.ops.init_params)
874 hw->mbx.ops.init_params(hw);
875
876 hw->allow_unsupported_sfp = allow_unsupported_sfp;
877
878 /* Pick up the 82599 settings */
879 if (hw->mac.type != ixgbe_mac_82598EB) {
880 hw->phy.smart_speed = ixgbe_smart_speed;
881 adapter->num_segs = IXGBE_82599_SCATTER;
882 } else
883 adapter->num_segs = IXGBE_82598_SCATTER;
884
885 /* Ensure SW/FW semaphore is free */
886 ixgbe_init_swfw_semaphore(hw);
887
888 hw->mac.ops.set_lan_id(hw);
889 ixgbe_init_device_features(adapter);
890
891 if (ixgbe_configure_interrupts(adapter)) {
892 error = ENXIO;
893 goto err_out;
894 }
895
896 /* Allocate multicast array memory. */
897 adapter->mta = malloc(sizeof(*adapter->mta) *
898 MAX_NUM_MULTICAST_ADDRESSES, M_DEVBUF, M_WAITOK);
899
900 /* Enable WoL (if supported) */
901 ixgbe_check_wol_support(adapter);
902
903 /* Register for VLAN events */
904 ether_set_vlan_cb(&adapter->osdep.ec, ixgbe_vlan_cb);
905
906 /* Verify adapter fan is still functional (if applicable) */
907 if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL) {
908 u32 esdp = IXGBE_READ_REG(hw, IXGBE_ESDP);
909 ixgbe_check_fan_failure(adapter, esdp, FALSE);
910 }
911
912 /* Set an initial default flow control value */
913 hw->fc.requested_mode = ixgbe_flow_control;
914
915 /* Sysctls for limiting the amount of work done in the taskqueues */
916 ixgbe_set_sysctl_value(adapter, "rx_processing_limit",
917 "max number of rx packets to process",
918 &adapter->rx_process_limit, ixgbe_rx_process_limit);
919
920 ixgbe_set_sysctl_value(adapter, "tx_processing_limit",
921 "max number of tx packets to process",
922 &adapter->tx_process_limit, ixgbe_tx_process_limit);
923
924 /* Do descriptor calc and sanity checks */
925 if (((ixgbe_txd * sizeof(union ixgbe_adv_tx_desc)) % DBA_ALIGN) != 0 ||
926 ixgbe_txd < MIN_TXD || ixgbe_txd > MAX_TXD) {
927 aprint_error_dev(dev, "TXD config issue, using default!\n");
928 adapter->num_tx_desc = DEFAULT_TXD;
929 } else
930 adapter->num_tx_desc = ixgbe_txd;
931
932 if (((ixgbe_rxd * sizeof(union ixgbe_adv_rx_desc)) % DBA_ALIGN) != 0 ||
933 ixgbe_rxd < MIN_RXD || ixgbe_rxd > MAX_RXD) {
934 aprint_error_dev(dev, "RXD config issue, using default!\n");
935 adapter->num_rx_desc = DEFAULT_RXD;
936 } else
937 adapter->num_rx_desc = ixgbe_rxd;
938
939 /* Allocate our TX/RX Queues */
940 if (ixgbe_allocate_queues(adapter)) {
941 error = ENOMEM;
942 goto err_out;
943 }
944
945 hw->phy.reset_if_overtemp = TRUE;
946 error = ixgbe_reset_hw(hw);
947 hw->phy.reset_if_overtemp = FALSE;
948 if (error == IXGBE_ERR_SFP_NOT_PRESENT) {
949 /*
950 * No optics in this port, set up
951 * so the timer routine will probe
952 * for later insertion.
953 */
954 adapter->sfp_probe = TRUE;
955 error = IXGBE_SUCCESS;
956 } else if (error == IXGBE_ERR_SFP_NOT_SUPPORTED) {
957 aprint_error_dev(dev, "Unsupported SFP+ module detected!\n");
958 unsupported_sfp = true;
959 error = IXGBE_SUCCESS;
960 } else if (error) {
961 aprint_error_dev(dev, "Hardware initialization failed\n");
962 error = EIO;
963 goto err_late;
964 }
965
966 /* Make sure we have a good EEPROM before we read from it */
967 if (ixgbe_validate_eeprom_checksum(&adapter->hw, NULL) < 0) {
968 aprint_error_dev(dev, "The EEPROM Checksum Is Not Valid\n");
969 error = EIO;
970 goto err_late;
971 }
972
973 aprint_normal("%s:", device_xname(dev));
974 /* NVM Image Version */
975 high = low = 0;
976 switch (hw->mac.type) {
977 case ixgbe_mac_X540:
978 case ixgbe_mac_X550EM_a:
979 hw->eeprom.ops.read(hw, IXGBE_NVM_IMAGE_VER, &nvmreg);
980 if (nvmreg == 0xffff)
981 break;
982 high = (nvmreg >> 12) & 0x0f;
983 low = (nvmreg >> 4) & 0xff;
984 id = nvmreg & 0x0f;
985 aprint_normal(" NVM Image Version %u.", high);
986 if (hw->mac.type == ixgbe_mac_X540)
987 str = "%x";
988 else
989 str = "%02x";
990 aprint_normal(str, low);
991 aprint_normal(" ID 0x%x,", id);
992 break;
993 case ixgbe_mac_X550EM_x:
994 case ixgbe_mac_X550:
995 hw->eeprom.ops.read(hw, IXGBE_NVM_IMAGE_VER, &nvmreg);
996 if (nvmreg == 0xffff)
997 break;
998 high = (nvmreg >> 12) & 0x0f;
999 low = nvmreg & 0xff;
1000 aprint_normal(" NVM Image Version %u.%02x,", high, low);
1001 break;
1002 default:
1003 break;
1004 }
1005 hw->eeprom.nvm_image_ver_high = high;
1006 hw->eeprom.nvm_image_ver_low = low;
1007
1008 /* PHY firmware revision */
1009 switch (hw->mac.type) {
1010 case ixgbe_mac_X540:
1011 case ixgbe_mac_X550:
1012 hw->eeprom.ops.read(hw, IXGBE_PHYFW_REV, &nvmreg);
1013 if (nvmreg == 0xffff)
1014 break;
1015 high = (nvmreg >> 12) & 0x0f;
1016 low = (nvmreg >> 4) & 0xff;
1017 id = nvmreg & 0x000f;
1018 aprint_normal(" PHY FW Revision %u.", high);
1019 if (hw->mac.type == ixgbe_mac_X540)
1020 str = "%x";
1021 else
1022 str = "%02x";
1023 aprint_normal(str, low);
1024 aprint_normal(" ID 0x%x,", id);
1025 break;
1026 default:
1027 break;
1028 }
1029
1030 /* NVM Map version & OEM NVM Image version */
1031 switch (hw->mac.type) {
1032 case ixgbe_mac_X550:
1033 case ixgbe_mac_X550EM_x:
1034 case ixgbe_mac_X550EM_a:
1035 hw->eeprom.ops.read(hw, IXGBE_NVM_MAP_VER, &nvmreg);
1036 if (nvmreg != 0xffff) {
1037 high = (nvmreg >> 12) & 0x0f;
1038 low = nvmreg & 0x00ff;
1039 aprint_normal(" NVM Map version %u.%02x,", high, low);
1040 }
1041 hw->eeprom.ops.read(hw, IXGBE_OEM_NVM_IMAGE_VER, &nvmreg);
1042 if (nvmreg != 0xffff) {
1043 high = (nvmreg >> 12) & 0x0f;
1044 low = nvmreg & 0x00ff;
1045 aprint_verbose(" OEM NVM Image version %u.%02x,", high,
1046 low);
1047 }
1048 break;
1049 default:
1050 break;
1051 }
1052
1053 /* Print the ETrackID */
1054 hw->eeprom.ops.read(hw, IXGBE_ETRACKID_H, &high);
1055 hw->eeprom.ops.read(hw, IXGBE_ETRACKID_L, &low);
1056 aprint_normal(" ETrackID %08x\n", ((uint32_t)high << 16) | low);
1057
1058 if (adapter->feat_en & IXGBE_FEATURE_MSIX) {
1059 error = ixgbe_allocate_msix(adapter, pa);
1060 if (error) {
1061 /* Free allocated queue structures first */
1062 ixgbe_free_queues(adapter);
1063
1064 /* Fallback to legacy interrupt */
1065 adapter->feat_en &= ~IXGBE_FEATURE_MSIX;
1066 if (adapter->feat_cap & IXGBE_FEATURE_MSI)
1067 adapter->feat_en |= IXGBE_FEATURE_MSI;
1068 adapter->num_queues = 1;
1069
1070 /* Allocate our TX/RX Queues again */
1071 if (ixgbe_allocate_queues(adapter)) {
1072 error = ENOMEM;
1073 goto err_out;
1074 }
1075 }
1076 }
1077 /* Recovery mode */
1078 switch (adapter->hw.mac.type) {
1079 case ixgbe_mac_X550:
1080 case ixgbe_mac_X550EM_x:
1081 case ixgbe_mac_X550EM_a:
1082 /* >= 2.00 */
1083 if (hw->eeprom.nvm_image_ver_high >= 2) {
1084 adapter->feat_cap |= IXGBE_FEATURE_RECOVERY_MODE;
1085 adapter->feat_en |= IXGBE_FEATURE_RECOVERY_MODE;
1086 }
1087 break;
1088 default:
1089 break;
1090 }
1091
1092 if ((adapter->feat_en & IXGBE_FEATURE_MSIX) == 0)
1093 error = ixgbe_allocate_legacy(adapter, pa);
1094 if (error)
1095 goto err_late;
1096
1097 /* Tasklets for Link, SFP, Multispeed Fiber and Flow Director */
1098 adapter->link_si = softint_establish(SOFTINT_NET |IXGBE_SOFTINFT_FLAGS,
1099 ixgbe_handle_link, adapter);
1100 adapter->mod_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
1101 ixgbe_handle_mod, adapter);
1102 adapter->msf_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
1103 ixgbe_handle_msf, adapter);
1104 adapter->phy_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
1105 ixgbe_handle_phy, adapter);
1106 if (adapter->feat_en & IXGBE_FEATURE_FDIR)
1107 adapter->fdir_si =
1108 softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
1109 ixgbe_reinit_fdir, adapter);
1110 if ((adapter->link_si == NULL) || (adapter->mod_si == NULL)
1111 || (adapter->msf_si == NULL) || (adapter->phy_si == NULL)
1112 || ((adapter->feat_en & IXGBE_FEATURE_FDIR)
1113 && (adapter->fdir_si == NULL))) {
1114 aprint_error_dev(dev,
1115 "could not establish software interrupts ()\n");
1116 goto err_out;
1117 }
1118
1119 error = ixgbe_start_hw(hw);
1120 switch (error) {
1121 case IXGBE_ERR_EEPROM_VERSION:
1122 aprint_error_dev(dev, "This device is a pre-production adapter/"
1123 "LOM. Please be aware there may be issues associated "
1124 "with your hardware.\nIf you are experiencing problems "
1125 "please contact your Intel or hardware representative "
1126 "who provided you with this hardware.\n");
1127 break;
1128 default:
1129 break;
1130 }
1131
1132 /* Setup OS specific network interface */
1133 if (ixgbe_setup_interface(dev, adapter) != 0)
1134 goto err_late;
1135
1136 /*
1137 * Print PHY ID only for copper PHY. On device which has SFP(+) cage
1138 * and a module is inserted, phy.id is not MII PHY id but SFF 8024 ID.
1139 */
1140 if (hw->phy.media_type == ixgbe_media_type_copper) {
1141 uint16_t id1, id2;
1142 int oui, model, rev;
1143 const char *descr;
1144
1145 id1 = hw->phy.id >> 16;
1146 id2 = hw->phy.id & 0xffff;
1147 oui = MII_OUI(id1, id2);
1148 model = MII_MODEL(id2);
1149 rev = MII_REV(id2);
1150 if ((descr = mii_get_descr(oui, model)) != NULL)
1151 aprint_normal_dev(dev,
1152 "PHY: %s (OUI 0x%06x, model 0x%04x), rev. %d\n",
1153 descr, oui, model, rev);
1154 else
1155 aprint_normal_dev(dev,
1156 "PHY OUI 0x%06x, model 0x%04x, rev. %d\n",
1157 oui, model, rev);
1158 }
1159
1160 /* Enable EEE power saving */
1161 if (adapter->feat_cap & IXGBE_FEATURE_EEE)
1162 hw->mac.ops.setup_eee(hw,
1163 adapter->feat_en & IXGBE_FEATURE_EEE);
1164
1165 /* Enable power to the phy. */
1166 if (!unsupported_sfp) {
1167 /* Enable the optics for 82599 SFP+ fiber */
1168 ixgbe_enable_tx_laser(hw);
1169
1170 /*
1171 * XXX Currently, ixgbe_set_phy_power() supports only copper
1172 * PHY, so it's not required to test with !unsupported_sfp.
1173 */
1174 ixgbe_set_phy_power(hw, TRUE);
1175 }
1176
1177 /* Initialize statistics */
1178 ixgbe_update_stats_counters(adapter);
1179
1180 /* Check PCIE slot type/speed/width */
1181 ixgbe_get_slot_info(adapter);
1182
1183 /*
1184 * Do time init and sysctl init here, but
1185 * only on the first port of a bypass adapter.
1186 */
1187 ixgbe_bypass_init(adapter);
1188
1189 /* Set an initial dmac value */
1190 adapter->dmac = 0;
1191 /* Set initial advertised speeds (if applicable) */
1192 adapter->advertise = ixgbe_get_advertise(adapter);
1193
1194 if (adapter->feat_cap & IXGBE_FEATURE_SRIOV)
1195 ixgbe_define_iov_schemas(dev, &error);
1196
1197 /* Add sysctls */
1198 ixgbe_add_device_sysctls(adapter);
1199 ixgbe_add_hw_stats(adapter);
1200
1201 /* For Netmap */
1202 adapter->init_locked = ixgbe_init_locked;
1203 adapter->stop_locked = ixgbe_stop;
1204
1205 if (adapter->feat_en & IXGBE_FEATURE_NETMAP)
1206 ixgbe_netmap_attach(adapter);
1207
1208 snprintb(buf, sizeof(buf), IXGBE_FEATURE_FLAGS, adapter->feat_cap);
1209 aprint_verbose_dev(dev, "feature cap %s\n", buf);
1210 snprintb(buf, sizeof(buf), IXGBE_FEATURE_FLAGS, adapter->feat_en);
1211 aprint_verbose_dev(dev, "feature ena %s\n", buf);
1212
1213 if (pmf_device_register(dev, ixgbe_suspend, ixgbe_resume))
1214 pmf_class_network_register(dev, adapter->ifp);
1215 else
1216 aprint_error_dev(dev, "couldn't establish power handler\n");
1217
1218 /* Init recovery mode timer and state variable */
1219 if (adapter->feat_en & IXGBE_FEATURE_RECOVERY_MODE) {
1220 adapter->recovery_mode = 0;
1221
1222 /* Set up the timer callout */
1223 callout_init(&adapter->recovery_mode_timer,
1224 IXGBE_CALLOUT_FLAGS);
1225
1226 /* Start the task */
1227 callout_reset(&adapter->recovery_mode_timer, hz,
1228 ixgbe_recovery_mode_timer, adapter);
1229 }
1230
1231 INIT_DEBUGOUT("ixgbe_attach: end");
1232 adapter->osdep.attached = true;
1233
1234 return;
1235
1236 err_late:
1237 ixgbe_free_queues(adapter);
1238 err_out:
1239 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
1240 ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
1241 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT, ctrl_ext);
1242 ixgbe_free_softint(adapter);
1243 ixgbe_free_pci_resources(adapter);
1244 if (adapter->mta != NULL)
1245 free(adapter->mta, M_DEVBUF);
1246 IXGBE_CORE_LOCK_DESTROY(adapter);
1247
1248 return;
1249 } /* ixgbe_attach */
1250
1251 /************************************************************************
1252 * ixgbe_check_wol_support
1253 *
1254 * Checks whether the adapter's ports are capable of
1255 * Wake On LAN by reading the adapter's NVM.
1256 *
1257 * Sets each port's hw->wol_enabled value depending
1258 * on the value read here.
1259 ************************************************************************/
1260 static void
1261 ixgbe_check_wol_support(struct adapter *adapter)
1262 {
1263 struct ixgbe_hw *hw = &adapter->hw;
1264 u16 dev_caps = 0;
1265
1266 /* Find out WoL support for port */
1267 adapter->wol_support = hw->wol_enabled = 0;
1268 ixgbe_get_device_caps(hw, &dev_caps);
1269 if ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0_1) ||
1270 ((dev_caps & IXGBE_DEVICE_CAPS_WOL_PORT0) &&
1271 hw->bus.func == 0))
1272 adapter->wol_support = hw->wol_enabled = 1;
1273
1274 /* Save initial wake up filter configuration */
1275 adapter->wufc = IXGBE_READ_REG(hw, IXGBE_WUFC);
1276
1277 return;
1278 } /* ixgbe_check_wol_support */
1279
1280 /************************************************************************
1281 * ixgbe_setup_interface
1282 *
1283 * Setup networking device structure and register an interface.
1284 ************************************************************************/
1285 static int
1286 ixgbe_setup_interface(device_t dev, struct adapter *adapter)
1287 {
1288 struct ethercom *ec = &adapter->osdep.ec;
1289 struct ifnet *ifp;
1290 int rv;
1291
1292 INIT_DEBUGOUT("ixgbe_setup_interface: begin");
1293
1294 ifp = adapter->ifp = &ec->ec_if;
1295 strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
1296 ifp->if_baudrate = IF_Gbps(10);
1297 ifp->if_init = ixgbe_init;
1298 ifp->if_stop = ixgbe_ifstop;
1299 ifp->if_softc = adapter;
1300 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1301 #ifdef IXGBE_MPSAFE
1302 ifp->if_extflags = IFEF_MPSAFE;
1303 #endif
1304 ifp->if_ioctl = ixgbe_ioctl;
1305 #if __FreeBSD_version >= 1100045
1306 /* TSO parameters */
1307 ifp->if_hw_tsomax = 65518;
1308 ifp->if_hw_tsomaxsegcount = IXGBE_82599_SCATTER;
1309 ifp->if_hw_tsomaxsegsize = 2048;
1310 #endif
1311 if (adapter->feat_en & IXGBE_FEATURE_LEGACY_TX) {
1312 #if 0
1313 ixgbe_start_locked = ixgbe_legacy_start_locked;
1314 #endif
1315 } else {
1316 ifp->if_transmit = ixgbe_mq_start;
1317 #if 0
1318 ixgbe_start_locked = ixgbe_mq_start_locked;
1319 #endif
1320 }
1321 ifp->if_start = ixgbe_legacy_start;
1322 IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
1323 IFQ_SET_READY(&ifp->if_snd);
1324
1325 rv = if_initialize(ifp);
1326 if (rv != 0) {
1327 aprint_error_dev(dev, "if_initialize failed(%d)\n", rv);
1328 return rv;
1329 }
1330 adapter->ipq = if_percpuq_create(&adapter->osdep.ec.ec_if);
1331 ether_ifattach(ifp, adapter->hw.mac.addr);
1332 aprint_normal_dev(dev, "Ethernet address %s\n",
1333 ether_sprintf(adapter->hw.mac.addr));
1334 /*
1335 * We use per TX queue softint, so if_deferred_start_init() isn't
1336 * used.
1337 */
1338 ether_set_ifflags_cb(ec, ixgbe_ifflags_cb);
1339
1340 adapter->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1341
1342 /*
1343 * Tell the upper layer(s) we support long frames.
1344 */
1345 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1346
1347 /* Set capability flags */
1348 ifp->if_capabilities |= IFCAP_RXCSUM
1349 | IFCAP_TXCSUM
1350 | IFCAP_TSOv4
1351 | IFCAP_TSOv6;
1352 ifp->if_capenable = 0;
1353
1354 ec->ec_capabilities |= ETHERCAP_VLAN_HWTAGGING
1355 | ETHERCAP_VLAN_HWCSUM
1356 | ETHERCAP_JUMBO_MTU
1357 | ETHERCAP_VLAN_MTU;
1358
1359 /* Enable the above capabilities by default */
1360 ec->ec_capenable = ec->ec_capabilities;
1361
1362 /*
1363 * Don't turn this on by default, if vlans are
1364 * created on another pseudo device (eg. lagg)
1365 * then vlan events are not passed thru, breaking
1366 * operation, but with HW FILTER off it works. If
1367 * using vlans directly on the ixgbe driver you can
1368 * enable this and get full hardware tag filtering.
1369 */
1370 ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER;
1371
1372 /*
1373 * Specify the media types supported by this adapter and register
1374 * callbacks to update media and link information
1375 */
1376 ec->ec_ifmedia = &adapter->media;
1377 ifmedia_init(&adapter->media, IFM_IMASK, ixgbe_media_change,
1378 ixgbe_media_status);
1379
1380 adapter->phy_layer = ixgbe_get_supported_physical_layer(&adapter->hw);
1381 ixgbe_add_media_types(adapter);
1382
1383 /* Set autoselect media by default */
1384 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
1385
1386 if_register(ifp);
1387
1388 return (0);
1389 } /* ixgbe_setup_interface */
1390
1391 /************************************************************************
1392 * ixgbe_add_media_types
1393 ************************************************************************/
1394 static void
1395 ixgbe_add_media_types(struct adapter *adapter)
1396 {
1397 struct ixgbe_hw *hw = &adapter->hw;
1398 u64 layer;
1399
1400 layer = adapter->phy_layer;
1401
1402 #define ADD(mm, dd) \
1403 ifmedia_add(&adapter->media, IFM_ETHER | (mm), (dd), NULL);
1404
1405 ADD(IFM_NONE, 0);
1406
1407 /* Media types with matching NetBSD media defines */
1408 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T) {
1409 ADD(IFM_10G_T | IFM_FDX, 0);
1410 }
1411 if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T) {
1412 ADD(IFM_1000_T | IFM_FDX, 0);
1413 }
1414 if (layer & IXGBE_PHYSICAL_LAYER_100BASE_TX) {
1415 ADD(IFM_100_TX | IFM_FDX, 0);
1416 }
1417 if (layer & IXGBE_PHYSICAL_LAYER_10BASE_T) {
1418 ADD(IFM_10_T | IFM_FDX, 0);
1419 }
1420
1421 if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
1422 layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA) {
1423 ADD(IFM_10G_TWINAX | IFM_FDX, 0);
1424 }
1425
1426 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR) {
1427 ADD(IFM_10G_LR | IFM_FDX, 0);
1428 if (hw->phy.multispeed_fiber) {
1429 ADD(IFM_1000_LX | IFM_FDX, 0);
1430 }
1431 }
1432 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
1433 ADD(IFM_10G_SR | IFM_FDX, 0);
1434 if (hw->phy.multispeed_fiber) {
1435 ADD(IFM_1000_SX | IFM_FDX, 0);
1436 }
1437 } else if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX) {
1438 ADD(IFM_1000_SX | IFM_FDX, 0);
1439 }
1440 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4) {
1441 ADD(IFM_10G_CX4 | IFM_FDX, 0);
1442 }
1443
1444 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR) {
1445 ADD(IFM_10G_KR | IFM_FDX, 0);
1446 }
1447 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4) {
1448 ADD(IFM_10G_KX4 | IFM_FDX, 0);
1449 }
1450 if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX) {
1451 ADD(IFM_1000_KX | IFM_FDX, 0);
1452 }
1453 if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX) {
1454 ADD(IFM_2500_KX | IFM_FDX, 0);
1455 }
1456 if (layer & IXGBE_PHYSICAL_LAYER_2500BASE_T) {
1457 ADD(IFM_2500_T | IFM_FDX, 0);
1458 }
1459 if (layer & IXGBE_PHYSICAL_LAYER_5GBASE_T) {
1460 ADD(IFM_5000_T | IFM_FDX, 0);
1461 }
1462 if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_BX)
1463 ADD(IFM_1000_BX10 | IFM_FDX, 0);
1464 /* XXX no ifmedia_set? */
1465
1466 ADD(IFM_AUTO, 0);
1467
1468 #undef ADD
1469 } /* ixgbe_add_media_types */
1470
1471 /************************************************************************
1472 * ixgbe_is_sfp
1473 ************************************************************************/
1474 static inline bool
1475 ixgbe_is_sfp(struct ixgbe_hw *hw)
1476 {
1477 switch (hw->mac.type) {
1478 case ixgbe_mac_82598EB:
1479 if (hw->phy.type == ixgbe_phy_nl)
1480 return (TRUE);
1481 return (FALSE);
1482 case ixgbe_mac_82599EB:
1483 case ixgbe_mac_X550EM_x:
1484 case ixgbe_mac_X550EM_a:
1485 switch (hw->mac.ops.get_media_type(hw)) {
1486 case ixgbe_media_type_fiber:
1487 case ixgbe_media_type_fiber_qsfp:
1488 return (TRUE);
1489 default:
1490 return (FALSE);
1491 }
1492 default:
1493 return (FALSE);
1494 }
1495 } /* ixgbe_is_sfp */
1496
1497 /************************************************************************
1498 * ixgbe_config_link
1499 ************************************************************************/
1500 static void
1501 ixgbe_config_link(struct adapter *adapter)
1502 {
1503 struct ixgbe_hw *hw = &adapter->hw;
1504 u32 autoneg, err = 0;
1505 bool sfp, negotiate = false;
1506
1507 sfp = ixgbe_is_sfp(hw);
1508
1509 if (sfp) {
1510 if (hw->phy.multispeed_fiber) {
1511 ixgbe_enable_tx_laser(hw);
1512 kpreempt_disable();
1513 softint_schedule(adapter->msf_si);
1514 kpreempt_enable();
1515 }
1516 kpreempt_disable();
1517 softint_schedule(adapter->mod_si);
1518 kpreempt_enable();
1519 } else {
1520 struct ifmedia *ifm = &adapter->media;
1521
1522 if (hw->mac.ops.check_link)
1523 err = ixgbe_check_link(hw, &adapter->link_speed,
1524 &adapter->link_up, FALSE);
1525 if (err)
1526 return;
1527
1528 /*
1529 * Check if it's the first call. If it's the first call,
1530 * get value for auto negotiation.
1531 */
1532 autoneg = hw->phy.autoneg_advertised;
1533 if ((IFM_SUBTYPE(ifm->ifm_cur->ifm_media) != IFM_NONE)
1534 && ((!autoneg) && (hw->mac.ops.get_link_capabilities)))
1535 err = hw->mac.ops.get_link_capabilities(hw, &autoneg,
1536 &negotiate);
1537 if (err)
1538 return;
1539 if (hw->mac.ops.setup_link)
1540 err = hw->mac.ops.setup_link(hw, autoneg,
1541 adapter->link_up);
1542 }
1543
1544 } /* ixgbe_config_link */
1545
1546 /************************************************************************
1547 * ixgbe_update_stats_counters - Update board statistics counters.
1548 ************************************************************************/
1549 static void
1550 ixgbe_update_stats_counters(struct adapter *adapter)
1551 {
1552 struct ifnet *ifp = adapter->ifp;
1553 struct ixgbe_hw *hw = &adapter->hw;
1554 struct ixgbe_hw_stats *stats = &adapter->stats.pf;
1555 u32 missed_rx = 0, bprc, lxon, lxoff, total;
1556 u64 total_missed_rx = 0;
1557 uint64_t crcerrs, rlec;
1558 unsigned int queue_counters;
1559 int i;
1560
1561 crcerrs = IXGBE_READ_REG(hw, IXGBE_CRCERRS);
1562 stats->crcerrs.ev_count += crcerrs;
1563 stats->illerrc.ev_count += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
1564 stats->errbc.ev_count += IXGBE_READ_REG(hw, IXGBE_ERRBC);
1565 stats->mspdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MSPDC);
1566 if (hw->mac.type >= ixgbe_mac_X550)
1567 stats->mbsdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MBSDC);
1568
1569 /* 16 registers exist */
1570 queue_counters = uimin(__arraycount(stats->qprc), adapter->num_queues);
1571 for (i = 0; i < queue_counters; i++) {
1572 stats->qprc[i].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
1573 stats->qptc[i].ev_count += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
1574 if (hw->mac.type >= ixgbe_mac_82599EB) {
1575 stats->qprdc[i].ev_count
1576 += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
1577 }
1578 }
1579
1580 /* 8 registers exist */
1581 for (i = 0; i < IXGBE_TC_COUNTER_NUM; i++) {
1582 uint32_t mp;
1583
1584 /* MPC */
1585 mp = IXGBE_READ_REG(hw, IXGBE_MPC(i));
1586 /* global total per queue */
1587 stats->mpc[i].ev_count += mp;
1588 /* running comprehensive total for stats display */
1589 total_missed_rx += mp;
1590
1591 if (hw->mac.type == ixgbe_mac_82598EB)
1592 stats->rnbc[i].ev_count
1593 += IXGBE_READ_REG(hw, IXGBE_RNBC(i));
1594
1595 stats->pxontxc[i].ev_count
1596 += IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
1597 stats->pxofftxc[i].ev_count
1598 += IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
1599 if (hw->mac.type >= ixgbe_mac_82599EB) {
1600 stats->pxonrxc[i].ev_count
1601 += IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
1602 stats->pxoffrxc[i].ev_count
1603 += IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
1604 stats->pxon2offc[i].ev_count
1605 += IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
1606 } else {
1607 stats->pxonrxc[i].ev_count
1608 += IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
1609 stats->pxoffrxc[i].ev_count
1610 += IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
1611 }
1612 }
1613 stats->mpctotal.ev_count += total_missed_rx;
1614
1615 /* Document says M[LR]FC are valid when link is up and 10Gbps */
1616 if ((adapter->link_active == LINK_STATE_UP)
1617 && (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL)) {
1618 stats->mlfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MLFC);
1619 stats->mrfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MRFC);
1620 }
1621 rlec = IXGBE_READ_REG(hw, IXGBE_RLEC);
1622 stats->rlec.ev_count += rlec;
1623
1624 /* Hardware workaround, gprc counts missed packets */
1625 stats->gprc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPRC) - missed_rx;
1626
1627 lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
1628 stats->lxontxc.ev_count += lxon;
1629 lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
1630 stats->lxofftxc.ev_count += lxoff;
1631 total = lxon + lxoff;
1632
1633 if (hw->mac.type != ixgbe_mac_82598EB) {
1634 stats->gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCL) +
1635 ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
1636 stats->gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
1637 ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32) - total * ETHER_MIN_LEN;
1638 stats->tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORL) +
1639 ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
1640 stats->lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
1641 stats->lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
1642 } else {
1643 stats->lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
1644 stats->lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
1645 /* 82598 only has a counter in the high register */
1646 stats->gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCH);
1647 stats->gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCH) - total * ETHER_MIN_LEN;
1648 stats->tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORH);
1649 }
1650
1651 /*
1652 * Workaround: mprc hardware is incorrectly counting
1653 * broadcasts, so for now we subtract those.
1654 */
1655 bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
1656 stats->bprc.ev_count += bprc;
1657 stats->mprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPRC)
1658 - ((hw->mac.type == ixgbe_mac_82598EB) ? bprc : 0);
1659
1660 stats->prc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC64);
1661 stats->prc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC127);
1662 stats->prc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC255);
1663 stats->prc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC511);
1664 stats->prc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1023);
1665 stats->prc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1522);
1666
1667 stats->gptc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPTC) - total;
1668 stats->mptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPTC) - total;
1669 stats->ptc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC64) - total;
1670
1671 stats->ruc.ev_count += IXGBE_READ_REG(hw, IXGBE_RUC);
1672 stats->rfc.ev_count += IXGBE_READ_REG(hw, IXGBE_RFC);
1673 stats->roc.ev_count += IXGBE_READ_REG(hw, IXGBE_ROC);
1674 stats->rjc.ev_count += IXGBE_READ_REG(hw, IXGBE_RJC);
1675 stats->mngprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
1676 stats->mngpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
1677 stats->mngptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
1678 stats->tpr.ev_count += IXGBE_READ_REG(hw, IXGBE_TPR);
1679 stats->tpt.ev_count += IXGBE_READ_REG(hw, IXGBE_TPT);
1680 stats->ptc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC127);
1681 stats->ptc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC255);
1682 stats->ptc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC511);
1683 stats->ptc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1023);
1684 stats->ptc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1522);
1685 stats->bptc.ev_count += IXGBE_READ_REG(hw, IXGBE_BPTC);
1686 stats->xec.ev_count += IXGBE_READ_REG(hw, IXGBE_XEC);
1687 stats->fccrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCCRC);
1688 stats->fclast.ev_count += IXGBE_READ_REG(hw, IXGBE_FCLAST);
1689 /* Only read FCOE on 82599 */
1690 if (hw->mac.type != ixgbe_mac_82598EB) {
1691 stats->fcoerpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
1692 stats->fcoeprc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
1693 stats->fcoeptc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
1694 stats->fcoedwrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
1695 stats->fcoedwtc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
1696 }
1697
1698 /* Fill out the OS statistics structure */
1699 /*
1700 * NetBSD: Don't override if_{i|o}{packets|bytes|mcasts} with
1701 * adapter->stats counters. It's required to make ifconfig -z
1702 * (SOICZIFDATA) work.
1703 */
1704 /* XXX Actually, just fill in the per-cpu stats, please !!! */
1705
1706 /* Rx Errors */
1707 net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
1708 if_statadd_ref(nsr, if_iqdrops, total_missed_rx);
1709 if_statadd_ref(nsr, if_ierrors, crcerrs + rlec);
1710 IF_STAT_PUTREF(ifp);
1711 } /* ixgbe_update_stats_counters */
1712
1713 /************************************************************************
1714 * ixgbe_add_hw_stats
1715 *
1716 * Add sysctl variables, one per statistic, to the system.
1717 ************************************************************************/
1718 static void
1719 ixgbe_add_hw_stats(struct adapter *adapter)
1720 {
1721 device_t dev = adapter->dev;
1722 const struct sysctlnode *rnode, *cnode;
1723 struct sysctllog **log = &adapter->sysctllog;
1724 struct tx_ring *txr = adapter->tx_rings;
1725 struct rx_ring *rxr = adapter->rx_rings;
1726 struct ixgbe_hw *hw = &adapter->hw;
1727 struct ixgbe_hw_stats *stats = &adapter->stats.pf;
1728 const char *xname = device_xname(dev);
1729 int i;
1730
1731 /* Driver Statistics */
1732 evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
1733 NULL, xname, "Driver tx dma soft fail EFBIG");
1734 evcnt_attach_dynamic(&adapter->mbuf_defrag_failed, EVCNT_TYPE_MISC,
1735 NULL, xname, "m_defrag() failed");
1736 evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
1737 NULL, xname, "Driver tx dma hard fail EFBIG");
1738 evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
1739 NULL, xname, "Driver tx dma hard fail EINVAL");
1740 evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
1741 NULL, xname, "Driver tx dma hard fail other");
1742 evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
1743 NULL, xname, "Driver tx dma soft fail EAGAIN");
1744 evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
1745 NULL, xname, "Driver tx dma soft fail ENOMEM");
1746 evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
1747 NULL, xname, "Watchdog timeouts");
1748 evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
1749 NULL, xname, "TSO errors");
1750 evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_INTR,
1751 NULL, xname, "Link MSI-X IRQ Handled");
1752 evcnt_attach_dynamic(&adapter->link_sicount, EVCNT_TYPE_INTR,
1753 NULL, xname, "Link softint");
1754 evcnt_attach_dynamic(&adapter->mod_sicount, EVCNT_TYPE_INTR,
1755 NULL, xname, "module softint");
1756 evcnt_attach_dynamic(&adapter->msf_sicount, EVCNT_TYPE_INTR,
1757 NULL, xname, "multimode softint");
1758 evcnt_attach_dynamic(&adapter->phy_sicount, EVCNT_TYPE_INTR,
1759 NULL, xname, "external PHY softint");
1760
1761 /* Max number of traffic class is 8 */
1762 KASSERT(IXGBE_DCB_MAX_TRAFFIC_CLASS == 8);
1763 for (i = 0; i < IXGBE_TC_COUNTER_NUM; i++) {
1764 snprintf(adapter->tcs[i].evnamebuf,
1765 sizeof(adapter->tcs[i].evnamebuf), "%s tc%d",
1766 xname, i);
1767 if (i < __arraycount(stats->mpc)) {
1768 evcnt_attach_dynamic(&stats->mpc[i],
1769 EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
1770 "RX Missed Packet Count");
1771 if (hw->mac.type == ixgbe_mac_82598EB)
1772 evcnt_attach_dynamic(&stats->rnbc[i],
1773 EVCNT_TYPE_MISC, NULL,
1774 adapter->tcs[i].evnamebuf,
1775 "Receive No Buffers");
1776 }
1777 if (i < __arraycount(stats->pxontxc)) {
1778 evcnt_attach_dynamic(&stats->pxontxc[i],
1779 EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
1780 "pxontxc");
1781 evcnt_attach_dynamic(&stats->pxonrxc[i],
1782 EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
1783 "pxonrxc");
1784 evcnt_attach_dynamic(&stats->pxofftxc[i],
1785 EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
1786 "pxofftxc");
1787 evcnt_attach_dynamic(&stats->pxoffrxc[i],
1788 EVCNT_TYPE_MISC, NULL, adapter->tcs[i].evnamebuf,
1789 "pxoffrxc");
1790 if (hw->mac.type >= ixgbe_mac_82599EB)
1791 evcnt_attach_dynamic(&stats->pxon2offc[i],
1792 EVCNT_TYPE_MISC, NULL,
1793 adapter->tcs[i].evnamebuf,
1794 "pxon2offc");
1795 }
1796 }
1797
1798 for (i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
1799 #ifdef LRO
1800 struct lro_ctrl *lro = &rxr->lro;
1801 #endif /* LRO */
1802
1803 snprintf(adapter->queues[i].evnamebuf,
1804 sizeof(adapter->queues[i].evnamebuf), "%s q%d",
1805 xname, i);
1806 snprintf(adapter->queues[i].namebuf,
1807 sizeof(adapter->queues[i].namebuf), "q%d", i);
1808
1809 if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
1810 aprint_error_dev(dev, "could not create sysctl root\n");
1811 break;
1812 }
1813
1814 if (sysctl_createv(log, 0, &rnode, &rnode,
1815 0, CTLTYPE_NODE,
1816 adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
1817 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
1818 break;
1819
1820 if (sysctl_createv(log, 0, &rnode, &cnode,
1821 CTLFLAG_READWRITE, CTLTYPE_INT,
1822 "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
1823 ixgbe_sysctl_interrupt_rate_handler, 0,
1824 (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
1825 break;
1826
1827 if (sysctl_createv(log, 0, &rnode, &cnode,
1828 CTLFLAG_READONLY, CTLTYPE_INT,
1829 "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
1830 ixgbe_sysctl_tdh_handler, 0, (void *)txr,
1831 0, CTL_CREATE, CTL_EOL) != 0)
1832 break;
1833
1834 if (sysctl_createv(log, 0, &rnode, &cnode,
1835 CTLFLAG_READONLY, CTLTYPE_INT,
1836 "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
1837 ixgbe_sysctl_tdt_handler, 0, (void *)txr,
1838 0, CTL_CREATE, CTL_EOL) != 0)
1839 break;
1840
1841 evcnt_attach_dynamic(&adapter->queues[i].irqs, EVCNT_TYPE_INTR,
1842 NULL, adapter->queues[i].evnamebuf, "IRQs on queue");
1843 evcnt_attach_dynamic(&adapter->queues[i].handleq,
1844 EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
1845 "Handled queue in softint");
1846 evcnt_attach_dynamic(&adapter->queues[i].req, EVCNT_TYPE_MISC,
1847 NULL, adapter->queues[i].evnamebuf, "Requeued in softint");
1848 evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
1849 NULL, adapter->queues[i].evnamebuf, "TSO");
1850 evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
1851 NULL, adapter->queues[i].evnamebuf,
1852 "Queue No Descriptor Available");
1853 evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
1854 NULL, adapter->queues[i].evnamebuf,
1855 "Queue Packets Transmitted");
1856 #ifndef IXGBE_LEGACY_TX
1857 evcnt_attach_dynamic(&txr->pcq_drops, EVCNT_TYPE_MISC,
1858 NULL, adapter->queues[i].evnamebuf,
1859 "Packets dropped in pcq");
1860 #endif
1861
1862 if (sysctl_createv(log, 0, &rnode, &cnode,
1863 CTLFLAG_READONLY,
1864 CTLTYPE_INT,
1865 "rxd_nxck", SYSCTL_DESCR("Receive Descriptor next to check"),
1866 ixgbe_sysctl_next_to_check_handler, 0, (void *)rxr, 0,
1867 CTL_CREATE, CTL_EOL) != 0)
1868 break;
1869
1870 if (sysctl_createv(log, 0, &rnode, &cnode,
1871 CTLFLAG_READONLY,
1872 CTLTYPE_INT,
1873 "rxd_head", SYSCTL_DESCR("Receive Descriptor Head"),
1874 ixgbe_sysctl_rdh_handler, 0, (void *)rxr, 0,
1875 CTL_CREATE, CTL_EOL) != 0)
1876 break;
1877
1878 if (sysctl_createv(log, 0, &rnode, &cnode,
1879 CTLFLAG_READONLY,
1880 CTLTYPE_INT,
1881 "rxd_tail", SYSCTL_DESCR("Receive Descriptor Tail"),
1882 ixgbe_sysctl_rdt_handler, 0, (void *)rxr, 0,
1883 CTL_CREATE, CTL_EOL) != 0)
1884 break;
1885
1886 if (i < __arraycount(stats->qprc)) {
1887 evcnt_attach_dynamic(&stats->qprc[i],
1888 EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
1889 "qprc");
1890 evcnt_attach_dynamic(&stats->qptc[i],
1891 EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
1892 "qptc");
1893 evcnt_attach_dynamic(&stats->qbrc[i],
1894 EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
1895 "qbrc");
1896 evcnt_attach_dynamic(&stats->qbtc[i],
1897 EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
1898 "qbtc");
1899 if (hw->mac.type >= ixgbe_mac_82599EB)
1900 evcnt_attach_dynamic(&stats->qprdc[i],
1901 EVCNT_TYPE_MISC, NULL,
1902 adapter->queues[i].evnamebuf, "qprdc");
1903 }
1904
1905 evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
1906 NULL, adapter->queues[i].evnamebuf, "Queue Packets Received");
1907 evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
1908 NULL, adapter->queues[i].evnamebuf, "Queue Bytes Received");
1909 evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
1910 NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
1911 evcnt_attach_dynamic(&rxr->no_jmbuf, EVCNT_TYPE_MISC,
1912 NULL, adapter->queues[i].evnamebuf, "Rx no jumbo mbuf");
1913 evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
1914 NULL, adapter->queues[i].evnamebuf, "Rx discarded");
1915 #ifdef LRO
1916 SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
1917 CTLFLAG_RD, &lro->lro_queued, 0,
1918 "LRO Queued");
1919 SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
1920 CTLFLAG_RD, &lro->lro_flushed, 0,
1921 "LRO Flushed");
1922 #endif /* LRO */
1923 }
1924
1925 /* MAC stats get their own sub node */
1926
1927 snprintf(stats->namebuf,
1928 sizeof(stats->namebuf), "%s MAC Statistics", xname);
1929
1930 evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
1931 stats->namebuf, "rx csum offload - IP");
1932 evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
1933 stats->namebuf, "rx csum offload - L4");
1934 evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
1935 stats->namebuf, "rx csum offload - IP bad");
1936 evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
1937 stats->namebuf, "rx csum offload - L4 bad");
1938 evcnt_attach_dynamic(&stats->intzero, EVCNT_TYPE_MISC, NULL,
1939 stats->namebuf, "Interrupt conditions zero");
1940 evcnt_attach_dynamic(&stats->legint, EVCNT_TYPE_MISC, NULL,
1941 stats->namebuf, "Legacy interrupts");
1942
1943 evcnt_attach_dynamic(&stats->crcerrs, EVCNT_TYPE_MISC, NULL,
1944 stats->namebuf, "CRC Errors");
1945 evcnt_attach_dynamic(&stats->illerrc, EVCNT_TYPE_MISC, NULL,
1946 stats->namebuf, "Illegal Byte Errors");
1947 evcnt_attach_dynamic(&stats->errbc, EVCNT_TYPE_MISC, NULL,
1948 stats->namebuf, "Byte Errors");
1949 evcnt_attach_dynamic(&stats->mspdc, EVCNT_TYPE_MISC, NULL,
1950 stats->namebuf, "MAC Short Packets Discarded");
1951 if (hw->mac.type >= ixgbe_mac_X550)
1952 evcnt_attach_dynamic(&stats->mbsdc, EVCNT_TYPE_MISC, NULL,
1953 stats->namebuf, "Bad SFD");
1954 evcnt_attach_dynamic(&stats->mpctotal, EVCNT_TYPE_MISC, NULL,
1955 stats->namebuf, "Total Packets Missed");
1956 evcnt_attach_dynamic(&stats->mlfc, EVCNT_TYPE_MISC, NULL,
1957 stats->namebuf, "MAC Local Faults");
1958 evcnt_attach_dynamic(&stats->mrfc, EVCNT_TYPE_MISC, NULL,
1959 stats->namebuf, "MAC Remote Faults");
1960 evcnt_attach_dynamic(&stats->rlec, EVCNT_TYPE_MISC, NULL,
1961 stats->namebuf, "Receive Length Errors");
1962 evcnt_attach_dynamic(&stats->lxontxc, EVCNT_TYPE_MISC, NULL,
1963 stats->namebuf, "Link XON Transmitted");
1964 evcnt_attach_dynamic(&stats->lxonrxc, EVCNT_TYPE_MISC, NULL,
1965 stats->namebuf, "Link XON Received");
1966 evcnt_attach_dynamic(&stats->lxofftxc, EVCNT_TYPE_MISC, NULL,
1967 stats->namebuf, "Link XOFF Transmitted");
1968 evcnt_attach_dynamic(&stats->lxoffrxc, EVCNT_TYPE_MISC, NULL,
1969 stats->namebuf, "Link XOFF Received");
1970
1971 /* Packet Reception Stats */
1972 evcnt_attach_dynamic(&stats->tor, EVCNT_TYPE_MISC, NULL,
1973 stats->namebuf, "Total Octets Received");
1974 evcnt_attach_dynamic(&stats->gorc, EVCNT_TYPE_MISC, NULL,
1975 stats->namebuf, "Good Octets Received");
1976 evcnt_attach_dynamic(&stats->tpr, EVCNT_TYPE_MISC, NULL,
1977 stats->namebuf, "Total Packets Received");
1978 evcnt_attach_dynamic(&stats->gprc, EVCNT_TYPE_MISC, NULL,
1979 stats->namebuf, "Good Packets Received");
1980 evcnt_attach_dynamic(&stats->mprc, EVCNT_TYPE_MISC, NULL,
1981 stats->namebuf, "Multicast Packets Received");
1982 evcnt_attach_dynamic(&stats->bprc, EVCNT_TYPE_MISC, NULL,
1983 stats->namebuf, "Broadcast Packets Received");
1984 evcnt_attach_dynamic(&stats->prc64, EVCNT_TYPE_MISC, NULL,
1985 stats->namebuf, "64 byte frames received ");
1986 evcnt_attach_dynamic(&stats->prc127, EVCNT_TYPE_MISC, NULL,
1987 stats->namebuf, "65-127 byte frames received");
1988 evcnt_attach_dynamic(&stats->prc255, EVCNT_TYPE_MISC, NULL,
1989 stats->namebuf, "128-255 byte frames received");
1990 evcnt_attach_dynamic(&stats->prc511, EVCNT_TYPE_MISC, NULL,
1991 stats->namebuf, "256-511 byte frames received");
1992 evcnt_attach_dynamic(&stats->prc1023, EVCNT_TYPE_MISC, NULL,
1993 stats->namebuf, "512-1023 byte frames received");
1994 evcnt_attach_dynamic(&stats->prc1522, EVCNT_TYPE_MISC, NULL,
1995 stats->namebuf, "1023-1522 byte frames received");
1996 evcnt_attach_dynamic(&stats->ruc, EVCNT_TYPE_MISC, NULL,
1997 stats->namebuf, "Receive Undersized");
1998 evcnt_attach_dynamic(&stats->rfc, EVCNT_TYPE_MISC, NULL,
1999 stats->namebuf, "Fragmented Packets Received ");
2000 evcnt_attach_dynamic(&stats->roc, EVCNT_TYPE_MISC, NULL,
2001 stats->namebuf, "Oversized Packets Received");
2002 evcnt_attach_dynamic(&stats->rjc, EVCNT_TYPE_MISC, NULL,
2003 stats->namebuf, "Received Jabber");
2004 evcnt_attach_dynamic(&stats->mngprc, EVCNT_TYPE_MISC, NULL,
2005 stats->namebuf, "Management Packets Received");
2006 evcnt_attach_dynamic(&stats->mngpdc, EVCNT_TYPE_MISC, NULL,
2007 stats->namebuf, "Management Packets Dropped");
2008 evcnt_attach_dynamic(&stats->xec, EVCNT_TYPE_MISC, NULL,
2009 stats->namebuf, "Checksum Errors");
2010
2011 /* Packet Transmission Stats */
2012 evcnt_attach_dynamic(&stats->gotc, EVCNT_TYPE_MISC, NULL,
2013 stats->namebuf, "Good Octets Transmitted");
2014 evcnt_attach_dynamic(&stats->tpt, EVCNT_TYPE_MISC, NULL,
2015 stats->namebuf, "Total Packets Transmitted");
2016 evcnt_attach_dynamic(&stats->gptc, EVCNT_TYPE_MISC, NULL,
2017 stats->namebuf, "Good Packets Transmitted");
2018 evcnt_attach_dynamic(&stats->bptc, EVCNT_TYPE_MISC, NULL,
2019 stats->namebuf, "Broadcast Packets Transmitted");
2020 evcnt_attach_dynamic(&stats->mptc, EVCNT_TYPE_MISC, NULL,
2021 stats->namebuf, "Multicast Packets Transmitted");
2022 evcnt_attach_dynamic(&stats->mngptc, EVCNT_TYPE_MISC, NULL,
2023 stats->namebuf, "Management Packets Transmitted");
2024 evcnt_attach_dynamic(&stats->ptc64, EVCNT_TYPE_MISC, NULL,
2025 stats->namebuf, "64 byte frames transmitted ");
2026 evcnt_attach_dynamic(&stats->ptc127, EVCNT_TYPE_MISC, NULL,
2027 stats->namebuf, "65-127 byte frames transmitted");
2028 evcnt_attach_dynamic(&stats->ptc255, EVCNT_TYPE_MISC, NULL,
2029 stats->namebuf, "128-255 byte frames transmitted");
2030 evcnt_attach_dynamic(&stats->ptc511, EVCNT_TYPE_MISC, NULL,
2031 stats->namebuf, "256-511 byte frames transmitted");
2032 evcnt_attach_dynamic(&stats->ptc1023, EVCNT_TYPE_MISC, NULL,
2033 stats->namebuf, "512-1023 byte frames transmitted");
2034 evcnt_attach_dynamic(&stats->ptc1522, EVCNT_TYPE_MISC, NULL,
2035 stats->namebuf, "1024-1522 byte frames transmitted");
2036 } /* ixgbe_add_hw_stats */
2037
2038 static void
2039 ixgbe_clear_evcnt(struct adapter *adapter)
2040 {
2041 struct tx_ring *txr = adapter->tx_rings;
2042 struct rx_ring *rxr = adapter->rx_rings;
2043 struct ixgbe_hw *hw = &adapter->hw;
2044 struct ixgbe_hw_stats *stats = &adapter->stats.pf;
2045 int i;
2046
2047 adapter->efbig_tx_dma_setup.ev_count = 0;
2048 adapter->mbuf_defrag_failed.ev_count = 0;
2049 adapter->efbig2_tx_dma_setup.ev_count = 0;
2050 adapter->einval_tx_dma_setup.ev_count = 0;
2051 adapter->other_tx_dma_setup.ev_count = 0;
2052 adapter->eagain_tx_dma_setup.ev_count = 0;
2053 adapter->enomem_tx_dma_setup.ev_count = 0;
2054 adapter->tso_err.ev_count = 0;
2055 adapter->watchdog_events.ev_count = 0;
2056 adapter->link_irq.ev_count = 0;
2057 adapter->link_sicount.ev_count = 0;
2058 adapter->mod_sicount.ev_count = 0;
2059 adapter->msf_sicount.ev_count = 0;
2060 adapter->phy_sicount.ev_count = 0;
2061
2062 for (i = 0; i < IXGBE_TC_COUNTER_NUM; i++) {
2063 if (i < __arraycount(stats->mpc)) {
2064 stats->mpc[i].ev_count = 0;
2065 if (hw->mac.type == ixgbe_mac_82598EB)
2066 stats->rnbc[i].ev_count = 0;
2067 }
2068 if (i < __arraycount(stats->pxontxc)) {
2069 stats->pxontxc[i].ev_count = 0;
2070 stats->pxonrxc[i].ev_count = 0;
2071 stats->pxofftxc[i].ev_count = 0;
2072 stats->pxoffrxc[i].ev_count = 0;
2073 if (hw->mac.type >= ixgbe_mac_82599EB)
2074 stats->pxon2offc[i].ev_count = 0;
2075 }
2076 }
2077
2078 txr = adapter->tx_rings;
2079 for (i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
2080 adapter->queues[i].irqs.ev_count = 0;
2081 adapter->queues[i].handleq.ev_count = 0;
2082 adapter->queues[i].req.ev_count = 0;
2083 txr->no_desc_avail.ev_count = 0;
2084 txr->total_packets.ev_count = 0;
2085 txr->tso_tx.ev_count = 0;
2086 #ifndef IXGBE_LEGACY_TX
2087 txr->pcq_drops.ev_count = 0;
2088 #endif
2089 txr->q_efbig_tx_dma_setup = 0;
2090 txr->q_mbuf_defrag_failed = 0;
2091 txr->q_efbig2_tx_dma_setup = 0;
2092 txr->q_einval_tx_dma_setup = 0;
2093 txr->q_other_tx_dma_setup = 0;
2094 txr->q_eagain_tx_dma_setup = 0;
2095 txr->q_enomem_tx_dma_setup = 0;
2096 txr->q_tso_err = 0;
2097
2098 if (i < __arraycount(stats->qprc)) {
2099 stats->qprc[i].ev_count = 0;
2100 stats->qptc[i].ev_count = 0;
2101 stats->qbrc[i].ev_count = 0;
2102 stats->qbtc[i].ev_count = 0;
2103 if (hw->mac.type >= ixgbe_mac_82599EB)
2104 stats->qprdc[i].ev_count = 0;
2105 }
2106
2107 rxr->rx_packets.ev_count = 0;
2108 rxr->rx_bytes.ev_count = 0;
2109 rxr->rx_copies.ev_count = 0;
2110 rxr->no_jmbuf.ev_count = 0;
2111 rxr->rx_discarded.ev_count = 0;
2112 }
2113 stats->ipcs.ev_count = 0;
2114 stats->l4cs.ev_count = 0;
2115 stats->ipcs_bad.ev_count = 0;
2116 stats->l4cs_bad.ev_count = 0;
2117 stats->intzero.ev_count = 0;
2118 stats->legint.ev_count = 0;
2119 stats->crcerrs.ev_count = 0;
2120 stats->illerrc.ev_count = 0;
2121 stats->errbc.ev_count = 0;
2122 stats->mspdc.ev_count = 0;
2123 if (hw->mac.type >= ixgbe_mac_X550)
2124 stats->mbsdc.ev_count = 0;
2125 stats->mpctotal.ev_count = 0;
2126 stats->mlfc.ev_count = 0;
2127 stats->mrfc.ev_count = 0;
2128 stats->rlec.ev_count = 0;
2129 stats->lxontxc.ev_count = 0;
2130 stats->lxonrxc.ev_count = 0;
2131 stats->lxofftxc.ev_count = 0;
2132 stats->lxoffrxc.ev_count = 0;
2133
2134 /* Packet Reception Stats */
2135 stats->tor.ev_count = 0;
2136 stats->gorc.ev_count = 0;
2137 stats->tpr.ev_count = 0;
2138 stats->gprc.ev_count = 0;
2139 stats->mprc.ev_count = 0;
2140 stats->bprc.ev_count = 0;
2141 stats->prc64.ev_count = 0;
2142 stats->prc127.ev_count = 0;
2143 stats->prc255.ev_count = 0;
2144 stats->prc511.ev_count = 0;
2145 stats->prc1023.ev_count = 0;
2146 stats->prc1522.ev_count = 0;
2147 stats->ruc.ev_count = 0;
2148 stats->rfc.ev_count = 0;
2149 stats->roc.ev_count = 0;
2150 stats->rjc.ev_count = 0;
2151 stats->mngprc.ev_count = 0;
2152 stats->mngpdc.ev_count = 0;
2153 stats->xec.ev_count = 0;
2154
2155 /* Packet Transmission Stats */
2156 stats->gotc.ev_count = 0;
2157 stats->tpt.ev_count = 0;
2158 stats->gptc.ev_count = 0;
2159 stats->bptc.ev_count = 0;
2160 stats->mptc.ev_count = 0;
2161 stats->mngptc.ev_count = 0;
2162 stats->ptc64.ev_count = 0;
2163 stats->ptc127.ev_count = 0;
2164 stats->ptc255.ev_count = 0;
2165 stats->ptc511.ev_count = 0;
2166 stats->ptc1023.ev_count = 0;
2167 stats->ptc1522.ev_count = 0;
2168 }
2169
2170 /************************************************************************
2171 * ixgbe_sysctl_tdh_handler - Transmit Descriptor Head handler function
2172 *
2173 * Retrieves the TDH value from the hardware
2174 ************************************************************************/
2175 static int
2176 ixgbe_sysctl_tdh_handler(SYSCTLFN_ARGS)
2177 {
2178 struct sysctlnode node = *rnode;
2179 struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
2180 struct adapter *adapter;
2181 uint32_t val;
2182
2183 if (!txr)
2184 return (0);
2185
2186 adapter = txr->adapter;
2187 if (ixgbe_fw_recovery_mode_swflag(adapter))
2188 return (EPERM);
2189
2190 val = IXGBE_READ_REG(&adapter->hw, IXGBE_TDH(txr->me));
2191 node.sysctl_data = &val;
2192 return sysctl_lookup(SYSCTLFN_CALL(&node));
2193 } /* ixgbe_sysctl_tdh_handler */
2194
2195 /************************************************************************
2196 * ixgbe_sysctl_tdt_handler - Transmit Descriptor Tail handler function
2197 *
2198 * Retrieves the TDT value from the hardware
2199 ************************************************************************/
2200 static int
2201 ixgbe_sysctl_tdt_handler(SYSCTLFN_ARGS)
2202 {
2203 struct sysctlnode node = *rnode;
2204 struct tx_ring *txr = (struct tx_ring *)node.sysctl_data;
2205 struct adapter *adapter;
2206 uint32_t val;
2207
2208 if (!txr)
2209 return (0);
2210
2211 adapter = txr->adapter;
2212 if (ixgbe_fw_recovery_mode_swflag(adapter))
2213 return (EPERM);
2214
2215 val = IXGBE_READ_REG(&adapter->hw, IXGBE_TDT(txr->me));
2216 node.sysctl_data = &val;
2217 return sysctl_lookup(SYSCTLFN_CALL(&node));
2218 } /* ixgbe_sysctl_tdt_handler */
2219
2220 /************************************************************************
2221 * ixgbe_sysctl_next_to_check_handler - Receive Descriptor next to check
2222 * handler function
2223 *
2224 * Retrieves the next_to_check value
2225 ************************************************************************/
2226 static int
2227 ixgbe_sysctl_next_to_check_handler(SYSCTLFN_ARGS)
2228 {
2229 struct sysctlnode node = *rnode;
2230 struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
2231 struct adapter *adapter;
2232 uint32_t val;
2233
2234 if (!rxr)
2235 return (0);
2236
2237 adapter = rxr->adapter;
2238 if (ixgbe_fw_recovery_mode_swflag(adapter))
2239 return (EPERM);
2240
2241 val = rxr->next_to_check;
2242 node.sysctl_data = &val;
2243 return sysctl_lookup(SYSCTLFN_CALL(&node));
2244 } /* ixgbe_sysctl_next_to_check_handler */
2245
2246 /************************************************************************
2247 * ixgbe_sysctl_rdh_handler - Receive Descriptor Head handler function
2248 *
2249 * Retrieves the RDH value from the hardware
2250 ************************************************************************/
2251 static int
2252 ixgbe_sysctl_rdh_handler(SYSCTLFN_ARGS)
2253 {
2254 struct sysctlnode node = *rnode;
2255 struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
2256 struct adapter *adapter;
2257 uint32_t val;
2258
2259 if (!rxr)
2260 return (0);
2261
2262 adapter = rxr->adapter;
2263 if (ixgbe_fw_recovery_mode_swflag(adapter))
2264 return (EPERM);
2265
2266 val = IXGBE_READ_REG(&adapter->hw, IXGBE_RDH(rxr->me));
2267 node.sysctl_data = &val;
2268 return sysctl_lookup(SYSCTLFN_CALL(&node));
2269 } /* ixgbe_sysctl_rdh_handler */
2270
2271 /************************************************************************
2272 * ixgbe_sysctl_rdt_handler - Receive Descriptor Tail handler function
2273 *
2274 * Retrieves the RDT value from the hardware
2275 ************************************************************************/
2276 static int
2277 ixgbe_sysctl_rdt_handler(SYSCTLFN_ARGS)
2278 {
2279 struct sysctlnode node = *rnode;
2280 struct rx_ring *rxr = (struct rx_ring *)node.sysctl_data;
2281 struct adapter *adapter;
2282 uint32_t val;
2283
2284 if (!rxr)
2285 return (0);
2286
2287 adapter = rxr->adapter;
2288 if (ixgbe_fw_recovery_mode_swflag(adapter))
2289 return (EPERM);
2290
2291 val = IXGBE_READ_REG(&adapter->hw, IXGBE_RDT(rxr->me));
2292 node.sysctl_data = &val;
2293 return sysctl_lookup(SYSCTLFN_CALL(&node));
2294 } /* ixgbe_sysctl_rdt_handler */
2295
2296 static int
2297 ixgbe_vlan_cb(struct ethercom *ec, uint16_t vid, bool set)
2298 {
2299 struct ifnet *ifp = &ec->ec_if;
2300 struct adapter *adapter = ifp->if_softc;
2301 int rv;
2302
2303 if (set)
2304 rv = ixgbe_register_vlan(adapter, vid);
2305 else
2306 rv = ixgbe_unregister_vlan(adapter, vid);
2307
2308 if (rv != 0)
2309 return rv;
2310
2311 /*
2312 * Control VLAN HW tagging when ec_nvlan is changed from 1 to 0
2313 * or 0 to 1.
2314 */
2315 if ((set && (ec->ec_nvlans == 1)) || (!set && (ec->ec_nvlans == 0)))
2316 ixgbe_setup_vlan_hw_tagging(adapter);
2317
2318 return rv;
2319 }
2320
2321 /************************************************************************
2322 * ixgbe_register_vlan
2323 *
2324 * Run via vlan config EVENT, it enables us to use the
2325 * HW Filter table since we can get the vlan id. This
2326 * just creates the entry in the soft version of the
2327 * VFTA, init will repopulate the real table.
2328 ************************************************************************/
2329 static int
2330 ixgbe_register_vlan(struct adapter *adapter, u16 vtag)
2331 {
2332 u16 index, bit;
2333 int error;
2334
2335 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
2336 return EINVAL;
2337
2338 IXGBE_CORE_LOCK(adapter);
2339 index = (vtag >> 5) & 0x7F;
2340 bit = vtag & 0x1F;
2341 adapter->shadow_vfta[index] |= ((u32)1 << bit);
2342 error = adapter->hw.mac.ops.set_vfta(&adapter->hw, vtag, 0, true,
2343 true);
2344 IXGBE_CORE_UNLOCK(adapter);
2345 if (error != 0)
2346 error = EACCES;
2347
2348 return error;
2349 } /* ixgbe_register_vlan */
2350
2351 /************************************************************************
2352 * ixgbe_unregister_vlan
2353 *
2354 * Run via vlan unconfig EVENT, remove our entry in the soft vfta.
2355 ************************************************************************/
2356 static int
2357 ixgbe_unregister_vlan(struct adapter *adapter, u16 vtag)
2358 {
2359 u16 index, bit;
2360 int error;
2361
2362 if ((vtag == 0) || (vtag > 4095)) /* Invalid */
2363 return EINVAL;
2364
2365 IXGBE_CORE_LOCK(adapter);
2366 index = (vtag >> 5) & 0x7F;
2367 bit = vtag & 0x1F;
2368 adapter->shadow_vfta[index] &= ~((u32)1 << bit);
2369 error = adapter->hw.mac.ops.set_vfta(&adapter->hw, vtag, 0, false,
2370 true);
2371 IXGBE_CORE_UNLOCK(adapter);
2372 if (error != 0)
2373 error = EACCES;
2374
2375 return error;
2376 } /* ixgbe_unregister_vlan */
2377
2378 static void
2379 ixgbe_setup_vlan_hw_tagging(struct adapter *adapter)
2380 {
2381 struct ethercom *ec = &adapter->osdep.ec;
2382 struct ixgbe_hw *hw = &adapter->hw;
2383 struct rx_ring *rxr;
2384 u32 ctrl;
2385 int i;
2386 bool hwtagging;
2387
2388 /* Enable HW tagging only if any vlan is attached */
2389 hwtagging = (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING)
2390 && VLAN_ATTACHED(ec);
2391
2392 /* Setup the queues for vlans */
2393 for (i = 0; i < adapter->num_queues; i++) {
2394 rxr = &adapter->rx_rings[i];
2395 /*
2396 * On 82599 and later, the VLAN enable is per/queue in RXDCTL.
2397 */
2398 if (hw->mac.type != ixgbe_mac_82598EB) {
2399 ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
2400 if (hwtagging)
2401 ctrl |= IXGBE_RXDCTL_VME;
2402 else
2403 ctrl &= ~IXGBE_RXDCTL_VME;
2404 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), ctrl);
2405 }
2406 rxr->vtag_strip = hwtagging ? TRUE : FALSE;
2407 }
2408
2409 /* VLAN hw tagging for 82598 */
2410 if (hw->mac.type == ixgbe_mac_82598EB) {
2411 ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
2412 if (hwtagging)
2413 ctrl |= IXGBE_VLNCTRL_VME;
2414 else
2415 ctrl &= ~IXGBE_VLNCTRL_VME;
2416 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
2417 }
2418 } /* ixgbe_setup_vlan_hw_tagging */
2419
2420 static void
2421 ixgbe_setup_vlan_hw_support(struct adapter *adapter)
2422 {
2423 struct ethercom *ec = &adapter->osdep.ec;
2424 struct ixgbe_hw *hw = &adapter->hw;
2425 int i;
2426 u32 ctrl;
2427 struct vlanid_list *vlanidp;
2428
2429 /*
2430 * This function is called from both if_init and ifflags_cb()
2431 * on NetBSD.
2432 */
2433
2434 /*
2435 * Part 1:
2436 * Setup VLAN HW tagging
2437 */
2438 ixgbe_setup_vlan_hw_tagging(adapter);
2439
2440 /*
2441 * Part 2:
2442 * Setup VLAN HW filter
2443 */
2444 /* Cleanup shadow_vfta */
2445 for (i = 0; i < IXGBE_VFTA_SIZE; i++)
2446 adapter->shadow_vfta[i] = 0;
2447 /* Generate shadow_vfta from ec_vids */
2448 ETHER_LOCK(ec);
2449 SIMPLEQ_FOREACH(vlanidp, &ec->ec_vids, vid_list) {
2450 uint32_t idx;
2451
2452 idx = vlanidp->vid / 32;
2453 KASSERT(idx < IXGBE_VFTA_SIZE);
2454 adapter->shadow_vfta[idx] |= (u32)1 << (vlanidp->vid % 32);
2455 }
2456 ETHER_UNLOCK(ec);
2457 for (i = 0; i < IXGBE_VFTA_SIZE; i++)
2458 IXGBE_WRITE_REG(hw, IXGBE_VFTA(i), adapter->shadow_vfta[i]);
2459
2460 ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
2461 /* Enable the Filter Table if enabled */
2462 if (ec->ec_capenable & ETHERCAP_VLAN_HWFILTER)
2463 ctrl |= IXGBE_VLNCTRL_VFE;
2464 else
2465 ctrl &= ~IXGBE_VLNCTRL_VFE;
2466 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
2467 } /* ixgbe_setup_vlan_hw_support */
2468
2469 /************************************************************************
2470 * ixgbe_get_slot_info
2471 *
2472 * Get the width and transaction speed of
2473 * the slot this adapter is plugged into.
2474 ************************************************************************/
2475 static void
2476 ixgbe_get_slot_info(struct adapter *adapter)
2477 {
2478 device_t dev = adapter->dev;
2479 struct ixgbe_hw *hw = &adapter->hw;
2480 u32 offset;
2481 u16 link;
2482 int bus_info_valid = TRUE;
2483
2484 /* Some devices are behind an internal bridge */
2485 switch (hw->device_id) {
2486 case IXGBE_DEV_ID_82599_SFP_SF_QP:
2487 case IXGBE_DEV_ID_82599_QSFP_SF_QP:
2488 goto get_parent_info;
2489 default:
2490 break;
2491 }
2492
2493 ixgbe_get_bus_info(hw);
2494
2495 /*
2496 * Some devices don't use PCI-E, but there is no need
2497 * to display "Unknown" for bus speed and width.
2498 */
2499 switch (hw->mac.type) {
2500 case ixgbe_mac_X550EM_x:
2501 case ixgbe_mac_X550EM_a:
2502 return;
2503 default:
2504 goto display;
2505 }
2506
2507 get_parent_info:
2508 /*
2509 * For the Quad port adapter we need to parse back
2510 * up the PCI tree to find the speed of the expansion
2511 * slot into which this adapter is plugged. A bit more work.
2512 */
2513 dev = device_parent(device_parent(dev));
2514 #if 0
2515 #ifdef IXGBE_DEBUG
2516 device_printf(dev, "parent pcib = %x,%x,%x\n", pci_get_bus(dev),
2517 pci_get_slot(dev), pci_get_function(dev));
2518 #endif
2519 dev = device_parent(device_parent(dev));
2520 #ifdef IXGBE_DEBUG
2521 device_printf(dev, "slot pcib = %x,%x,%x\n", pci_get_bus(dev),
2522 pci_get_slot(dev), pci_get_function(dev));
2523 #endif
2524 #endif
2525 /* Now get the PCI Express Capabilities offset */
2526 if (pci_get_capability(adapter->osdep.pc, adapter->osdep.tag,
2527 PCI_CAP_PCIEXPRESS, &offset, NULL)) {
2528 /*
2529 * Hmm...can't get PCI-Express capabilities.
2530 * Falling back to default method.
2531 */
2532 bus_info_valid = FALSE;
2533 ixgbe_get_bus_info(hw);
2534 goto display;
2535 }
2536 /* ...and read the Link Status Register */
2537 link = pci_conf_read(adapter->osdep.pc, adapter->osdep.tag,
2538 offset + PCIE_LCSR) >> 16;
2539 ixgbe_set_pci_config_data_generic(hw, link);
2540
2541 display:
2542 device_printf(dev, "PCI Express Bus: Speed %s Width %s\n",
2543 ((hw->bus.speed == ixgbe_bus_speed_8000) ? "8.0GT/s" :
2544 (hw->bus.speed == ixgbe_bus_speed_5000) ? "5.0GT/s" :
2545 (hw->bus.speed == ixgbe_bus_speed_2500) ? "2.5GT/s" :
2546 "Unknown"),
2547 ((hw->bus.width == ixgbe_bus_width_pcie_x8) ? "x8" :
2548 (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "x4" :
2549 (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "x1" :
2550 "Unknown"));
2551
2552 if (bus_info_valid) {
2553 if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
2554 ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
2555 (hw->bus.speed == ixgbe_bus_speed_2500))) {
2556 device_printf(dev, "PCI-Express bandwidth available"
2557 " for this card\n is not sufficient for"
2558 " optimal performance.\n");
2559 device_printf(dev, "For optimal performance a x8 "
2560 "PCIE, or x4 PCIE Gen2 slot is required.\n");
2561 }
2562 if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
2563 ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
2564 (hw->bus.speed < ixgbe_bus_speed_8000))) {
2565 device_printf(dev, "PCI-Express bandwidth available"
2566 " for this card\n is not sufficient for"
2567 " optimal performance.\n");
2568 device_printf(dev, "For optimal performance a x8 "
2569 "PCIE Gen3 slot is required.\n");
2570 }
2571 } else
2572 device_printf(dev, "Unable to determine slot speed/width. The speed/width reported are that of the internal switch.\n");
2573
2574 return;
2575 } /* ixgbe_get_slot_info */
2576
2577 /************************************************************************
2578 * ixgbe_enable_queue - MSI-X Interrupt Handlers and Tasklets
2579 ************************************************************************/
2580 static inline void
2581 ixgbe_enable_queue(struct adapter *adapter, u32 vector)
2582 {
2583 struct ixgbe_hw *hw = &adapter->hw;
2584 struct ix_queue *que = &adapter->queues[vector];
2585 u64 queue = 1ULL << vector;
2586 u32 mask;
2587
2588 mutex_enter(&que->dc_mtx);
2589 if (que->disabled_count > 0 && --que->disabled_count > 0)
2590 goto out;
2591
2592 if (hw->mac.type == ixgbe_mac_82598EB) {
2593 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
2594 IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
2595 } else {
2596 mask = (queue & 0xFFFFFFFF);
2597 if (mask)
2598 IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
2599 mask = (queue >> 32);
2600 if (mask)
2601 IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
2602 }
2603 out:
2604 mutex_exit(&que->dc_mtx);
2605 } /* ixgbe_enable_queue */
2606
2607 /************************************************************************
2608 * ixgbe_disable_queue_internal
2609 ************************************************************************/
2610 static inline void
2611 ixgbe_disable_queue_internal(struct adapter *adapter, u32 vector, bool nestok)
2612 {
2613 struct ixgbe_hw *hw = &adapter->hw;
2614 struct ix_queue *que = &adapter->queues[vector];
2615 u64 queue = 1ULL << vector;
2616 u32 mask;
2617
2618 mutex_enter(&que->dc_mtx);
2619
2620 if (que->disabled_count > 0) {
2621 if (nestok)
2622 que->disabled_count++;
2623 goto out;
2624 }
2625 que->disabled_count++;
2626
2627 if (hw->mac.type == ixgbe_mac_82598EB) {
2628 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
2629 IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
2630 } else {
2631 mask = (queue & 0xFFFFFFFF);
2632 if (mask)
2633 IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
2634 mask = (queue >> 32);
2635 if (mask)
2636 IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
2637 }
2638 out:
2639 mutex_exit(&que->dc_mtx);
2640 } /* ixgbe_disable_queue_internal */
2641
2642 /************************************************************************
2643 * ixgbe_disable_queue
2644 ************************************************************************/
2645 static inline void
2646 ixgbe_disable_queue(struct adapter *adapter, u32 vector)
2647 {
2648
2649 ixgbe_disable_queue_internal(adapter, vector, true);
2650 } /* ixgbe_disable_queue */
2651
2652 /************************************************************************
2653 * ixgbe_sched_handle_que - schedule deferred packet processing
2654 ************************************************************************/
2655 static inline void
2656 ixgbe_sched_handle_que(struct adapter *adapter, struct ix_queue *que)
2657 {
2658
2659 if (que->txrx_use_workqueue) {
2660 /*
2661 * adapter->que_wq is bound to each CPU instead of
2662 * each NIC queue to reduce workqueue kthread. As we
2663 * should consider about interrupt affinity in this
2664 * function, the workqueue kthread must be WQ_PERCPU.
2665 * If create WQ_PERCPU workqueue kthread for each NIC
2666 * queue, that number of created workqueue kthread is
2667 * (number of used NIC queue) * (number of CPUs) =
2668 * (number of CPUs) ^ 2 most often.
2669 *
2670 * The same NIC queue's interrupts are avoided by
2671 * masking the queue's interrupt. And different
2672 * NIC queue's interrupts use different struct work
2673 * (que->wq_cookie). So, "enqueued flag" to avoid
2674 * twice workqueue_enqueue() is not required .
2675 */
2676 workqueue_enqueue(adapter->que_wq, &que->wq_cookie, curcpu());
2677 } else {
2678 softint_schedule(que->que_si);
2679 }
2680 }
2681
2682 /************************************************************************
2683 * ixgbe_msix_que - MSI-X Queue Interrupt Service routine
2684 ************************************************************************/
2685 static int
2686 ixgbe_msix_que(void *arg)
2687 {
2688 struct ix_queue *que = arg;
2689 struct adapter *adapter = que->adapter;
2690 struct ifnet *ifp = adapter->ifp;
2691 struct tx_ring *txr = que->txr;
2692 struct rx_ring *rxr = que->rxr;
2693 bool more;
2694 u32 newitr = 0;
2695
2696 /* Protect against spurious interrupts */
2697 if ((ifp->if_flags & IFF_RUNNING) == 0)
2698 return 0;
2699
2700 ixgbe_disable_queue(adapter, que->msix);
2701 ++que->irqs.ev_count;
2702
2703 /*
2704 * Don't change "que->txrx_use_workqueue" from this point to avoid
2705 * flip-flopping softint/workqueue mode in one deferred processing.
2706 */
2707 que->txrx_use_workqueue = adapter->txrx_use_workqueue;
2708
2709 #ifdef __NetBSD__
2710 /* Don't run ixgbe_rxeof in interrupt context */
2711 more = true;
2712 #else
2713 more = ixgbe_rxeof(que);
2714 #endif
2715
2716 IXGBE_TX_LOCK(txr);
2717 ixgbe_txeof(txr);
2718 IXGBE_TX_UNLOCK(txr);
2719
2720 /* Do AIM now? */
2721
2722 if (adapter->enable_aim == false)
2723 goto no_calc;
2724 /*
2725 * Do Adaptive Interrupt Moderation:
2726 * - Write out last calculated setting
2727 * - Calculate based on average size over
2728 * the last interval.
2729 */
2730 if (que->eitr_setting)
2731 ixgbe_eitr_write(adapter, que->msix, que->eitr_setting);
2732
2733 que->eitr_setting = 0;
2734
2735 /* Idle, do nothing */
2736 if ((txr->bytes == 0) && (rxr->bytes == 0))
2737 goto no_calc;
2738
2739 if ((txr->bytes) && (txr->packets))
2740 newitr = txr->bytes/txr->packets;
2741 if ((rxr->bytes) && (rxr->packets))
2742 newitr = uimax(newitr, (rxr->bytes / rxr->packets));
2743 newitr += 24; /* account for hardware frame, crc */
2744
2745 /* set an upper boundary */
2746 newitr = uimin(newitr, 3000);
2747
2748 /* Be nice to the mid range */
2749 if ((newitr > 300) && (newitr < 1200))
2750 newitr = (newitr / 3);
2751 else
2752 newitr = (newitr / 2);
2753
2754 /*
2755 * When RSC is used, ITR interval must be larger than RSC_DELAY.
2756 * Currently, we use 2us for RSC_DELAY. The minimum value is always
2757 * greater than 2us on 100M (and 10M?(not documented)), but it's not
2758 * on 1G and higher.
2759 */
2760 if ((adapter->link_speed != IXGBE_LINK_SPEED_100_FULL)
2761 && (adapter->link_speed != IXGBE_LINK_SPEED_10_FULL)) {
2762 if (newitr < IXGBE_MIN_RSC_EITR_10G1G)
2763 newitr = IXGBE_MIN_RSC_EITR_10G1G;
2764 }
2765
2766 /* save for next interrupt */
2767 que->eitr_setting = newitr;
2768
2769 /* Reset state */
2770 txr->bytes = 0;
2771 txr->packets = 0;
2772 rxr->bytes = 0;
2773 rxr->packets = 0;
2774
2775 no_calc:
2776 if (more)
2777 ixgbe_sched_handle_que(adapter, que);
2778 else
2779 ixgbe_enable_queue(adapter, que->msix);
2780
2781 return 1;
2782 } /* ixgbe_msix_que */
2783
2784 /************************************************************************
2785 * ixgbe_media_status - Media Ioctl callback
2786 *
2787 * Called whenever the user queries the status of
2788 * the interface using ifconfig.
2789 ************************************************************************/
2790 static void
2791 ixgbe_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
2792 {
2793 struct adapter *adapter = ifp->if_softc;
2794 struct ixgbe_hw *hw = &adapter->hw;
2795 int layer;
2796
2797 INIT_DEBUGOUT("ixgbe_media_status: begin");
2798 IXGBE_CORE_LOCK(adapter);
2799 ixgbe_update_link_status(adapter);
2800
2801 ifmr->ifm_status = IFM_AVALID;
2802 ifmr->ifm_active = IFM_ETHER;
2803
2804 if (adapter->link_active != LINK_STATE_UP) {
2805 ifmr->ifm_active |= IFM_NONE;
2806 IXGBE_CORE_UNLOCK(adapter);
2807 return;
2808 }
2809
2810 ifmr->ifm_status |= IFM_ACTIVE;
2811 layer = adapter->phy_layer;
2812
2813 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T ||
2814 layer & IXGBE_PHYSICAL_LAYER_5GBASE_T ||
2815 layer & IXGBE_PHYSICAL_LAYER_2500BASE_T ||
2816 layer & IXGBE_PHYSICAL_LAYER_1000BASE_T ||
2817 layer & IXGBE_PHYSICAL_LAYER_100BASE_TX ||
2818 layer & IXGBE_PHYSICAL_LAYER_10BASE_T)
2819 switch (adapter->link_speed) {
2820 case IXGBE_LINK_SPEED_10GB_FULL:
2821 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
2822 break;
2823 case IXGBE_LINK_SPEED_5GB_FULL:
2824 ifmr->ifm_active |= IFM_5000_T | IFM_FDX;
2825 break;
2826 case IXGBE_LINK_SPEED_2_5GB_FULL:
2827 ifmr->ifm_active |= IFM_2500_T | IFM_FDX;
2828 break;
2829 case IXGBE_LINK_SPEED_1GB_FULL:
2830 ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
2831 break;
2832 case IXGBE_LINK_SPEED_100_FULL:
2833 ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
2834 break;
2835 case IXGBE_LINK_SPEED_10_FULL:
2836 ifmr->ifm_active |= IFM_10_T | IFM_FDX;
2837 break;
2838 }
2839 if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU ||
2840 layer & IXGBE_PHYSICAL_LAYER_SFP_ACTIVE_DA)
2841 switch (adapter->link_speed) {
2842 case IXGBE_LINK_SPEED_10GB_FULL:
2843 ifmr->ifm_active |= IFM_10G_TWINAX | IFM_FDX;
2844 break;
2845 }
2846 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LR)
2847 switch (adapter->link_speed) {
2848 case IXGBE_LINK_SPEED_10GB_FULL:
2849 ifmr->ifm_active |= IFM_10G_LR | IFM_FDX;
2850 break;
2851 case IXGBE_LINK_SPEED_1GB_FULL:
2852 ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
2853 break;
2854 }
2855 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_LRM)
2856 switch (adapter->link_speed) {
2857 case IXGBE_LINK_SPEED_10GB_FULL:
2858 ifmr->ifm_active |= IFM_10G_LRM | IFM_FDX;
2859 break;
2860 case IXGBE_LINK_SPEED_1GB_FULL:
2861 ifmr->ifm_active |= IFM_1000_LX | IFM_FDX;
2862 break;
2863 }
2864 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR ||
2865 layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX)
2866 switch (adapter->link_speed) {
2867 case IXGBE_LINK_SPEED_10GB_FULL:
2868 ifmr->ifm_active |= IFM_10G_SR | IFM_FDX;
2869 break;
2870 case IXGBE_LINK_SPEED_1GB_FULL:
2871 ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
2872 break;
2873 }
2874 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_CX4)
2875 switch (adapter->link_speed) {
2876 case IXGBE_LINK_SPEED_10GB_FULL:
2877 ifmr->ifm_active |= IFM_10G_CX4 | IFM_FDX;
2878 break;
2879 }
2880 /*
2881 * XXX: These need to use the proper media types once
2882 * they're added.
2883 */
2884 if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KR)
2885 switch (adapter->link_speed) {
2886 case IXGBE_LINK_SPEED_10GB_FULL:
2887 ifmr->ifm_active |= IFM_10G_KR | IFM_FDX;
2888 break;
2889 case IXGBE_LINK_SPEED_2_5GB_FULL:
2890 ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
2891 break;
2892 case IXGBE_LINK_SPEED_1GB_FULL:
2893 ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
2894 break;
2895 }
2896 else if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_KX4 ||
2897 layer & IXGBE_PHYSICAL_LAYER_2500BASE_KX ||
2898 layer & IXGBE_PHYSICAL_LAYER_1000BASE_KX)
2899 switch (adapter->link_speed) {
2900 case IXGBE_LINK_SPEED_10GB_FULL:
2901 ifmr->ifm_active |= IFM_10G_KX4 | IFM_FDX;
2902 break;
2903 case IXGBE_LINK_SPEED_2_5GB_FULL:
2904 ifmr->ifm_active |= IFM_2500_KX | IFM_FDX;
2905 break;
2906 case IXGBE_LINK_SPEED_1GB_FULL:
2907 ifmr->ifm_active |= IFM_1000_KX | IFM_FDX;
2908 break;
2909 }
2910
2911 /* If nothing is recognized... */
2912 #if 0
2913 if (IFM_SUBTYPE(ifmr->ifm_active) == 0)
2914 ifmr->ifm_active |= IFM_UNKNOWN;
2915 #endif
2916
2917 ifp->if_baudrate = ifmedia_baudrate(ifmr->ifm_active);
2918
2919 /* Display current flow control setting used on link */
2920 if (hw->fc.current_mode == ixgbe_fc_rx_pause ||
2921 hw->fc.current_mode == ixgbe_fc_full)
2922 ifmr->ifm_active |= IFM_ETH_RXPAUSE;
2923 if (hw->fc.current_mode == ixgbe_fc_tx_pause ||
2924 hw->fc.current_mode == ixgbe_fc_full)
2925 ifmr->ifm_active |= IFM_ETH_TXPAUSE;
2926
2927 IXGBE_CORE_UNLOCK(adapter);
2928
2929 return;
2930 } /* ixgbe_media_status */
2931
2932 /************************************************************************
2933 * ixgbe_media_change - Media Ioctl callback
2934 *
2935 * Called when the user changes speed/duplex using
2936 * media/mediopt option with ifconfig.
2937 ************************************************************************/
2938 static int
2939 ixgbe_media_change(struct ifnet *ifp)
2940 {
2941 struct adapter *adapter = ifp->if_softc;
2942 struct ifmedia *ifm = &adapter->media;
2943 struct ixgbe_hw *hw = &adapter->hw;
2944 ixgbe_link_speed speed = 0;
2945 ixgbe_link_speed link_caps = 0;
2946 bool negotiate = false;
2947 s32 err = IXGBE_NOT_IMPLEMENTED;
2948
2949 INIT_DEBUGOUT("ixgbe_media_change: begin");
2950
2951 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2952 return (EINVAL);
2953
2954 if (hw->phy.media_type == ixgbe_media_type_backplane)
2955 return (EPERM);
2956
2957 IXGBE_CORE_LOCK(adapter);
2958 /*
2959 * We don't actually need to check against the supported
2960 * media types of the adapter; ifmedia will take care of
2961 * that for us.
2962 */
2963 switch (IFM_SUBTYPE(ifm->ifm_media)) {
2964 case IFM_AUTO:
2965 err = hw->mac.ops.get_link_capabilities(hw, &link_caps,
2966 &negotiate);
2967 if (err != IXGBE_SUCCESS) {
2968 device_printf(adapter->dev, "Unable to determine "
2969 "supported advertise speeds\n");
2970 IXGBE_CORE_UNLOCK(adapter);
2971 return (ENODEV);
2972 }
2973 speed |= link_caps;
2974 break;
2975 case IFM_10G_T:
2976 case IFM_10G_LRM:
2977 case IFM_10G_LR:
2978 case IFM_10G_TWINAX:
2979 case IFM_10G_SR:
2980 case IFM_10G_CX4:
2981 case IFM_10G_KR:
2982 case IFM_10G_KX4:
2983 speed |= IXGBE_LINK_SPEED_10GB_FULL;
2984 break;
2985 case IFM_5000_T:
2986 speed |= IXGBE_LINK_SPEED_5GB_FULL;
2987 break;
2988 case IFM_2500_T:
2989 case IFM_2500_KX:
2990 speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
2991 break;
2992 case IFM_1000_T:
2993 case IFM_1000_LX:
2994 case IFM_1000_SX:
2995 case IFM_1000_KX:
2996 speed |= IXGBE_LINK_SPEED_1GB_FULL;
2997 break;
2998 case IFM_100_TX:
2999 speed |= IXGBE_LINK_SPEED_100_FULL;
3000 break;
3001 case IFM_10_T:
3002 speed |= IXGBE_LINK_SPEED_10_FULL;
3003 break;
3004 case IFM_NONE:
3005 break;
3006 default:
3007 goto invalid;
3008 }
3009
3010 hw->mac.autotry_restart = TRUE;
3011 hw->mac.ops.setup_link(hw, speed, TRUE);
3012 adapter->advertise = 0;
3013 if (IFM_SUBTYPE(ifm->ifm_media) != IFM_AUTO) {
3014 if ((speed & IXGBE_LINK_SPEED_10GB_FULL) != 0)
3015 adapter->advertise |= 1 << 2;
3016 if ((speed & IXGBE_LINK_SPEED_1GB_FULL) != 0)
3017 adapter->advertise |= 1 << 1;
3018 if ((speed & IXGBE_LINK_SPEED_100_FULL) != 0)
3019 adapter->advertise |= 1 << 0;
3020 if ((speed & IXGBE_LINK_SPEED_10_FULL) != 0)
3021 adapter->advertise |= 1 << 3;
3022 if ((speed & IXGBE_LINK_SPEED_2_5GB_FULL) != 0)
3023 adapter->advertise |= 1 << 4;
3024 if ((speed & IXGBE_LINK_SPEED_5GB_FULL) != 0)
3025 adapter->advertise |= 1 << 5;
3026 }
3027
3028 IXGBE_CORE_UNLOCK(adapter);
3029 return (0);
3030
3031 invalid:
3032 device_printf(adapter->dev, "Invalid media type!\n");
3033 IXGBE_CORE_UNLOCK(adapter);
3034
3035 return (EINVAL);
3036 } /* ixgbe_media_change */
3037
3038 /************************************************************************
3039 * ixgbe_msix_link - Link status change ISR (MSI/MSI-X)
3040 ************************************************************************/
3041 static int
3042 ixgbe_msix_link(void *arg)
3043 {
3044 struct adapter *adapter = arg;
3045 struct ixgbe_hw *hw = &adapter->hw;
3046 u32 eicr, eicr_mask;
3047 s32 retval;
3048
3049 ++adapter->link_irq.ev_count;
3050
3051 /* Pause other interrupts */
3052 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_OTHER);
3053
3054 /* First get the cause */
3055 /*
3056 * The specifications of 82598, 82599, X540 and X550 say EICS register
3057 * is write only. However, Linux says it is a workaround for silicon
3058 * errata to read EICS instead of EICR to get interrupt cause. It seems
3059 * there is a problem about read clear mechanism for EICR register.
3060 */
3061 eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
3062 /* Be sure the queue bits are not cleared */
3063 eicr &= ~IXGBE_EICR_RTX_QUEUE;
3064 /* Clear interrupt with write */
3065 IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr);
3066
3067 if (ixgbe_is_sfp(hw)) {
3068 /* Pluggable optics-related interrupt */
3069 if (hw->mac.type >= ixgbe_mac_X540)
3070 eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
3071 else
3072 eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
3073
3074 /*
3075 * An interrupt might not arrive when a module is inserted.
3076 * When an link status change interrupt occurred and the driver
3077 * still regard SFP as unplugged, issue the module softint
3078 * and then issue LSC interrupt.
3079 */
3080 if ((eicr & eicr_mask)
3081 || ((hw->phy.sfp_type == ixgbe_sfp_type_not_present)
3082 && (eicr & IXGBE_EICR_LSC))) {
3083 IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
3084 softint_schedule(adapter->mod_si);
3085 }
3086
3087 if ((hw->mac.type == ixgbe_mac_82599EB) &&
3088 (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
3089 IXGBE_WRITE_REG(hw, IXGBE_EICR,
3090 IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3091 softint_schedule(adapter->msf_si);
3092 }
3093 }
3094
3095 /* Link status change */
3096 if (eicr & IXGBE_EICR_LSC) {
3097 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_LSC);
3098 softint_schedule(adapter->link_si);
3099 }
3100
3101 if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
3102 if ((adapter->feat_en & IXGBE_FEATURE_FDIR) &&
3103 (eicr & IXGBE_EICR_FLOW_DIR)) {
3104 /* This is probably overkill :) */
3105 if (!atomic_cas_uint(&adapter->fdir_reinit, 0, 1))
3106 return 1;
3107 /* Disable the interrupt */
3108 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EIMC_FLOW_DIR);
3109 softint_schedule(adapter->fdir_si);
3110 }
3111
3112 if (eicr & IXGBE_EICR_ECC) {
3113 device_printf(adapter->dev,
3114 "CRITICAL: ECC ERROR!! Please Reboot!!\n");
3115 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
3116 }
3117
3118 /* Check for over temp condition */
3119 if (adapter->feat_en & IXGBE_FEATURE_TEMP_SENSOR) {
3120 switch (adapter->hw.mac.type) {
3121 case ixgbe_mac_X550EM_a:
3122 if (!(eicr & IXGBE_EICR_GPI_SDP0_X550EM_a))
3123 break;
3124 IXGBE_WRITE_REG(hw, IXGBE_EIMC,
3125 IXGBE_EICR_GPI_SDP0_X550EM_a);
3126 IXGBE_WRITE_REG(hw, IXGBE_EICR,
3127 IXGBE_EICR_GPI_SDP0_X550EM_a);
3128 retval = hw->phy.ops.check_overtemp(hw);
3129 if (retval != IXGBE_ERR_OVERTEMP)
3130 break;
3131 device_printf(adapter->dev, "CRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
3132 device_printf(adapter->dev, "System shutdown required!\n");
3133 break;
3134 default:
3135 if (!(eicr & IXGBE_EICR_TS))
3136 break;
3137 retval = hw->phy.ops.check_overtemp(hw);
3138 if (retval != IXGBE_ERR_OVERTEMP)
3139 break;
3140 device_printf(adapter->dev, "CRITICAL: OVER TEMP!! PHY IS SHUT DOWN!!\n");
3141 device_printf(adapter->dev, "System shutdown required!\n");
3142 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
3143 break;
3144 }
3145 }
3146
3147 /* Check for VF message */
3148 if ((adapter->feat_en & IXGBE_FEATURE_SRIOV) &&
3149 (eicr & IXGBE_EICR_MAILBOX))
3150 softint_schedule(adapter->mbx_si);
3151 }
3152
3153 /* Check for fan failure */
3154 if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL) {
3155 ixgbe_check_fan_failure(adapter, eicr, TRUE);
3156 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
3157 }
3158
3159 /* External PHY interrupt */
3160 if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
3161 (eicr & IXGBE_EICR_GPI_SDP0_X540)) {
3162 IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP0_X540);
3163 softint_schedule(adapter->phy_si);
3164 }
3165
3166 /* Re-enable other interrupts */
3167 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
3168 return 1;
3169 } /* ixgbe_msix_link */
3170
3171 static void
3172 ixgbe_eitr_write(struct adapter *adapter, uint32_t index, uint32_t itr)
3173 {
3174
3175 if (adapter->hw.mac.type == ixgbe_mac_82598EB)
3176 itr |= itr << 16;
3177 else
3178 itr |= IXGBE_EITR_CNT_WDIS;
3179
3180 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EITR(index), itr);
3181 }
3182
3183
3184 /************************************************************************
3185 * ixgbe_sysctl_interrupt_rate_handler
3186 ************************************************************************/
3187 static int
3188 ixgbe_sysctl_interrupt_rate_handler(SYSCTLFN_ARGS)
3189 {
3190 struct sysctlnode node = *rnode;
3191 struct ix_queue *que = (struct ix_queue *)node.sysctl_data;
3192 struct adapter *adapter;
3193 uint32_t reg, usec, rate;
3194 int error;
3195
3196 if (que == NULL)
3197 return 0;
3198
3199 adapter = que->adapter;
3200 if (ixgbe_fw_recovery_mode_swflag(adapter))
3201 return (EPERM);
3202
3203 reg = IXGBE_READ_REG(&adapter->hw, IXGBE_EITR(que->msix));
3204 usec = ((reg & 0x0FF8) >> 3);
3205 if (usec > 0)
3206 rate = 500000 / usec;
3207 else
3208 rate = 0;
3209 node.sysctl_data = &rate;
3210 error = sysctl_lookup(SYSCTLFN_CALL(&node));
3211 if (error || newp == NULL)
3212 return error;
3213 reg &= ~0xfff; /* default, no limitation */
3214 if (rate > 0 && rate < 500000) {
3215 if (rate < 1000)
3216 rate = 1000;
3217 reg |= ((4000000/rate) & 0xff8);
3218 /*
3219 * When RSC is used, ITR interval must be larger than
3220 * RSC_DELAY. Currently, we use 2us for RSC_DELAY.
3221 * The minimum value is always greater than 2us on 100M
3222 * (and 10M?(not documented)), but it's not on 1G and higher.
3223 */
3224 if ((adapter->link_speed != IXGBE_LINK_SPEED_100_FULL)
3225 && (adapter->link_speed != IXGBE_LINK_SPEED_10_FULL)) {
3226 if ((adapter->num_queues > 1)
3227 && (reg < IXGBE_MIN_RSC_EITR_10G1G))
3228 return EINVAL;
3229 }
3230 ixgbe_max_interrupt_rate = rate;
3231 } else
3232 ixgbe_max_interrupt_rate = 0;
3233 ixgbe_eitr_write(adapter, que->msix, reg);
3234
3235 return (0);
3236 } /* ixgbe_sysctl_interrupt_rate_handler */
3237
3238 const struct sysctlnode *
3239 ixgbe_sysctl_instance(struct adapter *adapter)
3240 {
3241 const char *dvname;
3242 struct sysctllog **log;
3243 int rc;
3244 const struct sysctlnode *rnode;
3245
3246 if (adapter->sysctltop != NULL)
3247 return adapter->sysctltop;
3248
3249 log = &adapter->sysctllog;
3250 dvname = device_xname(adapter->dev);
3251
3252 if ((rc = sysctl_createv(log, 0, NULL, &rnode,
3253 0, CTLTYPE_NODE, dvname,
3254 SYSCTL_DESCR("ixgbe information and settings"),
3255 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
3256 goto err;
3257
3258 return rnode;
3259 err:
3260 device_printf(adapter->dev,
3261 "%s: sysctl_createv failed, rc = %d\n", __func__, rc);
3262 return NULL;
3263 }
3264
3265 /************************************************************************
3266 * ixgbe_add_device_sysctls
3267 ************************************************************************/
3268 static void
3269 ixgbe_add_device_sysctls(struct adapter *adapter)
3270 {
3271 device_t dev = adapter->dev;
3272 struct ixgbe_hw *hw = &adapter->hw;
3273 struct sysctllog **log;
3274 const struct sysctlnode *rnode, *cnode;
3275
3276 log = &adapter->sysctllog;
3277
3278 if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
3279 aprint_error_dev(dev, "could not create sysctl root\n");
3280 return;
3281 }
3282
3283 if (sysctl_createv(log, 0, &rnode, &cnode,
3284 CTLFLAG_READWRITE, CTLTYPE_INT,
3285 "debug", SYSCTL_DESCR("Debug Info"),
3286 ixgbe_sysctl_debug, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
3287 aprint_error_dev(dev, "could not create sysctl\n");
3288
3289 if (sysctl_createv(log, 0, &rnode, &cnode,
3290 CTLFLAG_READONLY, CTLTYPE_INT,
3291 "num_rx_desc", SYSCTL_DESCR("Number of rx descriptors"),
3292 NULL, 0, &adapter->num_rx_desc, 0, CTL_CREATE, CTL_EOL) != 0)
3293 aprint_error_dev(dev, "could not create sysctl\n");
3294
3295 if (sysctl_createv(log, 0, &rnode, &cnode,
3296 CTLFLAG_READONLY, CTLTYPE_INT,
3297 "num_queues", SYSCTL_DESCR("Number of queues"),
3298 NULL, 0, &adapter->num_queues, 0, CTL_CREATE, CTL_EOL) != 0)
3299 aprint_error_dev(dev, "could not create sysctl\n");
3300
3301 /* Sysctls for all devices */
3302 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
3303 CTLTYPE_INT, "fc", SYSCTL_DESCR(IXGBE_SYSCTL_DESC_SET_FC),
3304 ixgbe_sysctl_flowcntl, 0, (void *)adapter, 0, CTL_CREATE,
3305 CTL_EOL) != 0)
3306 aprint_error_dev(dev, "could not create sysctl\n");
3307
3308 adapter->enable_aim = ixgbe_enable_aim;
3309 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
3310 CTLTYPE_BOOL, "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
3311 NULL, 0, &adapter->enable_aim, 0, CTL_CREATE, CTL_EOL) != 0)
3312 aprint_error_dev(dev, "could not create sysctl\n");
3313
3314 if (sysctl_createv(log, 0, &rnode, &cnode,
3315 CTLFLAG_READWRITE, CTLTYPE_INT,
3316 "advertise_speed", SYSCTL_DESCR(IXGBE_SYSCTL_DESC_ADV_SPEED),
3317 ixgbe_sysctl_advertise, 0, (void *)adapter, 0, CTL_CREATE,
3318 CTL_EOL) != 0)
3319 aprint_error_dev(dev, "could not create sysctl\n");
3320
3321 /*
3322 * If each "que->txrx_use_workqueue" is changed in sysctl handler,
3323 * it causesflip-flopping softint/workqueue mode in one deferred
3324 * processing. Therefore, preempt_disable()/preempt_enable() are
3325 * required in ixgbe_sched_handle_que() to avoid
3326 * KASSERT(ixgbe_sched_handle_que()) in softint_schedule().
3327 * I think changing "que->txrx_use_workqueue" in interrupt handler
3328 * is lighter than doing preempt_disable()/preempt_enable() in every
3329 * ixgbe_sched_handle_que().
3330 */
3331 adapter->txrx_use_workqueue = ixgbe_txrx_workqueue;
3332 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
3333 CTLTYPE_BOOL, "txrx_workqueue", SYSCTL_DESCR("Use workqueue for packet processing"),
3334 NULL, 0, &adapter->txrx_use_workqueue, 0, CTL_CREATE, CTL_EOL) != 0)
3335 aprint_error_dev(dev, "could not create sysctl\n");
3336
3337 #ifdef IXGBE_DEBUG
3338 /* testing sysctls (for all devices) */
3339 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
3340 CTLTYPE_INT, "power_state", SYSCTL_DESCR("PCI Power State"),
3341 ixgbe_sysctl_power_state, 0, (void *)adapter, 0, CTL_CREATE,
3342 CTL_EOL) != 0)
3343 aprint_error_dev(dev, "could not create sysctl\n");
3344
3345 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READONLY,
3346 CTLTYPE_STRING, "print_rss_config",
3347 SYSCTL_DESCR("Prints RSS Configuration"),
3348 ixgbe_sysctl_print_rss_config, 0, (void *)adapter, 0, CTL_CREATE,
3349 CTL_EOL) != 0)
3350 aprint_error_dev(dev, "could not create sysctl\n");
3351 #endif
3352 /* for X550 series devices */
3353 if (hw->mac.type >= ixgbe_mac_X550)
3354 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
3355 CTLTYPE_INT, "dmac", SYSCTL_DESCR("DMA Coalesce"),
3356 ixgbe_sysctl_dmac, 0, (void *)adapter, 0, CTL_CREATE,
3357 CTL_EOL) != 0)
3358 aprint_error_dev(dev, "could not create sysctl\n");
3359
3360 /* for WoL-capable devices */
3361 if (adapter->wol_support) {
3362 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
3363 CTLTYPE_BOOL, "wol_enable",
3364 SYSCTL_DESCR("Enable/Disable Wake on LAN"),
3365 ixgbe_sysctl_wol_enable, 0, (void *)adapter, 0, CTL_CREATE,
3366 CTL_EOL) != 0)
3367 aprint_error_dev(dev, "could not create sysctl\n");
3368
3369 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
3370 CTLTYPE_INT, "wufc",
3371 SYSCTL_DESCR("Enable/Disable Wake Up Filters"),
3372 ixgbe_sysctl_wufc, 0, (void *)adapter, 0, CTL_CREATE,
3373 CTL_EOL) != 0)
3374 aprint_error_dev(dev, "could not create sysctl\n");
3375 }
3376
3377 /* for X552/X557-AT devices */
3378 if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T) {
3379 const struct sysctlnode *phy_node;
3380
3381 if (sysctl_createv(log, 0, &rnode, &phy_node, 0, CTLTYPE_NODE,
3382 "phy", SYSCTL_DESCR("External PHY sysctls"),
3383 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0) {
3384 aprint_error_dev(dev, "could not create sysctl\n");
3385 return;
3386 }
3387
3388 if (sysctl_createv(log, 0, &phy_node, &cnode, CTLFLAG_READONLY,
3389 CTLTYPE_INT, "temp",
3390 SYSCTL_DESCR("Current External PHY Temperature (Celsius)"),
3391 ixgbe_sysctl_phy_temp, 0, (void *)adapter, 0, CTL_CREATE,
3392 CTL_EOL) != 0)
3393 aprint_error_dev(dev, "could not create sysctl\n");
3394
3395 if (sysctl_createv(log, 0, &phy_node, &cnode, CTLFLAG_READONLY,
3396 CTLTYPE_INT, "overtemp_occurred",
3397 SYSCTL_DESCR("External PHY High Temperature Event Occurred"),
3398 ixgbe_sysctl_phy_overtemp_occurred, 0, (void *)adapter, 0,
3399 CTL_CREATE, CTL_EOL) != 0)
3400 aprint_error_dev(dev, "could not create sysctl\n");
3401 }
3402
3403 if ((hw->mac.type == ixgbe_mac_X550EM_a)
3404 && (hw->phy.type == ixgbe_phy_fw))
3405 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
3406 CTLTYPE_BOOL, "force_10_100_autonego",
3407 SYSCTL_DESCR("Force autonego on 10M and 100M"),
3408 NULL, 0, &hw->phy.force_10_100_autonego, 0,
3409 CTL_CREATE, CTL_EOL) != 0)
3410 aprint_error_dev(dev, "could not create sysctl\n");
3411
3412 if (adapter->feat_cap & IXGBE_FEATURE_EEE) {
3413 if (sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
3414 CTLTYPE_INT, "eee_state",
3415 SYSCTL_DESCR("EEE Power Save State"),
3416 ixgbe_sysctl_eee_state, 0, (void *)adapter, 0, CTL_CREATE,
3417 CTL_EOL) != 0)
3418 aprint_error_dev(dev, "could not create sysctl\n");
3419 }
3420 } /* ixgbe_add_device_sysctls */
3421
3422 /************************************************************************
3423 * ixgbe_allocate_pci_resources
3424 ************************************************************************/
3425 static int
3426 ixgbe_allocate_pci_resources(struct adapter *adapter,
3427 const struct pci_attach_args *pa)
3428 {
3429 pcireg_t memtype, csr;
3430 device_t dev = adapter->dev;
3431 bus_addr_t addr;
3432 int flags;
3433
3434 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
3435 switch (memtype) {
3436 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
3437 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
3438 adapter->osdep.mem_bus_space_tag = pa->pa_memt;
3439 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
3440 memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
3441 goto map_err;
3442 if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
3443 aprint_normal_dev(dev, "clearing prefetchable bit\n");
3444 flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
3445 }
3446 if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
3447 adapter->osdep.mem_size, flags,
3448 &adapter->osdep.mem_bus_space_handle) != 0) {
3449 map_err:
3450 adapter->osdep.mem_size = 0;
3451 aprint_error_dev(dev, "unable to map BAR0\n");
3452 return ENXIO;
3453 }
3454 /*
3455 * Enable address decoding for memory range in case BIOS or
3456 * UEFI don't set it.
3457 */
3458 csr = pci_conf_read(pa->pa_pc, pa->pa_tag,
3459 PCI_COMMAND_STATUS_REG);
3460 csr |= PCI_COMMAND_MEM_ENABLE;
3461 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
3462 csr);
3463 break;
3464 default:
3465 aprint_error_dev(dev, "unexpected type on BAR0\n");
3466 return ENXIO;
3467 }
3468
3469 return (0);
3470 } /* ixgbe_allocate_pci_resources */
3471
3472 static void
3473 ixgbe_free_softint(struct adapter *adapter)
3474 {
3475 struct ix_queue *que = adapter->queues;
3476 struct tx_ring *txr = adapter->tx_rings;
3477 int i;
3478
3479 for (i = 0; i < adapter->num_queues; i++, que++, txr++) {
3480 if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX)) {
3481 if (txr->txr_si != NULL)
3482 softint_disestablish(txr->txr_si);
3483 }
3484 if (que->que_si != NULL)
3485 softint_disestablish(que->que_si);
3486 }
3487 if (adapter->txr_wq != NULL)
3488 workqueue_destroy(adapter->txr_wq);
3489 if (adapter->txr_wq_enqueued != NULL)
3490 percpu_free(adapter->txr_wq_enqueued, sizeof(u_int));
3491 if (adapter->que_wq != NULL)
3492 workqueue_destroy(adapter->que_wq);
3493
3494 /* Drain the Link queue */
3495 if (adapter->link_si != NULL) {
3496 softint_disestablish(adapter->link_si);
3497 adapter->link_si = NULL;
3498 }
3499 if (adapter->mod_si != NULL) {
3500 softint_disestablish(adapter->mod_si);
3501 adapter->mod_si = NULL;
3502 }
3503 if (adapter->msf_si != NULL) {
3504 softint_disestablish(adapter->msf_si);
3505 adapter->msf_si = NULL;
3506 }
3507 if (adapter->phy_si != NULL) {
3508 softint_disestablish(adapter->phy_si);
3509 adapter->phy_si = NULL;
3510 }
3511 if (adapter->feat_en & IXGBE_FEATURE_FDIR) {
3512 if (adapter->fdir_si != NULL) {
3513 softint_disestablish(adapter->fdir_si);
3514 adapter->fdir_si = NULL;
3515 }
3516 }
3517 if (adapter->feat_cap & IXGBE_FEATURE_SRIOV) {
3518 if (adapter->mbx_si != NULL) {
3519 softint_disestablish(adapter->mbx_si);
3520 adapter->mbx_si = NULL;
3521 }
3522 }
3523 } /* ixgbe_free_softint */
3524
3525 /************************************************************************
3526 * ixgbe_detach - Device removal routine
3527 *
3528 * Called when the driver is being removed.
3529 * Stops the adapter and deallocates all the resources
3530 * that were allocated for driver operation.
3531 *
3532 * return 0 on success, positive on failure
3533 ************************************************************************/
3534 static int
3535 ixgbe_detach(device_t dev, int flags)
3536 {
3537 struct adapter *adapter = device_private(dev);
3538 struct rx_ring *rxr = adapter->rx_rings;
3539 struct tx_ring *txr = adapter->tx_rings;
3540 struct ixgbe_hw *hw = &adapter->hw;
3541 struct ixgbe_hw_stats *stats = &adapter->stats.pf;
3542 u32 ctrl_ext;
3543 int i;
3544
3545 INIT_DEBUGOUT("ixgbe_detach: begin");
3546 if (adapter->osdep.attached == false)
3547 return 0;
3548
3549 if (ixgbe_pci_iov_detach(dev) != 0) {
3550 device_printf(dev, "SR-IOV in use; detach first.\n");
3551 return (EBUSY);
3552 }
3553
3554 /*
3555 * Stop the interface. ixgbe_setup_low_power_mode() calls ixgbe_stop(),
3556 * so it's not required to call ixgbe_stop() directly.
3557 */
3558 IXGBE_CORE_LOCK(adapter);
3559 ixgbe_setup_low_power_mode(adapter);
3560 IXGBE_CORE_UNLOCK(adapter);
3561 #if NVLAN > 0
3562 /* Make sure VLANs are not using driver */
3563 if (!VLAN_ATTACHED(&adapter->osdep.ec))
3564 ; /* nothing to do: no VLANs */
3565 else if ((flags & (DETACH_SHUTDOWN | DETACH_FORCE)) != 0)
3566 vlan_ifdetach(adapter->ifp);
3567 else {
3568 aprint_error_dev(dev, "VLANs in use, detach first\n");
3569 return (EBUSY);
3570 }
3571 #endif
3572
3573 pmf_device_deregister(dev);
3574
3575 ether_ifdetach(adapter->ifp);
3576
3577 ixgbe_free_softint(adapter);
3578
3579 /* let hardware know driver is unloading */
3580 ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
3581 ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
3582 IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT, ctrl_ext);
3583
3584 callout_halt(&adapter->timer, NULL);
3585 if (adapter->feat_en & IXGBE_FEATURE_RECOVERY_MODE)
3586 callout_halt(&adapter->recovery_mode_timer, NULL);
3587
3588 if (adapter->feat_en & IXGBE_FEATURE_NETMAP)
3589 netmap_detach(adapter->ifp);
3590
3591 ixgbe_free_pci_resources(adapter);
3592 #if 0 /* XXX the NetBSD port is probably missing something here */
3593 bus_generic_detach(dev);
3594 #endif
3595 if_detach(adapter->ifp);
3596 if_percpuq_destroy(adapter->ipq);
3597
3598 sysctl_teardown(&adapter->sysctllog);
3599 evcnt_detach(&adapter->efbig_tx_dma_setup);
3600 evcnt_detach(&adapter->mbuf_defrag_failed);
3601 evcnt_detach(&adapter->efbig2_tx_dma_setup);
3602 evcnt_detach(&adapter->einval_tx_dma_setup);
3603 evcnt_detach(&adapter->other_tx_dma_setup);
3604 evcnt_detach(&adapter->eagain_tx_dma_setup);
3605 evcnt_detach(&adapter->enomem_tx_dma_setup);
3606 evcnt_detach(&adapter->watchdog_events);
3607 evcnt_detach(&adapter->tso_err);
3608 evcnt_detach(&adapter->link_irq);
3609 evcnt_detach(&adapter->link_sicount);
3610 evcnt_detach(&adapter->mod_sicount);
3611 evcnt_detach(&adapter->msf_sicount);
3612 evcnt_detach(&adapter->phy_sicount);
3613
3614 for (i = 0; i < IXGBE_TC_COUNTER_NUM; i++) {
3615 if (i < __arraycount(stats->mpc)) {
3616 evcnt_detach(&stats->mpc[i]);
3617 if (hw->mac.type == ixgbe_mac_82598EB)
3618 evcnt_detach(&stats->rnbc[i]);
3619 }
3620 if (i < __arraycount(stats->pxontxc)) {
3621 evcnt_detach(&stats->pxontxc[i]);
3622 evcnt_detach(&stats->pxonrxc[i]);
3623 evcnt_detach(&stats->pxofftxc[i]);
3624 evcnt_detach(&stats->pxoffrxc[i]);
3625 if (hw->mac.type >= ixgbe_mac_82599EB)
3626 evcnt_detach(&stats->pxon2offc[i]);
3627 }
3628 }
3629
3630 txr = adapter->tx_rings;
3631 for (i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
3632 evcnt_detach(&adapter->queues[i].irqs);
3633 evcnt_detach(&adapter->queues[i].handleq);
3634 evcnt_detach(&adapter->queues[i].req);
3635 evcnt_detach(&txr->no_desc_avail);
3636 evcnt_detach(&txr->total_packets);
3637 evcnt_detach(&txr->tso_tx);
3638 #ifndef IXGBE_LEGACY_TX
3639 evcnt_detach(&txr->pcq_drops);
3640 #endif
3641
3642 if (i < __arraycount(stats->qprc)) {
3643 evcnt_detach(&stats->qprc[i]);
3644 evcnt_detach(&stats->qptc[i]);
3645 evcnt_detach(&stats->qbrc[i]);
3646 evcnt_detach(&stats->qbtc[i]);
3647 if (hw->mac.type >= ixgbe_mac_82599EB)
3648 evcnt_detach(&stats->qprdc[i]);
3649 }
3650
3651 evcnt_detach(&rxr->rx_packets);
3652 evcnt_detach(&rxr->rx_bytes);
3653 evcnt_detach(&rxr->rx_copies);
3654 evcnt_detach(&rxr->no_jmbuf);
3655 evcnt_detach(&rxr->rx_discarded);
3656 }
3657 evcnt_detach(&stats->ipcs);
3658 evcnt_detach(&stats->l4cs);
3659 evcnt_detach(&stats->ipcs_bad);
3660 evcnt_detach(&stats->l4cs_bad);
3661 evcnt_detach(&stats->intzero);
3662 evcnt_detach(&stats->legint);
3663 evcnt_detach(&stats->crcerrs);
3664 evcnt_detach(&stats->illerrc);
3665 evcnt_detach(&stats->errbc);
3666 evcnt_detach(&stats->mspdc);
3667 if (hw->mac.type >= ixgbe_mac_X550)
3668 evcnt_detach(&stats->mbsdc);
3669 evcnt_detach(&stats->mpctotal);
3670 evcnt_detach(&stats->mlfc);
3671 evcnt_detach(&stats->mrfc);
3672 evcnt_detach(&stats->rlec);
3673 evcnt_detach(&stats->lxontxc);
3674 evcnt_detach(&stats->lxonrxc);
3675 evcnt_detach(&stats->lxofftxc);
3676 evcnt_detach(&stats->lxoffrxc);
3677
3678 /* Packet Reception Stats */
3679 evcnt_detach(&stats->tor);
3680 evcnt_detach(&stats->gorc);
3681 evcnt_detach(&stats->tpr);
3682 evcnt_detach(&stats->gprc);
3683 evcnt_detach(&stats->mprc);
3684 evcnt_detach(&stats->bprc);
3685 evcnt_detach(&stats->prc64);
3686 evcnt_detach(&stats->prc127);
3687 evcnt_detach(&stats->prc255);
3688 evcnt_detach(&stats->prc511);
3689 evcnt_detach(&stats->prc1023);
3690 evcnt_detach(&stats->prc1522);
3691 evcnt_detach(&stats->ruc);
3692 evcnt_detach(&stats->rfc);
3693 evcnt_detach(&stats->roc);
3694 evcnt_detach(&stats->rjc);
3695 evcnt_detach(&stats->mngprc);
3696 evcnt_detach(&stats->mngpdc);
3697 evcnt_detach(&stats->xec);
3698
3699 /* Packet Transmission Stats */
3700 evcnt_detach(&stats->gotc);
3701 evcnt_detach(&stats->tpt);
3702 evcnt_detach(&stats->gptc);
3703 evcnt_detach(&stats->bptc);
3704 evcnt_detach(&stats->mptc);
3705 evcnt_detach(&stats->mngptc);
3706 evcnt_detach(&stats->ptc64);
3707 evcnt_detach(&stats->ptc127);
3708 evcnt_detach(&stats->ptc255);
3709 evcnt_detach(&stats->ptc511);
3710 evcnt_detach(&stats->ptc1023);
3711 evcnt_detach(&stats->ptc1522);
3712
3713 ixgbe_free_queues(adapter);
3714 free(adapter->mta, M_DEVBUF);
3715
3716 IXGBE_CORE_LOCK_DESTROY(adapter);
3717
3718 return (0);
3719 } /* ixgbe_detach */
3720
3721 /************************************************************************
3722 * ixgbe_setup_low_power_mode - LPLU/WoL preparation
3723 *
3724 * Prepare the adapter/port for LPLU and/or WoL
3725 ************************************************************************/
3726 static int
3727 ixgbe_setup_low_power_mode(struct adapter *adapter)
3728 {
3729 struct ixgbe_hw *hw = &adapter->hw;
3730 device_t dev = adapter->dev;
3731 s32 error = 0;
3732
3733 KASSERT(mutex_owned(&adapter->core_mtx));
3734
3735 /* Limit power management flow to X550EM baseT */
3736 if (hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T &&
3737 hw->phy.ops.enter_lplu) {
3738 /* X550EM baseT adapters need a special LPLU flow */
3739 hw->phy.reset_disable = true;
3740 ixgbe_stop(adapter);
3741 error = hw->phy.ops.enter_lplu(hw);
3742 if (error)
3743 device_printf(dev,
3744 "Error entering LPLU: %d\n", error);
3745 hw->phy.reset_disable = false;
3746 } else {
3747 /* Just stop for other adapters */
3748 ixgbe_stop(adapter);
3749 }
3750
3751 if (!hw->wol_enabled) {
3752 ixgbe_set_phy_power(hw, FALSE);
3753 IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
3754 IXGBE_WRITE_REG(hw, IXGBE_WUC, 0);
3755 } else {
3756 /* Turn off support for APM wakeup. (Using ACPI instead) */
3757 IXGBE_WRITE_REG(hw, IXGBE_GRC_BY_MAC(hw),
3758 IXGBE_READ_REG(hw, IXGBE_GRC_BY_MAC(hw)) & ~(u32)2);
3759
3760 /*
3761 * Clear Wake Up Status register to prevent any previous wakeup
3762 * events from waking us up immediately after we suspend.
3763 */
3764 IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
3765
3766 /*
3767 * Program the Wakeup Filter Control register with user filter
3768 * settings
3769 */
3770 IXGBE_WRITE_REG(hw, IXGBE_WUFC, adapter->wufc);
3771
3772 /* Enable wakeups and power management in Wakeup Control */
3773 IXGBE_WRITE_REG(hw, IXGBE_WUC,
3774 IXGBE_WUC_WKEN | IXGBE_WUC_PME_EN);
3775
3776 }
3777
3778 return error;
3779 } /* ixgbe_setup_low_power_mode */
3780
3781 /************************************************************************
3782 * ixgbe_shutdown - Shutdown entry point
3783 ************************************************************************/
3784 #if 0 /* XXX NetBSD ought to register something like this through pmf(9) */
3785 static int
3786 ixgbe_shutdown(device_t dev)
3787 {
3788 struct adapter *adapter = device_private(dev);
3789 int error = 0;
3790
3791 INIT_DEBUGOUT("ixgbe_shutdown: begin");
3792
3793 IXGBE_CORE_LOCK(adapter);
3794 error = ixgbe_setup_low_power_mode(adapter);
3795 IXGBE_CORE_UNLOCK(adapter);
3796
3797 return (error);
3798 } /* ixgbe_shutdown */
3799 #endif
3800
3801 /************************************************************************
3802 * ixgbe_suspend
3803 *
3804 * From D0 to D3
3805 ************************************************************************/
3806 static bool
3807 ixgbe_suspend(device_t dev, const pmf_qual_t *qual)
3808 {
3809 struct adapter *adapter = device_private(dev);
3810 int error = 0;
3811
3812 INIT_DEBUGOUT("ixgbe_suspend: begin");
3813
3814 IXGBE_CORE_LOCK(adapter);
3815
3816 error = ixgbe_setup_low_power_mode(adapter);
3817
3818 IXGBE_CORE_UNLOCK(adapter);
3819
3820 return (error);
3821 } /* ixgbe_suspend */
3822
3823 /************************************************************************
3824 * ixgbe_resume
3825 *
3826 * From D3 to D0
3827 ************************************************************************/
3828 static bool
3829 ixgbe_resume(device_t dev, const pmf_qual_t *qual)
3830 {
3831 struct adapter *adapter = device_private(dev);
3832 struct ifnet *ifp = adapter->ifp;
3833 struct ixgbe_hw *hw = &adapter->hw;
3834 u32 wus;
3835
3836 INIT_DEBUGOUT("ixgbe_resume: begin");
3837
3838 IXGBE_CORE_LOCK(adapter);
3839
3840 /* Read & clear WUS register */
3841 wus = IXGBE_READ_REG(hw, IXGBE_WUS);
3842 if (wus)
3843 device_printf(dev, "Woken up by (WUS): %#010x\n",
3844 IXGBE_READ_REG(hw, IXGBE_WUS));
3845 IXGBE_WRITE_REG(hw, IXGBE_WUS, 0xffffffff);
3846 /* And clear WUFC until next low-power transition */
3847 IXGBE_WRITE_REG(hw, IXGBE_WUFC, 0);
3848
3849 /*
3850 * Required after D3->D0 transition;
3851 * will re-advertise all previous advertised speeds
3852 */
3853 if (ifp->if_flags & IFF_UP)
3854 ixgbe_init_locked(adapter);
3855
3856 IXGBE_CORE_UNLOCK(adapter);
3857
3858 return true;
3859 } /* ixgbe_resume */
3860
3861 /*
3862 * Set the various hardware offload abilities.
3863 *
3864 * This takes the ifnet's if_capenable flags (e.g. set by the user using
3865 * ifconfig) and indicates to the OS via the ifnet's if_hwassist field what
3866 * mbuf offload flags the driver will understand.
3867 */
3868 static void
3869 ixgbe_set_if_hwassist(struct adapter *adapter)
3870 {
3871 /* XXX */
3872 }
3873
3874 /************************************************************************
3875 * ixgbe_init_locked - Init entry point
3876 *
3877 * Used in two ways: It is used by the stack as an init
3878 * entry point in network interface structure. It is also
3879 * used by the driver as a hw/sw initialization routine to
3880 * get to a consistent state.
3881 *
3882 * return 0 on success, positive on failure
3883 ************************************************************************/
3884 static void
3885 ixgbe_init_locked(struct adapter *adapter)
3886 {
3887 struct ifnet *ifp = adapter->ifp;
3888 device_t dev = adapter->dev;
3889 struct ixgbe_hw *hw = &adapter->hw;
3890 struct ix_queue *que;
3891 struct tx_ring *txr;
3892 struct rx_ring *rxr;
3893 u32 txdctl, mhadd;
3894 u32 rxdctl, rxctrl;
3895 u32 ctrl_ext;
3896 bool unsupported_sfp = false;
3897 int i, j, err;
3898
3899 /* XXX check IFF_UP and IFF_RUNNING, power-saving state! */
3900
3901 KASSERT(mutex_owned(&adapter->core_mtx));
3902 INIT_DEBUGOUT("ixgbe_init_locked: begin");
3903
3904 hw->need_unsupported_sfp_recovery = false;
3905 hw->adapter_stopped = FALSE;
3906 ixgbe_stop_adapter(hw);
3907 callout_stop(&adapter->timer);
3908 for (i = 0, que = adapter->queues; i < adapter->num_queues; i++, que++)
3909 que->disabled_count = 0;
3910
3911 /* XXX I moved this here from the SIOCSIFMTU case in ixgbe_ioctl(). */
3912 adapter->max_frame_size =
3913 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
3914
3915 /* Queue indices may change with IOV mode */
3916 ixgbe_align_all_queue_indices(adapter);
3917
3918 /* reprogram the RAR[0] in case user changed it. */
3919 ixgbe_set_rar(hw, 0, hw->mac.addr, adapter->pool, IXGBE_RAH_AV);
3920
3921 /* Get the latest mac address, User can use a LAA */
3922 memcpy(hw->mac.addr, CLLADDR(ifp->if_sadl),
3923 IXGBE_ETH_LENGTH_OF_ADDRESS);
3924 ixgbe_set_rar(hw, 0, hw->mac.addr, adapter->pool, 1);
3925 hw->addr_ctrl.rar_used_count = 1;
3926
3927 /* Set hardware offload abilities from ifnet flags */
3928 ixgbe_set_if_hwassist(adapter);
3929
3930 /* Prepare transmit descriptors and buffers */
3931 if (ixgbe_setup_transmit_structures(adapter)) {
3932 device_printf(dev, "Could not setup transmit structures\n");
3933 ixgbe_stop(adapter);
3934 return;
3935 }
3936
3937 ixgbe_init_hw(hw);
3938
3939 ixgbe_initialize_iov(adapter);
3940
3941 ixgbe_initialize_transmit_units(adapter);
3942
3943 /* Setup Multicast table */
3944 ixgbe_set_rxfilter(adapter);
3945
3946 /* Determine the correct mbuf pool, based on frame size */
3947 if (adapter->max_frame_size <= MCLBYTES)
3948 adapter->rx_mbuf_sz = MCLBYTES;
3949 else
3950 adapter->rx_mbuf_sz = MJUMPAGESIZE;
3951
3952 /* Prepare receive descriptors and buffers */
3953 if (ixgbe_setup_receive_structures(adapter)) {
3954 device_printf(dev, "Could not setup receive structures\n");
3955 ixgbe_stop(adapter);
3956 return;
3957 }
3958
3959 /* Configure RX settings */
3960 ixgbe_initialize_receive_units(adapter);
3961
3962 /* Enable SDP & MSI-X interrupts based on adapter */
3963 ixgbe_config_gpie(adapter);
3964
3965 /* Set MTU size */
3966 if (ifp->if_mtu > ETHERMTU) {
3967 /* aka IXGBE_MAXFRS on 82599 and newer */
3968 mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
3969 mhadd &= ~IXGBE_MHADD_MFS_MASK;
3970 mhadd |= adapter->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
3971 IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
3972 }
3973
3974 /* Now enable all the queues */
3975 for (i = 0; i < adapter->num_queues; i++) {
3976 txr = &adapter->tx_rings[i];
3977 txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(txr->me));
3978 txdctl |= IXGBE_TXDCTL_ENABLE;
3979 /* Set WTHRESH to 8, burst writeback */
3980 txdctl |= (8 << 16);
3981 /*
3982 * When the internal queue falls below PTHRESH (32),
3983 * start prefetching as long as there are at least
3984 * HTHRESH (1) buffers ready. The values are taken
3985 * from the Intel linux driver 3.8.21.
3986 * Prefetching enables tx line rate even with 1 queue.
3987 */
3988 txdctl |= (32 << 0) | (1 << 8);
3989 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(txr->me), txdctl);
3990 }
3991
3992 for (i = 0; i < adapter->num_queues; i++) {
3993 rxr = &adapter->rx_rings[i];
3994 rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me));
3995 if (hw->mac.type == ixgbe_mac_82598EB) {
3996 /*
3997 * PTHRESH = 21
3998 * HTHRESH = 4
3999 * WTHRESH = 8
4000 */
4001 rxdctl &= ~0x3FFFFF;
4002 rxdctl |= 0x080420;
4003 }
4004 rxdctl |= IXGBE_RXDCTL_ENABLE;
4005 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(rxr->me), rxdctl);
4006 for (j = 0; j < 10; j++) {
4007 if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(rxr->me)) &
4008 IXGBE_RXDCTL_ENABLE)
4009 break;
4010 else
4011 msec_delay(1);
4012 }
4013 IXGBE_WRITE_BARRIER(hw);
4014
4015 /*
4016 * In netmap mode, we must preserve the buffers made
4017 * available to userspace before the if_init()
4018 * (this is true by default on the TX side, because
4019 * init makes all buffers available to userspace).
4020 *
4021 * netmap_reset() and the device specific routines
4022 * (e.g. ixgbe_setup_receive_rings()) map these
4023 * buffers at the end of the NIC ring, so here we
4024 * must set the RDT (tail) register to make sure
4025 * they are not overwritten.
4026 *
4027 * In this driver the NIC ring starts at RDH = 0,
4028 * RDT points to the last slot available for reception (?),
4029 * so RDT = num_rx_desc - 1 means the whole ring is available.
4030 */
4031 #ifdef DEV_NETMAP
4032 if ((adapter->feat_en & IXGBE_FEATURE_NETMAP) &&
4033 (ifp->if_capenable & IFCAP_NETMAP)) {
4034 struct netmap_adapter *na = NA(adapter->ifp);
4035 struct netmap_kring *kring = na->rx_rings[i];
4036 int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
4037
4038 IXGBE_WRITE_REG(hw, IXGBE_RDT(rxr->me), t);
4039 } else
4040 #endif /* DEV_NETMAP */
4041 IXGBE_WRITE_REG(hw, IXGBE_RDT(rxr->me),
4042 adapter->num_rx_desc - 1);
4043 }
4044
4045 /* Enable Receive engine */
4046 rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
4047 if (hw->mac.type == ixgbe_mac_82598EB)
4048 rxctrl |= IXGBE_RXCTRL_DMBYPS;
4049 rxctrl |= IXGBE_RXCTRL_RXEN;
4050 ixgbe_enable_rx_dma(hw, rxctrl);
4051
4052 callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
4053
4054 /* Set up MSI/MSI-X routing */
4055 if (adapter->feat_en & IXGBE_FEATURE_MSIX) {
4056 ixgbe_configure_ivars(adapter);
4057 /* Set up auto-mask */
4058 if (hw->mac.type == ixgbe_mac_82598EB)
4059 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
4060 else {
4061 IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
4062 IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
4063 }
4064 } else { /* Simple settings for Legacy/MSI */
4065 ixgbe_set_ivar(adapter, 0, 0, 0);
4066 ixgbe_set_ivar(adapter, 0, 0, 1);
4067 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
4068 }
4069
4070 ixgbe_init_fdir(adapter);
4071
4072 /*
4073 * Check on any SFP devices that
4074 * need to be kick-started
4075 */
4076 if (hw->phy.type == ixgbe_phy_none) {
4077 err = hw->phy.ops.identify(hw);
4078 if (err == IXGBE_ERR_SFP_NOT_SUPPORTED)
4079 unsupported_sfp = true;
4080 } else if (hw->phy.type == ixgbe_phy_sfp_unsupported)
4081 unsupported_sfp = true;
4082
4083 if (unsupported_sfp)
4084 device_printf(dev,
4085 "Unsupported SFP+ module type was detected.\n");
4086
4087 /* Set moderation on the Link interrupt */
4088 ixgbe_eitr_write(adapter, adapter->vector, IXGBE_LINK_ITR);
4089
4090 /* Enable EEE power saving */
4091 if (adapter->feat_cap & IXGBE_FEATURE_EEE)
4092 hw->mac.ops.setup_eee(hw,
4093 adapter->feat_en & IXGBE_FEATURE_EEE);
4094
4095 /* Enable power to the phy. */
4096 if (!unsupported_sfp) {
4097 ixgbe_set_phy_power(hw, TRUE);
4098
4099 /* Config/Enable Link */
4100 ixgbe_config_link(adapter);
4101 }
4102
4103 /* Hardware Packet Buffer & Flow Control setup */
4104 ixgbe_config_delay_values(adapter);
4105
4106 /* Initialize the FC settings */
4107 ixgbe_start_hw(hw);
4108
4109 /* Set up VLAN support and filter */
4110 ixgbe_setup_vlan_hw_support(adapter);
4111
4112 /* Setup DMA Coalescing */
4113 ixgbe_config_dmac(adapter);
4114
4115 /* And now turn on interrupts */
4116 ixgbe_enable_intr(adapter);
4117
4118 /* Enable the use of the MBX by the VF's */
4119 if (adapter->feat_en & IXGBE_FEATURE_SRIOV) {
4120 ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
4121 ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
4122 IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
4123 }
4124
4125 /* Update saved flags. See ixgbe_ifflags_cb() */
4126 adapter->if_flags = ifp->if_flags;
4127 adapter->ec_capenable = adapter->osdep.ec.ec_capenable;
4128
4129 /* Now inform the stack we're ready */
4130 ifp->if_flags |= IFF_RUNNING;
4131
4132 return;
4133 } /* ixgbe_init_locked */
4134
4135 /************************************************************************
4136 * ixgbe_init
4137 ************************************************************************/
4138 static int
4139 ixgbe_init(struct ifnet *ifp)
4140 {
4141 struct adapter *adapter = ifp->if_softc;
4142
4143 IXGBE_CORE_LOCK(adapter);
4144 ixgbe_init_locked(adapter);
4145 IXGBE_CORE_UNLOCK(adapter);
4146
4147 return 0; /* XXX ixgbe_init_locked cannot fail? really? */
4148 } /* ixgbe_init */
4149
4150 /************************************************************************
4151 * ixgbe_set_ivar
4152 *
4153 * Setup the correct IVAR register for a particular MSI-X interrupt
4154 * (yes this is all very magic and confusing :)
4155 * - entry is the register array entry
4156 * - vector is the MSI-X vector for this queue
4157 * - type is RX/TX/MISC
4158 ************************************************************************/
4159 static void
4160 ixgbe_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
4161 {
4162 struct ixgbe_hw *hw = &adapter->hw;
4163 u32 ivar, index;
4164
4165 vector |= IXGBE_IVAR_ALLOC_VAL;
4166
4167 switch (hw->mac.type) {
4168 case ixgbe_mac_82598EB:
4169 if (type == -1)
4170 entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
4171 else
4172 entry += (type * 64);
4173 index = (entry >> 2) & 0x1F;
4174 ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
4175 ivar &= ~(0xffUL << (8 * (entry & 0x3)));
4176 ivar |= ((u32)vector << (8 * (entry & 0x3)));
4177 IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
4178 break;
4179 case ixgbe_mac_82599EB:
4180 case ixgbe_mac_X540:
4181 case ixgbe_mac_X550:
4182 case ixgbe_mac_X550EM_x:
4183 case ixgbe_mac_X550EM_a:
4184 if (type == -1) { /* MISC IVAR */
4185 index = (entry & 1) * 8;
4186 ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
4187 ivar &= ~(0xffUL << index);
4188 ivar |= ((u32)vector << index);
4189 IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
4190 } else { /* RX/TX IVARS */
4191 index = (16 * (entry & 1)) + (8 * type);
4192 ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
4193 ivar &= ~(0xffUL << index);
4194 ivar |= ((u32)vector << index);
4195 IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
4196 }
4197 break;
4198 default:
4199 break;
4200 }
4201 } /* ixgbe_set_ivar */
4202
4203 /************************************************************************
4204 * ixgbe_configure_ivars
4205 ************************************************************************/
4206 static void
4207 ixgbe_configure_ivars(struct adapter *adapter)
4208 {
4209 struct ix_queue *que = adapter->queues;
4210 u32 newitr;
4211
4212 if (ixgbe_max_interrupt_rate > 0)
4213 newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
4214 else {
4215 /*
4216 * Disable DMA coalescing if interrupt moderation is
4217 * disabled.
4218 */
4219 adapter->dmac = 0;
4220 newitr = 0;
4221 }
4222
4223 for (int i = 0; i < adapter->num_queues; i++, que++) {
4224 struct rx_ring *rxr = &adapter->rx_rings[i];
4225 struct tx_ring *txr = &adapter->tx_rings[i];
4226 /* First the RX queue entry */
4227 ixgbe_set_ivar(adapter, rxr->me, que->msix, 0);
4228 /* ... and the TX */
4229 ixgbe_set_ivar(adapter, txr->me, que->msix, 1);
4230 /* Set an Initial EITR value */
4231 ixgbe_eitr_write(adapter, que->msix, newitr);
4232 /*
4233 * To eliminate influence of the previous state.
4234 * At this point, Tx/Rx interrupt handler
4235 * (ixgbe_msix_que()) cannot be called, so both
4236 * IXGBE_TX_LOCK and IXGBE_RX_LOCK are not required.
4237 */
4238 que->eitr_setting = 0;
4239 }
4240
4241 /* For the Link interrupt */
4242 ixgbe_set_ivar(adapter, 1, adapter->vector, -1);
4243 } /* ixgbe_configure_ivars */
4244
4245 /************************************************************************
4246 * ixgbe_config_gpie
4247 ************************************************************************/
4248 static void
4249 ixgbe_config_gpie(struct adapter *adapter)
4250 {
4251 struct ixgbe_hw *hw = &adapter->hw;
4252 u32 gpie;
4253
4254 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
4255
4256 if (adapter->feat_en & IXGBE_FEATURE_MSIX) {
4257 /* Enable Enhanced MSI-X mode */
4258 gpie |= IXGBE_GPIE_MSIX_MODE
4259 | IXGBE_GPIE_EIAME
4260 | IXGBE_GPIE_PBA_SUPPORT
4261 | IXGBE_GPIE_OCD;
4262 }
4263
4264 /* Fan Failure Interrupt */
4265 if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL)
4266 gpie |= IXGBE_SDP1_GPIEN;
4267
4268 /* Thermal Sensor Interrupt */
4269 if (adapter->feat_en & IXGBE_FEATURE_TEMP_SENSOR)
4270 gpie |= IXGBE_SDP0_GPIEN_X540;
4271
4272 /* Link detection */
4273 switch (hw->mac.type) {
4274 case ixgbe_mac_82599EB:
4275 gpie |= IXGBE_SDP1_GPIEN | IXGBE_SDP2_GPIEN;
4276 break;
4277 case ixgbe_mac_X550EM_x:
4278 case ixgbe_mac_X550EM_a:
4279 gpie |= IXGBE_SDP0_GPIEN_X540;
4280 break;
4281 default:
4282 break;
4283 }
4284
4285 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
4286
4287 } /* ixgbe_config_gpie */
4288
4289 /************************************************************************
4290 * ixgbe_config_delay_values
4291 *
4292 * Requires adapter->max_frame_size to be set.
4293 ************************************************************************/
4294 static void
4295 ixgbe_config_delay_values(struct adapter *adapter)
4296 {
4297 struct ixgbe_hw *hw = &adapter->hw;
4298 u32 rxpb, frame, size, tmp;
4299
4300 frame = adapter->max_frame_size;
4301
4302 /* Calculate High Water */
4303 switch (hw->mac.type) {
4304 case ixgbe_mac_X540:
4305 case ixgbe_mac_X550:
4306 case ixgbe_mac_X550EM_x:
4307 case ixgbe_mac_X550EM_a:
4308 tmp = IXGBE_DV_X540(frame, frame);
4309 break;
4310 default:
4311 tmp = IXGBE_DV(frame, frame);
4312 break;
4313 }
4314 size = IXGBE_BT2KB(tmp);
4315 rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
4316 hw->fc.high_water[0] = rxpb - size;
4317
4318 /* Now calculate Low Water */
4319 switch (hw->mac.type) {
4320 case ixgbe_mac_X540:
4321 case ixgbe_mac_X550:
4322 case ixgbe_mac_X550EM_x:
4323 case ixgbe_mac_X550EM_a:
4324 tmp = IXGBE_LOW_DV_X540(frame);
4325 break;
4326 default:
4327 tmp = IXGBE_LOW_DV(frame);
4328 break;
4329 }
4330 hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
4331
4332 hw->fc.pause_time = IXGBE_FC_PAUSE;
4333 hw->fc.send_xon = TRUE;
4334 } /* ixgbe_config_delay_values */
4335
4336 /************************************************************************
4337 * ixgbe_set_rxfilter - Multicast Update
4338 *
4339 * Called whenever multicast address list is updated.
4340 ************************************************************************/
4341 static void
4342 ixgbe_set_rxfilter(struct adapter *adapter)
4343 {
4344 struct ixgbe_mc_addr *mta;
4345 struct ifnet *ifp = adapter->ifp;
4346 u8 *update_ptr;
4347 int mcnt = 0;
4348 u32 fctrl;
4349 struct ethercom *ec = &adapter->osdep.ec;
4350 struct ether_multi *enm;
4351 struct ether_multistep step;
4352
4353 KASSERT(mutex_owned(&adapter->core_mtx));
4354 IOCTL_DEBUGOUT("ixgbe_set_rxfilter: begin");
4355
4356 mta = adapter->mta;
4357 bzero(mta, sizeof(*mta) * MAX_NUM_MULTICAST_ADDRESSES);
4358
4359 ETHER_LOCK(ec);
4360 ec->ec_flags &= ~ETHER_F_ALLMULTI;
4361 ETHER_FIRST_MULTI(step, ec, enm);
4362 while (enm != NULL) {
4363 if ((mcnt == MAX_NUM_MULTICAST_ADDRESSES) ||
4364 (memcmp(enm->enm_addrlo, enm->enm_addrhi,
4365 ETHER_ADDR_LEN) != 0)) {
4366 ec->ec_flags |= ETHER_F_ALLMULTI;
4367 break;
4368 }
4369 bcopy(enm->enm_addrlo,
4370 mta[mcnt].addr, IXGBE_ETH_LENGTH_OF_ADDRESS);
4371 mta[mcnt].vmdq = adapter->pool;
4372 mcnt++;
4373 ETHER_NEXT_MULTI(step, enm);
4374 }
4375
4376 fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
4377 if (ifp->if_flags & IFF_PROMISC)
4378 fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
4379 else if (ec->ec_flags & ETHER_F_ALLMULTI) {
4380 fctrl |= IXGBE_FCTRL_MPE;
4381 fctrl &= ~IXGBE_FCTRL_UPE;
4382 } else
4383 fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
4384
4385 IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
4386
4387 /* Update multicast filter entries only when it's not ALLMULTI */
4388 if ((ec->ec_flags & ETHER_F_ALLMULTI) == 0) {
4389 ETHER_UNLOCK(ec);
4390 update_ptr = (u8 *)mta;
4391 ixgbe_update_mc_addr_list(&adapter->hw, update_ptr, mcnt,
4392 ixgbe_mc_array_itr, TRUE);
4393 } else
4394 ETHER_UNLOCK(ec);
4395 } /* ixgbe_set_rxfilter */
4396
4397 /************************************************************************
4398 * ixgbe_mc_array_itr
4399 *
4400 * An iterator function needed by the multicast shared code.
4401 * It feeds the shared code routine the addresses in the
4402 * array of ixgbe_set_rxfilter() one by one.
4403 ************************************************************************/
4404 static u8 *
4405 ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
4406 {
4407 struct ixgbe_mc_addr *mta;
4408
4409 mta = (struct ixgbe_mc_addr *)*update_ptr;
4410 *vmdq = mta->vmdq;
4411
4412 *update_ptr = (u8*)(mta + 1);
4413
4414 return (mta->addr);
4415 } /* ixgbe_mc_array_itr */
4416
4417 /************************************************************************
4418 * ixgbe_local_timer - Timer routine
4419 *
4420 * Checks for link status, updates statistics,
4421 * and runs the watchdog check.
4422 ************************************************************************/
4423 static void
4424 ixgbe_local_timer(void *arg)
4425 {
4426 struct adapter *adapter = arg;
4427
4428 IXGBE_CORE_LOCK(adapter);
4429 ixgbe_local_timer1(adapter);
4430 IXGBE_CORE_UNLOCK(adapter);
4431 }
4432
4433 static void
4434 ixgbe_local_timer1(void *arg)
4435 {
4436 struct adapter *adapter = arg;
4437 device_t dev = adapter->dev;
4438 struct ix_queue *que = adapter->queues;
4439 u64 queues = 0;
4440 u64 v0, v1, v2, v3, v4, v5, v6, v7;
4441 int hung = 0;
4442 int i;
4443
4444 KASSERT(mutex_owned(&adapter->core_mtx));
4445
4446 /* Check for pluggable optics */
4447 if (adapter->sfp_probe)
4448 if (!ixgbe_sfp_probe(adapter))
4449 goto out; /* Nothing to do */
4450
4451 ixgbe_update_link_status(adapter);
4452 ixgbe_update_stats_counters(adapter);
4453
4454 /* Update some event counters */
4455 v0 = v1 = v2 = v3 = v4 = v5 = v6 = v7 = 0;
4456 que = adapter->queues;
4457 for (i = 0; i < adapter->num_queues; i++, que++) {
4458 struct tx_ring *txr = que->txr;
4459
4460 v0 += txr->q_efbig_tx_dma_setup;
4461 v1 += txr->q_mbuf_defrag_failed;
4462 v2 += txr->q_efbig2_tx_dma_setup;
4463 v3 += txr->q_einval_tx_dma_setup;
4464 v4 += txr->q_other_tx_dma_setup;
4465 v5 += txr->q_eagain_tx_dma_setup;
4466 v6 += txr->q_enomem_tx_dma_setup;
4467 v7 += txr->q_tso_err;
4468 }
4469 adapter->efbig_tx_dma_setup.ev_count = v0;
4470 adapter->mbuf_defrag_failed.ev_count = v1;
4471 adapter->efbig2_tx_dma_setup.ev_count = v2;
4472 adapter->einval_tx_dma_setup.ev_count = v3;
4473 adapter->other_tx_dma_setup.ev_count = v4;
4474 adapter->eagain_tx_dma_setup.ev_count = v5;
4475 adapter->enomem_tx_dma_setup.ev_count = v6;
4476 adapter->tso_err.ev_count = v7;
4477
4478 /*
4479 * Check the TX queues status
4480 * - mark hung queues so we don't schedule on them
4481 * - watchdog only if all queues show hung
4482 */
4483 que = adapter->queues;
4484 for (i = 0; i < adapter->num_queues; i++, que++) {
4485 /* Keep track of queues with work for soft irq */
4486 if (que->txr->busy)
4487 queues |= 1ULL << que->me;
4488 /*
4489 * Each time txeof runs without cleaning, but there
4490 * are uncleaned descriptors it increments busy. If
4491 * we get to the MAX we declare it hung.
4492 */
4493 if (que->busy == IXGBE_QUEUE_HUNG) {
4494 ++hung;
4495 /* Mark the queue as inactive */
4496 adapter->active_queues &= ~(1ULL << que->me);
4497 continue;
4498 } else {
4499 /* Check if we've come back from hung */
4500 if ((adapter->active_queues & (1ULL << que->me)) == 0)
4501 adapter->active_queues |= 1ULL << que->me;
4502 }
4503 if (que->busy >= IXGBE_MAX_TX_BUSY) {
4504 device_printf(dev,
4505 "Warning queue %d appears to be hung!\n", i);
4506 que->txr->busy = IXGBE_QUEUE_HUNG;
4507 ++hung;
4508 }
4509 }
4510
4511 /* Only truely watchdog if all queues show hung */
4512 if (hung == adapter->num_queues)
4513 goto watchdog;
4514 #if 0 /* XXX Avoid unexpectedly disabling interrupt forever (PR#53294) */
4515 else if (queues != 0) { /* Force an IRQ on queues with work */
4516 que = adapter->queues;
4517 for (i = 0; i < adapter->num_queues; i++, que++) {
4518 mutex_enter(&que->dc_mtx);
4519 if (que->disabled_count == 0)
4520 ixgbe_rearm_queues(adapter,
4521 queues & ((u64)1 << i));
4522 mutex_exit(&que->dc_mtx);
4523 }
4524 }
4525 #endif
4526
4527 out:
4528 callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
4529 return;
4530
4531 watchdog:
4532 device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
4533 adapter->ifp->if_flags &= ~IFF_RUNNING;
4534 adapter->watchdog_events.ev_count++;
4535 ixgbe_init_locked(adapter);
4536 } /* ixgbe_local_timer */
4537
4538 /************************************************************************
4539 * ixgbe_recovery_mode_timer - Recovery mode timer routine
4540 ************************************************************************/
4541 static void
4542 ixgbe_recovery_mode_timer(void *arg)
4543 {
4544 struct adapter *adapter = arg;
4545 struct ixgbe_hw *hw = &adapter->hw;
4546
4547 IXGBE_CORE_LOCK(adapter);
4548 if (ixgbe_fw_recovery_mode(hw)) {
4549 if (atomic_cas_uint(&adapter->recovery_mode, 0, 1)) {
4550 /* Firmware error detected, entering recovery mode */
4551 device_printf(adapter->dev, "Firmware recovery mode detected. Limiting functionality. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
4552
4553 if (hw->adapter_stopped == FALSE)
4554 ixgbe_stop(adapter);
4555 }
4556 } else
4557 atomic_cas_uint(&adapter->recovery_mode, 1, 0);
4558
4559 callout_reset(&adapter->recovery_mode_timer, hz,
4560 ixgbe_recovery_mode_timer, adapter);
4561 IXGBE_CORE_UNLOCK(adapter);
4562 } /* ixgbe_recovery_mode_timer */
4563
4564 /************************************************************************
4565 * ixgbe_sfp_probe
4566 *
4567 * Determine if a port had optics inserted.
4568 ************************************************************************/
4569 static bool
4570 ixgbe_sfp_probe(struct adapter *adapter)
4571 {
4572 struct ixgbe_hw *hw = &adapter->hw;
4573 device_t dev = adapter->dev;
4574 bool result = FALSE;
4575
4576 if ((hw->phy.type == ixgbe_phy_nl) &&
4577 (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
4578 s32 ret = hw->phy.ops.identify_sfp(hw);
4579 if (ret)
4580 goto out;
4581 ret = hw->phy.ops.reset(hw);
4582 adapter->sfp_probe = FALSE;
4583 if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
4584 device_printf(dev,"Unsupported SFP+ module detected!");
4585 device_printf(dev,
4586 "Reload driver with supported module.\n");
4587 goto out;
4588 } else
4589 device_printf(dev, "SFP+ module detected!\n");
4590 /* We now have supported optics */
4591 result = TRUE;
4592 }
4593 out:
4594
4595 return (result);
4596 } /* ixgbe_sfp_probe */
4597
4598 /************************************************************************
4599 * ixgbe_handle_mod - Tasklet for SFP module interrupts
4600 ************************************************************************/
4601 static void
4602 ixgbe_handle_mod(void *context)
4603 {
4604 struct adapter *adapter = context;
4605 struct ixgbe_hw *hw = &adapter->hw;
4606 device_t dev = adapter->dev;
4607 u32 err, cage_full = 0;
4608
4609 IXGBE_CORE_LOCK(adapter);
4610 ++adapter->mod_sicount.ev_count;
4611 if (adapter->hw.need_crosstalk_fix) {
4612 switch (hw->mac.type) {
4613 case ixgbe_mac_82599EB:
4614 cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
4615 IXGBE_ESDP_SDP2;
4616 break;
4617 case ixgbe_mac_X550EM_x:
4618 case ixgbe_mac_X550EM_a:
4619 cage_full = IXGBE_READ_REG(hw, IXGBE_ESDP) &
4620 IXGBE_ESDP_SDP0;
4621 break;
4622 default:
4623 break;
4624 }
4625
4626 if (!cage_full)
4627 goto out;
4628 }
4629
4630 err = hw->phy.ops.identify_sfp(hw);
4631 if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
4632 device_printf(dev,
4633 "Unsupported SFP+ module type was detected.\n");
4634 goto out;
4635 }
4636
4637 if (hw->need_unsupported_sfp_recovery) {
4638 device_printf(dev, "Recovering from unsupported SFP\n");
4639 /*
4640 * We could recover the status by calling setup_sfp(),
4641 * setup_link() and some others. It's complex and might not
4642 * work correctly on some unknown cases. To avoid such type of
4643 * problem, call ixgbe_init_locked(). It's simple and safe
4644 * approach.
4645 */
4646 ixgbe_init_locked(adapter);
4647 } else {
4648 if (hw->mac.type == ixgbe_mac_82598EB)
4649 err = hw->phy.ops.reset(hw);
4650 else {
4651 err = hw->mac.ops.setup_sfp(hw);
4652 hw->phy.sfp_setup_needed = FALSE;
4653 }
4654 if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
4655 device_printf(dev,
4656 "Setup failure - unsupported SFP+ module type.\n");
4657 goto out;
4658 }
4659 }
4660 softint_schedule(adapter->msf_si);
4661 out:
4662 IXGBE_CORE_UNLOCK(adapter);
4663 } /* ixgbe_handle_mod */
4664
4665
4666 /************************************************************************
4667 * ixgbe_handle_msf - Tasklet for MSF (multispeed fiber) interrupts
4668 ************************************************************************/
4669 static void
4670 ixgbe_handle_msf(void *context)
4671 {
4672 struct adapter *adapter = context;
4673 struct ixgbe_hw *hw = &adapter->hw;
4674 u32 autoneg;
4675 bool negotiate;
4676
4677 IXGBE_CORE_LOCK(adapter);
4678 ++adapter->msf_sicount.ev_count;
4679 /* get_supported_phy_layer will call hw->phy.ops.identify_sfp() */
4680 adapter->phy_layer = ixgbe_get_supported_physical_layer(hw);
4681
4682 autoneg = hw->phy.autoneg_advertised;
4683 if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
4684 hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
4685 else
4686 negotiate = 0;
4687 if (hw->mac.ops.setup_link)
4688 hw->mac.ops.setup_link(hw, autoneg, TRUE);
4689
4690 /* Adjust media types shown in ifconfig */
4691 ifmedia_removeall(&adapter->media);
4692 ixgbe_add_media_types(adapter);
4693 ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
4694 IXGBE_CORE_UNLOCK(adapter);
4695 } /* ixgbe_handle_msf */
4696
4697 /************************************************************************
4698 * ixgbe_handle_phy - Tasklet for external PHY interrupts
4699 ************************************************************************/
4700 static void
4701 ixgbe_handle_phy(void *context)
4702 {
4703 struct adapter *adapter = context;
4704 struct ixgbe_hw *hw = &adapter->hw;
4705 int error;
4706
4707 ++adapter->phy_sicount.ev_count;
4708 error = hw->phy.ops.handle_lasi(hw);
4709 if (error == IXGBE_ERR_OVERTEMP)
4710 device_printf(adapter->dev,
4711 "CRITICAL: EXTERNAL PHY OVER TEMP!! "
4712 " PHY will downshift to lower power state!\n");
4713 else if (error)
4714 device_printf(adapter->dev,
4715 "Error handling LASI interrupt: %d\n", error);
4716 } /* ixgbe_handle_phy */
4717
4718 static void
4719 ixgbe_ifstop(struct ifnet *ifp, int disable)
4720 {
4721 struct adapter *adapter = ifp->if_softc;
4722
4723 IXGBE_CORE_LOCK(adapter);
4724 ixgbe_stop(adapter);
4725 IXGBE_CORE_UNLOCK(adapter);
4726 }
4727
4728 /************************************************************************
4729 * ixgbe_stop - Stop the hardware
4730 *
4731 * Disables all traffic on the adapter by issuing a
4732 * global reset on the MAC and deallocates TX/RX buffers.
4733 ************************************************************************/
4734 static void
4735 ixgbe_stop(void *arg)
4736 {
4737 struct ifnet *ifp;
4738 struct adapter *adapter = arg;
4739 struct ixgbe_hw *hw = &adapter->hw;
4740
4741 ifp = adapter->ifp;
4742
4743 KASSERT(mutex_owned(&adapter->core_mtx));
4744
4745 INIT_DEBUGOUT("ixgbe_stop: begin\n");
4746 ixgbe_disable_intr(adapter);
4747 callout_stop(&adapter->timer);
4748
4749 /* Let the stack know...*/
4750 ifp->if_flags &= ~IFF_RUNNING;
4751
4752 ixgbe_reset_hw(hw);
4753 hw->adapter_stopped = FALSE;
4754 ixgbe_stop_adapter(hw);
4755 if (hw->mac.type == ixgbe_mac_82599EB)
4756 ixgbe_stop_mac_link_on_d3_82599(hw);
4757 /* Turn off the laser - noop with no optics */
4758 ixgbe_disable_tx_laser(hw);
4759
4760 /* Update the stack */
4761 adapter->link_up = FALSE;
4762 ixgbe_update_link_status(adapter);
4763
4764 /* reprogram the RAR[0] in case user changed it. */
4765 ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
4766
4767 return;
4768 } /* ixgbe_stop */
4769
4770 /************************************************************************
4771 * ixgbe_update_link_status - Update OS on link state
4772 *
4773 * Note: Only updates the OS on the cached link state.
4774 * The real check of the hardware only happens with
4775 * a link interrupt.
4776 ************************************************************************/
4777 static void
4778 ixgbe_update_link_status(struct adapter *adapter)
4779 {
4780 struct ifnet *ifp = adapter->ifp;
4781 device_t dev = adapter->dev;
4782 struct ixgbe_hw *hw = &adapter->hw;
4783
4784 KASSERT(mutex_owned(&adapter->core_mtx));
4785
4786 if (adapter->link_up) {
4787 if (adapter->link_active != LINK_STATE_UP) {
4788 /*
4789 * To eliminate influence of the previous state
4790 * in the same way as ixgbe_init_locked().
4791 */
4792 struct ix_queue *que = adapter->queues;
4793 for (int i = 0; i < adapter->num_queues; i++, que++)
4794 que->eitr_setting = 0;
4795
4796 if (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL){
4797 /*
4798 * Discard count for both MAC Local Fault and
4799 * Remote Fault because those registers are
4800 * valid only when the link speed is up and
4801 * 10Gbps.
4802 */
4803 IXGBE_READ_REG(hw, IXGBE_MLFC);
4804 IXGBE_READ_REG(hw, IXGBE_MRFC);
4805 }
4806
4807 if (bootverbose) {
4808 const char *bpsmsg;
4809
4810 switch (adapter->link_speed) {
4811 case IXGBE_LINK_SPEED_10GB_FULL:
4812 bpsmsg = "10 Gbps";
4813 break;
4814 case IXGBE_LINK_SPEED_5GB_FULL:
4815 bpsmsg = "5 Gbps";
4816 break;
4817 case IXGBE_LINK_SPEED_2_5GB_FULL:
4818 bpsmsg = "2.5 Gbps";
4819 break;
4820 case IXGBE_LINK_SPEED_1GB_FULL:
4821 bpsmsg = "1 Gbps";
4822 break;
4823 case IXGBE_LINK_SPEED_100_FULL:
4824 bpsmsg = "100 Mbps";
4825 break;
4826 case IXGBE_LINK_SPEED_10_FULL:
4827 bpsmsg = "10 Mbps";
4828 break;
4829 default:
4830 bpsmsg = "unknown speed";
4831 break;
4832 }
4833 device_printf(dev, "Link is up %s %s \n",
4834 bpsmsg, "Full Duplex");
4835 }
4836 adapter->link_active = LINK_STATE_UP;
4837 /* Update any Flow Control changes */
4838 ixgbe_fc_enable(&adapter->hw);
4839 /* Update DMA coalescing config */
4840 ixgbe_config_dmac(adapter);
4841 if_link_state_change(ifp, LINK_STATE_UP);
4842
4843 if (adapter->feat_en & IXGBE_FEATURE_SRIOV)
4844 ixgbe_ping_all_vfs(adapter);
4845 }
4846 } else {
4847 /*
4848 * Do it when link active changes to DOWN. i.e.
4849 * a) LINK_STATE_UNKNOWN -> LINK_STATE_DOWN
4850 * b) LINK_STATE_UP -> LINK_STATE_DOWN
4851 */
4852 if (adapter->link_active != LINK_STATE_DOWN) {
4853 if (bootverbose)
4854 device_printf(dev, "Link is Down\n");
4855 if_link_state_change(ifp, LINK_STATE_DOWN);
4856 adapter->link_active = LINK_STATE_DOWN;
4857 if (adapter->feat_en & IXGBE_FEATURE_SRIOV)
4858 ixgbe_ping_all_vfs(adapter);
4859 ixgbe_drain_all(adapter);
4860 }
4861 }
4862 } /* ixgbe_update_link_status */
4863
4864 /************************************************************************
4865 * ixgbe_config_dmac - Configure DMA Coalescing
4866 ************************************************************************/
4867 static void
4868 ixgbe_config_dmac(struct adapter *adapter)
4869 {
4870 struct ixgbe_hw *hw = &adapter->hw;
4871 struct ixgbe_dmac_config *dcfg = &hw->mac.dmac_config;
4872
4873 if (hw->mac.type < ixgbe_mac_X550 || !hw->mac.ops.dmac_config)
4874 return;
4875
4876 if (dcfg->watchdog_timer ^ adapter->dmac ||
4877 dcfg->link_speed ^ adapter->link_speed) {
4878 dcfg->watchdog_timer = adapter->dmac;
4879 dcfg->fcoe_en = false;
4880 dcfg->link_speed = adapter->link_speed;
4881 dcfg->num_tcs = 1;
4882
4883 INIT_DEBUGOUT2("dmac settings: watchdog %d, link speed %d\n",
4884 dcfg->watchdog_timer, dcfg->link_speed);
4885
4886 hw->mac.ops.dmac_config(hw);
4887 }
4888 } /* ixgbe_config_dmac */
4889
4890 /************************************************************************
4891 * ixgbe_enable_intr
4892 ************************************************************************/
4893 static void
4894 ixgbe_enable_intr(struct adapter *adapter)
4895 {
4896 struct ixgbe_hw *hw = &adapter->hw;
4897 struct ix_queue *que = adapter->queues;
4898 u32 mask, fwsm;
4899
4900 mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
4901
4902 switch (adapter->hw.mac.type) {
4903 case ixgbe_mac_82599EB:
4904 mask |= IXGBE_EIMS_ECC;
4905 /* Temperature sensor on some adapters */
4906 mask |= IXGBE_EIMS_GPI_SDP0;
4907 /* SFP+ (RX_LOS_N & MOD_ABS_N) */
4908 mask |= IXGBE_EIMS_GPI_SDP1;
4909 mask |= IXGBE_EIMS_GPI_SDP2;
4910 break;
4911 case ixgbe_mac_X540:
4912 /* Detect if Thermal Sensor is enabled */
4913 fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
4914 if (fwsm & IXGBE_FWSM_TS_ENABLED)
4915 mask |= IXGBE_EIMS_TS;
4916 mask |= IXGBE_EIMS_ECC;
4917 break;
4918 case ixgbe_mac_X550:
4919 /* MAC thermal sensor is automatically enabled */
4920 mask |= IXGBE_EIMS_TS;
4921 mask |= IXGBE_EIMS_ECC;
4922 break;
4923 case ixgbe_mac_X550EM_x:
4924 case ixgbe_mac_X550EM_a:
4925 /* Some devices use SDP0 for important information */
4926 if (hw->device_id == IXGBE_DEV_ID_X550EM_X_SFP ||
4927 hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP ||
4928 hw->device_id == IXGBE_DEV_ID_X550EM_A_SFP_N ||
4929 hw->device_id == IXGBE_DEV_ID_X550EM_X_10G_T)
4930 mask |= IXGBE_EIMS_GPI_SDP0_BY_MAC(hw);
4931 if (hw->phy.type == ixgbe_phy_x550em_ext_t)
4932 mask |= IXGBE_EICR_GPI_SDP0_X540;
4933 mask |= IXGBE_EIMS_ECC;
4934 break;
4935 default:
4936 break;
4937 }
4938
4939 /* Enable Fan Failure detection */
4940 if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL)
4941 mask |= IXGBE_EIMS_GPI_SDP1;
4942 /* Enable SR-IOV */
4943 if (adapter->feat_en & IXGBE_FEATURE_SRIOV)
4944 mask |= IXGBE_EIMS_MAILBOX;
4945 /* Enable Flow Director */
4946 if (adapter->feat_en & IXGBE_FEATURE_FDIR)
4947 mask |= IXGBE_EIMS_FLOW_DIR;
4948
4949 IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
4950
4951 /* With MSI-X we use auto clear */
4952 if (adapter->msix_mem) {
4953 mask = IXGBE_EIMS_ENABLE_MASK;
4954 /* Don't autoclear Link */
4955 mask &= ~IXGBE_EIMS_OTHER;
4956 mask &= ~IXGBE_EIMS_LSC;
4957 if (adapter->feat_cap & IXGBE_FEATURE_SRIOV)
4958 mask &= ~IXGBE_EIMS_MAILBOX;
4959 IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
4960 }
4961
4962 /*
4963 * Now enable all queues, this is done separately to
4964 * allow for handling the extended (beyond 32) MSI-X
4965 * vectors that can be used by 82599
4966 */
4967 for (int i = 0; i < adapter->num_queues; i++, que++)
4968 ixgbe_enable_queue(adapter, que->msix);
4969
4970 IXGBE_WRITE_FLUSH(hw);
4971
4972 } /* ixgbe_enable_intr */
4973
4974 /************************************************************************
4975 * ixgbe_disable_intr_internal
4976 ************************************************************************/
4977 static void
4978 ixgbe_disable_intr_internal(struct adapter *adapter, bool nestok)
4979 {
4980 struct ix_queue *que = adapter->queues;
4981
4982 /* disable interrupts other than queues */
4983 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~IXGBE_EIMC_RTX_QUEUE);
4984
4985 if (adapter->msix_mem)
4986 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, 0);
4987
4988 for (int i = 0; i < adapter->num_queues; i++, que++)
4989 ixgbe_disable_queue_internal(adapter, que->msix, nestok);
4990
4991 IXGBE_WRITE_FLUSH(&adapter->hw);
4992
4993 } /* ixgbe_do_disable_intr_internal */
4994
4995 /************************************************************************
4996 * ixgbe_disable_intr
4997 ************************************************************************/
4998 static void
4999 ixgbe_disable_intr(struct adapter *adapter)
5000 {
5001
5002 ixgbe_disable_intr_internal(adapter, true);
5003 } /* ixgbe_disable_intr */
5004
5005 /************************************************************************
5006 * ixgbe_ensure_disabled_intr
5007 ************************************************************************/
5008 void
5009 ixgbe_ensure_disabled_intr(struct adapter *adapter)
5010 {
5011
5012 ixgbe_disable_intr_internal(adapter, false);
5013 } /* ixgbe_ensure_disabled_intr */
5014
5015 /************************************************************************
5016 * ixgbe_legacy_irq - Legacy Interrupt Service routine
5017 ************************************************************************/
5018 static int
5019 ixgbe_legacy_irq(void *arg)
5020 {
5021 struct ix_queue *que = arg;
5022 struct adapter *adapter = que->adapter;
5023 struct ixgbe_hw *hw = &adapter->hw;
5024 struct ifnet *ifp = adapter->ifp;
5025 struct tx_ring *txr = adapter->tx_rings;
5026 bool more = false;
5027 u32 eicr, eicr_mask;
5028
5029 /* Silicon errata #26 on 82598 */
5030 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
5031
5032 eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
5033
5034 adapter->stats.pf.legint.ev_count++;
5035 ++que->irqs.ev_count;
5036 if (eicr == 0) {
5037 adapter->stats.pf.intzero.ev_count++;
5038 if ((ifp->if_flags & IFF_UP) != 0)
5039 ixgbe_enable_intr(adapter);
5040 return 0;
5041 }
5042
5043 if ((ifp->if_flags & IFF_RUNNING) != 0) {
5044 /*
5045 * The same as ixgbe_msix_que() about "que->txrx_use_workqueue".
5046 */
5047 que->txrx_use_workqueue = adapter->txrx_use_workqueue;
5048
5049 #ifdef __NetBSD__
5050 /* Don't run ixgbe_rxeof in interrupt context */
5051 more = true;
5052 #else
5053 more = ixgbe_rxeof(que);
5054 #endif
5055
5056 IXGBE_TX_LOCK(txr);
5057 ixgbe_txeof(txr);
5058 #ifdef notyet
5059 if (!ixgbe_ring_empty(ifp, txr->br))
5060 ixgbe_start_locked(ifp, txr);
5061 #endif
5062 IXGBE_TX_UNLOCK(txr);
5063 }
5064
5065 /* Check for fan failure */
5066 if (adapter->feat_en & IXGBE_FEATURE_FAN_FAIL) {
5067 ixgbe_check_fan_failure(adapter, eicr, true);
5068 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
5069 }
5070
5071 /* Link status change */
5072 if (eicr & IXGBE_EICR_LSC)
5073 softint_schedule(adapter->link_si);
5074
5075 if (ixgbe_is_sfp(hw)) {
5076 /* Pluggable optics-related interrupt */
5077 if (hw->mac.type >= ixgbe_mac_X540)
5078 eicr_mask = IXGBE_EICR_GPI_SDP0_X540;
5079 else
5080 eicr_mask = IXGBE_EICR_GPI_SDP2_BY_MAC(hw);
5081
5082 if (eicr & eicr_mask) {
5083 IXGBE_WRITE_REG(hw, IXGBE_EICR, eicr_mask);
5084 softint_schedule(adapter->mod_si);
5085 }
5086
5087 if ((hw->mac.type == ixgbe_mac_82599EB) &&
5088 (eicr & IXGBE_EICR_GPI_SDP1_BY_MAC(hw))) {
5089 IXGBE_WRITE_REG(hw, IXGBE_EICR,
5090 IXGBE_EICR_GPI_SDP1_BY_MAC(hw));
5091 softint_schedule(adapter->msf_si);
5092 }
5093 }
5094
5095 /* External PHY interrupt */
5096 if ((hw->phy.type == ixgbe_phy_x550em_ext_t) &&
5097 (eicr & IXGBE_EICR_GPI_SDP0_X540))
5098 softint_schedule(adapter->phy_si);
5099
5100 if (more) {
5101 que->req.ev_count++;
5102 ixgbe_sched_handle_que(adapter, que);
5103 } else
5104 ixgbe_enable_intr(adapter);
5105
5106 return 1;
5107 } /* ixgbe_legacy_irq */
5108
5109 /************************************************************************
5110 * ixgbe_free_pciintr_resources
5111 ************************************************************************/
5112 static void
5113 ixgbe_free_pciintr_resources(struct adapter *adapter)
5114 {
5115 struct ix_queue *que = adapter->queues;
5116 int rid;
5117
5118 /*
5119 * Release all msix queue resources:
5120 */
5121 for (int i = 0; i < adapter->num_queues; i++, que++) {
5122 if (que->res != NULL) {
5123 pci_intr_disestablish(adapter->osdep.pc,
5124 adapter->osdep.ihs[i]);
5125 adapter->osdep.ihs[i] = NULL;
5126 }
5127 }
5128
5129 /* Clean the Legacy or Link interrupt last */
5130 if (adapter->vector) /* we are doing MSIX */
5131 rid = adapter->vector;
5132 else
5133 rid = 0;
5134
5135 if (adapter->osdep.ihs[rid] != NULL) {
5136 pci_intr_disestablish(adapter->osdep.pc,
5137 adapter->osdep.ihs[rid]);
5138 adapter->osdep.ihs[rid] = NULL;
5139 }
5140
5141 if (adapter->osdep.intrs != NULL) {
5142 pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
5143 adapter->osdep.nintrs);
5144 adapter->osdep.intrs = NULL;
5145 }
5146 } /* ixgbe_free_pciintr_resources */
5147
5148 /************************************************************************
5149 * ixgbe_free_pci_resources
5150 ************************************************************************/
5151 static void
5152 ixgbe_free_pci_resources(struct adapter *adapter)
5153 {
5154
5155 ixgbe_free_pciintr_resources(adapter);
5156
5157 if (adapter->osdep.mem_size != 0) {
5158 bus_space_unmap(adapter->osdep.mem_bus_space_tag,
5159 adapter->osdep.mem_bus_space_handle,
5160 adapter->osdep.mem_size);
5161 }
5162
5163 } /* ixgbe_free_pci_resources */
5164
5165 /************************************************************************
5166 * ixgbe_set_sysctl_value
5167 ************************************************************************/
5168 static void
5169 ixgbe_set_sysctl_value(struct adapter *adapter, const char *name,
5170 const char *description, int *limit, int value)
5171 {
5172 device_t dev = adapter->dev;
5173 struct sysctllog **log;
5174 const struct sysctlnode *rnode, *cnode;
5175
5176 /*
5177 * It's not required to check recovery mode because this function never
5178 * touches hardware.
5179 */
5180
5181 log = &adapter->sysctllog;
5182 if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
5183 aprint_error_dev(dev, "could not create sysctl root\n");
5184 return;
5185 }
5186 if (sysctl_createv(log, 0, &rnode, &cnode,
5187 CTLFLAG_READWRITE, CTLTYPE_INT,
5188 name, SYSCTL_DESCR(description),
5189 NULL, 0, limit, 0, CTL_CREATE, CTL_EOL) != 0)
5190 aprint_error_dev(dev, "could not create sysctl\n");
5191 *limit = value;
5192 } /* ixgbe_set_sysctl_value */
5193
5194 /************************************************************************
5195 * ixgbe_sysctl_flowcntl
5196 *
5197 * SYSCTL wrapper around setting Flow Control
5198 ************************************************************************/
5199 static int
5200 ixgbe_sysctl_flowcntl(SYSCTLFN_ARGS)
5201 {
5202 struct sysctlnode node = *rnode;
5203 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5204 int error, fc;
5205
5206 if (ixgbe_fw_recovery_mode_swflag(adapter))
5207 return (EPERM);
5208
5209 fc = adapter->hw.fc.current_mode;
5210 node.sysctl_data = &fc;
5211 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5212 if (error != 0 || newp == NULL)
5213 return error;
5214
5215 /* Don't bother if it's not changed */
5216 if (fc == adapter->hw.fc.current_mode)
5217 return (0);
5218
5219 return ixgbe_set_flowcntl(adapter, fc);
5220 } /* ixgbe_sysctl_flowcntl */
5221
5222 /************************************************************************
5223 * ixgbe_set_flowcntl - Set flow control
5224 *
5225 * Flow control values:
5226 * 0 - off
5227 * 1 - rx pause
5228 * 2 - tx pause
5229 * 3 - full
5230 ************************************************************************/
5231 static int
5232 ixgbe_set_flowcntl(struct adapter *adapter, int fc)
5233 {
5234 switch (fc) {
5235 case ixgbe_fc_rx_pause:
5236 case ixgbe_fc_tx_pause:
5237 case ixgbe_fc_full:
5238 adapter->hw.fc.requested_mode = fc;
5239 if (adapter->num_queues > 1)
5240 ixgbe_disable_rx_drop(adapter);
5241 break;
5242 case ixgbe_fc_none:
5243 adapter->hw.fc.requested_mode = ixgbe_fc_none;
5244 if (adapter->num_queues > 1)
5245 ixgbe_enable_rx_drop(adapter);
5246 break;
5247 default:
5248 return (EINVAL);
5249 }
5250
5251 #if 0 /* XXX NetBSD */
5252 /* Don't autoneg if forcing a value */
5253 adapter->hw.fc.disable_fc_autoneg = TRUE;
5254 #endif
5255 ixgbe_fc_enable(&adapter->hw);
5256
5257 return (0);
5258 } /* ixgbe_set_flowcntl */
5259
5260 /************************************************************************
5261 * ixgbe_enable_rx_drop
5262 *
5263 * Enable the hardware to drop packets when the buffer is
5264 * full. This is useful with multiqueue, so that no single
5265 * queue being full stalls the entire RX engine. We only
5266 * enable this when Multiqueue is enabled AND Flow Control
5267 * is disabled.
5268 ************************************************************************/
5269 static void
5270 ixgbe_enable_rx_drop(struct adapter *adapter)
5271 {
5272 struct ixgbe_hw *hw = &adapter->hw;
5273 struct rx_ring *rxr;
5274 u32 srrctl;
5275
5276 for (int i = 0; i < adapter->num_queues; i++) {
5277 rxr = &adapter->rx_rings[i];
5278 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
5279 srrctl |= IXGBE_SRRCTL_DROP_EN;
5280 IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
5281 }
5282
5283 /* enable drop for each vf */
5284 for (int i = 0; i < adapter->num_vfs; i++) {
5285 IXGBE_WRITE_REG(hw, IXGBE_QDE,
5286 (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT) |
5287 IXGBE_QDE_ENABLE));
5288 }
5289 } /* ixgbe_enable_rx_drop */
5290
5291 /************************************************************************
5292 * ixgbe_disable_rx_drop
5293 ************************************************************************/
5294 static void
5295 ixgbe_disable_rx_drop(struct adapter *adapter)
5296 {
5297 struct ixgbe_hw *hw = &adapter->hw;
5298 struct rx_ring *rxr;
5299 u32 srrctl;
5300
5301 for (int i = 0; i < adapter->num_queues; i++) {
5302 rxr = &adapter->rx_rings[i];
5303 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(rxr->me));
5304 srrctl &= ~IXGBE_SRRCTL_DROP_EN;
5305 IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(rxr->me), srrctl);
5306 }
5307
5308 /* disable drop for each vf */
5309 for (int i = 0; i < adapter->num_vfs; i++) {
5310 IXGBE_WRITE_REG(hw, IXGBE_QDE,
5311 (IXGBE_QDE_WRITE | (i << IXGBE_QDE_IDX_SHIFT)));
5312 }
5313 } /* ixgbe_disable_rx_drop */
5314
5315 /************************************************************************
5316 * ixgbe_sysctl_advertise
5317 *
5318 * SYSCTL wrapper around setting advertised speed
5319 ************************************************************************/
5320 static int
5321 ixgbe_sysctl_advertise(SYSCTLFN_ARGS)
5322 {
5323 struct sysctlnode node = *rnode;
5324 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5325 int error = 0, advertise;
5326
5327 if (ixgbe_fw_recovery_mode_swflag(adapter))
5328 return (EPERM);
5329
5330 advertise = adapter->advertise;
5331 node.sysctl_data = &advertise;
5332 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5333 if (error != 0 || newp == NULL)
5334 return error;
5335
5336 return ixgbe_set_advertise(adapter, advertise);
5337 } /* ixgbe_sysctl_advertise */
5338
5339 /************************************************************************
5340 * ixgbe_set_advertise - Control advertised link speed
5341 *
5342 * Flags:
5343 * 0x00 - Default (all capable link speed)
5344 * 0x01 - advertise 100 Mb
5345 * 0x02 - advertise 1G
5346 * 0x04 - advertise 10G
5347 * 0x08 - advertise 10 Mb
5348 * 0x10 - advertise 2.5G
5349 * 0x20 - advertise 5G
5350 ************************************************************************/
5351 static int
5352 ixgbe_set_advertise(struct adapter *adapter, int advertise)
5353 {
5354 device_t dev;
5355 struct ixgbe_hw *hw;
5356 ixgbe_link_speed speed = 0;
5357 ixgbe_link_speed link_caps = 0;
5358 s32 err = IXGBE_NOT_IMPLEMENTED;
5359 bool negotiate = FALSE;
5360
5361 /* Checks to validate new value */
5362 if (adapter->advertise == advertise) /* no change */
5363 return (0);
5364
5365 dev = adapter->dev;
5366 hw = &adapter->hw;
5367
5368 /* No speed changes for backplane media */
5369 if (hw->phy.media_type == ixgbe_media_type_backplane)
5370 return (ENODEV);
5371
5372 if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
5373 (hw->phy.multispeed_fiber))) {
5374 device_printf(dev,
5375 "Advertised speed can only be set on copper or "
5376 "multispeed fiber media types.\n");
5377 return (EINVAL);
5378 }
5379
5380 if (advertise < 0x0 || advertise > 0x2f) {
5381 device_printf(dev,
5382 "Invalid advertised speed; valid modes are 0x0 through 0x7\n");
5383 return (EINVAL);
5384 }
5385
5386 if (hw->mac.ops.get_link_capabilities) {
5387 err = hw->mac.ops.get_link_capabilities(hw, &link_caps,
5388 &negotiate);
5389 if (err != IXGBE_SUCCESS) {
5390 device_printf(dev, "Unable to determine supported advertise speeds\n");
5391 return (ENODEV);
5392 }
5393 }
5394
5395 /* Set new value and report new advertised mode */
5396 if (advertise & 0x1) {
5397 if (!(link_caps & IXGBE_LINK_SPEED_100_FULL)) {
5398 device_printf(dev, "Interface does not support 100Mb advertised speed\n");
5399 return (EINVAL);
5400 }
5401 speed |= IXGBE_LINK_SPEED_100_FULL;
5402 }
5403 if (advertise & 0x2) {
5404 if (!(link_caps & IXGBE_LINK_SPEED_1GB_FULL)) {
5405 device_printf(dev, "Interface does not support 1Gb advertised speed\n");
5406 return (EINVAL);
5407 }
5408 speed |= IXGBE_LINK_SPEED_1GB_FULL;
5409 }
5410 if (advertise & 0x4) {
5411 if (!(link_caps & IXGBE_LINK_SPEED_10GB_FULL)) {
5412 device_printf(dev, "Interface does not support 10Gb advertised speed\n");
5413 return (EINVAL);
5414 }
5415 speed |= IXGBE_LINK_SPEED_10GB_FULL;
5416 }
5417 if (advertise & 0x8) {
5418 if (!(link_caps & IXGBE_LINK_SPEED_10_FULL)) {
5419 device_printf(dev, "Interface does not support 10Mb advertised speed\n");
5420 return (EINVAL);
5421 }
5422 speed |= IXGBE_LINK_SPEED_10_FULL;
5423 }
5424 if (advertise & 0x10) {
5425 if (!(link_caps & IXGBE_LINK_SPEED_2_5GB_FULL)) {
5426 device_printf(dev, "Interface does not support 2.5Gb advertised speed\n");
5427 return (EINVAL);
5428 }
5429 speed |= IXGBE_LINK_SPEED_2_5GB_FULL;
5430 }
5431 if (advertise & 0x20) {
5432 if (!(link_caps & IXGBE_LINK_SPEED_5GB_FULL)) {
5433 device_printf(dev, "Interface does not support 5Gb advertised speed\n");
5434 return (EINVAL);
5435 }
5436 speed |= IXGBE_LINK_SPEED_5GB_FULL;
5437 }
5438 if (advertise == 0)
5439 speed = link_caps; /* All capable link speed */
5440
5441 hw->mac.autotry_restart = TRUE;
5442 hw->mac.ops.setup_link(hw, speed, TRUE);
5443 adapter->advertise = advertise;
5444
5445 return (0);
5446 } /* ixgbe_set_advertise */
5447
5448 /************************************************************************
5449 * ixgbe_get_advertise - Get current advertised speed settings
5450 *
5451 * Formatted for sysctl usage.
5452 * Flags:
5453 * 0x01 - advertise 100 Mb
5454 * 0x02 - advertise 1G
5455 * 0x04 - advertise 10G
5456 * 0x08 - advertise 10 Mb (yes, Mb)
5457 * 0x10 - advertise 2.5G
5458 * 0x20 - advertise 5G
5459 ************************************************************************/
5460 static int
5461 ixgbe_get_advertise(struct adapter *adapter)
5462 {
5463 struct ixgbe_hw *hw = &adapter->hw;
5464 int speed;
5465 ixgbe_link_speed link_caps = 0;
5466 s32 err;
5467 bool negotiate = FALSE;
5468
5469 /*
5470 * Advertised speed means nothing unless it's copper or
5471 * multi-speed fiber
5472 */
5473 if (!(hw->phy.media_type == ixgbe_media_type_copper) &&
5474 !(hw->phy.multispeed_fiber))
5475 return (0);
5476
5477 err = hw->mac.ops.get_link_capabilities(hw, &link_caps, &negotiate);
5478 if (err != IXGBE_SUCCESS)
5479 return (0);
5480
5481 speed =
5482 ((link_caps & IXGBE_LINK_SPEED_10GB_FULL) ? 0x04 : 0) |
5483 ((link_caps & IXGBE_LINK_SPEED_1GB_FULL) ? 0x02 : 0) |
5484 ((link_caps & IXGBE_LINK_SPEED_100_FULL) ? 0x01 : 0) |
5485 ((link_caps & IXGBE_LINK_SPEED_10_FULL) ? 0x08 : 0) |
5486 ((link_caps & IXGBE_LINK_SPEED_2_5GB_FULL) ? 0x10 : 0) |
5487 ((link_caps & IXGBE_LINK_SPEED_5GB_FULL) ? 0x20 : 0);
5488
5489 return speed;
5490 } /* ixgbe_get_advertise */
5491
5492 /************************************************************************
5493 * ixgbe_sysctl_dmac - Manage DMA Coalescing
5494 *
5495 * Control values:
5496 * 0/1 - off / on (use default value of 1000)
5497 *
5498 * Legal timer values are:
5499 * 50,100,250,500,1000,2000,5000,10000
5500 *
5501 * Turning off interrupt moderation will also turn this off.
5502 ************************************************************************/
5503 static int
5504 ixgbe_sysctl_dmac(SYSCTLFN_ARGS)
5505 {
5506 struct sysctlnode node = *rnode;
5507 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5508 struct ifnet *ifp = adapter->ifp;
5509 int error;
5510 int newval;
5511
5512 if (ixgbe_fw_recovery_mode_swflag(adapter))
5513 return (EPERM);
5514
5515 newval = adapter->dmac;
5516 node.sysctl_data = &newval;
5517 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5518 if ((error) || (newp == NULL))
5519 return (error);
5520
5521 switch (newval) {
5522 case 0:
5523 /* Disabled */
5524 adapter->dmac = 0;
5525 break;
5526 case 1:
5527 /* Enable and use default */
5528 adapter->dmac = 1000;
5529 break;
5530 case 50:
5531 case 100:
5532 case 250:
5533 case 500:
5534 case 1000:
5535 case 2000:
5536 case 5000:
5537 case 10000:
5538 /* Legal values - allow */
5539 adapter->dmac = newval;
5540 break;
5541 default:
5542 /* Do nothing, illegal value */
5543 return (EINVAL);
5544 }
5545
5546 /* Re-initialize hardware if it's already running */
5547 if (ifp->if_flags & IFF_RUNNING)
5548 ifp->if_init(ifp);
5549
5550 return (0);
5551 }
5552
5553 #ifdef IXGBE_DEBUG
5554 /************************************************************************
5555 * ixgbe_sysctl_power_state
5556 *
5557 * Sysctl to test power states
5558 * Values:
5559 * 0 - set device to D0
5560 * 3 - set device to D3
5561 * (none) - get current device power state
5562 ************************************************************************/
5563 static int
5564 ixgbe_sysctl_power_state(SYSCTLFN_ARGS)
5565 {
5566 #ifdef notyet
5567 struct sysctlnode node = *rnode;
5568 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5569 device_t dev = adapter->dev;
5570 int curr_ps, new_ps, error = 0;
5571
5572 if (ixgbe_fw_recovery_mode_swflag(adapter))
5573 return (EPERM);
5574
5575 curr_ps = new_ps = pci_get_powerstate(dev);
5576
5577 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5578 if ((error) || (req->newp == NULL))
5579 return (error);
5580
5581 if (new_ps == curr_ps)
5582 return (0);
5583
5584 if (new_ps == 3 && curr_ps == 0)
5585 error = DEVICE_SUSPEND(dev);
5586 else if (new_ps == 0 && curr_ps == 3)
5587 error = DEVICE_RESUME(dev);
5588 else
5589 return (EINVAL);
5590
5591 device_printf(dev, "New state: %d\n", pci_get_powerstate(dev));
5592
5593 return (error);
5594 #else
5595 return 0;
5596 #endif
5597 } /* ixgbe_sysctl_power_state */
5598 #endif
5599
5600 /************************************************************************
5601 * ixgbe_sysctl_wol_enable
5602 *
5603 * Sysctl to enable/disable the WoL capability,
5604 * if supported by the adapter.
5605 *
5606 * Values:
5607 * 0 - disabled
5608 * 1 - enabled
5609 ************************************************************************/
5610 static int
5611 ixgbe_sysctl_wol_enable(SYSCTLFN_ARGS)
5612 {
5613 struct sysctlnode node = *rnode;
5614 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5615 struct ixgbe_hw *hw = &adapter->hw;
5616 bool new_wol_enabled;
5617 int error = 0;
5618
5619 /*
5620 * It's not required to check recovery mode because this function never
5621 * touches hardware.
5622 */
5623 new_wol_enabled = hw->wol_enabled;
5624 node.sysctl_data = &new_wol_enabled;
5625 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5626 if ((error) || (newp == NULL))
5627 return (error);
5628 if (new_wol_enabled == hw->wol_enabled)
5629 return (0);
5630
5631 if (new_wol_enabled && !adapter->wol_support)
5632 return (ENODEV);
5633 else
5634 hw->wol_enabled = new_wol_enabled;
5635
5636 return (0);
5637 } /* ixgbe_sysctl_wol_enable */
5638
5639 /************************************************************************
5640 * ixgbe_sysctl_wufc - Wake Up Filter Control
5641 *
5642 * Sysctl to enable/disable the types of packets that the
5643 * adapter will wake up on upon receipt.
5644 * Flags:
5645 * 0x1 - Link Status Change
5646 * 0x2 - Magic Packet
5647 * 0x4 - Direct Exact
5648 * 0x8 - Directed Multicast
5649 * 0x10 - Broadcast
5650 * 0x20 - ARP/IPv4 Request Packet
5651 * 0x40 - Direct IPv4 Packet
5652 * 0x80 - Direct IPv6 Packet
5653 *
5654 * Settings not listed above will cause the sysctl to return an error.
5655 ************************************************************************/
5656 static int
5657 ixgbe_sysctl_wufc(SYSCTLFN_ARGS)
5658 {
5659 struct sysctlnode node = *rnode;
5660 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5661 int error = 0;
5662 u32 new_wufc;
5663
5664 /*
5665 * It's not required to check recovery mode because this function never
5666 * touches hardware.
5667 */
5668 new_wufc = adapter->wufc;
5669 node.sysctl_data = &new_wufc;
5670 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5671 if ((error) || (newp == NULL))
5672 return (error);
5673 if (new_wufc == adapter->wufc)
5674 return (0);
5675
5676 if (new_wufc & 0xffffff00)
5677 return (EINVAL);
5678
5679 new_wufc &= 0xff;
5680 new_wufc |= (0xffffff & adapter->wufc);
5681 adapter->wufc = new_wufc;
5682
5683 return (0);
5684 } /* ixgbe_sysctl_wufc */
5685
5686 #ifdef IXGBE_DEBUG
5687 /************************************************************************
5688 * ixgbe_sysctl_print_rss_config
5689 ************************************************************************/
5690 static int
5691 ixgbe_sysctl_print_rss_config(SYSCTLFN_ARGS)
5692 {
5693 #ifdef notyet
5694 struct sysctlnode node = *rnode;
5695 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5696 struct ixgbe_hw *hw = &adapter->hw;
5697 device_t dev = adapter->dev;
5698 struct sbuf *buf;
5699 int error = 0, reta_size;
5700 u32 reg;
5701
5702 if (ixgbe_fw_recovery_mode_swflag(adapter))
5703 return (EPERM);
5704
5705 buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
5706 if (!buf) {
5707 device_printf(dev, "Could not allocate sbuf for output.\n");
5708 return (ENOMEM);
5709 }
5710
5711 // TODO: use sbufs to make a string to print out
5712 /* Set multiplier for RETA setup and table size based on MAC */
5713 switch (adapter->hw.mac.type) {
5714 case ixgbe_mac_X550:
5715 case ixgbe_mac_X550EM_x:
5716 case ixgbe_mac_X550EM_a:
5717 reta_size = 128;
5718 break;
5719 default:
5720 reta_size = 32;
5721 break;
5722 }
5723
5724 /* Print out the redirection table */
5725 sbuf_cat(buf, "\n");
5726 for (int i = 0; i < reta_size; i++) {
5727 if (i < 32) {
5728 reg = IXGBE_READ_REG(hw, IXGBE_RETA(i));
5729 sbuf_printf(buf, "RETA(%2d): 0x%08x\n", i, reg);
5730 } else {
5731 reg = IXGBE_READ_REG(hw, IXGBE_ERETA(i - 32));
5732 sbuf_printf(buf, "ERETA(%2d): 0x%08x\n", i - 32, reg);
5733 }
5734 }
5735
5736 // TODO: print more config
5737
5738 error = sbuf_finish(buf);
5739 if (error)
5740 device_printf(dev, "Error finishing sbuf: %d\n", error);
5741
5742 sbuf_delete(buf);
5743 #endif
5744 return (0);
5745 } /* ixgbe_sysctl_print_rss_config */
5746 #endif /* IXGBE_DEBUG */
5747
5748 /************************************************************************
5749 * ixgbe_sysctl_phy_temp - Retrieve temperature of PHY
5750 *
5751 * For X552/X557-AT devices using an external PHY
5752 ************************************************************************/
5753 static int
5754 ixgbe_sysctl_phy_temp(SYSCTLFN_ARGS)
5755 {
5756 struct sysctlnode node = *rnode;
5757 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5758 struct ixgbe_hw *hw = &adapter->hw;
5759 int val;
5760 u16 reg;
5761 int error;
5762
5763 if (ixgbe_fw_recovery_mode_swflag(adapter))
5764 return (EPERM);
5765
5766 if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
5767 device_printf(adapter->dev,
5768 "Device has no supported external thermal sensor.\n");
5769 return (ENODEV);
5770 }
5771
5772 if (hw->phy.ops.read_reg(hw, IXGBE_PHY_CURRENT_TEMP,
5773 IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, ®)) {
5774 device_printf(adapter->dev,
5775 "Error reading from PHY's current temperature register\n");
5776 return (EAGAIN);
5777 }
5778
5779 node.sysctl_data = &val;
5780
5781 /* Shift temp for output */
5782 val = reg >> 8;
5783
5784 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5785 if ((error) || (newp == NULL))
5786 return (error);
5787
5788 return (0);
5789 } /* ixgbe_sysctl_phy_temp */
5790
5791 /************************************************************************
5792 * ixgbe_sysctl_phy_overtemp_occurred
5793 *
5794 * Reports (directly from the PHY) whether the current PHY
5795 * temperature is over the overtemp threshold.
5796 ************************************************************************/
5797 static int
5798 ixgbe_sysctl_phy_overtemp_occurred(SYSCTLFN_ARGS)
5799 {
5800 struct sysctlnode node = *rnode;
5801 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5802 struct ixgbe_hw *hw = &adapter->hw;
5803 int val, error;
5804 u16 reg;
5805
5806 if (ixgbe_fw_recovery_mode_swflag(adapter))
5807 return (EPERM);
5808
5809 if (hw->device_id != IXGBE_DEV_ID_X550EM_X_10G_T) {
5810 device_printf(adapter->dev,
5811 "Device has no supported external thermal sensor.\n");
5812 return (ENODEV);
5813 }
5814
5815 if (hw->phy.ops.read_reg(hw, IXGBE_PHY_OVERTEMP_STATUS,
5816 IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE, ®)) {
5817 device_printf(adapter->dev,
5818 "Error reading from PHY's temperature status register\n");
5819 return (EAGAIN);
5820 }
5821
5822 node.sysctl_data = &val;
5823
5824 /* Get occurrence bit */
5825 val = !!(reg & 0x4000);
5826
5827 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5828 if ((error) || (newp == NULL))
5829 return (error);
5830
5831 return (0);
5832 } /* ixgbe_sysctl_phy_overtemp_occurred */
5833
5834 /************************************************************************
5835 * ixgbe_sysctl_eee_state
5836 *
5837 * Sysctl to set EEE power saving feature
5838 * Values:
5839 * 0 - disable EEE
5840 * 1 - enable EEE
5841 * (none) - get current device EEE state
5842 ************************************************************************/
5843 static int
5844 ixgbe_sysctl_eee_state(SYSCTLFN_ARGS)
5845 {
5846 struct sysctlnode node = *rnode;
5847 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5848 struct ifnet *ifp = adapter->ifp;
5849 device_t dev = adapter->dev;
5850 int curr_eee, new_eee, error = 0;
5851 s32 retval;
5852
5853 if (ixgbe_fw_recovery_mode_swflag(adapter))
5854 return (EPERM);
5855
5856 curr_eee = new_eee = !!(adapter->feat_en & IXGBE_FEATURE_EEE);
5857 node.sysctl_data = &new_eee;
5858 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5859 if ((error) || (newp == NULL))
5860 return (error);
5861
5862 /* Nothing to do */
5863 if (new_eee == curr_eee)
5864 return (0);
5865
5866 /* Not supported */
5867 if (!(adapter->feat_cap & IXGBE_FEATURE_EEE))
5868 return (EINVAL);
5869
5870 /* Bounds checking */
5871 if ((new_eee < 0) || (new_eee > 1))
5872 return (EINVAL);
5873
5874 retval = ixgbe_setup_eee(&adapter->hw, new_eee);
5875 if (retval) {
5876 device_printf(dev, "Error in EEE setup: 0x%08X\n", retval);
5877 return (EINVAL);
5878 }
5879
5880 /* Restart auto-neg */
5881 ifp->if_init(ifp);
5882
5883 device_printf(dev, "New EEE state: %d\n", new_eee);
5884
5885 /* Cache new value */
5886 if (new_eee)
5887 adapter->feat_en |= IXGBE_FEATURE_EEE;
5888 else
5889 adapter->feat_en &= ~IXGBE_FEATURE_EEE;
5890
5891 return (error);
5892 } /* ixgbe_sysctl_eee_state */
5893
5894 #define PRINTQS(adapter, regname) \
5895 do { \
5896 struct ixgbe_hw *_hw = &(adapter)->hw; \
5897 int _i; \
5898 \
5899 printf("%s: %s", device_xname((adapter)->dev), #regname); \
5900 for (_i = 0; _i < (adapter)->num_queues; _i++) { \
5901 printf((_i == 0) ? "\t" : " "); \
5902 printf("%08x", IXGBE_READ_REG(_hw, \
5903 IXGBE_##regname(_i))); \
5904 } \
5905 printf("\n"); \
5906 } while (0)
5907
5908 /************************************************************************
5909 * ixgbe_print_debug_info
5910 *
5911 * Called only when em_display_debug_stats is enabled.
5912 * Provides a way to take a look at important statistics
5913 * maintained by the driver and hardware.
5914 ************************************************************************/
5915 static void
5916 ixgbe_print_debug_info(struct adapter *adapter)
5917 {
5918 device_t dev = adapter->dev;
5919 struct ixgbe_hw *hw = &adapter->hw;
5920 int table_size;
5921 int i;
5922
5923 switch (adapter->hw.mac.type) {
5924 case ixgbe_mac_X550:
5925 case ixgbe_mac_X550EM_x:
5926 case ixgbe_mac_X550EM_a:
5927 table_size = 128;
5928 break;
5929 default:
5930 table_size = 32;
5931 break;
5932 }
5933
5934 device_printf(dev, "[E]RETA:\n");
5935 for (i = 0; i < table_size; i++) {
5936 if (i < 32)
5937 printf("%02x: %08x\n", i, IXGBE_READ_REG(hw,
5938 IXGBE_RETA(i)));
5939 else
5940 printf("%02x: %08x\n", i, IXGBE_READ_REG(hw,
5941 IXGBE_ERETA(i - 32)));
5942 }
5943
5944 device_printf(dev, "queue:");
5945 for (i = 0; i < adapter->num_queues; i++) {
5946 printf((i == 0) ? "\t" : " ");
5947 printf("%8d", i);
5948 }
5949 printf("\n");
5950 PRINTQS(adapter, RDBAL);
5951 PRINTQS(adapter, RDBAH);
5952 PRINTQS(adapter, RDLEN);
5953 PRINTQS(adapter, SRRCTL);
5954 PRINTQS(adapter, RDH);
5955 PRINTQS(adapter, RDT);
5956 PRINTQS(adapter, RXDCTL);
5957
5958 device_printf(dev, "RQSMR:");
5959 for (i = 0; i < adapter->num_queues / 4; i++) {
5960 printf((i == 0) ? "\t" : " ");
5961 printf("%08x", IXGBE_READ_REG(hw, IXGBE_RQSMR(i)));
5962 }
5963 printf("\n");
5964
5965 device_printf(dev, "disabled_count:");
5966 for (i = 0; i < adapter->num_queues; i++) {
5967 printf((i == 0) ? "\t" : " ");
5968 printf("%8d", adapter->queues[i].disabled_count);
5969 }
5970 printf("\n");
5971
5972 device_printf(dev, "EIMS:\t%08x\n", IXGBE_READ_REG(hw, IXGBE_EIMS));
5973 if (hw->mac.type != ixgbe_mac_82598EB) {
5974 device_printf(dev, "EIMS_EX(0):\t%08x\n",
5975 IXGBE_READ_REG(hw, IXGBE_EIMS_EX(0)));
5976 device_printf(dev, "EIMS_EX(1):\t%08x\n",
5977 IXGBE_READ_REG(hw, IXGBE_EIMS_EX(1)));
5978 }
5979 } /* ixgbe_print_debug_info */
5980
5981 /************************************************************************
5982 * ixgbe_sysctl_debug
5983 ************************************************************************/
5984 static int
5985 ixgbe_sysctl_debug(SYSCTLFN_ARGS)
5986 {
5987 struct sysctlnode node = *rnode;
5988 struct adapter *adapter = (struct adapter *)node.sysctl_data;
5989 int error, result = 0;
5990
5991 if (ixgbe_fw_recovery_mode_swflag(adapter))
5992 return (EPERM);
5993
5994 node.sysctl_data = &result;
5995 error = sysctl_lookup(SYSCTLFN_CALL(&node));
5996
5997 if (error || newp == NULL)
5998 return error;
5999
6000 if (result == 1)
6001 ixgbe_print_debug_info(adapter);
6002
6003 return 0;
6004 } /* ixgbe_sysctl_debug */
6005
6006 /************************************************************************
6007 * ixgbe_init_device_features
6008 ************************************************************************/
6009 static void
6010 ixgbe_init_device_features(struct adapter *adapter)
6011 {
6012 adapter->feat_cap = IXGBE_FEATURE_NETMAP
6013 | IXGBE_FEATURE_RSS
6014 | IXGBE_FEATURE_MSI
6015 | IXGBE_FEATURE_MSIX
6016 | IXGBE_FEATURE_LEGACY_IRQ
6017 | IXGBE_FEATURE_LEGACY_TX;
6018
6019 /* Set capabilities first... */
6020 switch (adapter->hw.mac.type) {
6021 case ixgbe_mac_82598EB:
6022 if (adapter->hw.device_id == IXGBE_DEV_ID_82598AT)
6023 adapter->feat_cap |= IXGBE_FEATURE_FAN_FAIL;
6024 break;
6025 case ixgbe_mac_X540:
6026 adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
6027 adapter->feat_cap |= IXGBE_FEATURE_FDIR;
6028 if ((adapter->hw.device_id == IXGBE_DEV_ID_X540_BYPASS) &&
6029 (adapter->hw.bus.func == 0))
6030 adapter->feat_cap |= IXGBE_FEATURE_BYPASS;
6031 break;
6032 case ixgbe_mac_X550:
6033 /*
6034 * IXGBE_FEATURE_RECOVERY_MODE will be set after reading
6035 * NVM Image version.
6036 */
6037 adapter->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
6038 adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
6039 adapter->feat_cap |= IXGBE_FEATURE_FDIR;
6040 break;
6041 case ixgbe_mac_X550EM_x:
6042 /*
6043 * IXGBE_FEATURE_RECOVERY_MODE will be set after reading
6044 * NVM Image version.
6045 */
6046 adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
6047 adapter->feat_cap |= IXGBE_FEATURE_FDIR;
6048 break;
6049 case ixgbe_mac_X550EM_a:
6050 /*
6051 * IXGBE_FEATURE_RECOVERY_MODE will be set after reading
6052 * NVM Image version.
6053 */
6054 adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
6055 adapter->feat_cap |= IXGBE_FEATURE_FDIR;
6056 adapter->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
6057 if ((adapter->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T) ||
6058 (adapter->hw.device_id == IXGBE_DEV_ID_X550EM_A_1G_T_L)) {
6059 adapter->feat_cap |= IXGBE_FEATURE_TEMP_SENSOR;
6060 adapter->feat_cap |= IXGBE_FEATURE_EEE;
6061 }
6062 break;
6063 case ixgbe_mac_82599EB:
6064 adapter->feat_cap |= IXGBE_FEATURE_SRIOV;
6065 adapter->feat_cap |= IXGBE_FEATURE_FDIR;
6066 if ((adapter->hw.device_id == IXGBE_DEV_ID_82599_BYPASS) &&
6067 (adapter->hw.bus.func == 0))
6068 adapter->feat_cap |= IXGBE_FEATURE_BYPASS;
6069 if (adapter->hw.device_id == IXGBE_DEV_ID_82599_QSFP_SF_QP)
6070 adapter->feat_cap &= ~IXGBE_FEATURE_LEGACY_IRQ;
6071 break;
6072 default:
6073 break;
6074 }
6075
6076 /* Enabled by default... */
6077 /* Fan failure detection */
6078 if (adapter->feat_cap & IXGBE_FEATURE_FAN_FAIL)
6079 adapter->feat_en |= IXGBE_FEATURE_FAN_FAIL;
6080 /* Netmap */
6081 if (adapter->feat_cap & IXGBE_FEATURE_NETMAP)
6082 adapter->feat_en |= IXGBE_FEATURE_NETMAP;
6083 /* EEE */
6084 if (adapter->feat_cap & IXGBE_FEATURE_EEE)
6085 adapter->feat_en |= IXGBE_FEATURE_EEE;
6086 /* Thermal Sensor */
6087 if (adapter->feat_cap & IXGBE_FEATURE_TEMP_SENSOR)
6088 adapter->feat_en |= IXGBE_FEATURE_TEMP_SENSOR;
6089 /*
6090 * Recovery mode:
6091 * NetBSD: IXGBE_FEATURE_RECOVERY_MODE will be controlled after reading
6092 * NVM Image version.
6093 */
6094
6095 /* Enabled via global sysctl... */
6096 /* Flow Director */
6097 if (ixgbe_enable_fdir) {
6098 if (adapter->feat_cap & IXGBE_FEATURE_FDIR)
6099 adapter->feat_en |= IXGBE_FEATURE_FDIR;
6100 else
6101 device_printf(adapter->dev, "Device does not support Flow Director. Leaving disabled.");
6102 }
6103 /* Legacy (single queue) transmit */
6104 if ((adapter->feat_cap & IXGBE_FEATURE_LEGACY_TX) &&
6105 ixgbe_enable_legacy_tx)
6106 adapter->feat_en |= IXGBE_FEATURE_LEGACY_TX;
6107 /*
6108 * Message Signal Interrupts - Extended (MSI-X)
6109 * Normal MSI is only enabled if MSI-X calls fail.
6110 */
6111 if (!ixgbe_enable_msix)
6112 adapter->feat_cap &= ~IXGBE_FEATURE_MSIX;
6113 /* Receive-Side Scaling (RSS) */
6114 if ((adapter->feat_cap & IXGBE_FEATURE_RSS) && ixgbe_enable_rss)
6115 adapter->feat_en |= IXGBE_FEATURE_RSS;
6116
6117 /* Disable features with unmet dependencies... */
6118 /* No MSI-X */
6119 if (!(adapter->feat_cap & IXGBE_FEATURE_MSIX)) {
6120 adapter->feat_cap &= ~IXGBE_FEATURE_RSS;
6121 adapter->feat_cap &= ~IXGBE_FEATURE_SRIOV;
6122 adapter->feat_en &= ~IXGBE_FEATURE_RSS;
6123 adapter->feat_en &= ~IXGBE_FEATURE_SRIOV;
6124 }
6125 } /* ixgbe_init_device_features */
6126
6127 /************************************************************************
6128 * ixgbe_probe - Device identification routine
6129 *
6130 * Determines if the driver should be loaded on
6131 * adapter based on its PCI vendor/device ID.
6132 *
6133 * return BUS_PROBE_DEFAULT on success, positive on failure
6134 ************************************************************************/
6135 static int
6136 ixgbe_probe(device_t dev, cfdata_t cf, void *aux)
6137 {
6138 const struct pci_attach_args *pa = aux;
6139
6140 return (ixgbe_lookup(pa) != NULL) ? 1 : 0;
6141 }
6142
6143 static const ixgbe_vendor_info_t *
6144 ixgbe_lookup(const struct pci_attach_args *pa)
6145 {
6146 const ixgbe_vendor_info_t *ent;
6147 pcireg_t subid;
6148
6149 INIT_DEBUGOUT("ixgbe_lookup: begin");
6150
6151 if (PCI_VENDOR(pa->pa_id) != IXGBE_INTEL_VENDOR_ID)
6152 return NULL;
6153
6154 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
6155
6156 for (ent = ixgbe_vendor_info_array; ent->vendor_id != 0; ent++) {
6157 if ((PCI_VENDOR(pa->pa_id) == ent->vendor_id) &&
6158 (PCI_PRODUCT(pa->pa_id) == ent->device_id) &&
6159 ((PCI_SUBSYS_VENDOR(subid) == ent->subvendor_id) ||
6160 (ent->subvendor_id == 0)) &&
6161 ((PCI_SUBSYS_ID(subid) == ent->subdevice_id) ||
6162 (ent->subdevice_id == 0))) {
6163 return ent;
6164 }
6165 }
6166 return NULL;
6167 }
6168
6169 static int
6170 ixgbe_ifflags_cb(struct ethercom *ec)
6171 {
6172 struct ifnet *ifp = &ec->ec_if;
6173 struct adapter *adapter = ifp->if_softc;
6174 u_short change;
6175 int rv = 0;
6176
6177 IXGBE_CORE_LOCK(adapter);
6178
6179 change = ifp->if_flags ^ adapter->if_flags;
6180 if (change != 0)
6181 adapter->if_flags = ifp->if_flags;
6182
6183 if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
6184 rv = ENETRESET;
6185 goto out;
6186 } else if ((change & IFF_PROMISC) != 0)
6187 ixgbe_set_rxfilter(adapter);
6188
6189 /* Check for ec_capenable. */
6190 change = ec->ec_capenable ^ adapter->ec_capenable;
6191 adapter->ec_capenable = ec->ec_capenable;
6192 if ((change & ~(ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING
6193 | ETHERCAP_VLAN_HWFILTER)) != 0) {
6194 rv = ENETRESET;
6195 goto out;
6196 }
6197
6198 /*
6199 * Special handling is not required for ETHERCAP_VLAN_MTU.
6200 * MAXFRS(MHADD) does not include the 4bytes of the VLAN header.
6201 */
6202
6203 /* Set up VLAN support and filter */
6204 if ((change & (ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_HWFILTER)) != 0)
6205 ixgbe_setup_vlan_hw_support(adapter);
6206
6207 out:
6208 IXGBE_CORE_UNLOCK(adapter);
6209
6210 return rv;
6211 }
6212
6213 /************************************************************************
6214 * ixgbe_ioctl - Ioctl entry point
6215 *
6216 * Called when the user wants to configure the interface.
6217 *
6218 * return 0 on success, positive on failure
6219 ************************************************************************/
6220 static int
6221 ixgbe_ioctl(struct ifnet * ifp, u_long command, void *data)
6222 {
6223 struct adapter *adapter = ifp->if_softc;
6224 struct ixgbe_hw *hw = &adapter->hw;
6225 struct ifcapreq *ifcr = data;
6226 struct ifreq *ifr = data;
6227 int error = 0;
6228 int l4csum_en;
6229 const int l4csum = IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
6230 IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
6231
6232 if (ixgbe_fw_recovery_mode_swflag(adapter))
6233 return (EPERM);
6234
6235 switch (command) {
6236 case SIOCSIFFLAGS:
6237 IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
6238 break;
6239 case SIOCADDMULTI:
6240 case SIOCDELMULTI:
6241 IOCTL_DEBUGOUT("ioctl: SIOC(ADD|DEL)MULTI");
6242 break;
6243 case SIOCSIFMEDIA:
6244 case SIOCGIFMEDIA:
6245 IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
6246 break;
6247 case SIOCSIFCAP:
6248 IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
6249 break;
6250 case SIOCSIFMTU:
6251 IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
6252 break;
6253 #ifdef __NetBSD__
6254 case SIOCINITIFADDR:
6255 IOCTL_DEBUGOUT("ioctl: SIOCINITIFADDR");
6256 break;
6257 case SIOCGIFFLAGS:
6258 IOCTL_DEBUGOUT("ioctl: SIOCGIFFLAGS");
6259 break;
6260 case SIOCGIFAFLAG_IN:
6261 IOCTL_DEBUGOUT("ioctl: SIOCGIFAFLAG_IN");
6262 break;
6263 case SIOCGIFADDR:
6264 IOCTL_DEBUGOUT("ioctl: SIOCGIFADDR");
6265 break;
6266 case SIOCGIFMTU:
6267 IOCTL_DEBUGOUT("ioctl: SIOCGIFMTU (Get Interface MTU)");
6268 break;
6269 case SIOCGIFCAP:
6270 IOCTL_DEBUGOUT("ioctl: SIOCGIFCAP (Get IF cap)");
6271 break;
6272 case SIOCGETHERCAP:
6273 IOCTL_DEBUGOUT("ioctl: SIOCGETHERCAP (Get ethercap)");
6274 break;
6275 case SIOCGLIFADDR:
6276 IOCTL_DEBUGOUT("ioctl: SIOCGLIFADDR (Get Interface addr)");
6277 break;
6278 case SIOCZIFDATA:
6279 IOCTL_DEBUGOUT("ioctl: SIOCZIFDATA (Zero counter)");
6280 hw->mac.ops.clear_hw_cntrs(hw);
6281 ixgbe_clear_evcnt(adapter);
6282 break;
6283 case SIOCAIFADDR:
6284 IOCTL_DEBUGOUT("ioctl: SIOCAIFADDR (add/chg IF alias)");
6285 break;
6286 #endif
6287 default:
6288 IOCTL_DEBUGOUT1("ioctl: UNKNOWN (0x%X)", (int)command);
6289 break;
6290 }
6291
6292 switch (command) {
6293 case SIOCGI2C:
6294 {
6295 struct ixgbe_i2c_req i2c;
6296
6297 IOCTL_DEBUGOUT("ioctl: SIOCGI2C (Get I2C Data)");
6298 error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
6299 if (error != 0)
6300 break;
6301 if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
6302 error = EINVAL;
6303 break;
6304 }
6305 if (i2c.len > sizeof(i2c.data)) {
6306 error = EINVAL;
6307 break;
6308 }
6309
6310 hw->phy.ops.read_i2c_byte(hw, i2c.offset,
6311 i2c.dev_addr, i2c.data);
6312 error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
6313 break;
6314 }
6315 case SIOCSIFCAP:
6316 /* Layer-4 Rx checksum offload has to be turned on and
6317 * off as a unit.
6318 */
6319 l4csum_en = ifcr->ifcr_capenable & l4csum;
6320 if (l4csum_en != l4csum && l4csum_en != 0)
6321 return EINVAL;
6322 /*FALLTHROUGH*/
6323 case SIOCADDMULTI:
6324 case SIOCDELMULTI:
6325 case SIOCSIFFLAGS:
6326 case SIOCSIFMTU:
6327 default:
6328 if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
6329 return error;
6330 if ((ifp->if_flags & IFF_RUNNING) == 0)
6331 ;
6332 else if (command == SIOCSIFCAP || command == SIOCSIFMTU) {
6333 IXGBE_CORE_LOCK(adapter);
6334 if ((ifp->if_flags & IFF_RUNNING) != 0)
6335 ixgbe_init_locked(adapter);
6336 ixgbe_recalculate_max_frame(adapter);
6337 IXGBE_CORE_UNLOCK(adapter);
6338 } else if (command == SIOCADDMULTI || command == SIOCDELMULTI) {
6339 /*
6340 * Multicast list has changed; set the hardware filter
6341 * accordingly.
6342 */
6343 IXGBE_CORE_LOCK(adapter);
6344 ixgbe_disable_intr(adapter);
6345 ixgbe_set_rxfilter(adapter);
6346 ixgbe_enable_intr(adapter);
6347 IXGBE_CORE_UNLOCK(adapter);
6348 }
6349 return 0;
6350 }
6351
6352 return error;
6353 } /* ixgbe_ioctl */
6354
6355 /************************************************************************
6356 * ixgbe_check_fan_failure
6357 ************************************************************************/
6358 static void
6359 ixgbe_check_fan_failure(struct adapter *adapter, u32 reg, bool in_interrupt)
6360 {
6361 u32 mask;
6362
6363 mask = (in_interrupt) ? IXGBE_EICR_GPI_SDP1_BY_MAC(&adapter->hw) :
6364 IXGBE_ESDP_SDP1;
6365
6366 if (reg & mask)
6367 device_printf(adapter->dev, "\nCRITICAL: FAN FAILURE!! REPLACE IMMEDIATELY!!\n");
6368 } /* ixgbe_check_fan_failure */
6369
6370 /************************************************************************
6371 * ixgbe_handle_que
6372 ************************************************************************/
6373 static void
6374 ixgbe_handle_que(void *context)
6375 {
6376 struct ix_queue *que = context;
6377 struct adapter *adapter = que->adapter;
6378 struct tx_ring *txr = que->txr;
6379 struct ifnet *ifp = adapter->ifp;
6380 bool more = false;
6381
6382 que->handleq.ev_count++;
6383
6384 if (ifp->if_flags & IFF_RUNNING) {
6385 more = ixgbe_rxeof(que);
6386 IXGBE_TX_LOCK(txr);
6387 more |= ixgbe_txeof(txr);
6388 if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX))
6389 if (!ixgbe_mq_ring_empty(ifp, txr->txr_interq))
6390 ixgbe_mq_start_locked(ifp, txr);
6391 /* Only for queue 0 */
6392 /* NetBSD still needs this for CBQ */
6393 if ((&adapter->queues[0] == que)
6394 && (!ixgbe_legacy_ring_empty(ifp, NULL)))
6395 ixgbe_legacy_start_locked(ifp, txr);
6396 IXGBE_TX_UNLOCK(txr);
6397 }
6398
6399 if (more) {
6400 que->req.ev_count++;
6401 ixgbe_sched_handle_que(adapter, que);
6402 } else if (que->res != NULL) {
6403 /* Re-enable this interrupt */
6404 ixgbe_enable_queue(adapter, que->msix);
6405 } else
6406 ixgbe_enable_intr(adapter);
6407
6408 return;
6409 } /* ixgbe_handle_que */
6410
6411 /************************************************************************
6412 * ixgbe_handle_que_work
6413 ************************************************************************/
6414 static void
6415 ixgbe_handle_que_work(struct work *wk, void *context)
6416 {
6417 struct ix_queue *que = container_of(wk, struct ix_queue, wq_cookie);
6418
6419 /*
6420 * "enqueued flag" is not required here.
6421 * See ixgbe_msix_que().
6422 */
6423 ixgbe_handle_que(que);
6424 }
6425
6426 /************************************************************************
6427 * ixgbe_allocate_legacy - Setup the Legacy or MSI Interrupt handler
6428 ************************************************************************/
6429 static int
6430 ixgbe_allocate_legacy(struct adapter *adapter,
6431 const struct pci_attach_args *pa)
6432 {
6433 device_t dev = adapter->dev;
6434 struct ix_queue *que = adapter->queues;
6435 struct tx_ring *txr = adapter->tx_rings;
6436 int counts[PCI_INTR_TYPE_SIZE];
6437 pci_intr_type_t intr_type, max_type;
6438 char intrbuf[PCI_INTRSTR_LEN];
6439 char wqname[MAXCOMLEN];
6440 const char *intrstr = NULL;
6441 int defertx_error = 0, error;
6442
6443 /* We allocate a single interrupt resource */
6444 max_type = PCI_INTR_TYPE_MSI;
6445 counts[PCI_INTR_TYPE_MSIX] = 0;
6446 counts[PCI_INTR_TYPE_MSI] =
6447 (adapter->feat_en & IXGBE_FEATURE_MSI) ? 1 : 0;
6448 /* Check not feat_en but feat_cap to fallback to INTx */
6449 counts[PCI_INTR_TYPE_INTX] =
6450 (adapter->feat_cap & IXGBE_FEATURE_LEGACY_IRQ) ? 1 : 0;
6451
6452 alloc_retry:
6453 if (pci_intr_alloc(pa, &adapter->osdep.intrs, counts, max_type) != 0) {
6454 aprint_error_dev(dev, "couldn't alloc interrupt\n");
6455 return ENXIO;
6456 }
6457 adapter->osdep.nintrs = 1;
6458 intrstr = pci_intr_string(adapter->osdep.pc, adapter->osdep.intrs[0],
6459 intrbuf, sizeof(intrbuf));
6460 adapter->osdep.ihs[0] = pci_intr_establish_xname(adapter->osdep.pc,
6461 adapter->osdep.intrs[0], IPL_NET, ixgbe_legacy_irq, que,
6462 device_xname(dev));
6463 intr_type = pci_intr_type(adapter->osdep.pc, adapter->osdep.intrs[0]);
6464 if (adapter->osdep.ihs[0] == NULL) {
6465 aprint_error_dev(dev,"unable to establish %s\n",
6466 (intr_type == PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
6467 pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
6468 adapter->osdep.intrs = NULL;
6469 switch (intr_type) {
6470 case PCI_INTR_TYPE_MSI:
6471 /* The next try is for INTx: Disable MSI */
6472 max_type = PCI_INTR_TYPE_INTX;
6473 counts[PCI_INTR_TYPE_INTX] = 1;
6474 adapter->feat_en &= ~IXGBE_FEATURE_MSI;
6475 if (adapter->feat_cap & IXGBE_FEATURE_LEGACY_IRQ) {
6476 adapter->feat_en |= IXGBE_FEATURE_LEGACY_IRQ;
6477 goto alloc_retry;
6478 } else
6479 break;
6480 case PCI_INTR_TYPE_INTX:
6481 default:
6482 /* See below */
6483 break;
6484 }
6485 }
6486 if (intr_type == PCI_INTR_TYPE_INTX) {
6487 adapter->feat_en &= ~IXGBE_FEATURE_MSI;
6488 adapter->feat_en |= IXGBE_FEATURE_LEGACY_IRQ;
6489 }
6490 if (adapter->osdep.ihs[0] == NULL) {
6491 aprint_error_dev(dev,
6492 "couldn't establish interrupt%s%s\n",
6493 intrstr ? " at " : "", intrstr ? intrstr : "");
6494 pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
6495 adapter->osdep.intrs = NULL;
6496 return ENXIO;
6497 }
6498 aprint_normal_dev(dev, "interrupting at %s\n", intrstr);
6499 /*
6500 * Try allocating a fast interrupt and the associated deferred
6501 * processing contexts.
6502 */
6503 if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX)) {
6504 txr->txr_si =
6505 softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
6506 ixgbe_deferred_mq_start, txr);
6507
6508 snprintf(wqname, sizeof(wqname), "%sdeferTx", device_xname(dev));
6509 defertx_error = workqueue_create(&adapter->txr_wq, wqname,
6510 ixgbe_deferred_mq_start_work, adapter, IXGBE_WORKQUEUE_PRI,
6511 IPL_NET, IXGBE_WORKQUEUE_FLAGS);
6512 adapter->txr_wq_enqueued = percpu_alloc(sizeof(u_int));
6513 }
6514 que->que_si = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
6515 ixgbe_handle_que, que);
6516 snprintf(wqname, sizeof(wqname), "%sTxRx", device_xname(dev));
6517 error = workqueue_create(&adapter->que_wq, wqname,
6518 ixgbe_handle_que_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
6519 IXGBE_WORKQUEUE_FLAGS);
6520
6521 if ((!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX)
6522 && ((txr->txr_si == NULL) || defertx_error != 0))
6523 || (que->que_si == NULL) || error != 0) {
6524 aprint_error_dev(dev,
6525 "could not establish software interrupts\n");
6526
6527 return ENXIO;
6528 }
6529 /* For simplicity in the handlers */
6530 adapter->active_queues = IXGBE_EIMS_ENABLE_MASK;
6531
6532 return (0);
6533 } /* ixgbe_allocate_legacy */
6534
6535 /************************************************************************
6536 * ixgbe_allocate_msix - Setup MSI-X Interrupt resources and handlers
6537 ************************************************************************/
6538 static int
6539 ixgbe_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
6540 {
6541 device_t dev = adapter->dev;
6542 struct ix_queue *que = adapter->queues;
6543 struct tx_ring *txr = adapter->tx_rings;
6544 pci_chipset_tag_t pc;
6545 char intrbuf[PCI_INTRSTR_LEN];
6546 char intr_xname[32];
6547 char wqname[MAXCOMLEN];
6548 const char *intrstr = NULL;
6549 int error, vector = 0;
6550 int cpu_id = 0;
6551 kcpuset_t *affinity;
6552 #ifdef RSS
6553 unsigned int rss_buckets = 0;
6554 kcpuset_t cpu_mask;
6555 #endif
6556
6557 pc = adapter->osdep.pc;
6558 #ifdef RSS
6559 /*
6560 * If we're doing RSS, the number of queues needs to
6561 * match the number of RSS buckets that are configured.
6562 *
6563 * + If there's more queues than RSS buckets, we'll end
6564 * up with queues that get no traffic.
6565 *
6566 * + If there's more RSS buckets than queues, we'll end
6567 * up having multiple RSS buckets map to the same queue,
6568 * so there'll be some contention.
6569 */
6570 rss_buckets = rss_getnumbuckets();
6571 if ((adapter->feat_en & IXGBE_FEATURE_RSS) &&
6572 (adapter->num_queues != rss_buckets)) {
6573 device_printf(dev,
6574 "%s: number of queues (%d) != number of RSS buckets (%d)"
6575 "; performance will be impacted.\n",
6576 __func__, adapter->num_queues, rss_buckets);
6577 }
6578 #endif
6579
6580 adapter->osdep.nintrs = adapter->num_queues + 1;
6581 if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
6582 adapter->osdep.nintrs) != 0) {
6583 aprint_error_dev(dev,
6584 "failed to allocate MSI-X interrupt\n");
6585 return (ENXIO);
6586 }
6587
6588 kcpuset_create(&affinity, false);
6589 for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
6590 snprintf(intr_xname, sizeof(intr_xname), "%s TXRX%d",
6591 device_xname(dev), i);
6592 intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
6593 sizeof(intrbuf));
6594 #ifdef IXGBE_MPSAFE
6595 pci_intr_setattr(pc, &adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
6596 true);
6597 #endif
6598 /* Set the handler function */
6599 que->res = adapter->osdep.ihs[i] = pci_intr_establish_xname(pc,
6600 adapter->osdep.intrs[i], IPL_NET, ixgbe_msix_que, que,
6601 intr_xname);
6602 if (que->res == NULL) {
6603 aprint_error_dev(dev,
6604 "Failed to register QUE handler\n");
6605 error = ENXIO;
6606 goto err_out;
6607 }
6608 que->msix = vector;
6609 adapter->active_queues |= 1ULL << que->msix;
6610
6611 if (adapter->feat_en & IXGBE_FEATURE_RSS) {
6612 #ifdef RSS
6613 /*
6614 * The queue ID is used as the RSS layer bucket ID.
6615 * We look up the queue ID -> RSS CPU ID and select
6616 * that.
6617 */
6618 cpu_id = rss_getcpu(i % rss_getnumbuckets());
6619 CPU_SETOF(cpu_id, &cpu_mask);
6620 #endif
6621 } else {
6622 /*
6623 * Bind the MSI-X vector, and thus the
6624 * rings to the corresponding CPU.
6625 *
6626 * This just happens to match the default RSS
6627 * round-robin bucket -> queue -> CPU allocation.
6628 */
6629 if (adapter->num_queues > 1)
6630 cpu_id = i;
6631 }
6632 /* Round-robin affinity */
6633 kcpuset_zero(affinity);
6634 kcpuset_set(affinity, cpu_id % ncpu);
6635 error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
6636 NULL);
6637 aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
6638 intrstr);
6639 if (error == 0) {
6640 #if 1 /* def IXGBE_DEBUG */
6641 #ifdef RSS
6642 aprintf_normal(", bound RSS bucket %d to CPU %d", i,
6643 cpu_id % ncpu);
6644 #else
6645 aprint_normal(", bound queue %d to cpu %d", i,
6646 cpu_id % ncpu);
6647 #endif
6648 #endif /* IXGBE_DEBUG */
6649 }
6650 aprint_normal("\n");
6651
6652 if (!(adapter->feat_en & IXGBE_FEATURE_LEGACY_TX)) {
6653 txr->txr_si = softint_establish(
6654 SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
6655 ixgbe_deferred_mq_start, txr);
6656 if (txr->txr_si == NULL) {
6657 aprint_error_dev(dev,
6658 "couldn't establish software interrupt\n");
6659 error = ENXIO;
6660 goto err_out;
6661 }
6662 }
6663 que->que_si
6664 = softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
6665 ixgbe_handle_que, que);
6666 if (que->que_si == NULL) {
6667 aprint_error_dev(dev,
6668 "couldn't establish software interrupt\n");
6669 error = ENXIO;
6670 goto err_out;
6671 }
6672 }
6673 snprintf(wqname, sizeof(wqname), "%sdeferTx", device_xname(dev));
6674 error = workqueue_create(&adapter->txr_wq, wqname,
6675 ixgbe_deferred_mq_start_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
6676 IXGBE_WORKQUEUE_FLAGS);
6677 if (error) {
6678 aprint_error_dev(dev, "couldn't create workqueue for deferred Tx\n");
6679 goto err_out;
6680 }
6681 adapter->txr_wq_enqueued = percpu_alloc(sizeof(u_int));
6682
6683 snprintf(wqname, sizeof(wqname), "%sTxRx", device_xname(dev));
6684 error = workqueue_create(&adapter->que_wq, wqname,
6685 ixgbe_handle_que_work, adapter, IXGBE_WORKQUEUE_PRI, IPL_NET,
6686 IXGBE_WORKQUEUE_FLAGS);
6687 if (error) {
6688 aprint_error_dev(dev, "couldn't create workqueue for Tx/Rx\n");
6689 goto err_out;
6690 }
6691
6692 /* and Link */
6693 cpu_id++;
6694 snprintf(intr_xname, sizeof(intr_xname), "%s link", device_xname(dev));
6695 adapter->vector = vector;
6696 intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
6697 sizeof(intrbuf));
6698 #ifdef IXGBE_MPSAFE
6699 pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
6700 true);
6701 #endif
6702 /* Set the link handler function */
6703 adapter->osdep.ihs[vector] = pci_intr_establish_xname(pc,
6704 adapter->osdep.intrs[vector], IPL_NET, ixgbe_msix_link, adapter,
6705 intr_xname);
6706 if (adapter->osdep.ihs[vector] == NULL) {
6707 aprint_error_dev(dev, "Failed to register LINK handler\n");
6708 error = ENXIO;
6709 goto err_out;
6710 }
6711 /* Round-robin affinity */
6712 kcpuset_zero(affinity);
6713 kcpuset_set(affinity, cpu_id % ncpu);
6714 error = interrupt_distribute(adapter->osdep.ihs[vector], affinity,
6715 NULL);
6716
6717 aprint_normal_dev(dev,
6718 "for link, interrupting at %s", intrstr);
6719 if (error == 0)
6720 aprint_normal(", affinity to cpu %d\n", cpu_id % ncpu);
6721 else
6722 aprint_normal("\n");
6723
6724 if (adapter->feat_cap & IXGBE_FEATURE_SRIOV) {
6725 adapter->mbx_si =
6726 softint_establish(SOFTINT_NET | IXGBE_SOFTINFT_FLAGS,
6727 ixgbe_handle_mbx, adapter);
6728 if (adapter->mbx_si == NULL) {
6729 aprint_error_dev(dev,
6730 "could not establish software interrupts\n");
6731
6732 error = ENXIO;
6733 goto err_out;
6734 }
6735 }
6736
6737 kcpuset_destroy(affinity);
6738 aprint_normal_dev(dev,
6739 "Using MSI-X interrupts with %d vectors\n", vector + 1);
6740
6741 return (0);
6742
6743 err_out:
6744 kcpuset_destroy(affinity);
6745 ixgbe_free_softint(adapter);
6746 ixgbe_free_pciintr_resources(adapter);
6747 return (error);
6748 } /* ixgbe_allocate_msix */
6749
6750 /************************************************************************
6751 * ixgbe_configure_interrupts
6752 *
6753 * Setup MSI-X, MSI, or legacy interrupts (in that order).
6754 * This will also depend on user settings.
6755 ************************************************************************/
6756 static int
6757 ixgbe_configure_interrupts(struct adapter *adapter)
6758 {
6759 device_t dev = adapter->dev;
6760 struct ixgbe_mac_info *mac = &adapter->hw.mac;
6761 int want, queues, msgs;
6762
6763 /* Default to 1 queue if MSI-X setup fails */
6764 adapter->num_queues = 1;
6765
6766 /* Override by tuneable */
6767 if (!(adapter->feat_cap & IXGBE_FEATURE_MSIX))
6768 goto msi;
6769
6770 /*
6771 * NetBSD only: Use single vector MSI when number of CPU is 1 to save
6772 * interrupt slot.
6773 */
6774 if (ncpu == 1)
6775 goto msi;
6776
6777 /* First try MSI-X */
6778 msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
6779 msgs = MIN(msgs, IXG_MAX_NINTR);
6780 if (msgs < 2)
6781 goto msi;
6782
6783 adapter->msix_mem = (void *)1; /* XXX */
6784
6785 /* Figure out a reasonable auto config value */
6786 queues = (ncpu > (msgs - 1)) ? (msgs - 1) : ncpu;
6787
6788 #ifdef RSS
6789 /* If we're doing RSS, clamp at the number of RSS buckets */
6790 if (adapter->feat_en & IXGBE_FEATURE_RSS)
6791 queues = uimin(queues, rss_getnumbuckets());
6792 #endif
6793 if (ixgbe_num_queues > queues) {
6794 aprint_error_dev(adapter->dev, "ixgbe_num_queues (%d) is too large, using reduced amount (%d).\n", ixgbe_num_queues, queues);
6795 ixgbe_num_queues = queues;
6796 }
6797
6798 if (ixgbe_num_queues != 0)
6799 queues = ixgbe_num_queues;
6800 else
6801 queues = uimin(queues,
6802 uimin(mac->max_tx_queues, mac->max_rx_queues));
6803
6804 /* reflect correct sysctl value */
6805 ixgbe_num_queues = queues;
6806
6807 /*
6808 * Want one vector (RX/TX pair) per queue
6809 * plus an additional for Link.
6810 */
6811 want = queues + 1;
6812 if (msgs >= want)
6813 msgs = want;
6814 else {
6815 aprint_error_dev(dev, "MSI-X Configuration Problem, "
6816 "%d vectors but %d queues wanted!\n",
6817 msgs, want);
6818 goto msi;
6819 }
6820 adapter->num_queues = queues;
6821 adapter->feat_en |= IXGBE_FEATURE_MSIX;
6822 return (0);
6823
6824 /*
6825 * MSI-X allocation failed or provided us with
6826 * less vectors than needed. Free MSI-X resources
6827 * and we'll try enabling MSI.
6828 */
6829 msi:
6830 /* Without MSI-X, some features are no longer supported */
6831 adapter->feat_cap &= ~IXGBE_FEATURE_RSS;
6832 adapter->feat_en &= ~IXGBE_FEATURE_RSS;
6833 adapter->feat_cap &= ~IXGBE_FEATURE_SRIOV;
6834 adapter->feat_en &= ~IXGBE_FEATURE_SRIOV;
6835
6836 msgs = pci_msi_count(adapter->osdep.pc, adapter->osdep.tag);
6837 adapter->msix_mem = NULL; /* XXX */
6838 if (msgs > 1)
6839 msgs = 1;
6840 if (msgs != 0) {
6841 msgs = 1;
6842 adapter->feat_en |= IXGBE_FEATURE_MSI;
6843 return (0);
6844 }
6845
6846 if (!(adapter->feat_cap & IXGBE_FEATURE_LEGACY_IRQ)) {
6847 aprint_error_dev(dev,
6848 "Device does not support legacy interrupts.\n");
6849 return 1;
6850 }
6851
6852 adapter->feat_en |= IXGBE_FEATURE_LEGACY_IRQ;
6853
6854 return (0);
6855 } /* ixgbe_configure_interrupts */
6856
6857
6858 /************************************************************************
6859 * ixgbe_handle_link - Tasklet for MSI-X Link interrupts
6860 *
6861 * Done outside of interrupt context since the driver might sleep
6862 ************************************************************************/
6863 static void
6864 ixgbe_handle_link(void *context)
6865 {
6866 struct adapter *adapter = context;
6867 struct ixgbe_hw *hw = &adapter->hw;
6868
6869 IXGBE_CORE_LOCK(adapter);
6870 ++adapter->link_sicount.ev_count;
6871 ixgbe_check_link(hw, &adapter->link_speed, &adapter->link_up, 0);
6872 ixgbe_update_link_status(adapter);
6873
6874 /* Re-enable link interrupts */
6875 IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EIMS_LSC);
6876
6877 IXGBE_CORE_UNLOCK(adapter);
6878 } /* ixgbe_handle_link */
6879
6880 #if 0
6881 /************************************************************************
6882 * ixgbe_rearm_queues
6883 ************************************************************************/
6884 static __inline void
6885 ixgbe_rearm_queues(struct adapter *adapter, u64 queues)
6886 {
6887 u32 mask;
6888
6889 switch (adapter->hw.mac.type) {
6890 case ixgbe_mac_82598EB:
6891 mask = (IXGBE_EIMS_RTX_QUEUE & queues);
6892 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS, mask);
6893 break;
6894 case ixgbe_mac_82599EB:
6895 case ixgbe_mac_X540:
6896 case ixgbe_mac_X550:
6897 case ixgbe_mac_X550EM_x:
6898 case ixgbe_mac_X550EM_a:
6899 mask = (queues & 0xFFFFFFFF);
6900 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(0), mask);
6901 mask = (queues >> 32);
6902 IXGBE_WRITE_REG(&adapter->hw, IXGBE_EICS_EX(1), mask);
6903 break;
6904 default:
6905 break;
6906 }
6907 } /* ixgbe_rearm_queues */
6908 #endif
6909