xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_tc_lib.c (revision 7b7fd0ac7dc1ffcaf24d9bca0f051b0168e43cd4)
10d08a441SKiran Patil // SPDX-License-Identifier: GPL-2.0
20d08a441SKiran Patil /* Copyright (C) 2019-2021, Intel Corporation. */
30d08a441SKiran Patil 
40d08a441SKiran Patil #include "ice.h"
50d08a441SKiran Patil #include "ice_tc_lib.h"
60d08a441SKiran Patil #include "ice_fltr.h"
79e300987SMichal Swiatkowski #include "ice_lib.h"
89e300987SMichal Swiatkowski #include "ice_protocol_type.h"
90d08a441SKiran Patil 
100960a27bSMarcin Szycik #define ICE_TC_METADATA_LKUP_IDX 0
110960a27bSMarcin Szycik 
120d08a441SKiran Patil /**
130d08a441SKiran Patil  * ice_tc_count_lkups - determine lookup count for switch filter
140d08a441SKiran Patil  * @flags: TC-flower flags
150d08a441SKiran Patil  * @headers: Pointer to TC flower filter header structure
160d08a441SKiran Patil  * @fltr: Pointer to outer TC filter structure
170d08a441SKiran Patil  *
180d08a441SKiran Patil  * Determine lookup count based on TC flower input for switch filter.
190d08a441SKiran Patil  */
200d08a441SKiran Patil static int
ice_tc_count_lkups(u32 flags,struct ice_tc_flower_lyr_2_4_hdrs * headers,struct ice_tc_flower_fltr * fltr)210d08a441SKiran Patil ice_tc_count_lkups(u32 flags, struct ice_tc_flower_lyr_2_4_hdrs *headers,
220d08a441SKiran Patil 		   struct ice_tc_flower_fltr *fltr)
230d08a441SKiran Patil {
240960a27bSMarcin Szycik 	int lkups_cnt = 1; /* 0th lookup is metadata */
250960a27bSMarcin Szycik 
260960a27bSMarcin Szycik 	/* Always add metadata as the 0th lookup. Included elements:
270960a27bSMarcin Szycik 	 * - Direction flag (always present)
280960a27bSMarcin Szycik 	 * - ICE_TC_FLWR_FIELD_VLAN_TPID (present if specified)
290960a27bSMarcin Szycik 	 * - Tunnel flag (present if tunnel)
300960a27bSMarcin Szycik 	 */
3191efeadfSMichal Swiatkowski 	if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS)
3291efeadfSMichal Swiatkowski 		lkups_cnt++;
330d08a441SKiran Patil 
349e300987SMichal Swiatkowski 	if (flags & ICE_TC_FLWR_FIELD_TENANT_ID)
359e300987SMichal Swiatkowski 		lkups_cnt++;
369e300987SMichal Swiatkowski 
3702ddec19SAmritha Nambiar 	if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC)
3802ddec19SAmritha Nambiar 		lkups_cnt++;
3902ddec19SAmritha Nambiar 
409a225f81SMarcin Szycik 	if (flags & ICE_TC_FLWR_FIELD_ENC_OPTS)
419a225f81SMarcin Szycik 		lkups_cnt++;
429a225f81SMarcin Szycik 
439e300987SMichal Swiatkowski 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
449e300987SMichal Swiatkowski 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
459e300987SMichal Swiatkowski 		     ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
469e300987SMichal Swiatkowski 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV6))
479e300987SMichal Swiatkowski 		lkups_cnt++;
489e300987SMichal Swiatkowski 
494c99bc96SMarcin Szycik 	if (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
504c99bc96SMarcin Szycik 		     ICE_TC_FLWR_FIELD_ENC_IP_TTL))
514c99bc96SMarcin Szycik 		lkups_cnt++;
524c99bc96SMarcin Szycik 
539e300987SMichal Swiatkowski 	if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT)
549e300987SMichal Swiatkowski 		lkups_cnt++;
559e300987SMichal Swiatkowski 
5634a89775SMartyna Szapar-Mudlaw 	if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID)
570d08a441SKiran Patil 		lkups_cnt++;
580d08a441SKiran Patil 
590d08a441SKiran Patil 	/* are MAC fields specified? */
600d08a441SKiran Patil 	if (flags & (ICE_TC_FLWR_FIELD_DST_MAC | ICE_TC_FLWR_FIELD_SRC_MAC))
610d08a441SKiran Patil 		lkups_cnt++;
620d08a441SKiran Patil 
630d08a441SKiran Patil 	/* is VLAN specified? */
6434800178SMartyna Szapar-Mudlaw 	if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO))
650d08a441SKiran Patil 		lkups_cnt++;
660d08a441SKiran Patil 
6706bca7c2SMartyna Szapar-Mudlaw 	/* is CVLAN specified? */
6834800178SMartyna Szapar-Mudlaw 	if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO))
6906bca7c2SMartyna Szapar-Mudlaw 		lkups_cnt++;
7006bca7c2SMartyna Szapar-Mudlaw 
71cd8efeeeSMarcin Szycik 	/* are PPPoE options specified? */
72cd8efeeeSMarcin Szycik 	if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
73cd8efeeeSMarcin Szycik 		     ICE_TC_FLWR_FIELD_PPP_PROTO))
74cd8efeeeSMarcin Szycik 		lkups_cnt++;
75cd8efeeeSMarcin Szycik 
760d08a441SKiran Patil 	/* are IPv[4|6] fields specified? */
779e300987SMichal Swiatkowski 	if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 | ICE_TC_FLWR_FIELD_SRC_IPV4 |
789e300987SMichal Swiatkowski 		     ICE_TC_FLWR_FIELD_DEST_IPV6 | ICE_TC_FLWR_FIELD_SRC_IPV6))
790d08a441SKiran Patil 		lkups_cnt++;
800d08a441SKiran Patil 
814c99bc96SMarcin Szycik 	if (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))
824c99bc96SMarcin Szycik 		lkups_cnt++;
834c99bc96SMarcin Szycik 
84cd634549SMarcin Szycik 	/* are L2TPv3 options specified? */
85cd634549SMarcin Szycik 	if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID)
86cd634549SMarcin Szycik 		lkups_cnt++;
87cd634549SMarcin Szycik 
880d08a441SKiran Patil 	/* is L4 (TCP/UDP/any other L4 protocol fields) specified? */
890d08a441SKiran Patil 	if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
900d08a441SKiran Patil 		     ICE_TC_FLWR_FIELD_SRC_L4_PORT))
910d08a441SKiran Patil 		lkups_cnt++;
920d08a441SKiran Patil 
930d08a441SKiran Patil 	return lkups_cnt;
940d08a441SKiran Patil }
950d08a441SKiran Patil 
ice_proto_type_from_mac(bool inner)969e300987SMichal Swiatkowski static enum ice_protocol_type ice_proto_type_from_mac(bool inner)
979e300987SMichal Swiatkowski {
989e300987SMichal Swiatkowski 	return inner ? ICE_MAC_IL : ICE_MAC_OFOS;
999e300987SMichal Swiatkowski }
1009e300987SMichal Swiatkowski 
ice_proto_type_from_etype(bool inner)10134a89775SMartyna Szapar-Mudlaw static enum ice_protocol_type ice_proto_type_from_etype(bool inner)
10234a89775SMartyna Szapar-Mudlaw {
10334a89775SMartyna Szapar-Mudlaw 	return inner ? ICE_ETYPE_IL : ICE_ETYPE_OL;
10434a89775SMartyna Szapar-Mudlaw }
10534a89775SMartyna Szapar-Mudlaw 
ice_proto_type_from_ipv4(bool inner)1069e300987SMichal Swiatkowski static enum ice_protocol_type ice_proto_type_from_ipv4(bool inner)
1079e300987SMichal Swiatkowski {
1089e300987SMichal Swiatkowski 	return inner ? ICE_IPV4_IL : ICE_IPV4_OFOS;
1099e300987SMichal Swiatkowski }
1109e300987SMichal Swiatkowski 
ice_proto_type_from_ipv6(bool inner)1119e300987SMichal Swiatkowski static enum ice_protocol_type ice_proto_type_from_ipv6(bool inner)
1129e300987SMichal Swiatkowski {
1139e300987SMichal Swiatkowski 	return inner ? ICE_IPV6_IL : ICE_IPV6_OFOS;
1149e300987SMichal Swiatkowski }
1159e300987SMichal Swiatkowski 
ice_proto_type_from_l4_port(u16 ip_proto)1160e32ff02SMichal Swiatkowski static enum ice_protocol_type ice_proto_type_from_l4_port(u16 ip_proto)
1179e300987SMichal Swiatkowski {
1189e300987SMichal Swiatkowski 	switch (ip_proto) {
1199e300987SMichal Swiatkowski 	case IPPROTO_TCP:
1209e300987SMichal Swiatkowski 		return ICE_TCP_IL;
1219e300987SMichal Swiatkowski 	case IPPROTO_UDP:
1220e32ff02SMichal Swiatkowski 		return ICE_UDP_ILOS;
1239e300987SMichal Swiatkowski 	}
1249e300987SMichal Swiatkowski 
1259e300987SMichal Swiatkowski 	return 0;
1269e300987SMichal Swiatkowski }
1279e300987SMichal Swiatkowski 
1289e300987SMichal Swiatkowski static enum ice_protocol_type
ice_proto_type_from_tunnel(enum ice_tunnel_type type)1299e300987SMichal Swiatkowski ice_proto_type_from_tunnel(enum ice_tunnel_type type)
1309e300987SMichal Swiatkowski {
1319e300987SMichal Swiatkowski 	switch (type) {
1329e300987SMichal Swiatkowski 	case TNL_VXLAN:
1339e300987SMichal Swiatkowski 		return ICE_VXLAN;
1349e300987SMichal Swiatkowski 	case TNL_GENEVE:
1359e300987SMichal Swiatkowski 		return ICE_GENEVE;
136f0a35040SMichal Swiatkowski 	case TNL_GRETAP:
137f0a35040SMichal Swiatkowski 		return ICE_NVGRE;
1389a225f81SMarcin Szycik 	case TNL_GTPU:
1399a225f81SMarcin Szycik 		/* NO_PAY profiles will not work with GTP-U */
1409a225f81SMarcin Szycik 		return ICE_GTP;
1419a225f81SMarcin Szycik 	case TNL_GTPC:
1429a225f81SMarcin Szycik 		return ICE_GTP_NO_PAY;
1439e300987SMichal Swiatkowski 	default:
1449e300987SMichal Swiatkowski 		return 0;
1459e300987SMichal Swiatkowski 	}
1469e300987SMichal Swiatkowski }
1479e300987SMichal Swiatkowski 
1489e300987SMichal Swiatkowski static enum ice_sw_tunnel_type
ice_sw_type_from_tunnel(enum ice_tunnel_type type)1499e300987SMichal Swiatkowski ice_sw_type_from_tunnel(enum ice_tunnel_type type)
1509e300987SMichal Swiatkowski {
1519e300987SMichal Swiatkowski 	switch (type) {
1529e300987SMichal Swiatkowski 	case TNL_VXLAN:
1539e300987SMichal Swiatkowski 		return ICE_SW_TUN_VXLAN;
1549e300987SMichal Swiatkowski 	case TNL_GENEVE:
1559e300987SMichal Swiatkowski 		return ICE_SW_TUN_GENEVE;
156f0a35040SMichal Swiatkowski 	case TNL_GRETAP:
157f0a35040SMichal Swiatkowski 		return ICE_SW_TUN_NVGRE;
1589a225f81SMarcin Szycik 	case TNL_GTPU:
1599a225f81SMarcin Szycik 		return ICE_SW_TUN_GTPU;
1609a225f81SMarcin Szycik 	case TNL_GTPC:
1619a225f81SMarcin Szycik 		return ICE_SW_TUN_GTPC;
1629e300987SMichal Swiatkowski 	default:
1639e300987SMichal Swiatkowski 		return ICE_NON_TUN;
1649e300987SMichal Swiatkowski 	}
1659e300987SMichal Swiatkowski }
1669e300987SMichal Swiatkowski 
ice_check_supported_vlan_tpid(u16 vlan_tpid)167ea71b967SMartyna Szapar-Mudlaw static u16 ice_check_supported_vlan_tpid(u16 vlan_tpid)
168ea71b967SMartyna Szapar-Mudlaw {
169ea71b967SMartyna Szapar-Mudlaw 	switch (vlan_tpid) {
170ea71b967SMartyna Szapar-Mudlaw 	case ETH_P_8021Q:
171ea71b967SMartyna Szapar-Mudlaw 	case ETH_P_8021AD:
172ea71b967SMartyna Szapar-Mudlaw 	case ETH_P_QINQ1:
173ea71b967SMartyna Szapar-Mudlaw 		return vlan_tpid;
174ea71b967SMartyna Szapar-Mudlaw 	default:
175ea71b967SMartyna Szapar-Mudlaw 		return 0;
176ea71b967SMartyna Szapar-Mudlaw 	}
177ea71b967SMartyna Szapar-Mudlaw }
178ea71b967SMartyna Szapar-Mudlaw 
1799e300987SMichal Swiatkowski static int
ice_tc_fill_tunnel_outer(u32 flags,struct ice_tc_flower_fltr * fltr,struct ice_adv_lkup_elem * list,int i)1809e300987SMichal Swiatkowski ice_tc_fill_tunnel_outer(u32 flags, struct ice_tc_flower_fltr *fltr,
1810960a27bSMarcin Szycik 			 struct ice_adv_lkup_elem *list, int i)
1829e300987SMichal Swiatkowski {
1839e300987SMichal Swiatkowski 	struct ice_tc_flower_lyr_2_4_hdrs *hdr = &fltr->outer_headers;
1849e300987SMichal Swiatkowski 
1859e300987SMichal Swiatkowski 	if (flags & ICE_TC_FLWR_FIELD_TENANT_ID) {
1869e300987SMichal Swiatkowski 		u32 tenant_id;
1879e300987SMichal Swiatkowski 
1889e300987SMichal Swiatkowski 		list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
189f0a35040SMichal Swiatkowski 		switch (fltr->tunnel_type) {
190f0a35040SMichal Swiatkowski 		case TNL_VXLAN:
191f0a35040SMichal Swiatkowski 		case TNL_GENEVE:
1929e300987SMichal Swiatkowski 			tenant_id = be32_to_cpu(fltr->tenant_id) << 8;
1939e300987SMichal Swiatkowski 			list[i].h_u.tnl_hdr.vni = cpu_to_be32(tenant_id);
1949e300987SMichal Swiatkowski 			memcpy(&list[i].m_u.tnl_hdr.vni, "\xff\xff\xff\x00", 4);
1959e300987SMichal Swiatkowski 			i++;
196f0a35040SMichal Swiatkowski 			break;
197f0a35040SMichal Swiatkowski 		case TNL_GRETAP:
198f0a35040SMichal Swiatkowski 			list[i].h_u.nvgre_hdr.tni_flow = fltr->tenant_id;
1999a225f81SMarcin Szycik 			memcpy(&list[i].m_u.nvgre_hdr.tni_flow,
2009a225f81SMarcin Szycik 			       "\xff\xff\xff\xff", 4);
2019a225f81SMarcin Szycik 			i++;
2029a225f81SMarcin Szycik 			break;
2039a225f81SMarcin Szycik 		case TNL_GTPC:
2049a225f81SMarcin Szycik 		case TNL_GTPU:
2059a225f81SMarcin Szycik 			list[i].h_u.gtp_hdr.teid = fltr->tenant_id;
2069a225f81SMarcin Szycik 			memcpy(&list[i].m_u.gtp_hdr.teid,
2079a225f81SMarcin Szycik 			       "\xff\xff\xff\xff", 4);
208f0a35040SMichal Swiatkowski 			i++;
209f0a35040SMichal Swiatkowski 			break;
210f0a35040SMichal Swiatkowski 		default:
211f0a35040SMichal Swiatkowski 			break;
212f0a35040SMichal Swiatkowski 		}
2139e300987SMichal Swiatkowski 	}
2149e300987SMichal Swiatkowski 
21502ddec19SAmritha Nambiar 	if (flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC) {
21602ddec19SAmritha Nambiar 		list[i].type = ice_proto_type_from_mac(false);
21702ddec19SAmritha Nambiar 		ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
21802ddec19SAmritha Nambiar 				hdr->l2_key.dst_mac);
21902ddec19SAmritha Nambiar 		ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
22002ddec19SAmritha Nambiar 				hdr->l2_mask.dst_mac);
22102ddec19SAmritha Nambiar 		i++;
22202ddec19SAmritha Nambiar 	}
22302ddec19SAmritha Nambiar 
2249a225f81SMarcin Szycik 	if (flags & ICE_TC_FLWR_FIELD_ENC_OPTS &&
2259a225f81SMarcin Szycik 	    (fltr->tunnel_type == TNL_GTPU || fltr->tunnel_type == TNL_GTPC)) {
2269a225f81SMarcin Szycik 		list[i].type = ice_proto_type_from_tunnel(fltr->tunnel_type);
2279a225f81SMarcin Szycik 
2289a225f81SMarcin Szycik 		if (fltr->gtp_pdu_info_masks.pdu_type) {
2299a225f81SMarcin Szycik 			list[i].h_u.gtp_hdr.pdu_type =
2309a225f81SMarcin Szycik 				fltr->gtp_pdu_info_keys.pdu_type << 4;
2319a225f81SMarcin Szycik 			memcpy(&list[i].m_u.gtp_hdr.pdu_type, "\xf0", 1);
2329a225f81SMarcin Szycik 		}
2339a225f81SMarcin Szycik 
2349a225f81SMarcin Szycik 		if (fltr->gtp_pdu_info_masks.qfi) {
2359a225f81SMarcin Szycik 			list[i].h_u.gtp_hdr.qfi = fltr->gtp_pdu_info_keys.qfi;
2369a225f81SMarcin Szycik 			memcpy(&list[i].m_u.gtp_hdr.qfi, "\x3f", 1);
2379a225f81SMarcin Szycik 		}
2389a225f81SMarcin Szycik 
2399a225f81SMarcin Szycik 		i++;
2409a225f81SMarcin Szycik 	}
2419a225f81SMarcin Szycik 
2429e300987SMichal Swiatkowski 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
2439e300987SMichal Swiatkowski 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV4)) {
2449e300987SMichal Swiatkowski 		list[i].type = ice_proto_type_from_ipv4(false);
2459e300987SMichal Swiatkowski 
2469e300987SMichal Swiatkowski 		if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV4) {
2479e300987SMichal Swiatkowski 			list[i].h_u.ipv4_hdr.src_addr = hdr->l3_key.src_ipv4;
2489e300987SMichal Swiatkowski 			list[i].m_u.ipv4_hdr.src_addr = hdr->l3_mask.src_ipv4;
2499e300987SMichal Swiatkowski 		}
2509e300987SMichal Swiatkowski 		if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV4) {
2519e300987SMichal Swiatkowski 			list[i].h_u.ipv4_hdr.dst_addr = hdr->l3_key.dst_ipv4;
2529e300987SMichal Swiatkowski 			list[i].m_u.ipv4_hdr.dst_addr = hdr->l3_mask.dst_ipv4;
2539e300987SMichal Swiatkowski 		}
2549e300987SMichal Swiatkowski 		i++;
2559e300987SMichal Swiatkowski 	}
2569e300987SMichal Swiatkowski 
2579e300987SMichal Swiatkowski 	if (flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
2589e300987SMichal Swiatkowski 		     ICE_TC_FLWR_FIELD_ENC_DEST_IPV6)) {
2599e300987SMichal Swiatkowski 		list[i].type = ice_proto_type_from_ipv6(false);
2609e300987SMichal Swiatkowski 
2619e300987SMichal Swiatkowski 		if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_IPV6) {
2629e300987SMichal Swiatkowski 			memcpy(&list[i].h_u.ipv6_hdr.src_addr,
2639e300987SMichal Swiatkowski 			       &hdr->l3_key.src_ipv6_addr,
2649e300987SMichal Swiatkowski 			       sizeof(hdr->l3_key.src_ipv6_addr));
2659e300987SMichal Swiatkowski 			memcpy(&list[i].m_u.ipv6_hdr.src_addr,
2669e300987SMichal Swiatkowski 			       &hdr->l3_mask.src_ipv6_addr,
2679e300987SMichal Swiatkowski 			       sizeof(hdr->l3_mask.src_ipv6_addr));
2689e300987SMichal Swiatkowski 		}
2699e300987SMichal Swiatkowski 		if (flags & ICE_TC_FLWR_FIELD_ENC_DEST_IPV6) {
2709e300987SMichal Swiatkowski 			memcpy(&list[i].h_u.ipv6_hdr.dst_addr,
2719e300987SMichal Swiatkowski 			       &hdr->l3_key.dst_ipv6_addr,
2729e300987SMichal Swiatkowski 			       sizeof(hdr->l3_key.dst_ipv6_addr));
2739e300987SMichal Swiatkowski 			memcpy(&list[i].m_u.ipv6_hdr.dst_addr,
2749e300987SMichal Swiatkowski 			       &hdr->l3_mask.dst_ipv6_addr,
2759e300987SMichal Swiatkowski 			       sizeof(hdr->l3_mask.dst_ipv6_addr));
2769e300987SMichal Swiatkowski 		}
2779e300987SMichal Swiatkowski 		i++;
2789e300987SMichal Swiatkowski 	}
2799e300987SMichal Swiatkowski 
2804c99bc96SMarcin Szycik 	if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IP) &&
2814c99bc96SMarcin Szycik 	    (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
2824c99bc96SMarcin Szycik 		      ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
2834c99bc96SMarcin Szycik 		list[i].type = ice_proto_type_from_ipv4(false);
2844c99bc96SMarcin Szycik 
2854c99bc96SMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
2864c99bc96SMarcin Szycik 			list[i].h_u.ipv4_hdr.tos = hdr->l3_key.tos;
2874c99bc96SMarcin Szycik 			list[i].m_u.ipv4_hdr.tos = hdr->l3_mask.tos;
2884c99bc96SMarcin Szycik 		}
2894c99bc96SMarcin Szycik 
2904c99bc96SMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
2914c99bc96SMarcin Szycik 			list[i].h_u.ipv4_hdr.time_to_live = hdr->l3_key.ttl;
2924c99bc96SMarcin Szycik 			list[i].m_u.ipv4_hdr.time_to_live = hdr->l3_mask.ttl;
2934c99bc96SMarcin Szycik 		}
2944c99bc96SMarcin Szycik 
2954c99bc96SMarcin Szycik 		i++;
2964c99bc96SMarcin Szycik 	}
2974c99bc96SMarcin Szycik 
2984c99bc96SMarcin Szycik 	if (fltr->inner_headers.l2_key.n_proto == htons(ETH_P_IPV6) &&
2994c99bc96SMarcin Szycik 	    (flags & (ICE_TC_FLWR_FIELD_ENC_IP_TOS |
3004c99bc96SMarcin Szycik 		      ICE_TC_FLWR_FIELD_ENC_IP_TTL))) {
3014c99bc96SMarcin Szycik 		struct ice_ipv6_hdr *hdr_h, *hdr_m;
3024c99bc96SMarcin Szycik 
3034c99bc96SMarcin Szycik 		hdr_h = &list[i].h_u.ipv6_hdr;
3044c99bc96SMarcin Szycik 		hdr_m = &list[i].m_u.ipv6_hdr;
3054c99bc96SMarcin Szycik 		list[i].type = ice_proto_type_from_ipv6(false);
3064c99bc96SMarcin Szycik 
3074c99bc96SMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TOS) {
3084c99bc96SMarcin Szycik 			be32p_replace_bits(&hdr_h->be_ver_tc_flow,
3094c99bc96SMarcin Szycik 					   hdr->l3_key.tos,
3104c99bc96SMarcin Szycik 					   ICE_IPV6_HDR_TC_MASK);
3114c99bc96SMarcin Szycik 			be32p_replace_bits(&hdr_m->be_ver_tc_flow,
3124c99bc96SMarcin Szycik 					   hdr->l3_mask.tos,
3134c99bc96SMarcin Szycik 					   ICE_IPV6_HDR_TC_MASK);
3144c99bc96SMarcin Szycik 		}
3154c99bc96SMarcin Szycik 
3164c99bc96SMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_ENC_IP_TTL) {
3174c99bc96SMarcin Szycik 			hdr_h->hop_limit = hdr->l3_key.ttl;
3184c99bc96SMarcin Szycik 			hdr_m->hop_limit = hdr->l3_mask.ttl;
3194c99bc96SMarcin Szycik 		}
3204c99bc96SMarcin Szycik 
3214c99bc96SMarcin Szycik 		i++;
3224c99bc96SMarcin Szycik 	}
3234c99bc96SMarcin Szycik 
3240e32ff02SMichal Swiatkowski 	if ((flags & ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT) &&
3250e32ff02SMichal Swiatkowski 	    hdr->l3_key.ip_proto == IPPROTO_UDP) {
3260e32ff02SMichal Swiatkowski 		list[i].type = ICE_UDP_OF;
3279e300987SMichal Swiatkowski 		list[i].h_u.l4_hdr.dst_port = hdr->l4_key.dst_port;
3289e300987SMichal Swiatkowski 		list[i].m_u.l4_hdr.dst_port = hdr->l4_mask.dst_port;
3299e300987SMichal Swiatkowski 		i++;
3309e300987SMichal Swiatkowski 	}
3319e300987SMichal Swiatkowski 
33203592a14SMichal Swiatkowski 	/* always fill matching on tunneled packets in metadata */
3330960a27bSMarcin Szycik 	ice_rule_add_tunnel_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
33403592a14SMichal Swiatkowski 
3359e300987SMichal Swiatkowski 	return i;
3369e300987SMichal Swiatkowski }
3379e300987SMichal Swiatkowski 
3380d08a441SKiran Patil /**
3390d08a441SKiran Patil  * ice_tc_fill_rules - fill filter rules based on TC fltr
3400d08a441SKiran Patil  * @hw: pointer to HW structure
3410d08a441SKiran Patil  * @flags: tc flower field flags
3420d08a441SKiran Patil  * @tc_fltr: pointer to TC flower filter
3430d08a441SKiran Patil  * @list: list of advance rule elements
3440d08a441SKiran Patil  * @rule_info: pointer to information about rule
3450d08a441SKiran Patil  * @l4_proto: pointer to information such as L4 proto type
3460d08a441SKiran Patil  *
3470d08a441SKiran Patil  * Fill ice_adv_lkup_elem list based on TC flower flags and
3480d08a441SKiran Patil  * TC flower headers. This list should be used to add
3490d08a441SKiran Patil  * advance filter in hardware.
3500d08a441SKiran Patil  */
3510d08a441SKiran Patil static int
ice_tc_fill_rules(struct ice_hw * hw,u32 flags,struct ice_tc_flower_fltr * tc_fltr,struct ice_adv_lkup_elem * list,struct ice_adv_rule_info * rule_info,u16 * l4_proto)3520d08a441SKiran Patil ice_tc_fill_rules(struct ice_hw *hw, u32 flags,
3530d08a441SKiran Patil 		  struct ice_tc_flower_fltr *tc_fltr,
3540d08a441SKiran Patil 		  struct ice_adv_lkup_elem *list,
3550d08a441SKiran Patil 		  struct ice_adv_rule_info *rule_info,
3560d08a441SKiran Patil 		  u16 *l4_proto)
3570d08a441SKiran Patil {
3580d08a441SKiran Patil 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
3599e300987SMichal Swiatkowski 	bool inner = false;
360ea71b967SMartyna Szapar-Mudlaw 	u16 vlan_tpid = 0;
3610960a27bSMarcin Szycik 	int i = 1; /* 0th lookup is metadata */
3620d08a441SKiran Patil 
363ea71b967SMartyna Szapar-Mudlaw 	rule_info->vlan_type = vlan_tpid;
364ea71b967SMartyna Szapar-Mudlaw 
3650960a27bSMarcin Szycik 	/* Always add direction metadata */
3660960a27bSMarcin Szycik 	ice_rule_add_direction_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
3670960a27bSMarcin Szycik 
36891efeadfSMichal Swiatkowski 	if (tc_fltr->direction == ICE_ESWITCH_FLTR_EGRESS) {
36991efeadfSMichal Swiatkowski 		ice_rule_add_src_vsi_metadata(&list[i]);
37091efeadfSMichal Swiatkowski 		i++;
37191efeadfSMichal Swiatkowski 	}
37291efeadfSMichal Swiatkowski 
3739e300987SMichal Swiatkowski 	rule_info->tun_type = ice_sw_type_from_tunnel(tc_fltr->tunnel_type);
3749e300987SMichal Swiatkowski 	if (tc_fltr->tunnel_type != TNL_LAST) {
3750960a27bSMarcin Szycik 		i = ice_tc_fill_tunnel_outer(flags, tc_fltr, list, i);
3769e300987SMichal Swiatkowski 
3779e300987SMichal Swiatkowski 		headers = &tc_fltr->inner_headers;
3789e300987SMichal Swiatkowski 		inner = true;
37934a89775SMartyna Szapar-Mudlaw 	}
38034a89775SMartyna Szapar-Mudlaw 
38134a89775SMartyna Szapar-Mudlaw 	if (flags & ICE_TC_FLWR_FIELD_ETH_TYPE_ID) {
38234a89775SMartyna Szapar-Mudlaw 		list[i].type = ice_proto_type_from_etype(inner);
3830d08a441SKiran Patil 		list[i].h_u.ethertype.ethtype_id = headers->l2_key.n_proto;
3840d08a441SKiran Patil 		list[i].m_u.ethertype.ethtype_id = headers->l2_mask.n_proto;
3850d08a441SKiran Patil 		i++;
3860d08a441SKiran Patil 	}
3870d08a441SKiran Patil 
3880d08a441SKiran Patil 	if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
3890d08a441SKiran Patil 		     ICE_TC_FLWR_FIELD_SRC_MAC)) {
3900d08a441SKiran Patil 		struct ice_tc_l2_hdr *l2_key, *l2_mask;
3910d08a441SKiran Patil 
3920d08a441SKiran Patil 		l2_key = &headers->l2_key;
3930d08a441SKiran Patil 		l2_mask = &headers->l2_mask;
3940d08a441SKiran Patil 
3959e300987SMichal Swiatkowski 		list[i].type = ice_proto_type_from_mac(inner);
3960d08a441SKiran Patil 		if (flags & ICE_TC_FLWR_FIELD_DST_MAC) {
3970d08a441SKiran Patil 			ether_addr_copy(list[i].h_u.eth_hdr.dst_addr,
3980d08a441SKiran Patil 					l2_key->dst_mac);
3990d08a441SKiran Patil 			ether_addr_copy(list[i].m_u.eth_hdr.dst_addr,
4000d08a441SKiran Patil 					l2_mask->dst_mac);
4010d08a441SKiran Patil 		}
4020d08a441SKiran Patil 		if (flags & ICE_TC_FLWR_FIELD_SRC_MAC) {
4030d08a441SKiran Patil 			ether_addr_copy(list[i].h_u.eth_hdr.src_addr,
4040d08a441SKiran Patil 					l2_key->src_mac);
4050d08a441SKiran Patil 			ether_addr_copy(list[i].m_u.eth_hdr.src_addr,
4060d08a441SKiran Patil 					l2_mask->src_mac);
4070d08a441SKiran Patil 		}
4080d08a441SKiran Patil 		i++;
4090d08a441SKiran Patil 	}
4100d08a441SKiran Patil 
4110d08a441SKiran Patil 	/* copy VLAN info */
41234800178SMartyna Szapar-Mudlaw 	if (flags & (ICE_TC_FLWR_FIELD_VLAN | ICE_TC_FLWR_FIELD_VLAN_PRIO)) {
41306bca7c2SMartyna Szapar-Mudlaw 		if (flags & ICE_TC_FLWR_FIELD_CVLAN)
41406bca7c2SMartyna Szapar-Mudlaw 			list[i].type = ICE_VLAN_EX;
41506bca7c2SMartyna Szapar-Mudlaw 		else
4160d08a441SKiran Patil 			list[i].type = ICE_VLAN_OFOS;
41734800178SMartyna Szapar-Mudlaw 
41834800178SMartyna Szapar-Mudlaw 		if (flags & ICE_TC_FLWR_FIELD_VLAN) {
4190d08a441SKiran Patil 			list[i].h_u.vlan_hdr.vlan = headers->vlan_hdr.vlan_id;
42034800178SMartyna Szapar-Mudlaw 			list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
42134800178SMartyna Szapar-Mudlaw 		}
42234800178SMartyna Szapar-Mudlaw 
42334800178SMartyna Szapar-Mudlaw 		if (flags & ICE_TC_FLWR_FIELD_VLAN_PRIO) {
42434800178SMartyna Szapar-Mudlaw 			if (flags & ICE_TC_FLWR_FIELD_VLAN) {
42534800178SMartyna Szapar-Mudlaw 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
42634800178SMartyna Szapar-Mudlaw 			} else {
42734800178SMartyna Szapar-Mudlaw 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
42834800178SMartyna Szapar-Mudlaw 				list[i].h_u.vlan_hdr.vlan = 0;
42934800178SMartyna Szapar-Mudlaw 			}
43034800178SMartyna Szapar-Mudlaw 			list[i].h_u.vlan_hdr.vlan |=
43134800178SMartyna Szapar-Mudlaw 				headers->vlan_hdr.vlan_prio;
43234800178SMartyna Szapar-Mudlaw 		}
43334800178SMartyna Szapar-Mudlaw 
4340d08a441SKiran Patil 		i++;
4350d08a441SKiran Patil 	}
4360d08a441SKiran Patil 
43703592a14SMichal Swiatkowski 	if (flags & ICE_TC_FLWR_FIELD_VLAN_TPID) {
43803592a14SMichal Swiatkowski 		vlan_tpid = be16_to_cpu(headers->vlan_hdr.vlan_tpid);
43903592a14SMichal Swiatkowski 		rule_info->vlan_type =
44003592a14SMichal Swiatkowski 				ice_check_supported_vlan_tpid(vlan_tpid);
44103592a14SMichal Swiatkowski 
4420960a27bSMarcin Szycik 		ice_rule_add_vlan_metadata(&list[ICE_TC_METADATA_LKUP_IDX]);
44303592a14SMichal Swiatkowski 	}
44403592a14SMichal Swiatkowski 
44534800178SMartyna Szapar-Mudlaw 	if (flags & (ICE_TC_FLWR_FIELD_CVLAN | ICE_TC_FLWR_FIELD_CVLAN_PRIO)) {
44606bca7c2SMartyna Szapar-Mudlaw 		list[i].type = ICE_VLAN_IN;
44734800178SMartyna Szapar-Mudlaw 
44834800178SMartyna Szapar-Mudlaw 		if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
44906bca7c2SMartyna Szapar-Mudlaw 			list[i].h_u.vlan_hdr.vlan = headers->cvlan_hdr.vlan_id;
45034800178SMartyna Szapar-Mudlaw 			list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0x0FFF);
45134800178SMartyna Szapar-Mudlaw 		}
45234800178SMartyna Szapar-Mudlaw 
45334800178SMartyna Szapar-Mudlaw 		if (flags & ICE_TC_FLWR_FIELD_CVLAN_PRIO) {
45434800178SMartyna Szapar-Mudlaw 			if (flags & ICE_TC_FLWR_FIELD_CVLAN) {
45534800178SMartyna Szapar-Mudlaw 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xEFFF);
45634800178SMartyna Szapar-Mudlaw 			} else {
45734800178SMartyna Szapar-Mudlaw 				list[i].m_u.vlan_hdr.vlan = cpu_to_be16(0xE000);
45834800178SMartyna Szapar-Mudlaw 				list[i].h_u.vlan_hdr.vlan = 0;
45934800178SMartyna Szapar-Mudlaw 			}
46034800178SMartyna Szapar-Mudlaw 			list[i].h_u.vlan_hdr.vlan |=
46134800178SMartyna Szapar-Mudlaw 				headers->cvlan_hdr.vlan_prio;
46234800178SMartyna Szapar-Mudlaw 		}
46334800178SMartyna Szapar-Mudlaw 
46406bca7c2SMartyna Szapar-Mudlaw 		i++;
46506bca7c2SMartyna Szapar-Mudlaw 	}
46606bca7c2SMartyna Szapar-Mudlaw 
467cd8efeeeSMarcin Szycik 	if (flags & (ICE_TC_FLWR_FIELD_PPPOE_SESSID |
468cd8efeeeSMarcin Szycik 		     ICE_TC_FLWR_FIELD_PPP_PROTO)) {
469cd8efeeeSMarcin Szycik 		struct ice_pppoe_hdr *vals, *masks;
470cd8efeeeSMarcin Szycik 
471cd8efeeeSMarcin Szycik 		vals = &list[i].h_u.pppoe_hdr;
472cd8efeeeSMarcin Szycik 		masks = &list[i].m_u.pppoe_hdr;
473cd8efeeeSMarcin Szycik 
474cd8efeeeSMarcin Szycik 		list[i].type = ICE_PPPOE;
475cd8efeeeSMarcin Szycik 
476cd8efeeeSMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_PPPOE_SESSID) {
477cd8efeeeSMarcin Szycik 			vals->session_id = headers->pppoe_hdr.session_id;
478cd8efeeeSMarcin Szycik 			masks->session_id = cpu_to_be16(0xFFFF);
479cd8efeeeSMarcin Szycik 		}
480cd8efeeeSMarcin Szycik 
481cd8efeeeSMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_PPP_PROTO) {
482cd8efeeeSMarcin Szycik 			vals->ppp_prot_id = headers->pppoe_hdr.ppp_proto;
483cd8efeeeSMarcin Szycik 			masks->ppp_prot_id = cpu_to_be16(0xFFFF);
484cd8efeeeSMarcin Szycik 		}
485cd8efeeeSMarcin Szycik 
486cd8efeeeSMarcin Szycik 		i++;
487cd8efeeeSMarcin Szycik 	}
488cd8efeeeSMarcin Szycik 
4890d08a441SKiran Patil 	/* copy L3 (IPv[4|6]: src, dest) address */
4900d08a441SKiran Patil 	if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV4 |
4910d08a441SKiran Patil 		     ICE_TC_FLWR_FIELD_SRC_IPV4)) {
4920d08a441SKiran Patil 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
4930d08a441SKiran Patil 
4949e300987SMichal Swiatkowski 		list[i].type = ice_proto_type_from_ipv4(inner);
4950d08a441SKiran Patil 		l3_key = &headers->l3_key;
4960d08a441SKiran Patil 		l3_mask = &headers->l3_mask;
4970d08a441SKiran Patil 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV4) {
4980d08a441SKiran Patil 			list[i].h_u.ipv4_hdr.dst_addr = l3_key->dst_ipv4;
4990d08a441SKiran Patil 			list[i].m_u.ipv4_hdr.dst_addr = l3_mask->dst_ipv4;
5000d08a441SKiran Patil 		}
5010d08a441SKiran Patil 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV4) {
5020d08a441SKiran Patil 			list[i].h_u.ipv4_hdr.src_addr = l3_key->src_ipv4;
5030d08a441SKiran Patil 			list[i].m_u.ipv4_hdr.src_addr = l3_mask->src_ipv4;
5040d08a441SKiran Patil 		}
5050d08a441SKiran Patil 		i++;
5060d08a441SKiran Patil 	} else if (flags & (ICE_TC_FLWR_FIELD_DEST_IPV6 |
5070d08a441SKiran Patil 			    ICE_TC_FLWR_FIELD_SRC_IPV6)) {
5080d08a441SKiran Patil 		struct ice_ipv6_hdr *ipv6_hdr, *ipv6_mask;
5090d08a441SKiran Patil 		struct ice_tc_l3_hdr *l3_key, *l3_mask;
5100d08a441SKiran Patil 
5119e300987SMichal Swiatkowski 		list[i].type = ice_proto_type_from_ipv6(inner);
5120d08a441SKiran Patil 		ipv6_hdr = &list[i].h_u.ipv6_hdr;
5130d08a441SKiran Patil 		ipv6_mask = &list[i].m_u.ipv6_hdr;
5140d08a441SKiran Patil 		l3_key = &headers->l3_key;
5150d08a441SKiran Patil 		l3_mask = &headers->l3_mask;
5160d08a441SKiran Patil 
5170d08a441SKiran Patil 		if (flags & ICE_TC_FLWR_FIELD_DEST_IPV6) {
5180d08a441SKiran Patil 			memcpy(&ipv6_hdr->dst_addr, &l3_key->dst_ipv6_addr,
5190d08a441SKiran Patil 			       sizeof(l3_key->dst_ipv6_addr));
5200d08a441SKiran Patil 			memcpy(&ipv6_mask->dst_addr, &l3_mask->dst_ipv6_addr,
5210d08a441SKiran Patil 			       sizeof(l3_mask->dst_ipv6_addr));
5220d08a441SKiran Patil 		}
5230d08a441SKiran Patil 		if (flags & ICE_TC_FLWR_FIELD_SRC_IPV6) {
5240d08a441SKiran Patil 			memcpy(&ipv6_hdr->src_addr, &l3_key->src_ipv6_addr,
5250d08a441SKiran Patil 			       sizeof(l3_key->src_ipv6_addr));
5260d08a441SKiran Patil 			memcpy(&ipv6_mask->src_addr, &l3_mask->src_ipv6_addr,
5270d08a441SKiran Patil 			       sizeof(l3_mask->src_ipv6_addr));
5280d08a441SKiran Patil 		}
5290d08a441SKiran Patil 		i++;
5300d08a441SKiran Patil 	}
5310d08a441SKiran Patil 
5324c99bc96SMarcin Szycik 	if (headers->l2_key.n_proto == htons(ETH_P_IP) &&
5334c99bc96SMarcin Szycik 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
5344c99bc96SMarcin Szycik 		list[i].type = ice_proto_type_from_ipv4(inner);
5354c99bc96SMarcin Szycik 
5364c99bc96SMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
5374c99bc96SMarcin Szycik 			list[i].h_u.ipv4_hdr.tos = headers->l3_key.tos;
5384c99bc96SMarcin Szycik 			list[i].m_u.ipv4_hdr.tos = headers->l3_mask.tos;
5394c99bc96SMarcin Szycik 		}
5404c99bc96SMarcin Szycik 
5414c99bc96SMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
5424c99bc96SMarcin Szycik 			list[i].h_u.ipv4_hdr.time_to_live =
5434c99bc96SMarcin Szycik 				headers->l3_key.ttl;
5444c99bc96SMarcin Szycik 			list[i].m_u.ipv4_hdr.time_to_live =
5454c99bc96SMarcin Szycik 				headers->l3_mask.ttl;
5464c99bc96SMarcin Szycik 		}
5474c99bc96SMarcin Szycik 
5484c99bc96SMarcin Szycik 		i++;
5494c99bc96SMarcin Szycik 	}
5504c99bc96SMarcin Szycik 
5514c99bc96SMarcin Szycik 	if (headers->l2_key.n_proto == htons(ETH_P_IPV6) &&
5524c99bc96SMarcin Szycik 	    (flags & (ICE_TC_FLWR_FIELD_IP_TOS | ICE_TC_FLWR_FIELD_IP_TTL))) {
5534c99bc96SMarcin Szycik 		struct ice_ipv6_hdr *hdr_h, *hdr_m;
5544c99bc96SMarcin Szycik 
5554c99bc96SMarcin Szycik 		hdr_h = &list[i].h_u.ipv6_hdr;
5564c99bc96SMarcin Szycik 		hdr_m = &list[i].m_u.ipv6_hdr;
5574c99bc96SMarcin Szycik 		list[i].type = ice_proto_type_from_ipv6(inner);
5584c99bc96SMarcin Szycik 
5594c99bc96SMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_IP_TOS) {
5604c99bc96SMarcin Szycik 			be32p_replace_bits(&hdr_h->be_ver_tc_flow,
5614c99bc96SMarcin Szycik 					   headers->l3_key.tos,
5624c99bc96SMarcin Szycik 					   ICE_IPV6_HDR_TC_MASK);
5634c99bc96SMarcin Szycik 			be32p_replace_bits(&hdr_m->be_ver_tc_flow,
5644c99bc96SMarcin Szycik 					   headers->l3_mask.tos,
5654c99bc96SMarcin Szycik 					   ICE_IPV6_HDR_TC_MASK);
5664c99bc96SMarcin Szycik 		}
5674c99bc96SMarcin Szycik 
5684c99bc96SMarcin Szycik 		if (flags & ICE_TC_FLWR_FIELD_IP_TTL) {
5694c99bc96SMarcin Szycik 			hdr_h->hop_limit = headers->l3_key.ttl;
5704c99bc96SMarcin Szycik 			hdr_m->hop_limit = headers->l3_mask.ttl;
5714c99bc96SMarcin Szycik 		}
5724c99bc96SMarcin Szycik 
5734c99bc96SMarcin Szycik 		i++;
5744c99bc96SMarcin Szycik 	}
5754c99bc96SMarcin Szycik 
576cd634549SMarcin Szycik 	if (flags & ICE_TC_FLWR_FIELD_L2TPV3_SESSID) {
577cd634549SMarcin Szycik 		list[i].type = ICE_L2TPV3;
578cd634549SMarcin Szycik 
579cd634549SMarcin Szycik 		list[i].h_u.l2tpv3_sess_hdr.session_id =
580cd634549SMarcin Szycik 			headers->l2tpv3_hdr.session_id;
581cd634549SMarcin Szycik 		list[i].m_u.l2tpv3_sess_hdr.session_id =
582cd634549SMarcin Szycik 			cpu_to_be32(0xFFFFFFFF);
583cd634549SMarcin Szycik 
584cd634549SMarcin Szycik 		i++;
585cd634549SMarcin Szycik 	}
586cd634549SMarcin Szycik 
5870d08a441SKiran Patil 	/* copy L4 (src, dest) port */
5880d08a441SKiran Patil 	if (flags & (ICE_TC_FLWR_FIELD_DEST_L4_PORT |
5890d08a441SKiran Patil 		     ICE_TC_FLWR_FIELD_SRC_L4_PORT)) {
5900d08a441SKiran Patil 		struct ice_tc_l4_hdr *l4_key, *l4_mask;
5910d08a441SKiran Patil 
5920e32ff02SMichal Swiatkowski 		list[i].type = ice_proto_type_from_l4_port(headers->l3_key.ip_proto);
5930d08a441SKiran Patil 		l4_key = &headers->l4_key;
5940d08a441SKiran Patil 		l4_mask = &headers->l4_mask;
5959e300987SMichal Swiatkowski 
5960d08a441SKiran Patil 		if (flags & ICE_TC_FLWR_FIELD_DEST_L4_PORT) {
5970d08a441SKiran Patil 			list[i].h_u.l4_hdr.dst_port = l4_key->dst_port;
5980d08a441SKiran Patil 			list[i].m_u.l4_hdr.dst_port = l4_mask->dst_port;
5990d08a441SKiran Patil 		}
6000d08a441SKiran Patil 		if (flags & ICE_TC_FLWR_FIELD_SRC_L4_PORT) {
6010d08a441SKiran Patil 			list[i].h_u.l4_hdr.src_port = l4_key->src_port;
6020d08a441SKiran Patil 			list[i].m_u.l4_hdr.src_port = l4_mask->src_port;
6030d08a441SKiran Patil 		}
6040d08a441SKiran Patil 		i++;
6050d08a441SKiran Patil 	}
6060d08a441SKiran Patil 
6070d08a441SKiran Patil 	return i;
6080d08a441SKiran Patil }
6090d08a441SKiran Patil 
6109e300987SMichal Swiatkowski /**
6119e300987SMichal Swiatkowski  * ice_tc_tun_get_type - get the tunnel type
6129e300987SMichal Swiatkowski  * @tunnel_dev: ptr to tunnel device
6139e300987SMichal Swiatkowski  *
6149e300987SMichal Swiatkowski  * This function detects appropriate tunnel_type if specified device is
6159e300987SMichal Swiatkowski  * tunnel device such as VXLAN/Geneve
6169e300987SMichal Swiatkowski  */
ice_tc_tun_get_type(struct net_device * tunnel_dev)6179e300987SMichal Swiatkowski static int ice_tc_tun_get_type(struct net_device *tunnel_dev)
6189e300987SMichal Swiatkowski {
6199e300987SMichal Swiatkowski 	if (netif_is_vxlan(tunnel_dev))
6209e300987SMichal Swiatkowski 		return TNL_VXLAN;
6219e300987SMichal Swiatkowski 	if (netif_is_geneve(tunnel_dev))
6229e300987SMichal Swiatkowski 		return TNL_GENEVE;
623f0a35040SMichal Swiatkowski 	if (netif_is_gretap(tunnel_dev) ||
624f0a35040SMichal Swiatkowski 	    netif_is_ip6gretap(tunnel_dev))
625f0a35040SMichal Swiatkowski 		return TNL_GRETAP;
6269a225f81SMarcin Szycik 
6279a225f81SMarcin Szycik 	/* Assume GTP-U by default in case of GTP netdev.
6289a225f81SMarcin Szycik 	 * GTP-C may be selected later, based on enc_dst_port.
6299a225f81SMarcin Szycik 	 */
6309a225f81SMarcin Szycik 	if (netif_is_gtp(tunnel_dev))
6319a225f81SMarcin Szycik 		return TNL_GTPU;
6329e300987SMichal Swiatkowski 	return TNL_LAST;
6339e300987SMichal Swiatkowski }
6349e300987SMichal Swiatkowski 
ice_is_tunnel_supported(struct net_device * dev)6359e300987SMichal Swiatkowski bool ice_is_tunnel_supported(struct net_device *dev)
6369e300987SMichal Swiatkowski {
6379e300987SMichal Swiatkowski 	return ice_tc_tun_get_type(dev) != TNL_LAST;
6389e300987SMichal Swiatkowski }
6399e300987SMichal Swiatkowski 
ice_tc_is_dev_uplink(struct net_device * dev)64010fc8d42SAniruddha Paul static bool ice_tc_is_dev_uplink(struct net_device *dev)
64110fc8d42SAniruddha Paul {
64210fc8d42SAniruddha Paul 	return netif_is_ice(dev) || ice_is_tunnel_supported(dev);
64310fc8d42SAniruddha Paul }
64410fc8d42SAniruddha Paul 
ice_tc_setup_redirect_action(struct net_device * filter_dev,struct ice_tc_flower_fltr * fltr,struct net_device * target_dev)64510fc8d42SAniruddha Paul static int ice_tc_setup_redirect_action(struct net_device *filter_dev,
64610fc8d42SAniruddha Paul 					struct ice_tc_flower_fltr *fltr,
64710fc8d42SAniruddha Paul 					struct net_device *target_dev)
6480d08a441SKiran Patil {
6490d08a441SKiran Patil 	struct ice_repr *repr;
6500d08a441SKiran Patil 
65110fc8d42SAniruddha Paul 	fltr->action.fltr_act = ICE_FWD_TO_VSI;
65210fc8d42SAniruddha Paul 
65310fc8d42SAniruddha Paul 	if (ice_is_port_repr_netdev(filter_dev) &&
65410fc8d42SAniruddha Paul 	    ice_is_port_repr_netdev(target_dev)) {
65510fc8d42SAniruddha Paul 		repr = ice_netdev_to_repr(target_dev);
65610fc8d42SAniruddha Paul 
65710fc8d42SAniruddha Paul 		fltr->dest_vsi = repr->src_vsi;
65810fc8d42SAniruddha Paul 		fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
65910fc8d42SAniruddha Paul 	} else if (ice_is_port_repr_netdev(filter_dev) &&
66010fc8d42SAniruddha Paul 		   ice_tc_is_dev_uplink(target_dev)) {
66110fc8d42SAniruddha Paul 		repr = ice_netdev_to_repr(filter_dev);
66210fc8d42SAniruddha Paul 
663*3b66266aSMichal Swiatkowski 		fltr->dest_vsi = repr->src_vsi->back->eswitch.uplink_vsi;
66410fc8d42SAniruddha Paul 		fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
66510fc8d42SAniruddha Paul 	} else if (ice_tc_is_dev_uplink(filter_dev) &&
66610fc8d42SAniruddha Paul 		   ice_is_port_repr_netdev(target_dev)) {
66710fc8d42SAniruddha Paul 		repr = ice_netdev_to_repr(target_dev);
66810fc8d42SAniruddha Paul 
66910fc8d42SAniruddha Paul 		fltr->dest_vsi = repr->src_vsi;
67010fc8d42SAniruddha Paul 		fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
67110fc8d42SAniruddha Paul 	} else {
67210fc8d42SAniruddha Paul 		NL_SET_ERR_MSG_MOD(fltr->extack,
67310fc8d42SAniruddha Paul 				   "Unsupported netdevice in switchdev mode");
67410fc8d42SAniruddha Paul 		return -EINVAL;
67510fc8d42SAniruddha Paul 	}
67610fc8d42SAniruddha Paul 
67710fc8d42SAniruddha Paul 	return 0;
67810fc8d42SAniruddha Paul }
67910fc8d42SAniruddha Paul 
680f00f4924SMarcin Szycik static int
ice_tc_setup_drop_action(struct net_device * filter_dev,struct ice_tc_flower_fltr * fltr)681f00f4924SMarcin Szycik ice_tc_setup_drop_action(struct net_device *filter_dev,
682f00f4924SMarcin Szycik 			 struct ice_tc_flower_fltr *fltr)
683f00f4924SMarcin Szycik {
684f00f4924SMarcin Szycik 	fltr->action.fltr_act = ICE_DROP_PACKET;
685f00f4924SMarcin Szycik 
686f00f4924SMarcin Szycik 	if (ice_is_port_repr_netdev(filter_dev)) {
687f00f4924SMarcin Szycik 		fltr->direction = ICE_ESWITCH_FLTR_EGRESS;
688f00f4924SMarcin Szycik 	} else if (ice_tc_is_dev_uplink(filter_dev)) {
689f00f4924SMarcin Szycik 		fltr->direction = ICE_ESWITCH_FLTR_INGRESS;
690f00f4924SMarcin Szycik 	} else {
691f00f4924SMarcin Szycik 		NL_SET_ERR_MSG_MOD(fltr->extack,
692f00f4924SMarcin Szycik 				   "Unsupported netdevice in switchdev mode");
693f00f4924SMarcin Szycik 		return -EINVAL;
694f00f4924SMarcin Szycik 	}
695f00f4924SMarcin Szycik 
696f00f4924SMarcin Szycik 	return 0;
697f00f4924SMarcin Szycik }
698f00f4924SMarcin Szycik 
ice_eswitch_tc_parse_action(struct net_device * filter_dev,struct ice_tc_flower_fltr * fltr,struct flow_action_entry * act)69910fc8d42SAniruddha Paul static int ice_eswitch_tc_parse_action(struct net_device *filter_dev,
70010fc8d42SAniruddha Paul 				       struct ice_tc_flower_fltr *fltr,
70110fc8d42SAniruddha Paul 				       struct flow_action_entry *act)
70210fc8d42SAniruddha Paul {
70310fc8d42SAniruddha Paul 	int err;
70410fc8d42SAniruddha Paul 
7050d08a441SKiran Patil 	switch (act->id) {
7060d08a441SKiran Patil 	case FLOW_ACTION_DROP:
707f00f4924SMarcin Szycik 		err = ice_tc_setup_drop_action(filter_dev, fltr);
708f00f4924SMarcin Szycik 		if (err)
709f00f4924SMarcin Szycik 			return err;
710f00f4924SMarcin Szycik 
7110d08a441SKiran Patil 		break;
7120d08a441SKiran Patil 
7130d08a441SKiran Patil 	case FLOW_ACTION_REDIRECT:
71410fc8d42SAniruddha Paul 		err = ice_tc_setup_redirect_action(filter_dev, fltr, act->dev);
71510fc8d42SAniruddha Paul 		if (err)
71610fc8d42SAniruddha Paul 			return err;
7170d08a441SKiran Patil 
7180d08a441SKiran Patil 		break;
7190d08a441SKiran Patil 
7200d08a441SKiran Patil 	default:
7210d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported action in switchdev mode");
7220d08a441SKiran Patil 		return -EINVAL;
7230d08a441SKiran Patil 	}
7240d08a441SKiran Patil 
7250d08a441SKiran Patil 	return 0;
7260d08a441SKiran Patil }
7270d08a441SKiran Patil 
7280d08a441SKiran Patil static int
ice_eswitch_add_tc_fltr(struct ice_vsi * vsi,struct ice_tc_flower_fltr * fltr)7290d08a441SKiran Patil ice_eswitch_add_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
7300d08a441SKiran Patil {
7310d08a441SKiran Patil 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
7320d08a441SKiran Patil 	struct ice_adv_rule_info rule_info = { 0 };
7330d08a441SKiran Patil 	struct ice_rule_query_data rule_added;
7340d08a441SKiran Patil 	struct ice_hw *hw = &vsi->back->hw;
7350d08a441SKiran Patil 	struct ice_adv_lkup_elem *list;
7360d08a441SKiran Patil 	u32 flags = fltr->flags;
7370d08a441SKiran Patil 	int lkups_cnt;
7382ccc1c1cSTony Nguyen 	int ret;
7390d08a441SKiran Patil 	int i;
7400d08a441SKiran Patil 
7418d2e34e4SMichal Swiatkowski 	if (flags & ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT) {
7420d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported encap field(s)");
7430d08a441SKiran Patil 		return -EOPNOTSUPP;
7440d08a441SKiran Patil 	}
7450d08a441SKiran Patil 
7460d08a441SKiran Patil 	lkups_cnt = ice_tc_count_lkups(flags, headers, fltr);
7470d08a441SKiran Patil 	list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
7480d08a441SKiran Patil 	if (!list)
7490d08a441SKiran Patil 		return -ENOMEM;
7500d08a441SKiran Patil 
7510d08a441SKiran Patil 	i = ice_tc_fill_rules(hw, flags, fltr, list, &rule_info, NULL);
7520d08a441SKiran Patil 	if (i != lkups_cnt) {
7530d08a441SKiran Patil 		ret = -EINVAL;
7540d08a441SKiran Patil 		goto exit;
7550d08a441SKiran Patil 	}
7560d08a441SKiran Patil 
7570d08a441SKiran Patil 	rule_info.sw_act.fltr_act = fltr->action.fltr_act;
7580d08a441SKiran Patil 	if (fltr->action.fltr_act != ICE_DROP_PACKET)
7590d08a441SKiran Patil 		rule_info.sw_act.vsi_handle = fltr->dest_vsi->idx;
7600d08a441SKiran Patil 	/* For now, making priority to be highest, and it also becomes
7610d08a441SKiran Patil 	 * the priority for recipe which will get created as a result of
7620d08a441SKiran Patil 	 * new extraction sequence based on input set.
7630d08a441SKiran Patil 	 * Priority '7' is max val for switch recipe, higher the number
7640d08a441SKiran Patil 	 * results into order of switch rule evaluation.
7650d08a441SKiran Patil 	 */
7660d08a441SKiran Patil 	rule_info.priority = 7;
7679f699b71SMichal Swiatkowski 	rule_info.flags_info.act_valid = true;
7680d08a441SKiran Patil 
7690d08a441SKiran Patil 	if (fltr->direction == ICE_ESWITCH_FLTR_INGRESS) {
77010fc8d42SAniruddha Paul 		/* Uplink to VF */
7710d08a441SKiran Patil 		rule_info.sw_act.flag |= ICE_FLTR_RX;
7720d08a441SKiran Patil 		rule_info.sw_act.src = hw->pf_id;
7739f699b71SMichal Swiatkowski 		rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
77410fc8d42SAniruddha Paul 	} else if (fltr->direction == ICE_ESWITCH_FLTR_EGRESS &&
775*3b66266aSMichal Swiatkowski 		   fltr->dest_vsi == vsi->back->eswitch.uplink_vsi) {
77610fc8d42SAniruddha Paul 		/* VF to Uplink */
7770d08a441SKiran Patil 		rule_info.sw_act.flag |= ICE_FLTR_TX;
7780d08a441SKiran Patil 		rule_info.sw_act.src = vsi->idx;
77973b483b7SWojciech Drewek 		rule_info.flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
780803f3107SMichal Swiatkowski 		/* This is a specific case. The destination VSI index is
781803f3107SMichal Swiatkowski 		 * overwritten by the source VSI index. This type of filter
782803f3107SMichal Swiatkowski 		 * should allow the packet to go to the LAN, not to the
783803f3107SMichal Swiatkowski 		 * VSI passed here. It should set LAN_EN bit only. However,
784803f3107SMichal Swiatkowski 		 * the VSI must be a valid one. Setting source VSI index
785803f3107SMichal Swiatkowski 		 * here is safe. Even if the result from switch is set LAN_EN
786803f3107SMichal Swiatkowski 		 * and LB_EN (which normally will pass the packet to this VSI)
787803f3107SMichal Swiatkowski 		 * packet won't be seen on the VSI, because local loopback is
788803f3107SMichal Swiatkowski 		 * turned off.
789803f3107SMichal Swiatkowski 		 */
790803f3107SMichal Swiatkowski 		rule_info.sw_act.vsi_handle = vsi->idx;
79110fc8d42SAniruddha Paul 	} else {
79210fc8d42SAniruddha Paul 		/* VF to VF */
79310fc8d42SAniruddha Paul 		rule_info.sw_act.flag |= ICE_FLTR_TX;
79410fc8d42SAniruddha Paul 		rule_info.sw_act.src = vsi->idx;
79510fc8d42SAniruddha Paul 		rule_info.flags_info.act = ICE_SINGLE_ACT_LB_ENABLE;
7960d08a441SKiran Patil 	}
7970d08a441SKiran Patil 
7980d08a441SKiran Patil 	/* specify the cookie as filter_rule_id */
7990d08a441SKiran Patil 	rule_info.fltr_rule_id = fltr->cookie;
80091efeadfSMichal Swiatkowski 	rule_info.src_vsi = vsi->idx;
8010d08a441SKiran Patil 
8022ccc1c1cSTony Nguyen 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
8032ccc1c1cSTony Nguyen 	if (ret == -EEXIST) {
8040d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter because it already exist");
8050d08a441SKiran Patil 		ret = -EINVAL;
8060d08a441SKiran Patil 		goto exit;
8072ccc1c1cSTony Nguyen 	} else if (ret) {
8080d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unable to add filter due to error");
8090d08a441SKiran Patil 		goto exit;
8100d08a441SKiran Patil 	}
8110d08a441SKiran Patil 
8120d08a441SKiran Patil 	/* store the output params, which are needed later for removing
8130d08a441SKiran Patil 	 * advanced switch filter
8140d08a441SKiran Patil 	 */
8150d08a441SKiran Patil 	fltr->rid = rule_added.rid;
8160d08a441SKiran Patil 	fltr->rule_id = rule_added.rule_id;
817143b86f3SAmritha Nambiar 	fltr->dest_vsi_handle = rule_added.vsi_handle;
8180d08a441SKiran Patil 
8190d08a441SKiran Patil exit:
8200d08a441SKiran Patil 	kfree(list);
8210d08a441SKiran Patil 	return ret;
8220d08a441SKiran Patil }
8230d08a441SKiran Patil 
8240d08a441SKiran Patil /**
825143b86f3SAmritha Nambiar  * ice_locate_vsi_using_queue - locate VSI using queue (forward to queue action)
826143b86f3SAmritha Nambiar  * @vsi: Pointer to VSI
827479cdfe3SSridhar Samudrala  * @queue: Queue index
828143b86f3SAmritha Nambiar  *
829479cdfe3SSridhar Samudrala  * Locate the VSI using specified "queue". When ADQ is not enabled,
830479cdfe3SSridhar Samudrala  * always return input VSI, otherwise locate corresponding
831479cdfe3SSridhar Samudrala  * VSI based on per channel "offset" and "qcount"
832143b86f3SAmritha Nambiar  */
833479cdfe3SSridhar Samudrala struct ice_vsi *
ice_locate_vsi_using_queue(struct ice_vsi * vsi,int queue)834479cdfe3SSridhar Samudrala ice_locate_vsi_using_queue(struct ice_vsi *vsi, int queue)
835143b86f3SAmritha Nambiar {
836479cdfe3SSridhar Samudrala 	int num_tc, tc;
837143b86f3SAmritha Nambiar 
838143b86f3SAmritha Nambiar 	/* if ADQ is not active, passed VSI is the candidate VSI */
839143b86f3SAmritha Nambiar 	if (!ice_is_adq_active(vsi->back))
840143b86f3SAmritha Nambiar 		return vsi;
841143b86f3SAmritha Nambiar 
842143b86f3SAmritha Nambiar 	/* Locate the VSI (it could still be main PF VSI or CHNL_VSI depending
843143b86f3SAmritha Nambiar 	 * upon queue number)
844143b86f3SAmritha Nambiar 	 */
845143b86f3SAmritha Nambiar 	num_tc = vsi->mqprio_qopt.qopt.num_tc;
846143b86f3SAmritha Nambiar 
847143b86f3SAmritha Nambiar 	for (tc = 0; tc < num_tc; tc++) {
848143b86f3SAmritha Nambiar 		int qcount = vsi->mqprio_qopt.qopt.count[tc];
849143b86f3SAmritha Nambiar 		int offset = vsi->mqprio_qopt.qopt.offset[tc];
850143b86f3SAmritha Nambiar 
851143b86f3SAmritha Nambiar 		if (queue >= offset && queue < offset + qcount) {
852143b86f3SAmritha Nambiar 			/* for non-ADQ TCs, passed VSI is the candidate VSI */
853143b86f3SAmritha Nambiar 			if (tc < ICE_CHNL_START_TC)
854143b86f3SAmritha Nambiar 				return vsi;
855143b86f3SAmritha Nambiar 			else
856143b86f3SAmritha Nambiar 				return vsi->tc_map_vsi[tc];
857143b86f3SAmritha Nambiar 		}
858143b86f3SAmritha Nambiar 	}
859143b86f3SAmritha Nambiar 	return NULL;
860143b86f3SAmritha Nambiar }
861143b86f3SAmritha Nambiar 
862143b86f3SAmritha Nambiar static struct ice_rx_ring *
ice_locate_rx_ring_using_queue(struct ice_vsi * vsi,struct ice_tc_flower_fltr * tc_fltr)863143b86f3SAmritha Nambiar ice_locate_rx_ring_using_queue(struct ice_vsi *vsi,
864143b86f3SAmritha Nambiar 			       struct ice_tc_flower_fltr *tc_fltr)
865143b86f3SAmritha Nambiar {
866143b86f3SAmritha Nambiar 	u16 queue = tc_fltr->action.fwd.q.queue;
867143b86f3SAmritha Nambiar 
868143b86f3SAmritha Nambiar 	return queue < vsi->num_rxq ? vsi->rx_rings[queue] : NULL;
869143b86f3SAmritha Nambiar }
870143b86f3SAmritha Nambiar 
871143b86f3SAmritha Nambiar /**
872143b86f3SAmritha Nambiar  * ice_tc_forward_action - Determine destination VSI and queue for the action
873143b86f3SAmritha Nambiar  * @vsi: Pointer to VSI
874143b86f3SAmritha Nambiar  * @tc_fltr: Pointer to TC flower filter structure
875143b86f3SAmritha Nambiar  *
876143b86f3SAmritha Nambiar  * Validates the tc forward action and determines the destination VSI and queue
877143b86f3SAmritha Nambiar  * for the forward action.
878143b86f3SAmritha Nambiar  */
879143b86f3SAmritha Nambiar static struct ice_vsi *
ice_tc_forward_action(struct ice_vsi * vsi,struct ice_tc_flower_fltr * tc_fltr)880143b86f3SAmritha Nambiar ice_tc_forward_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *tc_fltr)
881143b86f3SAmritha Nambiar {
882143b86f3SAmritha Nambiar 	struct ice_rx_ring *ring = NULL;
883390889a4SAmritha Nambiar 	struct ice_vsi *dest_vsi = NULL;
884143b86f3SAmritha Nambiar 	struct ice_pf *pf = vsi->back;
885143b86f3SAmritha Nambiar 	struct device *dev;
886143b86f3SAmritha Nambiar 	u32 tc_class;
887479cdfe3SSridhar Samudrala 	int q;
888143b86f3SAmritha Nambiar 
889143b86f3SAmritha Nambiar 	dev = ice_pf_to_dev(pf);
890143b86f3SAmritha Nambiar 
891143b86f3SAmritha Nambiar 	/* Get the destination VSI and/or destination queue and validate them */
892143b86f3SAmritha Nambiar 	switch (tc_fltr->action.fltr_act) {
893143b86f3SAmritha Nambiar 	case ICE_FWD_TO_VSI:
894143b86f3SAmritha Nambiar 		tc_class = tc_fltr->action.fwd.tc.tc_class;
895143b86f3SAmritha Nambiar 		/* Select the destination VSI */
896143b86f3SAmritha Nambiar 		if (tc_class < ICE_CHNL_START_TC) {
897143b86f3SAmritha Nambiar 			NL_SET_ERR_MSG_MOD(tc_fltr->extack,
898143b86f3SAmritha Nambiar 					   "Unable to add filter because of unsupported destination");
899143b86f3SAmritha Nambiar 			return ERR_PTR(-EOPNOTSUPP);
900143b86f3SAmritha Nambiar 		}
901143b86f3SAmritha Nambiar 		/* Locate ADQ VSI depending on hw_tc number */
902390889a4SAmritha Nambiar 		dest_vsi = vsi->tc_map_vsi[tc_class];
903143b86f3SAmritha Nambiar 		break;
904143b86f3SAmritha Nambiar 	case ICE_FWD_TO_Q:
905143b86f3SAmritha Nambiar 		/* Locate the Rx queue */
906143b86f3SAmritha Nambiar 		ring = ice_locate_rx_ring_using_queue(vsi, tc_fltr);
907143b86f3SAmritha Nambiar 		if (!ring) {
908143b86f3SAmritha Nambiar 			dev_err(dev,
909143b86f3SAmritha Nambiar 				"Unable to locate Rx queue for action fwd_to_queue: %u\n",
910143b86f3SAmritha Nambiar 				tc_fltr->action.fwd.q.queue);
911143b86f3SAmritha Nambiar 			return ERR_PTR(-EINVAL);
912143b86f3SAmritha Nambiar 		}
913143b86f3SAmritha Nambiar 		/* Determine destination VSI even though the action is
914143b86f3SAmritha Nambiar 		 * FWD_TO_QUEUE, because QUEUE is associated with VSI
915143b86f3SAmritha Nambiar 		 */
916479cdfe3SSridhar Samudrala 		q = tc_fltr->action.fwd.q.queue;
917479cdfe3SSridhar Samudrala 		dest_vsi = ice_locate_vsi_using_queue(vsi, q);
918143b86f3SAmritha Nambiar 		break;
919143b86f3SAmritha Nambiar 	default:
920143b86f3SAmritha Nambiar 		dev_err(dev,
921143b86f3SAmritha Nambiar 			"Unable to add filter because of unsupported action %u (supported actions: fwd to tc, fwd to queue)\n",
922143b86f3SAmritha Nambiar 			tc_fltr->action.fltr_act);
923143b86f3SAmritha Nambiar 		return ERR_PTR(-EINVAL);
924143b86f3SAmritha Nambiar 	}
925390889a4SAmritha Nambiar 	/* Must have valid dest_vsi (it could be main VSI or ADQ VSI) */
926390889a4SAmritha Nambiar 	if (!dest_vsi) {
927143b86f3SAmritha Nambiar 		dev_err(dev,
928143b86f3SAmritha Nambiar 			"Unable to add filter because specified destination VSI doesn't exist\n");
929143b86f3SAmritha Nambiar 		return ERR_PTR(-EINVAL);
930143b86f3SAmritha Nambiar 	}
931390889a4SAmritha Nambiar 	return dest_vsi;
932143b86f3SAmritha Nambiar }
933143b86f3SAmritha Nambiar 
934143b86f3SAmritha Nambiar /**
9359fea7498SKiran Patil  * ice_add_tc_flower_adv_fltr - add appropriate filter rules
9369fea7498SKiran Patil  * @vsi: Pointer to VSI
9379fea7498SKiran Patil  * @tc_fltr: Pointer to TC flower filter structure
9389fea7498SKiran Patil  *
9399fea7498SKiran Patil  * based on filter parameters using Advance recipes supported
9409fea7498SKiran Patil  * by OS package.
9419fea7498SKiran Patil  */
9429fea7498SKiran Patil static int
ice_add_tc_flower_adv_fltr(struct ice_vsi * vsi,struct ice_tc_flower_fltr * tc_fltr)9439fea7498SKiran Patil ice_add_tc_flower_adv_fltr(struct ice_vsi *vsi,
9449fea7498SKiran Patil 			   struct ice_tc_flower_fltr *tc_fltr)
9459fea7498SKiran Patil {
9469fea7498SKiran Patil 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &tc_fltr->outer_headers;
9479fea7498SKiran Patil 	struct ice_adv_rule_info rule_info = {0};
9489fea7498SKiran Patil 	struct ice_rule_query_data rule_added;
9499fea7498SKiran Patil 	struct ice_adv_lkup_elem *list;
9509fea7498SKiran Patil 	struct ice_pf *pf = vsi->back;
9519fea7498SKiran Patil 	struct ice_hw *hw = &pf->hw;
9529fea7498SKiran Patil 	u32 flags = tc_fltr->flags;
953390889a4SAmritha Nambiar 	struct ice_vsi *dest_vsi;
9549fea7498SKiran Patil 	struct device *dev;
9559fea7498SKiran Patil 	u16 lkups_cnt = 0;
9569fea7498SKiran Patil 	u16 l4_proto = 0;
9579fea7498SKiran Patil 	int ret = 0;
9589fea7498SKiran Patil 	u16 i = 0;
9599fea7498SKiran Patil 
9609fea7498SKiran Patil 	dev = ice_pf_to_dev(pf);
9619fea7498SKiran Patil 	if (ice_is_safe_mode(pf)) {
9629fea7498SKiran Patil 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unable to add filter because driver is in safe mode");
9639fea7498SKiran Patil 		return -EOPNOTSUPP;
9649fea7498SKiran Patil 	}
9659fea7498SKiran Patil 
9669fea7498SKiran Patil 	if (!flags || (flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV4 |
9679fea7498SKiran Patil 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV4 |
9689fea7498SKiran Patil 				ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
9699fea7498SKiran Patil 				ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
9709fea7498SKiran Patil 				ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT))) {
9719fea7498SKiran Patil 		NL_SET_ERR_MSG_MOD(tc_fltr->extack, "Unsupported encap field(s)");
9729fea7498SKiran Patil 		return -EOPNOTSUPP;
9739fea7498SKiran Patil 	}
9749fea7498SKiran Patil 
975143b86f3SAmritha Nambiar 	/* validate forwarding action VSI and queue */
976390889a4SAmritha Nambiar 	if (ice_is_forward_action(tc_fltr->action.fltr_act)) {
977390889a4SAmritha Nambiar 		dest_vsi = ice_tc_forward_action(vsi, tc_fltr);
978390889a4SAmritha Nambiar 		if (IS_ERR(dest_vsi))
979390889a4SAmritha Nambiar 			return PTR_ERR(dest_vsi);
980390889a4SAmritha Nambiar 	}
9819fea7498SKiran Patil 
9829fea7498SKiran Patil 	lkups_cnt = ice_tc_count_lkups(flags, headers, tc_fltr);
9839fea7498SKiran Patil 	list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
9849fea7498SKiran Patil 	if (!list)
9859fea7498SKiran Patil 		return -ENOMEM;
9869fea7498SKiran Patil 
9879fea7498SKiran Patil 	i = ice_tc_fill_rules(hw, flags, tc_fltr, list, &rule_info, &l4_proto);
9889fea7498SKiran Patil 	if (i != lkups_cnt) {
9899fea7498SKiran Patil 		ret = -EINVAL;
9909fea7498SKiran Patil 		goto exit;
9919fea7498SKiran Patil 	}
9929fea7498SKiran Patil 
9939fea7498SKiran Patil 	rule_info.sw_act.fltr_act = tc_fltr->action.fltr_act;
994143b86f3SAmritha Nambiar 	/* specify the cookie as filter_rule_id */
995143b86f3SAmritha Nambiar 	rule_info.fltr_rule_id = tc_fltr->cookie;
9969fea7498SKiran Patil 
997143b86f3SAmritha Nambiar 	switch (tc_fltr->action.fltr_act) {
998143b86f3SAmritha Nambiar 	case ICE_FWD_TO_VSI:
999390889a4SAmritha Nambiar 		rule_info.sw_act.vsi_handle = dest_vsi->idx;
1000143b86f3SAmritha Nambiar 		rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
10019fea7498SKiran Patil 		rule_info.sw_act.src = hw->pf_id;
10029fea7498SKiran Patil 		dev_dbg(dev, "add switch rule for TC:%u vsi_idx:%u, lkups_cnt:%u\n",
1003143b86f3SAmritha Nambiar 			tc_fltr->action.fwd.tc.tc_class,
10049fea7498SKiran Patil 			rule_info.sw_act.vsi_handle, lkups_cnt);
1005143b86f3SAmritha Nambiar 		break;
1006143b86f3SAmritha Nambiar 	case ICE_FWD_TO_Q:
1007143b86f3SAmritha Nambiar 		/* HW queue number in global space */
1008143b86f3SAmritha Nambiar 		rule_info.sw_act.fwd_id.q_id = tc_fltr->action.fwd.q.hw_queue;
1009390889a4SAmritha Nambiar 		rule_info.sw_act.vsi_handle = dest_vsi->idx;
1010143b86f3SAmritha Nambiar 		rule_info.priority = ICE_SWITCH_FLTR_PRIO_QUEUE;
1011143b86f3SAmritha Nambiar 		rule_info.sw_act.src = hw->pf_id;
1012143b86f3SAmritha Nambiar 		dev_dbg(dev, "add switch rule action to forward to queue:%u (HW queue %u), lkups_cnt:%u\n",
1013143b86f3SAmritha Nambiar 			tc_fltr->action.fwd.q.queue,
1014143b86f3SAmritha Nambiar 			tc_fltr->action.fwd.q.hw_queue, lkups_cnt);
1015143b86f3SAmritha Nambiar 		break;
1016390889a4SAmritha Nambiar 	case ICE_DROP_PACKET:
1017390889a4SAmritha Nambiar 		rule_info.sw_act.flag |= ICE_FLTR_RX;
1018390889a4SAmritha Nambiar 		rule_info.sw_act.src = hw->pf_id;
1019143b86f3SAmritha Nambiar 		rule_info.priority = ICE_SWITCH_FLTR_PRIO_VSI;
1020143b86f3SAmritha Nambiar 		break;
1021390889a4SAmritha Nambiar 	default:
1022390889a4SAmritha Nambiar 		ret = -EOPNOTSUPP;
1023390889a4SAmritha Nambiar 		goto exit;
10249fea7498SKiran Patil 	}
10259fea7498SKiran Patil 
10269fea7498SKiran Patil 	ret = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, &rule_added);
10279fea7498SKiran Patil 	if (ret == -EEXIST) {
10289fea7498SKiran Patil 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
10299fea7498SKiran Patil 				   "Unable to add filter because it already exist");
10309fea7498SKiran Patil 		ret = -EINVAL;
10319fea7498SKiran Patil 		goto exit;
10329fea7498SKiran Patil 	} else if (ret) {
10339fea7498SKiran Patil 		NL_SET_ERR_MSG_MOD(tc_fltr->extack,
10349fea7498SKiran Patil 				   "Unable to add filter due to error");
10359fea7498SKiran Patil 		goto exit;
10369fea7498SKiran Patil 	}
10379fea7498SKiran Patil 
10389fea7498SKiran Patil 	/* store the output params, which are needed later for removing
10399fea7498SKiran Patil 	 * advanced switch filter
10409fea7498SKiran Patil 	 */
10419fea7498SKiran Patil 	tc_fltr->rid = rule_added.rid;
10429fea7498SKiran Patil 	tc_fltr->rule_id = rule_added.rule_id;
1043143b86f3SAmritha Nambiar 	tc_fltr->dest_vsi_handle = rule_added.vsi_handle;
1044143b86f3SAmritha Nambiar 	if (tc_fltr->action.fltr_act == ICE_FWD_TO_VSI ||
1045143b86f3SAmritha Nambiar 	    tc_fltr->action.fltr_act == ICE_FWD_TO_Q) {
1046390889a4SAmritha Nambiar 		tc_fltr->dest_vsi = dest_vsi;
10479fea7498SKiran Patil 		/* keep track of advanced switch filter for
1048143b86f3SAmritha Nambiar 		 * destination VSI
10499fea7498SKiran Patil 		 */
1050390889a4SAmritha Nambiar 		dest_vsi->num_chnl_fltr++;
10519fea7498SKiran Patil 
10529fea7498SKiran Patil 		/* keeps track of channel filters for PF VSI */
10539fea7498SKiran Patil 		if (vsi->type == ICE_VSI_PF &&
10549fea7498SKiran Patil 		    (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
10559fea7498SKiran Patil 			      ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
10569fea7498SKiran Patil 			pf->num_dmac_chnl_fltrs++;
10579fea7498SKiran Patil 	}
1058143b86f3SAmritha Nambiar 	switch (tc_fltr->action.fltr_act) {
1059143b86f3SAmritha Nambiar 	case ICE_FWD_TO_VSI:
1060143b86f3SAmritha Nambiar 		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",
10619fea7498SKiran Patil 			lkups_cnt, flags,
1062143b86f3SAmritha Nambiar 			tc_fltr->action.fwd.tc.tc_class, rule_added.rid,
10639fea7498SKiran Patil 			rule_added.rule_id, rule_added.vsi_handle);
1064143b86f3SAmritha Nambiar 		break;
1065143b86f3SAmritha Nambiar 	case ICE_FWD_TO_Q:
1066143b86f3SAmritha Nambiar 		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",
1067143b86f3SAmritha Nambiar 			lkups_cnt, flags, tc_fltr->action.fwd.q.queue,
1068143b86f3SAmritha Nambiar 			tc_fltr->action.fwd.q.hw_queue, rule_added.rid,
1069143b86f3SAmritha Nambiar 			rule_added.rule_id);
1070143b86f3SAmritha Nambiar 		break;
1071390889a4SAmritha Nambiar 	case ICE_DROP_PACKET:
1072390889a4SAmritha Nambiar 		dev_dbg(dev, "added switch rule (lkups_cnt %u, flags 0x%x), action is drop, rid %u, rule_id %u\n",
1073390889a4SAmritha Nambiar 			lkups_cnt, flags, rule_added.rid, rule_added.rule_id);
1074390889a4SAmritha Nambiar 		break;
1075143b86f3SAmritha Nambiar 	default:
1076143b86f3SAmritha Nambiar 		break;
1077143b86f3SAmritha Nambiar 	}
10789fea7498SKiran Patil exit:
10799fea7498SKiran Patil 	kfree(list);
10809fea7498SKiran Patil 	return ret;
10819fea7498SKiran Patil }
10829fea7498SKiran Patil 
10839fea7498SKiran Patil /**
1084cd8efeeeSMarcin Szycik  * ice_tc_set_pppoe - Parse PPPoE fields from TC flower filter
1085cd8efeeeSMarcin Szycik  * @match: Pointer to flow match structure
1086cd8efeeeSMarcin Szycik  * @fltr: Pointer to filter structure
1087cd8efeeeSMarcin Szycik  * @headers: Pointer to outer header fields
1088cd8efeeeSMarcin Szycik  * @returns PPP protocol used in filter (ppp_ses or ppp_disc)
1089cd8efeeeSMarcin Szycik  */
1090cd8efeeeSMarcin Szycik static u16
ice_tc_set_pppoe(struct flow_match_pppoe * match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers)1091cd8efeeeSMarcin Szycik ice_tc_set_pppoe(struct flow_match_pppoe *match,
1092cd8efeeeSMarcin Szycik 		 struct ice_tc_flower_fltr *fltr,
1093cd8efeeeSMarcin Szycik 		 struct ice_tc_flower_lyr_2_4_hdrs *headers)
1094cd8efeeeSMarcin Szycik {
1095cd8efeeeSMarcin Szycik 	if (match->mask->session_id) {
1096cd8efeeeSMarcin Szycik 		fltr->flags |= ICE_TC_FLWR_FIELD_PPPOE_SESSID;
1097cd8efeeeSMarcin Szycik 		headers->pppoe_hdr.session_id = match->key->session_id;
1098cd8efeeeSMarcin Szycik 	}
1099cd8efeeeSMarcin Szycik 
1100cd8efeeeSMarcin Szycik 	if (match->mask->ppp_proto) {
1101cd8efeeeSMarcin Szycik 		fltr->flags |= ICE_TC_FLWR_FIELD_PPP_PROTO;
1102cd8efeeeSMarcin Szycik 		headers->pppoe_hdr.ppp_proto = match->key->ppp_proto;
1103cd8efeeeSMarcin Szycik 	}
1104cd8efeeeSMarcin Szycik 
1105cd8efeeeSMarcin Szycik 	return be16_to_cpu(match->key->type);
1106cd8efeeeSMarcin Szycik }
1107cd8efeeeSMarcin Szycik 
1108cd8efeeeSMarcin Szycik /**
11090d08a441SKiran Patil  * ice_tc_set_ipv4 - Parse IPv4 addresses from TC flower filter
11100d08a441SKiran Patil  * @match: Pointer to flow match structure
11110d08a441SKiran Patil  * @fltr: Pointer to filter structure
11120d08a441SKiran Patil  * @headers: inner or outer header fields
11139e300987SMichal Swiatkowski  * @is_encap: set true for tunnel IPv4 address
11140d08a441SKiran Patil  */
11150d08a441SKiran Patil static int
ice_tc_set_ipv4(struct flow_match_ipv4_addrs * match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers,bool is_encap)11160d08a441SKiran Patil ice_tc_set_ipv4(struct flow_match_ipv4_addrs *match,
11170d08a441SKiran Patil 		struct ice_tc_flower_fltr *fltr,
11189e300987SMichal Swiatkowski 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
11190d08a441SKiran Patil {
11200d08a441SKiran Patil 	if (match->key->dst) {
11219e300987SMichal Swiatkowski 		if (is_encap)
11229e300987SMichal Swiatkowski 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV4;
11239e300987SMichal Swiatkowski 		else
11240d08a441SKiran Patil 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV4;
11250d08a441SKiran Patil 		headers->l3_key.dst_ipv4 = match->key->dst;
11260d08a441SKiran Patil 		headers->l3_mask.dst_ipv4 = match->mask->dst;
11270d08a441SKiran Patil 	}
11280d08a441SKiran Patil 	if (match->key->src) {
11299e300987SMichal Swiatkowski 		if (is_encap)
11309e300987SMichal Swiatkowski 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV4;
11319e300987SMichal Swiatkowski 		else
11320d08a441SKiran Patil 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV4;
11330d08a441SKiran Patil 		headers->l3_key.src_ipv4 = match->key->src;
11340d08a441SKiran Patil 		headers->l3_mask.src_ipv4 = match->mask->src;
11350d08a441SKiran Patil 	}
11360d08a441SKiran Patil 	return 0;
11370d08a441SKiran Patil }
11380d08a441SKiran Patil 
11390d08a441SKiran Patil /**
11400d08a441SKiran Patil  * ice_tc_set_ipv6 - Parse IPv6 addresses from TC flower filter
11410d08a441SKiran Patil  * @match: Pointer to flow match structure
11420d08a441SKiran Patil  * @fltr: Pointer to filter structure
11430d08a441SKiran Patil  * @headers: inner or outer header fields
11449e300987SMichal Swiatkowski  * @is_encap: set true for tunnel IPv6 address
11450d08a441SKiran Patil  */
11460d08a441SKiran Patil static int
ice_tc_set_ipv6(struct flow_match_ipv6_addrs * match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers,bool is_encap)11470d08a441SKiran Patil ice_tc_set_ipv6(struct flow_match_ipv6_addrs *match,
11480d08a441SKiran Patil 		struct ice_tc_flower_fltr *fltr,
11499e300987SMichal Swiatkowski 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
11500d08a441SKiran Patil {
11510d08a441SKiran Patil 	struct ice_tc_l3_hdr *l3_key, *l3_mask;
11520d08a441SKiran Patil 
11530d08a441SKiran Patil 	/* src and dest IPV6 address should not be LOOPBACK
11540d08a441SKiran Patil 	 * (0:0:0:0:0:0:0:1), which can be represented as ::1
11550d08a441SKiran Patil 	 */
11560d08a441SKiran Patil 	if (ipv6_addr_loopback(&match->key->dst) ||
11570d08a441SKiran Patil 	    ipv6_addr_loopback(&match->key->src)) {
11580d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad IPv6, addr is LOOPBACK");
11590d08a441SKiran Patil 		return -EINVAL;
11600d08a441SKiran Patil 	}
11610d08a441SKiran Patil 	/* if src/dest IPv6 address is *,* error */
11620d08a441SKiran Patil 	if (ipv6_addr_any(&match->mask->dst) &&
11630d08a441SKiran Patil 	    ipv6_addr_any(&match->mask->src)) {
11640d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(fltr->extack, "Bad src/dest IPv6, addr is any");
11650d08a441SKiran Patil 		return -EINVAL;
11660d08a441SKiran Patil 	}
11679e300987SMichal Swiatkowski 	if (!ipv6_addr_any(&match->mask->dst)) {
11689e300987SMichal Swiatkowski 		if (is_encap)
11699e300987SMichal Swiatkowski 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_IPV6;
11709e300987SMichal Swiatkowski 		else
11710d08a441SKiran Patil 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_IPV6;
11729e300987SMichal Swiatkowski 	}
11739e300987SMichal Swiatkowski 	if (!ipv6_addr_any(&match->mask->src)) {
11749e300987SMichal Swiatkowski 		if (is_encap)
11759e300987SMichal Swiatkowski 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_IPV6;
11769e300987SMichal Swiatkowski 		else
11770d08a441SKiran Patil 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_IPV6;
11789e300987SMichal Swiatkowski 	}
11790d08a441SKiran Patil 
11800d08a441SKiran Patil 	l3_key = &headers->l3_key;
11810d08a441SKiran Patil 	l3_mask = &headers->l3_mask;
11820d08a441SKiran Patil 
11839e300987SMichal Swiatkowski 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_SRC_IPV6 |
11849e300987SMichal Swiatkowski 			   ICE_TC_FLWR_FIELD_SRC_IPV6)) {
11850d08a441SKiran Patil 		memcpy(&l3_key->src_ipv6_addr, &match->key->src.s6_addr,
11860d08a441SKiran Patil 		       sizeof(match->key->src.s6_addr));
11870d08a441SKiran Patil 		memcpy(&l3_mask->src_ipv6_addr, &match->mask->src.s6_addr,
11880d08a441SKiran Patil 		       sizeof(match->mask->src.s6_addr));
11890d08a441SKiran Patil 	}
11909e300987SMichal Swiatkowski 	if (fltr->flags & (ICE_TC_FLWR_FIELD_ENC_DEST_IPV6 |
11919e300987SMichal Swiatkowski 			   ICE_TC_FLWR_FIELD_DEST_IPV6)) {
11920d08a441SKiran Patil 		memcpy(&l3_key->dst_ipv6_addr, &match->key->dst.s6_addr,
11930d08a441SKiran Patil 		       sizeof(match->key->dst.s6_addr));
11940d08a441SKiran Patil 		memcpy(&l3_mask->dst_ipv6_addr, &match->mask->dst.s6_addr,
11950d08a441SKiran Patil 		       sizeof(match->mask->dst.s6_addr));
11960d08a441SKiran Patil 	}
11970d08a441SKiran Patil 
11980d08a441SKiran Patil 	return 0;
11990d08a441SKiran Patil }
12000d08a441SKiran Patil 
12010d08a441SKiran Patil /**
12024c99bc96SMarcin Szycik  * ice_tc_set_tos_ttl - Parse IP ToS/TTL from TC flower filter
12034c99bc96SMarcin Szycik  * @match: Pointer to flow match structure
12044c99bc96SMarcin Szycik  * @fltr: Pointer to filter structure
12054c99bc96SMarcin Szycik  * @headers: inner or outer header fields
12064c99bc96SMarcin Szycik  * @is_encap: set true for tunnel
12074c99bc96SMarcin Szycik  */
12084c99bc96SMarcin Szycik static void
ice_tc_set_tos_ttl(struct flow_match_ip * match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers,bool is_encap)12094c99bc96SMarcin Szycik ice_tc_set_tos_ttl(struct flow_match_ip *match,
12104c99bc96SMarcin Szycik 		   struct ice_tc_flower_fltr *fltr,
12114c99bc96SMarcin Szycik 		   struct ice_tc_flower_lyr_2_4_hdrs *headers,
12124c99bc96SMarcin Szycik 		   bool is_encap)
12134c99bc96SMarcin Szycik {
12144c99bc96SMarcin Szycik 	if (match->mask->tos) {
12154c99bc96SMarcin Szycik 		if (is_encap)
12164c99bc96SMarcin Szycik 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TOS;
12174c99bc96SMarcin Szycik 		else
12184c99bc96SMarcin Szycik 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TOS;
12194c99bc96SMarcin Szycik 
12204c99bc96SMarcin Szycik 		headers->l3_key.tos = match->key->tos;
12214c99bc96SMarcin Szycik 		headers->l3_mask.tos = match->mask->tos;
12224c99bc96SMarcin Szycik 	}
12234c99bc96SMarcin Szycik 
12244c99bc96SMarcin Szycik 	if (match->mask->ttl) {
12254c99bc96SMarcin Szycik 		if (is_encap)
12264c99bc96SMarcin Szycik 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_IP_TTL;
12274c99bc96SMarcin Szycik 		else
12284c99bc96SMarcin Szycik 			fltr->flags |= ICE_TC_FLWR_FIELD_IP_TTL;
12294c99bc96SMarcin Szycik 
12304c99bc96SMarcin Szycik 		headers->l3_key.ttl = match->key->ttl;
12314c99bc96SMarcin Szycik 		headers->l3_mask.ttl = match->mask->ttl;
12324c99bc96SMarcin Szycik 	}
12334c99bc96SMarcin Szycik }
12344c99bc96SMarcin Szycik 
12354c99bc96SMarcin Szycik /**
12360d08a441SKiran Patil  * ice_tc_set_port - Parse ports from TC flower filter
12370d08a441SKiran Patil  * @match: Flow match structure
12380d08a441SKiran Patil  * @fltr: Pointer to filter structure
12390d08a441SKiran Patil  * @headers: inner or outer header fields
12409e300987SMichal Swiatkowski  * @is_encap: set true for tunnel port
12410d08a441SKiran Patil  */
12420d08a441SKiran Patil static int
ice_tc_set_port(struct flow_match_ports match,struct ice_tc_flower_fltr * fltr,struct ice_tc_flower_lyr_2_4_hdrs * headers,bool is_encap)12430d08a441SKiran Patil ice_tc_set_port(struct flow_match_ports match,
12440d08a441SKiran Patil 		struct ice_tc_flower_fltr *fltr,
12459e300987SMichal Swiatkowski 		struct ice_tc_flower_lyr_2_4_hdrs *headers, bool is_encap)
12460d08a441SKiran Patil {
12470d08a441SKiran Patil 	if (match.key->dst) {
12489e300987SMichal Swiatkowski 		if (is_encap)
12499e300987SMichal Swiatkowski 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DEST_L4_PORT;
12509e300987SMichal Swiatkowski 		else
12519e300987SMichal Swiatkowski 			fltr->flags |= ICE_TC_FLWR_FIELD_DEST_L4_PORT;
1252932645c2SMichal Swiatkowski 
12530d08a441SKiran Patil 		headers->l4_key.dst_port = match.key->dst;
12540d08a441SKiran Patil 		headers->l4_mask.dst_port = match.mask->dst;
12550d08a441SKiran Patil 	}
12560d08a441SKiran Patil 	if (match.key->src) {
12579e300987SMichal Swiatkowski 		if (is_encap)
12589e300987SMichal Swiatkowski 			fltr->flags |= ICE_TC_FLWR_FIELD_ENC_SRC_L4_PORT;
12599e300987SMichal Swiatkowski 		else
12609e300987SMichal Swiatkowski 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_L4_PORT;
1261932645c2SMichal Swiatkowski 
12620d08a441SKiran Patil 		headers->l4_key.src_port = match.key->src;
12630d08a441SKiran Patil 		headers->l4_mask.src_port = match.mask->src;
12640d08a441SKiran Patil 	}
12650d08a441SKiran Patil 	return 0;
12660d08a441SKiran Patil }
12670d08a441SKiran Patil 
12689e300987SMichal Swiatkowski static struct net_device *
ice_get_tunnel_device(struct net_device * dev,struct flow_rule * rule)12699e300987SMichal Swiatkowski ice_get_tunnel_device(struct net_device *dev, struct flow_rule *rule)
12709e300987SMichal Swiatkowski {
12719e300987SMichal Swiatkowski 	struct flow_action_entry *act;
12729e300987SMichal Swiatkowski 	int i;
12739e300987SMichal Swiatkowski 
12749e300987SMichal Swiatkowski 	if (ice_is_tunnel_supported(dev))
12759e300987SMichal Swiatkowski 		return dev;
12769e300987SMichal Swiatkowski 
12779e300987SMichal Swiatkowski 	flow_action_for_each(i, act, &rule->action) {
12789e300987SMichal Swiatkowski 		if (act->id == FLOW_ACTION_REDIRECT &&
12799e300987SMichal Swiatkowski 		    ice_is_tunnel_supported(act->dev))
12809e300987SMichal Swiatkowski 			return act->dev;
12819e300987SMichal Swiatkowski 	}
12829e300987SMichal Swiatkowski 
12839e300987SMichal Swiatkowski 	return NULL;
12849e300987SMichal Swiatkowski }
12859e300987SMichal Swiatkowski 
12869a225f81SMarcin Szycik /**
12879a225f81SMarcin Szycik  * ice_parse_gtp_type - Sets GTP tunnel type to GTP-U or GTP-C
12889a225f81SMarcin Szycik  * @match: Flow match structure
12899a225f81SMarcin Szycik  * @fltr: Pointer to filter structure
12909a225f81SMarcin Szycik  *
12919a225f81SMarcin Szycik  * GTP-C/GTP-U is selected based on destination port number (enc_dst_port).
12929a225f81SMarcin Szycik  * Before calling this funtcion, fltr->tunnel_type should be set to TNL_GTPU,
12939a225f81SMarcin Szycik  * therefore making GTP-U the default choice (when destination port number is
12949a225f81SMarcin Szycik  * not specified).
12959a225f81SMarcin Szycik  */
12969a225f81SMarcin Szycik static int
ice_parse_gtp_type(struct flow_match_ports match,struct ice_tc_flower_fltr * fltr)12979a225f81SMarcin Szycik ice_parse_gtp_type(struct flow_match_ports match,
12989a225f81SMarcin Szycik 		   struct ice_tc_flower_fltr *fltr)
12999a225f81SMarcin Szycik {
13009a225f81SMarcin Szycik 	u16 dst_port;
13019a225f81SMarcin Szycik 
13029a225f81SMarcin Szycik 	if (match.key->dst) {
13039a225f81SMarcin Szycik 		dst_port = be16_to_cpu(match.key->dst);
13049a225f81SMarcin Szycik 
13059a225f81SMarcin Szycik 		switch (dst_port) {
13069a225f81SMarcin Szycik 		case 2152:
13079a225f81SMarcin Szycik 			break;
13089a225f81SMarcin Szycik 		case 2123:
13099a225f81SMarcin Szycik 			fltr->tunnel_type = TNL_GTPC;
13109a225f81SMarcin Szycik 			break;
13119a225f81SMarcin Szycik 		default:
13129a225f81SMarcin Szycik 			NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported GTP port number");
13139a225f81SMarcin Szycik 			return -EINVAL;
13149a225f81SMarcin Szycik 		}
13159a225f81SMarcin Szycik 	}
13169a225f81SMarcin Szycik 
13179a225f81SMarcin Szycik 	return 0;
13189a225f81SMarcin Szycik }
13199a225f81SMarcin Szycik 
13209e300987SMichal Swiatkowski static int
ice_parse_tunnel_attr(struct net_device * dev,struct flow_rule * rule,struct ice_tc_flower_fltr * fltr)13219e300987SMichal Swiatkowski ice_parse_tunnel_attr(struct net_device *dev, struct flow_rule *rule,
13229e300987SMichal Swiatkowski 		      struct ice_tc_flower_fltr *fltr)
13239e300987SMichal Swiatkowski {
13249e300987SMichal Swiatkowski 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
13259e300987SMichal Swiatkowski 	struct flow_match_control enc_control;
13269e300987SMichal Swiatkowski 
13279e300987SMichal Swiatkowski 	fltr->tunnel_type = ice_tc_tun_get_type(dev);
13289e300987SMichal Swiatkowski 	headers->l3_key.ip_proto = IPPROTO_UDP;
13299e300987SMichal Swiatkowski 
13309e300987SMichal Swiatkowski 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
13319e300987SMichal Swiatkowski 		struct flow_match_enc_keyid enc_keyid;
13329e300987SMichal Swiatkowski 
13339e300987SMichal Swiatkowski 		flow_rule_match_enc_keyid(rule, &enc_keyid);
13349e300987SMichal Swiatkowski 
13359e300987SMichal Swiatkowski 		if (!enc_keyid.mask->keyid ||
13369e300987SMichal Swiatkowski 		    enc_keyid.mask->keyid != cpu_to_be32(ICE_TC_FLOWER_MASK_32))
13379e300987SMichal Swiatkowski 			return -EINVAL;
13389e300987SMichal Swiatkowski 
13399e300987SMichal Swiatkowski 		fltr->flags |= ICE_TC_FLWR_FIELD_TENANT_ID;
13409e300987SMichal Swiatkowski 		fltr->tenant_id = enc_keyid.key->keyid;
13419e300987SMichal Swiatkowski 	}
13429e300987SMichal Swiatkowski 
13439e300987SMichal Swiatkowski 	flow_rule_match_enc_control(rule, &enc_control);
13449e300987SMichal Swiatkowski 
13459e300987SMichal Swiatkowski 	if (enc_control.key->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
13469e300987SMichal Swiatkowski 		struct flow_match_ipv4_addrs match;
13479e300987SMichal Swiatkowski 
13489e300987SMichal Swiatkowski 		flow_rule_match_enc_ipv4_addrs(rule, &match);
13499e300987SMichal Swiatkowski 		if (ice_tc_set_ipv4(&match, fltr, headers, true))
13509e300987SMichal Swiatkowski 			return -EINVAL;
13519e300987SMichal Swiatkowski 	} else if (enc_control.key->addr_type ==
13529e300987SMichal Swiatkowski 					FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
13539e300987SMichal Swiatkowski 		struct flow_match_ipv6_addrs match;
13549e300987SMichal Swiatkowski 
13559e300987SMichal Swiatkowski 		flow_rule_match_enc_ipv6_addrs(rule, &match);
13569e300987SMichal Swiatkowski 		if (ice_tc_set_ipv6(&match, fltr, headers, true))
13579e300987SMichal Swiatkowski 			return -EINVAL;
13589e300987SMichal Swiatkowski 	}
13599e300987SMichal Swiatkowski 
13609e300987SMichal Swiatkowski 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
13619e300987SMichal Swiatkowski 		struct flow_match_ip match;
13629e300987SMichal Swiatkowski 
13639e300987SMichal Swiatkowski 		flow_rule_match_enc_ip(rule, &match);
13644c99bc96SMarcin Szycik 		ice_tc_set_tos_ttl(&match, fltr, headers, true);
13659e300987SMichal Swiatkowski 	}
13669e300987SMichal Swiatkowski 
1367de6acd1cSMichal Swiatkowski 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) &&
1368de6acd1cSMichal Swiatkowski 	    fltr->tunnel_type != TNL_VXLAN && fltr->tunnel_type != TNL_GENEVE) {
13699e300987SMichal Swiatkowski 		struct flow_match_ports match;
13709e300987SMichal Swiatkowski 
13719e300987SMichal Swiatkowski 		flow_rule_match_enc_ports(rule, &match);
13729a225f81SMarcin Szycik 
13739a225f81SMarcin Szycik 		if (fltr->tunnel_type != TNL_GTPU) {
13749e300987SMichal Swiatkowski 			if (ice_tc_set_port(match, fltr, headers, true))
13759e300987SMichal Swiatkowski 				return -EINVAL;
13769a225f81SMarcin Szycik 		} else {
13779a225f81SMarcin Szycik 			if (ice_parse_gtp_type(match, fltr))
13789a225f81SMarcin Szycik 				return -EINVAL;
13799a225f81SMarcin Szycik 		}
13809a225f81SMarcin Szycik 	}
13819a225f81SMarcin Szycik 
13829a225f81SMarcin Szycik 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_OPTS)) {
13839a225f81SMarcin Szycik 		struct flow_match_enc_opts match;
13849a225f81SMarcin Szycik 
13859a225f81SMarcin Szycik 		flow_rule_match_enc_opts(rule, &match);
13869a225f81SMarcin Szycik 
13879a225f81SMarcin Szycik 		memcpy(&fltr->gtp_pdu_info_keys, &match.key->data[0],
13889a225f81SMarcin Szycik 		       sizeof(struct gtp_pdu_session_info));
13899a225f81SMarcin Szycik 
13909a225f81SMarcin Szycik 		memcpy(&fltr->gtp_pdu_info_masks, &match.mask->data[0],
13919a225f81SMarcin Szycik 		       sizeof(struct gtp_pdu_session_info));
13929a225f81SMarcin Szycik 
13939a225f81SMarcin Szycik 		fltr->flags |= ICE_TC_FLWR_FIELD_ENC_OPTS;
13949e300987SMichal Swiatkowski 	}
13959e300987SMichal Swiatkowski 
13969e300987SMichal Swiatkowski 	return 0;
13979e300987SMichal Swiatkowski }
13989e300987SMichal Swiatkowski 
13990d08a441SKiran Patil /**
14000d08a441SKiran Patil  * ice_parse_cls_flower - Parse TC flower filters provided by kernel
14010d08a441SKiran Patil  * @vsi: Pointer to the VSI
14020d08a441SKiran Patil  * @filter_dev: Pointer to device on which filter is being added
14030d08a441SKiran Patil  * @f: Pointer to struct flow_cls_offload
14040d08a441SKiran Patil  * @fltr: Pointer to filter structure
14050d08a441SKiran Patil  */
14060d08a441SKiran Patil static int
ice_parse_cls_flower(struct net_device * filter_dev,struct ice_vsi * vsi,struct flow_cls_offload * f,struct ice_tc_flower_fltr * fltr)14070d08a441SKiran Patil ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
14080d08a441SKiran Patil 		     struct flow_cls_offload *f,
14090d08a441SKiran Patil 		     struct ice_tc_flower_fltr *fltr)
14100d08a441SKiran Patil {
14110d08a441SKiran Patil 	struct ice_tc_flower_lyr_2_4_hdrs *headers = &fltr->outer_headers;
14120d08a441SKiran Patil 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
14130d08a441SKiran Patil 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
14140d08a441SKiran Patil 	struct flow_dissector *dissector;
1415f0a35040SMichal Swiatkowski 	struct net_device *tunnel_dev;
14160d08a441SKiran Patil 
14170d08a441SKiran Patil 	dissector = rule->match.dissector;
14180d08a441SKiran Patil 
14190d08a441SKiran Patil 	if (dissector->used_keys &
14202b3082c6SRatheesh Kannoth 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
14212b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
14222b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
14232b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
14242b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_CVLAN) |
14252b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
14262b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
14272b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
14282b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID) |
14292b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
14302b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
14312b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_PORTS) |
14322b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_OPTS) |
14332b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_IP) |
14342b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IP) |
14352b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
14362b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_PPPOE) |
14372b3082c6SRatheesh Kannoth 	      BIT_ULL(FLOW_DISSECTOR_KEY_L2TPV3))) {
14380d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported key used");
14390d08a441SKiran Patil 		return -EOPNOTSUPP;
14400d08a441SKiran Patil 	}
14410d08a441SKiran Patil 
1442f0a35040SMichal Swiatkowski 	tunnel_dev = ice_get_tunnel_device(filter_dev, rule);
1443f0a35040SMichal Swiatkowski 	if (tunnel_dev) {
14449e300987SMichal Swiatkowski 		int err;
14459e300987SMichal Swiatkowski 
1446f0a35040SMichal Swiatkowski 		filter_dev = tunnel_dev;
14479e300987SMichal Swiatkowski 
14489e300987SMichal Swiatkowski 		err = ice_parse_tunnel_attr(filter_dev, rule, fltr);
14499e300987SMichal Swiatkowski 		if (err) {
14509e300987SMichal Swiatkowski 			NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to parse TC flower tunnel attributes");
14519e300987SMichal Swiatkowski 			return err;
14529e300987SMichal Swiatkowski 		}
14539e300987SMichal Swiatkowski 
14549e300987SMichal Swiatkowski 		/* header pointers should point to the inner headers, outer
14559e300987SMichal Swiatkowski 		 * header were already set by ice_parse_tunnel_attr
14569e300987SMichal Swiatkowski 		 */
14579e300987SMichal Swiatkowski 		headers = &fltr->inner_headers;
1458f0a35040SMichal Swiatkowski 	} else if (dissector->used_keys &
14592b3082c6SRatheesh Kannoth 		  (BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
14602b3082c6SRatheesh Kannoth 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
14612b3082c6SRatheesh Kannoth 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID) |
14620ada9981SMarcin Szycik 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_PORTS) |
14630ada9981SMarcin Szycik 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_IP) |
14640ada9981SMarcin Szycik 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_OPTS) |
14650ada9981SMarcin Szycik 		   BIT_ULL(FLOW_DISSECTOR_KEY_ENC_CONTROL))) {
1466f0a35040SMichal Swiatkowski 		NL_SET_ERR_MSG_MOD(fltr->extack, "Tunnel key used, but device isn't a tunnel");
1467f0a35040SMichal Swiatkowski 		return -EOPNOTSUPP;
14689e300987SMichal Swiatkowski 	} else {
14699e300987SMichal Swiatkowski 		fltr->tunnel_type = TNL_LAST;
14709e300987SMichal Swiatkowski 	}
14719e300987SMichal Swiatkowski 
14720d08a441SKiran Patil 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
14730d08a441SKiran Patil 		struct flow_match_basic match;
14740d08a441SKiran Patil 
14750d08a441SKiran Patil 		flow_rule_match_basic(rule, &match);
14760d08a441SKiran Patil 
14770d08a441SKiran Patil 		n_proto_key = ntohs(match.key->n_proto);
14780d08a441SKiran Patil 		n_proto_mask = ntohs(match.mask->n_proto);
14790d08a441SKiran Patil 
1480d4ea6f63SMarcin Szycik 		if (n_proto_key == ETH_P_ALL || n_proto_key == 0 ||
1481d4ea6f63SMarcin Szycik 		    fltr->tunnel_type == TNL_GTPU ||
1482d4ea6f63SMarcin Szycik 		    fltr->tunnel_type == TNL_GTPC) {
14830d08a441SKiran Patil 			n_proto_key = 0;
14840d08a441SKiran Patil 			n_proto_mask = 0;
14850d08a441SKiran Patil 		} else {
14860d08a441SKiran Patil 			fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
14870d08a441SKiran Patil 		}
14880d08a441SKiran Patil 
14890d08a441SKiran Patil 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
14900d08a441SKiran Patil 		headers->l2_mask.n_proto = cpu_to_be16(n_proto_mask);
14910d08a441SKiran Patil 		headers->l3_key.ip_proto = match.key->ip_proto;
14920d08a441SKiran Patil 	}
14930d08a441SKiran Patil 
14940d08a441SKiran Patil 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
14950d08a441SKiran Patil 		struct flow_match_eth_addrs match;
14960d08a441SKiran Patil 
14970d08a441SKiran Patil 		flow_rule_match_eth_addrs(rule, &match);
14980d08a441SKiran Patil 
14990d08a441SKiran Patil 		if (!is_zero_ether_addr(match.key->dst)) {
15000d08a441SKiran Patil 			ether_addr_copy(headers->l2_key.dst_mac,
15010d08a441SKiran Patil 					match.key->dst);
15020d08a441SKiran Patil 			ether_addr_copy(headers->l2_mask.dst_mac,
15030d08a441SKiran Patil 					match.mask->dst);
15040d08a441SKiran Patil 			fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
15050d08a441SKiran Patil 		}
15060d08a441SKiran Patil 
15070d08a441SKiran Patil 		if (!is_zero_ether_addr(match.key->src)) {
15080d08a441SKiran Patil 			ether_addr_copy(headers->l2_key.src_mac,
15090d08a441SKiran Patil 					match.key->src);
15100d08a441SKiran Patil 			ether_addr_copy(headers->l2_mask.src_mac,
15110d08a441SKiran Patil 					match.mask->src);
15120d08a441SKiran Patil 			fltr->flags |= ICE_TC_FLWR_FIELD_SRC_MAC;
15130d08a441SKiran Patil 		}
15140d08a441SKiran Patil 	}
15150d08a441SKiran Patil 
15160d08a441SKiran Patil 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN) ||
15170d08a441SKiran Patil 	    is_vlan_dev(filter_dev)) {
15180d08a441SKiran Patil 		struct flow_dissector_key_vlan mask;
15190d08a441SKiran Patil 		struct flow_dissector_key_vlan key;
15200d08a441SKiran Patil 		struct flow_match_vlan match;
15210d08a441SKiran Patil 
15220d08a441SKiran Patil 		if (is_vlan_dev(filter_dev)) {
15230d08a441SKiran Patil 			match.key = &key;
15240d08a441SKiran Patil 			match.key->vlan_id = vlan_dev_vlan_id(filter_dev);
15250d08a441SKiran Patil 			match.key->vlan_priority = 0;
15260d08a441SKiran Patil 			match.mask = &mask;
15270d08a441SKiran Patil 			memset(match.mask, 0xff, sizeof(*match.mask));
15280d08a441SKiran Patil 			match.mask->vlan_priority = 0;
15290d08a441SKiran Patil 		} else {
15300d08a441SKiran Patil 			flow_rule_match_vlan(rule, &match);
15310d08a441SKiran Patil 		}
15320d08a441SKiran Patil 
15330d08a441SKiran Patil 		if (match.mask->vlan_id) {
15340d08a441SKiran Patil 			if (match.mask->vlan_id == VLAN_VID_MASK) {
15350d08a441SKiran Patil 				fltr->flags |= ICE_TC_FLWR_FIELD_VLAN;
153634800178SMartyna Szapar-Mudlaw 				headers->vlan_hdr.vlan_id =
153734800178SMartyna Szapar-Mudlaw 					cpu_to_be16(match.key->vlan_id &
153834800178SMartyna Szapar-Mudlaw 						    VLAN_VID_MASK);
15390d08a441SKiran Patil 			} else {
15400d08a441SKiran Patil 				NL_SET_ERR_MSG_MOD(fltr->extack, "Bad VLAN mask");
15410d08a441SKiran Patil 				return -EINVAL;
15420d08a441SKiran Patil 			}
15430d08a441SKiran Patil 		}
15440d08a441SKiran Patil 
154534800178SMartyna Szapar-Mudlaw 		if (match.mask->vlan_priority) {
154634800178SMartyna Szapar-Mudlaw 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
154734800178SMartyna Szapar-Mudlaw 			headers->vlan_hdr.vlan_prio =
15488f5c5a79SArnd Bergmann 				be16_encode_bits(match.key->vlan_priority,
15498f5c5a79SArnd Bergmann 						 VLAN_PRIO_MASK);
155034800178SMartyna Szapar-Mudlaw 		}
155134800178SMartyna Szapar-Mudlaw 
155203592a14SMichal Swiatkowski 		if (match.mask->vlan_tpid) {
1553ea71b967SMartyna Szapar-Mudlaw 			headers->vlan_hdr.vlan_tpid = match.key->vlan_tpid;
155403592a14SMichal Swiatkowski 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_TPID;
155503592a14SMichal Swiatkowski 		}
15560d08a441SKiran Patil 	}
15570d08a441SKiran Patil 
155806bca7c2SMartyna Szapar-Mudlaw 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CVLAN)) {
155906bca7c2SMartyna Szapar-Mudlaw 		struct flow_match_vlan match;
156006bca7c2SMartyna Szapar-Mudlaw 
156106bca7c2SMartyna Szapar-Mudlaw 		if (!ice_is_dvm_ena(&vsi->back->hw)) {
156206bca7c2SMartyna Szapar-Mudlaw 			NL_SET_ERR_MSG_MOD(fltr->extack, "Double VLAN mode is not enabled");
156306bca7c2SMartyna Szapar-Mudlaw 			return -EINVAL;
156406bca7c2SMartyna Szapar-Mudlaw 		}
156506bca7c2SMartyna Szapar-Mudlaw 
156606bca7c2SMartyna Szapar-Mudlaw 		flow_rule_match_cvlan(rule, &match);
156706bca7c2SMartyna Szapar-Mudlaw 
156806bca7c2SMartyna Szapar-Mudlaw 		if (match.mask->vlan_id) {
156906bca7c2SMartyna Szapar-Mudlaw 			if (match.mask->vlan_id == VLAN_VID_MASK) {
157006bca7c2SMartyna Szapar-Mudlaw 				fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN;
157134800178SMartyna Szapar-Mudlaw 				headers->cvlan_hdr.vlan_id =
157234800178SMartyna Szapar-Mudlaw 					cpu_to_be16(match.key->vlan_id &
157334800178SMartyna Szapar-Mudlaw 						    VLAN_VID_MASK);
157406bca7c2SMartyna Szapar-Mudlaw 			} else {
157506bca7c2SMartyna Szapar-Mudlaw 				NL_SET_ERR_MSG_MOD(fltr->extack,
157606bca7c2SMartyna Szapar-Mudlaw 						   "Bad CVLAN mask");
157706bca7c2SMartyna Szapar-Mudlaw 				return -EINVAL;
157806bca7c2SMartyna Szapar-Mudlaw 			}
157906bca7c2SMartyna Szapar-Mudlaw 		}
158006bca7c2SMartyna Szapar-Mudlaw 
158134800178SMartyna Szapar-Mudlaw 		if (match.mask->vlan_priority) {
158234800178SMartyna Szapar-Mudlaw 			fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
158334800178SMartyna Szapar-Mudlaw 			headers->cvlan_hdr.vlan_prio =
15848f5c5a79SArnd Bergmann 				be16_encode_bits(match.key->vlan_priority,
15858f5c5a79SArnd Bergmann 						 VLAN_PRIO_MASK);
158634800178SMartyna Szapar-Mudlaw 		}
158706bca7c2SMartyna Szapar-Mudlaw 	}
158806bca7c2SMartyna Szapar-Mudlaw 
1589cd8efeeeSMarcin Szycik 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PPPOE)) {
1590cd8efeeeSMarcin Szycik 		struct flow_match_pppoe match;
1591cd8efeeeSMarcin Szycik 
1592cd8efeeeSMarcin Szycik 		flow_rule_match_pppoe(rule, &match);
1593cd8efeeeSMarcin Szycik 		n_proto_key = ice_tc_set_pppoe(&match, fltr, headers);
1594cd8efeeeSMarcin Szycik 
1595cd8efeeeSMarcin Szycik 		/* If ethertype equals ETH_P_PPP_SES, n_proto might be
1596cd8efeeeSMarcin Szycik 		 * overwritten by encapsulated protocol (ppp_proto field) or set
1597cd8efeeeSMarcin Szycik 		 * to 0. To correct this, flow_match_pppoe provides the type
1598cd8efeeeSMarcin Szycik 		 * field, which contains the actual ethertype (ETH_P_PPP_SES).
1599cd8efeeeSMarcin Szycik 		 */
1600cd8efeeeSMarcin Szycik 		headers->l2_key.n_proto = cpu_to_be16(n_proto_key);
1601cd8efeeeSMarcin Szycik 		headers->l2_mask.n_proto = cpu_to_be16(0xFFFF);
1602cd8efeeeSMarcin Szycik 		fltr->flags |= ICE_TC_FLWR_FIELD_ETH_TYPE_ID;
1603cd8efeeeSMarcin Szycik 	}
1604cd8efeeeSMarcin Szycik 
16050d08a441SKiran Patil 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
16060d08a441SKiran Patil 		struct flow_match_control match;
16070d08a441SKiran Patil 
16080d08a441SKiran Patil 		flow_rule_match_control(rule, &match);
16090d08a441SKiran Patil 
16100d08a441SKiran Patil 		addr_type = match.key->addr_type;
16110d08a441SKiran Patil 	}
16120d08a441SKiran Patil 
16130d08a441SKiran Patil 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
16140d08a441SKiran Patil 		struct flow_match_ipv4_addrs match;
16150d08a441SKiran Patil 
16160d08a441SKiran Patil 		flow_rule_match_ipv4_addrs(rule, &match);
16179e300987SMichal Swiatkowski 		if (ice_tc_set_ipv4(&match, fltr, headers, false))
16180d08a441SKiran Patil 			return -EINVAL;
16190d08a441SKiran Patil 	}
16200d08a441SKiran Patil 
16210d08a441SKiran Patil 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
16220d08a441SKiran Patil 		struct flow_match_ipv6_addrs match;
16230d08a441SKiran Patil 
16240d08a441SKiran Patil 		flow_rule_match_ipv6_addrs(rule, &match);
16259e300987SMichal Swiatkowski 		if (ice_tc_set_ipv6(&match, fltr, headers, false))
16260d08a441SKiran Patil 			return -EINVAL;
16270d08a441SKiran Patil 	}
16280d08a441SKiran Patil 
16294c99bc96SMarcin Szycik 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_IP)) {
16304c99bc96SMarcin Szycik 		struct flow_match_ip match;
16314c99bc96SMarcin Szycik 
16324c99bc96SMarcin Szycik 		flow_rule_match_ip(rule, &match);
16334c99bc96SMarcin Szycik 		ice_tc_set_tos_ttl(&match, fltr, headers, false);
16344c99bc96SMarcin Szycik 	}
16354c99bc96SMarcin Szycik 
1636cd634549SMarcin Szycik 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_L2TPV3)) {
1637cd634549SMarcin Szycik 		struct flow_match_l2tpv3 match;
1638cd634549SMarcin Szycik 
1639cd634549SMarcin Szycik 		flow_rule_match_l2tpv3(rule, &match);
1640cd634549SMarcin Szycik 
1641cd634549SMarcin Szycik 		fltr->flags |= ICE_TC_FLWR_FIELD_L2TPV3_SESSID;
1642cd634549SMarcin Szycik 		headers->l2tpv3_hdr.session_id = match.key->session_id;
1643cd634549SMarcin Szycik 	}
1644cd634549SMarcin Szycik 
16450d08a441SKiran Patil 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
16460d08a441SKiran Patil 		struct flow_match_ports match;
16470d08a441SKiran Patil 
16480d08a441SKiran Patil 		flow_rule_match_ports(rule, &match);
16499e300987SMichal Swiatkowski 		if (ice_tc_set_port(match, fltr, headers, false))
16500d08a441SKiran Patil 			return -EINVAL;
16510d08a441SKiran Patil 		switch (headers->l3_key.ip_proto) {
16520d08a441SKiran Patil 		case IPPROTO_TCP:
16530d08a441SKiran Patil 		case IPPROTO_UDP:
16540d08a441SKiran Patil 			break;
16550d08a441SKiran Patil 		default:
16560d08a441SKiran Patil 			NL_SET_ERR_MSG_MOD(fltr->extack, "Only UDP and TCP transport are supported");
16570d08a441SKiran Patil 			return -EINVAL;
16580d08a441SKiran Patil 		}
16590d08a441SKiran Patil 	}
16600d08a441SKiran Patil 	return 0;
16610d08a441SKiran Patil }
16620d08a441SKiran Patil 
16630d08a441SKiran Patil /**
16640d08a441SKiran Patil  * ice_add_switch_fltr - Add TC flower filters
16650d08a441SKiran Patil  * @vsi: Pointer to VSI
16660d08a441SKiran Patil  * @fltr: Pointer to struct ice_tc_flower_fltr
16670d08a441SKiran Patil  *
16680d08a441SKiran Patil  * Add filter in HW switch block
16690d08a441SKiran Patil  */
16700d08a441SKiran Patil static int
ice_add_switch_fltr(struct ice_vsi * vsi,struct ice_tc_flower_fltr * fltr)16710d08a441SKiran Patil ice_add_switch_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
16720d08a441SKiran Patil {
16739fea7498SKiran Patil 	if (fltr->action.fltr_act == ICE_FWD_TO_QGRP)
16749fea7498SKiran Patil 		return -EOPNOTSUPP;
16759fea7498SKiran Patil 
16760d08a441SKiran Patil 	if (ice_is_eswitch_mode_switchdev(vsi->back))
16770d08a441SKiran Patil 		return ice_eswitch_add_tc_fltr(vsi, fltr);
16780d08a441SKiran Patil 
16799fea7498SKiran Patil 	return ice_add_tc_flower_adv_fltr(vsi, fltr);
16800d08a441SKiran Patil }
16810d08a441SKiran Patil 
16820d08a441SKiran Patil /**
1683143b86f3SAmritha Nambiar  * ice_prep_adq_filter - Prepare ADQ filter with the required additional headers
16840d08a441SKiran Patil  * @vsi: Pointer to VSI
16850d08a441SKiran Patil  * @fltr: Pointer to TC flower filter structure
16860d08a441SKiran Patil  *
1687143b86f3SAmritha Nambiar  * Prepare ADQ filter with the required additional header fields
16880d08a441SKiran Patil  */
16890d08a441SKiran Patil static int
ice_prep_adq_filter(struct ice_vsi * vsi,struct ice_tc_flower_fltr * fltr)1690143b86f3SAmritha Nambiar ice_prep_adq_filter(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
16910d08a441SKiran Patil {
16929fea7498SKiran Patil 	if ((fltr->flags & ICE_TC_FLWR_FIELD_TENANT_ID) &&
16939fea7498SKiran Patil 	    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
16949fea7498SKiran Patil 			   ICE_TC_FLWR_FIELD_SRC_MAC))) {
16959fea7498SKiran Patil 		NL_SET_ERR_MSG_MOD(fltr->extack,
16969fea7498SKiran Patil 				   "Unable to add filter because filter using tunnel key and inner MAC is unsupported combination");
16979fea7498SKiran Patil 		return -EOPNOTSUPP;
16989fea7498SKiran Patil 	}
16999fea7498SKiran Patil 
17009fea7498SKiran Patil 	/* For ADQ, filter must include dest MAC address, otherwise unwanted
17019fea7498SKiran Patil 	 * packets with unrelated MAC address get delivered to ADQ VSIs as long
17029fea7498SKiran Patil 	 * as remaining filter criteria is satisfied such as dest IP address
1703143b86f3SAmritha Nambiar 	 * and dest/src L4 port. Below code handles the following cases:
1704143b86f3SAmritha Nambiar 	 * 1. For non-tunnel, if user specify MAC addresses, use them.
17059fea7498SKiran Patil 	 * 2. For non-tunnel, if user didn't specify MAC address, add implicit
17069fea7498SKiran Patil 	 * dest MAC to be lower netdev's active unicast MAC address
170702ddec19SAmritha Nambiar 	 * 3. For tunnel,  as of now TC-filter through flower classifier doesn't
170802ddec19SAmritha Nambiar 	 * have provision for user to specify outer DMAC, hence driver to
170902ddec19SAmritha Nambiar 	 * implicitly add outer dest MAC to be lower netdev's active unicast
171002ddec19SAmritha Nambiar 	 * MAC address.
17119fea7498SKiran Patil 	 */
171202ddec19SAmritha Nambiar 	if (fltr->tunnel_type != TNL_LAST &&
171302ddec19SAmritha Nambiar 	    !(fltr->flags & ICE_TC_FLWR_FIELD_ENC_DST_MAC))
171402ddec19SAmritha Nambiar 		fltr->flags |= ICE_TC_FLWR_FIELD_ENC_DST_MAC;
171502ddec19SAmritha Nambiar 
171602ddec19SAmritha Nambiar 	if (fltr->tunnel_type == TNL_LAST &&
171702ddec19SAmritha Nambiar 	    !(fltr->flags & ICE_TC_FLWR_FIELD_DST_MAC))
17189fea7498SKiran Patil 		fltr->flags |= ICE_TC_FLWR_FIELD_DST_MAC;
171902ddec19SAmritha Nambiar 
172002ddec19SAmritha Nambiar 	if (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
172102ddec19SAmritha Nambiar 			   ICE_TC_FLWR_FIELD_ENC_DST_MAC)) {
172202ddec19SAmritha Nambiar 		ether_addr_copy(fltr->outer_headers.l2_key.dst_mac,
172302ddec19SAmritha Nambiar 				vsi->netdev->dev_addr);
17240ca85829SLu Wei 		eth_broadcast_addr(fltr->outer_headers.l2_mask.dst_mac);
17259fea7498SKiran Patil 	}
17269fea7498SKiran Patil 
17279fea7498SKiran Patil 	/* Make sure VLAN is already added to main VSI, before allowing ADQ to
17289fea7498SKiran Patil 	 * add a VLAN based filter such as MAC + VLAN + L4 port.
17299fea7498SKiran Patil 	 */
17309fea7498SKiran Patil 	if (fltr->flags & ICE_TC_FLWR_FIELD_VLAN) {
17319fea7498SKiran Patil 		u16 vlan_id = be16_to_cpu(fltr->outer_headers.vlan_hdr.vlan_id);
17329fea7498SKiran Patil 
1733143b86f3SAmritha Nambiar 		if (!ice_vlan_fltr_exist(&vsi->back->hw, vlan_id, vsi->idx)) {
17349fea7498SKiran Patil 			NL_SET_ERR_MSG_MOD(fltr->extack,
17359fea7498SKiran Patil 					   "Unable to add filter because legacy VLAN filter for specified destination doesn't exist");
17369fea7498SKiran Patil 			return -EINVAL;
17379fea7498SKiran Patil 		}
17389fea7498SKiran Patil 	}
17390d08a441SKiran Patil 	return 0;
17400d08a441SKiran Patil }
17410d08a441SKiran Patil 
17420d08a441SKiran Patil /**
1743143b86f3SAmritha Nambiar  * ice_handle_tclass_action - Support directing to a traffic class
1744143b86f3SAmritha Nambiar  * @vsi: Pointer to VSI
1745143b86f3SAmritha Nambiar  * @cls_flower: Pointer to TC flower offload structure
1746143b86f3SAmritha Nambiar  * @fltr: Pointer to TC flower filter structure
1747143b86f3SAmritha Nambiar  *
1748143b86f3SAmritha Nambiar  * Support directing traffic to a traffic class/queue-set
1749143b86f3SAmritha Nambiar  */
1750143b86f3SAmritha Nambiar static int
ice_handle_tclass_action(struct ice_vsi * vsi,struct flow_cls_offload * cls_flower,struct ice_tc_flower_fltr * fltr)1751143b86f3SAmritha Nambiar ice_handle_tclass_action(struct ice_vsi *vsi,
1752143b86f3SAmritha Nambiar 			 struct flow_cls_offload *cls_flower,
1753143b86f3SAmritha Nambiar 			 struct ice_tc_flower_fltr *fltr)
1754143b86f3SAmritha Nambiar {
1755143b86f3SAmritha Nambiar 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
1756143b86f3SAmritha Nambiar 
1757143b86f3SAmritha Nambiar 	/* user specified hw_tc (must be non-zero for ADQ TC), action is forward
1758143b86f3SAmritha Nambiar 	 * to hw_tc (i.e. ADQ channel number)
1759143b86f3SAmritha Nambiar 	 */
1760143b86f3SAmritha Nambiar 	if (tc < ICE_CHNL_START_TC) {
1761143b86f3SAmritha Nambiar 		NL_SET_ERR_MSG_MOD(fltr->extack,
1762143b86f3SAmritha Nambiar 				   "Unable to add filter because of unsupported destination");
1763143b86f3SAmritha Nambiar 		return -EOPNOTSUPP;
1764143b86f3SAmritha Nambiar 	}
1765143b86f3SAmritha Nambiar 	if (!(vsi->all_enatc & BIT(tc))) {
1766143b86f3SAmritha Nambiar 		NL_SET_ERR_MSG_MOD(fltr->extack,
1767143b86f3SAmritha Nambiar 				   "Unable to add filter because of non-existence destination");
1768143b86f3SAmritha Nambiar 		return -EINVAL;
1769143b86f3SAmritha Nambiar 	}
1770143b86f3SAmritha Nambiar 	fltr->action.fltr_act = ICE_FWD_TO_VSI;
1771143b86f3SAmritha Nambiar 	fltr->action.fwd.tc.tc_class = tc;
1772143b86f3SAmritha Nambiar 
1773143b86f3SAmritha Nambiar 	return ice_prep_adq_filter(vsi, fltr);
1774143b86f3SAmritha Nambiar }
1775143b86f3SAmritha Nambiar 
1776143b86f3SAmritha Nambiar static int
ice_tc_forward_to_queue(struct ice_vsi * vsi,struct ice_tc_flower_fltr * fltr,struct flow_action_entry * act)1777143b86f3SAmritha Nambiar ice_tc_forward_to_queue(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1778143b86f3SAmritha Nambiar 			struct flow_action_entry *act)
1779143b86f3SAmritha Nambiar {
1780143b86f3SAmritha Nambiar 	struct ice_vsi *ch_vsi = NULL;
1781143b86f3SAmritha Nambiar 	u16 queue = act->rx_queue;
1782143b86f3SAmritha Nambiar 
17833f4870dfSDan Carpenter 	if (queue >= vsi->num_rxq) {
1784143b86f3SAmritha Nambiar 		NL_SET_ERR_MSG_MOD(fltr->extack,
1785143b86f3SAmritha Nambiar 				   "Unable to add filter because specified queue is invalid");
1786143b86f3SAmritha Nambiar 		return -EINVAL;
1787143b86f3SAmritha Nambiar 	}
1788143b86f3SAmritha Nambiar 	fltr->action.fltr_act = ICE_FWD_TO_Q;
1789143b86f3SAmritha Nambiar 	fltr->action.fwd.q.queue = queue;
1790143b86f3SAmritha Nambiar 	/* determine corresponding HW queue */
1791143b86f3SAmritha Nambiar 	fltr->action.fwd.q.hw_queue = vsi->rxq_map[queue];
1792143b86f3SAmritha Nambiar 
1793143b86f3SAmritha Nambiar 	/* If ADQ is configured, and the queue belongs to ADQ VSI, then prepare
1794143b86f3SAmritha Nambiar 	 * ADQ switch filter
1795143b86f3SAmritha Nambiar 	 */
1796479cdfe3SSridhar Samudrala 	ch_vsi = ice_locate_vsi_using_queue(vsi, fltr->action.fwd.q.queue);
1797143b86f3SAmritha Nambiar 	if (!ch_vsi)
1798143b86f3SAmritha Nambiar 		return -EINVAL;
1799143b86f3SAmritha Nambiar 	fltr->dest_vsi = ch_vsi;
1800143b86f3SAmritha Nambiar 	if (!ice_is_chnl_fltr(fltr))
1801143b86f3SAmritha Nambiar 		return 0;
1802143b86f3SAmritha Nambiar 
1803143b86f3SAmritha Nambiar 	return ice_prep_adq_filter(vsi, fltr);
1804143b86f3SAmritha Nambiar }
1805143b86f3SAmritha Nambiar 
1806143b86f3SAmritha Nambiar static int
ice_tc_parse_action(struct ice_vsi * vsi,struct ice_tc_flower_fltr * fltr,struct flow_action_entry * act)1807143b86f3SAmritha Nambiar ice_tc_parse_action(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr,
1808143b86f3SAmritha Nambiar 		    struct flow_action_entry *act)
1809143b86f3SAmritha Nambiar {
1810143b86f3SAmritha Nambiar 	switch (act->id) {
1811143b86f3SAmritha Nambiar 	case FLOW_ACTION_RX_QUEUE_MAPPING:
1812143b86f3SAmritha Nambiar 		/* forward to queue */
1813143b86f3SAmritha Nambiar 		return ice_tc_forward_to_queue(vsi, fltr, act);
1814390889a4SAmritha Nambiar 	case FLOW_ACTION_DROP:
1815390889a4SAmritha Nambiar 		fltr->action.fltr_act = ICE_DROP_PACKET;
1816390889a4SAmritha Nambiar 		return 0;
1817143b86f3SAmritha Nambiar 	default:
1818143b86f3SAmritha Nambiar 		NL_SET_ERR_MSG_MOD(fltr->extack, "Unsupported TC action");
1819143b86f3SAmritha Nambiar 		return -EOPNOTSUPP;
1820143b86f3SAmritha Nambiar 	}
1821143b86f3SAmritha Nambiar }
1822143b86f3SAmritha Nambiar 
1823143b86f3SAmritha Nambiar /**
18240d08a441SKiran Patil  * ice_parse_tc_flower_actions - Parse the actions for a TC filter
182510fc8d42SAniruddha Paul  * @filter_dev: Pointer to device on which filter is being added
18260d08a441SKiran Patil  * @vsi: Pointer to VSI
18270d08a441SKiran Patil  * @cls_flower: Pointer to TC flower offload structure
18280d08a441SKiran Patil  * @fltr: Pointer to TC flower filter structure
18290d08a441SKiran Patil  *
18300d08a441SKiran Patil  * Parse the actions for a TC filter
18310d08a441SKiran Patil  */
ice_parse_tc_flower_actions(struct net_device * filter_dev,struct ice_vsi * vsi,struct flow_cls_offload * cls_flower,struct ice_tc_flower_fltr * fltr)183210fc8d42SAniruddha Paul static int ice_parse_tc_flower_actions(struct net_device *filter_dev,
183310fc8d42SAniruddha Paul 				       struct ice_vsi *vsi,
18340d08a441SKiran Patil 				       struct flow_cls_offload *cls_flower,
18350d08a441SKiran Patil 				       struct ice_tc_flower_fltr *fltr)
18360d08a441SKiran Patil {
18370d08a441SKiran Patil 	struct flow_rule *rule = flow_cls_offload_flow_rule(cls_flower);
18380d08a441SKiran Patil 	struct flow_action *flow_action = &rule->action;
18390d08a441SKiran Patil 	struct flow_action_entry *act;
1840143b86f3SAmritha Nambiar 	int i, err;
18410d08a441SKiran Patil 
18420d08a441SKiran Patil 	if (cls_flower->classid)
18430d08a441SKiran Patil 		return ice_handle_tclass_action(vsi, cls_flower, fltr);
18440d08a441SKiran Patil 
18450d08a441SKiran Patil 	if (!flow_action_has_entries(flow_action))
18460d08a441SKiran Patil 		return -EINVAL;
18470d08a441SKiran Patil 
18480d08a441SKiran Patil 	flow_action_for_each(i, act, flow_action) {
1849143b86f3SAmritha Nambiar 		if (ice_is_eswitch_mode_switchdev(vsi->back))
185010fc8d42SAniruddha Paul 			err = ice_eswitch_tc_parse_action(filter_dev, fltr, act);
1851143b86f3SAmritha Nambiar 		else
1852143b86f3SAmritha Nambiar 			err = ice_tc_parse_action(vsi, fltr, act);
18530d08a441SKiran Patil 		if (err)
18540d08a441SKiran Patil 			return err;
18550d08a441SKiran Patil 		continue;
18560d08a441SKiran Patil 	}
18570d08a441SKiran Patil 	return 0;
18580d08a441SKiran Patil }
18590d08a441SKiran Patil 
18600d08a441SKiran Patil /**
18610d08a441SKiran Patil  * ice_del_tc_fltr - deletes a filter from HW table
18620d08a441SKiran Patil  * @vsi: Pointer to VSI
18630d08a441SKiran Patil  * @fltr: Pointer to struct ice_tc_flower_fltr
18640d08a441SKiran Patil  *
18650d08a441SKiran Patil  * This function deletes a filter from HW table and manages book-keeping
18660d08a441SKiran Patil  */
ice_del_tc_fltr(struct ice_vsi * vsi,struct ice_tc_flower_fltr * fltr)18670d08a441SKiran Patil static int ice_del_tc_fltr(struct ice_vsi *vsi, struct ice_tc_flower_fltr *fltr)
18680d08a441SKiran Patil {
18690d08a441SKiran Patil 	struct ice_rule_query_data rule_rem;
18700d08a441SKiran Patil 	struct ice_pf *pf = vsi->back;
18710d08a441SKiran Patil 	int err;
18720d08a441SKiran Patil 
18730d08a441SKiran Patil 	rule_rem.rid = fltr->rid;
18740d08a441SKiran Patil 	rule_rem.rule_id = fltr->rule_id;
1875143b86f3SAmritha Nambiar 	rule_rem.vsi_handle = fltr->dest_vsi_handle;
18760d08a441SKiran Patil 	err = ice_rem_adv_rule_by_id(&pf->hw, &rule_rem);
18770d08a441SKiran Patil 	if (err) {
1878d54699e2STony Nguyen 		if (err == -ENOENT) {
18790d08a441SKiran Patil 			NL_SET_ERR_MSG_MOD(fltr->extack, "Filter does not exist");
18800d08a441SKiran Patil 			return -ENOENT;
18810d08a441SKiran Patil 		}
18820d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(fltr->extack, "Failed to delete TC flower filter");
18830d08a441SKiran Patil 		return -EIO;
18840d08a441SKiran Patil 	}
18850d08a441SKiran Patil 
18869fea7498SKiran Patil 	/* update advanced switch filter count for destination
18879fea7498SKiran Patil 	 * VSI if filter destination was VSI
18889fea7498SKiran Patil 	 */
18899fea7498SKiran Patil 	if (fltr->dest_vsi) {
18909fea7498SKiran Patil 		if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
18919fea7498SKiran Patil 			fltr->dest_vsi->num_chnl_fltr--;
18929fea7498SKiran Patil 
18939fea7498SKiran Patil 			/* keeps track of channel filters for PF VSI */
18949fea7498SKiran Patil 			if (vsi->type == ICE_VSI_PF &&
18959fea7498SKiran Patil 			    (fltr->flags & (ICE_TC_FLWR_FIELD_DST_MAC |
18969fea7498SKiran Patil 					    ICE_TC_FLWR_FIELD_ENC_DST_MAC)))
18979fea7498SKiran Patil 				pf->num_dmac_chnl_fltrs--;
18989fea7498SKiran Patil 		}
18999fea7498SKiran Patil 	}
19000d08a441SKiran Patil 	return 0;
19010d08a441SKiran Patil }
19020d08a441SKiran Patil 
19030d08a441SKiran Patil /**
19040d08a441SKiran Patil  * ice_add_tc_fltr - adds a TC flower filter
19050d08a441SKiran Patil  * @netdev: Pointer to netdev
19060d08a441SKiran Patil  * @vsi: Pointer to VSI
19070d08a441SKiran Patil  * @f: Pointer to flower offload structure
19080d08a441SKiran Patil  * @__fltr: Pointer to struct ice_tc_flower_fltr
19090d08a441SKiran Patil  *
19100d08a441SKiran Patil  * This function parses TC-flower input fields, parses action,
19110d08a441SKiran Patil  * and adds a filter.
19120d08a441SKiran Patil  */
19130d08a441SKiran Patil static int
ice_add_tc_fltr(struct net_device * netdev,struct ice_vsi * vsi,struct flow_cls_offload * f,struct ice_tc_flower_fltr ** __fltr)19140d08a441SKiran Patil ice_add_tc_fltr(struct net_device *netdev, struct ice_vsi *vsi,
19150d08a441SKiran Patil 		struct flow_cls_offload *f,
19160d08a441SKiran Patil 		struct ice_tc_flower_fltr **__fltr)
19170d08a441SKiran Patil {
19180d08a441SKiran Patil 	struct ice_tc_flower_fltr *fltr;
19190d08a441SKiran Patil 	int err;
19200d08a441SKiran Patil 
19210d08a441SKiran Patil 	/* by default, set output to be INVALID */
19220d08a441SKiran Patil 	*__fltr = NULL;
19230d08a441SKiran Patil 
19240d08a441SKiran Patil 	fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
19250d08a441SKiran Patil 	if (!fltr)
19260d08a441SKiran Patil 		return -ENOMEM;
19270d08a441SKiran Patil 
19280d08a441SKiran Patil 	fltr->cookie = f->cookie;
19290d08a441SKiran Patil 	fltr->extack = f->common.extack;
19300d08a441SKiran Patil 	fltr->src_vsi = vsi;
19310d08a441SKiran Patil 	INIT_HLIST_NODE(&fltr->tc_flower_node);
19320d08a441SKiran Patil 
19330d08a441SKiran Patil 	err = ice_parse_cls_flower(netdev, vsi, f, fltr);
19340d08a441SKiran Patil 	if (err < 0)
19350d08a441SKiran Patil 		goto err;
19360d08a441SKiran Patil 
193710fc8d42SAniruddha Paul 	err = ice_parse_tc_flower_actions(netdev, vsi, f, fltr);
19380d08a441SKiran Patil 	if (err < 0)
19390d08a441SKiran Patil 		goto err;
19400d08a441SKiran Patil 
19410d08a441SKiran Patil 	err = ice_add_switch_fltr(vsi, fltr);
19420d08a441SKiran Patil 	if (err < 0)
19430d08a441SKiran Patil 		goto err;
19440d08a441SKiran Patil 
19450d08a441SKiran Patil 	/* return the newly created filter */
19460d08a441SKiran Patil 	*__fltr = fltr;
19470d08a441SKiran Patil 
19480d08a441SKiran Patil 	return 0;
19490d08a441SKiran Patil err:
19500d08a441SKiran Patil 	kfree(fltr);
19510d08a441SKiran Patil 	return err;
19520d08a441SKiran Patil }
19530d08a441SKiran Patil 
19540d08a441SKiran Patil /**
19550d08a441SKiran Patil  * ice_find_tc_flower_fltr - Find the TC flower filter in the list
19560d08a441SKiran Patil  * @pf: Pointer to PF
19570d08a441SKiran Patil  * @cookie: filter specific cookie
19580d08a441SKiran Patil  */
19590d08a441SKiran Patil static struct ice_tc_flower_fltr *
ice_find_tc_flower_fltr(struct ice_pf * pf,unsigned long cookie)19600d08a441SKiran Patil ice_find_tc_flower_fltr(struct ice_pf *pf, unsigned long cookie)
19610d08a441SKiran Patil {
19620d08a441SKiran Patil 	struct ice_tc_flower_fltr *fltr;
19630d08a441SKiran Patil 
19640d08a441SKiran Patil 	hlist_for_each_entry(fltr, &pf->tc_flower_fltr_list, tc_flower_node)
19650d08a441SKiran Patil 		if (cookie == fltr->cookie)
19660d08a441SKiran Patil 			return fltr;
19670d08a441SKiran Patil 
19680d08a441SKiran Patil 	return NULL;
19690d08a441SKiran Patil }
19700d08a441SKiran Patil 
19710d08a441SKiran Patil /**
19720d08a441SKiran Patil  * ice_add_cls_flower - add TC flower filters
19730d08a441SKiran Patil  * @netdev: Pointer to filter device
19740d08a441SKiran Patil  * @vsi: Pointer to VSI
19750d08a441SKiran Patil  * @cls_flower: Pointer to flower offload structure
19760d08a441SKiran Patil  */
19770d08a441SKiran Patil int
ice_add_cls_flower(struct net_device * netdev,struct ice_vsi * vsi,struct flow_cls_offload * cls_flower)19780d08a441SKiran Patil ice_add_cls_flower(struct net_device *netdev, struct ice_vsi *vsi,
19790d08a441SKiran Patil 		   struct flow_cls_offload *cls_flower)
19800d08a441SKiran Patil {
19810d08a441SKiran Patil 	struct netlink_ext_ack *extack = cls_flower->common.extack;
19820d08a441SKiran Patil 	struct net_device *vsi_netdev = vsi->netdev;
19830d08a441SKiran Patil 	struct ice_tc_flower_fltr *fltr;
19840d08a441SKiran Patil 	struct ice_pf *pf = vsi->back;
19850d08a441SKiran Patil 	int err;
19860d08a441SKiran Patil 
19870d08a441SKiran Patil 	if (ice_is_reset_in_progress(pf->state))
19880d08a441SKiran Patil 		return -EBUSY;
19890d08a441SKiran Patil 	if (test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
19900d08a441SKiran Patil 		return -EINVAL;
19910d08a441SKiran Patil 
19920d08a441SKiran Patil 	if (ice_is_port_repr_netdev(netdev))
19930d08a441SKiran Patil 		vsi_netdev = netdev;
19940d08a441SKiran Patil 
19950d08a441SKiran Patil 	if (!(vsi_netdev->features & NETIF_F_HW_TC) &&
19960d08a441SKiran Patil 	    !test_bit(ICE_FLAG_CLS_FLOWER, pf->flags)) {
19970d08a441SKiran Patil 		/* Based on TC indirect notifications from kernel, all ice
19980d08a441SKiran Patil 		 * devices get an instance of rule from higher level device.
19990d08a441SKiran Patil 		 * Avoid triggering explicit error in this case.
20000d08a441SKiran Patil 		 */
20010d08a441SKiran Patil 		if (netdev == vsi_netdev)
20020d08a441SKiran Patil 			NL_SET_ERR_MSG_MOD(extack, "can't apply TC flower filters, turn ON hw-tc-offload and try again");
20030d08a441SKiran Patil 		return -EINVAL;
20040d08a441SKiran Patil 	}
20050d08a441SKiran Patil 
20060d08a441SKiran Patil 	/* avoid duplicate entries, if exists - return error */
20070d08a441SKiran Patil 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
20080d08a441SKiran Patil 	if (fltr) {
20090d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(extack, "filter cookie already exists, ignoring");
20100d08a441SKiran Patil 		return -EEXIST;
20110d08a441SKiran Patil 	}
20120d08a441SKiran Patil 
20130d08a441SKiran Patil 	/* prep and add TC-flower filter in HW */
20140d08a441SKiran Patil 	err = ice_add_tc_fltr(netdev, vsi, cls_flower, &fltr);
20150d08a441SKiran Patil 	if (err)
20160d08a441SKiran Patil 		return err;
20170d08a441SKiran Patil 
20180d08a441SKiran Patil 	/* add filter into an ordered list */
20190d08a441SKiran Patil 	hlist_add_head(&fltr->tc_flower_node, &pf->tc_flower_fltr_list);
20200d08a441SKiran Patil 	return 0;
20210d08a441SKiran Patil }
20220d08a441SKiran Patil 
20230d08a441SKiran Patil /**
20240d08a441SKiran Patil  * ice_del_cls_flower - delete TC flower filters
20250d08a441SKiran Patil  * @vsi: Pointer to VSI
20260d08a441SKiran Patil  * @cls_flower: Pointer to struct flow_cls_offload
20270d08a441SKiran Patil  */
20280d08a441SKiran Patil int
ice_del_cls_flower(struct ice_vsi * vsi,struct flow_cls_offload * cls_flower)20290d08a441SKiran Patil ice_del_cls_flower(struct ice_vsi *vsi, struct flow_cls_offload *cls_flower)
20300d08a441SKiran Patil {
20310d08a441SKiran Patil 	struct ice_tc_flower_fltr *fltr;
20320d08a441SKiran Patil 	struct ice_pf *pf = vsi->back;
20330d08a441SKiran Patil 	int err;
20340d08a441SKiran Patil 
20350d08a441SKiran Patil 	/* find filter */
20360d08a441SKiran Patil 	fltr = ice_find_tc_flower_fltr(pf, cls_flower->cookie);
20370d08a441SKiran Patil 	if (!fltr) {
20389fea7498SKiran Patil 		if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) &&
20399fea7498SKiran Patil 		    hlist_empty(&pf->tc_flower_fltr_list))
20400d08a441SKiran Patil 			return 0;
20410d08a441SKiran Patil 
20420d08a441SKiran Patil 		NL_SET_ERR_MSG_MOD(cls_flower->common.extack, "failed to delete TC flower filter because unable to find it");
20430d08a441SKiran Patil 		return -EINVAL;
20440d08a441SKiran Patil 	}
20450d08a441SKiran Patil 
20460d08a441SKiran Patil 	fltr->extack = cls_flower->common.extack;
20470d08a441SKiran Patil 	/* delete filter from HW */
20480d08a441SKiran Patil 	err = ice_del_tc_fltr(vsi, fltr);
20490d08a441SKiran Patil 	if (err)
20500d08a441SKiran Patil 		return err;
20510d08a441SKiran Patil 
20520d08a441SKiran Patil 	/* delete filter from an ordered list */
20530d08a441SKiran Patil 	hlist_del(&fltr->tc_flower_node);
20540d08a441SKiran Patil 
20550d08a441SKiran Patil 	/* free the filter node */
20560d08a441SKiran Patil 	kfree(fltr);
20570d08a441SKiran Patil 
20580d08a441SKiran Patil 	return 0;
20590d08a441SKiran Patil }
20607fde6d8bSMichal Swiatkowski 
20617fde6d8bSMichal Swiatkowski /**
20627fde6d8bSMichal Swiatkowski  * ice_replay_tc_fltrs - replay TC filters
20637fde6d8bSMichal Swiatkowski  * @pf: pointer to PF struct
20647fde6d8bSMichal Swiatkowski  */
ice_replay_tc_fltrs(struct ice_pf * pf)20657fde6d8bSMichal Swiatkowski void ice_replay_tc_fltrs(struct ice_pf *pf)
20667fde6d8bSMichal Swiatkowski {
20677fde6d8bSMichal Swiatkowski 	struct ice_tc_flower_fltr *fltr;
20687fde6d8bSMichal Swiatkowski 	struct hlist_node *node;
20697fde6d8bSMichal Swiatkowski 
20707fde6d8bSMichal Swiatkowski 	hlist_for_each_entry_safe(fltr, node,
20717fde6d8bSMichal Swiatkowski 				  &pf->tc_flower_fltr_list,
20727fde6d8bSMichal Swiatkowski 				  tc_flower_node) {
20737fde6d8bSMichal Swiatkowski 		fltr->extack = NULL;
20747fde6d8bSMichal Swiatkowski 		ice_add_switch_fltr(fltr->src_vsi, fltr);
20757fde6d8bSMichal Swiatkowski 	}
20767fde6d8bSMichal Swiatkowski }
2077