1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2021 Broadcom
6 #include <rte_malloc.h>
8 #include "bnxt_tf_common.h"
10 #include "ulp_template_struct.h"
11 #include "ulp_mapper.h"
12 #include "ulp_flow_db.h"
13 #include "ulp_fc_mgr.h"
16 #define ULP_FLOW_DB_RES_DIR_BIT 31
17 #define ULP_FLOW_DB_RES_DIR_MASK 0x80000000
18 #define ULP_FLOW_DB_RES_FUNC_BITS 28
19 #define ULP_FLOW_DB_RES_FUNC_MASK 0x70000000
20 #define ULP_FLOW_DB_RES_NXT_MASK 0x0FFFFFFF
21 #define ULP_FLOW_DB_RES_FUNC_UPPER 5
22 #define ULP_FLOW_DB_RES_FUNC_NEED_LOWER 0x80
23 #define ULP_FLOW_DB_RES_FUNC_LOWER_MASK 0x1F
25 /* Macro to copy the nxt_resource_idx */
26 #define ULP_FLOW_DB_RES_NXT_SET(dst, src) {(dst) |= ((src) &\
27 ULP_FLOW_DB_RES_NXT_MASK); }
28 #define ULP_FLOW_DB_RES_NXT_RESET(dst) ((dst) &= ~(ULP_FLOW_DB_RES_NXT_MASK))
31 * Helper function to set the bit in the active flows
32 * No validation is done in this function.
34 * flow_db [in] Ptr to flow database
35 * flow_type [in] - specify default or regular
36 * idx [in] The index to bit to be set or reset.
37 * flag [in] 1 to set and 0 to reset.
42 ulp_flow_db_active_flows_bit_set(struct bnxt_ulp_flow_db *flow_db,
43 enum bnxt_ulp_fdb_type flow_type,
47 struct bnxt_ulp_flow_tbl *f_tbl = &flow_db->flow_tbl;
48 uint32_t a_idx = idx / ULP_INDEX_BITMAP_SIZE;
51 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
52 ULP_INDEX_BITMAP_SET(f_tbl->active_reg_flows[a_idx],
55 ULP_INDEX_BITMAP_SET(f_tbl->active_dflt_flows[a_idx],
58 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
59 ULP_INDEX_BITMAP_RESET(f_tbl->active_reg_flows[a_idx],
62 ULP_INDEX_BITMAP_RESET(f_tbl->active_dflt_flows[a_idx],
68 * Helper function to check if given fid is active flow.
69 * No validation being done in this function.
71 * flow_db [in] Ptr to flow database
72 * flow_type [in] - specify default or regular
73 * idx [in] The index to bit to be set or reset.
75 * returns 1 on set or 0 if not set.
78 ulp_flow_db_active_flows_bit_is_set(struct bnxt_ulp_flow_db *flow_db,
79 enum bnxt_ulp_fdb_type flow_type,
82 struct bnxt_ulp_flow_tbl *f_tbl = &flow_db->flow_tbl;
83 uint32_t a_idx = idx / ULP_INDEX_BITMAP_SIZE;
85 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
86 return ULP_INDEX_BITMAP_GET(f_tbl->active_reg_flows[a_idx],
89 return ULP_INDEX_BITMAP_GET(f_tbl->active_dflt_flows[a_idx],
93 static inline enum tf_dir
94 ulp_flow_db_resource_dir_get(struct ulp_fdb_resource_info *res_info)
96 return ((res_info->nxt_resource_idx & ULP_FLOW_DB_RES_DIR_MASK) >>
97 ULP_FLOW_DB_RES_DIR_BIT);
101 ulp_flow_db_resource_func_get(struct ulp_fdb_resource_info *res_info)
105 func = (((res_info->nxt_resource_idx & ULP_FLOW_DB_RES_FUNC_MASK) >>
106 ULP_FLOW_DB_RES_FUNC_BITS) << ULP_FLOW_DB_RES_FUNC_UPPER);
107 /* The reource func is split into upper and lower */
108 if (func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER)
109 return (func | res_info->resource_func_lower);
114 * Helper function to copy the resource params to resource info
115 * No validation being done in this function.
117 * resource_info [out] Ptr to resource information
118 * params [in] The input params from the caller
122 ulp_flow_db_res_params_to_info(struct ulp_fdb_resource_info *resource_info,
123 struct ulp_flow_db_res_params *params)
125 uint32_t resource_func;
127 resource_info->nxt_resource_idx |= ((params->direction <<
128 ULP_FLOW_DB_RES_DIR_BIT) &
129 ULP_FLOW_DB_RES_DIR_MASK);
130 resource_func = (params->resource_func >> ULP_FLOW_DB_RES_FUNC_UPPER);
131 resource_info->nxt_resource_idx |= ((resource_func <<
132 ULP_FLOW_DB_RES_FUNC_BITS) &
133 ULP_FLOW_DB_RES_FUNC_MASK);
135 if (params->resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
136 /* Break the resource func into two parts */
137 resource_func = (params->resource_func &
138 ULP_FLOW_DB_RES_FUNC_LOWER_MASK);
139 resource_info->resource_func_lower = resource_func;
142 /* Store the handle as 64bit only for EM table entries */
143 if (params->resource_func != BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE &&
144 params->resource_func != BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
145 resource_info->resource_hndl = (uint32_t)params->resource_hndl;
146 resource_info->resource_type = params->resource_type;
147 resource_info->resource_sub_type = params->resource_sub_type;
148 resource_info->reserved = params->reserved;
150 resource_info->resource_em_handle = params->resource_hndl;
155 * Helper function to copy the resource params to resource info
156 * No validation being done in this function.
158 * resource_info [in] Ptr to resource information
159 * params [out] The output params to the caller
164 ulp_flow_db_res_info_to_params(struct ulp_fdb_resource_info *resource_info,
165 struct ulp_flow_db_res_params *params)
167 memset(params, 0, sizeof(struct ulp_flow_db_res_params));
169 /* use the helper function to get the resource func */
170 params->direction = ulp_flow_db_resource_dir_get(resource_info);
171 params->resource_func = ulp_flow_db_resource_func_get(resource_info);
173 if (params->resource_func == BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE ||
174 params->resource_func == BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
175 params->resource_hndl = resource_info->resource_em_handle;
176 } else if (params->resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
177 params->resource_hndl = resource_info->resource_hndl;
178 params->resource_type = resource_info->resource_type;
179 params->resource_sub_type = resource_info->resource_sub_type;
180 params->reserved = resource_info->reserved;
185 * Helper function to allocate the flow table and initialize
186 * the stack for allocation operations.
188 * flow_db [in] Ptr to flow database structure
190 * Returns 0 on success or negative number on failure.
193 ulp_flow_db_alloc_resource(struct bnxt_ulp_flow_db *flow_db)
196 struct bnxt_ulp_flow_tbl *flow_tbl;
199 flow_tbl = &flow_db->flow_tbl;
201 size = sizeof(struct ulp_fdb_resource_info) * flow_tbl->num_resources;
202 flow_tbl->flow_resources =
203 rte_zmalloc("ulp_fdb_resource_info", size, 0);
205 if (!flow_tbl->flow_resources) {
206 BNXT_TF_DBG(ERR, "Failed to alloc memory for flow table\n");
209 size = sizeof(uint32_t) * flow_tbl->num_resources;
210 flow_tbl->flow_tbl_stack = rte_zmalloc("flow_tbl_stack", size, 0);
211 if (!flow_tbl->flow_tbl_stack) {
212 BNXT_TF_DBG(ERR, "Failed to alloc memory flow tbl stack\n");
215 size = (flow_tbl->num_flows / sizeof(uint64_t)) + 1;
216 size = ULP_BYTE_ROUND_OFF_8(size);
217 flow_tbl->active_reg_flows = rte_zmalloc("active reg flows", size,
218 ULP_BUFFER_ALIGN_64_BYTE);
219 if (!flow_tbl->active_reg_flows) {
220 BNXT_TF_DBG(ERR, "Failed to alloc memory active reg flows\n");
224 flow_tbl->active_dflt_flows = rte_zmalloc("active dflt flows", size,
225 ULP_BUFFER_ALIGN_64_BYTE);
226 if (!flow_tbl->active_dflt_flows) {
227 BNXT_TF_DBG(ERR, "Failed to alloc memory active dflt flows\n");
231 /* Initialize the stack table. */
232 for (idx = 0; idx < flow_tbl->num_resources; idx++)
233 flow_tbl->flow_tbl_stack[idx] = idx;
235 /* Ignore the first element in the list. */
236 flow_tbl->head_index = 1;
237 /* Tail points to the last entry in the list. */
238 flow_tbl->tail_index = flow_tbl->num_resources - 1;
243 * Helper function to deallocate the flow table.
245 * flow_db [in] Ptr to flow database structure
250 ulp_flow_db_dealloc_resource(struct bnxt_ulp_flow_db *flow_db)
252 struct bnxt_ulp_flow_tbl *flow_tbl = &flow_db->flow_tbl;
254 /* Free all the allocated tables in the flow table. */
255 if (flow_tbl->active_reg_flows) {
256 rte_free(flow_tbl->active_reg_flows);
257 flow_tbl->active_reg_flows = NULL;
259 if (flow_tbl->active_dflt_flows) {
260 rte_free(flow_tbl->active_dflt_flows);
261 flow_tbl->active_dflt_flows = NULL;
264 if (flow_tbl->flow_tbl_stack) {
265 rte_free(flow_tbl->flow_tbl_stack);
266 flow_tbl->flow_tbl_stack = NULL;
269 if (flow_tbl->flow_resources) {
270 rte_free(flow_tbl->flow_resources);
271 flow_tbl->flow_resources = NULL;
276 * Helper function to add function id to the flow table
278 * flow_db [in] Ptr to flow table
279 * flow_id [in] The flow id of the flow
280 * func_id [in] The func_id to be set, for reset pass zero
285 ulp_flow_db_func_id_set(struct bnxt_ulp_flow_db *flow_db,
289 /* set the function id in the function table */
290 if (flow_id < flow_db->func_id_tbl_size)
291 flow_db->func_id_tbl[flow_id] = func_id;
292 else /* This should never happen */
293 BNXT_TF_DBG(ERR, "Invalid flow id, flowdb corrupt\n");
297 * Initialize the parent-child database. Memory is allocated in this
298 * call and assigned to the database
300 * flow_db [in] Ptr to flow table
301 * num_entries[in] - number of entries to allocate
303 * Returns 0 on success or negative number on failure.
306 ulp_flow_db_parent_tbl_init(struct bnxt_ulp_flow_db *flow_db,
307 uint32_t num_entries)
309 struct ulp_fdb_parent_child_db *p_db;
315 /* update the sizes for the allocation */
316 p_db = &flow_db->parent_child_db;
317 p_db->child_bitset_size = (flow_db->flow_tbl.num_flows /
318 sizeof(uint64_t)) + 1; /* size in bytes */
319 p_db->child_bitset_size = ULP_BYTE_ROUND_OFF_8(p_db->child_bitset_size);
320 p_db->entries_count = num_entries;
322 /* allocate the memory */
323 p_db->parent_flow_tbl = rte_zmalloc("fdb parent flow tbl",
324 sizeof(struct ulp_fdb_parent_info) *
325 p_db->entries_count, 0);
326 if (!p_db->parent_flow_tbl) {
328 "Failed to allocate memory fdb parent flow tbl\n");
331 size = p_db->child_bitset_size * p_db->entries_count;
334 * allocate the big chunk of memory to be statically carved into
335 * child_fid_bitset pointer.
337 p_db->parent_flow_tbl_mem = rte_zmalloc("fdb parent flow tbl mem",
339 ULP_BUFFER_ALIGN_64_BYTE);
340 if (!p_db->parent_flow_tbl_mem) {
342 "Failed to allocate memory fdb parent flow mem\n");
346 /* set the pointers in parent table to their offsets */
347 for (idx = 0 ; idx < p_db->entries_count; idx++) {
348 p_db->parent_flow_tbl[idx].child_fid_bitset =
349 (uint64_t *)&p_db->parent_flow_tbl_mem[idx *
350 p_db->child_bitset_size];
357 * Deinitialize the parent-child database. Memory is deallocated in
358 * this call and all flows should have been purged before this
361 * flow_db [in] Ptr to flow table
366 ulp_flow_db_parent_tbl_deinit(struct bnxt_ulp_flow_db *flow_db)
368 /* free the memory related to parent child database */
369 if (flow_db->parent_child_db.parent_flow_tbl_mem) {
370 rte_free(flow_db->parent_child_db.parent_flow_tbl_mem);
371 flow_db->parent_child_db.parent_flow_tbl_mem = NULL;
373 if (flow_db->parent_child_db.parent_flow_tbl) {
374 rte_free(flow_db->parent_child_db.parent_flow_tbl);
375 flow_db->parent_child_db.parent_flow_tbl = NULL;
379 /* internal validation function for parent flow tbl */
380 static struct bnxt_ulp_flow_db *
381 ulp_flow_db_parent_arg_validation(struct bnxt_ulp_context *ulp_ctxt,
384 struct bnxt_ulp_flow_db *flow_db;
386 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
388 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
392 /* check for max flows */
393 if (fid >= flow_db->flow_tbl.num_flows || !fid) {
394 BNXT_TF_DBG(ERR, "Invalid flow index\n");
398 /* No support for parent child db then just exit */
399 if (!flow_db->parent_child_db.entries_count) {
400 BNXT_TF_DBG(ERR, "parent child db not supported\n");
408 * Set the tunnel index in the parent flow
410 * ulp_ctxt [in] Ptr to ulp_context
411 * parent_idx [in] The parent index of the parent flow entry
413 * returns index on success and negative on failure.
416 ulp_flow_db_parent_tun_idx_set(struct bnxt_ulp_context *ulp_ctxt,
417 uint32_t parent_idx, uint8_t tun_idx)
419 struct bnxt_ulp_flow_db *flow_db;
420 struct ulp_fdb_parent_child_db *p_pdb;
422 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
424 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
428 /* check for parent idx validity */
429 p_pdb = &flow_db->parent_child_db;
430 if (parent_idx >= p_pdb->entries_count ||
431 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
432 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
436 p_pdb->parent_flow_tbl[parent_idx].tun_idx = tun_idx;
441 * Get the tunnel index from the parent flow
443 * ulp_ctxt [in] Ptr to ulp_context
444 * parent_fid [in] The flow id of the parent flow entry
446 * returns 0 if counter accum is set else -1.
449 ulp_flow_db_parent_tun_idx_get(struct bnxt_ulp_context *ulp_ctxt,
450 uint32_t parent_fid, uint8_t *tun_idx)
452 struct bnxt_ulp_flow_db *flow_db;
453 struct ulp_fdb_parent_child_db *p_pdb;
456 /* validate the arguments */
457 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
459 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
463 p_pdb = &flow_db->parent_child_db;
464 for (idx = 0; idx < p_pdb->entries_count; idx++) {
465 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
466 *tun_idx = p_pdb->parent_flow_tbl[idx].tun_idx;
475 * Initialize the flow database. Memory is allocated in this
476 * call and assigned to the flow database.
478 * ulp_ctxt [in] Ptr to ulp context
480 * Returns 0 on success or negative number on failure.
483 ulp_flow_db_init(struct bnxt_ulp_context *ulp_ctxt)
485 struct bnxt_ulp_device_params *dparms;
486 struct bnxt_ulp_flow_tbl *flow_tbl;
487 struct bnxt_ulp_flow_db *flow_db;
488 uint32_t dev_id, num_flows;
489 enum bnxt_ulp_flow_mem_type mtype;
491 /* Get the dev specific number of flows that needed to be supported. */
492 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctxt, &dev_id)) {
493 BNXT_TF_DBG(ERR, "Invalid device id\n");
497 dparms = bnxt_ulp_device_params_get(dev_id);
499 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
503 flow_db = rte_zmalloc("bnxt_ulp_flow_db",
504 sizeof(struct bnxt_ulp_flow_db), 0);
507 "Failed to allocate memory for flow table ptr\n");
511 /* Attach the flow database to the ulp context. */
512 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, flow_db);
514 /* Determine the number of flows based on EM type */
515 bnxt_ulp_cntxt_mem_type_get(ulp_ctxt, &mtype);
516 if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT)
517 num_flows = dparms->int_flow_db_num_entries;
519 num_flows = dparms->ext_flow_db_num_entries;
521 /* Populate the regular flow table limits. */
522 flow_tbl = &flow_db->flow_tbl;
523 flow_tbl->num_flows = num_flows + 1;
524 flow_tbl->num_resources = ((num_flows + 1) *
525 dparms->num_resources_per_flow);
527 /* Include the default flow table limits. */
528 flow_tbl->num_flows += (BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1);
529 flow_tbl->num_resources += ((BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1) *
530 BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES);
532 /* Allocate the resource for the flow table. */
533 if (ulp_flow_db_alloc_resource(flow_db))
536 /* add 1 since we are not using index 0 for flow id */
537 flow_db->func_id_tbl_size = flow_tbl->num_flows + 1;
538 /* Allocate the function Id table */
539 flow_db->func_id_tbl = rte_zmalloc("bnxt_ulp_flow_db_func_id_table",
540 flow_db->func_id_tbl_size *
541 sizeof(uint16_t), 0);
542 if (!flow_db->func_id_tbl) {
544 "Failed to allocate mem for flow table func id\n");
547 /* initialize the parent child database */
548 if (ulp_flow_db_parent_tbl_init(flow_db,
549 dparms->fdb_parent_flow_entries)) {
551 "Failed to allocate mem for parent child db\n");
555 /* All good so return. */
556 BNXT_TF_DBG(INFO, "FlowDB initialized with %d flows.\n",
557 flow_tbl->num_flows);
560 ulp_flow_db_deinit(ulp_ctxt);
565 * Deinitialize the flow database. Memory is deallocated in
566 * this call and all flows should have been purged before this
569 * ulp_ctxt [in] Ptr to ulp context
571 * Returns 0 on success.
574 ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
576 struct bnxt_ulp_flow_db *flow_db;
578 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
582 /* Detach the flow database from the ulp context. */
583 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, NULL);
585 /* Free up all the memory. */
586 ulp_flow_db_parent_tbl_deinit(flow_db);
587 ulp_flow_db_dealloc_resource(flow_db);
588 rte_free(flow_db->func_id_tbl);
595 * Allocate the flow database entry
597 * ulp_ctxt [in] Ptr to ulp_context
598 * flow_type [in] - specify default or regular
599 * func_id [in].function id of the ingress port
600 * fid [out] The index to the flow entry
602 * returns 0 on success and negative on failure.
605 ulp_flow_db_fid_alloc(struct bnxt_ulp_context *ulp_ctxt,
606 enum bnxt_ulp_fdb_type flow_type,
610 struct bnxt_ulp_flow_db *flow_db;
611 struct bnxt_ulp_flow_tbl *flow_tbl;
613 *fid = 0; /* Initialize fid to invalid value */
614 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
616 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
620 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
621 BNXT_TF_DBG(ERR, "Invalid flow type\n");
625 flow_tbl = &flow_db->flow_tbl;
626 /* check for max flows */
627 if (flow_tbl->num_flows <= flow_tbl->head_index) {
628 BNXT_TF_DBG(ERR, "Flow database has reached max flows\n");
631 if (flow_tbl->tail_index <= (flow_tbl->head_index + 1)) {
632 BNXT_TF_DBG(ERR, "Flow database has reached max resources\n");
635 *fid = flow_tbl->flow_tbl_stack[flow_tbl->head_index];
636 flow_tbl->head_index++;
638 /* Set the flow type */
639 ulp_flow_db_active_flows_bit_set(flow_db, flow_type, *fid, 1);
641 /* function id update is only valid for regular flow table */
642 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
643 ulp_flow_db_func_id_set(flow_db, *fid, func_id);
650 * Allocate the flow database entry.
651 * The params->critical_resource has to be set to 0 to allocate a new resource.
653 * ulp_ctxt [in] Ptr to ulp_context
654 * flow_type [in] Specify it is regular or default flow
655 * fid [in] The index to the flow entry
656 * params [in] The contents to be copied into resource
658 * returns 0 on success and negative on failure.
661 ulp_flow_db_resource_add(struct bnxt_ulp_context *ulp_ctxt,
662 enum bnxt_ulp_fdb_type flow_type,
664 struct ulp_flow_db_res_params *params)
666 struct bnxt_ulp_flow_db *flow_db;
667 struct bnxt_ulp_flow_tbl *flow_tbl;
668 struct ulp_fdb_resource_info *resource, *fid_resource;
671 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
673 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
677 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
678 BNXT_TF_DBG(ERR, "Invalid flow type\n");
682 flow_tbl = &flow_db->flow_tbl;
683 /* check for max flows */
684 if (fid >= flow_tbl->num_flows || !fid) {
685 BNXT_TF_DBG(ERR, "Invalid flow index\n");
689 /* check if the flow is active or not */
690 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
691 BNXT_TF_DBG(ERR, "flow does not exist\n");
695 /* check for max resource */
696 if ((flow_tbl->head_index + 1) >= flow_tbl->tail_index) {
697 BNXT_TF_DBG(ERR, "Flow db has reached max resources\n");
700 fid_resource = &flow_tbl->flow_resources[fid];
702 if (!params->critical_resource) {
703 /* Not the critical_resource so allocate a resource */
704 idx = flow_tbl->flow_tbl_stack[flow_tbl->tail_index];
705 resource = &flow_tbl->flow_resources[idx];
706 flow_tbl->tail_index--;
708 /* Update the chain list of resource*/
709 ULP_FLOW_DB_RES_NXT_SET(resource->nxt_resource_idx,
710 fid_resource->nxt_resource_idx);
711 /* update the contents */
712 ulp_flow_db_res_params_to_info(resource, params);
713 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
714 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
717 /* critical resource. Just update the fid resource */
718 ulp_flow_db_res_params_to_info(fid_resource, params);
721 if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
722 params->resource_sub_type ==
723 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
724 /* Store the first HW counter ID for this table */
725 if (!ulp_fc_mgr_start_idx_isset(ulp_ctxt, params->direction))
726 ulp_fc_mgr_start_idx_set(ulp_ctxt, params->direction,
727 params->resource_hndl);
729 ulp_fc_mgr_cntr_set(ulp_ctxt, params->direction,
730 params->resource_hndl);
732 if (!ulp_fc_mgr_thread_isstarted(ulp_ctxt))
733 ulp_fc_mgr_thread_start(ulp_ctxt);
736 /* all good, return success */
741 * Free the flow database entry.
742 * The params->critical_resource has to be set to 1 to free the first resource.
744 * ulp_ctxt [in] Ptr to ulp_context
745 * flow_type [in] Specify it is regular or default flow
746 * fid [in] The index to the flow entry
747 * params [in/out] The contents to be copied into params.
748 * Onlythe critical_resource needs to be set by the caller.
750 * Returns 0 on success and negative on failure.
753 ulp_flow_db_resource_del(struct bnxt_ulp_context *ulp_ctxt,
754 enum bnxt_ulp_fdb_type flow_type,
756 struct ulp_flow_db_res_params *params)
758 struct bnxt_ulp_flow_db *flow_db;
759 struct bnxt_ulp_flow_tbl *flow_tbl;
760 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
761 uint32_t nxt_idx = 0;
762 struct bnxt_tun_cache_entry *tun_tbl;
766 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
768 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
772 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
773 BNXT_TF_DBG(ERR, "Invalid flow type\n");
777 flow_tbl = &flow_db->flow_tbl;
778 /* check for max flows */
779 if (fid >= flow_tbl->num_flows || !fid) {
780 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
784 /* check if the flow is active or not */
785 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
786 BNXT_TF_DBG(ERR, "flow does not exist\n");
790 fid_resource = &flow_tbl->flow_resources[fid];
791 if (!params->critical_resource) {
792 /* Not the critical resource so free the resource */
793 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
794 fid_resource->nxt_resource_idx);
796 /* reached end of resources */
799 nxt_resource = &flow_tbl->flow_resources[nxt_idx];
801 /* connect the fid resource to the next resource */
802 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
803 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
804 nxt_resource->nxt_resource_idx);
806 /* update the contents to be given to caller */
807 ulp_flow_db_res_info_to_params(nxt_resource, params);
809 /* Delete the nxt_resource */
810 memset(nxt_resource, 0, sizeof(struct ulp_fdb_resource_info));
812 /* add it to the free list */
813 flow_tbl->tail_index++;
814 if (flow_tbl->tail_index >= flow_tbl->num_resources) {
815 BNXT_TF_DBG(ERR, "FlowDB:Tail reached max\n");
818 flow_tbl->flow_tbl_stack[flow_tbl->tail_index] = nxt_idx;
821 /* Critical resource. copy the contents and exit */
822 ulp_flow_db_res_info_to_params(fid_resource, params);
823 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
824 fid_resource->nxt_resource_idx);
825 memset(fid_resource, 0, sizeof(struct ulp_fdb_resource_info));
826 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
830 /* Now that the HW Flow counter resource is deleted, reset it's
831 * corresponding slot in the SW accumulation table in the Flow Counter
834 if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
835 params->resource_sub_type ==
836 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
837 ulp_fc_mgr_cntr_reset(ulp_ctxt, params->direction,
838 params->resource_hndl);
841 if (params->resource_func == BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW) {
842 tun_tbl = bnxt_ulp_cntxt_ptr2_tun_tbl_get(ulp_ctxt);
846 rc = ulp_flow_db_parent_tun_idx_get(ulp_ctxt, fid, &tun_idx);
850 ulp_clear_tun_entry(tun_tbl, tun_idx);
853 /* all good, return success */
858 * Free the flow database entry
860 * ulp_ctxt [in] Ptr to ulp_context
861 * flow_type [in] - specify default or regular
862 * fid [in] The index to the flow entry
864 * returns 0 on success and negative on failure.
867 ulp_flow_db_fid_free(struct bnxt_ulp_context *ulp_ctxt,
868 enum bnxt_ulp_fdb_type flow_type,
871 struct bnxt_ulp_flow_db *flow_db;
872 struct bnxt_ulp_flow_tbl *flow_tbl;
874 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
876 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
880 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
881 BNXT_TF_DBG(ERR, "Invalid flow type\n");
885 flow_tbl = &flow_db->flow_tbl;
887 /* check for limits of fid */
888 if (fid >= flow_tbl->num_flows || !fid) {
889 BNXT_TF_DBG(ERR, "Invalid flow index\n");
893 /* check if the flow is active or not */
894 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
895 BNXT_TF_DBG(ERR, "flow does not exist\n");
898 flow_tbl->head_index--;
899 if (!flow_tbl->head_index) {
900 BNXT_TF_DBG(ERR, "FlowDB: Head Ptr is zero\n");
903 flow_tbl->flow_tbl_stack[flow_tbl->head_index] = fid;
905 /* Clear the flows bitmap */
906 ulp_flow_db_active_flows_bit_set(flow_db, flow_type, fid, 0);
908 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
909 ulp_flow_db_func_id_set(flow_db, fid, 0);
911 /* all good, return success */
916 * Get the flow database entry details
918 * ulp_ctxt [in] Ptr to ulp_context
919 * flow_type [in] - specify default or regular
920 * fid [in] The index to the flow entry
921 * nxt_idx [in/out] the index to the next entry
922 * params [out] The contents to be copied into params.
924 * returns 0 on success and negative on failure.
927 ulp_flow_db_resource_get(struct bnxt_ulp_context *ulp_ctxt,
928 enum bnxt_ulp_fdb_type flow_type,
931 struct ulp_flow_db_res_params *params)
933 struct bnxt_ulp_flow_db *flow_db;
934 struct bnxt_ulp_flow_tbl *flow_tbl;
935 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
937 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
939 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
943 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
944 BNXT_TF_DBG(ERR, "Invalid flow type\n");
948 flow_tbl = &flow_db->flow_tbl;
950 /* check for limits of fid */
951 if (fid >= flow_tbl->num_flows || !fid) {
952 BNXT_TF_DBG(ERR, "Invalid flow index\n");
956 /* check if the flow is active or not */
957 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
958 BNXT_TF_DBG(ERR, "flow does not exist\n");
963 fid_resource = &flow_tbl->flow_resources[fid];
964 ulp_flow_db_res_info_to_params(fid_resource, params);
965 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
966 fid_resource->nxt_resource_idx);
968 nxt_resource = &flow_tbl->flow_resources[*nxt_idx];
969 ulp_flow_db_res_info_to_params(nxt_resource, params);
971 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
972 nxt_resource->nxt_resource_idx);
975 /* all good, return success */
980 * Get the flow database entry iteratively
982 * flow_tbl [in] Ptr to flow table
983 * flow_type [in] - specify default or regular
984 * fid [in/out] The index to the flow entry
986 * returns 0 on success and negative on failure.
989 ulp_flow_db_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
990 enum bnxt_ulp_fdb_type flow_type,
993 uint32_t lfid = *fid;
994 uint32_t idx, s_idx, mod_fid;
996 uint64_t *active_flows;
997 struct bnxt_ulp_flow_tbl *flowtbl = &flow_db->flow_tbl;
999 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
1000 active_flows = flowtbl->active_reg_flows;
1002 active_flows = flowtbl->active_dflt_flows;
1005 /* increment the flow id to find the next valid flow id */
1007 if (lfid >= flowtbl->num_flows)
1009 idx = lfid / ULP_INDEX_BITMAP_SIZE;
1010 mod_fid = lfid % ULP_INDEX_BITMAP_SIZE;
1012 while (!(bs = active_flows[idx])) {
1014 if ((idx * ULP_INDEX_BITMAP_SIZE) >= flowtbl->num_flows)
1018 * remove the previous bits in the bitset bs to find the
1019 * next non zero bit in the bitset. This needs to be done
1020 * only if the idx is same as he one you started.
1023 bs &= (-1UL >> mod_fid);
1024 lfid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1026 BNXT_TF_DBG(ERR, "Flow Database is corrupt\n");
1029 } while (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type,
1032 /* all good, return success */
1038 * Flush all flows in the flow database.
1040 * ulp_ctxt [in] Ptr to ulp context
1041 * flow_type [in] - specify default or regular
1043 * returns 0 on success or negative number on failure
1046 ulp_flow_db_flush_flows(struct bnxt_ulp_context *ulp_ctx,
1047 enum bnxt_ulp_fdb_type flow_type)
1050 struct bnxt_ulp_flow_db *flow_db;
1053 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1057 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1059 BNXT_TF_DBG(ERR, "Flow database not found\n");
1062 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1063 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1067 while (!ulp_flow_db_next_entry_get(flow_db, flow_type, &fid))
1068 ulp_mapper_resources_free(ulp_ctx, flow_type, fid);
1070 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1076 * Flush all flows in the flow database that belong to a device function.
1078 * ulp_ctxt [in] Ptr to ulp context
1079 * func_id [in] - The port function id
1081 * returns 0 on success or negative number on failure
1084 ulp_flow_db_function_flow_flush(struct bnxt_ulp_context *ulp_ctx,
1087 uint32_t flow_id = 0;
1088 struct bnxt_ulp_flow_db *flow_db;
1090 if (!ulp_ctx || !func_id) {
1091 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1095 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1097 BNXT_TF_DBG(ERR, "Flow database not found\n");
1100 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1101 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1105 while (!ulp_flow_db_next_entry_get(flow_db, BNXT_ULP_FDB_TYPE_REGULAR,
1107 if (flow_db->func_id_tbl[flow_id] == func_id)
1108 ulp_mapper_resources_free(ulp_ctx,
1109 BNXT_ULP_FDB_TYPE_REGULAR,
1112 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1117 * Flush all flows in the flow database that are associated with the session.
1119 * ulp_ctxt [in] Ptr to ulp context
1121 * returns 0 on success or negative number on failure
1124 ulp_flow_db_session_flow_flush(struct bnxt_ulp_context *ulp_ctx)
1127 * TBD: Tf core implementation of FW session flush shall change this
1130 return ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1134 * Check that flow id matches the function id or not
1136 * ulp_ctxt [in] Ptr to ulp context
1137 * flow_db [in] Ptr to flow table
1138 * func_id [in] The func_id to be set, for reset pass zero.
1140 * returns true on success or false on failure
1143 ulp_flow_db_validate_flow_func(struct bnxt_ulp_context *ulp_ctx,
1147 struct bnxt_ulp_flow_db *flow_db;
1149 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1151 BNXT_TF_DBG(ERR, "Flow database not found\n");
1155 /* set the function id in the function table */
1156 if (flow_id < flow_db->func_id_tbl_size && func_id &&
1157 flow_db->func_id_tbl[flow_id] == func_id)
1164 * Internal api to traverse the resource list within a flow
1165 * and match a resource based on resource func and resource
1166 * sub type. This api should be used only for resources that
1167 * are unique and do not have multiple instances of resource
1168 * func and sub type combination since it will return only
1172 ulp_flow_db_resource_params_get(struct bnxt_ulp_context *ulp_ctx,
1173 enum bnxt_ulp_fdb_type flow_type,
1175 uint32_t resource_func,
1176 uint32_t res_subtype,
1177 struct ulp_flow_db_res_params *params)
1179 struct bnxt_ulp_flow_db *flow_db;
1180 struct bnxt_ulp_flow_tbl *flow_tbl;
1181 struct ulp_fdb_resource_info *fid_res;
1184 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1186 BNXT_TF_DBG(ERR, "Flow database not found\n");
1191 BNXT_TF_DBG(ERR, "invalid argument\n");
1195 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
1196 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1200 flow_tbl = &flow_db->flow_tbl;
1202 /* check for limits of fid */
1203 if (flow_id >= flow_tbl->num_flows || !flow_id) {
1204 BNXT_TF_DBG(ERR, "Invalid flow index\n");
1208 /* check if the flow is active or not */
1209 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, flow_id)) {
1210 BNXT_TF_DBG(ERR, "flow does not exist\n");
1213 /* Iterate the resource to get the resource handle */
1215 memset(params, 0, sizeof(struct ulp_flow_db_res_params));
1217 fid_res = &flow_tbl->flow_resources[res_id];
1218 if (ulp_flow_db_resource_func_get(fid_res) == resource_func) {
1219 if (resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
1220 if (res_subtype == fid_res->resource_sub_type) {
1221 ulp_flow_db_res_info_to_params(fid_res,
1226 } else if (resource_func ==
1227 BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE ||
1229 BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
1230 ulp_flow_db_res_info_to_params(fid_res,
1236 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1242 * Api to get the cfa action pointer from a flow.
1244 * ulp_ctxt [in] Ptr to ulp context
1245 * flow_id [in] flow id
1246 * cfa_action [out] The resource handle stored in the flow database
1248 * returns 0 on success
1251 ulp_default_flow_db_cfa_action_get(struct bnxt_ulp_context *ulp_ctx,
1253 uint16_t *cfa_action)
1255 uint8_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_VFR_CFA_ACTION;
1256 struct ulp_flow_db_res_params params;
1259 rc = ulp_flow_db_resource_params_get(ulp_ctx,
1260 BNXT_ULP_FDB_TYPE_DEFAULT,
1262 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1265 BNXT_TF_DBG(ERR, "CFA Action ptr not found for flow id %u\n",
1269 *cfa_action = params.resource_hndl;
1274 * Allocate the entry in the parent-child database
1276 * ulp_ctxt [in] Ptr to ulp_context
1277 * fid [in] The flow id to the flow entry
1279 * returns index on success and negative on failure.
1282 ulp_flow_db_parent_flow_alloc(struct bnxt_ulp_context *ulp_ctxt,
1285 struct bnxt_ulp_flow_db *flow_db;
1286 struct ulp_fdb_parent_child_db *p_pdb;
1287 uint32_t idx, free_idx = 0;
1289 /* validate the arguments */
1290 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1292 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1296 p_pdb = &flow_db->parent_child_db;
1297 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1298 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1299 BNXT_TF_DBG(ERR, "fid is already allocated\n");
1302 if (!p_pdb->parent_flow_tbl[idx].parent_fid && !free_idx)
1307 BNXT_TF_DBG(ERR, "parent child db is full\n");
1312 /* set the Fid in the parent child */
1313 p_pdb->parent_flow_tbl[free_idx].parent_fid = fid;
1318 * Free the entry in the parent-child database
1320 * ulp_ctxt [in] Ptr to ulp_context
1321 * fid [in] The flow id to the flow entry
1323 * returns 0 on success and negative on failure.
1326 ulp_flow_db_parent_flow_free(struct bnxt_ulp_context *ulp_ctxt,
1329 struct bnxt_ulp_flow_db *flow_db;
1330 struct ulp_fdb_parent_child_db *p_pdb;
1333 /* validate the arguments */
1334 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1336 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1340 p_pdb = &flow_db->parent_child_db;
1341 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1342 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1343 /* free the contents */
1344 p_pdb->parent_flow_tbl[idx].parent_fid = 0;
1345 memset(p_pdb->parent_flow_tbl[idx].child_fid_bitset,
1346 0, p_pdb->child_bitset_size);
1350 BNXT_TF_DBG(ERR, "parent entry not found = %x\n", fid);
1355 * Set or reset the child flow in the parent-child database
1357 * ulp_ctxt [in] Ptr to ulp_context
1358 * parent_fid [in] The flow id of the parent flow entry
1359 * child_fid [in] The flow id of the child flow entry
1360 * set_flag [in] Use 1 for setting child, 0 to reset
1362 * returns zero on success and negative on failure.
1365 ulp_flow_db_parent_child_flow_set(struct bnxt_ulp_context *ulp_ctxt,
1366 uint32_t parent_fid,
1370 struct bnxt_ulp_flow_db *flow_db;
1371 struct ulp_fdb_parent_child_db *p_pdb;
1372 uint32_t idx, a_idx;
1375 /* validate the arguments */
1376 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1378 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1382 /* check for fid validity */
1383 if (child_fid >= flow_db->flow_tbl.num_flows || !child_fid) {
1384 BNXT_TF_DBG(ERR, "Invalid child flow index %x\n", child_fid);
1388 p_pdb = &flow_db->parent_child_db;
1389 a_idx = child_fid / ULP_INDEX_BITMAP_SIZE;
1390 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1391 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1392 t = p_pdb->parent_flow_tbl[idx].child_fid_bitset;
1394 ULP_INDEX_BITMAP_SET(t[a_idx], child_fid);
1396 ULP_INDEX_BITMAP_RESET(t[a_idx], child_fid);
1400 BNXT_TF_DBG(ERR, "Unable to set the parent-child flow %x:%x\n",
1401 parent_fid, child_fid);
1406 * Get the parent index from the parent-child database
1408 * ulp_ctxt [in] Ptr to ulp_context
1409 * parent_fid [in] The flow id of the parent flow entry
1410 * parent_idx [out] The parent index of parent flow entry
1412 * returns zero on success and negative on failure.
1415 ulp_flow_db_parent_flow_idx_get(struct bnxt_ulp_context *ulp_ctxt,
1416 uint32_t parent_fid,
1417 uint32_t *parent_idx)
1419 struct bnxt_ulp_flow_db *flow_db;
1420 struct ulp_fdb_parent_child_db *p_pdb;
1423 /* validate the arguments */
1424 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1426 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1430 p_pdb = &flow_db->parent_child_db;
1431 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1432 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1437 BNXT_TF_DBG(ERR, "Unable to get the parent flow %x\n", parent_fid);
1442 * Get the next child flow in the parent-child database
1444 * ulp_ctxt [in] Ptr to ulp_context
1445 * parent_fid [in] The flow id of the parent flow entry
1446 * child_fid [in/out] The flow id of the child flow entry
1448 * returns zero on success and negative on failure.
1449 * Pass child_fid as zero for first entry.
1452 ulp_flow_db_parent_child_flow_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
1453 uint32_t parent_idx,
1454 uint32_t *child_fid)
1456 struct ulp_fdb_parent_child_db *p_pdb;
1457 uint32_t idx, s_idx, mod_fid;
1458 uint32_t next_fid = *child_fid;
1459 uint64_t *child_bitset;
1462 /* check for fid validity */
1463 p_pdb = &flow_db->parent_child_db;
1464 if (parent_idx >= p_pdb->entries_count ||
1465 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1466 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1470 child_bitset = p_pdb->parent_flow_tbl[parent_idx].child_fid_bitset;
1472 /* increment the flow id to find the next valid flow id */
1474 if (next_fid >= flow_db->flow_tbl.num_flows)
1476 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1477 mod_fid = next_fid % ULP_INDEX_BITMAP_SIZE;
1479 while (!(bs = child_bitset[idx])) {
1481 if ((idx * ULP_INDEX_BITMAP_SIZE) >=
1482 flow_db->flow_tbl.num_flows)
1486 * remove the previous bits in the bitset bs to find the
1487 * next non zero bit in the bitset. This needs to be done
1488 * only if the idx is same as he one you started.
1491 bs &= (-1UL >> mod_fid);
1492 next_fid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1493 if (*child_fid >= next_fid) {
1494 BNXT_TF_DBG(ERR, "Parent Child Database is corrupt\n");
1497 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1498 } while (!ULP_INDEX_BITMAP_GET(child_bitset[idx], next_fid));
1499 *child_fid = next_fid;
1504 * Set the counter accumulation in the parent flow
1506 * ulp_ctxt [in] Ptr to ulp_context
1507 * parent_idx [in] The parent index of the parent flow entry
1509 * returns index on success and negative on failure.
1512 ulp_flow_db_parent_flow_count_accum_set(struct bnxt_ulp_context *ulp_ctxt,
1513 uint32_t parent_idx)
1515 struct bnxt_ulp_flow_db *flow_db;
1516 struct ulp_fdb_parent_child_db *p_pdb;
1518 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1520 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1524 /* check for parent idx validity */
1525 p_pdb = &flow_db->parent_child_db;
1526 if (parent_idx >= p_pdb->entries_count ||
1527 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1528 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1532 p_pdb->parent_flow_tbl[parent_idx].counter_acc = 1;
1537 * Get the counter accumulation in the parent flow
1539 * ulp_ctxt [in] Ptr to ulp_context
1540 * parent_fid [in] The flow id of the parent flow entry
1542 * returns 0 if counter accum is set else -1.
1545 ulp_flow_db_parent_flow_count_accum_get(struct bnxt_ulp_context *ulp_ctxt,
1546 uint32_t parent_fid)
1548 struct bnxt_ulp_flow_db *flow_db;
1549 struct ulp_fdb_parent_child_db *p_pdb;
1552 /* validate the arguments */
1553 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1555 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1559 p_pdb = &flow_db->parent_child_db;
1560 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1561 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1562 if (p_pdb->parent_flow_tbl[idx].counter_acc)
1571 * Orphan the child flow entry
1572 * This is called only for child flows that have
1573 * BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW resource
1575 * ulp_ctxt [in] Ptr to ulp_context
1576 * flow_type [in] Specify it is regular or default flow
1577 * fid [in] The index to the flow entry
1579 * Returns 0 on success and negative on failure.
1582 ulp_flow_db_child_flow_reset(struct bnxt_ulp_context *ulp_ctxt,
1583 enum bnxt_ulp_fdb_type flow_type,
1586 struct bnxt_ulp_flow_db *flow_db;
1587 struct bnxt_ulp_flow_tbl *flow_tbl;
1588 struct ulp_fdb_resource_info *fid_res;
1589 uint32_t res_id = 0;
1591 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1593 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1597 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
1598 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1602 flow_tbl = &flow_db->flow_tbl;
1603 /* check for max flows */
1604 if (fid >= flow_tbl->num_flows || !fid) {
1605 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
1609 /* check if the flow is active or not */
1610 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
1611 BNXT_TF_DBG(ERR, "flow does not exist\n");
1615 /* Iterate the resource to get the resource handle */
1618 fid_res = &flow_tbl->flow_resources[res_id];
1619 if (ulp_flow_db_resource_func_get(fid_res) ==
1620 BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW) {
1621 /* invalidate the resource details */
1622 fid_res->resource_hndl = 0;
1626 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1633 * Create parent flow in the parent flow tbl
1635 * parms [in] Ptr to mapper params
1637 * Returns 0 on success and negative on failure.
1640 ulp_flow_db_parent_flow_create(struct bnxt_ulp_mapper_parms *parms)
1642 struct ulp_flow_db_res_params fid_parms;
1643 uint32_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT_ACC;
1644 struct ulp_flow_db_res_params res_params;
1645 int32_t fid_idx, rc;
1647 /* create the child flow entry in parent flow table */
1648 fid_idx = ulp_flow_db_parent_flow_alloc(parms->ulp_ctx, parms->fid);
1650 BNXT_TF_DBG(ERR, "Error in creating parent flow fid %x\n",
1655 /* Add the parent details in the resource list of the flow */
1656 memset(&fid_parms, 0, sizeof(fid_parms));
1657 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW;
1658 fid_parms.resource_hndl = fid_idx;
1659 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1660 if (ulp_flow_db_resource_add(parms->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
1661 parms->fid, &fid_parms)) {
1662 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1667 /* check of the flow has internal counter accumulation enabled */
1668 if (!ulp_flow_db_resource_params_get(parms->ulp_ctx,
1669 BNXT_ULP_FDB_TYPE_REGULAR,
1671 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1674 /* Enable the counter accumulation in parent entry */
1675 if (ulp_flow_db_parent_flow_count_accum_set(parms->ulp_ctx,
1677 BNXT_TF_DBG(ERR, "Error in setting counter acc %x\n",
1683 rc = ulp_flow_db_parent_tun_idx_set(parms->ulp_ctx, fid_idx,
1686 BNXT_TF_DBG(ERR, "Error setting tun_idx in the parent flow\n");
1694 * Create child flow in the parent flow tbl
1696 * parms [in] Ptr to mapper params
1698 * Returns 0 on success and negative on failure.
1701 ulp_flow_db_child_flow_create(struct bnxt_ulp_mapper_parms *parms)
1703 struct ulp_flow_db_res_params fid_parms;
1704 uint32_t sub_type = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT;
1705 enum bnxt_ulp_resource_func res_fun;
1706 struct ulp_flow_db_res_params res_p;
1707 uint32_t parent_fid = parms->parent_fid;
1710 /* create the parent flow entry in parent flow table */
1711 rc = ulp_flow_db_parent_child_flow_set(parms->ulp_ctx,
1715 BNXT_TF_DBG(ERR, "Error in setting child fid %x\n", parms->fid);
1719 /* Add the parent details in the resource list of the flow */
1720 memset(&fid_parms, 0, sizeof(fid_parms));
1721 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW;
1722 fid_parms.resource_hndl = parms->parent_fid;
1723 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1724 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1725 BNXT_ULP_FDB_TYPE_REGULAR,
1726 parms->fid, &fid_parms);
1728 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1733 /* check if accumulation count is set for parent flow */
1734 rc = ulp_flow_db_parent_flow_count_accum_get(parms->ulp_ctx,
1737 /* check if internal count action included for this flow.*/
1738 res_fun = BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE;
1739 rc = ulp_flow_db_resource_params_get(parms->ulp_ctx,
1740 BNXT_ULP_FDB_TYPE_REGULAR,
1746 /* update the counter manager to include parent fid */
1747 if (ulp_fc_mgr_cntr_parent_flow_set(parms->ulp_ctx,
1749 res_p.resource_hndl,
1751 BNXT_TF_DBG(ERR, "Error in setting child %x\n",
1757 /* return success */
1762 * Update the parent counters
1764 * ulp_ctxt [in] Ptr to ulp_context
1765 * parent_fid [in] The flow id of the parent flow entry
1766 * packet_count [in] - packet count
1767 * byte_count [in] - byte count
1769 * returns 0 on success
1772 ulp_flow_db_parent_flow_count_update(struct bnxt_ulp_context *ulp_ctxt,
1773 uint32_t parent_fid,
1774 uint64_t packet_count,
1775 uint64_t byte_count)
1777 struct bnxt_ulp_flow_db *flow_db;
1778 struct ulp_fdb_parent_child_db *p_pdb;
1781 /* validate the arguments */
1782 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1784 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1788 p_pdb = &flow_db->parent_child_db;
1789 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1790 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1791 if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1792 p_pdb->parent_flow_tbl[idx].pkt_count +=
1794 p_pdb->parent_flow_tbl[idx].byte_count +=
1804 * Get the parent accumulation counters
1806 * ulp_ctxt [in] Ptr to ulp_context
1807 * parent_fid [in] The flow id of the parent flow entry
1808 * packet_count [out] - packet count
1809 * byte_count [out] - byte count
1811 * returns 0 on success
1814 ulp_flow_db_parent_flow_count_get(struct bnxt_ulp_context *ulp_ctxt,
1815 uint32_t parent_fid,
1816 uint64_t *packet_count,
1817 uint64_t *byte_count)
1819 struct bnxt_ulp_flow_db *flow_db;
1820 struct ulp_fdb_parent_child_db *p_pdb;
1823 /* validate the arguments */
1824 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1826 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1830 p_pdb = &flow_db->parent_child_db;
1831 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1832 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1833 if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1835 p_pdb->parent_flow_tbl[idx].pkt_count;
1837 p_pdb->parent_flow_tbl[idx].byte_count;
1846 * reset the parent accumulation counters
1848 * ulp_ctxt [in] Ptr to ulp_context
1853 ulp_flow_db_parent_flow_count_reset(struct bnxt_ulp_context *ulp_ctxt)
1855 struct bnxt_ulp_flow_db *flow_db;
1856 struct ulp_fdb_parent_child_db *p_pdb;
1859 /* validate the arguments */
1860 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, 1);
1862 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1866 p_pdb = &flow_db->parent_child_db;
1867 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1868 if (p_pdb->parent_flow_tbl[idx].parent_fid &&
1869 p_pdb->parent_flow_tbl[idx].counter_acc) {
1870 p_pdb->parent_flow_tbl[idx].pkt_count = 0;
1871 p_pdb->parent_flow_tbl[idx].byte_count = 0;