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