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