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));
704 if (parms->rx_num_flows_in_k != 0 &&
705 (parms->rx_max_key_sz_in_bits / 8 == 0)) {
707 "EEM: Rx key size required: %u\n",
708 (parms->rx_max_key_sz_in_bits));
712 if (parms->tx_num_flows_in_k != 0 &&
713 (parms->tx_max_key_sz_in_bits / 8 == 0)) {
715 "EEM: Tx key size required: %u\n",
716 (parms->tx_max_key_sz_in_bits));
720 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[KEY0_TABLE].num_entries =
721 parms->rx_num_flows_in_k * TF_KILOBYTE;
722 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[KEY0_TABLE].entry_size =
723 parms->rx_max_key_sz_in_bits / 8;
725 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[KEY1_TABLE].num_entries =
726 parms->rx_num_flows_in_k * TF_KILOBYTE;
727 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[KEY1_TABLE].entry_size =
728 parms->rx_max_key_sz_in_bits / 8;
730 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[RECORD_TABLE].num_entries =
731 parms->rx_num_flows_in_k * TF_KILOBYTE;
732 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[RECORD_TABLE].entry_size =
733 parms->rx_max_action_entry_sz_in_bits / 8;
735 tbl_scope_cb->em_ctx_info[TF_DIR_RX].em_tables[EFC_TABLE].num_entries =
739 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[KEY0_TABLE].num_entries =
740 parms->tx_num_flows_in_k * TF_KILOBYTE;
741 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[KEY0_TABLE].entry_size =
742 parms->tx_max_key_sz_in_bits / 8;
744 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[KEY1_TABLE].num_entries =
745 parms->tx_num_flows_in_k * TF_KILOBYTE;
746 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[KEY1_TABLE].entry_size =
747 parms->tx_max_key_sz_in_bits / 8;
749 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[RECORD_TABLE].num_entries =
750 parms->tx_num_flows_in_k * TF_KILOBYTE;
751 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[RECORD_TABLE].entry_size =
752 parms->tx_max_action_entry_sz_in_bits / 8;
754 tbl_scope_cb->em_ctx_info[TF_DIR_TX].em_tables[EFC_TABLE].num_entries =
761 * Internal function to set a Table Entry. Supports all internal Table Types
764 * Pointer to TruFlow handle
767 * Pointer to input parameters
771 * -EINVAL - Parameter error
774 tf_set_tbl_entry_internal(struct tf *tfp,
775 struct tf_set_tbl_entry_parms *parms)
780 struct bitalloc *session_pool;
781 struct tf_session *tfs = (struct tf_session *)(tfp->session->core_data);
783 /* Lookup the pool using the table type of the element */
784 rc = tf_rm_lookup_tbl_type_pool(tfs,
788 /* Error logging handled by tf_rm_lookup_tbl_type_pool */
794 if (parms->type != TF_TBL_TYPE_FULL_ACT_RECORD &&
795 parms->type != TF_TBL_TYPE_ACT_SP_SMAC_IPV4) {
797 "dir:%d, Type not supported, type:%d\n",
803 /* Adjust the returned index/offset as there is no guarantee
804 * that the start is 0 at time of RM allocation
806 tf_rm_convert_index(tfs,
809 TF_RM_CONVERT_RM_BASE,
813 /* Verify that the entry has been previously allocated */
814 id = ba_inuse(session_pool, index);
817 "dir:%d, Invalid or not allocated index, type:%d, idx:%d\n",
825 rc = tf_msg_set_tbl_entry(tfp,
828 parms->data_sz_in_bytes,
833 "dir:%d, Set failed, type:%d, rc:%d\n",
843 * Internal function to get a Table Entry. Supports all Table Types
844 * except the TF_TBL_TYPE_EXT as that is handled as a table scope.
847 * Pointer to TruFlow handle
850 * Pointer to input parameters
854 * -EINVAL - Parameter error
857 tf_get_tbl_entry_internal(struct tf *tfp,
858 struct tf_get_tbl_entry_parms *parms)
863 struct bitalloc *session_pool;
864 struct tf_session *tfs = (struct tf_session *)(tfp->session->core_data);
866 /* Lookup the pool using the table type of the element */
867 rc = tf_rm_lookup_tbl_type_pool(tfs,
871 /* Error logging handled by tf_rm_lookup_tbl_type_pool */
877 /* Adjust the returned index/offset as there is no guarantee
878 * that the start is 0 at time of RM allocation
880 tf_rm_convert_index(tfs,
883 TF_RM_CONVERT_RM_BASE,
887 /* Verify that the entry has been previously allocated */
888 id = ba_inuse(session_pool, index);
891 "dir:%d, Invalid or not allocated index, type:%d, idx:%d\n",
899 rc = tf_msg_get_tbl_entry(tfp,
902 parms->data_sz_in_bytes,
907 "dir:%d, Get failed, type:%d, rc:%d\n",
918 * Allocate Tbl entry from the Shadow DB. Shadow DB is searched for
919 * the requested entry. If found the ref count is incremente and
925 * Allocation parameters
928 * 0 - Success, entry found and ref count incremented
929 * -ENOENT - Failure, entry not found
932 tf_alloc_tbl_entry_shadow(struct tf_session *tfs __rte_unused,
933 struct tf_alloc_tbl_entry_parms *parms __rte_unused)
936 "dir:%d, Entry Alloc with search not supported\n",
944 * Free Tbl entry from the Shadow DB. Shadow DB is searched for
945 * the requested entry. If found the ref count is decremente and
946 * new ref_count returned.
951 * Allocation parameters
954 * 0 - Success, entry found and ref count decremented
955 * -ENOENT - Failure, entry not found
958 tf_free_tbl_entry_shadow(struct tf_session *tfs,
959 struct tf_free_tbl_entry_parms *parms)
962 "dir:%d, Entry Free with search not supported\n",
967 #endif /* TF_SHADOW */
970 * Create External Tbl pool of memory indexes.
975 * pointer to the table scope
977 * number of entries to write
978 * [in] entry_sz_bytes
982 * 0 - Success, entry allocated - no search support
983 * -ENOMEM -EINVAL -EOPNOTSUPP
984 * - Failure, entry not allocated, out of resources
987 tf_create_tbl_pool_external(enum tf_dir dir,
988 struct tf_tbl_scope_cb *tbl_scope_cb,
989 uint32_t num_entries,
990 uint32_t entry_sz_bytes)
992 struct tfp_calloc_parms parms;
996 struct stack *pool = &tbl_scope_cb->ext_act_pool[dir];
998 parms.nitems = num_entries;
999 parms.size = sizeof(uint32_t);
1000 parms.alignment = 0;
1002 if (tfp_calloc(&parms) != 0) {
1003 PMD_DRV_LOG(ERR, "%d: TBL: external pool failure %s\n",
1004 dir, strerror(-ENOMEM));
1008 /* Create empty stack
1010 rc = stack_init(num_entries, parms.mem_va, pool);
1013 PMD_DRV_LOG(ERR, "%d: TBL: stack init failure %s\n",
1014 dir, strerror(-rc));
1018 /* Save the malloced memory address so that it can
1019 * be freed when the table scope is freed.
1021 tbl_scope_cb->ext_act_pool_mem[dir] = (uint32_t *)parms.mem_va;
1023 /* Fill pool with indexes in reverse
1025 j = (num_entries - 1) * entry_sz_bytes;
1027 for (i = 0; i < num_entries; i++) {
1028 rc = stack_push(pool, j);
1030 PMD_DRV_LOG(ERR, "%s TBL: stack failure %s\n",
1031 tf_dir_2_str(dir), strerror(-rc));
1036 PMD_DRV_LOG(ERR, "%d TBL: invalid offset (%d)\n",
1040 j -= entry_sz_bytes;
1043 if (!stack_is_full(pool)) {
1045 PMD_DRV_LOG(ERR, "%d TBL: stack failure %s\n",
1046 dir, strerror(-rc));
1051 tfp_free((void *)parms.mem_va);
1056 * Destroy External Tbl pool of memory indexes.
1061 * pointer to the table scope
1065 tf_destroy_tbl_pool_external(enum tf_dir dir,
1066 struct tf_tbl_scope_cb *tbl_scope_cb)
1068 uint32_t *ext_act_pool_mem =
1069 tbl_scope_cb->ext_act_pool_mem[dir];
1071 tfp_free(ext_act_pool_mem);
1075 * Allocate External Tbl entry from the Session Pool.
1078 * Pointer to Truflow Handle
1080 * Allocation parameters
1083 * 0 - Success, entry allocated - no search support
1084 * -ENOMEM -EINVAL -EOPNOTSUPP
1085 * - Failure, entry not allocated, out of resources
1088 tf_alloc_tbl_entry_pool_external(struct tf *tfp,
1089 struct tf_alloc_tbl_entry_parms *parms)
1093 struct tf_session *tfs;
1094 struct tf_tbl_scope_cb *tbl_scope_cb;
1097 /* Check parameters */
1098 if (tfp == NULL || parms == NULL) {
1099 PMD_DRV_LOG(ERR, "Invalid parameters\n");
1103 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1105 "dir:%d, Session info invalid\n",
1110 tfs = (struct tf_session *)(tfp->session->core_data);
1112 /* Get the pool info from the table scope
1114 tbl_scope_cb = tbl_scope_cb_find(tfs, parms->tbl_scope_id);
1116 if (tbl_scope_cb == NULL) {
1118 "%s, table scope not allocated\n",
1119 tf_dir_2_str(parms->dir));
1122 pool = &tbl_scope_cb->ext_act_pool[parms->dir];
1124 /* Allocate an element
1126 rc = stack_pop(pool, &index);
1130 "dir:%d, Allocation failed, type:%d\n",
1140 * Allocate Internal Tbl entry from the Session Pool.
1143 * Pointer to Truflow Handle
1145 * Allocation parameters
1148 * 0 - Success, entry found and ref count decremented
1149 * -ENOMEM - Failure, entry not allocated, out of resources
1152 tf_alloc_tbl_entry_pool_internal(struct tf *tfp,
1153 struct tf_alloc_tbl_entry_parms *parms)
1159 struct bitalloc *session_pool;
1160 struct tf_session *tfs;
1162 /* Check parameters */
1163 if (tfp == NULL || parms == NULL) {
1164 PMD_DRV_LOG(ERR, "Invalid parameters\n");
1168 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1170 "dir:%d, Session info invalid\n",
1175 tfs = (struct tf_session *)(tfp->session->core_data);
1177 if (parms->type != TF_TBL_TYPE_FULL_ACT_RECORD &&
1178 parms->type != TF_TBL_TYPE_ACT_SP_SMAC &&
1179 parms->type != TF_TBL_TYPE_ACT_SP_SMAC_IPV4 &&
1180 parms->type != TF_TBL_TYPE_ACT_ENCAP_8B &&
1181 parms->type != TF_TBL_TYPE_ACT_ENCAP_16B &&
1182 parms->type != TF_TBL_TYPE_ACT_ENCAP_64B) {
1184 "dir:%d, Type not supported, type:%d\n",
1190 /* Lookup the pool using the table type of the element */
1191 rc = tf_rm_lookup_tbl_type_pool(tfs,
1195 /* Error logging handled by tf_rm_lookup_tbl_type_pool */
1199 id = ba_alloc(session_pool);
1201 free_cnt = ba_free_count(session_pool);
1204 "dir:%d, Allocation failed, type:%d, free:%d\n",
1211 /* Adjust the returned index/offset as there is no guarantee
1212 * that the start is 0 at time of RM allocation
1214 tf_rm_convert_index(tfs,
1217 TF_RM_CONVERT_ADD_BASE,
1225 * Free External Tbl entry to the session pool.
1228 * Pointer to Truflow Handle
1230 * Allocation parameters
1233 * 0 - Success, entry freed
1235 * - Failure, entry not successfully freed for these reasons
1241 tf_free_tbl_entry_pool_external(struct tf *tfp,
1242 struct tf_free_tbl_entry_parms *parms)
1245 struct tf_session *tfs;
1247 struct tf_tbl_scope_cb *tbl_scope_cb;
1250 /* Check parameters */
1251 if (tfp == NULL || parms == NULL) {
1252 PMD_DRV_LOG(ERR, "Invalid parameters\n");
1256 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1258 "dir:%d, Session info invalid\n",
1263 tfs = (struct tf_session *)(tfp->session->core_data);
1265 /* Get the pool info from the table scope
1267 tbl_scope_cb = tbl_scope_cb_find(tfs, parms->tbl_scope_id);
1269 if (tbl_scope_cb == NULL) {
1271 "dir:%d, Session info invalid\n",
1275 pool = &tbl_scope_cb->ext_act_pool[parms->dir];
1279 rc = stack_push(pool, index);
1283 "dir:%d, consistency error, stack full, type:%d, idx:%d\n",
1292 * Free Internal Tbl entry from the Session Pool.
1295 * Pointer to Truflow Handle
1297 * Allocation parameters
1300 * 0 - Success, entry found and ref count decremented
1301 * -ENOMEM - Failure, entry not allocated, out of resources
1304 tf_free_tbl_entry_pool_internal(struct tf *tfp,
1305 struct tf_free_tbl_entry_parms *parms)
1309 struct bitalloc *session_pool;
1310 struct tf_session *tfs;
1313 /* Check parameters */
1314 if (tfp == NULL || parms == NULL) {
1315 PMD_DRV_LOG(ERR, "Invalid parameters\n");
1319 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1321 "dir:%d, Session info invalid\n",
1326 tfs = (struct tf_session *)(tfp->session->core_data);
1328 if (parms->type != TF_TBL_TYPE_FULL_ACT_RECORD &&
1329 parms->type != TF_TBL_TYPE_ACT_SP_SMAC &&
1330 parms->type != TF_TBL_TYPE_ACT_SP_SMAC_IPV4 &&
1331 parms->type != TF_TBL_TYPE_ACT_ENCAP_8B &&
1332 parms->type != TF_TBL_TYPE_ACT_ENCAP_16B &&
1333 parms->type != TF_TBL_TYPE_ACT_ENCAP_64B) {
1335 "dir:%d, Type not supported, type:%d\n",
1341 /* Lookup the pool using the table type of the element */
1342 rc = tf_rm_lookup_tbl_type_pool(tfs,
1346 /* Error logging handled by tf_rm_lookup_tbl_type_pool */
1352 /* Adjust the returned index/offset as there is no guarantee
1353 * that the start is 0 at time of RM allocation
1355 tf_rm_convert_index(tfs,
1358 TF_RM_CONVERT_RM_BASE,
1362 /* Check if element was indeed allocated */
1363 id = ba_inuse_free(session_pool, index);
1366 "dir:%d, Element not previously alloc'ed, type:%d, idx:%d\n",
1376 /* API defined in tf_em.h */
1377 struct tf_tbl_scope_cb *
1378 tbl_scope_cb_find(struct tf_session *session,
1379 uint32_t tbl_scope_id)
1383 /* Check that id is valid */
1384 i = ba_inuse(session->tbl_scope_pool_rx, tbl_scope_id);
1388 for (i = 0; i < TF_NUM_TBL_SCOPE; i++) {
1389 if (session->tbl_scopes[i].tbl_scope_id == tbl_scope_id)
1390 return &session->tbl_scopes[i];
1396 /* API defined in tf_core.h */
1398 tf_free_eem_tbl_scope_cb(struct tf *tfp,
1399 struct tf_free_tbl_scope_parms *parms)
1403 struct tf_tbl_scope_cb *tbl_scope_cb;
1404 struct tf_session *session;
1406 session = (struct tf_session *)(tfp->session->core_data);
1408 tbl_scope_cb = tbl_scope_cb_find(session,
1409 parms->tbl_scope_id);
1411 if (tbl_scope_cb == NULL)
1414 /* Free Table control block */
1415 ba_free(session->tbl_scope_pool_rx, tbl_scope_cb->index);
1417 /* free table scope locks */
1418 for (dir = 0; dir < TF_DIR_MAX; dir++) {
1419 /* Free associated external pools
1421 tf_destroy_tbl_pool_external(dir,
1425 HWRM_TF_EXT_EM_OP_INPUT_OP_EXT_EM_DISABLE);
1427 /* free table scope and all associated resources */
1428 tf_em_ctx_unreg(tfp, tbl_scope_cb, dir);
1434 /* API defined in tf_em.h */
1436 tf_alloc_eem_tbl_scope(struct tf *tfp,
1437 struct tf_alloc_tbl_scope_parms *parms)
1441 struct tf_tbl_scope_cb *tbl_scope_cb;
1442 struct tf_em_table *em_tables;
1444 struct tf_session *session;
1445 struct tf_free_tbl_scope_parms free_parms;
1447 /* check parameters */
1448 if (parms == NULL || tfp->session == NULL) {
1449 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1453 session = (struct tf_session *)tfp->session->core_data;
1455 /* Get Table Scope control block from the session pool */
1456 index = ba_alloc(session->tbl_scope_pool_rx);
1458 PMD_DRV_LOG(ERR, "EEM: Unable to allocate table scope "
1463 tbl_scope_cb = &session->tbl_scopes[index];
1464 tbl_scope_cb->index = index;
1465 tbl_scope_cb->tbl_scope_id = index;
1466 parms->tbl_scope_id = index;
1468 for (dir = 0; dir < TF_DIR_MAX; dir++) {
1469 rc = tf_msg_em_qcaps(tfp,
1471 &tbl_scope_cb->em_caps[dir]);
1474 "EEM: Unable to query for EEM capability\n");
1480 * Validate and setup table sizes
1482 if (tf_em_validate_num_entries(tbl_scope_cb, parms))
1485 for (dir = 0; dir < TF_DIR_MAX; dir++) {
1487 * Allocate tables and signal configuration to FW
1489 rc = tf_em_ctx_reg(tfp, tbl_scope_cb, dir);
1492 "EEM: Unable to register for EEM ctx\n");
1496 em_tables = tbl_scope_cb->em_ctx_info[dir].em_tables;
1497 rc = tf_msg_em_cfg(tfp,
1498 em_tables[KEY0_TABLE].num_entries,
1499 em_tables[KEY0_TABLE].ctx_id,
1500 em_tables[KEY1_TABLE].ctx_id,
1501 em_tables[RECORD_TABLE].ctx_id,
1502 em_tables[EFC_TABLE].ctx_id,
1503 parms->hw_flow_cache_flush_timer,
1507 "TBL: Unable to configure EEM in firmware\n");
1511 rc = tf_msg_em_op(tfp,
1513 HWRM_TF_EXT_EM_OP_INPUT_OP_EXT_EM_ENABLE);
1517 "EEM: Unable to enable EEM in firmware\n");
1521 /* Allocate the pool of offsets of the external memory.
1522 * Initially, this is a single fixed size pool for all external
1523 * actions related to a single table scope.
1525 rc = tf_create_tbl_pool_external(dir,
1527 em_tables[RECORD_TABLE].num_entries,
1528 em_tables[RECORD_TABLE].entry_size);
1531 "%d TBL: Unable to allocate idx pools %s\n",
1541 free_parms.tbl_scope_id = index;
1542 tf_free_eem_tbl_scope_cb(tfp, &free_parms);
1546 /* Free Table control block */
1547 ba_free(session->tbl_scope_pool_rx, tbl_scope_cb->index);
1551 /* API defined in tf_core.h */
1553 tf_set_tbl_entry(struct tf *tfp,
1554 struct tf_set_tbl_entry_parms *parms)
1557 struct tf_tbl_scope_cb *tbl_scope_cb;
1558 struct tf_session *session;
1560 if (tfp == NULL || parms == NULL || parms->data == NULL)
1563 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1565 "dir:%d, Session info invalid\n",
1570 if (parms->type == TF_TBL_TYPE_EXT) {
1572 uint32_t offset = parms->idx;
1573 uint32_t tbl_scope_id;
1575 session = (struct tf_session *)(tfp->session->core_data);
1577 tbl_scope_id = parms->tbl_scope_id;
1579 if (tbl_scope_id == TF_TBL_SCOPE_INVALID) {
1581 "dir:%d, Table scope not allocated\n",
1586 /* Get the table scope control block associated with the
1589 tbl_scope_cb = tbl_scope_cb_find(session, tbl_scope_id);
1591 if (tbl_scope_cb == NULL)
1594 /* External table, implicitly the Action table */
1595 base_addr = tf_em_get_table_page(tbl_scope_cb,
1599 if (base_addr == NULL) {
1601 "dir:%d, Base address lookup failed\n",
1606 offset %= TF_EM_PAGE_SIZE;
1607 rte_memcpy((char *)base_addr + offset,
1609 parms->data_sz_in_bytes);
1611 /* Internal table type processing */
1612 rc = tf_set_tbl_entry_internal(tfp, parms);
1615 "dir:%d, Set failed, type:%d, rc:%d\n",
1625 /* API defined in tf_core.h */
1627 tf_get_tbl_entry(struct tf *tfp,
1628 struct tf_get_tbl_entry_parms *parms)
1632 if (tfp == NULL || parms == NULL)
1635 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1637 "dir:%d, Session info invalid\n",
1642 if (parms->type == TF_TBL_TYPE_EXT) {
1644 "dir:%d, External table type not supported\n",
1649 /* Internal table type processing */
1650 rc = tf_get_tbl_entry_internal(tfp, parms);
1653 "dir:%d, Get failed, type:%d, rc:%d\n",
1662 /* API defined in tf_core.h */
1664 tf_alloc_tbl_scope(struct tf *tfp,
1665 struct tf_alloc_tbl_scope_parms *parms)
1669 /* check parameters */
1670 if (parms == NULL || tfp == NULL) {
1671 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1675 rc = tf_alloc_eem_tbl_scope(tfp, parms);
1680 /* API defined in tf_core.h */
1682 tf_free_tbl_scope(struct tf *tfp,
1683 struct tf_free_tbl_scope_parms *parms)
1687 /* check parameters */
1688 if (parms == NULL || tfp == NULL) {
1689 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1693 /* free table scope and all associated resources */
1694 rc = tf_free_eem_tbl_scope_cb(tfp, parms);
1699 /* API defined in tf_core.h */
1701 tf_alloc_tbl_entry(struct tf *tfp,
1702 struct tf_alloc_tbl_entry_parms *parms)
1705 #if (TF_SHADOW == 1)
1706 struct tf_session *tfs;
1707 #endif /* TF_SHADOW */
1709 /* Check parameters */
1710 if (parms == NULL || tfp == NULL) {
1711 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1715 * No shadow copy support for external tables, allocate and return
1717 if (parms->type == TF_TBL_TYPE_EXT) {
1718 rc = tf_alloc_tbl_entry_pool_external(tfp, parms);
1722 #if (TF_SHADOW == 1)
1723 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1725 "dir:%d, Session info invalid\n",
1730 tfs = (struct tf_session *)(tfp->session->core_data);
1732 /* Search the Shadow DB for requested element. If not found go
1733 * allocate one from the Session Pool
1735 if (parms->search_enable && tfs->shadow_copy) {
1736 rc = tf_alloc_tbl_entry_shadow(tfs, parms);
1737 /* Entry found and parms populated with return data */
1741 #endif /* TF_SHADOW */
1743 rc = tf_alloc_tbl_entry_pool_internal(tfp, parms);
1745 PMD_DRV_LOG(ERR, "dir%d, Alloc failed, rc:%d\n",
1752 /* API defined in tf_core.h */
1754 tf_free_tbl_entry(struct tf *tfp,
1755 struct tf_free_tbl_entry_parms *parms)
1758 #if (TF_SHADOW == 1)
1759 struct tf_session *tfs;
1760 #endif /* TF_SHADOW */
1762 /* Check parameters */
1763 if (parms == NULL || tfp == NULL) {
1764 PMD_DRV_LOG(ERR, "TBL: Invalid parameters\n");
1768 * No shadow of external tables so just free the entry
1770 if (parms->type == TF_TBL_TYPE_EXT) {
1771 rc = tf_free_tbl_entry_pool_external(tfp, parms);
1775 #if (TF_SHADOW == 1)
1776 if (tfp->session == NULL || tfp->session->core_data == NULL) {
1778 "dir:%d, Session info invalid\n",
1783 tfs = (struct tf_session *)(tfp->session->core_data);
1785 /* Search the Shadow DB for requested element. If not found go
1786 * allocate one from the Session Pool
1788 if (parms->search_enable && tfs->shadow_copy) {
1789 rc = tf_free_tbl_entry_shadow(tfs, parms);
1790 /* Entry free'ed and parms populated with return data */
1794 #endif /* TF_SHADOW */
1796 rc = tf_free_tbl_entry_pool_internal(tfp, parms);
1799 PMD_DRV_LOG(ERR, "dir:%d, Alloc failed, rc:%d\n",