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