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;
542 bool clear_on_read = false;
544 TF_CHECK_PARMS3(tfp, parms, parms->data);
546 /* Retrieve the session information */
547 rc = tf_session_get(tfp, &tfs, &dev);
551 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
554 "Failed to get em_ext_db from session, rc:%s\n",
558 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
560 rc = tf_session_get_sram_db(tfp, &sram_handle);
563 "Failed to get sram_handle from session, rc:%s\n",
568 iparms.rm_db = tbl_db->tbl_db[parms->dir];
569 iparms.dir = parms->dir;
570 iparms.tbl_type = parms->type;
572 rc = tf_tbl_sram_get_info(&iparms);
575 "%s: Failed to get table info:%s\n",
576 tf_dir_2_str(parms->dir),
577 tf_tbl_type_2_str(parms->type));
581 aparms.sram_offset = parms->idx;
582 aparms.slice_size = iparms.slice_size;
583 aparms.bank_id = iparms.bank_id;
584 aparms.dir = parms->dir;
585 aparms.is_allocated = &allocated;
587 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
588 if (rc || !allocated) {
590 "%s: Entry not allocated:%s idx(%d):(%s)\n",
591 tf_dir_2_str(parms->dir),
592 tf_tbl_type_2_str(parms->type),
600 hparms.rm_db = tbl_db->tbl_db[parms->dir];
601 hparms.subtype = parms->type;
602 hparms.hcapi_type = &hcapi_type;
603 rc = tf_rm_get_hcapi_type(&hparms);
606 "%s, Failed type lookup, type:%s, rc:%s\n",
607 tf_dir_2_str(parms->dir),
608 tf_tbl_type_2_str(parms->type),
612 if (parms->type == TF_TBL_TYPE_ACT_STATS_64)
613 clear_on_read = true;
616 rc = tf_msg_get_tbl_entry(tfp,
619 parms->data_sz_in_bytes,
625 "%s, Get failed, type:%s, rc:%s\n",
626 tf_dir_2_str(parms->dir),
627 tf_tbl_type_2_str(parms->type),
635 tf_tbl_sram_bulk_get(struct tf *tfp,
636 struct tf_tbl_get_bulk_parms *parms)
640 struct tf_rm_get_hcapi_parms hparms = { 0 };
641 struct tf_tbl_sram_get_info_parms iparms = { 0 };
642 struct tf_session *tfs;
643 struct tf_dev_info *dev;
644 struct tbl_rm_db *tbl_db;
645 void *tbl_db_ptr = NULL;
647 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
648 bool allocated = false;
649 void *sram_handle = NULL;
650 bool clear_on_read = false;
652 TF_CHECK_PARMS2(tfp, parms);
654 /* Retrieve the session information */
655 rc = tf_session_get(tfp, &tfs, &dev);
659 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
662 "Failed to get em_ext_db from session, rc:%s\n",
666 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
668 rc = tf_session_get_sram_db(tfp, &sram_handle);
671 "Failed to get sram_handle from session, rc:%s\n",
676 iparms.rm_db = tbl_db->tbl_db[parms->dir];
677 iparms.dir = parms->dir;
678 iparms.tbl_type = parms->type;
680 rc = tf_tbl_sram_get_info(&iparms);
683 "%s: Failed to get table info:%s\n",
684 tf_dir_2_str(parms->dir),
685 tf_tbl_type_2_str(parms->type));
689 /* Validate the start offset and the end offset is allocated
690 * This API is only used for statistics. 8 Byte entry allocation
693 aparms.sram_offset = parms->starting_idx;
694 aparms.slice_size = iparms.slice_size;
695 aparms.bank_id = iparms.bank_id;
696 aparms.dir = parms->dir;
697 aparms.is_allocated = &allocated;
698 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
699 if (rc || !allocated) {
701 "%s: Entry not allocated:%s starting_idx(%d):(%s)\n",
702 tf_dir_2_str(parms->dir),
703 tf_tbl_type_2_str(parms->type),
709 idx = parms->starting_idx + parms->num_entries - 1;
710 aparms.sram_offset = idx;
711 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
712 if (rc || !allocated) {
714 "%s: Entry not allocated:%s last_idx(%d):(%s)\n",
715 tf_dir_2_str(parms->dir),
716 tf_tbl_type_2_str(parms->type),
723 hparms.rm_db = tbl_db->tbl_db[parms->dir];
724 hparms.subtype = parms->type;
725 hparms.hcapi_type = &hcapi_type;
726 rc = tf_rm_get_hcapi_type(&hparms);
729 "%s, Failed type lookup, type:%s, rc:%s\n",
730 tf_dir_2_str(parms->dir),
731 tf_tbl_type_2_str(parms->type),
736 if (parms->type == TF_TBL_TYPE_ACT_STATS_64)
737 clear_on_read = true;
739 /* Get the entries */
740 rc = tf_msg_bulk_get_tbl_entry(tfp,
745 parms->entry_sz_in_bytes,
746 parms->physical_mem_addr,
750 "%s, Bulk get failed, type:%s, rc:%s\n",
751 tf_dir_2_str(parms->dir),
752 tf_tbl_type_2_str(parms->type),