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_utils.h"
11 #include "ulp_template_struct.h"
12 #include "ulp_mapper.h"
14 #define ULP_FLOW_DB_RES_DIR_BIT 31
15 #define ULP_FLOW_DB_RES_DIR_MASK 0x80000000
16 #define ULP_FLOW_DB_RES_FUNC_BITS 28
17 #define ULP_FLOW_DB_RES_FUNC_MASK 0x70000000
18 #define ULP_FLOW_DB_RES_NXT_MASK 0x0FFFFFFF
20 /* Macro to copy the nxt_resource_idx */
21 #define ULP_FLOW_DB_RES_NXT_SET(dst, src) {(dst) |= ((src) &\
22 ULP_FLOW_DB_RES_NXT_MASK); }
23 #define ULP_FLOW_DB_RES_NXT_RESET(dst) ((dst) &= ~(ULP_FLOW_DB_RES_NXT_MASK))
26 * Helper function to allocate the flow table and initialize
27 * is set.No validation being done in this function.
29 * flow_tbl [in] Ptr to flow table
30 * idx [in] The index to bit to be set or reset.
32 * returns 1 on set or 0 if not set.
35 ulp_flow_db_active_flow_is_set(struct bnxt_ulp_flow_tbl *flow_tbl,
38 uint32_t active_index;
40 active_index = idx / ULP_INDEX_BITMAP_SIZE;
41 return ULP_INDEX_BITMAP_GET(flow_tbl->active_flow_tbl[active_index],
46 * Helper function to copy the resource params to resource info
47 * No validation being done in this function.
49 * resource_info [out] Ptr to resource information
50 * params [in] The input params from the caller
54 ulp_flow_db_res_params_to_info(struct ulp_fdb_resource_info *resource_info,
55 struct ulp_flow_db_res_params *params)
57 resource_info->nxt_resource_idx |= ((params->direction <<
58 ULP_FLOW_DB_RES_DIR_BIT) &
59 ULP_FLOW_DB_RES_DIR_MASK);
60 resource_info->nxt_resource_idx |= ((params->resource_func <<
61 ULP_FLOW_DB_RES_FUNC_BITS) &
62 ULP_FLOW_DB_RES_FUNC_MASK);
64 if (params->resource_func != BNXT_ULP_RESOURCE_FUNC_EM_TABLE) {
65 resource_info->resource_hndl = (uint32_t)params->resource_hndl;
66 resource_info->resource_type = params->resource_type;
69 resource_info->resource_em_handle = params->resource_hndl;
74 * Helper function to allocate the flow table and initialize
75 * the stack for allocation operations.
77 * flow_db [in] Ptr to flow database structure
78 * tbl_idx [in] The index to table creation.
80 * Returns 0 on success or negative number on failure.
83 ulp_flow_db_alloc_resource(struct bnxt_ulp_flow_db *flow_db,
84 enum bnxt_ulp_flow_db_tables tbl_idx)
87 struct bnxt_ulp_flow_tbl *flow_tbl;
90 flow_tbl = &flow_db->flow_tbl[tbl_idx];
92 size = sizeof(struct ulp_fdb_resource_info) * flow_tbl->num_resources;
93 flow_tbl->flow_resources =
94 rte_zmalloc("ulp_fdb_resource_info", size, 0);
96 if (!flow_tbl->flow_resources) {
97 BNXT_TF_DBG(ERR, "Failed to alloc memory for flow table\n");
100 size = sizeof(uint32_t) * flow_tbl->num_resources;
101 flow_tbl->flow_tbl_stack = rte_zmalloc("flow_tbl_stack", size, 0);
102 if (!flow_tbl->flow_tbl_stack) {
103 BNXT_TF_DBG(ERR, "Failed to alloc memory flow tbl stack\n");
106 size = (flow_tbl->num_flows / sizeof(uint64_t)) + 1;
107 flow_tbl->active_flow_tbl = rte_zmalloc("active flow tbl", size, 0);
108 if (!flow_tbl->active_flow_tbl) {
109 BNXT_TF_DBG(ERR, "Failed to alloc memory active tbl\n");
113 /* Initialize the stack table. */
114 for (idx = 0; idx < flow_tbl->num_resources; idx++)
115 flow_tbl->flow_tbl_stack[idx] = idx;
117 /* Ignore the first element in the list. */
118 flow_tbl->head_index = 1;
119 /* Tail points to the last entry in the list. */
120 flow_tbl->tail_index = flow_tbl->num_resources - 1;
125 * Helper function to de allocate the flow table.
127 * flow_db [in] Ptr to flow database structure
128 * tbl_idx [in] The index to table creation.
133 ulp_flow_db_dealloc_resource(struct bnxt_ulp_flow_db *flow_db,
134 enum bnxt_ulp_flow_db_tables tbl_idx)
136 struct bnxt_ulp_flow_tbl *flow_tbl;
138 flow_tbl = &flow_db->flow_tbl[tbl_idx];
140 /* Free all the allocated tables in the flow table. */
141 if (flow_tbl->active_flow_tbl) {
142 rte_free(flow_tbl->active_flow_tbl);
143 flow_tbl->active_flow_tbl = NULL;
146 if (flow_tbl->flow_tbl_stack) {
147 rte_free(flow_tbl->flow_tbl_stack);
148 flow_tbl->flow_tbl_stack = NULL;
151 if (flow_tbl->flow_resources) {
152 rte_free(flow_tbl->flow_resources);
153 flow_tbl->flow_resources = NULL;
158 * Initialize the flow database. Memory is allocated in this
159 * call and assigned to the flow database.
161 * ulp_ctxt [in] Ptr to ulp context
163 * Returns 0 on success or negative number on failure.
165 int32_t ulp_flow_db_init(struct bnxt_ulp_context *ulp_ctxt)
167 struct bnxt_ulp_device_params *dparms;
168 struct bnxt_ulp_flow_tbl *flow_tbl;
169 struct bnxt_ulp_flow_db *flow_db;
172 /* Get the dev specific number of flows that needed to be supported. */
173 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctxt, &dev_id)) {
174 BNXT_TF_DBG(ERR, "Invalid device id\n");
178 dparms = bnxt_ulp_device_params_get(dev_id);
180 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
184 flow_db = rte_zmalloc("bnxt_ulp_flow_db",
185 sizeof(struct bnxt_ulp_flow_db), 0);
188 "Failed to allocate memory for flow table ptr\n");
192 /* Attach the flow database to the ulp context. */
193 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, flow_db);
195 /* Populate the regular flow table limits. */
196 flow_tbl = &flow_db->flow_tbl[BNXT_ULP_REGULAR_FLOW_TABLE];
197 flow_tbl->num_flows = dparms->num_flows + 1;
198 flow_tbl->num_resources = (flow_tbl->num_flows *
199 dparms->num_resources_per_flow);
201 /* Populate the default flow table limits. */
202 flow_tbl = &flow_db->flow_tbl[BNXT_ULP_DEFAULT_FLOW_TABLE];
203 flow_tbl->num_flows = BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1;
204 flow_tbl->num_resources = (flow_tbl->num_flows *
205 BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES);
207 /* Allocate the resource for the regular flow table. */
208 if (ulp_flow_db_alloc_resource(flow_db, BNXT_ULP_REGULAR_FLOW_TABLE))
210 if (ulp_flow_db_alloc_resource(flow_db, BNXT_ULP_DEFAULT_FLOW_TABLE))
213 /* All good so return. */
216 ulp_flow_db_deinit(ulp_ctxt);
221 * Deinitialize the flow database. Memory is deallocated in
222 * this call and all flows should have been purged before this
225 * ulp_ctxt [in] Ptr to ulp context
227 * Returns 0 on success.
229 int32_t ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
231 struct bnxt_ulp_flow_db *flow_db;
233 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
235 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
239 /* Detach the flow database from the ulp context. */
240 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, NULL);
242 /* Free up all the memory. */
243 ulp_flow_db_dealloc_resource(flow_db, BNXT_ULP_REGULAR_FLOW_TABLE);
244 ulp_flow_db_dealloc_resource(flow_db, BNXT_ULP_DEFAULT_FLOW_TABLE);
251 * Allocate the flow database entry.
252 * The params->critical_resource has to be set to 0 to allocate a new resource.
254 * ulp_ctxt [in] Ptr to ulp_context
255 * tbl_idx [in] Specify it is regular or default flow
256 * fid [in] The index to the flow entry
257 * params [in] The contents to be copied into resource
259 * returns 0 on success and negative on failure.
261 int32_t ulp_flow_db_resource_add(struct bnxt_ulp_context *ulp_ctxt,
262 enum bnxt_ulp_flow_db_tables tbl_idx,
264 struct ulp_flow_db_res_params *params)
266 struct bnxt_ulp_flow_db *flow_db;
267 struct bnxt_ulp_flow_tbl *flow_tbl;
268 struct ulp_fdb_resource_info *resource, *fid_resource;
271 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
273 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
277 if (tbl_idx >= BNXT_ULP_FLOW_TABLE_MAX) {
278 BNXT_TF_DBG(ERR, "Invalid table index\n");
281 flow_tbl = &flow_db->flow_tbl[tbl_idx];
283 /* check for max flows */
284 if (fid >= flow_tbl->num_flows || !fid) {
285 BNXT_TF_DBG(ERR, "Invalid flow index\n");
289 /* check if the flow is active or not */
290 if (!ulp_flow_db_active_flow_is_set(flow_tbl, fid)) {
291 BNXT_TF_DBG(ERR, "flow does not exist\n");
295 /* check for max resource */
296 if ((flow_tbl->num_flows + 1) >= flow_tbl->tail_index) {
297 BNXT_TF_DBG(ERR, "Flow db has reached max resources\n");
300 fid_resource = &flow_tbl->flow_resources[fid];
302 if (!params->critical_resource) {
303 /* Not the critical_resource so allocate a resource */
304 idx = flow_tbl->flow_tbl_stack[flow_tbl->tail_index];
305 resource = &flow_tbl->flow_resources[idx];
306 flow_tbl->tail_index--;
308 /* Update the chain list of resource*/
309 ULP_FLOW_DB_RES_NXT_SET(resource->nxt_resource_idx,
310 fid_resource->nxt_resource_idx);
311 /* update the contents */
312 ulp_flow_db_res_params_to_info(resource, params);
313 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
314 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
317 /* critical resource. Just update the fid resource */
318 ulp_flow_db_res_params_to_info(fid_resource, params);
321 /* all good, return success */