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