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 * Init flag, set on bind and cleared on unbind
24 * Identifier shadow DBs.
26 static void *ident_shadow_db[TF_DIR_MAX];
29 * Shadow DB Init flag, set on bind and cleared on unbind
31 static uint8_t shadow_init;
34 tf_ident_bind(struct tf *tfp,
35 struct tf_ident_cfg_parms *parms)
39 struct tf_rm_create_db_parms db_cfg = { 0 };
40 struct tf_shadow_ident_cfg_parms shadow_cfg = { 0 };
41 struct tf_shadow_ident_create_db_parms shadow_cdb = { 0 };
42 struct ident_rm_db *ident_db;
43 struct tfp_calloc_parms cparms;
45 TF_CHECK_PARMS2(tfp, parms);
49 "Identifier DB already initialized\n");
53 memset(&db_cfg, 0, sizeof(db_cfg));
55 cparms.size = sizeof(struct ident_rm_db);
57 if (tfp_calloc(&cparms) != 0) {
58 TFP_DRV_LOG(ERR, "ident_rm_db alloc error %s\n",
63 ident_db = cparms.mem_va;
64 for (i = 0; i < TF_DIR_MAX; i++)
65 ident_db->ident_db[i] = NULL;
66 tf_session_set_db(tfp, TF_MODULE_TYPE_IDENTIFIER, ident_db);
68 db_cfg.module = TF_MODULE_TYPE_IDENTIFIER;
69 db_cfg.num_elements = parms->num_elements;
70 db_cfg.cfg = parms->cfg;
72 for (i = 0; i < TF_DIR_MAX; i++) {
73 db_cfg.rm_db = (void *)&ident_db->ident_db[i];
75 db_cfg.alloc_cnt = parms->resources->ident_cnt[i].cnt;
76 rc = tf_rm_create_db(tfp, &db_cfg);
79 "%s: Identifier DB creation failed\n",
85 if (parms->shadow_copy) {
86 shadow_cfg.alloc_cnt =
87 parms->resources->ident_cnt[i].cnt;
88 shadow_cdb.num_elements = parms->num_elements;
89 shadow_cdb.tf_shadow_ident_db = &ident_shadow_db[i];
90 shadow_cdb.cfg = &shadow_cfg;
91 rc = tf_shadow_ident_create_db(&shadow_cdb);
94 "%s: Ident shadow DB creation failed\n",
106 "Identifier - initialized\n");
112 tf_ident_unbind(struct tf *tfp)
116 struct tf_rm_free_db_parms fparms = { 0 };
117 struct tf_shadow_ident_free_db_parms sparms = { 0 };
118 struct ident_rm_db *ident_db;
119 void *ident_db_ptr = NULL;
121 TF_CHECK_PARMS1(tfp);
123 /* Bail if nothing has been initialized */
126 "No Identifier DBs created\n");
130 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
133 "Failed to get ident_db from session, rc:%s\n",
137 ident_db = (struct ident_rm_db *)ident_db_ptr;
139 for (i = 0; i < TF_DIR_MAX; i++) {
140 fparms.rm_db = ident_db->ident_db[i];
142 rc = tf_rm_free_db(tfp, &fparms);
145 "rm free failed on unbind\n");
148 sparms.tf_shadow_ident_db = ident_shadow_db[i];
149 rc = tf_shadow_ident_free_db(&sparms);
151 /* TODO: If there are failures on unbind we
152 * really just have to try until all DBs are
153 * attempted to be cleared.
156 ident_shadow_db[i] = NULL;
158 ident_db->ident_db[i] = NULL;
168 tf_ident_alloc(struct tf *tfp __rte_unused,
169 struct tf_ident_alloc_parms *parms)
174 struct tf_rm_allocate_parms aparms = { 0 };
175 struct tf_shadow_ident_insert_parms iparms = { 0 };
176 struct ident_rm_db *ident_db;
177 void *ident_db_ptr = NULL;
179 TF_CHECK_PARMS2(tfp, parms);
183 "%s: No Identifier DBs created\n",
184 tf_dir_2_str(parms->dir));
188 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
191 "Failed to get ident_db from session, rc:%s\n",
195 ident_db = (struct ident_rm_db *)ident_db_ptr;
197 aparms.rm_db = ident_db->ident_db[parms->dir];
198 aparms.subtype = parms->type;
200 aparms.base_index = &base_id;
201 rc = tf_rm_allocate(&aparms);
204 "%s: Failed allocate, type:%d\n",
205 tf_dir_2_str(parms->dir),
211 iparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
212 iparms.type = parms->type;
215 rc = tf_shadow_ident_insert(&iparms);
218 "%s: Failed insert shadow DB, type:%d\n",
219 tf_dir_2_str(parms->dir),
231 tf_ident_free(struct tf *tfp __rte_unused,
232 struct tf_ident_free_parms *parms)
235 struct tf_rm_is_allocated_parms aparms = { 0 };
236 struct tf_rm_free_parms fparms = { 0 };
237 struct tf_shadow_ident_remove_parms rparms = { 0 };
240 struct ident_rm_db *ident_db;
241 void *ident_db_ptr = NULL;
243 TF_CHECK_PARMS2(tfp, parms);
247 "%s: No Identifier DBs created\n",
248 tf_dir_2_str(parms->dir));
252 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
255 "Failed to get ident_db from session, rc:%s\n",
259 ident_db = (struct ident_rm_db *)ident_db_ptr;
261 /* Check if element is in use */
262 aparms.rm_db = ident_db->ident_db[parms->dir];
263 aparms.subtype = parms->type;
264 aparms.index = parms->id;
265 aparms.base_index = &base_id;
266 aparms.allocated = &allocated;
267 rc = tf_rm_is_allocated(&aparms);
271 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
273 "%s: Entry already free, type:%d, index:%d\n",
274 tf_dir_2_str(parms->dir),
281 rparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
282 rparms.type = parms->type;
284 rparms.ref_cnt = parms->ref_cnt;
286 rc = tf_shadow_ident_remove(&rparms);
289 "%s: ref_cnt was 0 in shadow DB,"
290 " type:%d, index:%d\n",
291 tf_dir_2_str(parms->dir),
297 if (*rparms.ref_cnt > 0)
301 /* Free requested element */
302 fparms.rm_db = ident_db->ident_db[parms->dir];
303 fparms.subtype = parms->type;
304 fparms.index = parms->id;
305 rc = tf_rm_free(&fparms);
308 "%s: Free failed, type:%d, index:%d\n",
309 tf_dir_2_str(parms->dir),
319 tf_ident_search(struct tf *tfp __rte_unused,
320 struct tf_ident_search_parms *parms)
323 struct tf_rm_is_allocated_parms aparms = { 0 };
324 struct tf_shadow_ident_search_parms sparms = { 0 };
327 struct ident_rm_db *ident_db;
328 void *ident_db_ptr = NULL;
330 TF_CHECK_PARMS2(tfp, parms);
334 "%s: No Identifier DBs created\n",
335 tf_dir_2_str(parms->dir));
341 "%s: Identifier Shadow copy is not enabled\n",
342 tf_dir_2_str(parms->dir));
346 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
349 "Failed to get ident_db from session, rc:%s\n",
353 ident_db = (struct ident_rm_db *)ident_db_ptr;
355 /* Check if element is in use */
356 aparms.rm_db = ident_db->ident_db[parms->dir];
357 aparms.subtype = parms->type;
358 aparms.index = parms->search_id;
359 aparms.base_index = &base_id;
360 aparms.allocated = &allocated;
361 rc = tf_rm_is_allocated(&aparms);
365 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
367 "%s: Entry not allocated, type:%d, index:%d\n",
368 tf_dir_2_str(parms->dir),
374 sparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
375 sparms.type = parms->type;
376 sparms.search_id = base_id;
377 sparms.hit = parms->hit;
378 sparms.ref_cnt = parms->ref_cnt;
380 rc = tf_shadow_ident_search(&sparms);
383 "%s: Failed search shadow DB, type:%d\n",
384 tf_dir_2_str(parms->dir),