1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2020 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_TYPE_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_TYPE_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_tun_cache_entry *tun_tbl;
872 struct bnxt_ulp_flow_tbl *flow_tbl;
873 struct bnxt_ulp_flow_db *flow_db;
875 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
877 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
881 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
882 BNXT_TF_DBG(ERR, "Invalid flow type\n");
886 flow_tbl = &flow_db->flow_tbl;
888 /* check for limits of fid */
889 if (fid >= flow_tbl->num_flows || !fid) {
890 BNXT_TF_DBG(ERR, "Invalid flow index\n");
894 /* check if the flow is active or not */
895 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
896 BNXT_TF_DBG(ERR, "flow does not exist\n");
899 flow_tbl->head_index--;
900 if (!flow_tbl->head_index) {
901 BNXT_TF_DBG(ERR, "FlowDB: Head Ptr is zero\n");
904 flow_tbl->flow_tbl_stack[flow_tbl->head_index] = fid;
906 /* Clear the flows bitmap */
907 ulp_flow_db_active_flows_bit_set(flow_db, flow_type, fid, 0);
909 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
910 ulp_flow_db_func_id_set(flow_db, fid, 0);
912 tun_tbl = bnxt_ulp_cntxt_ptr2_tun_tbl_get(ulp_ctxt);
916 ulp_clear_tun_inner_entry(tun_tbl, fid);
918 /* all good, return success */
923 * Get the flow database entry details
925 * ulp_ctxt [in] Ptr to ulp_context
926 * flow_type [in] - specify default or regular
927 * fid [in] The index to the flow entry
928 * nxt_idx [in/out] the index to the next entry
929 * params [out] The contents to be copied into params.
931 * returns 0 on success and negative on failure.
934 ulp_flow_db_resource_get(struct bnxt_ulp_context *ulp_ctxt,
935 enum bnxt_ulp_fdb_type flow_type,
938 struct ulp_flow_db_res_params *params)
940 struct bnxt_ulp_flow_db *flow_db;
941 struct bnxt_ulp_flow_tbl *flow_tbl;
942 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
944 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
946 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
950 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
951 BNXT_TF_DBG(ERR, "Invalid flow type\n");
955 flow_tbl = &flow_db->flow_tbl;
957 /* check for limits of fid */
958 if (fid >= flow_tbl->num_flows || !fid) {
959 BNXT_TF_DBG(ERR, "Invalid flow index\n");
963 /* check if the flow is active or not */
964 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
965 BNXT_TF_DBG(ERR, "flow does not exist\n");
970 fid_resource = &flow_tbl->flow_resources[fid];
971 ulp_flow_db_res_info_to_params(fid_resource, params);
972 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
973 fid_resource->nxt_resource_idx);
975 nxt_resource = &flow_tbl->flow_resources[*nxt_idx];
976 ulp_flow_db_res_info_to_params(nxt_resource, params);
978 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
979 nxt_resource->nxt_resource_idx);
982 /* all good, return success */
987 * Get the flow database entry iteratively
989 * flow_tbl [in] Ptr to flow table
990 * flow_type [in] - specify default or regular
991 * fid [in/out] The index to the flow entry
993 * returns 0 on success and negative on failure.
996 ulp_flow_db_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
997 enum bnxt_ulp_fdb_type flow_type,
1000 uint32_t lfid = *fid;
1001 uint32_t idx, s_idx, mod_fid;
1003 uint64_t *active_flows;
1004 struct bnxt_ulp_flow_tbl *flowtbl = &flow_db->flow_tbl;
1006 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
1007 active_flows = flowtbl->active_reg_flows;
1009 active_flows = flowtbl->active_dflt_flows;
1012 /* increment the flow id to find the next valid flow id */
1014 if (lfid >= flowtbl->num_flows)
1016 idx = lfid / ULP_INDEX_BITMAP_SIZE;
1017 mod_fid = lfid % ULP_INDEX_BITMAP_SIZE;
1019 while (!(bs = active_flows[idx])) {
1021 if ((idx * ULP_INDEX_BITMAP_SIZE) >= flowtbl->num_flows)
1025 * remove the previous bits in the bitset bs to find the
1026 * next non zero bit in the bitset. This needs to be done
1027 * only if the idx is same as he one you started.
1030 bs &= (-1UL >> mod_fid);
1031 lfid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1033 BNXT_TF_DBG(ERR, "Flow Database is corrupt\n");
1036 } while (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type,
1039 /* all good, return success */
1045 * Flush all flows in the flow database.
1047 * ulp_ctxt [in] Ptr to ulp context
1048 * flow_type [in] - specify default or regular
1050 * returns 0 on success or negative number on failure
1053 ulp_flow_db_flush_flows(struct bnxt_ulp_context *ulp_ctx,
1054 enum bnxt_ulp_fdb_type flow_type)
1057 struct bnxt_ulp_flow_db *flow_db;
1060 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1064 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1066 BNXT_TF_DBG(ERR, "Flow database not found\n");
1069 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1070 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1074 while (!ulp_flow_db_next_entry_get(flow_db, flow_type, &fid))
1075 ulp_mapper_resources_free(ulp_ctx, flow_type, fid);
1077 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1083 * Flush all flows in the flow database that belong to a device function.
1085 * ulp_ctxt [in] Ptr to ulp context
1086 * func_id [in] - The port function id
1088 * returns 0 on success or negative number on failure
1091 ulp_flow_db_function_flow_flush(struct bnxt_ulp_context *ulp_ctx,
1094 uint32_t flow_id = 0;
1095 struct bnxt_ulp_flow_db *flow_db;
1097 if (!ulp_ctx || !func_id) {
1098 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1102 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1104 BNXT_TF_DBG(ERR, "Flow database not found\n");
1107 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1108 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1112 while (!ulp_flow_db_next_entry_get(flow_db, BNXT_ULP_FDB_TYPE_REGULAR,
1114 if (flow_db->func_id_tbl[flow_id] == func_id)
1115 ulp_mapper_resources_free(ulp_ctx,
1116 BNXT_ULP_FDB_TYPE_REGULAR,
1119 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1124 * Flush all flows in the flow database that are associated with the session.
1126 * ulp_ctxt [in] Ptr to ulp context
1128 * returns 0 on success or negative number on failure
1131 ulp_flow_db_session_flow_flush(struct bnxt_ulp_context *ulp_ctx)
1134 * TBD: Tf core implementation of FW session flush shall change this
1137 return ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1141 * Check that flow id matches the function id or not
1143 * ulp_ctxt [in] Ptr to ulp context
1144 * flow_db [in] Ptr to flow table
1145 * func_id [in] The func_id to be set, for reset pass zero.
1147 * returns true on success or false on failure
1150 ulp_flow_db_validate_flow_func(struct bnxt_ulp_context *ulp_ctx,
1154 struct bnxt_ulp_flow_db *flow_db;
1156 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1158 BNXT_TF_DBG(ERR, "Flow database not found\n");
1162 /* set the function id in the function table */
1163 if (flow_id < flow_db->func_id_tbl_size && func_id &&
1164 flow_db->func_id_tbl[flow_id] == func_id)
1171 * Internal api to traverse the resource list within a flow
1172 * and match a resource based on resource func and resource
1173 * sub type. This api should be used only for resources that
1174 * are unique and do not have multiple instances of resource
1175 * func and sub type combination since it will return only
1179 ulp_flow_db_resource_params_get(struct bnxt_ulp_context *ulp_ctx,
1180 enum bnxt_ulp_fdb_type flow_type,
1182 uint32_t resource_func,
1183 uint32_t res_subtype,
1184 struct ulp_flow_db_res_params *params)
1186 struct bnxt_ulp_flow_db *flow_db;
1187 struct bnxt_ulp_flow_tbl *flow_tbl;
1188 struct ulp_fdb_resource_info *fid_res;
1191 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1193 BNXT_TF_DBG(ERR, "Flow database not found\n");
1198 BNXT_TF_DBG(ERR, "invalid argument\n");
1202 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
1203 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1207 flow_tbl = &flow_db->flow_tbl;
1209 /* check for limits of fid */
1210 if (flow_id >= flow_tbl->num_flows || !flow_id) {
1211 BNXT_TF_DBG(ERR, "Invalid flow index\n");
1215 /* check if the flow is active or not */
1216 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, flow_id)) {
1217 BNXT_TF_DBG(ERR, "flow does not exist\n");
1220 /* Iterate the resource to get the resource handle */
1222 memset(params, 0, sizeof(struct ulp_flow_db_res_params));
1224 fid_res = &flow_tbl->flow_resources[res_id];
1225 if (ulp_flow_db_resource_func_get(fid_res) == resource_func) {
1226 if (resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
1227 if (res_subtype == fid_res->resource_sub_type) {
1228 ulp_flow_db_res_info_to_params(fid_res,
1233 } else if (resource_func ==
1234 BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE ||
1236 BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
1237 ulp_flow_db_res_info_to_params(fid_res,
1243 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1249 * Api to get the cfa action pointer from a flow.
1251 * ulp_ctxt [in] Ptr to ulp context
1252 * flow_id [in] flow id
1253 * cfa_action [out] The resource handle stored in the flow database
1255 * returns 0 on success
1258 ulp_default_flow_db_cfa_action_get(struct bnxt_ulp_context *ulp_ctx,
1260 uint16_t *cfa_action)
1262 uint8_t sub_type = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TYPE_VFR_CFA_ACTION;
1263 struct ulp_flow_db_res_params params;
1266 rc = ulp_flow_db_resource_params_get(ulp_ctx,
1267 BNXT_ULP_FDB_TYPE_DEFAULT,
1269 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1272 BNXT_TF_DBG(ERR, "CFA Action ptr not found for flow id %u\n",
1276 *cfa_action = params.resource_hndl;
1281 * Allocate the entry in the parent-child database
1283 * ulp_ctxt [in] Ptr to ulp_context
1284 * fid [in] The flow id to the flow entry
1286 * returns index on success and negative on failure.
1289 ulp_flow_db_parent_flow_alloc(struct bnxt_ulp_context *ulp_ctxt,
1292 struct bnxt_ulp_flow_db *flow_db;
1293 struct ulp_fdb_parent_child_db *p_pdb;
1294 uint32_t idx, free_idx = 0;
1296 /* validate the arguments */
1297 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1299 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1303 p_pdb = &flow_db->parent_child_db;
1304 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1305 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1306 BNXT_TF_DBG(ERR, "fid is already allocated\n");
1309 if (!p_pdb->parent_flow_tbl[idx].parent_fid && !free_idx)
1314 BNXT_TF_DBG(ERR, "parent child db is full\n");
1319 /* set the Fid in the parent child */
1320 p_pdb->parent_flow_tbl[free_idx].parent_fid = fid;
1325 * Free the entry in the parent-child database
1327 * ulp_ctxt [in] Ptr to ulp_context
1328 * fid [in] The flow id to the flow entry
1330 * returns 0 on success and negative on failure.
1333 ulp_flow_db_parent_flow_free(struct bnxt_ulp_context *ulp_ctxt,
1336 struct bnxt_ulp_flow_db *flow_db;
1337 struct ulp_fdb_parent_child_db *p_pdb;
1340 /* validate the arguments */
1341 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1343 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1347 p_pdb = &flow_db->parent_child_db;
1348 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1349 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1350 /* free the contents */
1351 p_pdb->parent_flow_tbl[idx].parent_fid = 0;
1352 memset(p_pdb->parent_flow_tbl[idx].child_fid_bitset,
1353 0, p_pdb->child_bitset_size);
1357 BNXT_TF_DBG(ERR, "parent entry not found = %x\n", fid);
1362 * Set or reset the child flow in the parent-child database
1364 * ulp_ctxt [in] Ptr to ulp_context
1365 * parent_fid [in] The flow id of the parent flow entry
1366 * child_fid [in] The flow id of the child flow entry
1367 * set_flag [in] Use 1 for setting child, 0 to reset
1369 * returns zero on success and negative on failure.
1372 ulp_flow_db_parent_child_flow_set(struct bnxt_ulp_context *ulp_ctxt,
1373 uint32_t parent_fid,
1377 struct bnxt_ulp_flow_db *flow_db;
1378 struct ulp_fdb_parent_child_db *p_pdb;
1379 uint32_t idx, a_idx;
1382 /* validate the arguments */
1383 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1385 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1389 /* check for fid validity */
1390 if (child_fid >= flow_db->flow_tbl.num_flows || !child_fid) {
1391 BNXT_TF_DBG(ERR, "Invalid child flow index %x\n", child_fid);
1395 p_pdb = &flow_db->parent_child_db;
1396 a_idx = child_fid / ULP_INDEX_BITMAP_SIZE;
1397 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1398 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1399 t = p_pdb->parent_flow_tbl[idx].child_fid_bitset;
1401 ULP_INDEX_BITMAP_SET(t[a_idx], child_fid);
1403 ULP_INDEX_BITMAP_RESET(t[a_idx], child_fid);
1407 BNXT_TF_DBG(ERR, "Unable to set the parent-child flow %x:%x\n",
1408 parent_fid, child_fid);
1413 * Get the parent index from the parent-child database
1415 * ulp_ctxt [in] Ptr to ulp_context
1416 * parent_fid [in] The flow id of the parent flow entry
1417 * parent_idx [out] The parent index of parent flow entry
1419 * returns zero on success and negative on failure.
1422 ulp_flow_db_parent_flow_idx_get(struct bnxt_ulp_context *ulp_ctxt,
1423 uint32_t parent_fid,
1424 uint32_t *parent_idx)
1426 struct bnxt_ulp_flow_db *flow_db;
1427 struct ulp_fdb_parent_child_db *p_pdb;
1430 /* validate the arguments */
1431 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1433 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1437 p_pdb = &flow_db->parent_child_db;
1438 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1439 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1444 BNXT_TF_DBG(ERR, "Unable to get the parent flow %x\n", parent_fid);
1449 * Get the next child flow in the parent-child database
1451 * ulp_ctxt [in] Ptr to ulp_context
1452 * parent_fid [in] The flow id of the parent flow entry
1453 * child_fid [in/out] The flow id of the child flow entry
1455 * returns zero on success and negative on failure.
1456 * Pass child_fid as zero for first entry.
1459 ulp_flow_db_parent_child_flow_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
1460 uint32_t parent_idx,
1461 uint32_t *child_fid)
1463 struct ulp_fdb_parent_child_db *p_pdb;
1464 uint32_t idx, s_idx, mod_fid;
1465 uint32_t next_fid = *child_fid;
1466 uint64_t *child_bitset;
1469 /* check for fid validity */
1470 p_pdb = &flow_db->parent_child_db;
1471 if (parent_idx >= p_pdb->entries_count ||
1472 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1473 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1477 child_bitset = p_pdb->parent_flow_tbl[parent_idx].child_fid_bitset;
1479 /* increment the flow id to find the next valid flow id */
1481 if (next_fid >= flow_db->flow_tbl.num_flows)
1483 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1484 mod_fid = next_fid % ULP_INDEX_BITMAP_SIZE;
1486 while (!(bs = child_bitset[idx])) {
1488 if ((idx * ULP_INDEX_BITMAP_SIZE) >=
1489 flow_db->flow_tbl.num_flows)
1493 * remove the previous bits in the bitset bs to find the
1494 * next non zero bit in the bitset. This needs to be done
1495 * only if the idx is same as he one you started.
1498 bs &= (-1UL >> mod_fid);
1499 next_fid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1500 if (*child_fid >= next_fid) {
1501 BNXT_TF_DBG(ERR, "Parent Child Database is corrupt\n");
1504 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1505 } while (!ULP_INDEX_BITMAP_GET(child_bitset[idx], next_fid));
1506 *child_fid = next_fid;
1511 * Set the counter accumulation in the parent flow
1513 * ulp_ctxt [in] Ptr to ulp_context
1514 * parent_idx [in] The parent index of the parent flow entry
1516 * returns index on success and negative on failure.
1519 ulp_flow_db_parent_flow_count_accum_set(struct bnxt_ulp_context *ulp_ctxt,
1520 uint32_t parent_idx)
1522 struct bnxt_ulp_flow_db *flow_db;
1523 struct ulp_fdb_parent_child_db *p_pdb;
1525 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1527 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1531 /* check for parent idx validity */
1532 p_pdb = &flow_db->parent_child_db;
1533 if (parent_idx >= p_pdb->entries_count ||
1534 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1535 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1539 p_pdb->parent_flow_tbl[parent_idx].counter_acc = 1;
1544 * Get the counter accumulation in the parent flow
1546 * ulp_ctxt [in] Ptr to ulp_context
1547 * parent_fid [in] The flow id of the parent flow entry
1549 * returns 0 if counter accum is set else -1.
1552 ulp_flow_db_parent_flow_count_accum_get(struct bnxt_ulp_context *ulp_ctxt,
1553 uint32_t parent_fid)
1555 struct bnxt_ulp_flow_db *flow_db;
1556 struct ulp_fdb_parent_child_db *p_pdb;
1559 /* validate the arguments */
1560 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1562 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1566 p_pdb = &flow_db->parent_child_db;
1567 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1568 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1569 if (p_pdb->parent_flow_tbl[idx].counter_acc)
1578 * Orphan the child flow entry
1579 * This is called only for child flows that have
1580 * BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW resource
1582 * ulp_ctxt [in] Ptr to ulp_context
1583 * flow_type [in] Specify it is regular or default flow
1584 * fid [in] The index to the flow entry
1586 * Returns 0 on success and negative on failure.
1589 ulp_flow_db_child_flow_reset(struct bnxt_ulp_context *ulp_ctxt,
1590 enum bnxt_ulp_fdb_type flow_type,
1593 struct bnxt_ulp_flow_db *flow_db;
1594 struct bnxt_ulp_flow_tbl *flow_tbl;
1595 struct ulp_fdb_resource_info *fid_res;
1596 uint32_t res_id = 0;
1598 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1600 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1604 if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
1605 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1609 flow_tbl = &flow_db->flow_tbl;
1610 /* check for max flows */
1611 if (fid >= flow_tbl->num_flows || !fid) {
1612 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
1616 /* check if the flow is active or not */
1617 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
1618 BNXT_TF_DBG(ERR, "flow does not exist\n");
1622 /* Iterate the resource to get the resource handle */
1625 fid_res = &flow_tbl->flow_resources[res_id];
1626 if (ulp_flow_db_resource_func_get(fid_res) ==
1627 BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW) {
1628 /* invalidate the resource details */
1629 fid_res->resource_hndl = 0;
1633 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1640 * Create parent flow in the parent flow tbl
1642 * parms [in] Ptr to mapper params
1644 * Returns 0 on success and negative on failure.
1647 ulp_flow_db_parent_flow_create(struct bnxt_ulp_mapper_parms *parms)
1649 struct ulp_flow_db_res_params fid_parms;
1650 uint32_t sub_type = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TYPE_INT_COUNT_ACC;
1651 struct ulp_flow_db_res_params res_params;
1652 int32_t fid_idx, rc;
1654 /* create the child flow entry in parent flow table */
1655 fid_idx = ulp_flow_db_parent_flow_alloc(parms->ulp_ctx, parms->fid);
1657 BNXT_TF_DBG(ERR, "Error in creating parent flow fid %x\n",
1662 /* Add the parent details in the resource list of the flow */
1663 memset(&fid_parms, 0, sizeof(fid_parms));
1664 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW;
1665 fid_parms.resource_hndl = fid_idx;
1666 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1667 if (ulp_flow_db_resource_add(parms->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
1668 parms->fid, &fid_parms)) {
1669 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1674 /* check of the flow has internal counter accumulation enabled */
1675 if (!ulp_flow_db_resource_params_get(parms->ulp_ctx,
1676 BNXT_ULP_FDB_TYPE_REGULAR,
1678 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1681 /* Enable the counter accumulation in parent entry */
1682 if (ulp_flow_db_parent_flow_count_accum_set(parms->ulp_ctx,
1684 BNXT_TF_DBG(ERR, "Error in setting counter acc %x\n",
1690 rc = ulp_flow_db_parent_tun_idx_set(parms->ulp_ctx, fid_idx,
1693 BNXT_TF_DBG(ERR, "Error setting tun_idx in the parent flow\n");
1701 * Create child flow in the parent flow tbl
1703 * parms [in] Ptr to mapper params
1705 * Returns 0 on success and negative on failure.
1708 ulp_flow_db_child_flow_create(struct bnxt_ulp_mapper_parms *parms)
1710 struct ulp_flow_db_res_params fid_parms;
1711 uint32_t sub_type = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TYPE_INT_COUNT;
1712 enum bnxt_ulp_resource_func res_fun;
1713 struct ulp_flow_db_res_params res_p;
1714 uint32_t parent_fid = parms->parent_fid;
1717 /* create the parent flow entry in parent flow table */
1718 rc = ulp_flow_db_parent_child_flow_set(parms->ulp_ctx,
1722 BNXT_TF_DBG(ERR, "Error in setting child fid %x\n", parms->fid);
1726 /* Add the parent details in the resource list of the flow */
1727 memset(&fid_parms, 0, sizeof(fid_parms));
1728 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW;
1729 fid_parms.resource_hndl = parms->parent_fid;
1730 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1731 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1732 BNXT_ULP_FDB_TYPE_REGULAR,
1733 parms->fid, &fid_parms);
1735 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1740 /* check if accumulation count is set for parent flow */
1741 rc = ulp_flow_db_parent_flow_count_accum_get(parms->ulp_ctx,
1744 /* check if internal count action included for this flow.*/
1745 res_fun = BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE;
1746 rc = ulp_flow_db_resource_params_get(parms->ulp_ctx,
1747 BNXT_ULP_FDB_TYPE_REGULAR,
1753 /* update the counter manager to include parent fid */
1754 if (ulp_fc_mgr_cntr_parent_flow_set(parms->ulp_ctx,
1756 res_p.resource_hndl,
1758 BNXT_TF_DBG(ERR, "Error in setting child %x\n",
1764 /* return success */
1769 * Update the parent counters
1771 * ulp_ctxt [in] Ptr to ulp_context
1772 * parent_fid [in] The flow id of the parent flow entry
1773 * packet_count [in] - packet count
1774 * byte_count [in] - byte count
1776 * returns 0 on success
1779 ulp_flow_db_parent_flow_count_update(struct bnxt_ulp_context *ulp_ctxt,
1780 uint32_t parent_fid,
1781 uint64_t packet_count,
1782 uint64_t byte_count)
1784 struct bnxt_ulp_flow_db *flow_db;
1785 struct ulp_fdb_parent_child_db *p_pdb;
1788 /* validate the arguments */
1789 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1791 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1795 p_pdb = &flow_db->parent_child_db;
1796 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1797 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1798 if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1799 p_pdb->parent_flow_tbl[idx].pkt_count +=
1801 p_pdb->parent_flow_tbl[idx].byte_count +=
1811 * Get the parent accumulation counters
1813 * ulp_ctxt [in] Ptr to ulp_context
1814 * parent_fid [in] The flow id of the parent flow entry
1815 * packet_count [out] - packet count
1816 * byte_count [out] - byte count
1818 * returns 0 on success
1821 ulp_flow_db_parent_flow_count_get(struct bnxt_ulp_context *ulp_ctxt,
1822 uint32_t parent_fid, uint64_t *packet_count,
1823 uint64_t *byte_count, uint8_t count_reset)
1825 struct bnxt_ulp_flow_db *flow_db;
1826 struct ulp_fdb_parent_child_db *p_pdb;
1829 /* validate the arguments */
1830 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1832 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1836 p_pdb = &flow_db->parent_child_db;
1837 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1838 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1839 if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1841 p_pdb->parent_flow_tbl[idx].pkt_count;
1843 p_pdb->parent_flow_tbl[idx].byte_count;
1845 p_pdb->parent_flow_tbl[idx].pkt_count = 0;
1846 p_pdb->parent_flow_tbl[idx].byte_count = 0;
1856 * reset the parent accumulation counters
1858 * ulp_ctxt [in] Ptr to ulp_context
1863 ulp_flow_db_parent_flow_count_reset(struct bnxt_ulp_context *ulp_ctxt)
1865 struct bnxt_ulp_flow_db *flow_db;
1866 struct ulp_fdb_parent_child_db *p_pdb;
1869 /* validate the arguments */
1870 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, 1);
1872 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1876 p_pdb = &flow_db->parent_child_db;
1877 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1878 if (p_pdb->parent_flow_tbl[idx].parent_fid &&
1879 p_pdb->parent_flow_tbl[idx].counter_acc) {
1880 p_pdb->parent_flow_tbl[idx].pkt_count = 0;
1881 p_pdb->parent_flow_tbl[idx].byte_count = 0;