xref: /freebsd/sys/dev/ice/ice_switch.c (revision cc1a53bc1aea0675d64e9547cdca241612906592)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2021, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*$FreeBSD$*/
32 
33 #include "ice_switch.h"
34 #include "ice_flex_type.h"
35 #include "ice_flow.h"
36 
37 #define ICE_ETH_DA_OFFSET		0
38 #define ICE_ETH_ETHTYPE_OFFSET		12
39 #define ICE_ETH_VLAN_TCI_OFFSET		14
40 #define ICE_MAX_VLAN_ID			0xFFF
41 #define ICE_IPV6_ETHER_ID		0x86DD
42 #define ICE_ETH_P_8021Q			0x8100
43 
44 /* Dummy ethernet header needed in the ice_aqc_sw_rules_elem
45  * struct to configure any switch filter rules.
46  * {DA (6 bytes), SA(6 bytes),
47  * Ether type (2 bytes for header without VLAN tag) OR
48  * VLAN tag (4 bytes for header with VLAN tag) }
49  *
50  * Word on Hardcoded values
51  * byte 0 = 0x2: to identify it as locally administered DA MAC
52  * byte 6 = 0x2: to identify it as locally administered SA MAC
53  * byte 12 = 0x81 & byte 13 = 0x00:
54  *	In case of VLAN filter first two bytes defines ether type (0x8100)
55  *	and remaining two bytes are placeholder for programming a given VLAN ID
56  *	In case of Ether type filter it is treated as header without VLAN tag
57  *	and byte 12 and 13 is used to program a given Ether type instead
58  */
59 static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
60 							0x2, 0, 0, 0, 0, 0,
61 							0x81, 0, 0, 0};
62 
63 /**
64  * ice_init_def_sw_recp - initialize the recipe book keeping tables
65  * @hw: pointer to the HW struct
66  * @recp_list: pointer to sw recipe list
67  *
68  * Allocate memory for the entire recipe table and initialize the structures/
69  * entries corresponding to basic recipes.
70  */
71 enum ice_status
72 ice_init_def_sw_recp(struct ice_hw *hw, struct ice_sw_recipe **recp_list)
73 {
74 	struct ice_sw_recipe *recps;
75 	u8 i;
76 
77 	recps = (struct ice_sw_recipe *)
78 		ice_calloc(hw, ICE_MAX_NUM_RECIPES, sizeof(*recps));
79 	if (!recps)
80 		return ICE_ERR_NO_MEMORY;
81 
82 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
83 		recps[i].root_rid = i;
84 		INIT_LIST_HEAD(&recps[i].filt_rules);
85 		INIT_LIST_HEAD(&recps[i].filt_replay_rules);
86 		INIT_LIST_HEAD(&recps[i].rg_list);
87 		ice_init_lock(&recps[i].filt_rule_lock);
88 	}
89 
90 	*recp_list = recps;
91 
92 	return ICE_SUCCESS;
93 }
94 
95 /**
96  * ice_aq_get_sw_cfg - get switch configuration
97  * @hw: pointer to the hardware structure
98  * @buf: pointer to the result buffer
99  * @buf_size: length of the buffer available for response
100  * @req_desc: pointer to requested descriptor
101  * @num_elems: pointer to number of elements
102  * @cd: pointer to command details structure or NULL
103  *
104  * Get switch configuration (0x0200) to be placed in buf.
105  * This admin command returns information such as initial VSI/port number
106  * and switch ID it belongs to.
107  *
108  * NOTE: *req_desc is both an input/output parameter.
109  * The caller of this function first calls this function with *request_desc set
110  * to 0. If the response from f/w has *req_desc set to 0, all the switch
111  * configuration information has been returned; if non-zero (meaning not all
112  * the information was returned), the caller should call this function again
113  * with *req_desc set to the previous value returned by f/w to get the
114  * next block of switch configuration information.
115  *
116  * *num_elems is output only parameter. This reflects the number of elements
117  * in response buffer. The caller of this function to use *num_elems while
118  * parsing the response buffer.
119  */
120 static enum ice_status
121 ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp_elem *buf,
122 		  u16 buf_size, u16 *req_desc, u16 *num_elems,
123 		  struct ice_sq_cd *cd)
124 {
125 	struct ice_aqc_get_sw_cfg *cmd;
126 	struct ice_aq_desc desc;
127 	enum ice_status status;
128 
129 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
130 	cmd = &desc.params.get_sw_conf;
131 	cmd->element = CPU_TO_LE16(*req_desc);
132 
133 	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
134 	if (!status) {
135 		*req_desc = LE16_TO_CPU(cmd->element);
136 		*num_elems = LE16_TO_CPU(cmd->num_elems);
137 	}
138 
139 	return status;
140 }
141 
142 /**
143  * ice_alloc_rss_global_lut - allocate a RSS global LUT
144  * @hw: pointer to the HW struct
145  * @shared_res: true to allocate as a shared resource and false to allocate as a dedicated resource
146  * @global_lut_id: output parameter for the RSS global LUT's ID
147  */
148 enum ice_status ice_alloc_rss_global_lut(struct ice_hw *hw, bool shared_res, u16 *global_lut_id)
149 {
150 	struct ice_aqc_alloc_free_res_elem *sw_buf;
151 	enum ice_status status;
152 	u16 buf_len;
153 
154 	buf_len = ice_struct_size(sw_buf, elem, 1);
155 	sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
156 	if (!sw_buf)
157 		return ICE_ERR_NO_MEMORY;
158 
159 	sw_buf->num_elems = CPU_TO_LE16(1);
160 	sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_GLOBAL_RSS_HASH |
161 				       (shared_res ? ICE_AQC_RES_TYPE_FLAG_SHARED :
162 				       ICE_AQC_RES_TYPE_FLAG_DEDICATED));
163 
164 	status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, ice_aqc_opc_alloc_res, NULL);
165 	if (status) {
166 		ice_debug(hw, ICE_DBG_RES, "Failed to allocate %s RSS global LUT, status %d\n",
167 			  shared_res ? "shared" : "dedicated", status);
168 		goto ice_alloc_global_lut_exit;
169 	}
170 
171 	*global_lut_id = LE16_TO_CPU(sw_buf->elem[0].e.sw_resp);
172 
173 ice_alloc_global_lut_exit:
174 	ice_free(hw, sw_buf);
175 	return status;
176 }
177 
178 /**
179  * ice_free_rss_global_lut - free a RSS global LUT
180  * @hw: pointer to the HW struct
181  * @global_lut_id: ID of the RSS global LUT to free
182  */
183 enum ice_status ice_free_rss_global_lut(struct ice_hw *hw, u16 global_lut_id)
184 {
185 	struct ice_aqc_alloc_free_res_elem *sw_buf;
186 	u16 buf_len, num_elems = 1;
187 	enum ice_status status;
188 
189 	buf_len = ice_struct_size(sw_buf, elem, num_elems);
190 	sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
191 	if (!sw_buf)
192 		return ICE_ERR_NO_MEMORY;
193 
194 	sw_buf->num_elems = CPU_TO_LE16(num_elems);
195 	sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_GLOBAL_RSS_HASH);
196 	sw_buf->elem[0].e.sw_resp = CPU_TO_LE16(global_lut_id);
197 
198 	status = ice_aq_alloc_free_res(hw, num_elems, sw_buf, buf_len, ice_aqc_opc_free_res, NULL);
199 	if (status)
200 		ice_debug(hw, ICE_DBG_RES, "Failed to free RSS global LUT %d, status %d\n",
201 			  global_lut_id, status);
202 
203 	ice_free(hw, sw_buf);
204 	return status;
205 }
206 
207 /**
208  * ice_alloc_sw - allocate resources specific to switch
209  * @hw: pointer to the HW struct
210  * @ena_stats: true to turn on VEB stats
211  * @shared_res: true for shared resource, false for dedicated resource
212  * @sw_id: switch ID returned
213  * @counter_id: VEB counter ID returned
214  *
215  * allocates switch resources (SWID and VEB counter) (0x0208)
216  */
217 enum ice_status
218 ice_alloc_sw(struct ice_hw *hw, bool ena_stats, bool shared_res, u16 *sw_id,
219 	     u16 *counter_id)
220 {
221 	struct ice_aqc_alloc_free_res_elem *sw_buf;
222 	struct ice_aqc_res_elem *sw_ele;
223 	enum ice_status status;
224 	u16 buf_len;
225 
226 	buf_len = ice_struct_size(sw_buf, elem, 1);
227 	sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
228 	if (!sw_buf)
229 		return ICE_ERR_NO_MEMORY;
230 
231 	/* Prepare buffer for switch ID.
232 	 * The number of resource entries in buffer is passed as 1 since only a
233 	 * single switch/VEB instance is allocated, and hence a single sw_id
234 	 * is requested.
235 	 */
236 	sw_buf->num_elems = CPU_TO_LE16(1);
237 	sw_buf->res_type =
238 		CPU_TO_LE16(ICE_AQC_RES_TYPE_SWID |
239 			    (shared_res ? ICE_AQC_RES_TYPE_FLAG_SHARED :
240 			    ICE_AQC_RES_TYPE_FLAG_DEDICATED));
241 
242 	status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
243 				       ice_aqc_opc_alloc_res, NULL);
244 
245 	if (status)
246 		goto ice_alloc_sw_exit;
247 
248 	sw_ele = &sw_buf->elem[0];
249 	*sw_id = LE16_TO_CPU(sw_ele->e.sw_resp);
250 
251 	if (ena_stats) {
252 		/* Prepare buffer for VEB Counter */
253 		enum ice_adminq_opc opc = ice_aqc_opc_alloc_res;
254 		struct ice_aqc_alloc_free_res_elem *counter_buf;
255 		struct ice_aqc_res_elem *counter_ele;
256 
257 		counter_buf = (struct ice_aqc_alloc_free_res_elem *)
258 				ice_malloc(hw, buf_len);
259 		if (!counter_buf) {
260 			status = ICE_ERR_NO_MEMORY;
261 			goto ice_alloc_sw_exit;
262 		}
263 
264 		/* The number of resource entries in buffer is passed as 1 since
265 		 * only a single switch/VEB instance is allocated, and hence a
266 		 * single VEB counter is requested.
267 		 */
268 		counter_buf->num_elems = CPU_TO_LE16(1);
269 		counter_buf->res_type =
270 			CPU_TO_LE16(ICE_AQC_RES_TYPE_VEB_COUNTER |
271 				    ICE_AQC_RES_TYPE_FLAG_DEDICATED);
272 		status = ice_aq_alloc_free_res(hw, 1, counter_buf, buf_len,
273 					       opc, NULL);
274 
275 		if (status) {
276 			ice_free(hw, counter_buf);
277 			goto ice_alloc_sw_exit;
278 		}
279 		counter_ele = &counter_buf->elem[0];
280 		*counter_id = LE16_TO_CPU(counter_ele->e.sw_resp);
281 		ice_free(hw, counter_buf);
282 	}
283 
284 ice_alloc_sw_exit:
285 	ice_free(hw, sw_buf);
286 	return status;
287 }
288 
289 /**
290  * ice_free_sw - free resources specific to switch
291  * @hw: pointer to the HW struct
292  * @sw_id: switch ID returned
293  * @counter_id: VEB counter ID returned
294  *
295  * free switch resources (SWID and VEB counter) (0x0209)
296  *
297  * NOTE: This function frees multiple resources. It continues
298  * releasing other resources even after it encounters error.
299  * The error code returned is the last error it encountered.
300  */
301 enum ice_status ice_free_sw(struct ice_hw *hw, u16 sw_id, u16 counter_id)
302 {
303 	struct ice_aqc_alloc_free_res_elem *sw_buf, *counter_buf;
304 	enum ice_status status, ret_status;
305 	u16 buf_len;
306 
307 	buf_len = ice_struct_size(sw_buf, elem, 1);
308 	sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
309 	if (!sw_buf)
310 		return ICE_ERR_NO_MEMORY;
311 
312 	/* Prepare buffer to free for switch ID res.
313 	 * The number of resource entries in buffer is passed as 1 since only a
314 	 * single switch/VEB instance is freed, and hence a single sw_id
315 	 * is released.
316 	 */
317 	sw_buf->num_elems = CPU_TO_LE16(1);
318 	sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_SWID);
319 	sw_buf->elem[0].e.sw_resp = CPU_TO_LE16(sw_id);
320 
321 	ret_status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
322 					   ice_aqc_opc_free_res, NULL);
323 
324 	if (ret_status)
325 		ice_debug(hw, ICE_DBG_SW, "CQ CMD Buffer:\n");
326 
327 	/* Prepare buffer to free for VEB Counter resource */
328 	counter_buf = (struct ice_aqc_alloc_free_res_elem *)
329 			ice_malloc(hw, buf_len);
330 	if (!counter_buf) {
331 		ice_free(hw, sw_buf);
332 		return ICE_ERR_NO_MEMORY;
333 	}
334 
335 	/* The number of resource entries in buffer is passed as 1 since only a
336 	 * single switch/VEB instance is freed, and hence a single VEB counter
337 	 * is released
338 	 */
339 	counter_buf->num_elems = CPU_TO_LE16(1);
340 	counter_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_VEB_COUNTER);
341 	counter_buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
342 
343 	status = ice_aq_alloc_free_res(hw, 1, counter_buf, buf_len,
344 				       ice_aqc_opc_free_res, NULL);
345 	if (status) {
346 		ice_debug(hw, ICE_DBG_SW, "VEB counter resource could not be freed\n");
347 		ret_status = status;
348 	}
349 
350 	ice_free(hw, counter_buf);
351 	ice_free(hw, sw_buf);
352 	return ret_status;
353 }
354 
355 /**
356  * ice_aq_add_vsi
357  * @hw: pointer to the HW struct
358  * @vsi_ctx: pointer to a VSI context struct
359  * @cd: pointer to command details structure or NULL
360  *
361  * Add a VSI context to the hardware (0x0210)
362  */
363 enum ice_status
364 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
365 	       struct ice_sq_cd *cd)
366 {
367 	struct ice_aqc_add_update_free_vsi_resp *res;
368 	struct ice_aqc_add_get_update_free_vsi *cmd;
369 	struct ice_aq_desc desc;
370 	enum ice_status status;
371 
372 	cmd = &desc.params.vsi_cmd;
373 	res = &desc.params.add_update_free_vsi_res;
374 
375 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
376 
377 	if (!vsi_ctx->alloc_from_pool)
378 		cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num |
379 					   ICE_AQ_VSI_IS_VALID);
380 	cmd->vf_id = vsi_ctx->vf_num;
381 
382 	cmd->vsi_flags = CPU_TO_LE16(vsi_ctx->flags);
383 
384 	desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
385 
386 	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
387 				 sizeof(vsi_ctx->info), cd);
388 
389 	if (!status) {
390 		vsi_ctx->vsi_num = LE16_TO_CPU(res->vsi_num) & ICE_AQ_VSI_NUM_M;
391 		vsi_ctx->vsis_allocd = LE16_TO_CPU(res->vsi_used);
392 		vsi_ctx->vsis_unallocated = LE16_TO_CPU(res->vsi_free);
393 	}
394 
395 	return status;
396 }
397 
398 /**
399  * ice_aq_free_vsi
400  * @hw: pointer to the HW struct
401  * @vsi_ctx: pointer to a VSI context struct
402  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
403  * @cd: pointer to command details structure or NULL
404  *
405  * Free VSI context info from hardware (0x0213)
406  */
407 enum ice_status
408 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
409 		bool keep_vsi_alloc, struct ice_sq_cd *cd)
410 {
411 	struct ice_aqc_add_update_free_vsi_resp *resp;
412 	struct ice_aqc_add_get_update_free_vsi *cmd;
413 	struct ice_aq_desc desc;
414 	enum ice_status status;
415 
416 	cmd = &desc.params.vsi_cmd;
417 	resp = &desc.params.add_update_free_vsi_res;
418 
419 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
420 
421 	cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
422 	if (keep_vsi_alloc)
423 		cmd->cmd_flags = CPU_TO_LE16(ICE_AQ_VSI_KEEP_ALLOC);
424 
425 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
426 	if (!status) {
427 		vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
428 		vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
429 	}
430 
431 	return status;
432 }
433 
434 /**
435  * ice_aq_update_vsi
436  * @hw: pointer to the HW struct
437  * @vsi_ctx: pointer to a VSI context struct
438  * @cd: pointer to command details structure or NULL
439  *
440  * Update VSI context in the hardware (0x0211)
441  */
442 enum ice_status
443 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
444 		  struct ice_sq_cd *cd)
445 {
446 	struct ice_aqc_add_update_free_vsi_resp *resp;
447 	struct ice_aqc_add_get_update_free_vsi *cmd;
448 	struct ice_aq_desc desc;
449 	enum ice_status status;
450 
451 	cmd = &desc.params.vsi_cmd;
452 	resp = &desc.params.add_update_free_vsi_res;
453 
454 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
455 
456 	cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
457 
458 	desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
459 
460 	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
461 				 sizeof(vsi_ctx->info), cd);
462 
463 	if (!status) {
464 		vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
465 		vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
466 	}
467 
468 	return status;
469 }
470 
471 /**
472  * ice_is_vsi_valid - check whether the VSI is valid or not
473  * @hw: pointer to the HW struct
474  * @vsi_handle: VSI handle
475  *
476  * check whether the VSI is valid or not
477  */
478 bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
479 {
480 	return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
481 }
482 
483 /**
484  * ice_get_hw_vsi_num - return the HW VSI number
485  * @hw: pointer to the HW struct
486  * @vsi_handle: VSI handle
487  *
488  * return the HW VSI number
489  * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
490  */
491 u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
492 {
493 	return hw->vsi_ctx[vsi_handle]->vsi_num;
494 }
495 
496 /**
497  * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
498  * @hw: pointer to the HW struct
499  * @vsi_handle: VSI handle
500  *
501  * return the VSI context entry for a given VSI handle
502  */
503 struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
504 {
505 	return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
506 }
507 
508 /**
509  * ice_save_vsi_ctx - save the VSI context for a given VSI handle
510  * @hw: pointer to the HW struct
511  * @vsi_handle: VSI handle
512  * @vsi: VSI context pointer
513  *
514  * save the VSI context entry for a given VSI handle
515  */
516 static void
517 ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi)
518 {
519 	hw->vsi_ctx[vsi_handle] = vsi;
520 }
521 
522 /**
523  * ice_clear_vsi_q_ctx - clear VSI queue contexts for all TCs
524  * @hw: pointer to the HW struct
525  * @vsi_handle: VSI handle
526  */
527 static void ice_clear_vsi_q_ctx(struct ice_hw *hw, u16 vsi_handle)
528 {
529 	struct ice_vsi_ctx *vsi;
530 	u8 i;
531 
532 	vsi = ice_get_vsi_ctx(hw, vsi_handle);
533 	if (!vsi)
534 		return;
535 	ice_for_each_traffic_class(i) {
536 		if (vsi->lan_q_ctx[i]) {
537 			ice_free(hw, vsi->lan_q_ctx[i]);
538 			vsi->lan_q_ctx[i] = NULL;
539 		}
540 	}
541 }
542 
543 /**
544  * ice_clear_vsi_ctx - clear the VSI context entry
545  * @hw: pointer to the HW struct
546  * @vsi_handle: VSI handle
547  *
548  * clear the VSI context entry
549  */
550 static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
551 {
552 	struct ice_vsi_ctx *vsi;
553 
554 	vsi = ice_get_vsi_ctx(hw, vsi_handle);
555 	if (vsi) {
556 		ice_clear_vsi_q_ctx(hw, vsi_handle);
557 		ice_free(hw, vsi);
558 		hw->vsi_ctx[vsi_handle] = NULL;
559 	}
560 }
561 
562 /**
563  * ice_clear_all_vsi_ctx - clear all the VSI context entries
564  * @hw: pointer to the HW struct
565  */
566 void ice_clear_all_vsi_ctx(struct ice_hw *hw)
567 {
568 	u16 i;
569 
570 	for (i = 0; i < ICE_MAX_VSI; i++)
571 		ice_clear_vsi_ctx(hw, i);
572 }
573 
574 /**
575  * ice_add_vsi - add VSI context to the hardware and VSI handle list
576  * @hw: pointer to the HW struct
577  * @vsi_handle: unique VSI handle provided by drivers
578  * @vsi_ctx: pointer to a VSI context struct
579  * @cd: pointer to command details structure or NULL
580  *
581  * Add a VSI context to the hardware also add it into the VSI handle list.
582  * If this function gets called after reset for existing VSIs then update
583  * with the new HW VSI number in the corresponding VSI handle list entry.
584  */
585 enum ice_status
586 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
587 	    struct ice_sq_cd *cd)
588 {
589 	struct ice_vsi_ctx *tmp_vsi_ctx;
590 	enum ice_status status;
591 
592 	if (vsi_handle >= ICE_MAX_VSI)
593 		return ICE_ERR_PARAM;
594 	status = ice_aq_add_vsi(hw, vsi_ctx, cd);
595 	if (status)
596 		return status;
597 	tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
598 	if (!tmp_vsi_ctx) {
599 		/* Create a new VSI context */
600 		tmp_vsi_ctx = (struct ice_vsi_ctx *)
601 			ice_malloc(hw, sizeof(*tmp_vsi_ctx));
602 		if (!tmp_vsi_ctx) {
603 			ice_aq_free_vsi(hw, vsi_ctx, false, cd);
604 			return ICE_ERR_NO_MEMORY;
605 		}
606 		*tmp_vsi_ctx = *vsi_ctx;
607 
608 		ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
609 	} else {
610 		/* update with new HW VSI num */
611 		tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
612 	}
613 
614 	return ICE_SUCCESS;
615 }
616 
617 /**
618  * ice_free_vsi- free VSI context from hardware and VSI handle list
619  * @hw: pointer to the HW struct
620  * @vsi_handle: unique VSI handle
621  * @vsi_ctx: pointer to a VSI context struct
622  * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
623  * @cd: pointer to command details structure or NULL
624  *
625  * Free VSI context info from hardware as well as from VSI handle list
626  */
627 enum ice_status
628 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
629 	     bool keep_vsi_alloc, struct ice_sq_cd *cd)
630 {
631 	enum ice_status status;
632 
633 	if (!ice_is_vsi_valid(hw, vsi_handle))
634 		return ICE_ERR_PARAM;
635 	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
636 	status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
637 	if (!status)
638 		ice_clear_vsi_ctx(hw, vsi_handle);
639 	return status;
640 }
641 
642 /**
643  * ice_update_vsi
644  * @hw: pointer to the HW struct
645  * @vsi_handle: unique VSI handle
646  * @vsi_ctx: pointer to a VSI context struct
647  * @cd: pointer to command details structure or NULL
648  *
649  * Update VSI context in the hardware
650  */
651 enum ice_status
652 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
653 	       struct ice_sq_cd *cd)
654 {
655 	if (!ice_is_vsi_valid(hw, vsi_handle))
656 		return ICE_ERR_PARAM;
657 	vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
658 	return ice_aq_update_vsi(hw, vsi_ctx, cd);
659 }
660 
661 /**
662  * ice_aq_get_vsi_params
663  * @hw: pointer to the HW struct
664  * @vsi_ctx: pointer to a VSI context struct
665  * @cd: pointer to command details structure or NULL
666  *
667  * Get VSI context info from hardware (0x0212)
668  */
669 enum ice_status
670 ice_aq_get_vsi_params(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
671 		      struct ice_sq_cd *cd)
672 {
673 	struct ice_aqc_add_get_update_free_vsi *cmd;
674 	struct ice_aqc_get_vsi_resp *resp;
675 	struct ice_aq_desc desc;
676 	enum ice_status status;
677 
678 	cmd = &desc.params.vsi_cmd;
679 	resp = &desc.params.get_vsi_resp;
680 
681 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_vsi_params);
682 
683 	cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
684 
685 	status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
686 				 sizeof(vsi_ctx->info), cd);
687 	if (!status) {
688 		vsi_ctx->vsi_num = LE16_TO_CPU(resp->vsi_num) &
689 					ICE_AQ_VSI_NUM_M;
690 		vsi_ctx->vf_num = resp->vf_id;
691 		vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
692 		vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
693 	}
694 
695 	return status;
696 }
697 
698 /**
699  * ice_aq_add_update_mir_rule - add/update a mirror rule
700  * @hw: pointer to the HW struct
701  * @rule_type: Rule Type
702  * @dest_vsi: VSI number to which packets will be mirrored
703  * @count: length of the list
704  * @mr_buf: buffer for list of mirrored VSI numbers
705  * @cd: pointer to command details structure or NULL
706  * @rule_id: Rule ID
707  *
708  * Add/Update Mirror Rule (0x260).
709  */
710 enum ice_status
711 ice_aq_add_update_mir_rule(struct ice_hw *hw, u16 rule_type, u16 dest_vsi,
712 			   u16 count, struct ice_mir_rule_buf *mr_buf,
713 			   struct ice_sq_cd *cd, u16 *rule_id)
714 {
715 	struct ice_aqc_add_update_mir_rule *cmd;
716 	struct ice_aq_desc desc;
717 	enum ice_status status;
718 	__le16 *mr_list = NULL;
719 	u16 buf_size = 0;
720 
721 	switch (rule_type) {
722 	case ICE_AQC_RULE_TYPE_VPORT_INGRESS:
723 	case ICE_AQC_RULE_TYPE_VPORT_EGRESS:
724 		/* Make sure count and mr_buf are set for these rule_types */
725 		if (!(count && mr_buf))
726 			return ICE_ERR_PARAM;
727 
728 		buf_size = count * sizeof(__le16);
729 		mr_list = (_FORCE_ __le16 *)ice_malloc(hw, buf_size);
730 		if (!mr_list)
731 			return ICE_ERR_NO_MEMORY;
732 		break;
733 	case ICE_AQC_RULE_TYPE_PPORT_INGRESS:
734 	case ICE_AQC_RULE_TYPE_PPORT_EGRESS:
735 		/* Make sure count and mr_buf are not set for these
736 		 * rule_types
737 		 */
738 		if (count || mr_buf)
739 			return ICE_ERR_PARAM;
740 		break;
741 	default:
742 		ice_debug(hw, ICE_DBG_SW, "Error due to unsupported rule_type %u\n", rule_type);
743 		return ICE_ERR_OUT_OF_RANGE;
744 	}
745 
746 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_update_mir_rule);
747 
748 	/* Pre-process 'mr_buf' items for add/update of virtual port
749 	 * ingress/egress mirroring (but not physical port ingress/egress
750 	 * mirroring)
751 	 */
752 	if (mr_buf) {
753 		int i;
754 
755 		for (i = 0; i < count; i++) {
756 			u16 id;
757 
758 			id = mr_buf[i].vsi_idx & ICE_AQC_RULE_MIRRORED_VSI_M;
759 
760 			/* Validate specified VSI number, make sure it is less
761 			 * than ICE_MAX_VSI, if not return with error.
762 			 */
763 			if (id >= ICE_MAX_VSI) {
764 				ice_debug(hw, ICE_DBG_SW, "Error VSI index (%u) out-of-range\n",
765 					  id);
766 				ice_free(hw, mr_list);
767 				return ICE_ERR_OUT_OF_RANGE;
768 			}
769 
770 			/* add VSI to mirror rule */
771 			if (mr_buf[i].add)
772 				mr_list[i] =
773 					CPU_TO_LE16(id | ICE_AQC_RULE_ACT_M);
774 			else /* remove VSI from mirror rule */
775 				mr_list[i] = CPU_TO_LE16(id);
776 		}
777 	}
778 
779 	cmd = &desc.params.add_update_rule;
780 	if ((*rule_id) != ICE_INVAL_MIRROR_RULE_ID)
781 		cmd->rule_id = CPU_TO_LE16(((*rule_id) & ICE_AQC_RULE_ID_M) |
782 					   ICE_AQC_RULE_ID_VALID_M);
783 	cmd->rule_type = CPU_TO_LE16(rule_type & ICE_AQC_RULE_TYPE_M);
784 	cmd->num_entries = CPU_TO_LE16(count);
785 	cmd->dest = CPU_TO_LE16(dest_vsi);
786 
787 	status = ice_aq_send_cmd(hw, &desc, mr_list, buf_size, cd);
788 	if (!status)
789 		*rule_id = LE16_TO_CPU(cmd->rule_id) & ICE_AQC_RULE_ID_M;
790 
791 	ice_free(hw, mr_list);
792 
793 	return status;
794 }
795 
796 /**
797  * ice_aq_delete_mir_rule - delete a mirror rule
798  * @hw: pointer to the HW struct
799  * @rule_id: Mirror rule ID (to be deleted)
800  * @keep_allocd: if set, the VSI stays part of the PF allocated res,
801  *		 otherwise it is returned to the shared pool
802  * @cd: pointer to command details structure or NULL
803  *
804  * Delete Mirror Rule (0x261).
805  */
806 enum ice_status
807 ice_aq_delete_mir_rule(struct ice_hw *hw, u16 rule_id, bool keep_allocd,
808 		       struct ice_sq_cd *cd)
809 {
810 	struct ice_aqc_delete_mir_rule *cmd;
811 	struct ice_aq_desc desc;
812 
813 	/* rule_id should be in the range 0...63 */
814 	if (rule_id >= ICE_MAX_NUM_MIRROR_RULES)
815 		return ICE_ERR_OUT_OF_RANGE;
816 
817 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_del_mir_rule);
818 
819 	cmd = &desc.params.del_rule;
820 	rule_id |= ICE_AQC_RULE_ID_VALID_M;
821 	cmd->rule_id = CPU_TO_LE16(rule_id);
822 
823 	if (keep_allocd)
824 		cmd->flags = CPU_TO_LE16(ICE_AQC_FLAG_KEEP_ALLOCD_M);
825 
826 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
827 }
828 
829 /**
830  * ice_aq_alloc_free_vsi_list
831  * @hw: pointer to the HW struct
832  * @vsi_list_id: VSI list ID returned or used for lookup
833  * @lkup_type: switch rule filter lookup type
834  * @opc: switch rules population command type - pass in the command opcode
835  *
836  * allocates or free a VSI list resource
837  */
838 static enum ice_status
839 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
840 			   enum ice_sw_lkup_type lkup_type,
841 			   enum ice_adminq_opc opc)
842 {
843 	struct ice_aqc_alloc_free_res_elem *sw_buf;
844 	struct ice_aqc_res_elem *vsi_ele;
845 	enum ice_status status;
846 	u16 buf_len;
847 
848 	buf_len = ice_struct_size(sw_buf, elem, 1);
849 	sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
850 	if (!sw_buf)
851 		return ICE_ERR_NO_MEMORY;
852 	sw_buf->num_elems = CPU_TO_LE16(1);
853 
854 	if (lkup_type == ICE_SW_LKUP_MAC ||
855 	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
856 	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
857 	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
858 	    lkup_type == ICE_SW_LKUP_PROMISC ||
859 	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
860 	    lkup_type == ICE_SW_LKUP_LAST) {
861 		sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
862 	} else if (lkup_type == ICE_SW_LKUP_VLAN) {
863 		sw_buf->res_type =
864 			CPU_TO_LE16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
865 	} else {
866 		status = ICE_ERR_PARAM;
867 		goto ice_aq_alloc_free_vsi_list_exit;
868 	}
869 
870 	if (opc == ice_aqc_opc_free_res)
871 		sw_buf->elem[0].e.sw_resp = CPU_TO_LE16(*vsi_list_id);
872 
873 	status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
874 	if (status)
875 		goto ice_aq_alloc_free_vsi_list_exit;
876 
877 	if (opc == ice_aqc_opc_alloc_res) {
878 		vsi_ele = &sw_buf->elem[0];
879 		*vsi_list_id = LE16_TO_CPU(vsi_ele->e.sw_resp);
880 	}
881 
882 ice_aq_alloc_free_vsi_list_exit:
883 	ice_free(hw, sw_buf);
884 	return status;
885 }
886 
887 /**
888  * ice_aq_set_storm_ctrl - Sets storm control configuration
889  * @hw: pointer to the HW struct
890  * @bcast_thresh: represents the upper threshold for broadcast storm control
891  * @mcast_thresh: represents the upper threshold for multicast storm control
892  * @ctl_bitmask: storm control knobs
893  *
894  * Sets the storm control configuration (0x0280)
895  */
896 enum ice_status
897 ice_aq_set_storm_ctrl(struct ice_hw *hw, u32 bcast_thresh, u32 mcast_thresh,
898 		      u32 ctl_bitmask)
899 {
900 	struct ice_aqc_storm_cfg *cmd;
901 	struct ice_aq_desc desc;
902 
903 	cmd = &desc.params.storm_conf;
904 
905 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_storm_cfg);
906 
907 	cmd->bcast_thresh_size = CPU_TO_LE32(bcast_thresh & ICE_AQ_THRESHOLD_M);
908 	cmd->mcast_thresh_size = CPU_TO_LE32(mcast_thresh & ICE_AQ_THRESHOLD_M);
909 	cmd->storm_ctrl_ctrl = CPU_TO_LE32(ctl_bitmask);
910 
911 	return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
912 }
913 
914 /**
915  * ice_aq_get_storm_ctrl - gets storm control configuration
916  * @hw: pointer to the HW struct
917  * @bcast_thresh: represents the upper threshold for broadcast storm control
918  * @mcast_thresh: represents the upper threshold for multicast storm control
919  * @ctl_bitmask: storm control knobs
920  *
921  * Gets the storm control configuration (0x0281)
922  */
923 enum ice_status
924 ice_aq_get_storm_ctrl(struct ice_hw *hw, u32 *bcast_thresh, u32 *mcast_thresh,
925 		      u32 *ctl_bitmask)
926 {
927 	enum ice_status status;
928 	struct ice_aq_desc desc;
929 
930 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_storm_cfg);
931 
932 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
933 	if (!status) {
934 		struct ice_aqc_storm_cfg *resp = &desc.params.storm_conf;
935 
936 		if (bcast_thresh)
937 			*bcast_thresh = LE32_TO_CPU(resp->bcast_thresh_size) &
938 				ICE_AQ_THRESHOLD_M;
939 		if (mcast_thresh)
940 			*mcast_thresh = LE32_TO_CPU(resp->mcast_thresh_size) &
941 				ICE_AQ_THRESHOLD_M;
942 		if (ctl_bitmask)
943 			*ctl_bitmask = LE32_TO_CPU(resp->storm_ctrl_ctrl);
944 	}
945 
946 	return status;
947 }
948 
949 /**
950  * ice_aq_sw_rules - add/update/remove switch rules
951  * @hw: pointer to the HW struct
952  * @rule_list: pointer to switch rule population list
953  * @rule_list_sz: total size of the rule list in bytes
954  * @num_rules: number of switch rules in the rule_list
955  * @opc: switch rules population command type - pass in the command opcode
956  * @cd: pointer to command details structure or NULL
957  *
958  * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
959  */
960 static enum ice_status
961 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
962 		u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
963 {
964 	struct ice_aq_desc desc;
965 	enum ice_status status;
966 
967 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
968 
969 	if (opc != ice_aqc_opc_add_sw_rules &&
970 	    opc != ice_aqc_opc_update_sw_rules &&
971 	    opc != ice_aqc_opc_remove_sw_rules)
972 		return ICE_ERR_PARAM;
973 
974 	ice_fill_dflt_direct_cmd_desc(&desc, opc);
975 
976 	desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
977 	desc.params.sw_rules.num_rules_fltr_entry_index =
978 		CPU_TO_LE16(num_rules);
979 	status = ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
980 	if (opc != ice_aqc_opc_add_sw_rules &&
981 	    hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT)
982 		status = ICE_ERR_DOES_NOT_EXIST;
983 
984 	return status;
985 }
986 
987 /* ice_init_port_info - Initialize port_info with switch configuration data
988  * @pi: pointer to port_info
989  * @vsi_port_num: VSI number or port number
990  * @type: Type of switch element (port or VSI)
991  * @swid: switch ID of the switch the element is attached to
992  * @pf_vf_num: PF or VF number
993  * @is_vf: true if the element is a VF, false otherwise
994  */
995 static void
996 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
997 		   u16 swid, u16 pf_vf_num, bool is_vf)
998 {
999 	switch (type) {
1000 	case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
1001 		pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
1002 		pi->sw_id = swid;
1003 		pi->pf_vf_num = pf_vf_num;
1004 		pi->is_vf = is_vf;
1005 		pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
1006 		pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
1007 		break;
1008 	default:
1009 		ice_debug(pi->hw, ICE_DBG_SW, "incorrect VSI/port type received\n");
1010 		break;
1011 	}
1012 }
1013 
1014 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
1015  * @hw: pointer to the hardware structure
1016  */
1017 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
1018 {
1019 	struct ice_aqc_get_sw_cfg_resp_elem *rbuf;
1020 	enum ice_status status;
1021 	u8 num_total_ports;
1022 	u16 req_desc = 0;
1023 	u16 num_elems;
1024 	u8 j = 0;
1025 	u16 i;
1026 
1027 	num_total_ports = 1;
1028 
1029 	rbuf = (struct ice_aqc_get_sw_cfg_resp_elem *)
1030 		ice_malloc(hw, ICE_SW_CFG_MAX_BUF_LEN);
1031 
1032 	if (!rbuf)
1033 		return ICE_ERR_NO_MEMORY;
1034 
1035 	/* Multiple calls to ice_aq_get_sw_cfg may be required
1036 	 * to get all the switch configuration information. The need
1037 	 * for additional calls is indicated by ice_aq_get_sw_cfg
1038 	 * writing a non-zero value in req_desc
1039 	 */
1040 	do {
1041 		struct ice_aqc_get_sw_cfg_resp_elem *ele;
1042 
1043 		status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
1044 					   &req_desc, &num_elems, NULL);
1045 
1046 		if (status)
1047 			break;
1048 
1049 		for (i = 0, ele = rbuf; i < num_elems; i++, ele++) {
1050 			u16 pf_vf_num, swid, vsi_port_num;
1051 			bool is_vf = false;
1052 			u8 res_type;
1053 
1054 			vsi_port_num = LE16_TO_CPU(ele->vsi_port_num) &
1055 				ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
1056 
1057 			pf_vf_num = LE16_TO_CPU(ele->pf_vf_num) &
1058 				ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
1059 
1060 			swid = LE16_TO_CPU(ele->swid);
1061 
1062 			if (LE16_TO_CPU(ele->pf_vf_num) &
1063 			    ICE_AQC_GET_SW_CONF_RESP_IS_VF)
1064 				is_vf = true;
1065 
1066 			res_type = (u8)(LE16_TO_CPU(ele->vsi_port_num) >>
1067 					ICE_AQC_GET_SW_CONF_RESP_TYPE_S);
1068 
1069 			switch (res_type) {
1070 			case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
1071 			case ICE_AQC_GET_SW_CONF_RESP_VIRT_PORT:
1072 				if (j == num_total_ports) {
1073 					ice_debug(hw, ICE_DBG_SW, "more ports than expected\n");
1074 					status = ICE_ERR_CFG;
1075 					goto out;
1076 				}
1077 				ice_init_port_info(hw->port_info,
1078 						   vsi_port_num, res_type, swid,
1079 						   pf_vf_num, is_vf);
1080 				j++;
1081 				break;
1082 			default:
1083 				break;
1084 			}
1085 		}
1086 	} while (req_desc && !status);
1087 
1088 out:
1089 	ice_free(hw, rbuf);
1090 	return status;
1091 }
1092 
1093 /**
1094  * ice_fill_sw_info - Helper function to populate lb_en and lan_en
1095  * @hw: pointer to the hardware structure
1096  * @fi: filter info structure to fill/update
1097  *
1098  * This helper function populates the lb_en and lan_en elements of the provided
1099  * ice_fltr_info struct using the switch's type and characteristics of the
1100  * switch rule being configured.
1101  */
1102 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
1103 {
1104 	fi->lb_en = false;
1105 	fi->lan_en = false;
1106 	if ((fi->flag & ICE_FLTR_TX) &&
1107 	    (fi->fltr_act == ICE_FWD_TO_VSI ||
1108 	     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1109 	     fi->fltr_act == ICE_FWD_TO_Q ||
1110 	     fi->fltr_act == ICE_FWD_TO_QGRP)) {
1111 		/* Setting LB for prune actions will result in replicated
1112 		 * packets to the internal switch that will be dropped.
1113 		 */
1114 		if (fi->lkup_type != ICE_SW_LKUP_VLAN)
1115 			fi->lb_en = true;
1116 
1117 		/* Set lan_en to TRUE if
1118 		 * 1. The switch is a VEB AND
1119 		 * 2
1120 		 * 2.1 The lookup is a directional lookup like ethertype,
1121 		 * promiscuous, ethertype-MAC, promiscuous-VLAN
1122 		 * and default-port OR
1123 		 * 2.2 The lookup is VLAN, OR
1124 		 * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
1125 		 * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
1126 		 *
1127 		 * OR
1128 		 *
1129 		 * The switch is a VEPA.
1130 		 *
1131 		 * In all other cases, the LAN enable has to be set to false.
1132 		 */
1133 		if (hw->evb_veb) {
1134 			if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1135 			    fi->lkup_type == ICE_SW_LKUP_PROMISC ||
1136 			    fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1137 			    fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1138 			    fi->lkup_type == ICE_SW_LKUP_DFLT ||
1139 			    fi->lkup_type == ICE_SW_LKUP_VLAN ||
1140 			    (fi->lkup_type == ICE_SW_LKUP_MAC &&
1141 			     !IS_UNICAST_ETHER_ADDR(fi->l_data.mac.mac_addr)) ||
1142 			    (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
1143 			     !IS_UNICAST_ETHER_ADDR(fi->l_data.mac.mac_addr)))
1144 				fi->lan_en = true;
1145 		} else {
1146 			fi->lan_en = true;
1147 		}
1148 	}
1149 }
1150 
1151 /**
1152  * ice_fill_sw_rule - Helper function to fill switch rule structure
1153  * @hw: pointer to the hardware structure
1154  * @f_info: entry containing packet forwarding information
1155  * @s_rule: switch rule structure to be filled in based on mac_entry
1156  * @opc: switch rules population command type - pass in the command opcode
1157  */
1158 static void
1159 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
1160 		 struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
1161 {
1162 	u16 vlan_id = ICE_MAX_VLAN_ID + 1;
1163 	u16 vlan_tpid = ICE_ETH_P_8021Q;
1164 	void *daddr = NULL;
1165 	u16 eth_hdr_sz;
1166 	u8 *eth_hdr;
1167 	u32 act = 0;
1168 	__be16 *off;
1169 	u8 q_rgn;
1170 
1171 	if (opc == ice_aqc_opc_remove_sw_rules) {
1172 		s_rule->pdata.lkup_tx_rx.act = 0;
1173 		s_rule->pdata.lkup_tx_rx.index =
1174 			CPU_TO_LE16(f_info->fltr_rule_id);
1175 		s_rule->pdata.lkup_tx_rx.hdr_len = 0;
1176 		return;
1177 	}
1178 
1179 	eth_hdr_sz = sizeof(dummy_eth_header);
1180 	eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
1181 
1182 	/* initialize the ether header with a dummy header */
1183 	ice_memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz, ICE_NONDMA_TO_NONDMA);
1184 	ice_fill_sw_info(hw, f_info);
1185 
1186 	switch (f_info->fltr_act) {
1187 	case ICE_FWD_TO_VSI:
1188 		act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
1189 			ICE_SINGLE_ACT_VSI_ID_M;
1190 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1191 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1192 				ICE_SINGLE_ACT_VALID_BIT;
1193 		break;
1194 	case ICE_FWD_TO_VSI_LIST:
1195 		act |= ICE_SINGLE_ACT_VSI_LIST;
1196 		act |= (f_info->fwd_id.vsi_list_id <<
1197 			ICE_SINGLE_ACT_VSI_LIST_ID_S) &
1198 			ICE_SINGLE_ACT_VSI_LIST_ID_M;
1199 		if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1200 			act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1201 				ICE_SINGLE_ACT_VALID_BIT;
1202 		break;
1203 	case ICE_FWD_TO_Q:
1204 		act |= ICE_SINGLE_ACT_TO_Q;
1205 		act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1206 			ICE_SINGLE_ACT_Q_INDEX_M;
1207 		break;
1208 	case ICE_DROP_PACKET:
1209 		act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
1210 			ICE_SINGLE_ACT_VALID_BIT;
1211 		break;
1212 	case ICE_FWD_TO_QGRP:
1213 		q_rgn = f_info->qgrp_size > 0 ?
1214 			(u8)ice_ilog2(f_info->qgrp_size) : 0;
1215 		act |= ICE_SINGLE_ACT_TO_Q;
1216 		act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1217 			ICE_SINGLE_ACT_Q_INDEX_M;
1218 		act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
1219 			ICE_SINGLE_ACT_Q_REGION_M;
1220 		break;
1221 	default:
1222 		return;
1223 	}
1224 
1225 	if (f_info->lb_en)
1226 		act |= ICE_SINGLE_ACT_LB_ENABLE;
1227 	if (f_info->lan_en)
1228 		act |= ICE_SINGLE_ACT_LAN_ENABLE;
1229 
1230 	switch (f_info->lkup_type) {
1231 	case ICE_SW_LKUP_MAC:
1232 		daddr = f_info->l_data.mac.mac_addr;
1233 		break;
1234 	case ICE_SW_LKUP_VLAN:
1235 		vlan_id = f_info->l_data.vlan.vlan_id;
1236 		if (f_info->l_data.vlan.tpid_valid)
1237 			vlan_tpid = f_info->l_data.vlan.tpid;
1238 		if (f_info->fltr_act == ICE_FWD_TO_VSI ||
1239 		    f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
1240 			act |= ICE_SINGLE_ACT_PRUNE;
1241 			act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
1242 		}
1243 		break;
1244 	case ICE_SW_LKUP_ETHERTYPE_MAC:
1245 		daddr = f_info->l_data.ethertype_mac.mac_addr;
1246 		/* fall-through */
1247 	case ICE_SW_LKUP_ETHERTYPE:
1248 		off = (_FORCE_ __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
1249 		*off = CPU_TO_BE16(f_info->l_data.ethertype_mac.ethertype);
1250 		break;
1251 	case ICE_SW_LKUP_MAC_VLAN:
1252 		daddr = f_info->l_data.mac_vlan.mac_addr;
1253 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
1254 		break;
1255 	case ICE_SW_LKUP_PROMISC_VLAN:
1256 		vlan_id = f_info->l_data.mac_vlan.vlan_id;
1257 		/* fall-through */
1258 	case ICE_SW_LKUP_PROMISC:
1259 		daddr = f_info->l_data.mac_vlan.mac_addr;
1260 		break;
1261 	default:
1262 		break;
1263 	}
1264 
1265 	s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
1266 		CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_RX) :
1267 		CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_TX);
1268 
1269 	/* Recipe set depending on lookup type */
1270 	s_rule->pdata.lkup_tx_rx.recipe_id = CPU_TO_LE16(f_info->lkup_type);
1271 	s_rule->pdata.lkup_tx_rx.src = CPU_TO_LE16(f_info->src);
1272 	s_rule->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1273 
1274 	if (daddr)
1275 		ice_memcpy(eth_hdr + ICE_ETH_DA_OFFSET, daddr, ETH_ALEN,
1276 			   ICE_NONDMA_TO_NONDMA);
1277 
1278 	if (!(vlan_id > ICE_MAX_VLAN_ID)) {
1279 		off = (_FORCE_ __be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
1280 		*off = CPU_TO_BE16(vlan_id);
1281 		off = (_FORCE_ __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
1282 		*off = CPU_TO_BE16(vlan_tpid);
1283 	}
1284 
1285 	/* Create the switch rule with the final dummy Ethernet header */
1286 	if (opc != ice_aqc_opc_update_sw_rules)
1287 		s_rule->pdata.lkup_tx_rx.hdr_len = CPU_TO_LE16(eth_hdr_sz);
1288 }
1289 
1290 /**
1291  * ice_add_marker_act
1292  * @hw: pointer to the hardware structure
1293  * @m_ent: the management entry for which sw marker needs to be added
1294  * @sw_marker: sw marker to tag the Rx descriptor with
1295  * @l_id: large action resource ID
1296  *
1297  * Create a large action to hold software marker and update the switch rule
1298  * entry pointed by m_ent with newly created large action
1299  */
1300 static enum ice_status
1301 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1302 		   u16 sw_marker, u16 l_id)
1303 {
1304 	struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
1305 	/* For software marker we need 3 large actions
1306 	 * 1. FWD action: FWD TO VSI or VSI LIST
1307 	 * 2. GENERIC VALUE action to hold the profile ID
1308 	 * 3. GENERIC VALUE action to hold the software marker ID
1309 	 */
1310 	const u16 num_lg_acts = 3;
1311 	enum ice_status status;
1312 	u16 lg_act_size;
1313 	u16 rules_size;
1314 	u32 act;
1315 	u16 id;
1316 
1317 	if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1318 		return ICE_ERR_PARAM;
1319 
1320 	/* Create two back-to-back switch rules and submit them to the HW using
1321 	 * one memory buffer:
1322 	 *    1. Large Action
1323 	 *    2. Look up Tx Rx
1324 	 */
1325 	lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
1326 	rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1327 	lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, rules_size);
1328 	if (!lg_act)
1329 		return ICE_ERR_NO_MEMORY;
1330 
1331 	rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
1332 
1333 	/* Fill in the first switch rule i.e. large action */
1334 	lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1335 	lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1336 	lg_act->pdata.lg_act.size = CPU_TO_LE16(num_lg_acts);
1337 
1338 	/* First action VSI forwarding or VSI list forwarding depending on how
1339 	 * many VSIs
1340 	 */
1341 	id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
1342 		m_ent->fltr_info.fwd_id.hw_vsi_id;
1343 
1344 	act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1345 	act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) & ICE_LG_ACT_VSI_LIST_ID_M;
1346 	if (m_ent->vsi_count > 1)
1347 		act |= ICE_LG_ACT_VSI_LIST;
1348 	lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1349 
1350 	/* Second action descriptor type */
1351 	act = ICE_LG_ACT_GENERIC;
1352 
1353 	act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
1354 	lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1355 
1356 	act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
1357 	       ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
1358 
1359 	/* Third action Marker value */
1360 	act |= ICE_LG_ACT_GENERIC;
1361 	act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
1362 		ICE_LG_ACT_GENERIC_VALUE_M;
1363 
1364 	lg_act->pdata.lg_act.act[2] = CPU_TO_LE32(act);
1365 
1366 	/* call the fill switch rule to fill the lookup Tx Rx structure */
1367 	ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1368 			 ice_aqc_opc_update_sw_rules);
1369 
1370 	/* Update the action to point to the large action ID */
1371 	rx_tx->pdata.lkup_tx_rx.act =
1372 		CPU_TO_LE32(ICE_SINGLE_ACT_PTR |
1373 			    ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
1374 			     ICE_SINGLE_ACT_PTR_VAL_M));
1375 
1376 	/* Use the filter rule ID of the previously created rule with single
1377 	 * act. Once the update happens, hardware will treat this as large
1378 	 * action
1379 	 */
1380 	rx_tx->pdata.lkup_tx_rx.index =
1381 		CPU_TO_LE16(m_ent->fltr_info.fltr_rule_id);
1382 
1383 	status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1384 				 ice_aqc_opc_update_sw_rules, NULL);
1385 	if (!status) {
1386 		m_ent->lg_act_idx = l_id;
1387 		m_ent->sw_marker_id = sw_marker;
1388 	}
1389 
1390 	ice_free(hw, lg_act);
1391 	return status;
1392 }
1393 
1394 /**
1395  * ice_add_counter_act - add/update filter rule with counter action
1396  * @hw: pointer to the hardware structure
1397  * @m_ent: the management entry for which counter needs to be added
1398  * @counter_id: VLAN counter ID returned as part of allocate resource
1399  * @l_id: large action resource ID
1400  */
1401 static enum ice_status
1402 ice_add_counter_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1403 		    u16 counter_id, u16 l_id)
1404 {
1405 	struct ice_aqc_sw_rules_elem *lg_act;
1406 	struct ice_aqc_sw_rules_elem *rx_tx;
1407 	enum ice_status status;
1408 	/* 2 actions will be added while adding a large action counter */
1409 	const int num_acts = 2;
1410 	u16 lg_act_size;
1411 	u16 rules_size;
1412 	u16 f_rule_id;
1413 	u32 act;
1414 	u16 id;
1415 
1416 	if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1417 		return ICE_ERR_PARAM;
1418 
1419 	/* Create two back-to-back switch rules and submit them to the HW using
1420 	 * one memory buffer:
1421 	 * 1. Large Action
1422 	 * 2. Look up Tx Rx
1423 	 */
1424 	lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_acts);
1425 	rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1426 	lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, rules_size);
1427 	if (!lg_act)
1428 		return ICE_ERR_NO_MEMORY;
1429 
1430 	rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
1431 
1432 	/* Fill in the first switch rule i.e. large action */
1433 	lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1434 	lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1435 	lg_act->pdata.lg_act.size = CPU_TO_LE16(num_acts);
1436 
1437 	/* First action VSI forwarding or VSI list forwarding depending on how
1438 	 * many VSIs
1439 	 */
1440 	id = (m_ent->vsi_count > 1) ?  m_ent->fltr_info.fwd_id.vsi_list_id :
1441 		m_ent->fltr_info.fwd_id.hw_vsi_id;
1442 
1443 	act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1444 	act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
1445 		ICE_LG_ACT_VSI_LIST_ID_M;
1446 	if (m_ent->vsi_count > 1)
1447 		act |= ICE_LG_ACT_VSI_LIST;
1448 	lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1449 
1450 	/* Second action counter ID */
1451 	act = ICE_LG_ACT_STAT_COUNT;
1452 	act |= (counter_id << ICE_LG_ACT_STAT_COUNT_S) &
1453 		ICE_LG_ACT_STAT_COUNT_M;
1454 	lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1455 
1456 	/* call the fill switch rule to fill the lookup Tx Rx structure */
1457 	ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1458 			 ice_aqc_opc_update_sw_rules);
1459 
1460 	act = ICE_SINGLE_ACT_PTR;
1461 	act |= (l_id << ICE_SINGLE_ACT_PTR_VAL_S) & ICE_SINGLE_ACT_PTR_VAL_M;
1462 	rx_tx->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1463 
1464 	/* Use the filter rule ID of the previously created rule with single
1465 	 * act. Once the update happens, hardware will treat this as large
1466 	 * action
1467 	 */
1468 	f_rule_id = m_ent->fltr_info.fltr_rule_id;
1469 	rx_tx->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_rule_id);
1470 
1471 	status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1472 				 ice_aqc_opc_update_sw_rules, NULL);
1473 	if (!status) {
1474 		m_ent->lg_act_idx = l_id;
1475 		m_ent->counter_index = counter_id;
1476 	}
1477 
1478 	ice_free(hw, lg_act);
1479 	return status;
1480 }
1481 
1482 /**
1483  * ice_create_vsi_list_map
1484  * @hw: pointer to the hardware structure
1485  * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
1486  * @num_vsi: number of VSI handles in the array
1487  * @vsi_list_id: VSI list ID generated as part of allocate resource
1488  *
1489  * Helper function to create a new entry of VSI list ID to VSI mapping
1490  * using the given VSI list ID
1491  */
1492 static struct ice_vsi_list_map_info *
1493 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1494 			u16 vsi_list_id)
1495 {
1496 	struct ice_switch_info *sw = hw->switch_info;
1497 	struct ice_vsi_list_map_info *v_map;
1498 	int i;
1499 
1500 	v_map = (struct ice_vsi_list_map_info *)ice_malloc(hw, sizeof(*v_map));
1501 	if (!v_map)
1502 		return NULL;
1503 
1504 	v_map->vsi_list_id = vsi_list_id;
1505 	v_map->ref_cnt = 1;
1506 	for (i = 0; i < num_vsi; i++)
1507 		ice_set_bit(vsi_handle_arr[i], v_map->vsi_map);
1508 
1509 	LIST_ADD(&v_map->list_entry, &sw->vsi_list_map_head);
1510 	return v_map;
1511 }
1512 
1513 /**
1514  * ice_update_vsi_list_rule
1515  * @hw: pointer to the hardware structure
1516  * @vsi_handle_arr: array of VSI handles to form a VSI list
1517  * @num_vsi: number of VSI handles in the array
1518  * @vsi_list_id: VSI list ID generated as part of allocate resource
1519  * @remove: Boolean value to indicate if this is a remove action
1520  * @opc: switch rules population command type - pass in the command opcode
1521  * @lkup_type: lookup type of the filter
1522  *
1523  * Call AQ command to add a new switch rule or update existing switch rule
1524  * using the given VSI list ID
1525  */
1526 static enum ice_status
1527 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1528 			 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
1529 			 enum ice_sw_lkup_type lkup_type)
1530 {
1531 	struct ice_aqc_sw_rules_elem *s_rule;
1532 	enum ice_status status;
1533 	u16 s_rule_size;
1534 	u16 rule_type;
1535 	int i;
1536 
1537 	if (!num_vsi)
1538 		return ICE_ERR_PARAM;
1539 
1540 	if (lkup_type == ICE_SW_LKUP_MAC ||
1541 	    lkup_type == ICE_SW_LKUP_MAC_VLAN ||
1542 	    lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1543 	    lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1544 	    lkup_type == ICE_SW_LKUP_PROMISC ||
1545 	    lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1546 	    lkup_type == ICE_SW_LKUP_LAST)
1547 		rule_type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
1548 			ICE_AQC_SW_RULES_T_VSI_LIST_SET;
1549 	else if (lkup_type == ICE_SW_LKUP_VLAN)
1550 		rule_type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
1551 			ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
1552 	else
1553 		return ICE_ERR_PARAM;
1554 
1555 	s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
1556 	s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
1557 	if (!s_rule)
1558 		return ICE_ERR_NO_MEMORY;
1559 	for (i = 0; i < num_vsi; i++) {
1560 		if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
1561 			status = ICE_ERR_PARAM;
1562 			goto exit;
1563 		}
1564 		/* AQ call requires hw_vsi_id(s) */
1565 		s_rule->pdata.vsi_list.vsi[i] =
1566 			CPU_TO_LE16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
1567 	}
1568 
1569 	s_rule->type = CPU_TO_LE16(rule_type);
1570 	s_rule->pdata.vsi_list.number_vsi = CPU_TO_LE16(num_vsi);
1571 	s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
1572 
1573 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
1574 
1575 exit:
1576 	ice_free(hw, s_rule);
1577 	return status;
1578 }
1579 
1580 /**
1581  * ice_create_vsi_list_rule - Creates and populates a VSI list rule
1582  * @hw: pointer to the HW struct
1583  * @vsi_handle_arr: array of VSI handles to form a VSI list
1584  * @num_vsi: number of VSI handles in the array
1585  * @vsi_list_id: stores the ID of the VSI list to be created
1586  * @lkup_type: switch rule filter's lookup type
1587  */
1588 static enum ice_status
1589 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1590 			 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1591 {
1592 	enum ice_status status;
1593 
1594 	status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1595 					    ice_aqc_opc_alloc_res);
1596 	if (status)
1597 		return status;
1598 
1599 	/* Update the newly created VSI list to include the specified VSIs */
1600 	return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1601 					*vsi_list_id, false,
1602 					ice_aqc_opc_add_sw_rules, lkup_type);
1603 }
1604 
1605 /**
1606  * ice_create_pkt_fwd_rule
1607  * @hw: pointer to the hardware structure
1608  * @recp_list: corresponding filter management list
1609  * @f_entry: entry containing packet forwarding information
1610  *
1611  * Create switch rule with given filter information and add an entry
1612  * to the corresponding filter management list to track this switch rule
1613  * and VSI mapping
1614  */
1615 static enum ice_status
1616 ice_create_pkt_fwd_rule(struct ice_hw *hw, struct ice_sw_recipe *recp_list,
1617 			struct ice_fltr_list_entry *f_entry)
1618 {
1619 	struct ice_fltr_mgmt_list_entry *fm_entry;
1620 	struct ice_aqc_sw_rules_elem *s_rule;
1621 	enum ice_status status;
1622 
1623 	s_rule = (struct ice_aqc_sw_rules_elem *)
1624 		ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1625 	if (!s_rule)
1626 		return ICE_ERR_NO_MEMORY;
1627 	fm_entry = (struct ice_fltr_mgmt_list_entry *)
1628 		   ice_malloc(hw, sizeof(*fm_entry));
1629 	if (!fm_entry) {
1630 		status = ICE_ERR_NO_MEMORY;
1631 		goto ice_create_pkt_fwd_rule_exit;
1632 	}
1633 
1634 	fm_entry->fltr_info = f_entry->fltr_info;
1635 
1636 	/* Initialize all the fields for the management entry */
1637 	fm_entry->vsi_count = 1;
1638 	fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1639 	fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1640 	fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1641 
1642 	ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1643 			 ice_aqc_opc_add_sw_rules);
1644 
1645 	status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1646 				 ice_aqc_opc_add_sw_rules, NULL);
1647 	if (status) {
1648 		ice_free(hw, fm_entry);
1649 		goto ice_create_pkt_fwd_rule_exit;
1650 	}
1651 
1652 	f_entry->fltr_info.fltr_rule_id =
1653 		LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1654 	fm_entry->fltr_info.fltr_rule_id =
1655 		LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1656 
1657 	/* The book keeping entries will get removed when base driver
1658 	 * calls remove filter AQ command
1659 	 */
1660 	LIST_ADD(&fm_entry->list_entry, &recp_list->filt_rules);
1661 
1662 ice_create_pkt_fwd_rule_exit:
1663 	ice_free(hw, s_rule);
1664 	return status;
1665 }
1666 
1667 /**
1668  * ice_update_pkt_fwd_rule
1669  * @hw: pointer to the hardware structure
1670  * @f_info: filter information for switch rule
1671  *
1672  * Call AQ command to update a previously created switch rule with a
1673  * VSI list ID
1674  */
1675 static enum ice_status
1676 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1677 {
1678 	struct ice_aqc_sw_rules_elem *s_rule;
1679 	enum ice_status status;
1680 
1681 	s_rule = (struct ice_aqc_sw_rules_elem *)
1682 		ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1683 	if (!s_rule)
1684 		return ICE_ERR_NO_MEMORY;
1685 
1686 	ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
1687 
1688 	s_rule->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_info->fltr_rule_id);
1689 
1690 	/* Update switch rule with new rule set to forward VSI list */
1691 	status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1692 				 ice_aqc_opc_update_sw_rules, NULL);
1693 
1694 	ice_free(hw, s_rule);
1695 	return status;
1696 }
1697 
1698 /**
1699  * ice_update_sw_rule_bridge_mode
1700  * @hw: pointer to the HW struct
1701  *
1702  * Updates unicast switch filter rules based on VEB/VEPA mode
1703  */
1704 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
1705 {
1706 	struct ice_switch_info *sw = hw->switch_info;
1707 	struct ice_fltr_mgmt_list_entry *fm_entry;
1708 	enum ice_status status = ICE_SUCCESS;
1709 	struct LIST_HEAD_TYPE *rule_head;
1710 	struct ice_lock *rule_lock; /* Lock to protect filter rule list */
1711 
1712 	rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1713 	rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1714 
1715 	ice_acquire_lock(rule_lock);
1716 	LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry,
1717 			    list_entry) {
1718 		struct ice_fltr_info *fi = &fm_entry->fltr_info;
1719 		u8 *addr = fi->l_data.mac.mac_addr;
1720 
1721 		/* Update unicast Tx rules to reflect the selected
1722 		 * VEB/VEPA mode
1723 		 */
1724 		if ((fi->flag & ICE_FLTR_TX) && IS_UNICAST_ETHER_ADDR(addr) &&
1725 		    (fi->fltr_act == ICE_FWD_TO_VSI ||
1726 		     fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1727 		     fi->fltr_act == ICE_FWD_TO_Q ||
1728 		     fi->fltr_act == ICE_FWD_TO_QGRP)) {
1729 			status = ice_update_pkt_fwd_rule(hw, fi);
1730 			if (status)
1731 				break;
1732 		}
1733 	}
1734 
1735 	ice_release_lock(rule_lock);
1736 
1737 	return status;
1738 }
1739 
1740 /**
1741  * ice_add_update_vsi_list
1742  * @hw: pointer to the hardware structure
1743  * @m_entry: pointer to current filter management list entry
1744  * @cur_fltr: filter information from the book keeping entry
1745  * @new_fltr: filter information with the new VSI to be added
1746  *
1747  * Call AQ command to add or update previously created VSI list with new VSI.
1748  *
1749  * Helper function to do book keeping associated with adding filter information
1750  * The algorithm to do the book keeping is described below :
1751  * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
1752  *	if only one VSI has been added till now
1753  *		Allocate a new VSI list and add two VSIs
1754  *		to this list using switch rule command
1755  *		Update the previously created switch rule with the
1756  *		newly created VSI list ID
1757  *	if a VSI list was previously created
1758  *		Add the new VSI to the previously created VSI list set
1759  *		using the update switch rule command
1760  */
1761 static enum ice_status
1762 ice_add_update_vsi_list(struct ice_hw *hw,
1763 			struct ice_fltr_mgmt_list_entry *m_entry,
1764 			struct ice_fltr_info *cur_fltr,
1765 			struct ice_fltr_info *new_fltr)
1766 {
1767 	enum ice_status status = ICE_SUCCESS;
1768 	u16 vsi_list_id = 0;
1769 
1770 	if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
1771 	     cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
1772 		return ICE_ERR_NOT_IMPL;
1773 
1774 	if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
1775 	     new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
1776 	    (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
1777 	     cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
1778 		return ICE_ERR_NOT_IMPL;
1779 
1780 	if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
1781 		/* Only one entry existed in the mapping and it was not already
1782 		 * a part of a VSI list. So, create a VSI list with the old and
1783 		 * new VSIs.
1784 		 */
1785 		struct ice_fltr_info tmp_fltr;
1786 		u16 vsi_handle_arr[2];
1787 
1788 		/* A rule already exists with the new VSI being added */
1789 		if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
1790 			return ICE_ERR_ALREADY_EXISTS;
1791 
1792 		vsi_handle_arr[0] = cur_fltr->vsi_handle;
1793 		vsi_handle_arr[1] = new_fltr->vsi_handle;
1794 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1795 						  &vsi_list_id,
1796 						  new_fltr->lkup_type);
1797 		if (status)
1798 			return status;
1799 
1800 		tmp_fltr = *new_fltr;
1801 		tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
1802 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1803 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1804 		/* Update the previous switch rule of "MAC forward to VSI" to
1805 		 * "MAC fwd to VSI list"
1806 		 */
1807 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1808 		if (status)
1809 			return status;
1810 
1811 		cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1812 		cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1813 		m_entry->vsi_list_info =
1814 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1815 						vsi_list_id);
1816 
1817 		if (!m_entry->vsi_list_info)
1818 			return ICE_ERR_NO_MEMORY;
1819 
1820 		/* If this entry was large action then the large action needs
1821 		 * to be updated to point to FWD to VSI list
1822 		 */
1823 		if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1824 			status =
1825 			    ice_add_marker_act(hw, m_entry,
1826 					       m_entry->sw_marker_id,
1827 					       m_entry->lg_act_idx);
1828 	} else {
1829 		u16 vsi_handle = new_fltr->vsi_handle;
1830 		enum ice_adminq_opc opcode;
1831 
1832 		if (!m_entry->vsi_list_info)
1833 			return ICE_ERR_CFG;
1834 
1835 		/* A rule already exists with the new VSI being added */
1836 		if (ice_is_bit_set(m_entry->vsi_list_info->vsi_map, vsi_handle))
1837 			return ICE_SUCCESS;
1838 
1839 		/* Update the previously created VSI list set with
1840 		 * the new VSI ID passed in
1841 		 */
1842 		vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1843 		opcode = ice_aqc_opc_update_sw_rules;
1844 
1845 		status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
1846 						  vsi_list_id, false, opcode,
1847 						  new_fltr->lkup_type);
1848 		/* update VSI list mapping info with new VSI ID */
1849 		if (!status)
1850 			ice_set_bit(vsi_handle,
1851 				    m_entry->vsi_list_info->vsi_map);
1852 	}
1853 	if (!status)
1854 		m_entry->vsi_count++;
1855 	return status;
1856 }
1857 
1858 /**
1859  * ice_find_rule_entry - Search a rule entry
1860  * @list_head: head of rule list
1861  * @f_info: rule information
1862  *
1863  * Helper function to search for a given rule entry
1864  * Returns pointer to entry storing the rule if found
1865  */
1866 static struct ice_fltr_mgmt_list_entry *
1867 ice_find_rule_entry(struct LIST_HEAD_TYPE *list_head,
1868 		    struct ice_fltr_info *f_info)
1869 {
1870 	struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
1871 
1872 	LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
1873 			    list_entry) {
1874 		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
1875 			    sizeof(f_info->l_data)) &&
1876 		    f_info->flag == list_itr->fltr_info.flag) {
1877 			ret = list_itr;
1878 			break;
1879 		}
1880 	}
1881 	return ret;
1882 }
1883 
1884 /**
1885  * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
1886  * @recp_list: VSI lists needs to be searched
1887  * @vsi_handle: VSI handle to be found in VSI list
1888  * @vsi_list_id: VSI list ID found containing vsi_handle
1889  *
1890  * Helper function to search a VSI list with single entry containing given VSI
1891  * handle element. This can be extended further to search VSI list with more
1892  * than 1 vsi_count. Returns pointer to VSI list entry if found.
1893  */
1894 static struct ice_vsi_list_map_info *
1895 ice_find_vsi_list_entry(struct ice_sw_recipe *recp_list, u16 vsi_handle,
1896 			u16 *vsi_list_id)
1897 {
1898 	struct ice_vsi_list_map_info *map_info = NULL;
1899 	struct LIST_HEAD_TYPE *list_head;
1900 
1901 	list_head = &recp_list->filt_rules;
1902 	if (recp_list->adv_rule) {
1903 		struct ice_adv_fltr_mgmt_list_entry *list_itr;
1904 
1905 		LIST_FOR_EACH_ENTRY(list_itr, list_head,
1906 				    ice_adv_fltr_mgmt_list_entry,
1907 				    list_entry) {
1908 			if (list_itr->vsi_list_info) {
1909 				map_info = list_itr->vsi_list_info;
1910 				if (ice_is_bit_set(map_info->vsi_map,
1911 						   vsi_handle)) {
1912 					*vsi_list_id = map_info->vsi_list_id;
1913 					return map_info;
1914 				}
1915 			}
1916 		}
1917 	} else {
1918 		struct ice_fltr_mgmt_list_entry *list_itr;
1919 
1920 		LIST_FOR_EACH_ENTRY(list_itr, list_head,
1921 				    ice_fltr_mgmt_list_entry,
1922 				    list_entry) {
1923 			if (list_itr->vsi_count == 1 &&
1924 			    list_itr->vsi_list_info) {
1925 				map_info = list_itr->vsi_list_info;
1926 				if (ice_is_bit_set(map_info->vsi_map,
1927 						   vsi_handle)) {
1928 					*vsi_list_id = map_info->vsi_list_id;
1929 					return map_info;
1930 				}
1931 			}
1932 		}
1933 	}
1934 	return NULL;
1935 }
1936 
1937 /**
1938  * ice_add_rule_internal - add rule for a given lookup type
1939  * @hw: pointer to the hardware structure
1940  * @recp_list: recipe list for which rule has to be added
1941  * @lport: logic port number on which function add rule
1942  * @f_entry: structure containing MAC forwarding information
1943  *
1944  * Adds or updates the rule lists for a given recipe
1945  */
1946 static enum ice_status
1947 ice_add_rule_internal(struct ice_hw *hw, struct ice_sw_recipe *recp_list,
1948 		      u8 lport, struct ice_fltr_list_entry *f_entry)
1949 {
1950 	struct ice_fltr_info *new_fltr, *cur_fltr;
1951 	struct ice_fltr_mgmt_list_entry *m_entry;
1952 	struct ice_lock *rule_lock; /* Lock to protect filter rule list */
1953 	enum ice_status status = ICE_SUCCESS;
1954 
1955 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1956 		return ICE_ERR_PARAM;
1957 
1958 	/* Load the hw_vsi_id only if the fwd action is fwd to VSI */
1959 	if (f_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI)
1960 		f_entry->fltr_info.fwd_id.hw_vsi_id =
1961 			ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1962 
1963 	rule_lock = &recp_list->filt_rule_lock;
1964 
1965 	ice_acquire_lock(rule_lock);
1966 	new_fltr = &f_entry->fltr_info;
1967 	if (new_fltr->flag & ICE_FLTR_RX)
1968 		new_fltr->src = lport;
1969 	else if (new_fltr->flag & ICE_FLTR_TX)
1970 		new_fltr->src =
1971 			ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1972 
1973 	m_entry = ice_find_rule_entry(&recp_list->filt_rules, new_fltr);
1974 	if (!m_entry) {
1975 		status = ice_create_pkt_fwd_rule(hw, recp_list, f_entry);
1976 		goto exit_add_rule_internal;
1977 	}
1978 
1979 	cur_fltr = &m_entry->fltr_info;
1980 	status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
1981 
1982 exit_add_rule_internal:
1983 	ice_release_lock(rule_lock);
1984 	return status;
1985 }
1986 
1987 /**
1988  * ice_remove_vsi_list_rule
1989  * @hw: pointer to the hardware structure
1990  * @vsi_list_id: VSI list ID generated as part of allocate resource
1991  * @lkup_type: switch rule filter lookup type
1992  *
1993  * The VSI list should be emptied before this function is called to remove the
1994  * VSI list.
1995  */
1996 static enum ice_status
1997 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1998 			 enum ice_sw_lkup_type lkup_type)
1999 {
2000 	/* Free the vsi_list resource that we allocated. It is assumed that the
2001 	 * list is empty at this point.
2002 	 */
2003 	return ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
2004 					    ice_aqc_opc_free_res);
2005 }
2006 
2007 /**
2008  * ice_rem_update_vsi_list
2009  * @hw: pointer to the hardware structure
2010  * @vsi_handle: VSI handle of the VSI to remove
2011  * @fm_list: filter management entry for which the VSI list management needs to
2012  *	     be done
2013  */
2014 static enum ice_status
2015 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
2016 			struct ice_fltr_mgmt_list_entry *fm_list)
2017 {
2018 	enum ice_sw_lkup_type lkup_type;
2019 	enum ice_status status = ICE_SUCCESS;
2020 	u16 vsi_list_id;
2021 
2022 	if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
2023 	    fm_list->vsi_count == 0)
2024 		return ICE_ERR_PARAM;
2025 
2026 	/* A rule with the VSI being removed does not exist */
2027 	if (!ice_is_bit_set(fm_list->vsi_list_info->vsi_map, vsi_handle))
2028 		return ICE_ERR_DOES_NOT_EXIST;
2029 
2030 	lkup_type = fm_list->fltr_info.lkup_type;
2031 	vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
2032 	status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
2033 					  ice_aqc_opc_update_sw_rules,
2034 					  lkup_type);
2035 	if (status)
2036 		return status;
2037 
2038 	fm_list->vsi_count--;
2039 	ice_clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
2040 
2041 	if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
2042 		struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
2043 		struct ice_vsi_list_map_info *vsi_list_info =
2044 			fm_list->vsi_list_info;
2045 		u16 rem_vsi_handle;
2046 
2047 		rem_vsi_handle = ice_find_first_bit(vsi_list_info->vsi_map,
2048 						    ICE_MAX_VSI);
2049 		if (!ice_is_vsi_valid(hw, rem_vsi_handle))
2050 			return ICE_ERR_OUT_OF_RANGE;
2051 
2052 		/* Make sure VSI list is empty before removing it below */
2053 		status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
2054 						  vsi_list_id, true,
2055 						  ice_aqc_opc_update_sw_rules,
2056 						  lkup_type);
2057 		if (status)
2058 			return status;
2059 
2060 		tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
2061 		tmp_fltr_info.fwd_id.hw_vsi_id =
2062 			ice_get_hw_vsi_num(hw, rem_vsi_handle);
2063 		tmp_fltr_info.vsi_handle = rem_vsi_handle;
2064 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
2065 		if (status) {
2066 			ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
2067 				  tmp_fltr_info.fwd_id.hw_vsi_id, status);
2068 			return status;
2069 		}
2070 
2071 		fm_list->fltr_info = tmp_fltr_info;
2072 	}
2073 
2074 	if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
2075 	    (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
2076 		struct ice_vsi_list_map_info *vsi_list_info =
2077 			fm_list->vsi_list_info;
2078 
2079 		/* Remove the VSI list since it is no longer used */
2080 		status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
2081 		if (status) {
2082 			ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
2083 				  vsi_list_id, status);
2084 			return status;
2085 		}
2086 
2087 		LIST_DEL(&vsi_list_info->list_entry);
2088 		ice_free(hw, vsi_list_info);
2089 		fm_list->vsi_list_info = NULL;
2090 	}
2091 
2092 	return status;
2093 }
2094 
2095 /**
2096  * ice_remove_rule_internal - Remove a filter rule of a given type
2097  *
2098  * @hw: pointer to the hardware structure
2099  * @recp_list: recipe list for which the rule needs to removed
2100  * @f_entry: rule entry containing filter information
2101  */
2102 static enum ice_status
2103 ice_remove_rule_internal(struct ice_hw *hw, struct ice_sw_recipe *recp_list,
2104 			 struct ice_fltr_list_entry *f_entry)
2105 {
2106 	struct ice_fltr_mgmt_list_entry *list_elem;
2107 	struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2108 	enum ice_status status = ICE_SUCCESS;
2109 	bool remove_rule = false;
2110 	u16 vsi_handle;
2111 
2112 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2113 		return ICE_ERR_PARAM;
2114 	f_entry->fltr_info.fwd_id.hw_vsi_id =
2115 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2116 
2117 	rule_lock = &recp_list->filt_rule_lock;
2118 	ice_acquire_lock(rule_lock);
2119 	list_elem = ice_find_rule_entry(&recp_list->filt_rules,
2120 					&f_entry->fltr_info);
2121 	if (!list_elem) {
2122 		status = ICE_ERR_DOES_NOT_EXIST;
2123 		goto exit;
2124 	}
2125 
2126 	if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
2127 		remove_rule = true;
2128 	} else if (!list_elem->vsi_list_info) {
2129 		status = ICE_ERR_DOES_NOT_EXIST;
2130 		goto exit;
2131 	} else if (list_elem->vsi_list_info->ref_cnt > 1) {
2132 		/* a ref_cnt > 1 indicates that the vsi_list is being
2133 		 * shared by multiple rules. Decrement the ref_cnt and
2134 		 * remove this rule, but do not modify the list, as it
2135 		 * is in-use by other rules.
2136 		 */
2137 		list_elem->vsi_list_info->ref_cnt--;
2138 		remove_rule = true;
2139 	} else {
2140 		/* a ref_cnt of 1 indicates the vsi_list is only used
2141 		 * by one rule. However, the original removal request is only
2142 		 * for a single VSI. Update the vsi_list first, and only
2143 		 * remove the rule if there are no further VSIs in this list.
2144 		 */
2145 		vsi_handle = f_entry->fltr_info.vsi_handle;
2146 		status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
2147 		if (status)
2148 			goto exit;
2149 		/* if VSI count goes to zero after updating the VSI list */
2150 		if (list_elem->vsi_count == 0)
2151 			remove_rule = true;
2152 	}
2153 
2154 	if (remove_rule) {
2155 		/* Remove the lookup rule */
2156 		struct ice_aqc_sw_rules_elem *s_rule;
2157 
2158 		s_rule = (struct ice_aqc_sw_rules_elem *)
2159 			ice_malloc(hw, ICE_SW_RULE_RX_TX_NO_HDR_SIZE);
2160 		if (!s_rule) {
2161 			status = ICE_ERR_NO_MEMORY;
2162 			goto exit;
2163 		}
2164 
2165 		ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
2166 				 ice_aqc_opc_remove_sw_rules);
2167 
2168 		status = ice_aq_sw_rules(hw, s_rule,
2169 					 ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
2170 					 ice_aqc_opc_remove_sw_rules, NULL);
2171 
2172 		/* Remove a book keeping from the list */
2173 		ice_free(hw, s_rule);
2174 
2175 		if (status)
2176 			goto exit;
2177 
2178 		LIST_DEL(&list_elem->list_entry);
2179 		ice_free(hw, list_elem);
2180 	}
2181 exit:
2182 	ice_release_lock(rule_lock);
2183 	return status;
2184 }
2185 
2186 /**
2187  * ice_aq_get_res_alloc - get allocated resources
2188  * @hw: pointer to the HW struct
2189  * @num_entries: pointer to u16 to store the number of resource entries returned
2190  * @buf: pointer to buffer
2191  * @buf_size: size of buf
2192  * @cd: pointer to command details structure or NULL
2193  *
2194  * The caller-supplied buffer must be large enough to store the resource
2195  * information for all resource types. Each resource type is an
2196  * ice_aqc_get_res_resp_elem structure.
2197  */
2198 enum ice_status
2199 ice_aq_get_res_alloc(struct ice_hw *hw, u16 *num_entries,
2200 		     struct ice_aqc_get_res_resp_elem *buf, u16 buf_size,
2201 		     struct ice_sq_cd *cd)
2202 {
2203 	struct ice_aqc_get_res_alloc *resp;
2204 	enum ice_status status;
2205 	struct ice_aq_desc desc;
2206 
2207 	if (!buf)
2208 		return ICE_ERR_BAD_PTR;
2209 
2210 	if (buf_size < ICE_AQ_GET_RES_ALLOC_BUF_LEN)
2211 		return ICE_ERR_INVAL_SIZE;
2212 
2213 	resp = &desc.params.get_res;
2214 
2215 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_res_alloc);
2216 	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2217 
2218 	if (!status && num_entries)
2219 		*num_entries = LE16_TO_CPU(resp->resp_elem_num);
2220 
2221 	return status;
2222 }
2223 
2224 /**
2225  * ice_aq_get_res_descs - get allocated resource descriptors
2226  * @hw: pointer to the hardware structure
2227  * @num_entries: number of resource entries in buffer
2228  * @buf: structure to hold response data buffer
2229  * @buf_size: size of buffer
2230  * @res_type: resource type
2231  * @res_shared: is resource shared
2232  * @desc_id: input - first desc ID to start; output - next desc ID
2233  * @cd: pointer to command details structure or NULL
2234  */
2235 enum ice_status
2236 ice_aq_get_res_descs(struct ice_hw *hw, u16 num_entries,
2237 		     struct ice_aqc_res_elem *buf, u16 buf_size, u16 res_type,
2238 		     bool res_shared, u16 *desc_id, struct ice_sq_cd *cd)
2239 {
2240 	struct ice_aqc_get_allocd_res_desc *cmd;
2241 	struct ice_aq_desc desc;
2242 	enum ice_status status;
2243 
2244 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2245 
2246 	cmd = &desc.params.get_res_desc;
2247 
2248 	if (!buf)
2249 		return ICE_ERR_PARAM;
2250 
2251 	if (buf_size != (num_entries * sizeof(*buf)))
2252 		return ICE_ERR_PARAM;
2253 
2254 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_allocd_res_desc);
2255 
2256 	cmd->ops.cmd.res = CPU_TO_LE16(((res_type << ICE_AQC_RES_TYPE_S) &
2257 					 ICE_AQC_RES_TYPE_M) | (res_shared ?
2258 					ICE_AQC_RES_TYPE_FLAG_SHARED : 0));
2259 	cmd->ops.cmd.first_desc = CPU_TO_LE16(*desc_id);
2260 
2261 	status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2262 	if (!status)
2263 		*desc_id = LE16_TO_CPU(cmd->ops.resp.next_desc);
2264 
2265 	return status;
2266 }
2267 
2268 /**
2269  * ice_add_mac_rule - Add a MAC address based filter rule
2270  * @hw: pointer to the hardware structure
2271  * @m_list: list of MAC addresses and forwarding information
2272  * @sw: pointer to switch info struct for which function add rule
2273  * @lport: logic port number on which function add rule
2274  *
2275  * IMPORTANT: When the umac_shared flag is set to false and m_list has
2276  * multiple unicast addresses, the function assumes that all the
2277  * addresses are unique in a given add_mac call. It doesn't
2278  * check for duplicates in this case, removing duplicates from a given
2279  * list should be taken care of in the caller of this function.
2280  */
2281 static enum ice_status
2282 ice_add_mac_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list,
2283 		 struct ice_switch_info *sw, u8 lport)
2284 {
2285 	struct ice_sw_recipe *recp_list = &sw->recp_list[ICE_SW_LKUP_MAC];
2286 	struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
2287 	struct ice_fltr_list_entry *m_list_itr;
2288 	struct LIST_HEAD_TYPE *rule_head;
2289 	u16 total_elem_left, s_rule_size;
2290 	struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2291 	enum ice_status status = ICE_SUCCESS;
2292 	u16 num_unicast = 0;
2293 	u8 elem_sent;
2294 
2295 	s_rule = NULL;
2296 	rule_lock = &recp_list->filt_rule_lock;
2297 	rule_head = &recp_list->filt_rules;
2298 
2299 	LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2300 			    list_entry) {
2301 		u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
2302 		u16 vsi_handle;
2303 		u16 hw_vsi_id;
2304 
2305 		m_list_itr->fltr_info.flag = ICE_FLTR_TX;
2306 		vsi_handle = m_list_itr->fltr_info.vsi_handle;
2307 		if (!ice_is_vsi_valid(hw, vsi_handle))
2308 			return ICE_ERR_PARAM;
2309 		hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2310 		m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
2311 		/* update the src in case it is VSI num */
2312 		if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
2313 			return ICE_ERR_PARAM;
2314 		m_list_itr->fltr_info.src = hw_vsi_id;
2315 		if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
2316 		    IS_ZERO_ETHER_ADDR(add))
2317 			return ICE_ERR_PARAM;
2318 		if (IS_UNICAST_ETHER_ADDR(add) && !hw->umac_shared) {
2319 			/* Don't overwrite the unicast address */
2320 			ice_acquire_lock(rule_lock);
2321 			if (ice_find_rule_entry(rule_head,
2322 						&m_list_itr->fltr_info)) {
2323 				ice_release_lock(rule_lock);
2324 				continue;
2325 			}
2326 			ice_release_lock(rule_lock);
2327 			num_unicast++;
2328 		} else if (IS_MULTICAST_ETHER_ADDR(add) ||
2329 			   (IS_UNICAST_ETHER_ADDR(add) && hw->umac_shared)) {
2330 			m_list_itr->status =
2331 				ice_add_rule_internal(hw, recp_list, lport,
2332 						      m_list_itr);
2333 			if (m_list_itr->status)
2334 				return m_list_itr->status;
2335 		}
2336 	}
2337 
2338 	ice_acquire_lock(rule_lock);
2339 	/* Exit if no suitable entries were found for adding bulk switch rule */
2340 	if (!num_unicast) {
2341 		status = ICE_SUCCESS;
2342 		goto ice_add_mac_exit;
2343 	}
2344 
2345 	/* Allocate switch rule buffer for the bulk update for unicast */
2346 	s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
2347 	s_rule = (struct ice_aqc_sw_rules_elem *)
2348 		ice_calloc(hw, num_unicast, s_rule_size);
2349 	if (!s_rule) {
2350 		status = ICE_ERR_NO_MEMORY;
2351 		goto ice_add_mac_exit;
2352 	}
2353 
2354 	r_iter = s_rule;
2355 	LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2356 			    list_entry) {
2357 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2358 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2359 
2360 		if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2361 			ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
2362 					 ice_aqc_opc_add_sw_rules);
2363 			r_iter = (struct ice_aqc_sw_rules_elem *)
2364 				((u8 *)r_iter + s_rule_size);
2365 		}
2366 	}
2367 
2368 	/* Call AQ bulk switch rule update for all unicast addresses */
2369 	r_iter = s_rule;
2370 	/* Call AQ switch rule in AQ_MAX chunk */
2371 	for (total_elem_left = num_unicast; total_elem_left > 0;
2372 	     total_elem_left -= elem_sent) {
2373 		struct ice_aqc_sw_rules_elem *entry = r_iter;
2374 
2375 		elem_sent = MIN_T(u8, total_elem_left,
2376 				  (ICE_AQ_MAX_BUF_LEN / s_rule_size));
2377 		status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
2378 					 elem_sent, ice_aqc_opc_add_sw_rules,
2379 					 NULL);
2380 		if (status)
2381 			goto ice_add_mac_exit;
2382 		r_iter = (struct ice_aqc_sw_rules_elem *)
2383 			((u8 *)r_iter + (elem_sent * s_rule_size));
2384 	}
2385 
2386 	/* Fill up rule ID based on the value returned from FW */
2387 	r_iter = s_rule;
2388 	LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2389 			    list_entry) {
2390 		struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2391 		u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2392 		struct ice_fltr_mgmt_list_entry *fm_entry;
2393 
2394 		if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2395 			f_info->fltr_rule_id =
2396 				LE16_TO_CPU(r_iter->pdata.lkup_tx_rx.index);
2397 			f_info->fltr_act = ICE_FWD_TO_VSI;
2398 			/* Create an entry to track this MAC address */
2399 			fm_entry = (struct ice_fltr_mgmt_list_entry *)
2400 				ice_malloc(hw, sizeof(*fm_entry));
2401 			if (!fm_entry) {
2402 				status = ICE_ERR_NO_MEMORY;
2403 				goto ice_add_mac_exit;
2404 			}
2405 			fm_entry->fltr_info = *f_info;
2406 			fm_entry->vsi_count = 1;
2407 			/* The book keeping entries will get removed when
2408 			 * base driver calls remove filter AQ command
2409 			 */
2410 
2411 			LIST_ADD(&fm_entry->list_entry, rule_head);
2412 			r_iter = (struct ice_aqc_sw_rules_elem *)
2413 				((u8 *)r_iter + s_rule_size);
2414 		}
2415 	}
2416 
2417 ice_add_mac_exit:
2418 	ice_release_lock(rule_lock);
2419 	if (s_rule)
2420 		ice_free(hw, s_rule);
2421 	return status;
2422 }
2423 
2424 /**
2425  * ice_add_mac - Add a MAC address based filter rule
2426  * @hw: pointer to the hardware structure
2427  * @m_list: list of MAC addresses and forwarding information
2428  *
2429  * Function add MAC rule for logical port from HW struct
2430  */
2431 enum ice_status ice_add_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2432 {
2433 	if (!m_list || !hw)
2434 		return ICE_ERR_PARAM;
2435 
2436 	return ice_add_mac_rule(hw, m_list, hw->switch_info,
2437 				hw->port_info->lport);
2438 }
2439 
2440 /**
2441  * ice_add_vlan_internal - Add one VLAN based filter rule
2442  * @hw: pointer to the hardware structure
2443  * @recp_list: recipe list for which rule has to be added
2444  * @f_entry: filter entry containing one VLAN information
2445  */
2446 static enum ice_status
2447 ice_add_vlan_internal(struct ice_hw *hw, struct ice_sw_recipe *recp_list,
2448 		      struct ice_fltr_list_entry *f_entry)
2449 {
2450 	struct ice_fltr_mgmt_list_entry *v_list_itr;
2451 	struct ice_fltr_info *new_fltr, *cur_fltr;
2452 	enum ice_sw_lkup_type lkup_type;
2453 	u16 vsi_list_id = 0, vsi_handle;
2454 	struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2455 	enum ice_status status = ICE_SUCCESS;
2456 
2457 	if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2458 		return ICE_ERR_PARAM;
2459 
2460 	f_entry->fltr_info.fwd_id.hw_vsi_id =
2461 		ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2462 	new_fltr = &f_entry->fltr_info;
2463 
2464 	/* VLAN ID should only be 12 bits */
2465 	if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
2466 		return ICE_ERR_PARAM;
2467 
2468 	if (new_fltr->src_id != ICE_SRC_ID_VSI)
2469 		return ICE_ERR_PARAM;
2470 
2471 	new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
2472 	lkup_type = new_fltr->lkup_type;
2473 	vsi_handle = new_fltr->vsi_handle;
2474 	rule_lock = &recp_list->filt_rule_lock;
2475 	ice_acquire_lock(rule_lock);
2476 	v_list_itr = ice_find_rule_entry(&recp_list->filt_rules, new_fltr);
2477 	if (!v_list_itr) {
2478 		struct ice_vsi_list_map_info *map_info = NULL;
2479 
2480 		if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
2481 			/* All VLAN pruning rules use a VSI list. Check if
2482 			 * there is already a VSI list containing VSI that we
2483 			 * want to add. If found, use the same vsi_list_id for
2484 			 * this new VLAN rule or else create a new list.
2485 			 */
2486 			map_info = ice_find_vsi_list_entry(recp_list,
2487 							   vsi_handle,
2488 							   &vsi_list_id);
2489 			if (!map_info) {
2490 				status = ice_create_vsi_list_rule(hw,
2491 								  &vsi_handle,
2492 								  1,
2493 								  &vsi_list_id,
2494 								  lkup_type);
2495 				if (status)
2496 					goto exit;
2497 			}
2498 			/* Convert the action to forwarding to a VSI list. */
2499 			new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2500 			new_fltr->fwd_id.vsi_list_id = vsi_list_id;
2501 		}
2502 
2503 		status = ice_create_pkt_fwd_rule(hw, recp_list, f_entry);
2504 		if (!status) {
2505 			v_list_itr = ice_find_rule_entry(&recp_list->filt_rules,
2506 							 new_fltr);
2507 			if (!v_list_itr) {
2508 				status = ICE_ERR_DOES_NOT_EXIST;
2509 				goto exit;
2510 			}
2511 			/* reuse VSI list for new rule and increment ref_cnt */
2512 			if (map_info) {
2513 				v_list_itr->vsi_list_info = map_info;
2514 				map_info->ref_cnt++;
2515 			} else {
2516 				v_list_itr->vsi_list_info =
2517 					ice_create_vsi_list_map(hw, &vsi_handle,
2518 								1, vsi_list_id);
2519 			}
2520 		}
2521 	} else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
2522 		/* Update existing VSI list to add new VSI ID only if it used
2523 		 * by one VLAN rule.
2524 		 */
2525 		cur_fltr = &v_list_itr->fltr_info;
2526 		status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
2527 						 new_fltr);
2528 	} else {
2529 		/* If VLAN rule exists and VSI list being used by this rule is
2530 		 * referenced by more than 1 VLAN rule. Then create a new VSI
2531 		 * list appending previous VSI with new VSI and update existing
2532 		 * VLAN rule to point to new VSI list ID
2533 		 */
2534 		struct ice_fltr_info tmp_fltr;
2535 		u16 vsi_handle_arr[2];
2536 		u16 cur_handle;
2537 
2538 		/* Current implementation only supports reusing VSI list with
2539 		 * one VSI count. We should never hit below condition
2540 		 */
2541 		if (v_list_itr->vsi_count > 1 &&
2542 		    v_list_itr->vsi_list_info->ref_cnt > 1) {
2543 			ice_debug(hw, ICE_DBG_SW, "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
2544 			status = ICE_ERR_CFG;
2545 			goto exit;
2546 		}
2547 
2548 		cur_handle =
2549 			ice_find_first_bit(v_list_itr->vsi_list_info->vsi_map,
2550 					   ICE_MAX_VSI);
2551 
2552 		/* A rule already exists with the new VSI being added */
2553 		if (cur_handle == vsi_handle) {
2554 			status = ICE_ERR_ALREADY_EXISTS;
2555 			goto exit;
2556 		}
2557 
2558 		vsi_handle_arr[0] = cur_handle;
2559 		vsi_handle_arr[1] = vsi_handle;
2560 		status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2561 						  &vsi_list_id, lkup_type);
2562 		if (status)
2563 			goto exit;
2564 
2565 		tmp_fltr = v_list_itr->fltr_info;
2566 		tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
2567 		tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2568 		tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2569 		/* Update the previous switch rule to a new VSI list which
2570 		 * includes current VSI that is requested
2571 		 */
2572 		status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2573 		if (status)
2574 			goto exit;
2575 
2576 		/* before overriding VSI list map info. decrement ref_cnt of
2577 		 * previous VSI list
2578 		 */
2579 		v_list_itr->vsi_list_info->ref_cnt--;
2580 
2581 		/* now update to newly created list */
2582 		v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
2583 		v_list_itr->vsi_list_info =
2584 			ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2585 						vsi_list_id);
2586 		v_list_itr->vsi_count++;
2587 	}
2588 
2589 exit:
2590 	ice_release_lock(rule_lock);
2591 	return status;
2592 }
2593 
2594 /**
2595  * ice_add_vlan_rule - Add VLAN based filter rule
2596  * @hw: pointer to the hardware structure
2597  * @v_list: list of VLAN entries and forwarding information
2598  * @sw: pointer to switch info struct for which function add rule
2599  */
2600 static enum ice_status
2601 ice_add_vlan_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list,
2602 		  struct ice_switch_info *sw)
2603 {
2604 	struct ice_fltr_list_entry *v_list_itr;
2605 	struct ice_sw_recipe *recp_list;
2606 
2607 	recp_list = &sw->recp_list[ICE_SW_LKUP_VLAN];
2608 	LIST_FOR_EACH_ENTRY(v_list_itr, v_list, ice_fltr_list_entry,
2609 			    list_entry) {
2610 		if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
2611 			return ICE_ERR_PARAM;
2612 		v_list_itr->fltr_info.flag = ICE_FLTR_TX;
2613 		v_list_itr->status = ice_add_vlan_internal(hw, recp_list,
2614 							   v_list_itr);
2615 		if (v_list_itr->status)
2616 			return v_list_itr->status;
2617 	}
2618 	return ICE_SUCCESS;
2619 }
2620 
2621 /**
2622  * ice_add_vlan - Add a VLAN based filter rule
2623  * @hw: pointer to the hardware structure
2624  * @v_list: list of VLAN and forwarding information
2625  *
2626  * Function add VLAN rule for logical port from HW struct
2627  */
2628 enum ice_status ice_add_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2629 {
2630 	if (!v_list || !hw)
2631 		return ICE_ERR_PARAM;
2632 
2633 	return ice_add_vlan_rule(hw, v_list, hw->switch_info);
2634 }
2635 
2636 /**
2637  * ice_add_eth_mac_rule - Add ethertype and MAC based filter rule
2638  * @hw: pointer to the hardware structure
2639  * @em_list: list of ether type MAC filter, MAC is optional
2640  * @sw: pointer to switch info struct for which function add rule
2641  * @lport: logic port number on which function add rule
2642  *
2643  * This function requires the caller to populate the entries in
2644  * the filter list with the necessary fields (including flags to
2645  * indicate Tx or Rx rules).
2646  */
2647 static enum ice_status
2648 ice_add_eth_mac_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list,
2649 		     struct ice_switch_info *sw, u8 lport)
2650 {
2651 	struct ice_fltr_list_entry *em_list_itr;
2652 
2653 	LIST_FOR_EACH_ENTRY(em_list_itr, em_list, ice_fltr_list_entry,
2654 			    list_entry) {
2655 		struct ice_sw_recipe *recp_list;
2656 		enum ice_sw_lkup_type l_type;
2657 
2658 		l_type = em_list_itr->fltr_info.lkup_type;
2659 		recp_list = &sw->recp_list[l_type];
2660 
2661 		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2662 		    l_type != ICE_SW_LKUP_ETHERTYPE)
2663 			return ICE_ERR_PARAM;
2664 
2665 		em_list_itr->status = ice_add_rule_internal(hw, recp_list,
2666 							    lport,
2667 							    em_list_itr);
2668 		if (em_list_itr->status)
2669 			return em_list_itr->status;
2670 	}
2671 	return ICE_SUCCESS;
2672 }
2673 
2674 /**
2675  * ice_add_eth_mac - Add a ethertype based filter rule
2676  * @hw: pointer to the hardware structure
2677  * @em_list: list of ethertype and forwarding information
2678  *
2679  * Function add ethertype rule for logical port from HW struct
2680  */
2681 enum ice_status
2682 ice_add_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2683 {
2684 	if (!em_list || !hw)
2685 		return ICE_ERR_PARAM;
2686 
2687 	return ice_add_eth_mac_rule(hw, em_list, hw->switch_info,
2688 				    hw->port_info->lport);
2689 }
2690 
2691 /**
2692  * ice_remove_eth_mac_rule - Remove an ethertype (or MAC) based filter rule
2693  * @hw: pointer to the hardware structure
2694  * @em_list: list of ethertype or ethertype MAC entries
2695  * @sw: pointer to switch info struct for which function add rule
2696  */
2697 static enum ice_status
2698 ice_remove_eth_mac_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list,
2699 			struct ice_switch_info *sw)
2700 {
2701 	struct ice_fltr_list_entry *em_list_itr, *tmp;
2702 
2703 	LIST_FOR_EACH_ENTRY_SAFE(em_list_itr, tmp, em_list, ice_fltr_list_entry,
2704 				 list_entry) {
2705 		struct ice_sw_recipe *recp_list;
2706 		enum ice_sw_lkup_type l_type;
2707 
2708 		l_type = em_list_itr->fltr_info.lkup_type;
2709 
2710 		if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2711 		    l_type != ICE_SW_LKUP_ETHERTYPE)
2712 			return ICE_ERR_PARAM;
2713 
2714 		recp_list = &sw->recp_list[l_type];
2715 		em_list_itr->status = ice_remove_rule_internal(hw, recp_list,
2716 							       em_list_itr);
2717 		if (em_list_itr->status)
2718 			return em_list_itr->status;
2719 	}
2720 	return ICE_SUCCESS;
2721 }
2722 
2723 /**
2724  * ice_remove_eth_mac - remove a ethertype based filter rule
2725  * @hw: pointer to the hardware structure
2726  * @em_list: list of ethertype and forwarding information
2727  *
2728  */
2729 enum ice_status
2730 ice_remove_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2731 {
2732 	if (!em_list || !hw)
2733 		return ICE_ERR_PARAM;
2734 
2735 	return ice_remove_eth_mac_rule(hw, em_list, hw->switch_info);
2736 }
2737 
2738 /**
2739  * ice_rem_sw_rule_info
2740  * @hw: pointer to the hardware structure
2741  * @rule_head: pointer to the switch list structure that we want to delete
2742  */
2743 static void
2744 ice_rem_sw_rule_info(struct ice_hw *hw, struct LIST_HEAD_TYPE *rule_head)
2745 {
2746 	if (!LIST_EMPTY(rule_head)) {
2747 		struct ice_fltr_mgmt_list_entry *entry;
2748 		struct ice_fltr_mgmt_list_entry *tmp;
2749 
2750 		LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, rule_head,
2751 					 ice_fltr_mgmt_list_entry, list_entry) {
2752 			LIST_DEL(&entry->list_entry);
2753 			ice_free(hw, entry);
2754 		}
2755 	}
2756 }
2757 
2758 /**
2759  * ice_rem_all_sw_rules_info
2760  * @hw: pointer to the hardware structure
2761  */
2762 void ice_rem_all_sw_rules_info(struct ice_hw *hw)
2763 {
2764 	struct ice_switch_info *sw = hw->switch_info;
2765 	u8 i;
2766 
2767 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
2768 		struct LIST_HEAD_TYPE *rule_head;
2769 
2770 		rule_head = &sw->recp_list[i].filt_rules;
2771 		if (!sw->recp_list[i].adv_rule)
2772 			ice_rem_sw_rule_info(hw, rule_head);
2773 	}
2774 }
2775 
2776 /**
2777  * ice_cfg_dflt_vsi - change state of VSI to set/clear default
2778  * @pi: pointer to the port_info structure
2779  * @vsi_handle: VSI handle to set as default
2780  * @set: true to add the above mentioned switch rule, false to remove it
2781  * @direction: ICE_FLTR_RX or ICE_FLTR_TX
2782  *
2783  * add filter rule to set/unset given VSI as default VSI for the switch
2784  * (represented by swid)
2785  */
2786 enum ice_status
2787 ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
2788 		 u8 direction)
2789 {
2790 	struct ice_aqc_sw_rules_elem *s_rule;
2791 	struct ice_fltr_info f_info;
2792 	struct ice_hw *hw = pi->hw;
2793 	enum ice_adminq_opc opcode;
2794 	enum ice_status status;
2795 	u16 s_rule_size;
2796 	u16 hw_vsi_id;
2797 
2798 	if (!ice_is_vsi_valid(hw, vsi_handle))
2799 		return ICE_ERR_PARAM;
2800 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2801 
2802 	s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
2803 		ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
2804 
2805 	s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
2806 	if (!s_rule)
2807 		return ICE_ERR_NO_MEMORY;
2808 
2809 	ice_memset(&f_info, 0, sizeof(f_info), ICE_NONDMA_MEM);
2810 
2811 	f_info.lkup_type = ICE_SW_LKUP_DFLT;
2812 	f_info.flag = direction;
2813 	f_info.fltr_act = ICE_FWD_TO_VSI;
2814 	f_info.fwd_id.hw_vsi_id = hw_vsi_id;
2815 
2816 	if (f_info.flag & ICE_FLTR_RX) {
2817 		f_info.src = pi->lport;
2818 		f_info.src_id = ICE_SRC_ID_LPORT;
2819 		if (!set)
2820 			f_info.fltr_rule_id =
2821 				pi->dflt_rx_vsi_rule_id;
2822 	} else if (f_info.flag & ICE_FLTR_TX) {
2823 		f_info.src_id = ICE_SRC_ID_VSI;
2824 		f_info.src = hw_vsi_id;
2825 		if (!set)
2826 			f_info.fltr_rule_id =
2827 				pi->dflt_tx_vsi_rule_id;
2828 	}
2829 
2830 	if (set)
2831 		opcode = ice_aqc_opc_add_sw_rules;
2832 	else
2833 		opcode = ice_aqc_opc_remove_sw_rules;
2834 
2835 	ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
2836 
2837 	status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
2838 	if (status || !(f_info.flag & ICE_FLTR_TX_RX))
2839 		goto out;
2840 	if (set) {
2841 		u16 index = LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
2842 
2843 		if (f_info.flag & ICE_FLTR_TX) {
2844 			pi->dflt_tx_vsi_num = hw_vsi_id;
2845 			pi->dflt_tx_vsi_rule_id = index;
2846 		} else if (f_info.flag & ICE_FLTR_RX) {
2847 			pi->dflt_rx_vsi_num = hw_vsi_id;
2848 			pi->dflt_rx_vsi_rule_id = index;
2849 		}
2850 	} else {
2851 		if (f_info.flag & ICE_FLTR_TX) {
2852 			pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
2853 			pi->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
2854 		} else if (f_info.flag & ICE_FLTR_RX) {
2855 			pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
2856 			pi->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
2857 		}
2858 	}
2859 
2860 out:
2861 	ice_free(hw, s_rule);
2862 	return status;
2863 }
2864 
2865 /**
2866  * ice_find_ucast_rule_entry - Search for a unicast MAC filter rule entry
2867  * @list_head: head of rule list
2868  * @f_info: rule information
2869  *
2870  * Helper function to search for a unicast rule entry - this is to be used
2871  * to remove unicast MAC filter that is not shared with other VSIs on the
2872  * PF switch.
2873  *
2874  * Returns pointer to entry storing the rule if found
2875  */
2876 static struct ice_fltr_mgmt_list_entry *
2877 ice_find_ucast_rule_entry(struct LIST_HEAD_TYPE *list_head,
2878 			  struct ice_fltr_info *f_info)
2879 {
2880 	struct ice_fltr_mgmt_list_entry *list_itr;
2881 
2882 	LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
2883 			    list_entry) {
2884 		if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
2885 			    sizeof(f_info->l_data)) &&
2886 		    f_info->fwd_id.hw_vsi_id ==
2887 		    list_itr->fltr_info.fwd_id.hw_vsi_id &&
2888 		    f_info->flag == list_itr->fltr_info.flag)
2889 			return list_itr;
2890 	}
2891 	return NULL;
2892 }
2893 
2894 /**
2895  * ice_remove_mac_rule - remove a MAC based filter rule
2896  * @hw: pointer to the hardware structure
2897  * @m_list: list of MAC addresses and forwarding information
2898  * @recp_list: list from which function remove MAC address
2899  *
2900  * This function removes either a MAC filter rule or a specific VSI from a
2901  * VSI list for a multicast MAC address.
2902  *
2903  * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
2904  * ice_add_mac. Caller should be aware that this call will only work if all
2905  * the entries passed into m_list were added previously. It will not attempt to
2906  * do a partial remove of entries that were found.
2907  */
2908 static enum ice_status
2909 ice_remove_mac_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list,
2910 		    struct ice_sw_recipe *recp_list)
2911 {
2912 	struct ice_fltr_list_entry *list_itr, *tmp;
2913 	struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2914 
2915 	if (!m_list)
2916 		return ICE_ERR_PARAM;
2917 
2918 	rule_lock = &recp_list->filt_rule_lock;
2919 	LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, m_list, ice_fltr_list_entry,
2920 				 list_entry) {
2921 		enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2922 		u8 *add = &list_itr->fltr_info.l_data.mac.mac_addr[0];
2923 		u16 vsi_handle;
2924 
2925 		if (l_type != ICE_SW_LKUP_MAC)
2926 			return ICE_ERR_PARAM;
2927 
2928 		vsi_handle = list_itr->fltr_info.vsi_handle;
2929 		if (!ice_is_vsi_valid(hw, vsi_handle))
2930 			return ICE_ERR_PARAM;
2931 
2932 		list_itr->fltr_info.fwd_id.hw_vsi_id =
2933 					ice_get_hw_vsi_num(hw, vsi_handle);
2934 		if (IS_UNICAST_ETHER_ADDR(add) && !hw->umac_shared) {
2935 			/* Don't remove the unicast address that belongs to
2936 			 * another VSI on the switch, since it is not being
2937 			 * shared...
2938 			 */
2939 			ice_acquire_lock(rule_lock);
2940 			if (!ice_find_ucast_rule_entry(&recp_list->filt_rules,
2941 						       &list_itr->fltr_info)) {
2942 				ice_release_lock(rule_lock);
2943 				return ICE_ERR_DOES_NOT_EXIST;
2944 			}
2945 			ice_release_lock(rule_lock);
2946 		}
2947 		list_itr->status = ice_remove_rule_internal(hw, recp_list,
2948 							    list_itr);
2949 		if (list_itr->status)
2950 			return list_itr->status;
2951 	}
2952 	return ICE_SUCCESS;
2953 }
2954 
2955 /**
2956  * ice_remove_mac - remove a MAC address based filter rule
2957  * @hw: pointer to the hardware structure
2958  * @m_list: list of MAC addresses and forwarding information
2959  *
2960  */
2961 enum ice_status ice_remove_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2962 {
2963 	struct ice_sw_recipe *recp_list;
2964 
2965 	recp_list = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC];
2966 	return ice_remove_mac_rule(hw, m_list, recp_list);
2967 }
2968 
2969 /**
2970  * ice_remove_vlan_rule - Remove VLAN based filter rule
2971  * @hw: pointer to the hardware structure
2972  * @v_list: list of VLAN entries and forwarding information
2973  * @recp_list: list from which function remove VLAN
2974  */
2975 static enum ice_status
2976 ice_remove_vlan_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list,
2977 		     struct ice_sw_recipe *recp_list)
2978 {
2979 	struct ice_fltr_list_entry *v_list_itr, *tmp;
2980 
2981 	LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2982 				 list_entry) {
2983 		enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2984 
2985 		if (l_type != ICE_SW_LKUP_VLAN)
2986 			return ICE_ERR_PARAM;
2987 		v_list_itr->status = ice_remove_rule_internal(hw, recp_list,
2988 							      v_list_itr);
2989 		if (v_list_itr->status)
2990 			return v_list_itr->status;
2991 	}
2992 	return ICE_SUCCESS;
2993 }
2994 
2995 /**
2996  * ice_remove_vlan - remove a VLAN address based filter rule
2997  * @hw: pointer to the hardware structure
2998  * @v_list: list of VLAN and forwarding information
2999  *
3000  */
3001 enum ice_status
3002 ice_remove_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
3003 {
3004 	struct ice_sw_recipe *recp_list;
3005 
3006 	if (!v_list || !hw)
3007 		return ICE_ERR_PARAM;
3008 
3009 	recp_list = &hw->switch_info->recp_list[ICE_SW_LKUP_VLAN];
3010 	return ice_remove_vlan_rule(hw, v_list, recp_list);
3011 }
3012 
3013 /**
3014  * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
3015  * @fm_entry: filter entry to inspect
3016  * @vsi_handle: VSI handle to compare with filter info
3017  */
3018 static bool
3019 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
3020 {
3021 	return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
3022 		 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
3023 		(fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
3024 		 fm_entry->vsi_list_info &&
3025 		 (ice_is_bit_set(fm_entry->vsi_list_info->vsi_map,
3026 				 vsi_handle))));
3027 }
3028 
3029 /**
3030  * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
3031  * @hw: pointer to the hardware structure
3032  * @vsi_handle: VSI handle to remove filters from
3033  * @vsi_list_head: pointer to the list to add entry to
3034  * @fi: pointer to fltr_info of filter entry to copy & add
3035  *
3036  * Helper function, used when creating a list of filters to remove from
3037  * a specific VSI. The entry added to vsi_list_head is a COPY of the
3038  * original filter entry, with the exception of fltr_info.fltr_act and
3039  * fltr_info.fwd_id fields. These are set such that later logic can
3040  * extract which VSI to remove the fltr from, and pass on that information.
3041  */
3042 static enum ice_status
3043 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3044 			       struct LIST_HEAD_TYPE *vsi_list_head,
3045 			       struct ice_fltr_info *fi)
3046 {
3047 	struct ice_fltr_list_entry *tmp;
3048 
3049 	/* this memory is freed up in the caller function
3050 	 * once filters for this VSI are removed
3051 	 */
3052 	tmp = (struct ice_fltr_list_entry *)ice_malloc(hw, sizeof(*tmp));
3053 	if (!tmp)
3054 		return ICE_ERR_NO_MEMORY;
3055 
3056 	tmp->fltr_info = *fi;
3057 
3058 	/* Overwrite these fields to indicate which VSI to remove filter from,
3059 	 * so find and remove logic can extract the information from the
3060 	 * list entries. Note that original entries will still have proper
3061 	 * values.
3062 	 */
3063 	tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
3064 	tmp->fltr_info.vsi_handle = vsi_handle;
3065 	tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3066 
3067 	LIST_ADD(&tmp->list_entry, vsi_list_head);
3068 
3069 	return ICE_SUCCESS;
3070 }
3071 
3072 /**
3073  * ice_add_to_vsi_fltr_list - Add VSI filters to the list
3074  * @hw: pointer to the hardware structure
3075  * @vsi_handle: VSI handle to remove filters from
3076  * @lkup_list_head: pointer to the list that has certain lookup type filters
3077  * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
3078  *
3079  * Locates all filters in lkup_list_head that are used by the given VSI,
3080  * and adds COPIES of those entries to vsi_list_head (intended to be used
3081  * to remove the listed filters).
3082  * Note that this means all entries in vsi_list_head must be explicitly
3083  * deallocated by the caller when done with list.
3084  */
3085 static enum ice_status
3086 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3087 			 struct LIST_HEAD_TYPE *lkup_list_head,
3088 			 struct LIST_HEAD_TYPE *vsi_list_head)
3089 {
3090 	struct ice_fltr_mgmt_list_entry *fm_entry;
3091 	enum ice_status status = ICE_SUCCESS;
3092 
3093 	/* check to make sure VSI ID is valid and within boundary */
3094 	if (!ice_is_vsi_valid(hw, vsi_handle))
3095 		return ICE_ERR_PARAM;
3096 
3097 	LIST_FOR_EACH_ENTRY(fm_entry, lkup_list_head,
3098 			    ice_fltr_mgmt_list_entry, list_entry) {
3099 		if (!ice_vsi_uses_fltr(fm_entry, vsi_handle))
3100 			continue;
3101 
3102 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3103 							vsi_list_head,
3104 							&fm_entry->fltr_info);
3105 		if (status)
3106 			return status;
3107 	}
3108 	return status;
3109 }
3110 
3111 /**
3112  * ice_determine_promisc_mask
3113  * @fi: filter info to parse
3114  *
3115  * Helper function to determine which ICE_PROMISC_ mask corresponds
3116  * to given filter into.
3117  */
3118 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
3119 {
3120 	u16 vid = fi->l_data.mac_vlan.vlan_id;
3121 	u8 *macaddr = fi->l_data.mac.mac_addr;
3122 	bool is_tx_fltr = false;
3123 	u8 promisc_mask = 0;
3124 
3125 	if (fi->flag == ICE_FLTR_TX)
3126 		is_tx_fltr = true;
3127 
3128 	if (IS_BROADCAST_ETHER_ADDR(macaddr))
3129 		promisc_mask |= is_tx_fltr ?
3130 			ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
3131 	else if (IS_MULTICAST_ETHER_ADDR(macaddr))
3132 		promisc_mask |= is_tx_fltr ?
3133 			ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
3134 	else if (IS_UNICAST_ETHER_ADDR(macaddr))
3135 		promisc_mask |= is_tx_fltr ?
3136 			ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
3137 	if (vid)
3138 		promisc_mask |= is_tx_fltr ?
3139 			ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
3140 
3141 	return promisc_mask;
3142 }
3143 
3144 /**
3145  * _ice_get_vsi_promisc - get promiscuous mode of given VSI
3146  * @hw: pointer to the hardware structure
3147  * @vsi_handle: VSI handle to retrieve info from
3148  * @promisc_mask: pointer to mask to be filled in
3149  * @vid: VLAN ID of promisc VLAN VSI
3150  * @sw: pointer to switch info struct for which function add rule
3151  * @lkup: switch rule filter lookup type
3152  */
3153 static enum ice_status
3154 _ice_get_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3155 		     u16 *vid, struct ice_switch_info *sw,
3156 		     enum ice_sw_lkup_type lkup)
3157 {
3158 	struct ice_fltr_mgmt_list_entry *itr;
3159 	struct LIST_HEAD_TYPE *rule_head;
3160 	struct ice_lock *rule_lock;	/* Lock to protect filter rule list */
3161 
3162 	if (!ice_is_vsi_valid(hw, vsi_handle) ||
3163 	    (lkup != ICE_SW_LKUP_PROMISC && lkup != ICE_SW_LKUP_PROMISC_VLAN))
3164 		return ICE_ERR_PARAM;
3165 
3166 	*vid = 0;
3167 	*promisc_mask = 0;
3168 	rule_head = &sw->recp_list[lkup].filt_rules;
3169 	rule_lock = &sw->recp_list[lkup].filt_rule_lock;
3170 
3171 	ice_acquire_lock(rule_lock);
3172 	LIST_FOR_EACH_ENTRY(itr, rule_head,
3173 			    ice_fltr_mgmt_list_entry, list_entry) {
3174 		/* Continue if this filter doesn't apply to this VSI or the
3175 		 * VSI ID is not in the VSI map for this filter
3176 		 */
3177 		if (!ice_vsi_uses_fltr(itr, vsi_handle))
3178 			continue;
3179 
3180 		*promisc_mask |= ice_determine_promisc_mask(&itr->fltr_info);
3181 	}
3182 	ice_release_lock(rule_lock);
3183 
3184 	return ICE_SUCCESS;
3185 }
3186 
3187 /**
3188  * ice_get_vsi_promisc - get promiscuous mode of given VSI
3189  * @hw: pointer to the hardware structure
3190  * @vsi_handle: VSI handle to retrieve info from
3191  * @promisc_mask: pointer to mask to be filled in
3192  * @vid: VLAN ID of promisc VLAN VSI
3193  */
3194 enum ice_status
3195 ice_get_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3196 		    u16 *vid)
3197 {
3198 	return _ice_get_vsi_promisc(hw, vsi_handle, promisc_mask,
3199 				    vid, hw->switch_info, ICE_SW_LKUP_PROMISC);
3200 }
3201 
3202 /**
3203  * ice_get_vsi_vlan_promisc - get VLAN promiscuous mode of given VSI
3204  * @hw: pointer to the hardware structure
3205  * @vsi_handle: VSI handle to retrieve info from
3206  * @promisc_mask: pointer to mask to be filled in
3207  * @vid: VLAN ID of promisc VLAN VSI
3208  */
3209 enum ice_status
3210 ice_get_vsi_vlan_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3211 			 u16 *vid)
3212 {
3213 	return _ice_get_vsi_promisc(hw, vsi_handle, promisc_mask,
3214 				    vid, hw->switch_info,
3215 				    ICE_SW_LKUP_PROMISC_VLAN);
3216 }
3217 
3218 /**
3219  * ice_remove_promisc - Remove promisc based filter rules
3220  * @hw: pointer to the hardware structure
3221  * @recp_id: recipe ID for which the rule needs to removed
3222  * @v_list: list of promisc entries
3223  */
3224 static enum ice_status
3225 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
3226 		   struct LIST_HEAD_TYPE *v_list)
3227 {
3228 	struct ice_fltr_list_entry *v_list_itr, *tmp;
3229 	struct ice_sw_recipe *recp_list;
3230 
3231 	recp_list = &hw->switch_info->recp_list[recp_id];
3232 	LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
3233 				 list_entry) {
3234 		v_list_itr->status =
3235 			ice_remove_rule_internal(hw, recp_list, v_list_itr);
3236 		if (v_list_itr->status)
3237 			return v_list_itr->status;
3238 	}
3239 	return ICE_SUCCESS;
3240 }
3241 
3242 /**
3243  * _ice_clear_vsi_promisc - clear specified promiscuous mode(s)
3244  * @hw: pointer to the hardware structure
3245  * @vsi_handle: VSI handle to clear mode
3246  * @promisc_mask: mask of promiscuous config bits to clear
3247  * @vid: VLAN ID to clear VLAN promiscuous
3248  * @sw: pointer to switch info struct for which function add rule
3249  */
3250 static enum ice_status
3251 _ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3252 		       u16 vid, struct ice_switch_info *sw)
3253 {
3254 	struct ice_fltr_list_entry *fm_entry, *tmp;
3255 	struct LIST_HEAD_TYPE remove_list_head;
3256 	struct ice_fltr_mgmt_list_entry *itr;
3257 	struct LIST_HEAD_TYPE *rule_head;
3258 	struct ice_lock *rule_lock;	/* Lock to protect filter rule list */
3259 	enum ice_status status = ICE_SUCCESS;
3260 	u8 recipe_id;
3261 
3262 	if (!ice_is_vsi_valid(hw, vsi_handle))
3263 		return ICE_ERR_PARAM;
3264 
3265 	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX))
3266 		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3267 	else
3268 		recipe_id = ICE_SW_LKUP_PROMISC;
3269 
3270 	rule_head = &sw->recp_list[recipe_id].filt_rules;
3271 	rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
3272 
3273 	INIT_LIST_HEAD(&remove_list_head);
3274 
3275 	ice_acquire_lock(rule_lock);
3276 	LIST_FOR_EACH_ENTRY(itr, rule_head,
3277 			    ice_fltr_mgmt_list_entry, list_entry) {
3278 		struct ice_fltr_info *fltr_info;
3279 		u8 fltr_promisc_mask = 0;
3280 
3281 		if (!ice_vsi_uses_fltr(itr, vsi_handle))
3282 			continue;
3283 		fltr_info = &itr->fltr_info;
3284 
3285 		if (recipe_id == ICE_SW_LKUP_PROMISC_VLAN &&
3286 		    vid != fltr_info->l_data.mac_vlan.vlan_id)
3287 			continue;
3288 
3289 		fltr_promisc_mask |= ice_determine_promisc_mask(fltr_info);
3290 
3291 		/* Skip if filter is not completely specified by given mask */
3292 		if (fltr_promisc_mask & ~promisc_mask)
3293 			continue;
3294 
3295 		status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3296 							&remove_list_head,
3297 							fltr_info);
3298 		if (status) {
3299 			ice_release_lock(rule_lock);
3300 			goto free_fltr_list;
3301 		}
3302 	}
3303 	ice_release_lock(rule_lock);
3304 
3305 	status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
3306 
3307 free_fltr_list:
3308 	LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3309 				 ice_fltr_list_entry, list_entry) {
3310 		LIST_DEL(&fm_entry->list_entry);
3311 		ice_free(hw, fm_entry);
3312 	}
3313 
3314 	return status;
3315 }
3316 
3317 /**
3318  * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
3319  * @hw: pointer to the hardware structure
3320  * @vsi_handle: VSI handle to clear mode
3321  * @promisc_mask: mask of promiscuous config bits to clear
3322  * @vid: VLAN ID to clear VLAN promiscuous
3323  */
3324 enum ice_status
3325 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle,
3326 		      u8 promisc_mask, u16 vid)
3327 {
3328 	return _ice_clear_vsi_promisc(hw, vsi_handle, promisc_mask,
3329 				      vid, hw->switch_info);
3330 }
3331 
3332 /**
3333  * _ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
3334  * @hw: pointer to the hardware structure
3335  * @vsi_handle: VSI handle to configure
3336  * @promisc_mask: mask of promiscuous config bits
3337  * @vid: VLAN ID to set VLAN promiscuous
3338  * @lport: logical port number to configure promisc mode
3339  * @sw: pointer to switch info struct for which function add rule
3340  */
3341 static enum ice_status
3342 _ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3343 		     u16 vid, u8 lport, struct ice_switch_info *sw)
3344 {
3345 	enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
3346 	struct ice_fltr_list_entry f_list_entry;
3347 	struct ice_fltr_info new_fltr;
3348 	enum ice_status status = ICE_SUCCESS;
3349 	bool is_tx_fltr;
3350 	u16 hw_vsi_id;
3351 	int pkt_type;
3352 	u8 recipe_id;
3353 
3354 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
3355 
3356 	if (!ice_is_vsi_valid(hw, vsi_handle))
3357 		return ICE_ERR_PARAM;
3358 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3359 
3360 	ice_memset(&new_fltr, 0, sizeof(new_fltr), ICE_NONDMA_MEM);
3361 
3362 	if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
3363 		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
3364 		new_fltr.l_data.mac_vlan.vlan_id = vid;
3365 		recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3366 	} else {
3367 		new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
3368 		recipe_id = ICE_SW_LKUP_PROMISC;
3369 	}
3370 
3371 	/* Separate filters must be set for each direction/packet type
3372 	 * combination, so we will loop over the mask value, store the
3373 	 * individual type, and clear it out in the input mask as it
3374 	 * is found.
3375 	 */
3376 	while (promisc_mask) {
3377 		struct ice_sw_recipe *recp_list;
3378 		u8 *mac_addr;
3379 
3380 		pkt_type = 0;
3381 		is_tx_fltr = false;
3382 
3383 		if (promisc_mask & ICE_PROMISC_UCAST_RX) {
3384 			promisc_mask &= ~ICE_PROMISC_UCAST_RX;
3385 			pkt_type = UCAST_FLTR;
3386 		} else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
3387 			promisc_mask &= ~ICE_PROMISC_UCAST_TX;
3388 			pkt_type = UCAST_FLTR;
3389 			is_tx_fltr = true;
3390 		} else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
3391 			promisc_mask &= ~ICE_PROMISC_MCAST_RX;
3392 			pkt_type = MCAST_FLTR;
3393 		} else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
3394 			promisc_mask &= ~ICE_PROMISC_MCAST_TX;
3395 			pkt_type = MCAST_FLTR;
3396 			is_tx_fltr = true;
3397 		} else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
3398 			promisc_mask &= ~ICE_PROMISC_BCAST_RX;
3399 			pkt_type = BCAST_FLTR;
3400 		} else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
3401 			promisc_mask &= ~ICE_PROMISC_BCAST_TX;
3402 			pkt_type = BCAST_FLTR;
3403 			is_tx_fltr = true;
3404 		}
3405 
3406 		/* Check for VLAN promiscuous flag */
3407 		if (promisc_mask & ICE_PROMISC_VLAN_RX) {
3408 			promisc_mask &= ~ICE_PROMISC_VLAN_RX;
3409 		} else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
3410 			promisc_mask &= ~ICE_PROMISC_VLAN_TX;
3411 			is_tx_fltr = true;
3412 		}
3413 
3414 		/* Set filter DA based on packet type */
3415 		mac_addr = new_fltr.l_data.mac.mac_addr;
3416 		if (pkt_type == BCAST_FLTR) {
3417 			ice_memset(mac_addr, 0xff, ETH_ALEN, ICE_NONDMA_MEM);
3418 		} else if (pkt_type == MCAST_FLTR ||
3419 			   pkt_type == UCAST_FLTR) {
3420 			/* Use the dummy ether header DA */
3421 			ice_memcpy(mac_addr, dummy_eth_header, ETH_ALEN,
3422 				   ICE_NONDMA_TO_NONDMA);
3423 			if (pkt_type == MCAST_FLTR)
3424 				mac_addr[0] |= 0x1;	/* Set multicast bit */
3425 		}
3426 
3427 		/* Need to reset this to zero for all iterations */
3428 		new_fltr.flag = 0;
3429 		if (is_tx_fltr) {
3430 			new_fltr.flag |= ICE_FLTR_TX;
3431 			new_fltr.src = hw_vsi_id;
3432 		} else {
3433 			new_fltr.flag |= ICE_FLTR_RX;
3434 			new_fltr.src = lport;
3435 		}
3436 
3437 		new_fltr.fltr_act = ICE_FWD_TO_VSI;
3438 		new_fltr.vsi_handle = vsi_handle;
3439 		new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
3440 		f_list_entry.fltr_info = new_fltr;
3441 		recp_list = &sw->recp_list[recipe_id];
3442 
3443 		status = ice_add_rule_internal(hw, recp_list, lport,
3444 					       &f_list_entry);
3445 		if (status != ICE_SUCCESS)
3446 			goto set_promisc_exit;
3447 	}
3448 
3449 set_promisc_exit:
3450 	return status;
3451 }
3452 
3453 /**
3454  * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
3455  * @hw: pointer to the hardware structure
3456  * @vsi_handle: VSI handle to configure
3457  * @promisc_mask: mask of promiscuous config bits
3458  * @vid: VLAN ID to set VLAN promiscuous
3459  */
3460 enum ice_status
3461 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3462 		    u16 vid)
3463 {
3464 	return _ice_set_vsi_promisc(hw, vsi_handle, promisc_mask, vid,
3465 				    hw->port_info->lport,
3466 				    hw->switch_info);
3467 }
3468 
3469 /**
3470  * _ice_set_vlan_vsi_promisc
3471  * @hw: pointer to the hardware structure
3472  * @vsi_handle: VSI handle to configure
3473  * @promisc_mask: mask of promiscuous config bits
3474  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
3475  * @lport: logical port number to configure promisc mode
3476  * @sw: pointer to switch info struct for which function add rule
3477  *
3478  * Configure VSI with all associated VLANs to given promiscuous mode(s)
3479  */
3480 static enum ice_status
3481 _ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3482 			  bool rm_vlan_promisc, u8 lport,
3483 			  struct ice_switch_info *sw)
3484 {
3485 	struct ice_fltr_list_entry *list_itr, *tmp;
3486 	struct LIST_HEAD_TYPE vsi_list_head;
3487 	struct LIST_HEAD_TYPE *vlan_head;
3488 	struct ice_lock *vlan_lock; /* Lock to protect filter rule list */
3489 	enum ice_status status;
3490 	u16 vlan_id;
3491 
3492 	INIT_LIST_HEAD(&vsi_list_head);
3493 	vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3494 	vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3495 	ice_acquire_lock(vlan_lock);
3496 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
3497 					  &vsi_list_head);
3498 	ice_release_lock(vlan_lock);
3499 	if (status)
3500 		goto free_fltr_list;
3501 
3502 	LIST_FOR_EACH_ENTRY(list_itr, &vsi_list_head, ice_fltr_list_entry,
3503 			    list_entry) {
3504 		vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
3505 		if (rm_vlan_promisc)
3506 			status =  _ice_clear_vsi_promisc(hw, vsi_handle,
3507 							 promisc_mask,
3508 							 vlan_id, sw);
3509 		else
3510 			status =  _ice_set_vsi_promisc(hw, vsi_handle,
3511 						       promisc_mask, vlan_id,
3512 						       lport, sw);
3513 		if (status)
3514 			break;
3515 	}
3516 
3517 free_fltr_list:
3518 	LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, &vsi_list_head,
3519 				 ice_fltr_list_entry, list_entry) {
3520 		LIST_DEL(&list_itr->list_entry);
3521 		ice_free(hw, list_itr);
3522 	}
3523 	return status;
3524 }
3525 
3526 /**
3527  * ice_set_vlan_vsi_promisc
3528  * @hw: pointer to the hardware structure
3529  * @vsi_handle: VSI handle to configure
3530  * @promisc_mask: mask of promiscuous config bits
3531  * @rm_vlan_promisc: Clear VLANs VSI promisc mode
3532  *
3533  * Configure VSI with all associated VLANs to given promiscuous mode(s)
3534  */
3535 enum ice_status
3536 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3537 			 bool rm_vlan_promisc)
3538 {
3539 	return _ice_set_vlan_vsi_promisc(hw, vsi_handle, promisc_mask,
3540 					 rm_vlan_promisc, hw->port_info->lport,
3541 					 hw->switch_info);
3542 }
3543 
3544 /**
3545  * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
3546  * @hw: pointer to the hardware structure
3547  * @vsi_handle: VSI handle to remove filters from
3548  * @recp_list: recipe list from which function remove fltr
3549  * @lkup: switch rule filter lookup type
3550  */
3551 static void
3552 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
3553 			 struct ice_sw_recipe *recp_list,
3554 			 enum ice_sw_lkup_type lkup)
3555 {
3556 	struct ice_fltr_list_entry *fm_entry;
3557 	struct LIST_HEAD_TYPE remove_list_head;
3558 	struct LIST_HEAD_TYPE *rule_head;
3559 	struct ice_fltr_list_entry *tmp;
3560 	struct ice_lock *rule_lock;	/* Lock to protect filter rule list */
3561 	enum ice_status status;
3562 
3563 	INIT_LIST_HEAD(&remove_list_head);
3564 	rule_lock = &recp_list[lkup].filt_rule_lock;
3565 	rule_head = &recp_list[lkup].filt_rules;
3566 	ice_acquire_lock(rule_lock);
3567 	status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
3568 					  &remove_list_head);
3569 	ice_release_lock(rule_lock);
3570 	if (status)
3571 		goto free_fltr_list;
3572 
3573 	switch (lkup) {
3574 	case ICE_SW_LKUP_MAC:
3575 		ice_remove_mac_rule(hw, &remove_list_head, &recp_list[lkup]);
3576 		break;
3577 	case ICE_SW_LKUP_VLAN:
3578 		ice_remove_vlan_rule(hw, &remove_list_head, &recp_list[lkup]);
3579 		break;
3580 	case ICE_SW_LKUP_PROMISC:
3581 	case ICE_SW_LKUP_PROMISC_VLAN:
3582 		ice_remove_promisc(hw, lkup, &remove_list_head);
3583 		break;
3584 	case ICE_SW_LKUP_MAC_VLAN:
3585 		ice_debug(hw, ICE_DBG_SW, "MAC VLAN look up is not supported yet\n");
3586 		break;
3587 	case ICE_SW_LKUP_ETHERTYPE:
3588 	case ICE_SW_LKUP_ETHERTYPE_MAC:
3589 		ice_remove_eth_mac(hw, &remove_list_head);
3590 		break;
3591 	case ICE_SW_LKUP_DFLT:
3592 		ice_debug(hw, ICE_DBG_SW, "Remove filters for this lookup type hasn't been implemented yet\n");
3593 		break;
3594 	case ICE_SW_LKUP_LAST:
3595 		ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type\n");
3596 		break;
3597 	}
3598 
3599 free_fltr_list:
3600 	LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3601 				 ice_fltr_list_entry, list_entry) {
3602 		LIST_DEL(&fm_entry->list_entry);
3603 		ice_free(hw, fm_entry);
3604 	}
3605 }
3606 
3607 /**
3608  * ice_remove_vsi_fltr_rule - Remove all filters for a VSI
3609  * @hw: pointer to the hardware structure
3610  * @vsi_handle: VSI handle to remove filters from
3611  * @sw: pointer to switch info struct
3612  */
3613 static void
3614 ice_remove_vsi_fltr_rule(struct ice_hw *hw, u16 vsi_handle,
3615 			 struct ice_switch_info *sw)
3616 {
3617 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
3618 
3619 	ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3620 				 sw->recp_list, ICE_SW_LKUP_MAC);
3621 	ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3622 				 sw->recp_list, ICE_SW_LKUP_MAC_VLAN);
3623 	ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3624 				 sw->recp_list, ICE_SW_LKUP_PROMISC);
3625 	ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3626 				 sw->recp_list, ICE_SW_LKUP_VLAN);
3627 	ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3628 				 sw->recp_list, ICE_SW_LKUP_DFLT);
3629 	ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3630 				 sw->recp_list, ICE_SW_LKUP_ETHERTYPE);
3631 	ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3632 				 sw->recp_list, ICE_SW_LKUP_ETHERTYPE_MAC);
3633 	ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3634 				 sw->recp_list, ICE_SW_LKUP_PROMISC_VLAN);
3635 }
3636 
3637 /**
3638  * ice_remove_vsi_fltr - Remove all filters for a VSI
3639  * @hw: pointer to the hardware structure
3640  * @vsi_handle: VSI handle to remove filters from
3641  */
3642 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
3643 {
3644 	ice_remove_vsi_fltr_rule(hw, vsi_handle, hw->switch_info);
3645 }
3646 
3647 /**
3648  * ice_alloc_res_cntr - allocating resource counter
3649  * @hw: pointer to the hardware structure
3650  * @type: type of resource
3651  * @alloc_shared: if set it is shared else dedicated
3652  * @num_items: number of entries requested for FD resource type
3653  * @counter_id: counter index returned by AQ call
3654  */
3655 static enum ice_status
3656 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3657 		   u16 *counter_id)
3658 {
3659 	struct ice_aqc_alloc_free_res_elem *buf;
3660 	enum ice_status status;
3661 	u16 buf_len;
3662 
3663 	/* Allocate resource */
3664 	buf_len = ice_struct_size(buf, elem, 1);
3665 	buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
3666 	if (!buf)
3667 		return ICE_ERR_NO_MEMORY;
3668 
3669 	buf->num_elems = CPU_TO_LE16(num_items);
3670 	buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3671 				      ICE_AQC_RES_TYPE_M) | alloc_shared);
3672 
3673 	status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3674 				       ice_aqc_opc_alloc_res, NULL);
3675 	if (status)
3676 		goto exit;
3677 
3678 	*counter_id = LE16_TO_CPU(buf->elem[0].e.sw_resp);
3679 
3680 exit:
3681 	ice_free(hw, buf);
3682 	return status;
3683 }
3684 
3685 /**
3686  * ice_free_res_cntr - free resource counter
3687  * @hw: pointer to the hardware structure
3688  * @type: type of resource
3689  * @alloc_shared: if set it is shared else dedicated
3690  * @num_items: number of entries to be freed for FD resource type
3691  * @counter_id: counter ID resource which needs to be freed
3692  */
3693 static enum ice_status
3694 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3695 		  u16 counter_id)
3696 {
3697 	struct ice_aqc_alloc_free_res_elem *buf;
3698 	enum ice_status status;
3699 	u16 buf_len;
3700 
3701 	/* Free resource */
3702 	buf_len = ice_struct_size(buf, elem, 1);
3703 	buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
3704 	if (!buf)
3705 		return ICE_ERR_NO_MEMORY;
3706 
3707 	buf->num_elems = CPU_TO_LE16(num_items);
3708 	buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3709 				      ICE_AQC_RES_TYPE_M) | alloc_shared);
3710 	buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
3711 
3712 	status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3713 				       ice_aqc_opc_free_res, NULL);
3714 	if (status)
3715 		ice_debug(hw, ICE_DBG_SW, "counter resource could not be freed\n");
3716 
3717 	ice_free(hw, buf);
3718 	return status;
3719 }
3720 
3721 /**
3722  * ice_alloc_vlan_res_counter - obtain counter resource for VLAN type
3723  * @hw: pointer to the hardware structure
3724  * @counter_id: returns counter index
3725  */
3726 enum ice_status ice_alloc_vlan_res_counter(struct ice_hw *hw, u16 *counter_id)
3727 {
3728 	return ice_alloc_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3729 				  ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3730 				  counter_id);
3731 }
3732 
3733 /**
3734  * ice_free_vlan_res_counter - Free counter resource for VLAN type
3735  * @hw: pointer to the hardware structure
3736  * @counter_id: counter index to be freed
3737  */
3738 enum ice_status ice_free_vlan_res_counter(struct ice_hw *hw, u16 counter_id)
3739 {
3740 	return ice_free_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3741 				 ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3742 				 counter_id);
3743 }
3744 
3745 /**
3746  * ice_alloc_res_lg_act - add large action resource
3747  * @hw: pointer to the hardware structure
3748  * @l_id: large action ID to fill it in
3749  * @num_acts: number of actions to hold with a large action entry
3750  */
3751 static enum ice_status
3752 ice_alloc_res_lg_act(struct ice_hw *hw, u16 *l_id, u16 num_acts)
3753 {
3754 	struct ice_aqc_alloc_free_res_elem *sw_buf;
3755 	enum ice_status status;
3756 	u16 buf_len;
3757 
3758 	if (num_acts > ICE_MAX_LG_ACT || num_acts == 0)
3759 		return ICE_ERR_PARAM;
3760 
3761 	/* Allocate resource for large action */
3762 	buf_len = ice_struct_size(sw_buf, elem, 1);
3763 	sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
3764 	if (!sw_buf)
3765 		return ICE_ERR_NO_MEMORY;
3766 
3767 	sw_buf->num_elems = CPU_TO_LE16(1);
3768 
3769 	/* If num_acts is 1, use ICE_AQC_RES_TYPE_WIDE_TABLE_1.
3770 	 * If num_acts is 2, use ICE_AQC_RES_TYPE_WIDE_TABLE_3.
3771 	 * If num_acts is greater than 2, then use
3772 	 * ICE_AQC_RES_TYPE_WIDE_TABLE_4.
3773 	 * The num_acts cannot exceed 4. This was ensured at the
3774 	 * beginning of the function.
3775 	 */
3776 	if (num_acts == 1)
3777 		sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_1);
3778 	else if (num_acts == 2)
3779 		sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_2);
3780 	else
3781 		sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_4);
3782 
3783 	status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
3784 				       ice_aqc_opc_alloc_res, NULL);
3785 	if (!status)
3786 		*l_id = LE16_TO_CPU(sw_buf->elem[0].e.sw_resp);
3787 
3788 	ice_free(hw, sw_buf);
3789 	return status;
3790 }
3791 
3792 /**
3793  * ice_add_mac_with_sw_marker - add filter with sw marker
3794  * @hw: pointer to the hardware structure
3795  * @f_info: filter info structure containing the MAC filter information
3796  * @sw_marker: sw marker to tag the Rx descriptor with
3797  */
3798 enum ice_status
3799 ice_add_mac_with_sw_marker(struct ice_hw *hw, struct ice_fltr_info *f_info,
3800 			   u16 sw_marker)
3801 {
3802 	struct ice_fltr_mgmt_list_entry *m_entry;
3803 	struct ice_fltr_list_entry fl_info;
3804 	struct ice_sw_recipe *recp_list;
3805 	struct LIST_HEAD_TYPE l_head;
3806 	struct ice_lock *rule_lock;	/* Lock to protect filter rule list */
3807 	enum ice_status ret;
3808 	bool entry_exists;
3809 	u16 lg_act_id;
3810 
3811 	if (f_info->fltr_act != ICE_FWD_TO_VSI)
3812 		return ICE_ERR_PARAM;
3813 
3814 	if (f_info->lkup_type != ICE_SW_LKUP_MAC)
3815 		return ICE_ERR_PARAM;
3816 
3817 	if (sw_marker == ICE_INVAL_SW_MARKER_ID)
3818 		return ICE_ERR_PARAM;
3819 
3820 	if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
3821 		return ICE_ERR_PARAM;
3822 	f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
3823 
3824 	/* Add filter if it doesn't exist so then the adding of large
3825 	 * action always results in update
3826 	 */
3827 
3828 	INIT_LIST_HEAD(&l_head);
3829 	fl_info.fltr_info = *f_info;
3830 	LIST_ADD(&fl_info.list_entry, &l_head);
3831 
3832 	entry_exists = false;
3833 	ret = ice_add_mac_rule(hw, &l_head, hw->switch_info,
3834 			       hw->port_info->lport);
3835 	if (ret == ICE_ERR_ALREADY_EXISTS)
3836 		entry_exists = true;
3837 	else if (ret)
3838 		return ret;
3839 
3840 	recp_list = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC];
3841 	rule_lock = &recp_list->filt_rule_lock;
3842 	ice_acquire_lock(rule_lock);
3843 	/* Get the book keeping entry for the filter */
3844 	m_entry = ice_find_rule_entry(&recp_list->filt_rules, f_info);
3845 	if (!m_entry)
3846 		goto exit_error;
3847 
3848 	/* If counter action was enabled for this rule then don't enable
3849 	 * sw marker large action
3850 	 */
3851 	if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
3852 		ret = ICE_ERR_PARAM;
3853 		goto exit_error;
3854 	}
3855 
3856 	/* if same marker was added before */
3857 	if (m_entry->sw_marker_id == sw_marker) {
3858 		ret = ICE_ERR_ALREADY_EXISTS;
3859 		goto exit_error;
3860 	}
3861 
3862 	/* Allocate a hardware table entry to hold large act. Three actions
3863 	 * for marker based large action
3864 	 */
3865 	ret = ice_alloc_res_lg_act(hw, &lg_act_id, 3);
3866 	if (ret)
3867 		goto exit_error;
3868 
3869 	if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
3870 		goto exit_error;
3871 
3872 	/* Update the switch rule to add the marker action */
3873 	ret = ice_add_marker_act(hw, m_entry, sw_marker, lg_act_id);
3874 	if (!ret) {
3875 		ice_release_lock(rule_lock);
3876 		return ret;
3877 	}
3878 
3879 exit_error:
3880 	ice_release_lock(rule_lock);
3881 	/* only remove entry if it did not exist previously */
3882 	if (!entry_exists)
3883 		ret = ice_remove_mac(hw, &l_head);
3884 
3885 	return ret;
3886 }
3887 
3888 /**
3889  * ice_add_mac_with_counter - add filter with counter enabled
3890  * @hw: pointer to the hardware structure
3891  * @f_info: pointer to filter info structure containing the MAC filter
3892  *          information
3893  */
3894 enum ice_status
3895 ice_add_mac_with_counter(struct ice_hw *hw, struct ice_fltr_info *f_info)
3896 {
3897 	struct ice_fltr_mgmt_list_entry *m_entry;
3898 	struct ice_fltr_list_entry fl_info;
3899 	struct ice_sw_recipe *recp_list;
3900 	struct LIST_HEAD_TYPE l_head;
3901 	struct ice_lock *rule_lock;	/* Lock to protect filter rule list */
3902 	enum ice_status ret;
3903 	bool entry_exist;
3904 	u16 counter_id;
3905 	u16 lg_act_id;
3906 
3907 	if (f_info->fltr_act != ICE_FWD_TO_VSI)
3908 		return ICE_ERR_PARAM;
3909 
3910 	if (f_info->lkup_type != ICE_SW_LKUP_MAC)
3911 		return ICE_ERR_PARAM;
3912 
3913 	if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
3914 		return ICE_ERR_PARAM;
3915 	f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
3916 	recp_list = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC];
3917 
3918 	entry_exist = false;
3919 
3920 	rule_lock = &recp_list->filt_rule_lock;
3921 
3922 	/* Add filter if it doesn't exist so then the adding of large
3923 	 * action always results in update
3924 	 */
3925 	INIT_LIST_HEAD(&l_head);
3926 
3927 	fl_info.fltr_info = *f_info;
3928 	LIST_ADD(&fl_info.list_entry, &l_head);
3929 
3930 	ret = ice_add_mac_rule(hw, &l_head, hw->switch_info,
3931 			       hw->port_info->lport);
3932 	if (ret == ICE_ERR_ALREADY_EXISTS)
3933 		entry_exist = true;
3934 	else if (ret)
3935 		return ret;
3936 
3937 	ice_acquire_lock(rule_lock);
3938 	m_entry = ice_find_rule_entry(&recp_list->filt_rules, f_info);
3939 	if (!m_entry) {
3940 		ret = ICE_ERR_BAD_PTR;
3941 		goto exit_error;
3942 	}
3943 
3944 	/* Don't enable counter for a filter for which sw marker was enabled */
3945 	if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID) {
3946 		ret = ICE_ERR_PARAM;
3947 		goto exit_error;
3948 	}
3949 
3950 	/* If a counter was already enabled then don't need to add again */
3951 	if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
3952 		ret = ICE_ERR_ALREADY_EXISTS;
3953 		goto exit_error;
3954 	}
3955 
3956 	/* Allocate a hardware table entry to VLAN counter */
3957 	ret = ice_alloc_vlan_res_counter(hw, &counter_id);
3958 	if (ret)
3959 		goto exit_error;
3960 
3961 	/* Allocate a hardware table entry to hold large act. Two actions for
3962 	 * counter based large action
3963 	 */
3964 	ret = ice_alloc_res_lg_act(hw, &lg_act_id, 2);
3965 	if (ret)
3966 		goto exit_error;
3967 
3968 	if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
3969 		goto exit_error;
3970 
3971 	/* Update the switch rule to add the counter action */
3972 	ret = ice_add_counter_act(hw, m_entry, counter_id, lg_act_id);
3973 	if (!ret) {
3974 		ice_release_lock(rule_lock);
3975 		return ret;
3976 	}
3977 
3978 exit_error:
3979 	ice_release_lock(rule_lock);
3980 	/* only remove entry if it did not exist previously */
3981 	if (!entry_exist)
3982 		ret = ice_remove_mac(hw, &l_head);
3983 
3984 	return ret;
3985 }
3986 
3987 /**
3988  * ice_replay_fltr - Replay all the filters stored by a specific list head
3989  * @hw: pointer to the hardware structure
3990  * @list_head: list for which filters needs to be replayed
3991  * @recp_id: Recipe ID for which rules need to be replayed
3992  */
3993 static enum ice_status
3994 ice_replay_fltr(struct ice_hw *hw, u8 recp_id, struct LIST_HEAD_TYPE *list_head)
3995 {
3996 	struct ice_fltr_mgmt_list_entry *itr;
3997 	enum ice_status status = ICE_SUCCESS;
3998 	struct ice_sw_recipe *recp_list;
3999 	u8 lport = hw->port_info->lport;
4000 	struct LIST_HEAD_TYPE l_head;
4001 
4002 	if (LIST_EMPTY(list_head))
4003 		return status;
4004 
4005 	recp_list = &hw->switch_info->recp_list[recp_id];
4006 	/* Move entries from the given list_head to a temporary l_head so that
4007 	 * they can be replayed. Otherwise when trying to re-add the same
4008 	 * filter, the function will return already exists
4009 	 */
4010 	LIST_REPLACE_INIT(list_head, &l_head);
4011 
4012 	/* Mark the given list_head empty by reinitializing it so filters
4013 	 * could be added again by *handler
4014 	 */
4015 	LIST_FOR_EACH_ENTRY(itr, &l_head, ice_fltr_mgmt_list_entry,
4016 			    list_entry) {
4017 		struct ice_fltr_list_entry f_entry;
4018 		u16 vsi_handle;
4019 
4020 		f_entry.fltr_info = itr->fltr_info;
4021 		if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN) {
4022 			status = ice_add_rule_internal(hw, recp_list, lport,
4023 						       &f_entry);
4024 			if (status != ICE_SUCCESS)
4025 				goto end;
4026 			continue;
4027 		}
4028 
4029 		/* Add a filter per VSI separately */
4030 		ice_for_each_set_bit(vsi_handle, itr->vsi_list_info->vsi_map,
4031 				     ICE_MAX_VSI) {
4032 			if (!ice_is_vsi_valid(hw, vsi_handle))
4033 				break;
4034 
4035 			ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
4036 			f_entry.fltr_info.vsi_handle = vsi_handle;
4037 			f_entry.fltr_info.fwd_id.hw_vsi_id =
4038 				ice_get_hw_vsi_num(hw, vsi_handle);
4039 			f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
4040 			if (recp_id == ICE_SW_LKUP_VLAN)
4041 				status = ice_add_vlan_internal(hw, recp_list,
4042 							       &f_entry);
4043 			else
4044 				status = ice_add_rule_internal(hw, recp_list,
4045 							       lport,
4046 							       &f_entry);
4047 			if (status != ICE_SUCCESS)
4048 				goto end;
4049 		}
4050 	}
4051 end:
4052 	/* Clear the filter management list */
4053 	ice_rem_sw_rule_info(hw, &l_head);
4054 	return status;
4055 }
4056 
4057 /**
4058  * ice_replay_all_fltr - replay all filters stored in bookkeeping lists
4059  * @hw: pointer to the hardware structure
4060  *
4061  * NOTE: This function does not clean up partially added filters on error.
4062  * It is up to caller of the function to issue a reset or fail early.
4063  */
4064 enum ice_status ice_replay_all_fltr(struct ice_hw *hw)
4065 {
4066 	struct ice_switch_info *sw = hw->switch_info;
4067 	enum ice_status status = ICE_SUCCESS;
4068 	u8 i;
4069 
4070 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4071 		struct LIST_HEAD_TYPE *head = &sw->recp_list[i].filt_rules;
4072 
4073 		status = ice_replay_fltr(hw, i, head);
4074 		if (status != ICE_SUCCESS)
4075 			return status;
4076 	}
4077 	return status;
4078 }
4079 
4080 /**
4081  * ice_replay_vsi_fltr - Replay filters for requested VSI
4082  * @hw: pointer to the hardware structure
4083  * @pi: pointer to port information structure
4084  * @sw: pointer to switch info struct for which function replays filters
4085  * @vsi_handle: driver VSI handle
4086  * @recp_id: Recipe ID for which rules need to be replayed
4087  * @list_head: list for which filters need to be replayed
4088  *
4089  * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
4090  * It is required to pass valid VSI handle.
4091  */
4092 static enum ice_status
4093 ice_replay_vsi_fltr(struct ice_hw *hw, struct ice_port_info *pi,
4094 		    struct ice_switch_info *sw, u16 vsi_handle, u8 recp_id,
4095 		    struct LIST_HEAD_TYPE *list_head)
4096 {
4097 	struct ice_fltr_mgmt_list_entry *itr;
4098 	enum ice_status status = ICE_SUCCESS;
4099 	struct ice_sw_recipe *recp_list;
4100 	u16 hw_vsi_id;
4101 
4102 	if (LIST_EMPTY(list_head))
4103 		return status;
4104 	recp_list = &sw->recp_list[recp_id];
4105 	hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
4106 
4107 	LIST_FOR_EACH_ENTRY(itr, list_head, ice_fltr_mgmt_list_entry,
4108 			    list_entry) {
4109 		struct ice_fltr_list_entry f_entry;
4110 
4111 		f_entry.fltr_info = itr->fltr_info;
4112 		if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
4113 		    itr->fltr_info.vsi_handle == vsi_handle) {
4114 			/* update the src in case it is VSI num */
4115 			if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
4116 				f_entry.fltr_info.src = hw_vsi_id;
4117 			status = ice_add_rule_internal(hw, recp_list,
4118 						       pi->lport,
4119 						       &f_entry);
4120 			if (status != ICE_SUCCESS)
4121 				goto end;
4122 			continue;
4123 		}
4124 		if (!itr->vsi_list_info ||
4125 		    !ice_is_bit_set(itr->vsi_list_info->vsi_map, vsi_handle))
4126 			continue;
4127 		/* Clearing it so that the logic can add it back */
4128 		ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
4129 		f_entry.fltr_info.vsi_handle = vsi_handle;
4130 		f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
4131 		/* update the src in case it is VSI num */
4132 		if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
4133 			f_entry.fltr_info.src = hw_vsi_id;
4134 		if (recp_id == ICE_SW_LKUP_VLAN)
4135 			status = ice_add_vlan_internal(hw, recp_list, &f_entry);
4136 		else
4137 			status = ice_add_rule_internal(hw, recp_list,
4138 						       pi->lport,
4139 						       &f_entry);
4140 		if (status != ICE_SUCCESS)
4141 			goto end;
4142 	}
4143 end:
4144 	return status;
4145 }
4146 
4147 /**
4148  * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
4149  * @hw: pointer to the hardware structure
4150  * @pi: pointer to port information structure
4151  * @vsi_handle: driver VSI handle
4152  *
4153  * Replays filters for requested VSI via vsi_handle.
4154  */
4155 enum ice_status
4156 ice_replay_vsi_all_fltr(struct ice_hw *hw, struct ice_port_info *pi,
4157 			u16 vsi_handle)
4158 {
4159 	struct ice_switch_info *sw = hw->switch_info;
4160 	enum ice_status status = ICE_SUCCESS;
4161 	u8 i;
4162 
4163 	/* Update the recipes that were created */
4164 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4165 		struct LIST_HEAD_TYPE *head;
4166 
4167 		head = &sw->recp_list[i].filt_replay_rules;
4168 		if (!sw->recp_list[i].adv_rule)
4169 			status = ice_replay_vsi_fltr(hw, pi, sw, vsi_handle, i,
4170 						     head);
4171 		if (status != ICE_SUCCESS)
4172 			return status;
4173 	}
4174 
4175 	return ICE_SUCCESS;
4176 }
4177 
4178 /**
4179  * ice_rm_sw_replay_rule_info - helper function to delete filter replay rules
4180  * @hw: pointer to the HW struct
4181  * @sw: pointer to switch info struct for which function removes filters
4182  *
4183  * Deletes the filter replay rules for given switch
4184  */
4185 void ice_rm_sw_replay_rule_info(struct ice_hw *hw, struct ice_switch_info *sw)
4186 {
4187 	u8 i;
4188 
4189 	if (!sw)
4190 		return;
4191 
4192 	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4193 		if (!LIST_EMPTY(&sw->recp_list[i].filt_replay_rules)) {
4194 			struct LIST_HEAD_TYPE *l_head;
4195 
4196 			l_head = &sw->recp_list[i].filt_replay_rules;
4197 			if (!sw->recp_list[i].adv_rule)
4198 				ice_rem_sw_rule_info(hw, l_head);
4199 		}
4200 	}
4201 }
4202 
4203 /**
4204  * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
4205  * @hw: pointer to the HW struct
4206  *
4207  * Deletes the filter replay rules.
4208  */
4209 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
4210 {
4211 	ice_rm_sw_replay_rule_info(hw, hw->switch_info);
4212 }
4213 
4214