1 /*
2  * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/mlx5/driver.h>
34 #include <linux/mlx5/device.h>
35 #include <linux/mlx5/mlx5_ifc.h>
36 
37 #include "fs_core.h"
38 #include "fs_cmd.h"
39 #include "fs_ft_pool.h"
40 #include "mlx5_core.h"
41 #include "eswitch.h"
42 
mlx5_cmd_stub_update_root_ft(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,u32 underlay_qpn,bool disconnect)43 static int mlx5_cmd_stub_update_root_ft(struct mlx5_flow_root_namespace *ns,
44 					struct mlx5_flow_table *ft,
45 					u32 underlay_qpn,
46 					bool disconnect)
47 {
48 	return 0;
49 }
50 
mlx5_cmd_stub_create_flow_table(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_table_attr * ft_attr,struct mlx5_flow_table * next_ft)51 static int mlx5_cmd_stub_create_flow_table(struct mlx5_flow_root_namespace *ns,
52 					   struct mlx5_flow_table *ft,
53 					   struct mlx5_flow_table_attr *ft_attr,
54 					   struct mlx5_flow_table *next_ft)
55 {
56 	int max_fte = ft_attr->max_fte;
57 
58 	ft->max_fte = max_fte ? roundup_pow_of_two(max_fte) : 1;
59 
60 	return 0;
61 }
62 
mlx5_cmd_stub_destroy_flow_table(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft)63 static int mlx5_cmd_stub_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
64 					    struct mlx5_flow_table *ft)
65 {
66 	return 0;
67 }
68 
mlx5_cmd_stub_modify_flow_table(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_table * next_ft)69 static int mlx5_cmd_stub_modify_flow_table(struct mlx5_flow_root_namespace *ns,
70 					   struct mlx5_flow_table *ft,
71 					   struct mlx5_flow_table *next_ft)
72 {
73 	return 0;
74 }
75 
mlx5_cmd_stub_create_flow_group(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,u32 * in,struct mlx5_flow_group * fg)76 static int mlx5_cmd_stub_create_flow_group(struct mlx5_flow_root_namespace *ns,
77 					   struct mlx5_flow_table *ft,
78 					   u32 *in,
79 					   struct mlx5_flow_group *fg)
80 {
81 	return 0;
82 }
83 
mlx5_cmd_stub_destroy_flow_group(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_group * fg)84 static int mlx5_cmd_stub_destroy_flow_group(struct mlx5_flow_root_namespace *ns,
85 					    struct mlx5_flow_table *ft,
86 					    struct mlx5_flow_group *fg)
87 {
88 	return 0;
89 }
90 
mlx5_cmd_stub_create_fte(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_group * group,struct fs_fte * fte)91 static int mlx5_cmd_stub_create_fte(struct mlx5_flow_root_namespace *ns,
92 				    struct mlx5_flow_table *ft,
93 				    struct mlx5_flow_group *group,
94 				    struct fs_fte *fte)
95 {
96 	return 0;
97 }
98 
mlx5_cmd_stub_update_fte(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_group * group,int modify_mask,struct fs_fte * fte)99 static int mlx5_cmd_stub_update_fte(struct mlx5_flow_root_namespace *ns,
100 				    struct mlx5_flow_table *ft,
101 				    struct mlx5_flow_group *group,
102 				    int modify_mask,
103 				    struct fs_fte *fte)
104 {
105 	return -EOPNOTSUPP;
106 }
107 
mlx5_cmd_stub_delete_fte(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct fs_fte * fte)108 static int mlx5_cmd_stub_delete_fte(struct mlx5_flow_root_namespace *ns,
109 				    struct mlx5_flow_table *ft,
110 				    struct fs_fte *fte)
111 {
112 	return 0;
113 }
114 
mlx5_cmd_stub_packet_reformat_alloc(struct mlx5_flow_root_namespace * ns,struct mlx5_pkt_reformat_params * params,enum mlx5_flow_namespace_type namespace,struct mlx5_pkt_reformat * pkt_reformat)115 static int mlx5_cmd_stub_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
116 					       struct mlx5_pkt_reformat_params *params,
117 					       enum mlx5_flow_namespace_type namespace,
118 					       struct mlx5_pkt_reformat *pkt_reformat)
119 {
120 	return 0;
121 }
122 
mlx5_cmd_stub_packet_reformat_dealloc(struct mlx5_flow_root_namespace * ns,struct mlx5_pkt_reformat * pkt_reformat)123 static void mlx5_cmd_stub_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
124 						  struct mlx5_pkt_reformat *pkt_reformat)
125 {
126 }
127 
mlx5_cmd_stub_modify_header_alloc(struct mlx5_flow_root_namespace * ns,u8 namespace,u8 num_actions,void * modify_actions,struct mlx5_modify_hdr * modify_hdr)128 static 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 
mlx5_cmd_stub_modify_header_dealloc(struct mlx5_flow_root_namespace * ns,struct mlx5_modify_hdr * modify_hdr)136 static void mlx5_cmd_stub_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
137 						struct mlx5_modify_hdr *modify_hdr)
138 {
139 }
140 
mlx5_cmd_stub_set_peer(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_root_namespace * peer_ns,u16 peer_vhca_id)141 static int mlx5_cmd_stub_set_peer(struct mlx5_flow_root_namespace *ns,
142 				  struct mlx5_flow_root_namespace *peer_ns,
143 				  u16 peer_vhca_id)
144 {
145 	return 0;
146 }
147 
mlx5_cmd_stub_create_ns(struct mlx5_flow_root_namespace * ns)148 static int mlx5_cmd_stub_create_ns(struct mlx5_flow_root_namespace *ns)
149 {
150 	return 0;
151 }
152 
mlx5_cmd_stub_destroy_ns(struct mlx5_flow_root_namespace * ns)153 static int mlx5_cmd_stub_destroy_ns(struct mlx5_flow_root_namespace *ns)
154 {
155 	return 0;
156 }
157 
mlx5_cmd_stub_get_capabilities(struct mlx5_flow_root_namespace * ns,enum fs_flow_table_type ft_type)158 static 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 
mlx5_cmd_set_slave_root_fdb(struct mlx5_core_dev * master,struct mlx5_core_dev * slave,bool ft_id_valid,u32 ft_id)164 static 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 
197 static int
mlx5_cmd_stub_destroy_match_definer(struct mlx5_flow_root_namespace * ns,int definer_id)198 mlx5_cmd_stub_destroy_match_definer(struct mlx5_flow_root_namespace *ns,
199 				    int definer_id)
200 {
201 	return 0;
202 }
203 
204 static int
mlx5_cmd_stub_create_match_definer(struct mlx5_flow_root_namespace * ns,u16 format_id,u32 * match_mask)205 mlx5_cmd_stub_create_match_definer(struct mlx5_flow_root_namespace *ns,
206 				   u16 format_id, u32 *match_mask)
207 {
208 	return 0;
209 }
210 
mlx5_cmd_update_root_ft(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,u32 underlay_qpn,bool disconnect)211 static int mlx5_cmd_update_root_ft(struct mlx5_flow_root_namespace *ns,
212 				   struct mlx5_flow_table *ft, u32 underlay_qpn,
213 				   bool disconnect)
214 {
215 	u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)] = {};
216 	struct mlx5_core_dev *dev = ns->dev;
217 	int err;
218 
219 	if ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
220 	    underlay_qpn == 0 &&
221 	    (ft->type != FS_FT_RDMA_RX && ft->type != FS_FT_RDMA_TX))
222 		return 0;
223 
224 	if (ft->type == FS_FT_FDB &&
225 	    mlx5_lag_is_shared_fdb(dev) &&
226 	    !mlx5_lag_is_master(dev))
227 		return 0;
228 
229 	MLX5_SET(set_flow_table_root_in, in, opcode,
230 		 MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
231 	MLX5_SET(set_flow_table_root_in, in, table_type, ft->type);
232 
233 	if (disconnect)
234 		MLX5_SET(set_flow_table_root_in, in, op_mod, 1);
235 	else
236 		MLX5_SET(set_flow_table_root_in, in, table_id, ft->id);
237 
238 	MLX5_SET(set_flow_table_root_in, in, underlay_qpn, underlay_qpn);
239 	MLX5_SET(set_flow_table_root_in, in, vport_number, ft->vport);
240 	MLX5_SET(set_flow_table_root_in, in, other_vport,
241 		 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
242 
243 	err = mlx5_cmd_exec_in(dev, set_flow_table_root, in);
244 	if (!err &&
245 	    ft->type == FS_FT_FDB &&
246 	    mlx5_lag_is_shared_fdb(dev) &&
247 	    mlx5_lag_is_master(dev)) {
248 		struct mlx5_core_dev *peer_dev;
249 		int i, j;
250 
251 		mlx5_lag_for_each_peer_mdev(dev, peer_dev, i) {
252 			err = mlx5_cmd_set_slave_root_fdb(dev, peer_dev, !disconnect,
253 							  (!disconnect) ? ft->id : 0);
254 			if (err && !disconnect) {
255 				mlx5_lag_for_each_peer_mdev(dev, peer_dev, j) {
256 					if (j < i)
257 						mlx5_cmd_set_slave_root_fdb(dev, peer_dev, 1,
258 									    ns->root_ft->id);
259 					else
260 						break;
261 				}
262 
263 				MLX5_SET(set_flow_table_root_in, in, op_mod, 0);
264 				MLX5_SET(set_flow_table_root_in, in, table_id,
265 					 ns->root_ft->id);
266 				mlx5_cmd_exec_in(dev, set_flow_table_root, in);
267 			}
268 			if (err)
269 				break;
270 		}
271 
272 	}
273 
274 	return err;
275 }
276 
mlx5_cmd_create_flow_table(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_table_attr * ft_attr,struct mlx5_flow_table * next_ft)277 static int mlx5_cmd_create_flow_table(struct mlx5_flow_root_namespace *ns,
278 				      struct mlx5_flow_table *ft,
279 				      struct mlx5_flow_table_attr *ft_attr,
280 				      struct mlx5_flow_table *next_ft)
281 {
282 	int en_encap = !!(ft->flags & MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT);
283 	int en_decap = !!(ft->flags & MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
284 	int term = !!(ft->flags & MLX5_FLOW_TABLE_TERMINATION);
285 	u32 out[MLX5_ST_SZ_DW(create_flow_table_out)] = {};
286 	u32 in[MLX5_ST_SZ_DW(create_flow_table_in)] = {};
287 	struct mlx5_core_dev *dev = ns->dev;
288 	unsigned int size;
289 	int err;
290 
291 	size = mlx5_ft_pool_get_avail_sz(dev, ft->type, ft_attr->max_fte);
292 	if (!size)
293 		return -ENOSPC;
294 
295 	MLX5_SET(create_flow_table_in, in, opcode,
296 		 MLX5_CMD_OP_CREATE_FLOW_TABLE);
297 
298 	MLX5_SET(create_flow_table_in, in, uid, ft_attr->uid);
299 	MLX5_SET(create_flow_table_in, in, table_type, ft->type);
300 	MLX5_SET(create_flow_table_in, in, flow_table_context.level, ft->level);
301 	MLX5_SET(create_flow_table_in, in, flow_table_context.log_size, size ? ilog2(size) : 0);
302 	MLX5_SET(create_flow_table_in, in, vport_number, ft->vport);
303 	MLX5_SET(create_flow_table_in, in, other_vport,
304 		 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
305 
306 	MLX5_SET(create_flow_table_in, in, flow_table_context.decap_en,
307 		 en_decap);
308 	MLX5_SET(create_flow_table_in, in, flow_table_context.reformat_en,
309 		 en_encap);
310 	MLX5_SET(create_flow_table_in, in, flow_table_context.termination_table,
311 		 term);
312 
313 	switch (ft->op_mod) {
314 	case FS_FT_OP_MOD_NORMAL:
315 		if (next_ft) {
316 			MLX5_SET(create_flow_table_in, in,
317 				 flow_table_context.table_miss_action,
318 				 MLX5_FLOW_TABLE_MISS_ACTION_FWD);
319 			MLX5_SET(create_flow_table_in, in,
320 				 flow_table_context.table_miss_id, next_ft->id);
321 		} else {
322 			MLX5_SET(create_flow_table_in, in,
323 				 flow_table_context.table_miss_action,
324 				 ft->def_miss_action);
325 		}
326 		break;
327 
328 	case FS_FT_OP_MOD_LAG_DEMUX:
329 		MLX5_SET(create_flow_table_in, in, op_mod, 0x1);
330 		if (next_ft)
331 			MLX5_SET(create_flow_table_in, in,
332 				 flow_table_context.lag_master_next_table_id,
333 				 next_ft->id);
334 		break;
335 	}
336 
337 	err = mlx5_cmd_exec_inout(dev, create_flow_table, in, out);
338 	if (!err) {
339 		ft->id = MLX5_GET(create_flow_table_out, out,
340 				  table_id);
341 		ft->max_fte = size;
342 	} else {
343 		mlx5_ft_pool_put_sz(ns->dev, size);
344 	}
345 
346 	return err;
347 }
348 
mlx5_cmd_destroy_flow_table(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft)349 static int mlx5_cmd_destroy_flow_table(struct mlx5_flow_root_namespace *ns,
350 				       struct mlx5_flow_table *ft)
351 {
352 	u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)] = {};
353 	struct mlx5_core_dev *dev = ns->dev;
354 	int err;
355 
356 	MLX5_SET(destroy_flow_table_in, in, opcode,
357 		 MLX5_CMD_OP_DESTROY_FLOW_TABLE);
358 	MLX5_SET(destroy_flow_table_in, in, table_type, ft->type);
359 	MLX5_SET(destroy_flow_table_in, in, table_id, ft->id);
360 	MLX5_SET(destroy_flow_table_in, in, vport_number, ft->vport);
361 	MLX5_SET(destroy_flow_table_in, in, other_vport,
362 		 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
363 
364 	err = mlx5_cmd_exec_in(dev, destroy_flow_table, in);
365 	if (!err)
366 		mlx5_ft_pool_put_sz(ns->dev, ft->max_fte);
367 
368 	return err;
369 }
370 
mlx5_cmd_modify_flow_table(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_table * next_ft)371 static int mlx5_cmd_modify_flow_table(struct mlx5_flow_root_namespace *ns,
372 				      struct mlx5_flow_table *ft,
373 				      struct mlx5_flow_table *next_ft)
374 {
375 	u32 in[MLX5_ST_SZ_DW(modify_flow_table_in)] = {};
376 	struct mlx5_core_dev *dev = ns->dev;
377 
378 	MLX5_SET(modify_flow_table_in, in, opcode,
379 		 MLX5_CMD_OP_MODIFY_FLOW_TABLE);
380 	MLX5_SET(modify_flow_table_in, in, table_type, ft->type);
381 	MLX5_SET(modify_flow_table_in, in, table_id, ft->id);
382 
383 	if (ft->op_mod == FS_FT_OP_MOD_LAG_DEMUX) {
384 		MLX5_SET(modify_flow_table_in, in, modify_field_select,
385 			 MLX5_MODIFY_FLOW_TABLE_LAG_NEXT_TABLE_ID);
386 		if (next_ft) {
387 			MLX5_SET(modify_flow_table_in, in,
388 				 flow_table_context.lag_master_next_table_id, next_ft->id);
389 		} else {
390 			MLX5_SET(modify_flow_table_in, in,
391 				 flow_table_context.lag_master_next_table_id, 0);
392 		}
393 	} else {
394 		MLX5_SET(modify_flow_table_in, in, vport_number, ft->vport);
395 		MLX5_SET(modify_flow_table_in, in, other_vport,
396 			 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
397 		MLX5_SET(modify_flow_table_in, in, modify_field_select,
398 			 MLX5_MODIFY_FLOW_TABLE_MISS_TABLE_ID);
399 		if (next_ft) {
400 			MLX5_SET(modify_flow_table_in, in,
401 				 flow_table_context.table_miss_action,
402 				 MLX5_FLOW_TABLE_MISS_ACTION_FWD);
403 			MLX5_SET(modify_flow_table_in, in,
404 				 flow_table_context.table_miss_id,
405 				 next_ft->id);
406 		} else {
407 			MLX5_SET(modify_flow_table_in, in,
408 				 flow_table_context.table_miss_action,
409 				 ft->def_miss_action);
410 		}
411 	}
412 
413 	return mlx5_cmd_exec_in(dev, modify_flow_table, in);
414 }
415 
mlx5_cmd_create_flow_group(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,u32 * in,struct mlx5_flow_group * fg)416 static int mlx5_cmd_create_flow_group(struct mlx5_flow_root_namespace *ns,
417 				      struct mlx5_flow_table *ft,
418 				      u32 *in,
419 				      struct mlx5_flow_group *fg)
420 {
421 	u32 out[MLX5_ST_SZ_DW(create_flow_group_out)] = {};
422 	struct mlx5_core_dev *dev = ns->dev;
423 	int err;
424 
425 	MLX5_SET(create_flow_group_in, in, opcode,
426 		 MLX5_CMD_OP_CREATE_FLOW_GROUP);
427 	MLX5_SET(create_flow_group_in, in, table_type, ft->type);
428 	MLX5_SET(create_flow_group_in, in, table_id, ft->id);
429 	MLX5_SET(create_flow_group_in, in, vport_number, ft->vport);
430 	MLX5_SET(create_flow_group_in, in, other_vport,
431 		 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
432 	err = mlx5_cmd_exec_inout(dev, create_flow_group, in, out);
433 	if (!err)
434 		fg->id = MLX5_GET(create_flow_group_out, out,
435 				  group_id);
436 	return err;
437 }
438 
mlx5_cmd_destroy_flow_group(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_group * fg)439 static int mlx5_cmd_destroy_flow_group(struct mlx5_flow_root_namespace *ns,
440 				       struct mlx5_flow_table *ft,
441 				       struct mlx5_flow_group *fg)
442 {
443 	u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)] = {};
444 	struct mlx5_core_dev *dev = ns->dev;
445 
446 	MLX5_SET(destroy_flow_group_in, in, opcode,
447 		 MLX5_CMD_OP_DESTROY_FLOW_GROUP);
448 	MLX5_SET(destroy_flow_group_in, in, table_type, ft->type);
449 	MLX5_SET(destroy_flow_group_in, in, table_id, ft->id);
450 	MLX5_SET(destroy_flow_group_in, in, group_id, fg->id);
451 	MLX5_SET(destroy_flow_group_in, in, vport_number, ft->vport);
452 	MLX5_SET(destroy_flow_group_in, in, other_vport,
453 		 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
454 	return mlx5_cmd_exec_in(dev, destroy_flow_group, in);
455 }
456 
mlx5_set_extended_dest(struct mlx5_core_dev * dev,struct fs_fte * fte,bool * extended_dest)457 static int mlx5_set_extended_dest(struct mlx5_core_dev *dev,
458 				  struct fs_fte *fte, bool *extended_dest)
459 {
460 	int fw_log_max_fdb_encap_uplink =
461 		MLX5_CAP_ESW(dev, log_max_fdb_encap_uplink);
462 	int num_fwd_destinations = 0;
463 	struct mlx5_flow_rule *dst;
464 	int num_encap = 0;
465 
466 	*extended_dest = false;
467 	if (!(fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
468 		return 0;
469 
470 	list_for_each_entry(dst, &fte->node.children, node.list) {
471 		if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER ||
472 		    dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_NONE)
473 			continue;
474 		if ((dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
475 		     dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) &&
476 		    dst->dest_attr.vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID)
477 			num_encap++;
478 		num_fwd_destinations++;
479 	}
480 	if (num_fwd_destinations > 1 && num_encap > 0)
481 		*extended_dest = true;
482 
483 	if (*extended_dest && !fw_log_max_fdb_encap_uplink) {
484 		mlx5_core_warn(dev, "FW does not support extended destination");
485 		return -EOPNOTSUPP;
486 	}
487 	if (num_encap > (1 << fw_log_max_fdb_encap_uplink)) {
488 		mlx5_core_warn(dev, "FW does not support more than %d encaps",
489 			       1 << fw_log_max_fdb_encap_uplink);
490 		return -EOPNOTSUPP;
491 	}
492 
493 	return 0;
494 }
495 
496 static void
mlx5_cmd_set_fte_flow_meter(struct fs_fte * fte,void * in_flow_context)497 mlx5_cmd_set_fte_flow_meter(struct fs_fte *fte, void *in_flow_context)
498 {
499 	void *exe_aso_ctrl;
500 	void *execute_aso;
501 
502 	execute_aso = MLX5_ADDR_OF(flow_context, in_flow_context,
503 				   execute_aso[0]);
504 	MLX5_SET(execute_aso, execute_aso, valid, 1);
505 	MLX5_SET(execute_aso, execute_aso, aso_object_id,
506 		 fte->act_dests.action.exe_aso.object_id);
507 
508 	exe_aso_ctrl = MLX5_ADDR_OF(execute_aso, execute_aso, exe_aso_ctrl);
509 	MLX5_SET(exe_aso_ctrl_flow_meter, exe_aso_ctrl, return_reg_id,
510 		 fte->act_dests.action.exe_aso.return_reg_id);
511 	MLX5_SET(exe_aso_ctrl_flow_meter, exe_aso_ctrl, aso_type,
512 		 fte->act_dests.action.exe_aso.type);
513 	MLX5_SET(exe_aso_ctrl_flow_meter, exe_aso_ctrl, init_color,
514 		 fte->act_dests.action.exe_aso.flow_meter.init_color);
515 	MLX5_SET(exe_aso_ctrl_flow_meter, exe_aso_ctrl, meter_id,
516 		 fte->act_dests.action.exe_aso.flow_meter.meter_idx);
517 }
518 
mlx5_cmd_set_fte(struct mlx5_core_dev * dev,int opmod,int modify_mask,struct mlx5_flow_table * ft,unsigned group_id,struct fs_fte * fte)519 static int mlx5_cmd_set_fte(struct mlx5_core_dev *dev,
520 			    int opmod, int modify_mask,
521 			    struct mlx5_flow_table *ft,
522 			    unsigned group_id,
523 			    struct fs_fte *fte)
524 {
525 	u32 out[MLX5_ST_SZ_DW(set_fte_out)] = {0};
526 	bool extended_dest = false;
527 	struct mlx5_flow_rule *dst;
528 	void *in_flow_context, *vlan;
529 	void *in_match_value;
530 	int reformat_id = 0;
531 	unsigned int inlen;
532 	int dst_cnt_size;
533 	u32 *in, action;
534 	void *in_dests;
535 	int err;
536 
537 	if (mlx5_set_extended_dest(dev, fte, &extended_dest))
538 		return -EOPNOTSUPP;
539 
540 	if (!extended_dest)
541 		dst_cnt_size = MLX5_ST_SZ_BYTES(dest_format_struct);
542 	else
543 		dst_cnt_size = MLX5_ST_SZ_BYTES(extended_dest_format);
544 
545 	inlen = MLX5_ST_SZ_BYTES(set_fte_in) + fte->act_dests.dests_size * dst_cnt_size;
546 	in = kvzalloc(inlen, GFP_KERNEL);
547 	if (!in)
548 		return -ENOMEM;
549 
550 	MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
551 	MLX5_SET(set_fte_in, in, op_mod, opmod);
552 	MLX5_SET(set_fte_in, in, modify_enable_mask, modify_mask);
553 	MLX5_SET(set_fte_in, in, table_type, ft->type);
554 	MLX5_SET(set_fte_in, in, table_id,   ft->id);
555 	MLX5_SET(set_fte_in, in, flow_index, fte->index);
556 	MLX5_SET(set_fte_in, in, ignore_flow_level,
557 		 !!(fte->act_dests.action.flags & FLOW_ACT_IGNORE_FLOW_LEVEL));
558 
559 	MLX5_SET(set_fte_in, in, vport_number, ft->vport);
560 	MLX5_SET(set_fte_in, in, other_vport,
561 		 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
562 
563 	in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
564 	MLX5_SET(flow_context, in_flow_context, group_id, group_id);
565 
566 	MLX5_SET(flow_context, in_flow_context, flow_tag,
567 		 fte->act_dests.flow_context.flow_tag);
568 	MLX5_SET(flow_context, in_flow_context, flow_source,
569 		 fte->act_dests.flow_context.flow_source);
570 	MLX5_SET(flow_context, in_flow_context, uplink_hairpin_en,
571 		 !!(fte->act_dests.flow_context.flags & FLOW_CONTEXT_UPLINK_HAIRPIN_EN));
572 
573 	MLX5_SET(flow_context, in_flow_context, extended_destination,
574 		 extended_dest);
575 
576 	action = fte->act_dests.action.action;
577 	if (extended_dest)
578 		action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
579 
580 	MLX5_SET(flow_context, in_flow_context, action, action);
581 
582 	if (!extended_dest && fte->act_dests.action.pkt_reformat) {
583 		struct mlx5_pkt_reformat *pkt_reformat = fte->act_dests.action.pkt_reformat;
584 
585 		if (pkt_reformat->owner == MLX5_FLOW_RESOURCE_OWNER_SW) {
586 			reformat_id = mlx5_fs_dr_action_get_pkt_reformat_id(pkt_reformat);
587 			if (reformat_id < 0) {
588 				mlx5_core_err(dev,
589 					      "Unsupported SW-owned pkt_reformat type (%d) in FW-owned table\n",
590 					      pkt_reformat->reformat_type);
591 				err = reformat_id;
592 				goto err_out;
593 			}
594 		} else {
595 			reformat_id = fte->act_dests.action.pkt_reformat->id;
596 		}
597 	}
598 
599 	MLX5_SET(flow_context, in_flow_context, packet_reformat_id, (u32)reformat_id);
600 
601 	if (fte->act_dests.action.modify_hdr) {
602 		if (fte->act_dests.action.modify_hdr->owner == MLX5_FLOW_RESOURCE_OWNER_SW) {
603 			mlx5_core_err(dev, "Can't use SW-owned modify_hdr in FW-owned table\n");
604 			err = -EOPNOTSUPP;
605 			goto err_out;
606 		}
607 
608 		MLX5_SET(flow_context, in_flow_context, modify_header_id,
609 			 fte->act_dests.action.modify_hdr->id);
610 	}
611 
612 	MLX5_SET(flow_context, in_flow_context, encrypt_decrypt_type,
613 		 fte->act_dests.action.crypto.type);
614 	MLX5_SET(flow_context, in_flow_context, encrypt_decrypt_obj_id,
615 		 fte->act_dests.action.crypto.obj_id);
616 
617 	vlan = MLX5_ADDR_OF(flow_context, in_flow_context, push_vlan);
618 
619 	MLX5_SET(vlan, vlan, ethtype, fte->act_dests.action.vlan[0].ethtype);
620 	MLX5_SET(vlan, vlan, vid, fte->act_dests.action.vlan[0].vid);
621 	MLX5_SET(vlan, vlan, prio, fte->act_dests.action.vlan[0].prio);
622 
623 	vlan = MLX5_ADDR_OF(flow_context, in_flow_context, push_vlan_2);
624 
625 	MLX5_SET(vlan, vlan, ethtype, fte->act_dests.action.vlan[1].ethtype);
626 	MLX5_SET(vlan, vlan, vid, fte->act_dests.action.vlan[1].vid);
627 	MLX5_SET(vlan, vlan, prio, fte->act_dests.action.vlan[1].prio);
628 
629 	in_match_value = MLX5_ADDR_OF(flow_context, in_flow_context,
630 				      match_value);
631 	memcpy(in_match_value, &fte->val, sizeof(fte->val));
632 
633 	in_dests = MLX5_ADDR_OF(flow_context, in_flow_context, destination);
634 	if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
635 		int list_size = 0;
636 
637 		list_for_each_entry(dst, &fte->node.children, node.list) {
638 			enum mlx5_flow_destination_type type = dst->dest_attr.type;
639 			enum mlx5_ifc_flow_destination_type ifc_type;
640 			unsigned int id;
641 
642 			if (type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
643 				continue;
644 
645 			switch (type) {
646 			case MLX5_FLOW_DESTINATION_TYPE_NONE:
647 				continue;
648 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM:
649 				id = dst->dest_attr.ft_num;
650 				ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_FLOW_TABLE;
651 				break;
652 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE:
653 				id = dst->dest_attr.ft->id;
654 				ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_FLOW_TABLE;
655 				break;
656 			case MLX5_FLOW_DESTINATION_TYPE_UPLINK:
657 			case MLX5_FLOW_DESTINATION_TYPE_VPORT:
658 				MLX5_SET(dest_format_struct, in_dests,
659 					 destination_eswitch_owner_vhca_id_valid,
660 					 !!(dst->dest_attr.vport.flags &
661 					    MLX5_FLOW_DEST_VPORT_VHCA_ID));
662 				MLX5_SET(dest_format_struct, in_dests,
663 					 destination_eswitch_owner_vhca_id,
664 					 dst->dest_attr.vport.vhca_id);
665 				if (type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) {
666 					/* destination_id is reserved */
667 					id = 0;
668 					ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_UPLINK;
669 					break;
670 				}
671 				ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_VPORT;
672 				id = dst->dest_attr.vport.num;
673 				if (extended_dest &&
674 				    dst->dest_attr.vport.pkt_reformat) {
675 					MLX5_SET(dest_format_struct, in_dests,
676 						 packet_reformat,
677 						 !!(dst->dest_attr.vport.flags &
678 						    MLX5_FLOW_DEST_VPORT_REFORMAT_ID));
679 					MLX5_SET(extended_dest_format, in_dests,
680 						 packet_reformat_id,
681 						 dst->dest_attr.vport.pkt_reformat->id);
682 				}
683 				break;
684 			case MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER:
685 				id = dst->dest_attr.sampler_id;
686 				ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_FLOW_SAMPLER;
687 				break;
688 			case MLX5_FLOW_DESTINATION_TYPE_TABLE_TYPE:
689 				MLX5_SET(dest_format_struct, in_dests,
690 					 destination_table_type, dst->dest_attr.ft->type);
691 				id = dst->dest_attr.ft->id;
692 				ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_TABLE_TYPE;
693 				break;
694 			default:
695 				id = dst->dest_attr.tir_num;
696 				ifc_type = MLX5_IFC_FLOW_DESTINATION_TYPE_TIR;
697 			}
698 
699 			MLX5_SET(dest_format_struct, in_dests, destination_type,
700 				 ifc_type);
701 			MLX5_SET(dest_format_struct, in_dests, destination_id, id);
702 			in_dests += dst_cnt_size;
703 			list_size++;
704 		}
705 
706 		MLX5_SET(flow_context, in_flow_context, destination_list_size,
707 			 list_size);
708 	}
709 
710 	if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
711 		int max_list_size = BIT(MLX5_CAP_FLOWTABLE_TYPE(dev,
712 					log_max_flow_counter,
713 					ft->type));
714 		int list_size = 0;
715 
716 		list_for_each_entry(dst, &fte->node.children, node.list) {
717 			if (dst->dest_attr.type !=
718 			    MLX5_FLOW_DESTINATION_TYPE_COUNTER)
719 				continue;
720 
721 			MLX5_SET(flow_counter_list, in_dests, flow_counter_id,
722 				 mlx5_fc_id(dst->dest_attr.counter));
723 			in_dests += dst_cnt_size;
724 			list_size++;
725 		}
726 		if (list_size > max_list_size) {
727 			err = -EINVAL;
728 			goto err_out;
729 		}
730 
731 		MLX5_SET(flow_context, in_flow_context, flow_counter_list_size,
732 			 list_size);
733 	}
734 
735 	if (fte->act_dests.action.action & MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO) {
736 		if (fte->act_dests.action.exe_aso.type == MLX5_EXE_ASO_FLOW_METER) {
737 			mlx5_cmd_set_fte_flow_meter(fte, in_flow_context);
738 		} else {
739 			err = -EOPNOTSUPP;
740 			goto err_out;
741 		}
742 	}
743 
744 	err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
745 err_out:
746 	kvfree(in);
747 	return err;
748 }
749 
mlx5_cmd_create_fte(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_group * group,struct fs_fte * fte)750 static int mlx5_cmd_create_fte(struct mlx5_flow_root_namespace *ns,
751 			       struct mlx5_flow_table *ft,
752 			       struct mlx5_flow_group *group,
753 			       struct fs_fte *fte)
754 {
755 	struct mlx5_core_dev *dev = ns->dev;
756 	unsigned int group_id = group->id;
757 
758 	return mlx5_cmd_set_fte(dev, 0, 0, ft, group_id, fte);
759 }
760 
mlx5_cmd_update_fte(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct mlx5_flow_group * fg,int modify_mask,struct fs_fte * fte)761 static int mlx5_cmd_update_fte(struct mlx5_flow_root_namespace *ns,
762 			       struct mlx5_flow_table *ft,
763 			       struct mlx5_flow_group *fg,
764 			       int modify_mask,
765 			       struct fs_fte *fte)
766 {
767 	int opmod;
768 	struct mlx5_core_dev *dev = ns->dev;
769 	int atomic_mod_cap = MLX5_CAP_FLOWTABLE(dev,
770 						flow_table_properties_nic_receive.
771 						flow_modify_en);
772 	if (!atomic_mod_cap)
773 		return -EOPNOTSUPP;
774 	opmod = 1;
775 
776 	return	mlx5_cmd_set_fte(dev, opmod, modify_mask, ft, fg->id, fte);
777 }
778 
mlx5_cmd_delete_fte(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_table * ft,struct fs_fte * fte)779 static int mlx5_cmd_delete_fte(struct mlx5_flow_root_namespace *ns,
780 			       struct mlx5_flow_table *ft,
781 			       struct fs_fte *fte)
782 {
783 	u32 in[MLX5_ST_SZ_DW(delete_fte_in)] = {};
784 	struct mlx5_core_dev *dev = ns->dev;
785 
786 	MLX5_SET(delete_fte_in, in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
787 	MLX5_SET(delete_fte_in, in, table_type, ft->type);
788 	MLX5_SET(delete_fte_in, in, table_id, ft->id);
789 	MLX5_SET(delete_fte_in, in, flow_index, fte->index);
790 	MLX5_SET(delete_fte_in, in, vport_number, ft->vport);
791 	MLX5_SET(delete_fte_in, in, other_vport,
792 		 !!(ft->flags & MLX5_FLOW_TABLE_OTHER_VPORT));
793 
794 	return mlx5_cmd_exec_in(dev, delete_fte, in);
795 }
796 
mlx5_cmd_fc_bulk_alloc(struct mlx5_core_dev * dev,enum mlx5_fc_bulk_alloc_bitmask alloc_bitmask,u32 * id)797 int mlx5_cmd_fc_bulk_alloc(struct mlx5_core_dev *dev,
798 			   enum mlx5_fc_bulk_alloc_bitmask alloc_bitmask,
799 			   u32 *id)
800 {
801 	u32 out[MLX5_ST_SZ_DW(alloc_flow_counter_out)] = {};
802 	u32 in[MLX5_ST_SZ_DW(alloc_flow_counter_in)] = {};
803 	int err;
804 
805 	MLX5_SET(alloc_flow_counter_in, in, opcode,
806 		 MLX5_CMD_OP_ALLOC_FLOW_COUNTER);
807 	MLX5_SET(alloc_flow_counter_in, in, flow_counter_bulk, alloc_bitmask);
808 
809 	err = mlx5_cmd_exec_inout(dev, alloc_flow_counter, in, out);
810 	if (!err)
811 		*id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
812 	return err;
813 }
814 
mlx5_cmd_fc_alloc(struct mlx5_core_dev * dev,u32 * id)815 int mlx5_cmd_fc_alloc(struct mlx5_core_dev *dev, u32 *id)
816 {
817 	return mlx5_cmd_fc_bulk_alloc(dev, 0, id);
818 }
819 
mlx5_cmd_fc_free(struct mlx5_core_dev * dev,u32 id)820 int mlx5_cmd_fc_free(struct mlx5_core_dev *dev, u32 id)
821 {
822 	u32 in[MLX5_ST_SZ_DW(dealloc_flow_counter_in)] = {};
823 
824 	MLX5_SET(dealloc_flow_counter_in, in, opcode,
825 		 MLX5_CMD_OP_DEALLOC_FLOW_COUNTER);
826 	MLX5_SET(dealloc_flow_counter_in, in, flow_counter_id, id);
827 	return mlx5_cmd_exec_in(dev, dealloc_flow_counter, in);
828 }
829 
mlx5_cmd_fc_query(struct mlx5_core_dev * dev,u32 id,u64 * packets,u64 * bytes)830 int mlx5_cmd_fc_query(struct mlx5_core_dev *dev, u32 id,
831 		      u64 *packets, u64 *bytes)
832 {
833 	u32 out[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
834 		MLX5_ST_SZ_BYTES(traffic_counter)] = {};
835 	u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {};
836 	void *stats;
837 	int err = 0;
838 
839 	MLX5_SET(query_flow_counter_in, in, opcode,
840 		 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
841 	MLX5_SET(query_flow_counter_in, in, op_mod, 0);
842 	MLX5_SET(query_flow_counter_in, in, flow_counter_id, id);
843 	err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
844 	if (err)
845 		return err;
846 
847 	stats = MLX5_ADDR_OF(query_flow_counter_out, out, flow_statistics);
848 	*packets = MLX5_GET64(traffic_counter, stats, packets);
849 	*bytes = MLX5_GET64(traffic_counter, stats, octets);
850 	return 0;
851 }
852 
mlx5_cmd_fc_get_bulk_query_out_len(int bulk_len)853 int mlx5_cmd_fc_get_bulk_query_out_len(int bulk_len)
854 {
855 	return MLX5_ST_SZ_BYTES(query_flow_counter_out) +
856 		MLX5_ST_SZ_BYTES(traffic_counter) * bulk_len;
857 }
858 
mlx5_cmd_fc_bulk_query(struct mlx5_core_dev * dev,u32 base_id,int bulk_len,u32 * out)859 int mlx5_cmd_fc_bulk_query(struct mlx5_core_dev *dev, u32 base_id, int bulk_len,
860 			   u32 *out)
861 {
862 	int outlen = mlx5_cmd_fc_get_bulk_query_out_len(bulk_len);
863 	u32 in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {};
864 
865 	MLX5_SET(query_flow_counter_in, in, opcode,
866 		 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
867 	MLX5_SET(query_flow_counter_in, in, flow_counter_id, base_id);
868 	MLX5_SET(query_flow_counter_in, in, num_of_counters, bulk_len);
869 	return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
870 }
871 
mlx5_cmd_packet_reformat_alloc(struct mlx5_flow_root_namespace * ns,struct mlx5_pkt_reformat_params * params,enum mlx5_flow_namespace_type namespace,struct mlx5_pkt_reformat * pkt_reformat)872 static int mlx5_cmd_packet_reformat_alloc(struct mlx5_flow_root_namespace *ns,
873 					  struct mlx5_pkt_reformat_params *params,
874 					  enum mlx5_flow_namespace_type namespace,
875 					  struct mlx5_pkt_reformat *pkt_reformat)
876 {
877 	u32 out[MLX5_ST_SZ_DW(alloc_packet_reformat_context_out)] = {};
878 	struct mlx5_core_dev *dev = ns->dev;
879 	void *packet_reformat_context_in;
880 	int max_encap_size;
881 	void *reformat;
882 	int inlen;
883 	int err;
884 	u32 *in;
885 
886 	if (namespace == MLX5_FLOW_NAMESPACE_FDB ||
887 	    namespace == MLX5_FLOW_NAMESPACE_FDB_BYPASS)
888 		max_encap_size = MLX5_CAP_ESW(dev, max_encap_header_size);
889 	else
890 		max_encap_size = MLX5_CAP_FLOWTABLE(dev, max_encap_header_size);
891 
892 	if (params->size > max_encap_size) {
893 		mlx5_core_warn(dev, "encap size %zd too big, max supported is %d\n",
894 			       params->size, max_encap_size);
895 		return -EINVAL;
896 	}
897 
898 	in = kzalloc(MLX5_ST_SZ_BYTES(alloc_packet_reformat_context_in) +
899 		     params->size, GFP_KERNEL);
900 	if (!in)
901 		return -ENOMEM;
902 
903 	packet_reformat_context_in = MLX5_ADDR_OF(alloc_packet_reformat_context_in,
904 						  in, packet_reformat_context);
905 	reformat = MLX5_ADDR_OF(packet_reformat_context_in,
906 				packet_reformat_context_in,
907 				reformat_data);
908 	inlen = reformat - (void *)in + params->size;
909 
910 	MLX5_SET(alloc_packet_reformat_context_in, in, opcode,
911 		 MLX5_CMD_OP_ALLOC_PACKET_REFORMAT_CONTEXT);
912 	MLX5_SET(packet_reformat_context_in, packet_reformat_context_in,
913 		 reformat_data_size, params->size);
914 	MLX5_SET(packet_reformat_context_in, packet_reformat_context_in,
915 		 reformat_type, params->type);
916 	MLX5_SET(packet_reformat_context_in, packet_reformat_context_in,
917 		 reformat_param_0, params->param_0);
918 	MLX5_SET(packet_reformat_context_in, packet_reformat_context_in,
919 		 reformat_param_1, params->param_1);
920 	if (params->data && params->size)
921 		memcpy(reformat, params->data, params->size);
922 
923 	err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
924 
925 	pkt_reformat->id = MLX5_GET(alloc_packet_reformat_context_out,
926 				    out, packet_reformat_id);
927 	pkt_reformat->owner = MLX5_FLOW_RESOURCE_OWNER_FW;
928 
929 	kfree(in);
930 	return err;
931 }
932 
mlx5_cmd_packet_reformat_dealloc(struct mlx5_flow_root_namespace * ns,struct mlx5_pkt_reformat * pkt_reformat)933 static void mlx5_cmd_packet_reformat_dealloc(struct mlx5_flow_root_namespace *ns,
934 					     struct mlx5_pkt_reformat *pkt_reformat)
935 {
936 	u32 in[MLX5_ST_SZ_DW(dealloc_packet_reformat_context_in)] = {};
937 	struct mlx5_core_dev *dev = ns->dev;
938 
939 	MLX5_SET(dealloc_packet_reformat_context_in, in, opcode,
940 		 MLX5_CMD_OP_DEALLOC_PACKET_REFORMAT_CONTEXT);
941 	MLX5_SET(dealloc_packet_reformat_context_in, in, packet_reformat_id,
942 		 pkt_reformat->id);
943 
944 	mlx5_cmd_exec_in(dev, dealloc_packet_reformat_context, in);
945 }
946 
mlx5_cmd_modify_header_alloc(struct mlx5_flow_root_namespace * ns,u8 namespace,u8 num_actions,void * modify_actions,struct mlx5_modify_hdr * modify_hdr)947 static int mlx5_cmd_modify_header_alloc(struct mlx5_flow_root_namespace *ns,
948 					u8 namespace, u8 num_actions,
949 					void *modify_actions,
950 					struct mlx5_modify_hdr *modify_hdr)
951 {
952 	u32 out[MLX5_ST_SZ_DW(alloc_modify_header_context_out)] = {};
953 	int max_actions, actions_size, inlen, err;
954 	struct mlx5_core_dev *dev = ns->dev;
955 	void *actions_in;
956 	u8 table_type;
957 	u32 *in;
958 
959 	switch (namespace) {
960 	case MLX5_FLOW_NAMESPACE_FDB:
961 	case MLX5_FLOW_NAMESPACE_FDB_BYPASS:
962 		max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(dev, max_modify_header_actions);
963 		table_type = FS_FT_FDB;
964 		break;
965 	case MLX5_FLOW_NAMESPACE_KERNEL_RX_MACSEC:
966 	case MLX5_FLOW_NAMESPACE_KERNEL:
967 	case MLX5_FLOW_NAMESPACE_BYPASS:
968 		max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(dev, max_modify_header_actions);
969 		table_type = FS_FT_NIC_RX;
970 		break;
971 	case MLX5_FLOW_NAMESPACE_EGRESS:
972 	case MLX5_FLOW_NAMESPACE_EGRESS_IPSEC:
973 	case MLX5_FLOW_NAMESPACE_EGRESS_MACSEC:
974 		max_actions = MLX5_CAP_FLOWTABLE_NIC_TX(dev, max_modify_header_actions);
975 		table_type = FS_FT_NIC_TX;
976 		break;
977 	case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
978 		max_actions = MLX5_CAP_ESW_INGRESS_ACL(dev, max_modify_header_actions);
979 		table_type = FS_FT_ESW_INGRESS_ACL;
980 		break;
981 	case MLX5_FLOW_NAMESPACE_RDMA_TX_MACSEC:
982 	case MLX5_FLOW_NAMESPACE_RDMA_TX:
983 		max_actions = MLX5_CAP_FLOWTABLE_RDMA_TX(dev, max_modify_header_actions);
984 		table_type = FS_FT_RDMA_TX;
985 		break;
986 	default:
987 		return -EOPNOTSUPP;
988 	}
989 
990 	if (num_actions > max_actions) {
991 		mlx5_core_warn(dev, "too many modify header actions %d, max supported %d\n",
992 			       num_actions, max_actions);
993 		return -EOPNOTSUPP;
994 	}
995 
996 	actions_size = MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto) * num_actions;
997 	inlen = MLX5_ST_SZ_BYTES(alloc_modify_header_context_in) + actions_size;
998 
999 	in = kzalloc(inlen, GFP_KERNEL);
1000 	if (!in)
1001 		return -ENOMEM;
1002 
1003 	MLX5_SET(alloc_modify_header_context_in, in, opcode,
1004 		 MLX5_CMD_OP_ALLOC_MODIFY_HEADER_CONTEXT);
1005 	MLX5_SET(alloc_modify_header_context_in, in, table_type, table_type);
1006 	MLX5_SET(alloc_modify_header_context_in, in, num_of_actions, num_actions);
1007 
1008 	actions_in = MLX5_ADDR_OF(alloc_modify_header_context_in, in, actions);
1009 	memcpy(actions_in, modify_actions, actions_size);
1010 
1011 	err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
1012 
1013 	modify_hdr->id = MLX5_GET(alloc_modify_header_context_out, out, modify_header_id);
1014 	modify_hdr->owner = MLX5_FLOW_RESOURCE_OWNER_FW;
1015 	kfree(in);
1016 	return err;
1017 }
1018 
mlx5_cmd_modify_header_dealloc(struct mlx5_flow_root_namespace * ns,struct mlx5_modify_hdr * modify_hdr)1019 static void mlx5_cmd_modify_header_dealloc(struct mlx5_flow_root_namespace *ns,
1020 					   struct mlx5_modify_hdr *modify_hdr)
1021 {
1022 	u32 in[MLX5_ST_SZ_DW(dealloc_modify_header_context_in)] = {};
1023 	struct mlx5_core_dev *dev = ns->dev;
1024 
1025 	MLX5_SET(dealloc_modify_header_context_in, in, opcode,
1026 		 MLX5_CMD_OP_DEALLOC_MODIFY_HEADER_CONTEXT);
1027 	MLX5_SET(dealloc_modify_header_context_in, in, modify_header_id,
1028 		 modify_hdr->id);
1029 
1030 	mlx5_cmd_exec_in(dev, dealloc_modify_header_context, in);
1031 }
1032 
mlx5_cmd_destroy_match_definer(struct mlx5_flow_root_namespace * ns,int definer_id)1033 static int mlx5_cmd_destroy_match_definer(struct mlx5_flow_root_namespace *ns,
1034 					  int definer_id)
1035 {
1036 	u32 in[MLX5_ST_SZ_DW(general_obj_in_cmd_hdr)] = {};
1037 	u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)];
1038 
1039 	MLX5_SET(general_obj_in_cmd_hdr, in, opcode,
1040 		 MLX5_CMD_OP_DESTROY_GENERAL_OBJECT);
1041 	MLX5_SET(general_obj_in_cmd_hdr, in, obj_type,
1042 		 MLX5_OBJ_TYPE_MATCH_DEFINER);
1043 	MLX5_SET(general_obj_in_cmd_hdr, in, obj_id, definer_id);
1044 
1045 	return mlx5_cmd_exec(ns->dev, in, sizeof(in), out, sizeof(out));
1046 }
1047 
mlx5_cmd_create_match_definer(struct mlx5_flow_root_namespace * ns,u16 format_id,u32 * match_mask)1048 static int mlx5_cmd_create_match_definer(struct mlx5_flow_root_namespace *ns,
1049 					 u16 format_id, u32 *match_mask)
1050 {
1051 	u32 out[MLX5_ST_SZ_DW(create_match_definer_out)] = {};
1052 	u32 in[MLX5_ST_SZ_DW(create_match_definer_in)] = {};
1053 	struct mlx5_core_dev *dev = ns->dev;
1054 	void *ptr;
1055 	int err;
1056 
1057 	MLX5_SET(create_match_definer_in, in, general_obj_in_cmd_hdr.opcode,
1058 		 MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
1059 	MLX5_SET(create_match_definer_in, in, general_obj_in_cmd_hdr.obj_type,
1060 		 MLX5_OBJ_TYPE_MATCH_DEFINER);
1061 
1062 	ptr = MLX5_ADDR_OF(create_match_definer_in, in, obj_context);
1063 	MLX5_SET(match_definer, ptr, format_id, format_id);
1064 
1065 	ptr = MLX5_ADDR_OF(match_definer, ptr, match_mask);
1066 	memcpy(ptr, match_mask, MLX5_FLD_SZ_BYTES(match_definer, match_mask));
1067 
1068 	err = mlx5_cmd_exec_inout(dev, create_match_definer, in, out);
1069 	return err ? err : MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);
1070 }
1071 
mlx5_cmd_get_capabilities(struct mlx5_flow_root_namespace * ns,enum fs_flow_table_type ft_type)1072 static u32 mlx5_cmd_get_capabilities(struct mlx5_flow_root_namespace *ns,
1073 				     enum fs_flow_table_type ft_type)
1074 {
1075 	return MLX5_FLOW_STEERING_CAP_DUPLICATE_MATCH;
1076 }
1077 
1078 static const struct mlx5_flow_cmds mlx5_flow_cmds = {
1079 	.create_flow_table = mlx5_cmd_create_flow_table,
1080 	.destroy_flow_table = mlx5_cmd_destroy_flow_table,
1081 	.modify_flow_table = mlx5_cmd_modify_flow_table,
1082 	.create_flow_group = mlx5_cmd_create_flow_group,
1083 	.destroy_flow_group = mlx5_cmd_destroy_flow_group,
1084 	.create_fte = mlx5_cmd_create_fte,
1085 	.update_fte = mlx5_cmd_update_fte,
1086 	.delete_fte = mlx5_cmd_delete_fte,
1087 	.update_root_ft = mlx5_cmd_update_root_ft,
1088 	.packet_reformat_alloc = mlx5_cmd_packet_reformat_alloc,
1089 	.packet_reformat_dealloc = mlx5_cmd_packet_reformat_dealloc,
1090 	.modify_header_alloc = mlx5_cmd_modify_header_alloc,
1091 	.modify_header_dealloc = mlx5_cmd_modify_header_dealloc,
1092 	.create_match_definer = mlx5_cmd_create_match_definer,
1093 	.destroy_match_definer = mlx5_cmd_destroy_match_definer,
1094 	.set_peer = mlx5_cmd_stub_set_peer,
1095 	.create_ns = mlx5_cmd_stub_create_ns,
1096 	.destroy_ns = mlx5_cmd_stub_destroy_ns,
1097 	.get_capabilities = mlx5_cmd_get_capabilities,
1098 };
1099 
1100 static const struct mlx5_flow_cmds mlx5_flow_cmd_stubs = {
1101 	.create_flow_table = mlx5_cmd_stub_create_flow_table,
1102 	.destroy_flow_table = mlx5_cmd_stub_destroy_flow_table,
1103 	.modify_flow_table = mlx5_cmd_stub_modify_flow_table,
1104 	.create_flow_group = mlx5_cmd_stub_create_flow_group,
1105 	.destroy_flow_group = mlx5_cmd_stub_destroy_flow_group,
1106 	.create_fte = mlx5_cmd_stub_create_fte,
1107 	.update_fte = mlx5_cmd_stub_update_fte,
1108 	.delete_fte = mlx5_cmd_stub_delete_fte,
1109 	.update_root_ft = mlx5_cmd_stub_update_root_ft,
1110 	.packet_reformat_alloc = mlx5_cmd_stub_packet_reformat_alloc,
1111 	.packet_reformat_dealloc = mlx5_cmd_stub_packet_reformat_dealloc,
1112 	.modify_header_alloc = mlx5_cmd_stub_modify_header_alloc,
1113 	.modify_header_dealloc = mlx5_cmd_stub_modify_header_dealloc,
1114 	.create_match_definer = mlx5_cmd_stub_create_match_definer,
1115 	.destroy_match_definer = mlx5_cmd_stub_destroy_match_definer,
1116 	.set_peer = mlx5_cmd_stub_set_peer,
1117 	.create_ns = mlx5_cmd_stub_create_ns,
1118 	.destroy_ns = mlx5_cmd_stub_destroy_ns,
1119 	.get_capabilities = mlx5_cmd_stub_get_capabilities,
1120 };
1121 
mlx5_fs_cmd_get_fw_cmds(void)1122 const struct mlx5_flow_cmds *mlx5_fs_cmd_get_fw_cmds(void)
1123 {
1124 	return &mlx5_flow_cmds;
1125 }
1126 
mlx5_fs_cmd_get_stub_cmds(void)1127 static const struct mlx5_flow_cmds *mlx5_fs_cmd_get_stub_cmds(void)
1128 {
1129 	return &mlx5_flow_cmd_stubs;
1130 }
1131 
mlx5_fs_cmd_get_default(enum fs_flow_table_type type)1132 const struct mlx5_flow_cmds *mlx5_fs_cmd_get_default(enum fs_flow_table_type type)
1133 {
1134 	switch (type) {
1135 	case FS_FT_NIC_RX:
1136 	case FS_FT_ESW_EGRESS_ACL:
1137 	case FS_FT_ESW_INGRESS_ACL:
1138 	case FS_FT_FDB:
1139 	case FS_FT_SNIFFER_RX:
1140 	case FS_FT_SNIFFER_TX:
1141 	case FS_FT_NIC_TX:
1142 	case FS_FT_RDMA_RX:
1143 	case FS_FT_RDMA_TX:
1144 	case FS_FT_PORT_SEL:
1145 		return mlx5_fs_cmd_get_fw_cmds();
1146 	default:
1147 		return mlx5_fs_cmd_get_stub_cmds();
1148 	}
1149 }
1150 
mlx5_fs_cmd_set_l2table_entry_silent(struct mlx5_core_dev * dev,u8 silent_mode)1151 int mlx5_fs_cmd_set_l2table_entry_silent(struct mlx5_core_dev *dev, u8 silent_mode)
1152 {
1153 	u32 in[MLX5_ST_SZ_DW(set_l2_table_entry_in)] = {};
1154 
1155 	if (silent_mode && !MLX5_CAP_GEN(dev, silent_mode))
1156 		return -EOPNOTSUPP;
1157 
1158 	MLX5_SET(set_l2_table_entry_in, in, opcode, MLX5_CMD_OP_SET_L2_TABLE_ENTRY);
1159 	MLX5_SET(set_l2_table_entry_in, in, silent_mode_valid, 1);
1160 	MLX5_SET(set_l2_table_entry_in, in, silent_mode, silent_mode);
1161 
1162 	return mlx5_cmd_exec_in(dev, set_l2_table_entry, in);
1163 }
1164 
mlx5_fs_cmd_set_tx_flow_table_root(struct mlx5_core_dev * dev,u32 ft_id,bool disconnect)1165 int mlx5_fs_cmd_set_tx_flow_table_root(struct mlx5_core_dev *dev, u32 ft_id, bool disconnect)
1166 {
1167 	u32 out[MLX5_ST_SZ_DW(set_flow_table_root_out)] = {};
1168 	u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)] = {};
1169 
1170 	if (disconnect && MLX5_CAP_FLOWTABLE_NIC_TX(dev, reset_root_to_default))
1171 		return -EOPNOTSUPP;
1172 
1173 	MLX5_SET(set_flow_table_root_in, in, opcode,
1174 		 MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
1175 	MLX5_SET(set_flow_table_root_in, in, table_type,
1176 		 FS_FT_NIC_TX);
1177 	if (disconnect)
1178 		MLX5_SET(set_flow_table_root_in, in, op_mod, 1);
1179 	else
1180 		MLX5_SET(set_flow_table_root_in, in, table_id, ft_id);
1181 
1182 	return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
1183 }
1184