net/bnxt: modify ULP mapper to use TCAM search
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_mapper.c
index 86858b8..2d3373d 100644 (file)
@@ -16,6 +16,7 @@
 #include "ulp_mark_mgr.h"
 #include "ulp_flow_db.h"
 #include "ulp_mapper.h"
+#include "tf_util.h"
 
 static struct bnxt_ulp_glb_resource_info *
 ulp_mapper_glb_resource_info_list_get(uint32_t *num_entries)
@@ -677,6 +678,98 @@ error:
        return rc;
 }
 
+/*
+ * Process the identifier instruction and extract it from result blob.
+ * Increment the identifier reference count and store it in the flow database.
+ */
+static int32_t
+ulp_mapper_ident_extract(struct bnxt_ulp_mapper_parms *parms,
+                        struct bnxt_ulp_mapper_tbl_info *tbl,
+                        struct bnxt_ulp_mapper_ident_info *ident,
+                        struct ulp_blob *res_blob)
+{
+       struct ulp_flow_db_res_params   fid_parms;
+       uint64_t id = 0;
+       uint32_t idx = 0;
+       struct tf_search_identifier_parms sparms = { 0 };
+       struct tf_free_identifier_parms free_parms = { 0 };
+       struct tf *tfp;
+       int rc;
+
+       /* Get the tfp from ulp context */
+       tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
+       if (!tfp) {
+               BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
+               return -EINVAL;
+       }
+
+       /* Extract the index from the result blob */
+       rc = ulp_blob_pull(res_blob, (uint8_t *)&idx, sizeof(idx),
+                          ident->ident_bit_pos, ident->ident_bit_size);
+       if (rc) {
+               BNXT_TF_DBG(ERR, "Failed to extract identifier from blob\n");
+               return -EIO;
+       }
+
+       /* populate the search params and search identifier shadow table */
+       sparms.ident_type = ident->ident_type;
+       sparms.dir = tbl->direction;
+       /* convert the idx into cpu format */
+       sparms.search_id = tfp_be_to_cpu_32(idx);
+
+       /* Search identifier also increase the reference count */
+       rc = tf_search_identifier(tfp, &sparms);
+       if (rc) {
+               BNXT_TF_DBG(ERR, "Search ident %s:%x failed.\n",
+                           tf_dir_2_str(sparms.dir),
+                           sparms.search_id);
+               return rc;
+       }
+       BNXT_TF_DBG(INFO, "Search ident %s:%x.success.\n",
+                   tf_dir_2_str(sparms.dir),
+                   sparms.search_id);
+
+       /* Write it to the regfile */
+       id = (uint64_t)tfp_cpu_to_be_64(sparms.search_id);
+       if (!ulp_regfile_write(parms->regfile, ident->regfile_idx, id)) {
+               BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
+               rc = -EINVAL;
+               /* Need to free the identifier, so goto error */
+               goto error;
+       }
+
+       /* Link the resource to the flow in the flow db */
+       memset(&fid_parms, 0, sizeof(fid_parms));
+       fid_parms.direction = tbl->direction;
+       fid_parms.resource_func = ident->resource_func;
+       fid_parms.resource_type = ident->ident_type;
+       fid_parms.resource_hndl = sparms.search_id;
+       fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
+       rc = ulp_flow_db_resource_add(parms->ulp_ctx,
+                                     parms->tbl_idx,
+                                     parms->fid,
+                                     &fid_parms);
+       if (rc) {
+               BNXT_TF_DBG(ERR, "Failed to link res to flow rc = %d\n",
+                           rc);
+               /* Need to free the identifier, so goto error */
+               goto error;
+       }
+
+       return 0;
+
+error:
+       /* Need to free the identifier */
+       free_parms.dir = tbl->direction;
+       free_parms.ident_type = ident->ident_type;
+       free_parms.id = sparms.search_id;
+       (void)tf_free_identifier(tfp, &free_parms);
+       BNXT_TF_DBG(ERR, "Ident extract failed for %s:%s:%x\n",
+                   ident->description,
+                   tf_dir_2_str(tbl->direction), sparms.search_id);
+       return rc;
+}
+
 static int32_t
 ulp_mapper_result_field_process(struct bnxt_ulp_mapper_parms *parms,
                                enum tf_dir dir,
@@ -1199,11 +1292,13 @@ ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
        struct tf *tfp;
        int32_t rc, trc;
        struct tf_alloc_tcam_entry_parms aparms         = { 0 };
+       struct tf_search_tcam_entry_parms searchparms   = { 0 };
        struct tf_set_tcam_entry_parms sparms           = { 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;
 
        /* Skip this if was handled by the cache. */
        if (parms->tcam_tbl_opc == BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_SKIP) {
@@ -1258,37 +1353,72 @@ ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
                }
        }
 
-       aparms.dir              = tbl->direction;
-       aparms.tcam_tbl_type    = tbl->resource_type;
-       aparms.search_enable    = tbl->srch_b4_alloc;
-       aparms.key_sz_in_bits   = tbl->key_bit_size;
-       aparms.key              = ulp_blob_data_get(&key, &tmplen);
-       if (tbl->key_bit_size != tmplen) {
-               BNXT_TF_DBG(ERR, "Key len (%d) != Expected (%d)\n",
-                           tmplen, tbl->key_bit_size);
-               return -EINVAL;
-       }
+       if (!tbl->srch_b4_alloc) {
+               /*
+                * No search for re-use is requested, so simply allocate the
+                * tcam index.
+                */
+               aparms.dir              = tbl->direction;
+               aparms.tcam_tbl_type    = tbl->resource_type;
+               aparms.search_enable    = tbl->srch_b4_alloc;
+               aparms.key_sz_in_bits   = tbl->key_bit_size;
+               aparms.key              = ulp_blob_data_get(&key, &tmplen);
+               if (tbl->key_bit_size != tmplen) {
+                       BNXT_TF_DBG(ERR, "Key len (%d) != Expected (%d)\n",
+                                   tmplen, tbl->key_bit_size);
+                       return -EINVAL;
+               }
 
-       aparms.mask             = ulp_blob_data_get(&mask, &tmplen);
-       if (tbl->key_bit_size != tmplen) {
-               BNXT_TF_DBG(ERR, "Mask len (%d) != Expected (%d)\n",
-                           tmplen, tbl->key_bit_size);
-               return -EINVAL;
-       }
+               aparms.mask             = ulp_blob_data_get(&mask, &tmplen);
+               if (tbl->key_bit_size != tmplen) {
+                       BNXT_TF_DBG(ERR, "Mask len (%d) != Expected (%d)\n",
+                                   tmplen, tbl->key_bit_size);
+                       return -EINVAL;
+               }
 
-       aparms.priority         = tbl->priority;
+               aparms.priority         = tbl->priority;
 
-       /*
-        * All failures after this succeeds require the entry to be freed.
-        * cannot return directly on failure, but needs to goto error
-        */
-       rc = tf_alloc_tcam_entry(tfp, &aparms);
-       if (rc) {
-               BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
-               return rc;
-       }
+               /*
+                * All failures after this succeeds require the entry to be
+                * freed. cannot return directly on failure, but needs to goto
+                * error.
+                */
+               rc = tf_alloc_tcam_entry(tfp, &aparms);
+               if (rc) {
+                       BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
+                       return rc;
+               }
+               idx = aparms.idx;
+               hit = aparms.hit;
+       } else {
+               /*
+                * Searching before allocation to see if we already have an
+                * entry.  This allows re-use of a constrained resource.
+                */
+               searchparms.dir = tbl->direction;
+               searchparms.tcam_tbl_type = tbl->resource_type;
+               searchparms.key = ulp_blob_data_get(&key, &tmplen);
+               searchparms.key_sz_in_bits = tbl->key_bit_size;
+               searchparms.mask = ulp_blob_data_get(&mask, &tmplen);
+               searchparms.priority = tbl->priority;
+               searchparms.alloc = 1;
+               searchparms.result = ulp_blob_data_get(&data, &tmplen);
+               searchparms.result_sz_in_bits = tbl->result_bit_size;
+
+               rc = tf_search_tcam_entry(tfp, &searchparms);
+               if (rc) {
+                       BNXT_TF_DBG(ERR, "tcam search failed rc=%d\n", rc);
+                       return rc;
+               }
 
-       hit = aparms.hit;
+               /* Successful search, check the result */
+               if (searchparms.search_status == REJECT) {
+                       BNXT_TF_DBG(ERR, "tcam alloc rejected\n");
+                       return -ENOMEM;
+               }
+               idx = searchparms.idx;
+               hit = searchparms.hit;
+       }
 
        /* Build the result */
        if (!tbl->srch_b4_alloc || !hit) {
@@ -1336,9 +1466,9 @@ ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
                        }
                }
 
-               sparms.dir              = aparms.dir;
-               sparms.tcam_tbl_type    = aparms.tcam_tbl_type;
-               sparms.idx              = aparms.idx;
+               sparms.dir              = tbl->direction;
+               sparms.tcam_tbl_type    = tbl->resource_type;
+               sparms.idx              = idx;
                /* Already verified the key/mask lengths */
                sparms.key              = ulp_blob_data_get(&key, &tmplen);
                sparms.mask             = ulp_blob_data_get(&mask, &tmplen);
@@ -1370,7 +1500,7 @@ ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
                                rc = -EINVAL;
                                goto error;
                        }
-                       parms->cache_ptr->tcam_idx = aparms.idx;
+                       parms->cache_ptr->tcam_idx = idx;
                }
 
                /* Mark action */
@@ -1379,9 +1509,23 @@ ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
                        goto error;
 
        } else {
-               BNXT_TF_DBG(ERR, "Not supporting search before alloc now\n");
-               rc = -EINVAL;
-               goto error;
+               struct bnxt_ulp_mapper_ident_info *idents;
+               uint32_t num_idents;
+
+               /*
+                * Extract the listed identifiers from the result field,
+                * no need to allocate them.
+                */
+               idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
+               for (i = 0; i < num_idents; i++) {
+                       rc = ulp_mapper_ident_extract(parms, tbl,
+                                                     &idents[i], &data);
+                       if (rc) {
+                               BNXT_TF_DBG(ERR,
+                                           "Error in ident extraction\n");
+                               goto error;
+                       }
+               }
        }
 
        /*
@@ -1393,7 +1537,7 @@ ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
                fid_parms.resource_func = tbl->resource_func;
                fid_parms.resource_type = tbl->resource_type;
                fid_parms.critical_resource = tbl->critical_resource;
-               fid_parms.resource_hndl = aparms.idx;
+               fid_parms.resource_hndl = idx;
                rc = ulp_flow_db_resource_add(parms->ulp_ctx,
                                              parms->tbl_idx,
                                              parms->fid,