net/mlx5_core: Connect flow tables
[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
54         memset(out, 0, sizeof(out));
55         return mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
56                                           sizeof(out));
57 }
58
59 int mlx5_cmd_create_flow_table(struct mlx5_core_dev *dev,
60                                enum fs_flow_table_type type, unsigned int level,
61                                unsigned int log_size, struct mlx5_flow_table
62                                *next_ft, unsigned int *table_id)
63 {
64         u32 out[MLX5_ST_SZ_DW(create_flow_table_out)];
65         u32 in[MLX5_ST_SZ_DW(create_flow_table_in)];
66         int err;
67
68         memset(in, 0, sizeof(in));
69
70         MLX5_SET(create_flow_table_in, in, opcode,
71                  MLX5_CMD_OP_CREATE_FLOW_TABLE);
72
73         if (next_ft) {
74                 MLX5_SET(create_flow_table_in, in, table_miss_mode, 1);
75                 MLX5_SET(create_flow_table_in, in, table_miss_id, next_ft->id);
76         }
77         MLX5_SET(create_flow_table_in, in, table_type, type);
78         MLX5_SET(create_flow_table_in, in, level, level);
79         MLX5_SET(create_flow_table_in, in, log_size, log_size);
80
81         memset(out, 0, sizeof(out));
82         err = mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
83                                          sizeof(out));
84
85         if (!err)
86                 *table_id = MLX5_GET(create_flow_table_out, out,
87                                      table_id);
88         return err;
89 }
90
91 int mlx5_cmd_destroy_flow_table(struct mlx5_core_dev *dev,
92                                 struct mlx5_flow_table *ft)
93 {
94         u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)];
95         u32 out[MLX5_ST_SZ_DW(destroy_flow_table_out)];
96
97         memset(in, 0, sizeof(in));
98         memset(out, 0, sizeof(out));
99
100         MLX5_SET(destroy_flow_table_in, in, opcode,
101                  MLX5_CMD_OP_DESTROY_FLOW_TABLE);
102         MLX5_SET(destroy_flow_table_in, in, table_type, ft->type);
103         MLX5_SET(destroy_flow_table_in, in, table_id, ft->id);
104
105         return mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
106                                           sizeof(out));
107 }
108
109 int mlx5_cmd_modify_flow_table(struct mlx5_core_dev *dev,
110                                struct mlx5_flow_table *ft,
111                                struct mlx5_flow_table *next_ft)
112 {
113         u32 in[MLX5_ST_SZ_DW(modify_flow_table_in)];
114         u32 out[MLX5_ST_SZ_DW(modify_flow_table_out)];
115
116         memset(in, 0, sizeof(in));
117         memset(out, 0, sizeof(out));
118
119         MLX5_SET(modify_flow_table_in, in, opcode,
120                  MLX5_CMD_OP_MODIFY_FLOW_TABLE);
121         MLX5_SET(modify_flow_table_in, in, table_type, ft->type);
122         MLX5_SET(modify_flow_table_in, in, table_id, ft->id);
123         MLX5_SET(modify_flow_table_in, in, modify_field_select,
124                  MLX5_MODIFY_FLOW_TABLE_MISS_TABLE_ID);
125         if (next_ft) {
126                 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 1);
127                 MLX5_SET(modify_flow_table_in, in, table_miss_id, next_ft->id);
128         } else {
129                 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 0);
130         }
131
132         return mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
133                                           sizeof(out));
134 }
135
136 int mlx5_cmd_create_flow_group(struct mlx5_core_dev *dev,
137                                struct mlx5_flow_table *ft,
138                                u32 *in,
139                                unsigned int *group_id)
140 {
141         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
142         u32 out[MLX5_ST_SZ_DW(create_flow_group_out)];
143         int err;
144
145         memset(out, 0, sizeof(out));
146
147         MLX5_SET(create_flow_group_in, in, opcode,
148                  MLX5_CMD_OP_CREATE_FLOW_GROUP);
149         MLX5_SET(create_flow_group_in, in, table_type, ft->type);
150         MLX5_SET(create_flow_group_in, in, table_id, ft->id);
151
152         err = mlx5_cmd_exec_check_status(dev, in,
153                                          inlen, out,
154                                          sizeof(out));
155         if (!err)
156                 *group_id = MLX5_GET(create_flow_group_out, out,
157                                      group_id);
158
159         return err;
160 }
161
162 int mlx5_cmd_destroy_flow_group(struct mlx5_core_dev *dev,
163                                 struct mlx5_flow_table *ft,
164                                 unsigned int group_id)
165 {
166         u32 out[MLX5_ST_SZ_DW(destroy_flow_group_out)];
167         u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)];
168
169         memset(in, 0, sizeof(in));
170         memset(out, 0, sizeof(out));
171
172         MLX5_SET(destroy_flow_group_in, in, opcode,
173                  MLX5_CMD_OP_DESTROY_FLOW_GROUP);
174         MLX5_SET(destroy_flow_group_in, in, table_type, ft->type);
175         MLX5_SET(destroy_flow_group_in, in, table_id, ft->id);
176         MLX5_SET(destroy_flow_group_in, in, group_id, group_id);
177
178         return mlx5_cmd_exec_check_status(dev, in, sizeof(in), out,
179                                           sizeof(out));
180 }
181
182 static int mlx5_cmd_set_fte(struct mlx5_core_dev *dev,
183                             int opmod, int modify_mask,
184                             struct mlx5_flow_table *ft,
185                             unsigned group_id,
186                             struct fs_fte *fte)
187 {
188         unsigned int inlen = MLX5_ST_SZ_BYTES(set_fte_in) +
189                 fte->dests_size * MLX5_ST_SZ_BYTES(dest_format_struct);
190         u32 out[MLX5_ST_SZ_DW(set_fte_out)];
191         struct mlx5_flow_rule *dst;
192         void *in_flow_context;
193         void *in_match_value;
194         void *in_dests;
195         u32 *in;
196         int err;
197
198         in = mlx5_vzalloc(inlen);
199         if (!in) {
200                 mlx5_core_warn(dev, "failed to allocate inbox\n");
201                 return -ENOMEM;
202         }
203
204         MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
205         MLX5_SET(set_fte_in, in, op_mod, opmod);
206         MLX5_SET(set_fte_in, in, modify_enable_mask, modify_mask);
207         MLX5_SET(set_fte_in, in, table_type, ft->type);
208         MLX5_SET(set_fte_in, in, table_id,   ft->id);
209         MLX5_SET(set_fte_in, in, flow_index, fte->index);
210
211         in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
212         MLX5_SET(flow_context, in_flow_context, group_id, group_id);
213         MLX5_SET(flow_context, in_flow_context, flow_tag, fte->flow_tag);
214         MLX5_SET(flow_context, in_flow_context, action, fte->action);
215         MLX5_SET(flow_context, in_flow_context, destination_list_size,
216                  fte->dests_size);
217         in_match_value = MLX5_ADDR_OF(flow_context, in_flow_context,
218                                       match_value);
219         memcpy(in_match_value, &fte->val, MLX5_ST_SZ_BYTES(fte_match_param));
220
221         in_dests = MLX5_ADDR_OF(flow_context, in_flow_context, destination);
222         list_for_each_entry(dst, &fte->node.children, node.list) {
223                 unsigned int id;
224
225                 MLX5_SET(dest_format_struct, in_dests, destination_type,
226                          dst->dest_attr.type);
227                 if (dst->dest_attr.type ==
228                     MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
229                         id = dst->dest_attr.ft->id;
230                 else
231                         id = dst->dest_attr.tir_num;
232                 MLX5_SET(dest_format_struct, in_dests, destination_id, id);
233                 in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
234         }
235         memset(out, 0, sizeof(out));
236         err = mlx5_cmd_exec_check_status(dev, in, inlen, out,
237                                          sizeof(out));
238         kvfree(in);
239
240         return err;
241 }
242
243 int mlx5_cmd_create_fte(struct mlx5_core_dev *dev,
244                         struct mlx5_flow_table *ft,
245                         unsigned group_id,
246                         struct fs_fte *fte)
247 {
248         return  mlx5_cmd_set_fte(dev, 0, 0, ft, group_id, fte);
249 }
250
251 int mlx5_cmd_update_fte(struct mlx5_core_dev *dev,
252                         struct mlx5_flow_table *ft,
253                         unsigned group_id,
254                         struct fs_fte *fte)
255 {
256         int opmod;
257         int modify_mask;
258         int atomic_mod_cap = MLX5_CAP_FLOWTABLE(dev,
259                                                 flow_table_properties_nic_receive.
260                                                 flow_modify_en);
261         if (!atomic_mod_cap)
262                 return -ENOTSUPP;
263         opmod = 1;
264         modify_mask = 1 <<
265                 MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST;
266
267         return  mlx5_cmd_set_fte(dev, opmod, modify_mask, ft, group_id, fte);
268 }
269
270 int mlx5_cmd_delete_fte(struct mlx5_core_dev *dev,
271                         struct mlx5_flow_table *ft,
272                         unsigned int index)
273 {
274         u32 out[MLX5_ST_SZ_DW(delete_fte_out)];
275         u32 in[MLX5_ST_SZ_DW(delete_fte_in)];
276         int err;
277
278         memset(in, 0, sizeof(in));
279         memset(out, 0, sizeof(out));
280
281         MLX5_SET(delete_fte_in, in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
282         MLX5_SET(delete_fte_in, in, table_type, ft->type);
283         MLX5_SET(delete_fte_in, in, table_id, ft->id);
284         MLX5_SET(delete_fte_in, in, flow_index, index);
285
286         err =  mlx5_cmd_exec_check_status(dev, in, sizeof(in), out, sizeof(out));
287
288         return err;
289 }