xref: /freebsd/sys/dev/iavf/if_iavf_iflib.c (revision 9aff62dee28239f84b4aa4a3429cf26dbbf770cc)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2024, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * @file if_iavf_iflib.c
34  * @brief iflib driver implementation
35  *
36  * Contains the main entry point for the iflib driver implementation. It
37  * implements the various ifdi driver methods, and sets up the module and
38  * driver values to load an iflib driver.
39  */
40 
41 #include "iavf_iflib.h"
42 #include "iavf_vc_common.h"
43 
44 #include "iavf_drv_info.h"
45 #include "iavf_sysctls_iflib.h"
46 
47 static const sbintime_t iavf_mbx_retry_delay[] = {
48 	250 * SBT_1MS,
49 	1 * SBT_1S,
50 	4 * SBT_1S,
51 	8 * SBT_1S,
52 };
53 
54 static const struct timeval iavf_mbx_log_interval = { 60, 0 };
55 
56 #define IAVF_MBX_RECOVERY_ASQ_RETRIES	10
57 #define IAVF_MBX_RECOVERY_VERSION_RETRIES	3
58 #define IAVF_MBX_RECOVERY_CONFIG_RETRIES	10
59 
60 /*********************************************************************
61  *  Function prototypes
62  *********************************************************************/
63 static void	 *iavf_register(device_t dev);
64 static int	 iavf_if_attach_pre(if_ctx_t ctx);
65 static int	 iavf_if_attach_post(if_ctx_t ctx);
66 static int	 iavf_if_detach(if_ctx_t ctx);
67 static int	 iavf_if_shutdown(if_ctx_t ctx);
68 static int	 iavf_if_suspend(if_ctx_t ctx);
69 static int	 iavf_if_resume(if_ctx_t ctx);
70 static int	 iavf_if_msix_intr_assign(if_ctx_t ctx, int msix);
71 static void	 iavf_if_enable_intr(if_ctx_t ctx);
72 static void	 iavf_if_disable_intr(if_ctx_t ctx);
73 static int	 iavf_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
74 static int	 iavf_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
75 static int	 iavf_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets);
76 static int	 iavf_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nqs, int nqsets);
77 static void	 iavf_if_queues_free(if_ctx_t ctx);
78 static void	 iavf_if_update_admin_status(if_ctx_t ctx);
79 static void	 iavf_if_multi_set(if_ctx_t ctx);
80 static int	 iavf_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
81 static void	 iavf_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
82 static int	 iavf_if_media_change(if_ctx_t ctx);
83 static int	 iavf_if_promisc_set(if_ctx_t ctx, int flags);
84 static void	 iavf_if_timer(if_ctx_t ctx, uint16_t qid);
85 static void	 iavf_if_vlan_register(if_ctx_t ctx, u16 vtag);
86 static void	 iavf_if_vlan_unregister(if_ctx_t ctx, u16 vtag);
87 static uint64_t	 iavf_if_get_counter(if_ctx_t ctx, ift_counter cnt);
88 static void	 iavf_if_init(if_ctx_t ctx);
89 static void	 iavf_if_stop(if_ctx_t ctx);
90 static bool	 iavf_if_needs_restart(if_ctx_t, enum iflib_restart_event);
91 
92 static void	iavf_mbx_lost(struct iavf_sc *);
93 static void	iavf_mbx_retry_detach(struct iavf_sc *);
94 static void	iavf_mbx_retry_failed(if_ctx_t);
95 static void	iavf_mbx_retry_prepare(struct iavf_sc *);
96 static void	iavf_mbx_retry_stop(struct iavf_sc *);
97 static void	iavf_mbx_retry_succeeded(struct iavf_sc *);
98 static int	iavf_reestablish_vc(struct iavf_sc *);
99 static void	iavf_replay_filters(struct iavf_sc *);
100 static int	iavf_wait_asq(struct iavf_sc *, u32);
101 
102 static int	iavf_allocate_pci_resources(struct iavf_sc *);
103 static void	iavf_free_pci_resources(struct iavf_sc *);
104 static void	iavf_setup_interface(struct iavf_sc *);
105 static void	iavf_add_device_sysctls(struct iavf_sc *);
106 static void	iavf_enable_queue_irq(struct iavf_hw *, int);
107 static void	iavf_disable_queue_irq(struct iavf_hw *, int);
108 static void	iavf_stop(struct iavf_sc *);
109 
110 static int	iavf_del_mac_filter(struct iavf_sc *sc, u8 *macaddr);
111 static int	iavf_msix_que(void *);
112 static int	iavf_msix_adminq(void *);
113 static void	iavf_configure_itr(struct iavf_sc *sc);
114 
115 static int	iavf_sysctl_queue_interrupt_table(SYSCTL_HANDLER_ARGS);
116 #ifdef IAVF_DEBUG
117 static int	iavf_sysctl_vf_reset(SYSCTL_HANDLER_ARGS);
118 static int	iavf_sysctl_vflr_reset(SYSCTL_HANDLER_ARGS);
119 #endif
120 
121 static enum iavf_status iavf_process_adminq(struct iavf_sc *, u16 *);
122 static void	iavf_vc_task(void *arg, int pending __unused);
123 static int	iavf_setup_vc_tq(struct iavf_sc *sc);
124 static int	iavf_vc_sleep_wait(struct iavf_sc *sc, u32 op);
125 
126 /*********************************************************************
127  *  FreeBSD Device Interface Entry Points
128  *********************************************************************/
129 
130 /**
131  * @var iavf_methods
132  * @brief device methods for the iavf driver
133  *
134  * Device method callbacks used to interact with the driver. For iflib this
135  * primarily resolves to the default iflib implementations.
136  */
137 static device_method_t iavf_methods[] = {
138 	/* Device interface */
139 	DEVMETHOD(device_register, iavf_register),
140 	DEVMETHOD(device_probe, iflib_device_probe),
141 	DEVMETHOD(device_attach, iflib_device_attach),
142 	DEVMETHOD(device_detach, iflib_device_detach),
143 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
144 	DEVMETHOD(device_suspend, iflib_device_suspend),
145 	DEVMETHOD(device_resume, iflib_device_resume),
146 	DEVMETHOD_END
147 };
148 
149 static driver_t iavf_driver = {
150 	"iavf", iavf_methods, sizeof(struct iavf_sc),
151 };
152 
153 DRIVER_MODULE(iavf, pci, iavf_driver, 0, 0);
154 MODULE_VERSION(iavf, 1);
155 
156 MODULE_DEPEND(iavf, pci, 1, 1, 1);
157 MODULE_DEPEND(iavf, ether, 1, 1, 1);
158 MODULE_DEPEND(iavf, iflib, 1, 1, 1);
159 
160 IFLIB_PNP_INFO(pci, iavf, iavf_vendor_info_array);
161 
162 /**
163  * @var M_IAVF
164  * @brief main iavf driver allocation type
165  *
166  * malloc(9) allocation type used by the majority of memory allocations in the
167  * iavf iflib driver.
168  */
169 MALLOC_DEFINE(M_IAVF, "iavf", "iavf driver allocations");
170 
171 static device_method_t iavf_if_methods[] = {
172 	DEVMETHOD(ifdi_attach_pre, iavf_if_attach_pre),
173 	DEVMETHOD(ifdi_attach_post, iavf_if_attach_post),
174 	DEVMETHOD(ifdi_detach, iavf_if_detach),
175 	DEVMETHOD(ifdi_shutdown, iavf_if_shutdown),
176 	DEVMETHOD(ifdi_suspend, iavf_if_suspend),
177 	DEVMETHOD(ifdi_resume, iavf_if_resume),
178 	DEVMETHOD(ifdi_init, iavf_if_init),
179 	DEVMETHOD(ifdi_stop, iavf_if_stop),
180 	DEVMETHOD(ifdi_msix_intr_assign, iavf_if_msix_intr_assign),
181 	DEVMETHOD(ifdi_intr_enable, iavf_if_enable_intr),
182 	DEVMETHOD(ifdi_intr_disable, iavf_if_disable_intr),
183 	DEVMETHOD(ifdi_rx_queue_intr_enable, iavf_if_rx_queue_intr_enable),
184 	DEVMETHOD(ifdi_tx_queue_intr_enable, iavf_if_tx_queue_intr_enable),
185 	DEVMETHOD(ifdi_tx_queues_alloc, iavf_if_tx_queues_alloc),
186 	DEVMETHOD(ifdi_rx_queues_alloc, iavf_if_rx_queues_alloc),
187 	DEVMETHOD(ifdi_queues_free, iavf_if_queues_free),
188 	DEVMETHOD(ifdi_update_admin_status, iavf_if_update_admin_status),
189 	DEVMETHOD(ifdi_multi_set, iavf_if_multi_set),
190 	DEVMETHOD(ifdi_mtu_set, iavf_if_mtu_set),
191 	DEVMETHOD(ifdi_media_status, iavf_if_media_status),
192 	DEVMETHOD(ifdi_media_change, iavf_if_media_change),
193 	DEVMETHOD(ifdi_promisc_set, iavf_if_promisc_set),
194 	DEVMETHOD(ifdi_timer, iavf_if_timer),
195 	DEVMETHOD(ifdi_vlan_register, iavf_if_vlan_register),
196 	DEVMETHOD(ifdi_vlan_unregister, iavf_if_vlan_unregister),
197 	DEVMETHOD(ifdi_get_counter, iavf_if_get_counter),
198 	DEVMETHOD(ifdi_needs_restart, iavf_if_needs_restart),
199 	DEVMETHOD_END
200 };
201 
202 static driver_t iavf_if_driver = {
203 	"iavf_if", iavf_if_methods, sizeof(struct iavf_sc)
204 };
205 
206 extern struct if_txrx iavf_txrx_hwb;
207 extern struct if_txrx iavf_txrx_dwb;
208 
209 static struct if_shared_ctx iavf_sctx = {
210 	.isc_magic = IFLIB_MAGIC,
211 	.isc_q_align = PAGE_SIZE,
212 	.isc_tx_maxsize = IAVF_MAX_FRAME,
213 	.isc_tx_maxsegsize = IAVF_MAX_FRAME,
214 	.isc_tso_maxsize = IAVF_TSO_SIZE + sizeof(struct ether_vlan_header),
215 	.isc_tso_maxsegsize = IAVF_MAX_DMA_SEG_SIZE,
216 	.isc_rx_maxsize = IAVF_MAX_FRAME,
217 	.isc_rx_nsegments = IAVF_MAX_RX_SEGS,
218 	.isc_rx_maxsegsize = IAVF_MAX_FRAME,
219 	.isc_nfl = 1,
220 	.isc_ntxqs = 1,
221 	.isc_nrxqs = 1,
222 
223 	.isc_admin_intrcnt = 1,
224 	.isc_vendor_info = iavf_vendor_info_array,
225 	.isc_driver_version = __DECONST(char *, iavf_driver_version),
226 	.isc_driver = &iavf_if_driver,
227 	.isc_flags = IFLIB_NEED_SCRATCH | IFLIB_NEED_ZERO_CSUM | IFLIB_TSO_INIT_IP | IFLIB_IS_VF,
228 
229 	.isc_nrxd_min = {IAVF_MIN_RING},
230 	.isc_ntxd_min = {IAVF_MIN_RING},
231 	.isc_nrxd_max = {IAVF_MAX_RING},
232 	.isc_ntxd_max = {IAVF_MAX_RING},
233 	.isc_nrxd_default = {IAVF_DEFAULT_RING},
234 	.isc_ntxd_default = {IAVF_DEFAULT_RING},
235 };
236 
237 /*** Functions ***/
238 
239 /**
240  * iavf_register - iflib callback to obtain the shared context pointer
241  * @dev: the device being registered
242  *
243  * Called when the driver is first being attached to the driver. This function
244  * is used by iflib to obtain a pointer to the shared context structure which
245  * describes the device features.
246  *
247  * @returns a pointer to the iavf shared context structure.
248  */
249 static void *
250 iavf_register(device_t dev __unused)
251 {
252 	return (&iavf_sctx);
253 }
254 
255 /**
256  * iavf_allocate_pci_resources - Allocate PCI resources
257  * @sc: the device private softc
258  *
259  * Allocate PCI resources used by the iflib driver.
260  *
261  * @returns zero or a non-zero error code on failure
262  */
263 static int
264 iavf_allocate_pci_resources(struct iavf_sc *sc)
265 {
266 	return iavf_allocate_pci_resources_common(sc);
267 }
268 
269 /**
270  * iavf_if_attach_pre - Begin attaching the device to the driver
271  * @ctx: the iflib context pointer
272  *
273  * Called by iflib to begin the attach process. Allocates resources and
274  * initializes the hardware for operation.
275  *
276  * @returns zero or a non-zero error code on failure.
277  */
278 static int
279 iavf_if_attach_pre(if_ctx_t ctx)
280 {
281 	device_t dev;
282 	struct iavf_sc *sc;
283 	struct iavf_hw *hw;
284 	struct iavf_vsi *vsi;
285 	if_softc_ctx_t scctx;
286 	int error = 0;
287 
288 	/* Setup pointers */
289 	dev = iflib_get_dev(ctx);
290 	sc = iavf_sc_from_ctx(ctx);
291 
292 	vsi = &sc->vsi;
293 	vsi->back = sc;
294 	sc->dev = sc->osdep.dev = dev;
295 	hw = &sc->hw;
296 
297 	vsi->dev = dev;
298 	vsi->hw = &sc->hw;
299 	vsi->num_vlans = 0;
300 	vsi->ctx = ctx;
301 	sc->media = iflib_get_media(ctx);
302 	vsi->ifp = iflib_get_ifp(ctx);
303 	vsi->shared = scctx = iflib_get_softc_ctx(ctx);
304 
305 	iavf_save_tunables(sc);
306 
307 	/* Setup VC mutex */
308 	snprintf(sc->vc_mtx_name, sizeof(sc->vc_mtx_name),
309 		 "%s:vc", device_get_nameunit(dev));
310 	mtx_init(&sc->vc_mtx, sc->vc_mtx_name, NULL, MTX_DEF);
311 
312 	/* Do PCI setup - map BAR0, etc */
313 	error = iavf_allocate_pci_resources(sc);
314 	if (error) {
315 		device_printf(dev, "%s: Allocation of PCI resources failed\n",
316 		    __func__);
317 		goto err_early;
318 	}
319 
320 	iavf_dbg_init(sc, "Allocated PCI resources and MSI-X vectors\n");
321 
322 	error = iavf_set_mac_type(hw);
323 	if (error) {
324 		device_printf(dev, "%s: set_mac_type failed: %d\n",
325 		    __func__, error);
326 		goto err_pci_res;
327 	}
328 
329 	error = iavf_reset_complete(hw);
330 	if (error) {
331 		device_printf(dev, "%s: Device is still being reset\n",
332 		    __func__);
333 		goto err_pci_res;
334 	}
335 
336 	iavf_dbg_init(sc, "VF Device is ready for configuration\n");
337 
338 	/* Sets up Admin Queue */
339 	error = iavf_setup_vc(sc);
340 	if (error) {
341 		device_printf(dev, "%s: Error setting up PF comms, %d\n",
342 		    __func__, error);
343 		goto err_pci_res;
344 	}
345 
346 	iavf_dbg_init(sc, "PF API version verified\n");
347 
348 	/* Need API version before sending reset message */
349 	error = iavf_reset(sc);
350 	if (error) {
351 		device_printf(dev, "VF reset failed; reload the driver\n");
352 		goto err_aq;
353 	}
354 
355 	iavf_dbg_init(sc, "VF reset complete\n");
356 
357 	/* Ask for VF config from PF */
358 	error = iavf_vf_config(sc);
359 	if (error) {
360 		device_printf(dev, "Error getting configuration from PF: %d\n",
361 		    error);
362 		goto err_aq;
363 	}
364 
365 	iavf_print_device_info(sc);
366 
367 	error = iavf_get_vsi_res_from_vf_res(sc);
368 	if (error)
369 		goto err_res_buf;
370 
371 	iavf_dbg_init(sc, "Resource Acquisition complete\n");
372 
373 	/* Setup taskqueue to service VC messages */
374 	error = iavf_setup_vc_tq(sc);
375 	if (error)
376 		goto err_vc_tq;
377 
378 	iavf_set_mac_addresses(sc);
379 	iflib_set_mac(ctx, hw->mac.addr);
380 
381 	/* Allocate filter lists */
382 	iavf_init_filters(sc);
383 
384 	/* Fill out more iflib parameters */
385 	scctx->isc_ntxqsets_max = scctx->isc_nrxqsets_max =
386 	    sc->vsi_res->num_queue_pairs;
387 	if (vsi->enable_head_writeback) {
388 		scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
389 		    * sizeof(struct iavf_tx_desc) + sizeof(u32), DBA_ALIGN);
390 		scctx->isc_txrx = &iavf_txrx_hwb;
391 	} else {
392 		scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
393 		    * sizeof(struct iavf_tx_desc), DBA_ALIGN);
394 		scctx->isc_txrx = &iavf_txrx_dwb;
395 	}
396 	scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0]
397 	    * sizeof(union iavf_32byte_rx_desc), DBA_ALIGN);
398 	scctx->isc_msix_bar = pci_msix_table_bar(dev);
399 	scctx->isc_tx_nsegments = IAVF_MAX_TX_SEGS;
400 	scctx->isc_tx_tso_segments_max = IAVF_MAX_TSO_SEGS;
401 	scctx->isc_tx_tso_size_max = IAVF_TSO_SIZE;
402 	scctx->isc_tx_tso_segsize_max = IAVF_MAX_DMA_SEG_SIZE;
403 	scctx->isc_rss_table_size = IAVF_RSS_VSI_LUT_SIZE;
404 	scctx->isc_capabilities = scctx->isc_capenable = IAVF_CAPS;
405 	scctx->isc_tx_csum_flags = CSUM_OFFLOAD;
406 
407 	return (0);
408 
409 err_vc_tq:
410 	taskqueue_free(sc->vc_tq);
411 err_res_buf:
412 	free(sc->vf_res, M_IAVF);
413 err_aq:
414 	iavf_shutdown_adminq(hw);
415 err_pci_res:
416 	iavf_free_pci_resources(sc);
417 err_early:
418 	IAVF_VC_LOCK_DESTROY(sc);
419 	return (error);
420 }
421 
422 /**
423  * iavf_vc_task - task used to process VC messages
424  * @arg: device softc
425  * @pending: unused
426  *
427  * Processes the admin queue, in order to process the virtual
428  * channel messages received from the PF.
429  */
430 static void
431 iavf_vc_task(void *arg, int pending __unused)
432 {
433 	struct iavf_sc *sc = (struct iavf_sc *)arg;
434 	u16 var;
435 
436 	iavf_process_adminq(sc, &var);
437 }
438 
439 /**
440  * iavf_setup_vc_tq - Setup task queues
441  * @sc: device softc
442  *
443  * Create taskqueue and tasklet for processing virtual channel messages. This
444  * is done in a separate non-iflib taskqueue so that the iflib context lock
445  * does not need to be held for VC messages to be processed.
446  *
447  * @returns zero on success, or an error code on failure.
448  */
449 static int
450 iavf_setup_vc_tq(struct iavf_sc *sc)
451 {
452 	device_t dev = sc->dev;
453 	int error = 0;
454 
455 	TASK_INIT(&sc->vc_task, 0, iavf_vc_task, sc);
456 
457 	sc->vc_tq = taskqueue_create_fast("iavf_vc", M_NOWAIT,
458 	    taskqueue_thread_enqueue, &sc->vc_tq);
459 	if (!sc->vc_tq) {
460 		device_printf(dev, "taskqueue_create_fast (for VC task) returned NULL!\n");
461 		return (ENOMEM);
462 	}
463 	error = taskqueue_start_threads(&sc->vc_tq, 1, PI_NET, "%s vc",
464 	    device_get_nameunit(dev));
465 	if (error) {
466 		device_printf(dev, "taskqueue_start_threads (for VC task) error: %d\n",
467 		    error);
468 		taskqueue_free(sc->vc_tq);
469 		return (error);
470 	}
471 
472 	return (error);
473 }
474 
475 /**
476  * iavf_if_attach_post - Finish attaching the device to the driver
477  * @ctx: the iflib context pointer
478  *
479  * Called by iflib after it has setup queues and interrupts. Used to finish up
480  * the attach process for a device. Attach logic which must occur after Tx and
481  * Rx queues are setup belongs here.
482  *
483  * @returns zero or a non-zero error code on failure
484  */
485 static int
486 iavf_if_attach_post(if_ctx_t ctx)
487 {
488 #ifdef IXL_DEBUG
489 	device_t dev = iflib_get_dev(ctx);
490 #endif
491 	struct iavf_sc	*sc;
492 	struct iavf_hw	*hw;
493 	struct iavf_vsi *vsi;
494 	int error = 0;
495 
496 	INIT_DBG_DEV(dev, "begin");
497 
498 	sc = iavf_sc_from_ctx(ctx);
499 	vsi = &sc->vsi;
500 	hw = &sc->hw;
501 
502 	/* Save off determined number of queues for interface */
503 	vsi->num_rx_queues = vsi->shared->isc_nrxqsets;
504 	vsi->num_tx_queues = vsi->shared->isc_ntxqsets;
505 
506 	/* Setup the stack interface */
507 	iavf_setup_interface(sc);
508 
509 	iavf_dbg_init(sc, "Interface setup complete\n");
510 
511 	/* Initialize statistics & add sysctls */
512 	bzero(&sc->vsi.eth_stats, sizeof(struct iavf_eth_stats));
513 	iavf_add_device_sysctls(sc);
514 
515 	atomic_store_rel_32(&sc->queues_enabled, 0);
516 	atomic_store_rel_32(&sc->mbx_ready, 1);
517 	atomic_store_rel_32(&sc->vc_reinit_required, 0);
518 	callout_init(&sc->mbx_retry, 1);
519 	sc->mbx_retry_initialized = true;
520 	iavf_set_state(&sc->state, IAVF_STATE_INITIALIZED);
521 
522 	/* We want AQ enabled early for init */
523 	iavf_enable_adminq_irq(hw);
524 
525 	INIT_DBG_DEV(dev, "end");
526 
527 	return (error);
528 }
529 
530 /**
531  * iavf_if_detach - Detach a device from the driver
532  * @ctx: the iflib context of the device to detach
533  *
534  * Called by iflib to detach a given device from the driver. Clean up any
535  * resources associated with the driver and shut the device down.
536  *
537  * @remark iflib always ignores the return value of IFDI_DETACH, so this
538  * function is effectively not allowed to fail. Instead, it should clean up
539  * and release as much as possible even if something goes wrong.
540  *
541  * @returns zero
542  */
543 static int
544 iavf_if_detach(if_ctx_t ctx)
545 {
546 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
547 	struct iavf_hw *hw = &sc->hw;
548 	device_t dev = sc->dev;
549 	enum iavf_status status;
550 
551 	INIT_DBG_DEV(dev, "begin");
552 
553 	iavf_mbx_retry_detach(sc);
554 	iavf_clear_state(&sc->state, IAVF_STATE_INITIALIZED);
555 
556 	/* Drain admin queue taskqueue */
557 	taskqueue_free(sc->vc_tq);
558 	IAVF_VC_LOCK_DESTROY(sc);
559 
560 	/* Remove all the media and link information */
561 	ifmedia_removeall(sc->media);
562 
563 	iavf_disable_adminq_irq(hw);
564 	status = iavf_shutdown_adminq(&sc->hw);
565 	if (status != IAVF_SUCCESS) {
566 		device_printf(dev,
567 		    "iavf_shutdown_adminq() failed with status %s\n",
568 		    iavf_stat_str(hw, status));
569 	}
570 
571 	free(sc->vf_res, M_IAVF);
572 	sc->vf_res = NULL;
573 	iavf_free_pci_resources(sc);
574 	iavf_free_filters(sc);
575 
576 	INIT_DBG_DEV(dev, "end");
577 	return (0);
578 }
579 
580 /**
581  * iavf_if_shutdown - called by iflib to handle shutdown
582  * @ctx: the iflib context pointer
583  *
584  * Callback for the IFDI_SHUTDOWN iflib function.
585  *
586  * @returns zero or an error code on failure
587  */
588 static int
589 iavf_if_shutdown(if_ctx_t ctx)
590 {
591 	return (iavf_if_suspend(ctx));
592 }
593 
594 /**
595  * iavf_if_suspend - called by iflib to handle suspend
596  * @ctx: the iflib context pointer
597  *
598  * Callback for the IFDI_SUSPEND iflib function.
599  *
600  * @returns zero or an error code on failure
601  */
602 static int
603 iavf_if_suspend(if_ctx_t ctx)
604 {
605 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
606 
607 	iavf_mbx_retry_stop(sc);
608 	return (0);
609 }
610 
611 /**
612  * iavf_if_resume - called by iflib to handle resume
613  * @ctx: the iflib context pointer
614  *
615  * Callback for the IFDI_RESUME iflib function.
616  *
617  * @returns zero or an error code on failure
618  */
619 static int
620 iavf_if_resume(if_ctx_t ctx __unused)
621 {
622 	return (0);
623 }
624 
625 /**
626  * iavf_vc_sleep_wait - Sleep for a response from a VC message
627  * @sc: device softc
628  * @op: the op code to sleep on
629  *
630  * Sleep until a response from the PF for the VC message sent by the
631  * given op.
632  *
633  * @returns zero on success, or EWOULDBLOCK if the sleep times out.
634  */
635 static int
636 iavf_vc_sleep_wait(struct iavf_sc *sc, u32 op)
637 {
638 	int error = 0;
639 
640 	IAVF_VC_LOCK_ASSERT(sc);
641 
642 	iavf_dbg_vc(sc, "Sleeping for op %b\n", op, IAVF_FLAGS);
643 
644 	error = mtx_sleep(iavf_vc_get_op_chan(sc, op),
645 	    &sc->vc_mtx, PRI_MAX, "iavf_vc", IAVF_AQ_TIMEOUT);
646 
647 	return (error);
648 }
649 
650 /**
651  * iavf_send_vc_msg_sleep - Send a virtchnl message and wait for a response
652  * @sc: device softc
653  * @op: the op code to send
654  *
655  * Send a virtchnl message to the PF, and sleep or busy wait for a response
656  * from the PF, depending on iflib context lock type.
657  *
658  * @remark this function does not wait if the device is detaching, on kernels
659  * that support indicating to the driver that the device is detaching
660  *
661  * @returns zero or an error code on failure.
662  */
663 int
664 iavf_send_vc_msg_sleep(struct iavf_sc *sc, u32 op)
665 {
666 	if_ctx_t ctx = sc->vsi.ctx;
667 	int error = 0;
668 
669 	IAVF_VC_LOCK(sc);
670 	error = iavf_vc_send_cmd(sc, op);
671 	if (error != 0) {
672 		iavf_dbg_vc(sc, "Error sending %b: %d\n", op, IAVF_FLAGS, error);
673 		goto release_lock;
674 	}
675 
676 	/* Don't wait for a response if the device is being detached. */
677 	if (!iflib_in_detach(ctx)) {
678 		error = iavf_vc_sleep_wait(sc, op);
679 		IAVF_VC_LOCK_ASSERT(sc);
680 
681 		if (error == EWOULDBLOCK && iavf_mbx_log_allowed(sc))
682 			device_printf(sc->dev, "%b timed out\n", op, IAVF_FLAGS);
683 	}
684 release_lock:
685 	IAVF_VC_UNLOCK(sc);
686 	return (error);
687 }
688 
689 /**
690  * iavf_send_vc_msg - Send a virtchnl message to the PF
691  * @sc: device softc
692  * @op: the op code to send
693  *
694  * Send a virtchnl message to the PF and do not wait for a response.
695  *
696  * @returns zero on success, or an error code on failure.
697  */
698 int
699 iavf_send_vc_msg(struct iavf_sc *sc, u32 op)
700 {
701 	int error = 0;
702 
703 	error = iavf_vc_send_cmd(sc, op);
704 	if (error != 0)
705 		iavf_dbg_vc(sc, "Error sending %b: %d\n", op, IAVF_FLAGS, error);
706 
707 	return (error);
708 }
709 
710 /**
711  * iavf_init_queues - initialize Tx and Rx queues
712  * @vsi: the VSI to initialize
713  *
714  * Refresh the Tx and Rx ring contents and update the tail pointers for each
715  * queue.
716  */
717 static void
718 iavf_init_queues(struct iavf_vsi *vsi)
719 {
720 	struct iavf_tx_queue *tx_que = vsi->tx_queues;
721 	struct iavf_rx_queue *rx_que = vsi->rx_queues;
722 	struct rx_ring *rxr;
723 	uint32_t mbuf_sz;
724 
725 	mbuf_sz = iflib_get_rx_mbuf_sz(vsi->ctx);
726 	MPASS(mbuf_sz <= UINT16_MAX);
727 
728 	for (int i = 0; i < vsi->num_tx_queues; i++, tx_que++)
729 		iavf_init_tx_ring(vsi, tx_que);
730 
731 	for (int i = 0; i < vsi->num_rx_queues; i++, rx_que++) {
732 		rxr = &rx_que->rxr;
733 
734 		rxr->mbuf_sz = mbuf_sz;
735 		wr32(vsi->hw, rxr->tail, 0);
736 	}
737 }
738 
739 /*
740  * A VF can outlive a PF reset or temporary loss of virtchnl service.  Keep
741  * repeated mailbox discovery out of ordinary status paths and retry complete
742  * initialization only while the interface remains administratively up.
743  */
744 static void
745 iavf_mbx_retry_callout(void *arg)
746 {
747 	struct iavf_sc *sc;
748 	if_t ifp;
749 
750 	sc = arg;
751 	if (atomic_readandclear_32(&sc->mbx_retry_pending) == 0 ||
752 	    atomic_load_acq_32(&sc->mbx_ready) != 0 ||
753 	    iflib_in_detach(sc->vsi.ctx))
754 		return;
755 	ifp = iflib_get_ifp(sc->vsi.ctx);
756 	if ((if_getflags(ifp) & IFF_UP) == 0)
757 		return;
758 
759 	iflib_request_reset_if_up(sc->vsi.ctx);
760 	iflib_admin_intr_deferred(sc->vsi.ctx);
761 }
762 
763 bool
764 iavf_mbx_log_allowed(struct iavf_sc *sc)
765 {
766 
767 	/* Report each backoff stage, then limit the steady eight-second retry. */
768 	if (sc->mbx_retry_stage != nitems(iavf_mbx_retry_delay) - 1)
769 		return (true);
770 	return (ratecheck(&sc->mbx_last_log, &iavf_mbx_log_interval) != 0);
771 }
772 
773 static void
774 iavf_mbx_retry_detach(struct iavf_sc *sc)
775 {
776 
777 	if (!sc->mbx_retry_initialized)
778 		return;
779 	atomic_readandclear_32(&sc->mbx_retry_pending);
780 	callout_drain(&sc->mbx_retry);
781 	sc->mbx_retry_initialized = false;
782 }
783 
784 static void
785 iavf_mbx_retry_prepare(struct iavf_sc *sc)
786 {
787 
788 	if (!sc->mbx_retry_initialized)
789 		return;
790 	atomic_readandclear_32(&sc->mbx_retry_pending);
791 	callout_drain(&sc->mbx_retry);
792 }
793 
794 static void
795 iavf_mbx_retry_stop(struct iavf_sc *sc)
796 {
797 	if_t ifp;
798 
799 	if (!sc->mbx_retry_initialized)
800 		return;
801 	atomic_readandclear_32(&sc->mbx_retry_pending);
802 	callout_drain(&sc->mbx_retry);
803 	ifp = iflib_get_ifp(sc->vsi.ctx);
804 	if ((if_getflags(ifp) & IFF_UP) == 0)
805 		sc->mbx_retry_stage = 0;
806 }
807 
808 static void
809 iavf_mbx_retry_failed(if_ctx_t ctx)
810 {
811 	struct iavf_sc *sc;
812 	struct iavf_vsi *vsi;
813 	if_t ifp;
814 	sbintime_t delay;
815 	u_int stage;
816 
817 	sc = iavf_sc_from_ctx(ctx);
818 	vsi = &sc->vsi;
819 	atomic_store_rel_32(&sc->mbx_ready, 0);
820 	iavf_clear_state(&sc->state, IAVF_STATE_RUNNING);
821 	sc->link_up = false;
822 	if (vsi->link_active) {
823 		vsi->link_active = false;
824 		iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
825 	}
826 	iflib_init_failed(ctx);
827 
828 	ifp = iflib_get_ifp(ctx);
829 	if (!sc->mbx_retry_initialized ||
830 	    (if_getflags(ifp) & IFF_UP) == 0)
831 		return;
832 	stage = sc->mbx_retry_stage;
833 	if (stage >= nitems(iavf_mbx_retry_delay))
834 		stage = nitems(iavf_mbx_retry_delay) - 1;
835 	delay = iavf_mbx_retry_delay[stage];
836 	if (sc->mbx_retry_stage + 1 < nitems(iavf_mbx_retry_delay))
837 		sc->mbx_retry_stage++;
838 	atomic_set_32(&sc->mbx_retry_pending, 1);
839 	callout_reset_sbt(&sc->mbx_retry, delay, 0,
840 	    iavf_mbx_retry_callout, sc, C_PREL(1));
841 }
842 
843 static void
844 iavf_mbx_retry_succeeded(struct iavf_sc *sc)
845 {
846 	bool recovered;
847 
848 	recovered = sc->mbx_retry_stage != 0;
849 	atomic_store_rel_32(&sc->vc_reinit_required, 0);
850 	atomic_store_rel_32(&sc->mbx_ready, 1);
851 	atomic_readandclear_32(&sc->mbx_retry_pending);
852 	if (sc->mbx_retry_initialized)
853 		callout_stop(&sc->mbx_retry);
854 	sc->mbx_retry_stage = 0;
855 	sc->mbx_last_log.tv_sec = 0;
856 	sc->mbx_last_log.tv_usec = 0;
857 	iavf_clear_state(&sc->state, IAVF_STATE_RESET_REQUIRED);
858 	iavf_clear_state(&sc->state, IAVF_STATE_RESET_PENDING);
859 	if (recovered)
860 		device_printf(sc->dev, "PF mailbox communication restored\n");
861 }
862 
863 static void
864 iavf_mbx_lost(struct iavf_sc *sc)
865 {
866 	struct iavf_vsi *vsi;
867 
868 	atomic_store_rel_32(&sc->vc_reinit_required, 1);
869 	if (atomic_readandclear_32(&sc->mbx_ready) == 0)
870 		return;
871 	vsi = &sc->vsi;
872 	iavf_clear_state(&sc->state, IAVF_STATE_RUNNING);
873 	sc->link_up = false;
874 	if (vsi->link_active) {
875 		vsi->link_active = false;
876 		iflib_link_state_change(vsi->ctx, LINK_STATE_DOWN, 0);
877 	}
878 	iflib_request_reset_if_up(vsi->ctx);
879 	iflib_admin_intr_deferred(vsi->ctx);
880 }
881 
882 static int
883 iavf_wait_asq(struct iavf_sc *sc, u32 max_retries)
884 {
885 	struct iavf_hw *hw;
886 
887 	hw = &sc->hw;
888 	for (u32 retry = 0; retry < max_retries; retry++) {
889 		if (iavf_asq_done(hw))
890 			return (0);
891 		iavf_msec_pause(10);
892 	}
893 	return (ETIMEDOUT);
894 }
895 
896 /*
897  * A VFLR discards the Admin Queue and lets the PF replace the VF's VSI.
898  * Re-establish VERSION and GET_VF_RESOURCES before using any cached VSI ID.
899  * Runtime attempts are deliberately shorter than attach-time discovery; the
900  * retry callout supplies the longer backoff when the PF remains unavailable.
901  */
902 static int
903 iavf_reestablish_vc(struct iavf_sc *sc)
904 {
905 	struct iavf_hw *hw;
906 	struct iavf_vsi *vsi;
907 	enum iavf_status status;
908 	int error;
909 
910 	hw = &sc->hw;
911 	vsi = &sc->vsi;
912 	iavf_disable_adminq_irq(hw);
913 	taskqueue_drain(sc->vc_tq, &sc->vc_task);
914 	/* A task already running when interrupts were masked can re-enable it. */
915 	iavf_disable_adminq_irq(hw);
916 	pci_enable_busmaster(sc->dev);
917 
918 	status = iavf_shutdown_adminq(hw);
919 	if (status != IAVF_SUCCESS)
920 		return (EIO);
921 	status = iavf_init_adminq(hw);
922 	if (status != IAVF_SUCCESS)
923 		return (EIO);
924 
925 	error = iavf_send_api_ver(sc);
926 	if (error != 0)
927 		goto fail;
928 	error = iavf_wait_asq(sc, IAVF_MBX_RECOVERY_ASQ_RETRIES);
929 	if (error != 0)
930 		goto fail;
931 	error = iavf_verify_api_ver_retries(sc,
932 	    IAVF_MBX_RECOVERY_VERSION_RETRIES);
933 	if (error != 0)
934 		goto fail;
935 
936 	error = iavf_send_vf_config_msg(sc);
937 	if (error != 0)
938 		goto fail;
939 	error = iavf_wait_asq(sc, IAVF_MBX_RECOVERY_ASQ_RETRIES);
940 	if (error != 0)
941 		goto fail;
942 	error = iavf_get_vf_config_retries(sc,
943 	    IAVF_MBX_RECOVERY_CONFIG_RETRIES);
944 	if (error != 0)
945 		goto fail;
946 	error = iavf_get_vsi_res_from_vf_res(sc);
947 	if (error != 0)
948 		goto fail;
949 
950 	if (vsi->num_tx_queues > sc->vsi_res->num_queue_pairs ||
951 	    vsi->num_rx_queues > sc->vsi_res->num_queue_pairs ||
952 	    vsi->num_rx_queues + 1 > sc->vf_res->max_vectors) {
953 		if (iavf_mbx_log_allowed(sc))
954 			device_printf(sc->dev,
955 			    "PF now provides %u queue pairs and %u vectors; "
956 			    "the VF has %u TX and %u RX queues\n",
957 			    sc->vsi_res->num_queue_pairs,
958 			    sc->vf_res->max_vectors, vsi->num_tx_queues,
959 			    vsi->num_rx_queues);
960 		error = ENOSPC;
961 		goto fail;
962 	}
963 
964 	/*
965 	 * RESET_PENDING prevents the ordinary AdminQ task from consuming
966 	 * messages while the queue may still belong to the pre-reset device.
967 	 * The successful VERSION and GET_VF_RESOURCES exchange above proves
968 	 * that the reset has completed and this is the replacement AdminQ.
969 	 * Clear the stale indication before normal virtchnl requests resume.
970 	 */
971 	iavf_clear_state(&sc->state, IAVF_STATE_RESET_PENDING);
972 	iavf_enable_adminq_irq(hw);
973 	return (0);
974 
975 fail:
976 	iavf_disable_adminq_irq(hw);
977 	return (error);
978 }
979 
980 static void
981 iavf_replay_filters(struct iavf_sc *sc)
982 {
983 	struct iavf_mac_filter *mac;
984 	struct iavf_vlan_filter *vlan;
985 
986 	SLIST_FOREACH(mac, sc->mac_filters, next) {
987 		if ((mac->flags & IAVF_FILTER_DEL) == 0)
988 			mac->flags |= IAVF_FILTER_ADD | IAVF_FILTER_USED;
989 	}
990 	SLIST_FOREACH(vlan, sc->vlan_filters, next) {
991 		if ((vlan->flags & IAVF_FILTER_DEL) == 0)
992 			vlan->flags = IAVF_FILTER_ADD;
993 	}
994 }
995 
996 /**
997  * iavf_if_init - Initialize device for operation
998  * @ctx: the iflib context pointer
999  *
1000  * Initializes a device for operation. Called by iflib in response to an
1001  * interface up event from the stack.
1002  *
1003  * Recoverable failures are reported to iflib with iflib_init_failed(), and a
1004  * bounded callout retries initialization while the interface remains
1005  * administratively up.
1006  */
1007 static void
1008 iavf_if_init(if_ctx_t ctx)
1009 {
1010 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1011 	struct iavf_vsi *vsi = &sc->vsi;
1012 	struct iavf_hw *hw = &sc->hw;
1013 	if_t ifp = iflib_get_ifp(ctx);
1014 	u8 tmpaddr[ETHER_ADDR_LEN];
1015 	device_t dev = sc->dev;
1016 	bool replay_filters;
1017 	int error = 0;
1018 
1019 	INIT_DBG_IF(ifp, "begin");
1020 
1021 	sx_assert(iflib_ctx_lock_get(ctx), SA_XLOCKED);
1022 	iavf_mbx_retry_prepare(sc);
1023 	replay_filters = atomic_load_acq_32(&sc->vc_reinit_required) != 0;
1024 
1025 	if (!iavf_reset_is_complete(hw)) {
1026 		atomic_store_rel_32(&sc->vc_reinit_required, 1);
1027 		if (iavf_mbx_log_allowed(sc))
1028 			device_printf(dev,
1029 			    "PF mailbox is unavailable; initialization deferred\n");
1030 		iavf_mbx_retry_failed(ctx);
1031 		return;
1032 	}
1033 	if (!iavf_check_asq_alive(hw)) {
1034 		atomic_store_rel_32(&sc->vc_reinit_required, 1);
1035 		replay_filters = true;
1036 	}
1037 	if (replay_filters) {
1038 		error = iavf_reestablish_vc(sc);
1039 		if (error != 0) {
1040 			if (iavf_mbx_log_allowed(sc))
1041 				device_printf(dev,
1042 				    "PF mailbox rediscovery failed: %d\n", error);
1043 			goto fail;
1044 		}
1045 	}
1046 
1047 	/* Make sure queues are disabled */
1048 	error = iavf_disable_queues_with_retries(sc);
1049 	if (error != 0)
1050 		goto fail;
1051 
1052 	bcopy(if_getlladdr(ifp), tmpaddr, ETHER_ADDR_LEN);
1053 	if (!cmp_etheraddr(hw->mac.addr, tmpaddr) &&
1054 	    (iavf_validate_mac_addr(tmpaddr) == IAVF_SUCCESS)) {
1055 		error = iavf_del_mac_filter(sc, hw->mac.addr);
1056 		if (error == 0)
1057 			iavf_send_vc_msg(sc, IAVF_FLAG_AQ_DEL_MAC_FILTER);
1058 
1059 		bcopy(tmpaddr, hw->mac.addr, ETH_ALEN);
1060 	}
1061 
1062 	error = iavf_add_mac_filter(sc, hw->mac.addr, 0);
1063 	if (replay_filters)
1064 		iavf_replay_filters(sc);
1065 	if (!error || error == EEXIST || replay_filters)
1066 		iavf_send_vc_msg(sc, IAVF_FLAG_AQ_ADD_MAC_FILTER);
1067 	if (replay_filters)
1068 		iavf_send_vc_msg(sc, IAVF_FLAG_AQ_ADD_VLAN_FILTER);
1069 	iflib_set_mac(ctx, hw->mac.addr);
1070 
1071 	/* Prepare the queues for operation */
1072 	iavf_init_queues(vsi);
1073 
1074 	/* Set initial ITR values */
1075 	iavf_configure_itr(sc);
1076 
1077 	iavf_send_vc_msg(sc, IAVF_FLAG_AQ_CONFIGURE_QUEUES);
1078 
1079 	/* Set up RSS */
1080 	iavf_config_rss(sc);
1081 
1082 	/* Map vectors */
1083 	iavf_send_vc_msg(sc, IAVF_FLAG_AQ_MAP_VECTORS);
1084 
1085 	/* Init SW TX ring indices */
1086 	if (vsi->enable_head_writeback)
1087 		iavf_init_tx_cidx(vsi);
1088 	else
1089 		iavf_init_tx_rsqs(vsi);
1090 
1091 	/* Configure promiscuous mode */
1092 	iavf_config_promisc(sc, if_getflags(ifp));
1093 
1094 	/* Enable queues */
1095 	atomic_store_rel_32(&sc->queues_enabled, 0);
1096 	error = iavf_send_vc_msg_sleep(sc, IAVF_FLAG_AQ_ENABLE_QUEUES);
1097 	if (error != 0 ||
1098 	    atomic_load_acq_32(&sc->queues_enabled) == 0)
1099 		goto fail;
1100 
1101 	iavf_mbx_retry_succeeded(sc);
1102 	iavf_set_state(&sc->state, IAVF_STATE_RUNNING);
1103 	return;
1104 
1105 fail:
1106 	atomic_store_rel_32(&sc->vc_reinit_required, 1);
1107 	iavf_mbx_retry_failed(ctx);
1108 }
1109 
1110 /**
1111  * iavf_if_msix_intr_assign - Assign MSI-X interrupts
1112  * @ctx: the iflib context pointer
1113  * @msix: the number of MSI-X vectors available
1114  *
1115  * Called by iflib to assign MSI-X interrupt vectors to queues. Assigns and
1116  * sets up vectors for each Tx and Rx queue, as well as the administrative
1117  * control interrupt.
1118  *
1119  * @returns zero or an error code on failure
1120  */
1121 static int
1122 iavf_if_msix_intr_assign(if_ctx_t ctx, int msix __unused)
1123 {
1124 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1125 	struct iavf_vsi *vsi = &sc->vsi;
1126 	struct iavf_rx_queue *rx_que = vsi->rx_queues;
1127 	struct iavf_tx_queue *tx_que = vsi->tx_queues;
1128 	int err, i, rid, vector = 0;
1129 	char buf[16];
1130 
1131 	MPASS(vsi->shared->isc_nrxqsets > 0);
1132 	MPASS(vsi->shared->isc_ntxqsets > 0);
1133 
1134 	/* Admin Que is vector 0*/
1135 	rid = vector + 1;
1136 	err = iflib_irq_alloc_generic(ctx, &vsi->irq, rid, IFLIB_INTR_ADMIN,
1137 	    iavf_msix_adminq, sc, 0, "aq");
1138 	if (err) {
1139 		iflib_irq_free(ctx, &vsi->irq);
1140 		device_printf(iflib_get_dev(ctx),
1141 		    "Failed to register Admin Que handler");
1142 		return (err);
1143 	}
1144 
1145 	/* Now set up the stations */
1146 	for (i = 0, vector = 1; i < vsi->shared->isc_nrxqsets; i++, vector++, rx_que++) {
1147 		rid = vector + 1;
1148 
1149 		snprintf(buf, sizeof(buf), "rxq%d", i);
1150 		err = iflib_irq_alloc_generic(ctx, &rx_que->que_irq, rid,
1151 		    IFLIB_INTR_RXTX, iavf_msix_que, rx_que, rx_que->rxr.me, buf);
1152 		if (err) {
1153 			device_printf(iflib_get_dev(ctx),
1154 			    "Failed to allocate queue RX int vector %d, err: %d\n", i, err);
1155 			vsi->num_rx_queues = i + 1;
1156 			goto fail;
1157 		}
1158 		rx_que->msix = vector;
1159 	}
1160 
1161 	bzero(buf, sizeof(buf));
1162 
1163 	for (i = 0; i < vsi->shared->isc_ntxqsets; i++, tx_que++) {
1164 		snprintf(buf, sizeof(buf), "txq%d", i);
1165 		iflib_softirq_alloc_generic(ctx,
1166 		    &vsi->rx_queues[i % vsi->shared->isc_nrxqsets].que_irq,
1167 		    IFLIB_INTR_TX, tx_que, tx_que->txr.me, buf);
1168 
1169 		tx_que->msix = (i % vsi->shared->isc_nrxqsets) + 1;
1170 	}
1171 
1172 	return (0);
1173 fail:
1174 	iflib_irq_free(ctx, &vsi->irq);
1175 	rx_que = vsi->rx_queues;
1176 	for (i = 0; i < vsi->num_rx_queues; i++, rx_que++)
1177 		iflib_irq_free(ctx, &rx_que->que_irq);
1178 	return (err);
1179 }
1180 
1181 /**
1182  * iavf_if_enable_intr - Enable all interrupts for a device
1183  * @ctx: the iflib context pointer
1184  *
1185  * Called by iflib to request enabling all interrupts.
1186  */
1187 static void
1188 iavf_if_enable_intr(if_ctx_t ctx)
1189 {
1190 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1191 	struct iavf_vsi *vsi = &sc->vsi;
1192 
1193 	iavf_enable_intr(vsi);
1194 }
1195 
1196 /**
1197  * iavf_if_disable_intr - Disable all interrupts for a device
1198  * @ctx: the iflib context pointer
1199  *
1200  * Called by iflib to request disabling all interrupts.
1201  */
1202 static void
1203 iavf_if_disable_intr(if_ctx_t ctx)
1204 {
1205 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1206 	struct iavf_vsi *vsi = &sc->vsi;
1207 
1208 	iavf_disable_intr(vsi);
1209 }
1210 
1211 /**
1212  * iavf_if_rx_queue_intr_enable - Enable one Rx queue interrupt
1213  * @ctx: the iflib context pointer
1214  * @rxqid: Rx queue index
1215  *
1216  * Enables the interrupt associated with a specified Rx queue.
1217  *
1218  * @returns zero
1219  */
1220 static int
1221 iavf_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
1222 {
1223 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1224 	struct iavf_vsi *vsi = &sc->vsi;
1225 	struct iavf_hw *hw = vsi->hw;
1226 	struct iavf_rx_queue *rx_que = &vsi->rx_queues[rxqid];
1227 
1228 	iavf_enable_queue_irq(hw, rx_que->msix - 1);
1229 	return (0);
1230 }
1231 
1232 /**
1233  * iavf_if_tx_queue_intr_enable - Enable one Tx queue interrupt
1234  * @ctx: the iflib context pointer
1235  * @txqid: Tx queue index
1236  *
1237  * Enables the interrupt associated with a specified Tx queue.
1238  *
1239  * @returns zero
1240  */
1241 static int
1242 iavf_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
1243 {
1244 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1245 	struct iavf_vsi *vsi = &sc->vsi;
1246 	struct iavf_hw *hw = vsi->hw;
1247 	struct iavf_tx_queue *tx_que = &vsi->tx_queues[txqid];
1248 
1249 	iavf_enable_queue_irq(hw, tx_que->msix - 1);
1250 	return (0);
1251 }
1252 
1253 /**
1254  * iavf_if_tx_queues_alloc - Allocate Tx queue memory
1255  * @ctx: the iflib context pointer
1256  * @vaddrs: Array of virtual addresses
1257  * @paddrs: Array of physical addresses
1258  * @ntxqs: the number of Tx queues per group (should always be 1)
1259  * @ntxqsets: the number of Tx queues
1260  *
1261  * Allocates memory for the specified number of Tx queues. This includes
1262  * memory for the queue structures and the report status array for the queues.
1263  * The virtual and physical addresses are saved for later use during
1264  * initialization.
1265  *
1266  * @returns zero or a non-zero error code on failure
1267  */
1268 static int
1269 iavf_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets)
1270 {
1271 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1272 	struct iavf_vsi *vsi = &sc->vsi;
1273 	if_softc_ctx_t scctx = vsi->shared;
1274 	struct iavf_tx_queue *que;
1275 	int i, j, error = 0;
1276 
1277 	MPASS(scctx->isc_ntxqsets > 0);
1278 	MPASS(ntxqs == 1);
1279 	MPASS(scctx->isc_ntxqsets == ntxqsets);
1280 
1281 	/* Allocate queue structure memory */
1282 	if (!(vsi->tx_queues =
1283 	    (struct iavf_tx_queue *)malloc(sizeof(struct iavf_tx_queue) *ntxqsets, M_IAVF, M_NOWAIT | M_ZERO))) {
1284 		device_printf(iflib_get_dev(ctx), "Unable to allocate TX ring memory\n");
1285 		return (ENOMEM);
1286 	}
1287 
1288 	for (i = 0, que = vsi->tx_queues; i < ntxqsets; i++, que++) {
1289 		struct tx_ring *txr = &que->txr;
1290 
1291 		txr->me = i;
1292 		que->vsi = vsi;
1293 
1294 		if (!vsi->enable_head_writeback) {
1295 			/* Allocate report status array */
1296 			if (!(txr->tx_rsq = (qidx_t *)malloc(sizeof(qidx_t) * scctx->isc_ntxd[0], M_IAVF, M_NOWAIT))) {
1297 				device_printf(iflib_get_dev(ctx), "failed to allocate tx_rsq memory\n");
1298 				error = ENOMEM;
1299 				goto fail;
1300 			}
1301 			/* Init report status array */
1302 			for (j = 0; j < scctx->isc_ntxd[0]; j++)
1303 				txr->tx_rsq[j] = QIDX_INVALID;
1304 		}
1305 		/* get the virtual and physical address of the hardware queues */
1306 		txr->tail = IAVF_QTX_TAIL1(txr->me);
1307 		txr->tx_base = (struct iavf_tx_desc *)vaddrs[i * ntxqs];
1308 		txr->tx_paddr = paddrs[i * ntxqs];
1309 		txr->que = que;
1310 	}
1311 
1312 	return (0);
1313 fail:
1314 	iavf_if_queues_free(ctx);
1315 	return (error);
1316 }
1317 
1318 /**
1319  * iavf_if_rx_queues_alloc - Allocate Rx queue memory
1320  * @ctx: the iflib context pointer
1321  * @vaddrs: Array of virtual addresses
1322  * @paddrs: Array of physical addresses
1323  * @nrxqs: number of Rx queues per group (should always be 1)
1324  * @nrxqsets: the number of Rx queues to allocate
1325  *
1326  * Called by iflib to allocate driver memory for a number of Rx queues.
1327  * Allocates memory for the drivers private Rx queue data structure, and saves
1328  * the physical and virtual addresses for later use.
1329  *
1330  * @returns zero or a non-zero error code on failure
1331  */
1332 static int
1333 iavf_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nrxqs, int nrxqsets)
1334 {
1335 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1336 	struct iavf_vsi *vsi = &sc->vsi;
1337 	struct iavf_rx_queue *que;
1338 	int i, error = 0;
1339 
1340 #ifdef INVARIANTS
1341 	if_softc_ctx_t scctx = vsi->shared;
1342 	MPASS(scctx->isc_nrxqsets > 0);
1343 	MPASS(nrxqs == 1);
1344 	MPASS(scctx->isc_nrxqsets == nrxqsets);
1345 #endif
1346 
1347 	/* Allocate queue structure memory */
1348 	if (!(vsi->rx_queues =
1349 	    (struct iavf_rx_queue *) malloc(sizeof(struct iavf_rx_queue) *
1350 	    nrxqsets, M_IAVF, M_NOWAIT | M_ZERO))) {
1351 		device_printf(iflib_get_dev(ctx), "Unable to allocate RX ring memory\n");
1352 		error = ENOMEM;
1353 		goto fail;
1354 	}
1355 
1356 	for (i = 0, que = vsi->rx_queues; i < nrxqsets; i++, que++) {
1357 		struct rx_ring *rxr = &que->rxr;
1358 
1359 		rxr->me = i;
1360 		que->vsi = vsi;
1361 
1362 		/* get the virtual and physical address of the hardware queues */
1363 		rxr->tail = IAVF_QRX_TAIL1(rxr->me);
1364 		rxr->rx_base = (union iavf_rx_desc *)vaddrs[i * nrxqs];
1365 		rxr->rx_paddr = paddrs[i * nrxqs];
1366 		rxr->que = que;
1367 	}
1368 
1369 	return (0);
1370 fail:
1371 	iavf_if_queues_free(ctx);
1372 	return (error);
1373 }
1374 
1375 /**
1376  * iavf_if_queues_free - Free driver queue memory
1377  * @ctx: the iflib context pointer
1378  *
1379  * Called by iflib to release memory allocated by the driver when setting up
1380  * Tx and Rx queues.
1381  *
1382  * @remark The ordering of this function and iavf_if_detach is not guaranteed.
1383  * It is possible for this function to be called either before or after the
1384  * iavf_if_detach. Thus, care must be taken to ensure that either ordering of
1385  * iavf_if_detach and iavf_if_queues_free is safe.
1386  */
1387 static void
1388 iavf_if_queues_free(if_ctx_t ctx)
1389 {
1390 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1391 	struct iavf_vsi *vsi = &sc->vsi;
1392 
1393 	if (!vsi->enable_head_writeback) {
1394 		struct iavf_tx_queue *que;
1395 		int i = 0;
1396 
1397 		for (i = 0, que = vsi->tx_queues; i < vsi->shared->isc_ntxqsets; i++, que++) {
1398 			struct tx_ring *txr = &que->txr;
1399 			if (txr->tx_rsq != NULL) {
1400 				free(txr->tx_rsq, M_IAVF);
1401 				txr->tx_rsq = NULL;
1402 			}
1403 		}
1404 	}
1405 
1406 	if (vsi->tx_queues != NULL) {
1407 		free(vsi->tx_queues, M_IAVF);
1408 		vsi->tx_queues = NULL;
1409 	}
1410 	if (vsi->rx_queues != NULL) {
1411 		free(vsi->rx_queues, M_IAVF);
1412 		vsi->rx_queues = NULL;
1413 	}
1414 }
1415 
1416 /**
1417  * iavf_check_aq_errors - Check for AdminQ errors
1418  * @sc: device softc
1419  *
1420  * Check the AdminQ registers for errors, and determine whether or not a reset
1421  * may be required to resolve them.
1422  *
1423  * @post if there are errors, the VF device will be stopped and a reset will
1424  * be requested.
1425  *
1426  * @returns zero if there are no issues, EBUSY if the device is resetting,
1427  * or EIO if there are any AQ errors.
1428  */
1429 static int
1430 iavf_check_aq_errors(struct iavf_sc *sc)
1431 {
1432 	struct iavf_hw *hw = &sc->hw;
1433 	device_t dev = sc->dev;
1434 	u32 reg, oldreg;
1435 	u8 aq_error = false;
1436 
1437 	oldreg = reg = rd32(hw, hw->aq.arq.len);
1438 
1439 	/* Check if device is in reset */
1440 	if (reg == 0xdeadbeef || reg == 0xffffffff) {
1441 		device_printf(dev, "VF in reset\n");
1442 		return (EBUSY);
1443 	}
1444 
1445 	/* Check for Admin queue errors */
1446 	if (reg & IAVF_VF_ARQLEN1_ARQVFE_MASK) {
1447 		device_printf(dev, "ARQ VF Error detected\n");
1448 		reg &= ~IAVF_VF_ARQLEN1_ARQVFE_MASK;
1449 		aq_error = true;
1450 	}
1451 	if (reg & IAVF_VF_ARQLEN1_ARQOVFL_MASK) {
1452 		device_printf(dev, "ARQ Overflow Error detected\n");
1453 		reg &= ~IAVF_VF_ARQLEN1_ARQOVFL_MASK;
1454 		aq_error = true;
1455 	}
1456 	if (reg & IAVF_VF_ARQLEN1_ARQCRIT_MASK) {
1457 		device_printf(dev, "ARQ Critical Error detected\n");
1458 		reg &= ~IAVF_VF_ARQLEN1_ARQCRIT_MASK;
1459 		aq_error = true;
1460 	}
1461 	if (oldreg != reg)
1462 		wr32(hw, hw->aq.arq.len, reg);
1463 
1464 	oldreg = reg = rd32(hw, hw->aq.asq.len);
1465 	if (reg & IAVF_VF_ATQLEN1_ATQVFE_MASK) {
1466 		device_printf(dev, "ASQ VF Error detected\n");
1467 		reg &= ~IAVF_VF_ATQLEN1_ATQVFE_MASK;
1468 		aq_error = true;
1469 	}
1470 	if (reg & IAVF_VF_ATQLEN1_ATQOVFL_MASK) {
1471 		device_printf(dev, "ASQ Overflow Error detected\n");
1472 		reg &= ~IAVF_VF_ATQLEN1_ATQOVFL_MASK;
1473 		aq_error = true;
1474 	}
1475 	if (reg & IAVF_VF_ATQLEN1_ATQCRIT_MASK) {
1476 		device_printf(dev, "ASQ Critical Error detected\n");
1477 		reg &= ~IAVF_VF_ATQLEN1_ATQCRIT_MASK;
1478 		aq_error = true;
1479 	}
1480 	if (oldreg != reg)
1481 		wr32(hw, hw->aq.asq.len, reg);
1482 
1483 	return (aq_error ? EIO : 0);
1484 }
1485 
1486 /**
1487  * iavf_process_adminq - Process adminq responses from the PF
1488  * @sc: device softc
1489  * @pending: output parameter indicating how many messages remain
1490  *
1491  * Process the adminq to handle replies from the PF over the virtchnl
1492  * connection.
1493  *
1494  * @returns zero or an iavf_status code on failure
1495  */
1496 static enum iavf_status
1497 iavf_process_adminq(struct iavf_sc *sc, u16 *pending)
1498 {
1499 	enum iavf_status status = IAVF_SUCCESS;
1500 	struct iavf_arq_event_info event;
1501 	struct iavf_hw *hw = &sc->hw;
1502 	struct virtchnl_msg *v_msg;
1503 	int error = 0, loop = 0;
1504 	u32 reg;
1505 
1506 	if (iavf_test_state(&sc->state, IAVF_STATE_RESET_PENDING)) {
1507 		status = IAVF_ERR_ADMIN_QUEUE_ERROR;
1508 		goto reenable_interrupt;
1509 	}
1510 
1511 	error = iavf_check_aq_errors(sc);
1512 	if (error) {
1513 		status = IAVF_ERR_ADMIN_QUEUE_CRITICAL_ERROR;
1514 		goto reenable_interrupt;
1515 	}
1516 
1517 	event.buf_len = IAVF_AQ_BUF_SZ;
1518         event.msg_buf = sc->aq_buffer;
1519 	bzero(event.msg_buf, IAVF_AQ_BUF_SZ);
1520 	v_msg = (struct virtchnl_msg *)&event.desc;
1521 
1522 	IAVF_VC_LOCK(sc);
1523 	/* clean and process any events */
1524 	do {
1525 		status = iavf_clean_arq_element(hw, &event, pending);
1526 		/*
1527 		 * Also covers normal case when iavf_clean_arq_element()
1528 		 * returns "IAVF_ERR_ADMIN_QUEUE_NO_WORK"
1529 		 */
1530 		if (status)
1531 			break;
1532 		iavf_vc_completion(sc, v_msg->v_opcode,
1533 		    v_msg->v_retval, event.msg_buf, event.msg_len);
1534 		bzero(event.msg_buf, IAVF_AQ_BUF_SZ);
1535 	} while (*pending && (loop++ < IAVF_ADM_LIMIT));
1536 	IAVF_VC_UNLOCK(sc);
1537 
1538 reenable_interrupt:
1539 	/* Re-enable admin queue interrupt cause */
1540 	reg = rd32(hw, IAVF_VFINT_ICR0_ENA1);
1541 	reg |= IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK;
1542 	wr32(hw, IAVF_VFINT_ICR0_ENA1, reg);
1543 
1544 	return (status);
1545 }
1546 
1547 /**
1548  * iavf_if_update_admin_status - Administrative status task
1549  * @ctx: iflib context
1550  *
1551  * Called by iflib to handle administrative status events. The iavf driver
1552  * uses this to process the adminq virtchnl messages outside of interrupt
1553  * context.
1554  */
1555 static void
1556 iavf_if_update_admin_status(if_ctx_t ctx)
1557 {
1558 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1559 	struct iavf_hw *hw = &sc->hw;
1560 	struct iavf_vsi *vsi = &sc->vsi;
1561 	if_t ifp = iflib_get_ifp(ctx);
1562 	u16 pending = 0;
1563 
1564 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 ||
1565 	    atomic_load_acq_32(&sc->mbx_ready) == 0) {
1566 		if (vsi->link_active) {
1567 			vsi->link_active = false;
1568 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
1569 		}
1570 		return;
1571 	}
1572 
1573 	iavf_process_adminq(sc, &pending);
1574 	iavf_update_link_status(sc);
1575 
1576 	/*
1577 	 * If there are still messages to process, reschedule.
1578 	 * Otherwise, re-enable the Admin Queue interrupt.
1579 	 */
1580 	if (pending > 0)
1581 		iflib_admin_intr_deferred(ctx);
1582 	else
1583 		iavf_enable_adminq_irq(hw);
1584 }
1585 
1586 /**
1587  * iavf_if_multi_set - Set multicast address filters
1588  * @ctx: iflib context
1589  *
1590  * Called by iflib to update the current list of multicast filters for the
1591  * device.
1592  */
1593 static void
1594 iavf_if_multi_set(if_ctx_t ctx)
1595 {
1596 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1597 
1598 	iavf_multi_set(sc);
1599 }
1600 
1601 /**
1602  * iavf_if_mtu_set - Set the device MTU
1603  * @ctx: iflib context
1604  * @mtu: MTU value to set
1605  *
1606  * Called by iflib to set the device MTU.
1607  *
1608  * @returns zero on success, or EINVAL if the MTU is invalid.
1609  */
1610 static int
1611 iavf_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
1612 {
1613 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1614 	struct iavf_vsi *vsi = &sc->vsi;
1615 
1616 	IOCTL_DEBUGOUT("ioctl: SiOCSIFMTU (Set Interface MTU)");
1617 	if (mtu < IAVF_MIN_MTU || mtu > IAVF_MAX_MTU) {
1618 		device_printf(sc->dev, "mtu %d is not in valid range [%d-%d]\n",
1619 		    mtu, IAVF_MIN_MTU, IAVF_MAX_MTU);
1620 		return (EINVAL);
1621 	}
1622 
1623 	vsi->shared->isc_max_frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1624 		ETHER_VLAN_ENCAP_LEN;
1625 
1626 	return (0);
1627 }
1628 
1629 /**
1630  * iavf_if_media_status - Report current media status
1631  * @ctx: iflib context
1632  * @ifmr: ifmedia request structure
1633  *
1634  * Called by iflib to report the current media status in the ifmr.
1635  */
1636 static void
1637 iavf_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
1638 {
1639 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1640 
1641 	iavf_media_status_common(sc, ifmr);
1642 }
1643 
1644 /**
1645  * iavf_if_media_change - Change the current media settings
1646  * @ctx: iflib context
1647  *
1648  * Called by iflib to change the current media settings.
1649  *
1650  * @returns zero on success, or an error code on failure.
1651  */
1652 static int
1653 iavf_if_media_change(if_ctx_t ctx)
1654 {
1655 	return iavf_media_change_common(iflib_get_ifp(ctx));
1656 }
1657 
1658 /**
1659  * iavf_if_promisc_set - Set device promiscuous mode
1660  * @ctx: iflib context
1661  * @flags: promiscuous configuration
1662  *
1663  * Called by iflib to request that the device enter promiscuous mode.
1664  *
1665  * @returns zero on success, or an error code on failure.
1666  */
1667 static int
1668 iavf_if_promisc_set(if_ctx_t ctx, int flags)
1669 {
1670 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1671 
1672 	return iavf_config_promisc(sc, flags);
1673 }
1674 
1675 /**
1676  * iavf_if_timer - Periodic timer called by iflib
1677  * @ctx: iflib context
1678  * @qid: The queue being triggered
1679  *
1680  * Called by iflib periodically as a timer task, so that the driver can handle
1681  * periodic work.
1682  *
1683  * @remark this timer is only called while the interface is up, even if
1684  * IFLIB_ADMIN_ALWAYS_RUN is set.
1685  */
1686 static void
1687 iavf_if_timer(if_ctx_t ctx, uint16_t qid)
1688 {
1689 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1690 	struct iavf_hw *hw = &sc->hw;
1691 	u32 val;
1692 
1693 	if (qid != 0)
1694 		return;
1695 
1696 	/* Check for a PF-triggered VF reset or a dead admin send queue. */
1697 	val = rd32(hw, IAVF_VFGEN_RSTAT) &
1698 	    IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1699 	if (iavf_test_state(&sc->state, IAVF_STATE_RESET_PENDING) ||
1700 	    !iavf_check_asq_alive(hw) ||
1701 	    (val != VIRTCHNL_VFR_VFACTIVE &&
1702 	    val != VIRTCHNL_VFR_COMPLETED)) {
1703 		iavf_dbg_info(sc, "PF mailbox unavailable (reset state %d)\n",
1704 		    val);
1705 		iavf_mbx_lost(sc);
1706 		return;
1707 	}
1708 
1709 	/* Fire off the adminq task */
1710 	iflib_admin_intr_deferred(ctx);
1711 
1712 	/* Update stats */
1713 	iavf_request_stats(sc);
1714 }
1715 
1716 /**
1717  * iavf_if_vlan_register - Register a VLAN
1718  * @ctx: iflib context
1719  * @vtag: the VLAN to register
1720  *
1721  * Register a VLAN filter for a given vtag.
1722  */
1723 static void
1724 iavf_if_vlan_register(if_ctx_t ctx, u16 vtag)
1725 {
1726 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1727 	struct iavf_vsi *vsi = &sc->vsi;
1728 
1729 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
1730 		return;
1731 
1732 	/* Add VLAN 0 to list, for untagged traffic */
1733 	if (vsi->num_vlans == 0)
1734 		iavf_add_vlan_filter(sc, 0);
1735 
1736 	iavf_add_vlan_filter(sc, vtag);
1737 
1738 	++vsi->num_vlans;
1739 
1740 	iavf_send_vc_msg(sc, IAVF_FLAG_AQ_ADD_VLAN_FILTER);
1741 }
1742 
1743 /**
1744  * iavf_if_vlan_unregister - Unregister a VLAN
1745  * @ctx: iflib context
1746  * @vtag: the VLAN to remove
1747  *
1748  * Unregister (remove) a VLAN filter for the given vtag.
1749  */
1750 static void
1751 iavf_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
1752 {
1753 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1754 	struct iavf_vsi *vsi = &sc->vsi;
1755 	int i = 0;
1756 
1757 	if ((vtag == 0) || (vtag > 4095) || (vsi->num_vlans == 0))	/* Invalid */
1758 		return;
1759 
1760 	i = iavf_mark_del_vlan_filter(sc, vtag);
1761 	vsi->num_vlans -= i;
1762 
1763 	/* Remove VLAN filter 0 if the last VLAN is being removed */
1764 	if (vsi->num_vlans == 0)
1765 		i += iavf_mark_del_vlan_filter(sc, 0);
1766 
1767 	if (i > 0)
1768 		iavf_send_vc_msg(sc, IAVF_FLAG_AQ_DEL_VLAN_FILTER);
1769 }
1770 
1771 /**
1772  * iavf_if_get_counter - Get network statistic counters
1773  * @ctx: iflib context
1774  * @cnt: The counter to obtain
1775  *
1776  * Called by iflib to obtain the value of the specified counter.
1777  *
1778  * @returns the uint64_t counter value.
1779  */
1780 static uint64_t
1781 iavf_if_get_counter(if_ctx_t ctx, ift_counter cnt)
1782 {
1783 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1784 	struct iavf_vsi *vsi = &sc->vsi;
1785 	if_t ifp = iflib_get_ifp(ctx);
1786 
1787 	switch (cnt) {
1788 	case IFCOUNTER_IPACKETS:
1789 		return (vsi->ipackets);
1790 	case IFCOUNTER_IERRORS:
1791 		return (vsi->ierrors);
1792 	case IFCOUNTER_OPACKETS:
1793 		return (vsi->opackets);
1794 	case IFCOUNTER_OERRORS:
1795 		return (vsi->oerrors);
1796 	case IFCOUNTER_COLLISIONS:
1797 		/* Collisions are by standard impossible in 40G/10G Ethernet */
1798 		return (0);
1799 	case IFCOUNTER_IBYTES:
1800 		return (vsi->ibytes);
1801 	case IFCOUNTER_OBYTES:
1802 		return (vsi->obytes);
1803 	case IFCOUNTER_IMCASTS:
1804 		return (vsi->imcasts);
1805 	case IFCOUNTER_OMCASTS:
1806 		return (vsi->omcasts);
1807 	case IFCOUNTER_IQDROPS:
1808 		return (vsi->iqdrops);
1809 	case IFCOUNTER_OQDROPS:
1810 		return (vsi->oqdrops);
1811 	case IFCOUNTER_NOPROTO:
1812 		return (vsi->noproto);
1813 	default:
1814 		return (if_get_counter_default(ifp, cnt));
1815 	}
1816 }
1817 
1818 /* iavf_if_needs_restart - Tell iflib when the driver needs to be reinitialized
1819  * @ctx: iflib context
1820  * @event: event code to check
1821  *
1822  * Defaults to returning false for unknown events.
1823  *
1824  * @returns true if iflib needs to reinit the interface
1825  */
1826 static bool
1827 iavf_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
1828 {
1829 	switch (event) {
1830 	case IFLIB_RESTART_VLAN_CONFIG:
1831 		return (true);
1832 	default:
1833 		return (false);
1834 	}
1835 }
1836 
1837 /**
1838  * iavf_free_pci_resources - Free PCI resources
1839  * @sc: device softc
1840  *
1841  * Called to release the PCI resources allocated during attach. May be called
1842  * in the error flow of attach_pre, or during detach as part of cleanup.
1843  */
1844 static void
1845 iavf_free_pci_resources(struct iavf_sc *sc)
1846 {
1847 	struct iavf_vsi		*vsi = &sc->vsi;
1848 	struct iavf_rx_queue	*rx_que = vsi->rx_queues;
1849 	device_t                dev = sc->dev;
1850 
1851 	/* We may get here before stations are set up */
1852 	if (rx_que == NULL)
1853 		goto early;
1854 
1855 	/* Release all interrupts */
1856 	iflib_irq_free(vsi->ctx, &vsi->irq);
1857 
1858 	for (int i = 0; i < vsi->num_rx_queues; i++, rx_que++)
1859 		iflib_irq_free(vsi->ctx, &rx_que->que_irq);
1860 
1861 early:
1862 	if (sc->pci_mem != NULL)
1863 		bus_release_resource(dev, SYS_RES_MEMORY,
1864 		    rman_get_rid(sc->pci_mem), sc->pci_mem);
1865 }
1866 
1867 /**
1868  * iavf_setup_interface - Setup the device interface
1869  * @sc: device softc
1870  *
1871  * Called to setup some device interface settings, such as the ifmedia
1872  * structure.
1873  */
1874 static void
1875 iavf_setup_interface(struct iavf_sc *sc)
1876 {
1877 	struct iavf_vsi *vsi = &sc->vsi;
1878 	if_ctx_t ctx = vsi->ctx;
1879 	if_t ifp = iflib_get_ifp(ctx);
1880 
1881 	iavf_dbg_init(sc, "begin\n");
1882 
1883 	vsi->shared->isc_max_frame_size =
1884 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN
1885 	    + ETHER_VLAN_ENCAP_LEN;
1886 
1887 	iavf_set_initial_baudrate(ifp);
1888 
1889 	ifmedia_add(sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1890 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
1891 }
1892 
1893 /**
1894  * iavf_msix_adminq - Admin Queue interrupt handler
1895  * @arg: void pointer to the device softc
1896  *
1897  * Interrupt handler for the non-queue interrupt causes. Primarily this will
1898  * be the adminq interrupt, but also includes other miscellaneous causes.
1899  *
1900  * @returns FILTER_SCHEDULE_THREAD if the admin task needs to be run, otherwise
1901  * returns FITLER_HANDLED.
1902  */
1903 static int
1904 iavf_msix_adminq(void *arg)
1905 {
1906 	struct iavf_sc	*sc = (struct iavf_sc *)arg;
1907 	struct iavf_hw	*hw = &sc->hw;
1908 	u32		reg, mask;
1909 
1910 	++sc->admin_irq;
1911 
1912 	if (!iavf_test_state(&sc->state, IAVF_STATE_INITIALIZED))
1913 		return (FILTER_HANDLED);
1914 
1915         reg = rd32(hw, IAVF_VFINT_ICR01);
1916 	/*
1917 	 * For masking off interrupt causes that need to be handled before
1918 	 * they can be re-enabled
1919 	 */
1920         mask = rd32(hw, IAVF_VFINT_ICR0_ENA1);
1921 
1922 	/* Check on the cause */
1923 	if (reg & IAVF_VFINT_ICR01_ADMINQ_MASK) {
1924 		mask &= ~IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK;
1925 
1926 		/* Process messages outside of the iflib context lock */
1927 		taskqueue_enqueue(sc->vc_tq, &sc->vc_task);
1928 	}
1929 
1930 	wr32(hw, IAVF_VFINT_ICR0_ENA1, mask);
1931 	iavf_enable_adminq_irq(hw);
1932 
1933 	return (FILTER_HANDLED);
1934 }
1935 
1936 /**
1937  * iavf_enable_intr - Enable device interrupts
1938  * @vsi: the main VSI
1939  *
1940  * Called to enable all queue interrupts.
1941  */
1942 void
1943 iavf_enable_intr(struct iavf_vsi *vsi)
1944 {
1945 	struct iavf_hw *hw = vsi->hw;
1946 	struct iavf_rx_queue *que = vsi->rx_queues;
1947 
1948 	iavf_enable_adminq_irq(hw);
1949 	for (int i = 0; i < vsi->num_rx_queues; i++, que++)
1950 		iavf_enable_queue_irq(hw, que->rxr.me);
1951 }
1952 
1953 /**
1954  * iavf_disable_intr - Disable device interrupts
1955  * @vsi: the main VSI
1956  *
1957  * Called to disable all interrupts
1958  *
1959  * @remark we never disable the admin status interrupt.
1960  */
1961 void
1962 iavf_disable_intr(struct iavf_vsi *vsi)
1963 {
1964         struct iavf_hw *hw = vsi->hw;
1965         struct iavf_rx_queue *que = vsi->rx_queues;
1966 
1967 	for (int i = 0; i < vsi->num_rx_queues; i++, que++)
1968 		iavf_disable_queue_irq(hw, que->rxr.me);
1969 }
1970 
1971 /**
1972  * iavf_enable_queue_irq - Enable IRQ register for a queue interrupt
1973  * @hw: hardware structure
1974  * @id: IRQ vector to enable
1975  *
1976  * Writes the IAVF_VFINT_DYN_CTLN1 register to enable a given IRQ interrupt.
1977  */
1978 static void
1979 iavf_enable_queue_irq(struct iavf_hw *hw, int id)
1980 {
1981 	u32		reg;
1982 
1983 	reg = IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1984 	    IAVF_VFINT_DYN_CTLN1_CLEARPBA_MASK |
1985 	    IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK;
1986 	wr32(hw, IAVF_VFINT_DYN_CTLN1(id), reg);
1987 }
1988 
1989 /**
1990  * iavf_disable_queue_irq - Disable IRQ register for a queue interrupt
1991  * @hw: hardware structure
1992  * @id: IRQ vector to disable
1993  *
1994  * Writes the IAVF_VFINT_DYN_CTLN1 register to disable a given IRQ interrupt.
1995  */
1996 static void
1997 iavf_disable_queue_irq(struct iavf_hw *hw, int id)
1998 {
1999 	wr32(hw, IAVF_VFINT_DYN_CTLN1(id),
2000 	    IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
2001 	rd32(hw, IAVF_VFGEN_RSTAT);
2002 }
2003 
2004 /**
2005  * iavf_configure_itr - Get initial ITR values from tunable values.
2006  * @sc: device softc
2007  *
2008  * Load the initial tunable values for the ITR configuration.
2009  */
2010 static void
2011 iavf_configure_itr(struct iavf_sc *sc)
2012 {
2013 	iavf_configure_tx_itr(sc);
2014 	iavf_configure_rx_itr(sc);
2015 }
2016 
2017 /**
2018  * iavf_set_queue_rx_itr - Update Rx ITR value
2019  * @que: Rx queue to update
2020  *
2021  * Provide a update to the queue RX interrupt moderation value.
2022  */
2023 static void
2024 iavf_set_queue_rx_itr(struct iavf_rx_queue *que)
2025 {
2026 	struct iavf_vsi	*vsi = que->vsi;
2027 	struct iavf_hw	*hw = vsi->hw;
2028 	struct rx_ring	*rxr = &que->rxr;
2029 
2030 	/* Idle, do nothing */
2031 	if (rxr->bytes == 0)
2032 		return;
2033 
2034 	/* Update the hardware if needed */
2035 	if (rxr->itr != vsi->rx_itr_setting) {
2036 		rxr->itr = vsi->rx_itr_setting;
2037 		wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR,
2038 		    que->rxr.me), rxr->itr);
2039 	}
2040 }
2041 
2042 /**
2043  * iavf_msix_que - Main Rx queue interrupt handler
2044  * @arg: void pointer to the Rx queue
2045  *
2046  * Main MSI-X interrupt handler for Rx queue interrupts
2047  *
2048  * @returns FILTER_SCHEDULE_THREAD if the main thread for Rx needs to run,
2049  * otherwise returns FILTER_HANDLED.
2050  */
2051 static int
2052 iavf_msix_que(void *arg)
2053 {
2054 	struct iavf_rx_queue *rx_que = (struct iavf_rx_queue *)arg;
2055 	struct iavf_sc *sc = rx_que->vsi->back;
2056 
2057 	++rx_que->irqs;
2058 
2059 	if (!iavf_test_state(&sc->state, IAVF_STATE_RUNNING))
2060 		return (FILTER_HANDLED);
2061 
2062 	iavf_set_queue_rx_itr(rx_que);
2063 
2064 	return (FILTER_SCHEDULE_THREAD);
2065 }
2066 
2067 /**
2068  * iavf_update_link_status - Update iflib Link status
2069  * @sc: device softc
2070  *
2071  * Notify the iflib stack of changes in link status. Called after the device
2072  * receives a virtchnl message indicating a change in link status.
2073  */
2074 void
2075 iavf_update_link_status(struct iavf_sc *sc)
2076 {
2077 	struct iavf_vsi *vsi = &sc->vsi;
2078 	if_t ifp;
2079 	u64 baudrate;
2080 
2081 	ifp = iflib_get_ifp(vsi->ctx);
2082 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
2083 		if (vsi->link_active) {
2084 			vsi->link_active = false;
2085 			iflib_link_state_change(vsi->ctx, LINK_STATE_DOWN, 0);
2086 		}
2087 		return;
2088 	}
2089 
2090 	if (sc->link_up){
2091 		if (vsi->link_active == FALSE) {
2092 			vsi->link_active = TRUE;
2093 			baudrate = iavf_baudrate_from_link_speed(sc);
2094 			iavf_dbg_info(sc, "baudrate: %llu\n", (unsigned long long)baudrate);
2095 			iflib_link_state_change(vsi->ctx, LINK_STATE_UP, baudrate);
2096 		}
2097 	} else { /* Link down */
2098 		if (vsi->link_active == TRUE) {
2099 			vsi->link_active = FALSE;
2100 			iflib_link_state_change(vsi->ctx, LINK_STATE_DOWN, 0);
2101 		}
2102 	}
2103 }
2104 
2105 /**
2106  * iavf_stop - Stop the interface
2107  * @sc: device softc
2108  *
2109  * This routine disables all traffic on the adapter by disabling interrupts
2110  * and sending a message to the PF to tell it to stop the hardware
2111  * Tx/Rx LAN queues.
2112  */
2113 static void
2114 iavf_stop(struct iavf_sc *sc)
2115 {
2116 	struct iavf_vsi *vsi;
2117 	bool mailbox_ready;
2118 
2119 	vsi = &sc->vsi;
2120 	iavf_mbx_retry_stop(sc);
2121 	iavf_clear_state(&sc->state, IAVF_STATE_RUNNING);
2122 
2123 	iavf_disable_intr(vsi);
2124 
2125 	mailbox_ready = atomic_load_acq_32(&sc->mbx_ready) != 0;
2126 	if (mailbox_ready && iavf_reset_is_complete(&sc->hw) &&
2127 	    iavf_disable_queues_with_retries(sc) != 0)
2128 		mailbox_ready = false;
2129 	atomic_store_rel_32(&sc->mbx_ready, 0);
2130 	if (!mailbox_ready) {
2131 		atomic_store_rel_32(&sc->vc_reinit_required, 1);
2132 		iavf_dbg_vc(sc, "PF mailbox unavailable while stopping\n");
2133 	}
2134 	sc->link_up = false;
2135 	if (vsi->link_active) {
2136 		vsi->link_active = false;
2137 		iflib_link_state_change(vsi->ctx, LINK_STATE_DOWN, 0);
2138 	}
2139 }
2140 
2141 /**
2142  * iavf_if_stop - iflib stop handler
2143  * @ctx: iflib context
2144  *
2145  * Call iavf_stop to stop the interface.
2146  */
2147 static void
2148 iavf_if_stop(if_ctx_t ctx)
2149 {
2150 	struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
2151 
2152 	iavf_stop(sc);
2153 }
2154 
2155 /**
2156  * iavf_del_mac_filter - Delete a MAC filter
2157  * @sc: device softc
2158  * @macaddr: MAC address to remove
2159  *
2160  * Marks a MAC filter for deletion.
2161  *
2162  * @returns zero if the filter existed, or ENOENT if it did not.
2163  */
2164 static int
2165 iavf_del_mac_filter(struct iavf_sc *sc, u8 *macaddr)
2166 {
2167 	struct iavf_mac_filter	*f;
2168 
2169 	f = iavf_find_mac_filter(sc, macaddr);
2170 	if (f == NULL)
2171 		return (ENOENT);
2172 
2173 	f->flags |= IAVF_FILTER_DEL;
2174 	return (0);
2175 }
2176 
2177 /**
2178  * iavf_init_tx_rsqs - Initialize Report Status array
2179  * @vsi: the main VSI
2180  *
2181  * Set the Report Status queue fields to zero in order to initialize the
2182  * queues for transmit.
2183  */
2184 void
2185 iavf_init_tx_rsqs(struct iavf_vsi *vsi)
2186 {
2187 	if_softc_ctx_t scctx = vsi->shared;
2188 	struct iavf_tx_queue *tx_que;
2189 	int i, j;
2190 
2191 	for (i = 0, tx_que = vsi->tx_queues; i < vsi->num_tx_queues; i++, tx_que++) {
2192 		struct tx_ring *txr = &tx_que->txr;
2193 
2194 		txr->tx_rs_cidx = txr->tx_rs_pidx;
2195 
2196 		/* Initialize the last processed descriptor to be the end of
2197 		 * the ring, rather than the start, so that we avoid an
2198 		 * off-by-one error when calculating how many descriptors are
2199 		 * done in the credits_update function.
2200 		 */
2201 		txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
2202 
2203 		for (j = 0; j < scctx->isc_ntxd[0]; j++)
2204 			txr->tx_rsq[j] = QIDX_INVALID;
2205 	}
2206 }
2207 
2208 /**
2209  * iavf_init_tx_cidx - Initialize Tx cidx values
2210  * @vsi: the main VSI
2211  *
2212  * Initialize the tx_cidx_processed values for Tx queues in order to
2213  * initialize the Tx queues for transmit.
2214  */
2215 void
2216 iavf_init_tx_cidx(struct iavf_vsi *vsi)
2217 {
2218 	if_softc_ctx_t scctx = vsi->shared;
2219 	struct iavf_tx_queue *tx_que;
2220 	int i;
2221 
2222 	for (i = 0, tx_que = vsi->tx_queues; i < vsi->num_tx_queues; i++, tx_que++) {
2223 		struct tx_ring *txr = &tx_que->txr;
2224 
2225 		txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
2226 	}
2227 }
2228 
2229 /**
2230  * iavf_add_device_sysctls - Add device sysctls for configuration
2231  * @sc: device softc
2232  *
2233  * Add the main sysctl nodes and sysctls for device configuration.
2234  */
2235 static void
2236 iavf_add_device_sysctls(struct iavf_sc *sc)
2237 {
2238 	struct iavf_vsi *vsi = &sc->vsi;
2239 	device_t dev = sc->dev;
2240 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
2241 	struct sysctl_oid_list *debug_list;
2242 
2243 	iavf_add_device_sysctls_common(sc);
2244 
2245 	debug_list = iavf_create_debug_sysctl_tree(sc);
2246 
2247 	iavf_add_debug_sysctls_common(sc, debug_list);
2248 
2249 	SYSCTL_ADD_PROC(ctx, debug_list,
2250 	    OID_AUTO, "queue_interrupt_table", CTLTYPE_STRING | CTLFLAG_RD,
2251 	    sc, 0, iavf_sysctl_queue_interrupt_table, "A", "View MSI-X indices for TX/RX queues");
2252 
2253 #ifdef IAVF_DEBUG
2254 	SYSCTL_ADD_PROC(ctx, debug_list,
2255 	    OID_AUTO, "do_vf_reset", CTLTYPE_INT | CTLFLAG_WR,
2256 	    sc, 0, iavf_sysctl_vf_reset, "A", "Request a VF reset from PF");
2257 
2258 	SYSCTL_ADD_PROC(ctx, debug_list,
2259 	    OID_AUTO, "do_vflr_reset", CTLTYPE_INT | CTLFLAG_WR,
2260 	    sc, 0, iavf_sysctl_vflr_reset, "A", "Request a VFLR reset from HW");
2261 #endif
2262 
2263 	/* Add stats sysctls */
2264 	iavf_add_vsi_sysctls(dev, vsi, ctx, "vsi");
2265 
2266 	iavf_add_queues_sysctls(dev, vsi);
2267 }
2268 
2269 /**
2270  * iavf_add_queues_sysctls - Add per-queue sysctls
2271  * @dev: device pointer
2272  * @vsi: the main VSI
2273  *
2274  * Add sysctls for each Tx and Rx queue.
2275  */
2276 void
2277 iavf_add_queues_sysctls(device_t dev, struct iavf_vsi *vsi)
2278 {
2279 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
2280 	struct sysctl_oid_list *vsi_list, *queue_list;
2281 	struct sysctl_oid *queue_node;
2282 	char queue_namebuf[32];
2283 
2284 	struct iavf_rx_queue *rx_que;
2285 	struct iavf_tx_queue *tx_que;
2286 	struct tx_ring *txr;
2287 	struct rx_ring *rxr;
2288 
2289 	vsi_list = SYSCTL_CHILDREN(vsi->vsi_node);
2290 
2291 	/* Queue statistics */
2292 	for (int q = 0; q < vsi->num_rx_queues; q++) {
2293 		bzero(queue_namebuf, sizeof(queue_namebuf));
2294 		snprintf(queue_namebuf, IAVF_QUEUE_NAME_LEN, "rxq%02d", q);
2295 		queue_node = SYSCTL_ADD_NODE(ctx, vsi_list,
2296 		    OID_AUTO, queue_namebuf, CTLFLAG_RD, NULL, "RX Queue #");
2297 		queue_list = SYSCTL_CHILDREN(queue_node);
2298 
2299 		rx_que = &(vsi->rx_queues[q]);
2300 		rxr = &(rx_que->rxr);
2301 
2302 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "irqs",
2303 				CTLFLAG_RD, &(rx_que->irqs),
2304 				"irqs on this queue (both Tx and Rx)");
2305 
2306 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "packets",
2307 				CTLFLAG_RD, &(rxr->rx_packets),
2308 				"Queue Packets Received");
2309 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "bytes",
2310 				CTLFLAG_RD, &(rxr->rx_bytes),
2311 				"Queue Bytes Received");
2312 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "desc_err",
2313 				CTLFLAG_RD, &(rxr->desc_errs),
2314 				"Queue Rx Descriptor Errors");
2315 		SYSCTL_ADD_UINT(ctx, queue_list, OID_AUTO, "itr",
2316 				CTLFLAG_RD, &(rxr->itr), 0,
2317 				"Queue Rx ITR Interval");
2318 	}
2319 	for (int q = 0; q < vsi->num_tx_queues; q++) {
2320 		bzero(queue_namebuf, sizeof(queue_namebuf));
2321 		snprintf(queue_namebuf, IAVF_QUEUE_NAME_LEN, "txq%02d", q);
2322 		queue_node = SYSCTL_ADD_NODE(ctx, vsi_list,
2323 		    OID_AUTO, queue_namebuf, CTLFLAG_RD, NULL, "TX Queue #");
2324 		queue_list = SYSCTL_CHILDREN(queue_node);
2325 
2326 		tx_que = &(vsi->tx_queues[q]);
2327 		txr = &(tx_que->txr);
2328 
2329 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tso",
2330 				CTLFLAG_RD, &(tx_que->tso),
2331 				"TSO");
2332 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "mss_too_small",
2333 				CTLFLAG_RD, &(txr->mss_too_small),
2334 				"TSO sends with an MSS less than 64");
2335 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "packets",
2336 				CTLFLAG_RD, &(txr->tx_packets),
2337 				"Queue Packets Transmitted");
2338 		SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "bytes",
2339 				CTLFLAG_RD, &(txr->tx_bytes),
2340 				"Queue Bytes Transmitted");
2341 		SYSCTL_ADD_UINT(ctx, queue_list, OID_AUTO, "itr",
2342 				CTLFLAG_RD, &(txr->itr), 0,
2343 				"Queue Tx ITR Interval");
2344 	}
2345 }
2346 
2347 /**
2348  * iavf_driver_is_detaching - Check if the driver is detaching/unloading
2349  * @sc: device private softc
2350  *
2351  * @returns true if the driver is detaching, false otherwise.
2352  *
2353  * @remark on newer kernels, take advantage of iflib_in_detach in order to
2354  * report detachment correctly as early as possible.
2355  *
2356  * @remark this function is used by various code paths that want to avoid
2357  * running if the driver is about to be removed. This includes sysctls and
2358  * other driver access points. Note that it does not fully resolve
2359  * detach-based race conditions as it is possible for a thread to race with
2360  * iflib_in_detach.
2361  */
2362 bool
2363 iavf_driver_is_detaching(struct iavf_sc *sc)
2364 {
2365 	return (!iavf_test_state(&sc->state, IAVF_STATE_INITIALIZED) ||
2366 		iflib_in_detach(sc->vsi.ctx));
2367 }
2368 
2369 /**
2370  * iavf_sysctl_queue_interrupt_table - Sysctl for displaying Tx queue mapping
2371  * @oidp: sysctl oid structure
2372  * @arg1: void pointer to device softc
2373  * @arg2: unused
2374  * @req: sysctl request pointer
2375  *
2376  * Print out mapping of TX queue indexes and Rx queue indexes to MSI-X vectors.
2377  *
2378  * @returns zero on success, or an error code on failure.
2379  */
2380 static int
2381 iavf_sysctl_queue_interrupt_table(SYSCTL_HANDLER_ARGS)
2382 {
2383 	struct iavf_sc *sc = (struct iavf_sc *)arg1;
2384 	struct iavf_vsi *vsi = &sc->vsi;
2385 	device_t dev = sc->dev;
2386 	struct sbuf *buf;
2387 	int error = 0;
2388 
2389 	struct iavf_rx_queue *rx_que;
2390 	struct iavf_tx_queue *tx_que;
2391 
2392 	UNREFERENCED_2PARAMETER(arg2, oidp);
2393 
2394 	if (iavf_driver_is_detaching(sc))
2395 		return (ESHUTDOWN);
2396 
2397 	buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
2398 	if (!buf) {
2399 		device_printf(dev, "Could not allocate sbuf for output.\n");
2400 		return (ENOMEM);
2401 	}
2402 
2403 	sbuf_cat(buf, "\n");
2404 	for (int i = 0; i < vsi->num_rx_queues; i++) {
2405 		rx_que = &vsi->rx_queues[i];
2406 		sbuf_printf(buf, "(rxq %3d): %d\n", i, rx_que->msix);
2407 	}
2408 	for (int i = 0; i < vsi->num_tx_queues; i++) {
2409 		tx_que = &vsi->tx_queues[i];
2410 		sbuf_printf(buf, "(txq %3d): %d\n", i, tx_que->msix);
2411 	}
2412 
2413 	error = sbuf_finish(buf);
2414 	if (error)
2415 		device_printf(dev, "Error finishing sbuf: %d\n", error);
2416 	sbuf_delete(buf);
2417 
2418 	return (error);
2419 }
2420 
2421 #ifdef IAVF_DEBUG
2422 #define CTX_ACTIVE(ctx) ((if_getdrvflags(iflib_get_ifp(ctx)) & IFF_DRV_RUNNING))
2423 
2424 /**
2425  * iavf_sysctl_vf_reset - Request a VF reset
2426  * @oidp: sysctl oid pointer
2427  * @arg1: void pointer to device softc
2428  * @arg2: unused
2429  * @req: sysctl request pointer
2430  *
2431  * Request a VF reset for the device.
2432  *
2433  * @returns zero on success, or an error code on failure.
2434  */
2435 static int
2436 iavf_sysctl_vf_reset(SYSCTL_HANDLER_ARGS)
2437 {
2438 	struct iavf_sc *sc = (struct iavf_sc *)arg1;
2439 	int do_reset = 0, error = 0;
2440 
2441 	UNREFERENCED_PARAMETER(arg2);
2442 
2443 	if (iavf_driver_is_detaching(sc))
2444 		return (ESHUTDOWN);
2445 
2446 	error = sysctl_handle_int(oidp, &do_reset, 0, req);
2447 	if ((error) || (req->newptr == NULL))
2448 		return (error);
2449 
2450 	if (do_reset == 1) {
2451 		iavf_reset(sc);
2452 		if (CTX_ACTIVE(sc->vsi.ctx))
2453 			iflib_request_reset(sc->vsi.ctx);
2454 	}
2455 
2456 	return (error);
2457 }
2458 
2459 /**
2460  * iavf_sysctl_vflr_reset - Trigger a PCIe FLR for the device
2461  * @oidp: sysctl oid pointer
2462  * @arg1: void pointer to device softc
2463  * @arg2: unused
2464  * @req: sysctl request pointer
2465  *
2466  * Sysctl callback to trigger a PCIe FLR.
2467  *
2468  * @returns zero on success, or an error code on failure.
2469  */
2470 static int
2471 iavf_sysctl_vflr_reset(SYSCTL_HANDLER_ARGS)
2472 {
2473 	struct iavf_sc *sc = (struct iavf_sc *)arg1;
2474 	device_t dev = sc->dev;
2475 	int do_reset = 0, error = 0;
2476 
2477 	UNREFERENCED_PARAMETER(arg2);
2478 
2479 	if (iavf_driver_is_detaching(sc))
2480 		return (ESHUTDOWN);
2481 
2482 	error = sysctl_handle_int(oidp, &do_reset, 0, req);
2483 	if ((error) || (req->newptr == NULL))
2484 		return (error);
2485 
2486 	if (do_reset == 1) {
2487 		if (!pcie_flr(dev, max(pcie_get_max_completion_timeout(dev) / 1000, 10), true)) {
2488 			device_printf(dev, "PCIE FLR failed\n");
2489 			error = EIO;
2490 		}
2491 		else if (CTX_ACTIVE(sc->vsi.ctx))
2492 			iflib_request_reset(sc->vsi.ctx);
2493 	}
2494 
2495 	return (error);
2496 }
2497 #undef CTX_ACTIVE
2498 #endif
2499