xref: /linux/drivers/net/ethernet/intel/ice/ice_tc_lib.c (revision 9175e1d6d2f0d4f353682833f78188cb35b80e4d)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2021, Intel Corporation. */
3 
4 #include "ice.h"
5 #include "ice_tc_lib.h"
6 #include "ice_fltr.h"
7 #include "ice_lib.h"
8 #include "ice_protocol_type.h"
9 
10 #define ICE_TC_METADATA_LKUP_IDX 0
11 
12 /**
13  * ice_tc_count_lkups - determine lookup count for switch filter
14  * @flags: TC-flower flags
15  * @fltr: Pointer to outer TC filter structure
16  *
17  * Return: lookup count based on TC flower input for a switch filter.
18  */
19 static int ice_tc_count_lkups(u32 flags, struct ice_tc_flower_fltr *fltr)
20 {
21 	int lkups_cnt = 1; /* 0th lookup is metadata */
22 
23 	/* Always add metadata as the 0th lookup. Included elements:
24 	 * - Direction flag (always present)
25 	 * - ICE_TC_FLWR_FIELD_VLAN_TPID (present if specified)
26 	 * - Tunnel flag (present if tunnel)
27 	 */
28 	if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS)
29 		lkups_cnt++;
30 
31 	if (flags & ICE_TC_FLWR_FIELD_TENANT_ID)
32 		lkups_cnt++;
33 
34 	if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC)
35 		lkups_cnt++;
36 
37 	if (flags & ICE_TC_FLWR_FIELD_GTP_OPTS)
38 		lkups_cnt++;
39 
40 	if (flags & ICE_TC_FLWR_FIELD_PFCP_OPTS)
41 		lkups_cnt++;
42 
43 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
44 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
45 		     ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
46 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV6))
47 		lkups_cnt++;
48 
49 	if (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
50 		     ICE_TC_FLWR_FIELD_ENC_IP_TTL))
51 		lkups_cnt++;
52 
53 	if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT)
54 		lkups_cnt++;
55 
56 	if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID)
57 		lkups_cnt++;
58 
59 	/* are MAC fields specified? */
60 	if (flags & (ICE_TC_FLWR_FIELD_DST_MAC | ICE_TC_FLWR_FIELD_SRC_MAC))
61 		lkups_cnt++;
62 
63 	/* is VLAN specified? */
64 	if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO))
65 		lkups_cnt++;
66 
67 	/* is CVLAN specified? */
68 	if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO))
69 		lkups_cnt++;
70 
71 	/* are PPPoE options specified? */
72 	if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
73 		     ICE_TC_FLWR_FIELD_PPP_PROTO))
74 		lkups_cnt++;
75 
76 	/* are IPv[4|6] fields specified? */
77 	if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 | ICE_TC_FLWR_FIELD_SRC_IPV4 |
78 		     ICE_TC_FLWR_FIELD_DEST_IPV6 | ICE_TC_FLWR_FIELD_SRC_IPV6))
79 		lkups_cnt++;
80 
81 	if (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))
82 		lkups_cnt++;
83 
84 	/* are L2TPv3 options specified? */
85 	if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID)
86 		lkups_cnt++;
87 
88 	/* is L4 (TCP/UDP/any other L4 protocol fields) specified? */
89 	if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
90 		     ICE_TC_FLWR_FIELD_SRC_L4_PORT))
91 		lkups_cnt++;
92 
93 	return lkups_cnt;
94 }
95 
96 static enum ice_protocol_type ice_proto_type_from_mac(bool inner)
97 {
98 	return inner ? ICE_MAC_IL : ICE_MAC_OFOS;
99 }
100 
101 static enum ice_protocol_type ice_proto_type_from_etype(bool inner)
102 {
103 	return inner ? ICE_ETYPE_IL : ICE_ETYPE_OL;
104 }
105 
106 static enum ice_protocol_type ice_proto_type_from_ipv4(bool inner)
107 {
108 	return inner ? ICE_IPV4_IL : ICE_IPV4_OFOS;
109 }
110 
111 static enum ice_protocol_type ice_proto_type_from_ipv6(bool inner)
112 {
113 	return inner ? ICE_IPV6_IL : ICE_IPV6_OFOS;
114 }
115 
116 static enum ice_protocol_type ice_proto_type_from_l4_port(u16 ip_proto)
117 {
118 	switch (ip_proto) {
119 	case IPPROTO_TCP:
120 		return ICE_TCP_IL;
121 	case IPPROTO_UDP:
122 		return ICE_UDP_ILOS;
123 	}
124 
125 	return 0;
126 }
127 
128 static enum ice_protocol_type
129 ice_proto_type_from_tunnel(enum ice_tunnel_type type)
130 {
131 	switch (type) {
132 	case TNL_VXLAN:
133 		return ICE_VXLAN;
134 	case TNL_GENEVE:
135 		return ICE_GENEVE;
136 	case TNL_GRETAP:
137 		return ICE_NVGRE;
138 	case TNL_GTPU:
139 		/* NO_PAY profiles will not work with GTP-U */
140 		return ICE_GTP;
141 	case TNL_GTPC:
142 		return ICE_GTP_NO_PAY;
143 	case TNL_PFCP:
144 		return ICE_PFCP;
145 	default:
146 		return 0;
147 	}
148 }
149 
150 static enum ice_sw_tunnel_type
151 ice_sw_type_from_tunnel(enum ice_tunnel_type type)
152 {
153 	switch (type) {
154 	case TNL_VXLAN:
155 		return ICE_SW_TUN_VXLAN;
156 	case TNL_GENEVE:
157 		return ICE_SW_TUN_GENEVE;
158 	case TNL_GRETAP:
159 		return ICE_SW_TUN_NVGRE;
160 	case TNL_GTPU:
161 		return ICE_SW_TUN_GTPU;
162 	case TNL_GTPC:
163 		return ICE_SW_TUN_GTPC;
164 	case TNL_PFCP:
165 		return ICE_SW_TUN_PFCP;
166 	default:
167 		return ICE_NON_TUN;
168 	}
169 }
170 
171 static u16 ice_check_supported_vlan_tpid(u16 vlan_tpid)
172 {
173 	switch (vlan_tpid) {
174 	case ETH_P_8021Q:
175 	case ETH_P_8021AD:
176 	case ETH_P_QINQ1:
177 		return vlan_tpid;
178 	default:
179 		return 0;
180 	}
181 }
182 
183 static int
184 ice_tc_fill_tunnel_outer(u32 flags, struct ice_tc_flower_fltr *fltr,
185 			 struct ice_adv_lkup_elem *list, int i)
186 {
187 	struct ice_tc_flower_lyr_2_4_hdrs *hdr = &fltr->outer_headers;
188 
189 	if (flags & ICE_TC_FLWR_FIELD_TENANT_ID) {
190 		u32 tenant_id;
191 
192 		list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
193 		switch (fltr->tunnel_type) {
194 		case TNL_VXLAN:
195 		case TNL_GENEVE:
196 			tenant_id = be32_to_cpu(fltr->tenant_id) << 8;
197 			list[i].h_u.tnl_hdr.vni = cpu_to_be32(tenant_id);
198 			memcpy(&list[i].m_u.tnl_hdr.vni, "\xff\xff\xff\x00", 4);
199 			i++;
200 			break;
201 		case TNL_GRETAP:
202 			list[i].h_u.nvgre_hdr.tni_flow = fltr->tenant_id;
203 			memcpy(&list[i].m_u.nvgre_hdr.tni_flow,
204 			       "\xff\xff\xff\xff", 4);
205 			i++;
206 			break;
207 		case TNL_GTPC:
208 		case TNL_GTPU:
209 			list[i].h_u.gtp_hdr.teid = fltr->tenant_id;
210 			memcpy(&list[i].m_u.gtp_hdr.teid,
211 			       "\xff\xff\xff\xff", 4);
212 			i++;
213 			break;
214 		default:
215 			break;
216 		}
217 	}
218 
219 	if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC) {
220 		list[i].type = ice_proto_type_from_mac(false);
221 		ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
222 				hdr->l2_key.dst_mac);
223 		ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
224 				hdr->l2_mask.dst_mac);
225 		i++;
226 	}
227 
228 	if (flags & ICE_TC_FLWR_FIELD_GTP_OPTS) {
229 		list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
230 
231 		if (fltr->gtp_pdu_info_masks.pdu_type) {
232 			list[i].h_u.gtp_hdr.pdu_type =
233 				fltr->gtp_pdu_info_keys.pdu_type << 4;
234 			memcpy(&list[i].m_u.gtp_hdr.pdu_type, "\xf0", 1);
235 		}
236 
237 		if (fltr->gtp_pdu_info_masks.qfi) {
238 			list[i].h_u.gtp_hdr.qfi = fltr->gtp_pdu_info_keys.qfi;
239 			memcpy(&list[i].m_u.gtp_hdr.qfi, "\x3f", 1);
240 		}
241 
242 		i++;
243 	}
244 
245 	if (flags & ICE_TC_FLWR_FIELD_PFCP_OPTS) {
246 		struct ice_pfcp_hdr *hdr_h, *hdr_m;
247 
248 		hdr_h = &list[i].h_u.pfcp_hdr;
249 		hdr_m = &list[i].m_u.pfcp_hdr;
250 		list[i].type = ICE_PFCP;
251 
252 		hdr_h->flags = fltr->pfcp_meta_keys.type;
253 		hdr_m->flags = fltr->pfcp_meta_masks.type & 0x01;
254 
255 		hdr_h->seid = fltr->pfcp_meta_keys.seid;
256 		hdr_m->seid = fltr->pfcp_meta_masks.seid;
257 
258 		i++;
259 	}
260 
261 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
262 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV4)) {
263 		list[i].type = ice_proto_type_from_ipv4(false);
264 
265 		if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV4) {
266 			list[i].h_u.ipv4_hdr.src_addr = hdr->l3_key.src_ipv4;
267 			list[i].m_u.ipv4_hdr.src_addr = hdr->l3_mask.src_ipv4;
268 		}
269 		if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV4) {
270 			list[i].h_u.ipv4_hdr.dst_addr = hdr->l3_key.dst_ipv4;
271 			list[i].m_u.ipv4_hdr.dst_addr = hdr->l3_mask.dst_ipv4;
272 		}
273 		i++;
274 	}
275 
276 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
277 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV6)) {
278 		list[i].type = ice_proto_type_from_ipv6(false);
279 
280 		if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV6) {
281 			memcpy(&list[i].h_u.ipv6_hdr.src_addr,
282 			       &hdr->l3_key.src_ipv6_addr,
283 			       sizeof(hdr->l3_key.src_ipv6_addr));
284 			memcpy(&list[i].m_u.ipv6_hdr.src_addr,
285 			       &hdr->l3_mask.src_ipv6_addr,
286 			       sizeof(hdr->l3_mask.src_ipv6_addr));
287 		}
288 		if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV6) {
289 			memcpy(&list[i].h_u.ipv6_hdr.dst_addr,
290 			       &hdr->l3_key.dst_ipv6_addr,
291 			       sizeof(hdr->l3_key.dst_ipv6_addr));
292 			memcpy(&list[i].m_u.ipv6_hdr.dst_addr,
293 			       &hdr->l3_mask.dst_ipv6_addr,
294 			       sizeof(hdr->l3_mask.dst_ipv6_addr));
295 		}
296 		i++;
297 	}
298 
299 	if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IP) &&
300 	    (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
301 		      ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
302 		list[i].type = ice_proto_type_from_ipv4(false);
303 
304 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
305 			list[i].h_u.ipv4_hdr.tos = hdr->l3_key.tos;
306 			list[i].m_u.ipv4_hdr.tos = hdr->l3_mask.tos;
307 		}
308 
309 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
310 			list[i].h_u.ipv4_hdr.time_to_live = hdr->l3_key.ttl;
311 			list[i].m_u.ipv4_hdr.time_to_live = hdr->l3_mask.ttl;
312 		}
313 
314 		i++;
315 	}
316 
317 	if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IPV6) &&
318 	    (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
319 		      ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
320 		struct ice_ipv6_hdr *hdr_h, *hdr_m;
321 
322 		hdr_h = &list[i].h_u.ipv6_hdr;
323 		hdr_m = &list[i].m_u.ipv6_hdr;
324 		list[i].type = ice_proto_type_from_ipv6(false);
325 
326 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
327 			be32p_replace_bits(&hdr_h->be_ver_tc_flow,
328 					   hdr->l3_key.tos,
329 					   ICE_IPV6_HDR_TC_MASK);
330 			be32p_replace_bits(&hdr_m->be_ver_tc_flow,
331 					   hdr->l3_mask.tos,
332 					   ICE_IPV6_HDR_TC_MASK);
333 		}
334 
335 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
336 			hdr_h->hop_limit = hdr->l3_key.ttl;
337 			hdr_m->hop_limit = hdr->l3_mask.ttl;
338 		}
339 
340 		i++;
341 	}
342 
343 	if ((flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT) &&
344 	    hdr->l3_key.ip_proto == IPPROTO_UDP) {
345 		list[i].type = ICE_UDP_OF;
346 		list[i].h_u.l4_hdr.dst_port = hdr->l4_key.dst_port;
347 		list[i].m_u.l4_hdr.dst_port = hdr->l4_mask.dst_port;
348 		i++;
349 	}
350 
351 	/* always fill matching on tunneled packets in metadata */
352 	ice_rule_add_tunnel_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
353 
354 	return i;
355 }
356 
357 /**
358  * ice_tc_fill_rules - fill filter rules based on TC fltr
359  * @hw: pointer to HW structure
360  * @flags: tc flower field flags
361  * @tc_fltr: pointer to TC flower filter
362  * @list: list of advance rule elements
363  * @rule_info: pointer to information about rule
364  * @l4_proto: pointer to information such as L4 proto type
365  *
366  * Fill ice_adv_lkup_elem list based on TC flower flags and
367  * TC flower headers. This list should be used to add
368  * advance filter in hardware.
369  */
370 static int
371 ice_tc_fill_rules(struct ice_hw *hw, u32 flags,
372 		  struct ice_tc_flower_fltr *tc_fltr,
373 		  struct ice_adv_lkup_elem *list,
374 		  struct ice_adv_rule_info *rule_info,
375 		  u16 *l4_proto)
376 {
377 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
378 	bool inner = false;
379 	u16 vlan_tpid = 0;
380 	int i = 1; /* 0th lookup is metadata */
381 
382 	rule_info->vlan_type = vlan_tpid;
383 
384 	/* Always add direction metadata */
385 	ice_rule_add_direction_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
386 
387 	if (tc_fltr->direction == ICE_ESWITCH_FLTR_EGRESS) {
388 		ice_rule_add_src_vsi_metadata(&list[i]);
389 		i++;
390 	}
391 
392 	rule_info->tun_type = ice_sw_type_from_tunnel(tc_fltr->tunnel_type);
393 	if (tc_fltr->tunnel_type != TNL_LAST) {
394 		i = ice_tc_fill_tunnel_outer(flags, tc_fltr, list, i);
395 
396 		/* PFCP is considered non-tunneled - don't swap headers. */
397 		if (tc_fltr->tunnel_type != TNL_PFCP) {
398 			headers = &tc_fltr->inner_headers;
399 			inner = true;
400 		}
401 	}
402 
403 	if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID) {
404 		list[i].type = ice_proto_type_from_etype(inner);
405 		list[i].h_u.ethertype.ethtype_id = headers->l2_key.n_proto;
406 		list[i].m_u.ethertype.ethtype_id = headers->l2_mask.n_proto;
407 		i++;
408 	}
409 
410 	if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
411 		     ICE_TC_FLWR_FIELD_SRC_MAC)) {
412 		struct ice_tc_l2_hdr *l2_key, *l2_mask;
413 
414 		l2_key = &headers->l2_key;
415 		l2_mask = &headers->l2_mask;
416 
417 		list[i].type = ice_proto_type_from_mac(inner);
418 		if (flags & ICE_TC_FLWR_FIELD_DST_MAC) {
419 			ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
420 					l2_key->dst_mac);
421 			ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
422 					l2_mask->dst_mac);
423 		}
424 		if (flags & ICE_TC_FLWR_FIELD_SRC_MAC) {
425 			ether_addr_copy(list[i].h_u.eth_hdr.src_addr,
426 					l2_key->src_mac);
427 			ether_addr_copy(list[i].m_u.eth_hdr.src_addr,
428 					l2_mask->src_mac);
429 		}
430 		i++;
431 	}
432 
433 	/* copy VLAN info */
434 	if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO)) {
435 		if (flags & ICE_TC_FLWR_FIELD_CVLAN)
436 			list[i].type = ICE_VLAN_EX;
437 		else
438 			list[i].type = ICE_VLAN_OFOS;
439 
440 		if (flags & ICE_TC_FLWR_FIELD_VLAN) {
441 			list[i].h_u.vlan_hdr.vlan = headers->vlan_hdr.vlan_id;
442 			list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
443 		}
444 
445 		if (flags & ICE_TC_FLWR_FIELD_VLAN_PRIO) {
446 			if (flags & ICE_TC_FLWR_FIELD_VLAN) {
447 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
448 			} else {
449 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
450 				list[i].h_u.vlan_hdr.vlan = 0;
451 			}
452 			list[i].h_u.vlan_hdr.vlan |=
453 				headers->vlan_hdr.vlan_prio;
454 		}
455 
456 		i++;
457 	}
458 
459 	if (flags & ICE_TC_FLWR_FIELD_VLAN_TPID) {
460 		vlan_tpid = be16_to_cpu(headers->vlan_hdr.vlan_tpid);
461 		rule_info->vlan_type =
462 				ice_check_supported_vlan_tpid(vlan_tpid);
463 
464 		ice_rule_add_vlan_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
465 	}
466 
467 	if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO)) {
468 		list[i].type = ICE_VLAN_IN;
469 
470 		if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
471 			list[i].h_u.vlan_hdr.vlan = headers->cvlan_hdr.vlan_id;
472 			list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
473 		}
474 
475 		if (flags & ICE_TC_FLWR_FIELD_CVLAN_PRIO) {
476 			if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
477 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
478 			} else {
479 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
480 				list[i].h_u.vlan_hdr.vlan = 0;
481 			}
482 			list[i].h_u.vlan_hdr.vlan |=
483 				headers->cvlan_hdr.vlan_prio;
484 		}
485 
486 		i++;
487 	}
488 
489 	if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
490 		     ICE_TC_FLWR_FIELD_PPP_PROTO)) {
491 		struct ice_pppoe_hdr *vals, *masks;
492 
493 		vals = &list[i].h_u.pppoe_hdr;
494 		masks = &list[i].m_u.pppoe_hdr;
495 
496 		list[i].type = ICE_PPPOE;
497 
498 		if (flags & ICE_TC_FLWR_FIELD_PPPOE_SESSID) {
499 			vals->session_id = headers->pppoe_hdr.session_id;
500 			masks->session_id = cpu_to_be16(0xFFFF);
501 		}
502 
503 		if (flags & ICE_TC_FLWR_FIELD_PPP_PROTO) {
504 			vals->ppp_prot_id = headers->pppoe_hdr.ppp_proto;
505 			masks->ppp_prot_id = cpu_to_be16(0xFFFF);
506 		}
507 
508 		i++;
509 	}
510 
511 	/* copy L3 (IPv[4|6]: src, dest) address */
512 	if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 |
513 		     ICE_TC_FLWR_FIELD_SRC_IPV4)) {
514 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
515 
516 		list[i].type = ice_proto_type_from_ipv4(inner);
517 		l3_key = &headers->l3_key;
518 		l3_mask = &headers->l3_mask;
519 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV4) {
520 			list[i].h_u.ipv4_hdr.dst_addr = l3_key->dst_ipv4;
521 			list[i].m_u.ipv4_hdr.dst_addr = l3_mask->dst_ipv4;
522 		}
523 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV4) {
524 			list[i].h_u.ipv4_hdr.src_addr = l3_key->src_ipv4;
525 			list[i].m_u.ipv4_hdr.src_addr = l3_mask->src_ipv4;
526 		}
527 		i++;
528 	} else if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV6 |
529 			    ICE_TC_FLWR_FIELD_SRC_IPV6)) {
530 		struct ice_ipv6_hdr *ipv6_hdr, *ipv6_mask;
531 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
532 
533 		list[i].type = ice_proto_type_from_ipv6(inner);
534 		ipv6_hdr = &list[i].h_u.ipv6_hdr;
535 		ipv6_mask = &list[i].m_u.ipv6_hdr;
536 		l3_key = &headers->l3_key;
537 		l3_mask = &headers->l3_mask;
538 
539 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV6) {
540 			memcpy(&ipv6_hdr->dst_addr, &l3_key->dst_ipv6_addr,
541 			       sizeof(l3_key->dst_ipv6_addr));
542 			memcpy(&ipv6_mask->dst_addr, &l3_mask->dst_ipv6_addr,
543 			       sizeof(l3_mask->dst_ipv6_addr));
544 		}
545 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV6) {
546 			memcpy(&ipv6_hdr->src_addr, &l3_key->src_ipv6_addr,
547 			       sizeof(l3_key->src_ipv6_addr));
548 			memcpy(&ipv6_mask->src_addr, &l3_mask->src_ipv6_addr,
549 			       sizeof(l3_mask->src_ipv6_addr));
550 		}
551 		i++;
552 	}
553 
554 	if (headers->l2_key.n_proto == htons(ETH_P_IP) &&
555 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
556 		list[i].type = ice_proto_type_from_ipv4(inner);
557 
558 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
559 			list[i].h_u.ipv4_hdr.tos = headers->l3_key.tos;
560 			list[i].m_u.ipv4_hdr.tos = headers->l3_mask.tos;
561 		}
562 
563 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
564 			list[i].h_u.ipv4_hdr.time_to_live =
565 				headers->l3_key.ttl;
566 			list[i].m_u.ipv4_hdr.time_to_live =
567 				headers->l3_mask.ttl;
568 		}
569 
570 		i++;
571 	}
572 
573 	if (headers->l2_key.n_proto == htons(ETH_P_IPV6) &&
574 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
575 		struct ice_ipv6_hdr *hdr_h, *hdr_m;
576 
577 		hdr_h = &list[i].h_u.ipv6_hdr;
578 		hdr_m = &list[i].m_u.ipv6_hdr;
579 		list[i].type = ice_proto_type_from_ipv6(inner);
580 
581 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
582 			be32p_replace_bits(&hdr_h->be_ver_tc_flow,
583 					   headers->l3_key.tos,
584 					   ICE_IPV6_HDR_TC_MASK);
585 			be32p_replace_bits(&hdr_m->be_ver_tc_flow,
586 					   headers->l3_mask.tos,
587 					   ICE_IPV6_HDR_TC_MASK);
588 		}
589 
590 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
591 			hdr_h->hop_limit = headers->l3_key.ttl;
592 			hdr_m->hop_limit = headers->l3_mask.ttl;
593 		}
594 
595 		i++;
596 	}
597 
598 	if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID) {
599 		list[i].type = ICE_L2TPV3;
600 
601 		list[i].h_u.l2tpv3_sess_hdr.session_id =
602 			headers->l2tpv3_hdr.session_id;
603 		list[i].m_u.l2tpv3_sess_hdr.session_id =
604 			cpu_to_be32(0xFFFFFFFF);
605 
606 		i++;
607 	}
608 
609 	/* copy L4 (src, dest) port */
610 	if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
611 		     ICE_TC_FLWR_FIELD_SRC_L4_PORT)) {
612 		struct ice_tc_l4_hdr *l4_key, *l4_mask;
613 
614 		list[i].type = ice_proto_type_from_l4_port(headers->l3_key.ip_proto);
615 		l4_key = &headers->l4_key;
616 		l4_mask = &headers->l4_mask;
617 
618 		if (flags & ICE_TC_FLWR_FIELD_DEST_L4_PORT) {
619 			list[i].h_u.l4_hdr.dst_port = l4_key->dst_port;
620 			list[i].m_u.l4_hdr.dst_port = l4_mask->dst_port;
621 		}
622 		if (flags & ICE_TC_FLWR_FIELD_SRC_L4_PORT) {
623 			list[i].h_u.l4_hdr.src_port = l4_key->src_port;
624 			list[i].m_u.l4_hdr.src_port = l4_mask->src_port;
625 		}
626 		i++;
627 	}
628 
629 	return i;
630 }
631 
632 /**
633  * ice_tc_tun_get_type - get the tunnel type
634  * @tunnel_dev: ptr to tunnel device
635  *
636  * This function detects appropriate tunnel_type if specified device is
637  * tunnel device such as VXLAN/Geneve
638  */
639 static int ice_tc_tun_get_type(struct net_device *tunnel_dev)
640 {
641 	if (netif_is_vxlan(tunnel_dev))
642 		return TNL_VXLAN;
643 	if (netif_is_geneve(tunnel_dev))
644 		return TNL_GENEVE;
645 	if (netif_is_gretap(tunnel_dev) ||
646 	    netif_is_ip6gretap(tunnel_dev))
647 		return TNL_GRETAP;
648 
649 	/* Assume GTP-U by default in case of GTP netdev.
650 	 * GTP-C may be selected later, based on enc_dst_port.
651 	 */
652 	if (netif_is_gtp(tunnel_dev))
653 		return TNL_GTPU;
654 	if (netif_is_pfcp(tunnel_dev))
655 		return TNL_PFCP;
656 	return TNL_LAST;
657 }
658 
659 bool ice_is_tunnel_supported(struct net_device *dev)
660 {
661 	return ice_tc_tun_get_type(dev) != TNL_LAST;
662 }
663 
664 static bool ice_tc_is_dev_uplink(struct net_device *dev)
665 {
666 	return netif_is_ice(dev) || ice_is_tunnel_supported(dev);
667 }
668 
669 static int ice_tc_setup_action(struct net_device *filter_dev,
670 			       struct ice_tc_flower_fltr *fltr,
671 			       struct net_device *target_dev,
672 			       enum ice_sw_fwd_act_type action)
673 {
674 	struct ice_repr *repr;
675 
676 	if (action != ICE_FWD_TO_VSI && action != ICE_MIRROR_PACKET) {
677 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action to setup provided");
678 		return -EINVAL;
679 	}
680 
681 	fltr->action.fltr_act = action;
682 
683 	if (ice_is_port_repr_netdev(filter_dev) &&
684 	    ice_is_port_repr_netdev(target_dev) &&
685 	    fltr->direction == ICE_ESWITCH_FLTR_EGRESS) {
686 		repr = ice_netdev_to_repr(target_dev);
687 
688 		fltr->dest_vsi = repr->src_vsi;
689 	} else if (ice_is_port_repr_netdev(filter_dev) &&
690 		   ice_tc_is_dev_uplink(target_dev) &&
691 		   fltr->direction == ICE_ESWITCH_FLTR_EGRESS) {
692 		repr = ice_netdev_to_repr(filter_dev);
693 
694 		fltr->dest_vsi = repr->src_vsi->back->eswitch.uplink_vsi;
695 	} else if (ice_tc_is_dev_uplink(filter_dev) &&
696 		   ice_is_port_repr_netdev(target_dev) &&
697 		   fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
698 		repr = ice_netdev_to_repr(target_dev);
699 
700 		fltr->dest_vsi = repr->src_vsi;
701 	} else {
702 		NL_SET_ERR_MSG_MOD(fltr->extack,
703 				   "The action is not supported for this netdevice");
704 		return -EINVAL;
705 	}
706 
707 	return 0;
708 }
709 
710 static int
711 ice_tc_setup_drop_action(struct net_device *filter_dev,
712 			 struct ice_tc_flower_fltr *fltr)
713 {
714 	fltr->action.fltr_act = ICE_DROP_PACKET;
715 
716 	if (!ice_tc_is_dev_uplink(filter_dev) &&
717 	    !(ice_is_port_repr_netdev(filter_dev) &&
718 	      fltr->direction == ICE_ESWITCH_FLTR_INGRESS)) {
719 		NL_SET_ERR_MSG_MOD(fltr->extack,
720 				   "The action is not supported for this netdevice");
721 		return -EINVAL;
722 	}
723 
724 	return 0;
725 }
726 
727 static int ice_eswitch_tc_parse_action(struct net_device *filter_dev,
728 				       struct ice_tc_flower_fltr *fltr,
729 				       struct flow_action_entry *act)
730 {
731 	int err;
732 
733 	switch (act->id) {
734 	case FLOW_ACTION_DROP:
735 		err = ice_tc_setup_drop_action(filter_dev, fltr);
736 		if (err)
737 			return err;
738 
739 		break;
740 
741 	case FLOW_ACTION_REDIRECT:
742 		err = ice_tc_setup_action(filter_dev, fltr,
743 					  act->dev, ICE_FWD_TO_VSI);
744 		if (err)
745 			return err;
746 
747 		break;
748 
749 	case FLOW_ACTION_MIRRED:
750 		err = ice_tc_setup_action(filter_dev, fltr,
751 					  act->dev, ICE_MIRROR_PACKET);
752 		if (err)
753 			return err;
754 
755 		break;
756 
757 	default:
758 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action in switchdev mode");
759 		return -EINVAL;
760 	}
761 
762 	return 0;
763 }
764 
765 static bool ice_is_fltr_lldp(struct ice_tc_flower_fltr *fltr)
766 {
767 	return fltr->outer_headers.l2_key.n_proto == htons(ETH_P_LLDP);
768 }
769 
770 static bool ice_is_fltr_pf_tx_lldp(struct ice_tc_flower_fltr *fltr)
771 {
772 	struct ice_vsi *vsi = fltr->src_vsi, *uplink;
773 
774 	if (!ice_is_switchdev_running(vsi->back))
775 		return false;
776 
777 	uplink = vsi->back->eswitch.uplink_vsi;
778 	return vsi == uplink && fltr->action.fltr_act == ICE_DROP_PACKET &&
779 	       ice_is_fltr_lldp(fltr) &&
780 	       fltr->direction == ICE_ESWITCH_FLTR_EGRESS &&
781 	       fltr->flags == ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
782 }
783 
784 static bool ice_is_fltr_vf_tx_lldp(struct ice_tc_flower_fltr *fltr)
785 {
786 	struct ice_vsi *vsi = fltr->src_vsi, *uplink;
787 
788 	uplink = vsi->back->eswitch.uplink_vsi;
789 	return fltr->src_vsi->type == ICE_VSI_VF && ice_is_fltr_lldp(fltr) &&
790 	       fltr->direction == ICE_ESWITCH_FLTR_EGRESS &&
791 	       fltr->dest_vsi == uplink;
792 }
793 
794 static struct ice_tc_flower_fltr *
795 ice_find_pf_tx_lldp_fltr(struct ice_pf *pf)
796 {
797 	struct ice_tc_flower_fltr *fltr;
798 
799 	hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
800 		if (ice_is_fltr_pf_tx_lldp(fltr))
801 			return fltr;
802 
803 	return NULL;
804 }
805 
806 static bool ice_any_vf_lldp_tx_ena(struct ice_pf *pf)
807 {
808 	struct ice_vf *vf;
809 	unsigned int bkt;
810 
811 	ice_for_each_vf(pf, bkt, vf)
812 		if (vf->lldp_tx_ena)
813 			return true;
814 
815 	return false;
816 }
817 
818 int ice_pass_vf_tx_lldp(struct ice_vsi *vsi, bool deinit)
819 {
820 	struct ice_rule_query_data remove_entry = {
821 		.rid = vsi->vf->lldp_recipe_id,
822 		.rule_id = vsi->vf->lldp_rule_id,
823 		.vsi_handle = vsi->idx,
824 	};
825 	struct ice_pf *pf = vsi->back;
826 	int err;
827 
828 	if (vsi->vf->lldp_tx_ena)
829 		return 0;
830 
831 	if (!deinit && !ice_find_pf_tx_lldp_fltr(vsi->back))
832 		return -EINVAL;
833 
834 	if (!deinit && ice_any_vf_lldp_tx_ena(pf))
835 		return -EINVAL;
836 
837 	err = ice_rem_adv_rule_by_id(&pf->hw, &remove_entry);
838 	if (!err)
839 		vsi->vf->lldp_tx_ena = true;
840 
841 	return err;
842 }
843 
844 int ice_drop_vf_tx_lldp(struct ice_vsi *vsi, bool init)
845 {
846 	struct ice_rule_query_data rule_added;
847 	struct ice_adv_rule_info rinfo = {
848 		.priority = 7,
849 		.src_vsi = vsi->idx,
850 		.sw_act = {
851 			.src = vsi->idx,
852 			.flag = ICE_FLTR_TX,
853 			.fltr_act = ICE_DROP_PACKET,
854 			.vsi_handle = vsi->idx,
855 		},
856 		.flags_info.act_valid = true,
857 	};
858 	struct ice_adv_lkup_elem list[3];
859 	struct ice_pf *pf = vsi->back;
860 	int err;
861 
862 	if (!init && !vsi->vf->lldp_tx_ena)
863 		return 0;
864 
865 	memset(list, 0, sizeof(list));
866 	ice_rule_add_direction_metadata(&list[0]);
867 	ice_rule_add_src_vsi_metadata(&list[1]);
868 	list[2].type = ICE_ETYPE_OL;
869 	list[2].h_u.ethertype.ethtype_id = htons(ETH_P_LLDP);
870 	list[2].m_u.ethertype.ethtype_id = htons(0xFFFF);
871 
872 	err = ice_add_adv_rule(&pf->hw, list, ARRAY_SIZE(list), &rinfo,
873 			       &rule_added);
874 	if (err) {
875 		dev_err(&pf->pdev->dev,
876 			"Failed to add an LLDP rule to VSI 0x%X: %d\n",
877 			vsi->idx, err);
878 	} else {
879 		vsi->vf->lldp_recipe_id = rule_added.rid;
880 		vsi->vf->lldp_rule_id = rule_added.rule_id;
881 		vsi->vf->lldp_tx_ena = false;
882 	}
883 
884 	return err;
885 }
886 
887 static void ice_handle_add_pf_lldp_drop_rule(struct ice_vsi *vsi)
888 {
889 	struct ice_tc_flower_fltr *fltr;
890 	struct ice_pf *pf = vsi->back;
891 
892 	hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node) {
893 		if (!ice_is_fltr_vf_tx_lldp(fltr))
894 			continue;
895 		ice_pass_vf_tx_lldp(fltr->src_vsi, true);
896 		break;
897 	}
898 }
899 
900 static void ice_handle_del_pf_lldp_drop_rule(struct ice_pf *pf)
901 {
902 	int i;
903 
904 	/* Make the VF LLDP fwd to uplink rule dormant */
905 	ice_for_each_vsi(pf, i) {
906 		struct ice_vsi *vf_vsi = pf->vsi[i];
907 
908 		if (vf_vsi && vf_vsi->type == ICE_VSI_VF)
909 			ice_drop_vf_tx_lldp(vf_vsi, false);
910 	}
911 }
912 
913 static int
914 ice_eswitch_add_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
915 {
916 	struct ice_adv_rule_info rule_info = { 0 };
917 	struct ice_rule_query_data rule_added;
918 	struct ice_hw *hw = &vsi->back->hw;
919 	struct ice_adv_lkup_elem *list;
920 	u32 flags = fltr->flags;
921 	int lkups_cnt;
922 	int ret;
923 	int i;
924 
925 	if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT) {
926 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported encap field(s)");
927 		return -EOPNOTSUPP;
928 	}
929 
930 	if (ice_is_fltr_vf_tx_lldp(fltr))
931 		return ice_pass_vf_tx_lldp(vsi, false);
932 
933 	lkups_cnt = ice_tc_count_lkups(flags, fltr);
934 	list = kzalloc_objs(*list, lkups_cnt, GFP_ATOMIC);
935 	if (!list)
936 		return -ENOMEM;
937 
938 	i = ice_tc_fill_rules(hw, flags, fltr, list, &rule_info, NULL);
939 	if (i != lkups_cnt) {
940 		ret = -EINVAL;
941 		goto exit;
942 	}
943 
944 	rule_info.sw_act.fltr_act = fltr->action.fltr_act;
945 	if (fltr->action.fltr_act != ICE_DROP_PACKET)
946 		rule_info.sw_act.vsi_handle = fltr->dest_vsi->idx;
947 	/* For now, making priority to be highest, and it also becomes
948 	 * the priority for recipe which will get created as a result of
949 	 * new extraction sequence based on input set.
950 	 * Priority '7' is max val for switch recipe, higher the number
951 	 * results into order of switch rule evaluation.
952 	 */
953 	rule_info.priority = 7;
954 	rule_info.flags_info.act_valid = true;
955 
956 	if (fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
957 		/* Uplink to VF */
958 		rule_info.sw_act.flag |= ICE_FLTR_RX;
959 		rule_info.sw_act.src = hw->pf_id;
960 		rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
961 	} else if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS &&
962 		   !fltr->dest_vsi && vsi == vsi->back->eswitch.uplink_vsi) {
963 		/* PF to Uplink */
964 		rule_info.sw_act.flag |= ICE_FLTR_TX;
965 		rule_info.sw_act.src = vsi->idx;
966 	} else if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS &&
967 		   fltr->dest_vsi == vsi->back->eswitch.uplink_vsi) {
968 		/* VF to Uplink */
969 		rule_info.sw_act.flag |= ICE_FLTR_TX;
970 		rule_info.sw_act.src = vsi->idx;
971 		rule_info.flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
972 		/* This is a specific case. The destination VSI index is
973 		 * overwritten by the source VSI index. This type of filter
974 		 * should allow the packet to go to the LAN, not to the
975 		 * VSI passed here. It should set LAN_EN bit only. However,
976 		 * the VSI must be a valid one. Setting source VSI index
977 		 * here is safe. Even if the result from switch is set LAN_EN
978 		 * and LB_EN (which normally will pass the packet to this VSI)
979 		 * packet won't be seen on the VSI, because local loopback is
980 		 * turned off.
981 		 */
982 		rule_info.sw_act.vsi_handle = vsi->idx;
983 	} else {
984 		/* VF to VF */
985 		rule_info.sw_act.flag |= ICE_FLTR_TX;
986 		rule_info.sw_act.src = vsi->idx;
987 		rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
988 	}
989 
990 	/* specify the cookie as filter_rule_id */
991 	rule_info.fltr_rule_id = fltr->cookie;
992 	rule_info.src_vsi = vsi->idx;
993 
994 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
995 	if (ret == -EEXIST) {
996 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because it already exist");
997 		ret = -EINVAL;
998 		goto exit;
999 	} else if (ret == -ENOSPC) {
1000 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter: insufficient space available.");
1001 		goto exit;
1002 	} else if (ret) {
1003 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter due to error");
1004 		goto exit;
1005 	}
1006 
1007 	if (ice_is_fltr_pf_tx_lldp(fltr))
1008 		ice_handle_add_pf_lldp_drop_rule(vsi);
1009 
1010 	/* store the output params, which are needed later for removing
1011 	 * advanced switch filter
1012 	 */
1013 	fltr->rid = rule_added.rid;
1014 	fltr->rule_id = rule_added.rule_id;
1015 	fltr->dest_vsi_handle = rule_added.vsi_handle;
1016 
1017 exit:
1018 	kfree(list);
1019 	return ret;
1020 }
1021 
1022 /**
1023  * ice_locate_vsi_using_queue - locate VSI using queue (forward to queue action)
1024  * @vsi: Pointer to VSI
1025  * @queue: Queue index
1026  *
1027  * Locate the VSI using specified "queue". When ADQ is not enabled,
1028  * always return input VSI, otherwise locate corresponding
1029  * VSI based on per channel "offset" and "qcount"
1030  */
1031 struct ice_vsi *
1032 ice_locate_vsi_using_queue(struct ice_vsi *vsi, int queue)
1033 {
1034 	int num_tc, tc;
1035 
1036 	/* if ADQ is not active, passed VSI is the candidate VSI */
1037 	if (!ice_is_adq_active(vsi->back))
1038 		return vsi;
1039 
1040 	/* Locate the VSI (it could still be main PF VSI or CHNL_VSI depending
1041 	 * upon queue number)
1042 	 */
1043 	num_tc = vsi->mqprio_qopt.qopt.num_tc;
1044 
1045 	for (tc = 0; tc < num_tc; tc++) {
1046 		int qcount = vsi->mqprio_qopt.qopt.count[tc];
1047 		int offset = vsi->mqprio_qopt.qopt.offset[tc];
1048 
1049 		if (queue >= offset && queue < offset + qcount) {
1050 			/* for non-ADQ TCs, passed VSI is the candidate VSI */
1051 			if (tc < ICE_CHNL_START_TC)
1052 				return vsi;
1053 			else
1054 				return vsi->tc_map_vsi[tc];
1055 		}
1056 	}
1057 	return NULL;
1058 }
1059 
1060 static struct ice_rx_ring *
1061 ice_locate_rx_ring_using_queue(struct ice_vsi *vsi,
1062 			       struct ice_tc_flower_fltr *tc_fltr)
1063 {
1064 	u16 queue = tc_fltr->action.fwd.q.queue;
1065 
1066 	return queue < vsi->num_rxq ? vsi->rx_rings[queue] : NULL;
1067 }
1068 
1069 /**
1070  * ice_tc_forward_action - Determine destination VSI and queue for the action
1071  * @vsi: Pointer to VSI
1072  * @tc_fltr: Pointer to TC flower filter structure
1073  *
1074  * Validates the tc forward action and determines the destination VSI and queue
1075  * for the forward action.
1076  */
1077 static struct ice_vsi *
1078 ice_tc_forward_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *tc_fltr)
1079 {
1080 	struct ice_rx_ring *ring = NULL;
1081 	struct ice_vsi *dest_vsi = NULL;
1082 	struct ice_pf *pf = vsi->back;
1083 	struct device *dev;
1084 	u32 tc_class;
1085 	int q;
1086 
1087 	dev = ice_pf_to_dev(pf);
1088 
1089 	/* Get the destination VSI and/or destination queue and validate them */
1090 	switch (tc_fltr->action.fltr_act) {
1091 	case ICE_FWD_TO_VSI:
1092 		tc_class = tc_fltr->action.fwd.tc.tc_class;
1093 		/* Select the destination VSI */
1094 		if (tc_class < ICE_CHNL_START_TC) {
1095 			NL_SET_ERR_MSG_MOD(tc_fltr->extack,
1096 					   "Unable to add filter because of unsupported destination");
1097 			return ERR_PTR(-EOPNOTSUPP);
1098 		}
1099 		/* Locate ADQ VSI depending on hw_tc number */
1100 		dest_vsi = vsi->tc_map_vsi[tc_class];
1101 		break;
1102 	case ICE_FWD_TO_Q:
1103 		/* Locate the Rx queue */
1104 		ring = ice_locate_rx_ring_using_queue(vsi, tc_fltr);
1105 		if (!ring) {
1106 			dev_err(dev,
1107 				"Unable to locate Rx queue for action fwd_to_queue: %u\n",
1108 				tc_fltr->action.fwd.q.queue);
1109 			return ERR_PTR(-EINVAL);
1110 		}
1111 		/* Determine destination VSI even though the action is
1112 		 * FWD_TO_QUEUE, because QUEUE is associated with VSI
1113 		 */
1114 		q = tc_fltr->action.fwd.q.queue;
1115 		dest_vsi = ice_locate_vsi_using_queue(vsi, q);
1116 		break;
1117 	default:
1118 		dev_err(dev,
1119 			"Unable to add filter because of unsupported action %u (supported actions: fwd to tc, fwd to queue)\n",
1120 			tc_fltr->action.fltr_act);
1121 		return ERR_PTR(-EINVAL);
1122 	}
1123 	/* Must have valid dest_vsi (it could be main VSI or ADQ VSI) */
1124 	if (!dest_vsi) {
1125 		dev_err(dev,
1126 			"Unable to add filter because specified destination VSI doesn't exist\n");
1127 		return ERR_PTR(-EINVAL);
1128 	}
1129 	return dest_vsi;
1130 }
1131 
1132 /**
1133  * ice_add_tc_flower_adv_fltr - add appropriate filter rules
1134  * @vsi: Pointer to VSI
1135  * @tc_fltr: Pointer to TC flower filter structure
1136  *
1137  * based on filter parameters using Advance recipes supported
1138  * by OS package.
1139  */
1140 static int
1141 ice_add_tc_flower_adv_fltr(struct ice_vsi *vsi,
1142 			   struct ice_tc_flower_fltr *tc_fltr)
1143 {
1144 	struct ice_adv_rule_info rule_info = {0};
1145 	struct ice_rule_query_data rule_added;
1146 	struct ice_adv_lkup_elem *list;
1147 	struct ice_pf *pf = vsi->back;
1148 	struct ice_hw *hw = &pf->hw;
1149 	u32 flags = tc_fltr->flags;
1150 	struct ice_vsi *dest_vsi;
1151 	struct device *dev;
1152 	u16 lkups_cnt = 0;
1153 	u16 l4_proto = 0;
1154 	int ret = 0;
1155 	u16 i = 0;
1156 
1157 	dev = ice_pf_to_dev(pf);
1158 	if (ice_is_safe_mode(pf)) {
1159 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because driver is in safe mode");
1160 		return -EOPNOTSUPP;
1161 	}
1162 
1163 	if (!flags || (flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
1164 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
1165 				ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
1166 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
1167 				ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT))) {
1168 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unsupported encap field(s)");
1169 		return -EOPNOTSUPP;
1170 	}
1171 
1172 	/* validate forwarding action VSI and queue */
1173 	if (ice_is_forward_action(tc_fltr->action.fltr_act)) {
1174 		dest_vsi = ice_tc_forward_action(vsi, tc_fltr);
1175 		if (IS_ERR(dest_vsi))
1176 			return PTR_ERR(dest_vsi);
1177 	}
1178 
1179 	lkups_cnt = ice_tc_count_lkups(flags, tc_fltr);
1180 	list = kzalloc_objs(*list, lkups_cnt, GFP_ATOMIC);
1181 	if (!list)
1182 		return -ENOMEM;
1183 
1184 	i = ice_tc_fill_rules(hw, flags, tc_fltr, list, &rule_info, &l4_proto);
1185 	if (i != lkups_cnt) {
1186 		ret = -EINVAL;
1187 		goto exit;
1188 	}
1189 
1190 	rule_info.sw_act.fltr_act = tc_fltr->action.fltr_act;
1191 	/* specify the cookie as filter_rule_id */
1192 	rule_info.fltr_rule_id = tc_fltr->cookie;
1193 
1194 	switch (tc_fltr->action.fltr_act) {
1195 	case ICE_FWD_TO_VSI:
1196 		rule_info.sw_act.vsi_handle = dest_vsi->idx;
1197 		rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
1198 		rule_info.sw_act.src = hw->pf_id;
1199 		dev_dbg(dev, "add switch rule for TC:%u vsi_idx:%u, lkups_cnt:%u\n",
1200 			tc_fltr->action.fwd.tc.tc_class,
1201 			rule_info.sw_act.vsi_handle, lkups_cnt);
1202 		break;
1203 	case ICE_FWD_TO_Q:
1204 		/* HW queue number in global space */
1205 		rule_info.sw_act.fwd_id.q_id = tc_fltr->action.fwd.q.hw_queue +
1206 			hw->func_caps.common_cap.rxq_first_id;
1207 		rule_info.sw_act.vsi_handle = dest_vsi->idx;
1208 		rule_info.priority = ICE_SWITCH_FLTR_PRIO_QUEUE;
1209 		rule_info.sw_act.src = hw->pf_id;
1210 		dev_dbg(dev, "add switch rule action to forward to queue:%u (HW queue %u), lkups_cnt:%u\n",
1211 			tc_fltr->action.fwd.q.queue,
1212 			tc_fltr->action.fwd.q.hw_queue, lkups_cnt);
1213 		break;
1214 	case ICE_DROP_PACKET:
1215 		if (tc_fltr->direction == ICE_ESWITCH_FLTR_EGRESS) {
1216 			rule_info.sw_act.flag |= ICE_FLTR_TX;
1217 			rule_info.sw_act.src = vsi->idx;
1218 		} else {
1219 			rule_info.sw_act.flag |= ICE_FLTR_RX;
1220 			rule_info.sw_act.src = hw->pf_id;
1221 		}
1222 		rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
1223 		break;
1224 	default:
1225 		ret = -EOPNOTSUPP;
1226 		goto exit;
1227 	}
1228 
1229 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
1230 	if (ret == -EEXIST) {
1231 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
1232 				   "Unable to add filter because it already exist");
1233 		ret = -EINVAL;
1234 		goto exit;
1235 	} else if (ret == -ENOSPC) {
1236 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
1237 				   "Unable to add filter: insufficient space available.");
1238 		goto exit;
1239 	} else if (ret) {
1240 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
1241 				   "Unable to add filter due to error");
1242 		goto exit;
1243 	}
1244 
1245 	/* store the output params, which are needed later for removing
1246 	 * advanced switch filter
1247 	 */
1248 	tc_fltr->rid = rule_added.rid;
1249 	tc_fltr->rule_id = rule_added.rule_id;
1250 	tc_fltr->dest_vsi_handle = rule_added.vsi_handle;
1251 	if (tc_fltr->action.fltr_act == ICE_FWD_TO_VSI ||
1252 	    tc_fltr->action.fltr_act == ICE_FWD_TO_Q) {
1253 		tc_fltr->dest_vsi = dest_vsi;
1254 		/* keep track of advanced switch filter for
1255 		 * destination VSI
1256 		 */
1257 		dest_vsi->num_chnl_fltr++;
1258 
1259 		/* keeps track of channel filters for PF VSI */
1260 		if (vsi->type == ICE_VSI_PF &&
1261 		    (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1262 			      ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
1263 			pf->num_dmac_chnl_fltrs++;
1264 	}
1265 	switch (tc_fltr->action.fltr_act) {
1266 	case ICE_FWD_TO_VSI:
1267 		dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is forward to TC %u, rid %u, rule_id %u, vsi_idx %u\n",
1268 			lkups_cnt, flags,
1269 			tc_fltr->action.fwd.tc.tc_class, rule_added.rid,
1270 			rule_added.rule_id, rule_added.vsi_handle);
1271 		break;
1272 	case ICE_FWD_TO_Q:
1273 		dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is forward to queue: %u (HW queue %u)     , rid %u, rule_id %u\n",
1274 			lkups_cnt, flags, tc_fltr->action.fwd.q.queue,
1275 			tc_fltr->action.fwd.q.hw_queue, rule_added.rid,
1276 			rule_added.rule_id);
1277 		break;
1278 	case ICE_DROP_PACKET:
1279 		dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is drop, rid %u, rule_id %u\n",
1280 			lkups_cnt, flags, rule_added.rid, rule_added.rule_id);
1281 		break;
1282 	default:
1283 		break;
1284 	}
1285 exit:
1286 	kfree(list);
1287 	return ret;
1288 }
1289 
1290 /**
1291  * ice_tc_set_pppoe - Parse PPPoE fields from TC flower filter
1292  * @match: Pointer to flow match structure
1293  * @fltr: Pointer to filter structure
1294  * @headers: Pointer to outer header fields
1295  * @returns PPP protocol used in filter (ppp_ses or ppp_disc)
1296  */
1297 static u16
1298 ice_tc_set_pppoe(struct flow_match_pppoe *match,
1299 		 struct ice_tc_flower_fltr *fltr,
1300 		 struct ice_tc_flower_lyr_2_4_hdrs *headers)
1301 {
1302 	if (match->mask->session_id) {
1303 		fltr->flags |= ICE_TC_FLWR_FIELD_PPPOE_SESSID;
1304 		headers->pppoe_hdr.session_id = match->key->session_id;
1305 	}
1306 
1307 	if (match->mask->ppp_proto) {
1308 		fltr->flags |= ICE_TC_FLWR_FIELD_PPP_PROTO;
1309 		headers->pppoe_hdr.ppp_proto = match->key->ppp_proto;
1310 	}
1311 
1312 	return be16_to_cpu(match->key->type);
1313 }
1314 
1315 /**
1316  * ice_tc_set_ipv4 - Parse IPv4 addresses from TC flower filter
1317  * @match: Pointer to flow match structure
1318  * @fltr: Pointer to filter structure
1319  * @headers: inner or outer header fields
1320  * @is_encap: set true for tunnel IPv4 address
1321  */
1322 static int
1323 ice_tc_set_ipv4(struct flow_match_ipv4_addrs *match,
1324 		struct ice_tc_flower_fltr *fltr,
1325 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1326 {
1327 	if (match->key->dst) {
1328 		if (is_encap)
1329 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV4;
1330 		else
1331 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV4;
1332 		headers->l3_key.dst_ipv4 = match->key->dst;
1333 		headers->l3_mask.dst_ipv4 = match->mask->dst;
1334 	}
1335 	if (match->key->src) {
1336 		if (is_encap)
1337 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV4;
1338 		else
1339 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV4;
1340 		headers->l3_key.src_ipv4 = match->key->src;
1341 		headers->l3_mask.src_ipv4 = match->mask->src;
1342 	}
1343 	return 0;
1344 }
1345 
1346 /**
1347  * ice_tc_set_ipv6 - Parse IPv6 addresses from TC flower filter
1348  * @match: Pointer to flow match structure
1349  * @fltr: Pointer to filter structure
1350  * @headers: inner or outer header fields
1351  * @is_encap: set true for tunnel IPv6 address
1352  */
1353 static int
1354 ice_tc_set_ipv6(struct flow_match_ipv6_addrs *match,
1355 		struct ice_tc_flower_fltr *fltr,
1356 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1357 {
1358 	struct ice_tc_l3_hdr *l3_key, *l3_mask;
1359 
1360 	/* src and dest IPV6 address should not be LOOPBACK
1361 	 * (0:0:0:0:0:0:0:1), which can be represented as ::1
1362 	 */
1363 	if (ipv6_addr_loopback(&match->key->dst) ||
1364 	    ipv6_addr_loopback(&match->key->src)) {
1365 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad IPv6, addr is LOOPBACK");
1366 		return -EINVAL;
1367 	}
1368 	/* if src/dest IPv6 address is *,* error */
1369 	if (ipv6_addr_any(&match->mask->dst) &&
1370 	    ipv6_addr_any(&match->mask->src)) {
1371 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad src/dest IPv6, addr is any");
1372 		return -EINVAL;
1373 	}
1374 	if (!ipv6_addr_any(&match->mask->dst)) {
1375 		if (is_encap)
1376 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV6;
1377 		else
1378 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV6;
1379 	}
1380 	if (!ipv6_addr_any(&match->mask->src)) {
1381 		if (is_encap)
1382 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV6;
1383 		else
1384 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV6;
1385 	}
1386 
1387 	l3_key = &headers->l3_key;
1388 	l3_mask = &headers->l3_mask;
1389 
1390 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
1391 			   ICE_TC_FLWR_FIELD_SRC_IPV6)) {
1392 		memcpy(&l3_key->src_ipv6_addr, &match->key->src.s6_addr,
1393 		       sizeof(match->key->src.s6_addr));
1394 		memcpy(&l3_mask->src_ipv6_addr, &match->mask->src.s6_addr,
1395 		       sizeof(match->mask->src.s6_addr));
1396 	}
1397 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
1398 			   ICE_TC_FLWR_FIELD_DEST_IPV6)) {
1399 		memcpy(&l3_key->dst_ipv6_addr, &match->key->dst.s6_addr,
1400 		       sizeof(match->key->dst.s6_addr));
1401 		memcpy(&l3_mask->dst_ipv6_addr, &match->mask->dst.s6_addr,
1402 		       sizeof(match->mask->dst.s6_addr));
1403 	}
1404 
1405 	return 0;
1406 }
1407 
1408 /**
1409  * ice_tc_set_tos_ttl - Parse IP ToS/TTL from TC flower filter
1410  * @match: Pointer to flow match structure
1411  * @fltr: Pointer to filter structure
1412  * @headers: inner or outer header fields
1413  * @is_encap: set true for tunnel
1414  */
1415 static void
1416 ice_tc_set_tos_ttl(struct flow_match_ip *match,
1417 		   struct ice_tc_flower_fltr *fltr,
1418 		   struct ice_tc_flower_lyr_2_4_hdrs *headers,
1419 		   bool is_encap)
1420 {
1421 	if (match->mask->tos) {
1422 		if (is_encap)
1423 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TOS;
1424 		else
1425 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TOS;
1426 
1427 		headers->l3_key.tos = match->key->tos;
1428 		headers->l3_mask.tos = match->mask->tos;
1429 	}
1430 
1431 	if (match->mask->ttl) {
1432 		if (is_encap)
1433 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TTL;
1434 		else
1435 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TTL;
1436 
1437 		headers->l3_key.ttl = match->key->ttl;
1438 		headers->l3_mask.ttl = match->mask->ttl;
1439 	}
1440 }
1441 
1442 /**
1443  * ice_tc_set_port - Parse ports from TC flower filter
1444  * @match: Flow match structure
1445  * @fltr: Pointer to filter structure
1446  * @headers: inner or outer header fields
1447  * @is_encap: set true for tunnel port
1448  */
1449 static int
1450 ice_tc_set_port(struct flow_match_ports match,
1451 		struct ice_tc_flower_fltr *fltr,
1452 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1453 {
1454 	if (match.key->dst) {
1455 		if (is_encap)
1456 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT;
1457 		else
1458 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_L4_PORT;
1459 
1460 		headers->l4_key.dst_port = match.key->dst;
1461 		headers->l4_mask.dst_port = match.mask->dst;
1462 	}
1463 	if (match.key->src) {
1464 		if (is_encap)
1465 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT;
1466 		else
1467 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_L4_PORT;
1468 
1469 		headers->l4_key.src_port = match.key->src;
1470 		headers->l4_mask.src_port = match.mask->src;
1471 	}
1472 	return 0;
1473 }
1474 
1475 static struct net_device *
1476 ice_get_tunnel_device(struct net_device *dev, struct flow_rule *rule)
1477 {
1478 	struct flow_action_entry *act;
1479 	int i;
1480 
1481 	if (ice_is_tunnel_supported(dev))
1482 		return dev;
1483 
1484 	flow_action_for_each(i, act, &rule->action) {
1485 		if (act->id == FLOW_ACTION_REDIRECT &&
1486 		    ice_is_tunnel_supported(act->dev))
1487 			return act->dev;
1488 	}
1489 
1490 	return NULL;
1491 }
1492 
1493 /**
1494  * ice_parse_gtp_type - Sets GTP tunnel type to GTP-U or GTP-C
1495  * @match: Flow match structure
1496  * @fltr: Pointer to filter structure
1497  *
1498  * GTP-C/GTP-U is selected based on destination port number (enc_dst_port).
1499  * Before calling this funtcion, fltr->tunnel_type should be set to TNL_GTPU,
1500  * therefore making GTP-U the default choice (when destination port number is
1501  * not specified).
1502  */
1503 static int
1504 ice_parse_gtp_type(struct flow_match_ports match,
1505 		   struct ice_tc_flower_fltr *fltr)
1506 {
1507 	u16 dst_port;
1508 
1509 	if (match.key->dst) {
1510 		dst_port = be16_to_cpu(match.key->dst);
1511 
1512 		switch (dst_port) {
1513 		case 2152:
1514 			break;
1515 		case 2123:
1516 			fltr->tunnel_type = TNL_GTPC;
1517 			break;
1518 		default:
1519 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported GTP port number");
1520 			return -EINVAL;
1521 		}
1522 	}
1523 
1524 	return 0;
1525 }
1526 
1527 static int
1528 ice_parse_tunnel_attr(struct net_device *dev, struct flow_rule *rule,
1529 		      struct ice_tc_flower_fltr *fltr)
1530 {
1531 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1532 	struct netlink_ext_ack *extack = fltr->extack;
1533 	struct flow_match_control enc_control;
1534 
1535 	fltr->tunnel_type = ice_tc_tun_get_type(dev);
1536 	headers->l3_key.ip_proto = IPPROTO_UDP;
1537 
1538 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
1539 		struct flow_match_enc_keyid enc_keyid;
1540 
1541 		flow_rule_match_enc_keyid(rule, &enc_keyid);
1542 
1543 		if (!enc_keyid.mask->keyid ||
1544 		    enc_keyid.mask->keyid != cpu_to_be32(ICE_TC_FLOWER_MASK_32))
1545 			return -EINVAL;
1546 
1547 		fltr->flags |= ICE_TC_FLWR_FIELD_TENANT_ID;
1548 		fltr->tenant_id = enc_keyid.key->keyid;
1549 	}
1550 
1551 	flow_rule_match_enc_control(rule, &enc_control);
1552 
1553 	if (flow_rule_has_enc_control_flags(enc_control.mask->flags, extack))
1554 		return -EOPNOTSUPP;
1555 
1556 	if (enc_control.key->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1557 		struct flow_match_ipv4_addrs match;
1558 
1559 		flow_rule_match_enc_ipv4_addrs(rule, &match);
1560 		if (ice_tc_set_ipv4(&match, fltr, headers, true))
1561 			return -EINVAL;
1562 	} else if (enc_control.key->addr_type ==
1563 					FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1564 		struct flow_match_ipv6_addrs match;
1565 
1566 		flow_rule_match_enc_ipv6_addrs(rule, &match);
1567 		if (ice_tc_set_ipv6(&match, fltr, headers, true))
1568 			return -EINVAL;
1569 	}
1570 
1571 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
1572 		struct flow_match_ip match;
1573 
1574 		flow_rule_match_enc_ip(rule, &match);
1575 		ice_tc_set_tos_ttl(&match, fltr, headers, true);
1576 	}
1577 
1578 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) &&
1579 	    fltr->tunnel_type != TNL_VXLAN && fltr->tunnel_type != TNL_GENEVE) {
1580 		struct flow_match_ports match;
1581 
1582 		flow_rule_match_enc_ports(rule, &match);
1583 
1584 		if (fltr->tunnel_type != TNL_GTPU) {
1585 			if (ice_tc_set_port(match, fltr, headers, true))
1586 				return -EINVAL;
1587 		} else {
1588 			if (ice_parse_gtp_type(match, fltr))
1589 				return -EINVAL;
1590 		}
1591 	}
1592 
1593 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS) &&
1594 	    (fltr->tunnel_type == TNL_GTPU || fltr->tunnel_type == TNL_GTPC)) {
1595 		struct flow_match_enc_opts match;
1596 
1597 		flow_rule_match_enc_opts(rule, &match);
1598 
1599 		memcpy(&fltr->gtp_pdu_info_keys, &match.key->data[0],
1600 		       sizeof(struct gtp_pdu_session_info));
1601 
1602 		memcpy(&fltr->gtp_pdu_info_masks, &match.mask->data[0],
1603 		       sizeof(struct gtp_pdu_session_info));
1604 
1605 		fltr->flags |= ICE_TC_FLWR_FIELD_GTP_OPTS;
1606 	}
1607 
1608 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS) &&
1609 	    fltr->tunnel_type == TNL_PFCP) {
1610 		struct flow_match_enc_opts match;
1611 
1612 		flow_rule_match_enc_opts(rule, &match);
1613 
1614 		memcpy(&fltr->pfcp_meta_keys, match.key->data,
1615 		       sizeof(struct pfcp_metadata));
1616 		memcpy(&fltr->pfcp_meta_masks, match.mask->data,
1617 		       sizeof(struct pfcp_metadata));
1618 
1619 		fltr->flags |= ICE_TC_FLWR_FIELD_PFCP_OPTS;
1620 	}
1621 
1622 	return 0;
1623 }
1624 
1625 /**
1626  * ice_parse_cls_flower - Parse TC flower filters provided by kernel
1627  * @vsi: Pointer to the VSI
1628  * @filter_dev: Pointer to device on which filter is being added
1629  * @f: Pointer to struct flow_cls_offload
1630  * @fltr: Pointer to filter structure
1631  * @ingress: if the rule is added to an ingress block
1632  *
1633  * Return: 0 if the flower was parsed successfully, -EINVAL if the flower
1634  *	   cannot be parsed, -EOPNOTSUPP if such filter cannot be configured
1635  *	   for the given VSI.
1636  */
1637 static int
1638 ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
1639 		     struct flow_cls_offload *f,
1640 		     struct ice_tc_flower_fltr *fltr, bool ingress)
1641 {
1642 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1643 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1644 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
1645 	struct flow_dissector *dissector;
1646 	struct net_device *tunnel_dev;
1647 
1648 	dissector = rule->match.dissector;
1649 
1650 	if (dissector->used_keys &
1651 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
1652 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
1653 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1654 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
1655 	      BIT_ULL(FLOW_DISSECTOR_KEY_CVLAN) |
1656 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1657 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1658 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1659 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1660 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1661 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1662 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1663 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_OPTS) |
1664 	      BIT_ULL(FLOW_DISSECTOR_KEY_IP) |
1665 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IP) |
1666 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
1667 	      BIT_ULL(FLOW_DISSECTOR_KEY_PPPOE) |
1668 	      BIT_ULL(FLOW_DISSECTOR_KEY_L2TPV3))) {
1669 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported key used");
1670 		return -EOPNOTSUPP;
1671 	}
1672 
1673 	tunnel_dev = ice_get_tunnel_device(filter_dev, rule);
1674 	if (tunnel_dev) {
1675 		int err;
1676 
1677 		filter_dev = tunnel_dev;
1678 
1679 		err = ice_parse_tunnel_attr(filter_dev, rule, fltr);
1680 		if (err) {
1681 			NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to parse TC flower tunnel attributes");
1682 			return err;
1683 		}
1684 
1685 		/* PFCP is considered non-tunneled - don't swap headers. */
1686 		if (fltr->tunnel_type != TNL_PFCP) {
1687 			/* Header pointers should point to the inner headers,
1688 			 * outer header were already set by
1689 			 * ice_parse_tunnel_attr().
1690 			 */
1691 			headers = &fltr->inner_headers;
1692 		}
1693 	} else if (dissector->used_keys &
1694 		  (BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1695 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1696 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1697 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1698 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IP) |
1699 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_OPTS) |
1700 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_CONTROL))) {
1701 		NL_SET_ERR_MSG_MOD(fltr->extack, "Tunnel key used, but device isn't a tunnel");
1702 		return -EOPNOTSUPP;
1703 	} else {
1704 		fltr->tunnel_type = TNL_LAST;
1705 	}
1706 
1707 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1708 		struct flow_match_basic match;
1709 
1710 		flow_rule_match_basic(rule, &match);
1711 
1712 		n_proto_key = ntohs(match.key->n_proto);
1713 		n_proto_mask = ntohs(match.mask->n_proto);
1714 
1715 		if (n_proto_key == ETH_P_ALL || n_proto_key == 0 ||
1716 		    fltr->tunnel_type == TNL_GTPU ||
1717 		    fltr->tunnel_type == TNL_GTPC) {
1718 			n_proto_key = 0;
1719 			n_proto_mask = 0;
1720 		} else {
1721 			fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1722 		}
1723 
1724 		if (!ingress) {
1725 			bool switchdev =
1726 				ice_is_eswitch_mode_switchdev(vsi->back);
1727 
1728 			if (switchdev != (n_proto_key == ETH_P_LLDP)) {
1729 				NL_SET_ERR_MSG_FMT_MOD(fltr->extack,
1730 						       "%sLLDP filtering is not supported on egress in %s mode",
1731 						       switchdev ? "Non-" : "",
1732 						       switchdev ? "switchdev" :
1733 								   "legacy");
1734 				return -EOPNOTSUPP;
1735 			}
1736 		}
1737 
1738 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1739 		headers->l2_mask.n_proto = cpu_to_be16(n_proto_mask);
1740 		headers->l3_key.ip_proto = match.key->ip_proto;
1741 	}
1742 
1743 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1744 		struct flow_match_eth_addrs match;
1745 
1746 		flow_rule_match_eth_addrs(rule, &match);
1747 
1748 		if (!is_zero_ether_addr(match.key->dst)) {
1749 			ether_addr_copy(headers->l2_key.dst_mac,
1750 					match.key->dst);
1751 			ether_addr_copy(headers->l2_mask.dst_mac,
1752 					match.mask->dst);
1753 			fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1754 		}
1755 
1756 		if (!is_zero_ether_addr(match.key->src)) {
1757 			ether_addr_copy(headers->l2_key.src_mac,
1758 					match.key->src);
1759 			ether_addr_copy(headers->l2_mask.src_mac,
1760 					match.mask->src);
1761 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_MAC;
1762 		}
1763 	}
1764 
1765 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
1766 	    is_vlan_dev(filter_dev)) {
1767 		struct flow_dissector_key_vlan mask;
1768 		struct flow_dissector_key_vlan key;
1769 		struct flow_match_vlan match;
1770 
1771 		if (is_vlan_dev(filter_dev)) {
1772 			match.key = &key;
1773 			match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
1774 			match.key->vlan_priority = 0;
1775 			match.mask = &mask;
1776 			memset(match.mask, 0xff, sizeof(*match.mask));
1777 			match.mask->vlan_priority = 0;
1778 		} else {
1779 			flow_rule_match_vlan(rule, &match);
1780 		}
1781 
1782 		if (match.mask->vlan_id) {
1783 			if (match.mask->vlan_id == VLAN_VID_MASK) {
1784 				fltr->flags |= ICE_TC_FLWR_FIELD_VLAN;
1785 				headers->vlan_hdr.vlan_id =
1786 					cpu_to_be16(match.key->vlan_id &
1787 						    VLAN_VID_MASK);
1788 			} else {
1789 				NL_SET_ERR_MSG_MOD(fltr->extack, "Bad VLAN mask");
1790 				return -EINVAL;
1791 			}
1792 		}
1793 
1794 		if (match.mask->vlan_priority) {
1795 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
1796 			headers->vlan_hdr.vlan_prio =
1797 				be16_encode_bits(match.key->vlan_priority,
1798 						 VLAN_PRIO_MASK);
1799 		}
1800 
1801 		if (match.mask->vlan_tpid) {
1802 			headers->vlan_hdr.vlan_tpid = match.key->vlan_tpid;
1803 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_TPID;
1804 		}
1805 	}
1806 
1807 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
1808 		struct flow_match_vlan match;
1809 
1810 		if (!ice_is_dvm_ena(&vsi->back->hw)) {
1811 			NL_SET_ERR_MSG_MOD(fltr->extack, "Double VLAN mode is not enabled");
1812 			return -EINVAL;
1813 		}
1814 
1815 		flow_rule_match_cvlan(rule, &match);
1816 
1817 		if (match.mask->vlan_id) {
1818 			if (match.mask->vlan_id == VLAN_VID_MASK) {
1819 				fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN;
1820 				headers->cvlan_hdr.vlan_id =
1821 					cpu_to_be16(match.key->vlan_id &
1822 						    VLAN_VID_MASK);
1823 			} else {
1824 				NL_SET_ERR_MSG_MOD(fltr->extack,
1825 						   "Bad CVLAN mask");
1826 				return -EINVAL;
1827 			}
1828 		}
1829 
1830 		if (match.mask->vlan_priority) {
1831 			fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
1832 			headers->cvlan_hdr.vlan_prio =
1833 				be16_encode_bits(match.key->vlan_priority,
1834 						 VLAN_PRIO_MASK);
1835 		}
1836 	}
1837 
1838 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PPPOE)) {
1839 		struct flow_match_pppoe match;
1840 
1841 		flow_rule_match_pppoe(rule, &match);
1842 		n_proto_key = ice_tc_set_pppoe(&match, fltr, headers);
1843 
1844 		/* If ethertype equals ETH_P_PPP_SES, n_proto might be
1845 		 * overwritten by encapsulated protocol (ppp_proto field) or set
1846 		 * to 0. To correct this, flow_match_pppoe provides the type
1847 		 * field, which contains the actual ethertype (ETH_P_PPP_SES).
1848 		 */
1849 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1850 		headers->l2_mask.n_proto = cpu_to_be16(0xFFFF);
1851 		fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1852 	}
1853 
1854 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
1855 		struct flow_match_control match;
1856 
1857 		flow_rule_match_control(rule, &match);
1858 
1859 		addr_type = match.key->addr_type;
1860 
1861 		if (flow_rule_has_control_flags(match.mask->flags,
1862 						fltr->extack))
1863 			return -EOPNOTSUPP;
1864 	}
1865 
1866 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1867 		struct flow_match_ipv4_addrs match;
1868 
1869 		flow_rule_match_ipv4_addrs(rule, &match);
1870 		if (ice_tc_set_ipv4(&match, fltr, headers, false))
1871 			return -EINVAL;
1872 	}
1873 
1874 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1875 		struct flow_match_ipv6_addrs match;
1876 
1877 		flow_rule_match_ipv6_addrs(rule, &match);
1878 		if (ice_tc_set_ipv6(&match, fltr, headers, false))
1879 			return -EINVAL;
1880 	}
1881 
1882 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
1883 		struct flow_match_ip match;
1884 
1885 		flow_rule_match_ip(rule, &match);
1886 		ice_tc_set_tos_ttl(&match, fltr, headers, false);
1887 	}
1888 
1889 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_L2TPV3)) {
1890 		struct flow_match_l2tpv3 match;
1891 
1892 		flow_rule_match_l2tpv3(rule, &match);
1893 
1894 		fltr->flags |= ICE_TC_FLWR_FIELD_L2TPV3_SESSID;
1895 		headers->l2tpv3_hdr.session_id = match.key->session_id;
1896 	}
1897 
1898 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
1899 		struct flow_match_ports match;
1900 
1901 		flow_rule_match_ports(rule, &match);
1902 		if (ice_tc_set_port(match, fltr, headers, false))
1903 			return -EINVAL;
1904 		switch (headers->l3_key.ip_proto) {
1905 		case IPPROTO_TCP:
1906 		case IPPROTO_UDP:
1907 			break;
1908 		default:
1909 			NL_SET_ERR_MSG_MOD(fltr->extack, "Only UDP and TCP transport are supported");
1910 			return -EINVAL;
1911 		}
1912 	}
1913 
1914 	/* Ingress filter on representor results in an egress filter in HW
1915 	 * and vice versa
1916 	 */
1917 	ingress = ice_is_port_repr_netdev(filter_dev) ? !ingress : ingress;
1918 	fltr->direction = ingress ? ICE_ESWITCH_FLTR_INGRESS :
1919 				    ICE_ESWITCH_FLTR_EGRESS;
1920 
1921 	return 0;
1922 }
1923 
1924 /**
1925  * ice_add_switch_fltr - Add TC flower filters
1926  * @vsi: Pointer to VSI
1927  * @fltr: Pointer to struct ice_tc_flower_fltr
1928  *
1929  * Add filter in HW switch block
1930  */
1931 static int
1932 ice_add_switch_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1933 {
1934 	if (fltr->action.fltr_act == ICE_FWD_TO_QGRP)
1935 		return -EOPNOTSUPP;
1936 
1937 	if (ice_is_eswitch_mode_switchdev(vsi->back))
1938 		return ice_eswitch_add_tc_fltr(vsi, fltr);
1939 
1940 	return ice_add_tc_flower_adv_fltr(vsi, fltr);
1941 }
1942 
1943 /**
1944  * ice_prep_adq_filter - Prepare ADQ filter with the required additional headers
1945  * @vsi: Pointer to VSI
1946  * @fltr: Pointer to TC flower filter structure
1947  *
1948  * Prepare ADQ filter with the required additional header fields
1949  */
1950 static int
1951 ice_prep_adq_filter(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1952 {
1953 	if ((fltr->flags & ICE_TC_FLWR_FIELD_TENANT_ID) &&
1954 	    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1955 			   ICE_TC_FLWR_FIELD_SRC_MAC))) {
1956 		NL_SET_ERR_MSG_MOD(fltr->extack,
1957 				   "Unable to add filter because filter using tunnel key and inner MAC is unsupported combination");
1958 		return -EOPNOTSUPP;
1959 	}
1960 
1961 	/* For ADQ, filter must include dest MAC address, otherwise unwanted
1962 	 * packets with unrelated MAC address get delivered to ADQ VSIs as long
1963 	 * as remaining filter criteria is satisfied such as dest IP address
1964 	 * and dest/src L4 port. Below code handles the following cases:
1965 	 * 1. For non-tunnel, if user specify MAC addresses, use them.
1966 	 * 2. For non-tunnel, if user didn't specify MAC address, add implicit
1967 	 * dest MAC to be lower netdev's active unicast MAC address
1968 	 * 3. For tunnel,  as of now TC-filter through flower classifier doesn't
1969 	 * have provision for user to specify outer DMAC, hence driver to
1970 	 * implicitly add outer dest MAC to be lower netdev's active unicast
1971 	 * MAC address.
1972 	 */
1973 	if (fltr->tunnel_type != TNL_LAST &&
1974 	    !(fltr->flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC))
1975 		fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DST_MAC;
1976 
1977 	if (fltr->tunnel_type == TNL_LAST &&
1978 	    !(fltr->flags & ICE_TC_FLWR_FIELD_DST_MAC))
1979 		fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1980 
1981 	if (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1982 			   ICE_TC_FLWR_FIELD_ENC_DST_MAC)) {
1983 		ether_addr_copy(fltr->outer_headers.l2_key.dst_mac,
1984 				vsi->netdev->dev_addr);
1985 		eth_broadcast_addr(fltr->outer_headers.l2_mask.dst_mac);
1986 	}
1987 
1988 	/* Make sure VLAN is already added to main VSI, before allowing ADQ to
1989 	 * add a VLAN based filter such as MAC + VLAN + L4 port.
1990 	 */
1991 	if (fltr->flags & ICE_TC_FLWR_FIELD_VLAN) {
1992 		u16 vlan_id = be16_to_cpu(fltr->outer_headers.vlan_hdr.vlan_id);
1993 
1994 		if (!ice_vlan_fltr_exist(&vsi->back->hw, vlan_id, vsi->idx)) {
1995 			NL_SET_ERR_MSG_MOD(fltr->extack,
1996 					   "Unable to add filter because legacy VLAN filter for specified destination doesn't exist");
1997 			return -EINVAL;
1998 		}
1999 	}
2000 	return 0;
2001 }
2002 
2003 /**
2004  * ice_handle_tclass_action - Support directing to a traffic class
2005  * @vsi: Pointer to VSI
2006  * @cls_flower: Pointer to TC flower offload structure
2007  * @fltr: Pointer to TC flower filter structure
2008  *
2009  * Support directing traffic to a traffic class/queue-set
2010  */
2011 static int
2012 ice_handle_tclass_action(struct ice_vsi *vsi,
2013 			 struct flow_cls_offload *cls_flower,
2014 			 struct ice_tc_flower_fltr *fltr)
2015 {
2016 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
2017 
2018 	/* user specified hw_tc (must be non-zero for ADQ TC), action is forward
2019 	 * to hw_tc (i.e. ADQ channel number)
2020 	 */
2021 	if (tc < ICE_CHNL_START_TC) {
2022 		NL_SET_ERR_MSG_MOD(fltr->extack,
2023 				   "Unable to add filter because of unsupported destination");
2024 		return -EOPNOTSUPP;
2025 	}
2026 	if (!(vsi->all_enatc & BIT(tc))) {
2027 		NL_SET_ERR_MSG_MOD(fltr->extack,
2028 				   "Unable to add filter because of non-existence destination");
2029 		return -EINVAL;
2030 	}
2031 	fltr->action.fltr_act = ICE_FWD_TO_VSI;
2032 	fltr->action.fwd.tc.tc_class = tc;
2033 
2034 	return ice_prep_adq_filter(vsi, fltr);
2035 }
2036 
2037 static int
2038 ice_tc_forward_to_queue(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
2039 			struct flow_action_entry *act)
2040 {
2041 	struct ice_vsi *ch_vsi = NULL;
2042 	u16 queue = act->rx_queue;
2043 
2044 	if (queue >= vsi->num_rxq) {
2045 		NL_SET_ERR_MSG_MOD(fltr->extack,
2046 				   "Unable to add filter because specified queue is invalid");
2047 		return -EINVAL;
2048 	}
2049 	fltr->action.fltr_act = ICE_FWD_TO_Q;
2050 	fltr->action.fwd.q.queue = queue;
2051 	/* determine corresponding HW queue */
2052 	fltr->action.fwd.q.hw_queue = vsi->rxq_map[queue];
2053 
2054 	/* If ADQ is configured, and the queue belongs to ADQ VSI, then prepare
2055 	 * ADQ switch filter
2056 	 */
2057 	ch_vsi = ice_locate_vsi_using_queue(vsi, fltr->action.fwd.q.queue);
2058 	if (!ch_vsi)
2059 		return -EINVAL;
2060 	fltr->dest_vsi = ch_vsi;
2061 	if (!ice_is_chnl_fltr(fltr))
2062 		return 0;
2063 
2064 	return ice_prep_adq_filter(vsi, fltr);
2065 }
2066 
2067 static int
2068 ice_tc_parse_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
2069 		    struct flow_action_entry *act)
2070 {
2071 	switch (act->id) {
2072 	case FLOW_ACTION_RX_QUEUE_MAPPING:
2073 		/* forward to queue */
2074 		return ice_tc_forward_to_queue(vsi, fltr, act);
2075 	case FLOW_ACTION_DROP:
2076 		fltr->action.fltr_act = ICE_DROP_PACKET;
2077 		return 0;
2078 	default:
2079 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported TC action");
2080 		return -EOPNOTSUPP;
2081 	}
2082 }
2083 
2084 /**
2085  * ice_parse_tc_flower_actions - Parse the actions for a TC filter
2086  * @filter_dev: Pointer to device on which filter is being added
2087  * @vsi: Pointer to VSI
2088  * @cls_flower: Pointer to TC flower offload structure
2089  * @fltr: Pointer to TC flower filter structure
2090  *
2091  * Parse the actions for a TC filter
2092  */
2093 static int ice_parse_tc_flower_actions(struct net_device *filter_dev,
2094 				       struct ice_vsi *vsi,
2095 				       struct flow_cls_offload *cls_flower,
2096 				       struct ice_tc_flower_fltr *fltr)
2097 {
2098 	struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
2099 	struct flow_action *flow_action = &rule->action;
2100 	struct flow_action_entry *act;
2101 	int i, err;
2102 
2103 	if (cls_flower->classid)
2104 		return ice_handle_tclass_action(vsi, cls_flower, fltr);
2105 
2106 	if (!flow_action_has_entries(flow_action))
2107 		return -EINVAL;
2108 
2109 	flow_action_for_each(i, act, flow_action) {
2110 		if (ice_is_eswitch_mode_switchdev(vsi->back))
2111 			err = ice_eswitch_tc_parse_action(filter_dev, fltr, act);
2112 		else
2113 			err = ice_tc_parse_action(vsi, fltr, act);
2114 		if (err)
2115 			return err;
2116 		continue;
2117 	}
2118 	return 0;
2119 }
2120 
2121 /**
2122  * ice_del_tc_fltr - deletes a filter from HW table
2123  * @vsi: Pointer to VSI
2124  * @fltr: Pointer to struct ice_tc_flower_fltr
2125  *
2126  * This function deletes a filter from HW table and manages book-keeping
2127  */
2128 static int ice_del_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
2129 {
2130 	struct ice_rule_query_data rule_rem;
2131 	struct ice_pf *pf = vsi->back;
2132 	int err;
2133 
2134 	if (ice_is_fltr_pf_tx_lldp(fltr))
2135 		ice_handle_del_pf_lldp_drop_rule(pf);
2136 
2137 	if (ice_is_fltr_vf_tx_lldp(fltr))
2138 		return ice_drop_vf_tx_lldp(vsi, false);
2139 
2140 	rule_rem.rid = fltr->rid;
2141 	rule_rem.rule_id = fltr->rule_id;
2142 	rule_rem.vsi_handle = fltr->dest_vsi_handle;
2143 	err = ice_rem_adv_rule_by_id(&pf->hw, &rule_rem);
2144 	if (err) {
2145 		if (err == -ENOENT) {
2146 			NL_SET_ERR_MSG_MOD(fltr->extack, "Filter does not exist");
2147 			return -ENOENT;
2148 		}
2149 		NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to delete TC flower filter");
2150 		return -EIO;
2151 	}
2152 
2153 	/* update advanced switch filter count for destination
2154 	 * VSI if filter destination was VSI
2155 	 */
2156 	if (fltr->dest_vsi) {
2157 		if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
2158 			fltr->dest_vsi->num_chnl_fltr--;
2159 
2160 			/* keeps track of channel filters for PF VSI */
2161 			if (vsi->type == ICE_VSI_PF &&
2162 			    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
2163 					    ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
2164 				pf->num_dmac_chnl_fltrs--;
2165 		}
2166 	}
2167 	return 0;
2168 }
2169 
2170 /**
2171  * ice_add_tc_fltr - adds a TC flower filter
2172  * @netdev: Pointer to netdev
2173  * @vsi: Pointer to VSI
2174  * @f: Pointer to flower offload structure
2175  * @__fltr: Pointer to struct ice_tc_flower_fltr
2176  * @ingress: if the rule is added to an ingress block
2177  *
2178  * This function parses TC-flower input fields, parses action,
2179  * and adds a filter.
2180  *
2181  * Return: 0 if the filter was successfully added,
2182  *	   negative error code otherwise.
2183  */
2184 static int
2185 ice_add_tc_fltr(struct net_device *netdev, struct ice_vsi *vsi,
2186 		struct flow_cls_offload *f,
2187 		struct ice_tc_flower_fltr **__fltr, bool ingress)
2188 {
2189 	struct ice_tc_flower_fltr *fltr;
2190 	int err;
2191 
2192 	/* by default, set output to be INVALID */
2193 	*__fltr = NULL;
2194 
2195 	fltr = kzalloc_obj(*fltr);
2196 	if (!fltr)
2197 		return -ENOMEM;
2198 
2199 	fltr->cookie = f->cookie;
2200 	fltr->extack = f->common.extack;
2201 	fltr->src_vsi = vsi;
2202 	INIT_HLIST_NODE(&fltr->tc_flower_node);
2203 
2204 	err = ice_parse_cls_flower(netdev, vsi, f, fltr, ingress);
2205 	if (err < 0)
2206 		goto err;
2207 
2208 	err = ice_parse_tc_flower_actions(netdev, vsi, f, fltr);
2209 	if (err < 0)
2210 		goto err;
2211 
2212 	err = ice_add_switch_fltr(vsi, fltr);
2213 	if (err < 0)
2214 		goto err;
2215 
2216 	/* return the newly created filter */
2217 	*__fltr = fltr;
2218 
2219 	return 0;
2220 err:
2221 	kfree(fltr);
2222 	return err;
2223 }
2224 
2225 /**
2226  * ice_find_tc_flower_fltr - Find the TC flower filter in the list
2227  * @pf: Pointer to PF
2228  * @cookie: filter specific cookie
2229  */
2230 static struct ice_tc_flower_fltr *
2231 ice_find_tc_flower_fltr(struct ice_pf *pf, unsigned long cookie)
2232 {
2233 	struct ice_tc_flower_fltr *fltr;
2234 
2235 	hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
2236 		if (cookie == fltr->cookie)
2237 			return fltr;
2238 
2239 	return NULL;
2240 }
2241 
2242 /**
2243  * ice_add_cls_flower - add TC flower filters
2244  * @netdev: Pointer to filter device
2245  * @vsi: Pointer to VSI
2246  * @cls_flower: Pointer to flower offload structure
2247  * @ingress: if the rule is added to an ingress block
2248  *
2249  * Return: 0 if the flower was successfully added,
2250  *	   negative error code otherwise.
2251  */
2252 int ice_add_cls_flower(struct net_device *netdev, struct ice_vsi *vsi,
2253 		       struct flow_cls_offload *cls_flower, bool ingress)
2254 {
2255 	struct netlink_ext_ack *extack = cls_flower->common.extack;
2256 	struct net_device *vsi_netdev = vsi->netdev;
2257 	struct ice_tc_flower_fltr *fltr;
2258 	struct ice_pf *pf = vsi->back;
2259 	int err;
2260 
2261 	if (ice_is_reset_in_progress(pf->state))
2262 		return -EBUSY;
2263 	if (test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2264 		return -EINVAL;
2265 
2266 	if (ice_is_port_repr_netdev(netdev))
2267 		vsi_netdev = netdev;
2268 
2269 	if (!(vsi_netdev->features & NETIF_F_HW_TC) &&
2270 	    !test_bit(ICE_FLAG_CLS_FLOWER, pf->flags)) {
2271 		/* Based on TC indirect notifications from kernel, all ice
2272 		 * devices get an instance of rule from higher level device.
2273 		 * Avoid triggering explicit error in this case.
2274 		 */
2275 		if (netdev == vsi_netdev)
2276 			NL_SET_ERR_MSG_MOD(extack, "can't apply TC flower filters, turn ON hw-tc-offload and try again");
2277 		return -EINVAL;
2278 	}
2279 
2280 	/* avoid duplicate entries, if exists - return error */
2281 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
2282 	if (fltr) {
2283 		NL_SET_ERR_MSG_MOD(extack, "filter cookie already exists, ignoring");
2284 		return -EEXIST;
2285 	}
2286 
2287 	/* prep and add TC-flower filter in HW */
2288 	err = ice_add_tc_fltr(netdev, vsi, cls_flower, &fltr, ingress);
2289 	if (err)
2290 		return err;
2291 
2292 	/* add filter into an ordered list */
2293 	hlist_add_head(&fltr->tc_flower_node, &pf->tc_flower_fltr_list);
2294 	return 0;
2295 }
2296 
2297 /**
2298  * ice_del_cls_flower - delete TC flower filters
2299  * @vsi: Pointer to VSI
2300  * @cls_flower: Pointer to struct flow_cls_offload
2301  */
2302 int
2303 ice_del_cls_flower(struct ice_vsi *vsi, struct flow_cls_offload *cls_flower)
2304 {
2305 	struct ice_tc_flower_fltr *fltr;
2306 	struct ice_pf *pf = vsi->back;
2307 	int err;
2308 
2309 	/* find filter */
2310 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
2311 	if (!fltr) {
2312 		if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) &&
2313 		    hlist_empty(&pf->tc_flower_fltr_list))
2314 			return 0;
2315 
2316 		NL_SET_ERR_MSG_MOD(cls_flower->common.extack, "failed to delete TC flower filter because unable to find it");
2317 		return -EINVAL;
2318 	}
2319 
2320 	fltr->extack = cls_flower->common.extack;
2321 	/* delete filter from HW */
2322 	err = ice_del_tc_fltr(vsi, fltr);
2323 	if (err)
2324 		return err;
2325 
2326 	/* delete filter from an ordered list */
2327 	hlist_del(&fltr->tc_flower_node);
2328 
2329 	/* free the filter node */
2330 	kfree(fltr);
2331 
2332 	return 0;
2333 }
2334 
2335 /**
2336  * ice_replay_tc_fltrs - replay TC filters
2337  * @pf: pointer to PF struct
2338  */
2339 void ice_replay_tc_fltrs(struct ice_pf *pf)
2340 {
2341 	struct ice_tc_flower_fltr *fltr;
2342 	struct hlist_node *node;
2343 
2344 	hlist_for_each_entry_safe(fltr, node,
2345 				  &pf->tc_flower_fltr_list,
2346 				  tc_flower_node) {
2347 		fltr->extack = NULL;
2348 		ice_add_switch_fltr(fltr->src_vsi, fltr);
2349 	}
2350 }
2351