2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/mlx5/driver.h>
34 #include <linux/mlx5/device.h>
35 #include <linux/mlx5/mlx5_ifc.h>
39 #include "mlx5_core.h"
42 int mlx5_cmd_update_root_ft(struct mlx5_core_dev *dev,
43 struct mlx5_flow_table *ft, u32 underlay_qpn)
45 u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)] = {0};
46 u32 out[MLX5_ST_SZ_DW(set_flow_table_root_out)] = {0};
48 if ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
52 MLX5_SET(set_flow_table_root_in, in, opcode,
53 MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
54 MLX5_SET(set_flow_table_root_in, in, table_type, ft->type);
55 MLX5_SET(set_flow_table_root_in, in, table_id, ft->id);
56 MLX5_SET(set_flow_table_root_in, in, underlay_qpn, underlay_qpn);
58 MLX5_SET(set_flow_table_root_in, in, vport_number, ft->vport);
59 MLX5_SET(set_flow_table_root_in, in, other_vport, 1);
62 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
65 int mlx5_cmd_create_flow_table(struct mlx5_core_dev *dev,
67 enum fs_flow_table_op_mod op_mod,
68 enum fs_flow_table_type type, unsigned int level,
69 unsigned int log_size, struct mlx5_flow_table
70 *next_ft, unsigned int *table_id, u32 flags)
72 int en_encap_decap = !!(flags & MLX5_FLOW_TABLE_TUNNEL_EN);
73 u32 out[MLX5_ST_SZ_DW(create_flow_table_out)] = {0};
74 u32 in[MLX5_ST_SZ_DW(create_flow_table_in)] = {0};
77 MLX5_SET(create_flow_table_in, in, opcode,
78 MLX5_CMD_OP_CREATE_FLOW_TABLE);
80 MLX5_SET(create_flow_table_in, in, table_type, type);
81 MLX5_SET(create_flow_table_in, in, level, level);
82 MLX5_SET(create_flow_table_in, in, log_size, log_size);
84 MLX5_SET(create_flow_table_in, in, vport_number, vport);
85 MLX5_SET(create_flow_table_in, in, other_vport, 1);
88 MLX5_SET(create_flow_table_in, in, decap_en, en_encap_decap);
89 MLX5_SET(create_flow_table_in, in, encap_en, en_encap_decap);
92 case FS_FT_OP_MOD_NORMAL:
94 MLX5_SET(create_flow_table_in, in, table_miss_mode, 1);
95 MLX5_SET(create_flow_table_in, in, table_miss_id, next_ft->id);
99 case FS_FT_OP_MOD_LAG_DEMUX:
100 MLX5_SET(create_flow_table_in, in, op_mod, 0x1);
102 MLX5_SET(create_flow_table_in, in, lag_master_next_table_id,
107 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
109 *table_id = MLX5_GET(create_flow_table_out, out,
114 int mlx5_cmd_destroy_flow_table(struct mlx5_core_dev *dev,
115 struct mlx5_flow_table *ft)
117 u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)] = {0};
118 u32 out[MLX5_ST_SZ_DW(destroy_flow_table_out)] = {0};
120 MLX5_SET(destroy_flow_table_in, in, opcode,
121 MLX5_CMD_OP_DESTROY_FLOW_TABLE);
122 MLX5_SET(destroy_flow_table_in, in, table_type, ft->type);
123 MLX5_SET(destroy_flow_table_in, in, table_id, ft->id);
125 MLX5_SET(destroy_flow_table_in, in, vport_number, ft->vport);
126 MLX5_SET(destroy_flow_table_in, in, other_vport, 1);
129 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
132 int mlx5_cmd_modify_flow_table(struct mlx5_core_dev *dev,
133 struct mlx5_flow_table *ft,
134 struct mlx5_flow_table *next_ft)
136 u32 in[MLX5_ST_SZ_DW(modify_flow_table_in)] = {0};
137 u32 out[MLX5_ST_SZ_DW(modify_flow_table_out)] = {0};
139 MLX5_SET(modify_flow_table_in, in, opcode,
140 MLX5_CMD_OP_MODIFY_FLOW_TABLE);
141 MLX5_SET(modify_flow_table_in, in, table_type, ft->type);
142 MLX5_SET(modify_flow_table_in, in, table_id, ft->id);
144 if (ft->op_mod == FS_FT_OP_MOD_LAG_DEMUX) {
145 MLX5_SET(modify_flow_table_in, in, modify_field_select,
146 MLX5_MODIFY_FLOW_TABLE_LAG_NEXT_TABLE_ID);
148 MLX5_SET(modify_flow_table_in, in,
149 lag_master_next_table_id, next_ft->id);
151 MLX5_SET(modify_flow_table_in, in,
152 lag_master_next_table_id, 0);
156 MLX5_SET(modify_flow_table_in, in, vport_number,
158 MLX5_SET(modify_flow_table_in, in, other_vport, 1);
160 MLX5_SET(modify_flow_table_in, in, modify_field_select,
161 MLX5_MODIFY_FLOW_TABLE_MISS_TABLE_ID);
163 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 1);
164 MLX5_SET(modify_flow_table_in, in, table_miss_id,
167 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 0);
171 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
174 int mlx5_cmd_create_flow_group(struct mlx5_core_dev *dev,
175 struct mlx5_flow_table *ft,
177 unsigned int *group_id)
179 u32 out[MLX5_ST_SZ_DW(create_flow_group_out)] = {0};
180 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
183 MLX5_SET(create_flow_group_in, in, opcode,
184 MLX5_CMD_OP_CREATE_FLOW_GROUP);
185 MLX5_SET(create_flow_group_in, in, table_type, ft->type);
186 MLX5_SET(create_flow_group_in, in, table_id, ft->id);
188 MLX5_SET(create_flow_group_in, in, vport_number, ft->vport);
189 MLX5_SET(create_flow_group_in, in, other_vport, 1);
192 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
194 *group_id = MLX5_GET(create_flow_group_out, out,
199 int mlx5_cmd_destroy_flow_group(struct mlx5_core_dev *dev,
200 struct mlx5_flow_table *ft,
201 unsigned int group_id)
203 u32 out[MLX5_ST_SZ_DW(destroy_flow_group_out)] = {0};
204 u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)] = {0};
206 MLX5_SET(destroy_flow_group_in, in, opcode,
207 MLX5_CMD_OP_DESTROY_FLOW_GROUP);
208 MLX5_SET(destroy_flow_group_in, in, table_type, ft->type);
209 MLX5_SET(destroy_flow_group_in, in, table_id, ft->id);
210 MLX5_SET(destroy_flow_group_in, in, group_id, group_id);
212 MLX5_SET(destroy_flow_group_in, in, vport_number, ft->vport);
213 MLX5_SET(destroy_flow_group_in, in, other_vport, 1);
216 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
219 static int mlx5_cmd_set_fte(struct mlx5_core_dev *dev,
220 int opmod, int modify_mask,
221 struct mlx5_flow_table *ft,
225 unsigned int inlen = MLX5_ST_SZ_BYTES(set_fte_in) +
226 fte->dests_size * MLX5_ST_SZ_BYTES(dest_format_struct);
227 u32 out[MLX5_ST_SZ_DW(set_fte_out)] = {0};
228 struct mlx5_flow_rule *dst;
229 void *in_flow_context;
230 void *in_match_value;
235 in = mlx5_vzalloc(inlen);
237 mlx5_core_warn(dev, "failed to allocate inbox\n");
241 MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
242 MLX5_SET(set_fte_in, in, op_mod, opmod);
243 MLX5_SET(set_fte_in, in, modify_enable_mask, modify_mask);
244 MLX5_SET(set_fte_in, in, table_type, ft->type);
245 MLX5_SET(set_fte_in, in, table_id, ft->id);
246 MLX5_SET(set_fte_in, in, flow_index, fte->index);
248 MLX5_SET(set_fte_in, in, vport_number, ft->vport);
249 MLX5_SET(set_fte_in, in, other_vport, 1);
252 in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
253 MLX5_SET(flow_context, in_flow_context, group_id, group_id);
254 MLX5_SET(flow_context, in_flow_context, flow_tag, fte->flow_tag);
255 MLX5_SET(flow_context, in_flow_context, action, fte->action);
256 MLX5_SET(flow_context, in_flow_context, encap_id, fte->encap_id);
257 MLX5_SET(flow_context, in_flow_context, modify_header_id, fte->modify_id);
258 in_match_value = MLX5_ADDR_OF(flow_context, in_flow_context,
260 memcpy(in_match_value, &fte->val, MLX5_ST_SZ_BYTES(fte_match_param));
262 in_dests = MLX5_ADDR_OF(flow_context, in_flow_context, destination);
263 if (fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
266 list_for_each_entry(dst, &fte->node.children, node.list) {
269 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
272 MLX5_SET(dest_format_struct, in_dests, destination_type,
273 dst->dest_attr.type);
274 if (dst->dest_attr.type ==
275 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
276 id = dst->dest_attr.ft->id;
278 id = dst->dest_attr.tir_num;
280 MLX5_SET(dest_format_struct, in_dests, destination_id, id);
281 in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
285 MLX5_SET(flow_context, in_flow_context, destination_list_size,
289 if (fte->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
292 list_for_each_entry(dst, &fte->node.children, node.list) {
293 if (dst->dest_attr.type !=
294 MLX5_FLOW_DESTINATION_TYPE_COUNTER)
297 MLX5_SET(flow_counter_list, in_dests, flow_counter_id,
298 dst->dest_attr.counter->id);
299 in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
303 MLX5_SET(flow_context, in_flow_context, flow_counter_list_size,
307 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
312 int mlx5_cmd_create_fte(struct mlx5_core_dev *dev,
313 struct mlx5_flow_table *ft,
317 return mlx5_cmd_set_fte(dev, 0, 0, ft, group_id, fte);
320 int mlx5_cmd_update_fte(struct mlx5_core_dev *dev,
321 struct mlx5_flow_table *ft,
327 int atomic_mod_cap = MLX5_CAP_FLOWTABLE(dev,
328 flow_table_properties_nic_receive.
334 return mlx5_cmd_set_fte(dev, opmod, modify_mask, ft, group_id, fte);
337 int mlx5_cmd_delete_fte(struct mlx5_core_dev *dev,
338 struct mlx5_flow_table *ft,
341 u32 out[MLX5_ST_SZ_DW(delete_fte_out)] = {0};
342 u32 in[MLX5_ST_SZ_DW(delete_fte_in)] = {0};
344 MLX5_SET(delete_fte_in, in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
345 MLX5_SET(delete_fte_in, in, table_type, ft->type);
346 MLX5_SET(delete_fte_in, in, table_id, ft->id);
347 MLX5_SET(delete_fte_in, in, flow_index, index);
349 MLX5_SET(delete_fte_in, in, vport_number, ft->vport);
350 MLX5_SET(delete_fte_in, in, other_vport, 1);
353 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
356 int mlx5_cmd_fc_alloc(struct mlx5_core_dev *dev, u16 *id)
358 u32 in[MLX5_ST_SZ_DW(alloc_flow_counter_in)] = {0};
359 u32 out[MLX5_ST_SZ_DW(alloc_flow_counter_out)] = {0};
362 MLX5_SET(alloc_flow_counter_in, in, opcode,
363 MLX5_CMD_OP_ALLOC_FLOW_COUNTER);
365 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
367 *id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
371 int mlx5_cmd_fc_free(struct mlx5_core_dev *dev, u16 id)
373 u32 in[MLX5_ST_SZ_DW(dealloc_flow_counter_in)] = {0};
374 u32 out[MLX5_ST_SZ_DW(dealloc_flow_counter_out)] = {0};
376 MLX5_SET(dealloc_flow_counter_in, in, opcode,
377 MLX5_CMD_OP_DEALLOC_FLOW_COUNTER);
378 MLX5_SET(dealloc_flow_counter_in, in, flow_counter_id, id);
379 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
382 int mlx5_cmd_fc_query(struct mlx5_core_dev *dev, u16 id,
383 u64 *packets, u64 *bytes)
385 u32 out[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
386 MLX5_ST_SZ_BYTES(traffic_counter)] = {0};
387 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
391 MLX5_SET(query_flow_counter_in, in, opcode,
392 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
393 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
394 MLX5_SET(query_flow_counter_in, in, flow_counter_id, id);
395 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
399 stats = MLX5_ADDR_OF(query_flow_counter_out, out, flow_statistics);
400 *packets = MLX5_GET64(traffic_counter, stats, packets);
401 *bytes = MLX5_GET64(traffic_counter, stats, octets);
405 struct mlx5_cmd_fc_bulk {
412 struct mlx5_cmd_fc_bulk *
413 mlx5_cmd_fc_bulk_alloc(struct mlx5_core_dev *dev, u16 id, int num)
415 struct mlx5_cmd_fc_bulk *b;
417 MLX5_ST_SZ_BYTES(query_flow_counter_out) +
418 MLX5_ST_SZ_BYTES(traffic_counter) * num;
420 b = kzalloc(sizeof(*b) + outlen, GFP_KERNEL);
431 void mlx5_cmd_fc_bulk_free(struct mlx5_cmd_fc_bulk *b)
437 mlx5_cmd_fc_bulk_query(struct mlx5_core_dev *dev, struct mlx5_cmd_fc_bulk *b)
439 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
441 MLX5_SET(query_flow_counter_in, in, opcode,
442 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
443 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
444 MLX5_SET(query_flow_counter_in, in, flow_counter_id, b->id);
445 MLX5_SET(query_flow_counter_in, in, num_of_counters, b->num);
446 return mlx5_cmd_exec(dev, in, sizeof(in), b->out, b->outlen);
449 void mlx5_cmd_fc_bulk_get(struct mlx5_core_dev *dev,
450 struct mlx5_cmd_fc_bulk *b, u16 id,
451 u64 *packets, u64 *bytes)
453 int index = id - b->id;
456 if (index < 0 || index >= b->num) {
457 mlx5_core_warn(dev, "Flow counter id (0x%x) out of range (0x%x..0x%x). Counter ignored.\n",
458 id, b->id, b->id + b->num - 1);
462 stats = MLX5_ADDR_OF(query_flow_counter_out, b->out,
463 flow_statistics[index]);
464 *packets = MLX5_GET64(traffic_counter, stats, packets);
465 *bytes = MLX5_GET64(traffic_counter, stats, octets);
468 int mlx5_encap_alloc(struct mlx5_core_dev *dev,
474 int max_encap_size = MLX5_CAP_ESW(dev, max_encap_header_size);
475 u32 out[MLX5_ST_SZ_DW(alloc_encap_header_out)];
476 void *encap_header_in;
482 if (size > max_encap_size) {
483 mlx5_core_warn(dev, "encap size %zd too big, max supported is %d\n",
484 size, max_encap_size);
488 in = kzalloc(MLX5_ST_SZ_BYTES(alloc_encap_header_in) + size,
493 encap_header_in = MLX5_ADDR_OF(alloc_encap_header_in, in, encap_header);
494 header = MLX5_ADDR_OF(encap_header_in, encap_header_in, encap_header);
495 inlen = header - (void *)in + size;
497 memset(in, 0, inlen);
498 MLX5_SET(alloc_encap_header_in, in, opcode,
499 MLX5_CMD_OP_ALLOC_ENCAP_HEADER);
500 MLX5_SET(encap_header_in, encap_header_in, encap_header_size, size);
501 MLX5_SET(encap_header_in, encap_header_in, header_type, header_type);
502 memcpy(header, encap_header, size);
504 memset(out, 0, sizeof(out));
505 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
507 *encap_id = MLX5_GET(alloc_encap_header_out, out, encap_id);
512 void mlx5_encap_dealloc(struct mlx5_core_dev *dev, u32 encap_id)
514 u32 in[MLX5_ST_SZ_DW(dealloc_encap_header_in)];
515 u32 out[MLX5_ST_SZ_DW(dealloc_encap_header_out)];
517 memset(in, 0, sizeof(in));
518 MLX5_SET(dealloc_encap_header_in, in, opcode,
519 MLX5_CMD_OP_DEALLOC_ENCAP_HEADER);
520 MLX5_SET(dealloc_encap_header_in, in, encap_id, encap_id);
522 mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
525 int mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
526 u8 namespace, u8 num_actions,
527 void *modify_actions, u32 *modify_header_id)
529 u32 out[MLX5_ST_SZ_DW(alloc_modify_header_context_out)];
530 int max_actions, actions_size, inlen, err;
536 case MLX5_FLOW_NAMESPACE_FDB:
537 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(dev, max_modify_header_actions);
538 table_type = FS_FT_FDB;
540 case MLX5_FLOW_NAMESPACE_KERNEL:
541 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(dev, max_modify_header_actions);
542 table_type = FS_FT_NIC_RX;
548 if (num_actions > max_actions) {
549 mlx5_core_warn(dev, "too many modify header actions %d, max supported %d\n",
550 num_actions, max_actions);
554 actions_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto) * num_actions;
555 inlen = MLX5_ST_SZ_BYTES(alloc_modify_header_context_in) + actions_size;
557 in = kzalloc(inlen, GFP_KERNEL);
561 MLX5_SET(alloc_modify_header_context_in, in, opcode,
562 MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT);
563 MLX5_SET(alloc_modify_header_context_in, in, table_type, table_type);
564 MLX5_SET(alloc_modify_header_context_in, in, num_of_actions, num_actions);
566 actions_in = MLX5_ADDR_OF(alloc_modify_header_context_in, in, actions);
567 memcpy(actions_in, modify_actions, actions_size);
569 memset(out, 0, sizeof(out));
570 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
572 *modify_header_id = MLX5_GET(alloc_modify_header_context_out, out, modify_header_id);
577 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev, u32 modify_header_id)
579 u32 in[MLX5_ST_SZ_DW(dealloc_modify_header_context_in)];
580 u32 out[MLX5_ST_SZ_DW(dealloc_modify_header_context_out)];
582 memset(in, 0, sizeof(in));
583 MLX5_SET(dealloc_modify_header_context_in, in, opcode,
584 MLX5_CMD_OP_DEALLOC_MODIFY_HEADER_CONTEXT);
585 MLX5_SET(dealloc_modify_header_context_in, in, modify_header_id,
588 mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));