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 | ICE_TC_FLWR_FIELD_VLAN_PRIO))
55 		lkups_cnt++;
56 
57 	/* is CVLAN specified? */
58 	if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO))
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 | ICE_TC_FLWR_FIELD_VLAN_PRIO)) {
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 
402 		if (flags & ICE_TC_FLWR_FIELD_VLAN) {
403 			list[i].h_u.vlan_hdr.vlan = headers->vlan_hdr.vlan_id;
404 			list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
405 		}
406 
407 		if (flags & ICE_TC_FLWR_FIELD_VLAN_PRIO) {
408 			if (flags & ICE_TC_FLWR_FIELD_VLAN) {
409 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
410 			} else {
411 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
412 				list[i].h_u.vlan_hdr.vlan = 0;
413 			}
414 			list[i].h_u.vlan_hdr.vlan |=
415 				headers->vlan_hdr.vlan_prio;
416 		}
417 
418 		i++;
419 	}
420 
421 	if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO)) {
422 		list[i].type = ICE_VLAN_IN;
423 
424 		if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
425 			list[i].h_u.vlan_hdr.vlan = headers->cvlan_hdr.vlan_id;
426 			list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
427 		}
428 
429 		if (flags & ICE_TC_FLWR_FIELD_CVLAN_PRIO) {
430 			if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
431 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
432 			} else {
433 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
434 				list[i].h_u.vlan_hdr.vlan = 0;
435 			}
436 			list[i].h_u.vlan_hdr.vlan |=
437 				headers->cvlan_hdr.vlan_prio;
438 		}
439 
440 		i++;
441 	}
442 
443 	if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
444 		     ICE_TC_FLWR_FIELD_PPP_PROTO)) {
445 		struct ice_pppoe_hdr *vals, *masks;
446 
447 		vals = &list[i].h_u.pppoe_hdr;
448 		masks = &list[i].m_u.pppoe_hdr;
449 
450 		list[i].type = ICE_PPPOE;
451 
452 		if (flags & ICE_TC_FLWR_FIELD_PPPOE_SESSID) {
453 			vals->session_id = headers->pppoe_hdr.session_id;
454 			masks->session_id = cpu_to_be16(0xFFFF);
455 		}
456 
457 		if (flags & ICE_TC_FLWR_FIELD_PPP_PROTO) {
458 			vals->ppp_prot_id = headers->pppoe_hdr.ppp_proto;
459 			masks->ppp_prot_id = cpu_to_be16(0xFFFF);
460 		}
461 
462 		i++;
463 	}
464 
465 	/* copy L3 (IPv[4|6]: src, dest) address */
466 	if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 |
467 		     ICE_TC_FLWR_FIELD_SRC_IPV4)) {
468 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
469 
470 		list[i].type = ice_proto_type_from_ipv4(inner);
471 		l3_key = &headers->l3_key;
472 		l3_mask = &headers->l3_mask;
473 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV4) {
474 			list[i].h_u.ipv4_hdr.dst_addr = l3_key->dst_ipv4;
475 			list[i].m_u.ipv4_hdr.dst_addr = l3_mask->dst_ipv4;
476 		}
477 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV4) {
478 			list[i].h_u.ipv4_hdr.src_addr = l3_key->src_ipv4;
479 			list[i].m_u.ipv4_hdr.src_addr = l3_mask->src_ipv4;
480 		}
481 		i++;
482 	} else if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV6 |
483 			    ICE_TC_FLWR_FIELD_SRC_IPV6)) {
484 		struct ice_ipv6_hdr *ipv6_hdr, *ipv6_mask;
485 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
486 
487 		list[i].type = ice_proto_type_from_ipv6(inner);
488 		ipv6_hdr = &list[i].h_u.ipv6_hdr;
489 		ipv6_mask = &list[i].m_u.ipv6_hdr;
490 		l3_key = &headers->l3_key;
491 		l3_mask = &headers->l3_mask;
492 
493 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV6) {
494 			memcpy(&ipv6_hdr->dst_addr, &l3_key->dst_ipv6_addr,
495 			       sizeof(l3_key->dst_ipv6_addr));
496 			memcpy(&ipv6_mask->dst_addr, &l3_mask->dst_ipv6_addr,
497 			       sizeof(l3_mask->dst_ipv6_addr));
498 		}
499 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV6) {
500 			memcpy(&ipv6_hdr->src_addr, &l3_key->src_ipv6_addr,
501 			       sizeof(l3_key->src_ipv6_addr));
502 			memcpy(&ipv6_mask->src_addr, &l3_mask->src_ipv6_addr,
503 			       sizeof(l3_mask->src_ipv6_addr));
504 		}
505 		i++;
506 	}
507 
508 	if (headers->l2_key.n_proto == htons(ETH_P_IP) &&
509 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
510 		list[i].type = ice_proto_type_from_ipv4(inner);
511 
512 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
513 			list[i].h_u.ipv4_hdr.tos = headers->l3_key.tos;
514 			list[i].m_u.ipv4_hdr.tos = headers->l3_mask.tos;
515 		}
516 
517 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
518 			list[i].h_u.ipv4_hdr.time_to_live =
519 				headers->l3_key.ttl;
520 			list[i].m_u.ipv4_hdr.time_to_live =
521 				headers->l3_mask.ttl;
522 		}
523 
524 		i++;
525 	}
526 
527 	if (headers->l2_key.n_proto == htons(ETH_P_IPV6) &&
528 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
529 		struct ice_ipv6_hdr *hdr_h, *hdr_m;
530 
531 		hdr_h = &list[i].h_u.ipv6_hdr;
532 		hdr_m = &list[i].m_u.ipv6_hdr;
533 		list[i].type = ice_proto_type_from_ipv6(inner);
534 
535 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
536 			be32p_replace_bits(&hdr_h->be_ver_tc_flow,
537 					   headers->l3_key.tos,
538 					   ICE_IPV6_HDR_TC_MASK);
539 			be32p_replace_bits(&hdr_m->be_ver_tc_flow,
540 					   headers->l3_mask.tos,
541 					   ICE_IPV6_HDR_TC_MASK);
542 		}
543 
544 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
545 			hdr_h->hop_limit = headers->l3_key.ttl;
546 			hdr_m->hop_limit = headers->l3_mask.ttl;
547 		}
548 
549 		i++;
550 	}
551 
552 	if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID) {
553 		list[i].type = ICE_L2TPV3;
554 
555 		list[i].h_u.l2tpv3_sess_hdr.session_id =
556 			headers->l2tpv3_hdr.session_id;
557 		list[i].m_u.l2tpv3_sess_hdr.session_id =
558 			cpu_to_be32(0xFFFFFFFF);
559 
560 		i++;
561 	}
562 
563 	/* copy L4 (src, dest) port */
564 	if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
565 		     ICE_TC_FLWR_FIELD_SRC_L4_PORT)) {
566 		struct ice_tc_l4_hdr *l4_key, *l4_mask;
567 
568 		list[i].type = ice_proto_type_from_l4_port(headers->l3_key.ip_proto);
569 		l4_key = &headers->l4_key;
570 		l4_mask = &headers->l4_mask;
571 
572 		if (flags & ICE_TC_FLWR_FIELD_DEST_L4_PORT) {
573 			list[i].h_u.l4_hdr.dst_port = l4_key->dst_port;
574 			list[i].m_u.l4_hdr.dst_port = l4_mask->dst_port;
575 		}
576 		if (flags & ICE_TC_FLWR_FIELD_SRC_L4_PORT) {
577 			list[i].h_u.l4_hdr.src_port = l4_key->src_port;
578 			list[i].m_u.l4_hdr.src_port = l4_mask->src_port;
579 		}
580 		i++;
581 	}
582 
583 	return i;
584 }
585 
586 /**
587  * ice_tc_tun_get_type - get the tunnel type
588  * @tunnel_dev: ptr to tunnel device
589  *
590  * This function detects appropriate tunnel_type if specified device is
591  * tunnel device such as VXLAN/Geneve
592  */
593 static int ice_tc_tun_get_type(struct net_device *tunnel_dev)
594 {
595 	if (netif_is_vxlan(tunnel_dev))
596 		return TNL_VXLAN;
597 	if (netif_is_geneve(tunnel_dev))
598 		return TNL_GENEVE;
599 	if (netif_is_gretap(tunnel_dev) ||
600 	    netif_is_ip6gretap(tunnel_dev))
601 		return TNL_GRETAP;
602 
603 	/* Assume GTP-U by default in case of GTP netdev.
604 	 * GTP-C may be selected later, based on enc_dst_port.
605 	 */
606 	if (netif_is_gtp(tunnel_dev))
607 		return TNL_GTPU;
608 	return TNL_LAST;
609 }
610 
611 bool ice_is_tunnel_supported(struct net_device *dev)
612 {
613 	return ice_tc_tun_get_type(dev) != TNL_LAST;
614 }
615 
616 static int
617 ice_eswitch_tc_parse_action(struct ice_tc_flower_fltr *fltr,
618 			    struct flow_action_entry *act)
619 {
620 	struct ice_repr *repr;
621 
622 	switch (act->id) {
623 	case FLOW_ACTION_DROP:
624 		fltr->action.fltr_act = ICE_DROP_PACKET;
625 		break;
626 
627 	case FLOW_ACTION_REDIRECT:
628 		fltr->action.fltr_act = ICE_FWD_TO_VSI;
629 
630 		if (ice_is_port_repr_netdev(act->dev)) {
631 			repr = ice_netdev_to_repr(act->dev);
632 
633 			fltr->dest_vsi = repr->src_vsi;
634 			fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
635 		} else if (netif_is_ice(act->dev) ||
636 			   ice_is_tunnel_supported(act->dev)) {
637 			fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
638 		} else {
639 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported netdevice in switchdev mode");
640 			return -EINVAL;
641 		}
642 
643 		break;
644 
645 	default:
646 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action in switchdev mode");
647 		return -EINVAL;
648 	}
649 
650 	return 0;
651 }
652 
653 static int
654 ice_eswitch_add_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
655 {
656 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
657 	struct ice_adv_rule_info rule_info = { 0 };
658 	struct ice_rule_query_data rule_added;
659 	struct ice_hw *hw = &vsi->back->hw;
660 	struct ice_adv_lkup_elem *list;
661 	u32 flags = fltr->flags;
662 	int lkups_cnt;
663 	int ret;
664 	int i;
665 
666 	if (!flags || (flags & ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT)) {
667 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported encap field(s)");
668 		return -EOPNOTSUPP;
669 	}
670 
671 	lkups_cnt = ice_tc_count_lkups(flags, headers, fltr);
672 	list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
673 	if (!list)
674 		return -ENOMEM;
675 
676 	i = ice_tc_fill_rules(hw, flags, fltr, list, &rule_info, NULL);
677 	if (i != lkups_cnt) {
678 		ret = -EINVAL;
679 		goto exit;
680 	}
681 
682 	/* egress traffic is always redirect to uplink */
683 	if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS)
684 		fltr->dest_vsi = vsi->back->switchdev.uplink_vsi;
685 
686 	rule_info.sw_act.fltr_act = fltr->action.fltr_act;
687 	if (fltr->action.fltr_act != ICE_DROP_PACKET)
688 		rule_info.sw_act.vsi_handle = fltr->dest_vsi->idx;
689 	/* For now, making priority to be highest, and it also becomes
690 	 * the priority for recipe which will get created as a result of
691 	 * new extraction sequence based on input set.
692 	 * Priority '7' is max val for switch recipe, higher the number
693 	 * results into order of switch rule evaluation.
694 	 */
695 	rule_info.priority = 7;
696 	rule_info.flags_info.act_valid = true;
697 
698 	if (fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
699 		rule_info.sw_act.flag |= ICE_FLTR_RX;
700 		rule_info.sw_act.src = hw->pf_id;
701 		rule_info.rx = true;
702 		rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
703 	} else {
704 		rule_info.sw_act.flag |= ICE_FLTR_TX;
705 		rule_info.sw_act.src = vsi->idx;
706 		rule_info.rx = false;
707 		rule_info.flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
708 	}
709 
710 	/* specify the cookie as filter_rule_id */
711 	rule_info.fltr_rule_id = fltr->cookie;
712 
713 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
714 	if (ret == -EEXIST) {
715 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because it already exist");
716 		ret = -EINVAL;
717 		goto exit;
718 	} else if (ret) {
719 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter due to error");
720 		goto exit;
721 	}
722 
723 	/* store the output params, which are needed later for removing
724 	 * advanced switch filter
725 	 */
726 	fltr->rid = rule_added.rid;
727 	fltr->rule_id = rule_added.rule_id;
728 	fltr->dest_vsi_handle = rule_added.vsi_handle;
729 
730 exit:
731 	kfree(list);
732 	return ret;
733 }
734 
735 /**
736  * ice_locate_vsi_using_queue - locate VSI using queue (forward to queue action)
737  * @vsi: Pointer to VSI
738  * @tc_fltr: Pointer to tc_flower_filter
739  *
740  * Locate the VSI using specified queue. When ADQ is not enabled, always
741  * return input VSI, otherwise locate corresponding VSI based on per channel
742  * offset and qcount
743  */
744 static struct ice_vsi *
745 ice_locate_vsi_using_queue(struct ice_vsi *vsi,
746 			   struct ice_tc_flower_fltr *tc_fltr)
747 {
748 	int num_tc, tc, queue;
749 
750 	/* if ADQ is not active, passed VSI is the candidate VSI */
751 	if (!ice_is_adq_active(vsi->back))
752 		return vsi;
753 
754 	/* Locate the VSI (it could still be main PF VSI or CHNL_VSI depending
755 	 * upon queue number)
756 	 */
757 	num_tc = vsi->mqprio_qopt.qopt.num_tc;
758 	queue = tc_fltr->action.fwd.q.queue;
759 
760 	for (tc = 0; tc < num_tc; tc++) {
761 		int qcount = vsi->mqprio_qopt.qopt.count[tc];
762 		int offset = vsi->mqprio_qopt.qopt.offset[tc];
763 
764 		if (queue >= offset && queue < offset + qcount) {
765 			/* for non-ADQ TCs, passed VSI is the candidate VSI */
766 			if (tc < ICE_CHNL_START_TC)
767 				return vsi;
768 			else
769 				return vsi->tc_map_vsi[tc];
770 		}
771 	}
772 	return NULL;
773 }
774 
775 static struct ice_rx_ring *
776 ice_locate_rx_ring_using_queue(struct ice_vsi *vsi,
777 			       struct ice_tc_flower_fltr *tc_fltr)
778 {
779 	u16 queue = tc_fltr->action.fwd.q.queue;
780 
781 	return queue < vsi->num_rxq ? vsi->rx_rings[queue] : NULL;
782 }
783 
784 /**
785  * ice_tc_forward_action - Determine destination VSI and queue for the action
786  * @vsi: Pointer to VSI
787  * @tc_fltr: Pointer to TC flower filter structure
788  *
789  * Validates the tc forward action and determines the destination VSI and queue
790  * for the forward action.
791  */
792 static struct ice_vsi *
793 ice_tc_forward_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *tc_fltr)
794 {
795 	struct ice_rx_ring *ring = NULL;
796 	struct ice_vsi *dest_vsi = NULL;
797 	struct ice_pf *pf = vsi->back;
798 	struct device *dev;
799 	u32 tc_class;
800 
801 	dev = ice_pf_to_dev(pf);
802 
803 	/* Get the destination VSI and/or destination queue and validate them */
804 	switch (tc_fltr->action.fltr_act) {
805 	case ICE_FWD_TO_VSI:
806 		tc_class = tc_fltr->action.fwd.tc.tc_class;
807 		/* Select the destination VSI */
808 		if (tc_class < ICE_CHNL_START_TC) {
809 			NL_SET_ERR_MSG_MOD(tc_fltr->extack,
810 					   "Unable to add filter because of unsupported destination");
811 			return ERR_PTR(-EOPNOTSUPP);
812 		}
813 		/* Locate ADQ VSI depending on hw_tc number */
814 		dest_vsi = vsi->tc_map_vsi[tc_class];
815 		break;
816 	case ICE_FWD_TO_Q:
817 		/* Locate the Rx queue */
818 		ring = ice_locate_rx_ring_using_queue(vsi, tc_fltr);
819 		if (!ring) {
820 			dev_err(dev,
821 				"Unable to locate Rx queue for action fwd_to_queue: %u\n",
822 				tc_fltr->action.fwd.q.queue);
823 			return ERR_PTR(-EINVAL);
824 		}
825 		/* Determine destination VSI even though the action is
826 		 * FWD_TO_QUEUE, because QUEUE is associated with VSI
827 		 */
828 		dest_vsi = tc_fltr->dest_vsi;
829 		break;
830 	default:
831 		dev_err(dev,
832 			"Unable to add filter because of unsupported action %u (supported actions: fwd to tc, fwd to queue)\n",
833 			tc_fltr->action.fltr_act);
834 		return ERR_PTR(-EINVAL);
835 	}
836 	/* Must have valid dest_vsi (it could be main VSI or ADQ VSI) */
837 	if (!dest_vsi) {
838 		dev_err(dev,
839 			"Unable to add filter because specified destination VSI doesn't exist\n");
840 		return ERR_PTR(-EINVAL);
841 	}
842 	return dest_vsi;
843 }
844 
845 /**
846  * ice_add_tc_flower_adv_fltr - add appropriate filter rules
847  * @vsi: Pointer to VSI
848  * @tc_fltr: Pointer to TC flower filter structure
849  *
850  * based on filter parameters using Advance recipes supported
851  * by OS package.
852  */
853 static int
854 ice_add_tc_flower_adv_fltr(struct ice_vsi *vsi,
855 			   struct ice_tc_flower_fltr *tc_fltr)
856 {
857 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
858 	struct ice_adv_rule_info rule_info = {0};
859 	struct ice_rule_query_data rule_added;
860 	struct ice_adv_lkup_elem *list;
861 	struct ice_pf *pf = vsi->back;
862 	struct ice_hw *hw = &pf->hw;
863 	u32 flags = tc_fltr->flags;
864 	struct ice_vsi *dest_vsi;
865 	struct device *dev;
866 	u16 lkups_cnt = 0;
867 	u16 l4_proto = 0;
868 	int ret = 0;
869 	u16 i = 0;
870 
871 	dev = ice_pf_to_dev(pf);
872 	if (ice_is_safe_mode(pf)) {
873 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because driver is in safe mode");
874 		return -EOPNOTSUPP;
875 	}
876 
877 	if (!flags || (flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
878 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
879 				ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
880 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
881 				ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT))) {
882 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unsupported encap field(s)");
883 		return -EOPNOTSUPP;
884 	}
885 
886 	/* validate forwarding action VSI and queue */
887 	if (ice_is_forward_action(tc_fltr->action.fltr_act)) {
888 		dest_vsi = ice_tc_forward_action(vsi, tc_fltr);
889 		if (IS_ERR(dest_vsi))
890 			return PTR_ERR(dest_vsi);
891 	}
892 
893 	lkups_cnt = ice_tc_count_lkups(flags, headers, tc_fltr);
894 	list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
895 	if (!list)
896 		return -ENOMEM;
897 
898 	i = ice_tc_fill_rules(hw, flags, tc_fltr, list, &rule_info, &l4_proto);
899 	if (i != lkups_cnt) {
900 		ret = -EINVAL;
901 		goto exit;
902 	}
903 
904 	rule_info.sw_act.fltr_act = tc_fltr->action.fltr_act;
905 	/* specify the cookie as filter_rule_id */
906 	rule_info.fltr_rule_id = tc_fltr->cookie;
907 
908 	switch (tc_fltr->action.fltr_act) {
909 	case ICE_FWD_TO_VSI:
910 		rule_info.sw_act.vsi_handle = dest_vsi->idx;
911 		rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
912 		rule_info.sw_act.src = hw->pf_id;
913 		rule_info.rx = true;
914 		dev_dbg(dev, "add switch rule for TC:%u vsi_idx:%u, lkups_cnt:%u\n",
915 			tc_fltr->action.fwd.tc.tc_class,
916 			rule_info.sw_act.vsi_handle, lkups_cnt);
917 		break;
918 	case ICE_FWD_TO_Q:
919 		/* HW queue number in global space */
920 		rule_info.sw_act.fwd_id.q_id = tc_fltr->action.fwd.q.hw_queue;
921 		rule_info.sw_act.vsi_handle = dest_vsi->idx;
922 		rule_info.priority = ICE_SWITCH_FLTR_PRIO_QUEUE;
923 		rule_info.sw_act.src = hw->pf_id;
924 		rule_info.rx = true;
925 		dev_dbg(dev, "add switch rule action to forward to queue:%u (HW queue %u), lkups_cnt:%u\n",
926 			tc_fltr->action.fwd.q.queue,
927 			tc_fltr->action.fwd.q.hw_queue, lkups_cnt);
928 		break;
929 	case ICE_DROP_PACKET:
930 		rule_info.sw_act.flag |= ICE_FLTR_RX;
931 		rule_info.sw_act.src = hw->pf_id;
932 		rule_info.rx = true;
933 		rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
934 		break;
935 	default:
936 		ret = -EOPNOTSUPP;
937 		goto exit;
938 	}
939 
940 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
941 	if (ret == -EEXIST) {
942 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
943 				   "Unable to add filter because it already exist");
944 		ret = -EINVAL;
945 		goto exit;
946 	} else if (ret) {
947 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
948 				   "Unable to add filter due to error");
949 		goto exit;
950 	}
951 
952 	/* store the output params, which are needed later for removing
953 	 * advanced switch filter
954 	 */
955 	tc_fltr->rid = rule_added.rid;
956 	tc_fltr->rule_id = rule_added.rule_id;
957 	tc_fltr->dest_vsi_handle = rule_added.vsi_handle;
958 	if (tc_fltr->action.fltr_act == ICE_FWD_TO_VSI ||
959 	    tc_fltr->action.fltr_act == ICE_FWD_TO_Q) {
960 		tc_fltr->dest_vsi = dest_vsi;
961 		/* keep track of advanced switch filter for
962 		 * destination VSI
963 		 */
964 		dest_vsi->num_chnl_fltr++;
965 
966 		/* keeps track of channel filters for PF VSI */
967 		if (vsi->type == ICE_VSI_PF &&
968 		    (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
969 			      ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
970 			pf->num_dmac_chnl_fltrs++;
971 	}
972 	switch (tc_fltr->action.fltr_act) {
973 	case ICE_FWD_TO_VSI:
974 		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",
975 			lkups_cnt, flags,
976 			tc_fltr->action.fwd.tc.tc_class, rule_added.rid,
977 			rule_added.rule_id, rule_added.vsi_handle);
978 		break;
979 	case ICE_FWD_TO_Q:
980 		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",
981 			lkups_cnt, flags, tc_fltr->action.fwd.q.queue,
982 			tc_fltr->action.fwd.q.hw_queue, rule_added.rid,
983 			rule_added.rule_id);
984 		break;
985 	case ICE_DROP_PACKET:
986 		dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is drop, rid %u, rule_id %u\n",
987 			lkups_cnt, flags, rule_added.rid, rule_added.rule_id);
988 		break;
989 	default:
990 		break;
991 	}
992 exit:
993 	kfree(list);
994 	return ret;
995 }
996 
997 /**
998  * ice_tc_set_pppoe - Parse PPPoE fields from TC flower filter
999  * @match: Pointer to flow match structure
1000  * @fltr: Pointer to filter structure
1001  * @headers: Pointer to outer header fields
1002  * @returns PPP protocol used in filter (ppp_ses or ppp_disc)
1003  */
1004 static u16
1005 ice_tc_set_pppoe(struct flow_match_pppoe *match,
1006 		 struct ice_tc_flower_fltr *fltr,
1007 		 struct ice_tc_flower_lyr_2_4_hdrs *headers)
1008 {
1009 	if (match->mask->session_id) {
1010 		fltr->flags |= ICE_TC_FLWR_FIELD_PPPOE_SESSID;
1011 		headers->pppoe_hdr.session_id = match->key->session_id;
1012 	}
1013 
1014 	if (match->mask->ppp_proto) {
1015 		fltr->flags |= ICE_TC_FLWR_FIELD_PPP_PROTO;
1016 		headers->pppoe_hdr.ppp_proto = match->key->ppp_proto;
1017 	}
1018 
1019 	return be16_to_cpu(match->key->type);
1020 }
1021 
1022 /**
1023  * ice_tc_set_ipv4 - Parse IPv4 addresses from TC flower filter
1024  * @match: Pointer to flow match structure
1025  * @fltr: Pointer to filter structure
1026  * @headers: inner or outer header fields
1027  * @is_encap: set true for tunnel IPv4 address
1028  */
1029 static int
1030 ice_tc_set_ipv4(struct flow_match_ipv4_addrs *match,
1031 		struct ice_tc_flower_fltr *fltr,
1032 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1033 {
1034 	if (match->key->dst) {
1035 		if (is_encap)
1036 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV4;
1037 		else
1038 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV4;
1039 		headers->l3_key.dst_ipv4 = match->key->dst;
1040 		headers->l3_mask.dst_ipv4 = match->mask->dst;
1041 	}
1042 	if (match->key->src) {
1043 		if (is_encap)
1044 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV4;
1045 		else
1046 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV4;
1047 		headers->l3_key.src_ipv4 = match->key->src;
1048 		headers->l3_mask.src_ipv4 = match->mask->src;
1049 	}
1050 	return 0;
1051 }
1052 
1053 /**
1054  * ice_tc_set_ipv6 - Parse IPv6 addresses from TC flower filter
1055  * @match: Pointer to flow match structure
1056  * @fltr: Pointer to filter structure
1057  * @headers: inner or outer header fields
1058  * @is_encap: set true for tunnel IPv6 address
1059  */
1060 static int
1061 ice_tc_set_ipv6(struct flow_match_ipv6_addrs *match,
1062 		struct ice_tc_flower_fltr *fltr,
1063 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1064 {
1065 	struct ice_tc_l3_hdr *l3_key, *l3_mask;
1066 
1067 	/* src and dest IPV6 address should not be LOOPBACK
1068 	 * (0:0:0:0:0:0:0:1), which can be represented as ::1
1069 	 */
1070 	if (ipv6_addr_loopback(&match->key->dst) ||
1071 	    ipv6_addr_loopback(&match->key->src)) {
1072 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad IPv6, addr is LOOPBACK");
1073 		return -EINVAL;
1074 	}
1075 	/* if src/dest IPv6 address is *,* error */
1076 	if (ipv6_addr_any(&match->mask->dst) &&
1077 	    ipv6_addr_any(&match->mask->src)) {
1078 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad src/dest IPv6, addr is any");
1079 		return -EINVAL;
1080 	}
1081 	if (!ipv6_addr_any(&match->mask->dst)) {
1082 		if (is_encap)
1083 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV6;
1084 		else
1085 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV6;
1086 	}
1087 	if (!ipv6_addr_any(&match->mask->src)) {
1088 		if (is_encap)
1089 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV6;
1090 		else
1091 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV6;
1092 	}
1093 
1094 	l3_key = &headers->l3_key;
1095 	l3_mask = &headers->l3_mask;
1096 
1097 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
1098 			   ICE_TC_FLWR_FIELD_SRC_IPV6)) {
1099 		memcpy(&l3_key->src_ipv6_addr, &match->key->src.s6_addr,
1100 		       sizeof(match->key->src.s6_addr));
1101 		memcpy(&l3_mask->src_ipv6_addr, &match->mask->src.s6_addr,
1102 		       sizeof(match->mask->src.s6_addr));
1103 	}
1104 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
1105 			   ICE_TC_FLWR_FIELD_DEST_IPV6)) {
1106 		memcpy(&l3_key->dst_ipv6_addr, &match->key->dst.s6_addr,
1107 		       sizeof(match->key->dst.s6_addr));
1108 		memcpy(&l3_mask->dst_ipv6_addr, &match->mask->dst.s6_addr,
1109 		       sizeof(match->mask->dst.s6_addr));
1110 	}
1111 
1112 	return 0;
1113 }
1114 
1115 /**
1116  * ice_tc_set_tos_ttl - Parse IP ToS/TTL from TC flower filter
1117  * @match: Pointer to flow match structure
1118  * @fltr: Pointer to filter structure
1119  * @headers: inner or outer header fields
1120  * @is_encap: set true for tunnel
1121  */
1122 static void
1123 ice_tc_set_tos_ttl(struct flow_match_ip *match,
1124 		   struct ice_tc_flower_fltr *fltr,
1125 		   struct ice_tc_flower_lyr_2_4_hdrs *headers,
1126 		   bool is_encap)
1127 {
1128 	if (match->mask->tos) {
1129 		if (is_encap)
1130 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TOS;
1131 		else
1132 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TOS;
1133 
1134 		headers->l3_key.tos = match->key->tos;
1135 		headers->l3_mask.tos = match->mask->tos;
1136 	}
1137 
1138 	if (match->mask->ttl) {
1139 		if (is_encap)
1140 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TTL;
1141 		else
1142 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TTL;
1143 
1144 		headers->l3_key.ttl = match->key->ttl;
1145 		headers->l3_mask.ttl = match->mask->ttl;
1146 	}
1147 }
1148 
1149 /**
1150  * ice_tc_set_port - Parse ports from TC flower filter
1151  * @match: Flow match structure
1152  * @fltr: Pointer to filter structure
1153  * @headers: inner or outer header fields
1154  * @is_encap: set true for tunnel port
1155  */
1156 static int
1157 ice_tc_set_port(struct flow_match_ports match,
1158 		struct ice_tc_flower_fltr *fltr,
1159 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
1160 {
1161 	if (match.key->dst) {
1162 		if (is_encap)
1163 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT;
1164 		else
1165 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_L4_PORT;
1166 
1167 		headers->l4_key.dst_port = match.key->dst;
1168 		headers->l4_mask.dst_port = match.mask->dst;
1169 	}
1170 	if (match.key->src) {
1171 		if (is_encap)
1172 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT;
1173 		else
1174 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_L4_PORT;
1175 
1176 		headers->l4_key.src_port = match.key->src;
1177 		headers->l4_mask.src_port = match.mask->src;
1178 	}
1179 	return 0;
1180 }
1181 
1182 static struct net_device *
1183 ice_get_tunnel_device(struct net_device *dev, struct flow_rule *rule)
1184 {
1185 	struct flow_action_entry *act;
1186 	int i;
1187 
1188 	if (ice_is_tunnel_supported(dev))
1189 		return dev;
1190 
1191 	flow_action_for_each(i, act, &rule->action) {
1192 		if (act->id == FLOW_ACTION_REDIRECT &&
1193 		    ice_is_tunnel_supported(act->dev))
1194 			return act->dev;
1195 	}
1196 
1197 	return NULL;
1198 }
1199 
1200 /**
1201  * ice_parse_gtp_type - Sets GTP tunnel type to GTP-U or GTP-C
1202  * @match: Flow match structure
1203  * @fltr: Pointer to filter structure
1204  *
1205  * GTP-C/GTP-U is selected based on destination port number (enc_dst_port).
1206  * Before calling this funtcion, fltr->tunnel_type should be set to TNL_GTPU,
1207  * therefore making GTP-U the default choice (when destination port number is
1208  * not specified).
1209  */
1210 static int
1211 ice_parse_gtp_type(struct flow_match_ports match,
1212 		   struct ice_tc_flower_fltr *fltr)
1213 {
1214 	u16 dst_port;
1215 
1216 	if (match.key->dst) {
1217 		dst_port = be16_to_cpu(match.key->dst);
1218 
1219 		switch (dst_port) {
1220 		case 2152:
1221 			break;
1222 		case 2123:
1223 			fltr->tunnel_type = TNL_GTPC;
1224 			break;
1225 		default:
1226 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported GTP port number");
1227 			return -EINVAL;
1228 		}
1229 	}
1230 
1231 	return 0;
1232 }
1233 
1234 static int
1235 ice_parse_tunnel_attr(struct net_device *dev, struct flow_rule *rule,
1236 		      struct ice_tc_flower_fltr *fltr)
1237 {
1238 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1239 	struct flow_match_control enc_control;
1240 
1241 	fltr->tunnel_type = ice_tc_tun_get_type(dev);
1242 	headers->l3_key.ip_proto = IPPROTO_UDP;
1243 
1244 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
1245 		struct flow_match_enc_keyid enc_keyid;
1246 
1247 		flow_rule_match_enc_keyid(rule, &enc_keyid);
1248 
1249 		if (!enc_keyid.mask->keyid ||
1250 		    enc_keyid.mask->keyid != cpu_to_be32(ICE_TC_FLOWER_MASK_32))
1251 			return -EINVAL;
1252 
1253 		fltr->flags |= ICE_TC_FLWR_FIELD_TENANT_ID;
1254 		fltr->tenant_id = enc_keyid.key->keyid;
1255 	}
1256 
1257 	flow_rule_match_enc_control(rule, &enc_control);
1258 
1259 	if (enc_control.key->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1260 		struct flow_match_ipv4_addrs match;
1261 
1262 		flow_rule_match_enc_ipv4_addrs(rule, &match);
1263 		if (ice_tc_set_ipv4(&match, fltr, headers, true))
1264 			return -EINVAL;
1265 	} else if (enc_control.key->addr_type ==
1266 					FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1267 		struct flow_match_ipv6_addrs match;
1268 
1269 		flow_rule_match_enc_ipv6_addrs(rule, &match);
1270 		if (ice_tc_set_ipv6(&match, fltr, headers, true))
1271 			return -EINVAL;
1272 	}
1273 
1274 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
1275 		struct flow_match_ip match;
1276 
1277 		flow_rule_match_enc_ip(rule, &match);
1278 		ice_tc_set_tos_ttl(&match, fltr, headers, true);
1279 	}
1280 
1281 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) &&
1282 	    fltr->tunnel_type != TNL_VXLAN && fltr->tunnel_type != TNL_GENEVE) {
1283 		struct flow_match_ports match;
1284 
1285 		flow_rule_match_enc_ports(rule, &match);
1286 
1287 		if (fltr->tunnel_type != TNL_GTPU) {
1288 			if (ice_tc_set_port(match, fltr, headers, true))
1289 				return -EINVAL;
1290 		} else {
1291 			if (ice_parse_gtp_type(match, fltr))
1292 				return -EINVAL;
1293 		}
1294 	}
1295 
1296 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
1297 		struct flow_match_enc_opts match;
1298 
1299 		flow_rule_match_enc_opts(rule, &match);
1300 
1301 		memcpy(&fltr->gtp_pdu_info_keys, &match.key->data[0],
1302 		       sizeof(struct gtp_pdu_session_info));
1303 
1304 		memcpy(&fltr->gtp_pdu_info_masks, &match.mask->data[0],
1305 		       sizeof(struct gtp_pdu_session_info));
1306 
1307 		fltr->flags |= ICE_TC_FLWR_FIELD_ENC_OPTS;
1308 	}
1309 
1310 	return 0;
1311 }
1312 
1313 /**
1314  * ice_parse_cls_flower - Parse TC flower filters provided by kernel
1315  * @vsi: Pointer to the VSI
1316  * @filter_dev: Pointer to device on which filter is being added
1317  * @f: Pointer to struct flow_cls_offload
1318  * @fltr: Pointer to filter structure
1319  */
1320 static int
1321 ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
1322 		     struct flow_cls_offload *f,
1323 		     struct ice_tc_flower_fltr *fltr)
1324 {
1325 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
1326 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1327 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
1328 	struct flow_dissector *dissector;
1329 	struct net_device *tunnel_dev;
1330 
1331 	dissector = rule->match.dissector;
1332 
1333 	if (dissector->used_keys &
1334 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1335 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
1336 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1337 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
1338 	      BIT(FLOW_DISSECTOR_KEY_CVLAN) |
1339 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1340 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1341 	      BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1342 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1343 	      BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1344 	      BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1345 	      BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1346 	      BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) |
1347 	      BIT(FLOW_DISSECTOR_KEY_IP) |
1348 	      BIT(FLOW_DISSECTOR_KEY_ENC_IP) |
1349 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
1350 	      BIT(FLOW_DISSECTOR_KEY_PPPOE) |
1351 	      BIT(FLOW_DISSECTOR_KEY_L2TPV3))) {
1352 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported key used");
1353 		return -EOPNOTSUPP;
1354 	}
1355 
1356 	tunnel_dev = ice_get_tunnel_device(filter_dev, rule);
1357 	if (tunnel_dev) {
1358 		int err;
1359 
1360 		filter_dev = tunnel_dev;
1361 
1362 		err = ice_parse_tunnel_attr(filter_dev, rule, fltr);
1363 		if (err) {
1364 			NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to parse TC flower tunnel attributes");
1365 			return err;
1366 		}
1367 
1368 		/* header pointers should point to the inner headers, outer
1369 		 * header were already set by ice_parse_tunnel_attr
1370 		 */
1371 		headers = &fltr->inner_headers;
1372 	} else if (dissector->used_keys &
1373 		  (BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1374 		   BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1375 		   BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1376 		   BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))) {
1377 		NL_SET_ERR_MSG_MOD(fltr->extack, "Tunnel key used, but device isn't a tunnel");
1378 		return -EOPNOTSUPP;
1379 	} else {
1380 		fltr->tunnel_type = TNL_LAST;
1381 	}
1382 
1383 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1384 		struct flow_match_basic match;
1385 
1386 		flow_rule_match_basic(rule, &match);
1387 
1388 		n_proto_key = ntohs(match.key->n_proto);
1389 		n_proto_mask = ntohs(match.mask->n_proto);
1390 
1391 		if (n_proto_key == ETH_P_ALL || n_proto_key == 0 ||
1392 		    fltr->tunnel_type == TNL_GTPU ||
1393 		    fltr->tunnel_type == TNL_GTPC) {
1394 			n_proto_key = 0;
1395 			n_proto_mask = 0;
1396 		} else {
1397 			fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1398 		}
1399 
1400 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1401 		headers->l2_mask.n_proto = cpu_to_be16(n_proto_mask);
1402 		headers->l3_key.ip_proto = match.key->ip_proto;
1403 	}
1404 
1405 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1406 		struct flow_match_eth_addrs match;
1407 
1408 		flow_rule_match_eth_addrs(rule, &match);
1409 
1410 		if (!is_zero_ether_addr(match.key->dst)) {
1411 			ether_addr_copy(headers->l2_key.dst_mac,
1412 					match.key->dst);
1413 			ether_addr_copy(headers->l2_mask.dst_mac,
1414 					match.mask->dst);
1415 			fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1416 		}
1417 
1418 		if (!is_zero_ether_addr(match.key->src)) {
1419 			ether_addr_copy(headers->l2_key.src_mac,
1420 					match.key->src);
1421 			ether_addr_copy(headers->l2_mask.src_mac,
1422 					match.mask->src);
1423 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_MAC;
1424 		}
1425 	}
1426 
1427 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
1428 	    is_vlan_dev(filter_dev)) {
1429 		struct flow_dissector_key_vlan mask;
1430 		struct flow_dissector_key_vlan key;
1431 		struct flow_match_vlan match;
1432 
1433 		if (is_vlan_dev(filter_dev)) {
1434 			match.key = &key;
1435 			match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
1436 			match.key->vlan_priority = 0;
1437 			match.mask = &mask;
1438 			memset(match.mask, 0xff, sizeof(*match.mask));
1439 			match.mask->vlan_priority = 0;
1440 		} else {
1441 			flow_rule_match_vlan(rule, &match);
1442 		}
1443 
1444 		if (match.mask->vlan_id) {
1445 			if (match.mask->vlan_id == VLAN_VID_MASK) {
1446 				fltr->flags |= ICE_TC_FLWR_FIELD_VLAN;
1447 				headers->vlan_hdr.vlan_id =
1448 					cpu_to_be16(match.key->vlan_id &
1449 						    VLAN_VID_MASK);
1450 			} else {
1451 				NL_SET_ERR_MSG_MOD(fltr->extack, "Bad VLAN mask");
1452 				return -EINVAL;
1453 			}
1454 		}
1455 
1456 		if (match.mask->vlan_priority) {
1457 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
1458 			headers->vlan_hdr.vlan_prio =
1459 				be16_encode_bits(match.key->vlan_priority,
1460 						 VLAN_PRIO_MASK);
1461 		}
1462 
1463 		if (match.mask->vlan_tpid)
1464 			headers->vlan_hdr.vlan_tpid = match.key->vlan_tpid;
1465 	}
1466 
1467 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
1468 		struct flow_match_vlan match;
1469 
1470 		if (!ice_is_dvm_ena(&vsi->back->hw)) {
1471 			NL_SET_ERR_MSG_MOD(fltr->extack, "Double VLAN mode is not enabled");
1472 			return -EINVAL;
1473 		}
1474 
1475 		flow_rule_match_cvlan(rule, &match);
1476 
1477 		if (match.mask->vlan_id) {
1478 			if (match.mask->vlan_id == VLAN_VID_MASK) {
1479 				fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN;
1480 				headers->cvlan_hdr.vlan_id =
1481 					cpu_to_be16(match.key->vlan_id &
1482 						    VLAN_VID_MASK);
1483 			} else {
1484 				NL_SET_ERR_MSG_MOD(fltr->extack,
1485 						   "Bad CVLAN mask");
1486 				return -EINVAL;
1487 			}
1488 		}
1489 
1490 		if (match.mask->vlan_priority) {
1491 			fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
1492 			headers->cvlan_hdr.vlan_prio =
1493 				be16_encode_bits(match.key->vlan_priority,
1494 						 VLAN_PRIO_MASK);
1495 		}
1496 	}
1497 
1498 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PPPOE)) {
1499 		struct flow_match_pppoe match;
1500 
1501 		flow_rule_match_pppoe(rule, &match);
1502 		n_proto_key = ice_tc_set_pppoe(&match, fltr, headers);
1503 
1504 		/* If ethertype equals ETH_P_PPP_SES, n_proto might be
1505 		 * overwritten by encapsulated protocol (ppp_proto field) or set
1506 		 * to 0. To correct this, flow_match_pppoe provides the type
1507 		 * field, which contains the actual ethertype (ETH_P_PPP_SES).
1508 		 */
1509 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1510 		headers->l2_mask.n_proto = cpu_to_be16(0xFFFF);
1511 		fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1512 	}
1513 
1514 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
1515 		struct flow_match_control match;
1516 
1517 		flow_rule_match_control(rule, &match);
1518 
1519 		addr_type = match.key->addr_type;
1520 	}
1521 
1522 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1523 		struct flow_match_ipv4_addrs match;
1524 
1525 		flow_rule_match_ipv4_addrs(rule, &match);
1526 		if (ice_tc_set_ipv4(&match, fltr, headers, false))
1527 			return -EINVAL;
1528 	}
1529 
1530 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1531 		struct flow_match_ipv6_addrs match;
1532 
1533 		flow_rule_match_ipv6_addrs(rule, &match);
1534 		if (ice_tc_set_ipv6(&match, fltr, headers, false))
1535 			return -EINVAL;
1536 	}
1537 
1538 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
1539 		struct flow_match_ip match;
1540 
1541 		flow_rule_match_ip(rule, &match);
1542 		ice_tc_set_tos_ttl(&match, fltr, headers, false);
1543 	}
1544 
1545 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_L2TPV3)) {
1546 		struct flow_match_l2tpv3 match;
1547 
1548 		flow_rule_match_l2tpv3(rule, &match);
1549 
1550 		fltr->flags |= ICE_TC_FLWR_FIELD_L2TPV3_SESSID;
1551 		headers->l2tpv3_hdr.session_id = match.key->session_id;
1552 	}
1553 
1554 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
1555 		struct flow_match_ports match;
1556 
1557 		flow_rule_match_ports(rule, &match);
1558 		if (ice_tc_set_port(match, fltr, headers, false))
1559 			return -EINVAL;
1560 		switch (headers->l3_key.ip_proto) {
1561 		case IPPROTO_TCP:
1562 		case IPPROTO_UDP:
1563 			break;
1564 		default:
1565 			NL_SET_ERR_MSG_MOD(fltr->extack, "Only UDP and TCP transport are supported");
1566 			return -EINVAL;
1567 		}
1568 	}
1569 	return 0;
1570 }
1571 
1572 /**
1573  * ice_add_switch_fltr - Add TC flower filters
1574  * @vsi: Pointer to VSI
1575  * @fltr: Pointer to struct ice_tc_flower_fltr
1576  *
1577  * Add filter in HW switch block
1578  */
1579 static int
1580 ice_add_switch_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1581 {
1582 	if (fltr->action.fltr_act == ICE_FWD_TO_QGRP)
1583 		return -EOPNOTSUPP;
1584 
1585 	if (ice_is_eswitch_mode_switchdev(vsi->back))
1586 		return ice_eswitch_add_tc_fltr(vsi, fltr);
1587 
1588 	return ice_add_tc_flower_adv_fltr(vsi, fltr);
1589 }
1590 
1591 /**
1592  * ice_prep_adq_filter - Prepare ADQ filter with the required additional headers
1593  * @vsi: Pointer to VSI
1594  * @fltr: Pointer to TC flower filter structure
1595  *
1596  * Prepare ADQ filter with the required additional header fields
1597  */
1598 static int
1599 ice_prep_adq_filter(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1600 {
1601 	if ((fltr->flags & ICE_TC_FLWR_FIELD_TENANT_ID) &&
1602 	    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1603 			   ICE_TC_FLWR_FIELD_SRC_MAC))) {
1604 		NL_SET_ERR_MSG_MOD(fltr->extack,
1605 				   "Unable to add filter because filter using tunnel key and inner MAC is unsupported combination");
1606 		return -EOPNOTSUPP;
1607 	}
1608 
1609 	/* For ADQ, filter must include dest MAC address, otherwise unwanted
1610 	 * packets with unrelated MAC address get delivered to ADQ VSIs as long
1611 	 * as remaining filter criteria is satisfied such as dest IP address
1612 	 * and dest/src L4 port. Below code handles the following cases:
1613 	 * 1. For non-tunnel, if user specify MAC addresses, use them.
1614 	 * 2. For non-tunnel, if user didn't specify MAC address, add implicit
1615 	 * dest MAC to be lower netdev's active unicast MAC address
1616 	 * 3. For tunnel,  as of now TC-filter through flower classifier doesn't
1617 	 * have provision for user to specify outer DMAC, hence driver to
1618 	 * implicitly add outer dest MAC to be lower netdev's active unicast
1619 	 * MAC address.
1620 	 */
1621 	if (fltr->tunnel_type != TNL_LAST &&
1622 	    !(fltr->flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC))
1623 		fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DST_MAC;
1624 
1625 	if (fltr->tunnel_type == TNL_LAST &&
1626 	    !(fltr->flags & ICE_TC_FLWR_FIELD_DST_MAC))
1627 		fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
1628 
1629 	if (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1630 			   ICE_TC_FLWR_FIELD_ENC_DST_MAC)) {
1631 		ether_addr_copy(fltr->outer_headers.l2_key.dst_mac,
1632 				vsi->netdev->dev_addr);
1633 		eth_broadcast_addr(fltr->outer_headers.l2_mask.dst_mac);
1634 	}
1635 
1636 	/* Make sure VLAN is already added to main VSI, before allowing ADQ to
1637 	 * add a VLAN based filter such as MAC + VLAN + L4 port.
1638 	 */
1639 	if (fltr->flags & ICE_TC_FLWR_FIELD_VLAN) {
1640 		u16 vlan_id = be16_to_cpu(fltr->outer_headers.vlan_hdr.vlan_id);
1641 
1642 		if (!ice_vlan_fltr_exist(&vsi->back->hw, vlan_id, vsi->idx)) {
1643 			NL_SET_ERR_MSG_MOD(fltr->extack,
1644 					   "Unable to add filter because legacy VLAN filter for specified destination doesn't exist");
1645 			return -EINVAL;
1646 		}
1647 	}
1648 	return 0;
1649 }
1650 
1651 /**
1652  * ice_handle_tclass_action - Support directing to a traffic class
1653  * @vsi: Pointer to VSI
1654  * @cls_flower: Pointer to TC flower offload structure
1655  * @fltr: Pointer to TC flower filter structure
1656  *
1657  * Support directing traffic to a traffic class/queue-set
1658  */
1659 static int
1660 ice_handle_tclass_action(struct ice_vsi *vsi,
1661 			 struct flow_cls_offload *cls_flower,
1662 			 struct ice_tc_flower_fltr *fltr)
1663 {
1664 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
1665 
1666 	/* user specified hw_tc (must be non-zero for ADQ TC), action is forward
1667 	 * to hw_tc (i.e. ADQ channel number)
1668 	 */
1669 	if (tc < ICE_CHNL_START_TC) {
1670 		NL_SET_ERR_MSG_MOD(fltr->extack,
1671 				   "Unable to add filter because of unsupported destination");
1672 		return -EOPNOTSUPP;
1673 	}
1674 	if (!(vsi->all_enatc & BIT(tc))) {
1675 		NL_SET_ERR_MSG_MOD(fltr->extack,
1676 				   "Unable to add filter because of non-existence destination");
1677 		return -EINVAL;
1678 	}
1679 	fltr->action.fltr_act = ICE_FWD_TO_VSI;
1680 	fltr->action.fwd.tc.tc_class = tc;
1681 
1682 	return ice_prep_adq_filter(vsi, fltr);
1683 }
1684 
1685 static int
1686 ice_tc_forward_to_queue(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1687 			struct flow_action_entry *act)
1688 {
1689 	struct ice_vsi *ch_vsi = NULL;
1690 	u16 queue = act->rx_queue;
1691 
1692 	if (queue >= vsi->num_rxq) {
1693 		NL_SET_ERR_MSG_MOD(fltr->extack,
1694 				   "Unable to add filter because specified queue is invalid");
1695 		return -EINVAL;
1696 	}
1697 	fltr->action.fltr_act = ICE_FWD_TO_Q;
1698 	fltr->action.fwd.q.queue = queue;
1699 	/* determine corresponding HW queue */
1700 	fltr->action.fwd.q.hw_queue = vsi->rxq_map[queue];
1701 
1702 	/* If ADQ is configured, and the queue belongs to ADQ VSI, then prepare
1703 	 * ADQ switch filter
1704 	 */
1705 	ch_vsi = ice_locate_vsi_using_queue(vsi, fltr);
1706 	if (!ch_vsi)
1707 		return -EINVAL;
1708 	fltr->dest_vsi = ch_vsi;
1709 	if (!ice_is_chnl_fltr(fltr))
1710 		return 0;
1711 
1712 	return ice_prep_adq_filter(vsi, fltr);
1713 }
1714 
1715 static int
1716 ice_tc_parse_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1717 		    struct flow_action_entry *act)
1718 {
1719 	switch (act->id) {
1720 	case FLOW_ACTION_RX_QUEUE_MAPPING:
1721 		/* forward to queue */
1722 		return ice_tc_forward_to_queue(vsi, fltr, act);
1723 	case FLOW_ACTION_DROP:
1724 		fltr->action.fltr_act = ICE_DROP_PACKET;
1725 		return 0;
1726 	default:
1727 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported TC action");
1728 		return -EOPNOTSUPP;
1729 	}
1730 }
1731 
1732 /**
1733  * ice_parse_tc_flower_actions - Parse the actions for a TC filter
1734  * @vsi: Pointer to VSI
1735  * @cls_flower: Pointer to TC flower offload structure
1736  * @fltr: Pointer to TC flower filter structure
1737  *
1738  * Parse the actions for a TC filter
1739  */
1740 static int
1741 ice_parse_tc_flower_actions(struct ice_vsi *vsi,
1742 			    struct flow_cls_offload *cls_flower,
1743 			    struct ice_tc_flower_fltr *fltr)
1744 {
1745 	struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
1746 	struct flow_action *flow_action = &rule->action;
1747 	struct flow_action_entry *act;
1748 	int i, err;
1749 
1750 	if (cls_flower->classid)
1751 		return ice_handle_tclass_action(vsi, cls_flower, fltr);
1752 
1753 	if (!flow_action_has_entries(flow_action))
1754 		return -EINVAL;
1755 
1756 	flow_action_for_each(i, act, flow_action) {
1757 		if (ice_is_eswitch_mode_switchdev(vsi->back))
1758 			err = ice_eswitch_tc_parse_action(fltr, act);
1759 		else
1760 			err = ice_tc_parse_action(vsi, fltr, act);
1761 		if (err)
1762 			return err;
1763 		continue;
1764 	}
1765 	return 0;
1766 }
1767 
1768 /**
1769  * ice_del_tc_fltr - deletes a filter from HW table
1770  * @vsi: Pointer to VSI
1771  * @fltr: Pointer to struct ice_tc_flower_fltr
1772  *
1773  * This function deletes a filter from HW table and manages book-keeping
1774  */
1775 static int ice_del_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
1776 {
1777 	struct ice_rule_query_data rule_rem;
1778 	struct ice_pf *pf = vsi->back;
1779 	int err;
1780 
1781 	rule_rem.rid = fltr->rid;
1782 	rule_rem.rule_id = fltr->rule_id;
1783 	rule_rem.vsi_handle = fltr->dest_vsi_handle;
1784 	err = ice_rem_adv_rule_by_id(&pf->hw, &rule_rem);
1785 	if (err) {
1786 		if (err == -ENOENT) {
1787 			NL_SET_ERR_MSG_MOD(fltr->extack, "Filter does not exist");
1788 			return -ENOENT;
1789 		}
1790 		NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to delete TC flower filter");
1791 		return -EIO;
1792 	}
1793 
1794 	/* update advanced switch filter count for destination
1795 	 * VSI if filter destination was VSI
1796 	 */
1797 	if (fltr->dest_vsi) {
1798 		if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
1799 			fltr->dest_vsi->num_chnl_fltr--;
1800 
1801 			/* keeps track of channel filters for PF VSI */
1802 			if (vsi->type == ICE_VSI_PF &&
1803 			    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
1804 					    ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
1805 				pf->num_dmac_chnl_fltrs--;
1806 		}
1807 	}
1808 	return 0;
1809 }
1810 
1811 /**
1812  * ice_add_tc_fltr - adds a TC flower filter
1813  * @netdev: Pointer to netdev
1814  * @vsi: Pointer to VSI
1815  * @f: Pointer to flower offload structure
1816  * @__fltr: Pointer to struct ice_tc_flower_fltr
1817  *
1818  * This function parses TC-flower input fields, parses action,
1819  * and adds a filter.
1820  */
1821 static int
1822 ice_add_tc_fltr(struct net_device *netdev, struct ice_vsi *vsi,
1823 		struct flow_cls_offload *f,
1824 		struct ice_tc_flower_fltr **__fltr)
1825 {
1826 	struct ice_tc_flower_fltr *fltr;
1827 	int err;
1828 
1829 	/* by default, set output to be INVALID */
1830 	*__fltr = NULL;
1831 
1832 	fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1833 	if (!fltr)
1834 		return -ENOMEM;
1835 
1836 	fltr->cookie = f->cookie;
1837 	fltr->extack = f->common.extack;
1838 	fltr->src_vsi = vsi;
1839 	INIT_HLIST_NODE(&fltr->tc_flower_node);
1840 
1841 	err = ice_parse_cls_flower(netdev, vsi, f, fltr);
1842 	if (err < 0)
1843 		goto err;
1844 
1845 	err = ice_parse_tc_flower_actions(vsi, f, fltr);
1846 	if (err < 0)
1847 		goto err;
1848 
1849 	err = ice_add_switch_fltr(vsi, fltr);
1850 	if (err < 0)
1851 		goto err;
1852 
1853 	/* return the newly created filter */
1854 	*__fltr = fltr;
1855 
1856 	return 0;
1857 err:
1858 	kfree(fltr);
1859 	return err;
1860 }
1861 
1862 /**
1863  * ice_find_tc_flower_fltr - Find the TC flower filter in the list
1864  * @pf: Pointer to PF
1865  * @cookie: filter specific cookie
1866  */
1867 static struct ice_tc_flower_fltr *
1868 ice_find_tc_flower_fltr(struct ice_pf *pf, unsigned long cookie)
1869 {
1870 	struct ice_tc_flower_fltr *fltr;
1871 
1872 	hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
1873 		if (cookie == fltr->cookie)
1874 			return fltr;
1875 
1876 	return NULL;
1877 }
1878 
1879 /**
1880  * ice_add_cls_flower - add TC flower filters
1881  * @netdev: Pointer to filter device
1882  * @vsi: Pointer to VSI
1883  * @cls_flower: Pointer to flower offload structure
1884  */
1885 int
1886 ice_add_cls_flower(struct net_device *netdev, struct ice_vsi *vsi,
1887 		   struct flow_cls_offload *cls_flower)
1888 {
1889 	struct netlink_ext_ack *extack = cls_flower->common.extack;
1890 	struct net_device *vsi_netdev = vsi->netdev;
1891 	struct ice_tc_flower_fltr *fltr;
1892 	struct ice_pf *pf = vsi->back;
1893 	int err;
1894 
1895 	if (ice_is_reset_in_progress(pf->state))
1896 		return -EBUSY;
1897 	if (test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
1898 		return -EINVAL;
1899 
1900 	if (ice_is_port_repr_netdev(netdev))
1901 		vsi_netdev = netdev;
1902 
1903 	if (!(vsi_netdev->features & NETIF_F_HW_TC) &&
1904 	    !test_bit(ICE_FLAG_CLS_FLOWER, pf->flags)) {
1905 		/* Based on TC indirect notifications from kernel, all ice
1906 		 * devices get an instance of rule from higher level device.
1907 		 * Avoid triggering explicit error in this case.
1908 		 */
1909 		if (netdev == vsi_netdev)
1910 			NL_SET_ERR_MSG_MOD(extack, "can't apply TC flower filters, turn ON hw-tc-offload and try again");
1911 		return -EINVAL;
1912 	}
1913 
1914 	/* avoid duplicate entries, if exists - return error */
1915 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1916 	if (fltr) {
1917 		NL_SET_ERR_MSG_MOD(extack, "filter cookie already exists, ignoring");
1918 		return -EEXIST;
1919 	}
1920 
1921 	/* prep and add TC-flower filter in HW */
1922 	err = ice_add_tc_fltr(netdev, vsi, cls_flower, &fltr);
1923 	if (err)
1924 		return err;
1925 
1926 	/* add filter into an ordered list */
1927 	hlist_add_head(&fltr->tc_flower_node, &pf->tc_flower_fltr_list);
1928 	return 0;
1929 }
1930 
1931 /**
1932  * ice_del_cls_flower - delete TC flower filters
1933  * @vsi: Pointer to VSI
1934  * @cls_flower: Pointer to struct flow_cls_offload
1935  */
1936 int
1937 ice_del_cls_flower(struct ice_vsi *vsi, struct flow_cls_offload *cls_flower)
1938 {
1939 	struct ice_tc_flower_fltr *fltr;
1940 	struct ice_pf *pf = vsi->back;
1941 	int err;
1942 
1943 	/* find filter */
1944 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
1945 	if (!fltr) {
1946 		if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) &&
1947 		    hlist_empty(&pf->tc_flower_fltr_list))
1948 			return 0;
1949 
1950 		NL_SET_ERR_MSG_MOD(cls_flower->common.extack, "failed to delete TC flower filter because unable to find it");
1951 		return -EINVAL;
1952 	}
1953 
1954 	fltr->extack = cls_flower->common.extack;
1955 	/* delete filter from HW */
1956 	err = ice_del_tc_fltr(vsi, fltr);
1957 	if (err)
1958 		return err;
1959 
1960 	/* delete filter from an ordered list */
1961 	hlist_del(&fltr->tc_flower_node);
1962 
1963 	/* free the filter node */
1964 	kfree(fltr);
1965 
1966 	return 0;
1967 }
1968 
1969 /**
1970  * ice_replay_tc_fltrs - replay TC filters
1971  * @pf: pointer to PF struct
1972  */
1973 void ice_replay_tc_fltrs(struct ice_pf *pf)
1974 {
1975 	struct ice_tc_flower_fltr *fltr;
1976 	struct hlist_node *node;
1977 
1978 	hlist_for_each_entry_safe(fltr, node,
1979 				  &pf->tc_flower_fltr_list,
1980 				  tc_flower_node) {
1981 		fltr->extack = NULL;
1982 		ice_add_switch_fltr(fltr->src_vsi, fltr);
1983 	}
1984 }
1985