1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
6 #include <rte_common.h>
8 #include "tf_identifier.h"
9 #include "tf_shadow_identifier.h"
10 #include "tf_common.h"
14 #include "tf_session.h"
19 * Identifier shadow DBs.
21 static void *ident_shadow_db[TF_DIR_MAX];
24 * Shadow DB Init flag, set on bind and cleared on unbind
26 static uint8_t shadow_init;
29 tf_ident_bind(struct tf *tfp,
30 struct tf_ident_cfg_parms *parms)
34 struct tf_rm_create_db_parms db_cfg = { 0 };
35 struct tf_shadow_ident_cfg_parms shadow_cfg = { 0 };
36 struct tf_shadow_ident_create_db_parms shadow_cdb = { 0 };
37 struct ident_rm_db *ident_db;
38 struct tfp_calloc_parms cparms;
39 struct tf_session *tfs;
41 TF_CHECK_PARMS2(tfp, parms);
43 /* Retrieve the session information */
44 rc = tf_session_get_session_internal(tfp, &tfs);
48 memset(&db_cfg, 0, sizeof(db_cfg));
50 cparms.size = sizeof(struct ident_rm_db);
52 if (tfp_calloc(&cparms) != 0) {
53 TFP_DRV_LOG(ERR, "ident_rm_db alloc error %s\n",
58 ident_db = cparms.mem_va;
59 for (i = 0; i < TF_DIR_MAX; i++)
60 ident_db->ident_db[i] = NULL;
61 tf_session_set_db(tfp, TF_MODULE_TYPE_IDENTIFIER, ident_db);
63 db_cfg.module = TF_MODULE_TYPE_IDENTIFIER;
64 db_cfg.num_elements = parms->num_elements;
65 db_cfg.cfg = parms->cfg;
67 for (i = 0; i < TF_DIR_MAX; i++) {
68 db_cfg.rm_db = (void *)&ident_db->ident_db[i];
70 db_cfg.alloc_cnt = parms->resources->ident_cnt[i].cnt;
71 if (tf_session_is_shared_session(tfs) &&
72 (!tf_session_is_shared_session_creator(tfs)))
73 rc = tf_rm_create_db_no_reservation(tfp, &db_cfg);
75 rc = tf_rm_create_db(tfp, &db_cfg);
78 "%s: Identifier DB creation failed\n",
84 if (parms->shadow_copy) {
85 shadow_cfg.alloc_cnt =
86 parms->resources->ident_cnt[i].cnt;
87 shadow_cdb.num_elements = parms->num_elements;
88 shadow_cdb.tf_shadow_ident_db = &ident_shadow_db[i];
89 shadow_cdb.cfg = &shadow_cfg;
90 rc = tf_shadow_ident_create_db(&shadow_cdb);
93 "%s: Ident shadow DB creation failed\n",
103 "Identifier - initialized\n");
109 tf_ident_unbind(struct tf *tfp)
113 struct tf_rm_free_db_parms fparms = { 0 };
114 struct tf_shadow_ident_free_db_parms sparms = { 0 };
115 struct ident_rm_db *ident_db;
116 void *ident_db_ptr = NULL;
118 TF_CHECK_PARMS1(tfp);
120 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
123 "Ident_db is not initialized, rc:%s\n",
127 ident_db = (struct ident_rm_db *)ident_db_ptr;
129 for (i = 0; i < TF_DIR_MAX; i++) {
130 if (ident_db->ident_db[i] == NULL)
132 fparms.rm_db = ident_db->ident_db[i];
134 rc = tf_rm_free_db(tfp, &fparms);
137 "rm free failed on unbind\n");
140 sparms.tf_shadow_ident_db = ident_shadow_db[i];
141 rc = tf_shadow_ident_free_db(&sparms);
143 /* TODO: If there are failures on unbind we
144 * really just have to try until all DBs are
145 * attempted to be cleared.
148 ident_shadow_db[i] = NULL;
150 ident_db->ident_db[i] = NULL;
159 tf_ident_alloc(struct tf *tfp __rte_unused,
160 struct tf_ident_alloc_parms *parms)
165 struct tf_rm_allocate_parms aparms = { 0 };
166 struct tf_shadow_ident_insert_parms iparms = { 0 };
167 struct ident_rm_db *ident_db;
168 void *ident_db_ptr = NULL;
170 TF_CHECK_PARMS2(tfp, parms);
172 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
175 "Failed to get ident_db from session, rc:%s\n",
179 ident_db = (struct ident_rm_db *)ident_db_ptr;
181 aparms.rm_db = ident_db->ident_db[parms->dir];
182 aparms.subtype = parms->type;
184 aparms.base_index = &base_id;
185 rc = tf_rm_allocate(&aparms);
188 "%s: Failed allocate, type:%d\n",
189 tf_dir_2_str(parms->dir),
195 iparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
196 iparms.type = parms->type;
199 rc = tf_shadow_ident_insert(&iparms);
202 "%s: Failed insert shadow DB, type:%d\n",
203 tf_dir_2_str(parms->dir),
215 tf_ident_free(struct tf *tfp __rte_unused,
216 struct tf_ident_free_parms *parms)
219 struct tf_rm_is_allocated_parms aparms = { 0 };
220 struct tf_rm_free_parms fparms = { 0 };
221 struct tf_shadow_ident_remove_parms rparms = { 0 };
224 struct ident_rm_db *ident_db;
225 void *ident_db_ptr = NULL;
227 TF_CHECK_PARMS2(tfp, parms);
229 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
232 "Failed to get ident_db from session, rc:%s\n",
236 ident_db = (struct ident_rm_db *)ident_db_ptr;
238 /* Check if element is in use */
239 aparms.rm_db = ident_db->ident_db[parms->dir];
240 aparms.subtype = parms->type;
241 aparms.index = parms->id;
242 aparms.base_index = &base_id;
243 aparms.allocated = &allocated;
244 rc = tf_rm_is_allocated(&aparms);
248 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
250 "%s: Entry already free, type:%d, index:%d\n",
251 tf_dir_2_str(parms->dir),
258 rparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
259 rparms.type = parms->type;
261 rparms.ref_cnt = parms->ref_cnt;
263 rc = tf_shadow_ident_remove(&rparms);
266 "%s: ref_cnt was 0 in shadow DB,"
267 " type:%d, index:%d\n",
268 tf_dir_2_str(parms->dir),
274 if (*rparms.ref_cnt > 0)
278 /* Free requested element */
279 fparms.rm_db = ident_db->ident_db[parms->dir];
280 fparms.subtype = parms->type;
281 fparms.index = parms->id;
282 rc = tf_rm_free(&fparms);
285 "%s: Free failed, type:%d, index:%d\n",
286 tf_dir_2_str(parms->dir),
296 tf_ident_search(struct tf *tfp __rte_unused,
297 struct tf_ident_search_parms *parms)
300 struct tf_rm_is_allocated_parms aparms = { 0 };
301 struct tf_shadow_ident_search_parms sparms = { 0 };
304 struct ident_rm_db *ident_db;
305 void *ident_db_ptr = NULL;
307 TF_CHECK_PARMS2(tfp, parms);
311 "%s: Identifier Shadow copy is not enabled\n",
312 tf_dir_2_str(parms->dir));
316 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
319 "Failed to get ident_db from session, rc:%s\n",
323 ident_db = (struct ident_rm_db *)ident_db_ptr;
325 /* Check if element is in use */
326 aparms.rm_db = ident_db->ident_db[parms->dir];
327 aparms.subtype = parms->type;
328 aparms.index = parms->search_id;
329 aparms.base_index = &base_id;
330 aparms.allocated = &allocated;
331 rc = tf_rm_is_allocated(&aparms);
335 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
337 "%s: Entry not allocated, type:%d, index:%d\n",
338 tf_dir_2_str(parms->dir),
344 sparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
345 sparms.type = parms->type;
346 sparms.search_id = base_id;
347 sparms.hit = parms->hit;
348 sparms.ref_cnt = parms->ref_cnt;
350 rc = tf_shadow_ident_search(&sparms);
353 "%s: Failed search shadow DB, type:%d\n",
354 tf_dir_2_str(parms->dir),
363 tf_ident_get_resc_info(struct tf *tfp,
364 struct tf_identifier_resource_info *ident)
368 struct tf_resource_info *dinfo;
369 struct tf_rm_get_alloc_info_parms ainfo;
370 void *ident_db_ptr = NULL;
371 struct ident_rm_db *ident_db;
373 TF_CHECK_PARMS2(tfp, ident);
375 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
378 "No resource allocated for ident from session\n");
381 ident_db = (struct ident_rm_db *)ident_db_ptr;
383 /* check if reserved resource for WC is multiple of num_slices */
384 for (d = 0; d < TF_DIR_MAX; d++) {
385 ainfo.rm_db = ident_db->ident_db[d];
386 dinfo = ident[d].info;
388 ainfo.info = (struct tf_rm_alloc_info *)dinfo;
390 rc = tf_rm_get_all_info(&ainfo, TF_IDENT_TYPE_MAX);