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