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 /**
11  * ice_tc_count_lkups - determine lookup count for switch filter
12  * @flags: TC-flower flags
13  * @headers: Pointer to TC flower filter header structure
14  * @fltr: Pointer to outer TC filter structure
15  *
16  * Determine lookup count based on TC flower input for switch filter.
17  */
18 static int
19 ice_tc_count_lkups(u32 flags, struct ice_tc_flower_lyr_2_4_hdrs *headers,
20 		   struct ice_tc_flower_fltr *fltr)
21 {
22 	int lkups_cnt = 0;
23 
24 	if (flags & ICE_TC_FLWR_FIELD_TENANT_ID)
25 		lkups_cnt++;
26 
27 	if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC)
28 		lkups_cnt++;
29 
30 	if (flags & ICE_TC_FLWR_FIELD_ENC_OPTS)
31 		lkups_cnt++;
32 
33 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
34 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
35 		     ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
36 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV6))
37 		lkups_cnt++;
38 
39 	if (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
40 		     ICE_TC_FLWR_FIELD_ENC_IP_TTL))
41 		lkups_cnt++;
42 
43 	if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT)
44 		lkups_cnt++;
45 
46 	if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID)
47 		lkups_cnt++;
48 
49 	/* are MAC fields specified? */
50 	if (flags & (ICE_TC_FLWR_FIELD_DST_MAC | ICE_TC_FLWR_FIELD_SRC_MAC))
51 		lkups_cnt++;
52 
53 	/* is VLAN specified? */
54 	if (flags & ICE_TC_FLWR_FIELD_VLAN)
55 		lkups_cnt++;
56 
57 	/* is CVLAN specified? */
58 	if (flags & ICE_TC_FLWR_FIELD_CVLAN)
59 		lkups_cnt++;
60 
61 	/* are PPPoE options specified? */
62 	if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
63 		     ICE_TC_FLWR_FIELD_PPP_PROTO))
64 		lkups_cnt++;
65 
66 	/* are IPv[4|6] fields specified? */
67 	if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 | ICE_TC_FLWR_FIELD_SRC_IPV4 |
68 		     ICE_TC_FLWR_FIELD_DEST_IPV6 | ICE_TC_FLWR_FIELD_SRC_IPV6))
69 		lkups_cnt++;
70 
71 	if (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))
72 		lkups_cnt++;
73 
74 	/* are L2TPv3 options specified? */
75 	if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID)
76 		lkups_cnt++;
77 
78 	/* is L4 (TCP/UDP/any other L4 protocol fields) specified? */
79 	if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
80 		     ICE_TC_FLWR_FIELD_SRC_L4_PORT))
81 		lkups_cnt++;
82 
83 	return lkups_cnt;
84 }
85 
86 static enum ice_protocol_type ice_proto_type_from_mac(bool inner)
87 {
88 	return inner ? ICE_MAC_IL : ICE_MAC_OFOS;
89 }
90 
91 static enum ice_protocol_type ice_proto_type_from_etype(bool inner)
92 {
93 	return inner ? ICE_ETYPE_IL : ICE_ETYPE_OL;
94 }
95 
96 static enum ice_protocol_type ice_proto_type_from_ipv4(bool inner)
97 {
98 	return inner ? ICE_IPV4_IL : ICE_IPV4_OFOS;
99 }
100 
101 static enum ice_protocol_type ice_proto_type_from_ipv6(bool inner)
102 {
103 	return inner ? ICE_IPV6_IL : ICE_IPV6_OFOS;
104 }
105 
106 static enum ice_protocol_type ice_proto_type_from_l4_port(u16 ip_proto)
107 {
108 	switch (ip_proto) {
109 	case IPPROTO_TCP:
110 		return ICE_TCP_IL;
111 	case IPPROTO_UDP:
112 		return ICE_UDP_ILOS;
113 	}
114 
115 	return 0;
116 }
117 
118 static enum ice_protocol_type
119 ice_proto_type_from_tunnel(enum ice_tunnel_type type)
120 {
121 	switch (type) {
122 	case TNL_VXLAN:
123 		return ICE_VXLAN;
124 	case TNL_GENEVE:
125 		return ICE_GENEVE;
126 	case TNL_GRETAP:
127 		return ICE_NVGRE;
128 	case TNL_GTPU:
129 		/* NO_PAY profiles will not work with GTP-U */
130 		return ICE_GTP;
131 	case TNL_GTPC:
132 		return ICE_GTP_NO_PAY;
133 	default:
134 		return 0;
135 	}
136 }
137 
138 static enum ice_sw_tunnel_type
139 ice_sw_type_from_tunnel(enum ice_tunnel_type type)
140 {
141 	switch (type) {
142 	case TNL_VXLAN:
143 		return ICE_SW_TUN_VXLAN;
144 	case TNL_GENEVE:
145 		return ICE_SW_TUN_GENEVE;
146 	case TNL_GRETAP:
147 		return ICE_SW_TUN_NVGRE;
148 	case TNL_GTPU:
149 		return ICE_SW_TUN_GTPU;
150 	case TNL_GTPC:
151 		return ICE_SW_TUN_GTPC;
152 	default:
153 		return ICE_NON_TUN;
154 	}
155 }
156 
157 static u16 ice_check_supported_vlan_tpid(u16 vlan_tpid)
158 {
159 	switch (vlan_tpid) {
160 	case ETH_P_8021Q:
161 	case ETH_P_8021AD:
162 	case ETH_P_QINQ1:
163 		return vlan_tpid;
164 	default:
165 		return 0;
166 	}
167 }
168 
169 static int
170 ice_tc_fill_tunnel_outer(u32 flags, struct ice_tc_flower_fltr *fltr,
171 			 struct ice_adv_lkup_elem *list)
172 {
173 	struct ice_tc_flower_lyr_2_4_hdrs *hdr = &fltr->outer_headers;
174 	int i = 0;
175 
176 	if (flags & ICE_TC_FLWR_FIELD_TENANT_ID) {
177 		u32 tenant_id;
178 
179 		list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
180 		switch (fltr->tunnel_type) {
181 		case TNL_VXLAN:
182 		case TNL_GENEVE:
183 			tenant_id = be32_to_cpu(fltr->tenant_id) << 8;
184 			list[i].h_u.tnl_hdr.vni = cpu_to_be32(tenant_id);
185 			memcpy(&list[i].m_u.tnl_hdr.vni, "\xff\xff\xff\x00", 4);
186 			i++;
187 			break;
188 		case TNL_GRETAP:
189 			list[i].h_u.nvgre_hdr.tni_flow = fltr->tenant_id;
190 			memcpy(&list[i].m_u.nvgre_hdr.tni_flow,
191 			       "\xff\xff\xff\xff", 4);
192 			i++;
193 			break;
194 		case TNL_GTPC:
195 		case TNL_GTPU:
196 			list[i].h_u.gtp_hdr.teid = fltr->tenant_id;
197 			memcpy(&list[i].m_u.gtp_hdr.teid,
198 			       "\xff\xff\xff\xff", 4);
199 			i++;
200 			break;
201 		default:
202 			break;
203 		}
204 	}
205 
206 	if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC) {
207 		list[i].type = ice_proto_type_from_mac(false);
208 		ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
209 				hdr->l2_key.dst_mac);
210 		ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
211 				hdr->l2_mask.dst_mac);
212 		i++;
213 	}
214 
215 	if (flags & ICE_TC_FLWR_FIELD_ENC_OPTS &&
216 	    (fltr->tunnel_type == TNL_GTPU || fltr->tunnel_type == TNL_GTPC)) {
217 		list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
218 
219 		if (fltr->gtp_pdu_info_masks.pdu_type) {
220 			list[i].h_u.gtp_hdr.pdu_type =
221 				fltr->gtp_pdu_info_keys.pdu_type << 4;
222 			memcpy(&list[i].m_u.gtp_hdr.pdu_type, "\xf0", 1);
223 		}
224 
225 		if (fltr->gtp_pdu_info_masks.qfi) {
226 			list[i].h_u.gtp_hdr.qfi = fltr->gtp_pdu_info_keys.qfi;
227 			memcpy(&list[i].m_u.gtp_hdr.qfi, "\x3f", 1);
228 		}
229 
230 		i++;
231 	}
232 
233 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
234 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV4)) {
235 		list[i].type = ice_proto_type_from_ipv4(false);
236 
237 		if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV4) {
238 			list[i].h_u.ipv4_hdr.src_addr = hdr->l3_key.src_ipv4;
239 			list[i].m_u.ipv4_hdr.src_addr = hdr->l3_mask.src_ipv4;
240 		}
241 		if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV4) {
242 			list[i].h_u.ipv4_hdr.dst_addr = hdr->l3_key.dst_ipv4;
243 			list[i].m_u.ipv4_hdr.dst_addr = hdr->l3_mask.dst_ipv4;
244 		}
245 		i++;
246 	}
247 
248 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
249 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV6)) {
250 		list[i].type = ice_proto_type_from_ipv6(false);
251 
252 		if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV6) {
253 			memcpy(&list[i].h_u.ipv6_hdr.src_addr,
254 			       &hdr->l3_key.src_ipv6_addr,
255 			       sizeof(hdr->l3_key.src_ipv6_addr));
256 			memcpy(&list[i].m_u.ipv6_hdr.src_addr,
257 			       &hdr->l3_mask.src_ipv6_addr,
258 			       sizeof(hdr->l3_mask.src_ipv6_addr));
259 		}
260 		if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV6) {
261 			memcpy(&list[i].h_u.ipv6_hdr.dst_addr,
262 			       &hdr->l3_key.dst_ipv6_addr,
263 			       sizeof(hdr->l3_key.dst_ipv6_addr));
264 			memcpy(&list[i].m_u.ipv6_hdr.dst_addr,
265 			       &hdr->l3_mask.dst_ipv6_addr,
266 			       sizeof(hdr->l3_mask.dst_ipv6_addr));
267 		}
268 		i++;
269 	}
270 
271 	if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IP) &&
272 	    (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
273 		      ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
274 		list[i].type = ice_proto_type_from_ipv4(false);
275 
276 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
277 			list[i].h_u.ipv4_hdr.tos = hdr->l3_key.tos;
278 			list[i].m_u.ipv4_hdr.tos = hdr->l3_mask.tos;
279 		}
280 
281 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
282 			list[i].h_u.ipv4_hdr.time_to_live = hdr->l3_key.ttl;
283 			list[i].m_u.ipv4_hdr.time_to_live = hdr->l3_mask.ttl;
284 		}
285 
286 		i++;
287 	}
288 
289 	if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IPV6) &&
290 	    (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
291 		      ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
292 		struct ice_ipv6_hdr *hdr_h, *hdr_m;
293 
294 		hdr_h = &list[i].h_u.ipv6_hdr;
295 		hdr_m = &list[i].m_u.ipv6_hdr;
296 		list[i].type = ice_proto_type_from_ipv6(false);
297 
298 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
299 			be32p_replace_bits(&hdr_h->be_ver_tc_flow,
300 					   hdr->l3_key.tos,
301 					   ICE_IPV6_HDR_TC_MASK);
302 			be32p_replace_bits(&hdr_m->be_ver_tc_flow,
303 					   hdr->l3_mask.tos,
304 					   ICE_IPV6_HDR_TC_MASK);
305 		}
306 
307 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
308 			hdr_h->hop_limit = hdr->l3_key.ttl;
309 			hdr_m->hop_limit = hdr->l3_mask.ttl;
310 		}
311 
312 		i++;
313 	}
314 
315 	if ((flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT) &&
316 	    hdr->l3_key.ip_proto == IPPROTO_UDP) {
317 		list[i].type = ICE_UDP_OF;
318 		list[i].h_u.l4_hdr.dst_port = hdr->l4_key.dst_port;
319 		list[i].m_u.l4_hdr.dst_port = hdr->l4_mask.dst_port;
320 		i++;
321 	}
322 
323 	return i;
324 }
325 
326 /**
327  * ice_tc_fill_rules - fill filter rules based on TC fltr
328  * @hw: pointer to HW structure
329  * @flags: tc flower field flags
330  * @tc_fltr: pointer to TC flower filter
331  * @list: list of advance rule elements
332  * @rule_info: pointer to information about rule
333  * @l4_proto: pointer to information such as L4 proto type
334  *
335  * Fill ice_adv_lkup_elem list based on TC flower flags and
336  * TC flower headers. This list should be used to add
337  * advance filter in hardware.
338  */
339 static int
340 ice_tc_fill_rules(struct ice_hw *hw, u32 flags,
341 		  struct ice_tc_flower_fltr *tc_fltr,
342 		  struct ice_adv_lkup_elem *list,
343 		  struct ice_adv_rule_info *rule_info,
344 		  u16 *l4_proto)
345 {
346 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
347 	bool inner = false;
348 	u16 vlan_tpid = 0;
349 	int i = 0;
350 
351 	rule_info->vlan_type = vlan_tpid;
352 
353 	rule_info->tun_type = ice_sw_type_from_tunnel(tc_fltr->tunnel_type);
354 	if (tc_fltr->tunnel_type != TNL_LAST) {
355 		i = ice_tc_fill_tunnel_outer(flags, tc_fltr, list);
356 
357 		headers = &tc_fltr->inner_headers;
358 		inner = true;
359 	}
360 
361 	if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID) {
362 		list[i].type = ice_proto_type_from_etype(inner);
363 		list[i].h_u.ethertype.ethtype_id = headers->l2_key.n_proto;
364 		list[i].m_u.ethertype.ethtype_id = headers->l2_mask.n_proto;
365 		i++;
366 	}
367 
368 	if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
369 		     ICE_TC_FLWR_FIELD_SRC_MAC)) {
370 		struct ice_tc_l2_hdr *l2_key, *l2_mask;
371 
372 		l2_key = &headers->l2_key;
373 		l2_mask = &headers->l2_mask;
374 
375 		list[i].type = ice_proto_type_from_mac(inner);
376 		if (flags & ICE_TC_FLWR_FIELD_DST_MAC) {
377 			ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
378 					l2_key->dst_mac);
379 			ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
380 					l2_mask->dst_mac);
381 		}
382 		if (flags & ICE_TC_FLWR_FIELD_SRC_MAC) {
383 			ether_addr_copy(list[i].h_u.eth_hdr.src_addr,
384 					l2_key->src_mac);
385 			ether_addr_copy(list[i].m_u.eth_hdr.src_addr,
386 					l2_mask->src_mac);
387 		}
388 		i++;
389 	}
390 
391 	/* copy VLAN info */
392 	if (flags & ICE_TC_FLWR_FIELD_VLAN) {
393 		vlan_tpid = be16_to_cpu(headers->vlan_hdr.vlan_tpid);
394 		rule_info->vlan_type =
395 				ice_check_supported_vlan_tpid(vlan_tpid);
396 
397 		if (flags & ICE_TC_FLWR_FIELD_CVLAN)
398 			list[i].type = ICE_VLAN_EX;
399 		else
400 			list[i].type = ICE_VLAN_OFOS;
401 		list[i].h_u.vlan_hdr.vlan = headers->vlan_hdr.vlan_id;
402 		list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xFFFF);
403 		i++;
404 	}
405 
406 	if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
407 		list[i].type = ICE_VLAN_IN;
408 		list[i].h_u.vlan_hdr.vlan = headers->cvlan_hdr.vlan_id;
409 		list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xFFFF);
410 		i++;
411 	}
412 
413 	if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
414 		     ICE_TC_FLWR_FIELD_PPP_PROTO)) {
415 		struct ice_pppoe_hdr *vals, *masks;
416 
417 		vals = &list[i].h_u.pppoe_hdr;
418 		masks = &list[i].m_u.pppoe_hdr;
419 
420 		list[i].type = ICE_PPPOE;
421 
422 		if (flags & ICE_TC_FLWR_FIELD_PPPOE_SESSID) {
423 			vals->session_id = headers->pppoe_hdr.session_id;
424 			masks->session_id = cpu_to_be16(0xFFFF);
425 		}
426 
427 		if (flags & ICE_TC_FLWR_FIELD_PPP_PROTO) {
428 			vals->ppp_prot_id = headers->pppoe_hdr.ppp_proto;
429 			masks->ppp_prot_id = cpu_to_be16(0xFFFF);
430 		}
431 
432 		i++;
433 	}
434 
435 	/* copy L3 (IPv[4|6]: src, dest) address */
436 	if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 |
437 		     ICE_TC_FLWR_FIELD_SRC_IPV4)) {
438 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
439 
440 		list[i].type = ice_proto_type_from_ipv4(inner);
441 		l3_key = &headers->l3_key;
442 		l3_mask = &headers->l3_mask;
443 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV4) {
444 			list[i].h_u.ipv4_hdr.dst_addr = l3_key->dst_ipv4;
445 			list[i].m_u.ipv4_hdr.dst_addr = l3_mask->dst_ipv4;
446 		}
447 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV4) {
448 			list[i].h_u.ipv4_hdr.src_addr = l3_key->src_ipv4;
449 			list[i].m_u.ipv4_hdr.src_addr = l3_mask->src_ipv4;
450 		}
451 		i++;
452 	} else if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV6 |
453 			    ICE_TC_FLWR_FIELD_SRC_IPV6)) {
454 		struct ice_ipv6_hdr *ipv6_hdr, *ipv6_mask;
455 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
456 
457 		list[i].type = ice_proto_type_from_ipv6(inner);
458 		ipv6_hdr = &list[i].h_u.ipv6_hdr;
459 		ipv6_mask = &list[i].m_u.ipv6_hdr;
460 		l3_key = &headers->l3_key;
461 		l3_mask = &headers->l3_mask;
462 
463 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV6) {
464 			memcpy(&ipv6_hdr->dst_addr, &l3_key->dst_ipv6_addr,
465 			       sizeof(l3_key->dst_ipv6_addr));
466 			memcpy(&ipv6_mask->dst_addr, &l3_mask->dst_ipv6_addr,
467 			       sizeof(l3_mask->dst_ipv6_addr));
468 		}
469 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV6) {
470 			memcpy(&ipv6_hdr->src_addr, &l3_key->src_ipv6_addr,
471 			       sizeof(l3_key->src_ipv6_addr));
472 			memcpy(&ipv6_mask->src_addr, &l3_mask->src_ipv6_addr,
473 			       sizeof(l3_mask->src_ipv6_addr));
474 		}
475 		i++;
476 	}
477 
478 	if (headers->l2_key.n_proto == htons(ETH_P_IP) &&
479 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
480 		list[i].type = ice_proto_type_from_ipv4(inner);
481 
482 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
483 			list[i].h_u.ipv4_hdr.tos = headers->l3_key.tos;
484 			list[i].m_u.ipv4_hdr.tos = headers->l3_mask.tos;
485 		}
486 
487 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
488 			list[i].h_u.ipv4_hdr.time_to_live =
489 				headers->l3_key.ttl;
490 			list[i].m_u.ipv4_hdr.time_to_live =
491 				headers->l3_mask.ttl;
492 		}
493 
494 		i++;
495 	}
496 
497 	if (headers->l2_key.n_proto == htons(ETH_P_IPV6) &&
498 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
499 		struct ice_ipv6_hdr *hdr_h, *hdr_m;
500 
501 		hdr_h = &list[i].h_u.ipv6_hdr;
502 		hdr_m = &list[i].m_u.ipv6_hdr;
503 		list[i].type = ice_proto_type_from_ipv6(inner);
504 
505 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
506 			be32p_replace_bits(&hdr_h->be_ver_tc_flow,
507 					   headers->l3_key.tos,
508 					   ICE_IPV6_HDR_TC_MASK);
509 			be32p_replace_bits(&hdr_m->be_ver_tc_flow,
510 					   headers->l3_mask.tos,
511 					   ICE_IPV6_HDR_TC_MASK);
512 		}
513 
514 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
515 			hdr_h->hop_limit = headers->l3_key.ttl;
516 			hdr_m->hop_limit = headers->l3_mask.ttl;
517 		}
518 
519 		i++;
520 	}
521 
522 	if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID) {
523 		list[i].type = ICE_L2TPV3;
524 
525 		list[i].h_u.l2tpv3_sess_hdr.session_id =
526 			headers->l2tpv3_hdr.session_id;
527 		list[i].m_u.l2tpv3_sess_hdr.session_id =
528 			cpu_to_be32(0xFFFFFFFF);
529 
530 		i++;
531 	}
532 
533 	/* copy L4 (src, dest) port */
534 	if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
535 		     ICE_TC_FLWR_FIELD_SRC_L4_PORT)) {
536 		struct ice_tc_l4_hdr *l4_key, *l4_mask;
537 
538 		list[i].type = ice_proto_type_from_l4_port(headers->l3_key.ip_proto);
539 		l4_key = &headers->l4_key;
540 		l4_mask = &headers->l4_mask;
541 
542 		if (flags & ICE_TC_FLWR_FIELD_DEST_L4_PORT) {
543 			list[i].h_u.l4_hdr.dst_port = l4_key->dst_port;
544 			list[i].m_u.l4_hdr.dst_port = l4_mask->dst_port;
545 		}
546 		if (flags & ICE_TC_FLWR_FIELD_SRC_L4_PORT) {
547 			list[i].h_u.l4_hdr.src_port = l4_key->src_port;
548 			list[i].m_u.l4_hdr.src_port = l4_mask->src_port;
549 		}
550 		i++;
551 	}
552 
553 	return i;
554 }
555 
556 /**
557  * ice_tc_tun_get_type - get the tunnel type
558  * @tunnel_dev: ptr to tunnel device
559  *
560  * This function detects appropriate tunnel_type if specified device is
561  * tunnel device such as VXLAN/Geneve
562  */
563 static int ice_tc_tun_get_type(struct net_device *tunnel_dev)
564 {
565 	if (netif_is_vxlan(tunnel_dev))
566 		return TNL_VXLAN;
567 	if (netif_is_geneve(tunnel_dev))
568 		return TNL_GENEVE;
569 	if (netif_is_gretap(tunnel_dev) ||
570 	    netif_is_ip6gretap(tunnel_dev))
571 		return TNL_GRETAP;
572 
573 	/* Assume GTP-U by default in case of GTP netdev.
574 	 * GTP-C may be selected later, based on enc_dst_port.
575 	 */
576 	if (netif_is_gtp(tunnel_dev))
577 		return TNL_GTPU;
578 	return TNL_LAST;
579 }
580 
581 bool ice_is_tunnel_supported(struct net_device *dev)
582 {
583 	return ice_tc_tun_get_type(dev) != TNL_LAST;
584 }
585 
586 static int
587 ice_eswitch_tc_parse_action(struct ice_tc_flower_fltr *fltr,
588 			    struct flow_action_entry *act)
589 {
590 	struct ice_repr *repr;
591 
592 	switch (act->id) {
593 	case FLOW_ACTION_DROP:
594 		fltr->action.fltr_act = ICE_DROP_PACKET;
595 		break;
596 
597 	case FLOW_ACTION_REDIRECT:
598 		fltr->action.fltr_act = ICE_FWD_TO_VSI;
599 
600 		if (ice_is_port_repr_netdev(act->dev)) {
601 			repr = ice_netdev_to_repr(act->dev);
602 
603 			fltr->dest_vsi = repr->src_vsi;
604 			fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
605 		} else if (netif_is_ice(act->dev) ||
606 			   ice_is_tunnel_supported(act->dev)) {
607 			fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
608 		} else {
609 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported netdevice in switchdev mode");
610 			return -EINVAL;
611 		}
612 
613 		break;
614 
615 	default:
616 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action in switchdev mode");
617 		return -EINVAL;
618 	}
619 
620 	return 0;
621 }
622 
623 static int
624 ice_eswitch_add_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
625 {
626 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
627 	struct ice_adv_rule_info rule_info = { 0 };
628 	struct ice_rule_query_data rule_added;
629 	struct ice_hw *hw = &vsi->back->hw;
630 	struct ice_adv_lkup_elem *list;
631 	u32 flags = fltr->flags;
632 	int lkups_cnt;
633 	int ret;
634 	int i;
635 
636 	if (!flags || (flags & ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT)) {
637 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported encap field(s)");
638 		return -EOPNOTSUPP;
639 	}
640 
641 	lkups_cnt = ice_tc_count_lkups(flags, headers, fltr);
642 	list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
643 	if (!list)
644 		return -ENOMEM;
645 
646 	i = ice_tc_fill_rules(hw, flags, fltr, list, &rule_info, NULL);
647 	if (i != lkups_cnt) {
648 		ret = -EINVAL;
649 		goto exit;
650 	}
651 
652 	/* egress traffic is always redirect to uplink */
653 	if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS)
654 		fltr->dest_vsi = vsi->back->switchdev.uplink_vsi;
655 
656 	rule_info.sw_act.fltr_act = fltr->action.fltr_act;
657 	if (fltr->action.fltr_act != ICE_DROP_PACKET)
658 		rule_info.sw_act.vsi_handle = fltr->dest_vsi->idx;
659 	/* For now, making priority to be highest, and it also becomes
660 	 * the priority for recipe which will get created as a result of
661 	 * new extraction sequence based on input set.
662 	 * Priority '7' is max val for switch recipe, higher the number
663 	 * results into order of switch rule evaluation.
664 	 */
665 	rule_info.priority = 7;
666 
667 	if (fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
668 		rule_info.sw_act.flag |= ICE_FLTR_RX;
669 		rule_info.sw_act.src = hw->pf_id;
670 		rule_info.rx = true;
671 	} else {
672 		rule_info.sw_act.flag |= ICE_FLTR_TX;
673 		rule_info.sw_act.src = vsi->idx;
674 		rule_info.rx = false;
675 		rule_info.flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
676 		rule_info.flags_info.act_valid = true;
677 	}
678 
679 	/* specify the cookie as filter_rule_id */
680 	rule_info.fltr_rule_id = fltr->cookie;
681 
682 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
683 	if (ret == -EEXIST) {
684 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because it already exist");
685 		ret = -EINVAL;
686 		goto exit;
687 	} else if (ret) {
688 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter due to error");
689 		goto exit;
690 	}
691 
692 	/* store the output params, which are needed later for removing
693 	 * advanced switch filter
694 	 */
695 	fltr->rid = rule_added.rid;
696 	fltr->rule_id = rule_added.rule_id;
697 	fltr->dest_id = rule_added.vsi_handle;
698 
699 exit:
700 	kfree(list);
701 	return ret;
702 }
703 
704 /**
705  * ice_add_tc_flower_adv_fltr - add appropriate filter rules
706  * @vsi: Pointer to VSI
707  * @tc_fltr: Pointer to TC flower filter structure
708  *
709  * based on filter parameters using Advance recipes supported
710  * by OS package.
711  */
712 static int
713 ice_add_tc_flower_adv_fltr(struct ice_vsi *vsi,
714 			   struct ice_tc_flower_fltr *tc_fltr)
715 {
716 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
717 	struct ice_adv_rule_info rule_info = {0};
718 	struct ice_rule_query_data rule_added;
719 	struct ice_adv_lkup_elem *list;
720 	struct ice_pf *pf = vsi->back;
721 	struct ice_hw *hw = &pf->hw;
722 	u32 flags = tc_fltr->flags;
723 	struct ice_vsi *ch_vsi;
724 	struct device *dev;
725 	u16 lkups_cnt = 0;
726 	u16 l4_proto = 0;
727 	int ret = 0;
728 	u16 i = 0;
729 
730 	dev = ice_pf_to_dev(pf);
731 	if (ice_is_safe_mode(pf)) {
732 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because driver is in safe mode");
733 		return -EOPNOTSUPP;
734 	}
735 
736 	if (!flags || (flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
737 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
738 				ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
739 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
740 				ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT))) {
741 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unsupported encap field(s)");
742 		return -EOPNOTSUPP;
743 	}
744 
745 	/* get the channel (aka ADQ VSI) */
746 	if (tc_fltr->dest_vsi)
747 		ch_vsi = tc_fltr->dest_vsi;
748 	else
749 		ch_vsi = vsi->tc_map_vsi[tc_fltr->action.tc_class];
750 
751 	lkups_cnt = ice_tc_count_lkups(flags, headers, tc_fltr);
752 	list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
753 	if (!list)
754 		return -ENOMEM;
755 
756 	i = ice_tc_fill_rules(hw, flags, tc_fltr, list, &rule_info, &l4_proto);
757 	if (i != lkups_cnt) {
758 		ret = -EINVAL;
759 		goto exit;
760 	}
761 
762 	rule_info.sw_act.fltr_act = tc_fltr->action.fltr_act;
763 	if (tc_fltr->action.tc_class >= ICE_CHNL_START_TC) {
764 		if (!ch_vsi) {
765 			NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because specified destination doesn't exist");
766 			ret = -EINVAL;
767 			goto exit;
768 		}
769 
770 		rule_info.sw_act.fltr_act = ICE_FWD_TO_VSI;
771 		rule_info.sw_act.vsi_handle = ch_vsi->idx;
772 		rule_info.priority = 7;
773 		rule_info.sw_act.src = hw->pf_id;
774 		rule_info.rx = true;
775 		dev_dbg(dev, "add switch rule for TC:%u vsi_idx:%u, lkups_cnt:%u\n",
776 			tc_fltr->action.tc_class,
777 			rule_info.sw_act.vsi_handle, lkups_cnt);
778 	} else {
779 		rule_info.sw_act.flag |= ICE_FLTR_TX;
780 		rule_info.sw_act.src = vsi->idx;
781 		rule_info.rx = false;
782 	}
783 
784 	/* specify the cookie as filter_rule_id */
785 	rule_info.fltr_rule_id = tc_fltr->cookie;
786 
787 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
788 	if (ret == -EEXIST) {
789 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
790 				   "Unable to add filter because it already exist");
791 		ret = -EINVAL;
792 		goto exit;
793 	} else if (ret) {
794 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
795 				   "Unable to add filter due to error");
796 		goto exit;
797 	}
798 
799 	/* store the output params, which are needed later for removing
800 	 * advanced switch filter
801 	 */
802 	tc_fltr->rid = rule_added.rid;
803 	tc_fltr->rule_id = rule_added.rule_id;
804 	if (tc_fltr->action.tc_class > 0 && ch_vsi) {
805 		/* For PF ADQ, VSI type is set as ICE_VSI_CHNL, and
806 		 * for PF ADQ filter, it is not yet set in tc_fltr,
807 		 * hence store the dest_vsi ptr in tc_fltr
808 		 */
809 		if (ch_vsi->type == ICE_VSI_CHNL)
810 			tc_fltr->dest_vsi = ch_vsi;
811 		/* keep track of advanced switch filter for
812 		 * destination VSI (channel VSI)
813 		 */
814 		ch_vsi->num_chnl_fltr++;
815 		/* in this case, dest_id is VSI handle (sw handle) */
816 		tc_fltr->dest_id = rule_added.vsi_handle;
817 
818 		/* keeps track of channel filters for PF VSI */
819 		if (vsi->type == ICE_VSI_PF &&
820 		    (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
821 			      ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
822 			pf->num_dmac_chnl_fltrs++;
823 	}
824 	dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x) for TC %u, rid %u, rule_id %u, vsi_idx %u\n",
825 		lkups_cnt, flags,
826 		tc_fltr->action.tc_class, rule_added.rid,
827 		rule_added.rule_id, rule_added.vsi_handle);
828 exit:
829 	kfree(list);
830 	return ret;
831 }
832 
833 /**
834  * ice_tc_set_pppoe - Parse PPPoE fields from TC flower filter
835  * @match: Pointer to flow match structure
836  * @fltr: Pointer to filter structure
837  * @headers: Pointer to outer header fields
838  * @returns PPP protocol used in filter (ppp_ses or ppp_disc)
839  */
840 static u16
841 ice_tc_set_pppoe(struct flow_match_pppoe *match,
842 		 struct ice_tc_flower_fltr *fltr,
843 		 struct ice_tc_flower_lyr_2_4_hdrs *headers)
844 {
845 	if (match->mask->session_id) {
846 		fltr->flags |= ICE_TC_FLWR_FIELD_PPPOE_SESSID;
847 		headers->pppoe_hdr.session_id = match->key->session_id;
848 	}
849 
850 	if (match->mask->ppp_proto) {
851 		fltr->flags |= ICE_TC_FLWR_FIELD_PPP_PROTO;
852 		headers->pppoe_hdr.ppp_proto = match->key->ppp_proto;
853 	}
854 
855 	return be16_to_cpu(match->key->type);
856 }
857 
858 /**
859  * ice_tc_set_ipv4 - Parse IPv4 addresses from TC flower filter
860  * @match: Pointer to flow match structure
861  * @fltr: Pointer to filter structure
862  * @headers: inner or outer header fields
863  * @is_encap: set true for tunnel IPv4 address
864  */
865 static int
866 ice_tc_set_ipv4(struct flow_match_ipv4_addrs *match,
867 		struct ice_tc_flower_fltr *fltr,
868 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
869 {
870 	if (match->key->dst) {
871 		if (is_encap)
872 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV4;
873 		else
874 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV4;
875 		headers->l3_key.dst_ipv4 = match->key->dst;
876 		headers->l3_mask.dst_ipv4 = match->mask->dst;
877 	}
878 	if (match->key->src) {
879 		if (is_encap)
880 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV4;
881 		else
882 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV4;
883 		headers->l3_key.src_ipv4 = match->key->src;
884 		headers->l3_mask.src_ipv4 = match->mask->src;
885 	}
886 	return 0;
887 }
888 
889 /**
890  * ice_tc_set_ipv6 - Parse IPv6 addresses from TC flower filter
891  * @match: Pointer to flow match structure
892  * @fltr: Pointer to filter structure
893  * @headers: inner or outer header fields
894  * @is_encap: set true for tunnel IPv6 address
895  */
896 static int
897 ice_tc_set_ipv6(struct flow_match_ipv6_addrs *match,
898 		struct ice_tc_flower_fltr *fltr,
899 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
900 {
901 	struct ice_tc_l3_hdr *l3_key, *l3_mask;
902 
903 	/* src and dest IPV6 address should not be LOOPBACK
904 	 * (0:0:0:0:0:0:0:1), which can be represented as ::1
905 	 */
906 	if (ipv6_addr_loopback(&match->key->dst) ||
907 	    ipv6_addr_loopback(&match->key->src)) {
908 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad IPv6, addr is LOOPBACK");
909 		return -EINVAL;
910 	}
911 	/* if src/dest IPv6 address is *,* error */
912 	if (ipv6_addr_any(&match->mask->dst) &&
913 	    ipv6_addr_any(&match->mask->src)) {
914 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad src/dest IPv6, addr is any");
915 		return -EINVAL;
916 	}
917 	if (!ipv6_addr_any(&match->mask->dst)) {
918 		if (is_encap)
919 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV6;
920 		else
921 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV6;
922 	}
923 	if (!ipv6_addr_any(&match->mask->src)) {
924 		if (is_encap)
925 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV6;
926 		else
927 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV6;
928 	}
929 
930 	l3_key = &headers->l3_key;
931 	l3_mask = &headers->l3_mask;
932 
933 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
934 			   ICE_TC_FLWR_FIELD_SRC_IPV6)) {
935 		memcpy(&l3_key->src_ipv6_addr, &match->key->src.s6_addr,
936 		       sizeof(match->key->src.s6_addr));
937 		memcpy(&l3_mask->src_ipv6_addr, &match->mask->src.s6_addr,
938 		       sizeof(match->mask->src.s6_addr));
939 	}
940 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
941 			   ICE_TC_FLWR_FIELD_DEST_IPV6)) {
942 		memcpy(&l3_key->dst_ipv6_addr, &match->key->dst.s6_addr,
943 		       sizeof(match->key->dst.s6_addr));
944 		memcpy(&l3_mask->dst_ipv6_addr, &match->mask->dst.s6_addr,
945 		       sizeof(match->mask->dst.s6_addr));
946 	}
947 
948 	return 0;
949 }
950 
951 /**
952  * ice_tc_set_tos_ttl - Parse IP ToS/TTL from TC flower filter
953  * @match: Pointer to flow match structure
954  * @fltr: Pointer to filter structure
955  * @headers: inner or outer header fields
956  * @is_encap: set true for tunnel
957  */
958 static void
959 ice_tc_set_tos_ttl(struct flow_match_ip *match,
960 		   struct ice_tc_flower_fltr *fltr,
961 		   struct ice_tc_flower_lyr_2_4_hdrs *headers,
962 		   bool is_encap)
963 {
964 	if (match->mask->tos) {
965 		if (is_encap)
966 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TOS;
967 		else
968 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TOS;
969 
970 		headers->l3_key.tos = match->key->tos;
971 		headers->l3_mask.tos = match->mask->tos;
972 	}
973 
974 	if (match->mask->ttl) {
975 		if (is_encap)
976 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TTL;
977 		else
978 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TTL;
979 
980 		headers->l3_key.ttl = match->key->ttl;
981 		headers->l3_mask.ttl = match->mask->ttl;
982 	}
983 }
984 
985 /**
986  * ice_tc_set_port - Parse ports from TC flower filter
987  * @match: Flow match structure
988  * @fltr: Pointer to filter structure
989  * @headers: inner or outer header fields
990  * @is_encap: set true for tunnel port
991  */
992 static int
993 ice_tc_set_port(struct flow_match_ports match,
994 		struct ice_tc_flower_fltr *fltr,
995 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
996 {
997 	if (match.key->dst) {
998 		if (is_encap)
999 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT;
1000 		else
1001 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_L4_PORT;
1002 
1003 		headers->l4_key.dst_port = match.key->dst;
1004 		headers->l4_mask.dst_port = match.mask->dst;
1005 	}
1006 	if (match.key->src) {
1007 		if (is_encap)
1008 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT;
1009 		else
1010 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_L4_PORT;
1011 
1012 		headers->l4_key.src_port = match.key->src;
1013 		headers->l4_mask.src_port = match.mask->src;
1014 	}
1015 	return 0;
1016 }
1017 
1018 static struct net_device *
1019 ice_get_tunnel_device(struct net_device *dev, struct flow_rule *rule)
1020 {
1021 	struct flow_action_entry *act;
1022 	int i;
1023 
1024 	if (ice_is_tunnel_supported(dev))
1025 		return dev;
1026 
1027 	flow_action_for_each(i, act, &rule->action) {
1028 		if (act->id == FLOW_ACTION_REDIRECT &&
1029 		    ice_is_tunnel_supported(act->dev))
1030 			return act->dev;
1031 	}
1032 
1033 	return NULL;
1034 }
1035 
1036 /**
1037  * ice_parse_gtp_type - Sets GTP tunnel type to GTP-U or GTP-C
1038  * @match: Flow match structure
1039  * @fltr: Pointer to filter structure
1040  *
1041  * GTP-C/GTP-U is selected based on destination port number (enc_dst_port).
1042  * Before calling this funtcion, fltr->tunnel_type should be set to TNL_GTPU,
1043  * therefore making GTP-U the default choice (when destination port number is
1044  * not specified).
1045  */
1046 static int
1047 ice_parse_gtp_type(struct flow_match_ports match,
1048 		   struct ice_tc_flower_fltr *fltr)
1049 {
1050 	u16 dst_port;
1051 
1052 	if (match.key->dst) {
1053 		dst_port = be16_to_cpu(match.key->dst);
1054 
1055 		switch (dst_port) {
1056 		case 2152:
1057 			break;
1058 		case 2123:
1059 			fltr->tunnel_type = TNL_GTPC;
1060 			break;
1061 		default:
1062 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported GTP port number");
1063 			return -EINVAL;
1064 		}
1065 	}
1066 
1067 	return 0;
1068 }
1069 
1070 static int
1071 ice_parse_tunnel_attr(struct net_device *dev, struct flow_rule *rule,
1072 		      struct ice_tc_flower_fltr *fltr)
1073 {
1074 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1075 	struct flow_match_control enc_control;
1076 
1077 	fltr->tunnel_type = ice_tc_tun_get_type(dev);
1078 	headers->l3_key.ip_proto = IPPROTO_UDP;
1079 
1080 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
1081 		struct flow_match_enc_keyid enc_keyid;
1082 
1083 		flow_rule_match_enc_keyid(rule, &enc_keyid);
1084 
1085 		if (!enc_keyid.mask->keyid ||
1086 		    enc_keyid.mask->keyid != cpu_to_be32(ICE_TC_FLOWER_MASK_32))
1087 			return -EINVAL;
1088 
1089 		fltr->flags |= ICE_TC_FLWR_FIELD_TENANT_ID;
1090 		fltr->tenant_id = enc_keyid.key->keyid;
1091 	}
1092 
1093 	flow_rule_match_enc_control(rule, &enc_control);
1094 
1095 	if (enc_control.key->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1096 		struct flow_match_ipv4_addrs match;
1097 
1098 		flow_rule_match_enc_ipv4_addrs(rule, &match);
1099 		if (ice_tc_set_ipv4(&match, fltr, headers, true))
1100 			return -EINVAL;
1101 	} else if (enc_control.key->addr_type ==
1102 					FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1103 		struct flow_match_ipv6_addrs match;
1104 
1105 		flow_rule_match_enc_ipv6_addrs(rule, &match);
1106 		if (ice_tc_set_ipv6(&match, fltr, headers, true))
1107 			return -EINVAL;
1108 	}
1109 
1110 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
1111 		struct flow_match_ip match;
1112 
1113 		flow_rule_match_enc_ip(rule, &match);
1114 		ice_tc_set_tos_ttl(&match, fltr, headers, true);
1115 	}
1116 
1117 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) &&
1118 	    fltr->tunnel_type != TNL_VXLAN && fltr->tunnel_type != TNL_GENEVE) {
1119 		struct flow_match_ports match;
1120 
1121 		flow_rule_match_enc_ports(rule, &match);
1122 
1123 		if (fltr->tunnel_type != TNL_GTPU) {
1124 			if (ice_tc_set_port(match, fltr, headers, true))
1125 				return -EINVAL;
1126 		} else {
1127 			if (ice_parse_gtp_type(match, fltr))
1128 				return -EINVAL;
1129 		}
1130 	}
1131 
1132 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
1133 		struct flow_match_enc_opts match;
1134 
1135 		flow_rule_match_enc_opts(rule, &match);
1136 
1137 		memcpy(&fltr->gtp_pdu_info_keys, &match.key->data[0],
1138 		       sizeof(struct gtp_pdu_session_info));
1139 
1140 		memcpy(&fltr->gtp_pdu_info_masks, &match.mask->data[0],
1141 		       sizeof(struct gtp_pdu_session_info));
1142 
1143 		fltr->flags |= ICE_TC_FLWR_FIELD_ENC_OPTS;
1144 	}
1145 
1146 	return 0;
1147 }
1148 
1149 /**
1150  * ice_parse_cls_flower - Parse TC flower filters provided by kernel
1151  * @vsi: Pointer to the VSI
1152  * @filter_dev: Pointer to device on which filter is being added
1153  * @f: Pointer to struct flow_cls_offload
1154  * @fltr: Pointer to filter structure
1155  */
1156 static int
1157 ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
1158 		     struct flow_cls_offload *f,
1159 		     struct ice_tc_flower_fltr *fltr)
1160 {
1161 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1162 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1163 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
1164 	struct flow_dissector *dissector;
1165 	struct net_device *tunnel_dev;
1166 
1167 	dissector = rule->match.dissector;
1168 
1169 	if (dissector->used_keys &
1170 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1171 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
1172 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1173 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
1174 	      BIT(FLOW_DISSECTOR_KEY_CVLAN) |
1175 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1176 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1177 	      BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1178 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1179 	      BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1180 	      BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1181 	      BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1182 	      BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) |
1183 	      BIT(FLOW_DISSECTOR_KEY_IP) |
1184 	      BIT(FLOW_DISSECTOR_KEY_ENC_IP) |
1185 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
1186 	      BIT(FLOW_DISSECTOR_KEY_PPPOE) |
1187 	      BIT(FLOW_DISSECTOR_KEY_L2TPV3))) {
1188 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported key used");
1189 		return -EOPNOTSUPP;
1190 	}
1191 
1192 	tunnel_dev = ice_get_tunnel_device(filter_dev, rule);
1193 	if (tunnel_dev) {
1194 		int err;
1195 
1196 		filter_dev = tunnel_dev;
1197 
1198 		err = ice_parse_tunnel_attr(filter_dev, rule, fltr);
1199 		if (err) {
1200 			NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to parse TC flower tunnel attributes");
1201 			return err;
1202 		}
1203 
1204 		/* header pointers should point to the inner headers, outer
1205 		 * header were already set by ice_parse_tunnel_attr
1206 		 */
1207 		headers = &fltr->inner_headers;
1208 	} else if (dissector->used_keys &
1209 		  (BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1210 		   BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1211 		   BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1212 		   BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))) {
1213 		NL_SET_ERR_MSG_MOD(fltr->extack, "Tunnel key used, but device isn't a tunnel");
1214 		return -EOPNOTSUPP;
1215 	} else {
1216 		fltr->tunnel_type = TNL_LAST;
1217 	}
1218 
1219 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1220 		struct flow_match_basic match;
1221 
1222 		flow_rule_match_basic(rule, &match);
1223 
1224 		n_proto_key = ntohs(match.key->n_proto);
1225 		n_proto_mask = ntohs(match.mask->n_proto);
1226 
1227 		if (n_proto_key == ETH_P_ALL || n_proto_key == 0 ||
1228 		    fltr->tunnel_type == TNL_GTPU ||
1229 		    fltr->tunnel_type == TNL_GTPC) {
1230 			n_proto_key = 0;
1231 			n_proto_mask = 0;
1232 		} else {
1233 			fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1234 		}
1235 
1236 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1237 		headers->l2_mask.n_proto = cpu_to_be16(n_proto_mask);
1238 		headers->l3_key.ip_proto = match.key->ip_proto;
1239 	}
1240 
1241 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1242 		struct flow_match_eth_addrs match;
1243 
1244 		flow_rule_match_eth_addrs(rule, &match);
1245 
1246 		if (!is_zero_ether_addr(match.key->dst)) {
1247 			ether_addr_copy(headers->l2_key.dst_mac,
1248 					match.key->dst);
1249 			ether_addr_copy(headers->l2_mask.dst_mac,
1250 					match.mask->dst);
1251 			fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1252 		}
1253 
1254 		if (!is_zero_ether_addr(match.key->src)) {
1255 			ether_addr_copy(headers->l2_key.src_mac,
1256 					match.key->src);
1257 			ether_addr_copy(headers->l2_mask.src_mac,
1258 					match.mask->src);
1259 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_MAC;
1260 		}
1261 	}
1262 
1263 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
1264 	    is_vlan_dev(filter_dev)) {
1265 		struct flow_dissector_key_vlan mask;
1266 		struct flow_dissector_key_vlan key;
1267 		struct flow_match_vlan match;
1268 
1269 		if (is_vlan_dev(filter_dev)) {
1270 			match.key = &key;
1271 			match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
1272 			match.key->vlan_priority = 0;
1273 			match.mask = &mask;
1274 			memset(match.mask, 0xff, sizeof(*match.mask));
1275 			match.mask->vlan_priority = 0;
1276 		} else {
1277 			flow_rule_match_vlan(rule, &match);
1278 		}
1279 
1280 		if (match.mask->vlan_id) {
1281 			if (match.mask->vlan_id == VLAN_VID_MASK) {
1282 				fltr->flags |= ICE_TC_FLWR_FIELD_VLAN;
1283 			} else {
1284 				NL_SET_ERR_MSG_MOD(fltr->extack, "Bad VLAN mask");
1285 				return -EINVAL;
1286 			}
1287 		}
1288 
1289 		headers->vlan_hdr.vlan_id =
1290 				cpu_to_be16(match.key->vlan_id & VLAN_VID_MASK);
1291 		if (match.mask->vlan_priority)
1292 			headers->vlan_hdr.vlan_prio = match.key->vlan_priority;
1293 		if (match.mask->vlan_tpid)
1294 			headers->vlan_hdr.vlan_tpid = match.key->vlan_tpid;
1295 	}
1296 
1297 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
1298 		struct flow_match_vlan match;
1299 
1300 		if (!ice_is_dvm_ena(&vsi->back->hw)) {
1301 			NL_SET_ERR_MSG_MOD(fltr->extack, "Double VLAN mode is not enabled");
1302 			return -EINVAL;
1303 		}
1304 
1305 		flow_rule_match_cvlan(rule, &match);
1306 
1307 		if (match.mask->vlan_id) {
1308 			if (match.mask->vlan_id == VLAN_VID_MASK) {
1309 				fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN;
1310 			} else {
1311 				NL_SET_ERR_MSG_MOD(fltr->extack,
1312 						   "Bad CVLAN mask");
1313 				return -EINVAL;
1314 			}
1315 		}
1316 
1317 		headers->cvlan_hdr.vlan_id =
1318 				cpu_to_be16(match.key->vlan_id & VLAN_VID_MASK);
1319 		if (match.mask->vlan_priority)
1320 			headers->cvlan_hdr.vlan_prio = match.key->vlan_priority;
1321 	}
1322 
1323 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PPPOE)) {
1324 		struct flow_match_pppoe match;
1325 
1326 		flow_rule_match_pppoe(rule, &match);
1327 		n_proto_key = ice_tc_set_pppoe(&match, fltr, headers);
1328 
1329 		/* If ethertype equals ETH_P_PPP_SES, n_proto might be
1330 		 * overwritten by encapsulated protocol (ppp_proto field) or set
1331 		 * to 0. To correct this, flow_match_pppoe provides the type
1332 		 * field, which contains the actual ethertype (ETH_P_PPP_SES).
1333 		 */
1334 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1335 		headers->l2_mask.n_proto = cpu_to_be16(0xFFFF);
1336 		fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1337 	}
1338 
1339 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
1340 		struct flow_match_control match;
1341 
1342 		flow_rule_match_control(rule, &match);
1343 
1344 		addr_type = match.key->addr_type;
1345 	}
1346 
1347 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1348 		struct flow_match_ipv4_addrs match;
1349 
1350 		flow_rule_match_ipv4_addrs(rule, &match);
1351 		if (ice_tc_set_ipv4(&match, fltr, headers, false))
1352 			return -EINVAL;
1353 	}
1354 
1355 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1356 		struct flow_match_ipv6_addrs match;
1357 
1358 		flow_rule_match_ipv6_addrs(rule, &match);
1359 		if (ice_tc_set_ipv6(&match, fltr, headers, false))
1360 			return -EINVAL;
1361 	}
1362 
1363 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
1364 		struct flow_match_ip match;
1365 
1366 		flow_rule_match_ip(rule, &match);
1367 		ice_tc_set_tos_ttl(&match, fltr, headers, false);
1368 	}
1369 
1370 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_L2TPV3)) {
1371 		struct flow_match_l2tpv3 match;
1372 
1373 		flow_rule_match_l2tpv3(rule, &match);
1374 
1375 		fltr->flags |= ICE_TC_FLWR_FIELD_L2TPV3_SESSID;
1376 		headers->l2tpv3_hdr.session_id = match.key->session_id;
1377 	}
1378 
1379 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
1380 		struct flow_match_ports match;
1381 
1382 		flow_rule_match_ports(rule, &match);
1383 		if (ice_tc_set_port(match, fltr, headers, false))
1384 			return -EINVAL;
1385 		switch (headers->l3_key.ip_proto) {
1386 		case IPPROTO_TCP:
1387 		case IPPROTO_UDP:
1388 			break;
1389 		default:
1390 			NL_SET_ERR_MSG_MOD(fltr->extack, "Only UDP and TCP transport are supported");
1391 			return -EINVAL;
1392 		}
1393 	}
1394 	return 0;
1395 }
1396 
1397 /**
1398  * ice_add_switch_fltr - Add TC flower filters
1399  * @vsi: Pointer to VSI
1400  * @fltr: Pointer to struct ice_tc_flower_fltr
1401  *
1402  * Add filter in HW switch block
1403  */
1404 static int
1405 ice_add_switch_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1406 {
1407 	if (fltr->action.fltr_act == ICE_FWD_TO_QGRP)
1408 		return -EOPNOTSUPP;
1409 
1410 	if (ice_is_eswitch_mode_switchdev(vsi->back))
1411 		return ice_eswitch_add_tc_fltr(vsi, fltr);
1412 
1413 	return ice_add_tc_flower_adv_fltr(vsi, fltr);
1414 }
1415 
1416 /**
1417  * ice_handle_tclass_action - Support directing to a traffic class
1418  * @vsi: Pointer to VSI
1419  * @cls_flower: Pointer to TC flower offload structure
1420  * @fltr: Pointer to TC flower filter structure
1421  *
1422  * Support directing traffic to a traffic class
1423  */
1424 static int
1425 ice_handle_tclass_action(struct ice_vsi *vsi,
1426 			 struct flow_cls_offload *cls_flower,
1427 			 struct ice_tc_flower_fltr *fltr)
1428 {
1429 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
1430 	struct ice_vsi *main_vsi;
1431 
1432 	if (tc < 0) {
1433 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because specified destination is invalid");
1434 		return -EINVAL;
1435 	}
1436 	if (!tc) {
1437 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because of invalid destination");
1438 		return -EINVAL;
1439 	}
1440 
1441 	if (!(vsi->all_enatc & BIT(tc))) {
1442 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because of non-existence destination");
1443 		return -EINVAL;
1444 	}
1445 
1446 	/* Redirect to a TC class or Queue Group */
1447 	main_vsi = ice_get_main_vsi(vsi->back);
1448 	if (!main_vsi || !main_vsi->netdev) {
1449 		NL_SET_ERR_MSG_MOD(fltr->extack,
1450 				   "Unable to add filter because of invalid netdevice");
1451 		return -EINVAL;
1452 	}
1453 
1454 	if ((fltr->flags & ICE_TC_FLWR_FIELD_TENANT_ID) &&
1455 	    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1456 			   ICE_TC_FLWR_FIELD_SRC_MAC))) {
1457 		NL_SET_ERR_MSG_MOD(fltr->extack,
1458 				   "Unable to add filter because filter using tunnel key and inner MAC is unsupported combination");
1459 		return -EOPNOTSUPP;
1460 	}
1461 
1462 	/* For ADQ, filter must include dest MAC address, otherwise unwanted
1463 	 * packets with unrelated MAC address get delivered to ADQ VSIs as long
1464 	 * as remaining filter criteria is satisfied such as dest IP address
1465 	 * and dest/src L4 port. Following code is trying to handle:
1466 	 * 1. For non-tunnel, if user specify MAC addresses, use them (means
1467 	 * this code won't do anything
1468 	 * 2. For non-tunnel, if user didn't specify MAC address, add implicit
1469 	 * dest MAC to be lower netdev's active unicast MAC address
1470 	 * 3. For tunnel,  as of now TC-filter through flower classifier doesn't
1471 	 * have provision for user to specify outer DMAC, hence driver to
1472 	 * implicitly add outer dest MAC to be lower netdev's active unicast
1473 	 * MAC address.
1474 	 */
1475 	if (fltr->tunnel_type != TNL_LAST &&
1476 	    !(fltr->flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC))
1477 		fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DST_MAC;
1478 
1479 	if (fltr->tunnel_type == TNL_LAST &&
1480 	    !(fltr->flags & ICE_TC_FLWR_FIELD_DST_MAC))
1481 		fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1482 
1483 	if (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1484 			   ICE_TC_FLWR_FIELD_ENC_DST_MAC)) {
1485 		ether_addr_copy(fltr->outer_headers.l2_key.dst_mac,
1486 				vsi->netdev->dev_addr);
1487 		eth_broadcast_addr(fltr->outer_headers.l2_mask.dst_mac);
1488 	}
1489 
1490 	/* validate specified dest MAC address, make sure either it belongs to
1491 	 * lower netdev or any of MACVLAN. MACVLANs MAC address are added as
1492 	 * unicast MAC filter destined to main VSI.
1493 	 */
1494 	if (!ice_mac_fltr_exist(&main_vsi->back->hw,
1495 				fltr->outer_headers.l2_key.dst_mac,
1496 				main_vsi->idx)) {
1497 		NL_SET_ERR_MSG_MOD(fltr->extack,
1498 				   "Unable to add filter because legacy MAC filter for specified destination doesn't exist");
1499 		return -EINVAL;
1500 	}
1501 
1502 	/* Make sure VLAN is already added to main VSI, before allowing ADQ to
1503 	 * add a VLAN based filter such as MAC + VLAN + L4 port.
1504 	 */
1505 	if (fltr->flags & ICE_TC_FLWR_FIELD_VLAN) {
1506 		u16 vlan_id = be16_to_cpu(fltr->outer_headers.vlan_hdr.vlan_id);
1507 
1508 		if (!ice_vlan_fltr_exist(&main_vsi->back->hw, vlan_id,
1509 					 main_vsi->idx)) {
1510 			NL_SET_ERR_MSG_MOD(fltr->extack,
1511 					   "Unable to add filter because legacy VLAN filter for specified destination doesn't exist");
1512 			return -EINVAL;
1513 		}
1514 	}
1515 	fltr->action.fltr_act = ICE_FWD_TO_VSI;
1516 	fltr->action.tc_class = tc;
1517 
1518 	return 0;
1519 }
1520 
1521 /**
1522  * ice_parse_tc_flower_actions - Parse the actions for a TC filter
1523  * @vsi: Pointer to VSI
1524  * @cls_flower: Pointer to TC flower offload structure
1525  * @fltr: Pointer to TC flower filter structure
1526  *
1527  * Parse the actions for a TC filter
1528  */
1529 static int
1530 ice_parse_tc_flower_actions(struct ice_vsi *vsi,
1531 			    struct flow_cls_offload *cls_flower,
1532 			    struct ice_tc_flower_fltr *fltr)
1533 {
1534 	struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1535 	struct flow_action *flow_action = &rule->action;
1536 	struct flow_action_entry *act;
1537 	int i;
1538 
1539 	if (cls_flower->classid)
1540 		return ice_handle_tclass_action(vsi, cls_flower, fltr);
1541 
1542 	if (!flow_action_has_entries(flow_action))
1543 		return -EINVAL;
1544 
1545 	flow_action_for_each(i, act, flow_action) {
1546 		if (ice_is_eswitch_mode_switchdev(vsi->back)) {
1547 			int err = ice_eswitch_tc_parse_action(fltr, act);
1548 
1549 			if (err)
1550 				return err;
1551 			continue;
1552 		}
1553 		/* Allow only one rule per filter */
1554 
1555 		/* Drop action */
1556 		if (act->id == FLOW_ACTION_DROP) {
1557 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action DROP");
1558 			return -EINVAL;
1559 		}
1560 		fltr->action.fltr_act = ICE_FWD_TO_VSI;
1561 	}
1562 	return 0;
1563 }
1564 
1565 /**
1566  * ice_del_tc_fltr - deletes a filter from HW table
1567  * @vsi: Pointer to VSI
1568  * @fltr: Pointer to struct ice_tc_flower_fltr
1569  *
1570  * This function deletes a filter from HW table and manages book-keeping
1571  */
1572 static int ice_del_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1573 {
1574 	struct ice_rule_query_data rule_rem;
1575 	struct ice_pf *pf = vsi->back;
1576 	int err;
1577 
1578 	rule_rem.rid = fltr->rid;
1579 	rule_rem.rule_id = fltr->rule_id;
1580 	rule_rem.vsi_handle = fltr->dest_id;
1581 	err = ice_rem_adv_rule_by_id(&pf->hw, &rule_rem);
1582 	if (err) {
1583 		if (err == -ENOENT) {
1584 			NL_SET_ERR_MSG_MOD(fltr->extack, "Filter does not exist");
1585 			return -ENOENT;
1586 		}
1587 		NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to delete TC flower filter");
1588 		return -EIO;
1589 	}
1590 
1591 	/* update advanced switch filter count for destination
1592 	 * VSI if filter destination was VSI
1593 	 */
1594 	if (fltr->dest_vsi) {
1595 		if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
1596 			fltr->dest_vsi->num_chnl_fltr--;
1597 
1598 			/* keeps track of channel filters for PF VSI */
1599 			if (vsi->type == ICE_VSI_PF &&
1600 			    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1601 					    ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
1602 				pf->num_dmac_chnl_fltrs--;
1603 		}
1604 	}
1605 	return 0;
1606 }
1607 
1608 /**
1609  * ice_add_tc_fltr - adds a TC flower filter
1610  * @netdev: Pointer to netdev
1611  * @vsi: Pointer to VSI
1612  * @f: Pointer to flower offload structure
1613  * @__fltr: Pointer to struct ice_tc_flower_fltr
1614  *
1615  * This function parses TC-flower input fields, parses action,
1616  * and adds a filter.
1617  */
1618 static int
1619 ice_add_tc_fltr(struct net_device *netdev, struct ice_vsi *vsi,
1620 		struct flow_cls_offload *f,
1621 		struct ice_tc_flower_fltr **__fltr)
1622 {
1623 	struct ice_tc_flower_fltr *fltr;
1624 	int err;
1625 
1626 	/* by default, set output to be INVALID */
1627 	*__fltr = NULL;
1628 
1629 	fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1630 	if (!fltr)
1631 		return -ENOMEM;
1632 
1633 	fltr->cookie = f->cookie;
1634 	fltr->extack = f->common.extack;
1635 	fltr->src_vsi = vsi;
1636 	INIT_HLIST_NODE(&fltr->tc_flower_node);
1637 
1638 	err = ice_parse_cls_flower(netdev, vsi, f, fltr);
1639 	if (err < 0)
1640 		goto err;
1641 
1642 	err = ice_parse_tc_flower_actions(vsi, f, fltr);
1643 	if (err < 0)
1644 		goto err;
1645 
1646 	err = ice_add_switch_fltr(vsi, fltr);
1647 	if (err < 0)
1648 		goto err;
1649 
1650 	/* return the newly created filter */
1651 	*__fltr = fltr;
1652 
1653 	return 0;
1654 err:
1655 	kfree(fltr);
1656 	return err;
1657 }
1658 
1659 /**
1660  * ice_find_tc_flower_fltr - Find the TC flower filter in the list
1661  * @pf: Pointer to PF
1662  * @cookie: filter specific cookie
1663  */
1664 static struct ice_tc_flower_fltr *
1665 ice_find_tc_flower_fltr(struct ice_pf *pf, unsigned long cookie)
1666 {
1667 	struct ice_tc_flower_fltr *fltr;
1668 
1669 	hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
1670 		if (cookie == fltr->cookie)
1671 			return fltr;
1672 
1673 	return NULL;
1674 }
1675 
1676 /**
1677  * ice_add_cls_flower - add TC flower filters
1678  * @netdev: Pointer to filter device
1679  * @vsi: Pointer to VSI
1680  * @cls_flower: Pointer to flower offload structure
1681  */
1682 int
1683 ice_add_cls_flower(struct net_device *netdev, struct ice_vsi *vsi,
1684 		   struct flow_cls_offload *cls_flower)
1685 {
1686 	struct netlink_ext_ack *extack = cls_flower->common.extack;
1687 	struct net_device *vsi_netdev = vsi->netdev;
1688 	struct ice_tc_flower_fltr *fltr;
1689 	struct ice_pf *pf = vsi->back;
1690 	int err;
1691 
1692 	if (ice_is_reset_in_progress(pf->state))
1693 		return -EBUSY;
1694 	if (test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
1695 		return -EINVAL;
1696 
1697 	if (ice_is_port_repr_netdev(netdev))
1698 		vsi_netdev = netdev;
1699 
1700 	if (!(vsi_netdev->features & NETIF_F_HW_TC) &&
1701 	    !test_bit(ICE_FLAG_CLS_FLOWER, pf->flags)) {
1702 		/* Based on TC indirect notifications from kernel, all ice
1703 		 * devices get an instance of rule from higher level device.
1704 		 * Avoid triggering explicit error in this case.
1705 		 */
1706 		if (netdev == vsi_netdev)
1707 			NL_SET_ERR_MSG_MOD(extack, "can't apply TC flower filters, turn ON hw-tc-offload and try again");
1708 		return -EINVAL;
1709 	}
1710 
1711 	/* avoid duplicate entries, if exists - return error */
1712 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1713 	if (fltr) {
1714 		NL_SET_ERR_MSG_MOD(extack, "filter cookie already exists, ignoring");
1715 		return -EEXIST;
1716 	}
1717 
1718 	/* prep and add TC-flower filter in HW */
1719 	err = ice_add_tc_fltr(netdev, vsi, cls_flower, &fltr);
1720 	if (err)
1721 		return err;
1722 
1723 	/* add filter into an ordered list */
1724 	hlist_add_head(&fltr->tc_flower_node, &pf->tc_flower_fltr_list);
1725 	return 0;
1726 }
1727 
1728 /**
1729  * ice_del_cls_flower - delete TC flower filters
1730  * @vsi: Pointer to VSI
1731  * @cls_flower: Pointer to struct flow_cls_offload
1732  */
1733 int
1734 ice_del_cls_flower(struct ice_vsi *vsi, struct flow_cls_offload *cls_flower)
1735 {
1736 	struct ice_tc_flower_fltr *fltr;
1737 	struct ice_pf *pf = vsi->back;
1738 	int err;
1739 
1740 	/* find filter */
1741 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1742 	if (!fltr) {
1743 		if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) &&
1744 		    hlist_empty(&pf->tc_flower_fltr_list))
1745 			return 0;
1746 
1747 		NL_SET_ERR_MSG_MOD(cls_flower->common.extack, "failed to delete TC flower filter because unable to find it");
1748 		return -EINVAL;
1749 	}
1750 
1751 	fltr->extack = cls_flower->common.extack;
1752 	/* delete filter from HW */
1753 	err = ice_del_tc_fltr(vsi, fltr);
1754 	if (err)
1755 		return err;
1756 
1757 	/* delete filter from an ordered list */
1758 	hlist_del(&fltr->tc_flower_node);
1759 
1760 	/* free the filter node */
1761 	kfree(fltr);
1762 
1763 	return 0;
1764 }
1765 
1766 /**
1767  * ice_replay_tc_fltrs - replay TC filters
1768  * @pf: pointer to PF struct
1769  */
1770 void ice_replay_tc_fltrs(struct ice_pf *pf)
1771 {
1772 	struct ice_tc_flower_fltr *fltr;
1773 	struct hlist_node *node;
1774 
1775 	hlist_for_each_entry_safe(fltr, node,
1776 				  &pf->tc_flower_fltr_list,
1777 				  tc_flower_node) {
1778 		fltr->extack = NULL;
1779 		ice_add_switch_fltr(fltr->src_vsi, fltr);
1780 	}
1781 }
1782