1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2022, Intel Corporation. */
3 
4 #include "ice_virtchnl.h"
5 #include "ice_vf_lib_private.h"
6 #include "ice.h"
7 #include "ice_base.h"
8 #include "ice_lib.h"
9 #include "ice_fltr.h"
10 #include "ice_virtchnl_allowlist.h"
11 #include "ice_vf_vsi_vlan_ops.h"
12 #include "ice_vlan.h"
13 #include "ice_flex_pipe.h"
14 #include "ice_dcb_lib.h"
15 
16 #define FIELD_SELECTOR(proto_hdr_field) \
17 		BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)
18 
19 struct ice_vc_hdr_match_type {
20 	u32 vc_hdr;	/* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
21 	u32 ice_hdr;	/* ice headers (ICE_FLOW_SEG_HDR_XXX) */
22 };
23 
24 static const struct ice_vc_hdr_match_type ice_vc_hdr_list[] = {
25 	{VIRTCHNL_PROTO_HDR_NONE,	ICE_FLOW_SEG_HDR_NONE},
26 	{VIRTCHNL_PROTO_HDR_ETH,	ICE_FLOW_SEG_HDR_ETH},
27 	{VIRTCHNL_PROTO_HDR_S_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
28 	{VIRTCHNL_PROTO_HDR_C_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
29 	{VIRTCHNL_PROTO_HDR_IPV4,	ICE_FLOW_SEG_HDR_IPV4 |
30 					ICE_FLOW_SEG_HDR_IPV_OTHER},
31 	{VIRTCHNL_PROTO_HDR_IPV6,	ICE_FLOW_SEG_HDR_IPV6 |
32 					ICE_FLOW_SEG_HDR_IPV_OTHER},
33 	{VIRTCHNL_PROTO_HDR_TCP,	ICE_FLOW_SEG_HDR_TCP},
34 	{VIRTCHNL_PROTO_HDR_UDP,	ICE_FLOW_SEG_HDR_UDP},
35 	{VIRTCHNL_PROTO_HDR_SCTP,	ICE_FLOW_SEG_HDR_SCTP},
36 	{VIRTCHNL_PROTO_HDR_PPPOE,	ICE_FLOW_SEG_HDR_PPPOE},
37 	{VIRTCHNL_PROTO_HDR_GTPU_IP,	ICE_FLOW_SEG_HDR_GTPU_IP},
38 	{VIRTCHNL_PROTO_HDR_GTPU_EH,	ICE_FLOW_SEG_HDR_GTPU_EH},
39 	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
40 					ICE_FLOW_SEG_HDR_GTPU_DWN},
41 	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
42 					ICE_FLOW_SEG_HDR_GTPU_UP},
43 	{VIRTCHNL_PROTO_HDR_L2TPV3,	ICE_FLOW_SEG_HDR_L2TPV3},
44 	{VIRTCHNL_PROTO_HDR_ESP,	ICE_FLOW_SEG_HDR_ESP},
45 	{VIRTCHNL_PROTO_HDR_AH,		ICE_FLOW_SEG_HDR_AH},
46 	{VIRTCHNL_PROTO_HDR_PFCP,	ICE_FLOW_SEG_HDR_PFCP_SESSION},
47 };
48 
49 struct ice_vc_hash_field_match_type {
50 	u32 vc_hdr;		/* virtchnl headers
51 				 * (VIRTCHNL_PROTO_HDR_XXX)
52 				 */
53 	u32 vc_hash_field;	/* virtchnl hash fields selector
54 				 * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
55 				 */
56 	u64 ice_hash_field;	/* ice hash fields
57 				 * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
58 				 */
59 };
60 
61 static const struct
62 ice_vc_hash_field_match_type ice_vc_hash_field_list[] = {
63 	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
64 		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
65 	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
66 		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
67 	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
68 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
69 		ICE_FLOW_HASH_ETH},
70 	{VIRTCHNL_PROTO_HDR_ETH,
71 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
72 		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
73 	{VIRTCHNL_PROTO_HDR_S_VLAN,
74 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
75 		BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
76 	{VIRTCHNL_PROTO_HDR_C_VLAN,
77 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
78 		BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
79 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
80 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
81 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
82 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
83 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
84 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
85 		ICE_FLOW_HASH_IPV4},
86 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
87 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
88 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
89 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
90 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
91 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
92 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
93 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
94 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
95 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
96 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
97 		ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
98 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
99 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
100 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
101 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
102 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
103 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
104 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
105 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
106 		ICE_FLOW_HASH_IPV6},
107 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
108 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
109 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
110 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
111 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
112 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
113 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
114 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
115 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
116 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
117 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
118 		ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
119 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
120 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
121 	{VIRTCHNL_PROTO_HDR_TCP,
122 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
123 		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
124 	{VIRTCHNL_PROTO_HDR_TCP,
125 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
126 		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
127 	{VIRTCHNL_PROTO_HDR_TCP,
128 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
129 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
130 		ICE_FLOW_HASH_TCP_PORT},
131 	{VIRTCHNL_PROTO_HDR_UDP,
132 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
133 		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
134 	{VIRTCHNL_PROTO_HDR_UDP,
135 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
136 		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
137 	{VIRTCHNL_PROTO_HDR_UDP,
138 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
139 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
140 		ICE_FLOW_HASH_UDP_PORT},
141 	{VIRTCHNL_PROTO_HDR_SCTP,
142 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
143 		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
144 	{VIRTCHNL_PROTO_HDR_SCTP,
145 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
146 		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
147 	{VIRTCHNL_PROTO_HDR_SCTP,
148 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
149 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
150 		ICE_FLOW_HASH_SCTP_PORT},
151 	{VIRTCHNL_PROTO_HDR_PPPOE,
152 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
153 		BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
154 	{VIRTCHNL_PROTO_HDR_GTPU_IP,
155 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
156 		BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
157 	{VIRTCHNL_PROTO_HDR_L2TPV3,
158 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
159 		BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
160 	{VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
161 		BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
162 	{VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
163 		BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
164 	{VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
165 		BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
166 };
167 
168 /**
169  * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
170  * @pf: pointer to the PF structure
171  * @v_opcode: operation code
172  * @v_retval: return value
173  * @msg: pointer to the msg buffer
174  * @msglen: msg length
175  */
176 static void
177 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
178 		    enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
179 {
180 	struct ice_hw *hw = &pf->hw;
181 	struct ice_vf *vf;
182 	unsigned int bkt;
183 
184 	mutex_lock(&pf->vfs.table_lock);
185 	ice_for_each_vf(pf, bkt, vf) {
186 		/* Not all vfs are enabled so skip the ones that are not */
187 		if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
188 		    !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
189 			continue;
190 
191 		/* Ignore return value on purpose - a given VF may fail, but
192 		 * we need to keep going and send to all of them
193 		 */
194 		ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
195 				      msglen, NULL);
196 	}
197 	mutex_unlock(&pf->vfs.table_lock);
198 }
199 
200 /**
201  * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
202  * @vf: pointer to the VF structure
203  * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
204  * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
205  * @link_up: whether or not to set the link up/down
206  */
207 static void
208 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
209 		 int ice_link_speed, bool link_up)
210 {
211 	if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
212 		pfe->event_data.link_event_adv.link_status = link_up;
213 		/* Speed in Mbps */
214 		pfe->event_data.link_event_adv.link_speed =
215 			ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
216 	} else {
217 		pfe->event_data.link_event.link_status = link_up;
218 		/* Legacy method for virtchnl link speeds */
219 		pfe->event_data.link_event.link_speed =
220 			(enum virtchnl_link_speed)
221 			ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
222 	}
223 }
224 
225 /**
226  * ice_vc_notify_vf_link_state - Inform a VF of link status
227  * @vf: pointer to the VF structure
228  *
229  * send a link status message to a single VF
230  */
231 void ice_vc_notify_vf_link_state(struct ice_vf *vf)
232 {
233 	struct virtchnl_pf_event pfe = { 0 };
234 	struct ice_hw *hw = &vf->pf->hw;
235 
236 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
237 	pfe.severity = PF_EVENT_SEVERITY_INFO;
238 
239 	if (ice_is_vf_link_up(vf))
240 		ice_set_pfe_link(vf, &pfe,
241 				 hw->port_info->phy.link_info.link_speed, true);
242 	else
243 		ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
244 
245 	ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
246 			      VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
247 			      sizeof(pfe), NULL);
248 }
249 
250 /**
251  * ice_vc_notify_link_state - Inform all VFs on a PF of link status
252  * @pf: pointer to the PF structure
253  */
254 void ice_vc_notify_link_state(struct ice_pf *pf)
255 {
256 	struct ice_vf *vf;
257 	unsigned int bkt;
258 
259 	mutex_lock(&pf->vfs.table_lock);
260 	ice_for_each_vf(pf, bkt, vf)
261 		ice_vc_notify_vf_link_state(vf);
262 	mutex_unlock(&pf->vfs.table_lock);
263 }
264 
265 /**
266  * ice_vc_notify_reset - Send pending reset message to all VFs
267  * @pf: pointer to the PF structure
268  *
269  * indicate a pending reset to all VFs on a given PF
270  */
271 void ice_vc_notify_reset(struct ice_pf *pf)
272 {
273 	struct virtchnl_pf_event pfe;
274 
275 	if (!ice_has_vfs(pf))
276 		return;
277 
278 	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
279 	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
280 	ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
281 			    (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
282 }
283 
284 /**
285  * ice_vc_send_msg_to_vf - Send message to VF
286  * @vf: pointer to the VF info
287  * @v_opcode: virtual channel opcode
288  * @v_retval: virtual channel return value
289  * @msg: pointer to the msg buffer
290  * @msglen: msg length
291  *
292  * send msg to VF
293  */
294 int
295 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
296 		      enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
297 {
298 	struct device *dev;
299 	struct ice_pf *pf;
300 	int aq_ret;
301 
302 	pf = vf->pf;
303 	dev = ice_pf_to_dev(pf);
304 
305 	aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
306 				       msg, msglen, NULL);
307 	if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
308 		dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
309 			 vf->vf_id, aq_ret,
310 			 ice_aq_str(pf->hw.mailboxq.sq_last_status));
311 		return -EIO;
312 	}
313 
314 	return 0;
315 }
316 
317 /**
318  * ice_vc_get_ver_msg
319  * @vf: pointer to the VF info
320  * @msg: pointer to the msg buffer
321  *
322  * called from the VF to request the API version used by the PF
323  */
324 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
325 {
326 	struct virtchnl_version_info info = {
327 		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
328 	};
329 
330 	vf->vf_ver = *(struct virtchnl_version_info *)msg;
331 	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
332 	if (VF_IS_V10(&vf->vf_ver))
333 		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
334 
335 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
336 				     VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
337 				     sizeof(struct virtchnl_version_info));
338 }
339 
340 /**
341  * ice_vc_get_max_frame_size - get max frame size allowed for VF
342  * @vf: VF used to determine max frame size
343  *
344  * Max frame size is determined based on the current port's max frame size and
345  * whether a port VLAN is configured on this VF. The VF is not aware whether
346  * it's in a port VLAN so the PF needs to account for this in max frame size
347  * checks and sending the max frame size to the VF.
348  */
349 static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
350 {
351 	struct ice_port_info *pi = ice_vf_get_port_info(vf);
352 	u16 max_frame_size;
353 
354 	max_frame_size = pi->phy.link_info.max_frame_size;
355 
356 	if (ice_vf_is_port_vlan_ena(vf))
357 		max_frame_size -= VLAN_HLEN;
358 
359 	return max_frame_size;
360 }
361 
362 /**
363  * ice_vc_get_vlan_caps
364  * @hw: pointer to the hw
365  * @vf: pointer to the VF info
366  * @vsi: pointer to the VSI
367  * @driver_caps: current driver caps
368  *
369  * Return 0 if there is no VLAN caps supported, or VLAN caps value
370  */
371 static u32
372 ice_vc_get_vlan_caps(struct ice_hw *hw, struct ice_vf *vf, struct ice_vsi *vsi,
373 		     u32 driver_caps)
374 {
375 	if (ice_is_eswitch_mode_switchdev(vf->pf))
376 		/* In switchdev setting VLAN from VF isn't supported */
377 		return 0;
378 
379 	if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
380 		/* VLAN offloads based on current device configuration */
381 		return VIRTCHNL_VF_OFFLOAD_VLAN_V2;
382 	} else if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN) {
383 		/* allow VF to negotiate VIRTCHNL_VF_OFFLOAD explicitly for
384 		 * these two conditions, which amounts to guest VLAN filtering
385 		 * and offloads being based on the inner VLAN or the
386 		 * inner/single VLAN respectively and don't allow VF to
387 		 * negotiate VIRTCHNL_VF_OFFLOAD in any other cases
388 		 */
389 		if (ice_is_dvm_ena(hw) && ice_vf_is_port_vlan_ena(vf)) {
390 			return VIRTCHNL_VF_OFFLOAD_VLAN;
391 		} else if (!ice_is_dvm_ena(hw) &&
392 			   !ice_vf_is_port_vlan_ena(vf)) {
393 			/* configure backward compatible support for VFs that
394 			 * only support VIRTCHNL_VF_OFFLOAD_VLAN, the PF is
395 			 * configured in SVM, and no port VLAN is configured
396 			 */
397 			ice_vf_vsi_cfg_svm_legacy_vlan_mode(vsi);
398 			return VIRTCHNL_VF_OFFLOAD_VLAN;
399 		} else if (ice_is_dvm_ena(hw)) {
400 			/* configure software offloaded VLAN support when DVM
401 			 * is enabled, but no port VLAN is enabled
402 			 */
403 			ice_vf_vsi_cfg_dvm_legacy_vlan_mode(vsi);
404 		}
405 	}
406 
407 	return 0;
408 }
409 
410 /**
411  * ice_vc_get_vf_res_msg
412  * @vf: pointer to the VF info
413  * @msg: pointer to the msg buffer
414  *
415  * called from the VF to request its resources
416  */
417 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
418 {
419 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
420 	struct virtchnl_vf_resource *vfres = NULL;
421 	struct ice_hw *hw = &vf->pf->hw;
422 	struct ice_vsi *vsi;
423 	int len = 0;
424 	int ret;
425 
426 	if (ice_check_vf_init(vf)) {
427 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
428 		goto err;
429 	}
430 
431 	len = virtchnl_struct_size(vfres, vsi_res, 0);
432 
433 	vfres = kzalloc(len, GFP_KERNEL);
434 	if (!vfres) {
435 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
436 		len = 0;
437 		goto err;
438 	}
439 	if (VF_IS_V11(&vf->vf_ver))
440 		vf->driver_caps = *(u32 *)msg;
441 	else
442 		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
443 				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
444 				  VIRTCHNL_VF_OFFLOAD_VLAN;
445 
446 	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
447 	vsi = ice_get_vf_vsi(vf);
448 	if (!vsi) {
449 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
450 		goto err;
451 	}
452 
453 	vfres->vf_cap_flags |= ice_vc_get_vlan_caps(hw, vf, vsi,
454 						    vf->driver_caps);
455 
456 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
457 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
458 	} else {
459 		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
460 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
461 		else
462 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
463 	}
464 
465 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)
466 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC;
467 
468 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
469 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
470 
471 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
472 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
473 
474 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
475 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
476 
477 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
478 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
479 
480 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
481 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
482 
483 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
484 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
485 
486 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
487 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
488 
489 	if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
490 		vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
491 
492 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF)
493 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
494 
495 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO)
496 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_USO;
497 
498 	vfres->num_vsis = 1;
499 	/* Tx and Rx queue are equal for VF */
500 	vfres->num_queue_pairs = vsi->num_txq;
501 	vfres->max_vectors = vf->pf->vfs.num_msix_per;
502 	vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
503 	vfres->rss_lut_size = ICE_LUT_VSI_SIZE;
504 	vfres->max_mtu = ice_vc_get_max_frame_size(vf);
505 
506 	vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
507 	vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
508 	vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
509 	ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
510 			vf->hw_lan_addr);
511 
512 	/* match guest capabilities */
513 	vf->driver_caps = vfres->vf_cap_flags;
514 
515 	ice_vc_set_caps_allowlist(vf);
516 	ice_vc_set_working_allowlist(vf);
517 
518 	set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
519 
520 err:
521 	/* send the response back to the VF */
522 	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, v_ret,
523 				    (u8 *)vfres, len);
524 
525 	kfree(vfres);
526 	return ret;
527 }
528 
529 /**
530  * ice_vc_reset_vf_msg
531  * @vf: pointer to the VF info
532  *
533  * called from the VF to reset itself,
534  * unlike other virtchnl messages, PF driver
535  * doesn't send the response back to the VF
536  */
537 static void ice_vc_reset_vf_msg(struct ice_vf *vf)
538 {
539 	if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
540 		ice_reset_vf(vf, 0);
541 }
542 
543 /**
544  * ice_vc_isvalid_vsi_id
545  * @vf: pointer to the VF info
546  * @vsi_id: VF relative VSI ID
547  *
548  * check for the valid VSI ID
549  */
550 bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
551 {
552 	struct ice_pf *pf = vf->pf;
553 	struct ice_vsi *vsi;
554 
555 	vsi = ice_find_vsi(pf, vsi_id);
556 
557 	return (vsi && (vsi->vf == vf));
558 }
559 
560 /**
561  * ice_vc_isvalid_q_id
562  * @vf: pointer to the VF info
563  * @vsi_id: VSI ID
564  * @qid: VSI relative queue ID
565  *
566  * check for the valid queue ID
567  */
568 static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
569 {
570 	struct ice_vsi *vsi = ice_find_vsi(vf->pf, vsi_id);
571 	/* allocated Tx and Rx queues should be always equal for VF VSI */
572 	return (vsi && (qid < vsi->alloc_txq));
573 }
574 
575 /**
576  * ice_vc_isvalid_ring_len
577  * @ring_len: length of ring
578  *
579  * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
580  * or zero
581  */
582 static bool ice_vc_isvalid_ring_len(u16 ring_len)
583 {
584 	return ring_len == 0 ||
585 	       (ring_len >= ICE_MIN_NUM_DESC &&
586 		ring_len <= ICE_MAX_NUM_DESC &&
587 		!(ring_len % ICE_REQ_DESC_MULTIPLE));
588 }
589 
590 /**
591  * ice_vc_validate_pattern
592  * @vf: pointer to the VF info
593  * @proto: virtchnl protocol headers
594  *
595  * validate the pattern is supported or not.
596  *
597  * Return: true on success, false on error.
598  */
599 bool
600 ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto)
601 {
602 	bool is_ipv4 = false;
603 	bool is_ipv6 = false;
604 	bool is_udp = false;
605 	u16 ptype = -1;
606 	int i = 0;
607 
608 	while (i < proto->count &&
609 	       proto->proto_hdr[i].type != VIRTCHNL_PROTO_HDR_NONE) {
610 		switch (proto->proto_hdr[i].type) {
611 		case VIRTCHNL_PROTO_HDR_ETH:
612 			ptype = ICE_PTYPE_MAC_PAY;
613 			break;
614 		case VIRTCHNL_PROTO_HDR_IPV4:
615 			ptype = ICE_PTYPE_IPV4_PAY;
616 			is_ipv4 = true;
617 			break;
618 		case VIRTCHNL_PROTO_HDR_IPV6:
619 			ptype = ICE_PTYPE_IPV6_PAY;
620 			is_ipv6 = true;
621 			break;
622 		case VIRTCHNL_PROTO_HDR_UDP:
623 			if (is_ipv4)
624 				ptype = ICE_PTYPE_IPV4_UDP_PAY;
625 			else if (is_ipv6)
626 				ptype = ICE_PTYPE_IPV6_UDP_PAY;
627 			is_udp = true;
628 			break;
629 		case VIRTCHNL_PROTO_HDR_TCP:
630 			if (is_ipv4)
631 				ptype = ICE_PTYPE_IPV4_TCP_PAY;
632 			else if (is_ipv6)
633 				ptype = ICE_PTYPE_IPV6_TCP_PAY;
634 			break;
635 		case VIRTCHNL_PROTO_HDR_SCTP:
636 			if (is_ipv4)
637 				ptype = ICE_PTYPE_IPV4_SCTP_PAY;
638 			else if (is_ipv6)
639 				ptype = ICE_PTYPE_IPV6_SCTP_PAY;
640 			break;
641 		case VIRTCHNL_PROTO_HDR_GTPU_IP:
642 		case VIRTCHNL_PROTO_HDR_GTPU_EH:
643 			if (is_ipv4)
644 				ptype = ICE_MAC_IPV4_GTPU;
645 			else if (is_ipv6)
646 				ptype = ICE_MAC_IPV6_GTPU;
647 			goto out;
648 		case VIRTCHNL_PROTO_HDR_L2TPV3:
649 			if (is_ipv4)
650 				ptype = ICE_MAC_IPV4_L2TPV3;
651 			else if (is_ipv6)
652 				ptype = ICE_MAC_IPV6_L2TPV3;
653 			goto out;
654 		case VIRTCHNL_PROTO_HDR_ESP:
655 			if (is_ipv4)
656 				ptype = is_udp ? ICE_MAC_IPV4_NAT_T_ESP :
657 						ICE_MAC_IPV4_ESP;
658 			else if (is_ipv6)
659 				ptype = is_udp ? ICE_MAC_IPV6_NAT_T_ESP :
660 						ICE_MAC_IPV6_ESP;
661 			goto out;
662 		case VIRTCHNL_PROTO_HDR_AH:
663 			if (is_ipv4)
664 				ptype = ICE_MAC_IPV4_AH;
665 			else if (is_ipv6)
666 				ptype = ICE_MAC_IPV6_AH;
667 			goto out;
668 		case VIRTCHNL_PROTO_HDR_PFCP:
669 			if (is_ipv4)
670 				ptype = ICE_MAC_IPV4_PFCP_SESSION;
671 			else if (is_ipv6)
672 				ptype = ICE_MAC_IPV6_PFCP_SESSION;
673 			goto out;
674 		default:
675 			break;
676 		}
677 		i++;
678 	}
679 
680 out:
681 	return ice_hw_ptype_ena(&vf->pf->hw, ptype);
682 }
683 
684 /**
685  * ice_vc_parse_rss_cfg - parses hash fields and headers from
686  * a specific virtchnl RSS cfg
687  * @hw: pointer to the hardware
688  * @rss_cfg: pointer to the virtchnl RSS cfg
689  * @addl_hdrs: pointer to the protocol header fields (ICE_FLOW_SEG_HDR_*)
690  * to configure
691  * @hash_flds: pointer to the hash bit fields (ICE_FLOW_HASH_*) to configure
692  *
693  * Return true if all the protocol header and hash fields in the RSS cfg could
694  * be parsed, else return false
695  *
696  * This function parses the virtchnl RSS cfg to be the intended
697  * hash fields and the intended header for RSS configuration
698  */
699 static bool
700 ice_vc_parse_rss_cfg(struct ice_hw *hw, struct virtchnl_rss_cfg *rss_cfg,
701 		     u32 *addl_hdrs, u64 *hash_flds)
702 {
703 	const struct ice_vc_hash_field_match_type *hf_list;
704 	const struct ice_vc_hdr_match_type *hdr_list;
705 	int i, hf_list_len, hdr_list_len;
706 
707 	hf_list = ice_vc_hash_field_list;
708 	hf_list_len = ARRAY_SIZE(ice_vc_hash_field_list);
709 	hdr_list = ice_vc_hdr_list;
710 	hdr_list_len = ARRAY_SIZE(ice_vc_hdr_list);
711 
712 	for (i = 0; i < rss_cfg->proto_hdrs.count; i++) {
713 		struct virtchnl_proto_hdr *proto_hdr =
714 					&rss_cfg->proto_hdrs.proto_hdr[i];
715 		bool hdr_found = false;
716 		int j;
717 
718 		/* Find matched ice headers according to virtchnl headers. */
719 		for (j = 0; j < hdr_list_len; j++) {
720 			struct ice_vc_hdr_match_type hdr_map = hdr_list[j];
721 
722 			if (proto_hdr->type == hdr_map.vc_hdr) {
723 				*addl_hdrs |= hdr_map.ice_hdr;
724 				hdr_found = true;
725 			}
726 		}
727 
728 		if (!hdr_found)
729 			return false;
730 
731 		/* Find matched ice hash fields according to
732 		 * virtchnl hash fields.
733 		 */
734 		for (j = 0; j < hf_list_len; j++) {
735 			struct ice_vc_hash_field_match_type hf_map = hf_list[j];
736 
737 			if (proto_hdr->type == hf_map.vc_hdr &&
738 			    proto_hdr->field_selector == hf_map.vc_hash_field) {
739 				*hash_flds |= hf_map.ice_hash_field;
740 				break;
741 			}
742 		}
743 	}
744 
745 	return true;
746 }
747 
748 /**
749  * ice_vf_adv_rss_offload_ena - determine if capabilities support advanced
750  * RSS offloads
751  * @caps: VF driver negotiated capabilities
752  *
753  * Return true if VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF capability is set,
754  * else return false
755  */
756 static bool ice_vf_adv_rss_offload_ena(u32 caps)
757 {
758 	return !!(caps & VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF);
759 }
760 
761 /**
762  * ice_vc_handle_rss_cfg
763  * @vf: pointer to the VF info
764  * @msg: pointer to the message buffer
765  * @add: add a RSS config if true, otherwise delete a RSS config
766  *
767  * This function adds/deletes a RSS config
768  */
769 static int ice_vc_handle_rss_cfg(struct ice_vf *vf, u8 *msg, bool add)
770 {
771 	u32 v_opcode = add ? VIRTCHNL_OP_ADD_RSS_CFG : VIRTCHNL_OP_DEL_RSS_CFG;
772 	struct virtchnl_rss_cfg *rss_cfg = (struct virtchnl_rss_cfg *)msg;
773 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
774 	struct device *dev = ice_pf_to_dev(vf->pf);
775 	struct ice_hw *hw = &vf->pf->hw;
776 	struct ice_vsi *vsi;
777 
778 	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
779 		dev_dbg(dev, "VF %d attempting to configure RSS, but RSS is not supported by the PF\n",
780 			vf->vf_id);
781 		v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
782 		goto error_param;
783 	}
784 
785 	if (!ice_vf_adv_rss_offload_ena(vf->driver_caps)) {
786 		dev_dbg(dev, "VF %d attempting to configure RSS, but Advanced RSS offload is not supported\n",
787 			vf->vf_id);
788 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
789 		goto error_param;
790 	}
791 
792 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
793 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
794 		goto error_param;
795 	}
796 
797 	if (rss_cfg->proto_hdrs.count > VIRTCHNL_MAX_NUM_PROTO_HDRS ||
798 	    rss_cfg->rss_algorithm < VIRTCHNL_RSS_ALG_TOEPLITZ_ASYMMETRIC ||
799 	    rss_cfg->rss_algorithm > VIRTCHNL_RSS_ALG_XOR_SYMMETRIC) {
800 		dev_dbg(dev, "VF %d attempting to configure RSS, but RSS configuration is not valid\n",
801 			vf->vf_id);
802 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
803 		goto error_param;
804 	}
805 
806 	vsi = ice_get_vf_vsi(vf);
807 	if (!vsi) {
808 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
809 		goto error_param;
810 	}
811 
812 	if (!ice_vc_validate_pattern(vf, &rss_cfg->proto_hdrs)) {
813 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
814 		goto error_param;
815 	}
816 
817 	if (rss_cfg->rss_algorithm == VIRTCHNL_RSS_ALG_R_ASYMMETRIC) {
818 		struct ice_vsi_ctx *ctx;
819 		u8 lut_type, hash_type;
820 		int status;
821 
822 		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
823 		hash_type = add ? ICE_AQ_VSI_Q_OPT_RSS_HASH_XOR :
824 				ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ;
825 
826 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
827 		if (!ctx) {
828 			v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
829 			goto error_param;
830 		}
831 
832 		ctx->info.q_opt_rss =
833 			FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_LUT_M, lut_type) |
834 			FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hash_type);
835 
836 		/* Preserve existing queueing option setting */
837 		ctx->info.q_opt_rss |= (vsi->info.q_opt_rss &
838 					  ICE_AQ_VSI_Q_OPT_RSS_GBL_LUT_M);
839 		ctx->info.q_opt_tc = vsi->info.q_opt_tc;
840 		ctx->info.q_opt_flags = vsi->info.q_opt_rss;
841 
842 		ctx->info.valid_sections =
843 				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
844 
845 		status = ice_update_vsi(hw, vsi->idx, ctx, NULL);
846 		if (status) {
847 			dev_err(dev, "update VSI for RSS failed, err %d aq_err %s\n",
848 				status, ice_aq_str(hw->adminq.sq_last_status));
849 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
850 		} else {
851 			vsi->info.q_opt_rss = ctx->info.q_opt_rss;
852 		}
853 
854 		kfree(ctx);
855 	} else {
856 		u32 addl_hdrs = ICE_FLOW_SEG_HDR_NONE;
857 		u64 hash_flds = ICE_HASH_INVALID;
858 
859 		if (!ice_vc_parse_rss_cfg(hw, rss_cfg, &addl_hdrs,
860 					  &hash_flds)) {
861 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
862 			goto error_param;
863 		}
864 
865 		if (add) {
866 			if (ice_add_rss_cfg(hw, vsi->idx, hash_flds,
867 					    addl_hdrs)) {
868 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
869 				dev_err(dev, "ice_add_rss_cfg failed for vsi = %d, v_ret = %d\n",
870 					vsi->vsi_num, v_ret);
871 			}
872 		} else {
873 			int status;
874 
875 			status = ice_rem_rss_cfg(hw, vsi->idx, hash_flds,
876 						 addl_hdrs);
877 			/* We just ignore -ENOENT, because if two configurations
878 			 * share the same profile remove one of them actually
879 			 * removes both, since the profile is deleted.
880 			 */
881 			if (status && status != -ENOENT) {
882 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
883 				dev_err(dev, "ice_rem_rss_cfg failed for VF ID:%d, error:%d\n",
884 					vf->vf_id, status);
885 			}
886 		}
887 	}
888 
889 error_param:
890 	return ice_vc_send_msg_to_vf(vf, v_opcode, v_ret, NULL, 0);
891 }
892 
893 /**
894  * ice_vc_config_rss_key
895  * @vf: pointer to the VF info
896  * @msg: pointer to the msg buffer
897  *
898  * Configure the VF's RSS key
899  */
900 static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
901 {
902 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
903 	struct virtchnl_rss_key *vrk =
904 		(struct virtchnl_rss_key *)msg;
905 	struct ice_vsi *vsi;
906 
907 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
908 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
909 		goto error_param;
910 	}
911 
912 	if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
913 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
914 		goto error_param;
915 	}
916 
917 	if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
918 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
919 		goto error_param;
920 	}
921 
922 	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
923 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
924 		goto error_param;
925 	}
926 
927 	vsi = ice_get_vf_vsi(vf);
928 	if (!vsi) {
929 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
930 		goto error_param;
931 	}
932 
933 	if (ice_set_rss_key(vsi, vrk->key))
934 		v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
935 error_param:
936 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, v_ret,
937 				     NULL, 0);
938 }
939 
940 /**
941  * ice_vc_config_rss_lut
942  * @vf: pointer to the VF info
943  * @msg: pointer to the msg buffer
944  *
945  * Configure the VF's RSS LUT
946  */
947 static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
948 {
949 	struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
950 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
951 	struct ice_vsi *vsi;
952 
953 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
954 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
955 		goto error_param;
956 	}
957 
958 	if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
959 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
960 		goto error_param;
961 	}
962 
963 	if (vrl->lut_entries != ICE_LUT_VSI_SIZE) {
964 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
965 		goto error_param;
966 	}
967 
968 	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
969 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
970 		goto error_param;
971 	}
972 
973 	vsi = ice_get_vf_vsi(vf);
974 	if (!vsi) {
975 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
976 		goto error_param;
977 	}
978 
979 	if (ice_set_rss_lut(vsi, vrl->lut, ICE_LUT_VSI_SIZE))
980 		v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
981 error_param:
982 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, v_ret,
983 				     NULL, 0);
984 }
985 
986 /**
987  * ice_vc_cfg_promiscuous_mode_msg
988  * @vf: pointer to the VF info
989  * @msg: pointer to the msg buffer
990  *
991  * called from the VF to configure VF VSIs promiscuous mode
992  */
993 static int ice_vc_cfg_promiscuous_mode_msg(struct ice_vf *vf, u8 *msg)
994 {
995 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
996 	bool rm_promisc, alluni = false, allmulti = false;
997 	struct virtchnl_promisc_info *info =
998 	    (struct virtchnl_promisc_info *)msg;
999 	struct ice_vsi_vlan_ops *vlan_ops;
1000 	int mcast_err = 0, ucast_err = 0;
1001 	struct ice_pf *pf = vf->pf;
1002 	struct ice_vsi *vsi;
1003 	u8 mcast_m, ucast_m;
1004 	struct device *dev;
1005 	int ret = 0;
1006 
1007 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1008 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1009 		goto error_param;
1010 	}
1011 
1012 	if (!ice_vc_isvalid_vsi_id(vf, info->vsi_id)) {
1013 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1014 		goto error_param;
1015 	}
1016 
1017 	vsi = ice_get_vf_vsi(vf);
1018 	if (!vsi) {
1019 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1020 		goto error_param;
1021 	}
1022 
1023 	dev = ice_pf_to_dev(pf);
1024 	if (!ice_is_vf_trusted(vf)) {
1025 		dev_err(dev, "Unprivileged VF %d is attempting to configure promiscuous mode\n",
1026 			vf->vf_id);
1027 		/* Leave v_ret alone, lie to the VF on purpose. */
1028 		goto error_param;
1029 	}
1030 
1031 	if (info->flags & FLAG_VF_UNICAST_PROMISC)
1032 		alluni = true;
1033 
1034 	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
1035 		allmulti = true;
1036 
1037 	rm_promisc = !allmulti && !alluni;
1038 
1039 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1040 	if (rm_promisc)
1041 		ret = vlan_ops->ena_rx_filtering(vsi);
1042 	else
1043 		ret = vlan_ops->dis_rx_filtering(vsi);
1044 	if (ret) {
1045 		dev_err(dev, "Failed to configure VLAN pruning in promiscuous mode\n");
1046 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1047 		goto error_param;
1048 	}
1049 
1050 	ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
1051 
1052 	if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
1053 		if (alluni) {
1054 			/* in this case we're turning on promiscuous mode */
1055 			ret = ice_set_dflt_vsi(vsi);
1056 		} else {
1057 			/* in this case we're turning off promiscuous mode */
1058 			if (ice_is_dflt_vsi_in_use(vsi->port_info))
1059 				ret = ice_clear_dflt_vsi(vsi);
1060 		}
1061 
1062 		/* in this case we're turning on/off only
1063 		 * allmulticast
1064 		 */
1065 		if (allmulti)
1066 			mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1067 		else
1068 			mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1069 
1070 		if (ret) {
1071 			dev_err(dev, "Turning on/off promiscuous mode for VF %d failed, error: %d\n",
1072 				vf->vf_id, ret);
1073 			v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1074 			goto error_param;
1075 		}
1076 	} else {
1077 		if (alluni)
1078 			ucast_err = ice_vf_set_vsi_promisc(vf, vsi, ucast_m);
1079 		else
1080 			ucast_err = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
1081 
1082 		if (allmulti)
1083 			mcast_err = ice_vf_set_vsi_promisc(vf, vsi, mcast_m);
1084 		else
1085 			mcast_err = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
1086 
1087 		if (ucast_err || mcast_err)
1088 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1089 	}
1090 
1091 	if (!mcast_err) {
1092 		if (allmulti &&
1093 		    !test_and_set_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
1094 			dev_info(dev, "VF %u successfully set multicast promiscuous mode\n",
1095 				 vf->vf_id);
1096 		else if (!allmulti &&
1097 			 test_and_clear_bit(ICE_VF_STATE_MC_PROMISC,
1098 					    vf->vf_states))
1099 			dev_info(dev, "VF %u successfully unset multicast promiscuous mode\n",
1100 				 vf->vf_id);
1101 	} else {
1102 		dev_err(dev, "Error while modifying multicast promiscuous mode for VF %u, error: %d\n",
1103 			vf->vf_id, mcast_err);
1104 	}
1105 
1106 	if (!ucast_err) {
1107 		if (alluni &&
1108 		    !test_and_set_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
1109 			dev_info(dev, "VF %u successfully set unicast promiscuous mode\n",
1110 				 vf->vf_id);
1111 		else if (!alluni &&
1112 			 test_and_clear_bit(ICE_VF_STATE_UC_PROMISC,
1113 					    vf->vf_states))
1114 			dev_info(dev, "VF %u successfully unset unicast promiscuous mode\n",
1115 				 vf->vf_id);
1116 	} else {
1117 		dev_err(dev, "Error while modifying unicast promiscuous mode for VF %u, error: %d\n",
1118 			vf->vf_id, ucast_err);
1119 	}
1120 
1121 error_param:
1122 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1123 				     v_ret, NULL, 0);
1124 }
1125 
1126 /**
1127  * ice_vc_get_stats_msg
1128  * @vf: pointer to the VF info
1129  * @msg: pointer to the msg buffer
1130  *
1131  * called from the VF to get VSI stats
1132  */
1133 static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
1134 {
1135 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1136 	struct virtchnl_queue_select *vqs =
1137 		(struct virtchnl_queue_select *)msg;
1138 	struct ice_eth_stats stats = { 0 };
1139 	struct ice_vsi *vsi;
1140 
1141 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1142 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1143 		goto error_param;
1144 	}
1145 
1146 	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1147 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1148 		goto error_param;
1149 	}
1150 
1151 	vsi = ice_get_vf_vsi(vf);
1152 	if (!vsi) {
1153 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1154 		goto error_param;
1155 	}
1156 
1157 	ice_update_eth_stats(vsi);
1158 
1159 	stats = vsi->eth_stats;
1160 
1161 error_param:
1162 	/* send the response to the VF */
1163 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, v_ret,
1164 				     (u8 *)&stats, sizeof(stats));
1165 }
1166 
1167 /**
1168  * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
1169  * @vqs: virtchnl_queue_select structure containing bitmaps to validate
1170  *
1171  * Return true on successful validation, else false
1172  */
1173 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
1174 {
1175 	if ((!vqs->rx_queues && !vqs->tx_queues) ||
1176 	    vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
1177 	    vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
1178 		return false;
1179 
1180 	return true;
1181 }
1182 
1183 /**
1184  * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
1185  * @vsi: VSI of the VF to configure
1186  * @q_idx: VF queue index used to determine the queue in the PF's space
1187  */
1188 static void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1189 {
1190 	struct ice_hw *hw = &vsi->back->hw;
1191 	u32 pfq = vsi->txq_map[q_idx];
1192 	u32 reg;
1193 
1194 	reg = rd32(hw, QINT_TQCTL(pfq));
1195 
1196 	/* MSI-X index 0 in the VF's space is always for the OICR, which means
1197 	 * this is most likely a poll mode VF driver, so don't enable an
1198 	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1199 	 */
1200 	if (!(reg & QINT_TQCTL_MSIX_INDX_M))
1201 		return;
1202 
1203 	wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
1204 }
1205 
1206 /**
1207  * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
1208  * @vsi: VSI of the VF to configure
1209  * @q_idx: VF queue index used to determine the queue in the PF's space
1210  */
1211 static void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
1212 {
1213 	struct ice_hw *hw = &vsi->back->hw;
1214 	u32 pfq = vsi->rxq_map[q_idx];
1215 	u32 reg;
1216 
1217 	reg = rd32(hw, QINT_RQCTL(pfq));
1218 
1219 	/* MSI-X index 0 in the VF's space is always for the OICR, which means
1220 	 * this is most likely a poll mode VF driver, so don't enable an
1221 	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
1222 	 */
1223 	if (!(reg & QINT_RQCTL_MSIX_INDX_M))
1224 		return;
1225 
1226 	wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
1227 }
1228 
1229 /**
1230  * ice_vc_ena_qs_msg
1231  * @vf: pointer to the VF info
1232  * @msg: pointer to the msg buffer
1233  *
1234  * called from the VF to enable all or specific queue(s)
1235  */
1236 static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
1237 {
1238 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1239 	struct virtchnl_queue_select *vqs =
1240 	    (struct virtchnl_queue_select *)msg;
1241 	struct ice_vsi *vsi;
1242 	unsigned long q_map;
1243 	u16 vf_q_id;
1244 
1245 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1246 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1247 		goto error_param;
1248 	}
1249 
1250 	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1251 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1252 		goto error_param;
1253 	}
1254 
1255 	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1256 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1257 		goto error_param;
1258 	}
1259 
1260 	vsi = ice_get_vf_vsi(vf);
1261 	if (!vsi) {
1262 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1263 		goto error_param;
1264 	}
1265 
1266 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
1267 	 * Tx queue group list was configured and the context bits were
1268 	 * programmed using ice_vsi_cfg_txqs
1269 	 */
1270 	q_map = vqs->rx_queues;
1271 	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1272 		if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1273 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1274 			goto error_param;
1275 		}
1276 
1277 		/* Skip queue if enabled */
1278 		if (test_bit(vf_q_id, vf->rxq_ena))
1279 			continue;
1280 
1281 		if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
1282 			dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
1283 				vf_q_id, vsi->vsi_num);
1284 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1285 			goto error_param;
1286 		}
1287 
1288 		ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
1289 		set_bit(vf_q_id, vf->rxq_ena);
1290 	}
1291 
1292 	q_map = vqs->tx_queues;
1293 	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1294 		if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1295 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1296 			goto error_param;
1297 		}
1298 
1299 		/* Skip queue if enabled */
1300 		if (test_bit(vf_q_id, vf->txq_ena))
1301 			continue;
1302 
1303 		ice_vf_ena_txq_interrupt(vsi, vf_q_id);
1304 		set_bit(vf_q_id, vf->txq_ena);
1305 	}
1306 
1307 	/* Set flag to indicate that queues are enabled */
1308 	if (v_ret == VIRTCHNL_STATUS_SUCCESS)
1309 		set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1310 
1311 error_param:
1312 	/* send the response to the VF */
1313 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
1314 				     NULL, 0);
1315 }
1316 
1317 /**
1318  * ice_vf_vsi_dis_single_txq - disable a single Tx queue
1319  * @vf: VF to disable queue for
1320  * @vsi: VSI for the VF
1321  * @q_id: VF relative (0-based) queue ID
1322  *
1323  * Attempt to disable the Tx queue passed in. If the Tx queue was successfully
1324  * disabled then clear q_id bit in the enabled queues bitmap and return
1325  * success. Otherwise return error.
1326  */
1327 static int
1328 ice_vf_vsi_dis_single_txq(struct ice_vf *vf, struct ice_vsi *vsi, u16 q_id)
1329 {
1330 	struct ice_txq_meta txq_meta = { 0 };
1331 	struct ice_tx_ring *ring;
1332 	int err;
1333 
1334 	if (!test_bit(q_id, vf->txq_ena))
1335 		dev_dbg(ice_pf_to_dev(vsi->back), "Queue %u on VSI %u is not enabled, but stopping it anyway\n",
1336 			q_id, vsi->vsi_num);
1337 
1338 	ring = vsi->tx_rings[q_id];
1339 	if (!ring)
1340 		return -EINVAL;
1341 
1342 	ice_fill_txq_meta(vsi, ring, &txq_meta);
1343 
1344 	err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id, ring, &txq_meta);
1345 	if (err) {
1346 		dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
1347 			q_id, vsi->vsi_num);
1348 		return err;
1349 	}
1350 
1351 	/* Clear enabled queues flag */
1352 	clear_bit(q_id, vf->txq_ena);
1353 
1354 	return 0;
1355 }
1356 
1357 /**
1358  * ice_vc_dis_qs_msg
1359  * @vf: pointer to the VF info
1360  * @msg: pointer to the msg buffer
1361  *
1362  * called from the VF to disable all or specific queue(s)
1363  */
1364 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
1365 {
1366 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1367 	struct virtchnl_queue_select *vqs =
1368 	    (struct virtchnl_queue_select *)msg;
1369 	struct ice_vsi *vsi;
1370 	unsigned long q_map;
1371 	u16 vf_q_id;
1372 
1373 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
1374 	    !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
1375 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1376 		goto error_param;
1377 	}
1378 
1379 	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1380 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1381 		goto error_param;
1382 	}
1383 
1384 	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1385 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1386 		goto error_param;
1387 	}
1388 
1389 	vsi = ice_get_vf_vsi(vf);
1390 	if (!vsi) {
1391 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1392 		goto error_param;
1393 	}
1394 
1395 	if (vqs->tx_queues) {
1396 		q_map = vqs->tx_queues;
1397 
1398 		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1399 			if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1400 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1401 				goto error_param;
1402 			}
1403 
1404 			if (ice_vf_vsi_dis_single_txq(vf, vsi, vf_q_id)) {
1405 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1406 				goto error_param;
1407 			}
1408 		}
1409 	}
1410 
1411 	q_map = vqs->rx_queues;
1412 	/* speed up Rx queue disable by batching them if possible */
1413 	if (q_map &&
1414 	    bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
1415 		if (ice_vsi_stop_all_rx_rings(vsi)) {
1416 			dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
1417 				vsi->vsi_num);
1418 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1419 			goto error_param;
1420 		}
1421 
1422 		bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
1423 	} else if (q_map) {
1424 		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1425 			if (!ice_vc_isvalid_q_id(vf, vqs->vsi_id, vf_q_id)) {
1426 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1427 				goto error_param;
1428 			}
1429 
1430 			/* Skip queue if not enabled */
1431 			if (!test_bit(vf_q_id, vf->rxq_ena))
1432 				continue;
1433 
1434 			if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
1435 						     true)) {
1436 				dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
1437 					vf_q_id, vsi->vsi_num);
1438 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1439 				goto error_param;
1440 			}
1441 
1442 			/* Clear enabled queues flag */
1443 			clear_bit(vf_q_id, vf->rxq_ena);
1444 		}
1445 	}
1446 
1447 	/* Clear enabled queues flag */
1448 	if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
1449 		clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1450 
1451 error_param:
1452 	/* send the response to the VF */
1453 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
1454 				     NULL, 0);
1455 }
1456 
1457 /**
1458  * ice_cfg_interrupt
1459  * @vf: pointer to the VF info
1460  * @vsi: the VSI being configured
1461  * @vector_id: vector ID
1462  * @map: vector map for mapping vectors to queues
1463  * @q_vector: structure for interrupt vector
1464  * configure the IRQ to queue map
1465  */
1466 static int
1467 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi, u16 vector_id,
1468 		  struct virtchnl_vector_map *map,
1469 		  struct ice_q_vector *q_vector)
1470 {
1471 	u16 vsi_q_id, vsi_q_id_idx;
1472 	unsigned long qmap;
1473 
1474 	q_vector->num_ring_rx = 0;
1475 	q_vector->num_ring_tx = 0;
1476 
1477 	qmap = map->rxq_map;
1478 	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1479 		vsi_q_id = vsi_q_id_idx;
1480 
1481 		if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
1482 			return VIRTCHNL_STATUS_ERR_PARAM;
1483 
1484 		q_vector->num_ring_rx++;
1485 		q_vector->rx.itr_idx = map->rxitr_idx;
1486 		vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
1487 		ice_cfg_rxq_interrupt(vsi, vsi_q_id, vector_id,
1488 				      q_vector->rx.itr_idx);
1489 	}
1490 
1491 	qmap = map->txq_map;
1492 	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1493 		vsi_q_id = vsi_q_id_idx;
1494 
1495 		if (!ice_vc_isvalid_q_id(vf, vsi->vsi_num, vsi_q_id))
1496 			return VIRTCHNL_STATUS_ERR_PARAM;
1497 
1498 		q_vector->num_ring_tx++;
1499 		q_vector->tx.itr_idx = map->txitr_idx;
1500 		vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
1501 		ice_cfg_txq_interrupt(vsi, vsi_q_id, vector_id,
1502 				      q_vector->tx.itr_idx);
1503 	}
1504 
1505 	return VIRTCHNL_STATUS_SUCCESS;
1506 }
1507 
1508 /**
1509  * ice_vc_cfg_irq_map_msg
1510  * @vf: pointer to the VF info
1511  * @msg: pointer to the msg buffer
1512  *
1513  * called from the VF to configure the IRQ to queue map
1514  */
1515 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
1516 {
1517 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1518 	u16 num_q_vectors_mapped, vsi_id, vector_id;
1519 	struct virtchnl_irq_map_info *irqmap_info;
1520 	struct virtchnl_vector_map *map;
1521 	struct ice_pf *pf = vf->pf;
1522 	struct ice_vsi *vsi;
1523 	int i;
1524 
1525 	irqmap_info = (struct virtchnl_irq_map_info *)msg;
1526 	num_q_vectors_mapped = irqmap_info->num_vectors;
1527 
1528 	/* Check to make sure number of VF vectors mapped is not greater than
1529 	 * number of VF vectors originally allocated, and check that
1530 	 * there is actually at least a single VF queue vector mapped
1531 	 */
1532 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1533 	    pf->vfs.num_msix_per < num_q_vectors_mapped ||
1534 	    !num_q_vectors_mapped) {
1535 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1536 		goto error_param;
1537 	}
1538 
1539 	vsi = ice_get_vf_vsi(vf);
1540 	if (!vsi) {
1541 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1542 		goto error_param;
1543 	}
1544 
1545 	for (i = 0; i < num_q_vectors_mapped; i++) {
1546 		struct ice_q_vector *q_vector;
1547 
1548 		map = &irqmap_info->vecmap[i];
1549 
1550 		vector_id = map->vector_id;
1551 		vsi_id = map->vsi_id;
1552 		/* vector_id is always 0-based for each VF, and can never be
1553 		 * larger than or equal to the max allowed interrupts per VF
1554 		 */
1555 		if (!(vector_id < pf->vfs.num_msix_per) ||
1556 		    !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
1557 		    (!vector_id && (map->rxq_map || map->txq_map))) {
1558 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1559 			goto error_param;
1560 		}
1561 
1562 		/* No need to map VF miscellaneous or rogue vector */
1563 		if (!vector_id)
1564 			continue;
1565 
1566 		/* Subtract non queue vector from vector_id passed by VF
1567 		 * to get actual number of VSI queue vector array index
1568 		 */
1569 		q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
1570 		if (!q_vector) {
1571 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1572 			goto error_param;
1573 		}
1574 
1575 		/* lookout for the invalid queue index */
1576 		v_ret = (enum virtchnl_status_code)
1577 			ice_cfg_interrupt(vf, vsi, vector_id, map, q_vector);
1578 		if (v_ret)
1579 			goto error_param;
1580 	}
1581 
1582 error_param:
1583 	/* send the response to the VF */
1584 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
1585 				     NULL, 0);
1586 }
1587 
1588 /**
1589  * ice_vc_cfg_qs_msg
1590  * @vf: pointer to the VF info
1591  * @msg: pointer to the msg buffer
1592  *
1593  * called from the VF to configure the Rx/Tx queues
1594  */
1595 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
1596 {
1597 	struct virtchnl_vsi_queue_config_info *qci =
1598 	    (struct virtchnl_vsi_queue_config_info *)msg;
1599 	struct virtchnl_queue_pair_info *qpi;
1600 	struct ice_pf *pf = vf->pf;
1601 	struct ice_lag *lag;
1602 	struct ice_vsi *vsi;
1603 	u8 act_prt, pri_prt;
1604 	int i = -1, q_idx;
1605 
1606 	lag = pf->lag;
1607 	mutex_lock(&pf->lag_mutex);
1608 	act_prt = ICE_LAG_INVALID_PORT;
1609 	pri_prt = pf->hw.port_info->lport;
1610 	if (lag && lag->bonded && lag->primary) {
1611 		act_prt = lag->active_port;
1612 		if (act_prt != pri_prt && act_prt != ICE_LAG_INVALID_PORT &&
1613 		    lag->upper_netdev)
1614 			ice_lag_move_vf_nodes_cfg(lag, act_prt, pri_prt);
1615 		else
1616 			act_prt = ICE_LAG_INVALID_PORT;
1617 	}
1618 
1619 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1620 		goto error_param;
1621 
1622 	if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id))
1623 		goto error_param;
1624 
1625 	vsi = ice_get_vf_vsi(vf);
1626 	if (!vsi)
1627 		goto error_param;
1628 
1629 	if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
1630 	    qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
1631 		dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
1632 			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
1633 		goto error_param;
1634 	}
1635 
1636 	for (i = 0; i < qci->num_queue_pairs; i++) {
1637 		qpi = &qci->qpair[i];
1638 		if (qpi->txq.vsi_id != qci->vsi_id ||
1639 		    qpi->rxq.vsi_id != qci->vsi_id ||
1640 		    qpi->rxq.queue_id != qpi->txq.queue_id ||
1641 		    qpi->txq.headwb_enabled ||
1642 		    !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
1643 		    !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
1644 		    !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
1645 			goto error_param;
1646 		}
1647 
1648 		q_idx = qpi->rxq.queue_id;
1649 
1650 		/* make sure selected "q_idx" is in valid range of queues
1651 		 * for selected "vsi"
1652 		 */
1653 		if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
1654 			goto error_param;
1655 		}
1656 
1657 		/* copy Tx queue info from VF into VSI */
1658 		if (qpi->txq.ring_len > 0) {
1659 			vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
1660 			vsi->tx_rings[i]->count = qpi->txq.ring_len;
1661 
1662 			/* Disable any existing queue first */
1663 			if (ice_vf_vsi_dis_single_txq(vf, vsi, q_idx))
1664 				goto error_param;
1665 
1666 			/* Configure a queue with the requested settings */
1667 			if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
1668 				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure TX queue %d\n",
1669 					 vf->vf_id, i);
1670 				goto error_param;
1671 			}
1672 		}
1673 
1674 		/* copy Rx queue info from VF into VSI */
1675 		if (qpi->rxq.ring_len > 0) {
1676 			u16 max_frame_size = ice_vc_get_max_frame_size(vf);
1677 			u32 rxdid;
1678 
1679 			vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
1680 			vsi->rx_rings[i]->count = qpi->rxq.ring_len;
1681 
1682 			if (qpi->rxq.databuffer_size != 0 &&
1683 			    (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
1684 			     qpi->rxq.databuffer_size < 1024))
1685 				goto error_param;
1686 			vsi->rx_buf_len = qpi->rxq.databuffer_size;
1687 			vsi->rx_rings[i]->rx_buf_len = vsi->rx_buf_len;
1688 			if (qpi->rxq.max_pkt_size > max_frame_size ||
1689 			    qpi->rxq.max_pkt_size < 64)
1690 				goto error_param;
1691 
1692 			vsi->max_frame = qpi->rxq.max_pkt_size;
1693 			/* add space for the port VLAN since the VF driver is
1694 			 * not expected to account for it in the MTU
1695 			 * calculation
1696 			 */
1697 			if (ice_vf_is_port_vlan_ena(vf))
1698 				vsi->max_frame += VLAN_HLEN;
1699 
1700 			if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
1701 				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure RX queue %d\n",
1702 					 vf->vf_id, i);
1703 				goto error_param;
1704 			}
1705 
1706 			/* If Rx flex desc is supported, select RXDID for Rx
1707 			 * queues. Otherwise, use legacy 32byte descriptor
1708 			 * format. Legacy 16byte descriptor is not supported.
1709 			 * If this RXDID is selected, return error.
1710 			 */
1711 			if (vf->driver_caps &
1712 			    VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1713 				rxdid = qpi->rxq.rxdid;
1714 				if (!(BIT(rxdid) & pf->supported_rxdids))
1715 					goto error_param;
1716 			} else {
1717 				rxdid = ICE_RXDID_LEGACY_1;
1718 			}
1719 
1720 			ice_write_qrxflxp_cntxt(&vsi->back->hw,
1721 						vsi->rxq_map[q_idx],
1722 						rxdid, 0x03, false);
1723 		}
1724 	}
1725 
1726 	if (lag && lag->bonded && lag->primary &&
1727 	    act_prt != ICE_LAG_INVALID_PORT)
1728 		ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
1729 	mutex_unlock(&pf->lag_mutex);
1730 
1731 	/* send the response to the VF */
1732 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1733 				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
1734 error_param:
1735 	/* disable whatever we can */
1736 	for (; i >= 0; i--) {
1737 		if (ice_vsi_ctrl_one_rx_ring(vsi, false, i, true))
1738 			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable RX queue %d\n",
1739 				vf->vf_id, i);
1740 		if (ice_vf_vsi_dis_single_txq(vf, vsi, i))
1741 			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable TX queue %d\n",
1742 				vf->vf_id, i);
1743 	}
1744 
1745 	if (lag && lag->bonded && lag->primary &&
1746 	    act_prt != ICE_LAG_INVALID_PORT)
1747 		ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
1748 	mutex_unlock(&pf->lag_mutex);
1749 
1750 	ice_lag_move_new_vf_nodes(vf);
1751 
1752 	/* send the response to the VF */
1753 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1754 				     VIRTCHNL_STATUS_ERR_PARAM, NULL, 0);
1755 }
1756 
1757 /**
1758  * ice_can_vf_change_mac
1759  * @vf: pointer to the VF info
1760  *
1761  * Return true if the VF is allowed to change its MAC filters, false otherwise
1762  */
1763 static bool ice_can_vf_change_mac(struct ice_vf *vf)
1764 {
1765 	/* If the VF MAC address has been set administratively (via the
1766 	 * ndo_set_vf_mac command), then deny permission to the VF to
1767 	 * add/delete unicast MAC addresses, unless the VF is trusted
1768 	 */
1769 	if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
1770 		return false;
1771 
1772 	return true;
1773 }
1774 
1775 /**
1776  * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
1777  * @vc_ether_addr: used to extract the type
1778  */
1779 static u8
1780 ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
1781 {
1782 	return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
1783 }
1784 
1785 /**
1786  * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
1787  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
1788  */
1789 static bool
1790 ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
1791 {
1792 	u8 type = ice_vc_ether_addr_type(vc_ether_addr);
1793 
1794 	return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
1795 }
1796 
1797 /**
1798  * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
1799  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
1800  *
1801  * This function should only be called when the MAC address in
1802  * virtchnl_ether_addr is a valid unicast MAC
1803  */
1804 static bool
1805 ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
1806 {
1807 	u8 type = ice_vc_ether_addr_type(vc_ether_addr);
1808 
1809 	return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
1810 }
1811 
1812 /**
1813  * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
1814  * @vf: VF to update
1815  * @vc_ether_addr: structure from VIRTCHNL with MAC to add
1816  */
1817 static void
1818 ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
1819 {
1820 	u8 *mac_addr = vc_ether_addr->addr;
1821 
1822 	if (!is_valid_ether_addr(mac_addr))
1823 		return;
1824 
1825 	/* only allow legacy VF drivers to set the device and hardware MAC if it
1826 	 * is zero and allow new VF drivers to set the hardware MAC if the type
1827 	 * was correctly specified over VIRTCHNL
1828 	 */
1829 	if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
1830 	     is_zero_ether_addr(vf->hw_lan_addr)) ||
1831 	    ice_is_vc_addr_primary(vc_ether_addr)) {
1832 		ether_addr_copy(vf->dev_lan_addr, mac_addr);
1833 		ether_addr_copy(vf->hw_lan_addr, mac_addr);
1834 	}
1835 
1836 	/* hardware and device MACs are already set, but its possible that the
1837 	 * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
1838 	 * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
1839 	 * away for the legacy VF driver case as it will be updated in the
1840 	 * delete flow for this case
1841 	 */
1842 	if (ice_is_vc_addr_legacy(vc_ether_addr)) {
1843 		ether_addr_copy(vf->legacy_last_added_umac.addr,
1844 				mac_addr);
1845 		vf->legacy_last_added_umac.time_modified = jiffies;
1846 	}
1847 }
1848 
1849 /**
1850  * ice_vc_add_mac_addr - attempt to add the MAC address passed in
1851  * @vf: pointer to the VF info
1852  * @vsi: pointer to the VF's VSI
1853  * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
1854  */
1855 static int
1856 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
1857 		    struct virtchnl_ether_addr *vc_ether_addr)
1858 {
1859 	struct device *dev = ice_pf_to_dev(vf->pf);
1860 	u8 *mac_addr = vc_ether_addr->addr;
1861 	int ret;
1862 
1863 	/* device MAC already added */
1864 	if (ether_addr_equal(mac_addr, vf->dev_lan_addr))
1865 		return 0;
1866 
1867 	if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
1868 		dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
1869 		return -EPERM;
1870 	}
1871 
1872 	ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
1873 	if (ret == -EEXIST) {
1874 		dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
1875 			vf->vf_id);
1876 		/* don't return since we might need to update
1877 		 * the primary MAC in ice_vfhw_mac_add() below
1878 		 */
1879 	} else if (ret) {
1880 		dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
1881 			mac_addr, vf->vf_id, ret);
1882 		return ret;
1883 	} else {
1884 		vf->num_mac++;
1885 	}
1886 
1887 	ice_vfhw_mac_add(vf, vc_ether_addr);
1888 
1889 	return ret;
1890 }
1891 
1892 /**
1893  * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
1894  * @last_added_umac: structure used to check expiration
1895  */
1896 static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
1897 {
1898 #define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME	msecs_to_jiffies(3000)
1899 	return time_is_before_jiffies(last_added_umac->time_modified +
1900 				      ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
1901 }
1902 
1903 /**
1904  * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
1905  * @vf: VF to update
1906  * @vc_ether_addr: structure from VIRTCHNL with MAC to check
1907  *
1908  * only update cached hardware MAC for legacy VF drivers on delete
1909  * because we cannot guarantee order/type of MAC from the VF driver
1910  */
1911 static void
1912 ice_update_legacy_cached_mac(struct ice_vf *vf,
1913 			     struct virtchnl_ether_addr *vc_ether_addr)
1914 {
1915 	if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
1916 	    ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
1917 		return;
1918 
1919 	ether_addr_copy(vf->dev_lan_addr, vf->legacy_last_added_umac.addr);
1920 	ether_addr_copy(vf->hw_lan_addr, vf->legacy_last_added_umac.addr);
1921 }
1922 
1923 /**
1924  * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
1925  * @vf: VF to update
1926  * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
1927  */
1928 static void
1929 ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
1930 {
1931 	u8 *mac_addr = vc_ether_addr->addr;
1932 
1933 	if (!is_valid_ether_addr(mac_addr) ||
1934 	    !ether_addr_equal(vf->dev_lan_addr, mac_addr))
1935 		return;
1936 
1937 	/* allow the device MAC to be repopulated in the add flow and don't
1938 	 * clear the hardware MAC (i.e. hw_lan_addr) here as that is meant
1939 	 * to be persistent on VM reboot and across driver unload/load, which
1940 	 * won't work if we clear the hardware MAC here
1941 	 */
1942 	eth_zero_addr(vf->dev_lan_addr);
1943 
1944 	ice_update_legacy_cached_mac(vf, vc_ether_addr);
1945 }
1946 
1947 /**
1948  * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
1949  * @vf: pointer to the VF info
1950  * @vsi: pointer to the VF's VSI
1951  * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
1952  */
1953 static int
1954 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
1955 		    struct virtchnl_ether_addr *vc_ether_addr)
1956 {
1957 	struct device *dev = ice_pf_to_dev(vf->pf);
1958 	u8 *mac_addr = vc_ether_addr->addr;
1959 	int status;
1960 
1961 	if (!ice_can_vf_change_mac(vf) &&
1962 	    ether_addr_equal(vf->dev_lan_addr, mac_addr))
1963 		return 0;
1964 
1965 	status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
1966 	if (status == -ENOENT) {
1967 		dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
1968 			vf->vf_id);
1969 		return -ENOENT;
1970 	} else if (status) {
1971 		dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
1972 			mac_addr, vf->vf_id, status);
1973 		return -EIO;
1974 	}
1975 
1976 	ice_vfhw_mac_del(vf, vc_ether_addr);
1977 
1978 	vf->num_mac--;
1979 
1980 	return 0;
1981 }
1982 
1983 /**
1984  * ice_vc_handle_mac_addr_msg
1985  * @vf: pointer to the VF info
1986  * @msg: pointer to the msg buffer
1987  * @set: true if MAC filters are being set, false otherwise
1988  *
1989  * add guest MAC address filter
1990  */
1991 static int
1992 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
1993 {
1994 	int (*ice_vc_cfg_mac)
1995 		(struct ice_vf *vf, struct ice_vsi *vsi,
1996 		 struct virtchnl_ether_addr *virtchnl_ether_addr);
1997 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1998 	struct virtchnl_ether_addr_list *al =
1999 	    (struct virtchnl_ether_addr_list *)msg;
2000 	struct ice_pf *pf = vf->pf;
2001 	enum virtchnl_ops vc_op;
2002 	struct ice_vsi *vsi;
2003 	int i;
2004 
2005 	if (set) {
2006 		vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2007 		ice_vc_cfg_mac = ice_vc_add_mac_addr;
2008 	} else {
2009 		vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2010 		ice_vc_cfg_mac = ice_vc_del_mac_addr;
2011 	}
2012 
2013 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2014 	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2015 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2016 		goto handle_mac_exit;
2017 	}
2018 
2019 	/* If this VF is not privileged, then we can't add more than a
2020 	 * limited number of addresses. Check to make sure that the
2021 	 * additions do not push us over the limit.
2022 	 */
2023 	if (set && !ice_is_vf_trusted(vf) &&
2024 	    (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2025 		dev_err(ice_pf_to_dev(pf), "Can't add more MAC addresses, because VF-%d is not trusted, switch the VF to trusted mode in order to add more functionalities\n",
2026 			vf->vf_id);
2027 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2028 		goto handle_mac_exit;
2029 	}
2030 
2031 	vsi = ice_get_vf_vsi(vf);
2032 	if (!vsi) {
2033 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2034 		goto handle_mac_exit;
2035 	}
2036 
2037 	for (i = 0; i < al->num_elements; i++) {
2038 		u8 *mac_addr = al->list[i].addr;
2039 		int result;
2040 
2041 		if (is_broadcast_ether_addr(mac_addr) ||
2042 		    is_zero_ether_addr(mac_addr))
2043 			continue;
2044 
2045 		result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
2046 		if (result == -EEXIST || result == -ENOENT) {
2047 			continue;
2048 		} else if (result) {
2049 			v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2050 			goto handle_mac_exit;
2051 		}
2052 	}
2053 
2054 handle_mac_exit:
2055 	/* send the response to the VF */
2056 	return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2057 }
2058 
2059 /**
2060  * ice_vc_add_mac_addr_msg
2061  * @vf: pointer to the VF info
2062  * @msg: pointer to the msg buffer
2063  *
2064  * add guest MAC address filter
2065  */
2066 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2067 {
2068 	return ice_vc_handle_mac_addr_msg(vf, msg, true);
2069 }
2070 
2071 /**
2072  * ice_vc_del_mac_addr_msg
2073  * @vf: pointer to the VF info
2074  * @msg: pointer to the msg buffer
2075  *
2076  * remove guest MAC address filter
2077  */
2078 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2079 {
2080 	return ice_vc_handle_mac_addr_msg(vf, msg, false);
2081 }
2082 
2083 /**
2084  * ice_vc_request_qs_msg
2085  * @vf: pointer to the VF info
2086  * @msg: pointer to the msg buffer
2087  *
2088  * VFs get a default number of queues but can use this message to request a
2089  * different number. If the request is successful, PF will reset the VF and
2090  * return 0. If unsuccessful, PF will send message informing VF of number of
2091  * available queue pairs via virtchnl message response to VF.
2092  */
2093 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2094 {
2095 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2096 	struct virtchnl_vf_res_request *vfres =
2097 		(struct virtchnl_vf_res_request *)msg;
2098 	u16 req_queues = vfres->num_queue_pairs;
2099 	struct ice_pf *pf = vf->pf;
2100 	u16 max_allowed_vf_queues;
2101 	u16 tx_rx_queue_left;
2102 	struct device *dev;
2103 	u16 cur_queues;
2104 
2105 	dev = ice_pf_to_dev(pf);
2106 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2107 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2108 		goto error_param;
2109 	}
2110 
2111 	cur_queues = vf->num_vf_qs;
2112 	tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
2113 				 ice_get_avail_rxq_count(pf));
2114 	max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2115 	if (!req_queues) {
2116 		dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
2117 			vf->vf_id);
2118 	} else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
2119 		dev_err(dev, "VF %d tried to request more than %d queues.\n",
2120 			vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
2121 		vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
2122 	} else if (req_queues > cur_queues &&
2123 		   req_queues - cur_queues > tx_rx_queue_left) {
2124 		dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
2125 			 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2126 		vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
2127 					       ICE_MAX_RSS_QS_PER_VF);
2128 	} else {
2129 		/* request is successful, then reset VF */
2130 		vf->num_req_qs = req_queues;
2131 		ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
2132 		dev_info(dev, "VF %d granted request of %u queues.\n",
2133 			 vf->vf_id, req_queues);
2134 		return 0;
2135 	}
2136 
2137 error_param:
2138 	/* send the response to the VF */
2139 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2140 				     v_ret, (u8 *)vfres, sizeof(*vfres));
2141 }
2142 
2143 /**
2144  * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
2145  * @caps: VF driver negotiated capabilities
2146  *
2147  * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
2148  */
2149 static bool ice_vf_vlan_offload_ena(u32 caps)
2150 {
2151 	return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
2152 }
2153 
2154 /**
2155  * ice_is_vlan_promisc_allowed - check if VLAN promiscuous config is allowed
2156  * @vf: VF used to determine if VLAN promiscuous config is allowed
2157  */
2158 static bool ice_is_vlan_promisc_allowed(struct ice_vf *vf)
2159 {
2160 	if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2161 	     test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
2162 	    test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, vf->pf->flags))
2163 		return true;
2164 
2165 	return false;
2166 }
2167 
2168 /**
2169  * ice_vf_ena_vlan_promisc - Enable Tx/Rx VLAN promiscuous for the VLAN
2170  * @vsi: VF's VSI used to enable VLAN promiscuous mode
2171  * @vlan: VLAN used to enable VLAN promiscuous
2172  *
2173  * This function should only be called if VLAN promiscuous mode is allowed,
2174  * which can be determined via ice_is_vlan_promisc_allowed().
2175  */
2176 static int ice_vf_ena_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2177 {
2178 	u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
2179 	int status;
2180 
2181 	status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2182 					  vlan->vid);
2183 	if (status && status != -EEXIST)
2184 		return status;
2185 
2186 	return 0;
2187 }
2188 
2189 /**
2190  * ice_vf_dis_vlan_promisc - Disable Tx/Rx VLAN promiscuous for the VLAN
2191  * @vsi: VF's VSI used to disable VLAN promiscuous mode for
2192  * @vlan: VLAN used to disable VLAN promiscuous
2193  *
2194  * This function should only be called if VLAN promiscuous mode is allowed,
2195  * which can be determined via ice_is_vlan_promisc_allowed().
2196  */
2197 static int ice_vf_dis_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2198 {
2199 	u8 promisc_m = ICE_PROMISC_VLAN_TX | ICE_PROMISC_VLAN_RX;
2200 	int status;
2201 
2202 	status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2203 					    vlan->vid);
2204 	if (status && status != -ENOENT)
2205 		return status;
2206 
2207 	return 0;
2208 }
2209 
2210 /**
2211  * ice_vf_has_max_vlans - check if VF already has the max allowed VLAN filters
2212  * @vf: VF to check against
2213  * @vsi: VF's VSI
2214  *
2215  * If the VF is trusted then the VF is allowed to add as many VLANs as it
2216  * wants to, so return false.
2217  *
2218  * When the VF is untrusted compare the number of non-zero VLANs + 1 to the max
2219  * allowed VLANs for an untrusted VF. Return the result of this comparison.
2220  */
2221 static bool ice_vf_has_max_vlans(struct ice_vf *vf, struct ice_vsi *vsi)
2222 {
2223 	if (ice_is_vf_trusted(vf))
2224 		return false;
2225 
2226 #define ICE_VF_ADDED_VLAN_ZERO_FLTRS	1
2227 	return ((ice_vsi_num_non_zero_vlans(vsi) +
2228 		ICE_VF_ADDED_VLAN_ZERO_FLTRS) >= ICE_MAX_VLAN_PER_VF);
2229 }
2230 
2231 /**
2232  * ice_vc_process_vlan_msg
2233  * @vf: pointer to the VF info
2234  * @msg: pointer to the msg buffer
2235  * @add_v: Add VLAN if true, otherwise delete VLAN
2236  *
2237  * Process virtchnl op to add or remove programmed guest VLAN ID
2238  */
2239 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2240 {
2241 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2242 	struct virtchnl_vlan_filter_list *vfl =
2243 	    (struct virtchnl_vlan_filter_list *)msg;
2244 	struct ice_pf *pf = vf->pf;
2245 	bool vlan_promisc = false;
2246 	struct ice_vsi *vsi;
2247 	struct device *dev;
2248 	int status = 0;
2249 	int i;
2250 
2251 	dev = ice_pf_to_dev(pf);
2252 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2253 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2254 		goto error_param;
2255 	}
2256 
2257 	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2258 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2259 		goto error_param;
2260 	}
2261 
2262 	if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2263 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2264 		goto error_param;
2265 	}
2266 
2267 	for (i = 0; i < vfl->num_elements; i++) {
2268 		if (vfl->vlan_id[i] >= VLAN_N_VID) {
2269 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2270 			dev_err(dev, "invalid VF VLAN id %d\n",
2271 				vfl->vlan_id[i]);
2272 			goto error_param;
2273 		}
2274 	}
2275 
2276 	vsi = ice_get_vf_vsi(vf);
2277 	if (!vsi) {
2278 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2279 		goto error_param;
2280 	}
2281 
2282 	if (add_v && ice_vf_has_max_vlans(vf, vsi)) {
2283 		dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2284 			 vf->vf_id);
2285 		/* There is no need to let VF know about being not trusted,
2286 		 * so we can just return success message here
2287 		 */
2288 		goto error_param;
2289 	}
2290 
2291 	/* in DVM a VF can add/delete inner VLAN filters when
2292 	 * VIRTCHNL_VF_OFFLOAD_VLAN is negotiated, so only reject in SVM
2293 	 */
2294 	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&pf->hw)) {
2295 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2296 		goto error_param;
2297 	}
2298 
2299 	/* in DVM VLAN promiscuous is based on the outer VLAN, which would be
2300 	 * the port VLAN if VIRTCHNL_VF_OFFLOAD_VLAN was negotiated, so only
2301 	 * allow vlan_promisc = true in SVM and if no port VLAN is configured
2302 	 */
2303 	vlan_promisc = ice_is_vlan_promisc_allowed(vf) &&
2304 		!ice_is_dvm_ena(&pf->hw) &&
2305 		!ice_vf_is_port_vlan_ena(vf);
2306 
2307 	if (add_v) {
2308 		for (i = 0; i < vfl->num_elements; i++) {
2309 			u16 vid = vfl->vlan_id[i];
2310 			struct ice_vlan vlan;
2311 
2312 			if (ice_vf_has_max_vlans(vf, vsi)) {
2313 				dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2314 					 vf->vf_id);
2315 				/* There is no need to let VF know about being
2316 				 * not trusted, so we can just return success
2317 				 * message here as well.
2318 				 */
2319 				goto error_param;
2320 			}
2321 
2322 			/* we add VLAN 0 by default for each VF so we can enable
2323 			 * Tx VLAN anti-spoof without triggering MDD events so
2324 			 * we don't need to add it again here
2325 			 */
2326 			if (!vid)
2327 				continue;
2328 
2329 			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2330 			status = vsi->inner_vlan_ops.add_vlan(vsi, &vlan);
2331 			if (status) {
2332 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2333 				goto error_param;
2334 			}
2335 
2336 			/* Enable VLAN filtering on first non-zero VLAN */
2337 			if (!vlan_promisc && vid && !ice_is_dvm_ena(&pf->hw)) {
2338 				if (vf->spoofchk) {
2339 					status = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
2340 					if (status) {
2341 						v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2342 						dev_err(dev, "Enable VLAN anti-spoofing on VLAN ID: %d failed error-%d\n",
2343 							vid, status);
2344 						goto error_param;
2345 					}
2346 				}
2347 				if (vsi->inner_vlan_ops.ena_rx_filtering(vsi)) {
2348 					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2349 					dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2350 						vid, status);
2351 					goto error_param;
2352 				}
2353 			} else if (vlan_promisc) {
2354 				status = ice_vf_ena_vlan_promisc(vsi, &vlan);
2355 				if (status) {
2356 					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2357 					dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2358 						vid, status);
2359 				}
2360 			}
2361 		}
2362 	} else {
2363 		/* In case of non_trusted VF, number of VLAN elements passed
2364 		 * to PF for removal might be greater than number of VLANs
2365 		 * filter programmed for that VF - So, use actual number of
2366 		 * VLANS added earlier with add VLAN opcode. In order to avoid
2367 		 * removing VLAN that doesn't exist, which result to sending
2368 		 * erroneous failed message back to the VF
2369 		 */
2370 		int num_vf_vlan;
2371 
2372 		num_vf_vlan = vsi->num_vlan;
2373 		for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
2374 			u16 vid = vfl->vlan_id[i];
2375 			struct ice_vlan vlan;
2376 
2377 			/* we add VLAN 0 by default for each VF so we can enable
2378 			 * Tx VLAN anti-spoof without triggering MDD events so
2379 			 * we don't want a VIRTCHNL request to remove it
2380 			 */
2381 			if (!vid)
2382 				continue;
2383 
2384 			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2385 			status = vsi->inner_vlan_ops.del_vlan(vsi, &vlan);
2386 			if (status) {
2387 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2388 				goto error_param;
2389 			}
2390 
2391 			/* Disable VLAN filtering when only VLAN 0 is left */
2392 			if (!ice_vsi_has_non_zero_vlans(vsi)) {
2393 				vsi->inner_vlan_ops.dis_tx_filtering(vsi);
2394 				vsi->inner_vlan_ops.dis_rx_filtering(vsi);
2395 			}
2396 
2397 			if (vlan_promisc)
2398 				ice_vf_dis_vlan_promisc(vsi, &vlan);
2399 		}
2400 	}
2401 
2402 error_param:
2403 	/* send the response to the VF */
2404 	if (add_v)
2405 		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
2406 					     NULL, 0);
2407 	else
2408 		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
2409 					     NULL, 0);
2410 }
2411 
2412 /**
2413  * ice_vc_add_vlan_msg
2414  * @vf: pointer to the VF info
2415  * @msg: pointer to the msg buffer
2416  *
2417  * Add and program guest VLAN ID
2418  */
2419 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2420 {
2421 	return ice_vc_process_vlan_msg(vf, msg, true);
2422 }
2423 
2424 /**
2425  * ice_vc_remove_vlan_msg
2426  * @vf: pointer to the VF info
2427  * @msg: pointer to the msg buffer
2428  *
2429  * remove programmed guest VLAN ID
2430  */
2431 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2432 {
2433 	return ice_vc_process_vlan_msg(vf, msg, false);
2434 }
2435 
2436 /**
2437  * ice_vc_ena_vlan_stripping
2438  * @vf: pointer to the VF info
2439  *
2440  * Enable VLAN header stripping for a given VF
2441  */
2442 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2443 {
2444 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2445 	struct ice_vsi *vsi;
2446 
2447 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2448 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2449 		goto error_param;
2450 	}
2451 
2452 	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2453 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2454 		goto error_param;
2455 	}
2456 
2457 	vsi = ice_get_vf_vsi(vf);
2458 	if (!vsi) {
2459 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2460 		goto error_param;
2461 	}
2462 
2463 	if (vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q))
2464 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2465 
2466 error_param:
2467 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2468 				     v_ret, NULL, 0);
2469 }
2470 
2471 /**
2472  * ice_vc_dis_vlan_stripping
2473  * @vf: pointer to the VF info
2474  *
2475  * Disable VLAN header stripping for a given VF
2476  */
2477 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2478 {
2479 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2480 	struct ice_vsi *vsi;
2481 
2482 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2483 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2484 		goto error_param;
2485 	}
2486 
2487 	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2488 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2489 		goto error_param;
2490 	}
2491 
2492 	vsi = ice_get_vf_vsi(vf);
2493 	if (!vsi) {
2494 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2495 		goto error_param;
2496 	}
2497 
2498 	if (vsi->inner_vlan_ops.dis_stripping(vsi))
2499 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2500 
2501 error_param:
2502 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2503 				     v_ret, NULL, 0);
2504 }
2505 
2506 /**
2507  * ice_vc_get_rss_hena - return the RSS HENA bits allowed by the hardware
2508  * @vf: pointer to the VF info
2509  */
2510 static int ice_vc_get_rss_hena(struct ice_vf *vf)
2511 {
2512 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2513 	struct virtchnl_rss_hena *vrh = NULL;
2514 	int len = 0, ret;
2515 
2516 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2517 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2518 		goto err;
2519 	}
2520 
2521 	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2522 		dev_err(ice_pf_to_dev(vf->pf), "RSS not supported by PF\n");
2523 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2524 		goto err;
2525 	}
2526 
2527 	len = sizeof(struct virtchnl_rss_hena);
2528 	vrh = kzalloc(len, GFP_KERNEL);
2529 	if (!vrh) {
2530 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2531 		len = 0;
2532 		goto err;
2533 	}
2534 
2535 	vrh->hena = ICE_DEFAULT_RSS_HENA;
2536 err:
2537 	/* send the response back to the VF */
2538 	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS, v_ret,
2539 				    (u8 *)vrh, len);
2540 	kfree(vrh);
2541 	return ret;
2542 }
2543 
2544 /**
2545  * ice_vc_set_rss_hena - set RSS HENA bits for the VF
2546  * @vf: pointer to the VF info
2547  * @msg: pointer to the msg buffer
2548  */
2549 static int ice_vc_set_rss_hena(struct ice_vf *vf, u8 *msg)
2550 {
2551 	struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
2552 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2553 	struct ice_pf *pf = vf->pf;
2554 	struct ice_vsi *vsi;
2555 	struct device *dev;
2556 	int status;
2557 
2558 	dev = ice_pf_to_dev(pf);
2559 
2560 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2561 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2562 		goto err;
2563 	}
2564 
2565 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2566 		dev_err(dev, "RSS not supported by PF\n");
2567 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2568 		goto err;
2569 	}
2570 
2571 	vsi = ice_get_vf_vsi(vf);
2572 	if (!vsi) {
2573 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2574 		goto err;
2575 	}
2576 
2577 	/* clear all previously programmed RSS configuration to allow VF drivers
2578 	 * the ability to customize the RSS configuration and/or completely
2579 	 * disable RSS
2580 	 */
2581 	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
2582 	if (status && !vrh->hena) {
2583 		/* only report failure to clear the current RSS configuration if
2584 		 * that was clearly the VF's intention (i.e. vrh->hena = 0)
2585 		 */
2586 		v_ret = ice_err_to_virt_err(status);
2587 		goto err;
2588 	} else if (status) {
2589 		/* allow the VF to update the RSS configuration even on failure
2590 		 * to clear the current RSS confguration in an attempt to keep
2591 		 * RSS in a working state
2592 		 */
2593 		dev_warn(dev, "Failed to clear the RSS configuration for VF %u\n",
2594 			 vf->vf_id);
2595 	}
2596 
2597 	if (vrh->hena) {
2598 		status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, vrh->hena);
2599 		v_ret = ice_err_to_virt_err(status);
2600 	}
2601 
2602 	/* send the response to the VF */
2603 err:
2604 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, v_ret,
2605 				     NULL, 0);
2606 }
2607 
2608 /**
2609  * ice_vc_query_rxdid - query RXDID supported by DDP package
2610  * @vf: pointer to VF info
2611  *
2612  * Called from VF to query a bitmap of supported flexible
2613  * descriptor RXDIDs of a DDP package.
2614  */
2615 static int ice_vc_query_rxdid(struct ice_vf *vf)
2616 {
2617 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2618 	struct virtchnl_supported_rxdids *rxdid = NULL;
2619 	struct ice_hw *hw = &vf->pf->hw;
2620 	struct ice_pf *pf = vf->pf;
2621 	int len = 0;
2622 	int ret, i;
2623 	u32 regval;
2624 
2625 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2626 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2627 		goto err;
2628 	}
2629 
2630 	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)) {
2631 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2632 		goto err;
2633 	}
2634 
2635 	len = sizeof(struct virtchnl_supported_rxdids);
2636 	rxdid = kzalloc(len, GFP_KERNEL);
2637 	if (!rxdid) {
2638 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2639 		len = 0;
2640 		goto err;
2641 	}
2642 
2643 	/* RXDIDs supported by DDP package can be read from the register
2644 	 * to get the supported RXDID bitmap. But the legacy 32byte RXDID
2645 	 * is not listed in DDP package, add it in the bitmap manually.
2646 	 * Legacy 16byte descriptor is not supported.
2647 	 */
2648 	rxdid->supported_rxdids |= BIT(ICE_RXDID_LEGACY_1);
2649 
2650 	for (i = ICE_RXDID_FLEX_NIC; i < ICE_FLEX_DESC_RXDID_MAX_NUM; i++) {
2651 		regval = rd32(hw, GLFLXP_RXDID_FLAGS(i, 0));
2652 		if ((regval >> GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_S)
2653 			& GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_M)
2654 			rxdid->supported_rxdids |= BIT(i);
2655 	}
2656 
2657 	pf->supported_rxdids = rxdid->supported_rxdids;
2658 
2659 err:
2660 	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_SUPPORTED_RXDIDS,
2661 				    v_ret, (u8 *)rxdid, len);
2662 	kfree(rxdid);
2663 	return ret;
2664 }
2665 
2666 /**
2667  * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
2668  * @vf: VF to enable/disable VLAN stripping for on initialization
2669  *
2670  * Set the default for VLAN stripping based on whether a port VLAN is configured
2671  * and the current VLAN mode of the device.
2672  */
2673 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
2674 {
2675 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
2676 
2677 	if (!vsi)
2678 		return -EINVAL;
2679 
2680 	/* don't modify stripping if port VLAN is configured in SVM since the
2681 	 * port VLAN is based on the inner/single VLAN in SVM
2682 	 */
2683 	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&vsi->back->hw))
2684 		return 0;
2685 
2686 	if (ice_vf_vlan_offload_ena(vf->driver_caps))
2687 		return vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q);
2688 	else
2689 		return vsi->inner_vlan_ops.dis_stripping(vsi);
2690 }
2691 
2692 static u16 ice_vc_get_max_vlan_fltrs(struct ice_vf *vf)
2693 {
2694 	if (vf->trusted)
2695 		return VLAN_N_VID;
2696 	else
2697 		return ICE_MAX_VLAN_PER_VF;
2698 }
2699 
2700 /**
2701  * ice_vf_outer_vlan_not_allowed - check if outer VLAN can be used
2702  * @vf: VF that being checked for
2703  *
2704  * When the device is in double VLAN mode, check whether or not the outer VLAN
2705  * is allowed.
2706  */
2707 static bool ice_vf_outer_vlan_not_allowed(struct ice_vf *vf)
2708 {
2709 	if (ice_vf_is_port_vlan_ena(vf))
2710 		return true;
2711 
2712 	return false;
2713 }
2714 
2715 /**
2716  * ice_vc_set_dvm_caps - set VLAN capabilities when the device is in DVM
2717  * @vf: VF that capabilities are being set for
2718  * @caps: VLAN capabilities to populate
2719  *
2720  * Determine VLAN capabilities support based on whether a port VLAN is
2721  * configured. If a port VLAN is configured then the VF should use the inner
2722  * filtering/offload capabilities since the port VLAN is using the outer VLAN
2723  * capabilies.
2724  */
2725 static void
2726 ice_vc_set_dvm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
2727 {
2728 	struct virtchnl_vlan_supported_caps *supported_caps;
2729 
2730 	if (ice_vf_outer_vlan_not_allowed(vf)) {
2731 		/* until support for inner VLAN filtering is added when a port
2732 		 * VLAN is configured, only support software offloaded inner
2733 		 * VLANs when a port VLAN is confgured in DVM
2734 		 */
2735 		supported_caps = &caps->filtering.filtering_support;
2736 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2737 
2738 		supported_caps = &caps->offloads.stripping_support;
2739 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2740 					VIRTCHNL_VLAN_TOGGLE |
2741 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2742 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2743 
2744 		supported_caps = &caps->offloads.insertion_support;
2745 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2746 					VIRTCHNL_VLAN_TOGGLE |
2747 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2748 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2749 
2750 		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2751 		caps->offloads.ethertype_match =
2752 			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2753 	} else {
2754 		supported_caps = &caps->filtering.filtering_support;
2755 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2756 		supported_caps->outer = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2757 					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2758 					VIRTCHNL_VLAN_ETHERTYPE_9100 |
2759 					VIRTCHNL_VLAN_ETHERTYPE_AND;
2760 		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2761 						 VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2762 						 VIRTCHNL_VLAN_ETHERTYPE_9100;
2763 
2764 		supported_caps = &caps->offloads.stripping_support;
2765 		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
2766 					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2767 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2768 		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
2769 					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2770 					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2771 					VIRTCHNL_VLAN_ETHERTYPE_9100 |
2772 					VIRTCHNL_VLAN_ETHERTYPE_XOR |
2773 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2;
2774 
2775 		supported_caps = &caps->offloads.insertion_support;
2776 		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
2777 					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2778 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2779 		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
2780 					VIRTCHNL_VLAN_ETHERTYPE_8100 |
2781 					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
2782 					VIRTCHNL_VLAN_ETHERTYPE_9100 |
2783 					VIRTCHNL_VLAN_ETHERTYPE_XOR |
2784 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2;
2785 
2786 		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2787 
2788 		caps->offloads.ethertype_match =
2789 			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2790 	}
2791 
2792 	caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
2793 }
2794 
2795 /**
2796  * ice_vc_set_svm_caps - set VLAN capabilities when the device is in SVM
2797  * @vf: VF that capabilities are being set for
2798  * @caps: VLAN capabilities to populate
2799  *
2800  * Determine VLAN capabilities support based on whether a port VLAN is
2801  * configured. If a port VLAN is configured then the VF does not have any VLAN
2802  * filtering or offload capabilities since the port VLAN is using the inner VLAN
2803  * capabilities in single VLAN mode (SVM). Otherwise allow the VF to use inner
2804  * VLAN fitlering and offload capabilities.
2805  */
2806 static void
2807 ice_vc_set_svm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
2808 {
2809 	struct virtchnl_vlan_supported_caps *supported_caps;
2810 
2811 	if (ice_vf_is_port_vlan_ena(vf)) {
2812 		supported_caps = &caps->filtering.filtering_support;
2813 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2814 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2815 
2816 		supported_caps = &caps->offloads.stripping_support;
2817 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2818 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2819 
2820 		supported_caps = &caps->offloads.insertion_support;
2821 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
2822 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2823 
2824 		caps->offloads.ethertype_init = VIRTCHNL_VLAN_UNSUPPORTED;
2825 		caps->offloads.ethertype_match = VIRTCHNL_VLAN_UNSUPPORTED;
2826 		caps->filtering.max_filters = 0;
2827 	} else {
2828 		supported_caps = &caps->filtering.filtering_support;
2829 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100;
2830 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2831 		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2832 
2833 		supported_caps = &caps->offloads.stripping_support;
2834 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2835 					VIRTCHNL_VLAN_TOGGLE |
2836 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2837 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2838 
2839 		supported_caps = &caps->offloads.insertion_support;
2840 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
2841 					VIRTCHNL_VLAN_TOGGLE |
2842 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
2843 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
2844 
2845 		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
2846 		caps->offloads.ethertype_match =
2847 			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
2848 		caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
2849 	}
2850 }
2851 
2852 /**
2853  * ice_vc_get_offload_vlan_v2_caps - determine VF's VLAN capabilities
2854  * @vf: VF to determine VLAN capabilities for
2855  *
2856  * This will only be called if the VF and PF successfully negotiated
2857  * VIRTCHNL_VF_OFFLOAD_VLAN_V2.
2858  *
2859  * Set VLAN capabilities based on the current VLAN mode and whether a port VLAN
2860  * is configured or not.
2861  */
2862 static int ice_vc_get_offload_vlan_v2_caps(struct ice_vf *vf)
2863 {
2864 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2865 	struct virtchnl_vlan_caps *caps = NULL;
2866 	int err, len = 0;
2867 
2868 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2869 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2870 		goto out;
2871 	}
2872 
2873 	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
2874 	if (!caps) {
2875 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2876 		goto out;
2877 	}
2878 	len = sizeof(*caps);
2879 
2880 	if (ice_is_dvm_ena(&vf->pf->hw))
2881 		ice_vc_set_dvm_caps(vf, caps);
2882 	else
2883 		ice_vc_set_svm_caps(vf, caps);
2884 
2885 	/* store negotiated caps to prevent invalid VF messages */
2886 	memcpy(&vf->vlan_v2_caps, caps, sizeof(*caps));
2887 
2888 out:
2889 	err = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS,
2890 				    v_ret, (u8 *)caps, len);
2891 	kfree(caps);
2892 	return err;
2893 }
2894 
2895 /**
2896  * ice_vc_validate_vlan_tpid - validate VLAN TPID
2897  * @filtering_caps: negotiated/supported VLAN filtering capabilities
2898  * @tpid: VLAN TPID used for validation
2899  *
2900  * Convert the VLAN TPID to a VIRTCHNL_VLAN_ETHERTYPE_* and then compare against
2901  * the negotiated/supported filtering caps to see if the VLAN TPID is valid.
2902  */
2903 static bool ice_vc_validate_vlan_tpid(u16 filtering_caps, u16 tpid)
2904 {
2905 	enum virtchnl_vlan_support vlan_ethertype = VIRTCHNL_VLAN_UNSUPPORTED;
2906 
2907 	switch (tpid) {
2908 	case ETH_P_8021Q:
2909 		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
2910 		break;
2911 	case ETH_P_8021AD:
2912 		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_88A8;
2913 		break;
2914 	case ETH_P_QINQ1:
2915 		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_9100;
2916 		break;
2917 	}
2918 
2919 	if (!(filtering_caps & vlan_ethertype))
2920 		return false;
2921 
2922 	return true;
2923 }
2924 
2925 /**
2926  * ice_vc_is_valid_vlan - validate the virtchnl_vlan
2927  * @vc_vlan: virtchnl_vlan to validate
2928  *
2929  * If the VLAN TCI and VLAN TPID are 0, then this filter is invalid, so return
2930  * false. Otherwise return true.
2931  */
2932 static bool ice_vc_is_valid_vlan(struct virtchnl_vlan *vc_vlan)
2933 {
2934 	if (!vc_vlan->tci || !vc_vlan->tpid)
2935 		return false;
2936 
2937 	return true;
2938 }
2939 
2940 /**
2941  * ice_vc_validate_vlan_filter_list - validate the filter list from the VF
2942  * @vfc: negotiated/supported VLAN filtering capabilities
2943  * @vfl: VLAN filter list from VF to validate
2944  *
2945  * Validate all of the filters in the VLAN filter list from the VF. If any of
2946  * the checks fail then return false. Otherwise return true.
2947  */
2948 static bool
2949 ice_vc_validate_vlan_filter_list(struct virtchnl_vlan_filtering_caps *vfc,
2950 				 struct virtchnl_vlan_filter_list_v2 *vfl)
2951 {
2952 	u16 i;
2953 
2954 	if (!vfl->num_elements)
2955 		return false;
2956 
2957 	for (i = 0; i < vfl->num_elements; i++) {
2958 		struct virtchnl_vlan_supported_caps *filtering_support =
2959 			&vfc->filtering_support;
2960 		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
2961 		struct virtchnl_vlan *outer = &vlan_fltr->outer;
2962 		struct virtchnl_vlan *inner = &vlan_fltr->inner;
2963 
2964 		if ((ice_vc_is_valid_vlan(outer) &&
2965 		     filtering_support->outer == VIRTCHNL_VLAN_UNSUPPORTED) ||
2966 		    (ice_vc_is_valid_vlan(inner) &&
2967 		     filtering_support->inner == VIRTCHNL_VLAN_UNSUPPORTED))
2968 			return false;
2969 
2970 		if ((outer->tci_mask &&
2971 		     !(filtering_support->outer & VIRTCHNL_VLAN_FILTER_MASK)) ||
2972 		    (inner->tci_mask &&
2973 		     !(filtering_support->inner & VIRTCHNL_VLAN_FILTER_MASK)))
2974 			return false;
2975 
2976 		if (((outer->tci & VLAN_PRIO_MASK) &&
2977 		     !(filtering_support->outer & VIRTCHNL_VLAN_PRIO)) ||
2978 		    ((inner->tci & VLAN_PRIO_MASK) &&
2979 		     !(filtering_support->inner & VIRTCHNL_VLAN_PRIO)))
2980 			return false;
2981 
2982 		if ((ice_vc_is_valid_vlan(outer) &&
2983 		     !ice_vc_validate_vlan_tpid(filtering_support->outer,
2984 						outer->tpid)) ||
2985 		    (ice_vc_is_valid_vlan(inner) &&
2986 		     !ice_vc_validate_vlan_tpid(filtering_support->inner,
2987 						inner->tpid)))
2988 			return false;
2989 	}
2990 
2991 	return true;
2992 }
2993 
2994 /**
2995  * ice_vc_to_vlan - transform from struct virtchnl_vlan to struct ice_vlan
2996  * @vc_vlan: struct virtchnl_vlan to transform
2997  */
2998 static struct ice_vlan ice_vc_to_vlan(struct virtchnl_vlan *vc_vlan)
2999 {
3000 	struct ice_vlan vlan = { 0 };
3001 
3002 	vlan.prio = (vc_vlan->tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
3003 	vlan.vid = vc_vlan->tci & VLAN_VID_MASK;
3004 	vlan.tpid = vc_vlan->tpid;
3005 
3006 	return vlan;
3007 }
3008 
3009 /**
3010  * ice_vc_vlan_action - action to perform on the virthcnl_vlan
3011  * @vsi: VF's VSI used to perform the action
3012  * @vlan_action: function to perform the action with (i.e. add/del)
3013  * @vlan: VLAN filter to perform the action with
3014  */
3015 static int
3016 ice_vc_vlan_action(struct ice_vsi *vsi,
3017 		   int (*vlan_action)(struct ice_vsi *, struct ice_vlan *),
3018 		   struct ice_vlan *vlan)
3019 {
3020 	int err;
3021 
3022 	err = vlan_action(vsi, vlan);
3023 	if (err)
3024 		return err;
3025 
3026 	return 0;
3027 }
3028 
3029 /**
3030  * ice_vc_del_vlans - delete VLAN(s) from the virtchnl filter list
3031  * @vf: VF used to delete the VLAN(s)
3032  * @vsi: VF's VSI used to delete the VLAN(s)
3033  * @vfl: virthchnl filter list used to delete the filters
3034  */
3035 static int
3036 ice_vc_del_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3037 		 struct virtchnl_vlan_filter_list_v2 *vfl)
3038 {
3039 	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3040 	int err;
3041 	u16 i;
3042 
3043 	for (i = 0; i < vfl->num_elements; i++) {
3044 		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3045 		struct virtchnl_vlan *vc_vlan;
3046 
3047 		vc_vlan = &vlan_fltr->outer;
3048 		if (ice_vc_is_valid_vlan(vc_vlan)) {
3049 			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3050 
3051 			err = ice_vc_vlan_action(vsi,
3052 						 vsi->outer_vlan_ops.del_vlan,
3053 						 &vlan);
3054 			if (err)
3055 				return err;
3056 
3057 			if (vlan_promisc)
3058 				ice_vf_dis_vlan_promisc(vsi, &vlan);
3059 
3060 			/* Disable VLAN filtering when only VLAN 0 is left */
3061 			if (!ice_vsi_has_non_zero_vlans(vsi) && ice_is_dvm_ena(&vsi->back->hw)) {
3062 				err = vsi->outer_vlan_ops.dis_tx_filtering(vsi);
3063 				if (err)
3064 					return err;
3065 			}
3066 		}
3067 
3068 		vc_vlan = &vlan_fltr->inner;
3069 		if (ice_vc_is_valid_vlan(vc_vlan)) {
3070 			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3071 
3072 			err = ice_vc_vlan_action(vsi,
3073 						 vsi->inner_vlan_ops.del_vlan,
3074 						 &vlan);
3075 			if (err)
3076 				return err;
3077 
3078 			/* no support for VLAN promiscuous on inner VLAN unless
3079 			 * we are in Single VLAN Mode (SVM)
3080 			 */
3081 			if (!ice_is_dvm_ena(&vsi->back->hw)) {
3082 				if (vlan_promisc)
3083 					ice_vf_dis_vlan_promisc(vsi, &vlan);
3084 
3085 				/* Disable VLAN filtering when only VLAN 0 is left */
3086 				if (!ice_vsi_has_non_zero_vlans(vsi)) {
3087 					err = vsi->inner_vlan_ops.dis_tx_filtering(vsi);
3088 					if (err)
3089 						return err;
3090 				}
3091 			}
3092 		}
3093 	}
3094 
3095 	return 0;
3096 }
3097 
3098 /**
3099  * ice_vc_remove_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_DEL_VLAN_V2
3100  * @vf: VF the message was received from
3101  * @msg: message received from the VF
3102  */
3103 static int ice_vc_remove_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3104 {
3105 	struct virtchnl_vlan_filter_list_v2 *vfl =
3106 		(struct virtchnl_vlan_filter_list_v2 *)msg;
3107 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3108 	struct ice_vsi *vsi;
3109 
3110 	if (!ice_vc_validate_vlan_filter_list(&vf->vlan_v2_caps.filtering,
3111 					      vfl)) {
3112 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3113 		goto out;
3114 	}
3115 
3116 	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3117 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3118 		goto out;
3119 	}
3120 
3121 	vsi = ice_get_vf_vsi(vf);
3122 	if (!vsi) {
3123 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3124 		goto out;
3125 	}
3126 
3127 	if (ice_vc_del_vlans(vf, vsi, vfl))
3128 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3129 
3130 out:
3131 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN_V2, v_ret, NULL,
3132 				     0);
3133 }
3134 
3135 /**
3136  * ice_vc_add_vlans - add VLAN(s) from the virtchnl filter list
3137  * @vf: VF used to add the VLAN(s)
3138  * @vsi: VF's VSI used to add the VLAN(s)
3139  * @vfl: virthchnl filter list used to add the filters
3140  */
3141 static int
3142 ice_vc_add_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3143 		 struct virtchnl_vlan_filter_list_v2 *vfl)
3144 {
3145 	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3146 	int err;
3147 	u16 i;
3148 
3149 	for (i = 0; i < vfl->num_elements; i++) {
3150 		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3151 		struct virtchnl_vlan *vc_vlan;
3152 
3153 		vc_vlan = &vlan_fltr->outer;
3154 		if (ice_vc_is_valid_vlan(vc_vlan)) {
3155 			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3156 
3157 			err = ice_vc_vlan_action(vsi,
3158 						 vsi->outer_vlan_ops.add_vlan,
3159 						 &vlan);
3160 			if (err)
3161 				return err;
3162 
3163 			if (vlan_promisc) {
3164 				err = ice_vf_ena_vlan_promisc(vsi, &vlan);
3165 				if (err)
3166 					return err;
3167 			}
3168 
3169 			/* Enable VLAN filtering on first non-zero VLAN */
3170 			if (vf->spoofchk && vlan.vid && ice_is_dvm_ena(&vsi->back->hw)) {
3171 				err = vsi->outer_vlan_ops.ena_tx_filtering(vsi);
3172 				if (err)
3173 					return err;
3174 			}
3175 		}
3176 
3177 		vc_vlan = &vlan_fltr->inner;
3178 		if (ice_vc_is_valid_vlan(vc_vlan)) {
3179 			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3180 
3181 			err = ice_vc_vlan_action(vsi,
3182 						 vsi->inner_vlan_ops.add_vlan,
3183 						 &vlan);
3184 			if (err)
3185 				return err;
3186 
3187 			/* no support for VLAN promiscuous on inner VLAN unless
3188 			 * we are in Single VLAN Mode (SVM)
3189 			 */
3190 			if (!ice_is_dvm_ena(&vsi->back->hw)) {
3191 				if (vlan_promisc) {
3192 					err = ice_vf_ena_vlan_promisc(vsi, &vlan);
3193 					if (err)
3194 						return err;
3195 				}
3196 
3197 				/* Enable VLAN filtering on first non-zero VLAN */
3198 				if (vf->spoofchk && vlan.vid) {
3199 					err = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
3200 					if (err)
3201 						return err;
3202 				}
3203 			}
3204 		}
3205 	}
3206 
3207 	return 0;
3208 }
3209 
3210 /**
3211  * ice_vc_validate_add_vlan_filter_list - validate add filter list from the VF
3212  * @vsi: VF VSI used to get number of existing VLAN filters
3213  * @vfc: negotiated/supported VLAN filtering capabilities
3214  * @vfl: VLAN filter list from VF to validate
3215  *
3216  * Validate all of the filters in the VLAN filter list from the VF during the
3217  * VIRTCHNL_OP_ADD_VLAN_V2 opcode. If any of the checks fail then return false.
3218  * Otherwise return true.
3219  */
3220 static bool
3221 ice_vc_validate_add_vlan_filter_list(struct ice_vsi *vsi,
3222 				     struct virtchnl_vlan_filtering_caps *vfc,
3223 				     struct virtchnl_vlan_filter_list_v2 *vfl)
3224 {
3225 	u16 num_requested_filters = ice_vsi_num_non_zero_vlans(vsi) +
3226 		vfl->num_elements;
3227 
3228 	if (num_requested_filters > vfc->max_filters)
3229 		return false;
3230 
3231 	return ice_vc_validate_vlan_filter_list(vfc, vfl);
3232 }
3233 
3234 /**
3235  * ice_vc_add_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_ADD_VLAN_V2
3236  * @vf: VF the message was received from
3237  * @msg: message received from the VF
3238  */
3239 static int ice_vc_add_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3240 {
3241 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3242 	struct virtchnl_vlan_filter_list_v2 *vfl =
3243 		(struct virtchnl_vlan_filter_list_v2 *)msg;
3244 	struct ice_vsi *vsi;
3245 
3246 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3247 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3248 		goto out;
3249 	}
3250 
3251 	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3252 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3253 		goto out;
3254 	}
3255 
3256 	vsi = ice_get_vf_vsi(vf);
3257 	if (!vsi) {
3258 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3259 		goto out;
3260 	}
3261 
3262 	if (!ice_vc_validate_add_vlan_filter_list(vsi,
3263 						  &vf->vlan_v2_caps.filtering,
3264 						  vfl)) {
3265 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3266 		goto out;
3267 	}
3268 
3269 	if (ice_vc_add_vlans(vf, vsi, vfl))
3270 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3271 
3272 out:
3273 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN_V2, v_ret, NULL,
3274 				     0);
3275 }
3276 
3277 /**
3278  * ice_vc_valid_vlan_setting - validate VLAN setting
3279  * @negotiated_settings: negotiated VLAN settings during VF init
3280  * @ethertype_setting: ethertype(s) requested for the VLAN setting
3281  */
3282 static bool
3283 ice_vc_valid_vlan_setting(u32 negotiated_settings, u32 ethertype_setting)
3284 {
3285 	if (ethertype_setting && !(negotiated_settings & ethertype_setting))
3286 		return false;
3287 
3288 	/* only allow a single VIRTCHNL_VLAN_ETHERTYPE if
3289 	 * VIRTHCNL_VLAN_ETHERTYPE_AND is not negotiated/supported
3290 	 */
3291 	if (!(negotiated_settings & VIRTCHNL_VLAN_ETHERTYPE_AND) &&
3292 	    hweight32(ethertype_setting) > 1)
3293 		return false;
3294 
3295 	/* ability to modify the VLAN setting was not negotiated */
3296 	if (!(negotiated_settings & VIRTCHNL_VLAN_TOGGLE))
3297 		return false;
3298 
3299 	return true;
3300 }
3301 
3302 /**
3303  * ice_vc_valid_vlan_setting_msg - validate the VLAN setting message
3304  * @caps: negotiated VLAN settings during VF init
3305  * @msg: message to validate
3306  *
3307  * Used to validate any VLAN virtchnl message sent as a
3308  * virtchnl_vlan_setting structure. Validates the message against the
3309  * negotiated/supported caps during VF driver init.
3310  */
3311 static bool
3312 ice_vc_valid_vlan_setting_msg(struct virtchnl_vlan_supported_caps *caps,
3313 			      struct virtchnl_vlan_setting *msg)
3314 {
3315 	if ((!msg->outer_ethertype_setting &&
3316 	     !msg->inner_ethertype_setting) ||
3317 	    (!caps->outer && !caps->inner))
3318 		return false;
3319 
3320 	if (msg->outer_ethertype_setting &&
3321 	    !ice_vc_valid_vlan_setting(caps->outer,
3322 				       msg->outer_ethertype_setting))
3323 		return false;
3324 
3325 	if (msg->inner_ethertype_setting &&
3326 	    !ice_vc_valid_vlan_setting(caps->inner,
3327 				       msg->inner_ethertype_setting))
3328 		return false;
3329 
3330 	return true;
3331 }
3332 
3333 /**
3334  * ice_vc_get_tpid - transform from VIRTCHNL_VLAN_ETHERTYPE_* to VLAN TPID
3335  * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* used to get VLAN TPID
3336  * @tpid: VLAN TPID to populate
3337  */
3338 static int ice_vc_get_tpid(u32 ethertype_setting, u16 *tpid)
3339 {
3340 	switch (ethertype_setting) {
3341 	case VIRTCHNL_VLAN_ETHERTYPE_8100:
3342 		*tpid = ETH_P_8021Q;
3343 		break;
3344 	case VIRTCHNL_VLAN_ETHERTYPE_88A8:
3345 		*tpid = ETH_P_8021AD;
3346 		break;
3347 	case VIRTCHNL_VLAN_ETHERTYPE_9100:
3348 		*tpid = ETH_P_QINQ1;
3349 		break;
3350 	default:
3351 		*tpid = 0;
3352 		return -EINVAL;
3353 	}
3354 
3355 	return 0;
3356 }
3357 
3358 /**
3359  * ice_vc_ena_vlan_offload - enable VLAN offload based on the ethertype_setting
3360  * @vsi: VF's VSI used to enable the VLAN offload
3361  * @ena_offload: function used to enable the VLAN offload
3362  * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* to enable offloads for
3363  */
3364 static int
3365 ice_vc_ena_vlan_offload(struct ice_vsi *vsi,
3366 			int (*ena_offload)(struct ice_vsi *vsi, u16 tpid),
3367 			u32 ethertype_setting)
3368 {
3369 	u16 tpid;
3370 	int err;
3371 
3372 	err = ice_vc_get_tpid(ethertype_setting, &tpid);
3373 	if (err)
3374 		return err;
3375 
3376 	err = ena_offload(vsi, tpid);
3377 	if (err)
3378 		return err;
3379 
3380 	return 0;
3381 }
3382 
3383 #define ICE_L2TSEL_QRX_CONTEXT_REG_IDX	3
3384 #define ICE_L2TSEL_BIT_OFFSET		23
3385 enum ice_l2tsel {
3386 	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND,
3387 	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1,
3388 };
3389 
3390 /**
3391  * ice_vsi_update_l2tsel - update l2tsel field for all Rx rings on this VSI
3392  * @vsi: VSI used to update l2tsel on
3393  * @l2tsel: l2tsel setting requested
3394  *
3395  * Use the l2tsel setting to update all of the Rx queue context bits for l2tsel.
3396  * This will modify which descriptor field the first offloaded VLAN will be
3397  * stripped into.
3398  */
3399 static void ice_vsi_update_l2tsel(struct ice_vsi *vsi, enum ice_l2tsel l2tsel)
3400 {
3401 	struct ice_hw *hw = &vsi->back->hw;
3402 	u32 l2tsel_bit;
3403 	int i;
3404 
3405 	if (l2tsel == ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND)
3406 		l2tsel_bit = 0;
3407 	else
3408 		l2tsel_bit = BIT(ICE_L2TSEL_BIT_OFFSET);
3409 
3410 	for (i = 0; i < vsi->alloc_rxq; i++) {
3411 		u16 pfq = vsi->rxq_map[i];
3412 		u32 qrx_context_offset;
3413 		u32 regval;
3414 
3415 		qrx_context_offset =
3416 			QRX_CONTEXT(ICE_L2TSEL_QRX_CONTEXT_REG_IDX, pfq);
3417 
3418 		regval = rd32(hw, qrx_context_offset);
3419 		regval &= ~BIT(ICE_L2TSEL_BIT_OFFSET);
3420 		regval |= l2tsel_bit;
3421 		wr32(hw, qrx_context_offset, regval);
3422 	}
3423 }
3424 
3425 /**
3426  * ice_vc_ena_vlan_stripping_v2_msg
3427  * @vf: VF the message was received from
3428  * @msg: message received from the VF
3429  *
3430  * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2
3431  */
3432 static int ice_vc_ena_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3433 {
3434 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3435 	struct virtchnl_vlan_supported_caps *stripping_support;
3436 	struct virtchnl_vlan_setting *strip_msg =
3437 		(struct virtchnl_vlan_setting *)msg;
3438 	u32 ethertype_setting;
3439 	struct ice_vsi *vsi;
3440 
3441 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3442 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3443 		goto out;
3444 	}
3445 
3446 	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3447 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3448 		goto out;
3449 	}
3450 
3451 	vsi = ice_get_vf_vsi(vf);
3452 	if (!vsi) {
3453 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3454 		goto out;
3455 	}
3456 
3457 	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3458 	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3459 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3460 		goto out;
3461 	}
3462 
3463 	ethertype_setting = strip_msg->outer_ethertype_setting;
3464 	if (ethertype_setting) {
3465 		if (ice_vc_ena_vlan_offload(vsi,
3466 					    vsi->outer_vlan_ops.ena_stripping,
3467 					    ethertype_setting)) {
3468 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3469 			goto out;
3470 		} else {
3471 			enum ice_l2tsel l2tsel =
3472 				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND;
3473 
3474 			/* PF tells the VF that the outer VLAN tag is always
3475 			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3476 			 * inner is always extracted to
3477 			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3478 			 * support outer stripping so the first tag always ends
3479 			 * up in L2TAG2_2ND and the second/inner tag, if
3480 			 * enabled, is extracted in L2TAG1.
3481 			 */
3482 			ice_vsi_update_l2tsel(vsi, l2tsel);
3483 		}
3484 	}
3485 
3486 	ethertype_setting = strip_msg->inner_ethertype_setting;
3487 	if (ethertype_setting &&
3488 	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_stripping,
3489 				    ethertype_setting)) {
3490 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3491 		goto out;
3492 	}
3493 
3494 out:
3495 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2,
3496 				     v_ret, NULL, 0);
3497 }
3498 
3499 /**
3500  * ice_vc_dis_vlan_stripping_v2_msg
3501  * @vf: VF the message was received from
3502  * @msg: message received from the VF
3503  *
3504  * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2
3505  */
3506 static int ice_vc_dis_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3507 {
3508 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3509 	struct virtchnl_vlan_supported_caps *stripping_support;
3510 	struct virtchnl_vlan_setting *strip_msg =
3511 		(struct virtchnl_vlan_setting *)msg;
3512 	u32 ethertype_setting;
3513 	struct ice_vsi *vsi;
3514 
3515 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3516 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3517 		goto out;
3518 	}
3519 
3520 	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3521 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3522 		goto out;
3523 	}
3524 
3525 	vsi = ice_get_vf_vsi(vf);
3526 	if (!vsi) {
3527 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3528 		goto out;
3529 	}
3530 
3531 	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3532 	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3533 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3534 		goto out;
3535 	}
3536 
3537 	ethertype_setting = strip_msg->outer_ethertype_setting;
3538 	if (ethertype_setting) {
3539 		if (vsi->outer_vlan_ops.dis_stripping(vsi)) {
3540 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3541 			goto out;
3542 		} else {
3543 			enum ice_l2tsel l2tsel =
3544 				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1;
3545 
3546 			/* PF tells the VF that the outer VLAN tag is always
3547 			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3548 			 * inner is always extracted to
3549 			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3550 			 * support inner stripping while outer stripping is
3551 			 * disabled so that the first and only tag is extracted
3552 			 * in L2TAG1.
3553 			 */
3554 			ice_vsi_update_l2tsel(vsi, l2tsel);
3555 		}
3556 	}
3557 
3558 	ethertype_setting = strip_msg->inner_ethertype_setting;
3559 	if (ethertype_setting && vsi->inner_vlan_ops.dis_stripping(vsi)) {
3560 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3561 		goto out;
3562 	}
3563 
3564 out:
3565 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2,
3566 				     v_ret, NULL, 0);
3567 }
3568 
3569 /**
3570  * ice_vc_ena_vlan_insertion_v2_msg
3571  * @vf: VF the message was received from
3572  * @msg: message received from the VF
3573  *
3574  * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2
3575  */
3576 static int ice_vc_ena_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3577 {
3578 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3579 	struct virtchnl_vlan_supported_caps *insertion_support;
3580 	struct virtchnl_vlan_setting *insertion_msg =
3581 		(struct virtchnl_vlan_setting *)msg;
3582 	u32 ethertype_setting;
3583 	struct ice_vsi *vsi;
3584 
3585 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3586 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3587 		goto out;
3588 	}
3589 
3590 	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
3591 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3592 		goto out;
3593 	}
3594 
3595 	vsi = ice_get_vf_vsi(vf);
3596 	if (!vsi) {
3597 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3598 		goto out;
3599 	}
3600 
3601 	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
3602 	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
3603 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3604 		goto out;
3605 	}
3606 
3607 	ethertype_setting = insertion_msg->outer_ethertype_setting;
3608 	if (ethertype_setting &&
3609 	    ice_vc_ena_vlan_offload(vsi, vsi->outer_vlan_ops.ena_insertion,
3610 				    ethertype_setting)) {
3611 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3612 		goto out;
3613 	}
3614 
3615 	ethertype_setting = insertion_msg->inner_ethertype_setting;
3616 	if (ethertype_setting &&
3617 	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_insertion,
3618 				    ethertype_setting)) {
3619 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3620 		goto out;
3621 	}
3622 
3623 out:
3624 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2,
3625 				     v_ret, NULL, 0);
3626 }
3627 
3628 /**
3629  * ice_vc_dis_vlan_insertion_v2_msg
3630  * @vf: VF the message was received from
3631  * @msg: message received from the VF
3632  *
3633  * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2
3634  */
3635 static int ice_vc_dis_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3636 {
3637 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3638 	struct virtchnl_vlan_supported_caps *insertion_support;
3639 	struct virtchnl_vlan_setting *insertion_msg =
3640 		(struct virtchnl_vlan_setting *)msg;
3641 	u32 ethertype_setting;
3642 	struct ice_vsi *vsi;
3643 
3644 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3645 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3646 		goto out;
3647 	}
3648 
3649 	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
3650 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3651 		goto out;
3652 	}
3653 
3654 	vsi = ice_get_vf_vsi(vf);
3655 	if (!vsi) {
3656 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3657 		goto out;
3658 	}
3659 
3660 	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
3661 	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
3662 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3663 		goto out;
3664 	}
3665 
3666 	ethertype_setting = insertion_msg->outer_ethertype_setting;
3667 	if (ethertype_setting && vsi->outer_vlan_ops.dis_insertion(vsi)) {
3668 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3669 		goto out;
3670 	}
3671 
3672 	ethertype_setting = insertion_msg->inner_ethertype_setting;
3673 	if (ethertype_setting && vsi->inner_vlan_ops.dis_insertion(vsi)) {
3674 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3675 		goto out;
3676 	}
3677 
3678 out:
3679 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2,
3680 				     v_ret, NULL, 0);
3681 }
3682 
3683 static const struct ice_virtchnl_ops ice_virtchnl_dflt_ops = {
3684 	.get_ver_msg = ice_vc_get_ver_msg,
3685 	.get_vf_res_msg = ice_vc_get_vf_res_msg,
3686 	.reset_vf = ice_vc_reset_vf_msg,
3687 	.add_mac_addr_msg = ice_vc_add_mac_addr_msg,
3688 	.del_mac_addr_msg = ice_vc_del_mac_addr_msg,
3689 	.cfg_qs_msg = ice_vc_cfg_qs_msg,
3690 	.ena_qs_msg = ice_vc_ena_qs_msg,
3691 	.dis_qs_msg = ice_vc_dis_qs_msg,
3692 	.request_qs_msg = ice_vc_request_qs_msg,
3693 	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
3694 	.config_rss_key = ice_vc_config_rss_key,
3695 	.config_rss_lut = ice_vc_config_rss_lut,
3696 	.get_stats_msg = ice_vc_get_stats_msg,
3697 	.cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
3698 	.add_vlan_msg = ice_vc_add_vlan_msg,
3699 	.remove_vlan_msg = ice_vc_remove_vlan_msg,
3700 	.query_rxdid = ice_vc_query_rxdid,
3701 	.get_rss_hena = ice_vc_get_rss_hena,
3702 	.set_rss_hena_msg = ice_vc_set_rss_hena,
3703 	.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
3704 	.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
3705 	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
3706 	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
3707 	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
3708 	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
3709 	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
3710 	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
3711 	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
3712 	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
3713 	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
3714 	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
3715 };
3716 
3717 /**
3718  * ice_virtchnl_set_dflt_ops - Switch to default virtchnl ops
3719  * @vf: the VF to switch ops
3720  */
3721 void ice_virtchnl_set_dflt_ops(struct ice_vf *vf)
3722 {
3723 	vf->virtchnl_ops = &ice_virtchnl_dflt_ops;
3724 }
3725 
3726 /**
3727  * ice_vc_repr_add_mac
3728  * @vf: pointer to VF
3729  * @msg: virtchannel message
3730  *
3731  * When port representors are created, we do not add MAC rule
3732  * to firmware, we store it so that PF could report same
3733  * MAC as VF.
3734  */
3735 static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
3736 {
3737 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3738 	struct virtchnl_ether_addr_list *al =
3739 	    (struct virtchnl_ether_addr_list *)msg;
3740 	struct ice_vsi *vsi;
3741 	struct ice_pf *pf;
3742 	int i;
3743 
3744 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
3745 	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3746 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3747 		goto handle_mac_exit;
3748 	}
3749 
3750 	pf = vf->pf;
3751 
3752 	vsi = ice_get_vf_vsi(vf);
3753 	if (!vsi) {
3754 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3755 		goto handle_mac_exit;
3756 	}
3757 
3758 	for (i = 0; i < al->num_elements; i++) {
3759 		u8 *mac_addr = al->list[i].addr;
3760 
3761 		if (!is_unicast_ether_addr(mac_addr) ||
3762 		    ether_addr_equal(mac_addr, vf->hw_lan_addr))
3763 			continue;
3764 
3765 		if (vf->pf_set_mac) {
3766 			dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
3767 			v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
3768 			goto handle_mac_exit;
3769 		}
3770 
3771 		ice_vfhw_mac_add(vf, &al->list[i]);
3772 		vf->num_mac++;
3773 		break;
3774 	}
3775 
3776 handle_mac_exit:
3777 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
3778 				     v_ret, NULL, 0);
3779 }
3780 
3781 /**
3782  * ice_vc_repr_del_mac - response with success for deleting MAC
3783  * @vf: pointer to VF
3784  * @msg: virtchannel message
3785  *
3786  * Respond with success to not break normal VF flow.
3787  * For legacy VF driver try to update cached MAC address.
3788  */
3789 static int
3790 ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
3791 {
3792 	struct virtchnl_ether_addr_list *al =
3793 		(struct virtchnl_ether_addr_list *)msg;
3794 
3795 	ice_update_legacy_cached_mac(vf, &al->list[0]);
3796 
3797 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
3798 				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
3799 }
3800 
3801 static int
3802 ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
3803 {
3804 	dev_dbg(ice_pf_to_dev(vf->pf),
3805 		"Can't config promiscuous mode in switchdev mode for VF %d\n",
3806 		vf->vf_id);
3807 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
3808 				     VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
3809 				     NULL, 0);
3810 }
3811 
3812 static const struct ice_virtchnl_ops ice_virtchnl_repr_ops = {
3813 	.get_ver_msg = ice_vc_get_ver_msg,
3814 	.get_vf_res_msg = ice_vc_get_vf_res_msg,
3815 	.reset_vf = ice_vc_reset_vf_msg,
3816 	.add_mac_addr_msg = ice_vc_repr_add_mac,
3817 	.del_mac_addr_msg = ice_vc_repr_del_mac,
3818 	.cfg_qs_msg = ice_vc_cfg_qs_msg,
3819 	.ena_qs_msg = ice_vc_ena_qs_msg,
3820 	.dis_qs_msg = ice_vc_dis_qs_msg,
3821 	.request_qs_msg = ice_vc_request_qs_msg,
3822 	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
3823 	.config_rss_key = ice_vc_config_rss_key,
3824 	.config_rss_lut = ice_vc_config_rss_lut,
3825 	.get_stats_msg = ice_vc_get_stats_msg,
3826 	.cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode,
3827 	.add_vlan_msg = ice_vc_add_vlan_msg,
3828 	.remove_vlan_msg = ice_vc_remove_vlan_msg,
3829 	.query_rxdid = ice_vc_query_rxdid,
3830 	.get_rss_hena = ice_vc_get_rss_hena,
3831 	.set_rss_hena_msg = ice_vc_set_rss_hena,
3832 	.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
3833 	.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
3834 	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
3835 	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
3836 	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
3837 	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
3838 	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
3839 	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
3840 	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
3841 	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
3842 	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
3843 	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
3844 };
3845 
3846 /**
3847  * ice_virtchnl_set_repr_ops - Switch to representor virtchnl ops
3848  * @vf: the VF to switch ops
3849  */
3850 void ice_virtchnl_set_repr_ops(struct ice_vf *vf)
3851 {
3852 	vf->virtchnl_ops = &ice_virtchnl_repr_ops;
3853 }
3854 
3855 /**
3856  * ice_is_malicious_vf - check if this vf might be overflowing mailbox
3857  * @vf: the VF to check
3858  * @mbxdata: data about the state of the mailbox
3859  *
3860  * Detect if a given VF might be malicious and attempting to overflow the PF
3861  * mailbox. If so, log a warning message and ignore this event.
3862  */
3863 static bool
3864 ice_is_malicious_vf(struct ice_vf *vf, struct ice_mbx_data *mbxdata)
3865 {
3866 	bool report_malvf = false;
3867 	struct device *dev;
3868 	struct ice_pf *pf;
3869 	int status;
3870 
3871 	pf = vf->pf;
3872 	dev = ice_pf_to_dev(pf);
3873 
3874 	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
3875 		return vf->mbx_info.malicious;
3876 
3877 	/* check to see if we have a newly malicious VF */
3878 	status = ice_mbx_vf_state_handler(&pf->hw, mbxdata, &vf->mbx_info,
3879 					  &report_malvf);
3880 	if (status)
3881 		dev_warn_ratelimited(dev, "Unable to check status of mailbox overflow for VF %u MAC %pM, status %d\n",
3882 				     vf->vf_id, vf->dev_lan_addr, status);
3883 
3884 	if (report_malvf) {
3885 		struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
3886 		u8 zero_addr[ETH_ALEN] = {};
3887 
3888 		dev_warn(dev, "VF MAC %pM on PF MAC %pM is generating asynchronous messages and may be overflowing the PF message queue. Please see the Adapter User Guide for more information\n",
3889 			 vf->dev_lan_addr,
3890 			 pf_vsi ? pf_vsi->netdev->dev_addr : zero_addr);
3891 	}
3892 
3893 	return vf->mbx_info.malicious;
3894 }
3895 
3896 /**
3897  * ice_vc_process_vf_msg - Process request from VF
3898  * @pf: pointer to the PF structure
3899  * @event: pointer to the AQ event
3900  * @mbxdata: information used to detect VF attempting mailbox overflow
3901  *
3902  * called from the common asq/arq handler to
3903  * process request from VF
3904  */
3905 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event,
3906 			   struct ice_mbx_data *mbxdata)
3907 {
3908 	u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
3909 	s16 vf_id = le16_to_cpu(event->desc.retval);
3910 	const struct ice_virtchnl_ops *ops;
3911 	u16 msglen = event->msg_len;
3912 	u8 *msg = event->msg_buf;
3913 	struct ice_vf *vf = NULL;
3914 	struct device *dev;
3915 	int err = 0;
3916 
3917 	dev = ice_pf_to_dev(pf);
3918 
3919 	vf = ice_get_vf_by_id(pf, vf_id);
3920 	if (!vf) {
3921 		dev_err(dev, "Unable to locate VF for message from VF ID %d, opcode %d, len %d\n",
3922 			vf_id, v_opcode, msglen);
3923 		return;
3924 	}
3925 
3926 	mutex_lock(&vf->cfg_lock);
3927 
3928 	/* Check if the VF is trying to overflow the mailbox */
3929 	if (ice_is_malicious_vf(vf, mbxdata))
3930 		goto finish;
3931 
3932 	/* Check if VF is disabled. */
3933 	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
3934 		err = -EPERM;
3935 		goto error_handler;
3936 	}
3937 
3938 	ops = vf->virtchnl_ops;
3939 
3940 	/* Perform basic checks on the msg */
3941 	err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
3942 	if (err) {
3943 		if (err == VIRTCHNL_STATUS_ERR_PARAM)
3944 			err = -EPERM;
3945 		else
3946 			err = -EINVAL;
3947 	}
3948 
3949 error_handler:
3950 	if (err) {
3951 		ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
3952 				      NULL, 0);
3953 		dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
3954 			vf_id, v_opcode, msglen, err);
3955 		goto finish;
3956 	}
3957 
3958 	if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
3959 		ice_vc_send_msg_to_vf(vf, v_opcode,
3960 				      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
3961 				      0);
3962 		goto finish;
3963 	}
3964 
3965 	switch (v_opcode) {
3966 	case VIRTCHNL_OP_VERSION:
3967 		err = ops->get_ver_msg(vf, msg);
3968 		break;
3969 	case VIRTCHNL_OP_GET_VF_RESOURCES:
3970 		err = ops->get_vf_res_msg(vf, msg);
3971 		if (ice_vf_init_vlan_stripping(vf))
3972 			dev_dbg(dev, "Failed to initialize VLAN stripping for VF %d\n",
3973 				vf->vf_id);
3974 		ice_vc_notify_vf_link_state(vf);
3975 		break;
3976 	case VIRTCHNL_OP_RESET_VF:
3977 		ops->reset_vf(vf);
3978 		break;
3979 	case VIRTCHNL_OP_ADD_ETH_ADDR:
3980 		err = ops->add_mac_addr_msg(vf, msg);
3981 		break;
3982 	case VIRTCHNL_OP_DEL_ETH_ADDR:
3983 		err = ops->del_mac_addr_msg(vf, msg);
3984 		break;
3985 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3986 		err = ops->cfg_qs_msg(vf, msg);
3987 		break;
3988 	case VIRTCHNL_OP_ENABLE_QUEUES:
3989 		err = ops->ena_qs_msg(vf, msg);
3990 		ice_vc_notify_vf_link_state(vf);
3991 		break;
3992 	case VIRTCHNL_OP_DISABLE_QUEUES:
3993 		err = ops->dis_qs_msg(vf, msg);
3994 		break;
3995 	case VIRTCHNL_OP_REQUEST_QUEUES:
3996 		err = ops->request_qs_msg(vf, msg);
3997 		break;
3998 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3999 		err = ops->cfg_irq_map_msg(vf, msg);
4000 		break;
4001 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
4002 		err = ops->config_rss_key(vf, msg);
4003 		break;
4004 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
4005 		err = ops->config_rss_lut(vf, msg);
4006 		break;
4007 	case VIRTCHNL_OP_GET_STATS:
4008 		err = ops->get_stats_msg(vf, msg);
4009 		break;
4010 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4011 		err = ops->cfg_promiscuous_mode_msg(vf, msg);
4012 		break;
4013 	case VIRTCHNL_OP_ADD_VLAN:
4014 		err = ops->add_vlan_msg(vf, msg);
4015 		break;
4016 	case VIRTCHNL_OP_DEL_VLAN:
4017 		err = ops->remove_vlan_msg(vf, msg);
4018 		break;
4019 	case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
4020 		err = ops->query_rxdid(vf);
4021 		break;
4022 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
4023 		err = ops->get_rss_hena(vf);
4024 		break;
4025 	case VIRTCHNL_OP_SET_RSS_HENA:
4026 		err = ops->set_rss_hena_msg(vf, msg);
4027 		break;
4028 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4029 		err = ops->ena_vlan_stripping(vf);
4030 		break;
4031 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4032 		err = ops->dis_vlan_stripping(vf);
4033 		break;
4034 	case VIRTCHNL_OP_ADD_FDIR_FILTER:
4035 		err = ops->add_fdir_fltr_msg(vf, msg);
4036 		break;
4037 	case VIRTCHNL_OP_DEL_FDIR_FILTER:
4038 		err = ops->del_fdir_fltr_msg(vf, msg);
4039 		break;
4040 	case VIRTCHNL_OP_ADD_RSS_CFG:
4041 		err = ops->handle_rss_cfg_msg(vf, msg, true);
4042 		break;
4043 	case VIRTCHNL_OP_DEL_RSS_CFG:
4044 		err = ops->handle_rss_cfg_msg(vf, msg, false);
4045 		break;
4046 	case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
4047 		err = ops->get_offload_vlan_v2_caps(vf);
4048 		break;
4049 	case VIRTCHNL_OP_ADD_VLAN_V2:
4050 		err = ops->add_vlan_v2_msg(vf, msg);
4051 		break;
4052 	case VIRTCHNL_OP_DEL_VLAN_V2:
4053 		err = ops->remove_vlan_v2_msg(vf, msg);
4054 		break;
4055 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
4056 		err = ops->ena_vlan_stripping_v2_msg(vf, msg);
4057 		break;
4058 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
4059 		err = ops->dis_vlan_stripping_v2_msg(vf, msg);
4060 		break;
4061 	case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
4062 		err = ops->ena_vlan_insertion_v2_msg(vf, msg);
4063 		break;
4064 	case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
4065 		err = ops->dis_vlan_insertion_v2_msg(vf, msg);
4066 		break;
4067 	case VIRTCHNL_OP_UNKNOWN:
4068 	default:
4069 		dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4070 			vf_id);
4071 		err = ice_vc_send_msg_to_vf(vf, v_opcode,
4072 					    VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4073 					    NULL, 0);
4074 		break;
4075 	}
4076 	if (err) {
4077 		/* Helper function cares less about error return values here
4078 		 * as it is busy with pending work.
4079 		 */
4080 		dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4081 			 vf_id, v_opcode, err);
4082 	}
4083 
4084 finish:
4085 	mutex_unlock(&vf->cfg_lock);
4086 	ice_put_vf(vf);
4087 }
4088