645f83cac34d7d1ea28315d844cce22bb35bab6e
[sfrench/cifs-2.6.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 #include "eswitch.h"
41
42 static int mlx5_cmd_stub_update_root_ft(struct mlx5_core_dev *dev,
43                                         struct mlx5_flow_table *ft,
44                                         u32 underlay_qpn,
45                                         bool disconnect)
46 {
47         return 0;
48 }
49
50 static int mlx5_cmd_stub_create_flow_table(struct mlx5_core_dev *dev,
51                                            u16 vport,
52                                            enum fs_flow_table_op_mod op_mod,
53                                            enum fs_flow_table_type type,
54                                            unsigned int level,
55                                            unsigned int log_size,
56                                            struct mlx5_flow_table *next_ft,
57                                            unsigned int *table_id, u32 flags)
58 {
59         return 0;
60 }
61
62 static int mlx5_cmd_stub_destroy_flow_table(struct mlx5_core_dev *dev,
63                                             struct mlx5_flow_table *ft)
64 {
65         return 0;
66 }
67
68 static int mlx5_cmd_stub_modify_flow_table(struct mlx5_core_dev *dev,
69                                            struct mlx5_flow_table *ft,
70                                            struct mlx5_flow_table *next_ft)
71 {
72         return 0;
73 }
74
75 static int mlx5_cmd_stub_create_flow_group(struct mlx5_core_dev *dev,
76                                            struct mlx5_flow_table *ft,
77                                            u32 *in,
78                                            unsigned int *group_id)
79 {
80         return 0;
81 }
82
83 static int mlx5_cmd_stub_destroy_flow_group(struct mlx5_core_dev *dev,
84                                             struct mlx5_flow_table *ft,
85                                             unsigned int group_id)
86 {
87         return 0;
88 }
89
90 static int mlx5_cmd_stub_create_fte(struct mlx5_core_dev *dev,
91                                     struct mlx5_flow_table *ft,
92                                     struct mlx5_flow_group *group,
93                                     struct fs_fte *fte)
94 {
95         return 0;
96 }
97
98 static int mlx5_cmd_stub_update_fte(struct mlx5_core_dev *dev,
99                                     struct mlx5_flow_table *ft,
100                                     unsigned int group_id,
101                                     int modify_mask,
102                                     struct fs_fte *fte)
103 {
104         return -EOPNOTSUPP;
105 }
106
107 static int mlx5_cmd_stub_delete_fte(struct mlx5_core_dev *dev,
108                                     struct mlx5_flow_table *ft,
109                                     struct fs_fte *fte)
110 {
111         return 0;
112 }
113
114 static int mlx5_cmd_update_root_ft(struct mlx5_core_dev *dev,
115                                    struct mlx5_flow_table *ft, u32 underlay_qpn,
116                                    bool disconnect)
117 {
118         u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)]   = {0};
119         u32 out[MLX5_ST_SZ_DW(set_flow_table_root_out)] = {0};
120
121         if ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
122             underlay_qpn == 0)
123                 return 0;
124
125         MLX5_SET(set_flow_table_root_in, in, opcode,
126                  MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
127         MLX5_SET(set_flow_table_root_in, in, table_type, ft->type);
128
129         if (disconnect) {
130                 MLX5_SET(set_flow_table_root_in, in, op_mod, 1);
131                 MLX5_SET(set_flow_table_root_in, in, table_id, 0);
132         } else {
133                 MLX5_SET(set_flow_table_root_in, in, op_mod, 0);
134                 MLX5_SET(set_flow_table_root_in, in, table_id, ft->id);
135         }
136
137         MLX5_SET(set_flow_table_root_in, in, underlay_qpn, underlay_qpn);
138         if (ft->vport) {
139                 MLX5_SET(set_flow_table_root_in, in, vport_number, ft->vport);
140                 MLX5_SET(set_flow_table_root_in, in, other_vport, 1);
141         }
142
143         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
144 }
145
146 static int mlx5_cmd_create_flow_table(struct mlx5_core_dev *dev,
147                                       u16 vport,
148                                       enum fs_flow_table_op_mod op_mod,
149                                       enum fs_flow_table_type type,
150                                       unsigned int level,
151                                       unsigned int log_size,
152                                       struct mlx5_flow_table *next_ft,
153                                       unsigned int *table_id, u32 flags)
154 {
155         int en_encap_decap = !!(flags & MLX5_FLOW_TABLE_TUNNEL_EN);
156         u32 out[MLX5_ST_SZ_DW(create_flow_table_out)] = {0};
157         u32 in[MLX5_ST_SZ_DW(create_flow_table_in)]   = {0};
158         int err;
159
160         MLX5_SET(create_flow_table_in, in, opcode,
161                  MLX5_CMD_OP_CREATE_FLOW_TABLE);
162
163         MLX5_SET(create_flow_table_in, in, table_type, type);
164         MLX5_SET(create_flow_table_in, in, flow_table_context.level, level);
165         MLX5_SET(create_flow_table_in, in, flow_table_context.log_size, log_size);
166         if (vport) {
167                 MLX5_SET(create_flow_table_in, in, vport_number, vport);
168                 MLX5_SET(create_flow_table_in, in, other_vport, 1);
169         }
170
171         MLX5_SET(create_flow_table_in, in, flow_table_context.decap_en,
172                  en_encap_decap);
173         MLX5_SET(create_flow_table_in, in, flow_table_context.encap_en,
174                  en_encap_decap);
175
176         switch (op_mod) {
177         case FS_FT_OP_MOD_NORMAL:
178                 if (next_ft) {
179                         MLX5_SET(create_flow_table_in, in,
180                                  flow_table_context.table_miss_action, 1);
181                         MLX5_SET(create_flow_table_in, in,
182                                  flow_table_context.table_miss_id, next_ft->id);
183                 }
184                 break;
185
186         case FS_FT_OP_MOD_LAG_DEMUX:
187                 MLX5_SET(create_flow_table_in, in, op_mod, 0x1);
188                 if (next_ft)
189                         MLX5_SET(create_flow_table_in, in,
190                                  flow_table_context.lag_master_next_table_id,
191                                  next_ft->id);
192                 break;
193         }
194
195         err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
196         if (!err)
197                 *table_id = MLX5_GET(create_flow_table_out, out,
198                                      table_id);
199         return err;
200 }
201
202 static int mlx5_cmd_destroy_flow_table(struct mlx5_core_dev *dev,
203                                        struct mlx5_flow_table *ft)
204 {
205         u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)]   = {0};
206         u32 out[MLX5_ST_SZ_DW(destroy_flow_table_out)] = {0};
207
208         MLX5_SET(destroy_flow_table_in, in, opcode,
209                  MLX5_CMD_OP_DESTROY_FLOW_TABLE);
210         MLX5_SET(destroy_flow_table_in, in, table_type, ft->type);
211         MLX5_SET(destroy_flow_table_in, in, table_id, ft->id);
212         if (ft->vport) {
213                 MLX5_SET(destroy_flow_table_in, in, vport_number, ft->vport);
214                 MLX5_SET(destroy_flow_table_in, in, other_vport, 1);
215         }
216
217         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
218 }
219
220 static int mlx5_cmd_modify_flow_table(struct mlx5_core_dev *dev,
221                                       struct mlx5_flow_table *ft,
222                                       struct mlx5_flow_table *next_ft)
223 {
224         u32 in[MLX5_ST_SZ_DW(modify_flow_table_in)]   = {0};
225         u32 out[MLX5_ST_SZ_DW(modify_flow_table_out)] = {0};
226
227         MLX5_SET(modify_flow_table_in, in, opcode,
228                  MLX5_CMD_OP_MODIFY_FLOW_TABLE);
229         MLX5_SET(modify_flow_table_in, in, table_type, ft->type);
230         MLX5_SET(modify_flow_table_in, in, table_id, ft->id);
231
232         if (ft->op_mod == FS_FT_OP_MOD_LAG_DEMUX) {
233                 MLX5_SET(modify_flow_table_in, in, modify_field_select,
234                          MLX5_MODIFY_FLOW_TABLE_LAG_NEXT_TABLE_ID);
235                 if (next_ft) {
236                         MLX5_SET(modify_flow_table_in, in,
237                                  flow_table_context.lag_master_next_table_id, next_ft->id);
238                 } else {
239                         MLX5_SET(modify_flow_table_in, in,
240                                  flow_table_context.lag_master_next_table_id, 0);
241                 }
242         } else {
243                 if (ft->vport) {
244                         MLX5_SET(modify_flow_table_in, in, vport_number,
245                                  ft->vport);
246                         MLX5_SET(modify_flow_table_in, in, other_vport, 1);
247                 }
248                 MLX5_SET(modify_flow_table_in, in, modify_field_select,
249                          MLX5_MODIFY_FLOW_TABLE_MISS_TABLE_ID);
250                 if (next_ft) {
251                         MLX5_SET(modify_flow_table_in, in,
252                                  flow_table_context.table_miss_action, 1);
253                         MLX5_SET(modify_flow_table_in, in,
254                                  flow_table_context.table_miss_id,
255                                  next_ft->id);
256                 } else {
257                         MLX5_SET(modify_flow_table_in, in,
258                                  flow_table_context.table_miss_action, 0);
259                 }
260         }
261
262         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
263 }
264
265 static int mlx5_cmd_create_flow_group(struct mlx5_core_dev *dev,
266                                       struct mlx5_flow_table *ft,
267                                       u32 *in,
268                                       unsigned int *group_id)
269 {
270         u32 out[MLX5_ST_SZ_DW(create_flow_group_out)] = {0};
271         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
272         int err;
273
274         MLX5_SET(create_flow_group_in, in, opcode,
275                  MLX5_CMD_OP_CREATE_FLOW_GROUP);
276         MLX5_SET(create_flow_group_in, in, table_type, ft->type);
277         MLX5_SET(create_flow_group_in, in, table_id, ft->id);
278         if (ft->vport) {
279                 MLX5_SET(create_flow_group_in, in, vport_number, ft->vport);
280                 MLX5_SET(create_flow_group_in, in, other_vport, 1);
281         }
282
283         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
284         if (!err)
285                 *group_id = MLX5_GET(create_flow_group_out, out,
286                                      group_id);
287         return err;
288 }
289
290 static int mlx5_cmd_destroy_flow_group(struct mlx5_core_dev *dev,
291                                        struct mlx5_flow_table *ft,
292                                        unsigned int group_id)
293 {
294         u32 out[MLX5_ST_SZ_DW(destroy_flow_group_out)] = {0};
295         u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)]   = {0};
296
297         MLX5_SET(destroy_flow_group_in, in, opcode,
298                  MLX5_CMD_OP_DESTROY_FLOW_GROUP);
299         MLX5_SET(destroy_flow_group_in, in, table_type, ft->type);
300         MLX5_SET(destroy_flow_group_in, in, table_id, ft->id);
301         MLX5_SET(destroy_flow_group_in, in, group_id, group_id);
302         if (ft->vport) {
303                 MLX5_SET(destroy_flow_group_in, in, vport_number, ft->vport);
304                 MLX5_SET(destroy_flow_group_in, in, other_vport, 1);
305         }
306
307         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
308 }
309
310 static int mlx5_cmd_set_fte(struct mlx5_core_dev *dev,
311                             int opmod, int modify_mask,
312                             struct mlx5_flow_table *ft,
313                             unsigned group_id,
314                             struct fs_fte *fte)
315 {
316         unsigned int inlen = MLX5_ST_SZ_BYTES(set_fte_in) +
317                 fte->dests_size * MLX5_ST_SZ_BYTES(dest_format_struct);
318         u32 out[MLX5_ST_SZ_DW(set_fte_out)] = {0};
319         struct mlx5_flow_rule *dst;
320         void *in_flow_context;
321         void *in_match_value;
322         void *in_dests;
323         u32 *in;
324         int err;
325
326         in = kvzalloc(inlen, GFP_KERNEL);
327         if (!in)
328                 return -ENOMEM;
329
330         MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
331         MLX5_SET(set_fte_in, in, op_mod, opmod);
332         MLX5_SET(set_fte_in, in, modify_enable_mask, modify_mask);
333         MLX5_SET(set_fte_in, in, table_type, ft->type);
334         MLX5_SET(set_fte_in, in, table_id,   ft->id);
335         MLX5_SET(set_fte_in, in, flow_index, fte->index);
336         if (ft->vport) {
337                 MLX5_SET(set_fte_in, in, vport_number, ft->vport);
338                 MLX5_SET(set_fte_in, in, other_vport, 1);
339         }
340
341         in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
342         MLX5_SET(flow_context, in_flow_context, group_id, group_id);
343         MLX5_SET(flow_context, in_flow_context, flow_tag, fte->action.flow_tag);
344         MLX5_SET(flow_context, in_flow_context, action, fte->action.action);
345         MLX5_SET(flow_context, in_flow_context, encap_id, fte->action.encap_id);
346         MLX5_SET(flow_context, in_flow_context, modify_header_id,
347                  fte->action.modify_id);
348         in_match_value = MLX5_ADDR_OF(flow_context, in_flow_context,
349                                       match_value);
350         memcpy(in_match_value, &fte->val, sizeof(fte->val));
351
352         in_dests = MLX5_ADDR_OF(flow_context, in_flow_context, destination);
353         if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
354                 int list_size = 0;
355
356                 list_for_each_entry(dst, &fte->node.children, node.list) {
357                         unsigned int id;
358
359                         if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
360                                 continue;
361
362                         MLX5_SET(dest_format_struct, in_dests, destination_type,
363                                  dst->dest_attr.type);
364                         if (dst->dest_attr.type ==
365                             MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
366                                 id = dst->dest_attr.ft->id;
367                         } else {
368                                 id = dst->dest_attr.tir_num;
369                         }
370                         MLX5_SET(dest_format_struct, in_dests, destination_id, id);
371                         in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
372                         list_size++;
373                 }
374
375                 MLX5_SET(flow_context, in_flow_context, destination_list_size,
376                          list_size);
377         }
378
379         if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
380                 int max_list_size = BIT(MLX5_CAP_FLOWTABLE_TYPE(dev,
381                                         log_max_flow_counter,
382                                         ft->type));
383                 int list_size = 0;
384
385                 list_for_each_entry(dst, &fte->node.children, node.list) {
386                         if (dst->dest_attr.type !=
387                             MLX5_FLOW_DESTINATION_TYPE_COUNTER)
388                                 continue;
389
390                         MLX5_SET(flow_counter_list, in_dests, flow_counter_id,
391                                  dst->dest_attr.counter->id);
392                         in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
393                         list_size++;
394                 }
395                 if (list_size > max_list_size) {
396                         err = -EINVAL;
397                         goto err_out;
398                 }
399
400                 MLX5_SET(flow_context, in_flow_context, flow_counter_list_size,
401                          list_size);
402         }
403
404         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
405 err_out:
406         kvfree(in);
407         return err;
408 }
409
410 static int mlx5_cmd_create_fte(struct mlx5_core_dev *dev,
411                                struct mlx5_flow_table *ft,
412                                struct mlx5_flow_group *group,
413                                struct fs_fte *fte)
414 {
415         unsigned int group_id = group->id;
416
417         return mlx5_cmd_set_fte(dev, 0, 0, ft, group_id, fte);
418 }
419
420 static int mlx5_cmd_update_fte(struct mlx5_core_dev *dev,
421                                struct mlx5_flow_table *ft,
422                                unsigned int group_id,
423                                int modify_mask,
424                                struct fs_fte *fte)
425 {
426         int opmod;
427         int atomic_mod_cap = MLX5_CAP_FLOWTABLE(dev,
428                                                 flow_table_properties_nic_receive.
429                                                 flow_modify_en);
430         if (!atomic_mod_cap)
431                 return -EOPNOTSUPP;
432         opmod = 1;
433
434         return  mlx5_cmd_set_fte(dev, opmod, modify_mask, ft, group_id, fte);
435 }
436
437 static int mlx5_cmd_delete_fte(struct mlx5_core_dev *dev,
438                                struct mlx5_flow_table *ft,
439                                struct fs_fte *fte)
440 {
441         u32 out[MLX5_ST_SZ_DW(delete_fte_out)] = {0};
442         u32 in[MLX5_ST_SZ_DW(delete_fte_in)]   = {0};
443
444         MLX5_SET(delete_fte_in, in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
445         MLX5_SET(delete_fte_in, in, table_type, ft->type);
446         MLX5_SET(delete_fte_in, in, table_id, ft->id);
447         MLX5_SET(delete_fte_in, in, flow_index, fte->index);
448         if (ft->vport) {
449                 MLX5_SET(delete_fte_in, in, vport_number, ft->vport);
450                 MLX5_SET(delete_fte_in, in, other_vport, 1);
451         }
452
453         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
454 }
455
456 int mlx5_cmd_fc_alloc(struct mlx5_core_dev *dev, u32 *id)
457 {
458         u32 in[MLX5_ST_SZ_DW(alloc_flow_counter_in)]   = {0};
459         u32 out[MLX5_ST_SZ_DW(alloc_flow_counter_out)] = {0};
460         int err;
461
462         MLX5_SET(alloc_flow_counter_in, in, opcode,
463                  MLX5_CMD_OP_ALLOC_FLOW_COUNTER);
464
465         err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
466         if (!err)
467                 *id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
468         return err;
469 }
470
471 int mlx5_cmd_fc_free(struct mlx5_core_dev *dev, u32 id)
472 {
473         u32 in[MLX5_ST_SZ_DW(dealloc_flow_counter_in)]   = {0};
474         u32 out[MLX5_ST_SZ_DW(dealloc_flow_counter_out)] = {0};
475
476         MLX5_SET(dealloc_flow_counter_in, in, opcode,
477                  MLX5_CMD_OP_DEALLOC_FLOW_COUNTER);
478         MLX5_SET(dealloc_flow_counter_in, in, flow_counter_id, id);
479         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
480 }
481
482 int mlx5_cmd_fc_query(struct mlx5_core_dev *dev, u32 id,
483                       u64 *packets, u64 *bytes)
484 {
485         u32 out[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
486                 MLX5_ST_SZ_BYTES(traffic_counter)]   = {0};
487         u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
488         void *stats;
489         int err = 0;
490
491         MLX5_SET(query_flow_counter_in, in, opcode,
492                  MLX5_CMD_OP_QUERY_FLOW_COUNTER);
493         MLX5_SET(query_flow_counter_in, in, op_mod, 0);
494         MLX5_SET(query_flow_counter_in, in, flow_counter_id, id);
495         err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
496         if (err)
497                 return err;
498
499         stats = MLX5_ADDR_OF(query_flow_counter_out, out, flow_statistics);
500         *packets = MLX5_GET64(traffic_counter, stats, packets);
501         *bytes = MLX5_GET64(traffic_counter, stats, octets);
502         return 0;
503 }
504
505 struct mlx5_cmd_fc_bulk {
506         u32 id;
507         int num;
508         int outlen;
509         u32 out[0];
510 };
511
512 struct mlx5_cmd_fc_bulk *
513 mlx5_cmd_fc_bulk_alloc(struct mlx5_core_dev *dev, u32 id, int num)
514 {
515         struct mlx5_cmd_fc_bulk *b;
516         int outlen =
517                 MLX5_ST_SZ_BYTES(query_flow_counter_out) +
518                 MLX5_ST_SZ_BYTES(traffic_counter) * num;
519
520         b = kzalloc(sizeof(*b) + outlen, GFP_KERNEL);
521         if (!b)
522                 return NULL;
523
524         b->id = id;
525         b->num = num;
526         b->outlen = outlen;
527
528         return b;
529 }
530
531 void mlx5_cmd_fc_bulk_free(struct mlx5_cmd_fc_bulk *b)
532 {
533         kfree(b);
534 }
535
536 int
537 mlx5_cmd_fc_bulk_query(struct mlx5_core_dev *dev, struct mlx5_cmd_fc_bulk *b)
538 {
539         u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
540
541         MLX5_SET(query_flow_counter_in, in, opcode,
542                  MLX5_CMD_OP_QUERY_FLOW_COUNTER);
543         MLX5_SET(query_flow_counter_in, in, op_mod, 0);
544         MLX5_SET(query_flow_counter_in, in, flow_counter_id, b->id);
545         MLX5_SET(query_flow_counter_in, in, num_of_counters, b->num);
546         return mlx5_cmd_exec(dev, in, sizeof(in), b->out, b->outlen);
547 }
548
549 void mlx5_cmd_fc_bulk_get(struct mlx5_core_dev *dev,
550                           struct mlx5_cmd_fc_bulk *b, u32 id,
551                           u64 *packets, u64 *bytes)
552 {
553         int index = id - b->id;
554         void *stats;
555
556         if (index < 0 || index >= b->num) {
557                 mlx5_core_warn(dev, "Flow counter id (0x%x) out of range (0x%x..0x%x). Counter ignored.\n",
558                                id, b->id, b->id + b->num - 1);
559                 return;
560         }
561
562         stats = MLX5_ADDR_OF(query_flow_counter_out, b->out,
563                              flow_statistics[index]);
564         *packets = MLX5_GET64(traffic_counter, stats, packets);
565         *bytes = MLX5_GET64(traffic_counter, stats, octets);
566 }
567
568 int mlx5_encap_alloc(struct mlx5_core_dev *dev,
569                      int header_type,
570                      size_t size,
571                      void *encap_header,
572                      u32 *encap_id)
573 {
574         int max_encap_size = MLX5_CAP_ESW(dev, max_encap_header_size);
575         u32 out[MLX5_ST_SZ_DW(alloc_encap_header_out)];
576         void *encap_header_in;
577         void *header;
578         int inlen;
579         int err;
580         u32 *in;
581
582         if (size > max_encap_size) {
583                 mlx5_core_warn(dev, "encap size %zd too big, max supported is %d\n",
584                                size, max_encap_size);
585                 return -EINVAL;
586         }
587
588         in = kzalloc(MLX5_ST_SZ_BYTES(alloc_encap_header_in) + size,
589                      GFP_KERNEL);
590         if (!in)
591                 return -ENOMEM;
592
593         encap_header_in = MLX5_ADDR_OF(alloc_encap_header_in, in, encap_header);
594         header = MLX5_ADDR_OF(encap_header_in, encap_header_in, encap_header);
595         inlen = header - (void *)in  + size;
596
597         memset(in, 0, inlen);
598         MLX5_SET(alloc_encap_header_in, in, opcode,
599                  MLX5_CMD_OP_ALLOC_ENCAP_HEADER);
600         MLX5_SET(encap_header_in, encap_header_in, encap_header_size, size);
601         MLX5_SET(encap_header_in, encap_header_in, header_type, header_type);
602         memcpy(header, encap_header, size);
603
604         memset(out, 0, sizeof(out));
605         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
606
607         *encap_id = MLX5_GET(alloc_encap_header_out, out, encap_id);
608         kfree(in);
609         return err;
610 }
611
612 void mlx5_encap_dealloc(struct mlx5_core_dev *dev, u32 encap_id)
613 {
614         u32 in[MLX5_ST_SZ_DW(dealloc_encap_header_in)];
615         u32 out[MLX5_ST_SZ_DW(dealloc_encap_header_out)];
616
617         memset(in, 0, sizeof(in));
618         MLX5_SET(dealloc_encap_header_in, in, opcode,
619                  MLX5_CMD_OP_DEALLOC_ENCAP_HEADER);
620         MLX5_SET(dealloc_encap_header_in, in, encap_id, encap_id);
621
622         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
623 }
624
625 int mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
626                              u8 namespace, u8 num_actions,
627                              void *modify_actions, u32 *modify_header_id)
628 {
629         u32 out[MLX5_ST_SZ_DW(alloc_modify_header_context_out)];
630         int max_actions, actions_size, inlen, err;
631         void *actions_in;
632         u8 table_type;
633         u32 *in;
634
635         switch (namespace) {
636         case MLX5_FLOW_NAMESPACE_FDB:
637                 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(dev, max_modify_header_actions);
638                 table_type = FS_FT_FDB;
639                 break;
640         case MLX5_FLOW_NAMESPACE_KERNEL:
641                 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(dev, max_modify_header_actions);
642                 table_type = FS_FT_NIC_RX;
643                 break;
644         default:
645                 return -EOPNOTSUPP;
646         }
647
648         if (num_actions > max_actions) {
649                 mlx5_core_warn(dev, "too many modify header actions %d, max supported %d\n",
650                                num_actions, max_actions);
651                 return -EOPNOTSUPP;
652         }
653
654         actions_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto) * num_actions;
655         inlen = MLX5_ST_SZ_BYTES(alloc_modify_header_context_in) + actions_size;
656
657         in = kzalloc(inlen, GFP_KERNEL);
658         if (!in)
659                 return -ENOMEM;
660
661         MLX5_SET(alloc_modify_header_context_in, in, opcode,
662                  MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT);
663         MLX5_SET(alloc_modify_header_context_in, in, table_type, table_type);
664         MLX5_SET(alloc_modify_header_context_in, in, num_of_actions, num_actions);
665
666         actions_in = MLX5_ADDR_OF(alloc_modify_header_context_in, in, actions);
667         memcpy(actions_in, modify_actions, actions_size);
668
669         memset(out, 0, sizeof(out));
670         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
671
672         *modify_header_id = MLX5_GET(alloc_modify_header_context_out, out, modify_header_id);
673         kfree(in);
674         return err;
675 }
676
677 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev, u32 modify_header_id)
678 {
679         u32 in[MLX5_ST_SZ_DW(dealloc_modify_header_context_in)];
680         u32 out[MLX5_ST_SZ_DW(dealloc_modify_header_context_out)];
681
682         memset(in, 0, sizeof(in));
683         MLX5_SET(dealloc_modify_header_context_in, in, opcode,
684                  MLX5_CMD_OP_DEALLOC_MODIFY_HEADER_CONTEXT);
685         MLX5_SET(dealloc_modify_header_context_in, in, modify_header_id,
686                  modify_header_id);
687
688         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
689 }
690
691 static const struct mlx5_flow_cmds mlx5_flow_cmds = {
692         .create_flow_table = mlx5_cmd_create_flow_table,
693         .destroy_flow_table = mlx5_cmd_destroy_flow_table,
694         .modify_flow_table = mlx5_cmd_modify_flow_table,
695         .create_flow_group = mlx5_cmd_create_flow_group,
696         .destroy_flow_group = mlx5_cmd_destroy_flow_group,
697         .create_fte = mlx5_cmd_create_fte,
698         .update_fte = mlx5_cmd_update_fte,
699         .delete_fte = mlx5_cmd_delete_fte,
700         .update_root_ft = mlx5_cmd_update_root_ft,
701 };
702
703 static const struct mlx5_flow_cmds mlx5_flow_cmd_stubs = {
704         .create_flow_table = mlx5_cmd_stub_create_flow_table,
705         .destroy_flow_table = mlx5_cmd_stub_destroy_flow_table,
706         .modify_flow_table = mlx5_cmd_stub_modify_flow_table,
707         .create_flow_group = mlx5_cmd_stub_create_flow_group,
708         .destroy_flow_group = mlx5_cmd_stub_destroy_flow_group,
709         .create_fte = mlx5_cmd_stub_create_fte,
710         .update_fte = mlx5_cmd_stub_update_fte,
711         .delete_fte = mlx5_cmd_stub_delete_fte,
712         .update_root_ft = mlx5_cmd_stub_update_root_ft,
713 };
714
715 static const struct mlx5_flow_cmds *mlx5_fs_cmd_get_fw_cmds(void)
716 {
717         return &mlx5_flow_cmds;
718 }
719
720 static const struct mlx5_flow_cmds *mlx5_fs_cmd_get_stub_cmds(void)
721 {
722         return &mlx5_flow_cmd_stubs;
723 }
724
725 const struct mlx5_flow_cmds *mlx5_fs_cmd_get_default(enum fs_flow_table_type type)
726 {
727         switch (type) {
728         case FS_FT_NIC_RX:
729         case FS_FT_ESW_EGRESS_ACL:
730         case FS_FT_ESW_INGRESS_ACL:
731         case FS_FT_FDB:
732         case FS_FT_SNIFFER_RX:
733         case FS_FT_SNIFFER_TX:
734                 return mlx5_fs_cmd_get_fw_cmds();
735         case FS_FT_NIC_TX:
736         default:
737                 return mlx5_fs_cmd_get_stub_cmds();
738         }
739 }