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;
90 reg = ULP_INDEX_BITMAP_GET(f_tbl->active_reg_flows[a_idx], idx);
91 dflt = ULP_INDEX_BITMAP_GET(f_tbl->active_dflt_flows[a_idx], idx);
94 case BNXT_ULP_FDB_TYPE_REGULAR:
95 return (reg && !dflt);
96 case BNXT_ULP_FDB_TYPE_DEFAULT:
97 return (!reg && dflt);
98 case BNXT_ULP_FDB_TYPE_RID:
105 static inline enum tf_dir
106 ulp_flow_db_resource_dir_get(struct ulp_fdb_resource_info *res_info)
108 return ((res_info->nxt_resource_idx & ULP_FLOW_DB_RES_DIR_MASK) >>
109 ULP_FLOW_DB_RES_DIR_BIT);
113 ulp_flow_db_resource_func_get(struct ulp_fdb_resource_info *res_info)
117 func = (((res_info->nxt_resource_idx & ULP_FLOW_DB_RES_FUNC_MASK) >>
118 ULP_FLOW_DB_RES_FUNC_BITS) << ULP_FLOW_DB_RES_FUNC_UPPER);
119 /* The resource func is split into upper and lower */
120 if (func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER)
121 return (func | res_info->resource_func_lower);
126 * Helper function to copy the resource params to resource info
127 * No validation being done in this function.
129 * resource_info [out] Ptr to resource information
130 * params [in] The input params from the caller
134 ulp_flow_db_res_params_to_info(struct ulp_fdb_resource_info *resource_info,
135 struct ulp_flow_db_res_params *params)
137 uint32_t resource_func;
139 resource_info->nxt_resource_idx |= ((params->direction <<
140 ULP_FLOW_DB_RES_DIR_BIT) &
141 ULP_FLOW_DB_RES_DIR_MASK);
142 resource_func = (params->resource_func >> ULP_FLOW_DB_RES_FUNC_UPPER);
143 resource_info->nxt_resource_idx |= ((resource_func <<
144 ULP_FLOW_DB_RES_FUNC_BITS) &
145 ULP_FLOW_DB_RES_FUNC_MASK);
147 if (params->resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
148 /* Break the resource func into two parts */
149 resource_func = (params->resource_func &
150 ULP_FLOW_DB_RES_FUNC_LOWER_MASK);
151 resource_info->resource_func_lower = resource_func;
154 /* Store the handle as 64bit only for EM table entries */
155 if (params->resource_func != BNXT_ULP_RESOURCE_FUNC_EM_TABLE) {
156 resource_info->resource_hndl = (uint32_t)params->resource_hndl;
157 resource_info->resource_type = params->resource_type;
158 resource_info->resource_sub_type = params->resource_sub_type;
159 resource_info->fdb_flags = params->fdb_flags;
161 resource_info->resource_em_handle = params->resource_hndl;
166 * Helper function to copy the resource params to resource info
167 * No validation being done in this function.
169 * resource_info [in] Ptr to resource information
170 * params [out] The output params to the caller
175 ulp_flow_db_res_info_to_params(struct ulp_fdb_resource_info *resource_info,
176 struct ulp_flow_db_res_params *params)
178 memset(params, 0, sizeof(struct ulp_flow_db_res_params));
180 /* use the helper function to get the resource func */
181 params->direction = ulp_flow_db_resource_dir_get(resource_info);
182 params->resource_func = ulp_flow_db_resource_func_get(resource_info);
184 if (params->resource_func == BNXT_ULP_RESOURCE_FUNC_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->fdb_flags = resource_info->fdb_flags;
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;
390 * Initialize the flow database. Memory is allocated in this
391 * call and assigned to the flow database.
393 * ulp_ctxt [in] Ptr to ulp context
395 * Returns 0 on success or negative number on failure.
398 ulp_flow_db_init(struct bnxt_ulp_context *ulp_ctxt)
400 struct bnxt_ulp_device_params *dparms;
401 struct bnxt_ulp_flow_tbl *flow_tbl;
402 struct bnxt_ulp_flow_db *flow_db;
403 uint32_t dev_id, num_flows;
404 enum bnxt_ulp_flow_mem_type mtype;
406 /* Get the dev specific number of flows that needed to be supported. */
407 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctxt, &dev_id)) {
408 BNXT_TF_DBG(ERR, "Invalid device id\n");
412 dparms = bnxt_ulp_device_params_get(dev_id);
414 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
418 flow_db = rte_zmalloc("bnxt_ulp_flow_db",
419 sizeof(struct bnxt_ulp_flow_db), 0);
422 "Failed to allocate memory for flow table ptr\n");
426 /* Attach the flow database to the ulp context. */
427 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, flow_db);
429 /* Determine the number of flows based on EM type */
430 if (bnxt_ulp_cntxt_mem_type_get(ulp_ctxt, &mtype))
433 if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT)
434 num_flows = dparms->int_flow_db_num_entries;
436 num_flows = dparms->ext_flow_db_num_entries;
438 /* Populate the regular flow table limits. */
439 flow_tbl = &flow_db->flow_tbl;
440 flow_tbl->num_flows = num_flows + 1;
441 flow_tbl->num_resources = ((num_flows + 1) *
442 dparms->num_resources_per_flow);
444 /* Include the default flow table limits. */
445 flow_tbl->num_flows += (BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1);
446 flow_tbl->num_resources += ((BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1) *
447 BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES);
449 /* Allocate the resource for the flow table. */
450 if (ulp_flow_db_alloc_resource(flow_db))
453 /* add 1 since we are not using index 0 for flow id */
454 flow_db->func_id_tbl_size = flow_tbl->num_flows + 1;
455 /* Allocate the function Id table */
456 flow_db->func_id_tbl = rte_zmalloc("bnxt_ulp_flow_db_func_id_table",
457 flow_db->func_id_tbl_size *
458 sizeof(uint16_t), 0);
459 if (!flow_db->func_id_tbl) {
461 "Failed to allocate mem for flow table func id\n");
464 /* initialize the parent child database */
465 if (ulp_flow_db_parent_tbl_init(flow_db,
466 dparms->fdb_parent_flow_entries)) {
468 "Failed to allocate mem for parent child db\n");
472 /* All good so return. */
473 BNXT_TF_DBG(INFO, "FlowDB initialized with %d flows.\n",
474 flow_tbl->num_flows);
477 ulp_flow_db_deinit(ulp_ctxt);
482 * Deinitialize the flow database. Memory is deallocated in
483 * this call and all flows should have been purged before this
486 * ulp_ctxt [in] Ptr to ulp context
488 * Returns 0 on success.
491 ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
493 struct bnxt_ulp_flow_db *flow_db;
495 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
499 /* Detach the flow database from the ulp context. */
500 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, NULL);
502 /* Free up all the memory. */
503 ulp_flow_db_parent_tbl_deinit(flow_db);
504 ulp_flow_db_dealloc_resource(flow_db);
505 rte_free(flow_db->func_id_tbl);
512 * Allocate the flow database entry
514 * ulp_ctxt [in] Ptr to ulp_context
515 * flow_type [in] - specify default or regular
516 * func_id [in].function id of the ingress port
517 * fid [out] The index to the flow entry
519 * returns 0 on success and negative on failure.
522 ulp_flow_db_fid_alloc(struct bnxt_ulp_context *ulp_ctxt,
523 enum bnxt_ulp_fdb_type flow_type,
527 struct bnxt_ulp_flow_db *flow_db;
528 struct bnxt_ulp_flow_tbl *flow_tbl;
530 *fid = 0; /* Initialize fid to invalid value */
531 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
533 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
537 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
538 BNXT_TF_DBG(ERR, "Invalid flow type\n");
542 flow_tbl = &flow_db->flow_tbl;
543 /* check for max flows */
544 if (flow_tbl->num_flows <= flow_tbl->head_index) {
545 BNXT_TF_DBG(ERR, "Flow database has reached max flows\n");
548 if (flow_tbl->tail_index <= (flow_tbl->head_index + 1)) {
549 BNXT_TF_DBG(ERR, "Flow database has reached max resources\n");
552 *fid = flow_tbl->flow_tbl_stack[flow_tbl->head_index];
553 flow_tbl->head_index++;
555 /* Set the flow type */
556 ulp_flow_db_active_flows_bit_set(flow_db, flow_type, *fid, 1);
558 /* function id update is only valid for regular flow table */
559 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
560 ulp_flow_db_func_id_set(flow_db, *fid, func_id);
562 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
563 BNXT_TF_DBG(ERR, "flow_id = %u:%u allocated\n", flow_type, *fid);
570 * Allocate the flow database entry.
571 * The params->critical_resource has to be set to 0 to allocate a new resource.
573 * ulp_ctxt [in] Ptr to ulp_context
574 * flow_type [in] Specify it is regular or default flow
575 * fid [in] The index to the flow entry
576 * params [in] The contents to be copied into resource
578 * returns 0 on success and negative on failure.
581 ulp_flow_db_resource_add(struct bnxt_ulp_context *ulp_ctxt,
582 enum bnxt_ulp_fdb_type flow_type,
584 struct ulp_flow_db_res_params *params)
586 struct bnxt_ulp_flow_db *flow_db;
587 struct bnxt_ulp_flow_tbl *flow_tbl;
588 struct ulp_fdb_resource_info *resource, *fid_resource;
589 struct bnxt_ulp_fc_info *ulp_fc_info;
592 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
594 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
598 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
599 BNXT_TF_DBG(ERR, "Invalid flow type\n");
603 flow_tbl = &flow_db->flow_tbl;
604 /* check for max flows */
605 if (fid >= flow_tbl->num_flows || !fid) {
606 BNXT_TF_DBG(ERR, "Invalid flow index\n");
610 /* check if the flow is active or not */
611 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
612 BNXT_TF_DBG(ERR, "flow does not exist %x:%x\n", flow_type, fid);
616 /* check for max resource */
617 if ((flow_tbl->head_index + 1) >= flow_tbl->tail_index) {
618 BNXT_TF_DBG(ERR, "Flow db has reached max resources\n");
621 fid_resource = &flow_tbl->flow_resources[fid];
623 if (params->critical_resource && fid_resource->resource_em_handle) {
624 BNXT_TF_DBG(DEBUG, "Ignore multiple critical resources\n");
625 /* Ignore the multiple critical resources */
626 params->critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
629 if (!params->critical_resource) {
630 /* Not the critical_resource so allocate a resource */
631 idx = flow_tbl->flow_tbl_stack[flow_tbl->tail_index];
632 resource = &flow_tbl->flow_resources[idx];
633 flow_tbl->tail_index--;
635 /* Update the chain list of resource*/
636 ULP_FLOW_DB_RES_NXT_SET(resource->nxt_resource_idx,
637 fid_resource->nxt_resource_idx);
638 /* update the contents */
639 ulp_flow_db_res_params_to_info(resource, params);
640 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
641 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
644 /* critical resource. Just update the fid resource */
645 ulp_flow_db_res_params_to_info(fid_resource, params);
648 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ulp_ctxt);
649 if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
650 params->resource_sub_type ==
651 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT &&
652 ulp_fc_info && ulp_fc_info->num_counters) {
653 /* Store the first HW counter ID for this table */
654 if (!ulp_fc_mgr_start_idx_isset(ulp_ctxt, params->direction))
655 ulp_fc_mgr_start_idx_set(ulp_ctxt, params->direction,
656 params->resource_hndl);
658 ulp_fc_mgr_cntr_set(ulp_ctxt, params->direction,
659 params->resource_hndl);
661 if (!ulp_fc_mgr_thread_isstarted(ulp_ctxt))
662 ulp_fc_mgr_thread_start(ulp_ctxt);
665 /* all good, return success */
670 * Free the flow database entry.
671 * The params->critical_resource has to be set to 1 to free the first resource.
673 * ulp_ctxt [in] Ptr to ulp_context
674 * flow_type [in] Specify it is regular or default flow
675 * fid [in] The index to the flow entry
676 * params [in/out] The contents to be copied into params.
677 * Only the critical_resource needs to be set by the caller.
679 * Returns 0 on success and negative on failure.
682 ulp_flow_db_resource_del(struct bnxt_ulp_context *ulp_ctxt,
683 enum bnxt_ulp_fdb_type flow_type,
685 struct ulp_flow_db_res_params *params)
687 struct bnxt_ulp_flow_db *flow_db;
688 struct bnxt_ulp_flow_tbl *flow_tbl;
689 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
690 uint32_t nxt_idx = 0;
692 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
694 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
698 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
699 BNXT_TF_DBG(ERR, "Invalid flow type\n");
703 flow_tbl = &flow_db->flow_tbl;
704 /* check for max flows */
705 if (fid >= flow_tbl->num_flows || !fid) {
706 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
710 /* check if the flow is active or not */
711 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
712 BNXT_TF_DBG(ERR, "flow does not exist %x:%x\n", flow_type, fid);
716 fid_resource = &flow_tbl->flow_resources[fid];
717 if (!params->critical_resource) {
718 /* Not the critical resource so free the resource */
719 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
720 fid_resource->nxt_resource_idx);
722 /* reached end of resources */
725 nxt_resource = &flow_tbl->flow_resources[nxt_idx];
727 /* connect the fid resource to the next resource */
728 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
729 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
730 nxt_resource->nxt_resource_idx);
732 /* update the contents to be given to caller */
733 ulp_flow_db_res_info_to_params(nxt_resource, params);
735 /* Delete the nxt_resource */
736 memset(nxt_resource, 0, sizeof(struct ulp_fdb_resource_info));
738 /* add it to the free list */
739 flow_tbl->tail_index++;
740 if (flow_tbl->tail_index >= flow_tbl->num_resources) {
741 BNXT_TF_DBG(ERR, "FlowDB:Tail reached max\n");
744 flow_tbl->flow_tbl_stack[flow_tbl->tail_index] = nxt_idx;
747 /* Critical resource. copy the contents and exit */
748 ulp_flow_db_res_info_to_params(fid_resource, params);
749 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
750 fid_resource->nxt_resource_idx);
751 memset(fid_resource, 0, sizeof(struct ulp_fdb_resource_info));
752 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
756 /* Now that the HW Flow counter resource is deleted, reset it's
757 * corresponding slot in the SW accumulation table in the Flow Counter
760 if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
761 params->resource_sub_type ==
762 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
763 ulp_fc_mgr_cntr_reset(ulp_ctxt, params->direction,
764 params->resource_hndl);
767 /* all good, return success */
772 * Free the flow database entry
774 * ulp_ctxt [in] Ptr to ulp_context
775 * flow_type [in] - specify default or regular
776 * fid [in] The index to the flow entry
778 * returns 0 on success and negative on failure.
781 ulp_flow_db_fid_free(struct bnxt_ulp_context *ulp_ctxt,
782 enum bnxt_ulp_fdb_type flow_type,
785 struct bnxt_ulp_flow_tbl *flow_tbl;
786 struct bnxt_ulp_flow_db *flow_db;
788 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
790 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
794 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
795 BNXT_TF_DBG(ERR, "Invalid flow type\n");
799 flow_tbl = &flow_db->flow_tbl;
801 /* check for limits of fid */
802 if (fid >= flow_tbl->num_flows || !fid) {
803 BNXT_TF_DBG(ERR, "Invalid flow index\n");
807 /* check if the flow is active or not */
808 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
809 BNXT_TF_DBG(ERR, "flow does not exist %x:%x\n", flow_type, fid);
812 flow_tbl->head_index--;
813 if (!flow_tbl->head_index) {
814 BNXT_TF_DBG(ERR, "FlowDB: Head Ptr is zero\n");
818 flow_tbl->flow_tbl_stack[flow_tbl->head_index] = fid;
820 /* Clear the flows bitmap */
821 ulp_flow_db_active_flows_bit_set(flow_db, flow_type, fid, 0);
823 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
824 ulp_flow_db_func_id_set(flow_db, fid, 0);
826 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
827 BNXT_TF_DBG(ERR, "flow_id = %u:%u freed\n", flow_type, fid);
829 /* all good, return success */
834 *Get the flow database entry details
836 * ulp_ctxt [in] Ptr to ulp_context
837 * flow_type [in] - specify default or regular
838 * fid [in] The index to the flow entry
839 * nxt_idx [in/out] the index to the next entry
840 * params [out] The contents to be copied into params.
842 * returns 0 on success and negative on failure.
845 ulp_flow_db_resource_get(struct bnxt_ulp_context *ulp_ctxt,
846 enum bnxt_ulp_fdb_type flow_type,
849 struct ulp_flow_db_res_params *params)
851 struct bnxt_ulp_flow_db *flow_db;
852 struct bnxt_ulp_flow_tbl *flow_tbl;
853 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
855 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
857 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
861 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
862 BNXT_TF_DBG(ERR, "Invalid flow type\n");
866 flow_tbl = &flow_db->flow_tbl;
868 /* check for limits of fid */
869 if (fid >= flow_tbl->num_flows || !fid) {
870 BNXT_TF_DBG(ERR, "Invalid flow index\n");
874 /* check if the flow is active or not */
875 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
876 BNXT_TF_DBG(ERR, "flow does not exist\n");
881 fid_resource = &flow_tbl->flow_resources[fid];
882 ulp_flow_db_res_info_to_params(fid_resource, params);
883 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
884 fid_resource->nxt_resource_idx);
886 nxt_resource = &flow_tbl->flow_resources[*nxt_idx];
887 ulp_flow_db_res_info_to_params(nxt_resource, params);
889 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
890 nxt_resource->nxt_resource_idx);
893 /* all good, return success */
898 * Get the flow database entry iteratively
900 * flow_tbl [in] Ptr to flow table
901 * flow_type [in] - specify default or regular
902 * fid [in/out] The index to the flow entry
904 * returns 0 on success and negative on failure.
907 ulp_flow_db_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
908 enum bnxt_ulp_fdb_type flow_type,
911 uint32_t lfid = *fid;
912 uint32_t idx, s_idx, mod_fid;
914 uint64_t *active_flows;
915 struct bnxt_ulp_flow_tbl *flowtbl = &flow_db->flow_tbl;
917 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR) {
918 active_flows = flowtbl->active_reg_flows;
919 } else if (flow_type == BNXT_ULP_FDB_TYPE_DEFAULT) {
920 active_flows = flowtbl->active_dflt_flows;
922 BNXT_TF_DBG(ERR, "Invalid flow type %x\n", flow_type);
927 /* increment the flow id to find the next valid flow id */
929 if (lfid >= flowtbl->num_flows)
931 idx = lfid / ULP_INDEX_BITMAP_SIZE;
932 mod_fid = lfid % ULP_INDEX_BITMAP_SIZE;
934 while (!(bs = active_flows[idx])) {
936 if ((idx * ULP_INDEX_BITMAP_SIZE) >= flowtbl->num_flows)
940 * remove the previous bits in the bitset bs to find the
941 * next non zero bit in the bitset. This needs to be done
942 * only if the idx is same as he one you started.
945 bs &= (-1UL >> mod_fid);
946 lfid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
948 BNXT_TF_DBG(ERR, "Flow Database is corrupt\n");
951 } while (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type,
954 /* all good, return success */
960 * Flush all flows in the flow database.
962 * ulp_ctxt [in] Ptr to ulp context
963 * flow_type [in] - specify default or regular
965 * returns 0 on success or negative number on failure
968 ulp_flow_db_flush_flows(struct bnxt_ulp_context *ulp_ctx,
969 enum bnxt_ulp_fdb_type flow_type)
972 struct bnxt_ulp_flow_db *flow_db;
975 BNXT_TF_DBG(ERR, "Invalid Argument\n");
979 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
981 BNXT_TF_DBG(ERR, "Flow database not found\n");
984 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
985 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
989 while (!ulp_flow_db_next_entry_get(flow_db, flow_type, &fid))
990 ulp_mapper_resources_free(ulp_ctx, flow_type, fid);
992 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
998 * Flush all flows in the flow database that belong to a device function.
1000 * ulp_ctxt [in] Ptr to ulp context
1001 * func_id [in] - The port function id
1003 * returns 0 on success or negative number on failure
1006 ulp_flow_db_function_flow_flush(struct bnxt_ulp_context *ulp_ctx,
1009 uint32_t flow_id = 0;
1010 struct bnxt_ulp_flow_db *flow_db;
1012 if (!ulp_ctx || !func_id) {
1013 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1017 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1019 BNXT_TF_DBG(ERR, "Flow database not found\n");
1022 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1023 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1027 while (!ulp_flow_db_next_entry_get(flow_db, BNXT_ULP_FDB_TYPE_REGULAR,
1029 if (flow_db->func_id_tbl[flow_id] == func_id)
1030 ulp_mapper_resources_free(ulp_ctx,
1031 BNXT_ULP_FDB_TYPE_REGULAR,
1034 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1039 * Flush all flows in the flow database that are associated with the session.
1041 * ulp_ctxt [in] Ptr to ulp context
1043 * returns 0 on success or negative number on failure
1046 ulp_flow_db_session_flow_flush(struct bnxt_ulp_context *ulp_ctx)
1049 * TBD: Tf core implementation of FW session flush shall change this
1052 return ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1056 * Check that flow id matches the function id or not
1058 * ulp_ctxt [in] Ptr to ulp context
1059 * flow_db [in] Ptr to flow table
1060 * func_id [in] The func_id to be set, for reset pass zero.
1062 * returns true on success or false on failure
1065 ulp_flow_db_validate_flow_func(struct bnxt_ulp_context *ulp_ctx,
1069 struct bnxt_ulp_flow_db *flow_db;
1071 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1073 BNXT_TF_DBG(ERR, "Flow database not found\n");
1077 /* set the function id in the function table */
1078 if (flow_id < flow_db->func_id_tbl_size && func_id &&
1079 flow_db->func_id_tbl[flow_id] == func_id)
1086 * Internal api to traverse the resource list within a flow
1087 * and match a resource based on resource func and resource
1088 * sub type. This api should be used only for resources that
1089 * are unique and do not have multiple instances of resource
1090 * func and sub type combination since it will return only
1094 ulp_flow_db_resource_params_get(struct bnxt_ulp_context *ulp_ctx,
1095 enum bnxt_ulp_fdb_type flow_type,
1097 uint32_t resource_func,
1098 uint32_t res_subtype,
1099 struct ulp_flow_db_res_params *params)
1101 struct bnxt_ulp_flow_db *flow_db;
1102 struct bnxt_ulp_flow_tbl *flow_tbl;
1103 struct ulp_fdb_resource_info *fid_res;
1106 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1108 BNXT_TF_DBG(ERR, "Flow database not found\n");
1113 BNXT_TF_DBG(ERR, "invalid argument\n");
1117 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
1118 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1122 flow_tbl = &flow_db->flow_tbl;
1124 /* check for limits of fid */
1125 if (flow_id >= flow_tbl->num_flows || !flow_id) {
1126 BNXT_TF_DBG(ERR, "Invalid flow index\n");
1130 /* check if the flow is active or not */
1131 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, flow_id)) {
1132 BNXT_TF_DBG(ERR, "flow does not exist\n");
1135 /* Iterate the resource to get the resource handle */
1137 memset(params, 0, sizeof(struct ulp_flow_db_res_params));
1139 fid_res = &flow_tbl->flow_resources[res_id];
1140 if (ulp_flow_db_resource_func_get(fid_res) == resource_func) {
1141 if (resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
1142 if (res_subtype == fid_res->resource_sub_type) {
1143 ulp_flow_db_res_info_to_params(fid_res,
1148 } else if (resource_func ==
1149 BNXT_ULP_RESOURCE_FUNC_EM_TABLE) {
1150 ulp_flow_db_res_info_to_params(fid_res,
1156 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1162 * Api to get the cfa action pointer from a flow.
1164 * ulp_ctxt [in] Ptr to ulp context
1165 * flow_id [in] flow id
1166 * cfa_action [out] The resource handle stored in the flow database
1168 * returns 0 on success
1171 ulp_default_flow_db_cfa_action_get(struct bnxt_ulp_context *ulp_ctx,
1173 uint16_t *cfa_action)
1175 uint8_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_VFR_CFA_ACTION;
1176 struct ulp_flow_db_res_params params;
1179 rc = ulp_flow_db_resource_params_get(ulp_ctx,
1180 BNXT_ULP_FDB_TYPE_DEFAULT,
1182 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1185 BNXT_TF_DBG(INFO, "CFA Action ptr not found for flow id %u\n",
1189 *cfa_action = params.resource_hndl;
1193 /* internal validation function for parent flow tbl */
1194 static struct ulp_fdb_parent_info *
1195 ulp_flow_db_pc_db_entry_get(struct bnxt_ulp_context *ulp_ctxt,
1198 struct bnxt_ulp_flow_db *flow_db;
1200 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1202 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1206 /* check for max flows */
1207 if (pc_idx >= BNXT_ULP_MAX_TUN_CACHE_ENTRIES) {
1208 BNXT_TF_DBG(ERR, "Invalid tunnel index\n");
1212 /* No support for parent child db then just exit */
1213 if (!flow_db->parent_child_db.entries_count) {
1214 BNXT_TF_DBG(ERR, "parent child db not supported\n");
1217 if (!flow_db->parent_child_db.parent_flow_tbl[pc_idx].valid) {
1218 BNXT_TF_DBG(ERR, "Not a valid tunnel index\n");
1222 return &flow_db->parent_child_db.parent_flow_tbl[pc_idx];
1225 /* internal validation function for parent flow tbl */
1226 static struct bnxt_ulp_flow_db *
1227 ulp_flow_db_parent_arg_validation(struct bnxt_ulp_context *ulp_ctxt,
1230 struct bnxt_ulp_flow_db *flow_db;
1232 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1234 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1238 /* check for max flows */
1239 if (tun_idx >= BNXT_ULP_MAX_TUN_CACHE_ENTRIES) {
1240 BNXT_TF_DBG(ERR, "Invalid tunnel index\n");
1244 /* No support for parent child db then just exit */
1245 if (!flow_db->parent_child_db.entries_count) {
1246 BNXT_TF_DBG(ERR, "parent child db not supported\n");
1254 * Allocate the entry in the parent-child database
1256 * ulp_ctxt [in] Ptr to ulp_context
1257 * tun_idx [in] The tunnel index of the flow entry
1259 * returns index on success and negative on failure.
1262 ulp_flow_db_pc_db_idx_alloc(struct bnxt_ulp_context *ulp_ctxt,
1265 struct bnxt_ulp_flow_db *flow_db;
1266 struct ulp_fdb_parent_child_db *p_pdb;
1267 uint32_t idx, free_idx = 0;
1269 /* validate the arguments */
1270 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, tun_idx);
1272 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1276 p_pdb = &flow_db->parent_child_db;
1277 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1278 if (p_pdb->parent_flow_tbl[idx].valid &&
1279 p_pdb->parent_flow_tbl[idx].tun_idx == tun_idx) {
1282 if (!p_pdb->parent_flow_tbl[idx].valid && !free_idx)
1287 BNXT_TF_DBG(ERR, "parent child db is full\n");
1292 /* set the Fid in the parent child */
1293 p_pdb->parent_flow_tbl[free_idx].tun_idx = tun_idx;
1294 p_pdb->parent_flow_tbl[free_idx].valid = 1;
1299 * Free the entry in the parent-child database
1301 * pc_entry [in] Ptr to parent child db entry
1306 ulp_flow_db_pc_db_entry_free(struct bnxt_ulp_context *ulp_ctxt,
1307 struct ulp_fdb_parent_info *pc_entry)
1309 struct bnxt_tun_cache_entry *tun_tbl;
1310 struct bnxt_ulp_flow_db *flow_db;
1311 uint64_t *tmp_bitset;
1313 /* free the tunnel entry */
1314 tun_tbl = bnxt_ulp_cntxt_ptr2_tun_tbl_get(ulp_ctxt);
1316 ulp_tunnel_offload_entry_clear(tun_tbl, pc_entry->tun_idx);
1318 /* free the child bitset*/
1319 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1321 memset(pc_entry->child_fid_bitset, 0,
1322 flow_db->parent_child_db.child_bitset_size);
1324 /* free the contents */
1325 tmp_bitset = pc_entry->child_fid_bitset;
1326 memset(pc_entry, 0, sizeof(struct ulp_fdb_parent_info));
1327 pc_entry->child_fid_bitset = tmp_bitset;
1331 * Set or reset the parent flow in the parent-child database
1333 * ulp_ctxt [in] Ptr to ulp_context
1334 * pc_idx [in] The index to parent child db
1335 * parent_fid [in] The flow id of the parent flow entry
1336 * set_flag [in] Use 1 for setting child, 0 to reset
1338 * returns zero on success and negative on failure.
1341 ulp_flow_db_pc_db_parent_flow_set(struct bnxt_ulp_context *ulp_ctxt,
1343 uint32_t parent_fid,
1346 struct ulp_fdb_parent_info *pc_entry;
1347 struct bnxt_ulp_flow_db *flow_db;
1349 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1351 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1355 /* check for fid validity */
1356 if (parent_fid >= flow_db->flow_tbl.num_flows || !parent_fid) {
1357 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_fid);
1361 /* validate the arguments and parent child entry */
1362 pc_entry = ulp_flow_db_pc_db_entry_get(ulp_ctxt, pc_idx);
1364 BNXT_TF_DBG(ERR, "failed to get the parent child entry\n");
1369 pc_entry->parent_fid = parent_fid;
1371 if (pc_entry->parent_fid != parent_fid)
1372 BNXT_TF_DBG(ERR, "Panic: invalid parent id\n");
1373 pc_entry->parent_fid = 0;
1375 /* Free the parent child db entry if no user present */
1376 if (!pc_entry->f2_cnt)
1377 ulp_flow_db_pc_db_entry_free(ulp_ctxt, pc_entry);
1383 * Set or reset the child flow in the parent-child database
1385 * ulp_ctxt [in] Ptr to ulp_context
1386 * pc_idx [in] The index to parent child db
1387 * child_fid [in] The flow id of the child flow entry
1388 * set_flag [in] Use 1 for setting child, 0 to reset
1390 * returns zero on success and negative on failure.
1393 ulp_flow_db_pc_db_child_flow_set(struct bnxt_ulp_context *ulp_ctxt,
1398 struct ulp_fdb_parent_info *pc_entry;
1399 struct bnxt_ulp_flow_db *flow_db;
1403 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1405 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1409 /* check for fid validity */
1410 if (child_fid >= flow_db->flow_tbl.num_flows || !child_fid) {
1411 BNXT_TF_DBG(ERR, "Invalid child flow index %x\n", child_fid);
1415 /* validate the arguments and parent child entry */
1416 pc_entry = ulp_flow_db_pc_db_entry_get(ulp_ctxt, pc_idx);
1418 BNXT_TF_DBG(ERR, "failed to get the parent child entry\n");
1422 a_idx = child_fid / ULP_INDEX_BITMAP_SIZE;
1423 t = pc_entry->child_fid_bitset;
1425 ULP_INDEX_BITMAP_SET(t[a_idx], child_fid);
1428 ULP_INDEX_BITMAP_RESET(t[a_idx], child_fid);
1429 if (pc_entry->f2_cnt)
1431 if (!pc_entry->f2_cnt && !pc_entry->parent_fid)
1432 ulp_flow_db_pc_db_entry_free(ulp_ctxt, pc_entry);
1438 * Get the next child flow in the parent-child database
1440 * ulp_ctxt [in] Ptr to ulp_context
1441 * parent_fid [in] The flow id of the parent flow entry
1442 * child_fid [in/out] The flow id of the child flow entry
1444 * returns zero on success and negative on failure.
1445 * Pass child_fid as zero for first entry.
1448 ulp_flow_db_parent_child_flow_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
1449 uint32_t parent_idx,
1450 uint32_t *child_fid)
1452 struct ulp_fdb_parent_child_db *p_pdb;
1453 uint32_t idx, s_idx, mod_fid;
1454 uint32_t next_fid = *child_fid;
1455 uint64_t *child_bitset;
1458 /* check for fid validity */
1459 p_pdb = &flow_db->parent_child_db;
1460 if (parent_idx >= p_pdb->entries_count ||
1461 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1462 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1466 child_bitset = p_pdb->parent_flow_tbl[parent_idx].child_fid_bitset;
1468 /* increment the flow id to find the next valid flow id */
1470 if (next_fid >= flow_db->flow_tbl.num_flows)
1472 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1473 mod_fid = next_fid % ULP_INDEX_BITMAP_SIZE;
1475 while (!(bs = child_bitset[idx])) {
1477 if ((idx * ULP_INDEX_BITMAP_SIZE) >=
1478 flow_db->flow_tbl.num_flows)
1482 * remove the previous bits in the bitset bs to find the
1483 * next non zero bit in the bitset. This needs to be done
1484 * only if the idx is same as he one you started.
1487 bs &= (-1UL >> mod_fid);
1488 next_fid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1489 if (*child_fid >= next_fid) {
1490 BNXT_TF_DBG(ERR, "Parent Child Database is corrupt\n");
1493 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1494 } while (!ULP_INDEX_BITMAP_GET(child_bitset[idx], next_fid));
1495 *child_fid = next_fid;
1500 * Set the counter accumulation in the parent flow
1502 * ulp_ctxt [in] Ptr to ulp_context
1503 * pc_idx [in] The parent child index of the parent flow entry
1505 * returns index on success and negative on failure.
1508 ulp_flow_db_parent_flow_count_accum_set(struct bnxt_ulp_context *ulp_ctxt,
1511 struct bnxt_ulp_flow_db *flow_db;
1512 struct ulp_fdb_parent_child_db *p_pdb;
1514 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1516 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1520 /* check for parent idx validity */
1521 p_pdb = &flow_db->parent_child_db;
1522 if (pc_idx >= p_pdb->entries_count ||
1523 !p_pdb->parent_flow_tbl[pc_idx].parent_fid) {
1524 BNXT_TF_DBG(ERR, "Invalid parent child index %x\n", pc_idx);
1528 p_pdb->parent_flow_tbl[pc_idx].counter_acc = 1;
1533 * Orphan the child flow entry
1534 * This is called only for child flows that have
1535 * BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW resource
1537 * ulp_ctxt [in] Ptr to ulp_context
1538 * flow_type [in] Specify it is regular or default flow
1539 * fid [in] The index to the flow entry
1541 * Returns 0 on success and negative on failure.
1544 ulp_flow_db_child_flow_reset(struct bnxt_ulp_context *ulp_ctxt,
1545 enum bnxt_ulp_fdb_type flow_type,
1548 struct bnxt_ulp_flow_db *flow_db;
1549 struct bnxt_ulp_flow_tbl *flow_tbl;
1550 struct ulp_fdb_resource_info *fid_res;
1551 uint32_t res_id = 0;
1553 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1555 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1559 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
1560 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1564 flow_tbl = &flow_db->flow_tbl;
1565 /* check for max flows */
1566 if (fid >= flow_tbl->num_flows || !fid) {
1567 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
1571 /* check if the flow is active or not */
1572 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
1573 BNXT_TF_DBG(ERR, "flow does not exist\n");
1577 /* Iterate the resource to get the resource handle */
1580 fid_res = &flow_tbl->flow_resources[res_id];
1581 if (ulp_flow_db_resource_func_get(fid_res) ==
1582 BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW) {
1583 /* invalidate the resource details */
1584 fid_res->resource_hndl = 0;
1588 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1595 * Create parent flow in the parent flow tbl
1597 * parms [in] Ptr to mapper params
1599 * Returns 0 on success and negative on failure.
1602 ulp_flow_db_parent_flow_create(struct bnxt_ulp_mapper_parms *parms)
1604 struct ulp_flow_db_res_params fid_parms;
1605 uint32_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT;
1606 struct ulp_flow_db_res_params res_params;
1609 /* create or get the parent child database */
1610 pc_idx = ulp_flow_db_pc_db_idx_alloc(parms->ulp_ctx, parms->tun_idx);
1612 BNXT_TF_DBG(ERR, "Error in getting parent child db %x\n",
1617 /* Update the parent fid */
1618 if (ulp_flow_db_pc_db_parent_flow_set(parms->ulp_ctx, pc_idx,
1620 BNXT_TF_DBG(ERR, "Error in setting parent fid %x\n",
1625 /* Add the parent details in the resource list of the flow */
1626 memset(&fid_parms, 0, sizeof(fid_parms));
1627 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW;
1628 fid_parms.resource_hndl = pc_idx;
1629 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1630 if (ulp_flow_db_resource_add(parms->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
1631 parms->fid, &fid_parms)) {
1632 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1637 /* check of the flow has internal counter accumulation enabled */
1638 if (!ulp_flow_db_resource_params_get(parms->ulp_ctx,
1639 BNXT_ULP_FDB_TYPE_REGULAR,
1641 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1644 /* Enable the counter accumulation in parent entry */
1645 if (ulp_flow_db_parent_flow_count_accum_set(parms->ulp_ctx,
1647 BNXT_TF_DBG(ERR, "Error in setting counter acc %x\n",
1657 * Create child flow in the parent flow tbl
1659 * parms [in] Ptr to mapper params
1661 * Returns 0 on success and negative on failure.
1664 ulp_flow_db_child_flow_create(struct bnxt_ulp_mapper_parms *parms)
1666 struct ulp_flow_db_res_params fid_parms;
1667 uint32_t sub_type = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT;
1668 enum bnxt_ulp_resource_func res_fun;
1669 struct ulp_flow_db_res_params res_p;
1672 /* create or get the parent child database */
1673 pc_idx = ulp_flow_db_pc_db_idx_alloc(parms->ulp_ctx, parms->tun_idx);
1675 BNXT_TF_DBG(ERR, "Error in getting parent child db %x\n",
1680 /* create the parent flow entry in parent flow table */
1681 rc = ulp_flow_db_pc_db_child_flow_set(parms->ulp_ctx, pc_idx,
1684 BNXT_TF_DBG(ERR, "Error in setting child fid %x\n", parms->fid);
1688 /* Add the parent details in the resource list of the flow */
1689 memset(&fid_parms, 0, sizeof(fid_parms));
1690 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW;
1691 fid_parms.resource_hndl = pc_idx;
1692 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1693 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1694 BNXT_ULP_FDB_TYPE_REGULAR,
1695 parms->fid, &fid_parms);
1697 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1702 /* check if internal count action included for this flow.*/
1703 res_fun = BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE;
1704 rc = ulp_flow_db_resource_params_get(parms->ulp_ctx,
1705 BNXT_ULP_FDB_TYPE_REGULAR,
1711 /* update the counter manager to include parent fid */
1712 if (ulp_fc_mgr_cntr_parent_flow_set(parms->ulp_ctx,
1714 res_p.resource_hndl,
1716 BNXT_TF_DBG(ERR, "Error in setting child %x\n",
1722 /* return success */
1727 * Update the parent counters
1729 * ulp_ctxt [in] Ptr to ulp_context
1730 * pc_idx [in] The parent flow entry idx
1731 * packet_count [in] - packet count
1732 * byte_count [in] - byte count
1734 * returns 0 on success
1737 ulp_flow_db_parent_flow_count_update(struct bnxt_ulp_context *ulp_ctxt,
1739 uint64_t packet_count,
1740 uint64_t byte_count)
1742 struct ulp_fdb_parent_info *pc_entry;
1744 /* validate the arguments and get parent child entry */
1745 pc_entry = ulp_flow_db_pc_db_entry_get(ulp_ctxt, pc_idx);
1747 BNXT_TF_DBG(ERR, "failed to get the parent child entry\n");
1751 if (pc_entry->counter_acc) {
1752 pc_entry->pkt_count += packet_count;
1753 pc_entry->byte_count += byte_count;
1759 * Get the parent accumulation counters
1761 * ulp_ctxt [in] Ptr to ulp_context
1762 * pc_idx [in] The parent flow entry idx
1763 * packet_count [out] - packet count
1764 * byte_count [out] - byte count
1766 * returns 0 on success
1769 ulp_flow_db_parent_flow_count_get(struct bnxt_ulp_context *ulp_ctxt,
1770 uint32_t pc_idx, uint64_t *packet_count,
1771 uint64_t *byte_count, uint8_t count_reset)
1773 struct ulp_fdb_parent_info *pc_entry;
1775 /* validate the arguments and get parent child entry */
1776 pc_entry = ulp_flow_db_pc_db_entry_get(ulp_ctxt, pc_idx);
1778 BNXT_TF_DBG(ERR, "failed to get the parent child entry\n");
1782 if (pc_entry->counter_acc) {
1783 *packet_count = pc_entry->pkt_count;
1784 *byte_count = pc_entry->byte_count;
1786 pc_entry->pkt_count = 0;
1787 pc_entry->byte_count = 0;
1794 * reset the parent accumulation counters
1796 * ulp_ctxt [in] Ptr to ulp_context
1801 ulp_flow_db_parent_flow_count_reset(struct bnxt_ulp_context *ulp_ctxt)
1803 struct bnxt_ulp_flow_db *flow_db;
1804 struct ulp_fdb_parent_child_db *p_pdb;
1807 /* validate the arguments */
1808 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1810 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1814 p_pdb = &flow_db->parent_child_db;
1815 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1816 if (p_pdb->parent_flow_tbl[idx].valid &&
1817 p_pdb->parent_flow_tbl[idx].counter_acc) {
1818 p_pdb->parent_flow_tbl[idx].pkt_count = 0;
1819 p_pdb->parent_flow_tbl[idx].byte_count = 0;
1825 * Set the shared bit for the flow db entry
1827 * res [in] Ptr to fdb entry
1828 * shared [in] shared flag
1832 void ulp_flow_db_shared_session_set(struct ulp_flow_db_res_params *res,
1833 enum bnxt_ulp_shared_session shared)
1835 if (res && (shared & BNXT_ULP_SHARED_SESSION_YES))
1836 res->fdb_flags |= ULP_FDB_FLAG_SHARED_SESSION;
1839 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1841 * Dump the entry details
1843 * ulp_ctxt [in] Ptr to ulp_context
1847 static void ulp_flow_db_res_dump(struct ulp_fdb_resource_info *r,
1850 uint8_t res_func = ulp_flow_db_resource_func_get(r);
1852 BNXT_TF_DBG(DEBUG, "Resource func = %x, nxt_resource_idx = %x\n",
1853 res_func, (ULP_FLOW_DB_RES_NXT_MASK & r->nxt_resource_idx));
1854 if (res_func == BNXT_ULP_RESOURCE_FUNC_EM_TABLE)
1855 BNXT_TF_DBG(DEBUG, "EM Handle = 0x%016" PRIX64 "\n",
1856 r->resource_em_handle);
1858 BNXT_TF_DBG(DEBUG, "Handle = 0x%08x\n", r->resource_hndl);
1861 ULP_FLOW_DB_RES_NXT_SET(*nxt_res,
1862 r->nxt_resource_idx);
1866 * Dump the flow entry details
1868 * flow_db [in] Ptr to flow db
1874 ulp_flow_db_debug_fid_dump(struct bnxt_ulp_flow_db *flow_db, uint32_t fid)
1876 struct ulp_fdb_resource_info *r;
1877 struct bnxt_ulp_flow_tbl *flow_tbl;
1878 uint32_t nxt_res = 0;
1879 uint32_t def_flag = 0, reg_flag = 0;
1881 flow_tbl = &flow_db->flow_tbl;
1882 if (ulp_flow_db_active_flows_bit_is_set(flow_db,
1883 BNXT_ULP_FDB_TYPE_REGULAR, fid))
1885 if (ulp_flow_db_active_flows_bit_is_set(flow_db,
1886 BNXT_ULP_FDB_TYPE_DEFAULT, fid))
1889 if (reg_flag && def_flag)
1890 BNXT_TF_DBG(DEBUG, "RID = %u\n", fid);
1892 BNXT_TF_DBG(DEBUG, "Regular fid = %u and func id = %u\n",
1893 fid, flow_db->func_id_tbl[fid]);
1895 BNXT_TF_DBG(DEBUG, "Default fid = %u\n", fid);
1898 /* iterate the resource */
1901 r = &flow_tbl->flow_resources[nxt_res];
1902 ulp_flow_db_res_dump(r, &nxt_res);
1907 * Dump the flow database entry details
1909 * ulp_ctxt [in] Ptr to ulp_context
1910 * flow_id [in] if zero then all fids are dumped.
1914 int32_t ulp_flow_db_debug_dump(struct bnxt_ulp_context *ulp_ctxt,
1917 struct bnxt_ulp_flow_db *flow_db;
1918 struct bnxt_ulp_flow_tbl *flow_tbl;
1921 if (!ulp_ctxt || !ulp_ctxt->cfg_data) {
1922 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1925 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1927 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1931 flow_tbl = &flow_db->flow_tbl;
1933 ulp_flow_db_debug_fid_dump(flow_db, flow_id);
1937 BNXT_TF_DBG(DEBUG, "Dump flows = %u:%u\n",
1938 flow_tbl->num_flows,
1939 flow_tbl->num_resources);
1940 BNXT_TF_DBG(DEBUG, "Head_index = %u, Tail_index = %u\n",
1941 flow_tbl->head_index, flow_tbl->tail_index);
1942 for (fid = 1; fid < flow_tbl->num_flows; fid++)
1943 ulp_flow_db_debug_fid_dump(flow_db, fid);
1944 BNXT_TF_DBG(DEBUG, "Done.\n");