Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2018, Intel Corporation. */
   3
   4#include "ice_lib.h"
   5#include "ice_switch.h"
   6#include "ice_trace.h"
   7
   8#define ICE_ETH_DA_OFFSET		0
   9#define ICE_ETH_ETHTYPE_OFFSET		12
  10#define ICE_ETH_VLAN_TCI_OFFSET		14
  11#define ICE_MAX_VLAN_ID			0xFFF
  12#define ICE_IPV6_ETHER_ID		0x86DD
  13
  14/* Dummy ethernet header needed in the ice_aqc_sw_rules_elem
  15 * struct to configure any switch filter rules.
  16 * {DA (6 bytes), SA(6 bytes),
  17 * Ether type (2 bytes for header without VLAN tag) OR
  18 * VLAN tag (4 bytes for header with VLAN tag) }
  19 *
  20 * Word on Hardcoded values
  21 * byte 0 = 0x2: to identify it as locally administered DA MAC
  22 * byte 6 = 0x2: to identify it as locally administered SA MAC
  23 * byte 12 = 0x81 & byte 13 = 0x00:
  24 *      In case of VLAN filter first two bytes defines ether type (0x8100)
  25 *      and remaining two bytes are placeholder for programming a given VLAN ID
  26 *      In case of Ether type filter it is treated as header without VLAN tag
  27 *      and byte 12 and 13 is used to program a given Ether type instead
  28 */
  29static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
  30							0x2, 0, 0, 0, 0, 0,
  31							0x81, 0, 0, 0};
  32
  33enum {
  34	ICE_PKT_OUTER_IPV6	= BIT(0),
  35	ICE_PKT_TUN_GTPC	= BIT(1),
  36	ICE_PKT_TUN_GTPU	= BIT(2),
  37	ICE_PKT_TUN_NVGRE	= BIT(3),
  38	ICE_PKT_TUN_UDP		= BIT(4),
  39	ICE_PKT_INNER_IPV6	= BIT(5),
  40	ICE_PKT_INNER_TCP	= BIT(6),
  41	ICE_PKT_INNER_UDP	= BIT(7),
  42	ICE_PKT_GTP_NOPAY	= BIT(8),
  43	ICE_PKT_KMALLOC		= BIT(9),
  44	ICE_PKT_PPPOE		= BIT(10),
  45	ICE_PKT_L2TPV3		= BIT(11),
  46	ICE_PKT_PFCP		= BIT(12),
  47};
  48
  49struct ice_dummy_pkt_offsets {
  50	enum ice_protocol_type type;
  51	u16 offset; /* ICE_PROTOCOL_LAST indicates end of list */
  52};
  53
  54struct ice_dummy_pkt_profile {
  55	const struct ice_dummy_pkt_offsets *offsets;
  56	const u8 *pkt;
  57	u32 match;
  58	u16 pkt_len;
  59	u16 offsets_len;
  60};
  61
  62#define ICE_DECLARE_PKT_OFFSETS(type)					\
  63	static const struct ice_dummy_pkt_offsets			\
  64	ice_dummy_##type##_packet_offsets[]
  65
  66#define ICE_DECLARE_PKT_TEMPLATE(type)					\
  67	static const u8 ice_dummy_##type##_packet[]
  68
  69#define ICE_PKT_PROFILE(type, m) {					\
  70	.match		= (m),						\
  71	.pkt		= ice_dummy_##type##_packet,			\
  72	.pkt_len	= sizeof(ice_dummy_##type##_packet),		\
  73	.offsets	= ice_dummy_##type##_packet_offsets,		\
  74	.offsets_len	= sizeof(ice_dummy_##type##_packet_offsets),	\
  75}
  76
  77ICE_DECLARE_PKT_OFFSETS(vlan) = {
  78	{ ICE_VLAN_OFOS,        12 },
  79};
  80
  81ICE_DECLARE_PKT_TEMPLATE(vlan) = {
  82	0x81, 0x00, 0x00, 0x00, /* ICE_VLAN_OFOS 12 */
  83};
  84
  85ICE_DECLARE_PKT_OFFSETS(qinq) = {
  86	{ ICE_VLAN_EX,          12 },
  87	{ ICE_VLAN_IN,          16 },
  88};
  89
  90ICE_DECLARE_PKT_TEMPLATE(qinq) = {
  91	0x91, 0x00, 0x00, 0x00, /* ICE_VLAN_EX 12 */
  92	0x81, 0x00, 0x00, 0x00, /* ICE_VLAN_IN 16 */
  93};
  94
  95ICE_DECLARE_PKT_OFFSETS(gre_tcp) = {
  96	{ ICE_MAC_OFOS,		0 },
  97	{ ICE_ETYPE_OL,		12 },
  98	{ ICE_IPV4_OFOS,	14 },
  99	{ ICE_NVGRE,		34 },
 100	{ ICE_MAC_IL,		42 },
 101	{ ICE_ETYPE_IL,		54 },
 102	{ ICE_IPV4_IL,		56 },
 103	{ ICE_TCP_IL,		76 },
 104	{ ICE_PROTOCOL_LAST,	0 },
 105};
 106
 107ICE_DECLARE_PKT_TEMPLATE(gre_tcp) = {
 108	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_OFOS 0 */
 109	0x00, 0x00, 0x00, 0x00,
 110	0x00, 0x00, 0x00, 0x00,
 111
 112	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 113
 114	0x45, 0x00, 0x00, 0x3E,	/* ICE_IPV4_OFOS 14 */
 115	0x00, 0x00, 0x00, 0x00,
 116	0x00, 0x2F, 0x00, 0x00,
 117	0x00, 0x00, 0x00, 0x00,
 118	0x00, 0x00, 0x00, 0x00,
 119
 120	0x80, 0x00, 0x65, 0x58,	/* ICE_NVGRE 34 */
 121	0x00, 0x00, 0x00, 0x00,
 122
 123	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_IL 42 */
 124	0x00, 0x00, 0x00, 0x00,
 125	0x00, 0x00, 0x00, 0x00,
 126
 127	0x08, 0x00,		/* ICE_ETYPE_IL 54 */
 128
 129	0x45, 0x00, 0x00, 0x14,	/* ICE_IPV4_IL 56 */
 130	0x00, 0x00, 0x00, 0x00,
 131	0x00, 0x06, 0x00, 0x00,
 132	0x00, 0x00, 0x00, 0x00,
 133	0x00, 0x00, 0x00, 0x00,
 134
 135	0x00, 0x00, 0x00, 0x00,	/* ICE_TCP_IL 76 */
 136	0x00, 0x00, 0x00, 0x00,
 137	0x00, 0x00, 0x00, 0x00,
 138	0x50, 0x02, 0x20, 0x00,
 139	0x00, 0x00, 0x00, 0x00
 140};
 141
 142ICE_DECLARE_PKT_OFFSETS(gre_udp) = {
 143	{ ICE_MAC_OFOS,		0 },
 144	{ ICE_ETYPE_OL,		12 },
 145	{ ICE_IPV4_OFOS,	14 },
 146	{ ICE_NVGRE,		34 },
 147	{ ICE_MAC_IL,		42 },
 148	{ ICE_ETYPE_IL,		54 },
 149	{ ICE_IPV4_IL,		56 },
 150	{ ICE_UDP_ILOS,		76 },
 151	{ ICE_PROTOCOL_LAST,	0 },
 152};
 153
 154ICE_DECLARE_PKT_TEMPLATE(gre_udp) = {
 155	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_OFOS 0 */
 156	0x00, 0x00, 0x00, 0x00,
 157	0x00, 0x00, 0x00, 0x00,
 158
 159	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 160
 161	0x45, 0x00, 0x00, 0x3E,	/* ICE_IPV4_OFOS 14 */
 162	0x00, 0x00, 0x00, 0x00,
 163	0x00, 0x2F, 0x00, 0x00,
 164	0x00, 0x00, 0x00, 0x00,
 165	0x00, 0x00, 0x00, 0x00,
 166
 167	0x80, 0x00, 0x65, 0x58,	/* ICE_NVGRE 34 */
 168	0x00, 0x00, 0x00, 0x00,
 169
 170	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_IL 42 */
 171	0x00, 0x00, 0x00, 0x00,
 172	0x00, 0x00, 0x00, 0x00,
 173
 174	0x08, 0x00,		/* ICE_ETYPE_IL 54 */
 175
 176	0x45, 0x00, 0x00, 0x14,	/* ICE_IPV4_IL 56 */
 177	0x00, 0x00, 0x00, 0x00,
 178	0x00, 0x11, 0x00, 0x00,
 179	0x00, 0x00, 0x00, 0x00,
 180	0x00, 0x00, 0x00, 0x00,
 181
 182	0x00, 0x00, 0x00, 0x00,	/* ICE_UDP_ILOS 76 */
 183	0x00, 0x08, 0x00, 0x00,
 184};
 185
 186ICE_DECLARE_PKT_OFFSETS(udp_tun_tcp) = {
 187	{ ICE_MAC_OFOS,		0 },
 188	{ ICE_ETYPE_OL,		12 },
 189	{ ICE_IPV4_OFOS,	14 },
 190	{ ICE_UDP_OF,		34 },
 191	{ ICE_VXLAN,		42 },
 192	{ ICE_GENEVE,		42 },
 193	{ ICE_VXLAN_GPE,	42 },
 194	{ ICE_MAC_IL,		50 },
 195	{ ICE_ETYPE_IL,		62 },
 196	{ ICE_IPV4_IL,		64 },
 197	{ ICE_TCP_IL,		84 },
 198	{ ICE_PROTOCOL_LAST,	0 },
 199};
 200
 201ICE_DECLARE_PKT_TEMPLATE(udp_tun_tcp) = {
 202	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
 203	0x00, 0x00, 0x00, 0x00,
 204	0x00, 0x00, 0x00, 0x00,
 205
 206	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 207
 208	0x45, 0x00, 0x00, 0x5a, /* ICE_IPV4_OFOS 14 */
 209	0x00, 0x01, 0x00, 0x00,
 210	0x40, 0x11, 0x00, 0x00,
 211	0x00, 0x00, 0x00, 0x00,
 212	0x00, 0x00, 0x00, 0x00,
 213
 214	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
 215	0x00, 0x46, 0x00, 0x00,
 216
 217	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
 218	0x00, 0x00, 0x00, 0x00,
 219
 220	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
 221	0x00, 0x00, 0x00, 0x00,
 222	0x00, 0x00, 0x00, 0x00,
 223
 224	0x08, 0x00,		/* ICE_ETYPE_IL 62 */
 225
 226	0x45, 0x00, 0x00, 0x28, /* ICE_IPV4_IL 64 */
 227	0x00, 0x01, 0x00, 0x00,
 228	0x40, 0x06, 0x00, 0x00,
 229	0x00, 0x00, 0x00, 0x00,
 230	0x00, 0x00, 0x00, 0x00,
 231
 232	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 84 */
 233	0x00, 0x00, 0x00, 0x00,
 234	0x00, 0x00, 0x00, 0x00,
 235	0x50, 0x02, 0x20, 0x00,
 236	0x00, 0x00, 0x00, 0x00
 237};
 238
 239ICE_DECLARE_PKT_OFFSETS(udp_tun_udp) = {
 240	{ ICE_MAC_OFOS,		0 },
 241	{ ICE_ETYPE_OL,		12 },
 242	{ ICE_IPV4_OFOS,	14 },
 243	{ ICE_UDP_OF,		34 },
 244	{ ICE_VXLAN,		42 },
 245	{ ICE_GENEVE,		42 },
 246	{ ICE_VXLAN_GPE,	42 },
 247	{ ICE_MAC_IL,		50 },
 248	{ ICE_ETYPE_IL,		62 },
 249	{ ICE_IPV4_IL,		64 },
 250	{ ICE_UDP_ILOS,		84 },
 251	{ ICE_PROTOCOL_LAST,	0 },
 252};
 253
 254ICE_DECLARE_PKT_TEMPLATE(udp_tun_udp) = {
 255	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
 256	0x00, 0x00, 0x00, 0x00,
 257	0x00, 0x00, 0x00, 0x00,
 258
 259	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 260
 261	0x45, 0x00, 0x00, 0x4e, /* ICE_IPV4_OFOS 14 */
 262	0x00, 0x01, 0x00, 0x00,
 263	0x00, 0x11, 0x00, 0x00,
 264	0x00, 0x00, 0x00, 0x00,
 265	0x00, 0x00, 0x00, 0x00,
 266
 267	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
 268	0x00, 0x3a, 0x00, 0x00,
 269
 270	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
 271	0x00, 0x00, 0x00, 0x00,
 272
 273	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
 274	0x00, 0x00, 0x00, 0x00,
 275	0x00, 0x00, 0x00, 0x00,
 276
 277	0x08, 0x00,		/* ICE_ETYPE_IL 62 */
 278
 279	0x45, 0x00, 0x00, 0x1c, /* ICE_IPV4_IL 64 */
 280	0x00, 0x01, 0x00, 0x00,
 281	0x00, 0x11, 0x00, 0x00,
 282	0x00, 0x00, 0x00, 0x00,
 283	0x00, 0x00, 0x00, 0x00,
 284
 285	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 84 */
 286	0x00, 0x08, 0x00, 0x00,
 287};
 288
 289ICE_DECLARE_PKT_OFFSETS(gre_ipv6_tcp) = {
 290	{ ICE_MAC_OFOS,		0 },
 291	{ ICE_ETYPE_OL,		12 },
 292	{ ICE_IPV4_OFOS,	14 },
 293	{ ICE_NVGRE,		34 },
 294	{ ICE_MAC_IL,		42 },
 295	{ ICE_ETYPE_IL,		54 },
 296	{ ICE_IPV6_IL,		56 },
 297	{ ICE_TCP_IL,		96 },
 298	{ ICE_PROTOCOL_LAST,	0 },
 299};
 300
 301ICE_DECLARE_PKT_TEMPLATE(gre_ipv6_tcp) = {
 302	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 303	0x00, 0x00, 0x00, 0x00,
 304	0x00, 0x00, 0x00, 0x00,
 305
 306	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 307
 308	0x45, 0x00, 0x00, 0x66, /* ICE_IPV4_OFOS 14 */
 309	0x00, 0x00, 0x00, 0x00,
 310	0x00, 0x2F, 0x00, 0x00,
 311	0x00, 0x00, 0x00, 0x00,
 312	0x00, 0x00, 0x00, 0x00,
 313
 314	0x80, 0x00, 0x65, 0x58, /* ICE_NVGRE 34 */
 315	0x00, 0x00, 0x00, 0x00,
 316
 317	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 42 */
 318	0x00, 0x00, 0x00, 0x00,
 319	0x00, 0x00, 0x00, 0x00,
 320
 321	0x86, 0xdd,		/* ICE_ETYPE_IL 54 */
 322
 323	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 56 */
 324	0x00, 0x08, 0x06, 0x40,
 325	0x00, 0x00, 0x00, 0x00,
 326	0x00, 0x00, 0x00, 0x00,
 327	0x00, 0x00, 0x00, 0x00,
 328	0x00, 0x00, 0x00, 0x00,
 329	0x00, 0x00, 0x00, 0x00,
 330	0x00, 0x00, 0x00, 0x00,
 331	0x00, 0x00, 0x00, 0x00,
 332	0x00, 0x00, 0x00, 0x00,
 333
 334	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 96 */
 335	0x00, 0x00, 0x00, 0x00,
 336	0x00, 0x00, 0x00, 0x00,
 337	0x50, 0x02, 0x20, 0x00,
 338	0x00, 0x00, 0x00, 0x00
 339};
 340
 341ICE_DECLARE_PKT_OFFSETS(gre_ipv6_udp) = {
 342	{ ICE_MAC_OFOS,		0 },
 343	{ ICE_ETYPE_OL,		12 },
 344	{ ICE_IPV4_OFOS,	14 },
 345	{ ICE_NVGRE,		34 },
 346	{ ICE_MAC_IL,		42 },
 347	{ ICE_ETYPE_IL,		54 },
 348	{ ICE_IPV6_IL,		56 },
 349	{ ICE_UDP_ILOS,		96 },
 350	{ ICE_PROTOCOL_LAST,	0 },
 351};
 352
 353ICE_DECLARE_PKT_TEMPLATE(gre_ipv6_udp) = {
 354	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 355	0x00, 0x00, 0x00, 0x00,
 356	0x00, 0x00, 0x00, 0x00,
 357
 358	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 359
 360	0x45, 0x00, 0x00, 0x5a, /* ICE_IPV4_OFOS 14 */
 361	0x00, 0x00, 0x00, 0x00,
 362	0x00, 0x2F, 0x00, 0x00,
 363	0x00, 0x00, 0x00, 0x00,
 364	0x00, 0x00, 0x00, 0x00,
 365
 366	0x80, 0x00, 0x65, 0x58, /* ICE_NVGRE 34 */
 367	0x00, 0x00, 0x00, 0x00,
 368
 369	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 42 */
 370	0x00, 0x00, 0x00, 0x00,
 371	0x00, 0x00, 0x00, 0x00,
 372
 373	0x86, 0xdd,		/* ICE_ETYPE_IL 54 */
 374
 375	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 56 */
 376	0x00, 0x08, 0x11, 0x40,
 377	0x00, 0x00, 0x00, 0x00,
 378	0x00, 0x00, 0x00, 0x00,
 379	0x00, 0x00, 0x00, 0x00,
 380	0x00, 0x00, 0x00, 0x00,
 381	0x00, 0x00, 0x00, 0x00,
 382	0x00, 0x00, 0x00, 0x00,
 383	0x00, 0x00, 0x00, 0x00,
 384	0x00, 0x00, 0x00, 0x00,
 385
 386	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 96 */
 387	0x00, 0x08, 0x00, 0x00,
 388};
 389
 390ICE_DECLARE_PKT_OFFSETS(udp_tun_ipv6_tcp) = {
 391	{ ICE_MAC_OFOS,		0 },
 392	{ ICE_ETYPE_OL,		12 },
 393	{ ICE_IPV4_OFOS,	14 },
 394	{ ICE_UDP_OF,		34 },
 395	{ ICE_VXLAN,		42 },
 396	{ ICE_GENEVE,		42 },
 397	{ ICE_VXLAN_GPE,	42 },
 398	{ ICE_MAC_IL,		50 },
 399	{ ICE_ETYPE_IL,		62 },
 400	{ ICE_IPV6_IL,		64 },
 401	{ ICE_TCP_IL,		104 },
 402	{ ICE_PROTOCOL_LAST,	0 },
 403};
 404
 405ICE_DECLARE_PKT_TEMPLATE(udp_tun_ipv6_tcp) = {
 406	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
 407	0x00, 0x00, 0x00, 0x00,
 408	0x00, 0x00, 0x00, 0x00,
 409
 410	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 411
 412	0x45, 0x00, 0x00, 0x6e, /* ICE_IPV4_OFOS 14 */
 413	0x00, 0x01, 0x00, 0x00,
 414	0x40, 0x11, 0x00, 0x00,
 415	0x00, 0x00, 0x00, 0x00,
 416	0x00, 0x00, 0x00, 0x00,
 417
 418	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
 419	0x00, 0x5a, 0x00, 0x00,
 420
 421	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
 422	0x00, 0x00, 0x00, 0x00,
 423
 424	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
 425	0x00, 0x00, 0x00, 0x00,
 426	0x00, 0x00, 0x00, 0x00,
 427
 428	0x86, 0xdd,		/* ICE_ETYPE_IL 62 */
 429
 430	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 64 */
 431	0x00, 0x08, 0x06, 0x40,
 432	0x00, 0x00, 0x00, 0x00,
 433	0x00, 0x00, 0x00, 0x00,
 434	0x00, 0x00, 0x00, 0x00,
 435	0x00, 0x00, 0x00, 0x00,
 436	0x00, 0x00, 0x00, 0x00,
 437	0x00, 0x00, 0x00, 0x00,
 438	0x00, 0x00, 0x00, 0x00,
 439	0x00, 0x00, 0x00, 0x00,
 440
 441	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 104 */
 442	0x00, 0x00, 0x00, 0x00,
 443	0x00, 0x00, 0x00, 0x00,
 444	0x50, 0x02, 0x20, 0x00,
 445	0x00, 0x00, 0x00, 0x00
 446};
 447
 448ICE_DECLARE_PKT_OFFSETS(udp_tun_ipv6_udp) = {
 449	{ ICE_MAC_OFOS,		0 },
 450	{ ICE_ETYPE_OL,		12 },
 451	{ ICE_IPV4_OFOS,	14 },
 452	{ ICE_UDP_OF,		34 },
 453	{ ICE_VXLAN,		42 },
 454	{ ICE_GENEVE,		42 },
 455	{ ICE_VXLAN_GPE,	42 },
 456	{ ICE_MAC_IL,		50 },
 457	{ ICE_ETYPE_IL,		62 },
 458	{ ICE_IPV6_IL,		64 },
 459	{ ICE_UDP_ILOS,		104 },
 460	{ ICE_PROTOCOL_LAST,	0 },
 461};
 462
 463ICE_DECLARE_PKT_TEMPLATE(udp_tun_ipv6_udp) = {
 464	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
 465	0x00, 0x00, 0x00, 0x00,
 466	0x00, 0x00, 0x00, 0x00,
 467
 468	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 469
 470	0x45, 0x00, 0x00, 0x62, /* ICE_IPV4_OFOS 14 */
 471	0x00, 0x01, 0x00, 0x00,
 472	0x00, 0x11, 0x00, 0x00,
 473	0x00, 0x00, 0x00, 0x00,
 474	0x00, 0x00, 0x00, 0x00,
 475
 476	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
 477	0x00, 0x4e, 0x00, 0x00,
 478
 479	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
 480	0x00, 0x00, 0x00, 0x00,
 481
 482	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
 483	0x00, 0x00, 0x00, 0x00,
 484	0x00, 0x00, 0x00, 0x00,
 485
 486	0x86, 0xdd,		/* ICE_ETYPE_IL 62 */
 487
 488	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 64 */
 489	0x00, 0x08, 0x11, 0x40,
 490	0x00, 0x00, 0x00, 0x00,
 491	0x00, 0x00, 0x00, 0x00,
 492	0x00, 0x00, 0x00, 0x00,
 493	0x00, 0x00, 0x00, 0x00,
 494	0x00, 0x00, 0x00, 0x00,
 495	0x00, 0x00, 0x00, 0x00,
 496	0x00, 0x00, 0x00, 0x00,
 497	0x00, 0x00, 0x00, 0x00,
 498
 499	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 104 */
 500	0x00, 0x08, 0x00, 0x00,
 501};
 502
 503/* offset info for MAC + IPv4 + UDP dummy packet */
 504ICE_DECLARE_PKT_OFFSETS(udp) = {
 505	{ ICE_MAC_OFOS,		0 },
 506	{ ICE_ETYPE_OL,		12 },
 507	{ ICE_IPV4_OFOS,	14 },
 508	{ ICE_UDP_ILOS,		34 },
 509	{ ICE_PROTOCOL_LAST,	0 },
 510};
 511
 512/* Dummy packet for MAC + IPv4 + UDP */
 513ICE_DECLARE_PKT_TEMPLATE(udp) = {
 514	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 515	0x00, 0x00, 0x00, 0x00,
 516	0x00, 0x00, 0x00, 0x00,
 517
 518	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 519
 520	0x45, 0x00, 0x00, 0x1c, /* ICE_IPV4_OFOS 14 */
 521	0x00, 0x01, 0x00, 0x00,
 522	0x00, 0x11, 0x00, 0x00,
 523	0x00, 0x00, 0x00, 0x00,
 524	0x00, 0x00, 0x00, 0x00,
 525
 526	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 34 */
 527	0x00, 0x08, 0x00, 0x00,
 528
 529	0x00, 0x00,	/* 2 bytes for 4 byte alignment */
 530};
 531
 532/* offset info for MAC + IPv4 + TCP dummy packet */
 533ICE_DECLARE_PKT_OFFSETS(tcp) = {
 534	{ ICE_MAC_OFOS,		0 },
 535	{ ICE_ETYPE_OL,		12 },
 536	{ ICE_IPV4_OFOS,	14 },
 537	{ ICE_TCP_IL,		34 },
 538	{ ICE_PROTOCOL_LAST,	0 },
 539};
 540
 541/* Dummy packet for MAC + IPv4 + TCP */
 542ICE_DECLARE_PKT_TEMPLATE(tcp) = {
 543	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 544	0x00, 0x00, 0x00, 0x00,
 545	0x00, 0x00, 0x00, 0x00,
 546
 547	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 548
 549	0x45, 0x00, 0x00, 0x28, /* ICE_IPV4_OFOS 14 */
 550	0x00, 0x01, 0x00, 0x00,
 551	0x00, 0x06, 0x00, 0x00,
 552	0x00, 0x00, 0x00, 0x00,
 553	0x00, 0x00, 0x00, 0x00,
 554
 555	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 34 */
 556	0x00, 0x00, 0x00, 0x00,
 557	0x00, 0x00, 0x00, 0x00,
 558	0x50, 0x00, 0x00, 0x00,
 559	0x00, 0x00, 0x00, 0x00,
 560
 561	0x00, 0x00,	/* 2 bytes for 4 byte alignment */
 562};
 563
 564ICE_DECLARE_PKT_OFFSETS(tcp_ipv6) = {
 565	{ ICE_MAC_OFOS,		0 },
 566	{ ICE_ETYPE_OL,		12 },
 567	{ ICE_IPV6_OFOS,	14 },
 568	{ ICE_TCP_IL,		54 },
 569	{ ICE_PROTOCOL_LAST,	0 },
 570};
 571
 572ICE_DECLARE_PKT_TEMPLATE(tcp_ipv6) = {
 573	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 574	0x00, 0x00, 0x00, 0x00,
 575	0x00, 0x00, 0x00, 0x00,
 576
 577	0x86, 0xDD,		/* ICE_ETYPE_OL 12 */
 578
 579	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 40 */
 580	0x00, 0x14, 0x06, 0x00, /* Next header is TCP */
 581	0x00, 0x00, 0x00, 0x00,
 582	0x00, 0x00, 0x00, 0x00,
 583	0x00, 0x00, 0x00, 0x00,
 584	0x00, 0x00, 0x00, 0x00,
 585	0x00, 0x00, 0x00, 0x00,
 586	0x00, 0x00, 0x00, 0x00,
 587	0x00, 0x00, 0x00, 0x00,
 588	0x00, 0x00, 0x00, 0x00,
 589
 590	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 54 */
 591	0x00, 0x00, 0x00, 0x00,
 592	0x00, 0x00, 0x00, 0x00,
 593	0x50, 0x00, 0x00, 0x00,
 594	0x00, 0x00, 0x00, 0x00,
 595
 596	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 597};
 598
 599/* IPv6 + UDP */
 600ICE_DECLARE_PKT_OFFSETS(udp_ipv6) = {
 601	{ ICE_MAC_OFOS,		0 },
 602	{ ICE_ETYPE_OL,		12 },
 603	{ ICE_IPV6_OFOS,	14 },
 604	{ ICE_UDP_ILOS,		54 },
 605	{ ICE_PROTOCOL_LAST,	0 },
 606};
 607
 608/* IPv6 + UDP dummy packet */
 609ICE_DECLARE_PKT_TEMPLATE(udp_ipv6) = {
 610	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 611	0x00, 0x00, 0x00, 0x00,
 612	0x00, 0x00, 0x00, 0x00,
 613
 614	0x86, 0xDD,		/* ICE_ETYPE_OL 12 */
 615
 616	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 40 */
 617	0x00, 0x10, 0x11, 0x00, /* Next header UDP */
 618	0x00, 0x00, 0x00, 0x00,
 619	0x00, 0x00, 0x00, 0x00,
 620	0x00, 0x00, 0x00, 0x00,
 621	0x00, 0x00, 0x00, 0x00,
 622	0x00, 0x00, 0x00, 0x00,
 623	0x00, 0x00, 0x00, 0x00,
 624	0x00, 0x00, 0x00, 0x00,
 625	0x00, 0x00, 0x00, 0x00,
 626
 627	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 54 */
 628	0x00, 0x10, 0x00, 0x00,
 629
 630	0x00, 0x00, 0x00, 0x00, /* needed for ESP packets */
 631	0x00, 0x00, 0x00, 0x00,
 632
 633	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 634};
 635
 636/* Outer IPv4 + Outer UDP + GTP + Inner IPv4 + Inner TCP */
 637ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv4_tcp) = {
 638	{ ICE_MAC_OFOS,		0 },
 639	{ ICE_IPV4_OFOS,	14 },
 640	{ ICE_UDP_OF,		34 },
 641	{ ICE_GTP,		42 },
 642	{ ICE_IPV4_IL,		62 },
 643	{ ICE_TCP_IL,		82 },
 644	{ ICE_PROTOCOL_LAST,	0 },
 645};
 646
 647ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv4_tcp) = {
 648	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 649	0x00, 0x00, 0x00, 0x00,
 650	0x00, 0x00, 0x00, 0x00,
 651	0x08, 0x00,
 652
 653	0x45, 0x00, 0x00, 0x58, /* IP 14 */
 654	0x00, 0x00, 0x00, 0x00,
 655	0x00, 0x11, 0x00, 0x00,
 656	0x00, 0x00, 0x00, 0x00,
 657	0x00, 0x00, 0x00, 0x00,
 658
 659	0x00, 0x00, 0x08, 0x68, /* UDP 34 */
 660	0x00, 0x44, 0x00, 0x00,
 661
 662	0x34, 0xff, 0x00, 0x34, /* ICE_GTP Header 42 */
 663	0x00, 0x00, 0x00, 0x00,
 664	0x00, 0x00, 0x00, 0x85,
 665
 666	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 54 */
 667	0x00, 0x00, 0x00, 0x00,
 668
 669	0x45, 0x00, 0x00, 0x28, /* IP 62 */
 670	0x00, 0x00, 0x00, 0x00,
 671	0x00, 0x06, 0x00, 0x00,
 672	0x00, 0x00, 0x00, 0x00,
 673	0x00, 0x00, 0x00, 0x00,
 674
 675	0x00, 0x00, 0x00, 0x00, /* TCP 82 */
 676	0x00, 0x00, 0x00, 0x00,
 677	0x00, 0x00, 0x00, 0x00,
 678	0x50, 0x00, 0x00, 0x00,
 679	0x00, 0x00, 0x00, 0x00,
 680
 681	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 682};
 683
 684/* Outer IPv4 + Outer UDP + GTP + Inner IPv4 + Inner UDP */
 685ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv4_udp) = {
 686	{ ICE_MAC_OFOS,		0 },
 687	{ ICE_IPV4_OFOS,	14 },
 688	{ ICE_UDP_OF,		34 },
 689	{ ICE_GTP,		42 },
 690	{ ICE_IPV4_IL,		62 },
 691	{ ICE_UDP_ILOS,		82 },
 692	{ ICE_PROTOCOL_LAST,	0 },
 693};
 694
 695ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv4_udp) = {
 696	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 697	0x00, 0x00, 0x00, 0x00,
 698	0x00, 0x00, 0x00, 0x00,
 699	0x08, 0x00,
 700
 701	0x45, 0x00, 0x00, 0x4c, /* IP 14 */
 702	0x00, 0x00, 0x00, 0x00,
 703	0x00, 0x11, 0x00, 0x00,
 704	0x00, 0x00, 0x00, 0x00,
 705	0x00, 0x00, 0x00, 0x00,
 706
 707	0x00, 0x00, 0x08, 0x68, /* UDP 34 */
 708	0x00, 0x38, 0x00, 0x00,
 709
 710	0x34, 0xff, 0x00, 0x28, /* ICE_GTP Header 42 */
 711	0x00, 0x00, 0x00, 0x00,
 712	0x00, 0x00, 0x00, 0x85,
 713
 714	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 54 */
 715	0x00, 0x00, 0x00, 0x00,
 716
 717	0x45, 0x00, 0x00, 0x1c, /* IP 62 */
 718	0x00, 0x00, 0x00, 0x00,
 719	0x00, 0x11, 0x00, 0x00,
 720	0x00, 0x00, 0x00, 0x00,
 721	0x00, 0x00, 0x00, 0x00,
 722
 723	0x00, 0x00, 0x00, 0x00, /* UDP 82 */
 724	0x00, 0x08, 0x00, 0x00,
 725
 726	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 727};
 728
 729/* Outer IPv6 + Outer UDP + GTP + Inner IPv4 + Inner TCP */
 730ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv6_tcp) = {
 731	{ ICE_MAC_OFOS,		0 },
 732	{ ICE_IPV4_OFOS,	14 },
 733	{ ICE_UDP_OF,		34 },
 734	{ ICE_GTP,		42 },
 735	{ ICE_IPV6_IL,		62 },
 736	{ ICE_TCP_IL,		102 },
 737	{ ICE_PROTOCOL_LAST,	0 },
 738};
 739
 740ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv6_tcp) = {
 741	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 742	0x00, 0x00, 0x00, 0x00,
 743	0x00, 0x00, 0x00, 0x00,
 744	0x08, 0x00,
 745
 746	0x45, 0x00, 0x00, 0x6c, /* IP 14 */
 747	0x00, 0x00, 0x00, 0x00,
 748	0x00, 0x11, 0x00, 0x00,
 749	0x00, 0x00, 0x00, 0x00,
 750	0x00, 0x00, 0x00, 0x00,
 751
 752	0x00, 0x00, 0x08, 0x68, /* UDP 34 */
 753	0x00, 0x58, 0x00, 0x00,
 754
 755	0x34, 0xff, 0x00, 0x48, /* ICE_GTP Header 42 */
 756	0x00, 0x00, 0x00, 0x00,
 757	0x00, 0x00, 0x00, 0x85,
 758
 759	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 54 */
 760	0x00, 0x00, 0x00, 0x00,
 761
 762	0x60, 0x00, 0x00, 0x00, /* IPv6 62 */
 763	0x00, 0x14, 0x06, 0x00,
 764	0x00, 0x00, 0x00, 0x00,
 765	0x00, 0x00, 0x00, 0x00,
 766	0x00, 0x00, 0x00, 0x00,
 767	0x00, 0x00, 0x00, 0x00,
 768	0x00, 0x00, 0x00, 0x00,
 769	0x00, 0x00, 0x00, 0x00,
 770	0x00, 0x00, 0x00, 0x00,
 771	0x00, 0x00, 0x00, 0x00,
 772
 773	0x00, 0x00, 0x00, 0x00, /* TCP 102 */
 774	0x00, 0x00, 0x00, 0x00,
 775	0x00, 0x00, 0x00, 0x00,
 776	0x50, 0x00, 0x00, 0x00,
 777	0x00, 0x00, 0x00, 0x00,
 778
 779	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 780};
 781
 782ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv6_udp) = {
 783	{ ICE_MAC_OFOS,		0 },
 784	{ ICE_IPV4_OFOS,	14 },
 785	{ ICE_UDP_OF,		34 },
 786	{ ICE_GTP,		42 },
 787	{ ICE_IPV6_IL,		62 },
 788	{ ICE_UDP_ILOS,		102 },
 789	{ ICE_PROTOCOL_LAST,	0 },
 790};
 791
 792ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv6_udp) = {
 793	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 794	0x00, 0x00, 0x00, 0x00,
 795	0x00, 0x00, 0x00, 0x00,
 796	0x08, 0x00,
 797
 798	0x45, 0x00, 0x00, 0x60, /* IP 14 */
 799	0x00, 0x00, 0x00, 0x00,
 800	0x00, 0x11, 0x00, 0x00,
 801	0x00, 0x00, 0x00, 0x00,
 802	0x00, 0x00, 0x00, 0x00,
 803
 804	0x00, 0x00, 0x08, 0x68, /* UDP 34 */
 805	0x00, 0x4c, 0x00, 0x00,
 806
 807	0x34, 0xff, 0x00, 0x3c, /* ICE_GTP Header 42 */
 808	0x00, 0x00, 0x00, 0x00,
 809	0x00, 0x00, 0x00, 0x85,
 810
 811	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 54 */
 812	0x00, 0x00, 0x00, 0x00,
 813
 814	0x60, 0x00, 0x00, 0x00, /* IPv6 62 */
 815	0x00, 0x08, 0x11, 0x00,
 816	0x00, 0x00, 0x00, 0x00,
 817	0x00, 0x00, 0x00, 0x00,
 818	0x00, 0x00, 0x00, 0x00,
 819	0x00, 0x00, 0x00, 0x00,
 820	0x00, 0x00, 0x00, 0x00,
 821	0x00, 0x00, 0x00, 0x00,
 822	0x00, 0x00, 0x00, 0x00,
 823	0x00, 0x00, 0x00, 0x00,
 824
 825	0x00, 0x00, 0x00, 0x00, /* UDP 102 */
 826	0x00, 0x08, 0x00, 0x00,
 827
 828	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 829};
 830
 831ICE_DECLARE_PKT_OFFSETS(ipv6_gtpu_ipv4_tcp) = {
 832	{ ICE_MAC_OFOS,		0 },
 833	{ ICE_IPV6_OFOS,	14 },
 834	{ ICE_UDP_OF,		54 },
 835	{ ICE_GTP,		62 },
 836	{ ICE_IPV4_IL,		82 },
 837	{ ICE_TCP_IL,		102 },
 838	{ ICE_PROTOCOL_LAST,	0 },
 839};
 840
 841ICE_DECLARE_PKT_TEMPLATE(ipv6_gtpu_ipv4_tcp) = {
 842	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 843	0x00, 0x00, 0x00, 0x00,
 844	0x00, 0x00, 0x00, 0x00,
 845	0x86, 0xdd,
 846
 847	0x60, 0x00, 0x00, 0x00, /* IPv6 14 */
 848	0x00, 0x44, 0x11, 0x00,
 849	0x00, 0x00, 0x00, 0x00,
 850	0x00, 0x00, 0x00, 0x00,
 851	0x00, 0x00, 0x00, 0x00,
 852	0x00, 0x00, 0x00, 0x00,
 853	0x00, 0x00, 0x00, 0x00,
 854	0x00, 0x00, 0x00, 0x00,
 855	0x00, 0x00, 0x00, 0x00,
 856	0x00, 0x00, 0x00, 0x00,
 857
 858	0x00, 0x00, 0x08, 0x68, /* UDP 54 */
 859	0x00, 0x44, 0x00, 0x00,
 860
 861	0x34, 0xff, 0x00, 0x34, /* ICE_GTP Header 62 */
 862	0x00, 0x00, 0x00, 0x00,
 863	0x00, 0x00, 0x00, 0x85,
 864
 865	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 74 */
 866	0x00, 0x00, 0x00, 0x00,
 867
 868	0x45, 0x00, 0x00, 0x28, /* IP 82 */
 869	0x00, 0x00, 0x00, 0x00,
 870	0x00, 0x06, 0x00, 0x00,
 871	0x00, 0x00, 0x00, 0x00,
 872	0x00, 0x00, 0x00, 0x00,
 873
 874	0x00, 0x00, 0x00, 0x00, /* TCP 102 */
 875	0x00, 0x00, 0x00, 0x00,
 876	0x00, 0x00, 0x00, 0x00,
 877	0x50, 0x00, 0x00, 0x00,
 878	0x00, 0x00, 0x00, 0x00,
 879
 880	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 881};
 882
 883ICE_DECLARE_PKT_OFFSETS(ipv6_gtpu_ipv4_udp) = {
 884	{ ICE_MAC_OFOS,		0 },
 885	{ ICE_IPV6_OFOS,	14 },
 886	{ ICE_UDP_OF,		54 },
 887	{ ICE_GTP,		62 },
 888	{ ICE_IPV4_IL,		82 },
 889	{ ICE_UDP_ILOS,		102 },
 890	{ ICE_PROTOCOL_LAST,	0 },
 891};
 892
 893ICE_DECLARE_PKT_TEMPLATE(ipv6_gtpu_ipv4_udp) = {
 894	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 895	0x00, 0x00, 0x00, 0x00,
 896	0x00, 0x00, 0x00, 0x00,
 897	0x86, 0xdd,
 898
 899	0x60, 0x00, 0x00, 0x00, /* IPv6 14 */
 900	0x00, 0x38, 0x11, 0x00,
 901	0x00, 0x00, 0x00, 0x00,
 902	0x00, 0x00, 0x00, 0x00,
 903	0x00, 0x00, 0x00, 0x00,
 904	0x00, 0x00, 0x00, 0x00,
 905	0x00, 0x00, 0x00, 0x00,
 906	0x00, 0x00, 0x00, 0x00,
 907	0x00, 0x00, 0x00, 0x00,
 908	0x00, 0x00, 0x00, 0x00,
 909
 910	0x00, 0x00, 0x08, 0x68, /* UDP 54 */
 911	0x00, 0x38, 0x00, 0x00,
 912
 913	0x34, 0xff, 0x00, 0x28, /* ICE_GTP Header 62 */
 914	0x00, 0x00, 0x00, 0x00,
 915	0x00, 0x00, 0x00, 0x85,
 916
 917	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 74 */
 918	0x00, 0x00, 0x00, 0x00,
 919
 920	0x45, 0x00, 0x00, 0x1c, /* IP 82 */
 921	0x00, 0x00, 0x00, 0x00,
 922	0x00, 0x11, 0x00, 0x00,
 923	0x00, 0x00, 0x00, 0x00,
 924	0x00, 0x00, 0x00, 0x00,
 925
 926	0x00, 0x00, 0x00, 0x00, /* UDP 102 */
 927	0x00, 0x08, 0x00, 0x00,
 928
 929	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 930};
 931
 932ICE_DECLARE_PKT_OFFSETS(ipv6_gtpu_ipv6_tcp) = {
 933	{ ICE_MAC_OFOS,		0 },
 934	{ ICE_IPV6_OFOS,	14 },
 935	{ ICE_UDP_OF,		54 },
 936	{ ICE_GTP,		62 },
 937	{ ICE_IPV6_IL,		82 },
 938	{ ICE_TCP_IL,		122 },
 939	{ ICE_PROTOCOL_LAST,	0 },
 940};
 941
 942ICE_DECLARE_PKT_TEMPLATE(ipv6_gtpu_ipv6_tcp) = {
 943	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 944	0x00, 0x00, 0x00, 0x00,
 945	0x00, 0x00, 0x00, 0x00,
 946	0x86, 0xdd,
 947
 948	0x60, 0x00, 0x00, 0x00, /* IPv6 14 */
 949	0x00, 0x58, 0x11, 0x00,
 950	0x00, 0x00, 0x00, 0x00,
 951	0x00, 0x00, 0x00, 0x00,
 952	0x00, 0x00, 0x00, 0x00,
 953	0x00, 0x00, 0x00, 0x00,
 954	0x00, 0x00, 0x00, 0x00,
 955	0x00, 0x00, 0x00, 0x00,
 956	0x00, 0x00, 0x00, 0x00,
 957	0x00, 0x00, 0x00, 0x00,
 958
 959	0x00, 0x00, 0x08, 0x68, /* UDP 54 */
 960	0x00, 0x58, 0x00, 0x00,
 961
 962	0x34, 0xff, 0x00, 0x48, /* ICE_GTP Header 62 */
 963	0x00, 0x00, 0x00, 0x00,
 964	0x00, 0x00, 0x00, 0x85,
 965
 966	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 74 */
 967	0x00, 0x00, 0x00, 0x00,
 968
 969	0x60, 0x00, 0x00, 0x00, /* IPv6 82 */
 970	0x00, 0x14, 0x06, 0x00,
 971	0x00, 0x00, 0x00, 0x00,
 972	0x00, 0x00, 0x00, 0x00,
 973	0x00, 0x00, 0x00, 0x00,
 974	0x00, 0x00, 0x00, 0x00,
 975	0x00, 0x00, 0x00, 0x00,
 976	0x00, 0x00, 0x00, 0x00,
 977	0x00, 0x00, 0x00, 0x00,
 978	0x00, 0x00, 0x00, 0x00,
 979
 980	0x00, 0x00, 0x00, 0x00, /* TCP 122 */
 981	0x00, 0x00, 0x00, 0x00,
 982	0x00, 0x00, 0x00, 0x00,
 983	0x50, 0x00, 0x00, 0x00,
 984	0x00, 0x00, 0x00, 0x00,
 985
 986	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 987};
 988
 989ICE_DECLARE_PKT_OFFSETS(ipv6_gtpu_ipv6_udp) = {
 990	{ ICE_MAC_OFOS,		0 },
 991	{ ICE_IPV6_OFOS,	14 },
 992	{ ICE_UDP_OF,		54 },
 993	{ ICE_GTP,		62 },
 994	{ ICE_IPV6_IL,		82 },
 995	{ ICE_UDP_ILOS,		122 },
 996	{ ICE_PROTOCOL_LAST,	0 },
 997};
 998
 999ICE_DECLARE_PKT_TEMPLATE(ipv6_gtpu_ipv6_udp) = {
1000	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
1001	0x00, 0x00, 0x00, 0x00,
1002	0x00, 0x00, 0x00, 0x00,
1003	0x86, 0xdd,
1004
1005	0x60, 0x00, 0x00, 0x00, /* IPv6 14 */
1006	0x00, 0x4c, 0x11, 0x00,
1007	0x00, 0x00, 0x00, 0x00,
1008	0x00, 0x00, 0x00, 0x00,
1009	0x00, 0x00, 0x00, 0x00,
1010	0x00, 0x00, 0x00, 0x00,
1011	0x00, 0x00, 0x00, 0x00,
1012	0x00, 0x00, 0x00, 0x00,
1013	0x00, 0x00, 0x00, 0x00,
1014	0x00, 0x00, 0x00, 0x00,
1015
1016	0x00, 0x00, 0x08, 0x68, /* UDP 54 */
1017	0x00, 0x4c, 0x00, 0x00,
1018
1019	0x34, 0xff, 0x00, 0x3c, /* ICE_GTP Header 62 */
1020	0x00, 0x00, 0x00, 0x00,
1021	0x00, 0x00, 0x00, 0x85,
1022
1023	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 74 */
1024	0x00, 0x00, 0x00, 0x00,
1025
1026	0x60, 0x00, 0x00, 0x00, /* IPv6 82 */
1027	0x00, 0x08, 0x11, 0x00,
1028	0x00, 0x00, 0x00, 0x00,
1029	0x00, 0x00, 0x00, 0x00,
1030	0x00, 0x00, 0x00, 0x00,
1031	0x00, 0x00, 0x00, 0x00,
1032	0x00, 0x00, 0x00, 0x00,
1033	0x00, 0x00, 0x00, 0x00,
1034	0x00, 0x00, 0x00, 0x00,
1035	0x00, 0x00, 0x00, 0x00,
1036
1037	0x00, 0x00, 0x00, 0x00, /* UDP 122 */
1038	0x00, 0x08, 0x00, 0x00,
1039
1040	0x00, 0x00, /* 2 bytes for 4 byte alignment */
1041};
1042
1043ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv4) = {
1044	{ ICE_MAC_OFOS,		0 },
1045	{ ICE_IPV4_OFOS,	14 },
1046	{ ICE_UDP_OF,		34 },
1047	{ ICE_GTP_NO_PAY,	42 },
1048	{ ICE_PROTOCOL_LAST,	0 },
1049};
1050
1051ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv4) = {
1052	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1053	0x00, 0x00, 0x00, 0x00,
1054	0x00, 0x00, 0x00, 0x00,
1055	0x08, 0x00,
1056
1057	0x45, 0x00, 0x00, 0x44, /* ICE_IPV4_OFOS 14 */
1058	0x00, 0x00, 0x40, 0x00,
1059	0x40, 0x11, 0x00, 0x00,
1060	0x00, 0x00, 0x00, 0x00,
1061	0x00, 0x00, 0x00, 0x00,
1062
1063	0x08, 0x68, 0x08, 0x68, /* ICE_UDP_OF 34 */
1064	0x00, 0x00, 0x00, 0x00,
1065
1066	0x34, 0xff, 0x00, 0x28, /* ICE_GTP 42 */
1067	0x00, 0x00, 0x00, 0x00,
1068	0x00, 0x00, 0x00, 0x85,
1069
1070	0x02, 0x00, 0x00, 0x00, /* PDU Session extension header */
1071	0x00, 0x00, 0x00, 0x00,
1072
1073	0x45, 0x00, 0x00, 0x14, /* ICE_IPV4_IL 62 */
1074	0x00, 0x00, 0x40, 0x00,
1075	0x40, 0x00, 0x00, 0x00,
1076	0x00, 0x00, 0x00, 0x00,
1077	0x00, 0x00, 0x00, 0x00,
1078	0x00, 0x00,
1079};
1080
1081ICE_DECLARE_PKT_OFFSETS(ipv6_gtp) = {
1082	{ ICE_MAC_OFOS,		0 },
1083	{ ICE_IPV6_OFOS,	14 },
1084	{ ICE_UDP_OF,		54 },
1085	{ ICE_GTP_NO_PAY,	62 },
1086	{ ICE_PROTOCOL_LAST,	0 },
1087};
1088
1089ICE_DECLARE_PKT_TEMPLATE(ipv6_gtp) = {
1090	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1091	0x00, 0x00, 0x00, 0x00,
1092	0x00, 0x00, 0x00, 0x00,
1093	0x86, 0xdd,
1094
1095	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 14 */
1096	0x00, 0x6c, 0x11, 0x00, /* Next header UDP*/
1097	0x00, 0x00, 0x00, 0x00,
1098	0x00, 0x00, 0x00, 0x00,
1099	0x00, 0x00, 0x00, 0x00,
1100	0x00, 0x00, 0x00, 0x00,
1101	0x00, 0x00, 0x00, 0x00,
1102	0x00, 0x00, 0x00, 0x00,
1103	0x00, 0x00, 0x00, 0x00,
1104	0x00, 0x00, 0x00, 0x00,
1105
1106	0x08, 0x68, 0x08, 0x68, /* ICE_UDP_OF 54 */
1107	0x00, 0x00, 0x00, 0x00,
1108
1109	0x30, 0x00, 0x00, 0x28, /* ICE_GTP 62 */
1110	0x00, 0x00, 0x00, 0x00,
1111
1112	0x00, 0x00,
1113};
1114
1115ICE_DECLARE_PKT_OFFSETS(pfcp_session_ipv4) = {
1116	{ ICE_MAC_OFOS,		0 },
1117	{ ICE_ETYPE_OL,		12 },
1118	{ ICE_IPV4_OFOS,	14 },
1119	{ ICE_UDP_ILOS,		34 },
1120	{ ICE_PFCP,		42 },
1121	{ ICE_PROTOCOL_LAST,	0 },
1122};
1123
1124ICE_DECLARE_PKT_TEMPLATE(pfcp_session_ipv4) = {
1125	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1126	0x00, 0x00, 0x00, 0x00,
1127	0x00, 0x00, 0x00, 0x00,
1128
1129	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
1130
1131	0x45, 0x00, 0x00, 0x2c, /* ICE_IPV4_OFOS 14 */
1132	0x00, 0x01, 0x00, 0x00,
1133	0x00, 0x11, 0x00, 0x00,
1134	0x00, 0x00, 0x00, 0x00,
1135	0x00, 0x00, 0x00, 0x00,
1136
1137	0x00, 0x00, 0x22, 0x65, /* ICE_UDP_ILOS 34 */
1138	0x00, 0x18, 0x00, 0x00,
1139
1140	0x21, 0x01, 0x00, 0x0c, /* ICE_PFCP 42 */
1141	0x00, 0x00, 0x00, 0x00,
1142	0x00, 0x00, 0x00, 0x00,
1143	0x00, 0x00, 0x00, 0x00,
1144
1145	0x00, 0x00,		/* 2 bytes for 4 byte alignment */
1146};
1147
1148ICE_DECLARE_PKT_OFFSETS(pfcp_session_ipv6) = {
1149	{ ICE_MAC_OFOS,		0 },
1150	{ ICE_ETYPE_OL,		12 },
1151	{ ICE_IPV6_OFOS,	14 },
1152	{ ICE_UDP_ILOS,		54 },
1153	{ ICE_PFCP,		62 },
1154	{ ICE_PROTOCOL_LAST,	0 },
1155};
1156
1157ICE_DECLARE_PKT_TEMPLATE(pfcp_session_ipv6) = {
1158	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1159	0x00, 0x00, 0x00, 0x00,
1160	0x00, 0x00, 0x00, 0x00,
1161
1162	0x86, 0xdd,		/* ICE_ETYPE_OL 12 */
1163
1164	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 14 */
1165	0x00, 0x10, 0x11, 0x00, /* Next header UDP */
1166	0x00, 0x00, 0x00, 0x00,
1167	0x00, 0x00, 0x00, 0x00,
1168	0x00, 0x00, 0x00, 0x00,
1169	0x00, 0x00, 0x00, 0x00,
1170	0x00, 0x00, 0x00, 0x00,
1171	0x00, 0x00, 0x00, 0x00,
1172	0x00, 0x00, 0x00, 0x00,
1173	0x00, 0x00, 0x00, 0x00,
1174
1175	0x00, 0x00, 0x22, 0x65, /* ICE_UDP_ILOS 54 */
1176	0x00, 0x18, 0x00, 0x00,
1177
1178	0x21, 0x01, 0x00, 0x0c, /* ICE_PFCP 62 */
1179	0x00, 0x00, 0x00, 0x00,
1180	0x00, 0x00, 0x00, 0x00,
1181	0x00, 0x00, 0x00, 0x00,
1182
1183	0x00, 0x00,		/* 2 bytes for 4 byte alignment */
1184};
1185
1186ICE_DECLARE_PKT_OFFSETS(pppoe_ipv4_tcp) = {
1187	{ ICE_MAC_OFOS,		0 },
1188	{ ICE_ETYPE_OL,		12 },
1189	{ ICE_PPPOE,		14 },
1190	{ ICE_IPV4_OFOS,	22 },
1191	{ ICE_TCP_IL,		42 },
1192	{ ICE_PROTOCOL_LAST,	0 },
1193};
1194
1195ICE_DECLARE_PKT_TEMPLATE(pppoe_ipv4_tcp) = {
1196	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1197	0x00, 0x00, 0x00, 0x00,
1198	0x00, 0x00, 0x00, 0x00,
1199
1200	0x88, 0x64,		/* ICE_ETYPE_OL 12 */
1201
1202	0x11, 0x00, 0x00, 0x00, /* ICE_PPPOE 14 */
1203	0x00, 0x16,
1204
1205	0x00, 0x21,		/* PPP Link Layer 20 */
1206
1207	0x45, 0x00, 0x00, 0x28, /* ICE_IPV4_OFOS 22 */
1208	0x00, 0x01, 0x00, 0x00,
1209	0x00, 0x06, 0x00, 0x00,
1210	0x00, 0x00, 0x00, 0x00,
1211	0x00, 0x00, 0x00, 0x00,
1212
1213	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 42 */
1214	0x00, 0x00, 0x00, 0x00,
1215	0x00, 0x00, 0x00, 0x00,
1216	0x50, 0x00, 0x00, 0x00,
1217	0x00, 0x00, 0x00, 0x00,
1218
1219	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1220};
1221
1222ICE_DECLARE_PKT_OFFSETS(pppoe_ipv4_udp) = {
1223	{ ICE_MAC_OFOS,		0 },
1224	{ ICE_ETYPE_OL,		12 },
1225	{ ICE_PPPOE,		14 },
1226	{ ICE_IPV4_OFOS,	22 },
1227	{ ICE_UDP_ILOS,		42 },
1228	{ ICE_PROTOCOL_LAST,	0 },
1229};
1230
1231ICE_DECLARE_PKT_TEMPLATE(pppoe_ipv4_udp) = {
1232	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1233	0x00, 0x00, 0x00, 0x00,
1234	0x00, 0x00, 0x00, 0x00,
1235
1236	0x88, 0x64,		/* ICE_ETYPE_OL 12 */
1237
1238	0x11, 0x00, 0x00, 0x00, /* ICE_PPPOE 14 */
1239	0x00, 0x16,
1240
1241	0x00, 0x21,		/* PPP Link Layer 20 */
1242
1243	0x45, 0x00, 0x00, 0x1c, /* ICE_IPV4_OFOS 22 */
1244	0x00, 0x01, 0x00, 0x00,
1245	0x00, 0x11, 0x00, 0x00,
1246	0x00, 0x00, 0x00, 0x00,
1247	0x00, 0x00, 0x00, 0x00,
1248
1249	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 42 */
1250	0x00, 0x08, 0x00, 0x00,
1251
1252	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1253};
1254
1255ICE_DECLARE_PKT_OFFSETS(pppoe_ipv6_tcp) = {
1256	{ ICE_MAC_OFOS,		0 },
1257	{ ICE_ETYPE_OL,		12 },
1258	{ ICE_PPPOE,		14 },
1259	{ ICE_IPV6_OFOS,	22 },
1260	{ ICE_TCP_IL,		62 },
1261	{ ICE_PROTOCOL_LAST,	0 },
1262};
1263
1264ICE_DECLARE_PKT_TEMPLATE(pppoe_ipv6_tcp) = {
1265	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1266	0x00, 0x00, 0x00, 0x00,
1267	0x00, 0x00, 0x00, 0x00,
1268
1269	0x88, 0x64,		/* ICE_ETYPE_OL 12 */
1270
1271	0x11, 0x00, 0x00, 0x00, /* ICE_PPPOE 14 */
1272	0x00, 0x2a,
1273
1274	0x00, 0x57,		/* PPP Link Layer 20 */
1275
1276	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 22 */
1277	0x00, 0x14, 0x06, 0x00, /* Next header is TCP */
1278	0x00, 0x00, 0x00, 0x00,
1279	0x00, 0x00, 0x00, 0x00,
1280	0x00, 0x00, 0x00, 0x00,
1281	0x00, 0x00, 0x00, 0x00,
1282	0x00, 0x00, 0x00, 0x00,
1283	0x00, 0x00, 0x00, 0x00,
1284	0x00, 0x00, 0x00, 0x00,
1285	0x00, 0x00, 0x00, 0x00,
1286
1287	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 62 */
1288	0x00, 0x00, 0x00, 0x00,
1289	0x00, 0x00, 0x00, 0x00,
1290	0x50, 0x00, 0x00, 0x00,
1291	0x00, 0x00, 0x00, 0x00,
1292
1293	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1294};
1295
1296ICE_DECLARE_PKT_OFFSETS(pppoe_ipv6_udp) = {
1297	{ ICE_MAC_OFOS,		0 },
1298	{ ICE_ETYPE_OL,		12 },
1299	{ ICE_PPPOE,		14 },
1300	{ ICE_IPV6_OFOS,	22 },
1301	{ ICE_UDP_ILOS,		62 },
1302	{ ICE_PROTOCOL_LAST,	0 },
1303};
1304
1305ICE_DECLARE_PKT_TEMPLATE(pppoe_ipv6_udp) = {
1306	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1307	0x00, 0x00, 0x00, 0x00,
1308	0x00, 0x00, 0x00, 0x00,
1309
1310	0x88, 0x64,		/* ICE_ETYPE_OL 12 */
1311
1312	0x11, 0x00, 0x00, 0x00, /* ICE_PPPOE 14 */
1313	0x00, 0x2a,
1314
1315	0x00, 0x57,		/* PPP Link Layer 20 */
1316
1317	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 22 */
1318	0x00, 0x08, 0x11, 0x00, /* Next header UDP*/
1319	0x00, 0x00, 0x00, 0x00,
1320	0x00, 0x00, 0x00, 0x00,
1321	0x00, 0x00, 0x00, 0x00,
1322	0x00, 0x00, 0x00, 0x00,
1323	0x00, 0x00, 0x00, 0x00,
1324	0x00, 0x00, 0x00, 0x00,
1325	0x00, 0x00, 0x00, 0x00,
1326	0x00, 0x00, 0x00, 0x00,
1327
1328	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 62 */
1329	0x00, 0x08, 0x00, 0x00,
1330
1331	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1332};
1333
1334ICE_DECLARE_PKT_OFFSETS(ipv4_l2tpv3) = {
1335	{ ICE_MAC_OFOS,		0 },
1336	{ ICE_ETYPE_OL,		12 },
1337	{ ICE_IPV4_OFOS,	14 },
1338	{ ICE_L2TPV3,		34 },
1339	{ ICE_PROTOCOL_LAST,	0 },
1340};
1341
1342ICE_DECLARE_PKT_TEMPLATE(ipv4_l2tpv3) = {
1343	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1344	0x00, 0x00, 0x00, 0x00,
1345	0x00, 0x00, 0x00, 0x00,
1346
1347	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
1348
1349	0x45, 0x00, 0x00, 0x20, /* ICE_IPV4_IL 14 */
1350	0x00, 0x00, 0x40, 0x00,
1351	0x40, 0x73, 0x00, 0x00,
1352	0x00, 0x00, 0x00, 0x00,
1353	0x00, 0x00, 0x00, 0x00,
1354
1355	0x00, 0x00, 0x00, 0x00, /* ICE_L2TPV3 34 */
1356	0x00, 0x00, 0x00, 0x00,
1357	0x00, 0x00, 0x00, 0x00,
1358	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1359};
1360
1361ICE_DECLARE_PKT_OFFSETS(ipv6_l2tpv3) = {
1362	{ ICE_MAC_OFOS,		0 },
1363	{ ICE_ETYPE_OL,		12 },
1364	{ ICE_IPV6_OFOS,	14 },
1365	{ ICE_L2TPV3,		54 },
1366	{ ICE_PROTOCOL_LAST,	0 },
1367};
1368
1369ICE_DECLARE_PKT_TEMPLATE(ipv6_l2tpv3) = {
1370	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1371	0x00, 0x00, 0x00, 0x00,
1372	0x00, 0x00, 0x00, 0x00,
1373
1374	0x86, 0xDD,		/* ICE_ETYPE_OL 12 */
1375
1376	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 14 */
1377	0x00, 0x0c, 0x73, 0x40,
1378	0x00, 0x00, 0x00, 0x00,
1379	0x00, 0x00, 0x00, 0x00,
1380	0x00, 0x00, 0x00, 0x00,
1381	0x00, 0x00, 0x00, 0x00,
1382	0x00, 0x00, 0x00, 0x00,
1383	0x00, 0x00, 0x00, 0x00,
1384	0x00, 0x00, 0x00, 0x00,
1385	0x00, 0x00, 0x00, 0x00,
1386
1387	0x00, 0x00, 0x00, 0x00, /* ICE_L2TPV3 54 */
1388	0x00, 0x00, 0x00, 0x00,
1389	0x00, 0x00, 0x00, 0x00,
1390	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1391};
1392
1393static const struct ice_dummy_pkt_profile ice_dummy_pkt_profiles[] = {
1394	ICE_PKT_PROFILE(ipv6_gtp, ICE_PKT_TUN_GTPU | ICE_PKT_OUTER_IPV6 |
1395				  ICE_PKT_GTP_NOPAY),
1396	ICE_PKT_PROFILE(ipv6_gtpu_ipv6_udp, ICE_PKT_TUN_GTPU |
1397					    ICE_PKT_OUTER_IPV6 |
1398					    ICE_PKT_INNER_IPV6 |
1399					    ICE_PKT_INNER_UDP),
1400	ICE_PKT_PROFILE(ipv6_gtpu_ipv6_tcp, ICE_PKT_TUN_GTPU |
1401					    ICE_PKT_OUTER_IPV6 |
1402					    ICE_PKT_INNER_IPV6),
1403	ICE_PKT_PROFILE(ipv6_gtpu_ipv4_udp, ICE_PKT_TUN_GTPU |
1404					    ICE_PKT_OUTER_IPV6 |
1405					    ICE_PKT_INNER_UDP),
1406	ICE_PKT_PROFILE(ipv6_gtpu_ipv4_tcp, ICE_PKT_TUN_GTPU |
1407					    ICE_PKT_OUTER_IPV6),
1408	ICE_PKT_PROFILE(ipv4_gtpu_ipv4, ICE_PKT_TUN_GTPU | ICE_PKT_GTP_NOPAY),
1409	ICE_PKT_PROFILE(ipv4_gtpu_ipv6_udp, ICE_PKT_TUN_GTPU |
1410					    ICE_PKT_INNER_IPV6 |
1411					    ICE_PKT_INNER_UDP),
1412	ICE_PKT_PROFILE(ipv4_gtpu_ipv6_tcp, ICE_PKT_TUN_GTPU |
1413					    ICE_PKT_INNER_IPV6),
1414	ICE_PKT_PROFILE(ipv4_gtpu_ipv4_udp, ICE_PKT_TUN_GTPU |
1415					    ICE_PKT_INNER_UDP),
1416	ICE_PKT_PROFILE(ipv4_gtpu_ipv4_tcp, ICE_PKT_TUN_GTPU),
1417	ICE_PKT_PROFILE(ipv6_gtp, ICE_PKT_TUN_GTPC | ICE_PKT_OUTER_IPV6),
1418	ICE_PKT_PROFILE(ipv4_gtpu_ipv4, ICE_PKT_TUN_GTPC),
1419	ICE_PKT_PROFILE(pfcp_session_ipv6, ICE_PKT_PFCP | ICE_PKT_OUTER_IPV6),
1420	ICE_PKT_PROFILE(pfcp_session_ipv4, ICE_PKT_PFCP),
1421	ICE_PKT_PROFILE(pppoe_ipv6_udp, ICE_PKT_PPPOE | ICE_PKT_OUTER_IPV6 |
1422					ICE_PKT_INNER_UDP),
1423	ICE_PKT_PROFILE(pppoe_ipv6_tcp, ICE_PKT_PPPOE | ICE_PKT_OUTER_IPV6),
1424	ICE_PKT_PROFILE(pppoe_ipv4_udp, ICE_PKT_PPPOE | ICE_PKT_INNER_UDP),
1425	ICE_PKT_PROFILE(pppoe_ipv4_tcp, ICE_PKT_PPPOE),
1426	ICE_PKT_PROFILE(gre_ipv6_tcp, ICE_PKT_TUN_NVGRE | ICE_PKT_INNER_IPV6 |
1427				      ICE_PKT_INNER_TCP),
1428	ICE_PKT_PROFILE(gre_tcp, ICE_PKT_TUN_NVGRE | ICE_PKT_INNER_TCP),
1429	ICE_PKT_PROFILE(gre_ipv6_udp, ICE_PKT_TUN_NVGRE | ICE_PKT_INNER_IPV6),
1430	ICE_PKT_PROFILE(gre_udp, ICE_PKT_TUN_NVGRE),
1431	ICE_PKT_PROFILE(udp_tun_ipv6_tcp, ICE_PKT_TUN_UDP |
1432					  ICE_PKT_INNER_IPV6 |
1433					  ICE_PKT_INNER_TCP),
1434	ICE_PKT_PROFILE(ipv6_l2tpv3, ICE_PKT_L2TPV3 | ICE_PKT_OUTER_IPV6),
1435	ICE_PKT_PROFILE(ipv4_l2tpv3, ICE_PKT_L2TPV3),
1436	ICE_PKT_PROFILE(udp_tun_tcp, ICE_PKT_TUN_UDP | ICE_PKT_INNER_TCP),
1437	ICE_PKT_PROFILE(udp_tun_ipv6_udp, ICE_PKT_TUN_UDP |
1438					  ICE_PKT_INNER_IPV6),
1439	ICE_PKT_PROFILE(udp_tun_udp, ICE_PKT_TUN_UDP),
1440	ICE_PKT_PROFILE(udp_ipv6, ICE_PKT_OUTER_IPV6 | ICE_PKT_INNER_UDP),
1441	ICE_PKT_PROFILE(udp, ICE_PKT_INNER_UDP),
1442	ICE_PKT_PROFILE(tcp_ipv6, ICE_PKT_OUTER_IPV6),
1443	ICE_PKT_PROFILE(tcp, 0),
1444};
1445
1446/* this is a recipe to profile association bitmap */
1447static DECLARE_BITMAP(recipe_to_profile[ICE_MAX_NUM_RECIPES],
1448			  ICE_MAX_NUM_PROFILES);
1449
1450/* this is a profile to recipe association bitmap */
1451static DECLARE_BITMAP(profile_to_recipe[ICE_MAX_NUM_PROFILES],
1452			  ICE_MAX_NUM_RECIPES);
1453
1454/**
1455 * ice_init_def_sw_recp - initialize the recipe book keeping tables
1456 * @hw: pointer to the HW struct
1457 *
1458 * Allocate memory for the entire recipe table and initialize the structures/
1459 * entries corresponding to basic recipes.
1460 */
1461int ice_init_def_sw_recp(struct ice_hw *hw)
1462{
1463	struct ice_sw_recipe *recps;
1464	u8 i;
1465
1466	recps = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_NUM_RECIPES,
1467			     sizeof(*recps), GFP_KERNEL);
1468	if (!recps)
1469		return -ENOMEM;
1470
1471	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
1472		recps[i].root_rid = i;
1473		INIT_LIST_HEAD(&recps[i].filt_rules);
1474		INIT_LIST_HEAD(&recps[i].filt_replay_rules);
 
1475		mutex_init(&recps[i].filt_rule_lock);
1476	}
1477
1478	hw->switch_info->recp_list = recps;
1479
1480	return 0;
1481}
1482
1483/**
1484 * ice_aq_get_sw_cfg - get switch configuration
1485 * @hw: pointer to the hardware structure
1486 * @buf: pointer to the result buffer
1487 * @buf_size: length of the buffer available for response
1488 * @req_desc: pointer to requested descriptor
1489 * @num_elems: pointer to number of elements
1490 * @cd: pointer to command details structure or NULL
1491 *
1492 * Get switch configuration (0x0200) to be placed in buf.
1493 * This admin command returns information such as initial VSI/port number
1494 * and switch ID it belongs to.
1495 *
1496 * NOTE: *req_desc is both an input/output parameter.
1497 * The caller of this function first calls this function with *request_desc set
1498 * to 0. If the response from f/w has *req_desc set to 0, all the switch
1499 * configuration information has been returned; if non-zero (meaning not all
1500 * the information was returned), the caller should call this function again
1501 * with *req_desc set to the previous value returned by f/w to get the
1502 * next block of switch configuration information.
1503 *
1504 * *num_elems is output only parameter. This reflects the number of elements
1505 * in response buffer. The caller of this function to use *num_elems while
1506 * parsing the response buffer.
1507 */
1508static int
1509ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp_elem *buf,
1510		  u16 buf_size, u16 *req_desc, u16 *num_elems,
1511		  struct ice_sq_cd *cd)
1512{
1513	struct ice_aqc_get_sw_cfg *cmd;
1514	struct ice_aq_desc desc;
1515	int status;
1516
1517	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
1518	cmd = &desc.params.get_sw_conf;
1519	cmd->element = cpu_to_le16(*req_desc);
1520
1521	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
1522	if (!status) {
1523		*req_desc = le16_to_cpu(cmd->element);
1524		*num_elems = le16_to_cpu(cmd->num_elems);
1525	}
1526
1527	return status;
1528}
1529
1530/**
1531 * ice_aq_add_vsi
1532 * @hw: pointer to the HW struct
1533 * @vsi_ctx: pointer to a VSI context struct
1534 * @cd: pointer to command details structure or NULL
1535 *
1536 * Add a VSI context to the hardware (0x0210)
1537 */
1538static int
1539ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
1540	       struct ice_sq_cd *cd)
1541{
1542	struct ice_aqc_add_update_free_vsi_resp *res;
1543	struct ice_aqc_add_get_update_free_vsi *cmd;
1544	struct ice_aq_desc desc;
1545	int status;
1546
1547	cmd = &desc.params.vsi_cmd;
1548	res = &desc.params.add_update_free_vsi_res;
1549
1550	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
1551
1552	if (!vsi_ctx->alloc_from_pool)
1553		cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num |
1554					   ICE_AQ_VSI_IS_VALID);
1555	cmd->vf_id = vsi_ctx->vf_num;
1556
1557	cmd->vsi_flags = cpu_to_le16(vsi_ctx->flags);
1558
1559	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1560
1561	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
1562				 sizeof(vsi_ctx->info), cd);
1563
1564	if (!status) {
1565		vsi_ctx->vsi_num = le16_to_cpu(res->vsi_num) & ICE_AQ_VSI_NUM_M;
1566		vsi_ctx->vsis_allocd = le16_to_cpu(res->vsi_used);
1567		vsi_ctx->vsis_unallocated = le16_to_cpu(res->vsi_free);
1568	}
1569
1570	return status;
1571}
1572
1573/**
1574 * ice_aq_free_vsi
1575 * @hw: pointer to the HW struct
1576 * @vsi_ctx: pointer to a VSI context struct
1577 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
1578 * @cd: pointer to command details structure or NULL
1579 *
1580 * Free VSI context info from hardware (0x0213)
1581 */
1582static int
1583ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
1584		bool keep_vsi_alloc, struct ice_sq_cd *cd)
1585{
1586	struct ice_aqc_add_update_free_vsi_resp *resp;
1587	struct ice_aqc_add_get_update_free_vsi *cmd;
1588	struct ice_aq_desc desc;
1589	int status;
1590
1591	cmd = &desc.params.vsi_cmd;
1592	resp = &desc.params.add_update_free_vsi_res;
1593
1594	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
1595
1596	cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
1597	if (keep_vsi_alloc)
1598		cmd->cmd_flags = cpu_to_le16(ICE_AQ_VSI_KEEP_ALLOC);
1599
1600	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1601	if (!status) {
1602		vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
1603		vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
1604	}
1605
1606	return status;
1607}
1608
1609/**
1610 * ice_aq_update_vsi
1611 * @hw: pointer to the HW struct
1612 * @vsi_ctx: pointer to a VSI context struct
1613 * @cd: pointer to command details structure or NULL
1614 *
1615 * Update VSI context in the hardware (0x0211)
1616 */
1617static int
1618ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
1619		  struct ice_sq_cd *cd)
1620{
1621	struct ice_aqc_add_update_free_vsi_resp *resp;
1622	struct ice_aqc_add_get_update_free_vsi *cmd;
1623	struct ice_aq_desc desc;
1624	int status;
1625
1626	cmd = &desc.params.vsi_cmd;
1627	resp = &desc.params.add_update_free_vsi_res;
1628
1629	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
1630
1631	cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
1632
1633	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1634
1635	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
1636				 sizeof(vsi_ctx->info), cd);
1637
1638	if (!status) {
1639		vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
1640		vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
1641	}
1642
1643	return status;
1644}
1645
1646/**
1647 * ice_is_vsi_valid - check whether the VSI is valid or not
1648 * @hw: pointer to the HW struct
1649 * @vsi_handle: VSI handle
1650 *
1651 * check whether the VSI is valid or not
1652 */
1653bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
1654{
1655	return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
1656}
1657
1658/**
1659 * ice_get_hw_vsi_num - return the HW VSI number
1660 * @hw: pointer to the HW struct
1661 * @vsi_handle: VSI handle
1662 *
1663 * return the HW VSI number
1664 * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
1665 */
1666u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
1667{
1668	return hw->vsi_ctx[vsi_handle]->vsi_num;
1669}
1670
1671/**
1672 * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
1673 * @hw: pointer to the HW struct
1674 * @vsi_handle: VSI handle
1675 *
1676 * return the VSI context entry for a given VSI handle
1677 */
1678struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
1679{
1680	return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
1681}
1682
1683/**
1684 * ice_save_vsi_ctx - save the VSI context for a given VSI handle
1685 * @hw: pointer to the HW struct
1686 * @vsi_handle: VSI handle
1687 * @vsi: VSI context pointer
1688 *
1689 * save the VSI context entry for a given VSI handle
1690 */
1691static void
1692ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi)
1693{
1694	hw->vsi_ctx[vsi_handle] = vsi;
1695}
1696
1697/**
1698 * ice_clear_vsi_q_ctx - clear VSI queue contexts for all TCs
1699 * @hw: pointer to the HW struct
1700 * @vsi_handle: VSI handle
1701 */
1702static void ice_clear_vsi_q_ctx(struct ice_hw *hw, u16 vsi_handle)
1703{
1704	struct ice_vsi_ctx *vsi = ice_get_vsi_ctx(hw, vsi_handle);
1705	u8 i;
1706
1707	if (!vsi)
1708		return;
1709	ice_for_each_traffic_class(i) {
1710		devm_kfree(ice_hw_to_dev(hw), vsi->lan_q_ctx[i]);
1711		vsi->lan_q_ctx[i] = NULL;
1712		devm_kfree(ice_hw_to_dev(hw), vsi->rdma_q_ctx[i]);
1713		vsi->rdma_q_ctx[i] = NULL;
1714	}
1715}
1716
1717/**
1718 * ice_clear_vsi_ctx - clear the VSI context entry
1719 * @hw: pointer to the HW struct
1720 * @vsi_handle: VSI handle
1721 *
1722 * clear the VSI context entry
1723 */
1724static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
1725{
1726	struct ice_vsi_ctx *vsi;
1727
1728	vsi = ice_get_vsi_ctx(hw, vsi_handle);
1729	if (vsi) {
1730		ice_clear_vsi_q_ctx(hw, vsi_handle);
1731		devm_kfree(ice_hw_to_dev(hw), vsi);
1732		hw->vsi_ctx[vsi_handle] = NULL;
1733	}
1734}
1735
1736/**
1737 * ice_clear_all_vsi_ctx - clear all the VSI context entries
1738 * @hw: pointer to the HW struct
1739 */
1740void ice_clear_all_vsi_ctx(struct ice_hw *hw)
1741{
1742	u16 i;
1743
1744	for (i = 0; i < ICE_MAX_VSI; i++)
1745		ice_clear_vsi_ctx(hw, i);
1746}
1747
1748/**
1749 * ice_add_vsi - add VSI context to the hardware and VSI handle list
1750 * @hw: pointer to the HW struct
1751 * @vsi_handle: unique VSI handle provided by drivers
1752 * @vsi_ctx: pointer to a VSI context struct
1753 * @cd: pointer to command details structure or NULL
1754 *
1755 * Add a VSI context to the hardware also add it into the VSI handle list.
1756 * If this function gets called after reset for existing VSIs then update
1757 * with the new HW VSI number in the corresponding VSI handle list entry.
1758 */
1759int
1760ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
1761	    struct ice_sq_cd *cd)
1762{
1763	struct ice_vsi_ctx *tmp_vsi_ctx;
1764	int status;
1765
1766	if (vsi_handle >= ICE_MAX_VSI)
1767		return -EINVAL;
1768	status = ice_aq_add_vsi(hw, vsi_ctx, cd);
1769	if (status)
1770		return status;
1771	tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1772	if (!tmp_vsi_ctx) {
1773		/* Create a new VSI context */
1774		tmp_vsi_ctx = devm_kzalloc(ice_hw_to_dev(hw),
1775					   sizeof(*tmp_vsi_ctx), GFP_KERNEL);
1776		if (!tmp_vsi_ctx) {
1777			ice_aq_free_vsi(hw, vsi_ctx, false, cd);
1778			return -ENOMEM;
1779		}
1780		*tmp_vsi_ctx = *vsi_ctx;
1781		ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
1782	} else {
1783		/* update with new HW VSI num */
1784		tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
1785	}
1786
1787	return 0;
1788}
1789
1790/**
1791 * ice_free_vsi- free VSI context from hardware and VSI handle list
1792 * @hw: pointer to the HW struct
1793 * @vsi_handle: unique VSI handle
1794 * @vsi_ctx: pointer to a VSI context struct
1795 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
1796 * @cd: pointer to command details structure or NULL
1797 *
1798 * Free VSI context info from hardware as well as from VSI handle list
1799 */
1800int
1801ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
1802	     bool keep_vsi_alloc, struct ice_sq_cd *cd)
1803{
1804	int status;
1805
1806	if (!ice_is_vsi_valid(hw, vsi_handle))
1807		return -EINVAL;
1808	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
1809	status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
1810	if (!status)
1811		ice_clear_vsi_ctx(hw, vsi_handle);
1812	return status;
1813}
1814
1815/**
1816 * ice_update_vsi
1817 * @hw: pointer to the HW struct
1818 * @vsi_handle: unique VSI handle
1819 * @vsi_ctx: pointer to a VSI context struct
1820 * @cd: pointer to command details structure or NULL
1821 *
1822 * Update VSI context in the hardware
1823 */
1824int
1825ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
1826	       struct ice_sq_cd *cd)
1827{
1828	if (!ice_is_vsi_valid(hw, vsi_handle))
1829		return -EINVAL;
1830	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
1831	return ice_aq_update_vsi(hw, vsi_ctx, cd);
1832}
1833
1834/**
1835 * ice_cfg_rdma_fltr - enable/disable RDMA filtering on VSI
1836 * @hw: pointer to HW struct
1837 * @vsi_handle: VSI SW index
1838 * @enable: boolean for enable/disable
1839 */
1840int
1841ice_cfg_rdma_fltr(struct ice_hw *hw, u16 vsi_handle, bool enable)
1842{
1843	struct ice_vsi_ctx *ctx, *cached_ctx;
1844	int status;
1845
1846	cached_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1847	if (!cached_ctx)
1848		return -ENOENT;
1849
1850	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1851	if (!ctx)
1852		return -ENOMEM;
1853
1854	ctx->info.q_opt_rss = cached_ctx->info.q_opt_rss;
1855	ctx->info.q_opt_tc = cached_ctx->info.q_opt_tc;
1856	ctx->info.q_opt_flags = cached_ctx->info.q_opt_flags;
1857
1858	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1859
1860	if (enable)
1861		ctx->info.q_opt_flags |= ICE_AQ_VSI_Q_OPT_PE_FLTR_EN;
1862	else
1863		ctx->info.q_opt_flags &= ~ICE_AQ_VSI_Q_OPT_PE_FLTR_EN;
1864
1865	status = ice_update_vsi(hw, vsi_handle, ctx, NULL);
1866	if (!status) {
1867		cached_ctx->info.q_opt_flags = ctx->info.q_opt_flags;
1868		cached_ctx->info.valid_sections |= ctx->info.valid_sections;
1869	}
1870
1871	kfree(ctx);
1872	return status;
1873}
1874
1875/**
1876 * ice_aq_alloc_free_vsi_list
1877 * @hw: pointer to the HW struct
1878 * @vsi_list_id: VSI list ID returned or used for lookup
1879 * @lkup_type: switch rule filter lookup type
1880 * @opc: switch rules population command type - pass in the command opcode
1881 *
1882 * allocates or free a VSI list resource
1883 */
1884static int
1885ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
1886			   enum ice_sw_lkup_type lkup_type,
1887			   enum ice_adminq_opc opc)
1888{
1889	DEFINE_RAW_FLEX(struct ice_aqc_alloc_free_res_elem, sw_buf, elem, 1);
1890	u16 buf_len = __struct_size(sw_buf);
1891	struct ice_aqc_res_elem *vsi_ele;
1892	int status;
1893
1894	sw_buf->num_elems = cpu_to_le16(1);
1895
1896	if (lkup_type == ICE_SW_LKUP_MAC ||
1897	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
1898	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1899	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1900	    lkup_type == ICE_SW_LKUP_PROMISC ||
1901	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1902	    lkup_type == ICE_SW_LKUP_DFLT ||
1903	    lkup_type == ICE_SW_LKUP_LAST) {
1904		sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
1905	} else if (lkup_type == ICE_SW_LKUP_VLAN) {
1906		if (opc == ice_aqc_opc_alloc_res)
1907			sw_buf->res_type =
1908				cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE |
1909					    ICE_AQC_RES_TYPE_FLAG_SHARED);
1910		else
1911			sw_buf->res_type =
1912				cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
1913	} else {
1914		return -EINVAL;
1915	}
1916
1917	if (opc == ice_aqc_opc_free_res)
1918		sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
1919
1920	status = ice_aq_alloc_free_res(hw, sw_buf, buf_len, opc);
1921	if (status)
1922		return status;
1923
1924	if (opc == ice_aqc_opc_alloc_res) {
1925		vsi_ele = &sw_buf->elem[0];
1926		*vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp);
1927	}
1928
1929	return 0;
1930}
1931
1932/**
1933 * ice_aq_sw_rules - add/update/remove switch rules
1934 * @hw: pointer to the HW struct
1935 * @rule_list: pointer to switch rule population list
1936 * @rule_list_sz: total size of the rule list in bytes
1937 * @num_rules: number of switch rules in the rule_list
1938 * @opc: switch rules population command type - pass in the command opcode
1939 * @cd: pointer to command details structure or NULL
1940 *
1941 * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
1942 */
1943int
1944ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
1945		u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
1946{
1947	struct ice_aq_desc desc;
1948	int status;
1949
1950	if (opc != ice_aqc_opc_add_sw_rules &&
1951	    opc != ice_aqc_opc_update_sw_rules &&
1952	    opc != ice_aqc_opc_remove_sw_rules)
1953		return -EINVAL;
1954
1955	ice_fill_dflt_direct_cmd_desc(&desc, opc);
1956
1957	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1958	desc.params.sw_rules.num_rules_fltr_entry_index =
1959		cpu_to_le16(num_rules);
1960	status = ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
1961	if (opc != ice_aqc_opc_add_sw_rules &&
1962	    hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT)
1963		status = -ENOENT;
1964
1965	if (!status) {
1966		if (opc == ice_aqc_opc_add_sw_rules)
1967			hw->switch_info->rule_cnt += num_rules;
1968		else if (opc == ice_aqc_opc_remove_sw_rules)
1969			hw->switch_info->rule_cnt -= num_rules;
1970	}
1971
1972	trace_ice_aq_sw_rules(hw->switch_info);
1973
1974	return status;
1975}
1976
1977/**
1978 * ice_aq_add_recipe - add switch recipe
1979 * @hw: pointer to the HW struct
1980 * @s_recipe_list: pointer to switch rule population list
1981 * @num_recipes: number of switch recipes in the list
1982 * @cd: pointer to command details structure or NULL
1983 *
1984 * Add(0x0290)
1985 */
1986int
1987ice_aq_add_recipe(struct ice_hw *hw,
1988		  struct ice_aqc_recipe_data_elem *s_recipe_list,
1989		  u16 num_recipes, struct ice_sq_cd *cd)
1990{
1991	struct ice_aqc_add_get_recipe *cmd;
1992	struct ice_aq_desc desc;
1993	u16 buf_size;
1994
1995	cmd = &desc.params.add_get_recipe;
1996	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_recipe);
1997
1998	cmd->num_sub_recipes = cpu_to_le16(num_recipes);
1999	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
2000
2001	buf_size = num_recipes * sizeof(*s_recipe_list);
2002
2003	return ice_aq_send_cmd(hw, &desc, s_recipe_list, buf_size, cd);
2004}
2005
2006/**
2007 * ice_aq_get_recipe - get switch recipe
2008 * @hw: pointer to the HW struct
2009 * @s_recipe_list: pointer to switch rule population list
2010 * @num_recipes: pointer to the number of recipes (input and output)
2011 * @recipe_root: root recipe number of recipe(s) to retrieve
2012 * @cd: pointer to command details structure or NULL
2013 *
2014 * Get(0x0292)
2015 *
2016 * On input, *num_recipes should equal the number of entries in s_recipe_list.
2017 * On output, *num_recipes will equal the number of entries returned in
2018 * s_recipe_list.
2019 *
2020 * The caller must supply enough space in s_recipe_list to hold all possible
2021 * recipes and *num_recipes must equal ICE_MAX_NUM_RECIPES.
2022 */
2023int
2024ice_aq_get_recipe(struct ice_hw *hw,
2025		  struct ice_aqc_recipe_data_elem *s_recipe_list,
2026		  u16 *num_recipes, u16 recipe_root, struct ice_sq_cd *cd)
2027{
2028	struct ice_aqc_add_get_recipe *cmd;
2029	struct ice_aq_desc desc;
2030	u16 buf_size;
2031	int status;
2032
2033	if (*num_recipes != ICE_MAX_NUM_RECIPES)
2034		return -EINVAL;
2035
2036	cmd = &desc.params.add_get_recipe;
2037	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_recipe);
2038
2039	cmd->return_index = cpu_to_le16(recipe_root);
2040	cmd->num_sub_recipes = 0;
2041
2042	buf_size = *num_recipes * sizeof(*s_recipe_list);
2043
2044	status = ice_aq_send_cmd(hw, &desc, s_recipe_list, buf_size, cd);
2045	*num_recipes = le16_to_cpu(cmd->num_sub_recipes);
2046
2047	return status;
2048}
2049
2050/**
2051 * ice_update_recipe_lkup_idx - update a default recipe based on the lkup_idx
2052 * @hw: pointer to the HW struct
2053 * @params: parameters used to update the default recipe
2054 *
2055 * This function only supports updating default recipes and it only supports
2056 * updating a single recipe based on the lkup_idx at a time.
2057 *
2058 * This is done as a read-modify-write operation. First, get the current recipe
2059 * contents based on the recipe's ID. Then modify the field vector index and
2060 * mask if it's valid at the lkup_idx. Finally, use the add recipe AQ to update
2061 * the pre-existing recipe with the modifications.
2062 */
2063int
2064ice_update_recipe_lkup_idx(struct ice_hw *hw,
2065			   struct ice_update_recipe_lkup_idx_params *params)
2066{
2067	struct ice_aqc_recipe_data_elem *rcp_list;
2068	u16 num_recps = ICE_MAX_NUM_RECIPES;
2069	int status;
2070
2071	rcp_list = kcalloc(num_recps, sizeof(*rcp_list), GFP_KERNEL);
2072	if (!rcp_list)
2073		return -ENOMEM;
2074
2075	/* read current recipe list from firmware */
2076	rcp_list->recipe_indx = params->rid;
2077	status = ice_aq_get_recipe(hw, rcp_list, &num_recps, params->rid, NULL);
2078	if (status) {
2079		ice_debug(hw, ICE_DBG_SW, "Failed to get recipe %d, status %d\n",
2080			  params->rid, status);
2081		goto error_out;
2082	}
2083
2084	/* only modify existing recipe's lkup_idx and mask if valid, while
2085	 * leaving all other fields the same, then update the recipe firmware
2086	 */
2087	rcp_list->content.lkup_indx[params->lkup_idx] = params->fv_idx;
2088	if (params->mask_valid)
2089		rcp_list->content.mask[params->lkup_idx] =
2090			cpu_to_le16(params->mask);
2091
2092	if (params->ignore_valid)
2093		rcp_list->content.lkup_indx[params->lkup_idx] |=
2094			ICE_AQ_RECIPE_LKUP_IGNORE;
2095
2096	status = ice_aq_add_recipe(hw, &rcp_list[0], 1, NULL);
2097	if (status)
2098		ice_debug(hw, ICE_DBG_SW, "Failed to update recipe %d lkup_idx %d fv_idx %d mask %d mask_valid %s, status %d\n",
2099			  params->rid, params->lkup_idx, params->fv_idx,
2100			  params->mask, params->mask_valid ? "true" : "false",
2101			  status);
2102
2103error_out:
2104	kfree(rcp_list);
2105	return status;
2106}
2107
2108/**
2109 * ice_aq_map_recipe_to_profile - Map recipe to packet profile
2110 * @hw: pointer to the HW struct
2111 * @profile_id: package profile ID to associate the recipe with
2112 * @r_assoc: Recipe bitmap filled in and need to be returned as response
2113 * @cd: pointer to command details structure or NULL
2114 * Recipe to profile association (0x0291)
2115 */
2116int
2117ice_aq_map_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u64 r_assoc,
2118			     struct ice_sq_cd *cd)
2119{
2120	struct ice_aqc_recipe_to_profile *cmd;
2121	struct ice_aq_desc desc;
2122
2123	cmd = &desc.params.recipe_to_profile;
2124	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_recipe_to_profile);
2125	cmd->profile_id = cpu_to_le16(profile_id);
2126	/* Set the recipe ID bit in the bitmask to let the device know which
2127	 * profile we are associating the recipe to
2128	 */
2129	cmd->recipe_assoc = cpu_to_le64(r_assoc);
2130
2131	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2132}
2133
2134/**
2135 * ice_aq_get_recipe_to_profile - Map recipe to packet profile
2136 * @hw: pointer to the HW struct
2137 * @profile_id: package profile ID to associate the recipe with
2138 * @r_assoc: Recipe bitmap filled in and need to be returned as response
2139 * @cd: pointer to command details structure or NULL
2140 * Associate profile ID with given recipe (0x0293)
2141 */
2142int
2143ice_aq_get_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u64 *r_assoc,
2144			     struct ice_sq_cd *cd)
2145{
2146	struct ice_aqc_recipe_to_profile *cmd;
2147	struct ice_aq_desc desc;
2148	int status;
2149
2150	cmd = &desc.params.recipe_to_profile;
2151	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_recipe_to_profile);
2152	cmd->profile_id = cpu_to_le16(profile_id);
2153
2154	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2155	if (!status)
2156		*r_assoc = le64_to_cpu(cmd->recipe_assoc);
2157
2158	return status;
2159}
2160
2161/**
2162 * ice_init_chk_recipe_reuse_support - check if recipe reuse is supported
2163 * @hw: pointer to the hardware structure
2164 */
2165void ice_init_chk_recipe_reuse_support(struct ice_hw *hw)
2166{
2167	struct ice_nvm_info *nvm = &hw->flash.nvm;
2168
2169	hw->recp_reuse = (nvm->major == 0x4 && nvm->minor >= 0x30) ||
2170			 nvm->major > 0x4;
2171}
2172
2173/**
2174 * ice_alloc_recipe - add recipe resource
2175 * @hw: pointer to the hardware structure
2176 * @rid: recipe ID returned as response to AQ call
2177 */
2178int ice_alloc_recipe(struct ice_hw *hw, u16 *rid)
2179{
2180	DEFINE_RAW_FLEX(struct ice_aqc_alloc_free_res_elem, sw_buf, elem, 1);
2181	u16 buf_len = __struct_size(sw_buf);
2182	u16 res_type;
2183	int status;
2184
2185	sw_buf->num_elems = cpu_to_le16(1);
2186	res_type = FIELD_PREP(ICE_AQC_RES_TYPE_M, ICE_AQC_RES_TYPE_RECIPE);
2187	if (hw->recp_reuse)
2188		res_type |= ICE_AQC_RES_TYPE_FLAG_SUBSCRIBE_SHARED;
2189	else
2190		res_type |= ICE_AQC_RES_TYPE_FLAG_SHARED;
2191	sw_buf->res_type = cpu_to_le16(res_type);
2192	status = ice_aq_alloc_free_res(hw, sw_buf, buf_len,
2193				       ice_aqc_opc_alloc_res);
2194	if (!status) {
2195		*rid = le16_to_cpu(sw_buf->elem[0].e.sw_resp);
2196		hw->switch_info->recp_cnt++;
2197	}
2198
2199	return status;
2200}
2201
2202/**
2203 * ice_free_recipe_res - free recipe resource
2204 * @hw: pointer to the hardware structure
2205 * @rid: recipe ID to free
2206 *
2207 * Return: 0 on success, and others on error
2208 */
2209static int ice_free_recipe_res(struct ice_hw *hw, u16 rid)
2210{
2211	int status;
2212
2213	status = ice_free_hw_res(hw, ICE_AQC_RES_TYPE_RECIPE, 1, &rid);
2214	if (!status)
2215		hw->switch_info->recp_cnt--;
2216
2217	return status;
2218}
2219
2220/**
2221 * ice_release_recipe_res - disassociate and free recipe resource
2222 * @hw: pointer to the hardware structure
2223 * @recp: the recipe struct resource to unassociate and free
2224 *
2225 * Return: 0 on success, and others on error
2226 */
2227static int ice_release_recipe_res(struct ice_hw *hw,
2228				  struct ice_sw_recipe *recp)
2229{
2230	DECLARE_BITMAP(r_bitmap, ICE_MAX_NUM_RECIPES);
2231	struct ice_switch_info *sw = hw->switch_info;
2232	u64 recp_assoc;
2233	u32 rid, prof;
2234	int status;
2235
2236	for_each_set_bit(rid, recp->r_bitmap, ICE_MAX_NUM_RECIPES) {
2237		for_each_set_bit(prof, recipe_to_profile[rid],
2238				 ICE_MAX_NUM_PROFILES) {
2239			status = ice_aq_get_recipe_to_profile(hw, prof,
2240							      &recp_assoc,
2241							      NULL);
2242			if (status)
2243				return status;
2244
2245			bitmap_from_arr64(r_bitmap, &recp_assoc,
2246					  ICE_MAX_NUM_RECIPES);
2247			bitmap_andnot(r_bitmap, r_bitmap, recp->r_bitmap,
2248				      ICE_MAX_NUM_RECIPES);
2249			bitmap_to_arr64(&recp_assoc, r_bitmap,
2250					ICE_MAX_NUM_RECIPES);
2251			ice_aq_map_recipe_to_profile(hw, prof,
2252						     recp_assoc, NULL);
2253
2254			clear_bit(rid, profile_to_recipe[prof]);
2255			clear_bit(prof, recipe_to_profile[rid]);
2256		}
2257
2258		status = ice_free_recipe_res(hw, rid);
2259		if (status)
2260			return status;
2261
2262		sw->recp_list[rid].recp_created = false;
2263		sw->recp_list[rid].adv_rule = false;
2264		memset(&sw->recp_list[rid].lkup_exts, 0,
2265		       sizeof(sw->recp_list[rid].lkup_exts));
2266		clear_bit(rid, recp->r_bitmap);
2267	}
2268
2269	return 0;
2270}
2271
2272/**
2273 * ice_get_recp_to_prof_map - updates recipe to profile mapping
2274 * @hw: pointer to hardware structure
2275 *
2276 * This function is used to populate recipe_to_profile matrix where index to
2277 * this array is the recipe ID and the element is the mapping of which profiles
2278 * is this recipe mapped to.
2279 */
2280static void ice_get_recp_to_prof_map(struct ice_hw *hw)
2281{
2282	DECLARE_BITMAP(r_bitmap, ICE_MAX_NUM_RECIPES);
2283	u64 recp_assoc;
2284	u16 i;
2285
2286	for (i = 0; i < hw->switch_info->max_used_prof_index + 1; i++) {
2287		u16 j;
2288
2289		bitmap_zero(profile_to_recipe[i], ICE_MAX_NUM_RECIPES);
2290		bitmap_zero(r_bitmap, ICE_MAX_NUM_RECIPES);
2291		if (ice_aq_get_recipe_to_profile(hw, i, &recp_assoc, NULL))
2292			continue;
2293		bitmap_from_arr64(r_bitmap, &recp_assoc, ICE_MAX_NUM_RECIPES);
2294		bitmap_copy(profile_to_recipe[i], r_bitmap,
2295			    ICE_MAX_NUM_RECIPES);
2296		for_each_set_bit(j, r_bitmap, ICE_MAX_NUM_RECIPES)
2297			set_bit(i, recipe_to_profile[j]);
2298	}
2299}
2300
2301/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2302 * ice_get_recp_frm_fw - update SW bookkeeping from FW recipe entries
2303 * @hw: pointer to hardware structure
2304 * @recps: struct that we need to populate
2305 * @rid: recipe ID that we are populating
2306 * @refresh_required: true if we should get recipe to profile mapping from FW
2307 * @is_add: flag of adding recipe
2308 *
2309 * This function is used to populate all the necessary entries into our
2310 * bookkeeping so that we have a current list of all the recipes that are
2311 * programmed in the firmware.
2312 */
2313static int
2314ice_get_recp_frm_fw(struct ice_hw *hw, struct ice_sw_recipe *recps, u8 rid,
2315		    bool *refresh_required, bool is_add)
2316{
2317	DECLARE_BITMAP(result_bm, ICE_MAX_FV_WORDS);
2318	struct ice_aqc_recipe_data_elem *tmp;
2319	u16 num_recps = ICE_MAX_NUM_RECIPES;
2320	struct ice_prot_lkup_ext *lkup_exts;
2321	u8 fv_word_idx = 0;
2322	u16 sub_recps;
2323	int status;
2324
2325	bitmap_zero(result_bm, ICE_MAX_FV_WORDS);
2326
2327	/* we need a buffer big enough to accommodate all the recipes */
2328	tmp = kcalloc(ICE_MAX_NUM_RECIPES, sizeof(*tmp), GFP_KERNEL);
2329	if (!tmp)
2330		return -ENOMEM;
2331
2332	tmp[0].recipe_indx = rid;
2333	status = ice_aq_get_recipe(hw, tmp, &num_recps, rid, NULL);
2334	/* non-zero status meaning recipe doesn't exist */
2335	if (status)
2336		goto err_unroll;
2337
2338	/* Get recipe to profile map so that we can get the fv from lkups that
2339	 * we read for a recipe from FW. Since we want to minimize the number of
2340	 * times we make this FW call, just make one call and cache the copy
2341	 * until a new recipe is added. This operation is only required the
2342	 * first time to get the changes from FW. Then to search existing
2343	 * entries we don't need to update the cache again until another recipe
2344	 * gets added.
2345	 */
2346	if (*refresh_required) {
2347		ice_get_recp_to_prof_map(hw);
2348		*refresh_required = false;
2349	}
2350
2351	/* Start populating all the entries for recps[rid] based on lkups from
2352	 * firmware. Note that we are only creating the root recipe in our
2353	 * database.
2354	 */
2355	lkup_exts = &recps[rid].lkup_exts;
2356
2357	for (sub_recps = 0; sub_recps < num_recps; sub_recps++) {
2358		struct ice_aqc_recipe_data_elem root_bufs = tmp[sub_recps];
 
2359		u8 i, prof, idx, prot = 0;
2360		bool is_root;
2361		u16 off = 0;
2362
 
 
 
 
 
 
 
2363		idx = root_bufs.recipe_indx;
2364		is_root = root_bufs.content.rid & ICE_AQ_RECIPE_ID_IS_ROOT;
2365
2366		/* Mark all result indices in this chain */
2367		if (root_bufs.content.result_indx & ICE_AQ_RECIPE_RESULT_EN)
2368			set_bit(root_bufs.content.result_indx & ~ICE_AQ_RECIPE_RESULT_EN,
2369				result_bm);
2370
2371		/* get the first profile that is associated with rid */
2372		prof = find_first_bit(recipe_to_profile[idx],
2373				      ICE_MAX_NUM_PROFILES);
2374		for (i = 0; i < ICE_NUM_WORDS_RECIPE; i++) {
2375			u8 lkup_indx = root_bufs.content.lkup_indx[i];
2376			u16 lkup_mask = le16_to_cpu(root_bufs.content.mask[i]);
 
 
 
2377
2378			/* If the recipe is a chained recipe then all its
2379			 * child recipe's result will have a result index.
2380			 * To fill fv_words we should not use those result
2381			 * index, we only need the protocol ids and offsets.
2382			 * We will skip all the fv_idx which stores result
2383			 * index in them. We also need to skip any fv_idx which
2384			 * has ICE_AQ_RECIPE_LKUP_IGNORE or 0 since it isn't a
2385			 * valid offset value.
2386			 */
2387			if (!lkup_indx ||
2388			    (lkup_indx & ICE_AQ_RECIPE_LKUP_IGNORE) ||
2389			    test_bit(lkup_indx,
2390				     hw->switch_info->prof_res_bm[prof]))
2391				continue;
2392
2393			ice_find_prot_off(hw, ICE_BLK_SW, prof, lkup_indx,
2394					  &prot, &off);
2395			lkup_exts->fv_words[fv_word_idx].prot_id = prot;
2396			lkup_exts->fv_words[fv_word_idx].off = off;
2397			lkup_exts->field_mask[fv_word_idx] = lkup_mask;
 
2398			fv_word_idx++;
2399		}
 
 
 
 
2400
2401		/* Propagate some data to the recipe database */
 
2402		recps[idx].priority = root_bufs.content.act_ctrl_fwd_priority;
2403		recps[idx].need_pass_l2 = !!(root_bufs.content.act_ctrl &
2404					     ICE_AQ_RECIPE_ACT_NEED_PASS_L2);
2405		recps[idx].allow_pass_l2 = !!(root_bufs.content.act_ctrl &
2406					      ICE_AQ_RECIPE_ACT_ALLOW_PASS_L2);
2407		bitmap_zero(recps[idx].res_idxs, ICE_MAX_FV_WORDS);
2408		if (root_bufs.content.result_indx & ICE_AQ_RECIPE_RESULT_EN) {
2409			set_bit(root_bufs.content.result_indx &
2410				~ICE_AQ_RECIPE_RESULT_EN, recps[idx].res_idxs);
 
 
 
2411		}
2412
2413		if (!is_root) {
2414			if (hw->recp_reuse && is_add)
2415				recps[idx].recp_created = true;
2416
2417			continue;
2418		}
2419
2420		/* Only do the following for root recipes entries */
2421		memcpy(recps[idx].r_bitmap, root_bufs.recipe_bitmap,
2422		       sizeof(recps[idx].r_bitmap));
2423		recps[idx].root_rid = root_bufs.content.rid &
2424			~ICE_AQ_RECIPE_ID_IS_ROOT;
2425		recps[idx].priority = root_bufs.content.act_ctrl_fwd_priority;
2426	}
2427
2428	/* Complete initialization of the root recipe entry */
2429	lkup_exts->n_val_words = fv_word_idx;
 
 
 
 
 
 
 
 
 
2430
2431	/* Copy result indexes */
2432	bitmap_copy(recps[rid].res_idxs, result_bm, ICE_MAX_FV_WORDS);
2433	if (is_add)
2434		recps[rid].recp_created = true;
2435
2436err_unroll:
2437	kfree(tmp);
2438	return status;
2439}
2440
2441/* ice_init_port_info - Initialize port_info with switch configuration data
2442 * @pi: pointer to port_info
2443 * @vsi_port_num: VSI number or port number
2444 * @type: Type of switch element (port or VSI)
2445 * @swid: switch ID of the switch the element is attached to
2446 * @pf_vf_num: PF or VF number
2447 * @is_vf: true if the element is a VF, false otherwise
2448 */
2449static void
2450ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
2451		   u16 swid, u16 pf_vf_num, bool is_vf)
2452{
2453	switch (type) {
2454	case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
2455		pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
2456		pi->sw_id = swid;
2457		pi->pf_vf_num = pf_vf_num;
2458		pi->is_vf = is_vf;
2459		break;
2460	default:
2461		ice_debug(pi->hw, ICE_DBG_SW, "incorrect VSI/port type received\n");
2462		break;
2463	}
2464}
2465
2466/* ice_get_initial_sw_cfg - Get initial port and default VSI data
2467 * @hw: pointer to the hardware structure
2468 */
2469int ice_get_initial_sw_cfg(struct ice_hw *hw)
2470{
2471	struct ice_aqc_get_sw_cfg_resp_elem *rbuf;
2472	u16 req_desc = 0;
2473	u16 num_elems;
2474	int status;
2475	u16 i;
2476
2477	rbuf = kzalloc(ICE_SW_CFG_MAX_BUF_LEN, GFP_KERNEL);
2478	if (!rbuf)
2479		return -ENOMEM;
2480
2481	/* Multiple calls to ice_aq_get_sw_cfg may be required
2482	 * to get all the switch configuration information. The need
2483	 * for additional calls is indicated by ice_aq_get_sw_cfg
2484	 * writing a non-zero value in req_desc
2485	 */
2486	do {
2487		struct ice_aqc_get_sw_cfg_resp_elem *ele;
2488
2489		status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
2490					   &req_desc, &num_elems, NULL);
2491
2492		if (status)
2493			break;
2494
2495		for (i = 0, ele = rbuf; i < num_elems; i++, ele++) {
2496			u16 pf_vf_num, swid, vsi_port_num;
2497			bool is_vf = false;
2498			u8 res_type;
2499
2500			vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
2501				ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
2502
2503			pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
2504				ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
2505
2506			swid = le16_to_cpu(ele->swid);
2507
2508			if (le16_to_cpu(ele->pf_vf_num) &
2509			    ICE_AQC_GET_SW_CONF_RESP_IS_VF)
2510				is_vf = true;
2511
2512			res_type = (u8)(le16_to_cpu(ele->vsi_port_num) >>
2513					ICE_AQC_GET_SW_CONF_RESP_TYPE_S);
2514
2515			if (res_type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
2516				/* FW VSI is not needed. Just continue. */
2517				continue;
2518			}
2519
2520			ice_init_port_info(hw->port_info, vsi_port_num,
2521					   res_type, swid, pf_vf_num, is_vf);
2522		}
2523	} while (req_desc && !status);
2524
2525	kfree(rbuf);
2526	return status;
2527}
2528
2529/**
2530 * ice_fill_sw_info - Helper function to populate lb_en and lan_en
2531 * @hw: pointer to the hardware structure
2532 * @fi: filter info structure to fill/update
2533 *
2534 * This helper function populates the lb_en and lan_en elements of the provided
2535 * ice_fltr_info struct using the switch's type and characteristics of the
2536 * switch rule being configured.
2537 */
2538static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
2539{
2540	fi->lb_en = false;
2541	fi->lan_en = false;
2542	if ((fi->flag & ICE_FLTR_TX) &&
2543	    (fi->fltr_act == ICE_FWD_TO_VSI ||
2544	     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
2545	     fi->fltr_act == ICE_FWD_TO_Q ||
2546	     fi->fltr_act == ICE_FWD_TO_QGRP)) {
2547		/* Setting LB for prune actions will result in replicated
2548		 * packets to the internal switch that will be dropped.
2549		 */
2550		if (fi->lkup_type != ICE_SW_LKUP_VLAN)
2551			fi->lb_en = true;
2552
2553		/* Set lan_en to TRUE if
2554		 * 1. The switch is a VEB AND
2555		 * 2
2556		 * 2.1 The lookup is a directional lookup like ethertype,
2557		 * promiscuous, ethertype-MAC, promiscuous-VLAN
2558		 * and default-port OR
2559		 * 2.2 The lookup is VLAN, OR
2560		 * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
2561		 * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
2562		 *
2563		 * OR
2564		 *
2565		 * The switch is a VEPA.
2566		 *
2567		 * In all other cases, the LAN enable has to be set to false.
2568		 */
2569		if (hw->evb_veb) {
2570			if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
2571			    fi->lkup_type == ICE_SW_LKUP_PROMISC ||
2572			    fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
2573			    fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
2574			    fi->lkup_type == ICE_SW_LKUP_DFLT ||
2575			    fi->lkup_type == ICE_SW_LKUP_VLAN ||
2576			    (fi->lkup_type == ICE_SW_LKUP_MAC &&
2577			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)) ||
2578			    (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
2579			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)))
2580				fi->lan_en = true;
2581		} else {
2582			fi->lan_en = true;
2583		}
2584	}
2585
2586	if (fi->flag & ICE_FLTR_TX_ONLY)
2587		fi->lan_en = false;
2588}
2589
2590/**
2591 * ice_fill_eth_hdr - helper to copy dummy_eth_hdr into supplied buffer
2592 * @eth_hdr: pointer to buffer to populate
2593 */
2594void ice_fill_eth_hdr(u8 *eth_hdr)
2595{
2596	memcpy(eth_hdr, dummy_eth_header, DUMMY_ETH_HDR_LEN);
2597}
2598
2599/**
2600 * ice_fill_sw_rule - Helper function to fill switch rule structure
2601 * @hw: pointer to the hardware structure
2602 * @f_info: entry containing packet forwarding information
2603 * @s_rule: switch rule structure to be filled in based on mac_entry
2604 * @opc: switch rules population command type - pass in the command opcode
2605 */
2606static void
2607ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
2608		 struct ice_sw_rule_lkup_rx_tx *s_rule,
2609		 enum ice_adminq_opc opc)
2610{
2611	u16 vlan_id = ICE_MAX_VLAN_ID + 1;
2612	u16 vlan_tpid = ETH_P_8021Q;
2613	void *daddr = NULL;
2614	u16 eth_hdr_sz;
2615	u8 *eth_hdr;
2616	u32 act = 0;
2617	__be16 *off;
2618	u8 q_rgn;
2619
2620	if (opc == ice_aqc_opc_remove_sw_rules) {
2621		s_rule->act = 0;
2622		s_rule->index = cpu_to_le16(f_info->fltr_rule_id);
2623		s_rule->hdr_len = 0;
2624		return;
2625	}
2626
2627	eth_hdr_sz = sizeof(dummy_eth_header);
2628	eth_hdr = s_rule->hdr_data;
2629
2630	/* initialize the ether header with a dummy header */
2631	memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz);
2632	ice_fill_sw_info(hw, f_info);
2633
2634	switch (f_info->fltr_act) {
2635	case ICE_FWD_TO_VSI:
2636		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
2637				  f_info->fwd_id.hw_vsi_id);
2638		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
2639			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
2640				ICE_SINGLE_ACT_VALID_BIT;
2641		break;
2642	case ICE_FWD_TO_VSI_LIST:
2643		act |= ICE_SINGLE_ACT_VSI_LIST;
2644		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_LIST_ID_M,
2645				  f_info->fwd_id.vsi_list_id);
2646		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
2647			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
2648				ICE_SINGLE_ACT_VALID_BIT;
2649		break;
2650	case ICE_FWD_TO_Q:
2651		act |= ICE_SINGLE_ACT_TO_Q;
2652		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
2653				  f_info->fwd_id.q_id);
2654		break;
2655	case ICE_DROP_PACKET:
2656		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
2657			ICE_SINGLE_ACT_VALID_BIT;
2658		break;
2659	case ICE_FWD_TO_QGRP:
2660		q_rgn = f_info->qgrp_size > 0 ?
2661			(u8)ilog2(f_info->qgrp_size) : 0;
2662		act |= ICE_SINGLE_ACT_TO_Q;
2663		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
2664				  f_info->fwd_id.q_id);
2665		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_REGION_M, q_rgn);
2666		break;
2667	default:
2668		return;
2669	}
2670
2671	if (f_info->lb_en)
2672		act |= ICE_SINGLE_ACT_LB_ENABLE;
2673	if (f_info->lan_en)
2674		act |= ICE_SINGLE_ACT_LAN_ENABLE;
2675
2676	switch (f_info->lkup_type) {
2677	case ICE_SW_LKUP_MAC:
2678		daddr = f_info->l_data.mac.mac_addr;
2679		break;
2680	case ICE_SW_LKUP_VLAN:
2681		vlan_id = f_info->l_data.vlan.vlan_id;
2682		if (f_info->l_data.vlan.tpid_valid)
2683			vlan_tpid = f_info->l_data.vlan.tpid;
2684		if (f_info->fltr_act == ICE_FWD_TO_VSI ||
2685		    f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
2686			act |= ICE_SINGLE_ACT_PRUNE;
2687			act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
2688		}
2689		break;
2690	case ICE_SW_LKUP_ETHERTYPE_MAC:
2691		daddr = f_info->l_data.ethertype_mac.mac_addr;
2692		fallthrough;
2693	case ICE_SW_LKUP_ETHERTYPE:
2694		off = (__force __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
2695		*off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
2696		break;
2697	case ICE_SW_LKUP_MAC_VLAN:
2698		daddr = f_info->l_data.mac_vlan.mac_addr;
2699		vlan_id = f_info->l_data.mac_vlan.vlan_id;
2700		break;
2701	case ICE_SW_LKUP_PROMISC_VLAN:
2702		vlan_id = f_info->l_data.mac_vlan.vlan_id;
2703		fallthrough;
2704	case ICE_SW_LKUP_PROMISC:
2705		daddr = f_info->l_data.mac_vlan.mac_addr;
2706		break;
2707	default:
2708		break;
2709	}
2710
2711	s_rule->hdr.type = (f_info->flag & ICE_FLTR_RX) ?
2712		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
2713		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
2714
2715	/* Recipe set depending on lookup type */
2716	s_rule->recipe_id = cpu_to_le16(f_info->lkup_type);
2717	s_rule->src = cpu_to_le16(f_info->src);
2718	s_rule->act = cpu_to_le32(act);
2719
2720	if (daddr)
2721		ether_addr_copy(eth_hdr + ICE_ETH_DA_OFFSET, daddr);
2722
2723	if (!(vlan_id > ICE_MAX_VLAN_ID)) {
2724		off = (__force __be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
2725		*off = cpu_to_be16(vlan_id);
2726		off = (__force __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
2727		*off = cpu_to_be16(vlan_tpid);
2728	}
2729
2730	/* Create the switch rule with the final dummy Ethernet header */
2731	if (opc != ice_aqc_opc_update_sw_rules)
2732		s_rule->hdr_len = cpu_to_le16(eth_hdr_sz);
2733}
2734
2735/**
2736 * ice_add_marker_act
2737 * @hw: pointer to the hardware structure
2738 * @m_ent: the management entry for which sw marker needs to be added
2739 * @sw_marker: sw marker to tag the Rx descriptor with
2740 * @l_id: large action resource ID
2741 *
2742 * Create a large action to hold software marker and update the switch rule
2743 * entry pointed by m_ent with newly created large action
2744 */
2745static int
2746ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
2747		   u16 sw_marker, u16 l_id)
2748{
2749	struct ice_sw_rule_lkup_rx_tx *rx_tx;
2750	struct ice_sw_rule_lg_act *lg_act;
2751	/* For software marker we need 3 large actions
2752	 * 1. FWD action: FWD TO VSI or VSI LIST
2753	 * 2. GENERIC VALUE action to hold the profile ID
2754	 * 3. GENERIC VALUE action to hold the software marker ID
2755	 */
2756	const u16 num_lg_acts = 3;
2757	u16 lg_act_size;
2758	u16 rules_size;
2759	int status;
2760	u32 act;
2761	u16 id;
2762
2763	if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
2764		return -EINVAL;
2765
2766	/* Create two back-to-back switch rules and submit them to the HW using
2767	 * one memory buffer:
2768	 *    1. Large Action
2769	 *    2. Look up Tx Rx
2770	 */
2771	lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(lg_act, num_lg_acts);
2772	rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(rx_tx);
2773	lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
2774	if (!lg_act)
2775		return -ENOMEM;
2776
2777	rx_tx = (typeof(rx_tx))((u8 *)lg_act + lg_act_size);
2778
2779	/* Fill in the first switch rule i.e. large action */
2780	lg_act->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
2781	lg_act->index = cpu_to_le16(l_id);
2782	lg_act->size = cpu_to_le16(num_lg_acts);
2783
2784	/* First action VSI forwarding or VSI list forwarding depending on how
2785	 * many VSIs
2786	 */
2787	id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
2788		m_ent->fltr_info.fwd_id.hw_vsi_id;
2789
2790	act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
2791	act |= FIELD_PREP(ICE_LG_ACT_VSI_LIST_ID_M, id);
2792	if (m_ent->vsi_count > 1)
2793		act |= ICE_LG_ACT_VSI_LIST;
2794	lg_act->act[0] = cpu_to_le32(act);
2795
2796	/* Second action descriptor type */
2797	act = ICE_LG_ACT_GENERIC;
2798
2799	act |= FIELD_PREP(ICE_LG_ACT_GENERIC_VALUE_M, 1);
2800	lg_act->act[1] = cpu_to_le32(act);
2801
2802	act = FIELD_PREP(ICE_LG_ACT_GENERIC_OFFSET_M,
2803			 ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX);
2804
2805	/* Third action Marker value */
2806	act |= ICE_LG_ACT_GENERIC;
2807	act |= FIELD_PREP(ICE_LG_ACT_GENERIC_VALUE_M, sw_marker);
2808
2809	lg_act->act[2] = cpu_to_le32(act);
2810
2811	/* call the fill switch rule to fill the lookup Tx Rx structure */
2812	ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
2813			 ice_aqc_opc_update_sw_rules);
2814
2815	/* Update the action to point to the large action ID */
2816	act = ICE_SINGLE_ACT_PTR;
2817	act |= FIELD_PREP(ICE_SINGLE_ACT_PTR_VAL_M, l_id);
2818	rx_tx->act = cpu_to_le32(act);
2819
2820	/* Use the filter rule ID of the previously created rule with single
2821	 * act. Once the update happens, hardware will treat this as large
2822	 * action
2823	 */
2824	rx_tx->index = cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
2825
2826	status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
2827				 ice_aqc_opc_update_sw_rules, NULL);
2828	if (!status) {
2829		m_ent->lg_act_idx = l_id;
2830		m_ent->sw_marker_id = sw_marker;
2831	}
2832
2833	devm_kfree(ice_hw_to_dev(hw), lg_act);
2834	return status;
2835}
2836
2837/**
2838 * ice_create_vsi_list_map
2839 * @hw: pointer to the hardware structure
2840 * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
2841 * @num_vsi: number of VSI handles in the array
2842 * @vsi_list_id: VSI list ID generated as part of allocate resource
2843 *
2844 * Helper function to create a new entry of VSI list ID to VSI mapping
2845 * using the given VSI list ID
2846 */
2847static struct ice_vsi_list_map_info *
2848ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
2849			u16 vsi_list_id)
2850{
2851	struct ice_switch_info *sw = hw->switch_info;
2852	struct ice_vsi_list_map_info *v_map;
2853	int i;
2854
2855	v_map = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*v_map), GFP_KERNEL);
2856	if (!v_map)
2857		return NULL;
2858
2859	v_map->vsi_list_id = vsi_list_id;
2860	v_map->ref_cnt = 1;
2861	for (i = 0; i < num_vsi; i++)
2862		set_bit(vsi_handle_arr[i], v_map->vsi_map);
2863
2864	list_add(&v_map->list_entry, &sw->vsi_list_map_head);
2865	return v_map;
2866}
2867
2868/**
2869 * ice_update_vsi_list_rule
2870 * @hw: pointer to the hardware structure
2871 * @vsi_handle_arr: array of VSI handles to form a VSI list
2872 * @num_vsi: number of VSI handles in the array
2873 * @vsi_list_id: VSI list ID generated as part of allocate resource
2874 * @remove: Boolean value to indicate if this is a remove action
2875 * @opc: switch rules population command type - pass in the command opcode
2876 * @lkup_type: lookup type of the filter
2877 *
2878 * Call AQ command to add a new switch rule or update existing switch rule
2879 * using the given VSI list ID
2880 */
2881static int
2882ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
2883			 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
2884			 enum ice_sw_lkup_type lkup_type)
2885{
2886	struct ice_sw_rule_vsi_list *s_rule;
2887	u16 s_rule_size;
2888	u16 rule_type;
2889	int status;
2890	int i;
2891
2892	if (!num_vsi)
2893		return -EINVAL;
2894
2895	if (lkup_type == ICE_SW_LKUP_MAC ||
2896	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
2897	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
2898	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
2899	    lkup_type == ICE_SW_LKUP_PROMISC ||
2900	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
2901	    lkup_type == ICE_SW_LKUP_DFLT ||
2902	    lkup_type == ICE_SW_LKUP_LAST)
2903		rule_type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
2904			ICE_AQC_SW_RULES_T_VSI_LIST_SET;
2905	else if (lkup_type == ICE_SW_LKUP_VLAN)
2906		rule_type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
2907			ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
2908	else
2909		return -EINVAL;
2910
2911	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, num_vsi);
2912	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
2913	if (!s_rule)
2914		return -ENOMEM;
2915	for (i = 0; i < num_vsi; i++) {
2916		if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
2917			status = -EINVAL;
2918			goto exit;
2919		}
2920		/* AQ call requires hw_vsi_id(s) */
2921		s_rule->vsi[i] =
2922			cpu_to_le16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
2923	}
2924
2925	s_rule->hdr.type = cpu_to_le16(rule_type);
2926	s_rule->number_vsi = cpu_to_le16(num_vsi);
2927	s_rule->index = cpu_to_le16(vsi_list_id);
2928
2929	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
2930
2931exit:
2932	devm_kfree(ice_hw_to_dev(hw), s_rule);
2933	return status;
2934}
2935
2936/**
2937 * ice_create_vsi_list_rule - Creates and populates a VSI list rule
2938 * @hw: pointer to the HW struct
2939 * @vsi_handle_arr: array of VSI handles to form a VSI list
2940 * @num_vsi: number of VSI handles in the array
2941 * @vsi_list_id: stores the ID of the VSI list to be created
2942 * @lkup_type: switch rule filter's lookup type
2943 */
2944static int
2945ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
2946			 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
2947{
2948	int status;
2949
2950	status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
2951					    ice_aqc_opc_alloc_res);
2952	if (status)
2953		return status;
2954
2955	/* Update the newly created VSI list to include the specified VSIs */
2956	return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
2957					*vsi_list_id, false,
2958					ice_aqc_opc_add_sw_rules, lkup_type);
2959}
2960
2961/**
2962 * ice_create_pkt_fwd_rule
2963 * @hw: pointer to the hardware structure
2964 * @f_entry: entry containing packet forwarding information
2965 *
2966 * Create switch rule with given filter information and add an entry
2967 * to the corresponding filter management list to track this switch rule
2968 * and VSI mapping
2969 */
2970static int
2971ice_create_pkt_fwd_rule(struct ice_hw *hw,
2972			struct ice_fltr_list_entry *f_entry)
2973{
2974	struct ice_fltr_mgmt_list_entry *fm_entry;
2975	struct ice_sw_rule_lkup_rx_tx *s_rule;
2976	enum ice_sw_lkup_type l_type;
2977	struct ice_sw_recipe *recp;
2978	int status;
2979
2980	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
2981			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule),
2982			      GFP_KERNEL);
2983	if (!s_rule)
2984		return -ENOMEM;
2985	fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
2986				GFP_KERNEL);
2987	if (!fm_entry) {
2988		status = -ENOMEM;
2989		goto ice_create_pkt_fwd_rule_exit;
2990	}
2991
2992	fm_entry->fltr_info = f_entry->fltr_info;
2993
2994	/* Initialize all the fields for the management entry */
2995	fm_entry->vsi_count = 1;
2996	fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
2997	fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
2998	fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
2999
3000	ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
3001			 ice_aqc_opc_add_sw_rules);
3002
3003	status = ice_aq_sw_rules(hw, s_rule,
3004				 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule), 1,
3005				 ice_aqc_opc_add_sw_rules, NULL);
3006	if (status) {
3007		devm_kfree(ice_hw_to_dev(hw), fm_entry);
3008		goto ice_create_pkt_fwd_rule_exit;
3009	}
3010
3011	f_entry->fltr_info.fltr_rule_id = le16_to_cpu(s_rule->index);
3012	fm_entry->fltr_info.fltr_rule_id = le16_to_cpu(s_rule->index);
3013
3014	/* The book keeping entries will get removed when base driver
3015	 * calls remove filter AQ command
3016	 */
3017	l_type = fm_entry->fltr_info.lkup_type;
3018	recp = &hw->switch_info->recp_list[l_type];
3019	list_add(&fm_entry->list_entry, &recp->filt_rules);
3020
3021ice_create_pkt_fwd_rule_exit:
3022	devm_kfree(ice_hw_to_dev(hw), s_rule);
3023	return status;
3024}
3025
3026/**
3027 * ice_update_pkt_fwd_rule
3028 * @hw: pointer to the hardware structure
3029 * @f_info: filter information for switch rule
3030 *
3031 * Call AQ command to update a previously created switch rule with a
3032 * VSI list ID
3033 */
3034static int
3035ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
3036{
3037	struct ice_sw_rule_lkup_rx_tx *s_rule;
3038	int status;
3039
3040	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
3041			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule),
3042			      GFP_KERNEL);
3043	if (!s_rule)
3044		return -ENOMEM;
3045
3046	ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
3047
3048	s_rule->index = cpu_to_le16(f_info->fltr_rule_id);
3049
3050	/* Update switch rule with new rule set to forward VSI list */
3051	status = ice_aq_sw_rules(hw, s_rule,
3052				 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule), 1,
3053				 ice_aqc_opc_update_sw_rules, NULL);
3054
3055	devm_kfree(ice_hw_to_dev(hw), s_rule);
3056	return status;
3057}
3058
3059/**
3060 * ice_update_sw_rule_bridge_mode
3061 * @hw: pointer to the HW struct
3062 *
3063 * Updates unicast switch filter rules based on VEB/VEPA mode
3064 */
3065int ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
3066{
3067	struct ice_switch_info *sw = hw->switch_info;
3068	struct ice_fltr_mgmt_list_entry *fm_entry;
3069	struct list_head *rule_head;
3070	struct mutex *rule_lock; /* Lock to protect filter rule list */
3071	int status = 0;
3072
3073	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
3074	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
3075
3076	mutex_lock(rule_lock);
3077	list_for_each_entry(fm_entry, rule_head, list_entry) {
3078		struct ice_fltr_info *fi = &fm_entry->fltr_info;
3079		u8 *addr = fi->l_data.mac.mac_addr;
3080
3081		/* Update unicast Tx rules to reflect the selected
3082		 * VEB/VEPA mode
3083		 */
3084		if ((fi->flag & ICE_FLTR_TX) && is_unicast_ether_addr(addr) &&
3085		    (fi->fltr_act == ICE_FWD_TO_VSI ||
3086		     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
3087		     fi->fltr_act == ICE_FWD_TO_Q ||
3088		     fi->fltr_act == ICE_FWD_TO_QGRP)) {
3089			status = ice_update_pkt_fwd_rule(hw, fi);
3090			if (status)
3091				break;
3092		}
3093	}
3094
3095	mutex_unlock(rule_lock);
3096
3097	return status;
3098}
3099
3100/**
3101 * ice_add_update_vsi_list
3102 * @hw: pointer to the hardware structure
3103 * @m_entry: pointer to current filter management list entry
3104 * @cur_fltr: filter information from the book keeping entry
3105 * @new_fltr: filter information with the new VSI to be added
3106 *
3107 * Call AQ command to add or update previously created VSI list with new VSI.
3108 *
3109 * Helper function to do book keeping associated with adding filter information
3110 * The algorithm to do the book keeping is described below :
3111 * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
3112 *	if only one VSI has been added till now
3113 *		Allocate a new VSI list and add two VSIs
3114 *		to this list using switch rule command
3115 *		Update the previously created switch rule with the
3116 *		newly created VSI list ID
3117 *	if a VSI list was previously created
3118 *		Add the new VSI to the previously created VSI list set
3119 *		using the update switch rule command
3120 */
3121static int
3122ice_add_update_vsi_list(struct ice_hw *hw,
3123			struct ice_fltr_mgmt_list_entry *m_entry,
3124			struct ice_fltr_info *cur_fltr,
3125			struct ice_fltr_info *new_fltr)
3126{
3127	u16 vsi_list_id = 0;
3128	int status = 0;
3129
3130	if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
3131	     cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
3132		return -EOPNOTSUPP;
3133
3134	if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
3135	     new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
3136	    (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
3137	     cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
3138		return -EOPNOTSUPP;
3139
3140	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
3141		/* Only one entry existed in the mapping and it was not already
3142		 * a part of a VSI list. So, create a VSI list with the old and
3143		 * new VSIs.
3144		 */
3145		struct ice_fltr_info tmp_fltr;
3146		u16 vsi_handle_arr[2];
3147
3148		/* A rule already exists with the new VSI being added */
3149		if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
3150			return -EEXIST;
3151
3152		vsi_handle_arr[0] = cur_fltr->vsi_handle;
3153		vsi_handle_arr[1] = new_fltr->vsi_handle;
3154		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
3155						  &vsi_list_id,
3156						  new_fltr->lkup_type);
3157		if (status)
3158			return status;
3159
3160		tmp_fltr = *new_fltr;
3161		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
3162		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
3163		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
3164		/* Update the previous switch rule of "MAC forward to VSI" to
3165		 * "MAC fwd to VSI list"
3166		 */
3167		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
3168		if (status)
3169			return status;
3170
3171		cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
3172		cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
3173		m_entry->vsi_list_info =
3174			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
3175						vsi_list_id);
3176
3177		if (!m_entry->vsi_list_info)
3178			return -ENOMEM;
3179
3180		/* If this entry was large action then the large action needs
3181		 * to be updated to point to FWD to VSI list
3182		 */
3183		if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
3184			status =
3185			    ice_add_marker_act(hw, m_entry,
3186					       m_entry->sw_marker_id,
3187					       m_entry->lg_act_idx);
3188	} else {
3189		u16 vsi_handle = new_fltr->vsi_handle;
3190		enum ice_adminq_opc opcode;
3191
3192		if (!m_entry->vsi_list_info)
3193			return -EIO;
3194
3195		/* A rule already exists with the new VSI being added */
3196		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
3197			return -EEXIST;
3198
3199		/* Update the previously created VSI list set with
3200		 * the new VSI ID passed in
3201		 */
3202		vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
3203		opcode = ice_aqc_opc_update_sw_rules;
3204
3205		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
3206						  vsi_list_id, false, opcode,
3207						  new_fltr->lkup_type);
3208		/* update VSI list mapping info with new VSI ID */
3209		if (!status)
3210			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
3211	}
3212	if (!status)
3213		m_entry->vsi_count++;
3214	return status;
3215}
3216
3217/**
3218 * ice_find_rule_entry - Search a rule entry
3219 * @hw: pointer to the hardware structure
3220 * @recp_id: lookup type for which the specified rule needs to be searched
3221 * @f_info: rule information
3222 *
3223 * Helper function to search for a given rule entry
3224 * Returns pointer to entry storing the rule if found
3225 */
3226static struct ice_fltr_mgmt_list_entry *
3227ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
3228{
3229	struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
3230	struct ice_switch_info *sw = hw->switch_info;
3231	struct list_head *list_head;
3232
3233	list_head = &sw->recp_list[recp_id].filt_rules;
3234	list_for_each_entry(list_itr, list_head, list_entry) {
3235		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
3236			    sizeof(f_info->l_data)) &&
3237		    f_info->flag == list_itr->fltr_info.flag) {
3238			ret = list_itr;
3239			break;
3240		}
3241	}
3242	return ret;
3243}
3244
3245/**
3246 * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
3247 * @hw: pointer to the hardware structure
3248 * @recp_id: lookup type for which VSI lists needs to be searched
3249 * @vsi_handle: VSI handle to be found in VSI list
3250 * @vsi_list_id: VSI list ID found containing vsi_handle
3251 *
3252 * Helper function to search a VSI list with single entry containing given VSI
3253 * handle element. This can be extended further to search VSI list with more
3254 * than 1 vsi_count. Returns pointer to VSI list entry if found.
3255 */
3256struct ice_vsi_list_map_info *
3257ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
3258			u16 *vsi_list_id)
3259{
3260	struct ice_vsi_list_map_info *map_info = NULL;
3261	struct ice_switch_info *sw = hw->switch_info;
3262	struct ice_fltr_mgmt_list_entry *list_itr;
3263	struct list_head *list_head;
3264
3265	list_head = &sw->recp_list[recp_id].filt_rules;
3266	list_for_each_entry(list_itr, list_head, list_entry) {
3267		if (list_itr->vsi_count == 1 && list_itr->vsi_list_info) {
3268			map_info = list_itr->vsi_list_info;
3269			if (test_bit(vsi_handle, map_info->vsi_map)) {
3270				*vsi_list_id = map_info->vsi_list_id;
3271				return map_info;
3272			}
3273		}
3274	}
3275	return NULL;
3276}
3277
3278/**
3279 * ice_add_rule_internal - add rule for a given lookup type
3280 * @hw: pointer to the hardware structure
3281 * @recp_id: lookup type (recipe ID) for which rule has to be added
3282 * @f_entry: structure containing MAC forwarding information
3283 *
3284 * Adds or updates the rule lists for a given recipe
3285 */
3286static int
3287ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
3288		      struct ice_fltr_list_entry *f_entry)
3289{
3290	struct ice_switch_info *sw = hw->switch_info;
3291	struct ice_fltr_info *new_fltr, *cur_fltr;
3292	struct ice_fltr_mgmt_list_entry *m_entry;
3293	struct mutex *rule_lock; /* Lock to protect filter rule list */
3294	int status = 0;
3295
3296	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
3297		return -EINVAL;
3298	f_entry->fltr_info.fwd_id.hw_vsi_id =
3299		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
3300
3301	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
3302
3303	mutex_lock(rule_lock);
3304	new_fltr = &f_entry->fltr_info;
3305	if (new_fltr->flag & ICE_FLTR_RX)
3306		new_fltr->src = hw->port_info->lport;
3307	else if (new_fltr->flag & ICE_FLTR_TX)
3308		new_fltr->src = f_entry->fltr_info.fwd_id.hw_vsi_id;
3309
3310	m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
3311	if (!m_entry) {
3312		mutex_unlock(rule_lock);
3313		return ice_create_pkt_fwd_rule(hw, f_entry);
3314	}
3315
3316	cur_fltr = &m_entry->fltr_info;
3317	status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
3318	mutex_unlock(rule_lock);
3319
3320	return status;
3321}
3322
3323/**
3324 * ice_remove_vsi_list_rule
3325 * @hw: pointer to the hardware structure
3326 * @vsi_list_id: VSI list ID generated as part of allocate resource
3327 * @lkup_type: switch rule filter lookup type
3328 *
3329 * The VSI list should be emptied before this function is called to remove the
3330 * VSI list.
3331 */
3332static int
3333ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
3334			 enum ice_sw_lkup_type lkup_type)
3335{
3336	struct ice_sw_rule_vsi_list *s_rule;
3337	u16 s_rule_size;
3338	int status;
3339
3340	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, 0);
3341	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
3342	if (!s_rule)
3343		return -ENOMEM;
3344
3345	s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
3346	s_rule->index = cpu_to_le16(vsi_list_id);
3347
3348	/* Free the vsi_list resource that we allocated. It is assumed that the
3349	 * list is empty at this point.
3350	 */
3351	status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
3352					    ice_aqc_opc_free_res);
3353
3354	devm_kfree(ice_hw_to_dev(hw), s_rule);
3355	return status;
3356}
3357
3358/**
3359 * ice_rem_update_vsi_list
3360 * @hw: pointer to the hardware structure
3361 * @vsi_handle: VSI handle of the VSI to remove
3362 * @fm_list: filter management entry for which the VSI list management needs to
3363 *           be done
3364 */
3365static int
3366ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
3367			struct ice_fltr_mgmt_list_entry *fm_list)
3368{
3369	enum ice_sw_lkup_type lkup_type;
3370	u16 vsi_list_id;
3371	int status = 0;
3372
3373	if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
3374	    fm_list->vsi_count == 0)
3375		return -EINVAL;
3376
3377	/* A rule with the VSI being removed does not exist */
3378	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
3379		return -ENOENT;
3380
3381	lkup_type = fm_list->fltr_info.lkup_type;
3382	vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
3383	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
3384					  ice_aqc_opc_update_sw_rules,
3385					  lkup_type);
3386	if (status)
3387		return status;
3388
3389	fm_list->vsi_count--;
3390	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
3391
3392	if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
3393		struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
3394		struct ice_vsi_list_map_info *vsi_list_info =
3395			fm_list->vsi_list_info;
3396		u16 rem_vsi_handle;
3397
3398		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
3399						ICE_MAX_VSI);
3400		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
3401			return -EIO;
3402
3403		/* Make sure VSI list is empty before removing it below */
3404		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
3405						  vsi_list_id, true,
3406						  ice_aqc_opc_update_sw_rules,
3407						  lkup_type);
3408		if (status)
3409			return status;
3410
3411		tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
3412		tmp_fltr_info.fwd_id.hw_vsi_id =
3413			ice_get_hw_vsi_num(hw, rem_vsi_handle);
3414		tmp_fltr_info.vsi_handle = rem_vsi_handle;
3415		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
3416		if (status) {
3417			ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
3418				  tmp_fltr_info.fwd_id.hw_vsi_id, status);
3419			return status;
3420		}
3421
3422		fm_list->fltr_info = tmp_fltr_info;
3423	}
3424
3425	if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
3426	    (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
3427		struct ice_vsi_list_map_info *vsi_list_info =
3428			fm_list->vsi_list_info;
3429
3430		/* Remove the VSI list since it is no longer used */
3431		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
3432		if (status) {
3433			ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
3434				  vsi_list_id, status);
3435			return status;
3436		}
3437
3438		list_del(&vsi_list_info->list_entry);
3439		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
3440		fm_list->vsi_list_info = NULL;
3441	}
3442
3443	return status;
3444}
3445
3446/**
3447 * ice_remove_rule_internal - Remove a filter rule of a given type
3448 * @hw: pointer to the hardware structure
3449 * @recp_id: recipe ID for which the rule needs to removed
3450 * @f_entry: rule entry containing filter information
3451 */
3452static int
3453ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
3454			 struct ice_fltr_list_entry *f_entry)
3455{
3456	struct ice_switch_info *sw = hw->switch_info;
3457	struct ice_fltr_mgmt_list_entry *list_elem;
3458	struct mutex *rule_lock; /* Lock to protect filter rule list */
3459	bool remove_rule = false;
3460	u16 vsi_handle;
3461	int status = 0;
3462
3463	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
3464		return -EINVAL;
3465	f_entry->fltr_info.fwd_id.hw_vsi_id =
3466		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
3467
3468	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
3469	mutex_lock(rule_lock);
3470	list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
3471	if (!list_elem) {
3472		status = -ENOENT;
3473		goto exit;
3474	}
3475
3476	if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
3477		remove_rule = true;
3478	} else if (!list_elem->vsi_list_info) {
3479		status = -ENOENT;
3480		goto exit;
3481	} else if (list_elem->vsi_list_info->ref_cnt > 1) {
3482		/* a ref_cnt > 1 indicates that the vsi_list is being
3483		 * shared by multiple rules. Decrement the ref_cnt and
3484		 * remove this rule, but do not modify the list, as it
3485		 * is in-use by other rules.
3486		 */
3487		list_elem->vsi_list_info->ref_cnt--;
3488		remove_rule = true;
3489	} else {
3490		/* a ref_cnt of 1 indicates the vsi_list is only used
3491		 * by one rule. However, the original removal request is only
3492		 * for a single VSI. Update the vsi_list first, and only
3493		 * remove the rule if there are no further VSIs in this list.
3494		 */
3495		vsi_handle = f_entry->fltr_info.vsi_handle;
3496		status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
3497		if (status)
3498			goto exit;
3499		/* if VSI count goes to zero after updating the VSI list */
3500		if (list_elem->vsi_count == 0)
3501			remove_rule = true;
3502	}
3503
3504	if (remove_rule) {
3505		/* Remove the lookup rule */
3506		struct ice_sw_rule_lkup_rx_tx *s_rule;
3507
3508		s_rule = devm_kzalloc(ice_hw_to_dev(hw),
3509				      ICE_SW_RULE_RX_TX_NO_HDR_SIZE(s_rule),
3510				      GFP_KERNEL);
3511		if (!s_rule) {
3512			status = -ENOMEM;
3513			goto exit;
3514		}
3515
3516		ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
3517				 ice_aqc_opc_remove_sw_rules);
3518
3519		status = ice_aq_sw_rules(hw, s_rule,
3520					 ICE_SW_RULE_RX_TX_NO_HDR_SIZE(s_rule),
3521					 1, ice_aqc_opc_remove_sw_rules, NULL);
3522
3523		/* Remove a book keeping from the list */
3524		devm_kfree(ice_hw_to_dev(hw), s_rule);
3525
3526		if (status)
3527			goto exit;
3528
3529		list_del(&list_elem->list_entry);
3530		devm_kfree(ice_hw_to_dev(hw), list_elem);
3531	}
3532exit:
3533	mutex_unlock(rule_lock);
3534	return status;
3535}
3536
3537/**
3538 * ice_vlan_fltr_exist - does this VLAN filter exist for given VSI
3539 * @hw: pointer to the hardware structure
3540 * @vlan_id: VLAN ID
3541 * @vsi_handle: check MAC filter for this VSI
3542 */
3543bool ice_vlan_fltr_exist(struct ice_hw *hw, u16 vlan_id, u16 vsi_handle)
3544{
3545	struct ice_fltr_mgmt_list_entry *entry;
3546	struct list_head *rule_head;
3547	struct ice_switch_info *sw;
3548	struct mutex *rule_lock; /* Lock to protect filter rule list */
3549	u16 hw_vsi_id;
3550
3551	if (vlan_id > ICE_MAX_VLAN_ID)
3552		return false;
3553
3554	if (!ice_is_vsi_valid(hw, vsi_handle))
3555		return false;
3556
3557	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3558	sw = hw->switch_info;
3559	rule_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3560	if (!rule_head)
3561		return false;
3562
3563	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3564	mutex_lock(rule_lock);
3565	list_for_each_entry(entry, rule_head, list_entry) {
3566		struct ice_fltr_info *f_info = &entry->fltr_info;
3567		u16 entry_vlan_id = f_info->l_data.vlan.vlan_id;
3568		struct ice_vsi_list_map_info *map_info;
3569
3570		if (entry_vlan_id > ICE_MAX_VLAN_ID)
3571			continue;
3572
3573		if (f_info->flag != ICE_FLTR_TX ||
3574		    f_info->src_id != ICE_SRC_ID_VSI ||
3575		    f_info->lkup_type != ICE_SW_LKUP_VLAN)
3576			continue;
3577
3578		/* Only allowed filter action are FWD_TO_VSI/_VSI_LIST */
3579		if (f_info->fltr_act != ICE_FWD_TO_VSI &&
3580		    f_info->fltr_act != ICE_FWD_TO_VSI_LIST)
3581			continue;
3582
3583		if (f_info->fltr_act == ICE_FWD_TO_VSI) {
3584			if (hw_vsi_id != f_info->fwd_id.hw_vsi_id)
3585				continue;
3586		} else if (f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
3587			/* If filter_action is FWD_TO_VSI_LIST, make sure
3588			 * that VSI being checked is part of VSI list
3589			 */
3590			if (entry->vsi_count == 1 &&
3591			    entry->vsi_list_info) {
3592				map_info = entry->vsi_list_info;
3593				if (!test_bit(vsi_handle, map_info->vsi_map))
3594					continue;
3595			}
3596		}
3597
3598		if (vlan_id == entry_vlan_id) {
3599			mutex_unlock(rule_lock);
3600			return true;
3601		}
3602	}
3603	mutex_unlock(rule_lock);
3604
3605	return false;
3606}
3607
3608/**
3609 * ice_add_mac - Add a MAC address based filter rule
3610 * @hw: pointer to the hardware structure
3611 * @m_list: list of MAC addresses and forwarding information
3612 */
3613int ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
3614{
3615	struct ice_fltr_list_entry *m_list_itr;
3616	int status = 0;
3617
3618	if (!m_list || !hw)
3619		return -EINVAL;
3620
3621	list_for_each_entry(m_list_itr, m_list, list_entry) {
3622		u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
3623		u16 vsi_handle;
3624		u16 hw_vsi_id;
3625
3626		m_list_itr->fltr_info.flag = ICE_FLTR_TX;
3627		vsi_handle = m_list_itr->fltr_info.vsi_handle;
3628		if (!ice_is_vsi_valid(hw, vsi_handle))
3629			return -EINVAL;
3630		hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3631		m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
3632		/* update the src in case it is VSI num */
3633		if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
3634			return -EINVAL;
3635		m_list_itr->fltr_info.src = hw_vsi_id;
3636		if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
3637		    is_zero_ether_addr(add))
3638			return -EINVAL;
3639
3640		m_list_itr->status = ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
3641							   m_list_itr);
3642		if (m_list_itr->status)
3643			return m_list_itr->status;
3644	}
3645
3646	return status;
3647}
3648
3649/**
3650 * ice_add_vlan_internal - Add one VLAN based filter rule
3651 * @hw: pointer to the hardware structure
3652 * @f_entry: filter entry containing one VLAN information
3653 */
3654static int
3655ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
3656{
3657	struct ice_switch_info *sw = hw->switch_info;
3658	struct ice_fltr_mgmt_list_entry *v_list_itr;
3659	struct ice_fltr_info *new_fltr, *cur_fltr;
3660	enum ice_sw_lkup_type lkup_type;
3661	u16 vsi_list_id = 0, vsi_handle;
3662	struct mutex *rule_lock; /* Lock to protect filter rule list */
3663	int status = 0;
3664
3665	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
3666		return -EINVAL;
3667
3668	f_entry->fltr_info.fwd_id.hw_vsi_id =
3669		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
3670	new_fltr = &f_entry->fltr_info;
3671
3672	/* VLAN ID should only be 12 bits */
3673	if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
3674		return -EINVAL;
3675
3676	if (new_fltr->src_id != ICE_SRC_ID_VSI)
3677		return -EINVAL;
3678
3679	new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
3680	lkup_type = new_fltr->lkup_type;
3681	vsi_handle = new_fltr->vsi_handle;
3682	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3683	mutex_lock(rule_lock);
3684	v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
3685	if (!v_list_itr) {
3686		struct ice_vsi_list_map_info *map_info = NULL;
3687
3688		if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
3689			/* All VLAN pruning rules use a VSI list. Check if
3690			 * there is already a VSI list containing VSI that we
3691			 * want to add. If found, use the same vsi_list_id for
3692			 * this new VLAN rule or else create a new list.
3693			 */
3694			map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
3695							   vsi_handle,
3696							   &vsi_list_id);
3697			if (!map_info) {
3698				status = ice_create_vsi_list_rule(hw,
3699								  &vsi_handle,
3700								  1,
3701								  &vsi_list_id,
3702								  lkup_type);
3703				if (status)
3704					goto exit;
3705			}
3706			/* Convert the action to forwarding to a VSI list. */
3707			new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
3708			new_fltr->fwd_id.vsi_list_id = vsi_list_id;
3709		}
3710
3711		status = ice_create_pkt_fwd_rule(hw, f_entry);
3712		if (!status) {
3713			v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
3714							 new_fltr);
3715			if (!v_list_itr) {
3716				status = -ENOENT;
3717				goto exit;
3718			}
3719			/* reuse VSI list for new rule and increment ref_cnt */
3720			if (map_info) {
3721				v_list_itr->vsi_list_info = map_info;
3722				map_info->ref_cnt++;
3723			} else {
3724				v_list_itr->vsi_list_info =
3725					ice_create_vsi_list_map(hw, &vsi_handle,
3726								1, vsi_list_id);
3727			}
3728		}
3729	} else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
3730		/* Update existing VSI list to add new VSI ID only if it used
3731		 * by one VLAN rule.
3732		 */
3733		cur_fltr = &v_list_itr->fltr_info;
3734		status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
3735						 new_fltr);
3736	} else {
3737		/* If VLAN rule exists and VSI list being used by this rule is
3738		 * referenced by more than 1 VLAN rule. Then create a new VSI
3739		 * list appending previous VSI with new VSI and update existing
3740		 * VLAN rule to point to new VSI list ID
3741		 */
3742		struct ice_fltr_info tmp_fltr;
3743		u16 vsi_handle_arr[2];
3744		u16 cur_handle;
3745
3746		/* Current implementation only supports reusing VSI list with
3747		 * one VSI count. We should never hit below condition
3748		 */
3749		if (v_list_itr->vsi_count > 1 &&
3750		    v_list_itr->vsi_list_info->ref_cnt > 1) {
3751			ice_debug(hw, ICE_DBG_SW, "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
3752			status = -EIO;
3753			goto exit;
3754		}
3755
3756		cur_handle =
3757			find_first_bit(v_list_itr->vsi_list_info->vsi_map,
3758				       ICE_MAX_VSI);
3759
3760		/* A rule already exists with the new VSI being added */
3761		if (cur_handle == vsi_handle) {
3762			status = -EEXIST;
3763			goto exit;
3764		}
3765
3766		vsi_handle_arr[0] = cur_handle;
3767		vsi_handle_arr[1] = vsi_handle;
3768		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
3769						  &vsi_list_id, lkup_type);
3770		if (status)
3771			goto exit;
3772
3773		tmp_fltr = v_list_itr->fltr_info;
3774		tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
3775		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
3776		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
3777		/* Update the previous switch rule to a new VSI list which
3778		 * includes current VSI that is requested
3779		 */
3780		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
3781		if (status)
3782			goto exit;
3783
3784		/* before overriding VSI list map info. decrement ref_cnt of
3785		 * previous VSI list
3786		 */
3787		v_list_itr->vsi_list_info->ref_cnt--;
3788
3789		/* now update to newly created list */
3790		v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
3791		v_list_itr->vsi_list_info =
3792			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
3793						vsi_list_id);
3794		v_list_itr->vsi_count++;
3795	}
3796
3797exit:
3798	mutex_unlock(rule_lock);
3799	return status;
3800}
3801
3802/**
3803 * ice_add_vlan - Add VLAN based filter rule
3804 * @hw: pointer to the hardware structure
3805 * @v_list: list of VLAN entries and forwarding information
3806 */
3807int ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
3808{
3809	struct ice_fltr_list_entry *v_list_itr;
3810
3811	if (!v_list || !hw)
3812		return -EINVAL;
3813
3814	list_for_each_entry(v_list_itr, v_list, list_entry) {
3815		if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
3816			return -EINVAL;
3817		v_list_itr->fltr_info.flag = ICE_FLTR_TX;
3818		v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
3819		if (v_list_itr->status)
3820			return v_list_itr->status;
3821	}
3822	return 0;
3823}
3824
3825/**
3826 * ice_add_eth_mac - Add ethertype and MAC based filter rule
3827 * @hw: pointer to the hardware structure
3828 * @em_list: list of ether type MAC filter, MAC is optional
3829 *
3830 * This function requires the caller to populate the entries in
3831 * the filter list with the necessary fields (including flags to
3832 * indicate Tx or Rx rules).
3833 */
3834int ice_add_eth_mac(struct ice_hw *hw, struct list_head *em_list)
3835{
3836	struct ice_fltr_list_entry *em_list_itr;
3837
3838	if (!em_list || !hw)
3839		return -EINVAL;
3840
3841	list_for_each_entry(em_list_itr, em_list, list_entry) {
3842		enum ice_sw_lkup_type l_type =
3843			em_list_itr->fltr_info.lkup_type;
3844
3845		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
3846		    l_type != ICE_SW_LKUP_ETHERTYPE)
3847			return -EINVAL;
3848
3849		em_list_itr->status = ice_add_rule_internal(hw, l_type,
3850							    em_list_itr);
3851		if (em_list_itr->status)
3852			return em_list_itr->status;
3853	}
3854	return 0;
3855}
3856
3857/**
3858 * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
3859 * @hw: pointer to the hardware structure
3860 * @em_list: list of ethertype or ethertype MAC entries
3861 */
3862int ice_remove_eth_mac(struct ice_hw *hw, struct list_head *em_list)
3863{
3864	struct ice_fltr_list_entry *em_list_itr, *tmp;
3865
3866	if (!em_list || !hw)
3867		return -EINVAL;
3868
3869	list_for_each_entry_safe(em_list_itr, tmp, em_list, list_entry) {
3870		enum ice_sw_lkup_type l_type =
3871			em_list_itr->fltr_info.lkup_type;
3872
3873		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
3874		    l_type != ICE_SW_LKUP_ETHERTYPE)
3875			return -EINVAL;
3876
3877		em_list_itr->status = ice_remove_rule_internal(hw, l_type,
3878							       em_list_itr);
3879		if (em_list_itr->status)
3880			return em_list_itr->status;
3881	}
3882	return 0;
3883}
3884
3885/**
3886 * ice_rem_sw_rule_info
3887 * @hw: pointer to the hardware structure
3888 * @rule_head: pointer to the switch list structure that we want to delete
3889 */
3890static void
3891ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
3892{
3893	if (!list_empty(rule_head)) {
3894		struct ice_fltr_mgmt_list_entry *entry;
3895		struct ice_fltr_mgmt_list_entry *tmp;
3896
3897		list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
3898			list_del(&entry->list_entry);
3899			devm_kfree(ice_hw_to_dev(hw), entry);
3900		}
3901	}
3902}
3903
3904/**
3905 * ice_rem_adv_rule_info
3906 * @hw: pointer to the hardware structure
3907 * @rule_head: pointer to the switch list structure that we want to delete
3908 */
3909static void
3910ice_rem_adv_rule_info(struct ice_hw *hw, struct list_head *rule_head)
3911{
3912	struct ice_adv_fltr_mgmt_list_entry *tmp_entry;
3913	struct ice_adv_fltr_mgmt_list_entry *lst_itr;
3914
3915	if (list_empty(rule_head))
3916		return;
3917
3918	list_for_each_entry_safe(lst_itr, tmp_entry, rule_head, list_entry) {
3919		list_del(&lst_itr->list_entry);
3920		devm_kfree(ice_hw_to_dev(hw), lst_itr->lkups);
3921		devm_kfree(ice_hw_to_dev(hw), lst_itr);
3922	}
3923}
3924
3925/**
3926 * ice_cfg_dflt_vsi - change state of VSI to set/clear default
3927 * @pi: pointer to the port_info structure
3928 * @vsi_handle: VSI handle to set as default
3929 * @set: true to add the above mentioned switch rule, false to remove it
3930 * @direction: ICE_FLTR_RX or ICE_FLTR_TX
3931 *
3932 * add filter rule to set/unset given VSI as default VSI for the switch
3933 * (represented by swid)
3934 */
3935int
3936ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
3937		 u8 direction)
3938{
3939	struct ice_fltr_list_entry f_list_entry;
3940	struct ice_fltr_info f_info;
3941	struct ice_hw *hw = pi->hw;
3942	u16 hw_vsi_id;
3943	int status;
3944
3945	if (!ice_is_vsi_valid(hw, vsi_handle))
3946		return -EINVAL;
3947
3948	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3949
3950	memset(&f_info, 0, sizeof(f_info));
3951
3952	f_info.lkup_type = ICE_SW_LKUP_DFLT;
3953	f_info.flag = direction;
3954	f_info.fltr_act = ICE_FWD_TO_VSI;
3955	f_info.fwd_id.hw_vsi_id = hw_vsi_id;
3956	f_info.vsi_handle = vsi_handle;
3957
3958	if (f_info.flag & ICE_FLTR_RX) {
3959		f_info.src = hw->port_info->lport;
3960		f_info.src_id = ICE_SRC_ID_LPORT;
3961	} else if (f_info.flag & ICE_FLTR_TX) {
3962		f_info.src_id = ICE_SRC_ID_VSI;
3963		f_info.src = hw_vsi_id;
3964		f_info.flag |= ICE_FLTR_TX_ONLY;
3965	}
3966	f_list_entry.fltr_info = f_info;
3967
3968	if (set)
3969		status = ice_add_rule_internal(hw, ICE_SW_LKUP_DFLT,
3970					       &f_list_entry);
3971	else
3972		status = ice_remove_rule_internal(hw, ICE_SW_LKUP_DFLT,
3973						  &f_list_entry);
3974
3975	return status;
3976}
3977
3978/**
3979 * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
3980 * @fm_entry: filter entry to inspect
3981 * @vsi_handle: VSI handle to compare with filter info
3982 */
3983static bool
3984ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
3985{
3986	return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
3987		 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
3988		(fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
3989		 fm_entry->vsi_list_info &&
3990		 (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
3991}
3992
3993/**
3994 * ice_check_if_dflt_vsi - check if VSI is default VSI
3995 * @pi: pointer to the port_info structure
3996 * @vsi_handle: vsi handle to check for in filter list
3997 * @rule_exists: indicates if there are any VSI's in the rule list
3998 *
3999 * checks if the VSI is in a default VSI list, and also indicates
4000 * if the default VSI list is empty
4001 */
4002bool
4003ice_check_if_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle,
4004		      bool *rule_exists)
4005{
4006	struct ice_fltr_mgmt_list_entry *fm_entry;
4007	struct ice_sw_recipe *recp_list;
4008	struct list_head *rule_head;
4009	struct mutex *rule_lock; /* Lock to protect filter rule list */
4010	bool ret = false;
4011
4012	recp_list = &pi->hw->switch_info->recp_list[ICE_SW_LKUP_DFLT];
4013	rule_lock = &recp_list->filt_rule_lock;
4014	rule_head = &recp_list->filt_rules;
4015
4016	mutex_lock(rule_lock);
4017
4018	if (rule_exists && !list_empty(rule_head))
4019		*rule_exists = true;
4020
4021	list_for_each_entry(fm_entry, rule_head, list_entry) {
4022		if (ice_vsi_uses_fltr(fm_entry, vsi_handle)) {
4023			ret = true;
4024			break;
4025		}
4026	}
4027
4028	mutex_unlock(rule_lock);
4029
4030	return ret;
4031}
4032
4033/**
4034 * ice_remove_mac - remove a MAC address based filter rule
4035 * @hw: pointer to the hardware structure
4036 * @m_list: list of MAC addresses and forwarding information
4037 *
4038 * This function removes either a MAC filter rule or a specific VSI from a
4039 * VSI list for a multicast MAC address.
4040 *
4041 * Returns -ENOENT if a given entry was not added by ice_add_mac. Caller should
4042 * be aware that this call will only work if all the entries passed into m_list
4043 * were added previously. It will not attempt to do a partial remove of entries
4044 * that were found.
4045 */
4046int ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
4047{
4048	struct ice_fltr_list_entry *list_itr, *tmp;
4049
4050	if (!m_list)
4051		return -EINVAL;
4052
4053	list_for_each_entry_safe(list_itr, tmp, m_list, list_entry) {
4054		enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
4055		u16 vsi_handle;
4056
4057		if (l_type != ICE_SW_LKUP_MAC)
4058			return -EINVAL;
4059
4060		vsi_handle = list_itr->fltr_info.vsi_handle;
4061		if (!ice_is_vsi_valid(hw, vsi_handle))
4062			return -EINVAL;
4063
4064		list_itr->fltr_info.fwd_id.hw_vsi_id =
4065					ice_get_hw_vsi_num(hw, vsi_handle);
4066
4067		list_itr->status = ice_remove_rule_internal(hw,
4068							    ICE_SW_LKUP_MAC,
4069							    list_itr);
4070		if (list_itr->status)
4071			return list_itr->status;
4072	}
4073	return 0;
4074}
4075
4076/**
4077 * ice_remove_vlan - Remove VLAN based filter rule
4078 * @hw: pointer to the hardware structure
4079 * @v_list: list of VLAN entries and forwarding information
4080 */
4081int ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
4082{
4083	struct ice_fltr_list_entry *v_list_itr, *tmp;
4084
4085	if (!v_list || !hw)
4086		return -EINVAL;
4087
4088	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
4089		enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
4090
4091		if (l_type != ICE_SW_LKUP_VLAN)
4092			return -EINVAL;
4093		v_list_itr->status = ice_remove_rule_internal(hw,
4094							      ICE_SW_LKUP_VLAN,
4095							      v_list_itr);
4096		if (v_list_itr->status)
4097			return v_list_itr->status;
4098	}
4099	return 0;
4100}
4101
4102/**
4103 * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
4104 * @hw: pointer to the hardware structure
4105 * @vsi_handle: VSI handle to remove filters from
4106 * @vsi_list_head: pointer to the list to add entry to
4107 * @fi: pointer to fltr_info of filter entry to copy & add
4108 *
4109 * Helper function, used when creating a list of filters to remove from
4110 * a specific VSI. The entry added to vsi_list_head is a COPY of the
4111 * original filter entry, with the exception of fltr_info.fltr_act and
4112 * fltr_info.fwd_id fields. These are set such that later logic can
4113 * extract which VSI to remove the fltr from, and pass on that information.
4114 */
4115static int
4116ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
4117			       struct list_head *vsi_list_head,
4118			       struct ice_fltr_info *fi)
4119{
4120	struct ice_fltr_list_entry *tmp;
4121
4122	/* this memory is freed up in the caller function
4123	 * once filters for this VSI are removed
4124	 */
4125	tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
4126	if (!tmp)
4127		return -ENOMEM;
4128
4129	tmp->fltr_info = *fi;
4130
4131	/* Overwrite these fields to indicate which VSI to remove filter from,
4132	 * so find and remove logic can extract the information from the
4133	 * list entries. Note that original entries will still have proper
4134	 * values.
4135	 */
4136	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
4137	tmp->fltr_info.vsi_handle = vsi_handle;
4138	tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
4139
4140	list_add(&tmp->list_entry, vsi_list_head);
4141
4142	return 0;
4143}
4144
4145/**
4146 * ice_add_to_vsi_fltr_list - Add VSI filters to the list
4147 * @hw: pointer to the hardware structure
4148 * @vsi_handle: VSI handle to remove filters from
4149 * @lkup_list_head: pointer to the list that has certain lookup type filters
4150 * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
4151 *
4152 * Locates all filters in lkup_list_head that are used by the given VSI,
4153 * and adds COPIES of those entries to vsi_list_head (intended to be used
4154 * to remove the listed filters).
4155 * Note that this means all entries in vsi_list_head must be explicitly
4156 * deallocated by the caller when done with list.
4157 */
4158static int
4159ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
4160			 struct list_head *lkup_list_head,
4161			 struct list_head *vsi_list_head)
4162{
4163	struct ice_fltr_mgmt_list_entry *fm_entry;
4164	int status = 0;
4165
4166	/* check to make sure VSI ID is valid and within boundary */
4167	if (!ice_is_vsi_valid(hw, vsi_handle))
4168		return -EINVAL;
4169
4170	list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
4171		if (!ice_vsi_uses_fltr(fm_entry, vsi_handle))
4172			continue;
4173
4174		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
4175							vsi_list_head,
4176							&fm_entry->fltr_info);
4177		if (status)
4178			return status;
4179	}
4180	return status;
4181}
4182
4183/**
4184 * ice_determine_promisc_mask
4185 * @fi: filter info to parse
4186 *
4187 * Helper function to determine which ICE_PROMISC_ mask corresponds
4188 * to given filter into.
4189 */
4190static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
4191{
4192	u16 vid = fi->l_data.mac_vlan.vlan_id;
4193	u8 *macaddr = fi->l_data.mac.mac_addr;
4194	bool is_tx_fltr = false;
4195	u8 promisc_mask = 0;
4196
4197	if (fi->flag == ICE_FLTR_TX)
4198		is_tx_fltr = true;
4199
4200	if (is_broadcast_ether_addr(macaddr))
4201		promisc_mask |= is_tx_fltr ?
4202			ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
4203	else if (is_multicast_ether_addr(macaddr))
4204		promisc_mask |= is_tx_fltr ?
4205			ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
4206	else if (is_unicast_ether_addr(macaddr))
4207		promisc_mask |= is_tx_fltr ?
4208			ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
4209	if (vid)
4210		promisc_mask |= is_tx_fltr ?
4211			ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
4212
4213	return promisc_mask;
4214}
4215
4216/**
4217 * ice_remove_promisc - Remove promisc based filter rules
4218 * @hw: pointer to the hardware structure
4219 * @recp_id: recipe ID for which the rule needs to removed
4220 * @v_list: list of promisc entries
4221 */
4222static int
4223ice_remove_promisc(struct ice_hw *hw, u8 recp_id, struct list_head *v_list)
4224{
4225	struct ice_fltr_list_entry *v_list_itr, *tmp;
4226
4227	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
4228		v_list_itr->status =
4229			ice_remove_rule_internal(hw, recp_id, v_list_itr);
4230		if (v_list_itr->status)
4231			return v_list_itr->status;
4232	}
4233	return 0;
4234}
4235
4236/**
4237 * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
4238 * @hw: pointer to the hardware structure
4239 * @vsi_handle: VSI handle to clear mode
4240 * @promisc_mask: mask of promiscuous config bits to clear
4241 * @vid: VLAN ID to clear VLAN promiscuous
4242 */
4243int
4244ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
4245		      u16 vid)
4246{
4247	struct ice_switch_info *sw = hw->switch_info;
4248	struct ice_fltr_list_entry *fm_entry, *tmp;
4249	struct list_head remove_list_head;
4250	struct ice_fltr_mgmt_list_entry *itr;
4251	struct list_head *rule_head;
4252	struct mutex *rule_lock;	/* Lock to protect filter rule list */
4253	int status = 0;
4254	u8 recipe_id;
4255
4256	if (!ice_is_vsi_valid(hw, vsi_handle))
4257		return -EINVAL;
4258
4259	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX))
4260		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
4261	else
4262		recipe_id = ICE_SW_LKUP_PROMISC;
4263
4264	rule_head = &sw->recp_list[recipe_id].filt_rules;
4265	rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
4266
4267	INIT_LIST_HEAD(&remove_list_head);
4268
4269	mutex_lock(rule_lock);
4270	list_for_each_entry(itr, rule_head, list_entry) {
4271		struct ice_fltr_info *fltr_info;
4272		u8 fltr_promisc_mask = 0;
4273
4274		if (!ice_vsi_uses_fltr(itr, vsi_handle))
4275			continue;
4276		fltr_info = &itr->fltr_info;
4277
4278		if (recipe_id == ICE_SW_LKUP_PROMISC_VLAN &&
4279		    vid != fltr_info->l_data.mac_vlan.vlan_id)
4280			continue;
4281
4282		fltr_promisc_mask |= ice_determine_promisc_mask(fltr_info);
4283
4284		/* Skip if filter is not completely specified by given mask */
4285		if (fltr_promisc_mask & ~promisc_mask)
4286			continue;
4287
4288		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
4289							&remove_list_head,
4290							fltr_info);
4291		if (status) {
4292			mutex_unlock(rule_lock);
4293			goto free_fltr_list;
4294		}
4295	}
4296	mutex_unlock(rule_lock);
4297
4298	status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
4299
4300free_fltr_list:
4301	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
4302		list_del(&fm_entry->list_entry);
4303		devm_kfree(ice_hw_to_dev(hw), fm_entry);
4304	}
4305
4306	return status;
4307}
4308
4309/**
4310 * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
4311 * @hw: pointer to the hardware structure
4312 * @vsi_handle: VSI handle to configure
4313 * @promisc_mask: mask of promiscuous config bits
4314 * @vid: VLAN ID to set VLAN promiscuous
4315 */
4316int
4317ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
4318{
4319	enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
4320	struct ice_fltr_list_entry f_list_entry;
4321	struct ice_fltr_info new_fltr;
4322	bool is_tx_fltr;
4323	int status = 0;
4324	u16 hw_vsi_id;
4325	int pkt_type;
4326	u8 recipe_id;
4327
4328	if (!ice_is_vsi_valid(hw, vsi_handle))
4329		return -EINVAL;
4330	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
4331
4332	memset(&new_fltr, 0, sizeof(new_fltr));
4333
4334	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
4335		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
4336		new_fltr.l_data.mac_vlan.vlan_id = vid;
4337		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
4338	} else {
4339		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
4340		recipe_id = ICE_SW_LKUP_PROMISC;
4341	}
4342
4343	/* Separate filters must be set for each direction/packet type
4344	 * combination, so we will loop over the mask value, store the
4345	 * individual type, and clear it out in the input mask as it
4346	 * is found.
4347	 */
4348	while (promisc_mask) {
4349		u8 *mac_addr;
4350
4351		pkt_type = 0;
4352		is_tx_fltr = false;
4353
4354		if (promisc_mask & ICE_PROMISC_UCAST_RX) {
4355			promisc_mask &= ~ICE_PROMISC_UCAST_RX;
4356			pkt_type = UCAST_FLTR;
4357		} else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
4358			promisc_mask &= ~ICE_PROMISC_UCAST_TX;
4359			pkt_type = UCAST_FLTR;
4360			is_tx_fltr = true;
4361		} else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
4362			promisc_mask &= ~ICE_PROMISC_MCAST_RX;
4363			pkt_type = MCAST_FLTR;
4364		} else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
4365			promisc_mask &= ~ICE_PROMISC_MCAST_TX;
4366			pkt_type = MCAST_FLTR;
4367			is_tx_fltr = true;
4368		} else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
4369			promisc_mask &= ~ICE_PROMISC_BCAST_RX;
4370			pkt_type = BCAST_FLTR;
4371		} else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
4372			promisc_mask &= ~ICE_PROMISC_BCAST_TX;
4373			pkt_type = BCAST_FLTR;
4374			is_tx_fltr = true;
4375		}
4376
4377		/* Check for VLAN promiscuous flag */
4378		if (promisc_mask & ICE_PROMISC_VLAN_RX) {
4379			promisc_mask &= ~ICE_PROMISC_VLAN_RX;
4380		} else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
4381			promisc_mask &= ~ICE_PROMISC_VLAN_TX;
4382			is_tx_fltr = true;
4383		}
4384
4385		/* Set filter DA based on packet type */
4386		mac_addr = new_fltr.l_data.mac.mac_addr;
4387		if (pkt_type == BCAST_FLTR) {
4388			eth_broadcast_addr(mac_addr);
4389		} else if (pkt_type == MCAST_FLTR ||
4390			   pkt_type == UCAST_FLTR) {
4391			/* Use the dummy ether header DA */
4392			ether_addr_copy(mac_addr, dummy_eth_header);
4393			if (pkt_type == MCAST_FLTR)
4394				mac_addr[0] |= 0x1;	/* Set multicast bit */
4395		}
4396
4397		/* Need to reset this to zero for all iterations */
4398		new_fltr.flag = 0;
4399		if (is_tx_fltr) {
4400			new_fltr.flag |= ICE_FLTR_TX;
4401			new_fltr.src = hw_vsi_id;
4402		} else {
4403			new_fltr.flag |= ICE_FLTR_RX;
4404			new_fltr.src = hw->port_info->lport;
4405		}
4406
4407		new_fltr.fltr_act = ICE_FWD_TO_VSI;
4408		new_fltr.vsi_handle = vsi_handle;
4409		new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
4410		f_list_entry.fltr_info = new_fltr;
4411
4412		status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
4413		if (status)
4414			goto set_promisc_exit;
4415	}
4416
4417set_promisc_exit:
4418	return status;
4419}
4420
4421/**
4422 * ice_set_vlan_vsi_promisc
4423 * @hw: pointer to the hardware structure
4424 * @vsi_handle: VSI handle to configure
4425 * @promisc_mask: mask of promiscuous config bits
4426 * @rm_vlan_promisc: Clear VLANs VSI promisc mode
4427 *
4428 * Configure VSI with all associated VLANs to given promiscuous mode(s)
4429 */
4430int
4431ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
4432			 bool rm_vlan_promisc)
4433{
4434	struct ice_switch_info *sw = hw->switch_info;
4435	struct ice_fltr_list_entry *list_itr, *tmp;
4436	struct list_head vsi_list_head;
4437	struct list_head *vlan_head;
4438	struct mutex *vlan_lock; /* Lock to protect filter rule list */
4439	u16 vlan_id;
4440	int status;
4441
4442	INIT_LIST_HEAD(&vsi_list_head);
4443	vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
4444	vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
4445	mutex_lock(vlan_lock);
4446	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
4447					  &vsi_list_head);
4448	mutex_unlock(vlan_lock);
4449	if (status)
4450		goto free_fltr_list;
4451
4452	list_for_each_entry(list_itr, &vsi_list_head, list_entry) {
4453		/* Avoid enabling or disabling VLAN zero twice when in double
4454		 * VLAN mode
4455		 */
4456		if (ice_is_dvm_ena(hw) &&
4457		    list_itr->fltr_info.l_data.vlan.tpid == 0)
4458			continue;
4459
4460		vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
4461		if (rm_vlan_promisc)
4462			status = ice_clear_vsi_promisc(hw, vsi_handle,
4463						       promisc_mask, vlan_id);
4464		else
4465			status = ice_set_vsi_promisc(hw, vsi_handle,
4466						     promisc_mask, vlan_id);
4467		if (status && status != -EEXIST)
4468			break;
4469	}
4470
4471free_fltr_list:
4472	list_for_each_entry_safe(list_itr, tmp, &vsi_list_head, list_entry) {
4473		list_del(&list_itr->list_entry);
4474		devm_kfree(ice_hw_to_dev(hw), list_itr);
4475	}
4476	return status;
4477}
4478
4479/**
4480 * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
4481 * @hw: pointer to the hardware structure
4482 * @vsi_handle: VSI handle to remove filters from
4483 * @lkup: switch rule filter lookup type
4484 */
4485static void
4486ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
4487			 enum ice_sw_lkup_type lkup)
4488{
4489	struct ice_switch_info *sw = hw->switch_info;
4490	struct ice_fltr_list_entry *fm_entry;
4491	struct list_head remove_list_head;
4492	struct list_head *rule_head;
4493	struct ice_fltr_list_entry *tmp;
4494	struct mutex *rule_lock;	/* Lock to protect filter rule list */
4495	int status;
4496
4497	INIT_LIST_HEAD(&remove_list_head);
4498	rule_lock = &sw->recp_list[lkup].filt_rule_lock;
4499	rule_head = &sw->recp_list[lkup].filt_rules;
4500	mutex_lock(rule_lock);
4501	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
4502					  &remove_list_head);
4503	mutex_unlock(rule_lock);
4504	if (status)
4505		goto free_fltr_list;
4506
4507	switch (lkup) {
4508	case ICE_SW_LKUP_MAC:
4509		ice_remove_mac(hw, &remove_list_head);
4510		break;
4511	case ICE_SW_LKUP_VLAN:
4512		ice_remove_vlan(hw, &remove_list_head);
4513		break;
4514	case ICE_SW_LKUP_PROMISC:
4515	case ICE_SW_LKUP_PROMISC_VLAN:
4516		ice_remove_promisc(hw, lkup, &remove_list_head);
4517		break;
4518	case ICE_SW_LKUP_MAC_VLAN:
4519	case ICE_SW_LKUP_ETHERTYPE:
4520	case ICE_SW_LKUP_ETHERTYPE_MAC:
4521	case ICE_SW_LKUP_DFLT:
4522	case ICE_SW_LKUP_LAST:
4523	default:
4524		ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
4525		break;
4526	}
4527
4528free_fltr_list:
4529	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
4530		list_del(&fm_entry->list_entry);
4531		devm_kfree(ice_hw_to_dev(hw), fm_entry);
4532	}
4533}
4534
4535/**
4536 * ice_remove_vsi_fltr - Remove all filters for a VSI
4537 * @hw: pointer to the hardware structure
4538 * @vsi_handle: VSI handle to remove filters from
4539 */
4540void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
4541{
4542	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
4543	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
4544	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
4545	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
4546	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
4547	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
4548	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
4549	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
4550}
4551
4552/**
4553 * ice_alloc_res_cntr - allocating resource counter
4554 * @hw: pointer to the hardware structure
4555 * @type: type of resource
4556 * @alloc_shared: if set it is shared else dedicated
4557 * @num_items: number of entries requested for FD resource type
4558 * @counter_id: counter index returned by AQ call
4559 */
4560int
4561ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
4562		   u16 *counter_id)
4563{
4564	DEFINE_RAW_FLEX(struct ice_aqc_alloc_free_res_elem, buf, elem, 1);
4565	u16 buf_len = __struct_size(buf);
4566	int status;
4567
4568	buf->num_elems = cpu_to_le16(num_items);
4569	buf->res_type = cpu_to_le16(FIELD_PREP(ICE_AQC_RES_TYPE_M, type) |
4570				    alloc_shared);
4571
4572	status = ice_aq_alloc_free_res(hw, buf, buf_len, ice_aqc_opc_alloc_res);
4573	if (status)
4574		return status;
4575
4576	*counter_id = le16_to_cpu(buf->elem[0].e.sw_resp);
4577	return status;
4578}
4579
4580/**
4581 * ice_free_res_cntr - free resource counter
4582 * @hw: pointer to the hardware structure
4583 * @type: type of resource
4584 * @alloc_shared: if set it is shared else dedicated
4585 * @num_items: number of entries to be freed for FD resource type
4586 * @counter_id: counter ID resource which needs to be freed
4587 */
4588int
4589ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
4590		  u16 counter_id)
4591{
4592	DEFINE_RAW_FLEX(struct ice_aqc_alloc_free_res_elem, buf, elem, 1);
4593	u16 buf_len = __struct_size(buf);
4594	int status;
4595
4596	buf->num_elems = cpu_to_le16(num_items);
4597	buf->res_type = cpu_to_le16(FIELD_PREP(ICE_AQC_RES_TYPE_M, type) |
4598				    alloc_shared);
4599	buf->elem[0].e.sw_resp = cpu_to_le16(counter_id);
4600
4601	status = ice_aq_alloc_free_res(hw, buf, buf_len, ice_aqc_opc_free_res);
4602	if (status)
4603		ice_debug(hw, ICE_DBG_SW, "counter resource could not be freed\n");
4604
4605	return status;
4606}
4607
4608#define ICE_PROTOCOL_ENTRY(id, ...) {		\
4609	.prot_type	= id,			\
4610	.offs		= {__VA_ARGS__},	\
4611}
4612
4613/**
4614 * ice_share_res - set a resource as shared or dedicated
4615 * @hw: hw struct of original owner of resource
4616 * @type: resource type
4617 * @shared: is the resource being set to shared
4618 * @res_id: resource id (descriptor)
4619 */
4620int ice_share_res(struct ice_hw *hw, u16 type, u8 shared, u16 res_id)
4621{
4622	DEFINE_RAW_FLEX(struct ice_aqc_alloc_free_res_elem, buf, elem, 1);
4623	u16 buf_len = __struct_size(buf);
4624	u16 res_type;
4625	int status;
4626
4627	buf->num_elems = cpu_to_le16(1);
4628	res_type = FIELD_PREP(ICE_AQC_RES_TYPE_M, type);
4629	if (shared)
4630		res_type |= ICE_AQC_RES_TYPE_FLAG_SHARED;
4631
4632	buf->res_type = cpu_to_le16(res_type);
4633	buf->elem[0].e.sw_resp = cpu_to_le16(res_id);
4634	status = ice_aq_alloc_free_res(hw, buf, buf_len,
4635				       ice_aqc_opc_share_res);
4636	if (status)
4637		ice_debug(hw, ICE_DBG_SW, "Could not set resource type %u id %u to %s\n",
4638			  type, res_id, shared ? "SHARED" : "DEDICATED");
4639
4640	return status;
4641}
4642
4643/* This is mapping table entry that maps every word within a given protocol
4644 * structure to the real byte offset as per the specification of that
4645 * protocol header.
4646 * for example dst address is 3 words in ethertype header and corresponding
4647 * bytes are 0, 2, 3 in the actual packet header and src address is at 4, 6, 8
4648 * IMPORTANT: Every structure part of "ice_prot_hdr" union should have a
4649 * matching entry describing its field. This needs to be updated if new
4650 * structure is added to that union.
4651 */
4652static const struct ice_prot_ext_tbl_entry ice_prot_ext[ICE_PROTOCOL_LAST] = {
4653	ICE_PROTOCOL_ENTRY(ICE_MAC_OFOS, 0, 2, 4, 6, 8, 10, 12),
4654	ICE_PROTOCOL_ENTRY(ICE_MAC_IL, 0, 2, 4, 6, 8, 10, 12),
4655	ICE_PROTOCOL_ENTRY(ICE_ETYPE_OL, 0),
4656	ICE_PROTOCOL_ENTRY(ICE_ETYPE_IL, 0),
4657	ICE_PROTOCOL_ENTRY(ICE_VLAN_OFOS, 2, 0),
4658	ICE_PROTOCOL_ENTRY(ICE_IPV4_OFOS, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18),
4659	ICE_PROTOCOL_ENTRY(ICE_IPV4_IL,	0, 2, 4, 6, 8, 10, 12, 14, 16, 18),
4660	ICE_PROTOCOL_ENTRY(ICE_IPV6_OFOS, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18,
4661			   20, 22, 24, 26, 28, 30, 32, 34, 36, 38),
4662	ICE_PROTOCOL_ENTRY(ICE_IPV6_IL, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
4663			   22, 24, 26, 28, 30, 32, 34, 36, 38),
4664	ICE_PROTOCOL_ENTRY(ICE_TCP_IL, 0, 2),
4665	ICE_PROTOCOL_ENTRY(ICE_UDP_OF, 0, 2),
4666	ICE_PROTOCOL_ENTRY(ICE_UDP_ILOS, 0, 2),
4667	ICE_PROTOCOL_ENTRY(ICE_VXLAN, 8, 10, 12, 14),
4668	ICE_PROTOCOL_ENTRY(ICE_GENEVE, 8, 10, 12, 14),
4669	ICE_PROTOCOL_ENTRY(ICE_NVGRE, 0, 2, 4, 6),
4670	ICE_PROTOCOL_ENTRY(ICE_GTP, 8, 10, 12, 14, 16, 18, 20, 22),
4671	ICE_PROTOCOL_ENTRY(ICE_GTP_NO_PAY, 8, 10, 12, 14),
4672	ICE_PROTOCOL_ENTRY(ICE_PFCP, 8, 10, 12, 14, 16, 18, 20, 22),
4673	ICE_PROTOCOL_ENTRY(ICE_PPPOE, 0, 2, 4, 6),
4674	ICE_PROTOCOL_ENTRY(ICE_L2TPV3, 0, 2, 4, 6, 8, 10),
4675	ICE_PROTOCOL_ENTRY(ICE_VLAN_EX, 2, 0),
4676	ICE_PROTOCOL_ENTRY(ICE_VLAN_IN, 2, 0),
4677	ICE_PROTOCOL_ENTRY(ICE_HW_METADATA,
4678			   ICE_SOURCE_PORT_MDID_OFFSET,
4679			   ICE_PTYPE_MDID_OFFSET,
4680			   ICE_PACKET_LENGTH_MDID_OFFSET,
4681			   ICE_SOURCE_VSI_MDID_OFFSET,
4682			   ICE_PKT_VLAN_MDID_OFFSET,
4683			   ICE_PKT_TUNNEL_MDID_OFFSET,
4684			   ICE_PKT_TCP_MDID_OFFSET,
4685			   ICE_PKT_ERROR_MDID_OFFSET),
4686};
4687
4688static struct ice_protocol_entry ice_prot_id_tbl[ICE_PROTOCOL_LAST] = {
4689	{ ICE_MAC_OFOS,		ICE_MAC_OFOS_HW },
4690	{ ICE_MAC_IL,		ICE_MAC_IL_HW },
4691	{ ICE_ETYPE_OL,		ICE_ETYPE_OL_HW },
4692	{ ICE_ETYPE_IL,		ICE_ETYPE_IL_HW },
4693	{ ICE_VLAN_OFOS,	ICE_VLAN_OL_HW },
4694	{ ICE_IPV4_OFOS,	ICE_IPV4_OFOS_HW },
4695	{ ICE_IPV4_IL,		ICE_IPV4_IL_HW },
4696	{ ICE_IPV6_OFOS,	ICE_IPV6_OFOS_HW },
4697	{ ICE_IPV6_IL,		ICE_IPV6_IL_HW },
4698	{ ICE_TCP_IL,		ICE_TCP_IL_HW },
4699	{ ICE_UDP_OF,		ICE_UDP_OF_HW },
4700	{ ICE_UDP_ILOS,		ICE_UDP_ILOS_HW },
4701	{ ICE_VXLAN,		ICE_UDP_OF_HW },
4702	{ ICE_GENEVE,		ICE_UDP_OF_HW },
4703	{ ICE_NVGRE,		ICE_GRE_OF_HW },
4704	{ ICE_GTP,		ICE_UDP_OF_HW },
4705	{ ICE_GTP_NO_PAY,	ICE_UDP_ILOS_HW },
4706	{ ICE_PFCP,		ICE_UDP_ILOS_HW },
4707	{ ICE_PPPOE,		ICE_PPPOE_HW },
4708	{ ICE_L2TPV3,		ICE_L2TPV3_HW },
4709	{ ICE_VLAN_EX,          ICE_VLAN_OF_HW },
4710	{ ICE_VLAN_IN,          ICE_VLAN_OL_HW },
4711	{ ICE_HW_METADATA,      ICE_META_DATA_ID_HW },
4712};
4713
4714/**
4715 * ice_find_recp - find a recipe
4716 * @hw: pointer to the hardware structure
4717 * @lkup_exts: extension sequence to match
4718 * @rinfo: information regarding the rule e.g. priority and action info
4719 * @is_add: flag of adding recipe
4720 *
4721 * Returns index of matching recipe, or ICE_MAX_NUM_RECIPES if not found.
4722 */
4723static u16
4724ice_find_recp(struct ice_hw *hw, struct ice_prot_lkup_ext *lkup_exts,
4725	      const struct ice_adv_rule_info *rinfo, bool is_add)
4726{
4727	bool refresh_required = true;
4728	struct ice_sw_recipe *recp;
4729	u8 i;
4730
4731	/* Walk through existing recipes to find a match */
4732	recp = hw->switch_info->recp_list;
4733	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4734		/* If recipe was not created for this ID, in SW bookkeeping,
4735		 * check if FW has an entry for this recipe. If the FW has an
4736		 * entry update it in our SW bookkeeping and continue with the
4737		 * matching.
4738		 */
4739		if (hw->recp_reuse) {
4740			if (ice_get_recp_frm_fw(hw,
4741						hw->switch_info->recp_list, i,
4742						&refresh_required, is_add))
4743				continue;
4744		}
 
 
 
 
4745
4746		/* if number of words we are looking for match */
4747		if (lkup_exts->n_val_words == recp[i].lkup_exts.n_val_words) {
4748			struct ice_fv_word *ar = recp[i].lkup_exts.fv_words;
4749			struct ice_fv_word *be = lkup_exts->fv_words;
4750			u16 *cr = recp[i].lkup_exts.field_mask;
4751			u16 *de = lkup_exts->field_mask;
4752			bool found = true;
4753			u8 pe, qr;
4754
4755			/* ar, cr, and qr are related to the recipe words, while
4756			 * be, de, and pe are related to the lookup words
4757			 */
4758			for (pe = 0; pe < lkup_exts->n_val_words; pe++) {
4759				for (qr = 0; qr < recp[i].lkup_exts.n_val_words;
4760				     qr++) {
4761					if (ar[qr].off == be[pe].off &&
4762					    ar[qr].prot_id == be[pe].prot_id &&
4763					    cr[qr] == de[pe])
4764						/* Found the "pe"th word in the
4765						 * given recipe
4766						 */
4767						break;
4768				}
4769				/* After walking through all the words in the
4770				 * "i"th recipe if "p"th word was not found then
4771				 * this recipe is not what we are looking for.
4772				 * So break out from this loop and try the next
4773				 * recipe
4774				 */
4775				if (qr >= recp[i].lkup_exts.n_val_words) {
4776					found = false;
4777					break;
4778				}
4779			}
4780			/* If for "i"th recipe the found was never set to false
4781			 * then it means we found our match
4782			 * Also tun type and *_pass_l2 of recipe needs to be
4783			 * checked
4784			 */
4785			if (found && recp[i].tun_type == rinfo->tun_type &&
4786			    recp[i].need_pass_l2 == rinfo->need_pass_l2 &&
4787			    recp[i].allow_pass_l2 == rinfo->allow_pass_l2)
4788				return i; /* Return the recipe ID */
4789		}
4790	}
4791	return ICE_MAX_NUM_RECIPES;
4792}
4793
4794/**
4795 * ice_change_proto_id_to_dvm - change proto id in prot_id_tbl
4796 *
4797 * As protocol id for outer vlan is different in dvm and svm, if dvm is
4798 * supported protocol array record for outer vlan has to be modified to
4799 * reflect the value proper for DVM.
4800 */
4801void ice_change_proto_id_to_dvm(void)
4802{
4803	u8 i;
4804
4805	for (i = 0; i < ARRAY_SIZE(ice_prot_id_tbl); i++)
4806		if (ice_prot_id_tbl[i].type == ICE_VLAN_OFOS &&
4807		    ice_prot_id_tbl[i].protocol_id != ICE_VLAN_OF_HW)
4808			ice_prot_id_tbl[i].protocol_id = ICE_VLAN_OF_HW;
4809}
4810
4811/**
4812 * ice_prot_type_to_id - get protocol ID from protocol type
4813 * @type: protocol type
4814 * @id: pointer to variable that will receive the ID
4815 *
4816 * Returns true if found, false otherwise
4817 */
4818static bool ice_prot_type_to_id(enum ice_protocol_type type, u8 *id)
4819{
4820	u8 i;
4821
4822	for (i = 0; i < ARRAY_SIZE(ice_prot_id_tbl); i++)
4823		if (ice_prot_id_tbl[i].type == type) {
4824			*id = ice_prot_id_tbl[i].protocol_id;
4825			return true;
4826		}
4827	return false;
4828}
4829
4830/**
4831 * ice_fill_valid_words - count valid words
4832 * @rule: advanced rule with lookup information
4833 * @lkup_exts: byte offset extractions of the words that are valid
4834 *
4835 * calculate valid words in a lookup rule using mask value
4836 */
4837static u8
4838ice_fill_valid_words(struct ice_adv_lkup_elem *rule,
4839		     struct ice_prot_lkup_ext *lkup_exts)
4840{
4841	u8 j, word, prot_id, ret_val;
4842
4843	if (!ice_prot_type_to_id(rule->type, &prot_id))
4844		return 0;
4845
4846	word = lkup_exts->n_val_words;
4847
4848	for (j = 0; j < sizeof(rule->m_u) / sizeof(u16); j++)
4849		if (((u16 *)&rule->m_u)[j] &&
4850		    rule->type < ARRAY_SIZE(ice_prot_ext)) {
4851			/* No more space to accommodate */
4852			if (word >= ICE_MAX_CHAIN_WORDS)
4853				return 0;
4854			lkup_exts->fv_words[word].off =
4855				ice_prot_ext[rule->type].offs[j];
4856			lkup_exts->fv_words[word].prot_id =
4857				ice_prot_id_tbl[rule->type].protocol_id;
4858			lkup_exts->field_mask[word] =
4859				be16_to_cpu(((__force __be16 *)&rule->m_u)[j]);
4860			word++;
4861		}
4862
4863	ret_val = word - lkup_exts->n_val_words;
4864	lkup_exts->n_val_words = word;
4865
4866	return ret_val;
4867}
4868
4869/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4870 * ice_fill_fv_word_index - fill in the field vector indices for a recipe group
4871 * @hw: pointer to the hardware structure
4872 * @rm: recipe management list entry
 
4873 *
4874 * Helper function to fill in the field vector indices for protocol-offset
4875 * pairs. These indexes are then ultimately programmed into a recipe.
4876 */
4877static int
4878ice_fill_fv_word_index(struct ice_hw *hw, struct ice_sw_recipe *rm)
 
4879{
4880	struct ice_sw_fv_list_entry *fv;
 
4881	struct ice_fv_word *fv_ext;
4882	u8 i;
4883
4884	if (list_empty(&rm->fv_list))
4885		return -EINVAL;
4886
4887	fv = list_first_entry(&rm->fv_list, struct ice_sw_fv_list_entry,
4888			      list_entry);
4889	fv_ext = fv->fv_ptr->ew;
4890
4891	/* Add switch id as the first word. */
4892	rm->fv_idx[0] = ICE_AQ_SW_ID_LKUP_IDX;
4893	rm->fv_mask[0] = ICE_AQ_SW_ID_LKUP_MASK;
4894	rm->n_ext_words++;
4895
4896	for (i = 1; i < rm->n_ext_words; i++) {
4897		struct ice_fv_word *fv_word = &rm->ext_words[i - 1];
4898		u16 fv_mask = rm->word_masks[i - 1];
4899		bool found = false;
4900		u8 j;
4901
4902		for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++) {
4903			if (fv_ext[j].prot_id == fv_word->prot_id &&
4904			    fv_ext[j].off == fv_word->off) {
4905				found = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4906
4907				/* Store index of field vector */
4908				rm->fv_idx[i] = j;
4909				rm->fv_mask[i] = fv_mask;
4910				break;
4911			}
4912		}
4913
4914		/* Protocol/offset could not be found, caller gave an invalid
4915		 * pair.
4916		 */
4917		if (!found)
4918			return -EINVAL;
4919	}
4920
4921	return 0;
4922}
4923
4924/**
4925 * ice_find_free_recp_res_idx - find free result indexes for recipe
4926 * @hw: pointer to hardware structure
4927 * @profiles: bitmap of profiles that will be associated with the new recipe
4928 * @free_idx: pointer to variable to receive the free index bitmap
4929 *
4930 * The algorithm used here is:
4931 *	1. When creating a new recipe, create a set P which contains all
4932 *	   Profiles that will be associated with our new recipe
4933 *
4934 *	2. For each Profile p in set P:
4935 *	    a. Add all recipes associated with Profile p into set R
4936 *	    b. Optional : PossibleIndexes &= profile[p].possibleIndexes
4937 *		[initially PossibleIndexes should be 0xFFFFFFFFFFFFFFFF]
4938 *		i. Or just assume they all have the same possible indexes:
4939 *			44, 45, 46, 47
4940 *			i.e., PossibleIndexes = 0x0000F00000000000
4941 *
4942 *	3. For each Recipe r in set R:
4943 *	    a. UsedIndexes |= (bitwise or ) recipe[r].res_indexes
4944 *	    b. FreeIndexes = UsedIndexes ^ PossibleIndexes
4945 *
4946 *	FreeIndexes will contain the bits indicating the indexes free for use,
4947 *      then the code needs to update the recipe[r].used_result_idx_bits to
4948 *      indicate which indexes were selected for use by this recipe.
4949 */
4950static u16
4951ice_find_free_recp_res_idx(struct ice_hw *hw, const unsigned long *profiles,
4952			   unsigned long *free_idx)
4953{
4954	DECLARE_BITMAP(possible_idx, ICE_MAX_FV_WORDS);
4955	DECLARE_BITMAP(recipes, ICE_MAX_NUM_RECIPES);
4956	DECLARE_BITMAP(used_idx, ICE_MAX_FV_WORDS);
4957	u16 bit;
4958
4959	bitmap_zero(recipes, ICE_MAX_NUM_RECIPES);
4960	bitmap_zero(used_idx, ICE_MAX_FV_WORDS);
4961
4962	bitmap_fill(possible_idx, ICE_MAX_FV_WORDS);
4963
4964	/* For each profile we are going to associate the recipe with, add the
4965	 * recipes that are associated with that profile. This will give us
4966	 * the set of recipes that our recipe may collide with. Also, determine
4967	 * what possible result indexes are usable given this set of profiles.
4968	 */
4969	for_each_set_bit(bit, profiles, ICE_MAX_NUM_PROFILES) {
4970		bitmap_or(recipes, recipes, profile_to_recipe[bit],
4971			  ICE_MAX_NUM_RECIPES);
4972		bitmap_and(possible_idx, possible_idx,
4973			   hw->switch_info->prof_res_bm[bit],
4974			   ICE_MAX_FV_WORDS);
4975	}
4976
4977	/* For each recipe that our new recipe may collide with, determine
4978	 * which indexes have been used.
4979	 */
4980	for_each_set_bit(bit, recipes, ICE_MAX_NUM_RECIPES)
4981		bitmap_or(used_idx, used_idx,
4982			  hw->switch_info->recp_list[bit].res_idxs,
4983			  ICE_MAX_FV_WORDS);
4984
4985	bitmap_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
4986
4987	/* return number of free indexes */
4988	return (u16)bitmap_weight(free_idx, ICE_MAX_FV_WORDS);
4989}
4990
4991/**
4992 * ice_calc_recp_cnt - calculate number of recipes based on word count
4993 * @word_cnt: number of lookup words
4994 *
4995 * Word count should include switch ID word and regular lookup words.
4996 * Returns: number of recipes required to fit @word_cnt, including extra recipes
4997 * needed for recipe chaining (if needed).
4998 */
4999static int ice_calc_recp_cnt(u8 word_cnt)
5000{
5001	/* All words fit in a single recipe, no need for chaining. */
5002	if (word_cnt <= ICE_NUM_WORDS_RECIPE)
5003		return 1;
5004
5005	/* Recipe chaining required. Result indexes are fitted right after
5006	 * regular lookup words. In some cases a new recipe must be added in
5007	 * order to fit result indexes.
5008	 *
5009	 * While the word count increases, every 5 words an extra recipe needs
5010	 * to be added. However, by adding a recipe, one word for its result
5011	 * index must also be added, therefore every 4 words recipe count
5012	 * increases by 1. This calculation does not apply to word count == 1,
5013	 * which is handled above.
5014	 */
5015	return (word_cnt + 2) / (ICE_NUM_WORDS_RECIPE - 1);
5016}
5017
5018static void fill_recipe_template(struct ice_aqc_recipe_data_elem *recp, u16 rid,
5019				 const struct ice_sw_recipe *rm)
5020{
5021	int i;
5022
5023	recp->recipe_indx = rid;
5024	recp->content.act_ctrl |= ICE_AQ_RECIPE_ACT_PRUNE_INDX_M;
5025
5026	for (i = 0; i < ICE_NUM_WORDS_RECIPE; i++) {
5027		recp->content.lkup_indx[i] = ICE_AQ_RECIPE_LKUP_IGNORE;
5028		recp->content.mask[i] = cpu_to_le16(0);
5029	}
5030
5031	set_bit(rid, (unsigned long *)recp->recipe_bitmap);
5032	recp->content.act_ctrl_fwd_priority = rm->priority;
5033
5034	if (rm->need_pass_l2)
5035		recp->content.act_ctrl |= ICE_AQ_RECIPE_ACT_NEED_PASS_L2;
5036
5037	if (rm->allow_pass_l2)
5038		recp->content.act_ctrl |= ICE_AQ_RECIPE_ACT_ALLOW_PASS_L2;
5039}
5040
5041static void bookkeep_recipe(struct ice_sw_recipe *recipe,
5042			    struct ice_aqc_recipe_data_elem *r,
5043			    const struct ice_sw_recipe *rm)
5044{
5045	memcpy(recipe->r_bitmap, r->recipe_bitmap, sizeof(recipe->r_bitmap));
5046
5047	recipe->priority = r->content.act_ctrl_fwd_priority;
5048	recipe->tun_type = rm->tun_type;
5049	recipe->need_pass_l2 = rm->need_pass_l2;
5050	recipe->allow_pass_l2 = rm->allow_pass_l2;
5051	recipe->recp_created = true;
5052}
5053
5054/* For memcpy in ice_add_sw_recipe. */
5055static_assert(sizeof_field(struct ice_aqc_recipe_data_elem, recipe_bitmap) ==
5056	      sizeof_field(struct ice_sw_recipe, r_bitmap));
5057
5058/**
5059 * ice_add_sw_recipe - function to call AQ calls to create switch recipe
5060 * @hw: pointer to hardware structure
5061 * @rm: recipe management list entry
5062 * @profiles: bitmap of profiles that will be associated.
5063 */
5064static int
5065ice_add_sw_recipe(struct ice_hw *hw, struct ice_sw_recipe *rm,
5066		  unsigned long *profiles)
5067{
5068	struct ice_aqc_recipe_data_elem *buf __free(kfree) = NULL;
5069	DECLARE_BITMAP(result_idx_bm, ICE_MAX_FV_WORDS);
5070	struct ice_aqc_recipe_data_elem *root;
5071	struct ice_sw_recipe *recipe;
5072	u16 free_res_idx, rid;
5073	int lookup = 0;
5074	int recp_cnt;
 
 
 
5075	int status;
5076	int word;
5077	int i;
5078
5079	recp_cnt = ice_calc_recp_cnt(rm->n_ext_words);
5080
 
 
 
 
 
 
5081	bitmap_zero(result_idx_bm, ICE_MAX_FV_WORDS);
5082	bitmap_zero(rm->r_bitmap, ICE_MAX_NUM_RECIPES);
5083
5084	/* Check number of free result indices */
5085	free_res_idx = ice_find_free_recp_res_idx(hw, profiles, result_idx_bm);
5086
5087	ice_debug(hw, ICE_DBG_SW, "Result idx slots: %d, need %d\n",
5088		  free_res_idx, recp_cnt);
5089
5090	/* Last recipe doesn't need result index */
5091	if (recp_cnt - 1 > free_res_idx)
5092		return -ENOSPC;
5093
5094	if (recp_cnt > ICE_MAX_CHAIN_RECIPE_RES)
5095		return -E2BIG;
5096
5097	buf = kcalloc(recp_cnt, sizeof(*buf), GFP_KERNEL);
5098	if (!buf)
 
 
 
5099		return -ENOMEM;
5100
5101	/* Setup the non-root subrecipes. These do not contain lookups for other
5102	 * subrecipes results. Set associated recipe only to own recipe index.
5103	 * Each non-root subrecipe needs a free result index from FV.
5104	 *
5105	 * Note: only done if there is more than one recipe.
 
 
 
 
 
 
 
 
 
 
 
5106	 */
5107	for (i = 0; i < recp_cnt - 1; i++) {
5108		struct ice_aqc_recipe_content *content;
5109		u8 result_idx;
5110
5111		status = ice_alloc_recipe(hw, &rid);
5112		if (status)
5113			return status;
5114
5115		fill_recipe_template(&buf[i], rid, rm);
5116
5117		result_idx = find_first_bit(result_idx_bm, ICE_MAX_FV_WORDS);
5118		/* Check if there really is a valid result index that can be
5119		 * used.
5120		 */
5121		if (result_idx >= ICE_MAX_FV_WORDS) {
5122			ice_debug(hw, ICE_DBG_SW, "No chain index available\n");
5123			return -ENOSPC;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5124		}
5125		clear_bit(result_idx, result_idx_bm);
5126
5127		content = &buf[i].content;
5128		content->result_indx = ICE_AQ_RECIPE_RESULT_EN |
5129				       FIELD_PREP(ICE_AQ_RECIPE_RESULT_DATA_M,
5130						  result_idx);
5131
5132		/* Set recipe association to be used for root recipe */
5133		set_bit(rid, rm->r_bitmap);
 
 
 
 
 
 
 
5134
5135		word = 0;
5136		while (lookup < rm->n_ext_words &&
5137		       word < ICE_NUM_WORDS_RECIPE) {
5138			content->lkup_indx[word] = rm->fv_idx[lookup];
5139			content->mask[word] = cpu_to_le16(rm->fv_mask[lookup]);
 
 
 
 
5140
5141			lookup++;
5142			word++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5143		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5144
5145		recipe = &hw->switch_info->recp_list[rid];
5146		set_bit(result_idx, recipe->res_idxs);
5147		bookkeep_recipe(recipe, &buf[i], rm);
5148	}
 
 
5149
5150	/* Setup the root recipe */
5151	status = ice_alloc_recipe(hw, &rid);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5152	if (status)
5153		return status;
5154
5155	recipe = &hw->switch_info->recp_list[rid];
5156	root = &buf[recp_cnt - 1];
5157	fill_recipe_template(root, rid, rm);
5158
5159	/* Set recipe association, use previously set bitmap and own rid */
5160	set_bit(rid, rm->r_bitmap);
5161	memcpy(root->recipe_bitmap, rm->r_bitmap, sizeof(root->recipe_bitmap));
5162
5163	/* For non-root recipes rid should be 0, for root it should be correct
5164	 * rid value ored with 0x80 (is root bit).
5165	 */
5166	root->content.rid = rid | ICE_AQ_RECIPE_ID_IS_ROOT;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5167
5168	/* Fill remaining lookups in root recipe */
5169	word = 0;
5170	while (lookup < rm->n_ext_words &&
5171	       word < ICE_NUM_WORDS_RECIPE /* should always be true */) {
5172		root->content.lkup_indx[word] = rm->fv_idx[lookup];
5173		root->content.mask[word] = cpu_to_le16(rm->fv_mask[lookup]);
5174
5175		lookup++;
5176		word++;
5177	}
5178
5179	/* Fill result indexes as lookups */
5180	i = 0;
5181	while (i < recp_cnt - 1 &&
5182	       word < ICE_NUM_WORDS_RECIPE /* should always be true */) {
5183		root->content.lkup_indx[word] = buf[i].content.result_indx &
5184						~ICE_AQ_RECIPE_RESULT_EN;
5185		root->content.mask[word] = cpu_to_le16(0xffff);
5186		/* For bookkeeping, it is needed to mark FV index as used for
5187		 * intermediate result.
5188		 */
5189		set_bit(root->content.lkup_indx[word], recipe->res_idxs);
5190
5191		i++;
5192		word++;
 
 
 
 
 
 
 
 
 
 
 
 
 
5193	}
 
 
 
5194
5195	rm->root_rid = rid;
5196	bookkeep_recipe(&hw->switch_info->recp_list[rid], root, rm);
 
 
 
 
5197
5198	/* Program the recipe */
5199	status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
5200	if (status)
5201		return status;
 
 
 
 
 
 
 
 
5202
5203	status = ice_aq_add_recipe(hw, buf, recp_cnt, NULL);
5204	ice_release_change_lock(hw);
5205	if (status)
5206		return status;
5207
5208	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5209}
5210
5211/* ice_get_compat_fv_bitmap - Get compatible field vector bitmap for rule
5212 * @hw: pointer to hardware structure
5213 * @rinfo: other information regarding the rule e.g. priority and action info
5214 * @bm: pointer to memory for returning the bitmap of field vectors
5215 */
5216static void
5217ice_get_compat_fv_bitmap(struct ice_hw *hw, struct ice_adv_rule_info *rinfo,
5218			 unsigned long *bm)
5219{
5220	enum ice_prof_type prof_type;
5221
5222	bitmap_zero(bm, ICE_MAX_NUM_PROFILES);
5223
5224	switch (rinfo->tun_type) {
5225	case ICE_NON_TUN:
5226		prof_type = ICE_PROF_NON_TUN;
5227		break;
5228	case ICE_ALL_TUNNELS:
5229		prof_type = ICE_PROF_TUN_ALL;
5230		break;
5231	case ICE_SW_TUN_GENEVE:
5232	case ICE_SW_TUN_VXLAN:
5233		prof_type = ICE_PROF_TUN_UDP;
5234		break;
5235	case ICE_SW_TUN_NVGRE:
5236		prof_type = ICE_PROF_TUN_GRE;
5237		break;
5238	case ICE_SW_TUN_GTPU:
5239		prof_type = ICE_PROF_TUN_GTPU;
5240		break;
5241	case ICE_SW_TUN_GTPC:
5242		prof_type = ICE_PROF_TUN_GTPC;
5243		break;
5244	case ICE_SW_TUN_PFCP:
5245		prof_type = ICE_PROF_TUN_PFCP;
5246		break;
5247	case ICE_SW_TUN_AND_NON_TUN:
5248	default:
5249		prof_type = ICE_PROF_ALL;
5250		break;
5251	}
5252
5253	ice_get_sw_fv_bitmap(hw, prof_type, bm);
5254}
5255
5256/**
5257 * ice_subscribe_recipe - subscribe to an existing recipe
5258 * @hw: pointer to the hardware structure
5259 * @rid: recipe ID to subscribe to
5260 *
5261 * Return: 0 on success, and others on error
5262 */
5263static int ice_subscribe_recipe(struct ice_hw *hw, u16 rid)
5264{
5265	DEFINE_RAW_FLEX(struct ice_aqc_alloc_free_res_elem, sw_buf, elem, 1);
5266	u16 buf_len = __struct_size(sw_buf);
5267	u16 res_type;
5268	int status;
5269
5270	/* Prepare buffer to allocate resource */
5271	sw_buf->num_elems = cpu_to_le16(1);
5272	res_type = FIELD_PREP(ICE_AQC_RES_TYPE_M, ICE_AQC_RES_TYPE_RECIPE) |
5273		   ICE_AQC_RES_TYPE_FLAG_SUBSCRIBE_SHARED |
5274		   ICE_AQC_RES_TYPE_FLAG_SUBSCRIBE_CTL;
5275	sw_buf->res_type = cpu_to_le16(res_type);
5276
5277	sw_buf->elem[0].e.sw_resp = cpu_to_le16(rid);
5278
5279	status = ice_aq_alloc_free_res(hw, sw_buf, buf_len,
5280				       ice_aqc_opc_alloc_res);
5281
5282	return status;
5283}
5284
5285/**
5286 * ice_subscribable_recp_shared - share an existing subscribable recipe
5287 * @hw: pointer to the hardware structure
5288 * @rid: recipe ID to subscribe to
5289 */
5290static void ice_subscribable_recp_shared(struct ice_hw *hw, u16 rid)
5291{
5292	struct ice_sw_recipe *recps = hw->switch_info->recp_list;
5293	u16 sub_rid;
5294
5295	for_each_set_bit(sub_rid, recps[rid].r_bitmap, ICE_MAX_NUM_RECIPES)
5296		ice_subscribe_recipe(hw, sub_rid);
5297}
5298
5299/**
5300 * ice_add_adv_recipe - Add an advanced recipe that is not part of the default
5301 * @hw: pointer to hardware structure
5302 * @lkups: lookup elements or match criteria for the advanced recipe, one
5303 *  structure per protocol header
5304 * @lkups_cnt: number of protocols
5305 * @rinfo: other information regarding the rule e.g. priority and action info
5306 * @rid: return the recipe ID of the recipe created
5307 */
5308static int
5309ice_add_adv_recipe(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5310		   u16 lkups_cnt, struct ice_adv_rule_info *rinfo, u16 *rid)
5311{
5312	DECLARE_BITMAP(fv_bitmap, ICE_MAX_NUM_PROFILES);
5313	DECLARE_BITMAP(profiles, ICE_MAX_NUM_PROFILES);
5314	struct ice_prot_lkup_ext *lkup_exts;
 
5315	struct ice_sw_fv_list_entry *fvit;
 
5316	struct ice_sw_fv_list_entry *tmp;
5317	struct ice_sw_recipe *rm;
5318	int status = 0;
5319	u16 rid_tmp;
5320	u8 i;
5321
5322	if (!lkups_cnt)
5323		return -EINVAL;
5324
5325	lkup_exts = kzalloc(sizeof(*lkup_exts), GFP_KERNEL);
5326	if (!lkup_exts)
5327		return -ENOMEM;
5328
5329	/* Determine the number of words to be matched and if it exceeds a
5330	 * recipe's restrictions
5331	 */
5332	for (i = 0; i < lkups_cnt; i++) {
5333		u16 count;
5334
5335		if (lkups[i].type >= ICE_PROTOCOL_LAST) {
5336			status = -EIO;
5337			goto err_free_lkup_exts;
5338		}
5339
5340		count = ice_fill_valid_words(&lkups[i], lkup_exts);
5341		if (!count) {
5342			status = -EIO;
5343			goto err_free_lkup_exts;
5344		}
5345	}
5346
5347	rm = kzalloc(sizeof(*rm), GFP_KERNEL);
5348	if (!rm) {
5349		status = -ENOMEM;
5350		goto err_free_lkup_exts;
5351	}
5352
5353	/* Get field vectors that contain fields extracted from all the protocol
5354	 * headers being programmed.
5355	 */
5356	INIT_LIST_HEAD(&rm->fv_list);
 
5357
5358	/* Get bitmap of field vectors (profiles) that are compatible with the
5359	 * rule request; only these will be searched in the subsequent call to
5360	 * ice_get_sw_fv_list.
5361	 */
5362	ice_get_compat_fv_bitmap(hw, rinfo, fv_bitmap);
5363
5364	status = ice_get_sw_fv_list(hw, lkup_exts, fv_bitmap, &rm->fv_list);
5365	if (status)
5366		goto err_unroll;
5367
5368	/* Copy FV words and masks from lkup_exts to recipe struct. */
5369	rm->n_ext_words = lkup_exts->n_val_words;
5370	memcpy(rm->ext_words, lkup_exts->fv_words, sizeof(rm->ext_words));
5371	memcpy(rm->word_masks, lkup_exts->field_mask, sizeof(rm->word_masks));
 
 
5372
5373	/* set the recipe priority if specified */
5374	rm->priority = (u8)rinfo->priority;
5375
5376	rm->need_pass_l2 = rinfo->need_pass_l2;
5377	rm->allow_pass_l2 = rinfo->allow_pass_l2;
5378
5379	/* Find offsets from the field vector. Pick the first one for all the
5380	 * recipes.
5381	 */
5382	status = ice_fill_fv_word_index(hw, rm);
5383	if (status)
5384		goto err_unroll;
5385
5386	/* get bitmap of all profiles the recipe will be associated with */
5387	bitmap_zero(profiles, ICE_MAX_NUM_PROFILES);
5388	list_for_each_entry(fvit, &rm->fv_list, list_entry) {
5389		ice_debug(hw, ICE_DBG_SW, "profile: %d\n", fvit->profile_id);
5390		set_bit((u16)fvit->profile_id, profiles);
5391	}
5392
5393	/* Look for a recipe which matches our requested fv / mask list */
5394	*rid = ice_find_recp(hw, lkup_exts, rinfo, true);
5395	if (*rid < ICE_MAX_NUM_RECIPES) {
5396		/* Success if found a recipe that match the existing criteria */
5397		if (hw->recp_reuse)
5398			ice_subscribable_recp_shared(hw, *rid);
5399
5400		goto err_unroll;
5401	}
5402
5403	rm->tun_type = rinfo->tun_type;
5404	/* Recipe we need does not exist, add a recipe */
5405	status = ice_add_sw_recipe(hw, rm, profiles);
5406	if (status)
5407		goto err_unroll;
5408
5409	/* Associate all the recipes created with all the profiles in the
5410	 * common field vector.
5411	 */
5412	list_for_each_entry(fvit, &rm->fv_list, list_entry) {
5413		DECLARE_BITMAP(r_bitmap, ICE_MAX_NUM_RECIPES);
5414		u64 recp_assoc;
5415		u16 j;
5416
5417		status = ice_aq_get_recipe_to_profile(hw, fvit->profile_id,
5418						      &recp_assoc, NULL);
5419		if (status)
5420			goto err_free_recipe;
5421
5422		bitmap_from_arr64(r_bitmap, &recp_assoc, ICE_MAX_NUM_RECIPES);
5423		bitmap_or(r_bitmap, r_bitmap, rm->r_bitmap,
5424			  ICE_MAX_NUM_RECIPES);
5425		status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
5426		if (status)
5427			goto err_free_recipe;
5428
5429		bitmap_to_arr64(&recp_assoc, r_bitmap, ICE_MAX_NUM_RECIPES);
5430		status = ice_aq_map_recipe_to_profile(hw, fvit->profile_id,
5431						      recp_assoc, NULL);
 
5432		ice_release_change_lock(hw);
5433
5434		if (status)
5435			goto err_free_recipe;
5436
5437		/* Update profile to recipe bitmap array */
5438		bitmap_copy(profile_to_recipe[fvit->profile_id], r_bitmap,
5439			    ICE_MAX_NUM_RECIPES);
5440
5441		/* Update recipe to profile bitmap array */
5442		for_each_set_bit(j, rm->r_bitmap, ICE_MAX_NUM_RECIPES)
5443			set_bit((u16)fvit->profile_id, recipe_to_profile[j]);
5444	}
5445
5446	*rid = rm->root_rid;
5447	memcpy(&hw->switch_info->recp_list[*rid].lkup_exts, lkup_exts,
5448	       sizeof(*lkup_exts));
5449	goto err_unroll;
5450
5451err_free_recipe:
5452	if (hw->recp_reuse) {
5453		for_each_set_bit(rid_tmp, rm->r_bitmap, ICE_MAX_NUM_RECIPES) {
5454			if (!ice_free_recipe_res(hw, rid_tmp))
5455				clear_bit(rid_tmp, rm->r_bitmap);
5456		}
5457	}
5458
5459err_unroll:
5460	list_for_each_entry_safe(fvit, tmp, &rm->fv_list, list_entry) {
5461		list_del(&fvit->list_entry);
5462		devm_kfree(ice_hw_to_dev(hw), fvit);
5463	}
5464
 
5465	kfree(rm);
5466
5467err_free_lkup_exts:
5468	kfree(lkup_exts);
5469
5470	return status;
5471}
5472
5473/**
5474 * ice_dummy_packet_add_vlan - insert VLAN header to dummy pkt
5475 *
5476 * @dummy_pkt: dummy packet profile pattern to which VLAN tag(s) will be added
5477 * @num_vlan: number of VLAN tags
5478 */
5479static struct ice_dummy_pkt_profile *
5480ice_dummy_packet_add_vlan(const struct ice_dummy_pkt_profile *dummy_pkt,
5481			  u32 num_vlan)
5482{
5483	struct ice_dummy_pkt_profile *profile;
5484	struct ice_dummy_pkt_offsets *offsets;
5485	u32 buf_len, off, etype_off, i;
5486	u8 *pkt;
5487
5488	if (num_vlan < 1 || num_vlan > 2)
5489		return ERR_PTR(-EINVAL);
5490
5491	off = num_vlan * VLAN_HLEN;
5492
5493	buf_len = array_size(num_vlan, sizeof(ice_dummy_vlan_packet_offsets)) +
5494		  dummy_pkt->offsets_len;
5495	offsets = kzalloc(buf_len, GFP_KERNEL);
5496	if (!offsets)
5497		return ERR_PTR(-ENOMEM);
5498
5499	offsets[0] = dummy_pkt->offsets[0];
5500	if (num_vlan == 2) {
5501		offsets[1] = ice_dummy_qinq_packet_offsets[0];
5502		offsets[2] = ice_dummy_qinq_packet_offsets[1];
5503	} else if (num_vlan == 1) {
5504		offsets[1] = ice_dummy_vlan_packet_offsets[0];
5505	}
5506
5507	for (i = 1; dummy_pkt->offsets[i].type != ICE_PROTOCOL_LAST; i++) {
5508		offsets[i + num_vlan].type = dummy_pkt->offsets[i].type;
5509		offsets[i + num_vlan].offset =
5510			dummy_pkt->offsets[i].offset + off;
5511	}
5512	offsets[i + num_vlan] = dummy_pkt->offsets[i];
5513
5514	etype_off = dummy_pkt->offsets[1].offset;
5515
5516	buf_len = array_size(num_vlan, sizeof(ice_dummy_vlan_packet)) +
5517		  dummy_pkt->pkt_len;
5518	pkt = kzalloc(buf_len, GFP_KERNEL);
5519	if (!pkt) {
5520		kfree(offsets);
5521		return ERR_PTR(-ENOMEM);
5522	}
5523
5524	memcpy(pkt, dummy_pkt->pkt, etype_off);
5525	memcpy(pkt + etype_off,
5526	       num_vlan == 2 ? ice_dummy_qinq_packet : ice_dummy_vlan_packet,
5527	       off);
5528	memcpy(pkt + etype_off + off, dummy_pkt->pkt + etype_off,
5529	       dummy_pkt->pkt_len - etype_off);
5530
5531	profile = kzalloc(sizeof(*profile), GFP_KERNEL);
5532	if (!profile) {
5533		kfree(offsets);
5534		kfree(pkt);
5535		return ERR_PTR(-ENOMEM);
5536	}
5537
5538	profile->offsets = offsets;
5539	profile->pkt = pkt;
5540	profile->pkt_len = buf_len;
5541	profile->match |= ICE_PKT_KMALLOC;
5542
5543	return profile;
5544}
5545
5546/**
5547 * ice_find_dummy_packet - find dummy packet
5548 *
5549 * @lkups: lookup elements or match criteria for the advanced recipe, one
5550 *	   structure per protocol header
5551 * @lkups_cnt: number of protocols
5552 * @tun_type: tunnel type
5553 *
5554 * Returns the &ice_dummy_pkt_profile corresponding to these lookup params.
5555 */
5556static const struct ice_dummy_pkt_profile *
5557ice_find_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5558		      enum ice_sw_tunnel_type tun_type)
5559{
5560	const struct ice_dummy_pkt_profile *ret = ice_dummy_pkt_profiles;
5561	u32 match = 0, vlan_count = 0;
5562	u16 i;
5563
5564	switch (tun_type) {
5565	case ICE_SW_TUN_GTPC:
5566		match |= ICE_PKT_TUN_GTPC;
5567		break;
5568	case ICE_SW_TUN_GTPU:
5569		match |= ICE_PKT_TUN_GTPU;
5570		break;
5571	case ICE_SW_TUN_NVGRE:
5572		match |= ICE_PKT_TUN_NVGRE;
5573		break;
5574	case ICE_SW_TUN_GENEVE:
5575	case ICE_SW_TUN_VXLAN:
5576		match |= ICE_PKT_TUN_UDP;
5577		break;
5578	case ICE_SW_TUN_PFCP:
5579		match |= ICE_PKT_PFCP;
5580		break;
5581	default:
5582		break;
5583	}
5584
5585	for (i = 0; i < lkups_cnt; i++) {
5586		if (lkups[i].type == ICE_UDP_ILOS)
5587			match |= ICE_PKT_INNER_UDP;
5588		else if (lkups[i].type == ICE_TCP_IL)
5589			match |= ICE_PKT_INNER_TCP;
5590		else if (lkups[i].type == ICE_IPV6_OFOS)
5591			match |= ICE_PKT_OUTER_IPV6;
5592		else if (lkups[i].type == ICE_VLAN_OFOS ||
5593			 lkups[i].type == ICE_VLAN_EX)
5594			vlan_count++;
5595		else if (lkups[i].type == ICE_VLAN_IN)
5596			vlan_count++;
5597		else if (lkups[i].type == ICE_ETYPE_OL &&
5598			 lkups[i].h_u.ethertype.ethtype_id ==
5599				cpu_to_be16(ICE_IPV6_ETHER_ID) &&
5600			 lkups[i].m_u.ethertype.ethtype_id ==
5601				cpu_to_be16(0xFFFF))
5602			match |= ICE_PKT_OUTER_IPV6;
5603		else if (lkups[i].type == ICE_ETYPE_IL &&
5604			 lkups[i].h_u.ethertype.ethtype_id ==
5605				cpu_to_be16(ICE_IPV6_ETHER_ID) &&
5606			 lkups[i].m_u.ethertype.ethtype_id ==
5607				cpu_to_be16(0xFFFF))
5608			match |= ICE_PKT_INNER_IPV6;
5609		else if (lkups[i].type == ICE_IPV6_IL)
5610			match |= ICE_PKT_INNER_IPV6;
5611		else if (lkups[i].type == ICE_GTP_NO_PAY)
5612			match |= ICE_PKT_GTP_NOPAY;
5613		else if (lkups[i].type == ICE_PPPOE) {
5614			match |= ICE_PKT_PPPOE;
5615			if (lkups[i].h_u.pppoe_hdr.ppp_prot_id ==
5616			    htons(PPP_IPV6))
5617				match |= ICE_PKT_OUTER_IPV6;
5618		} else if (lkups[i].type == ICE_L2TPV3)
5619			match |= ICE_PKT_L2TPV3;
5620	}
5621
5622	while (ret->match && (match & ret->match) != ret->match)
5623		ret++;
5624
5625	if (vlan_count != 0)
5626		ret = ice_dummy_packet_add_vlan(ret, vlan_count);
5627
5628	return ret;
5629}
5630
5631/**
5632 * ice_fill_adv_dummy_packet - fill a dummy packet with given match criteria
5633 *
5634 * @lkups: lookup elements or match criteria for the advanced recipe, one
5635 *	   structure per protocol header
5636 * @lkups_cnt: number of protocols
5637 * @s_rule: stores rule information from the match criteria
5638 * @profile: dummy packet profile (the template, its size and header offsets)
5639 */
5640static int
5641ice_fill_adv_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5642			  struct ice_sw_rule_lkup_rx_tx *s_rule,
5643			  const struct ice_dummy_pkt_profile *profile)
5644{
5645	u8 *pkt;
5646	u16 i;
5647
5648	/* Start with a packet with a pre-defined/dummy content. Then, fill
5649	 * in the header values to be looked up or matched.
5650	 */
5651	pkt = s_rule->hdr_data;
5652
5653	memcpy(pkt, profile->pkt, profile->pkt_len);
5654
5655	for (i = 0; i < lkups_cnt; i++) {
5656		const struct ice_dummy_pkt_offsets *offsets = profile->offsets;
5657		enum ice_protocol_type type;
5658		u16 offset = 0, len = 0, j;
5659		bool found = false;
5660
5661		/* find the start of this layer; it should be found since this
5662		 * was already checked when search for the dummy packet
5663		 */
5664		type = lkups[i].type;
5665		/* metadata isn't present in the packet */
5666		if (type == ICE_HW_METADATA)
5667			continue;
5668
5669		for (j = 0; offsets[j].type != ICE_PROTOCOL_LAST; j++) {
5670			if (type == offsets[j].type) {
5671				offset = offsets[j].offset;
5672				found = true;
5673				break;
5674			}
5675		}
5676		/* this should never happen in a correct calling sequence */
5677		if (!found)
5678			return -EINVAL;
5679
5680		switch (lkups[i].type) {
5681		case ICE_MAC_OFOS:
5682		case ICE_MAC_IL:
5683			len = sizeof(struct ice_ether_hdr);
5684			break;
5685		case ICE_ETYPE_OL:
5686		case ICE_ETYPE_IL:
5687			len = sizeof(struct ice_ethtype_hdr);
5688			break;
5689		case ICE_VLAN_OFOS:
5690		case ICE_VLAN_EX:
5691		case ICE_VLAN_IN:
5692			len = sizeof(struct ice_vlan_hdr);
5693			break;
5694		case ICE_IPV4_OFOS:
5695		case ICE_IPV4_IL:
5696			len = sizeof(struct ice_ipv4_hdr);
5697			break;
5698		case ICE_IPV6_OFOS:
5699		case ICE_IPV6_IL:
5700			len = sizeof(struct ice_ipv6_hdr);
5701			break;
5702		case ICE_TCP_IL:
5703		case ICE_UDP_OF:
5704		case ICE_UDP_ILOS:
5705			len = sizeof(struct ice_l4_hdr);
5706			break;
5707		case ICE_SCTP_IL:
5708			len = sizeof(struct ice_sctp_hdr);
5709			break;
5710		case ICE_NVGRE:
5711			len = sizeof(struct ice_nvgre_hdr);
5712			break;
5713		case ICE_VXLAN:
5714		case ICE_GENEVE:
5715			len = sizeof(struct ice_udp_tnl_hdr);
5716			break;
5717		case ICE_GTP_NO_PAY:
5718		case ICE_GTP:
5719			len = sizeof(struct ice_udp_gtp_hdr);
5720			break;
5721		case ICE_PFCP:
5722			len = sizeof(struct ice_pfcp_hdr);
5723			break;
5724		case ICE_PPPOE:
5725			len = sizeof(struct ice_pppoe_hdr);
5726			break;
5727		case ICE_L2TPV3:
5728			len = sizeof(struct ice_l2tpv3_sess_hdr);
5729			break;
5730		default:
5731			return -EINVAL;
5732		}
5733
5734		/* the length should be a word multiple */
5735		if (len % ICE_BYTES_PER_WORD)
5736			return -EIO;
5737
5738		/* We have the offset to the header start, the length, the
5739		 * caller's header values and mask. Use this information to
5740		 * copy the data into the dummy packet appropriately based on
5741		 * the mask. Note that we need to only write the bits as
5742		 * indicated by the mask to make sure we don't improperly write
5743		 * over any significant packet data.
5744		 */
5745		for (j = 0; j < len / sizeof(u16); j++) {
5746			u16 *ptr = (u16 *)(pkt + offset);
5747			u16 mask = lkups[i].m_raw[j];
5748
5749			if (!mask)
5750				continue;
5751
5752			ptr[j] = (ptr[j] & ~mask) | (lkups[i].h_raw[j] & mask);
5753		}
5754	}
5755
5756	s_rule->hdr_len = cpu_to_le16(profile->pkt_len);
5757
5758	return 0;
5759}
5760
5761/**
5762 * ice_fill_adv_packet_tun - fill dummy packet with udp tunnel port
5763 * @hw: pointer to the hardware structure
5764 * @tun_type: tunnel type
5765 * @pkt: dummy packet to fill in
5766 * @offsets: offset info for the dummy packet
5767 */
5768static int
5769ice_fill_adv_packet_tun(struct ice_hw *hw, enum ice_sw_tunnel_type tun_type,
5770			u8 *pkt, const struct ice_dummy_pkt_offsets *offsets)
5771{
5772	u16 open_port, i;
5773
5774	switch (tun_type) {
5775	case ICE_SW_TUN_VXLAN:
5776		if (!ice_get_open_tunnel_port(hw, &open_port, TNL_VXLAN))
5777			return -EIO;
5778		break;
5779	case ICE_SW_TUN_GENEVE:
5780		if (!ice_get_open_tunnel_port(hw, &open_port, TNL_GENEVE))
5781			return -EIO;
5782		break;
5783	default:
5784		/* Nothing needs to be done for this tunnel type */
5785		return 0;
5786	}
5787
5788	/* Find the outer UDP protocol header and insert the port number */
5789	for (i = 0; offsets[i].type != ICE_PROTOCOL_LAST; i++) {
5790		if (offsets[i].type == ICE_UDP_OF) {
5791			struct ice_l4_hdr *hdr;
5792			u16 offset;
5793
5794			offset = offsets[i].offset;
5795			hdr = (struct ice_l4_hdr *)&pkt[offset];
5796			hdr->dst_port = cpu_to_be16(open_port);
5797
5798			return 0;
5799		}
5800	}
5801
5802	return -EIO;
5803}
5804
5805/**
5806 * ice_fill_adv_packet_vlan - fill dummy packet with VLAN tag type
5807 * @hw: pointer to hw structure
5808 * @vlan_type: VLAN tag type
5809 * @pkt: dummy packet to fill in
5810 * @offsets: offset info for the dummy packet
5811 */
5812static int
5813ice_fill_adv_packet_vlan(struct ice_hw *hw, u16 vlan_type, u8 *pkt,
5814			 const struct ice_dummy_pkt_offsets *offsets)
5815{
5816	u16 i;
5817
5818	/* Check if there is something to do */
5819	if (!vlan_type || !ice_is_dvm_ena(hw))
5820		return 0;
5821
5822	/* Find VLAN header and insert VLAN TPID */
5823	for (i = 0; offsets[i].type != ICE_PROTOCOL_LAST; i++) {
5824		if (offsets[i].type == ICE_VLAN_OFOS ||
5825		    offsets[i].type == ICE_VLAN_EX) {
5826			struct ice_vlan_hdr *hdr;
5827			u16 offset;
5828
5829			offset = offsets[i].offset;
5830			hdr = (struct ice_vlan_hdr *)&pkt[offset];
5831			hdr->type = cpu_to_be16(vlan_type);
5832
5833			return 0;
5834		}
5835	}
5836
5837	return -EIO;
5838}
5839
5840static bool ice_rules_equal(const struct ice_adv_rule_info *first,
5841			    const struct ice_adv_rule_info *second)
5842{
5843	return first->sw_act.flag == second->sw_act.flag &&
5844	       first->tun_type == second->tun_type &&
5845	       first->vlan_type == second->vlan_type &&
5846	       first->src_vsi == second->src_vsi &&
5847	       first->need_pass_l2 == second->need_pass_l2 &&
5848	       first->allow_pass_l2 == second->allow_pass_l2;
5849}
5850
5851/**
5852 * ice_find_adv_rule_entry - Search a rule entry
5853 * @hw: pointer to the hardware structure
5854 * @lkups: lookup elements or match criteria for the advanced recipe, one
5855 *	   structure per protocol header
5856 * @lkups_cnt: number of protocols
5857 * @recp_id: recipe ID for which we are finding the rule
5858 * @rinfo: other information regarding the rule e.g. priority and action info
5859 *
5860 * Helper function to search for a given advance rule entry
5861 * Returns pointer to entry storing the rule if found
5862 */
5863static struct ice_adv_fltr_mgmt_list_entry *
5864ice_find_adv_rule_entry(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5865			u16 lkups_cnt, u16 recp_id,
5866			struct ice_adv_rule_info *rinfo)
5867{
5868	struct ice_adv_fltr_mgmt_list_entry *list_itr;
5869	struct ice_switch_info *sw = hw->switch_info;
5870	int i;
5871
5872	list_for_each_entry(list_itr, &sw->recp_list[recp_id].filt_rules,
5873			    list_entry) {
5874		bool lkups_matched = true;
5875
5876		if (lkups_cnt != list_itr->lkups_cnt)
5877			continue;
5878		for (i = 0; i < list_itr->lkups_cnt; i++)
5879			if (memcmp(&list_itr->lkups[i], &lkups[i],
5880				   sizeof(*lkups))) {
5881				lkups_matched = false;
5882				break;
5883			}
5884		if (ice_rules_equal(rinfo, &list_itr->rule_info) &&
5885		    lkups_matched)
5886			return list_itr;
5887	}
5888	return NULL;
5889}
5890
5891/**
5892 * ice_adv_add_update_vsi_list
5893 * @hw: pointer to the hardware structure
5894 * @m_entry: pointer to current adv filter management list entry
5895 * @cur_fltr: filter information from the book keeping entry
5896 * @new_fltr: filter information with the new VSI to be added
5897 *
5898 * Call AQ command to add or update previously created VSI list with new VSI.
5899 *
5900 * Helper function to do book keeping associated with adding filter information
5901 * The algorithm to do the booking keeping is described below :
5902 * When a VSI needs to subscribe to a given advanced filter
5903 *	if only one VSI has been added till now
5904 *		Allocate a new VSI list and add two VSIs
5905 *		to this list using switch rule command
5906 *		Update the previously created switch rule with the
5907 *		newly created VSI list ID
5908 *	if a VSI list was previously created
5909 *		Add the new VSI to the previously created VSI list set
5910 *		using the update switch rule command
5911 */
5912static int
5913ice_adv_add_update_vsi_list(struct ice_hw *hw,
5914			    struct ice_adv_fltr_mgmt_list_entry *m_entry,
5915			    struct ice_adv_rule_info *cur_fltr,
5916			    struct ice_adv_rule_info *new_fltr)
5917{
5918	u16 vsi_list_id = 0;
5919	int status;
5920
5921	if (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5922	    cur_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP ||
5923	    cur_fltr->sw_act.fltr_act == ICE_DROP_PACKET)
5924		return -EOPNOTSUPP;
5925
5926	if ((new_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5927	     new_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP) &&
5928	    (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI ||
5929	     cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI_LIST))
5930		return -EOPNOTSUPP;
5931
5932	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
5933		 /* Only one entry existed in the mapping and it was not already
5934		  * a part of a VSI list. So, create a VSI list with the old and
5935		  * new VSIs.
5936		  */
5937		struct ice_fltr_info tmp_fltr;
5938		u16 vsi_handle_arr[2];
5939
5940		/* A rule already exists with the new VSI being added */
5941		if (cur_fltr->sw_act.fwd_id.hw_vsi_id ==
5942		    new_fltr->sw_act.fwd_id.hw_vsi_id)
5943			return -EEXIST;
5944
5945		vsi_handle_arr[0] = cur_fltr->sw_act.vsi_handle;
5946		vsi_handle_arr[1] = new_fltr->sw_act.vsi_handle;
5947		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
5948						  &vsi_list_id,
5949						  ICE_SW_LKUP_LAST);
5950		if (status)
5951			return status;
5952
5953		memset(&tmp_fltr, 0, sizeof(tmp_fltr));
5954		tmp_fltr.flag = m_entry->rule_info.sw_act.flag;
5955		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
5956		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
5957		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
5958		tmp_fltr.lkup_type = ICE_SW_LKUP_LAST;
5959
5960		/* Update the previous switch rule of "forward to VSI" to
5961		 * "fwd to VSI list"
5962		 */
5963		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5964		if (status)
5965			return status;
5966
5967		cur_fltr->sw_act.fwd_id.vsi_list_id = vsi_list_id;
5968		cur_fltr->sw_act.fltr_act = ICE_FWD_TO_VSI_LIST;
5969		m_entry->vsi_list_info =
5970			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
5971						vsi_list_id);
5972	} else {
5973		u16 vsi_handle = new_fltr->sw_act.vsi_handle;
5974
5975		if (!m_entry->vsi_list_info)
5976			return -EIO;
5977
5978		/* A rule already exists with the new VSI being added */
5979		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
5980			return 0;
5981
5982		/* Update the previously created VSI list set with
5983		 * the new VSI ID passed in
5984		 */
5985		vsi_list_id = cur_fltr->sw_act.fwd_id.vsi_list_id;
5986
5987		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
5988						  vsi_list_id, false,
5989						  ice_aqc_opc_update_sw_rules,
5990						  ICE_SW_LKUP_LAST);
5991		/* update VSI list mapping info with new VSI ID */
5992		if (!status)
5993			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
5994	}
5995	if (!status)
5996		m_entry->vsi_count++;
5997	return status;
5998}
5999
6000void ice_rule_add_tunnel_metadata(struct ice_adv_lkup_elem *lkup)
6001{
6002	lkup->type = ICE_HW_METADATA;
6003	lkup->m_u.metadata.flags[ICE_PKT_FLAGS_MDID21] |=
6004		cpu_to_be16(ICE_PKT_TUNNEL_MASK);
6005}
6006
6007void ice_rule_add_direction_metadata(struct ice_adv_lkup_elem *lkup)
6008{
6009	lkup->type = ICE_HW_METADATA;
6010	lkup->m_u.metadata.flags[ICE_PKT_FLAGS_MDID20] |=
6011		cpu_to_be16(ICE_PKT_FROM_NETWORK);
6012}
6013
6014void ice_rule_add_vlan_metadata(struct ice_adv_lkup_elem *lkup)
6015{
6016	lkup->type = ICE_HW_METADATA;
6017	lkup->m_u.metadata.flags[ICE_PKT_FLAGS_MDID20] |=
6018		cpu_to_be16(ICE_PKT_VLAN_MASK);
6019}
6020
6021void ice_rule_add_src_vsi_metadata(struct ice_adv_lkup_elem *lkup)
6022{
6023	lkup->type = ICE_HW_METADATA;
6024	lkup->m_u.metadata.source_vsi = cpu_to_be16(ICE_MDID_SOURCE_VSI_MASK);
6025}
6026
6027/**
6028 * ice_add_adv_rule - helper function to create an advanced switch rule
6029 * @hw: pointer to the hardware structure
6030 * @lkups: information on the words that needs to be looked up. All words
6031 * together makes one recipe
6032 * @lkups_cnt: num of entries in the lkups array
6033 * @rinfo: other information related to the rule that needs to be programmed
6034 * @added_entry: this will return recipe_id, rule_id and vsi_handle. should be
6035 *               ignored is case of error.
6036 *
6037 * This function can program only 1 rule at a time. The lkups is used to
6038 * describe the all the words that forms the "lookup" portion of the recipe.
6039 * These words can span multiple protocols. Callers to this function need to
6040 * pass in a list of protocol headers with lookup information along and mask
6041 * that determines which words are valid from the given protocol header.
6042 * rinfo describes other information related to this rule such as forwarding
6043 * IDs, priority of this rule, etc.
6044 */
6045int
6046ice_add_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
6047		 u16 lkups_cnt, struct ice_adv_rule_info *rinfo,
6048		 struct ice_rule_query_data *added_entry)
6049{
6050	struct ice_adv_fltr_mgmt_list_entry *m_entry, *adv_fltr = NULL;
6051	struct ice_sw_rule_lkup_rx_tx *s_rule = NULL;
6052	const struct ice_dummy_pkt_profile *profile;
6053	u16 rid = 0, i, rule_buf_sz, vsi_handle;
6054	struct list_head *rule_head;
6055	struct ice_switch_info *sw;
6056	u16 word_cnt;
6057	u32 act = 0;
6058	int status;
6059	u8 q_rgn;
6060
6061	/* Initialize profile to result index bitmap */
6062	if (!hw->switch_info->prof_res_bm_init) {
6063		hw->switch_info->prof_res_bm_init = 1;
6064		ice_init_prof_result_bm(hw);
6065	}
6066
6067	if (!lkups_cnt)
6068		return -EINVAL;
6069
6070	/* get # of words we need to match */
6071	word_cnt = 0;
6072	for (i = 0; i < lkups_cnt; i++) {
6073		u16 j;
6074
6075		for (j = 0; j < ARRAY_SIZE(lkups->m_raw); j++)
6076			if (lkups[i].m_raw[j])
6077				word_cnt++;
6078	}
6079
6080	if (!word_cnt)
6081		return -EINVAL;
6082
6083	if (word_cnt > ICE_MAX_CHAIN_WORDS)
6084		return -ENOSPC;
6085
6086	/* locate a dummy packet */
6087	profile = ice_find_dummy_packet(lkups, lkups_cnt, rinfo->tun_type);
6088	if (IS_ERR(profile))
6089		return PTR_ERR(profile);
6090
6091	if (!(rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI ||
6092	      rinfo->sw_act.fltr_act == ICE_FWD_TO_Q ||
6093	      rinfo->sw_act.fltr_act == ICE_FWD_TO_QGRP ||
6094	      rinfo->sw_act.fltr_act == ICE_DROP_PACKET ||
6095	      rinfo->sw_act.fltr_act == ICE_MIRROR_PACKET ||
6096	      rinfo->sw_act.fltr_act == ICE_NOP)) {
6097		status = -EIO;
6098		goto free_pkt_profile;
6099	}
6100
6101	vsi_handle = rinfo->sw_act.vsi_handle;
6102	if (!ice_is_vsi_valid(hw, vsi_handle)) {
6103		status =  -EINVAL;
6104		goto free_pkt_profile;
6105	}
6106
6107	if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI ||
6108	    rinfo->sw_act.fltr_act == ICE_MIRROR_PACKET ||
6109	    rinfo->sw_act.fltr_act == ICE_NOP) {
6110		rinfo->sw_act.fwd_id.hw_vsi_id =
6111			ice_get_hw_vsi_num(hw, vsi_handle);
6112	}
6113
6114	if (rinfo->src_vsi)
6115		rinfo->sw_act.src = ice_get_hw_vsi_num(hw, rinfo->src_vsi);
6116	else
6117		rinfo->sw_act.src = ice_get_hw_vsi_num(hw, vsi_handle);
6118
6119	status = ice_add_adv_recipe(hw, lkups, lkups_cnt, rinfo, &rid);
6120	if (status)
6121		goto free_pkt_profile;
6122	m_entry = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
6123	if (m_entry) {
6124		/* we have to add VSI to VSI_LIST and increment vsi_count.
6125		 * Also Update VSI list so that we can change forwarding rule
6126		 * if the rule already exists, we will check if it exists with
6127		 * same vsi_id, if not then add it to the VSI list if it already
6128		 * exists if not then create a VSI list and add the existing VSI
6129		 * ID and the new VSI ID to the list
6130		 * We will add that VSI to the list
6131		 */
6132		status = ice_adv_add_update_vsi_list(hw, m_entry,
6133						     &m_entry->rule_info,
6134						     rinfo);
6135		if (added_entry) {
6136			added_entry->rid = rid;
6137			added_entry->rule_id = m_entry->rule_info.fltr_rule_id;
6138			added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
6139		}
6140		goto free_pkt_profile;
6141	}
6142	rule_buf_sz = ICE_SW_RULE_RX_TX_HDR_SIZE(s_rule, profile->pkt_len);
6143	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
6144	if (!s_rule) {
6145		status = -ENOMEM;
6146		goto free_pkt_profile;
6147	}
6148
6149	if (rinfo->sw_act.fltr_act != ICE_MIRROR_PACKET) {
6150		if (!rinfo->flags_info.act_valid) {
6151			act |= ICE_SINGLE_ACT_LAN_ENABLE;
6152			act |= ICE_SINGLE_ACT_LB_ENABLE;
6153		} else {
6154			act |= rinfo->flags_info.act & (ICE_SINGLE_ACT_LAN_ENABLE |
6155							ICE_SINGLE_ACT_LB_ENABLE);
6156		}
6157	}
6158
6159	switch (rinfo->sw_act.fltr_act) {
6160	case ICE_FWD_TO_VSI:
6161		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
6162				  rinfo->sw_act.fwd_id.hw_vsi_id);
6163		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_VALID_BIT;
6164		break;
6165	case ICE_FWD_TO_Q:
6166		act |= ICE_SINGLE_ACT_TO_Q;
6167		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
6168				  rinfo->sw_act.fwd_id.q_id);
6169		break;
6170	case ICE_FWD_TO_QGRP:
6171		q_rgn = rinfo->sw_act.qgrp_size > 0 ?
6172			(u8)ilog2(rinfo->sw_act.qgrp_size) : 0;
6173		act |= ICE_SINGLE_ACT_TO_Q;
6174		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
6175				  rinfo->sw_act.fwd_id.q_id);
6176		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_REGION_M, q_rgn);
6177		break;
6178	case ICE_DROP_PACKET:
6179		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
6180		       ICE_SINGLE_ACT_VALID_BIT;
6181		break;
6182	case ICE_MIRROR_PACKET:
6183		act |= ICE_SINGLE_ACT_OTHER_ACTS;
6184		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
6185				  rinfo->sw_act.fwd_id.hw_vsi_id);
6186		break;
6187	case ICE_NOP:
6188		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
6189				  rinfo->sw_act.fwd_id.hw_vsi_id);
6190		act &= ~ICE_SINGLE_ACT_VALID_BIT;
6191		break;
6192	default:
6193		status = -EIO;
6194		goto err_ice_add_adv_rule;
6195	}
6196
6197	/* If there is no matching criteria for direction there
6198	 * is only one difference between Rx and Tx:
6199	 * - get switch id base on VSI number from source field (Tx)
6200	 * - get switch id base on port number (Rx)
6201	 *
6202	 * If matching on direction metadata is chose rule direction is
6203	 * extracted from type value set here.
6204	 */
6205	if (rinfo->sw_act.flag & ICE_FLTR_TX) {
6206		s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
6207		s_rule->src = cpu_to_le16(rinfo->sw_act.src);
6208	} else {
6209		s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX);
6210		s_rule->src = cpu_to_le16(hw->port_info->lport);
6211	}
6212
6213	s_rule->recipe_id = cpu_to_le16(rid);
6214	s_rule->act = cpu_to_le32(act);
6215
6216	status = ice_fill_adv_dummy_packet(lkups, lkups_cnt, s_rule, profile);
6217	if (status)
6218		goto err_ice_add_adv_rule;
6219
6220	status = ice_fill_adv_packet_tun(hw, rinfo->tun_type, s_rule->hdr_data,
6221					 profile->offsets);
6222	if (status)
6223		goto err_ice_add_adv_rule;
6224
6225	status = ice_fill_adv_packet_vlan(hw, rinfo->vlan_type,
6226					  s_rule->hdr_data,
6227					  profile->offsets);
6228	if (status)
6229		goto err_ice_add_adv_rule;
6230
6231	status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
6232				 rule_buf_sz, 1, ice_aqc_opc_add_sw_rules,
6233				 NULL);
6234	if (status)
6235		goto err_ice_add_adv_rule;
6236	adv_fltr = devm_kzalloc(ice_hw_to_dev(hw),
6237				sizeof(struct ice_adv_fltr_mgmt_list_entry),
6238				GFP_KERNEL);
6239	if (!adv_fltr) {
6240		status = -ENOMEM;
6241		goto err_ice_add_adv_rule;
6242	}
6243
6244	adv_fltr->lkups = devm_kmemdup(ice_hw_to_dev(hw), lkups,
6245				       lkups_cnt * sizeof(*lkups), GFP_KERNEL);
6246	if (!adv_fltr->lkups) {
6247		status = -ENOMEM;
6248		goto err_ice_add_adv_rule;
6249	}
6250
6251	adv_fltr->lkups_cnt = lkups_cnt;
6252	adv_fltr->rule_info = *rinfo;
6253	adv_fltr->rule_info.fltr_rule_id = le16_to_cpu(s_rule->index);
6254	sw = hw->switch_info;
6255	sw->recp_list[rid].adv_rule = true;
6256	rule_head = &sw->recp_list[rid].filt_rules;
6257
6258	if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI)
6259		adv_fltr->vsi_count = 1;
6260
6261	/* Add rule entry to book keeping list */
6262	list_add(&adv_fltr->list_entry, rule_head);
6263	if (added_entry) {
6264		added_entry->rid = rid;
6265		added_entry->rule_id = adv_fltr->rule_info.fltr_rule_id;
6266		added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
6267	}
6268err_ice_add_adv_rule:
6269	if (status && adv_fltr) {
6270		devm_kfree(ice_hw_to_dev(hw), adv_fltr->lkups);
6271		devm_kfree(ice_hw_to_dev(hw), adv_fltr);
6272	}
6273
6274	kfree(s_rule);
6275
6276free_pkt_profile:
6277	if (profile->match & ICE_PKT_KMALLOC) {
6278		kfree(profile->offsets);
6279		kfree(profile->pkt);
6280		kfree(profile);
6281	}
6282
6283	return status;
6284}
6285
6286/**
6287 * ice_replay_vsi_fltr - Replay filters for requested VSI
6288 * @hw: pointer to the hardware structure
6289 * @vsi_handle: driver VSI handle
6290 * @recp_id: Recipe ID for which rules need to be replayed
6291 * @list_head: list for which filters need to be replayed
6292 *
6293 * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
6294 * It is required to pass valid VSI handle.
6295 */
6296static int
6297ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
6298		    struct list_head *list_head)
6299{
6300	struct ice_fltr_mgmt_list_entry *itr;
6301	int status = 0;
6302	u16 hw_vsi_id;
6303
6304	if (list_empty(list_head))
6305		return status;
6306	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
6307
6308	list_for_each_entry(itr, list_head, list_entry) {
6309		struct ice_fltr_list_entry f_entry;
6310
6311		f_entry.fltr_info = itr->fltr_info;
6312		if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
6313		    itr->fltr_info.vsi_handle == vsi_handle) {
6314			/* update the src in case it is VSI num */
6315			if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6316				f_entry.fltr_info.src = hw_vsi_id;
6317			status = ice_add_rule_internal(hw, recp_id, &f_entry);
6318			if (status)
6319				goto end;
6320			continue;
6321		}
6322		if (!itr->vsi_list_info ||
6323		    !test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
6324			continue;
 
 
6325		f_entry.fltr_info.vsi_handle = vsi_handle;
6326		f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
6327		/* update the src in case it is VSI num */
6328		if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6329			f_entry.fltr_info.src = hw_vsi_id;
6330		if (recp_id == ICE_SW_LKUP_VLAN)
6331			status = ice_add_vlan_internal(hw, &f_entry);
6332		else
6333			status = ice_add_rule_internal(hw, recp_id, &f_entry);
6334		if (status)
6335			goto end;
6336	}
6337end:
6338	return status;
6339}
6340
6341/**
6342 * ice_adv_rem_update_vsi_list
6343 * @hw: pointer to the hardware structure
6344 * @vsi_handle: VSI handle of the VSI to remove
6345 * @fm_list: filter management entry for which the VSI list management needs to
6346 *	     be done
6347 */
6348static int
6349ice_adv_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
6350			    struct ice_adv_fltr_mgmt_list_entry *fm_list)
6351{
6352	struct ice_vsi_list_map_info *vsi_list_info;
6353	enum ice_sw_lkup_type lkup_type;
6354	u16 vsi_list_id;
6355	int status;
6356
6357	if (fm_list->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST ||
6358	    fm_list->vsi_count == 0)
6359		return -EINVAL;
6360
6361	/* A rule with the VSI being removed does not exist */
6362	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
6363		return -ENOENT;
6364
6365	lkup_type = ICE_SW_LKUP_LAST;
6366	vsi_list_id = fm_list->rule_info.sw_act.fwd_id.vsi_list_id;
6367	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
6368					  ice_aqc_opc_update_sw_rules,
6369					  lkup_type);
6370	if (status)
6371		return status;
6372
6373	fm_list->vsi_count--;
6374	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
6375	vsi_list_info = fm_list->vsi_list_info;
6376	if (fm_list->vsi_count == 1) {
6377		struct ice_fltr_info tmp_fltr;
6378		u16 rem_vsi_handle;
6379
6380		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
6381						ICE_MAX_VSI);
6382		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
6383			return -EIO;
6384
6385		/* Make sure VSI list is empty before removing it below */
6386		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
6387						  vsi_list_id, true,
6388						  ice_aqc_opc_update_sw_rules,
6389						  lkup_type);
6390		if (status)
6391			return status;
6392
6393		memset(&tmp_fltr, 0, sizeof(tmp_fltr));
6394		tmp_fltr.flag = fm_list->rule_info.sw_act.flag;
6395		tmp_fltr.fltr_rule_id = fm_list->rule_info.fltr_rule_id;
6396		fm_list->rule_info.sw_act.fltr_act = ICE_FWD_TO_VSI;
6397		tmp_fltr.fltr_act = ICE_FWD_TO_VSI;
6398		tmp_fltr.fwd_id.hw_vsi_id =
6399			ice_get_hw_vsi_num(hw, rem_vsi_handle);
6400		fm_list->rule_info.sw_act.fwd_id.hw_vsi_id =
6401			ice_get_hw_vsi_num(hw, rem_vsi_handle);
6402		fm_list->rule_info.sw_act.vsi_handle = rem_vsi_handle;
6403
6404		/* Update the previous switch rule of "MAC forward to VSI" to
6405		 * "MAC fwd to VSI list"
6406		 */
6407		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
6408		if (status) {
6409			ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
6410				  tmp_fltr.fwd_id.hw_vsi_id, status);
6411			return status;
6412		}
6413		fm_list->vsi_list_info->ref_cnt--;
6414
6415		/* Remove the VSI list since it is no longer used */
6416		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
6417		if (status) {
6418			ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
6419				  vsi_list_id, status);
6420			return status;
6421		}
6422
6423		list_del(&vsi_list_info->list_entry);
6424		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
6425		fm_list->vsi_list_info = NULL;
6426	}
6427
6428	return status;
6429}
6430
6431/**
6432 * ice_rem_adv_rule - removes existing advanced switch rule
6433 * @hw: pointer to the hardware structure
6434 * @lkups: information on the words that needs to be looked up. All words
6435 *         together makes one recipe
6436 * @lkups_cnt: num of entries in the lkups array
6437 * @rinfo: Its the pointer to the rule information for the rule
6438 *
6439 * This function can be used to remove 1 rule at a time. The lkups is
6440 * used to describe all the words that forms the "lookup" portion of the
6441 * rule. These words can span multiple protocols. Callers to this function
6442 * need to pass in a list of protocol headers with lookup information along
6443 * and mask that determines which words are valid from the given protocol
6444 * header. rinfo describes other information related to this rule such as
6445 * forwarding IDs, priority of this rule, etc.
6446 */
6447static int
6448ice_rem_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
6449		 u16 lkups_cnt, struct ice_adv_rule_info *rinfo)
6450{
6451	struct ice_adv_fltr_mgmt_list_entry *list_elem;
6452	struct ice_prot_lkup_ext lkup_exts;
6453	bool remove_rule = false;
6454	struct mutex *rule_lock; /* Lock to protect filter rule list */
6455	u16 i, rid, vsi_handle;
6456	int status = 0;
6457
6458	memset(&lkup_exts, 0, sizeof(lkup_exts));
6459	for (i = 0; i < lkups_cnt; i++) {
6460		u16 count;
6461
6462		if (lkups[i].type >= ICE_PROTOCOL_LAST)
6463			return -EIO;
6464
6465		count = ice_fill_valid_words(&lkups[i], &lkup_exts);
6466		if (!count)
6467			return -EIO;
6468	}
6469
6470	rid = ice_find_recp(hw, &lkup_exts, rinfo, false);
6471	/* If did not find a recipe that match the existing criteria */
6472	if (rid == ICE_MAX_NUM_RECIPES)
6473		return -EINVAL;
6474
6475	rule_lock = &hw->switch_info->recp_list[rid].filt_rule_lock;
6476	list_elem = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
6477	/* the rule is already removed */
6478	if (!list_elem)
6479		return 0;
6480	mutex_lock(rule_lock);
6481	if (list_elem->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST) {
6482		remove_rule = true;
6483	} else if (list_elem->vsi_count > 1) {
6484		remove_rule = false;
6485		vsi_handle = rinfo->sw_act.vsi_handle;
6486		status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
6487	} else {
6488		vsi_handle = rinfo->sw_act.vsi_handle;
6489		status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
6490		if (status) {
6491			mutex_unlock(rule_lock);
6492			return status;
6493		}
6494		if (list_elem->vsi_count == 0)
6495			remove_rule = true;
6496	}
6497	mutex_unlock(rule_lock);
6498	if (remove_rule) {
6499		struct ice_sw_rule_lkup_rx_tx *s_rule;
6500		u16 rule_buf_sz;
6501
6502		rule_buf_sz = ICE_SW_RULE_RX_TX_NO_HDR_SIZE(s_rule);
6503		s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
6504		if (!s_rule)
6505			return -ENOMEM;
6506		s_rule->act = 0;
6507		s_rule->index = cpu_to_le16(list_elem->rule_info.fltr_rule_id);
6508		s_rule->hdr_len = 0;
6509		status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
6510					 rule_buf_sz, 1,
6511					 ice_aqc_opc_remove_sw_rules, NULL);
6512		if (!status || status == -ENOENT) {
6513			struct ice_switch_info *sw = hw->switch_info;
6514			struct ice_sw_recipe *r_list = sw->recp_list;
6515
6516			mutex_lock(rule_lock);
6517			list_del(&list_elem->list_entry);
6518			devm_kfree(ice_hw_to_dev(hw), list_elem->lkups);
6519			devm_kfree(ice_hw_to_dev(hw), list_elem);
6520			mutex_unlock(rule_lock);
6521			if (list_empty(&r_list[rid].filt_rules)) {
6522				r_list[rid].adv_rule = false;
6523
6524				/* All rules for this recipe are now removed */
6525				if (hw->recp_reuse)
6526					ice_release_recipe_res(hw,
6527							       &r_list[rid]);
6528			}
6529		}
6530		kfree(s_rule);
6531	}
6532	return status;
6533}
6534
6535/**
6536 * ice_rem_adv_rule_by_id - removes existing advanced switch rule by ID
6537 * @hw: pointer to the hardware structure
6538 * @remove_entry: data struct which holds rule_id, VSI handle and recipe ID
6539 *
6540 * This function is used to remove 1 rule at a time. The removal is based on
6541 * the remove_entry parameter. This function will remove rule for a given
6542 * vsi_handle with a given rule_id which is passed as parameter in remove_entry
6543 */
6544int
6545ice_rem_adv_rule_by_id(struct ice_hw *hw,
6546		       struct ice_rule_query_data *remove_entry)
6547{
6548	struct ice_adv_fltr_mgmt_list_entry *list_itr;
6549	struct list_head *list_head;
6550	struct ice_adv_rule_info rinfo;
6551	struct ice_switch_info *sw;
6552
6553	sw = hw->switch_info;
6554	if (!sw->recp_list[remove_entry->rid].recp_created)
6555		return -EINVAL;
6556	list_head = &sw->recp_list[remove_entry->rid].filt_rules;
6557	list_for_each_entry(list_itr, list_head, list_entry) {
6558		if (list_itr->rule_info.fltr_rule_id ==
6559		    remove_entry->rule_id) {
6560			rinfo = list_itr->rule_info;
6561			rinfo.sw_act.vsi_handle = remove_entry->vsi_handle;
6562			return ice_rem_adv_rule(hw, list_itr->lkups,
6563						list_itr->lkups_cnt, &rinfo);
6564		}
6565	}
6566	/* either list is empty or unable to find rule */
6567	return -ENOENT;
6568}
6569
6570/**
6571 * ice_replay_vsi_adv_rule - Replay advanced rule for requested VSI
6572 * @hw: pointer to the hardware structure
6573 * @vsi_handle: driver VSI handle
6574 * @list_head: list for which filters need to be replayed
6575 *
6576 * Replay the advanced rule for the given VSI.
6577 */
6578static int
6579ice_replay_vsi_adv_rule(struct ice_hw *hw, u16 vsi_handle,
6580			struct list_head *list_head)
6581{
6582	struct ice_rule_query_data added_entry = { 0 };
6583	struct ice_adv_fltr_mgmt_list_entry *adv_fltr;
6584	int status = 0;
6585
6586	if (list_empty(list_head))
6587		return status;
6588	list_for_each_entry(adv_fltr, list_head, list_entry) {
6589		struct ice_adv_rule_info *rinfo = &adv_fltr->rule_info;
6590		u16 lk_cnt = adv_fltr->lkups_cnt;
6591
6592		if (vsi_handle != rinfo->sw_act.vsi_handle)
6593			continue;
6594		status = ice_add_adv_rule(hw, adv_fltr->lkups, lk_cnt, rinfo,
6595					  &added_entry);
6596		if (status)
6597			break;
6598	}
6599	return status;
6600}
6601
6602/**
6603 * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
6604 * @hw: pointer to the hardware structure
6605 * @vsi_handle: driver VSI handle
6606 *
6607 * Replays filters for requested VSI via vsi_handle.
6608 */
6609int ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
6610{
6611	struct ice_switch_info *sw = hw->switch_info;
6612	int status;
6613	u8 i;
6614
6615	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6616		struct list_head *head;
6617
6618		head = &sw->recp_list[i].filt_replay_rules;
6619		if (!sw->recp_list[i].adv_rule)
6620			status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
6621		else
6622			status = ice_replay_vsi_adv_rule(hw, vsi_handle, head);
6623		if (status)
6624			return status;
6625	}
6626	return status;
6627}
6628
6629/**
6630 * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
6631 * @hw: pointer to the HW struct
6632 *
6633 * Deletes the filter replay rules.
6634 */
6635void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
6636{
6637	struct ice_switch_info *sw = hw->switch_info;
6638	u8 i;
6639
6640	if (!sw)
6641		return;
6642
6643	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6644		if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
6645			struct list_head *l_head;
6646
6647			l_head = &sw->recp_list[i].filt_replay_rules;
6648			if (!sw->recp_list[i].adv_rule)
6649				ice_rem_sw_rule_info(hw, l_head);
6650			else
6651				ice_rem_adv_rule_info(hw, l_head);
6652		}
6653	}
6654}
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2018, Intel Corporation. */
   3
   4#include "ice_lib.h"
   5#include "ice_switch.h"
 
   6
   7#define ICE_ETH_DA_OFFSET		0
   8#define ICE_ETH_ETHTYPE_OFFSET		12
   9#define ICE_ETH_VLAN_TCI_OFFSET		14
  10#define ICE_MAX_VLAN_ID			0xFFF
  11#define ICE_IPV6_ETHER_ID		0x86DD
  12
  13/* Dummy ethernet header needed in the ice_aqc_sw_rules_elem
  14 * struct to configure any switch filter rules.
  15 * {DA (6 bytes), SA(6 bytes),
  16 * Ether type (2 bytes for header without VLAN tag) OR
  17 * VLAN tag (4 bytes for header with VLAN tag) }
  18 *
  19 * Word on Hardcoded values
  20 * byte 0 = 0x2: to identify it as locally administered DA MAC
  21 * byte 6 = 0x2: to identify it as locally administered SA MAC
  22 * byte 12 = 0x81 & byte 13 = 0x00:
  23 *      In case of VLAN filter first two bytes defines ether type (0x8100)
  24 *      and remaining two bytes are placeholder for programming a given VLAN ID
  25 *      In case of Ether type filter it is treated as header without VLAN tag
  26 *      and byte 12 and 13 is used to program a given Ether type instead
  27 */
  28static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
  29							0x2, 0, 0, 0, 0, 0,
  30							0x81, 0, 0, 0};
  31
  32enum {
  33	ICE_PKT_OUTER_IPV6	= BIT(0),
  34	ICE_PKT_TUN_GTPC	= BIT(1),
  35	ICE_PKT_TUN_GTPU	= BIT(2),
  36	ICE_PKT_TUN_NVGRE	= BIT(3),
  37	ICE_PKT_TUN_UDP		= BIT(4),
  38	ICE_PKT_INNER_IPV6	= BIT(5),
  39	ICE_PKT_INNER_TCP	= BIT(6),
  40	ICE_PKT_INNER_UDP	= BIT(7),
  41	ICE_PKT_GTP_NOPAY	= BIT(8),
  42	ICE_PKT_KMALLOC		= BIT(9),
  43	ICE_PKT_PPPOE		= BIT(10),
  44	ICE_PKT_L2TPV3		= BIT(11),
 
  45};
  46
  47struct ice_dummy_pkt_offsets {
  48	enum ice_protocol_type type;
  49	u16 offset; /* ICE_PROTOCOL_LAST indicates end of list */
  50};
  51
  52struct ice_dummy_pkt_profile {
  53	const struct ice_dummy_pkt_offsets *offsets;
  54	const u8 *pkt;
  55	u32 match;
  56	u16 pkt_len;
  57	u16 offsets_len;
  58};
  59
  60#define ICE_DECLARE_PKT_OFFSETS(type)					\
  61	static const struct ice_dummy_pkt_offsets			\
  62	ice_dummy_##type##_packet_offsets[]
  63
  64#define ICE_DECLARE_PKT_TEMPLATE(type)					\
  65	static const u8 ice_dummy_##type##_packet[]
  66
  67#define ICE_PKT_PROFILE(type, m) {					\
  68	.match		= (m),						\
  69	.pkt		= ice_dummy_##type##_packet,			\
  70	.pkt_len	= sizeof(ice_dummy_##type##_packet),		\
  71	.offsets	= ice_dummy_##type##_packet_offsets,		\
  72	.offsets_len	= sizeof(ice_dummy_##type##_packet_offsets),	\
  73}
  74
  75ICE_DECLARE_PKT_OFFSETS(vlan) = {
  76	{ ICE_VLAN_OFOS,        12 },
  77};
  78
  79ICE_DECLARE_PKT_TEMPLATE(vlan) = {
  80	0x81, 0x00, 0x00, 0x00, /* ICE_VLAN_OFOS 12 */
  81};
  82
  83ICE_DECLARE_PKT_OFFSETS(qinq) = {
  84	{ ICE_VLAN_EX,          12 },
  85	{ ICE_VLAN_IN,          16 },
  86};
  87
  88ICE_DECLARE_PKT_TEMPLATE(qinq) = {
  89	0x91, 0x00, 0x00, 0x00, /* ICE_VLAN_EX 12 */
  90	0x81, 0x00, 0x00, 0x00, /* ICE_VLAN_IN 16 */
  91};
  92
  93ICE_DECLARE_PKT_OFFSETS(gre_tcp) = {
  94	{ ICE_MAC_OFOS,		0 },
  95	{ ICE_ETYPE_OL,		12 },
  96	{ ICE_IPV4_OFOS,	14 },
  97	{ ICE_NVGRE,		34 },
  98	{ ICE_MAC_IL,		42 },
  99	{ ICE_ETYPE_IL,		54 },
 100	{ ICE_IPV4_IL,		56 },
 101	{ ICE_TCP_IL,		76 },
 102	{ ICE_PROTOCOL_LAST,	0 },
 103};
 104
 105ICE_DECLARE_PKT_TEMPLATE(gre_tcp) = {
 106	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_OFOS 0 */
 107	0x00, 0x00, 0x00, 0x00,
 108	0x00, 0x00, 0x00, 0x00,
 109
 110	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 111
 112	0x45, 0x00, 0x00, 0x3E,	/* ICE_IPV4_OFOS 14 */
 113	0x00, 0x00, 0x00, 0x00,
 114	0x00, 0x2F, 0x00, 0x00,
 115	0x00, 0x00, 0x00, 0x00,
 116	0x00, 0x00, 0x00, 0x00,
 117
 118	0x80, 0x00, 0x65, 0x58,	/* ICE_NVGRE 34 */
 119	0x00, 0x00, 0x00, 0x00,
 120
 121	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_IL 42 */
 122	0x00, 0x00, 0x00, 0x00,
 123	0x00, 0x00, 0x00, 0x00,
 124
 125	0x08, 0x00,		/* ICE_ETYPE_IL 54 */
 126
 127	0x45, 0x00, 0x00, 0x14,	/* ICE_IPV4_IL 56 */
 128	0x00, 0x00, 0x00, 0x00,
 129	0x00, 0x06, 0x00, 0x00,
 130	0x00, 0x00, 0x00, 0x00,
 131	0x00, 0x00, 0x00, 0x00,
 132
 133	0x00, 0x00, 0x00, 0x00,	/* ICE_TCP_IL 76 */
 134	0x00, 0x00, 0x00, 0x00,
 135	0x00, 0x00, 0x00, 0x00,
 136	0x50, 0x02, 0x20, 0x00,
 137	0x00, 0x00, 0x00, 0x00
 138};
 139
 140ICE_DECLARE_PKT_OFFSETS(gre_udp) = {
 141	{ ICE_MAC_OFOS,		0 },
 142	{ ICE_ETYPE_OL,		12 },
 143	{ ICE_IPV4_OFOS,	14 },
 144	{ ICE_NVGRE,		34 },
 145	{ ICE_MAC_IL,		42 },
 146	{ ICE_ETYPE_IL,		54 },
 147	{ ICE_IPV4_IL,		56 },
 148	{ ICE_UDP_ILOS,		76 },
 149	{ ICE_PROTOCOL_LAST,	0 },
 150};
 151
 152ICE_DECLARE_PKT_TEMPLATE(gre_udp) = {
 153	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_OFOS 0 */
 154	0x00, 0x00, 0x00, 0x00,
 155	0x00, 0x00, 0x00, 0x00,
 156
 157	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 158
 159	0x45, 0x00, 0x00, 0x3E,	/* ICE_IPV4_OFOS 14 */
 160	0x00, 0x00, 0x00, 0x00,
 161	0x00, 0x2F, 0x00, 0x00,
 162	0x00, 0x00, 0x00, 0x00,
 163	0x00, 0x00, 0x00, 0x00,
 164
 165	0x80, 0x00, 0x65, 0x58,	/* ICE_NVGRE 34 */
 166	0x00, 0x00, 0x00, 0x00,
 167
 168	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_IL 42 */
 169	0x00, 0x00, 0x00, 0x00,
 170	0x00, 0x00, 0x00, 0x00,
 171
 172	0x08, 0x00,		/* ICE_ETYPE_IL 54 */
 173
 174	0x45, 0x00, 0x00, 0x14,	/* ICE_IPV4_IL 56 */
 175	0x00, 0x00, 0x00, 0x00,
 176	0x00, 0x11, 0x00, 0x00,
 177	0x00, 0x00, 0x00, 0x00,
 178	0x00, 0x00, 0x00, 0x00,
 179
 180	0x00, 0x00, 0x00, 0x00,	/* ICE_UDP_ILOS 76 */
 181	0x00, 0x08, 0x00, 0x00,
 182};
 183
 184ICE_DECLARE_PKT_OFFSETS(udp_tun_tcp) = {
 185	{ ICE_MAC_OFOS,		0 },
 186	{ ICE_ETYPE_OL,		12 },
 187	{ ICE_IPV4_OFOS,	14 },
 188	{ ICE_UDP_OF,		34 },
 189	{ ICE_VXLAN,		42 },
 190	{ ICE_GENEVE,		42 },
 191	{ ICE_VXLAN_GPE,	42 },
 192	{ ICE_MAC_IL,		50 },
 193	{ ICE_ETYPE_IL,		62 },
 194	{ ICE_IPV4_IL,		64 },
 195	{ ICE_TCP_IL,		84 },
 196	{ ICE_PROTOCOL_LAST,	0 },
 197};
 198
 199ICE_DECLARE_PKT_TEMPLATE(udp_tun_tcp) = {
 200	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
 201	0x00, 0x00, 0x00, 0x00,
 202	0x00, 0x00, 0x00, 0x00,
 203
 204	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 205
 206	0x45, 0x00, 0x00, 0x5a, /* ICE_IPV4_OFOS 14 */
 207	0x00, 0x01, 0x00, 0x00,
 208	0x40, 0x11, 0x00, 0x00,
 209	0x00, 0x00, 0x00, 0x00,
 210	0x00, 0x00, 0x00, 0x00,
 211
 212	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
 213	0x00, 0x46, 0x00, 0x00,
 214
 215	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
 216	0x00, 0x00, 0x00, 0x00,
 217
 218	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
 219	0x00, 0x00, 0x00, 0x00,
 220	0x00, 0x00, 0x00, 0x00,
 221
 222	0x08, 0x00,		/* ICE_ETYPE_IL 62 */
 223
 224	0x45, 0x00, 0x00, 0x28, /* ICE_IPV4_IL 64 */
 225	0x00, 0x01, 0x00, 0x00,
 226	0x40, 0x06, 0x00, 0x00,
 227	0x00, 0x00, 0x00, 0x00,
 228	0x00, 0x00, 0x00, 0x00,
 229
 230	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 84 */
 231	0x00, 0x00, 0x00, 0x00,
 232	0x00, 0x00, 0x00, 0x00,
 233	0x50, 0x02, 0x20, 0x00,
 234	0x00, 0x00, 0x00, 0x00
 235};
 236
 237ICE_DECLARE_PKT_OFFSETS(udp_tun_udp) = {
 238	{ ICE_MAC_OFOS,		0 },
 239	{ ICE_ETYPE_OL,		12 },
 240	{ ICE_IPV4_OFOS,	14 },
 241	{ ICE_UDP_OF,		34 },
 242	{ ICE_VXLAN,		42 },
 243	{ ICE_GENEVE,		42 },
 244	{ ICE_VXLAN_GPE,	42 },
 245	{ ICE_MAC_IL,		50 },
 246	{ ICE_ETYPE_IL,		62 },
 247	{ ICE_IPV4_IL,		64 },
 248	{ ICE_UDP_ILOS,		84 },
 249	{ ICE_PROTOCOL_LAST,	0 },
 250};
 251
 252ICE_DECLARE_PKT_TEMPLATE(udp_tun_udp) = {
 253	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
 254	0x00, 0x00, 0x00, 0x00,
 255	0x00, 0x00, 0x00, 0x00,
 256
 257	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 258
 259	0x45, 0x00, 0x00, 0x4e, /* ICE_IPV4_OFOS 14 */
 260	0x00, 0x01, 0x00, 0x00,
 261	0x00, 0x11, 0x00, 0x00,
 262	0x00, 0x00, 0x00, 0x00,
 263	0x00, 0x00, 0x00, 0x00,
 264
 265	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
 266	0x00, 0x3a, 0x00, 0x00,
 267
 268	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
 269	0x00, 0x00, 0x00, 0x00,
 270
 271	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
 272	0x00, 0x00, 0x00, 0x00,
 273	0x00, 0x00, 0x00, 0x00,
 274
 275	0x08, 0x00,		/* ICE_ETYPE_IL 62 */
 276
 277	0x45, 0x00, 0x00, 0x1c, /* ICE_IPV4_IL 64 */
 278	0x00, 0x01, 0x00, 0x00,
 279	0x00, 0x11, 0x00, 0x00,
 280	0x00, 0x00, 0x00, 0x00,
 281	0x00, 0x00, 0x00, 0x00,
 282
 283	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 84 */
 284	0x00, 0x08, 0x00, 0x00,
 285};
 286
 287ICE_DECLARE_PKT_OFFSETS(gre_ipv6_tcp) = {
 288	{ ICE_MAC_OFOS,		0 },
 289	{ ICE_ETYPE_OL,		12 },
 290	{ ICE_IPV4_OFOS,	14 },
 291	{ ICE_NVGRE,		34 },
 292	{ ICE_MAC_IL,		42 },
 293	{ ICE_ETYPE_IL,		54 },
 294	{ ICE_IPV6_IL,		56 },
 295	{ ICE_TCP_IL,		96 },
 296	{ ICE_PROTOCOL_LAST,	0 },
 297};
 298
 299ICE_DECLARE_PKT_TEMPLATE(gre_ipv6_tcp) = {
 300	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 301	0x00, 0x00, 0x00, 0x00,
 302	0x00, 0x00, 0x00, 0x00,
 303
 304	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 305
 306	0x45, 0x00, 0x00, 0x66, /* ICE_IPV4_OFOS 14 */
 307	0x00, 0x00, 0x00, 0x00,
 308	0x00, 0x2F, 0x00, 0x00,
 309	0x00, 0x00, 0x00, 0x00,
 310	0x00, 0x00, 0x00, 0x00,
 311
 312	0x80, 0x00, 0x65, 0x58, /* ICE_NVGRE 34 */
 313	0x00, 0x00, 0x00, 0x00,
 314
 315	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 42 */
 316	0x00, 0x00, 0x00, 0x00,
 317	0x00, 0x00, 0x00, 0x00,
 318
 319	0x86, 0xdd,		/* ICE_ETYPE_IL 54 */
 320
 321	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 56 */
 322	0x00, 0x08, 0x06, 0x40,
 323	0x00, 0x00, 0x00, 0x00,
 324	0x00, 0x00, 0x00, 0x00,
 325	0x00, 0x00, 0x00, 0x00,
 326	0x00, 0x00, 0x00, 0x00,
 327	0x00, 0x00, 0x00, 0x00,
 328	0x00, 0x00, 0x00, 0x00,
 329	0x00, 0x00, 0x00, 0x00,
 330	0x00, 0x00, 0x00, 0x00,
 331
 332	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 96 */
 333	0x00, 0x00, 0x00, 0x00,
 334	0x00, 0x00, 0x00, 0x00,
 335	0x50, 0x02, 0x20, 0x00,
 336	0x00, 0x00, 0x00, 0x00
 337};
 338
 339ICE_DECLARE_PKT_OFFSETS(gre_ipv6_udp) = {
 340	{ ICE_MAC_OFOS,		0 },
 341	{ ICE_ETYPE_OL,		12 },
 342	{ ICE_IPV4_OFOS,	14 },
 343	{ ICE_NVGRE,		34 },
 344	{ ICE_MAC_IL,		42 },
 345	{ ICE_ETYPE_IL,		54 },
 346	{ ICE_IPV6_IL,		56 },
 347	{ ICE_UDP_ILOS,		96 },
 348	{ ICE_PROTOCOL_LAST,	0 },
 349};
 350
 351ICE_DECLARE_PKT_TEMPLATE(gre_ipv6_udp) = {
 352	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 353	0x00, 0x00, 0x00, 0x00,
 354	0x00, 0x00, 0x00, 0x00,
 355
 356	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 357
 358	0x45, 0x00, 0x00, 0x5a, /* ICE_IPV4_OFOS 14 */
 359	0x00, 0x00, 0x00, 0x00,
 360	0x00, 0x2F, 0x00, 0x00,
 361	0x00, 0x00, 0x00, 0x00,
 362	0x00, 0x00, 0x00, 0x00,
 363
 364	0x80, 0x00, 0x65, 0x58, /* ICE_NVGRE 34 */
 365	0x00, 0x00, 0x00, 0x00,
 366
 367	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 42 */
 368	0x00, 0x00, 0x00, 0x00,
 369	0x00, 0x00, 0x00, 0x00,
 370
 371	0x86, 0xdd,		/* ICE_ETYPE_IL 54 */
 372
 373	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 56 */
 374	0x00, 0x08, 0x11, 0x40,
 375	0x00, 0x00, 0x00, 0x00,
 376	0x00, 0x00, 0x00, 0x00,
 377	0x00, 0x00, 0x00, 0x00,
 378	0x00, 0x00, 0x00, 0x00,
 379	0x00, 0x00, 0x00, 0x00,
 380	0x00, 0x00, 0x00, 0x00,
 381	0x00, 0x00, 0x00, 0x00,
 382	0x00, 0x00, 0x00, 0x00,
 383
 384	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 96 */
 385	0x00, 0x08, 0x00, 0x00,
 386};
 387
 388ICE_DECLARE_PKT_OFFSETS(udp_tun_ipv6_tcp) = {
 389	{ ICE_MAC_OFOS,		0 },
 390	{ ICE_ETYPE_OL,		12 },
 391	{ ICE_IPV4_OFOS,	14 },
 392	{ ICE_UDP_OF,		34 },
 393	{ ICE_VXLAN,		42 },
 394	{ ICE_GENEVE,		42 },
 395	{ ICE_VXLAN_GPE,	42 },
 396	{ ICE_MAC_IL,		50 },
 397	{ ICE_ETYPE_IL,		62 },
 398	{ ICE_IPV6_IL,		64 },
 399	{ ICE_TCP_IL,		104 },
 400	{ ICE_PROTOCOL_LAST,	0 },
 401};
 402
 403ICE_DECLARE_PKT_TEMPLATE(udp_tun_ipv6_tcp) = {
 404	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
 405	0x00, 0x00, 0x00, 0x00,
 406	0x00, 0x00, 0x00, 0x00,
 407
 408	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 409
 410	0x45, 0x00, 0x00, 0x6e, /* ICE_IPV4_OFOS 14 */
 411	0x00, 0x01, 0x00, 0x00,
 412	0x40, 0x11, 0x00, 0x00,
 413	0x00, 0x00, 0x00, 0x00,
 414	0x00, 0x00, 0x00, 0x00,
 415
 416	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
 417	0x00, 0x5a, 0x00, 0x00,
 418
 419	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
 420	0x00, 0x00, 0x00, 0x00,
 421
 422	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
 423	0x00, 0x00, 0x00, 0x00,
 424	0x00, 0x00, 0x00, 0x00,
 425
 426	0x86, 0xdd,		/* ICE_ETYPE_IL 62 */
 427
 428	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 64 */
 429	0x00, 0x08, 0x06, 0x40,
 430	0x00, 0x00, 0x00, 0x00,
 431	0x00, 0x00, 0x00, 0x00,
 432	0x00, 0x00, 0x00, 0x00,
 433	0x00, 0x00, 0x00, 0x00,
 434	0x00, 0x00, 0x00, 0x00,
 435	0x00, 0x00, 0x00, 0x00,
 436	0x00, 0x00, 0x00, 0x00,
 437	0x00, 0x00, 0x00, 0x00,
 438
 439	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 104 */
 440	0x00, 0x00, 0x00, 0x00,
 441	0x00, 0x00, 0x00, 0x00,
 442	0x50, 0x02, 0x20, 0x00,
 443	0x00, 0x00, 0x00, 0x00
 444};
 445
 446ICE_DECLARE_PKT_OFFSETS(udp_tun_ipv6_udp) = {
 447	{ ICE_MAC_OFOS,		0 },
 448	{ ICE_ETYPE_OL,		12 },
 449	{ ICE_IPV4_OFOS,	14 },
 450	{ ICE_UDP_OF,		34 },
 451	{ ICE_VXLAN,		42 },
 452	{ ICE_GENEVE,		42 },
 453	{ ICE_VXLAN_GPE,	42 },
 454	{ ICE_MAC_IL,		50 },
 455	{ ICE_ETYPE_IL,		62 },
 456	{ ICE_IPV6_IL,		64 },
 457	{ ICE_UDP_ILOS,		104 },
 458	{ ICE_PROTOCOL_LAST,	0 },
 459};
 460
 461ICE_DECLARE_PKT_TEMPLATE(udp_tun_ipv6_udp) = {
 462	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
 463	0x00, 0x00, 0x00, 0x00,
 464	0x00, 0x00, 0x00, 0x00,
 465
 466	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 467
 468	0x45, 0x00, 0x00, 0x62, /* ICE_IPV4_OFOS 14 */
 469	0x00, 0x01, 0x00, 0x00,
 470	0x00, 0x11, 0x00, 0x00,
 471	0x00, 0x00, 0x00, 0x00,
 472	0x00, 0x00, 0x00, 0x00,
 473
 474	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
 475	0x00, 0x4e, 0x00, 0x00,
 476
 477	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
 478	0x00, 0x00, 0x00, 0x00,
 479
 480	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
 481	0x00, 0x00, 0x00, 0x00,
 482	0x00, 0x00, 0x00, 0x00,
 483
 484	0x86, 0xdd,		/* ICE_ETYPE_IL 62 */
 485
 486	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 64 */
 487	0x00, 0x08, 0x11, 0x40,
 488	0x00, 0x00, 0x00, 0x00,
 489	0x00, 0x00, 0x00, 0x00,
 490	0x00, 0x00, 0x00, 0x00,
 491	0x00, 0x00, 0x00, 0x00,
 492	0x00, 0x00, 0x00, 0x00,
 493	0x00, 0x00, 0x00, 0x00,
 494	0x00, 0x00, 0x00, 0x00,
 495	0x00, 0x00, 0x00, 0x00,
 496
 497	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 104 */
 498	0x00, 0x08, 0x00, 0x00,
 499};
 500
 501/* offset info for MAC + IPv4 + UDP dummy packet */
 502ICE_DECLARE_PKT_OFFSETS(udp) = {
 503	{ ICE_MAC_OFOS,		0 },
 504	{ ICE_ETYPE_OL,		12 },
 505	{ ICE_IPV4_OFOS,	14 },
 506	{ ICE_UDP_ILOS,		34 },
 507	{ ICE_PROTOCOL_LAST,	0 },
 508};
 509
 510/* Dummy packet for MAC + IPv4 + UDP */
 511ICE_DECLARE_PKT_TEMPLATE(udp) = {
 512	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 513	0x00, 0x00, 0x00, 0x00,
 514	0x00, 0x00, 0x00, 0x00,
 515
 516	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 517
 518	0x45, 0x00, 0x00, 0x1c, /* ICE_IPV4_OFOS 14 */
 519	0x00, 0x01, 0x00, 0x00,
 520	0x00, 0x11, 0x00, 0x00,
 521	0x00, 0x00, 0x00, 0x00,
 522	0x00, 0x00, 0x00, 0x00,
 523
 524	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 34 */
 525	0x00, 0x08, 0x00, 0x00,
 526
 527	0x00, 0x00,	/* 2 bytes for 4 byte alignment */
 528};
 529
 530/* offset info for MAC + IPv4 + TCP dummy packet */
 531ICE_DECLARE_PKT_OFFSETS(tcp) = {
 532	{ ICE_MAC_OFOS,		0 },
 533	{ ICE_ETYPE_OL,		12 },
 534	{ ICE_IPV4_OFOS,	14 },
 535	{ ICE_TCP_IL,		34 },
 536	{ ICE_PROTOCOL_LAST,	0 },
 537};
 538
 539/* Dummy packet for MAC + IPv4 + TCP */
 540ICE_DECLARE_PKT_TEMPLATE(tcp) = {
 541	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 542	0x00, 0x00, 0x00, 0x00,
 543	0x00, 0x00, 0x00, 0x00,
 544
 545	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
 546
 547	0x45, 0x00, 0x00, 0x28, /* ICE_IPV4_OFOS 14 */
 548	0x00, 0x01, 0x00, 0x00,
 549	0x00, 0x06, 0x00, 0x00,
 550	0x00, 0x00, 0x00, 0x00,
 551	0x00, 0x00, 0x00, 0x00,
 552
 553	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 34 */
 554	0x00, 0x00, 0x00, 0x00,
 555	0x00, 0x00, 0x00, 0x00,
 556	0x50, 0x00, 0x00, 0x00,
 557	0x00, 0x00, 0x00, 0x00,
 558
 559	0x00, 0x00,	/* 2 bytes for 4 byte alignment */
 560};
 561
 562ICE_DECLARE_PKT_OFFSETS(tcp_ipv6) = {
 563	{ ICE_MAC_OFOS,		0 },
 564	{ ICE_ETYPE_OL,		12 },
 565	{ ICE_IPV6_OFOS,	14 },
 566	{ ICE_TCP_IL,		54 },
 567	{ ICE_PROTOCOL_LAST,	0 },
 568};
 569
 570ICE_DECLARE_PKT_TEMPLATE(tcp_ipv6) = {
 571	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 572	0x00, 0x00, 0x00, 0x00,
 573	0x00, 0x00, 0x00, 0x00,
 574
 575	0x86, 0xDD,		/* ICE_ETYPE_OL 12 */
 576
 577	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 40 */
 578	0x00, 0x14, 0x06, 0x00, /* Next header is TCP */
 579	0x00, 0x00, 0x00, 0x00,
 580	0x00, 0x00, 0x00, 0x00,
 581	0x00, 0x00, 0x00, 0x00,
 582	0x00, 0x00, 0x00, 0x00,
 583	0x00, 0x00, 0x00, 0x00,
 584	0x00, 0x00, 0x00, 0x00,
 585	0x00, 0x00, 0x00, 0x00,
 586	0x00, 0x00, 0x00, 0x00,
 587
 588	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 54 */
 589	0x00, 0x00, 0x00, 0x00,
 590	0x00, 0x00, 0x00, 0x00,
 591	0x50, 0x00, 0x00, 0x00,
 592	0x00, 0x00, 0x00, 0x00,
 593
 594	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 595};
 596
 597/* IPv6 + UDP */
 598ICE_DECLARE_PKT_OFFSETS(udp_ipv6) = {
 599	{ ICE_MAC_OFOS,		0 },
 600	{ ICE_ETYPE_OL,		12 },
 601	{ ICE_IPV6_OFOS,	14 },
 602	{ ICE_UDP_ILOS,		54 },
 603	{ ICE_PROTOCOL_LAST,	0 },
 604};
 605
 606/* IPv6 + UDP dummy packet */
 607ICE_DECLARE_PKT_TEMPLATE(udp_ipv6) = {
 608	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
 609	0x00, 0x00, 0x00, 0x00,
 610	0x00, 0x00, 0x00, 0x00,
 611
 612	0x86, 0xDD,		/* ICE_ETYPE_OL 12 */
 613
 614	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 40 */
 615	0x00, 0x10, 0x11, 0x00, /* Next header UDP */
 616	0x00, 0x00, 0x00, 0x00,
 617	0x00, 0x00, 0x00, 0x00,
 618	0x00, 0x00, 0x00, 0x00,
 619	0x00, 0x00, 0x00, 0x00,
 620	0x00, 0x00, 0x00, 0x00,
 621	0x00, 0x00, 0x00, 0x00,
 622	0x00, 0x00, 0x00, 0x00,
 623	0x00, 0x00, 0x00, 0x00,
 624
 625	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 54 */
 626	0x00, 0x10, 0x00, 0x00,
 627
 628	0x00, 0x00, 0x00, 0x00, /* needed for ESP packets */
 629	0x00, 0x00, 0x00, 0x00,
 630
 631	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 632};
 633
 634/* Outer IPv4 + Outer UDP + GTP + Inner IPv4 + Inner TCP */
 635ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv4_tcp) = {
 636	{ ICE_MAC_OFOS,		0 },
 637	{ ICE_IPV4_OFOS,	14 },
 638	{ ICE_UDP_OF,		34 },
 639	{ ICE_GTP,		42 },
 640	{ ICE_IPV4_IL,		62 },
 641	{ ICE_TCP_IL,		82 },
 642	{ ICE_PROTOCOL_LAST,	0 },
 643};
 644
 645ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv4_tcp) = {
 646	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 647	0x00, 0x00, 0x00, 0x00,
 648	0x00, 0x00, 0x00, 0x00,
 649	0x08, 0x00,
 650
 651	0x45, 0x00, 0x00, 0x58, /* IP 14 */
 652	0x00, 0x00, 0x00, 0x00,
 653	0x00, 0x11, 0x00, 0x00,
 654	0x00, 0x00, 0x00, 0x00,
 655	0x00, 0x00, 0x00, 0x00,
 656
 657	0x00, 0x00, 0x08, 0x68, /* UDP 34 */
 658	0x00, 0x44, 0x00, 0x00,
 659
 660	0x34, 0xff, 0x00, 0x34, /* ICE_GTP Header 42 */
 661	0x00, 0x00, 0x00, 0x00,
 662	0x00, 0x00, 0x00, 0x85,
 663
 664	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 54 */
 665	0x00, 0x00, 0x00, 0x00,
 666
 667	0x45, 0x00, 0x00, 0x28, /* IP 62 */
 668	0x00, 0x00, 0x00, 0x00,
 669	0x00, 0x06, 0x00, 0x00,
 670	0x00, 0x00, 0x00, 0x00,
 671	0x00, 0x00, 0x00, 0x00,
 672
 673	0x00, 0x00, 0x00, 0x00, /* TCP 82 */
 674	0x00, 0x00, 0x00, 0x00,
 675	0x00, 0x00, 0x00, 0x00,
 676	0x50, 0x00, 0x00, 0x00,
 677	0x00, 0x00, 0x00, 0x00,
 678
 679	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 680};
 681
 682/* Outer IPv4 + Outer UDP + GTP + Inner IPv4 + Inner UDP */
 683ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv4_udp) = {
 684	{ ICE_MAC_OFOS,		0 },
 685	{ ICE_IPV4_OFOS,	14 },
 686	{ ICE_UDP_OF,		34 },
 687	{ ICE_GTP,		42 },
 688	{ ICE_IPV4_IL,		62 },
 689	{ ICE_UDP_ILOS,		82 },
 690	{ ICE_PROTOCOL_LAST,	0 },
 691};
 692
 693ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv4_udp) = {
 694	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 695	0x00, 0x00, 0x00, 0x00,
 696	0x00, 0x00, 0x00, 0x00,
 697	0x08, 0x00,
 698
 699	0x45, 0x00, 0x00, 0x4c, /* IP 14 */
 700	0x00, 0x00, 0x00, 0x00,
 701	0x00, 0x11, 0x00, 0x00,
 702	0x00, 0x00, 0x00, 0x00,
 703	0x00, 0x00, 0x00, 0x00,
 704
 705	0x00, 0x00, 0x08, 0x68, /* UDP 34 */
 706	0x00, 0x38, 0x00, 0x00,
 707
 708	0x34, 0xff, 0x00, 0x28, /* ICE_GTP Header 42 */
 709	0x00, 0x00, 0x00, 0x00,
 710	0x00, 0x00, 0x00, 0x85,
 711
 712	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 54 */
 713	0x00, 0x00, 0x00, 0x00,
 714
 715	0x45, 0x00, 0x00, 0x1c, /* IP 62 */
 716	0x00, 0x00, 0x00, 0x00,
 717	0x00, 0x11, 0x00, 0x00,
 718	0x00, 0x00, 0x00, 0x00,
 719	0x00, 0x00, 0x00, 0x00,
 720
 721	0x00, 0x00, 0x00, 0x00, /* UDP 82 */
 722	0x00, 0x08, 0x00, 0x00,
 723
 724	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 725};
 726
 727/* Outer IPv6 + Outer UDP + GTP + Inner IPv4 + Inner TCP */
 728ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv6_tcp) = {
 729	{ ICE_MAC_OFOS,		0 },
 730	{ ICE_IPV4_OFOS,	14 },
 731	{ ICE_UDP_OF,		34 },
 732	{ ICE_GTP,		42 },
 733	{ ICE_IPV6_IL,		62 },
 734	{ ICE_TCP_IL,		102 },
 735	{ ICE_PROTOCOL_LAST,	0 },
 736};
 737
 738ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv6_tcp) = {
 739	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 740	0x00, 0x00, 0x00, 0x00,
 741	0x00, 0x00, 0x00, 0x00,
 742	0x08, 0x00,
 743
 744	0x45, 0x00, 0x00, 0x6c, /* IP 14 */
 745	0x00, 0x00, 0x00, 0x00,
 746	0x00, 0x11, 0x00, 0x00,
 747	0x00, 0x00, 0x00, 0x00,
 748	0x00, 0x00, 0x00, 0x00,
 749
 750	0x00, 0x00, 0x08, 0x68, /* UDP 34 */
 751	0x00, 0x58, 0x00, 0x00,
 752
 753	0x34, 0xff, 0x00, 0x48, /* ICE_GTP Header 42 */
 754	0x00, 0x00, 0x00, 0x00,
 755	0x00, 0x00, 0x00, 0x85,
 756
 757	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 54 */
 758	0x00, 0x00, 0x00, 0x00,
 759
 760	0x60, 0x00, 0x00, 0x00, /* IPv6 62 */
 761	0x00, 0x14, 0x06, 0x00,
 762	0x00, 0x00, 0x00, 0x00,
 763	0x00, 0x00, 0x00, 0x00,
 764	0x00, 0x00, 0x00, 0x00,
 765	0x00, 0x00, 0x00, 0x00,
 766	0x00, 0x00, 0x00, 0x00,
 767	0x00, 0x00, 0x00, 0x00,
 768	0x00, 0x00, 0x00, 0x00,
 769	0x00, 0x00, 0x00, 0x00,
 770
 771	0x00, 0x00, 0x00, 0x00, /* TCP 102 */
 772	0x00, 0x00, 0x00, 0x00,
 773	0x00, 0x00, 0x00, 0x00,
 774	0x50, 0x00, 0x00, 0x00,
 775	0x00, 0x00, 0x00, 0x00,
 776
 777	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 778};
 779
 780ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv6_udp) = {
 781	{ ICE_MAC_OFOS,		0 },
 782	{ ICE_IPV4_OFOS,	14 },
 783	{ ICE_UDP_OF,		34 },
 784	{ ICE_GTP,		42 },
 785	{ ICE_IPV6_IL,		62 },
 786	{ ICE_UDP_ILOS,		102 },
 787	{ ICE_PROTOCOL_LAST,	0 },
 788};
 789
 790ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv6_udp) = {
 791	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 792	0x00, 0x00, 0x00, 0x00,
 793	0x00, 0x00, 0x00, 0x00,
 794	0x08, 0x00,
 795
 796	0x45, 0x00, 0x00, 0x60, /* IP 14 */
 797	0x00, 0x00, 0x00, 0x00,
 798	0x00, 0x11, 0x00, 0x00,
 799	0x00, 0x00, 0x00, 0x00,
 800	0x00, 0x00, 0x00, 0x00,
 801
 802	0x00, 0x00, 0x08, 0x68, /* UDP 34 */
 803	0x00, 0x4c, 0x00, 0x00,
 804
 805	0x34, 0xff, 0x00, 0x3c, /* ICE_GTP Header 42 */
 806	0x00, 0x00, 0x00, 0x00,
 807	0x00, 0x00, 0x00, 0x85,
 808
 809	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 54 */
 810	0x00, 0x00, 0x00, 0x00,
 811
 812	0x60, 0x00, 0x00, 0x00, /* IPv6 62 */
 813	0x00, 0x08, 0x11, 0x00,
 814	0x00, 0x00, 0x00, 0x00,
 815	0x00, 0x00, 0x00, 0x00,
 816	0x00, 0x00, 0x00, 0x00,
 817	0x00, 0x00, 0x00, 0x00,
 818	0x00, 0x00, 0x00, 0x00,
 819	0x00, 0x00, 0x00, 0x00,
 820	0x00, 0x00, 0x00, 0x00,
 821	0x00, 0x00, 0x00, 0x00,
 822
 823	0x00, 0x00, 0x00, 0x00, /* UDP 102 */
 824	0x00, 0x08, 0x00, 0x00,
 825
 826	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 827};
 828
 829ICE_DECLARE_PKT_OFFSETS(ipv6_gtpu_ipv4_tcp) = {
 830	{ ICE_MAC_OFOS,		0 },
 831	{ ICE_IPV6_OFOS,	14 },
 832	{ ICE_UDP_OF,		54 },
 833	{ ICE_GTP,		62 },
 834	{ ICE_IPV4_IL,		82 },
 835	{ ICE_TCP_IL,		102 },
 836	{ ICE_PROTOCOL_LAST,	0 },
 837};
 838
 839ICE_DECLARE_PKT_TEMPLATE(ipv6_gtpu_ipv4_tcp) = {
 840	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 841	0x00, 0x00, 0x00, 0x00,
 842	0x00, 0x00, 0x00, 0x00,
 843	0x86, 0xdd,
 844
 845	0x60, 0x00, 0x00, 0x00, /* IPv6 14 */
 846	0x00, 0x44, 0x11, 0x00,
 847	0x00, 0x00, 0x00, 0x00,
 848	0x00, 0x00, 0x00, 0x00,
 849	0x00, 0x00, 0x00, 0x00,
 850	0x00, 0x00, 0x00, 0x00,
 851	0x00, 0x00, 0x00, 0x00,
 852	0x00, 0x00, 0x00, 0x00,
 853	0x00, 0x00, 0x00, 0x00,
 854	0x00, 0x00, 0x00, 0x00,
 855
 856	0x00, 0x00, 0x08, 0x68, /* UDP 54 */
 857	0x00, 0x44, 0x00, 0x00,
 858
 859	0x34, 0xff, 0x00, 0x34, /* ICE_GTP Header 62 */
 860	0x00, 0x00, 0x00, 0x00,
 861	0x00, 0x00, 0x00, 0x85,
 862
 863	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 74 */
 864	0x00, 0x00, 0x00, 0x00,
 865
 866	0x45, 0x00, 0x00, 0x28, /* IP 82 */
 867	0x00, 0x00, 0x00, 0x00,
 868	0x00, 0x06, 0x00, 0x00,
 869	0x00, 0x00, 0x00, 0x00,
 870	0x00, 0x00, 0x00, 0x00,
 871
 872	0x00, 0x00, 0x00, 0x00, /* TCP 102 */
 873	0x00, 0x00, 0x00, 0x00,
 874	0x00, 0x00, 0x00, 0x00,
 875	0x50, 0x00, 0x00, 0x00,
 876	0x00, 0x00, 0x00, 0x00,
 877
 878	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 879};
 880
 881ICE_DECLARE_PKT_OFFSETS(ipv6_gtpu_ipv4_udp) = {
 882	{ ICE_MAC_OFOS,		0 },
 883	{ ICE_IPV6_OFOS,	14 },
 884	{ ICE_UDP_OF,		54 },
 885	{ ICE_GTP,		62 },
 886	{ ICE_IPV4_IL,		82 },
 887	{ ICE_UDP_ILOS,		102 },
 888	{ ICE_PROTOCOL_LAST,	0 },
 889};
 890
 891ICE_DECLARE_PKT_TEMPLATE(ipv6_gtpu_ipv4_udp) = {
 892	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 893	0x00, 0x00, 0x00, 0x00,
 894	0x00, 0x00, 0x00, 0x00,
 895	0x86, 0xdd,
 896
 897	0x60, 0x00, 0x00, 0x00, /* IPv6 14 */
 898	0x00, 0x38, 0x11, 0x00,
 899	0x00, 0x00, 0x00, 0x00,
 900	0x00, 0x00, 0x00, 0x00,
 901	0x00, 0x00, 0x00, 0x00,
 902	0x00, 0x00, 0x00, 0x00,
 903	0x00, 0x00, 0x00, 0x00,
 904	0x00, 0x00, 0x00, 0x00,
 905	0x00, 0x00, 0x00, 0x00,
 906	0x00, 0x00, 0x00, 0x00,
 907
 908	0x00, 0x00, 0x08, 0x68, /* UDP 54 */
 909	0x00, 0x38, 0x00, 0x00,
 910
 911	0x34, 0xff, 0x00, 0x28, /* ICE_GTP Header 62 */
 912	0x00, 0x00, 0x00, 0x00,
 913	0x00, 0x00, 0x00, 0x85,
 914
 915	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 74 */
 916	0x00, 0x00, 0x00, 0x00,
 917
 918	0x45, 0x00, 0x00, 0x1c, /* IP 82 */
 919	0x00, 0x00, 0x00, 0x00,
 920	0x00, 0x11, 0x00, 0x00,
 921	0x00, 0x00, 0x00, 0x00,
 922	0x00, 0x00, 0x00, 0x00,
 923
 924	0x00, 0x00, 0x00, 0x00, /* UDP 102 */
 925	0x00, 0x08, 0x00, 0x00,
 926
 927	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 928};
 929
 930ICE_DECLARE_PKT_OFFSETS(ipv6_gtpu_ipv6_tcp) = {
 931	{ ICE_MAC_OFOS,		0 },
 932	{ ICE_IPV6_OFOS,	14 },
 933	{ ICE_UDP_OF,		54 },
 934	{ ICE_GTP,		62 },
 935	{ ICE_IPV6_IL,		82 },
 936	{ ICE_TCP_IL,		122 },
 937	{ ICE_PROTOCOL_LAST,	0 },
 938};
 939
 940ICE_DECLARE_PKT_TEMPLATE(ipv6_gtpu_ipv6_tcp) = {
 941	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 942	0x00, 0x00, 0x00, 0x00,
 943	0x00, 0x00, 0x00, 0x00,
 944	0x86, 0xdd,
 945
 946	0x60, 0x00, 0x00, 0x00, /* IPv6 14 */
 947	0x00, 0x58, 0x11, 0x00,
 948	0x00, 0x00, 0x00, 0x00,
 949	0x00, 0x00, 0x00, 0x00,
 950	0x00, 0x00, 0x00, 0x00,
 951	0x00, 0x00, 0x00, 0x00,
 952	0x00, 0x00, 0x00, 0x00,
 953	0x00, 0x00, 0x00, 0x00,
 954	0x00, 0x00, 0x00, 0x00,
 955	0x00, 0x00, 0x00, 0x00,
 956
 957	0x00, 0x00, 0x08, 0x68, /* UDP 54 */
 958	0x00, 0x58, 0x00, 0x00,
 959
 960	0x34, 0xff, 0x00, 0x48, /* ICE_GTP Header 62 */
 961	0x00, 0x00, 0x00, 0x00,
 962	0x00, 0x00, 0x00, 0x85,
 963
 964	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 74 */
 965	0x00, 0x00, 0x00, 0x00,
 966
 967	0x60, 0x00, 0x00, 0x00, /* IPv6 82 */
 968	0x00, 0x14, 0x06, 0x00,
 969	0x00, 0x00, 0x00, 0x00,
 970	0x00, 0x00, 0x00, 0x00,
 971	0x00, 0x00, 0x00, 0x00,
 972	0x00, 0x00, 0x00, 0x00,
 973	0x00, 0x00, 0x00, 0x00,
 974	0x00, 0x00, 0x00, 0x00,
 975	0x00, 0x00, 0x00, 0x00,
 976	0x00, 0x00, 0x00, 0x00,
 977
 978	0x00, 0x00, 0x00, 0x00, /* TCP 122 */
 979	0x00, 0x00, 0x00, 0x00,
 980	0x00, 0x00, 0x00, 0x00,
 981	0x50, 0x00, 0x00, 0x00,
 982	0x00, 0x00, 0x00, 0x00,
 983
 984	0x00, 0x00, /* 2 bytes for 4 byte alignment */
 985};
 986
 987ICE_DECLARE_PKT_OFFSETS(ipv6_gtpu_ipv6_udp) = {
 988	{ ICE_MAC_OFOS,		0 },
 989	{ ICE_IPV6_OFOS,	14 },
 990	{ ICE_UDP_OF,		54 },
 991	{ ICE_GTP,		62 },
 992	{ ICE_IPV6_IL,		82 },
 993	{ ICE_UDP_ILOS,		122 },
 994	{ ICE_PROTOCOL_LAST,	0 },
 995};
 996
 997ICE_DECLARE_PKT_TEMPLATE(ipv6_gtpu_ipv6_udp) = {
 998	0x00, 0x00, 0x00, 0x00, /* Ethernet 0 */
 999	0x00, 0x00, 0x00, 0x00,
1000	0x00, 0x00, 0x00, 0x00,
1001	0x86, 0xdd,
1002
1003	0x60, 0x00, 0x00, 0x00, /* IPv6 14 */
1004	0x00, 0x4c, 0x11, 0x00,
1005	0x00, 0x00, 0x00, 0x00,
1006	0x00, 0x00, 0x00, 0x00,
1007	0x00, 0x00, 0x00, 0x00,
1008	0x00, 0x00, 0x00, 0x00,
1009	0x00, 0x00, 0x00, 0x00,
1010	0x00, 0x00, 0x00, 0x00,
1011	0x00, 0x00, 0x00, 0x00,
1012	0x00, 0x00, 0x00, 0x00,
1013
1014	0x00, 0x00, 0x08, 0x68, /* UDP 54 */
1015	0x00, 0x4c, 0x00, 0x00,
1016
1017	0x34, 0xff, 0x00, 0x3c, /* ICE_GTP Header 62 */
1018	0x00, 0x00, 0x00, 0x00,
1019	0x00, 0x00, 0x00, 0x85,
1020
1021	0x02, 0x00, 0x00, 0x00, /* GTP_PDUSession_ExtensionHeader 74 */
1022	0x00, 0x00, 0x00, 0x00,
1023
1024	0x60, 0x00, 0x00, 0x00, /* IPv6 82 */
1025	0x00, 0x08, 0x11, 0x00,
1026	0x00, 0x00, 0x00, 0x00,
1027	0x00, 0x00, 0x00, 0x00,
1028	0x00, 0x00, 0x00, 0x00,
1029	0x00, 0x00, 0x00, 0x00,
1030	0x00, 0x00, 0x00, 0x00,
1031	0x00, 0x00, 0x00, 0x00,
1032	0x00, 0x00, 0x00, 0x00,
1033	0x00, 0x00, 0x00, 0x00,
1034
1035	0x00, 0x00, 0x00, 0x00, /* UDP 122 */
1036	0x00, 0x08, 0x00, 0x00,
1037
1038	0x00, 0x00, /* 2 bytes for 4 byte alignment */
1039};
1040
1041ICE_DECLARE_PKT_OFFSETS(ipv4_gtpu_ipv4) = {
1042	{ ICE_MAC_OFOS,		0 },
1043	{ ICE_IPV4_OFOS,	14 },
1044	{ ICE_UDP_OF,		34 },
1045	{ ICE_GTP_NO_PAY,	42 },
1046	{ ICE_PROTOCOL_LAST,	0 },
1047};
1048
1049ICE_DECLARE_PKT_TEMPLATE(ipv4_gtpu_ipv4) = {
1050	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1051	0x00, 0x00, 0x00, 0x00,
1052	0x00, 0x00, 0x00, 0x00,
1053	0x08, 0x00,
1054
1055	0x45, 0x00, 0x00, 0x44, /* ICE_IPV4_OFOS 14 */
1056	0x00, 0x00, 0x40, 0x00,
1057	0x40, 0x11, 0x00, 0x00,
1058	0x00, 0x00, 0x00, 0x00,
1059	0x00, 0x00, 0x00, 0x00,
1060
1061	0x08, 0x68, 0x08, 0x68, /* ICE_UDP_OF 34 */
1062	0x00, 0x00, 0x00, 0x00,
1063
1064	0x34, 0xff, 0x00, 0x28, /* ICE_GTP 42 */
1065	0x00, 0x00, 0x00, 0x00,
1066	0x00, 0x00, 0x00, 0x85,
1067
1068	0x02, 0x00, 0x00, 0x00, /* PDU Session extension header */
1069	0x00, 0x00, 0x00, 0x00,
1070
1071	0x45, 0x00, 0x00, 0x14, /* ICE_IPV4_IL 62 */
1072	0x00, 0x00, 0x40, 0x00,
1073	0x40, 0x00, 0x00, 0x00,
1074	0x00, 0x00, 0x00, 0x00,
1075	0x00, 0x00, 0x00, 0x00,
1076	0x00, 0x00,
1077};
1078
1079ICE_DECLARE_PKT_OFFSETS(ipv6_gtp) = {
1080	{ ICE_MAC_OFOS,		0 },
1081	{ ICE_IPV6_OFOS,	14 },
1082	{ ICE_UDP_OF,		54 },
1083	{ ICE_GTP_NO_PAY,	62 },
1084	{ ICE_PROTOCOL_LAST,	0 },
1085};
1086
1087ICE_DECLARE_PKT_TEMPLATE(ipv6_gtp) = {
1088	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1089	0x00, 0x00, 0x00, 0x00,
1090	0x00, 0x00, 0x00, 0x00,
1091	0x86, 0xdd,
1092
1093	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 14 */
1094	0x00, 0x6c, 0x11, 0x00, /* Next header UDP*/
1095	0x00, 0x00, 0x00, 0x00,
1096	0x00, 0x00, 0x00, 0x00,
1097	0x00, 0x00, 0x00, 0x00,
1098	0x00, 0x00, 0x00, 0x00,
1099	0x00, 0x00, 0x00, 0x00,
1100	0x00, 0x00, 0x00, 0x00,
1101	0x00, 0x00, 0x00, 0x00,
1102	0x00, 0x00, 0x00, 0x00,
1103
1104	0x08, 0x68, 0x08, 0x68, /* ICE_UDP_OF 54 */
1105	0x00, 0x00, 0x00, 0x00,
1106
1107	0x30, 0x00, 0x00, 0x28, /* ICE_GTP 62 */
1108	0x00, 0x00, 0x00, 0x00,
1109
1110	0x00, 0x00,
1111};
1112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1113ICE_DECLARE_PKT_OFFSETS(pppoe_ipv4_tcp) = {
1114	{ ICE_MAC_OFOS,		0 },
1115	{ ICE_ETYPE_OL,		12 },
1116	{ ICE_PPPOE,		14 },
1117	{ ICE_IPV4_OFOS,	22 },
1118	{ ICE_TCP_IL,		42 },
1119	{ ICE_PROTOCOL_LAST,	0 },
1120};
1121
1122ICE_DECLARE_PKT_TEMPLATE(pppoe_ipv4_tcp) = {
1123	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1124	0x00, 0x00, 0x00, 0x00,
1125	0x00, 0x00, 0x00, 0x00,
1126
1127	0x88, 0x64,		/* ICE_ETYPE_OL 12 */
1128
1129	0x11, 0x00, 0x00, 0x00, /* ICE_PPPOE 14 */
1130	0x00, 0x16,
1131
1132	0x00, 0x21,		/* PPP Link Layer 20 */
1133
1134	0x45, 0x00, 0x00, 0x28, /* ICE_IPV4_OFOS 22 */
1135	0x00, 0x01, 0x00, 0x00,
1136	0x00, 0x06, 0x00, 0x00,
1137	0x00, 0x00, 0x00, 0x00,
1138	0x00, 0x00, 0x00, 0x00,
1139
1140	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 42 */
1141	0x00, 0x00, 0x00, 0x00,
1142	0x00, 0x00, 0x00, 0x00,
1143	0x50, 0x00, 0x00, 0x00,
1144	0x00, 0x00, 0x00, 0x00,
1145
1146	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1147};
1148
1149ICE_DECLARE_PKT_OFFSETS(pppoe_ipv4_udp) = {
1150	{ ICE_MAC_OFOS,		0 },
1151	{ ICE_ETYPE_OL,		12 },
1152	{ ICE_PPPOE,		14 },
1153	{ ICE_IPV4_OFOS,	22 },
1154	{ ICE_UDP_ILOS,		42 },
1155	{ ICE_PROTOCOL_LAST,	0 },
1156};
1157
1158ICE_DECLARE_PKT_TEMPLATE(pppoe_ipv4_udp) = {
1159	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1160	0x00, 0x00, 0x00, 0x00,
1161	0x00, 0x00, 0x00, 0x00,
1162
1163	0x88, 0x64,		/* ICE_ETYPE_OL 12 */
1164
1165	0x11, 0x00, 0x00, 0x00, /* ICE_PPPOE 14 */
1166	0x00, 0x16,
1167
1168	0x00, 0x21,		/* PPP Link Layer 20 */
1169
1170	0x45, 0x00, 0x00, 0x1c, /* ICE_IPV4_OFOS 22 */
1171	0x00, 0x01, 0x00, 0x00,
1172	0x00, 0x11, 0x00, 0x00,
1173	0x00, 0x00, 0x00, 0x00,
1174	0x00, 0x00, 0x00, 0x00,
1175
1176	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 42 */
1177	0x00, 0x08, 0x00, 0x00,
1178
1179	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1180};
1181
1182ICE_DECLARE_PKT_OFFSETS(pppoe_ipv6_tcp) = {
1183	{ ICE_MAC_OFOS,		0 },
1184	{ ICE_ETYPE_OL,		12 },
1185	{ ICE_PPPOE,		14 },
1186	{ ICE_IPV6_OFOS,	22 },
1187	{ ICE_TCP_IL,		62 },
1188	{ ICE_PROTOCOL_LAST,	0 },
1189};
1190
1191ICE_DECLARE_PKT_TEMPLATE(pppoe_ipv6_tcp) = {
1192	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1193	0x00, 0x00, 0x00, 0x00,
1194	0x00, 0x00, 0x00, 0x00,
1195
1196	0x88, 0x64,		/* ICE_ETYPE_OL 12 */
1197
1198	0x11, 0x00, 0x00, 0x00, /* ICE_PPPOE 14 */
1199	0x00, 0x2a,
1200
1201	0x00, 0x57,		/* PPP Link Layer 20 */
1202
1203	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 22 */
1204	0x00, 0x14, 0x06, 0x00, /* Next header is TCP */
1205	0x00, 0x00, 0x00, 0x00,
1206	0x00, 0x00, 0x00, 0x00,
1207	0x00, 0x00, 0x00, 0x00,
1208	0x00, 0x00, 0x00, 0x00,
1209	0x00, 0x00, 0x00, 0x00,
1210	0x00, 0x00, 0x00, 0x00,
1211	0x00, 0x00, 0x00, 0x00,
1212	0x00, 0x00, 0x00, 0x00,
1213
1214	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 62 */
1215	0x00, 0x00, 0x00, 0x00,
1216	0x00, 0x00, 0x00, 0x00,
1217	0x50, 0x00, 0x00, 0x00,
1218	0x00, 0x00, 0x00, 0x00,
1219
1220	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1221};
1222
1223ICE_DECLARE_PKT_OFFSETS(pppoe_ipv6_udp) = {
1224	{ ICE_MAC_OFOS,		0 },
1225	{ ICE_ETYPE_OL,		12 },
1226	{ ICE_PPPOE,		14 },
1227	{ ICE_IPV6_OFOS,	22 },
1228	{ ICE_UDP_ILOS,		62 },
1229	{ ICE_PROTOCOL_LAST,	0 },
1230};
1231
1232ICE_DECLARE_PKT_TEMPLATE(pppoe_ipv6_udp) = {
1233	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1234	0x00, 0x00, 0x00, 0x00,
1235	0x00, 0x00, 0x00, 0x00,
1236
1237	0x88, 0x64,		/* ICE_ETYPE_OL 12 */
1238
1239	0x11, 0x00, 0x00, 0x00, /* ICE_PPPOE 14 */
1240	0x00, 0x2a,
1241
1242	0x00, 0x57,		/* PPP Link Layer 20 */
1243
1244	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 22 */
1245	0x00, 0x08, 0x11, 0x00, /* Next header UDP*/
1246	0x00, 0x00, 0x00, 0x00,
1247	0x00, 0x00, 0x00, 0x00,
1248	0x00, 0x00, 0x00, 0x00,
1249	0x00, 0x00, 0x00, 0x00,
1250	0x00, 0x00, 0x00, 0x00,
1251	0x00, 0x00, 0x00, 0x00,
1252	0x00, 0x00, 0x00, 0x00,
1253	0x00, 0x00, 0x00, 0x00,
1254
1255	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 62 */
1256	0x00, 0x08, 0x00, 0x00,
1257
1258	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1259};
1260
1261ICE_DECLARE_PKT_OFFSETS(ipv4_l2tpv3) = {
1262	{ ICE_MAC_OFOS,		0 },
1263	{ ICE_ETYPE_OL,		12 },
1264	{ ICE_IPV4_OFOS,	14 },
1265	{ ICE_L2TPV3,		34 },
1266	{ ICE_PROTOCOL_LAST,	0 },
1267};
1268
1269ICE_DECLARE_PKT_TEMPLATE(ipv4_l2tpv3) = {
1270	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1271	0x00, 0x00, 0x00, 0x00,
1272	0x00, 0x00, 0x00, 0x00,
1273
1274	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
1275
1276	0x45, 0x00, 0x00, 0x20, /* ICE_IPV4_IL 14 */
1277	0x00, 0x00, 0x40, 0x00,
1278	0x40, 0x73, 0x00, 0x00,
1279	0x00, 0x00, 0x00, 0x00,
1280	0x00, 0x00, 0x00, 0x00,
1281
1282	0x00, 0x00, 0x00, 0x00, /* ICE_L2TPV3 34 */
1283	0x00, 0x00, 0x00, 0x00,
1284	0x00, 0x00, 0x00, 0x00,
1285	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1286};
1287
1288ICE_DECLARE_PKT_OFFSETS(ipv6_l2tpv3) = {
1289	{ ICE_MAC_OFOS,		0 },
1290	{ ICE_ETYPE_OL,		12 },
1291	{ ICE_IPV6_OFOS,	14 },
1292	{ ICE_L2TPV3,		54 },
1293	{ ICE_PROTOCOL_LAST,	0 },
1294};
1295
1296ICE_DECLARE_PKT_TEMPLATE(ipv6_l2tpv3) = {
1297	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
1298	0x00, 0x00, 0x00, 0x00,
1299	0x00, 0x00, 0x00, 0x00,
1300
1301	0x86, 0xDD,		/* ICE_ETYPE_OL 12 */
1302
1303	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_IL 14 */
1304	0x00, 0x0c, 0x73, 0x40,
1305	0x00, 0x00, 0x00, 0x00,
1306	0x00, 0x00, 0x00, 0x00,
1307	0x00, 0x00, 0x00, 0x00,
1308	0x00, 0x00, 0x00, 0x00,
1309	0x00, 0x00, 0x00, 0x00,
1310	0x00, 0x00, 0x00, 0x00,
1311	0x00, 0x00, 0x00, 0x00,
1312	0x00, 0x00, 0x00, 0x00,
1313
1314	0x00, 0x00, 0x00, 0x00, /* ICE_L2TPV3 54 */
1315	0x00, 0x00, 0x00, 0x00,
1316	0x00, 0x00, 0x00, 0x00,
1317	0x00, 0x00,		/* 2 bytes for 4 bytes alignment */
1318};
1319
1320static const struct ice_dummy_pkt_profile ice_dummy_pkt_profiles[] = {
1321	ICE_PKT_PROFILE(ipv6_gtp, ICE_PKT_TUN_GTPU | ICE_PKT_OUTER_IPV6 |
1322				  ICE_PKT_GTP_NOPAY),
1323	ICE_PKT_PROFILE(ipv6_gtpu_ipv6_udp, ICE_PKT_TUN_GTPU |
1324					    ICE_PKT_OUTER_IPV6 |
1325					    ICE_PKT_INNER_IPV6 |
1326					    ICE_PKT_INNER_UDP),
1327	ICE_PKT_PROFILE(ipv6_gtpu_ipv6_tcp, ICE_PKT_TUN_GTPU |
1328					    ICE_PKT_OUTER_IPV6 |
1329					    ICE_PKT_INNER_IPV6),
1330	ICE_PKT_PROFILE(ipv6_gtpu_ipv4_udp, ICE_PKT_TUN_GTPU |
1331					    ICE_PKT_OUTER_IPV6 |
1332					    ICE_PKT_INNER_UDP),
1333	ICE_PKT_PROFILE(ipv6_gtpu_ipv4_tcp, ICE_PKT_TUN_GTPU |
1334					    ICE_PKT_OUTER_IPV6),
1335	ICE_PKT_PROFILE(ipv4_gtpu_ipv4, ICE_PKT_TUN_GTPU | ICE_PKT_GTP_NOPAY),
1336	ICE_PKT_PROFILE(ipv4_gtpu_ipv6_udp, ICE_PKT_TUN_GTPU |
1337					    ICE_PKT_INNER_IPV6 |
1338					    ICE_PKT_INNER_UDP),
1339	ICE_PKT_PROFILE(ipv4_gtpu_ipv6_tcp, ICE_PKT_TUN_GTPU |
1340					    ICE_PKT_INNER_IPV6),
1341	ICE_PKT_PROFILE(ipv4_gtpu_ipv4_udp, ICE_PKT_TUN_GTPU |
1342					    ICE_PKT_INNER_UDP),
1343	ICE_PKT_PROFILE(ipv4_gtpu_ipv4_tcp, ICE_PKT_TUN_GTPU),
1344	ICE_PKT_PROFILE(ipv6_gtp, ICE_PKT_TUN_GTPC | ICE_PKT_OUTER_IPV6),
1345	ICE_PKT_PROFILE(ipv4_gtpu_ipv4, ICE_PKT_TUN_GTPC),
 
 
1346	ICE_PKT_PROFILE(pppoe_ipv6_udp, ICE_PKT_PPPOE | ICE_PKT_OUTER_IPV6 |
1347					ICE_PKT_INNER_UDP),
1348	ICE_PKT_PROFILE(pppoe_ipv6_tcp, ICE_PKT_PPPOE | ICE_PKT_OUTER_IPV6),
1349	ICE_PKT_PROFILE(pppoe_ipv4_udp, ICE_PKT_PPPOE | ICE_PKT_INNER_UDP),
1350	ICE_PKT_PROFILE(pppoe_ipv4_tcp, ICE_PKT_PPPOE),
1351	ICE_PKT_PROFILE(gre_ipv6_tcp, ICE_PKT_TUN_NVGRE | ICE_PKT_INNER_IPV6 |
1352				      ICE_PKT_INNER_TCP),
1353	ICE_PKT_PROFILE(gre_tcp, ICE_PKT_TUN_NVGRE | ICE_PKT_INNER_TCP),
1354	ICE_PKT_PROFILE(gre_ipv6_udp, ICE_PKT_TUN_NVGRE | ICE_PKT_INNER_IPV6),
1355	ICE_PKT_PROFILE(gre_udp, ICE_PKT_TUN_NVGRE),
1356	ICE_PKT_PROFILE(udp_tun_ipv6_tcp, ICE_PKT_TUN_UDP |
1357					  ICE_PKT_INNER_IPV6 |
1358					  ICE_PKT_INNER_TCP),
1359	ICE_PKT_PROFILE(ipv6_l2tpv3, ICE_PKT_L2TPV3 | ICE_PKT_OUTER_IPV6),
1360	ICE_PKT_PROFILE(ipv4_l2tpv3, ICE_PKT_L2TPV3),
1361	ICE_PKT_PROFILE(udp_tun_tcp, ICE_PKT_TUN_UDP | ICE_PKT_INNER_TCP),
1362	ICE_PKT_PROFILE(udp_tun_ipv6_udp, ICE_PKT_TUN_UDP |
1363					  ICE_PKT_INNER_IPV6),
1364	ICE_PKT_PROFILE(udp_tun_udp, ICE_PKT_TUN_UDP),
1365	ICE_PKT_PROFILE(udp_ipv6, ICE_PKT_OUTER_IPV6 | ICE_PKT_INNER_UDP),
1366	ICE_PKT_PROFILE(udp, ICE_PKT_INNER_UDP),
1367	ICE_PKT_PROFILE(tcp_ipv6, ICE_PKT_OUTER_IPV6),
1368	ICE_PKT_PROFILE(tcp, 0),
1369};
1370
1371/* this is a recipe to profile association bitmap */
1372static DECLARE_BITMAP(recipe_to_profile[ICE_MAX_NUM_RECIPES],
1373			  ICE_MAX_NUM_PROFILES);
1374
1375/* this is a profile to recipe association bitmap */
1376static DECLARE_BITMAP(profile_to_recipe[ICE_MAX_NUM_PROFILES],
1377			  ICE_MAX_NUM_RECIPES);
1378
1379/**
1380 * ice_init_def_sw_recp - initialize the recipe book keeping tables
1381 * @hw: pointer to the HW struct
1382 *
1383 * Allocate memory for the entire recipe table and initialize the structures/
1384 * entries corresponding to basic recipes.
1385 */
1386int ice_init_def_sw_recp(struct ice_hw *hw)
1387{
1388	struct ice_sw_recipe *recps;
1389	u8 i;
1390
1391	recps = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_NUM_RECIPES,
1392			     sizeof(*recps), GFP_KERNEL);
1393	if (!recps)
1394		return -ENOMEM;
1395
1396	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
1397		recps[i].root_rid = i;
1398		INIT_LIST_HEAD(&recps[i].filt_rules);
1399		INIT_LIST_HEAD(&recps[i].filt_replay_rules);
1400		INIT_LIST_HEAD(&recps[i].rg_list);
1401		mutex_init(&recps[i].filt_rule_lock);
1402	}
1403
1404	hw->switch_info->recp_list = recps;
1405
1406	return 0;
1407}
1408
1409/**
1410 * ice_aq_get_sw_cfg - get switch configuration
1411 * @hw: pointer to the hardware structure
1412 * @buf: pointer to the result buffer
1413 * @buf_size: length of the buffer available for response
1414 * @req_desc: pointer to requested descriptor
1415 * @num_elems: pointer to number of elements
1416 * @cd: pointer to command details structure or NULL
1417 *
1418 * Get switch configuration (0x0200) to be placed in buf.
1419 * This admin command returns information such as initial VSI/port number
1420 * and switch ID it belongs to.
1421 *
1422 * NOTE: *req_desc is both an input/output parameter.
1423 * The caller of this function first calls this function with *request_desc set
1424 * to 0. If the response from f/w has *req_desc set to 0, all the switch
1425 * configuration information has been returned; if non-zero (meaning not all
1426 * the information was returned), the caller should call this function again
1427 * with *req_desc set to the previous value returned by f/w to get the
1428 * next block of switch configuration information.
1429 *
1430 * *num_elems is output only parameter. This reflects the number of elements
1431 * in response buffer. The caller of this function to use *num_elems while
1432 * parsing the response buffer.
1433 */
1434static int
1435ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp_elem *buf,
1436		  u16 buf_size, u16 *req_desc, u16 *num_elems,
1437		  struct ice_sq_cd *cd)
1438{
1439	struct ice_aqc_get_sw_cfg *cmd;
1440	struct ice_aq_desc desc;
1441	int status;
1442
1443	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
1444	cmd = &desc.params.get_sw_conf;
1445	cmd->element = cpu_to_le16(*req_desc);
1446
1447	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
1448	if (!status) {
1449		*req_desc = le16_to_cpu(cmd->element);
1450		*num_elems = le16_to_cpu(cmd->num_elems);
1451	}
1452
1453	return status;
1454}
1455
1456/**
1457 * ice_aq_add_vsi
1458 * @hw: pointer to the HW struct
1459 * @vsi_ctx: pointer to a VSI context struct
1460 * @cd: pointer to command details structure or NULL
1461 *
1462 * Add a VSI context to the hardware (0x0210)
1463 */
1464static int
1465ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
1466	       struct ice_sq_cd *cd)
1467{
1468	struct ice_aqc_add_update_free_vsi_resp *res;
1469	struct ice_aqc_add_get_update_free_vsi *cmd;
1470	struct ice_aq_desc desc;
1471	int status;
1472
1473	cmd = &desc.params.vsi_cmd;
1474	res = &desc.params.add_update_free_vsi_res;
1475
1476	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
1477
1478	if (!vsi_ctx->alloc_from_pool)
1479		cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num |
1480					   ICE_AQ_VSI_IS_VALID);
1481	cmd->vf_id = vsi_ctx->vf_num;
1482
1483	cmd->vsi_flags = cpu_to_le16(vsi_ctx->flags);
1484
1485	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1486
1487	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
1488				 sizeof(vsi_ctx->info), cd);
1489
1490	if (!status) {
1491		vsi_ctx->vsi_num = le16_to_cpu(res->vsi_num) & ICE_AQ_VSI_NUM_M;
1492		vsi_ctx->vsis_allocd = le16_to_cpu(res->vsi_used);
1493		vsi_ctx->vsis_unallocated = le16_to_cpu(res->vsi_free);
1494	}
1495
1496	return status;
1497}
1498
1499/**
1500 * ice_aq_free_vsi
1501 * @hw: pointer to the HW struct
1502 * @vsi_ctx: pointer to a VSI context struct
1503 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
1504 * @cd: pointer to command details structure or NULL
1505 *
1506 * Free VSI context info from hardware (0x0213)
1507 */
1508static int
1509ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
1510		bool keep_vsi_alloc, struct ice_sq_cd *cd)
1511{
1512	struct ice_aqc_add_update_free_vsi_resp *resp;
1513	struct ice_aqc_add_get_update_free_vsi *cmd;
1514	struct ice_aq_desc desc;
1515	int status;
1516
1517	cmd = &desc.params.vsi_cmd;
1518	resp = &desc.params.add_update_free_vsi_res;
1519
1520	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
1521
1522	cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
1523	if (keep_vsi_alloc)
1524		cmd->cmd_flags = cpu_to_le16(ICE_AQ_VSI_KEEP_ALLOC);
1525
1526	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1527	if (!status) {
1528		vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
1529		vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
1530	}
1531
1532	return status;
1533}
1534
1535/**
1536 * ice_aq_update_vsi
1537 * @hw: pointer to the HW struct
1538 * @vsi_ctx: pointer to a VSI context struct
1539 * @cd: pointer to command details structure or NULL
1540 *
1541 * Update VSI context in the hardware (0x0211)
1542 */
1543static int
1544ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
1545		  struct ice_sq_cd *cd)
1546{
1547	struct ice_aqc_add_update_free_vsi_resp *resp;
1548	struct ice_aqc_add_get_update_free_vsi *cmd;
1549	struct ice_aq_desc desc;
1550	int status;
1551
1552	cmd = &desc.params.vsi_cmd;
1553	resp = &desc.params.add_update_free_vsi_res;
1554
1555	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
1556
1557	cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
1558
1559	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1560
1561	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
1562				 sizeof(vsi_ctx->info), cd);
1563
1564	if (!status) {
1565		vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
1566		vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
1567	}
1568
1569	return status;
1570}
1571
1572/**
1573 * ice_is_vsi_valid - check whether the VSI is valid or not
1574 * @hw: pointer to the HW struct
1575 * @vsi_handle: VSI handle
1576 *
1577 * check whether the VSI is valid or not
1578 */
1579bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
1580{
1581	return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
1582}
1583
1584/**
1585 * ice_get_hw_vsi_num - return the HW VSI number
1586 * @hw: pointer to the HW struct
1587 * @vsi_handle: VSI handle
1588 *
1589 * return the HW VSI number
1590 * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
1591 */
1592u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
1593{
1594	return hw->vsi_ctx[vsi_handle]->vsi_num;
1595}
1596
1597/**
1598 * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
1599 * @hw: pointer to the HW struct
1600 * @vsi_handle: VSI handle
1601 *
1602 * return the VSI context entry for a given VSI handle
1603 */
1604struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
1605{
1606	return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
1607}
1608
1609/**
1610 * ice_save_vsi_ctx - save the VSI context for a given VSI handle
1611 * @hw: pointer to the HW struct
1612 * @vsi_handle: VSI handle
1613 * @vsi: VSI context pointer
1614 *
1615 * save the VSI context entry for a given VSI handle
1616 */
1617static void
1618ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi)
1619{
1620	hw->vsi_ctx[vsi_handle] = vsi;
1621}
1622
1623/**
1624 * ice_clear_vsi_q_ctx - clear VSI queue contexts for all TCs
1625 * @hw: pointer to the HW struct
1626 * @vsi_handle: VSI handle
1627 */
1628static void ice_clear_vsi_q_ctx(struct ice_hw *hw, u16 vsi_handle)
1629{
1630	struct ice_vsi_ctx *vsi = ice_get_vsi_ctx(hw, vsi_handle);
1631	u8 i;
1632
1633	if (!vsi)
1634		return;
1635	ice_for_each_traffic_class(i) {
1636		devm_kfree(ice_hw_to_dev(hw), vsi->lan_q_ctx[i]);
1637		vsi->lan_q_ctx[i] = NULL;
1638		devm_kfree(ice_hw_to_dev(hw), vsi->rdma_q_ctx[i]);
1639		vsi->rdma_q_ctx[i] = NULL;
1640	}
1641}
1642
1643/**
1644 * ice_clear_vsi_ctx - clear the VSI context entry
1645 * @hw: pointer to the HW struct
1646 * @vsi_handle: VSI handle
1647 *
1648 * clear the VSI context entry
1649 */
1650static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
1651{
1652	struct ice_vsi_ctx *vsi;
1653
1654	vsi = ice_get_vsi_ctx(hw, vsi_handle);
1655	if (vsi) {
1656		ice_clear_vsi_q_ctx(hw, vsi_handle);
1657		devm_kfree(ice_hw_to_dev(hw), vsi);
1658		hw->vsi_ctx[vsi_handle] = NULL;
1659	}
1660}
1661
1662/**
1663 * ice_clear_all_vsi_ctx - clear all the VSI context entries
1664 * @hw: pointer to the HW struct
1665 */
1666void ice_clear_all_vsi_ctx(struct ice_hw *hw)
1667{
1668	u16 i;
1669
1670	for (i = 0; i < ICE_MAX_VSI; i++)
1671		ice_clear_vsi_ctx(hw, i);
1672}
1673
1674/**
1675 * ice_add_vsi - add VSI context to the hardware and VSI handle list
1676 * @hw: pointer to the HW struct
1677 * @vsi_handle: unique VSI handle provided by drivers
1678 * @vsi_ctx: pointer to a VSI context struct
1679 * @cd: pointer to command details structure or NULL
1680 *
1681 * Add a VSI context to the hardware also add it into the VSI handle list.
1682 * If this function gets called after reset for existing VSIs then update
1683 * with the new HW VSI number in the corresponding VSI handle list entry.
1684 */
1685int
1686ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
1687	    struct ice_sq_cd *cd)
1688{
1689	struct ice_vsi_ctx *tmp_vsi_ctx;
1690	int status;
1691
1692	if (vsi_handle >= ICE_MAX_VSI)
1693		return -EINVAL;
1694	status = ice_aq_add_vsi(hw, vsi_ctx, cd);
1695	if (status)
1696		return status;
1697	tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1698	if (!tmp_vsi_ctx) {
1699		/* Create a new VSI context */
1700		tmp_vsi_ctx = devm_kzalloc(ice_hw_to_dev(hw),
1701					   sizeof(*tmp_vsi_ctx), GFP_KERNEL);
1702		if (!tmp_vsi_ctx) {
1703			ice_aq_free_vsi(hw, vsi_ctx, false, cd);
1704			return -ENOMEM;
1705		}
1706		*tmp_vsi_ctx = *vsi_ctx;
1707		ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
1708	} else {
1709		/* update with new HW VSI num */
1710		tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
1711	}
1712
1713	return 0;
1714}
1715
1716/**
1717 * ice_free_vsi- free VSI context from hardware and VSI handle list
1718 * @hw: pointer to the HW struct
1719 * @vsi_handle: unique VSI handle
1720 * @vsi_ctx: pointer to a VSI context struct
1721 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
1722 * @cd: pointer to command details structure or NULL
1723 *
1724 * Free VSI context info from hardware as well as from VSI handle list
1725 */
1726int
1727ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
1728	     bool keep_vsi_alloc, struct ice_sq_cd *cd)
1729{
1730	int status;
1731
1732	if (!ice_is_vsi_valid(hw, vsi_handle))
1733		return -EINVAL;
1734	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
1735	status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
1736	if (!status)
1737		ice_clear_vsi_ctx(hw, vsi_handle);
1738	return status;
1739}
1740
1741/**
1742 * ice_update_vsi
1743 * @hw: pointer to the HW struct
1744 * @vsi_handle: unique VSI handle
1745 * @vsi_ctx: pointer to a VSI context struct
1746 * @cd: pointer to command details structure or NULL
1747 *
1748 * Update VSI context in the hardware
1749 */
1750int
1751ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
1752	       struct ice_sq_cd *cd)
1753{
1754	if (!ice_is_vsi_valid(hw, vsi_handle))
1755		return -EINVAL;
1756	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
1757	return ice_aq_update_vsi(hw, vsi_ctx, cd);
1758}
1759
1760/**
1761 * ice_cfg_rdma_fltr - enable/disable RDMA filtering on VSI
1762 * @hw: pointer to HW struct
1763 * @vsi_handle: VSI SW index
1764 * @enable: boolean for enable/disable
1765 */
1766int
1767ice_cfg_rdma_fltr(struct ice_hw *hw, u16 vsi_handle, bool enable)
1768{
1769	struct ice_vsi_ctx *ctx, *cached_ctx;
1770	int status;
1771
1772	cached_ctx = ice_get_vsi_ctx(hw, vsi_handle);
1773	if (!cached_ctx)
1774		return -ENOENT;
1775
1776	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1777	if (!ctx)
1778		return -ENOMEM;
1779
1780	ctx->info.q_opt_rss = cached_ctx->info.q_opt_rss;
1781	ctx->info.q_opt_tc = cached_ctx->info.q_opt_tc;
1782	ctx->info.q_opt_flags = cached_ctx->info.q_opt_flags;
1783
1784	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1785
1786	if (enable)
1787		ctx->info.q_opt_flags |= ICE_AQ_VSI_Q_OPT_PE_FLTR_EN;
1788	else
1789		ctx->info.q_opt_flags &= ~ICE_AQ_VSI_Q_OPT_PE_FLTR_EN;
1790
1791	status = ice_update_vsi(hw, vsi_handle, ctx, NULL);
1792	if (!status) {
1793		cached_ctx->info.q_opt_flags = ctx->info.q_opt_flags;
1794		cached_ctx->info.valid_sections |= ctx->info.valid_sections;
1795	}
1796
1797	kfree(ctx);
1798	return status;
1799}
1800
1801/**
1802 * ice_aq_alloc_free_vsi_list
1803 * @hw: pointer to the HW struct
1804 * @vsi_list_id: VSI list ID returned or used for lookup
1805 * @lkup_type: switch rule filter lookup type
1806 * @opc: switch rules population command type - pass in the command opcode
1807 *
1808 * allocates or free a VSI list resource
1809 */
1810static int
1811ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
1812			   enum ice_sw_lkup_type lkup_type,
1813			   enum ice_adminq_opc opc)
1814{
1815	DEFINE_FLEX(struct ice_aqc_alloc_free_res_elem, sw_buf, elem, 1);
1816	u16 buf_len = __struct_size(sw_buf);
1817	struct ice_aqc_res_elem *vsi_ele;
1818	int status;
1819
1820	sw_buf->num_elems = cpu_to_le16(1);
1821
1822	if (lkup_type == ICE_SW_LKUP_MAC ||
1823	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
1824	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1825	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1826	    lkup_type == ICE_SW_LKUP_PROMISC ||
1827	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1828	    lkup_type == ICE_SW_LKUP_DFLT) {
 
1829		sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
1830	} else if (lkup_type == ICE_SW_LKUP_VLAN) {
1831		if (opc == ice_aqc_opc_alloc_res)
1832			sw_buf->res_type =
1833				cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE |
1834					    ICE_AQC_RES_TYPE_FLAG_SHARED);
1835		else
1836			sw_buf->res_type =
1837				cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
1838	} else {
1839		return -EINVAL;
1840	}
1841
1842	if (opc == ice_aqc_opc_free_res)
1843		sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
1844
1845	status = ice_aq_alloc_free_res(hw, sw_buf, buf_len, opc);
1846	if (status)
1847		return status;
1848
1849	if (opc == ice_aqc_opc_alloc_res) {
1850		vsi_ele = &sw_buf->elem[0];
1851		*vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp);
1852	}
1853
1854	return 0;
1855}
1856
1857/**
1858 * ice_aq_sw_rules - add/update/remove switch rules
1859 * @hw: pointer to the HW struct
1860 * @rule_list: pointer to switch rule population list
1861 * @rule_list_sz: total size of the rule list in bytes
1862 * @num_rules: number of switch rules in the rule_list
1863 * @opc: switch rules population command type - pass in the command opcode
1864 * @cd: pointer to command details structure or NULL
1865 *
1866 * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
1867 */
1868int
1869ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
1870		u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
1871{
1872	struct ice_aq_desc desc;
1873	int status;
1874
1875	if (opc != ice_aqc_opc_add_sw_rules &&
1876	    opc != ice_aqc_opc_update_sw_rules &&
1877	    opc != ice_aqc_opc_remove_sw_rules)
1878		return -EINVAL;
1879
1880	ice_fill_dflt_direct_cmd_desc(&desc, opc);
1881
1882	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1883	desc.params.sw_rules.num_rules_fltr_entry_index =
1884		cpu_to_le16(num_rules);
1885	status = ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
1886	if (opc != ice_aqc_opc_add_sw_rules &&
1887	    hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT)
1888		status = -ENOENT;
1889
 
 
 
 
 
 
 
 
 
1890	return status;
1891}
1892
1893/**
1894 * ice_aq_add_recipe - add switch recipe
1895 * @hw: pointer to the HW struct
1896 * @s_recipe_list: pointer to switch rule population list
1897 * @num_recipes: number of switch recipes in the list
1898 * @cd: pointer to command details structure or NULL
1899 *
1900 * Add(0x0290)
1901 */
1902int
1903ice_aq_add_recipe(struct ice_hw *hw,
1904		  struct ice_aqc_recipe_data_elem *s_recipe_list,
1905		  u16 num_recipes, struct ice_sq_cd *cd)
1906{
1907	struct ice_aqc_add_get_recipe *cmd;
1908	struct ice_aq_desc desc;
1909	u16 buf_size;
1910
1911	cmd = &desc.params.add_get_recipe;
1912	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_recipe);
1913
1914	cmd->num_sub_recipes = cpu_to_le16(num_recipes);
1915	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1916
1917	buf_size = num_recipes * sizeof(*s_recipe_list);
1918
1919	return ice_aq_send_cmd(hw, &desc, s_recipe_list, buf_size, cd);
1920}
1921
1922/**
1923 * ice_aq_get_recipe - get switch recipe
1924 * @hw: pointer to the HW struct
1925 * @s_recipe_list: pointer to switch rule population list
1926 * @num_recipes: pointer to the number of recipes (input and output)
1927 * @recipe_root: root recipe number of recipe(s) to retrieve
1928 * @cd: pointer to command details structure or NULL
1929 *
1930 * Get(0x0292)
1931 *
1932 * On input, *num_recipes should equal the number of entries in s_recipe_list.
1933 * On output, *num_recipes will equal the number of entries returned in
1934 * s_recipe_list.
1935 *
1936 * The caller must supply enough space in s_recipe_list to hold all possible
1937 * recipes and *num_recipes must equal ICE_MAX_NUM_RECIPES.
1938 */
1939int
1940ice_aq_get_recipe(struct ice_hw *hw,
1941		  struct ice_aqc_recipe_data_elem *s_recipe_list,
1942		  u16 *num_recipes, u16 recipe_root, struct ice_sq_cd *cd)
1943{
1944	struct ice_aqc_add_get_recipe *cmd;
1945	struct ice_aq_desc desc;
1946	u16 buf_size;
1947	int status;
1948
1949	if (*num_recipes != ICE_MAX_NUM_RECIPES)
1950		return -EINVAL;
1951
1952	cmd = &desc.params.add_get_recipe;
1953	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_recipe);
1954
1955	cmd->return_index = cpu_to_le16(recipe_root);
1956	cmd->num_sub_recipes = 0;
1957
1958	buf_size = *num_recipes * sizeof(*s_recipe_list);
1959
1960	status = ice_aq_send_cmd(hw, &desc, s_recipe_list, buf_size, cd);
1961	*num_recipes = le16_to_cpu(cmd->num_sub_recipes);
1962
1963	return status;
1964}
1965
1966/**
1967 * ice_update_recipe_lkup_idx - update a default recipe based on the lkup_idx
1968 * @hw: pointer to the HW struct
1969 * @params: parameters used to update the default recipe
1970 *
1971 * This function only supports updating default recipes and it only supports
1972 * updating a single recipe based on the lkup_idx at a time.
1973 *
1974 * This is done as a read-modify-write operation. First, get the current recipe
1975 * contents based on the recipe's ID. Then modify the field vector index and
1976 * mask if it's valid at the lkup_idx. Finally, use the add recipe AQ to update
1977 * the pre-existing recipe with the modifications.
1978 */
1979int
1980ice_update_recipe_lkup_idx(struct ice_hw *hw,
1981			   struct ice_update_recipe_lkup_idx_params *params)
1982{
1983	struct ice_aqc_recipe_data_elem *rcp_list;
1984	u16 num_recps = ICE_MAX_NUM_RECIPES;
1985	int status;
1986
1987	rcp_list = kcalloc(num_recps, sizeof(*rcp_list), GFP_KERNEL);
1988	if (!rcp_list)
1989		return -ENOMEM;
1990
1991	/* read current recipe list from firmware */
1992	rcp_list->recipe_indx = params->rid;
1993	status = ice_aq_get_recipe(hw, rcp_list, &num_recps, params->rid, NULL);
1994	if (status) {
1995		ice_debug(hw, ICE_DBG_SW, "Failed to get recipe %d, status %d\n",
1996			  params->rid, status);
1997		goto error_out;
1998	}
1999
2000	/* only modify existing recipe's lkup_idx and mask if valid, while
2001	 * leaving all other fields the same, then update the recipe firmware
2002	 */
2003	rcp_list->content.lkup_indx[params->lkup_idx] = params->fv_idx;
2004	if (params->mask_valid)
2005		rcp_list->content.mask[params->lkup_idx] =
2006			cpu_to_le16(params->mask);
2007
2008	if (params->ignore_valid)
2009		rcp_list->content.lkup_indx[params->lkup_idx] |=
2010			ICE_AQ_RECIPE_LKUP_IGNORE;
2011
2012	status = ice_aq_add_recipe(hw, &rcp_list[0], 1, NULL);
2013	if (status)
2014		ice_debug(hw, ICE_DBG_SW, "Failed to update recipe %d lkup_idx %d fv_idx %d mask %d mask_valid %s, status %d\n",
2015			  params->rid, params->lkup_idx, params->fv_idx,
2016			  params->mask, params->mask_valid ? "true" : "false",
2017			  status);
2018
2019error_out:
2020	kfree(rcp_list);
2021	return status;
2022}
2023
2024/**
2025 * ice_aq_map_recipe_to_profile - Map recipe to packet profile
2026 * @hw: pointer to the HW struct
2027 * @profile_id: package profile ID to associate the recipe with
2028 * @r_bitmap: Recipe bitmap filled in and need to be returned as response
2029 * @cd: pointer to command details structure or NULL
2030 * Recipe to profile association (0x0291)
2031 */
2032int
2033ice_aq_map_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u8 *r_bitmap,
2034			     struct ice_sq_cd *cd)
2035{
2036	struct ice_aqc_recipe_to_profile *cmd;
2037	struct ice_aq_desc desc;
2038
2039	cmd = &desc.params.recipe_to_profile;
2040	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_recipe_to_profile);
2041	cmd->profile_id = cpu_to_le16(profile_id);
2042	/* Set the recipe ID bit in the bitmask to let the device know which
2043	 * profile we are associating the recipe to
2044	 */
2045	memcpy(cmd->recipe_assoc, r_bitmap, sizeof(cmd->recipe_assoc));
2046
2047	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2048}
2049
2050/**
2051 * ice_aq_get_recipe_to_profile - Map recipe to packet profile
2052 * @hw: pointer to the HW struct
2053 * @profile_id: package profile ID to associate the recipe with
2054 * @r_bitmap: Recipe bitmap filled in and need to be returned as response
2055 * @cd: pointer to command details structure or NULL
2056 * Associate profile ID with given recipe (0x0293)
2057 */
2058int
2059ice_aq_get_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u8 *r_bitmap,
2060			     struct ice_sq_cd *cd)
2061{
2062	struct ice_aqc_recipe_to_profile *cmd;
2063	struct ice_aq_desc desc;
2064	int status;
2065
2066	cmd = &desc.params.recipe_to_profile;
2067	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_recipe_to_profile);
2068	cmd->profile_id = cpu_to_le16(profile_id);
2069
2070	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
2071	if (!status)
2072		memcpy(r_bitmap, cmd->recipe_assoc, sizeof(cmd->recipe_assoc));
2073
2074	return status;
2075}
2076
2077/**
 
 
 
 
 
 
 
 
 
 
 
 
2078 * ice_alloc_recipe - add recipe resource
2079 * @hw: pointer to the hardware structure
2080 * @rid: recipe ID returned as response to AQ call
2081 */
2082int ice_alloc_recipe(struct ice_hw *hw, u16 *rid)
2083{
2084	DEFINE_FLEX(struct ice_aqc_alloc_free_res_elem, sw_buf, elem, 1);
2085	u16 buf_len = __struct_size(sw_buf);
 
2086	int status;
2087
2088	sw_buf->num_elems = cpu_to_le16(1);
2089	sw_buf->res_type = cpu_to_le16((ICE_AQC_RES_TYPE_RECIPE <<
2090					ICE_AQC_RES_TYPE_S) |
2091					ICE_AQC_RES_TYPE_FLAG_SHARED);
 
 
 
2092	status = ice_aq_alloc_free_res(hw, sw_buf, buf_len,
2093				       ice_aqc_opc_alloc_res);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2094	if (!status)
2095		*rid = le16_to_cpu(sw_buf->elem[0].e.sw_resp);
2096
2097	return status;
2098}
2099
2100/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2101 * ice_get_recp_to_prof_map - updates recipe to profile mapping
2102 * @hw: pointer to hardware structure
2103 *
2104 * This function is used to populate recipe_to_profile matrix where index to
2105 * this array is the recipe ID and the element is the mapping of which profiles
2106 * is this recipe mapped to.
2107 */
2108static void ice_get_recp_to_prof_map(struct ice_hw *hw)
2109{
2110	DECLARE_BITMAP(r_bitmap, ICE_MAX_NUM_RECIPES);
 
2111	u16 i;
2112
2113	for (i = 0; i < hw->switch_info->max_used_prof_index + 1; i++) {
2114		u16 j;
2115
2116		bitmap_zero(profile_to_recipe[i], ICE_MAX_NUM_RECIPES);
2117		bitmap_zero(r_bitmap, ICE_MAX_NUM_RECIPES);
2118		if (ice_aq_get_recipe_to_profile(hw, i, (u8 *)r_bitmap, NULL))
2119			continue;
 
2120		bitmap_copy(profile_to_recipe[i], r_bitmap,
2121			    ICE_MAX_NUM_RECIPES);
2122		for_each_set_bit(j, r_bitmap, ICE_MAX_NUM_RECIPES)
2123			set_bit(i, recipe_to_profile[j]);
2124	}
2125}
2126
2127/**
2128 * ice_collect_result_idx - copy result index values
2129 * @buf: buffer that contains the result index
2130 * @recp: the recipe struct to copy data into
2131 */
2132static void
2133ice_collect_result_idx(struct ice_aqc_recipe_data_elem *buf,
2134		       struct ice_sw_recipe *recp)
2135{
2136	if (buf->content.result_indx & ICE_AQ_RECIPE_RESULT_EN)
2137		set_bit(buf->content.result_indx & ~ICE_AQ_RECIPE_RESULT_EN,
2138			recp->res_idxs);
2139}
2140
2141/**
2142 * ice_get_recp_frm_fw - update SW bookkeeping from FW recipe entries
2143 * @hw: pointer to hardware structure
2144 * @recps: struct that we need to populate
2145 * @rid: recipe ID that we are populating
2146 * @refresh_required: true if we should get recipe to profile mapping from FW
 
2147 *
2148 * This function is used to populate all the necessary entries into our
2149 * bookkeeping so that we have a current list of all the recipes that are
2150 * programmed in the firmware.
2151 */
2152static int
2153ice_get_recp_frm_fw(struct ice_hw *hw, struct ice_sw_recipe *recps, u8 rid,
2154		    bool *refresh_required)
2155{
2156	DECLARE_BITMAP(result_bm, ICE_MAX_FV_WORDS);
2157	struct ice_aqc_recipe_data_elem *tmp;
2158	u16 num_recps = ICE_MAX_NUM_RECIPES;
2159	struct ice_prot_lkup_ext *lkup_exts;
2160	u8 fv_word_idx = 0;
2161	u16 sub_recps;
2162	int status;
2163
2164	bitmap_zero(result_bm, ICE_MAX_FV_WORDS);
2165
2166	/* we need a buffer big enough to accommodate all the recipes */
2167	tmp = kcalloc(ICE_MAX_NUM_RECIPES, sizeof(*tmp), GFP_KERNEL);
2168	if (!tmp)
2169		return -ENOMEM;
2170
2171	tmp[0].recipe_indx = rid;
2172	status = ice_aq_get_recipe(hw, tmp, &num_recps, rid, NULL);
2173	/* non-zero status meaning recipe doesn't exist */
2174	if (status)
2175		goto err_unroll;
2176
2177	/* Get recipe to profile map so that we can get the fv from lkups that
2178	 * we read for a recipe from FW. Since we want to minimize the number of
2179	 * times we make this FW call, just make one call and cache the copy
2180	 * until a new recipe is added. This operation is only required the
2181	 * first time to get the changes from FW. Then to search existing
2182	 * entries we don't need to update the cache again until another recipe
2183	 * gets added.
2184	 */
2185	if (*refresh_required) {
2186		ice_get_recp_to_prof_map(hw);
2187		*refresh_required = false;
2188	}
2189
2190	/* Start populating all the entries for recps[rid] based on lkups from
2191	 * firmware. Note that we are only creating the root recipe in our
2192	 * database.
2193	 */
2194	lkup_exts = &recps[rid].lkup_exts;
2195
2196	for (sub_recps = 0; sub_recps < num_recps; sub_recps++) {
2197		struct ice_aqc_recipe_data_elem root_bufs = tmp[sub_recps];
2198		struct ice_recp_grp_entry *rg_entry;
2199		u8 i, prof, idx, prot = 0;
2200		bool is_root;
2201		u16 off = 0;
2202
2203		rg_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*rg_entry),
2204					GFP_KERNEL);
2205		if (!rg_entry) {
2206			status = -ENOMEM;
2207			goto err_unroll;
2208		}
2209
2210		idx = root_bufs.recipe_indx;
2211		is_root = root_bufs.content.rid & ICE_AQ_RECIPE_ID_IS_ROOT;
2212
2213		/* Mark all result indices in this chain */
2214		if (root_bufs.content.result_indx & ICE_AQ_RECIPE_RESULT_EN)
2215			set_bit(root_bufs.content.result_indx & ~ICE_AQ_RECIPE_RESULT_EN,
2216				result_bm);
2217
2218		/* get the first profile that is associated with rid */
2219		prof = find_first_bit(recipe_to_profile[idx],
2220				      ICE_MAX_NUM_PROFILES);
2221		for (i = 0; i < ICE_NUM_WORDS_RECIPE; i++) {
2222			u8 lkup_indx = root_bufs.content.lkup_indx[i + 1];
2223
2224			rg_entry->fv_idx[i] = lkup_indx;
2225			rg_entry->fv_mask[i] =
2226				le16_to_cpu(root_bufs.content.mask[i + 1]);
2227
2228			/* If the recipe is a chained recipe then all its
2229			 * child recipe's result will have a result index.
2230			 * To fill fv_words we should not use those result
2231			 * index, we only need the protocol ids and offsets.
2232			 * We will skip all the fv_idx which stores result
2233			 * index in them. We also need to skip any fv_idx which
2234			 * has ICE_AQ_RECIPE_LKUP_IGNORE or 0 since it isn't a
2235			 * valid offset value.
2236			 */
2237			if (test_bit(rg_entry->fv_idx[i], hw->switch_info->prof_res_bm[prof]) ||
2238			    rg_entry->fv_idx[i] & ICE_AQ_RECIPE_LKUP_IGNORE ||
2239			    rg_entry->fv_idx[i] == 0)
 
2240				continue;
2241
2242			ice_find_prot_off(hw, ICE_BLK_SW, prof,
2243					  rg_entry->fv_idx[i], &prot, &off);
2244			lkup_exts->fv_words[fv_word_idx].prot_id = prot;
2245			lkup_exts->fv_words[fv_word_idx].off = off;
2246			lkup_exts->field_mask[fv_word_idx] =
2247				rg_entry->fv_mask[i];
2248			fv_word_idx++;
2249		}
2250		/* populate rg_list with the data from the child entry of this
2251		 * recipe
2252		 */
2253		list_add(&rg_entry->l_entry, &recps[rid].rg_list);
2254
2255		/* Propagate some data to the recipe database */
2256		recps[idx].is_root = !!is_root;
2257		recps[idx].priority = root_bufs.content.act_ctrl_fwd_priority;
2258		recps[idx].need_pass_l2 = root_bufs.content.act_ctrl &
2259					  ICE_AQ_RECIPE_ACT_NEED_PASS_L2;
2260		recps[idx].allow_pass_l2 = root_bufs.content.act_ctrl &
2261					   ICE_AQ_RECIPE_ACT_ALLOW_PASS_L2;
2262		bitmap_zero(recps[idx].res_idxs, ICE_MAX_FV_WORDS);
2263		if (root_bufs.content.result_indx & ICE_AQ_RECIPE_RESULT_EN) {
2264			recps[idx].chain_idx = root_bufs.content.result_indx &
2265				~ICE_AQ_RECIPE_RESULT_EN;
2266			set_bit(recps[idx].chain_idx, recps[idx].res_idxs);
2267		} else {
2268			recps[idx].chain_idx = ICE_INVAL_CHAIN_IND;
2269		}
2270
2271		if (!is_root)
 
 
 
2272			continue;
 
2273
2274		/* Only do the following for root recipes entries */
2275		memcpy(recps[idx].r_bitmap, root_bufs.recipe_bitmap,
2276		       sizeof(recps[idx].r_bitmap));
2277		recps[idx].root_rid = root_bufs.content.rid &
2278			~ICE_AQ_RECIPE_ID_IS_ROOT;
2279		recps[idx].priority = root_bufs.content.act_ctrl_fwd_priority;
2280	}
2281
2282	/* Complete initialization of the root recipe entry */
2283	lkup_exts->n_val_words = fv_word_idx;
2284	recps[rid].big_recp = (num_recps > 1);
2285	recps[rid].n_grp_count = (u8)num_recps;
2286	recps[rid].root_buf = devm_kmemdup(ice_hw_to_dev(hw), tmp,
2287					   recps[rid].n_grp_count * sizeof(*recps[rid].root_buf),
2288					   GFP_KERNEL);
2289	if (!recps[rid].root_buf) {
2290		status = -ENOMEM;
2291		goto err_unroll;
2292	}
2293
2294	/* Copy result indexes */
2295	bitmap_copy(recps[rid].res_idxs, result_bm, ICE_MAX_FV_WORDS);
2296	recps[rid].recp_created = true;
 
2297
2298err_unroll:
2299	kfree(tmp);
2300	return status;
2301}
2302
2303/* ice_init_port_info - Initialize port_info with switch configuration data
2304 * @pi: pointer to port_info
2305 * @vsi_port_num: VSI number or port number
2306 * @type: Type of switch element (port or VSI)
2307 * @swid: switch ID of the switch the element is attached to
2308 * @pf_vf_num: PF or VF number
2309 * @is_vf: true if the element is a VF, false otherwise
2310 */
2311static void
2312ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
2313		   u16 swid, u16 pf_vf_num, bool is_vf)
2314{
2315	switch (type) {
2316	case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
2317		pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
2318		pi->sw_id = swid;
2319		pi->pf_vf_num = pf_vf_num;
2320		pi->is_vf = is_vf;
2321		break;
2322	default:
2323		ice_debug(pi->hw, ICE_DBG_SW, "incorrect VSI/port type received\n");
2324		break;
2325	}
2326}
2327
2328/* ice_get_initial_sw_cfg - Get initial port and default VSI data
2329 * @hw: pointer to the hardware structure
2330 */
2331int ice_get_initial_sw_cfg(struct ice_hw *hw)
2332{
2333	struct ice_aqc_get_sw_cfg_resp_elem *rbuf;
2334	u16 req_desc = 0;
2335	u16 num_elems;
2336	int status;
2337	u16 i;
2338
2339	rbuf = kzalloc(ICE_SW_CFG_MAX_BUF_LEN, GFP_KERNEL);
2340	if (!rbuf)
2341		return -ENOMEM;
2342
2343	/* Multiple calls to ice_aq_get_sw_cfg may be required
2344	 * to get all the switch configuration information. The need
2345	 * for additional calls is indicated by ice_aq_get_sw_cfg
2346	 * writing a non-zero value in req_desc
2347	 */
2348	do {
2349		struct ice_aqc_get_sw_cfg_resp_elem *ele;
2350
2351		status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
2352					   &req_desc, &num_elems, NULL);
2353
2354		if (status)
2355			break;
2356
2357		for (i = 0, ele = rbuf; i < num_elems; i++, ele++) {
2358			u16 pf_vf_num, swid, vsi_port_num;
2359			bool is_vf = false;
2360			u8 res_type;
2361
2362			vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
2363				ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
2364
2365			pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
2366				ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
2367
2368			swid = le16_to_cpu(ele->swid);
2369
2370			if (le16_to_cpu(ele->pf_vf_num) &
2371			    ICE_AQC_GET_SW_CONF_RESP_IS_VF)
2372				is_vf = true;
2373
2374			res_type = (u8)(le16_to_cpu(ele->vsi_port_num) >>
2375					ICE_AQC_GET_SW_CONF_RESP_TYPE_S);
2376
2377			if (res_type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
2378				/* FW VSI is not needed. Just continue. */
2379				continue;
2380			}
2381
2382			ice_init_port_info(hw->port_info, vsi_port_num,
2383					   res_type, swid, pf_vf_num, is_vf);
2384		}
2385	} while (req_desc && !status);
2386
2387	kfree(rbuf);
2388	return status;
2389}
2390
2391/**
2392 * ice_fill_sw_info - Helper function to populate lb_en and lan_en
2393 * @hw: pointer to the hardware structure
2394 * @fi: filter info structure to fill/update
2395 *
2396 * This helper function populates the lb_en and lan_en elements of the provided
2397 * ice_fltr_info struct using the switch's type and characteristics of the
2398 * switch rule being configured.
2399 */
2400static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
2401{
2402	fi->lb_en = false;
2403	fi->lan_en = false;
2404	if ((fi->flag & ICE_FLTR_TX) &&
2405	    (fi->fltr_act == ICE_FWD_TO_VSI ||
2406	     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
2407	     fi->fltr_act == ICE_FWD_TO_Q ||
2408	     fi->fltr_act == ICE_FWD_TO_QGRP)) {
2409		/* Setting LB for prune actions will result in replicated
2410		 * packets to the internal switch that will be dropped.
2411		 */
2412		if (fi->lkup_type != ICE_SW_LKUP_VLAN)
2413			fi->lb_en = true;
2414
2415		/* Set lan_en to TRUE if
2416		 * 1. The switch is a VEB AND
2417		 * 2
2418		 * 2.1 The lookup is a directional lookup like ethertype,
2419		 * promiscuous, ethertype-MAC, promiscuous-VLAN
2420		 * and default-port OR
2421		 * 2.2 The lookup is VLAN, OR
2422		 * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
2423		 * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
2424		 *
2425		 * OR
2426		 *
2427		 * The switch is a VEPA.
2428		 *
2429		 * In all other cases, the LAN enable has to be set to false.
2430		 */
2431		if (hw->evb_veb) {
2432			if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
2433			    fi->lkup_type == ICE_SW_LKUP_PROMISC ||
2434			    fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
2435			    fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
2436			    fi->lkup_type == ICE_SW_LKUP_DFLT ||
2437			    fi->lkup_type == ICE_SW_LKUP_VLAN ||
2438			    (fi->lkup_type == ICE_SW_LKUP_MAC &&
2439			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)) ||
2440			    (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
2441			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)))
2442				fi->lan_en = true;
2443		} else {
2444			fi->lan_en = true;
2445		}
2446	}
 
 
 
2447}
2448
2449/**
2450 * ice_fill_eth_hdr - helper to copy dummy_eth_hdr into supplied buffer
2451 * @eth_hdr: pointer to buffer to populate
2452 */
2453void ice_fill_eth_hdr(u8 *eth_hdr)
2454{
2455	memcpy(eth_hdr, dummy_eth_header, DUMMY_ETH_HDR_LEN);
2456}
2457
2458/**
2459 * ice_fill_sw_rule - Helper function to fill switch rule structure
2460 * @hw: pointer to the hardware structure
2461 * @f_info: entry containing packet forwarding information
2462 * @s_rule: switch rule structure to be filled in based on mac_entry
2463 * @opc: switch rules population command type - pass in the command opcode
2464 */
2465static void
2466ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
2467		 struct ice_sw_rule_lkup_rx_tx *s_rule,
2468		 enum ice_adminq_opc opc)
2469{
2470	u16 vlan_id = ICE_MAX_VLAN_ID + 1;
2471	u16 vlan_tpid = ETH_P_8021Q;
2472	void *daddr = NULL;
2473	u16 eth_hdr_sz;
2474	u8 *eth_hdr;
2475	u32 act = 0;
2476	__be16 *off;
2477	u8 q_rgn;
2478
2479	if (opc == ice_aqc_opc_remove_sw_rules) {
2480		s_rule->act = 0;
2481		s_rule->index = cpu_to_le16(f_info->fltr_rule_id);
2482		s_rule->hdr_len = 0;
2483		return;
2484	}
2485
2486	eth_hdr_sz = sizeof(dummy_eth_header);
2487	eth_hdr = s_rule->hdr_data;
2488
2489	/* initialize the ether header with a dummy header */
2490	memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz);
2491	ice_fill_sw_info(hw, f_info);
2492
2493	switch (f_info->fltr_act) {
2494	case ICE_FWD_TO_VSI:
2495		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
2496				  f_info->fwd_id.hw_vsi_id);
2497		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
2498			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
2499				ICE_SINGLE_ACT_VALID_BIT;
2500		break;
2501	case ICE_FWD_TO_VSI_LIST:
2502		act |= ICE_SINGLE_ACT_VSI_LIST;
2503		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_LIST_ID_M,
2504				  f_info->fwd_id.vsi_list_id);
2505		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
2506			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
2507				ICE_SINGLE_ACT_VALID_BIT;
2508		break;
2509	case ICE_FWD_TO_Q:
2510		act |= ICE_SINGLE_ACT_TO_Q;
2511		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
2512				  f_info->fwd_id.q_id);
2513		break;
2514	case ICE_DROP_PACKET:
2515		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
2516			ICE_SINGLE_ACT_VALID_BIT;
2517		break;
2518	case ICE_FWD_TO_QGRP:
2519		q_rgn = f_info->qgrp_size > 0 ?
2520			(u8)ilog2(f_info->qgrp_size) : 0;
2521		act |= ICE_SINGLE_ACT_TO_Q;
2522		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
2523				  f_info->fwd_id.q_id);
2524		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_REGION_M, q_rgn);
2525		break;
2526	default:
2527		return;
2528	}
2529
2530	if (f_info->lb_en)
2531		act |= ICE_SINGLE_ACT_LB_ENABLE;
2532	if (f_info->lan_en)
2533		act |= ICE_SINGLE_ACT_LAN_ENABLE;
2534
2535	switch (f_info->lkup_type) {
2536	case ICE_SW_LKUP_MAC:
2537		daddr = f_info->l_data.mac.mac_addr;
2538		break;
2539	case ICE_SW_LKUP_VLAN:
2540		vlan_id = f_info->l_data.vlan.vlan_id;
2541		if (f_info->l_data.vlan.tpid_valid)
2542			vlan_tpid = f_info->l_data.vlan.tpid;
2543		if (f_info->fltr_act == ICE_FWD_TO_VSI ||
2544		    f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
2545			act |= ICE_SINGLE_ACT_PRUNE;
2546			act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
2547		}
2548		break;
2549	case ICE_SW_LKUP_ETHERTYPE_MAC:
2550		daddr = f_info->l_data.ethertype_mac.mac_addr;
2551		fallthrough;
2552	case ICE_SW_LKUP_ETHERTYPE:
2553		off = (__force __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
2554		*off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
2555		break;
2556	case ICE_SW_LKUP_MAC_VLAN:
2557		daddr = f_info->l_data.mac_vlan.mac_addr;
2558		vlan_id = f_info->l_data.mac_vlan.vlan_id;
2559		break;
2560	case ICE_SW_LKUP_PROMISC_VLAN:
2561		vlan_id = f_info->l_data.mac_vlan.vlan_id;
2562		fallthrough;
2563	case ICE_SW_LKUP_PROMISC:
2564		daddr = f_info->l_data.mac_vlan.mac_addr;
2565		break;
2566	default:
2567		break;
2568	}
2569
2570	s_rule->hdr.type = (f_info->flag & ICE_FLTR_RX) ?
2571		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
2572		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
2573
2574	/* Recipe set depending on lookup type */
2575	s_rule->recipe_id = cpu_to_le16(f_info->lkup_type);
2576	s_rule->src = cpu_to_le16(f_info->src);
2577	s_rule->act = cpu_to_le32(act);
2578
2579	if (daddr)
2580		ether_addr_copy(eth_hdr + ICE_ETH_DA_OFFSET, daddr);
2581
2582	if (!(vlan_id > ICE_MAX_VLAN_ID)) {
2583		off = (__force __be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
2584		*off = cpu_to_be16(vlan_id);
2585		off = (__force __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
2586		*off = cpu_to_be16(vlan_tpid);
2587	}
2588
2589	/* Create the switch rule with the final dummy Ethernet header */
2590	if (opc != ice_aqc_opc_update_sw_rules)
2591		s_rule->hdr_len = cpu_to_le16(eth_hdr_sz);
2592}
2593
2594/**
2595 * ice_add_marker_act
2596 * @hw: pointer to the hardware structure
2597 * @m_ent: the management entry for which sw marker needs to be added
2598 * @sw_marker: sw marker to tag the Rx descriptor with
2599 * @l_id: large action resource ID
2600 *
2601 * Create a large action to hold software marker and update the switch rule
2602 * entry pointed by m_ent with newly created large action
2603 */
2604static int
2605ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
2606		   u16 sw_marker, u16 l_id)
2607{
2608	struct ice_sw_rule_lkup_rx_tx *rx_tx;
2609	struct ice_sw_rule_lg_act *lg_act;
2610	/* For software marker we need 3 large actions
2611	 * 1. FWD action: FWD TO VSI or VSI LIST
2612	 * 2. GENERIC VALUE action to hold the profile ID
2613	 * 3. GENERIC VALUE action to hold the software marker ID
2614	 */
2615	const u16 num_lg_acts = 3;
2616	u16 lg_act_size;
2617	u16 rules_size;
2618	int status;
2619	u32 act;
2620	u16 id;
2621
2622	if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
2623		return -EINVAL;
2624
2625	/* Create two back-to-back switch rules and submit them to the HW using
2626	 * one memory buffer:
2627	 *    1. Large Action
2628	 *    2. Look up Tx Rx
2629	 */
2630	lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(lg_act, num_lg_acts);
2631	rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(rx_tx);
2632	lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
2633	if (!lg_act)
2634		return -ENOMEM;
2635
2636	rx_tx = (typeof(rx_tx))((u8 *)lg_act + lg_act_size);
2637
2638	/* Fill in the first switch rule i.e. large action */
2639	lg_act->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
2640	lg_act->index = cpu_to_le16(l_id);
2641	lg_act->size = cpu_to_le16(num_lg_acts);
2642
2643	/* First action VSI forwarding or VSI list forwarding depending on how
2644	 * many VSIs
2645	 */
2646	id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
2647		m_ent->fltr_info.fwd_id.hw_vsi_id;
2648
2649	act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
2650	act |= FIELD_PREP(ICE_LG_ACT_VSI_LIST_ID_M, id);
2651	if (m_ent->vsi_count > 1)
2652		act |= ICE_LG_ACT_VSI_LIST;
2653	lg_act->act[0] = cpu_to_le32(act);
2654
2655	/* Second action descriptor type */
2656	act = ICE_LG_ACT_GENERIC;
2657
2658	act |= FIELD_PREP(ICE_LG_ACT_GENERIC_VALUE_M, 1);
2659	lg_act->act[1] = cpu_to_le32(act);
2660
2661	act = FIELD_PREP(ICE_LG_ACT_GENERIC_OFFSET_M,
2662			 ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX);
2663
2664	/* Third action Marker value */
2665	act |= ICE_LG_ACT_GENERIC;
2666	act |= FIELD_PREP(ICE_LG_ACT_GENERIC_VALUE_M, sw_marker);
2667
2668	lg_act->act[2] = cpu_to_le32(act);
2669
2670	/* call the fill switch rule to fill the lookup Tx Rx structure */
2671	ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
2672			 ice_aqc_opc_update_sw_rules);
2673
2674	/* Update the action to point to the large action ID */
2675	act = ICE_SINGLE_ACT_PTR;
2676	act |= FIELD_PREP(ICE_SINGLE_ACT_PTR_VAL_M, l_id);
2677	rx_tx->act = cpu_to_le32(act);
2678
2679	/* Use the filter rule ID of the previously created rule with single
2680	 * act. Once the update happens, hardware will treat this as large
2681	 * action
2682	 */
2683	rx_tx->index = cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
2684
2685	status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
2686				 ice_aqc_opc_update_sw_rules, NULL);
2687	if (!status) {
2688		m_ent->lg_act_idx = l_id;
2689		m_ent->sw_marker_id = sw_marker;
2690	}
2691
2692	devm_kfree(ice_hw_to_dev(hw), lg_act);
2693	return status;
2694}
2695
2696/**
2697 * ice_create_vsi_list_map
2698 * @hw: pointer to the hardware structure
2699 * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
2700 * @num_vsi: number of VSI handles in the array
2701 * @vsi_list_id: VSI list ID generated as part of allocate resource
2702 *
2703 * Helper function to create a new entry of VSI list ID to VSI mapping
2704 * using the given VSI list ID
2705 */
2706static struct ice_vsi_list_map_info *
2707ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
2708			u16 vsi_list_id)
2709{
2710	struct ice_switch_info *sw = hw->switch_info;
2711	struct ice_vsi_list_map_info *v_map;
2712	int i;
2713
2714	v_map = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*v_map), GFP_KERNEL);
2715	if (!v_map)
2716		return NULL;
2717
2718	v_map->vsi_list_id = vsi_list_id;
2719	v_map->ref_cnt = 1;
2720	for (i = 0; i < num_vsi; i++)
2721		set_bit(vsi_handle_arr[i], v_map->vsi_map);
2722
2723	list_add(&v_map->list_entry, &sw->vsi_list_map_head);
2724	return v_map;
2725}
2726
2727/**
2728 * ice_update_vsi_list_rule
2729 * @hw: pointer to the hardware structure
2730 * @vsi_handle_arr: array of VSI handles to form a VSI list
2731 * @num_vsi: number of VSI handles in the array
2732 * @vsi_list_id: VSI list ID generated as part of allocate resource
2733 * @remove: Boolean value to indicate if this is a remove action
2734 * @opc: switch rules population command type - pass in the command opcode
2735 * @lkup_type: lookup type of the filter
2736 *
2737 * Call AQ command to add a new switch rule or update existing switch rule
2738 * using the given VSI list ID
2739 */
2740static int
2741ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
2742			 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
2743			 enum ice_sw_lkup_type lkup_type)
2744{
2745	struct ice_sw_rule_vsi_list *s_rule;
2746	u16 s_rule_size;
2747	u16 rule_type;
2748	int status;
2749	int i;
2750
2751	if (!num_vsi)
2752		return -EINVAL;
2753
2754	if (lkup_type == ICE_SW_LKUP_MAC ||
2755	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
2756	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
2757	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
2758	    lkup_type == ICE_SW_LKUP_PROMISC ||
2759	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
2760	    lkup_type == ICE_SW_LKUP_DFLT)
 
2761		rule_type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
2762			ICE_AQC_SW_RULES_T_VSI_LIST_SET;
2763	else if (lkup_type == ICE_SW_LKUP_VLAN)
2764		rule_type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
2765			ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
2766	else
2767		return -EINVAL;
2768
2769	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, num_vsi);
2770	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
2771	if (!s_rule)
2772		return -ENOMEM;
2773	for (i = 0; i < num_vsi; i++) {
2774		if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
2775			status = -EINVAL;
2776			goto exit;
2777		}
2778		/* AQ call requires hw_vsi_id(s) */
2779		s_rule->vsi[i] =
2780			cpu_to_le16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
2781	}
2782
2783	s_rule->hdr.type = cpu_to_le16(rule_type);
2784	s_rule->number_vsi = cpu_to_le16(num_vsi);
2785	s_rule->index = cpu_to_le16(vsi_list_id);
2786
2787	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
2788
2789exit:
2790	devm_kfree(ice_hw_to_dev(hw), s_rule);
2791	return status;
2792}
2793
2794/**
2795 * ice_create_vsi_list_rule - Creates and populates a VSI list rule
2796 * @hw: pointer to the HW struct
2797 * @vsi_handle_arr: array of VSI handles to form a VSI list
2798 * @num_vsi: number of VSI handles in the array
2799 * @vsi_list_id: stores the ID of the VSI list to be created
2800 * @lkup_type: switch rule filter's lookup type
2801 */
2802static int
2803ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
2804			 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
2805{
2806	int status;
2807
2808	status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
2809					    ice_aqc_opc_alloc_res);
2810	if (status)
2811		return status;
2812
2813	/* Update the newly created VSI list to include the specified VSIs */
2814	return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
2815					*vsi_list_id, false,
2816					ice_aqc_opc_add_sw_rules, lkup_type);
2817}
2818
2819/**
2820 * ice_create_pkt_fwd_rule
2821 * @hw: pointer to the hardware structure
2822 * @f_entry: entry containing packet forwarding information
2823 *
2824 * Create switch rule with given filter information and add an entry
2825 * to the corresponding filter management list to track this switch rule
2826 * and VSI mapping
2827 */
2828static int
2829ice_create_pkt_fwd_rule(struct ice_hw *hw,
2830			struct ice_fltr_list_entry *f_entry)
2831{
2832	struct ice_fltr_mgmt_list_entry *fm_entry;
2833	struct ice_sw_rule_lkup_rx_tx *s_rule;
2834	enum ice_sw_lkup_type l_type;
2835	struct ice_sw_recipe *recp;
2836	int status;
2837
2838	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
2839			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule),
2840			      GFP_KERNEL);
2841	if (!s_rule)
2842		return -ENOMEM;
2843	fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
2844				GFP_KERNEL);
2845	if (!fm_entry) {
2846		status = -ENOMEM;
2847		goto ice_create_pkt_fwd_rule_exit;
2848	}
2849
2850	fm_entry->fltr_info = f_entry->fltr_info;
2851
2852	/* Initialize all the fields for the management entry */
2853	fm_entry->vsi_count = 1;
2854	fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
2855	fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
2856	fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
2857
2858	ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
2859			 ice_aqc_opc_add_sw_rules);
2860
2861	status = ice_aq_sw_rules(hw, s_rule,
2862				 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule), 1,
2863				 ice_aqc_opc_add_sw_rules, NULL);
2864	if (status) {
2865		devm_kfree(ice_hw_to_dev(hw), fm_entry);
2866		goto ice_create_pkt_fwd_rule_exit;
2867	}
2868
2869	f_entry->fltr_info.fltr_rule_id = le16_to_cpu(s_rule->index);
2870	fm_entry->fltr_info.fltr_rule_id = le16_to_cpu(s_rule->index);
2871
2872	/* The book keeping entries will get removed when base driver
2873	 * calls remove filter AQ command
2874	 */
2875	l_type = fm_entry->fltr_info.lkup_type;
2876	recp = &hw->switch_info->recp_list[l_type];
2877	list_add(&fm_entry->list_entry, &recp->filt_rules);
2878
2879ice_create_pkt_fwd_rule_exit:
2880	devm_kfree(ice_hw_to_dev(hw), s_rule);
2881	return status;
2882}
2883
2884/**
2885 * ice_update_pkt_fwd_rule
2886 * @hw: pointer to the hardware structure
2887 * @f_info: filter information for switch rule
2888 *
2889 * Call AQ command to update a previously created switch rule with a
2890 * VSI list ID
2891 */
2892static int
2893ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
2894{
2895	struct ice_sw_rule_lkup_rx_tx *s_rule;
2896	int status;
2897
2898	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
2899			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule),
2900			      GFP_KERNEL);
2901	if (!s_rule)
2902		return -ENOMEM;
2903
2904	ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
2905
2906	s_rule->index = cpu_to_le16(f_info->fltr_rule_id);
2907
2908	/* Update switch rule with new rule set to forward VSI list */
2909	status = ice_aq_sw_rules(hw, s_rule,
2910				 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule), 1,
2911				 ice_aqc_opc_update_sw_rules, NULL);
2912
2913	devm_kfree(ice_hw_to_dev(hw), s_rule);
2914	return status;
2915}
2916
2917/**
2918 * ice_update_sw_rule_bridge_mode
2919 * @hw: pointer to the HW struct
2920 *
2921 * Updates unicast switch filter rules based on VEB/VEPA mode
2922 */
2923int ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
2924{
2925	struct ice_switch_info *sw = hw->switch_info;
2926	struct ice_fltr_mgmt_list_entry *fm_entry;
2927	struct list_head *rule_head;
2928	struct mutex *rule_lock; /* Lock to protect filter rule list */
2929	int status = 0;
2930
2931	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2932	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2933
2934	mutex_lock(rule_lock);
2935	list_for_each_entry(fm_entry, rule_head, list_entry) {
2936		struct ice_fltr_info *fi = &fm_entry->fltr_info;
2937		u8 *addr = fi->l_data.mac.mac_addr;
2938
2939		/* Update unicast Tx rules to reflect the selected
2940		 * VEB/VEPA mode
2941		 */
2942		if ((fi->flag & ICE_FLTR_TX) && is_unicast_ether_addr(addr) &&
2943		    (fi->fltr_act == ICE_FWD_TO_VSI ||
2944		     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
2945		     fi->fltr_act == ICE_FWD_TO_Q ||
2946		     fi->fltr_act == ICE_FWD_TO_QGRP)) {
2947			status = ice_update_pkt_fwd_rule(hw, fi);
2948			if (status)
2949				break;
2950		}
2951	}
2952
2953	mutex_unlock(rule_lock);
2954
2955	return status;
2956}
2957
2958/**
2959 * ice_add_update_vsi_list
2960 * @hw: pointer to the hardware structure
2961 * @m_entry: pointer to current filter management list entry
2962 * @cur_fltr: filter information from the book keeping entry
2963 * @new_fltr: filter information with the new VSI to be added
2964 *
2965 * Call AQ command to add or update previously created VSI list with new VSI.
2966 *
2967 * Helper function to do book keeping associated with adding filter information
2968 * The algorithm to do the book keeping is described below :
2969 * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
2970 *	if only one VSI has been added till now
2971 *		Allocate a new VSI list and add two VSIs
2972 *		to this list using switch rule command
2973 *		Update the previously created switch rule with the
2974 *		newly created VSI list ID
2975 *	if a VSI list was previously created
2976 *		Add the new VSI to the previously created VSI list set
2977 *		using the update switch rule command
2978 */
2979static int
2980ice_add_update_vsi_list(struct ice_hw *hw,
2981			struct ice_fltr_mgmt_list_entry *m_entry,
2982			struct ice_fltr_info *cur_fltr,
2983			struct ice_fltr_info *new_fltr)
2984{
2985	u16 vsi_list_id = 0;
2986	int status = 0;
2987
2988	if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
2989	     cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
2990		return -EOPNOTSUPP;
2991
2992	if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
2993	     new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
2994	    (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
2995	     cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
2996		return -EOPNOTSUPP;
2997
2998	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
2999		/* Only one entry existed in the mapping and it was not already
3000		 * a part of a VSI list. So, create a VSI list with the old and
3001		 * new VSIs.
3002		 */
3003		struct ice_fltr_info tmp_fltr;
3004		u16 vsi_handle_arr[2];
3005
3006		/* A rule already exists with the new VSI being added */
3007		if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
3008			return -EEXIST;
3009
3010		vsi_handle_arr[0] = cur_fltr->vsi_handle;
3011		vsi_handle_arr[1] = new_fltr->vsi_handle;
3012		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
3013						  &vsi_list_id,
3014						  new_fltr->lkup_type);
3015		if (status)
3016			return status;
3017
3018		tmp_fltr = *new_fltr;
3019		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
3020		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
3021		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
3022		/* Update the previous switch rule of "MAC forward to VSI" to
3023		 * "MAC fwd to VSI list"
3024		 */
3025		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
3026		if (status)
3027			return status;
3028
3029		cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
3030		cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
3031		m_entry->vsi_list_info =
3032			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
3033						vsi_list_id);
3034
3035		if (!m_entry->vsi_list_info)
3036			return -ENOMEM;
3037
3038		/* If this entry was large action then the large action needs
3039		 * to be updated to point to FWD to VSI list
3040		 */
3041		if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
3042			status =
3043			    ice_add_marker_act(hw, m_entry,
3044					       m_entry->sw_marker_id,
3045					       m_entry->lg_act_idx);
3046	} else {
3047		u16 vsi_handle = new_fltr->vsi_handle;
3048		enum ice_adminq_opc opcode;
3049
3050		if (!m_entry->vsi_list_info)
3051			return -EIO;
3052
3053		/* A rule already exists with the new VSI being added */
3054		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
3055			return 0;
3056
3057		/* Update the previously created VSI list set with
3058		 * the new VSI ID passed in
3059		 */
3060		vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
3061		opcode = ice_aqc_opc_update_sw_rules;
3062
3063		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
3064						  vsi_list_id, false, opcode,
3065						  new_fltr->lkup_type);
3066		/* update VSI list mapping info with new VSI ID */
3067		if (!status)
3068			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
3069	}
3070	if (!status)
3071		m_entry->vsi_count++;
3072	return status;
3073}
3074
3075/**
3076 * ice_find_rule_entry - Search a rule entry
3077 * @hw: pointer to the hardware structure
3078 * @recp_id: lookup type for which the specified rule needs to be searched
3079 * @f_info: rule information
3080 *
3081 * Helper function to search for a given rule entry
3082 * Returns pointer to entry storing the rule if found
3083 */
3084static struct ice_fltr_mgmt_list_entry *
3085ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
3086{
3087	struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
3088	struct ice_switch_info *sw = hw->switch_info;
3089	struct list_head *list_head;
3090
3091	list_head = &sw->recp_list[recp_id].filt_rules;
3092	list_for_each_entry(list_itr, list_head, list_entry) {
3093		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
3094			    sizeof(f_info->l_data)) &&
3095		    f_info->flag == list_itr->fltr_info.flag) {
3096			ret = list_itr;
3097			break;
3098		}
3099	}
3100	return ret;
3101}
3102
3103/**
3104 * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
3105 * @hw: pointer to the hardware structure
3106 * @recp_id: lookup type for which VSI lists needs to be searched
3107 * @vsi_handle: VSI handle to be found in VSI list
3108 * @vsi_list_id: VSI list ID found containing vsi_handle
3109 *
3110 * Helper function to search a VSI list with single entry containing given VSI
3111 * handle element. This can be extended further to search VSI list with more
3112 * than 1 vsi_count. Returns pointer to VSI list entry if found.
3113 */
3114struct ice_vsi_list_map_info *
3115ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
3116			u16 *vsi_list_id)
3117{
3118	struct ice_vsi_list_map_info *map_info = NULL;
3119	struct ice_switch_info *sw = hw->switch_info;
3120	struct ice_fltr_mgmt_list_entry *list_itr;
3121	struct list_head *list_head;
3122
3123	list_head = &sw->recp_list[recp_id].filt_rules;
3124	list_for_each_entry(list_itr, list_head, list_entry) {
3125		if (list_itr->vsi_list_info) {
3126			map_info = list_itr->vsi_list_info;
3127			if (test_bit(vsi_handle, map_info->vsi_map)) {
3128				*vsi_list_id = map_info->vsi_list_id;
3129				return map_info;
3130			}
3131		}
3132	}
3133	return NULL;
3134}
3135
3136/**
3137 * ice_add_rule_internal - add rule for a given lookup type
3138 * @hw: pointer to the hardware structure
3139 * @recp_id: lookup type (recipe ID) for which rule has to be added
3140 * @f_entry: structure containing MAC forwarding information
3141 *
3142 * Adds or updates the rule lists for a given recipe
3143 */
3144static int
3145ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
3146		      struct ice_fltr_list_entry *f_entry)
3147{
3148	struct ice_switch_info *sw = hw->switch_info;
3149	struct ice_fltr_info *new_fltr, *cur_fltr;
3150	struct ice_fltr_mgmt_list_entry *m_entry;
3151	struct mutex *rule_lock; /* Lock to protect filter rule list */
3152	int status = 0;
3153
3154	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
3155		return -EINVAL;
3156	f_entry->fltr_info.fwd_id.hw_vsi_id =
3157		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
3158
3159	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
3160
3161	mutex_lock(rule_lock);
3162	new_fltr = &f_entry->fltr_info;
3163	if (new_fltr->flag & ICE_FLTR_RX)
3164		new_fltr->src = hw->port_info->lport;
3165	else if (new_fltr->flag & ICE_FLTR_TX)
3166		new_fltr->src = f_entry->fltr_info.fwd_id.hw_vsi_id;
3167
3168	m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
3169	if (!m_entry) {
3170		mutex_unlock(rule_lock);
3171		return ice_create_pkt_fwd_rule(hw, f_entry);
3172	}
3173
3174	cur_fltr = &m_entry->fltr_info;
3175	status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
3176	mutex_unlock(rule_lock);
3177
3178	return status;
3179}
3180
3181/**
3182 * ice_remove_vsi_list_rule
3183 * @hw: pointer to the hardware structure
3184 * @vsi_list_id: VSI list ID generated as part of allocate resource
3185 * @lkup_type: switch rule filter lookup type
3186 *
3187 * The VSI list should be emptied before this function is called to remove the
3188 * VSI list.
3189 */
3190static int
3191ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
3192			 enum ice_sw_lkup_type lkup_type)
3193{
3194	struct ice_sw_rule_vsi_list *s_rule;
3195	u16 s_rule_size;
3196	int status;
3197
3198	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, 0);
3199	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
3200	if (!s_rule)
3201		return -ENOMEM;
3202
3203	s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
3204	s_rule->index = cpu_to_le16(vsi_list_id);
3205
3206	/* Free the vsi_list resource that we allocated. It is assumed that the
3207	 * list is empty at this point.
3208	 */
3209	status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
3210					    ice_aqc_opc_free_res);
3211
3212	devm_kfree(ice_hw_to_dev(hw), s_rule);
3213	return status;
3214}
3215
3216/**
3217 * ice_rem_update_vsi_list
3218 * @hw: pointer to the hardware structure
3219 * @vsi_handle: VSI handle of the VSI to remove
3220 * @fm_list: filter management entry for which the VSI list management needs to
3221 *           be done
3222 */
3223static int
3224ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
3225			struct ice_fltr_mgmt_list_entry *fm_list)
3226{
3227	enum ice_sw_lkup_type lkup_type;
3228	u16 vsi_list_id;
3229	int status = 0;
3230
3231	if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
3232	    fm_list->vsi_count == 0)
3233		return -EINVAL;
3234
3235	/* A rule with the VSI being removed does not exist */
3236	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
3237		return -ENOENT;
3238
3239	lkup_type = fm_list->fltr_info.lkup_type;
3240	vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
3241	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
3242					  ice_aqc_opc_update_sw_rules,
3243					  lkup_type);
3244	if (status)
3245		return status;
3246
3247	fm_list->vsi_count--;
3248	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
3249
3250	if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
3251		struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
3252		struct ice_vsi_list_map_info *vsi_list_info =
3253			fm_list->vsi_list_info;
3254		u16 rem_vsi_handle;
3255
3256		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
3257						ICE_MAX_VSI);
3258		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
3259			return -EIO;
3260
3261		/* Make sure VSI list is empty before removing it below */
3262		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
3263						  vsi_list_id, true,
3264						  ice_aqc_opc_update_sw_rules,
3265						  lkup_type);
3266		if (status)
3267			return status;
3268
3269		tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
3270		tmp_fltr_info.fwd_id.hw_vsi_id =
3271			ice_get_hw_vsi_num(hw, rem_vsi_handle);
3272		tmp_fltr_info.vsi_handle = rem_vsi_handle;
3273		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
3274		if (status) {
3275			ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
3276				  tmp_fltr_info.fwd_id.hw_vsi_id, status);
3277			return status;
3278		}
3279
3280		fm_list->fltr_info = tmp_fltr_info;
3281	}
3282
3283	if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
3284	    (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
3285		struct ice_vsi_list_map_info *vsi_list_info =
3286			fm_list->vsi_list_info;
3287
3288		/* Remove the VSI list since it is no longer used */
3289		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
3290		if (status) {
3291			ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
3292				  vsi_list_id, status);
3293			return status;
3294		}
3295
3296		list_del(&vsi_list_info->list_entry);
3297		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
3298		fm_list->vsi_list_info = NULL;
3299	}
3300
3301	return status;
3302}
3303
3304/**
3305 * ice_remove_rule_internal - Remove a filter rule of a given type
3306 * @hw: pointer to the hardware structure
3307 * @recp_id: recipe ID for which the rule needs to removed
3308 * @f_entry: rule entry containing filter information
3309 */
3310static int
3311ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
3312			 struct ice_fltr_list_entry *f_entry)
3313{
3314	struct ice_switch_info *sw = hw->switch_info;
3315	struct ice_fltr_mgmt_list_entry *list_elem;
3316	struct mutex *rule_lock; /* Lock to protect filter rule list */
3317	bool remove_rule = false;
3318	u16 vsi_handle;
3319	int status = 0;
3320
3321	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
3322		return -EINVAL;
3323	f_entry->fltr_info.fwd_id.hw_vsi_id =
3324		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
3325
3326	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
3327	mutex_lock(rule_lock);
3328	list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
3329	if (!list_elem) {
3330		status = -ENOENT;
3331		goto exit;
3332	}
3333
3334	if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
3335		remove_rule = true;
3336	} else if (!list_elem->vsi_list_info) {
3337		status = -ENOENT;
3338		goto exit;
3339	} else if (list_elem->vsi_list_info->ref_cnt > 1) {
3340		/* a ref_cnt > 1 indicates that the vsi_list is being
3341		 * shared by multiple rules. Decrement the ref_cnt and
3342		 * remove this rule, but do not modify the list, as it
3343		 * is in-use by other rules.
3344		 */
3345		list_elem->vsi_list_info->ref_cnt--;
3346		remove_rule = true;
3347	} else {
3348		/* a ref_cnt of 1 indicates the vsi_list is only used
3349		 * by one rule. However, the original removal request is only
3350		 * for a single VSI. Update the vsi_list first, and only
3351		 * remove the rule if there are no further VSIs in this list.
3352		 */
3353		vsi_handle = f_entry->fltr_info.vsi_handle;
3354		status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
3355		if (status)
3356			goto exit;
3357		/* if VSI count goes to zero after updating the VSI list */
3358		if (list_elem->vsi_count == 0)
3359			remove_rule = true;
3360	}
3361
3362	if (remove_rule) {
3363		/* Remove the lookup rule */
3364		struct ice_sw_rule_lkup_rx_tx *s_rule;
3365
3366		s_rule = devm_kzalloc(ice_hw_to_dev(hw),
3367				      ICE_SW_RULE_RX_TX_NO_HDR_SIZE(s_rule),
3368				      GFP_KERNEL);
3369		if (!s_rule) {
3370			status = -ENOMEM;
3371			goto exit;
3372		}
3373
3374		ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
3375				 ice_aqc_opc_remove_sw_rules);
3376
3377		status = ice_aq_sw_rules(hw, s_rule,
3378					 ICE_SW_RULE_RX_TX_NO_HDR_SIZE(s_rule),
3379					 1, ice_aqc_opc_remove_sw_rules, NULL);
3380
3381		/* Remove a book keeping from the list */
3382		devm_kfree(ice_hw_to_dev(hw), s_rule);
3383
3384		if (status)
3385			goto exit;
3386
3387		list_del(&list_elem->list_entry);
3388		devm_kfree(ice_hw_to_dev(hw), list_elem);
3389	}
3390exit:
3391	mutex_unlock(rule_lock);
3392	return status;
3393}
3394
3395/**
3396 * ice_vlan_fltr_exist - does this VLAN filter exist for given VSI
3397 * @hw: pointer to the hardware structure
3398 * @vlan_id: VLAN ID
3399 * @vsi_handle: check MAC filter for this VSI
3400 */
3401bool ice_vlan_fltr_exist(struct ice_hw *hw, u16 vlan_id, u16 vsi_handle)
3402{
3403	struct ice_fltr_mgmt_list_entry *entry;
3404	struct list_head *rule_head;
3405	struct ice_switch_info *sw;
3406	struct mutex *rule_lock; /* Lock to protect filter rule list */
3407	u16 hw_vsi_id;
3408
3409	if (vlan_id > ICE_MAX_VLAN_ID)
3410		return false;
3411
3412	if (!ice_is_vsi_valid(hw, vsi_handle))
3413		return false;
3414
3415	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3416	sw = hw->switch_info;
3417	rule_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3418	if (!rule_head)
3419		return false;
3420
3421	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3422	mutex_lock(rule_lock);
3423	list_for_each_entry(entry, rule_head, list_entry) {
3424		struct ice_fltr_info *f_info = &entry->fltr_info;
3425		u16 entry_vlan_id = f_info->l_data.vlan.vlan_id;
3426		struct ice_vsi_list_map_info *map_info;
3427
3428		if (entry_vlan_id > ICE_MAX_VLAN_ID)
3429			continue;
3430
3431		if (f_info->flag != ICE_FLTR_TX ||
3432		    f_info->src_id != ICE_SRC_ID_VSI ||
3433		    f_info->lkup_type != ICE_SW_LKUP_VLAN)
3434			continue;
3435
3436		/* Only allowed filter action are FWD_TO_VSI/_VSI_LIST */
3437		if (f_info->fltr_act != ICE_FWD_TO_VSI &&
3438		    f_info->fltr_act != ICE_FWD_TO_VSI_LIST)
3439			continue;
3440
3441		if (f_info->fltr_act == ICE_FWD_TO_VSI) {
3442			if (hw_vsi_id != f_info->fwd_id.hw_vsi_id)
3443				continue;
3444		} else if (f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
3445			/* If filter_action is FWD_TO_VSI_LIST, make sure
3446			 * that VSI being checked is part of VSI list
3447			 */
3448			if (entry->vsi_count == 1 &&
3449			    entry->vsi_list_info) {
3450				map_info = entry->vsi_list_info;
3451				if (!test_bit(vsi_handle, map_info->vsi_map))
3452					continue;
3453			}
3454		}
3455
3456		if (vlan_id == entry_vlan_id) {
3457			mutex_unlock(rule_lock);
3458			return true;
3459		}
3460	}
3461	mutex_unlock(rule_lock);
3462
3463	return false;
3464}
3465
3466/**
3467 * ice_add_mac - Add a MAC address based filter rule
3468 * @hw: pointer to the hardware structure
3469 * @m_list: list of MAC addresses and forwarding information
3470 */
3471int ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
3472{
3473	struct ice_fltr_list_entry *m_list_itr;
3474	int status = 0;
3475
3476	if (!m_list || !hw)
3477		return -EINVAL;
3478
3479	list_for_each_entry(m_list_itr, m_list, list_entry) {
3480		u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
3481		u16 vsi_handle;
3482		u16 hw_vsi_id;
3483
3484		m_list_itr->fltr_info.flag = ICE_FLTR_TX;
3485		vsi_handle = m_list_itr->fltr_info.vsi_handle;
3486		if (!ice_is_vsi_valid(hw, vsi_handle))
3487			return -EINVAL;
3488		hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3489		m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
3490		/* update the src in case it is VSI num */
3491		if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
3492			return -EINVAL;
3493		m_list_itr->fltr_info.src = hw_vsi_id;
3494		if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
3495		    is_zero_ether_addr(add))
3496			return -EINVAL;
3497
3498		m_list_itr->status = ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
3499							   m_list_itr);
3500		if (m_list_itr->status)
3501			return m_list_itr->status;
3502	}
3503
3504	return status;
3505}
3506
3507/**
3508 * ice_add_vlan_internal - Add one VLAN based filter rule
3509 * @hw: pointer to the hardware structure
3510 * @f_entry: filter entry containing one VLAN information
3511 */
3512static int
3513ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
3514{
3515	struct ice_switch_info *sw = hw->switch_info;
3516	struct ice_fltr_mgmt_list_entry *v_list_itr;
3517	struct ice_fltr_info *new_fltr, *cur_fltr;
3518	enum ice_sw_lkup_type lkup_type;
3519	u16 vsi_list_id = 0, vsi_handle;
3520	struct mutex *rule_lock; /* Lock to protect filter rule list */
3521	int status = 0;
3522
3523	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
3524		return -EINVAL;
3525
3526	f_entry->fltr_info.fwd_id.hw_vsi_id =
3527		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
3528	new_fltr = &f_entry->fltr_info;
3529
3530	/* VLAN ID should only be 12 bits */
3531	if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
3532		return -EINVAL;
3533
3534	if (new_fltr->src_id != ICE_SRC_ID_VSI)
3535		return -EINVAL;
3536
3537	new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
3538	lkup_type = new_fltr->lkup_type;
3539	vsi_handle = new_fltr->vsi_handle;
3540	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3541	mutex_lock(rule_lock);
3542	v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
3543	if (!v_list_itr) {
3544		struct ice_vsi_list_map_info *map_info = NULL;
3545
3546		if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
3547			/* All VLAN pruning rules use a VSI list. Check if
3548			 * there is already a VSI list containing VSI that we
3549			 * want to add. If found, use the same vsi_list_id for
3550			 * this new VLAN rule or else create a new list.
3551			 */
3552			map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
3553							   vsi_handle,
3554							   &vsi_list_id);
3555			if (!map_info) {
3556				status = ice_create_vsi_list_rule(hw,
3557								  &vsi_handle,
3558								  1,
3559								  &vsi_list_id,
3560								  lkup_type);
3561				if (status)
3562					goto exit;
3563			}
3564			/* Convert the action to forwarding to a VSI list. */
3565			new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
3566			new_fltr->fwd_id.vsi_list_id = vsi_list_id;
3567		}
3568
3569		status = ice_create_pkt_fwd_rule(hw, f_entry);
3570		if (!status) {
3571			v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
3572							 new_fltr);
3573			if (!v_list_itr) {
3574				status = -ENOENT;
3575				goto exit;
3576			}
3577			/* reuse VSI list for new rule and increment ref_cnt */
3578			if (map_info) {
3579				v_list_itr->vsi_list_info = map_info;
3580				map_info->ref_cnt++;
3581			} else {
3582				v_list_itr->vsi_list_info =
3583					ice_create_vsi_list_map(hw, &vsi_handle,
3584								1, vsi_list_id);
3585			}
3586		}
3587	} else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
3588		/* Update existing VSI list to add new VSI ID only if it used
3589		 * by one VLAN rule.
3590		 */
3591		cur_fltr = &v_list_itr->fltr_info;
3592		status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
3593						 new_fltr);
3594	} else {
3595		/* If VLAN rule exists and VSI list being used by this rule is
3596		 * referenced by more than 1 VLAN rule. Then create a new VSI
3597		 * list appending previous VSI with new VSI and update existing
3598		 * VLAN rule to point to new VSI list ID
3599		 */
3600		struct ice_fltr_info tmp_fltr;
3601		u16 vsi_handle_arr[2];
3602		u16 cur_handle;
3603
3604		/* Current implementation only supports reusing VSI list with
3605		 * one VSI count. We should never hit below condition
3606		 */
3607		if (v_list_itr->vsi_count > 1 &&
3608		    v_list_itr->vsi_list_info->ref_cnt > 1) {
3609			ice_debug(hw, ICE_DBG_SW, "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
3610			status = -EIO;
3611			goto exit;
3612		}
3613
3614		cur_handle =
3615			find_first_bit(v_list_itr->vsi_list_info->vsi_map,
3616				       ICE_MAX_VSI);
3617
3618		/* A rule already exists with the new VSI being added */
3619		if (cur_handle == vsi_handle) {
3620			status = -EEXIST;
3621			goto exit;
3622		}
3623
3624		vsi_handle_arr[0] = cur_handle;
3625		vsi_handle_arr[1] = vsi_handle;
3626		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
3627						  &vsi_list_id, lkup_type);
3628		if (status)
3629			goto exit;
3630
3631		tmp_fltr = v_list_itr->fltr_info;
3632		tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
3633		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
3634		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
3635		/* Update the previous switch rule to a new VSI list which
3636		 * includes current VSI that is requested
3637		 */
3638		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
3639		if (status)
3640			goto exit;
3641
3642		/* before overriding VSI list map info. decrement ref_cnt of
3643		 * previous VSI list
3644		 */
3645		v_list_itr->vsi_list_info->ref_cnt--;
3646
3647		/* now update to newly created list */
3648		v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
3649		v_list_itr->vsi_list_info =
3650			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
3651						vsi_list_id);
3652		v_list_itr->vsi_count++;
3653	}
3654
3655exit:
3656	mutex_unlock(rule_lock);
3657	return status;
3658}
3659
3660/**
3661 * ice_add_vlan - Add VLAN based filter rule
3662 * @hw: pointer to the hardware structure
3663 * @v_list: list of VLAN entries and forwarding information
3664 */
3665int ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
3666{
3667	struct ice_fltr_list_entry *v_list_itr;
3668
3669	if (!v_list || !hw)
3670		return -EINVAL;
3671
3672	list_for_each_entry(v_list_itr, v_list, list_entry) {
3673		if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
3674			return -EINVAL;
3675		v_list_itr->fltr_info.flag = ICE_FLTR_TX;
3676		v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
3677		if (v_list_itr->status)
3678			return v_list_itr->status;
3679	}
3680	return 0;
3681}
3682
3683/**
3684 * ice_add_eth_mac - Add ethertype and MAC based filter rule
3685 * @hw: pointer to the hardware structure
3686 * @em_list: list of ether type MAC filter, MAC is optional
3687 *
3688 * This function requires the caller to populate the entries in
3689 * the filter list with the necessary fields (including flags to
3690 * indicate Tx or Rx rules).
3691 */
3692int ice_add_eth_mac(struct ice_hw *hw, struct list_head *em_list)
3693{
3694	struct ice_fltr_list_entry *em_list_itr;
3695
3696	if (!em_list || !hw)
3697		return -EINVAL;
3698
3699	list_for_each_entry(em_list_itr, em_list, list_entry) {
3700		enum ice_sw_lkup_type l_type =
3701			em_list_itr->fltr_info.lkup_type;
3702
3703		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
3704		    l_type != ICE_SW_LKUP_ETHERTYPE)
3705			return -EINVAL;
3706
3707		em_list_itr->status = ice_add_rule_internal(hw, l_type,
3708							    em_list_itr);
3709		if (em_list_itr->status)
3710			return em_list_itr->status;
3711	}
3712	return 0;
3713}
3714
3715/**
3716 * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
3717 * @hw: pointer to the hardware structure
3718 * @em_list: list of ethertype or ethertype MAC entries
3719 */
3720int ice_remove_eth_mac(struct ice_hw *hw, struct list_head *em_list)
3721{
3722	struct ice_fltr_list_entry *em_list_itr, *tmp;
3723
3724	if (!em_list || !hw)
3725		return -EINVAL;
3726
3727	list_for_each_entry_safe(em_list_itr, tmp, em_list, list_entry) {
3728		enum ice_sw_lkup_type l_type =
3729			em_list_itr->fltr_info.lkup_type;
3730
3731		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
3732		    l_type != ICE_SW_LKUP_ETHERTYPE)
3733			return -EINVAL;
3734
3735		em_list_itr->status = ice_remove_rule_internal(hw, l_type,
3736							       em_list_itr);
3737		if (em_list_itr->status)
3738			return em_list_itr->status;
3739	}
3740	return 0;
3741}
3742
3743/**
3744 * ice_rem_sw_rule_info
3745 * @hw: pointer to the hardware structure
3746 * @rule_head: pointer to the switch list structure that we want to delete
3747 */
3748static void
3749ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
3750{
3751	if (!list_empty(rule_head)) {
3752		struct ice_fltr_mgmt_list_entry *entry;
3753		struct ice_fltr_mgmt_list_entry *tmp;
3754
3755		list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
3756			list_del(&entry->list_entry);
3757			devm_kfree(ice_hw_to_dev(hw), entry);
3758		}
3759	}
3760}
3761
3762/**
3763 * ice_rem_adv_rule_info
3764 * @hw: pointer to the hardware structure
3765 * @rule_head: pointer to the switch list structure that we want to delete
3766 */
3767static void
3768ice_rem_adv_rule_info(struct ice_hw *hw, struct list_head *rule_head)
3769{
3770	struct ice_adv_fltr_mgmt_list_entry *tmp_entry;
3771	struct ice_adv_fltr_mgmt_list_entry *lst_itr;
3772
3773	if (list_empty(rule_head))
3774		return;
3775
3776	list_for_each_entry_safe(lst_itr, tmp_entry, rule_head, list_entry) {
3777		list_del(&lst_itr->list_entry);
3778		devm_kfree(ice_hw_to_dev(hw), lst_itr->lkups);
3779		devm_kfree(ice_hw_to_dev(hw), lst_itr);
3780	}
3781}
3782
3783/**
3784 * ice_cfg_dflt_vsi - change state of VSI to set/clear default
3785 * @pi: pointer to the port_info structure
3786 * @vsi_handle: VSI handle to set as default
3787 * @set: true to add the above mentioned switch rule, false to remove it
3788 * @direction: ICE_FLTR_RX or ICE_FLTR_TX
3789 *
3790 * add filter rule to set/unset given VSI as default VSI for the switch
3791 * (represented by swid)
3792 */
3793int
3794ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
3795		 u8 direction)
3796{
3797	struct ice_fltr_list_entry f_list_entry;
3798	struct ice_fltr_info f_info;
3799	struct ice_hw *hw = pi->hw;
3800	u16 hw_vsi_id;
3801	int status;
3802
3803	if (!ice_is_vsi_valid(hw, vsi_handle))
3804		return -EINVAL;
3805
3806	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3807
3808	memset(&f_info, 0, sizeof(f_info));
3809
3810	f_info.lkup_type = ICE_SW_LKUP_DFLT;
3811	f_info.flag = direction;
3812	f_info.fltr_act = ICE_FWD_TO_VSI;
3813	f_info.fwd_id.hw_vsi_id = hw_vsi_id;
3814	f_info.vsi_handle = vsi_handle;
3815
3816	if (f_info.flag & ICE_FLTR_RX) {
3817		f_info.src = hw->port_info->lport;
3818		f_info.src_id = ICE_SRC_ID_LPORT;
3819	} else if (f_info.flag & ICE_FLTR_TX) {
3820		f_info.src_id = ICE_SRC_ID_VSI;
3821		f_info.src = hw_vsi_id;
 
3822	}
3823	f_list_entry.fltr_info = f_info;
3824
3825	if (set)
3826		status = ice_add_rule_internal(hw, ICE_SW_LKUP_DFLT,
3827					       &f_list_entry);
3828	else
3829		status = ice_remove_rule_internal(hw, ICE_SW_LKUP_DFLT,
3830						  &f_list_entry);
3831
3832	return status;
3833}
3834
3835/**
3836 * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
3837 * @fm_entry: filter entry to inspect
3838 * @vsi_handle: VSI handle to compare with filter info
3839 */
3840static bool
3841ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
3842{
3843	return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
3844		 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
3845		(fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
3846		 fm_entry->vsi_list_info &&
3847		 (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
3848}
3849
3850/**
3851 * ice_check_if_dflt_vsi - check if VSI is default VSI
3852 * @pi: pointer to the port_info structure
3853 * @vsi_handle: vsi handle to check for in filter list
3854 * @rule_exists: indicates if there are any VSI's in the rule list
3855 *
3856 * checks if the VSI is in a default VSI list, and also indicates
3857 * if the default VSI list is empty
3858 */
3859bool
3860ice_check_if_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle,
3861		      bool *rule_exists)
3862{
3863	struct ice_fltr_mgmt_list_entry *fm_entry;
3864	struct ice_sw_recipe *recp_list;
3865	struct list_head *rule_head;
3866	struct mutex *rule_lock; /* Lock to protect filter rule list */
3867	bool ret = false;
3868
3869	recp_list = &pi->hw->switch_info->recp_list[ICE_SW_LKUP_DFLT];
3870	rule_lock = &recp_list->filt_rule_lock;
3871	rule_head = &recp_list->filt_rules;
3872
3873	mutex_lock(rule_lock);
3874
3875	if (rule_exists && !list_empty(rule_head))
3876		*rule_exists = true;
3877
3878	list_for_each_entry(fm_entry, rule_head, list_entry) {
3879		if (ice_vsi_uses_fltr(fm_entry, vsi_handle)) {
3880			ret = true;
3881			break;
3882		}
3883	}
3884
3885	mutex_unlock(rule_lock);
3886
3887	return ret;
3888}
3889
3890/**
3891 * ice_remove_mac - remove a MAC address based filter rule
3892 * @hw: pointer to the hardware structure
3893 * @m_list: list of MAC addresses and forwarding information
3894 *
3895 * This function removes either a MAC filter rule or a specific VSI from a
3896 * VSI list for a multicast MAC address.
3897 *
3898 * Returns -ENOENT if a given entry was not added by ice_add_mac. Caller should
3899 * be aware that this call will only work if all the entries passed into m_list
3900 * were added previously. It will not attempt to do a partial remove of entries
3901 * that were found.
3902 */
3903int ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
3904{
3905	struct ice_fltr_list_entry *list_itr, *tmp;
3906
3907	if (!m_list)
3908		return -EINVAL;
3909
3910	list_for_each_entry_safe(list_itr, tmp, m_list, list_entry) {
3911		enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
3912		u16 vsi_handle;
3913
3914		if (l_type != ICE_SW_LKUP_MAC)
3915			return -EINVAL;
3916
3917		vsi_handle = list_itr->fltr_info.vsi_handle;
3918		if (!ice_is_vsi_valid(hw, vsi_handle))
3919			return -EINVAL;
3920
3921		list_itr->fltr_info.fwd_id.hw_vsi_id =
3922					ice_get_hw_vsi_num(hw, vsi_handle);
3923
3924		list_itr->status = ice_remove_rule_internal(hw,
3925							    ICE_SW_LKUP_MAC,
3926							    list_itr);
3927		if (list_itr->status)
3928			return list_itr->status;
3929	}
3930	return 0;
3931}
3932
3933/**
3934 * ice_remove_vlan - Remove VLAN based filter rule
3935 * @hw: pointer to the hardware structure
3936 * @v_list: list of VLAN entries and forwarding information
3937 */
3938int ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
3939{
3940	struct ice_fltr_list_entry *v_list_itr, *tmp;
3941
3942	if (!v_list || !hw)
3943		return -EINVAL;
3944
3945	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
3946		enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
3947
3948		if (l_type != ICE_SW_LKUP_VLAN)
3949			return -EINVAL;
3950		v_list_itr->status = ice_remove_rule_internal(hw,
3951							      ICE_SW_LKUP_VLAN,
3952							      v_list_itr);
3953		if (v_list_itr->status)
3954			return v_list_itr->status;
3955	}
3956	return 0;
3957}
3958
3959/**
3960 * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
3961 * @hw: pointer to the hardware structure
3962 * @vsi_handle: VSI handle to remove filters from
3963 * @vsi_list_head: pointer to the list to add entry to
3964 * @fi: pointer to fltr_info of filter entry to copy & add
3965 *
3966 * Helper function, used when creating a list of filters to remove from
3967 * a specific VSI. The entry added to vsi_list_head is a COPY of the
3968 * original filter entry, with the exception of fltr_info.fltr_act and
3969 * fltr_info.fwd_id fields. These are set such that later logic can
3970 * extract which VSI to remove the fltr from, and pass on that information.
3971 */
3972static int
3973ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3974			       struct list_head *vsi_list_head,
3975			       struct ice_fltr_info *fi)
3976{
3977	struct ice_fltr_list_entry *tmp;
3978
3979	/* this memory is freed up in the caller function
3980	 * once filters for this VSI are removed
3981	 */
3982	tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
3983	if (!tmp)
3984		return -ENOMEM;
3985
3986	tmp->fltr_info = *fi;
3987
3988	/* Overwrite these fields to indicate which VSI to remove filter from,
3989	 * so find and remove logic can extract the information from the
3990	 * list entries. Note that original entries will still have proper
3991	 * values.
3992	 */
3993	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
3994	tmp->fltr_info.vsi_handle = vsi_handle;
3995	tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3996
3997	list_add(&tmp->list_entry, vsi_list_head);
3998
3999	return 0;
4000}
4001
4002/**
4003 * ice_add_to_vsi_fltr_list - Add VSI filters to the list
4004 * @hw: pointer to the hardware structure
4005 * @vsi_handle: VSI handle to remove filters from
4006 * @lkup_list_head: pointer to the list that has certain lookup type filters
4007 * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
4008 *
4009 * Locates all filters in lkup_list_head that are used by the given VSI,
4010 * and adds COPIES of those entries to vsi_list_head (intended to be used
4011 * to remove the listed filters).
4012 * Note that this means all entries in vsi_list_head must be explicitly
4013 * deallocated by the caller when done with list.
4014 */
4015static int
4016ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
4017			 struct list_head *lkup_list_head,
4018			 struct list_head *vsi_list_head)
4019{
4020	struct ice_fltr_mgmt_list_entry *fm_entry;
4021	int status = 0;
4022
4023	/* check to make sure VSI ID is valid and within boundary */
4024	if (!ice_is_vsi_valid(hw, vsi_handle))
4025		return -EINVAL;
4026
4027	list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
4028		if (!ice_vsi_uses_fltr(fm_entry, vsi_handle))
4029			continue;
4030
4031		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
4032							vsi_list_head,
4033							&fm_entry->fltr_info);
4034		if (status)
4035			return status;
4036	}
4037	return status;
4038}
4039
4040/**
4041 * ice_determine_promisc_mask
4042 * @fi: filter info to parse
4043 *
4044 * Helper function to determine which ICE_PROMISC_ mask corresponds
4045 * to given filter into.
4046 */
4047static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
4048{
4049	u16 vid = fi->l_data.mac_vlan.vlan_id;
4050	u8 *macaddr = fi->l_data.mac.mac_addr;
4051	bool is_tx_fltr = false;
4052	u8 promisc_mask = 0;
4053
4054	if (fi->flag == ICE_FLTR_TX)
4055		is_tx_fltr = true;
4056
4057	if (is_broadcast_ether_addr(macaddr))
4058		promisc_mask |= is_tx_fltr ?
4059			ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
4060	else if (is_multicast_ether_addr(macaddr))
4061		promisc_mask |= is_tx_fltr ?
4062			ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
4063	else if (is_unicast_ether_addr(macaddr))
4064		promisc_mask |= is_tx_fltr ?
4065			ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
4066	if (vid)
4067		promisc_mask |= is_tx_fltr ?
4068			ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
4069
4070	return promisc_mask;
4071}
4072
4073/**
4074 * ice_remove_promisc - Remove promisc based filter rules
4075 * @hw: pointer to the hardware structure
4076 * @recp_id: recipe ID for which the rule needs to removed
4077 * @v_list: list of promisc entries
4078 */
4079static int
4080ice_remove_promisc(struct ice_hw *hw, u8 recp_id, struct list_head *v_list)
4081{
4082	struct ice_fltr_list_entry *v_list_itr, *tmp;
4083
4084	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
4085		v_list_itr->status =
4086			ice_remove_rule_internal(hw, recp_id, v_list_itr);
4087		if (v_list_itr->status)
4088			return v_list_itr->status;
4089	}
4090	return 0;
4091}
4092
4093/**
4094 * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
4095 * @hw: pointer to the hardware structure
4096 * @vsi_handle: VSI handle to clear mode
4097 * @promisc_mask: mask of promiscuous config bits to clear
4098 * @vid: VLAN ID to clear VLAN promiscuous
4099 */
4100int
4101ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
4102		      u16 vid)
4103{
4104	struct ice_switch_info *sw = hw->switch_info;
4105	struct ice_fltr_list_entry *fm_entry, *tmp;
4106	struct list_head remove_list_head;
4107	struct ice_fltr_mgmt_list_entry *itr;
4108	struct list_head *rule_head;
4109	struct mutex *rule_lock;	/* Lock to protect filter rule list */
4110	int status = 0;
4111	u8 recipe_id;
4112
4113	if (!ice_is_vsi_valid(hw, vsi_handle))
4114		return -EINVAL;
4115
4116	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX))
4117		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
4118	else
4119		recipe_id = ICE_SW_LKUP_PROMISC;
4120
4121	rule_head = &sw->recp_list[recipe_id].filt_rules;
4122	rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
4123
4124	INIT_LIST_HEAD(&remove_list_head);
4125
4126	mutex_lock(rule_lock);
4127	list_for_each_entry(itr, rule_head, list_entry) {
4128		struct ice_fltr_info *fltr_info;
4129		u8 fltr_promisc_mask = 0;
4130
4131		if (!ice_vsi_uses_fltr(itr, vsi_handle))
4132			continue;
4133		fltr_info = &itr->fltr_info;
4134
4135		if (recipe_id == ICE_SW_LKUP_PROMISC_VLAN &&
4136		    vid != fltr_info->l_data.mac_vlan.vlan_id)
4137			continue;
4138
4139		fltr_promisc_mask |= ice_determine_promisc_mask(fltr_info);
4140
4141		/* Skip if filter is not completely specified by given mask */
4142		if (fltr_promisc_mask & ~promisc_mask)
4143			continue;
4144
4145		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
4146							&remove_list_head,
4147							fltr_info);
4148		if (status) {
4149			mutex_unlock(rule_lock);
4150			goto free_fltr_list;
4151		}
4152	}
4153	mutex_unlock(rule_lock);
4154
4155	status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
4156
4157free_fltr_list:
4158	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
4159		list_del(&fm_entry->list_entry);
4160		devm_kfree(ice_hw_to_dev(hw), fm_entry);
4161	}
4162
4163	return status;
4164}
4165
4166/**
4167 * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
4168 * @hw: pointer to the hardware structure
4169 * @vsi_handle: VSI handle to configure
4170 * @promisc_mask: mask of promiscuous config bits
4171 * @vid: VLAN ID to set VLAN promiscuous
4172 */
4173int
4174ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
4175{
4176	enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
4177	struct ice_fltr_list_entry f_list_entry;
4178	struct ice_fltr_info new_fltr;
4179	bool is_tx_fltr;
4180	int status = 0;
4181	u16 hw_vsi_id;
4182	int pkt_type;
4183	u8 recipe_id;
4184
4185	if (!ice_is_vsi_valid(hw, vsi_handle))
4186		return -EINVAL;
4187	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
4188
4189	memset(&new_fltr, 0, sizeof(new_fltr));
4190
4191	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
4192		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
4193		new_fltr.l_data.mac_vlan.vlan_id = vid;
4194		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
4195	} else {
4196		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
4197		recipe_id = ICE_SW_LKUP_PROMISC;
4198	}
4199
4200	/* Separate filters must be set for each direction/packet type
4201	 * combination, so we will loop over the mask value, store the
4202	 * individual type, and clear it out in the input mask as it
4203	 * is found.
4204	 */
4205	while (promisc_mask) {
4206		u8 *mac_addr;
4207
4208		pkt_type = 0;
4209		is_tx_fltr = false;
4210
4211		if (promisc_mask & ICE_PROMISC_UCAST_RX) {
4212			promisc_mask &= ~ICE_PROMISC_UCAST_RX;
4213			pkt_type = UCAST_FLTR;
4214		} else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
4215			promisc_mask &= ~ICE_PROMISC_UCAST_TX;
4216			pkt_type = UCAST_FLTR;
4217			is_tx_fltr = true;
4218		} else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
4219			promisc_mask &= ~ICE_PROMISC_MCAST_RX;
4220			pkt_type = MCAST_FLTR;
4221		} else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
4222			promisc_mask &= ~ICE_PROMISC_MCAST_TX;
4223			pkt_type = MCAST_FLTR;
4224			is_tx_fltr = true;
4225		} else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
4226			promisc_mask &= ~ICE_PROMISC_BCAST_RX;
4227			pkt_type = BCAST_FLTR;
4228		} else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
4229			promisc_mask &= ~ICE_PROMISC_BCAST_TX;
4230			pkt_type = BCAST_FLTR;
4231			is_tx_fltr = true;
4232		}
4233
4234		/* Check for VLAN promiscuous flag */
4235		if (promisc_mask & ICE_PROMISC_VLAN_RX) {
4236			promisc_mask &= ~ICE_PROMISC_VLAN_RX;
4237		} else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
4238			promisc_mask &= ~ICE_PROMISC_VLAN_TX;
4239			is_tx_fltr = true;
4240		}
4241
4242		/* Set filter DA based on packet type */
4243		mac_addr = new_fltr.l_data.mac.mac_addr;
4244		if (pkt_type == BCAST_FLTR) {
4245			eth_broadcast_addr(mac_addr);
4246		} else if (pkt_type == MCAST_FLTR ||
4247			   pkt_type == UCAST_FLTR) {
4248			/* Use the dummy ether header DA */
4249			ether_addr_copy(mac_addr, dummy_eth_header);
4250			if (pkt_type == MCAST_FLTR)
4251				mac_addr[0] |= 0x1;	/* Set multicast bit */
4252		}
4253
4254		/* Need to reset this to zero for all iterations */
4255		new_fltr.flag = 0;
4256		if (is_tx_fltr) {
4257			new_fltr.flag |= ICE_FLTR_TX;
4258			new_fltr.src = hw_vsi_id;
4259		} else {
4260			new_fltr.flag |= ICE_FLTR_RX;
4261			new_fltr.src = hw->port_info->lport;
4262		}
4263
4264		new_fltr.fltr_act = ICE_FWD_TO_VSI;
4265		new_fltr.vsi_handle = vsi_handle;
4266		new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
4267		f_list_entry.fltr_info = new_fltr;
4268
4269		status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
4270		if (status)
4271			goto set_promisc_exit;
4272	}
4273
4274set_promisc_exit:
4275	return status;
4276}
4277
4278/**
4279 * ice_set_vlan_vsi_promisc
4280 * @hw: pointer to the hardware structure
4281 * @vsi_handle: VSI handle to configure
4282 * @promisc_mask: mask of promiscuous config bits
4283 * @rm_vlan_promisc: Clear VLANs VSI promisc mode
4284 *
4285 * Configure VSI with all associated VLANs to given promiscuous mode(s)
4286 */
4287int
4288ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
4289			 bool rm_vlan_promisc)
4290{
4291	struct ice_switch_info *sw = hw->switch_info;
4292	struct ice_fltr_list_entry *list_itr, *tmp;
4293	struct list_head vsi_list_head;
4294	struct list_head *vlan_head;
4295	struct mutex *vlan_lock; /* Lock to protect filter rule list */
4296	u16 vlan_id;
4297	int status;
4298
4299	INIT_LIST_HEAD(&vsi_list_head);
4300	vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
4301	vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
4302	mutex_lock(vlan_lock);
4303	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
4304					  &vsi_list_head);
4305	mutex_unlock(vlan_lock);
4306	if (status)
4307		goto free_fltr_list;
4308
4309	list_for_each_entry(list_itr, &vsi_list_head, list_entry) {
4310		/* Avoid enabling or disabling VLAN zero twice when in double
4311		 * VLAN mode
4312		 */
4313		if (ice_is_dvm_ena(hw) &&
4314		    list_itr->fltr_info.l_data.vlan.tpid == 0)
4315			continue;
4316
4317		vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
4318		if (rm_vlan_promisc)
4319			status = ice_clear_vsi_promisc(hw, vsi_handle,
4320						       promisc_mask, vlan_id);
4321		else
4322			status = ice_set_vsi_promisc(hw, vsi_handle,
4323						     promisc_mask, vlan_id);
4324		if (status && status != -EEXIST)
4325			break;
4326	}
4327
4328free_fltr_list:
4329	list_for_each_entry_safe(list_itr, tmp, &vsi_list_head, list_entry) {
4330		list_del(&list_itr->list_entry);
4331		devm_kfree(ice_hw_to_dev(hw), list_itr);
4332	}
4333	return status;
4334}
4335
4336/**
4337 * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
4338 * @hw: pointer to the hardware structure
4339 * @vsi_handle: VSI handle to remove filters from
4340 * @lkup: switch rule filter lookup type
4341 */
4342static void
4343ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
4344			 enum ice_sw_lkup_type lkup)
4345{
4346	struct ice_switch_info *sw = hw->switch_info;
4347	struct ice_fltr_list_entry *fm_entry;
4348	struct list_head remove_list_head;
4349	struct list_head *rule_head;
4350	struct ice_fltr_list_entry *tmp;
4351	struct mutex *rule_lock;	/* Lock to protect filter rule list */
4352	int status;
4353
4354	INIT_LIST_HEAD(&remove_list_head);
4355	rule_lock = &sw->recp_list[lkup].filt_rule_lock;
4356	rule_head = &sw->recp_list[lkup].filt_rules;
4357	mutex_lock(rule_lock);
4358	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
4359					  &remove_list_head);
4360	mutex_unlock(rule_lock);
4361	if (status)
4362		goto free_fltr_list;
4363
4364	switch (lkup) {
4365	case ICE_SW_LKUP_MAC:
4366		ice_remove_mac(hw, &remove_list_head);
4367		break;
4368	case ICE_SW_LKUP_VLAN:
4369		ice_remove_vlan(hw, &remove_list_head);
4370		break;
4371	case ICE_SW_LKUP_PROMISC:
4372	case ICE_SW_LKUP_PROMISC_VLAN:
4373		ice_remove_promisc(hw, lkup, &remove_list_head);
4374		break;
4375	case ICE_SW_LKUP_MAC_VLAN:
4376	case ICE_SW_LKUP_ETHERTYPE:
4377	case ICE_SW_LKUP_ETHERTYPE_MAC:
4378	case ICE_SW_LKUP_DFLT:
4379	case ICE_SW_LKUP_LAST:
4380	default:
4381		ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
4382		break;
4383	}
4384
4385free_fltr_list:
4386	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
4387		list_del(&fm_entry->list_entry);
4388		devm_kfree(ice_hw_to_dev(hw), fm_entry);
4389	}
4390}
4391
4392/**
4393 * ice_remove_vsi_fltr - Remove all filters for a VSI
4394 * @hw: pointer to the hardware structure
4395 * @vsi_handle: VSI handle to remove filters from
4396 */
4397void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
4398{
4399	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
4400	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
4401	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
4402	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
4403	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
4404	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
4405	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
4406	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
4407}
4408
4409/**
4410 * ice_alloc_res_cntr - allocating resource counter
4411 * @hw: pointer to the hardware structure
4412 * @type: type of resource
4413 * @alloc_shared: if set it is shared else dedicated
4414 * @num_items: number of entries requested for FD resource type
4415 * @counter_id: counter index returned by AQ call
4416 */
4417int
4418ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
4419		   u16 *counter_id)
4420{
4421	DEFINE_FLEX(struct ice_aqc_alloc_free_res_elem, buf, elem, 1);
4422	u16 buf_len = __struct_size(buf);
4423	int status;
4424
4425	buf->num_elems = cpu_to_le16(num_items);
4426	buf->res_type = cpu_to_le16(FIELD_PREP(ICE_AQC_RES_TYPE_M, type) |
4427				    alloc_shared);
4428
4429	status = ice_aq_alloc_free_res(hw, buf, buf_len, ice_aqc_opc_alloc_res);
4430	if (status)
4431		return status;
4432
4433	*counter_id = le16_to_cpu(buf->elem[0].e.sw_resp);
4434	return status;
4435}
4436
4437/**
4438 * ice_free_res_cntr - free resource counter
4439 * @hw: pointer to the hardware structure
4440 * @type: type of resource
4441 * @alloc_shared: if set it is shared else dedicated
4442 * @num_items: number of entries to be freed for FD resource type
4443 * @counter_id: counter ID resource which needs to be freed
4444 */
4445int
4446ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
4447		  u16 counter_id)
4448{
4449	DEFINE_FLEX(struct ice_aqc_alloc_free_res_elem, buf, elem, 1);
4450	u16 buf_len = __struct_size(buf);
4451	int status;
4452
4453	buf->num_elems = cpu_to_le16(num_items);
4454	buf->res_type = cpu_to_le16(FIELD_PREP(ICE_AQC_RES_TYPE_M, type) |
4455				    alloc_shared);
4456	buf->elem[0].e.sw_resp = cpu_to_le16(counter_id);
4457
4458	status = ice_aq_alloc_free_res(hw, buf, buf_len, ice_aqc_opc_free_res);
4459	if (status)
4460		ice_debug(hw, ICE_DBG_SW, "counter resource could not be freed\n");
4461
4462	return status;
4463}
4464
4465#define ICE_PROTOCOL_ENTRY(id, ...) {		\
4466	.prot_type	= id,			\
4467	.offs		= {__VA_ARGS__},	\
4468}
4469
4470/**
4471 * ice_share_res - set a resource as shared or dedicated
4472 * @hw: hw struct of original owner of resource
4473 * @type: resource type
4474 * @shared: is the resource being set to shared
4475 * @res_id: resource id (descriptor)
4476 */
4477int ice_share_res(struct ice_hw *hw, u16 type, u8 shared, u16 res_id)
4478{
4479	DEFINE_FLEX(struct ice_aqc_alloc_free_res_elem, buf, elem, 1);
4480	u16 buf_len = __struct_size(buf);
4481	u16 res_type;
4482	int status;
4483
4484	buf->num_elems = cpu_to_le16(1);
4485	res_type = FIELD_PREP(ICE_AQC_RES_TYPE_M, type);
4486	if (shared)
4487		res_type |= ICE_AQC_RES_TYPE_FLAG_SHARED;
4488
4489	buf->res_type = cpu_to_le16(res_type);
4490	buf->elem[0].e.sw_resp = cpu_to_le16(res_id);
4491	status = ice_aq_alloc_free_res(hw, buf, buf_len,
4492				       ice_aqc_opc_share_res);
4493	if (status)
4494		ice_debug(hw, ICE_DBG_SW, "Could not set resource type %u id %u to %s\n",
4495			  type, res_id, shared ? "SHARED" : "DEDICATED");
4496
4497	return status;
4498}
4499
4500/* This is mapping table entry that maps every word within a given protocol
4501 * structure to the real byte offset as per the specification of that
4502 * protocol header.
4503 * for example dst address is 3 words in ethertype header and corresponding
4504 * bytes are 0, 2, 3 in the actual packet header and src address is at 4, 6, 8
4505 * IMPORTANT: Every structure part of "ice_prot_hdr" union should have a
4506 * matching entry describing its field. This needs to be updated if new
4507 * structure is added to that union.
4508 */
4509static const struct ice_prot_ext_tbl_entry ice_prot_ext[ICE_PROTOCOL_LAST] = {
4510	ICE_PROTOCOL_ENTRY(ICE_MAC_OFOS, 0, 2, 4, 6, 8, 10, 12),
4511	ICE_PROTOCOL_ENTRY(ICE_MAC_IL, 0, 2, 4, 6, 8, 10, 12),
4512	ICE_PROTOCOL_ENTRY(ICE_ETYPE_OL, 0),
4513	ICE_PROTOCOL_ENTRY(ICE_ETYPE_IL, 0),
4514	ICE_PROTOCOL_ENTRY(ICE_VLAN_OFOS, 2, 0),
4515	ICE_PROTOCOL_ENTRY(ICE_IPV4_OFOS, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18),
4516	ICE_PROTOCOL_ENTRY(ICE_IPV4_IL,	0, 2, 4, 6, 8, 10, 12, 14, 16, 18),
4517	ICE_PROTOCOL_ENTRY(ICE_IPV6_OFOS, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18,
4518			   20, 22, 24, 26, 28, 30, 32, 34, 36, 38),
4519	ICE_PROTOCOL_ENTRY(ICE_IPV6_IL, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
4520			   22, 24, 26, 28, 30, 32, 34, 36, 38),
4521	ICE_PROTOCOL_ENTRY(ICE_TCP_IL, 0, 2),
4522	ICE_PROTOCOL_ENTRY(ICE_UDP_OF, 0, 2),
4523	ICE_PROTOCOL_ENTRY(ICE_UDP_ILOS, 0, 2),
4524	ICE_PROTOCOL_ENTRY(ICE_VXLAN, 8, 10, 12, 14),
4525	ICE_PROTOCOL_ENTRY(ICE_GENEVE, 8, 10, 12, 14),
4526	ICE_PROTOCOL_ENTRY(ICE_NVGRE, 0, 2, 4, 6),
4527	ICE_PROTOCOL_ENTRY(ICE_GTP, 8, 10, 12, 14, 16, 18, 20, 22),
4528	ICE_PROTOCOL_ENTRY(ICE_GTP_NO_PAY, 8, 10, 12, 14),
 
4529	ICE_PROTOCOL_ENTRY(ICE_PPPOE, 0, 2, 4, 6),
4530	ICE_PROTOCOL_ENTRY(ICE_L2TPV3, 0, 2, 4, 6, 8, 10),
4531	ICE_PROTOCOL_ENTRY(ICE_VLAN_EX, 2, 0),
4532	ICE_PROTOCOL_ENTRY(ICE_VLAN_IN, 2, 0),
4533	ICE_PROTOCOL_ENTRY(ICE_HW_METADATA,
4534			   ICE_SOURCE_PORT_MDID_OFFSET,
4535			   ICE_PTYPE_MDID_OFFSET,
4536			   ICE_PACKET_LENGTH_MDID_OFFSET,
4537			   ICE_SOURCE_VSI_MDID_OFFSET,
4538			   ICE_PKT_VLAN_MDID_OFFSET,
4539			   ICE_PKT_TUNNEL_MDID_OFFSET,
4540			   ICE_PKT_TCP_MDID_OFFSET,
4541			   ICE_PKT_ERROR_MDID_OFFSET),
4542};
4543
4544static struct ice_protocol_entry ice_prot_id_tbl[ICE_PROTOCOL_LAST] = {
4545	{ ICE_MAC_OFOS,		ICE_MAC_OFOS_HW },
4546	{ ICE_MAC_IL,		ICE_MAC_IL_HW },
4547	{ ICE_ETYPE_OL,		ICE_ETYPE_OL_HW },
4548	{ ICE_ETYPE_IL,		ICE_ETYPE_IL_HW },
4549	{ ICE_VLAN_OFOS,	ICE_VLAN_OL_HW },
4550	{ ICE_IPV4_OFOS,	ICE_IPV4_OFOS_HW },
4551	{ ICE_IPV4_IL,		ICE_IPV4_IL_HW },
4552	{ ICE_IPV6_OFOS,	ICE_IPV6_OFOS_HW },
4553	{ ICE_IPV6_IL,		ICE_IPV6_IL_HW },
4554	{ ICE_TCP_IL,		ICE_TCP_IL_HW },
4555	{ ICE_UDP_OF,		ICE_UDP_OF_HW },
4556	{ ICE_UDP_ILOS,		ICE_UDP_ILOS_HW },
4557	{ ICE_VXLAN,		ICE_UDP_OF_HW },
4558	{ ICE_GENEVE,		ICE_UDP_OF_HW },
4559	{ ICE_NVGRE,		ICE_GRE_OF_HW },
4560	{ ICE_GTP,		ICE_UDP_OF_HW },
4561	{ ICE_GTP_NO_PAY,	ICE_UDP_ILOS_HW },
 
4562	{ ICE_PPPOE,		ICE_PPPOE_HW },
4563	{ ICE_L2TPV3,		ICE_L2TPV3_HW },
4564	{ ICE_VLAN_EX,          ICE_VLAN_OF_HW },
4565	{ ICE_VLAN_IN,          ICE_VLAN_OL_HW },
4566	{ ICE_HW_METADATA,      ICE_META_DATA_ID_HW },
4567};
4568
4569/**
4570 * ice_find_recp - find a recipe
4571 * @hw: pointer to the hardware structure
4572 * @lkup_exts: extension sequence to match
4573 * @rinfo: information regarding the rule e.g. priority and action info
 
4574 *
4575 * Returns index of matching recipe, or ICE_MAX_NUM_RECIPES if not found.
4576 */
4577static u16
4578ice_find_recp(struct ice_hw *hw, struct ice_prot_lkup_ext *lkup_exts,
4579	      const struct ice_adv_rule_info *rinfo)
4580{
4581	bool refresh_required = true;
4582	struct ice_sw_recipe *recp;
4583	u8 i;
4584
4585	/* Walk through existing recipes to find a match */
4586	recp = hw->switch_info->recp_list;
4587	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4588		/* If recipe was not created for this ID, in SW bookkeeping,
4589		 * check if FW has an entry for this recipe. If the FW has an
4590		 * entry update it in our SW bookkeeping and continue with the
4591		 * matching.
4592		 */
4593		if (!recp[i].recp_created)
4594			if (ice_get_recp_frm_fw(hw,
4595						hw->switch_info->recp_list, i,
4596						&refresh_required))
4597				continue;
4598
4599		/* Skip inverse action recipes */
4600		if (recp[i].root_buf && recp[i].root_buf->content.act_ctrl &
4601		    ICE_AQ_RECIPE_ACT_INV_ACT)
4602			continue;
4603
4604		/* if number of words we are looking for match */
4605		if (lkup_exts->n_val_words == recp[i].lkup_exts.n_val_words) {
4606			struct ice_fv_word *ar = recp[i].lkup_exts.fv_words;
4607			struct ice_fv_word *be = lkup_exts->fv_words;
4608			u16 *cr = recp[i].lkup_exts.field_mask;
4609			u16 *de = lkup_exts->field_mask;
4610			bool found = true;
4611			u8 pe, qr;
4612
4613			/* ar, cr, and qr are related to the recipe words, while
4614			 * be, de, and pe are related to the lookup words
4615			 */
4616			for (pe = 0; pe < lkup_exts->n_val_words; pe++) {
4617				for (qr = 0; qr < recp[i].lkup_exts.n_val_words;
4618				     qr++) {
4619					if (ar[qr].off == be[pe].off &&
4620					    ar[qr].prot_id == be[pe].prot_id &&
4621					    cr[qr] == de[pe])
4622						/* Found the "pe"th word in the
4623						 * given recipe
4624						 */
4625						break;
4626				}
4627				/* After walking through all the words in the
4628				 * "i"th recipe if "p"th word was not found then
4629				 * this recipe is not what we are looking for.
4630				 * So break out from this loop and try the next
4631				 * recipe
4632				 */
4633				if (qr >= recp[i].lkup_exts.n_val_words) {
4634					found = false;
4635					break;
4636				}
4637			}
4638			/* If for "i"th recipe the found was never set to false
4639			 * then it means we found our match
4640			 * Also tun type and *_pass_l2 of recipe needs to be
4641			 * checked
4642			 */
4643			if (found && recp[i].tun_type == rinfo->tun_type &&
4644			    recp[i].need_pass_l2 == rinfo->need_pass_l2 &&
4645			    recp[i].allow_pass_l2 == rinfo->allow_pass_l2)
4646				return i; /* Return the recipe ID */
4647		}
4648	}
4649	return ICE_MAX_NUM_RECIPES;
4650}
4651
4652/**
4653 * ice_change_proto_id_to_dvm - change proto id in prot_id_tbl
4654 *
4655 * As protocol id for outer vlan is different in dvm and svm, if dvm is
4656 * supported protocol array record for outer vlan has to be modified to
4657 * reflect the value proper for DVM.
4658 */
4659void ice_change_proto_id_to_dvm(void)
4660{
4661	u8 i;
4662
4663	for (i = 0; i < ARRAY_SIZE(ice_prot_id_tbl); i++)
4664		if (ice_prot_id_tbl[i].type == ICE_VLAN_OFOS &&
4665		    ice_prot_id_tbl[i].protocol_id != ICE_VLAN_OF_HW)
4666			ice_prot_id_tbl[i].protocol_id = ICE_VLAN_OF_HW;
4667}
4668
4669/**
4670 * ice_prot_type_to_id - get protocol ID from protocol type
4671 * @type: protocol type
4672 * @id: pointer to variable that will receive the ID
4673 *
4674 * Returns true if found, false otherwise
4675 */
4676static bool ice_prot_type_to_id(enum ice_protocol_type type, u8 *id)
4677{
4678	u8 i;
4679
4680	for (i = 0; i < ARRAY_SIZE(ice_prot_id_tbl); i++)
4681		if (ice_prot_id_tbl[i].type == type) {
4682			*id = ice_prot_id_tbl[i].protocol_id;
4683			return true;
4684		}
4685	return false;
4686}
4687
4688/**
4689 * ice_fill_valid_words - count valid words
4690 * @rule: advanced rule with lookup information
4691 * @lkup_exts: byte offset extractions of the words that are valid
4692 *
4693 * calculate valid words in a lookup rule using mask value
4694 */
4695static u8
4696ice_fill_valid_words(struct ice_adv_lkup_elem *rule,
4697		     struct ice_prot_lkup_ext *lkup_exts)
4698{
4699	u8 j, word, prot_id, ret_val;
4700
4701	if (!ice_prot_type_to_id(rule->type, &prot_id))
4702		return 0;
4703
4704	word = lkup_exts->n_val_words;
4705
4706	for (j = 0; j < sizeof(rule->m_u) / sizeof(u16); j++)
4707		if (((u16 *)&rule->m_u)[j] &&
4708		    rule->type < ARRAY_SIZE(ice_prot_ext)) {
4709			/* No more space to accommodate */
4710			if (word >= ICE_MAX_CHAIN_WORDS)
4711				return 0;
4712			lkup_exts->fv_words[word].off =
4713				ice_prot_ext[rule->type].offs[j];
4714			lkup_exts->fv_words[word].prot_id =
4715				ice_prot_id_tbl[rule->type].protocol_id;
4716			lkup_exts->field_mask[word] =
4717				be16_to_cpu(((__force __be16 *)&rule->m_u)[j]);
4718			word++;
4719		}
4720
4721	ret_val = word - lkup_exts->n_val_words;
4722	lkup_exts->n_val_words = word;
4723
4724	return ret_val;
4725}
4726
4727/**
4728 * ice_create_first_fit_recp_def - Create a recipe grouping
4729 * @hw: pointer to the hardware structure
4730 * @lkup_exts: an array of protocol header extractions
4731 * @rg_list: pointer to a list that stores new recipe groups
4732 * @recp_cnt: pointer to a variable that stores returned number of recipe groups
4733 *
4734 * Using first fit algorithm, take all the words that are still not done
4735 * and start grouping them in 4-word groups. Each group makes up one
4736 * recipe.
4737 */
4738static int
4739ice_create_first_fit_recp_def(struct ice_hw *hw,
4740			      struct ice_prot_lkup_ext *lkup_exts,
4741			      struct list_head *rg_list,
4742			      u8 *recp_cnt)
4743{
4744	struct ice_pref_recipe_group *grp = NULL;
4745	u8 j;
4746
4747	*recp_cnt = 0;
4748
4749	/* Walk through every word in the rule to check if it is not done. If so
4750	 * then this word needs to be part of a new recipe.
4751	 */
4752	for (j = 0; j < lkup_exts->n_val_words; j++)
4753		if (!test_bit(j, lkup_exts->done)) {
4754			if (!grp ||
4755			    grp->n_val_pairs == ICE_NUM_WORDS_RECIPE) {
4756				struct ice_recp_grp_entry *entry;
4757
4758				entry = devm_kzalloc(ice_hw_to_dev(hw),
4759						     sizeof(*entry),
4760						     GFP_KERNEL);
4761				if (!entry)
4762					return -ENOMEM;
4763				list_add(&entry->l_entry, rg_list);
4764				grp = &entry->r_group;
4765				(*recp_cnt)++;
4766			}
4767
4768			grp->pairs[grp->n_val_pairs].prot_id =
4769				lkup_exts->fv_words[j].prot_id;
4770			grp->pairs[grp->n_val_pairs].off =
4771				lkup_exts->fv_words[j].off;
4772			grp->mask[grp->n_val_pairs] = lkup_exts->field_mask[j];
4773			grp->n_val_pairs++;
4774		}
4775
4776	return 0;
4777}
4778
4779/**
4780 * ice_fill_fv_word_index - fill in the field vector indices for a recipe group
4781 * @hw: pointer to the hardware structure
4782 * @fv_list: field vector with the extraction sequence information
4783 * @rg_list: recipe groupings with protocol-offset pairs
4784 *
4785 * Helper function to fill in the field vector indices for protocol-offset
4786 * pairs. These indexes are then ultimately programmed into a recipe.
4787 */
4788static int
4789ice_fill_fv_word_index(struct ice_hw *hw, struct list_head *fv_list,
4790		       struct list_head *rg_list)
4791{
4792	struct ice_sw_fv_list_entry *fv;
4793	struct ice_recp_grp_entry *rg;
4794	struct ice_fv_word *fv_ext;
 
4795
4796	if (list_empty(fv_list))
4797		return 0;
4798
4799	fv = list_first_entry(fv_list, struct ice_sw_fv_list_entry,
4800			      list_entry);
4801	fv_ext = fv->fv_ptr->ew;
4802
4803	list_for_each_entry(rg, rg_list, l_entry) {
4804		u8 i;
 
 
 
 
 
 
 
 
4805
4806		for (i = 0; i < rg->r_group.n_val_pairs; i++) {
4807			struct ice_fv_word *pr;
4808			bool found = false;
4809			u16 mask;
4810			u8 j;
4811
4812			pr = &rg->r_group.pairs[i];
4813			mask = rg->r_group.mask[i];
4814
4815			for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
4816				if (fv_ext[j].prot_id == pr->prot_id &&
4817				    fv_ext[j].off == pr->off) {
4818					found = true;
4819
4820					/* Store index of field vector */
4821					rg->fv_idx[i] = j;
4822					rg->fv_mask[i] = mask;
4823					break;
4824				}
4825
4826			/* Protocol/offset could not be found, caller gave an
4827			 * invalid pair
4828			 */
4829			if (!found)
4830				return -EINVAL;
4831		}
 
 
 
 
 
 
4832	}
4833
4834	return 0;
4835}
4836
4837/**
4838 * ice_find_free_recp_res_idx - find free result indexes for recipe
4839 * @hw: pointer to hardware structure
4840 * @profiles: bitmap of profiles that will be associated with the new recipe
4841 * @free_idx: pointer to variable to receive the free index bitmap
4842 *
4843 * The algorithm used here is:
4844 *	1. When creating a new recipe, create a set P which contains all
4845 *	   Profiles that will be associated with our new recipe
4846 *
4847 *	2. For each Profile p in set P:
4848 *	    a. Add all recipes associated with Profile p into set R
4849 *	    b. Optional : PossibleIndexes &= profile[p].possibleIndexes
4850 *		[initially PossibleIndexes should be 0xFFFFFFFFFFFFFFFF]
4851 *		i. Or just assume they all have the same possible indexes:
4852 *			44, 45, 46, 47
4853 *			i.e., PossibleIndexes = 0x0000F00000000000
4854 *
4855 *	3. For each Recipe r in set R:
4856 *	    a. UsedIndexes |= (bitwise or ) recipe[r].res_indexes
4857 *	    b. FreeIndexes = UsedIndexes ^ PossibleIndexes
4858 *
4859 *	FreeIndexes will contain the bits indicating the indexes free for use,
4860 *      then the code needs to update the recipe[r].used_result_idx_bits to
4861 *      indicate which indexes were selected for use by this recipe.
4862 */
4863static u16
4864ice_find_free_recp_res_idx(struct ice_hw *hw, const unsigned long *profiles,
4865			   unsigned long *free_idx)
4866{
4867	DECLARE_BITMAP(possible_idx, ICE_MAX_FV_WORDS);
4868	DECLARE_BITMAP(recipes, ICE_MAX_NUM_RECIPES);
4869	DECLARE_BITMAP(used_idx, ICE_MAX_FV_WORDS);
4870	u16 bit;
4871
4872	bitmap_zero(recipes, ICE_MAX_NUM_RECIPES);
4873	bitmap_zero(used_idx, ICE_MAX_FV_WORDS);
4874
4875	bitmap_fill(possible_idx, ICE_MAX_FV_WORDS);
4876
4877	/* For each profile we are going to associate the recipe with, add the
4878	 * recipes that are associated with that profile. This will give us
4879	 * the set of recipes that our recipe may collide with. Also, determine
4880	 * what possible result indexes are usable given this set of profiles.
4881	 */
4882	for_each_set_bit(bit, profiles, ICE_MAX_NUM_PROFILES) {
4883		bitmap_or(recipes, recipes, profile_to_recipe[bit],
4884			  ICE_MAX_NUM_RECIPES);
4885		bitmap_and(possible_idx, possible_idx,
4886			   hw->switch_info->prof_res_bm[bit],
4887			   ICE_MAX_FV_WORDS);
4888	}
4889
4890	/* For each recipe that our new recipe may collide with, determine
4891	 * which indexes have been used.
4892	 */
4893	for_each_set_bit(bit, recipes, ICE_MAX_NUM_RECIPES)
4894		bitmap_or(used_idx, used_idx,
4895			  hw->switch_info->recp_list[bit].res_idxs,
4896			  ICE_MAX_FV_WORDS);
4897
4898	bitmap_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
4899
4900	/* return number of free indexes */
4901	return (u16)bitmap_weight(free_idx, ICE_MAX_FV_WORDS);
4902}
4903
4904/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4905 * ice_add_sw_recipe - function to call AQ calls to create switch recipe
4906 * @hw: pointer to hardware structure
4907 * @rm: recipe management list entry
4908 * @profiles: bitmap of profiles that will be associated.
4909 */
4910static int
4911ice_add_sw_recipe(struct ice_hw *hw, struct ice_sw_recipe *rm,
4912		  unsigned long *profiles)
4913{
 
4914	DECLARE_BITMAP(result_idx_bm, ICE_MAX_FV_WORDS);
4915	struct ice_aqc_recipe_content *content;
4916	struct ice_aqc_recipe_data_elem *tmp;
4917	struct ice_aqc_recipe_data_elem *buf;
4918	struct ice_recp_grp_entry *entry;
4919	u16 free_res_idx;
4920	u16 recipe_count;
4921	u8 chain_idx;
4922	u8 recps = 0;
4923	int status;
 
 
 
 
4924
4925	/* When more than one recipe are required, another recipe is needed to
4926	 * chain them together. Matching a tunnel metadata ID takes up one of
4927	 * the match fields in the chaining recipe reducing the number of
4928	 * chained recipes by one.
4929	 */
4930	 /* check number of free result indices */
4931	bitmap_zero(result_idx_bm, ICE_MAX_FV_WORDS);
 
 
 
4932	free_res_idx = ice_find_free_recp_res_idx(hw, profiles, result_idx_bm);
4933
4934	ice_debug(hw, ICE_DBG_SW, "Result idx slots: %d, need %d\n",
4935		  free_res_idx, rm->n_grp_count);
4936
4937	if (rm->n_grp_count > 1) {
4938		if (rm->n_grp_count > free_res_idx)
4939			return -ENOSPC;
4940
4941		rm->n_grp_count++;
4942	}
4943
4944	if (rm->n_grp_count > ICE_MAX_CHAIN_RECIPE)
4945		return -ENOSPC;
4946
4947	tmp = kcalloc(ICE_MAX_NUM_RECIPES, sizeof(*tmp), GFP_KERNEL);
4948	if (!tmp)
4949		return -ENOMEM;
4950
4951	buf = devm_kcalloc(ice_hw_to_dev(hw), rm->n_grp_count, sizeof(*buf),
4952			   GFP_KERNEL);
4953	if (!buf) {
4954		status = -ENOMEM;
4955		goto err_mem;
4956	}
4957
4958	bitmap_zero(rm->r_bitmap, ICE_MAX_NUM_RECIPES);
4959	recipe_count = ICE_MAX_NUM_RECIPES;
4960	status = ice_aq_get_recipe(hw, tmp, &recipe_count, ICE_SW_LKUP_MAC,
4961				   NULL);
4962	if (status || recipe_count == 0)
4963		goto err_unroll;
4964
4965	/* Allocate the recipe resources, and configure them according to the
4966	 * match fields from protocol headers and extracted field vectors.
4967	 */
4968	chain_idx = find_first_bit(result_idx_bm, ICE_MAX_FV_WORDS);
4969	list_for_each_entry(entry, &rm->rg_list, l_entry) {
4970		u8 i;
4971
4972		status = ice_alloc_recipe(hw, &entry->rid);
4973		if (status)
4974			goto err_unroll;
4975
4976		content = &buf[recps].content;
4977
4978		/* Clear the result index of the located recipe, as this will be
4979		 * updated, if needed, later in the recipe creation process.
 
4980		 */
4981		tmp[0].content.result_indx = 0;
4982
4983		buf[recps] = tmp[0];
4984		buf[recps].recipe_indx = (u8)entry->rid;
4985		/* if the recipe is a non-root recipe RID should be programmed
4986		 * as 0 for the rules to be applied correctly.
4987		 */
4988		content->rid = 0;
4989		memset(&content->lkup_indx, 0,
4990		       sizeof(content->lkup_indx));
4991
4992		/* All recipes use look-up index 0 to match switch ID. */
4993		content->lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
4994		content->mask[0] = cpu_to_le16(ICE_AQ_SW_ID_LKUP_MASK);
4995		/* Setup lkup_indx 1..4 to INVALID/ignore and set the mask
4996		 * to be 0
4997		 */
4998		for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
4999			content->lkup_indx[i] = 0x80;
5000			content->mask[i] = 0;
5001		}
 
5002
5003		for (i = 0; i < entry->r_group.n_val_pairs; i++) {
5004			content->lkup_indx[i + 1] = entry->fv_idx[i];
5005			content->mask[i + 1] = cpu_to_le16(entry->fv_mask[i]);
5006		}
5007
5008		if (rm->n_grp_count > 1) {
5009			/* Checks to see if there really is a valid result index
5010			 * that can be used.
5011			 */
5012			if (chain_idx >= ICE_MAX_FV_WORDS) {
5013				ice_debug(hw, ICE_DBG_SW, "No chain index available\n");
5014				status = -ENOSPC;
5015				goto err_unroll;
5016			}
5017
5018			entry->chain_idx = chain_idx;
5019			content->result_indx =
5020				ICE_AQ_RECIPE_RESULT_EN |
5021				FIELD_PREP(ICE_AQ_RECIPE_RESULT_DATA_M,
5022					   chain_idx);
5023			clear_bit(chain_idx, result_idx_bm);
5024			chain_idx = find_first_bit(result_idx_bm,
5025						   ICE_MAX_FV_WORDS);
5026		}
5027
5028		/* fill recipe dependencies */
5029		bitmap_zero((unsigned long *)buf[recps].recipe_bitmap,
5030			    ICE_MAX_NUM_RECIPES);
5031		set_bit(buf[recps].recipe_indx,
5032			(unsigned long *)buf[recps].recipe_bitmap);
5033		content->act_ctrl_fwd_priority = rm->priority;
5034
5035		if (rm->need_pass_l2)
5036			content->act_ctrl |= ICE_AQ_RECIPE_ACT_NEED_PASS_L2;
5037
5038		if (rm->allow_pass_l2)
5039			content->act_ctrl |= ICE_AQ_RECIPE_ACT_ALLOW_PASS_L2;
5040		recps++;
5041	}
5042
5043	if (rm->n_grp_count == 1) {
5044		rm->root_rid = buf[0].recipe_indx;
5045		set_bit(buf[0].recipe_indx, rm->r_bitmap);
5046		buf[0].content.rid = rm->root_rid | ICE_AQ_RECIPE_ID_IS_ROOT;
5047		if (sizeof(buf[0].recipe_bitmap) >= sizeof(rm->r_bitmap)) {
5048			memcpy(buf[0].recipe_bitmap, rm->r_bitmap,
5049			       sizeof(buf[0].recipe_bitmap));
5050		} else {
5051			status = -EINVAL;
5052			goto err_unroll;
5053		}
5054		/* Applicable only for ROOT_RECIPE, set the fwd_priority for
5055		 * the recipe which is getting created if specified
5056		 * by user. Usually any advanced switch filter, which results
5057		 * into new extraction sequence, ended up creating a new recipe
5058		 * of type ROOT and usually recipes are associated with profiles
5059		 * Switch rule referreing newly created recipe, needs to have
5060		 * either/or 'fwd' or 'join' priority, otherwise switch rule
5061		 * evaluation will not happen correctly. In other words, if
5062		 * switch rule to be evaluated on priority basis, then recipe
5063		 * needs to have priority, otherwise it will be evaluated last.
5064		 */
5065		buf[0].content.act_ctrl_fwd_priority = rm->priority;
5066	} else {
5067		struct ice_recp_grp_entry *last_chain_entry;
5068		u16 rid, i;
5069
5070		/* Allocate the last recipe that will chain the outcomes of the
5071		 * other recipes together
5072		 */
5073		status = ice_alloc_recipe(hw, &rid);
5074		if (status)
5075			goto err_unroll;
5076
5077		content = &buf[recps].content;
5078
5079		buf[recps].recipe_indx = (u8)rid;
5080		content->rid = (u8)rid;
5081		content->rid |= ICE_AQ_RECIPE_ID_IS_ROOT;
5082		/* the new entry created should also be part of rg_list to
5083		 * make sure we have complete recipe
5084		 */
5085		last_chain_entry = devm_kzalloc(ice_hw_to_dev(hw),
5086						sizeof(*last_chain_entry),
5087						GFP_KERNEL);
5088		if (!last_chain_entry) {
5089			status = -ENOMEM;
5090			goto err_unroll;
5091		}
5092		last_chain_entry->rid = rid;
5093		memset(&content->lkup_indx, 0, sizeof(content->lkup_indx));
5094		/* All recipes use look-up index 0 to match switch ID. */
5095		content->lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
5096		content->mask[0] = cpu_to_le16(ICE_AQ_SW_ID_LKUP_MASK);
5097		for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
5098			content->lkup_indx[i] = ICE_AQ_RECIPE_LKUP_IGNORE;
5099			content->mask[i] = 0;
5100		}
5101
5102		i = 1;
5103		/* update r_bitmap with the recp that is used for chaining */
5104		set_bit(rid, rm->r_bitmap);
5105		/* this is the recipe that chains all the other recipes so it
5106		 * should not have a chaining ID to indicate the same
5107		 */
5108		last_chain_entry->chain_idx = ICE_INVAL_CHAIN_IND;
5109		list_for_each_entry(entry, &rm->rg_list, l_entry) {
5110			last_chain_entry->fv_idx[i] = entry->chain_idx;
5111			content->lkup_indx[i] = entry->chain_idx;
5112			content->mask[i++] = cpu_to_le16(0xFFFF);
5113			set_bit(entry->rid, rm->r_bitmap);
5114		}
5115		list_add(&last_chain_entry->l_entry, &rm->rg_list);
5116		if (sizeof(buf[recps].recipe_bitmap) >=
5117		    sizeof(rm->r_bitmap)) {
5118			memcpy(buf[recps].recipe_bitmap, rm->r_bitmap,
5119			       sizeof(buf[recps].recipe_bitmap));
5120		} else {
5121			status = -EINVAL;
5122			goto err_unroll;
5123		}
5124		content->act_ctrl_fwd_priority = rm->priority;
5125
5126		recps++;
5127		rm->root_rid = (u8)rid;
5128	}
5129	status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
5130	if (status)
5131		goto err_unroll;
5132
5133	status = ice_aq_add_recipe(hw, buf, rm->n_grp_count, NULL);
5134	ice_release_change_lock(hw);
5135	if (status)
5136		goto err_unroll;
 
 
 
5137
5138	/* Every recipe that just got created add it to the recipe
5139	 * book keeping list
5140	 */
5141	list_for_each_entry(entry, &rm->rg_list, l_entry) {
5142		struct ice_switch_info *sw = hw->switch_info;
5143		bool is_root, idx_found = false;
5144		struct ice_sw_recipe *recp;
5145		u16 idx, buf_idx = 0;
5146
5147		/* find buffer index for copying some data */
5148		for (idx = 0; idx < rm->n_grp_count; idx++)
5149			if (buf[idx].recipe_indx == entry->rid) {
5150				buf_idx = idx;
5151				idx_found = true;
5152			}
5153
5154		if (!idx_found) {
5155			status = -EIO;
5156			goto err_unroll;
5157		}
5158
5159		recp = &sw->recp_list[entry->rid];
5160		is_root = (rm->root_rid == entry->rid);
5161		recp->is_root = is_root;
5162
5163		recp->root_rid = entry->rid;
5164		recp->big_recp = (is_root && rm->n_grp_count > 1);
5165
5166		memcpy(&recp->ext_words, entry->r_group.pairs,
5167		       entry->r_group.n_val_pairs * sizeof(struct ice_fv_word));
5168
5169		memcpy(recp->r_bitmap, buf[buf_idx].recipe_bitmap,
5170		       sizeof(recp->r_bitmap));
5171
5172		/* Copy non-result fv index values and masks to recipe. This
5173		 * call will also update the result recipe bitmask.
 
 
 
 
 
 
 
 
 
5174		 */
5175		ice_collect_result_idx(&buf[buf_idx], recp);
5176
5177		/* for non-root recipes, also copy to the root, this allows
5178		 * easier matching of a complete chained recipe
5179		 */
5180		if (!is_root)
5181			ice_collect_result_idx(&buf[buf_idx],
5182					       &sw->recp_list[rm->root_rid]);
5183
5184		recp->n_ext_words = entry->r_group.n_val_pairs;
5185		recp->chain_idx = entry->chain_idx;
5186		recp->priority = buf[buf_idx].content.act_ctrl_fwd_priority;
5187		recp->n_grp_count = rm->n_grp_count;
5188		recp->tun_type = rm->tun_type;
5189		recp->need_pass_l2 = rm->need_pass_l2;
5190		recp->allow_pass_l2 = rm->allow_pass_l2;
5191		recp->recp_created = true;
5192	}
5193	rm->root_buf = buf;
5194	kfree(tmp);
5195	return status;
5196
5197err_unroll:
5198err_mem:
5199	kfree(tmp);
5200	devm_kfree(ice_hw_to_dev(hw), buf);
5201	return status;
5202}
5203
5204/**
5205 * ice_create_recipe_group - creates recipe group
5206 * @hw: pointer to hardware structure
5207 * @rm: recipe management list entry
5208 * @lkup_exts: lookup elements
5209 */
5210static int
5211ice_create_recipe_group(struct ice_hw *hw, struct ice_sw_recipe *rm,
5212			struct ice_prot_lkup_ext *lkup_exts)
5213{
5214	u8 recp_count = 0;
5215	int status;
5216
5217	rm->n_grp_count = 0;
 
 
 
5218
5219	/* Create recipes for words that are marked not done by packing them
5220	 * as best fit.
5221	 */
5222	status = ice_create_first_fit_recp_def(hw, lkup_exts,
5223					       &rm->rg_list, &recp_count);
5224	if (!status) {
5225		rm->n_grp_count += recp_count;
5226		rm->n_ext_words = lkup_exts->n_val_words;
5227		memcpy(&rm->ext_words, lkup_exts->fv_words,
5228		       sizeof(rm->ext_words));
5229		memcpy(rm->word_masks, lkup_exts->field_mask,
5230		       sizeof(rm->word_masks));
5231	}
5232
5233	return status;
5234}
5235
5236/* ice_get_compat_fv_bitmap - Get compatible field vector bitmap for rule
5237 * @hw: pointer to hardware structure
5238 * @rinfo: other information regarding the rule e.g. priority and action info
5239 * @bm: pointer to memory for returning the bitmap of field vectors
5240 */
5241static void
5242ice_get_compat_fv_bitmap(struct ice_hw *hw, struct ice_adv_rule_info *rinfo,
5243			 unsigned long *bm)
5244{
5245	enum ice_prof_type prof_type;
5246
5247	bitmap_zero(bm, ICE_MAX_NUM_PROFILES);
5248
5249	switch (rinfo->tun_type) {
5250	case ICE_NON_TUN:
5251		prof_type = ICE_PROF_NON_TUN;
5252		break;
5253	case ICE_ALL_TUNNELS:
5254		prof_type = ICE_PROF_TUN_ALL;
5255		break;
5256	case ICE_SW_TUN_GENEVE:
5257	case ICE_SW_TUN_VXLAN:
5258		prof_type = ICE_PROF_TUN_UDP;
5259		break;
5260	case ICE_SW_TUN_NVGRE:
5261		prof_type = ICE_PROF_TUN_GRE;
5262		break;
5263	case ICE_SW_TUN_GTPU:
5264		prof_type = ICE_PROF_TUN_GTPU;
5265		break;
5266	case ICE_SW_TUN_GTPC:
5267		prof_type = ICE_PROF_TUN_GTPC;
5268		break;
 
 
 
5269	case ICE_SW_TUN_AND_NON_TUN:
5270	default:
5271		prof_type = ICE_PROF_ALL;
5272		break;
5273	}
5274
5275	ice_get_sw_fv_bitmap(hw, prof_type, bm);
5276}
5277
5278/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5279 * ice_add_adv_recipe - Add an advanced recipe that is not part of the default
5280 * @hw: pointer to hardware structure
5281 * @lkups: lookup elements or match criteria for the advanced recipe, one
5282 *  structure per protocol header
5283 * @lkups_cnt: number of protocols
5284 * @rinfo: other information regarding the rule e.g. priority and action info
5285 * @rid: return the recipe ID of the recipe created
5286 */
5287static int
5288ice_add_adv_recipe(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5289		   u16 lkups_cnt, struct ice_adv_rule_info *rinfo, u16 *rid)
5290{
5291	DECLARE_BITMAP(fv_bitmap, ICE_MAX_NUM_PROFILES);
5292	DECLARE_BITMAP(profiles, ICE_MAX_NUM_PROFILES);
5293	struct ice_prot_lkup_ext *lkup_exts;
5294	struct ice_recp_grp_entry *r_entry;
5295	struct ice_sw_fv_list_entry *fvit;
5296	struct ice_recp_grp_entry *r_tmp;
5297	struct ice_sw_fv_list_entry *tmp;
5298	struct ice_sw_recipe *rm;
5299	int status = 0;
 
5300	u8 i;
5301
5302	if (!lkups_cnt)
5303		return -EINVAL;
5304
5305	lkup_exts = kzalloc(sizeof(*lkup_exts), GFP_KERNEL);
5306	if (!lkup_exts)
5307		return -ENOMEM;
5308
5309	/* Determine the number of words to be matched and if it exceeds a
5310	 * recipe's restrictions
5311	 */
5312	for (i = 0; i < lkups_cnt; i++) {
5313		u16 count;
5314
5315		if (lkups[i].type >= ICE_PROTOCOL_LAST) {
5316			status = -EIO;
5317			goto err_free_lkup_exts;
5318		}
5319
5320		count = ice_fill_valid_words(&lkups[i], lkup_exts);
5321		if (!count) {
5322			status = -EIO;
5323			goto err_free_lkup_exts;
5324		}
5325	}
5326
5327	rm = kzalloc(sizeof(*rm), GFP_KERNEL);
5328	if (!rm) {
5329		status = -ENOMEM;
5330		goto err_free_lkup_exts;
5331	}
5332
5333	/* Get field vectors that contain fields extracted from all the protocol
5334	 * headers being programmed.
5335	 */
5336	INIT_LIST_HEAD(&rm->fv_list);
5337	INIT_LIST_HEAD(&rm->rg_list);
5338
5339	/* Get bitmap of field vectors (profiles) that are compatible with the
5340	 * rule request; only these will be searched in the subsequent call to
5341	 * ice_get_sw_fv_list.
5342	 */
5343	ice_get_compat_fv_bitmap(hw, rinfo, fv_bitmap);
5344
5345	status = ice_get_sw_fv_list(hw, lkup_exts, fv_bitmap, &rm->fv_list);
5346	if (status)
5347		goto err_unroll;
5348
5349	/* Group match words into recipes using preferred recipe grouping
5350	 * criteria.
5351	 */
5352	status = ice_create_recipe_group(hw, rm, lkup_exts);
5353	if (status)
5354		goto err_unroll;
5355
5356	/* set the recipe priority if specified */
5357	rm->priority = (u8)rinfo->priority;
5358
5359	rm->need_pass_l2 = rinfo->need_pass_l2;
5360	rm->allow_pass_l2 = rinfo->allow_pass_l2;
5361
5362	/* Find offsets from the field vector. Pick the first one for all the
5363	 * recipes.
5364	 */
5365	status = ice_fill_fv_word_index(hw, &rm->fv_list, &rm->rg_list);
5366	if (status)
5367		goto err_unroll;
5368
5369	/* get bitmap of all profiles the recipe will be associated with */
5370	bitmap_zero(profiles, ICE_MAX_NUM_PROFILES);
5371	list_for_each_entry(fvit, &rm->fv_list, list_entry) {
5372		ice_debug(hw, ICE_DBG_SW, "profile: %d\n", fvit->profile_id);
5373		set_bit((u16)fvit->profile_id, profiles);
5374	}
5375
5376	/* Look for a recipe which matches our requested fv / mask list */
5377	*rid = ice_find_recp(hw, lkup_exts, rinfo);
5378	if (*rid < ICE_MAX_NUM_RECIPES)
5379		/* Success if found a recipe that match the existing criteria */
 
 
 
5380		goto err_unroll;
 
5381
5382	rm->tun_type = rinfo->tun_type;
5383	/* Recipe we need does not exist, add a recipe */
5384	status = ice_add_sw_recipe(hw, rm, profiles);
5385	if (status)
5386		goto err_unroll;
5387
5388	/* Associate all the recipes created with all the profiles in the
5389	 * common field vector.
5390	 */
5391	list_for_each_entry(fvit, &rm->fv_list, list_entry) {
5392		DECLARE_BITMAP(r_bitmap, ICE_MAX_NUM_RECIPES);
 
5393		u16 j;
5394
5395		status = ice_aq_get_recipe_to_profile(hw, fvit->profile_id,
5396						      (u8 *)r_bitmap, NULL);
5397		if (status)
5398			goto err_unroll;
5399
 
5400		bitmap_or(r_bitmap, r_bitmap, rm->r_bitmap,
5401			  ICE_MAX_NUM_RECIPES);
5402		status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
5403		if (status)
5404			goto err_unroll;
5405
 
5406		status = ice_aq_map_recipe_to_profile(hw, fvit->profile_id,
5407						      (u8 *)r_bitmap,
5408						      NULL);
5409		ice_release_change_lock(hw);
5410
5411		if (status)
5412			goto err_unroll;
5413
5414		/* Update profile to recipe bitmap array */
5415		bitmap_copy(profile_to_recipe[fvit->profile_id], r_bitmap,
5416			    ICE_MAX_NUM_RECIPES);
5417
5418		/* Update recipe to profile bitmap array */
5419		for_each_set_bit(j, rm->r_bitmap, ICE_MAX_NUM_RECIPES)
5420			set_bit((u16)fvit->profile_id, recipe_to_profile[j]);
5421	}
5422
5423	*rid = rm->root_rid;
5424	memcpy(&hw->switch_info->recp_list[*rid].lkup_exts, lkup_exts,
5425	       sizeof(*lkup_exts));
5426err_unroll:
5427	list_for_each_entry_safe(r_entry, r_tmp, &rm->rg_list, l_entry) {
5428		list_del(&r_entry->l_entry);
5429		devm_kfree(ice_hw_to_dev(hw), r_entry);
 
 
 
 
5430	}
5431
 
5432	list_for_each_entry_safe(fvit, tmp, &rm->fv_list, list_entry) {
5433		list_del(&fvit->list_entry);
5434		devm_kfree(ice_hw_to_dev(hw), fvit);
5435	}
5436
5437	devm_kfree(ice_hw_to_dev(hw), rm->root_buf);
5438	kfree(rm);
5439
5440err_free_lkup_exts:
5441	kfree(lkup_exts);
5442
5443	return status;
5444}
5445
5446/**
5447 * ice_dummy_packet_add_vlan - insert VLAN header to dummy pkt
5448 *
5449 * @dummy_pkt: dummy packet profile pattern to which VLAN tag(s) will be added
5450 * @num_vlan: number of VLAN tags
5451 */
5452static struct ice_dummy_pkt_profile *
5453ice_dummy_packet_add_vlan(const struct ice_dummy_pkt_profile *dummy_pkt,
5454			  u32 num_vlan)
5455{
5456	struct ice_dummy_pkt_profile *profile;
5457	struct ice_dummy_pkt_offsets *offsets;
5458	u32 buf_len, off, etype_off, i;
5459	u8 *pkt;
5460
5461	if (num_vlan < 1 || num_vlan > 2)
5462		return ERR_PTR(-EINVAL);
5463
5464	off = num_vlan * VLAN_HLEN;
5465
5466	buf_len = array_size(num_vlan, sizeof(ice_dummy_vlan_packet_offsets)) +
5467		  dummy_pkt->offsets_len;
5468	offsets = kzalloc(buf_len, GFP_KERNEL);
5469	if (!offsets)
5470		return ERR_PTR(-ENOMEM);
5471
5472	offsets[0] = dummy_pkt->offsets[0];
5473	if (num_vlan == 2) {
5474		offsets[1] = ice_dummy_qinq_packet_offsets[0];
5475		offsets[2] = ice_dummy_qinq_packet_offsets[1];
5476	} else if (num_vlan == 1) {
5477		offsets[1] = ice_dummy_vlan_packet_offsets[0];
5478	}
5479
5480	for (i = 1; dummy_pkt->offsets[i].type != ICE_PROTOCOL_LAST; i++) {
5481		offsets[i + num_vlan].type = dummy_pkt->offsets[i].type;
5482		offsets[i + num_vlan].offset =
5483			dummy_pkt->offsets[i].offset + off;
5484	}
5485	offsets[i + num_vlan] = dummy_pkt->offsets[i];
5486
5487	etype_off = dummy_pkt->offsets[1].offset;
5488
5489	buf_len = array_size(num_vlan, sizeof(ice_dummy_vlan_packet)) +
5490		  dummy_pkt->pkt_len;
5491	pkt = kzalloc(buf_len, GFP_KERNEL);
5492	if (!pkt) {
5493		kfree(offsets);
5494		return ERR_PTR(-ENOMEM);
5495	}
5496
5497	memcpy(pkt, dummy_pkt->pkt, etype_off);
5498	memcpy(pkt + etype_off,
5499	       num_vlan == 2 ? ice_dummy_qinq_packet : ice_dummy_vlan_packet,
5500	       off);
5501	memcpy(pkt + etype_off + off, dummy_pkt->pkt + etype_off,
5502	       dummy_pkt->pkt_len - etype_off);
5503
5504	profile = kzalloc(sizeof(*profile), GFP_KERNEL);
5505	if (!profile) {
5506		kfree(offsets);
5507		kfree(pkt);
5508		return ERR_PTR(-ENOMEM);
5509	}
5510
5511	profile->offsets = offsets;
5512	profile->pkt = pkt;
5513	profile->pkt_len = buf_len;
5514	profile->match |= ICE_PKT_KMALLOC;
5515
5516	return profile;
5517}
5518
5519/**
5520 * ice_find_dummy_packet - find dummy packet
5521 *
5522 * @lkups: lookup elements or match criteria for the advanced recipe, one
5523 *	   structure per protocol header
5524 * @lkups_cnt: number of protocols
5525 * @tun_type: tunnel type
5526 *
5527 * Returns the &ice_dummy_pkt_profile corresponding to these lookup params.
5528 */
5529static const struct ice_dummy_pkt_profile *
5530ice_find_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5531		      enum ice_sw_tunnel_type tun_type)
5532{
5533	const struct ice_dummy_pkt_profile *ret = ice_dummy_pkt_profiles;
5534	u32 match = 0, vlan_count = 0;
5535	u16 i;
5536
5537	switch (tun_type) {
5538	case ICE_SW_TUN_GTPC:
5539		match |= ICE_PKT_TUN_GTPC;
5540		break;
5541	case ICE_SW_TUN_GTPU:
5542		match |= ICE_PKT_TUN_GTPU;
5543		break;
5544	case ICE_SW_TUN_NVGRE:
5545		match |= ICE_PKT_TUN_NVGRE;
5546		break;
5547	case ICE_SW_TUN_GENEVE:
5548	case ICE_SW_TUN_VXLAN:
5549		match |= ICE_PKT_TUN_UDP;
5550		break;
 
 
 
5551	default:
5552		break;
5553	}
5554
5555	for (i = 0; i < lkups_cnt; i++) {
5556		if (lkups[i].type == ICE_UDP_ILOS)
5557			match |= ICE_PKT_INNER_UDP;
5558		else if (lkups[i].type == ICE_TCP_IL)
5559			match |= ICE_PKT_INNER_TCP;
5560		else if (lkups[i].type == ICE_IPV6_OFOS)
5561			match |= ICE_PKT_OUTER_IPV6;
5562		else if (lkups[i].type == ICE_VLAN_OFOS ||
5563			 lkups[i].type == ICE_VLAN_EX)
5564			vlan_count++;
5565		else if (lkups[i].type == ICE_VLAN_IN)
5566			vlan_count++;
5567		else if (lkups[i].type == ICE_ETYPE_OL &&
5568			 lkups[i].h_u.ethertype.ethtype_id ==
5569				cpu_to_be16(ICE_IPV6_ETHER_ID) &&
5570			 lkups[i].m_u.ethertype.ethtype_id ==
5571				cpu_to_be16(0xFFFF))
5572			match |= ICE_PKT_OUTER_IPV6;
5573		else if (lkups[i].type == ICE_ETYPE_IL &&
5574			 lkups[i].h_u.ethertype.ethtype_id ==
5575				cpu_to_be16(ICE_IPV6_ETHER_ID) &&
5576			 lkups[i].m_u.ethertype.ethtype_id ==
5577				cpu_to_be16(0xFFFF))
5578			match |= ICE_PKT_INNER_IPV6;
5579		else if (lkups[i].type == ICE_IPV6_IL)
5580			match |= ICE_PKT_INNER_IPV6;
5581		else if (lkups[i].type == ICE_GTP_NO_PAY)
5582			match |= ICE_PKT_GTP_NOPAY;
5583		else if (lkups[i].type == ICE_PPPOE) {
5584			match |= ICE_PKT_PPPOE;
5585			if (lkups[i].h_u.pppoe_hdr.ppp_prot_id ==
5586			    htons(PPP_IPV6))
5587				match |= ICE_PKT_OUTER_IPV6;
5588		} else if (lkups[i].type == ICE_L2TPV3)
5589			match |= ICE_PKT_L2TPV3;
5590	}
5591
5592	while (ret->match && (match & ret->match) != ret->match)
5593		ret++;
5594
5595	if (vlan_count != 0)
5596		ret = ice_dummy_packet_add_vlan(ret, vlan_count);
5597
5598	return ret;
5599}
5600
5601/**
5602 * ice_fill_adv_dummy_packet - fill a dummy packet with given match criteria
5603 *
5604 * @lkups: lookup elements or match criteria for the advanced recipe, one
5605 *	   structure per protocol header
5606 * @lkups_cnt: number of protocols
5607 * @s_rule: stores rule information from the match criteria
5608 * @profile: dummy packet profile (the template, its size and header offsets)
5609 */
5610static int
5611ice_fill_adv_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5612			  struct ice_sw_rule_lkup_rx_tx *s_rule,
5613			  const struct ice_dummy_pkt_profile *profile)
5614{
5615	u8 *pkt;
5616	u16 i;
5617
5618	/* Start with a packet with a pre-defined/dummy content. Then, fill
5619	 * in the header values to be looked up or matched.
5620	 */
5621	pkt = s_rule->hdr_data;
5622
5623	memcpy(pkt, profile->pkt, profile->pkt_len);
5624
5625	for (i = 0; i < lkups_cnt; i++) {
5626		const struct ice_dummy_pkt_offsets *offsets = profile->offsets;
5627		enum ice_protocol_type type;
5628		u16 offset = 0, len = 0, j;
5629		bool found = false;
5630
5631		/* find the start of this layer; it should be found since this
5632		 * was already checked when search for the dummy packet
5633		 */
5634		type = lkups[i].type;
5635		/* metadata isn't present in the packet */
5636		if (type == ICE_HW_METADATA)
5637			continue;
5638
5639		for (j = 0; offsets[j].type != ICE_PROTOCOL_LAST; j++) {
5640			if (type == offsets[j].type) {
5641				offset = offsets[j].offset;
5642				found = true;
5643				break;
5644			}
5645		}
5646		/* this should never happen in a correct calling sequence */
5647		if (!found)
5648			return -EINVAL;
5649
5650		switch (lkups[i].type) {
5651		case ICE_MAC_OFOS:
5652		case ICE_MAC_IL:
5653			len = sizeof(struct ice_ether_hdr);
5654			break;
5655		case ICE_ETYPE_OL:
5656		case ICE_ETYPE_IL:
5657			len = sizeof(struct ice_ethtype_hdr);
5658			break;
5659		case ICE_VLAN_OFOS:
5660		case ICE_VLAN_EX:
5661		case ICE_VLAN_IN:
5662			len = sizeof(struct ice_vlan_hdr);
5663			break;
5664		case ICE_IPV4_OFOS:
5665		case ICE_IPV4_IL:
5666			len = sizeof(struct ice_ipv4_hdr);
5667			break;
5668		case ICE_IPV6_OFOS:
5669		case ICE_IPV6_IL:
5670			len = sizeof(struct ice_ipv6_hdr);
5671			break;
5672		case ICE_TCP_IL:
5673		case ICE_UDP_OF:
5674		case ICE_UDP_ILOS:
5675			len = sizeof(struct ice_l4_hdr);
5676			break;
5677		case ICE_SCTP_IL:
5678			len = sizeof(struct ice_sctp_hdr);
5679			break;
5680		case ICE_NVGRE:
5681			len = sizeof(struct ice_nvgre_hdr);
5682			break;
5683		case ICE_VXLAN:
5684		case ICE_GENEVE:
5685			len = sizeof(struct ice_udp_tnl_hdr);
5686			break;
5687		case ICE_GTP_NO_PAY:
5688		case ICE_GTP:
5689			len = sizeof(struct ice_udp_gtp_hdr);
5690			break;
 
 
 
5691		case ICE_PPPOE:
5692			len = sizeof(struct ice_pppoe_hdr);
5693			break;
5694		case ICE_L2TPV3:
5695			len = sizeof(struct ice_l2tpv3_sess_hdr);
5696			break;
5697		default:
5698			return -EINVAL;
5699		}
5700
5701		/* the length should be a word multiple */
5702		if (len % ICE_BYTES_PER_WORD)
5703			return -EIO;
5704
5705		/* We have the offset to the header start, the length, the
5706		 * caller's header values and mask. Use this information to
5707		 * copy the data into the dummy packet appropriately based on
5708		 * the mask. Note that we need to only write the bits as
5709		 * indicated by the mask to make sure we don't improperly write
5710		 * over any significant packet data.
5711		 */
5712		for (j = 0; j < len / sizeof(u16); j++) {
5713			u16 *ptr = (u16 *)(pkt + offset);
5714			u16 mask = lkups[i].m_raw[j];
5715
5716			if (!mask)
5717				continue;
5718
5719			ptr[j] = (ptr[j] & ~mask) | (lkups[i].h_raw[j] & mask);
5720		}
5721	}
5722
5723	s_rule->hdr_len = cpu_to_le16(profile->pkt_len);
5724
5725	return 0;
5726}
5727
5728/**
5729 * ice_fill_adv_packet_tun - fill dummy packet with udp tunnel port
5730 * @hw: pointer to the hardware structure
5731 * @tun_type: tunnel type
5732 * @pkt: dummy packet to fill in
5733 * @offsets: offset info for the dummy packet
5734 */
5735static int
5736ice_fill_adv_packet_tun(struct ice_hw *hw, enum ice_sw_tunnel_type tun_type,
5737			u8 *pkt, const struct ice_dummy_pkt_offsets *offsets)
5738{
5739	u16 open_port, i;
5740
5741	switch (tun_type) {
5742	case ICE_SW_TUN_VXLAN:
5743		if (!ice_get_open_tunnel_port(hw, &open_port, TNL_VXLAN))
5744			return -EIO;
5745		break;
5746	case ICE_SW_TUN_GENEVE:
5747		if (!ice_get_open_tunnel_port(hw, &open_port, TNL_GENEVE))
5748			return -EIO;
5749		break;
5750	default:
5751		/* Nothing needs to be done for this tunnel type */
5752		return 0;
5753	}
5754
5755	/* Find the outer UDP protocol header and insert the port number */
5756	for (i = 0; offsets[i].type != ICE_PROTOCOL_LAST; i++) {
5757		if (offsets[i].type == ICE_UDP_OF) {
5758			struct ice_l4_hdr *hdr;
5759			u16 offset;
5760
5761			offset = offsets[i].offset;
5762			hdr = (struct ice_l4_hdr *)&pkt[offset];
5763			hdr->dst_port = cpu_to_be16(open_port);
5764
5765			return 0;
5766		}
5767	}
5768
5769	return -EIO;
5770}
5771
5772/**
5773 * ice_fill_adv_packet_vlan - fill dummy packet with VLAN tag type
5774 * @hw: pointer to hw structure
5775 * @vlan_type: VLAN tag type
5776 * @pkt: dummy packet to fill in
5777 * @offsets: offset info for the dummy packet
5778 */
5779static int
5780ice_fill_adv_packet_vlan(struct ice_hw *hw, u16 vlan_type, u8 *pkt,
5781			 const struct ice_dummy_pkt_offsets *offsets)
5782{
5783	u16 i;
5784
5785	/* Check if there is something to do */
5786	if (!vlan_type || !ice_is_dvm_ena(hw))
5787		return 0;
5788
5789	/* Find VLAN header and insert VLAN TPID */
5790	for (i = 0; offsets[i].type != ICE_PROTOCOL_LAST; i++) {
5791		if (offsets[i].type == ICE_VLAN_OFOS ||
5792		    offsets[i].type == ICE_VLAN_EX) {
5793			struct ice_vlan_hdr *hdr;
5794			u16 offset;
5795
5796			offset = offsets[i].offset;
5797			hdr = (struct ice_vlan_hdr *)&pkt[offset];
5798			hdr->type = cpu_to_be16(vlan_type);
5799
5800			return 0;
5801		}
5802	}
5803
5804	return -EIO;
5805}
5806
5807static bool ice_rules_equal(const struct ice_adv_rule_info *first,
5808			    const struct ice_adv_rule_info *second)
5809{
5810	return first->sw_act.flag == second->sw_act.flag &&
5811	       first->tun_type == second->tun_type &&
5812	       first->vlan_type == second->vlan_type &&
5813	       first->src_vsi == second->src_vsi &&
5814	       first->need_pass_l2 == second->need_pass_l2 &&
5815	       first->allow_pass_l2 == second->allow_pass_l2;
5816}
5817
5818/**
5819 * ice_find_adv_rule_entry - Search a rule entry
5820 * @hw: pointer to the hardware structure
5821 * @lkups: lookup elements or match criteria for the advanced recipe, one
5822 *	   structure per protocol header
5823 * @lkups_cnt: number of protocols
5824 * @recp_id: recipe ID for which we are finding the rule
5825 * @rinfo: other information regarding the rule e.g. priority and action info
5826 *
5827 * Helper function to search for a given advance rule entry
5828 * Returns pointer to entry storing the rule if found
5829 */
5830static struct ice_adv_fltr_mgmt_list_entry *
5831ice_find_adv_rule_entry(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5832			u16 lkups_cnt, u16 recp_id,
5833			struct ice_adv_rule_info *rinfo)
5834{
5835	struct ice_adv_fltr_mgmt_list_entry *list_itr;
5836	struct ice_switch_info *sw = hw->switch_info;
5837	int i;
5838
5839	list_for_each_entry(list_itr, &sw->recp_list[recp_id].filt_rules,
5840			    list_entry) {
5841		bool lkups_matched = true;
5842
5843		if (lkups_cnt != list_itr->lkups_cnt)
5844			continue;
5845		for (i = 0; i < list_itr->lkups_cnt; i++)
5846			if (memcmp(&list_itr->lkups[i], &lkups[i],
5847				   sizeof(*lkups))) {
5848				lkups_matched = false;
5849				break;
5850			}
5851		if (ice_rules_equal(rinfo, &list_itr->rule_info) &&
5852		    lkups_matched)
5853			return list_itr;
5854	}
5855	return NULL;
5856}
5857
5858/**
5859 * ice_adv_add_update_vsi_list
5860 * @hw: pointer to the hardware structure
5861 * @m_entry: pointer to current adv filter management list entry
5862 * @cur_fltr: filter information from the book keeping entry
5863 * @new_fltr: filter information with the new VSI to be added
5864 *
5865 * Call AQ command to add or update previously created VSI list with new VSI.
5866 *
5867 * Helper function to do book keeping associated with adding filter information
5868 * The algorithm to do the booking keeping is described below :
5869 * When a VSI needs to subscribe to a given advanced filter
5870 *	if only one VSI has been added till now
5871 *		Allocate a new VSI list and add two VSIs
5872 *		to this list using switch rule command
5873 *		Update the previously created switch rule with the
5874 *		newly created VSI list ID
5875 *	if a VSI list was previously created
5876 *		Add the new VSI to the previously created VSI list set
5877 *		using the update switch rule command
5878 */
5879static int
5880ice_adv_add_update_vsi_list(struct ice_hw *hw,
5881			    struct ice_adv_fltr_mgmt_list_entry *m_entry,
5882			    struct ice_adv_rule_info *cur_fltr,
5883			    struct ice_adv_rule_info *new_fltr)
5884{
5885	u16 vsi_list_id = 0;
5886	int status;
5887
5888	if (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5889	    cur_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP ||
5890	    cur_fltr->sw_act.fltr_act == ICE_DROP_PACKET)
5891		return -EOPNOTSUPP;
5892
5893	if ((new_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5894	     new_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP) &&
5895	    (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI ||
5896	     cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI_LIST))
5897		return -EOPNOTSUPP;
5898
5899	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
5900		 /* Only one entry existed in the mapping and it was not already
5901		  * a part of a VSI list. So, create a VSI list with the old and
5902		  * new VSIs.
5903		  */
5904		struct ice_fltr_info tmp_fltr;
5905		u16 vsi_handle_arr[2];
5906
5907		/* A rule already exists with the new VSI being added */
5908		if (cur_fltr->sw_act.fwd_id.hw_vsi_id ==
5909		    new_fltr->sw_act.fwd_id.hw_vsi_id)
5910			return -EEXIST;
5911
5912		vsi_handle_arr[0] = cur_fltr->sw_act.vsi_handle;
5913		vsi_handle_arr[1] = new_fltr->sw_act.vsi_handle;
5914		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
5915						  &vsi_list_id,
5916						  ICE_SW_LKUP_LAST);
5917		if (status)
5918			return status;
5919
5920		memset(&tmp_fltr, 0, sizeof(tmp_fltr));
5921		tmp_fltr.flag = m_entry->rule_info.sw_act.flag;
5922		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
5923		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
5924		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
5925		tmp_fltr.lkup_type = ICE_SW_LKUP_LAST;
5926
5927		/* Update the previous switch rule of "forward to VSI" to
5928		 * "fwd to VSI list"
5929		 */
5930		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5931		if (status)
5932			return status;
5933
5934		cur_fltr->sw_act.fwd_id.vsi_list_id = vsi_list_id;
5935		cur_fltr->sw_act.fltr_act = ICE_FWD_TO_VSI_LIST;
5936		m_entry->vsi_list_info =
5937			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
5938						vsi_list_id);
5939	} else {
5940		u16 vsi_handle = new_fltr->sw_act.vsi_handle;
5941
5942		if (!m_entry->vsi_list_info)
5943			return -EIO;
5944
5945		/* A rule already exists with the new VSI being added */
5946		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
5947			return 0;
5948
5949		/* Update the previously created VSI list set with
5950		 * the new VSI ID passed in
5951		 */
5952		vsi_list_id = cur_fltr->sw_act.fwd_id.vsi_list_id;
5953
5954		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
5955						  vsi_list_id, false,
5956						  ice_aqc_opc_update_sw_rules,
5957						  ICE_SW_LKUP_LAST);
5958		/* update VSI list mapping info with new VSI ID */
5959		if (!status)
5960			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
5961	}
5962	if (!status)
5963		m_entry->vsi_count++;
5964	return status;
5965}
5966
5967void ice_rule_add_tunnel_metadata(struct ice_adv_lkup_elem *lkup)
5968{
5969	lkup->type = ICE_HW_METADATA;
5970	lkup->m_u.metadata.flags[ICE_PKT_FLAGS_MDID21] |=
5971		cpu_to_be16(ICE_PKT_TUNNEL_MASK);
5972}
5973
5974void ice_rule_add_direction_metadata(struct ice_adv_lkup_elem *lkup)
5975{
5976	lkup->type = ICE_HW_METADATA;
5977	lkup->m_u.metadata.flags[ICE_PKT_FLAGS_MDID20] |=
5978		cpu_to_be16(ICE_PKT_FROM_NETWORK);
5979}
5980
5981void ice_rule_add_vlan_metadata(struct ice_adv_lkup_elem *lkup)
5982{
5983	lkup->type = ICE_HW_METADATA;
5984	lkup->m_u.metadata.flags[ICE_PKT_FLAGS_MDID20] |=
5985		cpu_to_be16(ICE_PKT_VLAN_MASK);
5986}
5987
5988void ice_rule_add_src_vsi_metadata(struct ice_adv_lkup_elem *lkup)
5989{
5990	lkup->type = ICE_HW_METADATA;
5991	lkup->m_u.metadata.source_vsi = cpu_to_be16(ICE_MDID_SOURCE_VSI_MASK);
5992}
5993
5994/**
5995 * ice_add_adv_rule - helper function to create an advanced switch rule
5996 * @hw: pointer to the hardware structure
5997 * @lkups: information on the words that needs to be looked up. All words
5998 * together makes one recipe
5999 * @lkups_cnt: num of entries in the lkups array
6000 * @rinfo: other information related to the rule that needs to be programmed
6001 * @added_entry: this will return recipe_id, rule_id and vsi_handle. should be
6002 *               ignored is case of error.
6003 *
6004 * This function can program only 1 rule at a time. The lkups is used to
6005 * describe the all the words that forms the "lookup" portion of the recipe.
6006 * These words can span multiple protocols. Callers to this function need to
6007 * pass in a list of protocol headers with lookup information along and mask
6008 * that determines which words are valid from the given protocol header.
6009 * rinfo describes other information related to this rule such as forwarding
6010 * IDs, priority of this rule, etc.
6011 */
6012int
6013ice_add_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
6014		 u16 lkups_cnt, struct ice_adv_rule_info *rinfo,
6015		 struct ice_rule_query_data *added_entry)
6016{
6017	struct ice_adv_fltr_mgmt_list_entry *m_entry, *adv_fltr = NULL;
6018	struct ice_sw_rule_lkup_rx_tx *s_rule = NULL;
6019	const struct ice_dummy_pkt_profile *profile;
6020	u16 rid = 0, i, rule_buf_sz, vsi_handle;
6021	struct list_head *rule_head;
6022	struct ice_switch_info *sw;
6023	u16 word_cnt;
6024	u32 act = 0;
6025	int status;
6026	u8 q_rgn;
6027
6028	/* Initialize profile to result index bitmap */
6029	if (!hw->switch_info->prof_res_bm_init) {
6030		hw->switch_info->prof_res_bm_init = 1;
6031		ice_init_prof_result_bm(hw);
6032	}
6033
6034	if (!lkups_cnt)
6035		return -EINVAL;
6036
6037	/* get # of words we need to match */
6038	word_cnt = 0;
6039	for (i = 0; i < lkups_cnt; i++) {
6040		u16 j;
6041
6042		for (j = 0; j < ARRAY_SIZE(lkups->m_raw); j++)
6043			if (lkups[i].m_raw[j])
6044				word_cnt++;
6045	}
6046
6047	if (!word_cnt)
6048		return -EINVAL;
6049
6050	if (word_cnt > ICE_MAX_CHAIN_WORDS)
6051		return -ENOSPC;
6052
6053	/* locate a dummy packet */
6054	profile = ice_find_dummy_packet(lkups, lkups_cnt, rinfo->tun_type);
6055	if (IS_ERR(profile))
6056		return PTR_ERR(profile);
6057
6058	if (!(rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI ||
6059	      rinfo->sw_act.fltr_act == ICE_FWD_TO_Q ||
6060	      rinfo->sw_act.fltr_act == ICE_FWD_TO_QGRP ||
6061	      rinfo->sw_act.fltr_act == ICE_DROP_PACKET ||
6062	      rinfo->sw_act.fltr_act == ICE_MIRROR_PACKET ||
6063	      rinfo->sw_act.fltr_act == ICE_NOP)) {
6064		status = -EIO;
6065		goto free_pkt_profile;
6066	}
6067
6068	vsi_handle = rinfo->sw_act.vsi_handle;
6069	if (!ice_is_vsi_valid(hw, vsi_handle)) {
6070		status =  -EINVAL;
6071		goto free_pkt_profile;
6072	}
6073
6074	if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI ||
6075	    rinfo->sw_act.fltr_act == ICE_MIRROR_PACKET ||
6076	    rinfo->sw_act.fltr_act == ICE_NOP) {
6077		rinfo->sw_act.fwd_id.hw_vsi_id =
6078			ice_get_hw_vsi_num(hw, vsi_handle);
6079	}
6080
6081	if (rinfo->src_vsi)
6082		rinfo->sw_act.src = ice_get_hw_vsi_num(hw, rinfo->src_vsi);
6083	else
6084		rinfo->sw_act.src = ice_get_hw_vsi_num(hw, vsi_handle);
6085
6086	status = ice_add_adv_recipe(hw, lkups, lkups_cnt, rinfo, &rid);
6087	if (status)
6088		goto free_pkt_profile;
6089	m_entry = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
6090	if (m_entry) {
6091		/* we have to add VSI to VSI_LIST and increment vsi_count.
6092		 * Also Update VSI list so that we can change forwarding rule
6093		 * if the rule already exists, we will check if it exists with
6094		 * same vsi_id, if not then add it to the VSI list if it already
6095		 * exists if not then create a VSI list and add the existing VSI
6096		 * ID and the new VSI ID to the list
6097		 * We will add that VSI to the list
6098		 */
6099		status = ice_adv_add_update_vsi_list(hw, m_entry,
6100						     &m_entry->rule_info,
6101						     rinfo);
6102		if (added_entry) {
6103			added_entry->rid = rid;
6104			added_entry->rule_id = m_entry->rule_info.fltr_rule_id;
6105			added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
6106		}
6107		goto free_pkt_profile;
6108	}
6109	rule_buf_sz = ICE_SW_RULE_RX_TX_HDR_SIZE(s_rule, profile->pkt_len);
6110	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
6111	if (!s_rule) {
6112		status = -ENOMEM;
6113		goto free_pkt_profile;
6114	}
6115
6116	if (rinfo->sw_act.fltr_act != ICE_MIRROR_PACKET) {
6117		if (!rinfo->flags_info.act_valid) {
6118			act |= ICE_SINGLE_ACT_LAN_ENABLE;
6119			act |= ICE_SINGLE_ACT_LB_ENABLE;
6120		} else {
6121			act |= rinfo->flags_info.act & (ICE_SINGLE_ACT_LAN_ENABLE |
6122							ICE_SINGLE_ACT_LB_ENABLE);
6123		}
6124	}
6125
6126	switch (rinfo->sw_act.fltr_act) {
6127	case ICE_FWD_TO_VSI:
6128		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
6129				  rinfo->sw_act.fwd_id.hw_vsi_id);
6130		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_VALID_BIT;
6131		break;
6132	case ICE_FWD_TO_Q:
6133		act |= ICE_SINGLE_ACT_TO_Q;
6134		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
6135				  rinfo->sw_act.fwd_id.q_id);
6136		break;
6137	case ICE_FWD_TO_QGRP:
6138		q_rgn = rinfo->sw_act.qgrp_size > 0 ?
6139			(u8)ilog2(rinfo->sw_act.qgrp_size) : 0;
6140		act |= ICE_SINGLE_ACT_TO_Q;
6141		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
6142				  rinfo->sw_act.fwd_id.q_id);
6143		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_REGION_M, q_rgn);
6144		break;
6145	case ICE_DROP_PACKET:
6146		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
6147		       ICE_SINGLE_ACT_VALID_BIT;
6148		break;
6149	case ICE_MIRROR_PACKET:
6150		act |= ICE_SINGLE_ACT_OTHER_ACTS;
6151		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
6152				  rinfo->sw_act.fwd_id.hw_vsi_id);
6153		break;
6154	case ICE_NOP:
6155		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
6156				  rinfo->sw_act.fwd_id.hw_vsi_id);
6157		act &= ~ICE_SINGLE_ACT_VALID_BIT;
6158		break;
6159	default:
6160		status = -EIO;
6161		goto err_ice_add_adv_rule;
6162	}
6163
6164	/* If there is no matching criteria for direction there
6165	 * is only one difference between Rx and Tx:
6166	 * - get switch id base on VSI number from source field (Tx)
6167	 * - get switch id base on port number (Rx)
6168	 *
6169	 * If matching on direction metadata is chose rule direction is
6170	 * extracted from type value set here.
6171	 */
6172	if (rinfo->sw_act.flag & ICE_FLTR_TX) {
6173		s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
6174		s_rule->src = cpu_to_le16(rinfo->sw_act.src);
6175	} else {
6176		s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX);
6177		s_rule->src = cpu_to_le16(hw->port_info->lport);
6178	}
6179
6180	s_rule->recipe_id = cpu_to_le16(rid);
6181	s_rule->act = cpu_to_le32(act);
6182
6183	status = ice_fill_adv_dummy_packet(lkups, lkups_cnt, s_rule, profile);
6184	if (status)
6185		goto err_ice_add_adv_rule;
6186
6187	status = ice_fill_adv_packet_tun(hw, rinfo->tun_type, s_rule->hdr_data,
6188					 profile->offsets);
6189	if (status)
6190		goto err_ice_add_adv_rule;
6191
6192	status = ice_fill_adv_packet_vlan(hw, rinfo->vlan_type,
6193					  s_rule->hdr_data,
6194					  profile->offsets);
6195	if (status)
6196		goto err_ice_add_adv_rule;
6197
6198	status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
6199				 rule_buf_sz, 1, ice_aqc_opc_add_sw_rules,
6200				 NULL);
6201	if (status)
6202		goto err_ice_add_adv_rule;
6203	adv_fltr = devm_kzalloc(ice_hw_to_dev(hw),
6204				sizeof(struct ice_adv_fltr_mgmt_list_entry),
6205				GFP_KERNEL);
6206	if (!adv_fltr) {
6207		status = -ENOMEM;
6208		goto err_ice_add_adv_rule;
6209	}
6210
6211	adv_fltr->lkups = devm_kmemdup(ice_hw_to_dev(hw), lkups,
6212				       lkups_cnt * sizeof(*lkups), GFP_KERNEL);
6213	if (!adv_fltr->lkups) {
6214		status = -ENOMEM;
6215		goto err_ice_add_adv_rule;
6216	}
6217
6218	adv_fltr->lkups_cnt = lkups_cnt;
6219	adv_fltr->rule_info = *rinfo;
6220	adv_fltr->rule_info.fltr_rule_id = le16_to_cpu(s_rule->index);
6221	sw = hw->switch_info;
6222	sw->recp_list[rid].adv_rule = true;
6223	rule_head = &sw->recp_list[rid].filt_rules;
6224
6225	if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI)
6226		adv_fltr->vsi_count = 1;
6227
6228	/* Add rule entry to book keeping list */
6229	list_add(&adv_fltr->list_entry, rule_head);
6230	if (added_entry) {
6231		added_entry->rid = rid;
6232		added_entry->rule_id = adv_fltr->rule_info.fltr_rule_id;
6233		added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
6234	}
6235err_ice_add_adv_rule:
6236	if (status && adv_fltr) {
6237		devm_kfree(ice_hw_to_dev(hw), adv_fltr->lkups);
6238		devm_kfree(ice_hw_to_dev(hw), adv_fltr);
6239	}
6240
6241	kfree(s_rule);
6242
6243free_pkt_profile:
6244	if (profile->match & ICE_PKT_KMALLOC) {
6245		kfree(profile->offsets);
6246		kfree(profile->pkt);
6247		kfree(profile);
6248	}
6249
6250	return status;
6251}
6252
6253/**
6254 * ice_replay_vsi_fltr - Replay filters for requested VSI
6255 * @hw: pointer to the hardware structure
6256 * @vsi_handle: driver VSI handle
6257 * @recp_id: Recipe ID for which rules need to be replayed
6258 * @list_head: list for which filters need to be replayed
6259 *
6260 * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
6261 * It is required to pass valid VSI handle.
6262 */
6263static int
6264ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
6265		    struct list_head *list_head)
6266{
6267	struct ice_fltr_mgmt_list_entry *itr;
6268	int status = 0;
6269	u16 hw_vsi_id;
6270
6271	if (list_empty(list_head))
6272		return status;
6273	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
6274
6275	list_for_each_entry(itr, list_head, list_entry) {
6276		struct ice_fltr_list_entry f_entry;
6277
6278		f_entry.fltr_info = itr->fltr_info;
6279		if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
6280		    itr->fltr_info.vsi_handle == vsi_handle) {
6281			/* update the src in case it is VSI num */
6282			if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6283				f_entry.fltr_info.src = hw_vsi_id;
6284			status = ice_add_rule_internal(hw, recp_id, &f_entry);
6285			if (status)
6286				goto end;
6287			continue;
6288		}
6289		if (!itr->vsi_list_info ||
6290		    !test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
6291			continue;
6292		/* Clearing it so that the logic can add it back */
6293		clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
6294		f_entry.fltr_info.vsi_handle = vsi_handle;
6295		f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
6296		/* update the src in case it is VSI num */
6297		if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6298			f_entry.fltr_info.src = hw_vsi_id;
6299		if (recp_id == ICE_SW_LKUP_VLAN)
6300			status = ice_add_vlan_internal(hw, &f_entry);
6301		else
6302			status = ice_add_rule_internal(hw, recp_id, &f_entry);
6303		if (status)
6304			goto end;
6305	}
6306end:
6307	return status;
6308}
6309
6310/**
6311 * ice_adv_rem_update_vsi_list
6312 * @hw: pointer to the hardware structure
6313 * @vsi_handle: VSI handle of the VSI to remove
6314 * @fm_list: filter management entry for which the VSI list management needs to
6315 *	     be done
6316 */
6317static int
6318ice_adv_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
6319			    struct ice_adv_fltr_mgmt_list_entry *fm_list)
6320{
6321	struct ice_vsi_list_map_info *vsi_list_info;
6322	enum ice_sw_lkup_type lkup_type;
6323	u16 vsi_list_id;
6324	int status;
6325
6326	if (fm_list->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST ||
6327	    fm_list->vsi_count == 0)
6328		return -EINVAL;
6329
6330	/* A rule with the VSI being removed does not exist */
6331	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
6332		return -ENOENT;
6333
6334	lkup_type = ICE_SW_LKUP_LAST;
6335	vsi_list_id = fm_list->rule_info.sw_act.fwd_id.vsi_list_id;
6336	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
6337					  ice_aqc_opc_update_sw_rules,
6338					  lkup_type);
6339	if (status)
6340		return status;
6341
6342	fm_list->vsi_count--;
6343	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
6344	vsi_list_info = fm_list->vsi_list_info;
6345	if (fm_list->vsi_count == 1) {
6346		struct ice_fltr_info tmp_fltr;
6347		u16 rem_vsi_handle;
6348
6349		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
6350						ICE_MAX_VSI);
6351		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
6352			return -EIO;
6353
6354		/* Make sure VSI list is empty before removing it below */
6355		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
6356						  vsi_list_id, true,
6357						  ice_aqc_opc_update_sw_rules,
6358						  lkup_type);
6359		if (status)
6360			return status;
6361
6362		memset(&tmp_fltr, 0, sizeof(tmp_fltr));
6363		tmp_fltr.flag = fm_list->rule_info.sw_act.flag;
6364		tmp_fltr.fltr_rule_id = fm_list->rule_info.fltr_rule_id;
6365		fm_list->rule_info.sw_act.fltr_act = ICE_FWD_TO_VSI;
6366		tmp_fltr.fltr_act = ICE_FWD_TO_VSI;
6367		tmp_fltr.fwd_id.hw_vsi_id =
6368			ice_get_hw_vsi_num(hw, rem_vsi_handle);
6369		fm_list->rule_info.sw_act.fwd_id.hw_vsi_id =
6370			ice_get_hw_vsi_num(hw, rem_vsi_handle);
6371		fm_list->rule_info.sw_act.vsi_handle = rem_vsi_handle;
6372
6373		/* Update the previous switch rule of "MAC forward to VSI" to
6374		 * "MAC fwd to VSI list"
6375		 */
6376		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
6377		if (status) {
6378			ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
6379				  tmp_fltr.fwd_id.hw_vsi_id, status);
6380			return status;
6381		}
6382		fm_list->vsi_list_info->ref_cnt--;
6383
6384		/* Remove the VSI list since it is no longer used */
6385		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
6386		if (status) {
6387			ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
6388				  vsi_list_id, status);
6389			return status;
6390		}
6391
6392		list_del(&vsi_list_info->list_entry);
6393		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
6394		fm_list->vsi_list_info = NULL;
6395	}
6396
6397	return status;
6398}
6399
6400/**
6401 * ice_rem_adv_rule - removes existing advanced switch rule
6402 * @hw: pointer to the hardware structure
6403 * @lkups: information on the words that needs to be looked up. All words
6404 *         together makes one recipe
6405 * @lkups_cnt: num of entries in the lkups array
6406 * @rinfo: Its the pointer to the rule information for the rule
6407 *
6408 * This function can be used to remove 1 rule at a time. The lkups is
6409 * used to describe all the words that forms the "lookup" portion of the
6410 * rule. These words can span multiple protocols. Callers to this function
6411 * need to pass in a list of protocol headers with lookup information along
6412 * and mask that determines which words are valid from the given protocol
6413 * header. rinfo describes other information related to this rule such as
6414 * forwarding IDs, priority of this rule, etc.
6415 */
6416static int
6417ice_rem_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
6418		 u16 lkups_cnt, struct ice_adv_rule_info *rinfo)
6419{
6420	struct ice_adv_fltr_mgmt_list_entry *list_elem;
6421	struct ice_prot_lkup_ext lkup_exts;
6422	bool remove_rule = false;
6423	struct mutex *rule_lock; /* Lock to protect filter rule list */
6424	u16 i, rid, vsi_handle;
6425	int status = 0;
6426
6427	memset(&lkup_exts, 0, sizeof(lkup_exts));
6428	for (i = 0; i < lkups_cnt; i++) {
6429		u16 count;
6430
6431		if (lkups[i].type >= ICE_PROTOCOL_LAST)
6432			return -EIO;
6433
6434		count = ice_fill_valid_words(&lkups[i], &lkup_exts);
6435		if (!count)
6436			return -EIO;
6437	}
6438
6439	rid = ice_find_recp(hw, &lkup_exts, rinfo);
6440	/* If did not find a recipe that match the existing criteria */
6441	if (rid == ICE_MAX_NUM_RECIPES)
6442		return -EINVAL;
6443
6444	rule_lock = &hw->switch_info->recp_list[rid].filt_rule_lock;
6445	list_elem = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
6446	/* the rule is already removed */
6447	if (!list_elem)
6448		return 0;
6449	mutex_lock(rule_lock);
6450	if (list_elem->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST) {
6451		remove_rule = true;
6452	} else if (list_elem->vsi_count > 1) {
6453		remove_rule = false;
6454		vsi_handle = rinfo->sw_act.vsi_handle;
6455		status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
6456	} else {
6457		vsi_handle = rinfo->sw_act.vsi_handle;
6458		status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
6459		if (status) {
6460			mutex_unlock(rule_lock);
6461			return status;
6462		}
6463		if (list_elem->vsi_count == 0)
6464			remove_rule = true;
6465	}
6466	mutex_unlock(rule_lock);
6467	if (remove_rule) {
6468		struct ice_sw_rule_lkup_rx_tx *s_rule;
6469		u16 rule_buf_sz;
6470
6471		rule_buf_sz = ICE_SW_RULE_RX_TX_NO_HDR_SIZE(s_rule);
6472		s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
6473		if (!s_rule)
6474			return -ENOMEM;
6475		s_rule->act = 0;
6476		s_rule->index = cpu_to_le16(list_elem->rule_info.fltr_rule_id);
6477		s_rule->hdr_len = 0;
6478		status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
6479					 rule_buf_sz, 1,
6480					 ice_aqc_opc_remove_sw_rules, NULL);
6481		if (!status || status == -ENOENT) {
6482			struct ice_switch_info *sw = hw->switch_info;
 
6483
6484			mutex_lock(rule_lock);
6485			list_del(&list_elem->list_entry);
6486			devm_kfree(ice_hw_to_dev(hw), list_elem->lkups);
6487			devm_kfree(ice_hw_to_dev(hw), list_elem);
6488			mutex_unlock(rule_lock);
6489			if (list_empty(&sw->recp_list[rid].filt_rules))
6490				sw->recp_list[rid].adv_rule = false;
 
 
 
 
 
 
6491		}
6492		kfree(s_rule);
6493	}
6494	return status;
6495}
6496
6497/**
6498 * ice_rem_adv_rule_by_id - removes existing advanced switch rule by ID
6499 * @hw: pointer to the hardware structure
6500 * @remove_entry: data struct which holds rule_id, VSI handle and recipe ID
6501 *
6502 * This function is used to remove 1 rule at a time. The removal is based on
6503 * the remove_entry parameter. This function will remove rule for a given
6504 * vsi_handle with a given rule_id which is passed as parameter in remove_entry
6505 */
6506int
6507ice_rem_adv_rule_by_id(struct ice_hw *hw,
6508		       struct ice_rule_query_data *remove_entry)
6509{
6510	struct ice_adv_fltr_mgmt_list_entry *list_itr;
6511	struct list_head *list_head;
6512	struct ice_adv_rule_info rinfo;
6513	struct ice_switch_info *sw;
6514
6515	sw = hw->switch_info;
6516	if (!sw->recp_list[remove_entry->rid].recp_created)
6517		return -EINVAL;
6518	list_head = &sw->recp_list[remove_entry->rid].filt_rules;
6519	list_for_each_entry(list_itr, list_head, list_entry) {
6520		if (list_itr->rule_info.fltr_rule_id ==
6521		    remove_entry->rule_id) {
6522			rinfo = list_itr->rule_info;
6523			rinfo.sw_act.vsi_handle = remove_entry->vsi_handle;
6524			return ice_rem_adv_rule(hw, list_itr->lkups,
6525						list_itr->lkups_cnt, &rinfo);
6526		}
6527	}
6528	/* either list is empty or unable to find rule */
6529	return -ENOENT;
6530}
6531
6532/**
6533 * ice_replay_vsi_adv_rule - Replay advanced rule for requested VSI
6534 * @hw: pointer to the hardware structure
6535 * @vsi_handle: driver VSI handle
6536 * @list_head: list for which filters need to be replayed
6537 *
6538 * Replay the advanced rule for the given VSI.
6539 */
6540static int
6541ice_replay_vsi_adv_rule(struct ice_hw *hw, u16 vsi_handle,
6542			struct list_head *list_head)
6543{
6544	struct ice_rule_query_data added_entry = { 0 };
6545	struct ice_adv_fltr_mgmt_list_entry *adv_fltr;
6546	int status = 0;
6547
6548	if (list_empty(list_head))
6549		return status;
6550	list_for_each_entry(adv_fltr, list_head, list_entry) {
6551		struct ice_adv_rule_info *rinfo = &adv_fltr->rule_info;
6552		u16 lk_cnt = adv_fltr->lkups_cnt;
6553
6554		if (vsi_handle != rinfo->sw_act.vsi_handle)
6555			continue;
6556		status = ice_add_adv_rule(hw, adv_fltr->lkups, lk_cnt, rinfo,
6557					  &added_entry);
6558		if (status)
6559			break;
6560	}
6561	return status;
6562}
6563
6564/**
6565 * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
6566 * @hw: pointer to the hardware structure
6567 * @vsi_handle: driver VSI handle
6568 *
6569 * Replays filters for requested VSI via vsi_handle.
6570 */
6571int ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
6572{
6573	struct ice_switch_info *sw = hw->switch_info;
6574	int status;
6575	u8 i;
6576
6577	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6578		struct list_head *head;
6579
6580		head = &sw->recp_list[i].filt_replay_rules;
6581		if (!sw->recp_list[i].adv_rule)
6582			status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
6583		else
6584			status = ice_replay_vsi_adv_rule(hw, vsi_handle, head);
6585		if (status)
6586			return status;
6587	}
6588	return status;
6589}
6590
6591/**
6592 * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
6593 * @hw: pointer to the HW struct
6594 *
6595 * Deletes the filter replay rules.
6596 */
6597void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
6598{
6599	struct ice_switch_info *sw = hw->switch_info;
6600	u8 i;
6601
6602	if (!sw)
6603		return;
6604
6605	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6606		if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
6607			struct list_head *l_head;
6608
6609			l_head = &sw->recp_list[i].filt_replay_rules;
6610			if (!sw->recp_list[i].adv_rule)
6611				ice_rem_sw_rule_info(hw, l_head);
6612			else
6613				ice_rem_adv_rule_info(hw, l_head);
6614		}
6615	}
6616}