1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright (c) 2020, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
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.
33 #include "ice_switch.h"
34 #include "ice_flex_type.h"
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
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) }
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
57 static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
62 * ice_init_def_sw_recp - initialize the recipe book keeping tables
63 * @hw: pointer to the HW struct
64 * @recp_list: pointer to sw recipe list
66 * Allocate memory for the entire recipe table and initialize the structures/
67 * entries corresponding to basic recipes.
70 ice_init_def_sw_recp(struct ice_hw *hw, struct ice_sw_recipe **recp_list)
72 struct ice_sw_recipe *recps;
75 recps = (struct ice_sw_recipe *)
76 ice_calloc(hw, ICE_MAX_NUM_RECIPES, sizeof(*recps));
78 return ICE_ERR_NO_MEMORY;
80 for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
81 recps[i].root_rid = i;
82 INIT_LIST_HEAD(&recps[i].filt_rules);
83 INIT_LIST_HEAD(&recps[i].filt_replay_rules);
84 INIT_LIST_HEAD(&recps[i].rg_list);
85 ice_init_lock(&recps[i].filt_rule_lock);
94 * ice_aq_get_sw_cfg - get switch configuration
95 * @hw: pointer to the hardware structure
96 * @buf: pointer to the result buffer
97 * @buf_size: length of the buffer available for response
98 * @req_desc: pointer to requested descriptor
99 * @num_elems: pointer to number of elements
100 * @cd: pointer to command details structure or NULL
102 * Get switch configuration (0x0200) to be placed in buf.
103 * This admin command returns information such as initial VSI/port number
104 * and switch ID it belongs to.
106 * NOTE: *req_desc is both an input/output parameter.
107 * The caller of this function first calls this function with *request_desc set
108 * to 0. If the response from f/w has *req_desc set to 0, all the switch
109 * configuration information has been returned; if non-zero (meaning not all
110 * the information was returned), the caller should call this function again
111 * with *req_desc set to the previous value returned by f/w to get the
112 * next block of switch configuration information.
114 * *num_elems is output only parameter. This reflects the number of elements
115 * in response buffer. The caller of this function to use *num_elems while
116 * parsing the response buffer.
118 static enum ice_status
119 ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp_elem *buf,
120 u16 buf_size, u16 *req_desc, u16 *num_elems,
121 struct ice_sq_cd *cd)
123 struct ice_aqc_get_sw_cfg *cmd;
124 struct ice_aq_desc desc;
125 enum ice_status status;
127 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
128 cmd = &desc.params.get_sw_conf;
129 cmd->element = CPU_TO_LE16(*req_desc);
131 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
133 *req_desc = LE16_TO_CPU(cmd->element);
134 *num_elems = LE16_TO_CPU(cmd->num_elems);
141 * ice_alloc_sw - allocate resources specific to switch
142 * @hw: pointer to the HW struct
143 * @ena_stats: true to turn on VEB stats
144 * @shared_res: true for shared resource, false for dedicated resource
145 * @sw_id: switch ID returned
146 * @counter_id: VEB counter ID returned
148 * allocates switch resources (SWID and VEB counter) (0x0208)
151 ice_alloc_sw(struct ice_hw *hw, bool ena_stats, bool shared_res, u16 *sw_id,
154 struct ice_aqc_alloc_free_res_elem *sw_buf;
155 struct ice_aqc_res_elem *sw_ele;
156 enum ice_status status;
159 buf_len = ice_struct_size(sw_buf, elem, 1);
160 sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
162 return ICE_ERR_NO_MEMORY;
164 /* Prepare buffer for switch ID.
165 * The number of resource entries in buffer is passed as 1 since only a
166 * single switch/VEB instance is allocated, and hence a single sw_id
169 sw_buf->num_elems = CPU_TO_LE16(1);
171 CPU_TO_LE16(ICE_AQC_RES_TYPE_SWID |
172 (shared_res ? ICE_AQC_RES_TYPE_FLAG_SHARED :
173 ICE_AQC_RES_TYPE_FLAG_DEDICATED));
175 status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
176 ice_aqc_opc_alloc_res, NULL);
179 goto ice_alloc_sw_exit;
181 sw_ele = &sw_buf->elem[0];
182 *sw_id = LE16_TO_CPU(sw_ele->e.sw_resp);
185 /* Prepare buffer for VEB Counter */
186 enum ice_adminq_opc opc = ice_aqc_opc_alloc_res;
187 struct ice_aqc_alloc_free_res_elem *counter_buf;
188 struct ice_aqc_res_elem *counter_ele;
190 counter_buf = (struct ice_aqc_alloc_free_res_elem *)
191 ice_malloc(hw, buf_len);
193 status = ICE_ERR_NO_MEMORY;
194 goto ice_alloc_sw_exit;
197 /* The number of resource entries in buffer is passed as 1 since
198 * only a single switch/VEB instance is allocated, and hence a
199 * single VEB counter is requested.
201 counter_buf->num_elems = CPU_TO_LE16(1);
202 counter_buf->res_type =
203 CPU_TO_LE16(ICE_AQC_RES_TYPE_VEB_COUNTER |
204 ICE_AQC_RES_TYPE_FLAG_DEDICATED);
205 status = ice_aq_alloc_free_res(hw, 1, counter_buf, buf_len,
209 ice_free(hw, counter_buf);
210 goto ice_alloc_sw_exit;
212 counter_ele = &counter_buf->elem[0];
213 *counter_id = LE16_TO_CPU(counter_ele->e.sw_resp);
214 ice_free(hw, counter_buf);
218 ice_free(hw, sw_buf);
223 * ice_free_sw - free resources specific to switch
224 * @hw: pointer to the HW struct
225 * @sw_id: switch ID returned
226 * @counter_id: VEB counter ID returned
228 * free switch resources (SWID and VEB counter) (0x0209)
230 * NOTE: This function frees multiple resources. It continues
231 * releasing other resources even after it encounters error.
232 * The error code returned is the last error it encountered.
234 enum ice_status ice_free_sw(struct ice_hw *hw, u16 sw_id, u16 counter_id)
236 struct ice_aqc_alloc_free_res_elem *sw_buf, *counter_buf;
237 enum ice_status status, ret_status;
240 buf_len = ice_struct_size(sw_buf, elem, 1);
241 sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
243 return ICE_ERR_NO_MEMORY;
245 /* Prepare buffer to free for switch ID res.
246 * The number of resource entries in buffer is passed as 1 since only a
247 * single switch/VEB instance is freed, and hence a single sw_id
250 sw_buf->num_elems = CPU_TO_LE16(1);
251 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_SWID);
252 sw_buf->elem[0].e.sw_resp = CPU_TO_LE16(sw_id);
254 ret_status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
255 ice_aqc_opc_free_res, NULL);
258 ice_debug(hw, ICE_DBG_SW, "CQ CMD Buffer:\n");
260 /* Prepare buffer to free for VEB Counter resource */
261 counter_buf = (struct ice_aqc_alloc_free_res_elem *)
262 ice_malloc(hw, buf_len);
264 ice_free(hw, sw_buf);
265 return ICE_ERR_NO_MEMORY;
268 /* The number of resource entries in buffer is passed as 1 since only a
269 * single switch/VEB instance is freed, and hence a single VEB counter
272 counter_buf->num_elems = CPU_TO_LE16(1);
273 counter_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_VEB_COUNTER);
274 counter_buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
276 status = ice_aq_alloc_free_res(hw, 1, counter_buf, buf_len,
277 ice_aqc_opc_free_res, NULL);
279 ice_debug(hw, ICE_DBG_SW, "VEB counter resource could not be freed\n");
283 ice_free(hw, counter_buf);
284 ice_free(hw, sw_buf);
290 * @hw: pointer to the HW struct
291 * @vsi_ctx: pointer to a VSI context struct
292 * @cd: pointer to command details structure or NULL
294 * Add a VSI context to the hardware (0x0210)
297 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
298 struct ice_sq_cd *cd)
300 struct ice_aqc_add_update_free_vsi_resp *res;
301 struct ice_aqc_add_get_update_free_vsi *cmd;
302 struct ice_aq_desc desc;
303 enum ice_status status;
305 cmd = &desc.params.vsi_cmd;
306 res = &desc.params.add_update_free_vsi_res;
308 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
310 if (!vsi_ctx->alloc_from_pool)
311 cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num |
312 ICE_AQ_VSI_IS_VALID);
313 cmd->vf_id = vsi_ctx->vf_num;
315 cmd->vsi_flags = CPU_TO_LE16(vsi_ctx->flags);
317 desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
319 status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
320 sizeof(vsi_ctx->info), cd);
323 vsi_ctx->vsi_num = LE16_TO_CPU(res->vsi_num) & ICE_AQ_VSI_NUM_M;
324 vsi_ctx->vsis_allocd = LE16_TO_CPU(res->vsi_used);
325 vsi_ctx->vsis_unallocated = LE16_TO_CPU(res->vsi_free);
333 * @hw: pointer to the HW struct
334 * @vsi_ctx: pointer to a VSI context struct
335 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
336 * @cd: pointer to command details structure or NULL
338 * Free VSI context info from hardware (0x0213)
341 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
342 bool keep_vsi_alloc, struct ice_sq_cd *cd)
344 struct ice_aqc_add_update_free_vsi_resp *resp;
345 struct ice_aqc_add_get_update_free_vsi *cmd;
346 struct ice_aq_desc desc;
347 enum ice_status status;
349 cmd = &desc.params.vsi_cmd;
350 resp = &desc.params.add_update_free_vsi_res;
352 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
354 cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
356 cmd->cmd_flags = CPU_TO_LE16(ICE_AQ_VSI_KEEP_ALLOC);
358 status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
360 vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
361 vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
369 * @hw: pointer to the HW struct
370 * @vsi_ctx: pointer to a VSI context struct
371 * @cd: pointer to command details structure or NULL
373 * Update VSI context in the hardware (0x0211)
376 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
377 struct ice_sq_cd *cd)
379 struct ice_aqc_add_update_free_vsi_resp *resp;
380 struct ice_aqc_add_get_update_free_vsi *cmd;
381 struct ice_aq_desc desc;
382 enum ice_status status;
384 cmd = &desc.params.vsi_cmd;
385 resp = &desc.params.add_update_free_vsi_res;
387 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
389 cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
391 desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
393 status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
394 sizeof(vsi_ctx->info), cd);
397 vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
398 vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
405 * ice_is_vsi_valid - check whether the VSI is valid or not
406 * @hw: pointer to the HW struct
407 * @vsi_handle: VSI handle
409 * check whether the VSI is valid or not
411 bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
413 return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
417 * ice_get_hw_vsi_num - return the HW VSI number
418 * @hw: pointer to the HW struct
419 * @vsi_handle: VSI handle
421 * return the HW VSI number
422 * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
424 u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
426 return hw->vsi_ctx[vsi_handle]->vsi_num;
430 * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
431 * @hw: pointer to the HW struct
432 * @vsi_handle: VSI handle
434 * return the VSI context entry for a given VSI handle
436 struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
438 return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
442 * ice_save_vsi_ctx - save the VSI context for a given VSI handle
443 * @hw: pointer to the HW struct
444 * @vsi_handle: VSI handle
445 * @vsi: VSI context pointer
447 * save the VSI context entry for a given VSI handle
450 ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi)
452 hw->vsi_ctx[vsi_handle] = vsi;
456 * ice_clear_vsi_q_ctx - clear VSI queue contexts for all TCs
457 * @hw: pointer to the HW struct
458 * @vsi_handle: VSI handle
460 static void ice_clear_vsi_q_ctx(struct ice_hw *hw, u16 vsi_handle)
462 struct ice_vsi_ctx *vsi;
465 vsi = ice_get_vsi_ctx(hw, vsi_handle);
468 ice_for_each_traffic_class(i) {
469 if (vsi->lan_q_ctx[i]) {
470 ice_free(hw, vsi->lan_q_ctx[i]);
471 vsi->lan_q_ctx[i] = NULL;
477 * ice_clear_vsi_ctx - clear the VSI context entry
478 * @hw: pointer to the HW struct
479 * @vsi_handle: VSI handle
481 * clear the VSI context entry
483 static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
485 struct ice_vsi_ctx *vsi;
487 vsi = ice_get_vsi_ctx(hw, vsi_handle);
489 ice_clear_vsi_q_ctx(hw, vsi_handle);
491 hw->vsi_ctx[vsi_handle] = NULL;
496 * ice_clear_all_vsi_ctx - clear all the VSI context entries
497 * @hw: pointer to the HW struct
499 void ice_clear_all_vsi_ctx(struct ice_hw *hw)
503 for (i = 0; i < ICE_MAX_VSI; i++)
504 ice_clear_vsi_ctx(hw, i);
508 * ice_add_vsi - add VSI context to the hardware and VSI handle list
509 * @hw: pointer to the HW struct
510 * @vsi_handle: unique VSI handle provided by drivers
511 * @vsi_ctx: pointer to a VSI context struct
512 * @cd: pointer to command details structure or NULL
514 * Add a VSI context to the hardware also add it into the VSI handle list.
515 * If this function gets called after reset for existing VSIs then update
516 * with the new HW VSI number in the corresponding VSI handle list entry.
519 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
520 struct ice_sq_cd *cd)
522 struct ice_vsi_ctx *tmp_vsi_ctx;
523 enum ice_status status;
525 if (vsi_handle >= ICE_MAX_VSI)
526 return ICE_ERR_PARAM;
527 status = ice_aq_add_vsi(hw, vsi_ctx, cd);
530 tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
532 /* Create a new VSI context */
533 tmp_vsi_ctx = (struct ice_vsi_ctx *)
534 ice_malloc(hw, sizeof(*tmp_vsi_ctx));
536 ice_aq_free_vsi(hw, vsi_ctx, false, cd);
537 return ICE_ERR_NO_MEMORY;
539 *tmp_vsi_ctx = *vsi_ctx;
541 ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
543 /* update with new HW VSI num */
544 tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
551 * ice_free_vsi- free VSI context from hardware and VSI handle list
552 * @hw: pointer to the HW struct
553 * @vsi_handle: unique VSI handle
554 * @vsi_ctx: pointer to a VSI context struct
555 * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
556 * @cd: pointer to command details structure or NULL
558 * Free VSI context info from hardware as well as from VSI handle list
561 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
562 bool keep_vsi_alloc, struct ice_sq_cd *cd)
564 enum ice_status status;
566 if (!ice_is_vsi_valid(hw, vsi_handle))
567 return ICE_ERR_PARAM;
568 vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
569 status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
571 ice_clear_vsi_ctx(hw, vsi_handle);
577 * @hw: pointer to the HW struct
578 * @vsi_handle: unique VSI handle
579 * @vsi_ctx: pointer to a VSI context struct
580 * @cd: pointer to command details structure or NULL
582 * Update VSI context in the hardware
585 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
586 struct ice_sq_cd *cd)
588 if (!ice_is_vsi_valid(hw, vsi_handle))
589 return ICE_ERR_PARAM;
590 vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
591 return ice_aq_update_vsi(hw, vsi_ctx, cd);
595 * ice_aq_get_vsi_params
596 * @hw: pointer to the HW struct
597 * @vsi_ctx: pointer to a VSI context struct
598 * @cd: pointer to command details structure or NULL
600 * Get VSI context info from hardware (0x0212)
603 ice_aq_get_vsi_params(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
604 struct ice_sq_cd *cd)
606 struct ice_aqc_add_get_update_free_vsi *cmd;
607 struct ice_aqc_get_vsi_resp *resp;
608 struct ice_aq_desc desc;
609 enum ice_status status;
611 cmd = &desc.params.vsi_cmd;
612 resp = &desc.params.get_vsi_resp;
614 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_vsi_params);
616 cmd->vsi_num = CPU_TO_LE16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
618 status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
619 sizeof(vsi_ctx->info), cd);
621 vsi_ctx->vsi_num = LE16_TO_CPU(resp->vsi_num) &
623 vsi_ctx->vf_num = resp->vf_id;
624 vsi_ctx->vsis_allocd = LE16_TO_CPU(resp->vsi_used);
625 vsi_ctx->vsis_unallocated = LE16_TO_CPU(resp->vsi_free);
632 * ice_aq_add_update_mir_rule - add/update a mirror rule
633 * @hw: pointer to the HW struct
634 * @rule_type: Rule Type
635 * @dest_vsi: VSI number to which packets will be mirrored
636 * @count: length of the list
637 * @mr_buf: buffer for list of mirrored VSI numbers
638 * @cd: pointer to command details structure or NULL
641 * Add/Update Mirror Rule (0x260).
644 ice_aq_add_update_mir_rule(struct ice_hw *hw, u16 rule_type, u16 dest_vsi,
645 u16 count, struct ice_mir_rule_buf *mr_buf,
646 struct ice_sq_cd *cd, u16 *rule_id)
648 struct ice_aqc_add_update_mir_rule *cmd;
649 struct ice_aq_desc desc;
650 enum ice_status status;
651 __le16 *mr_list = NULL;
655 case ICE_AQC_RULE_TYPE_VPORT_INGRESS:
656 case ICE_AQC_RULE_TYPE_VPORT_EGRESS:
657 /* Make sure count and mr_buf are set for these rule_types */
658 if (!(count && mr_buf))
659 return ICE_ERR_PARAM;
661 buf_size = count * sizeof(__le16);
662 mr_list = (_FORCE_ __le16 *)ice_malloc(hw, buf_size);
664 return ICE_ERR_NO_MEMORY;
666 case ICE_AQC_RULE_TYPE_PPORT_INGRESS:
667 case ICE_AQC_RULE_TYPE_PPORT_EGRESS:
668 /* Make sure count and mr_buf are not set for these
672 return ICE_ERR_PARAM;
675 ice_debug(hw, ICE_DBG_SW, "Error due to unsupported rule_type %u\n", rule_type);
676 return ICE_ERR_OUT_OF_RANGE;
679 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_update_mir_rule);
681 /* Pre-process 'mr_buf' items for add/update of virtual port
682 * ingress/egress mirroring (but not physical port ingress/egress
688 for (i = 0; i < count; i++) {
691 id = mr_buf[i].vsi_idx & ICE_AQC_RULE_MIRRORED_VSI_M;
693 /* Validate specified VSI number, make sure it is less
694 * than ICE_MAX_VSI, if not return with error.
696 if (id >= ICE_MAX_VSI) {
697 ice_debug(hw, ICE_DBG_SW, "Error VSI index (%u) out-of-range\n",
699 ice_free(hw, mr_list);
700 return ICE_ERR_OUT_OF_RANGE;
703 /* add VSI to mirror rule */
706 CPU_TO_LE16(id | ICE_AQC_RULE_ACT_M);
707 else /* remove VSI from mirror rule */
708 mr_list[i] = CPU_TO_LE16(id);
712 cmd = &desc.params.add_update_rule;
713 if ((*rule_id) != ICE_INVAL_MIRROR_RULE_ID)
714 cmd->rule_id = CPU_TO_LE16(((*rule_id) & ICE_AQC_RULE_ID_M) |
715 ICE_AQC_RULE_ID_VALID_M);
716 cmd->rule_type = CPU_TO_LE16(rule_type & ICE_AQC_RULE_TYPE_M);
717 cmd->num_entries = CPU_TO_LE16(count);
718 cmd->dest = CPU_TO_LE16(dest_vsi);
720 status = ice_aq_send_cmd(hw, &desc, mr_list, buf_size, cd);
722 *rule_id = LE16_TO_CPU(cmd->rule_id) & ICE_AQC_RULE_ID_M;
724 ice_free(hw, mr_list);
730 * ice_aq_delete_mir_rule - delete a mirror rule
731 * @hw: pointer to the HW struct
732 * @rule_id: Mirror rule ID (to be deleted)
733 * @keep_allocd: if set, the VSI stays part of the PF allocated res,
734 * otherwise it is returned to the shared pool
735 * @cd: pointer to command details structure or NULL
737 * Delete Mirror Rule (0x261).
740 ice_aq_delete_mir_rule(struct ice_hw *hw, u16 rule_id, bool keep_allocd,
741 struct ice_sq_cd *cd)
743 struct ice_aqc_delete_mir_rule *cmd;
744 struct ice_aq_desc desc;
746 /* rule_id should be in the range 0...63 */
747 if (rule_id >= ICE_MAX_NUM_MIRROR_RULES)
748 return ICE_ERR_OUT_OF_RANGE;
750 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_del_mir_rule);
752 cmd = &desc.params.del_rule;
753 rule_id |= ICE_AQC_RULE_ID_VALID_M;
754 cmd->rule_id = CPU_TO_LE16(rule_id);
757 cmd->flags = CPU_TO_LE16(ICE_AQC_FLAG_KEEP_ALLOCD_M);
759 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
763 * ice_aq_alloc_free_vsi_list
764 * @hw: pointer to the HW struct
765 * @vsi_list_id: VSI list ID returned or used for lookup
766 * @lkup_type: switch rule filter lookup type
767 * @opc: switch rules population command type - pass in the command opcode
769 * allocates or free a VSI list resource
771 static enum ice_status
772 ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
773 enum ice_sw_lkup_type lkup_type,
774 enum ice_adminq_opc opc)
776 struct ice_aqc_alloc_free_res_elem *sw_buf;
777 struct ice_aqc_res_elem *vsi_ele;
778 enum ice_status status;
781 buf_len = ice_struct_size(sw_buf, elem, 1);
782 sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
784 return ICE_ERR_NO_MEMORY;
785 sw_buf->num_elems = CPU_TO_LE16(1);
787 if (lkup_type == ICE_SW_LKUP_MAC ||
788 lkup_type == ICE_SW_LKUP_MAC_VLAN ||
789 lkup_type == ICE_SW_LKUP_ETHERTYPE ||
790 lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
791 lkup_type == ICE_SW_LKUP_PROMISC ||
792 lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
793 lkup_type == ICE_SW_LKUP_LAST) {
794 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
795 } else if (lkup_type == ICE_SW_LKUP_VLAN) {
797 CPU_TO_LE16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
799 status = ICE_ERR_PARAM;
800 goto ice_aq_alloc_free_vsi_list_exit;
803 if (opc == ice_aqc_opc_free_res)
804 sw_buf->elem[0].e.sw_resp = CPU_TO_LE16(*vsi_list_id);
806 status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
808 goto ice_aq_alloc_free_vsi_list_exit;
810 if (opc == ice_aqc_opc_alloc_res) {
811 vsi_ele = &sw_buf->elem[0];
812 *vsi_list_id = LE16_TO_CPU(vsi_ele->e.sw_resp);
815 ice_aq_alloc_free_vsi_list_exit:
816 ice_free(hw, sw_buf);
821 * ice_aq_set_storm_ctrl - Sets storm control configuration
822 * @hw: pointer to the HW struct
823 * @bcast_thresh: represents the upper threshold for broadcast storm control
824 * @mcast_thresh: represents the upper threshold for multicast storm control
825 * @ctl_bitmask: storm control knobs
827 * Sets the storm control configuration (0x0280)
830 ice_aq_set_storm_ctrl(struct ice_hw *hw, u32 bcast_thresh, u32 mcast_thresh,
833 struct ice_aqc_storm_cfg *cmd;
834 struct ice_aq_desc desc;
836 cmd = &desc.params.storm_conf;
838 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_storm_cfg);
840 cmd->bcast_thresh_size = CPU_TO_LE32(bcast_thresh & ICE_AQ_THRESHOLD_M);
841 cmd->mcast_thresh_size = CPU_TO_LE32(mcast_thresh & ICE_AQ_THRESHOLD_M);
842 cmd->storm_ctrl_ctrl = CPU_TO_LE32(ctl_bitmask);
844 return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
848 * ice_aq_get_storm_ctrl - gets storm control configuration
849 * @hw: pointer to the HW struct
850 * @bcast_thresh: represents the upper threshold for broadcast storm control
851 * @mcast_thresh: represents the upper threshold for multicast storm control
852 * @ctl_bitmask: storm control knobs
854 * Gets the storm control configuration (0x0281)
857 ice_aq_get_storm_ctrl(struct ice_hw *hw, u32 *bcast_thresh, u32 *mcast_thresh,
860 enum ice_status status;
861 struct ice_aq_desc desc;
863 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_storm_cfg);
865 status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
867 struct ice_aqc_storm_cfg *resp = &desc.params.storm_conf;
870 *bcast_thresh = LE32_TO_CPU(resp->bcast_thresh_size) &
873 *mcast_thresh = LE32_TO_CPU(resp->mcast_thresh_size) &
876 *ctl_bitmask = LE32_TO_CPU(resp->storm_ctrl_ctrl);
883 * ice_aq_sw_rules - add/update/remove switch rules
884 * @hw: pointer to the HW struct
885 * @rule_list: pointer to switch rule population list
886 * @rule_list_sz: total size of the rule list in bytes
887 * @num_rules: number of switch rules in the rule_list
888 * @opc: switch rules population command type - pass in the command opcode
889 * @cd: pointer to command details structure or NULL
891 * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
893 static enum ice_status
894 ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
895 u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
897 struct ice_aq_desc desc;
898 enum ice_status status;
900 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
902 if (opc != ice_aqc_opc_add_sw_rules &&
903 opc != ice_aqc_opc_update_sw_rules &&
904 opc != ice_aqc_opc_remove_sw_rules)
905 return ICE_ERR_PARAM;
907 ice_fill_dflt_direct_cmd_desc(&desc, opc);
909 desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
910 desc.params.sw_rules.num_rules_fltr_entry_index =
911 CPU_TO_LE16(num_rules);
912 status = ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
913 if (opc != ice_aqc_opc_add_sw_rules &&
914 hw->adminq.sq_last_status == ICE_AQ_RC_ENOENT)
915 status = ICE_ERR_DOES_NOT_EXIST;
920 /* ice_init_port_info - Initialize port_info with switch configuration data
921 * @pi: pointer to port_info
922 * @vsi_port_num: VSI number or port number
923 * @type: Type of switch element (port or VSI)
924 * @swid: switch ID of the switch the element is attached to
925 * @pf_vf_num: PF or VF number
926 * @is_vf: true if the element is a VF, false otherwise
929 ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
930 u16 swid, u16 pf_vf_num, bool is_vf)
933 case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
934 pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
936 pi->pf_vf_num = pf_vf_num;
938 pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
939 pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
942 ice_debug(pi->hw, ICE_DBG_SW, "incorrect VSI/port type received\n");
947 /* ice_get_initial_sw_cfg - Get initial port and default VSI data
948 * @hw: pointer to the hardware structure
950 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
952 struct ice_aqc_get_sw_cfg_resp_elem *rbuf;
953 enum ice_status status;
962 rbuf = (struct ice_aqc_get_sw_cfg_resp_elem *)
963 ice_malloc(hw, ICE_SW_CFG_MAX_BUF_LEN);
966 return ICE_ERR_NO_MEMORY;
968 /* Multiple calls to ice_aq_get_sw_cfg may be required
969 * to get all the switch configuration information. The need
970 * for additional calls is indicated by ice_aq_get_sw_cfg
971 * writing a non-zero value in req_desc
974 struct ice_aqc_get_sw_cfg_resp_elem *ele;
976 status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
977 &req_desc, &num_elems, NULL);
982 for (i = 0, ele = rbuf; i < num_elems; i++, ele++) {
983 u16 pf_vf_num, swid, vsi_port_num;
987 vsi_port_num = LE16_TO_CPU(ele->vsi_port_num) &
988 ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
990 pf_vf_num = LE16_TO_CPU(ele->pf_vf_num) &
991 ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
993 swid = LE16_TO_CPU(ele->swid);
995 if (LE16_TO_CPU(ele->pf_vf_num) &
996 ICE_AQC_GET_SW_CONF_RESP_IS_VF)
999 res_type = (u8)(LE16_TO_CPU(ele->vsi_port_num) >>
1000 ICE_AQC_GET_SW_CONF_RESP_TYPE_S);
1003 case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
1004 case ICE_AQC_GET_SW_CONF_RESP_VIRT_PORT:
1005 if (j == num_total_ports) {
1006 ice_debug(hw, ICE_DBG_SW, "more ports than expected\n");
1007 status = ICE_ERR_CFG;
1010 ice_init_port_info(hw->port_info,
1011 vsi_port_num, res_type, swid,
1019 } while (req_desc && !status);
1027 * ice_fill_sw_info - Helper function to populate lb_en and lan_en
1028 * @hw: pointer to the hardware structure
1029 * @fi: filter info structure to fill/update
1031 * This helper function populates the lb_en and lan_en elements of the provided
1032 * ice_fltr_info struct using the switch's type and characteristics of the
1033 * switch rule being configured.
1035 static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *fi)
1039 if ((fi->flag & ICE_FLTR_TX) &&
1040 (fi->fltr_act == ICE_FWD_TO_VSI ||
1041 fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1042 fi->fltr_act == ICE_FWD_TO_Q ||
1043 fi->fltr_act == ICE_FWD_TO_QGRP)) {
1044 /* Setting LB for prune actions will result in replicated
1045 * packets to the internal switch that will be dropped.
1047 if (fi->lkup_type != ICE_SW_LKUP_VLAN)
1050 /* Set lan_en to TRUE if
1051 * 1. The switch is a VEB AND
1053 * 2.1 The lookup is a directional lookup like ethertype,
1054 * promiscuous, ethertype-MAC, promiscuous-VLAN
1055 * and default-port OR
1056 * 2.2 The lookup is VLAN, OR
1057 * 2.3 The lookup is MAC with mcast or bcast addr for MAC, OR
1058 * 2.4 The lookup is MAC_VLAN with mcast or bcast addr for MAC.
1062 * The switch is a VEPA.
1064 * In all other cases, the LAN enable has to be set to false.
1067 if (fi->lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1068 fi->lkup_type == ICE_SW_LKUP_PROMISC ||
1069 fi->lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1070 fi->lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1071 fi->lkup_type == ICE_SW_LKUP_DFLT ||
1072 fi->lkup_type == ICE_SW_LKUP_VLAN ||
1073 (fi->lkup_type == ICE_SW_LKUP_MAC &&
1074 !IS_UNICAST_ETHER_ADDR(fi->l_data.mac.mac_addr)) ||
1075 (fi->lkup_type == ICE_SW_LKUP_MAC_VLAN &&
1076 !IS_UNICAST_ETHER_ADDR(fi->l_data.mac.mac_addr)))
1085 * ice_fill_sw_rule - Helper function to fill switch rule structure
1086 * @hw: pointer to the hardware structure
1087 * @f_info: entry containing packet forwarding information
1088 * @s_rule: switch rule structure to be filled in based on mac_entry
1089 * @opc: switch rules population command type - pass in the command opcode
1092 ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
1093 struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
1095 u16 vlan_id = ICE_MAX_VLAN_ID + 1;
1103 if (opc == ice_aqc_opc_remove_sw_rules) {
1104 s_rule->pdata.lkup_tx_rx.act = 0;
1105 s_rule->pdata.lkup_tx_rx.index =
1106 CPU_TO_LE16(f_info->fltr_rule_id);
1107 s_rule->pdata.lkup_tx_rx.hdr_len = 0;
1111 eth_hdr_sz = sizeof(dummy_eth_header);
1112 eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
1114 /* initialize the ether header with a dummy header */
1115 ice_memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz, ICE_NONDMA_TO_NONDMA);
1116 ice_fill_sw_info(hw, f_info);
1118 switch (f_info->fltr_act) {
1119 case ICE_FWD_TO_VSI:
1120 act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
1121 ICE_SINGLE_ACT_VSI_ID_M;
1122 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1123 act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1124 ICE_SINGLE_ACT_VALID_BIT;
1126 case ICE_FWD_TO_VSI_LIST:
1127 act |= ICE_SINGLE_ACT_VSI_LIST;
1128 act |= (f_info->fwd_id.vsi_list_id <<
1129 ICE_SINGLE_ACT_VSI_LIST_ID_S) &
1130 ICE_SINGLE_ACT_VSI_LIST_ID_M;
1131 if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
1132 act |= ICE_SINGLE_ACT_VSI_FORWARDING |
1133 ICE_SINGLE_ACT_VALID_BIT;
1136 act |= ICE_SINGLE_ACT_TO_Q;
1137 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1138 ICE_SINGLE_ACT_Q_INDEX_M;
1140 case ICE_DROP_PACKET:
1141 act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
1142 ICE_SINGLE_ACT_VALID_BIT;
1144 case ICE_FWD_TO_QGRP:
1145 q_rgn = f_info->qgrp_size > 0 ?
1146 (u8)ice_ilog2(f_info->qgrp_size) : 0;
1147 act |= ICE_SINGLE_ACT_TO_Q;
1148 act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
1149 ICE_SINGLE_ACT_Q_INDEX_M;
1150 act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
1151 ICE_SINGLE_ACT_Q_REGION_M;
1158 act |= ICE_SINGLE_ACT_LB_ENABLE;
1160 act |= ICE_SINGLE_ACT_LAN_ENABLE;
1162 switch (f_info->lkup_type) {
1163 case ICE_SW_LKUP_MAC:
1164 daddr = f_info->l_data.mac.mac_addr;
1166 case ICE_SW_LKUP_VLAN:
1167 vlan_id = f_info->l_data.vlan.vlan_id;
1168 if (f_info->fltr_act == ICE_FWD_TO_VSI ||
1169 f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
1170 act |= ICE_SINGLE_ACT_PRUNE;
1171 act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
1174 case ICE_SW_LKUP_ETHERTYPE_MAC:
1175 daddr = f_info->l_data.ethertype_mac.mac_addr;
1177 case ICE_SW_LKUP_ETHERTYPE:
1178 off = (_FORCE_ __be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
1179 *off = CPU_TO_BE16(f_info->l_data.ethertype_mac.ethertype);
1181 case ICE_SW_LKUP_MAC_VLAN:
1182 daddr = f_info->l_data.mac_vlan.mac_addr;
1183 vlan_id = f_info->l_data.mac_vlan.vlan_id;
1185 case ICE_SW_LKUP_PROMISC_VLAN:
1186 vlan_id = f_info->l_data.mac_vlan.vlan_id;
1188 case ICE_SW_LKUP_PROMISC:
1189 daddr = f_info->l_data.mac_vlan.mac_addr;
1195 s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
1196 CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_RX) :
1197 CPU_TO_LE16(ICE_AQC_SW_RULES_T_LKUP_TX);
1199 /* Recipe set depending on lookup type */
1200 s_rule->pdata.lkup_tx_rx.recipe_id = CPU_TO_LE16(f_info->lkup_type);
1201 s_rule->pdata.lkup_tx_rx.src = CPU_TO_LE16(f_info->src);
1202 s_rule->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1205 ice_memcpy(eth_hdr + ICE_ETH_DA_OFFSET, daddr, ETH_ALEN,
1206 ICE_NONDMA_TO_NONDMA);
1208 if (!(vlan_id > ICE_MAX_VLAN_ID)) {
1209 off = (_FORCE_ __be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
1210 *off = CPU_TO_BE16(vlan_id);
1213 /* Create the switch rule with the final dummy Ethernet header */
1214 if (opc != ice_aqc_opc_update_sw_rules)
1215 s_rule->pdata.lkup_tx_rx.hdr_len = CPU_TO_LE16(eth_hdr_sz);
1219 * ice_add_marker_act
1220 * @hw: pointer to the hardware structure
1221 * @m_ent: the management entry for which sw marker needs to be added
1222 * @sw_marker: sw marker to tag the Rx descriptor with
1223 * @l_id: large action resource ID
1225 * Create a large action to hold software marker and update the switch rule
1226 * entry pointed by m_ent with newly created large action
1228 static enum ice_status
1229 ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1230 u16 sw_marker, u16 l_id)
1232 struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
1233 /* For software marker we need 3 large actions
1234 * 1. FWD action: FWD TO VSI or VSI LIST
1235 * 2. GENERIC VALUE action to hold the profile ID
1236 * 3. GENERIC VALUE action to hold the software marker ID
1238 const u16 num_lg_acts = 3;
1239 enum ice_status status;
1245 if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1246 return ICE_ERR_PARAM;
1248 /* Create two back-to-back switch rules and submit them to the HW using
1249 * one memory buffer:
1253 lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
1254 rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1255 lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, rules_size);
1257 return ICE_ERR_NO_MEMORY;
1259 rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
1261 /* Fill in the first switch rule i.e. large action */
1262 lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1263 lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1264 lg_act->pdata.lg_act.size = CPU_TO_LE16(num_lg_acts);
1266 /* First action VSI forwarding or VSI list forwarding depending on how
1269 id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
1270 m_ent->fltr_info.fwd_id.hw_vsi_id;
1272 act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1273 act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) & ICE_LG_ACT_VSI_LIST_ID_M;
1274 if (m_ent->vsi_count > 1)
1275 act |= ICE_LG_ACT_VSI_LIST;
1276 lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1278 /* Second action descriptor type */
1279 act = ICE_LG_ACT_GENERIC;
1281 act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
1282 lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1284 act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
1285 ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
1287 /* Third action Marker value */
1288 act |= ICE_LG_ACT_GENERIC;
1289 act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
1290 ICE_LG_ACT_GENERIC_VALUE_M;
1292 lg_act->pdata.lg_act.act[2] = CPU_TO_LE32(act);
1294 /* call the fill switch rule to fill the lookup Tx Rx structure */
1295 ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1296 ice_aqc_opc_update_sw_rules);
1298 /* Update the action to point to the large action ID */
1299 rx_tx->pdata.lkup_tx_rx.act =
1300 CPU_TO_LE32(ICE_SINGLE_ACT_PTR |
1301 ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
1302 ICE_SINGLE_ACT_PTR_VAL_M));
1304 /* Use the filter rule ID of the previously created rule with single
1305 * act. Once the update happens, hardware will treat this as large
1308 rx_tx->pdata.lkup_tx_rx.index =
1309 CPU_TO_LE16(m_ent->fltr_info.fltr_rule_id);
1311 status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1312 ice_aqc_opc_update_sw_rules, NULL);
1314 m_ent->lg_act_idx = l_id;
1315 m_ent->sw_marker_id = sw_marker;
1318 ice_free(hw, lg_act);
1323 * ice_add_counter_act - add/update filter rule with counter action
1324 * @hw: pointer to the hardware structure
1325 * @m_ent: the management entry for which counter needs to be added
1326 * @counter_id: VLAN counter ID returned as part of allocate resource
1327 * @l_id: large action resource ID
1329 static enum ice_status
1330 ice_add_counter_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
1331 u16 counter_id, u16 l_id)
1333 struct ice_aqc_sw_rules_elem *lg_act;
1334 struct ice_aqc_sw_rules_elem *rx_tx;
1335 enum ice_status status;
1336 /* 2 actions will be added while adding a large action counter */
1337 const int num_acts = 2;
1344 if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
1345 return ICE_ERR_PARAM;
1347 /* Create two back-to-back switch rules and submit them to the HW using
1348 * one memory buffer:
1352 lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_acts);
1353 rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
1354 lg_act = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, rules_size);
1356 return ICE_ERR_NO_MEMORY;
1358 rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
1360 /* Fill in the first switch rule i.e. large action */
1361 lg_act->type = CPU_TO_LE16(ICE_AQC_SW_RULES_T_LG_ACT);
1362 lg_act->pdata.lg_act.index = CPU_TO_LE16(l_id);
1363 lg_act->pdata.lg_act.size = CPU_TO_LE16(num_acts);
1365 /* First action VSI forwarding or VSI list forwarding depending on how
1368 id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
1369 m_ent->fltr_info.fwd_id.hw_vsi_id;
1371 act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
1372 act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
1373 ICE_LG_ACT_VSI_LIST_ID_M;
1374 if (m_ent->vsi_count > 1)
1375 act |= ICE_LG_ACT_VSI_LIST;
1376 lg_act->pdata.lg_act.act[0] = CPU_TO_LE32(act);
1378 /* Second action counter ID */
1379 act = ICE_LG_ACT_STAT_COUNT;
1380 act |= (counter_id << ICE_LG_ACT_STAT_COUNT_S) &
1381 ICE_LG_ACT_STAT_COUNT_M;
1382 lg_act->pdata.lg_act.act[1] = CPU_TO_LE32(act);
1384 /* call the fill switch rule to fill the lookup Tx Rx structure */
1385 ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
1386 ice_aqc_opc_update_sw_rules);
1388 act = ICE_SINGLE_ACT_PTR;
1389 act |= (l_id << ICE_SINGLE_ACT_PTR_VAL_S) & ICE_SINGLE_ACT_PTR_VAL_M;
1390 rx_tx->pdata.lkup_tx_rx.act = CPU_TO_LE32(act);
1392 /* Use the filter rule ID of the previously created rule with single
1393 * act. Once the update happens, hardware will treat this as large
1396 f_rule_id = m_ent->fltr_info.fltr_rule_id;
1397 rx_tx->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_rule_id);
1399 status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
1400 ice_aqc_opc_update_sw_rules, NULL);
1402 m_ent->lg_act_idx = l_id;
1403 m_ent->counter_index = counter_id;
1406 ice_free(hw, lg_act);
1411 * ice_create_vsi_list_map
1412 * @hw: pointer to the hardware structure
1413 * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
1414 * @num_vsi: number of VSI handles in the array
1415 * @vsi_list_id: VSI list ID generated as part of allocate resource
1417 * Helper function to create a new entry of VSI list ID to VSI mapping
1418 * using the given VSI list ID
1420 static struct ice_vsi_list_map_info *
1421 ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1424 struct ice_switch_info *sw = hw->switch_info;
1425 struct ice_vsi_list_map_info *v_map;
1428 v_map = (struct ice_vsi_list_map_info *)ice_calloc(hw, 1,
1433 v_map->vsi_list_id = vsi_list_id;
1435 for (i = 0; i < num_vsi; i++)
1436 ice_set_bit(vsi_handle_arr[i], v_map->vsi_map);
1438 LIST_ADD(&v_map->list_entry, &sw->vsi_list_map_head);
1443 * ice_update_vsi_list_rule
1444 * @hw: pointer to the hardware structure
1445 * @vsi_handle_arr: array of VSI handles to form a VSI list
1446 * @num_vsi: number of VSI handles in the array
1447 * @vsi_list_id: VSI list ID generated as part of allocate resource
1448 * @remove: Boolean value to indicate if this is a remove action
1449 * @opc: switch rules population command type - pass in the command opcode
1450 * @lkup_type: lookup type of the filter
1452 * Call AQ command to add a new switch rule or update existing switch rule
1453 * using the given VSI list ID
1455 static enum ice_status
1456 ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1457 u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
1458 enum ice_sw_lkup_type lkup_type)
1460 struct ice_aqc_sw_rules_elem *s_rule;
1461 enum ice_status status;
1467 return ICE_ERR_PARAM;
1469 if (lkup_type == ICE_SW_LKUP_MAC ||
1470 lkup_type == ICE_SW_LKUP_MAC_VLAN ||
1471 lkup_type == ICE_SW_LKUP_ETHERTYPE ||
1472 lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
1473 lkup_type == ICE_SW_LKUP_PROMISC ||
1474 lkup_type == ICE_SW_LKUP_PROMISC_VLAN ||
1475 lkup_type == ICE_SW_LKUP_LAST)
1476 rule_type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
1477 ICE_AQC_SW_RULES_T_VSI_LIST_SET;
1478 else if (lkup_type == ICE_SW_LKUP_VLAN)
1479 rule_type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
1480 ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
1482 return ICE_ERR_PARAM;
1484 s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
1485 s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
1487 return ICE_ERR_NO_MEMORY;
1488 for (i = 0; i < num_vsi; i++) {
1489 if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
1490 status = ICE_ERR_PARAM;
1493 /* AQ call requires hw_vsi_id(s) */
1494 s_rule->pdata.vsi_list.vsi[i] =
1495 CPU_TO_LE16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
1498 s_rule->type = CPU_TO_LE16(rule_type);
1499 s_rule->pdata.vsi_list.number_vsi = CPU_TO_LE16(num_vsi);
1500 s_rule->pdata.vsi_list.index = CPU_TO_LE16(vsi_list_id);
1502 status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
1505 ice_free(hw, s_rule);
1510 * ice_create_vsi_list_rule - Creates and populates a VSI list rule
1511 * @hw: pointer to the HW struct
1512 * @vsi_handle_arr: array of VSI handles to form a VSI list
1513 * @num_vsi: number of VSI handles in the array
1514 * @vsi_list_id: stores the ID of the VSI list to be created
1515 * @lkup_type: switch rule filter's lookup type
1517 static enum ice_status
1518 ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
1519 u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
1521 enum ice_status status;
1523 status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
1524 ice_aqc_opc_alloc_res);
1528 /* Update the newly created VSI list to include the specified VSIs */
1529 return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
1530 *vsi_list_id, false,
1531 ice_aqc_opc_add_sw_rules, lkup_type);
1535 * ice_create_pkt_fwd_rule
1536 * @hw: pointer to the hardware structure
1537 * @recp_list: corresponding filter management list
1538 * @f_entry: entry containing packet forwarding information
1540 * Create switch rule with given filter information and add an entry
1541 * to the corresponding filter management list to track this switch rule
1544 static enum ice_status
1545 ice_create_pkt_fwd_rule(struct ice_hw *hw, struct ice_sw_recipe *recp_list,
1546 struct ice_fltr_list_entry *f_entry)
1548 struct ice_fltr_mgmt_list_entry *fm_entry;
1549 struct ice_aqc_sw_rules_elem *s_rule;
1550 enum ice_status status;
1552 s_rule = (struct ice_aqc_sw_rules_elem *)
1553 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1555 return ICE_ERR_NO_MEMORY;
1556 fm_entry = (struct ice_fltr_mgmt_list_entry *)
1557 ice_malloc(hw, sizeof(*fm_entry));
1559 status = ICE_ERR_NO_MEMORY;
1560 goto ice_create_pkt_fwd_rule_exit;
1563 fm_entry->fltr_info = f_entry->fltr_info;
1565 /* Initialize all the fields for the management entry */
1566 fm_entry->vsi_count = 1;
1567 fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
1568 fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
1569 fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
1571 ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
1572 ice_aqc_opc_add_sw_rules);
1574 status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1575 ice_aqc_opc_add_sw_rules, NULL);
1577 ice_free(hw, fm_entry);
1578 goto ice_create_pkt_fwd_rule_exit;
1581 f_entry->fltr_info.fltr_rule_id =
1582 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1583 fm_entry->fltr_info.fltr_rule_id =
1584 LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
1586 /* The book keeping entries will get removed when base driver
1587 * calls remove filter AQ command
1589 LIST_ADD(&fm_entry->list_entry, &recp_list->filt_rules);
1591 ice_create_pkt_fwd_rule_exit:
1592 ice_free(hw, s_rule);
1597 * ice_update_pkt_fwd_rule
1598 * @hw: pointer to the hardware structure
1599 * @f_info: filter information for switch rule
1601 * Call AQ command to update a previously created switch rule with a
1604 static enum ice_status
1605 ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
1607 struct ice_aqc_sw_rules_elem *s_rule;
1608 enum ice_status status;
1610 s_rule = (struct ice_aqc_sw_rules_elem *)
1611 ice_malloc(hw, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE);
1613 return ICE_ERR_NO_MEMORY;
1615 ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
1617 s_rule->pdata.lkup_tx_rx.index = CPU_TO_LE16(f_info->fltr_rule_id);
1619 /* Update switch rule with new rule set to forward VSI list */
1620 status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
1621 ice_aqc_opc_update_sw_rules, NULL);
1623 ice_free(hw, s_rule);
1628 * ice_update_sw_rule_bridge_mode
1629 * @hw: pointer to the HW struct
1631 * Updates unicast switch filter rules based on VEB/VEPA mode
1633 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
1635 struct ice_switch_info *sw = hw->switch_info;
1636 struct ice_fltr_mgmt_list_entry *fm_entry;
1637 enum ice_status status = ICE_SUCCESS;
1638 struct LIST_HEAD_TYPE *rule_head;
1639 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
1641 rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
1642 rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
1644 ice_acquire_lock(rule_lock);
1645 LIST_FOR_EACH_ENTRY(fm_entry, rule_head, ice_fltr_mgmt_list_entry,
1647 struct ice_fltr_info *fi = &fm_entry->fltr_info;
1648 u8 *addr = fi->l_data.mac.mac_addr;
1650 /* Update unicast Tx rules to reflect the selected
1653 if ((fi->flag & ICE_FLTR_TX) && IS_UNICAST_ETHER_ADDR(addr) &&
1654 (fi->fltr_act == ICE_FWD_TO_VSI ||
1655 fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
1656 fi->fltr_act == ICE_FWD_TO_Q ||
1657 fi->fltr_act == ICE_FWD_TO_QGRP)) {
1658 status = ice_update_pkt_fwd_rule(hw, fi);
1664 ice_release_lock(rule_lock);
1670 * ice_add_update_vsi_list
1671 * @hw: pointer to the hardware structure
1672 * @m_entry: pointer to current filter management list entry
1673 * @cur_fltr: filter information from the book keeping entry
1674 * @new_fltr: filter information with the new VSI to be added
1676 * Call AQ command to add or update previously created VSI list with new VSI.
1678 * Helper function to do book keeping associated with adding filter information
1679 * The algorithm to do the book keeping is described below :
1680 * When a VSI needs to subscribe to a given filter (MAC/VLAN/Ethtype etc.)
1681 * if only one VSI has been added till now
1682 * Allocate a new VSI list and add two VSIs
1683 * to this list using switch rule command
1684 * Update the previously created switch rule with the
1685 * newly created VSI list ID
1686 * if a VSI list was previously created
1687 * Add the new VSI to the previously created VSI list set
1688 * using the update switch rule command
1690 static enum ice_status
1691 ice_add_update_vsi_list(struct ice_hw *hw,
1692 struct ice_fltr_mgmt_list_entry *m_entry,
1693 struct ice_fltr_info *cur_fltr,
1694 struct ice_fltr_info *new_fltr)
1696 enum ice_status status = ICE_SUCCESS;
1697 u16 vsi_list_id = 0;
1699 if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
1700 cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
1701 return ICE_ERR_NOT_IMPL;
1703 if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
1704 new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
1705 (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
1706 cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
1707 return ICE_ERR_NOT_IMPL;
1709 if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
1710 /* Only one entry existed in the mapping and it was not already
1711 * a part of a VSI list. So, create a VSI list with the old and
1714 struct ice_fltr_info tmp_fltr;
1715 u16 vsi_handle_arr[2];
1717 /* A rule already exists with the new VSI being added */
1718 if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
1719 return ICE_ERR_ALREADY_EXISTS;
1721 vsi_handle_arr[0] = cur_fltr->vsi_handle;
1722 vsi_handle_arr[1] = new_fltr->vsi_handle;
1723 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
1725 new_fltr->lkup_type);
1729 tmp_fltr = *new_fltr;
1730 tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
1731 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
1732 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
1733 /* Update the previous switch rule of "MAC forward to VSI" to
1734 * "MAC fwd to VSI list"
1736 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
1740 cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
1741 cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
1742 m_entry->vsi_list_info =
1743 ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
1746 /* If this entry was large action then the large action needs
1747 * to be updated to point to FWD to VSI list
1749 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
1751 ice_add_marker_act(hw, m_entry,
1752 m_entry->sw_marker_id,
1753 m_entry->lg_act_idx);
1755 u16 vsi_handle = new_fltr->vsi_handle;
1756 enum ice_adminq_opc opcode;
1758 if (!m_entry->vsi_list_info)
1761 /* A rule already exists with the new VSI being added */
1762 if (ice_is_bit_set(m_entry->vsi_list_info->vsi_map, vsi_handle))
1765 /* Update the previously created VSI list set with
1766 * the new VSI ID passed in
1768 vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
1769 opcode = ice_aqc_opc_update_sw_rules;
1771 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
1772 vsi_list_id, false, opcode,
1773 new_fltr->lkup_type);
1774 /* update VSI list mapping info with new VSI ID */
1776 ice_set_bit(vsi_handle,
1777 m_entry->vsi_list_info->vsi_map);
1780 m_entry->vsi_count++;
1785 * ice_find_rule_entry - Search a rule entry
1786 * @list_head: head of rule list
1787 * @f_info: rule information
1789 * Helper function to search for a given rule entry
1790 * Returns pointer to entry storing the rule if found
1792 static struct ice_fltr_mgmt_list_entry *
1793 ice_find_rule_entry(struct LIST_HEAD_TYPE *list_head,
1794 struct ice_fltr_info *f_info)
1796 struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
1798 LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
1800 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
1801 sizeof(f_info->l_data)) &&
1802 f_info->flag == list_itr->fltr_info.flag) {
1811 * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
1812 * @recp_list: VSI lists needs to be searched
1813 * @vsi_handle: VSI handle to be found in VSI list
1814 * @vsi_list_id: VSI list ID found containing vsi_handle
1816 * Helper function to search a VSI list with single entry containing given VSI
1817 * handle element. This can be extended further to search VSI list with more
1818 * than 1 vsi_count. Returns pointer to VSI list entry if found.
1820 static struct ice_vsi_list_map_info *
1821 ice_find_vsi_list_entry(struct ice_sw_recipe *recp_list, u16 vsi_handle,
1824 struct ice_vsi_list_map_info *map_info = NULL;
1825 struct LIST_HEAD_TYPE *list_head;
1827 list_head = &recp_list->filt_rules;
1828 if (recp_list->adv_rule) {
1829 struct ice_adv_fltr_mgmt_list_entry *list_itr;
1831 LIST_FOR_EACH_ENTRY(list_itr, list_head,
1832 ice_adv_fltr_mgmt_list_entry,
1834 if (list_itr->vsi_list_info) {
1835 map_info = list_itr->vsi_list_info;
1836 if (ice_is_bit_set(map_info->vsi_map,
1838 *vsi_list_id = map_info->vsi_list_id;
1844 struct ice_fltr_mgmt_list_entry *list_itr;
1846 LIST_FOR_EACH_ENTRY(list_itr, list_head,
1847 ice_fltr_mgmt_list_entry,
1849 if (list_itr->vsi_count == 1 &&
1850 list_itr->vsi_list_info) {
1851 map_info = list_itr->vsi_list_info;
1852 if (ice_is_bit_set(map_info->vsi_map,
1854 *vsi_list_id = map_info->vsi_list_id;
1864 * ice_add_rule_internal - add rule for a given lookup type
1865 * @hw: pointer to the hardware structure
1866 * @recp_list: recipe list for which rule has to be added
1867 * @lport: logic port number on which function add rule
1868 * @f_entry: structure containing MAC forwarding information
1870 * Adds or updates the rule lists for a given recipe
1872 static enum ice_status
1873 ice_add_rule_internal(struct ice_hw *hw, struct ice_sw_recipe *recp_list,
1874 u8 lport, struct ice_fltr_list_entry *f_entry)
1876 struct ice_fltr_info *new_fltr, *cur_fltr;
1877 struct ice_fltr_mgmt_list_entry *m_entry;
1878 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
1879 enum ice_status status = ICE_SUCCESS;
1881 if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
1882 return ICE_ERR_PARAM;
1884 /* Load the hw_vsi_id only if the fwd action is fwd to VSI */
1885 if (f_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI)
1886 f_entry->fltr_info.fwd_id.hw_vsi_id =
1887 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1889 rule_lock = &recp_list->filt_rule_lock;
1891 ice_acquire_lock(rule_lock);
1892 new_fltr = &f_entry->fltr_info;
1893 if (new_fltr->flag & ICE_FLTR_RX)
1894 new_fltr->src = lport;
1895 else if (new_fltr->flag & ICE_FLTR_TX)
1897 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
1899 m_entry = ice_find_rule_entry(&recp_list->filt_rules, new_fltr);
1901 status = ice_create_pkt_fwd_rule(hw, recp_list, f_entry);
1902 goto exit_add_rule_internal;
1905 cur_fltr = &m_entry->fltr_info;
1906 status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
1908 exit_add_rule_internal:
1909 ice_release_lock(rule_lock);
1914 * ice_remove_vsi_list_rule
1915 * @hw: pointer to the hardware structure
1916 * @vsi_list_id: VSI list ID generated as part of allocate resource
1917 * @lkup_type: switch rule filter lookup type
1919 * The VSI list should be emptied before this function is called to remove the
1922 static enum ice_status
1923 ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
1924 enum ice_sw_lkup_type lkup_type)
1926 /* Free the vsi_list resource that we allocated. It is assumed that the
1927 * list is empty at this point.
1929 return ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
1930 ice_aqc_opc_free_res);
1934 * ice_rem_update_vsi_list
1935 * @hw: pointer to the hardware structure
1936 * @vsi_handle: VSI handle of the VSI to remove
1937 * @fm_list: filter management entry for which the VSI list management needs to
1940 static enum ice_status
1941 ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
1942 struct ice_fltr_mgmt_list_entry *fm_list)
1944 enum ice_sw_lkup_type lkup_type;
1945 enum ice_status status = ICE_SUCCESS;
1948 if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
1949 fm_list->vsi_count == 0)
1950 return ICE_ERR_PARAM;
1952 /* A rule with the VSI being removed does not exist */
1953 if (!ice_is_bit_set(fm_list->vsi_list_info->vsi_map, vsi_handle))
1954 return ICE_ERR_DOES_NOT_EXIST;
1956 lkup_type = fm_list->fltr_info.lkup_type;
1957 vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
1958 status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
1959 ice_aqc_opc_update_sw_rules,
1964 fm_list->vsi_count--;
1965 ice_clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
1967 if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
1968 struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
1969 struct ice_vsi_list_map_info *vsi_list_info =
1970 fm_list->vsi_list_info;
1973 rem_vsi_handle = ice_find_first_bit(vsi_list_info->vsi_map,
1975 if (!ice_is_vsi_valid(hw, rem_vsi_handle))
1976 return ICE_ERR_OUT_OF_RANGE;
1978 /* Make sure VSI list is empty before removing it below */
1979 status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
1981 ice_aqc_opc_update_sw_rules,
1986 tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
1987 tmp_fltr_info.fwd_id.hw_vsi_id =
1988 ice_get_hw_vsi_num(hw, rem_vsi_handle);
1989 tmp_fltr_info.vsi_handle = rem_vsi_handle;
1990 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
1992 ice_debug(hw, ICE_DBG_SW, "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
1993 tmp_fltr_info.fwd_id.hw_vsi_id, status);
1997 fm_list->fltr_info = tmp_fltr_info;
2000 if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
2001 (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
2002 struct ice_vsi_list_map_info *vsi_list_info =
2003 fm_list->vsi_list_info;
2005 /* Remove the VSI list since it is no longer used */
2006 status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
2008 ice_debug(hw, ICE_DBG_SW, "Failed to remove VSI list %d, error %d\n",
2009 vsi_list_id, status);
2013 LIST_DEL(&vsi_list_info->list_entry);
2014 ice_free(hw, vsi_list_info);
2015 fm_list->vsi_list_info = NULL;
2022 * ice_remove_rule_internal - Remove a filter rule of a given type
2024 * @hw: pointer to the hardware structure
2025 * @recp_list: recipe list for which the rule needs to removed
2026 * @f_entry: rule entry containing filter information
2028 static enum ice_status
2029 ice_remove_rule_internal(struct ice_hw *hw, struct ice_sw_recipe *recp_list,
2030 struct ice_fltr_list_entry *f_entry)
2032 struct ice_fltr_mgmt_list_entry *list_elem;
2033 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2034 enum ice_status status = ICE_SUCCESS;
2035 bool remove_rule = false;
2038 if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2039 return ICE_ERR_PARAM;
2040 f_entry->fltr_info.fwd_id.hw_vsi_id =
2041 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2043 rule_lock = &recp_list->filt_rule_lock;
2044 ice_acquire_lock(rule_lock);
2045 list_elem = ice_find_rule_entry(&recp_list->filt_rules,
2046 &f_entry->fltr_info);
2048 status = ICE_ERR_DOES_NOT_EXIST;
2052 if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
2054 } else if (!list_elem->vsi_list_info) {
2055 status = ICE_ERR_DOES_NOT_EXIST;
2057 } else if (list_elem->vsi_list_info->ref_cnt > 1) {
2058 /* a ref_cnt > 1 indicates that the vsi_list is being
2059 * shared by multiple rules. Decrement the ref_cnt and
2060 * remove this rule, but do not modify the list, as it
2061 * is in-use by other rules.
2063 list_elem->vsi_list_info->ref_cnt--;
2066 /* a ref_cnt of 1 indicates the vsi_list is only used
2067 * by one rule. However, the original removal request is only
2068 * for a single VSI. Update the vsi_list first, and only
2069 * remove the rule if there are no further VSIs in this list.
2071 vsi_handle = f_entry->fltr_info.vsi_handle;
2072 status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
2075 /* if VSI count goes to zero after updating the VSI list */
2076 if (list_elem->vsi_count == 0)
2081 /* Remove the lookup rule */
2082 struct ice_aqc_sw_rules_elem *s_rule;
2084 s_rule = (struct ice_aqc_sw_rules_elem *)
2085 ice_malloc(hw, ICE_SW_RULE_RX_TX_NO_HDR_SIZE);
2087 status = ICE_ERR_NO_MEMORY;
2091 ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
2092 ice_aqc_opc_remove_sw_rules);
2094 status = ice_aq_sw_rules(hw, s_rule,
2095 ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
2096 ice_aqc_opc_remove_sw_rules, NULL);
2098 /* Remove a book keeping from the list */
2099 ice_free(hw, s_rule);
2104 LIST_DEL(&list_elem->list_entry);
2105 ice_free(hw, list_elem);
2108 ice_release_lock(rule_lock);
2113 * ice_aq_get_res_alloc - get allocated resources
2114 * @hw: pointer to the HW struct
2115 * @num_entries: pointer to u16 to store the number of resource entries returned
2116 * @buf: pointer to buffer
2117 * @buf_size: size of buf
2118 * @cd: pointer to command details structure or NULL
2120 * The caller-supplied buffer must be large enough to store the resource
2121 * information for all resource types. Each resource type is an
2122 * ice_aqc_get_res_resp_elem structure.
2125 ice_aq_get_res_alloc(struct ice_hw *hw, u16 *num_entries,
2126 struct ice_aqc_get_res_resp_elem *buf, u16 buf_size,
2127 struct ice_sq_cd *cd)
2129 struct ice_aqc_get_res_alloc *resp;
2130 enum ice_status status;
2131 struct ice_aq_desc desc;
2134 return ICE_ERR_BAD_PTR;
2136 if (buf_size < ICE_AQ_GET_RES_ALLOC_BUF_LEN)
2137 return ICE_ERR_INVAL_SIZE;
2139 resp = &desc.params.get_res;
2141 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_res_alloc);
2142 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2144 if (!status && num_entries)
2145 *num_entries = LE16_TO_CPU(resp->resp_elem_num);
2151 * ice_aq_get_res_descs - get allocated resource descriptors
2152 * @hw: pointer to the hardware structure
2153 * @num_entries: number of resource entries in buffer
2154 * @buf: structure to hold response data buffer
2155 * @buf_size: size of buffer
2156 * @res_type: resource type
2157 * @res_shared: is resource shared
2158 * @desc_id: input - first desc ID to start; output - next desc ID
2159 * @cd: pointer to command details structure or NULL
2162 ice_aq_get_res_descs(struct ice_hw *hw, u16 num_entries,
2163 struct ice_aqc_res_elem *buf, u16 buf_size, u16 res_type,
2164 bool res_shared, u16 *desc_id, struct ice_sq_cd *cd)
2166 struct ice_aqc_get_allocd_res_desc *cmd;
2167 struct ice_aq_desc desc;
2168 enum ice_status status;
2170 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2172 cmd = &desc.params.get_res_desc;
2175 return ICE_ERR_PARAM;
2177 if (buf_size != (num_entries * sizeof(*buf)))
2178 return ICE_ERR_PARAM;
2180 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_allocd_res_desc);
2182 cmd->ops.cmd.res = CPU_TO_LE16(((res_type << ICE_AQC_RES_TYPE_S) &
2183 ICE_AQC_RES_TYPE_M) | (res_shared ?
2184 ICE_AQC_RES_TYPE_FLAG_SHARED : 0));
2185 cmd->ops.cmd.first_desc = CPU_TO_LE16(*desc_id);
2187 status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
2189 *desc_id = LE16_TO_CPU(cmd->ops.resp.next_desc);
2195 * ice_add_mac_rule - Add a MAC address based filter rule
2196 * @hw: pointer to the hardware structure
2197 * @m_list: list of MAC addresses and forwarding information
2198 * @sw: pointer to switch info struct for which function add rule
2199 * @lport: logic port number on which function add rule
2201 * IMPORTANT: When the ucast_shared flag is set to false and m_list has
2202 * multiple unicast addresses, the function assumes that all the
2203 * addresses are unique in a given add_mac call. It doesn't
2204 * check for duplicates in this case, removing duplicates from a given
2205 * list should be taken care of in the caller of this function.
2207 static enum ice_status
2208 ice_add_mac_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list,
2209 struct ice_switch_info *sw, u8 lport)
2211 struct ice_sw_recipe *recp_list = &sw->recp_list[ICE_SW_LKUP_MAC];
2212 struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
2213 struct ice_fltr_list_entry *m_list_itr;
2214 struct LIST_HEAD_TYPE *rule_head;
2215 u16 total_elem_left, s_rule_size;
2216 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2217 enum ice_status status = ICE_SUCCESS;
2218 u16 num_unicast = 0;
2222 rule_lock = &recp_list->filt_rule_lock;
2223 rule_head = &recp_list->filt_rules;
2225 LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2227 u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
2231 m_list_itr->fltr_info.flag = ICE_FLTR_TX;
2232 vsi_handle = m_list_itr->fltr_info.vsi_handle;
2233 if (!ice_is_vsi_valid(hw, vsi_handle))
2234 return ICE_ERR_PARAM;
2235 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2236 m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
2237 /* update the src in case it is VSI num */
2238 if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
2239 return ICE_ERR_PARAM;
2240 m_list_itr->fltr_info.src = hw_vsi_id;
2241 if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
2242 IS_ZERO_ETHER_ADDR(add))
2243 return ICE_ERR_PARAM;
2244 if (IS_UNICAST_ETHER_ADDR(add) && !hw->ucast_shared) {
2245 /* Don't overwrite the unicast address */
2246 ice_acquire_lock(rule_lock);
2247 if (ice_find_rule_entry(rule_head,
2248 &m_list_itr->fltr_info)) {
2249 ice_release_lock(rule_lock);
2250 return ICE_ERR_ALREADY_EXISTS;
2252 ice_release_lock(rule_lock);
2254 } else if (IS_MULTICAST_ETHER_ADDR(add) ||
2255 (IS_UNICAST_ETHER_ADDR(add) && hw->ucast_shared)) {
2256 m_list_itr->status =
2257 ice_add_rule_internal(hw, recp_list, lport,
2259 if (m_list_itr->status)
2260 return m_list_itr->status;
2264 ice_acquire_lock(rule_lock);
2265 /* Exit if no suitable entries were found for adding bulk switch rule */
2267 status = ICE_SUCCESS;
2268 goto ice_add_mac_exit;
2271 /* Allocate switch rule buffer for the bulk update for unicast */
2272 s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
2273 s_rule = (struct ice_aqc_sw_rules_elem *)
2274 ice_calloc(hw, num_unicast, s_rule_size);
2276 status = ICE_ERR_NO_MEMORY;
2277 goto ice_add_mac_exit;
2281 LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2283 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2284 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2286 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2287 ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
2288 ice_aqc_opc_add_sw_rules);
2289 r_iter = (struct ice_aqc_sw_rules_elem *)
2290 ((u8 *)r_iter + s_rule_size);
2294 /* Call AQ bulk switch rule update for all unicast addresses */
2296 /* Call AQ switch rule in AQ_MAX chunk */
2297 for (total_elem_left = num_unicast; total_elem_left > 0;
2298 total_elem_left -= elem_sent) {
2299 struct ice_aqc_sw_rules_elem *entry = r_iter;
2301 elem_sent = MIN_T(u8, total_elem_left,
2302 (ICE_AQ_MAX_BUF_LEN / s_rule_size));
2303 status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
2304 elem_sent, ice_aqc_opc_add_sw_rules,
2307 goto ice_add_mac_exit;
2308 r_iter = (struct ice_aqc_sw_rules_elem *)
2309 ((u8 *)r_iter + (elem_sent * s_rule_size));
2312 /* Fill up rule ID based on the value returned from FW */
2314 LIST_FOR_EACH_ENTRY(m_list_itr, m_list, ice_fltr_list_entry,
2316 struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
2317 u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
2318 struct ice_fltr_mgmt_list_entry *fm_entry;
2320 if (IS_UNICAST_ETHER_ADDR(mac_addr)) {
2321 f_info->fltr_rule_id =
2322 LE16_TO_CPU(r_iter->pdata.lkup_tx_rx.index);
2323 f_info->fltr_act = ICE_FWD_TO_VSI;
2324 /* Create an entry to track this MAC address */
2325 fm_entry = (struct ice_fltr_mgmt_list_entry *)
2326 ice_malloc(hw, sizeof(*fm_entry));
2328 status = ICE_ERR_NO_MEMORY;
2329 goto ice_add_mac_exit;
2331 fm_entry->fltr_info = *f_info;
2332 fm_entry->vsi_count = 1;
2333 /* The book keeping entries will get removed when
2334 * base driver calls remove filter AQ command
2337 LIST_ADD(&fm_entry->list_entry, rule_head);
2338 r_iter = (struct ice_aqc_sw_rules_elem *)
2339 ((u8 *)r_iter + s_rule_size);
2344 ice_release_lock(rule_lock);
2346 ice_free(hw, s_rule);
2351 * ice_add_mac - Add a MAC address based filter rule
2352 * @hw: pointer to the hardware structure
2353 * @m_list: list of MAC addresses and forwarding information
2355 * Function add MAC rule for logical port from HW struct
2357 enum ice_status ice_add_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2360 return ICE_ERR_PARAM;
2362 return ice_add_mac_rule(hw, m_list, hw->switch_info,
2363 hw->port_info->lport);
2367 * ice_add_vlan_internal - Add one VLAN based filter rule
2368 * @hw: pointer to the hardware structure
2369 * @recp_list: recipe list for which rule has to be added
2370 * @f_entry: filter entry containing one VLAN information
2372 static enum ice_status
2373 ice_add_vlan_internal(struct ice_hw *hw, struct ice_sw_recipe *recp_list,
2374 struct ice_fltr_list_entry *f_entry)
2376 struct ice_fltr_mgmt_list_entry *v_list_itr;
2377 struct ice_fltr_info *new_fltr, *cur_fltr;
2378 enum ice_sw_lkup_type lkup_type;
2379 u16 vsi_list_id = 0, vsi_handle;
2380 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2381 enum ice_status status = ICE_SUCCESS;
2383 if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
2384 return ICE_ERR_PARAM;
2386 f_entry->fltr_info.fwd_id.hw_vsi_id =
2387 ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
2388 new_fltr = &f_entry->fltr_info;
2390 /* VLAN ID should only be 12 bits */
2391 if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
2392 return ICE_ERR_PARAM;
2394 if (new_fltr->src_id != ICE_SRC_ID_VSI)
2395 return ICE_ERR_PARAM;
2397 new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
2398 lkup_type = new_fltr->lkup_type;
2399 vsi_handle = new_fltr->vsi_handle;
2400 rule_lock = &recp_list->filt_rule_lock;
2401 ice_acquire_lock(rule_lock);
2402 v_list_itr = ice_find_rule_entry(&recp_list->filt_rules, new_fltr);
2404 struct ice_vsi_list_map_info *map_info = NULL;
2406 if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
2407 /* All VLAN pruning rules use a VSI list. Check if
2408 * there is already a VSI list containing VSI that we
2409 * want to add. If found, use the same vsi_list_id for
2410 * this new VLAN rule or else create a new list.
2412 map_info = ice_find_vsi_list_entry(recp_list,
2416 status = ice_create_vsi_list_rule(hw,
2424 /* Convert the action to forwarding to a VSI list. */
2425 new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
2426 new_fltr->fwd_id.vsi_list_id = vsi_list_id;
2429 status = ice_create_pkt_fwd_rule(hw, recp_list, f_entry);
2431 v_list_itr = ice_find_rule_entry(&recp_list->filt_rules,
2434 status = ICE_ERR_DOES_NOT_EXIST;
2437 /* reuse VSI list for new rule and increment ref_cnt */
2439 v_list_itr->vsi_list_info = map_info;
2440 map_info->ref_cnt++;
2442 v_list_itr->vsi_list_info =
2443 ice_create_vsi_list_map(hw, &vsi_handle,
2447 } else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
2448 /* Update existing VSI list to add new VSI ID only if it used
2451 cur_fltr = &v_list_itr->fltr_info;
2452 status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
2455 /* If VLAN rule exists and VSI list being used by this rule is
2456 * referenced by more than 1 VLAN rule. Then create a new VSI
2457 * list appending previous VSI with new VSI and update existing
2458 * VLAN rule to point to new VSI list ID
2460 struct ice_fltr_info tmp_fltr;
2461 u16 vsi_handle_arr[2];
2464 /* Current implementation only supports reusing VSI list with
2465 * one VSI count. We should never hit below condition
2467 if (v_list_itr->vsi_count > 1 &&
2468 v_list_itr->vsi_list_info->ref_cnt > 1) {
2469 ice_debug(hw, ICE_DBG_SW, "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
2470 status = ICE_ERR_CFG;
2475 ice_find_first_bit(v_list_itr->vsi_list_info->vsi_map,
2478 /* A rule already exists with the new VSI being added */
2479 if (cur_handle == vsi_handle) {
2480 status = ICE_ERR_ALREADY_EXISTS;
2484 vsi_handle_arr[0] = cur_handle;
2485 vsi_handle_arr[1] = vsi_handle;
2486 status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
2487 &vsi_list_id, lkup_type);
2491 tmp_fltr = v_list_itr->fltr_info;
2492 tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
2493 tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
2494 tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
2495 /* Update the previous switch rule to a new VSI list which
2496 * includes current VSI that is requested
2498 status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
2502 /* before overriding VSI list map info. decrement ref_cnt of
2505 v_list_itr->vsi_list_info->ref_cnt--;
2507 /* now update to newly created list */
2508 v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
2509 v_list_itr->vsi_list_info =
2510 ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
2512 v_list_itr->vsi_count++;
2516 ice_release_lock(rule_lock);
2521 * ice_add_vlan_rule - Add VLAN based filter rule
2522 * @hw: pointer to the hardware structure
2523 * @v_list: list of VLAN entries and forwarding information
2524 * @sw: pointer to switch info struct for which function add rule
2526 static enum ice_status
2527 ice_add_vlan_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list,
2528 struct ice_switch_info *sw)
2530 struct ice_fltr_list_entry *v_list_itr;
2531 struct ice_sw_recipe *recp_list;
2533 recp_list = &sw->recp_list[ICE_SW_LKUP_VLAN];
2534 LIST_FOR_EACH_ENTRY(v_list_itr, v_list, ice_fltr_list_entry,
2536 if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
2537 return ICE_ERR_PARAM;
2538 v_list_itr->fltr_info.flag = ICE_FLTR_TX;
2539 v_list_itr->status = ice_add_vlan_internal(hw, recp_list,
2541 if (v_list_itr->status)
2542 return v_list_itr->status;
2548 * ice_add_vlan - Add a VLAN based filter rule
2549 * @hw: pointer to the hardware structure
2550 * @v_list: list of VLAN and forwarding information
2552 * Function add VLAN rule for logical port from HW struct
2554 enum ice_status ice_add_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2557 return ICE_ERR_PARAM;
2559 return ice_add_vlan_rule(hw, v_list, hw->switch_info);
2563 * ice_add_eth_mac_rule - Add ethertype and MAC based filter rule
2564 * @hw: pointer to the hardware structure
2565 * @em_list: list of ether type MAC filter, MAC is optional
2566 * @sw: pointer to switch info struct for which function add rule
2567 * @lport: logic port number on which function add rule
2569 * This function requires the caller to populate the entries in
2570 * the filter list with the necessary fields (including flags to
2571 * indicate Tx or Rx rules).
2573 static enum ice_status
2574 ice_add_eth_mac_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list,
2575 struct ice_switch_info *sw, u8 lport)
2577 struct ice_fltr_list_entry *em_list_itr;
2579 LIST_FOR_EACH_ENTRY(em_list_itr, em_list, ice_fltr_list_entry,
2581 struct ice_sw_recipe *recp_list;
2582 enum ice_sw_lkup_type l_type;
2584 l_type = em_list_itr->fltr_info.lkup_type;
2585 recp_list = &sw->recp_list[l_type];
2587 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2588 l_type != ICE_SW_LKUP_ETHERTYPE)
2589 return ICE_ERR_PARAM;
2591 em_list_itr->status = ice_add_rule_internal(hw, recp_list,
2594 if (em_list_itr->status)
2595 return em_list_itr->status;
2601 * ice_add_eth_mac - Add a ethertype based filter rule
2602 * @hw: pointer to the hardware structure
2603 * @em_list: list of ethertype and forwarding information
2605 * Function add ethertype rule for logical port from HW struct
2608 ice_add_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2610 if (!em_list || !hw)
2611 return ICE_ERR_PARAM;
2613 return ice_add_eth_mac_rule(hw, em_list, hw->switch_info,
2614 hw->port_info->lport);
2618 * ice_remove_eth_mac_rule - Remove an ethertype (or MAC) based filter rule
2619 * @hw: pointer to the hardware structure
2620 * @em_list: list of ethertype or ethertype MAC entries
2621 * @sw: pointer to switch info struct for which function add rule
2623 static enum ice_status
2624 ice_remove_eth_mac_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list,
2625 struct ice_switch_info *sw)
2627 struct ice_fltr_list_entry *em_list_itr, *tmp;
2629 LIST_FOR_EACH_ENTRY_SAFE(em_list_itr, tmp, em_list, ice_fltr_list_entry,
2631 struct ice_sw_recipe *recp_list;
2632 enum ice_sw_lkup_type l_type;
2634 l_type = em_list_itr->fltr_info.lkup_type;
2636 if (l_type != ICE_SW_LKUP_ETHERTYPE_MAC &&
2637 l_type != ICE_SW_LKUP_ETHERTYPE)
2638 return ICE_ERR_PARAM;
2640 recp_list = &sw->recp_list[l_type];
2641 em_list_itr->status = ice_remove_rule_internal(hw, recp_list,
2643 if (em_list_itr->status)
2644 return em_list_itr->status;
2650 * ice_remove_eth_mac - remove a ethertype based filter rule
2651 * @hw: pointer to the hardware structure
2652 * @em_list: list of ethertype and forwarding information
2656 ice_remove_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list)
2658 if (!em_list || !hw)
2659 return ICE_ERR_PARAM;
2661 return ice_remove_eth_mac_rule(hw, em_list, hw->switch_info);
2665 * ice_rem_sw_rule_info
2666 * @hw: pointer to the hardware structure
2667 * @rule_head: pointer to the switch list structure that we want to delete
2670 ice_rem_sw_rule_info(struct ice_hw *hw, struct LIST_HEAD_TYPE *rule_head)
2672 if (!LIST_EMPTY(rule_head)) {
2673 struct ice_fltr_mgmt_list_entry *entry;
2674 struct ice_fltr_mgmt_list_entry *tmp;
2676 LIST_FOR_EACH_ENTRY_SAFE(entry, tmp, rule_head,
2677 ice_fltr_mgmt_list_entry, list_entry) {
2678 LIST_DEL(&entry->list_entry);
2679 ice_free(hw, entry);
2685 * ice_rem_all_sw_rules_info
2686 * @hw: pointer to the hardware structure
2688 void ice_rem_all_sw_rules_info(struct ice_hw *hw)
2690 struct ice_switch_info *sw = hw->switch_info;
2693 for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
2694 struct LIST_HEAD_TYPE *rule_head;
2696 rule_head = &sw->recp_list[i].filt_rules;
2697 if (!sw->recp_list[i].adv_rule)
2698 ice_rem_sw_rule_info(hw, rule_head);
2703 * ice_cfg_dflt_vsi - change state of VSI to set/clear default
2704 * @pi: pointer to the port_info structure
2705 * @vsi_handle: VSI handle to set as default
2706 * @set: true to add the above mentioned switch rule, false to remove it
2707 * @direction: ICE_FLTR_RX or ICE_FLTR_TX
2709 * add filter rule to set/unset given VSI as default VSI for the switch
2710 * (represented by swid)
2713 ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
2716 struct ice_aqc_sw_rules_elem *s_rule;
2717 struct ice_fltr_info f_info;
2718 struct ice_hw *hw = pi->hw;
2719 enum ice_adminq_opc opcode;
2720 enum ice_status status;
2724 if (!ice_is_vsi_valid(hw, vsi_handle))
2725 return ICE_ERR_PARAM;
2726 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2728 s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
2729 ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
2731 s_rule = (struct ice_aqc_sw_rules_elem *)ice_malloc(hw, s_rule_size);
2733 return ICE_ERR_NO_MEMORY;
2735 ice_memset(&f_info, 0, sizeof(f_info), ICE_NONDMA_MEM);
2737 f_info.lkup_type = ICE_SW_LKUP_DFLT;
2738 f_info.flag = direction;
2739 f_info.fltr_act = ICE_FWD_TO_VSI;
2740 f_info.fwd_id.hw_vsi_id = hw_vsi_id;
2742 if (f_info.flag & ICE_FLTR_RX) {
2743 f_info.src = pi->lport;
2744 f_info.src_id = ICE_SRC_ID_LPORT;
2746 f_info.fltr_rule_id =
2747 pi->dflt_rx_vsi_rule_id;
2748 } else if (f_info.flag & ICE_FLTR_TX) {
2749 f_info.src_id = ICE_SRC_ID_VSI;
2750 f_info.src = hw_vsi_id;
2752 f_info.fltr_rule_id =
2753 pi->dflt_tx_vsi_rule_id;
2757 opcode = ice_aqc_opc_add_sw_rules;
2759 opcode = ice_aqc_opc_remove_sw_rules;
2761 ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
2763 status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
2764 if (status || !(f_info.flag & ICE_FLTR_TX_RX))
2767 u16 index = LE16_TO_CPU(s_rule->pdata.lkup_tx_rx.index);
2769 if (f_info.flag & ICE_FLTR_TX) {
2770 pi->dflt_tx_vsi_num = hw_vsi_id;
2771 pi->dflt_tx_vsi_rule_id = index;
2772 } else if (f_info.flag & ICE_FLTR_RX) {
2773 pi->dflt_rx_vsi_num = hw_vsi_id;
2774 pi->dflt_rx_vsi_rule_id = index;
2777 if (f_info.flag & ICE_FLTR_TX) {
2778 pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
2779 pi->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
2780 } else if (f_info.flag & ICE_FLTR_RX) {
2781 pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
2782 pi->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
2787 ice_free(hw, s_rule);
2792 * ice_find_ucast_rule_entry - Search for a unicast MAC filter rule entry
2793 * @list_head: head of rule list
2794 * @f_info: rule information
2796 * Helper function to search for a unicast rule entry - this is to be used
2797 * to remove unicast MAC filter that is not shared with other VSIs on the
2800 * Returns pointer to entry storing the rule if found
2802 static struct ice_fltr_mgmt_list_entry *
2803 ice_find_ucast_rule_entry(struct LIST_HEAD_TYPE *list_head,
2804 struct ice_fltr_info *f_info)
2806 struct ice_fltr_mgmt_list_entry *list_itr;
2808 LIST_FOR_EACH_ENTRY(list_itr, list_head, ice_fltr_mgmt_list_entry,
2810 if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
2811 sizeof(f_info->l_data)) &&
2812 f_info->fwd_id.hw_vsi_id ==
2813 list_itr->fltr_info.fwd_id.hw_vsi_id &&
2814 f_info->flag == list_itr->fltr_info.flag)
2821 * ice_remove_mac_rule - remove a MAC based filter rule
2822 * @hw: pointer to the hardware structure
2823 * @m_list: list of MAC addresses and forwarding information
2824 * @recp_list: list from which function remove MAC address
2826 * This function removes either a MAC filter rule or a specific VSI from a
2827 * VSI list for a multicast MAC address.
2829 * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
2830 * ice_add_mac. Caller should be aware that this call will only work if all
2831 * the entries passed into m_list were added previously. It will not attempt to
2832 * do a partial remove of entries that were found.
2834 static enum ice_status
2835 ice_remove_mac_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list,
2836 struct ice_sw_recipe *recp_list)
2838 struct ice_fltr_list_entry *list_itr, *tmp;
2839 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
2842 return ICE_ERR_PARAM;
2844 rule_lock = &recp_list->filt_rule_lock;
2845 LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, m_list, ice_fltr_list_entry,
2847 enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
2848 u8 *add = &list_itr->fltr_info.l_data.mac.mac_addr[0];
2851 if (l_type != ICE_SW_LKUP_MAC)
2852 return ICE_ERR_PARAM;
2854 vsi_handle = list_itr->fltr_info.vsi_handle;
2855 if (!ice_is_vsi_valid(hw, vsi_handle))
2856 return ICE_ERR_PARAM;
2858 list_itr->fltr_info.fwd_id.hw_vsi_id =
2859 ice_get_hw_vsi_num(hw, vsi_handle);
2860 if (IS_UNICAST_ETHER_ADDR(add) && !hw->ucast_shared) {
2861 /* Don't remove the unicast address that belongs to
2862 * another VSI on the switch, since it is not being
2865 ice_acquire_lock(rule_lock);
2866 if (!ice_find_ucast_rule_entry(&recp_list->filt_rules,
2867 &list_itr->fltr_info)) {
2868 ice_release_lock(rule_lock);
2869 return ICE_ERR_DOES_NOT_EXIST;
2871 ice_release_lock(rule_lock);
2873 list_itr->status = ice_remove_rule_internal(hw, recp_list,
2875 if (list_itr->status)
2876 return list_itr->status;
2882 * ice_remove_mac - remove a MAC address based filter rule
2883 * @hw: pointer to the hardware structure
2884 * @m_list: list of MAC addresses and forwarding information
2887 enum ice_status ice_remove_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list)
2889 struct ice_sw_recipe *recp_list;
2891 recp_list = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC];
2892 return ice_remove_mac_rule(hw, m_list, recp_list);
2896 * ice_remove_vlan_rule - Remove VLAN based filter rule
2897 * @hw: pointer to the hardware structure
2898 * @v_list: list of VLAN entries and forwarding information
2899 * @recp_list: list from which function remove VLAN
2901 static enum ice_status
2902 ice_remove_vlan_rule(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list,
2903 struct ice_sw_recipe *recp_list)
2905 struct ice_fltr_list_entry *v_list_itr, *tmp;
2907 LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
2909 enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
2911 if (l_type != ICE_SW_LKUP_VLAN)
2912 return ICE_ERR_PARAM;
2913 v_list_itr->status = ice_remove_rule_internal(hw, recp_list,
2915 if (v_list_itr->status)
2916 return v_list_itr->status;
2922 * ice_remove_vlan - remove a VLAN address based filter rule
2923 * @hw: pointer to the hardware structure
2924 * @v_list: list of VLAN and forwarding information
2928 ice_remove_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list)
2930 struct ice_sw_recipe *recp_list;
2933 return ICE_ERR_PARAM;
2935 recp_list = &hw->switch_info->recp_list[ICE_SW_LKUP_VLAN];
2936 return ice_remove_vlan_rule(hw, v_list, recp_list);
2940 * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
2941 * @fm_entry: filter entry to inspect
2942 * @vsi_handle: VSI handle to compare with filter info
2945 ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
2947 return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
2948 fm_entry->fltr_info.vsi_handle == vsi_handle) ||
2949 (fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
2950 (ice_is_bit_set(fm_entry->vsi_list_info->vsi_map,
2955 * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
2956 * @hw: pointer to the hardware structure
2957 * @vsi_handle: VSI handle to remove filters from
2958 * @vsi_list_head: pointer to the list to add entry to
2959 * @fi: pointer to fltr_info of filter entry to copy & add
2961 * Helper function, used when creating a list of filters to remove from
2962 * a specific VSI. The entry added to vsi_list_head is a COPY of the
2963 * original filter entry, with the exception of fltr_info.fltr_act and
2964 * fltr_info.fwd_id fields. These are set such that later logic can
2965 * extract which VSI to remove the fltr from, and pass on that information.
2967 static enum ice_status
2968 ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
2969 struct LIST_HEAD_TYPE *vsi_list_head,
2970 struct ice_fltr_info *fi)
2972 struct ice_fltr_list_entry *tmp;
2974 /* this memory is freed up in the caller function
2975 * once filters for this VSI are removed
2977 tmp = (struct ice_fltr_list_entry *)ice_malloc(hw, sizeof(*tmp));
2979 return ICE_ERR_NO_MEMORY;
2981 tmp->fltr_info = *fi;
2983 /* Overwrite these fields to indicate which VSI to remove filter from,
2984 * so find and remove logic can extract the information from the
2985 * list entries. Note that original entries will still have proper
2988 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2989 tmp->fltr_info.vsi_handle = vsi_handle;
2990 tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
2992 LIST_ADD(&tmp->list_entry, vsi_list_head);
2998 * ice_add_to_vsi_fltr_list - Add VSI filters to the list
2999 * @hw: pointer to the hardware structure
3000 * @vsi_handle: VSI handle to remove filters from
3001 * @lkup_list_head: pointer to the list that has certain lookup type filters
3002 * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
3004 * Locates all filters in lkup_list_head that are used by the given VSI,
3005 * and adds COPIES of those entries to vsi_list_head (intended to be used
3006 * to remove the listed filters).
3007 * Note that this means all entries in vsi_list_head must be explicitly
3008 * deallocated by the caller when done with list.
3010 static enum ice_status
3011 ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
3012 struct LIST_HEAD_TYPE *lkup_list_head,
3013 struct LIST_HEAD_TYPE *vsi_list_head)
3015 struct ice_fltr_mgmt_list_entry *fm_entry;
3016 enum ice_status status = ICE_SUCCESS;
3018 /* check to make sure VSI ID is valid and within boundary */
3019 if (!ice_is_vsi_valid(hw, vsi_handle))
3020 return ICE_ERR_PARAM;
3022 LIST_FOR_EACH_ENTRY(fm_entry, lkup_list_head,
3023 ice_fltr_mgmt_list_entry, list_entry) {
3024 struct ice_fltr_info *fi;
3026 fi = &fm_entry->fltr_info;
3027 if (!fi || !ice_vsi_uses_fltr(fm_entry, vsi_handle))
3030 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3039 * ice_determine_promisc_mask
3040 * @fi: filter info to parse
3042 * Helper function to determine which ICE_PROMISC_ mask corresponds
3043 * to given filter into.
3045 static u8 ice_determine_promisc_mask(struct ice_fltr_info *fi)
3047 u16 vid = fi->l_data.mac_vlan.vlan_id;
3048 u8 *macaddr = fi->l_data.mac.mac_addr;
3049 bool is_tx_fltr = false;
3050 u8 promisc_mask = 0;
3052 if (fi->flag == ICE_FLTR_TX)
3055 if (IS_BROADCAST_ETHER_ADDR(macaddr))
3056 promisc_mask |= is_tx_fltr ?
3057 ICE_PROMISC_BCAST_TX : ICE_PROMISC_BCAST_RX;
3058 else if (IS_MULTICAST_ETHER_ADDR(macaddr))
3059 promisc_mask |= is_tx_fltr ?
3060 ICE_PROMISC_MCAST_TX : ICE_PROMISC_MCAST_RX;
3061 else if (IS_UNICAST_ETHER_ADDR(macaddr))
3062 promisc_mask |= is_tx_fltr ?
3063 ICE_PROMISC_UCAST_TX : ICE_PROMISC_UCAST_RX;
3065 promisc_mask |= is_tx_fltr ?
3066 ICE_PROMISC_VLAN_TX : ICE_PROMISC_VLAN_RX;
3068 return promisc_mask;
3072 * _ice_get_vsi_promisc - get promiscuous mode of given VSI
3073 * @hw: pointer to the hardware structure
3074 * @vsi_handle: VSI handle to retrieve info from
3075 * @promisc_mask: pointer to mask to be filled in
3076 * @vid: VLAN ID of promisc VLAN VSI
3077 * @sw: pointer to switch info struct for which function add rule
3079 static enum ice_status
3080 _ice_get_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3081 u16 *vid, struct ice_switch_info *sw)
3083 struct ice_fltr_mgmt_list_entry *itr;
3084 struct LIST_HEAD_TYPE *rule_head;
3085 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
3087 if (!ice_is_vsi_valid(hw, vsi_handle))
3088 return ICE_ERR_PARAM;
3092 rule_head = &sw->recp_list[ICE_SW_LKUP_PROMISC].filt_rules;
3093 rule_lock = &sw->recp_list[ICE_SW_LKUP_PROMISC].filt_rule_lock;
3095 ice_acquire_lock(rule_lock);
3096 LIST_FOR_EACH_ENTRY(itr, rule_head,
3097 ice_fltr_mgmt_list_entry, list_entry) {
3098 /* Continue if this filter doesn't apply to this VSI or the
3099 * VSI ID is not in the VSI map for this filter
3101 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3104 *promisc_mask |= ice_determine_promisc_mask(&itr->fltr_info);
3106 ice_release_lock(rule_lock);
3112 * ice_get_vsi_promisc - get promiscuous mode of given VSI
3113 * @hw: pointer to the hardware structure
3114 * @vsi_handle: VSI handle to retrieve info from
3115 * @promisc_mask: pointer to mask to be filled in
3116 * @vid: VLAN ID of promisc VLAN VSI
3119 ice_get_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3122 return _ice_get_vsi_promisc(hw, vsi_handle, promisc_mask,
3123 vid, hw->switch_info);
3127 * ice_get_vsi_vlan_promisc - get VLAN promiscuous mode of given VSI
3128 * @hw: pointer to the hardware structure
3129 * @vsi_handle: VSI handle to retrieve info from
3130 * @promisc_mask: pointer to mask to be filled in
3131 * @vid: VLAN ID of promisc VLAN VSI
3132 * @sw: pointer to switch info struct for which function add rule
3134 static enum ice_status
3135 _ice_get_vsi_vlan_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3136 u16 *vid, struct ice_switch_info *sw)
3138 struct ice_fltr_mgmt_list_entry *itr;
3139 struct LIST_HEAD_TYPE *rule_head;
3140 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
3142 if (!ice_is_vsi_valid(hw, vsi_handle))
3143 return ICE_ERR_PARAM;
3147 rule_head = &sw->recp_list[ICE_SW_LKUP_PROMISC_VLAN].filt_rules;
3148 rule_lock = &sw->recp_list[ICE_SW_LKUP_PROMISC_VLAN].filt_rule_lock;
3150 ice_acquire_lock(rule_lock);
3151 LIST_FOR_EACH_ENTRY(itr, rule_head, ice_fltr_mgmt_list_entry,
3153 /* Continue if this filter doesn't apply to this VSI or the
3154 * VSI ID is not in the VSI map for this filter
3156 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3159 *promisc_mask |= ice_determine_promisc_mask(&itr->fltr_info);
3161 ice_release_lock(rule_lock);
3167 * ice_get_vsi_vlan_promisc - get VLAN promiscuous mode of given VSI
3168 * @hw: pointer to the hardware structure
3169 * @vsi_handle: VSI handle to retrieve info from
3170 * @promisc_mask: pointer to mask to be filled in
3171 * @vid: VLAN ID of promisc VLAN VSI
3174 ice_get_vsi_vlan_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
3177 return _ice_get_vsi_vlan_promisc(hw, vsi_handle, promisc_mask,
3178 vid, hw->switch_info);
3182 * ice_remove_promisc - Remove promisc based filter rules
3183 * @hw: pointer to the hardware structure
3184 * @recp_id: recipe ID for which the rule needs to removed
3185 * @v_list: list of promisc entries
3187 static enum ice_status
3188 ice_remove_promisc(struct ice_hw *hw, u8 recp_id,
3189 struct LIST_HEAD_TYPE *v_list)
3191 struct ice_fltr_list_entry *v_list_itr, *tmp;
3192 struct ice_sw_recipe *recp_list;
3194 recp_list = &hw->switch_info->recp_list[recp_id];
3195 LIST_FOR_EACH_ENTRY_SAFE(v_list_itr, tmp, v_list, ice_fltr_list_entry,
3197 v_list_itr->status =
3198 ice_remove_rule_internal(hw, recp_list, v_list_itr);
3199 if (v_list_itr->status)
3200 return v_list_itr->status;
3206 * _ice_clear_vsi_promisc - clear specified promiscuous mode(s)
3207 * @hw: pointer to the hardware structure
3208 * @vsi_handle: VSI handle to clear mode
3209 * @promisc_mask: mask of promiscuous config bits to clear
3210 * @vid: VLAN ID to clear VLAN promiscuous
3211 * @sw: pointer to switch info struct for which function add rule
3213 static enum ice_status
3214 _ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3215 u16 vid, struct ice_switch_info *sw)
3217 struct ice_fltr_list_entry *fm_entry, *tmp;
3218 struct LIST_HEAD_TYPE remove_list_head;
3219 struct ice_fltr_mgmt_list_entry *itr;
3220 struct LIST_HEAD_TYPE *rule_head;
3221 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
3222 enum ice_status status = ICE_SUCCESS;
3225 if (!ice_is_vsi_valid(hw, vsi_handle))
3226 return ICE_ERR_PARAM;
3228 if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX))
3229 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3231 recipe_id = ICE_SW_LKUP_PROMISC;
3233 rule_head = &sw->recp_list[recipe_id].filt_rules;
3234 rule_lock = &sw->recp_list[recipe_id].filt_rule_lock;
3236 INIT_LIST_HEAD(&remove_list_head);
3238 ice_acquire_lock(rule_lock);
3239 LIST_FOR_EACH_ENTRY(itr, rule_head,
3240 ice_fltr_mgmt_list_entry, list_entry) {
3241 struct ice_fltr_info *fltr_info;
3242 u8 fltr_promisc_mask = 0;
3244 if (!ice_vsi_uses_fltr(itr, vsi_handle))
3246 fltr_info = &itr->fltr_info;
3248 if (recipe_id == ICE_SW_LKUP_PROMISC_VLAN &&
3249 vid != fltr_info->l_data.mac_vlan.vlan_id)
3252 fltr_promisc_mask |= ice_determine_promisc_mask(fltr_info);
3254 /* Skip if filter is not completely specified by given mask */
3255 if (fltr_promisc_mask & ~promisc_mask)
3258 status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
3262 ice_release_lock(rule_lock);
3263 goto free_fltr_list;
3266 ice_release_lock(rule_lock);
3268 status = ice_remove_promisc(hw, recipe_id, &remove_list_head);
3271 LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3272 ice_fltr_list_entry, list_entry) {
3273 LIST_DEL(&fm_entry->list_entry);
3274 ice_free(hw, fm_entry);
3281 * ice_clear_vsi_promisc - clear specified promiscuous mode(s) for given VSI
3282 * @hw: pointer to the hardware structure
3283 * @vsi_handle: VSI handle to clear mode
3284 * @promisc_mask: mask of promiscuous config bits to clear
3285 * @vid: VLAN ID to clear VLAN promiscuous
3288 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle,
3289 u8 promisc_mask, u16 vid)
3291 return _ice_clear_vsi_promisc(hw, vsi_handle, promisc_mask,
3292 vid, hw->switch_info);
3296 * _ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
3297 * @hw: pointer to the hardware structure
3298 * @vsi_handle: VSI handle to configure
3299 * @promisc_mask: mask of promiscuous config bits
3300 * @vid: VLAN ID to set VLAN promiscuous
3301 * @lport: logical port number to configure promisc mode
3302 * @sw: pointer to switch info struct for which function add rule
3304 static enum ice_status
3305 _ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3306 u16 vid, u8 lport, struct ice_switch_info *sw)
3308 enum { UCAST_FLTR = 1, MCAST_FLTR, BCAST_FLTR };
3309 struct ice_fltr_list_entry f_list_entry;
3310 struct ice_fltr_info new_fltr;
3311 enum ice_status status = ICE_SUCCESS;
3317 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
3319 if (!ice_is_vsi_valid(hw, vsi_handle))
3320 return ICE_ERR_PARAM;
3321 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
3323 ice_memset(&new_fltr, 0, sizeof(new_fltr), ICE_NONDMA_MEM);
3325 if (promisc_mask & (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX)) {
3326 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC_VLAN;
3327 new_fltr.l_data.mac_vlan.vlan_id = vid;
3328 recipe_id = ICE_SW_LKUP_PROMISC_VLAN;
3330 new_fltr.lkup_type = ICE_SW_LKUP_PROMISC;
3331 recipe_id = ICE_SW_LKUP_PROMISC;
3334 /* Separate filters must be set for each direction/packet type
3335 * combination, so we will loop over the mask value, store the
3336 * individual type, and clear it out in the input mask as it
3339 while (promisc_mask) {
3340 struct ice_sw_recipe *recp_list;
3346 if (promisc_mask & ICE_PROMISC_UCAST_RX) {
3347 promisc_mask &= ~ICE_PROMISC_UCAST_RX;
3348 pkt_type = UCAST_FLTR;
3349 } else if (promisc_mask & ICE_PROMISC_UCAST_TX) {
3350 promisc_mask &= ~ICE_PROMISC_UCAST_TX;
3351 pkt_type = UCAST_FLTR;
3353 } else if (promisc_mask & ICE_PROMISC_MCAST_RX) {
3354 promisc_mask &= ~ICE_PROMISC_MCAST_RX;
3355 pkt_type = MCAST_FLTR;
3356 } else if (promisc_mask & ICE_PROMISC_MCAST_TX) {
3357 promisc_mask &= ~ICE_PROMISC_MCAST_TX;
3358 pkt_type = MCAST_FLTR;
3360 } else if (promisc_mask & ICE_PROMISC_BCAST_RX) {
3361 promisc_mask &= ~ICE_PROMISC_BCAST_RX;
3362 pkt_type = BCAST_FLTR;
3363 } else if (promisc_mask & ICE_PROMISC_BCAST_TX) {
3364 promisc_mask &= ~ICE_PROMISC_BCAST_TX;
3365 pkt_type = BCAST_FLTR;
3369 /* Check for VLAN promiscuous flag */
3370 if (promisc_mask & ICE_PROMISC_VLAN_RX) {
3371 promisc_mask &= ~ICE_PROMISC_VLAN_RX;
3372 } else if (promisc_mask & ICE_PROMISC_VLAN_TX) {
3373 promisc_mask &= ~ICE_PROMISC_VLAN_TX;
3377 /* Set filter DA based on packet type */
3378 mac_addr = new_fltr.l_data.mac.mac_addr;
3379 if (pkt_type == BCAST_FLTR) {
3380 ice_memset(mac_addr, 0xff, ETH_ALEN, ICE_NONDMA_MEM);
3381 } else if (pkt_type == MCAST_FLTR ||
3382 pkt_type == UCAST_FLTR) {
3383 /* Use the dummy ether header DA */
3384 ice_memcpy(mac_addr, dummy_eth_header, ETH_ALEN,
3385 ICE_NONDMA_TO_NONDMA);
3386 if (pkt_type == MCAST_FLTR)
3387 mac_addr[0] |= 0x1; /* Set multicast bit */
3390 /* Need to reset this to zero for all iterations */
3393 new_fltr.flag |= ICE_FLTR_TX;
3394 new_fltr.src = hw_vsi_id;
3396 new_fltr.flag |= ICE_FLTR_RX;
3397 new_fltr.src = lport;
3400 new_fltr.fltr_act = ICE_FWD_TO_VSI;
3401 new_fltr.vsi_handle = vsi_handle;
3402 new_fltr.fwd_id.hw_vsi_id = hw_vsi_id;
3403 f_list_entry.fltr_info = new_fltr;
3404 recp_list = &sw->recp_list[recipe_id];
3406 status = ice_add_rule_internal(hw, recp_list, lport,
3408 if (status != ICE_SUCCESS)
3409 goto set_promisc_exit;
3417 * ice_set_vsi_promisc - set given VSI to given promiscuous mode(s)
3418 * @hw: pointer to the hardware structure
3419 * @vsi_handle: VSI handle to configure
3420 * @promisc_mask: mask of promiscuous config bits
3421 * @vid: VLAN ID to set VLAN promiscuous
3424 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3427 return _ice_set_vsi_promisc(hw, vsi_handle, promisc_mask, vid,
3428 hw->port_info->lport,
3433 * _ice_set_vlan_vsi_promisc
3434 * @hw: pointer to the hardware structure
3435 * @vsi_handle: VSI handle to configure
3436 * @promisc_mask: mask of promiscuous config bits
3437 * @rm_vlan_promisc: Clear VLANs VSI promisc mode
3438 * @lport: logical port number to configure promisc mode
3439 * @sw: pointer to switch info struct for which function add rule
3441 * Configure VSI with all associated VLANs to given promiscuous mode(s)
3443 static enum ice_status
3444 _ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3445 bool rm_vlan_promisc, u8 lport,
3446 struct ice_switch_info *sw)
3448 struct ice_fltr_list_entry *list_itr, *tmp;
3449 struct LIST_HEAD_TYPE vsi_list_head;
3450 struct LIST_HEAD_TYPE *vlan_head;
3451 struct ice_lock *vlan_lock; /* Lock to protect filter rule list */
3452 enum ice_status status;
3455 INIT_LIST_HEAD(&vsi_list_head);
3456 vlan_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
3457 vlan_head = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rules;
3458 ice_acquire_lock(vlan_lock);
3459 status = ice_add_to_vsi_fltr_list(hw, vsi_handle, vlan_head,
3461 ice_release_lock(vlan_lock);
3463 goto free_fltr_list;
3465 LIST_FOR_EACH_ENTRY(list_itr, &vsi_list_head, ice_fltr_list_entry,
3467 vlan_id = list_itr->fltr_info.l_data.vlan.vlan_id;
3468 if (rm_vlan_promisc)
3469 status = _ice_clear_vsi_promisc(hw, vsi_handle,
3473 status = _ice_set_vsi_promisc(hw, vsi_handle,
3474 promisc_mask, vlan_id,
3481 LIST_FOR_EACH_ENTRY_SAFE(list_itr, tmp, &vsi_list_head,
3482 ice_fltr_list_entry, list_entry) {
3483 LIST_DEL(&list_itr->list_entry);
3484 ice_free(hw, list_itr);
3490 * ice_set_vlan_vsi_promisc
3491 * @hw: pointer to the hardware structure
3492 * @vsi_handle: VSI handle to configure
3493 * @promisc_mask: mask of promiscuous config bits
3494 * @rm_vlan_promisc: Clear VLANs VSI promisc mode
3496 * Configure VSI with all associated VLANs to given promiscuous mode(s)
3499 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
3500 bool rm_vlan_promisc)
3502 return _ice_set_vlan_vsi_promisc(hw, vsi_handle, promisc_mask,
3503 rm_vlan_promisc, hw->port_info->lport,
3508 * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
3509 * @hw: pointer to the hardware structure
3510 * @vsi_handle: VSI handle to remove filters from
3511 * @recp_list: recipe list from which function remove fltr
3512 * @lkup: switch rule filter lookup type
3515 ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
3516 struct ice_sw_recipe *recp_list,
3517 enum ice_sw_lkup_type lkup)
3519 struct ice_fltr_list_entry *fm_entry;
3520 struct LIST_HEAD_TYPE remove_list_head;
3521 struct LIST_HEAD_TYPE *rule_head;
3522 struct ice_fltr_list_entry *tmp;
3523 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
3524 enum ice_status status;
3526 INIT_LIST_HEAD(&remove_list_head);
3527 rule_lock = &recp_list[lkup].filt_rule_lock;
3528 rule_head = &recp_list[lkup].filt_rules;
3529 ice_acquire_lock(rule_lock);
3530 status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
3532 ice_release_lock(rule_lock);
3537 case ICE_SW_LKUP_MAC:
3538 ice_remove_mac_rule(hw, &remove_list_head, &recp_list[lkup]);
3540 case ICE_SW_LKUP_VLAN:
3541 ice_remove_vlan_rule(hw, &remove_list_head, &recp_list[lkup]);
3543 case ICE_SW_LKUP_PROMISC:
3544 case ICE_SW_LKUP_PROMISC_VLAN:
3545 ice_remove_promisc(hw, lkup, &remove_list_head);
3547 case ICE_SW_LKUP_MAC_VLAN:
3548 ice_debug(hw, ICE_DBG_SW, "MAC VLAN look up is not supported yet\n");
3550 case ICE_SW_LKUP_ETHERTYPE:
3551 case ICE_SW_LKUP_ETHERTYPE_MAC:
3552 ice_remove_eth_mac(hw, &remove_list_head);
3554 case ICE_SW_LKUP_DFLT:
3555 ice_debug(hw, ICE_DBG_SW, "Remove filters for this lookup type hasn't been implemented yet\n");
3557 case ICE_SW_LKUP_LAST:
3558 ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type\n");
3562 LIST_FOR_EACH_ENTRY_SAFE(fm_entry, tmp, &remove_list_head,
3563 ice_fltr_list_entry, list_entry) {
3564 LIST_DEL(&fm_entry->list_entry);
3565 ice_free(hw, fm_entry);
3570 * ice_remove_vsi_fltr_rule - Remove all filters for a VSI
3571 * @hw: pointer to the hardware structure
3572 * @vsi_handle: VSI handle to remove filters from
3573 * @sw: pointer to switch info struct
3576 ice_remove_vsi_fltr_rule(struct ice_hw *hw, u16 vsi_handle,
3577 struct ice_switch_info *sw)
3579 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
3581 ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3582 sw->recp_list, ICE_SW_LKUP_MAC);
3583 ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3584 sw->recp_list, ICE_SW_LKUP_MAC_VLAN);
3585 ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3586 sw->recp_list, ICE_SW_LKUP_PROMISC);
3587 ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3588 sw->recp_list, ICE_SW_LKUP_VLAN);
3589 ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3590 sw->recp_list, ICE_SW_LKUP_DFLT);
3591 ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3592 sw->recp_list, ICE_SW_LKUP_ETHERTYPE);
3593 ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3594 sw->recp_list, ICE_SW_LKUP_ETHERTYPE_MAC);
3595 ice_remove_vsi_lkup_fltr(hw, vsi_handle,
3596 sw->recp_list, ICE_SW_LKUP_PROMISC_VLAN);
3600 * ice_remove_vsi_fltr - Remove all filters for a VSI
3601 * @hw: pointer to the hardware structure
3602 * @vsi_handle: VSI handle to remove filters from
3604 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
3606 ice_remove_vsi_fltr_rule(hw, vsi_handle, hw->switch_info);
3610 * ice_alloc_res_cntr - allocating resource counter
3611 * @hw: pointer to the hardware structure
3612 * @type: type of resource
3613 * @alloc_shared: if set it is shared else dedicated
3614 * @num_items: number of entries requested for FD resource type
3615 * @counter_id: counter index returned by AQ call
3617 static enum ice_status
3618 ice_alloc_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3621 struct ice_aqc_alloc_free_res_elem *buf;
3622 enum ice_status status;
3625 /* Allocate resource */
3626 buf_len = ice_struct_size(buf, elem, 1);
3627 buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
3629 return ICE_ERR_NO_MEMORY;
3631 buf->num_elems = CPU_TO_LE16(num_items);
3632 buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3633 ICE_AQC_RES_TYPE_M) | alloc_shared);
3635 status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3636 ice_aqc_opc_alloc_res, NULL);
3640 *counter_id = LE16_TO_CPU(buf->elem[0].e.sw_resp);
3648 * ice_free_res_cntr - free resource counter
3649 * @hw: pointer to the hardware structure
3650 * @type: type of resource
3651 * @alloc_shared: if set it is shared else dedicated
3652 * @num_items: number of entries to be freed for FD resource type
3653 * @counter_id: counter ID resource which needs to be freed
3655 static enum ice_status
3656 ice_free_res_cntr(struct ice_hw *hw, u8 type, u8 alloc_shared, u16 num_items,
3659 struct ice_aqc_alloc_free_res_elem *buf;
3660 enum ice_status status;
3664 buf_len = ice_struct_size(buf, elem, 1);
3665 buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
3667 return ICE_ERR_NO_MEMORY;
3669 buf->num_elems = CPU_TO_LE16(num_items);
3670 buf->res_type = CPU_TO_LE16(((type << ICE_AQC_RES_TYPE_S) &
3671 ICE_AQC_RES_TYPE_M) | alloc_shared);
3672 buf->elem[0].e.sw_resp = CPU_TO_LE16(counter_id);
3674 status = ice_aq_alloc_free_res(hw, 1, buf, buf_len,
3675 ice_aqc_opc_free_res, NULL);
3677 ice_debug(hw, ICE_DBG_SW, "counter resource could not be freed\n");
3684 * ice_alloc_vlan_res_counter - obtain counter resource for VLAN type
3685 * @hw: pointer to the hardware structure
3686 * @counter_id: returns counter index
3688 enum ice_status ice_alloc_vlan_res_counter(struct ice_hw *hw, u16 *counter_id)
3690 return ice_alloc_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3691 ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3696 * ice_free_vlan_res_counter - Free counter resource for VLAN type
3697 * @hw: pointer to the hardware structure
3698 * @counter_id: counter index to be freed
3700 enum ice_status ice_free_vlan_res_counter(struct ice_hw *hw, u16 counter_id)
3702 return ice_free_res_cntr(hw, ICE_AQC_RES_TYPE_VLAN_COUNTER,
3703 ICE_AQC_RES_TYPE_FLAG_DEDICATED, 1,
3708 * ice_alloc_res_lg_act - add large action resource
3709 * @hw: pointer to the hardware structure
3710 * @l_id: large action ID to fill it in
3711 * @num_acts: number of actions to hold with a large action entry
3713 static enum ice_status
3714 ice_alloc_res_lg_act(struct ice_hw *hw, u16 *l_id, u16 num_acts)
3716 struct ice_aqc_alloc_free_res_elem *sw_buf;
3717 enum ice_status status;
3720 if (num_acts > ICE_MAX_LG_ACT || num_acts == 0)
3721 return ICE_ERR_PARAM;
3723 /* Allocate resource for large action */
3724 buf_len = ice_struct_size(sw_buf, elem, 1);
3725 sw_buf = (struct ice_aqc_alloc_free_res_elem *)ice_malloc(hw, buf_len);
3727 return ICE_ERR_NO_MEMORY;
3729 sw_buf->num_elems = CPU_TO_LE16(1);
3731 /* If num_acts is 1, use ICE_AQC_RES_TYPE_WIDE_TABLE_1.
3732 * If num_acts is 2, use ICE_AQC_RES_TYPE_WIDE_TABLE_3.
3733 * If num_acts is greater than 2, then use
3734 * ICE_AQC_RES_TYPE_WIDE_TABLE_4.
3735 * The num_acts cannot exceed 4. This was ensured at the
3736 * beginning of the function.
3739 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_1);
3740 else if (num_acts == 2)
3741 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_2);
3743 sw_buf->res_type = CPU_TO_LE16(ICE_AQC_RES_TYPE_WIDE_TABLE_4);
3745 status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len,
3746 ice_aqc_opc_alloc_res, NULL);
3748 *l_id = LE16_TO_CPU(sw_buf->elem[0].e.sw_resp);
3750 ice_free(hw, sw_buf);
3755 * ice_add_mac_with_sw_marker - add filter with sw marker
3756 * @hw: pointer to the hardware structure
3757 * @f_info: filter info structure containing the MAC filter information
3758 * @sw_marker: sw marker to tag the Rx descriptor with
3761 ice_add_mac_with_sw_marker(struct ice_hw *hw, struct ice_fltr_info *f_info,
3764 struct ice_fltr_mgmt_list_entry *m_entry;
3765 struct ice_fltr_list_entry fl_info;
3766 struct ice_sw_recipe *recp_list;
3767 struct LIST_HEAD_TYPE l_head;
3768 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
3769 enum ice_status ret;
3773 if (f_info->fltr_act != ICE_FWD_TO_VSI)
3774 return ICE_ERR_PARAM;
3776 if (f_info->lkup_type != ICE_SW_LKUP_MAC)
3777 return ICE_ERR_PARAM;
3779 if (sw_marker == ICE_INVAL_SW_MARKER_ID)
3780 return ICE_ERR_PARAM;
3782 if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
3783 return ICE_ERR_PARAM;
3784 f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
3786 /* Add filter if it doesn't exist so then the adding of large
3787 * action always results in update
3790 INIT_LIST_HEAD(&l_head);
3791 fl_info.fltr_info = *f_info;
3792 LIST_ADD(&fl_info.list_entry, &l_head);
3794 entry_exists = false;
3795 ret = ice_add_mac_rule(hw, &l_head, hw->switch_info,
3796 hw->port_info->lport);
3797 if (ret == ICE_ERR_ALREADY_EXISTS)
3798 entry_exists = true;
3802 recp_list = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC];
3803 rule_lock = &recp_list->filt_rule_lock;
3804 ice_acquire_lock(rule_lock);
3805 /* Get the book keeping entry for the filter */
3806 m_entry = ice_find_rule_entry(&recp_list->filt_rules, f_info);
3810 /* If counter action was enabled for this rule then don't enable
3811 * sw marker large action
3813 if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
3814 ret = ICE_ERR_PARAM;
3818 /* if same marker was added before */
3819 if (m_entry->sw_marker_id == sw_marker) {
3820 ret = ICE_ERR_ALREADY_EXISTS;
3824 /* Allocate a hardware table entry to hold large act. Three actions
3825 * for marker based large action
3827 ret = ice_alloc_res_lg_act(hw, &lg_act_id, 3);
3831 if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
3834 /* Update the switch rule to add the marker action */
3835 ret = ice_add_marker_act(hw, m_entry, sw_marker, lg_act_id);
3837 ice_release_lock(rule_lock);
3842 ice_release_lock(rule_lock);
3843 /* only remove entry if it did not exist previously */
3845 ret = ice_remove_mac(hw, &l_head);
3851 * ice_add_mac_with_counter - add filter with counter enabled
3852 * @hw: pointer to the hardware structure
3853 * @f_info: pointer to filter info structure containing the MAC filter
3857 ice_add_mac_with_counter(struct ice_hw *hw, struct ice_fltr_info *f_info)
3859 struct ice_fltr_mgmt_list_entry *m_entry;
3860 struct ice_fltr_list_entry fl_info;
3861 struct ice_sw_recipe *recp_list;
3862 struct LIST_HEAD_TYPE l_head;
3863 struct ice_lock *rule_lock; /* Lock to protect filter rule list */
3864 enum ice_status ret;
3869 if (f_info->fltr_act != ICE_FWD_TO_VSI)
3870 return ICE_ERR_PARAM;
3872 if (f_info->lkup_type != ICE_SW_LKUP_MAC)
3873 return ICE_ERR_PARAM;
3875 if (!ice_is_vsi_valid(hw, f_info->vsi_handle))
3876 return ICE_ERR_PARAM;
3877 f_info->fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, f_info->vsi_handle);
3878 recp_list = &hw->switch_info->recp_list[ICE_SW_LKUP_MAC];
3880 entry_exist = false;
3882 rule_lock = &recp_list->filt_rule_lock;
3884 /* Add filter if it doesn't exist so then the adding of large
3885 * action always results in update
3887 INIT_LIST_HEAD(&l_head);
3889 fl_info.fltr_info = *f_info;
3890 LIST_ADD(&fl_info.list_entry, &l_head);
3892 ret = ice_add_mac_rule(hw, &l_head, hw->switch_info,
3893 hw->port_info->lport);
3894 if (ret == ICE_ERR_ALREADY_EXISTS)
3899 ice_acquire_lock(rule_lock);
3900 m_entry = ice_find_rule_entry(&recp_list->filt_rules, f_info);
3902 ret = ICE_ERR_BAD_PTR;
3906 /* Don't enable counter for a filter for which sw marker was enabled */
3907 if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID) {
3908 ret = ICE_ERR_PARAM;
3912 /* If a counter was already enabled then don't need to add again */
3913 if (m_entry->counter_index != ICE_INVAL_COUNTER_ID) {
3914 ret = ICE_ERR_ALREADY_EXISTS;
3918 /* Allocate a hardware table entry to VLAN counter */
3919 ret = ice_alloc_vlan_res_counter(hw, &counter_id);
3923 /* Allocate a hardware table entry to hold large act. Two actions for
3924 * counter based large action
3926 ret = ice_alloc_res_lg_act(hw, &lg_act_id, 2);
3930 if (lg_act_id == ICE_INVAL_LG_ACT_INDEX)
3933 /* Update the switch rule to add the counter action */
3934 ret = ice_add_counter_act(hw, m_entry, counter_id, lg_act_id);
3936 ice_release_lock(rule_lock);
3941 ice_release_lock(rule_lock);
3942 /* only remove entry if it did not exist previously */
3944 ret = ice_remove_mac(hw, &l_head);
3950 * ice_replay_fltr - Replay all the filters stored by a specific list head
3951 * @hw: pointer to the hardware structure
3952 * @list_head: list for which filters needs to be replayed
3953 * @recp_id: Recipe ID for which rules need to be replayed
3955 static enum ice_status
3956 ice_replay_fltr(struct ice_hw *hw, u8 recp_id, struct LIST_HEAD_TYPE *list_head)
3958 struct ice_fltr_mgmt_list_entry *itr;
3959 enum ice_status status = ICE_SUCCESS;
3960 struct ice_sw_recipe *recp_list;
3961 u8 lport = hw->port_info->lport;
3962 struct LIST_HEAD_TYPE l_head;
3964 if (LIST_EMPTY(list_head))
3967 recp_list = &hw->switch_info->recp_list[recp_id];
3968 /* Move entries from the given list_head to a temporary l_head so that
3969 * they can be replayed. Otherwise when trying to re-add the same
3970 * filter, the function will return already exists
3972 LIST_REPLACE_INIT(list_head, &l_head);
3974 /* Mark the given list_head empty by reinitializing it so filters
3975 * could be added again by *handler
3977 LIST_FOR_EACH_ENTRY(itr, &l_head, ice_fltr_mgmt_list_entry,
3979 struct ice_fltr_list_entry f_entry;
3982 f_entry.fltr_info = itr->fltr_info;
3983 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN) {
3984 status = ice_add_rule_internal(hw, recp_list, lport,
3986 if (status != ICE_SUCCESS)
3991 /* Add a filter per VSI separately */
3992 ice_for_each_set_bit(vsi_handle, itr->vsi_list_info->vsi_map,
3994 if (!ice_is_vsi_valid(hw, vsi_handle))
3997 ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
3998 f_entry.fltr_info.vsi_handle = vsi_handle;
3999 f_entry.fltr_info.fwd_id.hw_vsi_id =
4000 ice_get_hw_vsi_num(hw, vsi_handle);
4001 f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
4002 if (recp_id == ICE_SW_LKUP_VLAN)
4003 status = ice_add_vlan_internal(hw, recp_list,
4006 status = ice_add_rule_internal(hw, recp_list,
4009 if (status != ICE_SUCCESS)
4014 /* Clear the filter management list */
4015 ice_rem_sw_rule_info(hw, &l_head);
4020 * ice_replay_all_fltr - replay all filters stored in bookkeeping lists
4021 * @hw: pointer to the hardware structure
4023 * NOTE: This function does not clean up partially added filters on error.
4024 * It is up to caller of the function to issue a reset or fail early.
4026 enum ice_status ice_replay_all_fltr(struct ice_hw *hw)
4028 struct ice_switch_info *sw = hw->switch_info;
4029 enum ice_status status = ICE_SUCCESS;
4032 for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4033 struct LIST_HEAD_TYPE *head = &sw->recp_list[i].filt_rules;
4035 status = ice_replay_fltr(hw, i, head);
4036 if (status != ICE_SUCCESS)
4043 * ice_replay_vsi_fltr - Replay filters for requested VSI
4044 * @hw: pointer to the hardware structure
4045 * @pi: pointer to port information structure
4046 * @sw: pointer to switch info struct for which function replays filters
4047 * @vsi_handle: driver VSI handle
4048 * @recp_id: Recipe ID for which rules need to be replayed
4049 * @list_head: list for which filters need to be replayed
4051 * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
4052 * It is required to pass valid VSI handle.
4054 static enum ice_status
4055 ice_replay_vsi_fltr(struct ice_hw *hw, struct ice_port_info *pi,
4056 struct ice_switch_info *sw, u16 vsi_handle, u8 recp_id,
4057 struct LIST_HEAD_TYPE *list_head)
4059 struct ice_fltr_mgmt_list_entry *itr;
4060 enum ice_status status = ICE_SUCCESS;
4061 struct ice_sw_recipe *recp_list;
4064 if (LIST_EMPTY(list_head))
4066 recp_list = &sw->recp_list[recp_id];
4067 hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
4069 LIST_FOR_EACH_ENTRY(itr, list_head, ice_fltr_mgmt_list_entry,
4071 struct ice_fltr_list_entry f_entry;
4073 f_entry.fltr_info = itr->fltr_info;
4074 if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
4075 itr->fltr_info.vsi_handle == vsi_handle) {
4076 /* update the src in case it is VSI num */
4077 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
4078 f_entry.fltr_info.src = hw_vsi_id;
4079 status = ice_add_rule_internal(hw, recp_list,
4082 if (status != ICE_SUCCESS)
4086 if (!itr->vsi_list_info ||
4087 !ice_is_bit_set(itr->vsi_list_info->vsi_map, vsi_handle))
4089 /* Clearing it so that the logic can add it back */
4090 ice_clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
4091 f_entry.fltr_info.vsi_handle = vsi_handle;
4092 f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
4093 /* update the src in case it is VSI num */
4094 if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
4095 f_entry.fltr_info.src = hw_vsi_id;
4096 if (recp_id == ICE_SW_LKUP_VLAN)
4097 status = ice_add_vlan_internal(hw, recp_list, &f_entry);
4099 status = ice_add_rule_internal(hw, recp_list,
4102 if (status != ICE_SUCCESS)
4110 * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
4111 * @hw: pointer to the hardware structure
4112 * @pi: pointer to port information structure
4113 * @vsi_handle: driver VSI handle
4115 * Replays filters for requested VSI via vsi_handle.
4118 ice_replay_vsi_all_fltr(struct ice_hw *hw, struct ice_port_info *pi,
4121 struct ice_switch_info *sw = hw->switch_info;
4122 enum ice_status status = ICE_SUCCESS;
4125 /* Update the recipes that were created */
4126 for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4127 struct LIST_HEAD_TYPE *head;
4129 head = &sw->recp_list[i].filt_replay_rules;
4130 if (!sw->recp_list[i].adv_rule)
4131 status = ice_replay_vsi_fltr(hw, pi, sw, vsi_handle, i,
4133 if (status != ICE_SUCCESS)
4141 * ice_rm_all_sw_replay_rule - helper function to delete filter replay rules
4142 * @hw: pointer to the HW struct
4143 * @sw: pointer to switch info struct for which function removes filters
4145 * Deletes the filter replay rules for given switch
4147 void ice_rm_sw_replay_rule_info(struct ice_hw *hw, struct ice_switch_info *sw)
4154 for (i = 0; i < ICE_MAX_NUM_RECIPES; i++) {
4155 if (!LIST_EMPTY(&sw->recp_list[i].filt_replay_rules)) {
4156 struct LIST_HEAD_TYPE *l_head;
4158 l_head = &sw->recp_list[i].filt_replay_rules;
4159 if (!sw->recp_list[i].adv_rule)
4160 ice_rem_sw_rule_info(hw, l_head);
4166 * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
4167 * @hw: pointer to the HW struct
4169 * Deletes the filter replay rules.
4171 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
4173 ice_rm_sw_replay_rule_info(hw, hw->switch_info);