1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2020 Broadcom
7 #include <rte_malloc.h>
9 #include "ulp_template_db_enum.h"
10 #include "ulp_template_struct.h"
11 #include "bnxt_tf_common.h"
12 #include "ulp_utils.h"
15 #include "tf_ext_flow_handle.h"
16 #include "ulp_mark_mgr.h"
17 #include "ulp_flow_db.h"
18 #include "ulp_mapper.h"
21 static struct bnxt_ulp_glb_resource_info *
22 ulp_mapper_glb_resource_info_list_get(uint32_t *num_entries)
26 *num_entries = BNXT_ULP_GLB_RESOURCE_TBL_MAX_SZ;
27 return ulp_glb_resource_tbl;
31 * Read the global resource from the mapper global resource list
33 * The regval is always returned in big-endian.
35 * returns 0 on success
38 ulp_mapper_glb_resource_read(struct bnxt_ulp_mapper_data *mapper_data,
43 if (!mapper_data || !regval ||
44 dir >= TF_DIR_MAX || idx >= BNXT_ULP_GLB_REGFILE_INDEX_LAST)
47 *regval = mapper_data->glb_res_tbl[dir][idx].resource_hndl;
52 * Write a global resource to the mapper global resource list
54 * The regval value must be in big-endian.
56 * return 0 on success.
59 ulp_mapper_glb_resource_write(struct bnxt_ulp_mapper_data *data,
60 struct bnxt_ulp_glb_resource_info *res,
63 struct bnxt_ulp_mapper_glb_resource_entry *ent;
65 /* validate the arguments */
66 if (!data || res->direction >= TF_DIR_MAX ||
67 res->glb_regfile_index >= BNXT_ULP_GLB_REGFILE_INDEX_LAST)
70 /* write to the mapper data */
71 ent = &data->glb_res_tbl[res->direction][res->glb_regfile_index];
72 ent->resource_func = res->resource_func;
73 ent->resource_type = res->resource_type;
74 ent->resource_hndl = regval;
79 * Internal function to allocate identity resource and store it in mapper data.
81 * returns 0 on success
84 ulp_mapper_resource_ident_allocate(struct bnxt_ulp_context *ulp_ctx,
85 struct bnxt_ulp_mapper_data *mapper_data,
86 struct bnxt_ulp_glb_resource_info *glb_res)
88 struct tf_alloc_identifier_parms iparms = { 0 };
89 struct tf_free_identifier_parms fparms;
94 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
98 iparms.ident_type = glb_res->resource_type;
99 iparms.dir = glb_res->direction;
101 /* Allocate the Identifier using tf api */
102 rc = tf_alloc_identifier(tfp, &iparms);
104 BNXT_TF_DBG(ERR, "Failed to alloc identifier [%s][%d]\n",
105 (iparms.dir == TF_DIR_RX) ? "RX" : "TX",
110 /* entries are stored as big-endian format */
111 regval = tfp_cpu_to_be_64((uint64_t)iparms.id);
112 /* write to the mapper global resource */
113 rc = ulp_mapper_glb_resource_write(mapper_data, glb_res, regval);
115 BNXT_TF_DBG(ERR, "Failed to write to global resource id\n");
116 /* Free the identifier when update failed */
117 fparms.dir = iparms.dir;
118 fparms.ident_type = iparms.ident_type;
119 fparms.id = iparms.id;
120 tf_free_identifier(tfp, &fparms);
127 * Internal function to allocate index tbl resource and store it in mapper data.
129 * returns 0 on success
132 ulp_mapper_resource_index_tbl_alloc(struct bnxt_ulp_context *ulp_ctx,
133 struct bnxt_ulp_mapper_data *mapper_data,
134 struct bnxt_ulp_glb_resource_info *glb_res)
136 struct tf_alloc_tbl_entry_parms aparms = { 0 };
137 struct tf_free_tbl_entry_parms free_parms = { 0 };
140 uint32_t tbl_scope_id;
143 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
147 /* Get the scope id */
148 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, &tbl_scope_id);
150 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
154 aparms.type = glb_res->resource_type;
155 aparms.dir = glb_res->direction;
156 aparms.search_enable = BNXT_ULP_SEARCH_BEFORE_ALLOC_NO;
157 aparms.tbl_scope_id = tbl_scope_id;
159 /* Allocate the index tbl using tf api */
160 rc = tf_alloc_tbl_entry(tfp, &aparms);
162 BNXT_TF_DBG(ERR, "Failed to alloc identifier [%s][%d]\n",
163 (aparms.dir == TF_DIR_RX) ? "RX" : "TX",
168 /* entries are stored as big-endian format */
169 regval = tfp_cpu_to_be_64((uint64_t)aparms.idx);
170 /* write to the mapper global resource */
171 rc = ulp_mapper_glb_resource_write(mapper_data, glb_res, regval);
173 BNXT_TF_DBG(ERR, "Failed to write to global resource id\n");
174 /* Free the identifier when update failed */
175 free_parms.dir = aparms.dir;
176 free_parms.type = aparms.type;
177 free_parms.idx = aparms.idx;
178 tf_free_tbl_entry(tfp, &free_parms);
184 /* Retrieve the cache initialization parameters for the tbl_idx */
185 static struct bnxt_ulp_cache_tbl_params *
186 ulp_mapper_cache_tbl_params_get(uint32_t tbl_idx)
188 if (tbl_idx >= BNXT_ULP_CACHE_TBL_MAX_SZ)
191 return &ulp_cache_tbl_params[tbl_idx];
194 /* Retrieve the global template table */
196 ulp_mapper_glb_template_table_get(uint32_t *num_entries)
200 *num_entries = BNXT_ULP_GLB_TEMPLATE_TBL_MAX_SZ;
201 return ulp_glb_template_tbl;
205 * Get the size of the action property for a given index.
207 * idx [in] The index for the action property
209 * returns the size of the action property.
212 ulp_mapper_act_prop_size_get(uint32_t idx)
214 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST)
216 return ulp_act_prop_map_table[idx];
220 * Get the list of result fields that implement the flow action.
221 * Gets a device dependent list of tables that implement the action template id.
223 * dev_id [in] The device id of the forwarding element
225 * tid [in] The action template id that matches the flow
227 * num_tbls [out] The number of action tables in the returned array
229 * Returns An array of action tables to implement the flow, or NULL on error.
231 static struct bnxt_ulp_mapper_tbl_info *
232 ulp_mapper_action_tbl_list_get(uint32_t dev_id,
240 BNXT_TF_DBG(ERR, "Invalid arguments\n");
244 /* template shift and device mask */
245 tidx = ULP_DEVICE_PARAMS_INDEX(tid, dev_id);
247 /* NOTE: Need to have something from template compiler to help validate
248 * range of dev_id and act_tid
250 idx = ulp_act_tmpl_list[tidx].start_tbl_idx;
251 *num_tbls = ulp_act_tmpl_list[tidx].num_tbls;
253 return &ulp_act_tbl_list[idx];
257 * Get a list of classifier tables that implement the flow
258 * Gets a device dependent list of tables that implement the class template id
260 * dev_id [in] The device id of the forwarding element
262 * tid [in] The template id that matches the flow
264 * num_tbls [out] The number of classifier tables in the returned array
266 * fdb_tbl_idx [out] The flow database index Regular or default
268 * returns An array of classifier tables to implement the flow, or NULL on
271 static struct bnxt_ulp_mapper_tbl_info *
272 ulp_mapper_class_tbl_list_get(uint32_t dev_id,
275 uint32_t *fdb_tbl_idx)
278 uint32_t tidx = ULP_DEVICE_PARAMS_INDEX(tid, dev_id);
283 /* NOTE: Need to have something from template compiler to help validate
284 * range of dev_id and tid
286 idx = ulp_class_tmpl_list[tidx].start_tbl_idx;
287 *num_tbls = ulp_class_tmpl_list[tidx].num_tbls;
288 *fdb_tbl_idx = ulp_class_tmpl_list[tidx].flow_db_table_type;
289 return &ulp_class_tbl_list[idx];
293 * Get the list of key fields that implement the flow.
295 * ctxt [in] The ulp context
297 * tbl [in] A single table instance to get the key fields from
299 * num_flds [out] The number of key fields in the returned array
301 * Returns array of Key fields, or NULL on error.
303 static struct bnxt_ulp_mapper_class_key_field_info *
304 ulp_mapper_key_fields_get(struct bnxt_ulp_mapper_tbl_info *tbl,
309 if (!tbl || !num_flds)
312 idx = tbl->key_start_idx;
313 *num_flds = tbl->key_num_fields;
315 /* NOTE: Need template to provide range checking define */
316 return &ulp_class_key_field_list[idx];
320 * Get the list of data fields that implement the flow.
322 * ctxt [in] The ulp context
324 * tbl [in] A single table instance to get the data fields from
326 * num_flds [out] The number of data fields in the returned array.
328 * Returns array of data fields, or NULL on error.
330 static struct bnxt_ulp_mapper_result_field_info *
331 ulp_mapper_result_fields_get(struct bnxt_ulp_mapper_tbl_info *tbl,
333 uint32_t *num_encap_flds)
337 if (!tbl || !num_flds)
340 idx = tbl->result_start_idx;
341 *num_flds = tbl->result_num_fields;
342 *num_encap_flds = tbl->encap_num_fields;
344 /* NOTE: Need template to provide range checking define */
345 return &ulp_class_result_field_list[idx];
349 * Get the list of result fields that implement the flow action.
351 * tbl [in] A single table instance to get the results fields
352 * from num_flds [out] The number of data fields in the returned
355 * Returns array of data fields, or NULL on error.
357 static struct bnxt_ulp_mapper_result_field_info *
358 ulp_mapper_act_result_fields_get(struct bnxt_ulp_mapper_tbl_info *tbl,
359 uint32_t *num_rslt_flds,
360 uint32_t *num_encap_flds)
364 if (!tbl || !num_rslt_flds || !num_encap_flds)
367 idx = tbl->result_start_idx;
368 *num_rslt_flds = tbl->result_num_fields;
369 *num_encap_flds = tbl->encap_num_fields;
371 /* NOTE: Need template to provide range checking define */
372 return &ulp_act_result_field_list[idx];
376 * Get the list of ident fields that implement the flow
378 * tbl [in] A single table instance to get the ident fields from
380 * num_flds [out] The number of ident fields in the returned array
382 * returns array of ident fields, or NULL on error
384 static struct bnxt_ulp_mapper_ident_info *
385 ulp_mapper_ident_fields_get(struct bnxt_ulp_mapper_tbl_info *tbl,
390 if (!tbl || !num_flds)
393 idx = tbl->ident_start_idx;
394 *num_flds = tbl->ident_nums;
396 /* NOTE: Need template to provide range checking define */
397 return &ulp_ident_list[idx];
400 static struct bnxt_ulp_mapper_cache_entry *
401 ulp_mapper_cache_entry_get(struct bnxt_ulp_context *ulp,
405 struct bnxt_ulp_mapper_data *mapper_data;
407 mapper_data = bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp);
408 if (!mapper_data || id >= BNXT_ULP_CACHE_TBL_MAX_SZ ||
409 !mapper_data->cache_tbl[id]) {
410 BNXT_TF_DBG(ERR, "Unable to acquire the cache tbl (%d)\n", id);
414 return &mapper_data->cache_tbl[id][key];
418 * Concatenates the tbl_type and tbl_id into a 32bit value for storing in the
419 * resource_type. This is done to conserve memory since both the tbl_type and
423 ulp_mapper_cache_res_type_set(struct ulp_flow_db_res_params *res,
427 res->resource_type = tbl_type;
428 res->resource_sub_type = tbl_id;
431 /* Extracts the tbl_type and tbl_id from the 32bit resource type. */
433 ulp_mapper_cache_res_type_get(struct ulp_flow_db_res_params *res,
437 *tbl_type = res->resource_type;
438 *tbl_id = res->resource_sub_type;
442 ulp_mapper_cache_entry_free(struct bnxt_ulp_context *ulp,
444 struct ulp_flow_db_res_params *res)
446 struct bnxt_ulp_mapper_cache_entry *cache_entry;
447 struct tf_free_identifier_parms ident_parms;
448 struct tf_free_tcam_entry_parms tcam_parms;
449 uint16_t table_id, table_type;
453 * The table id, used for cache, and table_type, used for tcam, are
454 * both encoded within the resource. We must first extract them to
455 * formulate the args for tf calls.
457 ulp_mapper_cache_res_type_get(res, &table_type, &table_id);
458 cache_entry = ulp_mapper_cache_entry_get(ulp, table_id,
459 (uint16_t)res->resource_hndl);
460 if (!cache_entry || !cache_entry->ref_count) {
461 BNXT_TF_DBG(ERR, "Cache entry (%d:%d) not valid on free.\n",
462 table_id, (uint16_t)res->resource_hndl);
467 * See if we need to delete the entry. The tcam and identifiers are all
468 * tracked by the cached entries reference count. All are deleted when
469 * the reference count hit zero.
471 cache_entry->ref_count--;
472 if (cache_entry->ref_count)
476 * Need to delete the tcam entry and the allocated identifiers.
477 * In the event of a failure, need to try to delete the remaining
478 * resources before returning error.
480 tcam_parms.dir = res->direction;
481 tcam_parms.tcam_tbl_type = table_type;
482 tcam_parms.idx = cache_entry->tcam_idx;
483 rc = tf_free_tcam_entry(tfp, &tcam_parms);
485 BNXT_TF_DBG(ERR, "Failed to free tcam [%d][%s][0x%04x] rc=%d\n",
487 (res->direction == TF_DIR_RX) ? "RX" : "TX",
491 * Free the identifiers associated with the tcam entry. Entries with
492 * negative one are considered uninitialized.
494 for (i = 0; i < BNXT_ULP_CACHE_TBL_IDENT_MAX_NUM; i++) {
495 if (cache_entry->idents[i] == ULP_IDENTS_INVALID)
498 ident_parms.dir = res->direction;
499 ident_parms.ident_type = cache_entry->ident_types[i];
500 ident_parms.id = cache_entry->idents[i];
501 trc = tf_free_identifier(tfp, &ident_parms);
503 BNXT_TF_DBG(ERR, "Failed to free identifier "
504 "[%d][%s][0x%04x] rc=%d\n",
505 ident_parms.ident_type,
506 (res->direction == TF_DIR_RX) ? "RX" : "TX",
507 ident_parms.id, trc);
515 static inline int32_t
516 ulp_mapper_tcam_entry_free(struct bnxt_ulp_context *ulp __rte_unused,
518 struct ulp_flow_db_res_params *res)
520 struct tf_free_tcam_entry_parms fparms = {
521 .dir = res->direction,
522 .tcam_tbl_type = res->resource_type,
523 .idx = (uint16_t)res->resource_hndl
526 return tf_free_tcam_entry(tfp, &fparms);
529 static inline int32_t
530 ulp_mapper_index_entry_free(struct bnxt_ulp_context *ulp,
532 struct ulp_flow_db_res_params *res)
534 struct tf_free_tbl_entry_parms fparms = {
535 .dir = res->direction,
536 .type = res->resource_type,
537 .idx = (uint32_t)res->resource_hndl
541 * Just get the table scope, it will be ignored if not necessary
542 * by the tf_free_tbl_entry
544 (void)bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
546 return tf_free_tbl_entry(tfp, &fparms);
549 static inline int32_t
550 ulp_mapper_em_entry_free(struct bnxt_ulp_context *ulp,
552 struct ulp_flow_db_res_params *res)
554 struct tf_delete_em_entry_parms fparms = { 0 };
557 fparms.dir = res->direction;
558 if (res->resource_func == BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE)
559 fparms.mem = TF_MEM_EXTERNAL;
561 fparms.mem = TF_MEM_INTERNAL;
562 fparms.flow_handle = res->resource_hndl;
564 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
566 BNXT_TF_DBG(ERR, "Failed to get table scope\n");
570 return tf_delete_em_entry(tfp, &fparms);
573 static inline int32_t
574 ulp_mapper_ident_free(struct bnxt_ulp_context *ulp __rte_unused,
576 struct ulp_flow_db_res_params *res)
578 struct tf_free_identifier_parms fparms = {
579 .dir = res->direction,
580 .ident_type = res->resource_type,
581 .id = (uint16_t)res->resource_hndl
584 return tf_free_identifier(tfp, &fparms);
587 static inline int32_t
588 ulp_mapper_mark_free(struct bnxt_ulp_context *ulp,
589 struct ulp_flow_db_res_params *res)
591 return ulp_mark_db_mark_del(ulp,
597 * Process the identifier instruction and either store it in the flow database
598 * or return it in the val (if not NULL) on success. If val is NULL, the
599 * identifier is to be stored in the flow database.
602 ulp_mapper_ident_process(struct bnxt_ulp_mapper_parms *parms,
603 struct bnxt_ulp_mapper_tbl_info *tbl,
604 struct bnxt_ulp_mapper_ident_info *ident,
607 struct ulp_flow_db_res_params fid_parms;
610 struct tf_alloc_identifier_parms iparms = { 0 };
611 struct tf_free_identifier_parms free_parms = { 0 };
615 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
617 BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
621 idx = ident->regfile_idx;
623 iparms.ident_type = ident->ident_type;
624 iparms.dir = tbl->direction;
626 rc = tf_alloc_identifier(tfp, &iparms);
628 BNXT_TF_DBG(ERR, "Alloc ident %s:%d failed.\n",
629 (iparms.dir == TF_DIR_RX) ? "RX" : "TX",
634 id = (uint64_t)tfp_cpu_to_be_64(iparms.id);
635 if (!ulp_regfile_write(parms->regfile, idx, id)) {
636 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
638 /* Need to free the identifier, so goto error */
642 /* Link the resource to the flow in the flow db */
644 memset(&fid_parms, 0, sizeof(fid_parms));
645 fid_parms.direction = tbl->direction;
646 fid_parms.resource_func = ident->resource_func;
647 fid_parms.resource_type = ident->ident_type;
648 fid_parms.resource_hndl = iparms.id;
649 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
651 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
656 BNXT_TF_DBG(ERR, "Failed to link res to flow rc = %d\n",
658 /* Need to free the identifier, so goto error */
668 /* Need to free the identifier */
669 free_parms.dir = tbl->direction;
670 free_parms.ident_type = ident->ident_type;
671 free_parms.id = iparms.id;
673 (void)tf_free_identifier(tfp, &free_parms);
675 BNXT_TF_DBG(ERR, "Ident process failed for %s:%s\n",
677 (tbl->direction == TF_DIR_RX) ? "RX" : "TX");
682 * Process the identifier instruction and extract it from result blob.
683 * Increment the identifier reference count and store it in the flow database.
686 ulp_mapper_ident_extract(struct bnxt_ulp_mapper_parms *parms,
687 struct bnxt_ulp_mapper_tbl_info *tbl,
688 struct bnxt_ulp_mapper_ident_info *ident,
689 struct ulp_blob *res_blob)
691 struct ulp_flow_db_res_params fid_parms;
694 struct tf_search_identifier_parms sparms = { 0 };
695 struct tf_free_identifier_parms free_parms = { 0 };
699 /* Get the tfp from ulp context */
700 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
702 BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
706 /* Extract the index from the result blob */
707 rc = ulp_blob_pull(res_blob, (uint8_t *)&idx, sizeof(idx),
708 ident->ident_bit_pos, ident->ident_bit_size);
710 BNXT_TF_DBG(ERR, "Failed to extract identifier from blob\n");
714 /* populate the search params and search identifier shadow table */
715 sparms.ident_type = ident->ident_type;
716 sparms.dir = tbl->direction;
717 /* convert the idx into cpu format */
718 sparms.search_id = tfp_be_to_cpu_32(idx);
720 /* Search identifier also increase the reference count */
721 rc = tf_search_identifier(tfp, &sparms);
723 BNXT_TF_DBG(ERR, "Search ident %s:%x failed.\n",
724 tf_dir_2_str(sparms.dir),
728 BNXT_TF_DBG(INFO, "Search ident %s:%x.success.\n",
729 tf_dir_2_str(sparms.dir),
732 /* Write it to the regfile */
733 id = (uint64_t)tfp_cpu_to_be_64(sparms.search_id);
734 if (!ulp_regfile_write(parms->regfile, ident->regfile_idx, id)) {
735 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
737 /* Need to free the identifier, so goto error */
741 /* Link the resource to the flow in the flow db */
742 memset(&fid_parms, 0, sizeof(fid_parms));
743 fid_parms.direction = tbl->direction;
744 fid_parms.resource_func = ident->resource_func;
745 fid_parms.resource_type = ident->ident_type;
746 fid_parms.resource_hndl = sparms.search_id;
747 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
748 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
753 BNXT_TF_DBG(ERR, "Failed to link res to flow rc = %d\n",
755 /* Need to free the identifier, so goto error */
762 /* Need to free the identifier */
763 free_parms.dir = tbl->direction;
764 free_parms.ident_type = ident->ident_type;
765 free_parms.id = sparms.search_id;
766 (void)tf_free_identifier(tfp, &free_parms);
767 BNXT_TF_DBG(ERR, "Ident extract failed for %s:%s:%x\n",
769 tf_dir_2_str(tbl->direction), sparms.search_id);
774 ulp_mapper_result_field_process(struct bnxt_ulp_mapper_parms *parms,
776 struct bnxt_ulp_mapper_result_field_info *fld,
777 struct ulp_blob *blob,
780 uint16_t idx, size_idx;
783 uint32_t val_size = 0, field_size = 0;
788 switch (fld->result_opcode) {
789 case BNXT_ULP_MAPPER_OPC_SET_TO_CONSTANT:
790 val = fld->result_operand;
791 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
792 BNXT_TF_DBG(ERR, "%s failed to add field\n", name);
796 case BNXT_ULP_MAPPER_OPC_SET_TO_ACT_PROP:
797 if (!ulp_operand_read(fld->result_operand,
798 (uint8_t *)&idx, sizeof(uint16_t))) {
799 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
802 idx = tfp_be_to_cpu_16(idx);
804 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
805 BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n", name, idx);
808 val = &parms->act_prop->act_details[idx];
809 field_size = ulp_mapper_act_prop_size_get(idx);
810 if (fld->field_bit_size < ULP_BYTE_2_BITS(field_size)) {
811 field_size = field_size -
812 ((fld->field_bit_size + 7) / 8);
815 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
816 BNXT_TF_DBG(ERR, "%s push field failed\n", name);
820 case BNXT_ULP_MAPPER_OPC_SET_TO_ACT_BIT:
821 if (!ulp_operand_read(fld->result_operand,
822 (uint8_t *)&act_bit, sizeof(uint64_t))) {
823 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
826 act_bit = tfp_be_to_cpu_64(act_bit);
827 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits, act_bit))
831 if (fld->field_bit_size > ULP_BYTE_2_BITS(sizeof(act_val))) {
832 BNXT_TF_DBG(ERR, "%s field size is incorrect\n", name);
835 if (!ulp_blob_push(blob, &act_val, fld->field_bit_size)) {
836 BNXT_TF_DBG(ERR, "%s push field failed\n", name);
841 case BNXT_ULP_MAPPER_OPC_SET_TO_ENCAP_ACT_PROP_SZ:
842 if (!ulp_operand_read(fld->result_operand,
843 (uint8_t *)&idx, sizeof(uint16_t))) {
844 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
847 idx = tfp_be_to_cpu_16(idx);
849 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
850 BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n", name, idx);
853 val = &parms->act_prop->act_details[idx];
855 /* get the size index next */
856 if (!ulp_operand_read(&fld->result_operand[sizeof(uint16_t)],
857 (uint8_t *)&size_idx, sizeof(uint16_t))) {
858 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
861 size_idx = tfp_be_to_cpu_16(size_idx);
863 if (size_idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
864 BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", size_idx);
867 memcpy(&val_size, &parms->act_prop->act_details[size_idx],
869 val_size = tfp_be_to_cpu_32(val_size);
870 val_size = ULP_BYTE_2_BITS(val_size);
871 ulp_blob_push_encap(blob, val, val_size);
873 case BNXT_ULP_MAPPER_OPC_SET_TO_REGFILE:
874 if (!ulp_operand_read(fld->result_operand,
875 (uint8_t *)&idx, sizeof(uint16_t))) {
876 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
880 idx = tfp_be_to_cpu_16(idx);
881 /* Uninitialized regfile entries return 0 */
882 if (!ulp_regfile_read(parms->regfile, idx, ®val)) {
883 BNXT_TF_DBG(ERR, "%s regfile[%d] read oob\n",
888 val = ulp_blob_push_64(blob, ®val, fld->field_bit_size);
890 BNXT_TF_DBG(ERR, "%s push field failed\n", name);
894 case BNXT_ULP_MAPPER_OPC_SET_TO_GLB_REGFILE:
895 if (!ulp_operand_read(fld->result_operand,
898 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
901 idx = tfp_be_to_cpu_16(idx);
902 if (ulp_mapper_glb_resource_read(parms->mapper_data,
905 BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
909 val = ulp_blob_push_64(blob, ®val, fld->field_bit_size);
911 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
915 case BNXT_ULP_MAPPER_OPC_SET_TO_COMP_FIELD:
916 if (!ulp_operand_read(fld->result_operand,
919 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
922 idx = tfp_be_to_cpu_16(idx);
923 if (idx < BNXT_ULP_CF_IDX_LAST)
924 val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
925 fld->field_bit_size);
927 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
931 case BNXT_ULP_MAPPER_OPC_SET_TO_ZERO:
932 if (ulp_blob_pad_push(blob, fld->field_bit_size) < 0) {
933 BNXT_TF_DBG(ERR, "%s too large for blob\n", name);
938 case BNXT_ULP_MAPPER_OPC_IF_ACT_BIT_THEN_ACT_PROP_ELSE_CONST:
939 if (!ulp_operand_read(fld->result_operand,
940 (uint8_t *)&act_bit, sizeof(uint64_t))) {
941 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
944 act_bit = tfp_be_to_cpu_64(act_bit);
945 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits, act_bit)) {
946 /* Action bit is set so consider operand_true */
947 if (!ulp_operand_read(fld->result_operand_true,
950 BNXT_TF_DBG(ERR, "%s operand read failed\n",
954 idx = tfp_be_to_cpu_16(idx);
955 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
956 BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n",
960 val = &parms->act_prop->act_details[idx];
961 field_size = ulp_mapper_act_prop_size_get(idx);
962 if (fld->field_bit_size < ULP_BYTE_2_BITS(field_size)) {
963 field_size = field_size -
964 ((fld->field_bit_size + 7) / 8);
967 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
968 BNXT_TF_DBG(ERR, "%s push field failed\n",
973 /* action bit is not set, use the operand false */
974 val = fld->result_operand_false;
975 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
976 BNXT_TF_DBG(ERR, "%s failed to add field\n",
982 case BNXT_ULP_MAPPER_OPC_IF_ACT_BIT_THEN_CONST_ELSE_CONST:
983 if (!ulp_operand_read(fld->result_operand,
984 (uint8_t *)&act_bit, sizeof(uint64_t))) {
985 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
988 act_bit = tfp_be_to_cpu_64(act_bit);
989 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits, act_bit)) {
990 /* Action bit is set so consider operand_true */
991 val = fld->result_operand_true;
993 /* action bit is not set, use the operand false */
994 val = fld->result_operand_false;
996 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
997 BNXT_TF_DBG(ERR, "%s failed to add field\n",
1002 case BNXT_ULP_MAPPER_OPC_IF_COMP_FIELD_THEN_CF_ELSE_CF:
1003 if (!ulp_operand_read(fld->result_operand,
1005 sizeof(uint16_t))) {
1006 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1009 idx = tfp_be_to_cpu_16(idx);
1010 if (idx >= BNXT_ULP_CF_IDX_LAST) {
1011 BNXT_TF_DBG(ERR, "%s invalid index %u\n", name, idx);
1014 /* check if the computed field is set */
1015 if (ULP_COMP_FLD_IDX_RD(parms, idx))
1016 val = fld->result_operand_true;
1018 val = fld->result_operand_false;
1020 /* read the appropriate computed field */
1021 if (!ulp_operand_read(val, (uint8_t *)&idx, sizeof(uint16_t))) {
1022 BNXT_TF_DBG(ERR, "%s val operand read failed\n", name);
1025 idx = tfp_be_to_cpu_16(idx);
1026 if (idx >= BNXT_ULP_CF_IDX_LAST) {
1027 BNXT_TF_DBG(ERR, "%s invalid index %u\n", name, idx);
1030 val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
1031 fld->field_bit_size);
1033 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1037 case BNXT_ULP_MAPPER_OPC_IF_HDR_BIT_THEN_CONST_ELSE_CONST:
1038 if (!ulp_operand_read(fld->result_operand,
1039 (uint8_t *)&hdr_bit, sizeof(uint64_t))) {
1040 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
1043 hdr_bit = tfp_be_to_cpu_64(hdr_bit);
1044 if (ULP_BITMAP_ISSET(parms->hdr_bitmap->bits, hdr_bit)) {
1045 /* Header bit is set so consider operand_true */
1046 val = fld->result_operand_true;
1048 /* Header bit is not set, use the operand false */
1049 val = fld->result_operand_false;
1051 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
1052 BNXT_TF_DBG(ERR, "%s failed to add field\n",
1058 BNXT_TF_DBG(ERR, "invalid result mapper opcode 0x%x\n",
1059 fld->result_opcode);
1065 /* Function to alloc action record and set the table. */
1067 ulp_mapper_keymask_field_process(struct bnxt_ulp_mapper_parms *parms,
1069 struct bnxt_ulp_mapper_class_key_field_info *f,
1070 struct ulp_blob *blob,
1075 uint16_t idx, bitlen;
1078 struct ulp_regfile *regfile = parms->regfile;
1079 uint8_t *val = NULL;
1080 struct bnxt_ulp_mapper_class_key_field_info *fld = f;
1081 uint32_t field_size;
1084 operand = fld->spec_operand;
1085 opcode = fld->spec_opcode;
1087 operand = fld->mask_operand;
1088 opcode = fld->mask_opcode;
1091 bitlen = fld->field_bit_size;
1094 case BNXT_ULP_MAPPER_OPC_SET_TO_CONSTANT:
1096 if (!ulp_blob_push(blob, val, bitlen)) {
1097 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1101 case BNXT_ULP_MAPPER_OPC_SET_TO_ZERO:
1102 if (ulp_blob_pad_push(blob, bitlen) < 0) {
1103 BNXT_TF_DBG(ERR, "%s pad too large for blob\n", name);
1108 case BNXT_ULP_MAPPER_OPC_SET_TO_HDR_FIELD:
1109 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1110 sizeof(uint16_t))) {
1111 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1114 idx = tfp_be_to_cpu_16(idx);
1116 val = parms->hdr_field[idx].spec;
1118 val = parms->hdr_field[idx].mask;
1121 * Need to account for how much data was pushed to the header
1122 * field vs how much is to be inserted in the key/mask.
1124 field_size = parms->hdr_field[idx].size;
1125 if (bitlen < ULP_BYTE_2_BITS(field_size)) {
1126 field_size = field_size - ((bitlen + 7) / 8);
1130 if (!ulp_blob_push(blob, val, bitlen)) {
1131 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1135 case BNXT_ULP_MAPPER_OPC_SET_TO_COMP_FIELD:
1136 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1137 sizeof(uint16_t))) {
1138 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1141 idx = tfp_be_to_cpu_16(idx);
1142 if (idx < BNXT_ULP_CF_IDX_LAST)
1143 val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
1146 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1150 case BNXT_ULP_MAPPER_OPC_SET_TO_REGFILE:
1151 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1152 sizeof(uint16_t))) {
1153 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1156 idx = tfp_be_to_cpu_16(idx);
1158 if (!ulp_regfile_read(regfile, idx, &val64)) {
1159 BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
1164 val = ulp_blob_push_64(blob, &val64, bitlen);
1166 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1170 case BNXT_ULP_MAPPER_OPC_SET_TO_GLB_REGFILE:
1171 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1172 sizeof(uint16_t))) {
1173 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1176 idx = tfp_be_to_cpu_16(idx);
1177 if (ulp_mapper_glb_resource_read(parms->mapper_data,
1180 BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
1184 val = ulp_blob_push_64(blob, &val64, bitlen);
1186 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1191 BNXT_TF_DBG(ERR, "invalid keymask mapper opcode 0x%x\n",
1201 ulp_mapper_mark_gfid_process(struct bnxt_ulp_mapper_parms *parms,
1202 struct bnxt_ulp_mapper_tbl_info *tbl,
1205 enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1206 struct ulp_flow_db_res_params fid_parms;
1207 uint32_t mark, gfid, mark_flag;
1210 if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1211 !(mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION &&
1212 ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1213 BNXT_ULP_ACTION_BIT_MARK)))
1214 return rc; /* no need to perform gfid process */
1216 /* Get the mark id details from action property */
1217 memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1219 mark = tfp_be_to_cpu_32(mark);
1221 TF_GET_GFID_FROM_FLOW_ID(flow_id, gfid);
1222 mark_flag = BNXT_ULP_MARK_GLOBAL_HW_FID;
1223 rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1226 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1229 fid_parms.direction = tbl->direction;
1230 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1231 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1232 fid_parms.resource_type = mark_flag;
1233 fid_parms.resource_hndl = gfid;
1234 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1239 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1244 ulp_mapper_mark_act_ptr_process(struct bnxt_ulp_mapper_parms *parms,
1245 struct bnxt_ulp_mapper_tbl_info *tbl)
1247 enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1248 struct ulp_flow_db_res_params fid_parms;
1249 uint32_t act_idx, mark, mark_flag;
1253 if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1254 !(mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION &&
1255 ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1256 BNXT_ULP_ACTION_BIT_MARK)))
1257 return rc; /* no need to perform mark action process */
1259 /* Get the mark id details from action property */
1260 memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1262 mark = tfp_be_to_cpu_32(mark);
1264 if (!ulp_regfile_read(parms->regfile,
1265 BNXT_ULP_REGFILE_INDEX_MAIN_ACTION_PTR,
1267 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1270 act_idx = tfp_be_to_cpu_64(val64);
1271 mark_flag = BNXT_ULP_MARK_LOCAL_HW_FID;
1272 rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1275 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1278 fid_parms.direction = tbl->direction;
1279 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1280 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1281 fid_parms.resource_type = mark_flag;
1282 fid_parms.resource_hndl = act_idx;
1283 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1288 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1293 ulp_mapper_mark_vfr_idx_process(struct bnxt_ulp_mapper_parms *parms,
1294 struct bnxt_ulp_mapper_tbl_info *tbl)
1296 struct ulp_flow_db_res_params fid_parms;
1297 uint32_t act_idx, mark, mark_flag;
1299 enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1302 if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1303 mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION)
1304 return rc; /* no need to perform mark action process */
1306 /* Get the mark id details from the computed field of dev port id */
1307 mark = ULP_COMP_FLD_IDX_RD(parms, BNXT_ULP_CF_IDX_DEV_PORT_ID);
1309 /* Get the main action pointer */
1310 if (!ulp_regfile_read(parms->regfile,
1311 BNXT_ULP_REGFILE_INDEX_MAIN_ACTION_PTR,
1313 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1316 act_idx = tfp_be_to_cpu_64(val64);
1318 /* Set the mark flag to local fid and vfr flag */
1319 mark_flag = BNXT_ULP_MARK_LOCAL_HW_FID | BNXT_ULP_MARK_VFR_ID;
1321 rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1324 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1327 fid_parms.direction = tbl->direction;
1328 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1329 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1330 fid_parms.resource_type = mark_flag;
1331 fid_parms.resource_hndl = act_idx;
1332 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1337 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1342 * Tcam table - create the result blob.
1343 * data [out] - the result blob data
1346 ulp_mapper_tcam_tbl_result_create(struct bnxt_ulp_mapper_parms *parms,
1347 struct bnxt_ulp_mapper_tbl_info *tbl,
1348 struct ulp_blob *data)
1350 struct bnxt_ulp_mapper_result_field_info *dflds;
1352 uint32_t encap_flds = 0;
1356 /* Create the result data blob */
1357 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds,
1359 if (!dflds || !num_dflds || encap_flds) {
1360 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
1364 for (i = 0; i < num_dflds; i++) {
1365 rc = ulp_mapper_result_field_process(parms,
1371 BNXT_TF_DBG(ERR, "Failed to set data fields\n");
1378 /* Tcam table scan the identifier list and allocate each identifier */
1380 ulp_mapper_tcam_tbl_scan_ident_alloc(struct bnxt_ulp_mapper_parms *parms,
1381 struct bnxt_ulp_mapper_tbl_info *tbl)
1383 struct bnxt_ulp_mapper_ident_info *idents;
1384 uint32_t num_idents;
1388 * Since the cache entry is responsible for allocating
1389 * identifiers when in use, allocate the identifiers only
1390 * during normal processing.
1392 if (parms->tcam_tbl_opc ==
1393 BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL) {
1394 idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
1396 for (i = 0; i < num_idents; i++) {
1397 if (ulp_mapper_ident_process(parms, tbl,
1406 * Tcam table scan the identifier list and extract the identifier from
1410 ulp_mapper_tcam_tbl_scan_ident_extract(struct bnxt_ulp_mapper_parms *parms,
1411 struct bnxt_ulp_mapper_tbl_info *tbl,
1412 struct ulp_blob *data)
1414 struct bnxt_ulp_mapper_ident_info *idents;
1415 uint32_t num_idents = 0, i;
1419 * Extract the listed identifiers from the result field,
1420 * no need to allocate them.
1422 idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
1423 for (i = 0; i < num_idents; i++) {
1424 rc = ulp_mapper_ident_extract(parms, tbl, &idents[i], data);
1426 BNXT_TF_DBG(ERR, "Error in identifier extraction\n");
1433 /* Internal function to write the tcam entry */
1435 ulp_mapper_tcam_tbl_entry_write(struct bnxt_ulp_mapper_parms *parms,
1436 struct bnxt_ulp_mapper_tbl_info *tbl,
1437 struct ulp_blob *key,
1438 struct ulp_blob *mask,
1439 struct ulp_blob *data,
1442 struct tf_set_tcam_entry_parms sparms = { 0 };
1447 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1449 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1453 sparms.dir = tbl->direction;
1454 sparms.tcam_tbl_type = tbl->resource_type;
1456 /* Already verified the key/mask lengths */
1457 sparms.key = ulp_blob_data_get(key, &tmplen);
1458 sparms.mask = ulp_blob_data_get(mask, &tmplen);
1459 sparms.key_sz_in_bits = tbl->key_bit_size;
1460 sparms.result = ulp_blob_data_get(data, &tmplen);
1462 if (tbl->result_bit_size != tmplen) {
1463 BNXT_TF_DBG(ERR, "Result len (%d) != Expected (%d)\n",
1464 tmplen, tbl->result_bit_size);
1467 sparms.result_sz_in_bits = tbl->result_bit_size;
1468 if (tf_set_tcam_entry(tfp, &sparms)) {
1469 BNXT_TF_DBG(ERR, "tcam[%s][%s][%x] write failed.\n",
1470 tf_tcam_tbl_2_str(sparms.tcam_tbl_type),
1471 tf_dir_2_str(sparms.dir), sparms.idx);
1474 BNXT_TF_DBG(INFO, "tcam[%s][%s][%x] write success.\n",
1475 tf_tcam_tbl_2_str(sparms.tcam_tbl_type),
1476 tf_dir_2_str(sparms.dir), sparms.idx);
1478 /* Update cache with TCAM index if the was cache allocated. */
1479 if (parms->tcam_tbl_opc ==
1480 BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_ALLOC) {
1481 if (!parms->cache_ptr) {
1482 BNXT_TF_DBG(ERR, "Unable to update cache");
1485 parms->cache_ptr->tcam_idx = idx;
1489 rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
1491 BNXT_TF_DBG(ERR, "failed mark action processing\n");
1499 ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1500 struct bnxt_ulp_mapper_tbl_info *tbl)
1502 struct bnxt_ulp_mapper_class_key_field_info *kflds;
1503 struct ulp_blob key, mask, data, update_data;
1504 uint32_t i, num_kflds;
1507 struct tf_alloc_tcam_entry_parms aparms = { 0 };
1508 struct tf_search_tcam_entry_parms searchparms = { 0 };
1509 struct ulp_flow_db_res_params fid_parms = { 0 };
1510 struct tf_free_tcam_entry_parms free_parms = { 0 };
1511 enum bnxt_ulp_search_before_alloc search_flag;
1513 uint16_t tmplen = 0;
1516 /* Skip this if was handled by the cache. */
1517 if (parms->tcam_tbl_opc == BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_SKIP) {
1518 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1522 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1524 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1528 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
1529 if (!kflds || !num_kflds) {
1530 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1534 if (!ulp_blob_init(&key, tbl->key_bit_size,
1535 parms->device_params->byte_order) ||
1536 !ulp_blob_init(&mask, tbl->key_bit_size,
1537 parms->device_params->byte_order) ||
1538 !ulp_blob_init(&data, tbl->result_bit_size,
1539 parms->device_params->byte_order) ||
1540 !ulp_blob_init(&update_data, tbl->result_bit_size,
1541 parms->device_params->byte_order)) {
1542 BNXT_TF_DBG(ERR, "blob inits failed.\n");
1546 /* create the key/mask */
1548 * NOTE: The WC table will require some kind of flag to handle the
1549 * mode bits within the key/mask
1551 for (i = 0; i < num_kflds; i++) {
1553 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1555 &key, 1, "TCAM Key");
1557 BNXT_TF_DBG(ERR, "Key field set failed.\n");
1561 /* Setup the mask */
1562 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1564 &mask, 0, "TCAM Mask");
1566 BNXT_TF_DBG(ERR, "Mask field set failed.\n");
1571 if (tbl->srch_b4_alloc == BNXT_ULP_SEARCH_BEFORE_ALLOC_NO) {
1573 * No search for re-use is requested, so simply allocate the
1576 aparms.dir = tbl->direction;
1577 aparms.tcam_tbl_type = tbl->resource_type;
1578 aparms.search_enable = tbl->srch_b4_alloc;
1579 aparms.key_sz_in_bits = tbl->key_bit_size;
1580 aparms.key = ulp_blob_data_get(&key, &tmplen);
1581 if (tbl->key_bit_size != tmplen) {
1582 BNXT_TF_DBG(ERR, "Key len (%d) != Expected (%d)\n",
1583 tmplen, tbl->key_bit_size);
1587 aparms.mask = ulp_blob_data_get(&mask, &tmplen);
1588 if (tbl->key_bit_size != tmplen) {
1589 BNXT_TF_DBG(ERR, "Mask len (%d) != Expected (%d)\n",
1590 tmplen, tbl->key_bit_size);
1594 aparms.priority = tbl->priority;
1597 * All failures after this succeeds require the entry to be
1598 * freed. cannot return directly on failure, but needs to goto
1601 rc = tf_alloc_tcam_entry(tfp, &aparms);
1603 BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
1610 * Searching before allocation to see if we already have an
1611 * entry. This allows re-use of a constrained resource.
1613 searchparms.dir = tbl->direction;
1614 searchparms.tcam_tbl_type = tbl->resource_type;
1615 searchparms.key = ulp_blob_data_get(&key, &tmplen);
1616 searchparms.key_sz_in_bits = tbl->key_bit_size;
1617 searchparms.mask = ulp_blob_data_get(&mask, &tmplen);
1618 searchparms.priority = tbl->priority;
1619 searchparms.alloc = 1;
1620 searchparms.result = ulp_blob_data_get(&data, &tmplen);
1621 searchparms.result_sz_in_bits = tbl->result_bit_size;
1623 rc = tf_search_tcam_entry(tfp, &searchparms);
1625 BNXT_TF_DBG(ERR, "tcam search failed rc=%d\n", rc);
1629 /* Successful search, check the result */
1630 if (searchparms.search_status == REJECT) {
1631 BNXT_TF_DBG(ERR, "tcam alloc rejected\n");
1634 idx = searchparms.idx;
1635 hit = searchparms.hit;
1638 /* if it is miss then it is same as no search before alloc */
1640 search_flag = BNXT_ULP_SEARCH_BEFORE_ALLOC_NO;
1642 search_flag = tbl->srch_b4_alloc;
1644 switch (search_flag) {
1645 case BNXT_ULP_SEARCH_BEFORE_ALLOC_NO:
1646 /*Scan identifier list, allocate identifier and update regfile*/
1647 rc = ulp_mapper_tcam_tbl_scan_ident_alloc(parms, tbl);
1648 /* Create the result blob */
1650 rc = ulp_mapper_tcam_tbl_result_create(parms, tbl,
1652 /* write the tcam entry */
1654 rc = ulp_mapper_tcam_tbl_entry_write(parms, tbl, &key,
1657 case BNXT_ULP_SEARCH_BEFORE_ALLOC_SEARCH_IF_HIT_SKIP:
1658 /*Scan identifier list, extract identifier and update regfile*/
1659 rc = ulp_mapper_tcam_tbl_scan_ident_extract(parms, tbl, &data);
1661 case BNXT_ULP_SEARCH_BEFORE_ALLOC_SEARCH_IF_HIT_UPDATE:
1662 /*Scan identifier list, extract identifier and update regfile*/
1663 rc = ulp_mapper_tcam_tbl_scan_ident_extract(parms, tbl, &data);
1664 /* Create the result blob */
1666 rc = ulp_mapper_tcam_tbl_result_create(parms, tbl,
1668 /* Update/overwrite the tcam entry */
1670 rc = ulp_mapper_tcam_tbl_entry_write(parms, tbl, &key,
1675 BNXT_TF_DBG(ERR, "invalid search opcode\n");
1683 * Only link the entry to the flow db in the event that cache was not
1686 if (parms->tcam_tbl_opc == BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL) {
1687 fid_parms.direction = tbl->direction;
1688 fid_parms.resource_func = tbl->resource_func;
1689 fid_parms.resource_type = tbl->resource_type;
1690 fid_parms.critical_resource = tbl->critical_resource;
1691 fid_parms.resource_hndl = idx;
1692 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1698 "Failed to link resource to flow rc = %d\n",
1700 /* Need to free the identifier, so goto error */
1705 * Reset the tcam table opcode to normal in case the next tcam
1706 * entry does not use cache.
1708 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1709 parms->cache_ptr = NULL;
1714 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1715 free_parms.dir = tbl->direction;
1716 free_parms.tcam_tbl_type = tbl->resource_type;
1717 free_parms.idx = idx;
1718 trc = tf_free_tcam_entry(tfp, &free_parms);
1720 BNXT_TF_DBG(ERR, "Failed to free tcam[%d][%d][%d] on failure\n",
1721 tbl->resource_type, tbl->direction, idx);
1727 ulp_mapper_em_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1728 struct bnxt_ulp_mapper_tbl_info *tbl)
1730 struct bnxt_ulp_mapper_class_key_field_info *kflds;
1731 struct bnxt_ulp_mapper_result_field_info *dflds;
1732 struct ulp_blob key, data;
1733 uint32_t i, num_kflds, num_dflds;
1735 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1736 struct ulp_flow_db_res_params fid_parms = { 0 };
1737 struct tf_insert_em_entry_parms iparms = { 0 };
1738 struct tf_delete_em_entry_parms free_parms = { 0 };
1740 enum bnxt_ulp_flow_mem_type mtype = parms->device_params->flow_mem_type;
1742 uint32_t encap_flds = 0;
1744 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
1745 if (!kflds || !num_kflds) {
1746 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1750 /* Initialize the key/result blobs */
1751 if (!ulp_blob_init(&key, tbl->blob_key_bit_size,
1752 parms->device_params->byte_order) ||
1753 !ulp_blob_init(&data, tbl->result_bit_size,
1754 parms->device_params->byte_order)) {
1755 BNXT_TF_DBG(ERR, "blob inits failed.\n");
1759 /* create the key */
1760 for (i = 0; i < num_kflds; i++) {
1762 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1766 BNXT_TF_DBG(ERR, "Key field set failed.\n");
1772 * TBD: Normally should process identifiers in case of using recycle or
1773 * loopback. Not supporting recycle for now.
1776 /* Create the result data blob */
1777 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds, &encap_flds);
1778 if (!dflds || !num_dflds || encap_flds) {
1779 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
1783 for (i = 0; i < num_dflds; i++) {
1784 struct bnxt_ulp_mapper_result_field_info *fld;
1788 rc = ulp_mapper_result_field_process(parms,
1794 BNXT_TF_DBG(ERR, "Failed to set data fields.\n");
1799 /* do the transpose for the internal EM keys */
1800 if (tbl->resource_func == BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE)
1801 ulp_blob_perform_byte_reverse(&key);
1803 rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
1804 &iparms.tbl_scope_id);
1806 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
1811 * NOTE: the actual blob size will differ from the size in the tbl
1812 * entry due to the padding.
1814 iparms.dup_check = 0;
1815 iparms.dir = tbl->direction;
1816 iparms.mem = tbl->resource_type;
1817 iparms.key = ulp_blob_data_get(&key, &tmplen);
1818 iparms.key_sz_in_bits = tbl->key_bit_size;
1819 iparms.em_record = ulp_blob_data_get(&data, &tmplen);
1820 iparms.em_record_sz_in_bits = tbl->result_bit_size;
1822 rc = tf_insert_em_entry(tfp, &iparms);
1824 BNXT_TF_DBG(ERR, "Failed to insert em entry rc=%d.\n", rc);
1828 /* Mark action process */
1829 if (mtype == BNXT_ULP_FLOW_MEM_TYPE_EXT &&
1830 tbl->resource_type == TF_MEM_EXTERNAL)
1831 rc = ulp_mapper_mark_gfid_process(parms, tbl, iparms.flow_id);
1832 else if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT &&
1833 tbl->resource_type == TF_MEM_INTERNAL)
1834 rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
1836 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1840 /* Link the EM resource to the flow in the flow db */
1841 memset(&fid_parms, 0, sizeof(fid_parms));
1842 fid_parms.direction = tbl->direction;
1843 fid_parms.resource_func = tbl->resource_func;
1844 fid_parms.resource_type = tbl->resource_type;
1845 fid_parms.critical_resource = tbl->critical_resource;
1846 fid_parms.resource_hndl = iparms.flow_handle;
1848 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1853 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1855 /* Need to free the identifier, so goto error */
1861 free_parms.dir = iparms.dir;
1862 free_parms.mem = iparms.mem;
1863 free_parms.tbl_scope_id = iparms.tbl_scope_id;
1864 free_parms.flow_handle = iparms.flow_handle;
1866 trc = tf_delete_em_entry(tfp, &free_parms);
1868 BNXT_TF_DBG(ERR, "Failed to delete EM entry on failed add\n");
1874 ulp_mapper_index_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1875 struct bnxt_ulp_mapper_tbl_info *tbl,
1878 struct bnxt_ulp_mapper_result_field_info *flds;
1879 struct ulp_flow_db_res_params fid_parms;
1880 struct ulp_blob data;
1883 uint32_t i, num_flds, index, hit;
1884 int32_t rc = 0, trc = 0;
1885 struct tf_alloc_tbl_entry_parms aparms = { 0 };
1886 struct tf_search_tbl_entry_parms srchparms = { 0 };
1887 struct tf_set_tbl_entry_parms sparms = { 0 };
1888 struct tf_free_tbl_entry_parms free_parms = { 0 };
1889 uint32_t tbl_scope_id;
1890 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1892 uint32_t encap_flds = 0;
1894 /* Get the scope id first */
1895 rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx, &tbl_scope_id);
1897 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
1901 /* use the max size if encap is enabled */
1902 if (tbl->encap_num_fields)
1903 bit_size = BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS;
1905 bit_size = tbl->result_bit_size;
1907 /* Initialize the blob data */
1908 if (!ulp_blob_init(&data, bit_size,
1909 parms->device_params->byte_order)) {
1910 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
1914 /* Get the result fields list */
1916 flds = ulp_mapper_result_fields_get(tbl, &num_flds,
1919 flds = ulp_mapper_act_result_fields_get(tbl, &num_flds,
1922 if (!flds || (!num_flds && !encap_flds)) {
1923 BNXT_TF_DBG(ERR, "template undefined for the index table\n");
1927 /* process the result fields, loop through them */
1928 for (i = 0; i < (num_flds + encap_flds); i++) {
1929 /* set the swap index if encap swap bit is enabled */
1930 if (parms->device_params->encap_byte_swap && encap_flds &&
1932 ulp_blob_encap_swap_idx_set(&data);
1934 /* Process the result fields */
1935 rc = ulp_mapper_result_field_process(parms,
1941 BNXT_TF_DBG(ERR, "data field failed\n");
1946 /* if encap bit swap is enabled perform the bit swap */
1947 if (parms->device_params->encap_byte_swap && encap_flds) {
1948 ulp_blob_perform_encap_swap(&data);
1952 * Check for index opcode, if it is Global then
1953 * no need to allocate the table, just set the table
1954 * and exit since it is not maintained in the flow db.
1956 if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_GLOBAL) {
1957 /* get the index from index operand */
1958 if (tbl->index_operand < BNXT_ULP_GLB_REGFILE_INDEX_LAST &&
1959 ulp_mapper_glb_resource_read(parms->mapper_data,
1963 BNXT_TF_DBG(ERR, "Glbl regfile[%d] read failed.\n",
1964 tbl->index_operand);
1967 /* set the Tf index table */
1968 sparms.dir = tbl->direction;
1969 sparms.type = tbl->resource_type;
1970 sparms.data = ulp_blob_data_get(&data, &tmplen);
1971 sparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1972 sparms.idx = tfp_be_to_cpu_64(idx);
1973 sparms.tbl_scope_id = tbl_scope_id;
1975 rc = tf_set_tbl_entry(tfp, &sparms);
1978 "Glbl Set table[%d][%s][%d] failed rc=%d\n",
1980 (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
1985 return 0; /* success */
1990 /* Perform the tf table allocation by filling the alloc params */
1991 if (tbl->srch_b4_alloc) {
1992 memset(&srchparms, 0, sizeof(srchparms));
1993 srchparms.dir = tbl->direction;
1994 srchparms.type = tbl->resource_type;
1995 srchparms.alloc = 1;
1996 srchparms.result = ulp_blob_data_get(&data, &tmplen);
1997 srchparms.result_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1998 srchparms.tbl_scope_id = tbl_scope_id;
1999 rc = tf_search_tbl_entry(tfp, &srchparms);
2001 BNXT_TF_DBG(ERR, "Alloc table[%s][%s] failed rc=%d\n",
2002 tf_tbl_type_2_str(tbl->resource_type),
2003 tf_dir_2_str(tbl->direction), rc);
2006 if (srchparms.search_status == REJECT) {
2007 BNXT_TF_DBG(ERR, "Alloc table[%s][%s] rejected.\n",
2008 tf_tbl_type_2_str(tbl->resource_type),
2009 tf_dir_2_str(tbl->direction));
2012 index = srchparms.idx;
2013 hit = srchparms.hit;
2015 aparms.dir = tbl->direction;
2016 aparms.type = tbl->resource_type;
2017 aparms.search_enable = tbl->srch_b4_alloc;
2018 aparms.result = ulp_blob_data_get(&data, &tmplen);
2019 aparms.result_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
2020 aparms.tbl_scope_id = tbl_scope_id;
2022 /* All failures after the alloc succeeds require a free */
2023 rc = tf_alloc_tbl_entry(tfp, &aparms);
2025 BNXT_TF_DBG(ERR, "Alloc table[%s][%s] failed rc=%d\n",
2026 tf_tbl_type_2_str(tbl->resource_type),
2027 tf_dir_2_str(tbl->direction), rc);
2033 * calculate the idx for the result record, for external EM the offset
2034 * needs to be shifted accordingly. If external non-inline table types
2035 * are used then need to revisit this logic.
2037 if (tbl->resource_type == TF_TBL_TYPE_EXT)
2038 idx = TF_ACT_REC_OFFSET_2_PTR(index);
2042 /* Always storing values in Regfile in BE */
2043 idx = tfp_cpu_to_be_64(idx);
2044 if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_ALLOCATE) {
2045 rc = ulp_regfile_write(parms->regfile, tbl->index_operand, idx);
2047 BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
2048 tbl->index_operand);
2053 /* Perform the tf table set by filling the set params */
2054 if (!tbl->srch_b4_alloc || !hit) {
2055 sparms.dir = tbl->direction;
2056 sparms.type = tbl->resource_type;
2057 sparms.data = ulp_blob_data_get(&data, &tmplen);
2058 sparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
2060 sparms.tbl_scope_id = tbl_scope_id;
2062 rc = tf_set_tbl_entry(tfp, &sparms);
2064 BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
2066 (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
2073 /* Link the resource to the flow in the flow db */
2074 memset(&fid_parms, 0, sizeof(fid_parms));
2075 fid_parms.direction = tbl->direction;
2076 fid_parms.resource_func = tbl->resource_func;
2077 fid_parms.resource_type = tbl->resource_type;
2078 fid_parms.resource_sub_type = tbl->resource_sub_type;
2079 fid_parms.resource_hndl = index;
2080 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
2082 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
2087 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
2092 /* Perform the VF rep action */
2093 rc = ulp_mapper_mark_vfr_idx_process(parms, tbl);
2095 BNXT_TF_DBG(ERR, "Failed to add vfr mark rc = %d\n", rc);
2101 * Free the allocated resource since we failed to either
2102 * write to the entry or link the flow
2104 free_parms.dir = tbl->direction;
2105 free_parms.type = tbl->resource_type;
2106 free_parms.idx = index;
2107 free_parms.tbl_scope_id = tbl_scope_id;
2109 trc = tf_free_tbl_entry(tfp, &free_parms);
2111 BNXT_TF_DBG(ERR, "Failed to free tbl entry on failure\n");
2117 ulp_mapper_cache_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2118 struct bnxt_ulp_mapper_tbl_info *tbl)
2120 struct bnxt_ulp_mapper_class_key_field_info *kflds;
2121 struct bnxt_ulp_mapper_cache_entry *cache_entry;
2122 struct bnxt_ulp_mapper_ident_info *idents;
2123 uint32_t i, num_kflds = 0, num_idents = 0;
2124 struct ulp_flow_db_res_params fid_parms;
2125 struct tf_free_identifier_parms fparms;
2126 uint16_t tmplen, tmp_ident;
2127 struct ulp_blob key;
2133 /* Get the key fields list and build the key. */
2134 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
2135 if (!kflds || !num_kflds) {
2136 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
2139 if (!ulp_blob_init(&key, tbl->key_bit_size,
2140 parms->device_params->byte_order)) {
2141 BNXT_TF_DBG(ERR, "Failed to alloc blob\n");
2144 for (i = 0; i < num_kflds; i++) {
2146 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
2148 &key, 1, "Cache Key");
2151 "Failed to create key for Cache rc=%d\n",
2158 * Perform the lookup in the cache table with constructed key. The
2159 * cache_key is a byte array of tmplen, it needs to be converted to a
2160 * index for the cache table.
2162 cache_key = ulp_blob_data_get(&key, &tmplen);
2163 ckey = (uint16_t *)cache_key;
2166 * The id computed based on resource sub type and direction where
2167 * dir is the bit0 and rest of the bits come from resource
2170 cache_entry = ulp_mapper_cache_entry_get(parms->ulp_ctx,
2171 (tbl->resource_sub_type << 1 |
2172 (tbl->direction & 0x1)),
2176 * Get the identifier list for processing by both the hit and miss
2179 idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
2181 if (!cache_entry->ref_count) {
2182 /* Initialize the cache entry */
2183 cache_entry->tcam_idx = 0;
2184 cache_entry->ref_count = 0;
2185 for (i = 0; i < BNXT_ULP_CACHE_TBL_IDENT_MAX_NUM; i++)
2186 cache_entry->idents[i] = ULP_IDENTS_INVALID;
2188 /* Need to allocate identifiers for storing in the cache. */
2189 for (i = 0; i < num_idents; i++) {
2191 * Since we are using the cache, the identifier does not
2192 * get added to the flow db. Pass in the pointer to the
2195 rc = ulp_mapper_ident_process(parms, tbl,
2196 &idents[i], &tmp_ident);
2200 cache_entry->ident_types[i] = idents[i].ident_type;
2201 cache_entry->idents[i] = tmp_ident;
2204 /* Tell the TCAM processor to alloc an entry */
2205 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_ALLOC;
2206 /* Store the cache key for use by the tcam process code */
2207 parms->cache_ptr = cache_entry;
2209 /* Cache hit, get values from result. */
2210 for (i = 0; i < num_idents; i++) {
2211 regval = (uint64_t)cache_entry->idents[i];
2212 if (!ulp_regfile_write(parms->regfile,
2213 idents[i].regfile_idx,
2214 tfp_cpu_to_be_64(regval))) {
2216 "Failed to write to regfile\n");
2221 * The cached entry is being used, so let the tcam processing
2222 * know not to process this table.
2224 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_SKIP;
2227 /* Made through the cache processing, increment the reference count. */
2228 cache_entry->ref_count++;
2230 /* Link the cache to the flow db. */
2231 memset(&fid_parms, 0, sizeof(fid_parms));
2232 fid_parms.direction = tbl->direction;
2233 fid_parms.resource_func = tbl->resource_func;
2236 * Cache resource type is composed of table_type, resource
2237 * sub type and direction, it needs to set appropriately via setter.
2239 ulp_mapper_cache_res_type_set(&fid_parms,
2241 (tbl->resource_sub_type << 1 |
2242 (tbl->direction & 0x1)));
2243 fid_parms.resource_hndl = (uint64_t)*ckey;
2244 fid_parms.critical_resource = tbl->critical_resource;
2245 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
2250 BNXT_TF_DBG(ERR, "Failed to add cache to flow db.\n");
2255 * This error handling only gets called when the idents are being
2256 * allocated for the cache on misses. Using the num_idents that was
2259 for (i = 0; i < num_idents; i++) {
2260 if (cache_entry->idents[i] == ULP_IDENTS_INVALID)
2263 fparms.dir = tbl->direction;
2264 fparms.ident_type = idents[i].ident_type;
2265 fparms.id = cache_entry->idents[i];
2266 tf_free_identifier(parms->tfp, &fparms);
2273 ulp_mapper_if_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2274 struct bnxt_ulp_mapper_tbl_info *tbl)
2276 struct bnxt_ulp_mapper_result_field_info *flds;
2277 struct ulp_blob data;
2280 uint32_t i, num_flds;
2282 struct tf_set_if_tbl_entry_parms iftbl_params = { 0 };
2283 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
2284 uint32_t encap_flds;
2286 /* Initialize the blob data */
2287 if (!ulp_blob_init(&data, tbl->result_bit_size,
2288 parms->device_params->byte_order)) {
2289 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
2293 /* Get the result fields list */
2294 flds = ulp_mapper_result_fields_get(tbl, &num_flds, &encap_flds);
2296 if (!flds || !num_flds || encap_flds) {
2297 BNXT_TF_DBG(ERR, "template undefined for the IF table\n");
2301 /* process the result fields, loop through them */
2302 for (i = 0; i < num_flds; i++) {
2303 /* Process the result fields */
2304 rc = ulp_mapper_result_field_process(parms,
2310 BNXT_TF_DBG(ERR, "data field failed\n");
2315 /* Get the index details from computed field */
2316 if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_COMP_FIELD) {
2317 idx = ULP_COMP_FLD_IDX_RD(parms, tbl->index_operand);
2318 } else if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_CONSTANT) {
2319 idx = tbl->index_operand;
2321 BNXT_TF_DBG(ERR, "Invalid tbl index opcode\n");
2325 /* Perform the tf table set by filling the set params */
2326 iftbl_params.dir = tbl->direction;
2327 iftbl_params.type = tbl->resource_type;
2328 iftbl_params.data = ulp_blob_data_get(&data, &tmplen);
2329 iftbl_params.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
2330 iftbl_params.idx = idx;
2332 rc = tf_set_if_tbl_entry(tfp, &iftbl_params);
2334 BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
2336 (iftbl_params.dir == TF_DIR_RX) ? "RX" : "TX",
2343 * TBD: Need to look at the need to store idx in flow db for restore
2344 * the table to its original state on deletion of this entry.
2350 ulp_mapper_glb_resource_info_init(struct bnxt_ulp_context *ulp_ctx,
2351 struct bnxt_ulp_mapper_data *mapper_data)
2353 struct bnxt_ulp_glb_resource_info *glb_res;
2354 uint32_t num_glb_res_ids, idx;
2357 glb_res = ulp_mapper_glb_resource_info_list_get(&num_glb_res_ids);
2358 if (!glb_res || !num_glb_res_ids) {
2359 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
2363 /* Iterate the global resources and process each one */
2364 for (idx = 0; idx < num_glb_res_ids; idx++) {
2365 switch (glb_res[idx].resource_func) {
2366 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
2367 rc = ulp_mapper_resource_ident_allocate(ulp_ctx,
2371 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2372 rc = ulp_mapper_resource_index_tbl_alloc(ulp_ctx,
2377 BNXT_TF_DBG(ERR, "Global resource %x not supported\n",
2378 glb_res[idx].resource_func);
2389 * Function to process the conditional opcode of the mapper table.
2390 * returns 1 to skip the table.
2391 * return 0 to continue processing the table.
2394 ulp_mapper_tbl_cond_opcode_process(struct bnxt_ulp_mapper_parms *parms,
2395 struct bnxt_ulp_mapper_tbl_info *tbl)
2399 switch (tbl->cond_opcode) {
2400 case BNXT_ULP_COND_OPCODE_NOP:
2403 case BNXT_ULP_COND_OPCODE_COMP_FIELD_IS_SET:
2404 if (tbl->cond_operand < BNXT_ULP_CF_IDX_LAST &&
2405 ULP_COMP_FLD_IDX_RD(parms, tbl->cond_operand))
2408 case BNXT_ULP_COND_OPCODE_ACTION_BIT_IS_SET:
2409 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits,
2413 case BNXT_ULP_COND_OPCODE_HDR_BIT_IS_SET:
2414 if (ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
2418 case BNXT_ULP_COND_OPCODE_COMP_FIELD_NOT_SET:
2419 if (tbl->cond_operand < BNXT_ULP_CF_IDX_LAST &&
2420 !ULP_COMP_FLD_IDX_RD(parms, tbl->cond_operand))
2423 case BNXT_ULP_COND_OPCODE_ACTION_BIT_NOT_SET:
2424 if (!ULP_BITMAP_ISSET(parms->act_bitmap->bits,
2428 case BNXT_ULP_COND_OPCODE_HDR_BIT_NOT_SET:
2429 if (!ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
2435 "Invalid arg in mapper tbl for cond opcode\n");
2442 * Function to process the action template. Iterate through the list
2443 * action info templates and process it.
2446 ulp_mapper_action_tbls_process(struct bnxt_ulp_mapper_parms *parms)
2450 struct bnxt_ulp_mapper_tbl_info *tbl;
2452 if (!parms->atbls || !parms->num_atbls) {
2453 BNXT_TF_DBG(ERR, "No action tables for template[%d][%d].\n",
2454 parms->dev_id, parms->act_tid);
2458 for (i = 0; i < parms->num_atbls; i++) {
2459 tbl = &parms->atbls[i];
2460 if (ulp_mapper_tbl_cond_opcode_process(parms, tbl))
2463 switch (tbl->resource_func) {
2464 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2465 rc = ulp_mapper_index_tbl_process(parms, tbl, false);
2467 BNXT_TF_DBG(ERR, "Resource type %d failed\n",
2468 tbl->resource_func);
2473 BNXT_TF_DBG(ERR, "Unexpected action resource %d\n",
2474 tbl->resource_func);
2482 /* Create the classifier table entries for a flow. */
2484 ulp_mapper_class_tbls_process(struct bnxt_ulp_mapper_parms *parms)
2492 if (!parms->ctbls || !parms->num_ctbls) {
2493 BNXT_TF_DBG(ERR, "No class tables for template[%d][%d].\n",
2494 parms->dev_id, parms->class_tid);
2498 for (i = 0; i < parms->num_ctbls; i++) {
2499 struct bnxt_ulp_mapper_tbl_info *tbl = &parms->ctbls[i];
2501 if (ulp_mapper_tbl_cond_opcode_process(parms, tbl))
2504 switch (tbl->resource_func) {
2505 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
2506 rc = ulp_mapper_tcam_tbl_process(parms, tbl);
2508 case BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE:
2509 case BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE:
2510 rc = ulp_mapper_em_tbl_process(parms, tbl);
2512 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2513 rc = ulp_mapper_index_tbl_process(parms, tbl, true);
2515 case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
2516 rc = ulp_mapper_cache_tbl_process(parms, tbl);
2518 case BNXT_ULP_RESOURCE_FUNC_IF_TABLE:
2519 rc = ulp_mapper_if_tbl_process(parms, tbl);
2522 BNXT_TF_DBG(ERR, "Unexpected class resource %d\n",
2523 tbl->resource_func);
2528 BNXT_TF_DBG(ERR, "Resource type %d failed\n",
2529 tbl->resource_func);
2538 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
2539 struct ulp_flow_db_res_params *res)
2545 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
2549 tfp = bnxt_ulp_cntxt_tfp_get(ulp);
2551 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
2555 switch (res->resource_func) {
2556 case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
2557 rc = ulp_mapper_cache_entry_free(ulp, tfp, res);
2559 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
2560 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
2562 case BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE:
2563 case BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE:
2564 rc = ulp_mapper_em_entry_free(ulp, tfp, res);
2566 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2567 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
2569 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
2570 rc = ulp_mapper_ident_free(ulp, tfp, res);
2572 case BNXT_ULP_RESOURCE_FUNC_HW_FID:
2573 rc = ulp_mapper_mark_free(ulp, res);
2583 ulp_mapper_resources_free(struct bnxt_ulp_context *ulp_ctx,
2585 enum bnxt_ulp_flow_db_tables tbl_type)
2587 struct ulp_flow_db_res_params res_parms = { 0 };
2591 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
2596 * Set the critical resource on the first resource del, then iterate
2597 * while status is good
2599 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_YES;
2600 rc = ulp_flow_db_resource_del(ulp_ctx, tbl_type, fid, &res_parms);
2604 * This is unexpected on the first call to resource del.
2605 * It likely means that the flow did not exist in the flow db.
2607 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
2613 trc = ulp_mapper_resource_free(ulp_ctx, &res_parms);
2616 * On fail, we still need to attempt to free the
2617 * remaining resources. Don't return
2620 "Flow[%d][0x%x] Res[%d][0x%016" PRIx64
2621 "] failed rc=%d.\n",
2622 tbl_type, fid, res_parms.resource_func,
2623 res_parms.resource_hndl, trc);
2625 /* All subsequent call require the non-critical_resource */
2626 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
2628 rc = ulp_flow_db_resource_del(ulp_ctx,
2634 /* Free the Flow ID since we've removed all resources */
2635 rc = ulp_flow_db_fid_free(ulp_ctx, tbl_type, fid);
2641 ulp_mapper_glb_resource_info_deinit(struct bnxt_ulp_context *ulp_ctx,
2642 struct bnxt_ulp_mapper_data *mapper_data)
2644 struct bnxt_ulp_mapper_glb_resource_entry *ent;
2645 struct ulp_flow_db_res_params res;
2648 /* Iterate the global resources and process each one */
2649 for (dir = TF_DIR_RX; dir < TF_DIR_MAX; dir++) {
2650 for (idx = 0; idx < BNXT_ULP_GLB_RESOURCE_TBL_MAX_SZ;
2652 ent = &mapper_data->glb_res_tbl[dir][idx];
2653 if (ent->resource_func ==
2654 BNXT_ULP_RESOURCE_FUNC_INVALID)
2656 memset(&res, 0, sizeof(struct ulp_flow_db_res_params));
2657 res.resource_func = ent->resource_func;
2658 res.direction = dir;
2659 res.resource_type = ent->resource_type;
2660 /*convert it from BE to cpu */
2662 tfp_be_to_cpu_64(ent->resource_hndl);
2663 ulp_mapper_resource_free(ulp_ctx, &res);
2669 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx, uint32_t fid,
2670 enum bnxt_ulp_flow_db_tables flow_tbl_type)
2673 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
2677 return ulp_mapper_resources_free(ulp_ctx, fid, flow_tbl_type);
2680 /* Function to handle the default global templates that are allocated during
2681 * the startup and reused later.
2684 ulp_mapper_glb_template_table_init(struct bnxt_ulp_context *ulp_ctx)
2686 uint32_t *glbl_tmpl_list;
2687 uint32_t num_glb_tmpls, idx, dev_id;
2688 struct bnxt_ulp_mapper_parms parms;
2689 struct bnxt_ulp_mapper_data *mapper_data;
2692 glbl_tmpl_list = ulp_mapper_glb_template_table_get(&num_glb_tmpls);
2693 if (!glbl_tmpl_list || !num_glb_tmpls)
2694 return rc; /* No global templates to process */
2696 /* Get the device id from the ulp context */
2697 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
2698 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
2702 mapper_data = bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2704 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
2708 /* Iterate the global resources and process each one */
2709 for (idx = 0; idx < num_glb_tmpls; idx++) {
2710 /* Initialize the parms structure */
2711 memset(&parms, 0, sizeof(parms));
2712 parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2713 parms.ulp_ctx = ulp_ctx;
2714 parms.dev_id = dev_id;
2715 parms.mapper_data = mapper_data;
2717 /* Get the class table entry from dev id and class id */
2718 parms.class_tid = glbl_tmpl_list[idx];
2719 parms.ctbls = ulp_mapper_class_tbl_list_get(parms.dev_id,
2723 if (!parms.ctbls || !parms.num_ctbls) {
2724 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2725 parms.dev_id, parms.class_tid);
2728 parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
2729 if (!parms.device_params) {
2730 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2731 parms.dev_id, parms.class_tid);
2734 rc = ulp_mapper_class_tbls_process(&parms);
2737 "class tables failed creation for %d:%d\n",
2738 parms.dev_id, parms.class_tid);
2745 /* Function to handle the mapping of the Flow to be compatible
2746 * with the underlying hardware.
2749 ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
2750 struct bnxt_ulp_mapper_create_parms *cparms,
2753 struct bnxt_ulp_mapper_parms parms;
2754 struct ulp_regfile regfile;
2757 if (!ulp_ctx || !cparms)
2760 /* Initialize the parms structure */
2761 memset(&parms, 0, sizeof(parms));
2762 parms.act_prop = cparms->act_prop;
2763 parms.act_bitmap = cparms->act;
2764 parms.hdr_bitmap = cparms->hdr_bitmap;
2765 parms.regfile = ®file;
2766 parms.hdr_field = cparms->hdr_field;
2767 parms.comp_fld = cparms->comp_fld;
2768 parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2769 parms.ulp_ctx = ulp_ctx;
2770 parms.tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
2772 /* Get the device id from the ulp context */
2773 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &parms.dev_id)) {
2774 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
2779 * Get the mapper data for dynamic mapper data such as default
2782 parms.mapper_data = (struct bnxt_ulp_mapper_data *)
2783 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2784 if (!parms.mapper_data) {
2785 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
2789 /* Get the action table entry from device id and act context id */
2790 parms.act_tid = cparms->act_tid;
2793 * Perform the action table get only if act template is not zero
2794 * for act template zero like for default rules ignore the action
2797 if (parms.act_tid) {
2798 parms.atbls = ulp_mapper_action_tbl_list_get(parms.dev_id,
2801 if (!parms.atbls || !parms.num_atbls) {
2802 BNXT_TF_DBG(ERR, "No action tables for %d:%d\n",
2803 parms.dev_id, parms.act_tid);
2808 /* Get the class table entry from device id and act context id */
2809 parms.class_tid = cparms->class_tid;
2810 parms.ctbls = ulp_mapper_class_tbl_list_get(parms.dev_id,
2814 if (!parms.ctbls || !parms.num_ctbls) {
2815 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2816 parms.dev_id, parms.class_tid);
2820 /* Get the device params, it will be used in later processing */
2821 parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
2822 if (!parms.device_params) {
2823 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2824 parms.dev_id, parms.class_tid);
2828 /* initialize the registry file for further processing */
2829 if (!ulp_regfile_init(parms.regfile)) {
2830 BNXT_TF_DBG(ERR, "regfile initialization failed.\n");
2834 rc = ulp_regfile_write(parms.regfile,
2835 BNXT_ULP_REGFILE_INDEX_CLASS_TID,
2836 tfp_cpu_to_be_64((uint64_t)parms.class_tid));
2838 BNXT_TF_DBG(ERR, "Unable to write template ID to regfile\n");
2842 /* Allocate a Flow ID for attaching all resources for the flow to.
2843 * Once allocated, all errors have to walk the list of resources and
2844 * free each of them.
2846 rc = ulp_flow_db_fid_alloc(ulp_ctx,
2851 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
2855 /* Process the action template list from the selected action table*/
2856 if (parms.act_tid) {
2857 rc = ulp_mapper_action_tbls_process(&parms);
2860 "action tables failed creation for %d:%d\n",
2861 parms.dev_id, parms.act_tid);
2866 /* All good. Now process the class template */
2867 rc = ulp_mapper_class_tbls_process(&parms);
2869 BNXT_TF_DBG(ERR, "class tables failed creation for %d:%d\n",
2870 parms.dev_id, parms.class_tid);
2874 *flowid = parms.fid;
2879 /* Free all resources that were allocated during flow creation */
2880 trc = ulp_mapper_flow_destroy(ulp_ctx, parms.fid,
2881 BNXT_ULP_REGULAR_FLOW_TABLE);
2883 BNXT_TF_DBG(ERR, "Failed to free all resources rc=%d\n", trc);
2889 ulp_mapper_init(struct bnxt_ulp_context *ulp_ctx)
2891 struct bnxt_ulp_cache_tbl_params *tbl;
2892 struct bnxt_ulp_mapper_data *data;
2900 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2904 data = rte_zmalloc("ulp_mapper_data",
2905 sizeof(struct bnxt_ulp_mapper_data), 0);
2907 BNXT_TF_DBG(ERR, "Failed to allocate the mapper data\n");
2911 if (bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, data)) {
2912 BNXT_TF_DBG(ERR, "Failed to set mapper data in context\n");
2913 /* Don't call deinit since the prof_func wasn't allocated. */
2918 /* Allocate the global resource ids */
2919 rc = ulp_mapper_glb_resource_info_init(ulp_ctx, data);
2921 BNXT_TF_DBG(ERR, "Failed to initialize global resource ids\n");
2925 /* Allocate the ulp cache tables. */
2926 for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2927 tbl = ulp_mapper_cache_tbl_params_get(i);
2929 BNXT_TF_DBG(ERR, "Failed to get cache table parms (%d)",
2933 if (tbl->num_entries != 0) {
2934 csize = sizeof(struct bnxt_ulp_mapper_cache_entry) *
2936 data->cache_tbl[i] = rte_zmalloc("ulp mapper cache tbl",
2938 if (!data->cache_tbl[i]) {
2939 BNXT_TF_DBG(ERR, "Failed to allocate Cache "
2947 rc = ulp_mapper_glb_template_table_init(ulp_ctx);
2949 BNXT_TF_DBG(ERR, "Failed to initialize global templates\n");
2955 /* Ignore the return code in favor of returning the original error. */
2956 ulp_mapper_deinit(ulp_ctx);
2961 ulp_mapper_deinit(struct bnxt_ulp_context *ulp_ctx)
2963 struct bnxt_ulp_mapper_data *data;
2969 "Failed to acquire ulp context, so data may "
2970 "not be released.\n");
2974 data = (struct bnxt_ulp_mapper_data *)
2975 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2977 /* Go ahead and return since there is no allocated data. */
2978 BNXT_TF_DBG(ERR, "No data appears to have been allocated.\n");
2982 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2984 BNXT_TF_DBG(ERR, "Failed to acquire tfp.\n");
2985 /* Free the mapper data regardless of errors. */
2986 goto free_mapper_data;
2989 /* Free the global resource info table entries */
2990 ulp_mapper_glb_resource_info_deinit(ulp_ctx, data);
2993 /* Free the ulp cache tables */
2994 for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2995 rte_free(data->cache_tbl[i]);
2996 data->cache_tbl[i] = NULL;
3000 /* Reset the data pointer within the ulp_ctx. */
3001 bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, NULL);