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