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)
33 int db_rc[TF_DIR_MAX] = { 0 };
35 struct tf_rm_create_db_parms db_cfg = { 0 };
36 struct tf_shadow_ident_cfg_parms shadow_cfg = { 0 };
37 struct tf_shadow_ident_create_db_parms shadow_cdb = { 0 };
38 struct ident_rm_db *ident_db;
39 struct tfp_calloc_parms cparms;
40 struct tf_session *tfs;
42 TF_CHECK_PARMS2(tfp, parms);
44 /* Retrieve the session information */
45 rc = tf_session_get_session_internal(tfp, &tfs);
49 memset(&db_cfg, 0, sizeof(db_cfg));
51 cparms.size = sizeof(struct ident_rm_db);
53 if (tfp_calloc(&cparms) != 0) {
54 TFP_DRV_LOG(ERR, "ident_rm_db alloc error %s\n",
59 ident_db = cparms.mem_va;
60 for (i = 0; i < TF_DIR_MAX; i++)
61 ident_db->ident_db[i] = NULL;
62 tf_session_set_db(tfp, TF_MODULE_TYPE_IDENTIFIER, ident_db);
64 db_cfg.module = TF_MODULE_TYPE_IDENTIFIER;
65 db_cfg.num_elements = parms->num_elements;
66 db_cfg.cfg = parms->cfg;
68 for (i = 0; i < TF_DIR_MAX; i++) {
69 db_cfg.rm_db = (void *)&ident_db->ident_db[i];
71 db_cfg.alloc_cnt = parms->resources->ident_cnt[i].cnt;
72 if (tf_session_is_shared_session(tfs) &&
73 (!tf_session_is_shared_session_creator(tfs)))
74 db_rc[i] = tf_rm_create_db_no_reservation(tfp, &db_cfg);
76 db_rc[i] = tf_rm_create_db(tfp, &db_cfg);
78 if (parms->shadow_copy) {
79 shadow_cfg.alloc_cnt =
80 parms->resources->ident_cnt[i].cnt;
81 shadow_cdb.num_elements = parms->num_elements;
82 shadow_cdb.tf_shadow_ident_db = &ident_shadow_db[i];
83 shadow_cdb.cfg = &shadow_cfg;
84 rc = tf_shadow_ident_create_db(&shadow_cdb);
87 "%s: Ident shadow DB creation failed\n",
97 if (db_rc[TF_DIR_RX] && db_rc[TF_DIR_TX]) {
98 TFP_DRV_LOG(ERR, "No Identifier DB created\n");
99 return db_rc[TF_DIR_RX];
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 = (struct ident_rm_db *)ident_db_ptr;
125 for (i = 0; i < TF_DIR_MAX; i++) {
126 if (ident_db->ident_db[i] == NULL)
128 fparms.rm_db = ident_db->ident_db[i];
130 rc = tf_rm_free_db(tfp, &fparms);
133 "rm free failed on unbind\n");
136 sparms.tf_shadow_ident_db = ident_shadow_db[i];
137 rc = tf_shadow_ident_free_db(&sparms);
139 /* TODO: If there are failures on unbind we
140 * really just have to try until all DBs are
141 * attempted to be cleared.
144 ident_shadow_db[i] = NULL;
146 ident_db->ident_db[i] = NULL;
155 tf_ident_alloc(struct tf *tfp __rte_unused,
156 struct tf_ident_alloc_parms *parms)
161 struct tf_rm_allocate_parms aparms = { 0 };
162 struct tf_shadow_ident_insert_parms iparms = { 0 };
163 struct ident_rm_db *ident_db;
164 void *ident_db_ptr = NULL;
166 TF_CHECK_PARMS2(tfp, parms);
168 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
171 "Failed to get ident_db from session, rc:%s\n",
175 ident_db = (struct ident_rm_db *)ident_db_ptr;
177 aparms.rm_db = ident_db->ident_db[parms->dir];
178 aparms.subtype = parms->type;
180 aparms.base_index = &base_id;
181 rc = tf_rm_allocate(&aparms);
184 "%s: Failed allocate, type:%d\n",
185 tf_dir_2_str(parms->dir),
191 iparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
192 iparms.type = parms->type;
195 rc = tf_shadow_ident_insert(&iparms);
198 "%s: Failed insert shadow DB, type:%d\n",
199 tf_dir_2_str(parms->dir),
211 tf_ident_free(struct tf *tfp __rte_unused,
212 struct tf_ident_free_parms *parms)
215 struct tf_rm_is_allocated_parms aparms = { 0 };
216 struct tf_rm_free_parms fparms = { 0 };
217 struct tf_shadow_ident_remove_parms rparms = { 0 };
220 struct ident_rm_db *ident_db;
221 void *ident_db_ptr = NULL;
223 TF_CHECK_PARMS2(tfp, parms);
225 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
228 "Failed to get ident_db from session, rc:%s\n",
232 ident_db = (struct ident_rm_db *)ident_db_ptr;
234 /* Check if element is in use */
235 aparms.rm_db = ident_db->ident_db[parms->dir];
236 aparms.subtype = parms->type;
237 aparms.index = parms->id;
238 aparms.base_index = &base_id;
239 aparms.allocated = &allocated;
240 rc = tf_rm_is_allocated(&aparms);
244 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
246 "%s: Entry already free, type:%d, index:%d\n",
247 tf_dir_2_str(parms->dir),
254 rparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
255 rparms.type = parms->type;
257 rparms.ref_cnt = parms->ref_cnt;
259 rc = tf_shadow_ident_remove(&rparms);
262 "%s: ref_cnt was 0 in shadow DB,"
263 " type:%d, index:%d\n",
264 tf_dir_2_str(parms->dir),
270 if (*rparms.ref_cnt > 0)
274 /* Free requested element */
275 fparms.rm_db = ident_db->ident_db[parms->dir];
276 fparms.subtype = parms->type;
277 fparms.index = parms->id;
278 rc = tf_rm_free(&fparms);
281 "%s: Free failed, type:%d, index:%d\n",
282 tf_dir_2_str(parms->dir),
292 tf_ident_search(struct tf *tfp __rte_unused,
293 struct tf_ident_search_parms *parms)
296 struct tf_rm_is_allocated_parms aparms = { 0 };
297 struct tf_shadow_ident_search_parms sparms = { 0 };
300 struct ident_rm_db *ident_db;
301 void *ident_db_ptr = NULL;
303 TF_CHECK_PARMS2(tfp, parms);
307 "%s: Identifier Shadow copy is not enabled\n",
308 tf_dir_2_str(parms->dir));
312 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
315 "Failed to get ident_db from session, rc:%s\n",
319 ident_db = (struct ident_rm_db *)ident_db_ptr;
321 /* Check if element is in use */
322 aparms.rm_db = ident_db->ident_db[parms->dir];
323 aparms.subtype = parms->type;
324 aparms.index = parms->search_id;
325 aparms.base_index = &base_id;
326 aparms.allocated = &allocated;
327 rc = tf_rm_is_allocated(&aparms);
331 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
333 "%s: Entry not allocated, type:%d, index:%d\n",
334 tf_dir_2_str(parms->dir),
340 sparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
341 sparms.type = parms->type;
342 sparms.search_id = base_id;
343 sparms.hit = parms->hit;
344 sparms.ref_cnt = parms->ref_cnt;
346 rc = tf_shadow_ident_search(&sparms);
349 "%s: Failed search shadow DB, type:%d\n",
350 tf_dir_2_str(parms->dir),
359 tf_ident_get_resc_info(struct tf *tfp,
360 struct tf_identifier_resource_info *ident)
364 struct tf_resource_info *dinfo;
365 struct tf_rm_get_alloc_info_parms ainfo;
366 void *ident_db_ptr = NULL;
367 struct ident_rm_db *ident_db;
369 TF_CHECK_PARMS2(tfp, ident);
371 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
373 return 0; /* db doesn't exist */
375 return rc; /* error getting db */
377 ident_db = (struct ident_rm_db *)ident_db_ptr;
379 /* check if reserved resource for WC is multiple of num_slices */
380 for (d = 0; d < TF_DIR_MAX; d++) {
381 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);