7ef306e58ef9ce7f78cb60ea54b034761f9b40f9
[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;
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         params.ulp_ctx = ulp_ctx;
94
95         if (attr->egress)
96                 params.dir = ULP_DIR_EGRESS;
97
98         /* copy the device port id and direction for further processing */
99         ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_INCOMING_IF,
100                             dev->data->port_id);
101         ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_DIRECTION, params.dir);
102         ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_SVIF_FLAG,
103                             BNXT_ULP_INVALID_SVIF_VAL);
104
105         /* Parse the rte flow pattern */
106         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
107         if (ret != BNXT_TF_RC_SUCCESS)
108                 goto parse_error;
109
110         /* Parse the rte flow action */
111         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
112         if (ret != BNXT_TF_RC_SUCCESS)
113                 goto parse_error;
114
115         ret = ulp_matcher_pattern_match(&params, &class_id);
116         if (ret != BNXT_TF_RC_SUCCESS)
117                 goto parse_error;
118
119         ret = ulp_matcher_action_match(&params, &act_tmpl);
120         if (ret != BNXT_TF_RC_SUCCESS)
121                 goto parse_error;
122
123         mapper_cparms.app_priority = attr->priority;
124         mapper_cparms.hdr_bitmap = &params.hdr_bitmap;
125         mapper_cparms.hdr_field = params.hdr_field;
126         mapper_cparms.comp_fld = params.comp_fld;
127         mapper_cparms.act = &params.act_bitmap;
128         mapper_cparms.act_prop = &params.act_prop;
129         mapper_cparms.class_tid = class_id;
130         mapper_cparms.act_tid = act_tmpl;
131         mapper_cparms.func_id = bnxt_get_fw_func_id(dev->data->port_id,
132                                                     BNXT_ULP_INTF_TYPE_INVALID);
133         mapper_cparms.dir = params.dir;
134
135         /* Call the ulp mapper to create the flow in the hardware. */
136         ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms, &fid);
137         if (!ret) {
138                 flow_id = (struct rte_flow *)((uintptr_t)fid);
139                 return flow_id;
140         }
141
142 parse_error:
143         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
144                            "Failed to create flow.");
145         return NULL;
146 }
147
148 /* Function to validate the rte flow. */
149 static int
150 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
151                        const struct rte_flow_attr *attr,
152                        const struct rte_flow_item pattern[],
153                        const struct rte_flow_action actions[],
154                        struct rte_flow_error *error)
155 {
156         struct ulp_rte_parser_params            params;
157         uint32_t class_id, act_tmpl;
158         int ret;
159         struct bnxt_ulp_context *ulp_ctx;
160
161         if (bnxt_ulp_flow_validate_args(attr,
162                                         pattern, actions,
163                                         error) == BNXT_TF_RC_ERROR) {
164                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
165                 return -EINVAL;
166         }
167
168         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
169         if (!ulp_ctx) {
170                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
171                 return -EINVAL;
172         }
173
174         /* Initialize the parser params */
175         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
176         params.ulp_ctx = ulp_ctx;
177
178         if (attr->egress)
179                 params.dir = ULP_DIR_EGRESS;
180
181         /* Parse the rte flow pattern */
182         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
183         if (ret != BNXT_TF_RC_SUCCESS)
184                 goto parse_error;
185
186         /* Parse the rte flow action */
187         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
188         if (ret != BNXT_TF_RC_SUCCESS)
189                 goto parse_error;
190
191         ret = ulp_matcher_pattern_match(&params, &class_id);
192
193         if (ret != BNXT_TF_RC_SUCCESS)
194                 goto parse_error;
195
196         ret = ulp_matcher_action_match(&params, &act_tmpl);
197         if (ret != BNXT_TF_RC_SUCCESS)
198                 goto parse_error;
199
200         /* all good return success */
201         return ret;
202
203 parse_error:
204         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
205                            "Failed to validate flow.");
206         return -EINVAL;
207 }
208
209 /* Function to destroy the rte flow. */
210 int
211 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
212                       struct rte_flow *flow,
213                       struct rte_flow_error *error)
214 {
215         int ret = 0;
216         struct bnxt_ulp_context *ulp_ctx;
217         uint32_t flow_id;
218         uint16_t func_id;
219
220         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
221         if (!ulp_ctx) {
222                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
223                 if (error)
224                         rte_flow_error_set(error, EINVAL,
225                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
226                                            "Failed to destroy flow.");
227                 return -EINVAL;
228         }
229
230         flow_id = (uint32_t)(uintptr_t)flow;
231         func_id = bnxt_get_fw_func_id(dev->data->port_id,
232                                       BNXT_ULP_INTF_TYPE_INVALID);
233
234         if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
235             false) {
236                 BNXT_TF_DBG(ERR, "Incorrect device params\n");
237                 if (error)
238                         rte_flow_error_set(error, EINVAL,
239                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
240                                            "Failed to destroy flow.");
241                 return -EINVAL;
242         }
243
244         ret = ulp_mapper_flow_destroy(ulp_ctx, flow_id,
245                                       BNXT_ULP_REGULAR_FLOW_TABLE);
246         if (ret) {
247                 BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
248                 if (error)
249                         rte_flow_error_set(error, -ret,
250                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
251                                            "Failed to destroy flow.");
252         }
253
254         return ret;
255 }
256
257 /* Function to destroy the rte flows. */
258 static int32_t
259 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
260                     struct rte_flow_error *error)
261 {
262         struct bnxt_ulp_context *ulp_ctx;
263         int32_t ret = 0;
264         struct bnxt *bp;
265         uint16_t func_id;
266
267         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
268         if (!ulp_ctx) {
269                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
270                 rte_flow_error_set(error, EINVAL,
271                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
272                                    "Failed to flush flow.");
273                 return -EINVAL;
274         }
275         bp = eth_dev->data->dev_private;
276
277         /* Free the resources for the last device */
278         if (ulp_ctx_deinit_allowed(bp)) {
279                 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
280         } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
281                 func_id = bnxt_get_fw_func_id(eth_dev->data->port_id,
282                                               BNXT_ULP_INTF_TYPE_INVALID);
283                 ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
284         }
285         if (ret)
286                 rte_flow_error_set(error, ret,
287                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
288                                    "Failed to flush flow.");
289         return ret;
290 }
291
292 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
293         .validate = bnxt_ulp_flow_validate,
294         .create = bnxt_ulp_flow_create,
295         .destroy = bnxt_ulp_flow_destroy,
296         .flush = bnxt_ulp_flow_flush,
297         .query = NULL,
298         .isolate = NULL
299 };