+/*
+ * internal function to post process key/mask blobs for dynamic pad WC tcam tbl
+ *
+ * parms [in] The mappers parms with data related to the flow.
+ *
+ * key [in] The original key to be transformed
+ *
+ * mask [in] The original mask to be transformed
+ *
+ * tkey [in/out] The transformed key
+ *
+ * tmask [in/out] The transformed mask
+ *
+ * returns zero on success, non-zero on failure
+ */
+static uint32_t
+ulp_mapper_wc_tcam_tbl_dyn_post_process(struct bnxt_ulp_device_params *dparms,
+ struct ulp_blob *key,
+ struct ulp_blob *mask,
+ struct ulp_blob *tkey,
+ struct ulp_blob *tmask)
+{
+ uint16_t tlen, blen, clen, slice_width, num_slices, max_slices, offset;
+ uint32_t cword, i, rc;
+ int32_t pad;
+ uint8_t *val;
+
+ slice_width = dparms->wc_slice_width;
+ clen = dparms->wc_ctl_size_bits;
+ max_slices = dparms->wc_max_slices;
+ blen = ulp_blob_data_len_get(key);
+
+ /* Get the length of the key based on number of slices and width */
+ num_slices = 1;
+ tlen = slice_width;
+ while (tlen < blen &&
+ num_slices <= max_slices) {
+ num_slices = num_slices << 1;
+ tlen = tlen << 1;
+ }
+
+ if (num_slices > max_slices) {
+ BNXT_TF_DBG(ERR, "Key size (%d) too large for WC\n", blen);
+ return -EINVAL;
+ }
+
+ /* The key/mask may not be on a natural slice boundary, pad it */
+ pad = tlen - blen;
+ if (ulp_blob_pad_push(key, pad) < 0 ||
+ ulp_blob_pad_push(mask, pad) < 0) {
+ BNXT_TF_DBG(ERR, "Unable to pad key/mask\n");
+ return -EINVAL;
+ }
+
+ /* The new length accounts for the ctrl word length and num slices */
+ tlen = tlen + clen * num_slices;
+ if (!ulp_blob_init(tkey, tlen, key->byte_order) ||
+ !ulp_blob_init(tmask, tlen, mask->byte_order)) {
+ BNXT_TF_DBG(ERR, "Unable to post process wc tcam entry\n");
+ return -EINVAL;
+ }
+
+ /* Build the transformed key/mask */
+ cword = dparms->wc_mode_list[num_slices - 1];
+ cword = tfp_cpu_to_be_32(cword);
+ offset = 0;
+ for (i = 0; i < num_slices; i++) {
+ val = ulp_blob_push_32(tkey, &cword, clen);
+ if (!val) {
+ BNXT_TF_DBG(ERR, "Key ctrl word push failed\n");
+ return -EINVAL;
+ }
+ val = ulp_blob_push_32(tmask, &cword, clen);
+ if (!val) {
+ BNXT_TF_DBG(ERR, "Mask ctrl word push failed\n");
+ return -EINVAL;
+ }
+ rc = ulp_blob_append(tkey, key, offset, slice_width);
+ if (rc) {
+ BNXT_TF_DBG(ERR, "Key blob append failed\n");
+ return rc;
+ }
+ rc = ulp_blob_append(tmask, mask, offset, slice_width);
+ if (rc) {
+ BNXT_TF_DBG(ERR, "Mask blob append failed\n");
+ return rc;
+ }
+ offset += slice_width;
+ }
+
+ /* The key/mask are byte reversed on every 4 byte chunk */
+ ulp_blob_perform_byte_reverse(tkey, 4);
+ ulp_blob_perform_byte_reverse(tmask, 4);
+
+ return 0;
+}
+
+/* internal function to post process the key/mask blobs for wildcard tcam tbl */
+static void ulp_mapper_wc_tcam_tbl_post_process(struct ulp_blob *blob)
+{
+ ulp_blob_perform_64B_word_swap(blob);
+ ulp_blob_perform_64B_byte_swap(blob);
+#ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
+#ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
+ BNXT_TF_DBG(INFO, "Dump after wc tcam post process\n");
+ ulp_mapper_blob_dump(blob);
+#endif
+#endif
+}
+
+static int32_t ulp_mapper_tcam_is_wc_tcam(struct bnxt_ulp_mapper_tbl_info *tbl)
+{
+ if (tbl->resource_type == TF_TCAM_TBL_TYPE_WC_TCAM ||
+ tbl->resource_type == TF_TCAM_TBL_TYPE_WC_TCAM_HIGH ||
+ tbl->resource_type == TF_TCAM_TBL_TYPE_WC_TCAM_LOW)
+ return 1;
+ return 0;
+}
+
+static int32_t
+ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
+ struct bnxt_ulp_mapper_tbl_info *tbl)
+{
+ struct bnxt_ulp_mapper_key_info *kflds;
+ struct ulp_blob okey, omask, data, update_data;
+ struct ulp_blob tkey, tmask; /* transform key and mask */
+ struct ulp_blob *key, *mask;
+ uint32_t i, num_kflds;
+ struct tf *tfp;
+ int32_t rc, trc;
+ struct bnxt_ulp_device_params *dparms = parms->device_params;
+ struct tf_alloc_tcam_entry_parms aparms = { 0 };
+ struct tf_search_tcam_entry_parms searchparms = { 0 };
+ struct ulp_flow_db_res_params fid_parms = { 0 };
+ struct tf_free_tcam_entry_parms free_parms = { 0 };
+ uint32_t hit = 0;
+ uint16_t tmplen = 0;
+ uint16_t idx;
+ enum bnxt_ulp_byte_order key_byte_order;
+
+ /* Set the key and mask to the original key and mask. */
+ key = &okey;
+ mask = &omask;
+
+ /* Skip this if table opcode is NOP */
+ if (tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_NOT_USED ||
+ tbl->tbl_opcode >= BNXT_ULP_TCAM_TBL_OPC_LAST) {
+ BNXT_TF_DBG(ERR, "Invalid tcam table opcode %d\n",
+ tbl->tbl_opcode);
+ return 0;
+ }
+
+ tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
+ if (!tfp) {
+ BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
+ return -EINVAL;
+ }
+
+ /* If only allocation of identifier then perform and exit */
+ if (tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_ALLOC_IDENT) {
+ rc = ulp_mapper_tcam_tbl_scan_ident_alloc(parms, tbl);
+ return rc;
+ }
+
+ kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
+ if (!kflds || !num_kflds) {
+ BNXT_TF_DBG(ERR, "Failed to get key fields\n");
+ return -EINVAL;
+ }
+
+ if (ulp_mapper_tcam_is_wc_tcam(tbl))
+ key_byte_order = dparms->wc_key_byte_order;
+ else
+ key_byte_order = dparms->key_byte_order;
+
+ if (!ulp_blob_init(key, tbl->blob_key_bit_size, key_byte_order) ||
+ !ulp_blob_init(mask, tbl->blob_key_bit_size, key_byte_order) ||
+ !ulp_blob_init(&data, tbl->result_bit_size,
+ dparms->result_byte_order) ||
+ !ulp_blob_init(&update_data, tbl->result_bit_size,
+ dparms->result_byte_order)) {
+ BNXT_TF_DBG(ERR, "blob inits failed.\n");
+ return -EINVAL;
+ }
+
+ /* create the key/mask */
+ /*
+ * NOTE: The WC table will require some kind of flag to handle the
+ * mode bits within the key/mask
+ */
+ for (i = 0; i < num_kflds; i++) {
+ /* Setup the key */
+ rc = ulp_mapper_field_opc_process(parms, tbl->direction,
+ &kflds[i].field_info_spec,
+ key, 1, "TCAM Key");
+ if (rc) {
+ BNXT_TF_DBG(ERR, "Key field set failed %s\n",
+ kflds[i].field_info_spec.description);
+ return rc;
+ }
+
+ /* Setup the mask */
+ rc = ulp_mapper_field_opc_process(parms, tbl->direction,
+ &kflds[i].field_info_mask,
+ mask, 0, "TCAM Mask");
+ if (rc) {
+ BNXT_TF_DBG(ERR, "Mask field set failed %s\n",
+ kflds[i].field_info_mask.description);
+ return rc;
+ }
+ }
+
+ /* For wild card tcam perform the post process to swap the blob */
+ if (ulp_mapper_tcam_is_wc_tcam(tbl)) {
+ if (dparms->dynamic_pad_en) {
+ /* Sets up the slices for writing to the WC TCAM */
+ rc = ulp_mapper_wc_tcam_tbl_dyn_post_process(dparms,
+ key, mask,
+ &tkey,
+ &tmask);
+ if (rc) {
+ BNXT_TF_DBG(ERR,
+ "Failed to post proc WC entry.\n");
+ return rc;
+ }
+ /* Now need to use the transform Key/Mask */
+ key = &tkey;
+ mask = &tmask;
+ } else {
+ ulp_mapper_wc_tcam_tbl_post_process(key);
+ ulp_mapper_wc_tcam_tbl_post_process(mask);
+ }
+
+ }
+
+ if (tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_ALLOC_WR_REGFILE) {
+ /* allocate the tcam index */
+ aparms.dir = tbl->direction;
+ aparms.tcam_tbl_type = tbl->resource_type;
+ aparms.key = ulp_blob_data_get(key, &tmplen);
+ aparms.key_sz_in_bits = tmplen;
+ aparms.mask = ulp_blob_data_get(mask, &tmplen);
+
+ /* calculate the entry priority */
+ rc = ulp_mapper_priority_opc_process(parms, tbl,
+ &aparms.priority);
+ if (rc) {
+ BNXT_TF_DBG(ERR, "entry priority process failed\n");
+ return rc;
+ }
+
+ rc = tf_alloc_tcam_entry(tfp, &aparms);
+ if (rc) {