1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Broadcom
9 #include <rte_common.h>
10 #include <rte_errno.h>
15 #include "tf_common.h"
17 #include "tf_em_common.h"
20 #include "tf_device.h"
21 #include "tf_ext_flow_handle.h"
22 #include "cfa_resource_types.h"
30 void *eem_db[TF_DIR_MAX];
33 * Init flag, set on bind and cleared on unbind
40 static enum tf_mem_type mem_type;
42 /* API defined in tf_em.h */
43 struct tf_tbl_scope_cb *
44 tbl_scope_cb_find(struct tf_session *session,
45 uint32_t tbl_scope_id)
48 struct tf_rm_is_allocated_parms parms;
51 /* Check that id is valid */
52 parms.rm_db = eem_db[TF_DIR_RX];
53 parms.db_index = 1/**** TYPE TABLE-SCOPE??? ****/;
54 parms.index = tbl_scope_id + TF_HACK_TBL_SCOPE_BASE;
55 parms.allocated = &allocated;
57 i = tf_rm_is_allocated(&parms);
59 if (i < 0 || !allocated)
62 for (i = 0; i < TF_NUM_TBL_SCOPE; i++) {
63 if (session->tbl_scopes[i].tbl_scope_id == tbl_scope_id)
64 return &session->tbl_scopes[i];
71 tf_create_tbl_pool_external(enum tf_dir dir,
72 struct tf_tbl_scope_cb *tbl_scope_cb,
74 uint32_t entry_sz_bytes)
76 struct tfp_calloc_parms parms;
80 struct stack *pool = &tbl_scope_cb->ext_act_pool[dir];
82 parms.nitems = num_entries;
83 parms.size = sizeof(uint32_t);
86 if (tfp_calloc(&parms) != 0) {
87 TFP_DRV_LOG(ERR, "%s: TBL: external pool failure %s\n",
88 tf_dir_2_str(dir), strerror(ENOMEM));
94 rc = stack_init(num_entries, parms.mem_va, pool);
97 TFP_DRV_LOG(ERR, "%s: TBL: stack init failure %s\n",
98 tf_dir_2_str(dir), strerror(-rc));
102 /* Save the malloced memory address so that it can
103 * be freed when the table scope is freed.
105 tbl_scope_cb->ext_act_pool_mem[dir] = (uint32_t *)parms.mem_va;
107 /* Fill pool with indexes in reverse
109 j = (num_entries - 1) * entry_sz_bytes;
111 for (i = 0; i < num_entries; i++) {
112 rc = stack_push(pool, j);
114 TFP_DRV_LOG(ERR, "%s TBL: stack failure %s\n",
115 tf_dir_2_str(dir), strerror(-rc));
120 TFP_DRV_LOG(ERR, "%d TBL: invalid offset (%d)\n",
127 if (!stack_is_full(pool)) {
129 TFP_DRV_LOG(ERR, "%s TBL: stack failure %s\n",
130 tf_dir_2_str(dir), strerror(-rc));
135 tfp_free((void *)parms.mem_va);
140 * Destroy External Tbl pool of memory indexes.
145 * pointer to the table scope
148 tf_destroy_tbl_pool_external(enum tf_dir dir,
149 struct tf_tbl_scope_cb *tbl_scope_cb)
151 uint32_t *ext_act_pool_mem =
152 tbl_scope_cb->ext_act_pool_mem[dir];
154 tfp_free(ext_act_pool_mem);
158 tf_em_get_key_mask(int num_entries)
160 uint32_t mask = num_entries - 1;
162 if (num_entries & TF_EM_MAX_MASK)
165 if (num_entries > TF_EM_MAX_ENTRY)
172 tf_em_create_key_entry(struct cfa_p4_eem_entry_hdr *result,
174 struct cfa_p4_eem_64b_entry *key_entry)
176 key_entry->hdr.word1 = result->word1;
178 if (result->word1 & CFA_P4_EEM_ENTRY_ACT_REC_INT_MASK)
179 key_entry->hdr.pointer = result->pointer;
181 key_entry->hdr.pointer = result->pointer;
183 memcpy(key_entry->key, in_key, TF_HW_EM_KEY_MAX_SIZE + 4);
186 dump_raw((uint8_t *)key_entry, TF_EM_KEY_RECORD_SIZE, "Create raw:");
191 tf_em_ext_common_bind(struct tf *tfp,
192 struct tf_em_cfg_parms *parms)
196 struct tf_rm_create_db_parms db_cfg = { 0 };
198 TF_CHECK_PARMS2(tfp, parms);
202 "Identifier already initialized\n");
206 db_cfg.type = TF_DEVICE_MODULE_TYPE_EM;
207 db_cfg.num_elements = parms->num_elements;
208 db_cfg.cfg = parms->cfg;
210 for (i = 0; i < TF_DIR_MAX; i++) {
212 db_cfg.alloc_cnt = parms->resources->em_cnt[i].cnt;
213 db_cfg.rm_db = &eem_db[i];
214 rc = tf_rm_create_db(tfp, &db_cfg);
217 "%s: EM DB creation failed\n",
224 mem_type = parms->mem_type;
231 tf_em_ext_common_unbind(struct tf *tfp)
235 struct tf_rm_free_db_parms fparms = { 0 };
237 TF_CHECK_PARMS1(tfp);
239 /* Bail if nothing has been initialized done silent as to
240 * allow for creation cleanup.
244 "No EM DBs created\n");
248 for (i = 0; i < TF_DIR_MAX; i++) {
250 fparms.rm_db = eem_db[i];
251 rc = tf_rm_free_db(tfp, &fparms);
264 tf_em_ext_common_alloc(struct tf *tfp,
265 struct tf_alloc_tbl_scope_parms *parms)
267 if (mem_type == TF_EEM_MEM_TYPE_HOST)
268 return tf_em_ext_host_alloc(tfp, parms);
270 return tf_em_ext_system_alloc(tfp, parms);
274 tf_em_ext_common_free(struct tf *tfp,
275 struct tf_free_tbl_scope_parms *parms)
277 if (mem_type == TF_EEM_MEM_TYPE_HOST)
278 return tf_em_ext_host_free(tfp, parms);
280 return tf_em_ext_system_free(tfp, parms);