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 || flow_type ==
52 BNXT_ULP_FDB_TYPE_RID)
53 ULP_INDEX_BITMAP_SET(f_tbl->active_reg_flows[a_idx],
55 if (flow_type == BNXT_ULP_FDB_TYPE_DEFAULT || flow_type ==
56 BNXT_ULP_FDB_TYPE_RID)
57 ULP_INDEX_BITMAP_SET(f_tbl->active_dflt_flows[a_idx],
60 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR || flow_type ==
61 BNXT_ULP_FDB_TYPE_RID)
62 ULP_INDEX_BITMAP_RESET(f_tbl->active_reg_flows[a_idx],
64 if (flow_type == BNXT_ULP_FDB_TYPE_DEFAULT || flow_type ==
65 BNXT_ULP_FDB_TYPE_RID)
66 ULP_INDEX_BITMAP_RESET(f_tbl->active_dflt_flows[a_idx],
72 * Helper function to check if given fid is active flow.
73 * No validation being done in this function.
75 * flow_db [in] Ptr to flow database
76 * flow_type [in] - specify default or regular
77 * idx [in] The index to bit to be set or reset.
79 * returns 1 on set or 0 if not set.
82 ulp_flow_db_active_flows_bit_is_set(struct bnxt_ulp_flow_db *flow_db,
83 enum bnxt_ulp_fdb_type flow_type,
86 struct bnxt_ulp_flow_tbl *f_tbl = &flow_db->flow_tbl;
87 uint32_t a_idx = idx / ULP_INDEX_BITMAP_SIZE;
89 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
90 return ULP_INDEX_BITMAP_GET(f_tbl->active_reg_flows[a_idx],
92 else if (flow_type == BNXT_ULP_FDB_TYPE_DEFAULT)
93 return ULP_INDEX_BITMAP_GET(f_tbl->active_dflt_flows[a_idx],
95 else if (flow_type == BNXT_ULP_FDB_TYPE_RID)
96 return (ULP_INDEX_BITMAP_GET(f_tbl->active_reg_flows[a_idx],
98 ULP_INDEX_BITMAP_GET(f_tbl->active_reg_flows[a_idx],
103 static inline enum tf_dir
104 ulp_flow_db_resource_dir_get(struct ulp_fdb_resource_info *res_info)
106 return ((res_info->nxt_resource_idx & ULP_FLOW_DB_RES_DIR_MASK) >>
107 ULP_FLOW_DB_RES_DIR_BIT);
111 ulp_flow_db_resource_func_get(struct ulp_fdb_resource_info *res_info)
115 func = (((res_info->nxt_resource_idx & ULP_FLOW_DB_RES_FUNC_MASK) >>
116 ULP_FLOW_DB_RES_FUNC_BITS) << ULP_FLOW_DB_RES_FUNC_UPPER);
117 /* The reource func is split into upper and lower */
118 if (func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER)
119 return (func | res_info->resource_func_lower);
124 * Helper function to copy the resource params to resource info
125 * No validation being done in this function.
127 * resource_info [out] Ptr to resource information
128 * params [in] The input params from the caller
132 ulp_flow_db_res_params_to_info(struct ulp_fdb_resource_info *resource_info,
133 struct ulp_flow_db_res_params *params)
135 uint32_t resource_func;
137 resource_info->nxt_resource_idx |= ((params->direction <<
138 ULP_FLOW_DB_RES_DIR_BIT) &
139 ULP_FLOW_DB_RES_DIR_MASK);
140 resource_func = (params->resource_func >> ULP_FLOW_DB_RES_FUNC_UPPER);
141 resource_info->nxt_resource_idx |= ((resource_func <<
142 ULP_FLOW_DB_RES_FUNC_BITS) &
143 ULP_FLOW_DB_RES_FUNC_MASK);
145 if (params->resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
146 /* Break the resource func into two parts */
147 resource_func = (params->resource_func &
148 ULP_FLOW_DB_RES_FUNC_LOWER_MASK);
149 resource_info->resource_func_lower = resource_func;
152 /* Store the handle as 64bit only for EM table entries */
153 if (params->resource_func != BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE &&
154 params->resource_func != BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
155 resource_info->resource_hndl = (uint32_t)params->resource_hndl;
156 resource_info->resource_type = params->resource_type;
157 resource_info->resource_sub_type = params->resource_sub_type;
158 resource_info->reserved = params->reserved;
160 resource_info->resource_em_handle = params->resource_hndl;
165 * Helper function to copy the resource params to resource info
166 * No validation being done in this function.
168 * resource_info [in] Ptr to resource information
169 * params [out] The output params to the caller
174 ulp_flow_db_res_info_to_params(struct ulp_fdb_resource_info *resource_info,
175 struct ulp_flow_db_res_params *params)
177 memset(params, 0, sizeof(struct ulp_flow_db_res_params));
179 /* use the helper function to get the resource func */
180 params->direction = ulp_flow_db_resource_dir_get(resource_info);
181 params->resource_func = ulp_flow_db_resource_func_get(resource_info);
183 if (params->resource_func == BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE ||
184 params->resource_func == BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
185 params->resource_hndl = resource_info->resource_em_handle;
186 } else if (params->resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
187 params->resource_hndl = resource_info->resource_hndl;
188 params->resource_type = resource_info->resource_type;
189 params->resource_sub_type = resource_info->resource_sub_type;
190 params->reserved = resource_info->reserved;
195 * Helper function to allocate the flow table and initialize
196 * the stack for allocation operations.
198 * flow_db [in] Ptr to flow database structure
200 * Returns 0 on success or negative number on failure.
203 ulp_flow_db_alloc_resource(struct bnxt_ulp_flow_db *flow_db)
206 struct bnxt_ulp_flow_tbl *flow_tbl;
209 flow_tbl = &flow_db->flow_tbl;
211 size = sizeof(struct ulp_fdb_resource_info) * flow_tbl->num_resources;
212 flow_tbl->flow_resources =
213 rte_zmalloc("ulp_fdb_resource_info", size, 0);
215 if (!flow_tbl->flow_resources) {
216 BNXT_TF_DBG(ERR, "Failed to alloc memory for flow table\n");
219 size = sizeof(uint32_t) * flow_tbl->num_resources;
220 flow_tbl->flow_tbl_stack = rte_zmalloc("flow_tbl_stack", size, 0);
221 if (!flow_tbl->flow_tbl_stack) {
222 BNXT_TF_DBG(ERR, "Failed to alloc memory flow tbl stack\n");
225 size = (flow_tbl->num_flows / sizeof(uint64_t)) + 1;
226 size = ULP_BYTE_ROUND_OFF_8(size);
227 flow_tbl->active_reg_flows = rte_zmalloc("active reg flows", size,
228 ULP_BUFFER_ALIGN_64_BYTE);
229 if (!flow_tbl->active_reg_flows) {
230 BNXT_TF_DBG(ERR, "Failed to alloc memory active reg flows\n");
234 flow_tbl->active_dflt_flows = rte_zmalloc("active dflt flows", size,
235 ULP_BUFFER_ALIGN_64_BYTE);
236 if (!flow_tbl->active_dflt_flows) {
237 BNXT_TF_DBG(ERR, "Failed to alloc memory active dflt flows\n");
241 /* Initialize the stack table. */
242 for (idx = 0; idx < flow_tbl->num_resources; idx++)
243 flow_tbl->flow_tbl_stack[idx] = idx;
245 /* Ignore the first element in the list. */
246 flow_tbl->head_index = 1;
247 /* Tail points to the last entry in the list. */
248 flow_tbl->tail_index = flow_tbl->num_resources - 1;
253 * Helper function to deallocate the flow table.
255 * flow_db [in] Ptr to flow database structure
260 ulp_flow_db_dealloc_resource(struct bnxt_ulp_flow_db *flow_db)
262 struct bnxt_ulp_flow_tbl *flow_tbl = &flow_db->flow_tbl;
264 /* Free all the allocated tables in the flow table. */
265 if (flow_tbl->active_reg_flows) {
266 rte_free(flow_tbl->active_reg_flows);
267 flow_tbl->active_reg_flows = NULL;
269 if (flow_tbl->active_dflt_flows) {
270 rte_free(flow_tbl->active_dflt_flows);
271 flow_tbl->active_dflt_flows = NULL;
274 if (flow_tbl->flow_tbl_stack) {
275 rte_free(flow_tbl->flow_tbl_stack);
276 flow_tbl->flow_tbl_stack = NULL;
279 if (flow_tbl->flow_resources) {
280 rte_free(flow_tbl->flow_resources);
281 flow_tbl->flow_resources = NULL;
286 * Helper function to add function id to the flow table
288 * flow_db [in] Ptr to flow table
289 * flow_id [in] The flow id of the flow
290 * func_id [in] The func_id to be set, for reset pass zero
295 ulp_flow_db_func_id_set(struct bnxt_ulp_flow_db *flow_db,
299 /* set the function id in the function table */
300 if (flow_id < flow_db->func_id_tbl_size)
301 flow_db->func_id_tbl[flow_id] = func_id;
302 else /* This should never happen */
303 BNXT_TF_DBG(ERR, "Invalid flow id, flowdb corrupt\n");
307 * Initialize the parent-child database. Memory is allocated in this
308 * call and assigned to the database
310 * flow_db [in] Ptr to flow table
311 * num_entries[in] - number of entries to allocate
313 * Returns 0 on success or negative number on failure.
316 ulp_flow_db_parent_tbl_init(struct bnxt_ulp_flow_db *flow_db,
317 uint32_t num_entries)
319 struct ulp_fdb_parent_child_db *p_db;
325 /* update the sizes for the allocation */
326 p_db = &flow_db->parent_child_db;
327 p_db->child_bitset_size = (flow_db->flow_tbl.num_flows /
328 sizeof(uint64_t)) + 1; /* size in bytes */
329 p_db->child_bitset_size = ULP_BYTE_ROUND_OFF_8(p_db->child_bitset_size);
330 p_db->entries_count = num_entries;
332 /* allocate the memory */
333 p_db->parent_flow_tbl = rte_zmalloc("fdb parent flow tbl",
334 sizeof(struct ulp_fdb_parent_info) *
335 p_db->entries_count, 0);
336 if (!p_db->parent_flow_tbl) {
338 "Failed to allocate memory fdb parent flow tbl\n");
341 size = p_db->child_bitset_size * p_db->entries_count;
344 * allocate the big chunk of memory to be statically carved into
345 * child_fid_bitset pointer.
347 p_db->parent_flow_tbl_mem = rte_zmalloc("fdb parent flow tbl mem",
349 ULP_BUFFER_ALIGN_64_BYTE);
350 if (!p_db->parent_flow_tbl_mem) {
352 "Failed to allocate memory fdb parent flow mem\n");
356 /* set the pointers in parent table to their offsets */
357 for (idx = 0 ; idx < p_db->entries_count; idx++) {
358 p_db->parent_flow_tbl[idx].child_fid_bitset =
359 (uint64_t *)&p_db->parent_flow_tbl_mem[idx *
360 p_db->child_bitset_size];
367 * Deinitialize the parent-child database. Memory is deallocated in
368 * this call and all flows should have been purged before this
371 * flow_db [in] Ptr to flow table
376 ulp_flow_db_parent_tbl_deinit(struct bnxt_ulp_flow_db *flow_db)
378 /* free the memory related to parent child database */
379 if (flow_db->parent_child_db.parent_flow_tbl_mem) {
380 rte_free(flow_db->parent_child_db.parent_flow_tbl_mem);
381 flow_db->parent_child_db.parent_flow_tbl_mem = NULL;
383 if (flow_db->parent_child_db.parent_flow_tbl) {
384 rte_free(flow_db->parent_child_db.parent_flow_tbl);
385 flow_db->parent_child_db.parent_flow_tbl = NULL;
389 /* internal validation function for parent flow tbl */
390 static struct bnxt_ulp_flow_db *
391 ulp_flow_db_parent_arg_validation(struct bnxt_ulp_context *ulp_ctxt,
394 struct bnxt_ulp_flow_db *flow_db;
396 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
398 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
402 /* check for max flows */
403 if (fid >= flow_db->flow_tbl.num_flows || !fid) {
404 BNXT_TF_DBG(ERR, "Invalid flow index\n");
408 /* No support for parent child db then just exit */
409 if (!flow_db->parent_child_db.entries_count) {
410 BNXT_TF_DBG(ERR, "parent child db not supported\n");
418 * Set the tunnel index in the parent flow
420 * ulp_ctxt [in] Ptr to ulp_context
421 * parent_idx [in] The parent index of the parent flow entry
423 * returns index on success and negative on failure.
426 ulp_flow_db_parent_tun_idx_set(struct bnxt_ulp_context *ulp_ctxt,
427 uint32_t parent_idx, uint8_t tun_idx)
429 struct bnxt_ulp_flow_db *flow_db;
430 struct ulp_fdb_parent_child_db *p_pdb;
432 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
434 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
438 /* check for parent idx validity */
439 p_pdb = &flow_db->parent_child_db;
440 if (parent_idx >= p_pdb->entries_count ||
441 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
442 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
446 p_pdb->parent_flow_tbl[parent_idx].tun_idx = tun_idx;
451 * Get the tunnel index from the parent flow
453 * ulp_ctxt [in] Ptr to ulp_context
454 * parent_fid [in] The flow id of the parent flow entry
456 * returns 0 if counter accum is set else -1.
459 ulp_flow_db_parent_tun_idx_get(struct bnxt_ulp_context *ulp_ctxt,
460 uint32_t parent_fid, uint8_t *tun_idx)
462 struct bnxt_ulp_flow_db *flow_db;
463 struct ulp_fdb_parent_child_db *p_pdb;
466 /* validate the arguments */
467 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
469 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
473 p_pdb = &flow_db->parent_child_db;
474 for (idx = 0; idx < p_pdb->entries_count; idx++) {
475 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
476 *tun_idx = p_pdb->parent_flow_tbl[idx].tun_idx;
485 * Initialize the flow database. Memory is allocated in this
486 * call and assigned to the flow database.
488 * ulp_ctxt [in] Ptr to ulp context
490 * Returns 0 on success or negative number on failure.
493 ulp_flow_db_init(struct bnxt_ulp_context *ulp_ctxt)
495 struct bnxt_ulp_device_params *dparms;
496 struct bnxt_ulp_flow_tbl *flow_tbl;
497 struct bnxt_ulp_flow_db *flow_db;
498 uint32_t dev_id, num_flows;
499 enum bnxt_ulp_flow_mem_type mtype;
501 /* Get the dev specific number of flows that needed to be supported. */
502 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctxt, &dev_id)) {
503 BNXT_TF_DBG(ERR, "Invalid device id\n");
507 dparms = bnxt_ulp_device_params_get(dev_id);
509 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
513 flow_db = rte_zmalloc("bnxt_ulp_flow_db",
514 sizeof(struct bnxt_ulp_flow_db), 0);
517 "Failed to allocate memory for flow table ptr\n");
521 /* Attach the flow database to the ulp context. */
522 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, flow_db);
524 /* Determine the number of flows based on EM type */
525 bnxt_ulp_cntxt_mem_type_get(ulp_ctxt, &mtype);
526 if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT)
527 num_flows = dparms->int_flow_db_num_entries;
529 num_flows = dparms->ext_flow_db_num_entries;
531 /* Populate the regular flow table limits. */
532 flow_tbl = &flow_db->flow_tbl;
533 flow_tbl->num_flows = num_flows + 1;
534 flow_tbl->num_resources = ((num_flows + 1) *
535 dparms->num_resources_per_flow);
537 /* Include the default flow table limits. */
538 flow_tbl->num_flows += (BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1);
539 flow_tbl->num_resources += ((BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1) *
540 BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES);
542 /* Allocate the resource for the flow table. */
543 if (ulp_flow_db_alloc_resource(flow_db))
546 /* add 1 since we are not using index 0 for flow id */
547 flow_db->func_id_tbl_size = flow_tbl->num_flows + 1;
548 /* Allocate the function Id table */
549 flow_db->func_id_tbl = rte_zmalloc("bnxt_ulp_flow_db_func_id_table",
550 flow_db->func_id_tbl_size *
551 sizeof(uint16_t), 0);
552 if (!flow_db->func_id_tbl) {
554 "Failed to allocate mem for flow table func id\n");
557 /* initialize the parent child database */
558 if (ulp_flow_db_parent_tbl_init(flow_db,
559 dparms->fdb_parent_flow_entries)) {
561 "Failed to allocate mem for parent child db\n");
565 /* All good so return. */
566 BNXT_TF_DBG(INFO, "FlowDB initialized with %d flows.\n",
567 flow_tbl->num_flows);
570 ulp_flow_db_deinit(ulp_ctxt);
575 * Deinitialize the flow database. Memory is deallocated in
576 * this call and all flows should have been purged before this
579 * ulp_ctxt [in] Ptr to ulp context
581 * Returns 0 on success.
584 ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
586 struct bnxt_ulp_flow_db *flow_db;
588 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
592 /* Detach the flow database from the ulp context. */
593 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, NULL);
595 /* Free up all the memory. */
596 ulp_flow_db_parent_tbl_deinit(flow_db);
597 ulp_flow_db_dealloc_resource(flow_db);
598 rte_free(flow_db->func_id_tbl);
605 * Allocate the flow database entry
607 * ulp_ctxt [in] Ptr to ulp_context
608 * flow_type [in] - specify default or regular
609 * func_id [in].function id of the ingress port
610 * fid [out] The index to the flow entry
612 * returns 0 on success and negative on failure.
615 ulp_flow_db_fid_alloc(struct bnxt_ulp_context *ulp_ctxt,
616 enum bnxt_ulp_fdb_type flow_type,
620 struct bnxt_ulp_flow_db *flow_db;
621 struct bnxt_ulp_flow_tbl *flow_tbl;
623 *fid = 0; /* Initialize fid to invalid value */
624 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
626 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
630 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
631 BNXT_TF_DBG(ERR, "Invalid flow type\n");
635 flow_tbl = &flow_db->flow_tbl;
636 /* check for max flows */
637 if (flow_tbl->num_flows <= flow_tbl->head_index) {
638 BNXT_TF_DBG(ERR, "Flow database has reached max flows\n");
641 if (flow_tbl->tail_index <= (flow_tbl->head_index + 1)) {
642 BNXT_TF_DBG(ERR, "Flow database has reached max resources\n");
645 *fid = flow_tbl->flow_tbl_stack[flow_tbl->head_index];
646 flow_tbl->head_index++;
648 /* Set the flow type */
649 ulp_flow_db_active_flows_bit_set(flow_db, flow_type, *fid, 1);
651 /* function id update is only valid for regular flow table */
652 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
653 ulp_flow_db_func_id_set(flow_db, *fid, func_id);
660 * Allocate the flow database entry.
661 * The params->critical_resource has to be set to 0 to allocate a new resource.
663 * ulp_ctxt [in] Ptr to ulp_context
664 * flow_type [in] Specify it is regular or default flow
665 * fid [in] The index to the flow entry
666 * params [in] The contents to be copied into resource
668 * returns 0 on success and negative on failure.
671 ulp_flow_db_resource_add(struct bnxt_ulp_context *ulp_ctxt,
672 enum bnxt_ulp_fdb_type flow_type,
674 struct ulp_flow_db_res_params *params)
676 struct bnxt_ulp_flow_db *flow_db;
677 struct bnxt_ulp_flow_tbl *flow_tbl;
678 struct ulp_fdb_resource_info *resource, *fid_resource;
681 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
683 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
687 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
688 BNXT_TF_DBG(ERR, "Invalid flow type\n");
692 flow_tbl = &flow_db->flow_tbl;
693 /* check for max flows */
694 if (fid >= flow_tbl->num_flows || !fid) {
695 BNXT_TF_DBG(ERR, "Invalid flow index\n");
699 /* check if the flow is active or not */
700 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
701 BNXT_TF_DBG(ERR, "flow does not exist %x:%x\n", flow_type, fid);
705 /* check for max resource */
706 if ((flow_tbl->head_index + 1) >= flow_tbl->tail_index) {
707 BNXT_TF_DBG(ERR, "Flow db has reached max resources\n");
710 fid_resource = &flow_tbl->flow_resources[fid];
712 if (!params->critical_resource) {
713 /* Not the critical_resource so allocate a resource */
714 idx = flow_tbl->flow_tbl_stack[flow_tbl->tail_index];
715 resource = &flow_tbl->flow_resources[idx];
716 flow_tbl->tail_index--;
718 /* Update the chain list of resource*/
719 ULP_FLOW_DB_RES_NXT_SET(resource->nxt_resource_idx,
720 fid_resource->nxt_resource_idx);
721 /* update the contents */
722 ulp_flow_db_res_params_to_info(resource, params);
723 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
724 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
727 /* critical resource. Just update the fid resource */
728 ulp_flow_db_res_params_to_info(fid_resource, params);
731 if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
732 params->resource_sub_type ==
733 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
734 /* Store the first HW counter ID for this table */
735 if (!ulp_fc_mgr_start_idx_isset(ulp_ctxt, params->direction))
736 ulp_fc_mgr_start_idx_set(ulp_ctxt, params->direction,
737 params->resource_hndl);
739 ulp_fc_mgr_cntr_set(ulp_ctxt, params->direction,
740 params->resource_hndl);
742 if (!ulp_fc_mgr_thread_isstarted(ulp_ctxt))
743 ulp_fc_mgr_thread_start(ulp_ctxt);
746 /* all good, return success */
751 * Free the flow database entry.
752 * The params->critical_resource has to be set to 1 to free the first resource.
754 * ulp_ctxt [in] Ptr to ulp_context
755 * flow_type [in] Specify it is regular or default flow
756 * fid [in] The index to the flow entry
757 * params [in/out] The contents to be copied into params.
758 * Onlythe critical_resource needs to be set by the caller.
760 * Returns 0 on success and negative on failure.
763 ulp_flow_db_resource_del(struct bnxt_ulp_context *ulp_ctxt,
764 enum bnxt_ulp_fdb_type flow_type,
766 struct ulp_flow_db_res_params *params)
768 struct bnxt_ulp_flow_db *flow_db;
769 struct bnxt_ulp_flow_tbl *flow_tbl;
770 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
771 uint32_t nxt_idx = 0;
772 struct bnxt_tun_cache_entry *tun_tbl;
776 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
778 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
782 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
783 BNXT_TF_DBG(ERR, "Invalid flow type\n");
787 flow_tbl = &flow_db->flow_tbl;
788 /* check for max flows */
789 if (fid >= flow_tbl->num_flows || !fid) {
790 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
794 /* check if the flow is active or not */
795 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
796 BNXT_TF_DBG(ERR, "flow does not exist %x:%x\n", flow_type, fid);
800 fid_resource = &flow_tbl->flow_resources[fid];
801 if (!params->critical_resource) {
802 /* Not the critical resource so free the resource */
803 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
804 fid_resource->nxt_resource_idx);
806 /* reached end of resources */
809 nxt_resource = &flow_tbl->flow_resources[nxt_idx];
811 /* connect the fid resource to the next resource */
812 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
813 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
814 nxt_resource->nxt_resource_idx);
816 /* update the contents to be given to caller */
817 ulp_flow_db_res_info_to_params(nxt_resource, params);
819 /* Delete the nxt_resource */
820 memset(nxt_resource, 0, sizeof(struct ulp_fdb_resource_info));
822 /* add it to the free list */
823 flow_tbl->tail_index++;
824 if (flow_tbl->tail_index >= flow_tbl->num_resources) {
825 BNXT_TF_DBG(ERR, "FlowDB:Tail reached max\n");
828 flow_tbl->flow_tbl_stack[flow_tbl->tail_index] = nxt_idx;
831 /* Critical resource. copy the contents and exit */
832 ulp_flow_db_res_info_to_params(fid_resource, params);
833 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
834 fid_resource->nxt_resource_idx);
835 memset(fid_resource, 0, sizeof(struct ulp_fdb_resource_info));
836 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
840 /* Now that the HW Flow counter resource is deleted, reset it's
841 * corresponding slot in the SW accumulation table in the Flow Counter
844 if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
845 params->resource_sub_type ==
846 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
847 ulp_fc_mgr_cntr_reset(ulp_ctxt, params->direction,
848 params->resource_hndl);
851 if (params->resource_func == BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW) {
852 tun_tbl = bnxt_ulp_cntxt_ptr2_tun_tbl_get(ulp_ctxt);
856 rc = ulp_flow_db_parent_tun_idx_get(ulp_ctxt, fid, &tun_idx);
860 ulp_clear_tun_entry(tun_tbl, tun_idx);
863 /* all good, return success */
868 * Free the flow database entry
870 * ulp_ctxt [in] Ptr to ulp_context
871 * flow_type [in] - specify default or regular
872 * fid [in] The index to the flow entry
874 * returns 0 on success and negative on failure.
877 ulp_flow_db_fid_free(struct bnxt_ulp_context *ulp_ctxt,
878 enum bnxt_ulp_fdb_type flow_type,
881 struct bnxt_ulp_flow_db *flow_db;
882 struct bnxt_ulp_flow_tbl *flow_tbl;
884 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
886 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
890 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
891 BNXT_TF_DBG(ERR, "Invalid flow type\n");
895 flow_tbl = &flow_db->flow_tbl;
897 /* check for limits of fid */
898 if (fid >= flow_tbl->num_flows || !fid) {
899 BNXT_TF_DBG(ERR, "Invalid flow index\n");
903 /* check if the flow is active or not */
904 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
905 BNXT_TF_DBG(ERR, "flow does not exist %x:%x\n", flow_type, fid);
908 flow_tbl->head_index--;
909 if (!flow_tbl->head_index) {
910 BNXT_TF_DBG(ERR, "FlowDB: Head Ptr is zero\n");
914 flow_tbl->flow_tbl_stack[flow_tbl->head_index] = fid;
916 /* Clear the flows bitmap */
917 ulp_flow_db_active_flows_bit_set(flow_db, flow_type, fid, 0);
919 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
920 ulp_flow_db_func_id_set(flow_db, fid, 0);
922 /* all good, return success */
927 *Get the flow database entry details
929 * ulp_ctxt [in] Ptr to ulp_context
930 * flow_type [in] - specify default or regular
931 * fid [in] The index to the flow entry
932 * nxt_idx [in/out] the index to the next entry
933 * params [out] The contents to be copied into params.
935 * returns 0 on success and negative on failure.
938 ulp_flow_db_resource_get(struct bnxt_ulp_context *ulp_ctxt,
939 enum bnxt_ulp_fdb_type flow_type,
942 struct ulp_flow_db_res_params *params)
944 struct bnxt_ulp_flow_db *flow_db;
945 struct bnxt_ulp_flow_tbl *flow_tbl;
946 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
948 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
950 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
954 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
955 BNXT_TF_DBG(ERR, "Invalid flow type\n");
959 flow_tbl = &flow_db->flow_tbl;
961 /* check for limits of fid */
962 if (fid >= flow_tbl->num_flows || !fid) {
963 BNXT_TF_DBG(ERR, "Invalid flow index\n");
967 /* check if the flow is active or not */
968 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
969 BNXT_TF_DBG(ERR, "flow does not exist\n");
974 fid_resource = &flow_tbl->flow_resources[fid];
975 ulp_flow_db_res_info_to_params(fid_resource, params);
976 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
977 fid_resource->nxt_resource_idx);
979 nxt_resource = &flow_tbl->flow_resources[*nxt_idx];
980 ulp_flow_db_res_info_to_params(nxt_resource, params);
982 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
983 nxt_resource->nxt_resource_idx);
986 /* all good, return success */
991 * Get the flow database entry iteratively
993 * flow_tbl [in] Ptr to flow table
994 * flow_type [in] - specify default or regular
995 * fid [in/out] The index to the flow entry
997 * returns 0 on success and negative on failure.
1000 ulp_flow_db_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
1001 enum bnxt_ulp_fdb_type flow_type,
1004 uint32_t lfid = *fid;
1005 uint32_t idx, s_idx, mod_fid;
1007 uint64_t *active_flows;
1008 struct bnxt_ulp_flow_tbl *flowtbl = &flow_db->flow_tbl;
1010 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR) {
1011 active_flows = flowtbl->active_reg_flows;
1012 } else if (flow_type == BNXT_ULP_FDB_TYPE_DEFAULT) {
1013 active_flows = flowtbl->active_dflt_flows;
1015 BNXT_TF_DBG(ERR, "Invalid flow type %x\n", flow_type);
1020 /* increment the flow id to find the next valid flow id */
1022 if (lfid >= flowtbl->num_flows)
1024 idx = lfid / ULP_INDEX_BITMAP_SIZE;
1025 mod_fid = lfid % ULP_INDEX_BITMAP_SIZE;
1027 while (!(bs = active_flows[idx])) {
1029 if ((idx * ULP_INDEX_BITMAP_SIZE) >= flowtbl->num_flows)
1033 * remove the previous bits in the bitset bs to find the
1034 * next non zero bit in the bitset. This needs to be done
1035 * only if the idx is same as he one you started.
1038 bs &= (-1UL >> mod_fid);
1039 lfid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1041 BNXT_TF_DBG(ERR, "Flow Database is corrupt\n");
1044 } while (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type,
1047 /* all good, return success */
1053 * Flush all flows in the flow database.
1055 * ulp_ctxt [in] Ptr to ulp context
1056 * flow_type [in] - specify default or regular
1058 * returns 0 on success or negative number on failure
1061 ulp_flow_db_flush_flows(struct bnxt_ulp_context *ulp_ctx,
1062 enum bnxt_ulp_fdb_type flow_type)
1065 struct bnxt_ulp_flow_db *flow_db;
1068 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1072 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1074 BNXT_TF_DBG(ERR, "Flow database not found\n");
1077 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1078 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1082 while (!ulp_flow_db_next_entry_get(flow_db, flow_type, &fid))
1083 ulp_mapper_resources_free(ulp_ctx, flow_type, fid);
1085 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1091 * Flush all flows in the flow database that belong to a device function.
1093 * ulp_ctxt [in] Ptr to ulp context
1094 * func_id [in] - The port function id
1096 * returns 0 on success or negative number on failure
1099 ulp_flow_db_function_flow_flush(struct bnxt_ulp_context *ulp_ctx,
1102 uint32_t flow_id = 0;
1103 struct bnxt_ulp_flow_db *flow_db;
1105 if (!ulp_ctx || !func_id) {
1106 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1110 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1112 BNXT_TF_DBG(ERR, "Flow database not found\n");
1115 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1116 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1120 while (!ulp_flow_db_next_entry_get(flow_db, BNXT_ULP_FDB_TYPE_REGULAR,
1122 if (flow_db->func_id_tbl[flow_id] == func_id)
1123 ulp_mapper_resources_free(ulp_ctx,
1124 BNXT_ULP_FDB_TYPE_REGULAR,
1127 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1132 * Flush all flows in the flow database that are associated with the session.
1134 * ulp_ctxt [in] Ptr to ulp context
1136 * returns 0 on success or negative number on failure
1139 ulp_flow_db_session_flow_flush(struct bnxt_ulp_context *ulp_ctx)
1142 * TBD: Tf core implementation of FW session flush shall change this
1145 return ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1149 * Check that flow id matches the function id or not
1151 * ulp_ctxt [in] Ptr to ulp context
1152 * flow_db [in] Ptr to flow table
1153 * func_id [in] The func_id to be set, for reset pass zero.
1155 * returns true on success or false on failure
1158 ulp_flow_db_validate_flow_func(struct bnxt_ulp_context *ulp_ctx,
1162 struct bnxt_ulp_flow_db *flow_db;
1164 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1166 BNXT_TF_DBG(ERR, "Flow database not found\n");
1170 /* set the function id in the function table */
1171 if (flow_id < flow_db->func_id_tbl_size && func_id &&
1172 flow_db->func_id_tbl[flow_id] == func_id)
1179 * Internal api to traverse the resource list within a flow
1180 * and match a resource based on resource func and resource
1181 * sub type. This api should be used only for resources that
1182 * are unique and do not have multiple instances of resource
1183 * func and sub type combination since it will return only
1187 ulp_flow_db_resource_params_get(struct bnxt_ulp_context *ulp_ctx,
1188 enum bnxt_ulp_fdb_type flow_type,
1190 uint32_t resource_func,
1191 uint32_t res_subtype,
1192 struct ulp_flow_db_res_params *params)
1194 struct bnxt_ulp_flow_db *flow_db;
1195 struct bnxt_ulp_flow_tbl *flow_tbl;
1196 struct ulp_fdb_resource_info *fid_res;
1199 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1201 BNXT_TF_DBG(ERR, "Flow database not found\n");
1206 BNXT_TF_DBG(ERR, "invalid argument\n");
1210 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
1211 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1215 flow_tbl = &flow_db->flow_tbl;
1217 /* check for limits of fid */
1218 if (flow_id >= flow_tbl->num_flows || !flow_id) {
1219 BNXT_TF_DBG(ERR, "Invalid flow index\n");
1223 /* check if the flow is active or not */
1224 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, flow_id)) {
1225 BNXT_TF_DBG(ERR, "flow does not exist\n");
1228 /* Iterate the resource to get the resource handle */
1230 memset(params, 0, sizeof(struct ulp_flow_db_res_params));
1232 fid_res = &flow_tbl->flow_resources[res_id];
1233 if (ulp_flow_db_resource_func_get(fid_res) == resource_func) {
1234 if (resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
1235 if (res_subtype == fid_res->resource_sub_type) {
1236 ulp_flow_db_res_info_to_params(fid_res,
1241 } else if (resource_func ==
1242 BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE ||
1244 BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
1245 ulp_flow_db_res_info_to_params(fid_res,
1251 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1257 * Api to get the cfa action pointer from a flow.
1259 * ulp_ctxt [in] Ptr to ulp context
1260 * flow_id [in] flow id
1261 * cfa_action [out] The resource handle stored in the flow database
1263 * returns 0 on success
1266 ulp_default_flow_db_cfa_action_get(struct bnxt_ulp_context *ulp_ctx,
1268 uint16_t *cfa_action)
1270 uint8_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_VFR_CFA_ACTION;
1271 struct ulp_flow_db_res_params params;
1274 rc = ulp_flow_db_resource_params_get(ulp_ctx,
1275 BNXT_ULP_FDB_TYPE_DEFAULT,
1277 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1280 BNXT_TF_DBG(ERR, "CFA Action ptr not found for flow id %u\n",
1284 *cfa_action = params.resource_hndl;
1289 * Allocate the entry in the parent-child database
1291 * ulp_ctxt [in] Ptr to ulp_context
1292 * fid [in] The flow id to the flow entry
1294 * returns index on success and negative on failure.
1297 ulp_flow_db_parent_flow_alloc(struct bnxt_ulp_context *ulp_ctxt,
1300 struct bnxt_ulp_flow_db *flow_db;
1301 struct ulp_fdb_parent_child_db *p_pdb;
1302 uint32_t idx, free_idx = 0;
1304 /* validate the arguments */
1305 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1307 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1311 p_pdb = &flow_db->parent_child_db;
1312 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1313 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1314 BNXT_TF_DBG(ERR, "fid is already allocated\n");
1317 if (!p_pdb->parent_flow_tbl[idx].parent_fid && !free_idx)
1322 BNXT_TF_DBG(ERR, "parent child db is full\n");
1327 /* set the Fid in the parent child */
1328 p_pdb->parent_flow_tbl[free_idx].parent_fid = fid;
1333 * Free the entry in the parent-child database
1335 * ulp_ctxt [in] Ptr to ulp_context
1336 * fid [in] The flow id to the flow entry
1338 * returns 0 on success and negative on failure.
1341 ulp_flow_db_parent_flow_free(struct bnxt_ulp_context *ulp_ctxt,
1344 struct bnxt_ulp_flow_db *flow_db;
1345 struct ulp_fdb_parent_child_db *p_pdb;
1348 /* validate the arguments */
1349 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1351 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1355 p_pdb = &flow_db->parent_child_db;
1356 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1357 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1358 /* free the contents */
1359 p_pdb->parent_flow_tbl[idx].parent_fid = 0;
1360 memset(p_pdb->parent_flow_tbl[idx].child_fid_bitset,
1361 0, p_pdb->child_bitset_size);
1365 BNXT_TF_DBG(ERR, "parent entry not found = %x\n", fid);
1370 * Set or reset the child flow in the parent-child database
1372 * ulp_ctxt [in] Ptr to ulp_context
1373 * parent_fid [in] The flow id of the parent flow entry
1374 * child_fid [in] The flow id of the child flow entry
1375 * set_flag [in] Use 1 for setting child, 0 to reset
1377 * returns zero on success and negative on failure.
1380 ulp_flow_db_parent_child_flow_set(struct bnxt_ulp_context *ulp_ctxt,
1381 uint32_t parent_fid,
1385 struct bnxt_ulp_flow_db *flow_db;
1386 struct ulp_fdb_parent_child_db *p_pdb;
1387 uint32_t idx, a_idx;
1390 /* validate the arguments */
1391 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1393 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1397 /* check for fid validity */
1398 if (child_fid >= flow_db->flow_tbl.num_flows || !child_fid) {
1399 BNXT_TF_DBG(ERR, "Invalid child flow index %x\n", child_fid);
1403 p_pdb = &flow_db->parent_child_db;
1404 a_idx = child_fid / ULP_INDEX_BITMAP_SIZE;
1405 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1406 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1407 t = p_pdb->parent_flow_tbl[idx].child_fid_bitset;
1409 ULP_INDEX_BITMAP_SET(t[a_idx], child_fid);
1411 ULP_INDEX_BITMAP_RESET(t[a_idx], child_fid);
1415 BNXT_TF_DBG(ERR, "Unable to set the parent-child flow %x:%x\n",
1416 parent_fid, child_fid);
1421 * Get the parent index from the parent-child database
1423 * ulp_ctxt [in] Ptr to ulp_context
1424 * parent_fid [in] The flow id of the parent flow entry
1425 * parent_idx [out] The parent index of parent flow entry
1427 * returns zero on success and negative on failure.
1430 ulp_flow_db_parent_flow_idx_get(struct bnxt_ulp_context *ulp_ctxt,
1431 uint32_t parent_fid,
1432 uint32_t *parent_idx)
1434 struct bnxt_ulp_flow_db *flow_db;
1435 struct ulp_fdb_parent_child_db *p_pdb;
1438 /* validate the arguments */
1439 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1441 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1445 p_pdb = &flow_db->parent_child_db;
1446 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1447 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1452 BNXT_TF_DBG(ERR, "Unable to get the parent flow %x\n", parent_fid);
1457 * Get the next child flow in the parent-child database
1459 * ulp_ctxt [in] Ptr to ulp_context
1460 * parent_fid [in] The flow id of the parent flow entry
1461 * child_fid [in/out] The flow id of the child flow entry
1463 * returns zero on success and negative on failure.
1464 * Pass child_fid as zero for first entry.
1467 ulp_flow_db_parent_child_flow_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
1468 uint32_t parent_idx,
1469 uint32_t *child_fid)
1471 struct ulp_fdb_parent_child_db *p_pdb;
1472 uint32_t idx, s_idx, mod_fid;
1473 uint32_t next_fid = *child_fid;
1474 uint64_t *child_bitset;
1477 /* check for fid validity */
1478 p_pdb = &flow_db->parent_child_db;
1479 if (parent_idx >= p_pdb->entries_count ||
1480 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1481 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1485 child_bitset = p_pdb->parent_flow_tbl[parent_idx].child_fid_bitset;
1487 /* increment the flow id to find the next valid flow id */
1489 if (next_fid >= flow_db->flow_tbl.num_flows)
1491 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1492 mod_fid = next_fid % ULP_INDEX_BITMAP_SIZE;
1494 while (!(bs = child_bitset[idx])) {
1496 if ((idx * ULP_INDEX_BITMAP_SIZE) >=
1497 flow_db->flow_tbl.num_flows)
1501 * remove the previous bits in the bitset bs to find the
1502 * next non zero bit in the bitset. This needs to be done
1503 * only if the idx is same as he one you started.
1506 bs &= (-1UL >> mod_fid);
1507 next_fid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1508 if (*child_fid >= next_fid) {
1509 BNXT_TF_DBG(ERR, "Parent Child Database is corrupt\n");
1512 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1513 } while (!ULP_INDEX_BITMAP_GET(child_bitset[idx], next_fid));
1514 *child_fid = next_fid;
1519 * Set the counter accumulation in the parent flow
1521 * ulp_ctxt [in] Ptr to ulp_context
1522 * parent_idx [in] The parent index of the parent flow entry
1524 * returns index on success and negative on failure.
1527 ulp_flow_db_parent_flow_count_accum_set(struct bnxt_ulp_context *ulp_ctxt,
1528 uint32_t parent_idx)
1530 struct bnxt_ulp_flow_db *flow_db;
1531 struct ulp_fdb_parent_child_db *p_pdb;
1533 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1535 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1539 /* check for parent idx validity */
1540 p_pdb = &flow_db->parent_child_db;
1541 if (parent_idx >= p_pdb->entries_count ||
1542 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1543 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1547 p_pdb->parent_flow_tbl[parent_idx].counter_acc = 1;
1552 * Get the counter accumulation in the parent flow
1554 * ulp_ctxt [in] Ptr to ulp_context
1555 * parent_fid [in] The flow id of the parent flow entry
1557 * returns 0 if counter accum is set else -1.
1560 ulp_flow_db_parent_flow_count_accum_get(struct bnxt_ulp_context *ulp_ctxt,
1561 uint32_t parent_fid)
1563 struct bnxt_ulp_flow_db *flow_db;
1564 struct ulp_fdb_parent_child_db *p_pdb;
1567 /* validate the arguments */
1568 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1570 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1574 p_pdb = &flow_db->parent_child_db;
1575 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1576 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1577 if (p_pdb->parent_flow_tbl[idx].counter_acc)
1586 * Orphan the child flow entry
1587 * This is called only for child flows that have
1588 * BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW resource
1590 * ulp_ctxt [in] Ptr to ulp_context
1591 * flow_type [in] Specify it is regular or default flow
1592 * fid [in] The index to the flow entry
1594 * Returns 0 on success and negative on failure.
1597 ulp_flow_db_child_flow_reset(struct bnxt_ulp_context *ulp_ctxt,
1598 enum bnxt_ulp_fdb_type flow_type,
1601 struct bnxt_ulp_flow_db *flow_db;
1602 struct bnxt_ulp_flow_tbl *flow_tbl;
1603 struct ulp_fdb_resource_info *fid_res;
1604 uint32_t res_id = 0;
1606 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1608 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1612 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
1613 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1617 flow_tbl = &flow_db->flow_tbl;
1618 /* check for max flows */
1619 if (fid >= flow_tbl->num_flows || !fid) {
1620 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
1624 /* check if the flow is active or not */
1625 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
1626 BNXT_TF_DBG(ERR, "flow does not exist\n");
1630 /* Iterate the resource to get the resource handle */
1633 fid_res = &flow_tbl->flow_resources[res_id];
1634 if (ulp_flow_db_resource_func_get(fid_res) ==
1635 BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW) {
1636 /* invalidate the resource details */
1637 fid_res->resource_hndl = 0;
1641 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1648 * Create parent flow in the parent flow tbl
1650 * parms [in] Ptr to mapper params
1652 * Returns 0 on success and negative on failure.
1655 ulp_flow_db_parent_flow_create(struct bnxt_ulp_mapper_parms *parms)
1657 struct ulp_flow_db_res_params fid_parms;
1658 uint32_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT_ACC;
1659 struct ulp_flow_db_res_params res_params;
1660 int32_t fid_idx, rc;
1662 /* create the child flow entry in parent flow table */
1663 fid_idx = ulp_flow_db_parent_flow_alloc(parms->ulp_ctx, parms->fid);
1665 BNXT_TF_DBG(ERR, "Error in creating parent flow fid %x\n",
1670 /* Add the parent details in the resource list of the flow */
1671 memset(&fid_parms, 0, sizeof(fid_parms));
1672 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW;
1673 fid_parms.resource_hndl = fid_idx;
1674 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1675 if (ulp_flow_db_resource_add(parms->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
1676 parms->fid, &fid_parms)) {
1677 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1682 /* check of the flow has internal counter accumulation enabled */
1683 if (!ulp_flow_db_resource_params_get(parms->ulp_ctx,
1684 BNXT_ULP_FDB_TYPE_REGULAR,
1686 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1689 /* Enable the counter accumulation in parent entry */
1690 if (ulp_flow_db_parent_flow_count_accum_set(parms->ulp_ctx,
1692 BNXT_TF_DBG(ERR, "Error in setting counter acc %x\n",
1698 rc = ulp_flow_db_parent_tun_idx_set(parms->ulp_ctx, fid_idx,
1701 BNXT_TF_DBG(ERR, "Error setting tun_idx in the parent flow\n");
1709 * Create child flow in the parent flow tbl
1711 * parms [in] Ptr to mapper params
1713 * Returns 0 on success and negative on failure.
1716 ulp_flow_db_child_flow_create(struct bnxt_ulp_mapper_parms *parms)
1718 struct ulp_flow_db_res_params fid_parms;
1719 uint32_t sub_type = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT;
1720 enum bnxt_ulp_resource_func res_fun;
1721 struct ulp_flow_db_res_params res_p;
1722 uint32_t parent_fid = parms->parent_fid;
1725 /* create the parent flow entry in parent flow table */
1726 rc = ulp_flow_db_parent_child_flow_set(parms->ulp_ctx,
1730 BNXT_TF_DBG(ERR, "Error in setting child fid %x\n", parms->fid);
1734 /* Add the parent details in the resource list of the flow */
1735 memset(&fid_parms, 0, sizeof(fid_parms));
1736 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW;
1737 fid_parms.resource_hndl = parms->parent_fid;
1738 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1739 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1740 BNXT_ULP_FDB_TYPE_REGULAR,
1741 parms->fid, &fid_parms);
1743 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1748 /* check if accumulation count is set for parent flow */
1749 rc = ulp_flow_db_parent_flow_count_accum_get(parms->ulp_ctx,
1752 /* check if internal count action included for this flow.*/
1753 res_fun = BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE;
1754 rc = ulp_flow_db_resource_params_get(parms->ulp_ctx,
1755 BNXT_ULP_FDB_TYPE_REGULAR,
1761 /* update the counter manager to include parent fid */
1762 if (ulp_fc_mgr_cntr_parent_flow_set(parms->ulp_ctx,
1764 res_p.resource_hndl,
1766 BNXT_TF_DBG(ERR, "Error in setting child %x\n",
1772 /* return success */
1777 * Update the parent counters
1779 * ulp_ctxt [in] Ptr to ulp_context
1780 * parent_fid [in] The flow id of the parent flow entry
1781 * packet_count [in] - packet count
1782 * byte_count [in] - byte count
1784 * returns 0 on success
1787 ulp_flow_db_parent_flow_count_update(struct bnxt_ulp_context *ulp_ctxt,
1788 uint32_t parent_fid,
1789 uint64_t packet_count,
1790 uint64_t byte_count)
1792 struct bnxt_ulp_flow_db *flow_db;
1793 struct ulp_fdb_parent_child_db *p_pdb;
1796 /* validate the arguments */
1797 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1799 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1803 p_pdb = &flow_db->parent_child_db;
1804 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1805 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1806 if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1807 p_pdb->parent_flow_tbl[idx].pkt_count +=
1809 p_pdb->parent_flow_tbl[idx].byte_count +=
1819 * Get the parent accumulation counters
1821 * ulp_ctxt [in] Ptr to ulp_context
1822 * parent_fid [in] The flow id of the parent flow entry
1823 * packet_count [out] - packet count
1824 * byte_count [out] - byte count
1826 * returns 0 on success
1829 ulp_flow_db_parent_flow_count_get(struct bnxt_ulp_context *ulp_ctxt,
1830 uint32_t parent_fid,
1831 uint64_t *packet_count,
1832 uint64_t *byte_count)
1834 struct bnxt_ulp_flow_db *flow_db;
1835 struct ulp_fdb_parent_child_db *p_pdb;
1838 /* validate the arguments */
1839 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1841 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1845 p_pdb = &flow_db->parent_child_db;
1846 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1847 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1848 if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1850 p_pdb->parent_flow_tbl[idx].pkt_count;
1852 p_pdb->parent_flow_tbl[idx].byte_count;
1861 * reset the parent accumulation counters
1863 * ulp_ctxt [in] Ptr to ulp_context
1868 ulp_flow_db_parent_flow_count_reset(struct bnxt_ulp_context *ulp_ctxt)
1870 struct bnxt_ulp_flow_db *flow_db;
1871 struct ulp_fdb_parent_child_db *p_pdb;
1874 /* validate the arguments */
1875 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, 1);
1877 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1881 p_pdb = &flow_db->parent_child_db;
1882 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1883 if (p_pdb->parent_flow_tbl[idx].parent_fid &&
1884 p_pdb->parent_flow_tbl[idx].counter_acc) {
1885 p_pdb->parent_flow_tbl[idx].pkt_count = 0;
1886 p_pdb->parent_flow_tbl[idx].byte_count = 0;