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