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