net/bnxt: support count action in flow query
[dpdk.git] / drivers / net / bnxt / tf_ulp / bnxt_ulp_flow.c
index 6402dd3..36a0141 100644 (file)
@@ -9,6 +9,7 @@
 #include "ulp_matcher.h"
 #include "ulp_flow_db.h"
 #include "ulp_mapper.h"
+#include "ulp_fc_mgr.h"
 #include <rte_malloc.h>
 
 static int32_t
@@ -61,25 +62,19 @@ bnxt_ulp_flow_validate_args(const struct rte_flow_attr *attr,
 
 /* Function to create the rte flow. */
 static struct rte_flow *
-bnxt_ulp_flow_create(struct rte_eth_dev                        *dev,
-                    const struct rte_flow_attr         *attr,
-                    const struct rte_flow_item         pattern[],
-                    const struct rte_flow_action       actions[],
-                    struct rte_flow_error              *error)
+bnxt_ulp_flow_create(struct rte_eth_dev *dev,
+                    const struct rte_flow_attr *attr,
+                    const struct rte_flow_item pattern[],
+                    const struct rte_flow_action actions[],
+                    struct rte_flow_error *error)
 {
-       struct ulp_rte_hdr_bitmap hdr_bitmap;
-       struct ulp_rte_hdr_field hdr_field[BNXT_ULP_PROTO_HDR_MAX];
-       struct ulp_rte_act_bitmap act_bitmap;
-       struct ulp_rte_act_prop act_prop;
-       enum ulp_direction_type dir = ULP_DIR_INGRESS;
+       struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
+       struct ulp_rte_parser_params params;
+       struct bnxt_ulp_context *ulp_ctx;
        uint32_t class_id, act_tmpl;
-       uint32_t app_priority;
-       int ret;
-       struct bnxt_ulp_context *ulp_ctx = NULL;
-       uint32_t vnic;
-       uint8_t svif;
        struct rte_flow *flow_id;
        uint32_t fid;
+       int ret;
 
        if (bnxt_ulp_flow_validate_args(attr,
                                        pattern, actions,
@@ -94,68 +89,52 @@ bnxt_ulp_flow_create(struct rte_eth_dev                     *dev,
                return NULL;
        }
 
-       /* clear the header bitmap and field structure */
-       memset(&hdr_bitmap, 0, sizeof(struct ulp_rte_hdr_bitmap));
-       memset(hdr_field, 0, sizeof(hdr_field));
-       memset(&act_bitmap, 0, sizeof(act_bitmap));
-       memset(&act_prop, 0, sizeof(act_prop));
-
-       svif = bnxt_get_svif(dev->data->port_id, false);
-       BNXT_TF_DBG(ERR, "SVIF for port[%d][port]=0x%08x\n",
-                   dev->data->port_id, svif);
+       /* Initialize the parser params */
+       memset(&params, 0, sizeof(struct ulp_rte_parser_params));
+       params.ulp_ctx = ulp_ctx;
 
-       hdr_field[BNXT_ULP_HDR_FIELD_SVIF_INDEX].size = sizeof(svif);
-       hdr_field[BNXT_ULP_HDR_FIELD_SVIF_INDEX].spec[0] = svif;
-       hdr_field[BNXT_ULP_HDR_FIELD_SVIF_INDEX].mask[0] = -1;
-       ULP_BITMAP_SET(hdr_bitmap.bits, BNXT_ULP_HDR_BIT_SVIF);
+       if (attr->egress)
+               params.dir = ULP_DIR_EGRESS;
 
-       /*
-        * VNIC is being pushed as 32bit and the pop will take care of
-        * proper size
-        */
-       vnic = (uint32_t)bnxt_get_vnic_id(dev->data->port_id);
-       vnic = htonl(vnic);
-       rte_memcpy(&act_prop.act_details[BNXT_ULP_ACT_PROP_IDX_VNIC],
-                  &vnic, BNXT_ULP_ACT_PROP_SZ_VNIC);
+       /* copy the device port id and direction for further processing */
+       ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_INCOMING_IF,
+                           dev->data->port_id);
+       ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_DIRECTION, params.dir);
+       ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_SVIF_FLAG,
+                           BNXT_ULP_INVALID_SVIF_VAL);
 
        /* Parse the rte flow pattern */
-       ret = bnxt_ulp_rte_parser_hdr_parse(pattern,
-                                           &hdr_bitmap,
-                                           hdr_field);
+       ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
        if (ret != BNXT_TF_RC_SUCCESS)
                goto parse_error;
 
        /* Parse the rte flow action */
-       ret = bnxt_ulp_rte_parser_act_parse(actions,
-                                           &act_bitmap,
-                                           &act_prop);
+       ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
        if (ret != BNXT_TF_RC_SUCCESS)
                goto parse_error;
 
-       if (attr->egress)
-               dir = ULP_DIR_EGRESS;
-
-       ret = ulp_matcher_pattern_match(dir, &hdr_bitmap, hdr_field,
-                                       &act_bitmap, &class_id);
-
+       ret = ulp_matcher_pattern_match(&params, &class_id);
        if (ret != BNXT_TF_RC_SUCCESS)
                goto parse_error;
 
-       ret = ulp_matcher_action_match(dir, &act_bitmap, &act_tmpl);
+       ret = ulp_matcher_action_match(&params, &act_tmpl);
        if (ret != BNXT_TF_RC_SUCCESS)
                goto parse_error;
 
-       app_priority = attr->priority;
-       /* call the ulp mapper to create the flow in the hardware */
-       ret = ulp_mapper_flow_create(ulp_ctx,
-                                    app_priority,
-                                    &hdr_bitmap,
-                                    hdr_field,
-                                    &act_bitmap,
-                                    &act_prop,
-                                    class_id,
-                                    act_tmpl,
-                                    &fid);
+       mapper_cparms.app_priority = attr->priority;
+       mapper_cparms.hdr_bitmap = &params.hdr_bitmap;
+       mapper_cparms.hdr_field = params.hdr_field;
+       mapper_cparms.comp_fld = params.comp_fld;
+       mapper_cparms.act = &params.act_bitmap;
+       mapper_cparms.act_prop = &params.act_prop;
+       mapper_cparms.class_tid = class_id;
+       mapper_cparms.act_tid = act_tmpl;
+       mapper_cparms.func_id = bnxt_get_fw_func_id(dev->data->port_id,
+                                                   BNXT_ULP_INTF_TYPE_INVALID);
+       mapper_cparms.dir = params.dir;
+
+       /* Call the ulp mapper to create the flow in the hardware. */
+       ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms, &fid);
        if (!ret) {
                flow_id = (struct rte_flow *)((uintptr_t)fid);
                return flow_id;
@@ -167,11 +146,197 @@ parse_error:
        return NULL;
 }
 
+/* Function to validate the rte flow. */
+static int
+bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
+                      const struct rte_flow_attr *attr,
+                      const struct rte_flow_item pattern[],
+                      const struct rte_flow_action actions[],
+                      struct rte_flow_error *error)
+{
+       struct ulp_rte_parser_params            params;
+       uint32_t class_id, act_tmpl;
+       int ret;
+       struct bnxt_ulp_context *ulp_ctx;
+
+       if (bnxt_ulp_flow_validate_args(attr,
+                                       pattern, actions,
+                                       error) == BNXT_TF_RC_ERROR) {
+               BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
+               return -EINVAL;
+       }
+
+       ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
+       if (!ulp_ctx) {
+               BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
+               return -EINVAL;
+       }
+
+       /* Initialize the parser params */
+       memset(&params, 0, sizeof(struct ulp_rte_parser_params));
+       params.ulp_ctx = ulp_ctx;
+
+       if (attr->egress)
+               params.dir = ULP_DIR_EGRESS;
+
+       /* Parse the rte flow pattern */
+       ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
+       if (ret != BNXT_TF_RC_SUCCESS)
+               goto parse_error;
+
+       /* Parse the rte flow action */
+       ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
+       if (ret != BNXT_TF_RC_SUCCESS)
+               goto parse_error;
+
+       ret = ulp_matcher_pattern_match(&params, &class_id);
+
+       if (ret != BNXT_TF_RC_SUCCESS)
+               goto parse_error;
+
+       ret = ulp_matcher_action_match(&params, &act_tmpl);
+       if (ret != BNXT_TF_RC_SUCCESS)
+               goto parse_error;
+
+       /* all good return success */
+       return ret;
+
+parse_error:
+       rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
+                          "Failed to validate flow.");
+       return -EINVAL;
+}
+
+/* Function to destroy the rte flow. */
+int
+bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
+                     struct rte_flow *flow,
+                     struct rte_flow_error *error)
+{
+       int ret = 0;
+       struct bnxt_ulp_context *ulp_ctx;
+       uint32_t flow_id;
+       uint16_t func_id;
+
+       ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
+       if (!ulp_ctx) {
+               BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
+               if (error)
+                       rte_flow_error_set(error, EINVAL,
+                                          RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
+                                          "Failed to destroy flow.");
+               return -EINVAL;
+       }
+
+       flow_id = (uint32_t)(uintptr_t)flow;
+       func_id = bnxt_get_fw_func_id(dev->data->port_id,
+                                     BNXT_ULP_INTF_TYPE_INVALID);
+
+       if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
+           false) {
+               BNXT_TF_DBG(ERR, "Incorrect device params\n");
+               if (error)
+                       rte_flow_error_set(error, EINVAL,
+                                          RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
+                                          "Failed to destroy flow.");
+               return -EINVAL;
+       }
+
+       ret = ulp_mapper_flow_destroy(ulp_ctx, flow_id,
+                                     BNXT_ULP_REGULAR_FLOW_TABLE);
+       if (ret) {
+               BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
+               if (error)
+                       rte_flow_error_set(error, -ret,
+                                          RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
+                                          "Failed to destroy flow.");
+       }
+
+       return ret;
+}
+
+/* Function to destroy the rte flows. */
+static int32_t
+bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
+                   struct rte_flow_error *error)
+{
+       struct bnxt_ulp_context *ulp_ctx;
+       int32_t ret = 0;
+       struct bnxt *bp;
+       uint16_t func_id;
+
+       ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
+       if (!ulp_ctx) {
+               BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
+               rte_flow_error_set(error, EINVAL,
+                                  RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
+                                  "Failed to flush flow.");
+               return -EINVAL;
+       }
+       bp = eth_dev->data->dev_private;
+
+       /* Free the resources for the last device */
+       if (ulp_ctx_deinit_allowed(bp)) {
+               ret = ulp_flow_db_session_flow_flush(ulp_ctx);
+       } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
+               func_id = bnxt_get_fw_func_id(eth_dev->data->port_id,
+                                             BNXT_ULP_INTF_TYPE_INVALID);
+               ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
+       }
+       if (ret)
+               rte_flow_error_set(error, ret,
+                                  RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
+                                  "Failed to flush flow.");
+       return ret;
+}
+
+/* Function to query the rte flows. */
+static int32_t
+bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
+                   struct rte_flow *flow,
+                   const struct rte_flow_action *action,
+                   void *data,
+                   struct rte_flow_error *error)
+{
+       int rc = 0;
+       struct bnxt_ulp_context *ulp_ctx;
+       struct rte_flow_query_count *count;
+       uint32_t flow_id;
+
+       ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
+       if (!ulp_ctx) {
+               BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
+               rte_flow_error_set(error, EINVAL,
+                                  RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
+                                  "Failed to query flow.");
+               return -EINVAL;
+       }
+
+       flow_id = (uint32_t)(uintptr_t)flow;
+
+       switch (action->type) {
+       case RTE_FLOW_ACTION_TYPE_COUNT:
+               count = data;
+               rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
+               if (rc) {
+                       rte_flow_error_set(error, EINVAL,
+                                          RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
+                                          "Failed to query flow.");
+               }
+               break;
+       default:
+               rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
+                                  NULL, "Unsupported action item");
+       }
+
+       return rc;
+}
+
 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
-       .validate = NULL,
+       .validate = bnxt_ulp_flow_validate,
        .create = bnxt_ulp_flow_create,
-       .destroy = NULL,
-       .flush = NULL,
-       .query = NULL,
+       .destroy = bnxt_ulp_flow_destroy,
+       .flush = bnxt_ulp_flow_flush,
+       .query = bnxt_ulp_flow_query,
        .isolate = NULL
 };