9326401b4d98d404c7c756b3ca7a8d4d0a79cbb4
[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         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         mapper_cparms.app_priority = attr->priority;
129         mapper_cparms.hdr_bitmap = &params.hdr_bitmap;
130         mapper_cparms.hdr_field = params.hdr_field;
131         mapper_cparms.act = &params.act_bitmap;
132         mapper_cparms.act_prop = &params.act_prop;
133         mapper_cparms.class_tid = class_id;
134         mapper_cparms.act_tid = act_tmpl;
135
136         /* call the ulp mapper to create the flow in the hardware */
137         ret = ulp_mapper_flow_create(ulp_ctx,
138                                      &mapper_cparms,
139                                      &fid);
140         if (!ret) {
141                 flow_id = (struct rte_flow *)((uintptr_t)fid);
142                 return flow_id;
143         }
144
145 parse_error:
146         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
147                            "Failed to create flow.");
148         return NULL;
149 }
150
151 /* Function to validate the rte flow. */
152 static int
153 bnxt_ulp_flow_validate(struct rte_eth_dev *dev __rte_unused,
154                        const struct rte_flow_attr *attr,
155                        const struct rte_flow_item pattern[],
156                        const struct rte_flow_action actions[],
157                        struct rte_flow_error *error)
158 {
159         struct ulp_rte_parser_params            params;
160         uint32_t class_id, act_tmpl;
161         int ret;
162
163         if (bnxt_ulp_flow_validate_args(attr,
164                                         pattern, actions,
165                                         error) == BNXT_TF_RC_ERROR) {
166                 BNXT_TF_DBG(ERR, "Invalid arguments being passed\n");
167                 return -EINVAL;
168         }
169
170         /* Initialize the parser params */
171         memset(&params, 0, sizeof(struct ulp_rte_parser_params));
172
173         if (attr->egress)
174                 params.dir = ULP_DIR_EGRESS;
175
176         /* Parse the rte flow pattern */
177         ret = bnxt_ulp_rte_parser_hdr_parse(pattern, &params);
178         if (ret != BNXT_TF_RC_SUCCESS)
179                 goto parse_error;
180
181         /* Parse the rte flow action */
182         ret = bnxt_ulp_rte_parser_act_parse(actions, &params);
183         if (ret != BNXT_TF_RC_SUCCESS)
184                 goto parse_error;
185
186         ret = ulp_matcher_pattern_match(&params, &class_id);
187
188         if (ret != BNXT_TF_RC_SUCCESS)
189                 goto parse_error;
190
191         ret = ulp_matcher_action_match(&params, &act_tmpl);
192         if (ret != BNXT_TF_RC_SUCCESS)
193                 goto parse_error;
194
195         /* all good return success */
196         return ret;
197
198 parse_error:
199         rte_flow_error_set(error, ret, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
200                            "Failed to validate flow.");
201         return -EINVAL;
202 }
203
204 /* Function to destroy the rte flow. */
205 static int
206 bnxt_ulp_flow_destroy(struct rte_eth_dev *dev,
207                       struct rte_flow *flow,
208                       struct rte_flow_error *error)
209 {
210         int ret = 0;
211         struct bnxt_ulp_context *ulp_ctx;
212         uint32_t fid;
213
214         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(dev);
215         if (!ulp_ctx) {
216                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
217                 rte_flow_error_set(error, EINVAL,
218                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
219                                    "Failed to destroy flow.");
220                 return -EINVAL;
221         }
222
223         fid = (uint32_t)(uintptr_t)flow;
224
225         ret = ulp_mapper_flow_destroy(ulp_ctx, fid);
226         if (ret)
227                 rte_flow_error_set(error, -ret,
228                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
229                                    "Failed to destroy flow.");
230
231         return ret;
232 }
233
234 /* Function to destroy the rte flows. */
235 static int32_t
236 bnxt_ulp_flow_flush(struct rte_eth_dev *eth_dev,
237                     struct rte_flow_error *error)
238 {
239         struct bnxt_ulp_context *ulp_ctx;
240         int32_t ret;
241         struct bnxt *bp;
242
243         ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
244         if (!ulp_ctx) {
245                 BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
246                 rte_flow_error_set(error, EINVAL,
247                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
248                                    "Failed to flush flow.");
249                 return -EINVAL;
250         }
251         bp = eth_dev->data->dev_private;
252
253         /* Free the resources for the last device */
254         if (!ulp_ctx_deinit_allowed(bp))
255                 return 0;
256
257         ret = ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_REGULAR_FLOW_TABLE);
258         if (ret)
259                 rte_flow_error_set(error, ret,
260                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
261                                    "Failed to flush flow.");
262         return ret;
263 }
264
265 const struct rte_flow_ops bnxt_ulp_rte_flow_ops = {
266         .validate = bnxt_ulp_flow_validate,
267         .create = bnxt_ulp_flow_create,
268         .destroy = bnxt_ulp_flow_destroy,
269         .flush = bnxt_ulp_flow_flush,
270         .query = NULL,
271         .isolate = NULL
272 };