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