1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Broadcom
7 #include <rte_common.h>
10 #include "tf_common.h"
13 #include "tf_device.h"
15 #include "tf_session.h"
23 static void *tcam_db[TF_DIR_MAX];
28 /* static void *shadow_tcam_db[TF_DIR_MAX]; */
31 * Init flag, set on bind and cleared on unbind
36 * Shadow init flag, set on bind and cleared on unbind
38 /* static uint8_t shadow_init; */
41 tf_tcam_bind(struct tf *tfp,
42 struct tf_tcam_cfg_parms *parms)
46 struct tf_tcam_resources *tcam_cnt;
47 struct tf_rm_create_db_parms db_cfg = { 0 };
49 TF_CHECK_PARMS2(tfp, parms);
53 "TCAM DB already initialized\n");
57 tcam_cnt = parms->resources->tcam_cnt;
58 if ((tcam_cnt[TF_DIR_RX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] % 2) ||
59 (tcam_cnt[TF_DIR_TX].cnt[TF_TCAM_TBL_TYPE_WC_TCAM] % 2)) {
61 "Number of WC TCAM entries cannot be odd num\n");
65 db_cfg.type = TF_DEVICE_MODULE_TYPE_TCAM;
66 db_cfg.num_elements = parms->num_elements;
67 db_cfg.cfg = parms->cfg;
69 for (i = 0; i < TF_DIR_MAX; i++) {
71 db_cfg.alloc_cnt = parms->resources->tcam_cnt[i].cnt;
72 db_cfg.rm_db = &tcam_db[i];
73 rc = tf_rm_create_db(tfp, &db_cfg);
76 "%s: TCAM DB creation failed\n",
84 printf("TCAM - initialized\n");
90 tf_tcam_unbind(struct tf *tfp)
94 struct tf_rm_free_db_parms fparms = { 0 };
98 /* Bail if nothing has been initialized */
101 "No TCAM DBs created\n");
105 for (i = 0; i < TF_DIR_MAX; i++) {
107 fparms.rm_db = tcam_db[i];
108 rc = tf_rm_free_db(tfp, &fparms);
121 tf_tcam_alloc(struct tf *tfp,
122 struct tf_tcam_alloc_parms *parms)
125 struct tf_session *tfs;
126 struct tf_dev_info *dev;
127 struct tf_rm_allocate_parms aparms = { 0 };
128 uint16_t num_slice_per_row = 1;
130 TF_CHECK_PARMS2(tfp, parms);
134 "%s: No TCAM DBs created\n",
135 tf_dir_2_str(parms->dir));
139 /* Retrieve the session information */
140 rc = tf_session_get_session(tfp, &tfs);
144 /* Retrieve the device information */
145 rc = tf_session_get_device(tfs, &dev);
149 if (dev->ops->tf_dev_get_tcam_slice_info == NULL) {
152 "%s: Operation not supported, rc:%s\n",
153 tf_dir_2_str(parms->dir),
158 /* Need to retrieve row size etc */
159 rc = dev->ops->tf_dev_get_tcam_slice_info(tfp,
166 /* Allocate requested element */
167 aparms.rm_db = tcam_db[parms->dir];
168 aparms.db_index = parms->type;
169 aparms.priority = parms->priority;
170 aparms.index = (uint32_t *)&parms->idx;
171 rc = tf_rm_allocate(&aparms);
174 "%s: Failed tcam, type:%d\n",
175 tf_dir_2_str(parms->dir),
180 if (parms->type == TF_TCAM_TBL_TYPE_WC_TCAM &&
181 (parms->idx % 2) != 0) {
182 rc = tf_rm_allocate(&aparms);
185 "%s: Failed tcam, type:%d\n",
186 tf_dir_2_str(parms->dir),
192 parms->idx *= num_slice_per_row;
198 tf_tcam_free(struct tf *tfp,
199 struct tf_tcam_free_parms *parms)
202 struct tf_session *tfs;
203 struct tf_dev_info *dev;
204 struct tf_rm_is_allocated_parms aparms = { 0 };
205 struct tf_rm_free_parms fparms = { 0 };
206 struct tf_rm_get_hcapi_parms hparms = { 0 };
207 uint16_t num_slice_per_row = 1;
210 TF_CHECK_PARMS2(tfp, parms);
214 "%s: No TCAM DBs created\n",
215 tf_dir_2_str(parms->dir));
219 /* Retrieve the session information */
220 rc = tf_session_get_session(tfp, &tfs);
224 /* Retrieve the device information */
225 rc = tf_session_get_device(tfs, &dev);
229 if (dev->ops->tf_dev_get_tcam_slice_info == NULL) {
232 "%s: Operation not supported, rc:%s\n",
233 tf_dir_2_str(parms->dir),
238 /* Need to retrieve row size etc */
239 rc = dev->ops->tf_dev_get_tcam_slice_info(tfp,
246 /* Check if element is in use */
247 aparms.rm_db = tcam_db[parms->dir];
248 aparms.db_index = parms->type;
249 aparms.index = parms->idx / num_slice_per_row;
250 aparms.allocated = &allocated;
251 rc = tf_rm_is_allocated(&aparms);
255 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
257 "%s: Entry already free, type:%d, index:%d\n",
258 tf_dir_2_str(parms->dir),
264 /* Free requested element */
265 fparms.rm_db = tcam_db[parms->dir];
266 fparms.db_index = parms->type;
267 fparms.index = parms->idx / num_slice_per_row;
268 rc = tf_rm_free(&fparms);
271 "%s: Free failed, type:%d, index:%d\n",
272 tf_dir_2_str(parms->dir),
278 /* Convert TF type to HCAPI RM type */
279 hparms.rm_db = tcam_db[parms->dir];
280 hparms.db_index = parms->type;
281 hparms.hcapi_type = &parms->hcapi_type;
283 rc = tf_rm_get_hcapi_type(&hparms);
287 rc = tf_msg_tcam_entry_free(tfp, parms);
291 "%s: %s: Entry %d free failed, rc:%s\n",
292 tf_dir_2_str(parms->dir),
293 tf_tcam_tbl_2_str(parms->type),
302 tf_tcam_alloc_search(struct tf *tfp __rte_unused,
303 struct tf_tcam_alloc_search_parms *parms __rte_unused)
309 tf_tcam_set(struct tf *tfp __rte_unused,
310 struct tf_tcam_set_parms *parms __rte_unused)
313 struct tf_session *tfs;
314 struct tf_dev_info *dev;
315 struct tf_rm_is_allocated_parms aparms = { 0 };
316 struct tf_rm_get_hcapi_parms hparms = { 0 };
317 uint16_t num_slice_per_row = 1;
320 TF_CHECK_PARMS2(tfp, parms);
324 "%s: No TCAM DBs created\n",
325 tf_dir_2_str(parms->dir));
329 /* Retrieve the session information */
330 rc = tf_session_get_session(tfp, &tfs);
334 /* Retrieve the device information */
335 rc = tf_session_get_device(tfs, &dev);
339 if (dev->ops->tf_dev_get_tcam_slice_info == NULL) {
342 "%s: Operation not supported, rc:%s\n",
343 tf_dir_2_str(parms->dir),
348 /* Need to retrieve row size etc */
349 rc = dev->ops->tf_dev_get_tcam_slice_info(tfp,
356 /* Check if element is in use */
357 aparms.rm_db = tcam_db[parms->dir];
358 aparms.db_index = parms->type;
359 aparms.index = parms->idx / num_slice_per_row;
360 aparms.allocated = &allocated;
361 rc = tf_rm_is_allocated(&aparms);
365 if (allocated != TF_RM_ALLOCATED_ENTRY_IN_USE) {
367 "%s: Entry is not allocated, type:%d, index:%d\n",
368 tf_dir_2_str(parms->dir),
374 /* Convert TF type to HCAPI RM type */
375 hparms.rm_db = tcam_db[parms->dir];
376 hparms.db_index = parms->type;
377 hparms.hcapi_type = &parms->hcapi_type;
379 rc = tf_rm_get_hcapi_type(&hparms);
383 rc = tf_msg_tcam_entry_set(tfp, parms);
387 "%s: %s: Entry %d set failed, rc:%s",
388 tf_dir_2_str(parms->dir),
389 tf_tcam_tbl_2_str(parms->type),
398 tf_tcam_get(struct tf *tfp __rte_unused,
399 struct tf_tcam_get_parms *parms __rte_unused)