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