1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Broadcom
6 /* Truflow Table APIs and supporting code */
12 #include <sys/param.h>
13 #include <rte_common.h>
14 #include <rte_errno.h>
15 #include "hsi_struct_def_dpdk.h"
23 #include "tf_resources.h"
26 #define PTU_PTE_VALID 0x1UL
27 #define PTU_PTE_LAST 0x2UL
28 #define PTU_PTE_NEXT_TO_LAST 0x4UL
30 /* Number of pointers per page_size */
31 #define MAX_PAGE_PTRS(page_size) ((page_size) / sizeof(void *))
33 #define TF_EM_PG_SZ_4K (1 << 12)
34 #define TF_EM_PG_SZ_8K (1 << 13)
35 #define TF_EM_PG_SZ_64K (1 << 16)
36 #define TF_EM_PG_SZ_256K (1 << 18)
37 #define TF_EM_PG_SZ_1M (1 << 20)
38 #define TF_EM_PG_SZ_2M (1 << 21)
39 #define TF_EM_PG_SZ_4M (1 << 22)
40 #define TF_EM_PG_SZ_1G (1 << 30)
42 #define TF_EM_CTX_ID_INVALID 0xFFFF
44 #define TF_EM_MIN_ENTRIES (1 << 15) /* 32K */
45 #define TF_EM_MAX_ENTRIES (1 << 27) /* 128M */
48 * Function to free a page table
51 * Pointer to the page table to free
54 tf_em_free_pg_tbl(struct tf_em_page_tbl *tp)
58 for (i = 0; i < tp->pg_count; i++) {
59 if (!tp->pg_va_tbl[i]) {
61 "No map for page %d table %016" PRIu64 "\n",
63 (uint64_t)(uintptr_t)tp);
67 tfp_free(tp->pg_va_tbl[i]);
68 tp->pg_va_tbl[i] = NULL;
72 tfp_free(tp->pg_va_tbl);
74 tfp_free(tp->pg_pa_tbl);
79 * Function to free an EM table
82 * Pointer to the EM table to free
85 tf_em_free_page_table(struct tf_em_table *tbl)
87 struct tf_em_page_tbl *tp;
90 for (i = 0; i < tbl->num_lvl; i++) {
94 "EEM: Freeing page table: size %u lvl %d cnt %u\n",
99 tf_em_free_pg_tbl(tp);
103 tbl->l0_dma_addr = 0;
105 tbl->num_data_pages = 0;
109 * Allocation of page tables
112 * Pointer to a TruFlow handle
115 * Page count to allocate
122 * -ENOMEM - Out of memory
125 tf_em_alloc_pg_tbl(struct tf_em_page_tbl *tp,
130 struct tfp_calloc_parms parms;
132 parms.nitems = pg_count;
133 parms.size = sizeof(void *);
136 if (tfp_calloc(&parms) != 0)
139 tp->pg_va_tbl = parms.mem_va;
141 if (tfp_calloc(&parms) != 0) {
142 tfp_free(tp->pg_va_tbl);
146 tp->pg_pa_tbl = parms.mem_va;
149 tp->pg_size = pg_size;
151 for (i = 0; i < pg_count; i++) {
153 parms.size = pg_size;
154 parms.alignment = TF_EM_PAGE_ALIGNMENT;
156 if (tfp_calloc(&parms) != 0)
159 tp->pg_pa_tbl[i] = (uint64_t)(uintptr_t)parms.mem_pa;
160 tp->pg_va_tbl[i] = parms.mem_va;
162 memset(tp->pg_va_tbl[i], 0, pg_size);
169 tf_em_free_pg_tbl(tp);
174 * Allocates EM page tables
177 * Table to allocate pages for
181 * -ENOMEM - Out of memory
184 tf_em_alloc_page_table(struct tf_em_table *tbl)
186 struct tf_em_page_tbl *tp;
191 for (i = 0; i < tbl->num_lvl; i++) {
192 tp = &tbl->pg_tbl[i];
194 rc = tf_em_alloc_pg_tbl(tp,
199 "Failed to allocate page table: lvl: %d\n",
204 for (j = 0; j < tp->pg_count; j++) {
206 "EEM: Allocated page table: size %u lvl %d cnt"
211 (uint32_t *)tp->pg_va_tbl[j],
212 (uint32_t *)(uintptr_t)tp->pg_pa_tbl[j]);
218 tf_em_free_page_table(tbl);
223 * Links EM page tables
226 * Pointer to page table
229 * Pointer to the next page table
232 * Flag controlling if the page table is last
235 tf_em_link_page_table(struct tf_em_page_tbl *tp,
236 struct tf_em_page_tbl *tp_next,
239 uint64_t *pg_pa = tp_next->pg_pa_tbl;
246 for (i = 0; i < tp->pg_count; i++) {
247 pg_va = tp->pg_va_tbl[i];
249 for (j = 0; j < MAX_PAGE_PTRS(tp->pg_size); j++) {
250 if (k == tp_next->pg_count - 2 && set_pte_last)
251 valid = PTU_PTE_NEXT_TO_LAST | PTU_PTE_VALID;
252 else if (k == tp_next->pg_count - 1 && set_pte_last)
253 valid = PTU_PTE_LAST | PTU_PTE_VALID;
255 valid = PTU_PTE_VALID;
257 pg_va[j] = tfp_cpu_to_le_64(pg_pa[k] | valid);
258 if (++k >= tp_next->pg_count)
265 * Setup a EM page table
268 * Pointer to EM page table
271 tf_em_setup_page_table(struct tf_em_table *tbl)
273 struct tf_em_page_tbl *tp_next;
274 struct tf_em_page_tbl *tp;
275 bool set_pte_last = 0;
278 for (i = 0; i < tbl->num_lvl - 1; i++) {
279 tp = &tbl->pg_tbl[i];
280 tp_next = &tbl->pg_tbl[i + 1];
281 if (i == tbl->num_lvl - 2)
283 tf_em_link_page_table(tp, tp_next, set_pte_last);
286 tbl->l0_addr = tbl->pg_tbl[PT_LVL_0].pg_va_tbl[0];
287 tbl->l0_dma_addr = tbl->pg_tbl[PT_LVL_0].pg_pa_tbl[0];
291 * Given the page size, size of each data item (entry size),
292 * and the total number of entries needed, determine the number
293 * of page table levels and the number of data pages required.
302 * Number of entries needed
304 * [out] num_data_pages
305 * Number of pages required
308 * Success - Number of EM page levels required
309 * -ENOMEM - Out of memory
312 tf_em_size_page_tbl_lvl(uint32_t page_size,
314 uint32_t num_entries,
315 uint64_t *num_data_pages)
317 uint64_t lvl_data_size = page_size;
322 data_size = (uint64_t)num_entries * entry_size;
324 while (lvl_data_size < data_size) {
328 lvl_data_size = (uint64_t)MAX_PAGE_PTRS(page_size) *
330 else if (lvl == PT_LVL_2)
331 lvl_data_size = (uint64_t)MAX_PAGE_PTRS(page_size) *
332 MAX_PAGE_PTRS(page_size) * page_size;
337 *num_data_pages = roundup(data_size, page_size) / page_size;
343 * Return the number of page table pages needed to
344 * reference the given number of next level pages.
350 * Size of each EM page
353 * Number of EM page table pages
356 tf_em_page_tbl_pgcnt(uint32_t num_pages,
359 return roundup(num_pages, MAX_PAGE_PTRS(page_size)) /
360 MAX_PAGE_PTRS(page_size);
365 * Given the number of data pages, page_size and the maximum
366 * number of page table levels (already determined), size
367 * the number of page table pages required at each level.
370 * Max number of levels
372 * [in] num_data_pages
373 * Number of EM data pages
382 tf_em_size_page_tbls(int max_lvl,
383 uint64_t num_data_pages,
387 if (max_lvl == PT_LVL_0) {
388 page_cnt[PT_LVL_0] = num_data_pages;
389 } else if (max_lvl == PT_LVL_1) {
390 page_cnt[PT_LVL_1] = num_data_pages;
392 tf_em_page_tbl_pgcnt(page_cnt[PT_LVL_1], page_size);
393 } else if (max_lvl == PT_LVL_2) {
394 page_cnt[PT_LVL_2] = num_data_pages;
396 tf_em_page_tbl_pgcnt(page_cnt[PT_LVL_2], page_size);
398 tf_em_page_tbl_pgcnt(page_cnt[PT_LVL_1], page_size);
405 * Size the EM table based on capabilities
412 * - EINVAL - Parameter error
413 * - ENOMEM - Out of memory
416 tf_em_size_table(struct tf_em_table *tbl)
418 uint64_t num_data_pages;
421 uint32_t num_entries;
422 uint32_t cnt = TF_EM_MIN_ENTRIES;
424 /* Ignore entry if both size and number are zero */
425 if (!tbl->entry_size && !tbl->num_entries)
428 /* If only one is set then error */
429 if (!tbl->entry_size || !tbl->num_entries)
432 /* Determine number of page table levels and the number
433 * of data pages needed to process the given eem table.
435 if (tbl->type == RECORD_TABLE) {
437 * For action records just a memory size is provided. Work
438 * backwards to resolve to number of entries
440 num_entries = tbl->num_entries / tbl->entry_size;
441 if (num_entries < TF_EM_MIN_ENTRIES) {
442 num_entries = TF_EM_MIN_ENTRIES;
444 while (num_entries > cnt && cnt <= TF_EM_MAX_ENTRIES)
449 num_entries = tbl->num_entries;
452 max_lvl = tf_em_size_page_tbl_lvl(TF_EM_PAGE_SIZE,
457 PMD_DRV_LOG(WARNING, "EEM: Failed to size page table levels\n");
459 "table: %d data-sz: %016" PRIu64 " page-sz: %u\n",
461 (uint64_t)num_entries * tbl->entry_size,
466 tbl->num_lvl = max_lvl + 1;
467 tbl->num_data_pages = num_data_pages;
469 /* Determine the number of pages needed at each level */
470 page_cnt = tbl->page_cnt;
471 memset(page_cnt, 0, sizeof(tbl->page_cnt));
472 tf_em_size_page_tbls(max_lvl, num_data_pages, TF_EM_PAGE_SIZE,
475 PMD_DRV_LOG(INFO, "EEM: Sized page table: %d\n", tbl->type);
477 "EEM: lvls: %d sz: %016" PRIu64 " pgs: %016" PRIu64 " l0: %u l1: %u l2: %u\n",
479 (uint64_t)num_data_pages * TF_EM_PAGE_SIZE,
489 * Unregisters EM Ctx in Firmware
492 * Pointer to a TruFlow handle
495 * Pointer to a table scope control block
498 * Receive or transmit direction
501 tf_em_ctx_unreg(struct tf *tfp,
502 struct tf_tbl_scope_cb *tbl_scope_cb,
505 struct tf_em_ctx_mem_info *ctxp = &tbl_scope_cb->em_ctx_info[dir];
506 struct tf_em_table *tbl;
509 for (i = KEY0_TABLE; i < MAX_TABLE; i++) {
510 tbl = &ctxp->em_tables[i];
512 if (tbl->num_entries != 0 && tbl->entry_size != 0) {
513 tf_msg_em_mem_unrgtr(tfp, &tbl->ctx_id);
514 tf_em_free_page_table(tbl);
520 * Registers EM Ctx in Firmware
523 * Pointer to a TruFlow handle
526 * Pointer to a table scope control block
529 * Receive or transmit direction
533 * -ENOMEM - Out of Memory
536 tf_em_ctx_reg(struct tf *tfp,
537 struct tf_tbl_scope_cb *tbl_scope_cb,
540 struct tf_em_ctx_mem_info *ctxp = &tbl_scope_cb->em_ctx_info[dir];
541 struct tf_em_table *tbl;
545 for (i = KEY0_TABLE; i < MAX_TABLE; i++) {
546 tbl = &ctxp->em_tables[i];
548 if (tbl->num_entries && tbl->entry_size) {
549 rc = tf_em_size_table(tbl);
554 rc = tf_em_alloc_page_table(tbl);
558 tf_em_setup_page_table(tbl);
559 rc = tf_msg_em_mem_rgtr(tfp,
561 TF_EM_PAGE_SIZE_ENUM,
571 tf_em_ctx_unreg(tfp, tbl_scope_cb, dir);
576 * Validates EM number of entries requested
579 * Pointer to table scope control block to be populated
582 * Pointer to input parameters
586 * -EINVAL - Parameter error
589 tf_em_validate_num_entries(struct tf_tbl_scope_cb *tbl_scope_cb,
590 struct tf_alloc_tbl_scope_parms *parms)
594 if (parms->rx_mem_size_in_mb != 0) {
595 uint32_t key_b = 2 * ((parms->rx_max_key_sz_in_bits / 8) + 1);
596 uint32_t action_b = ((parms->rx_max_action_entry_sz_in_bits / 8)
598 uint32_t num_entries = (parms->rx_mem_size_in_mb *
599 TF_MEGABYTE) / (key_b + action_b);
601 if (num_entries < TF_EM_MIN_ENTRIES) {
602 PMD_DRV_LOG(ERR, "EEM: Insufficient memory requested:"
604 parms->rx_mem_size_in_mb);
608 cnt = TF_EM_MIN_ENTRIES;
609 while (num_entries > cnt &&
610 cnt <= TF_EM_MAX_ENTRIES)
613 if (cnt > TF_EM_MAX_ENTRIES) {
614 PMD_DRV_LOG(ERR, "EEM: Invalid number of Tx requested: "
616 (parms->tx_num_flows_in_k * TF_KILOBYTE));
620 parms->rx_num_flows_in_k = cnt / TF_KILOBYTE;
622 if ((parms->rx_num_flows_in_k * TF_KILOBYTE) <
624 (parms->rx_num_flows_in_k * TF_KILOBYTE) >
625 tbl_scope_cb->em_caps[TF_DIR_RX].max_entries_supported) {
627 "EEM: Invalid number of Rx flows "
628 "requested:%u max:%u\n",
629 parms->rx_num_flows_in_k * TF_KILOBYTE,
630 tbl_scope_cb->em_caps[TF_DIR_RX].max_entries_supported);
634 /* must be a power-of-2 supported value
635 * in the range 32K - 128M
637 cnt = TF_EM_MIN_ENTRIES;
638 while ((parms->rx_num_flows_in_k * TF_KILOBYTE) != cnt &&
639 cnt <= TF_EM_MAX_ENTRIES)
642 if (cnt > TF_EM_MAX_ENTRIES) {
644 "EEM: Invalid number of Rx requested: %u\n",
645 (parms->rx_num_flows_in_k * TF_KILOBYTE));
650 if (parms->tx_mem_size_in_mb != 0) {
651 uint32_t key_b = 2 * (parms->tx_max_key_sz_in_bits / 8 + 1);
652 uint32_t action_b = ((parms->tx_max_action_entry_sz_in_bits / 8)
654 uint32_t num_entries = (parms->tx_mem_size_in_mb *
655 (TF_KILOBYTE * TF_KILOBYTE)) /
658 if (num_entries < TF_EM_MIN_ENTRIES) {
660 "EEM: Insufficient memory requested:%uMB\n",
661 parms->rx_mem_size_in_mb);
665 cnt = TF_EM_MIN_ENTRIES;
666 while (num_entries > cnt &&
667 cnt <= TF_EM_MAX_ENTRIES)
670 if (cnt > TF_EM_MAX_ENTRIES) {
672 "EEM: Invalid number of Tx requested: %u\n",
673 (parms->tx_num_flows_in_k * TF_KILOBYTE));
677 parms->tx_num_flows_in_k = cnt / TF_KILOBYTE;
679 if ((parms->tx_num_flows_in_k * TF_KILOBYTE) <
681 (parms->tx_num_flows_in_k * TF_KILOBYTE) >
682 tbl_scope_cb->em_caps[TF_DIR_TX].max_entries_supported) {
684 "EEM: Invalid number of Tx flows "
685 "requested:%u max:%u\n",
686 (parms->tx_num_flows_in_k * TF_KILOBYTE),
687 tbl_scope_cb->em_caps[TF_DIR_TX].max_entries_supported);
691 cnt = TF_EM_MIN_ENTRIES;
692 while ((parms->tx_num_flows_in_k * TF_KILOBYTE) != cnt &&
693 cnt <= TF_EM_MAX_ENTRIES)
696 if (cnt > TF_EM_MAX_ENTRIES) {
698 "EEM: Invalid number of Tx requested: %u\n",
699 (parms->tx_num_flows_in_k * TF_KILOBYTE));
705 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[KEY0_TABLE].num_entries =
706 parms->rx_num_flows_in_k * TF_KILOBYTE;
707 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[KEY0_TABLE].entry_size =
708 parms->rx_max_key_sz_in_bits / 8;
710 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[KEY1_TABLE].num_entries =
711 parms->rx_num_flows_in_k * TF_KILOBYTE;
712 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[KEY1_TABLE].entry_size =
713 parms->rx_max_key_sz_in_bits / 8;
715 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[RECORD_TABLE].num_entries =
716 parms->rx_num_flows_in_k * TF_KILOBYTE;
717 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[RECORD_TABLE].entry_size =
718 parms->tx_max_action_entry_sz_in_bits / 8;
720 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[EFC_TABLE].num_entries =
724 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[KEY0_TABLE].num_entries =
725 parms->tx_num_flows_in_k * TF_KILOBYTE;
726 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[KEY0_TABLE].entry_size =
727 parms->tx_max_key_sz_in_bits / 8;
729 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[KEY1_TABLE].num_entries =
730 parms->tx_num_flows_in_k * TF_KILOBYTE;
731 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[KEY1_TABLE].entry_size =
732 parms->tx_max_key_sz_in_bits / 8;
734 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[RECORD_TABLE].num_entries =
735 parms->tx_num_flows_in_k * TF_KILOBYTE;
736 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[RECORD_TABLE].entry_size =
737 parms->tx_max_action_entry_sz_in_bits / 8;
739 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[EFC_TABLE].num_entries =
746 * Internal function to set a Table Entry. Supports all internal Table Types
749 * Pointer to TruFlow handle
752 * Pointer to input parameters
756 * -EINVAL - Parameter error
759 tf_set_tbl_entry_internal(struct tf *tfp,
760 struct tf_set_tbl_entry_parms *parms)
765 struct bitalloc *session_pool;
766 struct tf_session *tfs = (struct tf_session *)(tfp->session->core_data);
768 /* Lookup the pool using the table type of the element */
769 rc = tf_rm_lookup_tbl_type_pool(tfs,
773 /* Error logging handled by tf_rm_lookup_tbl_type_pool */
779 if (parms->type != TF_TBL_TYPE_FULL_ACT_RECORD &&
780 parms->type != TF_TBL_TYPE_ACT_SP_SMAC_IPV4) {
782 "dir:%d, Type not supported, type:%d\n",
788 /* Adjust the returned index/offset as there is no guarantee
789 * that the start is 0 at time of RM allocation
791 tf_rm_convert_index(tfs,
794 TF_RM_CONVERT_RM_BASE,
798 /* Verify that the entry has been previously allocated */
799 id = ba_inuse(session_pool, index);
802 "dir:%d, Invalid or not allocated index, type:%d, idx:%d\n",
810 rc = tf_msg_set_tbl_entry(tfp,
813 parms->data_sz_in_bytes,
818 "dir:%d, Set failed, type:%d, rc:%d\n",
828 * Internal function to get a Table Entry. Supports all Table Types
829 * except the TF_TBL_TYPE_EXT as that is handled as a table scope.
832 * Pointer to TruFlow handle
835 * Pointer to input parameters
839 * -EINVAL - Parameter error
842 tf_get_tbl_entry_internal(struct tf *tfp,
843 struct tf_get_tbl_entry_parms *parms)
848 struct bitalloc *session_pool;
849 struct tf_session *tfs = (struct tf_session *)(tfp->session->core_data);
851 /* Lookup the pool using the table type of the element */
852 rc = tf_rm_lookup_tbl_type_pool(tfs,
856 /* Error logging handled by tf_rm_lookup_tbl_type_pool */
862 /* Adjust the returned index/offset as there is no guarantee
863 * that the start is 0 at time of RM allocation
865 tf_rm_convert_index(tfs,
868 TF_RM_CONVERT_RM_BASE,
872 /* Verify that the entry has been previously allocated */
873 id = ba_inuse(session_pool, index);
876 "dir:%d, Invalid or not allocated index, type:%d, idx:%d\n",
884 rc = tf_msg_get_tbl_entry(tfp,
887 parms->data_sz_in_bytes,
892 "dir:%d, Get failed, type:%d, rc:%d\n",
903 * Allocate Tbl entry from the Shadow DB. Shadow DB is searched for
904 * the requested entry. If found the ref count is incremente and
910 * Allocation parameters
913 * 0 - Success, entry found and ref count incremented
914 * -ENOENT - Failure, entry not found
917 tf_alloc_tbl_entry_shadow(struct tf_session *tfs __rte_unused,
918 struct tf_alloc_tbl_entry_parms *parms __rte_unused)
921 "dir:%d, Entry Alloc with search not supported\n",
929 * Free Tbl entry from the Shadow DB. Shadow DB is searched for
930 * the requested entry. If found the ref count is decremente and
931 * new ref_count returned.
936 * Allocation parameters
939 * 0 - Success, entry found and ref count decremented
940 * -ENOENT - Failure, entry not found
943 tf_free_tbl_entry_shadow(struct tf_session *tfs,
944 struct tf_free_tbl_entry_parms *parms)
947 "dir:%d, Entry Free with search not supported\n",
952 #endif /* TF_SHADOW */
955 * Create External Tbl pool of memory indexes.
962 * pointer to the table scope
964 * id of the table scope
966 * number of entries to write
967 * [in] entry_sz_bytes
971 * 0 - Success, entry allocated - no search support
972 * -ENOMEM -EINVAL -EOPNOTSUPP
973 * - Failure, entry not allocated, out of resources
976 tf_create_tbl_pool_external(struct tf_session *session,
978 struct tf_tbl_scope_cb *tbl_scope_cb,
979 uint32_t table_scope_id,
980 uint32_t num_entries,
981 uint32_t entry_sz_bytes)
984 struct tfp_calloc_parms parms;
987 struct stack *pool = &tbl_scope_cb->ext_pool[dir][TF_EXT_POOL_0];
989 parms.nitems = num_entries;
990 parms.size = sizeof(uint32_t);
993 if (tfp_calloc(&parms) != 0) {
994 PMD_DRV_LOG(ERR, "%d: TBL: external pool failure %s\n",
995 dir, strerror(-ENOMEM));
999 /* Create empty stack
1001 rc = stack_init(num_entries, parms.mem_va, pool);
1004 PMD_DRV_LOG(ERR, "%d: TBL: stack init failure %s\n",
1005 dir, strerror(-rc));
1009 /* Save the malloced memory address so that it can
1010 * be freed when the table scope is freed.
1012 tbl_scope_cb->ext_pool_mem[dir][TF_EXT_POOL_0] =
1013 (uint32_t *)parms.mem_va;
1015 /* Fill pool with indexes
1017 j = num_entries * entry_sz_bytes - 1;
1019 for (i = 0; i < num_entries; i++) {
1020 rc = stack_push(pool, j);
1022 PMD_DRV_LOG(ERR, "%d TBL: stack failure %s\n",
1023 dir, strerror(-rc));
1026 j -= entry_sz_bytes;
1029 if (!stack_is_full(pool)) {
1031 PMD_DRV_LOG(ERR, "%d TBL: stack failure %s\n",
1032 dir, strerror(-rc));
1035 /* Set the table scope associated with the pool
1037 session->ext_pool_2_scope[dir][TF_EXT_POOL_0] = table_scope_id;
1041 tfp_free((void *)parms.mem_va);
1046 * Destroy External Tbl pool of memory indexes.
1049 * Pointer to session
1053 * pointer to the table scope
1057 tf_destroy_tbl_pool_external(struct tf_session *session,
1059 struct tf_tbl_scope_cb *tbl_scope_cb)
1061 uint32_t *ext_pool_mem =
1062 tbl_scope_cb->ext_pool_mem[dir][TF_EXT_POOL_0];
1064 tfp_free(ext_pool_mem);
1066 /* Set the table scope associated with the pool
1068 session->ext_pool_2_scope[dir][TF_EXT_POOL_0] = TF_TBL_SCOPE_INVALID;
1072 * Allocate External Tbl entry from the Session Pool.
1075 * Pointer to Truflow Handle
1077 * Allocation parameters
1080 * 0 - Success, entry allocated - no search support
1081 * -ENOMEM -EINVAL -EOPNOTSUPP
1082 * - Failure, entry not allocated, out of resources
1085 tf_alloc_tbl_entry_pool_external(struct tf *tfp,
1086 struct tf_alloc_tbl_entry_parms *parms)
1090 struct tf_session *tfs;
1091 uint32_t tbl_scope_id;
1092 struct tf_tbl_scope_cb *tbl_scope_cb;
1095 /* Check parameters */
1096 if (tfp == NULL || parms == NULL) {
1097 PMD_DRV_LOG(ERR, "Invalid parameters\n");
1101 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1103 "dir:%d, Session info invalid\n",
1108 tfs = (struct tf_session *)(tfp->session->core_data);
1110 if (parms->type != TF_TBL_TYPE_EXT) {
1112 "dir:%d, Type not supported, type:%d\n",
1118 /* Get the pool info from the table scope
1120 tbl_scope_id = tfs->ext_pool_2_scope[parms->dir][TF_EXT_POOL_0];
1121 tbl_scope_cb = tbl_scope_cb_find(tfs, tbl_scope_id);
1123 if (tbl_scope_cb == NULL) {
1125 "dir:%d, table scope not allocated\n",
1129 pool = &tbl_scope_cb->ext_pool[parms->dir][TF_EXT_POOL_0];
1131 /* Allocate an element
1133 rc = stack_pop(pool, &index);
1137 "dir:%d, Allocation failed, type:%d\n",
1147 * Allocate Internal Tbl entry from the Session Pool.
1150 * Pointer to Truflow Handle
1152 * Allocation parameters
1155 * 0 - Success, entry found and ref count decremented
1156 * -ENOMEM - Failure, entry not allocated, out of resources
1159 tf_alloc_tbl_entry_pool_internal(struct tf *tfp,
1160 struct tf_alloc_tbl_entry_parms *parms)
1166 struct bitalloc *session_pool;
1167 struct tf_session *tfs;
1169 /* Check parameters */
1170 if (tfp == NULL || parms == NULL) {
1171 PMD_DRV_LOG(ERR, "Invalid parameters\n");
1175 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1177 "dir:%d, Session info invalid\n",
1182 tfs = (struct tf_session *)(tfp->session->core_data);
1184 if (parms->type != TF_TBL_TYPE_FULL_ACT_RECORD &&
1185 parms->type != TF_TBL_TYPE_ACT_SP_SMAC &&
1186 parms->type != TF_TBL_TYPE_ACT_SP_SMAC_IPV4 &&
1187 parms->type != TF_TBL_TYPE_ACT_ENCAP_8B &&
1188 parms->type != TF_TBL_TYPE_ACT_ENCAP_16B &&
1189 parms->type != TF_TBL_TYPE_ACT_ENCAP_64B) {
1191 "dir:%d, Type not supported, type:%d\n",
1197 /* Lookup the pool using the table type of the element */
1198 rc = tf_rm_lookup_tbl_type_pool(tfs,
1202 /* Error logging handled by tf_rm_lookup_tbl_type_pool */
1206 id = ba_alloc(session_pool);
1208 free_cnt = ba_free_count(session_pool);
1211 "dir:%d, Allocation failed, type:%d, free:%d\n",
1218 /* Adjust the returned index/offset as there is no guarantee
1219 * that the start is 0 at time of RM allocation
1221 tf_rm_convert_index(tfs,
1224 TF_RM_CONVERT_ADD_BASE,
1232 * Free External Tbl entry to the session pool.
1235 * Pointer to Truflow Handle
1237 * Allocation parameters
1240 * 0 - Success, entry freed
1242 * - Failure, entry not successfully freed for these reasons
1248 tf_free_tbl_entry_pool_external(struct tf *tfp,
1249 struct tf_free_tbl_entry_parms *parms)
1252 struct tf_session *tfs;
1254 uint32_t tbl_scope_id;
1255 struct tf_tbl_scope_cb *tbl_scope_cb;
1258 /* Check parameters */
1259 if (tfp == NULL || parms == NULL) {
1260 PMD_DRV_LOG(ERR, "Invalid parameters\n");
1264 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1266 "dir:%d, Session info invalid\n",
1271 tfs = (struct tf_session *)(tfp->session->core_data);
1273 if (parms->type != TF_TBL_TYPE_EXT) {
1275 "dir:%d, Type not supported, type:%d\n",
1281 /* Get the pool info from the table scope
1283 tbl_scope_id = tfs->ext_pool_2_scope[parms->dir][TF_EXT_POOL_0];
1284 tbl_scope_cb = tbl_scope_cb_find(tfs, tbl_scope_id);
1286 if (tbl_scope_cb == NULL) {
1288 "dir:%d, table scope error\n",
1292 pool = &tbl_scope_cb->ext_pool[parms->dir][TF_EXT_POOL_0];
1296 rc = stack_push(pool, index);
1300 "dir:%d, consistency error, stack full, type:%d, idx:%d\n",
1309 * Free Internal Tbl entry from the Session Pool.
1312 * Pointer to Truflow Handle
1314 * Allocation parameters
1317 * 0 - Success, entry found and ref count decremented
1318 * -ENOMEM - Failure, entry not allocated, out of resources
1321 tf_free_tbl_entry_pool_internal(struct tf *tfp,
1322 struct tf_free_tbl_entry_parms *parms)
1326 struct bitalloc *session_pool;
1327 struct tf_session *tfs;
1330 /* Check parameters */
1331 if (tfp == NULL || parms == NULL) {
1332 PMD_DRV_LOG(ERR, "Invalid parameters\n");
1336 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1338 "dir:%d, Session info invalid\n",
1343 tfs = (struct tf_session *)(tfp->session->core_data);
1345 if (parms->type != TF_TBL_TYPE_FULL_ACT_RECORD &&
1346 parms->type != TF_TBL_TYPE_ACT_SP_SMAC &&
1347 parms->type != TF_TBL_TYPE_ACT_SP_SMAC_IPV4 &&
1348 parms->type != TF_TBL_TYPE_ACT_ENCAP_8B &&
1349 parms->type != TF_TBL_TYPE_ACT_ENCAP_16B &&
1350 parms->type != TF_TBL_TYPE_ACT_ENCAP_64B) {
1352 "dir:%d, Type not supported, type:%d\n",
1358 /* Lookup the pool using the table type of the element */
1359 rc = tf_rm_lookup_tbl_type_pool(tfs,
1363 /* Error logging handled by tf_rm_lookup_tbl_type_pool */
1369 /* Adjust the returned index/offset as there is no guarantee
1370 * that the start is 0 at time of RM allocation
1372 tf_rm_convert_index(tfs,
1375 TF_RM_CONVERT_RM_BASE,
1379 /* Check if element was indeed allocated */
1380 id = ba_inuse_free(session_pool, index);
1383 "dir:%d, Element not previously alloc'ed, type:%d, idx:%d\n",
1393 /* API defined in tf_tbl.h */
1395 tf_init_tbl_pool(struct tf_session *session)
1399 for (dir = 0; dir < TF_DIR_MAX; dir++) {
1400 session->ext_pool_2_scope[dir][TF_EXT_POOL_0] =
1401 TF_TBL_SCOPE_INVALID;
1405 /* API defined in tf_em.h */
1406 struct tf_tbl_scope_cb *
1407 tbl_scope_cb_find(struct tf_session *session,
1408 uint32_t tbl_scope_id)
1412 /* Check that id is valid */
1413 i = ba_inuse(session->tbl_scope_pool_rx, tbl_scope_id);
1417 for (i = 0; i < TF_NUM_TBL_SCOPE; i++) {
1418 if (session->tbl_scopes[i].tbl_scope_id == tbl_scope_id)
1419 return &session->tbl_scopes[i];
1425 /* API defined in tf_core.h */
1427 tf_free_eem_tbl_scope_cb(struct tf *tfp,
1428 struct tf_free_tbl_scope_parms *parms)
1432 struct tf_tbl_scope_cb *tbl_scope_cb;
1433 struct tf_session *session;
1435 session = (struct tf_session *)(tfp->session->core_data);
1437 tbl_scope_cb = tbl_scope_cb_find(session,
1438 parms->tbl_scope_id);
1440 if (tbl_scope_cb == NULL)
1443 /* Free Table control block */
1444 ba_free(session->tbl_scope_pool_rx, tbl_scope_cb->index);
1446 /* free table scope locks */
1447 for (dir = 0; dir < TF_DIR_MAX; dir++) {
1448 /* Free associated external pools
1450 tf_destroy_tbl_pool_external(session,
1455 HWRM_TF_EXT_EM_OP_INPUT_OP_EXT_EM_DISABLE);
1457 /* free table scope and all associated resources */
1458 tf_em_ctx_unreg(tfp, tbl_scope_cb, dir);
1464 /* API defined in tf_em.h */
1466 tf_alloc_eem_tbl_scope(struct tf *tfp,
1467 struct tf_alloc_tbl_scope_parms *parms)
1471 struct tf_tbl_scope_cb *tbl_scope_cb;
1472 struct tf_em_table *em_tables;
1474 struct tf_session *session;
1475 struct tf_free_tbl_scope_parms free_parms;
1477 /* check parameters */
1478 if (parms == NULL || tfp->session == NULL) {
1479 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1483 session = (struct tf_session *)tfp->session->core_data;
1485 /* Get Table Scope control block from the session pool */
1486 index = ba_alloc(session->tbl_scope_pool_rx);
1488 PMD_DRV_LOG(ERR, "EEM: Unable to allocate table scope "
1493 tbl_scope_cb = &session->tbl_scopes[index];
1494 tbl_scope_cb->index = index;
1495 tbl_scope_cb->tbl_scope_id = index;
1496 parms->tbl_scope_id = index;
1498 for (dir = 0; dir < TF_DIR_MAX; dir++) {
1499 rc = tf_msg_em_qcaps(tfp,
1501 &tbl_scope_cb->em_caps[dir]);
1504 "EEM: Unable to query for EEM capability\n");
1510 * Validate and setup table sizes
1512 if (tf_em_validate_num_entries(tbl_scope_cb, parms))
1515 for (dir = 0; dir < TF_DIR_MAX; dir++) {
1517 * Allocate tables and signal configuration to FW
1519 rc = tf_em_ctx_reg(tfp, tbl_scope_cb, dir);
1522 "EEM: Unable to register for EEM ctx\n");
1526 em_tables = tbl_scope_cb->em_ctx_info[dir].em_tables;
1527 rc = tf_msg_em_cfg(tfp,
1528 em_tables[KEY0_TABLE].num_entries,
1529 em_tables[KEY0_TABLE].ctx_id,
1530 em_tables[KEY1_TABLE].ctx_id,
1531 em_tables[RECORD_TABLE].ctx_id,
1532 em_tables[EFC_TABLE].ctx_id,
1536 "TBL: Unable to configure EEM in firmware\n");
1540 rc = tf_msg_em_op(tfp,
1542 HWRM_TF_EXT_EM_OP_INPUT_OP_EXT_EM_ENABLE);
1546 "EEM: Unable to enable EEM in firmware\n");
1550 /* Allocate the pool of offsets of the external memory.
1551 * Initially, this is a single fixed size pool for all external
1552 * actions related to a single table scope.
1554 rc = tf_create_tbl_pool_external(session,
1558 TF_EXT_POOL_ENTRY_CNT,
1559 TF_EXT_POOL_ENTRY_SZ_BYTES);
1562 "%d TBL: Unable to allocate idx pools %s\n",
1572 free_parms.tbl_scope_id = index;
1573 tf_free_eem_tbl_scope_cb(tfp, &free_parms);
1577 /* Free Table control block */
1578 ba_free(session->tbl_scope_pool_rx, tbl_scope_cb->index);
1582 /* API defined in tf_core.h */
1584 tf_set_tbl_entry(struct tf *tfp,
1585 struct tf_set_tbl_entry_parms *parms)
1588 struct tf_tbl_scope_cb *tbl_scope_cb;
1589 struct tf_session *session;
1591 if (tfp == NULL || parms == NULL || parms->data == NULL)
1594 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1596 "dir:%d, Session info invalid\n",
1601 if (parms->type == TF_TBL_TYPE_EXT) {
1603 uint32_t offset = TF_ACT_REC_INDEX_2_OFFSET(parms->idx);
1604 uint32_t tbl_scope_id;
1606 session = (struct tf_session *)(tfp->session->core_data);
1609 session->ext_pool_2_scope[parms->dir][TF_EXT_POOL_0];
1611 if (tbl_scope_id == TF_TBL_SCOPE_INVALID) {
1613 "dir:%d, Table scope not allocated\n",
1618 /* Get the table scope control block associated with the
1622 tbl_scope_cb = tbl_scope_cb_find(session, tbl_scope_id);
1624 if (tbl_scope_cb == NULL)
1627 /* External table, implicitly the Action table */
1628 base_addr = tf_em_get_table_page(tbl_scope_cb,
1632 if (base_addr == NULL) {
1634 "dir:%d, Base address lookup failed\n",
1639 offset %= TF_EM_PAGE_SIZE;
1640 rte_memcpy((char *)base_addr + offset,
1642 parms->data_sz_in_bytes);
1644 /* Internal table type processing */
1645 rc = tf_set_tbl_entry_internal(tfp, parms);
1648 "dir:%d, Set failed, type:%d, rc:%d\n",
1658 /* API defined in tf_core.h */
1660 tf_get_tbl_entry(struct tf *tfp,
1661 struct tf_get_tbl_entry_parms *parms)
1665 if (tfp == NULL || parms == NULL)
1668 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1670 "dir:%d, Session info invalid\n",
1675 if (parms->type == TF_TBL_TYPE_EXT) {
1677 "dir:%d, External table type not supported\n",
1682 /* Internal table type processing */
1683 rc = tf_get_tbl_entry_internal(tfp, parms);
1686 "dir:%d, Get failed, type:%d, rc:%d\n",
1695 /* API defined in tf_core.h */
1697 tf_alloc_tbl_scope(struct tf *tfp,
1698 struct tf_alloc_tbl_scope_parms *parms)
1702 /* check parameters */
1703 if (parms == NULL || tfp == NULL) {
1704 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1708 rc = tf_alloc_eem_tbl_scope(tfp, parms);
1713 /* API defined in tf_core.h */
1715 tf_free_tbl_scope(struct tf *tfp,
1716 struct tf_free_tbl_scope_parms *parms)
1720 /* check parameters */
1721 if (parms == NULL || tfp == NULL) {
1722 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1726 /* free table scope and all associated resources */
1727 rc = tf_free_eem_tbl_scope_cb(tfp, parms);
1732 /* API defined in tf_core.h */
1734 tf_alloc_tbl_entry(struct tf *tfp,
1735 struct tf_alloc_tbl_entry_parms *parms)
1738 #if (TF_SHADOW == 1)
1739 struct tf_session *tfs;
1740 #endif /* TF_SHADOW */
1742 /* Check parameters */
1743 if (parms == NULL || tfp == NULL) {
1744 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1748 * No shadow copy support for external tables, allocate and return
1750 if (parms->type == TF_TBL_TYPE_EXT) {
1751 rc = tf_alloc_tbl_entry_pool_external(tfp, parms);
1755 #if (TF_SHADOW == 1)
1756 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1758 "dir:%d, Session info invalid\n",
1763 tfs = (struct tf_session *)(tfp->session->core_data);
1765 /* Search the Shadow DB for requested element. If not found go
1766 * allocate one from the Session Pool
1768 if (parms->search_enable && tfs->shadow_copy) {
1769 rc = tf_alloc_tbl_entry_shadow(tfs, parms);
1770 /* Entry found and parms populated with return data */
1774 #endif /* TF_SHADOW */
1776 rc = tf_alloc_tbl_entry_pool_internal(tfp, parms);
1778 PMD_DRV_LOG(ERR, "dir%d, Alloc failed, rc:%d\n",
1785 /* API defined in tf_core.h */
1787 tf_free_tbl_entry(struct tf *tfp,
1788 struct tf_free_tbl_entry_parms *parms)
1791 #if (TF_SHADOW == 1)
1792 struct tf_session *tfs;
1793 #endif /* TF_SHADOW */
1795 /* Check parameters */
1796 if (parms == NULL || tfp == NULL) {
1797 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1801 * No shadow of external tables so just free the entry
1803 if (parms->type == TF_TBL_TYPE_EXT) {
1804 rc = tf_free_tbl_entry_pool_external(tfp, parms);
1808 #if (TF_SHADOW == 1)
1809 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1811 "dir:%d, Session info invalid\n",
1816 tfs = (struct tf_session *)(tfp->session->core_data);
1818 /* Search the Shadow DB for requested element. If not found go
1819 * allocate one from the Session Pool
1821 if (parms->search_enable && tfs->shadow_copy) {
1822 rc = tf_free_tbl_entry_shadow(tfs, parms);
1823 /* Entry free'ed and parms populated with return data */
1827 #endif /* TF_SHADOW */
1829 rc = tf_free_tbl_entry_pool_internal(tfp, parms);
1832 PMD_DRV_LOG(ERR, "dir:%d, Alloc failed, rc:%d\n",