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);
79 "%s: Identifier DB creation failed\n",
83 if (parms->shadow_copy) {
84 shadow_cfg.alloc_cnt =
85 parms->resources->ident_cnt[i].cnt;
86 shadow_cdb.num_elements = parms->num_elements;
87 shadow_cdb.tf_shadow_ident_db = &ident_shadow_db[i];
88 shadow_cdb.cfg = &shadow_cfg;
89 rc = tf_shadow_ident_create_db(&shadow_cdb);
92 "%s: Ident shadow DB creation failed\n",
102 if (db_rc[TF_DIR_RX] && db_rc[TF_DIR_TX])
103 return db_rc[TF_DIR_RX];
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 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
126 "Ident_db is not initialized, rc:%s\n",
130 ident_db = (struct ident_rm_db *)ident_db_ptr;
132 for (i = 0; i < TF_DIR_MAX; i++) {
133 if (ident_db->ident_db[i] == NULL)
135 fparms.rm_db = ident_db->ident_db[i];
137 rc = tf_rm_free_db(tfp, &fparms);
140 "rm free failed on unbind\n");
143 sparms.tf_shadow_ident_db = ident_shadow_db[i];
144 rc = tf_shadow_ident_free_db(&sparms);
146 /* TODO: If there are failures on unbind we
147 * really just have to try until all DBs are
148 * attempted to be cleared.
151 ident_shadow_db[i] = NULL;
153 ident_db->ident_db[i] = NULL;
162 tf_ident_alloc(struct tf *tfp __rte_unused,
163 struct tf_ident_alloc_parms *parms)
168 struct tf_rm_allocate_parms aparms = { 0 };
169 struct tf_shadow_ident_insert_parms iparms = { 0 };
170 struct ident_rm_db *ident_db;
171 void *ident_db_ptr = NULL;
173 TF_CHECK_PARMS2(tfp, parms);
175 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
178 "Failed to get ident_db from session, rc:%s\n",
182 ident_db = (struct ident_rm_db *)ident_db_ptr;
184 aparms.rm_db = ident_db->ident_db[parms->dir];
185 aparms.subtype = parms->type;
187 aparms.base_index = &base_id;
188 rc = tf_rm_allocate(&aparms);
191 "%s: Failed allocate, type:%d\n",
192 tf_dir_2_str(parms->dir),
198 iparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
199 iparms.type = parms->type;
202 rc = tf_shadow_ident_insert(&iparms);
205 "%s: Failed insert shadow DB, type:%d\n",
206 tf_dir_2_str(parms->dir),
218 tf_ident_free(struct tf *tfp __rte_unused,
219 struct tf_ident_free_parms *parms)
222 struct tf_rm_is_allocated_parms aparms = { 0 };
223 struct tf_rm_free_parms fparms = { 0 };
224 struct tf_shadow_ident_remove_parms rparms = { 0 };
227 struct ident_rm_db *ident_db;
228 void *ident_db_ptr = NULL;
230 TF_CHECK_PARMS2(tfp, parms);
232 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
235 "Failed to get ident_db from session, rc:%s\n",
239 ident_db = (struct ident_rm_db *)ident_db_ptr;
241 /* Check if element is in use */
242 aparms.rm_db = ident_db->ident_db[parms->dir];
243 aparms.subtype = parms->type;
244 aparms.index = parms->id;
245 aparms.base_index = &base_id;
246 aparms.allocated = &allocated;
247 rc = tf_rm_is_allocated(&aparms);
251 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
253 "%s: Entry already free, type:%d, index:%d\n",
254 tf_dir_2_str(parms->dir),
261 rparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
262 rparms.type = parms->type;
264 rparms.ref_cnt = parms->ref_cnt;
266 rc = tf_shadow_ident_remove(&rparms);
269 "%s: ref_cnt was 0 in shadow DB,"
270 " type:%d, index:%d\n",
271 tf_dir_2_str(parms->dir),
277 if (*rparms.ref_cnt > 0)
281 /* Free requested element */
282 fparms.rm_db = ident_db->ident_db[parms->dir];
283 fparms.subtype = parms->type;
284 fparms.index = parms->id;
285 rc = tf_rm_free(&fparms);
288 "%s: Free failed, type:%d, index:%d\n",
289 tf_dir_2_str(parms->dir),
299 tf_ident_search(struct tf *tfp __rte_unused,
300 struct tf_ident_search_parms *parms)
303 struct tf_rm_is_allocated_parms aparms = { 0 };
304 struct tf_shadow_ident_search_parms sparms = { 0 };
307 struct ident_rm_db *ident_db;
308 void *ident_db_ptr = NULL;
310 TF_CHECK_PARMS2(tfp, parms);
314 "%s: Identifier Shadow copy is not enabled\n",
315 tf_dir_2_str(parms->dir));
319 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
322 "Failed to get ident_db from session, rc:%s\n",
326 ident_db = (struct ident_rm_db *)ident_db_ptr;
328 /* Check if element is in use */
329 aparms.rm_db = ident_db->ident_db[parms->dir];
330 aparms.subtype = parms->type;
331 aparms.index = parms->search_id;
332 aparms.base_index = &base_id;
333 aparms.allocated = &allocated;
334 rc = tf_rm_is_allocated(&aparms);
338 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
340 "%s: Entry not allocated, type:%d, index:%d\n",
341 tf_dir_2_str(parms->dir),
347 sparms.tf_shadow_ident_db = ident_shadow_db[parms->dir];
348 sparms.type = parms->type;
349 sparms.search_id = base_id;
350 sparms.hit = parms->hit;
351 sparms.ref_cnt = parms->ref_cnt;
353 rc = tf_shadow_ident_search(&sparms);
356 "%s: Failed search shadow DB, type:%d\n",
357 tf_dir_2_str(parms->dir),
366 tf_ident_get_resc_info(struct tf *tfp,
367 struct tf_identifier_resource_info *ident)
371 struct tf_resource_info *dinfo;
372 struct tf_rm_get_alloc_info_parms ainfo;
373 void *ident_db_ptr = NULL;
374 struct ident_rm_db *ident_db;
376 TF_CHECK_PARMS2(tfp, ident);
378 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_IDENTIFIER, &ident_db_ptr);
381 "No resource allocated for ident from session\n");
384 ident_db = (struct ident_rm_db *)ident_db_ptr;
386 /* check if reserved resource for WC is multiple of num_slices */
387 for (d = 0; d < TF_DIR_MAX; d++) {
388 ainfo.rm_db = ident_db->ident_db[d];
389 dinfo = ident[d].info;
391 ainfo.info = (struct tf_rm_alloc_info *)dinfo;
393 rc = tf_rm_get_all_info(&ainfo, TF_IDENT_TYPE_MAX);