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 /*********************************************************************
48 * Function prototypes
49 *********************************************************************/
50 static void *iavf_register(device_t dev);
51 static int iavf_if_attach_pre(if_ctx_t ctx);
52 static int iavf_if_attach_post(if_ctx_t ctx);
53 static int iavf_if_detach(if_ctx_t ctx);
54 static int iavf_if_shutdown(if_ctx_t ctx);
55 static int iavf_if_suspend(if_ctx_t ctx);
56 static int iavf_if_resume(if_ctx_t ctx);
57 static int iavf_if_msix_intr_assign(if_ctx_t ctx, int msix);
58 static void iavf_if_enable_intr(if_ctx_t ctx);
59 static void iavf_if_disable_intr(if_ctx_t ctx);
60 static int iavf_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
61 static int iavf_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
62 static int iavf_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets);
63 static int iavf_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nqs, int nqsets);
64 static void iavf_if_queues_free(if_ctx_t ctx);
65 static void iavf_if_update_admin_status(if_ctx_t ctx);
66 static void iavf_if_multi_set(if_ctx_t ctx);
67 static int iavf_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
68 static void iavf_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
69 static int iavf_if_media_change(if_ctx_t ctx);
70 static int iavf_if_promisc_set(if_ctx_t ctx, int flags);
71 static void iavf_if_timer(if_ctx_t ctx, uint16_t qid);
72 static void iavf_if_vlan_register(if_ctx_t ctx, u16 vtag);
73 static void iavf_if_vlan_unregister(if_ctx_t ctx, u16 vtag);
74 static uint64_t iavf_if_get_counter(if_ctx_t ctx, ift_counter cnt);
75 static void iavf_if_init(if_ctx_t ctx);
76 static void iavf_if_stop(if_ctx_t ctx);
77 static bool iavf_if_needs_restart(if_ctx_t, enum iflib_restart_event);
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 DRIVER_MODULE(iavf, pci, iavf_driver, 0, 0);
129 MODULE_VERSION(iavf, 1);
130
131 MODULE_DEPEND(iavf, pci, 1, 1, 1);
132 MODULE_DEPEND(iavf, ether, 1, 1, 1);
133 MODULE_DEPEND(iavf, iflib, 1, 1, 1);
134
135 IFLIB_PNP_INFO(pci, iavf, iavf_vendor_info_array);
136
137 /**
138 * @var M_IAVF
139 * @brief main iavf driver allocation type
140 *
141 * malloc(9) allocation type used by the majority of memory allocations in the
142 * iavf iflib driver.
143 */
144 MALLOC_DEFINE(M_IAVF, "iavf", "iavf driver allocations");
145
146 static device_method_t iavf_if_methods[] = {
147 DEVMETHOD(ifdi_attach_pre, iavf_if_attach_pre),
148 DEVMETHOD(ifdi_attach_post, iavf_if_attach_post),
149 DEVMETHOD(ifdi_detach, iavf_if_detach),
150 DEVMETHOD(ifdi_shutdown, iavf_if_shutdown),
151 DEVMETHOD(ifdi_suspend, iavf_if_suspend),
152 DEVMETHOD(ifdi_resume, iavf_if_resume),
153 DEVMETHOD(ifdi_init, iavf_if_init),
154 DEVMETHOD(ifdi_stop, iavf_if_stop),
155 DEVMETHOD(ifdi_msix_intr_assign, iavf_if_msix_intr_assign),
156 DEVMETHOD(ifdi_intr_enable, iavf_if_enable_intr),
157 DEVMETHOD(ifdi_intr_disable, iavf_if_disable_intr),
158 DEVMETHOD(ifdi_rx_queue_intr_enable, iavf_if_rx_queue_intr_enable),
159 DEVMETHOD(ifdi_tx_queue_intr_enable, iavf_if_tx_queue_intr_enable),
160 DEVMETHOD(ifdi_tx_queues_alloc, iavf_if_tx_queues_alloc),
161 DEVMETHOD(ifdi_rx_queues_alloc, iavf_if_rx_queues_alloc),
162 DEVMETHOD(ifdi_queues_free, iavf_if_queues_free),
163 DEVMETHOD(ifdi_update_admin_status, iavf_if_update_admin_status),
164 DEVMETHOD(ifdi_multi_set, iavf_if_multi_set),
165 DEVMETHOD(ifdi_mtu_set, iavf_if_mtu_set),
166 DEVMETHOD(ifdi_media_status, iavf_if_media_status),
167 DEVMETHOD(ifdi_media_change, iavf_if_media_change),
168 DEVMETHOD(ifdi_promisc_set, iavf_if_promisc_set),
169 DEVMETHOD(ifdi_timer, iavf_if_timer),
170 DEVMETHOD(ifdi_vlan_register, iavf_if_vlan_register),
171 DEVMETHOD(ifdi_vlan_unregister, iavf_if_vlan_unregister),
172 DEVMETHOD(ifdi_get_counter, iavf_if_get_counter),
173 DEVMETHOD(ifdi_needs_restart, iavf_if_needs_restart),
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 *
iavf_register(device_t dev __unused)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
iavf_allocate_pci_resources(struct iavf_sc * sc)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
iavf_if_attach_pre(if_ctx_t ctx)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 = pci_msix_table_bar(dev);
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
iavf_vc_task(void * arg,int pending __unused)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
iavf_setup_vc_tq(struct iavf_sc * sc)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
iavf_if_attach_post(if_ctx_t ctx)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
iavf_if_detach(if_ctx_t ctx)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
iavf_if_shutdown(if_ctx_t ctx __unused)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
iavf_if_suspend(if_ctx_t ctx __unused)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
iavf_if_resume(if_ctx_t ctx __unused)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
iavf_vc_sleep_wait(struct iavf_sc * sc,u32 op)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
iavf_send_vc_msg_sleep(struct iavf_sc * sc,u32 op)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
iavf_send_vc_msg(struct iavf_sc * sc,u32 op)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
iavf_init_queues(struct iavf_vsi * vsi)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
iavf_if_init(if_ctx_t ctx)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 sx_assert(iflib_ctx_lock_get(ctx), SA_XLOCKED);
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_getlladdr(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
iavf_if_msix_intr_assign(if_ctx_t ctx,int msix __unused)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 (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
iavf_if_enable_intr(if_ctx_t ctx)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
iavf_if_disable_intr(if_ctx_t ctx)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
iavf_if_rx_queue_intr_enable(if_ctx_t ctx,uint16_t rxqid)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
iavf_if_tx_queue_intr_enable(if_ctx_t ctx,uint16_t txqid)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
iavf_if_tx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int ntxqs,int ntxqsets)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
iavf_if_rx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int nrxqs,int nrxqsets)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
iavf_if_queues_free(if_ctx_t ctx)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
iavf_check_aq_errors(struct iavf_sc * sc)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
iavf_process_adminq(struct iavf_sc * sc,u16 * pending)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
iavf_if_update_admin_status(if_ctx_t ctx)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
iavf_if_multi_set(if_ctx_t ctx)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
iavf_if_mtu_set(if_ctx_t ctx,uint32_t mtu)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
iavf_if_media_status(if_ctx_t ctx,struct ifmediareq * ifmr)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
iavf_if_media_change(if_ctx_t ctx)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
iavf_if_promisc_set(if_ctx_t ctx,int flags)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
iavf_if_timer(if_ctx_t ctx,uint16_t qid)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
iavf_if_vlan_register(if_ctx_t ctx,u16 vtag)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
iavf_if_vlan_unregister(if_ctx_t ctx,u16 vtag)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
iavf_if_get_counter(if_ctx_t ctx,ift_counter cnt)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 /* iavf_if_needs_restart - Tell iflib when the driver needs to be reinitialized
1503 * @ctx: iflib context
1504 * @event: event code to check
1505 *
1506 * Defaults to returning false for unknown events.
1507 *
1508 * @returns true if iflib needs to reinit the interface
1509 */
1510 static bool
iavf_if_needs_restart(if_ctx_t ctx __unused,enum iflib_restart_event event)1511 iavf_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
1512 {
1513 switch (event) {
1514 case IFLIB_RESTART_VLAN_CONFIG:
1515 return (true);
1516 default:
1517 return (false);
1518 }
1519 }
1520
1521 /**
1522 * iavf_free_pci_resources - Free PCI resources
1523 * @sc: device softc
1524 *
1525 * Called to release the PCI resources allocated during attach. May be called
1526 * in the error flow of attach_pre, or during detach as part of cleanup.
1527 */
1528 static void
iavf_free_pci_resources(struct iavf_sc * sc)1529 iavf_free_pci_resources(struct iavf_sc *sc)
1530 {
1531 struct iavf_vsi *vsi = &sc->vsi;
1532 struct iavf_rx_queue *rx_que = vsi->rx_queues;
1533 device_t dev = sc->dev;
1534
1535 /* We may get here before stations are set up */
1536 if (rx_que == NULL)
1537 goto early;
1538
1539 /* Release all interrupts */
1540 iflib_irq_free(vsi->ctx, &vsi->irq);
1541
1542 for (int i = 0; i < vsi->num_rx_queues; i++, rx_que++)
1543 iflib_irq_free(vsi->ctx, &rx_que->que_irq);
1544
1545 early:
1546 if (sc->pci_mem != NULL)
1547 bus_release_resource(dev, SYS_RES_MEMORY,
1548 rman_get_rid(sc->pci_mem), sc->pci_mem);
1549 }
1550
1551 /**
1552 * iavf_setup_interface - Setup the device interface
1553 * @sc: device softc
1554 *
1555 * Called to setup some device interface settings, such as the ifmedia
1556 * structure.
1557 */
1558 static void
iavf_setup_interface(struct iavf_sc * sc)1559 iavf_setup_interface(struct iavf_sc *sc)
1560 {
1561 struct iavf_vsi *vsi = &sc->vsi;
1562 if_ctx_t ctx = vsi->ctx;
1563 if_t ifp = iflib_get_ifp(ctx);
1564
1565 iavf_dbg_init(sc, "begin\n");
1566
1567 vsi->shared->isc_max_frame_size =
1568 if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN
1569 + ETHER_VLAN_ENCAP_LEN;
1570
1571 iavf_set_initial_baudrate(ifp);
1572
1573 ifmedia_add(sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1574 ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
1575 }
1576
1577 /**
1578 * iavf_msix_adminq - Admin Queue interrupt handler
1579 * @arg: void pointer to the device softc
1580 *
1581 * Interrupt handler for the non-queue interrupt causes. Primarily this will
1582 * be the adminq interrupt, but also includes other miscellaneous causes.
1583 *
1584 * @returns FILTER_SCHEDULE_THREAD if the admin task needs to be run, otherwise
1585 * returns FITLER_HANDLED.
1586 */
1587 static int
iavf_msix_adminq(void * arg)1588 iavf_msix_adminq(void *arg)
1589 {
1590 struct iavf_sc *sc = (struct iavf_sc *)arg;
1591 struct iavf_hw *hw = &sc->hw;
1592 u32 reg, mask;
1593
1594 ++sc->admin_irq;
1595
1596 if (!iavf_test_state(&sc->state, IAVF_STATE_INITIALIZED))
1597 return (FILTER_HANDLED);
1598
1599 reg = rd32(hw, IAVF_VFINT_ICR01);
1600 /*
1601 * For masking off interrupt causes that need to be handled before
1602 * they can be re-enabled
1603 */
1604 mask = rd32(hw, IAVF_VFINT_ICR0_ENA1);
1605
1606 /* Check on the cause */
1607 if (reg & IAVF_VFINT_ICR01_ADMINQ_MASK) {
1608 mask &= ~IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK;
1609
1610 /* Process messages outside of the iflib context lock */
1611 taskqueue_enqueue(sc->vc_tq, &sc->vc_task);
1612 }
1613
1614 wr32(hw, IAVF_VFINT_ICR0_ENA1, mask);
1615 iavf_enable_adminq_irq(hw);
1616
1617 return (FILTER_HANDLED);
1618 }
1619
1620 /**
1621 * iavf_enable_intr - Enable device interrupts
1622 * @vsi: the main VSI
1623 *
1624 * Called to enable all queue interrupts.
1625 */
1626 void
iavf_enable_intr(struct iavf_vsi * vsi)1627 iavf_enable_intr(struct iavf_vsi *vsi)
1628 {
1629 struct iavf_hw *hw = vsi->hw;
1630 struct iavf_rx_queue *que = vsi->rx_queues;
1631
1632 iavf_enable_adminq_irq(hw);
1633 for (int i = 0; i < vsi->num_rx_queues; i++, que++)
1634 iavf_enable_queue_irq(hw, que->rxr.me);
1635 }
1636
1637 /**
1638 * iavf_disable_intr - Disable device interrupts
1639 * @vsi: the main VSI
1640 *
1641 * Called to disable all interrupts
1642 *
1643 * @remark we never disable the admin status interrupt.
1644 */
1645 void
iavf_disable_intr(struct iavf_vsi * vsi)1646 iavf_disable_intr(struct iavf_vsi *vsi)
1647 {
1648 struct iavf_hw *hw = vsi->hw;
1649 struct iavf_rx_queue *que = vsi->rx_queues;
1650
1651 for (int i = 0; i < vsi->num_rx_queues; i++, que++)
1652 iavf_disable_queue_irq(hw, que->rxr.me);
1653 }
1654
1655 /**
1656 * iavf_enable_queue_irq - Enable IRQ register for a queue interrupt
1657 * @hw: hardware structure
1658 * @id: IRQ vector to enable
1659 *
1660 * Writes the IAVF_VFINT_DYN_CTLN1 register to enable a given IRQ interrupt.
1661 */
1662 static void
iavf_enable_queue_irq(struct iavf_hw * hw,int id)1663 iavf_enable_queue_irq(struct iavf_hw *hw, int id)
1664 {
1665 u32 reg;
1666
1667 reg = IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1668 IAVF_VFINT_DYN_CTLN1_CLEARPBA_MASK |
1669 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK;
1670 wr32(hw, IAVF_VFINT_DYN_CTLN1(id), reg);
1671 }
1672
1673 /**
1674 * iavf_disable_queue_irq - Disable IRQ register for a queue interrupt
1675 * @hw: hardware structure
1676 * @id: IRQ vector to disable
1677 *
1678 * Writes the IAVF_VFINT_DYN_CTLN1 register to disable a given IRQ interrupt.
1679 */
1680 static void
iavf_disable_queue_irq(struct iavf_hw * hw,int id)1681 iavf_disable_queue_irq(struct iavf_hw *hw, int id)
1682 {
1683 wr32(hw, IAVF_VFINT_DYN_CTLN1(id),
1684 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
1685 rd32(hw, IAVF_VFGEN_RSTAT);
1686 }
1687
1688 /**
1689 * iavf_configure_itr - Get initial ITR values from tunable values.
1690 * @sc: device softc
1691 *
1692 * Load the initial tunable values for the ITR configuration.
1693 */
1694 static void
iavf_configure_itr(struct iavf_sc * sc)1695 iavf_configure_itr(struct iavf_sc *sc)
1696 {
1697 iavf_configure_tx_itr(sc);
1698 iavf_configure_rx_itr(sc);
1699 }
1700
1701 /**
1702 * iavf_set_queue_rx_itr - Update Rx ITR value
1703 * @que: Rx queue to update
1704 *
1705 * Provide a update to the queue RX interrupt moderation value.
1706 */
1707 static void
iavf_set_queue_rx_itr(struct iavf_rx_queue * que)1708 iavf_set_queue_rx_itr(struct iavf_rx_queue *que)
1709 {
1710 struct iavf_vsi *vsi = que->vsi;
1711 struct iavf_hw *hw = vsi->hw;
1712 struct rx_ring *rxr = &que->rxr;
1713
1714 /* Idle, do nothing */
1715 if (rxr->bytes == 0)
1716 return;
1717
1718 /* Update the hardware if needed */
1719 if (rxr->itr != vsi->rx_itr_setting) {
1720 rxr->itr = vsi->rx_itr_setting;
1721 wr32(hw, IAVF_VFINT_ITRN1(IAVF_RX_ITR,
1722 que->rxr.me), rxr->itr);
1723 }
1724 }
1725
1726 /**
1727 * iavf_msix_que - Main Rx queue interrupt handler
1728 * @arg: void pointer to the Rx queue
1729 *
1730 * Main MSI-X interrupt handler for Rx queue interrupts
1731 *
1732 * @returns FILTER_SCHEDULE_THREAD if the main thread for Rx needs to run,
1733 * otherwise returns FILTER_HANDLED.
1734 */
1735 static int
iavf_msix_que(void * arg)1736 iavf_msix_que(void *arg)
1737 {
1738 struct iavf_rx_queue *rx_que = (struct iavf_rx_queue *)arg;
1739 struct iavf_sc *sc = rx_que->vsi->back;
1740
1741 ++rx_que->irqs;
1742
1743 if (!iavf_test_state(&sc->state, IAVF_STATE_RUNNING))
1744 return (FILTER_HANDLED);
1745
1746 iavf_set_queue_rx_itr(rx_que);
1747
1748 return (FILTER_SCHEDULE_THREAD);
1749 }
1750
1751 /**
1752 * iavf_update_link_status - Update iflib Link status
1753 * @sc: device softc
1754 *
1755 * Notify the iflib stack of changes in link status. Called after the device
1756 * receives a virtchnl message indicating a change in link status.
1757 */
1758 void
iavf_update_link_status(struct iavf_sc * sc)1759 iavf_update_link_status(struct iavf_sc *sc)
1760 {
1761 struct iavf_vsi *vsi = &sc->vsi;
1762 u64 baudrate;
1763
1764 if (sc->link_up){
1765 if (vsi->link_active == FALSE) {
1766 vsi->link_active = TRUE;
1767 baudrate = iavf_baudrate_from_link_speed(sc);
1768 iavf_dbg_info(sc, "baudrate: %llu\n", (unsigned long long)baudrate);
1769 iflib_link_state_change(vsi->ctx, LINK_STATE_UP, baudrate);
1770 }
1771 } else { /* Link down */
1772 if (vsi->link_active == TRUE) {
1773 vsi->link_active = FALSE;
1774 iflib_link_state_change(vsi->ctx, LINK_STATE_DOWN, 0);
1775 }
1776 }
1777 }
1778
1779 /**
1780 * iavf_stop - Stop the interface
1781 * @sc: device softc
1782 *
1783 * This routine disables all traffic on the adapter by disabling interrupts
1784 * and sending a message to the PF to tell it to stop the hardware
1785 * Tx/Rx LAN queues.
1786 */
1787 static void
iavf_stop(struct iavf_sc * sc)1788 iavf_stop(struct iavf_sc *sc)
1789 {
1790 iavf_clear_state(&sc->state, IAVF_STATE_RUNNING);
1791
1792 iavf_disable_intr(&sc->vsi);
1793
1794 iavf_disable_queues_with_retries(sc);
1795 }
1796
1797 /**
1798 * iavf_if_stop - iflib stop handler
1799 * @ctx: iflib context
1800 *
1801 * Call iavf_stop to stop the interface.
1802 */
1803 static void
iavf_if_stop(if_ctx_t ctx)1804 iavf_if_stop(if_ctx_t ctx)
1805 {
1806 struct iavf_sc *sc = iavf_sc_from_ctx(ctx);
1807
1808 iavf_stop(sc);
1809 }
1810
1811 /**
1812 * iavf_del_mac_filter - Delete a MAC filter
1813 * @sc: device softc
1814 * @macaddr: MAC address to remove
1815 *
1816 * Marks a MAC filter for deletion.
1817 *
1818 * @returns zero if the filter existed, or ENOENT if it did not.
1819 */
1820 static int
iavf_del_mac_filter(struct iavf_sc * sc,u8 * macaddr)1821 iavf_del_mac_filter(struct iavf_sc *sc, u8 *macaddr)
1822 {
1823 struct iavf_mac_filter *f;
1824
1825 f = iavf_find_mac_filter(sc, macaddr);
1826 if (f == NULL)
1827 return (ENOENT);
1828
1829 f->flags |= IAVF_FILTER_DEL;
1830 return (0);
1831 }
1832
1833 /**
1834 * iavf_init_tx_rsqs - Initialize Report Status array
1835 * @vsi: the main VSI
1836 *
1837 * Set the Report Status queue fields to zero in order to initialize the
1838 * queues for transmit.
1839 */
1840 void
iavf_init_tx_rsqs(struct iavf_vsi * vsi)1841 iavf_init_tx_rsqs(struct iavf_vsi *vsi)
1842 {
1843 if_softc_ctx_t scctx = vsi->shared;
1844 struct iavf_tx_queue *tx_que;
1845 int i, j;
1846
1847 for (i = 0, tx_que = vsi->tx_queues; i < vsi->num_tx_queues; i++, tx_que++) {
1848 struct tx_ring *txr = &tx_que->txr;
1849
1850 txr->tx_rs_cidx = txr->tx_rs_pidx;
1851
1852 /* Initialize the last processed descriptor to be the end of
1853 * the ring, rather than the start, so that we avoid an
1854 * off-by-one error when calculating how many descriptors are
1855 * done in the credits_update function.
1856 */
1857 txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
1858
1859 for (j = 0; j < scctx->isc_ntxd[0]; j++)
1860 txr->tx_rsq[j] = QIDX_INVALID;
1861 }
1862 }
1863
1864 /**
1865 * iavf_init_tx_cidx - Initialize Tx cidx values
1866 * @vsi: the main VSI
1867 *
1868 * Initialize the tx_cidx_processed values for Tx queues in order to
1869 * initialize the Tx queues for transmit.
1870 */
1871 void
iavf_init_tx_cidx(struct iavf_vsi * vsi)1872 iavf_init_tx_cidx(struct iavf_vsi *vsi)
1873 {
1874 if_softc_ctx_t scctx = vsi->shared;
1875 struct iavf_tx_queue *tx_que;
1876 int i;
1877
1878 for (i = 0, tx_que = vsi->tx_queues; i < vsi->num_tx_queues; i++, tx_que++) {
1879 struct tx_ring *txr = &tx_que->txr;
1880
1881 txr->tx_cidx_processed = scctx->isc_ntxd[0] - 1;
1882 }
1883 }
1884
1885 /**
1886 * iavf_add_device_sysctls - Add device sysctls for configuration
1887 * @sc: device softc
1888 *
1889 * Add the main sysctl nodes and sysctls for device configuration.
1890 */
1891 static void
iavf_add_device_sysctls(struct iavf_sc * sc)1892 iavf_add_device_sysctls(struct iavf_sc *sc)
1893 {
1894 struct iavf_vsi *vsi = &sc->vsi;
1895 device_t dev = sc->dev;
1896 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
1897 struct sysctl_oid_list *debug_list;
1898
1899 iavf_add_device_sysctls_common(sc);
1900
1901 debug_list = iavf_create_debug_sysctl_tree(sc);
1902
1903 iavf_add_debug_sysctls_common(sc, debug_list);
1904
1905 SYSCTL_ADD_PROC(ctx, debug_list,
1906 OID_AUTO, "queue_interrupt_table", CTLTYPE_STRING | CTLFLAG_RD,
1907 sc, 0, iavf_sysctl_queue_interrupt_table, "A", "View MSI-X indices for TX/RX queues");
1908
1909 #ifdef IAVF_DEBUG
1910 SYSCTL_ADD_PROC(ctx, debug_list,
1911 OID_AUTO, "do_vf_reset", CTLTYPE_INT | CTLFLAG_WR,
1912 sc, 0, iavf_sysctl_vf_reset, "A", "Request a VF reset from PF");
1913
1914 SYSCTL_ADD_PROC(ctx, debug_list,
1915 OID_AUTO, "do_vflr_reset", CTLTYPE_INT | CTLFLAG_WR,
1916 sc, 0, iavf_sysctl_vflr_reset, "A", "Request a VFLR reset from HW");
1917 #endif
1918
1919 /* Add stats sysctls */
1920 iavf_add_vsi_sysctls(dev, vsi, ctx, "vsi");
1921
1922 iavf_add_queues_sysctls(dev, vsi);
1923 }
1924
1925 /**
1926 * iavf_add_queues_sysctls - Add per-queue sysctls
1927 * @dev: device pointer
1928 * @vsi: the main VSI
1929 *
1930 * Add sysctls for each Tx and Rx queue.
1931 */
1932 void
iavf_add_queues_sysctls(device_t dev,struct iavf_vsi * vsi)1933 iavf_add_queues_sysctls(device_t dev, struct iavf_vsi *vsi)
1934 {
1935 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(dev);
1936 struct sysctl_oid_list *vsi_list, *queue_list;
1937 struct sysctl_oid *queue_node;
1938 char queue_namebuf[32];
1939
1940 struct iavf_rx_queue *rx_que;
1941 struct iavf_tx_queue *tx_que;
1942 struct tx_ring *txr;
1943 struct rx_ring *rxr;
1944
1945 vsi_list = SYSCTL_CHILDREN(vsi->vsi_node);
1946
1947 /* Queue statistics */
1948 for (int q = 0; q < vsi->num_rx_queues; q++) {
1949 bzero(queue_namebuf, sizeof(queue_namebuf));
1950 snprintf(queue_namebuf, IAVF_QUEUE_NAME_LEN, "rxq%02d", q);
1951 queue_node = SYSCTL_ADD_NODE(ctx, vsi_list,
1952 OID_AUTO, queue_namebuf, CTLFLAG_RD, NULL, "RX Queue #");
1953 queue_list = SYSCTL_CHILDREN(queue_node);
1954
1955 rx_que = &(vsi->rx_queues[q]);
1956 rxr = &(rx_que->rxr);
1957
1958 SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "irqs",
1959 CTLFLAG_RD, &(rx_que->irqs),
1960 "irqs on this queue (both Tx and Rx)");
1961
1962 SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "packets",
1963 CTLFLAG_RD, &(rxr->rx_packets),
1964 "Queue Packets Received");
1965 SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "bytes",
1966 CTLFLAG_RD, &(rxr->rx_bytes),
1967 "Queue Bytes Received");
1968 SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "desc_err",
1969 CTLFLAG_RD, &(rxr->desc_errs),
1970 "Queue Rx Descriptor Errors");
1971 SYSCTL_ADD_UINT(ctx, queue_list, OID_AUTO, "itr",
1972 CTLFLAG_RD, &(rxr->itr), 0,
1973 "Queue Rx ITR Interval");
1974 }
1975 for (int q = 0; q < vsi->num_tx_queues; q++) {
1976 bzero(queue_namebuf, sizeof(queue_namebuf));
1977 snprintf(queue_namebuf, IAVF_QUEUE_NAME_LEN, "txq%02d", q);
1978 queue_node = SYSCTL_ADD_NODE(ctx, vsi_list,
1979 OID_AUTO, queue_namebuf, CTLFLAG_RD, NULL, "TX Queue #");
1980 queue_list = SYSCTL_CHILDREN(queue_node);
1981
1982 tx_que = &(vsi->tx_queues[q]);
1983 txr = &(tx_que->txr);
1984
1985 SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "tso",
1986 CTLFLAG_RD, &(tx_que->tso),
1987 "TSO");
1988 SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "mss_too_small",
1989 CTLFLAG_RD, &(txr->mss_too_small),
1990 "TSO sends with an MSS less than 64");
1991 SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "packets",
1992 CTLFLAG_RD, &(txr->tx_packets),
1993 "Queue Packets Transmitted");
1994 SYSCTL_ADD_UQUAD(ctx, queue_list, OID_AUTO, "bytes",
1995 CTLFLAG_RD, &(txr->tx_bytes),
1996 "Queue Bytes Transmitted");
1997 SYSCTL_ADD_UINT(ctx, queue_list, OID_AUTO, "itr",
1998 CTLFLAG_RD, &(txr->itr), 0,
1999 "Queue Tx ITR Interval");
2000 }
2001 }
2002
2003 /**
2004 * iavf_driver_is_detaching - Check if the driver is detaching/unloading
2005 * @sc: device private softc
2006 *
2007 * @returns true if the driver is detaching, false otherwise.
2008 *
2009 * @remark on newer kernels, take advantage of iflib_in_detach in order to
2010 * report detachment correctly as early as possible.
2011 *
2012 * @remark this function is used by various code paths that want to avoid
2013 * running if the driver is about to be removed. This includes sysctls and
2014 * other driver access points. Note that it does not fully resolve
2015 * detach-based race conditions as it is possible for a thread to race with
2016 * iflib_in_detach.
2017 */
2018 bool
iavf_driver_is_detaching(struct iavf_sc * sc)2019 iavf_driver_is_detaching(struct iavf_sc *sc)
2020 {
2021 return (!iavf_test_state(&sc->state, IAVF_STATE_INITIALIZED) ||
2022 iflib_in_detach(sc->vsi.ctx));
2023 }
2024
2025 /**
2026 * iavf_sysctl_queue_interrupt_table - Sysctl for displaying Tx queue mapping
2027 * @oidp: sysctl oid structure
2028 * @arg1: void pointer to device softc
2029 * @arg2: unused
2030 * @req: sysctl request pointer
2031 *
2032 * Print out mapping of TX queue indexes and Rx queue indexes to MSI-X vectors.
2033 *
2034 * @returns zero on success, or an error code on failure.
2035 */
2036 static int
iavf_sysctl_queue_interrupt_table(SYSCTL_HANDLER_ARGS)2037 iavf_sysctl_queue_interrupt_table(SYSCTL_HANDLER_ARGS)
2038 {
2039 struct iavf_sc *sc = (struct iavf_sc *)arg1;
2040 struct iavf_vsi *vsi = &sc->vsi;
2041 device_t dev = sc->dev;
2042 struct sbuf *buf;
2043 int error = 0;
2044
2045 struct iavf_rx_queue *rx_que;
2046 struct iavf_tx_queue *tx_que;
2047
2048 UNREFERENCED_2PARAMETER(arg2, oidp);
2049
2050 if (iavf_driver_is_detaching(sc))
2051 return (ESHUTDOWN);
2052
2053 buf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
2054 if (!buf) {
2055 device_printf(dev, "Could not allocate sbuf for output.\n");
2056 return (ENOMEM);
2057 }
2058
2059 sbuf_cat(buf, "\n");
2060 for (int i = 0; i < vsi->num_rx_queues; i++) {
2061 rx_que = &vsi->rx_queues[i];
2062 sbuf_printf(buf, "(rxq %3d): %d\n", i, rx_que->msix);
2063 }
2064 for (int i = 0; i < vsi->num_tx_queues; i++) {
2065 tx_que = &vsi->tx_queues[i];
2066 sbuf_printf(buf, "(txq %3d): %d\n", i, tx_que->msix);
2067 }
2068
2069 error = sbuf_finish(buf);
2070 if (error)
2071 device_printf(dev, "Error finishing sbuf: %d\n", error);
2072 sbuf_delete(buf);
2073
2074 return (error);
2075 }
2076
2077 #ifdef IAVF_DEBUG
2078 #define CTX_ACTIVE(ctx) ((if_getdrvflags(iflib_get_ifp(ctx)) & IFF_DRV_RUNNING))
2079
2080 /**
2081 * iavf_sysctl_vf_reset - Request a VF reset
2082 * @oidp: sysctl oid pointer
2083 * @arg1: void pointer to device softc
2084 * @arg2: unused
2085 * @req: sysctl request pointer
2086 *
2087 * Request a VF reset for the device.
2088 *
2089 * @returns zero on success, or an error code on failure.
2090 */
2091 static int
iavf_sysctl_vf_reset(SYSCTL_HANDLER_ARGS)2092 iavf_sysctl_vf_reset(SYSCTL_HANDLER_ARGS)
2093 {
2094 struct iavf_sc *sc = (struct iavf_sc *)arg1;
2095 int do_reset = 0, error = 0;
2096
2097 UNREFERENCED_PARAMETER(arg2);
2098
2099 if (iavf_driver_is_detaching(sc))
2100 return (ESHUTDOWN);
2101
2102 error = sysctl_handle_int(oidp, &do_reset, 0, req);
2103 if ((error) || (req->newptr == NULL))
2104 return (error);
2105
2106 if (do_reset == 1) {
2107 iavf_reset(sc);
2108 if (CTX_ACTIVE(sc->vsi.ctx))
2109 iflib_request_reset(sc->vsi.ctx);
2110 }
2111
2112 return (error);
2113 }
2114
2115 /**
2116 * iavf_sysctl_vflr_reset - Trigger a PCIe FLR for the device
2117 * @oidp: sysctl oid pointer
2118 * @arg1: void pointer to device softc
2119 * @arg2: unused
2120 * @req: sysctl request pointer
2121 *
2122 * Sysctl callback to trigger a PCIe FLR.
2123 *
2124 * @returns zero on success, or an error code on failure.
2125 */
2126 static int
iavf_sysctl_vflr_reset(SYSCTL_HANDLER_ARGS)2127 iavf_sysctl_vflr_reset(SYSCTL_HANDLER_ARGS)
2128 {
2129 struct iavf_sc *sc = (struct iavf_sc *)arg1;
2130 device_t dev = sc->dev;
2131 int do_reset = 0, error = 0;
2132
2133 UNREFERENCED_PARAMETER(arg2);
2134
2135 if (iavf_driver_is_detaching(sc))
2136 return (ESHUTDOWN);
2137
2138 error = sysctl_handle_int(oidp, &do_reset, 0, req);
2139 if ((error) || (req->newptr == NULL))
2140 return (error);
2141
2142 if (do_reset == 1) {
2143 if (!pcie_flr(dev, max(pcie_get_max_completion_timeout(dev) / 1000, 10), true)) {
2144 device_printf(dev, "PCIE FLR failed\n");
2145 error = EIO;
2146 }
2147 else if (CTX_ACTIVE(sc->vsi.ctx))
2148 iflib_request_reset(sc->vsi.ctx);
2149 }
2150
2151 return (error);
2152 }
2153 #undef CTX_ACTIVE
2154 #endif
2155