026f33f6602ed9af35b805e759dfb8178b563f78
[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 ulp_rte_parser_params params;
71         struct bnxt_ulp_context *ulp_ctx = NULL;
72         uint32_t class_id, act_tmpl;
73         struct rte_flow *flow_id;
74         uint32_t app_priority;
75         uint32_t fid;
76         uint8_t *buffer;
77         uint32_t vnic;
78         int ret;
79
80         if (bnxt_ulp_flow_validate_args(attr,
81                                         pattern, actions,
82                                         error) == BNXT_TF_RC_ERROR) {
83                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
84                 return NULL;
85         }
86
87         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
88         if (!ulp_ctx) {
89                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
90                 return NULL;
91         }
92
93         /* Initialize the parser params */
94         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
95
96         if (attr->egress)
97                 params.dir = ULP_DIR_EGRESS;
98
99         /* copy the device port id and direction for further processing */
100         buffer = params.hdr_field[BNXT_ULP_HDR_FIELD_SVIF_INDEX].spec;
101         rte_memcpy(buffer, &dev->data->port_id, sizeof(uint16_t));
102
103         /* Set the implicit vnic in the action property */
104         vnic = (uint32_t)bnxt_get_vnic_id(dev->data->port_id);
105         vnic = htonl(vnic);
106         rte_memcpy(&params.act_prop.act_details[BNXT_ULP_ACT_PROP_IDX_VNIC],
107                    &vnic, BNXT_ULP_ACT_PROP_SZ_VNIC);
108
109         /* Parse the rte flow pattern */
110         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
111         if (ret != BNXT_TF_RC_SUCCESS)
112                 goto parse_error;
113
114         /* Parse the rte flow action */
115         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
116         if (ret != BNXT_TF_RC_SUCCESS)
117                 goto parse_error;
118
119         ret = ulp_matcher_pattern_match(&params, &class_id);
120
121         if (ret != BNXT_TF_RC_SUCCESS)
122                 goto parse_error;
123
124         ret = ulp_matcher_action_match(&params, &act_tmpl);
125         if (ret != BNXT_TF_RC_SUCCESS)
126                 goto parse_error;
127
128         app_priority = attr->priority;
129         /* call the ulp mapper to create the flow in the hardware */
130         ret = ulp_mapper_flow_create(ulp_ctx,
131                                      app_priority,
132                                      &params.hdr_bitmap,
133                                      params.hdr_field,
134                                      &params.act_bitmap,
135                                      &params.act_prop,
136                                      class_id,
137                                      act_tmpl,
138                                      &fid);
139         if (!ret) {
140                 flow_id = (struct rte_flow *)((uintptr_t)fid);
141                 return flow_id;
142         }
143
144 parse_error:
145         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
146                            "Failed to create flow.");
147         return NULL;
148 }
149
150 /* Function to validate the rte flow. */
151 static int
152 bnxt_ulp_flow_validate(struct rte_eth_dev *dev __rte_unused,
153                        const struct rte_flow_attr *attr,
154                        const struct rte_flow_item pattern[],
155                        const struct rte_flow_action actions[],
156                        struct rte_flow_error *error)
157 {
158         struct ulp_rte_parser_params            params;
159         uint32_t class_id, act_tmpl;
160         int ret;
161
162         if (bnxt_ulp_flow_validate_args(attr,
163                                         pattern, actions,
164                                         error) == BNXT_TF_RC_ERROR) {
165                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
166                 return -EINVAL;
167         }
168
169         /* Initialize the parser params */
170         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
171
172         if (attr->egress)
173                 params.dir = ULP_DIR_EGRESS;
174
175         /* Parse the rte flow pattern */
176         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
177         if (ret != BNXT_TF_RC_SUCCESS)
178                 goto parse_error;
179
180         /* Parse the rte flow action */
181         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
182         if (ret != BNXT_TF_RC_SUCCESS)
183                 goto parse_error;
184
185         ret = ulp_matcher_pattern_match(&params, &class_id);
186
187         if (ret != BNXT_TF_RC_SUCCESS)
188                 goto parse_error;
189
190         ret = ulp_matcher_action_match(&params, &act_tmpl);
191         if (ret != BNXT_TF_RC_SUCCESS)
192                 goto parse_error;
193
194         /* all good return success */
195         return ret;
196
197 parse_error:
198         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
199                            "Failed to validate flow.");
200         return -EINVAL;
201 }
202
203 /* Function to destroy the rte flow. */
204 static int
205 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
206                       struct rte_flow *flow,
207                       struct rte_flow_error *error)
208 {
209         int ret = 0;
210         struct bnxt_ulp_context *ulp_ctx;
211         uint32_t fid;
212
213         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
214         if (!ulp_ctx) {
215                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
216                 rte_flow_error_set(error, EINVAL,
217                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
218                                    "Failed to destroy flow.");
219                 return -EINVAL;
220         }
221
222         fid = (uint32_t)(uintptr_t)flow;
223
224         ret = ulp_mapper_flow_destroy(ulp_ctx, fid);
225         if (ret)
226                 rte_flow_error_set(error, -ret,
227                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
228                                    "Failed to destroy flow.");
229
230         return ret;
231 }
232
233 /* Function to destroy the rte flows. */
234 static int32_t
235 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
236                     struct rte_flow_error *error)
237 {
238         struct bnxt_ulp_context *ulp_ctx;
239         int32_t ret;
240         struct bnxt *bp;
241
242         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
243         if (!ulp_ctx) {
244                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
245                 rte_flow_error_set(error, EINVAL,
246                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
247                                    "Failed to flush flow.");
248                 return -EINVAL;
249         }
250         bp = eth_dev->data->dev_private;
251
252         /* Free the resources for the last device */
253         if (!ulp_ctx_deinit_allowed(bp))
254                 return 0;
255
256         ret = ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
257         if (ret)
258                 rte_flow_error_set(error, ret,
259                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
260                                    "Failed to flush flow.");
261         return ret;
262 }
263
264 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
265         .validate = bnxt_ulp_flow_validate,
266         .create = bnxt_ulp_flow_create,
267         .destroy = bnxt_ulp_flow_destroy,
268         .flush = bnxt_ulp_flow_flush,
269         .query = NULL,
270         .isolate = NULL
271 };