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 };
197 uint8_t db_exists = 0;
199 TF_CHECK_PARMS2(tfp, parms);
203 "EM Ext DB already initialized\n");
207 db_cfg.type = TF_DEVICE_MODULE_TYPE_EM;
208 db_cfg.num_elements = parms->num_elements;
209 db_cfg.cfg = parms->cfg;
211 for (i = 0; i < TF_DIR_MAX; i++) {
213 db_cfg.alloc_cnt = parms->resources->em_cnt[i].cnt;
215 /* Check if we got any request to support EEM, if so
216 * we build an EM Ext DB holding Table Scopes.
218 if (db_cfg.alloc_cnt[TF_EM_TBL_TYPE_TBL_SCOPE] == 0)
221 db_cfg.rm_db = &eem_db[i];
222 rc = tf_rm_create_db(tfp, &db_cfg);
225 "%s: EM Ext DB creation failed\n",
234 mem_type = parms->mem_type;
242 tf_em_ext_common_unbind(struct tf *tfp)
246 struct tf_rm_free_db_parms fparms = { 0 };
248 TF_CHECK_PARMS1(tfp);
250 /* Bail if nothing has been initialized */
253 "No EM Ext DBs created\n");
257 for (i = 0; i < TF_DIR_MAX; i++) {
259 fparms.rm_db = eem_db[i];
260 rc = tf_rm_free_db(tfp, &fparms);
273 tf_em_ext_common_alloc(struct tf *tfp,
274 struct tf_alloc_tbl_scope_parms *parms)
276 if (mem_type == TF_EEM_MEM_TYPE_HOST)
277 return tf_em_ext_host_alloc(tfp, parms);
279 return tf_em_ext_system_alloc(tfp, parms);
283 tf_em_ext_common_free(struct tf *tfp,
284 struct tf_free_tbl_scope_parms *parms)
286 if (mem_type == TF_EEM_MEM_TYPE_HOST)
287 return tf_em_ext_host_free(tfp, parms);
289 return tf_em_ext_system_free(tfp, parms);