net/bnxt: add default identifiers to ULP mapper
[dpdk.git] / drivers / net / bnxt / tf_ulp / bnxt_ulp_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2020 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 <rte_malloc.h>
13
14 static int32_t
15 bnxt_ulp_flow_validate_args(const struct rte_flow_attr *attr,
16                             const struct rte_flow_item pattern[],
17                             const struct rte_flow_action actions[],
18                             struct rte_flow_error *error)
19 {
20         /* Perform the validation of the arguments for null */
21         if (!error)
22                 return BNXT_TF_RC_ERROR;
23
24         if (!pattern) {
25                 rte_flow_error_set(error,
26                                    EINVAL,
27                                    RTE_FLOW_ERROR_TYPE_ITEM_NUM,
28                                    NULL,
29                                    "NULL pattern.");
30                 return BNXT_TF_RC_ERROR;
31         }
32
33         if (!actions) {
34                 rte_flow_error_set(error,
35                                    EINVAL,
36                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM,
37                                    NULL,
38                                    "NULL action.");
39                 return BNXT_TF_RC_ERROR;
40         }
41
42         if (!attr) {
43                 rte_flow_error_set(error,
44                                    EINVAL,
45                                    RTE_FLOW_ERROR_TYPE_ATTR,
46                                    NULL,
47                                    "NULL attribute.");
48                 return BNXT_TF_RC_ERROR;
49         }
50
51         if (attr->egress && attr->ingress) {
52                 rte_flow_error_set(error,
53                                    EINVAL,
54                                    RTE_FLOW_ERROR_TYPE_ATTR,
55                                    attr,
56                                    "EGRESS AND INGRESS UNSUPPORTED");
57                 return BNXT_TF_RC_ERROR;
58         }
59         return BNXT_TF_RC_SUCCESS;
60 }
61
62 /* Function to create the rte flow. */
63 static struct rte_flow *
64 bnxt_ulp_flow_create(struct rte_eth_dev *dev,
65                      const struct rte_flow_attr *attr,
66                      const struct rte_flow_item pattern[],
67                      const struct rte_flow_action actions[],
68                      struct rte_flow_error *error)
69 {
70         struct bnxt_ulp_mapper_create_parms mapper_cparms = { 0 };
71         struct ulp_rte_parser_params params;
72         struct bnxt_ulp_context *ulp_ctx = NULL;
73         uint32_t class_id, act_tmpl;
74         struct rte_flow *flow_id;
75         uint32_t fid;
76         int ret;
77
78         if (bnxt_ulp_flow_validate_args(attr,
79                                         pattern, actions,
80                                         error) == BNXT_TF_RC_ERROR) {
81                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
82                 return NULL;
83         }
84
85         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
86         if (!ulp_ctx) {
87                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
88                 return NULL;
89         }
90
91         /* Initialize the parser params */
92         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
93
94         if (attr->egress)
95                 params.dir = ULP_DIR_EGRESS;
96
97         /* copy the device port id and direction for further processing */
98         ULP_UTIL_CHF_IDX_WR(&params, BNXT_ULP_CHF_IDX_INCOMING_IF,
99                             dev->data->port_id);
100         ULP_UTIL_CHF_IDX_WR(&params, BNXT_ULP_CHF_IDX_DIRECTION, params.dir);
101
102         /* Parse the rte flow pattern */
103         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
104         if (ret != BNXT_TF_RC_SUCCESS)
105                 goto parse_error;
106
107         /* Parse the rte flow action */
108         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
109         if (ret != BNXT_TF_RC_SUCCESS)
110                 goto parse_error;
111
112         ret = ulp_matcher_pattern_match(&params, &class_id);
113         if (ret != BNXT_TF_RC_SUCCESS)
114                 goto parse_error;
115
116         ret = ulp_matcher_action_match(&params, &act_tmpl);
117         if (ret != BNXT_TF_RC_SUCCESS)
118                 goto parse_error;
119
120         mapper_cparms.app_priority = attr->priority;
121         mapper_cparms.hdr_bitmap = &params.hdr_bitmap;
122         mapper_cparms.hdr_field = params.hdr_field;
123         mapper_cparms.act = &params.act_bitmap;
124         mapper_cparms.act_prop = &params.act_prop;
125         mapper_cparms.class_tid = class_id;
126         mapper_cparms.act_tid = act_tmpl;
127         mapper_cparms.func_id = bnxt_get_fw_func_id(dev->data->port_id);
128         mapper_cparms.dir = params.dir;
129
130         /* Call the ulp mapper to create the flow in the hardware. */
131         ret = ulp_mapper_flow_create(ulp_ctx, &mapper_cparms, &fid);
132         if (!ret) {
133                 flow_id = (struct rte_flow *)((uintptr_t)fid);
134                 return flow_id;
135         }
136
137 parse_error:
138         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
139                            "Failed to create flow.");
140         return NULL;
141 }
142
143 /* Function to validate the rte flow. */
144 static int
145 bnxt_ulp_flow_validate(struct rte_eth_dev *dev __rte_unused,
146                        const struct rte_flow_attr *attr,
147                        const struct rte_flow_item pattern[],
148                        const struct rte_flow_action actions[],
149                        struct rte_flow_error *error)
150 {
151         struct ulp_rte_parser_params            params;
152         uint32_t class_id, act_tmpl;
153         int ret;
154
155         if (bnxt_ulp_flow_validate_args(attr,
156                                         pattern, actions,
157                                         error) == BNXT_TF_RC_ERROR) {
158                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
159                 return -EINVAL;
160         }
161
162         /* Initialize the parser params */
163         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
164
165         if (attr->egress)
166                 params.dir = ULP_DIR_EGRESS;
167
168         /* Parse the rte flow pattern */
169         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
170         if (ret != BNXT_TF_RC_SUCCESS)
171                 goto parse_error;
172
173         /* Parse the rte flow action */
174         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
175         if (ret != BNXT_TF_RC_SUCCESS)
176                 goto parse_error;
177
178         ret = ulp_matcher_pattern_match(&params, &class_id);
179
180         if (ret != BNXT_TF_RC_SUCCESS)
181                 goto parse_error;
182
183         ret = ulp_matcher_action_match(&params, &act_tmpl);
184         if (ret != BNXT_TF_RC_SUCCESS)
185                 goto parse_error;
186
187         /* all good return success */
188         return ret;
189
190 parse_error:
191         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
192                            "Failed to validate flow.");
193         return -EINVAL;
194 }
195
196 /* Function to destroy the rte flow. */
197 static int
198 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
199                       struct rte_flow *flow,
200                       struct rte_flow_error *error)
201 {
202         int ret = 0;
203         struct bnxt_ulp_context *ulp_ctx;
204         uint32_t flow_id;
205         uint16_t func_id;
206
207         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
208         if (!ulp_ctx) {
209                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
210                 rte_flow_error_set(error, EINVAL,
211                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
212                                    "Failed to destroy flow.");
213                 return -EINVAL;
214         }
215
216         flow_id = (uint32_t)(uintptr_t)flow;
217         func_id = bnxt_get_fw_func_id(dev->data->port_id);
218
219         if (ulp_flow_db_validate_flow_func(ulp_ctx, flow_id, func_id) ==
220             false) {
221                 BNXT_TF_DBG(ERR, "Incorrect device params\n");
222                 rte_flow_error_set(error, EINVAL,
223                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
224                                    "Failed to destroy flow.");
225                 return -EINVAL;
226         }
227
228         ret = ulp_mapper_flow_destroy(ulp_ctx, flow_id);
229         if (ret)
230                 rte_flow_error_set(error, -ret,
231                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
232                                    "Failed to destroy flow.");
233
234         return ret;
235 }
236
237 /* Function to destroy the rte flows. */
238 static int32_t
239 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
240                     struct rte_flow_error *error)
241 {
242         struct bnxt_ulp_context *ulp_ctx;
243         int32_t ret = 0;
244         struct bnxt *bp;
245         uint16_t func_id;
246
247         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
248         if (!ulp_ctx) {
249                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
250                 rte_flow_error_set(error, EINVAL,
251                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
252                                    "Failed to flush flow.");
253                 return -EINVAL;
254         }
255         bp = eth_dev->data->dev_private;
256
257         /* Free the resources for the last device */
258         if (ulp_ctx_deinit_allowed(bp)) {
259                 ret = ulp_flow_db_session_flow_flush(ulp_ctx);
260         } else if (bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx)) {
261                 func_id = bnxt_get_fw_func_id(eth_dev->data->port_id);
262                 ret = ulp_flow_db_function_flow_flush(ulp_ctx, func_id);
263         }
264         if (ret)
265                 rte_flow_error_set(error, ret,
266                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
267                                    "Failed to flush flow.");
268         return ret;
269 }
270
271 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
272         .validate = bnxt_ulp_flow_validate,
273         .create = bnxt_ulp_flow_create,
274         .destroy = bnxt_ulp_flow_destroy,
275         .flush = bnxt_ulp_flow_flush,
276         .query = NULL,
277         .isolate = NULL
278 };