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"
25 * tf_sram_tbl_get_info_parms parameter definition
27 struct tf_tbl_sram_get_info_parms {
29 * [in] table RM database
33 * [in] Receive or transmit direction
39 * the TF index table type
41 enum tf_tbl_type tbl_type;
45 * The SRAM bank associated with the type
47 enum tf_sram_bank_id bank_id;
51 * the slice size for the indicated table type
53 enum tf_sram_slice_size slice_size;
57 * Translate HCAPI type to SRAM Manager bank
59 const uint16_t tf_tbl_sram_hcapi_2_bank[CFA_RESOURCE_TYPE_P58_LAST] = {
60 [CFA_RESOURCE_TYPE_P58_SRAM_BANK_0] = TF_SRAM_BANK_ID_0,
61 [CFA_RESOURCE_TYPE_P58_SRAM_BANK_1] = TF_SRAM_BANK_ID_1,
62 [CFA_RESOURCE_TYPE_P58_SRAM_BANK_2] = TF_SRAM_BANK_ID_2,
63 [CFA_RESOURCE_TYPE_P58_SRAM_BANK_3] = TF_SRAM_BANK_ID_3
66 #define TF_TBL_SRAM_SLICES_MAX \
67 (TF_SRAM_MGR_BLOCK_SZ_BYTES / TF_SRAM_MGR_MIN_SLICE_BYTES)
69 * Translate HCAPI type to SRAM Manager bank
71 const uint8_t tf_tbl_sram_slices_2_size[TF_TBL_SRAM_SLICES_MAX + 1] = {
72 [0] = TF_SRAM_SLICE_SIZE_64B, /* if 0 slices assume 1 64B block */
73 [1] = TF_SRAM_SLICE_SIZE_64B, /* 1 slice per 64B block */
74 [2] = TF_SRAM_SLICE_SIZE_32B, /* 2 slices per 64B block */
75 [4] = TF_SRAM_SLICE_SIZE_16B, /* 4 slices per 64B block */
76 [8] = TF_SRAM_SLICE_SIZE_8B /* 8 slices per 64B block */
80 * Get SRAM Table Information for a given index table type
84 * Pointer to SRAM handle
87 * Pointer to the SRAM get info parameters
91 * - (-EINVAL) on failure
94 static int tf_tbl_sram_get_info(struct tf_tbl_sram_get_info_parms *parms)
99 struct tf_rm_get_hcapi_parms hparms;
100 struct tf_rm_get_slices_parms sparms;
102 hparms.rm_db = parms->rm_db;
103 hparms.subtype = parms->tbl_type;
104 hparms.hcapi_type = &hcapi_type;
106 rc = tf_rm_get_hcapi_type(&hparms);
109 "%s: Failed to get hcapi_type %s, rc:%s\n",
110 tf_dir_2_str(parms->dir),
111 tf_tbl_type_2_str(parms->tbl_type),
115 parms->bank_id = tf_tbl_sram_hcapi_2_bank[hcapi_type];
117 sparms.rm_db = parms->rm_db;
118 sparms.subtype = parms->tbl_type;
119 sparms.slices = &slices;
121 rc = tf_rm_get_slices(&sparms);
124 "%s: Failed to get slice cnt %s, rc:%s\n",
125 tf_dir_2_str(parms->dir),
126 tf_tbl_type_2_str(parms->tbl_type),
131 parms->slice_size = tf_tbl_sram_slices_2_size[slices];
134 "(%s) bank(%s) slice_size(%s)\n",
135 tf_tbl_type_2_str(parms->tbl_type),
136 tf_sram_bank_2_str(parms->bank_id),
137 tf_sram_slice_2_str(parms->slice_size));
142 tf_tbl_sram_bind(struct tf *tfp __rte_unused)
145 void *sram_handle = NULL;
147 TF_CHECK_PARMS1(tfp);
149 rc = tf_sram_mgr_bind(&sram_handle);
151 tf_session_set_sram_db(tfp, sram_handle);
154 "SRAM Table - initialized\n");
160 tf_tbl_sram_unbind(struct tf *tfp __rte_unused)
163 void *sram_handle = NULL;
165 TF_CHECK_PARMS1(tfp);
167 rc = tf_session_get_sram_db(tfp, &sram_handle);
170 "Failed to get sram_handle from session, rc:%s\n",
175 rc = tf_sram_mgr_unbind(sram_handle);
178 "SRAM Table - deinitialized\n");
183 tf_tbl_sram_alloc(struct tf *tfp,
184 struct tf_tbl_alloc_parms *parms)
188 struct tf_session *tfs;
189 struct tf_dev_info *dev;
190 struct tf_tbl_sram_get_info_parms iparms = { 0 };
191 struct tf_sram_mgr_alloc_parms aparms = { 0 };
192 struct tbl_rm_db *tbl_db;
193 void *tbl_db_ptr = NULL;
194 void *sram_handle = NULL;
196 TF_CHECK_PARMS2(tfp, parms);
198 /* Retrieve the session information */
199 rc = tf_session_get(tfp, &tfs, &dev);
203 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
206 "Failed to get tbl_db from session, rc:%s\n",
211 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
213 rc = tf_session_get_sram_db(tfp, &sram_handle);
216 "Failed to get sram_handle from session, rc:%s\n",
221 iparms.rm_db = tbl_db->tbl_db[parms->dir];
222 iparms.dir = parms->dir;
223 iparms.tbl_type = parms->type;
225 rc = tf_tbl_sram_get_info(&iparms);
229 "%s: Failed to get SRAM info %s\n",
230 tf_dir_2_str(parms->dir),
231 tf_tbl_type_2_str(parms->type));
235 aparms.dir = parms->dir;
236 aparms.bank_id = iparms.bank_id;
237 aparms.slice_size = iparms.slice_size;
238 aparms.sram_offset = &idx;
239 aparms.tbl_type = parms->type;
240 aparms.rm_db = tbl_db->tbl_db[parms->dir];
242 rc = tf_sram_mgr_alloc(sram_handle, &aparms);
245 "%s: Failed to allocate SRAM table:%s\n",
246 tf_dir_2_str(parms->dir),
247 tf_tbl_type_2_str(parms->type));
254 struct tf_sram_mgr_dump_parms dparms;
256 dparms.dir = parms->dir;
257 dparms.bank_id = iparms.bank_id;
258 dparms.slice_size = iparms.slice_size;
260 rc = tf_sram_mgr_dump(sram_handle, &dparms);
268 tf_tbl_sram_free(struct tf *tfp __rte_unused,
269 struct tf_tbl_free_parms *parms)
272 struct tf_session *tfs;
273 struct tf_dev_info *dev;
274 struct tbl_rm_db *tbl_db;
275 void *tbl_db_ptr = NULL;
276 struct tf_tbl_sram_get_info_parms iparms = { 0 };
277 struct tf_sram_mgr_free_parms fparms = { 0 };
278 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
279 bool allocated = false;
280 void *sram_handle = NULL;
282 TF_CHECK_PARMS2(tfp, parms);
284 /* Retrieve the session information */
285 rc = tf_session_get(tfp, &tfs, &dev);
289 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
292 "Failed to get em_ext_db from session, rc:%s\n",
296 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
298 rc = tf_session_get_sram_db(tfp, &sram_handle);
301 "Failed to get sram_handle from session, rc:%s\n",
306 iparms.rm_db = tbl_db->tbl_db[parms->dir];
307 iparms.dir = parms->dir;
308 iparms.tbl_type = parms->type;
310 rc = tf_tbl_sram_get_info(&iparms);
313 "%s: Failed to get table info:%s\n",
314 tf_dir_2_str(parms->dir),
315 tf_tbl_type_2_str(parms->type));
321 struct tf_sram_mgr_dump_parms dparms;
323 printf("%s: %s: %s\n", tf_dir_2_str(parms->dir),
324 tf_sram_slice_2_str(iparms.slice_size),
325 tf_sram_bank_2_str(iparms.bank_id));
327 dparms.dir = parms->dir;
328 dparms.bank_id = iparms.bank_id;
329 dparms.slice_size = iparms.slice_size;
331 rc = tf_sram_mgr_dump(sram_handle, &dparms);
335 aparms.sram_offset = parms->idx;
336 aparms.slice_size = iparms.slice_size;
337 aparms.bank_id = iparms.bank_id;
338 aparms.dir = parms->dir;
339 aparms.is_allocated = &allocated;
341 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
342 if (rc || !allocated) {
344 "%s: Free of invalid entry:%s idx(%d):(%s)\n",
345 tf_dir_2_str(parms->dir),
346 tf_tbl_type_2_str(parms->type),
353 fparms.rm_db = tbl_db->tbl_db[parms->dir];
354 fparms.tbl_type = parms->type;
355 fparms.sram_offset = parms->idx;
356 fparms.slice_size = iparms.slice_size;
357 fparms.bank_id = iparms.bank_id;
358 fparms.dir = parms->dir;
359 #if (STATS_CLEAR_ON_READ_SUPPORT == 0)
362 rc = tf_sram_mgr_free(sram_handle, &fparms);
365 "%s: Failed to free entry:%s idx(%d)\n",
366 tf_dir_2_str(parms->dir),
367 tf_tbl_type_2_str(parms->type),
375 struct tf_sram_mgr_dump_parms dparms;
377 printf("%s: %s: %s\n", tf_dir_2_str(parms->dir),
378 tf_sram_slice_2_str(iparms.slice_size),
379 tf_sram_bank_2_str(iparms.bank_id));
381 dparms.dir = parms->dir;
382 dparms.bank_id = iparms.bank_id;
383 dparms.slice_size = iparms.slice_size;
385 rc = tf_sram_mgr_dump(sram_handle, &dparms);
392 tf_tbl_sram_set(struct tf *tfp,
393 struct tf_tbl_set_parms *parms)
398 struct tf_rm_get_hcapi_parms hparms = { 0 };
399 struct tf_session *tfs;
400 struct tf_dev_info *dev;
401 struct tbl_rm_db *tbl_db;
402 void *tbl_db_ptr = NULL;
403 struct tf_tbl_sram_get_info_parms iparms = { 0 };
404 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
405 void *sram_handle = NULL;
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 aparms.sram_offset = parms->idx;
446 aparms.slice_size = iparms.slice_size;
447 aparms.bank_id = iparms.bank_id;
448 aparms.dir = parms->dir;
449 aparms.is_allocated = &allocated;
450 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
451 if (rc || !allocated) {
453 "%s: Entry not allocated:%s idx(%d):(%s)\n",
454 tf_dir_2_str(parms->dir),
455 tf_tbl_type_2_str(parms->type),
463 hparms.rm_db = tbl_db->tbl_db[parms->dir];
464 hparms.subtype = parms->type;
465 hparms.hcapi_type = &hcapi_type;
466 rc = tf_rm_get_hcapi_type(&hparms);
469 "%s, Failed type lookup, type:%s, rc:%s\n",
470 tf_dir_2_str(parms->dir),
471 tf_tbl_type_2_str(parms->type),
476 rc = tf_msg_set_tbl_entry(tfp,
479 parms->data_sz_in_bytes,
484 "%s, Set failed, type:%s, rc:%s\n",
485 tf_dir_2_str(parms->dir),
486 tf_tbl_type_2_str(parms->type),
494 tf_tbl_sram_get(struct tf *tfp,
495 struct tf_tbl_get_parms *parms)
500 struct tf_rm_get_hcapi_parms hparms = { 0 };
501 struct tf_session *tfs;
502 struct tf_dev_info *dev;
503 struct tbl_rm_db *tbl_db;
504 void *tbl_db_ptr = NULL;
505 struct tf_tbl_sram_get_info_parms iparms = { 0 };
506 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
507 void *sram_handle = NULL;
509 TF_CHECK_PARMS3(tfp, parms, parms->data);
511 /* Retrieve the session information */
512 rc = tf_session_get(tfp, &tfs, &dev);
516 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
519 "Failed to get em_ext_db from session, rc:%s\n",
523 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
525 rc = tf_session_get_sram_db(tfp, &sram_handle);
528 "Failed to get sram_handle from session, rc:%s\n",
533 iparms.rm_db = tbl_db->tbl_db[parms->dir];
534 iparms.dir = parms->dir;
535 iparms.tbl_type = parms->type;
537 rc = tf_tbl_sram_get_info(&iparms);
540 "%s: Failed to get table info:%s\n",
541 tf_dir_2_str(parms->dir),
542 tf_tbl_type_2_str(parms->type));
546 aparms.sram_offset = parms->idx;
547 aparms.slice_size = iparms.slice_size;
548 aparms.bank_id = iparms.bank_id;
549 aparms.dir = parms->dir;
550 aparms.is_allocated = &allocated;
552 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
553 if (rc || !allocated) {
555 "%s: Entry not allocated:%s idx(%d):(%s)\n",
556 tf_dir_2_str(parms->dir),
557 tf_tbl_type_2_str(parms->type),
565 hparms.rm_db = tbl_db->tbl_db[parms->dir];
566 hparms.subtype = parms->type;
567 hparms.hcapi_type = &hcapi_type;
568 rc = tf_rm_get_hcapi_type(&hparms);
571 "%s, Failed type lookup, type:%s, rc:%s\n",
572 tf_dir_2_str(parms->dir),
573 tf_tbl_type_2_str(parms->type),
579 rc = tf_msg_get_tbl_entry(tfp,
582 parms->data_sz_in_bytes,
587 "%s, Get failed, type:%s, rc:%s\n",
588 tf_dir_2_str(parms->dir),
589 tf_tbl_type_2_str(parms->type),
597 tf_tbl_sram_bulk_get(struct tf *tfp,
598 struct tf_tbl_get_bulk_parms *parms)
602 struct tf_rm_get_hcapi_parms hparms = { 0 };
603 struct tf_tbl_sram_get_info_parms iparms = { 0 };
604 struct tf_session *tfs;
605 struct tf_dev_info *dev;
606 struct tbl_rm_db *tbl_db;
607 void *tbl_db_ptr = NULL;
609 struct tf_sram_mgr_is_allocated_parms aparms = { 0 };
610 bool allocated = false;
611 void *sram_handle = NULL;
613 TF_CHECK_PARMS2(tfp, parms);
615 /* Retrieve the session information */
616 rc = tf_session_get(tfp, &tfs, &dev);
620 rc = tf_session_get_db(tfp, TF_MODULE_TYPE_TABLE, &tbl_db_ptr);
623 "Failed to get em_ext_db from session, rc:%s\n",
627 tbl_db = (struct tbl_rm_db *)tbl_db_ptr;
629 rc = tf_session_get_sram_db(tfp, &sram_handle);
632 "Failed to get sram_handle from session, rc:%s\n",
637 iparms.rm_db = tbl_db->tbl_db[parms->dir];
638 iparms.dir = parms->dir;
639 iparms.tbl_type = parms->type;
641 rc = tf_tbl_sram_get_info(&iparms);
644 "%s: Failed to get table info:%s\n",
645 tf_dir_2_str(parms->dir),
646 tf_tbl_type_2_str(parms->type));
650 /* Validate the start offset and the end offset is allocated
651 * This API is only used for statistics. 8 Byte entry allocation
654 aparms.sram_offset = parms->starting_idx;
655 aparms.slice_size = iparms.slice_size;
656 aparms.bank_id = iparms.bank_id;
657 aparms.dir = parms->dir;
658 aparms.is_allocated = &allocated;
659 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
660 if (rc || !allocated) {
662 "%s: Entry not allocated:%s starting_idx(%d):(%s)\n",
663 tf_dir_2_str(parms->dir),
664 tf_tbl_type_2_str(parms->type),
670 idx = parms->starting_idx + parms->num_entries - 1;
671 aparms.sram_offset = idx;
672 rc = tf_sram_mgr_is_allocated(sram_handle, &aparms);
673 if (rc || !allocated) {
675 "%s: Entry not allocated:%s last_idx(%d):(%s)\n",
676 tf_dir_2_str(parms->dir),
677 tf_tbl_type_2_str(parms->type),
684 hparms.rm_db = tbl_db->tbl_db[parms->dir];
685 hparms.subtype = parms->type;
686 hparms.hcapi_type = &hcapi_type;
687 rc = tf_rm_get_hcapi_type(&hparms);
690 "%s, Failed type lookup, type:%s, rc:%s\n",
691 tf_dir_2_str(parms->dir),
692 tf_tbl_type_2_str(parms->type),
697 /* Get the entries */
698 rc = tf_msg_bulk_get_tbl_entry(tfp,
703 parms->entry_sz_in_bytes,
704 parms->physical_mem_addr);
707 "%s, Bulk get failed, type:%s, rc:%s\n",
708 tf_dir_2_str(parms->dir),
709 tf_tbl_type_2_str(parms->type),