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