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