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