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