7f7aa24e6561986d50383a34e023458216215c76
[dpdk.git] / drivers / net / bnxt / tf_ulp / bnxt_ulp_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #include "bnxt.h"
7 #include "bnxt_tf_common.h"
8 #include "ulp_rte_parser.h"
9 #include "ulp_matcher.h"
10 #include "ulp_flow_db.h"
11 #include "ulp_mapper.h"
12 #include <rte_malloc.h>
13
14 static int32_t
15 bnxt_ulp_flow_validate_args(const struct rte_flow_attr *attr,
16                             const struct rte_flow_item pattern[],
17                             const struct rte_flow_action actions[],
18                             struct rte_flow_error *error)
19 {
20         /* Perform the validation of the arguments for null */
21         if (!error)
22                 return BNXT_TF_RC_ERROR;
23
24         if (!pattern) {
25                 rte_flow_error_set(error,
26                                    EINVAL,
27                                    RTE_FLOW_ERROR_TYPE_ITEM_NUM,
28                                    NULL,
29                                    "NULL pattern.");
30                 return BNXT_TF_RC_ERROR;
31         }
32
33         if (!actions) {
34                 rte_flow_error_set(error,
35                                    EINVAL,
36                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
37                                    NULL,
38                                    "NULL action.");
39                 return BNXT_TF_RC_ERROR;
40         }
41
42         if (!attr) {
43                 rte_flow_error_set(error,
44                                    EINVAL,
45                                    RTE_FLOW_ERROR_TYPE_ATTR,
46                                    NULL,
47                                    "NULL attribute.");
48                 return BNXT_TF_RC_ERROR;
49         }
50
51         if (attr->egress && attr->ingress) {
52                 rte_flow_error_set(error,
53                                    EINVAL,
54                                    RTE_FLOW_ERROR_TYPE_ATTR,
55                                    attr,
56                                    "EGRESS AND INGRESS UNSUPPORTED");
57                 return BNXT_TF_RC_ERROR;
58         }
59         return BNXT_TF_RC_SUCCESS;
60 }
61
62 /* Function to create the rte flow. */
63 static struct rte_flow *
64 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
65                      const struct rte_flow_attr *attr,
66                      const struct rte_flow_item pattern[],
67                      const struct rte_flow_action actions[],
68                      struct rte_flow_error *error)
69 {
70         struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
71         struct ulp_rte_parser_params params;
72         struct bnxt_ulp_context *ulp_ctx = NULL;
73         uint32_t class_id, act_tmpl;
74         struct rte_flow *flow_id;
75         uint32_t fid;
76         int ret;
77
78         if (bnxt_ulp_flow_validate_args(attr,
79                                         pattern, actions,
80                                         error) == BNXT_TF_RC_ERROR) {
81                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
82                 return NULL;
83         }
84
85         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
86         if (!ulp_ctx) {
87                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
88                 return NULL;
89         }
90
91         /* Initialize the parser params */
92         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
93
94         if (attr->egress)
95                 params.dir = ULP_DIR_EGRESS;
96
97         /* copy the device port id and direction for further processing */
98         ULP_UTIL_CHF_IDX_WR(&params, BNXT_ULP_CHF_IDX_INCOMING_IF,
99                             dev->data->port_id);
100         ULP_UTIL_CHF_IDX_WR(&params, BNXT_ULP_CHF_IDX_DIRECTION, params.dir);
101
102         /* Parse the rte flow pattern */
103         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
104         if (ret != BNXT_TF_RC_SUCCESS)
105                 goto parse_error;
106
107         /* Parse the rte flow action */
108         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
109         if (ret != BNXT_TF_RC_SUCCESS)
110                 goto parse_error;
111
112         ret = ulp_matcher_pattern_match(&params, &class_id);
113
114         if (ret != BNXT_TF_RC_SUCCESS)
115                 goto parse_error;
116
117         ret = ulp_matcher_action_match(&params, &act_tmpl);
118         if (ret != BNXT_TF_RC_SUCCESS)
119                 goto parse_error;
120
121         mapper_cparms.app_priority = attr->priority;
122         mapper_cparms.hdr_bitmap = &params.hdr_bitmap;
123         mapper_cparms.hdr_field = params.hdr_field;
124         mapper_cparms.act = &params.act_bitmap;
125         mapper_cparms.act_prop = &params.act_prop;
126         mapper_cparms.class_tid = class_id;
127         mapper_cparms.act_tid = act_tmpl;
128
129         /* call the ulp mapper to create the flow in the hardware */
130         ret = ulp_mapper_flow_create(ulp_ctx,
131                                      &mapper_cparms,
132                                      &fid);
133         if (!ret) {
134                 flow_id = (struct rte_flow *)((uintptr_t)fid);
135                 return flow_id;
136         }
137
138 parse_error:
139         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
140                            "Failed to create flow.");
141         return NULL;
142 }
143
144 /* Function to validate the rte flow. */
145 static int
146 bnxt_ulp_flow_validate(struct rte_eth_dev *dev __rte_unused,
147                        const struct rte_flow_attr *attr,
148                        const struct rte_flow_item pattern[],
149                        const struct rte_flow_action actions[],
150                        struct rte_flow_error *error)
151 {
152         struct ulp_rte_parser_params            params;
153         uint32_t class_id, act_tmpl;
154         int ret;
155
156         if (bnxt_ulp_flow_validate_args(attr,
157                                         pattern, actions,
158                                         error) == BNXT_TF_RC_ERROR) {
159                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
160                 return -EINVAL;
161         }
162
163         /* Initialize the parser params */
164         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
165
166         if (attr->egress)
167                 params.dir = ULP_DIR_EGRESS;
168
169         /* Parse the rte flow pattern */
170         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
171         if (ret != BNXT_TF_RC_SUCCESS)
172                 goto parse_error;
173
174         /* Parse the rte flow action */
175         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
176         if (ret != BNXT_TF_RC_SUCCESS)
177                 goto parse_error;
178
179         ret = ulp_matcher_pattern_match(&params, &class_id);
180
181         if (ret != BNXT_TF_RC_SUCCESS)
182                 goto parse_error;
183
184         ret = ulp_matcher_action_match(&params, &act_tmpl);
185         if (ret != BNXT_TF_RC_SUCCESS)
186                 goto parse_error;
187
188         /* all good return success */
189         return ret;
190
191 parse_error:
192         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
193                            "Failed to validate flow.");
194         return -EINVAL;
195 }
196
197 /* Function to destroy the rte flow. */
198 static int
199 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
200                       struct rte_flow *flow,
201                       struct rte_flow_error *error)
202 {
203         int ret = 0;
204         struct bnxt_ulp_context *ulp_ctx;
205         uint32_t fid;
206
207         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
208         if (!ulp_ctx) {
209                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
210                 rte_flow_error_set(error, EINVAL,
211                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
212                                    "Failed to destroy flow.");
213                 return -EINVAL;
214         }
215
216         fid = (uint32_t)(uintptr_t)flow;
217
218         ret = ulp_mapper_flow_destroy(ulp_ctx, fid);
219         if (ret)
220                 rte_flow_error_set(error, -ret,
221                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
222                                    "Failed to destroy flow.");
223
224         return ret;
225 }
226
227 /* Function to destroy the rte flows. */
228 static int32_t
229 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
230                     struct rte_flow_error *error)
231 {
232         struct bnxt_ulp_context *ulp_ctx;
233         int32_t ret;
234         struct bnxt *bp;
235
236         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
237         if (!ulp_ctx) {
238                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
239                 rte_flow_error_set(error, EINVAL,
240                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
241                                    "Failed to flush flow.");
242                 return -EINVAL;
243         }
244         bp = eth_dev->data->dev_private;
245
246         /* Free the resources for the last device */
247         if (!ulp_ctx_deinit_allowed(bp))
248                 return 0;
249
250         ret = ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
251         if (ret)
252                 rte_flow_error_set(error, ret,
253                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
254                                    "Failed to flush flow.");
255         return ret;
256 }
257
258 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
259         .validate = bnxt_ulp_flow_validate,
260         .create = bnxt_ulp_flow_create,
261         .destroy = bnxt_ulp_flow_destroy,
262         .flush = bnxt_ulp_flow_flush,
263         .query = NULL,
264         .isolate = NULL
265 };