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, flow_table_context.level, level);
82 MLX5_SET(create_flow_table_in, in, flow_table_context.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, flow_table_context.decap_en,
90 MLX5_SET(create_flow_table_in, in, flow_table_context.encap_en,
94 case FS_FT_OP_MOD_NORMAL:
96 MLX5_SET(create_flow_table_in, in,
97 flow_table_context.table_miss_action, 1);
98 MLX5_SET(create_flow_table_in, in,
99 flow_table_context.table_miss_id, next_ft->id);
103 case FS_FT_OP_MOD_LAG_DEMUX:
104 MLX5_SET(create_flow_table_in, in, op_mod, 0x1);
106 MLX5_SET(create_flow_table_in, in,
107 flow_table_context.lag_master_next_table_id,
112 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
114 *table_id = MLX5_GET(create_flow_table_out, out,
119 int mlx5_cmd_destroy_flow_table(struct mlx5_core_dev *dev,
120 struct mlx5_flow_table *ft)
122 u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)] = {0};
123 u32 out[MLX5_ST_SZ_DW(destroy_flow_table_out)] = {0};
125 MLX5_SET(destroy_flow_table_in, in, opcode,
126 MLX5_CMD_OP_DESTROY_FLOW_TABLE);
127 MLX5_SET(destroy_flow_table_in, in, table_type, ft->type);
128 MLX5_SET(destroy_flow_table_in, in, table_id, ft->id);
130 MLX5_SET(destroy_flow_table_in, in, vport_number, ft->vport);
131 MLX5_SET(destroy_flow_table_in, in, other_vport, 1);
134 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
137 int mlx5_cmd_modify_flow_table(struct mlx5_core_dev *dev,
138 struct mlx5_flow_table *ft,
139 struct mlx5_flow_table *next_ft)
141 u32 in[MLX5_ST_SZ_DW(modify_flow_table_in)] = {0};
142 u32 out[MLX5_ST_SZ_DW(modify_flow_table_out)] = {0};
144 MLX5_SET(modify_flow_table_in, in, opcode,
145 MLX5_CMD_OP_MODIFY_FLOW_TABLE);
146 MLX5_SET(modify_flow_table_in, in, table_type, ft->type);
147 MLX5_SET(modify_flow_table_in, in, table_id, ft->id);
149 if (ft->op_mod == FS_FT_OP_MOD_LAG_DEMUX) {
150 MLX5_SET(modify_flow_table_in, in, modify_field_select,
151 MLX5_MODIFY_FLOW_TABLE_LAG_NEXT_TABLE_ID);
153 MLX5_SET(modify_flow_table_in, in,
154 flow_table_context.lag_master_next_table_id, next_ft->id);
156 MLX5_SET(modify_flow_table_in, in,
157 flow_table_context.lag_master_next_table_id, 0);
161 MLX5_SET(modify_flow_table_in, in, vport_number,
163 MLX5_SET(modify_flow_table_in, in, other_vport, 1);
165 MLX5_SET(modify_flow_table_in, in, modify_field_select,
166 MLX5_MODIFY_FLOW_TABLE_MISS_TABLE_ID);
168 MLX5_SET(modify_flow_table_in, in,
169 flow_table_context.table_miss_action, 1);
170 MLX5_SET(modify_flow_table_in, in,
171 flow_table_context.table_miss_id,
174 MLX5_SET(modify_flow_table_in, in,
175 flow_table_context.table_miss_action, 0);
179 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
182 int mlx5_cmd_create_flow_group(struct mlx5_core_dev *dev,
183 struct mlx5_flow_table *ft,
185 unsigned int *group_id)
187 u32 out[MLX5_ST_SZ_DW(create_flow_group_out)] = {0};
188 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
191 MLX5_SET(create_flow_group_in, in, opcode,
192 MLX5_CMD_OP_CREATE_FLOW_GROUP);
193 MLX5_SET(create_flow_group_in, in, table_type, ft->type);
194 MLX5_SET(create_flow_group_in, in, table_id, ft->id);
196 MLX5_SET(create_flow_group_in, in, vport_number, ft->vport);
197 MLX5_SET(create_flow_group_in, in, other_vport, 1);
200 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
202 *group_id = MLX5_GET(create_flow_group_out, out,
207 int mlx5_cmd_destroy_flow_group(struct mlx5_core_dev *dev,
208 struct mlx5_flow_table *ft,
209 unsigned int group_id)
211 u32 out[MLX5_ST_SZ_DW(destroy_flow_group_out)] = {0};
212 u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)] = {0};
214 MLX5_SET(destroy_flow_group_in, in, opcode,
215 MLX5_CMD_OP_DESTROY_FLOW_GROUP);
216 MLX5_SET(destroy_flow_group_in, in, table_type, ft->type);
217 MLX5_SET(destroy_flow_group_in, in, table_id, ft->id);
218 MLX5_SET(destroy_flow_group_in, in, group_id, group_id);
220 MLX5_SET(destroy_flow_group_in, in, vport_number, ft->vport);
221 MLX5_SET(destroy_flow_group_in, in, other_vport, 1);
224 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
227 static int mlx5_cmd_set_fte(struct mlx5_core_dev *dev,
228 int opmod, int modify_mask,
229 struct mlx5_flow_table *ft,
233 unsigned int inlen = MLX5_ST_SZ_BYTES(set_fte_in) +
234 fte->dests_size * MLX5_ST_SZ_BYTES(dest_format_struct);
235 u32 out[MLX5_ST_SZ_DW(set_fte_out)] = {0};
236 struct mlx5_flow_rule *dst;
237 void *in_flow_context;
238 void *in_match_value;
243 in = kvzalloc(inlen, GFP_KERNEL);
247 MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
248 MLX5_SET(set_fte_in, in, op_mod, opmod);
249 MLX5_SET(set_fte_in, in, modify_enable_mask, modify_mask);
250 MLX5_SET(set_fte_in, in, table_type, ft->type);
251 MLX5_SET(set_fte_in, in, table_id, ft->id);
252 MLX5_SET(set_fte_in, in, flow_index, fte->index);
254 MLX5_SET(set_fte_in, in, vport_number, ft->vport);
255 MLX5_SET(set_fte_in, in, other_vport, 1);
258 in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
259 MLX5_SET(flow_context, in_flow_context, group_id, group_id);
260 MLX5_SET(flow_context, in_flow_context, flow_tag, fte->flow_tag);
261 MLX5_SET(flow_context, in_flow_context, action, fte->action);
262 MLX5_SET(flow_context, in_flow_context, encap_id, fte->encap_id);
263 MLX5_SET(flow_context, in_flow_context, modify_header_id, fte->modify_id);
264 in_match_value = MLX5_ADDR_OF(flow_context, in_flow_context,
266 memcpy(in_match_value, &fte->val, sizeof(fte->val));
268 in_dests = MLX5_ADDR_OF(flow_context, in_flow_context, destination);
269 if (fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
272 list_for_each_entry(dst, &fte->node.children, node.list) {
275 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
278 MLX5_SET(dest_format_struct, in_dests, destination_type,
279 dst->dest_attr.type);
280 if (dst->dest_attr.type ==
281 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
282 id = dst->dest_attr.ft->id;
284 id = dst->dest_attr.tir_num;
286 MLX5_SET(dest_format_struct, in_dests, destination_id, id);
287 in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
291 MLX5_SET(flow_context, in_flow_context, destination_list_size,
295 if (fte->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
296 int max_list_size = BIT(MLX5_CAP_FLOWTABLE_TYPE(dev,
297 log_max_flow_counter,
301 list_for_each_entry(dst, &fte->node.children, node.list) {
302 if (dst->dest_attr.type !=
303 MLX5_FLOW_DESTINATION_TYPE_COUNTER)
306 MLX5_SET(flow_counter_list, in_dests, flow_counter_id,
307 dst->dest_attr.counter->id);
308 in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
311 if (list_size > max_list_size) {
316 MLX5_SET(flow_context, in_flow_context, flow_counter_list_size,
320 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
326 int mlx5_cmd_create_fte(struct mlx5_core_dev *dev,
327 struct mlx5_flow_table *ft,
331 return mlx5_cmd_set_fte(dev, 0, 0, ft, group_id, fte);
334 int mlx5_cmd_update_fte(struct mlx5_core_dev *dev,
335 struct mlx5_flow_table *ft,
341 int atomic_mod_cap = MLX5_CAP_FLOWTABLE(dev,
342 flow_table_properties_nic_receive.
348 return mlx5_cmd_set_fte(dev, opmod, modify_mask, ft, group_id, fte);
351 int mlx5_cmd_delete_fte(struct mlx5_core_dev *dev,
352 struct mlx5_flow_table *ft,
355 u32 out[MLX5_ST_SZ_DW(delete_fte_out)] = {0};
356 u32 in[MLX5_ST_SZ_DW(delete_fte_in)] = {0};
358 MLX5_SET(delete_fte_in, in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
359 MLX5_SET(delete_fte_in, in, table_type, ft->type);
360 MLX5_SET(delete_fte_in, in, table_id, ft->id);
361 MLX5_SET(delete_fte_in, in, flow_index, index);
363 MLX5_SET(delete_fte_in, in, vport_number, ft->vport);
364 MLX5_SET(delete_fte_in, in, other_vport, 1);
367 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
370 int mlx5_cmd_fc_alloc(struct mlx5_core_dev *dev, u32 *id)
372 u32 in[MLX5_ST_SZ_DW(alloc_flow_counter_in)] = {0};
373 u32 out[MLX5_ST_SZ_DW(alloc_flow_counter_out)] = {0};
376 MLX5_SET(alloc_flow_counter_in, in, opcode,
377 MLX5_CMD_OP_ALLOC_FLOW_COUNTER);
379 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
381 *id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
385 int mlx5_cmd_fc_free(struct mlx5_core_dev *dev, u32 id)
387 u32 in[MLX5_ST_SZ_DW(dealloc_flow_counter_in)] = {0};
388 u32 out[MLX5_ST_SZ_DW(dealloc_flow_counter_out)] = {0};
390 MLX5_SET(dealloc_flow_counter_in, in, opcode,
391 MLX5_CMD_OP_DEALLOC_FLOW_COUNTER);
392 MLX5_SET(dealloc_flow_counter_in, in, flow_counter_id, id);
393 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
396 int mlx5_cmd_fc_query(struct mlx5_core_dev *dev, u32 id,
397 u64 *packets, u64 *bytes)
399 u32 out[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
400 MLX5_ST_SZ_BYTES(traffic_counter)] = {0};
401 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
405 MLX5_SET(query_flow_counter_in, in, opcode,
406 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
407 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
408 MLX5_SET(query_flow_counter_in, in, flow_counter_id, id);
409 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
413 stats = MLX5_ADDR_OF(query_flow_counter_out, out, flow_statistics);
414 *packets = MLX5_GET64(traffic_counter, stats, packets);
415 *bytes = MLX5_GET64(traffic_counter, stats, octets);
419 struct mlx5_cmd_fc_bulk {
426 struct mlx5_cmd_fc_bulk *
427 mlx5_cmd_fc_bulk_alloc(struct mlx5_core_dev *dev, u32 id, int num)
429 struct mlx5_cmd_fc_bulk *b;
431 MLX5_ST_SZ_BYTES(query_flow_counter_out) +
432 MLX5_ST_SZ_BYTES(traffic_counter) * num;
434 b = kzalloc(sizeof(*b) + outlen, GFP_KERNEL);
445 void mlx5_cmd_fc_bulk_free(struct mlx5_cmd_fc_bulk *b)
451 mlx5_cmd_fc_bulk_query(struct mlx5_core_dev *dev, struct mlx5_cmd_fc_bulk *b)
453 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
455 MLX5_SET(query_flow_counter_in, in, opcode,
456 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
457 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
458 MLX5_SET(query_flow_counter_in, in, flow_counter_id, b->id);
459 MLX5_SET(query_flow_counter_in, in, num_of_counters, b->num);
460 return mlx5_cmd_exec(dev, in, sizeof(in), b->out, b->outlen);
463 void mlx5_cmd_fc_bulk_get(struct mlx5_core_dev *dev,
464 struct mlx5_cmd_fc_bulk *b, u32 id,
465 u64 *packets, u64 *bytes)
467 int index = id - b->id;
470 if (index < 0 || index >= b->num) {
471 mlx5_core_warn(dev, "Flow counter id (0x%x) out of range (0x%x..0x%x). Counter ignored.\n",
472 id, b->id, b->id + b->num - 1);
476 stats = MLX5_ADDR_OF(query_flow_counter_out, b->out,
477 flow_statistics[index]);
478 *packets = MLX5_GET64(traffic_counter, stats, packets);
479 *bytes = MLX5_GET64(traffic_counter, stats, octets);
482 int mlx5_encap_alloc(struct mlx5_core_dev *dev,
488 int max_encap_size = MLX5_CAP_ESW(dev, max_encap_header_size);
489 u32 out[MLX5_ST_SZ_DW(alloc_encap_header_out)];
490 void *encap_header_in;
496 if (size > max_encap_size) {
497 mlx5_core_warn(dev, "encap size %zd too big, max supported is %d\n",
498 size, max_encap_size);
502 in = kzalloc(MLX5_ST_SZ_BYTES(alloc_encap_header_in) + size,
507 encap_header_in = MLX5_ADDR_OF(alloc_encap_header_in, in, encap_header);
508 header = MLX5_ADDR_OF(encap_header_in, encap_header_in, encap_header);
509 inlen = header - (void *)in + size;
511 memset(in, 0, inlen);
512 MLX5_SET(alloc_encap_header_in, in, opcode,
513 MLX5_CMD_OP_ALLOC_ENCAP_HEADER);
514 MLX5_SET(encap_header_in, encap_header_in, encap_header_size, size);
515 MLX5_SET(encap_header_in, encap_header_in, header_type, header_type);
516 memcpy(header, encap_header, size);
518 memset(out, 0, sizeof(out));
519 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
521 *encap_id = MLX5_GET(alloc_encap_header_out, out, encap_id);
526 void mlx5_encap_dealloc(struct mlx5_core_dev *dev, u32 encap_id)
528 u32 in[MLX5_ST_SZ_DW(dealloc_encap_header_in)];
529 u32 out[MLX5_ST_SZ_DW(dealloc_encap_header_out)];
531 memset(in, 0, sizeof(in));
532 MLX5_SET(dealloc_encap_header_in, in, opcode,
533 MLX5_CMD_OP_DEALLOC_ENCAP_HEADER);
534 MLX5_SET(dealloc_encap_header_in, in, encap_id, encap_id);
536 mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
539 int mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
540 u8 namespace, u8 num_actions,
541 void *modify_actions, u32 *modify_header_id)
543 u32 out[MLX5_ST_SZ_DW(alloc_modify_header_context_out)];
544 int max_actions, actions_size, inlen, err;
550 case MLX5_FLOW_NAMESPACE_FDB:
551 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(dev, max_modify_header_actions);
552 table_type = FS_FT_FDB;
554 case MLX5_FLOW_NAMESPACE_KERNEL:
555 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(dev, max_modify_header_actions);
556 table_type = FS_FT_NIC_RX;
562 if (num_actions > max_actions) {
563 mlx5_core_warn(dev, "too many modify header actions %d, max supported %d\n",
564 num_actions, max_actions);
568 actions_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto) * num_actions;
569 inlen = MLX5_ST_SZ_BYTES(alloc_modify_header_context_in) + actions_size;
571 in = kzalloc(inlen, GFP_KERNEL);
575 MLX5_SET(alloc_modify_header_context_in, in, opcode,
576 MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT);
577 MLX5_SET(alloc_modify_header_context_in, in, table_type, table_type);
578 MLX5_SET(alloc_modify_header_context_in, in, num_of_actions, num_actions);
580 actions_in = MLX5_ADDR_OF(alloc_modify_header_context_in, in, actions);
581 memcpy(actions_in, modify_actions, actions_size);
583 memset(out, 0, sizeof(out));
584 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
586 *modify_header_id = MLX5_GET(alloc_modify_header_context_out, out, modify_header_id);
591 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev, u32 modify_header_id)
593 u32 in[MLX5_ST_SZ_DW(dealloc_modify_header_context_in)];
594 u32 out[MLX5_ST_SZ_DW(dealloc_modify_header_context_out)];
596 memset(in, 0, sizeof(in));
597 MLX5_SET(dealloc_modify_header_context_in, in, opcode,
598 MLX5_CMD_OP_DEALLOC_MODIFY_HEADER_CONTEXT);
599 MLX5_SET(dealloc_modify_header_context_in, in, modify_header_id,
602 mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));