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;
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 if (bnxt_ulp_cntxt_mem_type_get(ulp_ctxt, &mtype))
528 if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT)
529 num_flows = dparms->int_flow_db_num_entries;
531 num_flows = dparms->ext_flow_db_num_entries;
533 /* Populate the regular flow table limits. */
534 flow_tbl = &flow_db->flow_tbl;
535 flow_tbl->num_flows = num_flows + 1;
536 flow_tbl->num_resources = ((num_flows + 1) *
537 dparms->num_resources_per_flow);
539 /* Include the default flow table limits. */
540 flow_tbl->num_flows += (BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1);
541 flow_tbl->num_resources += ((BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1) *
542 BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES);
544 /* Allocate the resource for the flow table. */
545 if (ulp_flow_db_alloc_resource(flow_db))
548 /* add 1 since we are not using index 0 for flow id */
549 flow_db->func_id_tbl_size = flow_tbl->num_flows + 1;
550 /* Allocate the function Id table */
551 flow_db->func_id_tbl = rte_zmalloc("bnxt_ulp_flow_db_func_id_table",
552 flow_db->func_id_tbl_size *
553 sizeof(uint16_t), 0);
554 if (!flow_db->func_id_tbl) {
556 "Failed to allocate mem for flow table func id\n");
559 /* initialize the parent child database */
560 if (ulp_flow_db_parent_tbl_init(flow_db,
561 dparms->fdb_parent_flow_entries)) {
563 "Failed to allocate mem for parent child db\n");
567 /* All good so return. */
568 BNXT_TF_DBG(INFO, "FlowDB initialized with %d flows.\n",
569 flow_tbl->num_flows);
572 ulp_flow_db_deinit(ulp_ctxt);
577 * Deinitialize the flow database. Memory is deallocated in
578 * this call and all flows should have been purged before this
581 * ulp_ctxt [in] Ptr to ulp context
583 * Returns 0 on success.
586 ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
588 struct bnxt_ulp_flow_db *flow_db;
590 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
594 /* Detach the flow database from the ulp context. */
595 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, NULL);
597 /* Free up all the memory. */
598 ulp_flow_db_parent_tbl_deinit(flow_db);
599 ulp_flow_db_dealloc_resource(flow_db);
600 rte_free(flow_db->func_id_tbl);
607 * Allocate the flow database entry
609 * ulp_ctxt [in] Ptr to ulp_context
610 * flow_type [in] - specify default or regular
611 * func_id [in].function id of the ingress port
612 * fid [out] The index to the flow entry
614 * returns 0 on success and negative on failure.
617 ulp_flow_db_fid_alloc(struct bnxt_ulp_context *ulp_ctxt,
618 enum bnxt_ulp_fdb_type flow_type,
622 struct bnxt_ulp_flow_db *flow_db;
623 struct bnxt_ulp_flow_tbl *flow_tbl;
625 *fid = 0; /* Initialize fid to invalid value */
626 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
628 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
632 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
633 BNXT_TF_DBG(ERR, "Invalid flow type\n");
637 flow_tbl = &flow_db->flow_tbl;
638 /* check for max flows */
639 if (flow_tbl->num_flows <= flow_tbl->head_index) {
640 BNXT_TF_DBG(ERR, "Flow database has reached max flows\n");
643 if (flow_tbl->tail_index <= (flow_tbl->head_index + 1)) {
644 BNXT_TF_DBG(ERR, "Flow database has reached max resources\n");
647 *fid = flow_tbl->flow_tbl_stack[flow_tbl->head_index];
648 flow_tbl->head_index++;
650 /* Set the flow type */
651 ulp_flow_db_active_flows_bit_set(flow_db, flow_type, *fid, 1);
653 /* function id update is only valid for regular flow table */
654 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
655 ulp_flow_db_func_id_set(flow_db, *fid, func_id);
662 * Allocate the flow database entry.
663 * The params->critical_resource has to be set to 0 to allocate a new resource.
665 * ulp_ctxt [in] Ptr to ulp_context
666 * flow_type [in] Specify it is regular or default flow
667 * fid [in] The index to the flow entry
668 * params [in] The contents to be copied into resource
670 * returns 0 on success and negative on failure.
673 ulp_flow_db_resource_add(struct bnxt_ulp_context *ulp_ctxt,
674 enum bnxt_ulp_fdb_type flow_type,
676 struct ulp_flow_db_res_params *params)
678 struct bnxt_ulp_flow_db *flow_db;
679 struct bnxt_ulp_flow_tbl *flow_tbl;
680 struct ulp_fdb_resource_info *resource, *fid_resource;
681 struct bnxt_ulp_fc_info *ulp_fc_info;
684 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
686 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
690 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
691 BNXT_TF_DBG(ERR, "Invalid flow type\n");
695 flow_tbl = &flow_db->flow_tbl;
696 /* check for max flows */
697 if (fid >= flow_tbl->num_flows || !fid) {
698 BNXT_TF_DBG(ERR, "Invalid flow index\n");
702 /* check if the flow is active or not */
703 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
704 BNXT_TF_DBG(ERR, "flow does not exist %x:%x\n", flow_type, fid);
708 /* check for max resource */
709 if ((flow_tbl->head_index + 1) >= flow_tbl->tail_index) {
710 BNXT_TF_DBG(ERR, "Flow db has reached max resources\n");
713 fid_resource = &flow_tbl->flow_resources[fid];
715 if (params->critical_resource && fid_resource->resource_em_handle) {
716 BNXT_TF_DBG(DEBUG, "Ignore multiple critical resources\n");
717 /* Ignore the multiple critical resources */
718 params->critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
721 if (!params->critical_resource) {
722 /* Not the critical_resource so allocate a resource */
723 idx = flow_tbl->flow_tbl_stack[flow_tbl->tail_index];
724 resource = &flow_tbl->flow_resources[idx];
725 flow_tbl->tail_index--;
727 /* Update the chain list of resource*/
728 ULP_FLOW_DB_RES_NXT_SET(resource->nxt_resource_idx,
729 fid_resource->nxt_resource_idx);
730 /* update the contents */
731 ulp_flow_db_res_params_to_info(resource, params);
732 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
733 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
736 /* critical resource. Just update the fid resource */
737 ulp_flow_db_res_params_to_info(fid_resource, params);
740 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ulp_ctxt);
741 if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
742 params->resource_sub_type ==
743 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT &&
744 ulp_fc_info && ulp_fc_info->num_counters) {
745 /* Store the first HW counter ID for this table */
746 if (!ulp_fc_mgr_start_idx_isset(ulp_ctxt, params->direction))
747 ulp_fc_mgr_start_idx_set(ulp_ctxt, params->direction,
748 params->resource_hndl);
750 ulp_fc_mgr_cntr_set(ulp_ctxt, params->direction,
751 params->resource_hndl);
753 if (!ulp_fc_mgr_thread_isstarted(ulp_ctxt))
754 ulp_fc_mgr_thread_start(ulp_ctxt);
757 /* all good, return success */
762 * Free the flow database entry.
763 * The params->critical_resource has to be set to 1 to free the first resource.
765 * ulp_ctxt [in] Ptr to ulp_context
766 * flow_type [in] Specify it is regular or default flow
767 * fid [in] The index to the flow entry
768 * params [in/out] The contents to be copied into params.
769 * Only the critical_resource needs to be set by the caller.
771 * Returns 0 on success and negative on failure.
774 ulp_flow_db_resource_del(struct bnxt_ulp_context *ulp_ctxt,
775 enum bnxt_ulp_fdb_type flow_type,
777 struct ulp_flow_db_res_params *params)
779 struct bnxt_ulp_flow_db *flow_db;
780 struct bnxt_ulp_flow_tbl *flow_tbl;
781 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
782 uint32_t nxt_idx = 0;
783 struct bnxt_tun_cache_entry *tun_tbl;
787 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
789 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
793 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
794 BNXT_TF_DBG(ERR, "Invalid flow type\n");
798 flow_tbl = &flow_db->flow_tbl;
799 /* check for max flows */
800 if (fid >= flow_tbl->num_flows || !fid) {
801 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
805 /* check if the flow is active or not */
806 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
807 BNXT_TF_DBG(ERR, "flow does not exist %x:%x\n", flow_type, fid);
811 fid_resource = &flow_tbl->flow_resources[fid];
812 if (!params->critical_resource) {
813 /* Not the critical resource so free the resource */
814 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
815 fid_resource->nxt_resource_idx);
817 /* reached end of resources */
820 nxt_resource = &flow_tbl->flow_resources[nxt_idx];
822 /* connect the fid resource to the next resource */
823 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
824 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
825 nxt_resource->nxt_resource_idx);
827 /* update the contents to be given to caller */
828 ulp_flow_db_res_info_to_params(nxt_resource, params);
830 /* Delete the nxt_resource */
831 memset(nxt_resource, 0, sizeof(struct ulp_fdb_resource_info));
833 /* add it to the free list */
834 flow_tbl->tail_index++;
835 if (flow_tbl->tail_index >= flow_tbl->num_resources) {
836 BNXT_TF_DBG(ERR, "FlowDB:Tail reached max\n");
839 flow_tbl->flow_tbl_stack[flow_tbl->tail_index] = nxt_idx;
842 /* Critical resource. copy the contents and exit */
843 ulp_flow_db_res_info_to_params(fid_resource, params);
844 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
845 fid_resource->nxt_resource_idx);
846 memset(fid_resource, 0, sizeof(struct ulp_fdb_resource_info));
847 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
851 /* Now that the HW Flow counter resource is deleted, reset it's
852 * corresponding slot in the SW accumulation table in the Flow Counter
855 if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
856 params->resource_sub_type ==
857 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
858 ulp_fc_mgr_cntr_reset(ulp_ctxt, params->direction,
859 params->resource_hndl);
862 if (params->resource_func == BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW) {
863 tun_tbl = bnxt_ulp_cntxt_ptr2_tun_tbl_get(ulp_ctxt);
867 rc = ulp_flow_db_parent_tun_idx_get(ulp_ctxt, fid, &tun_idx);
871 ulp_clear_tun_entry(tun_tbl, tun_idx);
874 /* all good, return success */
879 * Free the flow database entry
881 * ulp_ctxt [in] Ptr to ulp_context
882 * flow_type [in] - specify default or regular
883 * fid [in] The index to the flow entry
885 * returns 0 on success and negative on failure.
888 ulp_flow_db_fid_free(struct bnxt_ulp_context *ulp_ctxt,
889 enum bnxt_ulp_fdb_type flow_type,
892 struct bnxt_tun_cache_entry *tun_tbl;
893 struct bnxt_ulp_flow_tbl *flow_tbl;
894 struct bnxt_ulp_flow_db *flow_db;
896 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
898 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
902 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
903 BNXT_TF_DBG(ERR, "Invalid flow type\n");
907 flow_tbl = &flow_db->flow_tbl;
909 /* check for limits of fid */
910 if (fid >= flow_tbl->num_flows || !fid) {
911 BNXT_TF_DBG(ERR, "Invalid flow index\n");
915 /* check if the flow is active or not */
916 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
917 BNXT_TF_DBG(ERR, "flow does not exist %x:%x\n", flow_type, fid);
920 flow_tbl->head_index--;
921 if (!flow_tbl->head_index) {
922 BNXT_TF_DBG(ERR, "FlowDB: Head Ptr is zero\n");
926 flow_tbl->flow_tbl_stack[flow_tbl->head_index] = fid;
928 /* Clear the flows bitmap */
929 ulp_flow_db_active_flows_bit_set(flow_db, flow_type, fid, 0);
931 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
932 ulp_flow_db_func_id_set(flow_db, fid, 0);
934 tun_tbl = bnxt_ulp_cntxt_ptr2_tun_tbl_get(ulp_ctxt);
938 ulp_clear_tun_inner_entry(tun_tbl, fid);
940 /* all good, return success */
945 *Get the flow database entry details
947 * ulp_ctxt [in] Ptr to ulp_context
948 * flow_type [in] - specify default or regular
949 * fid [in] The index to the flow entry
950 * nxt_idx [in/out] the index to the next entry
951 * params [out] The contents to be copied into params.
953 * returns 0 on success and negative on failure.
956 ulp_flow_db_resource_get(struct bnxt_ulp_context *ulp_ctxt,
957 enum bnxt_ulp_fdb_type flow_type,
960 struct ulp_flow_db_res_params *params)
962 struct bnxt_ulp_flow_db *flow_db;
963 struct bnxt_ulp_flow_tbl *flow_tbl;
964 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
966 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
968 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
972 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
973 BNXT_TF_DBG(ERR, "Invalid flow type\n");
977 flow_tbl = &flow_db->flow_tbl;
979 /* check for limits of fid */
980 if (fid >= flow_tbl->num_flows || !fid) {
981 BNXT_TF_DBG(ERR, "Invalid flow index\n");
985 /* check if the flow is active or not */
986 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
987 BNXT_TF_DBG(ERR, "flow does not exist\n");
992 fid_resource = &flow_tbl->flow_resources[fid];
993 ulp_flow_db_res_info_to_params(fid_resource, params);
994 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
995 fid_resource->nxt_resource_idx);
997 nxt_resource = &flow_tbl->flow_resources[*nxt_idx];
998 ulp_flow_db_res_info_to_params(nxt_resource, params);
1000 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
1001 nxt_resource->nxt_resource_idx);
1004 /* all good, return success */
1009 * Get the flow database entry iteratively
1011 * flow_tbl [in] Ptr to flow table
1012 * flow_type [in] - specify default or regular
1013 * fid [in/out] The index to the flow entry
1015 * returns 0 on success and negative on failure.
1018 ulp_flow_db_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
1019 enum bnxt_ulp_fdb_type flow_type,
1022 uint32_t lfid = *fid;
1023 uint32_t idx, s_idx, mod_fid;
1025 uint64_t *active_flows;
1026 struct bnxt_ulp_flow_tbl *flowtbl = &flow_db->flow_tbl;
1028 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR) {
1029 active_flows = flowtbl->active_reg_flows;
1030 } else if (flow_type == BNXT_ULP_FDB_TYPE_DEFAULT) {
1031 active_flows = flowtbl->active_dflt_flows;
1033 BNXT_TF_DBG(ERR, "Invalid flow type %x\n", flow_type);
1038 /* increment the flow id to find the next valid flow id */
1040 if (lfid >= flowtbl->num_flows)
1042 idx = lfid / ULP_INDEX_BITMAP_SIZE;
1043 mod_fid = lfid % ULP_INDEX_BITMAP_SIZE;
1045 while (!(bs = active_flows[idx])) {
1047 if ((idx * ULP_INDEX_BITMAP_SIZE) >= flowtbl->num_flows)
1051 * remove the previous bits in the bitset bs to find the
1052 * next non zero bit in the bitset. This needs to be done
1053 * only if the idx is same as he one you started.
1056 bs &= (-1UL >> mod_fid);
1057 lfid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1059 BNXT_TF_DBG(ERR, "Flow Database is corrupt\n");
1062 } while (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type,
1065 /* all good, return success */
1071 * Flush all flows in the flow database.
1073 * ulp_ctxt [in] Ptr to ulp context
1074 * flow_type [in] - specify default or regular
1076 * returns 0 on success or negative number on failure
1079 ulp_flow_db_flush_flows(struct bnxt_ulp_context *ulp_ctx,
1080 enum bnxt_ulp_fdb_type flow_type)
1083 struct bnxt_ulp_flow_db *flow_db;
1086 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1090 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1092 BNXT_TF_DBG(ERR, "Flow database not found\n");
1095 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1096 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1100 while (!ulp_flow_db_next_entry_get(flow_db, flow_type, &fid))
1101 ulp_mapper_resources_free(ulp_ctx, flow_type, fid);
1103 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1109 * Flush all flows in the flow database that belong to a device function.
1111 * ulp_ctxt [in] Ptr to ulp context
1112 * func_id [in] - The port function id
1114 * returns 0 on success or negative number on failure
1117 ulp_flow_db_function_flow_flush(struct bnxt_ulp_context *ulp_ctx,
1120 uint32_t flow_id = 0;
1121 struct bnxt_ulp_flow_db *flow_db;
1123 if (!ulp_ctx || !func_id) {
1124 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1128 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1130 BNXT_TF_DBG(ERR, "Flow database not found\n");
1133 if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1134 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1138 while (!ulp_flow_db_next_entry_get(flow_db, BNXT_ULP_FDB_TYPE_REGULAR,
1140 if (flow_db->func_id_tbl[flow_id] == func_id)
1141 ulp_mapper_resources_free(ulp_ctx,
1142 BNXT_ULP_FDB_TYPE_REGULAR,
1145 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1150 * Flush all flows in the flow database that are associated with the session.
1152 * ulp_ctxt [in] Ptr to ulp context
1154 * returns 0 on success or negative number on failure
1157 ulp_flow_db_session_flow_flush(struct bnxt_ulp_context *ulp_ctx)
1160 * TBD: Tf core implementation of FW session flush shall change this
1163 return ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1167 * Check that flow id matches the function id or not
1169 * ulp_ctxt [in] Ptr to ulp context
1170 * flow_db [in] Ptr to flow table
1171 * func_id [in] The func_id to be set, for reset pass zero.
1173 * returns true on success or false on failure
1176 ulp_flow_db_validate_flow_func(struct bnxt_ulp_context *ulp_ctx,
1180 struct bnxt_ulp_flow_db *flow_db;
1182 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1184 BNXT_TF_DBG(ERR, "Flow database not found\n");
1188 /* set the function id in the function table */
1189 if (flow_id < flow_db->func_id_tbl_size && func_id &&
1190 flow_db->func_id_tbl[flow_id] == func_id)
1197 * Internal api to traverse the resource list within a flow
1198 * and match a resource based on resource func and resource
1199 * sub type. This api should be used only for resources that
1200 * are unique and do not have multiple instances of resource
1201 * func and sub type combination since it will return only
1205 ulp_flow_db_resource_params_get(struct bnxt_ulp_context *ulp_ctx,
1206 enum bnxt_ulp_fdb_type flow_type,
1208 uint32_t resource_func,
1209 uint32_t res_subtype,
1210 struct ulp_flow_db_res_params *params)
1212 struct bnxt_ulp_flow_db *flow_db;
1213 struct bnxt_ulp_flow_tbl *flow_tbl;
1214 struct ulp_fdb_resource_info *fid_res;
1217 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1219 BNXT_TF_DBG(ERR, "Flow database not found\n");
1224 BNXT_TF_DBG(ERR, "invalid argument\n");
1228 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
1229 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1233 flow_tbl = &flow_db->flow_tbl;
1235 /* check for limits of fid */
1236 if (flow_id >= flow_tbl->num_flows || !flow_id) {
1237 BNXT_TF_DBG(ERR, "Invalid flow index\n");
1241 /* check if the flow is active or not */
1242 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, flow_id)) {
1243 BNXT_TF_DBG(ERR, "flow does not exist\n");
1246 /* Iterate the resource to get the resource handle */
1248 memset(params, 0, sizeof(struct ulp_flow_db_res_params));
1250 fid_res = &flow_tbl->flow_resources[res_id];
1251 if (ulp_flow_db_resource_func_get(fid_res) == resource_func) {
1252 if (resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
1253 if (res_subtype == fid_res->resource_sub_type) {
1254 ulp_flow_db_res_info_to_params(fid_res,
1259 } else if (resource_func ==
1260 BNXT_ULP_RESOURCE_FUNC_EM_TABLE) {
1261 ulp_flow_db_res_info_to_params(fid_res,
1267 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1273 * Api to get the cfa action pointer from a flow.
1275 * ulp_ctxt [in] Ptr to ulp context
1276 * flow_id [in] flow id
1277 * cfa_action [out] The resource handle stored in the flow database
1279 * returns 0 on success
1282 ulp_default_flow_db_cfa_action_get(struct bnxt_ulp_context *ulp_ctx,
1284 uint16_t *cfa_action)
1286 uint8_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_VFR_CFA_ACTION;
1287 struct ulp_flow_db_res_params params;
1290 rc = ulp_flow_db_resource_params_get(ulp_ctx,
1291 BNXT_ULP_FDB_TYPE_DEFAULT,
1293 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1296 BNXT_TF_DBG(INFO, "CFA Action ptr not found for flow id %u\n",
1300 *cfa_action = params.resource_hndl;
1305 * Allocate the entry in the parent-child database
1307 * ulp_ctxt [in] Ptr to ulp_context
1308 * fid [in] The flow id to the flow entry
1310 * returns index on success and negative on failure.
1313 ulp_flow_db_parent_flow_alloc(struct bnxt_ulp_context *ulp_ctxt,
1316 struct bnxt_ulp_flow_db *flow_db;
1317 struct ulp_fdb_parent_child_db *p_pdb;
1318 uint32_t idx, free_idx = 0;
1320 /* validate the arguments */
1321 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1323 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1327 p_pdb = &flow_db->parent_child_db;
1328 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1329 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1330 BNXT_TF_DBG(ERR, "fid is already allocated\n");
1333 if (!p_pdb->parent_flow_tbl[idx].parent_fid && !free_idx)
1338 BNXT_TF_DBG(ERR, "parent child db is full\n");
1343 /* set the Fid in the parent child */
1344 p_pdb->parent_flow_tbl[free_idx].parent_fid = fid;
1349 * Free the entry in the parent-child database
1351 * ulp_ctxt [in] Ptr to ulp_context
1352 * fid [in] The flow id to the flow entry
1354 * returns 0 on success and negative on failure.
1357 ulp_flow_db_parent_flow_free(struct bnxt_ulp_context *ulp_ctxt,
1360 struct bnxt_ulp_flow_db *flow_db;
1361 struct ulp_fdb_parent_child_db *p_pdb;
1364 /* validate the arguments */
1365 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1367 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1371 p_pdb = &flow_db->parent_child_db;
1372 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1373 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1374 /* free the contents */
1375 p_pdb->parent_flow_tbl[idx].parent_fid = 0;
1376 memset(p_pdb->parent_flow_tbl[idx].child_fid_bitset,
1377 0, p_pdb->child_bitset_size);
1381 BNXT_TF_DBG(ERR, "parent entry not found = %x\n", fid);
1386 * Set or reset the child flow in the parent-child database
1388 * ulp_ctxt [in] Ptr to ulp_context
1389 * parent_fid [in] The flow id of the parent flow entry
1390 * child_fid [in] The flow id of the child flow entry
1391 * set_flag [in] Use 1 for setting child, 0 to reset
1393 * returns zero on success and negative on failure.
1396 ulp_flow_db_parent_child_flow_set(struct bnxt_ulp_context *ulp_ctxt,
1397 uint32_t parent_fid,
1401 struct bnxt_ulp_flow_db *flow_db;
1402 struct ulp_fdb_parent_child_db *p_pdb;
1403 uint32_t idx, a_idx;
1406 /* validate the arguments */
1407 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1409 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1413 /* check for fid validity */
1414 if (child_fid >= flow_db->flow_tbl.num_flows || !child_fid) {
1415 BNXT_TF_DBG(ERR, "Invalid child flow index %x\n", child_fid);
1419 p_pdb = &flow_db->parent_child_db;
1420 a_idx = child_fid / ULP_INDEX_BITMAP_SIZE;
1421 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1422 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1423 t = p_pdb->parent_flow_tbl[idx].child_fid_bitset;
1425 ULP_INDEX_BITMAP_SET(t[a_idx], child_fid);
1427 ULP_INDEX_BITMAP_RESET(t[a_idx], child_fid);
1431 BNXT_TF_DBG(ERR, "Unable to set the parent-child flow %x:%x\n",
1432 parent_fid, child_fid);
1437 * Get the parent index from the parent-child database
1439 * ulp_ctxt [in] Ptr to ulp_context
1440 * parent_fid [in] The flow id of the parent flow entry
1441 * parent_idx [out] The parent index of parent flow entry
1443 * returns zero on success and negative on failure.
1446 ulp_flow_db_parent_flow_idx_get(struct bnxt_ulp_context *ulp_ctxt,
1447 uint32_t parent_fid,
1448 uint32_t *parent_idx)
1450 struct bnxt_ulp_flow_db *flow_db;
1451 struct ulp_fdb_parent_child_db *p_pdb;
1454 /* validate the arguments */
1455 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1457 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1461 p_pdb = &flow_db->parent_child_db;
1462 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1463 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1468 BNXT_TF_DBG(ERR, "Unable to get the parent flow %x\n", parent_fid);
1473 * Get the next child flow in the parent-child database
1475 * ulp_ctxt [in] Ptr to ulp_context
1476 * parent_fid [in] The flow id of the parent flow entry
1477 * child_fid [in/out] The flow id of the child flow entry
1479 * returns zero on success and negative on failure.
1480 * Pass child_fid as zero for first entry.
1483 ulp_flow_db_parent_child_flow_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
1484 uint32_t parent_idx,
1485 uint32_t *child_fid)
1487 struct ulp_fdb_parent_child_db *p_pdb;
1488 uint32_t idx, s_idx, mod_fid;
1489 uint32_t next_fid = *child_fid;
1490 uint64_t *child_bitset;
1493 /* check for fid validity */
1494 p_pdb = &flow_db->parent_child_db;
1495 if (parent_idx >= p_pdb->entries_count ||
1496 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1497 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1501 child_bitset = p_pdb->parent_flow_tbl[parent_idx].child_fid_bitset;
1503 /* increment the flow id to find the next valid flow id */
1505 if (next_fid >= flow_db->flow_tbl.num_flows)
1507 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1508 mod_fid = next_fid % ULP_INDEX_BITMAP_SIZE;
1510 while (!(bs = child_bitset[idx])) {
1512 if ((idx * ULP_INDEX_BITMAP_SIZE) >=
1513 flow_db->flow_tbl.num_flows)
1517 * remove the previous bits in the bitset bs to find the
1518 * next non zero bit in the bitset. This needs to be done
1519 * only if the idx is same as he one you started.
1522 bs &= (-1UL >> mod_fid);
1523 next_fid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1524 if (*child_fid >= next_fid) {
1525 BNXT_TF_DBG(ERR, "Parent Child Database is corrupt\n");
1528 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1529 } while (!ULP_INDEX_BITMAP_GET(child_bitset[idx], next_fid));
1530 *child_fid = next_fid;
1535 * Set the counter accumulation in the parent flow
1537 * ulp_ctxt [in] Ptr to ulp_context
1538 * parent_idx [in] The parent index of the parent flow entry
1540 * returns index on success and negative on failure.
1543 ulp_flow_db_parent_flow_count_accum_set(struct bnxt_ulp_context *ulp_ctxt,
1544 uint32_t parent_idx)
1546 struct bnxt_ulp_flow_db *flow_db;
1547 struct ulp_fdb_parent_child_db *p_pdb;
1549 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1551 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1555 /* check for parent idx validity */
1556 p_pdb = &flow_db->parent_child_db;
1557 if (parent_idx >= p_pdb->entries_count ||
1558 !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1559 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1563 p_pdb->parent_flow_tbl[parent_idx].counter_acc = 1;
1568 * Get the counter accumulation in the parent flow
1570 * ulp_ctxt [in] Ptr to ulp_context
1571 * parent_fid [in] The flow id of the parent flow entry
1573 * returns 0 if counter accum is set else -1.
1576 ulp_flow_db_parent_flow_count_accum_get(struct bnxt_ulp_context *ulp_ctxt,
1577 uint32_t parent_fid)
1579 struct bnxt_ulp_flow_db *flow_db;
1580 struct ulp_fdb_parent_child_db *p_pdb;
1583 /* validate the arguments */
1584 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1586 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1590 p_pdb = &flow_db->parent_child_db;
1591 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1592 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1593 if (p_pdb->parent_flow_tbl[idx].counter_acc)
1602 * Orphan the child flow entry
1603 * This is called only for child flows that have
1604 * BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW resource
1606 * ulp_ctxt [in] Ptr to ulp_context
1607 * flow_type [in] Specify it is regular or default flow
1608 * fid [in] The index to the flow entry
1610 * Returns 0 on success and negative on failure.
1613 ulp_flow_db_child_flow_reset(struct bnxt_ulp_context *ulp_ctxt,
1614 enum bnxt_ulp_fdb_type flow_type,
1617 struct bnxt_ulp_flow_db *flow_db;
1618 struct bnxt_ulp_flow_tbl *flow_tbl;
1619 struct ulp_fdb_resource_info *fid_res;
1620 uint32_t res_id = 0;
1622 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1624 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1628 if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
1629 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1633 flow_tbl = &flow_db->flow_tbl;
1634 /* check for max flows */
1635 if (fid >= flow_tbl->num_flows || !fid) {
1636 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
1640 /* check if the flow is active or not */
1641 if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
1642 BNXT_TF_DBG(ERR, "flow does not exist\n");
1646 /* Iterate the resource to get the resource handle */
1649 fid_res = &flow_tbl->flow_resources[res_id];
1650 if (ulp_flow_db_resource_func_get(fid_res) ==
1651 BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW) {
1652 /* invalidate the resource details */
1653 fid_res->resource_hndl = 0;
1657 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1664 * Create parent flow in the parent flow tbl
1666 * parms [in] Ptr to mapper params
1668 * Returns 0 on success and negative on failure.
1671 ulp_flow_db_parent_flow_create(struct bnxt_ulp_mapper_parms *parms)
1673 struct ulp_flow_db_res_params fid_parms;
1674 uint32_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT_ACC;
1675 struct ulp_flow_db_res_params res_params;
1676 int32_t fid_idx, rc;
1678 /* create the child flow entry in parent flow table */
1679 fid_idx = ulp_flow_db_parent_flow_alloc(parms->ulp_ctx, parms->fid);
1681 BNXT_TF_DBG(ERR, "Error in creating parent flow fid %x\n",
1686 /* Add the parent details in the resource list of the flow */
1687 memset(&fid_parms, 0, sizeof(fid_parms));
1688 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW;
1689 fid_parms.resource_hndl = fid_idx;
1690 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1691 if (ulp_flow_db_resource_add(parms->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
1692 parms->fid, &fid_parms)) {
1693 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1698 /* check of the flow has internal counter accumulation enabled */
1699 if (!ulp_flow_db_resource_params_get(parms->ulp_ctx,
1700 BNXT_ULP_FDB_TYPE_REGULAR,
1702 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1705 /* Enable the counter accumulation in parent entry */
1706 if (ulp_flow_db_parent_flow_count_accum_set(parms->ulp_ctx,
1708 BNXT_TF_DBG(ERR, "Error in setting counter acc %x\n",
1714 rc = ulp_flow_db_parent_tun_idx_set(parms->ulp_ctx, fid_idx,
1717 BNXT_TF_DBG(ERR, "Error setting tun_idx in the parent flow\n");
1725 * Create child flow in the parent flow tbl
1727 * parms [in] Ptr to mapper params
1729 * Returns 0 on success and negative on failure.
1732 ulp_flow_db_child_flow_create(struct bnxt_ulp_mapper_parms *parms)
1734 struct ulp_flow_db_res_params fid_parms;
1735 uint32_t sub_type = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT;
1736 enum bnxt_ulp_resource_func res_fun;
1737 struct ulp_flow_db_res_params res_p;
1738 uint32_t parent_fid = parms->parent_fid;
1741 /* create the parent flow entry in parent flow table */
1742 rc = ulp_flow_db_parent_child_flow_set(parms->ulp_ctx,
1746 BNXT_TF_DBG(ERR, "Error in setting child fid %x\n", parms->fid);
1750 /* Add the parent details in the resource list of the flow */
1751 memset(&fid_parms, 0, sizeof(fid_parms));
1752 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW;
1753 fid_parms.resource_hndl = parms->parent_fid;
1754 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1755 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1756 BNXT_ULP_FDB_TYPE_REGULAR,
1757 parms->fid, &fid_parms);
1759 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1764 /* check if accumulation count is set for parent flow */
1765 rc = ulp_flow_db_parent_flow_count_accum_get(parms->ulp_ctx,
1768 /* check if internal count action included for this flow.*/
1769 res_fun = BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE;
1770 rc = ulp_flow_db_resource_params_get(parms->ulp_ctx,
1771 BNXT_ULP_FDB_TYPE_REGULAR,
1777 /* update the counter manager to include parent fid */
1778 if (ulp_fc_mgr_cntr_parent_flow_set(parms->ulp_ctx,
1780 res_p.resource_hndl,
1782 BNXT_TF_DBG(ERR, "Error in setting child %x\n",
1788 /* return success */
1793 * Update the parent counters
1795 * ulp_ctxt [in] Ptr to ulp_context
1796 * parent_fid [in] The flow id of the parent flow entry
1797 * packet_count [in] - packet count
1798 * byte_count [in] - byte count
1800 * returns 0 on success
1803 ulp_flow_db_parent_flow_count_update(struct bnxt_ulp_context *ulp_ctxt,
1804 uint32_t parent_fid,
1805 uint64_t packet_count,
1806 uint64_t byte_count)
1808 struct bnxt_ulp_flow_db *flow_db;
1809 struct ulp_fdb_parent_child_db *p_pdb;
1812 /* validate the arguments */
1813 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1815 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1819 p_pdb = &flow_db->parent_child_db;
1820 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1821 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1822 if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1823 p_pdb->parent_flow_tbl[idx].pkt_count +=
1825 p_pdb->parent_flow_tbl[idx].byte_count +=
1835 * Get the parent accumulation counters
1837 * ulp_ctxt [in] Ptr to ulp_context
1838 * parent_fid [in] The flow id of the parent flow entry
1839 * packet_count [out] - packet count
1840 * byte_count [out] - byte count
1842 * returns 0 on success
1845 ulp_flow_db_parent_flow_count_get(struct bnxt_ulp_context *ulp_ctxt,
1846 uint32_t parent_fid, uint64_t *packet_count,
1847 uint64_t *byte_count, uint8_t count_reset)
1849 struct bnxt_ulp_flow_db *flow_db;
1850 struct ulp_fdb_parent_child_db *p_pdb;
1853 /* validate the arguments */
1854 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1856 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1860 p_pdb = &flow_db->parent_child_db;
1861 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1862 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1863 if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1865 p_pdb->parent_flow_tbl[idx].pkt_count;
1867 p_pdb->parent_flow_tbl[idx].byte_count;
1869 p_pdb->parent_flow_tbl[idx].pkt_count = 0;
1870 p_pdb->parent_flow_tbl[idx].byte_count = 0;
1880 * reset the parent accumulation counters
1882 * ulp_ctxt [in] Ptr to ulp_context
1887 ulp_flow_db_parent_flow_count_reset(struct bnxt_ulp_context *ulp_ctxt)
1889 struct bnxt_ulp_flow_db *flow_db;
1890 struct ulp_fdb_parent_child_db *p_pdb;
1893 /* validate the arguments */
1894 flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, 1);
1896 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1900 p_pdb = &flow_db->parent_child_db;
1901 for (idx = 0; idx < p_pdb->entries_count; idx++) {
1902 if (p_pdb->parent_flow_tbl[idx].parent_fid &&
1903 p_pdb->parent_flow_tbl[idx].counter_acc) {
1904 p_pdb->parent_flow_tbl[idx].pkt_count = 0;
1905 p_pdb->parent_flow_tbl[idx].byte_count = 0;
1911 * Set the shared bit for the flow db entry
1913 * res [in] Ptr to fdb entry
1914 * shared [in] shared flag
1918 void ulp_flow_db_shared_session_set(struct ulp_flow_db_res_params *res,
1919 enum bnxt_ulp_shared_session shared)
1921 if (res && (shared & BNXT_ULP_SHARED_SESSION_YES))
1922 res->fdb_flags |= ULP_FDB_FLAG_SHARED_SESSION;