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