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"
20 static struct bnxt_ulp_glb_resource_info *
21 ulp_mapper_glb_resource_info_list_get(uint32_t *num_entries)
25 *num_entries = BNXT_ULP_GLB_RESOURCE_INFO_TBL_MAX_SZ;
26 return ulp_glb_resource_tbl;
30 * Read the global resource from the mapper global resource list
32 * The regval is always returned in big-endian.
34 * returns 0 on success
37 ulp_mapper_glb_resource_read(struct bnxt_ulp_mapper_data *mapper_data,
42 if (!mapper_data || !regval ||
43 dir >= TF_DIR_MAX || idx >= BNXT_ULP_GLB_REGFILE_INDEX_LAST)
46 *regval = mapper_data->glb_res_tbl[dir][idx].resource_hndl;
51 * Write a global resource to the mapper global resource list
53 * The regval value must be in big-endian.
55 * return 0 on success.
58 ulp_mapper_glb_resource_write(struct bnxt_ulp_mapper_data *data,
59 struct bnxt_ulp_glb_resource_info *res,
62 struct bnxt_ulp_mapper_glb_resource_entry *ent;
64 /* validate the arguments */
65 if (!data || res->direction >= TF_DIR_MAX ||
66 res->glb_regfile_index >= BNXT_ULP_GLB_REGFILE_INDEX_LAST)
69 /* write to the mapper data */
70 ent = &data->glb_res_tbl[res->direction][res->glb_regfile_index];
71 ent->resource_func = res->resource_func;
72 ent->resource_type = res->resource_type;
73 ent->resource_hndl = regval;
78 * Internal function to allocate identity resource and store it in mapper data.
80 * returns 0 on success
83 ulp_mapper_resource_ident_allocate(struct tf *tfp,
84 struct bnxt_ulp_mapper_data *mapper_data,
85 struct bnxt_ulp_glb_resource_info *glb_res)
87 struct tf_alloc_identifier_parms iparms = { 0 };
88 struct tf_free_identifier_parms fparms;
92 iparms.ident_type = glb_res->resource_type;
93 iparms.dir = glb_res->direction;
95 /* Allocate the Identifier using tf api */
96 rc = tf_alloc_identifier(tfp, &iparms);
98 BNXT_TF_DBG(ERR, "Failed to alloc identifier [%s][%d]\n",
99 (iparms.dir == TF_DIR_RX) ? "RX" : "TX",
104 /* entries are stored as big-endian format */
105 regval = tfp_cpu_to_be_64((uint64_t)iparms.id);
106 /* write to the mapper global resource */
107 rc = ulp_mapper_glb_resource_write(mapper_data, glb_res, regval);
109 BNXT_TF_DBG(ERR, "Failed to write to global resource id\n");
110 /* Free the identifier when update failed */
111 fparms.dir = iparms.dir;
112 fparms.ident_type = iparms.ident_type;
113 fparms.id = iparms.id;
114 tf_free_identifier(tfp, &fparms);
117 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
118 BNXT_TF_DBG(DEBUG, "Allocated Glb Res[%s][%d][%d] = 0x%04x\n",
119 (iparms.dir == TF_DIR_RX) ? "RX" : "TX",
120 glb_res->glb_regfile_index, iparms.ident_type, iparms.id);
125 /* Retrieve the cache initialization parameters for the tbl_idx */
126 static struct bnxt_ulp_cache_tbl_params *
127 ulp_mapper_cache_tbl_params_get(uint32_t tbl_idx)
129 if (tbl_idx >= BNXT_ULP_CACHE_TBL_MAX_SZ)
132 return &ulp_cache_tbl_params[tbl_idx];
136 * Get the size of the action property for a given index.
138 * idx [in] The index for the action property
140 * returns the size of the action property.
143 ulp_mapper_act_prop_size_get(uint32_t idx)
145 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST)
147 return ulp_act_prop_map_table[idx];
151 * Get the list of result fields that implement the flow action.
152 * Gets a device dependent list of tables that implement the action template id.
154 * dev_id [in] The device id of the forwarding element
156 * tid [in] The action template id that matches the flow
158 * num_tbls [out] The number of action tables in the returned array
160 * Returns An array of action tables to implement the flow, or NULL on error.
162 static struct bnxt_ulp_mapper_tbl_info *
163 ulp_mapper_action_tbl_list_get(uint32_t dev_id,
171 BNXT_TF_DBG(ERR, "Invalid arguments\n");
175 /* template shift and device mask */
176 tidx = ULP_DEVICE_PARAMS_INDEX(tid, dev_id);
178 /* NOTE: Need to have something from template compiler to help validate
179 * range of dev_id and act_tid
181 idx = ulp_act_tmpl_list[tidx].start_tbl_idx;
182 *num_tbls = ulp_act_tmpl_list[tidx].num_tbls;
184 return &ulp_act_tbl_list[idx];
187 /** Get a list of classifier tables that implement the flow
188 * Gets a device dependent list of tables that implement the class template id
190 * dev_id [in] The device id of the forwarding element
192 * tid [in] The template id that matches the flow
194 * num_tbls [out] The number of classifier tables in the returned array
196 * returns An array of classifier tables to implement the flow, or NULL on
199 static struct bnxt_ulp_mapper_tbl_info *
200 ulp_mapper_class_tbl_list_get(uint32_t dev_id,
205 uint32_t tidx = ULP_DEVICE_PARAMS_INDEX(tid, dev_id);
210 /* NOTE: Need to have something from template compiler to help validate
211 * range of dev_id and tid
213 idx = ulp_class_tmpl_list[tidx].start_tbl_idx;
214 *num_tbls = ulp_class_tmpl_list[tidx].num_tbls;
216 return &ulp_class_tbl_list[idx];
220 * Get the list of key fields that implement the flow.
222 * ctxt [in] The ulp context
224 * tbl [in] A single table instance to get the key fields from
226 * num_flds [out] The number of key fields in the returned array
228 * Returns array of Key fields, or NULL on error.
230 static struct bnxt_ulp_mapper_class_key_field_info *
231 ulp_mapper_key_fields_get(struct bnxt_ulp_mapper_tbl_info *tbl,
236 if (!tbl || !num_flds)
239 idx = tbl->key_start_idx;
240 *num_flds = tbl->key_num_fields;
242 /* NOTE: Need template to provide range checking define */
243 return &ulp_class_key_field_list[idx];
247 * Get the list of data fields that implement the flow.
249 * ctxt [in] The ulp context
251 * tbl [in] A single table instance to get the data fields from
253 * num_flds [out] The number of data fields in the returned array.
255 * Returns array of data fields, or NULL on error.
257 static struct bnxt_ulp_mapper_result_field_info *
258 ulp_mapper_result_fields_get(struct bnxt_ulp_mapper_tbl_info *tbl,
263 if (!tbl || !num_flds)
266 idx = tbl->result_start_idx;
267 *num_flds = tbl->result_num_fields;
269 /* NOTE: Need template to provide range checking define */
270 return &ulp_class_result_field_list[idx];
274 * Get the list of result fields that implement the flow action.
276 * tbl [in] A single table instance to get the results fields
277 * from num_flds [out] The number of data fields in the returned
280 * Returns array of data fields, or NULL on error.
282 static struct bnxt_ulp_mapper_result_field_info *
283 ulp_mapper_act_result_fields_get(struct bnxt_ulp_mapper_tbl_info *tbl,
284 uint32_t *num_rslt_flds,
285 uint32_t *num_encap_flds)
289 if (!tbl || !num_rslt_flds || !num_encap_flds)
292 idx = tbl->result_start_idx;
293 *num_rslt_flds = tbl->result_num_fields;
294 *num_encap_flds = tbl->encap_num_fields;
296 /* NOTE: Need template to provide range checking define */
297 return &ulp_act_result_field_list[idx];
301 * Get the list of ident fields that implement the flow
303 * tbl [in] A single table instance to get the ident fields from
305 * num_flds [out] The number of ident fields in the returned array
307 * returns array of ident fields, or NULL on error
309 static struct bnxt_ulp_mapper_ident_info *
310 ulp_mapper_ident_fields_get(struct bnxt_ulp_mapper_tbl_info *tbl,
315 if (!tbl || !num_flds)
318 idx = tbl->ident_start_idx;
319 *num_flds = tbl->ident_nums;
321 /* NOTE: Need template to provide range checking define */
322 return &ulp_ident_list[idx];
325 static struct bnxt_ulp_mapper_cache_entry *
326 ulp_mapper_cache_entry_get(struct bnxt_ulp_context *ulp,
330 struct bnxt_ulp_mapper_data *mapper_data;
332 mapper_data = bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp);
333 if (!mapper_data || id >= BNXT_ULP_CACHE_TBL_MAX_SZ ||
334 !mapper_data->cache_tbl[id]) {
335 BNXT_TF_DBG(ERR, "Unable to acquire the cache tbl (%d)\n", id);
339 return &mapper_data->cache_tbl[id][key];
343 * Concatenates the tbl_type and tbl_id into a 32bit value for storing in the
344 * resource_type. This is done to conserve memory since both the tbl_type and
348 ulp_mapper_cache_res_type_set(struct ulp_flow_db_res_params *res,
352 res->resource_type = tbl_type;
353 res->resource_sub_type = tbl_id;
356 /* Extracts the tbl_type and tbl_id from the 32bit resource type. */
358 ulp_mapper_cache_res_type_get(struct ulp_flow_db_res_params *res,
362 *tbl_type = res->resource_type;
363 *tbl_id = res->resource_sub_type;
367 ulp_mapper_cache_entry_free(struct bnxt_ulp_context *ulp,
369 struct ulp_flow_db_res_params *res)
371 struct bnxt_ulp_mapper_cache_entry *cache_entry;
372 struct tf_free_identifier_parms ident_parms;
373 struct tf_free_tcam_entry_parms tcam_parms;
374 uint16_t table_id, table_type;
378 * The table id, used for cache, and table_type, used for tcam, are
379 * both encoded within the resource. We must first extract them to
380 * formulate the args for tf calls.
382 ulp_mapper_cache_res_type_get(res, &table_type, &table_id);
383 cache_entry = ulp_mapper_cache_entry_get(ulp, table_id,
384 (uint16_t)res->resource_hndl);
385 if (!cache_entry || !cache_entry->ref_count) {
386 BNXT_TF_DBG(ERR, "Cache entry (%d:%d) not valid on free.\n",
387 table_id, (uint16_t)res->resource_hndl);
392 * See if we need to delete the entry. The tcam and identifiers are all
393 * tracked by the cached entries reference count. All are deleted when
394 * the reference count hit zero.
396 cache_entry->ref_count--;
397 if (cache_entry->ref_count)
401 * Need to delete the tcam entry and the allocated identifiers.
402 * In the event of a failure, need to try to delete the remaining
403 * resources before returning error.
405 tcam_parms.dir = res->direction;
406 tcam_parms.tcam_tbl_type = table_type;
407 tcam_parms.idx = cache_entry->tcam_idx;
408 rc = tf_free_tcam_entry(tfp, &tcam_parms);
410 BNXT_TF_DBG(ERR, "Failed to free tcam [%d][%s][0x%04x] rc=%d\n",
412 (res->direction == TF_DIR_RX) ? "RX" : "TX",
416 * Free the identifiers associated with the tcam entry. Entries with
417 * negative one are considered uninitialized.
419 for (i = 0; i < BNXT_ULP_CACHE_TBL_IDENT_MAX_NUM; i++) {
420 if (cache_entry->idents[i] == ULP_IDENTS_INVALID)
423 ident_parms.dir = res->direction;
424 ident_parms.ident_type = cache_entry->ident_types[i];
425 ident_parms.id = cache_entry->idents[i];
426 trc = tf_free_identifier(tfp, &ident_parms);
428 BNXT_TF_DBG(ERR, "Failed to free identifier "
429 "[%d][%s][0x%04x] rc=%d\n",
430 ident_parms.ident_type,
431 (res->direction == TF_DIR_RX) ? "RX" : "TX",
432 ident_parms.id, trc);
440 static inline int32_t
441 ulp_mapper_tcam_entry_free(struct bnxt_ulp_context *ulp __rte_unused,
443 struct ulp_flow_db_res_params *res)
445 struct tf_free_tcam_entry_parms fparms = {
446 .dir = res->direction,
447 .tcam_tbl_type = res->resource_type,
448 .idx = (uint16_t)res->resource_hndl
451 return tf_free_tcam_entry(tfp, &fparms);
454 static inline int32_t
455 ulp_mapper_index_entry_free(struct bnxt_ulp_context *ulp,
457 struct ulp_flow_db_res_params *res)
459 struct tf_free_tbl_entry_parms fparms = {
460 .dir = res->direction,
461 .type = res->resource_type,
462 .idx = (uint32_t)res->resource_hndl
466 * Just set the table scope, it will be ignored if not necessary
467 * by the tf_free_tbl_entry
469 bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
471 return tf_free_tbl_entry(tfp, &fparms);
474 static inline int32_t
475 ulp_mapper_eem_entry_free(struct bnxt_ulp_context *ulp,
477 struct ulp_flow_db_res_params *res)
479 struct tf_delete_em_entry_parms fparms = { 0 };
482 fparms.dir = res->direction;
483 fparms.mem = TF_MEM_EXTERNAL;
484 fparms.flow_handle = res->resource_hndl;
486 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
488 BNXT_TF_DBG(ERR, "Failed to get table scope\n");
492 return tf_delete_em_entry(tfp, &fparms);
495 static inline int32_t
496 ulp_mapper_ident_free(struct bnxt_ulp_context *ulp __rte_unused,
498 struct ulp_flow_db_res_params *res)
500 struct tf_free_identifier_parms fparms = {
501 .dir = res->direction,
502 .ident_type = res->resource_type,
503 .id = (uint16_t)res->resource_hndl
506 return tf_free_identifier(tfp, &fparms);
509 static inline int32_t
510 ulp_mapper_mark_free(struct bnxt_ulp_context *ulp,
511 struct ulp_flow_db_res_params *res)
513 return ulp_mark_db_mark_del(ulp,
519 * Process the identifier instruction and either store it in the flow database
520 * or return it in the val (if not NULL) on success. If val is NULL, the
521 * identifier is to be stored in the flow database.
524 ulp_mapper_ident_process(struct bnxt_ulp_mapper_parms *parms,
525 struct bnxt_ulp_mapper_tbl_info *tbl,
526 struct bnxt_ulp_mapper_ident_info *ident,
529 struct ulp_flow_db_res_params fid_parms;
532 struct tf_alloc_identifier_parms iparms = { 0 };
533 struct tf_free_identifier_parms free_parms = { 0 };
537 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
539 BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
543 idx = ident->regfile_idx;
545 iparms.ident_type = ident->ident_type;
546 iparms.dir = tbl->direction;
548 rc = tf_alloc_identifier(tfp, &iparms);
550 BNXT_TF_DBG(ERR, "Alloc ident %s:%d failed.\n",
551 (iparms.dir == TF_DIR_RX) ? "RX" : "TX",
556 id = (uint64_t)tfp_cpu_to_be_64(iparms.id);
557 if (!ulp_regfile_write(parms->regfile, idx, id)) {
558 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
560 /* Need to free the identifier, so goto error */
564 /* Link the resource to the flow in the flow db */
566 memset(&fid_parms, 0, sizeof(fid_parms));
567 fid_parms.direction = tbl->direction;
568 fid_parms.resource_func = ident->resource_func;
569 fid_parms.resource_type = ident->ident_type;
570 fid_parms.resource_hndl = iparms.id;
571 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
573 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
578 BNXT_TF_DBG(ERR, "Failed to link res to flow rc = %d\n",
580 /* Need to free the identifier, so goto error */
590 /* Need to free the identifier */
591 free_parms.dir = tbl->direction;
592 free_parms.ident_type = ident->ident_type;
593 free_parms.id = iparms.id;
595 (void)tf_free_identifier(tfp, &free_parms);
597 BNXT_TF_DBG(ERR, "Ident process failed for %s:%s\n",
599 (tbl->direction == TF_DIR_RX) ? "RX" : "TX");
604 ulp_mapper_result_field_process(struct bnxt_ulp_mapper_parms *parms,
606 struct bnxt_ulp_mapper_result_field_info *fld,
607 struct ulp_blob *blob,
610 uint16_t idx, size_idx;
613 uint32_t val_size = 0, field_size = 0;
617 switch (fld->result_opcode) {
618 case BNXT_ULP_MAPPER_OPC_SET_TO_CONSTANT:
619 val = fld->result_operand;
620 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
621 BNXT_TF_DBG(ERR, "%s failed to add field\n", name);
625 case BNXT_ULP_MAPPER_OPC_SET_TO_ACT_PROP:
626 if (!ulp_operand_read(fld->result_operand,
627 (uint8_t *)&idx, sizeof(uint16_t))) {
628 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
631 idx = tfp_be_to_cpu_16(idx);
633 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
634 BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n", name, idx);
637 val = &parms->act_prop->act_details[idx];
638 field_size = ulp_mapper_act_prop_size_get(idx);
639 if (fld->field_bit_size < ULP_BYTE_2_BITS(field_size)) {
640 field_size = field_size -
641 ((fld->field_bit_size + 7) / 8);
644 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
645 BNXT_TF_DBG(ERR, "%s push field failed\n", name);
649 case BNXT_ULP_MAPPER_OPC_SET_TO_ACT_BIT:
650 if (!ulp_operand_read(fld->result_operand,
651 (uint8_t *)&act_bit, sizeof(uint64_t))) {
652 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
655 act_bit = tfp_be_to_cpu_64(act_bit);
656 act_val = ULP_BITMAP_ISSET(parms->act_bitmap->bits, act_bit);
657 if (fld->field_bit_size > ULP_BYTE_2_BITS(sizeof(act_val))) {
658 BNXT_TF_DBG(ERR, "%s field size is incorrect\n", name);
661 if (!ulp_blob_push(blob, &act_val, fld->field_bit_size)) {
662 BNXT_TF_DBG(ERR, "%s push field failed\n", name);
667 case BNXT_ULP_MAPPER_OPC_SET_TO_ENCAP_ACT_PROP_SZ:
668 if (!ulp_operand_read(fld->result_operand,
669 (uint8_t *)&idx, sizeof(uint16_t))) {
670 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
673 idx = tfp_be_to_cpu_16(idx);
675 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
676 BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n", name, idx);
679 val = &parms->act_prop->act_details[idx];
681 /* get the size index next */
682 if (!ulp_operand_read(&fld->result_operand[sizeof(uint16_t)],
683 (uint8_t *)&size_idx, sizeof(uint16_t))) {
684 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
687 size_idx = tfp_be_to_cpu_16(size_idx);
689 if (size_idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
690 BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", size_idx);
693 memcpy(&val_size, &parms->act_prop->act_details[size_idx],
695 val_size = tfp_be_to_cpu_32(val_size);
696 val_size = ULP_BYTE_2_BITS(val_size);
697 ulp_blob_push_encap(blob, val, val_size);
699 case BNXT_ULP_MAPPER_OPC_SET_TO_REGFILE:
700 if (!ulp_operand_read(fld->result_operand,
701 (uint8_t *)&idx, sizeof(uint16_t))) {
702 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
706 idx = tfp_be_to_cpu_16(idx);
707 /* Uninitialized regfile entries return 0 */
708 if (!ulp_regfile_read(parms->regfile, idx, ®val)) {
709 BNXT_TF_DBG(ERR, "%s regfile[%d] read oob\n",
714 val = ulp_blob_push_64(blob, ®val, fld->field_bit_size);
716 BNXT_TF_DBG(ERR, "%s push field failed\n", name);
720 case BNXT_ULP_MAPPER_OPC_SET_TO_GLB_REGFILE:
721 if (!ulp_operand_read(fld->result_operand,
724 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
727 idx = tfp_be_to_cpu_16(idx);
728 if (ulp_mapper_glb_resource_read(parms->mapper_data,
731 BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
735 val = ulp_blob_push_64(blob, ®val, fld->field_bit_size);
737 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
741 case BNXT_ULP_MAPPER_OPC_SET_TO_COMP_FIELD:
742 if (!ulp_operand_read(fld->result_operand,
745 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
748 idx = tfp_be_to_cpu_16(idx);
749 if (idx < BNXT_ULP_CF_IDX_LAST)
750 val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
751 fld->field_bit_size);
753 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
757 case BNXT_ULP_MAPPER_OPC_SET_TO_ZERO:
758 if (ulp_blob_pad_push(blob, fld->field_bit_size) < 0) {
759 BNXT_TF_DBG(ERR, "%s too large for blob\n", name);
770 /* Function to alloc action record and set the table. */
772 ulp_mapper_keymask_field_process(struct bnxt_ulp_mapper_parms *parms,
774 struct bnxt_ulp_mapper_class_key_field_info *f,
775 struct ulp_blob *blob,
780 uint16_t idx, bitlen;
783 struct ulp_regfile *regfile = parms->regfile;
785 struct bnxt_ulp_mapper_class_key_field_info *fld = f;
789 operand = fld->spec_operand;
790 opcode = fld->spec_opcode;
792 operand = fld->mask_operand;
793 opcode = fld->mask_opcode;
796 bitlen = fld->field_bit_size;
799 case BNXT_ULP_MAPPER_OPC_SET_TO_CONSTANT:
801 if (!ulp_blob_push(blob, val, bitlen)) {
802 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
806 case BNXT_ULP_MAPPER_OPC_SET_TO_ZERO:
807 if (ulp_blob_pad_push(blob, bitlen) < 0) {
808 BNXT_TF_DBG(ERR, "%s pad too large for blob\n", name);
813 case BNXT_ULP_MAPPER_OPC_SET_TO_HDR_FIELD:
814 if (!ulp_operand_read(operand, (uint8_t *)&idx,
816 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
819 idx = tfp_be_to_cpu_16(idx);
821 val = parms->hdr_field[idx].spec;
823 val = parms->hdr_field[idx].mask;
826 * Need to account for how much data was pushed to the header
827 * field vs how much is to be inserted in the key/mask.
829 field_size = parms->hdr_field[idx].size;
830 if (bitlen < ULP_BYTE_2_BITS(field_size)) {
831 field_size = field_size - ((bitlen + 7) / 8);
835 if (!ulp_blob_push(blob, val, bitlen)) {
836 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
840 case BNXT_ULP_MAPPER_OPC_SET_TO_COMP_FIELD:
841 if (!ulp_operand_read(operand, (uint8_t *)&idx,
843 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
846 idx = tfp_be_to_cpu_16(idx);
847 if (idx < BNXT_ULP_CF_IDX_LAST)
848 val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
851 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
855 case BNXT_ULP_MAPPER_OPC_SET_TO_REGFILE:
856 if (!ulp_operand_read(operand, (uint8_t *)&idx,
858 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
861 idx = tfp_be_to_cpu_16(idx);
863 if (!ulp_regfile_read(regfile, idx, &val64)) {
864 BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
869 val = ulp_blob_push_64(blob, &val64, bitlen);
871 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
875 case BNXT_ULP_MAPPER_OPC_SET_TO_GLB_REGFILE:
876 if (!ulp_operand_read(operand, (uint8_t *)&idx,
878 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
881 idx = tfp_be_to_cpu_16(idx);
882 if (ulp_mapper_glb_resource_read(parms->mapper_data,
885 BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
889 val = ulp_blob_push_64(blob, &val64, bitlen);
891 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
903 ulp_mapper_mark_gfid_process(struct bnxt_ulp_mapper_parms *parms,
904 struct bnxt_ulp_mapper_tbl_info *tbl,
907 enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
908 struct ulp_flow_db_res_params fid_parms;
909 uint32_t mark, gfid, mark_flag;
912 if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
913 !(mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION &&
914 ULP_BITMAP_ISSET(parms->act_bitmap->bits,
915 BNXT_ULP_ACTION_BIT_MARK)))
916 return rc; /* no need to perform gfid process */
918 /* Get the mark id details from action property */
919 memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
921 mark = tfp_be_to_cpu_32(mark);
923 TF_GET_GFID_FROM_FLOW_ID(flow_id, gfid);
924 mark_flag = BNXT_ULP_MARK_GLOBAL_HW_FID;
925 rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
928 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
931 fid_parms.direction = tbl->direction;
932 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
933 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
934 fid_parms.resource_type = mark_flag;
935 fid_parms.resource_hndl = gfid;
936 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
941 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
946 ulp_mapper_mark_act_ptr_process(struct bnxt_ulp_mapper_parms *parms,
947 struct bnxt_ulp_mapper_tbl_info *tbl)
949 enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
950 struct ulp_flow_db_res_params fid_parms;
951 uint32_t act_idx, mark, mark_flag;
955 if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
956 !(mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION &&
957 ULP_BITMAP_ISSET(parms->act_bitmap->bits,
958 BNXT_ULP_ACTION_BIT_MARK)))
959 return rc; /* no need to perform mark action process */
961 /* Get the mark id details from action property */
962 memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
964 mark = tfp_be_to_cpu_32(mark);
966 if (!ulp_regfile_read(parms->regfile,
967 BNXT_ULP_REGFILE_INDEX_MAIN_ACTION_PTR,
969 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
972 act_idx = tfp_be_to_cpu_64(val64);
973 mark_flag = BNXT_ULP_MARK_LOCAL_HW_FID;
974 rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
977 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
980 fid_parms.direction = tbl->direction;
981 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
982 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
983 fid_parms.resource_type = mark_flag;
984 fid_parms.resource_hndl = act_idx;
985 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
990 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
995 ulp_mapper_mark_vfr_idx_process(struct bnxt_ulp_mapper_parms *parms,
996 struct bnxt_ulp_mapper_tbl_info *tbl)
998 struct ulp_flow_db_res_params fid_parms;
999 uint32_t act_idx, mark, mark_flag;
1001 enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1004 if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1005 mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION)
1006 return rc; /* no need to perform mark action process */
1008 /* Get the mark id details from the computed field of dev port id */
1009 mark = ULP_COMP_FLD_IDX_RD(parms, BNXT_ULP_CF_IDX_DEV_PORT_ID);
1011 /* Get the main action pointer */
1012 if (!ulp_regfile_read(parms->regfile,
1013 BNXT_ULP_REGFILE_INDEX_MAIN_ACTION_PTR,
1015 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1018 act_idx = tfp_be_to_cpu_64(val64);
1020 /* Set the mark flag to local fid and vfr flag */
1021 mark_flag = BNXT_ULP_MARK_LOCAL_HW_FID | BNXT_ULP_MARK_VFR_ID;
1023 rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1026 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1029 fid_parms.direction = tbl->direction;
1030 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1031 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1032 fid_parms.resource_type = mark_flag;
1033 fid_parms.resource_hndl = act_idx;
1034 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1039 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1044 ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1045 struct bnxt_ulp_mapper_tbl_info *tbl)
1047 struct bnxt_ulp_mapper_class_key_field_info *kflds;
1048 struct ulp_blob key, mask, data;
1049 uint32_t i, num_kflds;
1052 struct tf_alloc_tcam_entry_parms aparms = { 0 };
1053 struct tf_set_tcam_entry_parms sparms = { 0 };
1054 struct ulp_flow_db_res_params fid_parms = { 0 };
1055 struct tf_free_tcam_entry_parms free_parms = { 0 };
1057 uint16_t tmplen = 0;
1059 /* Skip this if was handled by the cache. */
1060 if (parms->tcam_tbl_opc == BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_SKIP) {
1061 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1065 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1067 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1071 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
1072 if (!kflds || !num_kflds) {
1073 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1077 if (!ulp_blob_init(&key, tbl->key_bit_size, parms->order) ||
1078 !ulp_blob_init(&mask, tbl->key_bit_size, parms->order) ||
1079 !ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
1080 BNXT_TF_DBG(ERR, "blob inits failed.\n");
1084 /* create the key/mask */
1086 * NOTE: The WC table will require some kind of flag to handle the
1087 * mode bits within the key/mask
1089 for (i = 0; i < num_kflds; i++) {
1091 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1093 &key, 1, "TCAM Key");
1095 BNXT_TF_DBG(ERR, "Key field set failed.\n");
1099 /* Setup the mask */
1100 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1102 &mask, 0, "TCAM Mask");
1104 BNXT_TF_DBG(ERR, "Mask field set failed.\n");
1109 aparms.dir = tbl->direction;
1110 aparms.tcam_tbl_type = tbl->resource_type;
1111 aparms.search_enable = tbl->srch_b4_alloc;
1112 aparms.key_sz_in_bits = tbl->key_bit_size;
1113 aparms.key = ulp_blob_data_get(&key, &tmplen);
1114 if (tbl->key_bit_size != tmplen) {
1115 BNXT_TF_DBG(ERR, "Key len (%d) != Expected (%d)\n",
1116 tmplen, tbl->key_bit_size);
1120 aparms.mask = ulp_blob_data_get(&mask, &tmplen);
1121 if (tbl->key_bit_size != tmplen) {
1122 BNXT_TF_DBG(ERR, "Mask len (%d) != Expected (%d)\n",
1123 tmplen, tbl->key_bit_size);
1127 aparms.priority = tbl->priority;
1130 * All failures after this succeeds require the entry to be freed.
1131 * cannot return directly on failure, but needs to goto error
1133 rc = tf_alloc_tcam_entry(tfp, &aparms);
1135 BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
1141 /* Build the result */
1142 if (!tbl->srch_b4_alloc || !hit) {
1143 struct bnxt_ulp_mapper_result_field_info *dflds;
1144 struct bnxt_ulp_mapper_ident_info *idents;
1145 uint32_t num_dflds, num_idents;
1148 * Since the cache entry is responsible for allocating
1149 * identifiers when in use, allocate the identifiers only
1150 * during normal processing.
1152 if (parms->tcam_tbl_opc ==
1153 BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL) {
1154 idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
1156 for (i = 0; i < num_idents; i++) {
1157 rc = ulp_mapper_ident_process(parms, tbl,
1159 /* Already logged the error, just return */
1165 /* Create the result data blob */
1166 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds);
1167 if (!dflds || !num_dflds) {
1168 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
1173 for (i = 0; i < num_dflds; i++) {
1174 rc = ulp_mapper_result_field_process(parms,
1180 BNXT_TF_DBG(ERR, "Failed to set data fields\n");
1185 sparms.dir = aparms.dir;
1186 sparms.tcam_tbl_type = aparms.tcam_tbl_type;
1187 sparms.idx = aparms.idx;
1188 /* Already verified the key/mask lengths */
1189 sparms.key = ulp_blob_data_get(&key, &tmplen);
1190 sparms.mask = ulp_blob_data_get(&mask, &tmplen);
1191 sparms.key_sz_in_bits = tbl->key_bit_size;
1192 sparms.result = ulp_blob_data_get(&data, &tmplen);
1194 if (tbl->result_bit_size != tmplen) {
1195 BNXT_TF_DBG(ERR, "Result len (%d) != Expected (%d)\n",
1196 tmplen, tbl->result_bit_size);
1200 sparms.result_sz_in_bits = tbl->result_bit_size;
1202 rc = tf_set_tcam_entry(tfp, &sparms);
1204 BNXT_TF_DBG(ERR, "tcam[%d][%s][%d] write failed.\n",
1205 sparms.tcam_tbl_type,
1206 (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
1211 /* Update cache with TCAM index if the was cache allocated. */
1212 if (parms->tcam_tbl_opc ==
1213 BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_ALLOC) {
1214 if (!parms->cache_ptr) {
1215 BNXT_TF_DBG(ERR, "Unable to update cache");
1219 parms->cache_ptr->tcam_idx = aparms.idx;
1223 BNXT_TF_DBG(ERR, "Not supporting search before alloc now\n");
1229 * Only link the entry to the flow db in the event that cache was not
1232 if (parms->tcam_tbl_opc == BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL) {
1233 fid_parms.direction = tbl->direction;
1234 fid_parms.resource_func = tbl->resource_func;
1235 fid_parms.resource_type = tbl->resource_type;
1236 fid_parms.critical_resource = tbl->critical_resource;
1237 fid_parms.resource_hndl = aparms.idx;
1238 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1244 "Failed to link resource to flow rc = %d\n",
1246 /* Need to free the identifier, so goto error */
1251 * Reset the tcam table opcode to normal in case the next tcam
1252 * entry does not use cache.
1254 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1255 parms->cache_ptr = NULL;
1260 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1261 free_parms.dir = tbl->direction;
1262 free_parms.tcam_tbl_type = tbl->resource_type;
1263 free_parms.idx = aparms.idx;
1264 trc = tf_free_tcam_entry(tfp, &free_parms);
1266 BNXT_TF_DBG(ERR, "Failed to free tcam[%d][%d][%d] on failure\n",
1267 tbl->resource_type, tbl->direction, aparms.idx);
1273 ulp_mapper_em_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1274 struct bnxt_ulp_mapper_tbl_info *tbl)
1276 struct bnxt_ulp_mapper_class_key_field_info *kflds;
1277 struct bnxt_ulp_mapper_result_field_info *dflds;
1278 struct ulp_blob key, data;
1279 uint32_t i, num_kflds, num_dflds;
1281 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1282 struct ulp_flow_db_res_params fid_parms = { 0 };
1283 struct tf_insert_em_entry_parms iparms = { 0 };
1284 struct tf_delete_em_entry_parms free_parms = { 0 };
1288 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
1289 if (!kflds || !num_kflds) {
1290 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1294 /* Initialize the key/result blobs */
1295 if (!ulp_blob_init(&key, tbl->blob_key_bit_size, parms->order) ||
1296 !ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
1297 BNXT_TF_DBG(ERR, "blob inits failed.\n");
1301 /* create the key */
1302 for (i = 0; i < num_kflds; i++) {
1304 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1308 BNXT_TF_DBG(ERR, "Key field set failed.\n");
1314 * TBD: Normally should process identifiers in case of using recycle or
1315 * loopback. Not supporting recycle for now.
1318 /* Create the result data blob */
1319 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds);
1320 if (!dflds || !num_dflds) {
1321 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
1325 for (i = 0; i < num_dflds; i++) {
1326 struct bnxt_ulp_mapper_result_field_info *fld;
1330 rc = ulp_mapper_result_field_process(parms,
1336 BNXT_TF_DBG(ERR, "Failed to set data fields.\n");
1340 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1341 ulp_mapper_result_dump("EEM Result", tbl, &data);
1344 /* do the transpose for the internal EM keys */
1345 if (tbl->resource_type == TF_MEM_INTERNAL)
1346 ulp_blob_perform_byte_reverse(&key);
1348 rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
1349 &iparms.tbl_scope_id);
1351 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
1356 * NOTE: the actual blob size will differ from the size in the tbl
1357 * entry due to the padding.
1359 iparms.dup_check = 0;
1360 iparms.dir = tbl->direction;
1361 iparms.mem = tbl->resource_type;
1362 iparms.key = ulp_blob_data_get(&key, &tmplen);
1363 iparms.key_sz_in_bits = tbl->key_bit_size;
1364 iparms.em_record = ulp_blob_data_get(&data, &tmplen);
1365 iparms.em_record_sz_in_bits = tbl->result_bit_size;
1367 rc = tf_insert_em_entry(tfp, &iparms);
1369 BNXT_TF_DBG(ERR, "Failed to insert em entry rc=%d.\n", rc);
1373 /* Mark action process */
1374 if (parms->device_params->global_fid_enable &&
1375 tbl->resource_type == TF_MEM_EXTERNAL)
1376 rc = ulp_mapper_mark_gfid_process(parms, tbl, iparms.flow_id);
1377 else if (!parms->device_params->global_fid_enable &&
1378 tbl->resource_type == TF_MEM_INTERNAL)
1379 rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
1381 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1385 /* Link the EM resource to the flow in the flow db */
1386 memset(&fid_parms, 0, sizeof(fid_parms));
1387 fid_parms.direction = tbl->direction;
1388 fid_parms.resource_func = tbl->resource_func;
1389 fid_parms.resource_type = tbl->resource_type;
1390 fid_parms.critical_resource = tbl->critical_resource;
1391 fid_parms.resource_hndl = iparms.flow_handle;
1393 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1398 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1400 /* Need to free the identifier, so goto error */
1406 free_parms.dir = iparms.dir;
1407 free_parms.mem = iparms.mem;
1408 free_parms.tbl_scope_id = iparms.tbl_scope_id;
1409 free_parms.flow_handle = iparms.flow_handle;
1411 trc = tf_delete_em_entry(tfp, &free_parms);
1413 BNXT_TF_DBG(ERR, "Failed to delete EM entry on failed add\n");
1419 ulp_mapper_index_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1420 struct bnxt_ulp_mapper_tbl_info *tbl,
1423 struct bnxt_ulp_mapper_result_field_info *flds;
1424 struct ulp_flow_db_res_params fid_parms;
1425 struct ulp_blob data;
1428 uint32_t i, num_flds;
1429 int32_t rc = 0, trc = 0;
1430 struct tf_alloc_tbl_entry_parms aparms = { 0 };
1431 struct tf_set_tbl_entry_parms sparms = { 0 };
1432 struct tf_free_tbl_entry_parms free_parms = { 0 };
1433 uint32_t tbl_scope_id;
1434 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1436 uint32_t encap_flds = 0;
1438 /* Get the scope id first */
1439 rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx, &tbl_scope_id);
1441 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
1445 /* use the max size if encap is enabled */
1446 if (tbl->encap_num_fields)
1447 bit_size = BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS;
1449 bit_size = tbl->result_bit_size;
1451 /* Initialize the blob data */
1452 if (!ulp_blob_init(&data, bit_size,
1453 parms->device_params->byte_order)) {
1454 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
1458 /* Get the result fields list */
1460 flds = ulp_mapper_result_fields_get(tbl, &num_flds);
1462 flds = ulp_mapper_act_result_fields_get(tbl, &num_flds,
1465 if (!flds || !num_flds) {
1466 BNXT_TF_DBG(ERR, "template undefined for the index table\n");
1470 /* process the result fields, loop through them */
1471 for (i = 0; i < (num_flds + encap_flds); i++) {
1472 /* set the swap index if encap swap bit is enabled */
1473 if (parms->device_params->encap_byte_swap && encap_flds &&
1474 ((i + 1) == num_flds))
1475 ulp_blob_encap_swap_idx_set(&data);
1477 /* Process the result fields */
1478 rc = ulp_mapper_result_field_process(parms,
1484 BNXT_TF_DBG(ERR, "data field failed\n");
1488 /* if encap bit swap is enabled perform the bit swap */
1489 if (parms->device_params->encap_byte_swap && encap_flds) {
1490 if ((i + 1) == (num_flds + encap_flds))
1491 ulp_blob_perform_encap_swap(&data);
1492 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1493 if ((i + 1) == (num_flds + encap_flds)) {
1494 BNXT_TF_DBG(INFO, "Dump fter encap swap\n");
1495 ulp_mapper_blob_dump(&data);
1501 /* Perform the tf table allocation by filling the alloc params */
1502 aparms.dir = tbl->direction;
1503 aparms.type = tbl->resource_type;
1504 aparms.search_enable = tbl->srch_b4_alloc;
1505 aparms.result = ulp_blob_data_get(&data, &tmplen);
1506 aparms.result_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1507 aparms.tbl_scope_id = tbl_scope_id;
1509 /* All failures after the alloc succeeds require a free */
1510 rc = tf_alloc_tbl_entry(tfp, &aparms);
1512 BNXT_TF_DBG(ERR, "Alloc table[%d][%s] failed rc=%d\n",
1514 (aparms.dir == TF_DIR_RX) ? "RX" : "TX",
1520 * calculate the idx for the result record, for external EM the offset
1521 * needs to be shifted accordingly. If external non-inline table types
1522 * are used then need to revisit this logic.
1524 if (aparms.type == TF_TBL_TYPE_EXT)
1525 idx = TF_ACT_REC_OFFSET_2_PTR(aparms.idx);
1529 /* Always storing values in Regfile in BE */
1530 idx = tfp_cpu_to_be_64(idx);
1531 rc = ulp_regfile_write(parms->regfile, tbl->regfile_idx, idx);
1533 BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
1538 /* Perform the tf table set by filling the set params */
1539 if (!tbl->srch_b4_alloc || !aparms.hit) {
1540 sparms.dir = tbl->direction;
1541 sparms.type = tbl->resource_type;
1542 sparms.data = ulp_blob_data_get(&data, &tmplen);
1543 sparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1544 sparms.idx = aparms.idx;
1545 sparms.tbl_scope_id = tbl_scope_id;
1547 rc = tf_set_tbl_entry(tfp, &sparms);
1549 BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
1551 (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
1558 /* Link the resource to the flow in the flow db */
1559 memset(&fid_parms, 0, sizeof(fid_parms));
1560 fid_parms.direction = tbl->direction;
1561 fid_parms.resource_func = tbl->resource_func;
1562 fid_parms.resource_type = tbl->resource_type;
1563 fid_parms.resource_hndl = aparms.idx;
1564 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1566 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1571 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
1576 /* Perform the VF rep action */
1577 rc = ulp_mapper_mark_vfr_idx_process(parms, tbl);
1579 BNXT_TF_DBG(ERR, "Failed to add vfr mark rc = %d\n", rc);
1585 * Free the allocated resource since we failed to either
1586 * write to the entry or link the flow
1588 free_parms.dir = tbl->direction;
1589 free_parms.type = tbl->resource_type;
1590 free_parms.idx = aparms.idx;
1591 free_parms.tbl_scope_id = tbl_scope_id;
1593 trc = tf_free_tbl_entry(tfp, &free_parms);
1595 BNXT_TF_DBG(ERR, "Failed to free tbl entry on failure\n");
1601 ulp_mapper_cache_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1602 struct bnxt_ulp_mapper_tbl_info *tbl)
1604 struct bnxt_ulp_mapper_class_key_field_info *kflds;
1605 struct bnxt_ulp_mapper_cache_entry *cache_entry;
1606 struct bnxt_ulp_mapper_ident_info *idents;
1607 uint32_t i, num_kflds = 0, num_idents = 0;
1608 struct ulp_flow_db_res_params fid_parms;
1609 struct tf_free_identifier_parms fparms;
1610 uint16_t tmplen, tmp_ident;
1611 struct ulp_blob key;
1617 /* Get the key fields list and build the key. */
1618 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
1619 if (!kflds || !num_kflds) {
1620 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1623 if (!ulp_blob_init(&key, tbl->key_bit_size, parms->order)) {
1624 BNXT_TF_DBG(ERR, "Failed to alloc blob\n");
1627 for (i = 0; i < num_kflds; i++) {
1629 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1631 &key, 1, "Cache Key");
1634 "Failed to create key for Cache rc=%d\n",
1641 * Perform the lookup in the cache table with constructed key. The
1642 * cache_key is a byte array of tmplen, it needs to be converted to a
1643 * index for the cache table.
1645 cache_key = ulp_blob_data_get(&key, &tmplen);
1646 ckey = (uint16_t *)cache_key;
1649 * The id computed based on resource sub type and direction where
1650 * dir is the bit0 and rest of the bits come from resource
1653 cache_entry = ulp_mapper_cache_entry_get(parms->ulp_ctx,
1654 (tbl->resource_sub_type << 1 |
1655 (tbl->direction & 0x1)),
1659 * Get the identifier list for processing by both the hit and miss
1662 idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
1664 if (!cache_entry->ref_count) {
1665 /* Initialize the cache entry */
1666 cache_entry->tcam_idx = 0;
1667 cache_entry->ref_count = 0;
1668 for (i = 0; i < BNXT_ULP_CACHE_TBL_IDENT_MAX_NUM; i++)
1669 cache_entry->idents[i] = ULP_IDENTS_INVALID;
1671 /* Need to allocate identifiers for storing in the cache. */
1672 for (i = 0; i < num_idents; i++) {
1674 * Since we are using the cache, the identifier does not
1675 * get added to the flow db. Pass in the pointer to the
1678 rc = ulp_mapper_ident_process(parms, tbl,
1679 &idents[i], &tmp_ident);
1683 cache_entry->ident_types[i] = idents[i].ident_type;
1684 cache_entry->idents[i] = tmp_ident;
1687 /* Tell the TCAM processor to alloc an entry */
1688 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_ALLOC;
1689 /* Store the cache key for use by the tcam process code */
1690 parms->cache_ptr = cache_entry;
1692 /* Cache hit, get values from result. */
1693 for (i = 0; i < num_idents; i++) {
1694 regval = (uint64_t)cache_entry->idents[i];
1695 if (!ulp_regfile_write(parms->regfile,
1696 idents[i].regfile_idx,
1697 tfp_cpu_to_be_64(regval))) {
1699 "Failed to write to regfile\n");
1704 * The cached entry is being used, so let the tcam processing
1705 * know not to process this table.
1707 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_SKIP;
1710 /* Made through the cache processing, increment the reference count. */
1711 cache_entry->ref_count++;
1713 /* Link the cache to the flow db. */
1714 memset(&fid_parms, 0, sizeof(fid_parms));
1715 fid_parms.direction = tbl->direction;
1716 fid_parms.resource_func = tbl->resource_func;
1719 * Cache resource type is composed of table_type, resource
1720 * sub type and direction, it needs to set appropriately via setter.
1722 ulp_mapper_cache_res_type_set(&fid_parms,
1724 (tbl->resource_sub_type << 1 |
1725 (tbl->direction & 0x1)));
1726 fid_parms.resource_hndl = (uint64_t)*ckey;
1727 fid_parms.critical_resource = tbl->critical_resource;
1728 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1733 BNXT_TF_DBG(ERR, "Failed to add cache to flow db.\n");
1738 * This error handling only gets called when the idents are being
1739 * allocated for the cache on misses. Using the num_idents that was
1742 for (i = 0; i < num_idents; i++) {
1743 if (cache_entry->idents[i] == ULP_IDENTS_INVALID)
1746 fparms.dir = tbl->direction;
1747 fparms.ident_type = idents[i].ident_type;
1748 fparms.id = cache_entry->idents[i];
1749 tf_free_identifier(parms->tfp, &fparms);
1756 ulp_mapper_glb_resource_info_init(struct tf *tfp,
1757 struct bnxt_ulp_mapper_data *mapper_data)
1759 struct bnxt_ulp_glb_resource_info *glb_res;
1760 uint32_t num_glb_res_ids, idx;
1763 glb_res = ulp_mapper_glb_resource_info_list_get(&num_glb_res_ids);
1764 if (!glb_res || !num_glb_res_ids) {
1765 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1769 /* Iterate the global resources and process each one */
1770 for (idx = 0; idx < num_glb_res_ids; idx++) {
1771 switch (glb_res[idx].resource_func) {
1772 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
1773 rc = ulp_mapper_resource_ident_allocate(tfp,
1778 BNXT_TF_DBG(ERR, "Global resource %x not supported\n",
1779 glb_res[idx].resource_func);
1787 * Function to process the action template. Iterate through the list
1788 * action info templates and process it.
1791 ulp_mapper_action_tbls_process(struct bnxt_ulp_mapper_parms *parms)
1795 struct bnxt_ulp_mapper_tbl_info *tbl;
1797 if (!parms->atbls || !parms->num_atbls) {
1798 BNXT_TF_DBG(ERR, "No action tables for template[%d][%d].\n",
1799 parms->dev_id, parms->act_tid);
1803 for (i = 0; i < parms->num_atbls; i++) {
1804 tbl = &parms->atbls[i];
1805 switch (tbl->resource_func) {
1806 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
1807 rc = ulp_mapper_index_tbl_process(parms, tbl, false);
1810 BNXT_TF_DBG(ERR, "Unexpected action resource %d\n",
1811 tbl->resource_func);
1816 BNXT_TF_DBG(ERR, "Resource type %d failed\n",
1817 tbl->resource_func);
1824 /* Create the classifier table entries for a flow. */
1826 ulp_mapper_class_tbls_process(struct bnxt_ulp_mapper_parms *parms)
1834 if (!parms->ctbls || !parms->num_ctbls) {
1835 BNXT_TF_DBG(ERR, "No class tables for template[%d][%d].\n",
1836 parms->dev_id, parms->class_tid);
1840 for (i = 0; i < parms->num_ctbls; i++) {
1841 struct bnxt_ulp_mapper_tbl_info *tbl = &parms->ctbls[i];
1843 switch (tbl->resource_func) {
1844 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
1845 rc = ulp_mapper_tcam_tbl_process(parms, tbl);
1847 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
1848 rc = ulp_mapper_em_tbl_process(parms, tbl);
1850 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
1851 rc = ulp_mapper_index_tbl_process(parms, tbl, true);
1853 case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
1854 rc = ulp_mapper_cache_tbl_process(parms, tbl);
1857 BNXT_TF_DBG(ERR, "Unexpected class resource %d\n",
1858 tbl->resource_func);
1863 BNXT_TF_DBG(ERR, "Resource type %d failed\n",
1864 tbl->resource_func);
1873 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
1874 struct ulp_flow_db_res_params *res)
1880 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
1884 tfp = bnxt_ulp_cntxt_tfp_get(ulp);
1886 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
1890 switch (res->resource_func) {
1891 case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
1892 rc = ulp_mapper_cache_entry_free(ulp, tfp, res);
1894 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
1895 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
1897 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
1898 rc = ulp_mapper_eem_entry_free(ulp, tfp, res);
1900 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
1901 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
1903 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
1904 rc = ulp_mapper_ident_free(ulp, tfp, res);
1906 case BNXT_ULP_RESOURCE_FUNC_HW_FID:
1907 rc = ulp_mapper_mark_free(ulp, res);
1917 ulp_mapper_resources_free(struct bnxt_ulp_context *ulp_ctx,
1919 enum bnxt_ulp_flow_db_tables tbl_type)
1921 struct ulp_flow_db_res_params res_parms = { 0 };
1925 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
1930 * Set the critical resource on the first resource del, then iterate
1931 * while status is good
1933 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_YES;
1934 rc = ulp_flow_db_resource_del(ulp_ctx, tbl_type, fid, &res_parms);
1938 * This is unexpected on the first call to resource del.
1939 * It likely means that the flow did not exist in the flow db.
1941 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
1947 trc = ulp_mapper_resource_free(ulp_ctx, &res_parms);
1950 * On fail, we still need to attempt to free the
1951 * remaining resources. Don't return
1954 "Flow[%d][0x%x] Res[%d][0x%016" PRIx64
1955 "] failed rc=%d.\n",
1956 tbl_type, fid, res_parms.resource_func,
1957 res_parms.resource_hndl, trc);
1959 /* All subsequent call require the non-critical_resource */
1960 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1962 rc = ulp_flow_db_resource_del(ulp_ctx,
1968 /* Free the Flow ID since we've removed all resources */
1969 rc = ulp_flow_db_fid_free(ulp_ctx, tbl_type, fid);
1975 ulp_mapper_glb_resource_info_deinit(struct bnxt_ulp_context *ulp_ctx,
1976 struct bnxt_ulp_mapper_data *mapper_data)
1978 struct bnxt_ulp_mapper_glb_resource_entry *ent;
1979 struct ulp_flow_db_res_params res;
1982 /* Iterate the global resources and process each one */
1983 for (dir = TF_DIR_RX; dir < TF_DIR_MAX; dir++) {
1984 for (idx = 0; idx < BNXT_ULP_GLB_RESOURCE_INFO_TBL_MAX_SZ;
1986 ent = &mapper_data->glb_res_tbl[dir][idx];
1987 if (ent->resource_func ==
1988 BNXT_ULP_RESOURCE_FUNC_INVALID)
1990 memset(&res, 0, sizeof(struct ulp_flow_db_res_params));
1991 res.resource_func = ent->resource_func;
1992 res.direction = dir;
1993 res.resource_type = ent->resource_type;
1994 res.resource_hndl = ent->resource_hndl;
1995 ulp_mapper_resource_free(ulp_ctx, &res);
2001 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx, uint32_t fid)
2004 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
2008 return ulp_mapper_resources_free(ulp_ctx,
2010 BNXT_ULP_REGULAR_FLOW_TABLE);
2013 /* Function to handle the mapping of the Flow to be compatible
2014 * with the underlying hardware.
2017 ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
2018 struct bnxt_ulp_mapper_create_parms *cparms,
2021 struct bnxt_ulp_device_params *device_params;
2022 struct bnxt_ulp_mapper_parms parms;
2023 struct ulp_regfile regfile;
2026 if (!ulp_ctx || !cparms)
2029 /* Initialize the parms structure */
2030 memset(&parms, 0, sizeof(parms));
2031 parms.act_prop = cparms->act_prop;
2032 parms.act_bitmap = cparms->act;
2033 parms.regfile = ®file;
2034 parms.hdr_field = cparms->hdr_field;
2035 parms.comp_fld = cparms->comp_fld;
2036 parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2037 parms.ulp_ctx = ulp_ctx;
2038 parms.tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
2040 /* Get the device id from the ulp context */
2041 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &parms.dev_id)) {
2042 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
2047 * Get the mapper data for dynamic mapper data such as default
2050 parms.mapper_data = (struct bnxt_ulp_mapper_data *)
2051 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2052 if (!parms.mapper_data) {
2053 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
2057 /* Get the action table entry from device id and act context id */
2058 parms.act_tid = cparms->act_tid;
2059 parms.atbls = ulp_mapper_action_tbl_list_get(parms.dev_id,
2062 if (!parms.atbls || !parms.num_atbls) {
2063 BNXT_TF_DBG(ERR, "No action tables for %d:%d\n",
2064 parms.dev_id, parms.act_tid);
2068 /* Get the class table entry from device id and act context id */
2069 parms.class_tid = cparms->class_tid;
2070 parms.ctbls = ulp_mapper_class_tbl_list_get(parms.dev_id,
2073 if (!parms.ctbls || !parms.num_ctbls) {
2074 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2075 parms.dev_id, parms.class_tid);
2079 /* Get the byte order for the further processing from device params */
2080 device_params = bnxt_ulp_device_params_get(parms.dev_id);
2081 if (!device_params) {
2082 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2083 parms.dev_id, parms.class_tid);
2086 parms.order = device_params->byte_order;
2087 parms.encap_byte_swap = device_params->encap_byte_swap;
2089 /* initialize the registry file for further processing */
2090 if (!ulp_regfile_init(parms.regfile)) {
2091 BNXT_TF_DBG(ERR, "regfile initialization failed.\n");
2095 rc = ulp_regfile_write(parms.regfile,
2096 BNXT_ULP_REGFILE_INDEX_CLASS_TID,
2097 tfp_cpu_to_be_64((uint64_t)parms.class_tid));
2099 BNXT_TF_DBG(ERR, "Unable to write template ID to regfile\n");
2103 /* Allocate a Flow ID for attaching all resources for the flow to.
2104 * Once allocated, all errors have to walk the list of resources and
2105 * free each of them.
2107 rc = ulp_flow_db_fid_alloc(ulp_ctx,
2108 BNXT_ULP_REGULAR_FLOW_TABLE,
2112 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
2116 /* Process the action template list from the selected action table*/
2117 rc = ulp_mapper_action_tbls_process(&parms);
2119 BNXT_TF_DBG(ERR, "action tables failed creation for %d:%d\n",
2120 parms.dev_id, parms.act_tid);
2124 /* All good. Now process the class template */
2125 rc = ulp_mapper_class_tbls_process(&parms);
2127 BNXT_TF_DBG(ERR, "class tables failed creation for %d:%d\n",
2128 parms.dev_id, parms.class_tid);
2132 *flowid = parms.fid;
2137 /* Free all resources that were allocated during flow creation */
2138 trc = ulp_mapper_flow_destroy(ulp_ctx, parms.fid);
2140 BNXT_TF_DBG(ERR, "Failed to free all resources rc=%d\n", trc);
2146 ulp_mapper_init(struct bnxt_ulp_context *ulp_ctx)
2148 struct bnxt_ulp_cache_tbl_params *tbl;
2149 struct bnxt_ulp_mapper_data *data;
2157 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2161 data = rte_zmalloc("ulp_mapper_data",
2162 sizeof(struct bnxt_ulp_mapper_data), 0);
2164 BNXT_TF_DBG(ERR, "Failed to allocate the mapper data\n");
2168 if (bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, data)) {
2169 BNXT_TF_DBG(ERR, "Failed to set mapper data in context\n");
2170 /* Don't call deinit since the prof_func wasn't allocated. */
2175 /* Allocate the global resource ids */
2176 rc = ulp_mapper_glb_resource_info_init(tfp, data);
2178 BNXT_TF_DBG(ERR, "Failed to initialize global resource ids\n");
2182 /* Allocate the ulp cache tables. */
2183 for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2184 tbl = ulp_mapper_cache_tbl_params_get(i);
2186 BNXT_TF_DBG(ERR, "Failed to get cache table parms (%d)",
2190 if (tbl->num_entries != 0) {
2191 csize = sizeof(struct bnxt_ulp_mapper_cache_entry) *
2193 data->cache_tbl[i] = rte_zmalloc("ulp mapper cache tbl",
2195 if (!data->cache_tbl[i]) {
2196 BNXT_TF_DBG(ERR, "Failed to allocate Cache "
2206 /* Ignore the return code in favor of returning the original error. */
2207 ulp_mapper_deinit(ulp_ctx);
2212 ulp_mapper_deinit(struct bnxt_ulp_context *ulp_ctx)
2214 struct bnxt_ulp_mapper_data *data;
2220 "Failed to acquire ulp context, so data may "
2221 "not be released.\n");
2225 data = (struct bnxt_ulp_mapper_data *)
2226 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2228 /* Go ahead and return since there is no allocated data. */
2229 BNXT_TF_DBG(ERR, "No data appears to have been allocated.\n");
2233 tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2235 BNXT_TF_DBG(ERR, "Failed to acquire tfp.\n");
2236 /* Free the mapper data regardless of errors. */
2237 goto free_mapper_data;
2240 /* Free the global resource info table entries */
2241 ulp_mapper_glb_resource_info_deinit(ulp_ctx, data);
2244 /* Free the ulp cache tables */
2245 for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2246 rte_free(data->cache_tbl[i]);
2247 data->cache_tbl[i] = NULL;
2251 /* Reset the data pointer within the ulp_ctx. */
2252 bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, NULL);