xref: /freebsd/sys/dev/ice/if_ice_iflib.c (revision 925a15b969a586bc9beec1afdc477f0d6e0aa625)
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_ice_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 "ice_iflib.h"
42 #include "ice_drv_info.h"
43 #include "ice_switch.h"
44 #include "ice_sched.h"
45 #ifdef PCI_IOV
46 #include "ice_iov.h"
47 #endif
48 
49 #include <sys/module.h>
50 #include <sys/sockio.h>
51 #include <sys/smp.h>
52 #include <dev/pci/pcivar.h>
53 #include <dev/pci/pcireg.h>
54 
55 /*
56  * Device method prototypes
57  */
58 
59 static void *ice_register(device_t);
60 static int  ice_if_attach_pre(if_ctx_t);
61 static int  ice_attach_pre_recovery_mode(struct ice_softc *sc);
62 static int  ice_if_attach_post(if_ctx_t);
63 static void ice_attach_post_recovery_mode(struct ice_softc *sc);
64 static int  ice_if_detach(if_ctx_t);
65 static int  ice_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int ntxqs, int ntxqsets);
66 static int  ice_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs, int nqs, int nqsets);
67 static int ice_if_msix_intr_assign(if_ctx_t ctx, int msix);
68 static void ice_if_queues_free(if_ctx_t ctx);
69 static int ice_if_mtu_set(if_ctx_t ctx, uint32_t mtu);
70 static void ice_if_intr_enable(if_ctx_t ctx);
71 static void ice_if_intr_disable(if_ctx_t ctx);
72 static int ice_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
73 static int ice_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
74 static int ice_if_promisc_set(if_ctx_t ctx, int flags);
75 static void ice_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
76 static int ice_if_media_change(if_ctx_t ctx);
77 static void ice_if_init(if_ctx_t ctx);
78 static void ice_if_timer(if_ctx_t ctx, uint16_t qid);
79 static void ice_if_update_admin_status(if_ctx_t ctx);
80 static void ice_if_multi_set(if_ctx_t ctx);
81 static void ice_if_vlan_register(if_ctx_t ctx, u16 vtag);
82 static void ice_if_vlan_unregister(if_ctx_t ctx, u16 vtag);
83 static void ice_if_stop(if_ctx_t ctx);
84 static uint64_t ice_if_get_counter(if_ctx_t ctx, ift_counter counter);
85 static int ice_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data);
86 static int ice_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req);
87 static int ice_if_suspend(if_ctx_t ctx);
88 static int ice_if_resume(if_ctx_t ctx);
89 static bool ice_if_needs_restart(if_ctx_t ctx, enum iflib_restart_event event);
90 static void ice_init_link(struct ice_softc *sc);
91 #ifdef PCI_IOV
92 static int ice_if_iov_init(if_ctx_t ctx, uint16_t num_vfs, const nvlist_t *params);
93 static void ice_if_iov_uninit(if_ctx_t ctx);
94 static int ice_if_iov_vf_add(if_ctx_t ctx, uint16_t vfnum, const nvlist_t *params);
95 static void ice_if_vflr_handle(if_ctx_t ctx);
96 #endif
97 static int ice_setup_mirror_vsi(struct ice_mirr_if *mif);
98 static int ice_wire_mirror_intrs(struct ice_mirr_if *mif);
99 static void ice_free_irqvs_subif(struct ice_mirr_if *mif);
100 static void *ice_subif_register(device_t);
101 static void ice_subif_setup_scctx(struct ice_mirr_if *mif);
102 static int ice_subif_rebuild(struct ice_softc *sc);
103 static int ice_subif_rebuild_vsi_qmap(struct ice_softc *sc);
104 
105 /* Iflib API */
106 static int ice_subif_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
107     uint64_t *paddrs, int ntxqs, int ntxqsets);
108 static int ice_subif_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
109     uint64_t *paddrs, int nrxqs, int nrxqsets);
110 static int ice_subif_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid);
111 static int ice_subif_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid);
112 static void ice_subif_if_intr_enable(if_ctx_t ctx);
113 static int ice_subif_if_msix_intr_assign(if_ctx_t ctx, int msix);
114 static void ice_subif_if_init(if_ctx_t ctx);
115 static void ice_subif_if_stop(if_ctx_t ctx);
116 static void ice_subif_if_queues_free(if_ctx_t ctx);
117 static int ice_subif_if_attach_pre(if_ctx_t);
118 static int ice_subif_if_attach_post(if_ctx_t);
119 static void ice_subif_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr);
120 static int ice_subif_if_promisc_set(if_ctx_t ctx, int flags);
121 
122 static int ice_msix_que(void *arg);
123 static int ice_msix_admin(void *arg);
124 
125 /*
126  * Helper function prototypes
127  */
128 static int ice_pci_mapping(struct ice_softc *sc);
129 static void ice_free_pci_mapping(struct ice_softc *sc);
130 static void ice_update_link_status(struct ice_softc *sc, bool update_media);
131 static void ice_init_device_features(struct ice_softc *sc);
132 static void ice_init_tx_tracking(struct ice_vsi *vsi);
133 static void ice_handle_reset_event(struct ice_softc *sc);
134 static void ice_handle_pf_reset_request(struct ice_softc *sc);
135 static void ice_prepare_for_reset(struct ice_softc *sc);
136 static int ice_rebuild_pf_vsi_qmap(struct ice_softc *sc);
137 static void ice_rebuild(struct ice_softc *sc);
138 static void ice_rebuild_recovery_mode(struct ice_softc *sc);
139 static void ice_free_irqvs(struct ice_softc *sc);
140 static void ice_update_rx_mbuf_sz(struct ice_softc *sc);
141 static void ice_poll_for_media_avail(struct ice_softc *sc);
142 static void ice_setup_scctx(struct ice_softc *sc);
143 static int ice_allocate_msix(struct ice_softc *sc);
144 static void ice_admin_timer(void *arg);
145 static void ice_transition_recovery_mode(struct ice_softc *sc);
146 static void ice_transition_safe_mode(struct ice_softc *sc);
147 static void ice_set_default_promisc_mask(ice_bitmap_t *promisc_mask);
148 
149 /*
150  * Device Interface Declaration
151  */
152 
153 /**
154  * @var ice_methods
155  * @brief ice driver method entry points
156  *
157  * List of device methods implementing the generic device interface used by
158  * the device stack to interact with the ice driver. Since this is an iflib
159  * driver, most of the methods point to the generic iflib implementation.
160  */
161 static device_method_t ice_methods[] = {
162 	/* Device interface */
163 	DEVMETHOD(device_register, ice_register),
164 	DEVMETHOD(device_probe,    iflib_device_probe_vendor),
165 	DEVMETHOD(device_attach,   iflib_device_attach),
166 	DEVMETHOD(device_detach,   iflib_device_detach),
167 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
168 	DEVMETHOD(device_suspend,  iflib_device_suspend),
169 	DEVMETHOD(device_resume,   iflib_device_resume),
170 #ifdef PCI_IOV
171 	DEVMETHOD(pci_iov_init, iflib_device_iov_init),
172 	DEVMETHOD(pci_iov_uninit, iflib_device_iov_uninit),
173 	DEVMETHOD(pci_iov_add_vf, iflib_device_iov_add_vf),
174 #endif
175 	DEVMETHOD_END
176 };
177 
178 /**
179  * @var ice_iflib_methods
180  * @brief iflib method entry points
181  *
182  * List of device methods used by the iflib stack to interact with this
183  * driver. These are the real main entry points used to interact with this
184  * driver.
185  */
186 static device_method_t ice_iflib_methods[] = {
187 	DEVMETHOD(ifdi_attach_pre, ice_if_attach_pre),
188 	DEVMETHOD(ifdi_attach_post, ice_if_attach_post),
189 	DEVMETHOD(ifdi_detach, ice_if_detach),
190 	DEVMETHOD(ifdi_tx_queues_alloc, ice_if_tx_queues_alloc),
191 	DEVMETHOD(ifdi_rx_queues_alloc, ice_if_rx_queues_alloc),
192 	DEVMETHOD(ifdi_msix_intr_assign, ice_if_msix_intr_assign),
193 	DEVMETHOD(ifdi_queues_free, ice_if_queues_free),
194 	DEVMETHOD(ifdi_mtu_set, ice_if_mtu_set),
195 	DEVMETHOD(ifdi_intr_enable, ice_if_intr_enable),
196 	DEVMETHOD(ifdi_intr_disable, ice_if_intr_disable),
197 	DEVMETHOD(ifdi_rx_queue_intr_enable, ice_if_rx_queue_intr_enable),
198 	DEVMETHOD(ifdi_tx_queue_intr_enable, ice_if_tx_queue_intr_enable),
199 	DEVMETHOD(ifdi_promisc_set, ice_if_promisc_set),
200 	DEVMETHOD(ifdi_media_status, ice_if_media_status),
201 	DEVMETHOD(ifdi_media_change, ice_if_media_change),
202 	DEVMETHOD(ifdi_init, ice_if_init),
203 	DEVMETHOD(ifdi_stop, ice_if_stop),
204 	DEVMETHOD(ifdi_timer, ice_if_timer),
205 	DEVMETHOD(ifdi_update_admin_status, ice_if_update_admin_status),
206 	DEVMETHOD(ifdi_multi_set, ice_if_multi_set),
207 	DEVMETHOD(ifdi_vlan_register, ice_if_vlan_register),
208 	DEVMETHOD(ifdi_vlan_unregister, ice_if_vlan_unregister),
209 	DEVMETHOD(ifdi_get_counter, ice_if_get_counter),
210 	DEVMETHOD(ifdi_priv_ioctl, ice_if_priv_ioctl),
211 	DEVMETHOD(ifdi_i2c_req, ice_if_i2c_req),
212 	DEVMETHOD(ifdi_suspend, ice_if_suspend),
213 	DEVMETHOD(ifdi_resume, ice_if_resume),
214 	DEVMETHOD(ifdi_needs_restart, ice_if_needs_restart),
215 #ifdef PCI_IOV
216 	DEVMETHOD(ifdi_iov_vf_add, ice_if_iov_vf_add),
217 	DEVMETHOD(ifdi_iov_init, ice_if_iov_init),
218 	DEVMETHOD(ifdi_iov_uninit, ice_if_iov_uninit),
219 	DEVMETHOD(ifdi_vflr_handle, ice_if_vflr_handle),
220 #endif
221 	DEVMETHOD_END
222 };
223 
224 /**
225  * @var ice_driver
226  * @brief driver structure for the generic device stack
227  *
228  * driver_t definition used to setup the generic device methods.
229  */
230 static driver_t ice_driver = {
231 	.name = "ice",
232 	.methods = ice_methods,
233 	.size = sizeof(struct ice_softc),
234 };
235 
236 /**
237  * @var ice_iflib_driver
238  * @brief driver structure for the iflib stack
239  *
240  * driver_t definition used to setup the iflib device methods.
241  */
242 static driver_t ice_iflib_driver = {
243 	.name = "ice",
244 	.methods = ice_iflib_methods,
245 	.size = sizeof(struct ice_softc),
246 };
247 
248 extern struct if_txrx ice_txrx;
249 extern struct if_txrx ice_recovery_txrx;
250 
251 /**
252  * @var ice_sctx
253  * @brief ice driver shared context
254  *
255  * Structure defining shared values (context) that is used by all instances of
256  * the device. Primarily used to setup details about how the iflib stack
257  * should treat this driver. Also defines the default, minimum, and maximum
258  * number of descriptors in each ring.
259  */
260 static struct if_shared_ctx ice_sctx = {
261 	.isc_magic = IFLIB_MAGIC,
262 	.isc_q_align = PAGE_SIZE,
263 
264 	.isc_tx_maxsize = ICE_MAX_FRAME_SIZE,
265 	/* We could technically set this as high as ICE_MAX_DMA_SEG_SIZE, but
266 	 * that doesn't make sense since that would be larger than the maximum
267 	 * size of a single packet.
268 	 */
269 	.isc_tx_maxsegsize = ICE_MAX_FRAME_SIZE,
270 
271 	/* XXX: This is only used by iflib to ensure that
272 	 * scctx->isc_tx_tso_size_max + the VLAN header is a valid size.
273 	 */
274 	.isc_tso_maxsize = ICE_TSO_SIZE + sizeof(struct ether_vlan_header),
275 	/* XXX: This is used by iflib to set the number of segments in the TSO
276 	 * DMA tag. However, scctx->isc_tx_tso_segsize_max is used to set the
277 	 * related ifnet parameter.
278 	 */
279 	.isc_tso_maxsegsize = ICE_MAX_DMA_SEG_SIZE,
280 
281 	.isc_rx_maxsize = ICE_MAX_FRAME_SIZE,
282 	.isc_rx_nsegments = ICE_MAX_RX_SEGS,
283 	.isc_rx_maxsegsize = ICE_MAX_FRAME_SIZE,
284 
285 	.isc_nfl = 1,
286 	.isc_ntxqs = 1,
287 	.isc_nrxqs = 1,
288 
289 	.isc_admin_intrcnt = 1,
290 	.isc_vendor_info = ice_vendor_info_array,
291 	.isc_driver_version = __DECONST(char *, ice_driver_version),
292 	.isc_driver = &ice_iflib_driver,
293 
294 	/*
295 	 * IFLIB_NEED_SCRATCH ensures that mbufs have scratch space available
296 	 * for hardware checksum offload
297 	 *
298 	 * IFLIB_TSO_INIT_IP ensures that the TSO packets have zeroed out the
299 	 * IP sum field, required by our hardware to calculate valid TSO
300 	 * checksums.
301 	 *
302 	 * IFLIB_ADMIN_ALWAYS_RUN ensures that the administrative task runs
303 	 * even when the interface is down.
304 	 *
305 	 * IFLIB_SKIP_MSIX allows the driver to handle allocating MSI-X
306 	 * vectors manually instead of relying on iflib code to do this.
307 	 */
308 	.isc_flags = IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP |
309 		IFLIB_ADMIN_ALWAYS_RUN | IFLIB_SKIP_MSIX,
310 
311 	.isc_nrxd_min = {ICE_MIN_DESC_COUNT},
312 	.isc_ntxd_min = {ICE_MIN_DESC_COUNT},
313 	.isc_nrxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
314 	.isc_ntxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
315 	.isc_nrxd_default = {ICE_DEFAULT_DESC_COUNT},
316 	.isc_ntxd_default = {ICE_DEFAULT_DESC_COUNT},
317 };
318 
319 DRIVER_MODULE(ice, pci, ice_driver, ice_module_event_handler, NULL);
320 
321 MODULE_VERSION(ice, 1);
322 MODULE_DEPEND(ice, pci, 1, 1, 1);
323 MODULE_DEPEND(ice, ether, 1, 1, 1);
324 MODULE_DEPEND(ice, iflib, 1, 1, 1);
325 
326 IFLIB_PNP_INFO(pci, ice, ice_vendor_info_array);
327 
328 /* Static driver-wide sysctls */
329 #include "ice_iflib_sysctls.h"
330 
331 /**
332  * ice_pci_mapping - Map PCI BAR memory
333  * @sc: device private softc
334  *
335  * Map PCI BAR 0 for device operation.
336  */
337 static int
338 ice_pci_mapping(struct ice_softc *sc)
339 {
340 	int rc;
341 
342 	/* Map BAR0 */
343 	rc = ice_map_bar(sc->dev, &sc->bar0, 0);
344 	if (rc)
345 		return rc;
346 
347 	return 0;
348 }
349 
350 /**
351  * ice_free_pci_mapping - Release PCI BAR memory
352  * @sc: device private softc
353  *
354  * Release PCI BARs which were previously mapped by ice_pci_mapping().
355  */
356 static void
357 ice_free_pci_mapping(struct ice_softc *sc)
358 {
359 	/* Free BAR0 */
360 	ice_free_bar(sc->dev, &sc->bar0);
361 }
362 
363 /*
364  * Device methods
365  */
366 
367 /**
368  * ice_register - register device method callback
369  * @dev: the device being registered
370  *
371  * Returns a pointer to the shared context structure, which is used by iflib.
372  */
373 static void *
374 ice_register(device_t dev __unused)
375 {
376 	return &ice_sctx;
377 } /* ice_register */
378 
379 /**
380  * ice_setup_scctx - Setup the iflib softc context structure
381  * @sc: the device private structure
382  *
383  * Setup the parameters in if_softc_ctx_t structure used by the iflib stack
384  * when loading.
385  */
386 static void
387 ice_setup_scctx(struct ice_softc *sc)
388 {
389 	if_softc_ctx_t scctx = sc->scctx;
390 	struct ice_hw *hw = &sc->hw;
391 	device_t dev = sc->dev;
392 	bool safe_mode, recovery_mode;
393 
394 	safe_mode = ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE);
395 	recovery_mode = ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE);
396 
397 	/*
398 	 * If the driver loads in Safe mode or Recovery mode, limit iflib to
399 	 * a single queue pair.
400 	 */
401 	if (safe_mode || recovery_mode) {
402 		scctx->isc_ntxqsets = scctx->isc_nrxqsets = 1;
403 		scctx->isc_ntxqsets_max = 1;
404 		scctx->isc_nrxqsets_max = 1;
405 	} else {
406 		/*
407 		 * iflib initially sets the isc_ntxqsets and isc_nrxqsets to
408 		 * the values of the override sysctls. Cache these initial
409 		 * values so that the driver can be aware of what the iflib
410 		 * sysctl value is when setting up MSI-X vectors.
411 		 */
412 		sc->ifc_sysctl_ntxqs = scctx->isc_ntxqsets;
413 		sc->ifc_sysctl_nrxqs = scctx->isc_nrxqsets;
414 
415 		if (scctx->isc_ntxqsets == 0)
416 			scctx->isc_ntxqsets = hw->func_caps.common_cap.rss_table_size;
417 		if (scctx->isc_nrxqsets == 0)
418 			scctx->isc_nrxqsets = hw->func_caps.common_cap.rss_table_size;
419 
420 		scctx->isc_ntxqsets_max = hw->func_caps.common_cap.num_txq;
421 		scctx->isc_nrxqsets_max = hw->func_caps.common_cap.num_rxq;
422 
423 		/*
424 		 * Sanity check that the iflib sysctl values are within the
425 		 * maximum supported range.
426 		 */
427 		if (sc->ifc_sysctl_ntxqs > scctx->isc_ntxqsets_max)
428 			sc->ifc_sysctl_ntxqs = scctx->isc_ntxqsets_max;
429 		if (sc->ifc_sysctl_nrxqs > scctx->isc_nrxqsets_max)
430 			sc->ifc_sysctl_nrxqs = scctx->isc_nrxqsets_max;
431 	}
432 
433 	scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
434 	    * sizeof(struct ice_tx_desc), DBA_ALIGN);
435 	scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0]
436 	    * sizeof(union ice_32b_rx_flex_desc), DBA_ALIGN);
437 
438 	scctx->isc_tx_nsegments = ICE_MAX_TX_SEGS;
439 	scctx->isc_tx_tso_segments_max = ICE_MAX_TSO_SEGS;
440 	scctx->isc_tx_tso_size_max = ICE_TSO_SIZE;
441 	scctx->isc_tx_tso_segsize_max = ICE_MAX_DMA_SEG_SIZE;
442 
443 	scctx->isc_msix_bar = pci_msix_table_bar(dev);
444 	scctx->isc_rss_table_size = hw->func_caps.common_cap.rss_table_size;
445 
446 	/*
447 	 * If the driver loads in recovery mode, disable Tx/Rx functionality
448 	 */
449 	if (recovery_mode)
450 		scctx->isc_txrx = &ice_recovery_txrx;
451 	else
452 		scctx->isc_txrx = &ice_txrx;
453 
454 	/*
455 	 * If the driver loads in Safe mode or Recovery mode, disable
456 	 * advanced features including hardware offloads.
457 	 */
458 	if (safe_mode || recovery_mode) {
459 		scctx->isc_capenable = ICE_SAFE_CAPS;
460 		scctx->isc_tx_csum_flags = 0;
461 	} else {
462 		scctx->isc_capenable = ICE_FULL_CAPS;
463 		scctx->isc_tx_csum_flags = ICE_CSUM_OFFLOAD;
464 	}
465 
466 	scctx->isc_capabilities = scctx->isc_capenable;
467 } /* ice_setup_scctx */
468 
469 /**
470  * ice_if_attach_pre - Early device attach logic
471  * @ctx: the iflib context structure
472  *
473  * Called by iflib during the attach process. Earliest main driver entry
474  * point which performs necessary hardware and driver initialization. Called
475  * before the Tx and Rx queues are allocated.
476  */
477 static int
478 ice_if_attach_pre(if_ctx_t ctx)
479 {
480 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
481 	enum ice_fw_modes fw_mode;
482 	int status;
483 	if_softc_ctx_t scctx;
484 	struct ice_hw *hw;
485 	device_t dev;
486 	int err;
487 
488 	device_printf(iflib_get_dev(ctx), "Loading the iflib ice driver\n");
489 
490 	ice_set_state(&sc->state, ICE_STATE_ATTACHING);
491 
492 	sc->ctx = ctx;
493 	sc->media = iflib_get_media(ctx);
494 	sc->sctx = iflib_get_sctx(ctx);
495 	sc->iflib_ctx_lock = iflib_ctx_lock_get(ctx);
496 	sc->ifp = iflib_get_ifp(ctx);
497 
498 	dev = sc->dev = iflib_get_dev(ctx);
499 	scctx = sc->scctx = iflib_get_softc_ctx(ctx);
500 
501 	hw = &sc->hw;
502 	hw->back = sc;
503 
504 	snprintf(sc->admin_mtx_name, sizeof(sc->admin_mtx_name),
505 		 "%s:admin", device_get_nameunit(dev));
506 	mtx_init(&sc->admin_mtx, sc->admin_mtx_name, NULL, MTX_DEF);
507 	callout_init_mtx(&sc->admin_timer, &sc->admin_mtx, 0);
508 
509 	ASSERT_CTX_LOCKED(sc);
510 
511 	if (ice_pci_mapping(sc)) {
512 		err = (ENXIO);
513 		goto destroy_admin_timer;
514 	}
515 
516 	/* Save off the PCI information */
517 	ice_save_pci_info(hw, dev);
518 
519 	/* create tunables as early as possible */
520 	ice_add_device_tunables(sc);
521 
522 	/* Setup ControlQ lengths */
523 	ice_set_ctrlq_len(hw);
524 
525 reinit_hw:
526 
527 	fw_mode = ice_get_fw_mode(hw);
528 	if (fw_mode == ICE_FW_MODE_REC) {
529 		device_printf(dev, "Firmware recovery mode detected. Limiting functionality. Refer to Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
530 
531 		err = ice_attach_pre_recovery_mode(sc);
532 		if (err)
533 			goto free_pci_mapping;
534 
535 		return (0);
536 	}
537 
538 	/* Initialize the hw data structure */
539 	status = ice_init_hw(hw);
540 	if (status) {
541 		if (status == ICE_ERR_FW_API_VER) {
542 			/* Enter recovery mode, so that the driver remains
543 			 * loaded. This way, if the system administrator
544 			 * cannot update the driver, they may still attempt to
545 			 * downgrade the NVM.
546 			 */
547 			err = ice_attach_pre_recovery_mode(sc);
548 			if (err)
549 				goto free_pci_mapping;
550 
551 			return (0);
552 		} else {
553 			err = EIO;
554 			device_printf(dev, "Unable to initialize hw, err %s aq_err %s\n",
555 				      ice_status_str(status),
556 				      ice_aq_str(hw->adminq.sq_last_status));
557 		}
558 		goto free_pci_mapping;
559 	}
560 
561 	ice_init_device_features(sc);
562 
563 	/* Keep flag set by default */
564 	ice_set_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN);
565 
566 	/* Notify firmware of the device driver version */
567 	err = ice_send_version(sc);
568 	if (err)
569 		goto deinit_hw;
570 
571 	/*
572 	 * Success indicates a change was made that requires a reinitialization
573 	 * of the hardware
574 	 */
575 	err = ice_load_pkg_file(sc);
576 	if (!err) {
577 		ice_deinit_hw(hw);
578 		goto reinit_hw;
579 	}
580 
581 	err = ice_init_link_events(sc);
582 	if (err) {
583 		device_printf(dev, "ice_init_link_events failed: %s\n",
584 			      ice_err_str(err));
585 		goto deinit_hw;
586 	}
587 
588 	/* Initialize VLAN mode in FW; if dual VLAN mode is supported by the package
589 	 * and firmware, this will force them to use single VLAN mode.
590 	 */
591 	status = ice_set_vlan_mode(hw);
592 	if (status) {
593 		err = EIO;
594 		device_printf(dev, "Unable to initialize VLAN mode, err %s aq_err %s\n",
595 			      ice_status_str(status),
596 			      ice_aq_str(hw->adminq.sq_last_status));
597 		goto deinit_hw;
598 	}
599 
600 	ice_print_nvm_version(sc);
601 
602 	/* Setup the MAC address */
603 	iflib_set_mac(ctx, hw->port_info->mac.lan_addr);
604 
605 	/* Setup the iflib softc context structure */
606 	ice_setup_scctx(sc);
607 
608 	/* Initialize the Tx queue manager */
609 	err = ice_resmgr_init(&sc->tx_qmgr, hw->func_caps.common_cap.num_txq);
610 	if (err) {
611 		device_printf(dev, "Unable to initialize Tx queue manager: %s\n",
612 			      ice_err_str(err));
613 		goto deinit_hw;
614 	}
615 
616 	/* Initialize the Rx queue manager */
617 	err = ice_resmgr_init(&sc->rx_qmgr, hw->func_caps.common_cap.num_rxq);
618 	if (err) {
619 		device_printf(dev, "Unable to initialize Rx queue manager: %s\n",
620 			      ice_err_str(err));
621 		goto free_tx_qmgr;
622 	}
623 
624 	/* Initialize the PF device interrupt resource manager */
625 	err = ice_alloc_intr_tracking(sc);
626 	if (err)
627 		/* Errors are already printed */
628 		goto free_rx_qmgr;
629 
630 	/* Determine maximum number of VSIs we'll prepare for */
631 	sc->num_available_vsi = min(ICE_MAX_VSI_AVAILABLE,
632 				    hw->func_caps.guar_num_vsi);
633 
634 	if (!sc->num_available_vsi) {
635 		err = EIO;
636 		device_printf(dev, "No VSIs allocated to host\n");
637 		goto free_intr_tracking;
638 	}
639 
640 	/* Allocate storage for the VSI pointers */
641 	sc->all_vsi = (struct ice_vsi **)
642 		malloc(sizeof(struct ice_vsi *) * sc->num_available_vsi,
643 		       M_ICE, M_WAITOK | M_ZERO);
644 	if (!sc->all_vsi) {
645 		err = ENOMEM;
646 		device_printf(dev, "Unable to allocate VSI array\n");
647 		goto free_intr_tracking;
648 	}
649 
650 	/*
651 	 * Prepare the statically allocated primary PF VSI in the softc
652 	 * structure. Other VSIs will be dynamically allocated as needed.
653 	 */
654 	ice_setup_pf_vsi(sc);
655 
656 	ice_alloc_vsi_qmap(&sc->pf_vsi, scctx->isc_ntxqsets_max,
657 	    scctx->isc_nrxqsets_max);
658 
659 	/* Allocate MSI-X vectors (due to isc_flags IFLIB_SKIP_MSIX) */
660 	err = ice_allocate_msix(sc);
661 	if (err)
662 		goto free_main_vsi;
663 
664 	return 0;
665 
666 free_main_vsi:
667 	/* ice_release_vsi will free the queue maps if they were allocated */
668 	ice_release_vsi(&sc->pf_vsi);
669 	free(sc->all_vsi, M_ICE);
670 	sc->all_vsi = NULL;
671 free_intr_tracking:
672 	ice_free_intr_tracking(sc);
673 free_rx_qmgr:
674 	ice_resmgr_destroy(&sc->rx_qmgr);
675 free_tx_qmgr:
676 	ice_resmgr_destroy(&sc->tx_qmgr);
677 deinit_hw:
678 	ice_deinit_hw(hw);
679 free_pci_mapping:
680 	ice_free_pci_mapping(sc);
681 destroy_admin_timer:
682 	mtx_lock(&sc->admin_mtx);
683 	callout_stop(&sc->admin_timer);
684 	mtx_unlock(&sc->admin_mtx);
685 	mtx_destroy(&sc->admin_mtx);
686 	return err;
687 } /* ice_if_attach_pre */
688 
689 /**
690  * ice_attach_pre_recovery_mode - Limited driver attach_pre for FW recovery
691  * @sc: the device private softc
692  *
693  * Loads the device driver in limited Firmware Recovery mode, intended to
694  * allow users to update the firmware to attempt to recover the device.
695  *
696  * @remark We may enter recovery mode in case either (a) the firmware is
697  * detected to be in an invalid state and must be re-programmed, or (b) the
698  * driver detects that the loaded firmware has a non-compatible API version
699  * that the driver cannot operate with.
700  */
701 static int
702 ice_attach_pre_recovery_mode(struct ice_softc *sc)
703 {
704 	ice_set_state(&sc->state, ICE_STATE_RECOVERY_MODE);
705 
706 	/* Setup the iflib softc context */
707 	ice_setup_scctx(sc);
708 
709 	/* Setup the PF VSI back pointer */
710 	sc->pf_vsi.sc = sc;
711 
712 	/*
713 	 * We still need to allocate MSI-X vectors since we need one vector to
714 	 * run the administrative admin interrupt
715 	 */
716 	return ice_allocate_msix(sc);
717 }
718 
719 /**
720  * ice_update_link_status - notify OS of link state change
721  * @sc: device private softc structure
722  * @update_media: true if we should update media even if link didn't change
723  *
724  * Called to notify iflib core of link status changes. Should be called once
725  * during attach_post, and whenever link status changes during runtime.
726  *
727  * This call only updates the currently supported media types if the link
728  * status changed, or if update_media is set to true.
729  */
730 static void
731 ice_update_link_status(struct ice_softc *sc, bool update_media)
732 {
733 	struct ice_hw *hw = &sc->hw;
734 	int status;
735 
736 	/* Never report link up when in recovery mode */
737 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
738 		return;
739 
740 	/* Report link status to iflib only once each time it changes */
741 	if (!ice_testandset_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED)) {
742 		if (sc->link_up) { /* link is up */
743 			uint64_t baudrate = ice_aq_speed_to_rate(sc->hw.port_info);
744 
745 			if (!(hw->port_info->phy.link_info_old.link_info & ICE_AQ_LINK_UP))
746 				ice_set_default_local_lldp_mib(sc);
747 
748 			iflib_link_state_change(sc->ctx, LINK_STATE_UP, baudrate);
749 			ice_rdma_link_change(sc, LINK_STATE_UP, baudrate);
750 
751 			ice_link_up_msg(sc);
752 		} else { /* link is down */
753 			iflib_link_state_change(sc->ctx, LINK_STATE_DOWN, 0);
754 			ice_rdma_link_change(sc, LINK_STATE_DOWN, 0);
755 		}
756 #ifdef PCI_IOV
757 		ice_vc_notify_all_vfs_link_state(sc);
758 #endif
759 		update_media = true;
760 	}
761 
762 	/* Update the supported media types */
763 	if (update_media && !ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
764 		status = ice_add_media_types(sc, sc->media);
765 		if (status)
766 			device_printf(sc->dev, "Error adding device media types: %s aq_err %s\n",
767 				      ice_status_str(status),
768 				      ice_aq_str(hw->adminq.sq_last_status));
769 	}
770 }
771 
772 /**
773  * ice_if_attach_post - Late device attach logic
774  * @ctx: the iflib context structure
775  *
776  * Called by iflib to finish up attaching the device. Performs any attach
777  * logic which must wait until after the Tx and Rx queues have been
778  * allocated.
779  */
780 static int
781 ice_if_attach_post(if_ctx_t ctx)
782 {
783 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
784 	if_t ifp = iflib_get_ifp(ctx);
785 	int status;
786 	int err;
787 
788 	ASSERT_CTX_LOCKED(sc);
789 
790 	/* We don't yet support loading if MSI-X is not supported */
791 	if (sc->scctx->isc_intr != IFLIB_INTR_MSIX) {
792 		device_printf(sc->dev, "The ice driver does not support loading without MSI-X\n");
793 		return (ENOTSUP);
794 	}
795 
796 	/* The ifnet structure hasn't yet been initialized when the attach_pre
797 	 * handler is called, so wait until attach_post to setup the
798 	 * isc_max_frame_size.
799 	 */
800 	sc->scctx->isc_max_frame_size = if_getmtu(ifp) +
801 		ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
802 
803 	/*
804 	 * If we are in recovery mode, only perform a limited subset of
805 	 * initialization to support NVM recovery.
806 	 */
807 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
808 		ice_attach_post_recovery_mode(sc);
809 		return (0);
810 	}
811 
812 	sc->pf_vsi.max_frame_size = sc->scctx->isc_max_frame_size;
813 
814 	err = ice_initialize_vsi(&sc->pf_vsi);
815 	if (err) {
816 		device_printf(sc->dev, "Unable to initialize Main VSI: %s\n",
817 			      ice_err_str(err));
818 		return err;
819 	}
820 
821 	/* Enable FW health event reporting */
822 	ice_init_health_events(sc);
823 
824 	/* Configure the main PF VSI for RSS */
825 	err = ice_config_rss(&sc->pf_vsi);
826 	if (err) {
827 		device_printf(sc->dev,
828 			      "Unable to configure RSS for the main VSI, err %s\n",
829 			      ice_err_str(err));
830 		return err;
831 	}
832 
833 	/* Configure switch to drop transmitted LLDP and PAUSE frames */
834 	err = ice_cfg_pf_ethertype_filters(sc);
835 	if (err)
836 		return err;
837 
838 	ice_get_and_print_bus_info(sc);
839 
840 	ice_set_link_management_mode(sc);
841 
842 	ice_init_saved_phy_cfg(sc);
843 
844 	ice_cfg_pba_num(sc);
845 
846 	/* Set a default value for PFC mode on attach since the FW state is unknown
847 	 * before sysctl tunables are executed and it can't be queried. This fixes an
848 	 * issue when loading the driver with the FW LLDP agent enabled but the FW
849 	 * was previously in DSCP PFC mode.
850 	 */
851 	status = ice_aq_set_pfc_mode(&sc->hw, ICE_AQC_PFC_VLAN_BASED_PFC, NULL);
852 	if (status)
853 		device_printf(sc->dev, "Setting pfc mode failed, status %s\n", ice_status_str(status));
854 
855 	ice_add_device_sysctls(sc);
856 
857 #ifdef PCI_IOV
858 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_SRIOV)) {
859 		err = ice_iov_attach(sc);
860 		if (err == ENOMEM)
861 			return (err);
862 	}
863 #endif /* PCI_IOV */
864 
865 	/* Get DCBX/LLDP state and start DCBX agent */
866 	ice_init_dcb_setup(sc);
867 
868 	/* Setup link, if PHY FW is ready */
869 	ice_init_link(sc);
870 
871 	/* Configure interrupt causes for the administrative interrupt */
872 	ice_configure_misc_interrupts(sc);
873 
874 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
875 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
876 
877 	err = ice_rdma_pf_attach(sc);
878 	if (err)
879 		return (err);
880 
881 	/* Start the admin timer */
882 	mtx_lock(&sc->admin_mtx);
883 	callout_reset(&sc->admin_timer, hz/2, ice_admin_timer, sc);
884 	mtx_unlock(&sc->admin_mtx);
885 
886 	if (ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) &&
887 		 !ice_test_state(&sc->state, ICE_STATE_NO_MEDIA))
888 		ice_set_state(&sc->state, ICE_STATE_FIRST_INIT_LINK);
889 
890 	ice_clear_state(&sc->state, ICE_STATE_ATTACHING);
891 
892 	return 0;
893 } /* ice_if_attach_post */
894 
895 /**
896  * ice_attach_post_recovery_mode - Limited driver attach_post for FW recovery
897  * @sc: the device private softc
898  *
899  * Performs minimal work to prepare the driver to recover an NVM in case the
900  * firmware is in recovery mode.
901  */
902 static void
903 ice_attach_post_recovery_mode(struct ice_softc *sc)
904 {
905 	/* Configure interrupt causes for the administrative interrupt */
906 	ice_configure_misc_interrupts(sc);
907 
908 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
909 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
910 
911 	/* Start the admin timer */
912 	mtx_lock(&sc->admin_mtx);
913 	callout_reset(&sc->admin_timer, hz/2, ice_admin_timer, sc);
914 	mtx_unlock(&sc->admin_mtx);
915 
916 	ice_clear_state(&sc->state, ICE_STATE_ATTACHING);
917 }
918 
919 /**
920  * ice_free_irqvs - Free IRQ vector memory
921  * @sc: the device private softc structure
922  *
923  * Free IRQ vector memory allocated during ice_if_msix_intr_assign.
924  */
925 static void
926 ice_free_irqvs(struct ice_softc *sc)
927 {
928 	struct ice_vsi *vsi = &sc->pf_vsi;
929 	if_ctx_t ctx = sc->ctx;
930 	int i;
931 
932 	/* If the irqvs array is NULL, then there are no vectors to free */
933 	if (sc->irqvs == NULL)
934 		return;
935 
936 	/* Free the IRQ vectors */
937 	for (i = 0; i < sc->num_irq_vectors; i++)
938 		iflib_irq_free(ctx, &sc->irqvs[i].irq);
939 
940 	/* Clear the irqv pointers */
941 	for (i = 0; i < vsi->num_rx_queues; i++)
942 		vsi->rx_queues[i].irqv = NULL;
943 
944 	for (i = 0; i < vsi->num_tx_queues; i++)
945 		vsi->tx_queues[i].irqv = NULL;
946 
947 	/* Release the vector array memory */
948 	free(sc->irqvs, M_ICE);
949 	sc->irqvs = NULL;
950 	sc->num_irq_vectors = 0;
951 }
952 
953 /**
954  * ice_if_detach - Device driver detach logic
955  * @ctx: iflib context structure
956  *
957  * Perform device shutdown logic to detach the device driver.
958  *
959  * Note that there is no guarantee of the ordering of ice_if_queues_free() and
960  * ice_if_detach(). It is possible for the functions to be called in either
961  * order, and they must not assume to have a strict ordering.
962  */
963 static int
964 ice_if_detach(if_ctx_t ctx)
965 {
966 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
967 	struct ice_vsi *vsi = &sc->pf_vsi;
968 	int status;
969 	int i;
970 
971 	ASSERT_CTX_LOCKED(sc);
972 
973 	/* Indicate that we're detaching */
974 	ice_set_state(&sc->state, ICE_STATE_DETACHING);
975 
976 	/* Stop the admin timer */
977 	mtx_lock(&sc->admin_mtx);
978 	callout_stop(&sc->admin_timer);
979 	mtx_unlock(&sc->admin_mtx);
980 	mtx_destroy(&sc->admin_mtx);
981 
982 	/* Remove additional interfaces if they exist */
983 	if (sc->mirr_if)
984 		ice_destroy_mirror_interface(sc);
985 	ice_rdma_pf_detach(sc);
986 
987 #ifdef PCI_IOV
988 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_SRIOV))
989 		ice_iov_detach(sc);
990 #endif /* PCI_IOV */
991 
992 	/* Free allocated media types */
993 	ifmedia_removeall(sc->media);
994 
995 	/* Free the Tx and Rx sysctl contexts, and assign NULL to the node
996 	 * pointers. Note, the calls here and those in ice_if_queues_free()
997 	 * are *BOTH* necessary, as we cannot guarantee which path will be
998 	 * run first
999 	 */
1000 	ice_vsi_del_txqs_ctx(vsi);
1001 	ice_vsi_del_rxqs_ctx(vsi);
1002 
1003 	/* Release MSI-X resources */
1004 	ice_free_irqvs(sc);
1005 
1006 	for (i = 0; i < sc->num_available_vsi; i++) {
1007 		if (sc->all_vsi[i])
1008 			ice_release_vsi(sc->all_vsi[i]);
1009 	}
1010 
1011 	if (sc->all_vsi) {
1012 		free(sc->all_vsi, M_ICE);
1013 		sc->all_vsi = NULL;
1014 	}
1015 
1016 	/* Release MSI-X memory */
1017 	pci_release_msi(sc->dev);
1018 
1019 	if (sc->msix_table != NULL) {
1020 		bus_release_resource(sc->dev, SYS_RES_MEMORY,
1021 				     rman_get_rid(sc->msix_table),
1022 				     sc->msix_table);
1023 		sc->msix_table = NULL;
1024 	}
1025 
1026 	ice_free_intr_tracking(sc);
1027 
1028 	/* Destroy the queue managers */
1029 	ice_resmgr_destroy(&sc->tx_qmgr);
1030 	ice_resmgr_destroy(&sc->rx_qmgr);
1031 
1032 	if (!ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1033 		ice_deinit_hw(&sc->hw);
1034 
1035 	IFLIB_CTX_UNLOCK(sc);
1036 	status = ice_reset(&sc->hw, ICE_RESET_PFR);
1037 	IFLIB_CTX_LOCK(sc);
1038 	if (status) {
1039 		device_printf(sc->dev, "device PF reset failed, err %s\n",
1040 			      ice_status_str(status));
1041 	}
1042 
1043 	ice_free_pci_mapping(sc);
1044 
1045 	return 0;
1046 } /* ice_if_detach */
1047 
1048 /**
1049  * ice_if_tx_queues_alloc - Allocate Tx queue memory
1050  * @ctx: iflib context structure
1051  * @vaddrs: virtual addresses for the queue memory
1052  * @paddrs: physical addresses for the queue memory
1053  * @ntxqs: the number of Tx queues per set (should always be 1)
1054  * @ntxqsets: the number of Tx queue sets to allocate
1055  *
1056  * Called by iflib to allocate Tx queues for the device. Allocates driver
1057  * memory to track each queue, the status arrays used for descriptor
1058  * status reporting, and Tx queue sysctls.
1059  */
1060 static int
1061 ice_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
1062 		       int __invariant_only ntxqs, int ntxqsets)
1063 {
1064 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1065 	struct ice_vsi *vsi = &sc->pf_vsi;
1066 	struct ice_tx_queue *txq;
1067 	int err, i, j;
1068 
1069 	MPASS(ntxqs == 1);
1070 	MPASS(sc->scctx->isc_ntxd[0] <= ICE_MAX_DESC_COUNT);
1071 	ASSERT_CTX_LOCKED(sc);
1072 
1073 	/* Do not bother allocating queues if we're in recovery mode */
1074 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1075 		return (0);
1076 
1077 	/* Allocate queue structure memory */
1078 	if (!(vsi->tx_queues =
1079 	      (struct ice_tx_queue *) malloc(sizeof(struct ice_tx_queue) * ntxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
1080 		device_printf(sc->dev, "Unable to allocate Tx queue memory\n");
1081 		return (ENOMEM);
1082 	}
1083 
1084 	/* Allocate report status arrays */
1085 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
1086 		if (!(txq->tx_rsq =
1087 		      (uint16_t *) malloc(sizeof(uint16_t) * sc->scctx->isc_ntxd[0], M_ICE, M_NOWAIT))) {
1088 			device_printf(sc->dev, "Unable to allocate tx_rsq memory\n");
1089 			err = ENOMEM;
1090 			goto free_tx_queues;
1091 		}
1092 		/* Initialize report status array */
1093 		for (j = 0; j < sc->scctx->isc_ntxd[0]; j++)
1094 			txq->tx_rsq[j] = QIDX_INVALID;
1095 	}
1096 
1097 	/* Assign queues from PF space to the main VSI */
1098 	err = ice_resmgr_assign_contiguous(&sc->tx_qmgr, vsi->tx_qmap, ntxqsets);
1099 	if (err) {
1100 		device_printf(sc->dev, "Unable to assign PF queues: %s\n",
1101 			      ice_err_str(err));
1102 		goto free_tx_queues;
1103 	}
1104 	vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
1105 
1106 	/* Add Tx queue sysctls context */
1107 	ice_vsi_add_txqs_ctx(vsi);
1108 
1109 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
1110 		/* q_handle == me when only one TC */
1111 		txq->me = txq->q_handle = i;
1112 		txq->vsi = vsi;
1113 
1114 		/* store the queue size for easier access */
1115 		txq->desc_count = sc->scctx->isc_ntxd[0];
1116 
1117 		/* get the virtual and physical address of the hardware queues */
1118 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
1119 		txq->tx_base = (struct ice_tx_desc *)vaddrs[i];
1120 		txq->tx_paddr = paddrs[i];
1121 
1122 		ice_add_txq_sysctls(txq);
1123 	}
1124 
1125 	vsi->num_tx_queues = ntxqsets;
1126 
1127 	return (0);
1128 
1129 free_tx_queues:
1130 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
1131 		if (txq->tx_rsq != NULL) {
1132 			free(txq->tx_rsq, M_ICE);
1133 			txq->tx_rsq = NULL;
1134 		}
1135 	}
1136 	free(vsi->tx_queues, M_ICE);
1137 	vsi->tx_queues = NULL;
1138 	return err;
1139 }
1140 
1141 /**
1142  * ice_if_rx_queues_alloc - Allocate Rx queue memory
1143  * @ctx: iflib context structure
1144  * @vaddrs: virtual addresses for the queue memory
1145  * @paddrs: physical addresses for the queue memory
1146  * @nrxqs: number of Rx queues per set (should always be 1)
1147  * @nrxqsets: number of Rx queue sets to allocate
1148  *
1149  * Called by iflib to allocate Rx queues for the device. Allocates driver
1150  * memory to track each queue, as well as sets up the Rx queue sysctls.
1151  */
1152 static int
1153 ice_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
1154 		       int __invariant_only nrxqs, int nrxqsets)
1155 {
1156 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1157 	struct ice_vsi *vsi = &sc->pf_vsi;
1158 	struct ice_rx_queue *rxq;
1159 	int err, i;
1160 
1161 	MPASS(nrxqs == 1);
1162 	MPASS(sc->scctx->isc_nrxd[0] <= ICE_MAX_DESC_COUNT);
1163 	ASSERT_CTX_LOCKED(sc);
1164 
1165 	/* Do not bother allocating queues if we're in recovery mode */
1166 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1167 		return (0);
1168 
1169 	/* Allocate queue structure memory */
1170 	if (!(vsi->rx_queues =
1171 	      (struct ice_rx_queue *) malloc(sizeof(struct ice_rx_queue) * nrxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
1172 		device_printf(sc->dev, "Unable to allocate Rx queue memory\n");
1173 		return (ENOMEM);
1174 	}
1175 
1176 	/* Assign queues from PF space to the main VSI */
1177 	err = ice_resmgr_assign_contiguous(&sc->rx_qmgr, vsi->rx_qmap, nrxqsets);
1178 	if (err) {
1179 		device_printf(sc->dev, "Unable to assign PF queues: %s\n",
1180 			      ice_err_str(err));
1181 		goto free_rx_queues;
1182 	}
1183 	vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
1184 
1185 	/* Add Rx queue sysctls context */
1186 	ice_vsi_add_rxqs_ctx(vsi);
1187 
1188 	for (i = 0, rxq = vsi->rx_queues; i < nrxqsets; i++, rxq++) {
1189 		rxq->me = i;
1190 		rxq->vsi = vsi;
1191 
1192 		/* store the queue size for easier access */
1193 		rxq->desc_count = sc->scctx->isc_nrxd[0];
1194 
1195 		/* get the virtual and physical address of the hardware queues */
1196 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
1197 		rxq->rx_base = (union ice_32b_rx_flex_desc *)vaddrs[i];
1198 		rxq->rx_paddr = paddrs[i];
1199 
1200 		ice_add_rxq_sysctls(rxq);
1201 	}
1202 
1203 	vsi->num_rx_queues = nrxqsets;
1204 
1205 	return (0);
1206 
1207 free_rx_queues:
1208 	free(vsi->rx_queues, M_ICE);
1209 	vsi->rx_queues = NULL;
1210 	return err;
1211 }
1212 
1213 /**
1214  * ice_if_queues_free - Free queue memory
1215  * @ctx: the iflib context structure
1216  *
1217  * Free queue memory allocated by ice_if_tx_queues_alloc() and
1218  * ice_if_rx_queues_alloc().
1219  *
1220  * There is no guarantee that ice_if_queues_free() and ice_if_detach() will be
1221  * called in the same order. It's possible for ice_if_queues_free() to be
1222  * called prior to ice_if_detach(), and vice versa.
1223  *
1224  * For this reason, the main VSI is a static member of the ice_softc, which is
1225  * not free'd until after iflib finishes calling both of these functions.
1226  *
1227  * Thus, care must be taken in how we manage the memory being freed by this
1228  * function, and in what tasks it can and must perform.
1229  */
1230 static void
1231 ice_if_queues_free(if_ctx_t ctx)
1232 {
1233 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1234 	struct ice_vsi *vsi = &sc->pf_vsi;
1235 	struct ice_tx_queue *txq;
1236 	int i;
1237 
1238 	/* Free the Tx and Rx sysctl contexts, and assign NULL to the node
1239 	 * pointers. Note, the calls here and those in ice_if_detach()
1240 	 * are *BOTH* necessary, as we cannot guarantee which path will be
1241 	 * run first
1242 	 */
1243 	ice_vsi_del_txqs_ctx(vsi);
1244 	ice_vsi_del_rxqs_ctx(vsi);
1245 
1246 	/* Release MSI-X IRQ vectors, if not yet released in ice_if_detach */
1247 	ice_free_irqvs(sc);
1248 
1249 	if (vsi->tx_queues != NULL) {
1250 		/* free the tx_rsq arrays */
1251 		for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
1252 			if (txq->tx_rsq != NULL) {
1253 				free(txq->tx_rsq, M_ICE);
1254 				txq->tx_rsq = NULL;
1255 			}
1256 		}
1257 		free(vsi->tx_queues, M_ICE);
1258 		vsi->tx_queues = NULL;
1259 		vsi->num_tx_queues = 0;
1260 	}
1261 	if (vsi->rx_queues != NULL) {
1262 		free(vsi->rx_queues, M_ICE);
1263 		vsi->rx_queues = NULL;
1264 		vsi->num_rx_queues = 0;
1265 	}
1266 }
1267 
1268 /**
1269  * ice_msix_que - Fast interrupt handler for MSI-X receive queues
1270  * @arg: The Rx queue memory
1271  *
1272  * Interrupt filter function for iflib MSI-X interrupts. Called by iflib when
1273  * an MSI-X interrupt for a given queue is triggered. Currently this just asks
1274  * iflib to schedule the main Rx thread.
1275  */
1276 static int
1277 ice_msix_que(void *arg)
1278 {
1279 	struct ice_rx_queue __unused *rxq = (struct ice_rx_queue *)arg;
1280 
1281 	/* TODO: dynamic ITR algorithm?? */
1282 
1283 	return (FILTER_SCHEDULE_THREAD);
1284 }
1285 
1286 /**
1287  * ice_msix_admin - Fast interrupt handler for MSI-X admin interrupt
1288  * @arg: pointer to device softc memory
1289  *
1290  * Called by iflib when an administrative interrupt occurs. Should perform any
1291  * fast logic for handling the interrupt cause, and then indicate whether the
1292  * admin task needs to be queued.
1293  */
1294 static int
1295 ice_msix_admin(void *arg)
1296 {
1297 	struct ice_softc *sc = (struct ice_softc *)arg;
1298 	struct ice_hw *hw = &sc->hw;
1299 	device_t dev = sc->dev;
1300 	u32 oicr;
1301 
1302 	/* There is no safe way to modify the enabled miscellaneous causes of
1303 	 * the OICR vector at runtime, as doing so would be prone to race
1304 	 * conditions. Reading PFINT_OICR will unmask the associated interrupt
1305 	 * causes and allow future interrupts to occur. The admin interrupt
1306 	 * vector will not be re-enabled until after we exit this function,
1307 	 * but any delayed tasks must be resilient against possible "late
1308 	 * arrival" interrupts that occur while we're already handling the
1309 	 * task. This is done by using state bits and serializing these
1310 	 * delayed tasks via the admin status task function.
1311 	 */
1312 	oicr = rd32(hw, PFINT_OICR);
1313 
1314 	/* Processing multiple controlq interrupts on a single vector does not
1315 	 * provide an indication of which controlq triggered the interrupt.
1316 	 * We might try reading the INTEVENT bit of the respective PFINT_*_CTL
1317 	 * registers. However, the INTEVENT bit is not guaranteed to be set as
1318 	 * it gets automatically cleared when the hardware acknowledges the
1319 	 * interrupt.
1320 	 *
1321 	 * This means we don't really have a good indication of whether or
1322 	 * which controlq triggered this interrupt. We'll just notify the
1323 	 * admin task that it should check all the controlqs.
1324 	 */
1325 	ice_set_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING);
1326 
1327 	if (oicr & PFINT_OICR_VFLR_M) {
1328 		ice_set_state(&sc->state, ICE_STATE_VFLR_PENDING);
1329 	}
1330 
1331 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
1332 		ice_set_state(&sc->state, ICE_STATE_MDD_PENDING);
1333 	}
1334 
1335 	if (oicr & PFINT_OICR_GRST_M) {
1336 		u32 reset;
1337 
1338 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1339 			GLGEN_RSTAT_RESET_TYPE_S;
1340 
1341 		if (reset == ICE_RESET_CORER)
1342 			sc->soft_stats.corer_count++;
1343 		else if (reset == ICE_RESET_GLOBR)
1344 			sc->soft_stats.globr_count++;
1345 		else
1346 			sc->soft_stats.empr_count++;
1347 
1348 		/* There are a couple of bits at play for handling resets.
1349 		 * First, the ICE_STATE_RESET_OICR_RECV bit is used to
1350 		 * indicate that the driver has received an OICR with a reset
1351 		 * bit active, indicating that a CORER/GLOBR/EMPR is about to
1352 		 * happen. Second, we set hw->reset_ongoing to indicate that
1353 		 * the hardware is in reset. We will set this back to false as
1354 		 * soon as the driver has determined that the hardware is out
1355 		 * of reset.
1356 		 *
1357 		 * If the driver wishes to trigger a request, it can set one of
1358 		 * the ICE_STATE_RESET_*_REQ bits, which will trigger the
1359 		 * correct type of reset.
1360 		 */
1361 		if (!ice_testandset_state(&sc->state, ICE_STATE_RESET_OICR_RECV)) {
1362 			hw->reset_ongoing = true;
1363 			/*
1364 			 * During the NVM update process, there is a driver reset and link
1365 			 * goes down and then up. The below if-statement prevents a second
1366 			 * link flap from occurring in ice_if_init().
1367 			 */
1368 			if (if_getflags(sc->ifp) & IFF_UP)
1369 				ice_set_state(&sc->state, ICE_STATE_FIRST_INIT_LINK);
1370 		}
1371 	}
1372 
1373 	if (oicr & PFINT_OICR_ECC_ERR_M) {
1374 		device_printf(dev, "ECC Error detected!\n");
1375 		ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
1376 	}
1377 
1378 	if (oicr & (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M)) {
1379 		if (oicr & PFINT_OICR_HMC_ERR_M)
1380 			/* Log the HMC errors */
1381 			ice_log_hmc_error(hw, dev);
1382 		ice_rdma_notify_pe_intr(sc, oicr);
1383 	}
1384 
1385 	if (oicr & PFINT_OICR_PCI_EXCEPTION_M) {
1386 		device_printf(dev, "PCI Exception detected!\n");
1387 		ice_set_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
1388 	}
1389 
1390 	return (FILTER_SCHEDULE_THREAD);
1391 }
1392 
1393 /**
1394  * ice_allocate_msix - Allocate MSI-X vectors for the interface
1395  * @sc: the device private softc
1396  *
1397  * Map the MSI-X bar, and then request MSI-X vectors in a two-stage process.
1398  *
1399  * First, determine a suitable total number of vectors based on the number
1400  * of CPUs, RSS buckets, the administrative vector, and other demands such as
1401  * RDMA.
1402  *
1403  * Request the desired amount of vectors, and see how many we obtain. If we
1404  * don't obtain as many as desired, reduce the demands by lowering the number
1405  * of requested queues or reducing the demand from other features such as
1406  * RDMA.
1407  *
1408  * @remark This function is required because the driver sets the
1409  * IFLIB_SKIP_MSIX flag indicating that the driver will manage MSI-X vectors
1410  * manually.
1411  *
1412  * @remark This driver will only use MSI-X vectors. If this is not possible,
1413  * neither MSI or legacy interrupts will be tried.
1414  *
1415  * @remark if it exists, os_imgr is initialized here for keeping track of
1416  * the assignments of extra MSIX vectors.
1417  *
1418  * @post on success this function must set the following scctx parameters:
1419  * isc_vectors, isc_nrxqsets, isc_ntxqsets, and isc_intr.
1420  *
1421  * @returns zero on success or an error code on failure.
1422  */
1423 static int
1424 ice_allocate_msix(struct ice_softc *sc)
1425 {
1426 	bool iflib_override_queue_count = false;
1427 	if_softc_ctx_t scctx = sc->scctx;
1428 	device_t dev = sc->dev;
1429 	cpuset_t cpus;
1430 	int bar, queues, vectors, requested;
1431 	int err = 0;
1432 	int rdma;
1433 
1434 	/* Allocate the MSI-X bar */
1435 	bar = scctx->isc_msix_bar;
1436 	sc->msix_table = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &bar, RF_ACTIVE);
1437 	if (!sc->msix_table) {
1438 		device_printf(dev, "Unable to map MSI-X table\n");
1439 		return (ENOMEM);
1440 	}
1441 
1442 	/* Check if the iflib queue count sysctls have been set */
1443 	if (sc->ifc_sysctl_ntxqs || sc->ifc_sysctl_nrxqs)
1444 		iflib_override_queue_count = true;
1445 
1446 	err = bus_get_cpus(dev, INTR_CPUS, sizeof(cpus), &cpus);
1447 	if (err) {
1448 		device_printf(dev, "%s: Unable to fetch the CPU list: %s\n",
1449 			      __func__, ice_err_str(err));
1450 		CPU_COPY(&all_cpus, &cpus);
1451 	}
1452 
1453 	/* Attempt to mimic behavior of iflib_msix_init */
1454 	if (iflib_override_queue_count) {
1455 		/*
1456 		 * If the override sysctls have been set, limit the queues to
1457 		 * the number of logical CPUs.
1458 		 */
1459 		queues = mp_ncpus;
1460 	} else {
1461 		/*
1462 		 * Otherwise, limit the queue count to the CPUs associated
1463 		 * with the NUMA node the device is associated with.
1464 		 */
1465 		queues = CPU_COUNT(&cpus);
1466 	}
1467 
1468 	/* Clamp to the number of RSS buckets */
1469 	queues = imin(queues, rss_getnumbuckets());
1470 
1471 	/*
1472 	 * Clamp the number of queue pairs to the minimum of the requested Tx
1473 	 * and Rx queues.
1474 	 */
1475 	queues = imin(queues, sc->ifc_sysctl_ntxqs ?: scctx->isc_ntxqsets);
1476 	queues = imin(queues, sc->ifc_sysctl_nrxqs ?: scctx->isc_nrxqsets);
1477 
1478 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_RDMA)) {
1479 		/*
1480 		 * Choose a number of RDMA vectors based on the number of CPUs
1481 		 * up to a maximum
1482 		 */
1483 		rdma = min(CPU_COUNT(&cpus), ICE_RDMA_MAX_MSIX);
1484 
1485 		/* Further limit by the user configurable tunable */
1486 		rdma = min(rdma, ice_rdma_max_msix);
1487 	} else {
1488 		rdma = 0;
1489 	}
1490 
1491 	/*
1492 	 * Determine the number of vectors to request. Note that we also need
1493 	 * to allocate one vector for administrative tasks.
1494 	 */
1495 	requested = rdma + queues + 1;
1496 	/* Add extra vectors requested by the user for later subinterface
1497 	 * creation.
1498 	 */
1499 	if_ctx_t ctx = sc->ctx;
1500 	u32 extra_vectors = iflib_get_extra_msix_vectors_sysctl(ctx);
1501 	requested += extra_vectors;
1502 
1503 	vectors = requested;
1504 	err = pci_alloc_msix(dev, &vectors);
1505 	if (err) {
1506 		device_printf(dev, "Failed to allocate %d MSI-X vectors, err %s\n",
1507 			      vectors, ice_err_str(err));
1508 		goto err_free_msix_table;
1509 	}
1510 
1511 	/* If we don't receive enough vectors, reduce demands */
1512 	if (vectors < requested) {
1513 		int diff = requested - vectors;
1514 
1515 		device_printf(dev, "Requested %d MSI-X vectors, but got only %d\n",
1516 			      requested, vectors);
1517 
1518 		diff += extra_vectors;
1519 		extra_vectors = 0;
1520 		/*
1521 		 * The OS didn't grant us the requested number of vectors.
1522 		 * Check to see if we can reduce demands by limiting the
1523 		 * number of vectors allocated to certain features.
1524 		 */
1525 
1526 		if (rdma >= diff) {
1527 			/* Reduce the number of RDMA vectors we reserve */
1528 			rdma -= diff;
1529 			diff = 0;
1530 		} else {
1531 			/* Disable RDMA and reduce the difference */
1532 			ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
1533 			diff -= rdma;
1534 			rdma = 0;
1535 		}
1536 
1537 		/*
1538 		 * If we still have a difference, we need to reduce the number
1539 		 * of queue pairs.
1540 		 *
1541 		 * However, we still need at least one vector for the admin
1542 		 * interrupt and one queue pair.
1543 		 */
1544 		if (queues <= diff) {
1545 			device_printf(dev, "Unable to allocate sufficient MSI-X vectors\n");
1546 			err = (ERANGE);
1547 			goto err_pci_release_msi;
1548 		}
1549 
1550 		queues -= diff;
1551 	}
1552 
1553 	device_printf(dev, "Using %d Tx and Rx queues\n", queues);
1554 	if (rdma)
1555 		device_printf(dev, "Reserving %d MSI-X interrupts for iRDMA\n",
1556 			      rdma);
1557 	device_printf(dev, "Using MSI-X interrupts with %d vectors\n",
1558 		      vectors);
1559 
1560 	/* Split resulting vectors back into requested splits */
1561 	scctx->isc_vectors = vectors;
1562 	scctx->isc_nrxqsets = queues;
1563 	scctx->isc_ntxqsets = queues;
1564 	scctx->isc_intr = IFLIB_INTR_MSIX;
1565 
1566 	sc->irdma_vectors = rdma;
1567 
1568 	/* Interrupt allocation tracking isn't required in recovery mode,
1569 	 * since neither RDMA nor VFs are enabled.
1570 	 */
1571 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1572 		return (0);
1573 
1574 	/* Keep track of which interrupt indices are being used for what */
1575 	sc->lan_vectors = vectors - rdma;
1576 	sc->lan_vectors -= extra_vectors;
1577 	err = ice_resmgr_assign_contiguous(&sc->dev_imgr, sc->pf_imap, sc->lan_vectors);
1578 	if (err) {
1579 		device_printf(dev, "Unable to assign PF interrupt mapping: %s\n",
1580 			      ice_err_str(err));
1581 		goto err_pci_release_msi;
1582 	}
1583 	err = ice_resmgr_assign_contiguous(&sc->dev_imgr, sc->rdma_imap, rdma);
1584 	if (err) {
1585 		device_printf(dev, "Unable to assign PF RDMA interrupt mapping: %s\n",
1586 			      ice_err_str(err));
1587 		goto err_release_pf_imap;
1588 	}
1589 	sc->extra_vectors = extra_vectors;
1590 	/* Setup another resource manager to track the assignments of extra OS
1591 	 * vectors. These OS interrupt allocations don't need to be contiguous,
1592 	 * unlike the ones that come from the device.
1593 	 */
1594 	err = ice_resmgr_init(&sc->os_imgr, sc->extra_vectors);
1595 	if (err) {
1596 		device_printf(dev, "Unable to initialize OS extra interrupt manager: %s\n",
1597 			      ice_err_str(err));
1598 		ice_resmgr_release_map(&sc->dev_imgr, sc->rdma_imap,
1599 					    rdma);
1600 		goto err_release_pf_imap;
1601 	}
1602 	return (0);
1603 
1604 err_release_pf_imap:
1605 	ice_resmgr_release_map(&sc->dev_imgr, sc->pf_imap,
1606 				    sc->lan_vectors);
1607 err_pci_release_msi:
1608 	pci_release_msi(dev);
1609 err_free_msix_table:
1610 	if (sc->msix_table != NULL) {
1611 		bus_release_resource(sc->dev, SYS_RES_MEMORY,
1612 				rman_get_rid(sc->msix_table),
1613 				sc->msix_table);
1614 		sc->msix_table = NULL;
1615 	}
1616 
1617 	return (err);
1618 }
1619 
1620 /**
1621  * ice_if_msix_intr_assign - Assign MSI-X interrupt vectors to queues
1622  * @ctx: the iflib context structure
1623  * @msix: the number of vectors we were assigned
1624  *
1625  * Called by iflib to assign MSI-X vectors to queues. Currently requires that
1626  * we get at least the same number of vectors as we have queues, and that we
1627  * always have the same number of Tx and Rx queues.
1628  *
1629  * Tx queues use a softirq instead of using their own hardware interrupt.
1630  */
1631 static int
1632 ice_if_msix_intr_assign(if_ctx_t ctx, int msix)
1633 {
1634 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1635 	struct ice_vsi *vsi = &sc->pf_vsi;
1636 	int err, i, vector;
1637 
1638 	ASSERT_CTX_LOCKED(sc);
1639 
1640 	if (vsi->num_rx_queues != vsi->num_tx_queues) {
1641 		device_printf(sc->dev,
1642 			      "iflib requested %d Tx queues, and %d Rx queues, but the driver isn't able to support a differing number of Tx and Rx queues\n",
1643 			      vsi->num_tx_queues, vsi->num_rx_queues);
1644 		return (EOPNOTSUPP);
1645 	}
1646 
1647 	if (msix < (vsi->num_rx_queues + 1)) {
1648 		device_printf(sc->dev,
1649 			      "Not enough MSI-X vectors to assign one vector to each queue pair\n");
1650 		return (EOPNOTSUPP);
1651 	}
1652 
1653 	/* Save the number of vectors for future use */
1654 	sc->num_irq_vectors = vsi->num_rx_queues + 1;
1655 
1656 	/* Allocate space to store the IRQ vector data */
1657 	if (!(sc->irqvs =
1658 	      (struct ice_irq_vector *) malloc(sizeof(struct ice_irq_vector) * (sc->num_irq_vectors),
1659 					       M_ICE, M_NOWAIT))) {
1660 		device_printf(sc->dev,
1661 			      "Unable to allocate irqv memory\n");
1662 		return (ENOMEM);
1663 	}
1664 
1665 	/* Administrative interrupt events will use vector 0 */
1666 	err = iflib_irq_alloc_generic(ctx, &sc->irqvs[0].irq, 1, IFLIB_INTR_ADMIN,
1667 				      ice_msix_admin, sc, 0, "admin");
1668 	if (err) {
1669 		device_printf(sc->dev,
1670 			      "Failed to register Admin queue handler: %s\n",
1671 			      ice_err_str(err));
1672 		goto free_irqvs;
1673 	}
1674 	sc->irqvs[0].me = 0;
1675 
1676 	/* Do not allocate queue interrupts when in recovery mode */
1677 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1678 		return (0);
1679 
1680 	int rid;
1681 	for (i = 0, vector = 1; i < vsi->num_rx_queues; i++, vector++) {
1682 		struct ice_rx_queue *rxq = &vsi->rx_queues[i];
1683 		struct ice_tx_queue *txq = &vsi->tx_queues[i];
1684 		char irq_name[16];
1685 
1686 		rid = vector + 1;
1687 
1688 		snprintf(irq_name, sizeof(irq_name), "rxq%d", i);
1689 		err = iflib_irq_alloc_generic(ctx, &sc->irqvs[vector].irq, rid,
1690 					      IFLIB_INTR_RXTX, ice_msix_que,
1691 					      rxq, rxq->me, irq_name);
1692 		if (err) {
1693 			device_printf(sc->dev,
1694 				      "Failed to allocate q int %d err: %s\n",
1695 				      i, ice_err_str(err));
1696 			vector--;
1697 			i--;
1698 			goto fail;
1699 		}
1700 		sc->irqvs[vector].me = vector;
1701 		rxq->irqv = &sc->irqvs[vector];
1702 
1703 		bzero(irq_name, sizeof(irq_name));
1704 
1705 		snprintf(irq_name, sizeof(irq_name), "txq%d", i);
1706 		iflib_softirq_alloc_generic(ctx, &sc->irqvs[vector].irq,
1707 					    IFLIB_INTR_TX, txq,
1708 					    txq->me, irq_name);
1709 		txq->irqv = &sc->irqvs[vector];
1710 	}
1711 
1712 	/* For future interrupt assignments */
1713 	sc->last_rid = rid + sc->irdma_vectors;
1714 
1715 #ifdef PCI_IOV
1716 	/* Create soft IRQ for handling VF resets */
1717 	iflib_softirq_alloc_generic(ctx, NULL, IFLIB_INTR_IOV, sc, 0, "iov");
1718 #endif
1719 
1720 	return (0);
1721 fail:
1722 	for (; i >= 0; i--, vector--)
1723 		iflib_irq_free(ctx, &sc->irqvs[vector].irq);
1724 	iflib_irq_free(ctx, &sc->irqvs[0].irq);
1725 free_irqvs:
1726 	free(sc->irqvs, M_ICE);
1727 	sc->irqvs = NULL;
1728 	return err;
1729 }
1730 
1731 /**
1732  * ice_if_mtu_set - Set the device MTU
1733  * @ctx: iflib context structure
1734  * @mtu: the MTU requested
1735  *
1736  * Called by iflib to configure the device's Maximum Transmission Unit (MTU).
1737  *
1738  * @pre assumes the caller holds the iflib CTX lock
1739  */
1740 static int
1741 ice_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
1742 {
1743 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1744 
1745 	ASSERT_CTX_LOCKED(sc);
1746 
1747 	/* Do not support configuration when in recovery mode */
1748 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1749 		return (ENOSYS);
1750 
1751 	if (mtu < ICE_MIN_MTU || mtu > ICE_MAX_MTU)
1752 		return (EINVAL);
1753 
1754 	sc->scctx->isc_max_frame_size = mtu +
1755 		ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN;
1756 
1757 	sc->pf_vsi.max_frame_size = sc->scctx->isc_max_frame_size;
1758 
1759 	return (0);
1760 }
1761 
1762 /**
1763  * ice_if_intr_enable - Enable device interrupts
1764  * @ctx: iflib context structure
1765  *
1766  * Called by iflib to request enabling device interrupts.
1767  */
1768 static void
1769 ice_if_intr_enable(if_ctx_t ctx)
1770 {
1771 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1772 	struct ice_vsi *vsi = &sc->pf_vsi;
1773 	struct ice_hw *hw = &sc->hw;
1774 
1775 	ASSERT_CTX_LOCKED(sc);
1776 
1777 	/* Enable ITR 0 */
1778 	ice_enable_intr(hw, sc->irqvs[0].me);
1779 
1780 	/* Do not enable queue interrupts in recovery mode */
1781 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1782 		return;
1783 
1784 	/* Enable all queue interrupts */
1785 	for (int i = 0; i < vsi->num_rx_queues; i++)
1786 		ice_enable_intr(hw, vsi->rx_queues[i].irqv->me);
1787 }
1788 
1789 /**
1790  * ice_if_intr_disable - Disable device interrupts
1791  * @ctx: iflib context structure
1792  *
1793  * Called by iflib to request disabling device interrupts.
1794  */
1795 static void
1796 ice_if_intr_disable(if_ctx_t ctx)
1797 {
1798 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1799 	struct ice_hw *hw = &sc->hw;
1800 	unsigned int i;
1801 
1802 	ASSERT_CTX_LOCKED(sc);
1803 
1804 	/* IFDI_INTR_DISABLE may be called prior to interrupts actually being
1805 	 * assigned to queues. Instead of assuming that the interrupt
1806 	 * assignment in the rx_queues structure is valid, just disable all
1807 	 * possible interrupts
1808 	 *
1809 	 * Note that we choose not to disable ITR 0 because this handles the
1810 	 * AdminQ interrupts, and we want to keep processing these even when
1811 	 * the interface is offline.
1812 	 */
1813 	for (i = 1; i < hw->func_caps.common_cap.num_msix_vectors; i++)
1814 		ice_disable_intr(hw, i);
1815 }
1816 
1817 /**
1818  * ice_if_rx_queue_intr_enable - Enable a specific Rx queue interrupt
1819  * @ctx: iflib context structure
1820  * @rxqid: the Rx queue to enable
1821  *
1822  * Enable a specific Rx queue interrupt.
1823  *
1824  * This function is not protected by the iflib CTX lock.
1825  */
1826 static int
1827 ice_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
1828 {
1829 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1830 	struct ice_vsi *vsi = &sc->pf_vsi;
1831 	struct ice_hw *hw = &sc->hw;
1832 
1833 	/* Do not enable queue interrupts in recovery mode */
1834 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1835 		return (ENOSYS);
1836 
1837 	ice_enable_intr(hw, vsi->rx_queues[rxqid].irqv->me);
1838 	return (0);
1839 }
1840 
1841 /**
1842  * ice_if_tx_queue_intr_enable - Enable a specific Tx queue interrupt
1843  * @ctx: iflib context structure
1844  * @txqid: the Tx queue to enable
1845  *
1846  * Enable a specific Tx queue interrupt.
1847  *
1848  * This function is not protected by the iflib CTX lock.
1849  */
1850 static int
1851 ice_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
1852 {
1853 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1854 	struct ice_vsi *vsi = &sc->pf_vsi;
1855 	struct ice_hw *hw = &sc->hw;
1856 
1857 	/* Do not enable queue interrupts in recovery mode */
1858 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1859 		return (ENOSYS);
1860 
1861 	ice_enable_intr(hw, vsi->tx_queues[txqid].irqv->me);
1862 	return (0);
1863 }
1864 
1865 /**
1866  * ice_set_default_promisc_mask - Set default config for promisc settings
1867  * @promisc_mask: bitmask to setup
1868  *
1869  * The ice_(set|clear)_vsi_promisc() function expects a mask of promiscuous
1870  * modes to operate on. The mask used in here is the default one for the
1871  * driver, where promiscuous is enabled/disabled for all types of
1872  * non-VLAN-tagged/VLAN 0 traffic.
1873  */
1874 static void
1875 ice_set_default_promisc_mask(ice_bitmap_t *promisc_mask)
1876 {
1877 	ice_zero_bitmap(promisc_mask, ICE_PROMISC_MAX);
1878 	ice_set_bit(ICE_PROMISC_UCAST_TX, promisc_mask);
1879 	ice_set_bit(ICE_PROMISC_UCAST_RX, promisc_mask);
1880 	ice_set_bit(ICE_PROMISC_MCAST_TX, promisc_mask);
1881 	ice_set_bit(ICE_PROMISC_MCAST_RX, promisc_mask);
1882 }
1883 
1884 /**
1885  * ice_if_promisc_set - Set device promiscuous mode
1886  * @ctx: iflib context structure
1887  * @flags: promiscuous flags to configure
1888  *
1889  * Called by iflib to configure device promiscuous mode.
1890  *
1891  * @remark Calls to this function will always overwrite the previous setting
1892  */
1893 static int
1894 ice_if_promisc_set(if_ctx_t ctx, int flags)
1895 {
1896 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1897 	struct ice_hw *hw = &sc->hw;
1898 	device_t dev = sc->dev;
1899 	int status;
1900 	bool promisc_enable = flags & IFF_PROMISC;
1901 	bool multi_enable = flags & IFF_ALLMULTI;
1902 	ice_declare_bitmap(promisc_mask, ICE_PROMISC_MAX);
1903 
1904 	/* Do not support configuration when in recovery mode */
1905 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1906 		return (ENOSYS);
1907 
1908 	ice_set_default_promisc_mask(promisc_mask);
1909 
1910 	if (multi_enable)
1911 		return (EOPNOTSUPP);
1912 
1913 	if (promisc_enable) {
1914 		status = ice_set_vsi_promisc(hw, sc->pf_vsi.idx,
1915 					     promisc_mask, 0);
1916 		if (status && status != ICE_ERR_ALREADY_EXISTS) {
1917 			device_printf(dev,
1918 				      "Failed to enable promiscuous mode for PF VSI, err %s aq_err %s\n",
1919 				      ice_status_str(status),
1920 				      ice_aq_str(hw->adminq.sq_last_status));
1921 			return (EIO);
1922 		}
1923 	} else {
1924 		status = ice_clear_vsi_promisc(hw, sc->pf_vsi.idx,
1925 					       promisc_mask, 0);
1926 		if (status) {
1927 			device_printf(dev,
1928 				      "Failed to disable promiscuous mode for PF VSI, err %s aq_err %s\n",
1929 				      ice_status_str(status),
1930 				      ice_aq_str(hw->adminq.sq_last_status));
1931 			return (EIO);
1932 		}
1933 	}
1934 
1935 	return (0);
1936 }
1937 
1938 /**
1939  * ice_if_media_change - Change device media
1940  * @ctx: device ctx structure
1941  *
1942  * Called by iflib when a media change is requested. This operation is not
1943  * supported by the hardware, so we just return an error code.
1944  */
1945 static int
1946 ice_if_media_change(if_ctx_t ctx)
1947 {
1948 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1949 
1950 	device_printf(sc->dev, "Media change is not supported.\n");
1951 	return (ENODEV);
1952 }
1953 
1954 /**
1955  * ice_if_media_status - Report current device media
1956  * @ctx: iflib context structure
1957  * @ifmr: ifmedia request structure to update
1958  *
1959  * Updates the provided ifmr with current device media status, including link
1960  * status and media type.
1961  */
1962 static void
1963 ice_if_media_status(if_ctx_t ctx, struct ifmediareq *ifmr)
1964 {
1965 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
1966 	struct ice_link_status *li = &sc->hw.port_info->phy.link_info;
1967 
1968 	ifmr->ifm_status = IFM_AVALID;
1969 	ifmr->ifm_active = IFM_ETHER;
1970 
1971 	/* Never report link up or media types when in recovery mode */
1972 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
1973 		return;
1974 
1975 	if (!sc->link_up)
1976 		return;
1977 
1978 	ifmr->ifm_status |= IFM_ACTIVE;
1979 	ifmr->ifm_active |= IFM_FDX;
1980 
1981 	if (li->phy_type_low)
1982 		ifmr->ifm_active |= ice_get_phy_type_low(li->phy_type_low);
1983 	else if (li->phy_type_high)
1984 		ifmr->ifm_active |= ice_get_phy_type_high(li->phy_type_high);
1985 	else
1986 		ifmr->ifm_active |= IFM_UNKNOWN;
1987 
1988 	/* Report flow control status as well */
1989 	if (li->an_info & ICE_AQ_LINK_PAUSE_TX)
1990 		ifmr->ifm_active |= IFM_ETH_TXPAUSE;
1991 	if (li->an_info & ICE_AQ_LINK_PAUSE_RX)
1992 		ifmr->ifm_active |= IFM_ETH_RXPAUSE;
1993 }
1994 
1995 /**
1996  * ice_init_tx_tracking - Initialize Tx queue software tracking values
1997  * @vsi: the VSI to initialize
1998  *
1999  * Initialize Tx queue software tracking values, including the Report Status
2000  * queue, and related software tracking values.
2001  */
2002 static void
2003 ice_init_tx_tracking(struct ice_vsi *vsi)
2004 {
2005 	struct ice_tx_queue *txq;
2006 	size_t j;
2007 	int i;
2008 
2009 	for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
2010 
2011 		txq->tx_rs_cidx = txq->tx_rs_pidx = 0;
2012 
2013 		/* Initialize the last processed descriptor to be the end of
2014 		 * the ring, rather than the start, so that we avoid an
2015 		 * off-by-one error in ice_ift_txd_credits_update for the
2016 		 * first packet.
2017 		 */
2018 		txq->tx_cidx_processed = txq->desc_count - 1;
2019 
2020 		for (j = 0; j < txq->desc_count; j++)
2021 			txq->tx_rsq[j] = QIDX_INVALID;
2022 	}
2023 }
2024 
2025 /**
2026  * ice_update_rx_mbuf_sz - Update the Rx buffer size for all queues
2027  * @sc: the device softc
2028  *
2029  * Called to update the Rx queue mbuf_sz parameter for configuring the receive
2030  * buffer sizes when programming hardware.
2031  */
2032 static void
2033 ice_update_rx_mbuf_sz(struct ice_softc *sc)
2034 {
2035 	uint32_t mbuf_sz = iflib_get_rx_mbuf_sz(sc->ctx);
2036 	struct ice_vsi *vsi = &sc->pf_vsi;
2037 
2038 	MPASS(mbuf_sz <= UINT16_MAX);
2039 	vsi->mbuf_sz = mbuf_sz;
2040 }
2041 
2042 /**
2043  * ice_if_init - Initialize the device
2044  * @ctx: iflib ctx structure
2045  *
2046  * Called by iflib to bring the device up, i.e. ifconfig ice0 up. Initializes
2047  * device filters and prepares the Tx and Rx engines.
2048  *
2049  * @pre assumes the caller holds the iflib CTX lock
2050  */
2051 static void
2052 ice_if_init(if_ctx_t ctx)
2053 {
2054 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
2055 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2056 	device_t dev = sc->dev;
2057 	int err;
2058 
2059 	ASSERT_CTX_LOCKED(sc);
2060 
2061 	/*
2062 	 * We've seen an issue with 11.3/12.1 where sideband routines are
2063 	 * called after detach is called.  This would call routines after
2064 	 * if_stop, causing issues with the teardown process.  This has
2065 	 * seemingly been fixed in STABLE snapshots, but it seems like a
2066 	 * good idea to have this guard here regardless.
2067 	 */
2068 	if (ice_driver_is_detaching(sc))
2069 		return;
2070 
2071 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
2072 		return;
2073 
2074 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
2075 		device_printf(sc->dev, "request to start interface cannot be completed as the device failed to reset\n");
2076 		return;
2077 	}
2078 
2079 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
2080 		device_printf(sc->dev, "request to start interface while device is prepared for impending reset\n");
2081 		return;
2082 	}
2083 
2084 	ice_update_rx_mbuf_sz(sc);
2085 
2086 	/* Update the MAC address... User might use a LAA */
2087 	err = ice_update_laa_mac(sc);
2088 	if (err) {
2089 		device_printf(dev,
2090 			      "LAA address change failed, err %s\n",
2091 			      ice_err_str(err));
2092 		return;
2093 	}
2094 
2095 	/* Initialize software Tx tracking values */
2096 	ice_init_tx_tracking(&sc->pf_vsi);
2097 
2098 	err = ice_cfg_vsi_for_tx(&sc->pf_vsi);
2099 	if (err) {
2100 		device_printf(dev,
2101 			      "Unable to configure the main VSI for Tx: %s\n",
2102 			      ice_err_str(err));
2103 		return;
2104 	}
2105 
2106 	err = ice_cfg_vsi_for_rx(&sc->pf_vsi);
2107 	if (err) {
2108 		device_printf(dev,
2109 			      "Unable to configure the main VSI for Rx: %s\n",
2110 			      ice_err_str(err));
2111 		goto err_cleanup_tx;
2112 	}
2113 
2114 	err = ice_control_all_rx_queues(&sc->pf_vsi, true);
2115 	if (err) {
2116 		device_printf(dev,
2117 			      "Unable to enable Rx rings for transmit: %s\n",
2118 			      ice_err_str(err));
2119 		goto err_cleanup_tx;
2120 	}
2121 
2122 	err = ice_cfg_pf_default_mac_filters(sc);
2123 	if (err) {
2124 		device_printf(dev,
2125 			      "Unable to configure default MAC filters: %s\n",
2126 			      ice_err_str(err));
2127 		goto err_stop_rx;
2128 	}
2129 
2130 	/* We use software interrupts for Tx, so we only program the hardware
2131 	 * interrupts for Rx.
2132 	 */
2133 	ice_configure_all_rxq_interrupts(&sc->pf_vsi);
2134 	ice_configure_rx_itr(&sc->pf_vsi);
2135 
2136 	/* Configure promiscuous mode */
2137 	ice_if_promisc_set(ctx, if_getflags(sc->ifp));
2138 
2139 	if (!ice_testandclear_state(&sc->state, ICE_STATE_FIRST_INIT_LINK))
2140 		if (!sc->link_up && ((if_getflags(sc->ifp) & IFF_UP) ||
2141 			 ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN)))
2142 			ice_set_link(sc, true);
2143 
2144 	ice_rdma_pf_init(sc);
2145 
2146 	ice_set_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED);
2147 
2148 	if (sc->mirr_if && ice_testandclear_state(&mif->state, ICE_STATE_SUBIF_NEEDS_REINIT)) {
2149 		ice_clear_state(&mif->state, ICE_STATE_DRIVER_INITIALIZED);
2150 		iflib_request_reset(sc->mirr_if->subctx);
2151 		iflib_admin_intr_deferred(sc->mirr_if->subctx);
2152 	}
2153 
2154 	return;
2155 
2156 err_stop_rx:
2157 	ice_control_all_rx_queues(&sc->pf_vsi, false);
2158 err_cleanup_tx:
2159 	ice_vsi_disable_tx(&sc->pf_vsi);
2160 }
2161 
2162 /**
2163  * ice_poll_for_media_avail - Re-enable link if media is detected
2164  * @sc: device private structure
2165  *
2166  * Intended to be called from the driver's timer function, this function
2167  * sends the Get Link Status AQ command and re-enables HW link if the
2168  * command says that media is available.
2169  *
2170  * If the driver doesn't have the "NO_MEDIA" state set, then this does nothing,
2171  * since media removal events are supposed to be sent to the driver through
2172  * a link status event.
2173  */
2174 static void
2175 ice_poll_for_media_avail(struct ice_softc *sc)
2176 {
2177 	struct ice_hw *hw = &sc->hw;
2178 	struct ice_port_info *pi = hw->port_info;
2179 
2180 	/* E830 only: There's no interrupt for when the PHY FW has finished loading,
2181 	 * so poll for the status in the media task here if it's previously
2182 	 * been detected that it's still loading.
2183 	 */
2184 	if (ice_is_e830(hw) &&
2185 	    ice_test_state(&sc->state, ICE_STATE_PHY_FW_INIT_PENDING)) {
2186 		if (rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M)
2187 			ice_clear_state(&sc->state, ICE_STATE_PHY_FW_INIT_PENDING);
2188 		else
2189 			return;
2190 	}
2191 
2192 	if (ice_test_state(&sc->state, ICE_STATE_NO_MEDIA)) {
2193 		pi->phy.get_link_info = true;
2194 		ice_get_link_status(pi, &sc->link_up);
2195 
2196 		if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2197 			int status;
2198 
2199 			/* Re-enable link and re-apply user link settings */
2200 			if (ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) ||
2201 			    (if_getflags(sc->ifp) & IFF_UP)) {
2202 				ice_apply_saved_phy_cfg(sc, ICE_APPLY_LS_FEC_FC);
2203 
2204 				/* Update the OS about changes in media capability */
2205 				status = ice_add_media_types(sc, sc->media);
2206 				if (status)
2207 					device_printf(sc->dev,
2208 					    "Error adding device media types: %s aq_err %s\n",
2209 					    ice_status_str(status),
2210 					    ice_aq_str(hw->adminq.sq_last_status));
2211 			}
2212 
2213 			ice_clear_state(&sc->state, ICE_STATE_NO_MEDIA);
2214 		}
2215 	}
2216 }
2217 
2218 /**
2219  * ice_if_timer - called by iflib periodically
2220  * @ctx: iflib ctx structure
2221  * @qid: the queue this timer was called for
2222  *
2223  * This callback is triggered by iflib periodically. We use it to update the
2224  * hw statistics.
2225  *
2226  * @remark this function is not protected by the iflib CTX lock.
2227  */
2228 static void
2229 ice_if_timer(if_ctx_t ctx, uint16_t qid)
2230 {
2231 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2232 	uint64_t prev_link_xoff_rx = sc->stats.cur.link_xoff_rx;
2233 
2234 	if (qid != 0)
2235 		return;
2236 
2237 	/* Do not attempt to update stats when in recovery mode */
2238 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
2239 		return;
2240 
2241 	/* Update device statistics */
2242 	ice_update_pf_stats(sc);
2243 
2244 	/*
2245 	 * For proper watchdog management, the iflib stack needs to know if
2246 	 * we've been paused during the last interval. Check if the
2247 	 * link_xoff_rx stat changed, and set the isc_pause_frames, if so.
2248 	 */
2249 	if (sc->stats.cur.link_xoff_rx != prev_link_xoff_rx)
2250 		sc->scctx->isc_pause_frames = 1;
2251 
2252 	/* Update the primary VSI stats */
2253 	ice_update_vsi_hw_stats(&sc->pf_vsi);
2254 
2255 	/* Update mirror VSI stats */
2256 	if (sc->mirr_if && sc->mirr_if->if_attached)
2257 		ice_update_vsi_hw_stats(sc->mirr_if->vsi);
2258 }
2259 
2260 /**
2261  * ice_admin_timer - called periodically to trigger the admin task
2262  * @arg: callout(9) argument pointing to the device private softc structure
2263  *
2264  * Timer function used as part of a callout(9) timer that will periodically
2265  * trigger the admin task, even when the interface is down.
2266  *
2267  * @remark this function is not called by iflib and is not protected by the
2268  * iflib CTX lock.
2269  *
2270  * @remark because this is a callout function, it cannot sleep and should not
2271  * attempt taking the iflib CTX lock.
2272  */
2273 static void
2274 ice_admin_timer(void *arg)
2275 {
2276 	struct ice_softc *sc = (struct ice_softc *)arg;
2277 
2278 	/*
2279 	 * There is a point where callout routines are no longer
2280 	 * cancelable.  So there exists a window of time where the
2281 	 * driver enters detach() and tries to cancel the callout, but the
2282 	 * callout routine has passed the cancellation point.  The detach()
2283 	 * routine is unaware of this and tries to free resources that the
2284 	 * callout routine needs.  So we check for the detach state flag to
2285 	 * at least shrink the window of opportunity.
2286 	 */
2287 	if (ice_driver_is_detaching(sc))
2288 		return;
2289 
2290 	/* Fire off the admin task */
2291 	iflib_admin_intr_deferred(sc->ctx);
2292 
2293 	/* Reschedule the admin timer */
2294 	callout_schedule(&sc->admin_timer, hz/2);
2295 }
2296 
2297 /**
2298  * ice_transition_recovery_mode - Transition to recovery mode
2299  * @sc: the device private softc
2300  *
2301  * Called when the driver detects that the firmware has entered recovery mode
2302  * at run time.
2303  */
2304 static void
2305 ice_transition_recovery_mode(struct ice_softc *sc)
2306 {
2307 	struct ice_vsi *vsi = &sc->pf_vsi;
2308 	int i;
2309 
2310 	device_printf(sc->dev, "Firmware recovery mode detected. Limiting functionality. Refer to Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
2311 
2312 	/* Tell the stack that the link has gone down */
2313 	iflib_link_state_change(sc->ctx, LINK_STATE_DOWN, 0);
2314 
2315 	/* Request that the device be re-initialized */
2316 	ice_request_stack_reinit(sc);
2317 
2318 	ice_rdma_pf_detach(sc);
2319 	ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
2320 
2321 #ifdef PCI_IOV
2322 	if (ice_test_and_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en))
2323 		 ice_iov_detach(sc);
2324 #else
2325 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en);
2326 #endif /* PCI_IOV */
2327 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
2328 
2329 	ice_vsi_del_txqs_ctx(vsi);
2330 	ice_vsi_del_rxqs_ctx(vsi);
2331 
2332 	for (i = 0; i < sc->num_available_vsi; i++) {
2333 		if (sc->all_vsi[i])
2334 			ice_release_vsi(sc->all_vsi[i]);
2335 	}
2336 	sc->num_available_vsi = 0;
2337 
2338 	if (sc->all_vsi) {
2339 		free(sc->all_vsi, M_ICE);
2340 		sc->all_vsi = NULL;
2341 	}
2342 
2343 	/* Destroy the interrupt manager */
2344 	ice_resmgr_destroy(&sc->dev_imgr);
2345 	/* Destroy the queue managers */
2346 	ice_resmgr_destroy(&sc->tx_qmgr);
2347 	ice_resmgr_destroy(&sc->rx_qmgr);
2348 
2349 	ice_deinit_hw(&sc->hw);
2350 }
2351 
2352 /**
2353  * ice_transition_safe_mode - Transition to safe mode
2354  * @sc: the device private softc
2355  *
2356  * Called when the driver attempts to reload the DDP package during a device
2357  * reset, and the new download fails. If so, we must transition to safe mode
2358  * at run time.
2359  *
2360  * @remark although safe mode normally allocates only a single queue, we can't
2361  * change the number of queues dynamically when using iflib. Due to this, we
2362  * do not attempt to reduce the number of queues.
2363  */
2364 static void
2365 ice_transition_safe_mode(struct ice_softc *sc)
2366 {
2367 	/* Indicate that we are in Safe mode */
2368 	ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_cap);
2369 	ice_set_bit(ICE_FEATURE_SAFE_MODE, sc->feat_en);
2370 
2371 	ice_rdma_pf_detach(sc);
2372 	ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
2373 
2374 #ifdef PCI_IOV
2375 	if (ice_test_and_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en))
2376 		 ice_iov_detach(sc);
2377 #else
2378 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en);
2379 #endif /* PCI_IOV */
2380 	ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
2381 
2382 	ice_clear_bit(ICE_FEATURE_RSS, sc->feat_cap);
2383 	ice_clear_bit(ICE_FEATURE_RSS, sc->feat_en);
2384 }
2385 
2386 /**
2387  * ice_if_update_admin_status - update admin status
2388  * @ctx: iflib ctx structure
2389  *
2390  * Called by iflib to update the admin status. For our purposes, this means
2391  * check the adminq, and update the link status. It's ultimately triggered by
2392  * our admin interrupt, or by the ice_if_timer periodically.
2393  *
2394  * @pre assumes the caller holds the iflib CTX lock
2395  */
2396 static void
2397 ice_if_update_admin_status(if_ctx_t ctx)
2398 {
2399 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
2400 	enum ice_fw_modes fw_mode;
2401 	bool reschedule = false;
2402 	u16 pending = 0;
2403 
2404 	ASSERT_CTX_LOCKED(sc);
2405 
2406 	/* Check if the firmware entered recovery mode at run time */
2407 	fw_mode = ice_get_fw_mode(&sc->hw);
2408 	if (fw_mode == ICE_FW_MODE_REC) {
2409 		if (!ice_testandset_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
2410 			/* If we just entered recovery mode, log a warning to
2411 			 * the system administrator and deinit driver state
2412 			 * that is no longer functional.
2413 			 */
2414 			ice_transition_recovery_mode(sc);
2415 		}
2416 	} else if (fw_mode == ICE_FW_MODE_ROLLBACK) {
2417 		if (!ice_testandset_state(&sc->state, ICE_STATE_ROLLBACK_MODE)) {
2418 			/* Rollback mode isn't fatal, but we don't want to
2419 			 * repeatedly post a message about it.
2420 			 */
2421 			ice_print_rollback_msg(&sc->hw);
2422 		}
2423 	}
2424 
2425 	/* Handle global reset events */
2426 	ice_handle_reset_event(sc);
2427 
2428 	/* Handle PF reset requests */
2429 	ice_handle_pf_reset_request(sc);
2430 
2431 	/* Handle MDD events */
2432 	ice_handle_mdd_event(sc);
2433 
2434 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED) ||
2435 	    ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET) ||
2436 	    ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
2437 		/*
2438 		 * If we know the control queues are disabled, skip processing
2439 		 * the control queues entirely.
2440 		 */
2441 		;
2442 	} else if (ice_testandclear_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING)) {
2443 		ice_process_ctrlq(sc, ICE_CTL_Q_ADMIN, &pending);
2444 		if (pending > 0)
2445 			reschedule = true;
2446 
2447 		if (ice_is_generic_mac(&sc->hw)) {
2448 			ice_process_ctrlq(sc, ICE_CTL_Q_SB, &pending);
2449 			if (pending > 0)
2450 				reschedule = true;
2451 		}
2452 
2453 		ice_process_ctrlq(sc, ICE_CTL_Q_MAILBOX, &pending);
2454 		if (pending > 0)
2455 			reschedule = true;
2456 	}
2457 
2458 	/* Poll for link up */
2459 	ice_poll_for_media_avail(sc);
2460 
2461 	/* Check and update link status */
2462 	ice_update_link_status(sc, false);
2463 
2464 #ifdef PCI_IOV
2465 	/*
2466 	 * Schedule VFs' reset handler after global resets
2467 	 * and other events were processed.
2468 	 */
2469 	if (ice_testandclear_state(&sc->state, ICE_STATE_VFLR_PENDING))
2470 		iflib_iov_intr_deferred(ctx);
2471 #endif
2472 
2473 	/*
2474 	 * If there are still messages to process, we need to reschedule
2475 	 * ourselves. Otherwise, we can just re-enable the interrupt. We'll be
2476 	 * woken up at the next interrupt or timer event.
2477 	 */
2478 	if (reschedule) {
2479 		ice_set_state(&sc->state, ICE_STATE_CONTROLQ_EVENT_PENDING);
2480 		iflib_admin_intr_deferred(ctx);
2481 	} else {
2482 		ice_enable_intr(&sc->hw, sc->irqvs[0].me);
2483 	}
2484 }
2485 
2486 /**
2487  * ice_prepare_for_reset - Prepare device for an impending reset
2488  * @sc: The device private softc
2489  *
2490  * Prepare the driver for an impending reset, shutting down VSIs, clearing the
2491  * scheduler setup, and shutting down controlqs. Uses the
2492  * ICE_STATE_PREPARED_FOR_RESET to indicate whether we've already prepared the
2493  * driver for reset or not.
2494  */
2495 static void
2496 ice_prepare_for_reset(struct ice_softc *sc)
2497 {
2498 	struct ice_hw *hw = &sc->hw;
2499 
2500 	/* If we're already prepared, there's nothing to do */
2501 	if (ice_testandset_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET))
2502 		return;
2503 
2504 	log(LOG_INFO, "%s: preparing to reset device logic\n", if_name(sc->ifp));
2505 
2506 	/* In recovery mode, hardware is not initialized */
2507 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
2508 		return;
2509 
2510 	/* inform the RDMA client */
2511 	ice_rdma_notify_reset(sc);
2512 	/* stop the RDMA client */
2513 	ice_rdma_pf_stop(sc);
2514 
2515 	/* Release the main PF VSI queue mappings */
2516 	ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
2517 				    sc->pf_vsi.num_tx_queues);
2518 	ice_resmgr_release_map(&sc->rx_qmgr, sc->pf_vsi.rx_qmap,
2519 				    sc->pf_vsi.num_rx_queues);
2520 	if (sc->mirr_if) {
2521 		ice_resmgr_release_map(&sc->tx_qmgr, sc->mirr_if->vsi->tx_qmap,
2522 		    sc->mirr_if->num_irq_vectors);
2523 		ice_resmgr_release_map(&sc->rx_qmgr, sc->mirr_if->vsi->rx_qmap,
2524 		    sc->mirr_if->num_irq_vectors);
2525 	}
2526 
2527 	ice_clear_hw_tbls(hw);
2528 
2529 	if (hw->port_info)
2530 		ice_sched_cleanup_all(hw);
2531 
2532 	ice_shutdown_all_ctrlq(hw, false);
2533 }
2534 
2535 /**
2536  * ice_rebuild_pf_vsi_qmap - Rebuild the main PF VSI queue mapping
2537  * @sc: the device softc pointer
2538  *
2539  * Loops over the Tx and Rx queues for the main PF VSI and reassigns the queue
2540  * mapping after a reset occurred.
2541  */
2542 static int
2543 ice_rebuild_pf_vsi_qmap(struct ice_softc *sc)
2544 {
2545 	struct ice_vsi *vsi = &sc->pf_vsi;
2546 	struct ice_tx_queue *txq;
2547 	struct ice_rx_queue *rxq;
2548 	int err, i;
2549 
2550 	/* Re-assign Tx queues from PF space to the main VSI */
2551 	err = ice_resmgr_assign_contiguous(&sc->tx_qmgr, vsi->tx_qmap,
2552 					    vsi->num_tx_queues);
2553 	if (err) {
2554 		device_printf(sc->dev, "Unable to re-assign PF Tx queues: %s\n",
2555 			      ice_err_str(err));
2556 		return (err);
2557 	}
2558 
2559 	/* Re-assign Rx queues from PF space to this VSI */
2560 	err = ice_resmgr_assign_contiguous(&sc->rx_qmgr, vsi->rx_qmap,
2561 					    vsi->num_rx_queues);
2562 	if (err) {
2563 		device_printf(sc->dev, "Unable to re-assign PF Rx queues: %s\n",
2564 			      ice_err_str(err));
2565 		goto err_release_tx_queues;
2566 	}
2567 
2568 	vsi->qmap_type = ICE_RESMGR_ALLOC_CONTIGUOUS;
2569 
2570 	/* Re-assign Tx queue tail pointers */
2571 	for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++)
2572 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
2573 
2574 	/* Re-assign Rx queue tail pointers */
2575 	for (i = 0, rxq = vsi->rx_queues; i < vsi->num_rx_queues; i++, rxq++)
2576 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
2577 
2578 	return (0);
2579 
2580 err_release_tx_queues:
2581 	ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
2582 				   sc->pf_vsi.num_tx_queues);
2583 
2584 	return (err);
2585 }
2586 
2587 /* determine if the iflib context is active */
2588 #define CTX_ACTIVE(ctx) ((if_getdrvflags(iflib_get_ifp(ctx)) & IFF_DRV_RUNNING))
2589 
2590 /**
2591  * ice_rebuild_recovery_mode - Rebuild driver state while in recovery mode
2592  * @sc: The device private softc
2593  *
2594  * Handle a driver rebuild while in recovery mode. This will only rebuild the
2595  * limited functionality supported while in recovery mode.
2596  */
2597 static void
2598 ice_rebuild_recovery_mode(struct ice_softc *sc)
2599 {
2600 	device_t dev = sc->dev;
2601 
2602 	/* enable PCIe bus master */
2603 	pci_enable_busmaster(dev);
2604 
2605 	/* Configure interrupt causes for the administrative interrupt */
2606 	ice_configure_misc_interrupts(sc);
2607 
2608 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
2609 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
2610 
2611 	/* Now that the rebuild is finished, we're no longer prepared to reset */
2612 	ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
2613 
2614 	log(LOG_INFO, "%s: device rebuild successful\n", if_name(sc->ifp));
2615 
2616 	/* In order to completely restore device functionality, the iflib core
2617 	 * needs to be reset. We need to request an iflib reset. Additionally,
2618 	 * because the state of IFC_DO_RESET is cached within task_fn_admin in
2619 	 * the iflib core, we also want re-run the admin task so that iflib
2620 	 * resets immediately instead of waiting for the next interrupt.
2621 	 */
2622 	ice_request_stack_reinit(sc);
2623 
2624 	return;
2625 }
2626 
2627 /**
2628  * ice_rebuild - Rebuild driver state post reset
2629  * @sc: The device private softc
2630  *
2631  * Restore driver state after a reset occurred. Restart the controlqs, setup
2632  * the hardware port, and re-enable the VSIs.
2633  */
2634 static void
2635 ice_rebuild(struct ice_softc *sc)
2636 {
2637 	struct ice_hw *hw = &sc->hw;
2638 	device_t dev = sc->dev;
2639 	enum ice_ddp_state pkg_state;
2640 	int status;
2641 	int err;
2642 
2643 	sc->rebuild_ticks = ticks;
2644 
2645 	/* If we're rebuilding, then a reset has succeeded. */
2646 	ice_clear_state(&sc->state, ICE_STATE_RESET_FAILED);
2647 
2648 	/*
2649 	 * If the firmware is in recovery mode, only restore the limited
2650 	 * functionality supported by recovery mode.
2651 	 */
2652 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE)) {
2653 		ice_rebuild_recovery_mode(sc);
2654 		return;
2655 	}
2656 
2657 	/* enable PCIe bus master */
2658 	pci_enable_busmaster(dev);
2659 
2660 	status = ice_init_all_ctrlq(hw);
2661 	if (status) {
2662 		device_printf(dev, "failed to re-init controlqs, err %s\n",
2663 			      ice_status_str(status));
2664 		goto err_shutdown_ctrlq;
2665 	}
2666 
2667 	/* Query the allocated resources for Tx scheduler */
2668 	status = ice_sched_query_res_alloc(hw);
2669 	if (status) {
2670 		device_printf(dev,
2671 			      "Failed to query scheduler resources, err %s aq_err %s\n",
2672 			      ice_status_str(status),
2673 			      ice_aq_str(hw->adminq.sq_last_status));
2674 		goto err_shutdown_ctrlq;
2675 	}
2676 
2677 	/* Re-enable FW logging. Keep going even if this fails */
2678 	status = ICE_SUCCESS;
2679 	if (hw->pf_id == 0)
2680 		status = ice_fwlog_set(hw, &hw->fwlog_cfg);
2681 	if (!status) {
2682 		/*
2683 		 * We should have the most updated cached copy of the
2684 		 * configuration, regardless of whether we're rebuilding
2685 		 * or not.  So we'll simply check to see if logging was
2686 		 * enabled pre-rebuild.
2687 		 */
2688 		if (hw->fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED) {
2689 			status = ice_fwlog_register(hw);
2690 			if (status)
2691 				device_printf(dev, "failed to re-register fw logging, err %s aq_err %s\n",
2692 				   ice_status_str(status),
2693 				   ice_aq_str(hw->adminq.sq_last_status));
2694 		}
2695 	} else
2696 		device_printf(dev, "failed to rebuild fw logging configuration, err %s aq_err %s\n",
2697 		   ice_status_str(status),
2698 		   ice_aq_str(hw->adminq.sq_last_status));
2699 
2700 	err = ice_send_version(sc);
2701 	if (err)
2702 		goto err_shutdown_ctrlq;
2703 
2704 	err = ice_init_link_events(sc);
2705 	if (err) {
2706 		device_printf(dev, "ice_init_link_events failed: %s\n",
2707 			      ice_err_str(err));
2708 		goto err_shutdown_ctrlq;
2709 	}
2710 
2711 	status = ice_clear_pf_cfg(hw);
2712 	if (status) {
2713 		device_printf(dev, "failed to clear PF configuration, err %s\n",
2714 			      ice_status_str(status));
2715 		goto err_shutdown_ctrlq;
2716 	}
2717 
2718 	ice_clean_all_vsi_rss_cfg(sc);
2719 
2720 	ice_clear_pxe_mode(hw);
2721 
2722 	status = ice_get_caps(hw);
2723 	if (status) {
2724 		device_printf(dev, "failed to get capabilities, err %s\n",
2725 			      ice_status_str(status));
2726 		goto err_shutdown_ctrlq;
2727 	}
2728 
2729 	status = ice_sched_init_port(hw->port_info);
2730 	if (status) {
2731 		device_printf(dev, "failed to initialize port, err %s\n",
2732 			      ice_status_str(status));
2733 		goto err_sched_cleanup;
2734 	}
2735 
2736 	/* If we previously loaded the package, it needs to be reloaded now */
2737 	if (!ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE)) {
2738 		pkg_state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
2739 		if (!ice_is_init_pkg_successful(pkg_state)) {
2740 			ice_log_pkg_init(sc, pkg_state);
2741 			ice_transition_safe_mode(sc);
2742 		}
2743 	}
2744 
2745 	ice_reset_pf_stats(sc);
2746 
2747 	err = ice_rebuild_pf_vsi_qmap(sc);
2748 	if (err) {
2749 		device_printf(sc->dev, "Unable to re-assign main VSI queues, err %s\n",
2750 			      ice_err_str(err));
2751 		goto err_sched_cleanup;
2752 	}
2753 	err = ice_initialize_vsi(&sc->pf_vsi);
2754 	if (err) {
2755 		device_printf(sc->dev, "Unable to re-initialize Main VSI, err %s\n",
2756 			      ice_err_str(err));
2757 		goto err_release_queue_allocations;
2758 	}
2759 
2760 	/* Replay all VSI configuration */
2761 	err = ice_replay_all_vsi_cfg(sc);
2762 	if (err)
2763 		goto err_deinit_pf_vsi;
2764 
2765 	/* Re-enable FW health event reporting */
2766 	ice_init_health_events(sc);
2767 
2768 	/* Reconfigure the main PF VSI for RSS */
2769 	err = ice_config_rss(&sc->pf_vsi);
2770 	if (err) {
2771 		device_printf(sc->dev,
2772 			      "Unable to reconfigure RSS for the main VSI, err %s\n",
2773 			      ice_err_str(err));
2774 		goto err_deinit_pf_vsi;
2775 	}
2776 
2777 	if (hw->port_info->qos_cfg.is_sw_lldp)
2778 		ice_add_rx_lldp_filter(sc);
2779 
2780 	/* Apply previous link settings and refresh link status, if PHY
2781 	 * FW is ready.
2782 	 */
2783 	ice_clear_state(&sc->state, ICE_STATE_LINK_STATUS_REPORTED);
2784 	ice_init_link(sc);
2785 
2786 	/* RDMA interface will be restarted by the stack re-init */
2787 
2788 	/* Configure interrupt causes for the administrative interrupt */
2789 	ice_configure_misc_interrupts(sc);
2790 
2791 	/* Enable ITR 0 right away, so that we can handle admin interrupts */
2792 	ice_enable_intr(&sc->hw, sc->irqvs[0].me);
2793 
2794 	/* Now that the rebuild is finished, we're no longer prepared to reset */
2795 	ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
2796 
2797 	/* Reconfigure the subinterface */
2798 	if (sc->mirr_if) {
2799 		err = ice_subif_rebuild(sc);
2800 		if (err)
2801 			goto err_deinit_pf_vsi;
2802 	}
2803 
2804 	log(LOG_INFO, "%s: device rebuild successful\n", if_name(sc->ifp));
2805 
2806 	/* In order to completely restore device functionality, the iflib core
2807 	 * needs to be reset. We need to request an iflib reset. Additionally,
2808 	 * because the state of IFC_DO_RESET is cached within task_fn_admin in
2809 	 * the iflib core, we also want re-run the admin task so that iflib
2810 	 * resets immediately instead of waiting for the next interrupt.
2811 	 * If LLDP is enabled we need to reconfig DCB to properly reinit all TC
2812 	 * queues, not only 0. It contains ice_request_stack_reinit as well.
2813 	 */
2814 	if (hw->port_info->qos_cfg.is_sw_lldp)
2815 		ice_request_stack_reinit(sc);
2816 	else
2817 		ice_do_dcb_reconfig(sc, false);
2818 
2819 	return;
2820 
2821 err_deinit_pf_vsi:
2822 	ice_deinit_vsi(&sc->pf_vsi);
2823 err_release_queue_allocations:
2824 	ice_resmgr_release_map(&sc->tx_qmgr, sc->pf_vsi.tx_qmap,
2825 				    sc->pf_vsi.num_tx_queues);
2826 	ice_resmgr_release_map(&sc->rx_qmgr, sc->pf_vsi.rx_qmap,
2827 				    sc->pf_vsi.num_rx_queues);
2828 err_sched_cleanup:
2829 	ice_sched_cleanup_all(hw);
2830 err_shutdown_ctrlq:
2831 	ice_shutdown_all_ctrlq(hw, false);
2832 	ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
2833 	ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
2834 	device_printf(dev, "Driver rebuild failed, please reload the device driver\n");
2835 }
2836 
2837 /**
2838  * ice_handle_reset_event - Handle reset events triggered by OICR
2839  * @sc: The device private softc
2840  *
2841  * Handle reset events triggered by an OICR notification. This includes CORER,
2842  * GLOBR, and EMPR resets triggered by software on this or any other PF or by
2843  * firmware.
2844  *
2845  * @pre assumes the iflib context lock is held, and will unlock it while
2846  * waiting for the hardware to finish reset.
2847  */
2848 static void
2849 ice_handle_reset_event(struct ice_softc *sc)
2850 {
2851 	struct ice_hw *hw = &sc->hw;
2852 	int status;
2853 	device_t dev = sc->dev;
2854 
2855 	/* When a CORER, GLOBR, or EMPR is about to happen, the hardware will
2856 	 * trigger an OICR interrupt. Our OICR handler will determine when
2857 	 * this occurs and set the ICE_STATE_RESET_OICR_RECV bit as
2858 	 * appropriate.
2859 	 */
2860 	if (!ice_testandclear_state(&sc->state, ICE_STATE_RESET_OICR_RECV))
2861 		return;
2862 
2863 	ice_prepare_for_reset(sc);
2864 
2865 	/*
2866 	 * Release the iflib context lock and wait for the device to finish
2867 	 * resetting.
2868 	 */
2869 	IFLIB_CTX_UNLOCK(sc);
2870 
2871 #define ICE_EMPR_ADDL_WAIT_MSEC_SLOW		20000
2872 	if ((ice_is_e830(hw) || ice_is_e825c(hw)) &&
2873 	    (((rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2874 	         GLGEN_RSTAT_RESET_TYPE_S) == ICE_RESET_EMPR))
2875 			ice_msec_pause(ICE_EMPR_ADDL_WAIT_MSEC_SLOW);
2876 
2877 	status = ice_check_reset(hw);
2878 	IFLIB_CTX_LOCK(sc);
2879 	if (status) {
2880 		device_printf(dev, "Device never came out of reset, err %s\n",
2881 			      ice_status_str(status));
2882 
2883 		ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
2884 		ice_clear_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
2885 		ice_clear_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET);
2886 		device_printf(dev, "Reset failed; please reload the device driver\n");
2887 		return;
2888 	}
2889 
2890 	/* We're done with the reset, so we can rebuild driver state */
2891 	sc->hw.reset_ongoing = false;
2892 	ice_rebuild(sc);
2893 
2894 	/* In the unlikely event that a PF reset request occurs at the same
2895 	 * time as a global reset, clear the request now. This avoids
2896 	 * resetting a second time right after we reset due to a global event.
2897 	 */
2898 	if (ice_testandclear_state(&sc->state, ICE_STATE_RESET_PFR_REQ))
2899 		device_printf(dev, "Ignoring PFR request that occurred while a reset was ongoing\n");
2900 }
2901 
2902 /**
2903  * ice_handle_pf_reset_request - Initiate PF reset requested by software
2904  * @sc: The device private softc
2905  *
2906  * Initiate a PF reset requested by software. We handle this in the admin task
2907  * so that only one thread actually handles driver preparation and cleanup,
2908  * rather than having multiple threads possibly attempt to run this code
2909  * simultaneously.
2910  *
2911  * @pre assumes the iflib context lock is held and will unlock it while
2912  * waiting for the PF reset to complete.
2913  */
2914 static void
2915 ice_handle_pf_reset_request(struct ice_softc *sc)
2916 {
2917 	struct ice_hw *hw = &sc->hw;
2918 	int status;
2919 
2920 	/* Check for PF reset requests */
2921 	if (!ice_testandclear_state(&sc->state, ICE_STATE_RESET_PFR_REQ))
2922 		return;
2923 
2924 	/* Make sure we're prepared for reset */
2925 	ice_prepare_for_reset(sc);
2926 
2927 	/*
2928 	 * Release the iflib context lock and wait for the device to finish
2929 	 * resetting.
2930 	 */
2931 	IFLIB_CTX_UNLOCK(sc);
2932 	status = ice_reset(hw, ICE_RESET_PFR);
2933 	IFLIB_CTX_LOCK(sc);
2934 	if (status) {
2935 		device_printf(sc->dev, "device PF reset failed, err %s\n",
2936 			      ice_status_str(status));
2937 		ice_set_state(&sc->state, ICE_STATE_RESET_FAILED);
2938 		return;
2939 	}
2940 
2941 	sc->soft_stats.pfr_count++;
2942 	ice_rebuild(sc);
2943 }
2944 
2945 /**
2946  * ice_init_device_features - Init device driver features
2947  * @sc: driver softc structure
2948  *
2949  * @pre assumes that the function capabilities bits have been set up by
2950  * ice_init_hw().
2951  */
2952 static void
2953 ice_init_device_features(struct ice_softc *sc)
2954 {
2955 	struct ice_hw *hw = &sc->hw;
2956 
2957 	/* Set capabilities that all devices support */
2958 	ice_set_bit(ICE_FEATURE_SRIOV, sc->feat_cap);
2959 	ice_set_bit(ICE_FEATURE_RSS, sc->feat_cap);
2960 	ice_set_bit(ICE_FEATURE_RDMA, sc->feat_cap);
2961 	ice_set_bit(ICE_FEATURE_LENIENT_LINK_MODE, sc->feat_cap);
2962 	ice_set_bit(ICE_FEATURE_LINK_MGMT_VER_1, sc->feat_cap);
2963 	ice_set_bit(ICE_FEATURE_LINK_MGMT_VER_2, sc->feat_cap);
2964 	ice_set_bit(ICE_FEATURE_HEALTH_STATUS, sc->feat_cap);
2965 	ice_set_bit(ICE_FEATURE_FW_LOGGING, sc->feat_cap);
2966 	ice_set_bit(ICE_FEATURE_HAS_PBA, sc->feat_cap);
2967 	ice_set_bit(ICE_FEATURE_DCB, sc->feat_cap);
2968 	ice_set_bit(ICE_FEATURE_TX_BALANCE, sc->feat_cap);
2969 	ice_set_bit(ICE_FEATURE_PHY_STATISTICS, sc->feat_cap);
2970 
2971 	if (ice_is_e810(hw))
2972 		ice_set_bit(ICE_FEATURE_PHY_STATISTICS, sc->feat_en);
2973 
2974 	if (ice_is_e825c(hw))
2975 		ice_set_bit(ICE_FEATURE_DUAL_NAC, sc->feat_cap);
2976 	/* Disable features due to hardware limitations... */
2977 	if (!hw->func_caps.common_cap.rss_table_size)
2978 		ice_clear_bit(ICE_FEATURE_RSS, sc->feat_cap);
2979 	if (!hw->func_caps.common_cap.iwarp || !ice_enable_irdma)
2980 		ice_clear_bit(ICE_FEATURE_RDMA, sc->feat_cap);
2981 	if (!hw->func_caps.common_cap.dcb)
2982 		ice_clear_bit(ICE_FEATURE_DCB, sc->feat_cap);
2983 	/* Disable features due to firmware limitations... */
2984 	if (!ice_is_fw_health_report_supported(hw))
2985 		ice_clear_bit(ICE_FEATURE_HEALTH_STATUS, sc->feat_cap);
2986 	if (!ice_fwlog_supported(hw))
2987 		ice_clear_bit(ICE_FEATURE_FW_LOGGING, sc->feat_cap);
2988 	if (hw->fwlog_cfg.options & ICE_FWLOG_OPTION_IS_REGISTERED) {
2989 		if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_FW_LOGGING))
2990 			ice_set_bit(ICE_FEATURE_FW_LOGGING, sc->feat_en);
2991 		else
2992 			ice_fwlog_unregister(hw);
2993 	}
2994 
2995 	/* Disable capabilities not supported by the OS */
2996 	ice_disable_unsupported_features(sc->feat_cap);
2997 
2998 	/* RSS is always enabled for iflib */
2999 	if (ice_is_bit_set(sc->feat_cap, ICE_FEATURE_RSS))
3000 		ice_set_bit(ICE_FEATURE_RSS, sc->feat_en);
3001 
3002 	/* Disable features based on sysctl settings */
3003 	if (!ice_tx_balance_en)
3004 		ice_clear_bit(ICE_FEATURE_TX_BALANCE, sc->feat_cap);
3005 
3006 	if (hw->dev_caps.supported_sensors & ICE_SENSOR_SUPPORT_E810_INT_TEMP) {
3007 		ice_set_bit(ICE_FEATURE_TEMP_SENSOR, sc->feat_cap);
3008 		ice_set_bit(ICE_FEATURE_TEMP_SENSOR, sc->feat_en);
3009 	}
3010 
3011 	if (hw->func_caps.common_cap.next_cluster_id_support ||
3012 	    hw->dev_caps.common_cap.next_cluster_id_support) {
3013 		ice_set_bit(ICE_FEATURE_NEXT_CLUSTER_ID, sc->feat_cap);
3014 		ice_set_bit(ICE_FEATURE_NEXT_CLUSTER_ID, sc->feat_en);
3015 	}
3016 }
3017 
3018 /**
3019  * ice_if_multi_set - Callback to update Multicast filters in HW
3020  * @ctx: iflib ctx structure
3021  *
3022  * Called by iflib in response to SIOCDELMULTI and SIOCADDMULTI. Must search
3023  * the if_multiaddrs list and determine which filters have been added or
3024  * removed from the list, and update HW programming to reflect the new list.
3025  *
3026  * @pre assumes the caller holds the iflib CTX lock
3027  */
3028 static void
3029 ice_if_multi_set(if_ctx_t ctx)
3030 {
3031 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3032 	int err;
3033 
3034 	ASSERT_CTX_LOCKED(sc);
3035 
3036 	/* Do not handle multicast configuration in recovery mode */
3037 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
3038 		return;
3039 
3040 	err = ice_sync_multicast_filters(sc);
3041 	if (err) {
3042 		device_printf(sc->dev,
3043 			      "Failed to synchronize multicast filter list: %s\n",
3044 			      ice_err_str(err));
3045 		return;
3046 	}
3047 }
3048 
3049 /**
3050  * ice_if_vlan_register - Register a VLAN with the hardware
3051  * @ctx: iflib ctx pointer
3052  * @vtag: VLAN to add
3053  *
3054  * Programs the main PF VSI with a hardware filter for the given VLAN.
3055  *
3056  * @pre assumes the caller holds the iflib CTX lock
3057  */
3058 static void
3059 ice_if_vlan_register(if_ctx_t ctx, u16 vtag)
3060 {
3061 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3062 	int status;
3063 
3064 	ASSERT_CTX_LOCKED(sc);
3065 
3066 	/* Do not handle VLAN configuration in recovery mode */
3067 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
3068 		return;
3069 
3070 	status = ice_add_vlan_hw_filter(&sc->pf_vsi, vtag);
3071 	if (status) {
3072 		device_printf(sc->dev,
3073 			      "Failure adding VLAN %d to main VSI, err %s aq_err %s\n",
3074 			      vtag, ice_status_str(status),
3075 			      ice_aq_str(sc->hw.adminq.sq_last_status));
3076 	}
3077 }
3078 
3079 /**
3080  * ice_if_vlan_unregister - Remove a VLAN filter from the hardware
3081  * @ctx: iflib ctx pointer
3082  * @vtag: VLAN to add
3083  *
3084  * Removes the previously programmed VLAN filter from the main PF VSI.
3085  *
3086  * @pre assumes the caller holds the iflib CTX lock
3087  */
3088 static void
3089 ice_if_vlan_unregister(if_ctx_t ctx, u16 vtag)
3090 {
3091 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3092 	int status;
3093 
3094 	ASSERT_CTX_LOCKED(sc);
3095 
3096 	/* Do not handle VLAN configuration in recovery mode */
3097 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
3098 		return;
3099 
3100 	status = ice_remove_vlan_hw_filter(&sc->pf_vsi, vtag);
3101 	if (status) {
3102 		device_printf(sc->dev,
3103 			      "Failure removing VLAN %d from main VSI, err %s aq_err %s\n",
3104 			      vtag, ice_status_str(status),
3105 			      ice_aq_str(sc->hw.adminq.sq_last_status));
3106 	}
3107 }
3108 
3109 /**
3110  * ice_if_stop - Stop the device
3111  * @ctx: iflib context structure
3112  *
3113  * Called by iflib to stop the device and bring it down. (i.e. ifconfig ice0
3114  * down)
3115  *
3116  * @pre assumes the caller holds the iflib CTX lock
3117  */
3118 static void
3119 ice_if_stop(if_ctx_t ctx)
3120 {
3121 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
3122 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3123 
3124 	ASSERT_CTX_LOCKED(sc);
3125 
3126 	/*
3127 	 * The iflib core may call IFDI_STOP prior to the first call to
3128 	 * IFDI_INIT. This will cause us to attempt to remove MAC filters we
3129 	 * don't have, and disable Tx queues which aren't yet configured.
3130 	 * Although it is likely these extra operations are harmless, they do
3131 	 * cause spurious warning messages to be displayed, which may confuse
3132 	 * users.
3133 	 *
3134 	 * To avoid these messages, we use a state bit indicating if we've
3135 	 * been initialized. It will be set when ice_if_init is called, and
3136 	 * cleared here in ice_if_stop.
3137 	 */
3138 	if (!ice_testandclear_state(&sc->state, ICE_STATE_DRIVER_INITIALIZED))
3139 		return;
3140 
3141 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
3142 		device_printf(sc->dev, "request to stop interface cannot be completed as the device failed to reset\n");
3143 		return;
3144 	}
3145 
3146 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
3147 		device_printf(sc->dev, "request to stop interface while device is prepared for impending reset\n");
3148 		return;
3149 	}
3150 
3151 	ice_rdma_pf_stop(sc);
3152 
3153 	/* Remove the MAC filters, stop Tx, and stop Rx. We don't check the
3154 	 * return of these functions because there's nothing we can really do
3155 	 * if they fail, and the functions already print error messages.
3156 	 * Just try to shut down as much as we can.
3157 	 */
3158 	ice_rm_pf_default_mac_filters(sc);
3159 
3160 	/* Dissociate the Tx and Rx queues from the interrupts */
3161 	ice_flush_txq_interrupts(&sc->pf_vsi);
3162 	ice_flush_rxq_interrupts(&sc->pf_vsi);
3163 
3164 	/* Disable the Tx and Rx queues */
3165 	ice_vsi_disable_tx(&sc->pf_vsi);
3166 	ice_control_all_rx_queues(&sc->pf_vsi, false);
3167 
3168 	if (!ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) &&
3169 		 !(if_getflags(sc->ifp) & IFF_UP) && sc->link_up)
3170 		ice_set_link(sc, false);
3171 
3172 	if (sc->mirr_if && ice_test_state(&mif->state, ICE_STATE_SUBIF_NEEDS_REINIT)) {
3173 		ice_subif_if_stop(sc->mirr_if->subctx);
3174 		device_printf(sc->dev, "The subinterface also comes down and up after reset\n");
3175 	}
3176 }
3177 
3178 /**
3179  * ice_if_get_counter - Get current value of an ifnet statistic
3180  * @ctx: iflib context pointer
3181  * @counter: ifnet counter to read
3182  *
3183  * Reads the current value of an ifnet counter for the device.
3184  *
3185  * This function is not protected by the iflib CTX lock.
3186  */
3187 static uint64_t
3188 ice_if_get_counter(if_ctx_t ctx, ift_counter counter)
3189 {
3190 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3191 
3192 	/* Return the counter for the main PF VSI */
3193 	return ice_get_ifnet_counter(&sc->pf_vsi, counter);
3194 }
3195 
3196 /**
3197  * ice_request_stack_reinit - Request that iflib re-initialize
3198  * @sc: the device private softc
3199  *
3200  * Request that the device be brought down and up, to re-initialize. For
3201  * example, this may be called when a device reset occurs, or when Tx and Rx
3202  * queues need to be re-initialized.
3203  *
3204  * This is required because the iflib state is outside the driver, and must be
3205  * re-initialized if we need to resart Tx and Rx queues.
3206  */
3207 void
3208 ice_request_stack_reinit(struct ice_softc *sc)
3209 {
3210 	if (CTX_ACTIVE(sc->ctx)) {
3211 		iflib_request_reset(sc->ctx);
3212 		iflib_admin_intr_deferred(sc->ctx);
3213 	}
3214 }
3215 
3216 /**
3217  * ice_driver_is_detaching - Check if the driver is detaching/unloading
3218  * @sc: device private softc
3219  *
3220  * Returns true if the driver is detaching, false otherwise.
3221  *
3222  * @remark on newer kernels, take advantage of iflib_in_detach in order to
3223  * report detachment correctly as early as possible.
3224  *
3225  * @remark this function is used by various code paths that want to avoid
3226  * running if the driver is about to be removed. This includes sysctls and
3227  * other driver access points. Note that it does not fully resolve
3228  * detach-based race conditions as it is possible for a thread to race with
3229  * iflib_in_detach.
3230  */
3231 bool
3232 ice_driver_is_detaching(struct ice_softc *sc)
3233 {
3234 	return (ice_test_state(&sc->state, ICE_STATE_DETACHING) ||
3235 		iflib_in_detach(sc->ctx));
3236 }
3237 
3238 /**
3239  * ice_if_priv_ioctl - Device private ioctl handler
3240  * @ctx: iflib context pointer
3241  * @command: The ioctl command issued
3242  * @data: ioctl specific data
3243  *
3244  * iflib callback for handling custom driver specific ioctls.
3245  *
3246  * @pre Assumes that the iflib context lock is held.
3247  */
3248 static int
3249 ice_if_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data)
3250 {
3251 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3252 	struct ifdrv *ifd;
3253 	device_t dev = sc->dev;
3254 
3255 	if (data == NULL)
3256 		return (EINVAL);
3257 
3258 	ASSERT_CTX_LOCKED(sc);
3259 
3260 	/* Make sure the command type is valid */
3261 	switch (command) {
3262 	case SIOCSDRVSPEC:
3263 	case SIOCGDRVSPEC:
3264 		/* Accepted commands */
3265 		break;
3266 	case SIOCGPRIVATE_0:
3267 		/*
3268 		 * Although we do not support this ioctl command, it's
3269 		 * expected that iflib will forward it to the IFDI_PRIV_IOCTL
3270 		 * handler. Do not print a message in this case
3271 		 */
3272 		return (ENOTSUP);
3273 	default:
3274 		/*
3275 		 * If we get a different command for this function, it's
3276 		 * definitely unexpected, so log a message indicating what
3277 		 * command we got for debugging purposes.
3278 		 */
3279 		device_printf(dev, "%s: unexpected ioctl command %08lx\n",
3280 			      __func__, command);
3281 		return (EINVAL);
3282 	}
3283 
3284 	ifd = (struct ifdrv *)data;
3285 
3286 	switch (ifd->ifd_cmd) {
3287 	case ICE_NVM_ACCESS:
3288 		return ice_handle_nvm_access_ioctl(sc, ifd);
3289 	case ICE_DEBUG_DUMP:
3290 		return ice_handle_debug_dump_ioctl(sc, ifd);
3291 	default:
3292 		return EINVAL;
3293 	}
3294 }
3295 
3296 /**
3297  * ice_if_i2c_req - I2C request handler for iflib
3298  * @ctx: iflib context pointer
3299  * @req: The I2C parameters to use
3300  *
3301  * Read from the port's I2C eeprom using the parameters from the ioctl.
3302  *
3303  * @remark The iflib-only part is pretty simple.
3304  */
3305 static int
3306 ice_if_i2c_req(if_ctx_t ctx, struct ifi2creq *req)
3307 {
3308 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3309 
3310 	return ice_handle_i2c_req(sc, req);
3311 }
3312 
3313 /**
3314  * ice_if_suspend - PCI device suspend handler for iflib
3315  * @ctx: iflib context pointer
3316  *
3317  * Deinitializes the driver and clears HW resources in preparation for
3318  * suspend or an FLR.
3319  *
3320  * @returns 0; this return value is ignored
3321  */
3322 static int
3323 ice_if_suspend(if_ctx_t ctx)
3324 {
3325 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3326 
3327 	/* At least a PFR is always going to happen after this;
3328 	 * either via FLR or during the D3->D0 transition.
3329 	 */
3330 	ice_clear_state(&sc->state, ICE_STATE_RESET_PFR_REQ);
3331 
3332 	ice_prepare_for_reset(sc);
3333 
3334 	return (0);
3335 }
3336 
3337 /**
3338  * ice_if_resume - PCI device resume handler for iflib
3339  * @ctx: iflib context pointer
3340  *
3341  * Reinitializes the driver and the HW after PCI resume or after
3342  * an FLR. An init is performed by iflib after this function is finished.
3343  *
3344  * @returns 0; this return value is ignored
3345  */
3346 static int
3347 ice_if_resume(if_ctx_t ctx)
3348 {
3349 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3350 
3351 	ice_rebuild(sc);
3352 
3353 	return (0);
3354 }
3355 
3356 /**
3357  * ice_if_needs_restart - Tell iflib when the driver needs to be reinitialized
3358  * @ctx: iflib context pointer
3359  * @event: event code to check
3360  *
3361  * Defaults to returning true for unknown events.
3362  *
3363  * @returns true if iflib needs to reinit the interface
3364  */
3365 static bool
3366 ice_if_needs_restart(if_ctx_t ctx, enum iflib_restart_event event)
3367 {
3368 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3369 
3370 	switch (event) {
3371 	case IFLIB_RESTART_VLAN_CONFIG:
3372 		if (!ice_test_state(&sc->state, ICE_STATE_LINK_ACTIVE_ON_DOWN) &&
3373 			 !(if_getflags(sc->ifp) & IFF_UP))
3374 			return false;
3375 	default:
3376 		return true;
3377 	}
3378 }
3379 
3380 /**
3381  * ice_init_link - Do link configuration and link status reporting
3382  * @sc: driver private structure
3383  *
3384  * Contains an extra check that skips link config when an E830 device
3385  * does not have the "FW_LOADING"/"PHYBUSY" bit set in GL_MNG_FWSM set.
3386  */
3387 static void
3388 ice_init_link(struct ice_softc *sc)
3389 {
3390 	struct ice_hw *hw = &sc->hw;
3391 	device_t dev = sc->dev;
3392 
3393 	/* Check if FW is ready before setting up link; defer setup to the
3394 	 * admin task if it isn't.
3395 	 */
3396 	if (ice_is_e830(hw) &&
3397 	    (rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M)) {
3398 		ice_set_state(&sc->state, ICE_STATE_PHY_FW_INIT_PENDING);
3399 		device_printf(dev,
3400 		    "Link initialization is blocked by PHY FW initialization.\n");
3401 		device_printf(dev,
3402 		    "Link initialization will continue after PHY FW initialization completes.\n");
3403 		/* Do not access PHY config while PHY FW is busy initializing */
3404 	} else {
3405 		ice_clear_state(&sc->state, ICE_STATE_PHY_FW_INIT_PENDING);
3406 		ice_init_link_configuration(sc);
3407 		ice_update_link_status(sc, true);
3408 	}
3409 
3410 }
3411 
3412 #ifdef PCI_IOV
3413 /**
3414  * ice_if_iov_init - iov init handler for iflib
3415  * @ctx: iflib context pointer
3416  * @num_vfs: number of VFs to create
3417  * @params: configuration parameters for the PF
3418  *
3419  * Configure the driver for SR-IOV mode. Used to setup things like memory
3420  * before any VFs are created.
3421  *
3422  * @remark This is a wrapper for ice_iov_init
3423  */
3424 static int
3425 ice_if_iov_init(if_ctx_t ctx, uint16_t num_vfs, const nvlist_t *params)
3426 {
3427 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3428 
3429 	return ice_iov_init(sc, num_vfs, params);
3430 }
3431 
3432 /**
3433  * ice_if_iov_uninit - iov uninit handler for iflib
3434  * @ctx: iflib context pointer
3435  *
3436  * Destroys VFs and frees their memory and resources.
3437  *
3438  * @remark This is a wrapper for ice_iov_uninit
3439  */
3440 static void
3441 ice_if_iov_uninit(if_ctx_t ctx)
3442 {
3443 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3444 
3445 	ice_iov_uninit(sc);
3446 }
3447 
3448 /**
3449  * ice_if_iov_vf_add - iov add vf handler for iflib
3450  * @ctx: iflib context pointer
3451  * @vfnum: index of VF to configure
3452  * @params: configuration parameters for the VF
3453  *
3454  * Sets up the VF given by the vfnum index. This is called by the OS
3455  * for each VF created by the PF driver after it is spawned.
3456  *
3457  * @remark This is a wrapper for ice_iov_vf_add
3458  */
3459 static int
3460 ice_if_iov_vf_add(if_ctx_t ctx, uint16_t vfnum, const nvlist_t *params)
3461 {
3462 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3463 
3464 	return ice_iov_add_vf(sc, vfnum, params);
3465 }
3466 
3467 /**
3468  * ice_if_vflr_handle - iov VFLR handler
3469  * @ctx: iflib context pointer
3470  *
3471  * Performs the necessar teardown or setup required for a VF after
3472  * a VFLR is initiated.
3473  *
3474  * @remark This is a wrapper for ice_iov_handle_vflr
3475  */
3476 static void
3477 ice_if_vflr_handle(if_ctx_t ctx)
3478 {
3479 	struct ice_softc *sc = (struct ice_softc *)iflib_get_softc(ctx);
3480 	ice_iov_handle_vflr(sc);
3481 }
3482 #endif /* PCI_IOV */
3483 
3484 extern struct if_txrx ice_subif_txrx;
3485 
3486 /**
3487  * @var ice_subif_methods
3488  * @brief ice driver method entry points
3489  */
3490 static device_method_t ice_subif_methods[] = {
3491 	/* Device interface */
3492 	DEVMETHOD(device_register, ice_subif_register),
3493 	DEVMETHOD_END
3494 };
3495 
3496 /**
3497  * @var ice_subif_driver
3498  * @brief driver structure for the device API
3499  */
3500 static driver_t ice_subif_driver = {
3501 	.name = "ice_subif",
3502 	.methods = ice_subif_methods,
3503 	.size = sizeof(struct ice_mirr_if),
3504 };
3505 
3506 static device_method_t ice_iflib_subif_methods[] = {
3507 	DEVMETHOD(ifdi_attach_pre, ice_subif_if_attach_pre),
3508 	DEVMETHOD(ifdi_attach_post, ice_subif_if_attach_post),
3509 	DEVMETHOD(ifdi_tx_queues_alloc, ice_subif_if_tx_queues_alloc),
3510 	DEVMETHOD(ifdi_rx_queues_alloc, ice_subif_if_rx_queues_alloc),
3511 	DEVMETHOD(ifdi_msix_intr_assign, ice_subif_if_msix_intr_assign),
3512 	DEVMETHOD(ifdi_intr_enable, ice_subif_if_intr_enable),
3513 	DEVMETHOD(ifdi_rx_queue_intr_enable, ice_subif_if_rx_queue_intr_enable),
3514 	DEVMETHOD(ifdi_tx_queue_intr_enable, ice_subif_if_tx_queue_intr_enable),
3515 	DEVMETHOD(ifdi_init, ice_subif_if_init),
3516 	DEVMETHOD(ifdi_stop, ice_subif_if_stop),
3517 	DEVMETHOD(ifdi_queues_free, ice_subif_if_queues_free),
3518 	DEVMETHOD(ifdi_media_status, ice_subif_if_media_status),
3519 	DEVMETHOD(ifdi_promisc_set, ice_subif_if_promisc_set),
3520 };
3521 
3522 /**
3523  * @var ice_iflib_subif_driver
3524  * @brief driver structure for the iflib stack
3525  *
3526  * driver_t definition used to setup the iflib device methods.
3527  */
3528 static driver_t ice_iflib_subif_driver = {
3529 	.name = "ice_subif",
3530 	.methods = ice_iflib_subif_methods,
3531 	.size = sizeof(struct ice_mirr_if),
3532 };
3533 
3534 /**
3535  * @var ice_subif_sctx
3536  * @brief ice driver shared context
3537  *
3538  * Similar to the existing ice_sctx, this structure has these differences:
3539  * - isc_admin_intrcnt is set to 0
3540  * - Uses subif iflib driver methods
3541  * - Flagged as a VF for iflib
3542  */
3543 static struct if_shared_ctx ice_subif_sctx = {
3544 	.isc_magic = IFLIB_MAGIC,
3545 	.isc_q_align = PAGE_SIZE,
3546 
3547 	.isc_tx_maxsize = ICE_MAX_FRAME_SIZE,
3548 	.isc_tx_maxsegsize = ICE_MAX_FRAME_SIZE,
3549 	.isc_tso_maxsize = ICE_TSO_SIZE + sizeof(struct ether_vlan_header),
3550 	.isc_tso_maxsegsize = ICE_MAX_DMA_SEG_SIZE,
3551 
3552 	.isc_rx_maxsize = ICE_MAX_FRAME_SIZE,
3553 	.isc_rx_nsegments = ICE_MAX_RX_SEGS,
3554 	.isc_rx_maxsegsize = ICE_MAX_FRAME_SIZE,
3555 
3556 	.isc_nfl = 1,
3557 	.isc_ntxqs = 1,
3558 	.isc_nrxqs = 1,
3559 
3560 	.isc_admin_intrcnt = 0,
3561 	.isc_vendor_info = ice_vendor_info_array,
3562 	.isc_driver_version = __DECONST(char *, ice_driver_version),
3563 	.isc_driver = &ice_iflib_subif_driver,
3564 
3565 	.isc_flags = IFLIB_NEED_SCRATCH | IFLIB_TSO_INIT_IP |
3566 		IFLIB_ADMIN_ALWAYS_RUN | IFLIB_SKIP_MSIX |
3567 		IFLIB_IS_VF,
3568 
3569 	.isc_nrxd_min = {ICE_MIN_DESC_COUNT},
3570 	.isc_ntxd_min = {ICE_MIN_DESC_COUNT},
3571 	.isc_nrxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
3572 	.isc_ntxd_max = {ICE_IFLIB_MAX_DESC_COUNT},
3573 	.isc_nrxd_default = {ICE_DEFAULT_DESC_COUNT},
3574 	.isc_ntxd_default = {ICE_DEFAULT_DESC_COUNT},
3575 };
3576 
3577 static void *
3578 ice_subif_register(device_t dev __unused)
3579 {
3580 	return (&ice_subif_sctx);
3581 }
3582 
3583 static void
3584 ice_subif_setup_scctx(struct ice_mirr_if *mif)
3585 {
3586 	if_softc_ctx_t scctx = mif->subscctx;
3587 
3588 	scctx->isc_txrx = &ice_subif_txrx;
3589 
3590 	scctx->isc_capenable = ICE_FULL_CAPS;
3591 	scctx->isc_tx_csum_flags = ICE_CSUM_OFFLOAD;
3592 
3593 	scctx->isc_ntxqsets = 4;
3594 	scctx->isc_nrxqsets = 4;
3595 	scctx->isc_vectors = scctx->isc_nrxqsets;
3596 
3597 	scctx->isc_ntxqsets_max = 256;
3598 	scctx->isc_nrxqsets_max = 256;
3599 
3600 	scctx->isc_txqsizes[0] = roundup2(scctx->isc_ntxd[0]
3601 	    * sizeof(struct ice_tx_desc), DBA_ALIGN);
3602 	scctx->isc_rxqsizes[0] = roundup2(scctx->isc_nrxd[0]
3603 	    * sizeof(union ice_32b_rx_flex_desc), DBA_ALIGN);
3604 
3605 	scctx->isc_tx_nsegments = ICE_MAX_TX_SEGS;
3606 	scctx->isc_tx_tso_segments_max = ICE_MAX_TSO_SEGS;
3607 	scctx->isc_tx_tso_size_max = ICE_TSO_SIZE;
3608 	scctx->isc_tx_tso_segsize_max = ICE_MAX_DMA_SEG_SIZE;
3609 }
3610 
3611 static int
3612 ice_subif_if_attach_pre(if_ctx_t ctx)
3613 {
3614 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
3615 	device_t dev = iflib_get_dev(ctx);
3616 
3617 	mif->subctx = ctx;
3618 	mif->subdev = dev;
3619 	mif->subscctx = iflib_get_softc_ctx(ctx);
3620 
3621 	/* Setup the iflib softc context structure */
3622 	ice_subif_setup_scctx(mif);
3623 
3624 	return (0);
3625 }
3626 
3627 static int
3628 ice_subif_if_attach_post(if_ctx_t ctx __unused)
3629 {
3630 	return (0);
3631 }
3632 
3633 /**
3634  * ice_destroy_mirror_interface - destroy mirror interface
3635  * @sc: driver private data
3636  *
3637  * Destroys all resources associated with the mirroring interface.
3638  * Will not exit early on failure.
3639  *
3640  * @pre: Mirror interface already exists and is initialized.
3641  */
3642 void
3643 ice_destroy_mirror_interface(struct ice_softc *sc)
3644 {
3645 	struct ice_mirr_if *mif = sc->mirr_if;
3646 	struct ice_vsi *vsi = mif->vsi;
3647 	bool is_locked = false;
3648 	int ret;
3649 
3650 	is_locked = sx_xlocked(sc->iflib_ctx_lock);
3651 	if (is_locked)
3652 		IFLIB_CTX_UNLOCK(sc);
3653 
3654 	if (mif->ifp) {
3655 		ret = iflib_device_deregister(mif->subctx);
3656 		if (ret) {
3657 			device_printf(sc->dev,
3658 			    "iflib_device_deregister for mirror interface failed: %d\n",
3659 			    ret);
3660 		}
3661 	}
3662 
3663 	bus_topo_lock();
3664 	ret = device_delete_child(sc->dev, mif->subdev);
3665 	bus_topo_unlock();
3666 	if (ret) {
3667 		device_printf(sc->dev,
3668 		    "device_delete_child for mirror interface failed: %d\n",
3669 		    ret);
3670 	}
3671 
3672 	if (is_locked)
3673 		IFLIB_CTX_LOCK(sc);
3674 
3675 	if (mif->if_imap) {
3676 		free(mif->if_imap, M_ICE);
3677 		mif->if_imap = NULL;
3678 	}
3679 	if (mif->os_imap) {
3680 		free(mif->os_imap, M_ICE);
3681 		mif->os_imap = NULL;
3682 	}
3683 
3684 	/* These are freed via ice_subif_queues_free_subif
3685 	 * vsi:
3686 	 * - rx_irqvs
3687 	 * - tx_queues
3688 	 * - rx_queues
3689 	 */
3690 	ice_release_vsi(vsi);
3691 
3692 	free(mif, M_ICE);
3693 	sc->mirr_if = NULL;
3694 
3695 }
3696 
3697 /**
3698  * ice_setup_mirror_vsi - Initialize mirror VSI
3699  * @mif: driver private data for mirror interface
3700  *
3701  * Allocates a VSI for a mirror interface, and sets that VSI up for use as a
3702  * mirror for the main PF VSI.
3703  *
3704  * Returns 0 on success, or a standard error code on failure.
3705  */
3706 static int
3707 ice_setup_mirror_vsi(struct ice_mirr_if *mif)
3708 {
3709 	struct ice_softc *sc = mif->back;
3710 	device_t dev = sc->dev;
3711 	struct ice_vsi *vsi;
3712 	int ret = 0;
3713 
3714 	/* vsi is for the new mirror vsi, not the PF's main VSI */
3715 	vsi = ice_alloc_vsi(sc, ICE_VSI_VMDQ2);
3716 	if (!vsi) {
3717 		/* Already prints an error message */
3718 		return (ENOMEM);
3719 	}
3720 	mif->vsi = vsi;
3721 
3722 	/* Reserve VSI queue allocation from PF queues */
3723 	ice_alloc_vsi_qmap(vsi, ICE_DEFAULT_VF_QUEUES, ICE_DEFAULT_VF_QUEUES);
3724 	vsi->num_tx_queues = vsi->num_rx_queues = ICE_DEFAULT_VF_QUEUES;
3725 
3726 	/* Assign Tx queues from PF space */
3727 	ret = ice_resmgr_assign_scattered(&sc->tx_qmgr, vsi->tx_qmap,
3728 	    vsi->num_tx_queues);
3729 	if (ret) {
3730 		device_printf(dev, "Unable to assign mirror VSI Tx queues: %s\n",
3731 		    ice_err_str(ret));
3732 		goto release_vsi;
3733 	}
3734 	/* Assign Rx queues from PF space */
3735 	ret = ice_resmgr_assign_scattered(&sc->rx_qmgr, vsi->rx_qmap,
3736 	    vsi->num_rx_queues);
3737 	if (ret) {
3738 		device_printf(dev, "Unable to assign mirror VSI Rx queues: %s\n",
3739 		    ice_err_str(ret));
3740 		goto release_vsi;
3741 	}
3742 	vsi->qmap_type = ICE_RESMGR_ALLOC_SCATTERED;
3743 	vsi->max_frame_size = ICE_MAX_FRAME_SIZE;
3744 
3745 	ret = ice_initialize_vsi(vsi);
3746 	if (ret) {
3747 		device_printf(dev, "%s: Error in ice_initialize_vsi for mirror VSI: %s\n",
3748 		    __func__, ice_err_str(ret));
3749 		goto release_vsi;
3750 	}
3751 
3752 	/* Setup this VSI for receiving traffic */
3753 	ret = ice_config_rss(vsi);
3754 	if (ret) {
3755 		device_printf(dev,
3756 		    "Unable to configure RSS for mirror VSI: %s\n",
3757 		    ice_err_str(ret));
3758 		goto release_vsi;
3759 	}
3760 
3761 	/* Set HW rules for mirroring traffic */
3762 	vsi->mirror_src_vsi = sc->pf_vsi.idx;
3763 
3764 	ice_debug(&sc->hw, ICE_DBG_INIT,
3765 	    "Configuring mirroring from VSI %d to %d\n",
3766 	    vsi->mirror_src_vsi, vsi->idx);
3767 	ice_debug(&sc->hw, ICE_DBG_INIT, "(HW num: VSI %d to %d)\n",
3768 	    ice_get_hw_vsi_num(&sc->hw, vsi->mirror_src_vsi),
3769 	    ice_get_hw_vsi_num(&sc->hw, vsi->idx));
3770 
3771 	ret = ice_setup_vsi_mirroring(vsi);
3772 	if (ret) {
3773 		device_printf(dev,
3774 		    "Unable to configure mirroring for VSI: %s\n",
3775 		    ice_err_str(ret));
3776 		goto release_vsi;
3777 	}
3778 
3779 	return (0);
3780 
3781 release_vsi:
3782 	ice_release_vsi(vsi);
3783 	mif->vsi = NULL;
3784 	return (ret);
3785 }
3786 
3787 /**
3788  * ice_create_mirror_interface - Initialize mirror interface
3789  * @sc: driver private data
3790  *
3791  * Creates and sets up a mirror interface that will mirror traffic from
3792  * the main PF interface. Includes a call to iflib_device_register() in order
3793  * to setup necessary iflib structures for this new interface as well.
3794  *
3795  * If it returns successfully, a new interface will be created and will show
3796  * up in the ifconfig interface list.
3797  *
3798  * Returns 0 on success, or a standard error code on failure.
3799  */
3800 int
3801 ice_create_mirror_interface(struct ice_softc *sc)
3802 {
3803 	device_t dev = sc->dev;
3804 	struct ice_mirr_if *mif;
3805 	struct ifmedia *media;
3806 	struct sbuf *sb;
3807 	int ret = 0;
3808 
3809 	mif = (struct ice_mirr_if *)malloc(sizeof(*mif), M_ICE, M_ZERO | M_NOWAIT);
3810 	if (!mif) {
3811 		device_printf(dev, "malloc() error allocating mirror interface\n");
3812 		return (ENOMEM);
3813 	}
3814 
3815 	/* Set pointers */
3816 	sc->mirr_if = mif;
3817 	mif->back = sc;
3818 
3819 	/* Do early setup because these will be called during iflib_device_register():
3820 	 * - ice_subif_if_tx_queues_alloc
3821 	 * - ice_subif_if_rx_queues_alloc
3822 	 */
3823 	ret = ice_setup_mirror_vsi(mif);
3824 	if (ret)
3825 		goto out;
3826 
3827 	/* Determine name for new interface:
3828 	 * (base interface name)(modifier name)(modifier unit number)
3829 	 * e.g. for ice0 with a new mirror interface (modifier m)
3830 	 * of index 0, this equals "ice0m0"
3831 	 */
3832 	sb = sbuf_new_auto();
3833 	MPASS(sb != NULL);
3834 	sbuf_printf(sb, "%sm", device_get_nameunit(dev));
3835 	sbuf_finish(sb);
3836 
3837 	bus_topo_lock();
3838 	mif->subdev = device_add_child(dev, sbuf_data(sb), 0);
3839 	bus_topo_unlock();
3840 
3841 	if (!mif->subdev) {
3842 		device_printf(dev, "device_add_child failed for %s0\n", sbuf_data(sb));
3843 		sbuf_delete(sb);
3844 		free(mif, M_ICE);
3845 		sc->mirr_if = NULL;
3846 		return (ENOMEM);
3847 	}
3848 	sbuf_delete(sb);
3849 
3850 	device_set_driver(mif->subdev, &ice_subif_driver);
3851 
3852 	/* Use iflib_device_register() directly because the driver already
3853 	 * has an initialized softc to pass to iflib
3854 	 */
3855 	ret = iflib_device_register(mif->subdev, mif, &ice_subif_sctx, &mif->subctx);
3856 	if (ret)
3857 		goto out;
3858 
3859 	/* Indicate that created interface will be just for monitoring */
3860 	mif->ifp = iflib_get_ifp(mif->subctx);
3861 	if_setflagbits(mif->ifp, IFF_MONITOR, 0);
3862 
3863 	/* Use autoselect media by default */
3864 	media = iflib_get_media(mif->subctx);
3865 	ifmedia_add(media, IFM_ETHER | IFM_AUTO, 0, NULL);
3866 	ifmedia_set(media, IFM_ETHER | IFM_AUTO);
3867 
3868 	device_printf(dev, "Created dev %s and ifnet %s for mirroring\n",
3869 	    device_get_nameunit(mif->subdev), if_name(mif->ifp));
3870 
3871 	ice_add_vsi_sysctls(mif->vsi);
3872 
3873 	ret = ice_wire_mirror_intrs(mif);
3874 	if (ret)
3875 		goto out;
3876 
3877 	mif->if_attached = true;
3878 	return (0);
3879 
3880 out:
3881 	ice_destroy_mirror_interface(sc);
3882 	return (ret);
3883 }
3884 
3885 /**
3886  * ice_wire_mirror_intrs
3887  * @mif: driver private subinterface structure
3888  *
3889  * Helper function that sets up driver interrupt data and calls
3890  * into iflib in order to setup interrupts in its data structures as well.
3891  *
3892  * Like ice_if_msix_intr_assign, currently requires that we get at least the same
3893  * number of vectors as we have queues, and that we always have the same number
3894  * of Tx and Rx queues. Unlike that function, this calls a special
3895  * iflib_irq_alloc_generic_subif() function for RX interrupts because the
3896  * driver needs to get MSI-X resources from the parent device.
3897  *
3898  * Tx queues use a softirq instead of using their own hardware interrupt so that
3899  * remains unchanged.
3900  *
3901  * Returns 0 on success or an error code from iflib_irq_alloc_generic_subctx()
3902  * on failure.
3903  */
3904 static int
3905 ice_wire_mirror_intrs(struct ice_mirr_if *mif)
3906 {
3907 	struct ice_softc *sc = mif->back;
3908 	struct ice_hw *hw = &sc->hw;
3909 	struct ice_vsi *vsi = mif->vsi;
3910 	device_t dev = mif->subdev;
3911 	int err, i, rid;
3912 
3913 	if_ctx_t ctx = mif->subctx;
3914 
3915 	ice_debug(hw, ICE_DBG_INIT, "%s: Last rid: %d\n", __func__, sc->last_rid);
3916 
3917 	rid = sc->last_rid + 1;
3918 	for (i = 0; i < vsi->num_rx_queues; i++, rid++) {
3919 		struct ice_rx_queue *rxq = &vsi->rx_queues[i];
3920 		struct ice_tx_queue *txq = &vsi->tx_queues[i];
3921 		char irq_name[16];
3922 
3923 		// TODO: Change to use dynamic interface number
3924 		snprintf(irq_name, sizeof(irq_name), "m0rxq%d", i);
3925 		/* First arg is parent device (physical port's) iflib ctx */
3926 		err = iflib_irq_alloc_generic_subctx(sc->ctx, ctx,
3927 		    &mif->rx_irqvs[i].irq, rid, IFLIB_INTR_RXTX, ice_msix_que,
3928 		    rxq, rxq->me, irq_name);
3929 		if (err) {
3930 			device_printf(dev,
3931 			    "Failed to allocate q int %d err: %s\n",
3932 			    i, ice_err_str(err));
3933 			i--;
3934 			goto fail;
3935 		}
3936 		MPASS(rid - 1 > 0);
3937 		/* Set vector number used in interrupt enable/disable functions */
3938 		mif->rx_irqvs[i].me = rid - 1;
3939 		rxq->irqv = &mif->rx_irqvs[i];
3940 
3941 		bzero(irq_name, sizeof(irq_name));
3942 		snprintf(irq_name, sizeof(irq_name), "m0txq%d", i);
3943 		iflib_softirq_alloc_generic(ctx, &mif->rx_irqvs[i].irq,
3944 		    IFLIB_INTR_TX, txq, txq->me, irq_name);
3945 		txq->irqv = &mif->rx_irqvs[i];
3946 	}
3947 
3948 	sc->last_rid = rid - 1;
3949 
3950 	ice_debug(hw, ICE_DBG_INIT, "%s: New last rid: %d\n", __func__,
3951 	    sc->last_rid);
3952 
3953 	return (0);
3954 
3955 fail:
3956 	for (; i >= 0; i--)
3957 		iflib_irq_free(ctx, &mif->rx_irqvs[i].irq);
3958 	return (err);
3959 }
3960 
3961 /**
3962  * ice_subif_rebuild - Rebuild subinterface post reset
3963  * @sc: The device private softc
3964  *
3965  * Restore subinterface state after a reset occurred.
3966  * Restart the VSI and enable the mirroring.
3967  */
3968 static int
3969 ice_subif_rebuild(struct ice_softc *sc)
3970 {
3971 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(sc->ctx);
3972 	struct ice_vsi *vsi = sc->mirr_if->vsi;
3973 	int err;
3974 
3975 	err = ice_subif_rebuild_vsi_qmap(sc);
3976 	if (err) {
3977 		device_printf(sc->dev, "Unable to re-assign mirror VSI queues, err %s\n",
3978 		      ice_err_str(err));
3979 		return (err);
3980 	}
3981 
3982 	err = ice_initialize_vsi(vsi);
3983 	if (err) {
3984 		device_printf(sc->dev, "Unable to re-initialize mirror VSI, err %s\n",
3985 		      ice_err_str(err));
3986 		goto err_release_queue_allocations_subif;
3987 	}
3988 
3989 	err = ice_config_rss(vsi);
3990 	if (err) {
3991 		device_printf(sc->dev,
3992 		      "Unable to reconfigure RSS for the mirror VSI, err %s\n",
3993 		      ice_err_str(err));
3994 		goto err_deinit_subif_vsi;
3995 	}
3996 
3997 	vsi->mirror_src_vsi = sc->pf_vsi.idx;
3998 
3999 	err = ice_setup_vsi_mirroring(vsi);
4000 	if (err) {
4001 		device_printf(sc->dev,
4002 		      "Unable to configure mirroring for VSI: %s\n",
4003 		      ice_err_str(err));
4004 		goto err_deinit_subif_vsi;
4005 	}
4006 
4007 	ice_set_state(&mif->state, ICE_STATE_SUBIF_NEEDS_REINIT);
4008 
4009 	return (0);
4010 
4011 err_deinit_subif_vsi:
4012 	ice_deinit_vsi(vsi);
4013 err_release_queue_allocations_subif:
4014 	ice_resmgr_release_map(&sc->tx_qmgr, vsi->tx_qmap,
4015 	    sc->mirr_if->num_irq_vectors);
4016 	ice_resmgr_release_map(&sc->rx_qmgr, vsi->rx_qmap,
4017 	    sc->mirr_if->num_irq_vectors);
4018 
4019 	return (err);
4020 }
4021 
4022 /**
4023  * ice_subif_rebuild_vsi_qmap - Rebuild the mirror VSI queue mapping
4024  * @sc: the device softc pointer
4025  *
4026  * Loops over the Tx and Rx queues for the mirror VSI and reassigns the queue
4027  * mapping after a reset occurred.
4028  */
4029 static int
4030 ice_subif_rebuild_vsi_qmap(struct ice_softc *sc)
4031 {
4032 	struct ice_vsi *vsi = sc->mirr_if->vsi;
4033 	struct ice_tx_queue *txq;
4034 	struct ice_rx_queue *rxq;
4035 	int err, i;
4036 
4037 	err = ice_resmgr_assign_scattered(&sc->tx_qmgr, vsi->tx_qmap, sc->mirr_if->num_irq_vectors);
4038 	if (err) {
4039 		device_printf(sc->dev, "Unable to assign mirror VSI Tx queues: %s\n",
4040 		      ice_err_str(err));
4041 		return (err);
4042 	}
4043 
4044 	err = ice_resmgr_assign_scattered(&sc->rx_qmgr, vsi->rx_qmap, sc->mirr_if->num_irq_vectors);
4045 	if (err) {
4046 		device_printf(sc->dev, "Unable to assign mirror VSI Rx queues: %s\n",
4047 		      ice_err_str(err));
4048 		goto err_release_tx_queues;
4049 	}
4050 
4051 	vsi->qmap_type = ICE_RESMGR_ALLOC_SCATTERED;
4052 
4053 	/* Re-assign Tx queue tail pointers */
4054 	for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++)
4055 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
4056 
4057 	/* Re-assign Rx queue tail pointers */
4058 	for (i = 0, rxq = vsi->rx_queues; i < vsi->num_rx_queues; i++, rxq++)
4059 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
4060 
4061 	return (0);
4062 
4063 err_release_tx_queues:
4064 	ice_resmgr_release_map(&sc->tx_qmgr, vsi->tx_qmap, vsi->num_tx_queues);
4065 
4066 	return (err);
4067 }
4068 
4069 /**
4070  * ice_subif_if_tx_queues_alloc - Allocate Tx queue memory for subinterfaces
4071  * @ctx: iflib context structure
4072  * @vaddrs: virtual addresses for the queue memory
4073  * @paddrs: physical addresses for the queue memory
4074  * @ntxqs: the number of Tx queues per set (should always be 1)
4075  * @ntxqsets: the number of Tx queue sets to allocate
4076  *
4077  * See ice_if_tx_queues_alloc() description. Similar to that function, but
4078  * for subinterfaces instead.
4079  */
4080 static int
4081 ice_subif_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
4082 			     int __invariant_only ntxqs, int ntxqsets)
4083 {
4084 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
4085 	struct ice_tx_queue *txq;
4086 	device_t dev = mif->subdev;
4087 	struct ice_vsi *vsi;
4088 	int err, i, j;
4089 
4090 	MPASS(mif != NULL);
4091 	MPASS(ntxqs == 1);
4092 	MPASS(mif->subscctx->isc_ntxd[0] <= ICE_MAX_DESC_COUNT);
4093 
4094 	vsi = mif->vsi;
4095 
4096 	MPASS(vsi->num_tx_queues == ntxqsets);
4097 
4098 	/* Allocate queue structure memory */
4099 	if (!(vsi->tx_queues =
4100 	      (struct ice_tx_queue *)malloc(sizeof(struct ice_tx_queue) * ntxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
4101 		device_printf(dev, "%s: Unable to allocate Tx queue memory for subfunction\n",
4102 		    __func__);
4103 		return (ENOMEM);
4104 	}
4105 
4106 	/* Allocate report status arrays */
4107 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
4108 		if (!(txq->tx_rsq =
4109 		      (uint16_t *)malloc(sizeof(uint16_t) * mif->subscctx->isc_ntxd[0], M_ICE, M_NOWAIT))) {
4110 			device_printf(dev,
4111 			    "%s: Unable to allocate tx_rsq memory for subfunction\n", __func__);
4112 			err = ENOMEM;
4113 			goto free_tx_queues;
4114 		}
4115 		/* Initialize report status array */
4116 		for (j = 0; j < mif->subscctx->isc_ntxd[0]; j++)
4117 			txq->tx_rsq[j] = QIDX_INVALID;
4118 	}
4119 
4120 	/* Add Tx queue sysctls context */
4121 	ice_vsi_add_txqs_ctx(vsi);
4122 
4123 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
4124 		/* q_handle == me when only one TC */
4125 		txq->me = txq->q_handle = i;
4126 		txq->vsi = vsi;
4127 
4128 		/* store the queue size for easier access */
4129 		txq->desc_count = mif->subscctx->isc_ntxd[0];
4130 
4131 		/* get the virtual and physical address of the hardware queues */
4132 		txq->tail = QTX_COMM_DBELL(vsi->tx_qmap[i]);
4133 		txq->tx_base = (struct ice_tx_desc *)vaddrs[i];
4134 		txq->tx_paddr = paddrs[i];
4135 
4136 		ice_add_txq_sysctls(txq);
4137 	}
4138 
4139 	return (0);
4140 
4141 free_tx_queues:
4142 	for (i = 0, txq = vsi->tx_queues; i < ntxqsets; i++, txq++) {
4143 		if (txq->tx_rsq != NULL) {
4144 			free(txq->tx_rsq, M_ICE);
4145 			txq->tx_rsq = NULL;
4146 		}
4147 	}
4148 	free(vsi->tx_queues, M_ICE);
4149 	vsi->tx_queues = NULL;
4150 	return (err);
4151 }
4152 
4153 /**
4154  * ice_subif_if_rx_queues_alloc - Allocate Rx queue memory for subinterfaces
4155  * @ctx: iflib context structure
4156  * @vaddrs: virtual addresses for the queue memory
4157  * @paddrs: physical addresses for the queue memory
4158  * @nrxqs: number of Rx queues per set (should always be 1)
4159  * @nrxqsets: number of Rx queue sets to allocate
4160  *
4161  * See ice_if_rx_queues_alloc() for general summary; this is similar to that
4162  * but implemented for subinterfaces.
4163  */
4164 static int
4165 ice_subif_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
4166     int __invariant_only nrxqs, int nrxqsets)
4167 {
4168 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
4169 	struct ice_rx_queue *rxq;
4170 	device_t dev = mif->subdev;
4171 	struct ice_vsi *vsi;
4172 	int i;
4173 
4174 	MPASS(mif != NULL);
4175 	MPASS(nrxqs == 1);
4176 	MPASS(mif->subscctx->isc_nrxd[0] <= ICE_MAX_DESC_COUNT);
4177 
4178 	vsi = mif->vsi;
4179 
4180 	MPASS(vsi->num_rx_queues == nrxqsets);
4181 
4182 	/* Allocate queue structure memory */
4183 	if (!(vsi->rx_queues =
4184 	      (struct ice_rx_queue *) malloc(sizeof(struct ice_rx_queue) * nrxqsets, M_ICE, M_NOWAIT | M_ZERO))) {
4185 		device_printf(dev, "%s: Unable to allocate Rx queue memory for subfunction\n",
4186 		    __func__);
4187 		return (ENOMEM);
4188 	}
4189 
4190 	/* Add Rx queue sysctls context */
4191 	ice_vsi_add_rxqs_ctx(vsi);
4192 
4193 	for (i = 0, rxq = vsi->rx_queues; i < nrxqsets; i++, rxq++) {
4194 		rxq->me = i;
4195 		rxq->vsi = vsi;
4196 
4197 		/* store the queue size for easier access */
4198 		rxq->desc_count = mif->subscctx->isc_nrxd[0];
4199 
4200 		/* get the virtual and physical address of the hardware queues */
4201 		rxq->tail = QRX_TAIL(vsi->rx_qmap[i]);
4202 		rxq->rx_base = (union ice_32b_rx_flex_desc *)vaddrs[i];
4203 		rxq->rx_paddr = paddrs[i];
4204 
4205 		ice_add_rxq_sysctls(rxq);
4206 	}
4207 
4208 	return (0);
4209 }
4210 
4211 /**
4212  * ice_subif_if_msix_intr_assign - Assign MSI-X interrupts to new sub interface
4213  * @ctx: the iflib context structure
4214  * @msix: the number of vectors we were assigned
4215  *
4216  * Allocates and assigns driver private resources for MSI-X interrupt tracking.
4217  *
4218  * @pre OS MSI-X resources have been pre-allocated by parent interface.
4219  */
4220 static int
4221 ice_subif_if_msix_intr_assign(if_ctx_t ctx, int msix)
4222 {
4223 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
4224 	struct ice_softc *sc = mif->back;
4225 	struct ice_vsi *vsi = mif->vsi;
4226 
4227 	device_t dev = mif->subdev;
4228 	int ret;
4229 
4230 	if (vsi->num_rx_queues != vsi->num_tx_queues) {
4231 		device_printf(dev,
4232 			      "iflib requested %d Tx queues, and %d Rx queues, but the driver isn't able to support a differing number of Tx and Rx queues\n",
4233 			      vsi->num_tx_queues, vsi->num_rx_queues);
4234 		return (EOPNOTSUPP);
4235 	}
4236 
4237 	if (msix > sc->extra_vectors) {
4238 		device_printf(dev,
4239 		     "%s: Not enough spare (%d) msix vectors for new sub-interface requested (%d)\n",
4240 		     __func__, sc->extra_vectors, msix);
4241 		return (ENOSPC);
4242 	}
4243 	device_printf(dev, "%s: Using %d vectors for sub-interface\n", __func__,
4244 	    msix);
4245 
4246 	/* Allocate space to store the IRQ vector data */
4247 	mif->num_irq_vectors = vsi->num_rx_queues;
4248 	mif->rx_irqvs = (struct ice_irq_vector *)
4249 	    malloc(sizeof(struct ice_irq_vector) * (mif->num_irq_vectors),
4250 		   M_ICE, M_NOWAIT);
4251 	if (!mif->rx_irqvs) {
4252 		device_printf(dev,
4253 			      "Unable to allocate RX irqv memory for mirror's %d vectors\n",
4254 			      mif->num_irq_vectors);
4255 		return (ENOMEM);
4256 	}
4257 
4258 	/* Assign mirror interface interrupts from PF device space */
4259 	if (!(mif->if_imap =
4260 	      (u16 *)malloc(sizeof(u16) * mif->num_irq_vectors,
4261 	      M_ICE, M_NOWAIT))) {
4262 		device_printf(dev, "Unable to allocate mirror intfc if_imap memory\n");
4263 		ret = ENOMEM;
4264 		goto free_irqvs;
4265 	}
4266 	ret = ice_resmgr_assign_contiguous(&sc->dev_imgr, mif->if_imap, mif->num_irq_vectors);
4267 	if (ret) {
4268 		device_printf(dev, "Unable to assign mirror intfc PF device interrupt mapping: %s\n",
4269 			      ice_err_str(ret));
4270 		goto free_if_imap;
4271 	}
4272 	/* Assign mirror interface interrupts from OS interrupt allocation space */
4273 	if (!(mif->os_imap =
4274 	      (u16 *)malloc(sizeof(u16) * mif->num_irq_vectors,
4275 	      M_ICE, M_NOWAIT))) {
4276 		device_printf(dev, "Unable to allocate mirror intfc os_imap memory\n");
4277 		ret = ENOMEM;
4278 		goto free_if_imap;
4279 	}
4280 	ret = ice_resmgr_assign_contiguous(&sc->os_imgr, mif->os_imap, mif->num_irq_vectors);
4281 	if (ret) {
4282 		device_printf(dev, "Unable to assign mirror intfc OS interrupt mapping: %s\n",
4283 			      ice_err_str(ret));
4284 		goto free_if_imap;
4285 	}
4286 
4287 	return (0);
4288 
4289 free_if_imap:
4290 	free(mif->if_imap, M_ICE);
4291 	mif->if_imap = NULL;
4292 free_irqvs:
4293 	free(mif->rx_irqvs, M_ICE);
4294 	mif->rx_irqvs = NULL;
4295 	return (ret);
4296 }
4297 
4298 /**
4299  * ice_subif_if_intr_enable - Enable device interrupts for a subinterface
4300  * @ctx: iflib context structure
4301  *
4302  * Called by iflib to request enabling all interrupts that belong to a
4303  * subinterface.
4304  */
4305 static void
4306 ice_subif_if_intr_enable(if_ctx_t ctx)
4307 {
4308 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
4309 	struct ice_softc *sc = mif->back;
4310 	struct ice_vsi *vsi = mif->vsi;
4311 	struct ice_hw *hw = &sc->hw;
4312 
4313 	/* Do not enable queue interrupts in recovery mode */
4314 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
4315 		return;
4316 
4317 	/* Enable all queue interrupts */
4318 	for (int i = 0; i < vsi->num_rx_queues; i++)
4319 		ice_enable_intr(hw, vsi->rx_queues[i].irqv->me);
4320 }
4321 
4322 /**
4323  * ice_subif_if_rx_queue_intr_enable - Enable a specific Rx queue interrupt
4324  * @ctx: iflib context structure
4325  * @rxqid: the Rx queue to enable
4326  *
4327  * Enable a specific Rx queue interrupt.
4328  *
4329  * This function is not protected by the iflib CTX lock.
4330  */
4331 static int
4332 ice_subif_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t rxqid)
4333 {
4334 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
4335 	struct ice_softc *sc = mif->back;
4336 	struct ice_vsi *vsi = mif->vsi;
4337 	struct ice_hw *hw = &sc->hw;
4338 
4339 	/* Do not enable queue interrupts in recovery mode */
4340 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
4341 		return (ENOSYS);
4342 
4343 	ice_enable_intr(hw, vsi->rx_queues[rxqid].irqv->me);
4344 	return (0);
4345 }
4346 
4347 /**
4348  * ice_subif_if_tx_queue_intr_enable - Enable a specific Tx queue interrupt
4349  * @ctx: iflib context structure
4350  * @txqid: the Tx queue to enable
4351  *
4352  * Enable a specific Tx queue interrupt.
4353  *
4354  * This function is not protected by the iflib CTX lock.
4355  */
4356 static int
4357 ice_subif_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t txqid)
4358 {
4359 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
4360 	struct ice_softc *sc = mif->back;
4361 	struct ice_vsi *vsi = mif->vsi;
4362 	struct ice_hw *hw = &sc->hw;
4363 
4364 	/* Do not enable queue interrupts in recovery mode */
4365 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
4366 		return (ENOSYS);
4367 
4368 	ice_enable_intr(hw, vsi->tx_queues[txqid].irqv->me);
4369 	return (0);
4370 }
4371 
4372 /**
4373  * ice_subif_if_init - Initialize the subinterface
4374  * @ctx: iflib ctx structure
4375  *
4376  * Called by iflib to bring the device up, i.e. ifconfig ice0m0 up.
4377  * Prepares the Tx and Rx engines and enables interrupts.
4378  *
4379  * @pre assumes the caller holds the iflib CTX lock
4380  */
4381 static void
4382 ice_subif_if_init(if_ctx_t ctx)
4383 {
4384 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
4385 	struct ice_softc *sc = mif->back;
4386 	struct ice_vsi *vsi = mif->vsi;
4387 	device_t dev = mif->subdev;
4388 	int err;
4389 
4390 	if (ice_driver_is_detaching(sc))
4391 		return;
4392 
4393 	if (ice_test_state(&sc->state, ICE_STATE_RECOVERY_MODE))
4394 		return;
4395 
4396 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
4397 		device_printf(dev,
4398 		    "request to start interface cannot be completed as the parent device %s failed to reset\n",
4399 		    device_get_nameunit(sc->dev));
4400 		return;
4401 	}
4402 
4403 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
4404 		device_printf(dev,
4405 		    "request to start interface cannot be completed while parent device %s is prepared for impending reset\n",
4406 		    device_get_nameunit(sc->dev));
4407 		return;
4408 	}
4409 
4410 	/* XXX: Equiv to ice_update_rx_mbuf_sz */
4411 	vsi->mbuf_sz = iflib_get_rx_mbuf_sz(ctx);
4412 
4413 	/* Initialize software Tx tracking values */
4414 	ice_init_tx_tracking(vsi);
4415 
4416 	err = ice_cfg_vsi_for_tx(vsi);
4417 	if (err) {
4418 		device_printf(dev,
4419 			      "Unable to configure subif VSI for Tx: %s\n",
4420 			      ice_err_str(err));
4421 		return;
4422 	}
4423 
4424 	err = ice_cfg_vsi_for_rx(vsi);
4425 	if (err) {
4426 		device_printf(dev,
4427 			      "Unable to configure subif VSI for Rx: %s\n",
4428 			      ice_err_str(err));
4429 		goto err_cleanup_tx;
4430 	}
4431 
4432 	err = ice_control_all_rx_queues(vsi, true);
4433 	if (err) {
4434 		device_printf(dev,
4435 			      "Unable to enable subif Rx rings for receive: %s\n",
4436 			      ice_err_str(err));
4437 		goto err_cleanup_tx;
4438 	}
4439 
4440 	ice_configure_all_rxq_interrupts(vsi);
4441 	ice_configure_rx_itr(vsi);
4442 
4443 	ice_set_state(&mif->state, ICE_STATE_DRIVER_INITIALIZED);
4444 	return;
4445 
4446 err_cleanup_tx:
4447 	ice_vsi_disable_tx(vsi);
4448 }
4449 
4450 /**
4451  * ice_if_stop_subif - Stop the subinterface
4452  * @ctx: iflib context structure
4453  * @ifs: subinterface context structure
4454  *
4455  * Called by iflib to stop the subinterface and bring it down.
4456  * (e.g. ifconfig ice0m0 down)
4457  *
4458  * @pre assumes the caller holds the iflib CTX lock
4459  */
4460 static void
4461 ice_subif_if_stop(if_ctx_t ctx)
4462 {
4463 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
4464 	struct ice_softc *sc = mif->back;
4465 	struct ice_vsi *vsi = mif->vsi;
4466 	device_t dev = mif->subdev;
4467 
4468 	if (!ice_testandclear_state(&mif->state, ICE_STATE_DRIVER_INITIALIZED))
4469 		return;
4470 
4471 	if (ice_test_state(&sc->state, ICE_STATE_RESET_FAILED)) {
4472 		device_printf(dev,
4473 		    "request to stop interface cannot be completed as the parent device %s failed to reset\n",
4474 		    device_get_nameunit(sc->dev));
4475 		return;
4476 	}
4477 
4478 	if (ice_test_state(&sc->state, ICE_STATE_PREPARED_FOR_RESET)) {
4479 		device_printf(dev,
4480 		    "request to stop interface cannot be completed while parent device %s is prepared for impending reset\n",
4481 		    device_get_nameunit(sc->dev));
4482 		return;
4483 	}
4484 
4485 	/* Dissociate the Tx and Rx queues from the interrupts */
4486 	ice_flush_txq_interrupts(vsi);
4487 	ice_flush_rxq_interrupts(vsi);
4488 
4489 	/* Disable the Tx and Rx queues */
4490 	ice_vsi_disable_tx(vsi);
4491 	ice_control_all_rx_queues(vsi, false);
4492 }
4493 
4494 /**
4495  * ice_free_irqvs_subif - Free IRQ vector memory for subinterfaces
4496  * @mif: Mirror interface private structure
4497  *
4498  * Free IRQ vector memory allocated during ice_subif_if_msix_intr_assign.
4499  */
4500 static void
4501 ice_free_irqvs_subif(struct ice_mirr_if *mif)
4502 {
4503 	struct ice_softc *sc = mif->back;
4504 	struct ice_vsi *vsi = mif->vsi;
4505 	if_ctx_t ctx = sc->ctx;
4506 	int i;
4507 
4508 	/* If the irqvs array is NULL, then there are no vectors to free */
4509 	if (mif->rx_irqvs == NULL)
4510 		return;
4511 
4512 	/* Free the IRQ vectors -- currently subinterfaces have number
4513 	 * of vectors equal to number of RX queues
4514 	 *
4515 	 * XXX: ctx is parent device's ctx, not the subinterface ctx
4516 	 */
4517 	for (i = 0; i < vsi->num_rx_queues; i++)
4518 		iflib_irq_free(ctx, &mif->rx_irqvs[i].irq);
4519 
4520 	ice_resmgr_release_map(&sc->os_imgr, mif->os_imap,
4521 	    mif->num_irq_vectors);
4522 	ice_resmgr_release_map(&sc->dev_imgr, mif->if_imap,
4523 	    mif->num_irq_vectors);
4524 
4525 	sc->last_rid -= vsi->num_rx_queues;
4526 
4527 	/* Clear the irqv pointers */
4528 	for (i = 0; i < vsi->num_rx_queues; i++)
4529 		vsi->rx_queues[i].irqv = NULL;
4530 
4531 	for (i = 0; i < vsi->num_tx_queues; i++)
4532 		vsi->tx_queues[i].irqv = NULL;
4533 
4534 	/* Release the vector array memory */
4535 	free(mif->rx_irqvs, M_ICE);
4536 	mif->rx_irqvs = NULL;
4537 }
4538 
4539 /**
4540  * ice_subif_if_queues_free - Free queue memory for subinterfaces
4541  * @ctx: the iflib context structure
4542  *
4543  * Free queue memory allocated by ice_subif_tx_queues_alloc() and
4544  * ice_subif_if_rx_queues_alloc().
4545  */
4546 static void
4547 ice_subif_if_queues_free(if_ctx_t ctx)
4548 {
4549 	struct ice_mirr_if *mif = (struct ice_mirr_if *)iflib_get_softc(ctx);
4550 	struct ice_vsi *vsi = mif->vsi;
4551 	struct ice_tx_queue *txq;
4552 	int i;
4553 
4554 	/* Free the Tx and Rx sysctl contexts, and assign NULL to the node
4555 	 * pointers.
4556 	 */
4557 	ice_vsi_del_txqs_ctx(vsi);
4558 	ice_vsi_del_rxqs_ctx(vsi);
4559 
4560 	/* Release MSI-X IRQ vectors */
4561 	ice_free_irqvs_subif(mif);
4562 
4563 	if (vsi->tx_queues != NULL) {
4564 		/* free the tx_rsq arrays */
4565 		for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
4566 			if (txq->tx_rsq != NULL) {
4567 				free(txq->tx_rsq, M_ICE);
4568 				txq->tx_rsq = NULL;
4569 			}
4570 		}
4571 		free(vsi->tx_queues, M_ICE);
4572 		vsi->tx_queues = NULL;
4573 	}
4574 	if (vsi->rx_queues != NULL) {
4575 		free(vsi->rx_queues, M_ICE);
4576 		vsi->rx_queues = NULL;
4577 	}
4578 }
4579 
4580 /**
4581  * ice_subif_if_media_status - Report subinterface media
4582  * @ctx: iflib context structure
4583  * @ifmr: ifmedia request structure to update
4584  *
4585  * Updates the provided ifmr with something, in order to prevent a
4586  * "no media types?" message from ifconfig.
4587  *
4588  * Mirror interfaces are always up.
4589  */
4590 static void
4591 ice_subif_if_media_status(if_ctx_t ctx __unused, struct ifmediareq *ifmr)
4592 {
4593 	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
4594 	ifmr->ifm_active = IFM_ETHER | IFM_AUTO;
4595 }
4596 
4597 /**
4598  * ice_subif_if_promisc_set - Set subinterface promiscuous mode
4599  * @ctx: iflib context structure
4600  * @flags: promiscuous flags to configure
4601  *
4602  * Called by iflib to configure device promiscuous mode.
4603  *
4604  * @remark This does not need to be implemented for now.
4605  */
4606 static int
4607 ice_subif_if_promisc_set(if_ctx_t ctx __unused, int flags __unused)
4608 {
4609 	return (0);
4610 }
4611 
4612