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