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