xref: /linux/drivers/net/ethernet/intel/ice/ice_switch.c (revision 385edf325efaad802e1bc8eeb710e02eb6461d73)
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  */
28 static 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 
32 enum {
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 
47 struct ice_dummy_pkt_offsets {
48 	enum ice_protocol_type type;
49 	u16 offset; /* ICE_PROTOCOL_LAST indicates end of list */
50 };
51 
52 struct 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 
75 ICE_DECLARE_PKT_OFFSETS(vlan) = {
76 	{ ICE_VLAN_OFOS,        12 },
77 };
78 
79 ICE_DECLARE_PKT_TEMPLATE(vlan) = {
80 	0x81, 0x00, 0x00, 0x00, /* ICE_VLAN_OFOS 12 */
81 };
82 
83 ICE_DECLARE_PKT_OFFSETS(qinq) = {
84 	{ ICE_VLAN_EX,          12 },
85 	{ ICE_VLAN_IN,          16 },
86 };
87 
88 ICE_DECLARE_PKT_TEMPLATE(qinq) = {
89 	0x91, 0x00, 0x00, 0x00, /* ICE_VLAN_EX 12 */
90 	0x81, 0x00, 0x00, 0x00, /* ICE_VLAN_IN 16 */
91 };
92 
93 ICE_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 
105 ICE_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 
140 ICE_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 
152 ICE_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 
184 ICE_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 
199 ICE_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 
237 ICE_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 
252 ICE_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 
287 ICE_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 
299 ICE_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 
339 ICE_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 
351 ICE_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 
388 ICE_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 
403 ICE_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 
446 ICE_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 
461 ICE_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 */
502 ICE_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 */
511 ICE_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 */
531 ICE_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 */
540 ICE_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 
562 ICE_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 
570 ICE_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 */
598 ICE_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 */
607 ICE_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 */
635 ICE_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 
645 ICE_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 */
683 ICE_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 
693 ICE_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 */
728 ICE_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 
738 ICE_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 
780 ICE_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 
790 ICE_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 
829 ICE_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 
839 ICE_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 
881 ICE_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 
891 ICE_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 
930 ICE_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 
940 ICE_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 
987 ICE_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 
997 ICE_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 
1041 ICE_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 
1049 ICE_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 
1079 ICE_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 
1087 ICE_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 
1113 ICE_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 
1122 ICE_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 
1149 ICE_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 
1158 ICE_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 
1182 ICE_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 
1191 ICE_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 
1223 ICE_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 
1232 ICE_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 
1261 ICE_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 
1269 ICE_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 
1288 ICE_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 
1296 ICE_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 
1320 static 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 */
1372 static DECLARE_BITMAP(recipe_to_profile[ICE_MAX_NUM_RECIPES],
1373 			  ICE_MAX_NUM_PROFILES);
1374 
1375 /* this is a profile to recipe association bitmap */
1376 static 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  */
1386 int 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  */
1434 static int
1435 ice_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  */
1464 static int
1465 ice_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  */
1508 static int
1509 ice_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  */
1543 static int
1544 ice_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  */
1579 bool 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  */
1592 u16 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  */
1604 struct 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  */
1617 static void
1618 ice_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  */
1628 static 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  */
1650 static 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  */
1666 void 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  */
1685 int
1686 ice_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  */
1726 int
1727 ice_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  */
1750 int
1751 ice_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  */
1766 int
1767 ice_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  */
1810 static int
1811 ice_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_RAW_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  */
1868 int
1869 ice_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  */
1902 int
1903 ice_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  */
1939 int
1940 ice_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  */
1979 int
1980 ice_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 
2019 error_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_assoc: 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  */
2032 int
2033 ice_aq_map_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u64 r_assoc,
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 	cmd->recipe_assoc = cpu_to_le64(r_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_assoc: 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  */
2058 int
2059 ice_aq_get_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u64 *r_assoc,
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 		*r_assoc = le64_to_cpu(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  */
2082 int ice_alloc_recipe(struct ice_hw *hw, u16 *rid)
2083 {
2084 	DEFINE_RAW_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  */
2108 static void ice_get_recp_to_prof_map(struct ice_hw *hw)
2109 {
2110 	DECLARE_BITMAP(r_bitmap, ICE_MAX_NUM_RECIPES);
2111 	u64 recp_assoc;
2112 	u16 i;
2113 
2114 	for (i = 0; i < hw->switch_info->max_used_prof_index + 1; i++) {
2115 		u16 j;
2116 
2117 		bitmap_zero(profile_to_recipe[i], ICE_MAX_NUM_RECIPES);
2118 		bitmap_zero(r_bitmap, ICE_MAX_NUM_RECIPES);
2119 		if (ice_aq_get_recipe_to_profile(hw, i, &recp_assoc, NULL))
2120 			continue;
2121 		bitmap_from_arr64(r_bitmap, &recp_assoc, ICE_MAX_NUM_RECIPES);
2122 		bitmap_copy(profile_to_recipe[i], r_bitmap,
2123 			    ICE_MAX_NUM_RECIPES);
2124 		for_each_set_bit(j, r_bitmap, ICE_MAX_NUM_RECIPES)
2125 			set_bit(i, recipe_to_profile[j]);
2126 	}
2127 }
2128 
2129 /**
2130  * ice_collect_result_idx - copy result index values
2131  * @buf: buffer that contains the result index
2132  * @recp: the recipe struct to copy data into
2133  */
2134 static void
2135 ice_collect_result_idx(struct ice_aqc_recipe_data_elem *buf,
2136 		       struct ice_sw_recipe *recp)
2137 {
2138 	if (buf->content.result_indx & ICE_AQ_RECIPE_RESULT_EN)
2139 		set_bit(buf->content.result_indx & ~ICE_AQ_RECIPE_RESULT_EN,
2140 			recp->res_idxs);
2141 }
2142 
2143 /**
2144  * ice_get_recp_frm_fw - update SW bookkeeping from FW recipe entries
2145  * @hw: pointer to hardware structure
2146  * @recps: struct that we need to populate
2147  * @rid: recipe ID that we are populating
2148  * @refresh_required: true if we should get recipe to profile mapping from FW
2149  *
2150  * This function is used to populate all the necessary entries into our
2151  * bookkeeping so that we have a current list of all the recipes that are
2152  * programmed in the firmware.
2153  */
2154 static int
2155 ice_get_recp_frm_fw(struct ice_hw *hw, struct ice_sw_recipe *recps, u8 rid,
2156 		    bool *refresh_required)
2157 {
2158 	DECLARE_BITMAP(result_bm, ICE_MAX_FV_WORDS);
2159 	struct ice_aqc_recipe_data_elem *tmp;
2160 	u16 num_recps = ICE_MAX_NUM_RECIPES;
2161 	struct ice_prot_lkup_ext *lkup_exts;
2162 	u8 fv_word_idx = 0;
2163 	u16 sub_recps;
2164 	int status;
2165 
2166 	bitmap_zero(result_bm, ICE_MAX_FV_WORDS);
2167 
2168 	/* we need a buffer big enough to accommodate all the recipes */
2169 	tmp = kcalloc(ICE_MAX_NUM_RECIPES, sizeof(*tmp), GFP_KERNEL);
2170 	if (!tmp)
2171 		return -ENOMEM;
2172 
2173 	tmp[0].recipe_indx = rid;
2174 	status = ice_aq_get_recipe(hw, tmp, &num_recps, rid, NULL);
2175 	/* non-zero status meaning recipe doesn't exist */
2176 	if (status)
2177 		goto err_unroll;
2178 
2179 	/* Get recipe to profile map so that we can get the fv from lkups that
2180 	 * we read for a recipe from FW. Since we want to minimize the number of
2181 	 * times we make this FW call, just make one call and cache the copy
2182 	 * until a new recipe is added. This operation is only required the
2183 	 * first time to get the changes from FW. Then to search existing
2184 	 * entries we don't need to update the cache again until another recipe
2185 	 * gets added.
2186 	 */
2187 	if (*refresh_required) {
2188 		ice_get_recp_to_prof_map(hw);
2189 		*refresh_required = false;
2190 	}
2191 
2192 	/* Start populating all the entries for recps[rid] based on lkups from
2193 	 * firmware. Note that we are only creating the root recipe in our
2194 	 * database.
2195 	 */
2196 	lkup_exts = &recps[rid].lkup_exts;
2197 
2198 	for (sub_recps = 0; sub_recps < num_recps; sub_recps++) {
2199 		struct ice_aqc_recipe_data_elem root_bufs = tmp[sub_recps];
2200 		struct ice_recp_grp_entry *rg_entry;
2201 		u8 i, prof, idx, prot = 0;
2202 		bool is_root;
2203 		u16 off = 0;
2204 
2205 		rg_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*rg_entry),
2206 					GFP_KERNEL);
2207 		if (!rg_entry) {
2208 			status = -ENOMEM;
2209 			goto err_unroll;
2210 		}
2211 
2212 		idx = root_bufs.recipe_indx;
2213 		is_root = root_bufs.content.rid & ICE_AQ_RECIPE_ID_IS_ROOT;
2214 
2215 		/* Mark all result indices in this chain */
2216 		if (root_bufs.content.result_indx & ICE_AQ_RECIPE_RESULT_EN)
2217 			set_bit(root_bufs.content.result_indx & ~ICE_AQ_RECIPE_RESULT_EN,
2218 				result_bm);
2219 
2220 		/* get the first profile that is associated with rid */
2221 		prof = find_first_bit(recipe_to_profile[idx],
2222 				      ICE_MAX_NUM_PROFILES);
2223 		for (i = 0; i < ICE_NUM_WORDS_RECIPE; i++) {
2224 			u8 lkup_indx = root_bufs.content.lkup_indx[i + 1];
2225 
2226 			rg_entry->fv_idx[i] = lkup_indx;
2227 			rg_entry->fv_mask[i] =
2228 				le16_to_cpu(root_bufs.content.mask[i + 1]);
2229 
2230 			/* If the recipe is a chained recipe then all its
2231 			 * child recipe's result will have a result index.
2232 			 * To fill fv_words we should not use those result
2233 			 * index, we only need the protocol ids and offsets.
2234 			 * We will skip all the fv_idx which stores result
2235 			 * index in them. We also need to skip any fv_idx which
2236 			 * has ICE_AQ_RECIPE_LKUP_IGNORE or 0 since it isn't a
2237 			 * valid offset value.
2238 			 */
2239 			if (test_bit(rg_entry->fv_idx[i], hw->switch_info->prof_res_bm[prof]) ||
2240 			    rg_entry->fv_idx[i] & ICE_AQ_RECIPE_LKUP_IGNORE ||
2241 			    rg_entry->fv_idx[i] == 0)
2242 				continue;
2243 
2244 			ice_find_prot_off(hw, ICE_BLK_SW, prof,
2245 					  rg_entry->fv_idx[i], &prot, &off);
2246 			lkup_exts->fv_words[fv_word_idx].prot_id = prot;
2247 			lkup_exts->fv_words[fv_word_idx].off = off;
2248 			lkup_exts->field_mask[fv_word_idx] =
2249 				rg_entry->fv_mask[i];
2250 			fv_word_idx++;
2251 		}
2252 		/* populate rg_list with the data from the child entry of this
2253 		 * recipe
2254 		 */
2255 		list_add(&rg_entry->l_entry, &recps[rid].rg_list);
2256 
2257 		/* Propagate some data to the recipe database */
2258 		recps[idx].is_root = !!is_root;
2259 		recps[idx].priority = root_bufs.content.act_ctrl_fwd_priority;
2260 		recps[idx].need_pass_l2 = root_bufs.content.act_ctrl &
2261 					  ICE_AQ_RECIPE_ACT_NEED_PASS_L2;
2262 		recps[idx].allow_pass_l2 = root_bufs.content.act_ctrl &
2263 					   ICE_AQ_RECIPE_ACT_ALLOW_PASS_L2;
2264 		bitmap_zero(recps[idx].res_idxs, ICE_MAX_FV_WORDS);
2265 		if (root_bufs.content.result_indx & ICE_AQ_RECIPE_RESULT_EN) {
2266 			recps[idx].chain_idx = root_bufs.content.result_indx &
2267 				~ICE_AQ_RECIPE_RESULT_EN;
2268 			set_bit(recps[idx].chain_idx, recps[idx].res_idxs);
2269 		} else {
2270 			recps[idx].chain_idx = ICE_INVAL_CHAIN_IND;
2271 		}
2272 
2273 		if (!is_root)
2274 			continue;
2275 
2276 		/* Only do the following for root recipes entries */
2277 		memcpy(recps[idx].r_bitmap, root_bufs.recipe_bitmap,
2278 		       sizeof(recps[idx].r_bitmap));
2279 		recps[idx].root_rid = root_bufs.content.rid &
2280 			~ICE_AQ_RECIPE_ID_IS_ROOT;
2281 		recps[idx].priority = root_bufs.content.act_ctrl_fwd_priority;
2282 	}
2283 
2284 	/* Complete initialization of the root recipe entry */
2285 	lkup_exts->n_val_words = fv_word_idx;
2286 	recps[rid].big_recp = (num_recps > 1);
2287 	recps[rid].n_grp_count = (u8)num_recps;
2288 	recps[rid].root_buf = devm_kmemdup(ice_hw_to_dev(hw), tmp,
2289 					   recps[rid].n_grp_count * sizeof(*recps[rid].root_buf),
2290 					   GFP_KERNEL);
2291 	if (!recps[rid].root_buf) {
2292 		status = -ENOMEM;
2293 		goto err_unroll;
2294 	}
2295 
2296 	/* Copy result indexes */
2297 	bitmap_copy(recps[rid].res_idxs, result_bm, ICE_MAX_FV_WORDS);
2298 	recps[rid].recp_created = true;
2299 
2300 err_unroll:
2301 	kfree(tmp);
2302 	return status;
2303 }
2304 
2305 /* ice_init_port_info - Initialize port_info with switch configuration data
2306  * @pi: pointer to port_info
2307  * @vsi_port_num: VSI number or port number
2308  * @type: Type of switch element (port or VSI)
2309  * @swid: switch ID of the switch the element is attached to
2310  * @pf_vf_num: PF or VF number
2311  * @is_vf: true if the element is a VF, false otherwise
2312  */
2313 static void
2314 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
2315 		   u16 swid, u16 pf_vf_num, bool is_vf)
2316 {
2317 	switch (type) {
2318 	case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
2319 		pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
2320 		pi->sw_id = swid;
2321 		pi->pf_vf_num = pf_vf_num;
2322 		pi->is_vf = is_vf;
2323 		break;
2324 	default:
2325 		ice_debug(pi->hw, ICE_DBG_SW, "incorrect VSI/port type received\n");
2326 		break;
2327 	}
2328 }
2329 
2330 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
2331  * @hw: pointer to the hardware structure
2332  */
2333 int ice_get_initial_sw_cfg(struct ice_hw *hw)
2334 {
2335 	struct ice_aqc_get_sw_cfg_resp_elem *rbuf;
2336 	u16 req_desc = 0;
2337 	u16 num_elems;
2338 	int status;
2339 	u16 i;
2340 
2341 	rbuf = kzalloc(ICE_SW_CFG_MAX_BUF_LEN, GFP_KERNEL);
2342 	if (!rbuf)
2343 		return -ENOMEM;
2344 
2345 	/* Multiple calls to ice_aq_get_sw_cfg may be required
2346 	 * to get all the switch configuration information. The need
2347 	 * for additional calls is indicated by ice_aq_get_sw_cfg
2348 	 * writing a non-zero value in req_desc
2349 	 */
2350 	do {
2351 		struct ice_aqc_get_sw_cfg_resp_elem *ele;
2352 
2353 		status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
2354 					   &req_desc, &num_elems, NULL);
2355 
2356 		if (status)
2357 			break;
2358 
2359 		for (i = 0, ele = rbuf; i < num_elems; i++, ele++) {
2360 			u16 pf_vf_num, swid, vsi_port_num;
2361 			bool is_vf = false;
2362 			u8 res_type;
2363 
2364 			vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
2365 				ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
2366 
2367 			pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
2368 				ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
2369 
2370 			swid = le16_to_cpu(ele->swid);
2371 
2372 			if (le16_to_cpu(ele->pf_vf_num) &
2373 			    ICE_AQC_GET_SW_CONF_RESP_IS_VF)
2374 				is_vf = true;
2375 
2376 			res_type = (u8)(le16_to_cpu(ele->vsi_port_num) >>
2377 					ICE_AQC_GET_SW_CONF_RESP_TYPE_S);
2378 
2379 			if (res_type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
2380 				/* FW VSI is not needed. Just continue. */
2381 				continue;
2382 			}
2383 
2384 			ice_init_port_info(hw->port_info, vsi_port_num,
2385 					   res_type, swid, pf_vf_num, is_vf);
2386 		}
2387 	} while (req_desc && !status);
2388 
2389 	kfree(rbuf);
2390 	return status;
2391 }
2392 
2393 /**
2394  * ice_fill_sw_info - Helper function to populate lb_en and lan_en
2395  * @hw: pointer to the hardware structure
2396  * @fi: filter info structure to fill/update
2397  *
2398  * This helper function populates the lb_en and lan_en elements of the provided
2399  * ice_fltr_info struct using the switch's type and characteristics of the
2400  * switch rule being configured.
2401  */
2402 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
2403 {
2404 	fi->lb_en = false;
2405 	fi->lan_en = false;
2406 	if ((fi->flag & ICE_FLTR_TX) &&
2407 	    (fi->fltr_act == ICE_FWD_TO_VSI ||
2408 	     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
2409 	     fi->fltr_act == ICE_FWD_TO_Q ||
2410 	     fi->fltr_act == ICE_FWD_TO_QGRP)) {
2411 		/* Setting LB for prune actions will result in replicated
2412 		 * packets to the internal switch that will be dropped.
2413 		 */
2414 		if (fi->lkup_type != ICE_SW_LKUP_VLAN)
2415 			fi->lb_en = true;
2416 
2417 		/* Set lan_en to TRUE if
2418 		 * 1. The switch is a VEB AND
2419 		 * 2
2420 		 * 2.1 The lookup is a directional lookup like ethertype,
2421 		 * promiscuous, ethertype-MAC, promiscuous-VLAN
2422 		 * and default-port OR
2423 		 * 2.2 The lookup is VLAN, OR
2424 		 * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
2425 		 * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
2426 		 *
2427 		 * OR
2428 		 *
2429 		 * The switch is a VEPA.
2430 		 *
2431 		 * In all other cases, the LAN enable has to be set to false.
2432 		 */
2433 		if (hw->evb_veb) {
2434 			if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
2435 			    fi->lkup_type == ICE_SW_LKUP_PROMISC ||
2436 			    fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
2437 			    fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
2438 			    fi->lkup_type == ICE_SW_LKUP_DFLT ||
2439 			    fi->lkup_type == ICE_SW_LKUP_VLAN ||
2440 			    (fi->lkup_type == ICE_SW_LKUP_MAC &&
2441 			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)) ||
2442 			    (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
2443 			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)))
2444 				fi->lan_en = true;
2445 		} else {
2446 			fi->lan_en = true;
2447 		}
2448 	}
2449 
2450 	if (fi->flag & ICE_FLTR_TX_ONLY)
2451 		fi->lan_en = false;
2452 }
2453 
2454 /**
2455  * ice_fill_eth_hdr - helper to copy dummy_eth_hdr into supplied buffer
2456  * @eth_hdr: pointer to buffer to populate
2457  */
2458 void ice_fill_eth_hdr(u8 *eth_hdr)
2459 {
2460 	memcpy(eth_hdr, dummy_eth_header, DUMMY_ETH_HDR_LEN);
2461 }
2462 
2463 /**
2464  * ice_fill_sw_rule - Helper function to fill switch rule structure
2465  * @hw: pointer to the hardware structure
2466  * @f_info: entry containing packet forwarding information
2467  * @s_rule: switch rule structure to be filled in based on mac_entry
2468  * @opc: switch rules population command type - pass in the command opcode
2469  */
2470 static void
2471 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
2472 		 struct ice_sw_rule_lkup_rx_tx *s_rule,
2473 		 enum ice_adminq_opc opc)
2474 {
2475 	u16 vlan_id = ICE_MAX_VLAN_ID + 1;
2476 	u16 vlan_tpid = ETH_P_8021Q;
2477 	void *daddr = NULL;
2478 	u16 eth_hdr_sz;
2479 	u8 *eth_hdr;
2480 	u32 act = 0;
2481 	__be16 *off;
2482 	u8 q_rgn;
2483 
2484 	if (opc == ice_aqc_opc_remove_sw_rules) {
2485 		s_rule->act = 0;
2486 		s_rule->index = cpu_to_le16(f_info->fltr_rule_id);
2487 		s_rule->hdr_len = 0;
2488 		return;
2489 	}
2490 
2491 	eth_hdr_sz = sizeof(dummy_eth_header);
2492 	eth_hdr = s_rule->hdr_data;
2493 
2494 	/* initialize the ether header with a dummy header */
2495 	memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz);
2496 	ice_fill_sw_info(hw, f_info);
2497 
2498 	switch (f_info->fltr_act) {
2499 	case ICE_FWD_TO_VSI:
2500 		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
2501 				  f_info->fwd_id.hw_vsi_id);
2502 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
2503 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
2504 				ICE_SINGLE_ACT_VALID_BIT;
2505 		break;
2506 	case ICE_FWD_TO_VSI_LIST:
2507 		act |= ICE_SINGLE_ACT_VSI_LIST;
2508 		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_LIST_ID_M,
2509 				  f_info->fwd_id.vsi_list_id);
2510 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
2511 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
2512 				ICE_SINGLE_ACT_VALID_BIT;
2513 		break;
2514 	case ICE_FWD_TO_Q:
2515 		act |= ICE_SINGLE_ACT_TO_Q;
2516 		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
2517 				  f_info->fwd_id.q_id);
2518 		break;
2519 	case ICE_DROP_PACKET:
2520 		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
2521 			ICE_SINGLE_ACT_VALID_BIT;
2522 		break;
2523 	case ICE_FWD_TO_QGRP:
2524 		q_rgn = f_info->qgrp_size > 0 ?
2525 			(u8)ilog2(f_info->qgrp_size) : 0;
2526 		act |= ICE_SINGLE_ACT_TO_Q;
2527 		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
2528 				  f_info->fwd_id.q_id);
2529 		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_REGION_M, q_rgn);
2530 		break;
2531 	default:
2532 		return;
2533 	}
2534 
2535 	if (f_info->lb_en)
2536 		act |= ICE_SINGLE_ACT_LB_ENABLE;
2537 	if (f_info->lan_en)
2538 		act |= ICE_SINGLE_ACT_LAN_ENABLE;
2539 
2540 	switch (f_info->lkup_type) {
2541 	case ICE_SW_LKUP_MAC:
2542 		daddr = f_info->l_data.mac.mac_addr;
2543 		break;
2544 	case ICE_SW_LKUP_VLAN:
2545 		vlan_id = f_info->l_data.vlan.vlan_id;
2546 		if (f_info->l_data.vlan.tpid_valid)
2547 			vlan_tpid = f_info->l_data.vlan.tpid;
2548 		if (f_info->fltr_act == ICE_FWD_TO_VSI ||
2549 		    f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
2550 			act |= ICE_SINGLE_ACT_PRUNE;
2551 			act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
2552 		}
2553 		break;
2554 	case ICE_SW_LKUP_ETHERTYPE_MAC:
2555 		daddr = f_info->l_data.ethertype_mac.mac_addr;
2556 		fallthrough;
2557 	case ICE_SW_LKUP_ETHERTYPE:
2558 		off = (__force __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
2559 		*off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
2560 		break;
2561 	case ICE_SW_LKUP_MAC_VLAN:
2562 		daddr = f_info->l_data.mac_vlan.mac_addr;
2563 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
2564 		break;
2565 	case ICE_SW_LKUP_PROMISC_VLAN:
2566 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
2567 		fallthrough;
2568 	case ICE_SW_LKUP_PROMISC:
2569 		daddr = f_info->l_data.mac_vlan.mac_addr;
2570 		break;
2571 	default:
2572 		break;
2573 	}
2574 
2575 	s_rule->hdr.type = (f_info->flag & ICE_FLTR_RX) ?
2576 		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
2577 		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
2578 
2579 	/* Recipe set depending on lookup type */
2580 	s_rule->recipe_id = cpu_to_le16(f_info->lkup_type);
2581 	s_rule->src = cpu_to_le16(f_info->src);
2582 	s_rule->act = cpu_to_le32(act);
2583 
2584 	if (daddr)
2585 		ether_addr_copy(eth_hdr + ICE_ETH_DA_OFFSET, daddr);
2586 
2587 	if (!(vlan_id > ICE_MAX_VLAN_ID)) {
2588 		off = (__force __be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
2589 		*off = cpu_to_be16(vlan_id);
2590 		off = (__force __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
2591 		*off = cpu_to_be16(vlan_tpid);
2592 	}
2593 
2594 	/* Create the switch rule with the final dummy Ethernet header */
2595 	if (opc != ice_aqc_opc_update_sw_rules)
2596 		s_rule->hdr_len = cpu_to_le16(eth_hdr_sz);
2597 }
2598 
2599 /**
2600  * ice_add_marker_act
2601  * @hw: pointer to the hardware structure
2602  * @m_ent: the management entry for which sw marker needs to be added
2603  * @sw_marker: sw marker to tag the Rx descriptor with
2604  * @l_id: large action resource ID
2605  *
2606  * Create a large action to hold software marker and update the switch rule
2607  * entry pointed by m_ent with newly created large action
2608  */
2609 static int
2610 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
2611 		   u16 sw_marker, u16 l_id)
2612 {
2613 	struct ice_sw_rule_lkup_rx_tx *rx_tx;
2614 	struct ice_sw_rule_lg_act *lg_act;
2615 	/* For software marker we need 3 large actions
2616 	 * 1. FWD action: FWD TO VSI or VSI LIST
2617 	 * 2. GENERIC VALUE action to hold the profile ID
2618 	 * 3. GENERIC VALUE action to hold the software marker ID
2619 	 */
2620 	const u16 num_lg_acts = 3;
2621 	u16 lg_act_size;
2622 	u16 rules_size;
2623 	int status;
2624 	u32 act;
2625 	u16 id;
2626 
2627 	if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
2628 		return -EINVAL;
2629 
2630 	/* Create two back-to-back switch rules and submit them to the HW using
2631 	 * one memory buffer:
2632 	 *    1. Large Action
2633 	 *    2. Look up Tx Rx
2634 	 */
2635 	lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(lg_act, num_lg_acts);
2636 	rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(rx_tx);
2637 	lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
2638 	if (!lg_act)
2639 		return -ENOMEM;
2640 
2641 	rx_tx = (typeof(rx_tx))((u8 *)lg_act + lg_act_size);
2642 
2643 	/* Fill in the first switch rule i.e. large action */
2644 	lg_act->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
2645 	lg_act->index = cpu_to_le16(l_id);
2646 	lg_act->size = cpu_to_le16(num_lg_acts);
2647 
2648 	/* First action VSI forwarding or VSI list forwarding depending on how
2649 	 * many VSIs
2650 	 */
2651 	id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
2652 		m_ent->fltr_info.fwd_id.hw_vsi_id;
2653 
2654 	act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
2655 	act |= FIELD_PREP(ICE_LG_ACT_VSI_LIST_ID_M, id);
2656 	if (m_ent->vsi_count > 1)
2657 		act |= ICE_LG_ACT_VSI_LIST;
2658 	lg_act->act[0] = cpu_to_le32(act);
2659 
2660 	/* Second action descriptor type */
2661 	act = ICE_LG_ACT_GENERIC;
2662 
2663 	act |= FIELD_PREP(ICE_LG_ACT_GENERIC_VALUE_M, 1);
2664 	lg_act->act[1] = cpu_to_le32(act);
2665 
2666 	act = FIELD_PREP(ICE_LG_ACT_GENERIC_OFFSET_M,
2667 			 ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX);
2668 
2669 	/* Third action Marker value */
2670 	act |= ICE_LG_ACT_GENERIC;
2671 	act |= FIELD_PREP(ICE_LG_ACT_GENERIC_VALUE_M, sw_marker);
2672 
2673 	lg_act->act[2] = cpu_to_le32(act);
2674 
2675 	/* call the fill switch rule to fill the lookup Tx Rx structure */
2676 	ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
2677 			 ice_aqc_opc_update_sw_rules);
2678 
2679 	/* Update the action to point to the large action ID */
2680 	act = ICE_SINGLE_ACT_PTR;
2681 	act |= FIELD_PREP(ICE_SINGLE_ACT_PTR_VAL_M, l_id);
2682 	rx_tx->act = cpu_to_le32(act);
2683 
2684 	/* Use the filter rule ID of the previously created rule with single
2685 	 * act. Once the update happens, hardware will treat this as large
2686 	 * action
2687 	 */
2688 	rx_tx->index = cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
2689 
2690 	status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
2691 				 ice_aqc_opc_update_sw_rules, NULL);
2692 	if (!status) {
2693 		m_ent->lg_act_idx = l_id;
2694 		m_ent->sw_marker_id = sw_marker;
2695 	}
2696 
2697 	devm_kfree(ice_hw_to_dev(hw), lg_act);
2698 	return status;
2699 }
2700 
2701 /**
2702  * ice_create_vsi_list_map
2703  * @hw: pointer to the hardware structure
2704  * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
2705  * @num_vsi: number of VSI handles in the array
2706  * @vsi_list_id: VSI list ID generated as part of allocate resource
2707  *
2708  * Helper function to create a new entry of VSI list ID to VSI mapping
2709  * using the given VSI list ID
2710  */
2711 static struct ice_vsi_list_map_info *
2712 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
2713 			u16 vsi_list_id)
2714 {
2715 	struct ice_switch_info *sw = hw->switch_info;
2716 	struct ice_vsi_list_map_info *v_map;
2717 	int i;
2718 
2719 	v_map = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*v_map), GFP_KERNEL);
2720 	if (!v_map)
2721 		return NULL;
2722 
2723 	v_map->vsi_list_id = vsi_list_id;
2724 	v_map->ref_cnt = 1;
2725 	for (i = 0; i < num_vsi; i++)
2726 		set_bit(vsi_handle_arr[i], v_map->vsi_map);
2727 
2728 	list_add(&v_map->list_entry, &sw->vsi_list_map_head);
2729 	return v_map;
2730 }
2731 
2732 /**
2733  * ice_update_vsi_list_rule
2734  * @hw: pointer to the hardware structure
2735  * @vsi_handle_arr: array of VSI handles to form a VSI list
2736  * @num_vsi: number of VSI handles in the array
2737  * @vsi_list_id: VSI list ID generated as part of allocate resource
2738  * @remove: Boolean value to indicate if this is a remove action
2739  * @opc: switch rules population command type - pass in the command opcode
2740  * @lkup_type: lookup type of the filter
2741  *
2742  * Call AQ command to add a new switch rule or update existing switch rule
2743  * using the given VSI list ID
2744  */
2745 static int
2746 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
2747 			 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
2748 			 enum ice_sw_lkup_type lkup_type)
2749 {
2750 	struct ice_sw_rule_vsi_list *s_rule;
2751 	u16 s_rule_size;
2752 	u16 rule_type;
2753 	int status;
2754 	int i;
2755 
2756 	if (!num_vsi)
2757 		return -EINVAL;
2758 
2759 	if (lkup_type == ICE_SW_LKUP_MAC ||
2760 	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
2761 	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
2762 	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
2763 	    lkup_type == ICE_SW_LKUP_PROMISC ||
2764 	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
2765 	    lkup_type == ICE_SW_LKUP_DFLT)
2766 		rule_type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
2767 			ICE_AQC_SW_RULES_T_VSI_LIST_SET;
2768 	else if (lkup_type == ICE_SW_LKUP_VLAN)
2769 		rule_type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
2770 			ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
2771 	else
2772 		return -EINVAL;
2773 
2774 	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, num_vsi);
2775 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
2776 	if (!s_rule)
2777 		return -ENOMEM;
2778 	for (i = 0; i < num_vsi; i++) {
2779 		if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
2780 			status = -EINVAL;
2781 			goto exit;
2782 		}
2783 		/* AQ call requires hw_vsi_id(s) */
2784 		s_rule->vsi[i] =
2785 			cpu_to_le16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
2786 	}
2787 
2788 	s_rule->hdr.type = cpu_to_le16(rule_type);
2789 	s_rule->number_vsi = cpu_to_le16(num_vsi);
2790 	s_rule->index = cpu_to_le16(vsi_list_id);
2791 
2792 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
2793 
2794 exit:
2795 	devm_kfree(ice_hw_to_dev(hw), s_rule);
2796 	return status;
2797 }
2798 
2799 /**
2800  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
2801  * @hw: pointer to the HW struct
2802  * @vsi_handle_arr: array of VSI handles to form a VSI list
2803  * @num_vsi: number of VSI handles in the array
2804  * @vsi_list_id: stores the ID of the VSI list to be created
2805  * @lkup_type: switch rule filter's lookup type
2806  */
2807 static int
2808 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
2809 			 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
2810 {
2811 	int status;
2812 
2813 	status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
2814 					    ice_aqc_opc_alloc_res);
2815 	if (status)
2816 		return status;
2817 
2818 	/* Update the newly created VSI list to include the specified VSIs */
2819 	return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
2820 					*vsi_list_id, false,
2821 					ice_aqc_opc_add_sw_rules, lkup_type);
2822 }
2823 
2824 /**
2825  * ice_create_pkt_fwd_rule
2826  * @hw: pointer to the hardware structure
2827  * @f_entry: entry containing packet forwarding information
2828  *
2829  * Create switch rule with given filter information and add an entry
2830  * to the corresponding filter management list to track this switch rule
2831  * and VSI mapping
2832  */
2833 static int
2834 ice_create_pkt_fwd_rule(struct ice_hw *hw,
2835 			struct ice_fltr_list_entry *f_entry)
2836 {
2837 	struct ice_fltr_mgmt_list_entry *fm_entry;
2838 	struct ice_sw_rule_lkup_rx_tx *s_rule;
2839 	enum ice_sw_lkup_type l_type;
2840 	struct ice_sw_recipe *recp;
2841 	int status;
2842 
2843 	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
2844 			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule),
2845 			      GFP_KERNEL);
2846 	if (!s_rule)
2847 		return -ENOMEM;
2848 	fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
2849 				GFP_KERNEL);
2850 	if (!fm_entry) {
2851 		status = -ENOMEM;
2852 		goto ice_create_pkt_fwd_rule_exit;
2853 	}
2854 
2855 	fm_entry->fltr_info = f_entry->fltr_info;
2856 
2857 	/* Initialize all the fields for the management entry */
2858 	fm_entry->vsi_count = 1;
2859 	fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
2860 	fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
2861 	fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
2862 
2863 	ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
2864 			 ice_aqc_opc_add_sw_rules);
2865 
2866 	status = ice_aq_sw_rules(hw, s_rule,
2867 				 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule), 1,
2868 				 ice_aqc_opc_add_sw_rules, NULL);
2869 	if (status) {
2870 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
2871 		goto ice_create_pkt_fwd_rule_exit;
2872 	}
2873 
2874 	f_entry->fltr_info.fltr_rule_id = le16_to_cpu(s_rule->index);
2875 	fm_entry->fltr_info.fltr_rule_id = le16_to_cpu(s_rule->index);
2876 
2877 	/* The book keeping entries will get removed when base driver
2878 	 * calls remove filter AQ command
2879 	 */
2880 	l_type = fm_entry->fltr_info.lkup_type;
2881 	recp = &hw->switch_info->recp_list[l_type];
2882 	list_add(&fm_entry->list_entry, &recp->filt_rules);
2883 
2884 ice_create_pkt_fwd_rule_exit:
2885 	devm_kfree(ice_hw_to_dev(hw), s_rule);
2886 	return status;
2887 }
2888 
2889 /**
2890  * ice_update_pkt_fwd_rule
2891  * @hw: pointer to the hardware structure
2892  * @f_info: filter information for switch rule
2893  *
2894  * Call AQ command to update a previously created switch rule with a
2895  * VSI list ID
2896  */
2897 static int
2898 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
2899 {
2900 	struct ice_sw_rule_lkup_rx_tx *s_rule;
2901 	int status;
2902 
2903 	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
2904 			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule),
2905 			      GFP_KERNEL);
2906 	if (!s_rule)
2907 		return -ENOMEM;
2908 
2909 	ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
2910 
2911 	s_rule->index = cpu_to_le16(f_info->fltr_rule_id);
2912 
2913 	/* Update switch rule with new rule set to forward VSI list */
2914 	status = ice_aq_sw_rules(hw, s_rule,
2915 				 ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule), 1,
2916 				 ice_aqc_opc_update_sw_rules, NULL);
2917 
2918 	devm_kfree(ice_hw_to_dev(hw), s_rule);
2919 	return status;
2920 }
2921 
2922 /**
2923  * ice_update_sw_rule_bridge_mode
2924  * @hw: pointer to the HW struct
2925  *
2926  * Updates unicast switch filter rules based on VEB/VEPA mode
2927  */
2928 int ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
2929 {
2930 	struct ice_switch_info *sw = hw->switch_info;
2931 	struct ice_fltr_mgmt_list_entry *fm_entry;
2932 	struct list_head *rule_head;
2933 	struct mutex *rule_lock; /* Lock to protect filter rule list */
2934 	int status = 0;
2935 
2936 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2937 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2938 
2939 	mutex_lock(rule_lock);
2940 	list_for_each_entry(fm_entry, rule_head, list_entry) {
2941 		struct ice_fltr_info *fi = &fm_entry->fltr_info;
2942 		u8 *addr = fi->l_data.mac.mac_addr;
2943 
2944 		/* Update unicast Tx rules to reflect the selected
2945 		 * VEB/VEPA mode
2946 		 */
2947 		if ((fi->flag & ICE_FLTR_TX) && is_unicast_ether_addr(addr) &&
2948 		    (fi->fltr_act == ICE_FWD_TO_VSI ||
2949 		     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
2950 		     fi->fltr_act == ICE_FWD_TO_Q ||
2951 		     fi->fltr_act == ICE_FWD_TO_QGRP)) {
2952 			status = ice_update_pkt_fwd_rule(hw, fi);
2953 			if (status)
2954 				break;
2955 		}
2956 	}
2957 
2958 	mutex_unlock(rule_lock);
2959 
2960 	return status;
2961 }
2962 
2963 /**
2964  * ice_add_update_vsi_list
2965  * @hw: pointer to the hardware structure
2966  * @m_entry: pointer to current filter management list entry
2967  * @cur_fltr: filter information from the book keeping entry
2968  * @new_fltr: filter information with the new VSI to be added
2969  *
2970  * Call AQ command to add or update previously created VSI list with new VSI.
2971  *
2972  * Helper function to do book keeping associated with adding filter information
2973  * The algorithm to do the book keeping is described below :
2974  * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
2975  *	if only one VSI has been added till now
2976  *		Allocate a new VSI list and add two VSIs
2977  *		to this list using switch rule command
2978  *		Update the previously created switch rule with the
2979  *		newly created VSI list ID
2980  *	if a VSI list was previously created
2981  *		Add the new VSI to the previously created VSI list set
2982  *		using the update switch rule command
2983  */
2984 static int
2985 ice_add_update_vsi_list(struct ice_hw *hw,
2986 			struct ice_fltr_mgmt_list_entry *m_entry,
2987 			struct ice_fltr_info *cur_fltr,
2988 			struct ice_fltr_info *new_fltr)
2989 {
2990 	u16 vsi_list_id = 0;
2991 	int status = 0;
2992 
2993 	if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
2994 	     cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
2995 		return -EOPNOTSUPP;
2996 
2997 	if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
2998 	     new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
2999 	    (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
3000 	     cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
3001 		return -EOPNOTSUPP;
3002 
3003 	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
3004 		/* Only one entry existed in the mapping and it was not already
3005 		 * a part of a VSI list. So, create a VSI list with the old and
3006 		 * new VSIs.
3007 		 */
3008 		struct ice_fltr_info tmp_fltr;
3009 		u16 vsi_handle_arr[2];
3010 
3011 		/* A rule already exists with the new VSI being added */
3012 		if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
3013 			return -EEXIST;
3014 
3015 		vsi_handle_arr[0] = cur_fltr->vsi_handle;
3016 		vsi_handle_arr[1] = new_fltr->vsi_handle;
3017 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
3018 						  &vsi_list_id,
3019 						  new_fltr->lkup_type);
3020 		if (status)
3021 			return status;
3022 
3023 		tmp_fltr = *new_fltr;
3024 		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
3025 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
3026 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
3027 		/* Update the previous switch rule of "MAC forward to VSI" to
3028 		 * "MAC fwd to VSI list"
3029 		 */
3030 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
3031 		if (status)
3032 			return status;
3033 
3034 		cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
3035 		cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
3036 		m_entry->vsi_list_info =
3037 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
3038 						vsi_list_id);
3039 
3040 		if (!m_entry->vsi_list_info)
3041 			return -ENOMEM;
3042 
3043 		/* If this entry was large action then the large action needs
3044 		 * to be updated to point to FWD to VSI list
3045 		 */
3046 		if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
3047 			status =
3048 			    ice_add_marker_act(hw, m_entry,
3049 					       m_entry->sw_marker_id,
3050 					       m_entry->lg_act_idx);
3051 	} else {
3052 		u16 vsi_handle = new_fltr->vsi_handle;
3053 		enum ice_adminq_opc opcode;
3054 
3055 		if (!m_entry->vsi_list_info)
3056 			return -EIO;
3057 
3058 		/* A rule already exists with the new VSI being added */
3059 		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
3060 			return 0;
3061 
3062 		/* Update the previously created VSI list set with
3063 		 * the new VSI ID passed in
3064 		 */
3065 		vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
3066 		opcode = ice_aqc_opc_update_sw_rules;
3067 
3068 		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
3069 						  vsi_list_id, false, opcode,
3070 						  new_fltr->lkup_type);
3071 		/* update VSI list mapping info with new VSI ID */
3072 		if (!status)
3073 			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
3074 	}
3075 	if (!status)
3076 		m_entry->vsi_count++;
3077 	return status;
3078 }
3079 
3080 /**
3081  * ice_find_rule_entry - Search a rule entry
3082  * @hw: pointer to the hardware structure
3083  * @recp_id: lookup type for which the specified rule needs to be searched
3084  * @f_info: rule information
3085  *
3086  * Helper function to search for a given rule entry
3087  * Returns pointer to entry storing the rule if found
3088  */
3089 static struct ice_fltr_mgmt_list_entry *
3090 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
3091 {
3092 	struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
3093 	struct ice_switch_info *sw = hw->switch_info;
3094 	struct list_head *list_head;
3095 
3096 	list_head = &sw->recp_list[recp_id].filt_rules;
3097 	list_for_each_entry(list_itr, list_head, list_entry) {
3098 		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
3099 			    sizeof(f_info->l_data)) &&
3100 		    f_info->flag == list_itr->fltr_info.flag) {
3101 			ret = list_itr;
3102 			break;
3103 		}
3104 	}
3105 	return ret;
3106 }
3107 
3108 /**
3109  * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
3110  * @hw: pointer to the hardware structure
3111  * @recp_id: lookup type for which VSI lists needs to be searched
3112  * @vsi_handle: VSI handle to be found in VSI list
3113  * @vsi_list_id: VSI list ID found containing vsi_handle
3114  *
3115  * Helper function to search a VSI list with single entry containing given VSI
3116  * handle element. This can be extended further to search VSI list with more
3117  * than 1 vsi_count. Returns pointer to VSI list entry if found.
3118  */
3119 struct ice_vsi_list_map_info *
3120 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
3121 			u16 *vsi_list_id)
3122 {
3123 	struct ice_vsi_list_map_info *map_info = NULL;
3124 	struct ice_switch_info *sw = hw->switch_info;
3125 	struct ice_fltr_mgmt_list_entry *list_itr;
3126 	struct list_head *list_head;
3127 
3128 	list_head = &sw->recp_list[recp_id].filt_rules;
3129 	list_for_each_entry(list_itr, list_head, list_entry) {
3130 		if (list_itr->vsi_list_info) {
3131 			map_info = list_itr->vsi_list_info;
3132 			if (test_bit(vsi_handle, map_info->vsi_map)) {
3133 				*vsi_list_id = map_info->vsi_list_id;
3134 				return map_info;
3135 			}
3136 		}
3137 	}
3138 	return NULL;
3139 }
3140 
3141 /**
3142  * ice_add_rule_internal - add rule for a given lookup type
3143  * @hw: pointer to the hardware structure
3144  * @recp_id: lookup type (recipe ID) for which rule has to be added
3145  * @f_entry: structure containing MAC forwarding information
3146  *
3147  * Adds or updates the rule lists for a given recipe
3148  */
3149 static int
3150 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
3151 		      struct ice_fltr_list_entry *f_entry)
3152 {
3153 	struct ice_switch_info *sw = hw->switch_info;
3154 	struct ice_fltr_info *new_fltr, *cur_fltr;
3155 	struct ice_fltr_mgmt_list_entry *m_entry;
3156 	struct mutex *rule_lock; /* Lock to protect filter rule list */
3157 	int status = 0;
3158 
3159 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
3160 		return -EINVAL;
3161 	f_entry->fltr_info.fwd_id.hw_vsi_id =
3162 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
3163 
3164 	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
3165 
3166 	mutex_lock(rule_lock);
3167 	new_fltr = &f_entry->fltr_info;
3168 	if (new_fltr->flag & ICE_FLTR_RX)
3169 		new_fltr->src = hw->port_info->lport;
3170 	else if (new_fltr->flag & ICE_FLTR_TX)
3171 		new_fltr->src = f_entry->fltr_info.fwd_id.hw_vsi_id;
3172 
3173 	m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
3174 	if (!m_entry) {
3175 		mutex_unlock(rule_lock);
3176 		return ice_create_pkt_fwd_rule(hw, f_entry);
3177 	}
3178 
3179 	cur_fltr = &m_entry->fltr_info;
3180 	status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
3181 	mutex_unlock(rule_lock);
3182 
3183 	return status;
3184 }
3185 
3186 /**
3187  * ice_remove_vsi_list_rule
3188  * @hw: pointer to the hardware structure
3189  * @vsi_list_id: VSI list ID generated as part of allocate resource
3190  * @lkup_type: switch rule filter lookup type
3191  *
3192  * The VSI list should be emptied before this function is called to remove the
3193  * VSI list.
3194  */
3195 static int
3196 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
3197 			 enum ice_sw_lkup_type lkup_type)
3198 {
3199 	struct ice_sw_rule_vsi_list *s_rule;
3200 	u16 s_rule_size;
3201 	int status;
3202 
3203 	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, 0);
3204 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
3205 	if (!s_rule)
3206 		return -ENOMEM;
3207 
3208 	s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
3209 	s_rule->index = cpu_to_le16(vsi_list_id);
3210 
3211 	/* Free the vsi_list resource that we allocated. It is assumed that the
3212 	 * list is empty at this point.
3213 	 */
3214 	status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
3215 					    ice_aqc_opc_free_res);
3216 
3217 	devm_kfree(ice_hw_to_dev(hw), s_rule);
3218 	return status;
3219 }
3220 
3221 /**
3222  * ice_rem_update_vsi_list
3223  * @hw: pointer to the hardware structure
3224  * @vsi_handle: VSI handle of the VSI to remove
3225  * @fm_list: filter management entry for which the VSI list management needs to
3226  *           be done
3227  */
3228 static int
3229 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
3230 			struct ice_fltr_mgmt_list_entry *fm_list)
3231 {
3232 	enum ice_sw_lkup_type lkup_type;
3233 	u16 vsi_list_id;
3234 	int status = 0;
3235 
3236 	if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
3237 	    fm_list->vsi_count == 0)
3238 		return -EINVAL;
3239 
3240 	/* A rule with the VSI being removed does not exist */
3241 	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
3242 		return -ENOENT;
3243 
3244 	lkup_type = fm_list->fltr_info.lkup_type;
3245 	vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
3246 	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
3247 					  ice_aqc_opc_update_sw_rules,
3248 					  lkup_type);
3249 	if (status)
3250 		return status;
3251 
3252 	fm_list->vsi_count--;
3253 	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
3254 
3255 	if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
3256 		struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
3257 		struct ice_vsi_list_map_info *vsi_list_info =
3258 			fm_list->vsi_list_info;
3259 		u16 rem_vsi_handle;
3260 
3261 		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
3262 						ICE_MAX_VSI);
3263 		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
3264 			return -EIO;
3265 
3266 		/* Make sure VSI list is empty before removing it below */
3267 		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
3268 						  vsi_list_id, true,
3269 						  ice_aqc_opc_update_sw_rules,
3270 						  lkup_type);
3271 		if (status)
3272 			return status;
3273 
3274 		tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
3275 		tmp_fltr_info.fwd_id.hw_vsi_id =
3276 			ice_get_hw_vsi_num(hw, rem_vsi_handle);
3277 		tmp_fltr_info.vsi_handle = rem_vsi_handle;
3278 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
3279 		if (status) {
3280 			ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
3281 				  tmp_fltr_info.fwd_id.hw_vsi_id, status);
3282 			return status;
3283 		}
3284 
3285 		fm_list->fltr_info = tmp_fltr_info;
3286 	}
3287 
3288 	if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
3289 	    (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
3290 		struct ice_vsi_list_map_info *vsi_list_info =
3291 			fm_list->vsi_list_info;
3292 
3293 		/* Remove the VSI list since it is no longer used */
3294 		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
3295 		if (status) {
3296 			ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
3297 				  vsi_list_id, status);
3298 			return status;
3299 		}
3300 
3301 		list_del(&vsi_list_info->list_entry);
3302 		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
3303 		fm_list->vsi_list_info = NULL;
3304 	}
3305 
3306 	return status;
3307 }
3308 
3309 /**
3310  * ice_remove_rule_internal - Remove a filter rule of a given type
3311  * @hw: pointer to the hardware structure
3312  * @recp_id: recipe ID for which the rule needs to removed
3313  * @f_entry: rule entry containing filter information
3314  */
3315 static int
3316 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
3317 			 struct ice_fltr_list_entry *f_entry)
3318 {
3319 	struct ice_switch_info *sw = hw->switch_info;
3320 	struct ice_fltr_mgmt_list_entry *list_elem;
3321 	struct mutex *rule_lock; /* Lock to protect filter rule list */
3322 	bool remove_rule = false;
3323 	u16 vsi_handle;
3324 	int status = 0;
3325 
3326 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
3327 		return -EINVAL;
3328 	f_entry->fltr_info.fwd_id.hw_vsi_id =
3329 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
3330 
3331 	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
3332 	mutex_lock(rule_lock);
3333 	list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
3334 	if (!list_elem) {
3335 		status = -ENOENT;
3336 		goto exit;
3337 	}
3338 
3339 	if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
3340 		remove_rule = true;
3341 	} else if (!list_elem->vsi_list_info) {
3342 		status = -ENOENT;
3343 		goto exit;
3344 	} else if (list_elem->vsi_list_info->ref_cnt > 1) {
3345 		/* a ref_cnt > 1 indicates that the vsi_list is being
3346 		 * shared by multiple rules. Decrement the ref_cnt and
3347 		 * remove this rule, but do not modify the list, as it
3348 		 * is in-use by other rules.
3349 		 */
3350 		list_elem->vsi_list_info->ref_cnt--;
3351 		remove_rule = true;
3352 	} else {
3353 		/* a ref_cnt of 1 indicates the vsi_list is only used
3354 		 * by one rule. However, the original removal request is only
3355 		 * for a single VSI. Update the vsi_list first, and only
3356 		 * remove the rule if there are no further VSIs in this list.
3357 		 */
3358 		vsi_handle = f_entry->fltr_info.vsi_handle;
3359 		status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
3360 		if (status)
3361 			goto exit;
3362 		/* if VSI count goes to zero after updating the VSI list */
3363 		if (list_elem->vsi_count == 0)
3364 			remove_rule = true;
3365 	}
3366 
3367 	if (remove_rule) {
3368 		/* Remove the lookup rule */
3369 		struct ice_sw_rule_lkup_rx_tx *s_rule;
3370 
3371 		s_rule = devm_kzalloc(ice_hw_to_dev(hw),
3372 				      ICE_SW_RULE_RX_TX_NO_HDR_SIZE(s_rule),
3373 				      GFP_KERNEL);
3374 		if (!s_rule) {
3375 			status = -ENOMEM;
3376 			goto exit;
3377 		}
3378 
3379 		ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
3380 				 ice_aqc_opc_remove_sw_rules);
3381 
3382 		status = ice_aq_sw_rules(hw, s_rule,
3383 					 ICE_SW_RULE_RX_TX_NO_HDR_SIZE(s_rule),
3384 					 1, ice_aqc_opc_remove_sw_rules, NULL);
3385 
3386 		/* Remove a book keeping from the list */
3387 		devm_kfree(ice_hw_to_dev(hw), s_rule);
3388 
3389 		if (status)
3390 			goto exit;
3391 
3392 		list_del(&list_elem->list_entry);
3393 		devm_kfree(ice_hw_to_dev(hw), list_elem);
3394 	}
3395 exit:
3396 	mutex_unlock(rule_lock);
3397 	return status;
3398 }
3399 
3400 /**
3401  * ice_vlan_fltr_exist - does this VLAN filter exist for given VSI
3402  * @hw: pointer to the hardware structure
3403  * @vlan_id: VLAN ID
3404  * @vsi_handle: check MAC filter for this VSI
3405  */
3406 bool ice_vlan_fltr_exist(struct ice_hw *hw, u16 vlan_id, u16 vsi_handle)
3407 {
3408 	struct ice_fltr_mgmt_list_entry *entry;
3409 	struct list_head *rule_head;
3410 	struct ice_switch_info *sw;
3411 	struct mutex *rule_lock; /* Lock to protect filter rule list */
3412 	u16 hw_vsi_id;
3413 
3414 	if (vlan_id > ICE_MAX_VLAN_ID)
3415 		return false;
3416 
3417 	if (!ice_is_vsi_valid(hw, vsi_handle))
3418 		return false;
3419 
3420 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3421 	sw = hw->switch_info;
3422 	rule_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3423 	if (!rule_head)
3424 		return false;
3425 
3426 	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3427 	mutex_lock(rule_lock);
3428 	list_for_each_entry(entry, rule_head, list_entry) {
3429 		struct ice_fltr_info *f_info = &entry->fltr_info;
3430 		u16 entry_vlan_id = f_info->l_data.vlan.vlan_id;
3431 		struct ice_vsi_list_map_info *map_info;
3432 
3433 		if (entry_vlan_id > ICE_MAX_VLAN_ID)
3434 			continue;
3435 
3436 		if (f_info->flag != ICE_FLTR_TX ||
3437 		    f_info->src_id != ICE_SRC_ID_VSI ||
3438 		    f_info->lkup_type != ICE_SW_LKUP_VLAN)
3439 			continue;
3440 
3441 		/* Only allowed filter action are FWD_TO_VSI/_VSI_LIST */
3442 		if (f_info->fltr_act != ICE_FWD_TO_VSI &&
3443 		    f_info->fltr_act != ICE_FWD_TO_VSI_LIST)
3444 			continue;
3445 
3446 		if (f_info->fltr_act == ICE_FWD_TO_VSI) {
3447 			if (hw_vsi_id != f_info->fwd_id.hw_vsi_id)
3448 				continue;
3449 		} else if (f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
3450 			/* If filter_action is FWD_TO_VSI_LIST, make sure
3451 			 * that VSI being checked is part of VSI list
3452 			 */
3453 			if (entry->vsi_count == 1 &&
3454 			    entry->vsi_list_info) {
3455 				map_info = entry->vsi_list_info;
3456 				if (!test_bit(vsi_handle, map_info->vsi_map))
3457 					continue;
3458 			}
3459 		}
3460 
3461 		if (vlan_id == entry_vlan_id) {
3462 			mutex_unlock(rule_lock);
3463 			return true;
3464 		}
3465 	}
3466 	mutex_unlock(rule_lock);
3467 
3468 	return false;
3469 }
3470 
3471 /**
3472  * ice_add_mac - Add a MAC address based filter rule
3473  * @hw: pointer to the hardware structure
3474  * @m_list: list of MAC addresses and forwarding information
3475  */
3476 int ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
3477 {
3478 	struct ice_fltr_list_entry *m_list_itr;
3479 	int status = 0;
3480 
3481 	if (!m_list || !hw)
3482 		return -EINVAL;
3483 
3484 	list_for_each_entry(m_list_itr, m_list, list_entry) {
3485 		u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
3486 		u16 vsi_handle;
3487 		u16 hw_vsi_id;
3488 
3489 		m_list_itr->fltr_info.flag = ICE_FLTR_TX;
3490 		vsi_handle = m_list_itr->fltr_info.vsi_handle;
3491 		if (!ice_is_vsi_valid(hw, vsi_handle))
3492 			return -EINVAL;
3493 		hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3494 		m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
3495 		/* update the src in case it is VSI num */
3496 		if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
3497 			return -EINVAL;
3498 		m_list_itr->fltr_info.src = hw_vsi_id;
3499 		if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
3500 		    is_zero_ether_addr(add))
3501 			return -EINVAL;
3502 
3503 		m_list_itr->status = ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
3504 							   m_list_itr);
3505 		if (m_list_itr->status)
3506 			return m_list_itr->status;
3507 	}
3508 
3509 	return status;
3510 }
3511 
3512 /**
3513  * ice_add_vlan_internal - Add one VLAN based filter rule
3514  * @hw: pointer to the hardware structure
3515  * @f_entry: filter entry containing one VLAN information
3516  */
3517 static int
3518 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
3519 {
3520 	struct ice_switch_info *sw = hw->switch_info;
3521 	struct ice_fltr_mgmt_list_entry *v_list_itr;
3522 	struct ice_fltr_info *new_fltr, *cur_fltr;
3523 	enum ice_sw_lkup_type lkup_type;
3524 	u16 vsi_list_id = 0, vsi_handle;
3525 	struct mutex *rule_lock; /* Lock to protect filter rule list */
3526 	int status = 0;
3527 
3528 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
3529 		return -EINVAL;
3530 
3531 	f_entry->fltr_info.fwd_id.hw_vsi_id =
3532 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
3533 	new_fltr = &f_entry->fltr_info;
3534 
3535 	/* VLAN ID should only be 12 bits */
3536 	if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
3537 		return -EINVAL;
3538 
3539 	if (new_fltr->src_id != ICE_SRC_ID_VSI)
3540 		return -EINVAL;
3541 
3542 	new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
3543 	lkup_type = new_fltr->lkup_type;
3544 	vsi_handle = new_fltr->vsi_handle;
3545 	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3546 	mutex_lock(rule_lock);
3547 	v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
3548 	if (!v_list_itr) {
3549 		struct ice_vsi_list_map_info *map_info = NULL;
3550 
3551 		if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
3552 			/* All VLAN pruning rules use a VSI list. Check if
3553 			 * there is already a VSI list containing VSI that we
3554 			 * want to add. If found, use the same vsi_list_id for
3555 			 * this new VLAN rule or else create a new list.
3556 			 */
3557 			map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
3558 							   vsi_handle,
3559 							   &vsi_list_id);
3560 			if (!map_info) {
3561 				status = ice_create_vsi_list_rule(hw,
3562 								  &vsi_handle,
3563 								  1,
3564 								  &vsi_list_id,
3565 								  lkup_type);
3566 				if (status)
3567 					goto exit;
3568 			}
3569 			/* Convert the action to forwarding to a VSI list. */
3570 			new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
3571 			new_fltr->fwd_id.vsi_list_id = vsi_list_id;
3572 		}
3573 
3574 		status = ice_create_pkt_fwd_rule(hw, f_entry);
3575 		if (!status) {
3576 			v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
3577 							 new_fltr);
3578 			if (!v_list_itr) {
3579 				status = -ENOENT;
3580 				goto exit;
3581 			}
3582 			/* reuse VSI list for new rule and increment ref_cnt */
3583 			if (map_info) {
3584 				v_list_itr->vsi_list_info = map_info;
3585 				map_info->ref_cnt++;
3586 			} else {
3587 				v_list_itr->vsi_list_info =
3588 					ice_create_vsi_list_map(hw, &vsi_handle,
3589 								1, vsi_list_id);
3590 			}
3591 		}
3592 	} else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
3593 		/* Update existing VSI list to add new VSI ID only if it used
3594 		 * by one VLAN rule.
3595 		 */
3596 		cur_fltr = &v_list_itr->fltr_info;
3597 		status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
3598 						 new_fltr);
3599 	} else {
3600 		/* If VLAN rule exists and VSI list being used by this rule is
3601 		 * referenced by more than 1 VLAN rule. Then create a new VSI
3602 		 * list appending previous VSI with new VSI and update existing
3603 		 * VLAN rule to point to new VSI list ID
3604 		 */
3605 		struct ice_fltr_info tmp_fltr;
3606 		u16 vsi_handle_arr[2];
3607 		u16 cur_handle;
3608 
3609 		/* Current implementation only supports reusing VSI list with
3610 		 * one VSI count. We should never hit below condition
3611 		 */
3612 		if (v_list_itr->vsi_count > 1 &&
3613 		    v_list_itr->vsi_list_info->ref_cnt > 1) {
3614 			ice_debug(hw, ICE_DBG_SW, "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
3615 			status = -EIO;
3616 			goto exit;
3617 		}
3618 
3619 		cur_handle =
3620 			find_first_bit(v_list_itr->vsi_list_info->vsi_map,
3621 				       ICE_MAX_VSI);
3622 
3623 		/* A rule already exists with the new VSI being added */
3624 		if (cur_handle == vsi_handle) {
3625 			status = -EEXIST;
3626 			goto exit;
3627 		}
3628 
3629 		vsi_handle_arr[0] = cur_handle;
3630 		vsi_handle_arr[1] = vsi_handle;
3631 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
3632 						  &vsi_list_id, lkup_type);
3633 		if (status)
3634 			goto exit;
3635 
3636 		tmp_fltr = v_list_itr->fltr_info;
3637 		tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
3638 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
3639 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
3640 		/* Update the previous switch rule to a new VSI list which
3641 		 * includes current VSI that is requested
3642 		 */
3643 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
3644 		if (status)
3645 			goto exit;
3646 
3647 		/* before overriding VSI list map info. decrement ref_cnt of
3648 		 * previous VSI list
3649 		 */
3650 		v_list_itr->vsi_list_info->ref_cnt--;
3651 
3652 		/* now update to newly created list */
3653 		v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
3654 		v_list_itr->vsi_list_info =
3655 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
3656 						vsi_list_id);
3657 		v_list_itr->vsi_count++;
3658 	}
3659 
3660 exit:
3661 	mutex_unlock(rule_lock);
3662 	return status;
3663 }
3664 
3665 /**
3666  * ice_add_vlan - Add VLAN based filter rule
3667  * @hw: pointer to the hardware structure
3668  * @v_list: list of VLAN entries and forwarding information
3669  */
3670 int ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
3671 {
3672 	struct ice_fltr_list_entry *v_list_itr;
3673 
3674 	if (!v_list || !hw)
3675 		return -EINVAL;
3676 
3677 	list_for_each_entry(v_list_itr, v_list, list_entry) {
3678 		if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
3679 			return -EINVAL;
3680 		v_list_itr->fltr_info.flag = ICE_FLTR_TX;
3681 		v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
3682 		if (v_list_itr->status)
3683 			return v_list_itr->status;
3684 	}
3685 	return 0;
3686 }
3687 
3688 /**
3689  * ice_add_eth_mac - Add ethertype and MAC based filter rule
3690  * @hw: pointer to the hardware structure
3691  * @em_list: list of ether type MAC filter, MAC is optional
3692  *
3693  * This function requires the caller to populate the entries in
3694  * the filter list with the necessary fields (including flags to
3695  * indicate Tx or Rx rules).
3696  */
3697 int ice_add_eth_mac(struct ice_hw *hw, struct list_head *em_list)
3698 {
3699 	struct ice_fltr_list_entry *em_list_itr;
3700 
3701 	if (!em_list || !hw)
3702 		return -EINVAL;
3703 
3704 	list_for_each_entry(em_list_itr, em_list, list_entry) {
3705 		enum ice_sw_lkup_type l_type =
3706 			em_list_itr->fltr_info.lkup_type;
3707 
3708 		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
3709 		    l_type != ICE_SW_LKUP_ETHERTYPE)
3710 			return -EINVAL;
3711 
3712 		em_list_itr->status = ice_add_rule_internal(hw, l_type,
3713 							    em_list_itr);
3714 		if (em_list_itr->status)
3715 			return em_list_itr->status;
3716 	}
3717 	return 0;
3718 }
3719 
3720 /**
3721  * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
3722  * @hw: pointer to the hardware structure
3723  * @em_list: list of ethertype or ethertype MAC entries
3724  */
3725 int ice_remove_eth_mac(struct ice_hw *hw, struct list_head *em_list)
3726 {
3727 	struct ice_fltr_list_entry *em_list_itr, *tmp;
3728 
3729 	if (!em_list || !hw)
3730 		return -EINVAL;
3731 
3732 	list_for_each_entry_safe(em_list_itr, tmp, em_list, list_entry) {
3733 		enum ice_sw_lkup_type l_type =
3734 			em_list_itr->fltr_info.lkup_type;
3735 
3736 		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
3737 		    l_type != ICE_SW_LKUP_ETHERTYPE)
3738 			return -EINVAL;
3739 
3740 		em_list_itr->status = ice_remove_rule_internal(hw, l_type,
3741 							       em_list_itr);
3742 		if (em_list_itr->status)
3743 			return em_list_itr->status;
3744 	}
3745 	return 0;
3746 }
3747 
3748 /**
3749  * ice_rem_sw_rule_info
3750  * @hw: pointer to the hardware structure
3751  * @rule_head: pointer to the switch list structure that we want to delete
3752  */
3753 static void
3754 ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
3755 {
3756 	if (!list_empty(rule_head)) {
3757 		struct ice_fltr_mgmt_list_entry *entry;
3758 		struct ice_fltr_mgmt_list_entry *tmp;
3759 
3760 		list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
3761 			list_del(&entry->list_entry);
3762 			devm_kfree(ice_hw_to_dev(hw), entry);
3763 		}
3764 	}
3765 }
3766 
3767 /**
3768  * ice_rem_adv_rule_info
3769  * @hw: pointer to the hardware structure
3770  * @rule_head: pointer to the switch list structure that we want to delete
3771  */
3772 static void
3773 ice_rem_adv_rule_info(struct ice_hw *hw, struct list_head *rule_head)
3774 {
3775 	struct ice_adv_fltr_mgmt_list_entry *tmp_entry;
3776 	struct ice_adv_fltr_mgmt_list_entry *lst_itr;
3777 
3778 	if (list_empty(rule_head))
3779 		return;
3780 
3781 	list_for_each_entry_safe(lst_itr, tmp_entry, rule_head, list_entry) {
3782 		list_del(&lst_itr->list_entry);
3783 		devm_kfree(ice_hw_to_dev(hw), lst_itr->lkups);
3784 		devm_kfree(ice_hw_to_dev(hw), lst_itr);
3785 	}
3786 }
3787 
3788 /**
3789  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
3790  * @pi: pointer to the port_info structure
3791  * @vsi_handle: VSI handle to set as default
3792  * @set: true to add the above mentioned switch rule, false to remove it
3793  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
3794  *
3795  * add filter rule to set/unset given VSI as default VSI for the switch
3796  * (represented by swid)
3797  */
3798 int
3799 ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
3800 		 u8 direction)
3801 {
3802 	struct ice_fltr_list_entry f_list_entry;
3803 	struct ice_fltr_info f_info;
3804 	struct ice_hw *hw = pi->hw;
3805 	u16 hw_vsi_id;
3806 	int status;
3807 
3808 	if (!ice_is_vsi_valid(hw, vsi_handle))
3809 		return -EINVAL;
3810 
3811 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3812 
3813 	memset(&f_info, 0, sizeof(f_info));
3814 
3815 	f_info.lkup_type = ICE_SW_LKUP_DFLT;
3816 	f_info.flag = direction;
3817 	f_info.fltr_act = ICE_FWD_TO_VSI;
3818 	f_info.fwd_id.hw_vsi_id = hw_vsi_id;
3819 	f_info.vsi_handle = vsi_handle;
3820 
3821 	if (f_info.flag & ICE_FLTR_RX) {
3822 		f_info.src = hw->port_info->lport;
3823 		f_info.src_id = ICE_SRC_ID_LPORT;
3824 	} else if (f_info.flag & ICE_FLTR_TX) {
3825 		f_info.src_id = ICE_SRC_ID_VSI;
3826 		f_info.src = hw_vsi_id;
3827 		f_info.flag |= ICE_FLTR_TX_ONLY;
3828 	}
3829 	f_list_entry.fltr_info = f_info;
3830 
3831 	if (set)
3832 		status = ice_add_rule_internal(hw, ICE_SW_LKUP_DFLT,
3833 					       &f_list_entry);
3834 	else
3835 		status = ice_remove_rule_internal(hw, ICE_SW_LKUP_DFLT,
3836 						  &f_list_entry);
3837 
3838 	return status;
3839 }
3840 
3841 /**
3842  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
3843  * @fm_entry: filter entry to inspect
3844  * @vsi_handle: VSI handle to compare with filter info
3845  */
3846 static bool
3847 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
3848 {
3849 	return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
3850 		 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
3851 		(fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
3852 		 fm_entry->vsi_list_info &&
3853 		 (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
3854 }
3855 
3856 /**
3857  * ice_check_if_dflt_vsi - check if VSI is default VSI
3858  * @pi: pointer to the port_info structure
3859  * @vsi_handle: vsi handle to check for in filter list
3860  * @rule_exists: indicates if there are any VSI's in the rule list
3861  *
3862  * checks if the VSI is in a default VSI list, and also indicates
3863  * if the default VSI list is empty
3864  */
3865 bool
3866 ice_check_if_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle,
3867 		      bool *rule_exists)
3868 {
3869 	struct ice_fltr_mgmt_list_entry *fm_entry;
3870 	struct ice_sw_recipe *recp_list;
3871 	struct list_head *rule_head;
3872 	struct mutex *rule_lock; /* Lock to protect filter rule list */
3873 	bool ret = false;
3874 
3875 	recp_list = &pi->hw->switch_info->recp_list[ICE_SW_LKUP_DFLT];
3876 	rule_lock = &recp_list->filt_rule_lock;
3877 	rule_head = &recp_list->filt_rules;
3878 
3879 	mutex_lock(rule_lock);
3880 
3881 	if (rule_exists && !list_empty(rule_head))
3882 		*rule_exists = true;
3883 
3884 	list_for_each_entry(fm_entry, rule_head, list_entry) {
3885 		if (ice_vsi_uses_fltr(fm_entry, vsi_handle)) {
3886 			ret = true;
3887 			break;
3888 		}
3889 	}
3890 
3891 	mutex_unlock(rule_lock);
3892 
3893 	return ret;
3894 }
3895 
3896 /**
3897  * ice_remove_mac - remove a MAC address based filter rule
3898  * @hw: pointer to the hardware structure
3899  * @m_list: list of MAC addresses and forwarding information
3900  *
3901  * This function removes either a MAC filter rule or a specific VSI from a
3902  * VSI list for a multicast MAC address.
3903  *
3904  * Returns -ENOENT if a given entry was not added by ice_add_mac. Caller should
3905  * be aware that this call will only work if all the entries passed into m_list
3906  * were added previously. It will not attempt to do a partial remove of entries
3907  * that were found.
3908  */
3909 int ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
3910 {
3911 	struct ice_fltr_list_entry *list_itr, *tmp;
3912 
3913 	if (!m_list)
3914 		return -EINVAL;
3915 
3916 	list_for_each_entry_safe(list_itr, tmp, m_list, list_entry) {
3917 		enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
3918 		u16 vsi_handle;
3919 
3920 		if (l_type != ICE_SW_LKUP_MAC)
3921 			return -EINVAL;
3922 
3923 		vsi_handle = list_itr->fltr_info.vsi_handle;
3924 		if (!ice_is_vsi_valid(hw, vsi_handle))
3925 			return -EINVAL;
3926 
3927 		list_itr->fltr_info.fwd_id.hw_vsi_id =
3928 					ice_get_hw_vsi_num(hw, vsi_handle);
3929 
3930 		list_itr->status = ice_remove_rule_internal(hw,
3931 							    ICE_SW_LKUP_MAC,
3932 							    list_itr);
3933 		if (list_itr->status)
3934 			return list_itr->status;
3935 	}
3936 	return 0;
3937 }
3938 
3939 /**
3940  * ice_remove_vlan - Remove VLAN based filter rule
3941  * @hw: pointer to the hardware structure
3942  * @v_list: list of VLAN entries and forwarding information
3943  */
3944 int ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
3945 {
3946 	struct ice_fltr_list_entry *v_list_itr, *tmp;
3947 
3948 	if (!v_list || !hw)
3949 		return -EINVAL;
3950 
3951 	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
3952 		enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
3953 
3954 		if (l_type != ICE_SW_LKUP_VLAN)
3955 			return -EINVAL;
3956 		v_list_itr->status = ice_remove_rule_internal(hw,
3957 							      ICE_SW_LKUP_VLAN,
3958 							      v_list_itr);
3959 		if (v_list_itr->status)
3960 			return v_list_itr->status;
3961 	}
3962 	return 0;
3963 }
3964 
3965 /**
3966  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
3967  * @hw: pointer to the hardware structure
3968  * @vsi_handle: VSI handle to remove filters from
3969  * @vsi_list_head: pointer to the list to add entry to
3970  * @fi: pointer to fltr_info of filter entry to copy & add
3971  *
3972  * Helper function, used when creating a list of filters to remove from
3973  * a specific VSI. The entry added to vsi_list_head is a COPY of the
3974  * original filter entry, with the exception of fltr_info.fltr_act and
3975  * fltr_info.fwd_id fields. These are set such that later logic can
3976  * extract which VSI to remove the fltr from, and pass on that information.
3977  */
3978 static int
3979 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3980 			       struct list_head *vsi_list_head,
3981 			       struct ice_fltr_info *fi)
3982 {
3983 	struct ice_fltr_list_entry *tmp;
3984 
3985 	/* this memory is freed up in the caller function
3986 	 * once filters for this VSI are removed
3987 	 */
3988 	tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
3989 	if (!tmp)
3990 		return -ENOMEM;
3991 
3992 	tmp->fltr_info = *fi;
3993 
3994 	/* Overwrite these fields to indicate which VSI to remove filter from,
3995 	 * so find and remove logic can extract the information from the
3996 	 * list entries. Note that original entries will still have proper
3997 	 * values.
3998 	 */
3999 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
4000 	tmp->fltr_info.vsi_handle = vsi_handle;
4001 	tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
4002 
4003 	list_add(&tmp->list_entry, vsi_list_head);
4004 
4005 	return 0;
4006 }
4007 
4008 /**
4009  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
4010  * @hw: pointer to the hardware structure
4011  * @vsi_handle: VSI handle to remove filters from
4012  * @lkup_list_head: pointer to the list that has certain lookup type filters
4013  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
4014  *
4015  * Locates all filters in lkup_list_head that are used by the given VSI,
4016  * and adds COPIES of those entries to vsi_list_head (intended to be used
4017  * to remove the listed filters).
4018  * Note that this means all entries in vsi_list_head must be explicitly
4019  * deallocated by the caller when done with list.
4020  */
4021 static int
4022 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
4023 			 struct list_head *lkup_list_head,
4024 			 struct list_head *vsi_list_head)
4025 {
4026 	struct ice_fltr_mgmt_list_entry *fm_entry;
4027 	int status = 0;
4028 
4029 	/* check to make sure VSI ID is valid and within boundary */
4030 	if (!ice_is_vsi_valid(hw, vsi_handle))
4031 		return -EINVAL;
4032 
4033 	list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
4034 		if (!ice_vsi_uses_fltr(fm_entry, vsi_handle))
4035 			continue;
4036 
4037 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
4038 							vsi_list_head,
4039 							&fm_entry->fltr_info);
4040 		if (status)
4041 			return status;
4042 	}
4043 	return status;
4044 }
4045 
4046 /**
4047  * ice_determine_promisc_mask
4048  * @fi: filter info to parse
4049  *
4050  * Helper function to determine which ICE_PROMISC_ mask corresponds
4051  * to given filter into.
4052  */
4053 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
4054 {
4055 	u16 vid = fi->l_data.mac_vlan.vlan_id;
4056 	u8 *macaddr = fi->l_data.mac.mac_addr;
4057 	bool is_tx_fltr = false;
4058 	u8 promisc_mask = 0;
4059 
4060 	if (fi->flag == ICE_FLTR_TX)
4061 		is_tx_fltr = true;
4062 
4063 	if (is_broadcast_ether_addr(macaddr))
4064 		promisc_mask |= is_tx_fltr ?
4065 			ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
4066 	else if (is_multicast_ether_addr(macaddr))
4067 		promisc_mask |= is_tx_fltr ?
4068 			ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
4069 	else if (is_unicast_ether_addr(macaddr))
4070 		promisc_mask |= is_tx_fltr ?
4071 			ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
4072 	if (vid)
4073 		promisc_mask |= is_tx_fltr ?
4074 			ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
4075 
4076 	return promisc_mask;
4077 }
4078 
4079 /**
4080  * ice_remove_promisc - Remove promisc based filter rules
4081  * @hw: pointer to the hardware structure
4082  * @recp_id: recipe ID for which the rule needs to removed
4083  * @v_list: list of promisc entries
4084  */
4085 static int
4086 ice_remove_promisc(struct ice_hw *hw, u8 recp_id, struct list_head *v_list)
4087 {
4088 	struct ice_fltr_list_entry *v_list_itr, *tmp;
4089 
4090 	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
4091 		v_list_itr->status =
4092 			ice_remove_rule_internal(hw, recp_id, v_list_itr);
4093 		if (v_list_itr->status)
4094 			return v_list_itr->status;
4095 	}
4096 	return 0;
4097 }
4098 
4099 /**
4100  * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
4101  * @hw: pointer to the hardware structure
4102  * @vsi_handle: VSI handle to clear mode
4103  * @promisc_mask: mask of promiscuous config bits to clear
4104  * @vid: VLAN ID to clear VLAN promiscuous
4105  */
4106 int
4107 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
4108 		      u16 vid)
4109 {
4110 	struct ice_switch_info *sw = hw->switch_info;
4111 	struct ice_fltr_list_entry *fm_entry, *tmp;
4112 	struct list_head remove_list_head;
4113 	struct ice_fltr_mgmt_list_entry *itr;
4114 	struct list_head *rule_head;
4115 	struct mutex *rule_lock;	/* Lock to protect filter rule list */
4116 	int status = 0;
4117 	u8 recipe_id;
4118 
4119 	if (!ice_is_vsi_valid(hw, vsi_handle))
4120 		return -EINVAL;
4121 
4122 	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX))
4123 		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
4124 	else
4125 		recipe_id = ICE_SW_LKUP_PROMISC;
4126 
4127 	rule_head = &sw->recp_list[recipe_id].filt_rules;
4128 	rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
4129 
4130 	INIT_LIST_HEAD(&remove_list_head);
4131 
4132 	mutex_lock(rule_lock);
4133 	list_for_each_entry(itr, rule_head, list_entry) {
4134 		struct ice_fltr_info *fltr_info;
4135 		u8 fltr_promisc_mask = 0;
4136 
4137 		if (!ice_vsi_uses_fltr(itr, vsi_handle))
4138 			continue;
4139 		fltr_info = &itr->fltr_info;
4140 
4141 		if (recipe_id == ICE_SW_LKUP_PROMISC_VLAN &&
4142 		    vid != fltr_info->l_data.mac_vlan.vlan_id)
4143 			continue;
4144 
4145 		fltr_promisc_mask |= ice_determine_promisc_mask(fltr_info);
4146 
4147 		/* Skip if filter is not completely specified by given mask */
4148 		if (fltr_promisc_mask & ~promisc_mask)
4149 			continue;
4150 
4151 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
4152 							&remove_list_head,
4153 							fltr_info);
4154 		if (status) {
4155 			mutex_unlock(rule_lock);
4156 			goto free_fltr_list;
4157 		}
4158 	}
4159 	mutex_unlock(rule_lock);
4160 
4161 	status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
4162 
4163 free_fltr_list:
4164 	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
4165 		list_del(&fm_entry->list_entry);
4166 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
4167 	}
4168 
4169 	return status;
4170 }
4171 
4172 /**
4173  * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
4174  * @hw: pointer to the hardware structure
4175  * @vsi_handle: VSI handle to configure
4176  * @promisc_mask: mask of promiscuous config bits
4177  * @vid: VLAN ID to set VLAN promiscuous
4178  */
4179 int
4180 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
4181 {
4182 	enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
4183 	struct ice_fltr_list_entry f_list_entry;
4184 	struct ice_fltr_info new_fltr;
4185 	bool is_tx_fltr;
4186 	int status = 0;
4187 	u16 hw_vsi_id;
4188 	int pkt_type;
4189 	u8 recipe_id;
4190 
4191 	if (!ice_is_vsi_valid(hw, vsi_handle))
4192 		return -EINVAL;
4193 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
4194 
4195 	memset(&new_fltr, 0, sizeof(new_fltr));
4196 
4197 	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
4198 		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
4199 		new_fltr.l_data.mac_vlan.vlan_id = vid;
4200 		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
4201 	} else {
4202 		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
4203 		recipe_id = ICE_SW_LKUP_PROMISC;
4204 	}
4205 
4206 	/* Separate filters must be set for each direction/packet type
4207 	 * combination, so we will loop over the mask value, store the
4208 	 * individual type, and clear it out in the input mask as it
4209 	 * is found.
4210 	 */
4211 	while (promisc_mask) {
4212 		u8 *mac_addr;
4213 
4214 		pkt_type = 0;
4215 		is_tx_fltr = false;
4216 
4217 		if (promisc_mask & ICE_PROMISC_UCAST_RX) {
4218 			promisc_mask &= ~ICE_PROMISC_UCAST_RX;
4219 			pkt_type = UCAST_FLTR;
4220 		} else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
4221 			promisc_mask &= ~ICE_PROMISC_UCAST_TX;
4222 			pkt_type = UCAST_FLTR;
4223 			is_tx_fltr = true;
4224 		} else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
4225 			promisc_mask &= ~ICE_PROMISC_MCAST_RX;
4226 			pkt_type = MCAST_FLTR;
4227 		} else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
4228 			promisc_mask &= ~ICE_PROMISC_MCAST_TX;
4229 			pkt_type = MCAST_FLTR;
4230 			is_tx_fltr = true;
4231 		} else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
4232 			promisc_mask &= ~ICE_PROMISC_BCAST_RX;
4233 			pkt_type = BCAST_FLTR;
4234 		} else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
4235 			promisc_mask &= ~ICE_PROMISC_BCAST_TX;
4236 			pkt_type = BCAST_FLTR;
4237 			is_tx_fltr = true;
4238 		}
4239 
4240 		/* Check for VLAN promiscuous flag */
4241 		if (promisc_mask & ICE_PROMISC_VLAN_RX) {
4242 			promisc_mask &= ~ICE_PROMISC_VLAN_RX;
4243 		} else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
4244 			promisc_mask &= ~ICE_PROMISC_VLAN_TX;
4245 			is_tx_fltr = true;
4246 		}
4247 
4248 		/* Set filter DA based on packet type */
4249 		mac_addr = new_fltr.l_data.mac.mac_addr;
4250 		if (pkt_type == BCAST_FLTR) {
4251 			eth_broadcast_addr(mac_addr);
4252 		} else if (pkt_type == MCAST_FLTR ||
4253 			   pkt_type == UCAST_FLTR) {
4254 			/* Use the dummy ether header DA */
4255 			ether_addr_copy(mac_addr, dummy_eth_header);
4256 			if (pkt_type == MCAST_FLTR)
4257 				mac_addr[0] |= 0x1;	/* Set multicast bit */
4258 		}
4259 
4260 		/* Need to reset this to zero for all iterations */
4261 		new_fltr.flag = 0;
4262 		if (is_tx_fltr) {
4263 			new_fltr.flag |= ICE_FLTR_TX;
4264 			new_fltr.src = hw_vsi_id;
4265 		} else {
4266 			new_fltr.flag |= ICE_FLTR_RX;
4267 			new_fltr.src = hw->port_info->lport;
4268 		}
4269 
4270 		new_fltr.fltr_act = ICE_FWD_TO_VSI;
4271 		new_fltr.vsi_handle = vsi_handle;
4272 		new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
4273 		f_list_entry.fltr_info = new_fltr;
4274 
4275 		status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
4276 		if (status)
4277 			goto set_promisc_exit;
4278 	}
4279 
4280 set_promisc_exit:
4281 	return status;
4282 }
4283 
4284 /**
4285  * ice_set_vlan_vsi_promisc
4286  * @hw: pointer to the hardware structure
4287  * @vsi_handle: VSI handle to configure
4288  * @promisc_mask: mask of promiscuous config bits
4289  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
4290  *
4291  * Configure VSI with all associated VLANs to given promiscuous mode(s)
4292  */
4293 int
4294 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
4295 			 bool rm_vlan_promisc)
4296 {
4297 	struct ice_switch_info *sw = hw->switch_info;
4298 	struct ice_fltr_list_entry *list_itr, *tmp;
4299 	struct list_head vsi_list_head;
4300 	struct list_head *vlan_head;
4301 	struct mutex *vlan_lock; /* Lock to protect filter rule list */
4302 	u16 vlan_id;
4303 	int status;
4304 
4305 	INIT_LIST_HEAD(&vsi_list_head);
4306 	vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
4307 	vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
4308 	mutex_lock(vlan_lock);
4309 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
4310 					  &vsi_list_head);
4311 	mutex_unlock(vlan_lock);
4312 	if (status)
4313 		goto free_fltr_list;
4314 
4315 	list_for_each_entry(list_itr, &vsi_list_head, list_entry) {
4316 		/* Avoid enabling or disabling VLAN zero twice when in double
4317 		 * VLAN mode
4318 		 */
4319 		if (ice_is_dvm_ena(hw) &&
4320 		    list_itr->fltr_info.l_data.vlan.tpid == 0)
4321 			continue;
4322 
4323 		vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
4324 		if (rm_vlan_promisc)
4325 			status = ice_clear_vsi_promisc(hw, vsi_handle,
4326 						       promisc_mask, vlan_id);
4327 		else
4328 			status = ice_set_vsi_promisc(hw, vsi_handle,
4329 						     promisc_mask, vlan_id);
4330 		if (status && status != -EEXIST)
4331 			break;
4332 	}
4333 
4334 free_fltr_list:
4335 	list_for_each_entry_safe(list_itr, tmp, &vsi_list_head, list_entry) {
4336 		list_del(&list_itr->list_entry);
4337 		devm_kfree(ice_hw_to_dev(hw), list_itr);
4338 	}
4339 	return status;
4340 }
4341 
4342 /**
4343  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
4344  * @hw: pointer to the hardware structure
4345  * @vsi_handle: VSI handle to remove filters from
4346  * @lkup: switch rule filter lookup type
4347  */
4348 static void
4349 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
4350 			 enum ice_sw_lkup_type lkup)
4351 {
4352 	struct ice_switch_info *sw = hw->switch_info;
4353 	struct ice_fltr_list_entry *fm_entry;
4354 	struct list_head remove_list_head;
4355 	struct list_head *rule_head;
4356 	struct ice_fltr_list_entry *tmp;
4357 	struct mutex *rule_lock;	/* Lock to protect filter rule list */
4358 	int status;
4359 
4360 	INIT_LIST_HEAD(&remove_list_head);
4361 	rule_lock = &sw->recp_list[lkup].filt_rule_lock;
4362 	rule_head = &sw->recp_list[lkup].filt_rules;
4363 	mutex_lock(rule_lock);
4364 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
4365 					  &remove_list_head);
4366 	mutex_unlock(rule_lock);
4367 	if (status)
4368 		goto free_fltr_list;
4369 
4370 	switch (lkup) {
4371 	case ICE_SW_LKUP_MAC:
4372 		ice_remove_mac(hw, &remove_list_head);
4373 		break;
4374 	case ICE_SW_LKUP_VLAN:
4375 		ice_remove_vlan(hw, &remove_list_head);
4376 		break;
4377 	case ICE_SW_LKUP_PROMISC:
4378 	case ICE_SW_LKUP_PROMISC_VLAN:
4379 		ice_remove_promisc(hw, lkup, &remove_list_head);
4380 		break;
4381 	case ICE_SW_LKUP_MAC_VLAN:
4382 	case ICE_SW_LKUP_ETHERTYPE:
4383 	case ICE_SW_LKUP_ETHERTYPE_MAC:
4384 	case ICE_SW_LKUP_DFLT:
4385 	case ICE_SW_LKUP_LAST:
4386 	default:
4387 		ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
4388 		break;
4389 	}
4390 
4391 free_fltr_list:
4392 	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
4393 		list_del(&fm_entry->list_entry);
4394 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
4395 	}
4396 }
4397 
4398 /**
4399  * ice_remove_vsi_fltr - Remove all filters for a VSI
4400  * @hw: pointer to the hardware structure
4401  * @vsi_handle: VSI handle to remove filters from
4402  */
4403 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
4404 {
4405 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
4406 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
4407 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
4408 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
4409 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
4410 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
4411 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
4412 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
4413 }
4414 
4415 /**
4416  * ice_alloc_res_cntr - allocating resource counter
4417  * @hw: pointer to the hardware structure
4418  * @type: type of resource
4419  * @alloc_shared: if set it is shared else dedicated
4420  * @num_items: number of entries requested for FD resource type
4421  * @counter_id: counter index returned by AQ call
4422  */
4423 int
4424 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
4425 		   u16 *counter_id)
4426 {
4427 	DEFINE_RAW_FLEX(struct ice_aqc_alloc_free_res_elem, buf, elem, 1);
4428 	u16 buf_len = __struct_size(buf);
4429 	int status;
4430 
4431 	buf->num_elems = cpu_to_le16(num_items);
4432 	buf->res_type = cpu_to_le16(FIELD_PREP(ICE_AQC_RES_TYPE_M, type) |
4433 				    alloc_shared);
4434 
4435 	status = ice_aq_alloc_free_res(hw, buf, buf_len, ice_aqc_opc_alloc_res);
4436 	if (status)
4437 		return status;
4438 
4439 	*counter_id = le16_to_cpu(buf->elem[0].e.sw_resp);
4440 	return status;
4441 }
4442 
4443 /**
4444  * ice_free_res_cntr - free resource counter
4445  * @hw: pointer to the hardware structure
4446  * @type: type of resource
4447  * @alloc_shared: if set it is shared else dedicated
4448  * @num_items: number of entries to be freed for FD resource type
4449  * @counter_id: counter ID resource which needs to be freed
4450  */
4451 int
4452 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
4453 		  u16 counter_id)
4454 {
4455 	DEFINE_RAW_FLEX(struct ice_aqc_alloc_free_res_elem, buf, elem, 1);
4456 	u16 buf_len = __struct_size(buf);
4457 	int status;
4458 
4459 	buf->num_elems = cpu_to_le16(num_items);
4460 	buf->res_type = cpu_to_le16(FIELD_PREP(ICE_AQC_RES_TYPE_M, type) |
4461 				    alloc_shared);
4462 	buf->elem[0].e.sw_resp = cpu_to_le16(counter_id);
4463 
4464 	status = ice_aq_alloc_free_res(hw, buf, buf_len, ice_aqc_opc_free_res);
4465 	if (status)
4466 		ice_debug(hw, ICE_DBG_SW, "counter resource could not be freed\n");
4467 
4468 	return status;
4469 }
4470 
4471 #define ICE_PROTOCOL_ENTRY(id, ...) {		\
4472 	.prot_type	= id,			\
4473 	.offs		= {__VA_ARGS__},	\
4474 }
4475 
4476 /**
4477  * ice_share_res - set a resource as shared or dedicated
4478  * @hw: hw struct of original owner of resource
4479  * @type: resource type
4480  * @shared: is the resource being set to shared
4481  * @res_id: resource id (descriptor)
4482  */
4483 int ice_share_res(struct ice_hw *hw, u16 type, u8 shared, u16 res_id)
4484 {
4485 	DEFINE_RAW_FLEX(struct ice_aqc_alloc_free_res_elem, buf, elem, 1);
4486 	u16 buf_len = __struct_size(buf);
4487 	u16 res_type;
4488 	int status;
4489 
4490 	buf->num_elems = cpu_to_le16(1);
4491 	res_type = FIELD_PREP(ICE_AQC_RES_TYPE_M, type);
4492 	if (shared)
4493 		res_type |= ICE_AQC_RES_TYPE_FLAG_SHARED;
4494 
4495 	buf->res_type = cpu_to_le16(res_type);
4496 	buf->elem[0].e.sw_resp = cpu_to_le16(res_id);
4497 	status = ice_aq_alloc_free_res(hw, buf, buf_len,
4498 				       ice_aqc_opc_share_res);
4499 	if (status)
4500 		ice_debug(hw, ICE_DBG_SW, "Could not set resource type %u id %u to %s\n",
4501 			  type, res_id, shared ? "SHARED" : "DEDICATED");
4502 
4503 	return status;
4504 }
4505 
4506 /* This is mapping table entry that maps every word within a given protocol
4507  * structure to the real byte offset as per the specification of that
4508  * protocol header.
4509  * for example dst address is 3 words in ethertype header and corresponding
4510  * bytes are 0, 2, 3 in the actual packet header and src address is at 4, 6, 8
4511  * IMPORTANT: Every structure part of "ice_prot_hdr" union should have a
4512  * matching entry describing its field. This needs to be updated if new
4513  * structure is added to that union.
4514  */
4515 static const struct ice_prot_ext_tbl_entry ice_prot_ext[ICE_PROTOCOL_LAST] = {
4516 	ICE_PROTOCOL_ENTRY(ICE_MAC_OFOS, 0, 2, 4, 6, 8, 10, 12),
4517 	ICE_PROTOCOL_ENTRY(ICE_MAC_IL, 0, 2, 4, 6, 8, 10, 12),
4518 	ICE_PROTOCOL_ENTRY(ICE_ETYPE_OL, 0),
4519 	ICE_PROTOCOL_ENTRY(ICE_ETYPE_IL, 0),
4520 	ICE_PROTOCOL_ENTRY(ICE_VLAN_OFOS, 2, 0),
4521 	ICE_PROTOCOL_ENTRY(ICE_IPV4_OFOS, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18),
4522 	ICE_PROTOCOL_ENTRY(ICE_IPV4_IL,	0, 2, 4, 6, 8, 10, 12, 14, 16, 18),
4523 	ICE_PROTOCOL_ENTRY(ICE_IPV6_OFOS, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18,
4524 			   20, 22, 24, 26, 28, 30, 32, 34, 36, 38),
4525 	ICE_PROTOCOL_ENTRY(ICE_IPV6_IL, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
4526 			   22, 24, 26, 28, 30, 32, 34, 36, 38),
4527 	ICE_PROTOCOL_ENTRY(ICE_TCP_IL, 0, 2),
4528 	ICE_PROTOCOL_ENTRY(ICE_UDP_OF, 0, 2),
4529 	ICE_PROTOCOL_ENTRY(ICE_UDP_ILOS, 0, 2),
4530 	ICE_PROTOCOL_ENTRY(ICE_VXLAN, 8, 10, 12, 14),
4531 	ICE_PROTOCOL_ENTRY(ICE_GENEVE, 8, 10, 12, 14),
4532 	ICE_PROTOCOL_ENTRY(ICE_NVGRE, 0, 2, 4, 6),
4533 	ICE_PROTOCOL_ENTRY(ICE_GTP, 8, 10, 12, 14, 16, 18, 20, 22),
4534 	ICE_PROTOCOL_ENTRY(ICE_GTP_NO_PAY, 8, 10, 12, 14),
4535 	ICE_PROTOCOL_ENTRY(ICE_PPPOE, 0, 2, 4, 6),
4536 	ICE_PROTOCOL_ENTRY(ICE_L2TPV3, 0, 2, 4, 6, 8, 10),
4537 	ICE_PROTOCOL_ENTRY(ICE_VLAN_EX, 2, 0),
4538 	ICE_PROTOCOL_ENTRY(ICE_VLAN_IN, 2, 0),
4539 	ICE_PROTOCOL_ENTRY(ICE_HW_METADATA,
4540 			   ICE_SOURCE_PORT_MDID_OFFSET,
4541 			   ICE_PTYPE_MDID_OFFSET,
4542 			   ICE_PACKET_LENGTH_MDID_OFFSET,
4543 			   ICE_SOURCE_VSI_MDID_OFFSET,
4544 			   ICE_PKT_VLAN_MDID_OFFSET,
4545 			   ICE_PKT_TUNNEL_MDID_OFFSET,
4546 			   ICE_PKT_TCP_MDID_OFFSET,
4547 			   ICE_PKT_ERROR_MDID_OFFSET),
4548 };
4549 
4550 static struct ice_protocol_entry ice_prot_id_tbl[ICE_PROTOCOL_LAST] = {
4551 	{ ICE_MAC_OFOS,		ICE_MAC_OFOS_HW },
4552 	{ ICE_MAC_IL,		ICE_MAC_IL_HW },
4553 	{ ICE_ETYPE_OL,		ICE_ETYPE_OL_HW },
4554 	{ ICE_ETYPE_IL,		ICE_ETYPE_IL_HW },
4555 	{ ICE_VLAN_OFOS,	ICE_VLAN_OL_HW },
4556 	{ ICE_IPV4_OFOS,	ICE_IPV4_OFOS_HW },
4557 	{ ICE_IPV4_IL,		ICE_IPV4_IL_HW },
4558 	{ ICE_IPV6_OFOS,	ICE_IPV6_OFOS_HW },
4559 	{ ICE_IPV6_IL,		ICE_IPV6_IL_HW },
4560 	{ ICE_TCP_IL,		ICE_TCP_IL_HW },
4561 	{ ICE_UDP_OF,		ICE_UDP_OF_HW },
4562 	{ ICE_UDP_ILOS,		ICE_UDP_ILOS_HW },
4563 	{ ICE_VXLAN,		ICE_UDP_OF_HW },
4564 	{ ICE_GENEVE,		ICE_UDP_OF_HW },
4565 	{ ICE_NVGRE,		ICE_GRE_OF_HW },
4566 	{ ICE_GTP,		ICE_UDP_OF_HW },
4567 	{ ICE_GTP_NO_PAY,	ICE_UDP_ILOS_HW },
4568 	{ ICE_PPPOE,		ICE_PPPOE_HW },
4569 	{ ICE_L2TPV3,		ICE_L2TPV3_HW },
4570 	{ ICE_VLAN_EX,          ICE_VLAN_OF_HW },
4571 	{ ICE_VLAN_IN,          ICE_VLAN_OL_HW },
4572 	{ ICE_HW_METADATA,      ICE_META_DATA_ID_HW },
4573 };
4574 
4575 /**
4576  * ice_find_recp - find a recipe
4577  * @hw: pointer to the hardware structure
4578  * @lkup_exts: extension sequence to match
4579  * @rinfo: information regarding the rule e.g. priority and action info
4580  *
4581  * Returns index of matching recipe, or ICE_MAX_NUM_RECIPES if not found.
4582  */
4583 static u16
4584 ice_find_recp(struct ice_hw *hw, struct ice_prot_lkup_ext *lkup_exts,
4585 	      const struct ice_adv_rule_info *rinfo)
4586 {
4587 	bool refresh_required = true;
4588 	struct ice_sw_recipe *recp;
4589 	u8 i;
4590 
4591 	/* Walk through existing recipes to find a match */
4592 	recp = hw->switch_info->recp_list;
4593 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4594 		/* If recipe was not created for this ID, in SW bookkeeping,
4595 		 * check if FW has an entry for this recipe. If the FW has an
4596 		 * entry update it in our SW bookkeeping and continue with the
4597 		 * matching.
4598 		 */
4599 		if (!recp[i].recp_created)
4600 			if (ice_get_recp_frm_fw(hw,
4601 						hw->switch_info->recp_list, i,
4602 						&refresh_required))
4603 				continue;
4604 
4605 		/* Skip inverse action recipes */
4606 		if (recp[i].root_buf && recp[i].root_buf->content.act_ctrl &
4607 		    ICE_AQ_RECIPE_ACT_INV_ACT)
4608 			continue;
4609 
4610 		/* if number of words we are looking for match */
4611 		if (lkup_exts->n_val_words == recp[i].lkup_exts.n_val_words) {
4612 			struct ice_fv_word *ar = recp[i].lkup_exts.fv_words;
4613 			struct ice_fv_word *be = lkup_exts->fv_words;
4614 			u16 *cr = recp[i].lkup_exts.field_mask;
4615 			u16 *de = lkup_exts->field_mask;
4616 			bool found = true;
4617 			u8 pe, qr;
4618 
4619 			/* ar, cr, and qr are related to the recipe words, while
4620 			 * be, de, and pe are related to the lookup words
4621 			 */
4622 			for (pe = 0; pe < lkup_exts->n_val_words; pe++) {
4623 				for (qr = 0; qr < recp[i].lkup_exts.n_val_words;
4624 				     qr++) {
4625 					if (ar[qr].off == be[pe].off &&
4626 					    ar[qr].prot_id == be[pe].prot_id &&
4627 					    cr[qr] == de[pe])
4628 						/* Found the "pe"th word in the
4629 						 * given recipe
4630 						 */
4631 						break;
4632 				}
4633 				/* After walking through all the words in the
4634 				 * "i"th recipe if "p"th word was not found then
4635 				 * this recipe is not what we are looking for.
4636 				 * So break out from this loop and try the next
4637 				 * recipe
4638 				 */
4639 				if (qr >= recp[i].lkup_exts.n_val_words) {
4640 					found = false;
4641 					break;
4642 				}
4643 			}
4644 			/* If for "i"th recipe the found was never set to false
4645 			 * then it means we found our match
4646 			 * Also tun type and *_pass_l2 of recipe needs to be
4647 			 * checked
4648 			 */
4649 			if (found && recp[i].tun_type == rinfo->tun_type &&
4650 			    recp[i].need_pass_l2 == rinfo->need_pass_l2 &&
4651 			    recp[i].allow_pass_l2 == rinfo->allow_pass_l2)
4652 				return i; /* Return the recipe ID */
4653 		}
4654 	}
4655 	return ICE_MAX_NUM_RECIPES;
4656 }
4657 
4658 /**
4659  * ice_change_proto_id_to_dvm - change proto id in prot_id_tbl
4660  *
4661  * As protocol id for outer vlan is different in dvm and svm, if dvm is
4662  * supported protocol array record for outer vlan has to be modified to
4663  * reflect the value proper for DVM.
4664  */
4665 void ice_change_proto_id_to_dvm(void)
4666 {
4667 	u8 i;
4668 
4669 	for (i = 0; i < ARRAY_SIZE(ice_prot_id_tbl); i++)
4670 		if (ice_prot_id_tbl[i].type == ICE_VLAN_OFOS &&
4671 		    ice_prot_id_tbl[i].protocol_id != ICE_VLAN_OF_HW)
4672 			ice_prot_id_tbl[i].protocol_id = ICE_VLAN_OF_HW;
4673 }
4674 
4675 /**
4676  * ice_prot_type_to_id - get protocol ID from protocol type
4677  * @type: protocol type
4678  * @id: pointer to variable that will receive the ID
4679  *
4680  * Returns true if found, false otherwise
4681  */
4682 static bool ice_prot_type_to_id(enum ice_protocol_type type, u8 *id)
4683 {
4684 	u8 i;
4685 
4686 	for (i = 0; i < ARRAY_SIZE(ice_prot_id_tbl); i++)
4687 		if (ice_prot_id_tbl[i].type == type) {
4688 			*id = ice_prot_id_tbl[i].protocol_id;
4689 			return true;
4690 		}
4691 	return false;
4692 }
4693 
4694 /**
4695  * ice_fill_valid_words - count valid words
4696  * @rule: advanced rule with lookup information
4697  * @lkup_exts: byte offset extractions of the words that are valid
4698  *
4699  * calculate valid words in a lookup rule using mask value
4700  */
4701 static u8
4702 ice_fill_valid_words(struct ice_adv_lkup_elem *rule,
4703 		     struct ice_prot_lkup_ext *lkup_exts)
4704 {
4705 	u8 j, word, prot_id, ret_val;
4706 
4707 	if (!ice_prot_type_to_id(rule->type, &prot_id))
4708 		return 0;
4709 
4710 	word = lkup_exts->n_val_words;
4711 
4712 	for (j = 0; j < sizeof(rule->m_u) / sizeof(u16); j++)
4713 		if (((u16 *)&rule->m_u)[j] &&
4714 		    rule->type < ARRAY_SIZE(ice_prot_ext)) {
4715 			/* No more space to accommodate */
4716 			if (word >= ICE_MAX_CHAIN_WORDS)
4717 				return 0;
4718 			lkup_exts->fv_words[word].off =
4719 				ice_prot_ext[rule->type].offs[j];
4720 			lkup_exts->fv_words[word].prot_id =
4721 				ice_prot_id_tbl[rule->type].protocol_id;
4722 			lkup_exts->field_mask[word] =
4723 				be16_to_cpu(((__force __be16 *)&rule->m_u)[j]);
4724 			word++;
4725 		}
4726 
4727 	ret_val = word - lkup_exts->n_val_words;
4728 	lkup_exts->n_val_words = word;
4729 
4730 	return ret_val;
4731 }
4732 
4733 /**
4734  * ice_create_first_fit_recp_def - Create a recipe grouping
4735  * @hw: pointer to the hardware structure
4736  * @lkup_exts: an array of protocol header extractions
4737  * @rg_list: pointer to a list that stores new recipe groups
4738  * @recp_cnt: pointer to a variable that stores returned number of recipe groups
4739  *
4740  * Using first fit algorithm, take all the words that are still not done
4741  * and start grouping them in 4-word groups. Each group makes up one
4742  * recipe.
4743  */
4744 static int
4745 ice_create_first_fit_recp_def(struct ice_hw *hw,
4746 			      struct ice_prot_lkup_ext *lkup_exts,
4747 			      struct list_head *rg_list,
4748 			      u8 *recp_cnt)
4749 {
4750 	struct ice_pref_recipe_group *grp = NULL;
4751 	u8 j;
4752 
4753 	*recp_cnt = 0;
4754 
4755 	/* Walk through every word in the rule to check if it is not done. If so
4756 	 * then this word needs to be part of a new recipe.
4757 	 */
4758 	for (j = 0; j < lkup_exts->n_val_words; j++)
4759 		if (!test_bit(j, lkup_exts->done)) {
4760 			if (!grp ||
4761 			    grp->n_val_pairs == ICE_NUM_WORDS_RECIPE) {
4762 				struct ice_recp_grp_entry *entry;
4763 
4764 				entry = devm_kzalloc(ice_hw_to_dev(hw),
4765 						     sizeof(*entry),
4766 						     GFP_KERNEL);
4767 				if (!entry)
4768 					return -ENOMEM;
4769 				list_add(&entry->l_entry, rg_list);
4770 				grp = &entry->r_group;
4771 				(*recp_cnt)++;
4772 			}
4773 
4774 			grp->pairs[grp->n_val_pairs].prot_id =
4775 				lkup_exts->fv_words[j].prot_id;
4776 			grp->pairs[grp->n_val_pairs].off =
4777 				lkup_exts->fv_words[j].off;
4778 			grp->mask[grp->n_val_pairs] = lkup_exts->field_mask[j];
4779 			grp->n_val_pairs++;
4780 		}
4781 
4782 	return 0;
4783 }
4784 
4785 /**
4786  * ice_fill_fv_word_index - fill in the field vector indices for a recipe group
4787  * @hw: pointer to the hardware structure
4788  * @fv_list: field vector with the extraction sequence information
4789  * @rg_list: recipe groupings with protocol-offset pairs
4790  *
4791  * Helper function to fill in the field vector indices for protocol-offset
4792  * pairs. These indexes are then ultimately programmed into a recipe.
4793  */
4794 static int
4795 ice_fill_fv_word_index(struct ice_hw *hw, struct list_head *fv_list,
4796 		       struct list_head *rg_list)
4797 {
4798 	struct ice_sw_fv_list_entry *fv;
4799 	struct ice_recp_grp_entry *rg;
4800 	struct ice_fv_word *fv_ext;
4801 
4802 	if (list_empty(fv_list))
4803 		return 0;
4804 
4805 	fv = list_first_entry(fv_list, struct ice_sw_fv_list_entry,
4806 			      list_entry);
4807 	fv_ext = fv->fv_ptr->ew;
4808 
4809 	list_for_each_entry(rg, rg_list, l_entry) {
4810 		u8 i;
4811 
4812 		for (i = 0; i < rg->r_group.n_val_pairs; i++) {
4813 			struct ice_fv_word *pr;
4814 			bool found = false;
4815 			u16 mask;
4816 			u8 j;
4817 
4818 			pr = &rg->r_group.pairs[i];
4819 			mask = rg->r_group.mask[i];
4820 
4821 			for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
4822 				if (fv_ext[j].prot_id == pr->prot_id &&
4823 				    fv_ext[j].off == pr->off) {
4824 					found = true;
4825 
4826 					/* Store index of field vector */
4827 					rg->fv_idx[i] = j;
4828 					rg->fv_mask[i] = mask;
4829 					break;
4830 				}
4831 
4832 			/* Protocol/offset could not be found, caller gave an
4833 			 * invalid pair
4834 			 */
4835 			if (!found)
4836 				return -EINVAL;
4837 		}
4838 	}
4839 
4840 	return 0;
4841 }
4842 
4843 /**
4844  * ice_find_free_recp_res_idx - find free result indexes for recipe
4845  * @hw: pointer to hardware structure
4846  * @profiles: bitmap of profiles that will be associated with the new recipe
4847  * @free_idx: pointer to variable to receive the free index bitmap
4848  *
4849  * The algorithm used here is:
4850  *	1. When creating a new recipe, create a set P which contains all
4851  *	   Profiles that will be associated with our new recipe
4852  *
4853  *	2. For each Profile p in set P:
4854  *	    a. Add all recipes associated with Profile p into set R
4855  *	    b. Optional : PossibleIndexes &= profile[p].possibleIndexes
4856  *		[initially PossibleIndexes should be 0xFFFFFFFFFFFFFFFF]
4857  *		i. Or just assume they all have the same possible indexes:
4858  *			44, 45, 46, 47
4859  *			i.e., PossibleIndexes = 0x0000F00000000000
4860  *
4861  *	3. For each Recipe r in set R:
4862  *	    a. UsedIndexes |= (bitwise or ) recipe[r].res_indexes
4863  *	    b. FreeIndexes = UsedIndexes ^ PossibleIndexes
4864  *
4865  *	FreeIndexes will contain the bits indicating the indexes free for use,
4866  *      then the code needs to update the recipe[r].used_result_idx_bits to
4867  *      indicate which indexes were selected for use by this recipe.
4868  */
4869 static u16
4870 ice_find_free_recp_res_idx(struct ice_hw *hw, const unsigned long *profiles,
4871 			   unsigned long *free_idx)
4872 {
4873 	DECLARE_BITMAP(possible_idx, ICE_MAX_FV_WORDS);
4874 	DECLARE_BITMAP(recipes, ICE_MAX_NUM_RECIPES);
4875 	DECLARE_BITMAP(used_idx, ICE_MAX_FV_WORDS);
4876 	u16 bit;
4877 
4878 	bitmap_zero(recipes, ICE_MAX_NUM_RECIPES);
4879 	bitmap_zero(used_idx, ICE_MAX_FV_WORDS);
4880 
4881 	bitmap_fill(possible_idx, ICE_MAX_FV_WORDS);
4882 
4883 	/* For each profile we are going to associate the recipe with, add the
4884 	 * recipes that are associated with that profile. This will give us
4885 	 * the set of recipes that our recipe may collide with. Also, determine
4886 	 * what possible result indexes are usable given this set of profiles.
4887 	 */
4888 	for_each_set_bit(bit, profiles, ICE_MAX_NUM_PROFILES) {
4889 		bitmap_or(recipes, recipes, profile_to_recipe[bit],
4890 			  ICE_MAX_NUM_RECIPES);
4891 		bitmap_and(possible_idx, possible_idx,
4892 			   hw->switch_info->prof_res_bm[bit],
4893 			   ICE_MAX_FV_WORDS);
4894 	}
4895 
4896 	/* For each recipe that our new recipe may collide with, determine
4897 	 * which indexes have been used.
4898 	 */
4899 	for_each_set_bit(bit, recipes, ICE_MAX_NUM_RECIPES)
4900 		bitmap_or(used_idx, used_idx,
4901 			  hw->switch_info->recp_list[bit].res_idxs,
4902 			  ICE_MAX_FV_WORDS);
4903 
4904 	bitmap_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
4905 
4906 	/* return number of free indexes */
4907 	return (u16)bitmap_weight(free_idx, ICE_MAX_FV_WORDS);
4908 }
4909 
4910 /**
4911  * ice_add_sw_recipe - function to call AQ calls to create switch recipe
4912  * @hw: pointer to hardware structure
4913  * @rm: recipe management list entry
4914  * @profiles: bitmap of profiles that will be associated.
4915  */
4916 static int
4917 ice_add_sw_recipe(struct ice_hw *hw, struct ice_sw_recipe *rm,
4918 		  unsigned long *profiles)
4919 {
4920 	DECLARE_BITMAP(result_idx_bm, ICE_MAX_FV_WORDS);
4921 	struct ice_aqc_recipe_content *content;
4922 	struct ice_aqc_recipe_data_elem *tmp;
4923 	struct ice_aqc_recipe_data_elem *buf;
4924 	struct ice_recp_grp_entry *entry;
4925 	u16 free_res_idx;
4926 	u16 recipe_count;
4927 	u8 chain_idx;
4928 	u8 recps = 0;
4929 	int status;
4930 
4931 	/* When more than one recipe are required, another recipe is needed to
4932 	 * chain them together. Matching a tunnel metadata ID takes up one of
4933 	 * the match fields in the chaining recipe reducing the number of
4934 	 * chained recipes by one.
4935 	 */
4936 	 /* check number of free result indices */
4937 	bitmap_zero(result_idx_bm, ICE_MAX_FV_WORDS);
4938 	free_res_idx = ice_find_free_recp_res_idx(hw, profiles, result_idx_bm);
4939 
4940 	ice_debug(hw, ICE_DBG_SW, "Result idx slots: %d, need %d\n",
4941 		  free_res_idx, rm->n_grp_count);
4942 
4943 	if (rm->n_grp_count > 1) {
4944 		if (rm->n_grp_count > free_res_idx)
4945 			return -ENOSPC;
4946 
4947 		rm->n_grp_count++;
4948 	}
4949 
4950 	if (rm->n_grp_count > ICE_MAX_CHAIN_RECIPE)
4951 		return -ENOSPC;
4952 
4953 	tmp = kcalloc(ICE_MAX_NUM_RECIPES, sizeof(*tmp), GFP_KERNEL);
4954 	if (!tmp)
4955 		return -ENOMEM;
4956 
4957 	buf = devm_kcalloc(ice_hw_to_dev(hw), rm->n_grp_count, sizeof(*buf),
4958 			   GFP_KERNEL);
4959 	if (!buf) {
4960 		status = -ENOMEM;
4961 		goto err_mem;
4962 	}
4963 
4964 	bitmap_zero(rm->r_bitmap, ICE_MAX_NUM_RECIPES);
4965 	recipe_count = ICE_MAX_NUM_RECIPES;
4966 	status = ice_aq_get_recipe(hw, tmp, &recipe_count, ICE_SW_LKUP_MAC,
4967 				   NULL);
4968 	if (status || recipe_count == 0)
4969 		goto err_unroll;
4970 
4971 	/* Allocate the recipe resources, and configure them according to the
4972 	 * match fields from protocol headers and extracted field vectors.
4973 	 */
4974 	chain_idx = find_first_bit(result_idx_bm, ICE_MAX_FV_WORDS);
4975 	list_for_each_entry(entry, &rm->rg_list, l_entry) {
4976 		u8 i;
4977 
4978 		status = ice_alloc_recipe(hw, &entry->rid);
4979 		if (status)
4980 			goto err_unroll;
4981 
4982 		content = &buf[recps].content;
4983 
4984 		/* Clear the result index of the located recipe, as this will be
4985 		 * updated, if needed, later in the recipe creation process.
4986 		 */
4987 		tmp[0].content.result_indx = 0;
4988 
4989 		buf[recps] = tmp[0];
4990 		buf[recps].recipe_indx = (u8)entry->rid;
4991 		/* if the recipe is a non-root recipe RID should be programmed
4992 		 * as 0 for the rules to be applied correctly.
4993 		 */
4994 		content->rid = 0;
4995 		memset(&content->lkup_indx, 0,
4996 		       sizeof(content->lkup_indx));
4997 
4998 		/* All recipes use look-up index 0 to match switch ID. */
4999 		content->lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
5000 		content->mask[0] = cpu_to_le16(ICE_AQ_SW_ID_LKUP_MASK);
5001 		/* Setup lkup_indx 1..4 to INVALID/ignore and set the mask
5002 		 * to be 0
5003 		 */
5004 		for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
5005 			content->lkup_indx[i] = 0x80;
5006 			content->mask[i] = 0;
5007 		}
5008 
5009 		for (i = 0; i < entry->r_group.n_val_pairs; i++) {
5010 			content->lkup_indx[i + 1] = entry->fv_idx[i];
5011 			content->mask[i + 1] = cpu_to_le16(entry->fv_mask[i]);
5012 		}
5013 
5014 		if (rm->n_grp_count > 1) {
5015 			/* Checks to see if there really is a valid result index
5016 			 * that can be used.
5017 			 */
5018 			if (chain_idx >= ICE_MAX_FV_WORDS) {
5019 				ice_debug(hw, ICE_DBG_SW, "No chain index available\n");
5020 				status = -ENOSPC;
5021 				goto err_unroll;
5022 			}
5023 
5024 			entry->chain_idx = chain_idx;
5025 			content->result_indx =
5026 				ICE_AQ_RECIPE_RESULT_EN |
5027 				FIELD_PREP(ICE_AQ_RECIPE_RESULT_DATA_M,
5028 					   chain_idx);
5029 			clear_bit(chain_idx, result_idx_bm);
5030 			chain_idx = find_first_bit(result_idx_bm,
5031 						   ICE_MAX_FV_WORDS);
5032 		}
5033 
5034 		/* fill recipe dependencies */
5035 		bitmap_zero((unsigned long *)buf[recps].recipe_bitmap,
5036 			    ICE_MAX_NUM_RECIPES);
5037 		set_bit(buf[recps].recipe_indx,
5038 			(unsigned long *)buf[recps].recipe_bitmap);
5039 		content->act_ctrl_fwd_priority = rm->priority;
5040 
5041 		if (rm->need_pass_l2)
5042 			content->act_ctrl |= ICE_AQ_RECIPE_ACT_NEED_PASS_L2;
5043 
5044 		if (rm->allow_pass_l2)
5045 			content->act_ctrl |= ICE_AQ_RECIPE_ACT_ALLOW_PASS_L2;
5046 		recps++;
5047 	}
5048 
5049 	if (rm->n_grp_count == 1) {
5050 		rm->root_rid = buf[0].recipe_indx;
5051 		set_bit(buf[0].recipe_indx, rm->r_bitmap);
5052 		buf[0].content.rid = rm->root_rid | ICE_AQ_RECIPE_ID_IS_ROOT;
5053 		if (sizeof(buf[0].recipe_bitmap) >= sizeof(rm->r_bitmap)) {
5054 			memcpy(buf[0].recipe_bitmap, rm->r_bitmap,
5055 			       sizeof(buf[0].recipe_bitmap));
5056 		} else {
5057 			status = -EINVAL;
5058 			goto err_unroll;
5059 		}
5060 		/* Applicable only for ROOT_RECIPE, set the fwd_priority for
5061 		 * the recipe which is getting created if specified
5062 		 * by user. Usually any advanced switch filter, which results
5063 		 * into new extraction sequence, ended up creating a new recipe
5064 		 * of type ROOT and usually recipes are associated with profiles
5065 		 * Switch rule referreing newly created recipe, needs to have
5066 		 * either/or 'fwd' or 'join' priority, otherwise switch rule
5067 		 * evaluation will not happen correctly. In other words, if
5068 		 * switch rule to be evaluated on priority basis, then recipe
5069 		 * needs to have priority, otherwise it will be evaluated last.
5070 		 */
5071 		buf[0].content.act_ctrl_fwd_priority = rm->priority;
5072 	} else {
5073 		struct ice_recp_grp_entry *last_chain_entry;
5074 		u16 rid, i;
5075 
5076 		/* Allocate the last recipe that will chain the outcomes of the
5077 		 * other recipes together
5078 		 */
5079 		status = ice_alloc_recipe(hw, &rid);
5080 		if (status)
5081 			goto err_unroll;
5082 
5083 		content = &buf[recps].content;
5084 
5085 		buf[recps].recipe_indx = (u8)rid;
5086 		content->rid = (u8)rid;
5087 		content->rid |= ICE_AQ_RECIPE_ID_IS_ROOT;
5088 		/* the new entry created should also be part of rg_list to
5089 		 * make sure we have complete recipe
5090 		 */
5091 		last_chain_entry = devm_kzalloc(ice_hw_to_dev(hw),
5092 						sizeof(*last_chain_entry),
5093 						GFP_KERNEL);
5094 		if (!last_chain_entry) {
5095 			status = -ENOMEM;
5096 			goto err_unroll;
5097 		}
5098 		last_chain_entry->rid = rid;
5099 		memset(&content->lkup_indx, 0, sizeof(content->lkup_indx));
5100 		/* All recipes use look-up index 0 to match switch ID. */
5101 		content->lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
5102 		content->mask[0] = cpu_to_le16(ICE_AQ_SW_ID_LKUP_MASK);
5103 		for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
5104 			content->lkup_indx[i] = ICE_AQ_RECIPE_LKUP_IGNORE;
5105 			content->mask[i] = 0;
5106 		}
5107 
5108 		i = 1;
5109 		/* update r_bitmap with the recp that is used for chaining */
5110 		set_bit(rid, rm->r_bitmap);
5111 		/* this is the recipe that chains all the other recipes so it
5112 		 * should not have a chaining ID to indicate the same
5113 		 */
5114 		last_chain_entry->chain_idx = ICE_INVAL_CHAIN_IND;
5115 		list_for_each_entry(entry, &rm->rg_list, l_entry) {
5116 			last_chain_entry->fv_idx[i] = entry->chain_idx;
5117 			content->lkup_indx[i] = entry->chain_idx;
5118 			content->mask[i++] = cpu_to_le16(0xFFFF);
5119 			set_bit(entry->rid, rm->r_bitmap);
5120 		}
5121 		list_add(&last_chain_entry->l_entry, &rm->rg_list);
5122 		if (sizeof(buf[recps].recipe_bitmap) >=
5123 		    sizeof(rm->r_bitmap)) {
5124 			memcpy(buf[recps].recipe_bitmap, rm->r_bitmap,
5125 			       sizeof(buf[recps].recipe_bitmap));
5126 		} else {
5127 			status = -EINVAL;
5128 			goto err_unroll;
5129 		}
5130 		content->act_ctrl_fwd_priority = rm->priority;
5131 
5132 		recps++;
5133 		rm->root_rid = (u8)rid;
5134 	}
5135 	status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
5136 	if (status)
5137 		goto err_unroll;
5138 
5139 	status = ice_aq_add_recipe(hw, buf, rm->n_grp_count, NULL);
5140 	ice_release_change_lock(hw);
5141 	if (status)
5142 		goto err_unroll;
5143 
5144 	/* Every recipe that just got created add it to the recipe
5145 	 * book keeping list
5146 	 */
5147 	list_for_each_entry(entry, &rm->rg_list, l_entry) {
5148 		struct ice_switch_info *sw = hw->switch_info;
5149 		bool is_root, idx_found = false;
5150 		struct ice_sw_recipe *recp;
5151 		u16 idx, buf_idx = 0;
5152 
5153 		/* find buffer index for copying some data */
5154 		for (idx = 0; idx < rm->n_grp_count; idx++)
5155 			if (buf[idx].recipe_indx == entry->rid) {
5156 				buf_idx = idx;
5157 				idx_found = true;
5158 			}
5159 
5160 		if (!idx_found) {
5161 			status = -EIO;
5162 			goto err_unroll;
5163 		}
5164 
5165 		recp = &sw->recp_list[entry->rid];
5166 		is_root = (rm->root_rid == entry->rid);
5167 		recp->is_root = is_root;
5168 
5169 		recp->root_rid = entry->rid;
5170 		recp->big_recp = (is_root && rm->n_grp_count > 1);
5171 
5172 		memcpy(&recp->ext_words, entry->r_group.pairs,
5173 		       entry->r_group.n_val_pairs * sizeof(struct ice_fv_word));
5174 
5175 		memcpy(recp->r_bitmap, buf[buf_idx].recipe_bitmap,
5176 		       sizeof(recp->r_bitmap));
5177 
5178 		/* Copy non-result fv index values and masks to recipe. This
5179 		 * call will also update the result recipe bitmask.
5180 		 */
5181 		ice_collect_result_idx(&buf[buf_idx], recp);
5182 
5183 		/* for non-root recipes, also copy to the root, this allows
5184 		 * easier matching of a complete chained recipe
5185 		 */
5186 		if (!is_root)
5187 			ice_collect_result_idx(&buf[buf_idx],
5188 					       &sw->recp_list[rm->root_rid]);
5189 
5190 		recp->n_ext_words = entry->r_group.n_val_pairs;
5191 		recp->chain_idx = entry->chain_idx;
5192 		recp->priority = buf[buf_idx].content.act_ctrl_fwd_priority;
5193 		recp->n_grp_count = rm->n_grp_count;
5194 		recp->tun_type = rm->tun_type;
5195 		recp->need_pass_l2 = rm->need_pass_l2;
5196 		recp->allow_pass_l2 = rm->allow_pass_l2;
5197 		recp->recp_created = true;
5198 	}
5199 	rm->root_buf = buf;
5200 	kfree(tmp);
5201 	return status;
5202 
5203 err_unroll:
5204 err_mem:
5205 	kfree(tmp);
5206 	devm_kfree(ice_hw_to_dev(hw), buf);
5207 	return status;
5208 }
5209 
5210 /**
5211  * ice_create_recipe_group - creates recipe group
5212  * @hw: pointer to hardware structure
5213  * @rm: recipe management list entry
5214  * @lkup_exts: lookup elements
5215  */
5216 static int
5217 ice_create_recipe_group(struct ice_hw *hw, struct ice_sw_recipe *rm,
5218 			struct ice_prot_lkup_ext *lkup_exts)
5219 {
5220 	u8 recp_count = 0;
5221 	int status;
5222 
5223 	rm->n_grp_count = 0;
5224 
5225 	/* Create recipes for words that are marked not done by packing them
5226 	 * as best fit.
5227 	 */
5228 	status = ice_create_first_fit_recp_def(hw, lkup_exts,
5229 					       &rm->rg_list, &recp_count);
5230 	if (!status) {
5231 		rm->n_grp_count += recp_count;
5232 		rm->n_ext_words = lkup_exts->n_val_words;
5233 		memcpy(&rm->ext_words, lkup_exts->fv_words,
5234 		       sizeof(rm->ext_words));
5235 		memcpy(rm->word_masks, lkup_exts->field_mask,
5236 		       sizeof(rm->word_masks));
5237 	}
5238 
5239 	return status;
5240 }
5241 
5242 /* ice_get_compat_fv_bitmap - Get compatible field vector bitmap for rule
5243  * @hw: pointer to hardware structure
5244  * @rinfo: other information regarding the rule e.g. priority and action info
5245  * @bm: pointer to memory for returning the bitmap of field vectors
5246  */
5247 static void
5248 ice_get_compat_fv_bitmap(struct ice_hw *hw, struct ice_adv_rule_info *rinfo,
5249 			 unsigned long *bm)
5250 {
5251 	enum ice_prof_type prof_type;
5252 
5253 	bitmap_zero(bm, ICE_MAX_NUM_PROFILES);
5254 
5255 	switch (rinfo->tun_type) {
5256 	case ICE_NON_TUN:
5257 		prof_type = ICE_PROF_NON_TUN;
5258 		break;
5259 	case ICE_ALL_TUNNELS:
5260 		prof_type = ICE_PROF_TUN_ALL;
5261 		break;
5262 	case ICE_SW_TUN_GENEVE:
5263 	case ICE_SW_TUN_VXLAN:
5264 		prof_type = ICE_PROF_TUN_UDP;
5265 		break;
5266 	case ICE_SW_TUN_NVGRE:
5267 		prof_type = ICE_PROF_TUN_GRE;
5268 		break;
5269 	case ICE_SW_TUN_GTPU:
5270 		prof_type = ICE_PROF_TUN_GTPU;
5271 		break;
5272 	case ICE_SW_TUN_GTPC:
5273 		prof_type = ICE_PROF_TUN_GTPC;
5274 		break;
5275 	case ICE_SW_TUN_AND_NON_TUN:
5276 	default:
5277 		prof_type = ICE_PROF_ALL;
5278 		break;
5279 	}
5280 
5281 	ice_get_sw_fv_bitmap(hw, prof_type, bm);
5282 }
5283 
5284 /**
5285  * ice_add_adv_recipe - Add an advanced recipe that is not part of the default
5286  * @hw: pointer to hardware structure
5287  * @lkups: lookup elements or match criteria for the advanced recipe, one
5288  *  structure per protocol header
5289  * @lkups_cnt: number of protocols
5290  * @rinfo: other information regarding the rule e.g. priority and action info
5291  * @rid: return the recipe ID of the recipe created
5292  */
5293 static int
5294 ice_add_adv_recipe(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5295 		   u16 lkups_cnt, struct ice_adv_rule_info *rinfo, u16 *rid)
5296 {
5297 	DECLARE_BITMAP(fv_bitmap, ICE_MAX_NUM_PROFILES);
5298 	DECLARE_BITMAP(profiles, ICE_MAX_NUM_PROFILES);
5299 	struct ice_prot_lkup_ext *lkup_exts;
5300 	struct ice_recp_grp_entry *r_entry;
5301 	struct ice_sw_fv_list_entry *fvit;
5302 	struct ice_recp_grp_entry *r_tmp;
5303 	struct ice_sw_fv_list_entry *tmp;
5304 	struct ice_sw_recipe *rm;
5305 	int status = 0;
5306 	u8 i;
5307 
5308 	if (!lkups_cnt)
5309 		return -EINVAL;
5310 
5311 	lkup_exts = kzalloc(sizeof(*lkup_exts), GFP_KERNEL);
5312 	if (!lkup_exts)
5313 		return -ENOMEM;
5314 
5315 	/* Determine the number of words to be matched and if it exceeds a
5316 	 * recipe's restrictions
5317 	 */
5318 	for (i = 0; i < lkups_cnt; i++) {
5319 		u16 count;
5320 
5321 		if (lkups[i].type >= ICE_PROTOCOL_LAST) {
5322 			status = -EIO;
5323 			goto err_free_lkup_exts;
5324 		}
5325 
5326 		count = ice_fill_valid_words(&lkups[i], lkup_exts);
5327 		if (!count) {
5328 			status = -EIO;
5329 			goto err_free_lkup_exts;
5330 		}
5331 	}
5332 
5333 	rm = kzalloc(sizeof(*rm), GFP_KERNEL);
5334 	if (!rm) {
5335 		status = -ENOMEM;
5336 		goto err_free_lkup_exts;
5337 	}
5338 
5339 	/* Get field vectors that contain fields extracted from all the protocol
5340 	 * headers being programmed.
5341 	 */
5342 	INIT_LIST_HEAD(&rm->fv_list);
5343 	INIT_LIST_HEAD(&rm->rg_list);
5344 
5345 	/* Get bitmap of field vectors (profiles) that are compatible with the
5346 	 * rule request; only these will be searched in the subsequent call to
5347 	 * ice_get_sw_fv_list.
5348 	 */
5349 	ice_get_compat_fv_bitmap(hw, rinfo, fv_bitmap);
5350 
5351 	status = ice_get_sw_fv_list(hw, lkup_exts, fv_bitmap, &rm->fv_list);
5352 	if (status)
5353 		goto err_unroll;
5354 
5355 	/* Group match words into recipes using preferred recipe grouping
5356 	 * criteria.
5357 	 */
5358 	status = ice_create_recipe_group(hw, rm, lkup_exts);
5359 	if (status)
5360 		goto err_unroll;
5361 
5362 	/* set the recipe priority if specified */
5363 	rm->priority = (u8)rinfo->priority;
5364 
5365 	rm->need_pass_l2 = rinfo->need_pass_l2;
5366 	rm->allow_pass_l2 = rinfo->allow_pass_l2;
5367 
5368 	/* Find offsets from the field vector. Pick the first one for all the
5369 	 * recipes.
5370 	 */
5371 	status = ice_fill_fv_word_index(hw, &rm->fv_list, &rm->rg_list);
5372 	if (status)
5373 		goto err_unroll;
5374 
5375 	/* get bitmap of all profiles the recipe will be associated with */
5376 	bitmap_zero(profiles, ICE_MAX_NUM_PROFILES);
5377 	list_for_each_entry(fvit, &rm->fv_list, list_entry) {
5378 		ice_debug(hw, ICE_DBG_SW, "profile: %d\n", fvit->profile_id);
5379 		set_bit((u16)fvit->profile_id, profiles);
5380 	}
5381 
5382 	/* Look for a recipe which matches our requested fv / mask list */
5383 	*rid = ice_find_recp(hw, lkup_exts, rinfo);
5384 	if (*rid < ICE_MAX_NUM_RECIPES)
5385 		/* Success if found a recipe that match the existing criteria */
5386 		goto err_unroll;
5387 
5388 	rm->tun_type = rinfo->tun_type;
5389 	/* Recipe we need does not exist, add a recipe */
5390 	status = ice_add_sw_recipe(hw, rm, profiles);
5391 	if (status)
5392 		goto err_unroll;
5393 
5394 	/* Associate all the recipes created with all the profiles in the
5395 	 * common field vector.
5396 	 */
5397 	list_for_each_entry(fvit, &rm->fv_list, list_entry) {
5398 		DECLARE_BITMAP(r_bitmap, ICE_MAX_NUM_RECIPES);
5399 		u64 recp_assoc;
5400 		u16 j;
5401 
5402 		status = ice_aq_get_recipe_to_profile(hw, fvit->profile_id,
5403 						      &recp_assoc, NULL);
5404 		if (status)
5405 			goto err_unroll;
5406 
5407 		bitmap_from_arr64(r_bitmap, &recp_assoc, ICE_MAX_NUM_RECIPES);
5408 		bitmap_or(r_bitmap, r_bitmap, rm->r_bitmap,
5409 			  ICE_MAX_NUM_RECIPES);
5410 		status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
5411 		if (status)
5412 			goto err_unroll;
5413 
5414 		bitmap_to_arr64(&recp_assoc, r_bitmap, ICE_MAX_NUM_RECIPES);
5415 		status = ice_aq_map_recipe_to_profile(hw, fvit->profile_id,
5416 						      recp_assoc, NULL);
5417 		ice_release_change_lock(hw);
5418 
5419 		if (status)
5420 			goto err_unroll;
5421 
5422 		/* Update profile to recipe bitmap array */
5423 		bitmap_copy(profile_to_recipe[fvit->profile_id], r_bitmap,
5424 			    ICE_MAX_NUM_RECIPES);
5425 
5426 		/* Update recipe to profile bitmap array */
5427 		for_each_set_bit(j, rm->r_bitmap, ICE_MAX_NUM_RECIPES)
5428 			set_bit((u16)fvit->profile_id, recipe_to_profile[j]);
5429 	}
5430 
5431 	*rid = rm->root_rid;
5432 	memcpy(&hw->switch_info->recp_list[*rid].lkup_exts, lkup_exts,
5433 	       sizeof(*lkup_exts));
5434 err_unroll:
5435 	list_for_each_entry_safe(r_entry, r_tmp, &rm->rg_list, l_entry) {
5436 		list_del(&r_entry->l_entry);
5437 		devm_kfree(ice_hw_to_dev(hw), r_entry);
5438 	}
5439 
5440 	list_for_each_entry_safe(fvit, tmp, &rm->fv_list, list_entry) {
5441 		list_del(&fvit->list_entry);
5442 		devm_kfree(ice_hw_to_dev(hw), fvit);
5443 	}
5444 
5445 	devm_kfree(ice_hw_to_dev(hw), rm->root_buf);
5446 	kfree(rm);
5447 
5448 err_free_lkup_exts:
5449 	kfree(lkup_exts);
5450 
5451 	return status;
5452 }
5453 
5454 /**
5455  * ice_dummy_packet_add_vlan - insert VLAN header to dummy pkt
5456  *
5457  * @dummy_pkt: dummy packet profile pattern to which VLAN tag(s) will be added
5458  * @num_vlan: number of VLAN tags
5459  */
5460 static struct ice_dummy_pkt_profile *
5461 ice_dummy_packet_add_vlan(const struct ice_dummy_pkt_profile *dummy_pkt,
5462 			  u32 num_vlan)
5463 {
5464 	struct ice_dummy_pkt_profile *profile;
5465 	struct ice_dummy_pkt_offsets *offsets;
5466 	u32 buf_len, off, etype_off, i;
5467 	u8 *pkt;
5468 
5469 	if (num_vlan < 1 || num_vlan > 2)
5470 		return ERR_PTR(-EINVAL);
5471 
5472 	off = num_vlan * VLAN_HLEN;
5473 
5474 	buf_len = array_size(num_vlan, sizeof(ice_dummy_vlan_packet_offsets)) +
5475 		  dummy_pkt->offsets_len;
5476 	offsets = kzalloc(buf_len, GFP_KERNEL);
5477 	if (!offsets)
5478 		return ERR_PTR(-ENOMEM);
5479 
5480 	offsets[0] = dummy_pkt->offsets[0];
5481 	if (num_vlan == 2) {
5482 		offsets[1] = ice_dummy_qinq_packet_offsets[0];
5483 		offsets[2] = ice_dummy_qinq_packet_offsets[1];
5484 	} else if (num_vlan == 1) {
5485 		offsets[1] = ice_dummy_vlan_packet_offsets[0];
5486 	}
5487 
5488 	for (i = 1; dummy_pkt->offsets[i].type != ICE_PROTOCOL_LAST; i++) {
5489 		offsets[i + num_vlan].type = dummy_pkt->offsets[i].type;
5490 		offsets[i + num_vlan].offset =
5491 			dummy_pkt->offsets[i].offset + off;
5492 	}
5493 	offsets[i + num_vlan] = dummy_pkt->offsets[i];
5494 
5495 	etype_off = dummy_pkt->offsets[1].offset;
5496 
5497 	buf_len = array_size(num_vlan, sizeof(ice_dummy_vlan_packet)) +
5498 		  dummy_pkt->pkt_len;
5499 	pkt = kzalloc(buf_len, GFP_KERNEL);
5500 	if (!pkt) {
5501 		kfree(offsets);
5502 		return ERR_PTR(-ENOMEM);
5503 	}
5504 
5505 	memcpy(pkt, dummy_pkt->pkt, etype_off);
5506 	memcpy(pkt + etype_off,
5507 	       num_vlan == 2 ? ice_dummy_qinq_packet : ice_dummy_vlan_packet,
5508 	       off);
5509 	memcpy(pkt + etype_off + off, dummy_pkt->pkt + etype_off,
5510 	       dummy_pkt->pkt_len - etype_off);
5511 
5512 	profile = kzalloc(sizeof(*profile), GFP_KERNEL);
5513 	if (!profile) {
5514 		kfree(offsets);
5515 		kfree(pkt);
5516 		return ERR_PTR(-ENOMEM);
5517 	}
5518 
5519 	profile->offsets = offsets;
5520 	profile->pkt = pkt;
5521 	profile->pkt_len = buf_len;
5522 	profile->match |= ICE_PKT_KMALLOC;
5523 
5524 	return profile;
5525 }
5526 
5527 /**
5528  * ice_find_dummy_packet - find dummy packet
5529  *
5530  * @lkups: lookup elements or match criteria for the advanced recipe, one
5531  *	   structure per protocol header
5532  * @lkups_cnt: number of protocols
5533  * @tun_type: tunnel type
5534  *
5535  * Returns the &ice_dummy_pkt_profile corresponding to these lookup params.
5536  */
5537 static const struct ice_dummy_pkt_profile *
5538 ice_find_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5539 		      enum ice_sw_tunnel_type tun_type)
5540 {
5541 	const struct ice_dummy_pkt_profile *ret = ice_dummy_pkt_profiles;
5542 	u32 match = 0, vlan_count = 0;
5543 	u16 i;
5544 
5545 	switch (tun_type) {
5546 	case ICE_SW_TUN_GTPC:
5547 		match |= ICE_PKT_TUN_GTPC;
5548 		break;
5549 	case ICE_SW_TUN_GTPU:
5550 		match |= ICE_PKT_TUN_GTPU;
5551 		break;
5552 	case ICE_SW_TUN_NVGRE:
5553 		match |= ICE_PKT_TUN_NVGRE;
5554 		break;
5555 	case ICE_SW_TUN_GENEVE:
5556 	case ICE_SW_TUN_VXLAN:
5557 		match |= ICE_PKT_TUN_UDP;
5558 		break;
5559 	default:
5560 		break;
5561 	}
5562 
5563 	for (i = 0; i < lkups_cnt; i++) {
5564 		if (lkups[i].type == ICE_UDP_ILOS)
5565 			match |= ICE_PKT_INNER_UDP;
5566 		else if (lkups[i].type == ICE_TCP_IL)
5567 			match |= ICE_PKT_INNER_TCP;
5568 		else if (lkups[i].type == ICE_IPV6_OFOS)
5569 			match |= ICE_PKT_OUTER_IPV6;
5570 		else if (lkups[i].type == ICE_VLAN_OFOS ||
5571 			 lkups[i].type == ICE_VLAN_EX)
5572 			vlan_count++;
5573 		else if (lkups[i].type == ICE_VLAN_IN)
5574 			vlan_count++;
5575 		else if (lkups[i].type == ICE_ETYPE_OL &&
5576 			 lkups[i].h_u.ethertype.ethtype_id ==
5577 				cpu_to_be16(ICE_IPV6_ETHER_ID) &&
5578 			 lkups[i].m_u.ethertype.ethtype_id ==
5579 				cpu_to_be16(0xFFFF))
5580 			match |= ICE_PKT_OUTER_IPV6;
5581 		else if (lkups[i].type == ICE_ETYPE_IL &&
5582 			 lkups[i].h_u.ethertype.ethtype_id ==
5583 				cpu_to_be16(ICE_IPV6_ETHER_ID) &&
5584 			 lkups[i].m_u.ethertype.ethtype_id ==
5585 				cpu_to_be16(0xFFFF))
5586 			match |= ICE_PKT_INNER_IPV6;
5587 		else if (lkups[i].type == ICE_IPV6_IL)
5588 			match |= ICE_PKT_INNER_IPV6;
5589 		else if (lkups[i].type == ICE_GTP_NO_PAY)
5590 			match |= ICE_PKT_GTP_NOPAY;
5591 		else if (lkups[i].type == ICE_PPPOE) {
5592 			match |= ICE_PKT_PPPOE;
5593 			if (lkups[i].h_u.pppoe_hdr.ppp_prot_id ==
5594 			    htons(PPP_IPV6))
5595 				match |= ICE_PKT_OUTER_IPV6;
5596 		} else if (lkups[i].type == ICE_L2TPV3)
5597 			match |= ICE_PKT_L2TPV3;
5598 	}
5599 
5600 	while (ret->match && (match & ret->match) != ret->match)
5601 		ret++;
5602 
5603 	if (vlan_count != 0)
5604 		ret = ice_dummy_packet_add_vlan(ret, vlan_count);
5605 
5606 	return ret;
5607 }
5608 
5609 /**
5610  * ice_fill_adv_dummy_packet - fill a dummy packet with given match criteria
5611  *
5612  * @lkups: lookup elements or match criteria for the advanced recipe, one
5613  *	   structure per protocol header
5614  * @lkups_cnt: number of protocols
5615  * @s_rule: stores rule information from the match criteria
5616  * @profile: dummy packet profile (the template, its size and header offsets)
5617  */
5618 static int
5619 ice_fill_adv_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
5620 			  struct ice_sw_rule_lkup_rx_tx *s_rule,
5621 			  const struct ice_dummy_pkt_profile *profile)
5622 {
5623 	u8 *pkt;
5624 	u16 i;
5625 
5626 	/* Start with a packet with a pre-defined/dummy content. Then, fill
5627 	 * in the header values to be looked up or matched.
5628 	 */
5629 	pkt = s_rule->hdr_data;
5630 
5631 	memcpy(pkt, profile->pkt, profile->pkt_len);
5632 
5633 	for (i = 0; i < lkups_cnt; i++) {
5634 		const struct ice_dummy_pkt_offsets *offsets = profile->offsets;
5635 		enum ice_protocol_type type;
5636 		u16 offset = 0, len = 0, j;
5637 		bool found = false;
5638 
5639 		/* find the start of this layer; it should be found since this
5640 		 * was already checked when search for the dummy packet
5641 		 */
5642 		type = lkups[i].type;
5643 		/* metadata isn't present in the packet */
5644 		if (type == ICE_HW_METADATA)
5645 			continue;
5646 
5647 		for (j = 0; offsets[j].type != ICE_PROTOCOL_LAST; j++) {
5648 			if (type == offsets[j].type) {
5649 				offset = offsets[j].offset;
5650 				found = true;
5651 				break;
5652 			}
5653 		}
5654 		/* this should never happen in a correct calling sequence */
5655 		if (!found)
5656 			return -EINVAL;
5657 
5658 		switch (lkups[i].type) {
5659 		case ICE_MAC_OFOS:
5660 		case ICE_MAC_IL:
5661 			len = sizeof(struct ice_ether_hdr);
5662 			break;
5663 		case ICE_ETYPE_OL:
5664 		case ICE_ETYPE_IL:
5665 			len = sizeof(struct ice_ethtype_hdr);
5666 			break;
5667 		case ICE_VLAN_OFOS:
5668 		case ICE_VLAN_EX:
5669 		case ICE_VLAN_IN:
5670 			len = sizeof(struct ice_vlan_hdr);
5671 			break;
5672 		case ICE_IPV4_OFOS:
5673 		case ICE_IPV4_IL:
5674 			len = sizeof(struct ice_ipv4_hdr);
5675 			break;
5676 		case ICE_IPV6_OFOS:
5677 		case ICE_IPV6_IL:
5678 			len = sizeof(struct ice_ipv6_hdr);
5679 			break;
5680 		case ICE_TCP_IL:
5681 		case ICE_UDP_OF:
5682 		case ICE_UDP_ILOS:
5683 			len = sizeof(struct ice_l4_hdr);
5684 			break;
5685 		case ICE_SCTP_IL:
5686 			len = sizeof(struct ice_sctp_hdr);
5687 			break;
5688 		case ICE_NVGRE:
5689 			len = sizeof(struct ice_nvgre_hdr);
5690 			break;
5691 		case ICE_VXLAN:
5692 		case ICE_GENEVE:
5693 			len = sizeof(struct ice_udp_tnl_hdr);
5694 			break;
5695 		case ICE_GTP_NO_PAY:
5696 		case ICE_GTP:
5697 			len = sizeof(struct ice_udp_gtp_hdr);
5698 			break;
5699 		case ICE_PPPOE:
5700 			len = sizeof(struct ice_pppoe_hdr);
5701 			break;
5702 		case ICE_L2TPV3:
5703 			len = sizeof(struct ice_l2tpv3_sess_hdr);
5704 			break;
5705 		default:
5706 			return -EINVAL;
5707 		}
5708 
5709 		/* the length should be a word multiple */
5710 		if (len % ICE_BYTES_PER_WORD)
5711 			return -EIO;
5712 
5713 		/* We have the offset to the header start, the length, the
5714 		 * caller's header values and mask. Use this information to
5715 		 * copy the data into the dummy packet appropriately based on
5716 		 * the mask. Note that we need to only write the bits as
5717 		 * indicated by the mask to make sure we don't improperly write
5718 		 * over any significant packet data.
5719 		 */
5720 		for (j = 0; j < len / sizeof(u16); j++) {
5721 			u16 *ptr = (u16 *)(pkt + offset);
5722 			u16 mask = lkups[i].m_raw[j];
5723 
5724 			if (!mask)
5725 				continue;
5726 
5727 			ptr[j] = (ptr[j] & ~mask) | (lkups[i].h_raw[j] & mask);
5728 		}
5729 	}
5730 
5731 	s_rule->hdr_len = cpu_to_le16(profile->pkt_len);
5732 
5733 	return 0;
5734 }
5735 
5736 /**
5737  * ice_fill_adv_packet_tun - fill dummy packet with udp tunnel port
5738  * @hw: pointer to the hardware structure
5739  * @tun_type: tunnel type
5740  * @pkt: dummy packet to fill in
5741  * @offsets: offset info for the dummy packet
5742  */
5743 static int
5744 ice_fill_adv_packet_tun(struct ice_hw *hw, enum ice_sw_tunnel_type tun_type,
5745 			u8 *pkt, const struct ice_dummy_pkt_offsets *offsets)
5746 {
5747 	u16 open_port, i;
5748 
5749 	switch (tun_type) {
5750 	case ICE_SW_TUN_VXLAN:
5751 		if (!ice_get_open_tunnel_port(hw, &open_port, TNL_VXLAN))
5752 			return -EIO;
5753 		break;
5754 	case ICE_SW_TUN_GENEVE:
5755 		if (!ice_get_open_tunnel_port(hw, &open_port, TNL_GENEVE))
5756 			return -EIO;
5757 		break;
5758 	default:
5759 		/* Nothing needs to be done for this tunnel type */
5760 		return 0;
5761 	}
5762 
5763 	/* Find the outer UDP protocol header and insert the port number */
5764 	for (i = 0; offsets[i].type != ICE_PROTOCOL_LAST; i++) {
5765 		if (offsets[i].type == ICE_UDP_OF) {
5766 			struct ice_l4_hdr *hdr;
5767 			u16 offset;
5768 
5769 			offset = offsets[i].offset;
5770 			hdr = (struct ice_l4_hdr *)&pkt[offset];
5771 			hdr->dst_port = cpu_to_be16(open_port);
5772 
5773 			return 0;
5774 		}
5775 	}
5776 
5777 	return -EIO;
5778 }
5779 
5780 /**
5781  * ice_fill_adv_packet_vlan - fill dummy packet with VLAN tag type
5782  * @hw: pointer to hw structure
5783  * @vlan_type: VLAN tag type
5784  * @pkt: dummy packet to fill in
5785  * @offsets: offset info for the dummy packet
5786  */
5787 static int
5788 ice_fill_adv_packet_vlan(struct ice_hw *hw, u16 vlan_type, u8 *pkt,
5789 			 const struct ice_dummy_pkt_offsets *offsets)
5790 {
5791 	u16 i;
5792 
5793 	/* Check if there is something to do */
5794 	if (!vlan_type || !ice_is_dvm_ena(hw))
5795 		return 0;
5796 
5797 	/* Find VLAN header and insert VLAN TPID */
5798 	for (i = 0; offsets[i].type != ICE_PROTOCOL_LAST; i++) {
5799 		if (offsets[i].type == ICE_VLAN_OFOS ||
5800 		    offsets[i].type == ICE_VLAN_EX) {
5801 			struct ice_vlan_hdr *hdr;
5802 			u16 offset;
5803 
5804 			offset = offsets[i].offset;
5805 			hdr = (struct ice_vlan_hdr *)&pkt[offset];
5806 			hdr->type = cpu_to_be16(vlan_type);
5807 
5808 			return 0;
5809 		}
5810 	}
5811 
5812 	return -EIO;
5813 }
5814 
5815 static bool ice_rules_equal(const struct ice_adv_rule_info *first,
5816 			    const struct ice_adv_rule_info *second)
5817 {
5818 	return first->sw_act.flag == second->sw_act.flag &&
5819 	       first->tun_type == second->tun_type &&
5820 	       first->vlan_type == second->vlan_type &&
5821 	       first->src_vsi == second->src_vsi &&
5822 	       first->need_pass_l2 == second->need_pass_l2 &&
5823 	       first->allow_pass_l2 == second->allow_pass_l2;
5824 }
5825 
5826 /**
5827  * ice_find_adv_rule_entry - Search a rule entry
5828  * @hw: pointer to the hardware structure
5829  * @lkups: lookup elements or match criteria for the advanced recipe, one
5830  *	   structure per protocol header
5831  * @lkups_cnt: number of protocols
5832  * @recp_id: recipe ID for which we are finding the rule
5833  * @rinfo: other information regarding the rule e.g. priority and action info
5834  *
5835  * Helper function to search for a given advance rule entry
5836  * Returns pointer to entry storing the rule if found
5837  */
5838 static struct ice_adv_fltr_mgmt_list_entry *
5839 ice_find_adv_rule_entry(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5840 			u16 lkups_cnt, u16 recp_id,
5841 			struct ice_adv_rule_info *rinfo)
5842 {
5843 	struct ice_adv_fltr_mgmt_list_entry *list_itr;
5844 	struct ice_switch_info *sw = hw->switch_info;
5845 	int i;
5846 
5847 	list_for_each_entry(list_itr, &sw->recp_list[recp_id].filt_rules,
5848 			    list_entry) {
5849 		bool lkups_matched = true;
5850 
5851 		if (lkups_cnt != list_itr->lkups_cnt)
5852 			continue;
5853 		for (i = 0; i < list_itr->lkups_cnt; i++)
5854 			if (memcmp(&list_itr->lkups[i], &lkups[i],
5855 				   sizeof(*lkups))) {
5856 				lkups_matched = false;
5857 				break;
5858 			}
5859 		if (ice_rules_equal(rinfo, &list_itr->rule_info) &&
5860 		    lkups_matched)
5861 			return list_itr;
5862 	}
5863 	return NULL;
5864 }
5865 
5866 /**
5867  * ice_adv_add_update_vsi_list
5868  * @hw: pointer to the hardware structure
5869  * @m_entry: pointer to current adv filter management list entry
5870  * @cur_fltr: filter information from the book keeping entry
5871  * @new_fltr: filter information with the new VSI to be added
5872  *
5873  * Call AQ command to add or update previously created VSI list with new VSI.
5874  *
5875  * Helper function to do book keeping associated with adding filter information
5876  * The algorithm to do the booking keeping is described below :
5877  * When a VSI needs to subscribe to a given advanced filter
5878  *	if only one VSI has been added till now
5879  *		Allocate a new VSI list and add two VSIs
5880  *		to this list using switch rule command
5881  *		Update the previously created switch rule with the
5882  *		newly created VSI list ID
5883  *	if a VSI list was previously created
5884  *		Add the new VSI to the previously created VSI list set
5885  *		using the update switch rule command
5886  */
5887 static int
5888 ice_adv_add_update_vsi_list(struct ice_hw *hw,
5889 			    struct ice_adv_fltr_mgmt_list_entry *m_entry,
5890 			    struct ice_adv_rule_info *cur_fltr,
5891 			    struct ice_adv_rule_info *new_fltr)
5892 {
5893 	u16 vsi_list_id = 0;
5894 	int status;
5895 
5896 	if (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5897 	    cur_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP ||
5898 	    cur_fltr->sw_act.fltr_act == ICE_DROP_PACKET)
5899 		return -EOPNOTSUPP;
5900 
5901 	if ((new_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5902 	     new_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP) &&
5903 	    (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI ||
5904 	     cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI_LIST))
5905 		return -EOPNOTSUPP;
5906 
5907 	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
5908 		 /* Only one entry existed in the mapping and it was not already
5909 		  * a part of a VSI list. So, create a VSI list with the old and
5910 		  * new VSIs.
5911 		  */
5912 		struct ice_fltr_info tmp_fltr;
5913 		u16 vsi_handle_arr[2];
5914 
5915 		/* A rule already exists with the new VSI being added */
5916 		if (cur_fltr->sw_act.fwd_id.hw_vsi_id ==
5917 		    new_fltr->sw_act.fwd_id.hw_vsi_id)
5918 			return -EEXIST;
5919 
5920 		vsi_handle_arr[0] = cur_fltr->sw_act.vsi_handle;
5921 		vsi_handle_arr[1] = new_fltr->sw_act.vsi_handle;
5922 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
5923 						  &vsi_list_id,
5924 						  ICE_SW_LKUP_LAST);
5925 		if (status)
5926 			return status;
5927 
5928 		memset(&tmp_fltr, 0, sizeof(tmp_fltr));
5929 		tmp_fltr.flag = m_entry->rule_info.sw_act.flag;
5930 		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
5931 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
5932 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
5933 		tmp_fltr.lkup_type = ICE_SW_LKUP_LAST;
5934 
5935 		/* Update the previous switch rule of "forward to VSI" to
5936 		 * "fwd to VSI list"
5937 		 */
5938 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5939 		if (status)
5940 			return status;
5941 
5942 		cur_fltr->sw_act.fwd_id.vsi_list_id = vsi_list_id;
5943 		cur_fltr->sw_act.fltr_act = ICE_FWD_TO_VSI_LIST;
5944 		m_entry->vsi_list_info =
5945 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
5946 						vsi_list_id);
5947 	} else {
5948 		u16 vsi_handle = new_fltr->sw_act.vsi_handle;
5949 
5950 		if (!m_entry->vsi_list_info)
5951 			return -EIO;
5952 
5953 		/* A rule already exists with the new VSI being added */
5954 		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
5955 			return 0;
5956 
5957 		/* Update the previously created VSI list set with
5958 		 * the new VSI ID passed in
5959 		 */
5960 		vsi_list_id = cur_fltr->sw_act.fwd_id.vsi_list_id;
5961 
5962 		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
5963 						  vsi_list_id, false,
5964 						  ice_aqc_opc_update_sw_rules,
5965 						  ICE_SW_LKUP_LAST);
5966 		/* update VSI list mapping info with new VSI ID */
5967 		if (!status)
5968 			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
5969 	}
5970 	if (!status)
5971 		m_entry->vsi_count++;
5972 	return status;
5973 }
5974 
5975 void ice_rule_add_tunnel_metadata(struct ice_adv_lkup_elem *lkup)
5976 {
5977 	lkup->type = ICE_HW_METADATA;
5978 	lkup->m_u.metadata.flags[ICE_PKT_FLAGS_MDID21] |=
5979 		cpu_to_be16(ICE_PKT_TUNNEL_MASK);
5980 }
5981 
5982 void ice_rule_add_direction_metadata(struct ice_adv_lkup_elem *lkup)
5983 {
5984 	lkup->type = ICE_HW_METADATA;
5985 	lkup->m_u.metadata.flags[ICE_PKT_FLAGS_MDID20] |=
5986 		cpu_to_be16(ICE_PKT_FROM_NETWORK);
5987 }
5988 
5989 void ice_rule_add_vlan_metadata(struct ice_adv_lkup_elem *lkup)
5990 {
5991 	lkup->type = ICE_HW_METADATA;
5992 	lkup->m_u.metadata.flags[ICE_PKT_FLAGS_MDID20] |=
5993 		cpu_to_be16(ICE_PKT_VLAN_MASK);
5994 }
5995 
5996 void ice_rule_add_src_vsi_metadata(struct ice_adv_lkup_elem *lkup)
5997 {
5998 	lkup->type = ICE_HW_METADATA;
5999 	lkup->m_u.metadata.source_vsi = cpu_to_be16(ICE_MDID_SOURCE_VSI_MASK);
6000 }
6001 
6002 /**
6003  * ice_add_adv_rule - helper function to create an advanced switch rule
6004  * @hw: pointer to the hardware structure
6005  * @lkups: information on the words that needs to be looked up. All words
6006  * together makes one recipe
6007  * @lkups_cnt: num of entries in the lkups array
6008  * @rinfo: other information related to the rule that needs to be programmed
6009  * @added_entry: this will return recipe_id, rule_id and vsi_handle. should be
6010  *               ignored is case of error.
6011  *
6012  * This function can program only 1 rule at a time. The lkups is used to
6013  * describe the all the words that forms the "lookup" portion of the recipe.
6014  * These words can span multiple protocols. Callers to this function need to
6015  * pass in a list of protocol headers with lookup information along and mask
6016  * that determines which words are valid from the given protocol header.
6017  * rinfo describes other information related to this rule such as forwarding
6018  * IDs, priority of this rule, etc.
6019  */
6020 int
6021 ice_add_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
6022 		 u16 lkups_cnt, struct ice_adv_rule_info *rinfo,
6023 		 struct ice_rule_query_data *added_entry)
6024 {
6025 	struct ice_adv_fltr_mgmt_list_entry *m_entry, *adv_fltr = NULL;
6026 	struct ice_sw_rule_lkup_rx_tx *s_rule = NULL;
6027 	const struct ice_dummy_pkt_profile *profile;
6028 	u16 rid = 0, i, rule_buf_sz, vsi_handle;
6029 	struct list_head *rule_head;
6030 	struct ice_switch_info *sw;
6031 	u16 word_cnt;
6032 	u32 act = 0;
6033 	int status;
6034 	u8 q_rgn;
6035 
6036 	/* Initialize profile to result index bitmap */
6037 	if (!hw->switch_info->prof_res_bm_init) {
6038 		hw->switch_info->prof_res_bm_init = 1;
6039 		ice_init_prof_result_bm(hw);
6040 	}
6041 
6042 	if (!lkups_cnt)
6043 		return -EINVAL;
6044 
6045 	/* get # of words we need to match */
6046 	word_cnt = 0;
6047 	for (i = 0; i < lkups_cnt; i++) {
6048 		u16 j;
6049 
6050 		for (j = 0; j < ARRAY_SIZE(lkups->m_raw); j++)
6051 			if (lkups[i].m_raw[j])
6052 				word_cnt++;
6053 	}
6054 
6055 	if (!word_cnt)
6056 		return -EINVAL;
6057 
6058 	if (word_cnt > ICE_MAX_CHAIN_WORDS)
6059 		return -ENOSPC;
6060 
6061 	/* locate a dummy packet */
6062 	profile = ice_find_dummy_packet(lkups, lkups_cnt, rinfo->tun_type);
6063 	if (IS_ERR(profile))
6064 		return PTR_ERR(profile);
6065 
6066 	if (!(rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI ||
6067 	      rinfo->sw_act.fltr_act == ICE_FWD_TO_Q ||
6068 	      rinfo->sw_act.fltr_act == ICE_FWD_TO_QGRP ||
6069 	      rinfo->sw_act.fltr_act == ICE_DROP_PACKET ||
6070 	      rinfo->sw_act.fltr_act == ICE_MIRROR_PACKET ||
6071 	      rinfo->sw_act.fltr_act == ICE_NOP)) {
6072 		status = -EIO;
6073 		goto free_pkt_profile;
6074 	}
6075 
6076 	vsi_handle = rinfo->sw_act.vsi_handle;
6077 	if (!ice_is_vsi_valid(hw, vsi_handle)) {
6078 		status =  -EINVAL;
6079 		goto free_pkt_profile;
6080 	}
6081 
6082 	if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI ||
6083 	    rinfo->sw_act.fltr_act == ICE_MIRROR_PACKET ||
6084 	    rinfo->sw_act.fltr_act == ICE_NOP) {
6085 		rinfo->sw_act.fwd_id.hw_vsi_id =
6086 			ice_get_hw_vsi_num(hw, vsi_handle);
6087 	}
6088 
6089 	if (rinfo->src_vsi)
6090 		rinfo->sw_act.src = ice_get_hw_vsi_num(hw, rinfo->src_vsi);
6091 	else
6092 		rinfo->sw_act.src = ice_get_hw_vsi_num(hw, vsi_handle);
6093 
6094 	status = ice_add_adv_recipe(hw, lkups, lkups_cnt, rinfo, &rid);
6095 	if (status)
6096 		goto free_pkt_profile;
6097 	m_entry = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
6098 	if (m_entry) {
6099 		/* we have to add VSI to VSI_LIST and increment vsi_count.
6100 		 * Also Update VSI list so that we can change forwarding rule
6101 		 * if the rule already exists, we will check if it exists with
6102 		 * same vsi_id, if not then add it to the VSI list if it already
6103 		 * exists if not then create a VSI list and add the existing VSI
6104 		 * ID and the new VSI ID to the list
6105 		 * We will add that VSI to the list
6106 		 */
6107 		status = ice_adv_add_update_vsi_list(hw, m_entry,
6108 						     &m_entry->rule_info,
6109 						     rinfo);
6110 		if (added_entry) {
6111 			added_entry->rid = rid;
6112 			added_entry->rule_id = m_entry->rule_info.fltr_rule_id;
6113 			added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
6114 		}
6115 		goto free_pkt_profile;
6116 	}
6117 	rule_buf_sz = ICE_SW_RULE_RX_TX_HDR_SIZE(s_rule, profile->pkt_len);
6118 	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
6119 	if (!s_rule) {
6120 		status = -ENOMEM;
6121 		goto free_pkt_profile;
6122 	}
6123 
6124 	if (rinfo->sw_act.fltr_act != ICE_MIRROR_PACKET) {
6125 		if (!rinfo->flags_info.act_valid) {
6126 			act |= ICE_SINGLE_ACT_LAN_ENABLE;
6127 			act |= ICE_SINGLE_ACT_LB_ENABLE;
6128 		} else {
6129 			act |= rinfo->flags_info.act & (ICE_SINGLE_ACT_LAN_ENABLE |
6130 							ICE_SINGLE_ACT_LB_ENABLE);
6131 		}
6132 	}
6133 
6134 	switch (rinfo->sw_act.fltr_act) {
6135 	case ICE_FWD_TO_VSI:
6136 		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
6137 				  rinfo->sw_act.fwd_id.hw_vsi_id);
6138 		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_VALID_BIT;
6139 		break;
6140 	case ICE_FWD_TO_Q:
6141 		act |= ICE_SINGLE_ACT_TO_Q;
6142 		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
6143 				  rinfo->sw_act.fwd_id.q_id);
6144 		break;
6145 	case ICE_FWD_TO_QGRP:
6146 		q_rgn = rinfo->sw_act.qgrp_size > 0 ?
6147 			(u8)ilog2(rinfo->sw_act.qgrp_size) : 0;
6148 		act |= ICE_SINGLE_ACT_TO_Q;
6149 		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_INDEX_M,
6150 				  rinfo->sw_act.fwd_id.q_id);
6151 		act |= FIELD_PREP(ICE_SINGLE_ACT_Q_REGION_M, q_rgn);
6152 		break;
6153 	case ICE_DROP_PACKET:
6154 		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
6155 		       ICE_SINGLE_ACT_VALID_BIT;
6156 		break;
6157 	case ICE_MIRROR_PACKET:
6158 		act |= ICE_SINGLE_ACT_OTHER_ACTS;
6159 		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
6160 				  rinfo->sw_act.fwd_id.hw_vsi_id);
6161 		break;
6162 	case ICE_NOP:
6163 		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M,
6164 				  rinfo->sw_act.fwd_id.hw_vsi_id);
6165 		act &= ~ICE_SINGLE_ACT_VALID_BIT;
6166 		break;
6167 	default:
6168 		status = -EIO;
6169 		goto err_ice_add_adv_rule;
6170 	}
6171 
6172 	/* If there is no matching criteria for direction there
6173 	 * is only one difference between Rx and Tx:
6174 	 * - get switch id base on VSI number from source field (Tx)
6175 	 * - get switch id base on port number (Rx)
6176 	 *
6177 	 * If matching on direction metadata is chose rule direction is
6178 	 * extracted from type value set here.
6179 	 */
6180 	if (rinfo->sw_act.flag & ICE_FLTR_TX) {
6181 		s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
6182 		s_rule->src = cpu_to_le16(rinfo->sw_act.src);
6183 	} else {
6184 		s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX);
6185 		s_rule->src = cpu_to_le16(hw->port_info->lport);
6186 	}
6187 
6188 	s_rule->recipe_id = cpu_to_le16(rid);
6189 	s_rule->act = cpu_to_le32(act);
6190 
6191 	status = ice_fill_adv_dummy_packet(lkups, lkups_cnt, s_rule, profile);
6192 	if (status)
6193 		goto err_ice_add_adv_rule;
6194 
6195 	status = ice_fill_adv_packet_tun(hw, rinfo->tun_type, s_rule->hdr_data,
6196 					 profile->offsets);
6197 	if (status)
6198 		goto err_ice_add_adv_rule;
6199 
6200 	status = ice_fill_adv_packet_vlan(hw, rinfo->vlan_type,
6201 					  s_rule->hdr_data,
6202 					  profile->offsets);
6203 	if (status)
6204 		goto err_ice_add_adv_rule;
6205 
6206 	status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
6207 				 rule_buf_sz, 1, ice_aqc_opc_add_sw_rules,
6208 				 NULL);
6209 	if (status)
6210 		goto err_ice_add_adv_rule;
6211 	adv_fltr = devm_kzalloc(ice_hw_to_dev(hw),
6212 				sizeof(struct ice_adv_fltr_mgmt_list_entry),
6213 				GFP_KERNEL);
6214 	if (!adv_fltr) {
6215 		status = -ENOMEM;
6216 		goto err_ice_add_adv_rule;
6217 	}
6218 
6219 	adv_fltr->lkups = devm_kmemdup(ice_hw_to_dev(hw), lkups,
6220 				       lkups_cnt * sizeof(*lkups), GFP_KERNEL);
6221 	if (!adv_fltr->lkups) {
6222 		status = -ENOMEM;
6223 		goto err_ice_add_adv_rule;
6224 	}
6225 
6226 	adv_fltr->lkups_cnt = lkups_cnt;
6227 	adv_fltr->rule_info = *rinfo;
6228 	adv_fltr->rule_info.fltr_rule_id = le16_to_cpu(s_rule->index);
6229 	sw = hw->switch_info;
6230 	sw->recp_list[rid].adv_rule = true;
6231 	rule_head = &sw->recp_list[rid].filt_rules;
6232 
6233 	if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI)
6234 		adv_fltr->vsi_count = 1;
6235 
6236 	/* Add rule entry to book keeping list */
6237 	list_add(&adv_fltr->list_entry, rule_head);
6238 	if (added_entry) {
6239 		added_entry->rid = rid;
6240 		added_entry->rule_id = adv_fltr->rule_info.fltr_rule_id;
6241 		added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
6242 	}
6243 err_ice_add_adv_rule:
6244 	if (status && adv_fltr) {
6245 		devm_kfree(ice_hw_to_dev(hw), adv_fltr->lkups);
6246 		devm_kfree(ice_hw_to_dev(hw), adv_fltr);
6247 	}
6248 
6249 	kfree(s_rule);
6250 
6251 free_pkt_profile:
6252 	if (profile->match & ICE_PKT_KMALLOC) {
6253 		kfree(profile->offsets);
6254 		kfree(profile->pkt);
6255 		kfree(profile);
6256 	}
6257 
6258 	return status;
6259 }
6260 
6261 /**
6262  * ice_replay_vsi_fltr - Replay filters for requested VSI
6263  * @hw: pointer to the hardware structure
6264  * @vsi_handle: driver VSI handle
6265  * @recp_id: Recipe ID for which rules need to be replayed
6266  * @list_head: list for which filters need to be replayed
6267  *
6268  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
6269  * It is required to pass valid VSI handle.
6270  */
6271 static int
6272 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
6273 		    struct list_head *list_head)
6274 {
6275 	struct ice_fltr_mgmt_list_entry *itr;
6276 	int status = 0;
6277 	u16 hw_vsi_id;
6278 
6279 	if (list_empty(list_head))
6280 		return status;
6281 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
6282 
6283 	list_for_each_entry(itr, list_head, list_entry) {
6284 		struct ice_fltr_list_entry f_entry;
6285 
6286 		f_entry.fltr_info = itr->fltr_info;
6287 		if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
6288 		    itr->fltr_info.vsi_handle == vsi_handle) {
6289 			/* update the src in case it is VSI num */
6290 			if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6291 				f_entry.fltr_info.src = hw_vsi_id;
6292 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
6293 			if (status)
6294 				goto end;
6295 			continue;
6296 		}
6297 		if (!itr->vsi_list_info ||
6298 		    !test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
6299 			continue;
6300 		/* Clearing it so that the logic can add it back */
6301 		clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
6302 		f_entry.fltr_info.vsi_handle = vsi_handle;
6303 		f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
6304 		/* update the src in case it is VSI num */
6305 		if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
6306 			f_entry.fltr_info.src = hw_vsi_id;
6307 		if (recp_id == ICE_SW_LKUP_VLAN)
6308 			status = ice_add_vlan_internal(hw, &f_entry);
6309 		else
6310 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
6311 		if (status)
6312 			goto end;
6313 	}
6314 end:
6315 	return status;
6316 }
6317 
6318 /**
6319  * ice_adv_rem_update_vsi_list
6320  * @hw: pointer to the hardware structure
6321  * @vsi_handle: VSI handle of the VSI to remove
6322  * @fm_list: filter management entry for which the VSI list management needs to
6323  *	     be done
6324  */
6325 static int
6326 ice_adv_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
6327 			    struct ice_adv_fltr_mgmt_list_entry *fm_list)
6328 {
6329 	struct ice_vsi_list_map_info *vsi_list_info;
6330 	enum ice_sw_lkup_type lkup_type;
6331 	u16 vsi_list_id;
6332 	int status;
6333 
6334 	if (fm_list->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST ||
6335 	    fm_list->vsi_count == 0)
6336 		return -EINVAL;
6337 
6338 	/* A rule with the VSI being removed does not exist */
6339 	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
6340 		return -ENOENT;
6341 
6342 	lkup_type = ICE_SW_LKUP_LAST;
6343 	vsi_list_id = fm_list->rule_info.sw_act.fwd_id.vsi_list_id;
6344 	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
6345 					  ice_aqc_opc_update_sw_rules,
6346 					  lkup_type);
6347 	if (status)
6348 		return status;
6349 
6350 	fm_list->vsi_count--;
6351 	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
6352 	vsi_list_info = fm_list->vsi_list_info;
6353 	if (fm_list->vsi_count == 1) {
6354 		struct ice_fltr_info tmp_fltr;
6355 		u16 rem_vsi_handle;
6356 
6357 		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
6358 						ICE_MAX_VSI);
6359 		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
6360 			return -EIO;
6361 
6362 		/* Make sure VSI list is empty before removing it below */
6363 		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
6364 						  vsi_list_id, true,
6365 						  ice_aqc_opc_update_sw_rules,
6366 						  lkup_type);
6367 		if (status)
6368 			return status;
6369 
6370 		memset(&tmp_fltr, 0, sizeof(tmp_fltr));
6371 		tmp_fltr.flag = fm_list->rule_info.sw_act.flag;
6372 		tmp_fltr.fltr_rule_id = fm_list->rule_info.fltr_rule_id;
6373 		fm_list->rule_info.sw_act.fltr_act = ICE_FWD_TO_VSI;
6374 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI;
6375 		tmp_fltr.fwd_id.hw_vsi_id =
6376 			ice_get_hw_vsi_num(hw, rem_vsi_handle);
6377 		fm_list->rule_info.sw_act.fwd_id.hw_vsi_id =
6378 			ice_get_hw_vsi_num(hw, rem_vsi_handle);
6379 		fm_list->rule_info.sw_act.vsi_handle = rem_vsi_handle;
6380 
6381 		/* Update the previous switch rule of "MAC forward to VSI" to
6382 		 * "MAC fwd to VSI list"
6383 		 */
6384 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
6385 		if (status) {
6386 			ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
6387 				  tmp_fltr.fwd_id.hw_vsi_id, status);
6388 			return status;
6389 		}
6390 		fm_list->vsi_list_info->ref_cnt--;
6391 
6392 		/* Remove the VSI list since it is no longer used */
6393 		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
6394 		if (status) {
6395 			ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
6396 				  vsi_list_id, status);
6397 			return status;
6398 		}
6399 
6400 		list_del(&vsi_list_info->list_entry);
6401 		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
6402 		fm_list->vsi_list_info = NULL;
6403 	}
6404 
6405 	return status;
6406 }
6407 
6408 /**
6409  * ice_rem_adv_rule - removes existing advanced switch rule
6410  * @hw: pointer to the hardware structure
6411  * @lkups: information on the words that needs to be looked up. All words
6412  *         together makes one recipe
6413  * @lkups_cnt: num of entries in the lkups array
6414  * @rinfo: Its the pointer to the rule information for the rule
6415  *
6416  * This function can be used to remove 1 rule at a time. The lkups is
6417  * used to describe all the words that forms the "lookup" portion of the
6418  * rule. These words can span multiple protocols. Callers to this function
6419  * need to pass in a list of protocol headers with lookup information along
6420  * and mask that determines which words are valid from the given protocol
6421  * header. rinfo describes other information related to this rule such as
6422  * forwarding IDs, priority of this rule, etc.
6423  */
6424 static int
6425 ice_rem_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
6426 		 u16 lkups_cnt, struct ice_adv_rule_info *rinfo)
6427 {
6428 	struct ice_adv_fltr_mgmt_list_entry *list_elem;
6429 	struct ice_prot_lkup_ext lkup_exts;
6430 	bool remove_rule = false;
6431 	struct mutex *rule_lock; /* Lock to protect filter rule list */
6432 	u16 i, rid, vsi_handle;
6433 	int status = 0;
6434 
6435 	memset(&lkup_exts, 0, sizeof(lkup_exts));
6436 	for (i = 0; i < lkups_cnt; i++) {
6437 		u16 count;
6438 
6439 		if (lkups[i].type >= ICE_PROTOCOL_LAST)
6440 			return -EIO;
6441 
6442 		count = ice_fill_valid_words(&lkups[i], &lkup_exts);
6443 		if (!count)
6444 			return -EIO;
6445 	}
6446 
6447 	rid = ice_find_recp(hw, &lkup_exts, rinfo);
6448 	/* If did not find a recipe that match the existing criteria */
6449 	if (rid == ICE_MAX_NUM_RECIPES)
6450 		return -EINVAL;
6451 
6452 	rule_lock = &hw->switch_info->recp_list[rid].filt_rule_lock;
6453 	list_elem = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
6454 	/* the rule is already removed */
6455 	if (!list_elem)
6456 		return 0;
6457 	mutex_lock(rule_lock);
6458 	if (list_elem->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST) {
6459 		remove_rule = true;
6460 	} else if (list_elem->vsi_count > 1) {
6461 		remove_rule = false;
6462 		vsi_handle = rinfo->sw_act.vsi_handle;
6463 		status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
6464 	} else {
6465 		vsi_handle = rinfo->sw_act.vsi_handle;
6466 		status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
6467 		if (status) {
6468 			mutex_unlock(rule_lock);
6469 			return status;
6470 		}
6471 		if (list_elem->vsi_count == 0)
6472 			remove_rule = true;
6473 	}
6474 	mutex_unlock(rule_lock);
6475 	if (remove_rule) {
6476 		struct ice_sw_rule_lkup_rx_tx *s_rule;
6477 		u16 rule_buf_sz;
6478 
6479 		rule_buf_sz = ICE_SW_RULE_RX_TX_NO_HDR_SIZE(s_rule);
6480 		s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
6481 		if (!s_rule)
6482 			return -ENOMEM;
6483 		s_rule->act = 0;
6484 		s_rule->index = cpu_to_le16(list_elem->rule_info.fltr_rule_id);
6485 		s_rule->hdr_len = 0;
6486 		status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
6487 					 rule_buf_sz, 1,
6488 					 ice_aqc_opc_remove_sw_rules, NULL);
6489 		if (!status || status == -ENOENT) {
6490 			struct ice_switch_info *sw = hw->switch_info;
6491 
6492 			mutex_lock(rule_lock);
6493 			list_del(&list_elem->list_entry);
6494 			devm_kfree(ice_hw_to_dev(hw), list_elem->lkups);
6495 			devm_kfree(ice_hw_to_dev(hw), list_elem);
6496 			mutex_unlock(rule_lock);
6497 			if (list_empty(&sw->recp_list[rid].filt_rules))
6498 				sw->recp_list[rid].adv_rule = false;
6499 		}
6500 		kfree(s_rule);
6501 	}
6502 	return status;
6503 }
6504 
6505 /**
6506  * ice_rem_adv_rule_by_id - removes existing advanced switch rule by ID
6507  * @hw: pointer to the hardware structure
6508  * @remove_entry: data struct which holds rule_id, VSI handle and recipe ID
6509  *
6510  * This function is used to remove 1 rule at a time. The removal is based on
6511  * the remove_entry parameter. This function will remove rule for a given
6512  * vsi_handle with a given rule_id which is passed as parameter in remove_entry
6513  */
6514 int
6515 ice_rem_adv_rule_by_id(struct ice_hw *hw,
6516 		       struct ice_rule_query_data *remove_entry)
6517 {
6518 	struct ice_adv_fltr_mgmt_list_entry *list_itr;
6519 	struct list_head *list_head;
6520 	struct ice_adv_rule_info rinfo;
6521 	struct ice_switch_info *sw;
6522 
6523 	sw = hw->switch_info;
6524 	if (!sw->recp_list[remove_entry->rid].recp_created)
6525 		return -EINVAL;
6526 	list_head = &sw->recp_list[remove_entry->rid].filt_rules;
6527 	list_for_each_entry(list_itr, list_head, list_entry) {
6528 		if (list_itr->rule_info.fltr_rule_id ==
6529 		    remove_entry->rule_id) {
6530 			rinfo = list_itr->rule_info;
6531 			rinfo.sw_act.vsi_handle = remove_entry->vsi_handle;
6532 			return ice_rem_adv_rule(hw, list_itr->lkups,
6533 						list_itr->lkups_cnt, &rinfo);
6534 		}
6535 	}
6536 	/* either list is empty or unable to find rule */
6537 	return -ENOENT;
6538 }
6539 
6540 /**
6541  * ice_replay_vsi_adv_rule - Replay advanced rule for requested VSI
6542  * @hw: pointer to the hardware structure
6543  * @vsi_handle: driver VSI handle
6544  * @list_head: list for which filters need to be replayed
6545  *
6546  * Replay the advanced rule for the given VSI.
6547  */
6548 static int
6549 ice_replay_vsi_adv_rule(struct ice_hw *hw, u16 vsi_handle,
6550 			struct list_head *list_head)
6551 {
6552 	struct ice_rule_query_data added_entry = { 0 };
6553 	struct ice_adv_fltr_mgmt_list_entry *adv_fltr;
6554 	int status = 0;
6555 
6556 	if (list_empty(list_head))
6557 		return status;
6558 	list_for_each_entry(adv_fltr, list_head, list_entry) {
6559 		struct ice_adv_rule_info *rinfo = &adv_fltr->rule_info;
6560 		u16 lk_cnt = adv_fltr->lkups_cnt;
6561 
6562 		if (vsi_handle != rinfo->sw_act.vsi_handle)
6563 			continue;
6564 		status = ice_add_adv_rule(hw, adv_fltr->lkups, lk_cnt, rinfo,
6565 					  &added_entry);
6566 		if (status)
6567 			break;
6568 	}
6569 	return status;
6570 }
6571 
6572 /**
6573  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
6574  * @hw: pointer to the hardware structure
6575  * @vsi_handle: driver VSI handle
6576  *
6577  * Replays filters for requested VSI via vsi_handle.
6578  */
6579 int ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
6580 {
6581 	struct ice_switch_info *sw = hw->switch_info;
6582 	int status;
6583 	u8 i;
6584 
6585 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6586 		struct list_head *head;
6587 
6588 		head = &sw->recp_list[i].filt_replay_rules;
6589 		if (!sw->recp_list[i].adv_rule)
6590 			status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
6591 		else
6592 			status = ice_replay_vsi_adv_rule(hw, vsi_handle, head);
6593 		if (status)
6594 			return status;
6595 	}
6596 	return status;
6597 }
6598 
6599 /**
6600  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
6601  * @hw: pointer to the HW struct
6602  *
6603  * Deletes the filter replay rules.
6604  */
6605 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
6606 {
6607 	struct ice_switch_info *sw = hw->switch_info;
6608 	u8 i;
6609 
6610 	if (!sw)
6611 		return;
6612 
6613 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
6614 		if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
6615 			struct list_head *l_head;
6616 
6617 			l_head = &sw->recp_list[i].filt_replay_rules;
6618 			if (!sw->recp_list[i].adv_rule)
6619 				ice_rem_sw_rule_info(hw, l_head);
6620 			else
6621 				ice_rem_adv_rule_info(hw, l_head);
6622 		}
6623 	}
6624 }
6625