net/mlx5: Unify and improve command interface
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_cmd.c
CommitLineData
26a81453
MG
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
2cc43b49
MG
41int mlx5_cmd_update_root_ft(struct mlx5_core_dev *dev,
42 struct mlx5_flow_table *ft)
43{
c4f287c4
SM
44 u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)] = {0};
45 u32 out[MLX5_ST_SZ_DW(set_flow_table_root_out)] = {0};
2cc43b49
MG
46
47 MLX5_SET(set_flow_table_root_in, in, opcode,
48 MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
49 MLX5_SET(set_flow_table_root_in, in, table_type, ft->type);
50 MLX5_SET(set_flow_table_root_in, in, table_id, ft->id);
efdc810b
MHY
51 if (ft->vport) {
52 MLX5_SET(set_flow_table_root_in, in, vport_number, ft->vport);
53 MLX5_SET(set_flow_table_root_in, in, other_vport, 1);
54 }
2cc43b49 55
c4f287c4 56 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
2cc43b49
MG
57}
58
26a81453 59int mlx5_cmd_create_flow_table(struct mlx5_core_dev *dev,
efdc810b 60 u16 vport,
26a81453 61 enum fs_flow_table_type type, unsigned int level,
f90edfd2
MG
62 unsigned int log_size, struct mlx5_flow_table
63 *next_ft, unsigned int *table_id)
26a81453 64{
c4f287c4
SM
65 u32 out[MLX5_ST_SZ_DW(create_flow_table_out)] = {0};
66 u32 in[MLX5_ST_SZ_DW(create_flow_table_in)] = {0};
26a81453
MG
67 int err;
68
26a81453
MG
69 MLX5_SET(create_flow_table_in, in, opcode,
70 MLX5_CMD_OP_CREATE_FLOW_TABLE);
71
f90edfd2
MG
72 if (next_ft) {
73 MLX5_SET(create_flow_table_in, in, table_miss_mode, 1);
74 MLX5_SET(create_flow_table_in, in, table_miss_id, next_ft->id);
75 }
26a81453
MG
76 MLX5_SET(create_flow_table_in, in, table_type, type);
77 MLX5_SET(create_flow_table_in, in, level, level);
78 MLX5_SET(create_flow_table_in, in, log_size, log_size);
efdc810b
MHY
79 if (vport) {
80 MLX5_SET(create_flow_table_in, in, vport_number, vport);
81 MLX5_SET(create_flow_table_in, in, other_vport, 1);
82 }
26a81453 83
c4f287c4 84 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
26a81453
MG
85 if (!err)
86 *table_id = MLX5_GET(create_flow_table_out, out,
87 table_id);
88 return err;
89}
90
91int mlx5_cmd_destroy_flow_table(struct mlx5_core_dev *dev,
92 struct mlx5_flow_table *ft)
93{
c4f287c4
SM
94 u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)] = {0};
95 u32 out[MLX5_ST_SZ_DW(destroy_flow_table_out)] = {0};
26a81453
MG
96
97 MLX5_SET(destroy_flow_table_in, in, opcode,
98 MLX5_CMD_OP_DESTROY_FLOW_TABLE);
99 MLX5_SET(destroy_flow_table_in, in, table_type, ft->type);
100 MLX5_SET(destroy_flow_table_in, in, table_id, ft->id);
efdc810b
MHY
101 if (ft->vport) {
102 MLX5_SET(destroy_flow_table_in, in, vport_number, ft->vport);
103 MLX5_SET(destroy_flow_table_in, in, other_vport, 1);
104 }
26a81453 105
c4f287c4 106 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
26a81453
MG
107}
108
34a40e68
MG
109int mlx5_cmd_modify_flow_table(struct mlx5_core_dev *dev,
110 struct mlx5_flow_table *ft,
111 struct mlx5_flow_table *next_ft)
112{
c4f287c4
SM
113 u32 in[MLX5_ST_SZ_DW(modify_flow_table_in)] = {0};
114 u32 out[MLX5_ST_SZ_DW(modify_flow_table_out)] = {0};
34a40e68
MG
115
116 MLX5_SET(modify_flow_table_in, in, opcode,
117 MLX5_CMD_OP_MODIFY_FLOW_TABLE);
118 MLX5_SET(modify_flow_table_in, in, table_type, ft->type);
119 MLX5_SET(modify_flow_table_in, in, table_id, ft->id);
efdc810b
MHY
120 if (ft->vport) {
121 MLX5_SET(modify_flow_table_in, in, vport_number, ft->vport);
122 MLX5_SET(modify_flow_table_in, in, other_vport, 1);
123 }
34a40e68
MG
124 MLX5_SET(modify_flow_table_in, in, modify_field_select,
125 MLX5_MODIFY_FLOW_TABLE_MISS_TABLE_ID);
126 if (next_ft) {
127 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 1);
128 MLX5_SET(modify_flow_table_in, in, table_miss_id, next_ft->id);
129 } else {
130 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 0);
131 }
132
c4f287c4 133 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
34a40e68
MG
134}
135
26a81453
MG
136int 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{
c4f287c4 141 u32 out[MLX5_ST_SZ_DW(create_flow_group_out)] = {0};
26a81453 142 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
26a81453
MG
143 int err;
144
26a81453
MG
145 MLX5_SET(create_flow_group_in, in, opcode,
146 MLX5_CMD_OP_CREATE_FLOW_GROUP);
147 MLX5_SET(create_flow_group_in, in, table_type, ft->type);
148 MLX5_SET(create_flow_group_in, in, table_id, ft->id);
efdc810b
MHY
149 if (ft->vport) {
150 MLX5_SET(create_flow_group_in, in, vport_number, ft->vport);
151 MLX5_SET(create_flow_group_in, in, other_vport, 1);
152 }
26a81453 153
c4f287c4 154 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
26a81453
MG
155 if (!err)
156 *group_id = MLX5_GET(create_flow_group_out, out,
157 group_id);
26a81453
MG
158 return err;
159}
160
161int mlx5_cmd_destroy_flow_group(struct mlx5_core_dev *dev,
162 struct mlx5_flow_table *ft,
163 unsigned int group_id)
164{
c4f287c4
SM
165 u32 out[MLX5_ST_SZ_DW(destroy_flow_group_out)] = {0};
166 u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)] = {0};
26a81453
MG
167
168 MLX5_SET(destroy_flow_group_in, in, opcode,
169 MLX5_CMD_OP_DESTROY_FLOW_GROUP);
170 MLX5_SET(destroy_flow_group_in, in, table_type, ft->type);
171 MLX5_SET(destroy_flow_group_in, in, table_id, ft->id);
172 MLX5_SET(destroy_flow_group_in, in, group_id, group_id);
efdc810b
MHY
173 if (ft->vport) {
174 MLX5_SET(destroy_flow_group_in, in, vport_number, ft->vport);
175 MLX5_SET(destroy_flow_group_in, in, other_vport, 1);
176 }
26a81453 177
c4f287c4 178 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
26a81453
MG
179}
180
181static int mlx5_cmd_set_fte(struct mlx5_core_dev *dev,
182 int opmod, int modify_mask,
183 struct mlx5_flow_table *ft,
184 unsigned group_id,
185 struct fs_fte *fte)
186{
187 unsigned int inlen = MLX5_ST_SZ_BYTES(set_fte_in) +
188 fte->dests_size * MLX5_ST_SZ_BYTES(dest_format_struct);
c4f287c4 189 u32 out[MLX5_ST_SZ_DW(set_fte_out)] = {0};
26a81453
MG
190 struct mlx5_flow_rule *dst;
191 void *in_flow_context;
192 void *in_match_value;
193 void *in_dests;
194 u32 *in;
195 int err;
196
197 in = mlx5_vzalloc(inlen);
198 if (!in) {
199 mlx5_core_warn(dev, "failed to allocate inbox\n");
200 return -ENOMEM;
201 }
202
203 MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
204 MLX5_SET(set_fte_in, in, op_mod, opmod);
205 MLX5_SET(set_fte_in, in, modify_enable_mask, modify_mask);
206 MLX5_SET(set_fte_in, in, table_type, ft->type);
207 MLX5_SET(set_fte_in, in, table_id, ft->id);
208 MLX5_SET(set_fte_in, in, flow_index, fte->index);
efdc810b
MHY
209 if (ft->vport) {
210 MLX5_SET(set_fte_in, in, vport_number, ft->vport);
211 MLX5_SET(set_fte_in, in, other_vport, 1);
212 }
26a81453
MG
213
214 in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
215 MLX5_SET(flow_context, in_flow_context, group_id, group_id);
216 MLX5_SET(flow_context, in_flow_context, flow_tag, fte->flow_tag);
217 MLX5_SET(flow_context, in_flow_context, action, fte->action);
26a81453
MG
218 in_match_value = MLX5_ADDR_OF(flow_context, in_flow_context,
219 match_value);
220 memcpy(in_match_value, &fte->val, MLX5_ST_SZ_BYTES(fte_match_param));
221
bd5251db 222 in_dests = MLX5_ADDR_OF(flow_context, in_flow_context, destination);
60ab4584 223 if (fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
bd5251db
AV
224 int list_size = 0;
225
60ab4584
AV
226 list_for_each_entry(dst, &fte->node.children, node.list) {
227 unsigned int id;
228
bd5251db
AV
229 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
230 continue;
231
60ab4584
AV
232 MLX5_SET(dest_format_struct, in_dests, destination_type,
233 dst->dest_attr.type);
234 if (dst->dest_attr.type ==
235 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
236 id = dst->dest_attr.ft->id;
237 } else {
238 id = dst->dest_attr.tir_num;
239 }
240 MLX5_SET(dest_format_struct, in_dests, destination_id, id);
241 in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
bd5251db
AV
242 list_size++;
243 }
244
245 MLX5_SET(flow_context, in_flow_context, destination_list_size,
246 list_size);
247 }
248
249 if (fte->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
250 int list_size = 0;
251
252 list_for_each_entry(dst, &fte->node.children, node.list) {
253 if (dst->dest_attr.type !=
254 MLX5_FLOW_DESTINATION_TYPE_COUNTER)
255 continue;
256
257 MLX5_SET(flow_counter_list, in_dests, flow_counter_id,
258 dst->dest_attr.counter->id);
259 in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
260 list_size++;
60ab4584 261 }
bd5251db
AV
262
263 MLX5_SET(flow_context, in_flow_context, flow_counter_list_size,
264 list_size);
26a81453 265 }
bd5251db 266
c4f287c4 267 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
26a81453 268 kvfree(in);
26a81453
MG
269 return err;
270}
271
272int mlx5_cmd_create_fte(struct mlx5_core_dev *dev,
273 struct mlx5_flow_table *ft,
274 unsigned group_id,
275 struct fs_fte *fte)
276{
c4f287c4 277 return mlx5_cmd_set_fte(dev, 0, 0, ft, group_id, fte);
26a81453
MG
278}
279
280int mlx5_cmd_update_fte(struct mlx5_core_dev *dev,
281 struct mlx5_flow_table *ft,
282 unsigned group_id,
bd5251db 283 int modify_mask,
26a81453
MG
284 struct fs_fte *fte)
285{
286 int opmod;
26a81453
MG
287 int atomic_mod_cap = MLX5_CAP_FLOWTABLE(dev,
288 flow_table_properties_nic_receive.
289 flow_modify_en);
290 if (!atomic_mod_cap)
291 return -ENOTSUPP;
292 opmod = 1;
26a81453
MG
293
294 return mlx5_cmd_set_fte(dev, opmod, modify_mask, ft, group_id, fte);
295}
296
297int mlx5_cmd_delete_fte(struct mlx5_core_dev *dev,
298 struct mlx5_flow_table *ft,
299 unsigned int index)
300{
c4f287c4
SM
301 u32 out[MLX5_ST_SZ_DW(delete_fte_out)] = {0};
302 u32 in[MLX5_ST_SZ_DW(delete_fte_in)] = {0};
26a81453
MG
303
304 MLX5_SET(delete_fte_in, in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
305 MLX5_SET(delete_fte_in, in, table_type, ft->type);
306 MLX5_SET(delete_fte_in, in, table_id, ft->id);
307 MLX5_SET(delete_fte_in, in, flow_index, index);
efdc810b
MHY
308 if (ft->vport) {
309 MLX5_SET(delete_fte_in, in, vport_number, ft->vport);
310 MLX5_SET(delete_fte_in, in, other_vport, 1);
311 }
26a81453 312
c4f287c4 313 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
26a81453 314}
9dc0b289
AV
315
316int mlx5_cmd_fc_alloc(struct mlx5_core_dev *dev, u16 *id)
317{
c4f287c4
SM
318 u32 in[MLX5_ST_SZ_DW(alloc_flow_counter_in)] = {0};
319 u32 out[MLX5_ST_SZ_DW(alloc_flow_counter_out)] = {0};
9dc0b289
AV
320 int err;
321
9dc0b289
AV
322 MLX5_SET(alloc_flow_counter_in, in, opcode,
323 MLX5_CMD_OP_ALLOC_FLOW_COUNTER);
324
c4f287c4
SM
325 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
326 if (!err)
327 *id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
328 return err;
9dc0b289
AV
329}
330
331int mlx5_cmd_fc_free(struct mlx5_core_dev *dev, u16 id)
332{
c4f287c4
SM
333 u32 in[MLX5_ST_SZ_DW(dealloc_flow_counter_in)] = {0};
334 u32 out[MLX5_ST_SZ_DW(dealloc_flow_counter_out)] = {0};
9dc0b289
AV
335
336 MLX5_SET(dealloc_flow_counter_in, in, opcode,
337 MLX5_CMD_OP_DEALLOC_FLOW_COUNTER);
338 MLX5_SET(dealloc_flow_counter_in, in, flow_counter_id, id);
c4f287c4 339 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
9dc0b289
AV
340}
341
342int mlx5_cmd_fc_query(struct mlx5_core_dev *dev, u16 id,
343 u64 *packets, u64 *bytes)
344{
345 u32 out[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
c4f287c4
SM
346 MLX5_ST_SZ_BYTES(traffic_counter)] = {0};
347 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
9dc0b289
AV
348 void *stats;
349 int err = 0;
350
9dc0b289
AV
351 MLX5_SET(query_flow_counter_in, in, opcode,
352 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
353 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
354 MLX5_SET(query_flow_counter_in, in, flow_counter_id, id);
c4f287c4 355 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
9dc0b289
AV
356 if (err)
357 return err;
358
359 stats = MLX5_ADDR_OF(query_flow_counter_out, out, flow_statistics);
360 *packets = MLX5_GET64(traffic_counter, stats, packets);
361 *bytes = MLX5_GET64(traffic_counter, stats, octets);
9dc0b289
AV
362 return 0;
363}
a351a1b0
AV
364
365struct mlx5_cmd_fc_bulk {
366 u16 id;
367 int num;
368 int outlen;
369 u32 out[0];
370};
371
372struct mlx5_cmd_fc_bulk *
373mlx5_cmd_fc_bulk_alloc(struct mlx5_core_dev *dev, u16 id, int num)
374{
375 struct mlx5_cmd_fc_bulk *b;
376 int outlen = sizeof(*b) +
377 MLX5_ST_SZ_BYTES(query_flow_counter_out) +
378 MLX5_ST_SZ_BYTES(traffic_counter) * num;
379
380 b = kzalloc(outlen, GFP_KERNEL);
381 if (!b)
382 return NULL;
383
384 b->id = id;
385 b->num = num;
386 b->outlen = outlen;
387
388 return b;
389}
390
391void mlx5_cmd_fc_bulk_free(struct mlx5_cmd_fc_bulk *b)
392{
393 kfree(b);
394}
395
396int
397mlx5_cmd_fc_bulk_query(struct mlx5_core_dev *dev, struct mlx5_cmd_fc_bulk *b)
398{
c4f287c4 399 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
a351a1b0
AV
400
401 MLX5_SET(query_flow_counter_in, in, opcode,
402 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
403 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
404 MLX5_SET(query_flow_counter_in, in, flow_counter_id, b->id);
405 MLX5_SET(query_flow_counter_in, in, num_of_counters, b->num);
c4f287c4 406 return mlx5_cmd_exec(dev, in, sizeof(in), b->out, b->outlen);
a351a1b0
AV
407}
408
409void mlx5_cmd_fc_bulk_get(struct mlx5_core_dev *dev,
410 struct mlx5_cmd_fc_bulk *b, u16 id,
411 u64 *packets, u64 *bytes)
412{
413 int index = id - b->id;
414 void *stats;
415
416 if (index < 0 || index >= b->num) {
417 mlx5_core_warn(dev, "Flow counter id (0x%x) out of range (0x%x..0x%x). Counter ignored.\n",
418 id, b->id, b->id + b->num - 1);
419 return;
420 }
421
422 stats = MLX5_ADDR_OF(query_flow_counter_out, b->out,
423 flow_statistics[index]);
424 *packets = MLX5_GET64(traffic_counter, stats, packets);
425 *bytes = MLX5_GET64(traffic_counter, stats, octets);
426}