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