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