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