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)
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) &&
49 ft->underlay_qpn == 0)
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);
57 MLX5_SET(set_flow_table_root_in, in, vport_number, ft->vport);
58 MLX5_SET(set_flow_table_root_in, in, other_vport, 1);
61 if ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
62 ft->underlay_qpn != 0)
63 MLX5_SET(set_flow_table_root_in, in, underlay_qpn, ft->underlay_qpn);
65 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
68 int mlx5_cmd_create_flow_table(struct mlx5_core_dev *dev,
70 enum fs_flow_table_op_mod op_mod,
71 enum fs_flow_table_type type, unsigned int level,
72 unsigned int log_size, struct mlx5_flow_table
73 *next_ft, unsigned int *table_id, u32 flags)
75 int en_encap_decap = !!(flags & MLX5_FLOW_TABLE_TUNNEL_EN);
76 u32 out[MLX5_ST_SZ_DW(create_flow_table_out)] = {0};
77 u32 in[MLX5_ST_SZ_DW(create_flow_table_in)] = {0};
80 MLX5_SET(create_flow_table_in, in, opcode,
81 MLX5_CMD_OP_CREATE_FLOW_TABLE);
83 MLX5_SET(create_flow_table_in, in, table_type, type);
84 MLX5_SET(create_flow_table_in, in, level, level);
85 MLX5_SET(create_flow_table_in, in, log_size, log_size);
87 MLX5_SET(create_flow_table_in, in, vport_number, vport);
88 MLX5_SET(create_flow_table_in, in, other_vport, 1);
91 MLX5_SET(create_flow_table_in, in, decap_en, en_encap_decap);
92 MLX5_SET(create_flow_table_in, in, encap_en, en_encap_decap);
95 case FS_FT_OP_MOD_NORMAL:
97 MLX5_SET(create_flow_table_in, in, table_miss_mode, 1);
98 MLX5_SET(create_flow_table_in, in, table_miss_id, next_ft->id);
102 case FS_FT_OP_MOD_LAG_DEMUX:
103 MLX5_SET(create_flow_table_in, in, op_mod, 0x1);
105 MLX5_SET(create_flow_table_in, in, lag_master_next_table_id,
110 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
112 *table_id = MLX5_GET(create_flow_table_out, out,
117 int mlx5_cmd_destroy_flow_table(struct mlx5_core_dev *dev,
118 struct mlx5_flow_table *ft)
120 u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)] = {0};
121 u32 out[MLX5_ST_SZ_DW(destroy_flow_table_out)] = {0};
123 MLX5_SET(destroy_flow_table_in, in, opcode,
124 MLX5_CMD_OP_DESTROY_FLOW_TABLE);
125 MLX5_SET(destroy_flow_table_in, in, table_type, ft->type);
126 MLX5_SET(destroy_flow_table_in, in, table_id, ft->id);
128 MLX5_SET(destroy_flow_table_in, in, vport_number, ft->vport);
129 MLX5_SET(destroy_flow_table_in, in, other_vport, 1);
132 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
135 int mlx5_cmd_modify_flow_table(struct mlx5_core_dev *dev,
136 struct mlx5_flow_table *ft,
137 struct mlx5_flow_table *next_ft)
139 u32 in[MLX5_ST_SZ_DW(modify_flow_table_in)] = {0};
140 u32 out[MLX5_ST_SZ_DW(modify_flow_table_out)] = {0};
142 MLX5_SET(modify_flow_table_in, in, opcode,
143 MLX5_CMD_OP_MODIFY_FLOW_TABLE);
144 MLX5_SET(modify_flow_table_in, in, table_type, ft->type);
145 MLX5_SET(modify_flow_table_in, in, table_id, ft->id);
147 if (ft->op_mod == FS_FT_OP_MOD_LAG_DEMUX) {
148 MLX5_SET(modify_flow_table_in, in, modify_field_select,
149 MLX5_MODIFY_FLOW_TABLE_LAG_NEXT_TABLE_ID);
151 MLX5_SET(modify_flow_table_in, in,
152 lag_master_next_table_id, next_ft->id);
154 MLX5_SET(modify_flow_table_in, in,
155 lag_master_next_table_id, 0);
159 MLX5_SET(modify_flow_table_in, in, vport_number,
161 MLX5_SET(modify_flow_table_in, in, other_vport, 1);
163 MLX5_SET(modify_flow_table_in, in, modify_field_select,
164 MLX5_MODIFY_FLOW_TABLE_MISS_TABLE_ID);
166 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 1);
167 MLX5_SET(modify_flow_table_in, in, table_miss_id,
170 MLX5_SET(modify_flow_table_in, in, table_miss_mode, 0);
174 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
177 int mlx5_cmd_create_flow_group(struct mlx5_core_dev *dev,
178 struct mlx5_flow_table *ft,
180 unsigned int *group_id)
182 u32 out[MLX5_ST_SZ_DW(create_flow_group_out)] = {0};
183 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
186 MLX5_SET(create_flow_group_in, in, opcode,
187 MLX5_CMD_OP_CREATE_FLOW_GROUP);
188 MLX5_SET(create_flow_group_in, in, table_type, ft->type);
189 MLX5_SET(create_flow_group_in, in, table_id, ft->id);
191 MLX5_SET(create_flow_group_in, in, vport_number, ft->vport);
192 MLX5_SET(create_flow_group_in, in, other_vport, 1);
195 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
197 *group_id = MLX5_GET(create_flow_group_out, out,
202 int mlx5_cmd_destroy_flow_group(struct mlx5_core_dev *dev,
203 struct mlx5_flow_table *ft,
204 unsigned int group_id)
206 u32 out[MLX5_ST_SZ_DW(destroy_flow_group_out)] = {0};
207 u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)] = {0};
209 MLX5_SET(destroy_flow_group_in, in, opcode,
210 MLX5_CMD_OP_DESTROY_FLOW_GROUP);
211 MLX5_SET(destroy_flow_group_in, in, table_type, ft->type);
212 MLX5_SET(destroy_flow_group_in, in, table_id, ft->id);
213 MLX5_SET(destroy_flow_group_in, in, group_id, group_id);
215 MLX5_SET(destroy_flow_group_in, in, vport_number, ft->vport);
216 MLX5_SET(destroy_flow_group_in, in, other_vport, 1);
219 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
222 static int mlx5_cmd_set_fte(struct mlx5_core_dev *dev,
223 int opmod, int modify_mask,
224 struct mlx5_flow_table *ft,
228 unsigned int inlen = MLX5_ST_SZ_BYTES(set_fte_in) +
229 fte->dests_size * MLX5_ST_SZ_BYTES(dest_format_struct);
230 u32 out[MLX5_ST_SZ_DW(set_fte_out)] = {0};
231 struct mlx5_flow_rule *dst;
232 void *in_flow_context;
233 void *in_match_value;
238 in = mlx5_vzalloc(inlen);
240 mlx5_core_warn(dev, "failed to allocate inbox\n");
244 MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
245 MLX5_SET(set_fte_in, in, op_mod, opmod);
246 MLX5_SET(set_fte_in, in, modify_enable_mask, modify_mask);
247 MLX5_SET(set_fte_in, in, table_type, ft->type);
248 MLX5_SET(set_fte_in, in, table_id, ft->id);
249 MLX5_SET(set_fte_in, in, flow_index, fte->index);
251 MLX5_SET(set_fte_in, in, vport_number, ft->vport);
252 MLX5_SET(set_fte_in, in, other_vport, 1);
255 in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
256 MLX5_SET(flow_context, in_flow_context, group_id, group_id);
257 MLX5_SET(flow_context, in_flow_context, flow_tag, fte->flow_tag);
258 MLX5_SET(flow_context, in_flow_context, action, fte->action);
259 MLX5_SET(flow_context, in_flow_context, encap_id, fte->encap_id);
260 MLX5_SET(flow_context, in_flow_context, modify_header_id, fte->modify_id);
261 in_match_value = MLX5_ADDR_OF(flow_context, in_flow_context,
263 memcpy(in_match_value, &fte->val, MLX5_ST_SZ_BYTES(fte_match_param));
265 in_dests = MLX5_ADDR_OF(flow_context, in_flow_context, destination);
266 if (fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
269 list_for_each_entry(dst, &fte->node.children, node.list) {
272 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
275 MLX5_SET(dest_format_struct, in_dests, destination_type,
276 dst->dest_attr.type);
277 if (dst->dest_attr.type ==
278 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
279 id = dst->dest_attr.ft->id;
281 id = dst->dest_attr.tir_num;
283 MLX5_SET(dest_format_struct, in_dests, destination_id, id);
284 in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
288 MLX5_SET(flow_context, in_flow_context, destination_list_size,
292 if (fte->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
295 list_for_each_entry(dst, &fte->node.children, node.list) {
296 if (dst->dest_attr.type !=
297 MLX5_FLOW_DESTINATION_TYPE_COUNTER)
300 MLX5_SET(flow_counter_list, in_dests, flow_counter_id,
301 dst->dest_attr.counter->id);
302 in_dests += MLX5_ST_SZ_BYTES(dest_format_struct);
306 MLX5_SET(flow_context, in_flow_context, flow_counter_list_size,
310 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
315 int mlx5_cmd_create_fte(struct mlx5_core_dev *dev,
316 struct mlx5_flow_table *ft,
320 return mlx5_cmd_set_fte(dev, 0, 0, ft, group_id, fte);
323 int mlx5_cmd_update_fte(struct mlx5_core_dev *dev,
324 struct mlx5_flow_table *ft,
330 int atomic_mod_cap = MLX5_CAP_FLOWTABLE(dev,
331 flow_table_properties_nic_receive.
337 return mlx5_cmd_set_fte(dev, opmod, modify_mask, ft, group_id, fte);
340 int mlx5_cmd_delete_fte(struct mlx5_core_dev *dev,
341 struct mlx5_flow_table *ft,
344 u32 out[MLX5_ST_SZ_DW(delete_fte_out)] = {0};
345 u32 in[MLX5_ST_SZ_DW(delete_fte_in)] = {0};
347 MLX5_SET(delete_fte_in, in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
348 MLX5_SET(delete_fte_in, in, table_type, ft->type);
349 MLX5_SET(delete_fte_in, in, table_id, ft->id);
350 MLX5_SET(delete_fte_in, in, flow_index, index);
352 MLX5_SET(delete_fte_in, in, vport_number, ft->vport);
353 MLX5_SET(delete_fte_in, in, other_vport, 1);
356 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
359 int mlx5_cmd_fc_alloc(struct mlx5_core_dev *dev, u16 *id)
361 u32 in[MLX5_ST_SZ_DW(alloc_flow_counter_in)] = {0};
362 u32 out[MLX5_ST_SZ_DW(alloc_flow_counter_out)] = {0};
365 MLX5_SET(alloc_flow_counter_in, in, opcode,
366 MLX5_CMD_OP_ALLOC_FLOW_COUNTER);
368 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
370 *id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
374 int mlx5_cmd_fc_free(struct mlx5_core_dev *dev, u16 id)
376 u32 in[MLX5_ST_SZ_DW(dealloc_flow_counter_in)] = {0};
377 u32 out[MLX5_ST_SZ_DW(dealloc_flow_counter_out)] = {0};
379 MLX5_SET(dealloc_flow_counter_in, in, opcode,
380 MLX5_CMD_OP_DEALLOC_FLOW_COUNTER);
381 MLX5_SET(dealloc_flow_counter_in, in, flow_counter_id, id);
382 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
385 int mlx5_cmd_fc_query(struct mlx5_core_dev *dev, u16 id,
386 u64 *packets, u64 *bytes)
388 u32 out[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
389 MLX5_ST_SZ_BYTES(traffic_counter)] = {0};
390 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
394 MLX5_SET(query_flow_counter_in, in, opcode,
395 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
396 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
397 MLX5_SET(query_flow_counter_in, in, flow_counter_id, id);
398 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
402 stats = MLX5_ADDR_OF(query_flow_counter_out, out, flow_statistics);
403 *packets = MLX5_GET64(traffic_counter, stats, packets);
404 *bytes = MLX5_GET64(traffic_counter, stats, octets);
408 struct mlx5_cmd_fc_bulk {
415 struct mlx5_cmd_fc_bulk *
416 mlx5_cmd_fc_bulk_alloc(struct mlx5_core_dev *dev, u16 id, int num)
418 struct mlx5_cmd_fc_bulk *b;
420 MLX5_ST_SZ_BYTES(query_flow_counter_out) +
421 MLX5_ST_SZ_BYTES(traffic_counter) * num;
423 b = kzalloc(sizeof(*b) + outlen, GFP_KERNEL);
434 void mlx5_cmd_fc_bulk_free(struct mlx5_cmd_fc_bulk *b)
440 mlx5_cmd_fc_bulk_query(struct mlx5_core_dev *dev, struct mlx5_cmd_fc_bulk *b)
442 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
444 MLX5_SET(query_flow_counter_in, in, opcode,
445 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
446 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
447 MLX5_SET(query_flow_counter_in, in, flow_counter_id, b->id);
448 MLX5_SET(query_flow_counter_in, in, num_of_counters, b->num);
449 return mlx5_cmd_exec(dev, in, sizeof(in), b->out, b->outlen);
452 void mlx5_cmd_fc_bulk_get(struct mlx5_core_dev *dev,
453 struct mlx5_cmd_fc_bulk *b, u16 id,
454 u64 *packets, u64 *bytes)
456 int index = id - b->id;
459 if (index < 0 || index >= b->num) {
460 mlx5_core_warn(dev, "Flow counter id (0x%x) out of range (0x%x..0x%x). Counter ignored.\n",
461 id, b->id, b->id + b->num - 1);
465 stats = MLX5_ADDR_OF(query_flow_counter_out, b->out,
466 flow_statistics[index]);
467 *packets = MLX5_GET64(traffic_counter, stats, packets);
468 *bytes = MLX5_GET64(traffic_counter, stats, octets);
471 int mlx5_encap_alloc(struct mlx5_core_dev *dev,
477 int max_encap_size = MLX5_CAP_ESW(dev, max_encap_header_size);
478 u32 out[MLX5_ST_SZ_DW(alloc_encap_header_out)];
479 void *encap_header_in;
485 if (size > max_encap_size) {
486 mlx5_core_warn(dev, "encap size %zd too big, max supported is %d\n",
487 size, max_encap_size);
491 in = kzalloc(MLX5_ST_SZ_BYTES(alloc_encap_header_in) + size,
496 encap_header_in = MLX5_ADDR_OF(alloc_encap_header_in, in, encap_header);
497 header = MLX5_ADDR_OF(encap_header_in, encap_header_in, encap_header);
498 inlen = header - (void *)in + size;
500 memset(in, 0, inlen);
501 MLX5_SET(alloc_encap_header_in, in, opcode,
502 MLX5_CMD_OP_ALLOC_ENCAP_HEADER);
503 MLX5_SET(encap_header_in, encap_header_in, encap_header_size, size);
504 MLX5_SET(encap_header_in, encap_header_in, header_type, header_type);
505 memcpy(header, encap_header, size);
507 memset(out, 0, sizeof(out));
508 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
510 *encap_id = MLX5_GET(alloc_encap_header_out, out, encap_id);
515 void mlx5_encap_dealloc(struct mlx5_core_dev *dev, u32 encap_id)
517 u32 in[MLX5_ST_SZ_DW(dealloc_encap_header_in)];
518 u32 out[MLX5_ST_SZ_DW(dealloc_encap_header_out)];
520 memset(in, 0, sizeof(in));
521 MLX5_SET(dealloc_encap_header_in, in, opcode,
522 MLX5_CMD_OP_DEALLOC_ENCAP_HEADER);
523 MLX5_SET(dealloc_encap_header_in, in, encap_id, encap_id);
525 mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
528 int mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
529 u8 namespace, u8 num_actions,
530 void *modify_actions, u32 *modify_header_id)
532 u32 out[MLX5_ST_SZ_DW(alloc_modify_header_context_out)];
533 int max_actions, actions_size, inlen, err;
539 case MLX5_FLOW_NAMESPACE_FDB:
540 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(dev, max_modify_header_actions);
541 table_type = FS_FT_FDB;
543 case MLX5_FLOW_NAMESPACE_KERNEL:
544 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(dev, max_modify_header_actions);
545 table_type = FS_FT_NIC_RX;
551 if (num_actions > max_actions) {
552 mlx5_core_warn(dev, "too many modify header actions %d, max supported %d\n",
553 num_actions, max_actions);
557 actions_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto) * num_actions;
558 inlen = MLX5_ST_SZ_BYTES(alloc_modify_header_context_in) + actions_size;
560 in = kzalloc(inlen, GFP_KERNEL);
564 MLX5_SET(alloc_modify_header_context_in, in, opcode,
565 MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT);
566 MLX5_SET(alloc_modify_header_context_in, in, table_type, table_type);
567 MLX5_SET(alloc_modify_header_context_in, in, num_of_actions, num_actions);
569 actions_in = MLX5_ADDR_OF(alloc_modify_header_context_in, in, actions);
570 memcpy(actions_in, modify_actions, actions_size);
572 memset(out, 0, sizeof(out));
573 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
575 *modify_header_id = MLX5_GET(alloc_modify_header_context_out, out, modify_header_id);
580 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev, u32 modify_header_id)
582 u32 in[MLX5_ST_SZ_DW(dealloc_modify_header_context_in)];
583 u32 out[MLX5_ST_SZ_DW(dealloc_modify_header_context_out)];
585 memset(in, 0, sizeof(in));
586 MLX5_SET(dealloc_modify_header_context_in, in, opcode,
587 MLX5_CMD_OP_DEALLOC_MODIFY_HEADER_CONTEXT);
588 MLX5_SET(dealloc_modify_header_context_in, in, modify_header_id,
591 mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));