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