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