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