xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_switch.c (revision a1c7c49c2091926962f8c1c866d386febffec5d8)
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 #define DUMMY_ETH_HDR_LEN		16
29 static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
30 							0x2, 0, 0, 0, 0, 0,
31 							0x81, 0, 0, 0};
32 
33 struct ice_dummy_pkt_offsets {
34 	enum ice_protocol_type type;
35 	u16 offset; /* ICE_PROTOCOL_LAST indicates end of list */
36 };
37 
38 static const struct ice_dummy_pkt_offsets dummy_gre_tcp_packet_offsets[] = {
39 	{ ICE_MAC_OFOS,		0 },
40 	{ ICE_ETYPE_OL,		12 },
41 	{ ICE_IPV4_OFOS,	14 },
42 	{ ICE_NVGRE,		34 },
43 	{ ICE_MAC_IL,		42 },
44 	{ ICE_IPV4_IL,		56 },
45 	{ ICE_TCP_IL,		76 },
46 	{ ICE_PROTOCOL_LAST,	0 },
47 };
48 
49 static const u8 dummy_gre_tcp_packet[] = {
50 	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_OFOS 0 */
51 	0x00, 0x00, 0x00, 0x00,
52 	0x00, 0x00, 0x00, 0x00,
53 
54 	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
55 
56 	0x45, 0x00, 0x00, 0x3E,	/* ICE_IPV4_OFOS 14 */
57 	0x00, 0x00, 0x00, 0x00,
58 	0x00, 0x2F, 0x00, 0x00,
59 	0x00, 0x00, 0x00, 0x00,
60 	0x00, 0x00, 0x00, 0x00,
61 
62 	0x80, 0x00, 0x65, 0x58,	/* ICE_NVGRE 34 */
63 	0x00, 0x00, 0x00, 0x00,
64 
65 	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_IL 42 */
66 	0x00, 0x00, 0x00, 0x00,
67 	0x00, 0x00, 0x00, 0x00,
68 	0x08, 0x00,
69 
70 	0x45, 0x00, 0x00, 0x14,	/* ICE_IPV4_IL 56 */
71 	0x00, 0x00, 0x00, 0x00,
72 	0x00, 0x06, 0x00, 0x00,
73 	0x00, 0x00, 0x00, 0x00,
74 	0x00, 0x00, 0x00, 0x00,
75 
76 	0x00, 0x00, 0x00, 0x00,	/* ICE_TCP_IL 76 */
77 	0x00, 0x00, 0x00, 0x00,
78 	0x00, 0x00, 0x00, 0x00,
79 	0x50, 0x02, 0x20, 0x00,
80 	0x00, 0x00, 0x00, 0x00
81 };
82 
83 static const struct ice_dummy_pkt_offsets dummy_gre_udp_packet_offsets[] = {
84 	{ ICE_MAC_OFOS,		0 },
85 	{ ICE_ETYPE_OL,		12 },
86 	{ ICE_IPV4_OFOS,	14 },
87 	{ ICE_NVGRE,		34 },
88 	{ ICE_MAC_IL,		42 },
89 	{ ICE_IPV4_IL,		56 },
90 	{ ICE_UDP_ILOS,		76 },
91 	{ ICE_PROTOCOL_LAST,	0 },
92 };
93 
94 static const u8 dummy_gre_udp_packet[] = {
95 	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_OFOS 0 */
96 	0x00, 0x00, 0x00, 0x00,
97 	0x00, 0x00, 0x00, 0x00,
98 
99 	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
100 
101 	0x45, 0x00, 0x00, 0x3E,	/* ICE_IPV4_OFOS 14 */
102 	0x00, 0x00, 0x00, 0x00,
103 	0x00, 0x2F, 0x00, 0x00,
104 	0x00, 0x00, 0x00, 0x00,
105 	0x00, 0x00, 0x00, 0x00,
106 
107 	0x80, 0x00, 0x65, 0x58,	/* ICE_NVGRE 34 */
108 	0x00, 0x00, 0x00, 0x00,
109 
110 	0x00, 0x00, 0x00, 0x00,	/* ICE_MAC_IL 42 */
111 	0x00, 0x00, 0x00, 0x00,
112 	0x00, 0x00, 0x00, 0x00,
113 	0x08, 0x00,
114 
115 	0x45, 0x00, 0x00, 0x14,	/* ICE_IPV4_IL 56 */
116 	0x00, 0x00, 0x00, 0x00,
117 	0x00, 0x11, 0x00, 0x00,
118 	0x00, 0x00, 0x00, 0x00,
119 	0x00, 0x00, 0x00, 0x00,
120 
121 	0x00, 0x00, 0x00, 0x00,	/* ICE_UDP_ILOS 76 */
122 	0x00, 0x08, 0x00, 0x00,
123 };
124 
125 static const struct ice_dummy_pkt_offsets dummy_udp_tun_tcp_packet_offsets[] = {
126 	{ ICE_MAC_OFOS,		0 },
127 	{ ICE_ETYPE_OL,		12 },
128 	{ ICE_IPV4_OFOS,	14 },
129 	{ ICE_UDP_OF,		34 },
130 	{ ICE_VXLAN,		42 },
131 	{ ICE_GENEVE,		42 },
132 	{ ICE_VXLAN_GPE,	42 },
133 	{ ICE_MAC_IL,		50 },
134 	{ ICE_IPV4_IL,		64 },
135 	{ ICE_TCP_IL,		84 },
136 	{ ICE_PROTOCOL_LAST,	0 },
137 };
138 
139 static const u8 dummy_udp_tun_tcp_packet[] = {
140 	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
141 	0x00, 0x00, 0x00, 0x00,
142 	0x00, 0x00, 0x00, 0x00,
143 
144 	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
145 
146 	0x45, 0x00, 0x00, 0x5a, /* ICE_IPV4_OFOS 14 */
147 	0x00, 0x01, 0x00, 0x00,
148 	0x40, 0x11, 0x00, 0x00,
149 	0x00, 0x00, 0x00, 0x00,
150 	0x00, 0x00, 0x00, 0x00,
151 
152 	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
153 	0x00, 0x46, 0x00, 0x00,
154 
155 	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
156 	0x00, 0x00, 0x00, 0x00,
157 
158 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
159 	0x00, 0x00, 0x00, 0x00,
160 	0x00, 0x00, 0x00, 0x00,
161 	0x08, 0x00,
162 
163 	0x45, 0x00, 0x00, 0x28, /* ICE_IPV4_IL 64 */
164 	0x00, 0x01, 0x00, 0x00,
165 	0x40, 0x06, 0x00, 0x00,
166 	0x00, 0x00, 0x00, 0x00,
167 	0x00, 0x00, 0x00, 0x00,
168 
169 	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 84 */
170 	0x00, 0x00, 0x00, 0x00,
171 	0x00, 0x00, 0x00, 0x00,
172 	0x50, 0x02, 0x20, 0x00,
173 	0x00, 0x00, 0x00, 0x00
174 };
175 
176 static const struct ice_dummy_pkt_offsets dummy_udp_tun_udp_packet_offsets[] = {
177 	{ ICE_MAC_OFOS,		0 },
178 	{ ICE_ETYPE_OL,		12 },
179 	{ ICE_IPV4_OFOS,	14 },
180 	{ ICE_UDP_OF,		34 },
181 	{ ICE_VXLAN,		42 },
182 	{ ICE_GENEVE,		42 },
183 	{ ICE_VXLAN_GPE,	42 },
184 	{ ICE_MAC_IL,		50 },
185 	{ ICE_IPV4_IL,		64 },
186 	{ ICE_UDP_ILOS,		84 },
187 	{ ICE_PROTOCOL_LAST,	0 },
188 };
189 
190 static const u8 dummy_udp_tun_udp_packet[] = {
191 	0x00, 0x00, 0x00, 0x00,  /* ICE_MAC_OFOS 0 */
192 	0x00, 0x00, 0x00, 0x00,
193 	0x00, 0x00, 0x00, 0x00,
194 
195 	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
196 
197 	0x45, 0x00, 0x00, 0x4e, /* ICE_IPV4_OFOS 14 */
198 	0x00, 0x01, 0x00, 0x00,
199 	0x00, 0x11, 0x00, 0x00,
200 	0x00, 0x00, 0x00, 0x00,
201 	0x00, 0x00, 0x00, 0x00,
202 
203 	0x00, 0x00, 0x12, 0xb5, /* ICE_UDP_OF 34 */
204 	0x00, 0x3a, 0x00, 0x00,
205 
206 	0x00, 0x00, 0x65, 0x58, /* ICE_VXLAN 42 */
207 	0x00, 0x00, 0x00, 0x00,
208 
209 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_IL 50 */
210 	0x00, 0x00, 0x00, 0x00,
211 	0x00, 0x00, 0x00, 0x00,
212 	0x08, 0x00,
213 
214 	0x45, 0x00, 0x00, 0x1c, /* ICE_IPV4_IL 64 */
215 	0x00, 0x01, 0x00, 0x00,
216 	0x00, 0x11, 0x00, 0x00,
217 	0x00, 0x00, 0x00, 0x00,
218 	0x00, 0x00, 0x00, 0x00,
219 
220 	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 84 */
221 	0x00, 0x08, 0x00, 0x00,
222 };
223 
224 /* offset info for MAC + IPv4 + UDP dummy packet */
225 static const struct ice_dummy_pkt_offsets dummy_udp_packet_offsets[] = {
226 	{ ICE_MAC_OFOS,		0 },
227 	{ ICE_ETYPE_OL,		12 },
228 	{ ICE_IPV4_OFOS,	14 },
229 	{ ICE_UDP_ILOS,		34 },
230 	{ ICE_PROTOCOL_LAST,	0 },
231 };
232 
233 /* Dummy packet for MAC + IPv4 + UDP */
234 static const u8 dummy_udp_packet[] = {
235 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
236 	0x00, 0x00, 0x00, 0x00,
237 	0x00, 0x00, 0x00, 0x00,
238 
239 	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
240 
241 	0x45, 0x00, 0x00, 0x1c, /* ICE_IPV4_OFOS 14 */
242 	0x00, 0x01, 0x00, 0x00,
243 	0x00, 0x11, 0x00, 0x00,
244 	0x00, 0x00, 0x00, 0x00,
245 	0x00, 0x00, 0x00, 0x00,
246 
247 	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 34 */
248 	0x00, 0x08, 0x00, 0x00,
249 
250 	0x00, 0x00,	/* 2 bytes for 4 byte alignment */
251 };
252 
253 /* offset info for MAC + VLAN + IPv4 + UDP dummy packet */
254 static const struct ice_dummy_pkt_offsets dummy_vlan_udp_packet_offsets[] = {
255 	{ ICE_MAC_OFOS,		0 },
256 	{ ICE_VLAN_OFOS,	12 },
257 	{ ICE_ETYPE_OL,		16 },
258 	{ ICE_IPV4_OFOS,	18 },
259 	{ ICE_UDP_ILOS,		38 },
260 	{ ICE_PROTOCOL_LAST,	0 },
261 };
262 
263 /* C-tag (801.1Q), IPv4:UDP dummy packet */
264 static const u8 dummy_vlan_udp_packet[] = {
265 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
266 	0x00, 0x00, 0x00, 0x00,
267 	0x00, 0x00, 0x00, 0x00,
268 
269 	0x81, 0x00, 0x00, 0x00, /* ICE_VLAN_OFOS 12 */
270 
271 	0x08, 0x00,		/* ICE_ETYPE_OL 16 */
272 
273 	0x45, 0x00, 0x00, 0x1c, /* ICE_IPV4_OFOS 18 */
274 	0x00, 0x01, 0x00, 0x00,
275 	0x00, 0x11, 0x00, 0x00,
276 	0x00, 0x00, 0x00, 0x00,
277 	0x00, 0x00, 0x00, 0x00,
278 
279 	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 38 */
280 	0x00, 0x08, 0x00, 0x00,
281 
282 	0x00, 0x00,	/* 2 bytes for 4 byte alignment */
283 };
284 
285 /* offset info for MAC + IPv4 + TCP dummy packet */
286 static const struct ice_dummy_pkt_offsets dummy_tcp_packet_offsets[] = {
287 	{ ICE_MAC_OFOS,		0 },
288 	{ ICE_ETYPE_OL,		12 },
289 	{ ICE_IPV4_OFOS,	14 },
290 	{ ICE_TCP_IL,		34 },
291 	{ ICE_PROTOCOL_LAST,	0 },
292 };
293 
294 /* Dummy packet for MAC + IPv4 + TCP */
295 static const u8 dummy_tcp_packet[] = {
296 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
297 	0x00, 0x00, 0x00, 0x00,
298 	0x00, 0x00, 0x00, 0x00,
299 
300 	0x08, 0x00,		/* ICE_ETYPE_OL 12 */
301 
302 	0x45, 0x00, 0x00, 0x28, /* ICE_IPV4_OFOS 14 */
303 	0x00, 0x01, 0x00, 0x00,
304 	0x00, 0x06, 0x00, 0x00,
305 	0x00, 0x00, 0x00, 0x00,
306 	0x00, 0x00, 0x00, 0x00,
307 
308 	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 34 */
309 	0x00, 0x00, 0x00, 0x00,
310 	0x00, 0x00, 0x00, 0x00,
311 	0x50, 0x00, 0x00, 0x00,
312 	0x00, 0x00, 0x00, 0x00,
313 
314 	0x00, 0x00,	/* 2 bytes for 4 byte alignment */
315 };
316 
317 /* offset info for MAC + VLAN (C-tag, 802.1Q) + IPv4 + TCP dummy packet */
318 static const struct ice_dummy_pkt_offsets dummy_vlan_tcp_packet_offsets[] = {
319 	{ ICE_MAC_OFOS,		0 },
320 	{ ICE_VLAN_OFOS,	12 },
321 	{ ICE_ETYPE_OL,		16 },
322 	{ ICE_IPV4_OFOS,	18 },
323 	{ ICE_TCP_IL,		38 },
324 	{ ICE_PROTOCOL_LAST,	0 },
325 };
326 
327 /* C-tag (801.1Q), IPv4:TCP dummy packet */
328 static const u8 dummy_vlan_tcp_packet[] = {
329 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
330 	0x00, 0x00, 0x00, 0x00,
331 	0x00, 0x00, 0x00, 0x00,
332 
333 	0x81, 0x00, 0x00, 0x00,	/* ICE_VLAN_OFOS 12 */
334 
335 	0x08, 0x00,		/* ICE_ETYPE_OL 16 */
336 
337 	0x45, 0x00, 0x00, 0x28, /* ICE_IPV4_OFOS 18 */
338 	0x00, 0x01, 0x00, 0x00,
339 	0x00, 0x06, 0x00, 0x00,
340 	0x00, 0x00, 0x00, 0x00,
341 	0x00, 0x00, 0x00, 0x00,
342 
343 	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 38 */
344 	0x00, 0x00, 0x00, 0x00,
345 	0x00, 0x00, 0x00, 0x00,
346 	0x50, 0x00, 0x00, 0x00,
347 	0x00, 0x00, 0x00, 0x00,
348 
349 	0x00, 0x00,	/* 2 bytes for 4 byte alignment */
350 };
351 
352 static const struct ice_dummy_pkt_offsets dummy_tcp_ipv6_packet_offsets[] = {
353 	{ ICE_MAC_OFOS,		0 },
354 	{ ICE_ETYPE_OL,		12 },
355 	{ ICE_IPV6_OFOS,	14 },
356 	{ ICE_TCP_IL,		54 },
357 	{ ICE_PROTOCOL_LAST,	0 },
358 };
359 
360 static const u8 dummy_tcp_ipv6_packet[] = {
361 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
362 	0x00, 0x00, 0x00, 0x00,
363 	0x00, 0x00, 0x00, 0x00,
364 
365 	0x86, 0xDD,		/* ICE_ETYPE_OL 12 */
366 
367 	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 40 */
368 	0x00, 0x14, 0x06, 0x00, /* Next header is TCP */
369 	0x00, 0x00, 0x00, 0x00,
370 	0x00, 0x00, 0x00, 0x00,
371 	0x00, 0x00, 0x00, 0x00,
372 	0x00, 0x00, 0x00, 0x00,
373 	0x00, 0x00, 0x00, 0x00,
374 	0x00, 0x00, 0x00, 0x00,
375 	0x00, 0x00, 0x00, 0x00,
376 	0x00, 0x00, 0x00, 0x00,
377 
378 	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 54 */
379 	0x00, 0x00, 0x00, 0x00,
380 	0x00, 0x00, 0x00, 0x00,
381 	0x50, 0x00, 0x00, 0x00,
382 	0x00, 0x00, 0x00, 0x00,
383 
384 	0x00, 0x00, /* 2 bytes for 4 byte alignment */
385 };
386 
387 /* C-tag (802.1Q): IPv6 + TCP */
388 static const struct ice_dummy_pkt_offsets
389 dummy_vlan_tcp_ipv6_packet_offsets[] = {
390 	{ ICE_MAC_OFOS,		0 },
391 	{ ICE_VLAN_OFOS,	12 },
392 	{ ICE_ETYPE_OL,		16 },
393 	{ ICE_IPV6_OFOS,	18 },
394 	{ ICE_TCP_IL,		58 },
395 	{ ICE_PROTOCOL_LAST,	0 },
396 };
397 
398 /* C-tag (802.1Q), IPv6 + TCP dummy packet */
399 static const u8 dummy_vlan_tcp_ipv6_packet[] = {
400 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
401 	0x00, 0x00, 0x00, 0x00,
402 	0x00, 0x00, 0x00, 0x00,
403 
404 	0x81, 0x00, 0x00, 0x00,	/* ICE_VLAN_OFOS 12 */
405 
406 	0x86, 0xDD,		/* ICE_ETYPE_OL 16 */
407 
408 	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 18 */
409 	0x00, 0x14, 0x06, 0x00, /* Next header is TCP */
410 	0x00, 0x00, 0x00, 0x00,
411 	0x00, 0x00, 0x00, 0x00,
412 	0x00, 0x00, 0x00, 0x00,
413 	0x00, 0x00, 0x00, 0x00,
414 	0x00, 0x00, 0x00, 0x00,
415 	0x00, 0x00, 0x00, 0x00,
416 	0x00, 0x00, 0x00, 0x00,
417 	0x00, 0x00, 0x00, 0x00,
418 
419 	0x00, 0x00, 0x00, 0x00, /* ICE_TCP_IL 58 */
420 	0x00, 0x00, 0x00, 0x00,
421 	0x00, 0x00, 0x00, 0x00,
422 	0x50, 0x00, 0x00, 0x00,
423 	0x00, 0x00, 0x00, 0x00,
424 
425 	0x00, 0x00, /* 2 bytes for 4 byte alignment */
426 };
427 
428 /* IPv6 + UDP */
429 static const struct ice_dummy_pkt_offsets dummy_udp_ipv6_packet_offsets[] = {
430 	{ ICE_MAC_OFOS,		0 },
431 	{ ICE_ETYPE_OL,		12 },
432 	{ ICE_IPV6_OFOS,	14 },
433 	{ ICE_UDP_ILOS,		54 },
434 	{ ICE_PROTOCOL_LAST,	0 },
435 };
436 
437 /* IPv6 + UDP dummy packet */
438 static const u8 dummy_udp_ipv6_packet[] = {
439 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
440 	0x00, 0x00, 0x00, 0x00,
441 	0x00, 0x00, 0x00, 0x00,
442 
443 	0x86, 0xDD,		/* ICE_ETYPE_OL 12 */
444 
445 	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 40 */
446 	0x00, 0x10, 0x11, 0x00, /* Next header UDP */
447 	0x00, 0x00, 0x00, 0x00,
448 	0x00, 0x00, 0x00, 0x00,
449 	0x00, 0x00, 0x00, 0x00,
450 	0x00, 0x00, 0x00, 0x00,
451 	0x00, 0x00, 0x00, 0x00,
452 	0x00, 0x00, 0x00, 0x00,
453 	0x00, 0x00, 0x00, 0x00,
454 	0x00, 0x00, 0x00, 0x00,
455 
456 	0x00, 0x00, 0x00, 0x00, /* ICE_UDP_ILOS 54 */
457 	0x00, 0x10, 0x00, 0x00,
458 
459 	0x00, 0x00, 0x00, 0x00, /* needed for ESP packets */
460 	0x00, 0x00, 0x00, 0x00,
461 
462 	0x00, 0x00, /* 2 bytes for 4 byte alignment */
463 };
464 
465 /* C-tag (802.1Q): IPv6 + UDP */
466 static const struct ice_dummy_pkt_offsets
467 dummy_vlan_udp_ipv6_packet_offsets[] = {
468 	{ ICE_MAC_OFOS,		0 },
469 	{ ICE_VLAN_OFOS,	12 },
470 	{ ICE_ETYPE_OL,		16 },
471 	{ ICE_IPV6_OFOS,	18 },
472 	{ ICE_UDP_ILOS,		58 },
473 	{ ICE_PROTOCOL_LAST,	0 },
474 };
475 
476 /* C-tag (802.1Q), IPv6 + UDP dummy packet */
477 static const u8 dummy_vlan_udp_ipv6_packet[] = {
478 	0x00, 0x00, 0x00, 0x00, /* ICE_MAC_OFOS 0 */
479 	0x00, 0x00, 0x00, 0x00,
480 	0x00, 0x00, 0x00, 0x00,
481 
482 	0x81, 0x00, 0x00, 0x00,/* ICE_VLAN_OFOS 12 */
483 
484 	0x86, 0xDD,		/* ICE_ETYPE_OL 16 */
485 
486 	0x60, 0x00, 0x00, 0x00, /* ICE_IPV6_OFOS 18 */
487 	0x00, 0x08, 0x11, 0x00, /* Next header UDP */
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 58 */
498 	0x00, 0x08, 0x00, 0x00,
499 
500 	0x00, 0x00, /* 2 bytes for 4 byte alignment */
501 };
502 
503 #define ICE_SW_RULE_RX_TX_ETH_HDR_SIZE \
504 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.lkup_tx_rx.hdr) + \
505 	 (DUMMY_ETH_HDR_LEN * \
506 	  sizeof(((struct ice_sw_rule_lkup_rx_tx *)0)->hdr[0])))
507 #define ICE_SW_RULE_RX_TX_NO_HDR_SIZE \
508 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.lkup_tx_rx.hdr))
509 #define ICE_SW_RULE_LG_ACT_SIZE(n) \
510 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.lg_act.act) + \
511 	 ((n) * sizeof(((struct ice_sw_rule_lg_act *)0)->act[0])))
512 #define ICE_SW_RULE_VSI_LIST_SIZE(n) \
513 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.vsi_list.vsi) + \
514 	 ((n) * sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi[0])))
515 
516 /* this is a recipe to profile association bitmap */
517 static DECLARE_BITMAP(recipe_to_profile[ICE_MAX_NUM_RECIPES],
518 			  ICE_MAX_NUM_PROFILES);
519 
520 /* this is a profile to recipe association bitmap */
521 static DECLARE_BITMAP(profile_to_recipe[ICE_MAX_NUM_PROFILES],
522 			  ICE_MAX_NUM_RECIPES);
523 
524 /**
525  * ice_init_def_sw_recp - initialize the recipe book keeping tables
526  * @hw: pointer to the HW struct
527  *
528  * Allocate memory for the entire recipe table and initialize the structures/
529  * entries corresponding to basic recipes.
530  */
531 enum ice_status ice_init_def_sw_recp(struct ice_hw *hw)
532 {
533 	struct ice_sw_recipe *recps;
534 	u8 i;
535 
536 	recps = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_NUM_RECIPES,
537 			     sizeof(*recps), GFP_KERNEL);
538 	if (!recps)
539 		return ICE_ERR_NO_MEMORY;
540 
541 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
542 		recps[i].root_rid = i;
543 		INIT_LIST_HEAD(&recps[i].filt_rules);
544 		INIT_LIST_HEAD(&recps[i].filt_replay_rules);
545 		INIT_LIST_HEAD(&recps[i].rg_list);
546 		mutex_init(&recps[i].filt_rule_lock);
547 	}
548 
549 	hw->switch_info->recp_list = recps;
550 
551 	return 0;
552 }
553 
554 /**
555  * ice_aq_get_sw_cfg - get switch configuration
556  * @hw: pointer to the hardware structure
557  * @buf: pointer to the result buffer
558  * @buf_size: length of the buffer available for response
559  * @req_desc: pointer to requested descriptor
560  * @num_elems: pointer to number of elements
561  * @cd: pointer to command details structure or NULL
562  *
563  * Get switch configuration (0x0200) to be placed in buf.
564  * This admin command returns information such as initial VSI/port number
565  * and switch ID it belongs to.
566  *
567  * NOTE: *req_desc is both an input/output parameter.
568  * The caller of this function first calls this function with *request_desc set
569  * to 0. If the response from f/w has *req_desc set to 0, all the switch
570  * configuration information has been returned; if non-zero (meaning not all
571  * the information was returned), the caller should call this function again
572  * with *req_desc set to the previous value returned by f/w to get the
573  * next block of switch configuration information.
574  *
575  * *num_elems is output only parameter. This reflects the number of elements
576  * in response buffer. The caller of this function to use *num_elems while
577  * parsing the response buffer.
578  */
579 static enum ice_status
580 ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp_elem *buf,
581 		  u16 buf_size, u16 *req_desc, u16 *num_elems,
582 		  struct ice_sq_cd *cd)
583 {
584 	struct ice_aqc_get_sw_cfg *cmd;
585 	struct ice_aq_desc desc;
586 	enum ice_status status;
587 
588 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
589 	cmd = &desc.params.get_sw_conf;
590 	cmd->element = cpu_to_le16(*req_desc);
591 
592 	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
593 	if (!status) {
594 		*req_desc = le16_to_cpu(cmd->element);
595 		*num_elems = le16_to_cpu(cmd->num_elems);
596 	}
597 
598 	return status;
599 }
600 
601 /**
602  * ice_aq_add_vsi
603  * @hw: pointer to the HW struct
604  * @vsi_ctx: pointer to a VSI context struct
605  * @cd: pointer to command details structure or NULL
606  *
607  * Add a VSI context to the hardware (0x0210)
608  */
609 static enum ice_status
610 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
611 	       struct ice_sq_cd *cd)
612 {
613 	struct ice_aqc_add_update_free_vsi_resp *res;
614 	struct ice_aqc_add_get_update_free_vsi *cmd;
615 	struct ice_aq_desc desc;
616 	enum ice_status status;
617 
618 	cmd = &desc.params.vsi_cmd;
619 	res = &desc.params.add_update_free_vsi_res;
620 
621 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
622 
623 	if (!vsi_ctx->alloc_from_pool)
624 		cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num |
625 					   ICE_AQ_VSI_IS_VALID);
626 	cmd->vf_id = vsi_ctx->vf_num;
627 
628 	cmd->vsi_flags = cpu_to_le16(vsi_ctx->flags);
629 
630 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
631 
632 	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
633 				 sizeof(vsi_ctx->info), cd);
634 
635 	if (!status) {
636 		vsi_ctx->vsi_num = le16_to_cpu(res->vsi_num) & ICE_AQ_VSI_NUM_M;
637 		vsi_ctx->vsis_allocd = le16_to_cpu(res->vsi_used);
638 		vsi_ctx->vsis_unallocated = le16_to_cpu(res->vsi_free);
639 	}
640 
641 	return status;
642 }
643 
644 /**
645  * ice_aq_free_vsi
646  * @hw: pointer to the HW struct
647  * @vsi_ctx: pointer to a VSI context struct
648  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
649  * @cd: pointer to command details structure or NULL
650  *
651  * Free VSI context info from hardware (0x0213)
652  */
653 static enum ice_status
654 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
655 		bool keep_vsi_alloc, struct ice_sq_cd *cd)
656 {
657 	struct ice_aqc_add_update_free_vsi_resp *resp;
658 	struct ice_aqc_add_get_update_free_vsi *cmd;
659 	struct ice_aq_desc desc;
660 	enum ice_status status;
661 
662 	cmd = &desc.params.vsi_cmd;
663 	resp = &desc.params.add_update_free_vsi_res;
664 
665 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
666 
667 	cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
668 	if (keep_vsi_alloc)
669 		cmd->cmd_flags = cpu_to_le16(ICE_AQ_VSI_KEEP_ALLOC);
670 
671 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
672 	if (!status) {
673 		vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
674 		vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
675 	}
676 
677 	return status;
678 }
679 
680 /**
681  * ice_aq_update_vsi
682  * @hw: pointer to the HW struct
683  * @vsi_ctx: pointer to a VSI context struct
684  * @cd: pointer to command details structure or NULL
685  *
686  * Update VSI context in the hardware (0x0211)
687  */
688 static enum ice_status
689 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
690 		  struct ice_sq_cd *cd)
691 {
692 	struct ice_aqc_add_update_free_vsi_resp *resp;
693 	struct ice_aqc_add_get_update_free_vsi *cmd;
694 	struct ice_aq_desc desc;
695 	enum ice_status status;
696 
697 	cmd = &desc.params.vsi_cmd;
698 	resp = &desc.params.add_update_free_vsi_res;
699 
700 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
701 
702 	cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
703 
704 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
705 
706 	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
707 				 sizeof(vsi_ctx->info), cd);
708 
709 	if (!status) {
710 		vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
711 		vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
712 	}
713 
714 	return status;
715 }
716 
717 /**
718  * ice_is_vsi_valid - check whether the VSI is valid or not
719  * @hw: pointer to the HW struct
720  * @vsi_handle: VSI handle
721  *
722  * check whether the VSI is valid or not
723  */
724 bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
725 {
726 	return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
727 }
728 
729 /**
730  * ice_get_hw_vsi_num - return the HW VSI number
731  * @hw: pointer to the HW struct
732  * @vsi_handle: VSI handle
733  *
734  * return the HW VSI number
735  * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
736  */
737 u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
738 {
739 	return hw->vsi_ctx[vsi_handle]->vsi_num;
740 }
741 
742 /**
743  * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
744  * @hw: pointer to the HW struct
745  * @vsi_handle: VSI handle
746  *
747  * return the VSI context entry for a given VSI handle
748  */
749 struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
750 {
751 	return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
752 }
753 
754 /**
755  * ice_save_vsi_ctx - save the VSI context for a given VSI handle
756  * @hw: pointer to the HW struct
757  * @vsi_handle: VSI handle
758  * @vsi: VSI context pointer
759  *
760  * save the VSI context entry for a given VSI handle
761  */
762 static void
763 ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi)
764 {
765 	hw->vsi_ctx[vsi_handle] = vsi;
766 }
767 
768 /**
769  * ice_clear_vsi_q_ctx - clear VSI queue contexts for all TCs
770  * @hw: pointer to the HW struct
771  * @vsi_handle: VSI handle
772  */
773 static void ice_clear_vsi_q_ctx(struct ice_hw *hw, u16 vsi_handle)
774 {
775 	struct ice_vsi_ctx *vsi;
776 	u8 i;
777 
778 	vsi = ice_get_vsi_ctx(hw, vsi_handle);
779 	if (!vsi)
780 		return;
781 	ice_for_each_traffic_class(i) {
782 		if (vsi->lan_q_ctx[i]) {
783 			devm_kfree(ice_hw_to_dev(hw), vsi->lan_q_ctx[i]);
784 			vsi->lan_q_ctx[i] = NULL;
785 		}
786 		if (vsi->rdma_q_ctx[i]) {
787 			devm_kfree(ice_hw_to_dev(hw), vsi->rdma_q_ctx[i]);
788 			vsi->rdma_q_ctx[i] = NULL;
789 		}
790 	}
791 }
792 
793 /**
794  * ice_clear_vsi_ctx - clear the VSI context entry
795  * @hw: pointer to the HW struct
796  * @vsi_handle: VSI handle
797  *
798  * clear the VSI context entry
799  */
800 static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
801 {
802 	struct ice_vsi_ctx *vsi;
803 
804 	vsi = ice_get_vsi_ctx(hw, vsi_handle);
805 	if (vsi) {
806 		ice_clear_vsi_q_ctx(hw, vsi_handle);
807 		devm_kfree(ice_hw_to_dev(hw), vsi);
808 		hw->vsi_ctx[vsi_handle] = NULL;
809 	}
810 }
811 
812 /**
813  * ice_clear_all_vsi_ctx - clear all the VSI context entries
814  * @hw: pointer to the HW struct
815  */
816 void ice_clear_all_vsi_ctx(struct ice_hw *hw)
817 {
818 	u16 i;
819 
820 	for (i = 0; i < ICE_MAX_VSI; i++)
821 		ice_clear_vsi_ctx(hw, i);
822 }
823 
824 /**
825  * ice_add_vsi - add VSI context to the hardware and VSI handle list
826  * @hw: pointer to the HW struct
827  * @vsi_handle: unique VSI handle provided by drivers
828  * @vsi_ctx: pointer to a VSI context struct
829  * @cd: pointer to command details structure or NULL
830  *
831  * Add a VSI context to the hardware also add it into the VSI handle list.
832  * If this function gets called after reset for existing VSIs then update
833  * with the new HW VSI number in the corresponding VSI handle list entry.
834  */
835 enum ice_status
836 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
837 	    struct ice_sq_cd *cd)
838 {
839 	struct ice_vsi_ctx *tmp_vsi_ctx;
840 	enum ice_status status;
841 
842 	if (vsi_handle >= ICE_MAX_VSI)
843 		return ICE_ERR_PARAM;
844 	status = ice_aq_add_vsi(hw, vsi_ctx, cd);
845 	if (status)
846 		return status;
847 	tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
848 	if (!tmp_vsi_ctx) {
849 		/* Create a new VSI context */
850 		tmp_vsi_ctx = devm_kzalloc(ice_hw_to_dev(hw),
851 					   sizeof(*tmp_vsi_ctx), GFP_KERNEL);
852 		if (!tmp_vsi_ctx) {
853 			ice_aq_free_vsi(hw, vsi_ctx, false, cd);
854 			return ICE_ERR_NO_MEMORY;
855 		}
856 		*tmp_vsi_ctx = *vsi_ctx;
857 		ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
858 	} else {
859 		/* update with new HW VSI num */
860 		tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
861 	}
862 
863 	return 0;
864 }
865 
866 /**
867  * ice_free_vsi- free VSI context from hardware and VSI handle list
868  * @hw: pointer to the HW struct
869  * @vsi_handle: unique VSI handle
870  * @vsi_ctx: pointer to a VSI context struct
871  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
872  * @cd: pointer to command details structure or NULL
873  *
874  * Free VSI context info from hardware as well as from VSI handle list
875  */
876 enum ice_status
877 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
878 	     bool keep_vsi_alloc, struct ice_sq_cd *cd)
879 {
880 	enum ice_status status;
881 
882 	if (!ice_is_vsi_valid(hw, vsi_handle))
883 		return ICE_ERR_PARAM;
884 	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
885 	status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
886 	if (!status)
887 		ice_clear_vsi_ctx(hw, vsi_handle);
888 	return status;
889 }
890 
891 /**
892  * ice_update_vsi
893  * @hw: pointer to the HW struct
894  * @vsi_handle: unique VSI handle
895  * @vsi_ctx: pointer to a VSI context struct
896  * @cd: pointer to command details structure or NULL
897  *
898  * Update VSI context in the hardware
899  */
900 enum ice_status
901 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
902 	       struct ice_sq_cd *cd)
903 {
904 	if (!ice_is_vsi_valid(hw, vsi_handle))
905 		return ICE_ERR_PARAM;
906 	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
907 	return ice_aq_update_vsi(hw, vsi_ctx, cd);
908 }
909 
910 /**
911  * ice_cfg_rdma_fltr - enable/disable RDMA filtering on VSI
912  * @hw: pointer to HW struct
913  * @vsi_handle: VSI SW index
914  * @enable: boolean for enable/disable
915  */
916 int
917 ice_cfg_rdma_fltr(struct ice_hw *hw, u16 vsi_handle, bool enable)
918 {
919 	struct ice_vsi_ctx *ctx;
920 
921 	ctx = ice_get_vsi_ctx(hw, vsi_handle);
922 	if (!ctx)
923 		return -EIO;
924 
925 	if (enable)
926 		ctx->info.q_opt_flags |= ICE_AQ_VSI_Q_OPT_PE_FLTR_EN;
927 	else
928 		ctx->info.q_opt_flags &= ~ICE_AQ_VSI_Q_OPT_PE_FLTR_EN;
929 
930 	return ice_status_to_errno(ice_update_vsi(hw, vsi_handle, ctx, NULL));
931 }
932 
933 /**
934  * ice_aq_alloc_free_vsi_list
935  * @hw: pointer to the HW struct
936  * @vsi_list_id: VSI list ID returned or used for lookup
937  * @lkup_type: switch rule filter lookup type
938  * @opc: switch rules population command type - pass in the command opcode
939  *
940  * allocates or free a VSI list resource
941  */
942 static enum ice_status
943 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
944 			   enum ice_sw_lkup_type lkup_type,
945 			   enum ice_adminq_opc opc)
946 {
947 	struct ice_aqc_alloc_free_res_elem *sw_buf;
948 	struct ice_aqc_res_elem *vsi_ele;
949 	enum ice_status status;
950 	u16 buf_len;
951 
952 	buf_len = struct_size(sw_buf, elem, 1);
953 	sw_buf = devm_kzalloc(ice_hw_to_dev(hw), buf_len, GFP_KERNEL);
954 	if (!sw_buf)
955 		return ICE_ERR_NO_MEMORY;
956 	sw_buf->num_elems = cpu_to_le16(1);
957 
958 	if (lkup_type == ICE_SW_LKUP_MAC ||
959 	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
960 	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
961 	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
962 	    lkup_type == ICE_SW_LKUP_PROMISC ||
963 	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN) {
964 		sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
965 	} else if (lkup_type == ICE_SW_LKUP_VLAN) {
966 		sw_buf->res_type =
967 			cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
968 	} else {
969 		status = ICE_ERR_PARAM;
970 		goto ice_aq_alloc_free_vsi_list_exit;
971 	}
972 
973 	if (opc == ice_aqc_opc_free_res)
974 		sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
975 
976 	status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
977 	if (status)
978 		goto ice_aq_alloc_free_vsi_list_exit;
979 
980 	if (opc == ice_aqc_opc_alloc_res) {
981 		vsi_ele = &sw_buf->elem[0];
982 		*vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp);
983 	}
984 
985 ice_aq_alloc_free_vsi_list_exit:
986 	devm_kfree(ice_hw_to_dev(hw), sw_buf);
987 	return status;
988 }
989 
990 /**
991  * ice_aq_sw_rules - add/update/remove switch rules
992  * @hw: pointer to the HW struct
993  * @rule_list: pointer to switch rule population list
994  * @rule_list_sz: total size of the rule list in bytes
995  * @num_rules: number of switch rules in the rule_list
996  * @opc: switch rules population command type - pass in the command opcode
997  * @cd: pointer to command details structure or NULL
998  *
999  * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
1000  */
1001 enum ice_status
1002 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
1003 		u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
1004 {
1005 	struct ice_aq_desc desc;
1006 	enum ice_status status;
1007 
1008 	if (opc != ice_aqc_opc_add_sw_rules &&
1009 	    opc != ice_aqc_opc_update_sw_rules &&
1010 	    opc != ice_aqc_opc_remove_sw_rules)
1011 		return ICE_ERR_PARAM;
1012 
1013 	ice_fill_dflt_direct_cmd_desc(&desc, opc);
1014 
1015 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1016 	desc.params.sw_rules.num_rules_fltr_entry_index =
1017 		cpu_to_le16(num_rules);
1018 	status = ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
1019 	if (opc != ice_aqc_opc_add_sw_rules &&
1020 	    hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT)
1021 		status = ICE_ERR_DOES_NOT_EXIST;
1022 
1023 	return status;
1024 }
1025 
1026 /**
1027  * ice_aq_add_recipe - add switch recipe
1028  * @hw: pointer to the HW struct
1029  * @s_recipe_list: pointer to switch rule population list
1030  * @num_recipes: number of switch recipes in the list
1031  * @cd: pointer to command details structure or NULL
1032  *
1033  * Add(0x0290)
1034  */
1035 static enum ice_status
1036 ice_aq_add_recipe(struct ice_hw *hw,
1037 		  struct ice_aqc_recipe_data_elem *s_recipe_list,
1038 		  u16 num_recipes, struct ice_sq_cd *cd)
1039 {
1040 	struct ice_aqc_add_get_recipe *cmd;
1041 	struct ice_aq_desc desc;
1042 	u16 buf_size;
1043 
1044 	cmd = &desc.params.add_get_recipe;
1045 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_recipe);
1046 
1047 	cmd->num_sub_recipes = cpu_to_le16(num_recipes);
1048 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1049 
1050 	buf_size = num_recipes * sizeof(*s_recipe_list);
1051 
1052 	return ice_aq_send_cmd(hw, &desc, s_recipe_list, buf_size, cd);
1053 }
1054 
1055 /**
1056  * ice_aq_get_recipe - get switch recipe
1057  * @hw: pointer to the HW struct
1058  * @s_recipe_list: pointer to switch rule population list
1059  * @num_recipes: pointer to the number of recipes (input and output)
1060  * @recipe_root: root recipe number of recipe(s) to retrieve
1061  * @cd: pointer to command details structure or NULL
1062  *
1063  * Get(0x0292)
1064  *
1065  * On input, *num_recipes should equal the number of entries in s_recipe_list.
1066  * On output, *num_recipes will equal the number of entries returned in
1067  * s_recipe_list.
1068  *
1069  * The caller must supply enough space in s_recipe_list to hold all possible
1070  * recipes and *num_recipes must equal ICE_MAX_NUM_RECIPES.
1071  */
1072 static enum ice_status
1073 ice_aq_get_recipe(struct ice_hw *hw,
1074 		  struct ice_aqc_recipe_data_elem *s_recipe_list,
1075 		  u16 *num_recipes, u16 recipe_root, struct ice_sq_cd *cd)
1076 {
1077 	struct ice_aqc_add_get_recipe *cmd;
1078 	struct ice_aq_desc desc;
1079 	enum ice_status status;
1080 	u16 buf_size;
1081 
1082 	if (*num_recipes != ICE_MAX_NUM_RECIPES)
1083 		return ICE_ERR_PARAM;
1084 
1085 	cmd = &desc.params.add_get_recipe;
1086 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_recipe);
1087 
1088 	cmd->return_index = cpu_to_le16(recipe_root);
1089 	cmd->num_sub_recipes = 0;
1090 
1091 	buf_size = *num_recipes * sizeof(*s_recipe_list);
1092 
1093 	status = ice_aq_send_cmd(hw, &desc, s_recipe_list, buf_size, cd);
1094 	*num_recipes = le16_to_cpu(cmd->num_sub_recipes);
1095 
1096 	return status;
1097 }
1098 
1099 /**
1100  * ice_aq_map_recipe_to_profile - Map recipe to packet profile
1101  * @hw: pointer to the HW struct
1102  * @profile_id: package profile ID to associate the recipe with
1103  * @r_bitmap: Recipe bitmap filled in and need to be returned as response
1104  * @cd: pointer to command details structure or NULL
1105  * Recipe to profile association (0x0291)
1106  */
1107 static enum ice_status
1108 ice_aq_map_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u8 *r_bitmap,
1109 			     struct ice_sq_cd *cd)
1110 {
1111 	struct ice_aqc_recipe_to_profile *cmd;
1112 	struct ice_aq_desc desc;
1113 
1114 	cmd = &desc.params.recipe_to_profile;
1115 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_recipe_to_profile);
1116 	cmd->profile_id = cpu_to_le16(profile_id);
1117 	/* Set the recipe ID bit in the bitmask to let the device know which
1118 	 * profile we are associating the recipe to
1119 	 */
1120 	memcpy(cmd->recipe_assoc, r_bitmap, sizeof(cmd->recipe_assoc));
1121 
1122 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1123 }
1124 
1125 /**
1126  * ice_aq_get_recipe_to_profile - Map recipe to packet profile
1127  * @hw: pointer to the HW struct
1128  * @profile_id: package profile ID to associate the recipe with
1129  * @r_bitmap: Recipe bitmap filled in and need to be returned as response
1130  * @cd: pointer to command details structure or NULL
1131  * Associate profile ID with given recipe (0x0293)
1132  */
1133 static enum ice_status
1134 ice_aq_get_recipe_to_profile(struct ice_hw *hw, u32 profile_id, u8 *r_bitmap,
1135 			     struct ice_sq_cd *cd)
1136 {
1137 	struct ice_aqc_recipe_to_profile *cmd;
1138 	struct ice_aq_desc desc;
1139 	enum ice_status status;
1140 
1141 	cmd = &desc.params.recipe_to_profile;
1142 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_recipe_to_profile);
1143 	cmd->profile_id = cpu_to_le16(profile_id);
1144 
1145 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
1146 	if (!status)
1147 		memcpy(r_bitmap, cmd->recipe_assoc, sizeof(cmd->recipe_assoc));
1148 
1149 	return status;
1150 }
1151 
1152 /**
1153  * ice_alloc_recipe - add recipe resource
1154  * @hw: pointer to the hardware structure
1155  * @rid: recipe ID returned as response to AQ call
1156  */
1157 static enum ice_status ice_alloc_recipe(struct ice_hw *hw, u16 *rid)
1158 {
1159 	struct ice_aqc_alloc_free_res_elem *sw_buf;
1160 	enum ice_status status;
1161 	u16 buf_len;
1162 
1163 	buf_len = struct_size(sw_buf, elem, 1);
1164 	sw_buf = kzalloc(buf_len, GFP_KERNEL);
1165 	if (!sw_buf)
1166 		return ICE_ERR_NO_MEMORY;
1167 
1168 	sw_buf->num_elems = cpu_to_le16(1);
1169 	sw_buf->res_type = cpu_to_le16((ICE_AQC_RES_TYPE_RECIPE <<
1170 					ICE_AQC_RES_TYPE_S) |
1171 					ICE_AQC_RES_TYPE_FLAG_SHARED);
1172 	status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
1173 				       ice_aqc_opc_alloc_res, NULL);
1174 	if (!status)
1175 		*rid = le16_to_cpu(sw_buf->elem[0].e.sw_resp);
1176 	kfree(sw_buf);
1177 
1178 	return status;
1179 }
1180 
1181 /**
1182  * ice_get_recp_to_prof_map - updates recipe to profile mapping
1183  * @hw: pointer to hardware structure
1184  *
1185  * This function is used to populate recipe_to_profile matrix where index to
1186  * this array is the recipe ID and the element is the mapping of which profiles
1187  * is this recipe mapped to.
1188  */
1189 static void ice_get_recp_to_prof_map(struct ice_hw *hw)
1190 {
1191 	DECLARE_BITMAP(r_bitmap, ICE_MAX_NUM_RECIPES);
1192 	u16 i;
1193 
1194 	for (i = 0; i < hw->switch_info->max_used_prof_index + 1; i++) {
1195 		u16 j;
1196 
1197 		bitmap_zero(profile_to_recipe[i], ICE_MAX_NUM_RECIPES);
1198 		bitmap_zero(r_bitmap, ICE_MAX_NUM_RECIPES);
1199 		if (ice_aq_get_recipe_to_profile(hw, i, (u8 *)r_bitmap, NULL))
1200 			continue;
1201 		bitmap_copy(profile_to_recipe[i], r_bitmap,
1202 			    ICE_MAX_NUM_RECIPES);
1203 		for_each_set_bit(j, r_bitmap, ICE_MAX_NUM_RECIPES)
1204 			set_bit(i, recipe_to_profile[j]);
1205 	}
1206 }
1207 
1208 /**
1209  * ice_collect_result_idx - copy result index values
1210  * @buf: buffer that contains the result index
1211  * @recp: the recipe struct to copy data into
1212  */
1213 static void
1214 ice_collect_result_idx(struct ice_aqc_recipe_data_elem *buf,
1215 		       struct ice_sw_recipe *recp)
1216 {
1217 	if (buf->content.result_indx & ICE_AQ_RECIPE_RESULT_EN)
1218 		set_bit(buf->content.result_indx & ~ICE_AQ_RECIPE_RESULT_EN,
1219 			recp->res_idxs);
1220 }
1221 
1222 /**
1223  * ice_get_recp_frm_fw - update SW bookkeeping from FW recipe entries
1224  * @hw: pointer to hardware structure
1225  * @recps: struct that we need to populate
1226  * @rid: recipe ID that we are populating
1227  * @refresh_required: true if we should get recipe to profile mapping from FW
1228  *
1229  * This function is used to populate all the necessary entries into our
1230  * bookkeeping so that we have a current list of all the recipes that are
1231  * programmed in the firmware.
1232  */
1233 static enum ice_status
1234 ice_get_recp_frm_fw(struct ice_hw *hw, struct ice_sw_recipe *recps, u8 rid,
1235 		    bool *refresh_required)
1236 {
1237 	DECLARE_BITMAP(result_bm, ICE_MAX_FV_WORDS);
1238 	struct ice_aqc_recipe_data_elem *tmp;
1239 	u16 num_recps = ICE_MAX_NUM_RECIPES;
1240 	struct ice_prot_lkup_ext *lkup_exts;
1241 	enum ice_status status;
1242 	u8 fv_word_idx = 0;
1243 	u16 sub_recps;
1244 
1245 	bitmap_zero(result_bm, ICE_MAX_FV_WORDS);
1246 
1247 	/* we need a buffer big enough to accommodate all the recipes */
1248 	tmp = kcalloc(ICE_MAX_NUM_RECIPES, sizeof(*tmp), GFP_KERNEL);
1249 	if (!tmp)
1250 		return ICE_ERR_NO_MEMORY;
1251 
1252 	tmp[0].recipe_indx = rid;
1253 	status = ice_aq_get_recipe(hw, tmp, &num_recps, rid, NULL);
1254 	/* non-zero status meaning recipe doesn't exist */
1255 	if (status)
1256 		goto err_unroll;
1257 
1258 	/* Get recipe to profile map so that we can get the fv from lkups that
1259 	 * we read for a recipe from FW. Since we want to minimize the number of
1260 	 * times we make this FW call, just make one call and cache the copy
1261 	 * until a new recipe is added. This operation is only required the
1262 	 * first time to get the changes from FW. Then to search existing
1263 	 * entries we don't need to update the cache again until another recipe
1264 	 * gets added.
1265 	 */
1266 	if (*refresh_required) {
1267 		ice_get_recp_to_prof_map(hw);
1268 		*refresh_required = false;
1269 	}
1270 
1271 	/* Start populating all the entries for recps[rid] based on lkups from
1272 	 * firmware. Note that we are only creating the root recipe in our
1273 	 * database.
1274 	 */
1275 	lkup_exts = &recps[rid].lkup_exts;
1276 
1277 	for (sub_recps = 0; sub_recps < num_recps; sub_recps++) {
1278 		struct ice_aqc_recipe_data_elem root_bufs = tmp[sub_recps];
1279 		struct ice_recp_grp_entry *rg_entry;
1280 		u8 i, prof, idx, prot = 0;
1281 		bool is_root;
1282 		u16 off = 0;
1283 
1284 		rg_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*rg_entry),
1285 					GFP_KERNEL);
1286 		if (!rg_entry) {
1287 			status = ICE_ERR_NO_MEMORY;
1288 			goto err_unroll;
1289 		}
1290 
1291 		idx = root_bufs.recipe_indx;
1292 		is_root = root_bufs.content.rid & ICE_AQ_RECIPE_ID_IS_ROOT;
1293 
1294 		/* Mark all result indices in this chain */
1295 		if (root_bufs.content.result_indx & ICE_AQ_RECIPE_RESULT_EN)
1296 			set_bit(root_bufs.content.result_indx & ~ICE_AQ_RECIPE_RESULT_EN,
1297 				result_bm);
1298 
1299 		/* get the first profile that is associated with rid */
1300 		prof = find_first_bit(recipe_to_profile[idx],
1301 				      ICE_MAX_NUM_PROFILES);
1302 		for (i = 0; i < ICE_NUM_WORDS_RECIPE; i++) {
1303 			u8 lkup_indx = root_bufs.content.lkup_indx[i + 1];
1304 
1305 			rg_entry->fv_idx[i] = lkup_indx;
1306 			rg_entry->fv_mask[i] =
1307 				le16_to_cpu(root_bufs.content.mask[i + 1]);
1308 
1309 			/* If the recipe is a chained recipe then all its
1310 			 * child recipe's result will have a result index.
1311 			 * To fill fv_words we should not use those result
1312 			 * index, we only need the protocol ids and offsets.
1313 			 * We will skip all the fv_idx which stores result
1314 			 * index in them. We also need to skip any fv_idx which
1315 			 * has ICE_AQ_RECIPE_LKUP_IGNORE or 0 since it isn't a
1316 			 * valid offset value.
1317 			 */
1318 			if (test_bit(rg_entry->fv_idx[i], hw->switch_info->prof_res_bm[prof]) ||
1319 			    rg_entry->fv_idx[i] & ICE_AQ_RECIPE_LKUP_IGNORE ||
1320 			    rg_entry->fv_idx[i] == 0)
1321 				continue;
1322 
1323 			ice_find_prot_off(hw, ICE_BLK_SW, prof,
1324 					  rg_entry->fv_idx[i], &prot, &off);
1325 			lkup_exts->fv_words[fv_word_idx].prot_id = prot;
1326 			lkup_exts->fv_words[fv_word_idx].off = off;
1327 			lkup_exts->field_mask[fv_word_idx] =
1328 				rg_entry->fv_mask[i];
1329 			fv_word_idx++;
1330 		}
1331 		/* populate rg_list with the data from the child entry of this
1332 		 * recipe
1333 		 */
1334 		list_add(&rg_entry->l_entry, &recps[rid].rg_list);
1335 
1336 		/* Propagate some data to the recipe database */
1337 		recps[idx].is_root = !!is_root;
1338 		recps[idx].priority = root_bufs.content.act_ctrl_fwd_priority;
1339 		bitmap_zero(recps[idx].res_idxs, ICE_MAX_FV_WORDS);
1340 		if (root_bufs.content.result_indx & ICE_AQ_RECIPE_RESULT_EN) {
1341 			recps[idx].chain_idx = root_bufs.content.result_indx &
1342 				~ICE_AQ_RECIPE_RESULT_EN;
1343 			set_bit(recps[idx].chain_idx, recps[idx].res_idxs);
1344 		} else {
1345 			recps[idx].chain_idx = ICE_INVAL_CHAIN_IND;
1346 		}
1347 
1348 		if (!is_root)
1349 			continue;
1350 
1351 		/* Only do the following for root recipes entries */
1352 		memcpy(recps[idx].r_bitmap, root_bufs.recipe_bitmap,
1353 		       sizeof(recps[idx].r_bitmap));
1354 		recps[idx].root_rid = root_bufs.content.rid &
1355 			~ICE_AQ_RECIPE_ID_IS_ROOT;
1356 		recps[idx].priority = root_bufs.content.act_ctrl_fwd_priority;
1357 	}
1358 
1359 	/* Complete initialization of the root recipe entry */
1360 	lkup_exts->n_val_words = fv_word_idx;
1361 	recps[rid].big_recp = (num_recps > 1);
1362 	recps[rid].n_grp_count = (u8)num_recps;
1363 	recps[rid].root_buf = devm_kmemdup(ice_hw_to_dev(hw), tmp,
1364 					   recps[rid].n_grp_count * sizeof(*recps[rid].root_buf),
1365 					   GFP_KERNEL);
1366 	if (!recps[rid].root_buf) {
1367 		status = ICE_ERR_NO_MEMORY;
1368 		goto err_unroll;
1369 	}
1370 
1371 	/* Copy result indexes */
1372 	bitmap_copy(recps[rid].res_idxs, result_bm, ICE_MAX_FV_WORDS);
1373 	recps[rid].recp_created = true;
1374 
1375 err_unroll:
1376 	kfree(tmp);
1377 	return status;
1378 }
1379 
1380 /* ice_init_port_info - Initialize port_info with switch configuration data
1381  * @pi: pointer to port_info
1382  * @vsi_port_num: VSI number or port number
1383  * @type: Type of switch element (port or VSI)
1384  * @swid: switch ID of the switch the element is attached to
1385  * @pf_vf_num: PF or VF number
1386  * @is_vf: true if the element is a VF, false otherwise
1387  */
1388 static void
1389 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
1390 		   u16 swid, u16 pf_vf_num, bool is_vf)
1391 {
1392 	switch (type) {
1393 	case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
1394 		pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
1395 		pi->sw_id = swid;
1396 		pi->pf_vf_num = pf_vf_num;
1397 		pi->is_vf = is_vf;
1398 		pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
1399 		pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
1400 		break;
1401 	default:
1402 		ice_debug(pi->hw, ICE_DBG_SW, "incorrect VSI/port type received\n");
1403 		break;
1404 	}
1405 }
1406 
1407 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
1408  * @hw: pointer to the hardware structure
1409  */
1410 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
1411 {
1412 	struct ice_aqc_get_sw_cfg_resp_elem *rbuf;
1413 	enum ice_status status;
1414 	u16 req_desc = 0;
1415 	u16 num_elems;
1416 	u16 i;
1417 
1418 	rbuf = devm_kzalloc(ice_hw_to_dev(hw), ICE_SW_CFG_MAX_BUF_LEN,
1419 			    GFP_KERNEL);
1420 
1421 	if (!rbuf)
1422 		return ICE_ERR_NO_MEMORY;
1423 
1424 	/* Multiple calls to ice_aq_get_sw_cfg may be required
1425 	 * to get all the switch configuration information. The need
1426 	 * for additional calls is indicated by ice_aq_get_sw_cfg
1427 	 * writing a non-zero value in req_desc
1428 	 */
1429 	do {
1430 		struct ice_aqc_get_sw_cfg_resp_elem *ele;
1431 
1432 		status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
1433 					   &req_desc, &num_elems, NULL);
1434 
1435 		if (status)
1436 			break;
1437 
1438 		for (i = 0, ele = rbuf; i < num_elems; i++, ele++) {
1439 			u16 pf_vf_num, swid, vsi_port_num;
1440 			bool is_vf = false;
1441 			u8 res_type;
1442 
1443 			vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
1444 				ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
1445 
1446 			pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
1447 				ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
1448 
1449 			swid = le16_to_cpu(ele->swid);
1450 
1451 			if (le16_to_cpu(ele->pf_vf_num) &
1452 			    ICE_AQC_GET_SW_CONF_RESP_IS_VF)
1453 				is_vf = true;
1454 
1455 			res_type = (u8)(le16_to_cpu(ele->vsi_port_num) >>
1456 					ICE_AQC_GET_SW_CONF_RESP_TYPE_S);
1457 
1458 			if (res_type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
1459 				/* FW VSI is not needed. Just continue. */
1460 				continue;
1461 			}
1462 
1463 			ice_init_port_info(hw->port_info, vsi_port_num,
1464 					   res_type, swid, pf_vf_num, is_vf);
1465 		}
1466 	} while (req_desc && !status);
1467 
1468 	devm_kfree(ice_hw_to_dev(hw), rbuf);
1469 	return status;
1470 }
1471 
1472 /**
1473  * ice_fill_sw_info - Helper function to populate lb_en and lan_en
1474  * @hw: pointer to the hardware structure
1475  * @fi: filter info structure to fill/update
1476  *
1477  * This helper function populates the lb_en and lan_en elements of the provided
1478  * ice_fltr_info struct using the switch's type and characteristics of the
1479  * switch rule being configured.
1480  */
1481 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
1482 {
1483 	fi->lb_en = false;
1484 	fi->lan_en = false;
1485 	if ((fi->flag & ICE_FLTR_TX) &&
1486 	    (fi->fltr_act == ICE_FWD_TO_VSI ||
1487 	     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1488 	     fi->fltr_act == ICE_FWD_TO_Q ||
1489 	     fi->fltr_act == ICE_FWD_TO_QGRP)) {
1490 		/* Setting LB for prune actions will result in replicated
1491 		 * packets to the internal switch that will be dropped.
1492 		 */
1493 		if (fi->lkup_type != ICE_SW_LKUP_VLAN)
1494 			fi->lb_en = true;
1495 
1496 		/* Set lan_en to TRUE if
1497 		 * 1. The switch is a VEB AND
1498 		 * 2
1499 		 * 2.1 The lookup is a directional lookup like ethertype,
1500 		 * promiscuous, ethertype-MAC, promiscuous-VLAN
1501 		 * and default-port OR
1502 		 * 2.2 The lookup is VLAN, OR
1503 		 * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
1504 		 * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
1505 		 *
1506 		 * OR
1507 		 *
1508 		 * The switch is a VEPA.
1509 		 *
1510 		 * In all other cases, the LAN enable has to be set to false.
1511 		 */
1512 		if (hw->evb_veb) {
1513 			if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1514 			    fi->lkup_type == ICE_SW_LKUP_PROMISC ||
1515 			    fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1516 			    fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1517 			    fi->lkup_type == ICE_SW_LKUP_DFLT ||
1518 			    fi->lkup_type == ICE_SW_LKUP_VLAN ||
1519 			    (fi->lkup_type == ICE_SW_LKUP_MAC &&
1520 			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)) ||
1521 			    (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
1522 			     !is_unicast_ether_addr(fi->l_data.mac.mac_addr)))
1523 				fi->lan_en = true;
1524 		} else {
1525 			fi->lan_en = true;
1526 		}
1527 	}
1528 }
1529 
1530 /**
1531  * ice_fill_sw_rule - Helper function to fill switch rule structure
1532  * @hw: pointer to the hardware structure
1533  * @f_info: entry containing packet forwarding information
1534  * @s_rule: switch rule structure to be filled in based on mac_entry
1535  * @opc: switch rules population command type - pass in the command opcode
1536  */
1537 static void
1538 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
1539 		 struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
1540 {
1541 	u16 vlan_id = ICE_MAX_VLAN_ID + 1;
1542 	void *daddr = NULL;
1543 	u16 eth_hdr_sz;
1544 	u8 *eth_hdr;
1545 	u32 act = 0;
1546 	__be16 *off;
1547 	u8 q_rgn;
1548 
1549 	if (opc == ice_aqc_opc_remove_sw_rules) {
1550 		s_rule->pdata.lkup_tx_rx.act = 0;
1551 		s_rule->pdata.lkup_tx_rx.index =
1552 			cpu_to_le16(f_info->fltr_rule_id);
1553 		s_rule->pdata.lkup_tx_rx.hdr_len = 0;
1554 		return;
1555 	}
1556 
1557 	eth_hdr_sz = sizeof(dummy_eth_header);
1558 	eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
1559 
1560 	/* initialize the ether header with a dummy header */
1561 	memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz);
1562 	ice_fill_sw_info(hw, f_info);
1563 
1564 	switch (f_info->fltr_act) {
1565 	case ICE_FWD_TO_VSI:
1566 		act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
1567 			ICE_SINGLE_ACT_VSI_ID_M;
1568 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1569 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1570 				ICE_SINGLE_ACT_VALID_BIT;
1571 		break;
1572 	case ICE_FWD_TO_VSI_LIST:
1573 		act |= ICE_SINGLE_ACT_VSI_LIST;
1574 		act |= (f_info->fwd_id.vsi_list_id <<
1575 			ICE_SINGLE_ACT_VSI_LIST_ID_S) &
1576 			ICE_SINGLE_ACT_VSI_LIST_ID_M;
1577 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1578 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1579 				ICE_SINGLE_ACT_VALID_BIT;
1580 		break;
1581 	case ICE_FWD_TO_Q:
1582 		act |= ICE_SINGLE_ACT_TO_Q;
1583 		act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1584 			ICE_SINGLE_ACT_Q_INDEX_M;
1585 		break;
1586 	case ICE_DROP_PACKET:
1587 		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
1588 			ICE_SINGLE_ACT_VALID_BIT;
1589 		break;
1590 	case ICE_FWD_TO_QGRP:
1591 		q_rgn = f_info->qgrp_size > 0 ?
1592 			(u8)ilog2(f_info->qgrp_size) : 0;
1593 		act |= ICE_SINGLE_ACT_TO_Q;
1594 		act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1595 			ICE_SINGLE_ACT_Q_INDEX_M;
1596 		act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
1597 			ICE_SINGLE_ACT_Q_REGION_M;
1598 		break;
1599 	default:
1600 		return;
1601 	}
1602 
1603 	if (f_info->lb_en)
1604 		act |= ICE_SINGLE_ACT_LB_ENABLE;
1605 	if (f_info->lan_en)
1606 		act |= ICE_SINGLE_ACT_LAN_ENABLE;
1607 
1608 	switch (f_info->lkup_type) {
1609 	case ICE_SW_LKUP_MAC:
1610 		daddr = f_info->l_data.mac.mac_addr;
1611 		break;
1612 	case ICE_SW_LKUP_VLAN:
1613 		vlan_id = f_info->l_data.vlan.vlan_id;
1614 		if (f_info->fltr_act == ICE_FWD_TO_VSI ||
1615 		    f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
1616 			act |= ICE_SINGLE_ACT_PRUNE;
1617 			act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
1618 		}
1619 		break;
1620 	case ICE_SW_LKUP_ETHERTYPE_MAC:
1621 		daddr = f_info->l_data.ethertype_mac.mac_addr;
1622 		fallthrough;
1623 	case ICE_SW_LKUP_ETHERTYPE:
1624 		off = (__force __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
1625 		*off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
1626 		break;
1627 	case ICE_SW_LKUP_MAC_VLAN:
1628 		daddr = f_info->l_data.mac_vlan.mac_addr;
1629 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
1630 		break;
1631 	case ICE_SW_LKUP_PROMISC_VLAN:
1632 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
1633 		fallthrough;
1634 	case ICE_SW_LKUP_PROMISC:
1635 		daddr = f_info->l_data.mac_vlan.mac_addr;
1636 		break;
1637 	default:
1638 		break;
1639 	}
1640 
1641 	s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
1642 		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
1643 		cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
1644 
1645 	/* Recipe set depending on lookup type */
1646 	s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(f_info->lkup_type);
1647 	s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(f_info->src);
1648 	s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);
1649 
1650 	if (daddr)
1651 		ether_addr_copy(eth_hdr + ICE_ETH_DA_OFFSET, daddr);
1652 
1653 	if (!(vlan_id > ICE_MAX_VLAN_ID)) {
1654 		off = (__force __be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
1655 		*off = cpu_to_be16(vlan_id);
1656 	}
1657 
1658 	/* Create the switch rule with the final dummy Ethernet header */
1659 	if (opc != ice_aqc_opc_update_sw_rules)
1660 		s_rule->pdata.lkup_tx_rx.hdr_len = cpu_to_le16(eth_hdr_sz);
1661 }
1662 
1663 /**
1664  * ice_add_marker_act
1665  * @hw: pointer to the hardware structure
1666  * @m_ent: the management entry for which sw marker needs to be added
1667  * @sw_marker: sw marker to tag the Rx descriptor with
1668  * @l_id: large action resource ID
1669  *
1670  * Create a large action to hold software marker and update the switch rule
1671  * entry pointed by m_ent with newly created large action
1672  */
1673 static enum ice_status
1674 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1675 		   u16 sw_marker, u16 l_id)
1676 {
1677 	struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
1678 	/* For software marker we need 3 large actions
1679 	 * 1. FWD action: FWD TO VSI or VSI LIST
1680 	 * 2. GENERIC VALUE action to hold the profile ID
1681 	 * 3. GENERIC VALUE action to hold the software marker ID
1682 	 */
1683 	const u16 num_lg_acts = 3;
1684 	enum ice_status status;
1685 	u16 lg_act_size;
1686 	u16 rules_size;
1687 	u32 act;
1688 	u16 id;
1689 
1690 	if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1691 		return ICE_ERR_PARAM;
1692 
1693 	/* Create two back-to-back switch rules and submit them to the HW using
1694 	 * one memory buffer:
1695 	 *    1. Large Action
1696 	 *    2. Look up Tx Rx
1697 	 */
1698 	lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
1699 	rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1700 	lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
1701 	if (!lg_act)
1702 		return ICE_ERR_NO_MEMORY;
1703 
1704 	rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
1705 
1706 	/* Fill in the first switch rule i.e. large action */
1707 	lg_act->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
1708 	lg_act->pdata.lg_act.index = cpu_to_le16(l_id);
1709 	lg_act->pdata.lg_act.size = cpu_to_le16(num_lg_acts);
1710 
1711 	/* First action VSI forwarding or VSI list forwarding depending on how
1712 	 * many VSIs
1713 	 */
1714 	id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
1715 		m_ent->fltr_info.fwd_id.hw_vsi_id;
1716 
1717 	act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1718 	act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) & ICE_LG_ACT_VSI_LIST_ID_M;
1719 	if (m_ent->vsi_count > 1)
1720 		act |= ICE_LG_ACT_VSI_LIST;
1721 	lg_act->pdata.lg_act.act[0] = cpu_to_le32(act);
1722 
1723 	/* Second action descriptor type */
1724 	act = ICE_LG_ACT_GENERIC;
1725 
1726 	act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
1727 	lg_act->pdata.lg_act.act[1] = cpu_to_le32(act);
1728 
1729 	act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
1730 	       ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
1731 
1732 	/* Third action Marker value */
1733 	act |= ICE_LG_ACT_GENERIC;
1734 	act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
1735 		ICE_LG_ACT_GENERIC_VALUE_M;
1736 
1737 	lg_act->pdata.lg_act.act[2] = cpu_to_le32(act);
1738 
1739 	/* call the fill switch rule to fill the lookup Tx Rx structure */
1740 	ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1741 			 ice_aqc_opc_update_sw_rules);
1742 
1743 	/* Update the action to point to the large action ID */
1744 	rx_tx->pdata.lkup_tx_rx.act =
1745 		cpu_to_le32(ICE_SINGLE_ACT_PTR |
1746 			    ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
1747 			     ICE_SINGLE_ACT_PTR_VAL_M));
1748 
1749 	/* Use the filter rule ID of the previously created rule with single
1750 	 * act. Once the update happens, hardware will treat this as large
1751 	 * action
1752 	 */
1753 	rx_tx->pdata.lkup_tx_rx.index =
1754 		cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
1755 
1756 	status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1757 				 ice_aqc_opc_update_sw_rules, NULL);
1758 	if (!status) {
1759 		m_ent->lg_act_idx = l_id;
1760 		m_ent->sw_marker_id = sw_marker;
1761 	}
1762 
1763 	devm_kfree(ice_hw_to_dev(hw), lg_act);
1764 	return status;
1765 }
1766 
1767 /**
1768  * ice_create_vsi_list_map
1769  * @hw: pointer to the hardware structure
1770  * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
1771  * @num_vsi: number of VSI handles in the array
1772  * @vsi_list_id: VSI list ID generated as part of allocate resource
1773  *
1774  * Helper function to create a new entry of VSI list ID to VSI mapping
1775  * using the given VSI list ID
1776  */
1777 static struct ice_vsi_list_map_info *
1778 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1779 			u16 vsi_list_id)
1780 {
1781 	struct ice_switch_info *sw = hw->switch_info;
1782 	struct ice_vsi_list_map_info *v_map;
1783 	int i;
1784 
1785 	v_map = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*v_map), GFP_KERNEL);
1786 	if (!v_map)
1787 		return NULL;
1788 
1789 	v_map->vsi_list_id = vsi_list_id;
1790 	v_map->ref_cnt = 1;
1791 	for (i = 0; i < num_vsi; i++)
1792 		set_bit(vsi_handle_arr[i], v_map->vsi_map);
1793 
1794 	list_add(&v_map->list_entry, &sw->vsi_list_map_head);
1795 	return v_map;
1796 }
1797 
1798 /**
1799  * ice_update_vsi_list_rule
1800  * @hw: pointer to the hardware structure
1801  * @vsi_handle_arr: array of VSI handles to form a VSI list
1802  * @num_vsi: number of VSI handles in the array
1803  * @vsi_list_id: VSI list ID generated as part of allocate resource
1804  * @remove: Boolean value to indicate if this is a remove action
1805  * @opc: switch rules population command type - pass in the command opcode
1806  * @lkup_type: lookup type of the filter
1807  *
1808  * Call AQ command to add a new switch rule or update existing switch rule
1809  * using the given VSI list ID
1810  */
1811 static enum ice_status
1812 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1813 			 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
1814 			 enum ice_sw_lkup_type lkup_type)
1815 {
1816 	struct ice_aqc_sw_rules_elem *s_rule;
1817 	enum ice_status status;
1818 	u16 s_rule_size;
1819 	u16 rule_type;
1820 	int i;
1821 
1822 	if (!num_vsi)
1823 		return ICE_ERR_PARAM;
1824 
1825 	if (lkup_type == ICE_SW_LKUP_MAC ||
1826 	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
1827 	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1828 	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1829 	    lkup_type == ICE_SW_LKUP_PROMISC ||
1830 	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
1831 		rule_type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
1832 			ICE_AQC_SW_RULES_T_VSI_LIST_SET;
1833 	else if (lkup_type == ICE_SW_LKUP_VLAN)
1834 		rule_type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
1835 			ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
1836 	else
1837 		return ICE_ERR_PARAM;
1838 
1839 	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
1840 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
1841 	if (!s_rule)
1842 		return ICE_ERR_NO_MEMORY;
1843 	for (i = 0; i < num_vsi; i++) {
1844 		if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
1845 			status = ICE_ERR_PARAM;
1846 			goto exit;
1847 		}
1848 		/* AQ call requires hw_vsi_id(s) */
1849 		s_rule->pdata.vsi_list.vsi[i] =
1850 			cpu_to_le16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
1851 	}
1852 
1853 	s_rule->type = cpu_to_le16(rule_type);
1854 	s_rule->pdata.vsi_list.number_vsi = cpu_to_le16(num_vsi);
1855 	s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
1856 
1857 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
1858 
1859 exit:
1860 	devm_kfree(ice_hw_to_dev(hw), s_rule);
1861 	return status;
1862 }
1863 
1864 /**
1865  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
1866  * @hw: pointer to the HW struct
1867  * @vsi_handle_arr: array of VSI handles to form a VSI list
1868  * @num_vsi: number of VSI handles in the array
1869  * @vsi_list_id: stores the ID of the VSI list to be created
1870  * @lkup_type: switch rule filter's lookup type
1871  */
1872 static enum ice_status
1873 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1874 			 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1875 {
1876 	enum ice_status status;
1877 
1878 	status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1879 					    ice_aqc_opc_alloc_res);
1880 	if (status)
1881 		return status;
1882 
1883 	/* Update the newly created VSI list to include the specified VSIs */
1884 	return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1885 					*vsi_list_id, false,
1886 					ice_aqc_opc_add_sw_rules, lkup_type);
1887 }
1888 
1889 /**
1890  * ice_create_pkt_fwd_rule
1891  * @hw: pointer to the hardware structure
1892  * @f_entry: entry containing packet forwarding information
1893  *
1894  * Create switch rule with given filter information and add an entry
1895  * to the corresponding filter management list to track this switch rule
1896  * and VSI mapping
1897  */
1898 static enum ice_status
1899 ice_create_pkt_fwd_rule(struct ice_hw *hw,
1900 			struct ice_fltr_list_entry *f_entry)
1901 {
1902 	struct ice_fltr_mgmt_list_entry *fm_entry;
1903 	struct ice_aqc_sw_rules_elem *s_rule;
1904 	enum ice_sw_lkup_type l_type;
1905 	struct ice_sw_recipe *recp;
1906 	enum ice_status status;
1907 
1908 	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1909 			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1910 	if (!s_rule)
1911 		return ICE_ERR_NO_MEMORY;
1912 	fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
1913 				GFP_KERNEL);
1914 	if (!fm_entry) {
1915 		status = ICE_ERR_NO_MEMORY;
1916 		goto ice_create_pkt_fwd_rule_exit;
1917 	}
1918 
1919 	fm_entry->fltr_info = f_entry->fltr_info;
1920 
1921 	/* Initialize all the fields for the management entry */
1922 	fm_entry->vsi_count = 1;
1923 	fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1924 	fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1925 	fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1926 
1927 	ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1928 			 ice_aqc_opc_add_sw_rules);
1929 
1930 	status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1931 				 ice_aqc_opc_add_sw_rules, NULL);
1932 	if (status) {
1933 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
1934 		goto ice_create_pkt_fwd_rule_exit;
1935 	}
1936 
1937 	f_entry->fltr_info.fltr_rule_id =
1938 		le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1939 	fm_entry->fltr_info.fltr_rule_id =
1940 		le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
1941 
1942 	/* The book keeping entries will get removed when base driver
1943 	 * calls remove filter AQ command
1944 	 */
1945 	l_type = fm_entry->fltr_info.lkup_type;
1946 	recp = &hw->switch_info->recp_list[l_type];
1947 	list_add(&fm_entry->list_entry, &recp->filt_rules);
1948 
1949 ice_create_pkt_fwd_rule_exit:
1950 	devm_kfree(ice_hw_to_dev(hw), s_rule);
1951 	return status;
1952 }
1953 
1954 /**
1955  * ice_update_pkt_fwd_rule
1956  * @hw: pointer to the hardware structure
1957  * @f_info: filter information for switch rule
1958  *
1959  * Call AQ command to update a previously created switch rule with a
1960  * VSI list ID
1961  */
1962 static enum ice_status
1963 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1964 {
1965 	struct ice_aqc_sw_rules_elem *s_rule;
1966 	enum ice_status status;
1967 
1968 	s_rule = devm_kzalloc(ice_hw_to_dev(hw),
1969 			      ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
1970 	if (!s_rule)
1971 		return ICE_ERR_NO_MEMORY;
1972 
1973 	ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
1974 
1975 	s_rule->pdata.lkup_tx_rx.index = cpu_to_le16(f_info->fltr_rule_id);
1976 
1977 	/* Update switch rule with new rule set to forward VSI list */
1978 	status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1979 				 ice_aqc_opc_update_sw_rules, NULL);
1980 
1981 	devm_kfree(ice_hw_to_dev(hw), s_rule);
1982 	return status;
1983 }
1984 
1985 /**
1986  * ice_update_sw_rule_bridge_mode
1987  * @hw: pointer to the HW struct
1988  *
1989  * Updates unicast switch filter rules based on VEB/VEPA mode
1990  */
1991 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
1992 {
1993 	struct ice_switch_info *sw = hw->switch_info;
1994 	struct ice_fltr_mgmt_list_entry *fm_entry;
1995 	enum ice_status status = 0;
1996 	struct list_head *rule_head;
1997 	struct mutex *rule_lock; /* Lock to protect filter rule list */
1998 
1999 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2000 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2001 
2002 	mutex_lock(rule_lock);
2003 	list_for_each_entry(fm_entry, rule_head, list_entry) {
2004 		struct ice_fltr_info *fi = &fm_entry->fltr_info;
2005 		u8 *addr = fi->l_data.mac.mac_addr;
2006 
2007 		/* Update unicast Tx rules to reflect the selected
2008 		 * VEB/VEPA mode
2009 		 */
2010 		if ((fi->flag & ICE_FLTR_TX) && is_unicast_ether_addr(addr) &&
2011 		    (fi->fltr_act == ICE_FWD_TO_VSI ||
2012 		     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
2013 		     fi->fltr_act == ICE_FWD_TO_Q ||
2014 		     fi->fltr_act == ICE_FWD_TO_QGRP)) {
2015 			status = ice_update_pkt_fwd_rule(hw, fi);
2016 			if (status)
2017 				break;
2018 		}
2019 	}
2020 
2021 	mutex_unlock(rule_lock);
2022 
2023 	return status;
2024 }
2025 
2026 /**
2027  * ice_add_update_vsi_list
2028  * @hw: pointer to the hardware structure
2029  * @m_entry: pointer to current filter management list entry
2030  * @cur_fltr: filter information from the book keeping entry
2031  * @new_fltr: filter information with the new VSI to be added
2032  *
2033  * Call AQ command to add or update previously created VSI list with new VSI.
2034  *
2035  * Helper function to do book keeping associated with adding filter information
2036  * The algorithm to do the book keeping is described below :
2037  * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
2038  *	if only one VSI has been added till now
2039  *		Allocate a new VSI list and add two VSIs
2040  *		to this list using switch rule command
2041  *		Update the previously created switch rule with the
2042  *		newly created VSI list ID
2043  *	if a VSI list was previously created
2044  *		Add the new VSI to the previously created VSI list set
2045  *		using the update switch rule command
2046  */
2047 static enum ice_status
2048 ice_add_update_vsi_list(struct ice_hw *hw,
2049 			struct ice_fltr_mgmt_list_entry *m_entry,
2050 			struct ice_fltr_info *cur_fltr,
2051 			struct ice_fltr_info *new_fltr)
2052 {
2053 	enum ice_status status = 0;
2054 	u16 vsi_list_id = 0;
2055 
2056 	if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
2057 	     cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
2058 		return ICE_ERR_NOT_IMPL;
2059 
2060 	if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
2061 	     new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
2062 	    (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
2063 	     cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
2064 		return ICE_ERR_NOT_IMPL;
2065 
2066 	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
2067 		/* Only one entry existed in the mapping and it was not already
2068 		 * a part of a VSI list. So, create a VSI list with the old and
2069 		 * new VSIs.
2070 		 */
2071 		struct ice_fltr_info tmp_fltr;
2072 		u16 vsi_handle_arr[2];
2073 
2074 		/* A rule already exists with the new VSI being added */
2075 		if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
2076 			return ICE_ERR_ALREADY_EXISTS;
2077 
2078 		vsi_handle_arr[0] = cur_fltr->vsi_handle;
2079 		vsi_handle_arr[1] = new_fltr->vsi_handle;
2080 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2081 						  &vsi_list_id,
2082 						  new_fltr->lkup_type);
2083 		if (status)
2084 			return status;
2085 
2086 		tmp_fltr = *new_fltr;
2087 		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
2088 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2089 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2090 		/* Update the previous switch rule of "MAC forward to VSI" to
2091 		 * "MAC fwd to VSI list"
2092 		 */
2093 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2094 		if (status)
2095 			return status;
2096 
2097 		cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
2098 		cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2099 		m_entry->vsi_list_info =
2100 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2101 						vsi_list_id);
2102 
2103 		if (!m_entry->vsi_list_info)
2104 			return ICE_ERR_NO_MEMORY;
2105 
2106 		/* If this entry was large action then the large action needs
2107 		 * to be updated to point to FWD to VSI list
2108 		 */
2109 		if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
2110 			status =
2111 			    ice_add_marker_act(hw, m_entry,
2112 					       m_entry->sw_marker_id,
2113 					       m_entry->lg_act_idx);
2114 	} else {
2115 		u16 vsi_handle = new_fltr->vsi_handle;
2116 		enum ice_adminq_opc opcode;
2117 
2118 		if (!m_entry->vsi_list_info)
2119 			return ICE_ERR_CFG;
2120 
2121 		/* A rule already exists with the new VSI being added */
2122 		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
2123 			return 0;
2124 
2125 		/* Update the previously created VSI list set with
2126 		 * the new VSI ID passed in
2127 		 */
2128 		vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
2129 		opcode = ice_aqc_opc_update_sw_rules;
2130 
2131 		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
2132 						  vsi_list_id, false, opcode,
2133 						  new_fltr->lkup_type);
2134 		/* update VSI list mapping info with new VSI ID */
2135 		if (!status)
2136 			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
2137 	}
2138 	if (!status)
2139 		m_entry->vsi_count++;
2140 	return status;
2141 }
2142 
2143 /**
2144  * ice_find_rule_entry - Search a rule entry
2145  * @hw: pointer to the hardware structure
2146  * @recp_id: lookup type for which the specified rule needs to be searched
2147  * @f_info: rule information
2148  *
2149  * Helper function to search for a given rule entry
2150  * Returns pointer to entry storing the rule if found
2151  */
2152 static struct ice_fltr_mgmt_list_entry *
2153 ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
2154 {
2155 	struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
2156 	struct ice_switch_info *sw = hw->switch_info;
2157 	struct list_head *list_head;
2158 
2159 	list_head = &sw->recp_list[recp_id].filt_rules;
2160 	list_for_each_entry(list_itr, list_head, list_entry) {
2161 		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
2162 			    sizeof(f_info->l_data)) &&
2163 		    f_info->flag == list_itr->fltr_info.flag) {
2164 			ret = list_itr;
2165 			break;
2166 		}
2167 	}
2168 	return ret;
2169 }
2170 
2171 /**
2172  * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
2173  * @hw: pointer to the hardware structure
2174  * @recp_id: lookup type for which VSI lists needs to be searched
2175  * @vsi_handle: VSI handle to be found in VSI list
2176  * @vsi_list_id: VSI list ID found containing vsi_handle
2177  *
2178  * Helper function to search a VSI list with single entry containing given VSI
2179  * handle element. This can be extended further to search VSI list with more
2180  * than 1 vsi_count. Returns pointer to VSI list entry if found.
2181  */
2182 static struct ice_vsi_list_map_info *
2183 ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
2184 			u16 *vsi_list_id)
2185 {
2186 	struct ice_vsi_list_map_info *map_info = NULL;
2187 	struct ice_switch_info *sw = hw->switch_info;
2188 	struct ice_fltr_mgmt_list_entry *list_itr;
2189 	struct list_head *list_head;
2190 
2191 	list_head = &sw->recp_list[recp_id].filt_rules;
2192 	list_for_each_entry(list_itr, list_head, list_entry) {
2193 		if (list_itr->vsi_count == 1 && list_itr->vsi_list_info) {
2194 			map_info = list_itr->vsi_list_info;
2195 			if (test_bit(vsi_handle, map_info->vsi_map)) {
2196 				*vsi_list_id = map_info->vsi_list_id;
2197 				return map_info;
2198 			}
2199 		}
2200 	}
2201 	return NULL;
2202 }
2203 
2204 /**
2205  * ice_add_rule_internal - add rule for a given lookup type
2206  * @hw: pointer to the hardware structure
2207  * @recp_id: lookup type (recipe ID) for which rule has to be added
2208  * @f_entry: structure containing MAC forwarding information
2209  *
2210  * Adds or updates the rule lists for a given recipe
2211  */
2212 static enum ice_status
2213 ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
2214 		      struct ice_fltr_list_entry *f_entry)
2215 {
2216 	struct ice_switch_info *sw = hw->switch_info;
2217 	struct ice_fltr_info *new_fltr, *cur_fltr;
2218 	struct ice_fltr_mgmt_list_entry *m_entry;
2219 	struct mutex *rule_lock; /* Lock to protect filter rule list */
2220 	enum ice_status status = 0;
2221 
2222 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2223 		return ICE_ERR_PARAM;
2224 	f_entry->fltr_info.fwd_id.hw_vsi_id =
2225 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2226 
2227 	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
2228 
2229 	mutex_lock(rule_lock);
2230 	new_fltr = &f_entry->fltr_info;
2231 	if (new_fltr->flag & ICE_FLTR_RX)
2232 		new_fltr->src = hw->port_info->lport;
2233 	else if (new_fltr->flag & ICE_FLTR_TX)
2234 		new_fltr->src = f_entry->fltr_info.fwd_id.hw_vsi_id;
2235 
2236 	m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
2237 	if (!m_entry) {
2238 		mutex_unlock(rule_lock);
2239 		return ice_create_pkt_fwd_rule(hw, f_entry);
2240 	}
2241 
2242 	cur_fltr = &m_entry->fltr_info;
2243 	status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
2244 	mutex_unlock(rule_lock);
2245 
2246 	return status;
2247 }
2248 
2249 /**
2250  * ice_remove_vsi_list_rule
2251  * @hw: pointer to the hardware structure
2252  * @vsi_list_id: VSI list ID generated as part of allocate resource
2253  * @lkup_type: switch rule filter lookup type
2254  *
2255  * The VSI list should be emptied before this function is called to remove the
2256  * VSI list.
2257  */
2258 static enum ice_status
2259 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
2260 			 enum ice_sw_lkup_type lkup_type)
2261 {
2262 	struct ice_aqc_sw_rules_elem *s_rule;
2263 	enum ice_status status;
2264 	u16 s_rule_size;
2265 
2266 	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
2267 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
2268 	if (!s_rule)
2269 		return ICE_ERR_NO_MEMORY;
2270 
2271 	s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
2272 	s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
2273 
2274 	/* Free the vsi_list resource that we allocated. It is assumed that the
2275 	 * list is empty at this point.
2276 	 */
2277 	status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
2278 					    ice_aqc_opc_free_res);
2279 
2280 	devm_kfree(ice_hw_to_dev(hw), s_rule);
2281 	return status;
2282 }
2283 
2284 /**
2285  * ice_rem_update_vsi_list
2286  * @hw: pointer to the hardware structure
2287  * @vsi_handle: VSI handle of the VSI to remove
2288  * @fm_list: filter management entry for which the VSI list management needs to
2289  *           be done
2290  */
2291 static enum ice_status
2292 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
2293 			struct ice_fltr_mgmt_list_entry *fm_list)
2294 {
2295 	enum ice_sw_lkup_type lkup_type;
2296 	enum ice_status status = 0;
2297 	u16 vsi_list_id;
2298 
2299 	if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
2300 	    fm_list->vsi_count == 0)
2301 		return ICE_ERR_PARAM;
2302 
2303 	/* A rule with the VSI being removed does not exist */
2304 	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
2305 		return ICE_ERR_DOES_NOT_EXIST;
2306 
2307 	lkup_type = fm_list->fltr_info.lkup_type;
2308 	vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
2309 	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
2310 					  ice_aqc_opc_update_sw_rules,
2311 					  lkup_type);
2312 	if (status)
2313 		return status;
2314 
2315 	fm_list->vsi_count--;
2316 	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
2317 
2318 	if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
2319 		struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
2320 		struct ice_vsi_list_map_info *vsi_list_info =
2321 			fm_list->vsi_list_info;
2322 		u16 rem_vsi_handle;
2323 
2324 		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
2325 						ICE_MAX_VSI);
2326 		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
2327 			return ICE_ERR_OUT_OF_RANGE;
2328 
2329 		/* Make sure VSI list is empty before removing it below */
2330 		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
2331 						  vsi_list_id, true,
2332 						  ice_aqc_opc_update_sw_rules,
2333 						  lkup_type);
2334 		if (status)
2335 			return status;
2336 
2337 		tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
2338 		tmp_fltr_info.fwd_id.hw_vsi_id =
2339 			ice_get_hw_vsi_num(hw, rem_vsi_handle);
2340 		tmp_fltr_info.vsi_handle = rem_vsi_handle;
2341 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
2342 		if (status) {
2343 			ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
2344 				  tmp_fltr_info.fwd_id.hw_vsi_id, status);
2345 			return status;
2346 		}
2347 
2348 		fm_list->fltr_info = tmp_fltr_info;
2349 	}
2350 
2351 	if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
2352 	    (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
2353 		struct ice_vsi_list_map_info *vsi_list_info =
2354 			fm_list->vsi_list_info;
2355 
2356 		/* Remove the VSI list since it is no longer used */
2357 		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
2358 		if (status) {
2359 			ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
2360 				  vsi_list_id, status);
2361 			return status;
2362 		}
2363 
2364 		list_del(&vsi_list_info->list_entry);
2365 		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
2366 		fm_list->vsi_list_info = NULL;
2367 	}
2368 
2369 	return status;
2370 }
2371 
2372 /**
2373  * ice_remove_rule_internal - Remove a filter rule of a given type
2374  * @hw: pointer to the hardware structure
2375  * @recp_id: recipe ID for which the rule needs to removed
2376  * @f_entry: rule entry containing filter information
2377  */
2378 static enum ice_status
2379 ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
2380 			 struct ice_fltr_list_entry *f_entry)
2381 {
2382 	struct ice_switch_info *sw = hw->switch_info;
2383 	struct ice_fltr_mgmt_list_entry *list_elem;
2384 	struct mutex *rule_lock; /* Lock to protect filter rule list */
2385 	enum ice_status status = 0;
2386 	bool remove_rule = false;
2387 	u16 vsi_handle;
2388 
2389 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2390 		return ICE_ERR_PARAM;
2391 	f_entry->fltr_info.fwd_id.hw_vsi_id =
2392 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2393 
2394 	rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
2395 	mutex_lock(rule_lock);
2396 	list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
2397 	if (!list_elem) {
2398 		status = ICE_ERR_DOES_NOT_EXIST;
2399 		goto exit;
2400 	}
2401 
2402 	if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
2403 		remove_rule = true;
2404 	} else if (!list_elem->vsi_list_info) {
2405 		status = ICE_ERR_DOES_NOT_EXIST;
2406 		goto exit;
2407 	} else if (list_elem->vsi_list_info->ref_cnt > 1) {
2408 		/* a ref_cnt > 1 indicates that the vsi_list is being
2409 		 * shared by multiple rules. Decrement the ref_cnt and
2410 		 * remove this rule, but do not modify the list, as it
2411 		 * is in-use by other rules.
2412 		 */
2413 		list_elem->vsi_list_info->ref_cnt--;
2414 		remove_rule = true;
2415 	} else {
2416 		/* a ref_cnt of 1 indicates the vsi_list is only used
2417 		 * by one rule. However, the original removal request is only
2418 		 * for a single VSI. Update the vsi_list first, and only
2419 		 * remove the rule if there are no further VSIs in this list.
2420 		 */
2421 		vsi_handle = f_entry->fltr_info.vsi_handle;
2422 		status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
2423 		if (status)
2424 			goto exit;
2425 		/* if VSI count goes to zero after updating the VSI list */
2426 		if (list_elem->vsi_count == 0)
2427 			remove_rule = true;
2428 	}
2429 
2430 	if (remove_rule) {
2431 		/* Remove the lookup rule */
2432 		struct ice_aqc_sw_rules_elem *s_rule;
2433 
2434 		s_rule = devm_kzalloc(ice_hw_to_dev(hw),
2435 				      ICE_SW_RULE_RX_TX_NO_HDR_SIZE,
2436 				      GFP_KERNEL);
2437 		if (!s_rule) {
2438 			status = ICE_ERR_NO_MEMORY;
2439 			goto exit;
2440 		}
2441 
2442 		ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
2443 				 ice_aqc_opc_remove_sw_rules);
2444 
2445 		status = ice_aq_sw_rules(hw, s_rule,
2446 					 ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
2447 					 ice_aqc_opc_remove_sw_rules, NULL);
2448 
2449 		/* Remove a book keeping from the list */
2450 		devm_kfree(ice_hw_to_dev(hw), s_rule);
2451 
2452 		if (status)
2453 			goto exit;
2454 
2455 		list_del(&list_elem->list_entry);
2456 		devm_kfree(ice_hw_to_dev(hw), list_elem);
2457 	}
2458 exit:
2459 	mutex_unlock(rule_lock);
2460 	return status;
2461 }
2462 
2463 /**
2464  * ice_mac_fltr_exist - does this MAC filter exist for given VSI
2465  * @hw: pointer to the hardware structure
2466  * @mac: MAC address to be checked (for MAC filter)
2467  * @vsi_handle: check MAC filter for this VSI
2468  */
2469 bool ice_mac_fltr_exist(struct ice_hw *hw, u8 *mac, u16 vsi_handle)
2470 {
2471 	struct ice_fltr_mgmt_list_entry *entry;
2472 	struct list_head *rule_head;
2473 	struct ice_switch_info *sw;
2474 	struct mutex *rule_lock; /* Lock to protect filter rule list */
2475 	u16 hw_vsi_id;
2476 
2477 	if (!ice_is_vsi_valid(hw, vsi_handle))
2478 		return false;
2479 
2480 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2481 	sw = hw->switch_info;
2482 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2483 	if (!rule_head)
2484 		return false;
2485 
2486 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2487 	mutex_lock(rule_lock);
2488 	list_for_each_entry(entry, rule_head, list_entry) {
2489 		struct ice_fltr_info *f_info = &entry->fltr_info;
2490 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2491 
2492 		if (is_zero_ether_addr(mac_addr))
2493 			continue;
2494 
2495 		if (f_info->flag != ICE_FLTR_TX ||
2496 		    f_info->src_id != ICE_SRC_ID_VSI ||
2497 		    f_info->lkup_type != ICE_SW_LKUP_MAC ||
2498 		    f_info->fltr_act != ICE_FWD_TO_VSI ||
2499 		    hw_vsi_id != f_info->fwd_id.hw_vsi_id)
2500 			continue;
2501 
2502 		if (ether_addr_equal(mac, mac_addr)) {
2503 			mutex_unlock(rule_lock);
2504 			return true;
2505 		}
2506 	}
2507 	mutex_unlock(rule_lock);
2508 	return false;
2509 }
2510 
2511 /**
2512  * ice_vlan_fltr_exist - does this VLAN filter exist for given VSI
2513  * @hw: pointer to the hardware structure
2514  * @vlan_id: VLAN ID
2515  * @vsi_handle: check MAC filter for this VSI
2516  */
2517 bool ice_vlan_fltr_exist(struct ice_hw *hw, u16 vlan_id, u16 vsi_handle)
2518 {
2519 	struct ice_fltr_mgmt_list_entry *entry;
2520 	struct list_head *rule_head;
2521 	struct ice_switch_info *sw;
2522 	struct mutex *rule_lock; /* Lock to protect filter rule list */
2523 	u16 hw_vsi_id;
2524 
2525 	if (vlan_id > ICE_MAX_VLAN_ID)
2526 		return false;
2527 
2528 	if (!ice_is_vsi_valid(hw, vsi_handle))
2529 		return false;
2530 
2531 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2532 	sw = hw->switch_info;
2533 	rule_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
2534 	if (!rule_head)
2535 		return false;
2536 
2537 	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2538 	mutex_lock(rule_lock);
2539 	list_for_each_entry(entry, rule_head, list_entry) {
2540 		struct ice_fltr_info *f_info = &entry->fltr_info;
2541 		u16 entry_vlan_id = f_info->l_data.vlan.vlan_id;
2542 		struct ice_vsi_list_map_info *map_info;
2543 
2544 		if (entry_vlan_id > ICE_MAX_VLAN_ID)
2545 			continue;
2546 
2547 		if (f_info->flag != ICE_FLTR_TX ||
2548 		    f_info->src_id != ICE_SRC_ID_VSI ||
2549 		    f_info->lkup_type != ICE_SW_LKUP_VLAN)
2550 			continue;
2551 
2552 		/* Only allowed filter action are FWD_TO_VSI/_VSI_LIST */
2553 		if (f_info->fltr_act != ICE_FWD_TO_VSI &&
2554 		    f_info->fltr_act != ICE_FWD_TO_VSI_LIST)
2555 			continue;
2556 
2557 		if (f_info->fltr_act == ICE_FWD_TO_VSI) {
2558 			if (hw_vsi_id != f_info->fwd_id.hw_vsi_id)
2559 				continue;
2560 		} else if (f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
2561 			/* If filter_action is FWD_TO_VSI_LIST, make sure
2562 			 * that VSI being checked is part of VSI list
2563 			 */
2564 			if (entry->vsi_count == 1 &&
2565 			    entry->vsi_list_info) {
2566 				map_info = entry->vsi_list_info;
2567 				if (!test_bit(vsi_handle, map_info->vsi_map))
2568 					continue;
2569 			}
2570 		}
2571 
2572 		if (vlan_id == entry_vlan_id) {
2573 			mutex_unlock(rule_lock);
2574 			return true;
2575 		}
2576 	}
2577 	mutex_unlock(rule_lock);
2578 
2579 	return false;
2580 }
2581 
2582 /**
2583  * ice_add_mac - Add a MAC address based filter rule
2584  * @hw: pointer to the hardware structure
2585  * @m_list: list of MAC addresses and forwarding information
2586  *
2587  * IMPORTANT: When the ucast_shared flag is set to false and m_list has
2588  * multiple unicast addresses, the function assumes that all the
2589  * addresses are unique in a given add_mac call. It doesn't
2590  * check for duplicates in this case, removing duplicates from a given
2591  * list should be taken care of in the caller of this function.
2592  */
2593 enum ice_status ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
2594 {
2595 	struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
2596 	struct ice_fltr_list_entry *m_list_itr;
2597 	struct list_head *rule_head;
2598 	u16 total_elem_left, s_rule_size;
2599 	struct ice_switch_info *sw;
2600 	struct mutex *rule_lock; /* Lock to protect filter rule list */
2601 	enum ice_status status = 0;
2602 	u16 num_unicast = 0;
2603 	u8 elem_sent;
2604 
2605 	if (!m_list || !hw)
2606 		return ICE_ERR_PARAM;
2607 
2608 	s_rule = NULL;
2609 	sw = hw->switch_info;
2610 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
2611 	list_for_each_entry(m_list_itr, m_list, list_entry) {
2612 		u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
2613 		u16 vsi_handle;
2614 		u16 hw_vsi_id;
2615 
2616 		m_list_itr->fltr_info.flag = ICE_FLTR_TX;
2617 		vsi_handle = m_list_itr->fltr_info.vsi_handle;
2618 		if (!ice_is_vsi_valid(hw, vsi_handle))
2619 			return ICE_ERR_PARAM;
2620 		hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2621 		m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
2622 		/* update the src in case it is VSI num */
2623 		if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
2624 			return ICE_ERR_PARAM;
2625 		m_list_itr->fltr_info.src = hw_vsi_id;
2626 		if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
2627 		    is_zero_ether_addr(add))
2628 			return ICE_ERR_PARAM;
2629 		if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
2630 			/* Don't overwrite the unicast address */
2631 			mutex_lock(rule_lock);
2632 			if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
2633 						&m_list_itr->fltr_info)) {
2634 				mutex_unlock(rule_lock);
2635 				return ICE_ERR_ALREADY_EXISTS;
2636 			}
2637 			mutex_unlock(rule_lock);
2638 			num_unicast++;
2639 		} else if (is_multicast_ether_addr(add) ||
2640 			   (is_unicast_ether_addr(add) && hw->ucast_shared)) {
2641 			m_list_itr->status =
2642 				ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
2643 						      m_list_itr);
2644 			if (m_list_itr->status)
2645 				return m_list_itr->status;
2646 		}
2647 	}
2648 
2649 	mutex_lock(rule_lock);
2650 	/* Exit if no suitable entries were found for adding bulk switch rule */
2651 	if (!num_unicast) {
2652 		status = 0;
2653 		goto ice_add_mac_exit;
2654 	}
2655 
2656 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
2657 
2658 	/* Allocate switch rule buffer for the bulk update for unicast */
2659 	s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
2660 	s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
2661 			      GFP_KERNEL);
2662 	if (!s_rule) {
2663 		status = ICE_ERR_NO_MEMORY;
2664 		goto ice_add_mac_exit;
2665 	}
2666 
2667 	r_iter = s_rule;
2668 	list_for_each_entry(m_list_itr, m_list, list_entry) {
2669 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2670 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2671 
2672 		if (is_unicast_ether_addr(mac_addr)) {
2673 			ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
2674 					 ice_aqc_opc_add_sw_rules);
2675 			r_iter = (struct ice_aqc_sw_rules_elem *)
2676 				((u8 *)r_iter + s_rule_size);
2677 		}
2678 	}
2679 
2680 	/* Call AQ bulk switch rule update for all unicast addresses */
2681 	r_iter = s_rule;
2682 	/* Call AQ switch rule in AQ_MAX chunk */
2683 	for (total_elem_left = num_unicast; total_elem_left > 0;
2684 	     total_elem_left -= elem_sent) {
2685 		struct ice_aqc_sw_rules_elem *entry = r_iter;
2686 
2687 		elem_sent = min_t(u8, total_elem_left,
2688 				  (ICE_AQ_MAX_BUF_LEN / s_rule_size));
2689 		status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
2690 					 elem_sent, ice_aqc_opc_add_sw_rules,
2691 					 NULL);
2692 		if (status)
2693 			goto ice_add_mac_exit;
2694 		r_iter = (struct ice_aqc_sw_rules_elem *)
2695 			((u8 *)r_iter + (elem_sent * s_rule_size));
2696 	}
2697 
2698 	/* Fill up rule ID based on the value returned from FW */
2699 	r_iter = s_rule;
2700 	list_for_each_entry(m_list_itr, m_list, list_entry) {
2701 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2702 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2703 		struct ice_fltr_mgmt_list_entry *fm_entry;
2704 
2705 		if (is_unicast_ether_addr(mac_addr)) {
2706 			f_info->fltr_rule_id =
2707 				le16_to_cpu(r_iter->pdata.lkup_tx_rx.index);
2708 			f_info->fltr_act = ICE_FWD_TO_VSI;
2709 			/* Create an entry to track this MAC address */
2710 			fm_entry = devm_kzalloc(ice_hw_to_dev(hw),
2711 						sizeof(*fm_entry), GFP_KERNEL);
2712 			if (!fm_entry) {
2713 				status = ICE_ERR_NO_MEMORY;
2714 				goto ice_add_mac_exit;
2715 			}
2716 			fm_entry->fltr_info = *f_info;
2717 			fm_entry->vsi_count = 1;
2718 			/* The book keeping entries will get removed when
2719 			 * base driver calls remove filter AQ command
2720 			 */
2721 
2722 			list_add(&fm_entry->list_entry, rule_head);
2723 			r_iter = (struct ice_aqc_sw_rules_elem *)
2724 				((u8 *)r_iter + s_rule_size);
2725 		}
2726 	}
2727 
2728 ice_add_mac_exit:
2729 	mutex_unlock(rule_lock);
2730 	if (s_rule)
2731 		devm_kfree(ice_hw_to_dev(hw), s_rule);
2732 	return status;
2733 }
2734 
2735 /**
2736  * ice_add_vlan_internal - Add one VLAN based filter rule
2737  * @hw: pointer to the hardware structure
2738  * @f_entry: filter entry containing one VLAN information
2739  */
2740 static enum ice_status
2741 ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
2742 {
2743 	struct ice_switch_info *sw = hw->switch_info;
2744 	struct ice_fltr_mgmt_list_entry *v_list_itr;
2745 	struct ice_fltr_info *new_fltr, *cur_fltr;
2746 	enum ice_sw_lkup_type lkup_type;
2747 	u16 vsi_list_id = 0, vsi_handle;
2748 	struct mutex *rule_lock; /* Lock to protect filter rule list */
2749 	enum ice_status status = 0;
2750 
2751 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2752 		return ICE_ERR_PARAM;
2753 
2754 	f_entry->fltr_info.fwd_id.hw_vsi_id =
2755 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2756 	new_fltr = &f_entry->fltr_info;
2757 
2758 	/* VLAN ID should only be 12 bits */
2759 	if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
2760 		return ICE_ERR_PARAM;
2761 
2762 	if (new_fltr->src_id != ICE_SRC_ID_VSI)
2763 		return ICE_ERR_PARAM;
2764 
2765 	new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
2766 	lkup_type = new_fltr->lkup_type;
2767 	vsi_handle = new_fltr->vsi_handle;
2768 	rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
2769 	mutex_lock(rule_lock);
2770 	v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
2771 	if (!v_list_itr) {
2772 		struct ice_vsi_list_map_info *map_info = NULL;
2773 
2774 		if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
2775 			/* All VLAN pruning rules use a VSI list. Check if
2776 			 * there is already a VSI list containing VSI that we
2777 			 * want to add. If found, use the same vsi_list_id for
2778 			 * this new VLAN rule or else create a new list.
2779 			 */
2780 			map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
2781 							   vsi_handle,
2782 							   &vsi_list_id);
2783 			if (!map_info) {
2784 				status = ice_create_vsi_list_rule(hw,
2785 								  &vsi_handle,
2786 								  1,
2787 								  &vsi_list_id,
2788 								  lkup_type);
2789 				if (status)
2790 					goto exit;
2791 			}
2792 			/* Convert the action to forwarding to a VSI list. */
2793 			new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2794 			new_fltr->fwd_id.vsi_list_id = vsi_list_id;
2795 		}
2796 
2797 		status = ice_create_pkt_fwd_rule(hw, f_entry);
2798 		if (!status) {
2799 			v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
2800 							 new_fltr);
2801 			if (!v_list_itr) {
2802 				status = ICE_ERR_DOES_NOT_EXIST;
2803 				goto exit;
2804 			}
2805 			/* reuse VSI list for new rule and increment ref_cnt */
2806 			if (map_info) {
2807 				v_list_itr->vsi_list_info = map_info;
2808 				map_info->ref_cnt++;
2809 			} else {
2810 				v_list_itr->vsi_list_info =
2811 					ice_create_vsi_list_map(hw, &vsi_handle,
2812 								1, vsi_list_id);
2813 			}
2814 		}
2815 	} else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
2816 		/* Update existing VSI list to add new VSI ID only if it used
2817 		 * by one VLAN rule.
2818 		 */
2819 		cur_fltr = &v_list_itr->fltr_info;
2820 		status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
2821 						 new_fltr);
2822 	} else {
2823 		/* If VLAN rule exists and VSI list being used by this rule is
2824 		 * referenced by more than 1 VLAN rule. Then create a new VSI
2825 		 * list appending previous VSI with new VSI and update existing
2826 		 * VLAN rule to point to new VSI list ID
2827 		 */
2828 		struct ice_fltr_info tmp_fltr;
2829 		u16 vsi_handle_arr[2];
2830 		u16 cur_handle;
2831 
2832 		/* Current implementation only supports reusing VSI list with
2833 		 * one VSI count. We should never hit below condition
2834 		 */
2835 		if (v_list_itr->vsi_count > 1 &&
2836 		    v_list_itr->vsi_list_info->ref_cnt > 1) {
2837 			ice_debug(hw, ICE_DBG_SW, "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
2838 			status = ICE_ERR_CFG;
2839 			goto exit;
2840 		}
2841 
2842 		cur_handle =
2843 			find_first_bit(v_list_itr->vsi_list_info->vsi_map,
2844 				       ICE_MAX_VSI);
2845 
2846 		/* A rule already exists with the new VSI being added */
2847 		if (cur_handle == vsi_handle) {
2848 			status = ICE_ERR_ALREADY_EXISTS;
2849 			goto exit;
2850 		}
2851 
2852 		vsi_handle_arr[0] = cur_handle;
2853 		vsi_handle_arr[1] = vsi_handle;
2854 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2855 						  &vsi_list_id, lkup_type);
2856 		if (status)
2857 			goto exit;
2858 
2859 		tmp_fltr = v_list_itr->fltr_info;
2860 		tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
2861 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2862 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2863 		/* Update the previous switch rule to a new VSI list which
2864 		 * includes current VSI that is requested
2865 		 */
2866 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2867 		if (status)
2868 			goto exit;
2869 
2870 		/* before overriding VSI list map info. decrement ref_cnt of
2871 		 * previous VSI list
2872 		 */
2873 		v_list_itr->vsi_list_info->ref_cnt--;
2874 
2875 		/* now update to newly created list */
2876 		v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
2877 		v_list_itr->vsi_list_info =
2878 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2879 						vsi_list_id);
2880 		v_list_itr->vsi_count++;
2881 	}
2882 
2883 exit:
2884 	mutex_unlock(rule_lock);
2885 	return status;
2886 }
2887 
2888 /**
2889  * ice_add_vlan - Add VLAN based filter rule
2890  * @hw: pointer to the hardware structure
2891  * @v_list: list of VLAN entries and forwarding information
2892  */
2893 enum ice_status ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
2894 {
2895 	struct ice_fltr_list_entry *v_list_itr;
2896 
2897 	if (!v_list || !hw)
2898 		return ICE_ERR_PARAM;
2899 
2900 	list_for_each_entry(v_list_itr, v_list, list_entry) {
2901 		if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
2902 			return ICE_ERR_PARAM;
2903 		v_list_itr->fltr_info.flag = ICE_FLTR_TX;
2904 		v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
2905 		if (v_list_itr->status)
2906 			return v_list_itr->status;
2907 	}
2908 	return 0;
2909 }
2910 
2911 /**
2912  * ice_add_eth_mac - Add ethertype and MAC based filter rule
2913  * @hw: pointer to the hardware structure
2914  * @em_list: list of ether type MAC filter, MAC is optional
2915  *
2916  * This function requires the caller to populate the entries in
2917  * the filter list with the necessary fields (including flags to
2918  * indicate Tx or Rx rules).
2919  */
2920 enum ice_status
2921 ice_add_eth_mac(struct ice_hw *hw, struct list_head *em_list)
2922 {
2923 	struct ice_fltr_list_entry *em_list_itr;
2924 
2925 	if (!em_list || !hw)
2926 		return ICE_ERR_PARAM;
2927 
2928 	list_for_each_entry(em_list_itr, em_list, list_entry) {
2929 		enum ice_sw_lkup_type l_type =
2930 			em_list_itr->fltr_info.lkup_type;
2931 
2932 		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2933 		    l_type != ICE_SW_LKUP_ETHERTYPE)
2934 			return ICE_ERR_PARAM;
2935 
2936 		em_list_itr->status = ice_add_rule_internal(hw, l_type,
2937 							    em_list_itr);
2938 		if (em_list_itr->status)
2939 			return em_list_itr->status;
2940 	}
2941 	return 0;
2942 }
2943 
2944 /**
2945  * ice_remove_eth_mac - Remove an ethertype (or MAC) based filter rule
2946  * @hw: pointer to the hardware structure
2947  * @em_list: list of ethertype or ethertype MAC entries
2948  */
2949 enum ice_status
2950 ice_remove_eth_mac(struct ice_hw *hw, struct list_head *em_list)
2951 {
2952 	struct ice_fltr_list_entry *em_list_itr, *tmp;
2953 
2954 	if (!em_list || !hw)
2955 		return ICE_ERR_PARAM;
2956 
2957 	list_for_each_entry_safe(em_list_itr, tmp, em_list, list_entry) {
2958 		enum ice_sw_lkup_type l_type =
2959 			em_list_itr->fltr_info.lkup_type;
2960 
2961 		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2962 		    l_type != ICE_SW_LKUP_ETHERTYPE)
2963 			return ICE_ERR_PARAM;
2964 
2965 		em_list_itr->status = ice_remove_rule_internal(hw, l_type,
2966 							       em_list_itr);
2967 		if (em_list_itr->status)
2968 			return em_list_itr->status;
2969 	}
2970 	return 0;
2971 }
2972 
2973 /**
2974  * ice_rem_sw_rule_info
2975  * @hw: pointer to the hardware structure
2976  * @rule_head: pointer to the switch list structure that we want to delete
2977  */
2978 static void
2979 ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
2980 {
2981 	if (!list_empty(rule_head)) {
2982 		struct ice_fltr_mgmt_list_entry *entry;
2983 		struct ice_fltr_mgmt_list_entry *tmp;
2984 
2985 		list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
2986 			list_del(&entry->list_entry);
2987 			devm_kfree(ice_hw_to_dev(hw), entry);
2988 		}
2989 	}
2990 }
2991 
2992 /**
2993  * ice_rem_adv_rule_info
2994  * @hw: pointer to the hardware structure
2995  * @rule_head: pointer to the switch list structure that we want to delete
2996  */
2997 static void
2998 ice_rem_adv_rule_info(struct ice_hw *hw, struct list_head *rule_head)
2999 {
3000 	struct ice_adv_fltr_mgmt_list_entry *tmp_entry;
3001 	struct ice_adv_fltr_mgmt_list_entry *lst_itr;
3002 
3003 	if (list_empty(rule_head))
3004 		return;
3005 
3006 	list_for_each_entry_safe(lst_itr, tmp_entry, rule_head, list_entry) {
3007 		list_del(&lst_itr->list_entry);
3008 		devm_kfree(ice_hw_to_dev(hw), lst_itr->lkups);
3009 		devm_kfree(ice_hw_to_dev(hw), lst_itr);
3010 	}
3011 }
3012 
3013 /**
3014  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
3015  * @hw: pointer to the hardware structure
3016  * @vsi_handle: VSI handle to set as default
3017  * @set: true to add the above mentioned switch rule, false to remove it
3018  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
3019  *
3020  * add filter rule to set/unset given VSI as default VSI for the switch
3021  * (represented by swid)
3022  */
3023 enum ice_status
3024 ice_cfg_dflt_vsi(struct ice_hw *hw, u16 vsi_handle, bool set, u8 direction)
3025 {
3026 	struct ice_aqc_sw_rules_elem *s_rule;
3027 	struct ice_fltr_info f_info;
3028 	enum ice_adminq_opc opcode;
3029 	enum ice_status status;
3030 	u16 s_rule_size;
3031 	u16 hw_vsi_id;
3032 
3033 	if (!ice_is_vsi_valid(hw, vsi_handle))
3034 		return ICE_ERR_PARAM;
3035 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3036 
3037 	s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
3038 		ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
3039 
3040 	s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
3041 	if (!s_rule)
3042 		return ICE_ERR_NO_MEMORY;
3043 
3044 	memset(&f_info, 0, sizeof(f_info));
3045 
3046 	f_info.lkup_type = ICE_SW_LKUP_DFLT;
3047 	f_info.flag = direction;
3048 	f_info.fltr_act = ICE_FWD_TO_VSI;
3049 	f_info.fwd_id.hw_vsi_id = hw_vsi_id;
3050 
3051 	if (f_info.flag & ICE_FLTR_RX) {
3052 		f_info.src = hw->port_info->lport;
3053 		f_info.src_id = ICE_SRC_ID_LPORT;
3054 		if (!set)
3055 			f_info.fltr_rule_id =
3056 				hw->port_info->dflt_rx_vsi_rule_id;
3057 	} else if (f_info.flag & ICE_FLTR_TX) {
3058 		f_info.src_id = ICE_SRC_ID_VSI;
3059 		f_info.src = hw_vsi_id;
3060 		if (!set)
3061 			f_info.fltr_rule_id =
3062 				hw->port_info->dflt_tx_vsi_rule_id;
3063 	}
3064 
3065 	if (set)
3066 		opcode = ice_aqc_opc_add_sw_rules;
3067 	else
3068 		opcode = ice_aqc_opc_remove_sw_rules;
3069 
3070 	ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
3071 
3072 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
3073 	if (status || !(f_info.flag & ICE_FLTR_TX_RX))
3074 		goto out;
3075 	if (set) {
3076 		u16 index = le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
3077 
3078 		if (f_info.flag & ICE_FLTR_TX) {
3079 			hw->port_info->dflt_tx_vsi_num = hw_vsi_id;
3080 			hw->port_info->dflt_tx_vsi_rule_id = index;
3081 		} else if (f_info.flag & ICE_FLTR_RX) {
3082 			hw->port_info->dflt_rx_vsi_num = hw_vsi_id;
3083 			hw->port_info->dflt_rx_vsi_rule_id = index;
3084 		}
3085 	} else {
3086 		if (f_info.flag & ICE_FLTR_TX) {
3087 			hw->port_info->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
3088 			hw->port_info->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
3089 		} else if (f_info.flag & ICE_FLTR_RX) {
3090 			hw->port_info->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
3091 			hw->port_info->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
3092 		}
3093 	}
3094 
3095 out:
3096 	devm_kfree(ice_hw_to_dev(hw), s_rule);
3097 	return status;
3098 }
3099 
3100 /**
3101  * ice_find_ucast_rule_entry - Search for a unicast MAC filter rule entry
3102  * @hw: pointer to the hardware structure
3103  * @recp_id: lookup type for which the specified rule needs to be searched
3104  * @f_info: rule information
3105  *
3106  * Helper function to search for a unicast rule entry - this is to be used
3107  * to remove unicast MAC filter that is not shared with other VSIs on the
3108  * PF switch.
3109  *
3110  * Returns pointer to entry storing the rule if found
3111  */
3112 static struct ice_fltr_mgmt_list_entry *
3113 ice_find_ucast_rule_entry(struct ice_hw *hw, u8 recp_id,
3114 			  struct ice_fltr_info *f_info)
3115 {
3116 	struct ice_switch_info *sw = hw->switch_info;
3117 	struct ice_fltr_mgmt_list_entry *list_itr;
3118 	struct list_head *list_head;
3119 
3120 	list_head = &sw->recp_list[recp_id].filt_rules;
3121 	list_for_each_entry(list_itr, list_head, list_entry) {
3122 		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
3123 			    sizeof(f_info->l_data)) &&
3124 		    f_info->fwd_id.hw_vsi_id ==
3125 		    list_itr->fltr_info.fwd_id.hw_vsi_id &&
3126 		    f_info->flag == list_itr->fltr_info.flag)
3127 			return list_itr;
3128 	}
3129 	return NULL;
3130 }
3131 
3132 /**
3133  * ice_remove_mac - remove a MAC address based filter rule
3134  * @hw: pointer to the hardware structure
3135  * @m_list: list of MAC addresses and forwarding information
3136  *
3137  * This function removes either a MAC filter rule or a specific VSI from a
3138  * VSI list for a multicast MAC address.
3139  *
3140  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
3141  * ice_add_mac. Caller should be aware that this call will only work if all
3142  * the entries passed into m_list were added previously. It will not attempt to
3143  * do a partial remove of entries that were found.
3144  */
3145 enum ice_status ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
3146 {
3147 	struct ice_fltr_list_entry *list_itr, *tmp;
3148 	struct mutex *rule_lock; /* Lock to protect filter rule list */
3149 
3150 	if (!m_list)
3151 		return ICE_ERR_PARAM;
3152 
3153 	rule_lock = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
3154 	list_for_each_entry_safe(list_itr, tmp, m_list, list_entry) {
3155 		enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
3156 		u8 *add = &list_itr->fltr_info.l_data.mac.mac_addr[0];
3157 		u16 vsi_handle;
3158 
3159 		if (l_type != ICE_SW_LKUP_MAC)
3160 			return ICE_ERR_PARAM;
3161 
3162 		vsi_handle = list_itr->fltr_info.vsi_handle;
3163 		if (!ice_is_vsi_valid(hw, vsi_handle))
3164 			return ICE_ERR_PARAM;
3165 
3166 		list_itr->fltr_info.fwd_id.hw_vsi_id =
3167 					ice_get_hw_vsi_num(hw, vsi_handle);
3168 		if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
3169 			/* Don't remove the unicast address that belongs to
3170 			 * another VSI on the switch, since it is not being
3171 			 * shared...
3172 			 */
3173 			mutex_lock(rule_lock);
3174 			if (!ice_find_ucast_rule_entry(hw, ICE_SW_LKUP_MAC,
3175 						       &list_itr->fltr_info)) {
3176 				mutex_unlock(rule_lock);
3177 				return ICE_ERR_DOES_NOT_EXIST;
3178 			}
3179 			mutex_unlock(rule_lock);
3180 		}
3181 		list_itr->status = ice_remove_rule_internal(hw,
3182 							    ICE_SW_LKUP_MAC,
3183 							    list_itr);
3184 		if (list_itr->status)
3185 			return list_itr->status;
3186 	}
3187 	return 0;
3188 }
3189 
3190 /**
3191  * ice_remove_vlan - Remove VLAN based filter rule
3192  * @hw: pointer to the hardware structure
3193  * @v_list: list of VLAN entries and forwarding information
3194  */
3195 enum ice_status
3196 ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
3197 {
3198 	struct ice_fltr_list_entry *v_list_itr, *tmp;
3199 
3200 	if (!v_list || !hw)
3201 		return ICE_ERR_PARAM;
3202 
3203 	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
3204 		enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
3205 
3206 		if (l_type != ICE_SW_LKUP_VLAN)
3207 			return ICE_ERR_PARAM;
3208 		v_list_itr->status = ice_remove_rule_internal(hw,
3209 							      ICE_SW_LKUP_VLAN,
3210 							      v_list_itr);
3211 		if (v_list_itr->status)
3212 			return v_list_itr->status;
3213 	}
3214 	return 0;
3215 }
3216 
3217 /**
3218  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
3219  * @fm_entry: filter entry to inspect
3220  * @vsi_handle: VSI handle to compare with filter info
3221  */
3222 static bool
3223 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
3224 {
3225 	return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
3226 		 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
3227 		(fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
3228 		 fm_entry->vsi_list_info &&
3229 		 (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
3230 }
3231 
3232 /**
3233  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
3234  * @hw: pointer to the hardware structure
3235  * @vsi_handle: VSI handle to remove filters from
3236  * @vsi_list_head: pointer to the list to add entry to
3237  * @fi: pointer to fltr_info of filter entry to copy & add
3238  *
3239  * Helper function, used when creating a list of filters to remove from
3240  * a specific VSI. The entry added to vsi_list_head is a COPY of the
3241  * original filter entry, with the exception of fltr_info.fltr_act and
3242  * fltr_info.fwd_id fields. These are set such that later logic can
3243  * extract which VSI to remove the fltr from, and pass on that information.
3244  */
3245 static enum ice_status
3246 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3247 			       struct list_head *vsi_list_head,
3248 			       struct ice_fltr_info *fi)
3249 {
3250 	struct ice_fltr_list_entry *tmp;
3251 
3252 	/* this memory is freed up in the caller function
3253 	 * once filters for this VSI are removed
3254 	 */
3255 	tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
3256 	if (!tmp)
3257 		return ICE_ERR_NO_MEMORY;
3258 
3259 	tmp->fltr_info = *fi;
3260 
3261 	/* Overwrite these fields to indicate which VSI to remove filter from,
3262 	 * so find and remove logic can extract the information from the
3263 	 * list entries. Note that original entries will still have proper
3264 	 * values.
3265 	 */
3266 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
3267 	tmp->fltr_info.vsi_handle = vsi_handle;
3268 	tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3269 
3270 	list_add(&tmp->list_entry, vsi_list_head);
3271 
3272 	return 0;
3273 }
3274 
3275 /**
3276  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
3277  * @hw: pointer to the hardware structure
3278  * @vsi_handle: VSI handle to remove filters from
3279  * @lkup_list_head: pointer to the list that has certain lookup type filters
3280  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
3281  *
3282  * Locates all filters in lkup_list_head that are used by the given VSI,
3283  * and adds COPIES of those entries to vsi_list_head (intended to be used
3284  * to remove the listed filters).
3285  * Note that this means all entries in vsi_list_head must be explicitly
3286  * deallocated by the caller when done with list.
3287  */
3288 static enum ice_status
3289 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3290 			 struct list_head *lkup_list_head,
3291 			 struct list_head *vsi_list_head)
3292 {
3293 	struct ice_fltr_mgmt_list_entry *fm_entry;
3294 	enum ice_status status = 0;
3295 
3296 	/* check to make sure VSI ID is valid and within boundary */
3297 	if (!ice_is_vsi_valid(hw, vsi_handle))
3298 		return ICE_ERR_PARAM;
3299 
3300 	list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
3301 		if (!ice_vsi_uses_fltr(fm_entry, vsi_handle))
3302 			continue;
3303 
3304 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3305 							vsi_list_head,
3306 							&fm_entry->fltr_info);
3307 		if (status)
3308 			return status;
3309 	}
3310 	return status;
3311 }
3312 
3313 /**
3314  * ice_determine_promisc_mask
3315  * @fi: filter info to parse
3316  *
3317  * Helper function to determine which ICE_PROMISC_ mask corresponds
3318  * to given filter into.
3319  */
3320 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
3321 {
3322 	u16 vid = fi->l_data.mac_vlan.vlan_id;
3323 	u8 *macaddr = fi->l_data.mac.mac_addr;
3324 	bool is_tx_fltr = false;
3325 	u8 promisc_mask = 0;
3326 
3327 	if (fi->flag == ICE_FLTR_TX)
3328 		is_tx_fltr = true;
3329 
3330 	if (is_broadcast_ether_addr(macaddr))
3331 		promisc_mask |= is_tx_fltr ?
3332 			ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
3333 	else if (is_multicast_ether_addr(macaddr))
3334 		promisc_mask |= is_tx_fltr ?
3335 			ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
3336 	else if (is_unicast_ether_addr(macaddr))
3337 		promisc_mask |= is_tx_fltr ?
3338 			ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
3339 	if (vid)
3340 		promisc_mask |= is_tx_fltr ?
3341 			ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
3342 
3343 	return promisc_mask;
3344 }
3345 
3346 /**
3347  * ice_remove_promisc - Remove promisc based filter rules
3348  * @hw: pointer to the hardware structure
3349  * @recp_id: recipe ID for which the rule needs to removed
3350  * @v_list: list of promisc entries
3351  */
3352 static enum ice_status
3353 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
3354 		   struct list_head *v_list)
3355 {
3356 	struct ice_fltr_list_entry *v_list_itr, *tmp;
3357 
3358 	list_for_each_entry_safe(v_list_itr, tmp, v_list, list_entry) {
3359 		v_list_itr->status =
3360 			ice_remove_rule_internal(hw, recp_id, v_list_itr);
3361 		if (v_list_itr->status)
3362 			return v_list_itr->status;
3363 	}
3364 	return 0;
3365 }
3366 
3367 /**
3368  * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
3369  * @hw: pointer to the hardware structure
3370  * @vsi_handle: VSI handle to clear mode
3371  * @promisc_mask: mask of promiscuous config bits to clear
3372  * @vid: VLAN ID to clear VLAN promiscuous
3373  */
3374 enum ice_status
3375 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3376 		      u16 vid)
3377 {
3378 	struct ice_switch_info *sw = hw->switch_info;
3379 	struct ice_fltr_list_entry *fm_entry, *tmp;
3380 	struct list_head remove_list_head;
3381 	struct ice_fltr_mgmt_list_entry *itr;
3382 	struct list_head *rule_head;
3383 	struct mutex *rule_lock;	/* Lock to protect filter rule list */
3384 	enum ice_status status = 0;
3385 	u8 recipe_id;
3386 
3387 	if (!ice_is_vsi_valid(hw, vsi_handle))
3388 		return ICE_ERR_PARAM;
3389 
3390 	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX))
3391 		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3392 	else
3393 		recipe_id = ICE_SW_LKUP_PROMISC;
3394 
3395 	rule_head = &sw->recp_list[recipe_id].filt_rules;
3396 	rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
3397 
3398 	INIT_LIST_HEAD(&remove_list_head);
3399 
3400 	mutex_lock(rule_lock);
3401 	list_for_each_entry(itr, rule_head, list_entry) {
3402 		struct ice_fltr_info *fltr_info;
3403 		u8 fltr_promisc_mask = 0;
3404 
3405 		if (!ice_vsi_uses_fltr(itr, vsi_handle))
3406 			continue;
3407 		fltr_info = &itr->fltr_info;
3408 
3409 		if (recipe_id == ICE_SW_LKUP_PROMISC_VLAN &&
3410 		    vid != fltr_info->l_data.mac_vlan.vlan_id)
3411 			continue;
3412 
3413 		fltr_promisc_mask |= ice_determine_promisc_mask(fltr_info);
3414 
3415 		/* Skip if filter is not completely specified by given mask */
3416 		if (fltr_promisc_mask & ~promisc_mask)
3417 			continue;
3418 
3419 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3420 							&remove_list_head,
3421 							fltr_info);
3422 		if (status) {
3423 			mutex_unlock(rule_lock);
3424 			goto free_fltr_list;
3425 		}
3426 	}
3427 	mutex_unlock(rule_lock);
3428 
3429 	status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
3430 
3431 free_fltr_list:
3432 	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
3433 		list_del(&fm_entry->list_entry);
3434 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
3435 	}
3436 
3437 	return status;
3438 }
3439 
3440 /**
3441  * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
3442  * @hw: pointer to the hardware structure
3443  * @vsi_handle: VSI handle to configure
3444  * @promisc_mask: mask of promiscuous config bits
3445  * @vid: VLAN ID to set VLAN promiscuous
3446  */
3447 enum ice_status
3448 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask, u16 vid)
3449 {
3450 	enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
3451 	struct ice_fltr_list_entry f_list_entry;
3452 	struct ice_fltr_info new_fltr;
3453 	enum ice_status status = 0;
3454 	bool is_tx_fltr;
3455 	u16 hw_vsi_id;
3456 	int pkt_type;
3457 	u8 recipe_id;
3458 
3459 	if (!ice_is_vsi_valid(hw, vsi_handle))
3460 		return ICE_ERR_PARAM;
3461 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3462 
3463 	memset(&new_fltr, 0, sizeof(new_fltr));
3464 
3465 	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
3466 		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
3467 		new_fltr.l_data.mac_vlan.vlan_id = vid;
3468 		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3469 	} else {
3470 		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
3471 		recipe_id = ICE_SW_LKUP_PROMISC;
3472 	}
3473 
3474 	/* Separate filters must be set for each direction/packet type
3475 	 * combination, so we will loop over the mask value, store the
3476 	 * individual type, and clear it out in the input mask as it
3477 	 * is found.
3478 	 */
3479 	while (promisc_mask) {
3480 		u8 *mac_addr;
3481 
3482 		pkt_type = 0;
3483 		is_tx_fltr = false;
3484 
3485 		if (promisc_mask & ICE_PROMISC_UCAST_RX) {
3486 			promisc_mask &= ~ICE_PROMISC_UCAST_RX;
3487 			pkt_type = UCAST_FLTR;
3488 		} else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
3489 			promisc_mask &= ~ICE_PROMISC_UCAST_TX;
3490 			pkt_type = UCAST_FLTR;
3491 			is_tx_fltr = true;
3492 		} else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
3493 			promisc_mask &= ~ICE_PROMISC_MCAST_RX;
3494 			pkt_type = MCAST_FLTR;
3495 		} else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
3496 			promisc_mask &= ~ICE_PROMISC_MCAST_TX;
3497 			pkt_type = MCAST_FLTR;
3498 			is_tx_fltr = true;
3499 		} else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
3500 			promisc_mask &= ~ICE_PROMISC_BCAST_RX;
3501 			pkt_type = BCAST_FLTR;
3502 		} else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
3503 			promisc_mask &= ~ICE_PROMISC_BCAST_TX;
3504 			pkt_type = BCAST_FLTR;
3505 			is_tx_fltr = true;
3506 		}
3507 
3508 		/* Check for VLAN promiscuous flag */
3509 		if (promisc_mask & ICE_PROMISC_VLAN_RX) {
3510 			promisc_mask &= ~ICE_PROMISC_VLAN_RX;
3511 		} else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
3512 			promisc_mask &= ~ICE_PROMISC_VLAN_TX;
3513 			is_tx_fltr = true;
3514 		}
3515 
3516 		/* Set filter DA based on packet type */
3517 		mac_addr = new_fltr.l_data.mac.mac_addr;
3518 		if (pkt_type == BCAST_FLTR) {
3519 			eth_broadcast_addr(mac_addr);
3520 		} else if (pkt_type == MCAST_FLTR ||
3521 			   pkt_type == UCAST_FLTR) {
3522 			/* Use the dummy ether header DA */
3523 			ether_addr_copy(mac_addr, dummy_eth_header);
3524 			if (pkt_type == MCAST_FLTR)
3525 				mac_addr[0] |= 0x1;	/* Set multicast bit */
3526 		}
3527 
3528 		/* Need to reset this to zero for all iterations */
3529 		new_fltr.flag = 0;
3530 		if (is_tx_fltr) {
3531 			new_fltr.flag |= ICE_FLTR_TX;
3532 			new_fltr.src = hw_vsi_id;
3533 		} else {
3534 			new_fltr.flag |= ICE_FLTR_RX;
3535 			new_fltr.src = hw->port_info->lport;
3536 		}
3537 
3538 		new_fltr.fltr_act = ICE_FWD_TO_VSI;
3539 		new_fltr.vsi_handle = vsi_handle;
3540 		new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
3541 		f_list_entry.fltr_info = new_fltr;
3542 
3543 		status = ice_add_rule_internal(hw, recipe_id, &f_list_entry);
3544 		if (status)
3545 			goto set_promisc_exit;
3546 	}
3547 
3548 set_promisc_exit:
3549 	return status;
3550 }
3551 
3552 /**
3553  * ice_set_vlan_vsi_promisc
3554  * @hw: pointer to the hardware structure
3555  * @vsi_handle: VSI handle to configure
3556  * @promisc_mask: mask of promiscuous config bits
3557  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
3558  *
3559  * Configure VSI with all associated VLANs to given promiscuous mode(s)
3560  */
3561 enum ice_status
3562 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3563 			 bool rm_vlan_promisc)
3564 {
3565 	struct ice_switch_info *sw = hw->switch_info;
3566 	struct ice_fltr_list_entry *list_itr, *tmp;
3567 	struct list_head vsi_list_head;
3568 	struct list_head *vlan_head;
3569 	struct mutex *vlan_lock; /* Lock to protect filter rule list */
3570 	enum ice_status status;
3571 	u16 vlan_id;
3572 
3573 	INIT_LIST_HEAD(&vsi_list_head);
3574 	vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3575 	vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3576 	mutex_lock(vlan_lock);
3577 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
3578 					  &vsi_list_head);
3579 	mutex_unlock(vlan_lock);
3580 	if (status)
3581 		goto free_fltr_list;
3582 
3583 	list_for_each_entry(list_itr, &vsi_list_head, list_entry) {
3584 		vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
3585 		if (rm_vlan_promisc)
3586 			status = ice_clear_vsi_promisc(hw, vsi_handle,
3587 						       promisc_mask, vlan_id);
3588 		else
3589 			status = ice_set_vsi_promisc(hw, vsi_handle,
3590 						     promisc_mask, vlan_id);
3591 		if (status)
3592 			break;
3593 	}
3594 
3595 free_fltr_list:
3596 	list_for_each_entry_safe(list_itr, tmp, &vsi_list_head, list_entry) {
3597 		list_del(&list_itr->list_entry);
3598 		devm_kfree(ice_hw_to_dev(hw), list_itr);
3599 	}
3600 	return status;
3601 }
3602 
3603 /**
3604  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
3605  * @hw: pointer to the hardware structure
3606  * @vsi_handle: VSI handle to remove filters from
3607  * @lkup: switch rule filter lookup type
3608  */
3609 static void
3610 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
3611 			 enum ice_sw_lkup_type lkup)
3612 {
3613 	struct ice_switch_info *sw = hw->switch_info;
3614 	struct ice_fltr_list_entry *fm_entry;
3615 	struct list_head remove_list_head;
3616 	struct list_head *rule_head;
3617 	struct ice_fltr_list_entry *tmp;
3618 	struct mutex *rule_lock;	/* Lock to protect filter rule list */
3619 	enum ice_status status;
3620 
3621 	INIT_LIST_HEAD(&remove_list_head);
3622 	rule_lock = &sw->recp_list[lkup].filt_rule_lock;
3623 	rule_head = &sw->recp_list[lkup].filt_rules;
3624 	mutex_lock(rule_lock);
3625 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
3626 					  &remove_list_head);
3627 	mutex_unlock(rule_lock);
3628 	if (status)
3629 		goto free_fltr_list;
3630 
3631 	switch (lkup) {
3632 	case ICE_SW_LKUP_MAC:
3633 		ice_remove_mac(hw, &remove_list_head);
3634 		break;
3635 	case ICE_SW_LKUP_VLAN:
3636 		ice_remove_vlan(hw, &remove_list_head);
3637 		break;
3638 	case ICE_SW_LKUP_PROMISC:
3639 	case ICE_SW_LKUP_PROMISC_VLAN:
3640 		ice_remove_promisc(hw, lkup, &remove_list_head);
3641 		break;
3642 	case ICE_SW_LKUP_MAC_VLAN:
3643 	case ICE_SW_LKUP_ETHERTYPE:
3644 	case ICE_SW_LKUP_ETHERTYPE_MAC:
3645 	case ICE_SW_LKUP_DFLT:
3646 	case ICE_SW_LKUP_LAST:
3647 	default:
3648 		ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
3649 		break;
3650 	}
3651 
3652 free_fltr_list:
3653 	list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
3654 		list_del(&fm_entry->list_entry);
3655 		devm_kfree(ice_hw_to_dev(hw), fm_entry);
3656 	}
3657 }
3658 
3659 /**
3660  * ice_remove_vsi_fltr - Remove all filters for a VSI
3661  * @hw: pointer to the hardware structure
3662  * @vsi_handle: VSI handle to remove filters from
3663  */
3664 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
3665 {
3666 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
3667 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
3668 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
3669 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
3670 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
3671 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
3672 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
3673 	ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
3674 }
3675 
3676 /**
3677  * ice_alloc_res_cntr - allocating resource counter
3678  * @hw: pointer to the hardware structure
3679  * @type: type of resource
3680  * @alloc_shared: if set it is shared else dedicated
3681  * @num_items: number of entries requested for FD resource type
3682  * @counter_id: counter index returned by AQ call
3683  */
3684 enum ice_status
3685 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3686 		   u16 *counter_id)
3687 {
3688 	struct ice_aqc_alloc_free_res_elem *buf;
3689 	enum ice_status status;
3690 	u16 buf_len;
3691 
3692 	/* Allocate resource */
3693 	buf_len = struct_size(buf, elem, 1);
3694 	buf = kzalloc(buf_len, GFP_KERNEL);
3695 	if (!buf)
3696 		return ICE_ERR_NO_MEMORY;
3697 
3698 	buf->num_elems = cpu_to_le16(num_items);
3699 	buf->res_type = cpu_to_le16(((type << ICE_AQC_RES_TYPE_S) &
3700 				      ICE_AQC_RES_TYPE_M) | alloc_shared);
3701 
3702 	status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3703 				       ice_aqc_opc_alloc_res, NULL);
3704 	if (status)
3705 		goto exit;
3706 
3707 	*counter_id = le16_to_cpu(buf->elem[0].e.sw_resp);
3708 
3709 exit:
3710 	kfree(buf);
3711 	return status;
3712 }
3713 
3714 /**
3715  * ice_free_res_cntr - free resource counter
3716  * @hw: pointer to the hardware structure
3717  * @type: type of resource
3718  * @alloc_shared: if set it is shared else dedicated
3719  * @num_items: number of entries to be freed for FD resource type
3720  * @counter_id: counter ID resource which needs to be freed
3721  */
3722 enum ice_status
3723 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3724 		  u16 counter_id)
3725 {
3726 	struct ice_aqc_alloc_free_res_elem *buf;
3727 	enum ice_status status;
3728 	u16 buf_len;
3729 
3730 	/* Free resource */
3731 	buf_len = struct_size(buf, elem, 1);
3732 	buf = kzalloc(buf_len, GFP_KERNEL);
3733 	if (!buf)
3734 		return ICE_ERR_NO_MEMORY;
3735 
3736 	buf->num_elems = cpu_to_le16(num_items);
3737 	buf->res_type = cpu_to_le16(((type << ICE_AQC_RES_TYPE_S) &
3738 				      ICE_AQC_RES_TYPE_M) | alloc_shared);
3739 	buf->elem[0].e.sw_resp = cpu_to_le16(counter_id);
3740 
3741 	status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3742 				       ice_aqc_opc_free_res, NULL);
3743 	if (status)
3744 		ice_debug(hw, ICE_DBG_SW, "counter resource could not be freed\n");
3745 
3746 	kfree(buf);
3747 	return status;
3748 }
3749 
3750 /* This is mapping table entry that maps every word within a given protocol
3751  * structure to the real byte offset as per the specification of that
3752  * protocol header.
3753  * for example dst address is 3 words in ethertype header and corresponding
3754  * bytes are 0, 2, 3 in the actual packet header and src address is at 4, 6, 8
3755  * IMPORTANT: Every structure part of "ice_prot_hdr" union should have a
3756  * matching entry describing its field. This needs to be updated if new
3757  * structure is added to that union.
3758  */
3759 static const struct ice_prot_ext_tbl_entry ice_prot_ext[ICE_PROTOCOL_LAST] = {
3760 	{ ICE_MAC_OFOS,		{ 0, 2, 4, 6, 8, 10, 12 } },
3761 	{ ICE_MAC_IL,		{ 0, 2, 4, 6, 8, 10, 12 } },
3762 	{ ICE_ETYPE_OL,		{ 0 } },
3763 	{ ICE_VLAN_OFOS,	{ 2, 0 } },
3764 	{ ICE_IPV4_OFOS,	{ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 } },
3765 	{ ICE_IPV4_IL,		{ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 } },
3766 	{ ICE_IPV6_OFOS,	{ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
3767 				 26, 28, 30, 32, 34, 36, 38 } },
3768 	{ ICE_IPV6_IL,		{ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
3769 				 26, 28, 30, 32, 34, 36, 38 } },
3770 	{ ICE_TCP_IL,		{ 0, 2 } },
3771 	{ ICE_UDP_OF,		{ 0, 2 } },
3772 	{ ICE_UDP_ILOS,		{ 0, 2 } },
3773 	{ ICE_VXLAN,		{ 8, 10, 12, 14 } },
3774 	{ ICE_GENEVE,		{ 8, 10, 12, 14 } },
3775 	{ ICE_NVGRE,            { 0, 2, 4, 6 } },
3776 };
3777 
3778 static struct ice_protocol_entry ice_prot_id_tbl[ICE_PROTOCOL_LAST] = {
3779 	{ ICE_MAC_OFOS,		ICE_MAC_OFOS_HW },
3780 	{ ICE_MAC_IL,		ICE_MAC_IL_HW },
3781 	{ ICE_ETYPE_OL,		ICE_ETYPE_OL_HW },
3782 	{ ICE_VLAN_OFOS,	ICE_VLAN_OL_HW },
3783 	{ ICE_IPV4_OFOS,	ICE_IPV4_OFOS_HW },
3784 	{ ICE_IPV4_IL,		ICE_IPV4_IL_HW },
3785 	{ ICE_IPV6_OFOS,	ICE_IPV6_OFOS_HW },
3786 	{ ICE_IPV6_IL,		ICE_IPV6_IL_HW },
3787 	{ ICE_TCP_IL,		ICE_TCP_IL_HW },
3788 	{ ICE_UDP_OF,		ICE_UDP_OF_HW },
3789 	{ ICE_UDP_ILOS,		ICE_UDP_ILOS_HW },
3790 	{ ICE_VXLAN,		ICE_UDP_OF_HW },
3791 	{ ICE_GENEVE,		ICE_UDP_OF_HW },
3792 	{ ICE_NVGRE,            ICE_GRE_OF_HW },
3793 };
3794 
3795 /**
3796  * ice_find_recp - find a recipe
3797  * @hw: pointer to the hardware structure
3798  * @lkup_exts: extension sequence to match
3799  * @tun_type: type of recipe tunnel
3800  *
3801  * Returns index of matching recipe, or ICE_MAX_NUM_RECIPES if not found.
3802  */
3803 static u16
3804 ice_find_recp(struct ice_hw *hw, struct ice_prot_lkup_ext *lkup_exts,
3805 	      enum ice_sw_tunnel_type tun_type)
3806 {
3807 	bool refresh_required = true;
3808 	struct ice_sw_recipe *recp;
3809 	u8 i;
3810 
3811 	/* Walk through existing recipes to find a match */
3812 	recp = hw->switch_info->recp_list;
3813 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
3814 		/* If recipe was not created for this ID, in SW bookkeeping,
3815 		 * check if FW has an entry for this recipe. If the FW has an
3816 		 * entry update it in our SW bookkeeping and continue with the
3817 		 * matching.
3818 		 */
3819 		if (!recp[i].recp_created)
3820 			if (ice_get_recp_frm_fw(hw,
3821 						hw->switch_info->recp_list, i,
3822 						&refresh_required))
3823 				continue;
3824 
3825 		/* Skip inverse action recipes */
3826 		if (recp[i].root_buf && recp[i].root_buf->content.act_ctrl &
3827 		    ICE_AQ_RECIPE_ACT_INV_ACT)
3828 			continue;
3829 
3830 		/* if number of words we are looking for match */
3831 		if (lkup_exts->n_val_words == recp[i].lkup_exts.n_val_words) {
3832 			struct ice_fv_word *ar = recp[i].lkup_exts.fv_words;
3833 			struct ice_fv_word *be = lkup_exts->fv_words;
3834 			u16 *cr = recp[i].lkup_exts.field_mask;
3835 			u16 *de = lkup_exts->field_mask;
3836 			bool found = true;
3837 			u8 pe, qr;
3838 
3839 			/* ar, cr, and qr are related to the recipe words, while
3840 			 * be, de, and pe are related to the lookup words
3841 			 */
3842 			for (pe = 0; pe < lkup_exts->n_val_words; pe++) {
3843 				for (qr = 0; qr < recp[i].lkup_exts.n_val_words;
3844 				     qr++) {
3845 					if (ar[qr].off == be[pe].off &&
3846 					    ar[qr].prot_id == be[pe].prot_id &&
3847 					    cr[qr] == de[pe])
3848 						/* Found the "pe"th word in the
3849 						 * given recipe
3850 						 */
3851 						break;
3852 				}
3853 				/* After walking through all the words in the
3854 				 * "i"th recipe if "p"th word was not found then
3855 				 * this recipe is not what we are looking for.
3856 				 * So break out from this loop and try the next
3857 				 * recipe
3858 				 */
3859 				if (qr >= recp[i].lkup_exts.n_val_words) {
3860 					found = false;
3861 					break;
3862 				}
3863 			}
3864 			/* If for "i"th recipe the found was never set to false
3865 			 * then it means we found our match
3866 			 * Also tun type of recipe needs to be checked
3867 			 */
3868 			if (found && recp[i].tun_type == tun_type)
3869 				return i; /* Return the recipe ID */
3870 		}
3871 	}
3872 	return ICE_MAX_NUM_RECIPES;
3873 }
3874 
3875 /**
3876  * ice_prot_type_to_id - get protocol ID from protocol type
3877  * @type: protocol type
3878  * @id: pointer to variable that will receive the ID
3879  *
3880  * Returns true if found, false otherwise
3881  */
3882 static bool ice_prot_type_to_id(enum ice_protocol_type type, u8 *id)
3883 {
3884 	u8 i;
3885 
3886 	for (i = 0; i < ARRAY_SIZE(ice_prot_id_tbl); i++)
3887 		if (ice_prot_id_tbl[i].type == type) {
3888 			*id = ice_prot_id_tbl[i].protocol_id;
3889 			return true;
3890 		}
3891 	return false;
3892 }
3893 
3894 /**
3895  * ice_fill_valid_words - count valid words
3896  * @rule: advanced rule with lookup information
3897  * @lkup_exts: byte offset extractions of the words that are valid
3898  *
3899  * calculate valid words in a lookup rule using mask value
3900  */
3901 static u8
3902 ice_fill_valid_words(struct ice_adv_lkup_elem *rule,
3903 		     struct ice_prot_lkup_ext *lkup_exts)
3904 {
3905 	u8 j, word, prot_id, ret_val;
3906 
3907 	if (!ice_prot_type_to_id(rule->type, &prot_id))
3908 		return 0;
3909 
3910 	word = lkup_exts->n_val_words;
3911 
3912 	for (j = 0; j < sizeof(rule->m_u) / sizeof(u16); j++)
3913 		if (((u16 *)&rule->m_u)[j] &&
3914 		    rule->type < ARRAY_SIZE(ice_prot_ext)) {
3915 			/* No more space to accommodate */
3916 			if (word >= ICE_MAX_CHAIN_WORDS)
3917 				return 0;
3918 			lkup_exts->fv_words[word].off =
3919 				ice_prot_ext[rule->type].offs[j];
3920 			lkup_exts->fv_words[word].prot_id =
3921 				ice_prot_id_tbl[rule->type].protocol_id;
3922 			lkup_exts->field_mask[word] =
3923 				be16_to_cpu(((__force __be16 *)&rule->m_u)[j]);
3924 			word++;
3925 		}
3926 
3927 	ret_val = word - lkup_exts->n_val_words;
3928 	lkup_exts->n_val_words = word;
3929 
3930 	return ret_val;
3931 }
3932 
3933 /**
3934  * ice_create_first_fit_recp_def - Create a recipe grouping
3935  * @hw: pointer to the hardware structure
3936  * @lkup_exts: an array of protocol header extractions
3937  * @rg_list: pointer to a list that stores new recipe groups
3938  * @recp_cnt: pointer to a variable that stores returned number of recipe groups
3939  *
3940  * Using first fit algorithm, take all the words that are still not done
3941  * and start grouping them in 4-word groups. Each group makes up one
3942  * recipe.
3943  */
3944 static enum ice_status
3945 ice_create_first_fit_recp_def(struct ice_hw *hw,
3946 			      struct ice_prot_lkup_ext *lkup_exts,
3947 			      struct list_head *rg_list,
3948 			      u8 *recp_cnt)
3949 {
3950 	struct ice_pref_recipe_group *grp = NULL;
3951 	u8 j;
3952 
3953 	*recp_cnt = 0;
3954 
3955 	/* Walk through every word in the rule to check if it is not done. If so
3956 	 * then this word needs to be part of a new recipe.
3957 	 */
3958 	for (j = 0; j < lkup_exts->n_val_words; j++)
3959 		if (!test_bit(j, lkup_exts->done)) {
3960 			if (!grp ||
3961 			    grp->n_val_pairs == ICE_NUM_WORDS_RECIPE) {
3962 				struct ice_recp_grp_entry *entry;
3963 
3964 				entry = devm_kzalloc(ice_hw_to_dev(hw),
3965 						     sizeof(*entry),
3966 						     GFP_KERNEL);
3967 				if (!entry)
3968 					return ICE_ERR_NO_MEMORY;
3969 				list_add(&entry->l_entry, rg_list);
3970 				grp = &entry->r_group;
3971 				(*recp_cnt)++;
3972 			}
3973 
3974 			grp->pairs[grp->n_val_pairs].prot_id =
3975 				lkup_exts->fv_words[j].prot_id;
3976 			grp->pairs[grp->n_val_pairs].off =
3977 				lkup_exts->fv_words[j].off;
3978 			grp->mask[grp->n_val_pairs] = lkup_exts->field_mask[j];
3979 			grp->n_val_pairs++;
3980 		}
3981 
3982 	return 0;
3983 }
3984 
3985 /**
3986  * ice_fill_fv_word_index - fill in the field vector indices for a recipe group
3987  * @hw: pointer to the hardware structure
3988  * @fv_list: field vector with the extraction sequence information
3989  * @rg_list: recipe groupings with protocol-offset pairs
3990  *
3991  * Helper function to fill in the field vector indices for protocol-offset
3992  * pairs. These indexes are then ultimately programmed into a recipe.
3993  */
3994 static enum ice_status
3995 ice_fill_fv_word_index(struct ice_hw *hw, struct list_head *fv_list,
3996 		       struct list_head *rg_list)
3997 {
3998 	struct ice_sw_fv_list_entry *fv;
3999 	struct ice_recp_grp_entry *rg;
4000 	struct ice_fv_word *fv_ext;
4001 
4002 	if (list_empty(fv_list))
4003 		return 0;
4004 
4005 	fv = list_first_entry(fv_list, struct ice_sw_fv_list_entry,
4006 			      list_entry);
4007 	fv_ext = fv->fv_ptr->ew;
4008 
4009 	list_for_each_entry(rg, rg_list, l_entry) {
4010 		u8 i;
4011 
4012 		for (i = 0; i < rg->r_group.n_val_pairs; i++) {
4013 			struct ice_fv_word *pr;
4014 			bool found = false;
4015 			u16 mask;
4016 			u8 j;
4017 
4018 			pr = &rg->r_group.pairs[i];
4019 			mask = rg->r_group.mask[i];
4020 
4021 			for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
4022 				if (fv_ext[j].prot_id == pr->prot_id &&
4023 				    fv_ext[j].off == pr->off) {
4024 					found = true;
4025 
4026 					/* Store index of field vector */
4027 					rg->fv_idx[i] = j;
4028 					rg->fv_mask[i] = mask;
4029 					break;
4030 				}
4031 
4032 			/* Protocol/offset could not be found, caller gave an
4033 			 * invalid pair
4034 			 */
4035 			if (!found)
4036 				return ICE_ERR_PARAM;
4037 		}
4038 	}
4039 
4040 	return 0;
4041 }
4042 
4043 /**
4044  * ice_find_free_recp_res_idx - find free result indexes for recipe
4045  * @hw: pointer to hardware structure
4046  * @profiles: bitmap of profiles that will be associated with the new recipe
4047  * @free_idx: pointer to variable to receive the free index bitmap
4048  *
4049  * The algorithm used here is:
4050  *	1. When creating a new recipe, create a set P which contains all
4051  *	   Profiles that will be associated with our new recipe
4052  *
4053  *	2. For each Profile p in set P:
4054  *	    a. Add all recipes associated with Profile p into set R
4055  *	    b. Optional : PossibleIndexes &= profile[p].possibleIndexes
4056  *		[initially PossibleIndexes should be 0xFFFFFFFFFFFFFFFF]
4057  *		i. Or just assume they all have the same possible indexes:
4058  *			44, 45, 46, 47
4059  *			i.e., PossibleIndexes = 0x0000F00000000000
4060  *
4061  *	3. For each Recipe r in set R:
4062  *	    a. UsedIndexes |= (bitwise or ) recipe[r].res_indexes
4063  *	    b. FreeIndexes = UsedIndexes ^ PossibleIndexes
4064  *
4065  *	FreeIndexes will contain the bits indicating the indexes free for use,
4066  *      then the code needs to update the recipe[r].used_result_idx_bits to
4067  *      indicate which indexes were selected for use by this recipe.
4068  */
4069 static u16
4070 ice_find_free_recp_res_idx(struct ice_hw *hw, const unsigned long *profiles,
4071 			   unsigned long *free_idx)
4072 {
4073 	DECLARE_BITMAP(possible_idx, ICE_MAX_FV_WORDS);
4074 	DECLARE_BITMAP(recipes, ICE_MAX_NUM_RECIPES);
4075 	DECLARE_BITMAP(used_idx, ICE_MAX_FV_WORDS);
4076 	u16 bit;
4077 
4078 	bitmap_zero(possible_idx, ICE_MAX_FV_WORDS);
4079 	bitmap_zero(recipes, ICE_MAX_NUM_RECIPES);
4080 	bitmap_zero(used_idx, ICE_MAX_FV_WORDS);
4081 	bitmap_zero(free_idx, ICE_MAX_FV_WORDS);
4082 
4083 	bitmap_set(possible_idx, 0, ICE_MAX_FV_WORDS);
4084 
4085 	/* For each profile we are going to associate the recipe with, add the
4086 	 * recipes that are associated with that profile. This will give us
4087 	 * the set of recipes that our recipe may collide with. Also, determine
4088 	 * what possible result indexes are usable given this set of profiles.
4089 	 */
4090 	for_each_set_bit(bit, profiles, ICE_MAX_NUM_PROFILES) {
4091 		bitmap_or(recipes, recipes, profile_to_recipe[bit],
4092 			  ICE_MAX_NUM_RECIPES);
4093 		bitmap_and(possible_idx, possible_idx,
4094 			   hw->switch_info->prof_res_bm[bit],
4095 			   ICE_MAX_FV_WORDS);
4096 	}
4097 
4098 	/* For each recipe that our new recipe may collide with, determine
4099 	 * which indexes have been used.
4100 	 */
4101 	for_each_set_bit(bit, recipes, ICE_MAX_NUM_RECIPES)
4102 		bitmap_or(used_idx, used_idx,
4103 			  hw->switch_info->recp_list[bit].res_idxs,
4104 			  ICE_MAX_FV_WORDS);
4105 
4106 	bitmap_xor(free_idx, used_idx, possible_idx, ICE_MAX_FV_WORDS);
4107 
4108 	/* return number of free indexes */
4109 	return (u16)bitmap_weight(free_idx, ICE_MAX_FV_WORDS);
4110 }
4111 
4112 /**
4113  * ice_add_sw_recipe - function to call AQ calls to create switch recipe
4114  * @hw: pointer to hardware structure
4115  * @rm: recipe management list entry
4116  * @profiles: bitmap of profiles that will be associated.
4117  */
4118 static enum ice_status
4119 ice_add_sw_recipe(struct ice_hw *hw, struct ice_sw_recipe *rm,
4120 		  unsigned long *profiles)
4121 {
4122 	DECLARE_BITMAP(result_idx_bm, ICE_MAX_FV_WORDS);
4123 	struct ice_aqc_recipe_data_elem *tmp;
4124 	struct ice_aqc_recipe_data_elem *buf;
4125 	struct ice_recp_grp_entry *entry;
4126 	enum ice_status status;
4127 	u16 free_res_idx;
4128 	u16 recipe_count;
4129 	u8 chain_idx;
4130 	u8 recps = 0;
4131 
4132 	/* When more than one recipe are required, another recipe is needed to
4133 	 * chain them together. Matching a tunnel metadata ID takes up one of
4134 	 * the match fields in the chaining recipe reducing the number of
4135 	 * chained recipes by one.
4136 	 */
4137 	 /* check number of free result indices */
4138 	bitmap_zero(result_idx_bm, ICE_MAX_FV_WORDS);
4139 	free_res_idx = ice_find_free_recp_res_idx(hw, profiles, result_idx_bm);
4140 
4141 	ice_debug(hw, ICE_DBG_SW, "Result idx slots: %d, need %d\n",
4142 		  free_res_idx, rm->n_grp_count);
4143 
4144 	if (rm->n_grp_count > 1) {
4145 		if (rm->n_grp_count > free_res_idx)
4146 			return ICE_ERR_MAX_LIMIT;
4147 
4148 		rm->n_grp_count++;
4149 	}
4150 
4151 	if (rm->n_grp_count > ICE_MAX_CHAIN_RECIPE)
4152 		return ICE_ERR_MAX_LIMIT;
4153 
4154 	tmp = kcalloc(ICE_MAX_NUM_RECIPES, sizeof(*tmp), GFP_KERNEL);
4155 	if (!tmp)
4156 		return ICE_ERR_NO_MEMORY;
4157 
4158 	buf = devm_kcalloc(ice_hw_to_dev(hw), rm->n_grp_count, sizeof(*buf),
4159 			   GFP_KERNEL);
4160 	if (!buf) {
4161 		status = ICE_ERR_NO_MEMORY;
4162 		goto err_mem;
4163 	}
4164 
4165 	bitmap_zero(rm->r_bitmap, ICE_MAX_NUM_RECIPES);
4166 	recipe_count = ICE_MAX_NUM_RECIPES;
4167 	status = ice_aq_get_recipe(hw, tmp, &recipe_count, ICE_SW_LKUP_MAC,
4168 				   NULL);
4169 	if (status || recipe_count == 0)
4170 		goto err_unroll;
4171 
4172 	/* Allocate the recipe resources, and configure them according to the
4173 	 * match fields from protocol headers and extracted field vectors.
4174 	 */
4175 	chain_idx = find_first_bit(result_idx_bm, ICE_MAX_FV_WORDS);
4176 	list_for_each_entry(entry, &rm->rg_list, l_entry) {
4177 		u8 i;
4178 
4179 		status = ice_alloc_recipe(hw, &entry->rid);
4180 		if (status)
4181 			goto err_unroll;
4182 
4183 		/* Clear the result index of the located recipe, as this will be
4184 		 * updated, if needed, later in the recipe creation process.
4185 		 */
4186 		tmp[0].content.result_indx = 0;
4187 
4188 		buf[recps] = tmp[0];
4189 		buf[recps].recipe_indx = (u8)entry->rid;
4190 		/* if the recipe is a non-root recipe RID should be programmed
4191 		 * as 0 for the rules to be applied correctly.
4192 		 */
4193 		buf[recps].content.rid = 0;
4194 		memset(&buf[recps].content.lkup_indx, 0,
4195 		       sizeof(buf[recps].content.lkup_indx));
4196 
4197 		/* All recipes use look-up index 0 to match switch ID. */
4198 		buf[recps].content.lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
4199 		buf[recps].content.mask[0] =
4200 			cpu_to_le16(ICE_AQ_SW_ID_LKUP_MASK);
4201 		/* Setup lkup_indx 1..4 to INVALID/ignore and set the mask
4202 		 * to be 0
4203 		 */
4204 		for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
4205 			buf[recps].content.lkup_indx[i] = 0x80;
4206 			buf[recps].content.mask[i] = 0;
4207 		}
4208 
4209 		for (i = 0; i < entry->r_group.n_val_pairs; i++) {
4210 			buf[recps].content.lkup_indx[i + 1] = entry->fv_idx[i];
4211 			buf[recps].content.mask[i + 1] =
4212 				cpu_to_le16(entry->fv_mask[i]);
4213 		}
4214 
4215 		if (rm->n_grp_count > 1) {
4216 			/* Checks to see if there really is a valid result index
4217 			 * that can be used.
4218 			 */
4219 			if (chain_idx >= ICE_MAX_FV_WORDS) {
4220 				ice_debug(hw, ICE_DBG_SW, "No chain index available\n");
4221 				status = ICE_ERR_MAX_LIMIT;
4222 				goto err_unroll;
4223 			}
4224 
4225 			entry->chain_idx = chain_idx;
4226 			buf[recps].content.result_indx =
4227 				ICE_AQ_RECIPE_RESULT_EN |
4228 				((chain_idx << ICE_AQ_RECIPE_RESULT_DATA_S) &
4229 				 ICE_AQ_RECIPE_RESULT_DATA_M);
4230 			clear_bit(chain_idx, result_idx_bm);
4231 			chain_idx = find_first_bit(result_idx_bm,
4232 						   ICE_MAX_FV_WORDS);
4233 		}
4234 
4235 		/* fill recipe dependencies */
4236 		bitmap_zero((unsigned long *)buf[recps].recipe_bitmap,
4237 			    ICE_MAX_NUM_RECIPES);
4238 		set_bit(buf[recps].recipe_indx,
4239 			(unsigned long *)buf[recps].recipe_bitmap);
4240 		buf[recps].content.act_ctrl_fwd_priority = rm->priority;
4241 		recps++;
4242 	}
4243 
4244 	if (rm->n_grp_count == 1) {
4245 		rm->root_rid = buf[0].recipe_indx;
4246 		set_bit(buf[0].recipe_indx, rm->r_bitmap);
4247 		buf[0].content.rid = rm->root_rid | ICE_AQ_RECIPE_ID_IS_ROOT;
4248 		if (sizeof(buf[0].recipe_bitmap) >= sizeof(rm->r_bitmap)) {
4249 			memcpy(buf[0].recipe_bitmap, rm->r_bitmap,
4250 			       sizeof(buf[0].recipe_bitmap));
4251 		} else {
4252 			status = ICE_ERR_BAD_PTR;
4253 			goto err_unroll;
4254 		}
4255 		/* Applicable only for ROOT_RECIPE, set the fwd_priority for
4256 		 * the recipe which is getting created if specified
4257 		 * by user. Usually any advanced switch filter, which results
4258 		 * into new extraction sequence, ended up creating a new recipe
4259 		 * of type ROOT and usually recipes are associated with profiles
4260 		 * Switch rule referreing newly created recipe, needs to have
4261 		 * either/or 'fwd' or 'join' priority, otherwise switch rule
4262 		 * evaluation will not happen correctly. In other words, if
4263 		 * switch rule to be evaluated on priority basis, then recipe
4264 		 * needs to have priority, otherwise it will be evaluated last.
4265 		 */
4266 		buf[0].content.act_ctrl_fwd_priority = rm->priority;
4267 	} else {
4268 		struct ice_recp_grp_entry *last_chain_entry;
4269 		u16 rid, i;
4270 
4271 		/* Allocate the last recipe that will chain the outcomes of the
4272 		 * other recipes together
4273 		 */
4274 		status = ice_alloc_recipe(hw, &rid);
4275 		if (status)
4276 			goto err_unroll;
4277 
4278 		buf[recps].recipe_indx = (u8)rid;
4279 		buf[recps].content.rid = (u8)rid;
4280 		buf[recps].content.rid |= ICE_AQ_RECIPE_ID_IS_ROOT;
4281 		/* the new entry created should also be part of rg_list to
4282 		 * make sure we have complete recipe
4283 		 */
4284 		last_chain_entry = devm_kzalloc(ice_hw_to_dev(hw),
4285 						sizeof(*last_chain_entry),
4286 						GFP_KERNEL);
4287 		if (!last_chain_entry) {
4288 			status = ICE_ERR_NO_MEMORY;
4289 			goto err_unroll;
4290 		}
4291 		last_chain_entry->rid = rid;
4292 		memset(&buf[recps].content.lkup_indx, 0,
4293 		       sizeof(buf[recps].content.lkup_indx));
4294 		/* All recipes use look-up index 0 to match switch ID. */
4295 		buf[recps].content.lkup_indx[0] = ICE_AQ_SW_ID_LKUP_IDX;
4296 		buf[recps].content.mask[0] =
4297 			cpu_to_le16(ICE_AQ_SW_ID_LKUP_MASK);
4298 		for (i = 1; i <= ICE_NUM_WORDS_RECIPE; i++) {
4299 			buf[recps].content.lkup_indx[i] =
4300 				ICE_AQ_RECIPE_LKUP_IGNORE;
4301 			buf[recps].content.mask[i] = 0;
4302 		}
4303 
4304 		i = 1;
4305 		/* update r_bitmap with the recp that is used for chaining */
4306 		set_bit(rid, rm->r_bitmap);
4307 		/* this is the recipe that chains all the other recipes so it
4308 		 * should not have a chaining ID to indicate the same
4309 		 */
4310 		last_chain_entry->chain_idx = ICE_INVAL_CHAIN_IND;
4311 		list_for_each_entry(entry, &rm->rg_list, l_entry) {
4312 			last_chain_entry->fv_idx[i] = entry->chain_idx;
4313 			buf[recps].content.lkup_indx[i] = entry->chain_idx;
4314 			buf[recps].content.mask[i++] = cpu_to_le16(0xFFFF);
4315 			set_bit(entry->rid, rm->r_bitmap);
4316 		}
4317 		list_add(&last_chain_entry->l_entry, &rm->rg_list);
4318 		if (sizeof(buf[recps].recipe_bitmap) >=
4319 		    sizeof(rm->r_bitmap)) {
4320 			memcpy(buf[recps].recipe_bitmap, rm->r_bitmap,
4321 			       sizeof(buf[recps].recipe_bitmap));
4322 		} else {
4323 			status = ICE_ERR_BAD_PTR;
4324 			goto err_unroll;
4325 		}
4326 		buf[recps].content.act_ctrl_fwd_priority = rm->priority;
4327 
4328 		recps++;
4329 		rm->root_rid = (u8)rid;
4330 	}
4331 	status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
4332 	if (status)
4333 		goto err_unroll;
4334 
4335 	status = ice_aq_add_recipe(hw, buf, rm->n_grp_count, NULL);
4336 	ice_release_change_lock(hw);
4337 	if (status)
4338 		goto err_unroll;
4339 
4340 	/* Every recipe that just got created add it to the recipe
4341 	 * book keeping list
4342 	 */
4343 	list_for_each_entry(entry, &rm->rg_list, l_entry) {
4344 		struct ice_switch_info *sw = hw->switch_info;
4345 		bool is_root, idx_found = false;
4346 		struct ice_sw_recipe *recp;
4347 		u16 idx, buf_idx = 0;
4348 
4349 		/* find buffer index for copying some data */
4350 		for (idx = 0; idx < rm->n_grp_count; idx++)
4351 			if (buf[idx].recipe_indx == entry->rid) {
4352 				buf_idx = idx;
4353 				idx_found = true;
4354 			}
4355 
4356 		if (!idx_found) {
4357 			status = ICE_ERR_OUT_OF_RANGE;
4358 			goto err_unroll;
4359 		}
4360 
4361 		recp = &sw->recp_list[entry->rid];
4362 		is_root = (rm->root_rid == entry->rid);
4363 		recp->is_root = is_root;
4364 
4365 		recp->root_rid = entry->rid;
4366 		recp->big_recp = (is_root && rm->n_grp_count > 1);
4367 
4368 		memcpy(&recp->ext_words, entry->r_group.pairs,
4369 		       entry->r_group.n_val_pairs * sizeof(struct ice_fv_word));
4370 
4371 		memcpy(recp->r_bitmap, buf[buf_idx].recipe_bitmap,
4372 		       sizeof(recp->r_bitmap));
4373 
4374 		/* Copy non-result fv index values and masks to recipe. This
4375 		 * call will also update the result recipe bitmask.
4376 		 */
4377 		ice_collect_result_idx(&buf[buf_idx], recp);
4378 
4379 		/* for non-root recipes, also copy to the root, this allows
4380 		 * easier matching of a complete chained recipe
4381 		 */
4382 		if (!is_root)
4383 			ice_collect_result_idx(&buf[buf_idx],
4384 					       &sw->recp_list[rm->root_rid]);
4385 
4386 		recp->n_ext_words = entry->r_group.n_val_pairs;
4387 		recp->chain_idx = entry->chain_idx;
4388 		recp->priority = buf[buf_idx].content.act_ctrl_fwd_priority;
4389 		recp->n_grp_count = rm->n_grp_count;
4390 		recp->tun_type = rm->tun_type;
4391 		recp->recp_created = true;
4392 	}
4393 	rm->root_buf = buf;
4394 	kfree(tmp);
4395 	return status;
4396 
4397 err_unroll:
4398 err_mem:
4399 	kfree(tmp);
4400 	devm_kfree(ice_hw_to_dev(hw), buf);
4401 	return status;
4402 }
4403 
4404 /**
4405  * ice_create_recipe_group - creates recipe group
4406  * @hw: pointer to hardware structure
4407  * @rm: recipe management list entry
4408  * @lkup_exts: lookup elements
4409  */
4410 static enum ice_status
4411 ice_create_recipe_group(struct ice_hw *hw, struct ice_sw_recipe *rm,
4412 			struct ice_prot_lkup_ext *lkup_exts)
4413 {
4414 	enum ice_status status;
4415 	u8 recp_count = 0;
4416 
4417 	rm->n_grp_count = 0;
4418 
4419 	/* Create recipes for words that are marked not done by packing them
4420 	 * as best fit.
4421 	 */
4422 	status = ice_create_first_fit_recp_def(hw, lkup_exts,
4423 					       &rm->rg_list, &recp_count);
4424 	if (!status) {
4425 		rm->n_grp_count += recp_count;
4426 		rm->n_ext_words = lkup_exts->n_val_words;
4427 		memcpy(&rm->ext_words, lkup_exts->fv_words,
4428 		       sizeof(rm->ext_words));
4429 		memcpy(rm->word_masks, lkup_exts->field_mask,
4430 		       sizeof(rm->word_masks));
4431 	}
4432 
4433 	return status;
4434 }
4435 
4436 /**
4437  * ice_get_fv - get field vectors/extraction sequences for spec. lookup types
4438  * @hw: pointer to hardware structure
4439  * @lkups: lookup elements or match criteria for the advanced recipe, one
4440  *	   structure per protocol header
4441  * @lkups_cnt: number of protocols
4442  * @bm: bitmap of field vectors to consider
4443  * @fv_list: pointer to a list that holds the returned field vectors
4444  */
4445 static enum ice_status
4446 ice_get_fv(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
4447 	   unsigned long *bm, struct list_head *fv_list)
4448 {
4449 	enum ice_status status;
4450 	u8 *prot_ids;
4451 	u16 i;
4452 
4453 	prot_ids = kcalloc(lkups_cnt, sizeof(*prot_ids), GFP_KERNEL);
4454 	if (!prot_ids)
4455 		return ICE_ERR_NO_MEMORY;
4456 
4457 	for (i = 0; i < lkups_cnt; i++)
4458 		if (!ice_prot_type_to_id(lkups[i].type, &prot_ids[i])) {
4459 			status = ICE_ERR_CFG;
4460 			goto free_mem;
4461 		}
4462 
4463 	/* Find field vectors that include all specified protocol types */
4464 	status = ice_get_sw_fv_list(hw, prot_ids, lkups_cnt, bm, fv_list);
4465 
4466 free_mem:
4467 	kfree(prot_ids);
4468 	return status;
4469 }
4470 
4471 /**
4472  * ice_tun_type_match_word - determine if tun type needs a match mask
4473  * @tun_type: tunnel type
4474  * @mask: mask to be used for the tunnel
4475  */
4476 static bool ice_tun_type_match_word(enum ice_sw_tunnel_type tun_type, u16 *mask)
4477 {
4478 	switch (tun_type) {
4479 	case ICE_SW_TUN_GENEVE:
4480 	case ICE_SW_TUN_VXLAN:
4481 	case ICE_SW_TUN_NVGRE:
4482 		*mask = ICE_TUN_FLAG_MASK;
4483 		return true;
4484 
4485 	default:
4486 		*mask = 0;
4487 		return false;
4488 	}
4489 }
4490 
4491 /**
4492  * ice_add_special_words - Add words that are not protocols, such as metadata
4493  * @rinfo: other information regarding the rule e.g. priority and action info
4494  * @lkup_exts: lookup word structure
4495  */
4496 static enum ice_status
4497 ice_add_special_words(struct ice_adv_rule_info *rinfo,
4498 		      struct ice_prot_lkup_ext *lkup_exts)
4499 {
4500 	u16 mask;
4501 
4502 	/* If this is a tunneled packet, then add recipe index to match the
4503 	 * tunnel bit in the packet metadata flags.
4504 	 */
4505 	if (ice_tun_type_match_word(rinfo->tun_type, &mask)) {
4506 		if (lkup_exts->n_val_words < ICE_MAX_CHAIN_WORDS) {
4507 			u8 word = lkup_exts->n_val_words++;
4508 
4509 			lkup_exts->fv_words[word].prot_id = ICE_META_DATA_ID_HW;
4510 			lkup_exts->fv_words[word].off = ICE_TUN_FLAG_MDID_OFF;
4511 			lkup_exts->field_mask[word] = mask;
4512 		} else {
4513 			return ICE_ERR_MAX_LIMIT;
4514 		}
4515 	}
4516 
4517 	return 0;
4518 }
4519 
4520 /* ice_get_compat_fv_bitmap - Get compatible field vector bitmap for rule
4521  * @hw: pointer to hardware structure
4522  * @rinfo: other information regarding the rule e.g. priority and action info
4523  * @bm: pointer to memory for returning the bitmap of field vectors
4524  */
4525 static void
4526 ice_get_compat_fv_bitmap(struct ice_hw *hw, struct ice_adv_rule_info *rinfo,
4527 			 unsigned long *bm)
4528 {
4529 	enum ice_prof_type prof_type;
4530 
4531 	bitmap_zero(bm, ICE_MAX_NUM_PROFILES);
4532 
4533 	switch (rinfo->tun_type) {
4534 	case ICE_NON_TUN:
4535 		prof_type = ICE_PROF_NON_TUN;
4536 		break;
4537 	case ICE_ALL_TUNNELS:
4538 		prof_type = ICE_PROF_TUN_ALL;
4539 		break;
4540 	case ICE_SW_TUN_GENEVE:
4541 	case ICE_SW_TUN_VXLAN:
4542 		prof_type = ICE_PROF_TUN_UDP;
4543 		break;
4544 	case ICE_SW_TUN_NVGRE:
4545 		prof_type = ICE_PROF_TUN_GRE;
4546 		break;
4547 	default:
4548 		prof_type = ICE_PROF_ALL;
4549 		break;
4550 	}
4551 
4552 	ice_get_sw_fv_bitmap(hw, prof_type, bm);
4553 }
4554 
4555 /**
4556  * ice_add_adv_recipe - Add an advanced recipe that is not part of the default
4557  * @hw: pointer to hardware structure
4558  * @lkups: lookup elements or match criteria for the advanced recipe, one
4559  *  structure per protocol header
4560  * @lkups_cnt: number of protocols
4561  * @rinfo: other information regarding the rule e.g. priority and action info
4562  * @rid: return the recipe ID of the recipe created
4563  */
4564 static enum ice_status
4565 ice_add_adv_recipe(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
4566 		   u16 lkups_cnt, struct ice_adv_rule_info *rinfo, u16 *rid)
4567 {
4568 	DECLARE_BITMAP(fv_bitmap, ICE_MAX_NUM_PROFILES);
4569 	DECLARE_BITMAP(profiles, ICE_MAX_NUM_PROFILES);
4570 	struct ice_prot_lkup_ext *lkup_exts;
4571 	struct ice_recp_grp_entry *r_entry;
4572 	struct ice_sw_fv_list_entry *fvit;
4573 	struct ice_recp_grp_entry *r_tmp;
4574 	struct ice_sw_fv_list_entry *tmp;
4575 	enum ice_status status = 0;
4576 	struct ice_sw_recipe *rm;
4577 	u8 i;
4578 
4579 	if (!lkups_cnt)
4580 		return ICE_ERR_PARAM;
4581 
4582 	lkup_exts = kzalloc(sizeof(*lkup_exts), GFP_KERNEL);
4583 	if (!lkup_exts)
4584 		return ICE_ERR_NO_MEMORY;
4585 
4586 	/* Determine the number of words to be matched and if it exceeds a
4587 	 * recipe's restrictions
4588 	 */
4589 	for (i = 0; i < lkups_cnt; i++) {
4590 		u16 count;
4591 
4592 		if (lkups[i].type >= ICE_PROTOCOL_LAST) {
4593 			status = ICE_ERR_CFG;
4594 			goto err_free_lkup_exts;
4595 		}
4596 
4597 		count = ice_fill_valid_words(&lkups[i], lkup_exts);
4598 		if (!count) {
4599 			status = ICE_ERR_CFG;
4600 			goto err_free_lkup_exts;
4601 		}
4602 	}
4603 
4604 	rm = kzalloc(sizeof(*rm), GFP_KERNEL);
4605 	if (!rm) {
4606 		status = ICE_ERR_NO_MEMORY;
4607 		goto err_free_lkup_exts;
4608 	}
4609 
4610 	/* Get field vectors that contain fields extracted from all the protocol
4611 	 * headers being programmed.
4612 	 */
4613 	INIT_LIST_HEAD(&rm->fv_list);
4614 	INIT_LIST_HEAD(&rm->rg_list);
4615 
4616 	/* Get bitmap of field vectors (profiles) that are compatible with the
4617 	 * rule request; only these will be searched in the subsequent call to
4618 	 * ice_get_fv.
4619 	 */
4620 	ice_get_compat_fv_bitmap(hw, rinfo, fv_bitmap);
4621 
4622 	status = ice_get_fv(hw, lkups, lkups_cnt, fv_bitmap, &rm->fv_list);
4623 	if (status)
4624 		goto err_unroll;
4625 
4626 	/* Create any special protocol/offset pairs, such as looking at tunnel
4627 	 * bits by extracting metadata
4628 	 */
4629 	status = ice_add_special_words(rinfo, lkup_exts);
4630 	if (status)
4631 		goto err_free_lkup_exts;
4632 
4633 	/* Group match words into recipes using preferred recipe grouping
4634 	 * criteria.
4635 	 */
4636 	status = ice_create_recipe_group(hw, rm, lkup_exts);
4637 	if (status)
4638 		goto err_unroll;
4639 
4640 	/* set the recipe priority if specified */
4641 	rm->priority = (u8)rinfo->priority;
4642 
4643 	/* Find offsets from the field vector. Pick the first one for all the
4644 	 * recipes.
4645 	 */
4646 	status = ice_fill_fv_word_index(hw, &rm->fv_list, &rm->rg_list);
4647 	if (status)
4648 		goto err_unroll;
4649 
4650 	/* get bitmap of all profiles the recipe will be associated with */
4651 	bitmap_zero(profiles, ICE_MAX_NUM_PROFILES);
4652 	list_for_each_entry(fvit, &rm->fv_list, list_entry) {
4653 		ice_debug(hw, ICE_DBG_SW, "profile: %d\n", fvit->profile_id);
4654 		set_bit((u16)fvit->profile_id, profiles);
4655 	}
4656 
4657 	/* Look for a recipe which matches our requested fv / mask list */
4658 	*rid = ice_find_recp(hw, lkup_exts, rinfo->tun_type);
4659 	if (*rid < ICE_MAX_NUM_RECIPES)
4660 		/* Success if found a recipe that match the existing criteria */
4661 		goto err_unroll;
4662 
4663 	rm->tun_type = rinfo->tun_type;
4664 	/* Recipe we need does not exist, add a recipe */
4665 	status = ice_add_sw_recipe(hw, rm, profiles);
4666 	if (status)
4667 		goto err_unroll;
4668 
4669 	/* Associate all the recipes created with all the profiles in the
4670 	 * common field vector.
4671 	 */
4672 	list_for_each_entry(fvit, &rm->fv_list, list_entry) {
4673 		DECLARE_BITMAP(r_bitmap, ICE_MAX_NUM_RECIPES);
4674 		u16 j;
4675 
4676 		status = ice_aq_get_recipe_to_profile(hw, fvit->profile_id,
4677 						      (u8 *)r_bitmap, NULL);
4678 		if (status)
4679 			goto err_unroll;
4680 
4681 		bitmap_or(r_bitmap, r_bitmap, rm->r_bitmap,
4682 			  ICE_MAX_NUM_RECIPES);
4683 		status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
4684 		if (status)
4685 			goto err_unroll;
4686 
4687 		status = ice_aq_map_recipe_to_profile(hw, fvit->profile_id,
4688 						      (u8 *)r_bitmap,
4689 						      NULL);
4690 		ice_release_change_lock(hw);
4691 
4692 		if (status)
4693 			goto err_unroll;
4694 
4695 		/* Update profile to recipe bitmap array */
4696 		bitmap_copy(profile_to_recipe[fvit->profile_id], r_bitmap,
4697 			    ICE_MAX_NUM_RECIPES);
4698 
4699 		/* Update recipe to profile bitmap array */
4700 		for_each_set_bit(j, rm->r_bitmap, ICE_MAX_NUM_RECIPES)
4701 			set_bit((u16)fvit->profile_id, recipe_to_profile[j]);
4702 	}
4703 
4704 	*rid = rm->root_rid;
4705 	memcpy(&hw->switch_info->recp_list[*rid].lkup_exts, lkup_exts,
4706 	       sizeof(*lkup_exts));
4707 err_unroll:
4708 	list_for_each_entry_safe(r_entry, r_tmp, &rm->rg_list, l_entry) {
4709 		list_del(&r_entry->l_entry);
4710 		devm_kfree(ice_hw_to_dev(hw), r_entry);
4711 	}
4712 
4713 	list_for_each_entry_safe(fvit, tmp, &rm->fv_list, list_entry) {
4714 		list_del(&fvit->list_entry);
4715 		devm_kfree(ice_hw_to_dev(hw), fvit);
4716 	}
4717 
4718 	if (rm->root_buf)
4719 		devm_kfree(ice_hw_to_dev(hw), rm->root_buf);
4720 
4721 	kfree(rm);
4722 
4723 err_free_lkup_exts:
4724 	kfree(lkup_exts);
4725 
4726 	return status;
4727 }
4728 
4729 /**
4730  * ice_find_dummy_packet - find dummy packet
4731  *
4732  * @lkups: lookup elements or match criteria for the advanced recipe, one
4733  *	   structure per protocol header
4734  * @lkups_cnt: number of protocols
4735  * @tun_type: tunnel type
4736  * @pkt: dummy packet to fill according to filter match criteria
4737  * @pkt_len: packet length of dummy packet
4738  * @offsets: pointer to receive the pointer to the offsets for the packet
4739  */
4740 static void
4741 ice_find_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
4742 		      enum ice_sw_tunnel_type tun_type,
4743 		      const u8 **pkt, u16 *pkt_len,
4744 		      const struct ice_dummy_pkt_offsets **offsets)
4745 {
4746 	bool tcp = false, udp = false, ipv6 = false, vlan = false;
4747 	u16 i;
4748 
4749 	for (i = 0; i < lkups_cnt; i++) {
4750 		if (lkups[i].type == ICE_UDP_ILOS)
4751 			udp = true;
4752 		else if (lkups[i].type == ICE_TCP_IL)
4753 			tcp = true;
4754 		else if (lkups[i].type == ICE_IPV6_OFOS)
4755 			ipv6 = true;
4756 		else if (lkups[i].type == ICE_VLAN_OFOS)
4757 			vlan = true;
4758 		else if (lkups[i].type == ICE_ETYPE_OL &&
4759 			 lkups[i].h_u.ethertype.ethtype_id ==
4760 				cpu_to_be16(ICE_IPV6_ETHER_ID) &&
4761 			 lkups[i].m_u.ethertype.ethtype_id ==
4762 					cpu_to_be16(0xFFFF))
4763 			ipv6 = true;
4764 	}
4765 
4766 	if (tun_type == ICE_SW_TUN_NVGRE) {
4767 		if (tcp) {
4768 			*pkt = dummy_gre_tcp_packet;
4769 			*pkt_len = sizeof(dummy_gre_tcp_packet);
4770 			*offsets = dummy_gre_tcp_packet_offsets;
4771 			return;
4772 		}
4773 
4774 		*pkt = dummy_gre_udp_packet;
4775 		*pkt_len = sizeof(dummy_gre_udp_packet);
4776 		*offsets = dummy_gre_udp_packet_offsets;
4777 		return;
4778 	}
4779 
4780 	if (tun_type == ICE_SW_TUN_VXLAN ||
4781 	    tun_type == ICE_SW_TUN_GENEVE) {
4782 		if (tcp) {
4783 			*pkt = dummy_udp_tun_tcp_packet;
4784 			*pkt_len = sizeof(dummy_udp_tun_tcp_packet);
4785 			*offsets = dummy_udp_tun_tcp_packet_offsets;
4786 			return;
4787 		}
4788 
4789 		*pkt = dummy_udp_tun_udp_packet;
4790 		*pkt_len = sizeof(dummy_udp_tun_udp_packet);
4791 		*offsets = dummy_udp_tun_udp_packet_offsets;
4792 		return;
4793 	}
4794 
4795 	if (udp && !ipv6) {
4796 		if (vlan) {
4797 			*pkt = dummy_vlan_udp_packet;
4798 			*pkt_len = sizeof(dummy_vlan_udp_packet);
4799 			*offsets = dummy_vlan_udp_packet_offsets;
4800 			return;
4801 		}
4802 		*pkt = dummy_udp_packet;
4803 		*pkt_len = sizeof(dummy_udp_packet);
4804 		*offsets = dummy_udp_packet_offsets;
4805 		return;
4806 	} else if (udp && ipv6) {
4807 		if (vlan) {
4808 			*pkt = dummy_vlan_udp_ipv6_packet;
4809 			*pkt_len = sizeof(dummy_vlan_udp_ipv6_packet);
4810 			*offsets = dummy_vlan_udp_ipv6_packet_offsets;
4811 			return;
4812 		}
4813 		*pkt = dummy_udp_ipv6_packet;
4814 		*pkt_len = sizeof(dummy_udp_ipv6_packet);
4815 		*offsets = dummy_udp_ipv6_packet_offsets;
4816 		return;
4817 	} else if ((tcp && ipv6) || ipv6) {
4818 		if (vlan) {
4819 			*pkt = dummy_vlan_tcp_ipv6_packet;
4820 			*pkt_len = sizeof(dummy_vlan_tcp_ipv6_packet);
4821 			*offsets = dummy_vlan_tcp_ipv6_packet_offsets;
4822 			return;
4823 		}
4824 		*pkt = dummy_tcp_ipv6_packet;
4825 		*pkt_len = sizeof(dummy_tcp_ipv6_packet);
4826 		*offsets = dummy_tcp_ipv6_packet_offsets;
4827 		return;
4828 	}
4829 
4830 	if (vlan) {
4831 		*pkt = dummy_vlan_tcp_packet;
4832 		*pkt_len = sizeof(dummy_vlan_tcp_packet);
4833 		*offsets = dummy_vlan_tcp_packet_offsets;
4834 	} else {
4835 		*pkt = dummy_tcp_packet;
4836 		*pkt_len = sizeof(dummy_tcp_packet);
4837 		*offsets = dummy_tcp_packet_offsets;
4838 	}
4839 }
4840 
4841 /**
4842  * ice_fill_adv_dummy_packet - fill a dummy packet with given match criteria
4843  *
4844  * @lkups: lookup elements or match criteria for the advanced recipe, one
4845  *	   structure per protocol header
4846  * @lkups_cnt: number of protocols
4847  * @s_rule: stores rule information from the match criteria
4848  * @dummy_pkt: dummy packet to fill according to filter match criteria
4849  * @pkt_len: packet length of dummy packet
4850  * @offsets: offset info for the dummy packet
4851  */
4852 static enum ice_status
4853 ice_fill_adv_dummy_packet(struct ice_adv_lkup_elem *lkups, u16 lkups_cnt,
4854 			  struct ice_aqc_sw_rules_elem *s_rule,
4855 			  const u8 *dummy_pkt, u16 pkt_len,
4856 			  const struct ice_dummy_pkt_offsets *offsets)
4857 {
4858 	u8 *pkt;
4859 	u16 i;
4860 
4861 	/* Start with a packet with a pre-defined/dummy content. Then, fill
4862 	 * in the header values to be looked up or matched.
4863 	 */
4864 	pkt = s_rule->pdata.lkup_tx_rx.hdr;
4865 
4866 	memcpy(pkt, dummy_pkt, pkt_len);
4867 
4868 	for (i = 0; i < lkups_cnt; i++) {
4869 		enum ice_protocol_type type;
4870 		u16 offset = 0, len = 0, j;
4871 		bool found = false;
4872 
4873 		/* find the start of this layer; it should be found since this
4874 		 * was already checked when search for the dummy packet
4875 		 */
4876 		type = lkups[i].type;
4877 		for (j = 0; offsets[j].type != ICE_PROTOCOL_LAST; j++) {
4878 			if (type == offsets[j].type) {
4879 				offset = offsets[j].offset;
4880 				found = true;
4881 				break;
4882 			}
4883 		}
4884 		/* this should never happen in a correct calling sequence */
4885 		if (!found)
4886 			return ICE_ERR_PARAM;
4887 
4888 		switch (lkups[i].type) {
4889 		case ICE_MAC_OFOS:
4890 		case ICE_MAC_IL:
4891 			len = sizeof(struct ice_ether_hdr);
4892 			break;
4893 		case ICE_ETYPE_OL:
4894 			len = sizeof(struct ice_ethtype_hdr);
4895 			break;
4896 		case ICE_VLAN_OFOS:
4897 			len = sizeof(struct ice_vlan_hdr);
4898 			break;
4899 		case ICE_IPV4_OFOS:
4900 		case ICE_IPV4_IL:
4901 			len = sizeof(struct ice_ipv4_hdr);
4902 			break;
4903 		case ICE_IPV6_OFOS:
4904 		case ICE_IPV6_IL:
4905 			len = sizeof(struct ice_ipv6_hdr);
4906 			break;
4907 		case ICE_TCP_IL:
4908 		case ICE_UDP_OF:
4909 		case ICE_UDP_ILOS:
4910 			len = sizeof(struct ice_l4_hdr);
4911 			break;
4912 		case ICE_SCTP_IL:
4913 			len = sizeof(struct ice_sctp_hdr);
4914 			break;
4915 		case ICE_NVGRE:
4916 			len = sizeof(struct ice_nvgre_hdr);
4917 			break;
4918 		case ICE_VXLAN:
4919 		case ICE_GENEVE:
4920 			len = sizeof(struct ice_udp_tnl_hdr);
4921 			break;
4922 		default:
4923 			return ICE_ERR_PARAM;
4924 		}
4925 
4926 		/* the length should be a word multiple */
4927 		if (len % ICE_BYTES_PER_WORD)
4928 			return ICE_ERR_CFG;
4929 
4930 		/* We have the offset to the header start, the length, the
4931 		 * caller's header values and mask. Use this information to
4932 		 * copy the data into the dummy packet appropriately based on
4933 		 * the mask. Note that we need to only write the bits as
4934 		 * indicated by the mask to make sure we don't improperly write
4935 		 * over any significant packet data.
4936 		 */
4937 		for (j = 0; j < len / sizeof(u16); j++)
4938 			if (((u16 *)&lkups[i].m_u)[j])
4939 				((u16 *)(pkt + offset))[j] =
4940 					(((u16 *)(pkt + offset))[j] &
4941 					 ~((u16 *)&lkups[i].m_u)[j]) |
4942 					(((u16 *)&lkups[i].h_u)[j] &
4943 					 ((u16 *)&lkups[i].m_u)[j]);
4944 	}
4945 
4946 	s_rule->pdata.lkup_tx_rx.hdr_len = cpu_to_le16(pkt_len);
4947 
4948 	return 0;
4949 }
4950 
4951 /**
4952  * ice_fill_adv_packet_tun - fill dummy packet with udp tunnel port
4953  * @hw: pointer to the hardware structure
4954  * @tun_type: tunnel type
4955  * @pkt: dummy packet to fill in
4956  * @offsets: offset info for the dummy packet
4957  */
4958 static enum ice_status
4959 ice_fill_adv_packet_tun(struct ice_hw *hw, enum ice_sw_tunnel_type tun_type,
4960 			u8 *pkt, const struct ice_dummy_pkt_offsets *offsets)
4961 {
4962 	u16 open_port, i;
4963 
4964 	switch (tun_type) {
4965 	case ICE_SW_TUN_VXLAN:
4966 		if (!ice_get_open_tunnel_port(hw, &open_port, TNL_VXLAN))
4967 			return ICE_ERR_CFG;
4968 		break;
4969 	case ICE_SW_TUN_GENEVE:
4970 		if (!ice_get_open_tunnel_port(hw, &open_port, TNL_GENEVE))
4971 			return ICE_ERR_CFG;
4972 		break;
4973 	default:
4974 		/* Nothing needs to be done for this tunnel type */
4975 		return 0;
4976 	}
4977 
4978 	/* Find the outer UDP protocol header and insert the port number */
4979 	for (i = 0; offsets[i].type != ICE_PROTOCOL_LAST; i++) {
4980 		if (offsets[i].type == ICE_UDP_OF) {
4981 			struct ice_l4_hdr *hdr;
4982 			u16 offset;
4983 
4984 			offset = offsets[i].offset;
4985 			hdr = (struct ice_l4_hdr *)&pkt[offset];
4986 			hdr->dst_port = cpu_to_be16(open_port);
4987 
4988 			return 0;
4989 		}
4990 	}
4991 
4992 	return ICE_ERR_CFG;
4993 }
4994 
4995 /**
4996  * ice_find_adv_rule_entry - Search a rule entry
4997  * @hw: pointer to the hardware structure
4998  * @lkups: lookup elements or match criteria for the advanced recipe, one
4999  *	   structure per protocol header
5000  * @lkups_cnt: number of protocols
5001  * @recp_id: recipe ID for which we are finding the rule
5002  * @rinfo: other information regarding the rule e.g. priority and action info
5003  *
5004  * Helper function to search for a given advance rule entry
5005  * Returns pointer to entry storing the rule if found
5006  */
5007 static struct ice_adv_fltr_mgmt_list_entry *
5008 ice_find_adv_rule_entry(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5009 			u16 lkups_cnt, u16 recp_id,
5010 			struct ice_adv_rule_info *rinfo)
5011 {
5012 	struct ice_adv_fltr_mgmt_list_entry *list_itr;
5013 	struct ice_switch_info *sw = hw->switch_info;
5014 	int i;
5015 
5016 	list_for_each_entry(list_itr, &sw->recp_list[recp_id].filt_rules,
5017 			    list_entry) {
5018 		bool lkups_matched = true;
5019 
5020 		if (lkups_cnt != list_itr->lkups_cnt)
5021 			continue;
5022 		for (i = 0; i < list_itr->lkups_cnt; i++)
5023 			if (memcmp(&list_itr->lkups[i], &lkups[i],
5024 				   sizeof(*lkups))) {
5025 				lkups_matched = false;
5026 				break;
5027 			}
5028 		if (rinfo->sw_act.flag == list_itr->rule_info.sw_act.flag &&
5029 		    rinfo->tun_type == list_itr->rule_info.tun_type &&
5030 		    lkups_matched)
5031 			return list_itr;
5032 	}
5033 	return NULL;
5034 }
5035 
5036 /**
5037  * ice_adv_add_update_vsi_list
5038  * @hw: pointer to the hardware structure
5039  * @m_entry: pointer to current adv filter management list entry
5040  * @cur_fltr: filter information from the book keeping entry
5041  * @new_fltr: filter information with the new VSI to be added
5042  *
5043  * Call AQ command to add or update previously created VSI list with new VSI.
5044  *
5045  * Helper function to do book keeping associated with adding filter information
5046  * The algorithm to do the booking keeping is described below :
5047  * When a VSI needs to subscribe to a given advanced filter
5048  *	if only one VSI has been added till now
5049  *		Allocate a new VSI list and add two VSIs
5050  *		to this list using switch rule command
5051  *		Update the previously created switch rule with the
5052  *		newly created VSI list ID
5053  *	if a VSI list was previously created
5054  *		Add the new VSI to the previously created VSI list set
5055  *		using the update switch rule command
5056  */
5057 static enum ice_status
5058 ice_adv_add_update_vsi_list(struct ice_hw *hw,
5059 			    struct ice_adv_fltr_mgmt_list_entry *m_entry,
5060 			    struct ice_adv_rule_info *cur_fltr,
5061 			    struct ice_adv_rule_info *new_fltr)
5062 {
5063 	enum ice_status status;
5064 	u16 vsi_list_id = 0;
5065 
5066 	if (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5067 	    cur_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP ||
5068 	    cur_fltr->sw_act.fltr_act == ICE_DROP_PACKET)
5069 		return ICE_ERR_NOT_IMPL;
5070 
5071 	if ((new_fltr->sw_act.fltr_act == ICE_FWD_TO_Q ||
5072 	     new_fltr->sw_act.fltr_act == ICE_FWD_TO_QGRP) &&
5073 	    (cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI ||
5074 	     cur_fltr->sw_act.fltr_act == ICE_FWD_TO_VSI_LIST))
5075 		return ICE_ERR_NOT_IMPL;
5076 
5077 	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
5078 		 /* Only one entry existed in the mapping and it was not already
5079 		  * a part of a VSI list. So, create a VSI list with the old and
5080 		  * new VSIs.
5081 		  */
5082 		struct ice_fltr_info tmp_fltr;
5083 		u16 vsi_handle_arr[2];
5084 
5085 		/* A rule already exists with the new VSI being added */
5086 		if (cur_fltr->sw_act.fwd_id.hw_vsi_id ==
5087 		    new_fltr->sw_act.fwd_id.hw_vsi_id)
5088 			return ICE_ERR_ALREADY_EXISTS;
5089 
5090 		vsi_handle_arr[0] = cur_fltr->sw_act.vsi_handle;
5091 		vsi_handle_arr[1] = new_fltr->sw_act.vsi_handle;
5092 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
5093 						  &vsi_list_id,
5094 						  ICE_SW_LKUP_LAST);
5095 		if (status)
5096 			return status;
5097 
5098 		memset(&tmp_fltr, 0, sizeof(tmp_fltr));
5099 		tmp_fltr.flag = m_entry->rule_info.sw_act.flag;
5100 		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
5101 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
5102 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
5103 		tmp_fltr.lkup_type = ICE_SW_LKUP_LAST;
5104 
5105 		/* Update the previous switch rule of "forward to VSI" to
5106 		 * "fwd to VSI list"
5107 		 */
5108 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5109 		if (status)
5110 			return status;
5111 
5112 		cur_fltr->sw_act.fwd_id.vsi_list_id = vsi_list_id;
5113 		cur_fltr->sw_act.fltr_act = ICE_FWD_TO_VSI_LIST;
5114 		m_entry->vsi_list_info =
5115 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
5116 						vsi_list_id);
5117 	} else {
5118 		u16 vsi_handle = new_fltr->sw_act.vsi_handle;
5119 
5120 		if (!m_entry->vsi_list_info)
5121 			return ICE_ERR_CFG;
5122 
5123 		/* A rule already exists with the new VSI being added */
5124 		if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
5125 			return 0;
5126 
5127 		/* Update the previously created VSI list set with
5128 		 * the new VSI ID passed in
5129 		 */
5130 		vsi_list_id = cur_fltr->sw_act.fwd_id.vsi_list_id;
5131 
5132 		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
5133 						  vsi_list_id, false,
5134 						  ice_aqc_opc_update_sw_rules,
5135 						  ICE_SW_LKUP_LAST);
5136 		/* update VSI list mapping info with new VSI ID */
5137 		if (!status)
5138 			set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
5139 	}
5140 	if (!status)
5141 		m_entry->vsi_count++;
5142 	return status;
5143 }
5144 
5145 /**
5146  * ice_add_adv_rule - helper function to create an advanced switch rule
5147  * @hw: pointer to the hardware structure
5148  * @lkups: information on the words that needs to be looked up. All words
5149  * together makes one recipe
5150  * @lkups_cnt: num of entries in the lkups array
5151  * @rinfo: other information related to the rule that needs to be programmed
5152  * @added_entry: this will return recipe_id, rule_id and vsi_handle. should be
5153  *               ignored is case of error.
5154  *
5155  * This function can program only 1 rule at a time. The lkups is used to
5156  * describe the all the words that forms the "lookup" portion of the recipe.
5157  * These words can span multiple protocols. Callers to this function need to
5158  * pass in a list of protocol headers with lookup information along and mask
5159  * that determines which words are valid from the given protocol header.
5160  * rinfo describes other information related to this rule such as forwarding
5161  * IDs, priority of this rule, etc.
5162  */
5163 enum ice_status
5164 ice_add_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5165 		 u16 lkups_cnt, struct ice_adv_rule_info *rinfo,
5166 		 struct ice_rule_query_data *added_entry)
5167 {
5168 	struct ice_adv_fltr_mgmt_list_entry *m_entry, *adv_fltr = NULL;
5169 	u16 rid = 0, i, pkt_len, rule_buf_sz, vsi_handle;
5170 	const struct ice_dummy_pkt_offsets *pkt_offsets;
5171 	struct ice_aqc_sw_rules_elem *s_rule = NULL;
5172 	struct list_head *rule_head;
5173 	struct ice_switch_info *sw;
5174 	enum ice_status status;
5175 	const u8 *pkt = NULL;
5176 	u16 word_cnt;
5177 	u32 act = 0;
5178 	u8 q_rgn;
5179 
5180 	/* Initialize profile to result index bitmap */
5181 	if (!hw->switch_info->prof_res_bm_init) {
5182 		hw->switch_info->prof_res_bm_init = 1;
5183 		ice_init_prof_result_bm(hw);
5184 	}
5185 
5186 	if (!lkups_cnt)
5187 		return ICE_ERR_PARAM;
5188 
5189 	/* get # of words we need to match */
5190 	word_cnt = 0;
5191 	for (i = 0; i < lkups_cnt; i++) {
5192 		u16 j, *ptr;
5193 
5194 		ptr = (u16 *)&lkups[i].m_u;
5195 		for (j = 0; j < sizeof(lkups->m_u) / sizeof(u16); j++)
5196 			if (ptr[j] != 0)
5197 				word_cnt++;
5198 	}
5199 
5200 	if (!word_cnt || word_cnt > ICE_MAX_CHAIN_WORDS)
5201 		return ICE_ERR_PARAM;
5202 
5203 	/* make sure that we can locate a dummy packet */
5204 	ice_find_dummy_packet(lkups, lkups_cnt, rinfo->tun_type, &pkt, &pkt_len,
5205 			      &pkt_offsets);
5206 	if (!pkt) {
5207 		status = ICE_ERR_PARAM;
5208 		goto err_ice_add_adv_rule;
5209 	}
5210 
5211 	if (!(rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI ||
5212 	      rinfo->sw_act.fltr_act == ICE_FWD_TO_Q ||
5213 	      rinfo->sw_act.fltr_act == ICE_FWD_TO_QGRP ||
5214 	      rinfo->sw_act.fltr_act == ICE_DROP_PACKET))
5215 		return ICE_ERR_CFG;
5216 
5217 	vsi_handle = rinfo->sw_act.vsi_handle;
5218 	if (!ice_is_vsi_valid(hw, vsi_handle))
5219 		return ICE_ERR_PARAM;
5220 
5221 	if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI)
5222 		rinfo->sw_act.fwd_id.hw_vsi_id =
5223 			ice_get_hw_vsi_num(hw, vsi_handle);
5224 	if (rinfo->sw_act.flag & ICE_FLTR_TX)
5225 		rinfo->sw_act.src = ice_get_hw_vsi_num(hw, vsi_handle);
5226 
5227 	status = ice_add_adv_recipe(hw, lkups, lkups_cnt, rinfo, &rid);
5228 	if (status)
5229 		return status;
5230 	m_entry = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
5231 	if (m_entry) {
5232 		/* we have to add VSI to VSI_LIST and increment vsi_count.
5233 		 * Also Update VSI list so that we can change forwarding rule
5234 		 * if the rule already exists, we will check if it exists with
5235 		 * same vsi_id, if not then add it to the VSI list if it already
5236 		 * exists if not then create a VSI list and add the existing VSI
5237 		 * ID and the new VSI ID to the list
5238 		 * We will add that VSI to the list
5239 		 */
5240 		status = ice_adv_add_update_vsi_list(hw, m_entry,
5241 						     &m_entry->rule_info,
5242 						     rinfo);
5243 		if (added_entry) {
5244 			added_entry->rid = rid;
5245 			added_entry->rule_id = m_entry->rule_info.fltr_rule_id;
5246 			added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
5247 		}
5248 		return status;
5249 	}
5250 	rule_buf_sz = ICE_SW_RULE_RX_TX_NO_HDR_SIZE + pkt_len;
5251 	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
5252 	if (!s_rule)
5253 		return ICE_ERR_NO_MEMORY;
5254 	if (!rinfo->flags_info.act_valid) {
5255 		act |= ICE_SINGLE_ACT_LAN_ENABLE;
5256 		act |= ICE_SINGLE_ACT_LB_ENABLE;
5257 	} else {
5258 		act |= rinfo->flags_info.act & (ICE_SINGLE_ACT_LAN_ENABLE |
5259 						ICE_SINGLE_ACT_LB_ENABLE);
5260 	}
5261 
5262 	switch (rinfo->sw_act.fltr_act) {
5263 	case ICE_FWD_TO_VSI:
5264 		act |= (rinfo->sw_act.fwd_id.hw_vsi_id <<
5265 			ICE_SINGLE_ACT_VSI_ID_S) & ICE_SINGLE_ACT_VSI_ID_M;
5266 		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_VALID_BIT;
5267 		break;
5268 	case ICE_FWD_TO_Q:
5269 		act |= ICE_SINGLE_ACT_TO_Q;
5270 		act |= (rinfo->sw_act.fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
5271 		       ICE_SINGLE_ACT_Q_INDEX_M;
5272 		break;
5273 	case ICE_FWD_TO_QGRP:
5274 		q_rgn = rinfo->sw_act.qgrp_size > 0 ?
5275 			(u8)ilog2(rinfo->sw_act.qgrp_size) : 0;
5276 		act |= ICE_SINGLE_ACT_TO_Q;
5277 		act |= (rinfo->sw_act.fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
5278 		       ICE_SINGLE_ACT_Q_INDEX_M;
5279 		act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
5280 		       ICE_SINGLE_ACT_Q_REGION_M;
5281 		break;
5282 	case ICE_DROP_PACKET:
5283 		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
5284 		       ICE_SINGLE_ACT_VALID_BIT;
5285 		break;
5286 	default:
5287 		status = ICE_ERR_CFG;
5288 		goto err_ice_add_adv_rule;
5289 	}
5290 
5291 	/* set the rule LOOKUP type based on caller specified 'Rx'
5292 	 * instead of hardcoding it to be either LOOKUP_TX/RX
5293 	 *
5294 	 * for 'Rx' set the source to be the port number
5295 	 * for 'Tx' set the source to be the source HW VSI number (determined
5296 	 * by caller)
5297 	 */
5298 	if (rinfo->rx) {
5299 		s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX);
5300 		s_rule->pdata.lkup_tx_rx.src =
5301 			cpu_to_le16(hw->port_info->lport);
5302 	} else {
5303 		s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
5304 		s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(rinfo->sw_act.src);
5305 	}
5306 
5307 	s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(rid);
5308 	s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);
5309 
5310 	status = ice_fill_adv_dummy_packet(lkups, lkups_cnt, s_rule, pkt,
5311 					   pkt_len, pkt_offsets);
5312 	if (status)
5313 		goto err_ice_add_adv_rule;
5314 
5315 	if (rinfo->tun_type != ICE_NON_TUN) {
5316 		status = ice_fill_adv_packet_tun(hw, rinfo->tun_type,
5317 						 s_rule->pdata.lkup_tx_rx.hdr,
5318 						 pkt_offsets);
5319 		if (status)
5320 			goto err_ice_add_adv_rule;
5321 	}
5322 
5323 	status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
5324 				 rule_buf_sz, 1, ice_aqc_opc_add_sw_rules,
5325 				 NULL);
5326 	if (status)
5327 		goto err_ice_add_adv_rule;
5328 	adv_fltr = devm_kzalloc(ice_hw_to_dev(hw),
5329 				sizeof(struct ice_adv_fltr_mgmt_list_entry),
5330 				GFP_KERNEL);
5331 	if (!adv_fltr) {
5332 		status = ICE_ERR_NO_MEMORY;
5333 		goto err_ice_add_adv_rule;
5334 	}
5335 
5336 	adv_fltr->lkups = devm_kmemdup(ice_hw_to_dev(hw), lkups,
5337 				       lkups_cnt * sizeof(*lkups), GFP_KERNEL);
5338 	if (!adv_fltr->lkups) {
5339 		status = ICE_ERR_NO_MEMORY;
5340 		goto err_ice_add_adv_rule;
5341 	}
5342 
5343 	adv_fltr->lkups_cnt = lkups_cnt;
5344 	adv_fltr->rule_info = *rinfo;
5345 	adv_fltr->rule_info.fltr_rule_id =
5346 		le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
5347 	sw = hw->switch_info;
5348 	sw->recp_list[rid].adv_rule = true;
5349 	rule_head = &sw->recp_list[rid].filt_rules;
5350 
5351 	if (rinfo->sw_act.fltr_act == ICE_FWD_TO_VSI)
5352 		adv_fltr->vsi_count = 1;
5353 
5354 	/* Add rule entry to book keeping list */
5355 	list_add(&adv_fltr->list_entry, rule_head);
5356 	if (added_entry) {
5357 		added_entry->rid = rid;
5358 		added_entry->rule_id = adv_fltr->rule_info.fltr_rule_id;
5359 		added_entry->vsi_handle = rinfo->sw_act.vsi_handle;
5360 	}
5361 err_ice_add_adv_rule:
5362 	if (status && adv_fltr) {
5363 		devm_kfree(ice_hw_to_dev(hw), adv_fltr->lkups);
5364 		devm_kfree(ice_hw_to_dev(hw), adv_fltr);
5365 	}
5366 
5367 	kfree(s_rule);
5368 
5369 	return status;
5370 }
5371 
5372 /**
5373  * ice_replay_vsi_fltr - Replay filters for requested VSI
5374  * @hw: pointer to the hardware structure
5375  * @vsi_handle: driver VSI handle
5376  * @recp_id: Recipe ID for which rules need to be replayed
5377  * @list_head: list for which filters need to be replayed
5378  *
5379  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
5380  * It is required to pass valid VSI handle.
5381  */
5382 static enum ice_status
5383 ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
5384 		    struct list_head *list_head)
5385 {
5386 	struct ice_fltr_mgmt_list_entry *itr;
5387 	enum ice_status status = 0;
5388 	u16 hw_vsi_id;
5389 
5390 	if (list_empty(list_head))
5391 		return status;
5392 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
5393 
5394 	list_for_each_entry(itr, list_head, list_entry) {
5395 		struct ice_fltr_list_entry f_entry;
5396 
5397 		f_entry.fltr_info = itr->fltr_info;
5398 		if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
5399 		    itr->fltr_info.vsi_handle == vsi_handle) {
5400 			/* update the src in case it is VSI num */
5401 			if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
5402 				f_entry.fltr_info.src = hw_vsi_id;
5403 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
5404 			if (status)
5405 				goto end;
5406 			continue;
5407 		}
5408 		if (!itr->vsi_list_info ||
5409 		    !test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
5410 			continue;
5411 		/* Clearing it so that the logic can add it back */
5412 		clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
5413 		f_entry.fltr_info.vsi_handle = vsi_handle;
5414 		f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
5415 		/* update the src in case it is VSI num */
5416 		if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
5417 			f_entry.fltr_info.src = hw_vsi_id;
5418 		if (recp_id == ICE_SW_LKUP_VLAN)
5419 			status = ice_add_vlan_internal(hw, &f_entry);
5420 		else
5421 			status = ice_add_rule_internal(hw, recp_id, &f_entry);
5422 		if (status)
5423 			goto end;
5424 	}
5425 end:
5426 	return status;
5427 }
5428 
5429 /**
5430  * ice_adv_rem_update_vsi_list
5431  * @hw: pointer to the hardware structure
5432  * @vsi_handle: VSI handle of the VSI to remove
5433  * @fm_list: filter management entry for which the VSI list management needs to
5434  *	     be done
5435  */
5436 static enum ice_status
5437 ice_adv_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
5438 			    struct ice_adv_fltr_mgmt_list_entry *fm_list)
5439 {
5440 	struct ice_vsi_list_map_info *vsi_list_info;
5441 	enum ice_sw_lkup_type lkup_type;
5442 	enum ice_status status;
5443 	u16 vsi_list_id;
5444 
5445 	if (fm_list->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST ||
5446 	    fm_list->vsi_count == 0)
5447 		return ICE_ERR_PARAM;
5448 
5449 	/* A rule with the VSI being removed does not exist */
5450 	if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
5451 		return ICE_ERR_DOES_NOT_EXIST;
5452 
5453 	lkup_type = ICE_SW_LKUP_LAST;
5454 	vsi_list_id = fm_list->rule_info.sw_act.fwd_id.vsi_list_id;
5455 	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
5456 					  ice_aqc_opc_update_sw_rules,
5457 					  lkup_type);
5458 	if (status)
5459 		return status;
5460 
5461 	fm_list->vsi_count--;
5462 	clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
5463 	vsi_list_info = fm_list->vsi_list_info;
5464 	if (fm_list->vsi_count == 1) {
5465 		struct ice_fltr_info tmp_fltr;
5466 		u16 rem_vsi_handle;
5467 
5468 		rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
5469 						ICE_MAX_VSI);
5470 		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
5471 			return ICE_ERR_OUT_OF_RANGE;
5472 
5473 		/* Make sure VSI list is empty before removing it below */
5474 		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
5475 						  vsi_list_id, true,
5476 						  ice_aqc_opc_update_sw_rules,
5477 						  lkup_type);
5478 		if (status)
5479 			return status;
5480 
5481 		memset(&tmp_fltr, 0, sizeof(tmp_fltr));
5482 		tmp_fltr.flag = fm_list->rule_info.sw_act.flag;
5483 		tmp_fltr.fltr_rule_id = fm_list->rule_info.fltr_rule_id;
5484 		fm_list->rule_info.sw_act.fltr_act = ICE_FWD_TO_VSI;
5485 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI;
5486 		tmp_fltr.fwd_id.hw_vsi_id =
5487 			ice_get_hw_vsi_num(hw, rem_vsi_handle);
5488 		fm_list->rule_info.sw_act.fwd_id.hw_vsi_id =
5489 			ice_get_hw_vsi_num(hw, rem_vsi_handle);
5490 		fm_list->rule_info.sw_act.vsi_handle = rem_vsi_handle;
5491 
5492 		/* Update the previous switch rule of "MAC forward to VSI" to
5493 		 * "MAC fwd to VSI list"
5494 		 */
5495 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
5496 		if (status) {
5497 			ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
5498 				  tmp_fltr.fwd_id.hw_vsi_id, status);
5499 			return status;
5500 		}
5501 		fm_list->vsi_list_info->ref_cnt--;
5502 
5503 		/* Remove the VSI list since it is no longer used */
5504 		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
5505 		if (status) {
5506 			ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
5507 				  vsi_list_id, status);
5508 			return status;
5509 		}
5510 
5511 		list_del(&vsi_list_info->list_entry);
5512 		devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
5513 		fm_list->vsi_list_info = NULL;
5514 	}
5515 
5516 	return status;
5517 }
5518 
5519 /**
5520  * ice_rem_adv_rule - removes existing advanced switch rule
5521  * @hw: pointer to the hardware structure
5522  * @lkups: information on the words that needs to be looked up. All words
5523  *         together makes one recipe
5524  * @lkups_cnt: num of entries in the lkups array
5525  * @rinfo: Its the pointer to the rule information for the rule
5526  *
5527  * This function can be used to remove 1 rule at a time. The lkups is
5528  * used to describe all the words that forms the "lookup" portion of the
5529  * rule. These words can span multiple protocols. Callers to this function
5530  * need to pass in a list of protocol headers with lookup information along
5531  * and mask that determines which words are valid from the given protocol
5532  * header. rinfo describes other information related to this rule such as
5533  * forwarding IDs, priority of this rule, etc.
5534  */
5535 static enum ice_status
5536 ice_rem_adv_rule(struct ice_hw *hw, struct ice_adv_lkup_elem *lkups,
5537 		 u16 lkups_cnt, struct ice_adv_rule_info *rinfo)
5538 {
5539 	struct ice_adv_fltr_mgmt_list_entry *list_elem;
5540 	struct ice_prot_lkup_ext lkup_exts;
5541 	enum ice_status status = 0;
5542 	bool remove_rule = false;
5543 	struct mutex *rule_lock; /* Lock to protect filter rule list */
5544 	u16 i, rid, vsi_handle;
5545 
5546 	memset(&lkup_exts, 0, sizeof(lkup_exts));
5547 	for (i = 0; i < lkups_cnt; i++) {
5548 		u16 count;
5549 
5550 		if (lkups[i].type >= ICE_PROTOCOL_LAST)
5551 			return ICE_ERR_CFG;
5552 
5553 		count = ice_fill_valid_words(&lkups[i], &lkup_exts);
5554 		if (!count)
5555 			return ICE_ERR_CFG;
5556 	}
5557 
5558 	/* Create any special protocol/offset pairs, such as looking at tunnel
5559 	 * bits by extracting metadata
5560 	 */
5561 	status = ice_add_special_words(rinfo, &lkup_exts);
5562 	if (status)
5563 		return status;
5564 
5565 	rid = ice_find_recp(hw, &lkup_exts, rinfo->tun_type);
5566 	/* If did not find a recipe that match the existing criteria */
5567 	if (rid == ICE_MAX_NUM_RECIPES)
5568 		return ICE_ERR_PARAM;
5569 
5570 	rule_lock = &hw->switch_info->recp_list[rid].filt_rule_lock;
5571 	list_elem = ice_find_adv_rule_entry(hw, lkups, lkups_cnt, rid, rinfo);
5572 	/* the rule is already removed */
5573 	if (!list_elem)
5574 		return 0;
5575 	mutex_lock(rule_lock);
5576 	if (list_elem->rule_info.sw_act.fltr_act != ICE_FWD_TO_VSI_LIST) {
5577 		remove_rule = true;
5578 	} else if (list_elem->vsi_count > 1) {
5579 		remove_rule = false;
5580 		vsi_handle = rinfo->sw_act.vsi_handle;
5581 		status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
5582 	} else {
5583 		vsi_handle = rinfo->sw_act.vsi_handle;
5584 		status = ice_adv_rem_update_vsi_list(hw, vsi_handle, list_elem);
5585 		if (status) {
5586 			mutex_unlock(rule_lock);
5587 			return status;
5588 		}
5589 		if (list_elem->vsi_count == 0)
5590 			remove_rule = true;
5591 	}
5592 	mutex_unlock(rule_lock);
5593 	if (remove_rule) {
5594 		struct ice_aqc_sw_rules_elem *s_rule;
5595 		u16 rule_buf_sz;
5596 
5597 		rule_buf_sz = ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
5598 		s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
5599 		if (!s_rule)
5600 			return ICE_ERR_NO_MEMORY;
5601 		s_rule->pdata.lkup_tx_rx.act = 0;
5602 		s_rule->pdata.lkup_tx_rx.index =
5603 			cpu_to_le16(list_elem->rule_info.fltr_rule_id);
5604 		s_rule->pdata.lkup_tx_rx.hdr_len = 0;
5605 		status = ice_aq_sw_rules(hw, (struct ice_aqc_sw_rules *)s_rule,
5606 					 rule_buf_sz, 1,
5607 					 ice_aqc_opc_remove_sw_rules, NULL);
5608 		if (!status || status == ICE_ERR_DOES_NOT_EXIST) {
5609 			struct ice_switch_info *sw = hw->switch_info;
5610 
5611 			mutex_lock(rule_lock);
5612 			list_del(&list_elem->list_entry);
5613 			devm_kfree(ice_hw_to_dev(hw), list_elem->lkups);
5614 			devm_kfree(ice_hw_to_dev(hw), list_elem);
5615 			mutex_unlock(rule_lock);
5616 			if (list_empty(&sw->recp_list[rid].filt_rules))
5617 				sw->recp_list[rid].adv_rule = false;
5618 		}
5619 		kfree(s_rule);
5620 	}
5621 	return status;
5622 }
5623 
5624 /**
5625  * ice_rem_adv_rule_by_id - removes existing advanced switch rule by ID
5626  * @hw: pointer to the hardware structure
5627  * @remove_entry: data struct which holds rule_id, VSI handle and recipe ID
5628  *
5629  * This function is used to remove 1 rule at a time. The removal is based on
5630  * the remove_entry parameter. This function will remove rule for a given
5631  * vsi_handle with a given rule_id which is passed as parameter in remove_entry
5632  */
5633 enum ice_status
5634 ice_rem_adv_rule_by_id(struct ice_hw *hw,
5635 		       struct ice_rule_query_data *remove_entry)
5636 {
5637 	struct ice_adv_fltr_mgmt_list_entry *list_itr;
5638 	struct list_head *list_head;
5639 	struct ice_adv_rule_info rinfo;
5640 	struct ice_switch_info *sw;
5641 
5642 	sw = hw->switch_info;
5643 	if (!sw->recp_list[remove_entry->rid].recp_created)
5644 		return ICE_ERR_PARAM;
5645 	list_head = &sw->recp_list[remove_entry->rid].filt_rules;
5646 	list_for_each_entry(list_itr, list_head, list_entry) {
5647 		if (list_itr->rule_info.fltr_rule_id ==
5648 		    remove_entry->rule_id) {
5649 			rinfo = list_itr->rule_info;
5650 			rinfo.sw_act.vsi_handle = remove_entry->vsi_handle;
5651 			return ice_rem_adv_rule(hw, list_itr->lkups,
5652 						list_itr->lkups_cnt, &rinfo);
5653 		}
5654 	}
5655 	/* either list is empty or unable to find rule */
5656 	return ICE_ERR_DOES_NOT_EXIST;
5657 }
5658 
5659 /**
5660  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
5661  * @hw: pointer to the hardware structure
5662  * @vsi_handle: driver VSI handle
5663  *
5664  * Replays filters for requested VSI via vsi_handle.
5665  */
5666 enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
5667 {
5668 	struct ice_switch_info *sw = hw->switch_info;
5669 	enum ice_status status = 0;
5670 	u8 i;
5671 
5672 	for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
5673 		struct list_head *head;
5674 
5675 		head = &sw->recp_list[i].filt_replay_rules;
5676 		status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
5677 		if (status)
5678 			return status;
5679 	}
5680 	return status;
5681 }
5682 
5683 /**
5684  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
5685  * @hw: pointer to the HW struct
5686  *
5687  * Deletes the filter replay rules.
5688  */
5689 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
5690 {
5691 	struct ice_switch_info *sw = hw->switch_info;
5692 	u8 i;
5693 
5694 	if (!sw)
5695 		return;
5696 
5697 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
5698 		if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
5699 			struct list_head *l_head;
5700 
5701 			l_head = &sw->recp_list[i].filt_replay_rules;
5702 			if (!sw->recp_list[i].adv_rule)
5703 				ice_rem_sw_rule_info(hw, l_head);
5704 			else
5705 				ice_rem_adv_rule_info(hw, l_head);
5706 		}
5707 	}
5708 }
5709