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 ulp_mapper_action_tbls_process(struct bnxt_ulp_mapper_parms *parms);
23 ulp_mapper_class_tbls_process(struct bnxt_ulp_mapper_parms *parms);
26 * Get the size of the action property for a given index.
28 * idx [in] The index for the action property
30 * returns the size of the action property.
33 ulp_mapper_act_prop_size_get(uint32_t idx)
35 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST)
37 return ulp_act_prop_map_table[idx];
41 * Get the list of result fields that implement the flow action
43 * ctxt [in] The ulp context
45 * tbl [in] A single table instance to get the key fields from
47 * num_flds [out] The number of key fields in the returned array
49 * returns array of Key fields, or NULL on error
51 static struct bnxt_ulp_mapper_class_key_field_info *
52 ulp_mapper_key_fields_get(struct bnxt_ulp_mapper_class_tbl_info *tbl,
57 if (!tbl || !num_flds)
60 idx = tbl->key_start_idx;
61 *num_flds = tbl->key_num_fields;
63 /* NOTE: Need template to provide range checking define */
64 return &ulp_class_key_field_list[idx];
68 * Get the list of data fields that implement the flow.
70 * ctxt [in] The ulp context
72 * tbl [in] A single table instance to get the data fields from
74 * num_flds [out] The number of data fields in the returned array.
76 * Returns array of data fields, or NULL on error.
78 static struct bnxt_ulp_mapper_result_field_info *
79 ulp_mapper_result_fields_get(struct bnxt_ulp_mapper_class_tbl_info *tbl,
84 if (!tbl || !num_flds)
87 idx = tbl->result_start_idx;
88 *num_flds = tbl->result_num_fields;
90 /* NOTE: Need template to provide range checking define */
91 return &ulp_class_result_field_list[idx];
95 * Get the list of result fields that implement the flow action.
97 * tbl [in] A single table instance to get the results fields
98 * from num_flds [out] The number of data fields in the returned
101 * Returns array of data fields, or NULL on error.
103 static struct bnxt_ulp_mapper_result_field_info *
104 ulp_mapper_act_result_fields_get(struct bnxt_ulp_mapper_act_tbl_info *tbl,
105 uint32_t *num_rslt_flds,
106 uint32_t *num_encap_flds)
110 if (!tbl || !num_rslt_flds || !num_encap_flds)
113 idx = tbl->result_start_idx;
114 *num_rslt_flds = tbl->result_num_fields;
115 *num_encap_flds = tbl->encap_num_fields;
117 /* NOTE: Need template to provide range checking define */
118 return &ulp_act_result_field_list[idx];
122 * Get the list of ident fields that implement the flow
124 * tbl [in] A single table instance to get the ident fields from
126 * num_flds [out] The number of ident fields in the returned array
128 * returns array of ident fields, or NULL on error
130 static struct bnxt_ulp_mapper_ident_info *
131 ulp_mapper_ident_fields_get(struct bnxt_ulp_mapper_class_tbl_info *tbl,
136 if (!tbl || !num_flds)
139 idx = tbl->ident_start_idx;
140 *num_flds = tbl->ident_nums;
142 /* NOTE: Need template to provide range checking define */
143 return &ulp_ident_list[idx];
146 static inline int32_t
147 ulp_mapper_tcam_entry_free(struct bnxt_ulp_context *ulp __rte_unused,
149 struct ulp_flow_db_res_params *res)
151 struct tf_free_tcam_entry_parms fparms = {
152 .dir = res->direction,
153 .tcam_tbl_type = res->resource_type,
154 .idx = (uint16_t)res->resource_hndl
157 return tf_free_tcam_entry(tfp, &fparms);
160 static inline int32_t
161 ulp_mapper_index_entry_free(struct bnxt_ulp_context *ulp __rte_unused,
163 struct ulp_flow_db_res_params *res)
165 struct tf_free_tbl_entry_parms fparms = {
166 .dir = res->direction,
167 .type = res->resource_type,
168 .idx = (uint32_t)res->resource_hndl
171 return tf_free_tbl_entry(tfp, &fparms);
174 static inline int32_t
175 ulp_mapper_eem_entry_free(struct bnxt_ulp_context *ulp,
177 struct ulp_flow_db_res_params *res)
179 struct tf_delete_em_entry_parms fparms = { 0 };
182 fparms.dir = res->direction;
183 fparms.mem = TF_MEM_EXTERNAL;
184 fparms.flow_handle = res->resource_hndl;
186 rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
188 BNXT_TF_DBG(ERR, "Failed to get table scope\n");
192 return tf_delete_em_entry(tfp, &fparms);
195 static inline int32_t
196 ulp_mapper_ident_free(struct bnxt_ulp_context *ulp __rte_unused,
198 struct ulp_flow_db_res_params *res)
200 struct tf_free_identifier_parms fparms = {
201 .dir = res->direction,
202 .ident_type = res->resource_type,
203 .id = (uint16_t)res->resource_hndl
206 return tf_free_identifier(tfp, &fparms);
209 static inline int32_t
210 ulp_mapper_mark_free(struct bnxt_ulp_context *ulp,
211 struct ulp_flow_db_res_params *res)
217 fid = (uint32_t)res->resource_hndl;
218 TF_GET_FLAG_FROM_FLOW_ID(fid, flag);
219 TF_GET_GFID_FROM_FLOW_ID(fid, gfid);
221 return ulp_mark_db_mark_del(ulp,
222 (flag == TF_GFID_TABLE_EXTERNAL),
228 ulp_mapper_ident_process(struct bnxt_ulp_mapper_parms *parms,
229 struct bnxt_ulp_mapper_class_tbl_info *tbl,
230 struct bnxt_ulp_mapper_ident_info *ident)
232 struct ulp_flow_db_res_params fid_parms;
235 struct tf_alloc_identifier_parms iparms = { 0 };
236 struct tf_free_identifier_parms free_parms = { 0 };
240 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
242 BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
246 idx = ident->regfile_wr_idx;
248 iparms.ident_type = ident->ident_type;
249 iparms.dir = tbl->direction;
251 rc = tf_alloc_identifier(tfp, &iparms);
253 BNXT_TF_DBG(ERR, "Alloc ident %s:%d failed.\n",
254 (iparms.dir == TF_DIR_RX) ? "RX" : "TX",
259 id = (uint64_t)tfp_cpu_to_be_64(iparms.id);
260 if (!ulp_regfile_write(parms->regfile, idx, id)) {
261 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
263 /* Need to free the identifier, so goto error */
267 /* Link the resource to the flow in the flow db */
268 memset(&fid_parms, 0, sizeof(fid_parms));
269 fid_parms.direction = tbl->direction;
270 fid_parms.resource_func = ident->resource_func;
271 fid_parms.resource_type = ident->ident_type;
272 fid_parms.resource_hndl = iparms.id;
273 fid_parms.critical_resource = 0;
275 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
280 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
282 /* Need to free the identifier, so goto error */
289 /* Need to free the identifier */
290 free_parms.dir = tbl->direction;
291 free_parms.ident_type = ident->ident_type;
292 free_parms.id = iparms.id;
294 (void)tf_free_identifier(tfp, &free_parms);
296 BNXT_TF_DBG(ERR, "Ident process failed for %s:%s\n",
298 (tbl->direction == TF_DIR_RX) ? "RX" : "TX");
303 ulp_mapper_result_field_process(struct bnxt_ulp_mapper_parms *parms,
304 struct bnxt_ulp_mapper_result_field_info *fld,
305 struct ulp_blob *blob)
307 uint16_t idx, size_idx;
310 uint32_t val_size = 0, field_size = 0;
312 switch (fld->result_opcode) {
313 case BNXT_ULP_RESULT_OPC_SET_TO_CONSTANT:
314 val = fld->result_operand;
315 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
316 BNXT_TF_DBG(ERR, "Failed to add field\n");
320 case BNXT_ULP_RESULT_OPC_SET_TO_ACT_PROP:
321 if (!ulp_operand_read(fld->result_operand,
322 (uint8_t *)&idx, sizeof(uint16_t))) {
323 BNXT_TF_DBG(ERR, "operand read failed\n");
326 idx = tfp_be_to_cpu_16(idx);
328 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
329 BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", idx);
332 val = &parms->act_prop->act_details[idx];
333 field_size = ulp_mapper_act_prop_size_get(idx);
334 if (fld->field_bit_size < ULP_BYTE_2_BITS(field_size)) {
335 field_size = field_size -
336 ((fld->field_bit_size + 7) / 8);
339 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
340 BNXT_TF_DBG(ERR, "push field failed\n");
344 case BNXT_ULP_RESULT_OPC_SET_TO_ACT_PROP_SZ:
345 if (!ulp_operand_read(fld->result_operand,
346 (uint8_t *)&idx, sizeof(uint16_t))) {
347 BNXT_TF_DBG(ERR, "operand read failed\n");
350 idx = tfp_be_to_cpu_16(idx);
352 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
353 BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", idx);
356 val = &parms->act_prop->act_details[idx];
358 /* get the size index next */
359 if (!ulp_operand_read(&fld->result_operand[sizeof(uint16_t)],
360 (uint8_t *)&size_idx, sizeof(uint16_t))) {
361 BNXT_TF_DBG(ERR, "operand read failed\n");
364 size_idx = tfp_be_to_cpu_16(size_idx);
366 if (size_idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
367 BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", size_idx);
370 memcpy(&val_size, &parms->act_prop->act_details[size_idx],
372 val_size = tfp_be_to_cpu_32(val_size);
373 val_size = ULP_BYTE_2_BITS(val_size);
374 ulp_blob_push_encap(blob, val, val_size);
376 case BNXT_ULP_RESULT_OPC_SET_TO_REGFILE:
377 if (!ulp_operand_read(fld->result_operand,
378 (uint8_t *)&idx, sizeof(uint16_t))) {
379 BNXT_TF_DBG(ERR, "operand read failed\n");
383 idx = tfp_be_to_cpu_16(idx);
384 /* Uninitialized regfile entries return 0 */
385 if (!ulp_regfile_read(parms->regfile, idx, ®val)) {
386 BNXT_TF_DBG(ERR, "regfile[%d] read oob\n", idx);
390 val = ulp_blob_push_64(blob, ®val, fld->field_bit_size);
392 BNXT_TF_DBG(ERR, "push field failed\n");
403 /* Function to alloc action record and set the table. */
405 ulp_mapper_keymask_field_process(struct bnxt_ulp_mapper_parms *parms,
406 struct bnxt_ulp_mapper_class_key_field_info *f,
407 struct ulp_blob *blob,
411 uint16_t idx, bitlen;
414 struct ulp_regfile *regfile = parms->regfile;
416 struct bnxt_ulp_mapper_class_key_field_info *fld = f;
420 operand = fld->spec_operand;
421 opcode = fld->spec_opcode;
423 operand = fld->mask_operand;
424 opcode = fld->mask_opcode;
427 bitlen = fld->field_bit_size;
430 case BNXT_ULP_SPEC_OPC_SET_TO_CONSTANT:
432 if (!ulp_blob_push(blob, val, bitlen)) {
433 BNXT_TF_DBG(ERR, "push to key blob failed\n");
437 case BNXT_ULP_SPEC_OPC_ADD_PAD:
438 if (!ulp_blob_pad_push(blob, bitlen)) {
439 BNXT_TF_DBG(ERR, "Pad too large for blob\n");
444 case BNXT_ULP_SPEC_OPC_SET_TO_HDR_FIELD:
445 if (!ulp_operand_read(operand, (uint8_t *)&idx,
447 BNXT_TF_DBG(ERR, "key operand read failed.\n");
450 idx = tfp_be_to_cpu_16(idx);
452 val = parms->hdr_field[idx].spec;
454 val = parms->hdr_field[idx].mask;
457 * Need to account for how much data was pushed to the header
458 * field vs how much is to be inserted in the key/mask.
460 field_size = parms->hdr_field[idx].size;
461 if (bitlen < ULP_BYTE_2_BITS(field_size)) {
462 field_size = field_size - ((bitlen + 7) / 8);
466 if (!ulp_blob_push(blob, val, bitlen)) {
467 BNXT_TF_DBG(ERR, "push to key blob failed\n");
471 case BNXT_ULP_SPEC_OPC_SET_TO_REGFILE:
472 if (!ulp_operand_read(operand, (uint8_t *)&idx,
474 BNXT_TF_DBG(ERR, "key operand read failed.\n");
477 idx = tfp_be_to_cpu_16(idx);
479 if (!ulp_regfile_read(regfile, idx, ®val)) {
480 BNXT_TF_DBG(ERR, "regfile[%d] read failed.\n",
485 val = ulp_blob_push_64(blob, ®val, bitlen);
487 BNXT_TF_DBG(ERR, "push to key blob failed\n");
497 /* Function to alloc action record and set the table. */
499 ulp_mapper_action_alloc_and_set(struct bnxt_ulp_mapper_parms *parms,
500 struct ulp_blob *blob)
502 struct ulp_flow_db_res_params fid_parms;
503 struct tf_alloc_tbl_entry_parms alloc_parms = { 0 };
504 struct tf_free_tbl_entry_parms free_parms = { 0 };
505 struct bnxt_ulp_mapper_act_tbl_info *atbls = parms->atbls;
510 /* Set the allocation parameters for the table*/
511 alloc_parms.dir = atbls->direction;
512 alloc_parms.type = atbls->table_type;
513 alloc_parms.search_enable = atbls->srch_b4_alloc;
514 alloc_parms.result = ulp_blob_data_get(blob,
515 &alloc_parms.result_sz_in_bytes);
516 if (!alloc_parms.result) {
517 BNXT_TF_DBG(ERR, "blob is not populated\n");
521 rc = tf_alloc_tbl_entry(parms->tfp, &alloc_parms);
523 BNXT_TF_DBG(ERR, "table type= [%d] dir = [%s] alloc failed\n",
525 (alloc_parms.dir == TF_DIR_RX) ? "RX" : "TX");
529 /* Need to calculate the idx for the result record */
531 * TBD: Need to get the stride from tflib instead of having to
532 * understand the construction of the pointer
534 uint64_t tmpidx = alloc_parms.idx;
536 if (atbls->table_type == TF_TBL_TYPE_EXT)
537 tmpidx = (alloc_parms.idx * TF_ACTION_RECORD_SZ) >> 4;
539 tmpidx = alloc_parms.idx;
541 idx = tfp_cpu_to_be_64(tmpidx);
543 /* Store the allocated index for future use in the regfile */
544 rc = ulp_regfile_write(parms->regfile, atbls->regfile_wr_idx, idx);
546 BNXT_TF_DBG(ERR, "regfile[%d] write failed\n",
547 atbls->regfile_wr_idx);
553 * The set_tbl_entry API if search is not enabled or searched entry
556 if (!atbls->srch_b4_alloc || !alloc_parms.hit) {
557 struct tf_set_tbl_entry_parms set_parm = { 0 };
560 set_parm.dir = atbls->direction;
561 set_parm.type = atbls->table_type;
562 set_parm.idx = alloc_parms.idx;
563 set_parm.data = ulp_blob_data_get(blob, &length);
564 set_parm.data_sz_in_bytes = length / 8;
566 if (set_parm.type == TF_TBL_TYPE_EXT)
567 bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
568 &set_parm.tbl_scope_id);
570 set_parm.tbl_scope_id = 0;
572 /* set the table entry */
573 rc = tf_set_tbl_entry(parms->tfp, &set_parm);
575 BNXT_TF_DBG(ERR, "table[%d][%s][%d] set failed\n",
577 (set_parm.dir == TF_DIR_RX) ? "RX" : "TX",
583 /* Link the resource to the flow in the flow db */
584 memset(&fid_parms, 0, sizeof(fid_parms));
585 fid_parms.direction = atbls->direction;
586 fid_parms.resource_func = atbls->resource_func;
587 fid_parms.resource_type = atbls->table_type;
588 fid_parms.resource_hndl = alloc_parms.idx;
589 fid_parms.critical_resource = 0;
591 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
596 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
605 free_parms.dir = alloc_parms.dir;
606 free_parms.type = alloc_parms.type;
607 free_parms.idx = alloc_parms.idx;
609 trc = tf_free_tbl_entry(parms->tfp, &free_parms);
611 BNXT_TF_DBG(ERR, "Failed to free table entry on failure\n");
617 * Function to process the action Info. Iterate through the list
618 * action info templates and process it.
621 ulp_mapper_action_info_process(struct bnxt_ulp_mapper_parms *parms,
622 struct bnxt_ulp_mapper_act_tbl_info *tbl)
624 struct ulp_blob blob;
625 struct bnxt_ulp_mapper_result_field_info *flds, *fld;
626 uint32_t num_flds = 0;
627 uint32_t encap_flds = 0;
632 if (!tbl || !parms->act_prop || !parms->act_bitmap || !parms->regfile)
635 /* use the max size if encap is enabled */
636 if (tbl->encap_num_fields)
637 bit_size = BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS;
639 bit_size = tbl->result_bit_size;
640 if (!ulp_blob_init(&blob, bit_size, parms->order)) {
641 BNXT_TF_DBG(ERR, "action blob init failed\n");
645 flds = ulp_mapper_act_result_fields_get(tbl, &num_flds, &encap_flds);
646 if (!flds || !num_flds) {
647 BNXT_TF_DBG(ERR, "Template undefined for action\n");
651 for (i = 0; i < (num_flds + encap_flds); i++) {
653 rc = ulp_mapper_result_field_process(parms,
657 BNXT_TF_DBG(ERR, "Action field failed\n");
660 /* set the swap index if 64 bit swap is enabled */
661 if (parms->encap_byte_swap && encap_flds) {
662 if ((i + 1) == num_flds)
663 ulp_blob_encap_swap_idx_set(&blob);
664 /* if 64 bit swap is enabled perform the 64bit swap */
665 if ((i + 1) == (num_flds + encap_flds))
666 ulp_blob_perform_encap_swap(&blob);
670 rc = ulp_mapper_action_alloc_and_set(parms, &blob);
675 ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
676 struct bnxt_ulp_mapper_class_tbl_info *tbl)
678 struct bnxt_ulp_mapper_class_key_field_info *kflds;
679 struct ulp_blob key, mask, data;
680 uint32_t i, num_kflds;
683 struct tf_alloc_tcam_entry_parms aparms = { 0 };
684 struct tf_set_tcam_entry_parms sparms = { 0 };
685 struct ulp_flow_db_res_params fid_parms = { 0 };
686 struct tf_free_tcam_entry_parms free_parms = { 0 };
690 tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
692 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
696 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
697 if (!kflds || !num_kflds) {
698 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
702 if (!ulp_blob_init(&key, tbl->key_bit_size, parms->order) ||
703 !ulp_blob_init(&mask, tbl->key_bit_size, parms->order) ||
704 !ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
705 BNXT_TF_DBG(ERR, "blob inits failed.\n");
709 /* create the key/mask */
711 * NOTE: The WC table will require some kind of flag to handle the
712 * mode bits within the key/mask
714 for (i = 0; i < num_kflds; i++) {
716 rc = ulp_mapper_keymask_field_process(parms, &kflds[i],
719 BNXT_TF_DBG(ERR, "Key field set failed.\n");
724 rc = ulp_mapper_keymask_field_process(parms, &kflds[i],
727 BNXT_TF_DBG(ERR, "Mask field set failed.\n");
732 aparms.dir = tbl->direction;
733 aparms.tcam_tbl_type = tbl->table_type;
734 aparms.search_enable = tbl->srch_b4_alloc;
735 aparms.key_sz_in_bits = tbl->key_bit_size;
736 aparms.key = ulp_blob_data_get(&key, &tmplen);
737 if (tbl->key_bit_size != tmplen) {
738 BNXT_TF_DBG(ERR, "Key len (%d) != Expected (%d)\n",
739 tmplen, tbl->key_bit_size);
743 aparms.mask = ulp_blob_data_get(&mask, &tmplen);
744 if (tbl->key_bit_size != tmplen) {
745 BNXT_TF_DBG(ERR, "Mask len (%d) != Expected (%d)\n",
746 tmplen, tbl->key_bit_size);
750 aparms.priority = tbl->priority;
753 * All failures after this succeeds require the entry to be freed.
754 * cannot return directly on failure, but needs to goto error
756 rc = tf_alloc_tcam_entry(tfp, &aparms);
758 BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
764 /* Build the result */
765 if (!tbl->srch_b4_alloc || !hit) {
766 struct bnxt_ulp_mapper_result_field_info *dflds;
767 struct bnxt_ulp_mapper_ident_info *idents;
768 uint32_t num_dflds, num_idents;
770 /* Alloc identifiers */
771 idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
773 for (i = 0; i < num_idents; i++) {
774 rc = ulp_mapper_ident_process(parms, tbl, &idents[i]);
776 /* Already logged the error, just return */
781 /* Create the result data blob */
782 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds);
783 if (!dflds || !num_dflds) {
784 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
789 for (i = 0; i < num_dflds; i++) {
790 rc = ulp_mapper_result_field_process(parms,
794 BNXT_TF_DBG(ERR, "Failed to set data fields\n");
799 sparms.dir = aparms.dir;
800 sparms.tcam_tbl_type = aparms.tcam_tbl_type;
801 sparms.idx = aparms.idx;
802 /* Already verified the key/mask lengths */
803 sparms.key = ulp_blob_data_get(&key, &tmplen);
804 sparms.mask = ulp_blob_data_get(&mask, &tmplen);
805 sparms.key_sz_in_bits = tbl->key_bit_size;
806 sparms.result = ulp_blob_data_get(&data, &tmplen);
808 if (tbl->result_bit_size != tmplen) {
809 BNXT_TF_DBG(ERR, "Result len (%d) != Expected (%d)\n",
810 tmplen, tbl->result_bit_size);
814 sparms.result_sz_in_bits = tbl->result_bit_size;
816 rc = tf_set_tcam_entry(tfp, &sparms);
818 BNXT_TF_DBG(ERR, "tcam[%d][%s][%d] write failed.\n",
819 sparms.tcam_tbl_type,
820 (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
825 BNXT_TF_DBG(ERR, "Not supporting search before alloc now\n");
830 /* Link the resource to the flow in the flow db */
831 fid_parms.direction = tbl->direction;
832 fid_parms.resource_func = tbl->resource_func;
833 fid_parms.resource_type = tbl->table_type;
834 fid_parms.critical_resource = tbl->critical_resource;
835 fid_parms.resource_hndl = aparms.idx;
837 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
842 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
844 /* Need to free the identifier, so goto error */
850 free_parms.dir = tbl->direction;
851 free_parms.tcam_tbl_type = tbl->table_type;
852 free_parms.idx = aparms.idx;
853 trc = tf_free_tcam_entry(tfp, &free_parms);
855 BNXT_TF_DBG(ERR, "Failed to free tcam[%d][%d][%d] on failure\n",
856 tbl->table_type, tbl->direction, aparms.idx);
862 ulp_mapper_em_tbl_process(struct bnxt_ulp_mapper_parms *parms,
863 struct bnxt_ulp_mapper_class_tbl_info *tbl)
865 struct bnxt_ulp_mapper_class_key_field_info *kflds;
866 struct bnxt_ulp_mapper_result_field_info *dflds;
867 struct ulp_blob key, data;
868 uint32_t i, num_kflds, num_dflds;
870 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
871 struct ulp_rte_act_prop *a_prop = parms->act_prop;
872 struct ulp_flow_db_res_params fid_parms = { 0 };
873 struct tf_insert_em_entry_parms iparms = { 0 };
874 struct tf_delete_em_entry_parms free_parms = { 0 };
878 kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
879 if (!kflds || !num_kflds) {
880 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
884 /* Initialize the key/result blobs */
885 if (!ulp_blob_init(&key, tbl->blob_key_bit_size, parms->order) ||
886 !ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
887 BNXT_TF_DBG(ERR, "blob inits failed.\n");
892 for (i = 0; i < num_kflds; i++) {
894 rc = ulp_mapper_keymask_field_process(parms, &kflds[i],
897 BNXT_TF_DBG(ERR, "Key field set failed.\n");
903 * TBD: Normally should process identifiers in case of using recycle or
904 * loopback. Not supporting recycle for now.
907 /* Create the result data blob */
908 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds);
909 if (!dflds || !num_dflds) {
910 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
914 for (i = 0; i < num_dflds; i++) {
915 struct bnxt_ulp_mapper_result_field_info *fld;
919 rc = ulp_mapper_result_field_process(parms,
923 BNXT_TF_DBG(ERR, "Failed to set data fields.\n");
928 rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
929 &iparms.tbl_scope_id);
931 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
936 * NOTE: the actual blob size will differ from the size in the tbl
937 * entry due to the padding.
939 iparms.dup_check = 0;
940 iparms.dir = tbl->direction;
941 iparms.mem = tbl->mem;
942 iparms.key = ulp_blob_data_get(&key, &tmplen);
943 iparms.key_sz_in_bits = tbl->key_bit_size;
944 iparms.em_record = ulp_blob_data_get(&data, &tmplen);
945 iparms.em_record_sz_in_bits = tbl->result_bit_size;
947 rc = tf_insert_em_entry(tfp, &iparms);
949 BNXT_TF_DBG(ERR, "Failed to insert em entry rc=%d.\n", rc);
953 if (tbl->mark_enable &&
954 ULP_BITMAP_ISSET(parms->act_bitmap->bits,
955 BNXT_ULP_ACTION_BIT_MARK)) {
956 uint32_t val, mark, gfid, flag;
957 /* TBD: Need to determine if GFID is enabled globally */
958 if (sizeof(val) != BNXT_ULP_ACT_PROP_SZ_MARK) {
959 BNXT_TF_DBG(ERR, "Mark size (%d) != expected (%zu)\n",
960 BNXT_ULP_ACT_PROP_SZ_MARK, sizeof(val));
966 &a_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
969 mark = tfp_be_to_cpu_32(val);
971 TF_GET_GFID_FROM_FLOW_ID(iparms.flow_id, gfid);
972 TF_GET_FLAG_FROM_FLOW_ID(iparms.flow_id, flag);
974 rc = ulp_mark_db_mark_add(parms->ulp_ctx,
975 (flag == TF_GFID_TABLE_EXTERNAL),
979 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
984 * Link the mark resource to the flow in the flow db
985 * The mark is never the critical resource, so it is 0.
987 memset(&fid_parms, 0, sizeof(fid_parms));
988 fid_parms.direction = tbl->direction;
989 fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
990 fid_parms.resource_type = tbl->table_type;
991 fid_parms.resource_hndl = iparms.flow_id;
992 fid_parms.critical_resource = 0;
994 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
999 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1001 /* Need to free the identifier, so goto error */
1006 /* Link the EM resource to the flow in the flow db */
1007 memset(&fid_parms, 0, sizeof(fid_parms));
1008 fid_parms.direction = tbl->direction;
1009 fid_parms.resource_func = tbl->resource_func;
1010 fid_parms.resource_type = tbl->table_type;
1011 fid_parms.critical_resource = tbl->critical_resource;
1012 fid_parms.resource_hndl = iparms.flow_handle;
1014 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1019 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1021 /* Need to free the identifier, so goto error */
1027 free_parms.dir = iparms.dir;
1028 free_parms.mem = iparms.mem;
1029 free_parms.tbl_scope_id = iparms.tbl_scope_id;
1030 free_parms.flow_handle = iparms.flow_handle;
1032 trc = tf_delete_em_entry(tfp, &free_parms);
1034 BNXT_TF_DBG(ERR, "Failed to delete EM entry on failed add\n");
1040 ulp_mapper_index_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1041 struct bnxt_ulp_mapper_class_tbl_info *tbl)
1043 struct bnxt_ulp_mapper_result_field_info *flds;
1044 struct ulp_flow_db_res_params fid_parms;
1045 struct ulp_blob data;
1048 uint32_t i, num_flds;
1049 int32_t rc = 0, trc = 0;
1050 struct tf_alloc_tbl_entry_parms aparms = { 0 };
1051 struct tf_set_tbl_entry_parms sparms = { 0 };
1052 struct tf_free_tbl_entry_parms free_parms = { 0 };
1054 struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1056 if (!ulp_blob_init(&data, tbl->result_bit_size, parms->order)) {
1057 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
1061 flds = ulp_mapper_result_fields_get(tbl, &num_flds);
1062 if (!flds || !num_flds) {
1063 BNXT_TF_DBG(ERR, "Template undefined for action\n");
1067 for (i = 0; i < num_flds; i++) {
1068 rc = ulp_mapper_result_field_process(parms,
1072 BNXT_TF_DBG(ERR, "data field failed\n");
1077 aparms.dir = tbl->direction;
1078 aparms.type = tbl->table_type;
1079 aparms.search_enable = tbl->srch_b4_alloc;
1080 aparms.result = ulp_blob_data_get(&data, &tmplen);
1081 aparms.result_sz_in_bytes = ULP_SZ_BITS2BYTES(tbl->result_bit_size);
1083 /* All failures after the alloc succeeds require a free */
1084 rc = tf_alloc_tbl_entry(tfp, &aparms);
1086 BNXT_TF_DBG(ERR, "Alloc table[%d][%s] failed rc=%d\n",
1088 (tbl->direction == TF_DIR_RX) ? "RX" : "TX",
1093 /* Always storing values in Regfile in BE */
1094 idx = tfp_cpu_to_be_64(aparms.idx);
1095 rc = ulp_regfile_write(parms->regfile, tbl->regfile_wr_idx, idx);
1097 BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
1098 tbl->regfile_wr_idx);
1102 if (!tbl->srch_b4_alloc) {
1103 sparms.dir = tbl->direction;
1104 sparms.type = tbl->table_type;
1105 sparms.data = ulp_blob_data_get(&data, &tmplen);
1106 sparms.data_sz_in_bytes =
1107 ULP_SZ_BITS2BYTES(tbl->result_bit_size);
1108 sparms.idx = aparms.idx;
1110 rc = tf_set_tbl_entry(tfp, &sparms);
1112 BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
1114 (tbl->direction == TF_DIR_RX) ? "RX" : "TX",
1122 /* Link the resource to the flow in the flow db */
1123 memset(&fid_parms, 0, sizeof(fid_parms));
1124 fid_parms.direction = tbl->direction;
1125 fid_parms.resource_func = tbl->resource_func;
1126 fid_parms.resource_type = tbl->table_type;
1127 fid_parms.resource_hndl = aparms.idx;
1128 fid_parms.critical_resource = 0;
1130 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1135 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
1143 * Free the allocated resource since we failed to either
1144 * write to the entry or link the flow
1146 free_parms.dir = tbl->direction;
1147 free_parms.type = tbl->table_type;
1148 free_parms.idx = aparms.idx;
1150 trc = tf_free_tbl_entry(tfp, &free_parms);
1152 BNXT_TF_DBG(ERR, "Failed to free tbl entry on failure\n");
1158 * Function to process the action template. Iterate through the list
1159 * action info templates and process it.
1162 ulp_mapper_action_tbls_process(struct bnxt_ulp_mapper_parms *parms)
1167 if (!parms->atbls || !parms->num_atbls) {
1168 BNXT_TF_DBG(ERR, "No action tables for template[%d][%d].\n",
1169 parms->dev_id, parms->act_tid);
1173 for (i = 0; i < parms->num_atbls; i++) {
1174 rc = ulp_mapper_action_info_process(parms, &parms->atbls[i]);
1182 /* Create the classifier table entries for a flow. */
1184 ulp_mapper_class_tbls_process(struct bnxt_ulp_mapper_parms *parms)
1192 if (!parms->ctbls || !parms->num_ctbls) {
1193 BNXT_TF_DBG(ERR, "No class tables for template[%d][%d].\n",
1194 parms->dev_id, parms->class_tid);
1198 for (i = 0; i < parms->num_ctbls; i++) {
1199 struct bnxt_ulp_mapper_class_tbl_info *tbl = &parms->ctbls[i];
1201 switch (tbl->resource_func) {
1202 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
1203 rc = ulp_mapper_tcam_tbl_process(parms, tbl);
1205 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
1206 rc = ulp_mapper_em_tbl_process(parms, tbl);
1208 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
1209 rc = ulp_mapper_index_tbl_process(parms, tbl);
1212 BNXT_TF_DBG(ERR, "Unexpected class resource %d\n",
1213 tbl->resource_func);
1218 BNXT_TF_DBG(ERR, "Resource type %d failed\n",
1219 tbl->resource_func);
1228 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
1229 struct ulp_flow_db_res_params *res)
1235 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
1239 tfp = bnxt_ulp_cntxt_tfp_get(ulp);
1241 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
1245 switch (res->resource_func) {
1246 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
1247 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
1249 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
1250 rc = ulp_mapper_eem_entry_free(ulp, tfp, res);
1252 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
1253 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
1255 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
1256 rc = ulp_mapper_ident_free(ulp, tfp, res);
1258 case BNXT_ULP_RESOURCE_FUNC_HW_FID:
1259 rc = ulp_mapper_mark_free(ulp, res);
1269 ulp_mapper_resources_free(struct bnxt_ulp_context *ulp_ctx,
1271 enum bnxt_ulp_flow_db_tables tbl_type)
1273 struct ulp_flow_db_res_params res_parms = { 0 };
1277 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
1282 * Set the critical resource on the first resource del, then iterate
1283 * while status is good
1285 res_parms.critical_resource = 1;
1286 rc = ulp_flow_db_resource_del(ulp_ctx, tbl_type, fid, &res_parms);
1290 * This is unexpected on the first call to resource del.
1291 * It likely means that the flow did not exist in the flow db.
1293 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
1299 trc = ulp_mapper_resource_free(ulp_ctx, &res_parms);
1302 * On fail, we still need to attempt to free the
1303 * remaining resources. Don't return
1306 "Flow[%d][0x%x] Res[%d][0x%016" PRIx64
1307 "] failed rc=%d.\n",
1308 tbl_type, fid, res_parms.resource_func,
1309 res_parms.resource_hndl, trc);
1311 /* All subsequent call require the critical_resource be zero */
1312 res_parms.critical_resource = 0;
1314 rc = ulp_flow_db_resource_del(ulp_ctx,
1320 /* Free the Flow ID since we've removed all resources */
1321 rc = ulp_flow_db_fid_free(ulp_ctx, tbl_type, fid);
1327 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx, uint32_t fid)
1330 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
1334 return ulp_mapper_resources_free(ulp_ctx,
1336 BNXT_ULP_REGULAR_FLOW_TABLE);