1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
6 #include <rte_common.h>
8 #include "tf_shadow_identifier.h"
14 * Shadow identifier DB element
16 struct tf_shadow_ident_element {
23 * Reference count, array of number of identifier type entries
29 * Shadow identifier DB definition
31 struct tf_shadow_ident_db {
33 * Number of elements in the DB
38 * The DB consists of an array of elements
40 struct tf_shadow_ident_element *db;
44 tf_shadow_ident_create_db(struct tf_shadow_ident_create_db_parms *parms)
48 struct tfp_calloc_parms cparms;
49 struct tf_shadow_ident_db *shadow_db;
50 struct tf_shadow_ident_element *db;
52 TF_CHECK_PARMS1(parms);
54 /* Build the shadow DB per the request */
56 cparms.size = sizeof(struct tf_shadow_ident_db);
58 rc = tfp_calloc(&cparms);
61 shadow_db = (void *)cparms.mem_va;
63 /* Build the DB within shadow DB */
64 cparms.nitems = parms->num_elements;
65 cparms.size = sizeof(struct tf_shadow_ident_element);
66 rc = tfp_calloc(&cparms);
69 shadow_db->db = (struct tf_shadow_ident_element *)cparms.mem_va;
70 shadow_db->num_entries = parms->num_elements;
73 for (i = 0; i < parms->num_elements; i++) {
74 /* If the element didn't request an allocation no need
75 * to create a pool nor verify if we got a reservation.
77 if (parms->cfg->alloc_cnt[i] == 0)
81 cparms.nitems = parms->cfg->alloc_cnt[i];
82 cparms.size = sizeof(uint32_t);
83 rc = tfp_calloc(&cparms);
86 "%s: Array alloc failed, type:%d\n",
87 tf_dir_2_str(parms->dir),
91 db[i].ref_count = (uint32_t *)cparms.mem_va;
94 *parms->tf_shadow_ident_db = (void *)shadow_db;
98 tfp_free((void *)db->ref_count);
100 tfp_free((void *)shadow_db);
101 parms->tf_shadow_ident_db = NULL;
107 tf_shadow_ident_free_db(struct tf_shadow_ident_free_db_parms *parms)
110 struct tf_shadow_ident_db *shadow_db;
112 TF_CHECK_PARMS1(parms);
114 shadow_db = (struct tf_shadow_ident_db *)parms->tf_shadow_ident_db;
115 for (i = 0; i < shadow_db->num_entries; i++)
116 tfp_free((void *)shadow_db->db[i].ref_count);
118 tfp_free((void *)shadow_db->db);
119 tfp_free((void *)parms->tf_shadow_ident_db);
125 tf_shadow_ident_search(struct tf_shadow_ident_search_parms *parms)
127 struct tf_shadow_ident_db *shadow_db;
128 uint32_t ref_cnt = 0;
130 TF_CHECK_PARMS1(parms);
132 shadow_db = (struct tf_shadow_ident_db *)parms->tf_shadow_ident_db;
133 ref_cnt = shadow_db->db[parms->type].ref_count[parms->search_id];
136 *parms->ref_cnt = ++ref_cnt;
137 shadow_db->db[parms->type].ref_count[parms->search_id] =
148 #define ID_REF_CNT_MAX 0xffffffff
150 tf_shadow_ident_insert(struct tf_shadow_ident_insert_parms *parms)
152 struct tf_shadow_ident_db *shadow_db;
154 TF_CHECK_PARMS1(parms);
156 shadow_db = (struct tf_shadow_ident_db *)parms->tf_shadow_ident_db;
158 /* In case of overflow, ref count keeps the max value */
159 if (shadow_db->db[parms->type].ref_count[parms->id] < ID_REF_CNT_MAX)
160 shadow_db->db[parms->type].ref_count[parms->id]++;
163 "Identifier %d in type %d reaches the max ref_cnt\n",
167 parms->ref_cnt = shadow_db->db[parms->type].ref_count[parms->id];
173 tf_shadow_ident_remove(struct tf_shadow_ident_remove_parms *parms)
175 struct tf_shadow_ident_db *shadow_db;
176 uint32_t ref_cnt = 0;
178 TF_CHECK_PARMS1(parms);
180 shadow_db = (struct tf_shadow_ident_db *)parms->tf_shadow_ident_db;
181 ref_cnt = shadow_db->db[parms->type].ref_count[parms->id];
183 shadow_db->db[parms->type].ref_count[parms->id]--;
187 *parms->ref_cnt = shadow_db->db[parms->type].ref_count[parms->id];