1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2020 Broadcom
6 #include <rte_malloc.h>
8 #include "bnxt_tf_common.h"
9 #include "ulp_flow_db.h"
10 #include "ulp_template_struct.h"
13 * Helper function to allocate the flow table and initialize
14 * the stack for allocation operations.
16 * flow_db [in] Ptr to flow database structure
17 * tbl_idx [in] The index to table creation.
19 * Returns 0 on success or negative number on failure.
22 ulp_flow_db_alloc_resource(struct bnxt_ulp_flow_db *flow_db,
23 enum bnxt_ulp_flow_db_tables tbl_idx)
26 struct bnxt_ulp_flow_tbl *flow_tbl;
29 flow_tbl = &flow_db->flow_tbl[tbl_idx];
31 size = sizeof(struct ulp_fdb_resource_info) * flow_tbl->num_resources;
32 flow_tbl->flow_resources =
33 rte_zmalloc("ulp_fdb_resource_info", size, 0);
35 if (!flow_tbl->flow_resources) {
36 BNXT_TF_DBG(ERR, "Failed to alloc memory for flow table\n");
39 size = sizeof(uint32_t) * flow_tbl->num_resources;
40 flow_tbl->flow_tbl_stack = rte_zmalloc("flow_tbl_stack", size, 0);
41 if (!flow_tbl->flow_tbl_stack) {
42 BNXT_TF_DBG(ERR, "Failed to alloc memory flow tbl stack\n");
45 size = (flow_tbl->num_flows / sizeof(uint64_t)) + 1;
46 flow_tbl->active_flow_tbl = rte_zmalloc("active flow tbl", size, 0);
47 if (!flow_tbl->active_flow_tbl) {
48 BNXT_TF_DBG(ERR, "Failed to alloc memory active tbl\n");
52 /* Initialize the stack table. */
53 for (idx = 0; idx < flow_tbl->num_resources; idx++)
54 flow_tbl->flow_tbl_stack[idx] = idx;
56 /* Ignore the first element in the list. */
57 flow_tbl->head_index = 1;
58 /* Tail points to the last entry in the list. */
59 flow_tbl->tail_index = flow_tbl->num_resources - 1;
64 * Helper function to de allocate the flow table.
66 * flow_db [in] Ptr to flow database structure
67 * tbl_idx [in] The index to table creation.
72 ulp_flow_db_dealloc_resource(struct bnxt_ulp_flow_db *flow_db,
73 enum bnxt_ulp_flow_db_tables tbl_idx)
75 struct bnxt_ulp_flow_tbl *flow_tbl;
77 flow_tbl = &flow_db->flow_tbl[tbl_idx];
79 /* Free all the allocated tables in the flow table. */
80 if (flow_tbl->active_flow_tbl) {
81 rte_free(flow_tbl->active_flow_tbl);
82 flow_tbl->active_flow_tbl = NULL;
85 if (flow_tbl->flow_tbl_stack) {
86 rte_free(flow_tbl->flow_tbl_stack);
87 flow_tbl->flow_tbl_stack = NULL;
90 if (flow_tbl->flow_resources) {
91 rte_free(flow_tbl->flow_resources);
92 flow_tbl->flow_resources = NULL;
97 * Initialize the flow database. Memory is allocated in this
98 * call and assigned to the flow database.
100 * ulp_ctxt [in] Ptr to ulp context
102 * Returns 0 on success or negative number on failure.
104 int32_t ulp_flow_db_init(struct bnxt_ulp_context *ulp_ctxt)
106 struct bnxt_ulp_device_params *dparms;
107 struct bnxt_ulp_flow_tbl *flow_tbl;
108 struct bnxt_ulp_flow_db *flow_db;
111 /* Get the dev specific number of flows that needed to be supported. */
112 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctxt, &dev_id)) {
113 BNXT_TF_DBG(ERR, "Invalid device id\n");
117 dparms = bnxt_ulp_device_params_get(dev_id);
119 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
123 flow_db = rte_zmalloc("bnxt_ulp_flow_db",
124 sizeof(struct bnxt_ulp_flow_db), 0);
127 "Failed to allocate memory for flow table ptr\n");
131 /* Attach the flow database to the ulp context. */
132 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, flow_db);
134 /* Populate the regular flow table limits. */
135 flow_tbl = &flow_db->flow_tbl[BNXT_ULP_REGULAR_FLOW_TABLE];
136 flow_tbl->num_flows = dparms->num_flows + 1;
137 flow_tbl->num_resources = (flow_tbl->num_flows *
138 dparms->num_resources_per_flow);
140 /* Populate the default flow table limits. */
141 flow_tbl = &flow_db->flow_tbl[BNXT_ULP_DEFAULT_FLOW_TABLE];
142 flow_tbl->num_flows = BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1;
143 flow_tbl->num_resources = (flow_tbl->num_flows *
144 BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES);
146 /* Allocate the resource for the regular flow table. */
147 if (ulp_flow_db_alloc_resource(flow_db, BNXT_ULP_REGULAR_FLOW_TABLE))
149 if (ulp_flow_db_alloc_resource(flow_db, BNXT_ULP_DEFAULT_FLOW_TABLE))
152 /* All good so return. */
155 ulp_flow_db_deinit(ulp_ctxt);
160 * Deinitialize the flow database. Memory is deallocated in
161 * this call and all flows should have been purged before this
164 * ulp_ctxt [in] Ptr to ulp context
166 * Returns 0 on success.
168 int32_t ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
170 struct bnxt_ulp_flow_db *flow_db;
172 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
174 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
178 /* Detach the flow database from the ulp context. */
179 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, NULL);
181 /* Free up all the memory. */
182 ulp_flow_db_dealloc_resource(flow_db, BNXT_ULP_REGULAR_FLOW_TABLE);
183 ulp_flow_db_dealloc_resource(flow_db, BNXT_ULP_DEFAULT_FLOW_TABLE);