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];
141 tf_tbl_sram_bind(struct tf *tfp __rte_unused)
144 void *sram_handle = NULL;
146 TF_CHECK_PARMS1(tfp);
148 rc = tf_sram_mgr_bind(&sram_handle);
150 tf_session_set_sram_db(tfp, sram_handle);
153 "SRAM Table - initialized\n");
159 tf_tbl_sram_unbind(struct tf *tfp __rte_unused)
162 void *sram_handle = NULL;
164 TF_CHECK_PARMS1(tfp);
166 rc = tf_session_get_sram_db(tfp, &sram_handle);
169 "Failed to get sram_handle from session, rc:%s\n",
174 rc = tf_sram_mgr_unbind(sram_handle);
177 "SRAM Table - deinitialized\n");
182 tf_tbl_sram_alloc(struct tf *tfp,
183 struct tf_tbl_alloc_parms *parms)
187 struct tf_session *tfs;
188 struct tf_dev_info *dev;
189 struct tf_tbl_sram_get_info_parms iparms = { 0 };
190 struct tf_sram_mgr_alloc_parms aparms = { 0 };
191 struct tbl_rm_db *tbl_db;
192 void *tbl_db_ptr = NULL;
193 void *sram_handle = NULL;
195 TF_CHECK_PARMS2(tfp, parms);
197 /* Retrieve the session information */
198 rc = tf_session_get(tfp, &tfs, &dev);
202 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
205 "Failed to get tbl_db from session, rc:%s\n",
210 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
212 rc = tf_session_get_sram_db(tfp, &sram_handle);
215 "Failed to get sram_handle from session, rc:%s\n",
220 iparms.rm_db = tbl_db->tbl_db[parms->dir];
221 iparms.dir = parms->dir;
222 iparms.tbl_type = parms->type;
224 rc = tf_tbl_sram_get_info(&iparms);
228 "%s: Failed to get SRAM info %s\n",
229 tf_dir_2_str(parms->dir),
230 tf_tbl_type_2_str(parms->type));
234 aparms.dir = parms->dir;
235 aparms.bank_id = iparms.bank_id;
236 aparms.slice_size = iparms.slice_size;
237 aparms.sram_offset = &idx;
238 aparms.tbl_type = parms->type;
239 aparms.rm_db = tbl_db->tbl_db[parms->dir];
241 rc = tf_sram_mgr_alloc(sram_handle, &aparms);
244 "%s: Failed to allocate SRAM table:%s\n",
245 tf_dir_2_str(parms->dir),
246 tf_tbl_type_2_str(parms->type));
253 struct tf_sram_mgr_dump_parms dparms;
255 dparms.dir = parms->dir;
256 dparms.bank_id = iparms.bank_id;
257 dparms.slice_size = iparms.slice_size;
259 rc = tf_sram_mgr_dump(sram_handle, &dparms);
267 tf_tbl_sram_free(struct tf *tfp __rte_unused,
268 struct tf_tbl_free_parms *parms)
271 struct tf_session *tfs;
272 struct tf_dev_info *dev;
273 struct tbl_rm_db *tbl_db;
274 void *tbl_db_ptr = NULL;
275 struct tf_tbl_sram_get_info_parms iparms = { 0 };
276 struct tf_sram_mgr_free_parms fparms = { 0 };
277 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
278 bool allocated = false;
279 void *sram_handle = NULL;
281 TF_CHECK_PARMS2(tfp, parms);
283 /* Retrieve the session information */
284 rc = tf_session_get(tfp, &tfs, &dev);
288 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
291 "Failed to get em_ext_db from session, rc:%s\n",
295 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
297 rc = tf_session_get_sram_db(tfp, &sram_handle);
300 "Failed to get sram_handle from session, rc:%s\n",
305 iparms.rm_db = tbl_db->tbl_db[parms->dir];
306 iparms.dir = parms->dir;
307 iparms.tbl_type = parms->type;
309 rc = tf_tbl_sram_get_info(&iparms);
312 "%s: Failed to get table info:%s\n",
313 tf_dir_2_str(parms->dir),
314 tf_tbl_type_2_str(parms->type));
320 struct tf_sram_mgr_dump_parms dparms;
322 printf("%s: %s: %s\n", tf_dir_2_str(parms->dir),
323 tf_sram_slice_2_str(iparms.slice_size),
324 tf_sram_bank_2_str(iparms.bank_id));
326 dparms.dir = parms->dir;
327 dparms.bank_id = iparms.bank_id;
328 dparms.slice_size = iparms.slice_size;
330 rc = tf_sram_mgr_dump(sram_handle, &dparms);
334 aparms.sram_offset = parms->idx;
335 aparms.slice_size = iparms.slice_size;
336 aparms.bank_id = iparms.bank_id;
337 aparms.dir = parms->dir;
338 aparms.is_allocated = &allocated;
340 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
341 if (rc || !allocated) {
343 "%s: Free of invalid entry:%s idx(%d):(%s)\n",
344 tf_dir_2_str(parms->dir),
345 tf_tbl_type_2_str(parms->type),
352 fparms.rm_db = tbl_db->tbl_db[parms->dir];
353 fparms.tbl_type = parms->type;
354 fparms.sram_offset = parms->idx;
355 fparms.slice_size = iparms.slice_size;
356 fparms.bank_id = iparms.bank_id;
357 fparms.dir = parms->dir;
358 #if (STATS_CLEAR_ON_READ_SUPPORT == 0)
361 rc = tf_sram_mgr_free(sram_handle, &fparms);
364 "%s: Failed to free entry:%s idx(%d)\n",
365 tf_dir_2_str(parms->dir),
366 tf_tbl_type_2_str(parms->type),
373 struct tf_sram_mgr_dump_parms dparms;
375 printf("%s: %s: %s\n", tf_dir_2_str(parms->dir),
376 tf_sram_slice_2_str(iparms.slice_size),
377 tf_sram_bank_2_str(iparms.bank_id));
379 dparms.dir = parms->dir;
380 dparms.bank_id = iparms.bank_id;
381 dparms.slice_size = iparms.slice_size;
383 rc = tf_sram_mgr_dump(sram_handle, &dparms);
390 tf_tbl_sram_set(struct tf *tfp,
391 struct tf_tbl_set_parms *parms)
397 struct tf_rm_get_hcapi_parms hparms = { 0 };
398 struct tf_session *tfs;
399 struct tf_dev_info *dev;
400 struct tbl_rm_db *tbl_db;
401 void *tbl_db_ptr = NULL;
402 struct tf_tbl_sram_get_info_parms iparms = { 0 };
403 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
404 struct tf_rm_is_allocated_parms raparms = { 0 };
405 void *sram_handle = NULL;
406 uint16_t base = 0, shift = 0;
408 TF_CHECK_PARMS3(tfp, parms, parms->data);
410 /* Retrieve the session information */
411 rc = tf_session_get(tfp, &tfs, &dev);
415 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
418 "Failed to get em_ext_db from session, rc:%s\n",
422 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
424 rc = tf_session_get_sram_db(tfp, &sram_handle);
427 "Failed to get sram_handle from session, rc:%s\n",
432 iparms.rm_db = tbl_db->tbl_db[parms->dir];
433 iparms.dir = parms->dir;
434 iparms.tbl_type = parms->type;
436 rc = tf_tbl_sram_get_info(&iparms);
439 "%s: Failed to get table info:%s\n",
440 tf_dir_2_str(parms->dir),
441 tf_tbl_type_2_str(parms->type));
445 if (tf_session_is_shared_session(tfs)) {
446 /* Only get table info if required for the device */
447 if (dev->ops->tf_dev_get_tbl_info) {
448 rc = dev->ops->tf_dev_get_tbl_info(tfp,
449 tbl_db->tbl_db[parms->dir],
455 "%s: Failed to get table info:%d\n",
456 tf_dir_2_str(parms->dir),
461 TF_TBL_PTR_TO_RM(&raparms.index, parms->idx, base, shift);
463 raparms.rm_db = tbl_db->tbl_db[parms->dir];
464 raparms.subtype = parms->type;
465 raparms.allocated = &rallocated;
466 rc = tf_rm_is_allocated(&raparms);
470 if (rallocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
472 "%s, Invalid or not allocated index, type:%s, idx:%d\n",
473 tf_dir_2_str(parms->dir),
474 tf_tbl_type_2_str(parms->type),
479 aparms.sram_offset = parms->idx;
480 aparms.slice_size = iparms.slice_size;
481 aparms.bank_id = iparms.bank_id;
482 aparms.dir = parms->dir;
483 aparms.is_allocated = &allocated;
484 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
485 if (rc || !allocated) {
487 "%s: Entry not allocated:%s idx(%d):(%s)\n",
488 tf_dir_2_str(parms->dir),
489 tf_tbl_type_2_str(parms->type),
497 hparms.rm_db = tbl_db->tbl_db[parms->dir];
498 hparms.subtype = parms->type;
499 hparms.hcapi_type = &hcapi_type;
500 rc = tf_rm_get_hcapi_type(&hparms);
503 "%s, Failed type lookup, type:%s, rc:%s\n",
504 tf_dir_2_str(parms->dir),
505 tf_tbl_type_2_str(parms->type),
510 rc = tf_msg_set_tbl_entry(tfp,
513 parms->data_sz_in_bytes,
518 "%s, Set failed, type:%s, rc:%s\n",
519 tf_dir_2_str(parms->dir),
520 tf_tbl_type_2_str(parms->type),
528 tf_tbl_sram_get(struct tf *tfp,
529 struct tf_tbl_get_parms *parms)
534 struct tf_rm_get_hcapi_parms hparms = { 0 };
535 struct tf_session *tfs;
536 struct tf_dev_info *dev;
537 struct tbl_rm_db *tbl_db;
538 void *tbl_db_ptr = NULL;
539 struct tf_tbl_sram_get_info_parms iparms = { 0 };
540 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
541 void *sram_handle = NULL;
543 TF_CHECK_PARMS3(tfp, parms, parms->data);
545 /* Retrieve the session information */
546 rc = tf_session_get(tfp, &tfs, &dev);
550 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
553 "Failed to get em_ext_db from session, rc:%s\n",
557 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
559 rc = tf_session_get_sram_db(tfp, &sram_handle);
562 "Failed to get sram_handle from session, rc:%s\n",
567 iparms.rm_db = tbl_db->tbl_db[parms->dir];
568 iparms.dir = parms->dir;
569 iparms.tbl_type = parms->type;
571 rc = tf_tbl_sram_get_info(&iparms);
574 "%s: Failed to get table info:%s\n",
575 tf_dir_2_str(parms->dir),
576 tf_tbl_type_2_str(parms->type));
580 aparms.sram_offset = parms->idx;
581 aparms.slice_size = iparms.slice_size;
582 aparms.bank_id = iparms.bank_id;
583 aparms.dir = parms->dir;
584 aparms.is_allocated = &allocated;
586 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
587 if (rc || !allocated) {
589 "%s: Entry not allocated:%s idx(%d):(%s)\n",
590 tf_dir_2_str(parms->dir),
591 tf_tbl_type_2_str(parms->type),
599 hparms.rm_db = tbl_db->tbl_db[parms->dir];
600 hparms.subtype = parms->type;
601 hparms.hcapi_type = &hcapi_type;
602 rc = tf_rm_get_hcapi_type(&hparms);
605 "%s, Failed type lookup, type:%s, rc:%s\n",
606 tf_dir_2_str(parms->dir),
607 tf_tbl_type_2_str(parms->type),
613 rc = tf_msg_get_tbl_entry(tfp,
616 parms->data_sz_in_bytes,
621 "%s, Get failed, type:%s, rc:%s\n",
622 tf_dir_2_str(parms->dir),
623 tf_tbl_type_2_str(parms->type),
631 tf_tbl_sram_bulk_get(struct tf *tfp,
632 struct tf_tbl_get_bulk_parms *parms)
636 struct tf_rm_get_hcapi_parms hparms = { 0 };
637 struct tf_tbl_sram_get_info_parms iparms = { 0 };
638 struct tf_session *tfs;
639 struct tf_dev_info *dev;
640 struct tbl_rm_db *tbl_db;
641 void *tbl_db_ptr = NULL;
643 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
644 bool allocated = false;
645 void *sram_handle = NULL;
647 TF_CHECK_PARMS2(tfp, parms);
649 /* Retrieve the session information */
650 rc = tf_session_get(tfp, &tfs, &dev);
654 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
657 "Failed to get em_ext_db from session, rc:%s\n",
661 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
663 rc = tf_session_get_sram_db(tfp, &sram_handle);
666 "Failed to get sram_handle from session, rc:%s\n",
671 iparms.rm_db = tbl_db->tbl_db[parms->dir];
672 iparms.dir = parms->dir;
673 iparms.tbl_type = parms->type;
675 rc = tf_tbl_sram_get_info(&iparms);
678 "%s: Failed to get table info:%s\n",
679 tf_dir_2_str(parms->dir),
680 tf_tbl_type_2_str(parms->type));
684 /* Validate the start offset and the end offset is allocated
685 * This API is only used for statistics. 8 Byte entry allocation
688 aparms.sram_offset = parms->starting_idx;
689 aparms.slice_size = iparms.slice_size;
690 aparms.bank_id = iparms.bank_id;
691 aparms.dir = parms->dir;
692 aparms.is_allocated = &allocated;
693 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
694 if (rc || !allocated) {
696 "%s: Entry not allocated:%s starting_idx(%d):(%s)\n",
697 tf_dir_2_str(parms->dir),
698 tf_tbl_type_2_str(parms->type),
704 idx = parms->starting_idx + parms->num_entries - 1;
705 aparms.sram_offset = idx;
706 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
707 if (rc || !allocated) {
709 "%s: Entry not allocated:%s last_idx(%d):(%s)\n",
710 tf_dir_2_str(parms->dir),
711 tf_tbl_type_2_str(parms->type),
718 hparms.rm_db = tbl_db->tbl_db[parms->dir];
719 hparms.subtype = parms->type;
720 hparms.hcapi_type = &hcapi_type;
721 rc = tf_rm_get_hcapi_type(&hparms);
724 "%s, Failed type lookup, type:%s, rc:%s\n",
725 tf_dir_2_str(parms->dir),
726 tf_tbl_type_2_str(parms->type),
731 /* Get the entries */
732 rc = tf_msg_bulk_get_tbl_entry(tfp,
737 parms->entry_sz_in_bytes,
738 parms->physical_mem_addr);
741 "%s, Bulk get failed, type:%s, rc:%s\n",
742 tf_dir_2_str(parms->dir),
743 tf_tbl_type_2_str(parms->type),