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