Linux Audio

Check our new training course

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