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,
371 uint16_t idx, size_idx;
374 uint32_t val_size = 0, field_size = 0;
376 switch (fld->result_opcode) {
377 case BNXT_ULP_RESULT_OPC_SET_TO_CONSTANT:
378 val = fld->result_operand;
379 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
380 BNXT_TF_DBG(ERR, "%s failed to add field\n", name);
384 case BNXT_ULP_RESULT_OPC_SET_TO_ACT_PROP:
385 if (!ulp_operand_read(fld->result_operand,
386 (uint8_t *)&idx, sizeof(uint16_t))) {
387 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
390 idx = tfp_be_to_cpu_16(idx);
392 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
393 BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n", name, idx);
396 val = &parms->act_prop->act_details[idx];
397 field_size = ulp_mapper_act_prop_size_get(idx);
398 if (fld->field_bit_size < ULP_BYTE_2_BITS(field_size)) {
399 field_size = field_size -
400 ((fld->field_bit_size + 7) / 8);
403 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
404 BNXT_TF_DBG(ERR, "%s push field failed\n", name);
408 case BNXT_ULP_RESULT_OPC_SET_TO_ENCAP_ACT_PROP_SZ:
409 if (!ulp_operand_read(fld->result_operand,
410 (uint8_t *)&idx, sizeof(uint16_t))) {
411 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
414 idx = tfp_be_to_cpu_16(idx);
416 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
417 BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n", name, idx);
420 val = &parms->act_prop->act_details[idx];
422 /* get the size index next */
423 if (!ulp_operand_read(&fld->result_operand[sizeof(uint16_t)],
424 (uint8_t *)&size_idx, sizeof(uint16_t))) {
425 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
428 size_idx = tfp_be_to_cpu_16(size_idx);
430 if (size_idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
431 BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", size_idx);
434 memcpy(&val_size, &parms->act_prop->act_details[size_idx],
436 val_size = tfp_be_to_cpu_32(val_size);
437 val_size = ULP_BYTE_2_BITS(val_size);
438 ulp_blob_push_encap(blob, val, val_size);
440 case BNXT_ULP_RESULT_OPC_SET_TO_REGFILE:
441 if (!ulp_operand_read(fld->result_operand,
442 (uint8_t *)&idx, sizeof(uint16_t))) {
443 BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
447 idx = tfp_be_to_cpu_16(idx);
448 /* Uninitialized regfile entries return 0 */
449 if (!ulp_regfile_read(parms->regfile, idx, ®val)) {
450 BNXT_TF_DBG(ERR, "%s regfile[%d] read oob\n",
455 val = ulp_blob_push_64(blob, ®val, fld->field_bit_size);
457 BNXT_TF_DBG(ERR, "%s push field failed\n", name);
468 /* Function to alloc action record and set the table. */
470 ulp_mapper_keymask_field_process(struct bnxt_ulp_mapper_parms *parms,
471 struct bnxt_ulp_mapper_class_key_field_info *f,
472 struct ulp_blob *blob,
477 uint16_t idx, bitlen;
480 struct ulp_regfile *regfile = parms->regfile;
482 struct bnxt_ulp_mapper_class_key_field_info *fld = f;
486 operand = fld->spec_operand;
487 opcode = fld->spec_opcode;
489 operand = fld->mask_operand;
490 opcode = fld->mask_opcode;
493 bitlen = fld->field_bit_size;
496 case BNXT_ULP_SPEC_OPC_SET_TO_CONSTANT:
498 if (!ulp_blob_push(blob, val, bitlen)) {
499 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
503 case BNXT_ULP_SPEC_OPC_ADD_PAD:
504 if (!ulp_blob_pad_push(blob, bitlen)) {
505 BNXT_TF_DBG(ERR, "%s pad too large for blob\n", name);
510 case BNXT_ULP_SPEC_OPC_SET_TO_HDR_FIELD:
511 if (!ulp_operand_read(operand, (uint8_t *)&idx,
513 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
516 idx = tfp_be_to_cpu_16(idx);
518 val = parms->hdr_field[idx].spec;
520 val = parms->hdr_field[idx].mask;
523 * Need to account for how much data was pushed to the header
524 * field vs how much is to be inserted in the key/mask.
526 field_size = parms->hdr_field[idx].size;
527 if (bitlen < ULP_BYTE_2_BITS(field_size)) {
528 field_size = field_size - ((bitlen + 7) / 8);
532 if (!ulp_blob_push(blob, val, bitlen)) {
533 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
537 case BNXT_ULP_SPEC_OPC_SET_TO_REGFILE:
538 if (!ulp_operand_read(operand, (uint8_t *)&idx,
540 BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
543 idx = tfp_be_to_cpu_16(idx);
545 if (!ulp_regfile_read(regfile, idx, ®val)) {
546 BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
551 val = ulp_blob_push_64(blob, ®val, bitlen);
553 BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
563 /* Function to alloc action record and set the table. */
565 ulp_mapper_action_alloc_and_set(struct bnxt_ulp_mapper_parms *parms,
566 struct ulp_blob *blob)
568 struct ulp_flow_db_res_params fid_parms;
569 struct tf_alloc_tbl_entry_parms alloc_parms = { 0 };
570 struct tf_free_tbl_entry_parms free_parms = { 0 };
571 struct bnxt_ulp_mapper_act_tbl_info *atbls = parms->atbls;
576 /* Set the allocation parameters for the table*/
577 alloc_parms.dir = atbls->direction;
578 alloc_parms.type = atbls->table_type;
579 alloc_parms.search_enable = atbls->srch_b4_alloc;
580 alloc_parms.result = ulp_blob_data_get(blob,
581 &alloc_parms.result_sz_in_bytes);
582 if (!alloc_parms.result) {
583 BNXT_TF_DBG(ERR, "blob is not populated\n");
587 rc = tf_alloc_tbl_entry(parms->tfp, &alloc_parms);
589 BNXT_TF_DBG(ERR, "table type= [%d] dir = [%s] alloc failed\n",
591 (alloc_parms.dir == TF_DIR_RX) ? "RX" : "TX");
595 /* Need to calculate the idx for the result record */
597 * TBD: Need to get the stride from tflib instead of having to
598 * understand the construction of the pointer
600 uint64_t tmpidx = alloc_parms.idx;
602 if (atbls->table_type == TF_TBL_TYPE_EXT)
603 tmpidx = (alloc_parms.idx * TF_ACTION_RECORD_SZ) >> 4;
605 tmpidx = alloc_parms.idx;
607 idx = tfp_cpu_to_be_64(tmpidx);
609 /* Store the allocated index for future use in the regfile */
610 rc = ulp_regfile_write(parms->regfile, atbls->regfile_wr_idx, idx);
612 BNXT_TF_DBG(ERR, "regfile[%d] write failed\n",
613 atbls->regfile_wr_idx);
619 * The set_tbl_entry API if search is not enabled or searched entry
622 if (!atbls->srch_b4_alloc || !alloc_parms.hit) {
623 struct tf_set_tbl_entry_parms set_parm = { 0 };
626 set_parm.dir = atbls->direction;
627 set_parm.type = atbls->table_type;
628 set_parm.idx = alloc_parms.idx;
629 set_parm.data = ulp_blob_data_get(blob, &length);
630 set_parm.data_sz_in_bytes = length / 8;
632 if (set_parm.type == TF_TBL_TYPE_EXT)
633 bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
634 &set_parm.tbl_scope_id);
636 set_parm.tbl_scope_id = 0;
638 /* set the table entry */
639 rc = tf_set_tbl_entry(parms->tfp, &set_parm);
641 BNXT_TF_DBG(ERR, "table[%d][%s][%d] set failed\n",
643 (set_parm.dir == TF_DIR_RX) ? "RX" : "TX",
649 /* Link the resource to the flow in the flow db */
650 memset(&fid_parms, 0, sizeof(fid_parms));
651 fid_parms.direction = atbls->direction;
652 fid_parms.resource_func = atbls->resource_func;
653 fid_parms.resource_type = atbls->table_type;
654 fid_parms.resource_hndl = alloc_parms.idx;
655 fid_parms.critical_resource = 0;
657 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
662 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
671 free_parms.dir = alloc_parms.dir;
672 free_parms.type = alloc_parms.type;
673 free_parms.idx = alloc_parms.idx;
675 trc = tf_free_tbl_entry(parms->tfp, &free_parms);
677 BNXT_TF_DBG(ERR, "Failed to free table entry on failure\n");
683 * Function to process the action Info. Iterate through the list
684 * action info templates and process it.
687 ulp_mapper_action_info_process(struct bnxt_ulp_mapper_parms *parms,
688 struct bnxt_ulp_mapper_act_tbl_info *tbl)
690 struct ulp_blob blob;
691 struct bnxt_ulp_mapper_result_field_info *flds, *fld;
692 uint32_t num_flds = 0;
693 uint32_t encap_flds = 0;
698 if (!tbl || !parms->act_prop || !parms->act_bitmap || !parms->regfile)
701 /* use the max size if encap is enabled */
702 if (tbl->encap_num_fields)
703 bit_size = BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS;
705 bit_size = tbl->result_bit_size;
706 if (!ulp_blob_init(&blob, bit_size, parms->order)) {
707 BNXT_TF_DBG(ERR, "action blob init failed\n");
711 flds = ulp_mapper_act_result_fields_get(tbl, &num_flds, &encap_flds);
712 if (!flds || !num_flds) {
713 BNXT_TF_DBG(ERR, "Template undefined for action\n");
717 for (i = 0; i < (num_flds + encap_flds); i++) {
719 rc = ulp_mapper_result_field_process(parms,
724 BNXT_TF_DBG(ERR, "Action field failed\n");
727 /* set the swap index if 64 bit swap is enabled */
728 if (parms->encap_byte_swap && encap_flds) {
729 if ((i + 1) == num_flds)
730 ulp_blob_encap_swap_idx_set(&blob);
731 /* if 64 bit swap is enabled perform the 64bit swap */
732 if ((i + 1) == (num_flds + encap_flds))
733 ulp_blob_perform_encap_swap(&blob);
737 rc = ulp_mapper_action_alloc_and_set(parms, &blob);
742 ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
743 struct bnxt_ulp_mapper_class_tbl_info *tbl)
745 struct bnxt_ulp_mapper_class_key_field_info *kflds;
746 struct ulp_blob key, mask, data;
747 uint32_t i, num_kflds;
750 struct tf_alloc_tcam_entry_parms aparms = { 0 };
751 struct tf_set_tcam_entry_parms sparms = { 0 };
752 struct ulp_flow_db_res_params fid_parms = { 0 };
753 struct tf_free_tcam_entry_parms free_parms = { 0 };
757 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
759 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
763 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
764 if (!kflds || !num_kflds) {
765 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
769 if (!ulp_blob_init(&key, tbl->key_bit_size, parms->order) ||
770 !ulp_blob_init(&mask, tbl->key_bit_size, parms->order) ||
771 !ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
772 BNXT_TF_DBG(ERR, "blob inits failed.\n");
776 /* create the key/mask */
778 * NOTE: The WC table will require some kind of flag to handle the
779 * mode bits within the key/mask
781 for (i = 0; i < num_kflds; i++) {
783 rc = ulp_mapper_keymask_field_process(parms, &kflds[i],
784 &key, 1, "TCAM Key");
786 BNXT_TF_DBG(ERR, "Key field set failed.\n");
791 rc = ulp_mapper_keymask_field_process(parms, &kflds[i],
792 &mask, 0, "TCAM Mask");
794 BNXT_TF_DBG(ERR, "Mask field set failed.\n");
799 aparms.dir = tbl->direction;
800 aparms.tcam_tbl_type = tbl->table_type;
801 aparms.search_enable = tbl->srch_b4_alloc;
802 aparms.key_sz_in_bits = tbl->key_bit_size;
803 aparms.key = ulp_blob_data_get(&key, &tmplen);
804 if (tbl->key_bit_size != tmplen) {
805 BNXT_TF_DBG(ERR, "Key len (%d) != Expected (%d)\n",
806 tmplen, tbl->key_bit_size);
810 aparms.mask = ulp_blob_data_get(&mask, &tmplen);
811 if (tbl->key_bit_size != tmplen) {
812 BNXT_TF_DBG(ERR, "Mask len (%d) != Expected (%d)\n",
813 tmplen, tbl->key_bit_size);
817 aparms.priority = tbl->priority;
820 * All failures after this succeeds require the entry to be freed.
821 * cannot return directly on failure, but needs to goto error
823 rc = tf_alloc_tcam_entry(tfp, &aparms);
825 BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
831 /* Build the result */
832 if (!tbl->srch_b4_alloc || !hit) {
833 struct bnxt_ulp_mapper_result_field_info *dflds;
834 struct bnxt_ulp_mapper_ident_info *idents;
835 uint32_t num_dflds, num_idents;
837 /* Alloc identifiers */
838 idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
840 for (i = 0; i < num_idents; i++) {
841 rc = ulp_mapper_ident_process(parms, tbl, &idents[i]);
843 /* Already logged the error, just return */
848 /* Create the result data blob */
849 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds);
850 if (!dflds || !num_dflds) {
851 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
856 for (i = 0; i < num_dflds; i++) {
857 rc = ulp_mapper_result_field_process(parms,
862 BNXT_TF_DBG(ERR, "Failed to set data fields\n");
867 sparms.dir = aparms.dir;
868 sparms.tcam_tbl_type = aparms.tcam_tbl_type;
869 sparms.idx = aparms.idx;
870 /* Already verified the key/mask lengths */
871 sparms.key = ulp_blob_data_get(&key, &tmplen);
872 sparms.mask = ulp_blob_data_get(&mask, &tmplen);
873 sparms.key_sz_in_bits = tbl->key_bit_size;
874 sparms.result = ulp_blob_data_get(&data, &tmplen);
876 if (tbl->result_bit_size != tmplen) {
877 BNXT_TF_DBG(ERR, "Result len (%d) != Expected (%d)\n",
878 tmplen, tbl->result_bit_size);
882 sparms.result_sz_in_bits = tbl->result_bit_size;
884 rc = tf_set_tcam_entry(tfp, &sparms);
886 BNXT_TF_DBG(ERR, "tcam[%d][%s][%d] write failed.\n",
887 sparms.tcam_tbl_type,
888 (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
893 BNXT_TF_DBG(ERR, "Not supporting search before alloc now\n");
898 /* Link the resource to the flow in the flow db */
899 fid_parms.direction = tbl->direction;
900 fid_parms.resource_func = tbl->resource_func;
901 fid_parms.resource_type = tbl->table_type;
902 fid_parms.critical_resource = tbl->critical_resource;
903 fid_parms.resource_hndl = aparms.idx;
905 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
910 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
912 /* Need to free the identifier, so goto error */
918 free_parms.dir = tbl->direction;
919 free_parms.tcam_tbl_type = tbl->table_type;
920 free_parms.idx = aparms.idx;
921 trc = tf_free_tcam_entry(tfp, &free_parms);
923 BNXT_TF_DBG(ERR, "Failed to free tcam[%d][%d][%d] on failure\n",
924 tbl->table_type, tbl->direction, aparms.idx);
930 ulp_mapper_em_tbl_process(struct bnxt_ulp_mapper_parms *parms,
931 struct bnxt_ulp_mapper_class_tbl_info *tbl)
933 struct bnxt_ulp_mapper_class_key_field_info *kflds;
934 struct bnxt_ulp_mapper_result_field_info *dflds;
935 struct ulp_blob key, data;
936 uint32_t i, num_kflds, num_dflds;
938 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
939 struct ulp_rte_act_prop *a_prop = parms->act_prop;
940 struct ulp_flow_db_res_params fid_parms = { 0 };
941 struct tf_insert_em_entry_parms iparms = { 0 };
942 struct tf_delete_em_entry_parms free_parms = { 0 };
946 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
947 if (!kflds || !num_kflds) {
948 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
952 /* Initialize the key/result blobs */
953 if (!ulp_blob_init(&key, tbl->blob_key_bit_size, parms->order) ||
954 !ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
955 BNXT_TF_DBG(ERR, "blob inits failed.\n");
960 for (i = 0; i < num_kflds; i++) {
962 rc = ulp_mapper_keymask_field_process(parms, &kflds[i],
965 BNXT_TF_DBG(ERR, "Key field set failed.\n");
971 * TBD: Normally should process identifiers in case of using recycle or
972 * loopback. Not supporting recycle for now.
975 /* Create the result data blob */
976 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds);
977 if (!dflds || !num_dflds) {
978 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
982 for (i = 0; i < num_dflds; i++) {
983 struct bnxt_ulp_mapper_result_field_info *fld;
987 rc = ulp_mapper_result_field_process(parms,
992 BNXT_TF_DBG(ERR, "Failed to set data fields.\n");
997 rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
998 &iparms.tbl_scope_id);
1000 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
1005 * NOTE: the actual blob size will differ from the size in the tbl
1006 * entry due to the padding.
1008 iparms.dup_check = 0;
1009 iparms.dir = tbl->direction;
1010 iparms.mem = tbl->mem;
1011 iparms.key = ulp_blob_data_get(&key, &tmplen);
1012 iparms.key_sz_in_bits = tbl->key_bit_size;
1013 iparms.em_record = ulp_blob_data_get(&data, &tmplen);
1014 iparms.em_record_sz_in_bits = tbl->result_bit_size;
1016 rc = tf_insert_em_entry(tfp, &iparms);
1018 BNXT_TF_DBG(ERR, "Failed to insert em entry rc=%d.\n", rc);
1022 if (tbl->mark_enable &&
1023 ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1024 BNXT_ULP_ACTION_BIT_MARK)) {
1025 uint32_t val, mark, gfid, flag;
1026 /* TBD: Need to determine if GFID is enabled globally */
1027 if (sizeof(val) != BNXT_ULP_ACT_PROP_SZ_MARK) {
1028 BNXT_TF_DBG(ERR, "Mark size (%d) != expected (%zu)\n",
1029 BNXT_ULP_ACT_PROP_SZ_MARK, sizeof(val));
1035 &a_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1038 mark = tfp_be_to_cpu_32(val);
1040 TF_GET_GFID_FROM_FLOW_ID(iparms.flow_id, gfid);
1041 TF_GET_FLAG_FROM_FLOW_ID(iparms.flow_id, flag);
1043 rc = ulp_mark_db_mark_add(parms->ulp_ctx,
1044 (flag == TF_GFID_TABLE_EXTERNAL),
1048 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1053 * Link the mark resource to the flow in the flow db
1054 * The mark is never the critical resource, so it is 0.
1056 memset(&fid_parms, 0, sizeof(fid_parms));
1057 fid_parms.direction = tbl->direction;
1058 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1059 fid_parms.resource_type = tbl->table_type;
1060 fid_parms.resource_hndl = iparms.flow_id;
1061 fid_parms.critical_resource = 0;
1063 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1068 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1070 /* Need to free the identifier, so goto error */
1075 /* Link the EM resource to the flow in the flow db */
1076 memset(&fid_parms, 0, sizeof(fid_parms));
1077 fid_parms.direction = tbl->direction;
1078 fid_parms.resource_func = tbl->resource_func;
1079 fid_parms.resource_type = tbl->table_type;
1080 fid_parms.critical_resource = tbl->critical_resource;
1081 fid_parms.resource_hndl = iparms.flow_handle;
1083 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1088 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1090 /* Need to free the identifier, so goto error */
1096 free_parms.dir = iparms.dir;
1097 free_parms.mem = iparms.mem;
1098 free_parms.tbl_scope_id = iparms.tbl_scope_id;
1099 free_parms.flow_handle = iparms.flow_handle;
1101 trc = tf_delete_em_entry(tfp, &free_parms);
1103 BNXT_TF_DBG(ERR, "Failed to delete EM entry on failed add\n");
1109 ulp_mapper_index_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1110 struct bnxt_ulp_mapper_class_tbl_info *tbl)
1112 struct bnxt_ulp_mapper_result_field_info *flds;
1113 struct ulp_flow_db_res_params fid_parms;
1114 struct ulp_blob data;
1117 uint32_t i, num_flds;
1118 int32_t rc = 0, trc = 0;
1119 struct tf_alloc_tbl_entry_parms aparms = { 0 };
1120 struct tf_set_tbl_entry_parms sparms = { 0 };
1121 struct tf_free_tbl_entry_parms free_parms = { 0 };
1123 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1125 if (!ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
1126 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
1130 flds = ulp_mapper_result_fields_get(tbl, &num_flds);
1131 if (!flds || !num_flds) {
1132 BNXT_TF_DBG(ERR, "Template undefined for action\n");
1136 for (i = 0; i < num_flds; i++) {
1137 rc = ulp_mapper_result_field_process(parms,
1142 BNXT_TF_DBG(ERR, "data field failed\n");
1147 aparms.dir = tbl->direction;
1148 aparms.type = tbl->table_type;
1149 aparms.search_enable = tbl->srch_b4_alloc;
1150 aparms.result = ulp_blob_data_get(&data, &tmplen);
1151 aparms.result_sz_in_bytes = ULP_SZ_BITS2BYTES(tbl->result_bit_size);
1153 /* All failures after the alloc succeeds require a free */
1154 rc = tf_alloc_tbl_entry(tfp, &aparms);
1156 BNXT_TF_DBG(ERR, "Alloc table[%d][%s] failed rc=%d\n",
1158 (tbl->direction == TF_DIR_RX) ? "RX" : "TX",
1163 /* Always storing values in Regfile in BE */
1164 idx = tfp_cpu_to_be_64(aparms.idx);
1165 rc = ulp_regfile_write(parms->regfile, tbl->regfile_wr_idx, idx);
1167 BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
1168 tbl->regfile_wr_idx);
1172 if (!tbl->srch_b4_alloc) {
1173 sparms.dir = tbl->direction;
1174 sparms.type = tbl->table_type;
1175 sparms.data = ulp_blob_data_get(&data, &tmplen);
1176 sparms.data_sz_in_bytes =
1177 ULP_SZ_BITS2BYTES(tbl->result_bit_size);
1178 sparms.idx = aparms.idx;
1180 rc = tf_set_tbl_entry(tfp, &sparms);
1182 BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
1184 (tbl->direction == TF_DIR_RX) ? "RX" : "TX",
1192 /* Link the resource to the flow in the flow db */
1193 memset(&fid_parms, 0, sizeof(fid_parms));
1194 fid_parms.direction = tbl->direction;
1195 fid_parms.resource_func = tbl->resource_func;
1196 fid_parms.resource_type = tbl->table_type;
1197 fid_parms.resource_hndl = aparms.idx;
1198 fid_parms.critical_resource = 0;
1200 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1205 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
1213 * Free the allocated resource since we failed to either
1214 * write to the entry or link the flow
1216 free_parms.dir = tbl->direction;
1217 free_parms.type = tbl->table_type;
1218 free_parms.idx = aparms.idx;
1220 trc = tf_free_tbl_entry(tfp, &free_parms);
1222 BNXT_TF_DBG(ERR, "Failed to free tbl entry on failure\n");
1228 * Function to process the action template. Iterate through the list
1229 * action info templates and process it.
1232 ulp_mapper_action_tbls_process(struct bnxt_ulp_mapper_parms *parms)
1237 if (!parms->atbls || !parms->num_atbls) {
1238 BNXT_TF_DBG(ERR, "No action tables for template[%d][%d].\n",
1239 parms->dev_id, parms->act_tid);
1243 for (i = 0; i < parms->num_atbls; i++) {
1244 rc = ulp_mapper_action_info_process(parms, &parms->atbls[i]);
1252 /* Create the classifier table entries for a flow. */
1254 ulp_mapper_class_tbls_process(struct bnxt_ulp_mapper_parms *parms)
1262 if (!parms->ctbls || !parms->num_ctbls) {
1263 BNXT_TF_DBG(ERR, "No class tables for template[%d][%d].\n",
1264 parms->dev_id, parms->class_tid);
1268 for (i = 0; i < parms->num_ctbls; i++) {
1269 struct bnxt_ulp_mapper_class_tbl_info *tbl = &parms->ctbls[i];
1271 switch (tbl->resource_func) {
1272 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
1273 rc = ulp_mapper_tcam_tbl_process(parms, tbl);
1275 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
1276 rc = ulp_mapper_em_tbl_process(parms, tbl);
1278 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
1279 rc = ulp_mapper_index_tbl_process(parms, tbl);
1282 BNXT_TF_DBG(ERR, "Unexpected class resource %d\n",
1283 tbl->resource_func);
1288 BNXT_TF_DBG(ERR, "Resource type %d failed\n",
1289 tbl->resource_func);
1298 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
1299 struct ulp_flow_db_res_params *res)
1305 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
1309 tfp = bnxt_ulp_cntxt_tfp_get(ulp);
1311 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
1315 switch (res->resource_func) {
1316 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
1317 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
1319 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
1320 rc = ulp_mapper_eem_entry_free(ulp, tfp, res);
1322 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
1323 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
1325 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
1326 rc = ulp_mapper_ident_free(ulp, tfp, res);
1328 case BNXT_ULP_RESOURCE_FUNC_HW_FID:
1329 rc = ulp_mapper_mark_free(ulp, res);
1339 ulp_mapper_resources_free(struct bnxt_ulp_context *ulp_ctx,
1341 enum bnxt_ulp_flow_db_tables tbl_type)
1343 struct ulp_flow_db_res_params res_parms = { 0 };
1347 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
1352 * Set the critical resource on the first resource del, then iterate
1353 * while status is good
1355 res_parms.critical_resource = 1;
1356 rc = ulp_flow_db_resource_del(ulp_ctx, tbl_type, fid, &res_parms);
1360 * This is unexpected on the first call to resource del.
1361 * It likely means that the flow did not exist in the flow db.
1363 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
1369 trc = ulp_mapper_resource_free(ulp_ctx, &res_parms);
1372 * On fail, we still need to attempt to free the
1373 * remaining resources. Don't return
1376 "Flow[%d][0x%x] Res[%d][0x%016" PRIx64
1377 "] failed rc=%d.\n",
1378 tbl_type, fid, res_parms.resource_func,
1379 res_parms.resource_hndl, trc);
1381 /* All subsequent call require the critical_resource be zero */
1382 res_parms.critical_resource = 0;
1384 rc = ulp_flow_db_resource_del(ulp_ctx,
1390 /* Free the Flow ID since we've removed all resources */
1391 rc = ulp_flow_db_fid_free(ulp_ctx, tbl_type, fid);
1397 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx, uint32_t fid)
1400 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
1404 return ulp_mapper_resources_free(ulp_ctx,
1406 BNXT_ULP_REGULAR_FLOW_TABLE);
1409 /* Function to handle the mapping of the Flow to be compatible
1410 * with the underlying hardware.
1413 ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
1414 struct bnxt_ulp_mapper_create_parms *cparms,
1417 struct bnxt_ulp_device_params *device_params;
1418 struct bnxt_ulp_mapper_parms parms;
1419 struct ulp_regfile regfile;
1422 if (!ulp_ctx || !cparms)
1425 /* Initialize the parms structure */
1426 memset(&parms, 0, sizeof(parms));
1427 parms.act_prop = cparms->act_prop;
1428 parms.act_bitmap = cparms->act;
1429 parms.regfile = ®file;
1430 parms.hdr_field = cparms->hdr_field;
1431 parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
1432 parms.ulp_ctx = ulp_ctx;
1434 /* Get the device id from the ulp context */
1435 if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &parms.dev_id)) {
1436 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
1440 /* Get the action table entry from device id and act context id */
1441 parms.act_tid = cparms->act_tid;
1442 parms.atbls = ulp_mapper_action_tbl_list_get(parms.dev_id,
1445 if (!parms.atbls || !parms.num_atbls) {
1446 BNXT_TF_DBG(ERR, "No action tables for %d:%d\n",
1447 parms.dev_id, parms.act_tid);
1451 /* Get the class table entry from device id and act context id */
1452 parms.class_tid = cparms->class_tid;
1453 parms.ctbls = ulp_mapper_class_tbl_list_get(parms.dev_id,
1456 if (!parms.ctbls || !parms.num_ctbls) {
1457 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
1458 parms.dev_id, parms.class_tid);
1462 /* Get the byte order for the further processing from device params */
1463 device_params = bnxt_ulp_device_params_get(parms.dev_id);
1464 if (!device_params) {
1465 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
1466 parms.dev_id, parms.class_tid);
1469 parms.order = device_params->byte_order;
1470 parms.encap_byte_swap = device_params->encap_byte_swap;
1472 /* initialize the registry file for further processing */
1473 if (!ulp_regfile_init(parms.regfile)) {
1474 BNXT_TF_DBG(ERR, "regfile initialization failed.\n");
1478 /* Allocate a Flow ID for attaching all resources for the flow to.
1479 * Once allocated, all errors have to walk the list of resources and
1480 * free each of them.
1482 rc = ulp_flow_db_fid_alloc(ulp_ctx,
1483 BNXT_ULP_REGULAR_FLOW_TABLE,
1486 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
1490 /* Process the action template list from the selected action table*/
1491 rc = ulp_mapper_action_tbls_process(&parms);
1493 BNXT_TF_DBG(ERR, "action tables failed creation for %d:%d\n",
1494 parms.dev_id, parms.act_tid);
1498 /* All good. Now process the class template */
1499 rc = ulp_mapper_class_tbls_process(&parms);
1501 BNXT_TF_DBG(ERR, "class tables failed creation for %d:%d\n",
1502 parms.dev_id, parms.class_tid);
1506 *flowid = parms.fid;
1511 /* Free all resources that were allocated during flow creation */
1512 trc = ulp_mapper_flow_destroy(ulp_ctx, parms.fid);
1514 BNXT_TF_DBG(ERR, "Failed to free all resources rc=%d\n", trc);