net/bnxt: support VXLAN decap offload
[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 "ulp_fc_mgr.h"
13 #include "ulp_port_db.h"
14 #include <rte_malloc.h>
15
16 static int32_t
17 bnxt_ulp_flow_validate_args(const struct rte_flow_attr *attr,
18                             const struct rte_flow_item pattern[],
19                             const struct rte_flow_action actions[],
20                             struct rte_flow_error *error)
21 {
22         /* Perform the validation of the arguments for null */
23         if (!error)
24                 return BNXT_TF_RC_ERROR;
25
26         if (!pattern) {
27                 rte_flow_error_set(error,
28                                    EINVAL,
29                                    RTE_FLOW_ERROR_TYPE_ITEM_NUM,
30                                    NULL,
31                                    "NULL pattern.");
32                 return BNXT_TF_RC_ERROR;
33         }
34
35         if (!actions) {
36                 rte_flow_error_set(error,
37                                    EINVAL,
38                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
39                                    NULL,
40                                    "NULL action.");
41                 return BNXT_TF_RC_ERROR;
42         }
43
44         if (!attr) {
45                 rte_flow_error_set(error,
46                                    EINVAL,
47                                    RTE_FLOW_ERROR_TYPE_ATTR,
48                                    NULL,
49                                    "NULL attribute.");
50                 return BNXT_TF_RC_ERROR;
51         }
52
53         if (attr->egress && attr->ingress) {
54                 rte_flow_error_set(error,
55                                    EINVAL,
56                                    RTE_FLOW_ERROR_TYPE_ATTR,
57                                    attr,
58                                    "EGRESS AND INGRESS UNSUPPORTED");
59                 return BNXT_TF_RC_ERROR;
60         }
61         return BNXT_TF_RC_SUCCESS;
62 }
63
64 static inline void
65 bnxt_ulp_set_dir_attributes(struct ulp_rte_parser_params *params,
66                             const struct rte_flow_attr *attr)
67 {
68         /* Set the flow attributes */
69         if (attr->egress)
70                 params->dir_attr |= BNXT_ULP_FLOW_ATTR_EGRESS;
71         if (attr->ingress)
72                 params->dir_attr |= BNXT_ULP_FLOW_ATTR_INGRESS;
73         if (attr->transfer)
74                 params->dir_attr |= BNXT_ULP_FLOW_ATTR_TRANSFER;
75 }
76
77 void
78 bnxt_ulp_init_mapper_params(struct bnxt_ulp_mapper_create_parms *mapper_cparms,
79                             struct ulp_rte_parser_params *params,
80                             enum bnxt_ulp_fdb_type flow_type)
81 {
82         mapper_cparms->flow_type        = flow_type;
83         mapper_cparms->app_priority     = params->priority;
84         mapper_cparms->dir_attr         = params->dir_attr;
85         mapper_cparms->class_tid        = params->class_id;
86         mapper_cparms->act_tid          = params->act_tmpl;
87         mapper_cparms->func_id          = params->func_id;
88         mapper_cparms->hdr_bitmap       = &params->hdr_bitmap;
89         mapper_cparms->hdr_field        = params->hdr_field;
90         mapper_cparms->comp_fld         = params->comp_fld;
91         mapper_cparms->act              = &params->act_bitmap;
92         mapper_cparms->act_prop         = &params->act_prop;
93         mapper_cparms->flow_id          = params->fid;
94         mapper_cparms->parent_flow      = params->parent_flow;
95         mapper_cparms->parent_fid       = params->parent_fid;
96 }
97
98 /* Function to create the rte flow. */
99 static struct rte_flow *
100 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
101                      const struct rte_flow_attr *attr,
102                      const struct rte_flow_item pattern[],
103                      const struct rte_flow_action actions[],
104                      struct rte_flow_error *error)
105 {
106         struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
107         struct ulp_rte_parser_params params;
108         struct bnxt_ulp_context *ulp_ctx;
109         int rc, ret = BNXT_TF_RC_ERROR;
110         struct rte_flow *flow_id;
111         uint16_t func_id;
112         uint32_t fid;
113
114         if (bnxt_ulp_flow_validate_args(attr,
115                                         pattern, actions,
116                                         error) == BNXT_TF_RC_ERROR) {
117                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
118                 goto flow_error;
119         }
120
121         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
122         if (!ulp_ctx) {
123                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
124                 goto flow_error;
125         }
126
127         /* Initialize the parser params */
128         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
129         params.ulp_ctx = ulp_ctx;
130
131         /* Set the flow attributes */
132         bnxt_ulp_set_dir_attributes(&params, attr);
133
134         /* copy the device port id and direction for further processing */
135         ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_INCOMING_IF,
136                             dev->data->port_id);
137         ULP_COMP_FLD_IDX_WR(&params, BNXT_ULP_CF_IDX_SVIF_FLAG,
138                             BNXT_ULP_INVALID_SVIF_VAL);
139
140         /* Get the function id */
141         if (ulp_port_db_port_func_id_get(ulp_ctx,
142                                          dev->data->port_id,
143                                          &func_id)) {
144                 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
145                 goto flow_error;
146         }
147
148         /* Protect flow creation */
149         if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
150                 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
151                 goto flow_error;
152         }
153
154         /* Allocate a Flow ID for attaching all resources for the flow to.
155          * Once allocated, all errors have to walk the list of resources and
156          * free each of them.
157          */
158         rc = ulp_flow_db_fid_alloc(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
159                                    func_id, &fid);
160         if (rc) {
161                 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
162                 goto release_lock;
163         }
164
165         /* Parse the rte flow pattern */
166         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
167         if (ret != BNXT_TF_RC_SUCCESS)
168                 goto free_fid;
169
170         /* Parse the rte flow action */
171         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
172         if (ret != BNXT_TF_RC_SUCCESS)
173                 goto free_fid;
174
175         params.fid = fid;
176         params.func_id = func_id;
177         params.priority = attr->priority;
178         /* Perform the rte flow post process */
179         ret = bnxt_ulp_rte_parser_post_process(&params);
180         if (ret == BNXT_TF_RC_ERROR)
181                 goto free_fid;
182         else if (ret == BNXT_TF_RC_FID)
183                 goto return_fid;
184
185         ret = ulp_matcher_pattern_match(&params, &params.class_id);
186         if (ret != BNXT_TF_RC_SUCCESS)
187                 goto free_fid;
188
189         ret = ulp_matcher_action_match(&params, &params.act_tmpl);
190         if (ret != BNXT_TF_RC_SUCCESS)
191                 goto free_fid;
192
193         bnxt_ulp_init_mapper_params(&mapper_cparms, &params,
194                                     BNXT_ULP_FDB_TYPE_REGULAR);
195         /* Call the ulp mapper to create the flow in the hardware. */
196         ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms);
197         if (ret)
198                 goto free_fid;
199
200 return_fid:
201         bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
202
203         flow_id = (struct rte_flow *)((uintptr_t)fid);
204         return flow_id;
205
206 free_fid:
207         ulp_flow_db_fid_free(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR, fid);
208 release_lock:
209         bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
210 flow_error:
211         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
212                            "Failed to create flow.");
213         return NULL;
214 }
215
216 /* Function to validate the rte flow. */
217 static int
218 bnxt_ulp_flow_validate(struct rte_eth_dev *dev,
219                        const struct rte_flow_attr *attr,
220                        const struct rte_flow_item pattern[],
221                        const struct rte_flow_action actions[],
222                        struct rte_flow_error *error)
223 {
224         struct ulp_rte_parser_params params;
225         struct bnxt_ulp_context *ulp_ctx;
226         uint32_t class_id, act_tmpl;
227         int ret = BNXT_TF_RC_ERROR;
228
229         if (bnxt_ulp_flow_validate_args(attr,
230                                         pattern, actions,
231                                         error) == BNXT_TF_RC_ERROR) {
232                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
233                 goto parse_error;
234         }
235
236         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
237         if (!ulp_ctx) {
238                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
239                 goto parse_error;
240         }
241
242         /* Initialize the parser params */
243         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
244         params.ulp_ctx = ulp_ctx;
245
246         /* Set the flow attributes */
247         bnxt_ulp_set_dir_attributes(&params, attr);
248
249         /* Parse the rte flow pattern */
250         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
251         if (ret != BNXT_TF_RC_SUCCESS)
252                 goto parse_error;
253
254         /* Parse the rte flow action */
255         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
256         if (ret != BNXT_TF_RC_SUCCESS)
257                 goto parse_error;
258
259         /* Perform the rte flow post process */
260         ret = bnxt_ulp_rte_parser_post_process(&params);
261         if (ret == BNXT_TF_RC_ERROR)
262                 goto parse_error;
263         else if (ret == BNXT_TF_RC_FID)
264                 return 0;
265
266         ret = ulp_matcher_pattern_match(&params, &class_id);
267
268         if (ret != BNXT_TF_RC_SUCCESS)
269                 goto parse_error;
270
271         ret = ulp_matcher_action_match(&params, &act_tmpl);
272         if (ret != BNXT_TF_RC_SUCCESS)
273                 goto parse_error;
274
275         /* all good return success */
276         return ret;
277
278 parse_error:
279         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
280                            "Failed to validate flow.");
281         return -EINVAL;
282 }
283
284 /* Function to destroy the rte flow. */
285 int
286 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
287                       struct rte_flow *flow,
288                       struct rte_flow_error *error)
289 {
290         struct bnxt_ulp_context *ulp_ctx;
291         uint32_t flow_id;
292         uint16_t func_id;
293         int ret;
294
295         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
296         if (!ulp_ctx) {
297                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
298                 if (error)
299                         rte_flow_error_set(error, EINVAL,
300                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
301                                            "Failed to destroy flow.");
302                 return -EINVAL;
303         }
304
305         flow_id = (uint32_t)(uintptr_t)flow;
306
307         if (ulp_port_db_port_func_id_get(ulp_ctx,
308                                          dev->data->port_id,
309                                          &func_id)) {
310                 BNXT_TF_DBG(ERR, "conversion of port to func id failed\n");
311                 if (error)
312                         rte_flow_error_set(error, EINVAL,
313                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
314                                            "Failed to destroy flow.");
315                 return -EINVAL;
316         }
317
318         if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
319             false) {
320                 BNXT_TF_DBG(ERR, "Incorrect device params\n");
321                 if (error)
322                         rte_flow_error_set(error, EINVAL,
323                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
324                                            "Failed to destroy flow.");
325                 return -EINVAL;
326         }
327
328         if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
329                 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
330                 return -EINVAL;
331         }
332         ret = ulp_mapper_flow_destroy(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
333                                       flow_id);
334         if (ret) {
335                 BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
336                 if (error)
337                         rte_flow_error_set(error, -ret,
338                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
339                                            "Failed to destroy flow.");
340         }
341         bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
342
343         return ret;
344 }
345
346 /* Function to destroy the rte flows. */
347 static int32_t
348 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
349                     struct rte_flow_error *error)
350 {
351         struct bnxt_ulp_context *ulp_ctx;
352         int32_t ret = 0;
353         uint16_t func_id;
354
355         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
356         if (!ulp_ctx) {
357                 return ret;
358         }
359
360         /* Free the resources for the last device */
361         if (ulp_ctx_deinit_allowed(ulp_ctx)) {
362                 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
363         } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
364                 ret = ulp_port_db_port_func_id_get(ulp_ctx,
365                                                    eth_dev->data->port_id,
366                                                    &func_id);
367                 if (!ret)
368                         ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
369                 else
370                         BNXT_TF_DBG(ERR, "convert port to func id failed\n");
371         }
372         if (ret)
373                 rte_flow_error_set(error, ret,
374                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
375                                    "Failed to flush flow.");
376         return ret;
377 }
378
379 /* Function to query the rte flows. */
380 static int32_t
381 bnxt_ulp_flow_query(struct rte_eth_dev *eth_dev,
382                     struct rte_flow *flow,
383                     const struct rte_flow_action *action,
384                     void *data,
385                     struct rte_flow_error *error)
386 {
387         int rc = 0;
388         struct bnxt_ulp_context *ulp_ctx;
389         struct rte_flow_query_count *count;
390         uint32_t flow_id;
391
392         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
393         if (!ulp_ctx) {
394                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
395                 rte_flow_error_set(error, EINVAL,
396                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
397                                    "Failed to query flow.");
398                 return -EINVAL;
399         }
400
401         flow_id = (uint32_t)(uintptr_t)flow;
402
403         switch (action->type) {
404         case RTE_FLOW_ACTION_TYPE_COUNT:
405                 count = data;
406                 rc = ulp_fc_mgr_query_count_get(ulp_ctx, flow_id, count);
407                 if (rc) {
408                         rte_flow_error_set(error, EINVAL,
409                                            RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
410                                            "Failed to query flow.");
411                 }
412                 break;
413         default:
414                 rte_flow_error_set(error, -rc, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
415                                    NULL, "Unsupported action item");
416         }
417
418         return rc;
419 }
420
421 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
422         .validate = bnxt_ulp_flow_validate,
423         .create = bnxt_ulp_flow_create,
424         .destroy = bnxt_ulp_flow_destroy,
425         .flush = bnxt_ulp_flow_flush,
426         .query = bnxt_ulp_flow_query,
427         .isolate = NULL
428 };