1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
6 /* Truflow Table APIs and supporting code */
8 #include <rte_common.h>
11 #include "tf_common.h"
16 #include "tf_session.h"
17 #include "tf_device.h"
21 #define TF_TBL_RM_TO_PTR(new_idx, idx, base, shift) { \
22 *(new_idx) = (((idx) + (base)) << (shift)); \
26 tf_tbl_bind(struct tf *tfp,
27 struct tf_tbl_cfg_parms *parms)
30 int db_rc[TF_DIR_MAX] = { 0 };
31 struct tf_rm_create_db_parms db_cfg = { 0 };
32 struct tbl_rm_db *tbl_db;
33 struct tfp_calloc_parms cparms;
34 struct tf_session *tfs;
36 TF_CHECK_PARMS2(tfp, parms);
38 /* Retrieve the session information */
39 rc = tf_session_get_session_internal(tfp, &tfs);
43 memset(&db_cfg, 0, sizeof(db_cfg));
45 cparms.size = sizeof(struct tbl_rm_db);
47 if (tfp_calloc(&cparms) != 0) {
48 TFP_DRV_LOG(ERR, "tbl_rm_db alloc error %s\n",
53 tbl_db = cparms.mem_va;
54 for (i = 0; i < TF_DIR_MAX; i++)
55 tbl_db->tbl_db[i] = NULL;
56 tf_session_set_db(tfp, TF_MODULE_TYPE_TABLE, tbl_db);
58 db_cfg.num_elements = parms->num_elements;
59 db_cfg.module = TF_MODULE_TYPE_TABLE;
60 db_cfg.num_elements = parms->num_elements;
62 for (d = 0; d < TF_DIR_MAX; d++) {
64 db_cfg.cfg = &parms->cfg[d ? TF_TBL_TYPE_MAX : 0];
65 db_cfg.alloc_cnt = parms->resources->tbl_cnt[d].cnt;
66 db_cfg.rm_db = (void *)&tbl_db->tbl_db[d];
67 if (tf_session_is_shared_session(tfs) &&
68 (!tf_session_is_shared_session_creator(tfs)))
69 db_rc[d] = tf_rm_create_db_no_reservation(tfp, &db_cfg);
71 db_rc[d] = tf_rm_create_db(tfp, &db_cfg);
75 if (db_rc[TF_DIR_RX] && db_rc[TF_DIR_TX]) {
77 "%s: No Table DB created\n",
79 return db_rc[TF_DIR_RX];
83 "Table Type - initialized\n");
89 tf_tbl_unbind(struct tf *tfp)
93 struct tf_rm_free_db_parms fparms = { 0 };
94 struct tbl_rm_db *tbl_db;
95 void *tbl_db_ptr = NULL;
98 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
101 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
103 for (i = 0; i < TF_DIR_MAX; i++) {
104 if (tbl_db->tbl_db[i] == NULL)
107 fparms.rm_db = tbl_db->tbl_db[i];
108 rc = tf_rm_free_db(tfp, &fparms);
112 tbl_db->tbl_db[i] = NULL;
119 tf_tbl_alloc(struct tf *tfp __rte_unused,
120 struct tf_tbl_alloc_parms *parms)
124 struct tf_rm_allocate_parms aparms = { 0 };
125 struct tf_session *tfs;
126 struct tf_dev_info *dev;
127 struct tbl_rm_db *tbl_db;
128 void *tbl_db_ptr = NULL;
130 TF_CHECK_PARMS2(tfp, parms);
132 /* Retrieve the session information */
133 rc = tf_session_get_session_internal(tfp, &tfs);
137 /* Retrieve the device information */
138 rc = tf_session_get_device(tfs, &dev);
142 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
145 "Failed to get tbl_db from session, rc:%s\n",
149 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
151 /* Allocate requested element */
152 aparms.rm_db = tbl_db->tbl_db[parms->dir];
153 aparms.subtype = parms->type;
155 rc = tf_rm_allocate(&aparms);
158 "%s: Failed allocate, type:%s\n",
159 tf_dir_2_str(parms->dir),
160 tf_tbl_type_2_str(parms->type));
170 tf_tbl_free(struct tf *tfp __rte_unused,
171 struct tf_tbl_free_parms *parms)
174 struct tf_rm_is_allocated_parms aparms = { 0 };
175 struct tf_rm_free_parms fparms = { 0 };
177 struct tf_session *tfs;
178 struct tf_dev_info *dev;
179 struct tbl_rm_db *tbl_db;
180 void *tbl_db_ptr = NULL;
182 TF_CHECK_PARMS2(tfp, parms);
184 /* Retrieve the session information */
185 rc = tf_session_get_session_internal(tfp, &tfs);
189 /* Retrieve the device information */
190 rc = tf_session_get_device(tfs, &dev);
194 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
197 "Failed to get em_ext_db from session, rc:%s\n",
201 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
203 /* Check if element is in use */
204 aparms.rm_db = tbl_db->tbl_db[parms->dir];
205 aparms.subtype = parms->type;
206 aparms.index = parms->idx;
207 aparms.allocated = &allocated;
208 rc = tf_rm_is_allocated(&aparms);
212 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
214 "%s: Entry already free, type:%s, index:%d\n",
215 tf_dir_2_str(parms->dir),
216 tf_tbl_type_2_str(parms->type),
221 /* If this is counter table, clear the entry on free */
222 if (parms->type == TF_TBL_TYPE_ACT_STATS_64) {
223 uint8_t data[8] = { 0 };
224 uint16_t hcapi_type = 0;
225 struct tf_rm_get_hcapi_parms hparms = { 0 };
227 /* Get the hcapi type */
228 hparms.rm_db = tbl_db->tbl_db[parms->dir];
229 hparms.subtype = parms->type;
230 hparms.hcapi_type = &hcapi_type;
231 rc = tf_rm_get_hcapi_type(&hparms);
234 "%s, Failed type lookup, type:%s, rc:%s\n",
235 tf_dir_2_str(parms->dir),
236 tf_tbl_type_2_str(parms->type),
242 rc = tf_msg_set_tbl_entry(tfp,
250 "%s, Set failed, type:%s, rc:%s\n",
251 tf_dir_2_str(parms->dir),
252 tf_tbl_type_2_str(parms->type),
258 /* Free requested element */
259 fparms.rm_db = tbl_db->tbl_db[parms->dir];
260 fparms.subtype = parms->type;
261 fparms.index = parms->idx;
262 rc = tf_rm_free(&fparms);
265 "%s: Free failed, type:%s, index:%d\n",
266 tf_dir_2_str(parms->dir),
267 tf_tbl_type_2_str(parms->type),
276 tf_tbl_set(struct tf *tfp,
277 struct tf_tbl_set_parms *parms)
282 struct tf_rm_is_allocated_parms aparms = { 0 };
283 struct tf_rm_get_hcapi_parms hparms = { 0 };
284 struct tf_session *tfs;
285 struct tf_dev_info *dev;
286 struct tbl_rm_db *tbl_db;
287 void *tbl_db_ptr = NULL;
289 TF_CHECK_PARMS3(tfp, parms, parms->data);
291 /* Retrieve the session information */
292 rc = tf_session_get_session_internal(tfp, &tfs);
296 /* Retrieve the device information */
297 rc = tf_session_get_device(tfs, &dev);
301 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
304 "Failed to get em_ext_db from session, rc:%s\n",
308 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
310 /* Verify that the entry has been previously allocated.
311 * for meter drop counter, check the corresponding meter
314 aparms.rm_db = tbl_db->tbl_db[parms->dir];
315 if (parms->type != TF_TBL_TYPE_METER_DROP_CNT)
316 aparms.subtype = parms->type;
318 aparms.subtype = TF_TBL_TYPE_METER_INST;
319 aparms.allocated = &allocated;
320 aparms.index = parms->idx;
321 rc = tf_rm_is_allocated(&aparms);
325 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
327 "%s, Invalid or not allocated, type:%s, idx:%d\n",
328 tf_dir_2_str(parms->dir),
329 tf_tbl_type_2_str(parms->type),
335 hparms.rm_db = tbl_db->tbl_db[parms->dir];
336 hparms.subtype = parms->type;
337 hparms.hcapi_type = &hcapi_type;
338 rc = tf_rm_get_hcapi_type(&hparms);
341 "%s, Failed type lookup, type:%s, rc:%s\n",
342 tf_dir_2_str(parms->dir),
343 tf_tbl_type_2_str(parms->type),
348 rc = tf_msg_set_tbl_entry(tfp,
351 parms->data_sz_in_bytes,
356 "%s, Set failed, type:%s, rc:%s\n",
357 tf_dir_2_str(parms->dir),
358 tf_tbl_type_2_str(parms->type),
367 tf_tbl_get(struct tf *tfp,
368 struct tf_tbl_get_parms *parms)
373 struct tf_rm_is_allocated_parms aparms = { 0 };
374 struct tf_rm_get_hcapi_parms hparms = { 0 };
375 struct tf_session *tfs;
376 struct tf_dev_info *dev;
377 struct tbl_rm_db *tbl_db;
378 void *tbl_db_ptr = NULL;
380 TF_CHECK_PARMS3(tfp, parms, parms->data);
382 /* Retrieve the session information */
383 rc = tf_session_get_session_internal(tfp, &tfs);
387 /* Retrieve the device information */
388 rc = tf_session_get_device(tfs, &dev);
392 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
395 "Failed to get em_ext_db from session, rc:%s\n",
399 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
401 /* Verify that the entry has been previously allocated.
402 * for meter drop counter, check the corresponding meter
405 aparms.rm_db = tbl_db->tbl_db[parms->dir];
406 if (parms->type != TF_TBL_TYPE_METER_DROP_CNT)
407 aparms.subtype = parms->type;
409 aparms.subtype = TF_TBL_TYPE_METER_INST;
410 aparms.index = parms->idx;
411 aparms.allocated = &allocated;
412 rc = tf_rm_is_allocated(&aparms);
416 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
418 "%s, Invalid or not allocated index, type:%s, idx:%d\n",
419 tf_dir_2_str(parms->dir),
420 tf_tbl_type_2_str(parms->type),
426 hparms.rm_db = tbl_db->tbl_db[parms->dir];
427 hparms.subtype = parms->type;
428 hparms.hcapi_type = &hcapi_type;
429 rc = tf_rm_get_hcapi_type(&hparms);
432 "%s, Failed type lookup, type:%s, rc:%s\n",
433 tf_dir_2_str(parms->dir),
434 tf_tbl_type_2_str(parms->type),
440 rc = tf_msg_get_tbl_entry(tfp,
443 parms->data_sz_in_bytes,
449 "%s, Get failed, type:%s, rc:%s\n",
450 tf_dir_2_str(parms->dir),
451 tf_tbl_type_2_str(parms->type),
460 tf_tbl_bulk_get(struct tf *tfp,
461 struct tf_tbl_get_bulk_parms *parms)
465 struct tf_rm_get_hcapi_parms hparms = { 0 };
466 struct tf_rm_check_indexes_in_range_parms cparms = { 0 };
467 struct tf_session *tfs;
468 struct tf_dev_info *dev;
469 struct tbl_rm_db *tbl_db;
470 void *tbl_db_ptr = NULL;
472 TF_CHECK_PARMS2(tfp, parms);
474 /* Retrieve the session information */
475 rc = tf_session_get_session_internal(tfp, &tfs);
479 /* Retrieve the device information */
480 rc = tf_session_get_device(tfs, &dev);
484 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
487 "Failed to get em_ext_db from session, rc:%s\n",
491 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
493 /* Verify that the entries are in the range of reserved resources. */
494 cparms.rm_db = tbl_db->tbl_db[parms->dir];
495 cparms.subtype = parms->type;
496 cparms.num_entries = parms->num_entries;
497 cparms.starting_index = parms->starting_idx;
499 rc = tf_rm_check_indexes_in_range(&cparms);
502 "%s, Invalid or %d index starting from %d"
503 " not in range, type:%s",
504 tf_dir_2_str(parms->dir),
507 tf_tbl_type_2_str(parms->type));
511 hparms.rm_db = tbl_db->tbl_db[parms->dir];
512 hparms.subtype = parms->type;
513 hparms.hcapi_type = &hcapi_type;
514 rc = tf_rm_get_hcapi_type(&hparms);
517 "%s, Failed type lookup, type:%s, rc:%s\n",
518 tf_dir_2_str(parms->dir),
519 tf_tbl_type_2_str(parms->type),
524 /* Get the entries */
525 rc = tf_msg_bulk_get_tbl_entry(tfp,
530 parms->entry_sz_in_bytes,
531 parms->physical_mem_addr,
535 "%s, Bulk get failed, type:%s, rc:%s\n",
536 tf_dir_2_str(parms->dir),
537 tf_tbl_type_2_str(parms->type),
545 tf_tbl_get_resc_info(struct tf *tfp,
546 struct tf_tbl_resource_info *tbl)
550 struct tf_resource_info *dinfo;
551 struct tf_rm_get_alloc_info_parms ainfo;
552 void *tbl_db_ptr = NULL;
553 struct tbl_rm_db *tbl_db;
554 struct tf_dev_info *dev;
555 struct tf_session *tfs;
556 uint16_t base = 0, shift = 0;
558 TF_CHECK_PARMS2(tfp, tbl);
560 /* Retrieve the session information */
561 rc = tf_session_get_session_internal(tfp, &tfs);
565 /* Retrieve the device information */
566 rc = tf_session_get_device(tfs, &dev);
570 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
572 return 0; /* db doesn't exist */
574 return rc; /* error getting db */
576 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
578 for (d = 0; d < TF_DIR_MAX; d++) {
579 ainfo.rm_db = tbl_db->tbl_db[d];
585 ainfo.info = (struct tf_rm_alloc_info *)dinfo;
587 rc = tf_rm_get_all_info(&ainfo, TF_TBL_TYPE_MAX);
591 if (dev->ops->tf_dev_get_tbl_info) {
593 for (i = 0; i < TF_TBL_TYPE_MAX; i++) {
594 /* Only get table info if required for the device */
595 rc = dev->ops->tf_dev_get_tbl_info(tfp,
602 "%s: Failed to get table info:%d\n",
608 TF_TBL_RM_TO_PTR(&dinfo[i].start,