xref: /freebsd/sys/dev/ice/ice_iov.c (revision a6deeaa2fb3b28a61ae56a85f2275f67fcf23262)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2025, 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 ice_iov.c
34  * @brief Virtualization support functions
35  *
36  * Contains functions for enabling and managing PCIe virtual function devices,
37  * including enabling new VFs, and managing VFs over the virtchnl interface.
38  */
39 
40 #include "ice_iov.h"
41 #include "ice_fault.h"
42 
43 #include <net/if_vf_status.h>
44 
45 /* Version 1 driver.ice extension schema; documented in ice(4). */
46 #define	ICE_VF_STATUS_NAMESPACE			"driver.ice"
47 #define	ICE_VF_STATUS_VERSION			1
48 #define	ICE_VF_STATUS_MIRROR_CONFIGURED		"mirror-configured"
49 #define	ICE_VF_STATUS_MIRROR_SOURCE_VSI		"mirror-source-vsi"
50 #define	ICE_VF_STATUS_MIRROR_INGRESS_ACTIVE	"mirror-ingress-active"
51 #define	ICE_VF_STATUS_MIRROR_EGRESS_ACTIVE	"mirror-egress-active"
52 #define	ICE_VF_STATUS_MDD_BLOCKED		"mdd-blocked"
53 #define	ICE_VF_STATUS_MDD_TX_EVENTS		"mdd-tx-events"
54 #define	ICE_VF_STATUS_MDD_RX_EVENTS		"mdd-rx-events"
55 #define	ICE_VF_STATUS_MBX_BLOCKED		"mailbox-blocked"
56 #define	ICE_VF_STATUS_MBX_OVERFLOW_EVENTS	"mailbox-overflow-events"
57 #define	ICE_VF_STATUS_MAC_FILTER_COUNT		"mac-filter-count"
58 #define	ICE_VF_STATUS_MAC_FILTER_LIMIT		"mac-filter-limit"
59 #define	ICE_VF_STATUS_RESET_FAILED		"reset-failed"
60 #define	ICE_VF_STATUS_REBUILD_REQUIRED		"rebuild-required"
61 
62 /* Optional fields are compacted when absent; values define schema order. */
63 enum ice_vf_status_field {
64 	ICE_VF_STATUS_FIELD_MIRROR_CONFIGURED,
65 	ICE_VF_STATUS_FIELD_MIRROR_SOURCE_VSI,
66 	ICE_VF_STATUS_FIELD_MIRROR_INGRESS_ACTIVE,
67 	ICE_VF_STATUS_FIELD_MIRROR_EGRESS_ACTIVE,
68 	ICE_VF_STATUS_FIELD_MDD_BLOCKED,
69 	ICE_VF_STATUS_FIELD_MDD_TX_EVENTS,
70 	ICE_VF_STATUS_FIELD_MDD_RX_EVENTS,
71 	ICE_VF_STATUS_FIELD_MBX_BLOCKED,
72 	ICE_VF_STATUS_FIELD_MBX_OVERFLOW_EVENTS,
73 	ICE_VF_STATUS_FIELD_MAC_FILTER_COUNT,
74 	ICE_VF_STATUS_FIELD_MAC_FILTER_LIMIT,
75 	ICE_VF_STATUS_FIELD_RESET_FAILED,
76 	ICE_VF_STATUS_FIELD_REBUILD_REQUIRED,
77 	ICE_VF_STATUS_NUM_FIELDS,
78 };
79 
80 #define	ICE_VC_MAX_RX_BUFFER			\
81 	((16 * 1024) - BIT(ICE_RLAN_CTX_DBUF_S))
82 #define	ICE_VIRTCHNL_QUEUE_MAP_SIZE		16
83 
84 #ifdef DRIVER_FAILPOINTS
85 static SYSCTL_NODE(_debug_fail_point_ice, OID_AUTO, iov,
86     CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "ice SR-IOV fail points");
87 
88 static int ice_iov_fail_vf = -1;
89 SYSCTL_INT(_debug_fail_point_ice_iov, OID_AUTO, vf,
90     CTLFLAG_RW | CTLFLAG_MPSAFE, &ice_iov_fail_vf, 0,
91     "VF eligible for ice SR-IOV fail points (-1 selects every VF)");
92 #endif /* DRIVER_FAILPOINTS */
93 static struct ice_vf *ice_iov_get_vf(struct ice_softc *sc, int vf_num);
94 static int ice_iov_configure_mac_anti_spoof(struct ice_softc *sc,
95     struct ice_vf *vf);
96 static int ice_iov_restore_vf_host_config(struct ice_softc *sc,
97     struct ice_vf *vf);
98 static void ice_iov_clear_vf_queue_state(struct ice_vf *vf);
99 static void ice_iov_clear_vf_mbx(struct ice_softc *sc, struct ice_vf *vf);
100 static void ice_iov_complete_vf_reset(struct ice_softc *sc,
101     struct ice_vf *vf, bool restore_mapping);
102 static void ice_iov_ready_vf(struct ice_softc *sc, struct ice_vf *vf);
103 static int ice_reset_vf(struct ice_softc *sc, struct ice_vf *vf,
104 			bool trigger_reset, bool release_vf);
105 static void ice_iov_setup_intr_mapping(struct ice_softc *sc, struct ice_vf *vf);
106 
107 static void ice_vc_version_msg(struct ice_softc *sc, struct ice_vf *vf,
108 			       u8 *msg_buf);
109 static void ice_vc_get_vf_res_msg(struct ice_softc *sc, struct ice_vf *vf,
110 				  u8 *msg_buf);
111 static void ice_vc_add_eth_addr_msg(struct ice_softc *sc, struct ice_vf *vf,
112 				    u8 *msg_buf);
113 static void ice_vc_del_eth_addr_msg(struct ice_softc *sc, struct ice_vf *vf,
114 				    u8 *msg_buf);
115 static bool ice_vc_isvalid_ring_len(u32 ring_len);
116 static void ice_vc_cfg_vsi_qs_msg(struct ice_softc *sc, struct ice_vf *vf,
117 				  u8 *msg_buf);
118 static void ice_vc_cfg_rss_key_msg(struct ice_softc *sc, struct ice_vf *vf,
119 				   u8 *msg_buf);
120 static void ice_vc_set_rss_hena_msg(struct ice_softc *sc, struct ice_vf *vf,
121 				    u8 *msg_buf);
122 static void ice_vc_enable_queues_msg(struct ice_softc *sc, struct ice_vf *vf,
123 				     u8 *msg_buf);
124 static void ice_vc_notify_vf_link_state(struct ice_softc *sc, struct ice_vf *vf);
125 static void ice_vc_disable_queues_msg(struct ice_softc *sc, struct ice_vf *vf,
126 				      u8 *msg_buf);
127 static int ice_vc_disable_queues(struct ice_softc *sc, struct ice_vf *vf,
128 				 u32 tx_queues, u32 rx_queues);
129 static void ice_vc_cfg_irq_map_msg(struct ice_softc *sc, struct ice_vf *vf,
130 				   u8 *msg_buf);
131 static void ice_vc_get_stats_msg(struct ice_softc *sc, struct ice_vf *vf,
132 				 u8 *msg_buf);
133 static void ice_eth_stats_to_virtchnl_eth_stats(struct ice_eth_stats *istats,
134      struct virtchnl_eth_stats *vstats);
135 static void ice_vc_cfg_rss_lut_msg(struct ice_softc *sc, struct ice_vf *vf,
136 				   u8 *msg_buf);
137 static void ice_vc_cfg_promisc_mode_msg(struct ice_softc *sc, struct ice_vf *vf,
138 				        u8 *msg_buf);
139 static void ice_vc_add_vlan_msg(struct ice_softc *sc, struct ice_vf *vf,
140 				u8 *msg_buf);
141 static void ice_vc_del_vlan_msg(struct ice_softc *sc, struct ice_vf *vf,
142 				u8 *msg_buf);
143 static int ice_vc_select_vlans(struct ice_vf *vf, u16 *vids, u16 count,
144 			       bool add, u16 *selected_count);
145 static enum virtchnl_status_code ice_iov_err_to_virt_err(int ice_err);
146 static int ice_vf_mac_filter_index(struct ice_vf *vf, const uint8_t *addr);
147 static int ice_vf_validate_mac(struct ice_vf *vf, const uint8_t *addr);
148 
149 #ifdef DRIVER_FAILPOINTS
150 static bool
151 ice_iov_fail_vf_matches(uint16_t vfnum)
152 {
153 	return (ice_iov_fail_vf == -1 || ice_iov_fail_vf == vfnum);
154 }
155 #endif
156 
157 #define	ICE_IOV_FAIL_POINT(_sc, _vfnum, _name, _error, _label) do { \
158 	ICE_FAIL_POINT_CODE_COND(_sc, _debug_fail_point_ice_iov, _name, \
159 	    ice_iov_fail_vf_matches((_vfnum)), \
160 	    FAIL_POINT_NONSLEEPABLE, { \
161 		(_error) = RETURN_VALUE; \
162 		if ((_error) <= 0) \
163 			(_error) = EIO; \
164 		device_printf((_sc)->dev, \
165 		    "injecting VF %u failure at %s: %d\n", \
166 		    (unsigned int)(_vfnum), #_name, (_error)); \
167 		goto _label; \
168 	}); \
169 } while (0)
170 
171 /**
172  * ice_iov_attach - Initialize SR-IOV PF host support
173  * @sc: device softc structure
174  *
175  * Initialize SR-IOV PF host support at the end of the driver attach process.
176  *
177  * @pre Must be called from sleepable context (calls malloc() w/ M_WAITOK)
178  *
179  * @returns 0 if successful, or
180  * - ENOMEM if there is no memory for the PF/VF schemas or iov device
181  * - ENXIO if the device isn't PCI-E or doesn't support the same SR-IOV
182  *   version as the kernel
183  * - ENOENT if the device doesn't have the SR-IOV capability
184  */
185 int
186 ice_iov_attach(struct ice_softc *sc)
187 {
188 	device_t dev = sc->dev;
189 	nvlist_t *pf_schema, *vf_schema;
190 	int error;
191 
192 	pf_schema = pci_iov_schema_alloc_node();
193 	vf_schema = pci_iov_schema_alloc_node();
194 
195 	pci_iov_schema_add_unicast_mac(vf_schema, "mac-addr", 0, NULL);
196 	pci_iov_schema_add_bool(vf_schema, "mac-anti-spoof",
197 	    IOV_SCHEMA_HASDEFAULT, TRUE);
198 	pci_iov_schema_add_bool(vf_schema, "allow-set-mac",
199 	    IOV_SCHEMA_HASDEFAULT, FALSE);
200 	pci_iov_schema_add_bool(vf_schema, "allow-promisc",
201 	    IOV_SCHEMA_HASDEFAULT, FALSE);
202 	pci_iov_schema_add_uint16(vf_schema, "num-queues",
203 	    IOV_SCHEMA_HASDEFAULT, ICE_DEFAULT_VF_QUEUES);
204 	pci_iov_schema_add_uint16(vf_schema, "mirror-src-vsi",
205 	    IOV_SCHEMA_HASDEFAULT, ICE_INVALID_MIRROR_VSI);
206 	pci_iov_schema_add_uint16(vf_schema, "max-vlan-allowed",
207 	    IOV_SCHEMA_HASDEFAULT, ICE_DEFAULT_VF_VLAN_LIMIT);
208 	pci_iov_schema_add_uint16(vf_schema, "max-mac-filters",
209 	    IOV_SCHEMA_HASDEFAULT, ICE_DEFAULT_VF_FILTER_LIMIT);
210 
211 	error = pci_iov_attach(dev, pf_schema, vf_schema);
212 	if (error != 0) {
213 		device_printf(dev,
214 		    "pci_iov_attach failed (error=%s)\n",
215 		    ice_err_str(error));
216 		ice_clear_bit(ICE_FEATURE_SRIOV, sc->feat_en);
217 	} else {
218 		ice_set_bit(ICE_FEATURE_SRIOV, sc->feat_en);
219 		if (ice_is_e830(&sc->hw))
220 			ice_iov_reconfigure_mbx(sc);
221 		else
222 			ice_mbx_init_snapshot(&sc->hw);
223 	}
224 
225 	return (error);
226 }
227 
228 /**
229  * ice_iov_reconfigure_mbx - Restore hardware mailbox flood protection
230  * @sc: device softc structure
231  *
232  * E830 limits each VF's outstanding messages in hardware.  The threshold
233  * register is reset by a core reset and must be restored during rebuild.
234  * Older devices use the software snapshot detector instead.
235  */
236 void
237 ice_iov_reconfigure_mbx(struct ice_softc *sc)
238 {
239 	struct ice_hw *hw = &sc->hw;
240 
241 	if (!ice_is_e830(hw))
242 		return;
243 
244 	wr32(hw, E830_MBX_PF_IN_FLIGHT_VF_MSGS_THRESH,
245 	    ICE_MBX_OVERFLOW_WATERMARK);
246 	ice_flush(hw);
247 }
248 
249 /**
250  * ice_iov_detach - Teardown SR-IOV PF host support
251  * @sc: device softc structure
252  *
253  * Teardown SR-IOV PF host support at the start of the driver detach process.
254  *
255  * @returns 0 if successful or IOV support hasn't been setup, or
256  * - EBUSY if VFs still exist
257  */
258 int
259 ice_iov_detach(struct ice_softc *sc)
260 {
261 	device_t dev = sc->dev;
262 	int error;
263 
264 	error = pci_iov_detach(dev);
265 	if (error != 0) {
266 		device_printf(dev,
267 		    "pci_iov_detach failed (error=%s)\n",
268 		    ice_err_str(error));
269 	}
270 
271 	return (error);
272 }
273 
274 /**
275  * ice_iov_init - Called by the OS before the first VF is created.
276  * @sc: device softc structure
277  * @num_vfs: number of VFs to setup resources for
278  * @params: configuration parameters for the PF
279  *
280  * @returns 0 if successful or an error code on failure
281  */
282 int
283 ice_iov_init(struct ice_softc *sc, uint16_t num_vfs, const nvlist_t *params __unused)
284 {
285 	/* Allocate array of VFs, for tracking */
286 	sc->vfs = (struct ice_vf *)malloc(sizeof(struct ice_vf) * num_vfs, M_ICE, M_NOWAIT |
287 	    M_ZERO);
288 	if (sc->vfs == NULL)
289 		return (ENOMEM);
290 
291 	/* Initialize each VF with basic information */
292 	for (int i = 0; i < num_vfs; i++) {
293 		sc->vfs[i].vf_num = i;
294 		if (ice_is_e830(&sc->hw))
295 			ice_mbx_vf_clear_cnt_e830(&sc->hw, i);
296 		else
297 			ice_mbx_init_vf_info(&sc->hw, &sc->vfs[i].mbx_info);
298 	}
299 
300 	/* Save off number of configured VFs */
301 	sc->num_vfs = num_vfs;
302 
303 	return (0);
304 }
305 
306 /**
307  * ice_iov_get_vf - Get pointer to VF at given index
308  * @sc: device softc structure
309  * @vf_num: Index of VF to retrieve
310  *
311  * @remark will throw an assertion if vf_num is not in the
312  * range of allocated VFs
313  *
314  * @returns a pointer to the VF structure at the given index
315  */
316 static struct ice_vf *
317 ice_iov_get_vf(struct ice_softc *sc, int vf_num)
318 {
319 	MPASS(vf_num < sc->num_vfs);
320 
321 	return &sc->vfs[vf_num];
322 }
323 
324 /**
325  * ice_iov_configure_mac_anti_spoof - Apply a VF's source-MAC policy
326  * @sc: device softc structure
327  * @vf: VF whose VSI security policy should be configured
328  *
329  * PF and device resets discard the hardware VSI context, so callers must
330  * replay this policy after creating or rebuilding the VF's VSI.  Also reapply
331  * the PF-owned policy defensively before releasing a VF after VFR.
332  */
333 static int
334 ice_iov_configure_mac_anti_spoof(struct ice_softc *sc, struct ice_vf *vf)
335 {
336 	struct ice_vsi_ctx ctx = { 0 };
337 	struct ice_vsi *vsi = vf->vsi;
338 	struct ice_hw *hw = &sc->hw;
339 	bool enable;
340 #ifdef DRIVER_FAILPOINTS
341 	int error;
342 #endif
343 	int status;
344 
345 	enable = (atomic_load_acq_32(&vf->vf_flags) &
346 	    VF_FLAG_MAC_ANTI_SPOOF) != 0;
347 	ctx.info.sec_flags = vsi->info.sec_flags;
348 	ctx.info.valid_sections =
349 	    CPU_TO_LE16(ICE_AQ_VSI_PROP_SECURITY_VALID);
350 	if (enable)
351 		ctx.info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
352 	else
353 		ctx.info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
354 
355 	ICE_IOV_FAIL_POINT(sc, vf->vf_num, mac_anti_spoof_update, error,
356 	    fail);
357 	status = ice_update_vsi(hw, vsi->idx, &ctx, NULL);
358 	if (status != 0) {
359 		device_printf(sc->dev,
360 		    "Unable to configure VF %u MAC anti-spoof %s, "
361 		    "err %s aq_err %s\n", vf->vf_num,
362 		    enable ? "on" : "off", ice_status_str(status),
363 		    ice_aq_str(hw->adminq.sq_last_status));
364 		return (EIO);
365 	}
366 
367 	vsi->info.sec_flags = ctx.info.sec_flags;
368 	return (0);
369 
370 #ifdef DRIVER_FAILPOINTS
371 fail:
372 	return (error);
373 #endif
374 }
375 
376 /**
377  * ice_iov_restore_vf_host_config - Restore PF-owned policy after a VF reset
378  * @sc: device softc structure
379  * @vf: VF whose host configuration should be restored
380  *
381  * A VF reset discards the guest's filter configuration. Remove the matching
382  * software switch state as well so that replayed guest requests reach
383  * firmware instead of being mistaken for filters which still exist. Restore
384  * the PF-owned source-MAC policy and base filters before releasing the VF.
385  */
386 static int
387 ice_iov_restore_vf_host_config(struct ice_softc *sc, struct ice_vf *vf)
388 {
389 	struct ice_vsi *vsi = vf->vsi;
390 	int error;
391 
392 	ice_remove_vsi_fltr(&sc->hw, vsi->idx);
393 	vf->mac_filter_cnt = 0;
394 	vf->vlan_cnt = 0;
395 	bzero(vf->vlans_map, sizeof(vf->vlans_map));
396 
397 	error = ice_iov_configure_mac_anti_spoof(sc, vf);
398 	if (error != 0)
399 		return (error);
400 
401 	error = ice_add_vsi_mac_filter(vsi, broadcastaddr);
402 	if (error != 0)
403 		return (error);
404 	if (!ETHER_IS_ZERO(vf->mac)) {
405 		error = ice_add_vsi_mac_filter(vsi, vf->mac);
406 		if (error != 0)
407 			return (error);
408 	}
409 
410 	return (0);
411 }
412 
413 /**
414  * ice_iov_add_vf - Called by the OS for each VF to create
415  * @sc: device softc structure
416  * @vfnum: index of VF to configure
417  * @params: configuration parameters for the VF
418  *
419  * @returns 0 if successful or an error code on failure
420  */
421 int
422 ice_iov_add_vf(struct ice_softc *sc, uint16_t vfnum, const nvlist_t *params)
423 {
424 	struct ice_tx_queue *txq;
425 	struct ice_rx_queue *rxq;
426 	device_t dev = sc->dev;
427 	struct ice_vsi *vsi;
428 	struct ice_vf *vf;
429 	int vf_num_queues;
430 	const void *mac;
431 	size_t size;
432 	int error;
433 	int i;
434 
435 	vf = ice_iov_get_vf(sc, vfnum);
436 	vf->vf_flags = 0;
437 
438 	/* This VF needs at least one VSI */
439 	vsi = ice_alloc_vsi(sc, ICE_VSI_VF);
440 	if (vsi == NULL)
441 		return (ENOMEM);
442 	vf->vsi = vsi;
443 	vsi->vf_num = vfnum;
444 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_vsi_alloc, error,
445 	    release_vsi);
446 
447 	vf_num_queues = nvlist_get_number(params, "num-queues");
448 	/* Validate and clamp value if invalid */
449 	if (vf_num_queues < 1 || vf_num_queues > ICE_MAX_SCATTERED_QUEUES)
450 		device_printf(dev, "Invalid num-queues (%d) for VF %d\n",
451 		    vf_num_queues, vf->vf_num);
452 	if (vf_num_queues < 1) {
453 		device_printf(dev, "Setting VF %d num-queues to 1\n", vf->vf_num);
454 		vf_num_queues = 1;
455 	} else if (vf_num_queues > ICE_MAX_SCATTERED_QUEUES) {
456 		device_printf(dev, "Setting VF %d num-queues to %d\n",
457 		    vf->vf_num, ICE_MAX_SCATTERED_QUEUES);
458 		vf_num_queues = ICE_MAX_SCATTERED_QUEUES;
459 	}
460 	vsi->qmap_type = ICE_RESMGR_ALLOC_SCATTERED;
461 
462 	/* Reserve VF queue allocation from PF queues */
463 	ice_alloc_vsi_qmap(vsi, vf_num_queues, vf_num_queues);
464 	vsi->num_tx_queues = vsi->num_rx_queues = vf_num_queues;
465 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_queue_maps, error,
466 	    release_vsi);
467 
468 	/* Assign Tx queues from PF space */
469 	error = ice_resmgr_assign_scattered(&sc->tx_qmgr, vsi->tx_qmap,
470 					     vsi->num_tx_queues);
471 	if (error) {
472 		device_printf(sc->dev, "Unable to assign VF Tx queues: %s\n",
473 			      ice_err_str(error));
474 		goto release_vsi;
475 	}
476 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_tx_reservation, error,
477 	    release_vsi);
478 
479 	/* Assign Rx queues from PF space */
480 	error = ice_resmgr_assign_scattered(&sc->rx_qmgr, vsi->rx_qmap,
481 					     vsi->num_rx_queues);
482 	if (error) {
483 		device_printf(sc->dev, "Unable to assign VF Rx queues: %s\n",
484 			      ice_err_str(error));
485 		goto release_vsi;
486 	}
487 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_rx_reservation, error,
488 	    release_vsi);
489 
490 	vsi->max_frame_size = ICE_MAX_FRAME_SIZE;
491 
492 	/* Allocate queue structure memory */
493 	vsi->tx_queues = (struct ice_tx_queue *)
494 	    malloc(sizeof(struct ice_tx_queue) * vsi->num_tx_queues, M_ICE,
495 		   M_NOWAIT | M_ZERO);
496 	if (!vsi->tx_queues) {
497 		device_printf(sc->dev, "VF-%d: Unable to allocate Tx queue memory\n",
498 			      vfnum);
499 		error = ENOMEM;
500 		goto release_vsi;
501 	}
502 	for (i = 0, txq = vsi->tx_queues; i < vsi->num_tx_queues; i++, txq++) {
503 		txq->me = i;
504 		txq->vsi = vsi;
505 	}
506 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_tx_queue_memory, error,
507 	    free_txqs);
508 
509 	/* Allocate queue structure memory */
510 	vsi->rx_queues = (struct ice_rx_queue *)
511 	    malloc(sizeof(struct ice_rx_queue) * vsi->num_rx_queues, M_ICE,
512 		   M_NOWAIT | M_ZERO);
513 	if (!vsi->rx_queues) {
514 		device_printf(sc->dev, "VF-%d: Unable to allocate Rx queue memory\n",
515 			      vfnum);
516 		error = ENOMEM;
517 		goto free_txqs;
518 	}
519 	for (i = 0, rxq = vsi->rx_queues; i < vsi->num_rx_queues; i++, rxq++) {
520 		rxq->me = i;
521 		rxq->vsi = vsi;
522 	}
523 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_rx_queue_memory, error,
524 	    free_rxqs);
525 
526 	/* Allocate space to store the IRQ vector data */
527 	vf->num_irq_vectors = vf_num_queues + 1;
528 	vf->tx_irqvs = (struct ice_irq_vector *)
529 	    malloc(sizeof(struct ice_irq_vector) * (vf->num_irq_vectors),
530 		   M_ICE, M_NOWAIT);
531 	if (!vf->tx_irqvs) {
532 		device_printf(sc->dev,
533 			      "Unable to allocate TX irqv memory for VF-%d's %d vectors\n",
534 			      vfnum, vf->num_irq_vectors);
535 		error = ENOMEM;
536 		goto free_rxqs;
537 	}
538 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_tx_irq_memory, error,
539 	    free_txirqvs);
540 	vf->rx_irqvs = (struct ice_irq_vector *)
541 	    malloc(sizeof(struct ice_irq_vector) * (vf->num_irq_vectors),
542 		   M_ICE, M_NOWAIT);
543 	if (!vf->rx_irqvs) {
544 		device_printf(sc->dev,
545 			      "Unable to allocate RX irqv memory for VF-%d's %d vectors\n",
546 			      vfnum, vf->num_irq_vectors);
547 		error = ENOMEM;
548 		goto free_txirqvs;
549 	}
550 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_rx_irq_memory, error,
551 	    free_rxirqvs);
552 
553 	/* Assign VF interrupts from PF space */
554 	if (!(vf->vf_imap =
555 	      (u16 *)malloc(sizeof(u16) * vf->num_irq_vectors,
556 	      M_ICE, M_NOWAIT))) {
557 		device_printf(dev, "Unable to allocate VF-%d imap memory\n", vfnum);
558 		error = ENOMEM;
559 		goto free_rxirqvs;
560 	}
561 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_imap_memory, error,
562 	    free_imap);
563 	error = ice_resmgr_assign_contiguous(&sc->dev_imgr, vf->vf_imap, vf->num_irq_vectors);
564 	if (error) {
565 		device_printf(dev, "Unable to assign VF-%d interrupt mapping: %s\n",
566 			      vfnum, ice_err_str(error));
567 		goto free_imap;
568 	}
569 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_imap_reservation, error,
570 	    release_imap);
571 
572 	if (nvlist_exists_binary(params, "mac-addr")) {
573 		mac = nvlist_get_binary(params, "mac-addr", &size);
574 		memcpy(vf->mac, mac, ETHER_ADDR_LEN);
575 
576 		if (nvlist_get_bool(params, "allow-set-mac"))
577 			vf->vf_flags |= VF_FLAG_SET_MAC_CAP;
578 	} else
579 		/*
580 		 * If the administrator has not specified a MAC address then
581 		 * we must allow the VF to choose one.
582 		 */
583 		vf->vf_flags |= VF_FLAG_SET_MAC_CAP;
584 
585 	if (nvlist_get_bool(params, "mac-anti-spoof"))
586 		vf->vf_flags |= VF_FLAG_MAC_ANTI_SPOOF;
587 
588 	if (nvlist_get_bool(params, "allow-promisc"))
589 		vf->vf_flags |= VF_FLAG_PROMISC_CAP;
590 
591 	vsi->mirror_src_vsi = nvlist_get_number(params, "mirror-src-vsi");
592 
593 	vf->vlan_limit = nvlist_get_number(params, "max-vlan-allowed");
594 	vf->mac_filter_limit = nvlist_get_number(params, "max-mac-filters");
595 	if (vf->mac_filter_limit != 0) {
596 		vf->mac_filters = mallocarray(vf->mac_filter_limit,
597 		    sizeof(*vf->mac_filters), M_ICE, M_NOWAIT | M_ZERO);
598 		if (vf->mac_filters == NULL) {
599 			device_printf(sc->dev,
600 			    "Unable to allocate VF-%d MAC filter memory\n",
601 			    vfnum);
602 			error = ENOMEM;
603 			goto release_imap;
604 		}
605 	}
606 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_mac_filter_memory, error,
607 	    free_mac_filters);
608 
609 	vf->vf_flags |= VF_FLAG_VLAN_CAP;
610 
611 	/* Create and setup VSI in HW */
612 	error = ice_initialize_vsi(vsi);
613 	if (error) {
614 		device_printf(sc->dev, "Unable to initialize VF %d VSI: %s\n",
615 			      vfnum, ice_err_str(error));
616 		goto free_mac_filters;
617 	}
618 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_vsi_init, error,
619 	    free_mac_filters);
620 	error = ice_iov_configure_mac_anti_spoof(sc, vf);
621 	if (error != 0)
622 		goto free_mac_filters;
623 
624 	/* Add the broadcast address */
625 	error = ice_add_vsi_mac_filter(vsi, broadcastaddr);
626 	if (error) {
627 		device_printf(sc->dev, "Unable to add broadcast filter VF %d VSI: %s\n",
628 			      vfnum, ice_err_str(error));
629 		goto free_mac_filters;
630 	}
631 	ICE_IOV_FAIL_POINT(sc, vfnum, add_after_broadcast_filter, error,
632 	    free_mac_filters);
633 
634 	atomic_set_32(&vf->vf_flags, VF_FLAG_ENABLED);
635 	ice_iov_ready_vf(sc, vf);
636 
637 	return (0);
638 
639 free_mac_filters:
640 	free(vf->mac_filters, M_ICE);
641 	vf->mac_filters = NULL;
642 	vf->mac_filter_cnt = 0;
643 release_imap:
644 	ice_resmgr_release_map(&sc->dev_imgr, vf->vf_imap,
645 			       vf->num_irq_vectors);
646 free_imap:
647 	free(vf->vf_imap, M_ICE);
648 	vf->vf_imap = NULL;
649 free_rxirqvs:
650 	free(vf->rx_irqvs, M_ICE);
651 	vf->rx_irqvs = NULL;
652 free_txirqvs:
653 	free(vf->tx_irqvs, M_ICE);
654 	vf->tx_irqvs = NULL;
655 free_rxqs:
656 	free(vsi->rx_queues, M_ICE);
657 	vsi->rx_queues = NULL;
658 free_txqs:
659 	free(vsi->tx_queues, M_ICE);
660 	vsi->tx_queues = NULL;
661 release_vsi:
662 	if (vsi->hw_vsi_created)
663 		ice_release_vsi(vsi);
664 	else
665 		ice_release_vsi_resources(vsi);
666 	vf->vsi = NULL;
667 	atomic_store_rel_32(&vf->vf_flags, 0);
668 	return (error);
669 }
670 
671 /**
672  * ice_iov_vf_status - report configured VF state
673  * @sc: device private structure
674  * @statusp: returned status snapshot
675  *
676  * The iflib context lock protects VF state and VSI lifetime while this
677  * method constructs the report.
678  */
679 int
680 ice_iov_vf_status(struct ice_softc *sc, struct if_vf_status **statusp)
681 {
682 	struct ice_vf *vf;
683 	struct ice_vsi *vsi;
684 	struct if_vf_extension *extension;
685 	struct if_vf_info *info;
686 	struct if_vf_status *status;
687 	u32 vf_flags;
688 	bool mirror_configured, software_mbx_limit;
689 	uint32_t field, num_fields;
690 	int i;
691 
692 	if (!ice_is_bit_set(sc->feat_en, ICE_FEATURE_SRIOV))
693 		return (EOPNOTSUPP);
694 	status = if_vf_status_alloc(sc->num_vfs);
695 	if (status == NULL)
696 		return (ENOMEM);
697 	for (i = 0; i < sc->num_vfs; i++) {
698 		vf = &sc->vfs[i];
699 		vsi = vf->vsi;
700 		vf_flags = atomic_load_acq_32(&vf->vf_flags);
701 		info = &status->vfs[i];
702 		info->fields = IFVF_F_CONFIGURED | IFVF_F_INITIALIZED |
703 		    IFVF_F_TRAFFIC_ALLOWED | IFVF_F_FAULT_BLOCKED |
704 		    IFVF_F_LINK_STATE_POLICY |
705 		    IFVF_F_VLAN_MODE | IFVF_F_VLAN_COUNT |
706 		    IFVF_F_ALLOW_SET_MAC | IFVF_F_ALLOW_SET_VLAN |
707 		    IFVF_F_MAC_ANTI_SPOOF | IFVF_F_ALLOW_PROMISC;
708 		info->index = i;
709 		info->configured =
710 		    (vf_flags & VF_FLAG_ENABLED) != 0 && vsi != NULL;
711 		info->initialized = info->configured &&
712 		    (vf_flags & VF_FLAG_INITIALIZED) != 0;
713 		info->traffic_allowed = info->configured &&
714 		    (vf_flags & (VF_FLAG_MDD_BLOCKED |
715 		    VF_FLAG_MBX_BLOCKED)) == 0;
716 		info->fault_blocked = (vf_flags & (VF_FLAG_MDD_BLOCKED |
717 		    VF_FLAG_MBX_BLOCKED)) != 0;
718 		info->link_state_policy = IFVF_LINK_AUTO;
719 		if (info->initialized) {
720 			snprintf(info->api_version, sizeof(info->api_version),
721 			    "%u.%u", vf->version.major, vf->version.minor);
722 			info->fields |= IFVF_F_API_VERSION;
723 		}
724 		if (!ETHER_IS_ZERO(vf->mac)) {
725 			memcpy(info->mac, vf->mac, sizeof(info->mac));
726 			info->fields |= IFVF_F_MAC;
727 		}
728 		/* The ICE IOV schema exposes only VF-managed trunk membership. */
729 		info->vlan_mode = IFVF_VLAN_TRUNK;
730 		info->vlan_count = vf->vlan_cnt;
731 		if (info->configured) {
732 			info->vlan_limit = vf->vlan_limit;
733 			info->fields |= IFVF_F_VLAN_LIMIT;
734 		}
735 		if (vsi != NULL) {
736 			info->tx_queue_count = vsi->num_tx_queues;
737 			info->rx_queue_count = vsi->num_rx_queues;
738 			info->fields |= IFVF_F_NUM_TX_QUEUES |
739 			    IFVF_F_NUM_RX_QUEUES;
740 		}
741 
742 		mirror_configured = vsi != NULL && vsi->mirror_src_vsi !=
743 		    ICE_INVALID_MIRROR_VSI;
744 		software_mbx_limit = !ice_is_e830(&sc->hw);
745 		num_fields = ICE_VF_STATUS_NUM_FIELDS -
746 		    (mirror_configured ? 0 : 1) -
747 		    (software_mbx_limit ? 0 : 2) -
748 		    (info->configured ? 0 : 1);
749 		extension = if_vf_status_add_extension(info,
750 		    ICE_VF_STATUS_NAMESPACE, ICE_VF_STATUS_VERSION,
751 		    num_fields);
752 		if (extension == NULL) {
753 			if_vf_status_free(status);
754 			return (ENOMEM);
755 		}
756 		field = ICE_VF_STATUS_FIELD_MIRROR_CONFIGURED;
757 		if_vf_extension_set_bool(extension, field++,
758 		    ICE_VF_STATUS_MIRROR_CONFIGURED, mirror_configured);
759 		if (mirror_configured)
760 			if_vf_extension_set_number(extension, field++,
761 			    ICE_VF_STATUS_MIRROR_SOURCE_VSI,
762 			    vsi->mirror_src_vsi);
763 		if_vf_extension_set_bool(extension, field++,
764 		    ICE_VF_STATUS_MIRROR_INGRESS_ACTIVE,
765 		    vsi != NULL &&
766 		    vsi->rule_mir_ingress != ICE_INVAL_MIRROR_RULE_ID);
767 		if_vf_extension_set_bool(extension, field++,
768 		    ICE_VF_STATUS_MIRROR_EGRESS_ACTIVE,
769 		    vsi != NULL &&
770 		    vsi->rule_mir_egress != ICE_INVAL_MIRROR_RULE_ID);
771 		if_vf_extension_set_bool(extension, field++,
772 		    ICE_VF_STATUS_MDD_BLOCKED,
773 		    (vf_flags & VF_FLAG_MDD_BLOCKED) != 0);
774 		if_vf_extension_set_number(extension, field++,
775 		    ICE_VF_STATUS_MDD_TX_EVENTS, vf->mdd_tx_events);
776 		if_vf_extension_set_number(extension, field++,
777 		    ICE_VF_STATUS_MDD_RX_EVENTS, vf->mdd_rx_events);
778 		if (software_mbx_limit) {
779 			if_vf_extension_set_bool(extension, field++,
780 			    ICE_VF_STATUS_MBX_BLOCKED,
781 			    (vf_flags & VF_FLAG_MBX_BLOCKED) != 0);
782 			if_vf_extension_set_number(extension, field++,
783 			    ICE_VF_STATUS_MBX_OVERFLOW_EVENTS,
784 			    vf->mbx_overflow_events);
785 		}
786 		if_vf_extension_set_number(extension, field++,
787 		    ICE_VF_STATUS_MAC_FILTER_COUNT, vf->mac_filter_cnt);
788 		if (info->configured)
789 			if_vf_extension_set_number(extension, field++,
790 			    ICE_VF_STATUS_MAC_FILTER_LIMIT, vf->mac_filter_limit);
791 		if_vf_extension_set_bool(extension, field++,
792 		    ICE_VF_STATUS_RESET_FAILED,
793 		    (vf_flags & VF_FLAG_RESET_FAILED) != 0);
794 		if_vf_extension_set_bool(extension, field++,
795 		    ICE_VF_STATUS_REBUILD_REQUIRED,
796 		    (vf_flags & VF_FLAG_REBUILD_REQUIRED) != 0);
797 		KASSERT(field == num_fields,
798 		    ("ICE VF status field count %u != %u", field, num_fields));
799 		info->allow_set_mac = (vf_flags & VF_FLAG_SET_MAC_CAP) != 0;
800 		info->allow_set_vlan = (vf_flags & VF_FLAG_VLAN_CAP) != 0;
801 		info->mac_anti_spoof =
802 		    (vf_flags & VF_FLAG_MAC_ANTI_SPOOF) != 0;
803 		info->allow_promisc = (vf_flags & VF_FLAG_PROMISC_CAP) != 0;
804 	}
805 	*statusp = status;
806 	return (0);
807 }
808 
809 /**
810  * ice_iov_uninit - Called by the OS when VFs are destroyed
811  * @sc: device softc structure
812  */
813 void
814 ice_iov_uninit(struct ice_softc *sc)
815 {
816 	struct ice_vf *vf;
817 	struct ice_vsi *vsi;
818 
819 	/* Release per-VF resources */
820 	for (int i = 0; i < sc->num_vfs; i++) {
821 		vf = &sc->vfs[i];
822 		if (!ice_is_e830(&sc->hw))
823 			LIST_DEL(&vf->mbx_info.list_entry);
824 		atomic_store_rel_32(&vf->vf_flags, 0);
825 		vsi = vf->vsi;
826 		free(vf->mac_filters, M_ICE);
827 		vf->mac_filters = NULL;
828 		vf->mac_filter_cnt = 0;
829 
830 		/* Free VF interrupt reservation */
831 		if (vf->vf_imap) {
832 			ice_resmgr_release_map(&sc->dev_imgr, vf->vf_imap,
833 			    vf->num_irq_vectors);
834 			free(vf->vf_imap, M_ICE);
835 			vf->vf_imap = NULL;
836 		}
837 
838 		/* Free queue interrupt mapping trackers */
839 		if (vf->tx_irqvs) {
840 			free(vf->tx_irqvs, M_ICE);
841 			vf->tx_irqvs = NULL;
842 		}
843 		if (vf->rx_irqvs) {
844 			free(vf->rx_irqvs, M_ICE);
845 			vf->rx_irqvs = NULL;
846 		}
847 
848 		if (!vsi)
849 			continue;
850 
851 		/* Free VSI queues */
852 		if (vsi->tx_queues) {
853 			free(vsi->tx_queues, M_ICE);
854 			vsi->tx_queues = NULL;
855 		}
856 		if (vsi->rx_queues) {
857 			free(vsi->rx_queues, M_ICE);
858 			vsi->rx_queues = NULL;
859 		}
860 
861 		if (vsi->hw_vsi_created)
862 			ice_release_vsi(vsi);
863 		else
864 			ice_release_vsi_resources(vsi);
865 		vf->vsi = NULL;
866 	}
867 
868 	/* Release memory used for VF tracking */
869 	if (sc->vfs) {
870 		free(sc->vfs, M_ICE);
871 		sc->vfs = NULL;
872 	}
873 	sc->num_vfs = 0;
874 }
875 
876 /**
877  * ice_iov_handle_vflr - Process VFLR event
878  * @sc: device softc structure
879  *
880  * Identifys which VFs have been reset and re-configure
881  * them.
882  */
883 void
884 ice_iov_handle_vflr(struct ice_softc *sc)
885 {
886 	struct ice_hw *hw = &sc->hw;
887 	struct ice_vf *vf;
888 	u32 reg, reg_idx, bit_idx, vf_flags;
889 
890 	for (int i = 0; i < sc->num_vfs; i++) {
891 		vf = &sc->vfs[i];
892 
893 		reg_idx = (hw->func_caps.vf_base_id + vf->vf_num) / 32;
894 		bit_idx = (hw->func_caps.vf_base_id + vf->vf_num) % 32;
895 		reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
896 		if ((reg & BIT(bit_idx)) == 0)
897 			continue;
898 		vf_flags = atomic_load_acq_32(&vf->vf_flags);
899 		if ((vf_flags & VF_FLAG_ENABLED) != 0 && vf->vsi != NULL) {
900 			if ((vf_flags & VF_FLAG_REBUILD_REQUIRED) != 0) {
901 				/* Consume the event but leave the invalid VF held. */
902 				wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
903 				ice_flush(hw);
904 				continue;
905 			}
906 			ice_reset_vf(sc, vf, false, true);
907 			continue;
908 		}
909 
910 		/* Consume reset events for inactive or incompletely added VFs. */
911 		wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
912 		ice_flush(hw);
913 	}
914 }
915 
916 /**
917  * ice_iov_handle_mdd - Attribute malicious-driver events to VFs
918  * @sc: device softc structure
919  *
920  * Consume every per-VF MDD latch. Block further virtchnl requests and reset a
921  * newly blocked VF without restoring its queues, so even event classes which
922  * only drop the offending packet cannot continue traffic. An optional policy
923  * reconstructs and releases the VF immediately instead.
924  *
925  * @returns a mask of enum ice_mdd_source_bits attributed to configured or
926  * unconfigured VFs of this PF.
927  */
928 u32
929 ice_iov_handle_mdd(struct ice_softc *sc)
930 {
931 	static const struct timeval log_interval = { 2, 0 };
932 	struct ice_hw *hw = &sc->hw;
933 	struct virtchnl_pf_event event = {};
934 	struct ice_vf *vf;
935 	u32 reg, sources, vf_sources, tx_events, rx_events, vf_flags;
936 	bool newly_blocked;
937 	int error;
938 
939 	event.event = VIRTCHNL_EVENT_RESET_IMPENDING;
940 	event.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
941 	vf_sources = 0;
942 	for (int i = 0; i < sc->num_vfs; i++) {
943 		vf = &sc->vfs[i];
944 		sources = 0;
945 		tx_events = 0;
946 		rx_events = 0;
947 
948 		reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_num));
949 		if ((reg & VP_MDET_TX_PQM_VALID_M) != 0) {
950 			wr32(hw, VP_MDET_TX_PQM(vf->vf_num), 0xffff);
951 			sources |= ICE_MDD_TX_PQM;
952 			tx_events++;
953 		}
954 		reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_num));
955 		if ((reg & VP_MDET_TX_TCLAN_VALID_M) != 0) {
956 			wr32(hw, VP_MDET_TX_TCLAN(vf->vf_num), 0xffff);
957 			sources |= ICE_MDD_TX_TCLAN;
958 			tx_events++;
959 		}
960 		reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_num));
961 		if ((reg & VP_MDET_TX_TDPU_VALID_M) != 0) {
962 			wr32(hw, VP_MDET_TX_TDPU(vf->vf_num), 0xffff);
963 			sources |= ICE_MDD_TX_TDPU;
964 			tx_events++;
965 		}
966 		reg = rd32(hw, VP_MDET_RX(vf->vf_num));
967 		if ((reg & VP_MDET_RX_VALID_M) != 0) {
968 			wr32(hw, VP_MDET_RX(vf->vf_num), 0xffff);
969 			sources |= ICE_MDD_RX;
970 			rx_events++;
971 		}
972 		if (tx_events == 0 && rx_events == 0)
973 			continue;
974 		vf_sources |= sources;
975 
976 		vf_flags = atomic_load_acq_32(&vf->vf_flags);
977 		if ((vf_flags & VF_FLAG_ENABLED) == 0 || vf->vsi == NULL)
978 			continue;
979 		vf->mdd_tx_events += tx_events;
980 		vf->mdd_rx_events += rx_events;
981 		newly_blocked = (vf_flags & VF_FLAG_MDD_BLOCKED) == 0;
982 		atomic_set_32(&vf->vf_flags, VF_FLAG_MDD_BLOCKED);
983 
984 		if (ratecheck(&vf->last_mdd_log, &log_interval)) {
985 			device_printf(sc->dev,
986 			    "malicious-driver event from VF-%d "
987 			    "(tx %ju, rx %ju); %s\n", vf->vf_num,
988 			    (uintmax_t)vf->mdd_tx_events,
989 			    (uintmax_t)vf->mdd_rx_events,
990 			    sc->mdd_auto_reset_vf && newly_blocked ?
991 			    "resetting VF" : "VF remains blocked");
992 		}
993 		if (!newly_blocked)
994 			continue;
995 
996 		/* Ignore notification failure; reset does not require VF help. */
997 		if (sc->mdd_auto_reset_vf &&
998 		    (vf_flags & VF_FLAG_INITIALIZED) != 0 &&
999 		    ice_check_sq_alive(hw, &hw->mailboxq)) {
1000 			(void)ice_aq_send_msg_to_vf(hw, vf->vf_num,
1001 			    VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1002 			    (u8 *)&event, sizeof(event), NULL);
1003 		}
1004 		/*
1005 		 * TDPU MDD drops only the offending packet. Reset the entire VF so
1006 		 * the software blocked state always means that traffic is actually
1007 		 * fenced. The opt-in policy reconstructs its queues immediately.
1008 		 */
1009 		error = ice_reset_vf(sc, vf, true, sc->mdd_auto_reset_vf);
1010 		if (error != 0) {
1011 			device_printf(sc->dev,
1012 			    "failed to quiesce MDD-blocked VF-%d: %d\n",
1013 			    vf->vf_num, error);
1014 		} else if (!sc->mdd_auto_reset_vf) {
1015 			/*
1016 			 * Complete VFR without restoring queues. This leaves the VF
1017 			 * inactive and DMA-fenced, but permits a later physical FLR to
1018 			 * create a new reset edge and recover it.
1019 			 */
1020 			ice_iov_complete_vf_reset(sc, vf, false);
1021 		}
1022 	}
1023 	ice_flush(hw);
1024 	return (vf_sources);
1025 }
1026 
1027 /**
1028  * ice_iov_notify_vfs_reset - Notify initialized VFs of an impending reset
1029  * @sc: device softc structure
1030  *
1031  * Give VF drivers advance notice while the mailbox control queue is still
1032  * alive. Ignore individual send failures so one VF cannot prevent the PF from
1033  * notifying its siblings or proceeding with the reset.
1034  */
1035 void
1036 ice_iov_notify_vfs_reset(struct ice_softc *sc)
1037 {
1038 	struct virtchnl_pf_event event = {};
1039 	struct ice_hw *hw = &sc->hw;
1040 	struct ice_vf *vf;
1041 
1042 	if (!ice_check_sq_alive(hw, &hw->mailboxq))
1043 		return;
1044 
1045 	event.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1046 	event.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1047 	for (int i = 0; i < sc->num_vfs; i++) {
1048 		vf = &sc->vfs[i];
1049 		if ((atomic_load_acq_32(&vf->vf_flags) &
1050 		    VF_FLAG_INITIALIZED) == 0)
1051 			continue;
1052 		(void)ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_EVENT,
1053 		    VIRTCHNL_STATUS_SUCCESS, (u8 *)&event, sizeof(event), NULL);
1054 	}
1055 }
1056 
1057 /**
1058  * ice_iov_clear_vf_queue_state - Clear tracked VF queue state
1059  * @vf: driver's VF structure for the VF to update
1060  */
1061 static void
1062 ice_iov_clear_vf_queue_state(struct ice_vf *vf)
1063 {
1064 	vf->txq_configured = 0;
1065 	vf->rxq_configured = 0;
1066 	vf->rxq_enabled = 0;
1067 }
1068 
1069 /**
1070  * ice_iov_clear_vf_mdd - Clear hardware MDD latches for a reset VF
1071  * @sc: device softc structure
1072  * @vf: driver's VF structure for the VF to update
1073  *
1074  * Function reset can generate a spurious anti-spoof MDD indication. Consume
1075  * all per-VF latches before releasing reset so it cannot re-block a VF which
1076  * has just been reconstructed successfully.
1077  */
1078 static void
1079 ice_iov_clear_vf_mdd(struct ice_softc *sc, struct ice_vf *vf)
1080 {
1081 	struct ice_hw *hw = &sc->hw;
1082 
1083 	wr32(hw, VP_MDET_TX_PQM(vf->vf_num), 0xffff);
1084 	wr32(hw, VP_MDET_TX_TCLAN(vf->vf_num), 0xffff);
1085 	wr32(hw, VP_MDET_TX_TDPU(vf->vf_num), 0xffff);
1086 	wr32(hw, VP_MDET_RX(vf->vf_num), 0xffff);
1087 	ice_flush(hw);
1088 }
1089 
1090 /**
1091  * ice_iov_complete_vf_reset - Complete a VF reset
1092  * @sc: device softc structure
1093  * @vf: driver's VF structure for the VF to update
1094  * @restore_mapping: restore the VF queue and interrupt mappings
1095  *
1096  * Clear VFSWR after the hardware drain, optionally restore the VF mappings,
1097  * and then publish VFACTIVE. The mapping registers do not retain writes made
1098  * while VFSWR remains asserted. Callers may instead leave a software-blocked
1099  * VF with no queue or interrupt mappings.
1100  */
1101 static void
1102 ice_iov_complete_vf_reset(struct ice_softc *sc, struct ice_vf *vf,
1103     bool restore_mapping)
1104 {
1105 	struct ice_hw *hw = &sc->hw;
1106 	u32 reg;
1107 
1108 	reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_num));
1109 	reg &= ~VPGEN_VFRTRIG_VFSWR_M;
1110 	wr32(hw, VPGEN_VFRTRIG(vf->vf_num), reg);
1111 	if (restore_mapping)
1112 		ice_iov_setup_intr_mapping(sc, vf);
1113 	wr32(hw, VFGEN_RSTAT(vf->vf_num), VIRTCHNL_VFR_VFACTIVE);
1114 	ice_flush(hw);
1115 }
1116 
1117 /**
1118  * ice_iov_ready_vf - Setup VF interrupts and mark it as ready
1119  * @sc: device softc structure
1120  * @vf: driver's VF structure for the VF to update
1121  *
1122  * Clears VF reset triggering bit, sets up the PF<->VF interrupt
1123  * mapping and marks the VF as active in the HW so that the VF
1124  * driver can use it.
1125  */
1126 static void
1127 ice_iov_ready_vf(struct ice_softc *sc, struct ice_vf *vf)
1128 {
1129 	/* A VF or PF reset discards all queue configuration and state. */
1130 	ice_iov_clear_vf_queue_state(vf);
1131 	ice_iov_clear_vf_mdd(sc, vf);
1132 	atomic_clear_32(&vf->vf_flags, VF_FLAG_MDD_BLOCKED);
1133 	ice_iov_clear_vf_mbx(sc, vf);
1134 
1135 	ice_iov_complete_vf_reset(sc, vf, true);
1136 }
1137 
1138 /**
1139  * ice_iov_clear_vf_mbx - Release mailbox isolation after a completed reset
1140  * @sc: device softc structure
1141  * @vf: VF whose mailbox state should be cleared
1142  */
1143 static void
1144 ice_iov_clear_vf_mbx(struct ice_softc *sc, struct ice_vf *vf)
1145 {
1146 	if (ice_is_e830(&sc->hw))
1147 		ice_mbx_vf_clear_cnt_e830(&sc->hw, vf->vf_num);
1148 	else
1149 		ice_mbx_clear_malvf(&vf->mbx_info);
1150 	atomic_clear_32(&vf->vf_flags, VF_FLAG_MBX_BLOCKED);
1151 }
1152 
1153 /**
1154  * ice_iov_rebuild_vf - Rebuild a VF VSI after a PF or device reset
1155  * @sc: device softc structure
1156  * @vsi: VF VSI to rebuild
1157  *
1158  * PF and device resets discard the hardware VSI and interrupt state for every
1159  * VF. Re-add the VSI and replay its configuration before reporting the VF as
1160  * active. A failed rebuild leaves the VF inactive while allowing the PF and
1161  * other VFs to recover.
1162  */
1163 int
1164 ice_iov_rebuild_vf(struct ice_softc *sc, struct ice_vsi *vsi)
1165 {
1166 	struct ice_eth_stats accumulated_stats;
1167 	struct ice_hw *hw = &sc->hw;
1168 	struct ice_vf *vf;
1169 	int error, status;
1170 
1171 	MPASS(vsi->type == ICE_VSI_VF);
1172 	vf = ice_iov_get_vf(sc, vsi->vf_num);
1173 	atomic_clear_32(&vf->vf_flags, VF_FLAG_INITIALIZED);
1174 	atomic_set_32(&vf->vf_flags, VF_FLAG_REBUILD_REQUIRED);
1175 	ice_iov_clear_vf_queue_state(vf);
1176 	ICE_IOV_FAIL_POINT(sc, vf->vf_num, rebuild_before_initialize, error,
1177 	    fail);
1178 
1179 	/* A new hardware VSI starts a new raw statistics epoch. */
1180 	accumulated_stats = vsi->hw_stats.cur;
1181 	error = ice_initialize_vsi(vsi);
1182 	if (error != 0) {
1183 		device_printf(sc->dev,
1184 		    "Unable to re-initialize VF %d VSI, err %s\n",
1185 		    vf->vf_num, ice_err_str(error));
1186 		return (error);
1187 	}
1188 	vsi->hw_stats.cur = accumulated_stats;
1189 	error = ice_iov_configure_mac_anti_spoof(sc, vf);
1190 	if (error != 0)
1191 		return (error);
1192 
1193 	status = ice_replay_vsi(hw, vsi->idx);
1194 	if (status != 0) {
1195 		device_printf(sc->dev,
1196 		    "Failed to replay VF %d VSI, err %s aq_err %s\n",
1197 		    vf->vf_num, ice_status_str(status),
1198 		    ice_aq_str(hw->adminq.sq_last_status));
1199 		return (EIO);
1200 	}
1201 
1202 	atomic_clear_32(&vf->vf_flags,
1203 	    VF_FLAG_REBUILD_REQUIRED | VF_FLAG_RESET_FAILED);
1204 	ice_iov_ready_vf(sc, vf);
1205 	return (0);
1206 
1207 #ifdef DRIVER_FAILPOINTS
1208 fail:
1209 	return (error);
1210 #endif /* DRIVER_FAILPOINTS */
1211 }
1212 
1213 /**
1214  * ice_reset_vf - Perform a hardware reset (VFR) on a VF
1215  * @sc: device softc structure
1216  * @vf: driver's VF structure for VF to be reset
1217  * @trigger_reset: trigger a reset or only handle an already executed reset
1218  * @release_vf: publish VFACTIVE after reset; otherwise leave the VF held
1219  *
1220  * Performs a VFR for the given VF. This function busy waits until the reset
1221  * completes in the HW and publishes VFACTIVE only after every mandatory
1222  * reset stage succeeds. In quiesce mode, it returns with VFSWR asserted and
1223  * without restoring interrupt mappings or publishing VFACTIVE.
1224  *
1225  * @remark Release mode also sets up the PF<->VF interrupt mapping and
1226  * allocations in the hardware after the hardware reset is finished, via
1227  * ice_iov_setup_intr_mapping()
1228  */
1229 static int
1230 ice_reset_vf(struct ice_softc *sc, struct ice_vf *vf, bool trigger_reset,
1231     bool release_vf)
1232 {
1233 	u16 global_vf_num, reg_idx, bit_idx;
1234 	struct ice_hw *hw = &sc->hw;
1235 	bool reset_done;
1236 	int error, status;
1237 	u32 reg;
1238 	int bit, i;
1239 
1240 	/* A VFR cannot recover PF-owned VSI state lost during PF rebuild. */
1241 	if (release_vf && (atomic_load_acq_32(&vf->vf_flags) &
1242 	    VF_FLAG_REBUILD_REQUIRED) != 0)
1243 		return (EIO);
1244 
1245 	global_vf_num = vf->vf_num + hw->func_caps.vf_base_id;
1246 	atomic_clear_32(&vf->vf_flags, VF_FLAG_INITIALIZED);
1247 	error = 0;
1248 
1249 	if (trigger_reset) {
1250 		reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_num));
1251 		reg |= VPGEN_VFRTRIG_VFSWR_M;
1252 		wr32(hw, VPGEN_VFRTRIG(vf->vf_num), reg);
1253 		ice_flush(hw);
1254 	}
1255 
1256 	/*
1257 	 * Remove the tracked queue leaves from the software scheduler before
1258 	 * issuing the reset-only AQ command.  That command drains hardware but
1259 	 * does not update the shared scheduler database.  Retain unresolved queue
1260 	 * state if cleanup fails so a later reset can retry it.
1261 	 */
1262 	status = ice_vc_disable_queues(sc, vf, vf->txq_configured,
1263 	    vf->rxq_enabled);
1264 	if (status == 0)
1265 		ice_iov_clear_vf_queue_state(vf);
1266 	else if (error == 0)
1267 		error = status;
1268 
1269 	/* This zero-queue command is required to complete every VF reset. */
1270 	status = ice_dis_vsi_txq(hw->port_info, vf->vsi->idx, 0, 0,
1271 	    NULL, NULL, NULL, ICE_VF_RESET, vf->vf_num, NULL);
1272 	ICE_FAIL_POINT_CODE_COND(sc, _debug_fail_point_ice_iov,
1273 	    vf_reset_tx_disable, ice_iov_fail_vf_matches(vf->vf_num),
1274 	    FAIL_POINT_NONSLEEPABLE, {
1275 		status = ICE_ERR_AQ_ERROR;
1276 	});
1277 	if (status) {
1278 		device_printf(sc->dev,
1279 		    "%s: Failed to disable LAN Tx queues: err %s aq_err %s\n",
1280 		    __func__, ice_status_str(status),
1281 		    ice_aq_str(hw->adminq.sq_last_status));
1282 		if (error == 0)
1283 			error = EIO;
1284 	}
1285 
1286 	/* Then check for the VF reset to finish in HW. */
1287 	reset_done = false;
1288 	for (i = 0; i < ICE_VPGEN_VFRSTAT_WAIT_COUNT; i++) {
1289 		reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_num));
1290 		if ((reg & VPGEN_VFRSTAT_VFRD_M)) {
1291 			reset_done = true;
1292 			break;
1293 		}
1294 
1295 		DELAY(ICE_VPGEN_VFRSTAT_WAIT_DELAY_US);
1296 	}
1297 	ICE_FAIL_POINT_CODE_COND(sc, _debug_fail_point_ice_iov,
1298 	    vf_reset_vfr_timeout, ice_iov_fail_vf_matches(vf->vf_num),
1299 	    FAIL_POINT_NONSLEEPABLE, {
1300 		reset_done = false;
1301 	});
1302 	if (!reset_done) {
1303 		device_printf(sc->dev,
1304 			"VF-%d Reset is stuck\n", vf->vf_num);
1305 		if (error == 0)
1306 			error = ETIMEDOUT;
1307 	} else {
1308 		/* VFLR status is W1C only after the hardware drain completes. */
1309 		reg_idx = global_vf_num / 32;
1310 		bit_idx = global_vf_num % 32;
1311 		wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1312 		ice_flush(hw);
1313 
1314 		/* Hardware resets Tx queues; the PF must disable every Rx. */
1315 		for (bit = 0; bit < vf->vsi->num_rx_queues; bit++) {
1316 			status = ice_control_rx_queue(vf->vsi, bit, false);
1317 			ICE_FAIL_POINT_CODE_COND(sc,
1318 			    _debug_fail_point_ice_iov, vf_reset_rx_disable,
1319 			    ice_iov_fail_vf_matches(vf->vf_num),
1320 			    FAIL_POINT_NONSLEEPABLE, {
1321 				status = EIO;
1322 			});
1323 			if (status != 0) {
1324 				device_printf(sc->dev,
1325 				    "Unable to disable VF-%d Rx queue %d: %s\n",
1326 				    vf->vf_num, bit, ice_err_str(status));
1327 				if (error == 0)
1328 					error = status;
1329 			}
1330 		}
1331 	}
1332 
1333 	/* Verify that post-drain cleanup left no outstanding DMA. */
1334 	wr32(hw, PF_PCI_CIAA,
1335 	    ICE_PCIE_DEV_STATUS | (global_vf_num << PF_PCI_CIAA_VF_NUM_S));
1336 	for (i = 0; i < ICE_PCI_CIAD_WAIT_COUNT; i++) {
1337 		reg = rd32(hw, PF_PCI_CIAD);
1338 		if (!(reg & PCIEM_STA_TRANSACTION_PND))
1339 			break;
1340 		DELAY(ICE_PCI_CIAD_WAIT_DELAY_US);
1341 	}
1342 	ICE_FAIL_POINT_CODE_COND(sc, _debug_fail_point_ice_iov,
1343 	    vf_reset_pcie_pending, ice_iov_fail_vf_matches(vf->vf_num),
1344 	    FAIL_POINT_NONSLEEPABLE, {
1345 		i = ICE_PCI_CIAD_WAIT_COUNT;
1346 	});
1347 	if (i == ICE_PCI_CIAD_WAIT_COUNT) {
1348 		device_printf(sc->dev,
1349 		    "VF-%d PCI transactions remain after reset\n", vf->vf_num);
1350 		if (error == 0)
1351 			error = ETIMEDOUT;
1352 	}
1353 
1354 	if (error != 0) {
1355 		atomic_set_32(&vf->vf_flags, VF_FLAG_RESET_FAILED);
1356 		return (error);
1357 	}
1358 
1359 	if (!release_vf) {
1360 		/* Discard any anti-spoof MDD indication caused by the reset. */
1361 		ice_iov_clear_vf_mdd(sc, vf);
1362 		atomic_clear_32(&vf->vf_flags, VF_FLAG_RESET_FAILED);
1363 		return (0);
1364 	}
1365 
1366 	error = ice_iov_restore_vf_host_config(sc, vf);
1367 	if (error != 0) {
1368 		atomic_set_32(&vf->vf_flags, VF_FLAG_RESET_FAILED);
1369 		return (error);
1370 	}
1371 
1372 	atomic_clear_32(&vf->vf_flags, VF_FLAG_RESET_FAILED);
1373 	ice_iov_ready_vf(sc, vf);
1374 	return (0);
1375 }
1376 
1377 /**
1378  * ice_iov_quiesce_vfs_for_reset - Hold configured VFs before device reset
1379  * @sc: device softc structure
1380  *
1381  * Gate VF master accesses, drain each VF data path, and leave VFSWR asserted.
1382  * Process VFs serially to remain below the E810 limit of four concurrent
1383  * VM/VF reset flows. A successful VSI rebuild releases each VF individually.
1384  */
1385 int
1386 ice_iov_quiesce_vfs_for_reset(struct ice_softc *sc)
1387 {
1388 	struct virtchnl_pf_event event = {};
1389 	struct ice_hw *hw = &sc->hw;
1390 	struct ice_vf *vf;
1391 	int error, first_error;
1392 	u32 reg, vf_flags;
1393 	bool notify;
1394 
1395 	notify = ice_check_sq_alive(hw, &hw->mailboxq);
1396 	event.event = VIRTCHNL_EVENT_RESET_IMPENDING;
1397 	event.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
1398 
1399 	/* Notify and then gate each VF before it can release its buffers. */
1400 	for (int i = 0; i < sc->num_vfs; i++) {
1401 		vf = &sc->vfs[i];
1402 		vf_flags = atomic_load_acq_32(&vf->vf_flags);
1403 		if ((vf_flags & VF_FLAG_ENABLED) == 0 || vf->vsi == NULL)
1404 			continue;
1405 		if (notify && (vf_flags & VF_FLAG_INITIALIZED) != 0)
1406 			ice_aq_send_msg_to_vf(hw, vf->vf_num,
1407 			    VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
1408 			    (u8 *)&event, sizeof(event), NULL);
1409 
1410 		/* Block mailbox reconfiguration before asserting reset. */
1411 		atomic_clear_32(&vf->vf_flags, VF_FLAG_INITIALIZED);
1412 		atomic_set_32(&vf->vf_flags, VF_FLAG_REBUILD_REQUIRED);
1413 		reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_num));
1414 		reg |= VPGEN_VFRTRIG_VFSWR_M;
1415 		wr32(hw, VPGEN_VFRTRIG(vf->vf_num), reg);
1416 	}
1417 	ice_flush(hw);
1418 
1419 	/* Firmware data-path drains remain serialized below its limit. */
1420 	first_error = 0;
1421 	for (int i = 0; i < sc->num_vfs; i++) {
1422 		vf = &sc->vfs[i];
1423 		vf_flags = atomic_load_acq_32(&vf->vf_flags);
1424 		if ((vf_flags & VF_FLAG_ENABLED) == 0 || vf->vsi == NULL)
1425 			continue;
1426 		error = ice_reset_vf(sc, vf, false, false);
1427 		if (error != 0) {
1428 			device_printf(sc->dev,
1429 			    "Failed to quiesce VF-%d for device reset: %d\n",
1430 			    vf->vf_num, error);
1431 			if (first_error == 0)
1432 				first_error = error;
1433 		}
1434 	}
1435 
1436 	return (first_error);
1437 }
1438 
1439 /**
1440  * ice_vc_get_vf_res_msg - Handle VIRTCHNL_OP_GET_VF_RESOURCES msg from VF
1441  * @sc: device private structure
1442  * @vf: VF tracking structure
1443  * @msg_buf: raw message buffer from the VF
1444  *
1445  * Receives a message from the VF listing its supported capabilities, and
1446  * replies to the VF with information about what resources the PF has
1447  * allocated for the VF.
1448  *
1449  * @remark This always replies to the VF with a success status; it does not
1450  * fail. It's up to the VF driver to reject or complain about the PF's response.
1451  */
1452 static void
1453 ice_vc_get_vf_res_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
1454 {
1455 	struct ice_hw *hw = &sc->hw;
1456 	struct virtchnl_vf_resource *vf_res;
1457 	struct virtchnl_vsi_resource *vsi_res;
1458 	u16 vf_res_len;
1459 	u32 vf_caps;
1460 	int status;
1461 
1462 	/* XXX: Only support one VSI per VF, so this size doesn't need adjusting */
1463 	vf_res_len = sizeof(struct virtchnl_vf_resource);
1464 	vf_res = (struct virtchnl_vf_resource *)malloc(vf_res_len, M_ICE,
1465 	    M_WAITOK | M_ZERO);
1466 
1467 	vf_res->num_vsis = 1;
1468 	vf_res->num_queue_pairs = vf->vsi->num_tx_queues;
1469 	vf_res->max_vectors = vf_res->num_queue_pairs + 1;
1470 
1471 	vf_res->rss_key_size = ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE;
1472 	vf_res->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
1473 	vf_res->max_mtu = ICE_MAX_FRAME_SIZE;
1474 
1475 	vf_res->vf_cap_flags = VF_BASE_MODE_OFFLOADS;
1476 	if (msg_buf != NULL) {
1477 		vf_caps = *((u32 *)(msg_buf));
1478 
1479 		if (vf_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
1480 			vf_res->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
1481 
1482 		if (vf_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1483                         vf_res->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1484 	}
1485 
1486 	vsi_res = &vf_res->vsi_res[0];
1487 	vsi_res->vsi_id = vf->vsi->idx;
1488 	vsi_res->num_queue_pairs = vf->vsi->num_tx_queues;
1489 	vsi_res->vsi_type = VIRTCHNL_VSI_SRIOV;
1490 	vsi_res->qset_handle = 0;
1491 	if (!ETHER_IS_ZERO(vf->mac))
1492 		memcpy(vsi_res->default_mac_addr, vf->mac, ETHER_ADDR_LEN);
1493 
1494 	status = ice_aq_send_msg_to_vf(hw, vf->vf_num,
1495 	    VIRTCHNL_OP_GET_VF_RESOURCES, VIRTCHNL_STATUS_SUCCESS,
1496 	    (u8 *)vf_res, vf_res_len, NULL);
1497 	if (status == 0)
1498 		atomic_set_32(&vf->vf_flags, VF_FLAG_INITIALIZED);
1499 	else
1500 		device_printf(sc->dev,
1501 		    "Unable to send VF-%u resource response, err %s\n",
1502 		    vf->vf_num, ice_status_str(status));
1503 
1504 	free(vf_res, M_ICE);
1505 }
1506 
1507 /**
1508  * ice_vc_version_msg - Handle VIRTCHNL_OP_VERSION msg from VF
1509  * @sc: device private structure
1510  * @vf: VF tracking structure
1511  * @msg_buf: raw message buffer from the VF
1512  *
1513  * Receives a version message from the VF, and responds to the VF with
1514  * the version number that the PF will use.
1515  *
1516  * @remark This always replies to the VF with a success status; it does not
1517  * fail.
1518  */
1519 static void
1520 ice_vc_version_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
1521 {
1522 	struct virtchnl_version_info *recv_vf_version;
1523 	struct ice_hw *hw = &sc->hw;
1524 	device_t dev = sc->dev;
1525 
1526 	recv_vf_version = (struct virtchnl_version_info *)msg_buf;
1527 
1528 	/* VFs running the 1.0 API expect to get 1.0 back */
1529 	if (VF_IS_V10(recv_vf_version)) {
1530 		vf->version.major = 1;
1531 		vf->version.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1532 	} else {
1533 		vf->version.major = VIRTCHNL_VERSION_MAJOR;
1534 		vf->version.minor = VIRTCHNL_VERSION_MINOR;
1535 
1536 		if ((recv_vf_version->major != VIRTCHNL_VERSION_MAJOR) ||
1537 		    (recv_vf_version->minor != VIRTCHNL_VERSION_MINOR))
1538 		    device_printf(dev,
1539 		        "%s: VF-%d requested version (%d.%d) differs from PF version (%d.%d)\n",
1540 			__func__, vf->vf_num,
1541 			recv_vf_version->major, recv_vf_version->minor,
1542 			VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR);
1543 	}
1544 
1545 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_VERSION,
1546 	    VIRTCHNL_STATUS_SUCCESS, (u8 *)&vf->version, sizeof(vf->version),
1547 	    NULL);
1548 }
1549 
1550 /**
1551  * ice_vf_validate_mac - Validate MAC address before adding it
1552  * @vf: VF tracking structure
1553  * @addr: MAC address to validate
1554  *
1555  * Validate a MAC address before adding it to a VF during the handling
1556  * of a VIRTCHNL_OP_ADD_ETH_ADDR operation. Notably, this also checks if
1557  * the VF is allowed to set its own arbitrary MAC addresses.
1558  *
1559  * Returns 0 if MAC address is valid for the given vf
1560  */
1561 static int
1562 ice_vf_validate_mac(struct ice_vf *vf, const uint8_t *addr)
1563 {
1564 
1565 	if (ETHER_IS_ZERO(addr) || ETHER_IS_BROADCAST(addr))
1566 		return (EINVAL);
1567 
1568 	/*
1569 	 * If the VF is not allowed to change its MAC address, don't let it
1570 	 * set a MAC filter for an address that is not a multicast address and
1571 	 * is not its assigned MAC.
1572 	 */
1573 	if (!(vf->vf_flags & VF_FLAG_SET_MAC_CAP) &&
1574 	    !(ETHER_IS_MULTICAST(addr) || !bcmp(addr, vf->mac, ETHER_ADDR_LEN)))
1575 		return (EPERM);
1576 
1577 	return (0);
1578 }
1579 
1580 /**
1581  * ice_vf_mac_filter_index - Find a VF-owned MAC filter
1582  * @vf: VF tracking structure
1583  * @addr: MAC address to find
1584  *
1585  * The administrator-assigned address does not consume the configurable VF
1586  * filter quota and is therefore not stored in this array.
1587  */
1588 static int
1589 ice_vf_mac_filter_index(struct ice_vf *vf, const uint8_t *addr)
1590 {
1591 
1592 	for (u16 i = 0; i < vf->mac_filter_cnt; i++) {
1593 		if (memcmp(vf->mac_filters[i].addr, addr, ETHER_ADDR_LEN) == 0)
1594 			return (i);
1595 	}
1596 	return (-1);
1597 }
1598 
1599 /**
1600  * ice_vc_add_eth_addr_msg - Handle VIRTCHNL_OP_ADD_ETH_ADDR msg from VF
1601  * @sc: device private structure
1602  * @vf: VF tracking structure
1603  * @msg_buf: raw message buffer from the VF
1604  *
1605  * Receives a list of MAC addresses from the VF and adds those addresses
1606  * to the VSI's filter list.
1607  */
1608 static void
1609 ice_vc_add_eth_addr_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
1610 {
1611 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
1612 	struct virtchnl_ether_addr_list *addr_list;
1613 	struct ice_hw *hw = &sc->hw;
1614 	u16 new_filters;
1615 	int error = 0;
1616 
1617 	addr_list = (struct virtchnl_ether_addr_list *)msg_buf;
1618 
1619 	/* Validate the entire batch and charge only unique, absent filters. */
1620 	new_filters = 0;
1621 	for (int i = 0; i < addr_list->num_elements; i++) {
1622 		u8 *addr = addr_list->list[i].addr;
1623 		int j;
1624 
1625 		error = ice_vf_validate_mac(vf, addr);
1626 		if (error != 0) {
1627 			device_printf(sc->dev,
1628 			    "%s: VF-%d: invalid or unauthorized MAC for VSI %d\n",
1629 			    __func__, vf->vf_num, vf->vsi->idx);
1630 			v_status = VIRTCHNL_STATUS_ERR_PARAM;
1631 			goto done;
1632 		}
1633 		for (j = 0; j < i; j++) {
1634 			if (memcmp(addr_list->list[j].addr, addr,
1635 			    ETHER_ADDR_LEN) == 0)
1636 				break;
1637 		}
1638 		if (j != i || memcmp(addr, vf->mac, ETHER_ADDR_LEN) == 0 ||
1639 		    ice_vf_mac_filter_index(vf, addr) >= 0)
1640 			continue;
1641 		new_filters++;
1642 	}
1643 	if ((u32)vf->mac_filter_cnt + new_filters > vf->mac_filter_limit) {
1644 		v_status = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1645 		goto done;
1646 	}
1647 
1648 	for (int i = 0; i < addr_list->num_elements; i++) {
1649 		u8 *addr = addr_list->list[i].addr;
1650 		bool assigned;
1651 
1652 		/* The type flag is currently ignored; every MAC address is
1653 		 * treated as the LEGACY type
1654 		 */
1655 		assigned = memcmp(addr, vf->mac, ETHER_ADDR_LEN) == 0;
1656 		if (!assigned && ice_vf_mac_filter_index(vf, addr) >= 0)
1657 			continue;
1658 
1659 		error = ice_add_vsi_mac_filter(vf->vsi, addr);
1660 		if (error) {
1661 			device_printf(sc->dev,
1662 			    "%s: VF-%d: Error adding MAC addr for VSI %d\n",
1663 			    __func__, vf->vf_num, vf->vsi->idx);
1664 			v_status = VIRTCHNL_STATUS_ERR_PARAM;
1665 			continue;
1666 		}
1667 		if (!assigned) {
1668 			MPASS(vf->mac_filter_cnt < vf->mac_filter_limit);
1669 			memcpy(vf->mac_filters[vf->mac_filter_cnt].addr, addr,
1670 			    ETHER_ADDR_LEN);
1671 			vf->mac_filter_cnt++;
1672 		}
1673 	}
1674 
1675 done:
1676 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_ADD_ETH_ADDR,
1677 	    v_status, NULL, 0, NULL);
1678 }
1679 
1680 /**
1681  * ice_vc_del_eth_addr_msg - Handle VIRTCHNL_OP_DEL_ETH_ADDR msg from VF
1682  * @sc: device private structure
1683  * @vf: VF tracking structure
1684  * @msg_buf: raw message buffer from the VF
1685  *
1686  * Receives a list of MAC addresses from the VF and removes those addresses
1687  * from the VSI's filter list.
1688  */
1689 static void
1690 ice_vc_del_eth_addr_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
1691 {
1692 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
1693 	struct virtchnl_ether_addr_list *addr_list;
1694 	struct ice_hw *hw = &sc->hw;
1695 	int error = 0;
1696 
1697 	addr_list = (struct virtchnl_ether_addr_list *)msg_buf;
1698 
1699 	for (int i = 0; i < addr_list->num_elements; i++) {
1700 		u8 *addr = addr_list->list[i].addr;
1701 		bool assigned;
1702 		int index;
1703 
1704 		error = ice_vf_validate_mac(vf, addr);
1705 		if (error != 0) {
1706 			v_status = VIRTCHNL_STATUS_ERR_PARAM;
1707 			continue;
1708 		}
1709 		assigned = memcmp(addr, vf->mac, ETHER_ADDR_LEN) == 0;
1710 		if (assigned &&
1711 		    (vf->vf_flags & VF_FLAG_SET_MAC_CAP) == 0)
1712 			continue;
1713 		index = assigned ? -1 : ice_vf_mac_filter_index(vf, addr);
1714 		if (!assigned && index < 0)
1715 			continue;
1716 
1717 		error = ice_remove_vsi_mac_filter(vf->vsi, addr);
1718 		if (error) {
1719 			device_printf(sc->dev,
1720 			    "%s: VF-%d: Error removing MAC addr for VSI %d\n",
1721 			    __func__, vf->vf_num, vf->vsi->idx);
1722 			v_status = VIRTCHNL_STATUS_ERR_PARAM;
1723 			continue;
1724 		}
1725 		if (!assigned) {
1726 			if (index + 1 < vf->mac_filter_cnt) {
1727 				memmove(&vf->mac_filters[index],
1728 				    &vf->mac_filters[index + 1],
1729 				    (vf->mac_filter_cnt - index - 1) *
1730 				    sizeof(*vf->mac_filters));
1731 			}
1732 			vf->mac_filter_cnt--;
1733 		}
1734 	}
1735 
1736 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_DEL_ETH_ADDR,
1737 	    v_status, NULL, 0, NULL);
1738 }
1739 
1740 /**
1741  * ice_vc_select_vlans - Compact a VF VLAN request in place
1742  * @vf: VF tracking structure
1743  * @vids: VLAN IDs supplied by the VF
1744  * @count: number of VLAN IDs in the request
1745  * @add: select absent VLANs for add, or present VLANs for delete
1746  * @selected_count: returned number of VLAN IDs requiring a hardware change
1747  *
1748  * A VF may replay its entire VLAN configuration after a reset or retry a
1749  * request whose reply was lost.  Select only unique IDs whose membership
1750  * actually changes so those requests remain idempotent and filter accounting
1751  * continues to enforce the configured limit.
1752  */
1753 static int
1754 ice_vc_select_vlans(struct ice_vf *vf, u16 *vids, u16 count, bool add,
1755     u16 *selected_count)
1756 {
1757 	bitstr_t bit_decl(seen, ICE_VF_VLAN_MAP_LEN);
1758 	u16 selected, vid;
1759 
1760 	bzero(seen, sizeof(seen));
1761 	selected = 0;
1762 	for (u16 i = 0; i < count; i++) {
1763 		vid = vids[i];
1764 		if (vid > EVL_VLID_MASK)
1765 			return (EINVAL);
1766 		if (bit_test(seen, vid))
1767 			continue;
1768 		bit_set(seen, vid);
1769 		if (bit_test(vf->vlans_map, vid) == add)
1770 			continue;
1771 		vids[selected++] = vid;
1772 	}
1773 	*selected_count = selected;
1774 	return (0);
1775 }
1776 
1777 /**
1778  * ice_vc_add_vlan_msg - Handle VIRTCHNL_OP_ADD_VLAN msg from VF
1779  * @sc: PF's softc structure
1780  * @vf: VF tracking structure
1781  * @msg_buf: message buffer from VF
1782  *
1783  * Adds the VLANs in msg_buf to the VF's VLAN filter list.
1784  */
1785 static void
1786 ice_vc_add_vlan_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
1787 {
1788 	struct ice_hw *hw = &sc->hw;
1789 	struct virtchnl_vlan_filter_list *vlan_list;
1790 	u16 selected;
1791 	int status = 0;
1792 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
1793 	struct ice_vsi *vsi = vf->vsi;
1794 
1795 	vlan_list = (struct virtchnl_vlan_filter_list *)msg_buf;
1796 
1797 	if (vlan_list->vsi_id != vsi->idx) {
1798 		device_printf(sc->dev,
1799 			      "VF-%d: Message has invalid VSI ID (expected %d, got %d)\n",
1800 			      vf->vf_num, vsi->idx, vlan_list->vsi_id);
1801 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
1802 		goto done;
1803 	}
1804 
1805 	status = ice_vc_select_vlans(vf, vlan_list->vlan_id,
1806 	    vlan_list->num_elements, true, &selected);
1807 	if (status != 0) {
1808 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
1809 		goto done;
1810 	}
1811 
1812 	if ((u32)vf->vlan_cnt + selected > vf->vlan_limit) {
1813 		v_status = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1814 		goto done;
1815 	}
1816 	if (selected == 0)
1817 		goto done;
1818 
1819 	for (u16 i = 0; i < selected; i++) {
1820 		status = ice_add_vlan_hw_filter(vsi, vlan_list->vlan_id[i]);
1821 		if (status != 0 && status != ICE_ERR_ALREADY_EXISTS) {
1822 			device_printf(sc->dev,
1823 			    "VF-%d: Failure adding VLAN %d to VSI %d, err %s aq_err %s\n",
1824 			    vf->vf_num, vlan_list->vlan_id[i], vsi->idx,
1825 			    ice_status_str(status),
1826 			    ice_aq_str(sc->hw.adminq.sq_last_status));
1827 			v_status = ice_iov_err_to_virt_err(status);
1828 			goto done;
1829 		}
1830 		bit_set(vf->vlans_map, vlan_list->vlan_id[i]);
1831 		vf->vlan_cnt++;
1832 	}
1833 
1834 done:
1835 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_ADD_VLAN,
1836 	    v_status, NULL, 0, NULL);
1837 }
1838 
1839 /**
1840  * ice_vc_del_vlan_msg - Handle VIRTCHNL_OP_DEL_VLAN msg from VF
1841  * @sc: PF's softc structure
1842  * @vf: VF tracking structure
1843  * @msg_buf: message buffer from VF
1844  *
1845  * Removes the VLANs in msg_buf from the VF's VLAN filter list.
1846  */
1847 static void
1848 ice_vc_del_vlan_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
1849 {
1850 	struct ice_hw *hw = &sc->hw;
1851 	struct virtchnl_vlan_filter_list *vlan_list;
1852 	u16 selected;
1853 	int status = 0;
1854 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
1855 	struct ice_vsi *vsi = vf->vsi;
1856 
1857 	vlan_list = (struct virtchnl_vlan_filter_list *)msg_buf;
1858 
1859 	if (vlan_list->vsi_id != vsi->idx) {
1860 		device_printf(sc->dev,
1861 			      "VF-%d: Message has invalid VSI ID (expected %d, got %d)\n",
1862 			      vf->vf_num, vsi->idx, vlan_list->vsi_id);
1863 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
1864 		goto done;
1865 	}
1866 
1867 	status = ice_vc_select_vlans(vf, vlan_list->vlan_id,
1868 	    vlan_list->num_elements, false, &selected);
1869 	if (status != 0) {
1870 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
1871 		goto done;
1872 	}
1873 	if (selected == 0)
1874 		goto done;
1875 
1876 	for (u16 i = 0; i < selected; i++) {
1877 		status = ice_remove_vlan_hw_filter(vsi, vlan_list->vlan_id[i]);
1878 		if (status != 0 && status != ICE_ERR_DOES_NOT_EXIST) {
1879 			device_printf(sc->dev,
1880 			    "VF-%d: Failure deleting VLAN %d from VSI %d, err %s aq_err %s\n",
1881 			    vf->vf_num, vlan_list->vlan_id[i], vsi->idx,
1882 			    ice_status_str(status),
1883 			    ice_aq_str(sc->hw.adminq.sq_last_status));
1884 			v_status = ice_iov_err_to_virt_err(status);
1885 			goto done;
1886 		}
1887 		bit_clear(vf->vlans_map, vlan_list->vlan_id[i]);
1888 		MPASS(vf->vlan_cnt > 0);
1889 		vf->vlan_cnt--;
1890 	}
1891 
1892 done:
1893 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_DEL_VLAN,
1894 	    v_status, NULL, 0, NULL);
1895 }
1896 
1897 /**
1898  * ice_vc_validate_queue_select - Validate a VF queue selection
1899  * @sc: PF's softc structure
1900  * @vf: VF tracking structure
1901  * @vqs: queue selection from the VF
1902  *
1903  * Return true when the VSI ID and both queue masks are valid for the VF.
1904  */
1905 static bool
1906 ice_vc_validate_queue_select(struct ice_softc *sc, struct ice_vf *vf,
1907     const struct virtchnl_queue_select *vqs)
1908 {
1909 	struct ice_vsi *vsi = vf->vsi;
1910 	int bit;
1911 
1912 	if (vqs->vsi_id != vsi->idx) {
1913 		device_printf(sc->dev,
1914 		    "%s: VF-%d: Message has invalid VSI ID (expected %d, got %d)\n",
1915 		    __func__, vf->vf_num, vsi->idx, vqs->vsi_id);
1916 		return (false);
1917 	}
1918 	if (vqs->rx_queues == 0 && vqs->tx_queues == 0) {
1919 		device_printf(sc->dev,
1920 		    "%s: VF-%d: message queue masks are empty\n",
1921 		    __func__, vf->vf_num);
1922 		return (false);
1923 	}
1924 
1925 	bit = fls(vqs->rx_queues);
1926 	if (bit > vsi->num_rx_queues) {
1927 		device_printf(sc->dev,
1928 		    "%s: VF-%d: message's Rx queue map (0x%08x) has invalid bit set (%d)\n",
1929 		    __func__, vf->vf_num, vqs->rx_queues, bit);
1930 		return (false);
1931 	}
1932 	bit = fls(vqs->tx_queues);
1933 	if (bit > vsi->num_tx_queues) {
1934 		device_printf(sc->dev,
1935 		    "%s: VF-%d: message's Tx queue map (0x%08x) has invalid bit set (%d)\n",
1936 		    __func__, vf->vf_num, vqs->tx_queues, bit);
1937 		return (false);
1938 	}
1939 
1940 	return (true);
1941 }
1942 
1943 /**
1944  * ice_vc_disable_tx_queue - Disable one configured VF Tx queue
1945  * @sc: PF's softc structure
1946  * @vf: VF tracking structure
1947  * @qid: VF-relative queue ID
1948  */
1949 static int
1950 ice_vc_disable_tx_queue(struct ice_softc *sc, struct ice_vf *vf, u16 qid)
1951 {
1952 	struct ice_vsi *vsi = vf->vsi;
1953 	struct ice_tx_queue *txq = &vsi->tx_queues[qid];
1954 	struct ice_hw *hw = &sc->hw;
1955 	u16 q_handle, q_id;
1956 	u32 q_teid;
1957 	int status;
1958 
1959 	q_handle = txq->q_handle;
1960 	q_id = vsi->tx_qmap[qid];
1961 	q_teid = txq->q_teid;
1962 	status = ice_dis_vsi_txq(hw->port_info, vsi->idx, txq->tc, 1,
1963 	    &q_handle, &q_id, &q_teid, ICE_NO_RESET, 0, NULL);
1964 	if (status != ICE_SUCCESS && status != ICE_ERR_DOES_NOT_EXIST &&
1965 	    status != ICE_ERR_RESET_ONGOING) {
1966 		device_printf(sc->dev,
1967 		    "Failed to disable VF-%d Tx queue %u, err %s aq_err %s\n",
1968 		    vf->vf_num, qid, ice_status_str(status),
1969 		    ice_aq_str(hw->adminq.sq_last_status));
1970 		return (EIO);
1971 	}
1972 	txq->q_handle = 0;
1973 	txq->q_teid = 0;
1974 
1975 	return (0);
1976 }
1977 
1978 /**
1979  * ice_vc_disable_queues - Disable selected configured VF queues
1980  * @sc: PF's softc structure
1981  * @vf: VF tracking structure
1982  * @tx_queues: VF-relative Tx queue bitmap
1983  * @rx_queues: VF-relative Rx queue bitmap
1984  *
1985  * Queue disable is idempotent. Queues which are not configured or enabled
1986  * have no hardware work to perform and are treated as successfully disabled.
1987  * Since CONFIG_VSI_QUEUES also enables Tx, disabling a Tx queue removes its
1988  * tracked configuration and the VF must configure it before enabling it again.
1989  */
1990 static int
1991 ice_vc_disable_queues(struct ice_softc *sc, struct ice_vf *vf,
1992     u32 tx_queues, u32 rx_queues)
1993 {
1994 	struct ice_vsi *vsi = vf->vsi;
1995 	u32 queues;
1996 	int bit, error;
1997 
1998 	queues = rx_queues & vf->rxq_enabled;
1999 	while (queues != 0) {
2000 		bit = ffs(queues) - 1;
2001 		error = ice_control_rx_queue(vsi, bit, false);
2002 		if (error != 0) {
2003 			device_printf(sc->dev,
2004 			    "Unable to disable VF-%d Rx queue %d: %s\n",
2005 			    vf->vf_num, bit, ice_err_str(error));
2006 			return (error);
2007 		}
2008 		vf->rxq_enabled &= ~BIT(bit);
2009 		queues &= ~BIT(bit);
2010 	}
2011 
2012 	queues = tx_queues & vf->txq_configured;
2013 	if (queues == vf->txq_configured && queues != 0) {
2014 		error = ice_vsi_disable_tx(vsi);
2015 		if (error != 0)
2016 			return (error);
2017 		vf->txq_configured = 0;
2018 		return (0);
2019 	}
2020 	while (queues != 0) {
2021 		bit = ffs(queues) - 1;
2022 		error = ice_vc_disable_tx_queue(sc, vf, bit);
2023 		if (error != 0)
2024 			return (error);
2025 		vf->txq_configured &= ~BIT(bit);
2026 		queues &= ~BIT(bit);
2027 	}
2028 
2029 	return (0);
2030 }
2031 
2032 /**
2033  * ice_vc_validate_ring_len - Check to see if a descriptor ring length is valid
2034  * @ring_len: length of ring
2035  *
2036  * Check whether a ring size value is valid.
2037  *
2038  * @returns true if given ring size is valid
2039  */
2040 static bool
2041 ice_vc_isvalid_ring_len(u32 ring_len)
2042 {
2043 	return (ring_len >= ICE_MIN_DESC_COUNT &&
2044 		ring_len <= ICE_MAX_DESC_COUNT &&
2045 		!(ring_len % ICE_DESC_COUNT_INCR));
2046 }
2047 
2048 /**
2049  * ice_vc_isvalid_txq - Validate a VF transmit queue description
2050  * @txq: VF-supplied transmit queue description
2051  *
2052  * Queue base addresses are encoded in the hardware context in 128-byte
2053  * units. Reject values which would be truncated while building the context.
2054  */
2055 static bool
2056 ice_vc_isvalid_txq(const struct virtchnl_txq_info *txq)
2057 {
2058 	u64 align;
2059 
2060 	align = BIT_ULL(ICE_TLAN_CTX_BASE_S);
2061 	return (ice_vc_isvalid_ring_len(txq->ring_len) &&
2062 	    txq->dma_ring_addr != 0 &&
2063 	    (txq->dma_ring_addr & (align - 1)) == 0 &&
2064 	    txq->headwb_enabled == 0);
2065 }
2066 
2067 /**
2068  * ice_vc_isvalid_rxq - Validate a VF receive queue description
2069  * @rxq: VF-supplied receive queue description
2070  *
2071  * The receive queue context stores its ring base and data buffer size in
2072  * 128-byte units. It can represent data buffers from 128 through 16256
2073  * bytes. The current driver supports neither header splitting nor retaining
2074  * the Ethernet CRC for VFs. Some older iavf drivers request the maximum PF
2075  * frame size with a buffer too small to hold it in five segments. Accept that
2076  * advisory mismatch; ice_setup_rx_ctx() safely limits the hardware RXMAX to
2077  * five data buffers.
2078  */
2079 static bool
2080 ice_vc_isvalid_rxq(const struct virtchnl_rxq_info *rxq)
2081 {
2082 	u64 ring_align;
2083 	u32 buffer_align;
2084 
2085 	ring_align = BIT_ULL(ICE_RLAN_BASE_S);
2086 	buffer_align = BIT(ICE_RLAN_CTX_DBUF_S);
2087 
2088 	return (ice_vc_isvalid_ring_len(rxq->ring_len) &&
2089 	    rxq->dma_ring_addr != 0 &&
2090 	    (rxq->dma_ring_addr & (ring_align - 1)) == 0 &&
2091 	    rxq->databuffer_size >= buffer_align &&
2092 	    rxq->databuffer_size <= ICE_VC_MAX_RX_BUFFER &&
2093 	    (rxq->databuffer_size & (buffer_align - 1)) == 0 &&
2094 	    rxq->max_pkt_size >= ETHER_MIN_LEN &&
2095 	    rxq->max_pkt_size <= ICE_MAX_FRAME_SIZE &&
2096 	    rxq->splithdr_enabled == 0 && rxq->crc_disable == 0);
2097 }
2098 
2099 /**
2100  * ice_vc_isvalid_itr_idx - Validate a virtchnl interrupt throttle index
2101  * @itr_idx: VF-supplied ITR index
2102  */
2103 static bool
2104 ice_vc_isvalid_itr_idx(u16 itr_idx)
2105 {
2106 
2107 	return (itr_idx == VIRTCHNL_ITR_IDX_0 ||
2108 	    itr_idx == VIRTCHNL_ITR_IDX_1 ||
2109 	    itr_idx == VIRTCHNL_ITR_IDX_NO_ITR);
2110 }
2111 
2112 /**
2113  * ice_vc_cfg_vsi_qs_msg - Handle VIRTCHNL_OP_CONFIG_VSI_QUEUES msg from VF
2114  * @sc: PF's softc structure
2115  * @vf: VF tracking structure
2116  * @msg_buf: message buffer from VF
2117  */
2118 static void
2119 ice_vc_cfg_vsi_qs_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
2120 {
2121 	device_t dev = sc->dev;
2122 	struct ice_hw *hw = &sc->hw;
2123 	struct virtchnl_vsi_queue_config_info *vqci;
2124 	struct virtchnl_queue_pair_info *vqpi;
2125 	enum virtchnl_status_code status = VIRTCHNL_STATUS_SUCCESS;
2126 	struct ice_vsi *vsi = vf->vsi;
2127 	struct ice_tx_queue *txq;
2128 	struct ice_rx_queue *rxq;
2129 	u32 expected_map, max_pkt_size, queue_map, rx_buffer_size;
2130 	int i, error = 0;
2131 
2132 	vqci = (struct virtchnl_vsi_queue_config_info *)msg_buf;
2133 	ICE_FAIL_POINT_CODE_COND(sc, _debug_fail_point_ice_iov,
2134 	    malformed_queues, ice_iov_fail_vf_matches(vf->vf_num),
2135 	    FAIL_POINT_NONSLEEPABLE, {
2136 		switch (RETURN_VALUE) {
2137 		case 1:
2138 			vqci->qpair[0].txq.dma_ring_addr |= 1;
2139 			break;
2140 		case 2:
2141 			vqci->qpair[0].rxq.dma_ring_addr |= 1;
2142 			break;
2143 		case 3:
2144 			vqci->qpair[0].rxq.databuffer_size++;
2145 			break;
2146 		case 4:
2147 			vqci->qpair[0].rxq.max_pkt_size = 0;
2148 			break;
2149 		case 5:
2150 			if (vqci->num_queue_pairs > 1) {
2151 				vqci->qpair[1].txq.queue_id =
2152 				    vqci->qpair[0].txq.queue_id;
2153 				vqci->qpair[1].rxq.queue_id =
2154 				    vqci->qpair[0].rxq.queue_id;
2155 			} else {
2156 				vqci->qpair[0].txq.queue_id++;
2157 			}
2158 			break;
2159 		case 6:
2160 			vqci->qpair[0].rxq.databuffer_size =
2161 			    ICE_VC_MAX_RX_BUFFER + BIT(ICE_RLAN_CTX_DBUF_S);
2162 			break;
2163 		default:
2164 			vqci->vsi_id++;
2165 			break;
2166 		}
2167 	});
2168 
2169 	if (vqci->vsi_id != vsi->idx || vqci->num_queue_pairs == 0 ||
2170 	    vqci->num_queue_pairs > sizeof(queue_map) * NBBY ||
2171 	    vqci->num_queue_pairs > vsi->num_tx_queues ||
2172 	    vqci->num_queue_pairs > vsi->num_rx_queues) {
2173 		status = VIRTCHNL_STATUS_ERR_PARAM;
2174 		goto done;
2175 	}
2176 
2177 	queue_map = 0;
2178 	rx_buffer_size = 0;
2179 	max_pkt_size = 0;
2180 	vqpi = vqci->qpair;
2181 	for (i = 0; i < vqci->num_queue_pairs; i++, vqpi++) {
2182 		if (vqpi->txq.vsi_id != vsi->idx ||
2183 		    vqpi->rxq.vsi_id != vsi->idx ||
2184 		    vqpi->txq.queue_id != vqpi->rxq.queue_id ||
2185 		    vqpi->txq.queue_id >= vsi->num_tx_queues ||
2186 		    vqpi->rxq.queue_id >= vsi->num_rx_queues ||
2187 		    (queue_map & BIT(vqpi->txq.queue_id)) != 0 ||
2188 		    !ice_vc_isvalid_txq(&vqpi->txq) ||
2189 		    !ice_vc_isvalid_rxq(&vqpi->rxq)) {
2190 			status = VIRTCHNL_STATUS_ERR_PARAM;
2191 			goto done;
2192 		}
2193 		if (i == 0) {
2194 			rx_buffer_size = vqpi->rxq.databuffer_size;
2195 			max_pkt_size = vqpi->rxq.max_pkt_size;
2196 		} else if (vqpi->rxq.databuffer_size != rx_buffer_size ||
2197 		    vqpi->rxq.max_pkt_size != max_pkt_size) {
2198 			status = VIRTCHNL_STATUS_ERR_PARAM;
2199 			goto done;
2200 		}
2201 		queue_map |= BIT(vqpi->txq.queue_id);
2202 	}
2203 	if (vqci->num_queue_pairs == sizeof(queue_map) * NBBY)
2204 		expected_map = ~0U;
2205 	else
2206 		expected_map = BIT(vqci->num_queue_pairs) - 1;
2207 	if (queue_map != expected_map) {
2208 		status = VIRTCHNL_STATUS_ERR_PARAM;
2209 		goto done;
2210 	}
2211 
2212 	error = ice_vc_disable_queues(sc, vf, vf->txq_configured,
2213 	    vf->rxq_enabled);
2214 	if (error != 0) {
2215 		status = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2216 		goto done;
2217 	}
2218 	vf->txq_configured = 0;
2219 	vf->rxq_configured = 0;
2220 	vf->rxq_enabled = 0;
2221 
2222 	/*
2223 	 * Clear TX and RX queues config in case VF
2224 	 * requests different number of queues.
2225 	 */
2226 	for (i = 0; i < vsi->num_tx_queues; i++) {
2227 		txq = &vsi->tx_queues[i];
2228 
2229 		txq->desc_count = 0;
2230 		txq->tx_paddr = 0;
2231 		txq->q_teid = 0;
2232 		txq->q_handle = 0;
2233 		txq->tc = 0;
2234 	}
2235 
2236 	for (i = 0; i < vsi->num_rx_queues; i++) {
2237 		rxq = &vsi->rx_queues[i];
2238 
2239 		rxq->desc_count = 0;
2240 		rxq->rx_paddr = 0;
2241 	}
2242 
2243 	vqpi = vqci->qpair;
2244 	for (i = 0; i < vqci->num_queue_pairs; i++, vqpi++) {
2245 		/* Copy parameters into VF's queue/VSI structs */
2246 		txq = &vsi->tx_queues[vqpi->txq.queue_id];
2247 
2248 		txq->desc_count = vqpi->txq.ring_len;
2249 		txq->tx_paddr = vqpi->txq.dma_ring_addr;
2250 		txq->q_handle = vqpi->txq.queue_id;
2251 		txq->tc = 0;
2252 
2253 		rxq = &vsi->rx_queues[vqpi->rxq.queue_id];
2254 
2255 		rxq->desc_count = vqpi->rxq.ring_len;
2256 		rxq->rx_paddr = vqpi->rxq.dma_ring_addr;
2257 	}
2258 	vsi->mbuf_sz = rx_buffer_size;
2259 	vsi->max_frame_size = max_pkt_size;
2260 
2261 	/* Configure TX queues in HW */
2262 	/*
2263 	 * Record the intended map before programming hardware so a partial
2264 	 * firmware failure remains discoverable and can be cleaned up by the
2265 	 * next configuration attempt.
2266 	 */
2267 	vf->txq_configured = queue_map;
2268 	error = ice_cfg_vsi_for_tx(vsi);
2269 	if (error) {
2270 		device_printf(dev,
2271 			      "VF-%d: Unable to configure VSI for Tx: %s\n",
2272 			      vf->vf_num, ice_err_str(error));
2273 		status = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2274 		if (ice_vsi_disable_tx(vsi) == 0)
2275 			vf->txq_configured = 0;
2276 		goto done;
2277 	}
2278 
2279 	/* Configure RX queues in HW */
2280 	error = ice_cfg_vsi_for_rx(vsi);
2281 	if (error) {
2282 		device_printf(dev,
2283 			      "VF-%d: Unable to configure VSI for Rx: %s\n",
2284 			      vf->vf_num, ice_err_str(error));
2285 		status = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2286 		(void)ice_vc_disable_queues(sc, vf, vf->txq_configured, 0);
2287 		goto done;
2288 	}
2289 	vf->rxq_configured = queue_map;
2290 
2291 done:
2292 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2293 	    status, NULL, 0, NULL);
2294 }
2295 
2296 /**
2297  * ice_vc_cfg_rss_key_msg - Handle VIRTCHNL_OP_CONFIG_RSS_KEY msg from VF
2298  * @sc: PF's softc structure
2299  * @vf: VF tracking structure
2300  * @msg_buf: message buffer from VF
2301  *
2302  * Sets the RSS key for the given VF, using the contents of msg_buf.
2303  */
2304 static void
2305 ice_vc_cfg_rss_key_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
2306 {
2307 	struct ice_aqc_get_set_rss_keys keydata =
2308 	    { .standard_rss_key = {0}, .extended_hash_key = {0} };
2309 	struct ice_hw *hw = &sc->hw;
2310 	struct virtchnl_rss_key *vrk;
2311 	int status = 0;
2312 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
2313 	struct ice_vsi *vsi = vf->vsi;
2314 
2315 	vrk = (struct virtchnl_rss_key *)msg_buf;
2316 	ICE_FAIL_POINT_CODE_COND(sc, _debug_fail_point_ice_iov,
2317 	    malformed_rss_key, ice_iov_fail_vf_matches(vf->vf_num),
2318 	    FAIL_POINT_NONSLEEPABLE, {
2319 		vrk->key_len--;
2320 	});
2321 
2322 	if (vrk->vsi_id != vsi->idx) {
2323 		device_printf(sc->dev,
2324 		    "VF-%d: Message has invalid VSI ID (expected %d, got %d)\n",
2325 		    vf->vf_num, vsi->idx, vrk->vsi_id);
2326 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2327 		goto done;
2328 	}
2329 
2330 	/* The VF must use the exact key size advertised by this PF. */
2331 	if (vrk->key_len != ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE) {
2332 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2333 		goto done;
2334 	}
2335 
2336 	memcpy(&keydata, vrk->key, vrk->key_len);
2337 
2338 	status = ice_aq_set_rss_key(hw, vsi->idx, &keydata);
2339 	if (status) {
2340 		device_printf(sc->dev,
2341 			      "ice_aq_set_rss_key status %s, error %s\n",
2342 			      ice_status_str(status), ice_aq_str(hw->adminq.sq_last_status));
2343 		v_status = ice_iov_err_to_virt_err(status);
2344 		goto done;
2345 	}
2346 
2347 done:
2348 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_CONFIG_RSS_KEY,
2349 	    v_status, NULL, 0, NULL);
2350 }
2351 
2352 /**
2353  * ice_vc_cfg_rss_lut_msg - Handle VIRTCHNL_OP_CONFIG_RSS_LUT msg from VF
2354  * @sc: PF's softc structure
2355  * @vf: VF tracking structure
2356  * @msg_buf: message buffer from VF
2357  *
2358  * Adds the LUT from the VF in msg_buf to the PF via an admin queue call.
2359  */
2360 static void
2361 ice_vc_cfg_rss_lut_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
2362 {
2363 	struct ice_hw *hw = &sc->hw;
2364 	struct virtchnl_rss_lut *vrl;
2365 	int i, status = 0;
2366 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
2367 	struct ice_aq_get_set_rss_lut_params lut_params = {};
2368 	struct ice_vsi *vsi = vf->vsi;
2369 
2370 	vrl = (struct virtchnl_rss_lut *)msg_buf;
2371 	ICE_FAIL_POINT_CODE_COND(sc, _debug_fail_point_ice_iov,
2372 	    malformed_rss_lut, ice_iov_fail_vf_matches(vf->vf_num),
2373 	    FAIL_POINT_NONSLEEPABLE, {
2374 		if (RETURN_VALUE == 1)
2375 			vrl->lut_entries--;
2376 		else
2377 			vrl->lut[0] = vsi->num_rx_queues;
2378 	});
2379 
2380 	if (vrl->vsi_id != vsi->idx) {
2381 		device_printf(sc->dev,
2382 		    "VF-%d: Message has invalid VSI ID (expected %d, got %d)\n",
2383 		    vf->vf_num, vsi->idx, vrl->vsi_id);
2384 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2385 		goto done;
2386 	}
2387 
2388 	/* The VF must use the exact LUT size advertised by this PF. */
2389 	if (vrl->lut_entries != vsi->rss_table_size) {
2390 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2391 		goto done;
2392 	}
2393 	for (i = 0; i < vrl->lut_entries; i++) {
2394 		if (vrl->lut[i] >= vsi->num_rx_queues) {
2395 			v_status = VIRTCHNL_STATUS_ERR_PARAM;
2396 			goto done;
2397 		}
2398 	}
2399 
2400 	lut_params.vsi_handle = vsi->idx;
2401 	lut_params.lut_size = vrl->lut_entries;
2402 	lut_params.lut_type = vsi->rss_lut_type;
2403 	lut_params.lut = vrl->lut;
2404 	lut_params.global_lut_id = 0;
2405 
2406 	status = ice_aq_set_rss_lut(hw, &lut_params);
2407 	if (status) {
2408 		device_printf(sc->dev,
2409 			      "VF-%d: Cannot set RSS lut, err %s aq_err %s\n",
2410 			      vf->vf_num, ice_status_str(status),
2411 			      ice_aq_str(hw->adminq.sq_last_status));
2412 		v_status = ice_iov_err_to_virt_err(status);
2413 	}
2414 
2415 done:
2416 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_CONFIG_RSS_LUT,
2417 	    v_status, NULL, 0, NULL);
2418 }
2419 
2420 /**
2421  * ice_vc_set_rss_hena_msg - Handle VIRTCHNL_OP_SET_RSS_HENA msg from VF
2422  * @sc: PF's softc structure
2423  * @vf: VF tracking structure
2424  * @msg_buf: message buffer from VF
2425  *
2426  * Adds the VF's hena (hash enable) bits as flow types to the PF's RSS flow
2427  * type list.
2428  */
2429 static void
2430 ice_vc_set_rss_hena_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
2431 {
2432 	struct ice_hw *hw = &sc->hw;
2433 	struct virtchnl_rss_hena *vrh;
2434 	int status = 0;
2435 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
2436 	struct ice_vsi *vsi = vf->vsi;
2437 
2438 	MPASS(vsi != NULL);
2439 
2440 	vrh = (struct virtchnl_rss_hena *)msg_buf;
2441 
2442 	/*
2443 	 * Remove existing configuration to make sure only requested
2444 	 * config is applied and allow VFs to disable RSS completly.
2445 	 */
2446 	status = ice_rem_vsi_rss_cfg(hw, vsi->idx);
2447 	if (vrh->hena) {
2448 		/*
2449 		 * Problem with removing config is not fatal, when new one
2450 		 * is requested. Warn about it but try to apply new config
2451 		 * anyway.
2452 		 */
2453 		if (status)
2454 			device_printf(sc->dev,
2455 			    "ice_rem_vsi_rss_cfg status %s, error %s\n",
2456 			    ice_status_str(status),
2457 			    ice_aq_str(hw->adminq.sq_last_status));
2458 		status = ice_add_avf_rss_cfg(hw, vsi->idx, vrh->hena);
2459 		if (status)
2460 			device_printf(sc->dev,
2461 			    "ice_add_avf_rss_cfg status %s, error %s\n",
2462 			    ice_status_str(status),
2463 			    ice_aq_str(hw->adminq.sq_last_status));
2464 	}
2465 	v_status = ice_iov_err_to_virt_err(status);
2466 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_SET_RSS_HENA,
2467 	    v_status, NULL, 0, NULL);
2468 }
2469 
2470 /**
2471  * ice_vc_enable_queues_msg - Handle VIRTCHNL_OP_ENABLE_QUEUES msg from VF
2472  * @sc: PF's softc structure
2473  * @vf: VF tracking structure
2474  * @msg_buf: message buffer from VF
2475  *
2476  * Enables VF queues selected in msg_buf for Tx/Rx traffic.
2477  *
2478  * @remark Only actually operates on Rx queues; Tx queues are enabled in
2479  * CONFIG_VSI_QUEUES message handler.
2480  */
2481 static void
2482 ice_vc_enable_queues_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
2483 {
2484 	struct ice_hw *hw = &sc->hw;
2485 	struct virtchnl_queue_select *vqs;
2486 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
2487 	struct ice_vsi *vsi = vf->vsi;
2488 	u32 queues;
2489 	int bit, error;
2490 
2491 	vqs = (struct virtchnl_queue_select *)msg_buf;
2492 
2493 	if (!ice_vc_validate_queue_select(sc, vf, vqs)) {
2494 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2495 		goto done;
2496 	}
2497 	if ((vqs->tx_queues & ~vf->txq_configured) != 0 ||
2498 	    (vqs->rx_queues & ~vf->rxq_configured) != 0) {
2499 		device_printf(sc->dev,
2500 		    "%s: VF-%d: cannot enable unconfigured queues "
2501 		    "(Tx 0x%08x, Rx 0x%08x)\n",
2502 		    __func__, vf->vf_num, vqs->tx_queues,
2503 		    vqs->rx_queues);
2504 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2505 		goto done;
2506 	}
2507 
2508 	queues = vqs->rx_queues & ~vf->rxq_enabled;
2509 	while (queues != 0) {
2510 		bit = ffs(queues) - 1;
2511 		error = ice_control_rx_queue(vsi, bit, true);
2512 		if (error) {
2513 			device_printf(sc->dev,
2514 			    "Unable to enable VF-%d Rx queue %d: %s\n",
2515 			    vf->vf_num, bit, ice_err_str(error));
2516 			v_status = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2517 			goto done;
2518 		}
2519 		vf->rxq_enabled |= BIT(bit);
2520 		queues &= ~BIT(bit);
2521 	}
2522 	/* Tx queues were enabled when their contexts were configured. */
2523 
2524 done:
2525 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_ENABLE_QUEUES,
2526 	    v_status, NULL, 0, NULL);
2527 }
2528 
2529 /**
2530  * ice_vc_disable_queues_msg - Handle VIRTCHNL_OP_DISABLE_QUEUES msg
2531  * @sc: PF's softc structure
2532  * @vf: VF tracking structure
2533  * @msg_buf: message buffer from VF
2534  *
2535  * Disables the selected VF Tx and Rx queues. Repeated requests for queues
2536  * which are already disabled complete successfully without touching hardware.
2537  */
2538 static void
2539 ice_vc_disable_queues_msg(struct ice_softc *sc, struct ice_vf *vf,
2540 			  u8 *msg_buf)
2541 {
2542 	struct ice_hw *hw = &sc->hw;
2543 	struct virtchnl_queue_select *vqs;
2544 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
2545 	int error;
2546 
2547 	vqs = (struct virtchnl_queue_select *)msg_buf;
2548 	if (!ice_vc_validate_queue_select(sc, vf, vqs)) {
2549 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2550 		goto done;
2551 	}
2552 	error = ice_vc_disable_queues(sc, vf, vqs->tx_queues,
2553 	    vqs->rx_queues);
2554 	if (error != 0)
2555 		v_status = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2556 
2557 done:
2558 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_DISABLE_QUEUES,
2559 	    v_status, NULL, 0, NULL);
2560 }
2561 
2562 /**
2563  * ice_vc_cfg_irq_map_msg - Handle VIRTCHNL_OP_CFG_IRQ_MAP msg from VF
2564  * @sc: PF's softc structure
2565  * @vf: VF tracking structure
2566  * @msg_buf: message buffer from VF
2567  *
2568  * Configures the interrupt vectors described in the message in msg_buf. The
2569  * VF needs to send this message during init, so that queues can be allowed
2570  * to generate interrupts.
2571  */
2572 static void
2573 ice_vc_cfg_irq_map_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
2574 {
2575 	struct ice_hw *hw = &sc->hw;
2576 	struct virtchnl_irq_map_info *vimi;
2577 	struct virtchnl_vector_map *vvm;
2578 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
2579 	struct ice_vsi *vsi = vf->vsi;
2580 	u32 vectors_seen;
2581 	u16 rxqs_seen, txqs_seen, valid_rxqs, valid_txqs, vector;
2582 
2583 	vimi = (struct virtchnl_irq_map_info *)msg_buf;
2584 	ICE_FAIL_POINT_CODE_COND(sc, _debug_fail_point_ice_iov,
2585 	    malformed_irq_map, ice_iov_fail_vf_matches(vf->vf_num),
2586 	    FAIL_POINT_NONSLEEPABLE, {
2587 		switch (RETURN_VALUE) {
2588 		case 1:
2589 			vimi->vecmap[0].rxitr_idx = VIRTCHNL_ITR_IDX_NO_ITR + 1;
2590 			break;
2591 		case 2:
2592 			vimi->vecmap[0].vector_id = 0;
2593 			vimi->vecmap[0].rxq_map = 1;
2594 			break;
2595 		case 3:
2596 			if (vimi->num_vectors > 1) {
2597 				vimi->vecmap[1].vector_id =
2598 				    vimi->vecmap[0].vector_id;
2599 			} else {
2600 				vimi->vecmap[0].vsi_id++;
2601 			}
2602 			break;
2603 		default:
2604 			vimi->vecmap[0].vsi_id++;
2605 			break;
2606 		}
2607 	});
2608 
2609 	if (vimi->num_vectors == 0 ||
2610 	    vimi->num_vectors > vf->num_irq_vectors ||
2611 	    vimi->num_vectors > sizeof(vectors_seen) * NBBY ||
2612 	    vsi->num_tx_queues < 1 ||
2613 	    vsi->num_tx_queues > ICE_VIRTCHNL_QUEUE_MAP_SIZE ||
2614 	    vsi->num_rx_queues < 1 ||
2615 	    vsi->num_rx_queues > ICE_VIRTCHNL_QUEUE_MAP_SIZE) {
2616 		device_printf(sc->dev,
2617 		    "%s: VF-%d: invalid vector count %d (VF has %d)\n",
2618 		    __func__, vf->vf_num, vimi->num_vectors, vf->num_irq_vectors);
2619 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2620 		goto done;
2621 	}
2622 
2623 	valid_txqs = vsi->num_tx_queues == ICE_VIRTCHNL_QUEUE_MAP_SIZE ?
2624 	    (u16)~0U : (u16)(BIT(vsi->num_tx_queues) - 1);
2625 	valid_rxqs = vsi->num_rx_queues == ICE_VIRTCHNL_QUEUE_MAP_SIZE ?
2626 	    (u16)~0U : (u16)(BIT(vsi->num_rx_queues) - 1);
2627 	vectors_seen = 0;
2628 	txqs_seen = 0;
2629 	rxqs_seen = 0;
2630 
2631 	/* Validate the complete request before changing any queue state. */
2632 	vvm = vimi->vecmap;
2633 	for (int i = 0; i < vimi->num_vectors; i++, vvm++) {
2634 		/* vvm->vector_id is relative to VF space */
2635 		vector = vvm->vector_id;
2636 		if (vvm->vsi_id != vsi->idx ||
2637 		    vector >= vf->num_irq_vectors ||
2638 		    vector >= sizeof(vectors_seen) * NBBY ||
2639 		    (vectors_seen & BIT(vector)) != 0 ||
2640 		    !ice_vc_isvalid_itr_idx(vvm->txitr_idx) ||
2641 		    !ice_vc_isvalid_itr_idx(vvm->rxitr_idx) ||
2642 		    (vvm->txq_map & ~valid_txqs) != 0 ||
2643 		    (vvm->rxq_map & ~valid_rxqs) != 0 ||
2644 		    (txqs_seen & vvm->txq_map) != 0 ||
2645 		    (rxqs_seen & vvm->rxq_map) != 0 ||
2646 		    (vector == 0 &&
2647 		    (vvm->txq_map != 0 || vvm->rxq_map != 0))) {
2648 			device_printf(sc->dev,
2649 			    "%s: VF-%d: invalid queue mapping for vector %u\n",
2650 			    __func__, vf->vf_num, vector);
2651 			v_status = VIRTCHNL_STATUS_ERR_PARAM;
2652 			goto done;
2653 		}
2654 		vectors_seen |= BIT(vector);
2655 		txqs_seen |= vvm->txq_map;
2656 		rxqs_seen |= vvm->rxq_map;
2657 	}
2658 
2659 	/* Save the validated queue-to-vector mappings. */
2660 	vvm = vimi->vecmap;
2661 	for (int i = 0; i < vimi->num_vectors; i++, vvm++) {
2662 		struct ice_tx_queue *txq;
2663 		struct ice_rx_queue *rxq;
2664 		int bit;
2665 
2666 		vector = vvm->vector_id;
2667 
2668 		/* The Misc/Admin Queue vector doesn't need mapping */
2669 		if (vector == 0)
2670 			continue;
2671 
2672 		for (bit = 0; bit < ICE_VIRTCHNL_QUEUE_MAP_SIZE; bit++) {
2673 			if ((vvm->txq_map & BIT(bit)) == 0)
2674 				continue;
2675 			vf->tx_irqvs[vector].me = vector;
2676 
2677 			txq = &vsi->tx_queues[bit];
2678 			txq->irqv = &vf->tx_irqvs[vector];
2679 			txq->itr_idx = vvm->txitr_idx;
2680 		}
2681 		for (bit = 0; bit < ICE_VIRTCHNL_QUEUE_MAP_SIZE; bit++) {
2682 			if ((vvm->rxq_map & BIT(bit)) == 0)
2683 				continue;
2684 			vf->rx_irqvs[vector].me = vector;
2685 
2686 			rxq = &vsi->rx_queues[bit];
2687 			rxq->irqv = &vf->rx_irqvs[vector];
2688 			rxq->itr_idx = vvm->rxitr_idx;
2689 		}
2690 	}
2691 
2692 	/* Write to T/RQCTL registers to actually map vectors to queues */
2693 	for (int i = 0; i < vf->vsi->num_rx_queues; i++)
2694 		if (vsi->rx_queues[i].irqv != NULL)
2695 			ice_configure_rxq_interrupt(hw, vsi->rx_qmap[i],
2696 			    vsi->rx_queues[i].irqv->me, vsi->rx_queues[i].itr_idx);
2697 
2698 	for (int i = 0; i < vf->vsi->num_tx_queues; i++)
2699 		if (vsi->tx_queues[i].irqv != NULL)
2700 			ice_configure_txq_interrupt(hw, vsi->tx_qmap[i],
2701 			    vsi->tx_queues[i].irqv->me, vsi->tx_queues[i].itr_idx);
2702 
2703 	ice_flush(hw);
2704 
2705 done:
2706 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_CONFIG_IRQ_MAP,
2707 	    v_status, NULL, 0, NULL);
2708 }
2709 
2710 /**
2711  * ice_eth_stats_to_virtchnl_eth_stats - Convert stats for virtchnl
2712  * @istats: VSI stats from HW to convert
2713  * @vstats: stats struct to copy to
2714  *
2715  * This function copies all known stats in struct virtchnl_eth_stats from the
2716  * input struct ice_eth_stats to an output struct virtchnl_eth_stats.
2717  *
2718  * @remark These two structure types currently have the same definition up to
2719  * the size of struct virtchnl_eth_stats (on FreeBSD), but that could change
2720  * in the future.
2721  */
2722 static void
2723 ice_eth_stats_to_virtchnl_eth_stats(struct ice_eth_stats *istats,
2724 				    struct virtchnl_eth_stats *vstats)
2725 {
2726 	vstats->rx_bytes = istats->rx_bytes;
2727 	vstats->rx_unicast = istats->rx_unicast;
2728 	vstats->rx_multicast = istats->rx_multicast;
2729 	vstats->rx_broadcast = istats->rx_broadcast;
2730 	vstats->rx_discards = istats->rx_discards;
2731 	vstats->rx_unknown_protocol = istats->rx_unknown_protocol;
2732 	vstats->tx_bytes = istats->tx_bytes;
2733 	vstats->tx_unicast = istats->tx_unicast;
2734 	vstats->tx_multicast = istats->tx_multicast;
2735 	vstats->tx_broadcast = istats->tx_broadcast;
2736 	vstats->tx_discards = istats->tx_discards;
2737 	vstats->tx_errors = istats->tx_errors;
2738 }
2739 
2740 /**
2741  * ice_vc_get_stats_msg - Handle VIRTCHNL_OP_GET_STATS msg
2742  * @sc: device private structure
2743  * @vf: VF tracking structure
2744  * @msg_buf: raw message buffer from the VF
2745  *
2746  * Updates the VF's VSI stats and sends those stats back to the VF.
2747  */
2748 static void
2749 ice_vc_get_stats_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
2750 {
2751 	struct virtchnl_queue_select *vqs;
2752 	struct virtchnl_eth_stats stats;
2753 	struct ice_vsi *vsi = vf->vsi;
2754 	struct ice_hw *hw = &sc->hw;
2755 
2756 	vqs = (struct virtchnl_queue_select *)msg_buf;
2757 	ICE_FAIL_POINT_CODE_COND(sc, _debug_fail_point_ice_iov,
2758 	    get_stats_bad_vsi, ice_iov_fail_vf_matches(vf->vf_num),
2759 	    FAIL_POINT_NONSLEEPABLE, {
2760 		vqs->vsi_id = vsi->idx + 1;
2761 		device_printf(sc->dev,
2762 		    "injecting invalid GET_STATS VSI ID for VF %u\n",
2763 		    (unsigned int)vf->vf_num);
2764 	});
2765 
2766 	if (vqs->vsi_id != vsi->idx) {
2767 		device_printf(sc->dev,
2768 		    "%s: VF-%d: message has invalid VSI ID %d (VF has VSI ID %d)\n",
2769 		    __func__, vf->vf_num, vqs->vsi_id, vsi->idx);
2770 		ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_GET_STATS,
2771 		    VIRTCHNL_STATUS_ERR_PARAM, NULL, 0, NULL);
2772 		return;
2773 	}
2774 
2775 	ice_update_vsi_hw_stats(vf->vsi);
2776 	ice_eth_stats_to_virtchnl_eth_stats(&vsi->hw_stats.cur, &stats);
2777 
2778 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_GET_STATS,
2779 	    VIRTCHNL_STATUS_SUCCESS, (u8 *)&stats,
2780 	    sizeof(struct virtchnl_eth_stats), NULL);
2781 }
2782 
2783 /**
2784  * ice_vc_cfg_promisc_mode_msg - Handle VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE
2785  * @sc: PF's softc structure
2786  * @vf: VF tracking structure
2787  * @msg_buf: message buffer from VF
2788  *
2789  * Configures the promiscuous modes for the given VSI in msg_buf.
2790  */
2791 static void
2792 ice_vc_cfg_promisc_mode_msg(struct ice_softc *sc, struct ice_vf *vf, u8 *msg_buf)
2793 {
2794 	struct ice_hw *hw = &sc->hw;
2795 	struct virtchnl_promisc_info *vpi;
2796 	enum virtchnl_status_code v_status = VIRTCHNL_STATUS_SUCCESS;
2797 	int status = 0;
2798 	struct ice_vsi *vsi = vf->vsi;
2799 	ice_declare_bitmap(old_promisc_mask, ICE_PROMISC_MAX);
2800 	ice_declare_bitmap(req_promisc_mask, ICE_PROMISC_MAX);
2801 	ice_declare_bitmap(clear_promisc_mask, ICE_PROMISC_MAX);
2802 	ice_declare_bitmap(set_promisc_mask, ICE_PROMISC_MAX);
2803 	ice_declare_bitmap(old_req_xor_mask, ICE_PROMISC_MAX);
2804 	u16 vid;
2805 
2806 	vpi = (struct virtchnl_promisc_info *)msg_buf;
2807 
2808 	/* Check to see if VF has permission to configure promiscuous mode */
2809 	if (!(vf->vf_flags & VF_FLAG_PROMISC_CAP)) {
2810 		device_printf(sc->dev,
2811 			      "VF-%d: attempted to configure promiscuous mode\n",
2812 			      vf->vf_num);
2813 		/* Don't reply to VF with an error */
2814 		goto done;
2815 	}
2816 
2817 	if (vpi->vsi_id != vsi->idx) {
2818 		device_printf(sc->dev,
2819 			      "VF-%d: Message has invalid VSI ID (expected %d, got %d)\n",
2820 			      vf->vf_num, vsi->idx, vpi->vsi_id);
2821 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2822 		goto done;
2823 	}
2824 
2825 	if (vpi->flags & ~ICE_VIRTCHNL_VALID_PROMISC_FLAGS) {
2826 		device_printf(sc->dev,
2827 			      "VF-%d: Message has invalid promiscuous flags set (valid 0x%02x, got 0x%02x)\n",
2828 			      vf->vf_num, ICE_VIRTCHNL_VALID_PROMISC_FLAGS,
2829 			      vpi->flags);
2830 		v_status = VIRTCHNL_STATUS_ERR_PARAM;
2831 		goto done;
2832 
2833 	}
2834 
2835 	ice_zero_bitmap(req_promisc_mask, ICE_PROMISC_MAX);
2836 	/* Convert virtchnl flags to ice AQ promiscuous mode flags */
2837 	if (vpi->flags & FLAG_VF_UNICAST_PROMISC) {
2838 		ice_set_bit(ICE_PROMISC_UCAST_TX, req_promisc_mask);
2839 		ice_set_bit(ICE_PROMISC_UCAST_RX, req_promisc_mask);
2840 	}
2841 	if (vpi->flags & FLAG_VF_MULTICAST_PROMISC) {
2842 		ice_set_bit(ICE_PROMISC_MCAST_TX, req_promisc_mask);
2843 		ice_set_bit(ICE_PROMISC_MCAST_RX, req_promisc_mask);
2844 	}
2845 
2846 	status = ice_get_vsi_promisc(hw, vsi->idx, old_promisc_mask, &vid);
2847 	if (status) {
2848 		device_printf(sc->dev,
2849 			      "VF-%d: Failed to get promiscuous mode mask for VSI %d, err %s aq_err %s\n",
2850 			      vf->vf_num, vsi->idx,
2851 			      ice_status_str(status),
2852 			      ice_aq_str(hw->adminq.sq_last_status));
2853 		v_status = ice_iov_err_to_virt_err(status);
2854 		goto done;
2855 	}
2856 
2857 	/* Figure out what got added and what got removed */
2858 	ice_zero_bitmap(old_req_xor_mask, ICE_PROMISC_MAX);
2859 	ice_xor_bitmap(old_req_xor_mask, old_promisc_mask, req_promisc_mask, ICE_PROMISC_MAX);
2860 	ice_and_bitmap(clear_promisc_mask, old_req_xor_mask, old_promisc_mask, ICE_PROMISC_MAX);
2861 	ice_and_bitmap(set_promisc_mask, old_req_xor_mask, req_promisc_mask, ICE_PROMISC_MAX);
2862 
2863 	if (ice_is_any_bit_set(clear_promisc_mask, ICE_PROMISC_MAX)) {
2864 		status = ice_clear_vsi_promisc(hw, vsi->idx,
2865 					       clear_promisc_mask, 0);
2866 		if (status) {
2867 			device_printf(sc->dev,
2868 				      "VF-%d: Failed to clear promiscuous mode for VSI %d, err %s aq_err %s\n",
2869 				      vf->vf_num, vsi->idx,
2870 				      ice_status_str(status),
2871 				      ice_aq_str(hw->adminq.sq_last_status));
2872 			v_status = ice_iov_err_to_virt_err(status);
2873 			goto done;
2874 		}
2875 	}
2876 
2877 	if (ice_is_any_bit_set(set_promisc_mask, ICE_PROMISC_MAX)) {
2878 		status = ice_set_vsi_promisc(hw, vsi->idx, set_promisc_mask, 0);
2879 		if (status) {
2880 			device_printf(sc->dev,
2881 				      "VF-%d: Failed to set promiscuous mode for VSI %d, err %s aq_err %s\n",
2882 				      vf->vf_num, vsi->idx,
2883 				      ice_status_str(status),
2884 				      ice_aq_str(hw->adminq.sq_last_status));
2885 			v_status = ice_iov_err_to_virt_err(status);
2886 			goto done;
2887 		}
2888 	}
2889 
2890 done:
2891 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
2892 	    v_status, NULL, 0, NULL);
2893 }
2894 
2895 /**
2896  * ice_vc_notify_all_vfs_link_state - Notify all VFs of PF link state
2897  * @sc: device private structure
2898  *
2899  * Sends a message to all VFs about the status of the PF's link
2900  * state. For more details, @see ice_vc_notify_vf_link_state.
2901  */
2902 void
2903 ice_vc_notify_all_vfs_link_state(struct ice_softc *sc)
2904 {
2905 	for (int i = 0; i < sc->num_vfs; i++)
2906 		ice_vc_notify_vf_link_state(sc, &sc->vfs[i]);
2907 }
2908 
2909 /**
2910  * ice_vc_notify_vf_link_state - Notify VF of PF link state
2911  * @sc: device private structure
2912  * @vf: VF tracking structure
2913  *
2914  * Sends an event message to the specified VF with information about
2915  * the current link state from the PF's port. This includes whether
2916  * link is up or down, and the link speed in 100Mbps units.
2917  */
2918 static void
2919 ice_vc_notify_vf_link_state(struct ice_softc *sc, struct ice_vf *vf)
2920 {
2921 	struct virtchnl_pf_event event = {};
2922 	struct ice_hw *hw = &sc->hw;
2923 
2924 	event.event = VIRTCHNL_EVENT_LINK_CHANGE;
2925 	event.severity = PF_EVENT_SEVERITY_INFO;
2926 	event.event_data.link_event_adv.link_status = sc->link_up;
2927 	event.event_data.link_event_adv.link_speed =
2928 		(u32)ice_conv_link_speed_to_virtchnl(true,
2929 		    hw->port_info->phy.link_info.link_speed);
2930 
2931 	ice_aq_send_msg_to_vf(hw, vf->vf_num, VIRTCHNL_OP_EVENT,
2932 	    VIRTCHNL_STATUS_SUCCESS, (u8 *)&event, sizeof(event), NULL);
2933 }
2934 
2935 /**
2936  * ice_iov_mbx_overflow - Detect and isolate a VF flooding the PF mailbox
2937  * @sc: device private structure
2938  * @vf: VF which sent the current message
2939  * @mbx_data: software mailbox snapshot data, or NULL on E830
2940  *
2941  * E830 enforces the per-VF watermark in hardware.  On older devices, reset
2942  * and block a VF after the Intel snapshot detector first attributes an
2943  * overflow.  A later external VF reset, PF reset, or SR-IOV recreation
2944  * releases it.
2945  *
2946  * @returns true if the current message must be discarded.
2947  */
2948 static bool
2949 ice_iov_mbx_overflow(struct ice_softc *sc, struct ice_vf *vf,
2950     struct ice_mbx_data *mbx_data)
2951 {
2952 	struct ice_hw *hw = &sc->hw;
2953 	bool report_malvf;
2954 	u32 reg, vf_flags;
2955 	int error, status;
2956 
2957 	if (mbx_data == NULL)
2958 		return (false);
2959 
2960 	/* Every message advances the snapshot, including a blocked VF's. */
2961 	report_malvf = false;
2962 	status = ice_mbx_vf_state_handler(hw, mbx_data, &vf->mbx_info,
2963 	    &report_malvf);
2964 	if ((atomic_load_acq_32(&vf->vf_flags) & VF_FLAG_MBX_BLOCKED) != 0)
2965 		return (true);
2966 	ICE_FAIL_POINT_CODE_COND(sc, _debug_fail_point_ice_iov,
2967 	    mailbox_overflow, ice_iov_fail_vf_matches(vf->vf_num),
2968 	    FAIL_POINT_NONSLEEPABLE, {
2969 		status = 0;
2970 		vf->mbx_info.malicious = 1;
2971 		report_malvf = true;
2972 	});
2973 	if (status != 0) {
2974 		device_printf(sc->dev,
2975 		    "Unable to check VF %u mailbox overflow, err %s\n",
2976 		    vf->vf_num, ice_status_str(status));
2977 		return (false);
2978 	}
2979 	if (!report_malvf)
2980 		return (vf->mbx_info.malicious != 0);
2981 
2982 	vf->mbx_overflow_events++;
2983 	atomic_set_32(&vf->vf_flags, VF_FLAG_MBX_BLOCKED);
2984 	device_printf(sc->dev,
2985 	    "VF %u exceeded the mailbox message limit; resetting and blocking it\n",
2986 	    vf->vf_num);
2987 
2988 	vf_flags = atomic_load_acq_32(&vf->vf_flags);
2989 	if ((vf_flags & VF_FLAG_ENABLED) != 0 && vf->vsi != NULL) {
2990 		error = ice_reset_vf(sc, vf, true, false);
2991 		if (error != 0) {
2992 			device_printf(sc->dev,
2993 			    "Unable to isolate VF %u after mailbox overflow: %s\n",
2994 			    vf->vf_num, ice_err_str(error));
2995 		} else {
2996 			/*
2997 			 * Leave queues and mailbox requests blocked, but complete VFR
2998 			 * so a later physical FLR can create a new reset edge and
2999 			 * recover the VF.
3000 			 */
3001 			ice_iov_complete_vf_reset(sc, vf, false);
3002 		}
3003 	} else {
3004 		/* An incompletely configured VF has no queues to drain. */
3005 		reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_num));
3006 		reg |= VPGEN_VFRTRIG_VFSWR_M;
3007 		wr32(hw, VPGEN_VFRTRIG(vf->vf_num), reg);
3008 		ice_flush(hw);
3009 	}
3010 
3011 	return (true);
3012 }
3013 
3014 /**
3015  * ice_vc_handle_vf_msg - Handle a message from a VF
3016  * @sc: device private structure
3017  * @event: event received from the HW MBX queue
3018  * @mbx_data: software overflow-detection data, or NULL on E830
3019  *
3020  * Called whenever an event is received from a VF on the HW mailbox queue.
3021  * Responsible for handling these messages as well as responding to the
3022  * VF afterwards, depending on the received message type.
3023  */
3024 void
3025 ice_vc_handle_vf_msg(struct ice_softc *sc, struct ice_rq_event_info *event,
3026     struct ice_mbx_data *mbx_data)
3027 {
3028 	struct ice_hw *hw = &sc->hw;
3029 	device_t dev = sc->dev;
3030 	struct ice_vf *vf;
3031 	int err = 0;
3032 	u32 vf_flags;
3033 
3034 	u32 v_opcode = event->desc.cookie_high;
3035 	u16 v_id = event->desc.retval;
3036 	u8 *msg = event->msg_buf;
3037 	u16 msglen = event->msg_len;
3038 
3039 	if (v_id >= sc->num_vfs) {
3040 		device_printf(dev, "%s: Received msg from invalid VF-%d: opcode %d, len %d\n",
3041 		    __func__, v_id, v_opcode, msglen);
3042 		return;
3043 	}
3044 
3045 	vf = &sc->vfs[v_id];
3046 	if (ice_iov_mbx_overflow(sc, vf, mbx_data))
3047 		return;
3048 
3049 	/* Perform basic checks on the msg */
3050 	err = virtchnl_vc_validate_vf_msg(&vf->version, v_opcode, msg, msglen);
3051 	if (err) {
3052 		device_printf(dev, "%s: Received invalid msg from VF-%d: opcode %d, len %d, error %d\n",
3053 		    __func__, vf->vf_num, v_opcode, msglen, err);
3054 		ice_aq_send_msg_to_vf(hw, v_id, v_opcode, VIRTCHNL_STATUS_ERR_PARAM, NULL, 0, NULL);
3055 		return;
3056 	}
3057 
3058 	vf_flags = atomic_load_acq_32(&vf->vf_flags);
3059 	if ((vf_flags & VF_FLAG_ENABLED) == 0 || vf->vsi == NULL)
3060 		return;
3061 	/* Only a reset outside this dispatcher may release an isolated VF. */
3062 	if ((vf_flags & (VF_FLAG_MDD_BLOCKED | VF_FLAG_MBX_BLOCKED)) != 0)
3063 		return;
3064 
3065 	/*
3066 	 * Permit only reset negotiation while VF hardware state is unsafe.
3067 	 * A VFR can retry RESET_FAILED; REBUILD_REQUIRED needs a PF rebuild.
3068 	 */
3069 	if ((vf_flags & (VF_FLAG_REBUILD_REQUIRED | VF_FLAG_RESET_FAILED)) != 0 &&
3070 	    v_opcode != VIRTCHNL_OP_VERSION &&
3071 	    v_opcode != VIRTCHNL_OP_RESET_VF) {
3072 		ice_aq_send_msg_to_vf(hw, v_id, v_opcode,
3073 		    VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR, NULL, 0, NULL);
3074 		return;
3075 	}
3076 
3077 	switch (v_opcode) {
3078 	case VIRTCHNL_OP_VERSION:
3079 		ice_vc_version_msg(sc, vf, msg);
3080 		break;
3081 	case VIRTCHNL_OP_RESET_VF:
3082 		ice_reset_vf(sc, vf, true, true);
3083 		break;
3084 	case VIRTCHNL_OP_GET_VF_RESOURCES:
3085 		ice_vc_get_vf_res_msg(sc, vf, msg);
3086 		break;
3087 	case VIRTCHNL_OP_ADD_ETH_ADDR:
3088 		ice_vc_add_eth_addr_msg(sc, vf, msg);
3089 		break;
3090 	case VIRTCHNL_OP_DEL_ETH_ADDR:
3091 		ice_vc_del_eth_addr_msg(sc, vf, msg);
3092 		break;
3093 	case VIRTCHNL_OP_ADD_VLAN:
3094 		ice_vc_add_vlan_msg(sc, vf, msg);
3095 		break;
3096 	case VIRTCHNL_OP_DEL_VLAN:
3097 		ice_vc_del_vlan_msg(sc, vf, msg);
3098 		break;
3099 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3100 		ice_vc_cfg_vsi_qs_msg(sc, vf, msg);
3101 		break;
3102 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
3103 		ice_vc_cfg_rss_key_msg(sc, vf, msg);
3104 		break;
3105 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
3106 		ice_vc_cfg_rss_lut_msg(sc, vf, msg);
3107 		break;
3108 	case VIRTCHNL_OP_SET_RSS_HENA:
3109 		ice_vc_set_rss_hena_msg(sc, vf, msg);
3110 		break;
3111 	case VIRTCHNL_OP_ENABLE_QUEUES:
3112 		ice_vc_enable_queues_msg(sc, vf, msg);
3113 		ice_vc_notify_vf_link_state(sc, vf);
3114 		break;
3115 	case VIRTCHNL_OP_DISABLE_QUEUES:
3116 		ice_vc_disable_queues_msg(sc, vf, msg);
3117 		break;
3118 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3119 		ice_vc_cfg_irq_map_msg(sc, vf, msg);
3120 		break;
3121 	case VIRTCHNL_OP_GET_STATS:
3122 		ice_vc_get_stats_msg(sc, vf, msg);
3123 		break;
3124 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
3125 		ice_vc_cfg_promisc_mode_msg(sc, vf, msg);
3126 		break;
3127 	default:
3128 		device_printf(dev, "%s: Received unknown msg from VF-%d: opcode %d, len %d\n",
3129 		    __func__, vf->vf_num, v_opcode, msglen);
3130 		ice_aq_send_msg_to_vf(hw, v_id, v_opcode,
3131 		    VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL, 0, NULL);
3132 		break;
3133 	}
3134 }
3135 
3136 /**
3137  * ice_iov_setup_intr_mapping - Setup interrupt config for a VF
3138  * @sc: device softc structure
3139  * @vf: driver's VF structure for VF to be configured
3140  *
3141  * Before a VF can be used, and after a VF reset, the PF must configure
3142  * the VF's interrupt allocation registers. This includes allocating
3143  * interrupts from the PF's interrupt pool to the VF using the
3144  * VPINT_ALLOC(_PCI) registers, and setting up a mapping from PF vectors
3145  * to VF vectors in GLINT_VECT2FUNC.
3146  *
3147  * As well, this sets up queue allocation registers and maps the mailbox
3148  * interrupt for the VF.
3149  */
3150 static void
3151 ice_iov_setup_intr_mapping(struct ice_softc *sc, struct ice_vf *vf)
3152 {
3153 	struct ice_hw *hw = &sc->hw;
3154 	struct ice_vsi *vsi = vf->vsi;
3155 	u16 v;
3156 
3157 	/* Calculate indices for register ops below */
3158 	u16 vf_first_irq_idx = vf->vf_imap[0];
3159 	u16 vf_last_irq_idx = (vf_first_irq_idx + vf->num_irq_vectors) - 1;
3160 	u16 abs_vf_first_irq_idx = hw->func_caps.common_cap.msix_vector_first_id +
3161 	    vf_first_irq_idx;
3162 	u16 abs_vf_last_irq_idx = (abs_vf_first_irq_idx + vf->num_irq_vectors) - 1;
3163 	u16 abs_vf_num = vf->vf_num + hw->func_caps.vf_base_id;
3164 
3165 	/* Map out VF interrupt allocation in global device space. Both
3166 	 * VPINT_ALLOC and VPINT_ALLOC_PCI use the same values.
3167 	 */
3168 	wr32(hw, VPINT_ALLOC(vf->vf_num),
3169 	    (((abs_vf_first_irq_idx << VPINT_ALLOC_FIRST_S) & VPINT_ALLOC_FIRST_M) |
3170 	    ((abs_vf_last_irq_idx << VPINT_ALLOC_LAST_S) & VPINT_ALLOC_LAST_M) |
3171 	    VPINT_ALLOC_VALID_M));
3172 	wr32(hw, VPINT_ALLOC_PCI(vf->vf_num),
3173 	    (((abs_vf_first_irq_idx << VPINT_ALLOC_PCI_FIRST_S) & VPINT_ALLOC_PCI_FIRST_M) |
3174 	    ((abs_vf_last_irq_idx << VPINT_ALLOC_PCI_LAST_S) & VPINT_ALLOC_PCI_LAST_M) |
3175 	    VPINT_ALLOC_PCI_VALID_M));
3176 
3177 	/* Create inverse mapping of vectors to PF/VF combinations */
3178 	for (v = vf_first_irq_idx; v <= vf_last_irq_idx; v++)
3179 	{
3180 		wr32(hw, GLINT_VECT2FUNC(v),
3181 		    (((abs_vf_num << GLINT_VECT2FUNC_VF_NUM_S) & GLINT_VECT2FUNC_VF_NUM_M) |
3182 		     ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) & GLINT_VECT2FUNC_PF_NUM_M)));
3183 	}
3184 
3185 	/* Map mailbox interrupt to MSI-X index 0. Disable ITR for it, too. */
3186 	wr32(hw, VPINT_MBX_CTL(abs_vf_num),
3187 	    ((0 << VPINT_MBX_CTL_MSIX_INDX_S) & VPINT_MBX_CTL_MSIX_INDX_M) |
3188 	    ((0x3 << VPINT_MBX_CTL_ITR_INDX_S) & VPINT_MBX_CTL_ITR_INDX_M) |
3189 	    VPINT_MBX_CTL_CAUSE_ENA_M);
3190 
3191 	/* Mark the TX queue mapping registers as valid */
3192 	wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_num), VPLAN_TXQ_MAPENA_TX_ENA_M);
3193 
3194 	/* Indicate to HW that VF has scattered queue allocation */
3195 	wr32(hw, VPLAN_TX_QBASE(vf->vf_num), VPLAN_TX_QBASE_VFQTABLE_ENA_M);
3196 	for (int i = 0; i < vsi->num_tx_queues; i++) {
3197 		wr32(hw, VPLAN_TX_QTABLE(i, vf->vf_num),
3198 		    (vsi->tx_qmap[i] << VPLAN_TX_QTABLE_QINDEX_S) & VPLAN_TX_QTABLE_QINDEX_M);
3199 	}
3200 
3201 	/* Mark the RX queue mapping registers as valid */
3202 	wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_num), VPLAN_RXQ_MAPENA_RX_ENA_M);
3203 	wr32(hw, VPLAN_RX_QBASE(vf->vf_num), VPLAN_RX_QBASE_VFQTABLE_ENA_M);
3204 	for (int i = 0; i < vsi->num_rx_queues; i++) {
3205 		wr32(hw, VPLAN_RX_QTABLE(i, vf->vf_num),
3206 		    (vsi->rx_qmap[i] << VPLAN_RX_QTABLE_QINDEX_S) & VPLAN_RX_QTABLE_QINDEX_M);
3207 	}
3208 }
3209 
3210 /**
3211  * ice_err_to_virt err - translate ice errors into virtchnl errors
3212  * @ice_err: status returned from ice function
3213  */
3214 static enum virtchnl_status_code
3215 ice_iov_err_to_virt_err(int ice_err)
3216 {
3217 	switch (ice_err) {
3218 	case 0:
3219 		return VIRTCHNL_STATUS_SUCCESS;
3220 	case ICE_ERR_BAD_PTR:
3221 	case ICE_ERR_INVAL_SIZE:
3222 	case ICE_ERR_DEVICE_NOT_SUPPORTED:
3223 	case ICE_ERR_PARAM:
3224 	case ICE_ERR_CFG:
3225 		return VIRTCHNL_STATUS_ERR_PARAM;
3226 	case ICE_ERR_NO_MEMORY:
3227 		return VIRTCHNL_STATUS_ERR_NO_MEMORY;
3228 	case ICE_ERR_NOT_READY:
3229 	case ICE_ERR_RESET_FAILED:
3230 	case ICE_ERR_FW_API_VER:
3231 	case ICE_ERR_AQ_ERROR:
3232 	case ICE_ERR_AQ_TIMEOUT:
3233 	case ICE_ERR_AQ_FULL:
3234 	case ICE_ERR_AQ_NO_WORK:
3235 	case ICE_ERR_AQ_EMPTY:
3236 		return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
3237 	default:
3238 		return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
3239 	}
3240 }
3241