xref: /freebsd/sys/dev/ixl/if_ixl.c (revision 9aff62dee28239f84b4aa4a3429cf26dbbf770cc)
1 /******************************************************************************
2 
3   Copyright (c) 2013-2018, 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 #include "ixl.h"
35 #include "ixl_pf.h"
36 
37 #ifdef IXL_IW
38 #include "ixl_iw.h"
39 #include "ixl_iw_int.h"
40 #endif
41 
42 #ifdef PCI_IOV
43 #include "ixl_pf_iov.h"
44 #endif
45 
46 /*********************************************************************
47  *  Driver version
48  *********************************************************************/
49 #define IXL_DRIVER_VERSION_MAJOR	2
50 #define IXL_DRIVER_VERSION_MINOR	3
51 #define IXL_DRIVER_VERSION_BUILD	3
52 
53 #define IXL_DRIVER_VERSION_STRING			\
54     __XSTRING(IXL_DRIVER_VERSION_MAJOR) "."		\
55     __XSTRING(IXL_DRIVER_VERSION_MINOR) "."		\
56     __XSTRING(IXL_DRIVER_VERSION_BUILD) "-k"
57 
58 /*********************************************************************
59  *  PCI Device ID Table
60  *
61  *  Used by probe to select devices to load on
62  *
63  *  ( Vendor ID, Device ID, Branding String )
64  *********************************************************************/
65 
66 static const pci_vendor_info_t ixl_vendor_info_array[] =
67 {
68 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_XL710, "Intel(R) Ethernet Controller X710 for 10GbE SFP+"),
69 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_B, "Intel(R) Ethernet Controller XL710 for 40GbE backplane"),
70 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_C, "Intel(R) Ethernet Controller X710 for 10GbE backplane"),
71 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_A, "Intel(R) Ethernet Controller XL710 for 40GbE QSFP+"),
72 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_B, "Intel(R) Ethernet Controller XL710 for 40GbE QSFP+"),
73 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_C, "Intel(R) Ethernet Controller X710 for 10GbE QSFP+"),
74 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T, "Intel(R) Ethernet Controller X710 for 10GBASE-T"),
75 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T4, "Intel(R) Ethernet Controller X710/X557-AT 10GBASE-T"),
76 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_KX_X722, "Intel(R) Ethernet Connection X722 for 10GbE backplane"),
77 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_QSFP_X722, "Intel(R) Ethernet Connection X722 for 10GbE QSFP+"),
78 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_X722, "Intel(R) Ethernet Connection X722 for 10GbE SFP+"),
79 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_1G_BASE_T_X722, "Intel(R) Ethernet Connection X722 for 1GbE"),
80 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T_X722, "Intel(R) Ethernet Connection X722 for 10GBASE-T"),
81 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_SFP_I_X722, "Intel(R) Ethernet Connection X722 for 10GbE SFP+"),
82 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_25G_B, "Intel(R) Ethernet Controller XXV710 for 25GbE backplane"),
83 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_25G_SFP28, "Intel(R) Ethernet Controller XXV710 for 25GbE SFP28"),
84 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_BASE_T_BC, "Intel(R) Ethernet Controller X710 for 10GBASE-T"),
85 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_SFP, "Intel(R) Ethernet Controller X710 for 10GbE SFP+"),
86 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_10G_B, "Intel(R) Ethernet Controller X710 for 10GbE backplane"),
87 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_5G_BASE_T_BC, "Intel(R) Ethernet Controller V710 for 5GBASE-T"),
88 	PVIDV(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_1G_BASE_T_BC, "Intel(R) Ethernet Controller I710 for 1GBASE-T"),
89 	/* required last entry */
90 	PVID_END
91 };
92 
93 /*********************************************************************
94  *  Function prototypes
95  *********************************************************************/
96 /*** IFLIB interface ***/
97 static void	*ixl_register(device_t dev);
98 static int	 ixl_if_attach_pre(if_ctx_t ctx);
99 static int	 ixl_if_attach_post(if_ctx_t ctx);
100 static int	 ixl_if_detach(if_ctx_t ctx);
101 static int	 ixl_if_shutdown(if_ctx_t ctx);
102 static int	 ixl_if_suspend(if_ctx_t ctx);
103 static int	 ixl_if_resume(if_ctx_t ctx);
104 static int	 ixl_if_msix_intr_assign(if_ctx_t ctx, int msix);
105 static void	 ixl_if_enable_intr(if_ctx_t ctx);
106 static void	 ixl_if_disable_intr(if_ctx_t ctx);
107 static int	 ixl_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
108 static int	 ixl_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
109 static int	 ixl_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets);
110 static int	 ixl_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nqs, int nqsets);
111 static void	 ixl_if_queues_free(if_ctx_t ctx);
112 static void	 ixl_if_update_admin_status(if_ctx_t ctx);
113 static void	 ixl_if_multi_set(if_ctx_t ctx);
114 static int	 ixl_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
115 static void	 ixl_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
116 static int	 ixl_if_media_change(if_ctx_t ctx);
117 static int	 ixl_if_promisc_set(if_ctx_t ctx, int flags);
118 static void	 ixl_if_timer(if_ctx_t ctx, uint16_t qid);
119 static void	 ixl_if_vlan_register(if_ctx_t ctx, u16 vtag);
120 static void	 ixl_if_vlan_unregister(if_ctx_t ctx, u16 vtag);
121 static uint64_t	 ixl_if_get_counter(if_ctx_t ctx, ift_counter cnt);
122 static int	 ixl_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req);
123 static int	 ixl_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data);
124 static bool	 ixl_if_needs_restart(if_ctx_t ctx, enum iflib_restart_event event);
125 static void	 ixl_if_led_func(if_ctx_t ctx, int onoff);
126 #ifdef PCI_IOV
127 static void	 ixl_if_vflr_handle(if_ctx_t ctx);
128 #endif
129 
130 /*** Other ***/
131 static void	 ixl_save_pf_tunables(struct ixl_pf *);
132 static int	 ixl_allocate_pci_resources(struct ixl_pf *);
133 static void	 ixl_setup_ssctx(struct ixl_pf *pf);
134 static void	 ixl_admin_timer(void *arg);
135 
136 /*********************************************************************
137  *  FreeBSD Device Interface Entry Points
138  *********************************************************************/
139 
140 static device_method_t ixl_methods[] = {
141 	/* Device interface */
142 	DEVMETHOD(device_register, ixl_register),
143 	DEVMETHOD(device_probe, iflib_device_probe),
144 	DEVMETHOD(device_attach, iflib_device_attach),
145 	DEVMETHOD(device_detach, iflib_device_detach),
146 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
147 	DEVMETHOD(device_suspend, iflib_device_suspend),
148 	DEVMETHOD(device_resume, iflib_device_resume),
149 #ifdef PCI_IOV
150 	DEVMETHOD(pci_iov_init, iflib_device_iov_init),
151 	DEVMETHOD(pci_iov_uninit, iflib_device_iov_uninit),
152 	DEVMETHOD(pci_iov_add_vf, iflib_device_iov_add_vf),
153 #endif
154 	DEVMETHOD_END
155 };
156 
157 static driver_t ixl_driver = {
158 	"ixl", ixl_methods, sizeof(struct ixl_pf),
159 };
160 
161 DRIVER_MODULE(ixl, pci, ixl_driver, 0, 0);
162 IFLIB_PNP_INFO(pci, ixl, ixl_vendor_info_array);
163 MODULE_VERSION(ixl, 3);
164 
165 MODULE_DEPEND(ixl, pci, 1, 1, 1);
166 MODULE_DEPEND(ixl, ether, 1, 1, 1);
167 MODULE_DEPEND(ixl, iflib, 1, 1, 1);
168 
169 static device_method_t ixl_if_methods[] = {
170 	DEVMETHOD(ifdi_attach_pre, ixl_if_attach_pre),
171 	DEVMETHOD(ifdi_attach_post, ixl_if_attach_post),
172 	DEVMETHOD(ifdi_detach, ixl_if_detach),
173 	DEVMETHOD(ifdi_shutdown, ixl_if_shutdown),
174 	DEVMETHOD(ifdi_suspend, ixl_if_suspend),
175 	DEVMETHOD(ifdi_resume, ixl_if_resume),
176 	DEVMETHOD(ifdi_init, ixl_if_init),
177 	DEVMETHOD(ifdi_stop, ixl_if_stop),
178 	DEVMETHOD(ifdi_msix_intr_assign, ixl_if_msix_intr_assign),
179 	DEVMETHOD(ifdi_intr_enable, ixl_if_enable_intr),
180 	DEVMETHOD(ifdi_intr_disable, ixl_if_disable_intr),
181 	DEVMETHOD(ifdi_rx_queue_intr_enable, ixl_if_rx_queue_intr_enable),
182 	DEVMETHOD(ifdi_tx_queue_intr_enable, ixl_if_tx_queue_intr_enable),
183 	DEVMETHOD(ifdi_tx_queues_alloc, ixl_if_tx_queues_alloc),
184 	DEVMETHOD(ifdi_rx_queues_alloc, ixl_if_rx_queues_alloc),
185 	DEVMETHOD(ifdi_queues_free, ixl_if_queues_free),
186 	DEVMETHOD(ifdi_update_admin_status, ixl_if_update_admin_status),
187 	DEVMETHOD(ifdi_multi_set, ixl_if_multi_set),
188 	DEVMETHOD(ifdi_mtu_set, ixl_if_mtu_set),
189 	DEVMETHOD(ifdi_media_status, ixl_if_media_status),
190 	DEVMETHOD(ifdi_media_change, ixl_if_media_change),
191 	DEVMETHOD(ifdi_promisc_set, ixl_if_promisc_set),
192 	DEVMETHOD(ifdi_timer, ixl_if_timer),
193 	DEVMETHOD(ifdi_vlan_register, ixl_if_vlan_register),
194 	DEVMETHOD(ifdi_vlan_unregister, ixl_if_vlan_unregister),
195 	DEVMETHOD(ifdi_get_counter, ixl_if_get_counter),
196 	DEVMETHOD(ifdi_i2c_req, ixl_if_i2c_req),
197 	DEVMETHOD(ifdi_priv_ioctl, ixl_if_priv_ioctl),
198 	DEVMETHOD(ifdi_needs_restart, ixl_if_needs_restart),
199 	DEVMETHOD(ifdi_led_func, ixl_if_led_func),
200 #ifdef PCI_IOV
201 	DEVMETHOD(ifdi_iov_init, ixl_if_iov_init),
202 	DEVMETHOD(ifdi_iov_uninit, ixl_if_iov_uninit),
203 	DEVMETHOD(ifdi_iov_vf_add, ixl_if_iov_vf_add),
204 	DEVMETHOD(ifdi_vflr_handle, ixl_if_vflr_handle),
205 #endif
206 	// ifdi_debug
207 	DEVMETHOD_END
208 };
209 
210 static driver_t ixl_if_driver = {
211 	"ixl_if", ixl_if_methods, sizeof(struct ixl_pf)
212 };
213 
214 /*
215 ** TUNEABLE PARAMETERS:
216 */
217 
218 static SYSCTL_NODE(_hw, OID_AUTO, ixl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
219     "ixl driver parameters");
220 
221 #ifdef IXL_DEBUG_FC
222 /*
223  * Leave this on unless you need to send flow control
224  * frames (or other control frames) from software
225  */
226 static int ixl_enable_tx_fc_filter = 1;
227 TUNABLE_INT("hw.ixl.enable_tx_fc_filter",
228     &ixl_enable_tx_fc_filter);
229 SYSCTL_INT(_hw_ixl, OID_AUTO, enable_tx_fc_filter, CTLFLAG_RDTUN,
230     &ixl_enable_tx_fc_filter, 0,
231     "Filter out packets with Ethertype 0x8808 from being sent out by non-HW sources");
232 #endif
233 
234 #ifdef IXL_DEBUG
235 static int ixl_debug_recovery_mode = 0;
236 TUNABLE_INT("hw.ixl.debug_recovery_mode",
237     &ixl_debug_recovery_mode);
238 SYSCTL_INT(_hw_ixl, OID_AUTO, debug_recovery_mode, CTLFLAG_RDTUN,
239     &ixl_debug_recovery_mode, 0,
240     "Act like when FW entered recovery mode (for debugging)");
241 #endif
242 
243 static int ixl_i2c_access_method = 0;
244 TUNABLE_INT("hw.ixl.i2c_access_method",
245     &ixl_i2c_access_method);
246 SYSCTL_INT(_hw_ixl, OID_AUTO, i2c_access_method, CTLFLAG_RDTUN,
247     &ixl_i2c_access_method, 0,
248     IXL_SYSCTL_HELP_I2C_METHOD);
249 
250 static int ixl_enable_vf_loopback = 1;
251 TUNABLE_INT("hw.ixl.enable_vf_loopback",
252     &ixl_enable_vf_loopback);
253 SYSCTL_INT(_hw_ixl, OID_AUTO, enable_vf_loopback, CTLFLAG_RDTUN,
254     &ixl_enable_vf_loopback, 0,
255     IXL_SYSCTL_HELP_VF_LOOPBACK);
256 
257 static int ixl_mdd_auto_reset_vf;
258 TUNABLE_INT("hw.ixl.mdd_auto_reset_vf", &ixl_mdd_auto_reset_vf);
259 SYSCTL_INT(_hw_ixl, OID_AUTO, mdd_auto_reset_vf, CTLFLAG_RDTUN,
260     &ixl_mdd_auto_reset_vf, 0,
261     "Automatically reset VFs blocked by malicious-driver detection");
262 
263 /*
264  * Different method for processing TX descriptor
265  * completion.
266  */
267 static int ixl_enable_head_writeback = 1;
268 TUNABLE_INT("hw.ixl.enable_head_writeback",
269     &ixl_enable_head_writeback);
270 SYSCTL_INT(_hw_ixl, OID_AUTO, enable_head_writeback, CTLFLAG_RDTUN,
271     &ixl_enable_head_writeback, 0,
272     "For detecting last completed TX descriptor by hardware, use value written by HW instead of checking descriptors");
273 
274 static int ixl_core_debug_mask = 0;
275 TUNABLE_INT("hw.ixl.core_debug_mask",
276     &ixl_core_debug_mask);
277 SYSCTL_INT(_hw_ixl, OID_AUTO, core_debug_mask, CTLFLAG_RDTUN,
278     &ixl_core_debug_mask, 0,
279     "Display debug statements that are printed in non-shared code");
280 
281 static int ixl_shared_debug_mask = 0;
282 TUNABLE_INT("hw.ixl.shared_debug_mask",
283     &ixl_shared_debug_mask);
284 SYSCTL_INT(_hw_ixl, OID_AUTO, shared_debug_mask, CTLFLAG_RDTUN,
285     &ixl_shared_debug_mask, 0,
286     "Display debug statements that are printed in shared code");
287 
288 #if 0
289 /*
290 ** Controls for Interrupt Throttling
291 **	- true/false for dynamic adjustment
292 ** 	- default values for static ITR
293 */
294 static int ixl_dynamic_rx_itr = 0;
295 TUNABLE_INT("hw.ixl.dynamic_rx_itr", &ixl_dynamic_rx_itr);
296 SYSCTL_INT(_hw_ixl, OID_AUTO, dynamic_rx_itr, CTLFLAG_RDTUN,
297     &ixl_dynamic_rx_itr, 0, "Dynamic RX Interrupt Rate");
298 
299 static int ixl_dynamic_tx_itr = 0;
300 TUNABLE_INT("hw.ixl.dynamic_tx_itr", &ixl_dynamic_tx_itr);
301 SYSCTL_INT(_hw_ixl, OID_AUTO, dynamic_tx_itr, CTLFLAG_RDTUN,
302     &ixl_dynamic_tx_itr, 0, "Dynamic TX Interrupt Rate");
303 #endif
304 
305 static int ixl_rx_itr = IXL_ITR_8K;
306 TUNABLE_INT("hw.ixl.rx_itr", &ixl_rx_itr);
307 SYSCTL_INT(_hw_ixl, OID_AUTO, rx_itr, CTLFLAG_RDTUN,
308     &ixl_rx_itr, 0, "RX Interrupt Rate");
309 
310 static int ixl_tx_itr = IXL_ITR_4K;
311 TUNABLE_INT("hw.ixl.tx_itr", &ixl_tx_itr);
312 SYSCTL_INT(_hw_ixl, OID_AUTO, tx_itr, CTLFLAG_RDTUN,
313     &ixl_tx_itr, 0, "TX Interrupt Rate");
314 
315 static int ixl_flow_control = -1;
316 SYSCTL_INT(_hw_ixl, OID_AUTO, flow_control, CTLFLAG_RDTUN,
317     &ixl_flow_control, 0, "Initial Flow Control setting");
318 
319 #ifdef IXL_IW
320 int ixl_enable_iwarp = 0;
321 TUNABLE_INT("hw.ixl.enable_iwarp", &ixl_enable_iwarp);
322 SYSCTL_INT(_hw_ixl, OID_AUTO, enable_iwarp, CTLFLAG_RDTUN,
323     &ixl_enable_iwarp, 0, "iWARP enabled");
324 
325 int ixl_limit_iwarp_msix = IXL_IW_MAX_MSIX;
326 TUNABLE_INT("hw.ixl.limit_iwarp_msix", &ixl_limit_iwarp_msix);
327 SYSCTL_INT(_hw_ixl, OID_AUTO, limit_iwarp_msix, CTLFLAG_RDTUN,
328     &ixl_limit_iwarp_msix, 0, "Limit MSI-X vectors assigned to iWARP");
329 #endif
330 
331 extern struct if_txrx ixl_txrx_hwb;
332 extern struct if_txrx ixl_txrx_dwb;
333 
334 static struct if_shared_ctx ixl_sctx_init = {
335 	.isc_magic = IFLIB_MAGIC,
336 	.isc_q_align = PAGE_SIZE,
337 	.isc_tx_maxsize = IXL_TSO_SIZE + sizeof(struct ether_vlan_header),
338 	.isc_tx_maxsegsize = IXL_MAX_DMA_SEG_SIZE,
339 	.isc_tso_maxsize = IXL_TSO_SIZE + sizeof(struct ether_vlan_header),
340 	.isc_tso_maxsegsize = IXL_MAX_DMA_SEG_SIZE,
341 	.isc_rx_maxsize = 16384,
342 	.isc_rx_nsegments = IXL_MAX_RX_SEGS,
343 	.isc_rx_maxsegsize = IXL_MAX_DMA_SEG_SIZE,
344 	.isc_nfl = 1,
345 	.isc_ntxqs = 1,
346 	.isc_nrxqs = 1,
347 
348 	.isc_admin_intrcnt = 1,
349 	.isc_vendor_info = ixl_vendor_info_array,
350 	.isc_driver_version = IXL_DRIVER_VERSION_STRING,
351 	.isc_driver = &ixl_if_driver,
352 	.isc_flags = IFLIB_NEED_SCRATCH | IFLIB_NEED_ZERO_CSUM | IFLIB_TSO_INIT_IP | IFLIB_ADMIN_ALWAYS_RUN,
353 
354 	.isc_nrxd_min = {IXL_MIN_RING},
355 	.isc_ntxd_min = {IXL_MIN_RING},
356 	.isc_nrxd_max = {IXL_MAX_RING},
357 	.isc_ntxd_max = {IXL_MAX_RING},
358 	.isc_nrxd_default = {IXL_DEFAULT_RING},
359 	.isc_ntxd_default = {IXL_DEFAULT_RING},
360 };
361 
362 /*** Functions ***/
363 static void *
364 ixl_register(device_t dev)
365 {
366 	return (&ixl_sctx_init);
367 }
368 static int
369 ixl_allocate_pci_resources(struct ixl_pf *pf)
370 {
371 	device_t dev = iflib_get_dev(pf->vsi.ctx);
372 	struct i40e_hw *hw = &pf->hw;
373 	int             rid;
374 
375 	/* Map BAR0 */
376 	rid = PCIR_BAR(0);
377 	pf->pci_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
378 	    &rid, RF_ACTIVE);
379 
380 	if (!(pf->pci_mem)) {
381 		device_printf(dev, "Unable to allocate bus resource: PCI memory\n");
382 		return (ENXIO);
383 	}
384 
385 	/* Save off the PCI information */
386 	hw->vendor_id = pci_get_vendor(dev);
387 	hw->device_id = pci_get_device(dev);
388 	hw->revision_id = pci_read_config(dev, PCIR_REVID, 1);
389 	hw->subsystem_vendor_id =
390 	    pci_read_config(dev, PCIR_SUBVEND_0, 2);
391 	hw->subsystem_device_id =
392 	    pci_read_config(dev, PCIR_SUBDEV_0, 2);
393 
394 	hw->bus.device = pci_get_slot(dev);
395 	hw->bus.func = pci_get_function(dev);
396 
397 	/* Save off register access information */
398 	pf->osdep.mem_bus_space_tag =
399 		rman_get_bustag(pf->pci_mem);
400 	pf->osdep.mem_bus_space_handle =
401 		rman_get_bushandle(pf->pci_mem);
402 	pf->osdep.mem_bus_space_size = rman_get_size(pf->pci_mem);
403 	pf->osdep.flush_reg = I40E_GLGEN_STAT;
404 	pf->osdep.dev = dev;
405 
406 	pf->hw.hw_addr = (u8 *) &pf->osdep.mem_bus_space_handle;
407 	pf->hw.back = &pf->osdep;
408 
409  	return (0);
410 }
411 
412 static void
413 ixl_setup_ssctx(struct ixl_pf *pf)
414 {
415 	if_softc_ctx_t scctx = pf->vsi.shared;
416 	struct i40e_hw *hw = &pf->hw;
417 
418 	if (IXL_PF_IN_RECOVERY_MODE(pf)) {
419 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 1;
420 		scctx->isc_ntxqsets = scctx->isc_nrxqsets = 1;
421 	} else if (hw->mac.type == I40E_MAC_X722)
422 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 128;
423 	else
424 		scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max = 64;
425 
426 	if (pf->vsi.enable_head_writeback) {
427 		scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
428 		    * sizeof(struct i40e_tx_desc) + sizeof(u32), DBA_ALIGN);
429 		scctx->isc_txrx = &ixl_txrx_hwb;
430 	} else {
431 		scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
432 		    * sizeof(struct i40e_tx_desc), DBA_ALIGN);
433 		scctx->isc_txrx = &ixl_txrx_dwb;
434 	}
435 
436 	scctx->isc_txrx->ift_legacy_intr = ixl_intr;
437 	scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0]
438 	    * sizeof(union i40e_32byte_rx_desc), DBA_ALIGN);
439 	scctx->isc_msix_bar = PCIR_BAR(IXL_MSIX_BAR);
440 	scctx->isc_tx_nsegments = IXL_MAX_TX_SEGS;
441 	scctx->isc_tx_tso_segments_max = IXL_MAX_TSO_SEGS;
442 	scctx->isc_tx_tso_size_max = IXL_TSO_SIZE;
443 	scctx->isc_tx_tso_segsize_max = IXL_MAX_DMA_SEG_SIZE;
444 	scctx->isc_rss_table_size = pf->hw.func_caps.rss_table_size;
445 	scctx->isc_tx_csum_flags = CSUM_OFFLOAD;
446 	scctx->isc_capabilities = scctx->isc_capenable = IXL_CAPS;
447 }
448 
449 static void
450 ixl_admin_timer(void *arg)
451 {
452 	struct ixl_pf *pf = (struct ixl_pf *)arg;
453 
454 	if (ixl_test_state(&pf->state, IXL_STATE_LINK_POLLING)) {
455 		struct i40e_hw *hw = &pf->hw;
456 		sbintime_t stime;
457 		enum i40e_status_code status;
458 
459 		hw->phy.get_link_info = TRUE;
460 		status = i40e_get_link_status(hw, &pf->link_up);
461 		if (status == I40E_SUCCESS) {
462 			ixl_clear_state(&pf->state, IXL_STATE_LINK_POLLING);
463 			/* OS link info is updated in the admin task */
464 		} else {
465 			device_printf(pf->dev,
466 			    "%s: i40e_get_link_status status %s, aq error %s\n",
467 			    __func__, i40e_stat_str(hw, status),
468 			    i40e_aq_str(hw, hw->aq.asq_last_status));
469 			stime = getsbinuptime();
470 			if (stime - pf->link_poll_start > IXL_PF_MAX_LINK_POLL) {
471 				device_printf(pf->dev, "Polling link status failed\n");
472 				ixl_clear_state(&pf->state, IXL_STATE_LINK_POLLING);
473 			}
474 		}
475 	}
476 
477 	/* Fire off the admin task */
478 	iflib_admin_intr_deferred(pf->vsi.ctx);
479 
480 	/* Reschedule the admin timer */
481 	callout_schedule(&pf->admin_timer, hz/2);
482 }
483 
484 static int
485 ixl_attach_pre_recovery_mode(struct ixl_pf *pf)
486 {
487 	struct ixl_vsi *vsi = &pf->vsi;
488 	struct i40e_hw *hw = &pf->hw;
489 	device_t dev = pf->dev;
490 
491 	device_printf(dev, "Firmware recovery mode detected. Limiting functionality. Refer to Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
492 
493 	i40e_get_mac_addr(hw, hw->mac.addr);
494 
495 	if (vsi->shared->isc_intr == IFLIB_INTR_MSIX) {
496 		ixl_configure_intr0_msix(pf);
497 		ixl_enable_intr0(hw);
498 	}
499 
500 	ixl_setup_ssctx(pf);
501 
502 	return (0);
503 }
504 
505 static int
506 ixl_if_attach_pre(if_ctx_t ctx)
507 {
508 	device_t dev;
509 	struct ixl_pf *pf;
510 	struct i40e_hw *hw;
511 	struct ixl_vsi *vsi;
512 	enum i40e_get_fw_lldp_status_resp lldp_status;
513 	struct i40e_filter_control_settings filter;
514 	enum i40e_status_code status;
515 	int error = 0;
516 
517 	dev = iflib_get_dev(ctx);
518 	pf = iflib_get_softc(ctx);
519 
520 	INIT_DBG_DEV(dev, "begin");
521 
522 	vsi = &pf->vsi;
523 	vsi->back = pf;
524 	pf->dev = dev;
525 	hw = &pf->hw;
526 
527 	vsi->dev = dev;
528 	vsi->hw = &pf->hw;
529 	vsi->id = 0;
530 	vsi->num_vlans = 0;
531 	vsi->ctx = ctx;
532 	vsi->media = iflib_get_media(ctx);
533 	vsi->shared = iflib_get_softc_ctx(ctx);
534 
535 	snprintf(pf->admin_mtx_name, sizeof(pf->admin_mtx_name),
536 	    "%s:admin", device_get_nameunit(dev));
537 	mtx_init(&pf->admin_mtx, pf->admin_mtx_name, NULL, MTX_DEF);
538 	callout_init_mtx(&pf->admin_timer, &pf->admin_mtx, 0);
539 
540 	/* Save tunable values */
541 	ixl_save_pf_tunables(pf);
542 
543 	/* Do PCI setup - map BAR0, etc */
544 	if (ixl_allocate_pci_resources(pf)) {
545 		device_printf(dev, "Allocation of PCI resources failed\n");
546 		error = ENXIO;
547 		goto err_pci_res;
548 	}
549 
550 	/* Establish a clean starting point */
551 	i40e_clear_hw(hw);
552 	i40e_set_mac_type(hw);
553 
554 	error = ixl_pf_reset(pf);
555 	if (error)
556 		goto err_out;
557 
558 	/* Initialize the shared code */
559 	status = i40e_init_shared_code(hw);
560 	if (status) {
561 		device_printf(dev, "Unable to initialize shared code, error %s\n",
562 		    i40e_stat_str(hw, status));
563 		error = EIO;
564 		goto err_out;
565 	}
566 
567 	/* Set up the admin queue */
568 	hw->aq.num_arq_entries = IXL_AQ_LEN;
569 	hw->aq.num_asq_entries = IXL_AQ_LEN;
570 	hw->aq.arq_buf_size = IXL_AQ_BUF_SZ;
571 	hw->aq.asq_buf_size = IXL_AQ_BUF_SZ;
572 
573 	status = i40e_init_adminq(hw);
574 	if (status != 0 && status != I40E_ERR_FIRMWARE_API_VERSION) {
575 		device_printf(dev, "Unable to initialize Admin Queue, error %s\n",
576 		    i40e_stat_str(hw, status));
577 		error = EIO;
578 		goto err_out;
579 	}
580 	ixl_print_nvm_version(pf);
581 
582 	if (status == I40E_ERR_FIRMWARE_API_VERSION) {
583 		device_printf(dev, "The driver for the device stopped "
584 		    "because the NVM image is newer than expected.\n");
585 		device_printf(dev, "You must install the most recent version of "
586 		    "the network driver.\n");
587 		error = EIO;
588 		goto err_out;
589 	}
590 
591         if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
592 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw)) {
593 		device_printf(dev, "The driver for the device detected "
594 		    "a newer version of the NVM image than expected.\n");
595 		device_printf(dev, "Please install the most recent version "
596 		    "of the network driver.\n");
597 	} else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4) {
598 		device_printf(dev, "The driver for the device detected "
599 		    "an older version of the NVM image than expected.\n");
600 		device_printf(dev, "Please update the NVM image.\n");
601 	}
602 
603 	if (IXL_PF_IN_RECOVERY_MODE(pf)) {
604 		error = ixl_attach_pre_recovery_mode(pf);
605 		if (error)
606 			goto err_out;
607 		return (error);
608 	}
609 
610 	/* Clear PXE mode */
611 	i40e_clear_pxe_mode(hw);
612 
613 	/* Get capabilities from the device */
614 	error = ixl_get_hw_capabilities(pf);
615 	if (error) {
616 		device_printf(dev, "get_hw_capabilities failed: %d\n",
617 		    error);
618 		goto err_get_cap;
619 	}
620 
621 	/* Set up host memory cache */
622 	error = ixl_setup_hmc(pf);
623 	if (error)
624 		goto err_mac_hmc;
625 
626 	/* Disable LLDP from the firmware for certain NVM versions */
627 	if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
628 	    (pf->hw.aq.fw_maj_ver < 4)) {
629 		i40e_aq_stop_lldp(hw, true, false, NULL);
630 		ixl_set_state(&pf->state, IXL_STATE_FW_LLDP_DISABLED);
631 	}
632 
633 	/* Try enabling Energy Efficient Ethernet (EEE) mode */
634 	if (i40e_enable_eee(hw, true) == I40E_SUCCESS)
635 		ixl_set_state(&pf->state, IXL_STATE_EEE_ENABLED);
636 	else
637 		ixl_clear_state(&pf->state, IXL_STATE_EEE_ENABLED);
638 
639 	/* Get MAC addresses from hardware */
640 	i40e_get_mac_addr(hw, hw->mac.addr);
641 	error = i40e_validate_mac_addr(hw->mac.addr);
642 	if (error) {
643 		device_printf(dev, "validate_mac_addr failed: %d\n", error);
644 		goto err_mac_hmc;
645 	}
646 	bcopy(hw->mac.addr, hw->mac.perm_addr, ETHER_ADDR_LEN);
647 	iflib_set_mac(ctx, hw->mac.addr);
648 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
649 
650 	/* Set up the device filtering */
651 	bzero(&filter, sizeof(filter));
652 	filter.enable_ethtype = TRUE;
653 	filter.enable_macvlan = TRUE;
654 	filter.enable_fdir = FALSE;
655 	filter.hash_lut_size = I40E_HASH_LUT_SIZE_512;
656 	if (i40e_set_filter_control(hw, &filter))
657 		device_printf(dev, "i40e_set_filter_control() failed\n");
658 
659 	/* Query device FW LLDP status */
660 	if (i40e_get_fw_lldp_status(hw, &lldp_status) == I40E_SUCCESS) {
661 		if (lldp_status == I40E_GET_FW_LLDP_STATUS_DISABLED) {
662 			ixl_set_state(&pf->state,
663 			    IXL_STATE_FW_LLDP_DISABLED);
664 		} else {
665 			ixl_clear_state(&pf->state,
666 			    IXL_STATE_FW_LLDP_DISABLED);
667 		}
668 	}
669 
670 	/* Tell FW to apply DCB config on link up */
671 	i40e_aq_set_dcb_parameters(hw, true, NULL);
672 
673 	/* Fill out iflib parameters */
674 	ixl_setup_ssctx(pf);
675 
676 	INIT_DBG_DEV(dev, "end");
677 	return (0);
678 
679 err_mac_hmc:
680 	ixl_shutdown_hmc(pf);
681 err_get_cap:
682 	i40e_shutdown_adminq(hw);
683 err_out:
684 	ixl_free_pci_resources(pf);
685 err_pci_res:
686 	mtx_lock(&pf->admin_mtx);
687 	callout_stop(&pf->admin_timer);
688 	mtx_unlock(&pf->admin_mtx);
689 	mtx_destroy(&pf->admin_mtx);
690 	return (error);
691 }
692 
693 static int
694 ixl_if_attach_post(if_ctx_t ctx)
695 {
696 	device_t dev;
697 	struct ixl_pf *pf;
698 	struct i40e_hw *hw;
699 	struct ixl_vsi *vsi;
700 	int error = 0;
701 	enum i40e_status_code status;
702 
703 	dev = iflib_get_dev(ctx);
704 	pf = iflib_get_softc(ctx);
705 
706 	INIT_DBG_DEV(dev, "begin");
707 
708 	vsi = &pf->vsi;
709 	vsi->ifp = iflib_get_ifp(ctx);
710 	hw = &pf->hw;
711 
712 	/* Save off determined number of queues for interface */
713 	vsi->num_rx_queues = vsi->shared->isc_nrxqsets;
714 	vsi->num_tx_queues = vsi->shared->isc_ntxqsets;
715 
716 	/* Setup OS network interface / ifnet */
717 	if (ixl_setup_interface(dev, pf)) {
718 		device_printf(dev, "interface setup failed!\n");
719 		error = EIO;
720 		goto err;
721 	}
722 
723 	if (IXL_PF_IN_RECOVERY_MODE(pf)) {
724 		/* Keep admin queue interrupts active while driver is loaded */
725 		if (vsi->shared->isc_intr == IFLIB_INTR_MSIX) {
726 			ixl_configure_intr0_msix(pf);
727 			ixl_enable_intr0(hw);
728 		}
729 
730 		ixl_add_sysctls_recovery_mode(pf);
731 
732 		/* Start the admin timer */
733 		mtx_lock(&pf->admin_mtx);
734 		callout_reset(&pf->admin_timer, hz/2, ixl_admin_timer, pf);
735 		mtx_unlock(&pf->admin_mtx);
736 		return (0);
737 	}
738 
739 	error = ixl_switch_config(pf);
740 	if (error) {
741 		device_printf(dev, "Initial ixl_switch_config() failed: %d\n",
742 		     error);
743 		goto err;
744 	}
745 
746 	/* Add protocol filters to list */
747 	ixl_init_filters(vsi);
748 
749 	/* Init queue allocation manager */
750 	error = ixl_pf_qmgr_init(&pf->qmgr, hw->func_caps.num_tx_qp);
751 	if (error) {
752 		device_printf(dev, "Failed to init queue manager for PF queues, error %d\n",
753 		    error);
754 		goto err;
755 	}
756 	/* reserve a contiguous allocation for the PF's VSI */
757 	error = ixl_pf_qmgr_alloc_contiguous(&pf->qmgr,
758 	    max(vsi->num_rx_queues, vsi->num_tx_queues), &pf->qtag);
759 	if (error) {
760 		device_printf(dev, "Failed to reserve queues for PF LAN VSI, error %d\n",
761 		    error);
762 		goto err;
763 	}
764 	device_printf(dev, "Allocating %d queues for PF LAN VSI; %d queues active\n",
765 	    pf->qtag.num_allocated, pf->qtag.num_active);
766 
767 	/* Determine link state */
768 	error = ixl_attach_get_link_status(pf);
769 	if (error == EINVAL)
770 		goto err;
771 
772 	/* Limit PHY interrupts to link, autoneg, and modules failure */
773 	status = i40e_aq_set_phy_int_mask(hw, IXL_DEFAULT_PHY_INT_MASK,
774 	    NULL);
775         if (status) {
776 		device_printf(dev, "i40e_aq_set_phy_mask() failed: err %s,"
777 		    " aq_err %s\n", i40e_stat_str(hw, status),
778 		    i40e_aq_str(hw, hw->aq.asq_last_status));
779 		goto err;
780 	}
781 
782 	/* Get the bus configuration and set the shared code */
783 	ixl_get_bus_info(pf);
784 
785 	/* Keep admin queue interrupts active while driver is loaded */
786 	if (vsi->shared->isc_intr == IFLIB_INTR_MSIX) {
787  		ixl_configure_intr0_msix(pf);
788  		ixl_enable_intr0(hw);
789 	}
790 
791 	/* Set initial advertised speed sysctl value */
792 	ixl_set_initial_advertised_speeds(pf);
793 
794 	/* Initialize statistics & add sysctls */
795 	ixl_add_device_sysctls(pf);
796 	ixl_pf_reset_stats(pf);
797 	ixl_update_stats_counters(pf);
798 	ixl_add_hw_stats(pf);
799 	ixl_vsi_reset_stats(vsi);
800 
801 	/*
802 	 * Driver may have been reloaded. Ensure that the link state
803 	 * is consistent with current settings.
804 	 */
805 	ixl_set_link(pf, ixl_test_state(&pf->state, IXL_STATE_LINK_ACTIVE_ON_DOWN));
806 
807 	hw->phy.get_link_info = true;
808 	status = i40e_get_link_status(hw, &pf->link_up);
809 	if (status != I40E_SUCCESS) {
810 		device_printf(dev,
811 		    "%s get link status, status: %s aq_err=%s\n",
812 		    __func__, i40e_stat_str(hw, status),
813 		    i40e_aq_str(hw, hw->aq.asq_last_status));
814 		/*
815 		 * Most probably FW has not finished configuring PHY.
816 		 * Retry periodically in a timer callback.
817 		 */
818 		ixl_set_state(&pf->state, IXL_STATE_LINK_POLLING);
819 		pf->link_poll_start = getsbinuptime();
820 	} else
821 		ixl_update_link_status(pf);
822 
823 #ifdef PCI_IOV
824 	ixl_initialize_sriov(pf);
825 #endif
826 
827 #ifdef IXL_IW
828 	if (hw->func_caps.iwarp && ixl_enable_iwarp) {
829 		pf->iw_enabled = (pf->iw_msix > 0) ? true : false;
830 		if (pf->iw_enabled) {
831 			error = ixl_iw_pf_attach(pf);
832 			if (error) {
833 				device_printf(dev,
834 				    "interfacing to iWARP driver failed: %d\n",
835 				    error);
836 				goto err;
837 			} else
838 				device_printf(dev, "iWARP ready\n");
839 		} else
840 			device_printf(dev, "iWARP disabled on this device "
841 			    "(no MSI-X vectors)\n");
842 	} else {
843 		pf->iw_enabled = false;
844 		device_printf(dev, "The device is not iWARP enabled\n");
845 	}
846 #endif
847 	/* Start the admin timer */
848 	mtx_lock(&pf->admin_mtx);
849 	callout_reset(&pf->admin_timer, hz/2, ixl_admin_timer, pf);
850 	mtx_unlock(&pf->admin_mtx);
851 
852 	INIT_DBG_DEV(dev, "end");
853 	return (0);
854 
855 err:
856 	INIT_DEBUGOUT("end: error %d", error);
857 	/* ixl_if_detach() is called on error from this */
858 	return (error);
859 }
860 
861 /**
862  * XXX: iflib always ignores the return value of detach()
863  * -> This means that this isn't allowed to fail
864  */
865 static int
866 ixl_if_detach(if_ctx_t ctx)
867 {
868 	struct ixl_pf *pf = iflib_get_softc(ctx);
869 	struct ixl_vsi *vsi = &pf->vsi;
870 	struct i40e_hw *hw = &pf->hw;
871 	device_t dev = pf->dev;
872 	enum i40e_status_code	status;
873 #ifdef IXL_IW
874 	int			error;
875 #endif
876 
877 	INIT_DBG_DEV(dev, "begin");
878 
879 	/* Stop the admin timer */
880 	mtx_lock(&pf->admin_mtx);
881 	callout_stop(&pf->admin_timer);
882 	mtx_unlock(&pf->admin_mtx);
883 	mtx_destroy(&pf->admin_mtx);
884 
885 #ifdef IXL_IW
886 	if (ixl_enable_iwarp && pf->iw_enabled) {
887 		error = ixl_iw_pf_detach(pf);
888 		if (error == EBUSY) {
889 			device_printf(dev, "iwarp in use; stop it first.\n");
890 			//return (error);
891 		}
892 	}
893 #endif
894 	/* Remove all previously allocated media types */
895 	ifmedia_removeall(vsi->media);
896 
897 	/* Shutdown LAN HMC */
898 	ixl_shutdown_hmc(pf);
899 
900 	/* Shutdown admin queue */
901 	ixl_disable_intr0(hw);
902 	status = i40e_shutdown_adminq(hw);
903 	if (status)
904 		device_printf(dev,
905 		    "i40e_shutdown_adminq() failed with status %s\n",
906 		    i40e_stat_str(hw, status));
907 
908 	ixl_pf_qmgr_destroy(&pf->qmgr);
909 	ixl_free_pci_resources(pf);
910 	ixl_free_filters(&vsi->ftl);
911 	INIT_DBG_DEV(dev, "end");
912 	return (0);
913 }
914 
915 static int
916 ixl_if_shutdown(if_ctx_t ctx)
917 {
918 	int error = 0;
919 
920 	INIT_DEBUGOUT("ixl_if_shutdown: begin");
921 
922 	/* TODO: Call ixl_if_stop()? */
923 
924 	/* TODO: Then setup low power mode */
925 
926 	return (error);
927 }
928 
929 static int
930 ixl_if_suspend(if_ctx_t ctx)
931 {
932 	int error = 0;
933 
934 	INIT_DEBUGOUT("ixl_if_suspend: begin");
935 
936 	/* TODO: Call ixl_if_stop()? */
937 
938 	/* TODO: Then setup low power mode */
939 
940 	return (error);
941 }
942 
943 static int
944 ixl_if_resume(if_ctx_t ctx)
945 {
946 	INIT_DEBUGOUT("ixl_if_resume: begin");
947 
948 	/* Read & clear wake-up registers */
949 
950 	return (0);
951 }
952 
953 void
954 ixl_if_init(if_ctx_t ctx)
955 {
956 	struct ixl_pf *pf = iflib_get_softc(ctx);
957 	struct ixl_vsi *vsi = &pf->vsi;
958 	struct i40e_hw	*hw = &pf->hw;
959 	if_t ifp = iflib_get_ifp(ctx);
960 	device_t 	dev = iflib_get_dev(ctx);
961 	u8		tmpaddr[ETHER_ADDR_LEN];
962 	int		ret;
963 
964 	if (IXL_PF_IN_RECOVERY_MODE(pf))
965 		goto fail;
966 	/*
967 	 * If the aq is dead here, it probably means something outside of the driver
968 	 * did something to the adapter, like a PF reset.
969 	 * So, rebuild the driver's state here if that occurs.
970 	 */
971 	if (!i40e_check_asq_alive(&pf->hw)) {
972 		device_printf(dev, "Admin Queue is down; resetting...\n");
973 		(void)ixl_teardown_hw_structs(pf);
974 		ret = ixl_rebuild_hw_structs_after_reset(pf, false);
975 		if (ret != 0)
976 			goto fail;
977 	}
978 
979 	/* Get the latest mac address... User might use a LAA */
980 	bcopy(if_getlladdr(vsi->ifp), tmpaddr, ETH_ALEN);
981 	if (!ixl_ether_is_equal(hw->mac.addr, tmpaddr) &&
982 	    (i40e_validate_mac_addr(tmpaddr) == I40E_SUCCESS)) {
983 		ret = i40e_aq_mac_address_write(hw,
984 		    I40E_AQC_WRITE_TYPE_LAA_ONLY,
985 		    tmpaddr, NULL);
986 		if (ret) {
987 			device_printf(dev, "LLA address change failed!!\n");
988 			goto fail;
989 		}
990 		ixl_del_all_vlan_filters(vsi, hw->mac.addr);
991 		bcopy(tmpaddr, hw->mac.addr, ETH_ALEN);
992 		/*
993 		 * New filters are configured by ixl_reconfigure_filters
994 		 * at the end of ixl_init_locked.
995 		 */
996 	}
997 
998 	iflib_set_mac(ctx, hw->mac.addr);
999 
1000 	/* Prepare the VSI: rings, hmc contexts, etc... */
1001 	if (ixl_initialize_vsi(vsi)) {
1002 		device_printf(dev, "initialize vsi failed!!\n");
1003 		goto fail;
1004 	}
1005 
1006 	ixl_set_link(pf, true);
1007 
1008 	/* Reconfigure multicast filters in HW */
1009 	ixl_if_multi_set(ctx);
1010 
1011 	/* Set up RSS */
1012 	ixl_config_rss(pf);
1013 
1014 	/* Set up MSI-X routing and the ITR settings */
1015 	if (vsi->shared->isc_intr == IFLIB_INTR_MSIX) {
1016 		ixl_configure_queue_intr_msix(pf);
1017 		ixl_configure_itr(pf);
1018 	} else
1019 		ixl_configure_legacy(pf);
1020 
1021 	if (vsi->enable_head_writeback)
1022 		ixl_init_tx_cidx(vsi);
1023 	else
1024 		ixl_init_tx_rsqs(vsi);
1025 
1026 	ret = ixl_enable_rings(vsi);
1027 	if (ret != 0) {
1028 		device_printf(dev, "enable rings failed: %d\n", ret);
1029 		ixl_disable_rings(pf, vsi, &pf->qtag);
1030 		goto fail;
1031 	}
1032 
1033 	i40e_aq_set_default_vsi(hw, vsi->seid, NULL);
1034 
1035 	/* Re-add configure filters to HW */
1036 	ixl_reconfigure_filters(vsi);
1037 
1038 	/* Configure promiscuous mode */
1039 	ixl_if_promisc_set(ctx, if_getflags(ifp));
1040 
1041 #ifdef IXL_IW
1042 	if (ixl_enable_iwarp && pf->iw_enabled) {
1043 		ret = ixl_iw_pf_init(pf);
1044 		if (ret)
1045 			device_printf(dev,
1046 			    "initialize iwarp failed, code %d\n", ret);
1047 	}
1048 #endif
1049 	return;
1050 
1051 fail:
1052 	iflib_init_failed(ctx);
1053 }
1054 
1055 void
1056 ixl_if_stop(if_ctx_t ctx)
1057 {
1058 	struct ixl_pf *pf = iflib_get_softc(ctx);
1059 	if_t ifp = iflib_get_ifp(ctx);
1060 	struct ixl_vsi *vsi = &pf->vsi;
1061 
1062 	INIT_DEBUGOUT("ixl_if_stop: begin\n");
1063 
1064 	ixl_led_restore(pf);
1065 	if (IXL_PF_IN_RECOVERY_MODE(pf))
1066 		return;
1067 
1068 	// TODO: This may need to be reworked
1069 #ifdef IXL_IW
1070 	/* Stop iWARP device */
1071 	if (ixl_enable_iwarp && pf->iw_enabled)
1072 		ixl_iw_pf_stop(pf);
1073 #endif
1074 
1075 	ixl_disable_rings_intr(vsi);
1076 	ixl_disable_rings(pf, vsi, &pf->qtag);
1077 
1078 	/*
1079 	 * Don't set link state if only reconfiguring
1080 	 * e.g. on MTU change.
1081 	 */
1082 	if ((if_getflags(ifp) & IFF_UP) == 0 &&
1083 	    !ixl_test_state(&pf->state, IXL_STATE_LINK_ACTIVE_ON_DOWN))
1084 		ixl_set_link(pf, false);
1085 }
1086 
1087 #define IXL_PHY_DEBUG_ALL					\
1088 	(I40E_AQ_PHY_DEBUG_DISABLE_LINK_FW |			\
1089 	 I40E_AQ_PHY_DEBUG_DISABLE_ALL_LINK_FW)
1090 
1091 static bool
1092 ixl_phy_controls_leds(const struct i40e_hw *hw)
1093 {
1094 
1095 	/* These external 10GBASE-T PHYs own the identification LED. */
1096 	return (hw->device_id == I40E_DEV_ID_10G_BASE_T ||
1097 	    hw->device_id == I40E_DEV_ID_10G_BASE_T4);
1098 }
1099 
1100 static void
1101 ixl_if_led_func(if_ctx_t ctx, int onoff)
1102 {
1103 	struct ixl_pf *pf;
1104 	struct i40e_hw *hw;
1105 	enum i40e_status_code status;
1106 	u16 phy_status;
1107 
1108 	pf = iflib_get_softc(ctx);
1109 	hw = &pf->hw;
1110 	if (!onoff) {
1111 		ixl_led_restore(pf);
1112 		return;
1113 	}
1114 	if (pf->led_active)
1115 		return;
1116 
1117 	pf->led_phy_controlled = ixl_phy_controls_leds(hw);
1118 	if (!pf->led_phy_controlled) {
1119 		pf->led_status = i40e_led_get(hw);
1120 		pf->led_active = true;
1121 		i40e_led_set(hw, 0xf, false);
1122 		return;
1123 	}
1124 
1125 	if ((hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE) == 0)
1126 		(void)i40e_aq_set_phy_debug(hw, IXL_PHY_DEBUG_ALL, NULL);
1127 	pf->led_phy_addr = I40E_PHY_LED_PROV_REG_1;
1128 	status = i40e_led_get_phy(hw, &pf->led_phy_addr, &phy_status);
1129 	if (status != I40E_SUCCESS) {
1130 		if ((hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE) == 0)
1131 			(void)i40e_aq_set_phy_debug(hw, 0, NULL);
1132 		return;
1133 	}
1134 	pf->led_status = phy_status;
1135 	pf->led_active = true;
1136 	status = i40e_led_set_phy(hw, true, pf->led_phy_addr, 0);
1137 	if (status != I40E_SUCCESS)
1138 		ixl_led_restore(pf);
1139 }
1140 
1141 void
1142 ixl_led_restore(struct ixl_pf *pf)
1143 {
1144 	struct i40e_hw *hw;
1145 
1146 	if (!pf->led_active)
1147 		return;
1148 
1149 	hw = &pf->hw;
1150 	if (pf->led_phy_controlled) {
1151 		(void)i40e_led_set_phy(hw, false, pf->led_phy_addr,
1152 		    pf->led_status | I40E_PHY_LED_MODE_ORIG);
1153 		if ((hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE) == 0)
1154 			(void)i40e_aq_set_phy_debug(hw, 0, NULL);
1155 	} else {
1156 		i40e_led_set(hw, pf->led_status, false);
1157 	}
1158 	pf->led_active = false;
1159 }
1160 
1161 static int
1162 ixl_if_msix_intr_assign(if_ctx_t ctx, int msix)
1163 {
1164 	struct ixl_pf *pf = iflib_get_softc(ctx);
1165 	struct ixl_vsi *vsi = &pf->vsi;
1166 	struct ixl_rx_queue *rx_que = vsi->rx_queues;
1167 	struct ixl_tx_queue *tx_que = vsi->tx_queues;
1168 	int err, i, rid, vector = 0;
1169 	char buf[16];
1170 
1171 	MPASS(vsi->shared->isc_nrxqsets > 0);
1172 	MPASS(vsi->shared->isc_ntxqsets > 0);
1173 
1174 	/* Admin Que must use vector 0*/
1175 	rid = vector + 1;
1176 	err = iflib_irq_alloc_generic(ctx, &vsi->irq, rid, IFLIB_INTR_ADMIN,
1177 	    ixl_msix_adminq, pf, 0, "aq");
1178 	if (err) {
1179 		iflib_irq_free(ctx, &vsi->irq);
1180 		device_printf(iflib_get_dev(ctx),
1181 		    "Failed to register Admin Que handler");
1182 		return (err);
1183 	}
1184 
1185 #ifdef PCI_IOV
1186 	/* Create soft IRQ for handling VFLRs */
1187 	iflib_softirq_alloc_generic(ctx, NULL, IFLIB_INTR_IOV, pf, 0, "iov");
1188 #endif
1189 
1190 	/* Now set up the stations */
1191 	for (i = 0, vector = 1; i < vsi->shared->isc_nrxqsets; i++, vector++, rx_que++) {
1192 		rid = vector + 1;
1193 
1194 		snprintf(buf, sizeof(buf), "rxq%d", i);
1195 		err = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
1196 		    IFLIB_INTR_RXTX, ixl_msix_que, rx_que, rx_que->rxr.me, buf);
1197 		/* XXX: Does the driver work as expected if there are fewer num_rx_queues than
1198 		 * what's expected in the iflib context? */
1199 		if (err) {
1200 			device_printf(iflib_get_dev(ctx),
1201 			    "Failed to allocate queue RX int vector %d, err: %d\n", i, err);
1202 			vsi->num_rx_queues = i + 1;
1203 			goto fail;
1204 		}
1205 		rx_que->msix = vector;
1206 	}
1207 
1208 	bzero(buf, sizeof(buf));
1209 
1210 	for (i = 0; i < vsi->shared->isc_ntxqsets; i++, tx_que++) {
1211 		snprintf(buf, sizeof(buf), "txq%d", i);
1212 		iflib_softirq_alloc_generic(ctx,
1213 		    &vsi->rx_queues[i % vsi->shared->isc_nrxqsets].que_irq,
1214 		    IFLIB_INTR_TX, tx_que, tx_que->txr.me, buf);
1215 
1216 		/* TODO: Maybe call a strategy function for this to figure out which
1217 		* interrupts to map Tx queues to. I don't know if there's an immediately
1218 		* better way than this other than a user-supplied map, though. */
1219 		tx_que->msix = (i % vsi->shared->isc_nrxqsets) + 1;
1220 	}
1221 
1222 	return (0);
1223 fail:
1224 	iflib_irq_free(ctx, &vsi->irq);
1225 	rx_que = vsi->rx_queues;
1226 	for (int i = 0; i < vsi->num_rx_queues; i++, rx_que++)
1227 		iflib_irq_free(ctx, &rx_que->que_irq);
1228 	return (err);
1229 }
1230 
1231 /*
1232  * Enable all interrupts
1233  *
1234  * Called in:
1235  * iflib_init_locked, after ixl_if_init()
1236  */
1237 static void
1238 ixl_if_enable_intr(if_ctx_t ctx)
1239 {
1240 	struct ixl_pf *pf = iflib_get_softc(ctx);
1241 	struct ixl_vsi *vsi = &pf->vsi;
1242 	struct i40e_hw		*hw = vsi->hw;
1243 	struct ixl_rx_queue	*rx_que = vsi->rx_queues;
1244 
1245 	ixl_enable_intr0(hw);
1246 	/* Enable queue interrupts */
1247 	if (vsi->shared->isc_intr == IFLIB_INTR_MSIX) {
1248 		for (int i = 0; i < vsi->num_rx_queues; i++, rx_que++)
1249 			ixl_enable_queue(hw, rx_que->rxr.me);
1250 	} else {
1251 		/*
1252 		 * Set PFINT_LNKLST0 FIRSTQ_INDX to 0x0 to enable
1253 		 * triggering interrupts by queues.
1254 		 */
1255 		wr32(hw, I40E_PFINT_LNKLST0, 0x0);
1256 	}
1257 }
1258 
1259 /*
1260  * Disable queue interrupts
1261  *
1262  * Other interrupt causes need to remain active.
1263  */
1264 static void
1265 ixl_if_disable_intr(if_ctx_t ctx)
1266 {
1267 	struct ixl_pf *pf = iflib_get_softc(ctx);
1268 	struct ixl_vsi *vsi = &pf->vsi;
1269 	struct i40e_hw		*hw = vsi->hw;
1270 	struct ixl_rx_queue	*rx_que = vsi->rx_queues;
1271 
1272 	if (vsi->shared->isc_intr == IFLIB_INTR_MSIX) {
1273 		for (int i = 0; i < vsi->num_rx_queues; i++, rx_que++)
1274 			ixl_disable_queue(hw, rx_que->rxr.me);
1275 	} else {
1276 		/*
1277 		 * Set PFINT_LNKLST0 FIRSTQ_INDX to End of List (0x7FF)
1278 		 * to stop queues from triggering interrupts.
1279 		 */
1280 		wr32(hw, I40E_PFINT_LNKLST0, IXL_QUEUE_EOL);
1281 	}
1282 }
1283 
1284 static int
1285 ixl_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
1286 {
1287 	struct ixl_pf *pf = iflib_get_softc(ctx);
1288 	struct ixl_vsi *vsi = &pf->vsi;
1289 	struct i40e_hw		*hw = vsi->hw;
1290 	struct ixl_rx_queue	*rx_que = &vsi->rx_queues[rxqid];
1291 
1292 	ixl_enable_queue(hw, rx_que->msix - 1);
1293 	return (0);
1294 }
1295 
1296 static int
1297 ixl_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
1298 {
1299 	struct ixl_pf *pf = iflib_get_softc(ctx);
1300 	struct ixl_vsi *vsi = &pf->vsi;
1301 	struct i40e_hw *hw = vsi->hw;
1302 	struct ixl_tx_queue *tx_que = &vsi->tx_queues[txqid];
1303 
1304 	ixl_enable_queue(hw, tx_que->msix - 1);
1305 	return (0);
1306 }
1307 
1308 static int
1309 ixl_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets)
1310 {
1311 	struct ixl_pf *pf = iflib_get_softc(ctx);
1312 	struct ixl_vsi *vsi = &pf->vsi;
1313 	if_softc_ctx_t scctx = vsi->shared;
1314 	struct ixl_tx_queue *que;
1315 	int i, j, error = 0;
1316 
1317 	MPASS(scctx->isc_ntxqsets > 0);
1318 	MPASS(ntxqs == 1);
1319 	MPASS(scctx->isc_ntxqsets == ntxqsets);
1320 
1321 	/* Allocate queue structure memory */
1322 	if (!(vsi->tx_queues =
1323 	    (struct ixl_tx_queue *) malloc(sizeof(struct ixl_tx_queue) *ntxqsets, M_IXL, M_NOWAIT | M_ZERO))) {
1324 		device_printf(iflib_get_dev(ctx), "Unable to allocate TX ring memory\n");
1325 		return (ENOMEM);
1326 	}
1327 
1328 	for (i = 0, que = vsi->tx_queues; i < ntxqsets; i++, que++) {
1329 		struct tx_ring *txr = &que->txr;
1330 
1331 		txr->me = i;
1332 		que->vsi = vsi;
1333 
1334 		if (!vsi->enable_head_writeback) {
1335 			/* Allocate report status array */
1336 			if (!(txr->tx_rsq = malloc(sizeof(qidx_t) * scctx->isc_ntxd[0], M_IXL, M_NOWAIT))) {
1337 				device_printf(iflib_get_dev(ctx), "failed to allocate tx_rsq memory\n");
1338 				error = ENOMEM;
1339 				goto fail;
1340 			}
1341 			/* Init report status array */
1342 			for (j = 0; j < scctx->isc_ntxd[0]; j++)
1343 				txr->tx_rsq[j] = QIDX_INVALID;
1344 		}
1345 		/* get the virtual and physical address of the hardware queues */
1346 		txr->tail = I40E_QTX_TAIL(txr->me);
1347 		txr->tx_base = (struct i40e_tx_desc *)vaddrs[i * ntxqs];
1348 		txr->tx_paddr = paddrs[i * ntxqs];
1349 		txr->que = que;
1350 	}
1351 
1352 	return (0);
1353 fail:
1354 	ixl_if_queues_free(ctx);
1355 	return (error);
1356 }
1357 
1358 static int
1359 ixl_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nrxqs, int nrxqsets)
1360 {
1361 	struct ixl_pf *pf = iflib_get_softc(ctx);
1362 	struct ixl_vsi *vsi = &pf->vsi;
1363 	struct ixl_rx_queue *que;
1364 	int i, error = 0;
1365 
1366 #ifdef INVARIANTS
1367 	if_softc_ctx_t scctx = vsi->shared;
1368 	MPASS(scctx->isc_nrxqsets > 0);
1369 	MPASS(nrxqs == 1);
1370 	MPASS(scctx->isc_nrxqsets == nrxqsets);
1371 #endif
1372 
1373 	/* Allocate queue structure memory */
1374 	if (!(vsi->rx_queues =
1375 	    (struct ixl_rx_queue *) malloc(sizeof(struct ixl_rx_queue) *
1376 	    nrxqsets, M_IXL, M_NOWAIT | M_ZERO))) {
1377 		device_printf(iflib_get_dev(ctx), "Unable to allocate RX ring memory\n");
1378 		error = ENOMEM;
1379 		goto fail;
1380 	}
1381 
1382 	for (i = 0, que = vsi->rx_queues; i < nrxqsets; i++, que++) {
1383 		struct rx_ring *rxr = &que->rxr;
1384 
1385 		rxr->me = i;
1386 		que->vsi = vsi;
1387 
1388 		/* get the virtual and physical address of the hardware queues */
1389 		rxr->tail = I40E_QRX_TAIL(rxr->me);
1390 		rxr->rx_base = (union i40e_rx_desc *)vaddrs[i * nrxqs];
1391 		rxr->rx_paddr = paddrs[i * nrxqs];
1392 		rxr->que = que;
1393 	}
1394 
1395 	return (0);
1396 fail:
1397 	ixl_if_queues_free(ctx);
1398 	return (error);
1399 }
1400 
1401 static void
1402 ixl_if_queues_free(if_ctx_t ctx)
1403 {
1404 	struct ixl_pf *pf = iflib_get_softc(ctx);
1405 	struct ixl_vsi *vsi = &pf->vsi;
1406 
1407 	if (vsi->tx_queues != NULL && !vsi->enable_head_writeback) {
1408 		struct ixl_tx_queue *que;
1409 		int i = 0;
1410 
1411 		for (i = 0, que = vsi->tx_queues; i < vsi->num_tx_queues; i++, que++) {
1412 			struct tx_ring *txr = &que->txr;
1413 			if (txr->tx_rsq != NULL) {
1414 				free(txr->tx_rsq, M_IXL);
1415 				txr->tx_rsq = NULL;
1416 			}
1417 		}
1418 	}
1419 
1420 	if (vsi->tx_queues != NULL) {
1421 		free(vsi->tx_queues, M_IXL);
1422 		vsi->tx_queues = NULL;
1423 	}
1424 	if (vsi->rx_queues != NULL) {
1425 		free(vsi->rx_queues, M_IXL);
1426 		vsi->rx_queues = NULL;
1427 	}
1428 
1429 	if (!IXL_PF_IN_RECOVERY_MODE(pf))
1430 		sysctl_ctx_free(&vsi->sysctl_ctx);
1431 }
1432 
1433 void
1434 ixl_update_link_status(struct ixl_pf *pf)
1435 {
1436 	struct ixl_vsi *vsi = &pf->vsi;
1437 	struct i40e_hw *hw = &pf->hw;
1438 	u64 baudrate;
1439 
1440 	if (pf->link_up) {
1441 		if (vsi->link_active == FALSE) {
1442 			vsi->link_active = TRUE;
1443 			baudrate = ixl_max_aq_speed_to_value(hw->phy.link_info.link_speed);
1444 			iflib_link_state_change(vsi->ctx, LINK_STATE_UP, baudrate);
1445 			ixl_link_up_msg(pf);
1446 #ifdef PCI_IOV
1447 			ixl_broadcast_link_state(pf);
1448 #endif
1449 		}
1450 	} else { /* Link down */
1451 		if (vsi->link_active == TRUE) {
1452 			vsi->link_active = FALSE;
1453 			iflib_link_state_change(vsi->ctx, LINK_STATE_DOWN, 0);
1454 #ifdef PCI_IOV
1455 			ixl_broadcast_link_state(pf);
1456 #endif
1457 		}
1458 	}
1459 }
1460 
1461 static void
1462 ixl_handle_lan_overflow_event(struct ixl_pf *pf, struct i40e_arq_event_info *e)
1463 {
1464 	device_t dev = pf->dev;
1465 	u32 rxq_idx, qtx_ctl;
1466 
1467 	rxq_idx = (e->desc.params.external.param0 & I40E_PRTDCB_RUPTQ_RXQNUM_MASK) >>
1468 	    I40E_PRTDCB_RUPTQ_RXQNUM_SHIFT;
1469 	qtx_ctl = e->desc.params.external.param1;
1470 
1471 	device_printf(dev, "LAN overflow event: global rxq_idx %d\n", rxq_idx);
1472 	device_printf(dev, "LAN overflow event: QTX_CTL 0x%08x\n", qtx_ctl);
1473 }
1474 
1475 static int
1476 ixl_process_adminq(struct ixl_pf *pf, u16 *pending)
1477 {
1478 	enum i40e_status_code status = I40E_SUCCESS;
1479 	struct i40e_arq_event_info event;
1480 	struct i40e_hw *hw = &pf->hw;
1481 	device_t dev = pf->dev;
1482 	u16 opcode;
1483 	u32 loop = 0, reg;
1484 
1485 	event.buf_len = IXL_AQ_BUF_SZ;
1486 	event.msg_buf = malloc(event.buf_len, M_IXL, M_NOWAIT | M_ZERO);
1487 	if (!event.msg_buf) {
1488 		device_printf(dev, "%s: Unable to allocate memory for Admin"
1489 		    " Queue event!\n", __func__);
1490 		return (ENOMEM);
1491 	}
1492 
1493 	/* clean and process any events */
1494 	do {
1495 		status = i40e_clean_arq_element(hw, &event, pending);
1496 		if (status)
1497 			break;
1498 		opcode = LE16_TO_CPU(event.desc.opcode);
1499 		ixl_dbg(pf, IXL_DBG_AQ,
1500 		    "Admin Queue event: %#06x\n", opcode);
1501 		switch (opcode) {
1502 		case i40e_aqc_opc_get_link_status:
1503 			ixl_link_event(pf, &event);
1504 			break;
1505 		case i40e_aqc_opc_send_msg_to_pf:
1506 #ifdef PCI_IOV
1507 			ixl_handle_vf_msg(pf, &event);
1508 #endif
1509 			break;
1510 		/*
1511 		 * This should only occur on no-drop queues, which
1512 		 * aren't currently configured.
1513 		 */
1514 		case i40e_aqc_opc_event_lan_overflow:
1515 			ixl_handle_lan_overflow_event(pf, &event);
1516 			break;
1517 		default:
1518 			break;
1519 		}
1520 	} while (*pending && (loop++ < IXL_ADM_LIMIT));
1521 
1522 	free(event.msg_buf, M_IXL);
1523 
1524 	/* Re-enable admin queue interrupt cause */
1525 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1526 	reg |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
1527 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1528 
1529 	return (status);
1530 }
1531 
1532 static void
1533 ixl_if_update_admin_status(if_ctx_t ctx)
1534 {
1535 	struct ixl_pf	*pf = iflib_get_softc(ctx);
1536 	struct i40e_hw	*hw = &pf->hw;
1537 	u16		pending;
1538 
1539 	if (IXL_PF_IS_RESETTING(pf))
1540 		ixl_handle_empr_reset(pf);
1541 
1542 	/*
1543 	 * Admin Queue is shut down while handling reset.
1544 	 * Don't proceed if it hasn't been re-initialized
1545 	 * e.g due to an issue with new FW.
1546 	 */
1547 	if (!i40e_check_asq_alive(&pf->hw))
1548 		return;
1549 
1550 	if (ixl_test_state(&pf->state, IXL_STATE_MDD_PENDING))
1551 		ixl_handle_mdd_event(pf);
1552 
1553 	ixl_process_adminq(pf, &pending);
1554 	ixl_update_link_status(pf);
1555 
1556 	/*
1557 	 * If there are still messages to process, reschedule ourselves.
1558 	 * Otherwise, re-enable our interrupt and go to sleep.
1559 	 */
1560 	if (pending > 0)
1561 		iflib_admin_intr_deferred(ctx);
1562 	else
1563 		ixl_enable_intr0(hw);
1564 }
1565 
1566 static void
1567 ixl_if_multi_set(if_ctx_t ctx)
1568 {
1569 	struct ixl_pf *pf = iflib_get_softc(ctx);
1570 	struct ixl_vsi *vsi = &pf->vsi;
1571 	struct i40e_hw *hw = vsi->hw;
1572 	enum i40e_status_code status;
1573 	int mcnt;
1574 	if_t ifp = iflib_get_ifp(ctx);
1575 
1576 	IOCTL_DEBUGOUT("ixl_if_multi_set: begin");
1577 
1578 	/* Delete filters for removed multicast addresses */
1579 	ixl_del_multi(vsi, false);
1580 
1581 	mcnt = min(if_llmaddr_count(ifp), MAX_MULTICAST_ADDR);
1582 	if (__predict_false(mcnt == MAX_MULTICAST_ADDR)) {
1583 		/* Check if promisc mode is already enabled, if yes return */
1584 		if (vsi->flags & IXL_FLAGS_MC_PROMISC)
1585 			return;
1586 
1587 		status = i40e_aq_set_vsi_multicast_promiscuous(hw,
1588 		    vsi->seid, TRUE, NULL);
1589 		if (status != I40E_SUCCESS)
1590 			if_printf(ifp, "Failed to enable multicast promiscuous "
1591 			    "mode, status: %s\n", i40e_stat_str(hw, status));
1592 		else {
1593 			if_printf(ifp, "Enabled multicast promiscuous mode\n");
1594 
1595 			/* Set the flag to track promiscuous mode */
1596 			vsi->flags |= IXL_FLAGS_MC_PROMISC;
1597 		}
1598 		/* Delete all existing MC filters */
1599 		ixl_del_multi(vsi, true);
1600 		return;
1601 	}
1602 
1603 	ixl_add_multi(vsi);
1604 	IOCTL_DEBUGOUT("ixl_if_multi_set: end");
1605 }
1606 
1607 static int
1608 ixl_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
1609 {
1610 	struct ixl_pf *pf = iflib_get_softc(ctx);
1611 	struct ixl_vsi *vsi = &pf->vsi;
1612 
1613 	IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
1614 	if (mtu > IXL_MAX_FRAME - ETHER_HDR_LEN - ETHER_CRC_LEN -
1615 		ETHER_VLAN_ENCAP_LEN)
1616 		return (EINVAL);
1617 
1618 	vsi->shared->isc_max_frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1619 		ETHER_VLAN_ENCAP_LEN;
1620 
1621 	return (0);
1622 }
1623 
1624 static void
1625 ixl_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
1626 {
1627 	struct ixl_pf *pf = iflib_get_softc(ctx);
1628 	struct i40e_hw  *hw = &pf->hw;
1629 
1630 	INIT_DEBUGOUT("ixl_media_status: begin");
1631 
1632 	ifmr->ifm_status = IFM_AVALID;
1633 	ifmr->ifm_active = IFM_ETHER;
1634 
1635 	if (!pf->link_up) {
1636 		return;
1637 	}
1638 
1639 	ifmr->ifm_status |= IFM_ACTIVE;
1640 	/* Hardware is always full-duplex */
1641 	ifmr->ifm_active |= IFM_FDX;
1642 
1643 	switch (hw->phy.link_info.phy_type) {
1644 		/* 100 M */
1645 		case I40E_PHY_TYPE_100BASE_TX:
1646 			ifmr->ifm_active |= IFM_100_TX;
1647 			break;
1648 		/* 1 G */
1649 		case I40E_PHY_TYPE_1000BASE_T:
1650 			ifmr->ifm_active |= IFM_1000_T;
1651 			break;
1652 		case I40E_PHY_TYPE_1000BASE_SX:
1653 			ifmr->ifm_active |= IFM_1000_SX;
1654 			break;
1655 		case I40E_PHY_TYPE_1000BASE_LX:
1656 			ifmr->ifm_active |= IFM_1000_LX;
1657 			break;
1658 		case I40E_PHY_TYPE_1000BASE_T_OPTICAL:
1659 			ifmr->ifm_active |= IFM_1000_T;
1660 			break;
1661 		/* 2.5 G */
1662 		case I40E_PHY_TYPE_2_5GBASE_T_LINK_STATUS:
1663 			ifmr->ifm_active |= IFM_2500_T;
1664 			break;
1665 		/* 5 G */
1666 		case I40E_PHY_TYPE_5GBASE_T_LINK_STATUS:
1667 			ifmr->ifm_active |= IFM_5000_T;
1668 			break;
1669 		/* 10 G */
1670 		case I40E_PHY_TYPE_10GBASE_SFPP_CU:
1671 			ifmr->ifm_active |= IFM_10G_TWINAX;
1672 			break;
1673 		case I40E_PHY_TYPE_10GBASE_SR:
1674 			ifmr->ifm_active |= IFM_10G_SR;
1675 			break;
1676 		case I40E_PHY_TYPE_10GBASE_LR:
1677 			ifmr->ifm_active |= IFM_10G_LR;
1678 			break;
1679 		case I40E_PHY_TYPE_10GBASE_T:
1680 			ifmr->ifm_active |= IFM_10G_T;
1681 			break;
1682 		case I40E_PHY_TYPE_XAUI:
1683 		case I40E_PHY_TYPE_XFI:
1684 			ifmr->ifm_active |= IFM_10G_TWINAX;
1685 			break;
1686 		case I40E_PHY_TYPE_10GBASE_AOC:
1687 			ifmr->ifm_active |= IFM_10G_AOC;
1688 			break;
1689 		/* 25 G */
1690 		case I40E_PHY_TYPE_25GBASE_KR:
1691 			ifmr->ifm_active |= IFM_25G_KR;
1692 			break;
1693 		case I40E_PHY_TYPE_25GBASE_CR:
1694 			ifmr->ifm_active |= IFM_25G_CR;
1695 			break;
1696 		case I40E_PHY_TYPE_25GBASE_SR:
1697 			ifmr->ifm_active |= IFM_25G_SR;
1698 			break;
1699 		case I40E_PHY_TYPE_25GBASE_LR:
1700 			ifmr->ifm_active |= IFM_25G_LR;
1701 			break;
1702 		case I40E_PHY_TYPE_25GBASE_AOC:
1703 			ifmr->ifm_active |= IFM_25G_AOC;
1704 			break;
1705 		case I40E_PHY_TYPE_25GBASE_ACC:
1706 			ifmr->ifm_active |= IFM_25G_ACC;
1707 			break;
1708 		/* 40 G */
1709 		case I40E_PHY_TYPE_40GBASE_CR4:
1710 		case I40E_PHY_TYPE_40GBASE_CR4_CU:
1711 			ifmr->ifm_active |= IFM_40G_CR4;
1712 			break;
1713 		case I40E_PHY_TYPE_40GBASE_SR4:
1714 			ifmr->ifm_active |= IFM_40G_SR4;
1715 			break;
1716 		case I40E_PHY_TYPE_40GBASE_LR4:
1717 			ifmr->ifm_active |= IFM_40G_LR4;
1718 			break;
1719 		case I40E_PHY_TYPE_XLAUI:
1720 			ifmr->ifm_active |= IFM_OTHER;
1721 			break;
1722 		case I40E_PHY_TYPE_1000BASE_KX:
1723 			ifmr->ifm_active |= IFM_1000_KX;
1724 			break;
1725 		case I40E_PHY_TYPE_SGMII:
1726 			ifmr->ifm_active |= IFM_1000_SGMII;
1727 			break;
1728 		/* ERJ: What's the difference between these? */
1729 		case I40E_PHY_TYPE_10GBASE_CR1_CU:
1730 		case I40E_PHY_TYPE_10GBASE_CR1:
1731 			ifmr->ifm_active |= IFM_10G_CR1;
1732 			break;
1733 		case I40E_PHY_TYPE_10GBASE_KX4:
1734 			ifmr->ifm_active |= IFM_10G_KX4;
1735 			break;
1736 		case I40E_PHY_TYPE_10GBASE_KR:
1737 			ifmr->ifm_active |= IFM_10G_KR;
1738 			break;
1739 		case I40E_PHY_TYPE_SFI:
1740 			ifmr->ifm_active |= IFM_10G_SFI;
1741 			break;
1742 		/* Our single 20G media type */
1743 		case I40E_PHY_TYPE_20GBASE_KR2:
1744 			ifmr->ifm_active |= IFM_20G_KR2;
1745 			break;
1746 		case I40E_PHY_TYPE_40GBASE_KR4:
1747 			ifmr->ifm_active |= IFM_40G_KR4;
1748 			break;
1749 		case I40E_PHY_TYPE_XLPPI:
1750 		case I40E_PHY_TYPE_40GBASE_AOC:
1751 			ifmr->ifm_active |= IFM_40G_XLPPI;
1752 			break;
1753 		/* Unknown to driver */
1754 		default:
1755 			ifmr->ifm_active |= IFM_UNKNOWN;
1756 			break;
1757 	}
1758 	/* Report flow control status as well */
1759 	if (hw->phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
1760 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
1761 	if (hw->phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
1762 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
1763 }
1764 
1765 static int
1766 ixl_if_media_change(if_ctx_t ctx)
1767 {
1768 	struct ifmedia *ifm = iflib_get_media(ctx);
1769 
1770 	INIT_DEBUGOUT("ixl_media_change: begin");
1771 
1772 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1773 		return (EINVAL);
1774 
1775 	if_printf(iflib_get_ifp(ctx), "Media change is not supported.\n");
1776 	return (ENODEV);
1777 }
1778 
1779 static int
1780 ixl_if_promisc_set(if_ctx_t ctx, int flags)
1781 {
1782 	struct ixl_pf *pf = iflib_get_softc(ctx);
1783 	struct ixl_vsi *vsi = &pf->vsi;
1784 	if_t ifp = iflib_get_ifp(ctx);
1785 	struct i40e_hw	*hw = vsi->hw;
1786 	int		err;
1787 	bool		uni = FALSE, multi = FALSE;
1788 
1789 	if (flags & IFF_PROMISC)
1790 		uni = multi = TRUE;
1791 	else if (flags & IFF_ALLMULTI || if_llmaddr_count(ifp) >=
1792 	    MAX_MULTICAST_ADDR)
1793 		multi = TRUE;
1794 
1795 	err = i40e_aq_set_vsi_unicast_promiscuous(hw,
1796 	    vsi->seid, uni, NULL, true);
1797 	if (err)
1798 		return (err);
1799 	err = i40e_aq_set_vsi_multicast_promiscuous(hw,
1800 	    vsi->seid, multi, NULL);
1801 
1802 	/* Update the multicast promiscuous flag based on the new state */
1803 	if (multi)
1804 		vsi->flags |= IXL_FLAGS_MC_PROMISC;
1805 	else
1806 		vsi->flags &= ~IXL_FLAGS_MC_PROMISC;
1807 
1808 	return (err);
1809 }
1810 
1811 static void
1812 ixl_if_timer(if_ctx_t ctx, uint16_t qid)
1813 {
1814 	struct ixl_pf *pf = iflib_get_softc(ctx);
1815 
1816 	if (qid != 0)
1817 		return;
1818 
1819 	ixl_update_stats_counters(pf);
1820 }
1821 
1822 static void
1823 ixl_if_vlan_register(if_ctx_t ctx, u16 vtag)
1824 {
1825 	struct ixl_pf *pf = iflib_get_softc(ctx);
1826 	struct ixl_vsi *vsi = &pf->vsi;
1827 	struct i40e_hw	*hw = vsi->hw;
1828 	if_t ifp = iflib_get_ifp(ctx);
1829 
1830 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
1831 		return;
1832 
1833 	/*
1834 	 * Keep track of registered VLANS to know what
1835 	 * filters have to be configured when VLAN_HWFILTER
1836 	 * capability is enabled.
1837 	 */
1838 	++vsi->num_vlans;
1839 	bit_set(vsi->vlans_map, vtag);
1840 
1841 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) == 0)
1842 		return;
1843 
1844 	if (vsi->num_vlans < IXL_MAX_VLAN_FILTERS)
1845 		ixl_add_filter(vsi, hw->mac.addr, vtag);
1846 	else if (vsi->num_vlans == IXL_MAX_VLAN_FILTERS) {
1847 		/*
1848 		 * There is not enough HW resources to add filters
1849 		 * for all registered VLANs. Re-configure filtering
1850 		 * to allow reception of all expected traffic.
1851 		 */
1852 		device_printf(vsi->dev,
1853 		    "Not enough HW filters for all VLANs. VLAN HW filtering disabled");
1854 		ixl_del_all_vlan_filters(vsi, hw->mac.addr);
1855 		ixl_add_filter(vsi, hw->mac.addr, IXL_VLAN_ANY);
1856 	}
1857 }
1858 
1859 static void
1860 ixl_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
1861 {
1862 	struct ixl_pf *pf = iflib_get_softc(ctx);
1863 	struct ixl_vsi *vsi = &pf->vsi;
1864 	struct i40e_hw	*hw = vsi->hw;
1865 	if_t ifp = iflib_get_ifp(ctx);
1866 
1867 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
1868 		return;
1869 
1870 	--vsi->num_vlans;
1871 	bit_clear(vsi->vlans_map, vtag);
1872 
1873 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) == 0)
1874 		return;
1875 
1876 	/* One filter is used for untagged frames */
1877 	if (vsi->num_vlans < IXL_MAX_VLAN_FILTERS - 1)
1878 		ixl_del_filter(vsi, hw->mac.addr, vtag);
1879 	else if (vsi->num_vlans == IXL_MAX_VLAN_FILTERS - 1) {
1880 		ixl_del_filter(vsi, hw->mac.addr, IXL_VLAN_ANY);
1881 		ixl_add_vlan_filters(vsi, hw->mac.addr);
1882 	}
1883 }
1884 
1885 static uint64_t
1886 ixl_if_get_counter(if_ctx_t ctx, ift_counter cnt)
1887 {
1888 	struct ixl_pf *pf = iflib_get_softc(ctx);
1889 	struct ixl_vsi *vsi = &pf->vsi;
1890 	if_t ifp = iflib_get_ifp(ctx);
1891 
1892 	switch (cnt) {
1893 	case IFCOUNTER_IPACKETS:
1894 		return (vsi->ipackets);
1895 	case IFCOUNTER_IERRORS:
1896 		return (vsi->ierrors);
1897 	case IFCOUNTER_OPACKETS:
1898 		return (vsi->opackets);
1899 	case IFCOUNTER_OERRORS:
1900 		return (if_get_counter_default(ifp, cnt) + vsi->oerrors);
1901 	case IFCOUNTER_COLLISIONS:
1902 		/* Collisions are by standard impossible in 40G/10G Ethernet */
1903 		return (0);
1904 	case IFCOUNTER_IBYTES:
1905 		return (vsi->ibytes);
1906 	case IFCOUNTER_OBYTES:
1907 		return (vsi->obytes);
1908 	case IFCOUNTER_IMCASTS:
1909 		return (vsi->imcasts);
1910 	case IFCOUNTER_OMCASTS:
1911 		return (vsi->omcasts);
1912 	case IFCOUNTER_IQDROPS:
1913 		return (vsi->iqdrops);
1914 	case IFCOUNTER_OQDROPS:
1915 		return (if_get_counter_default(ifp, cnt) + vsi->oqdrops);
1916 	case IFCOUNTER_NOPROTO:
1917 		return (vsi->noproto);
1918 	default:
1919 		return (if_get_counter_default(ifp, cnt));
1920 	}
1921 }
1922 
1923 #ifdef PCI_IOV
1924 static void
1925 ixl_if_vflr_handle(if_ctx_t ctx)
1926 {
1927 	struct ixl_pf *pf = iflib_get_softc(ctx);
1928 
1929 	ixl_handle_vflr(pf);
1930 }
1931 #endif
1932 
1933 static int
1934 ixl_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req)
1935 {
1936 	struct ixl_pf		*pf = iflib_get_softc(ctx);
1937 
1938 	if (pf->read_i2c_byte == NULL)
1939 		return (EINVAL);
1940 
1941 	for (int i = 0; i < req->len; i++)
1942 		if (pf->read_i2c_byte(pf, req->offset + i,
1943 		    req->dev_addr, &req->data[i]))
1944 			return (EIO);
1945 	return (0);
1946 }
1947 
1948 static int
1949 ixl_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data)
1950 {
1951 	struct ixl_pf *pf = iflib_get_softc(ctx);
1952 	struct ifdrv *ifd = (struct ifdrv *)data;
1953 	int error = 0;
1954 
1955 	/*
1956 	 * The iflib_if_ioctl forwards SIOCxDRVSPEC and SIOGPRIVATE_0 without
1957 	 * performing privilege checks. It is important that this function
1958 	 * perform the necessary checks for commands which should only be
1959 	 * executed by privileged threads.
1960 	 */
1961 
1962 	switch(command) {
1963 	case SIOCGDRVSPEC:
1964 	case SIOCSDRVSPEC:
1965 		/* NVM update command */
1966 		if (ifd->ifd_cmd == I40E_NVM_ACCESS) {
1967 			error = priv_check(curthread, PRIV_DRIVER);
1968 			if (error)
1969 				break;
1970 			error = ixl_handle_nvmupd_cmd(pf, ifd);
1971 		} else {
1972 			error = EINVAL;
1973 		}
1974 		break;
1975 	default:
1976 		error = EOPNOTSUPP;
1977 	}
1978 
1979 	return (error);
1980 }
1981 
1982 /* ixl_if_needs_restart - Tell iflib when the driver needs to be reinitialized
1983  * @ctx: iflib context
1984  * @event: event code to check
1985  *
1986  * Defaults to returning false for every event.
1987  *
1988  * @returns true if iflib needs to reinit the interface, false otherwise
1989  */
1990 static bool
1991 ixl_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
1992 {
1993 	switch (event) {
1994 	case IFLIB_RESTART_VLAN_CONFIG:
1995 	default:
1996 		return (false);
1997 	}
1998 }
1999 
2000 /*
2001  * Sanity check and save off tunable values.
2002  */
2003 static void
2004 ixl_save_pf_tunables(struct ixl_pf *pf)
2005 {
2006 	device_t dev = pf->dev;
2007 
2008 	/* Save tunable information */
2009 #ifdef IXL_DEBUG_FC
2010 	pf->enable_tx_fc_filter = ixl_enable_tx_fc_filter;
2011 #endif
2012 #ifdef IXL_DEBUG
2013 	pf->recovery_mode = ixl_debug_recovery_mode;
2014 #endif
2015 	pf->dbg_mask = ixl_core_debug_mask;
2016 	pf->hw.debug_mask = ixl_shared_debug_mask;
2017 	pf->vsi.enable_head_writeback = !!(ixl_enable_head_writeback);
2018 	pf->enable_vf_loopback = !!(ixl_enable_vf_loopback);
2019 	pf->mdd_auto_reset_vf = !!(ixl_mdd_auto_reset_vf);
2020 #if 0
2021 	pf->dynamic_rx_itr = ixl_dynamic_rx_itr;
2022 	pf->dynamic_tx_itr = ixl_dynamic_tx_itr;
2023 #endif
2024 
2025 	if (ixl_i2c_access_method > 3 || ixl_i2c_access_method < 0)
2026 		pf->i2c_access_method = 0;
2027 	else
2028 		pf->i2c_access_method = ixl_i2c_access_method;
2029 
2030 	if (ixl_tx_itr < 0 || ixl_tx_itr > IXL_MAX_ITR) {
2031 		device_printf(dev, "Invalid tx_itr value of %d set!\n",
2032 		    ixl_tx_itr);
2033 		device_printf(dev, "tx_itr must be between %d and %d, "
2034 		    "inclusive\n",
2035 		    0, IXL_MAX_ITR);
2036 		device_printf(dev, "Using default value of %d instead\n",
2037 		    IXL_ITR_4K);
2038 		pf->tx_itr = IXL_ITR_4K;
2039 	} else
2040 		pf->tx_itr = ixl_tx_itr;
2041 
2042 	if (ixl_rx_itr < 0 || ixl_rx_itr > IXL_MAX_ITR) {
2043 		device_printf(dev, "Invalid rx_itr value of %d set!\n",
2044 		    ixl_rx_itr);
2045 		device_printf(dev, "rx_itr must be between %d and %d, "
2046 		    "inclusive\n",
2047 		    0, IXL_MAX_ITR);
2048 		device_printf(dev, "Using default value of %d instead\n",
2049 		    IXL_ITR_8K);
2050 		pf->rx_itr = IXL_ITR_8K;
2051 	} else
2052 		pf->rx_itr = ixl_rx_itr;
2053 
2054 	pf->fc = -1;
2055 	if (ixl_flow_control != -1) {
2056 		if (ixl_flow_control < 0 || ixl_flow_control > 3) {
2057 			device_printf(dev,
2058 			    "Invalid flow_control value of %d set!\n",
2059 			    ixl_flow_control);
2060 			device_printf(dev,
2061 			    "flow_control must be between %d and %d, "
2062 			    "inclusive\n", 0, 3);
2063 			device_printf(dev,
2064 			    "Using default configuration instead\n");
2065 		} else
2066 			pf->fc = ixl_flow_control;
2067 	}
2068 }
2069