xref: /linux/drivers/net/ethernet/intel/ice/ice_virtchnl.c (revision c3b999cad7ec39a5487b20e8b6e737d2ab0c5393)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2022, Intel Corporation. */
3 
4 #include "ice_virtchnl.h"
5 #include "ice_vf_lib_private.h"
6 #include "ice.h"
7 #include "ice_base.h"
8 #include "ice_lib.h"
9 #include "ice_fltr.h"
10 #include "ice_virtchnl_allowlist.h"
11 #include "ice_vf_vsi_vlan_ops.h"
12 #include "ice_vlan.h"
13 #include "ice_flex_pipe.h"
14 #include "ice_dcb_lib.h"
15 
16 #define FIELD_SELECTOR(proto_hdr_field) \
17 		BIT((proto_hdr_field) & PROTO_HDR_FIELD_MASK)
18 
19 struct ice_vc_hdr_match_type {
20 	u32 vc_hdr;	/* virtchnl headers (VIRTCHNL_PROTO_HDR_XXX) */
21 	u32 ice_hdr;	/* ice headers (ICE_FLOW_SEG_HDR_XXX) */
22 };
23 
24 static const struct ice_vc_hdr_match_type ice_vc_hdr_list[] = {
25 	{VIRTCHNL_PROTO_HDR_NONE,	ICE_FLOW_SEG_HDR_NONE},
26 	{VIRTCHNL_PROTO_HDR_ETH,	ICE_FLOW_SEG_HDR_ETH},
27 	{VIRTCHNL_PROTO_HDR_S_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
28 	{VIRTCHNL_PROTO_HDR_C_VLAN,	ICE_FLOW_SEG_HDR_VLAN},
29 	{VIRTCHNL_PROTO_HDR_IPV4,	ICE_FLOW_SEG_HDR_IPV4 |
30 					ICE_FLOW_SEG_HDR_IPV_OTHER},
31 	{VIRTCHNL_PROTO_HDR_IPV6,	ICE_FLOW_SEG_HDR_IPV6 |
32 					ICE_FLOW_SEG_HDR_IPV_OTHER},
33 	{VIRTCHNL_PROTO_HDR_TCP,	ICE_FLOW_SEG_HDR_TCP},
34 	{VIRTCHNL_PROTO_HDR_UDP,	ICE_FLOW_SEG_HDR_UDP},
35 	{VIRTCHNL_PROTO_HDR_SCTP,	ICE_FLOW_SEG_HDR_SCTP},
36 	{VIRTCHNL_PROTO_HDR_PPPOE,	ICE_FLOW_SEG_HDR_PPPOE},
37 	{VIRTCHNL_PROTO_HDR_GTPU_IP,	ICE_FLOW_SEG_HDR_GTPU_IP},
38 	{VIRTCHNL_PROTO_HDR_GTPU_EH,	ICE_FLOW_SEG_HDR_GTPU_EH},
39 	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_DWN,
40 					ICE_FLOW_SEG_HDR_GTPU_DWN},
41 	{VIRTCHNL_PROTO_HDR_GTPU_EH_PDU_UP,
42 					ICE_FLOW_SEG_HDR_GTPU_UP},
43 	{VIRTCHNL_PROTO_HDR_L2TPV3,	ICE_FLOW_SEG_HDR_L2TPV3},
44 	{VIRTCHNL_PROTO_HDR_ESP,	ICE_FLOW_SEG_HDR_ESP},
45 	{VIRTCHNL_PROTO_HDR_AH,		ICE_FLOW_SEG_HDR_AH},
46 	{VIRTCHNL_PROTO_HDR_PFCP,	ICE_FLOW_SEG_HDR_PFCP_SESSION},
47 };
48 
49 struct ice_vc_hash_field_match_type {
50 	u32 vc_hdr;		/* virtchnl headers
51 				 * (VIRTCHNL_PROTO_HDR_XXX)
52 				 */
53 	u32 vc_hash_field;	/* virtchnl hash fields selector
54 				 * FIELD_SELECTOR((VIRTCHNL_PROTO_HDR_ETH_XXX))
55 				 */
56 	u64 ice_hash_field;	/* ice hash fields
57 				 * (BIT_ULL(ICE_FLOW_FIELD_IDX_XXX))
58 				 */
59 };
60 
61 static const struct
62 ice_vc_hash_field_match_type ice_vc_hash_field_list[] = {
63 	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC),
64 		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_SA)},
65 	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
66 		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_DA)},
67 	{VIRTCHNL_PROTO_HDR_ETH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_SRC) |
68 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_DST),
69 		ICE_FLOW_HASH_ETH},
70 	{VIRTCHNL_PROTO_HDR_ETH,
71 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE),
72 		BIT_ULL(ICE_FLOW_FIELD_IDX_ETH_TYPE)},
73 	{VIRTCHNL_PROTO_HDR_S_VLAN,
74 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_S_VLAN_ID),
75 		BIT_ULL(ICE_FLOW_FIELD_IDX_S_VLAN)},
76 	{VIRTCHNL_PROTO_HDR_C_VLAN,
77 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_C_VLAN_ID),
78 		BIT_ULL(ICE_FLOW_FIELD_IDX_C_VLAN)},
79 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC),
80 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)},
81 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
82 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)},
83 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
84 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST),
85 		ICE_FLOW_HASH_IPV4},
86 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
87 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
88 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) |
89 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
90 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
91 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
92 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) |
93 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
94 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_SRC) |
95 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_DST) |
96 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
97 		ICE_FLOW_HASH_IPV4 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
98 	{VIRTCHNL_PROTO_HDR_IPV4, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV4_PROT),
99 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_PROT)},
100 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC),
101 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)},
102 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
103 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)},
104 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
105 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST),
106 		ICE_FLOW_HASH_IPV6},
107 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
108 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
109 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) |
110 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
111 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
112 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
113 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) |
114 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
115 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_SRC) |
116 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_DST) |
117 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
118 		ICE_FLOW_HASH_IPV6 | BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
119 	{VIRTCHNL_PROTO_HDR_IPV6, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_IPV6_PROT),
120 		BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_PROT)},
121 	{VIRTCHNL_PROTO_HDR_TCP,
122 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT),
123 		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)},
124 	{VIRTCHNL_PROTO_HDR_TCP,
125 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
126 		BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)},
127 	{VIRTCHNL_PROTO_HDR_TCP,
128 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_SRC_PORT) |
129 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_TCP_DST_PORT),
130 		ICE_FLOW_HASH_TCP_PORT},
131 	{VIRTCHNL_PROTO_HDR_UDP,
132 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT),
133 		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)},
134 	{VIRTCHNL_PROTO_HDR_UDP,
135 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
136 		BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)},
137 	{VIRTCHNL_PROTO_HDR_UDP,
138 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_SRC_PORT) |
139 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_UDP_DST_PORT),
140 		ICE_FLOW_HASH_UDP_PORT},
141 	{VIRTCHNL_PROTO_HDR_SCTP,
142 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT),
143 		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)},
144 	{VIRTCHNL_PROTO_HDR_SCTP,
145 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
146 		BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)},
147 	{VIRTCHNL_PROTO_HDR_SCTP,
148 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT) |
149 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_SCTP_DST_PORT),
150 		ICE_FLOW_HASH_SCTP_PORT},
151 	{VIRTCHNL_PROTO_HDR_PPPOE,
152 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PPPOE_SESS_ID),
153 		BIT_ULL(ICE_FLOW_FIELD_IDX_PPPOE_SESS_ID)},
154 	{VIRTCHNL_PROTO_HDR_GTPU_IP,
155 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_GTPU_IP_TEID),
156 		BIT_ULL(ICE_FLOW_FIELD_IDX_GTPU_IP_TEID)},
157 	{VIRTCHNL_PROTO_HDR_L2TPV3,
158 		FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID),
159 		BIT_ULL(ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID)},
160 	{VIRTCHNL_PROTO_HDR_ESP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_ESP_SPI),
161 		BIT_ULL(ICE_FLOW_FIELD_IDX_ESP_SPI)},
162 	{VIRTCHNL_PROTO_HDR_AH, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_AH_SPI),
163 		BIT_ULL(ICE_FLOW_FIELD_IDX_AH_SPI)},
164 	{VIRTCHNL_PROTO_HDR_PFCP, FIELD_SELECTOR(VIRTCHNL_PROTO_HDR_PFCP_SEID),
165 		BIT_ULL(ICE_FLOW_FIELD_IDX_PFCP_SEID)},
166 };
167 
168 /**
169  * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
170  * @pf: pointer to the PF structure
171  * @v_opcode: operation code
172  * @v_retval: return value
173  * @msg: pointer to the msg buffer
174  * @msglen: msg length
175  */
176 static void
177 ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
178 		    enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
179 {
180 	struct ice_hw *hw = &pf->hw;
181 	struct ice_vf *vf;
182 	unsigned int bkt;
183 
184 	mutex_lock(&pf->vfs.table_lock);
185 	ice_for_each_vf(pf, bkt, vf) {
186 		/* Not all vfs are enabled so skip the ones that are not */
187 		if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
188 		    !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
189 			continue;
190 
191 		/* Ignore return value on purpose - a given VF may fail, but
192 		 * we need to keep going and send to all of them
193 		 */
194 		ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
195 				      msglen, NULL);
196 	}
197 	mutex_unlock(&pf->vfs.table_lock);
198 }
199 
200 /**
201  * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
202  * @vf: pointer to the VF structure
203  * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
204  * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
205  * @link_up: whether or not to set the link up/down
206  */
207 static void
208 ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
209 		 int ice_link_speed, bool link_up)
210 {
211 	if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
212 		pfe->event_data.link_event_adv.link_status = link_up;
213 		/* Speed in Mbps */
214 		pfe->event_data.link_event_adv.link_speed =
215 			ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
216 	} else {
217 		pfe->event_data.link_event.link_status = link_up;
218 		/* Legacy method for virtchnl link speeds */
219 		pfe->event_data.link_event.link_speed =
220 			(enum virtchnl_link_speed)
221 			ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
222 	}
223 }
224 
225 /**
226  * ice_vc_notify_vf_link_state - Inform a VF of link status
227  * @vf: pointer to the VF structure
228  *
229  * send a link status message to a single VF
230  */
231 void ice_vc_notify_vf_link_state(struct ice_vf *vf)
232 {
233 	struct virtchnl_pf_event pfe = { 0 };
234 	struct ice_hw *hw = &vf->pf->hw;
235 
236 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
237 	pfe.severity = PF_EVENT_SEVERITY_INFO;
238 
239 	if (ice_is_vf_link_up(vf))
240 		ice_set_pfe_link(vf, &pfe,
241 				 hw->port_info->phy.link_info.link_speed, true);
242 	else
243 		ice_set_pfe_link(vf, &pfe, ICE_AQ_LINK_SPEED_UNKNOWN, false);
244 
245 	ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
246 			      VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe,
247 			      sizeof(pfe), NULL);
248 }
249 
250 /**
251  * ice_vc_notify_link_state - Inform all VFs on a PF of link status
252  * @pf: pointer to the PF structure
253  */
254 void ice_vc_notify_link_state(struct ice_pf *pf)
255 {
256 	struct ice_vf *vf;
257 	unsigned int bkt;
258 
259 	mutex_lock(&pf->vfs.table_lock);
260 	ice_for_each_vf(pf, bkt, vf)
261 		ice_vc_notify_vf_link_state(vf);
262 	mutex_unlock(&pf->vfs.table_lock);
263 }
264 
265 /**
266  * ice_vc_notify_reset - Send pending reset message to all VFs
267  * @pf: pointer to the PF structure
268  *
269  * indicate a pending reset to all VFs on a given PF
270  */
271 void ice_vc_notify_reset(struct ice_pf *pf)
272 {
273 	struct virtchnl_pf_event pfe;
274 
275 	if (!ice_has_vfs(pf))
276 		return;
277 
278 	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
279 	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
280 	ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, VIRTCHNL_STATUS_SUCCESS,
281 			    (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
282 }
283 
284 /**
285  * ice_vc_send_msg_to_vf - Send message to VF
286  * @vf: pointer to the VF info
287  * @v_opcode: virtual channel opcode
288  * @v_retval: virtual channel return value
289  * @msg: pointer to the msg buffer
290  * @msglen: msg length
291  *
292  * send msg to VF
293  */
294 int
295 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
296 		      enum virtchnl_status_code v_retval, u8 *msg, u16 msglen)
297 {
298 	struct device *dev;
299 	struct ice_pf *pf;
300 	int aq_ret;
301 
302 	pf = vf->pf;
303 	dev = ice_pf_to_dev(pf);
304 
305 	aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
306 				       msg, msglen, NULL);
307 	if (aq_ret && pf->hw.mailboxq.sq_last_status != ICE_AQ_RC_ENOSYS) {
308 		dev_info(dev, "Unable to send the message to VF %d ret %d aq_err %s\n",
309 			 vf->vf_id, aq_ret,
310 			 ice_aq_str(pf->hw.mailboxq.sq_last_status));
311 		return -EIO;
312 	}
313 
314 	return 0;
315 }
316 
317 /**
318  * ice_vc_get_ver_msg
319  * @vf: pointer to the VF info
320  * @msg: pointer to the msg buffer
321  *
322  * called from the VF to request the API version used by the PF
323  */
324 static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
325 {
326 	struct virtchnl_version_info info = {
327 		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
328 	};
329 
330 	vf->vf_ver = *(struct virtchnl_version_info *)msg;
331 	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
332 	if (VF_IS_V10(&vf->vf_ver))
333 		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
334 
335 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
336 				     VIRTCHNL_STATUS_SUCCESS, (u8 *)&info,
337 				     sizeof(struct virtchnl_version_info));
338 }
339 
340 /**
341  * ice_vc_get_max_frame_size - get max frame size allowed for VF
342  * @vf: VF used to determine max frame size
343  *
344  * Max frame size is determined based on the current port's max frame size and
345  * whether a port VLAN is configured on this VF. The VF is not aware whether
346  * it's in a port VLAN so the PF needs to account for this in max frame size
347  * checks and sending the max frame size to the VF.
348  */
349 static u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
350 {
351 	struct ice_port_info *pi = ice_vf_get_port_info(vf);
352 	u16 max_frame_size;
353 
354 	max_frame_size = pi->phy.link_info.max_frame_size;
355 
356 	if (ice_vf_is_port_vlan_ena(vf))
357 		max_frame_size -= VLAN_HLEN;
358 
359 	return max_frame_size;
360 }
361 
362 /**
363  * ice_vc_get_vlan_caps
364  * @hw: pointer to the hw
365  * @vf: pointer to the VF info
366  * @vsi: pointer to the VSI
367  * @driver_caps: current driver caps
368  *
369  * Return 0 if there is no VLAN caps supported, or VLAN caps value
370  */
371 static u32
372 ice_vc_get_vlan_caps(struct ice_hw *hw, struct ice_vf *vf, struct ice_vsi *vsi,
373 		     u32 driver_caps)
374 {
375 	if (ice_is_eswitch_mode_switchdev(vf->pf))
376 		/* In switchdev setting VLAN from VF isn't supported */
377 		return 0;
378 
379 	if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
380 		/* VLAN offloads based on current device configuration */
381 		return VIRTCHNL_VF_OFFLOAD_VLAN_V2;
382 	} else if (driver_caps & VIRTCHNL_VF_OFFLOAD_VLAN) {
383 		/* allow VF to negotiate VIRTCHNL_VF_OFFLOAD explicitly for
384 		 * these two conditions, which amounts to guest VLAN filtering
385 		 * and offloads being based on the inner VLAN or the
386 		 * inner/single VLAN respectively and don't allow VF to
387 		 * negotiate VIRTCHNL_VF_OFFLOAD in any other cases
388 		 */
389 		if (ice_is_dvm_ena(hw) && ice_vf_is_port_vlan_ena(vf)) {
390 			return VIRTCHNL_VF_OFFLOAD_VLAN;
391 		} else if (!ice_is_dvm_ena(hw) &&
392 			   !ice_vf_is_port_vlan_ena(vf)) {
393 			/* configure backward compatible support for VFs that
394 			 * only support VIRTCHNL_VF_OFFLOAD_VLAN, the PF is
395 			 * configured in SVM, and no port VLAN is configured
396 			 */
397 			ice_vf_vsi_cfg_svm_legacy_vlan_mode(vsi);
398 			return VIRTCHNL_VF_OFFLOAD_VLAN;
399 		} else if (ice_is_dvm_ena(hw)) {
400 			/* configure software offloaded VLAN support when DVM
401 			 * is enabled, but no port VLAN is enabled
402 			 */
403 			ice_vf_vsi_cfg_dvm_legacy_vlan_mode(vsi);
404 		}
405 	}
406 
407 	return 0;
408 }
409 
410 /**
411  * ice_vc_get_vf_res_msg
412  * @vf: pointer to the VF info
413  * @msg: pointer to the msg buffer
414  *
415  * called from the VF to request its resources
416  */
417 static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
418 {
419 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
420 	struct virtchnl_vf_resource *vfres = NULL;
421 	struct ice_hw *hw = &vf->pf->hw;
422 	struct ice_vsi *vsi;
423 	int len = 0;
424 	int ret;
425 
426 	if (ice_check_vf_init(vf)) {
427 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
428 		goto err;
429 	}
430 
431 	len = virtchnl_struct_size(vfres, vsi_res, 0);
432 
433 	vfres = kzalloc(len, GFP_KERNEL);
434 	if (!vfres) {
435 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
436 		len = 0;
437 		goto err;
438 	}
439 	if (VF_IS_V11(&vf->vf_ver))
440 		vf->driver_caps = *(u32 *)msg;
441 	else
442 		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
443 				  VIRTCHNL_VF_OFFLOAD_VLAN;
444 
445 	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
446 	vsi = ice_get_vf_vsi(vf);
447 	if (!vsi) {
448 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
449 		goto err;
450 	}
451 
452 	vfres->vf_cap_flags |= ice_vc_get_vlan_caps(hw, vf, vsi,
453 						    vf->driver_caps);
454 
455 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF)
456 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
457 
458 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)
459 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC;
460 
461 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF)
462 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_FDIR_PF;
463 
464 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_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, u8 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, ice_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 static 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 static 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 static int
1570 ice_vf_vsi_dis_single_txq(struct ice_vf *vf, struct ice_vsi *vsi, u16 q_id)
1571 {
1572 	struct ice_txq_meta txq_meta = { 0 };
1573 	struct ice_tx_ring *ring;
1574 	int err;
1575 
1576 	if (!test_bit(q_id, vf->txq_ena))
1577 		dev_dbg(ice_pf_to_dev(vsi->back), "Queue %u on VSI %u is not enabled, but stopping it anyway\n",
1578 			q_id, vsi->vsi_num);
1579 
1580 	ring = vsi->tx_rings[q_id];
1581 	if (!ring)
1582 		return -EINVAL;
1583 
1584 	ice_fill_txq_meta(vsi, ring, &txq_meta);
1585 
1586 	err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id, ring, &txq_meta);
1587 	if (err) {
1588 		dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
1589 			q_id, vsi->vsi_num);
1590 		return err;
1591 	}
1592 
1593 	/* Clear enabled queues flag */
1594 	clear_bit(q_id, vf->txq_ena);
1595 
1596 	return 0;
1597 }
1598 
1599 /**
1600  * ice_vc_dis_qs_msg
1601  * @vf: pointer to the VF info
1602  * @msg: pointer to the msg buffer
1603  *
1604  * called from the VF to disable all or specific queue(s)
1605  */
1606 static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
1607 {
1608 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1609 	struct virtchnl_queue_select *vqs =
1610 	    (struct virtchnl_queue_select *)msg;
1611 	struct ice_vsi *vsi;
1612 	unsigned long q_map;
1613 	u16 vf_q_id;
1614 
1615 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
1616 	    !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
1617 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1618 		goto error_param;
1619 	}
1620 
1621 	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1622 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1623 		goto error_param;
1624 	}
1625 
1626 	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
1627 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1628 		goto error_param;
1629 	}
1630 
1631 	vsi = ice_get_vf_vsi(vf);
1632 	if (!vsi) {
1633 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1634 		goto error_param;
1635 	}
1636 
1637 	if (vqs->tx_queues) {
1638 		q_map = vqs->tx_queues;
1639 
1640 		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1641 			if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1642 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1643 				goto error_param;
1644 			}
1645 
1646 			if (ice_vf_vsi_dis_single_txq(vf, vsi, vf_q_id)) {
1647 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1648 				goto error_param;
1649 			}
1650 		}
1651 	}
1652 
1653 	q_map = vqs->rx_queues;
1654 	/* speed up Rx queue disable by batching them if possible */
1655 	if (q_map &&
1656 	    bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
1657 		if (ice_vsi_stop_all_rx_rings(vsi)) {
1658 			dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
1659 				vsi->vsi_num);
1660 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1661 			goto error_param;
1662 		}
1663 
1664 		bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
1665 	} else if (q_map) {
1666 		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
1667 			if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
1668 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1669 				goto error_param;
1670 			}
1671 
1672 			/* Skip queue if not enabled */
1673 			if (!test_bit(vf_q_id, vf->rxq_ena))
1674 				continue;
1675 
1676 			if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
1677 						     true)) {
1678 				dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
1679 					vf_q_id, vsi->vsi_num);
1680 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1681 				goto error_param;
1682 			}
1683 
1684 			/* Clear enabled queues flag */
1685 			clear_bit(vf_q_id, vf->rxq_ena);
1686 		}
1687 	}
1688 
1689 	/* Clear enabled queues flag */
1690 	if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
1691 		clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
1692 
1693 error_param:
1694 	/* send the response to the VF */
1695 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
1696 				     NULL, 0);
1697 }
1698 
1699 /**
1700  * ice_cfg_interrupt
1701  * @vf: pointer to the VF info
1702  * @vsi: the VSI being configured
1703  * @map: vector map for mapping vectors to queues
1704  * @q_vector: structure for interrupt vector
1705  * configure the IRQ to queue map
1706  */
1707 static enum virtchnl_status_code
1708 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi,
1709 		  struct virtchnl_vector_map *map,
1710 		  struct ice_q_vector *q_vector)
1711 {
1712 	u16 vsi_q_id, vsi_q_id_idx;
1713 	unsigned long qmap;
1714 
1715 	q_vector->num_ring_rx = 0;
1716 	q_vector->num_ring_tx = 0;
1717 
1718 	qmap = map->rxq_map;
1719 	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1720 		vsi_q_id = vsi_q_id_idx;
1721 
1722 		if (!ice_vc_isvalid_q_id(vsi, vsi_q_id))
1723 			return VIRTCHNL_STATUS_ERR_PARAM;
1724 
1725 		q_vector->num_ring_rx++;
1726 		q_vector->rx.itr_idx = map->rxitr_idx;
1727 		vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
1728 		ice_cfg_rxq_interrupt(vsi, vsi_q_id,
1729 				      q_vector->vf_reg_idx,
1730 				      q_vector->rx.itr_idx);
1731 	}
1732 
1733 	qmap = map->txq_map;
1734 	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
1735 		vsi_q_id = vsi_q_id_idx;
1736 
1737 		if (!ice_vc_isvalid_q_id(vsi, vsi_q_id))
1738 			return VIRTCHNL_STATUS_ERR_PARAM;
1739 
1740 		q_vector->num_ring_tx++;
1741 		q_vector->tx.itr_idx = map->txitr_idx;
1742 		vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
1743 		ice_cfg_txq_interrupt(vsi, vsi_q_id,
1744 				      q_vector->vf_reg_idx,
1745 				      q_vector->tx.itr_idx);
1746 	}
1747 
1748 	return VIRTCHNL_STATUS_SUCCESS;
1749 }
1750 
1751 /**
1752  * ice_vc_cfg_irq_map_msg
1753  * @vf: pointer to the VF info
1754  * @msg: pointer to the msg buffer
1755  *
1756  * called from the VF to configure the IRQ to queue map
1757  */
1758 static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
1759 {
1760 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1761 	u16 num_q_vectors_mapped, vsi_id, vector_id;
1762 	struct virtchnl_irq_map_info *irqmap_info;
1763 	struct virtchnl_vector_map *map;
1764 	struct ice_vsi *vsi;
1765 	int i;
1766 
1767 	irqmap_info = (struct virtchnl_irq_map_info *)msg;
1768 	num_q_vectors_mapped = irqmap_info->num_vectors;
1769 
1770 	/* Check to make sure number of VF vectors mapped is not greater than
1771 	 * number of VF vectors originally allocated, and check that
1772 	 * there is actually at least a single VF queue vector mapped
1773 	 */
1774 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1775 	    vf->num_msix < num_q_vectors_mapped ||
1776 	    !num_q_vectors_mapped) {
1777 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1778 		goto error_param;
1779 	}
1780 
1781 	vsi = ice_get_vf_vsi(vf);
1782 	if (!vsi) {
1783 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1784 		goto error_param;
1785 	}
1786 
1787 	for (i = 0; i < num_q_vectors_mapped; i++) {
1788 		struct ice_q_vector *q_vector;
1789 
1790 		map = &irqmap_info->vecmap[i];
1791 
1792 		vector_id = map->vector_id;
1793 		vsi_id = map->vsi_id;
1794 		/* vector_id is always 0-based for each VF, and can never be
1795 		 * larger than or equal to the max allowed interrupts per VF
1796 		 */
1797 		if (!(vector_id < vf->num_msix) ||
1798 		    !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
1799 		    (!vector_id && (map->rxq_map || map->txq_map))) {
1800 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1801 			goto error_param;
1802 		}
1803 
1804 		/* No need to map VF miscellaneous or rogue vector */
1805 		if (!vector_id)
1806 			continue;
1807 
1808 		/* Subtract non queue vector from vector_id passed by VF
1809 		 * to get actual number of VSI queue vector array index
1810 		 */
1811 		q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
1812 		if (!q_vector) {
1813 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1814 			goto error_param;
1815 		}
1816 
1817 		/* lookout for the invalid queue index */
1818 		v_ret = ice_cfg_interrupt(vf, vsi, map, q_vector);
1819 		if (v_ret)
1820 			goto error_param;
1821 	}
1822 
1823 error_param:
1824 	/* send the response to the VF */
1825 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
1826 				     NULL, 0);
1827 }
1828 
1829 /**
1830  * ice_vc_cfg_q_bw - Configure per queue bandwidth
1831  * @vf: pointer to the VF info
1832  * @msg: pointer to the msg buffer which holds the command descriptor
1833  *
1834  * Configure VF queues bandwidth.
1835  *
1836  * Return: 0 on success or negative error value.
1837  */
1838 static int ice_vc_cfg_q_bw(struct ice_vf *vf, u8 *msg)
1839 {
1840 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1841 	struct virtchnl_queues_bw_cfg *qbw =
1842 		(struct virtchnl_queues_bw_cfg *)msg;
1843 	struct ice_vsi *vsi;
1844 	u16 i;
1845 
1846 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
1847 	    !ice_vc_isvalid_vsi_id(vf, qbw->vsi_id)) {
1848 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1849 		goto err;
1850 	}
1851 
1852 	vsi = ice_get_vf_vsi(vf);
1853 	if (!vsi) {
1854 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1855 		goto err;
1856 	}
1857 
1858 	if (qbw->num_queues > ICE_MAX_RSS_QS_PER_VF ||
1859 	    qbw->num_queues > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
1860 		dev_err(ice_pf_to_dev(vf->pf), "VF-%d trying to configure more than allocated number of queues: %d\n",
1861 			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
1862 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1863 		goto err;
1864 	}
1865 
1866 	for (i = 0; i < qbw->num_queues; i++) {
1867 		if (qbw->cfg[i].shaper.peak != 0 && vf->max_tx_rate != 0 &&
1868 		    qbw->cfg[i].shaper.peak > vf->max_tx_rate)
1869 			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",
1870 				 qbw->cfg[i].queue_id, vf->vf_id,
1871 				 vf->max_tx_rate);
1872 		if (qbw->cfg[i].shaper.committed != 0 && vf->min_tx_rate != 0 &&
1873 		    qbw->cfg[i].shaper.committed < vf->min_tx_rate)
1874 			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",
1875 				 qbw->cfg[i].queue_id, vf->vf_id,
1876 				 vf->max_tx_rate);
1877 	}
1878 
1879 	for (i = 0; i < qbw->num_queues; i++) {
1880 		vf->qs_bw[i].queue_id = qbw->cfg[i].queue_id;
1881 		vf->qs_bw[i].peak = qbw->cfg[i].shaper.peak;
1882 		vf->qs_bw[i].committed = qbw->cfg[i].shaper.committed;
1883 		vf->qs_bw[i].tc = qbw->cfg[i].tc;
1884 	}
1885 
1886 	if (ice_vf_cfg_qs_bw(vf, qbw->num_queues))
1887 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1888 
1889 err:
1890 	/* send the response to the VF */
1891 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_QUEUE_BW,
1892 				    v_ret, NULL, 0);
1893 }
1894 
1895 /**
1896  * ice_vc_cfg_q_quanta - Configure per queue quanta
1897  * @vf: pointer to the VF info
1898  * @msg: pointer to the msg buffer which holds the command descriptor
1899  *
1900  * Configure VF queues quanta.
1901  *
1902  * Return: 0 on success or negative error value.
1903  */
1904 static int ice_vc_cfg_q_quanta(struct ice_vf *vf, u8 *msg)
1905 {
1906 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
1907 	u16 quanta_prof_id, quanta_size, start_qid, end_qid, i;
1908 	struct virtchnl_quanta_cfg *qquanta =
1909 		(struct virtchnl_quanta_cfg *)msg;
1910 	struct ice_vsi *vsi;
1911 	int ret;
1912 
1913 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
1914 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1915 		goto err;
1916 	}
1917 
1918 	vsi = ice_get_vf_vsi(vf);
1919 	if (!vsi) {
1920 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1921 		goto err;
1922 	}
1923 
1924 	end_qid = qquanta->queue_select.start_queue_id +
1925 		  qquanta->queue_select.num_queues;
1926 	if (end_qid > ICE_MAX_RSS_QS_PER_VF ||
1927 	    end_qid > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
1928 		dev_err(ice_pf_to_dev(vf->pf), "VF-%d trying to configure more than allocated number of queues: %d\n",
1929 			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
1930 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1931 		goto err;
1932 	}
1933 
1934 	quanta_size = qquanta->quanta_size;
1935 	if (quanta_size > ICE_MAX_QUANTA_SIZE ||
1936 	    quanta_size < ICE_MIN_QUANTA_SIZE) {
1937 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1938 		goto err;
1939 	}
1940 
1941 	if (quanta_size % 64) {
1942 		dev_err(ice_pf_to_dev(vf->pf), "quanta size should be the product of 64\n");
1943 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1944 		goto err;
1945 	}
1946 
1947 	ret = ice_vf_cfg_q_quanta_profile(vf, quanta_size,
1948 					  &quanta_prof_id);
1949 	if (ret) {
1950 		v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
1951 		goto err;
1952 	}
1953 
1954 	start_qid = qquanta->queue_select.start_queue_id;
1955 	for (i = start_qid; i < end_qid; i++)
1956 		vsi->tx_rings[i]->quanta_prof_id = quanta_prof_id;
1957 
1958 err:
1959 	/* send the response to the VF */
1960 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_QUANTA,
1961 				     v_ret, NULL, 0);
1962 }
1963 
1964 /**
1965  * ice_vc_cfg_qs_msg
1966  * @vf: pointer to the VF info
1967  * @msg: pointer to the msg buffer
1968  *
1969  * called from the VF to configure the Rx/Tx queues
1970  */
1971 static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
1972 {
1973 	struct virtchnl_vsi_queue_config_info *qci =
1974 	    (struct virtchnl_vsi_queue_config_info *)msg;
1975 	struct virtchnl_queue_pair_info *qpi;
1976 	struct ice_pf *pf = vf->pf;
1977 	struct ice_lag *lag;
1978 	struct ice_vsi *vsi;
1979 	u8 act_prt, pri_prt;
1980 	int i = -1, q_idx;
1981 	bool ena_ts;
1982 
1983 	lag = pf->lag;
1984 	mutex_lock(&pf->lag_mutex);
1985 	act_prt = ICE_LAG_INVALID_PORT;
1986 	pri_prt = pf->hw.port_info->lport;
1987 	if (lag && lag->bonded && lag->primary) {
1988 		act_prt = lag->active_port;
1989 		if (act_prt != pri_prt && act_prt != ICE_LAG_INVALID_PORT &&
1990 		    lag->upper_netdev)
1991 			ice_lag_move_vf_nodes_cfg(lag, act_prt, pri_prt);
1992 		else
1993 			act_prt = ICE_LAG_INVALID_PORT;
1994 	}
1995 
1996 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1997 		goto error_param;
1998 
1999 	if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id))
2000 		goto error_param;
2001 
2002 	vsi = ice_get_vf_vsi(vf);
2003 	if (!vsi)
2004 		goto error_param;
2005 
2006 	if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
2007 	    qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
2008 		dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
2009 			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
2010 		goto error_param;
2011 	}
2012 
2013 	for (i = 0; i < qci->num_queue_pairs; i++) {
2014 		if (!qci->qpair[i].rxq.crc_disable)
2015 			continue;
2016 
2017 		if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_CRC) ||
2018 		    vf->vlan_strip_ena)
2019 			goto error_param;
2020 	}
2021 
2022 	for (i = 0; i < qci->num_queue_pairs; i++) {
2023 		qpi = &qci->qpair[i];
2024 		if (qpi->txq.vsi_id != qci->vsi_id ||
2025 		    qpi->rxq.vsi_id != qci->vsi_id ||
2026 		    qpi->rxq.queue_id != qpi->txq.queue_id ||
2027 		    qpi->txq.headwb_enabled ||
2028 		    !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
2029 		    !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
2030 		    !ice_vc_isvalid_q_id(vsi, qpi->txq.queue_id)) {
2031 			goto error_param;
2032 		}
2033 
2034 		q_idx = qpi->rxq.queue_id;
2035 
2036 		/* make sure selected "q_idx" is in valid range of queues
2037 		 * for selected "vsi"
2038 		 */
2039 		if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
2040 			goto error_param;
2041 		}
2042 
2043 		/* copy Tx queue info from VF into VSI */
2044 		if (qpi->txq.ring_len > 0) {
2045 			vsi->tx_rings[q_idx]->dma = qpi->txq.dma_ring_addr;
2046 			vsi->tx_rings[q_idx]->count = qpi->txq.ring_len;
2047 
2048 			/* Disable any existing queue first */
2049 			if (ice_vf_vsi_dis_single_txq(vf, vsi, q_idx))
2050 				goto error_param;
2051 
2052 			/* Configure a queue with the requested settings */
2053 			if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
2054 				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure TX queue %d\n",
2055 					 vf->vf_id, q_idx);
2056 				goto error_param;
2057 			}
2058 		}
2059 
2060 		/* copy Rx queue info from VF into VSI */
2061 		if (qpi->rxq.ring_len > 0) {
2062 			u16 max_frame_size = ice_vc_get_max_frame_size(vf);
2063 			struct ice_rx_ring *ring = vsi->rx_rings[q_idx];
2064 			u32 rxdid;
2065 
2066 			ring->dma = qpi->rxq.dma_ring_addr;
2067 			ring->count = qpi->rxq.ring_len;
2068 
2069 			if (qpi->rxq.crc_disable)
2070 				ring->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS;
2071 			else
2072 				ring->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS;
2073 
2074 			if (qpi->rxq.databuffer_size != 0 &&
2075 			    (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
2076 			     qpi->rxq.databuffer_size < 1024))
2077 				goto error_param;
2078 			ring->rx_buf_len = qpi->rxq.databuffer_size;
2079 			if (qpi->rxq.max_pkt_size > max_frame_size ||
2080 			    qpi->rxq.max_pkt_size < 64)
2081 				goto error_param;
2082 
2083 			ring->max_frame = qpi->rxq.max_pkt_size;
2084 			/* add space for the port VLAN since the VF driver is
2085 			 * not expected to account for it in the MTU
2086 			 * calculation
2087 			 */
2088 			if (ice_vf_is_port_vlan_ena(vf))
2089 				ring->max_frame += VLAN_HLEN;
2090 
2091 			if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
2092 				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure RX queue %d\n",
2093 					 vf->vf_id, q_idx);
2094 				goto error_param;
2095 			}
2096 
2097 			/* If Rx flex desc is supported, select RXDID for Rx
2098 			 * queues. Otherwise, use legacy 32byte descriptor
2099 			 * format. Legacy 16byte descriptor is not supported.
2100 			 * If this RXDID is selected, return error.
2101 			 */
2102 			if (vf->driver_caps &
2103 			    VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
2104 				rxdid = qpi->rxq.rxdid;
2105 				if (!(BIT(rxdid) & pf->supported_rxdids))
2106 					goto error_param;
2107 			} else {
2108 				rxdid = ICE_RXDID_LEGACY_1;
2109 			}
2110 
2111 			ena_ts = ((vf->driver_caps &
2112 				  VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) &&
2113 				  (vf->driver_caps & VIRTCHNL_VF_CAP_PTP) &&
2114 				  (qpi->rxq.flags & VIRTCHNL_PTP_RX_TSTAMP));
2115 
2116 			ice_write_qrxflxp_cntxt(&vsi->back->hw,
2117 						vsi->rxq_map[q_idx], rxdid,
2118 						ICE_RXDID_PRIO, ena_ts);
2119 		}
2120 	}
2121 
2122 	if (lag && lag->bonded && lag->primary &&
2123 	    act_prt != ICE_LAG_INVALID_PORT)
2124 		ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
2125 	mutex_unlock(&pf->lag_mutex);
2126 
2127 	/* send the response to the VF */
2128 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2129 				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
2130 error_param:
2131 	/* disable whatever we can */
2132 	for (; i >= 0; i--) {
2133 		if (ice_vsi_ctrl_one_rx_ring(vsi, false, i, true))
2134 			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable RX queue %d\n",
2135 				vf->vf_id, i);
2136 		if (ice_vf_vsi_dis_single_txq(vf, vsi, i))
2137 			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable TX queue %d\n",
2138 				vf->vf_id, i);
2139 	}
2140 
2141 	if (lag && lag->bonded && lag->primary &&
2142 	    act_prt != ICE_LAG_INVALID_PORT)
2143 		ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
2144 	mutex_unlock(&pf->lag_mutex);
2145 
2146 	ice_lag_move_new_vf_nodes(vf);
2147 
2148 	/* send the response to the VF */
2149 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2150 				     VIRTCHNL_STATUS_ERR_PARAM, NULL, 0);
2151 }
2152 
2153 /**
2154  * ice_can_vf_change_mac
2155  * @vf: pointer to the VF info
2156  *
2157  * Return true if the VF is allowed to change its MAC filters, false otherwise
2158  */
2159 static bool ice_can_vf_change_mac(struct ice_vf *vf)
2160 {
2161 	/* If the VF MAC address has been set administratively (via the
2162 	 * ndo_set_vf_mac command), then deny permission to the VF to
2163 	 * add/delete unicast MAC addresses, unless the VF is trusted
2164 	 */
2165 	if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
2166 		return false;
2167 
2168 	return true;
2169 }
2170 
2171 /**
2172  * ice_vc_ether_addr_type - get type of virtchnl_ether_addr
2173  * @vc_ether_addr: used to extract the type
2174  */
2175 static u8
2176 ice_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
2177 {
2178 	return (vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK);
2179 }
2180 
2181 /**
2182  * ice_is_vc_addr_legacy - check if the MAC address is from an older VF
2183  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
2184  */
2185 static bool
2186 ice_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
2187 {
2188 	u8 type = ice_vc_ether_addr_type(vc_ether_addr);
2189 
2190 	return (type == VIRTCHNL_ETHER_ADDR_LEGACY);
2191 }
2192 
2193 /**
2194  * ice_is_vc_addr_primary - check if the MAC address is the VF's primary MAC
2195  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
2196  *
2197  * This function should only be called when the MAC address in
2198  * virtchnl_ether_addr is a valid unicast MAC
2199  */
2200 static bool
2201 ice_is_vc_addr_primary(struct virtchnl_ether_addr __maybe_unused *vc_ether_addr)
2202 {
2203 	u8 type = ice_vc_ether_addr_type(vc_ether_addr);
2204 
2205 	return (type == VIRTCHNL_ETHER_ADDR_PRIMARY);
2206 }
2207 
2208 /**
2209  * ice_vfhw_mac_add - update the VF's cached hardware MAC if allowed
2210  * @vf: VF to update
2211  * @vc_ether_addr: structure from VIRTCHNL with MAC to add
2212  */
2213 static void
2214 ice_vfhw_mac_add(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
2215 {
2216 	u8 *mac_addr = vc_ether_addr->addr;
2217 
2218 	if (!is_valid_ether_addr(mac_addr))
2219 		return;
2220 
2221 	/* only allow legacy VF drivers to set the device and hardware MAC if it
2222 	 * is zero and allow new VF drivers to set the hardware MAC if the type
2223 	 * was correctly specified over VIRTCHNL
2224 	 */
2225 	if ((ice_is_vc_addr_legacy(vc_ether_addr) &&
2226 	     is_zero_ether_addr(vf->hw_lan_addr)) ||
2227 	    ice_is_vc_addr_primary(vc_ether_addr)) {
2228 		ether_addr_copy(vf->dev_lan_addr, mac_addr);
2229 		ether_addr_copy(vf->hw_lan_addr, mac_addr);
2230 	}
2231 
2232 	/* hardware and device MACs are already set, but its possible that the
2233 	 * VF driver sent the VIRTCHNL_OP_ADD_ETH_ADDR message before the
2234 	 * VIRTCHNL_OP_DEL_ETH_ADDR when trying to update its MAC, so save it
2235 	 * away for the legacy VF driver case as it will be updated in the
2236 	 * delete flow for this case
2237 	 */
2238 	if (ice_is_vc_addr_legacy(vc_ether_addr)) {
2239 		ether_addr_copy(vf->legacy_last_added_umac.addr,
2240 				mac_addr);
2241 		vf->legacy_last_added_umac.time_modified = jiffies;
2242 	}
2243 }
2244 
2245 /**
2246  * ice_vc_add_mac_addr - attempt to add the MAC address passed in
2247  * @vf: pointer to the VF info
2248  * @vsi: pointer to the VF's VSI
2249  * @vc_ether_addr: VIRTCHNL MAC address structure used to add MAC
2250  */
2251 static int
2252 ice_vc_add_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
2253 		    struct virtchnl_ether_addr *vc_ether_addr)
2254 {
2255 	struct device *dev = ice_pf_to_dev(vf->pf);
2256 	u8 *mac_addr = vc_ether_addr->addr;
2257 	int ret;
2258 
2259 	/* device MAC already added */
2260 	if (ether_addr_equal(mac_addr, vf->dev_lan_addr))
2261 		return 0;
2262 
2263 	if (is_unicast_ether_addr(mac_addr) && !ice_can_vf_change_mac(vf)) {
2264 		dev_err(dev, "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
2265 		return -EPERM;
2266 	}
2267 
2268 	ret = ice_fltr_add_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
2269 	if (ret == -EEXIST) {
2270 		dev_dbg(dev, "MAC %pM already exists for VF %d\n", mac_addr,
2271 			vf->vf_id);
2272 		/* don't return since we might need to update
2273 		 * the primary MAC in ice_vfhw_mac_add() below
2274 		 */
2275 	} else if (ret) {
2276 		dev_err(dev, "Failed to add MAC %pM for VF %d\n, error %d\n",
2277 			mac_addr, vf->vf_id, ret);
2278 		return ret;
2279 	} else {
2280 		vf->num_mac++;
2281 	}
2282 
2283 	ice_vfhw_mac_add(vf, vc_ether_addr);
2284 
2285 	return ret;
2286 }
2287 
2288 /**
2289  * ice_is_legacy_umac_expired - check if last added legacy unicast MAC expired
2290  * @last_added_umac: structure used to check expiration
2291  */
2292 static bool ice_is_legacy_umac_expired(struct ice_time_mac *last_added_umac)
2293 {
2294 #define ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME	msecs_to_jiffies(3000)
2295 	return time_is_before_jiffies(last_added_umac->time_modified +
2296 				      ICE_LEGACY_VF_MAC_CHANGE_EXPIRE_TIME);
2297 }
2298 
2299 /**
2300  * ice_update_legacy_cached_mac - update cached hardware MAC for legacy VF
2301  * @vf: VF to update
2302  * @vc_ether_addr: structure from VIRTCHNL with MAC to check
2303  *
2304  * only update cached hardware MAC for legacy VF drivers on delete
2305  * because we cannot guarantee order/type of MAC from the VF driver
2306  */
2307 static void
2308 ice_update_legacy_cached_mac(struct ice_vf *vf,
2309 			     struct virtchnl_ether_addr *vc_ether_addr)
2310 {
2311 	if (!ice_is_vc_addr_legacy(vc_ether_addr) ||
2312 	    ice_is_legacy_umac_expired(&vf->legacy_last_added_umac))
2313 		return;
2314 
2315 	ether_addr_copy(vf->dev_lan_addr, vf->legacy_last_added_umac.addr);
2316 	ether_addr_copy(vf->hw_lan_addr, vf->legacy_last_added_umac.addr);
2317 }
2318 
2319 /**
2320  * ice_vfhw_mac_del - update the VF's cached hardware MAC if allowed
2321  * @vf: VF to update
2322  * @vc_ether_addr: structure from VIRTCHNL with MAC to delete
2323  */
2324 static void
2325 ice_vfhw_mac_del(struct ice_vf *vf, struct virtchnl_ether_addr *vc_ether_addr)
2326 {
2327 	u8 *mac_addr = vc_ether_addr->addr;
2328 
2329 	if (!is_valid_ether_addr(mac_addr) ||
2330 	    !ether_addr_equal(vf->dev_lan_addr, mac_addr))
2331 		return;
2332 
2333 	/* allow the device MAC to be repopulated in the add flow and don't
2334 	 * clear the hardware MAC (i.e. hw_lan_addr) here as that is meant
2335 	 * to be persistent on VM reboot and across driver unload/load, which
2336 	 * won't work if we clear the hardware MAC here
2337 	 */
2338 	eth_zero_addr(vf->dev_lan_addr);
2339 
2340 	ice_update_legacy_cached_mac(vf, vc_ether_addr);
2341 }
2342 
2343 /**
2344  * ice_vc_del_mac_addr - attempt to delete the MAC address passed in
2345  * @vf: pointer to the VF info
2346  * @vsi: pointer to the VF's VSI
2347  * @vc_ether_addr: VIRTCHNL MAC address structure used to delete MAC
2348  */
2349 static int
2350 ice_vc_del_mac_addr(struct ice_vf *vf, struct ice_vsi *vsi,
2351 		    struct virtchnl_ether_addr *vc_ether_addr)
2352 {
2353 	struct device *dev = ice_pf_to_dev(vf->pf);
2354 	u8 *mac_addr = vc_ether_addr->addr;
2355 	int status;
2356 
2357 	if (!ice_can_vf_change_mac(vf) &&
2358 	    ether_addr_equal(vf->dev_lan_addr, mac_addr))
2359 		return 0;
2360 
2361 	status = ice_fltr_remove_mac(vsi, mac_addr, ICE_FWD_TO_VSI);
2362 	if (status == -ENOENT) {
2363 		dev_err(dev, "MAC %pM does not exist for VF %d\n", mac_addr,
2364 			vf->vf_id);
2365 		return -ENOENT;
2366 	} else if (status) {
2367 		dev_err(dev, "Failed to delete MAC %pM for VF %d, error %d\n",
2368 			mac_addr, vf->vf_id, status);
2369 		return -EIO;
2370 	}
2371 
2372 	ice_vfhw_mac_del(vf, vc_ether_addr);
2373 
2374 	vf->num_mac--;
2375 
2376 	return 0;
2377 }
2378 
2379 /**
2380  * ice_vc_handle_mac_addr_msg
2381  * @vf: pointer to the VF info
2382  * @msg: pointer to the msg buffer
2383  * @set: true if MAC filters are being set, false otherwise
2384  *
2385  * add guest MAC address filter
2386  */
2387 static int
2388 ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
2389 {
2390 	int (*ice_vc_cfg_mac)
2391 		(struct ice_vf *vf, struct ice_vsi *vsi,
2392 		 struct virtchnl_ether_addr *virtchnl_ether_addr);
2393 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2394 	struct virtchnl_ether_addr_list *al =
2395 	    (struct virtchnl_ether_addr_list *)msg;
2396 	struct ice_pf *pf = vf->pf;
2397 	enum virtchnl_ops vc_op;
2398 	struct ice_vsi *vsi;
2399 	int i;
2400 
2401 	if (set) {
2402 		vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
2403 		ice_vc_cfg_mac = ice_vc_add_mac_addr;
2404 	} else {
2405 		vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
2406 		ice_vc_cfg_mac = ice_vc_del_mac_addr;
2407 	}
2408 
2409 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
2410 	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2411 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2412 		goto handle_mac_exit;
2413 	}
2414 
2415 	/* If this VF is not privileged, then we can't add more than a
2416 	 * limited number of addresses. Check to make sure that the
2417 	 * additions do not push us over the limit.
2418 	 */
2419 	if (set && !ice_is_vf_trusted(vf) &&
2420 	    (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
2421 		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",
2422 			vf->vf_id);
2423 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2424 		goto handle_mac_exit;
2425 	}
2426 
2427 	vsi = ice_get_vf_vsi(vf);
2428 	if (!vsi) {
2429 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2430 		goto handle_mac_exit;
2431 	}
2432 
2433 	for (i = 0; i < al->num_elements; i++) {
2434 		u8 *mac_addr = al->list[i].addr;
2435 		int result;
2436 
2437 		if (is_broadcast_ether_addr(mac_addr) ||
2438 		    is_zero_ether_addr(mac_addr))
2439 			continue;
2440 
2441 		result = ice_vc_cfg_mac(vf, vsi, &al->list[i]);
2442 		if (result == -EEXIST || result == -ENOENT) {
2443 			continue;
2444 		} else if (result) {
2445 			v_ret = VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
2446 			goto handle_mac_exit;
2447 		}
2448 	}
2449 
2450 handle_mac_exit:
2451 	/* send the response to the VF */
2452 	return ice_vc_send_msg_to_vf(vf, vc_op, v_ret, NULL, 0);
2453 }
2454 
2455 /**
2456  * ice_vc_add_mac_addr_msg
2457  * @vf: pointer to the VF info
2458  * @msg: pointer to the msg buffer
2459  *
2460  * add guest MAC address filter
2461  */
2462 static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2463 {
2464 	return ice_vc_handle_mac_addr_msg(vf, msg, true);
2465 }
2466 
2467 /**
2468  * ice_vc_del_mac_addr_msg
2469  * @vf: pointer to the VF info
2470  * @msg: pointer to the msg buffer
2471  *
2472  * remove guest MAC address filter
2473  */
2474 static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
2475 {
2476 	return ice_vc_handle_mac_addr_msg(vf, msg, false);
2477 }
2478 
2479 /**
2480  * ice_vc_request_qs_msg
2481  * @vf: pointer to the VF info
2482  * @msg: pointer to the msg buffer
2483  *
2484  * VFs get a default number of queues but can use this message to request a
2485  * different number. If the request is successful, PF will reset the VF and
2486  * return 0. If unsuccessful, PF will send message informing VF of number of
2487  * available queue pairs via virtchnl message response to VF.
2488  */
2489 static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
2490 {
2491 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2492 	struct virtchnl_vf_res_request *vfres =
2493 		(struct virtchnl_vf_res_request *)msg;
2494 	u16 req_queues = vfres->num_queue_pairs;
2495 	struct ice_pf *pf = vf->pf;
2496 	u16 max_allowed_vf_queues;
2497 	u16 tx_rx_queue_left;
2498 	struct device *dev;
2499 	u16 cur_queues;
2500 
2501 	dev = ice_pf_to_dev(pf);
2502 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2503 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2504 		goto error_param;
2505 	}
2506 
2507 	cur_queues = vf->num_vf_qs;
2508 	tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
2509 				 ice_get_avail_rxq_count(pf));
2510 	max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
2511 	if (!req_queues) {
2512 		dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
2513 			vf->vf_id);
2514 	} else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
2515 		dev_err(dev, "VF %d tried to request more than %d queues.\n",
2516 			vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
2517 		vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
2518 	} else if (req_queues > cur_queues &&
2519 		   req_queues - cur_queues > tx_rx_queue_left) {
2520 		dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
2521 			 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
2522 		vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
2523 					       ICE_MAX_RSS_QS_PER_VF);
2524 	} else {
2525 		/* request is successful, then reset VF */
2526 		vf->num_req_qs = req_queues;
2527 		ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
2528 		dev_info(dev, "VF %d granted request of %u queues.\n",
2529 			 vf->vf_id, req_queues);
2530 		return 0;
2531 	}
2532 
2533 error_param:
2534 	/* send the response to the VF */
2535 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
2536 				     v_ret, (u8 *)vfres, sizeof(*vfres));
2537 }
2538 
2539 /**
2540  * ice_vf_vlan_offload_ena - determine if capabilities support VLAN offloads
2541  * @caps: VF driver negotiated capabilities
2542  *
2543  * Return true if VIRTCHNL_VF_OFFLOAD_VLAN capability is set, else return false
2544  */
2545 static bool ice_vf_vlan_offload_ena(u32 caps)
2546 {
2547 	return !!(caps & VIRTCHNL_VF_OFFLOAD_VLAN);
2548 }
2549 
2550 /**
2551  * ice_is_vlan_promisc_allowed - check if VLAN promiscuous config is allowed
2552  * @vf: VF used to determine if VLAN promiscuous config is allowed
2553  */
2554 static bool ice_is_vlan_promisc_allowed(struct ice_vf *vf)
2555 {
2556 	if ((test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states) ||
2557 	     test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) &&
2558 	    test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, vf->pf->flags))
2559 		return true;
2560 
2561 	return false;
2562 }
2563 
2564 /**
2565  * ice_vf_ena_vlan_promisc - Enable Tx/Rx VLAN promiscuous for the VLAN
2566  * @vf: VF to enable VLAN promisc on
2567  * @vsi: VF's VSI used to enable VLAN promiscuous mode
2568  * @vlan: VLAN used to enable VLAN promiscuous
2569  *
2570  * This function should only be called if VLAN promiscuous mode is allowed,
2571  * which can be determined via ice_is_vlan_promisc_allowed().
2572  */
2573 static int ice_vf_ena_vlan_promisc(struct ice_vf *vf, struct ice_vsi *vsi,
2574 				   struct ice_vlan *vlan)
2575 {
2576 	u8 promisc_m = 0;
2577 	int status;
2578 
2579 	if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states))
2580 		promisc_m |= ICE_UCAST_VLAN_PROMISC_BITS;
2581 	if (test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states))
2582 		promisc_m |= ICE_MCAST_VLAN_PROMISC_BITS;
2583 
2584 	if (!promisc_m)
2585 		return 0;
2586 
2587 	status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2588 					  vlan->vid);
2589 	if (status && status != -EEXIST)
2590 		return status;
2591 
2592 	return 0;
2593 }
2594 
2595 /**
2596  * ice_vf_dis_vlan_promisc - Disable Tx/Rx VLAN promiscuous for the VLAN
2597  * @vsi: VF's VSI used to disable VLAN promiscuous mode for
2598  * @vlan: VLAN used to disable VLAN promiscuous
2599  *
2600  * This function should only be called if VLAN promiscuous mode is allowed,
2601  * which can be determined via ice_is_vlan_promisc_allowed().
2602  */
2603 static int ice_vf_dis_vlan_promisc(struct ice_vsi *vsi, struct ice_vlan *vlan)
2604 {
2605 	u8 promisc_m = ICE_UCAST_VLAN_PROMISC_BITS | ICE_MCAST_VLAN_PROMISC_BITS;
2606 	int status;
2607 
2608 	status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx, promisc_m,
2609 					    vlan->vid);
2610 	if (status && status != -ENOENT)
2611 		return status;
2612 
2613 	return 0;
2614 }
2615 
2616 /**
2617  * ice_vf_has_max_vlans - check if VF already has the max allowed VLAN filters
2618  * @vf: VF to check against
2619  * @vsi: VF's VSI
2620  *
2621  * If the VF is trusted then the VF is allowed to add as many VLANs as it
2622  * wants to, so return false.
2623  *
2624  * When the VF is untrusted compare the number of non-zero VLANs + 1 to the max
2625  * allowed VLANs for an untrusted VF. Return the result of this comparison.
2626  */
2627 static bool ice_vf_has_max_vlans(struct ice_vf *vf, struct ice_vsi *vsi)
2628 {
2629 	if (ice_is_vf_trusted(vf))
2630 		return false;
2631 
2632 #define ICE_VF_ADDED_VLAN_ZERO_FLTRS	1
2633 	return ((ice_vsi_num_non_zero_vlans(vsi) +
2634 		ICE_VF_ADDED_VLAN_ZERO_FLTRS) >= ICE_MAX_VLAN_PER_VF);
2635 }
2636 
2637 /**
2638  * ice_vc_process_vlan_msg
2639  * @vf: pointer to the VF info
2640  * @msg: pointer to the msg buffer
2641  * @add_v: Add VLAN if true, otherwise delete VLAN
2642  *
2643  * Process virtchnl op to add or remove programmed guest VLAN ID
2644  */
2645 static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
2646 {
2647 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2648 	struct virtchnl_vlan_filter_list *vfl =
2649 	    (struct virtchnl_vlan_filter_list *)msg;
2650 	struct ice_pf *pf = vf->pf;
2651 	bool vlan_promisc = false;
2652 	struct ice_vsi *vsi;
2653 	struct device *dev;
2654 	int status = 0;
2655 	int i;
2656 
2657 	dev = ice_pf_to_dev(pf);
2658 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2659 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2660 		goto error_param;
2661 	}
2662 
2663 	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2664 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2665 		goto error_param;
2666 	}
2667 
2668 	if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2669 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2670 		goto error_param;
2671 	}
2672 
2673 	for (i = 0; i < vfl->num_elements; i++) {
2674 		if (vfl->vlan_id[i] >= VLAN_N_VID) {
2675 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2676 			dev_err(dev, "invalid VF VLAN id %d\n",
2677 				vfl->vlan_id[i]);
2678 			goto error_param;
2679 		}
2680 	}
2681 
2682 	vsi = ice_get_vf_vsi(vf);
2683 	if (!vsi) {
2684 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2685 		goto error_param;
2686 	}
2687 
2688 	if (add_v && ice_vf_has_max_vlans(vf, vsi)) {
2689 		dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2690 			 vf->vf_id);
2691 		/* There is no need to let VF know about being not trusted,
2692 		 * so we can just return success message here
2693 		 */
2694 		goto error_param;
2695 	}
2696 
2697 	/* in DVM a VF can add/delete inner VLAN filters when
2698 	 * VIRTCHNL_VF_OFFLOAD_VLAN is negotiated, so only reject in SVM
2699 	 */
2700 	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&pf->hw)) {
2701 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2702 		goto error_param;
2703 	}
2704 
2705 	/* in DVM VLAN promiscuous is based on the outer VLAN, which would be
2706 	 * the port VLAN if VIRTCHNL_VF_OFFLOAD_VLAN was negotiated, so only
2707 	 * allow vlan_promisc = true in SVM and if no port VLAN is configured
2708 	 */
2709 	vlan_promisc = ice_is_vlan_promisc_allowed(vf) &&
2710 		!ice_is_dvm_ena(&pf->hw) &&
2711 		!ice_vf_is_port_vlan_ena(vf);
2712 
2713 	if (add_v) {
2714 		for (i = 0; i < vfl->num_elements; i++) {
2715 			u16 vid = vfl->vlan_id[i];
2716 			struct ice_vlan vlan;
2717 
2718 			if (ice_vf_has_max_vlans(vf, vsi)) {
2719 				dev_info(dev, "VF-%d is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n",
2720 					 vf->vf_id);
2721 				/* There is no need to let VF know about being
2722 				 * not trusted, so we can just return success
2723 				 * message here as well.
2724 				 */
2725 				goto error_param;
2726 			}
2727 
2728 			/* we add VLAN 0 by default for each VF so we can enable
2729 			 * Tx VLAN anti-spoof without triggering MDD events so
2730 			 * we don't need to add it again here
2731 			 */
2732 			if (!vid)
2733 				continue;
2734 
2735 			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2736 			status = vsi->inner_vlan_ops.add_vlan(vsi, &vlan);
2737 			if (status) {
2738 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2739 				goto error_param;
2740 			}
2741 
2742 			/* Enable VLAN filtering on first non-zero VLAN */
2743 			if (!vlan_promisc && vid && !ice_is_dvm_ena(&pf->hw)) {
2744 				if (vf->spoofchk) {
2745 					status = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
2746 					if (status) {
2747 						v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2748 						dev_err(dev, "Enable VLAN anti-spoofing on VLAN ID: %d failed error-%d\n",
2749 							vid, status);
2750 						goto error_param;
2751 					}
2752 				}
2753 				if (vsi->inner_vlan_ops.ena_rx_filtering(vsi)) {
2754 					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2755 					dev_err(dev, "Enable VLAN pruning on VLAN ID: %d failed error-%d\n",
2756 						vid, status);
2757 					goto error_param;
2758 				}
2759 			} else if (vlan_promisc) {
2760 				status = ice_vf_ena_vlan_promisc(vf, vsi, &vlan);
2761 				if (status) {
2762 					v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2763 					dev_err(dev, "Enable Unicast/multicast promiscuous mode on VLAN ID:%d failed error-%d\n",
2764 						vid, status);
2765 				}
2766 			}
2767 		}
2768 	} else {
2769 		/* In case of non_trusted VF, number of VLAN elements passed
2770 		 * to PF for removal might be greater than number of VLANs
2771 		 * filter programmed for that VF - So, use actual number of
2772 		 * VLANS added earlier with add VLAN opcode. In order to avoid
2773 		 * removing VLAN that doesn't exist, which result to sending
2774 		 * erroneous failed message back to the VF
2775 		 */
2776 		int num_vf_vlan;
2777 
2778 		num_vf_vlan = vsi->num_vlan;
2779 		for (i = 0; i < vfl->num_elements && i < num_vf_vlan; i++) {
2780 			u16 vid = vfl->vlan_id[i];
2781 			struct ice_vlan vlan;
2782 
2783 			/* we add VLAN 0 by default for each VF so we can enable
2784 			 * Tx VLAN anti-spoof without triggering MDD events so
2785 			 * we don't want a VIRTCHNL request to remove it
2786 			 */
2787 			if (!vid)
2788 				continue;
2789 
2790 			vlan = ICE_VLAN(ETH_P_8021Q, vid, 0);
2791 			status = vsi->inner_vlan_ops.del_vlan(vsi, &vlan);
2792 			if (status) {
2793 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2794 				goto error_param;
2795 			}
2796 
2797 			/* Disable VLAN filtering when only VLAN 0 is left */
2798 			if (!ice_vsi_has_non_zero_vlans(vsi)) {
2799 				vsi->inner_vlan_ops.dis_tx_filtering(vsi);
2800 				vsi->inner_vlan_ops.dis_rx_filtering(vsi);
2801 			}
2802 
2803 			if (vlan_promisc)
2804 				ice_vf_dis_vlan_promisc(vsi, &vlan);
2805 		}
2806 	}
2807 
2808 error_param:
2809 	/* send the response to the VF */
2810 	if (add_v)
2811 		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, v_ret,
2812 					     NULL, 0);
2813 	else
2814 		return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, v_ret,
2815 					     NULL, 0);
2816 }
2817 
2818 /**
2819  * ice_vc_add_vlan_msg
2820  * @vf: pointer to the VF info
2821  * @msg: pointer to the msg buffer
2822  *
2823  * Add and program guest VLAN ID
2824  */
2825 static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
2826 {
2827 	return ice_vc_process_vlan_msg(vf, msg, true);
2828 }
2829 
2830 /**
2831  * ice_vc_remove_vlan_msg
2832  * @vf: pointer to the VF info
2833  * @msg: pointer to the msg buffer
2834  *
2835  * remove programmed guest VLAN ID
2836  */
2837 static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
2838 {
2839 	return ice_vc_process_vlan_msg(vf, msg, false);
2840 }
2841 
2842 /**
2843  * ice_vsi_is_rxq_crc_strip_dis - check if Rx queue CRC strip is disabled or not
2844  * @vsi: pointer to the VF VSI info
2845  */
2846 static bool ice_vsi_is_rxq_crc_strip_dis(struct ice_vsi *vsi)
2847 {
2848 	unsigned int i;
2849 
2850 	ice_for_each_alloc_rxq(vsi, i)
2851 		if (vsi->rx_rings[i]->flags & ICE_RX_FLAGS_CRC_STRIP_DIS)
2852 			return true;
2853 
2854 	return false;
2855 }
2856 
2857 /**
2858  * ice_vc_ena_vlan_stripping
2859  * @vf: pointer to the VF info
2860  *
2861  * Enable VLAN header stripping for a given VF
2862  */
2863 static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
2864 {
2865 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2866 	struct ice_vsi *vsi;
2867 
2868 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2869 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2870 		goto error_param;
2871 	}
2872 
2873 	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2874 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2875 		goto error_param;
2876 	}
2877 
2878 	vsi = ice_get_vf_vsi(vf);
2879 	if (!vsi) {
2880 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2881 		goto error_param;
2882 	}
2883 
2884 	if (vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q))
2885 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2886 	else
2887 		vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
2888 
2889 error_param:
2890 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2891 				     v_ret, NULL, 0);
2892 }
2893 
2894 /**
2895  * ice_vc_dis_vlan_stripping
2896  * @vf: pointer to the VF info
2897  *
2898  * Disable VLAN header stripping for a given VF
2899  */
2900 static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
2901 {
2902 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2903 	struct ice_vsi *vsi;
2904 
2905 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2906 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2907 		goto error_param;
2908 	}
2909 
2910 	if (!ice_vf_vlan_offload_ena(vf->driver_caps)) {
2911 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2912 		goto error_param;
2913 	}
2914 
2915 	vsi = ice_get_vf_vsi(vf);
2916 	if (!vsi) {
2917 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2918 		goto error_param;
2919 	}
2920 
2921 	if (vsi->inner_vlan_ops.dis_stripping(vsi))
2922 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2923 	else
2924 		vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
2925 
2926 error_param:
2927 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2928 				     v_ret, NULL, 0);
2929 }
2930 
2931 /**
2932  * ice_vc_get_rss_hena - return the RSS HENA bits allowed by the hardware
2933  * @vf: pointer to the VF info
2934  */
2935 static int ice_vc_get_rss_hena(struct ice_vf *vf)
2936 {
2937 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2938 	struct virtchnl_rss_hena *vrh = NULL;
2939 	int len = 0, ret;
2940 
2941 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2942 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2943 		goto err;
2944 	}
2945 
2946 	if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
2947 		dev_err(ice_pf_to_dev(vf->pf), "RSS not supported by PF\n");
2948 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2949 		goto err;
2950 	}
2951 
2952 	len = sizeof(struct virtchnl_rss_hena);
2953 	vrh = kzalloc(len, GFP_KERNEL);
2954 	if (!vrh) {
2955 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2956 		len = 0;
2957 		goto err;
2958 	}
2959 
2960 	vrh->hena = ICE_DEFAULT_RSS_HENA;
2961 err:
2962 	/* send the response back to the VF */
2963 	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS, v_ret,
2964 				    (u8 *)vrh, len);
2965 	kfree(vrh);
2966 	return ret;
2967 }
2968 
2969 /**
2970  * ice_vc_set_rss_hena - set RSS HENA bits for the VF
2971  * @vf: pointer to the VF info
2972  * @msg: pointer to the msg buffer
2973  */
2974 static int ice_vc_set_rss_hena(struct ice_vf *vf, u8 *msg)
2975 {
2976 	struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
2977 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
2978 	struct ice_pf *pf = vf->pf;
2979 	struct ice_vsi *vsi;
2980 	struct device *dev;
2981 	int status;
2982 
2983 	dev = ice_pf_to_dev(pf);
2984 
2985 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
2986 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2987 		goto err;
2988 	}
2989 
2990 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2991 		dev_err(dev, "RSS not supported by PF\n");
2992 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2993 		goto err;
2994 	}
2995 
2996 	vsi = ice_get_vf_vsi(vf);
2997 	if (!vsi) {
2998 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2999 		goto err;
3000 	}
3001 
3002 	/* clear all previously programmed RSS configuration to allow VF drivers
3003 	 * the ability to customize the RSS configuration and/or completely
3004 	 * disable RSS
3005 	 */
3006 	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
3007 	if (status && !vrh->hena) {
3008 		/* only report failure to clear the current RSS configuration if
3009 		 * that was clearly the VF's intention (i.e. vrh->hena = 0)
3010 		 */
3011 		v_ret = ice_err_to_virt_err(status);
3012 		goto err;
3013 	} else if (status) {
3014 		/* allow the VF to update the RSS configuration even on failure
3015 		 * to clear the current RSS confguration in an attempt to keep
3016 		 * RSS in a working state
3017 		 */
3018 		dev_warn(dev, "Failed to clear the RSS configuration for VF %u\n",
3019 			 vf->vf_id);
3020 	}
3021 
3022 	if (vrh->hena) {
3023 		status = ice_add_avf_rss_cfg(&pf->hw, vsi, vrh->hena);
3024 		v_ret = ice_err_to_virt_err(status);
3025 	}
3026 
3027 	/* send the response to the VF */
3028 err:
3029 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, v_ret,
3030 				     NULL, 0);
3031 }
3032 
3033 /**
3034  * ice_vc_query_rxdid - query RXDID supported by DDP package
3035  * @vf: pointer to VF info
3036  *
3037  * Called from VF to query a bitmap of supported flexible
3038  * descriptor RXDIDs of a DDP package.
3039  */
3040 static int ice_vc_query_rxdid(struct ice_vf *vf)
3041 {
3042 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3043 	struct ice_pf *pf = vf->pf;
3044 	u64 rxdid;
3045 
3046 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3047 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3048 		goto err;
3049 	}
3050 
3051 	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC)) {
3052 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3053 		goto err;
3054 	}
3055 
3056 	rxdid = pf->supported_rxdids;
3057 
3058 err:
3059 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_SUPPORTED_RXDIDS,
3060 				     v_ret, (u8 *)&rxdid, sizeof(rxdid));
3061 }
3062 
3063 /**
3064  * ice_vf_init_vlan_stripping - enable/disable VLAN stripping on initialization
3065  * @vf: VF to enable/disable VLAN stripping for on initialization
3066  *
3067  * Set the default for VLAN stripping based on whether a port VLAN is configured
3068  * and the current VLAN mode of the device.
3069  */
3070 static int ice_vf_init_vlan_stripping(struct ice_vf *vf)
3071 {
3072 	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
3073 
3074 	vf->vlan_strip_ena = 0;
3075 
3076 	if (!vsi)
3077 		return -EINVAL;
3078 
3079 	/* don't modify stripping if port VLAN is configured in SVM since the
3080 	 * port VLAN is based on the inner/single VLAN in SVM
3081 	 */
3082 	if (ice_vf_is_port_vlan_ena(vf) && !ice_is_dvm_ena(&vsi->back->hw))
3083 		return 0;
3084 
3085 	if (ice_vf_vlan_offload_ena(vf->driver_caps)) {
3086 		int err;
3087 
3088 		err = vsi->inner_vlan_ops.ena_stripping(vsi, ETH_P_8021Q);
3089 		if (!err)
3090 			vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
3091 		return err;
3092 	}
3093 
3094 	return vsi->inner_vlan_ops.dis_stripping(vsi);
3095 }
3096 
3097 static u16 ice_vc_get_max_vlan_fltrs(struct ice_vf *vf)
3098 {
3099 	if (vf->trusted)
3100 		return VLAN_N_VID;
3101 	else
3102 		return ICE_MAX_VLAN_PER_VF;
3103 }
3104 
3105 /**
3106  * ice_vf_outer_vlan_not_allowed - check if outer VLAN can be used
3107  * @vf: VF that being checked for
3108  *
3109  * When the device is in double VLAN mode, check whether or not the outer VLAN
3110  * is allowed.
3111  */
3112 static bool ice_vf_outer_vlan_not_allowed(struct ice_vf *vf)
3113 {
3114 	if (ice_vf_is_port_vlan_ena(vf))
3115 		return true;
3116 
3117 	return false;
3118 }
3119 
3120 /**
3121  * ice_vc_set_dvm_caps - set VLAN capabilities when the device is in DVM
3122  * @vf: VF that capabilities are being set for
3123  * @caps: VLAN capabilities to populate
3124  *
3125  * Determine VLAN capabilities support based on whether a port VLAN is
3126  * configured. If a port VLAN is configured then the VF should use the inner
3127  * filtering/offload capabilities since the port VLAN is using the outer VLAN
3128  * capabilies.
3129  */
3130 static void
3131 ice_vc_set_dvm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
3132 {
3133 	struct virtchnl_vlan_supported_caps *supported_caps;
3134 
3135 	if (ice_vf_outer_vlan_not_allowed(vf)) {
3136 		/* until support for inner VLAN filtering is added when a port
3137 		 * VLAN is configured, only support software offloaded inner
3138 		 * VLANs when a port VLAN is confgured in DVM
3139 		 */
3140 		supported_caps = &caps->filtering.filtering_support;
3141 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3142 
3143 		supported_caps = &caps->offloads.stripping_support;
3144 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3145 					VIRTCHNL_VLAN_TOGGLE |
3146 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3147 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3148 
3149 		supported_caps = &caps->offloads.insertion_support;
3150 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3151 					VIRTCHNL_VLAN_TOGGLE |
3152 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3153 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3154 
3155 		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
3156 		caps->offloads.ethertype_match =
3157 			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
3158 	} else {
3159 		supported_caps = &caps->filtering.filtering_support;
3160 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3161 		supported_caps->outer = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3162 					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
3163 					VIRTCHNL_VLAN_ETHERTYPE_9100 |
3164 					VIRTCHNL_VLAN_ETHERTYPE_AND;
3165 		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3166 						 VIRTCHNL_VLAN_ETHERTYPE_88A8 |
3167 						 VIRTCHNL_VLAN_ETHERTYPE_9100;
3168 
3169 		supported_caps = &caps->offloads.stripping_support;
3170 		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
3171 					VIRTCHNL_VLAN_ETHERTYPE_8100 |
3172 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3173 		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
3174 					VIRTCHNL_VLAN_ETHERTYPE_8100 |
3175 					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
3176 					VIRTCHNL_VLAN_ETHERTYPE_9100 |
3177 					VIRTCHNL_VLAN_ETHERTYPE_XOR |
3178 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2;
3179 
3180 		supported_caps = &caps->offloads.insertion_support;
3181 		supported_caps->inner = VIRTCHNL_VLAN_TOGGLE |
3182 					VIRTCHNL_VLAN_ETHERTYPE_8100 |
3183 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3184 		supported_caps->outer = VIRTCHNL_VLAN_TOGGLE |
3185 					VIRTCHNL_VLAN_ETHERTYPE_8100 |
3186 					VIRTCHNL_VLAN_ETHERTYPE_88A8 |
3187 					VIRTCHNL_VLAN_ETHERTYPE_9100 |
3188 					VIRTCHNL_VLAN_ETHERTYPE_XOR |
3189 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2;
3190 
3191 		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
3192 
3193 		caps->offloads.ethertype_match =
3194 			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
3195 	}
3196 
3197 	caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
3198 }
3199 
3200 /**
3201  * ice_vc_set_svm_caps - set VLAN capabilities when the device is in SVM
3202  * @vf: VF that capabilities are being set for
3203  * @caps: VLAN capabilities to populate
3204  *
3205  * Determine VLAN capabilities support based on whether a port VLAN is
3206  * configured. If a port VLAN is configured then the VF does not have any VLAN
3207  * filtering or offload capabilities since the port VLAN is using the inner VLAN
3208  * capabilities in single VLAN mode (SVM). Otherwise allow the VF to use inner
3209  * VLAN fitlering and offload capabilities.
3210  */
3211 static void
3212 ice_vc_set_svm_caps(struct ice_vf *vf, struct virtchnl_vlan_caps *caps)
3213 {
3214 	struct virtchnl_vlan_supported_caps *supported_caps;
3215 
3216 	if (ice_vf_is_port_vlan_ena(vf)) {
3217 		supported_caps = &caps->filtering.filtering_support;
3218 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3219 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3220 
3221 		supported_caps = &caps->offloads.stripping_support;
3222 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3223 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3224 
3225 		supported_caps = &caps->offloads.insertion_support;
3226 		supported_caps->inner = VIRTCHNL_VLAN_UNSUPPORTED;
3227 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3228 
3229 		caps->offloads.ethertype_init = VIRTCHNL_VLAN_UNSUPPORTED;
3230 		caps->offloads.ethertype_match = VIRTCHNL_VLAN_UNSUPPORTED;
3231 		caps->filtering.max_filters = 0;
3232 	} else {
3233 		supported_caps = &caps->filtering.filtering_support;
3234 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100;
3235 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3236 		caps->filtering.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
3237 
3238 		supported_caps = &caps->offloads.stripping_support;
3239 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3240 					VIRTCHNL_VLAN_TOGGLE |
3241 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3242 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3243 
3244 		supported_caps = &caps->offloads.insertion_support;
3245 		supported_caps->inner = VIRTCHNL_VLAN_ETHERTYPE_8100 |
3246 					VIRTCHNL_VLAN_TOGGLE |
3247 					VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1;
3248 		supported_caps->outer = VIRTCHNL_VLAN_UNSUPPORTED;
3249 
3250 		caps->offloads.ethertype_init = VIRTCHNL_VLAN_ETHERTYPE_8100;
3251 		caps->offloads.ethertype_match =
3252 			VIRTCHNL_ETHERTYPE_STRIPPING_MATCHES_INSERTION;
3253 		caps->filtering.max_filters = ice_vc_get_max_vlan_fltrs(vf);
3254 	}
3255 }
3256 
3257 /**
3258  * ice_vc_get_offload_vlan_v2_caps - determine VF's VLAN capabilities
3259  * @vf: VF to determine VLAN capabilities for
3260  *
3261  * This will only be called if the VF and PF successfully negotiated
3262  * VIRTCHNL_VF_OFFLOAD_VLAN_V2.
3263  *
3264  * Set VLAN capabilities based on the current VLAN mode and whether a port VLAN
3265  * is configured or not.
3266  */
3267 static int ice_vc_get_offload_vlan_v2_caps(struct ice_vf *vf)
3268 {
3269 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3270 	struct virtchnl_vlan_caps *caps = NULL;
3271 	int err, len = 0;
3272 
3273 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3274 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3275 		goto out;
3276 	}
3277 
3278 	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
3279 	if (!caps) {
3280 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
3281 		goto out;
3282 	}
3283 	len = sizeof(*caps);
3284 
3285 	if (ice_is_dvm_ena(&vf->pf->hw))
3286 		ice_vc_set_dvm_caps(vf, caps);
3287 	else
3288 		ice_vc_set_svm_caps(vf, caps);
3289 
3290 	/* store negotiated caps to prevent invalid VF messages */
3291 	memcpy(&vf->vlan_v2_caps, caps, sizeof(*caps));
3292 
3293 out:
3294 	err = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS,
3295 				    v_ret, (u8 *)caps, len);
3296 	kfree(caps);
3297 	return err;
3298 }
3299 
3300 /**
3301  * ice_vc_validate_vlan_tpid - validate VLAN TPID
3302  * @filtering_caps: negotiated/supported VLAN filtering capabilities
3303  * @tpid: VLAN TPID used for validation
3304  *
3305  * Convert the VLAN TPID to a VIRTCHNL_VLAN_ETHERTYPE_* and then compare against
3306  * the negotiated/supported filtering caps to see if the VLAN TPID is valid.
3307  */
3308 static bool ice_vc_validate_vlan_tpid(u16 filtering_caps, u16 tpid)
3309 {
3310 	enum virtchnl_vlan_support vlan_ethertype = VIRTCHNL_VLAN_UNSUPPORTED;
3311 
3312 	switch (tpid) {
3313 	case ETH_P_8021Q:
3314 		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
3315 		break;
3316 	case ETH_P_8021AD:
3317 		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_88A8;
3318 		break;
3319 	case ETH_P_QINQ1:
3320 		vlan_ethertype = VIRTCHNL_VLAN_ETHERTYPE_9100;
3321 		break;
3322 	}
3323 
3324 	if (!(filtering_caps & vlan_ethertype))
3325 		return false;
3326 
3327 	return true;
3328 }
3329 
3330 /**
3331  * ice_vc_is_valid_vlan - validate the virtchnl_vlan
3332  * @vc_vlan: virtchnl_vlan to validate
3333  *
3334  * If the VLAN TCI and VLAN TPID are 0, then this filter is invalid, so return
3335  * false. Otherwise return true.
3336  */
3337 static bool ice_vc_is_valid_vlan(struct virtchnl_vlan *vc_vlan)
3338 {
3339 	if (!vc_vlan->tci || !vc_vlan->tpid)
3340 		return false;
3341 
3342 	return true;
3343 }
3344 
3345 /**
3346  * ice_vc_validate_vlan_filter_list - validate the filter list from the VF
3347  * @vfc: negotiated/supported VLAN filtering capabilities
3348  * @vfl: VLAN filter list from VF to validate
3349  *
3350  * Validate all of the filters in the VLAN filter list from the VF. If any of
3351  * the checks fail then return false. Otherwise return true.
3352  */
3353 static bool
3354 ice_vc_validate_vlan_filter_list(struct virtchnl_vlan_filtering_caps *vfc,
3355 				 struct virtchnl_vlan_filter_list_v2 *vfl)
3356 {
3357 	u16 i;
3358 
3359 	if (!vfl->num_elements)
3360 		return false;
3361 
3362 	for (i = 0; i < vfl->num_elements; i++) {
3363 		struct virtchnl_vlan_supported_caps *filtering_support =
3364 			&vfc->filtering_support;
3365 		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3366 		struct virtchnl_vlan *outer = &vlan_fltr->outer;
3367 		struct virtchnl_vlan *inner = &vlan_fltr->inner;
3368 
3369 		if ((ice_vc_is_valid_vlan(outer) &&
3370 		     filtering_support->outer == VIRTCHNL_VLAN_UNSUPPORTED) ||
3371 		    (ice_vc_is_valid_vlan(inner) &&
3372 		     filtering_support->inner == VIRTCHNL_VLAN_UNSUPPORTED))
3373 			return false;
3374 
3375 		if ((outer->tci_mask &&
3376 		     !(filtering_support->outer & VIRTCHNL_VLAN_FILTER_MASK)) ||
3377 		    (inner->tci_mask &&
3378 		     !(filtering_support->inner & VIRTCHNL_VLAN_FILTER_MASK)))
3379 			return false;
3380 
3381 		if (((outer->tci & VLAN_PRIO_MASK) &&
3382 		     !(filtering_support->outer & VIRTCHNL_VLAN_PRIO)) ||
3383 		    ((inner->tci & VLAN_PRIO_MASK) &&
3384 		     !(filtering_support->inner & VIRTCHNL_VLAN_PRIO)))
3385 			return false;
3386 
3387 		if ((ice_vc_is_valid_vlan(outer) &&
3388 		     !ice_vc_validate_vlan_tpid(filtering_support->outer,
3389 						outer->tpid)) ||
3390 		    (ice_vc_is_valid_vlan(inner) &&
3391 		     !ice_vc_validate_vlan_tpid(filtering_support->inner,
3392 						inner->tpid)))
3393 			return false;
3394 	}
3395 
3396 	return true;
3397 }
3398 
3399 /**
3400  * ice_vc_to_vlan - transform from struct virtchnl_vlan to struct ice_vlan
3401  * @vc_vlan: struct virtchnl_vlan to transform
3402  */
3403 static struct ice_vlan ice_vc_to_vlan(struct virtchnl_vlan *vc_vlan)
3404 {
3405 	struct ice_vlan vlan = { 0 };
3406 
3407 	vlan.prio = FIELD_GET(VLAN_PRIO_MASK, vc_vlan->tci);
3408 	vlan.vid = vc_vlan->tci & VLAN_VID_MASK;
3409 	vlan.tpid = vc_vlan->tpid;
3410 
3411 	return vlan;
3412 }
3413 
3414 /**
3415  * ice_vc_vlan_action - action to perform on the virthcnl_vlan
3416  * @vsi: VF's VSI used to perform the action
3417  * @vlan_action: function to perform the action with (i.e. add/del)
3418  * @vlan: VLAN filter to perform the action with
3419  */
3420 static int
3421 ice_vc_vlan_action(struct ice_vsi *vsi,
3422 		   int (*vlan_action)(struct ice_vsi *, struct ice_vlan *),
3423 		   struct ice_vlan *vlan)
3424 {
3425 	int err;
3426 
3427 	err = vlan_action(vsi, vlan);
3428 	if (err)
3429 		return err;
3430 
3431 	return 0;
3432 }
3433 
3434 /**
3435  * ice_vc_del_vlans - delete VLAN(s) from the virtchnl filter list
3436  * @vf: VF used to delete the VLAN(s)
3437  * @vsi: VF's VSI used to delete the VLAN(s)
3438  * @vfl: virthchnl filter list used to delete the filters
3439  */
3440 static int
3441 ice_vc_del_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3442 		 struct virtchnl_vlan_filter_list_v2 *vfl)
3443 {
3444 	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3445 	int err;
3446 	u16 i;
3447 
3448 	for (i = 0; i < vfl->num_elements; i++) {
3449 		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3450 		struct virtchnl_vlan *vc_vlan;
3451 
3452 		vc_vlan = &vlan_fltr->outer;
3453 		if (ice_vc_is_valid_vlan(vc_vlan)) {
3454 			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3455 
3456 			err = ice_vc_vlan_action(vsi,
3457 						 vsi->outer_vlan_ops.del_vlan,
3458 						 &vlan);
3459 			if (err)
3460 				return err;
3461 
3462 			if (vlan_promisc)
3463 				ice_vf_dis_vlan_promisc(vsi, &vlan);
3464 
3465 			/* Disable VLAN filtering when only VLAN 0 is left */
3466 			if (!ice_vsi_has_non_zero_vlans(vsi) && ice_is_dvm_ena(&vsi->back->hw)) {
3467 				err = vsi->outer_vlan_ops.dis_tx_filtering(vsi);
3468 				if (err)
3469 					return err;
3470 			}
3471 		}
3472 
3473 		vc_vlan = &vlan_fltr->inner;
3474 		if (ice_vc_is_valid_vlan(vc_vlan)) {
3475 			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3476 
3477 			err = ice_vc_vlan_action(vsi,
3478 						 vsi->inner_vlan_ops.del_vlan,
3479 						 &vlan);
3480 			if (err)
3481 				return err;
3482 
3483 			/* no support for VLAN promiscuous on inner VLAN unless
3484 			 * we are in Single VLAN Mode (SVM)
3485 			 */
3486 			if (!ice_is_dvm_ena(&vsi->back->hw)) {
3487 				if (vlan_promisc)
3488 					ice_vf_dis_vlan_promisc(vsi, &vlan);
3489 
3490 				/* Disable VLAN filtering when only VLAN 0 is left */
3491 				if (!ice_vsi_has_non_zero_vlans(vsi)) {
3492 					err = vsi->inner_vlan_ops.dis_tx_filtering(vsi);
3493 					if (err)
3494 						return err;
3495 				}
3496 			}
3497 		}
3498 	}
3499 
3500 	return 0;
3501 }
3502 
3503 /**
3504  * ice_vc_remove_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_DEL_VLAN_V2
3505  * @vf: VF the message was received from
3506  * @msg: message received from the VF
3507  */
3508 static int ice_vc_remove_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3509 {
3510 	struct virtchnl_vlan_filter_list_v2 *vfl =
3511 		(struct virtchnl_vlan_filter_list_v2 *)msg;
3512 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3513 	struct ice_vsi *vsi;
3514 
3515 	if (!ice_vc_validate_vlan_filter_list(&vf->vlan_v2_caps.filtering,
3516 					      vfl)) {
3517 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3518 		goto out;
3519 	}
3520 
3521 	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3522 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3523 		goto out;
3524 	}
3525 
3526 	vsi = ice_get_vf_vsi(vf);
3527 	if (!vsi) {
3528 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3529 		goto out;
3530 	}
3531 
3532 	if (ice_vc_del_vlans(vf, vsi, vfl))
3533 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3534 
3535 out:
3536 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN_V2, v_ret, NULL,
3537 				     0);
3538 }
3539 
3540 /**
3541  * ice_vc_add_vlans - add VLAN(s) from the virtchnl filter list
3542  * @vf: VF used to add the VLAN(s)
3543  * @vsi: VF's VSI used to add the VLAN(s)
3544  * @vfl: virthchnl filter list used to add the filters
3545  */
3546 static int
3547 ice_vc_add_vlans(struct ice_vf *vf, struct ice_vsi *vsi,
3548 		 struct virtchnl_vlan_filter_list_v2 *vfl)
3549 {
3550 	bool vlan_promisc = ice_is_vlan_promisc_allowed(vf);
3551 	int err;
3552 	u16 i;
3553 
3554 	for (i = 0; i < vfl->num_elements; i++) {
3555 		struct virtchnl_vlan_filter *vlan_fltr = &vfl->filters[i];
3556 		struct virtchnl_vlan *vc_vlan;
3557 
3558 		vc_vlan = &vlan_fltr->outer;
3559 		if (ice_vc_is_valid_vlan(vc_vlan)) {
3560 			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3561 
3562 			err = ice_vc_vlan_action(vsi,
3563 						 vsi->outer_vlan_ops.add_vlan,
3564 						 &vlan);
3565 			if (err)
3566 				return err;
3567 
3568 			if (vlan_promisc) {
3569 				err = ice_vf_ena_vlan_promisc(vf, vsi, &vlan);
3570 				if (err)
3571 					return err;
3572 			}
3573 
3574 			/* Enable VLAN filtering on first non-zero VLAN */
3575 			if (vf->spoofchk && vlan.vid && ice_is_dvm_ena(&vsi->back->hw)) {
3576 				err = vsi->outer_vlan_ops.ena_tx_filtering(vsi);
3577 				if (err)
3578 					return err;
3579 			}
3580 		}
3581 
3582 		vc_vlan = &vlan_fltr->inner;
3583 		if (ice_vc_is_valid_vlan(vc_vlan)) {
3584 			struct ice_vlan vlan = ice_vc_to_vlan(vc_vlan);
3585 
3586 			err = ice_vc_vlan_action(vsi,
3587 						 vsi->inner_vlan_ops.add_vlan,
3588 						 &vlan);
3589 			if (err)
3590 				return err;
3591 
3592 			/* no support for VLAN promiscuous on inner VLAN unless
3593 			 * we are in Single VLAN Mode (SVM)
3594 			 */
3595 			if (!ice_is_dvm_ena(&vsi->back->hw)) {
3596 				if (vlan_promisc) {
3597 					err = ice_vf_ena_vlan_promisc(vf, vsi,
3598 								      &vlan);
3599 					if (err)
3600 						return err;
3601 				}
3602 
3603 				/* Enable VLAN filtering on first non-zero VLAN */
3604 				if (vf->spoofchk && vlan.vid) {
3605 					err = vsi->inner_vlan_ops.ena_tx_filtering(vsi);
3606 					if (err)
3607 						return err;
3608 				}
3609 			}
3610 		}
3611 	}
3612 
3613 	return 0;
3614 }
3615 
3616 /**
3617  * ice_vc_validate_add_vlan_filter_list - validate add filter list from the VF
3618  * @vsi: VF VSI used to get number of existing VLAN filters
3619  * @vfc: negotiated/supported VLAN filtering capabilities
3620  * @vfl: VLAN filter list from VF to validate
3621  *
3622  * Validate all of the filters in the VLAN filter list from the VF during the
3623  * VIRTCHNL_OP_ADD_VLAN_V2 opcode. If any of the checks fail then return false.
3624  * Otherwise return true.
3625  */
3626 static bool
3627 ice_vc_validate_add_vlan_filter_list(struct ice_vsi *vsi,
3628 				     struct virtchnl_vlan_filtering_caps *vfc,
3629 				     struct virtchnl_vlan_filter_list_v2 *vfl)
3630 {
3631 	u16 num_requested_filters = ice_vsi_num_non_zero_vlans(vsi) +
3632 		vfl->num_elements;
3633 
3634 	if (num_requested_filters > vfc->max_filters)
3635 		return false;
3636 
3637 	return ice_vc_validate_vlan_filter_list(vfc, vfl);
3638 }
3639 
3640 /**
3641  * ice_vc_add_vlan_v2_msg - virtchnl handler for VIRTCHNL_OP_ADD_VLAN_V2
3642  * @vf: VF the message was received from
3643  * @msg: message received from the VF
3644  */
3645 static int ice_vc_add_vlan_v2_msg(struct ice_vf *vf, u8 *msg)
3646 {
3647 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3648 	struct virtchnl_vlan_filter_list_v2 *vfl =
3649 		(struct virtchnl_vlan_filter_list_v2 *)msg;
3650 	struct ice_vsi *vsi;
3651 
3652 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3653 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3654 		goto out;
3655 	}
3656 
3657 	if (!ice_vc_isvalid_vsi_id(vf, vfl->vport_id)) {
3658 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3659 		goto out;
3660 	}
3661 
3662 	vsi = ice_get_vf_vsi(vf);
3663 	if (!vsi) {
3664 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3665 		goto out;
3666 	}
3667 
3668 	if (!ice_vc_validate_add_vlan_filter_list(vsi,
3669 						  &vf->vlan_v2_caps.filtering,
3670 						  vfl)) {
3671 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3672 		goto out;
3673 	}
3674 
3675 	if (ice_vc_add_vlans(vf, vsi, vfl))
3676 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3677 
3678 out:
3679 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN_V2, v_ret, NULL,
3680 				     0);
3681 }
3682 
3683 /**
3684  * ice_vc_valid_vlan_setting - validate VLAN setting
3685  * @negotiated_settings: negotiated VLAN settings during VF init
3686  * @ethertype_setting: ethertype(s) requested for the VLAN setting
3687  */
3688 static bool
3689 ice_vc_valid_vlan_setting(u32 negotiated_settings, u32 ethertype_setting)
3690 {
3691 	if (ethertype_setting && !(negotiated_settings & ethertype_setting))
3692 		return false;
3693 
3694 	/* only allow a single VIRTCHNL_VLAN_ETHERTYPE if
3695 	 * VIRTHCNL_VLAN_ETHERTYPE_AND is not negotiated/supported
3696 	 */
3697 	if (!(negotiated_settings & VIRTCHNL_VLAN_ETHERTYPE_AND) &&
3698 	    hweight32(ethertype_setting) > 1)
3699 		return false;
3700 
3701 	/* ability to modify the VLAN setting was not negotiated */
3702 	if (!(negotiated_settings & VIRTCHNL_VLAN_TOGGLE))
3703 		return false;
3704 
3705 	return true;
3706 }
3707 
3708 /**
3709  * ice_vc_valid_vlan_setting_msg - validate the VLAN setting message
3710  * @caps: negotiated VLAN settings during VF init
3711  * @msg: message to validate
3712  *
3713  * Used to validate any VLAN virtchnl message sent as a
3714  * virtchnl_vlan_setting structure. Validates the message against the
3715  * negotiated/supported caps during VF driver init.
3716  */
3717 static bool
3718 ice_vc_valid_vlan_setting_msg(struct virtchnl_vlan_supported_caps *caps,
3719 			      struct virtchnl_vlan_setting *msg)
3720 {
3721 	if ((!msg->outer_ethertype_setting &&
3722 	     !msg->inner_ethertype_setting) ||
3723 	    (!caps->outer && !caps->inner))
3724 		return false;
3725 
3726 	if (msg->outer_ethertype_setting &&
3727 	    !ice_vc_valid_vlan_setting(caps->outer,
3728 				       msg->outer_ethertype_setting))
3729 		return false;
3730 
3731 	if (msg->inner_ethertype_setting &&
3732 	    !ice_vc_valid_vlan_setting(caps->inner,
3733 				       msg->inner_ethertype_setting))
3734 		return false;
3735 
3736 	return true;
3737 }
3738 
3739 /**
3740  * ice_vc_get_tpid - transform from VIRTCHNL_VLAN_ETHERTYPE_* to VLAN TPID
3741  * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* used to get VLAN TPID
3742  * @tpid: VLAN TPID to populate
3743  */
3744 static int ice_vc_get_tpid(u32 ethertype_setting, u16 *tpid)
3745 {
3746 	switch (ethertype_setting) {
3747 	case VIRTCHNL_VLAN_ETHERTYPE_8100:
3748 		*tpid = ETH_P_8021Q;
3749 		break;
3750 	case VIRTCHNL_VLAN_ETHERTYPE_88A8:
3751 		*tpid = ETH_P_8021AD;
3752 		break;
3753 	case VIRTCHNL_VLAN_ETHERTYPE_9100:
3754 		*tpid = ETH_P_QINQ1;
3755 		break;
3756 	default:
3757 		*tpid = 0;
3758 		return -EINVAL;
3759 	}
3760 
3761 	return 0;
3762 }
3763 
3764 /**
3765  * ice_vc_ena_vlan_offload - enable VLAN offload based on the ethertype_setting
3766  * @vsi: VF's VSI used to enable the VLAN offload
3767  * @ena_offload: function used to enable the VLAN offload
3768  * @ethertype_setting: VIRTCHNL_VLAN_ETHERTYPE_* to enable offloads for
3769  */
3770 static int
3771 ice_vc_ena_vlan_offload(struct ice_vsi *vsi,
3772 			int (*ena_offload)(struct ice_vsi *vsi, u16 tpid),
3773 			u32 ethertype_setting)
3774 {
3775 	u16 tpid;
3776 	int err;
3777 
3778 	err = ice_vc_get_tpid(ethertype_setting, &tpid);
3779 	if (err)
3780 		return err;
3781 
3782 	err = ena_offload(vsi, tpid);
3783 	if (err)
3784 		return err;
3785 
3786 	return 0;
3787 }
3788 
3789 #define ICE_L2TSEL_QRX_CONTEXT_REG_IDX	3
3790 #define ICE_L2TSEL_BIT_OFFSET		23
3791 enum ice_l2tsel {
3792 	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND,
3793 	ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1,
3794 };
3795 
3796 /**
3797  * ice_vsi_update_l2tsel - update l2tsel field for all Rx rings on this VSI
3798  * @vsi: VSI used to update l2tsel on
3799  * @l2tsel: l2tsel setting requested
3800  *
3801  * Use the l2tsel setting to update all of the Rx queue context bits for l2tsel.
3802  * This will modify which descriptor field the first offloaded VLAN will be
3803  * stripped into.
3804  */
3805 static void ice_vsi_update_l2tsel(struct ice_vsi *vsi, enum ice_l2tsel l2tsel)
3806 {
3807 	struct ice_hw *hw = &vsi->back->hw;
3808 	u32 l2tsel_bit;
3809 	int i;
3810 
3811 	if (l2tsel == ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND)
3812 		l2tsel_bit = 0;
3813 	else
3814 		l2tsel_bit = BIT(ICE_L2TSEL_BIT_OFFSET);
3815 
3816 	for (i = 0; i < vsi->alloc_rxq; i++) {
3817 		u16 pfq = vsi->rxq_map[i];
3818 		u32 qrx_context_offset;
3819 		u32 regval;
3820 
3821 		qrx_context_offset =
3822 			QRX_CONTEXT(ICE_L2TSEL_QRX_CONTEXT_REG_IDX, pfq);
3823 
3824 		regval = rd32(hw, qrx_context_offset);
3825 		regval &= ~BIT(ICE_L2TSEL_BIT_OFFSET);
3826 		regval |= l2tsel_bit;
3827 		wr32(hw, qrx_context_offset, regval);
3828 	}
3829 }
3830 
3831 /**
3832  * ice_vc_ena_vlan_stripping_v2_msg
3833  * @vf: VF the message was received from
3834  * @msg: message received from the VF
3835  *
3836  * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2
3837  */
3838 static int ice_vc_ena_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3839 {
3840 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3841 	struct virtchnl_vlan_supported_caps *stripping_support;
3842 	struct virtchnl_vlan_setting *strip_msg =
3843 		(struct virtchnl_vlan_setting *)msg;
3844 	u32 ethertype_setting;
3845 	struct ice_vsi *vsi;
3846 
3847 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3848 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3849 		goto out;
3850 	}
3851 
3852 	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3853 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3854 		goto out;
3855 	}
3856 
3857 	vsi = ice_get_vf_vsi(vf);
3858 	if (!vsi) {
3859 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3860 		goto out;
3861 	}
3862 
3863 	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3864 	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3865 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3866 		goto out;
3867 	}
3868 
3869 	if (ice_vsi_is_rxq_crc_strip_dis(vsi)) {
3870 		v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
3871 		goto out;
3872 	}
3873 
3874 	ethertype_setting = strip_msg->outer_ethertype_setting;
3875 	if (ethertype_setting) {
3876 		if (ice_vc_ena_vlan_offload(vsi,
3877 					    vsi->outer_vlan_ops.ena_stripping,
3878 					    ethertype_setting)) {
3879 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3880 			goto out;
3881 		} else {
3882 			enum ice_l2tsel l2tsel =
3883 				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG2_2ND;
3884 
3885 			/* PF tells the VF that the outer VLAN tag is always
3886 			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3887 			 * inner is always extracted to
3888 			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3889 			 * support outer stripping so the first tag always ends
3890 			 * up in L2TAG2_2ND and the second/inner tag, if
3891 			 * enabled, is extracted in L2TAG1.
3892 			 */
3893 			ice_vsi_update_l2tsel(vsi, l2tsel);
3894 
3895 			vf->vlan_strip_ena |= ICE_OUTER_VLAN_STRIP_ENA;
3896 		}
3897 	}
3898 
3899 	ethertype_setting = strip_msg->inner_ethertype_setting;
3900 	if (ethertype_setting &&
3901 	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_stripping,
3902 				    ethertype_setting)) {
3903 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3904 		goto out;
3905 	}
3906 
3907 	if (ethertype_setting)
3908 		vf->vlan_strip_ena |= ICE_INNER_VLAN_STRIP_ENA;
3909 
3910 out:
3911 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2,
3912 				     v_ret, NULL, 0);
3913 }
3914 
3915 /**
3916  * ice_vc_dis_vlan_stripping_v2_msg
3917  * @vf: VF the message was received from
3918  * @msg: message received from the VF
3919  *
3920  * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2
3921  */
3922 static int ice_vc_dis_vlan_stripping_v2_msg(struct ice_vf *vf, u8 *msg)
3923 {
3924 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
3925 	struct virtchnl_vlan_supported_caps *stripping_support;
3926 	struct virtchnl_vlan_setting *strip_msg =
3927 		(struct virtchnl_vlan_setting *)msg;
3928 	u32 ethertype_setting;
3929 	struct ice_vsi *vsi;
3930 
3931 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
3932 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3933 		goto out;
3934 	}
3935 
3936 	if (!ice_vc_isvalid_vsi_id(vf, strip_msg->vport_id)) {
3937 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3938 		goto out;
3939 	}
3940 
3941 	vsi = ice_get_vf_vsi(vf);
3942 	if (!vsi) {
3943 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3944 		goto out;
3945 	}
3946 
3947 	stripping_support = &vf->vlan_v2_caps.offloads.stripping_support;
3948 	if (!ice_vc_valid_vlan_setting_msg(stripping_support, strip_msg)) {
3949 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3950 		goto out;
3951 	}
3952 
3953 	ethertype_setting = strip_msg->outer_ethertype_setting;
3954 	if (ethertype_setting) {
3955 		if (vsi->outer_vlan_ops.dis_stripping(vsi)) {
3956 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3957 			goto out;
3958 		} else {
3959 			enum ice_l2tsel l2tsel =
3960 				ICE_L2TSEL_EXTRACT_FIRST_TAG_L2TAG1;
3961 
3962 			/* PF tells the VF that the outer VLAN tag is always
3963 			 * extracted to VIRTCHNL_VLAN_TAG_LOCATION_L2TAG2_2 and
3964 			 * inner is always extracted to
3965 			 * VIRTCHNL_VLAN_TAG_LOCATION_L2TAG1. This is needed to
3966 			 * support inner stripping while outer stripping is
3967 			 * disabled so that the first and only tag is extracted
3968 			 * in L2TAG1.
3969 			 */
3970 			ice_vsi_update_l2tsel(vsi, l2tsel);
3971 
3972 			vf->vlan_strip_ena &= ~ICE_OUTER_VLAN_STRIP_ENA;
3973 		}
3974 	}
3975 
3976 	ethertype_setting = strip_msg->inner_ethertype_setting;
3977 	if (ethertype_setting && vsi->inner_vlan_ops.dis_stripping(vsi)) {
3978 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
3979 		goto out;
3980 	}
3981 
3982 	if (ethertype_setting)
3983 		vf->vlan_strip_ena &= ~ICE_INNER_VLAN_STRIP_ENA;
3984 
3985 out:
3986 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2,
3987 				     v_ret, NULL, 0);
3988 }
3989 
3990 /**
3991  * ice_vc_ena_vlan_insertion_v2_msg
3992  * @vf: VF the message was received from
3993  * @msg: message received from the VF
3994  *
3995  * virthcnl handler for VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2
3996  */
3997 static int ice_vc_ena_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
3998 {
3999 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4000 	struct virtchnl_vlan_supported_caps *insertion_support;
4001 	struct virtchnl_vlan_setting *insertion_msg =
4002 		(struct virtchnl_vlan_setting *)msg;
4003 	u32 ethertype_setting;
4004 	struct ice_vsi *vsi;
4005 
4006 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4007 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4008 		goto out;
4009 	}
4010 
4011 	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
4012 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4013 		goto out;
4014 	}
4015 
4016 	vsi = ice_get_vf_vsi(vf);
4017 	if (!vsi) {
4018 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4019 		goto out;
4020 	}
4021 
4022 	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
4023 	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
4024 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4025 		goto out;
4026 	}
4027 
4028 	ethertype_setting = insertion_msg->outer_ethertype_setting;
4029 	if (ethertype_setting &&
4030 	    ice_vc_ena_vlan_offload(vsi, vsi->outer_vlan_ops.ena_insertion,
4031 				    ethertype_setting)) {
4032 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4033 		goto out;
4034 	}
4035 
4036 	ethertype_setting = insertion_msg->inner_ethertype_setting;
4037 	if (ethertype_setting &&
4038 	    ice_vc_ena_vlan_offload(vsi, vsi->inner_vlan_ops.ena_insertion,
4039 				    ethertype_setting)) {
4040 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4041 		goto out;
4042 	}
4043 
4044 out:
4045 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2,
4046 				     v_ret, NULL, 0);
4047 }
4048 
4049 /**
4050  * ice_vc_dis_vlan_insertion_v2_msg
4051  * @vf: VF the message was received from
4052  * @msg: message received from the VF
4053  *
4054  * virthcnl handler for VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2
4055  */
4056 static int ice_vc_dis_vlan_insertion_v2_msg(struct ice_vf *vf, u8 *msg)
4057 {
4058 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4059 	struct virtchnl_vlan_supported_caps *insertion_support;
4060 	struct virtchnl_vlan_setting *insertion_msg =
4061 		(struct virtchnl_vlan_setting *)msg;
4062 	u32 ethertype_setting;
4063 	struct ice_vsi *vsi;
4064 
4065 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
4066 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4067 		goto out;
4068 	}
4069 
4070 	if (!ice_vc_isvalid_vsi_id(vf, insertion_msg->vport_id)) {
4071 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4072 		goto out;
4073 	}
4074 
4075 	vsi = ice_get_vf_vsi(vf);
4076 	if (!vsi) {
4077 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4078 		goto out;
4079 	}
4080 
4081 	insertion_support = &vf->vlan_v2_caps.offloads.insertion_support;
4082 	if (!ice_vc_valid_vlan_setting_msg(insertion_support, insertion_msg)) {
4083 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4084 		goto out;
4085 	}
4086 
4087 	ethertype_setting = insertion_msg->outer_ethertype_setting;
4088 	if (ethertype_setting && vsi->outer_vlan_ops.dis_insertion(vsi)) {
4089 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4090 		goto out;
4091 	}
4092 
4093 	ethertype_setting = insertion_msg->inner_ethertype_setting;
4094 	if (ethertype_setting && vsi->inner_vlan_ops.dis_insertion(vsi)) {
4095 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4096 		goto out;
4097 	}
4098 
4099 out:
4100 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2,
4101 				     v_ret, NULL, 0);
4102 }
4103 
4104 static int ice_vc_get_ptp_cap(struct ice_vf *vf,
4105 			      const struct virtchnl_ptp_caps *msg)
4106 {
4107 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4108 	u32 caps = VIRTCHNL_1588_PTP_CAP_RX_TSTAMP |
4109 		   VIRTCHNL_1588_PTP_CAP_READ_PHC;
4110 
4111 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
4112 		goto err;
4113 
4114 	v_ret = VIRTCHNL_STATUS_SUCCESS;
4115 
4116 	if (msg->caps & caps)
4117 		vf->ptp_caps = caps;
4118 
4119 err:
4120 	/* send the response back to the VF */
4121 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_1588_PTP_GET_CAPS, v_ret,
4122 				     (u8 *)&vf->ptp_caps,
4123 				     sizeof(struct virtchnl_ptp_caps));
4124 }
4125 
4126 static int ice_vc_get_phc_time(struct ice_vf *vf)
4127 {
4128 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4129 	struct virtchnl_phc_time *phc_time = NULL;
4130 	struct ice_pf *pf = vf->pf;
4131 	u32 len = 0;
4132 	int ret;
4133 
4134 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
4135 		goto err;
4136 
4137 	v_ret = VIRTCHNL_STATUS_SUCCESS;
4138 
4139 	phc_time = kzalloc(sizeof(*phc_time), GFP_KERNEL);
4140 	if (!phc_time) {
4141 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
4142 		goto err;
4143 	}
4144 
4145 	len = sizeof(*phc_time);
4146 
4147 	phc_time->time = ice_ptp_read_src_clk_reg(pf, NULL);
4148 
4149 err:
4150 	/* send the response back to the VF */
4151 	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_1588_PTP_GET_TIME, v_ret,
4152 				    (u8 *)phc_time, len);
4153 	kfree(phc_time);
4154 	return ret;
4155 }
4156 
4157 static const struct ice_virtchnl_ops ice_virtchnl_dflt_ops = {
4158 	.get_ver_msg = ice_vc_get_ver_msg,
4159 	.get_vf_res_msg = ice_vc_get_vf_res_msg,
4160 	.reset_vf = ice_vc_reset_vf_msg,
4161 	.add_mac_addr_msg = ice_vc_add_mac_addr_msg,
4162 	.del_mac_addr_msg = ice_vc_del_mac_addr_msg,
4163 	.cfg_qs_msg = ice_vc_cfg_qs_msg,
4164 	.ena_qs_msg = ice_vc_ena_qs_msg,
4165 	.dis_qs_msg = ice_vc_dis_qs_msg,
4166 	.request_qs_msg = ice_vc_request_qs_msg,
4167 	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
4168 	.config_rss_key = ice_vc_config_rss_key,
4169 	.config_rss_lut = ice_vc_config_rss_lut,
4170 	.config_rss_hfunc = ice_vc_config_rss_hfunc,
4171 	.get_stats_msg = ice_vc_get_stats_msg,
4172 	.cfg_promiscuous_mode_msg = ice_vc_cfg_promiscuous_mode_msg,
4173 	.add_vlan_msg = ice_vc_add_vlan_msg,
4174 	.remove_vlan_msg = ice_vc_remove_vlan_msg,
4175 	.query_rxdid = ice_vc_query_rxdid,
4176 	.get_rss_hena = ice_vc_get_rss_hena,
4177 	.set_rss_hena_msg = ice_vc_set_rss_hena,
4178 	.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
4179 	.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
4180 	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
4181 	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
4182 	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
4183 	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
4184 	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
4185 	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
4186 	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
4187 	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
4188 	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
4189 	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
4190 	.get_qos_caps = ice_vc_get_qos_caps,
4191 	.cfg_q_bw = ice_vc_cfg_q_bw,
4192 	.cfg_q_quanta = ice_vc_cfg_q_quanta,
4193 	.get_ptp_cap = ice_vc_get_ptp_cap,
4194 	.get_phc_time = ice_vc_get_phc_time,
4195 	/* If you add a new op here please make sure to add it to
4196 	 * ice_virtchnl_repr_ops as well.
4197 	 */
4198 };
4199 
4200 /**
4201  * ice_virtchnl_set_dflt_ops - Switch to default virtchnl ops
4202  * @vf: the VF to switch ops
4203  */
4204 void ice_virtchnl_set_dflt_ops(struct ice_vf *vf)
4205 {
4206 	vf->virtchnl_ops = &ice_virtchnl_dflt_ops;
4207 }
4208 
4209 /**
4210  * ice_vc_repr_add_mac
4211  * @vf: pointer to VF
4212  * @msg: virtchannel message
4213  *
4214  * When port representors are created, we do not add MAC rule
4215  * to firmware, we store it so that PF could report same
4216  * MAC as VF.
4217  */
4218 static int ice_vc_repr_add_mac(struct ice_vf *vf, u8 *msg)
4219 {
4220 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
4221 	struct virtchnl_ether_addr_list *al =
4222 	    (struct virtchnl_ether_addr_list *)msg;
4223 	struct ice_vsi *vsi;
4224 	struct ice_pf *pf;
4225 	int i;
4226 
4227 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
4228 	    !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
4229 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4230 		goto handle_mac_exit;
4231 	}
4232 
4233 	pf = vf->pf;
4234 
4235 	vsi = ice_get_vf_vsi(vf);
4236 	if (!vsi) {
4237 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
4238 		goto handle_mac_exit;
4239 	}
4240 
4241 	for (i = 0; i < al->num_elements; i++) {
4242 		u8 *mac_addr = al->list[i].addr;
4243 
4244 		if (!is_unicast_ether_addr(mac_addr) ||
4245 		    ether_addr_equal(mac_addr, vf->hw_lan_addr))
4246 			continue;
4247 
4248 		if (vf->pf_set_mac) {
4249 			dev_err(ice_pf_to_dev(pf), "VF attempting to override administratively set MAC address\n");
4250 			v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
4251 			goto handle_mac_exit;
4252 		}
4253 
4254 		ice_vfhw_mac_add(vf, &al->list[i]);
4255 		vf->num_mac++;
4256 		break;
4257 	}
4258 
4259 handle_mac_exit:
4260 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
4261 				     v_ret, NULL, 0);
4262 }
4263 
4264 /**
4265  * ice_vc_repr_del_mac - response with success for deleting MAC
4266  * @vf: pointer to VF
4267  * @msg: virtchannel message
4268  *
4269  * Respond with success to not break normal VF flow.
4270  * For legacy VF driver try to update cached MAC address.
4271  */
4272 static int
4273 ice_vc_repr_del_mac(struct ice_vf __always_unused *vf, u8 __always_unused *msg)
4274 {
4275 	struct virtchnl_ether_addr_list *al =
4276 		(struct virtchnl_ether_addr_list *)msg;
4277 
4278 	ice_update_legacy_cached_mac(vf, &al->list[0]);
4279 
4280 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
4281 				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
4282 }
4283 
4284 static int
4285 ice_vc_repr_cfg_promiscuous_mode(struct ice_vf *vf, u8 __always_unused *msg)
4286 {
4287 	dev_dbg(ice_pf_to_dev(vf->pf),
4288 		"Can't config promiscuous mode in switchdev mode for VF %d\n",
4289 		vf->vf_id);
4290 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
4291 				     VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4292 				     NULL, 0);
4293 }
4294 
4295 static const struct ice_virtchnl_ops ice_virtchnl_repr_ops = {
4296 	.get_ver_msg = ice_vc_get_ver_msg,
4297 	.get_vf_res_msg = ice_vc_get_vf_res_msg,
4298 	.reset_vf = ice_vc_reset_vf_msg,
4299 	.add_mac_addr_msg = ice_vc_repr_add_mac,
4300 	.del_mac_addr_msg = ice_vc_repr_del_mac,
4301 	.cfg_qs_msg = ice_vc_cfg_qs_msg,
4302 	.ena_qs_msg = ice_vc_ena_qs_msg,
4303 	.dis_qs_msg = ice_vc_dis_qs_msg,
4304 	.request_qs_msg = ice_vc_request_qs_msg,
4305 	.cfg_irq_map_msg = ice_vc_cfg_irq_map_msg,
4306 	.config_rss_key = ice_vc_config_rss_key,
4307 	.config_rss_lut = ice_vc_config_rss_lut,
4308 	.config_rss_hfunc = ice_vc_config_rss_hfunc,
4309 	.get_stats_msg = ice_vc_get_stats_msg,
4310 	.cfg_promiscuous_mode_msg = ice_vc_repr_cfg_promiscuous_mode,
4311 	.add_vlan_msg = ice_vc_add_vlan_msg,
4312 	.remove_vlan_msg = ice_vc_remove_vlan_msg,
4313 	.query_rxdid = ice_vc_query_rxdid,
4314 	.get_rss_hena = ice_vc_get_rss_hena,
4315 	.set_rss_hena_msg = ice_vc_set_rss_hena,
4316 	.ena_vlan_stripping = ice_vc_ena_vlan_stripping,
4317 	.dis_vlan_stripping = ice_vc_dis_vlan_stripping,
4318 	.handle_rss_cfg_msg = ice_vc_handle_rss_cfg,
4319 	.add_fdir_fltr_msg = ice_vc_add_fdir_fltr,
4320 	.del_fdir_fltr_msg = ice_vc_del_fdir_fltr,
4321 	.get_offload_vlan_v2_caps = ice_vc_get_offload_vlan_v2_caps,
4322 	.add_vlan_v2_msg = ice_vc_add_vlan_v2_msg,
4323 	.remove_vlan_v2_msg = ice_vc_remove_vlan_v2_msg,
4324 	.ena_vlan_stripping_v2_msg = ice_vc_ena_vlan_stripping_v2_msg,
4325 	.dis_vlan_stripping_v2_msg = ice_vc_dis_vlan_stripping_v2_msg,
4326 	.ena_vlan_insertion_v2_msg = ice_vc_ena_vlan_insertion_v2_msg,
4327 	.dis_vlan_insertion_v2_msg = ice_vc_dis_vlan_insertion_v2_msg,
4328 	.get_qos_caps = ice_vc_get_qos_caps,
4329 	.cfg_q_bw = ice_vc_cfg_q_bw,
4330 	.cfg_q_quanta = ice_vc_cfg_q_quanta,
4331 	.get_ptp_cap = ice_vc_get_ptp_cap,
4332 	.get_phc_time = ice_vc_get_phc_time,
4333 };
4334 
4335 /**
4336  * ice_virtchnl_set_repr_ops - Switch to representor virtchnl ops
4337  * @vf: the VF to switch ops
4338  */
4339 void ice_virtchnl_set_repr_ops(struct ice_vf *vf)
4340 {
4341 	vf->virtchnl_ops = &ice_virtchnl_repr_ops;
4342 }
4343 
4344 /**
4345  * ice_is_malicious_vf - check if this vf might be overflowing mailbox
4346  * @vf: the VF to check
4347  * @mbxdata: data about the state of the mailbox
4348  *
4349  * Detect if a given VF might be malicious and attempting to overflow the PF
4350  * mailbox. If so, log a warning message and ignore this event.
4351  */
4352 static bool
4353 ice_is_malicious_vf(struct ice_vf *vf, struct ice_mbx_data *mbxdata)
4354 {
4355 	bool report_malvf = false;
4356 	struct device *dev;
4357 	struct ice_pf *pf;
4358 	int status;
4359 
4360 	pf = vf->pf;
4361 	dev = ice_pf_to_dev(pf);
4362 
4363 	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
4364 		return vf->mbx_info.malicious;
4365 
4366 	/* check to see if we have a newly malicious VF */
4367 	status = ice_mbx_vf_state_handler(&pf->hw, mbxdata, &vf->mbx_info,
4368 					  &report_malvf);
4369 	if (status)
4370 		dev_warn_ratelimited(dev, "Unable to check status of mailbox overflow for VF %u MAC %pM, status %d\n",
4371 				     vf->vf_id, vf->dev_lan_addr, status);
4372 
4373 	if (report_malvf) {
4374 		struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
4375 		u8 zero_addr[ETH_ALEN] = {};
4376 
4377 		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",
4378 			 vf->dev_lan_addr,
4379 			 pf_vsi ? pf_vsi->netdev->dev_addr : zero_addr);
4380 	}
4381 
4382 	return vf->mbx_info.malicious;
4383 }
4384 
4385 /**
4386  * ice_vc_process_vf_msg - Process request from VF
4387  * @pf: pointer to the PF structure
4388  * @event: pointer to the AQ event
4389  * @mbxdata: information used to detect VF attempting mailbox overflow
4390  *
4391  * Called from the common asq/arq handler to process request from VF. When this
4392  * flow is used for devices with hardware VF to PF message queue overflow
4393  * support (ICE_F_MBX_LIMIT) mbxdata is set to NULL and ice_is_malicious_vf
4394  * check is skipped.
4395  */
4396 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event,
4397 			   struct ice_mbx_data *mbxdata)
4398 {
4399 	u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
4400 	s16 vf_id = le16_to_cpu(event->desc.retval);
4401 	const struct ice_virtchnl_ops *ops;
4402 	u16 msglen = event->msg_len;
4403 	u8 *msg = event->msg_buf;
4404 	struct ice_vf *vf = NULL;
4405 	struct device *dev;
4406 	int err = 0;
4407 
4408 	dev = ice_pf_to_dev(pf);
4409 
4410 	vf = ice_get_vf_by_id(pf, vf_id);
4411 	if (!vf) {
4412 		dev_err(dev, "Unable to locate VF for message from VF ID %d, opcode %d, len %d\n",
4413 			vf_id, v_opcode, msglen);
4414 		return;
4415 	}
4416 
4417 	mutex_lock(&vf->cfg_lock);
4418 
4419 	/* Check if the VF is trying to overflow the mailbox */
4420 	if (mbxdata && ice_is_malicious_vf(vf, mbxdata))
4421 		goto finish;
4422 
4423 	/* Check if VF is disabled. */
4424 	if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
4425 		err = -EPERM;
4426 		goto error_handler;
4427 	}
4428 
4429 	ops = vf->virtchnl_ops;
4430 
4431 	/* Perform basic checks on the msg */
4432 	err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4433 	if (err) {
4434 		if (err == VIRTCHNL_STATUS_ERR_PARAM)
4435 			err = -EPERM;
4436 		else
4437 			err = -EINVAL;
4438 	}
4439 
4440 error_handler:
4441 	if (err) {
4442 		ice_vc_send_msg_to_vf(vf, v_opcode, VIRTCHNL_STATUS_ERR_PARAM,
4443 				      NULL, 0);
4444 		dev_err(dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
4445 			vf_id, v_opcode, msglen, err);
4446 		goto finish;
4447 	}
4448 
4449 	if (!ice_vc_is_opcode_allowed(vf, v_opcode)) {
4450 		ice_vc_send_msg_to_vf(vf, v_opcode,
4451 				      VIRTCHNL_STATUS_ERR_NOT_SUPPORTED, NULL,
4452 				      0);
4453 		goto finish;
4454 	}
4455 
4456 	switch (v_opcode) {
4457 	case VIRTCHNL_OP_VERSION:
4458 		err = ops->get_ver_msg(vf, msg);
4459 		break;
4460 	case VIRTCHNL_OP_GET_VF_RESOURCES:
4461 		err = ops->get_vf_res_msg(vf, msg);
4462 		if (ice_vf_init_vlan_stripping(vf))
4463 			dev_dbg(dev, "Failed to initialize VLAN stripping for VF %d\n",
4464 				vf->vf_id);
4465 		ice_vc_notify_vf_link_state(vf);
4466 		break;
4467 	case VIRTCHNL_OP_RESET_VF:
4468 		ops->reset_vf(vf);
4469 		break;
4470 	case VIRTCHNL_OP_ADD_ETH_ADDR:
4471 		err = ops->add_mac_addr_msg(vf, msg);
4472 		break;
4473 	case VIRTCHNL_OP_DEL_ETH_ADDR:
4474 		err = ops->del_mac_addr_msg(vf, msg);
4475 		break;
4476 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4477 		err = ops->cfg_qs_msg(vf, msg);
4478 		break;
4479 	case VIRTCHNL_OP_ENABLE_QUEUES:
4480 		err = ops->ena_qs_msg(vf, msg);
4481 		ice_vc_notify_vf_link_state(vf);
4482 		break;
4483 	case VIRTCHNL_OP_DISABLE_QUEUES:
4484 		err = ops->dis_qs_msg(vf, msg);
4485 		break;
4486 	case VIRTCHNL_OP_REQUEST_QUEUES:
4487 		err = ops->request_qs_msg(vf, msg);
4488 		break;
4489 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4490 		err = ops->cfg_irq_map_msg(vf, msg);
4491 		break;
4492 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
4493 		err = ops->config_rss_key(vf, msg);
4494 		break;
4495 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
4496 		err = ops->config_rss_lut(vf, msg);
4497 		break;
4498 	case VIRTCHNL_OP_CONFIG_RSS_HFUNC:
4499 		err = ops->config_rss_hfunc(vf, msg);
4500 		break;
4501 	case VIRTCHNL_OP_GET_STATS:
4502 		err = ops->get_stats_msg(vf, msg);
4503 		break;
4504 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4505 		err = ops->cfg_promiscuous_mode_msg(vf, msg);
4506 		break;
4507 	case VIRTCHNL_OP_ADD_VLAN:
4508 		err = ops->add_vlan_msg(vf, msg);
4509 		break;
4510 	case VIRTCHNL_OP_DEL_VLAN:
4511 		err = ops->remove_vlan_msg(vf, msg);
4512 		break;
4513 	case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
4514 		err = ops->query_rxdid(vf);
4515 		break;
4516 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
4517 		err = ops->get_rss_hena(vf);
4518 		break;
4519 	case VIRTCHNL_OP_SET_RSS_HENA:
4520 		err = ops->set_rss_hena_msg(vf, msg);
4521 		break;
4522 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4523 		err = ops->ena_vlan_stripping(vf);
4524 		break;
4525 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4526 		err = ops->dis_vlan_stripping(vf);
4527 		break;
4528 	case VIRTCHNL_OP_ADD_FDIR_FILTER:
4529 		err = ops->add_fdir_fltr_msg(vf, msg);
4530 		break;
4531 	case VIRTCHNL_OP_DEL_FDIR_FILTER:
4532 		err = ops->del_fdir_fltr_msg(vf, msg);
4533 		break;
4534 	case VIRTCHNL_OP_ADD_RSS_CFG:
4535 		err = ops->handle_rss_cfg_msg(vf, msg, true);
4536 		break;
4537 	case VIRTCHNL_OP_DEL_RSS_CFG:
4538 		err = ops->handle_rss_cfg_msg(vf, msg, false);
4539 		break;
4540 	case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
4541 		err = ops->get_offload_vlan_v2_caps(vf);
4542 		break;
4543 	case VIRTCHNL_OP_ADD_VLAN_V2:
4544 		err = ops->add_vlan_v2_msg(vf, msg);
4545 		break;
4546 	case VIRTCHNL_OP_DEL_VLAN_V2:
4547 		err = ops->remove_vlan_v2_msg(vf, msg);
4548 		break;
4549 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2:
4550 		err = ops->ena_vlan_stripping_v2_msg(vf, msg);
4551 		break;
4552 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2:
4553 		err = ops->dis_vlan_stripping_v2_msg(vf, msg);
4554 		break;
4555 	case VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2:
4556 		err = ops->ena_vlan_insertion_v2_msg(vf, msg);
4557 		break;
4558 	case VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2:
4559 		err = ops->dis_vlan_insertion_v2_msg(vf, msg);
4560 		break;
4561 	case VIRTCHNL_OP_GET_QOS_CAPS:
4562 		err = ops->get_qos_caps(vf);
4563 		break;
4564 	case VIRTCHNL_OP_CONFIG_QUEUE_BW:
4565 		err = ops->cfg_q_bw(vf, msg);
4566 		break;
4567 	case VIRTCHNL_OP_CONFIG_QUANTA:
4568 		err = ops->cfg_q_quanta(vf, msg);
4569 		break;
4570 	case VIRTCHNL_OP_1588_PTP_GET_CAPS:
4571 		err = ops->get_ptp_cap(vf, (const void *)msg);
4572 		break;
4573 	case VIRTCHNL_OP_1588_PTP_GET_TIME:
4574 		err = ops->get_phc_time(vf);
4575 		break;
4576 	case VIRTCHNL_OP_UNKNOWN:
4577 	default:
4578 		dev_err(dev, "Unsupported opcode %d from VF %d\n", v_opcode,
4579 			vf_id);
4580 		err = ice_vc_send_msg_to_vf(vf, v_opcode,
4581 					    VIRTCHNL_STATUS_ERR_NOT_SUPPORTED,
4582 					    NULL, 0);
4583 		break;
4584 	}
4585 	if (err) {
4586 		/* Helper function cares less about error return values here
4587 		 * as it is busy with pending work.
4588 		 */
4589 		dev_info(dev, "PF failed to honor VF %d, opcode %d, error %d\n",
4590 			 vf_id, v_opcode, err);
4591 	}
4592 
4593 finish:
4594 	mutex_unlock(&vf->cfg_lock);
4595 	ice_put_vf(vf);
4596 }
4597