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