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