xref: /linux/drivers/net/ethernet/intel/ice/virt/fdir.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021-2023, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_base.h"
6 #include "ice_lib.h"
7 #include "ice_flow.h"
8 #include "ice_vf_lib_private.h"
9 
10 #define to_fltr_conf_from_desc(p) \
11 	container_of(p, struct virtchnl_fdir_fltr_conf, input)
12 
13 #define GTPU_TEID_OFFSET 4
14 #define GTPU_EH_QFI_OFFSET 1
15 #define GTPU_EH_QFI_MASK 0x3F
16 #define PFCP_S_OFFSET 0
17 #define PFCP_S_MASK 0x1
18 #define PFCP_PORT_NR 8805
19 
20 #define FDIR_INSET_FLAG_ESP_S 0
21 #define FDIR_INSET_FLAG_ESP_M BIT_ULL(FDIR_INSET_FLAG_ESP_S)
22 #define FDIR_INSET_FLAG_ESP_UDP BIT_ULL(FDIR_INSET_FLAG_ESP_S)
23 #define FDIR_INSET_FLAG_ESP_IPSEC (0ULL << FDIR_INSET_FLAG_ESP_S)
24 
25 enum ice_fdir_tunnel_type {
26 	ICE_FDIR_TUNNEL_TYPE_NONE = 0,
27 	ICE_FDIR_TUNNEL_TYPE_GTPU,
28 	ICE_FDIR_TUNNEL_TYPE_GTPU_EH,
29 	ICE_FDIR_TUNNEL_TYPE_ECPRI,
30 	ICE_FDIR_TUNNEL_TYPE_GTPU_INNER,
31 	ICE_FDIR_TUNNEL_TYPE_GTPU_EH_INNER,
32 	ICE_FDIR_TUNNEL_TYPE_GRE,
33 	ICE_FDIR_TUNNEL_TYPE_GTPOGRE,
34 	ICE_FDIR_TUNNEL_TYPE_GTPOGRE_INNER,
35 	ICE_FDIR_TUNNEL_TYPE_GRE_INNER,
36 	ICE_FDIR_TUNNEL_TYPE_L2TPV2,
37 	ICE_FDIR_TUNNEL_TYPE_L2TPV2_INNER,
38 };
39 
40 struct virtchnl_fdir_fltr_conf {
41 	struct ice_ntuple_fltr input;
42 	enum ice_fdir_tunnel_type ttype;
43 	u64 inset_flag;
44 	u32 flow_id;
45 
46 	struct ice_parser_profile *prof;
47 	bool parser_ena;
48 	u8 *pkt_buf;
49 	u8 pkt_len;
50 };
51 
52 struct virtchnl_fdir_inset_map {
53 	enum virtchnl_proto_hdr_field field;
54 	enum ice_flow_field fld;
55 	u64 flag;
56 	u64 mask;
57 };
58 
59 static const struct virtchnl_fdir_inset_map fdir_inset_map[] = {
60 	{VIRTCHNL_PROTO_HDR_ETH_ETHERTYPE, ICE_FLOW_FIELD_IDX_ETH_TYPE, 0, 0},
61 	{VIRTCHNL_PROTO_HDR_IPV4_SRC, ICE_FLOW_FIELD_IDX_IPV4_SA, 0, 0},
62 	{VIRTCHNL_PROTO_HDR_IPV4_DST, ICE_FLOW_FIELD_IDX_IPV4_DA, 0, 0},
63 	{VIRTCHNL_PROTO_HDR_IPV4_DSCP, ICE_FLOW_FIELD_IDX_IPV4_DSCP, 0, 0},
64 	{VIRTCHNL_PROTO_HDR_IPV4_TTL, ICE_FLOW_FIELD_IDX_IPV4_TTL, 0, 0},
65 	{VIRTCHNL_PROTO_HDR_IPV4_PROT, ICE_FLOW_FIELD_IDX_IPV4_PROT, 0, 0},
66 	{VIRTCHNL_PROTO_HDR_IPV6_SRC, ICE_FLOW_FIELD_IDX_IPV6_SA, 0, 0},
67 	{VIRTCHNL_PROTO_HDR_IPV6_DST, ICE_FLOW_FIELD_IDX_IPV6_DA, 0, 0},
68 	{VIRTCHNL_PROTO_HDR_IPV6_TC, ICE_FLOW_FIELD_IDX_IPV6_DSCP, 0, 0},
69 	{VIRTCHNL_PROTO_HDR_IPV6_HOP_LIMIT, ICE_FLOW_FIELD_IDX_IPV6_TTL, 0, 0},
70 	{VIRTCHNL_PROTO_HDR_IPV6_PROT, ICE_FLOW_FIELD_IDX_IPV6_PROT, 0, 0},
71 	{VIRTCHNL_PROTO_HDR_UDP_SRC_PORT, ICE_FLOW_FIELD_IDX_UDP_SRC_PORT, 0, 0},
72 	{VIRTCHNL_PROTO_HDR_UDP_DST_PORT, ICE_FLOW_FIELD_IDX_UDP_DST_PORT, 0, 0},
73 	{VIRTCHNL_PROTO_HDR_TCP_SRC_PORT, ICE_FLOW_FIELD_IDX_TCP_SRC_PORT, 0, 0},
74 	{VIRTCHNL_PROTO_HDR_TCP_DST_PORT, ICE_FLOW_FIELD_IDX_TCP_DST_PORT, 0, 0},
75 	{VIRTCHNL_PROTO_HDR_SCTP_SRC_PORT, ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT, 0, 0},
76 	{VIRTCHNL_PROTO_HDR_SCTP_DST_PORT, ICE_FLOW_FIELD_IDX_SCTP_DST_PORT, 0, 0},
77 	{VIRTCHNL_PROTO_HDR_GTPU_IP_TEID, ICE_FLOW_FIELD_IDX_GTPU_IP_TEID, 0, 0},
78 	{VIRTCHNL_PROTO_HDR_GTPU_EH_QFI, ICE_FLOW_FIELD_IDX_GTPU_EH_QFI, 0, 0},
79 	{VIRTCHNL_PROTO_HDR_ESP_SPI, ICE_FLOW_FIELD_IDX_ESP_SPI,
80 		FDIR_INSET_FLAG_ESP_IPSEC, FDIR_INSET_FLAG_ESP_M},
81 	{VIRTCHNL_PROTO_HDR_ESP_SPI, ICE_FLOW_FIELD_IDX_NAT_T_ESP_SPI,
82 		FDIR_INSET_FLAG_ESP_UDP, FDIR_INSET_FLAG_ESP_M},
83 	{VIRTCHNL_PROTO_HDR_AH_SPI, ICE_FLOW_FIELD_IDX_AH_SPI, 0, 0},
84 	{VIRTCHNL_PROTO_HDR_L2TPV3_SESS_ID, ICE_FLOW_FIELD_IDX_L2TPV3_SESS_ID, 0, 0},
85 	{VIRTCHNL_PROTO_HDR_PFCP_S_FIELD, ICE_FLOW_FIELD_IDX_UDP_DST_PORT, 0, 0},
86 };
87 
88 /**
89  * ice_vc_fdir_param_check
90  * @vf: pointer to the VF structure
91  * @vsi_id: VF relative VSI ID
92  *
93  * Check for the valid VSI ID, PF's state and VF's state
94  *
95  * Return: 0 on success, and -EINVAL on error.
96  */
97 static int
98 ice_vc_fdir_param_check(struct ice_vf *vf, u16 vsi_id)
99 {
100 	struct ice_pf *pf = vf->pf;
101 
102 	if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
103 		return -EINVAL;
104 
105 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
106 		return -EINVAL;
107 
108 	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_FDIR_PF))
109 		return -EINVAL;
110 
111 	if (!ice_vc_isvalid_vsi_id(vf, vsi_id))
112 		return -EINVAL;
113 
114 	if (!ice_get_vf_vsi(vf))
115 		return -EINVAL;
116 
117 	return 0;
118 }
119 
120 /**
121  * ice_vf_start_ctrl_vsi
122  * @vf: pointer to the VF structure
123  *
124  * Allocate ctrl_vsi for the first time and open the ctrl_vsi port for VF
125  *
126  * Return: 0 on success, and other on error.
127  */
128 static int ice_vf_start_ctrl_vsi(struct ice_vf *vf)
129 {
130 	struct ice_pf *pf = vf->pf;
131 	struct ice_vsi *ctrl_vsi;
132 	struct device *dev;
133 	int err;
134 
135 	dev = ice_pf_to_dev(pf);
136 	if (vf->ctrl_vsi_idx != ICE_NO_VSI)
137 		return -EEXIST;
138 
139 	ctrl_vsi = ice_vf_ctrl_vsi_setup(vf);
140 	if (!ctrl_vsi) {
141 		dev_dbg(dev, "Could not setup control VSI for VF %d\n",
142 			vf->vf_id);
143 		return -ENOMEM;
144 	}
145 
146 	err = ice_vsi_open_ctrl(ctrl_vsi);
147 	if (err) {
148 		dev_dbg(dev, "Could not open control VSI for VF %d\n",
149 			vf->vf_id);
150 		goto err_vsi_open;
151 	}
152 
153 	return 0;
154 
155 err_vsi_open:
156 	ice_vsi_release(ctrl_vsi);
157 	if (vf->ctrl_vsi_idx != ICE_NO_VSI) {
158 		pf->vsi[vf->ctrl_vsi_idx] = NULL;
159 		vf->ctrl_vsi_idx = ICE_NO_VSI;
160 	}
161 	return err;
162 }
163 
164 /**
165  * ice_vc_fdir_alloc_prof - allocate profile for this filter flow type
166  * @vf: pointer to the VF structure
167  * @flow: filter flow type
168  *
169  * Return: 0 on success, and other on error.
170  */
171 static int
172 ice_vc_fdir_alloc_prof(struct ice_vf *vf, enum ice_fltr_ptype flow)
173 {
174 	struct ice_vf_fdir *fdir = &vf->fdir;
175 
176 	if (!fdir->fdir_prof) {
177 		fdir->fdir_prof = devm_kzalloc_objs(ice_pf_to_dev(vf->pf),
178 						    *fdir->fdir_prof,
179 						    ICE_FLTR_PTYPE_MAX);
180 		if (!fdir->fdir_prof)
181 			return -ENOMEM;
182 	}
183 
184 	if (!fdir->fdir_prof[flow]) {
185 		fdir->fdir_prof[flow] = devm_kzalloc_obj(ice_pf_to_dev(vf->pf),
186 							 **fdir->fdir_prof);
187 		if (!fdir->fdir_prof[flow])
188 			return -ENOMEM;
189 	}
190 
191 	return 0;
192 }
193 
194 /**
195  * ice_vc_fdir_free_prof - free profile for this filter flow type
196  * @vf: pointer to the VF structure
197  * @flow: filter flow type
198  */
199 static void
200 ice_vc_fdir_free_prof(struct ice_vf *vf, enum ice_fltr_ptype flow)
201 {
202 	struct ice_vf_fdir *fdir = &vf->fdir;
203 
204 	if (!fdir->fdir_prof)
205 		return;
206 
207 	if (!fdir->fdir_prof[flow])
208 		return;
209 
210 	devm_kfree(ice_pf_to_dev(vf->pf), fdir->fdir_prof[flow]);
211 	fdir->fdir_prof[flow] = NULL;
212 }
213 
214 /**
215  * ice_vc_fdir_free_prof_all - free all the profile for this VF
216  * @vf: pointer to the VF structure
217  */
218 static void ice_vc_fdir_free_prof_all(struct ice_vf *vf)
219 {
220 	struct ice_vf_fdir *fdir = &vf->fdir;
221 	enum ice_fltr_ptype flow;
222 
223 	if (!fdir->fdir_prof)
224 		return;
225 
226 	for (flow = ICE_FLTR_PTYPE_NONF_NONE; flow < ICE_FLTR_PTYPE_MAX; flow++)
227 		ice_vc_fdir_free_prof(vf, flow);
228 
229 	devm_kfree(ice_pf_to_dev(vf->pf), fdir->fdir_prof);
230 	fdir->fdir_prof = NULL;
231 }
232 
233 /**
234  * ice_vc_fdir_parse_flow_fld
235  * @proto_hdr: virtual channel protocol filter header
236  * @conf: FDIR configuration for each filter
237  * @fld: field type array
238  * @fld_cnt: field counter
239  *
240  * Parse the virtual channel filter header and store them into field type array
241  *
242  * Return: 0 on success, and other on error.
243  */
244 static int
245 ice_vc_fdir_parse_flow_fld(struct virtchnl_proto_hdr *proto_hdr,
246 			   struct virtchnl_fdir_fltr_conf *conf,
247 			   enum ice_flow_field *fld, int *fld_cnt)
248 {
249 	struct virtchnl_proto_hdr hdr;
250 	u32 i;
251 
252 	memcpy(&hdr, proto_hdr, sizeof(hdr));
253 
254 	for (i = 0; (i < ARRAY_SIZE(fdir_inset_map)) &&
255 	     VIRTCHNL_GET_PROTO_HDR_FIELD(&hdr); i++)
256 		if (VIRTCHNL_TEST_PROTO_HDR(&hdr, fdir_inset_map[i].field)) {
257 			if (fdir_inset_map[i].mask &&
258 			    ((fdir_inset_map[i].mask & conf->inset_flag) !=
259 			     fdir_inset_map[i].flag))
260 				continue;
261 
262 			fld[*fld_cnt] = fdir_inset_map[i].fld;
263 			*fld_cnt += 1;
264 			if (*fld_cnt >= ICE_FLOW_FIELD_IDX_MAX)
265 				return -EINVAL;
266 			VIRTCHNL_DEL_PROTO_HDR_FIELD(&hdr,
267 						     fdir_inset_map[i].field);
268 		}
269 
270 	return 0;
271 }
272 
273 /**
274  * ice_vc_fdir_set_flow_fld
275  * @vf: pointer to the VF structure
276  * @fltr: virtual channel add cmd buffer
277  * @conf: FDIR configuration for each filter
278  * @seg: array of one or more packet segments that describe the flow
279  *
280  * Parse the virtual channel add msg buffer's field vector and store them into
281  * flow's packet segment field
282  *
283  * Return: 0 on success, and other on error.
284  */
285 static int
286 ice_vc_fdir_set_flow_fld(struct ice_vf *vf, struct virtchnl_fdir_add *fltr,
287 			 struct virtchnl_fdir_fltr_conf *conf,
288 			 struct ice_flow_seg_info *seg)
289 {
290 	struct virtchnl_fdir_rule *rule = &fltr->rule_cfg;
291 	enum ice_flow_field fld[ICE_FLOW_FIELD_IDX_MAX];
292 	struct device *dev = ice_pf_to_dev(vf->pf);
293 	struct virtchnl_proto_hdrs *proto;
294 	int fld_cnt = 0;
295 	int i;
296 
297 	proto = &rule->proto_hdrs;
298 	for (i = 0; i < proto->count; i++) {
299 		struct virtchnl_proto_hdr *hdr = &proto->proto_hdr[i];
300 		int ret;
301 
302 		ret = ice_vc_fdir_parse_flow_fld(hdr, conf, fld, &fld_cnt);
303 		if (ret)
304 			return ret;
305 	}
306 
307 	if (fld_cnt == 0) {
308 		dev_dbg(dev, "Empty input set for VF %d\n", vf->vf_id);
309 		return -EINVAL;
310 	}
311 
312 	for (i = 0; i < fld_cnt; i++)
313 		ice_flow_set_fld(seg, fld[i],
314 				 ICE_FLOW_FLD_OFF_INVAL,
315 				 ICE_FLOW_FLD_OFF_INVAL,
316 				 ICE_FLOW_FLD_OFF_INVAL, false);
317 
318 	return 0;
319 }
320 
321 /**
322  * ice_vc_fdir_set_flow_hdr - config the flow's packet segment header
323  * @vf: pointer to the VF structure
324  * @conf: FDIR configuration for each filter
325  * @seg: array of one or more packet segments that describe the flow
326  *
327  * Return: 0 on success, and other on error.
328  */
329 static int
330 ice_vc_fdir_set_flow_hdr(struct ice_vf *vf,
331 			 struct virtchnl_fdir_fltr_conf *conf,
332 			 struct ice_flow_seg_info *seg)
333 {
334 	enum ice_fltr_ptype flow = conf->input.flow_type;
335 	enum ice_fdir_tunnel_type ttype = conf->ttype;
336 	struct device *dev = ice_pf_to_dev(vf->pf);
337 
338 	switch (flow) {
339 	case ICE_FLTR_PTYPE_NON_IP_L2:
340 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_ETH_NON_IP);
341 		break;
342 	case ICE_FLTR_PTYPE_NONF_IPV4_L2TPV3:
343 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_L2TPV3 |
344 				  ICE_FLOW_SEG_HDR_IPV4 |
345 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
346 		break;
347 	case ICE_FLTR_PTYPE_NONF_IPV4_ESP:
348 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_ESP |
349 				  ICE_FLOW_SEG_HDR_IPV4 |
350 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
351 		break;
352 	case ICE_FLTR_PTYPE_NONF_IPV4_AH:
353 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_AH |
354 				  ICE_FLOW_SEG_HDR_IPV4 |
355 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
356 		break;
357 	case ICE_FLTR_PTYPE_NONF_IPV4_NAT_T_ESP:
358 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_NAT_T_ESP |
359 				  ICE_FLOW_SEG_HDR_IPV4 |
360 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
361 		break;
362 	case ICE_FLTR_PTYPE_NONF_IPV4_PFCP_NODE:
363 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_PFCP_NODE |
364 				  ICE_FLOW_SEG_HDR_IPV4 |
365 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
366 		break;
367 	case ICE_FLTR_PTYPE_NONF_IPV4_PFCP_SESSION:
368 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_PFCP_SESSION |
369 				  ICE_FLOW_SEG_HDR_IPV4 |
370 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
371 		break;
372 	case ICE_FLTR_PTYPE_NONF_IPV4_OTHER:
373 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_IPV4 |
374 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
375 		break;
376 	case ICE_FLTR_PTYPE_NONF_IPV4_TCP:
377 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_TCP |
378 				  ICE_FLOW_SEG_HDR_IPV4 |
379 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
380 		break;
381 	case ICE_FLTR_PTYPE_NONF_IPV4_UDP:
382 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_UDP |
383 				  ICE_FLOW_SEG_HDR_IPV4 |
384 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
385 		break;
386 	case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_UDP:
387 	case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_TCP:
388 	case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_ICMP:
389 	case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_OTHER:
390 		if (ttype == ICE_FDIR_TUNNEL_TYPE_GTPU) {
391 			ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_GTPU_IP |
392 					  ICE_FLOW_SEG_HDR_IPV4 |
393 					  ICE_FLOW_SEG_HDR_IPV_OTHER);
394 		} else if (ttype == ICE_FDIR_TUNNEL_TYPE_GTPU_EH) {
395 			ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_GTPU_EH |
396 					  ICE_FLOW_SEG_HDR_GTPU_IP |
397 					  ICE_FLOW_SEG_HDR_IPV4 |
398 					  ICE_FLOW_SEG_HDR_IPV_OTHER);
399 		} else {
400 			dev_dbg(dev, "Invalid tunnel type 0x%x for VF %d\n",
401 				flow, vf->vf_id);
402 			return -EINVAL;
403 		}
404 		break;
405 	case ICE_FLTR_PTYPE_NONF_IPV4_SCTP:
406 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_SCTP |
407 				  ICE_FLOW_SEG_HDR_IPV4 |
408 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
409 		break;
410 	case ICE_FLTR_PTYPE_NONF_IPV6_L2TPV3:
411 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_L2TPV3 |
412 				  ICE_FLOW_SEG_HDR_IPV6 |
413 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
414 		break;
415 	case ICE_FLTR_PTYPE_NONF_IPV6_ESP:
416 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_ESP |
417 				  ICE_FLOW_SEG_HDR_IPV6 |
418 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
419 		break;
420 	case ICE_FLTR_PTYPE_NONF_IPV6_AH:
421 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_AH |
422 				  ICE_FLOW_SEG_HDR_IPV6 |
423 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
424 		break;
425 	case ICE_FLTR_PTYPE_NONF_IPV6_NAT_T_ESP:
426 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_NAT_T_ESP |
427 				  ICE_FLOW_SEG_HDR_IPV6 |
428 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
429 		break;
430 	case ICE_FLTR_PTYPE_NONF_IPV6_PFCP_NODE:
431 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_PFCP_NODE |
432 				  ICE_FLOW_SEG_HDR_IPV6 |
433 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
434 		break;
435 	case ICE_FLTR_PTYPE_NONF_IPV6_PFCP_SESSION:
436 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_PFCP_SESSION |
437 				  ICE_FLOW_SEG_HDR_IPV6 |
438 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
439 		break;
440 	case ICE_FLTR_PTYPE_NONF_IPV6_OTHER:
441 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_IPV6 |
442 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
443 		break;
444 	case ICE_FLTR_PTYPE_NONF_IPV6_TCP:
445 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_TCP |
446 				  ICE_FLOW_SEG_HDR_IPV6 |
447 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
448 		break;
449 	case ICE_FLTR_PTYPE_NONF_IPV6_UDP:
450 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_UDP |
451 				  ICE_FLOW_SEG_HDR_IPV6 |
452 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
453 		break;
454 	case ICE_FLTR_PTYPE_NONF_IPV6_SCTP:
455 		ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_SCTP |
456 				  ICE_FLOW_SEG_HDR_IPV6 |
457 				  ICE_FLOW_SEG_HDR_IPV_OTHER);
458 		break;
459 	default:
460 		dev_dbg(dev, "Invalid flow type 0x%x for VF %d failed\n",
461 			flow, vf->vf_id);
462 		return -EINVAL;
463 	}
464 
465 	return 0;
466 }
467 
468 /**
469  * ice_vc_fdir_rem_prof - remove profile for this filter flow type
470  * @vf: pointer to the VF structure
471  * @flow: filter flow type
472  * @tun: 0 implies non-tunnel type filter, 1 implies tunnel type filter
473  */
474 static void
475 ice_vc_fdir_rem_prof(struct ice_vf *vf, enum ice_fltr_ptype flow, int tun)
476 {
477 	struct ice_vf_fdir *fdir = &vf->fdir;
478 	struct ice_fd_hw_prof *vf_prof;
479 	struct ice_pf *pf = vf->pf;
480 	struct ice_vsi *vf_vsi;
481 	struct device *dev;
482 	struct ice_hw *hw;
483 	u64 prof_id;
484 	int i;
485 
486 	dev = ice_pf_to_dev(pf);
487 	hw = &pf->hw;
488 	if (!fdir->fdir_prof || !fdir->fdir_prof[flow])
489 		return;
490 
491 	vf_prof = fdir->fdir_prof[flow];
492 	prof_id = vf_prof->prof_id[tun];
493 
494 	vf_vsi = ice_get_vf_vsi(vf);
495 	if (!vf_vsi) {
496 		dev_dbg(dev, "NULL vf %d vsi pointer\n", vf->vf_id);
497 		return;
498 	}
499 
500 	if (!fdir->prof_entry_cnt[flow][tun])
501 		return;
502 
503 	for (i = 0; i < fdir->prof_entry_cnt[flow][tun]; i++)
504 		if (vf_prof->entry_h[i][tun]) {
505 			u16 vsi_num = ice_get_hw_vsi_num(hw, vf_prof->vsi_h[i]);
506 
507 			ice_rem_prof_id_flow(hw, ICE_BLK_FD, vsi_num, prof_id);
508 			ice_flow_rem_entry(hw, ICE_BLK_FD,
509 					   vf_prof->entry_h[i][tun]);
510 			vf_prof->entry_h[i][tun] = 0;
511 		}
512 
513 	ice_flow_rem_prof(hw, ICE_BLK_FD, prof_id);
514 	devm_kfree(dev, vf_prof->fdir_seg[tun]);
515 	vf_prof->fdir_seg[tun] = NULL;
516 
517 	for (i = 0; i < vf_prof->cnt; i++)
518 		vf_prof->vsi_h[i] = 0;
519 
520 	fdir->prof_entry_cnt[flow][tun] = 0;
521 }
522 
523 /**
524  * ice_vc_fdir_rem_prof_all - remove profile for this VF
525  * @vf: pointer to the VF structure
526  */
527 static void ice_vc_fdir_rem_prof_all(struct ice_vf *vf)
528 {
529 	enum ice_fltr_ptype flow;
530 
531 	for (flow = ICE_FLTR_PTYPE_NONF_NONE;
532 	     flow < ICE_FLTR_PTYPE_MAX; flow++) {
533 		ice_vc_fdir_rem_prof(vf, flow, 0);
534 		ice_vc_fdir_rem_prof(vf, flow, 1);
535 	}
536 }
537 
538 /**
539  * ice_vc_fdir_reset_cnt_all - reset all FDIR counters for this VF FDIR
540  * @fdir: pointer to the VF FDIR structure
541  */
542 static void ice_vc_fdir_reset_cnt_all(struct ice_vf_fdir *fdir)
543 {
544 	enum ice_fltr_ptype flow;
545 
546 	for (flow = ICE_FLTR_PTYPE_NONF_NONE;
547 	     flow < ICE_FLTR_PTYPE_MAX; flow++) {
548 		fdir->fdir_fltr_cnt[flow][0] = 0;
549 		fdir->fdir_fltr_cnt[flow][1] = 0;
550 	}
551 
552 	fdir->fdir_fltr_cnt_total = 0;
553 }
554 
555 /**
556  * ice_vc_fdir_has_prof_conflict
557  * @vf: pointer to the VF structure
558  * @conf: FDIR configuration for each filter
559  *
560  * Check if @conf has conflicting profile with existing profiles
561  *
562  * Return: true on success, and false on error.
563  */
564 static bool
565 ice_vc_fdir_has_prof_conflict(struct ice_vf *vf,
566 			      struct virtchnl_fdir_fltr_conf *conf)
567 {
568 	struct ice_ntuple_fltr *desc;
569 
570 	list_for_each_entry(desc, &vf->fdir.fdir_rule_list, fltr_node) {
571 		struct virtchnl_fdir_fltr_conf *existing_conf;
572 		enum ice_fltr_ptype flow_type_a, flow_type_b;
573 		struct ice_ntuple_fltr *a, *b;
574 
575 		existing_conf = to_fltr_conf_from_desc(desc);
576 		a = &existing_conf->input;
577 		b = &conf->input;
578 		flow_type_a = a->flow_type;
579 		flow_type_b = b->flow_type;
580 
581 		/* No need to compare two rules with different tunnel types or
582 		 * with the same protocol type.
583 		 */
584 		if (existing_conf->ttype != conf->ttype ||
585 		    flow_type_a == flow_type_b)
586 			continue;
587 
588 		switch (flow_type_a) {
589 		case ICE_FLTR_PTYPE_NONF_IPV4_UDP:
590 		case ICE_FLTR_PTYPE_NONF_IPV4_TCP:
591 		case ICE_FLTR_PTYPE_NONF_IPV4_SCTP:
592 			if (flow_type_b == ICE_FLTR_PTYPE_NONF_IPV4_OTHER)
593 				return true;
594 			break;
595 		case ICE_FLTR_PTYPE_NONF_IPV4_OTHER:
596 			if (flow_type_b == ICE_FLTR_PTYPE_NONF_IPV4_UDP ||
597 			    flow_type_b == ICE_FLTR_PTYPE_NONF_IPV4_TCP ||
598 			    flow_type_b == ICE_FLTR_PTYPE_NONF_IPV4_SCTP)
599 				return true;
600 			break;
601 		case ICE_FLTR_PTYPE_NONF_IPV6_UDP:
602 		case ICE_FLTR_PTYPE_NONF_IPV6_TCP:
603 		case ICE_FLTR_PTYPE_NONF_IPV6_SCTP:
604 			if (flow_type_b == ICE_FLTR_PTYPE_NONF_IPV6_OTHER)
605 				return true;
606 			break;
607 		case ICE_FLTR_PTYPE_NONF_IPV6_OTHER:
608 			if (flow_type_b == ICE_FLTR_PTYPE_NONF_IPV6_UDP ||
609 			    flow_type_b == ICE_FLTR_PTYPE_NONF_IPV6_TCP ||
610 			    flow_type_b == ICE_FLTR_PTYPE_NONF_IPV6_SCTP)
611 				return true;
612 			break;
613 		default:
614 			break;
615 		}
616 	}
617 
618 	return false;
619 }
620 
621 /**
622  * ice_vc_fdir_write_flow_prof
623  * @vf: pointer to the VF structure
624  * @flow: filter flow type
625  * @seg: array of one or more packet segments that describe the flow
626  * @tun: 0 implies non-tunnel type filter, 1 implies tunnel type filter
627  *
628  * Write the flow's profile config and packet segment into the hardware
629  *
630  * Return: 0 on success, and other on error.
631  */
632 static int
633 ice_vc_fdir_write_flow_prof(struct ice_vf *vf, enum ice_fltr_ptype flow,
634 			    struct ice_flow_seg_info *seg, int tun)
635 {
636 	struct ice_vf_fdir *fdir = &vf->fdir;
637 	struct ice_vsi *vf_vsi, *ctrl_vsi;
638 	struct ice_flow_seg_info *old_seg;
639 	struct ice_flow_prof *prof = NULL;
640 	struct ice_fd_hw_prof *vf_prof;
641 	struct device *dev;
642 	struct ice_pf *pf;
643 	struct ice_hw *hw;
644 	u64 entry1_h = 0;
645 	u64 entry2_h = 0;
646 	int ret;
647 
648 	pf = vf->pf;
649 	dev = ice_pf_to_dev(pf);
650 	hw = &pf->hw;
651 	vf_vsi = ice_get_vf_vsi(vf);
652 	if (!vf_vsi)
653 		return -EINVAL;
654 
655 	ctrl_vsi = pf->vsi[vf->ctrl_vsi_idx];
656 	if (!ctrl_vsi)
657 		return -EINVAL;
658 
659 	vf_prof = fdir->fdir_prof[flow];
660 	old_seg = vf_prof->fdir_seg[tun];
661 	if (old_seg) {
662 		if (!memcmp(old_seg, seg, sizeof(*seg))) {
663 			dev_dbg(dev, "Duplicated profile for VF %d!\n",
664 				vf->vf_id);
665 			return -EEXIST;
666 		}
667 
668 		if (fdir->fdir_fltr_cnt[flow][tun]) {
669 			ret = -EINVAL;
670 			dev_dbg(dev, "Input set conflicts for VF %d\n",
671 				vf->vf_id);
672 			goto err_exit;
673 		}
674 
675 		/* remove previously allocated profile */
676 		ice_vc_fdir_rem_prof(vf, flow, tun);
677 	}
678 
679 	ret = ice_flow_add_prof(hw, ICE_BLK_FD, ICE_FLOW_RX, seg,
680 				tun + 1, false, &prof);
681 	if (ret) {
682 		dev_dbg(dev, "Could not add VSI flow 0x%x for VF %d\n",
683 			flow, vf->vf_id);
684 		goto err_exit;
685 	}
686 
687 	ret = ice_flow_add_entry(hw, ICE_BLK_FD, prof->id, vf_vsi->idx,
688 				 vf_vsi->idx, ICE_FLOW_PRIO_NORMAL,
689 				 seg, &entry1_h);
690 	if (ret) {
691 		dev_dbg(dev, "Could not add flow 0x%x VSI entry for VF %d\n",
692 			flow, vf->vf_id);
693 		goto err_prof;
694 	}
695 
696 	ret = ice_flow_add_entry(hw, ICE_BLK_FD, prof->id, vf_vsi->idx,
697 				 ctrl_vsi->idx, ICE_FLOW_PRIO_NORMAL,
698 				 seg, &entry2_h);
699 	if (ret) {
700 		dev_dbg(dev,
701 			"Could not add flow 0x%x Ctrl VSI entry for VF %d\n",
702 			flow, vf->vf_id);
703 		goto err_entry_1;
704 	}
705 
706 	vf_prof->fdir_seg[tun] = seg;
707 	vf_prof->cnt = 0;
708 	fdir->prof_entry_cnt[flow][tun] = 0;
709 
710 	vf_prof->entry_h[vf_prof->cnt][tun] = entry1_h;
711 	vf_prof->vsi_h[vf_prof->cnt] = vf_vsi->idx;
712 	vf_prof->cnt++;
713 	fdir->prof_entry_cnt[flow][tun]++;
714 
715 	vf_prof->entry_h[vf_prof->cnt][tun] = entry2_h;
716 	vf_prof->vsi_h[vf_prof->cnt] = ctrl_vsi->idx;
717 	vf_prof->cnt++;
718 	fdir->prof_entry_cnt[flow][tun]++;
719 
720 	vf_prof->prof_id[tun] = prof->id;
721 
722 	return 0;
723 
724 err_entry_1:
725 	ice_rem_prof_id_flow(hw, ICE_BLK_FD,
726 			     ice_get_hw_vsi_num(hw, vf_vsi->idx), prof->id);
727 	ice_flow_rem_entry(hw, ICE_BLK_FD, entry1_h);
728 err_prof:
729 	ice_flow_rem_prof(hw, ICE_BLK_FD, prof->id);
730 err_exit:
731 	return ret;
732 }
733 
734 /**
735  * ice_vc_fdir_config_input_set
736  * @vf: pointer to the VF structure
737  * @fltr: virtual channel add cmd buffer
738  * @conf: FDIR configuration for each filter
739  * @tun: 0 implies non-tunnel type filter, 1 implies tunnel type filter
740  *
741  * Config the input set type and value for virtual channel add msg buffer
742  *
743  * Return: 0 on success, and other on error.
744  */
745 static int
746 ice_vc_fdir_config_input_set(struct ice_vf *vf, struct virtchnl_fdir_add *fltr,
747 			     struct virtchnl_fdir_fltr_conf *conf, int tun)
748 {
749 	struct ice_ntuple_fltr *input = &conf->input;
750 	struct device *dev = ice_pf_to_dev(vf->pf);
751 	struct ice_flow_seg_info *seg;
752 	enum ice_fltr_ptype flow;
753 	int ret;
754 
755 	ret = ice_vc_fdir_has_prof_conflict(vf, conf);
756 	if (ret) {
757 		dev_dbg(dev, "Found flow profile conflict for VF %d\n",
758 			vf->vf_id);
759 		return ret;
760 	}
761 
762 	flow = input->flow_type;
763 	ret = ice_vc_fdir_alloc_prof(vf, flow);
764 	if (ret) {
765 		dev_dbg(dev, "Alloc flow prof for VF %d failed\n", vf->vf_id);
766 		return ret;
767 	}
768 
769 	seg = devm_kzalloc_obj(dev, *seg);
770 	if (!seg)
771 		return -ENOMEM;
772 
773 	ret = ice_vc_fdir_set_flow_fld(vf, fltr, conf, seg);
774 	if (ret) {
775 		dev_dbg(dev, "Set flow field for VF %d failed\n", vf->vf_id);
776 		goto err_exit;
777 	}
778 
779 	ret = ice_vc_fdir_set_flow_hdr(vf, conf, seg);
780 	if (ret) {
781 		dev_dbg(dev, "Set flow hdr for VF %d failed\n", vf->vf_id);
782 		goto err_exit;
783 	}
784 
785 	ret = ice_vc_fdir_write_flow_prof(vf, flow, seg, tun);
786 	if (ret == -EEXIST) {
787 		devm_kfree(dev, seg);
788 	} else if (ret) {
789 		dev_dbg(dev, "Write flow profile for VF %d failed\n",
790 			vf->vf_id);
791 		goto err_exit;
792 	}
793 
794 	return 0;
795 
796 err_exit:
797 	devm_kfree(dev, seg);
798 	return ret;
799 }
800 
801 /**
802  * ice_vc_fdir_is_raw_flow - check if FDIR flow is raw (binary)
803  * @proto: virtchnl protocol headers
804  *
805  * Check if the FDIR rule is raw flow (protocol agnostic flow) or not. Note
806  * that common FDIR rule must have non-zero proto->count. Thus, we choose the
807  * tunnel_level and count of proto as the indicators. If both tunnel_level and
808  * count of proto are zero, this FDIR rule will be regarded as raw flow.
809  *
810  * Returns: true if headers describe raw flow, false otherwise.
811  */
812 static bool
813 ice_vc_fdir_is_raw_flow(struct virtchnl_proto_hdrs *proto)
814 {
815 	return (proto->tunnel_level == 0 && proto->count == 0);
816 }
817 
818 /**
819  * ice_vc_fdir_parse_raw - parse a virtchnl raw FDIR rule
820  * @vf: pointer to the VF info
821  * @proto: virtchnl protocol headers
822  * @conf: FDIR configuration for each filter
823  *
824  * Parse the virtual channel filter's raw flow and store it in @conf
825  *
826  * Return: 0 on success or negative errno on failure.
827  */
828 static int
829 ice_vc_fdir_parse_raw(struct ice_vf *vf,
830 		      struct virtchnl_proto_hdrs *proto,
831 		      struct virtchnl_fdir_fltr_conf *conf)
832 {
833 	u8 *pkt_buf, *msk_buf __free(kfree) = NULL;
834 	struct ice_parser_result rslt;
835 	struct ice_pf *pf = vf->pf;
836 	u16 pkt_len, udp_port = 0;
837 	struct ice_parser *psr;
838 	int status = -ENOMEM;
839 	struct ice_hw *hw;
840 
841 	pkt_len = proto->raw.pkt_len;
842 
843 	if (!pkt_len || pkt_len > VIRTCHNL_MAX_SIZE_RAW_PACKET)
844 		return -EINVAL;
845 
846 	pkt_buf = kzalloc(pkt_len, GFP_KERNEL);
847 	msk_buf = kzalloc(pkt_len, GFP_KERNEL);
848 
849 	if (!pkt_buf || !msk_buf)
850 		goto err_mem_alloc;
851 
852 	memcpy(pkt_buf, proto->raw.spec, pkt_len);
853 	memcpy(msk_buf, proto->raw.mask, pkt_len);
854 
855 	hw = &pf->hw;
856 
857 	/* Get raw profile info via Parser Lib */
858 	psr = ice_parser_create(hw);
859 	if (IS_ERR(psr)) {
860 		status = PTR_ERR(psr);
861 		goto err_mem_alloc;
862 	}
863 
864 	ice_parser_dvm_set(psr, ice_is_dvm_ena(hw));
865 
866 	if (ice_get_open_tunnel_port(hw, &udp_port, TNL_VXLAN))
867 		ice_parser_vxlan_tunnel_set(psr, udp_port, true);
868 
869 	status = ice_parser_run(psr, pkt_buf, pkt_len, &rslt);
870 	if (status)
871 		goto err_parser_destroy;
872 
873 	if (hw->debug_mask & ICE_DBG_PARSER)
874 		ice_parser_result_dump(hw, &rslt);
875 
876 	conf->prof = kzalloc_obj(*conf->prof);
877 	if (!conf->prof) {
878 		status = -ENOMEM;
879 		goto err_parser_destroy;
880 	}
881 
882 	status = ice_parser_profile_init(&rslt, pkt_buf, msk_buf,
883 					 pkt_len, ICE_BLK_FD,
884 					 conf->prof);
885 	if (status)
886 		goto err_parser_profile_init;
887 
888 	if (hw->debug_mask & ICE_DBG_PARSER)
889 		ice_parser_profile_dump(hw, conf->prof);
890 
891 	/* Store raw flow info into @conf */
892 	conf->pkt_len = pkt_len;
893 	conf->pkt_buf = pkt_buf;
894 	conf->parser_ena = true;
895 
896 	ice_parser_destroy(psr);
897 	return 0;
898 
899 err_parser_profile_init:
900 	kfree(conf->prof);
901 err_parser_destroy:
902 	ice_parser_destroy(psr);
903 err_mem_alloc:
904 	kfree(pkt_buf);
905 	return status;
906 }
907 
908 /**
909  * ice_vc_fdir_parse_pattern
910  * @vf: pointer to the VF info
911  * @fltr: virtual channel add cmd buffer
912  * @conf: FDIR configuration for each filter
913  *
914  * Parse the virtual channel filter's pattern and store them into conf
915  *
916  * Return: 0 on success, and other on error.
917  */
918 static int
919 ice_vc_fdir_parse_pattern(struct ice_vf *vf, struct virtchnl_fdir_add *fltr,
920 			  struct virtchnl_fdir_fltr_conf *conf)
921 {
922 	struct virtchnl_proto_hdrs *proto = &fltr->rule_cfg.proto_hdrs;
923 	enum virtchnl_proto_hdr_type l3 = VIRTCHNL_PROTO_HDR_NONE;
924 	enum virtchnl_proto_hdr_type l4 = VIRTCHNL_PROTO_HDR_NONE;
925 	struct ice_ntuple_fltr *input = &conf->input;
926 	struct device *dev = ice_pf_to_dev(vf->pf);
927 	int i;
928 
929 	if (proto->count > VIRTCHNL_MAX_NUM_PROTO_HDRS) {
930 		dev_dbg(dev, "Invalid protocol count:0x%x for VF %d\n",
931 			proto->count, vf->vf_id);
932 		return -EINVAL;
933 	}
934 
935 	/* For raw FDIR filters created by the parser */
936 	if (ice_vc_fdir_is_raw_flow(proto))
937 		return ice_vc_fdir_parse_raw(vf, proto, conf);
938 
939 	for (i = 0; i < proto->count; i++) {
940 		struct virtchnl_proto_hdr *hdr = &proto->proto_hdr[i];
941 		struct ip_esp_hdr *esph;
942 		struct ip_auth_hdr *ah;
943 		struct sctphdr *sctph;
944 		struct ipv6hdr *ip6h;
945 		struct udphdr *udph;
946 		struct tcphdr *tcph;
947 		struct ethhdr *eth;
948 		struct iphdr *iph;
949 		u8 s_field;
950 		u8 *rawh;
951 
952 		switch (hdr->type) {
953 		case VIRTCHNL_PROTO_HDR_ETH:
954 			eth = (struct ethhdr *)hdr->buffer;
955 			input->flow_type = ICE_FLTR_PTYPE_NON_IP_L2;
956 
957 			if (hdr->field_selector)
958 				input->ext_data.ether_type = eth->h_proto;
959 			break;
960 		case VIRTCHNL_PROTO_HDR_IPV4:
961 			iph = (struct iphdr *)hdr->buffer;
962 			l3 = VIRTCHNL_PROTO_HDR_IPV4;
963 			input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_OTHER;
964 
965 			if (hdr->field_selector) {
966 				input->ip.v4.src_ip = iph->saddr;
967 				input->ip.v4.dst_ip = iph->daddr;
968 				input->ip.v4.tos = iph->tos;
969 				input->ip.v4.proto = iph->protocol;
970 			}
971 			break;
972 		case VIRTCHNL_PROTO_HDR_IPV6:
973 			ip6h = (struct ipv6hdr *)hdr->buffer;
974 			l3 = VIRTCHNL_PROTO_HDR_IPV6;
975 			input->flow_type = ICE_FLTR_PTYPE_NONF_IPV6_OTHER;
976 
977 			if (hdr->field_selector) {
978 				memcpy(input->ip.v6.src_ip,
979 				       ip6h->saddr.in6_u.u6_addr8,
980 				       sizeof(ip6h->saddr));
981 				memcpy(input->ip.v6.dst_ip,
982 				       ip6h->daddr.in6_u.u6_addr8,
983 				       sizeof(ip6h->daddr));
984 				input->ip.v6.tc = ((u8)(ip6h->priority) << 4) |
985 						  (ip6h->flow_lbl[0] >> 4);
986 				input->ip.v6.proto = ip6h->nexthdr;
987 			}
988 			break;
989 		case VIRTCHNL_PROTO_HDR_TCP:
990 			tcph = (struct tcphdr *)hdr->buffer;
991 			if (l3 == VIRTCHNL_PROTO_HDR_IPV4)
992 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_TCP;
993 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV6)
994 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV6_TCP;
995 
996 			if (hdr->field_selector) {
997 				if (l3 == VIRTCHNL_PROTO_HDR_IPV4) {
998 					input->ip.v4.src_port = tcph->source;
999 					input->ip.v4.dst_port = tcph->dest;
1000 				} else if (l3 == VIRTCHNL_PROTO_HDR_IPV6) {
1001 					input->ip.v6.src_port = tcph->source;
1002 					input->ip.v6.dst_port = tcph->dest;
1003 				}
1004 			}
1005 			break;
1006 		case VIRTCHNL_PROTO_HDR_UDP:
1007 			udph = (struct udphdr *)hdr->buffer;
1008 			if (l3 == VIRTCHNL_PROTO_HDR_IPV4)
1009 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_UDP;
1010 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV6)
1011 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV6_UDP;
1012 
1013 			if (hdr->field_selector) {
1014 				if (l3 == VIRTCHNL_PROTO_HDR_IPV4) {
1015 					input->ip.v4.src_port = udph->source;
1016 					input->ip.v4.dst_port = udph->dest;
1017 				} else if (l3 == VIRTCHNL_PROTO_HDR_IPV6) {
1018 					input->ip.v6.src_port = udph->source;
1019 					input->ip.v6.dst_port = udph->dest;
1020 				}
1021 			}
1022 			break;
1023 		case VIRTCHNL_PROTO_HDR_SCTP:
1024 			sctph = (struct sctphdr *)hdr->buffer;
1025 			if (l3 == VIRTCHNL_PROTO_HDR_IPV4)
1026 				input->flow_type =
1027 					ICE_FLTR_PTYPE_NONF_IPV4_SCTP;
1028 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV6)
1029 				input->flow_type =
1030 					ICE_FLTR_PTYPE_NONF_IPV6_SCTP;
1031 
1032 			if (hdr->field_selector) {
1033 				if (l3 == VIRTCHNL_PROTO_HDR_IPV4) {
1034 					input->ip.v4.src_port = sctph->source;
1035 					input->ip.v4.dst_port = sctph->dest;
1036 				} else if (l3 == VIRTCHNL_PROTO_HDR_IPV6) {
1037 					input->ip.v6.src_port = sctph->source;
1038 					input->ip.v6.dst_port = sctph->dest;
1039 				}
1040 			}
1041 			break;
1042 		case VIRTCHNL_PROTO_HDR_L2TPV3:
1043 			if (l3 == VIRTCHNL_PROTO_HDR_IPV4)
1044 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_L2TPV3;
1045 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV6)
1046 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV6_L2TPV3;
1047 
1048 			if (hdr->field_selector)
1049 				input->l2tpv3_data.session_id = *((__be32 *)hdr->buffer);
1050 			break;
1051 		case VIRTCHNL_PROTO_HDR_ESP:
1052 			esph = (struct ip_esp_hdr *)hdr->buffer;
1053 			if (l3 == VIRTCHNL_PROTO_HDR_IPV4 &&
1054 			    l4 == VIRTCHNL_PROTO_HDR_UDP)
1055 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_NAT_T_ESP;
1056 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV6 &&
1057 				 l4 == VIRTCHNL_PROTO_HDR_UDP)
1058 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV6_NAT_T_ESP;
1059 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV4 &&
1060 				 l4 == VIRTCHNL_PROTO_HDR_NONE)
1061 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_ESP;
1062 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV6 &&
1063 				 l4 == VIRTCHNL_PROTO_HDR_NONE)
1064 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV6_ESP;
1065 
1066 			if (l4 == VIRTCHNL_PROTO_HDR_UDP)
1067 				conf->inset_flag |= FDIR_INSET_FLAG_ESP_UDP;
1068 			else
1069 				conf->inset_flag |= FDIR_INSET_FLAG_ESP_IPSEC;
1070 
1071 			if (hdr->field_selector) {
1072 				if (l3 == VIRTCHNL_PROTO_HDR_IPV4)
1073 					input->ip.v4.sec_parm_idx = esph->spi;
1074 				else if (l3 == VIRTCHNL_PROTO_HDR_IPV6)
1075 					input->ip.v6.sec_parm_idx = esph->spi;
1076 			}
1077 			break;
1078 		case VIRTCHNL_PROTO_HDR_AH:
1079 			ah = (struct ip_auth_hdr *)hdr->buffer;
1080 			if (l3 == VIRTCHNL_PROTO_HDR_IPV4)
1081 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_AH;
1082 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV6)
1083 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV6_AH;
1084 
1085 			if (hdr->field_selector) {
1086 				if (l3 == VIRTCHNL_PROTO_HDR_IPV4)
1087 					input->ip.v4.sec_parm_idx = ah->spi;
1088 				else if (l3 == VIRTCHNL_PROTO_HDR_IPV6)
1089 					input->ip.v6.sec_parm_idx = ah->spi;
1090 			}
1091 			break;
1092 		case VIRTCHNL_PROTO_HDR_PFCP:
1093 			rawh = (u8 *)hdr->buffer;
1094 			s_field = (rawh[0] >> PFCP_S_OFFSET) & PFCP_S_MASK;
1095 			if (l3 == VIRTCHNL_PROTO_HDR_IPV4 && s_field == 0)
1096 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_PFCP_NODE;
1097 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV4 && s_field == 1)
1098 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_PFCP_SESSION;
1099 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV6 && s_field == 0)
1100 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV6_PFCP_NODE;
1101 			else if (l3 == VIRTCHNL_PROTO_HDR_IPV6 && s_field == 1)
1102 				input->flow_type = ICE_FLTR_PTYPE_NONF_IPV6_PFCP_SESSION;
1103 
1104 			if (hdr->field_selector) {
1105 				if (l3 == VIRTCHNL_PROTO_HDR_IPV4)
1106 					input->ip.v4.dst_port = cpu_to_be16(PFCP_PORT_NR);
1107 				else if (l3 == VIRTCHNL_PROTO_HDR_IPV6)
1108 					input->ip.v6.dst_port = cpu_to_be16(PFCP_PORT_NR);
1109 			}
1110 			break;
1111 		case VIRTCHNL_PROTO_HDR_GTPU_IP:
1112 			rawh = (u8 *)hdr->buffer;
1113 			input->flow_type = ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_OTHER;
1114 
1115 			if (hdr->field_selector)
1116 				input->gtpu_data.teid = *(__be32 *)(&rawh[GTPU_TEID_OFFSET]);
1117 			conf->ttype = ICE_FDIR_TUNNEL_TYPE_GTPU;
1118 			break;
1119 		case VIRTCHNL_PROTO_HDR_GTPU_EH:
1120 			rawh = (u8 *)hdr->buffer;
1121 
1122 			if (hdr->field_selector)
1123 				input->gtpu_data.qfi = rawh[GTPU_EH_QFI_OFFSET] & GTPU_EH_QFI_MASK;
1124 			conf->ttype = ICE_FDIR_TUNNEL_TYPE_GTPU_EH;
1125 			break;
1126 		default:
1127 			dev_dbg(dev, "Invalid header type 0x:%x for VF %d\n",
1128 				hdr->type, vf->vf_id);
1129 			return -EINVAL;
1130 		}
1131 	}
1132 
1133 	return 0;
1134 }
1135 
1136 /**
1137  * ice_vc_fdir_parse_action
1138  * @vf: pointer to the VF info
1139  * @fltr: virtual channel add cmd buffer
1140  * @conf: FDIR configuration for each filter
1141  *
1142  * Parse the virtual channel filter's action and store them into conf
1143  *
1144  * Return: 0 on success, and other on error.
1145  */
1146 static int
1147 ice_vc_fdir_parse_action(struct ice_vf *vf, struct virtchnl_fdir_add *fltr,
1148 			 struct virtchnl_fdir_fltr_conf *conf)
1149 {
1150 	struct virtchnl_filter_action_set *as = &fltr->rule_cfg.action_set;
1151 	struct ice_ntuple_fltr *input = &conf->input;
1152 	struct device *dev = ice_pf_to_dev(vf->pf);
1153 	u32 dest_num = 0;
1154 	u32 mark_num = 0;
1155 	int i;
1156 
1157 	if (as->count > VIRTCHNL_MAX_NUM_ACTIONS) {
1158 		dev_dbg(dev, "Invalid action numbers:0x%x for VF %d\n",
1159 			as->count, vf->vf_id);
1160 		return -EINVAL;
1161 	}
1162 
1163 	for (i = 0; i < as->count; i++) {
1164 		struct virtchnl_filter_action *action = &as->actions[i];
1165 
1166 		switch (action->type) {
1167 		case VIRTCHNL_ACTION_PASSTHRU:
1168 			dest_num++;
1169 			input->dest_ctl = ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_OTHER;
1170 			break;
1171 		case VIRTCHNL_ACTION_DROP:
1172 			dest_num++;
1173 			input->dest_ctl = ICE_FLTR_PRGM_DESC_DEST_DROP_PKT;
1174 			break;
1175 		case VIRTCHNL_ACTION_QUEUE:
1176 			dest_num++;
1177 			input->dest_ctl = ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_QINDEX;
1178 			input->q_index = action->act_conf.queue.index;
1179 			break;
1180 		case VIRTCHNL_ACTION_Q_REGION:
1181 			dest_num++;
1182 			input->dest_ctl = ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_QGROUP;
1183 			input->q_index = action->act_conf.queue.index;
1184 			input->q_region = action->act_conf.queue.region;
1185 			break;
1186 		case VIRTCHNL_ACTION_MARK:
1187 			mark_num++;
1188 			input->fltr_id = action->act_conf.mark_id;
1189 			input->fdid_prio = ICE_FXD_FLTR_QW1_FDID_PRI_THREE;
1190 			break;
1191 		default:
1192 			dev_dbg(dev, "Invalid action type:0x%x for VF %d\n",
1193 				action->type, vf->vf_id);
1194 			return -EINVAL;
1195 		}
1196 	}
1197 
1198 	if (dest_num == 0 || dest_num >= 2) {
1199 		dev_dbg(dev, "Invalid destination action for VF %d\n",
1200 			vf->vf_id);
1201 		return -EINVAL;
1202 	}
1203 
1204 	if (mark_num >= 2) {
1205 		dev_dbg(dev, "Too many mark actions for VF %d\n", vf->vf_id);
1206 		return -EINVAL;
1207 	}
1208 
1209 	return 0;
1210 }
1211 
1212 /**
1213  * ice_vc_validate_fdir_fltr - validate the virtual channel filter
1214  * @vf: pointer to the VF info
1215  * @fltr: virtual channel add cmd buffer
1216  * @conf: FDIR configuration for each filter
1217  *
1218  * Return: 0 on success, and other on error.
1219  */
1220 static int
1221 ice_vc_validate_fdir_fltr(struct ice_vf *vf, struct virtchnl_fdir_add *fltr,
1222 			  struct virtchnl_fdir_fltr_conf *conf)
1223 {
1224 	struct virtchnl_proto_hdrs *proto = &fltr->rule_cfg.proto_hdrs;
1225 	int ret;
1226 
1227 	/* For raw FDIR filters created by the parser */
1228 	if (!ice_vc_fdir_is_raw_flow(proto))
1229 		if (!ice_vc_validate_pattern(vf, proto))
1230 			return -EINVAL;
1231 
1232 	ret = ice_vc_fdir_parse_pattern(vf, fltr, conf);
1233 	if (ret)
1234 		return ret;
1235 
1236 	return ice_vc_fdir_parse_action(vf, fltr, conf);
1237 }
1238 
1239 /**
1240  * ice_vc_fdir_comp_rules - compare if two filter rules have the same value
1241  * @conf_a: FDIR configuration for filter a
1242  * @conf_b: FDIR configuration for filter b
1243  *
1244  * Return: 0 on success, and other on error.
1245  */
1246 static bool
1247 ice_vc_fdir_comp_rules(struct virtchnl_fdir_fltr_conf *conf_a,
1248 		       struct virtchnl_fdir_fltr_conf *conf_b)
1249 {
1250 	struct ice_ntuple_fltr *a = &conf_a->input;
1251 	struct ice_ntuple_fltr *b = &conf_b->input;
1252 
1253 	if (conf_a->ttype != conf_b->ttype)
1254 		return false;
1255 	if (a->flow_type != b->flow_type)
1256 		return false;
1257 	if (memcmp(&a->ip, &b->ip, sizeof(a->ip)))
1258 		return false;
1259 	if (memcmp(&a->mask, &b->mask, sizeof(a->mask)))
1260 		return false;
1261 	if (memcmp(&a->gtpu_data, &b->gtpu_data, sizeof(a->gtpu_data)))
1262 		return false;
1263 	if (memcmp(&a->gtpu_mask, &b->gtpu_mask, sizeof(a->gtpu_mask)))
1264 		return false;
1265 	if (memcmp(&a->l2tpv3_data, &b->l2tpv3_data, sizeof(a->l2tpv3_data)))
1266 		return false;
1267 	if (memcmp(&a->l2tpv3_mask, &b->l2tpv3_mask, sizeof(a->l2tpv3_mask)))
1268 		return false;
1269 	if (memcmp(&a->ext_data, &b->ext_data, sizeof(a->ext_data)))
1270 		return false;
1271 	if (memcmp(&a->ext_mask, &b->ext_mask, sizeof(a->ext_mask)))
1272 		return false;
1273 
1274 	return true;
1275 }
1276 
1277 /**
1278  * ice_vc_fdir_is_dup_fltr
1279  * @vf: pointer to the VF info
1280  * @conf: FDIR configuration for each filter
1281  *
1282  * Check if there is duplicated rule with same conf value
1283  *
1284  * Return: 0 true success, and false on error.
1285  */
1286 static bool
1287 ice_vc_fdir_is_dup_fltr(struct ice_vf *vf, struct virtchnl_fdir_fltr_conf *conf)
1288 {
1289 	struct ice_ntuple_fltr *desc;
1290 	bool ret;
1291 
1292 	list_for_each_entry(desc, &vf->fdir.fdir_rule_list, fltr_node) {
1293 		struct virtchnl_fdir_fltr_conf *node =
1294 				to_fltr_conf_from_desc(desc);
1295 
1296 		ret = ice_vc_fdir_comp_rules(node, conf);
1297 		if (ret)
1298 			return true;
1299 	}
1300 
1301 	return false;
1302 }
1303 
1304 /**
1305  * ice_vc_fdir_insert_entry
1306  * @vf: pointer to the VF info
1307  * @conf: FDIR configuration for each filter
1308  * @id: pointer to ID value allocated by driver
1309  *
1310  * Insert FDIR conf entry into list and allocate ID for this filter
1311  *
1312  * Return: 0 true success, and other on error.
1313  */
1314 static int
1315 ice_vc_fdir_insert_entry(struct ice_vf *vf,
1316 			 struct virtchnl_fdir_fltr_conf *conf, u32 *id)
1317 {
1318 	struct ice_ntuple_fltr *input = &conf->input;
1319 	int i;
1320 
1321 	/* alloc ID corresponding with conf */
1322 	i = idr_alloc(&vf->fdir.fdir_rule_idr, conf, 0,
1323 		      ICE_FDIR_MAX_FLTRS, GFP_KERNEL);
1324 	if (i < 0)
1325 		return -EINVAL;
1326 	*id = i;
1327 
1328 	list_add(&input->fltr_node, &vf->fdir.fdir_rule_list);
1329 	return 0;
1330 }
1331 
1332 /**
1333  * ice_vc_fdir_remove_entry - remove FDIR conf entry by ID value
1334  * @vf: pointer to the VF info
1335  * @conf: FDIR configuration for each filter
1336  * @id: filter rule's ID
1337  */
1338 static void
1339 ice_vc_fdir_remove_entry(struct ice_vf *vf,
1340 			 struct virtchnl_fdir_fltr_conf *conf, u32 id)
1341 {
1342 	struct ice_ntuple_fltr *input = &conf->input;
1343 
1344 	idr_remove(&vf->fdir.fdir_rule_idr, id);
1345 	list_del(&input->fltr_node);
1346 }
1347 
1348 /**
1349  * ice_vc_fdir_lookup_entry - lookup FDIR conf entry by ID value
1350  * @vf: pointer to the VF info
1351  * @id: filter rule's ID
1352  *
1353  * Return: NULL on error, and other on success.
1354  */
1355 static struct virtchnl_fdir_fltr_conf *
1356 ice_vc_fdir_lookup_entry(struct ice_vf *vf, u32 id)
1357 {
1358 	return idr_find(&vf->fdir.fdir_rule_idr, id);
1359 }
1360 
1361 /**
1362  * ice_vc_fdir_flush_entry - remove all FDIR conf entry
1363  * @vf: pointer to the VF info
1364  */
1365 static void ice_vc_fdir_flush_entry(struct ice_vf *vf)
1366 {
1367 	struct virtchnl_fdir_fltr_conf *conf;
1368 	struct ice_ntuple_fltr *desc, *temp;
1369 
1370 	list_for_each_entry_safe(desc, temp,
1371 				 &vf->fdir.fdir_rule_list, fltr_node) {
1372 		conf = to_fltr_conf_from_desc(desc);
1373 		list_del(&desc->fltr_node);
1374 		devm_kfree(ice_pf_to_dev(vf->pf), conf);
1375 	}
1376 }
1377 
1378 /**
1379  * ice_vc_fdir_write_fltr - write filter rule into hardware
1380  * @vf: pointer to the VF info
1381  * @conf: FDIR configuration for each filter
1382  * @add: true implies add rule, false implies del rules
1383  * @is_tun: false implies non-tunnel type filter, true implies tunnel filter
1384  *
1385  * Return: 0 on success, and other on error.
1386  */
1387 static int ice_vc_fdir_write_fltr(struct ice_vf *vf,
1388 				  struct virtchnl_fdir_fltr_conf *conf,
1389 				  bool add, bool is_tun)
1390 {
1391 	struct ice_ntuple_fltr *input = &conf->input;
1392 	struct ice_vsi *vsi, *ctrl_vsi;
1393 	struct ice_fltr_desc desc;
1394 	struct device *dev;
1395 	struct ice_pf *pf;
1396 	struct ice_hw *hw;
1397 	int ret;
1398 	u8 *pkt;
1399 
1400 	pf = vf->pf;
1401 	dev = ice_pf_to_dev(pf);
1402 	hw = &pf->hw;
1403 	vsi = ice_get_vf_vsi(vf);
1404 	if (!vsi) {
1405 		dev_dbg(dev, "Invalid vsi for VF %d\n", vf->vf_id);
1406 		return -EINVAL;
1407 	}
1408 
1409 	input->dest_vsi = vsi->idx;
1410 	input->comp_report = ICE_FXD_FLTR_QW0_COMP_REPORT_SW;
1411 
1412 	ctrl_vsi = pf->vsi[vf->ctrl_vsi_idx];
1413 	if (!ctrl_vsi) {
1414 		dev_dbg(dev, "Invalid ctrl_vsi for VF %d\n", vf->vf_id);
1415 		return -EINVAL;
1416 	}
1417 
1418 	pkt = devm_kzalloc(dev, ICE_FDIR_MAX_RAW_PKT_SIZE, GFP_KERNEL);
1419 	if (!pkt)
1420 		return -ENOMEM;
1421 
1422 	ice_fdir_get_prgm_desc(hw, input, &desc, add);
1423 	if (conf->parser_ena) {
1424 		memcpy(pkt, conf->pkt_buf, conf->pkt_len);
1425 	} else {
1426 		ret = ice_fdir_get_gen_prgm_pkt(hw, input, pkt, false, is_tun);
1427 		if (ret) {
1428 			dev_dbg(dev, "Gen training pkt for VF %d ptype %d failed\n",
1429 				vf->vf_id, input->flow_type);
1430 			goto err_free_pkt;
1431 		}
1432 	}
1433 
1434 	ret = ice_prgm_fdir_fltr(ctrl_vsi, &desc, pkt);
1435 	if (ret)
1436 		goto err_free_pkt;
1437 
1438 	return 0;
1439 
1440 err_free_pkt:
1441 	devm_kfree(dev, pkt);
1442 	return ret;
1443 }
1444 
1445 /**
1446  * ice_vf_fdir_timer - FDIR program waiting timer interrupt handler
1447  * @t: pointer to timer_list
1448  */
1449 static void ice_vf_fdir_timer(struct timer_list *t)
1450 {
1451 	struct ice_vf_fdir_ctx *ctx_irq = timer_container_of(ctx_irq, t,
1452 							     rx_tmr);
1453 	struct ice_vf_fdir_ctx *ctx_done;
1454 	struct ice_vf_fdir *fdir;
1455 	unsigned long flags;
1456 	struct ice_vf *vf;
1457 	struct ice_pf *pf;
1458 
1459 	fdir = container_of(ctx_irq, struct ice_vf_fdir, ctx_irq);
1460 	vf = container_of(fdir, struct ice_vf, fdir);
1461 	ctx_done = &fdir->ctx_done;
1462 	pf = vf->pf;
1463 	spin_lock_irqsave(&fdir->ctx_lock, flags);
1464 	if (!(ctx_irq->flags & ICE_VF_FDIR_CTX_VALID)) {
1465 		spin_unlock_irqrestore(&fdir->ctx_lock, flags);
1466 		WARN_ON_ONCE(1);
1467 		return;
1468 	}
1469 
1470 	ctx_irq->flags &= ~ICE_VF_FDIR_CTX_VALID;
1471 
1472 	ctx_done->flags |= ICE_VF_FDIR_CTX_VALID;
1473 	ctx_done->conf = ctx_irq->conf;
1474 	ctx_done->stat = ICE_FDIR_CTX_TIMEOUT;
1475 	ctx_done->v_opcode = ctx_irq->v_opcode;
1476 	spin_unlock_irqrestore(&fdir->ctx_lock, flags);
1477 
1478 	set_bit(ICE_FD_VF_FLUSH_CTX, pf->state);
1479 	ice_service_task_schedule(pf);
1480 }
1481 
1482 /**
1483  * ice_vc_fdir_irq_handler - ctrl_vsi Rx queue interrupt handler
1484  * @ctrl_vsi: pointer to a VF's CTRL VSI
1485  * @rx_desc: pointer to FDIR Rx queue descriptor
1486  */
1487 void
1488 ice_vc_fdir_irq_handler(struct ice_vsi *ctrl_vsi,
1489 			union ice_32b_rx_flex_desc *rx_desc)
1490 {
1491 	struct ice_pf *pf = ctrl_vsi->back;
1492 	struct ice_vf *vf = ctrl_vsi->vf;
1493 	struct ice_vf_fdir_ctx *ctx_done;
1494 	struct ice_vf_fdir_ctx *ctx_irq;
1495 	struct ice_vf_fdir *fdir;
1496 	unsigned long flags;
1497 	struct device *dev;
1498 	int ret;
1499 
1500 	if (WARN_ON(!vf))
1501 		return;
1502 
1503 	fdir = &vf->fdir;
1504 	ctx_done = &fdir->ctx_done;
1505 	ctx_irq = &fdir->ctx_irq;
1506 	dev = ice_pf_to_dev(pf);
1507 	spin_lock_irqsave(&fdir->ctx_lock, flags);
1508 	if (!(ctx_irq->flags & ICE_VF_FDIR_CTX_VALID)) {
1509 		spin_unlock_irqrestore(&fdir->ctx_lock, flags);
1510 		WARN_ON_ONCE(1);
1511 		return;
1512 	}
1513 
1514 	ctx_irq->flags &= ~ICE_VF_FDIR_CTX_VALID;
1515 
1516 	ctx_done->flags |= ICE_VF_FDIR_CTX_VALID;
1517 	ctx_done->conf = ctx_irq->conf;
1518 	ctx_done->stat = ICE_FDIR_CTX_IRQ;
1519 	ctx_done->v_opcode = ctx_irq->v_opcode;
1520 	memcpy(&ctx_done->rx_desc, rx_desc, sizeof(*rx_desc));
1521 	spin_unlock_irqrestore(&fdir->ctx_lock, flags);
1522 
1523 	ret = timer_delete(&ctx_irq->rx_tmr);
1524 	if (!ret)
1525 		dev_err(dev, "VF %d: Unexpected inactive timer!\n", vf->vf_id);
1526 
1527 	set_bit(ICE_FD_VF_FLUSH_CTX, pf->state);
1528 	ice_service_task_schedule(pf);
1529 }
1530 
1531 /**
1532  * ice_vf_fdir_dump_info - dump FDIR information for diagnosis
1533  * @vf: pointer to the VF info
1534  */
1535 static void ice_vf_fdir_dump_info(struct ice_vf *vf)
1536 {
1537 	u32 fd_size, fd_cnt, fd_size_g, fd_cnt_g, fd_size_b, fd_cnt_b;
1538 	struct ice_vsi *vf_vsi;
1539 	struct device *dev;
1540 	struct ice_pf *pf;
1541 	struct ice_hw *hw;
1542 	u16 vsi_num;
1543 
1544 	pf = vf->pf;
1545 	hw = &pf->hw;
1546 	dev = ice_pf_to_dev(pf);
1547 	vf_vsi = ice_get_vf_vsi(vf);
1548 	if (!vf_vsi) {
1549 		dev_dbg(dev, "VF %d: invalid VSI pointer\n", vf->vf_id);
1550 		return;
1551 	}
1552 
1553 	vsi_num = ice_get_hw_vsi_num(hw, vf_vsi->idx);
1554 
1555 	fd_size = rd32(hw, VSIQF_FD_SIZE(vsi_num));
1556 	fd_cnt = rd32(hw, VSIQF_FD_CNT(vsi_num));
1557 	switch (hw->mac_type) {
1558 	case ICE_MAC_E830:
1559 		fd_size_g = FIELD_GET(E830_VSIQF_FD_CNT_FD_GCNT_M, fd_size);
1560 		fd_size_b = FIELD_GET(E830_VSIQF_FD_CNT_FD_BCNT_M, fd_size);
1561 		fd_cnt_g = FIELD_GET(E830_VSIQF_FD_CNT_FD_GCNT_M, fd_cnt);
1562 		fd_cnt_b = FIELD_GET(E830_VSIQF_FD_CNT_FD_BCNT_M, fd_cnt);
1563 		break;
1564 	case ICE_MAC_E810:
1565 	default:
1566 		fd_size_g = FIELD_GET(E800_VSIQF_FD_CNT_FD_GCNT_M, fd_size);
1567 		fd_size_b = FIELD_GET(E800_VSIQF_FD_CNT_FD_BCNT_M, fd_size);
1568 		fd_cnt_g = FIELD_GET(E800_VSIQF_FD_CNT_FD_GCNT_M, fd_cnt);
1569 		fd_cnt_b = FIELD_GET(E800_VSIQF_FD_CNT_FD_BCNT_M, fd_cnt);
1570 	}
1571 
1572 	dev_dbg(dev, "VF %d: Size in the FD table: guaranteed:0x%x, best effort:0x%x\n",
1573 		vf->vf_id, fd_size_g, fd_size_b);
1574 	dev_dbg(dev, "VF %d: Filter counter in the FD table: guaranteed:0x%x, best effort:0x%x\n",
1575 		vf->vf_id, fd_cnt_g, fd_cnt_b);
1576 }
1577 
1578 /**
1579  * ice_vf_verify_rx_desc - verify received FDIR programming status descriptor
1580  * @vf: pointer to the VF info
1581  * @ctx: FDIR context info for post processing
1582  * @status: virtchnl FDIR program status
1583  *
1584  * Return: 0 on success, and other on error.
1585  */
1586 static int
1587 ice_vf_verify_rx_desc(struct ice_vf *vf, struct ice_vf_fdir_ctx *ctx,
1588 		      enum virtchnl_fdir_prgm_status *status)
1589 {
1590 	struct device *dev = ice_pf_to_dev(vf->pf);
1591 	u32 stat_err, error, prog_id;
1592 	int ret;
1593 
1594 	stat_err = le16_to_cpu(ctx->rx_desc.wb.status_error0);
1595 	if (FIELD_GET(ICE_FXD_FLTR_WB_QW1_DD_M, stat_err) !=
1596 	    ICE_FXD_FLTR_WB_QW1_DD_YES) {
1597 		*status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
1598 		dev_err(dev, "VF %d: Desc Done not set\n", vf->vf_id);
1599 		ret = -EINVAL;
1600 		goto err_exit;
1601 	}
1602 
1603 	prog_id = FIELD_GET(ICE_FXD_FLTR_WB_QW1_PROG_ID_M, stat_err);
1604 	if (prog_id == ICE_FXD_FLTR_WB_QW1_PROG_ADD &&
1605 	    ctx->v_opcode != VIRTCHNL_OP_ADD_FDIR_FILTER) {
1606 		dev_err(dev, "VF %d: Desc show add, but ctx not",
1607 			vf->vf_id);
1608 		*status = VIRTCHNL_FDIR_FAILURE_RULE_INVALID;
1609 		ret = -EINVAL;
1610 		goto err_exit;
1611 	}
1612 
1613 	if (prog_id == ICE_FXD_FLTR_WB_QW1_PROG_DEL &&
1614 	    ctx->v_opcode != VIRTCHNL_OP_DEL_FDIR_FILTER) {
1615 		dev_err(dev, "VF %d: Desc show del, but ctx not",
1616 			vf->vf_id);
1617 		*status = VIRTCHNL_FDIR_FAILURE_RULE_INVALID;
1618 		ret = -EINVAL;
1619 		goto err_exit;
1620 	}
1621 
1622 	error = FIELD_GET(ICE_FXD_FLTR_WB_QW1_FAIL_M, stat_err);
1623 	if (error == ICE_FXD_FLTR_WB_QW1_FAIL_YES) {
1624 		if (prog_id == ICE_FXD_FLTR_WB_QW1_PROG_ADD) {
1625 			dev_err(dev, "VF %d, Failed to add FDIR rule due to no space in the table",
1626 				vf->vf_id);
1627 			*status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
1628 		} else {
1629 			dev_err(dev, "VF %d, Failed to remove FDIR rule, attempt to remove non-existent entry",
1630 				vf->vf_id);
1631 			*status = VIRTCHNL_FDIR_FAILURE_RULE_NONEXIST;
1632 		}
1633 		ret = -EINVAL;
1634 		goto err_exit;
1635 	}
1636 
1637 	error = FIELD_GET(ICE_FXD_FLTR_WB_QW1_FAIL_PROF_M, stat_err);
1638 	if (error == ICE_FXD_FLTR_WB_QW1_FAIL_PROF_YES) {
1639 		dev_err(dev, "VF %d: Profile matching error", vf->vf_id);
1640 		*status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
1641 		ret = -EINVAL;
1642 		goto err_exit;
1643 	}
1644 
1645 	*status = VIRTCHNL_FDIR_SUCCESS;
1646 
1647 	return 0;
1648 
1649 err_exit:
1650 	ice_vf_fdir_dump_info(vf);
1651 	return ret;
1652 }
1653 
1654 static int ice_fdir_is_tunnel(enum ice_fdir_tunnel_type ttype)
1655 {
1656 	return (ttype == ICE_FDIR_TUNNEL_TYPE_GRE_INNER ||
1657 		ttype == ICE_FDIR_TUNNEL_TYPE_GTPU_INNER ||
1658 		ttype == ICE_FDIR_TUNNEL_TYPE_GTPU_EH_INNER ||
1659 		ttype == ICE_FDIR_TUNNEL_TYPE_GTPOGRE_INNER ||
1660 		ttype == ICE_FDIR_TUNNEL_TYPE_ECPRI ||
1661 		ttype == ICE_FDIR_TUNNEL_TYPE_L2TPV2_INNER);
1662 }
1663 
1664 /**
1665  * ice_vc_add_fdir_fltr_post
1666  * @vf: pointer to the VF structure
1667  * @ctx: FDIR context info for post processing
1668  * @status: virtchnl FDIR program status
1669  * @success: true implies success, false implies failure
1670  *
1671  * Post process for flow director add command. If success, then do post process
1672  * and send back success msg by virtchnl. Otherwise, do context reversion and
1673  * send back failure msg by virtchnl.
1674  *
1675  * Return: 0 on success, and other on error.
1676  */
1677 static int
1678 ice_vc_add_fdir_fltr_post(struct ice_vf *vf, struct ice_vf_fdir_ctx *ctx,
1679 			  enum virtchnl_fdir_prgm_status status,
1680 			  bool success)
1681 {
1682 	struct virtchnl_fdir_fltr_conf *conf = ctx->conf;
1683 	struct device *dev = ice_pf_to_dev(vf->pf);
1684 	enum virtchnl_status_code v_ret;
1685 	struct virtchnl_fdir_add *resp;
1686 	int ret, len, is_tun;
1687 
1688 	v_ret = VIRTCHNL_STATUS_SUCCESS;
1689 	len = sizeof(*resp);
1690 	resp = kzalloc(len, GFP_KERNEL);
1691 	if (!resp) {
1692 		len = 0;
1693 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1694 		dev_dbg(dev, "VF %d: Alloc resp buf fail", vf->vf_id);
1695 		goto err_exit;
1696 	}
1697 
1698 	if (!success)
1699 		goto err_exit;
1700 
1701 	is_tun = 0;
1702 	resp->status = status;
1703 	resp->flow_id = conf->flow_id;
1704 	vf->fdir.fdir_fltr_cnt[conf->input.flow_type][is_tun]++;
1705 	vf->fdir.fdir_fltr_cnt_total++;
1706 
1707 	ret = ice_vc_send_msg_to_vf(vf, ctx->v_opcode, v_ret,
1708 				    (u8 *)resp, len);
1709 	kfree(resp);
1710 
1711 	dev_dbg(dev, "VF %d: flow_id:0x%X, FDIR %s success!\n",
1712 		vf->vf_id, conf->flow_id,
1713 		(ctx->v_opcode == VIRTCHNL_OP_ADD_FDIR_FILTER) ?
1714 		"add" : "del");
1715 	return ret;
1716 
1717 err_exit:
1718 	if (resp)
1719 		resp->status = status;
1720 	ice_vc_fdir_remove_entry(vf, conf, conf->flow_id);
1721 	devm_kfree(dev, conf);
1722 
1723 	ret = ice_vc_send_msg_to_vf(vf, ctx->v_opcode, v_ret,
1724 				    (u8 *)resp, len);
1725 	kfree(resp);
1726 	return ret;
1727 }
1728 
1729 /**
1730  * ice_vc_del_fdir_fltr_post
1731  * @vf: pointer to the VF structure
1732  * @ctx: FDIR context info for post processing
1733  * @status: virtchnl FDIR program status
1734  * @success: true implies success, false implies failure
1735  *
1736  * Post process for flow director del command. If success, then do post process
1737  * and send back success msg by virtchnl. Otherwise, do context reversion and
1738  * send back failure msg by virtchnl.
1739  *
1740  * Return: 0 on success, and other on error.
1741  */
1742 static int
1743 ice_vc_del_fdir_fltr_post(struct ice_vf *vf, struct ice_vf_fdir_ctx *ctx,
1744 			  enum virtchnl_fdir_prgm_status status,
1745 			  bool success)
1746 {
1747 	struct virtchnl_fdir_fltr_conf *conf = ctx->conf;
1748 	struct device *dev = ice_pf_to_dev(vf->pf);
1749 	enum virtchnl_status_code v_ret;
1750 	struct virtchnl_fdir_del *resp;
1751 	int ret, len, is_tun;
1752 
1753 	v_ret = VIRTCHNL_STATUS_SUCCESS;
1754 	len = sizeof(*resp);
1755 	resp = kzalloc(len, GFP_KERNEL);
1756 	if (!resp) {
1757 		len = 0;
1758 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
1759 		dev_dbg(dev, "VF %d: Alloc resp buf fail", vf->vf_id);
1760 		goto err_exit;
1761 	}
1762 
1763 	if (!success)
1764 		goto err_exit;
1765 
1766 	is_tun = 0;
1767 	resp->status = status;
1768 	ice_vc_fdir_remove_entry(vf, conf, conf->flow_id);
1769 	vf->fdir.fdir_fltr_cnt[conf->input.flow_type][is_tun]--;
1770 	vf->fdir.fdir_fltr_cnt_total--;
1771 
1772 	ret = ice_vc_send_msg_to_vf(vf, ctx->v_opcode, v_ret,
1773 				    (u8 *)resp, len);
1774 	kfree(resp);
1775 
1776 	dev_dbg(dev, "VF %d: flow_id:0x%X, FDIR %s success!\n",
1777 		vf->vf_id, conf->flow_id,
1778 		(ctx->v_opcode == VIRTCHNL_OP_ADD_FDIR_FILTER) ?
1779 		"add" : "del");
1780 	devm_kfree(dev, conf);
1781 	return ret;
1782 
1783 err_exit:
1784 	if (resp)
1785 		resp->status = status;
1786 	if (success)
1787 		devm_kfree(dev, conf);
1788 
1789 	ret = ice_vc_send_msg_to_vf(vf, ctx->v_opcode, v_ret,
1790 				    (u8 *)resp, len);
1791 	kfree(resp);
1792 	return ret;
1793 }
1794 
1795 /**
1796  * ice_flush_fdir_ctx
1797  * @pf: pointer to the PF structure
1798  *
1799  * Flush all the pending event on ctx_done list and process them.
1800  */
1801 void ice_flush_fdir_ctx(struct ice_pf *pf)
1802 {
1803 	struct ice_vf *vf;
1804 	unsigned int bkt;
1805 
1806 	if (!test_and_clear_bit(ICE_FD_VF_FLUSH_CTX, pf->state))
1807 		return;
1808 
1809 	mutex_lock(&pf->vfs.table_lock);
1810 	ice_for_each_vf(pf, bkt, vf) {
1811 		struct device *dev = ice_pf_to_dev(pf);
1812 		enum virtchnl_fdir_prgm_status status;
1813 		struct ice_vf_fdir_ctx *ctx;
1814 		unsigned long flags;
1815 		int ret;
1816 
1817 		if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
1818 			continue;
1819 
1820 		if (vf->ctrl_vsi_idx == ICE_NO_VSI)
1821 			continue;
1822 
1823 		ctx = &vf->fdir.ctx_done;
1824 		spin_lock_irqsave(&vf->fdir.ctx_lock, flags);
1825 		if (!(ctx->flags & ICE_VF_FDIR_CTX_VALID)) {
1826 			spin_unlock_irqrestore(&vf->fdir.ctx_lock, flags);
1827 			continue;
1828 		}
1829 		spin_unlock_irqrestore(&vf->fdir.ctx_lock, flags);
1830 
1831 		WARN_ON(ctx->stat == ICE_FDIR_CTX_READY);
1832 		if (ctx->stat == ICE_FDIR_CTX_TIMEOUT) {
1833 			status = VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT;
1834 			dev_err(dev, "VF %d: ctrl_vsi irq timeout\n",
1835 				vf->vf_id);
1836 			goto err_exit;
1837 		}
1838 
1839 		ret = ice_vf_verify_rx_desc(vf, ctx, &status);
1840 		if (ret)
1841 			goto err_exit;
1842 
1843 		if (ctx->v_opcode == VIRTCHNL_OP_ADD_FDIR_FILTER)
1844 			ice_vc_add_fdir_fltr_post(vf, ctx, status, true);
1845 		else if (ctx->v_opcode == VIRTCHNL_OP_DEL_FDIR_FILTER)
1846 			ice_vc_del_fdir_fltr_post(vf, ctx, status, true);
1847 		else
1848 			dev_err(dev, "VF %d: Unsupported opcode\n", vf->vf_id);
1849 
1850 		spin_lock_irqsave(&vf->fdir.ctx_lock, flags);
1851 		ctx->flags &= ~ICE_VF_FDIR_CTX_VALID;
1852 		spin_unlock_irqrestore(&vf->fdir.ctx_lock, flags);
1853 		continue;
1854 err_exit:
1855 		if (ctx->v_opcode == VIRTCHNL_OP_ADD_FDIR_FILTER)
1856 			ice_vc_add_fdir_fltr_post(vf, ctx, status, false);
1857 		else if (ctx->v_opcode == VIRTCHNL_OP_DEL_FDIR_FILTER)
1858 			ice_vc_del_fdir_fltr_post(vf, ctx, status, false);
1859 		else
1860 			dev_err(dev, "VF %d: Unsupported opcode\n", vf->vf_id);
1861 
1862 		spin_lock_irqsave(&vf->fdir.ctx_lock, flags);
1863 		ctx->flags &= ~ICE_VF_FDIR_CTX_VALID;
1864 		spin_unlock_irqrestore(&vf->fdir.ctx_lock, flags);
1865 	}
1866 	mutex_unlock(&pf->vfs.table_lock);
1867 }
1868 
1869 /**
1870  * ice_vc_fdir_set_irq_ctx - set FDIR context info for later IRQ handler
1871  * @vf: pointer to the VF structure
1872  * @conf: FDIR configuration for each filter
1873  * @v_opcode: virtual channel operation code
1874  *
1875  * Return: 0 on success, and other on error.
1876  */
1877 static int
1878 ice_vc_fdir_set_irq_ctx(struct ice_vf *vf, struct virtchnl_fdir_fltr_conf *conf,
1879 			enum virtchnl_ops v_opcode)
1880 {
1881 	struct device *dev = ice_pf_to_dev(vf->pf);
1882 	struct ice_vf_fdir_ctx *ctx;
1883 	unsigned long flags;
1884 
1885 	ctx = &vf->fdir.ctx_irq;
1886 	spin_lock_irqsave(&vf->fdir.ctx_lock, flags);
1887 	if ((vf->fdir.ctx_irq.flags & ICE_VF_FDIR_CTX_VALID) ||
1888 	    (vf->fdir.ctx_done.flags & ICE_VF_FDIR_CTX_VALID)) {
1889 		spin_unlock_irqrestore(&vf->fdir.ctx_lock, flags);
1890 		dev_dbg(dev, "VF %d: Last request is still in progress\n",
1891 			vf->vf_id);
1892 		return -EBUSY;
1893 	}
1894 	ctx->flags |= ICE_VF_FDIR_CTX_VALID;
1895 	spin_unlock_irqrestore(&vf->fdir.ctx_lock, flags);
1896 
1897 	ctx->conf = conf;
1898 	ctx->v_opcode = v_opcode;
1899 	ctx->stat = ICE_FDIR_CTX_READY;
1900 	timer_setup(&ctx->rx_tmr, ice_vf_fdir_timer, 0);
1901 
1902 	mod_timer(&ctx->rx_tmr, round_jiffies(msecs_to_jiffies(10) + jiffies));
1903 
1904 	return 0;
1905 }
1906 
1907 /**
1908  * ice_vc_fdir_clear_irq_ctx - clear FDIR context info for IRQ handler
1909  * @vf: pointer to the VF structure
1910  *
1911  * Return: 0 on success, and other on error.
1912  */
1913 static void ice_vc_fdir_clear_irq_ctx(struct ice_vf *vf)
1914 {
1915 	struct ice_vf_fdir_ctx *ctx = &vf->fdir.ctx_irq;
1916 	unsigned long flags;
1917 
1918 	timer_delete(&ctx->rx_tmr);
1919 	spin_lock_irqsave(&vf->fdir.ctx_lock, flags);
1920 	ctx->flags &= ~ICE_VF_FDIR_CTX_VALID;
1921 	spin_unlock_irqrestore(&vf->fdir.ctx_lock, flags);
1922 }
1923 
1924 /**
1925  * ice_vc_parser_fv_check_diff - check two parsed FDIR profile fv context
1926  * @fv_a: struct of parsed FDIR profile field vector
1927  * @fv_b: struct of parsed FDIR profile field vector
1928  *
1929  * Check if the two parsed FDIR profile field vector context are different,
1930  * including proto_id, offset and mask.
1931  *
1932  * Return: true on different, false on otherwise.
1933  */
1934 static bool ice_vc_parser_fv_check_diff(struct ice_parser_fv *fv_a,
1935 					struct ice_parser_fv *fv_b)
1936 {
1937 	return (fv_a->proto_id	!= fv_b->proto_id ||
1938 		fv_a->offset	!= fv_b->offset ||
1939 		fv_a->msk	!= fv_b->msk);
1940 }
1941 
1942 /**
1943  * ice_vc_parser_fv_save - save parsed FDIR profile fv context
1944  * @fv: struct of parsed FDIR profile field vector
1945  * @fv_src: parsed FDIR profile field vector context to save
1946  *
1947  * Save the parsed FDIR profile field vector context, including proto_id,
1948  * offset and mask.
1949  *
1950  * Return: Void.
1951  */
1952 static void ice_vc_parser_fv_save(struct ice_parser_fv *fv,
1953 				  struct ice_parser_fv *fv_src)
1954 {
1955 	fv->proto_id	= fv_src->proto_id;
1956 	fv->offset	= fv_src->offset;
1957 	fv->msk		= fv_src->msk;
1958 	fv->spec	= 0;
1959 }
1960 
1961 /**
1962  * ice_vc_add_fdir_raw - add a raw FDIR filter for VF
1963  * @vf: pointer to the VF info
1964  * @conf: FDIR configuration for each filter
1965  * @v_ret: the final VIRTCHNL code
1966  * @stat: pointer to the VIRTCHNL_OP_ADD_FDIR_FILTER
1967  * @len: length of the stat
1968  *
1969  * Return: 0 on success or negative errno on failure.
1970  */
1971 static int
1972 ice_vc_add_fdir_raw(struct ice_vf *vf,
1973 		    struct virtchnl_fdir_fltr_conf *conf,
1974 		    enum virtchnl_status_code *v_ret,
1975 		    struct virtchnl_fdir_add *stat, int len)
1976 {
1977 	struct ice_vsi *vf_vsi, *ctrl_vsi;
1978 	struct ice_fdir_prof_info *pi;
1979 	struct ice_pf *pf = vf->pf;
1980 	int ret, ptg, id, i;
1981 	struct device *dev;
1982 	struct ice_hw *hw;
1983 	bool fv_found;
1984 
1985 	dev = ice_pf_to_dev(pf);
1986 	hw = &pf->hw;
1987 	*v_ret = VIRTCHNL_STATUS_ERR_PARAM;
1988 	stat->status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
1989 
1990 	id = find_first_bit(conf->prof->ptypes, ICE_FLOW_PTYPE_MAX);
1991 	ptg = hw->blk[ICE_BLK_FD].xlt1.t[id];
1992 
1993 	vf_vsi = ice_get_vf_vsi(vf);
1994 	if (!vf_vsi) {
1995 		dev_err(dev, "Can not get FDIR vf_vsi for VF %d\n", vf->vf_id);
1996 		return -ENODEV;
1997 	}
1998 
1999 	ctrl_vsi = pf->vsi[vf->ctrl_vsi_idx];
2000 	if (!ctrl_vsi) {
2001 		dev_err(dev, "Can not get FDIR ctrl_vsi for VF %d\n",
2002 			vf->vf_id);
2003 		return -ENODEV;
2004 	}
2005 
2006 	fv_found = false;
2007 
2008 	/* Check if profile info already exists, then update the counter */
2009 	pi = &vf->fdir_prof_info[ptg];
2010 	if (pi->fdir_active_cnt != 0) {
2011 		for (i = 0; i < ICE_MAX_FV_WORDS; i++)
2012 			if (ice_vc_parser_fv_check_diff(&pi->prof.fv[i],
2013 							&conf->prof->fv[i]))
2014 				break;
2015 		if (i == ICE_MAX_FV_WORDS) {
2016 			fv_found = true;
2017 			pi->fdir_active_cnt++;
2018 		}
2019 	}
2020 
2021 	/* HW profile setting is only required for the first time */
2022 	if (!fv_found) {
2023 		ret = ice_flow_set_parser_prof(hw, vf_vsi->idx,
2024 					       ctrl_vsi->idx, conf->prof,
2025 					       ICE_BLK_FD);
2026 
2027 		if (ret) {
2028 			*v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2029 			dev_dbg(dev, "VF %d: insert hw prof failed\n",
2030 				vf->vf_id);
2031 			return ret;
2032 		}
2033 	}
2034 
2035 	ret = ice_vc_fdir_insert_entry(vf, conf, &conf->flow_id);
2036 	if (ret) {
2037 		*v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2038 		dev_dbg(dev, "VF %d: insert FDIR list failed\n",
2039 			vf->vf_id);
2040 		return ret;
2041 	}
2042 
2043 	ret = ice_vc_fdir_set_irq_ctx(vf, conf,
2044 				      VIRTCHNL_OP_ADD_FDIR_FILTER);
2045 	if (ret) {
2046 		dev_dbg(dev, "VF %d: set FDIR context failed\n",
2047 			vf->vf_id);
2048 		goto err_rem_entry;
2049 	}
2050 
2051 	ret = ice_vc_fdir_write_fltr(vf, conf, true, false);
2052 	if (ret) {
2053 		dev_err(dev, "VF %d: adding FDIR raw flow rule failed, ret:%d\n",
2054 			vf->vf_id, ret);
2055 		goto err_clr_irq;
2056 	}
2057 
2058 	/* Save parsed profile fv info of the FDIR rule for the first time */
2059 	if (!fv_found) {
2060 		for (i = 0; i < conf->prof->fv_num; i++)
2061 			ice_vc_parser_fv_save(&pi->prof.fv[i],
2062 					      &conf->prof->fv[i]);
2063 		pi->prof.fv_num = conf->prof->fv_num;
2064 		pi->fdir_active_cnt = 1;
2065 	}
2066 
2067 	return 0;
2068 
2069 err_clr_irq:
2070 	ice_vc_fdir_clear_irq_ctx(vf);
2071 err_rem_entry:
2072 	ice_vc_fdir_remove_entry(vf, conf, conf->flow_id);
2073 	return ret;
2074 }
2075 
2076 /**
2077  * ice_vc_add_fdir_fltr - add a FDIR filter for VF by the msg buffer
2078  * @vf: pointer to the VF info
2079  * @msg: pointer to the msg buffer
2080  *
2081  * Return: 0 on success, and other on error.
2082  */
2083 int ice_vc_add_fdir_fltr(struct ice_vf *vf, u8 *msg)
2084 {
2085 	struct virtchnl_fdir_add *fltr = (struct virtchnl_fdir_add *)msg;
2086 	struct virtchnl_fdir_add *stat = NULL;
2087 	struct virtchnl_fdir_fltr_conf *conf;
2088 	enum virtchnl_status_code v_ret;
2089 	struct ice_vsi *vf_vsi;
2090 	struct device *dev;
2091 	struct ice_pf *pf;
2092 	int is_tun = 0;
2093 	int len = 0;
2094 	int ret;
2095 
2096 	pf = vf->pf;
2097 	dev = ice_pf_to_dev(pf);
2098 	vf_vsi = ice_get_vf_vsi(vf);
2099 	if (!vf_vsi) {
2100 		dev_err(dev, "Can not get FDIR vf_vsi for VF %u\n", vf->vf_id);
2101 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2102 		goto err_exit;
2103 	}
2104 
2105 #define ICE_VF_MAX_FDIR_FILTERS	128
2106 	if (!ice_fdir_num_avail_fltr(&pf->hw, vf_vsi) ||
2107 	    vf->fdir.fdir_fltr_cnt_total >= ICE_VF_MAX_FDIR_FILTERS) {
2108 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2109 		dev_err(dev, "Max number of FDIR filters for VF %d is reached\n",
2110 			vf->vf_id);
2111 		goto err_exit;
2112 	}
2113 
2114 	ret = ice_vc_fdir_param_check(vf, fltr->vsi_id);
2115 	if (ret) {
2116 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2117 		dev_dbg(dev, "Parameter check for VF %d failed\n", vf->vf_id);
2118 		goto err_exit;
2119 	}
2120 
2121 	ret = ice_vf_start_ctrl_vsi(vf);
2122 	if (ret && (ret != -EEXIST)) {
2123 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2124 		dev_err(dev, "Init FDIR for VF %d failed, ret:%d\n",
2125 			vf->vf_id, ret);
2126 		goto err_exit;
2127 	}
2128 
2129 	stat = kzalloc_obj(*stat);
2130 	if (!stat) {
2131 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2132 		dev_dbg(dev, "Alloc stat for VF %d failed\n", vf->vf_id);
2133 		goto err_exit;
2134 	}
2135 
2136 	conf = devm_kzalloc_obj(dev, *conf);
2137 	if (!conf) {
2138 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2139 		dev_dbg(dev, "Alloc conf for VF %d failed\n", vf->vf_id);
2140 		goto err_exit;
2141 	}
2142 
2143 	len = sizeof(*stat);
2144 	ret = ice_vc_validate_fdir_fltr(vf, fltr, conf);
2145 	if (ret) {
2146 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2147 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_INVALID;
2148 		dev_dbg(dev, "Invalid FDIR filter from VF %d\n", vf->vf_id);
2149 		goto err_free_conf;
2150 	}
2151 
2152 	if (fltr->validate_only) {
2153 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2154 		stat->status = VIRTCHNL_FDIR_SUCCESS;
2155 		devm_kfree(dev, conf);
2156 		ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_FDIR_FILTER,
2157 					    v_ret, (u8 *)stat, len);
2158 		goto exit;
2159 	}
2160 
2161 	/* For raw FDIR filters created by the parser */
2162 	if (conf->parser_ena) {
2163 		ret = ice_vc_add_fdir_raw(vf, conf, &v_ret, stat, len);
2164 		if (ret)
2165 			goto err_free_conf;
2166 		goto exit;
2167 	}
2168 
2169 	is_tun = ice_fdir_is_tunnel(conf->ttype);
2170 	ret = ice_vc_fdir_config_input_set(vf, fltr, conf, is_tun);
2171 	if (ret) {
2172 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2173 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_CONFLICT;
2174 		dev_err(dev, "VF %d: FDIR input set configure failed, ret:%d\n",
2175 			vf->vf_id, ret);
2176 		goto err_free_conf;
2177 	}
2178 
2179 	ret = ice_vc_fdir_is_dup_fltr(vf, conf);
2180 	if (ret) {
2181 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2182 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_EXIST;
2183 		dev_dbg(dev, "VF %d: duplicated FDIR rule detected\n",
2184 			vf->vf_id);
2185 		goto err_free_conf;
2186 	}
2187 
2188 	ret = ice_vc_fdir_insert_entry(vf, conf, &conf->flow_id);
2189 	if (ret) {
2190 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2191 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
2192 		dev_dbg(dev, "VF %d: insert FDIR list failed\n", vf->vf_id);
2193 		goto err_free_conf;
2194 	}
2195 
2196 	ret = ice_vc_fdir_set_irq_ctx(vf, conf, VIRTCHNL_OP_ADD_FDIR_FILTER);
2197 	if (ret) {
2198 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2199 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
2200 		dev_dbg(dev, "VF %d: set FDIR context failed\n", vf->vf_id);
2201 		goto err_rem_entry;
2202 	}
2203 
2204 	ret = ice_vc_fdir_write_fltr(vf, conf, true, is_tun);
2205 	if (ret) {
2206 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2207 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
2208 		dev_err(dev, "VF %d: writing FDIR rule failed, ret:%d\n",
2209 			vf->vf_id, ret);
2210 		goto err_clr_irq;
2211 	}
2212 
2213 exit:
2214 	kfree(stat);
2215 	return ret;
2216 
2217 err_clr_irq:
2218 	ice_vc_fdir_clear_irq_ctx(vf);
2219 err_rem_entry:
2220 	ice_vc_fdir_remove_entry(vf, conf, conf->flow_id);
2221 err_free_conf:
2222 	devm_kfree(dev, conf);
2223 err_exit:
2224 	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_FDIR_FILTER, v_ret,
2225 				    (u8 *)stat, len);
2226 	kfree(stat);
2227 	return ret;
2228 }
2229 
2230 /**
2231  * ice_vc_del_fdir_raw - delete a raw FDIR filter for VF
2232  * @vf: pointer to the VF info
2233  * @conf: FDIR configuration for each filter
2234  * @v_ret: the final VIRTCHNL code
2235  * @stat: pointer to the VIRTCHNL_OP_DEL_FDIR_FILTER
2236  * @len: length of the stat
2237  *
2238  * Return: 0 on success or negative errno on failure.
2239  */
2240 static int
2241 ice_vc_del_fdir_raw(struct ice_vf *vf,
2242 		    struct virtchnl_fdir_fltr_conf *conf,
2243 		    enum virtchnl_status_code *v_ret,
2244 		    struct virtchnl_fdir_del *stat, int len)
2245 {
2246 	struct ice_vsi *vf_vsi, *ctrl_vsi;
2247 	enum ice_block blk = ICE_BLK_FD;
2248 	struct ice_fdir_prof_info *pi;
2249 	struct ice_pf *pf = vf->pf;
2250 	struct device *dev;
2251 	struct ice_hw *hw;
2252 	unsigned long id;
2253 	u16 vsi_num;
2254 	int ptg;
2255 	int ret;
2256 
2257 	dev = ice_pf_to_dev(pf);
2258 	hw = &pf->hw;
2259 	*v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2260 	stat->status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
2261 
2262 	id = find_first_bit(conf->prof->ptypes, ICE_FLOW_PTYPE_MAX);
2263 	ptg = hw->blk[ICE_BLK_FD].xlt1.t[id];
2264 
2265 	ret = ice_vc_fdir_write_fltr(vf, conf, false, false);
2266 	if (ret) {
2267 		dev_err(dev, "VF %u: deleting FDIR raw flow rule failed: %d\n",
2268 			vf->vf_id, ret);
2269 		return ret;
2270 	}
2271 
2272 	vf_vsi = ice_get_vf_vsi(vf);
2273 	if (!vf_vsi) {
2274 		dev_err(dev, "Can not get FDIR vf_vsi for VF %u\n", vf->vf_id);
2275 		return -ENODEV;
2276 	}
2277 
2278 	ctrl_vsi = pf->vsi[vf->ctrl_vsi_idx];
2279 	if (!ctrl_vsi) {
2280 		dev_err(dev, "Can not get FDIR ctrl_vsi for VF %u\n",
2281 			vf->vf_id);
2282 		return -ENODEV;
2283 	}
2284 
2285 	pi = &vf->fdir_prof_info[ptg];
2286 	if (pi->fdir_active_cnt != 0) {
2287 		pi->fdir_active_cnt--;
2288 		/* Remove the profile id flow if no active FDIR rule left */
2289 		if (!pi->fdir_active_cnt) {
2290 			vsi_num = ice_get_hw_vsi_num(hw, ctrl_vsi->idx);
2291 			ice_rem_prof_id_flow(hw, blk, vsi_num, id);
2292 
2293 			vsi_num = ice_get_hw_vsi_num(hw, vf_vsi->idx);
2294 			ice_rem_prof_id_flow(hw, blk, vsi_num, id);
2295 		}
2296 	}
2297 
2298 	conf->parser_ena = false;
2299 	return 0;
2300 }
2301 
2302 /**
2303  * ice_vc_del_fdir_fltr - delete a FDIR filter for VF by the msg buffer
2304  * @vf: pointer to the VF info
2305  * @msg: pointer to the msg buffer
2306  *
2307  * Return: 0 on success, and other on error.
2308  */
2309 int ice_vc_del_fdir_fltr(struct ice_vf *vf, u8 *msg)
2310 {
2311 	struct virtchnl_fdir_del *fltr = (struct virtchnl_fdir_del *)msg;
2312 	struct virtchnl_fdir_del *stat = NULL;
2313 	struct virtchnl_fdir_fltr_conf *conf;
2314 	struct ice_vf_fdir *fdir = &vf->fdir;
2315 	enum virtchnl_status_code v_ret;
2316 	struct ice_ntuple_fltr *input;
2317 	enum ice_fltr_ptype flow;
2318 	struct device *dev;
2319 	struct ice_pf *pf;
2320 	int is_tun = 0;
2321 	int len = 0;
2322 	int ret;
2323 
2324 	pf = vf->pf;
2325 	dev = ice_pf_to_dev(pf);
2326 	ret = ice_vc_fdir_param_check(vf, fltr->vsi_id);
2327 	if (ret) {
2328 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
2329 		dev_dbg(dev, "Parameter check for VF %d failed\n", vf->vf_id);
2330 		goto err_exit;
2331 	}
2332 
2333 	stat = kzalloc_obj(*stat);
2334 	if (!stat) {
2335 		v_ret = VIRTCHNL_STATUS_ERR_NO_MEMORY;
2336 		dev_dbg(dev, "Alloc stat for VF %d failed\n", vf->vf_id);
2337 		goto err_exit;
2338 	}
2339 
2340 	len = sizeof(*stat);
2341 
2342 	conf = ice_vc_fdir_lookup_entry(vf, fltr->flow_id);
2343 	if (!conf) {
2344 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2345 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_NONEXIST;
2346 		dev_dbg(dev, "VF %d: FDIR invalid flow_id:0x%X\n",
2347 			vf->vf_id, fltr->flow_id);
2348 		goto err_exit;
2349 	}
2350 
2351 	/* Just return failure when ctrl_vsi idx is invalid */
2352 	if (vf->ctrl_vsi_idx == ICE_NO_VSI) {
2353 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2354 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
2355 		dev_err(dev, "Invalid FDIR ctrl_vsi for VF %d\n", vf->vf_id);
2356 		goto err_exit;
2357 	}
2358 
2359 	ret = ice_vc_fdir_set_irq_ctx(vf, conf, VIRTCHNL_OP_DEL_FDIR_FILTER);
2360 	if (ret) {
2361 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2362 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
2363 		dev_dbg(dev, "VF %d: set FDIR context failed\n", vf->vf_id);
2364 		goto err_exit;
2365 	}
2366 
2367 	/* For raw FDIR filters created by the parser */
2368 	if (conf->parser_ena) {
2369 		ret = ice_vc_del_fdir_raw(vf, conf, &v_ret, stat, len);
2370 		if (ret)
2371 			goto err_del_tmr;
2372 		goto exit;
2373 	}
2374 
2375 	is_tun = ice_fdir_is_tunnel(conf->ttype);
2376 	ret = ice_vc_fdir_write_fltr(vf, conf, false, is_tun);
2377 	if (ret) {
2378 		v_ret = VIRTCHNL_STATUS_SUCCESS;
2379 		stat->status = VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE;
2380 		dev_err(dev, "VF %d: writing FDIR rule failed, ret:%d\n",
2381 			vf->vf_id, ret);
2382 		goto err_del_tmr;
2383 	}
2384 
2385 	/* Remove unused profiles to avoid unexpected behaviors */
2386 	input = &conf->input;
2387 	flow = input->flow_type;
2388 	if (fdir->fdir_fltr_cnt[flow][is_tun] == 1)
2389 		ice_vc_fdir_rem_prof(vf, flow, is_tun);
2390 
2391 exit:
2392 	kfree(stat);
2393 
2394 	return ret;
2395 
2396 err_del_tmr:
2397 	ice_vc_fdir_clear_irq_ctx(vf);
2398 err_exit:
2399 	ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_FDIR_FILTER, v_ret,
2400 				    (u8 *)stat, len);
2401 	kfree(stat);
2402 	return ret;
2403 }
2404 
2405 /**
2406  * ice_vf_fdir_init - init FDIR resource for VF
2407  * @vf: pointer to the VF info
2408  */
2409 void ice_vf_fdir_init(struct ice_vf *vf)
2410 {
2411 	struct ice_vf_fdir *fdir = &vf->fdir;
2412 
2413 	idr_init(&fdir->fdir_rule_idr);
2414 	INIT_LIST_HEAD(&fdir->fdir_rule_list);
2415 
2416 	spin_lock_init(&fdir->ctx_lock);
2417 	fdir->ctx_irq.flags = 0;
2418 	fdir->ctx_done.flags = 0;
2419 	ice_vc_fdir_reset_cnt_all(fdir);
2420 }
2421 
2422 /**
2423  * ice_vf_fdir_exit - destroy FDIR resource for VF
2424  * @vf: pointer to the VF info
2425  */
2426 void ice_vf_fdir_exit(struct ice_vf *vf)
2427 {
2428 	ice_vc_fdir_flush_entry(vf);
2429 	idr_destroy(&vf->fdir.fdir_rule_idr);
2430 	ice_vc_fdir_rem_prof_all(vf);
2431 	ice_vc_fdir_free_prof_all(vf);
2432 }
2433