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 set the bit in the active flow table
27 * No validation is done in this function.
29 * flow_tbl [in] Ptr to flow table
30 * idx [in] The index to bit to be set or reset.
31 * flag [in] 1 to set and 0 to reset.
36 ulp_flow_db_active_flow_set(struct bnxt_ulp_flow_tbl *flow_tbl,
40 uint32_t active_index;
42 active_index = idx / ULP_INDEX_BITMAP_SIZE;
44 ULP_INDEX_BITMAP_SET(flow_tbl->active_flow_tbl[active_index],
47 ULP_INDEX_BITMAP_RESET(flow_tbl->active_flow_tbl[active_index],
52 * Helper function to allocate the flow table and initialize
53 * is set.No validation being done in this function.
55 * flow_tbl [in] Ptr to flow table
56 * idx [in] The index to bit to be set or reset.
58 * returns 1 on set or 0 if not set.
61 ulp_flow_db_active_flow_is_set(struct bnxt_ulp_flow_tbl *flow_tbl,
64 uint32_t active_index;
66 active_index = idx / ULP_INDEX_BITMAP_SIZE;
67 return ULP_INDEX_BITMAP_GET(flow_tbl->active_flow_tbl[active_index],
72 * Helper function to copy the resource params to resource info
73 * No validation being done in this function.
75 * resource_info [out] Ptr to resource information
76 * params [in] The input params from the caller
80 ulp_flow_db_res_params_to_info(struct ulp_fdb_resource_info *resource_info,
81 struct ulp_flow_db_res_params *params)
83 resource_info->nxt_resource_idx |= ((params->direction <<
84 ULP_FLOW_DB_RES_DIR_BIT) &
85 ULP_FLOW_DB_RES_DIR_MASK);
86 resource_info->nxt_resource_idx |= ((params->resource_func <<
87 ULP_FLOW_DB_RES_FUNC_BITS) &
88 ULP_FLOW_DB_RES_FUNC_MASK);
90 if (params->resource_func != BNXT_ULP_RESOURCE_FUNC_EM_TABLE) {
91 resource_info->resource_hndl = (uint32_t)params->resource_hndl;
92 resource_info->resource_type = params->resource_type;
95 resource_info->resource_em_handle = params->resource_hndl;
100 * Helper function to copy the resource params to resource info
101 * No validation being done in this function.
103 * resource_info [in] Ptr to resource information
104 * params [out] The output params to the caller
109 ulp_flow_db_res_info_to_params(struct ulp_fdb_resource_info *resource_info,
110 struct ulp_flow_db_res_params *params)
112 memset(params, 0, sizeof(struct ulp_flow_db_res_params));
113 params->direction = ((resource_info->nxt_resource_idx &
114 ULP_FLOW_DB_RES_DIR_MASK) >>
115 ULP_FLOW_DB_RES_DIR_BIT);
116 params->resource_func = ((resource_info->nxt_resource_idx &
117 ULP_FLOW_DB_RES_FUNC_MASK) >>
118 ULP_FLOW_DB_RES_FUNC_BITS);
120 if (params->resource_func != BNXT_ULP_RESOURCE_FUNC_EM_TABLE) {
121 params->resource_hndl = resource_info->resource_hndl;
122 params->resource_type = resource_info->resource_type;
124 params->resource_hndl = resource_info->resource_em_handle;
129 * Helper function to allocate the flow table and initialize
130 * the stack for allocation operations.
132 * flow_db [in] Ptr to flow database structure
133 * tbl_idx [in] The index to table creation.
135 * Returns 0 on success or negative number on failure.
138 ulp_flow_db_alloc_resource(struct bnxt_ulp_flow_db *flow_db,
139 enum bnxt_ulp_flow_db_tables tbl_idx)
142 struct bnxt_ulp_flow_tbl *flow_tbl;
145 flow_tbl = &flow_db->flow_tbl[tbl_idx];
147 size = sizeof(struct ulp_fdb_resource_info) * flow_tbl->num_resources;
148 flow_tbl->flow_resources =
149 rte_zmalloc("ulp_fdb_resource_info", size, 0);
151 if (!flow_tbl->flow_resources) {
152 BNXT_TF_DBG(ERR, "Failed to alloc memory for flow table\n");
155 size = sizeof(uint32_t) * flow_tbl->num_resources;
156 flow_tbl->flow_tbl_stack = rte_zmalloc("flow_tbl_stack", size, 0);
157 if (!flow_tbl->flow_tbl_stack) {
158 BNXT_TF_DBG(ERR, "Failed to alloc memory flow tbl stack\n");
161 size = (flow_tbl->num_flows / sizeof(uint64_t)) + 1;
162 flow_tbl->active_flow_tbl = rte_zmalloc("active flow tbl", size, 0);
163 if (!flow_tbl->active_flow_tbl) {
164 BNXT_TF_DBG(ERR, "Failed to alloc memory active tbl\n");
168 /* Initialize the stack table. */
169 for (idx = 0; idx < flow_tbl->num_resources; idx++)
170 flow_tbl->flow_tbl_stack[idx] = idx;
172 /* Ignore the first element in the list. */
173 flow_tbl->head_index = 1;
174 /* Tail points to the last entry in the list. */
175 flow_tbl->tail_index = flow_tbl->num_resources - 1;
180 * Helper function to deallocate the flow table.
182 * flow_db [in] Ptr to flow database structure
183 * tbl_idx [in] The index to table creation.
188 ulp_flow_db_dealloc_resource(struct bnxt_ulp_flow_db *flow_db,
189 enum bnxt_ulp_flow_db_tables tbl_idx)
191 struct bnxt_ulp_flow_tbl *flow_tbl;
193 flow_tbl = &flow_db->flow_tbl[tbl_idx];
195 /* Free all the allocated tables in the flow table. */
196 if (flow_tbl->active_flow_tbl) {
197 rte_free(flow_tbl->active_flow_tbl);
198 flow_tbl->active_flow_tbl = NULL;
201 if (flow_tbl->flow_tbl_stack) {
202 rte_free(flow_tbl->flow_tbl_stack);
203 flow_tbl->flow_tbl_stack = NULL;
206 if (flow_tbl->flow_resources) {
207 rte_free(flow_tbl->flow_resources);
208 flow_tbl->flow_resources = NULL;
213 * Initialize the flow database. Memory is allocated in this
214 * call and assigned to the flow database.
216 * ulp_ctxt [in] Ptr to ulp context
218 * Returns 0 on success or negative number on failure.
220 int32_t ulp_flow_db_init(struct bnxt_ulp_context *ulp_ctxt)
222 struct bnxt_ulp_device_params *dparms;
223 struct bnxt_ulp_flow_tbl *flow_tbl;
224 struct bnxt_ulp_flow_db *flow_db;
227 /* Get the dev specific number of flows that needed to be supported. */
228 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctxt, &dev_id)) {
229 BNXT_TF_DBG(ERR, "Invalid device id\n");
233 dparms = bnxt_ulp_device_params_get(dev_id);
235 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
239 flow_db = rte_zmalloc("bnxt_ulp_flow_db",
240 sizeof(struct bnxt_ulp_flow_db), 0);
243 "Failed to allocate memory for flow table ptr\n");
247 /* Attach the flow database to the ulp context. */
248 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, flow_db);
250 /* Populate the regular flow table limits. */
251 flow_tbl = &flow_db->flow_tbl[BNXT_ULP_REGULAR_FLOW_TABLE];
252 flow_tbl->num_flows = dparms->num_flows + 1;
253 flow_tbl->num_resources = (flow_tbl->num_flows *
254 dparms->num_resources_per_flow);
256 /* Populate the default flow table limits. */
257 flow_tbl = &flow_db->flow_tbl[BNXT_ULP_DEFAULT_FLOW_TABLE];
258 flow_tbl->num_flows = BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1;
259 flow_tbl->num_resources = (flow_tbl->num_flows *
260 BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES);
262 /* Allocate the resource for the regular flow table. */
263 if (ulp_flow_db_alloc_resource(flow_db, BNXT_ULP_REGULAR_FLOW_TABLE))
265 if (ulp_flow_db_alloc_resource(flow_db, BNXT_ULP_DEFAULT_FLOW_TABLE))
268 /* All good so return. */
271 ulp_flow_db_deinit(ulp_ctxt);
276 * Deinitialize the flow database. Memory is deallocated in
277 * this call and all flows should have been purged before this
280 * ulp_ctxt [in] Ptr to ulp context
282 * Returns 0 on success.
284 int32_t ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
286 struct bnxt_ulp_flow_db *flow_db;
288 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
290 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
294 /* Detach the flow database from the ulp context. */
295 bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, NULL);
297 /* Free up all the memory. */
298 ulp_flow_db_dealloc_resource(flow_db, BNXT_ULP_REGULAR_FLOW_TABLE);
299 ulp_flow_db_dealloc_resource(flow_db, BNXT_ULP_DEFAULT_FLOW_TABLE);
306 * Allocate the flow database entry
308 * ulp_ctxt [in] Ptr to ulp_context
309 * tbl_idx [in] Specify it is regular or default flow
310 * fid [out] The index to the flow entry
312 * returns 0 on success and negative on failure.
314 int32_t ulp_flow_db_fid_alloc(struct bnxt_ulp_context *ulp_ctxt,
315 enum bnxt_ulp_flow_db_tables tbl_idx,
318 struct bnxt_ulp_flow_db *flow_db;
319 struct bnxt_ulp_flow_tbl *flow_tbl;
321 *fid = 0; /* Initialize fid to invalid value */
322 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
324 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
328 flow_tbl = &flow_db->flow_tbl[tbl_idx];
329 /* check for max flows */
330 if (flow_tbl->num_flows <= flow_tbl->head_index) {
331 BNXT_TF_DBG(ERR, "Flow database has reached max flows\n");
334 *fid = flow_tbl->flow_tbl_stack[flow_tbl->head_index];
335 flow_tbl->head_index++;
336 ulp_flow_db_active_flow_set(flow_tbl, *fid, 1);
338 /* all good, return success */
343 * Allocate the flow database entry.
344 * The params->critical_resource has to be set to 0 to allocate a new resource.
346 * ulp_ctxt [in] Ptr to ulp_context
347 * tbl_idx [in] Specify it is regular or default flow
348 * fid [in] The index to the flow entry
349 * params [in] The contents to be copied into resource
351 * returns 0 on success and negative on failure.
353 int32_t ulp_flow_db_resource_add(struct bnxt_ulp_context *ulp_ctxt,
354 enum bnxt_ulp_flow_db_tables tbl_idx,
356 struct ulp_flow_db_res_params *params)
358 struct bnxt_ulp_flow_db *flow_db;
359 struct bnxt_ulp_flow_tbl *flow_tbl;
360 struct ulp_fdb_resource_info *resource, *fid_resource;
363 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
365 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
369 if (tbl_idx >= BNXT_ULP_FLOW_TABLE_MAX) {
370 BNXT_TF_DBG(ERR, "Invalid table index\n");
373 flow_tbl = &flow_db->flow_tbl[tbl_idx];
375 /* check for max flows */
376 if (fid >= flow_tbl->num_flows || !fid) {
377 BNXT_TF_DBG(ERR, "Invalid flow index\n");
381 /* check if the flow is active or not */
382 if (!ulp_flow_db_active_flow_is_set(flow_tbl, fid)) {
383 BNXT_TF_DBG(ERR, "flow does not exist\n");
387 /* check for max resource */
388 if ((flow_tbl->num_flows + 1) >= flow_tbl->tail_index) {
389 BNXT_TF_DBG(ERR, "Flow db has reached max resources\n");
392 fid_resource = &flow_tbl->flow_resources[fid];
394 if (!params->critical_resource) {
395 /* Not the critical_resource so allocate a resource */
396 idx = flow_tbl->flow_tbl_stack[flow_tbl->tail_index];
397 resource = &flow_tbl->flow_resources[idx];
398 flow_tbl->tail_index--;
400 /* Update the chain list of resource*/
401 ULP_FLOW_DB_RES_NXT_SET(resource->nxt_resource_idx,
402 fid_resource->nxt_resource_idx);
403 /* update the contents */
404 ulp_flow_db_res_params_to_info(resource, params);
405 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
406 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
409 /* critical resource. Just update the fid resource */
410 ulp_flow_db_res_params_to_info(fid_resource, params);
413 /* all good, return success */
418 * Free the flow database entry.
419 * The params->critical_resource has to be set to 1 to free the first resource.
421 * ulp_ctxt [in] Ptr to ulp_context
422 * tbl_idx [in] Specify it is regular or default flow
423 * fid [in] The index to the flow entry
424 * params [in/out] The contents to be copied into params.
425 * Onlythe critical_resource needs to be set by the caller.
427 * Returns 0 on success and negative on failure.
429 int32_t ulp_flow_db_resource_del(struct bnxt_ulp_context *ulp_ctxt,
430 enum bnxt_ulp_flow_db_tables tbl_idx,
432 struct ulp_flow_db_res_params *params)
434 struct bnxt_ulp_flow_db *flow_db;
435 struct bnxt_ulp_flow_tbl *flow_tbl;
436 struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
437 uint32_t nxt_idx = 0;
439 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
441 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
445 if (tbl_idx >= BNXT_ULP_FLOW_TABLE_MAX) {
446 BNXT_TF_DBG(ERR, "Invalid table index\n");
449 flow_tbl = &flow_db->flow_tbl[tbl_idx];
451 /* check for max flows */
452 if (fid >= flow_tbl->num_flows || !fid) {
453 BNXT_TF_DBG(ERR, "Invalid flow index\n");
457 /* check if the flow is active or not */
458 if (!ulp_flow_db_active_flow_is_set(flow_tbl, fid)) {
459 BNXT_TF_DBG(ERR, "flow does not exist\n");
463 fid_resource = &flow_tbl->flow_resources[fid];
464 if (!params->critical_resource) {
465 /* Not the critical resource so free the resource */
466 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
467 fid_resource->nxt_resource_idx);
469 /* reached end of resources */
472 nxt_resource = &flow_tbl->flow_resources[nxt_idx];
474 /* connect the fid resource to the next resource */
475 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
476 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
477 nxt_resource->nxt_resource_idx);
479 /* update the contents to be given to caller */
480 ulp_flow_db_res_info_to_params(nxt_resource, params);
482 /* Delete the nxt_resource */
483 memset(nxt_resource, 0, sizeof(struct ulp_fdb_resource_info));
485 /* add it to the free list */
486 flow_tbl->tail_index++;
487 if (flow_tbl->tail_index >= flow_tbl->num_resources) {
488 BNXT_TF_DBG(ERR, "FlowDB:Tail reached max\n");
491 flow_tbl->flow_tbl_stack[flow_tbl->tail_index] = nxt_idx;
494 /* Critical resource. copy the contents and exit */
495 ulp_flow_db_res_info_to_params(fid_resource, params);
496 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
497 fid_resource->nxt_resource_idx);
498 memset(fid_resource, 0, sizeof(struct ulp_fdb_resource_info));
499 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
503 /* all good, return success */
508 * Free the flow database entry
510 * ulp_ctxt [in] Ptr to ulp_context
511 * tbl_idx [in] Specify it is regular or default flow
512 * fid [in] The index to the flow entry
514 * returns 0 on success and negative on failure.
516 int32_t ulp_flow_db_fid_free(struct bnxt_ulp_context *ulp_ctxt,
517 enum bnxt_ulp_flow_db_tables tbl_idx,
520 struct bnxt_ulp_flow_db *flow_db;
521 struct bnxt_ulp_flow_tbl *flow_tbl;
523 flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
525 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
529 if (tbl_idx >= BNXT_ULP_FLOW_TABLE_MAX) {
530 BNXT_TF_DBG(ERR, "Invalid table index\n");
534 flow_tbl = &flow_db->flow_tbl[tbl_idx];
536 /* check for limits of fid */
537 if (fid >= flow_tbl->num_flows || !fid) {
538 BNXT_TF_DBG(ERR, "Invalid flow index\n");
542 /* check if the flow is active or not */
543 if (!ulp_flow_db_active_flow_is_set(flow_tbl, fid)) {
544 BNXT_TF_DBG(ERR, "flow does not exist\n");
547 flow_tbl->head_index--;
548 if (!flow_tbl->head_index) {
549 BNXT_TF_DBG(ERR, "FlowDB: Head Ptr is zero\n");
552 flow_tbl->flow_tbl_stack[flow_tbl->head_index] = fid;
553 ulp_flow_db_active_flow_set(flow_tbl, fid, 0);
555 /* all good, return success */