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_tbl_sram.h"
12 #include "tf_sram_mgr.h"
13 #include "tf_common.h"
18 #include "tf_session.h"
19 #include "tf_device.h"
20 #include "cfa_resource_types.h"
24 #define TF_TBL_PTR_TO_RM(new_idx, idx, base, shift) { \
25 *(new_idx) = (((idx) >> (shift)) - (base)); \
29 * tf_sram_tbl_get_info_parms parameter definition
31 struct tf_tbl_sram_get_info_parms {
33 * [in] table RM database
37 * [in] Receive or transmit direction
43 * the TF index table type
45 enum tf_tbl_type tbl_type;
49 * The SRAM bank associated with the type
51 enum tf_sram_bank_id bank_id;
55 * the slice size for the indicated table type
57 enum tf_sram_slice_size slice_size;
61 * Translate HCAPI type to SRAM Manager bank
63 const uint16_t tf_tbl_sram_hcapi_2_bank[CFA_RESOURCE_TYPE_P58_LAST] = {
64 [CFA_RESOURCE_TYPE_P58_SRAM_BANK_0] = TF_SRAM_BANK_ID_0,
65 [CFA_RESOURCE_TYPE_P58_SRAM_BANK_1] = TF_SRAM_BANK_ID_1,
66 [CFA_RESOURCE_TYPE_P58_SRAM_BANK_2] = TF_SRAM_BANK_ID_2,
67 [CFA_RESOURCE_TYPE_P58_SRAM_BANK_3] = TF_SRAM_BANK_ID_3
70 #define TF_TBL_SRAM_SLICES_MAX \
71 (TF_SRAM_MGR_BLOCK_SZ_BYTES / TF_SRAM_MGR_MIN_SLICE_BYTES)
73 * Translate HCAPI type to SRAM Manager bank
75 const uint8_t tf_tbl_sram_slices_2_size[TF_TBL_SRAM_SLICES_MAX + 1] = {
76 [0] = TF_SRAM_SLICE_SIZE_64B, /* if 0 slices assume 1 64B block */
77 [1] = TF_SRAM_SLICE_SIZE_64B, /* 1 slice per 64B block */
78 [2] = TF_SRAM_SLICE_SIZE_32B, /* 2 slices per 64B block */
79 [4] = TF_SRAM_SLICE_SIZE_16B, /* 4 slices per 64B block */
80 [8] = TF_SRAM_SLICE_SIZE_8B /* 8 slices per 64B block */
84 * Get SRAM Table Information for a given index table type
88 * Pointer to SRAM handle
91 * Pointer to the SRAM get info parameters
95 * - (-EINVAL) on failure
98 static int tf_tbl_sram_get_info(struct tf_tbl_sram_get_info_parms *parms)
103 struct tf_rm_get_hcapi_parms hparms;
104 struct tf_rm_get_slices_parms sparms;
106 hparms.rm_db = parms->rm_db;
107 hparms.subtype = parms->tbl_type;
108 hparms.hcapi_type = &hcapi_type;
110 rc = tf_rm_get_hcapi_type(&hparms);
113 "%s: Failed to get hcapi_type %s, rc:%s\n",
114 tf_dir_2_str(parms->dir),
115 tf_tbl_type_2_str(parms->tbl_type),
119 parms->bank_id = tf_tbl_sram_hcapi_2_bank[hcapi_type];
121 sparms.rm_db = parms->rm_db;
122 sparms.subtype = parms->tbl_type;
123 sparms.slices = &slices;
125 rc = tf_rm_get_slices(&sparms);
128 "%s: Failed to get slice cnt %s, rc:%s\n",
129 tf_dir_2_str(parms->dir),
130 tf_tbl_type_2_str(parms->tbl_type),
135 parms->slice_size = tf_tbl_sram_slices_2_size[slices];
138 "(%s) bank(%s) slice_size(%s)\n",
139 tf_tbl_type_2_str(parms->tbl_type),
140 tf_sram_bank_2_str(parms->bank_id),
141 tf_sram_slice_2_str(parms->slice_size));
146 tf_tbl_sram_bind(struct tf *tfp __rte_unused)
149 void *sram_handle = NULL;
151 TF_CHECK_PARMS1(tfp);
153 rc = tf_sram_mgr_bind(&sram_handle);
155 tf_session_set_sram_db(tfp, sram_handle);
158 "SRAM Table - initialized\n");
164 tf_tbl_sram_unbind(struct tf *tfp __rte_unused)
167 void *sram_handle = NULL;
169 TF_CHECK_PARMS1(tfp);
171 rc = tf_session_get_sram_db(tfp, &sram_handle);
174 "Failed to get sram_handle from session, rc:%s\n",
179 rc = tf_sram_mgr_unbind(sram_handle);
182 "SRAM Table - deinitialized\n");
187 tf_tbl_sram_alloc(struct tf *tfp,
188 struct tf_tbl_alloc_parms *parms)
192 struct tf_session *tfs;
193 struct tf_dev_info *dev;
194 struct tf_tbl_sram_get_info_parms iparms = { 0 };
195 struct tf_sram_mgr_alloc_parms aparms = { 0 };
196 struct tbl_rm_db *tbl_db;
197 void *tbl_db_ptr = NULL;
198 void *sram_handle = NULL;
200 TF_CHECK_PARMS2(tfp, parms);
202 /* Retrieve the session information */
203 rc = tf_session_get(tfp, &tfs, &dev);
207 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
210 "Failed to get tbl_db from session, rc:%s\n",
215 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
217 rc = tf_session_get_sram_db(tfp, &sram_handle);
220 "Failed to get sram_handle from session, rc:%s\n",
225 iparms.rm_db = tbl_db->tbl_db[parms->dir];
226 iparms.dir = parms->dir;
227 iparms.tbl_type = parms->type;
229 rc = tf_tbl_sram_get_info(&iparms);
233 "%s: Failed to get SRAM info %s\n",
234 tf_dir_2_str(parms->dir),
235 tf_tbl_type_2_str(parms->type));
239 aparms.dir = parms->dir;
240 aparms.bank_id = iparms.bank_id;
241 aparms.slice_size = iparms.slice_size;
242 aparms.sram_offset = &idx;
243 aparms.tbl_type = parms->type;
244 aparms.rm_db = tbl_db->tbl_db[parms->dir];
246 rc = tf_sram_mgr_alloc(sram_handle, &aparms);
249 "%s: Failed to allocate SRAM table:%s\n",
250 tf_dir_2_str(parms->dir),
251 tf_tbl_type_2_str(parms->type));
258 struct tf_sram_mgr_dump_parms dparms;
260 dparms.dir = parms->dir;
261 dparms.bank_id = iparms.bank_id;
262 dparms.slice_size = iparms.slice_size;
264 rc = tf_sram_mgr_dump(sram_handle, &dparms);
272 tf_tbl_sram_free(struct tf *tfp __rte_unused,
273 struct tf_tbl_free_parms *parms)
276 struct tf_session *tfs;
277 struct tf_dev_info *dev;
278 struct tbl_rm_db *tbl_db;
279 void *tbl_db_ptr = NULL;
280 struct tf_tbl_sram_get_info_parms iparms = { 0 };
281 struct tf_sram_mgr_free_parms fparms = { 0 };
282 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
283 bool allocated = false;
284 void *sram_handle = NULL;
286 TF_CHECK_PARMS2(tfp, parms);
288 /* Retrieve the session information */
289 rc = tf_session_get(tfp, &tfs, &dev);
293 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
296 "Failed to get em_ext_db from session, rc:%s\n",
300 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
302 rc = tf_session_get_sram_db(tfp, &sram_handle);
305 "Failed to get sram_handle from session, rc:%s\n",
310 iparms.rm_db = tbl_db->tbl_db[parms->dir];
311 iparms.dir = parms->dir;
312 iparms.tbl_type = parms->type;
314 rc = tf_tbl_sram_get_info(&iparms);
317 "%s: Failed to get table info:%s\n",
318 tf_dir_2_str(parms->dir),
319 tf_tbl_type_2_str(parms->type));
325 struct tf_sram_mgr_dump_parms dparms;
327 printf("%s: %s: %s\n", tf_dir_2_str(parms->dir),
328 tf_sram_slice_2_str(iparms.slice_size),
329 tf_sram_bank_2_str(iparms.bank_id));
331 dparms.dir = parms->dir;
332 dparms.bank_id = iparms.bank_id;
333 dparms.slice_size = iparms.slice_size;
335 rc = tf_sram_mgr_dump(sram_handle, &dparms);
339 aparms.sram_offset = parms->idx;
340 aparms.slice_size = iparms.slice_size;
341 aparms.bank_id = iparms.bank_id;
342 aparms.dir = parms->dir;
343 aparms.is_allocated = &allocated;
345 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
346 if (rc || !allocated) {
348 "%s: Free of invalid entry:%s idx(%d):(%s)\n",
349 tf_dir_2_str(parms->dir),
350 tf_tbl_type_2_str(parms->type),
357 fparms.rm_db = tbl_db->tbl_db[parms->dir];
358 fparms.tbl_type = parms->type;
359 fparms.sram_offset = parms->idx;
360 fparms.slice_size = iparms.slice_size;
361 fparms.bank_id = iparms.bank_id;
362 fparms.dir = parms->dir;
363 #if (STATS_CLEAR_ON_READ_SUPPORT == 0)
366 rc = tf_sram_mgr_free(sram_handle, &fparms);
369 "%s: Failed to free entry:%s idx(%d)\n",
370 tf_dir_2_str(parms->dir),
371 tf_tbl_type_2_str(parms->type),
379 struct tf_sram_mgr_dump_parms dparms;
381 printf("%s: %s: %s\n", tf_dir_2_str(parms->dir),
382 tf_sram_slice_2_str(iparms.slice_size),
383 tf_sram_bank_2_str(iparms.bank_id));
385 dparms.dir = parms->dir;
386 dparms.bank_id = iparms.bank_id;
387 dparms.slice_size = iparms.slice_size;
389 rc = tf_sram_mgr_dump(sram_handle, &dparms);
396 tf_tbl_sram_set(struct tf *tfp,
397 struct tf_tbl_set_parms *parms)
403 struct tf_rm_get_hcapi_parms hparms = { 0 };
404 struct tf_session *tfs;
405 struct tf_dev_info *dev;
406 struct tbl_rm_db *tbl_db;
407 void *tbl_db_ptr = NULL;
408 struct tf_tbl_sram_get_info_parms iparms = { 0 };
409 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
410 struct tf_rm_is_allocated_parms raparms = { 0 };
411 void *sram_handle = NULL;
412 uint16_t base = 0, shift = 0;
415 TF_CHECK_PARMS3(tfp, parms, parms->data);
417 /* Retrieve the session information */
418 rc = tf_session_get(tfp, &tfs, &dev);
422 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
425 "Failed to get em_ext_db from session, rc:%s\n",
429 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
431 rc = tf_session_get_sram_db(tfp, &sram_handle);
434 "Failed to get sram_handle from session, rc:%s\n",
439 iparms.rm_db = tbl_db->tbl_db[parms->dir];
440 iparms.dir = parms->dir;
441 iparms.tbl_type = parms->type;
443 rc = tf_tbl_sram_get_info(&iparms);
446 "%s: Failed to get table info:%s\n",
447 tf_dir_2_str(parms->dir),
448 tf_tbl_type_2_str(parms->type));
452 if (tf_session_is_shared_session(tfs)) {
453 /* Only get table info if required for the device */
454 if (dev->ops->tf_dev_get_tbl_info) {
455 rc = dev->ops->tf_dev_get_tbl_info(tfp,
456 tbl_db->tbl_db[parms->dir],
462 "%s: Failed to get table info:%d\n",
463 tf_dir_2_str(parms->dir),
468 TF_TBL_PTR_TO_RM(&raparms.index, parms->idx, base, shift);
470 raparms.rm_db = tbl_db->tbl_db[parms->dir];
471 raparms.subtype = parms->type;
472 raparms.allocated = &rallocated;
473 rc = tf_rm_is_allocated(&raparms);
477 if (rallocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
479 "%s, Invalid or not allocated index, type:%s, idx:%d\n",
480 tf_dir_2_str(parms->dir),
481 tf_tbl_type_2_str(parms->type),
486 aparms.sram_offset = parms->idx;
487 aparms.slice_size = iparms.slice_size;
488 aparms.bank_id = iparms.bank_id;
489 aparms.dir = parms->dir;
490 aparms.is_allocated = &allocated;
491 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
492 if (rc || !allocated) {
494 "%s: Entry not allocated:%s idx(%d):(%s)\n",
495 tf_dir_2_str(parms->dir),
496 tf_tbl_type_2_str(parms->type),
504 hparms.rm_db = tbl_db->tbl_db[parms->dir];
505 hparms.subtype = parms->type;
506 hparms.hcapi_type = &hcapi_type;
507 rc = tf_rm_get_hcapi_type(&hparms);
510 "%s, Failed type lookup, type:%s, rc:%s\n",
511 tf_dir_2_str(parms->dir),
512 tf_tbl_type_2_str(parms->type),
517 rc = tf_msg_set_tbl_entry(tfp,
520 parms->data_sz_in_bytes,
525 "%s, Set failed, type:%s, rc:%s\n",
526 tf_dir_2_str(parms->dir),
527 tf_tbl_type_2_str(parms->type),
535 tf_tbl_sram_get(struct tf *tfp,
536 struct tf_tbl_get_parms *parms)
541 struct tf_rm_get_hcapi_parms hparms = { 0 };
542 struct tf_session *tfs;
543 struct tf_dev_info *dev;
544 struct tbl_rm_db *tbl_db;
545 void *tbl_db_ptr = NULL;
546 struct tf_tbl_sram_get_info_parms iparms = { 0 };
547 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
548 void *sram_handle = NULL;
550 TF_CHECK_PARMS3(tfp, parms, parms->data);
552 /* Retrieve the session information */
553 rc = tf_session_get(tfp, &tfs, &dev);
557 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
560 "Failed to get em_ext_db from session, rc:%s\n",
564 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
566 rc = tf_session_get_sram_db(tfp, &sram_handle);
569 "Failed to get sram_handle from session, rc:%s\n",
574 iparms.rm_db = tbl_db->tbl_db[parms->dir];
575 iparms.dir = parms->dir;
576 iparms.tbl_type = parms->type;
578 rc = tf_tbl_sram_get_info(&iparms);
581 "%s: Failed to get table info:%s\n",
582 tf_dir_2_str(parms->dir),
583 tf_tbl_type_2_str(parms->type));
587 aparms.sram_offset = parms->idx;
588 aparms.slice_size = iparms.slice_size;
589 aparms.bank_id = iparms.bank_id;
590 aparms.dir = parms->dir;
591 aparms.is_allocated = &allocated;
593 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
594 if (rc || !allocated) {
596 "%s: Entry not allocated:%s idx(%d):(%s)\n",
597 tf_dir_2_str(parms->dir),
598 tf_tbl_type_2_str(parms->type),
606 hparms.rm_db = tbl_db->tbl_db[parms->dir];
607 hparms.subtype = parms->type;
608 hparms.hcapi_type = &hcapi_type;
609 rc = tf_rm_get_hcapi_type(&hparms);
612 "%s, Failed type lookup, type:%s, rc:%s\n",
613 tf_dir_2_str(parms->dir),
614 tf_tbl_type_2_str(parms->type),
620 rc = tf_msg_get_tbl_entry(tfp,
623 parms->data_sz_in_bytes,
628 "%s, Get failed, type:%s, rc:%s\n",
629 tf_dir_2_str(parms->dir),
630 tf_tbl_type_2_str(parms->type),
638 tf_tbl_sram_bulk_get(struct tf *tfp,
639 struct tf_tbl_get_bulk_parms *parms)
643 struct tf_rm_get_hcapi_parms hparms = { 0 };
644 struct tf_tbl_sram_get_info_parms iparms = { 0 };
645 struct tf_session *tfs;
646 struct tf_dev_info *dev;
647 struct tbl_rm_db *tbl_db;
648 void *tbl_db_ptr = NULL;
650 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
651 bool allocated = false;
652 void *sram_handle = NULL;
654 TF_CHECK_PARMS2(tfp, parms);
656 /* Retrieve the session information */
657 rc = tf_session_get(tfp, &tfs, &dev);
661 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
664 "Failed to get em_ext_db from session, rc:%s\n",
668 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
670 rc = tf_session_get_sram_db(tfp, &sram_handle);
673 "Failed to get sram_handle from session, rc:%s\n",
678 iparms.rm_db = tbl_db->tbl_db[parms->dir];
679 iparms.dir = parms->dir;
680 iparms.tbl_type = parms->type;
682 rc = tf_tbl_sram_get_info(&iparms);
685 "%s: Failed to get table info:%s\n",
686 tf_dir_2_str(parms->dir),
687 tf_tbl_type_2_str(parms->type));
691 /* Validate the start offset and the end offset is allocated
692 * This API is only used for statistics. 8 Byte entry allocation
695 aparms.sram_offset = parms->starting_idx;
696 aparms.slice_size = iparms.slice_size;
697 aparms.bank_id = iparms.bank_id;
698 aparms.dir = parms->dir;
699 aparms.is_allocated = &allocated;
700 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
701 if (rc || !allocated) {
703 "%s: Entry not allocated:%s starting_idx(%d):(%s)\n",
704 tf_dir_2_str(parms->dir),
705 tf_tbl_type_2_str(parms->type),
711 idx = parms->starting_idx + parms->num_entries - 1;
712 aparms.sram_offset = idx;
713 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
714 if (rc || !allocated) {
716 "%s: Entry not allocated:%s last_idx(%d):(%s)\n",
717 tf_dir_2_str(parms->dir),
718 tf_tbl_type_2_str(parms->type),
725 hparms.rm_db = tbl_db->tbl_db[parms->dir];
726 hparms.subtype = parms->type;
727 hparms.hcapi_type = &hcapi_type;
728 rc = tf_rm_get_hcapi_type(&hparms);
731 "%s, Failed type lookup, type:%s, rc:%s\n",
732 tf_dir_2_str(parms->dir),
733 tf_tbl_type_2_str(parms->type),
738 /* Get the entries */
739 rc = tf_msg_bulk_get_tbl_entry(tfp,
744 parms->entry_sz_in_bytes,
745 parms->physical_mem_addr);
748 "%s, Bulk get failed, type:%s, rc:%s\n",
749 tf_dir_2_str(parms->dir),
750 tf_tbl_type_2_str(parms->type),