1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2020 Broadcom
8 #include "ulp_template_db.h"
9 #include "ulp_template_struct.h"
10 #include "bnxt_tf_common.h"
11 #include "ulp_utils.h"
14 #include "tf_ext_flow_handle.h"
15 #include "ulp_mark_mgr.h"
16 #include "ulp_flow_db.h"
17 #include "ulp_mapper.h"
20 * Get the size of the action property for a given index.
22 * idx [in] The index for the action property
24 * returns the size of the action property.
27 ulp_mapper_act_prop_size_get(uint32_t idx)
29 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST)
31 return ulp_act_prop_map_table[idx];
35 * Get the list of result fields that implement the flow action.
36 * Gets a device dependent list of tables that implement the action template id.
38 * dev_id [in] The device id of the forwarding element
40 * tid [in] The action template id that matches the flow
42 * num_tbls [out] The number of action tables in the returned array
44 * Returns An array of action tables to implement the flow, or NULL on error.
46 static struct bnxt_ulp_mapper_act_tbl_info *
47 ulp_mapper_action_tbl_list_get(uint32_t dev_id,
55 BNXT_TF_DBG(ERR, "Invalid arguments\n");
59 /* template shift and device mask */
60 tidx = ULP_DEVICE_PARAMS_INDEX(tid, dev_id);
62 /* NOTE: Need to have something from template compiler to help validate
63 * range of dev_id and act_tid
65 idx = ulp_act_tmpl_list[tidx].start_tbl_idx;
66 *num_tbls = ulp_act_tmpl_list[tidx].num_tbls;
68 return &ulp_act_tbl_list[idx];
71 /** Get a list of classifier tables that implement the flow
72 * Gets a device dependent list of tables that implement the class template id
74 * dev_id [in] The device id of the forwarding element
76 * tid [in] The template id that matches the flow
78 * num_tbls [out] The number of classifier tables in the returned array
80 * returns An array of classifier tables to implement the flow, or NULL on
83 static struct bnxt_ulp_mapper_class_tbl_info *
84 ulp_mapper_class_tbl_list_get(uint32_t dev_id,
89 uint32_t tidx = ULP_DEVICE_PARAMS_INDEX(tid, dev_id);
94 /* NOTE: Need to have something from template compiler to help validate
95 * range of dev_id and tid
97 idx = ulp_class_tmpl_list[tidx].start_tbl_idx;
98 *num_tbls = ulp_class_tmpl_list[tidx].num_tbls;
100 return &ulp_class_tbl_list[idx];
104 * Get the list of key fields that implement the flow.
106 * ctxt [in] The ulp context
108 * tbl [in] A single table instance to get the key fields from
110 * num_flds [out] The number of key fields in the returned array
112 * Returns array of Key fields, or NULL on error.
114 static struct bnxt_ulp_mapper_class_key_field_info *
115 ulp_mapper_key_fields_get(struct bnxt_ulp_mapper_class_tbl_info *tbl,
120 if (!tbl || !num_flds)
123 idx = tbl->key_start_idx;
124 *num_flds = tbl->key_num_fields;
126 /* NOTE: Need template to provide range checking define */
127 return &ulp_class_key_field_list[idx];
131 * Get the list of data fields that implement the flow.
133 * ctxt [in] The ulp context
135 * tbl [in] A single table instance to get the data fields from
137 * num_flds [out] The number of data fields in the returned array.
139 * Returns array of data fields, or NULL on error.
141 static struct bnxt_ulp_mapper_result_field_info *
142 ulp_mapper_result_fields_get(struct bnxt_ulp_mapper_class_tbl_info *tbl,
147 if (!tbl || !num_flds)
150 idx = tbl->result_start_idx;
151 *num_flds = tbl->result_num_fields;
153 /* NOTE: Need template to provide range checking define */
154 return &ulp_class_result_field_list[idx];
158 * Get the list of result fields that implement the flow action.
160 * tbl [in] A single table instance to get the results fields
161 * from num_flds [out] The number of data fields in the returned
164 * Returns array of data fields, or NULL on error.
166 static struct bnxt_ulp_mapper_result_field_info *
167 ulp_mapper_act_result_fields_get(struct bnxt_ulp_mapper_act_tbl_info *tbl,
168 uint32_t *num_rslt_flds,
169 uint32_t *num_encap_flds)
173 if (!tbl || !num_rslt_flds || !num_encap_flds)
176 idx = tbl->result_start_idx;
177 *num_rslt_flds = tbl->result_num_fields;
178 *num_encap_flds = tbl->encap_num_fields;
180 /* NOTE: Need template to provide range checking define */
181 return &ulp_act_result_field_list[idx];
185 * Get the list of ident fields that implement the flow
187 * tbl [in] A single table instance to get the ident fields from
189 * num_flds [out] The number of ident fields in the returned array
191 * returns array of ident fields, or NULL on error
193 static struct bnxt_ulp_mapper_ident_info *
194 ulp_mapper_ident_fields_get(struct bnxt_ulp_mapper_class_tbl_info *tbl,
199 if (!tbl || !num_flds)
202 idx = tbl->ident_start_idx;
203 *num_flds = tbl->ident_nums;
205 /* NOTE: Need template to provide range checking define */
206 return &ulp_ident_list[idx];
209 static inline int32_t
210 ulp_mapper_tcam_entry_free(struct bnxt_ulp_context *ulp __rte_unused,
212 struct ulp_flow_db_res_params *res)
214 struct tf_free_tcam_entry_parms fparms = {
215 .dir = res->direction,
216 .tcam_tbl_type = res->resource_type,
217 .idx = (uint16_t)res->resource_hndl
220 return tf_free_tcam_entry(tfp, &fparms);
223 static inline int32_t
224 ulp_mapper_index_entry_free(struct bnxt_ulp_context *ulp __rte_unused,
226 struct ulp_flow_db_res_params *res)
228 struct tf_free_tbl_entry_parms fparms = {
229 .dir = res->direction,
230 .type = res->resource_type,
231 .idx = (uint32_t)res->resource_hndl
234 return tf_free_tbl_entry(tfp, &fparms);
237 static inline int32_t
238 ulp_mapper_eem_entry_free(struct bnxt_ulp_context *ulp,
240 struct ulp_flow_db_res_params *res)
242 struct tf_delete_em_entry_parms fparms = { 0 };
245 fparms.dir = res->direction;
246 fparms.mem = TF_MEM_EXTERNAL;
247 fparms.flow_handle = res->resource_hndl;
249 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
251 BNXT_TF_DBG(ERR, "Failed to get table scope\n");
255 return tf_delete_em_entry(tfp, &fparms);
258 static inline int32_t
259 ulp_mapper_ident_free(struct bnxt_ulp_context *ulp __rte_unused,
261 struct ulp_flow_db_res_params *res)
263 struct tf_free_identifier_parms fparms = {
264 .dir = res->direction,
265 .ident_type = res->resource_type,
266 .id = (uint16_t)res->resource_hndl
269 return tf_free_identifier(tfp, &fparms);
272 static inline int32_t
273 ulp_mapper_mark_free(struct bnxt_ulp_context *ulp,
274 struct ulp_flow_db_res_params *res)
280 fid = (uint32_t)res->resource_hndl;
281 TF_GET_FLAG_FROM_FLOW_ID(fid, flag);
282 TF_GET_GFID_FROM_FLOW_ID(fid, gfid);
284 return ulp_mark_db_mark_del(ulp,
285 (flag == TF_GFID_TABLE_EXTERNAL),
291 ulp_mapper_ident_process(struct bnxt_ulp_mapper_parms *parms,
292 struct bnxt_ulp_mapper_class_tbl_info *tbl,
293 struct bnxt_ulp_mapper_ident_info *ident)
295 struct ulp_flow_db_res_params fid_parms;
298 struct tf_alloc_identifier_parms iparms = { 0 };
299 struct tf_free_identifier_parms free_parms = { 0 };
303 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
305 BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
309 idx = ident->regfile_wr_idx;
311 iparms.ident_type = ident->ident_type;
312 iparms.dir = tbl->direction;
314 rc = tf_alloc_identifier(tfp, &iparms);
316 BNXT_TF_DBG(ERR, "Alloc ident %s:%d failed.\n",
317 (iparms.dir == TF_DIR_RX) ? "RX" : "TX",
322 id = (uint64_t)tfp_cpu_to_be_64(iparms.id);
323 if (!ulp_regfile_write(parms->regfile, idx, id)) {
324 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
326 /* Need to free the identifier, so goto error */
330 /* Link the resource to the flow in the flow db */
331 memset(&fid_parms, 0, sizeof(fid_parms));
332 fid_parms.direction = tbl->direction;
333 fid_parms.resource_func = ident->resource_func;
334 fid_parms.resource_type = ident->ident_type;
335 fid_parms.resource_hndl = iparms.id;
336 fid_parms.critical_resource = 0;
338 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
343 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
345 /* Need to free the identifier, so goto error */
352 /* Need to free the identifier */
353 free_parms.dir = tbl->direction;
354 free_parms.ident_type = ident->ident_type;
355 free_parms.id = iparms.id;
357 (void)tf_free_identifier(tfp, &free_parms);
359 BNXT_TF_DBG(ERR, "Ident process failed for %s:%s\n",
361 (tbl->direction == TF_DIR_RX) ? "RX" : "TX");
366 ulp_mapper_result_field_process(struct bnxt_ulp_mapper_parms *parms,
367 struct bnxt_ulp_mapper_result_field_info *fld,
368 struct ulp_blob *blob)
370 uint16_t idx, size_idx;
373 uint32_t val_size = 0, field_size = 0;
375 switch (fld->result_opcode) {
376 case BNXT_ULP_RESULT_OPC_SET_TO_CONSTANT:
377 val = fld->result_operand;
378 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
379 BNXT_TF_DBG(ERR, "Failed to add field\n");
383 case BNXT_ULP_RESULT_OPC_SET_TO_ACT_PROP:
384 if (!ulp_operand_read(fld->result_operand,
385 (uint8_t *)&idx, sizeof(uint16_t))) {
386 BNXT_TF_DBG(ERR, "operand read failed\n");
389 idx = tfp_be_to_cpu_16(idx);
391 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
392 BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", idx);
395 val = &parms->act_prop->act_details[idx];
396 field_size = ulp_mapper_act_prop_size_get(idx);
397 if (fld->field_bit_size < ULP_BYTE_2_BITS(field_size)) {
398 field_size = field_size -
399 ((fld->field_bit_size + 7) / 8);
402 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
403 BNXT_TF_DBG(ERR, "push field failed\n");
407 case BNXT_ULP_RESULT_OPC_SET_TO_ACT_PROP_SZ:
408 if (!ulp_operand_read(fld->result_operand,
409 (uint8_t *)&idx, sizeof(uint16_t))) {
410 BNXT_TF_DBG(ERR, "operand read failed\n");
413 idx = tfp_be_to_cpu_16(idx);
415 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
416 BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", idx);
419 val = &parms->act_prop->act_details[idx];
421 /* get the size index next */
422 if (!ulp_operand_read(&fld->result_operand[sizeof(uint16_t)],
423 (uint8_t *)&size_idx, sizeof(uint16_t))) {
424 BNXT_TF_DBG(ERR, "operand read failed\n");
427 size_idx = tfp_be_to_cpu_16(size_idx);
429 if (size_idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
430 BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", size_idx);
433 memcpy(&val_size, &parms->act_prop->act_details[size_idx],
435 val_size = tfp_be_to_cpu_32(val_size);
436 val_size = ULP_BYTE_2_BITS(val_size);
437 ulp_blob_push_encap(blob, val, val_size);
439 case BNXT_ULP_RESULT_OPC_SET_TO_REGFILE:
440 if (!ulp_operand_read(fld->result_operand,
441 (uint8_t *)&idx, sizeof(uint16_t))) {
442 BNXT_TF_DBG(ERR, "operand read failed\n");
446 idx = tfp_be_to_cpu_16(idx);
447 /* Uninitialized regfile entries return 0 */
448 if (!ulp_regfile_read(parms->regfile, idx, ®val)) {
449 BNXT_TF_DBG(ERR, "regfile[%d] read oob\n", idx);
453 val = ulp_blob_push_64(blob, ®val, fld->field_bit_size);
455 BNXT_TF_DBG(ERR, "push field failed\n");
466 /* Function to alloc action record and set the table. */
468 ulp_mapper_keymask_field_process(struct bnxt_ulp_mapper_parms *parms,
469 struct bnxt_ulp_mapper_class_key_field_info *f,
470 struct ulp_blob *blob,
474 uint16_t idx, bitlen;
477 struct ulp_regfile *regfile = parms->regfile;
479 struct bnxt_ulp_mapper_class_key_field_info *fld = f;
483 operand = fld->spec_operand;
484 opcode = fld->spec_opcode;
486 operand = fld->mask_operand;
487 opcode = fld->mask_opcode;
490 bitlen = fld->field_bit_size;
493 case BNXT_ULP_SPEC_OPC_SET_TO_CONSTANT:
495 if (!ulp_blob_push(blob, val, bitlen)) {
496 BNXT_TF_DBG(ERR, "push to key blob failed\n");
500 case BNXT_ULP_SPEC_OPC_ADD_PAD:
501 if (!ulp_blob_pad_push(blob, bitlen)) {
502 BNXT_TF_DBG(ERR, "Pad too large for blob\n");
507 case BNXT_ULP_SPEC_OPC_SET_TO_HDR_FIELD:
508 if (!ulp_operand_read(operand, (uint8_t *)&idx,
510 BNXT_TF_DBG(ERR, "key operand read failed.\n");
513 idx = tfp_be_to_cpu_16(idx);
515 val = parms->hdr_field[idx].spec;
517 val = parms->hdr_field[idx].mask;
520 * Need to account for how much data was pushed to the header
521 * field vs how much is to be inserted in the key/mask.
523 field_size = parms->hdr_field[idx].size;
524 if (bitlen < ULP_BYTE_2_BITS(field_size)) {
525 field_size = field_size - ((bitlen + 7) / 8);
529 if (!ulp_blob_push(blob, val, bitlen)) {
530 BNXT_TF_DBG(ERR, "push to key blob failed\n");
534 case BNXT_ULP_SPEC_OPC_SET_TO_REGFILE:
535 if (!ulp_operand_read(operand, (uint8_t *)&idx,
537 BNXT_TF_DBG(ERR, "key operand read failed.\n");
540 idx = tfp_be_to_cpu_16(idx);
542 if (!ulp_regfile_read(regfile, idx, ®val)) {
543 BNXT_TF_DBG(ERR, "regfile[%d] read failed.\n",
548 val = ulp_blob_push_64(blob, ®val, bitlen);
550 BNXT_TF_DBG(ERR, "push to key blob failed\n");
560 /* Function to alloc action record and set the table. */
562 ulp_mapper_action_alloc_and_set(struct bnxt_ulp_mapper_parms *parms,
563 struct ulp_blob *blob)
565 struct ulp_flow_db_res_params fid_parms;
566 struct tf_alloc_tbl_entry_parms alloc_parms = { 0 };
567 struct tf_free_tbl_entry_parms free_parms = { 0 };
568 struct bnxt_ulp_mapper_act_tbl_info *atbls = parms->atbls;
573 /* Set the allocation parameters for the table*/
574 alloc_parms.dir = atbls->direction;
575 alloc_parms.type = atbls->table_type;
576 alloc_parms.search_enable = atbls->srch_b4_alloc;
577 alloc_parms.result = ulp_blob_data_get(blob,
578 &alloc_parms.result_sz_in_bytes);
579 if (!alloc_parms.result) {
580 BNXT_TF_DBG(ERR, "blob is not populated\n");
584 rc = tf_alloc_tbl_entry(parms->tfp, &alloc_parms);
586 BNXT_TF_DBG(ERR, "table type= [%d] dir = [%s] alloc failed\n",
588 (alloc_parms.dir == TF_DIR_RX) ? "RX" : "TX");
592 /* Need to calculate the idx for the result record */
594 * TBD: Need to get the stride from tflib instead of having to
595 * understand the construction of the pointer
597 uint64_t tmpidx = alloc_parms.idx;
599 if (atbls->table_type == TF_TBL_TYPE_EXT)
600 tmpidx = (alloc_parms.idx * TF_ACTION_RECORD_SZ) >> 4;
602 tmpidx = alloc_parms.idx;
604 idx = tfp_cpu_to_be_64(tmpidx);
606 /* Store the allocated index for future use in the regfile */
607 rc = ulp_regfile_write(parms->regfile, atbls->regfile_wr_idx, idx);
609 BNXT_TF_DBG(ERR, "regfile[%d] write failed\n",
610 atbls->regfile_wr_idx);
616 * The set_tbl_entry API if search is not enabled or searched entry
619 if (!atbls->srch_b4_alloc || !alloc_parms.hit) {
620 struct tf_set_tbl_entry_parms set_parm = { 0 };
623 set_parm.dir = atbls->direction;
624 set_parm.type = atbls->table_type;
625 set_parm.idx = alloc_parms.idx;
626 set_parm.data = ulp_blob_data_get(blob, &length);
627 set_parm.data_sz_in_bytes = length / 8;
629 if (set_parm.type == TF_TBL_TYPE_EXT)
630 bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
631 &set_parm.tbl_scope_id);
633 set_parm.tbl_scope_id = 0;
635 /* set the table entry */
636 rc = tf_set_tbl_entry(parms->tfp, &set_parm);
638 BNXT_TF_DBG(ERR, "table[%d][%s][%d] set failed\n",
640 (set_parm.dir == TF_DIR_RX) ? "RX" : "TX",
646 /* Link the resource to the flow in the flow db */
647 memset(&fid_parms, 0, sizeof(fid_parms));
648 fid_parms.direction = atbls->direction;
649 fid_parms.resource_func = atbls->resource_func;
650 fid_parms.resource_type = atbls->table_type;
651 fid_parms.resource_hndl = alloc_parms.idx;
652 fid_parms.critical_resource = 0;
654 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
659 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
668 free_parms.dir = alloc_parms.dir;
669 free_parms.type = alloc_parms.type;
670 free_parms.idx = alloc_parms.idx;
672 trc = tf_free_tbl_entry(parms->tfp, &free_parms);
674 BNXT_TF_DBG(ERR, "Failed to free table entry on failure\n");
680 * Function to process the action Info. Iterate through the list
681 * action info templates and process it.
684 ulp_mapper_action_info_process(struct bnxt_ulp_mapper_parms *parms,
685 struct bnxt_ulp_mapper_act_tbl_info *tbl)
687 struct ulp_blob blob;
688 struct bnxt_ulp_mapper_result_field_info *flds, *fld;
689 uint32_t num_flds = 0;
690 uint32_t encap_flds = 0;
695 if (!tbl || !parms->act_prop || !parms->act_bitmap || !parms->regfile)
698 /* use the max size if encap is enabled */
699 if (tbl->encap_num_fields)
700 bit_size = BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS;
702 bit_size = tbl->result_bit_size;
703 if (!ulp_blob_init(&blob, bit_size, parms->order)) {
704 BNXT_TF_DBG(ERR, "action blob init failed\n");
708 flds = ulp_mapper_act_result_fields_get(tbl, &num_flds, &encap_flds);
709 if (!flds || !num_flds) {
710 BNXT_TF_DBG(ERR, "Template undefined for action\n");
714 for (i = 0; i < (num_flds + encap_flds); i++) {
716 rc = ulp_mapper_result_field_process(parms,
720 BNXT_TF_DBG(ERR, "Action field failed\n");
723 /* set the swap index if 64 bit swap is enabled */
724 if (parms->encap_byte_swap && encap_flds) {
725 if ((i + 1) == num_flds)
726 ulp_blob_encap_swap_idx_set(&blob);
727 /* if 64 bit swap is enabled perform the 64bit swap */
728 if ((i + 1) == (num_flds + encap_flds))
729 ulp_blob_perform_encap_swap(&blob);
733 rc = ulp_mapper_action_alloc_and_set(parms, &blob);
738 ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
739 struct bnxt_ulp_mapper_class_tbl_info *tbl)
741 struct bnxt_ulp_mapper_class_key_field_info *kflds;
742 struct ulp_blob key, mask, data;
743 uint32_t i, num_kflds;
746 struct tf_alloc_tcam_entry_parms aparms = { 0 };
747 struct tf_set_tcam_entry_parms sparms = { 0 };
748 struct ulp_flow_db_res_params fid_parms = { 0 };
749 struct tf_free_tcam_entry_parms free_parms = { 0 };
753 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
755 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
759 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
760 if (!kflds || !num_kflds) {
761 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
765 if (!ulp_blob_init(&key, tbl->key_bit_size, parms->order) ||
766 !ulp_blob_init(&mask, tbl->key_bit_size, parms->order) ||
767 !ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
768 BNXT_TF_DBG(ERR, "blob inits failed.\n");
772 /* create the key/mask */
774 * NOTE: The WC table will require some kind of flag to handle the
775 * mode bits within the key/mask
777 for (i = 0; i < num_kflds; i++) {
779 rc = ulp_mapper_keymask_field_process(parms, &kflds[i],
782 BNXT_TF_DBG(ERR, "Key field set failed.\n");
787 rc = ulp_mapper_keymask_field_process(parms, &kflds[i],
790 BNXT_TF_DBG(ERR, "Mask field set failed.\n");
795 aparms.dir = tbl->direction;
796 aparms.tcam_tbl_type = tbl->table_type;
797 aparms.search_enable = tbl->srch_b4_alloc;
798 aparms.key_sz_in_bits = tbl->key_bit_size;
799 aparms.key = ulp_blob_data_get(&key, &tmplen);
800 if (tbl->key_bit_size != tmplen) {
801 BNXT_TF_DBG(ERR, "Key len (%d) != Expected (%d)\n",
802 tmplen, tbl->key_bit_size);
806 aparms.mask = ulp_blob_data_get(&mask, &tmplen);
807 if (tbl->key_bit_size != tmplen) {
808 BNXT_TF_DBG(ERR, "Mask len (%d) != Expected (%d)\n",
809 tmplen, tbl->key_bit_size);
813 aparms.priority = tbl->priority;
816 * All failures after this succeeds require the entry to be freed.
817 * cannot return directly on failure, but needs to goto error
819 rc = tf_alloc_tcam_entry(tfp, &aparms);
821 BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
827 /* Build the result */
828 if (!tbl->srch_b4_alloc || !hit) {
829 struct bnxt_ulp_mapper_result_field_info *dflds;
830 struct bnxt_ulp_mapper_ident_info *idents;
831 uint32_t num_dflds, num_idents;
833 /* Alloc identifiers */
834 idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
836 for (i = 0; i < num_idents; i++) {
837 rc = ulp_mapper_ident_process(parms, tbl, &idents[i]);
839 /* Already logged the error, just return */
844 /* Create the result data blob */
845 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds);
846 if (!dflds || !num_dflds) {
847 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
852 for (i = 0; i < num_dflds; i++) {
853 rc = ulp_mapper_result_field_process(parms,
857 BNXT_TF_DBG(ERR, "Failed to set data fields\n");
862 sparms.dir = aparms.dir;
863 sparms.tcam_tbl_type = aparms.tcam_tbl_type;
864 sparms.idx = aparms.idx;
865 /* Already verified the key/mask lengths */
866 sparms.key = ulp_blob_data_get(&key, &tmplen);
867 sparms.mask = ulp_blob_data_get(&mask, &tmplen);
868 sparms.key_sz_in_bits = tbl->key_bit_size;
869 sparms.result = ulp_blob_data_get(&data, &tmplen);
871 if (tbl->result_bit_size != tmplen) {
872 BNXT_TF_DBG(ERR, "Result len (%d) != Expected (%d)\n",
873 tmplen, tbl->result_bit_size);
877 sparms.result_sz_in_bits = tbl->result_bit_size;
879 rc = tf_set_tcam_entry(tfp, &sparms);
881 BNXT_TF_DBG(ERR, "tcam[%d][%s][%d] write failed.\n",
882 sparms.tcam_tbl_type,
883 (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
888 BNXT_TF_DBG(ERR, "Not supporting search before alloc now\n");
893 /* Link the resource to the flow in the flow db */
894 fid_parms.direction = tbl->direction;
895 fid_parms.resource_func = tbl->resource_func;
896 fid_parms.resource_type = tbl->table_type;
897 fid_parms.critical_resource = tbl->critical_resource;
898 fid_parms.resource_hndl = aparms.idx;
900 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
905 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
907 /* Need to free the identifier, so goto error */
913 free_parms.dir = tbl->direction;
914 free_parms.tcam_tbl_type = tbl->table_type;
915 free_parms.idx = aparms.idx;
916 trc = tf_free_tcam_entry(tfp, &free_parms);
918 BNXT_TF_DBG(ERR, "Failed to free tcam[%d][%d][%d] on failure\n",
919 tbl->table_type, tbl->direction, aparms.idx);
925 ulp_mapper_em_tbl_process(struct bnxt_ulp_mapper_parms *parms,
926 struct bnxt_ulp_mapper_class_tbl_info *tbl)
928 struct bnxt_ulp_mapper_class_key_field_info *kflds;
929 struct bnxt_ulp_mapper_result_field_info *dflds;
930 struct ulp_blob key, data;
931 uint32_t i, num_kflds, num_dflds;
933 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
934 struct ulp_rte_act_prop *a_prop = parms->act_prop;
935 struct ulp_flow_db_res_params fid_parms = { 0 };
936 struct tf_insert_em_entry_parms iparms = { 0 };
937 struct tf_delete_em_entry_parms free_parms = { 0 };
941 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
942 if (!kflds || !num_kflds) {
943 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
947 /* Initialize the key/result blobs */
948 if (!ulp_blob_init(&key, tbl->blob_key_bit_size, parms->order) ||
949 !ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
950 BNXT_TF_DBG(ERR, "blob inits failed.\n");
955 for (i = 0; i < num_kflds; i++) {
957 rc = ulp_mapper_keymask_field_process(parms, &kflds[i],
960 BNXT_TF_DBG(ERR, "Key field set failed.\n");
966 * TBD: Normally should process identifiers in case of using recycle or
967 * loopback. Not supporting recycle for now.
970 /* Create the result data blob */
971 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds);
972 if (!dflds || !num_dflds) {
973 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
977 for (i = 0; i < num_dflds; i++) {
978 struct bnxt_ulp_mapper_result_field_info *fld;
982 rc = ulp_mapper_result_field_process(parms,
986 BNXT_TF_DBG(ERR, "Failed to set data fields.\n");
991 rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
992 &iparms.tbl_scope_id);
994 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
999 * NOTE: the actual blob size will differ from the size in the tbl
1000 * entry due to the padding.
1002 iparms.dup_check = 0;
1003 iparms.dir = tbl->direction;
1004 iparms.mem = tbl->mem;
1005 iparms.key = ulp_blob_data_get(&key, &tmplen);
1006 iparms.key_sz_in_bits = tbl->key_bit_size;
1007 iparms.em_record = ulp_blob_data_get(&data, &tmplen);
1008 iparms.em_record_sz_in_bits = tbl->result_bit_size;
1010 rc = tf_insert_em_entry(tfp, &iparms);
1012 BNXT_TF_DBG(ERR, "Failed to insert em entry rc=%d.\n", rc);
1016 if (tbl->mark_enable &&
1017 ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1018 BNXT_ULP_ACTION_BIT_MARK)) {
1019 uint32_t val, mark, gfid, flag;
1020 /* TBD: Need to determine if GFID is enabled globally */
1021 if (sizeof(val) != BNXT_ULP_ACT_PROP_SZ_MARK) {
1022 BNXT_TF_DBG(ERR, "Mark size (%d) != expected (%zu)\n",
1023 BNXT_ULP_ACT_PROP_SZ_MARK, sizeof(val));
1029 &a_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1032 mark = tfp_be_to_cpu_32(val);
1034 TF_GET_GFID_FROM_FLOW_ID(iparms.flow_id, gfid);
1035 TF_GET_FLAG_FROM_FLOW_ID(iparms.flow_id, flag);
1037 rc = ulp_mark_db_mark_add(parms->ulp_ctx,
1038 (flag == TF_GFID_TABLE_EXTERNAL),
1042 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1047 * Link the mark resource to the flow in the flow db
1048 * The mark is never the critical resource, so it is 0.
1050 memset(&fid_parms, 0, sizeof(fid_parms));
1051 fid_parms.direction = tbl->direction;
1052 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1053 fid_parms.resource_type = tbl->table_type;
1054 fid_parms.resource_hndl = iparms.flow_id;
1055 fid_parms.critical_resource = 0;
1057 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1062 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1064 /* Need to free the identifier, so goto error */
1069 /* Link the EM resource to the flow in the flow db */
1070 memset(&fid_parms, 0, sizeof(fid_parms));
1071 fid_parms.direction = tbl->direction;
1072 fid_parms.resource_func = tbl->resource_func;
1073 fid_parms.resource_type = tbl->table_type;
1074 fid_parms.critical_resource = tbl->critical_resource;
1075 fid_parms.resource_hndl = iparms.flow_handle;
1077 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1082 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1084 /* Need to free the identifier, so goto error */
1090 free_parms.dir = iparms.dir;
1091 free_parms.mem = iparms.mem;
1092 free_parms.tbl_scope_id = iparms.tbl_scope_id;
1093 free_parms.flow_handle = iparms.flow_handle;
1095 trc = tf_delete_em_entry(tfp, &free_parms);
1097 BNXT_TF_DBG(ERR, "Failed to delete EM entry on failed add\n");
1103 ulp_mapper_index_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1104 struct bnxt_ulp_mapper_class_tbl_info *tbl)
1106 struct bnxt_ulp_mapper_result_field_info *flds;
1107 struct ulp_flow_db_res_params fid_parms;
1108 struct ulp_blob data;
1111 uint32_t i, num_flds;
1112 int32_t rc = 0, trc = 0;
1113 struct tf_alloc_tbl_entry_parms aparms = { 0 };
1114 struct tf_set_tbl_entry_parms sparms = { 0 };
1115 struct tf_free_tbl_entry_parms free_parms = { 0 };
1117 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1119 if (!ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
1120 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
1124 flds = ulp_mapper_result_fields_get(tbl, &num_flds);
1125 if (!flds || !num_flds) {
1126 BNXT_TF_DBG(ERR, "Template undefined for action\n");
1130 for (i = 0; i < num_flds; i++) {
1131 rc = ulp_mapper_result_field_process(parms,
1135 BNXT_TF_DBG(ERR, "data field failed\n");
1140 aparms.dir = tbl->direction;
1141 aparms.type = tbl->table_type;
1142 aparms.search_enable = tbl->srch_b4_alloc;
1143 aparms.result = ulp_blob_data_get(&data, &tmplen);
1144 aparms.result_sz_in_bytes = ULP_SZ_BITS2BYTES(tbl->result_bit_size);
1146 /* All failures after the alloc succeeds require a free */
1147 rc = tf_alloc_tbl_entry(tfp, &aparms);
1149 BNXT_TF_DBG(ERR, "Alloc table[%d][%s] failed rc=%d\n",
1151 (tbl->direction == TF_DIR_RX) ? "RX" : "TX",
1156 /* Always storing values in Regfile in BE */
1157 idx = tfp_cpu_to_be_64(aparms.idx);
1158 rc = ulp_regfile_write(parms->regfile, tbl->regfile_wr_idx, idx);
1160 BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
1161 tbl->regfile_wr_idx);
1165 if (!tbl->srch_b4_alloc) {
1166 sparms.dir = tbl->direction;
1167 sparms.type = tbl->table_type;
1168 sparms.data = ulp_blob_data_get(&data, &tmplen);
1169 sparms.data_sz_in_bytes =
1170 ULP_SZ_BITS2BYTES(tbl->result_bit_size);
1171 sparms.idx = aparms.idx;
1173 rc = tf_set_tbl_entry(tfp, &sparms);
1175 BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
1177 (tbl->direction == TF_DIR_RX) ? "RX" : "TX",
1185 /* Link the resource to the flow in the flow db */
1186 memset(&fid_parms, 0, sizeof(fid_parms));
1187 fid_parms.direction = tbl->direction;
1188 fid_parms.resource_func = tbl->resource_func;
1189 fid_parms.resource_type = tbl->table_type;
1190 fid_parms.resource_hndl = aparms.idx;
1191 fid_parms.critical_resource = 0;
1193 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1198 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
1206 * Free the allocated resource since we failed to either
1207 * write to the entry or link the flow
1209 free_parms.dir = tbl->direction;
1210 free_parms.type = tbl->table_type;
1211 free_parms.idx = aparms.idx;
1213 trc = tf_free_tbl_entry(tfp, &free_parms);
1215 BNXT_TF_DBG(ERR, "Failed to free tbl entry on failure\n");
1221 * Function to process the action template. Iterate through the list
1222 * action info templates and process it.
1225 ulp_mapper_action_tbls_process(struct bnxt_ulp_mapper_parms *parms)
1230 if (!parms->atbls || !parms->num_atbls) {
1231 BNXT_TF_DBG(ERR, "No action tables for template[%d][%d].\n",
1232 parms->dev_id, parms->act_tid);
1236 for (i = 0; i < parms->num_atbls; i++) {
1237 rc = ulp_mapper_action_info_process(parms, &parms->atbls[i]);
1245 /* Create the classifier table entries for a flow. */
1247 ulp_mapper_class_tbls_process(struct bnxt_ulp_mapper_parms *parms)
1255 if (!parms->ctbls || !parms->num_ctbls) {
1256 BNXT_TF_DBG(ERR, "No class tables for template[%d][%d].\n",
1257 parms->dev_id, parms->class_tid);
1261 for (i = 0; i < parms->num_ctbls; i++) {
1262 struct bnxt_ulp_mapper_class_tbl_info *tbl = &parms->ctbls[i];
1264 switch (tbl->resource_func) {
1265 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
1266 rc = ulp_mapper_tcam_tbl_process(parms, tbl);
1268 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
1269 rc = ulp_mapper_em_tbl_process(parms, tbl);
1271 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
1272 rc = ulp_mapper_index_tbl_process(parms, tbl);
1275 BNXT_TF_DBG(ERR, "Unexpected class resource %d\n",
1276 tbl->resource_func);
1281 BNXT_TF_DBG(ERR, "Resource type %d failed\n",
1282 tbl->resource_func);
1291 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
1292 struct ulp_flow_db_res_params *res)
1298 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
1302 tfp = bnxt_ulp_cntxt_tfp_get(ulp);
1304 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
1308 switch (res->resource_func) {
1309 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
1310 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
1312 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
1313 rc = ulp_mapper_eem_entry_free(ulp, tfp, res);
1315 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
1316 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
1318 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
1319 rc = ulp_mapper_ident_free(ulp, tfp, res);
1321 case BNXT_ULP_RESOURCE_FUNC_HW_FID:
1322 rc = ulp_mapper_mark_free(ulp, res);
1332 ulp_mapper_resources_free(struct bnxt_ulp_context *ulp_ctx,
1334 enum bnxt_ulp_flow_db_tables tbl_type)
1336 struct ulp_flow_db_res_params res_parms = { 0 };
1340 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
1345 * Set the critical resource on the first resource del, then iterate
1346 * while status is good
1348 res_parms.critical_resource = 1;
1349 rc = ulp_flow_db_resource_del(ulp_ctx, tbl_type, fid, &res_parms);
1353 * This is unexpected on the first call to resource del.
1354 * It likely means that the flow did not exist in the flow db.
1356 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
1362 trc = ulp_mapper_resource_free(ulp_ctx, &res_parms);
1365 * On fail, we still need to attempt to free the
1366 * remaining resources. Don't return
1369 "Flow[%d][0x%x] Res[%d][0x%016" PRIx64
1370 "] failed rc=%d.\n",
1371 tbl_type, fid, res_parms.resource_func,
1372 res_parms.resource_hndl, trc);
1374 /* All subsequent call require the critical_resource be zero */
1375 res_parms.critical_resource = 0;
1377 rc = ulp_flow_db_resource_del(ulp_ctx,
1383 /* Free the Flow ID since we've removed all resources */
1384 rc = ulp_flow_db_fid_free(ulp_ctx, tbl_type, fid);
1390 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx, uint32_t fid)
1393 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
1397 return ulp_mapper_resources_free(ulp_ctx,
1399 BNXT_ULP_REGULAR_FLOW_TABLE);
1402 /* Function to handle the mapping of the Flow to be compatible
1403 * with the underlying hardware.
1406 ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
1407 uint32_t app_priority __rte_unused,
1408 struct ulp_rte_hdr_bitmap *hdr_bitmap __rte_unused,
1409 struct ulp_rte_hdr_field *hdr_field,
1410 struct ulp_rte_act_bitmap *act_bitmap,
1411 struct ulp_rte_act_prop *act_prop,
1416 struct ulp_regfile regfile;
1417 struct bnxt_ulp_mapper_parms parms;
1418 struct bnxt_ulp_device_params *device_params;
1421 /* Initialize the parms structure */
1422 memset(&parms, 0, sizeof(parms));
1423 parms.act_prop = act_prop;
1424 parms.act_bitmap = act_bitmap;
1425 parms.regfile = ®file;
1426 parms.hdr_field = hdr_field;
1427 parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
1428 parms.ulp_ctx = ulp_ctx;
1430 /* Get the device id from the ulp context */
1431 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &parms.dev_id)) {
1432 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
1436 /* Get the action table entry from device id and act context id */
1437 parms.act_tid = act_tid;
1438 parms.atbls = ulp_mapper_action_tbl_list_get(parms.dev_id,
1441 if (!parms.atbls || !parms.num_atbls) {
1442 BNXT_TF_DBG(ERR, "No action tables for %d:%d\n",
1443 parms.dev_id, parms.act_tid);
1447 /* Get the class table entry from device id and act context id */
1448 parms.class_tid = class_tid;
1449 parms.ctbls = ulp_mapper_class_tbl_list_get(parms.dev_id,
1452 if (!parms.ctbls || !parms.num_ctbls) {
1453 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
1454 parms.dev_id, parms.class_tid);
1458 /* Get the byte order for the further processing from device params */
1459 device_params = bnxt_ulp_device_params_get(parms.dev_id);
1460 if (!device_params) {
1461 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
1462 parms.dev_id, parms.class_tid);
1465 parms.order = device_params->byte_order;
1466 parms.encap_byte_swap = device_params->encap_byte_swap;
1468 /* initialize the registry file for further processing */
1469 if (!ulp_regfile_init(parms.regfile)) {
1470 BNXT_TF_DBG(ERR, "regfile initialization failed.\n");
1474 /* Allocate a Flow ID for attaching all resources for the flow to.
1475 * Once allocated, all errors have to walk the list of resources and
1476 * free each of them.
1478 rc = ulp_flow_db_fid_alloc(ulp_ctx,
1479 BNXT_ULP_REGULAR_FLOW_TABLE,
1482 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
1486 /* Process the action template list from the selected action table*/
1487 rc = ulp_mapper_action_tbls_process(&parms);
1489 BNXT_TF_DBG(ERR, "action tables failed creation for %d:%d\n",
1490 parms.dev_id, parms.act_tid);
1494 /* All good. Now process the class template */
1495 rc = ulp_mapper_class_tbls_process(&parms);
1497 BNXT_TF_DBG(ERR, "class tables failed creation for %d:%d\n",
1498 parms.dev_id, parms.class_tid);
1502 *flow_id = parms.fid;
1507 /* Free all resources that were allocated during flow creation */
1508 trc = ulp_mapper_flow_destroy(ulp_ctx, parms.fid);
1510 BNXT_TF_DBG(ERR, "Failed to free all resources rc=%d\n", trc);