Merge tag 'io_uring-6.5-2023-07-03' of git://git.kernel.dk/linux
[linux-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"
4a98544d 39#include "fs_ft_pool.h"
26a81453 40#include "mlx5_core.h"
c9f1b073 41#include "eswitch.h"
26a81453 42
ae288a48 43static int mlx5_cmd_stub_update_root_ft(struct mlx5_flow_root_namespace *ns,
af76c501
MB
44 struct mlx5_flow_table *ft,
45 u32 underlay_qpn,
46 bool disconnect)
47{
48 return 0;
49}
50
ae288a48
MG
51static int mlx5_cmd_stub_create_flow_table(struct mlx5_flow_root_namespace *ns,
52 struct mlx5_flow_table *ft,
b0bb369e 53 struct mlx5_flow_table_attr *ft_attr,
ae288a48 54 struct mlx5_flow_table *next_ft)
af76c501 55{
b0bb369e
MB
56 int max_fte = ft_attr->max_fte;
57
58 ft->max_fte = max_fte ? roundup_pow_of_two(max_fte) : 1;
04745afb 59
af76c501
MB
60 return 0;
61}
62
ae288a48 63static int mlx5_cmd_stub_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
af76c501
MB
64 struct mlx5_flow_table *ft)
65{
66 return 0;
67}
68
ae288a48 69static int mlx5_cmd_stub_modify_flow_table(struct mlx5_flow_root_namespace *ns,
af76c501
MB
70 struct mlx5_flow_table *ft,
71 struct mlx5_flow_table *next_ft)
72{
73 return 0;
74}
75
ae288a48 76static int mlx5_cmd_stub_create_flow_group(struct mlx5_flow_root_namespace *ns,
af76c501
MB
77 struct mlx5_flow_table *ft,
78 u32 *in,
ae288a48 79 struct mlx5_flow_group *fg)
af76c501
MB
80{
81 return 0;
82}
83
ae288a48 84static int mlx5_cmd_stub_destroy_flow_group(struct mlx5_flow_root_namespace *ns,
af76c501 85 struct mlx5_flow_table *ft,
ae288a48 86 struct mlx5_flow_group *fg)
af76c501
MB
87{
88 return 0;
89}
90
ae288a48 91static int mlx5_cmd_stub_create_fte(struct mlx5_flow_root_namespace *ns,
af76c501
MB
92 struct mlx5_flow_table *ft,
93 struct mlx5_flow_group *group,
94 struct fs_fte *fte)
95{
96 return 0;
97}
98
ae288a48 99static int mlx5_cmd_stub_update_fte(struct mlx5_flow_root_namespace *ns,
af76c501 100 struct mlx5_flow_table *ft,
ae288a48 101 struct mlx5_flow_group *group,
af76c501
MB
102 int modify_mask,
103 struct fs_fte *fte)
104{
105 return -EOPNOTSUPP;
106}
107
ae288a48 108static int mlx5_cmd_stub_delete_fte(struct mlx5_flow_root_namespace *ns,
af76c501 109 struct mlx5_flow_table *ft,
e810bf5e 110 struct fs_fte *fte)
af76c501
MB
111{
112 return 0;
113}
114
2b688ea5 115static int mlx5_cmd_stub_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
3f3f05ab 116 struct mlx5_pkt_reformat_params *params,
2b688ea5
MG
117 enum mlx5_flow_namespace_type namespace,
118 struct mlx5_pkt_reformat *pkt_reformat)
119{
120 return 0;
121}
122
123static void mlx5_cmd_stub_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
124 struct mlx5_pkt_reformat *pkt_reformat)
125{
126}
127
128static int mlx5_cmd_stub_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
129 u8 namespace, u8 num_actions,
130 void *modify_actions,
131 struct mlx5_modify_hdr *modify_hdr)
132{
133 return 0;
134}
135
136static void mlx5_cmd_stub_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
137 struct mlx5_modify_hdr *modify_hdr)
138{
139}
140
6a48faee 141static int mlx5_cmd_stub_set_peer(struct mlx5_flow_root_namespace *ns,
6d5b7321
SD
142 struct mlx5_flow_root_namespace *peer_ns,
143 u8 peer_idx)
6a48faee
MG
144{
145 return 0;
146}
147
148static int mlx5_cmd_stub_create_ns(struct mlx5_flow_root_namespace *ns)
149{
150 return 0;
151}
152
153static int mlx5_cmd_stub_destroy_ns(struct mlx5_flow_root_namespace *ns)
154{
155 return 0;
156}
157
8348b71c
DC
158static u32 mlx5_cmd_stub_get_capabilities(struct mlx5_flow_root_namespace *ns,
159 enum fs_flow_table_type ft_type)
160{
161 return 0;
162}
163
db202995
MB
164static int mlx5_cmd_set_slave_root_fdb(struct mlx5_core_dev *master,
165 struct mlx5_core_dev *slave,
166 bool ft_id_valid,
167 u32 ft_id)
168{
169 u32 out[MLX5_ST_SZ_DW(set_flow_table_root_out)] = {};
170 u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)] = {};
171 struct mlx5_flow_root_namespace *root;
172 struct mlx5_flow_namespace *ns;
173
174 MLX5_SET(set_flow_table_root_in, in, opcode,
175 MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
176 MLX5_SET(set_flow_table_root_in, in, table_type,
177 FS_FT_FDB);
178 if (ft_id_valid) {
179 MLX5_SET(set_flow_table_root_in, in,
180 table_eswitch_owner_vhca_id_valid, 1);
181 MLX5_SET(set_flow_table_root_in, in,
182 table_eswitch_owner_vhca_id,
183 MLX5_CAP_GEN(master, vhca_id));
184 MLX5_SET(set_flow_table_root_in, in, table_id,
185 ft_id);
186 } else {
187 ns = mlx5_get_flow_namespace(slave,
188 MLX5_FLOW_NAMESPACE_FDB);
189 root = find_root(&ns->node);
190 MLX5_SET(set_flow_table_root_in, in, table_id,
191 root->root_ft->id);
192 }
193
194 return mlx5_cmd_exec(slave, in, sizeof(in), out, sizeof(out));
195}
196
e7e2519e
MG
197static int
198mlx5_cmd_stub_destroy_match_definer(struct mlx5_flow_root_namespace *ns,
199 int definer_id)
200{
201 return 0;
202}
203
204static int
205mlx5_cmd_stub_create_match_definer(struct mlx5_flow_root_namespace *ns,
206 u16 format_id, u32 *match_mask)
207{
208 return 0;
209}
210
ae288a48 211static int mlx5_cmd_update_root_ft(struct mlx5_flow_root_namespace *ns,
af76c501
MB
212 struct mlx5_flow_table *ft, u32 underlay_qpn,
213 bool disconnect)
2cc43b49 214{
31a0956e 215 u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)] = {};
ae288a48 216 struct mlx5_core_dev *dev = ns->dev;
db202995 217 int err;
2cc43b49 218
b3ba5149 219 if ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
50854114 220 underlay_qpn == 0)
b3ba5149
ES
221 return 0;
222
db202995
MB
223 if (ft->type == FS_FT_FDB &&
224 mlx5_lag_is_shared_fdb(dev) &&
225 !mlx5_lag_is_master(dev))
226 return 0;
227
2cc43b49
MG
228 MLX5_SET(set_flow_table_root_in, in, opcode,
229 MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
230 MLX5_SET(set_flow_table_root_in, in, table_type, ft->type);
dae37456 231
31a0956e 232 if (disconnect)
dae37456 233 MLX5_SET(set_flow_table_root_in, in, op_mod, 1);
31a0956e 234 else
dae37456 235 MLX5_SET(set_flow_table_root_in, in, table_id, ft->id);
dae37456 236
50854114 237 MLX5_SET(set_flow_table_root_in, in, underlay_qpn, underlay_qpn);
617b860c
PP
238 MLX5_SET(set_flow_table_root_in, in, vport_number, ft->vport);
239 MLX5_SET(set_flow_table_root_in, in, other_vport,
240 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
2cc43b49 241
db202995
MB
242 err = mlx5_cmd_exec_in(dev, set_flow_table_root, in);
243 if (!err &&
244 ft->type == FS_FT_FDB &&
245 mlx5_lag_is_shared_fdb(dev) &&
246 mlx5_lag_is_master(dev)) {
222dd185
SD
247 struct mlx5_core_dev *peer_dev;
248 int i;
249
250 mlx5_lag_for_each_peer_mdev(dev, peer_dev, i) {
251 err = mlx5_cmd_set_slave_root_fdb(dev, peer_dev, !disconnect,
252 (!disconnect) ? ft->id : 0);
253 if (err && !disconnect) {
254 MLX5_SET(set_flow_table_root_in, in, op_mod, 0);
255 MLX5_SET(set_flow_table_root_in, in, table_id,
256 ns->root_ft->id);
257 mlx5_cmd_exec_in(dev, set_flow_table_root, in);
258 }
259 if (err)
260 break;
db202995 261 }
222dd185 262
db202995
MB
263 }
264
265 return err;
2cc43b49
MG
266}
267
ae288a48
MG
268static int mlx5_cmd_create_flow_table(struct mlx5_flow_root_namespace *ns,
269 struct mlx5_flow_table *ft,
b0bb369e 270 struct mlx5_flow_table_attr *ft_attr,
ae288a48 271 struct mlx5_flow_table *next_ft)
26a81453 272{
ae288a48
MG
273 int en_encap = !!(ft->flags & MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT);
274 int en_decap = !!(ft->flags & MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
c6d4e45d 275 int term = !!(ft->flags & MLX5_FLOW_TABLE_TERMINATION);
31a0956e
LR
276 u32 out[MLX5_ST_SZ_DW(create_flow_table_out)] = {};
277 u32 in[MLX5_ST_SZ_DW(create_flow_table_in)] = {};
ae288a48 278 struct mlx5_core_dev *dev = ns->dev;
b0bb369e 279 unsigned int size;
26a81453
MG
280 int err;
281
b0bb369e 282 size = mlx5_ft_pool_get_avail_sz(dev, ft->type, ft_attr->max_fte);
4a98544d
PB
283 if (!size)
284 return -ENOSPC;
04745afb 285
26a81453
MG
286 MLX5_SET(create_flow_table_in, in, opcode,
287 MLX5_CMD_OP_CREATE_FLOW_TABLE);
288
b0bb369e 289 MLX5_SET(create_flow_table_in, in, uid, ft_attr->uid);
ae288a48
MG
290 MLX5_SET(create_flow_table_in, in, table_type, ft->type);
291 MLX5_SET(create_flow_table_in, in, flow_table_context.level, ft->level);
4a98544d 292 MLX5_SET(create_flow_table_in, in, flow_table_context.log_size, size ? ilog2(size) : 0);
617b860c
PP
293 MLX5_SET(create_flow_table_in, in, vport_number, ft->vport);
294 MLX5_SET(create_flow_table_in, in, other_vport,
295 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
26a81453 296
0c90e9c6 297 MLX5_SET(create_flow_table_in, in, flow_table_context.decap_en,
61444b45 298 en_decap);
60786f09 299 MLX5_SET(create_flow_table_in, in, flow_table_context.reformat_en,
61444b45 300 en_encap);
c6d4e45d
EB
301 MLX5_SET(create_flow_table_in, in, flow_table_context.termination_table,
302 term);
c9f1b073 303
ae288a48 304 switch (ft->op_mod) {
aaff1bea
AH
305 case FS_FT_OP_MOD_NORMAL:
306 if (next_ft) {
0c90e9c6 307 MLX5_SET(create_flow_table_in, in,
f6f7d6b5
MG
308 flow_table_context.table_miss_action,
309 MLX5_FLOW_TABLE_MISS_ACTION_FWD);
0c90e9c6
MG
310 MLX5_SET(create_flow_table_in, in,
311 flow_table_context.table_miss_id, next_ft->id);
f6f7d6b5
MG
312 } else {
313 MLX5_SET(create_flow_table_in, in,
314 flow_table_context.table_miss_action,
f66ad830 315 ft->def_miss_action);
aaff1bea
AH
316 }
317 break;
318
319 case FS_FT_OP_MOD_LAG_DEMUX:
320 MLX5_SET(create_flow_table_in, in, op_mod, 0x1);
321 if (next_ft)
0c90e9c6
MG
322 MLX5_SET(create_flow_table_in, in,
323 flow_table_context.lag_master_next_table_id,
aaff1bea
AH
324 next_ft->id);
325 break;
326 }
327
31a0956e 328 err = mlx5_cmd_exec_inout(dev, create_flow_table, in, out);
4a98544d 329 if (!err) {
ae288a48
MG
330 ft->id = MLX5_GET(create_flow_table_out, out,
331 table_id);
4a98544d
PB
332 ft->max_fte = size;
333 } else {
334 mlx5_ft_pool_put_sz(ns->dev, size);
335 }
336
26a81453
MG
337 return err;
338}
339
ae288a48 340static int mlx5_cmd_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
af76c501 341 struct mlx5_flow_table *ft)
26a81453 342{
31a0956e 343 u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)] = {};
ae288a48 344 struct mlx5_core_dev *dev = ns->dev;
4a98544d 345 int err;
26a81453
MG
346
347 MLX5_SET(destroy_flow_table_in, in, opcode,
348 MLX5_CMD_OP_DESTROY_FLOW_TABLE);
349 MLX5_SET(destroy_flow_table_in, in, table_type, ft->type);
350 MLX5_SET(destroy_flow_table_in, in, table_id, ft->id);
617b860c
PP
351 MLX5_SET(destroy_flow_table_in, in, vport_number, ft->vport);
352 MLX5_SET(destroy_flow_table_in, in, other_vport,
353 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
26a81453 354
4a98544d
PB
355 err = mlx5_cmd_exec_in(dev, destroy_flow_table, in);
356 if (!err)
357 mlx5_ft_pool_put_sz(ns->dev, ft->max_fte);
358
359 return err;
26a81453
MG
360}
361
ae288a48 362static int mlx5_cmd_modify_flow_table(struct mlx5_flow_root_namespace *ns,
af76c501
MB
363 struct mlx5_flow_table *ft,
364 struct mlx5_flow_table *next_ft)
34a40e68 365{
31a0956e 366 u32 in[MLX5_ST_SZ_DW(modify_flow_table_in)] = {};
ae288a48 367 struct mlx5_core_dev *dev = ns->dev;
34a40e68
MG
368
369 MLX5_SET(modify_flow_table_in, in, opcode,
370 MLX5_CMD_OP_MODIFY_FLOW_TABLE);
371 MLX5_SET(modify_flow_table_in, in, table_type, ft->type);
372 MLX5_SET(modify_flow_table_in, in, table_id, ft->id);
aaff1bea
AH
373
374 if (ft->op_mod == FS_FT_OP_MOD_LAG_DEMUX) {
375 MLX5_SET(modify_flow_table_in, in, modify_field_select,
376 MLX5_MODIFY_FLOW_TABLE_LAG_NEXT_TABLE_ID);
377 if (next_ft) {
378 MLX5_SET(modify_flow_table_in, in,
0c90e9c6 379 flow_table_context.lag_master_next_table_id, next_ft->id);
aaff1bea
AH
380 } else {
381 MLX5_SET(modify_flow_table_in, in,
0c90e9c6 382 flow_table_context.lag_master_next_table_id, 0);
aaff1bea 383 }
34a40e68 384 } else {
617b860c
PP
385 MLX5_SET(modify_flow_table_in, in, vport_number, ft->vport);
386 MLX5_SET(modify_flow_table_in, in, other_vport,
387 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
aaff1bea
AH
388 MLX5_SET(modify_flow_table_in, in, modify_field_select,
389 MLX5_MODIFY_FLOW_TABLE_MISS_TABLE_ID);
390 if (next_ft) {
0c90e9c6 391 MLX5_SET(modify_flow_table_in, in,
f6f7d6b5
MG
392 flow_table_context.table_miss_action,
393 MLX5_FLOW_TABLE_MISS_ACTION_FWD);
0c90e9c6
MG
394 MLX5_SET(modify_flow_table_in, in,
395 flow_table_context.table_miss_id,
aaff1bea
AH
396 next_ft->id);
397 } else {
0c90e9c6 398 MLX5_SET(modify_flow_table_in, in,
f6f7d6b5 399 flow_table_context.table_miss_action,
f66ad830 400 ft->def_miss_action);
aaff1bea 401 }
34a40e68
MG
402 }
403
31a0956e 404 return mlx5_cmd_exec_in(dev, modify_flow_table, in);
34a40e68
MG
405}
406
ae288a48 407static int mlx5_cmd_create_flow_group(struct mlx5_flow_root_namespace *ns,
af76c501
MB
408 struct mlx5_flow_table *ft,
409 u32 *in,
ae288a48 410 struct mlx5_flow_group *fg)
26a81453 411{
31a0956e 412 u32 out[MLX5_ST_SZ_DW(create_flow_group_out)] = {};
ae288a48 413 struct mlx5_core_dev *dev = ns->dev;
26a81453
MG
414 int err;
415
26a81453
MG
416 MLX5_SET(create_flow_group_in, in, opcode,
417 MLX5_CMD_OP_CREATE_FLOW_GROUP);
418 MLX5_SET(create_flow_group_in, in, table_type, ft->type);
419 MLX5_SET(create_flow_group_in, in, table_id, ft->id);
617b860c
PP
420 MLX5_SET(create_flow_group_in, in, vport_number, ft->vport);
421 MLX5_SET(create_flow_group_in, in, other_vport,
422 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
31a0956e 423 err = mlx5_cmd_exec_inout(dev, create_flow_group, in, out);
26a81453 424 if (!err)
ae288a48
MG
425 fg->id = MLX5_GET(create_flow_group_out, out,
426 group_id);
26a81453
MG
427 return err;
428}
429
ae288a48 430static int mlx5_cmd_destroy_flow_group(struct mlx5_flow_root_namespace *ns,
af76c501 431 struct mlx5_flow_table *ft,
ae288a48 432 struct mlx5_flow_group *fg)
26a81453 433{
31a0956e 434 u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)] = {};
ae288a48 435 struct mlx5_core_dev *dev = ns->dev;
26a81453
MG
436
437 MLX5_SET(destroy_flow_group_in, in, opcode,
438 MLX5_CMD_OP_DESTROY_FLOW_GROUP);
439 MLX5_SET(destroy_flow_group_in, in, table_type, ft->type);
440 MLX5_SET(destroy_flow_group_in, in, table_id, ft->id);
ae288a48 441 MLX5_SET(destroy_flow_group_in, in, group_id, fg->id);
617b860c
PP
442 MLX5_SET(destroy_flow_group_in, in, vport_number, ft->vport);
443 MLX5_SET(destroy_flow_group_in, in, other_vport,
444 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
31a0956e 445 return mlx5_cmd_exec_in(dev, destroy_flow_group, in);
26a81453
MG
446}
447
a2c6162b
EB
448static int mlx5_set_extended_dest(struct mlx5_core_dev *dev,
449 struct fs_fte *fte, bool *extended_dest)
450{
451 int fw_log_max_fdb_encap_uplink =
452 MLX5_CAP_ESW(dev, log_max_fdb_encap_uplink);
453 int num_fwd_destinations = 0;
454 struct mlx5_flow_rule *dst;
455 int num_encap = 0;
456
457 *extended_dest = false;
458 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
459 return 0;
460
461 list_for_each_entry(dst, &fte->node.children, node.list) {
6510bc0d
MB
462 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER ||
463 dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_NONE)
a2c6162b 464 continue;
e3a0f40b
YK
465 if ((dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
466 dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) &&
a2c6162b
EB
467 dst->dest_attr.vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID)
468 num_encap++;
469 num_fwd_destinations++;
470 }
471 if (num_fwd_destinations > 1 && num_encap > 0)
472 *extended_dest = true;
473
474 if (*extended_dest && !fw_log_max_fdb_encap_uplink) {
475 mlx5_core_warn(dev, "FW does not support extended destination");
476 return -EOPNOTSUPP;
477 }
478 if (num_encap > (1 << fw_log_max_fdb_encap_uplink)) {
479 mlx5_core_warn(dev, "FW does not support more than %d encaps",
480 1 << fw_log_max_fdb_encap_uplink);
481 return -EOPNOTSUPP;
482 }
483
484 return 0;
485}
91707779
JL
486
487static void
488mlx5_cmd_set_fte_flow_meter(struct fs_fte *fte, void *in_flow_context)
489{
490 void *exe_aso_ctrl;
491 void *execute_aso;
492
493 execute_aso = MLX5_ADDR_OF(flow_context, in_flow_context,
494 execute_aso[0]);
495 MLX5_SET(execute_aso, execute_aso, valid, 1);
496 MLX5_SET(execute_aso, execute_aso, aso_object_id,
497 fte->action.exe_aso.object_id);
498
499 exe_aso_ctrl = MLX5_ADDR_OF(execute_aso, execute_aso, exe_aso_ctrl);
500 MLX5_SET(exe_aso_ctrl_flow_meter, exe_aso_ctrl, return_reg_id,
501 fte->action.exe_aso.return_reg_id);
502 MLX5_SET(exe_aso_ctrl_flow_meter, exe_aso_ctrl, aso_type,
503 fte->action.exe_aso.type);
504 MLX5_SET(exe_aso_ctrl_flow_meter, exe_aso_ctrl, init_color,
505 fte->action.exe_aso.flow_meter.init_color);
506 MLX5_SET(exe_aso_ctrl_flow_meter, exe_aso_ctrl, meter_id,
507 fte->action.exe_aso.flow_meter.meter_idx);
508}
509
26a81453
MG
510static int mlx5_cmd_set_fte(struct mlx5_core_dev *dev,
511 int opmod, int modify_mask,
512 struct mlx5_flow_table *ft,
513 unsigned group_id,
514 struct fs_fte *fte)
515{
c4f287c4 516 u32 out[MLX5_ST_SZ_DW(set_fte_out)] = {0};
a2c6162b 517 bool extended_dest = false;
26a81453 518 struct mlx5_flow_rule *dst;
0c06897a 519 void *in_flow_context, *vlan;
26a81453 520 void *in_match_value;
87cd0649 521 int reformat_id = 0;
a2c6162b
EB
522 unsigned int inlen;
523 int dst_cnt_size;
87cd0649 524 u32 *in, action;
26a81453 525 void *in_dests;
26a81453
MG
526 int err;
527
a2c6162b
EB
528 if (mlx5_set_extended_dest(dev, fte, &extended_dest))
529 return -EOPNOTSUPP;
530
531 if (!extended_dest)
532 dst_cnt_size = MLX5_ST_SZ_BYTES(dest_format_struct);
533 else
534 dst_cnt_size = MLX5_ST_SZ_BYTES(extended_dest_format);
535
536 inlen = MLX5_ST_SZ_BYTES(set_fte_in) + fte->dests_size * dst_cnt_size;
1b9a07ee
LR
537 in = kvzalloc(inlen, GFP_KERNEL);
538 if (!in)
26a81453 539 return -ENOMEM;
26a81453
MG
540
541 MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
542 MLX5_SET(set_fte_in, in, op_mod, opmod);
543 MLX5_SET(set_fte_in, in, modify_enable_mask, modify_mask);
544 MLX5_SET(set_fte_in, in, table_type, ft->type);
545 MLX5_SET(set_fte_in, in, table_id, ft->id);
546 MLX5_SET(set_fte_in, in, flow_index, fte->index);
ff189b43
PB
547 MLX5_SET(set_fte_in, in, ignore_flow_level,
548 !!(fte->action.flags & FLOW_ACT_IGNORE_FLOW_LEVEL));
549
617b860c
PP
550 MLX5_SET(set_fte_in, in, vport_number, ft->vport);
551 MLX5_SET(set_fte_in, in, other_vport,
552 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
26a81453
MG
553
554 in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
555 MLX5_SET(flow_context, in_flow_context, group_id, group_id);
0c06897a 556
bb0ee7dc
JL
557 MLX5_SET(flow_context, in_flow_context, flow_tag,
558 fte->flow_context.flow_tag);
8d212ff0
JL
559 MLX5_SET(flow_context, in_flow_context, flow_source,
560 fte->flow_context.flow_source);
561
a2c6162b
EB
562 MLX5_SET(flow_context, in_flow_context, extended_destination,
563 extended_dest);
a2c6162b 564
87cd0649
YK
565 action = fte->action.action;
566 if (extended_dest)
567 action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
568
569 MLX5_SET(flow_context, in_flow_context, action, action);
570
571 if (!extended_dest && fte->action.pkt_reformat) {
572 struct mlx5_pkt_reformat *pkt_reformat = fte->action.pkt_reformat;
573
574 if (pkt_reformat->owner == MLX5_FLOW_RESOURCE_OWNER_SW) {
575 reformat_id = mlx5_fs_dr_action_get_pkt_reformat_id(pkt_reformat);
576 if (reformat_id < 0) {
577 mlx5_core_err(dev,
578 "Unsupported SW-owned pkt_reformat type (%d) in FW-owned table\n",
579 pkt_reformat->reformat_type);
580 err = reformat_id;
581 goto err_out;
582 }
583 } else {
584 reformat_id = fte->action.pkt_reformat->id;
585 }
a2c6162b 586 }
87cd0649
YK
587
588 MLX5_SET(flow_context, in_flow_context, packet_reformat_id, (u32)reformat_id);
589
590 if (fte->action.modify_hdr) {
591 if (fte->action.modify_hdr->owner == MLX5_FLOW_RESOURCE_OWNER_SW) {
592 mlx5_core_err(dev, "Can't use SW-owned modify_hdr in FW-owned table\n");
593 err = -EOPNOTSUPP;
594 goto err_out;
595 }
596
2b688ea5
MG
597 MLX5_SET(flow_context, in_flow_context, modify_header_id,
598 fte->action.modify_hdr->id);
87cd0649 599 }
0c06897a 600
e227ee99
LN
601 MLX5_SET(flow_context, in_flow_context, encrypt_decrypt_type,
602 fte->action.crypto.type);
603 MLX5_SET(flow_context, in_flow_context, encrypt_decrypt_obj_id,
604 fte->action.crypto.obj_id);
5e466345 605
0c06897a
OG
606 vlan = MLX5_ADDR_OF(flow_context, in_flow_context, push_vlan);
607
8da6fe2a
JL
608 MLX5_SET(vlan, vlan, ethtype, fte->action.vlan[0].ethtype);
609 MLX5_SET(vlan, vlan, vid, fte->action.vlan[0].vid);
610 MLX5_SET(vlan, vlan, prio, fte->action.vlan[0].prio);
611
612 vlan = MLX5_ADDR_OF(flow_context, in_flow_context, push_vlan_2);
613
614 MLX5_SET(vlan, vlan, ethtype, fte->action.vlan[1].ethtype);
615 MLX5_SET(vlan, vlan, vid, fte->action.vlan[1].vid);
616 MLX5_SET(vlan, vlan, prio, fte->action.vlan[1].prio);
0c06897a 617
26a81453
MG
618 in_match_value = MLX5_ADDR_OF(flow_context, in_flow_context,
619 match_value);
667cb65a 620 memcpy(in_match_value, &fte->val, sizeof(fte->val));
26a81453 621
bd5251db 622 in_dests = MLX5_ADDR_OF(flow_context, in_flow_context, destination);
d2ec6a35 623 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
bd5251db
AV
624 int list_size = 0;
625
60ab4584 626 list_for_each_entry(dst, &fte->node.children, node.list) {
d639af62
MB
627 enum mlx5_flow_destination_type type = dst->dest_attr.type;
628 enum mlx5_ifc_flow_destination_type ifc_type;
629 unsigned int id;
60ab4584 630
664000b6 631 if (type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
bd5251db
AV
632 continue;
633
664000b6 634 switch (type) {
6510bc0d
MB
635 case MLX5_FLOW_DESTINATION_TYPE_NONE:
636 continue;
664000b6
YH
637 case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM:
638 id = dst->dest_attr.ft_num;
d639af62 639 ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_FLOW_TABLE;
664000b6
YH
640 break;
641 case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE:
60ab4584 642 id = dst->dest_attr.ft->id;
d639af62 643 ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_FLOW_TABLE;
664000b6 644 break;
58a606db 645 case MLX5_FLOW_DESTINATION_TYPE_UPLINK:
664000b6 646 case MLX5_FLOW_DESTINATION_TYPE_VPORT:
b17f7fc1
SK
647 MLX5_SET(dest_format_struct, in_dests,
648 destination_eswitch_owner_vhca_id_valid,
aa39c2c0
EB
649 !!(dst->dest_attr.vport.flags &
650 MLX5_FLOW_DEST_VPORT_VHCA_ID));
b17f7fc1
SK
651 MLX5_SET(dest_format_struct, in_dests,
652 destination_eswitch_owner_vhca_id,
653 dst->dest_attr.vport.vhca_id);
58a606db
MG
654 if (type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) {
655 /* destination_id is reserved */
656 id = 0;
d639af62 657 ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_UPLINK;
58a606db
MG
658 break;
659 }
d639af62 660 ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_VPORT;
58a606db 661 id = dst->dest_attr.vport.num;
0fd79b1e
EB
662 if (extended_dest &&
663 dst->dest_attr.vport.pkt_reformat) {
a2c6162b
EB
664 MLX5_SET(dest_format_struct, in_dests,
665 packet_reformat,
666 !!(dst->dest_attr.vport.flags &
667 MLX5_FLOW_DEST_VPORT_REFORMAT_ID));
668 MLX5_SET(extended_dest_format, in_dests,
669 packet_reformat_id,
2b688ea5 670 dst->dest_attr.vport.pkt_reformat->id);
a2c6162b 671 }
664000b6 672 break;
38730630
CM
673 case MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER:
674 id = dst->dest_attr.sampler_id;
d639af62 675 ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_FLOW_SAMPLER;
38730630 676 break;
4f226b71
MZ
677 case MLX5_FLOW_DESTINATION_TYPE_TABLE_TYPE:
678 MLX5_SET(dest_format_struct, in_dests,
679 destination_table_type, dst->dest_attr.ft->type);
680 id = dst->dest_attr.ft->id;
681 ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_TABLE_TYPE;
682 break;
664000b6 683 default:
60ab4584 684 id = dst->dest_attr.tir_num;
d639af62 685 ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_TIR;
60ab4584 686 }
664000b6
YH
687
688 MLX5_SET(dest_format_struct, in_dests, destination_type,
d639af62 689 ifc_type);
60ab4584 690 MLX5_SET(dest_format_struct, in_dests, destination_id, id);
a2c6162b 691 in_dests += dst_cnt_size;
bd5251db
AV
692 list_size++;
693 }
694
695 MLX5_SET(flow_context, in_flow_context, destination_list_size,
696 list_size);
697 }
698
d2ec6a35 699 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
16f1c5bb
RS
700 int max_list_size = BIT(MLX5_CAP_FLOWTABLE_TYPE(dev,
701 log_max_flow_counter,
702 ft->type));
bd5251db
AV
703 int list_size = 0;
704
705 list_for_each_entry(dst, &fte->node.children, node.list) {
706 if (dst->dest_attr.type !=
707 MLX5_FLOW_DESTINATION_TYPE_COUNTER)
708 continue;
709
710 MLX5_SET(flow_counter_list, in_dests, flow_counter_id,
171c7625 711 dst->dest_attr.counter_id);
a2c6162b 712 in_dests += dst_cnt_size;
bd5251db 713 list_size++;
60ab4584 714 }
16f1c5bb
RS
715 if (list_size > max_list_size) {
716 err = -EINVAL;
717 goto err_out;
718 }
bd5251db
AV
719
720 MLX5_SET(flow_context, in_flow_context, flow_counter_list_size,
721 list_size);
26a81453 722 }
bd5251db 723
91707779
JL
724 if (fte->action.action & MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO) {
725 if (fte->action.exe_aso.type == MLX5_EXE_ASO_FLOW_METER) {
726 mlx5_cmd_set_fte_flow_meter(fte, in_flow_context);
727 } else {
728 err = -EOPNOTSUPP;
729 goto err_out;
730 }
731 }
732
c4f287c4 733 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
16f1c5bb 734err_out:
26a81453 735 kvfree(in);
26a81453
MG
736 return err;
737}
738
ae288a48 739static int mlx5_cmd_create_fte(struct mlx5_flow_root_namespace *ns,
af76c501
MB
740 struct mlx5_flow_table *ft,
741 struct mlx5_flow_group *group,
742 struct fs_fte *fte)
26a81453 743{
ae288a48 744 struct mlx5_core_dev *dev = ns->dev;
af76c501
MB
745 unsigned int group_id = group->id;
746
c4f287c4 747 return mlx5_cmd_set_fte(dev, 0, 0, ft, group_id, fte);
26a81453
MG
748}
749
ae288a48 750static int mlx5_cmd_update_fte(struct mlx5_flow_root_namespace *ns,
af76c501 751 struct mlx5_flow_table *ft,
ae288a48 752 struct mlx5_flow_group *fg,
af76c501
MB
753 int modify_mask,
754 struct fs_fte *fte)
26a81453
MG
755{
756 int opmod;
ae288a48 757 struct mlx5_core_dev *dev = ns->dev;
26a81453
MG
758 int atomic_mod_cap = MLX5_CAP_FLOWTABLE(dev,
759 flow_table_properties_nic_receive.
760 flow_modify_en);
761 if (!atomic_mod_cap)
9eb78923 762 return -EOPNOTSUPP;
26a81453 763 opmod = 1;
26a81453 764
ae288a48 765 return mlx5_cmd_set_fte(dev, opmod, modify_mask, ft, fg->id, fte);
26a81453
MG
766}
767
ae288a48 768static int mlx5_cmd_delete_fte(struct mlx5_flow_root_namespace *ns,
af76c501 769 struct mlx5_flow_table *ft,
e810bf5e 770 struct fs_fte *fte)
26a81453 771{
31a0956e 772 u32 in[MLX5_ST_SZ_DW(delete_fte_in)] = {};
ae288a48 773 struct mlx5_core_dev *dev = ns->dev;
26a81453
MG
774
775 MLX5_SET(delete_fte_in, in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
776 MLX5_SET(delete_fte_in, in, table_type, ft->type);
777 MLX5_SET(delete_fte_in, in, table_id, ft->id);
e810bf5e 778 MLX5_SET(delete_fte_in, in, flow_index, fte->index);
617b860c
PP
779 MLX5_SET(delete_fte_in, in, vport_number, ft->vport);
780 MLX5_SET(delete_fte_in, in, other_vport,
781 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
26a81453 782
31a0956e 783 return mlx5_cmd_exec_in(dev, delete_fte, in);
26a81453 784}
9dc0b289 785
8536a6bf
GT
786int mlx5_cmd_fc_bulk_alloc(struct mlx5_core_dev *dev,
787 enum mlx5_fc_bulk_alloc_bitmask alloc_bitmask,
788 u32 *id)
9dc0b289 789{
31a0956e
LR
790 u32 out[MLX5_ST_SZ_DW(alloc_flow_counter_out)] = {};
791 u32 in[MLX5_ST_SZ_DW(alloc_flow_counter_in)] = {};
9dc0b289
AV
792 int err;
793
9dc0b289
AV
794 MLX5_SET(alloc_flow_counter_in, in, opcode,
795 MLX5_CMD_OP_ALLOC_FLOW_COUNTER);
8536a6bf 796 MLX5_SET(alloc_flow_counter_in, in, flow_counter_bulk, alloc_bitmask);
9dc0b289 797
31a0956e 798 err = mlx5_cmd_exec_inout(dev, alloc_flow_counter, in, out);
c4f287c4
SM
799 if (!err)
800 *id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
801 return err;
9dc0b289
AV
802}
803
8536a6bf
GT
804int mlx5_cmd_fc_alloc(struct mlx5_core_dev *dev, u32 *id)
805{
806 return mlx5_cmd_fc_bulk_alloc(dev, 0, id);
807}
808
a8ffcc74 809int mlx5_cmd_fc_free(struct mlx5_core_dev *dev, u32 id)
9dc0b289 810{
31a0956e 811 u32 in[MLX5_ST_SZ_DW(dealloc_flow_counter_in)] = {};
9dc0b289
AV
812
813 MLX5_SET(dealloc_flow_counter_in, in, opcode,
814 MLX5_CMD_OP_DEALLOC_FLOW_COUNTER);
815 MLX5_SET(dealloc_flow_counter_in, in, flow_counter_id, id);
31a0956e 816 return mlx5_cmd_exec_in(dev, dealloc_flow_counter, in);
9dc0b289
AV
817}
818
a8ffcc74 819int mlx5_cmd_fc_query(struct mlx5_core_dev *dev, u32 id,
9dc0b289
AV
820 u64 *packets, u64 *bytes)
821{
822 u32 out[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
31a0956e
LR
823 MLX5_ST_SZ_BYTES(traffic_counter)] = {};
824 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {};
9dc0b289
AV
825 void *stats;
826 int err = 0;
827
9dc0b289
AV
828 MLX5_SET(query_flow_counter_in, in, opcode,
829 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
830 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
831 MLX5_SET(query_flow_counter_in, in, flow_counter_id, id);
c4f287c4 832 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
9dc0b289
AV
833 if (err)
834 return err;
835
836 stats = MLX5_ADDR_OF(query_flow_counter_out, out, flow_statistics);
837 *packets = MLX5_GET64(traffic_counter, stats, packets);
838 *bytes = MLX5_GET64(traffic_counter, stats, octets);
9dc0b289
AV
839 return 0;
840}
a351a1b0 841
6f06e04b 842int mlx5_cmd_fc_get_bulk_query_out_len(int bulk_len)
a351a1b0 843{
6f06e04b
GT
844 return MLX5_ST_SZ_BYTES(query_flow_counter_out) +
845 MLX5_ST_SZ_BYTES(traffic_counter) * bulk_len;
a351a1b0
AV
846}
847
6f06e04b
GT
848int mlx5_cmd_fc_bulk_query(struct mlx5_core_dev *dev, u32 base_id, int bulk_len,
849 u32 *out)
a351a1b0 850{
6f06e04b 851 int outlen = mlx5_cmd_fc_get_bulk_query_out_len(bulk_len);
31a0956e 852 u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {};
a351a1b0
AV
853
854 MLX5_SET(query_flow_counter_in, in, opcode,
855 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
6f06e04b
GT
856 MLX5_SET(query_flow_counter_in, in, flow_counter_id, base_id);
857 MLX5_SET(query_flow_counter_in, in, num_of_counters, bulk_len);
858 return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
a351a1b0 859}
575ddf58 860
2b688ea5 861static int mlx5_cmd_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
3f3f05ab 862 struct mlx5_pkt_reformat_params *params,
2b688ea5
MG
863 enum mlx5_flow_namespace_type namespace,
864 struct mlx5_pkt_reformat *pkt_reformat)
575ddf58 865{
31a0956e 866 u32 out[MLX5_ST_SZ_DW(alloc_packet_reformat_context_out)] = {};
2b688ea5 867 struct mlx5_core_dev *dev = ns->dev;
60786f09 868 void *packet_reformat_context_in;
31ca3648 869 int max_encap_size;
60786f09 870 void *reformat;
43f93839 871 int inlen;
575ddf58 872 int err;
43f93839 873 u32 *in;
575ddf58 874
22c3f2f5
MG
875 if (namespace == MLX5_FLOW_NAMESPACE_FDB ||
876 namespace == MLX5_FLOW_NAMESPACE_FDB_BYPASS)
31ca3648
MB
877 max_encap_size = MLX5_CAP_ESW(dev, max_encap_header_size);
878 else
879 max_encap_size = MLX5_CAP_FLOWTABLE(dev, max_encap_header_size);
880
3f3f05ab 881 if (params->size > max_encap_size) {
073ff3c8 882 mlx5_core_warn(dev, "encap size %zd too big, max supported is %d\n",
3f3f05ab 883 params->size, max_encap_size);
575ddf58 884 return -EINVAL;
073ff3c8 885 }
575ddf58 886
3f3f05ab
YK
887 in = kzalloc(MLX5_ST_SZ_BYTES(alloc_packet_reformat_context_in) +
888 params->size, GFP_KERNEL);
43f93839
HHZ
889 if (!in)
890 return -ENOMEM;
891
60786f09
MB
892 packet_reformat_context_in = MLX5_ADDR_OF(alloc_packet_reformat_context_in,
893 in, packet_reformat_context);
894 reformat = MLX5_ADDR_OF(packet_reformat_context_in,
895 packet_reformat_context_in,
896 reformat_data);
3f3f05ab 897 inlen = reformat - (void *)in + params->size;
43f93839 898
60786f09
MB
899 MLX5_SET(alloc_packet_reformat_context_in, in, opcode,
900 MLX5_CMD_OP_ALLOC_PACKET_REFORMAT_CONTEXT);
901 MLX5_SET(packet_reformat_context_in, packet_reformat_context_in,
3f3f05ab 902 reformat_data_size, params->size);
60786f09 903 MLX5_SET(packet_reformat_context_in, packet_reformat_context_in,
3f3f05ab
YK
904 reformat_type, params->type);
905 MLX5_SET(packet_reformat_context_in, packet_reformat_context_in,
906 reformat_param_0, params->param_0);
907 MLX5_SET(packet_reformat_context_in, packet_reformat_context_in,
908 reformat_param_1, params->param_1);
909 if (params->data && params->size)
910 memcpy(reformat, params->data, params->size);
575ddf58 911
575ddf58
IL
912 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
913
2b688ea5
MG
914 pkt_reformat->id = MLX5_GET(alloc_packet_reformat_context_out,
915 out, packet_reformat_id);
87cd0649
YK
916 pkt_reformat->owner = MLX5_FLOW_RESOURCE_OWNER_FW;
917
43f93839 918 kfree(in);
575ddf58
IL
919 return err;
920}
921
2b688ea5
MG
922static void mlx5_cmd_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
923 struct mlx5_pkt_reformat *pkt_reformat)
575ddf58 924{
31a0956e 925 u32 in[MLX5_ST_SZ_DW(dealloc_packet_reformat_context_in)] = {};
2b688ea5 926 struct mlx5_core_dev *dev = ns->dev;
575ddf58 927
60786f09
MB
928 MLX5_SET(dealloc_packet_reformat_context_in, in, opcode,
929 MLX5_CMD_OP_DEALLOC_PACKET_REFORMAT_CONTEXT);
930 MLX5_SET(dealloc_packet_reformat_context_in, in, packet_reformat_id,
2b688ea5 931 pkt_reformat->id);
575ddf58 932
31a0956e 933 mlx5_cmd_exec_in(dev, dealloc_packet_reformat_context, in);
575ddf58 934}
2de24fed 935
2b688ea5
MG
936static int mlx5_cmd_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
937 u8 namespace, u8 num_actions,
938 void *modify_actions,
939 struct mlx5_modify_hdr *modify_hdr)
2de24fed 940{
31a0956e 941 u32 out[MLX5_ST_SZ_DW(alloc_modify_header_context_out)] = {};
2de24fed 942 int max_actions, actions_size, inlen, err;
2b688ea5 943 struct mlx5_core_dev *dev = ns->dev;
2de24fed
OG
944 void *actions_in;
945 u8 table_type;
946 u32 *in;
947
948 switch (namespace) {
949 case MLX5_FLOW_NAMESPACE_FDB:
22c3f2f5 950 case MLX5_FLOW_NAMESPACE_FDB_BYPASS:
2de24fed
OG
951 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(dev, max_modify_header_actions);
952 table_type = FS_FT_FDB;
953 break;
15d187e2 954 case MLX5_FLOW_NAMESPACE_KERNEL_RX_MACSEC:
2de24fed 955 case MLX5_FLOW_NAMESPACE_KERNEL:
c3c062f8 956 case MLX5_FLOW_NAMESPACE_BYPASS:
2de24fed
OG
957 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(dev, max_modify_header_actions);
958 table_type = FS_FT_NIC_RX;
959 break;
c3c062f8 960 case MLX5_FLOW_NAMESPACE_EGRESS:
ee534d7f
LN
961 case MLX5_FLOW_NAMESPACE_EGRESS_IPSEC:
962 case MLX5_FLOW_NAMESPACE_EGRESS_MACSEC:
c3c062f8
MB
963 max_actions = MLX5_CAP_FLOWTABLE_NIC_TX(dev, max_modify_header_actions);
964 table_type = FS_FT_NIC_TX;
965 break;
84b0d6a7
JL
966 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
967 max_actions = MLX5_CAP_ESW_INGRESS_ACL(dev, max_modify_header_actions);
968 table_type = FS_FT_ESW_INGRESS_ACL;
969 break;
ecf814e0
MG
970 case MLX5_FLOW_NAMESPACE_RDMA_TX:
971 max_actions = MLX5_CAP_FLOWTABLE_RDMA_TX(dev, max_modify_header_actions);
972 table_type = FS_FT_RDMA_TX;
973 break;
2de24fed
OG
974 default:
975 return -EOPNOTSUPP;
976 }
977
978 if (num_actions > max_actions) {
979 mlx5_core_warn(dev, "too many modify header actions %d, max supported %d\n",
980 num_actions, max_actions);
981 return -EOPNOTSUPP;
982 }
983
d65dbedf 984 actions_size = MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto) * num_actions;
2de24fed
OG
985 inlen = MLX5_ST_SZ_BYTES(alloc_modify_header_context_in) + actions_size;
986
987 in = kzalloc(inlen, GFP_KERNEL);
988 if (!in)
989 return -ENOMEM;
990
991 MLX5_SET(alloc_modify_header_context_in, in, opcode,
992 MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT);
993 MLX5_SET(alloc_modify_header_context_in, in, table_type, table_type);
994 MLX5_SET(alloc_modify_header_context_in, in, num_of_actions, num_actions);
995
996 actions_in = MLX5_ADDR_OF(alloc_modify_header_context_in, in, actions);
997 memcpy(actions_in, modify_actions, actions_size);
998
2de24fed
OG
999 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
1000
2b688ea5 1001 modify_hdr->id = MLX5_GET(alloc_modify_header_context_out, out, modify_header_id);
87cd0649 1002 modify_hdr->owner = MLX5_FLOW_RESOURCE_OWNER_FW;
2de24fed
OG
1003 kfree(in);
1004 return err;
1005}
1006
2b688ea5
MG
1007static void mlx5_cmd_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
1008 struct mlx5_modify_hdr *modify_hdr)
2de24fed 1009{
31a0956e 1010 u32 in[MLX5_ST_SZ_DW(dealloc_modify_header_context_in)] = {};
2b688ea5 1011 struct mlx5_core_dev *dev = ns->dev;
2de24fed 1012
2de24fed
OG
1013 MLX5_SET(dealloc_modify_header_context_in, in, opcode,
1014 MLX5_CMD_OP_DEALLOC_MODIFY_HEADER_CONTEXT);
1015 MLX5_SET(dealloc_modify_header_context_in, in, modify_header_id,
2b688ea5 1016 modify_hdr->id);
2de24fed 1017
31a0956e 1018 mlx5_cmd_exec_in(dev, dealloc_modify_header_context, in);
2de24fed 1019}
af76c501 1020
e7e2519e
MG
1021static int mlx5_cmd_destroy_match_definer(struct mlx5_flow_root_namespace *ns,
1022 int definer_id)
1023{
1024 u32 in[MLX5_ST_SZ_DW(general_obj_in_cmd_hdr)] = {};
1025 u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
1026
1027 MLX5_SET(general_obj_in_cmd_hdr, in, opcode,
1028 MLX5_CMD_OP_DESTROY_GENERAL_OBJECT);
1029 MLX5_SET(general_obj_in_cmd_hdr, in, obj_type,
1030 MLX5_OBJ_TYPE_MATCH_DEFINER);
1031 MLX5_SET(general_obj_in_cmd_hdr, in, obj_id, definer_id);
1032
1033 return mlx5_cmd_exec(ns->dev, in, sizeof(in), out, sizeof(out));
1034}
1035
1036static int mlx5_cmd_create_match_definer(struct mlx5_flow_root_namespace *ns,
1037 u16 format_id, u32 *match_mask)
1038{
1039 u32 out[MLX5_ST_SZ_DW(create_match_definer_out)] = {};
1040 u32 in[MLX5_ST_SZ_DW(create_match_definer_in)] = {};
1041 struct mlx5_core_dev *dev = ns->dev;
1042 void *ptr;
1043 int err;
1044
1045 MLX5_SET(create_match_definer_in, in, general_obj_in_cmd_hdr.opcode,
1046 MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
1047 MLX5_SET(create_match_definer_in, in, general_obj_in_cmd_hdr.obj_type,
1048 MLX5_OBJ_TYPE_MATCH_DEFINER);
1049
1050 ptr = MLX5_ADDR_OF(create_match_definer_in, in, obj_context);
1051 MLX5_SET(match_definer, ptr, format_id, format_id);
1052
1053 ptr = MLX5_ADDR_OF(match_definer, ptr, match_mask);
1054 memcpy(ptr, match_mask, MLX5_FLD_SZ_BYTES(match_definer, match_mask));
1055
1056 err = mlx5_cmd_exec_inout(dev, create_match_definer, in, out);
1057 return err ? err : MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);
1058}
1059
8348b71c
DC
1060static u32 mlx5_cmd_get_capabilities(struct mlx5_flow_root_namespace *ns,
1061 enum fs_flow_table_type ft_type)
1062{
1063 return 0;
1064}
1065
af76c501
MB
1066static const struct mlx5_flow_cmds mlx5_flow_cmds = {
1067 .create_flow_table = mlx5_cmd_create_flow_table,
1068 .destroy_flow_table = mlx5_cmd_destroy_flow_table,
1069 .modify_flow_table = mlx5_cmd_modify_flow_table,
1070 .create_flow_group = mlx5_cmd_create_flow_group,
1071 .destroy_flow_group = mlx5_cmd_destroy_flow_group,
1072 .create_fte = mlx5_cmd_create_fte,
1073 .update_fte = mlx5_cmd_update_fte,
1074 .delete_fte = mlx5_cmd_delete_fte,
1075 .update_root_ft = mlx5_cmd_update_root_ft,
2b688ea5
MG
1076 .packet_reformat_alloc = mlx5_cmd_packet_reformat_alloc,
1077 .packet_reformat_dealloc = mlx5_cmd_packet_reformat_dealloc,
1078 .modify_header_alloc = mlx5_cmd_modify_header_alloc,
6a48faee 1079 .modify_header_dealloc = mlx5_cmd_modify_header_dealloc,
e7e2519e
MG
1080 .create_match_definer = mlx5_cmd_create_match_definer,
1081 .destroy_match_definer = mlx5_cmd_destroy_match_definer,
6a48faee
MG
1082 .set_peer = mlx5_cmd_stub_set_peer,
1083 .create_ns = mlx5_cmd_stub_create_ns,
1084 .destroy_ns = mlx5_cmd_stub_destroy_ns,
8348b71c 1085 .get_capabilities = mlx5_cmd_get_capabilities,
af76c501
MB
1086};
1087
1088static const struct mlx5_flow_cmds mlx5_flow_cmd_stubs = {
1089 .create_flow_table = mlx5_cmd_stub_create_flow_table,
1090 .destroy_flow_table = mlx5_cmd_stub_destroy_flow_table,
1091 .modify_flow_table = mlx5_cmd_stub_modify_flow_table,
1092 .create_flow_group = mlx5_cmd_stub_create_flow_group,
1093 .destroy_flow_group = mlx5_cmd_stub_destroy_flow_group,
1094 .create_fte = mlx5_cmd_stub_create_fte,
1095 .update_fte = mlx5_cmd_stub_update_fte,
1096 .delete_fte = mlx5_cmd_stub_delete_fte,
1097 .update_root_ft = mlx5_cmd_stub_update_root_ft,
2b688ea5
MG
1098 .packet_reformat_alloc = mlx5_cmd_stub_packet_reformat_alloc,
1099 .packet_reformat_dealloc = mlx5_cmd_stub_packet_reformat_dealloc,
1100 .modify_header_alloc = mlx5_cmd_stub_modify_header_alloc,
6a48faee 1101 .modify_header_dealloc = mlx5_cmd_stub_modify_header_dealloc,
e7e2519e
MG
1102 .create_match_definer = mlx5_cmd_stub_create_match_definer,
1103 .destroy_match_definer = mlx5_cmd_stub_destroy_match_definer,
6a48faee
MG
1104 .set_peer = mlx5_cmd_stub_set_peer,
1105 .create_ns = mlx5_cmd_stub_create_ns,
1106 .destroy_ns = mlx5_cmd_stub_destroy_ns,
8348b71c 1107 .get_capabilities = mlx5_cmd_stub_get_capabilities,
af76c501
MB
1108};
1109
6a48faee 1110const struct mlx5_flow_cmds *mlx5_fs_cmd_get_fw_cmds(void)
af76c501
MB
1111{
1112 return &mlx5_flow_cmds;
1113}
1114
1115static const struct mlx5_flow_cmds *mlx5_fs_cmd_get_stub_cmds(void)
1116{
1117 return &mlx5_flow_cmd_stubs;
1118}
1119
1120const struct mlx5_flow_cmds *mlx5_fs_cmd_get_default(enum fs_flow_table_type type)
1121{
1122 switch (type) {
1123 case FS_FT_NIC_RX:
1124 case FS_FT_ESW_EGRESS_ACL:
1125 case FS_FT_ESW_INGRESS_ACL:
1126 case FS_FT_FDB:
1127 case FS_FT_SNIFFER_RX:
1128 case FS_FT_SNIFFER_TX:
5f418378 1129 case FS_FT_NIC_TX:
d83eb50e 1130 case FS_FT_RDMA_RX:
24670b1a 1131 case FS_FT_RDMA_TX:
425a563a 1132 case FS_FT_PORT_SEL:
8ce78257 1133 return mlx5_fs_cmd_get_fw_cmds();
af76c501
MB
1134 default:
1135 return mlx5_fs_cmd_get_stub_cmds();
1136 }
1137}