net/mlx5: Flow steering, Add vport ACL support
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_cmd.c
1 /*
2  * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/mlx5/driver.h>
34 #include <linux/mlx5/device.h>
35 #include <linux/mlx5/mlx5_ifc.h>
36
37 #include "fs_core.h"
38 #include "fs_cmd.h"
39 #include "mlx5_core.h"
40
41 int mlx5_cmd_update_root_ft(struct mlx5_core_dev *dev,
42                             struct mlx5_flow_table *ft)
43 {
44         u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)];
45         u32 out[MLX5_ST_SZ_DW(set_flow_table_root_out)];
46
47         memset(in, 0, sizeof(in));
48
49         MLX5_SET(set_flow_table_root_in, in, opcode,
50                  MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
51         MLX5_SET(set_flow_table_root_in, in, table_type, ft->type);
52         MLX5_SET(set_flow_table_root_in, in, table_id, ft->id);
53         if (ft->vport) {
54                 MLX5_SET(set_flow_table_root_in, in, vport_number, ft->vport);
55                 MLX5_SET(set_flow_table_root_in, in, other_vport, 1);
56         }
57
58         memset(out, 0, sizeof(out));
59         return mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
60                                           sizeof(out));
61 }
62
63 int mlx5_cmd_create_flow_table(struct mlx5_core_dev *dev,
64                                u16 vport,
65                                enum fs_flow_table_type type, unsigned int level,
66                                unsigned int log_size, struct mlx5_flow_table
67                                *next_ft, unsigned int *table_id)
68 {
69         u32 out[MLX5_ST_SZ_DW(create_flow_table_out)];
70         u32 in[MLX5_ST_SZ_DW(create_flow_table_in)];
71         int err;
72
73         memset(in, 0, sizeof(in));
74
75         MLX5_SET(create_flow_table_in, in, opcode,
76                  MLX5_CMD_OP_CREATE_FLOW_TABLE);
77
78         if (next_ft) {
79                 MLX5_SET(create_flow_table_in, in, table_miss_mode, 1);
80                 MLX5_SET(create_flow_table_in, in, table_miss_id, next_ft->id);
81         }
82         MLX5_SET(create_flow_table_in, in, table_type, type);
83         MLX5_SET(create_flow_table_in, in, level, level);
84         MLX5_SET(create_flow_table_in, in, log_size, log_size);
85         if (vport) {
86                 MLX5_SET(create_flow_table_in, in, vport_number, vport);
87                 MLX5_SET(create_flow_table_in, in, other_vport, 1);
88         }
89
90         memset(out, 0, sizeof(out));
91         err = mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
92                                          sizeof(out));
93
94         if (!err)
95                 *table_id = MLX5_GET(create_flow_table_out, out,
96                                      table_id);
97         return err;
98 }
99
100 int mlx5_cmd_destroy_flow_table(struct mlx5_core_dev *dev,
101                                 struct mlx5_flow_table *ft)
102 {
103         u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)];
104         u32 out[MLX5_ST_SZ_DW(destroy_flow_table_out)];
105
106         memset(in, 0, sizeof(in));
107         memset(out, 0, sizeof(out));
108
109         MLX5_SET(destroy_flow_table_in, in, opcode,
110                  MLX5_CMD_OP_DESTROY_FLOW_TABLE);
111         MLX5_SET(destroy_flow_table_in, in, table_type, ft->type);
112         MLX5_SET(destroy_flow_table_in, in, table_id, ft->id);
113         if (ft->vport) {
114                 MLX5_SET(destroy_flow_table_in, in, vport_number, ft->vport);
115                 MLX5_SET(destroy_flow_table_in, in, other_vport, 1);
116         }
117
118         return mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
119                                           sizeof(out));
120 }
121
122 int mlx5_cmd_modify_flow_table(struct mlx5_core_dev *dev,
123                                struct mlx5_flow_table *ft,
124                                struct mlx5_flow_table *next_ft)
125 {
126         u32 in[MLX5_ST_SZ_DW(modify_flow_table_in)];
127         u32 out[MLX5_ST_SZ_DW(modify_flow_table_out)];
128
129         memset(in, 0, sizeof(in));
130         memset(out, 0, sizeof(out));
131
132         MLX5_SET(modify_flow_table_in, in, opcode,
133                  MLX5_CMD_OP_MODIFY_FLOW_TABLE);
134         MLX5_SET(modify_flow_table_in, in, table_type, ft->type);
135         MLX5_SET(modify_flow_table_in, in, table_id, ft->id);
136         if (ft->vport) {
137                 MLX5_SET(modify_flow_table_in, in, vport_number, ft->vport);
138                 MLX5_SET(modify_flow_table_in, in, other_vport, 1);
139         }
140         MLX5_SET(modify_flow_table_in, in, modify_field_select,
141                  MLX5_MODIFY_FLOW_TABLE_MISS_TABLE_ID);
142         if (next_ft) {
143                 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 1);
144                 MLX5_SET(modify_flow_table_in, in, table_miss_id, next_ft->id);
145         } else {
146                 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 0);
147         }
148
149         return mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
150                                           sizeof(out));
151 }
152
153 int mlx5_cmd_create_flow_group(struct mlx5_core_dev *dev,
154                                struct mlx5_flow_table *ft,
155                                u32 *in,
156                                unsigned int *group_id)
157 {
158         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
159         u32 out[MLX5_ST_SZ_DW(create_flow_group_out)];
160         int err;
161
162         memset(out, 0, sizeof(out));
163
164         MLX5_SET(create_flow_group_in, in, opcode,
165                  MLX5_CMD_OP_CREATE_FLOW_GROUP);
166         MLX5_SET(create_flow_group_in, in, table_type, ft->type);
167         MLX5_SET(create_flow_group_in, in, table_id, ft->id);
168         if (ft->vport) {
169                 MLX5_SET(create_flow_group_in, in, vport_number, ft->vport);
170                 MLX5_SET(create_flow_group_in, in, other_vport, 1);
171         }
172
173         err = mlx5_cmd_exec_check_status(dev, in,
174                                          inlen, out,
175                                          sizeof(out));
176         if (!err)
177                 *group_id = MLX5_GET(create_flow_group_out, out,
178                                      group_id);
179
180         return err;
181 }
182
183 int mlx5_cmd_destroy_flow_group(struct mlx5_core_dev *dev,
184                                 struct mlx5_flow_table *ft,
185                                 unsigned int group_id)
186 {
187         u32 out[MLX5_ST_SZ_DW(destroy_flow_group_out)];
188         u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)];
189
190         memset(in, 0, sizeof(in));
191         memset(out, 0, sizeof(out));
192
193         MLX5_SET(destroy_flow_group_in, in, opcode,
194                  MLX5_CMD_OP_DESTROY_FLOW_GROUP);
195         MLX5_SET(destroy_flow_group_in, in, table_type, ft->type);
196         MLX5_SET(destroy_flow_group_in, in, table_id, ft->id);
197         MLX5_SET(destroy_flow_group_in, in, group_id, group_id);
198         if (ft->vport) {
199                 MLX5_SET(destroy_flow_group_in, in, vport_number, ft->vport);
200                 MLX5_SET(destroy_flow_group_in, in, other_vport, 1);
201         }
202
203         return mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
204                                           sizeof(out));
205 }
206
207 static int mlx5_cmd_set_fte(struct mlx5_core_dev *dev,
208                             int opmod, int modify_mask,
209                             struct mlx5_flow_table *ft,
210                             unsigned group_id,
211                             struct fs_fte *fte)
212 {
213         unsigned int inlen = MLX5_ST_SZ_BYTES(set_fte_in) +
214                 fte->dests_size * MLX5_ST_SZ_BYTES(dest_format_struct);
215         u32 out[MLX5_ST_SZ_DW(set_fte_out)];
216         struct mlx5_flow_rule *dst;
217         void *in_flow_context;
218         void *in_match_value;
219         void *in_dests;
220         u32 *in;
221         int err;
222
223         in = mlx5_vzalloc(inlen);
224         if (!in) {
225                 mlx5_core_warn(dev, "failed to allocate inbox\n");
226                 return -ENOMEM;
227         }
228
229         MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
230         MLX5_SET(set_fte_in, in, op_mod, opmod);
231         MLX5_SET(set_fte_in, in, modify_enable_mask, modify_mask);
232         MLX5_SET(set_fte_in, in, table_type, ft->type);
233         MLX5_SET(set_fte_in, in, table_id,   ft->id);
234         MLX5_SET(set_fte_in, in, flow_index, fte->index);
235         if (ft->vport) {
236                 MLX5_SET(set_fte_in, in, vport_number, ft->vport);
237                 MLX5_SET(set_fte_in, in, other_vport, 1);
238         }
239
240         in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
241         MLX5_SET(flow_context, in_flow_context, group_id, group_id);
242         MLX5_SET(flow_context, in_flow_context, flow_tag, fte->flow_tag);
243         MLX5_SET(flow_context, in_flow_context, action, fte->action);
244         MLX5_SET(flow_context, in_flow_context, destination_list_size,
245                  fte->dests_size);
246         in_match_value = MLX5_ADDR_OF(flow_context, in_flow_context,
247                                       match_value);
248         memcpy(in_match_value, &fte->val, MLX5_ST_SZ_BYTES(fte_match_param));
249
250         if (fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
251                 in_dests = MLX5_ADDR_OF(flow_context, in_flow_context, destination);
252                 list_for_each_entry(dst, &fte->node.children, node.list) {
253                         unsigned int id;
254
255                         MLX5_SET(dest_format_struct, in_dests, destination_type,
256                                  dst->dest_attr.type);
257                         if (dst->dest_attr.type ==
258                             MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
259                                 id = dst->dest_attr.ft->id;
260                         } else {
261                                 id = dst->dest_attr.tir_num;
262                         }
263                         MLX5_SET(dest_format_struct, in_dests, destination_id, id);
264                         in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
265                 }
266         }
267         memset(out, 0, sizeof(out));
268         err = mlx5_cmd_exec_check_status(dev, in, inlen, out,
269                                          sizeof(out));
270         kvfree(in);
271
272         return err;
273 }
274
275 int mlx5_cmd_create_fte(struct mlx5_core_dev *dev,
276                         struct mlx5_flow_table *ft,
277                         unsigned group_id,
278                         struct fs_fte *fte)
279 {
280         return  mlx5_cmd_set_fte(dev, 0, 0, ft, group_id, fte);
281 }
282
283 int mlx5_cmd_update_fte(struct mlx5_core_dev *dev,
284                         struct mlx5_flow_table *ft,
285                         unsigned group_id,
286                         struct fs_fte *fte)
287 {
288         int opmod;
289         int modify_mask;
290         int atomic_mod_cap = MLX5_CAP_FLOWTABLE(dev,
291                                                 flow_table_properties_nic_receive.
292                                                 flow_modify_en);
293         if (!atomic_mod_cap)
294                 return -ENOTSUPP;
295         opmod = 1;
296         modify_mask = 1 <<
297                 MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST;
298
299         return  mlx5_cmd_set_fte(dev, opmod, modify_mask, ft, group_id, fte);
300 }
301
302 int mlx5_cmd_delete_fte(struct mlx5_core_dev *dev,
303                         struct mlx5_flow_table *ft,
304                         unsigned int index)
305 {
306         u32 out[MLX5_ST_SZ_DW(delete_fte_out)];
307         u32 in[MLX5_ST_SZ_DW(delete_fte_in)];
308         int err;
309
310         memset(in, 0, sizeof(in));
311         memset(out, 0, sizeof(out));
312
313         MLX5_SET(delete_fte_in, in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
314         MLX5_SET(delete_fte_in, in, table_type, ft->type);
315         MLX5_SET(delete_fte_in, in, table_id, ft->id);
316         MLX5_SET(delete_fte_in, in, flow_index, index);
317         if (ft->vport) {
318                 MLX5_SET(delete_fte_in, in, vport_number, ft->vport);
319                 MLX5_SET(delete_fte_in, in, other_vport, 1);
320         }
321
322         err =  mlx5_cmd_exec_check_status(dev, in, sizeof(in), out, sizeof(out));
323
324         return err;
325 }