xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c (revision d6c9b4de8109a3b4ca9c6c6b7c5fbc42cfeff9ae)
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/mutex.h>
34 #include <linux/mlx5/driver.h>
35 #include <linux/mlx5/vport.h>
36 #include <linux/mlx5/eswitch.h>
37 #include <net/devlink.h>
38 
39 #include "mlx5_core.h"
40 #include "fs_core.h"
41 #include "fs_cmd.h"
42 #include "fs_ft_pool.h"
43 #include "diag/fs_tracepoint.h"
44 #include "devlink.h"
45 
46 #define INIT_TREE_NODE_ARRAY_SIZE(...)	(sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
47 					 sizeof(struct init_tree_node))
48 
49 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
50 		 ...) {.type = FS_TYPE_PRIO,\
51 	.min_ft_level = min_level_val,\
52 	.num_levels = num_levels_val,\
53 	.num_leaf_prios = num_prios_val,\
54 	.caps = caps_val,\
55 	.children = (struct init_tree_node[]) {__VA_ARGS__},\
56 	.ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
57 }
58 
59 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
60 	ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
61 		 __VA_ARGS__)\
62 
63 #define ADD_NS(def_miss_act, ...) {.type = FS_TYPE_NAMESPACE,	\
64 	.def_miss_action = def_miss_act,\
65 	.children = (struct init_tree_node[]) {__VA_ARGS__},\
66 	.ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
67 }
68 
69 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
70 				   sizeof(long))
71 
72 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
73 
74 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
75 			       .caps = (long[]) {__VA_ARGS__} }
76 
77 #define FS_CHAINING_CAPS  FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
78 					   FS_CAP(flow_table_properties_nic_receive.modify_root), \
79 					   FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
80 					   FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
81 
82 #define FS_CHAINING_CAPS_EGRESS                                                \
83 	FS_REQUIRED_CAPS(                                                      \
84 		FS_CAP(flow_table_properties_nic_transmit.flow_modify_en),     \
85 		FS_CAP(flow_table_properties_nic_transmit.modify_root),        \
86 		FS_CAP(flow_table_properties_nic_transmit                      \
87 			       .identified_miss_table_mode),                   \
88 		FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
89 
90 #define FS_CHAINING_CAPS_RDMA_TX                                                \
91 	FS_REQUIRED_CAPS(                                                       \
92 		FS_CAP(flow_table_properties_nic_transmit_rdma.flow_modify_en), \
93 		FS_CAP(flow_table_properties_nic_transmit_rdma.modify_root),    \
94 		FS_CAP(flow_table_properties_nic_transmit_rdma                  \
95 			       .identified_miss_table_mode),                    \
96 		FS_CAP(flow_table_properties_nic_transmit_rdma                  \
97 			       .flow_table_modify))
98 
99 #define LEFTOVERS_NUM_LEVELS 1
100 #define LEFTOVERS_NUM_PRIOS 1
101 
102 #define RDMA_RX_COUNTERS_PRIO_NUM_LEVELS 1
103 #define RDMA_TX_COUNTERS_PRIO_NUM_LEVELS 1
104 
105 #define BY_PASS_PRIO_NUM_LEVELS 1
106 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
107 			   LEFTOVERS_NUM_PRIOS)
108 
109 #define KERNEL_RX_MACSEC_NUM_PRIOS  1
110 #define KERNEL_RX_MACSEC_NUM_LEVELS 3
111 #define KERNEL_RX_MACSEC_MIN_LEVEL (BY_PASS_MIN_LEVEL + KERNEL_RX_MACSEC_NUM_PRIOS)
112 
113 #define ETHTOOL_PRIO_NUM_LEVELS 1
114 #define ETHTOOL_NUM_PRIOS 11
115 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
116 /* Vlan, mac, ttc, inner ttc, {UDP/ANY/aRFS/accel/{esp, esp_err}}, IPsec policy,
117  * IPsec policy miss, {IPsec RoCE MPV,Alias table},IPsec RoCE policy
118  */
119 #define KERNEL_NIC_PRIO_NUM_LEVELS 11
120 #define KERNEL_NIC_NUM_PRIOS 1
121 /* One more level for tc, and one more for promisc */
122 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 2)
123 
124 #define KERNEL_NIC_PROMISC_NUM_PRIOS 1
125 #define KERNEL_NIC_PROMISC_NUM_LEVELS 1
126 
127 #define KERNEL_NIC_TC_NUM_PRIOS  1
128 #define KERNEL_NIC_TC_NUM_LEVELS 3
129 
130 #define ANCHOR_NUM_LEVELS 1
131 #define ANCHOR_NUM_PRIOS 1
132 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
133 
134 #define OFFLOADS_MAX_FT 2
135 #define OFFLOADS_NUM_PRIOS 2
136 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + OFFLOADS_NUM_PRIOS)
137 
138 #define LAG_PRIO_NUM_LEVELS 1
139 #define LAG_NUM_PRIOS 1
140 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + KERNEL_RX_MACSEC_MIN_LEVEL + 1)
141 
142 #define KERNEL_TX_IPSEC_NUM_PRIOS  1
143 #define KERNEL_TX_IPSEC_NUM_LEVELS 4
144 #define KERNEL_TX_IPSEC_MIN_LEVEL        (KERNEL_TX_IPSEC_NUM_LEVELS)
145 
146 #define KERNEL_TX_MACSEC_NUM_PRIOS  1
147 #define KERNEL_TX_MACSEC_NUM_LEVELS 2
148 #define KERNEL_TX_MACSEC_MIN_LEVEL       (KERNEL_TX_IPSEC_MIN_LEVEL + KERNEL_TX_MACSEC_NUM_PRIOS)
149 
150 struct node_caps {
151 	size_t	arr_sz;
152 	long	*caps;
153 };
154 
155 static struct init_tree_node {
156 	enum fs_node_type	type;
157 	struct init_tree_node *children;
158 	int ar_size;
159 	struct node_caps caps;
160 	int min_ft_level;
161 	int num_leaf_prios;
162 	int prio;
163 	int num_levels;
164 	enum mlx5_flow_table_miss_action def_miss_action;
165 } root_fs = {
166 	.type = FS_TYPE_NAMESPACE,
167 	.ar_size = 8,
168 	  .children = (struct init_tree_node[]){
169 		  ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
170 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
171 				  ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
172 						    BY_PASS_PRIO_NUM_LEVELS))),
173 		  ADD_PRIO(0, KERNEL_RX_MACSEC_MIN_LEVEL, 0, FS_CHAINING_CAPS,
174 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
175 				  ADD_MULTIPLE_PRIO(KERNEL_RX_MACSEC_NUM_PRIOS,
176 						    KERNEL_RX_MACSEC_NUM_LEVELS))),
177 		  ADD_PRIO(0, LAG_MIN_LEVEL, 0, FS_CHAINING_CAPS,
178 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
179 				  ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
180 						    LAG_PRIO_NUM_LEVELS))),
181 		  ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
182 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
183 				  ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS,
184 						    OFFLOADS_MAX_FT))),
185 		  ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0, FS_CHAINING_CAPS,
186 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
187 				  ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
188 						    ETHTOOL_PRIO_NUM_LEVELS))),
189 		  ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
190 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
191 				  ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS,
192 						    KERNEL_NIC_TC_NUM_LEVELS),
193 				  ADD_MULTIPLE_PRIO(KERNEL_NIC_PROMISC_NUM_PRIOS,
194 						    KERNEL_NIC_PROMISC_NUM_LEVELS),
195 				  ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
196 						    KERNEL_NIC_PRIO_NUM_LEVELS))),
197 		  ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
198 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
199 				  ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS,
200 						    LEFTOVERS_NUM_LEVELS))),
201 		  ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
202 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
203 				  ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS,
204 						    ANCHOR_NUM_LEVELS))),
205 	}
206 };
207 
208 static struct init_tree_node egress_root_fs = {
209 	.type = FS_TYPE_NAMESPACE,
210 	.ar_size = 3,
211 	.children = (struct init_tree_node[]) {
212 		ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
213 			 FS_CHAINING_CAPS_EGRESS,
214 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
215 				ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
216 						  BY_PASS_PRIO_NUM_LEVELS))),
217 		ADD_PRIO(0, KERNEL_TX_IPSEC_MIN_LEVEL, 0,
218 			 FS_CHAINING_CAPS_EGRESS,
219 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
220 				ADD_MULTIPLE_PRIO(KERNEL_TX_IPSEC_NUM_PRIOS,
221 						  KERNEL_TX_IPSEC_NUM_LEVELS))),
222 		ADD_PRIO(0, KERNEL_TX_MACSEC_MIN_LEVEL, 0,
223 			 FS_CHAINING_CAPS_EGRESS,
224 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
225 				ADD_MULTIPLE_PRIO(KERNEL_TX_MACSEC_NUM_PRIOS,
226 						  KERNEL_TX_MACSEC_NUM_LEVELS))),
227 	}
228 };
229 
230 enum {
231 	RDMA_RX_IPSEC_PRIO,
232 	RDMA_RX_MACSEC_PRIO,
233 	RDMA_RX_COUNTERS_PRIO,
234 	RDMA_RX_BYPASS_PRIO,
235 	RDMA_RX_KERNEL_PRIO,
236 };
237 
238 #define RDMA_RX_IPSEC_NUM_PRIOS 1
239 #define RDMA_RX_IPSEC_NUM_LEVELS 4
240 #define RDMA_RX_IPSEC_MIN_LEVEL  (RDMA_RX_IPSEC_NUM_LEVELS)
241 
242 #define RDMA_RX_BYPASS_MIN_LEVEL MLX5_BY_PASS_NUM_REGULAR_PRIOS
243 #define RDMA_RX_KERNEL_MIN_LEVEL (RDMA_RX_BYPASS_MIN_LEVEL + 1)
244 #define RDMA_RX_COUNTERS_MIN_LEVEL (RDMA_RX_KERNEL_MIN_LEVEL + 2)
245 
246 #define RDMA_RX_MACSEC_NUM_PRIOS 1
247 #define RDMA_RX_MACSEC_PRIO_NUM_LEVELS 2
248 #define RDMA_RX_MACSEC_MIN_LEVEL  (RDMA_RX_COUNTERS_MIN_LEVEL + RDMA_RX_MACSEC_NUM_PRIOS)
249 
250 static struct init_tree_node rdma_rx_root_fs = {
251 	.type = FS_TYPE_NAMESPACE,
252 	.ar_size = 5,
253 	.children = (struct init_tree_node[]) {
254 		[RDMA_RX_IPSEC_PRIO] =
255 		ADD_PRIO(0, RDMA_RX_IPSEC_MIN_LEVEL, 0,
256 			 FS_CHAINING_CAPS,
257 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
258 				ADD_MULTIPLE_PRIO(RDMA_RX_IPSEC_NUM_PRIOS,
259 						  RDMA_RX_IPSEC_NUM_LEVELS))),
260 		[RDMA_RX_MACSEC_PRIO] =
261 		ADD_PRIO(0, RDMA_RX_MACSEC_MIN_LEVEL, 0,
262 			 FS_CHAINING_CAPS,
263 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
264 				ADD_MULTIPLE_PRIO(RDMA_RX_MACSEC_NUM_PRIOS,
265 						  RDMA_RX_MACSEC_PRIO_NUM_LEVELS))),
266 		[RDMA_RX_COUNTERS_PRIO] =
267 		ADD_PRIO(0, RDMA_RX_COUNTERS_MIN_LEVEL, 0,
268 			 FS_CHAINING_CAPS,
269 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
270 				ADD_MULTIPLE_PRIO(MLX5_RDMA_RX_NUM_COUNTERS_PRIOS,
271 						  RDMA_RX_COUNTERS_PRIO_NUM_LEVELS))),
272 		[RDMA_RX_BYPASS_PRIO] =
273 		ADD_PRIO(0, RDMA_RX_BYPASS_MIN_LEVEL, 0,
274 			 FS_CHAINING_CAPS,
275 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
276 				ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_REGULAR_PRIOS,
277 						  BY_PASS_PRIO_NUM_LEVELS))),
278 		[RDMA_RX_KERNEL_PRIO] =
279 		ADD_PRIO(0, RDMA_RX_KERNEL_MIN_LEVEL, 0,
280 			 FS_CHAINING_CAPS,
281 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_SWITCH_DOMAIN,
282 				ADD_MULTIPLE_PRIO(1, 1))),
283 	}
284 };
285 
286 enum {
287 	RDMA_TX_COUNTERS_PRIO,
288 	RDMA_TX_IPSEC_PRIO,
289 	RDMA_TX_MACSEC_PRIO,
290 	RDMA_TX_BYPASS_PRIO,
291 };
292 
293 #define RDMA_TX_BYPASS_MIN_LEVEL MLX5_BY_PASS_NUM_PRIOS
294 #define RDMA_TX_COUNTERS_MIN_LEVEL (RDMA_TX_BYPASS_MIN_LEVEL + 1)
295 
296 #define RDMA_TX_IPSEC_NUM_PRIOS 2
297 #define RDMA_TX_IPSEC_PRIO_NUM_LEVELS 1
298 #define RDMA_TX_IPSEC_MIN_LEVEL  (RDMA_TX_COUNTERS_MIN_LEVEL + RDMA_TX_IPSEC_NUM_PRIOS)
299 
300 #define RDMA_TX_MACSEC_NUM_PRIOS 1
301 #define RDMA_TX_MACESC_PRIO_NUM_LEVELS 1
302 #define RDMA_TX_MACSEC_MIN_LEVEL  (RDMA_TX_COUNTERS_MIN_LEVEL + RDMA_TX_MACSEC_NUM_PRIOS)
303 
304 static struct init_tree_node rdma_tx_root_fs = {
305 	.type = FS_TYPE_NAMESPACE,
306 	.ar_size = 4,
307 	.children = (struct init_tree_node[]) {
308 		[RDMA_TX_COUNTERS_PRIO] =
309 		ADD_PRIO(0, RDMA_TX_COUNTERS_MIN_LEVEL, 0,
310 			 FS_CHAINING_CAPS,
311 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
312 				ADD_MULTIPLE_PRIO(MLX5_RDMA_TX_NUM_COUNTERS_PRIOS,
313 						  RDMA_TX_COUNTERS_PRIO_NUM_LEVELS))),
314 		[RDMA_TX_IPSEC_PRIO] =
315 		ADD_PRIO(0, RDMA_TX_IPSEC_MIN_LEVEL, 0,
316 			 FS_CHAINING_CAPS,
317 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
318 				ADD_MULTIPLE_PRIO(RDMA_TX_IPSEC_NUM_PRIOS,
319 						  RDMA_TX_IPSEC_PRIO_NUM_LEVELS))),
320 		[RDMA_TX_MACSEC_PRIO] =
321 		ADD_PRIO(0, RDMA_TX_MACSEC_MIN_LEVEL, 0,
322 			 FS_CHAINING_CAPS,
323 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
324 				ADD_MULTIPLE_PRIO(RDMA_TX_MACSEC_NUM_PRIOS,
325 						  RDMA_TX_MACESC_PRIO_NUM_LEVELS))),
326 		[RDMA_TX_BYPASS_PRIO] =
327 		ADD_PRIO(0, RDMA_TX_BYPASS_MIN_LEVEL, 0,
328 			 FS_CHAINING_CAPS_RDMA_TX,
329 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
330 				ADD_MULTIPLE_PRIO(RDMA_TX_BYPASS_MIN_LEVEL,
331 						  BY_PASS_PRIO_NUM_LEVELS))),
332 	}
333 };
334 
335 enum fs_i_lock_class {
336 	FS_LOCK_GRANDPARENT,
337 	FS_LOCK_PARENT,
338 	FS_LOCK_CHILD
339 };
340 
341 static const struct rhashtable_params rhash_fte = {
342 	.key_len = sizeof_field(struct fs_fte, val),
343 	.key_offset = offsetof(struct fs_fte, val),
344 	.head_offset = offsetof(struct fs_fte, hash),
345 	.automatic_shrinking = true,
346 	.min_size = 1,
347 };
348 
349 static const struct rhashtable_params rhash_fg = {
350 	.key_len = sizeof_field(struct mlx5_flow_group, mask),
351 	.key_offset = offsetof(struct mlx5_flow_group, mask),
352 	.head_offset = offsetof(struct mlx5_flow_group, hash),
353 	.automatic_shrinking = true,
354 	.min_size = 1,
355 
356 };
357 
358 static void del_hw_flow_table(struct fs_node *node);
359 static void del_hw_flow_group(struct fs_node *node);
360 static void del_hw_fte(struct fs_node *node);
361 static void del_sw_flow_table(struct fs_node *node);
362 static void del_sw_flow_group(struct fs_node *node);
363 static void del_sw_fte(struct fs_node *node);
364 static void del_sw_prio(struct fs_node *node);
365 static void del_sw_ns(struct fs_node *node);
366 /* Delete rule (destination) is special case that
367  * requires to lock the FTE for all the deletion process.
368  */
369 static void del_sw_hw_rule(struct fs_node *node);
370 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
371 				struct mlx5_flow_destination *d2);
372 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
373 static struct mlx5_flow_rule *
374 find_flow_rule(struct fs_fte *fte,
375 	       struct mlx5_flow_destination *dest);
376 
377 static void tree_init_node(struct fs_node *node,
378 			   void (*del_hw_func)(struct fs_node *),
379 			   void (*del_sw_func)(struct fs_node *))
380 {
381 	refcount_set(&node->refcount, 1);
382 	INIT_LIST_HEAD(&node->list);
383 	INIT_LIST_HEAD(&node->children);
384 	init_rwsem(&node->lock);
385 	node->del_hw_func = del_hw_func;
386 	node->del_sw_func = del_sw_func;
387 	node->active = false;
388 }
389 
390 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
391 {
392 	if (parent)
393 		refcount_inc(&parent->refcount);
394 	node->parent = parent;
395 
396 	/* Parent is the root */
397 	if (!parent)
398 		node->root = node;
399 	else
400 		node->root = parent->root;
401 }
402 
403 static int tree_get_node(struct fs_node *node)
404 {
405 	return refcount_inc_not_zero(&node->refcount);
406 }
407 
408 static void nested_down_read_ref_node(struct fs_node *node,
409 				      enum fs_i_lock_class class)
410 {
411 	if (node) {
412 		down_read_nested(&node->lock, class);
413 		refcount_inc(&node->refcount);
414 	}
415 }
416 
417 static void nested_down_write_ref_node(struct fs_node *node,
418 				       enum fs_i_lock_class class)
419 {
420 	if (node) {
421 		down_write_nested(&node->lock, class);
422 		refcount_inc(&node->refcount);
423 	}
424 }
425 
426 static void down_write_ref_node(struct fs_node *node, bool locked)
427 {
428 	if (node) {
429 		if (!locked)
430 			down_write(&node->lock);
431 		refcount_inc(&node->refcount);
432 	}
433 }
434 
435 static void up_read_ref_node(struct fs_node *node)
436 {
437 	refcount_dec(&node->refcount);
438 	up_read(&node->lock);
439 }
440 
441 static void up_write_ref_node(struct fs_node *node, bool locked)
442 {
443 	refcount_dec(&node->refcount);
444 	if (!locked)
445 		up_write(&node->lock);
446 }
447 
448 static void tree_put_node(struct fs_node *node, bool locked)
449 {
450 	struct fs_node *parent_node = node->parent;
451 
452 	if (refcount_dec_and_test(&node->refcount)) {
453 		if (node->del_hw_func)
454 			node->del_hw_func(node);
455 		if (parent_node) {
456 			down_write_ref_node(parent_node, locked);
457 			list_del_init(&node->list);
458 		}
459 		node->del_sw_func(node);
460 		if (parent_node)
461 			up_write_ref_node(parent_node, locked);
462 		node = NULL;
463 	}
464 	if (!node && parent_node)
465 		tree_put_node(parent_node, locked);
466 }
467 
468 static int tree_remove_node(struct fs_node *node, bool locked)
469 {
470 	if (refcount_read(&node->refcount) > 1) {
471 		refcount_dec(&node->refcount);
472 		return -EEXIST;
473 	}
474 	tree_put_node(node, locked);
475 	return 0;
476 }
477 
478 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
479 				 unsigned int prio)
480 {
481 	struct fs_prio *iter_prio;
482 
483 	fs_for_each_prio(iter_prio, ns) {
484 		if (iter_prio->prio == prio)
485 			return iter_prio;
486 	}
487 
488 	return NULL;
489 }
490 
491 static bool is_fwd_next_action(u32 action)
492 {
493 	return action & (MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
494 			 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
495 }
496 
497 static bool is_fwd_dest_type(enum mlx5_flow_destination_type type)
498 {
499 	return type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM ||
500 		type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE ||
501 		type == MLX5_FLOW_DESTINATION_TYPE_UPLINK ||
502 		type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
503 		type == MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER ||
504 		type == MLX5_FLOW_DESTINATION_TYPE_TIR ||
505 		type == MLX5_FLOW_DESTINATION_TYPE_RANGE ||
506 		type == MLX5_FLOW_DESTINATION_TYPE_TABLE_TYPE ||
507 		type == MLX5_FLOW_DESTINATION_TYPE_VHCA_RX;
508 }
509 
510 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
511 {
512 	int i;
513 
514 	for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
515 		if (spec->match_value[i] & ~spec->match_criteria[i]) {
516 			pr_warn("mlx5_core: match_value differs from match_criteria\n");
517 			return false;
518 		}
519 
520 	return true;
521 }
522 
523 struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
524 {
525 	struct fs_node *root;
526 	struct mlx5_flow_namespace *ns;
527 
528 	root = node->root;
529 
530 	if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
531 		pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
532 		return NULL;
533 	}
534 
535 	ns = container_of(root, struct mlx5_flow_namespace, node);
536 	return container_of(ns, struct mlx5_flow_root_namespace, ns);
537 }
538 
539 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
540 {
541 	struct mlx5_flow_root_namespace *root = find_root(node);
542 
543 	if (root)
544 		return root->dev->priv.steering;
545 	return NULL;
546 }
547 
548 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
549 {
550 	struct mlx5_flow_root_namespace *root = find_root(node);
551 
552 	if (root)
553 		return root->dev;
554 	return NULL;
555 }
556 
557 static void del_sw_ns(struct fs_node *node)
558 {
559 	kfree(node);
560 }
561 
562 static void del_sw_prio(struct fs_node *node)
563 {
564 	kfree(node);
565 }
566 
567 static void del_hw_flow_table(struct fs_node *node)
568 {
569 	struct mlx5_flow_root_namespace *root;
570 	struct mlx5_flow_table *ft;
571 	struct mlx5_core_dev *dev;
572 	int err;
573 
574 	fs_get_obj(ft, node);
575 	dev = get_dev(&ft->node);
576 	root = find_root(&ft->node);
577 	trace_mlx5_fs_del_ft(ft);
578 
579 	if (node->active) {
580 		err = root->cmds->destroy_flow_table(root, ft);
581 		if (err)
582 			mlx5_core_warn(dev, "flow steering can't destroy ft\n");
583 	}
584 }
585 
586 static void del_sw_flow_table(struct fs_node *node)
587 {
588 	struct mlx5_flow_table *ft;
589 	struct fs_prio *prio;
590 
591 	fs_get_obj(ft, node);
592 
593 	rhltable_destroy(&ft->fgs_hash);
594 	if (ft->node.parent) {
595 		fs_get_obj(prio, ft->node.parent);
596 		prio->num_ft--;
597 	}
598 	kfree(ft);
599 }
600 
601 static void modify_fte(struct fs_fte *fte)
602 {
603 	struct mlx5_flow_root_namespace *root;
604 	struct mlx5_flow_table *ft;
605 	struct mlx5_flow_group *fg;
606 	struct mlx5_core_dev *dev;
607 	int err;
608 
609 	fs_get_obj(fg, fte->node.parent);
610 	fs_get_obj(ft, fg->node.parent);
611 	dev = get_dev(&fte->node);
612 
613 	root = find_root(&ft->node);
614 	err = root->cmds->update_fte(root, ft, fg, fte->act_dests.modify_mask, fte);
615 	if (err)
616 		mlx5_core_warn(dev,
617 			       "%s can't del rule fg id=%d fte_index=%d\n",
618 			       __func__, fg->id, fte->index);
619 	fte->act_dests.modify_mask = 0;
620 }
621 
622 static void del_sw_hw_dup_rule(struct fs_node *node)
623 {
624 	struct mlx5_flow_rule *rule;
625 	struct fs_fte *fte;
626 
627 	fs_get_obj(rule, node);
628 	fs_get_obj(fte, rule->node.parent);
629 	trace_mlx5_fs_del_rule(rule);
630 
631 	if (is_fwd_next_action(rule->sw_action)) {
632 		mutex_lock(&rule->dest_attr.ft->lock);
633 		list_del(&rule->next_ft);
634 		mutex_unlock(&rule->dest_attr.ft->lock);
635 	}
636 
637 	/* If a pending rule is being deleted it means
638 	 * this is a NO APPEND rule, so there are no partial deletions,
639 	 * all the rules of the mlx5_flow_handle are going to be deleted
640 	 * and the rules aren't shared with any other mlx5_flow_handle instance
641 	 * so no need to do any bookkeeping like in del_sw_hw_rule().
642 	 */
643 
644 	kfree(rule);
645 }
646 
647 static void del_sw_hw_rule(struct fs_node *node)
648 {
649 	struct mlx5_flow_rule *rule;
650 	struct fs_fte *fte;
651 
652 	fs_get_obj(rule, node);
653 	fs_get_obj(fte, rule->node.parent);
654 	trace_mlx5_fs_del_rule(rule);
655 	if (is_fwd_next_action(rule->sw_action)) {
656 		mutex_lock(&rule->dest_attr.ft->lock);
657 		list_del(&rule->next_ft);
658 		mutex_unlock(&rule->dest_attr.ft->lock);
659 	}
660 
661 	if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER) {
662 		--fte->act_dests.dests_size;
663 		fte->act_dests.modify_mask |=
664 			BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
665 			BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
666 		fte->act_dests.action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
667 		mlx5_fc_local_put(rule->dest_attr.counter);
668 		goto out;
669 	}
670 
671 	if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_PORT) {
672 		--fte->act_dests.dests_size;
673 		fte->act_dests.modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
674 		fte->act_dests.action.action &= ~MLX5_FLOW_CONTEXT_ACTION_ALLOW;
675 		goto out;
676 	}
677 
678 	if (is_fwd_dest_type(rule->dest_attr.type)) {
679 		--fte->act_dests.dests_size;
680 		--fte->act_dests.fwd_dests;
681 
682 		if (!fte->act_dests.fwd_dests)
683 			fte->act_dests.action.action &=
684 				~MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
685 		fte->act_dests.modify_mask |=
686 			BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
687 		goto out;
688 	}
689 out:
690 	kfree(rule);
691 }
692 
693 static void switch_to_pending_act_dests(struct fs_fte *fte)
694 {
695 	struct fs_node *iter;
696 
697 	memcpy(&fte->act_dests, &fte->dup->act_dests, sizeof(fte->act_dests));
698 
699 	list_bulk_move_tail(&fte->node.children,
700 			    fte->dup->children.next,
701 			    fte->dup->children.prev);
702 
703 	list_for_each_entry(iter, &fte->node.children, list)
704 		iter->del_sw_func = del_sw_hw_rule;
705 
706 	/* Make sure the fte isn't deleted
707 	 * as mlx5_del_flow_rules() decreases the refcount
708 	 * of the fte to trigger deletion.
709 	 */
710 	tree_get_node(&fte->node);
711 }
712 
713 static void del_hw_fte(struct fs_node *node)
714 {
715 	struct mlx5_flow_root_namespace *root;
716 	struct mlx5_flow_table *ft;
717 	struct mlx5_flow_group *fg;
718 	struct mlx5_core_dev *dev;
719 	bool pending_used = false;
720 	struct fs_fte *fte;
721 	int err;
722 
723 	fs_get_obj(fte, node);
724 	fs_get_obj(fg, fte->node.parent);
725 	fs_get_obj(ft, fg->node.parent);
726 
727 	trace_mlx5_fs_del_fte(fte);
728 	WARN_ON(fte->act_dests.dests_size);
729 	dev = get_dev(&ft->node);
730 	root = find_root(&ft->node);
731 
732 	if (fte->dup && !list_empty(&fte->dup->children)) {
733 		switch_to_pending_act_dests(fte);
734 		pending_used = true;
735 	} else {
736 		/* Avoid double call to del_hw_fte */
737 		node->del_hw_func = NULL;
738 	}
739 
740 	if (node->active) {
741 		if (pending_used) {
742 			err = root->cmds->update_fte(root, ft, fg,
743 						     fte->act_dests.modify_mask, fte);
744 			if (err)
745 				mlx5_core_warn(dev,
746 					       "flow steering can't update to pending rule in index %d of flow group id %d\n",
747 					       fte->index, fg->id);
748 			fte->act_dests.modify_mask = 0;
749 		} else {
750 			err = root->cmds->delete_fte(root, ft, fte);
751 			if (err)
752 				mlx5_core_warn(dev,
753 					       "flow steering can't delete fte in index %d of flow group id %d\n",
754 					       fte->index, fg->id);
755 			node->active = false;
756 		}
757 	}
758 }
759 
760 static void del_sw_fte(struct fs_node *node)
761 {
762 	struct mlx5_flow_steering *steering = get_steering(node);
763 	struct mlx5_flow_group *fg;
764 	struct fs_fte *fte;
765 	int err;
766 
767 	fs_get_obj(fte, node);
768 	fs_get_obj(fg, fte->node.parent);
769 
770 	err = rhashtable_remove_fast(&fg->ftes_hash,
771 				     &fte->hash,
772 				     rhash_fte);
773 	WARN_ON(err);
774 	ida_free(&fg->fte_allocator, fte->index - fg->start_index);
775 	kvfree(fte->dup);
776 	kmem_cache_free(steering->ftes_cache, fte);
777 }
778 
779 static void del_hw_flow_group(struct fs_node *node)
780 {
781 	struct mlx5_flow_root_namespace *root;
782 	struct mlx5_flow_group *fg;
783 	struct mlx5_flow_table *ft;
784 	struct mlx5_core_dev *dev;
785 
786 	fs_get_obj(fg, node);
787 	fs_get_obj(ft, fg->node.parent);
788 	dev = get_dev(&ft->node);
789 	trace_mlx5_fs_del_fg(fg);
790 
791 	root = find_root(&ft->node);
792 	if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
793 		mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
794 			       fg->id, ft->id);
795 }
796 
797 static void del_sw_flow_group(struct fs_node *node)
798 {
799 	struct mlx5_flow_steering *steering = get_steering(node);
800 	struct mlx5_flow_group *fg;
801 	struct mlx5_flow_table *ft;
802 	int err;
803 
804 	fs_get_obj(fg, node);
805 	fs_get_obj(ft, fg->node.parent);
806 
807 	rhashtable_destroy(&fg->ftes_hash);
808 	ida_destroy(&fg->fte_allocator);
809 	if (ft->autogroup.active &&
810 	    fg->max_ftes == ft->autogroup.group_size &&
811 	    fg->start_index < ft->autogroup.max_fte)
812 		ft->autogroup.num_groups--;
813 	err = rhltable_remove(&ft->fgs_hash,
814 			      &fg->hash,
815 			      rhash_fg);
816 	WARN_ON(err);
817 	kmem_cache_free(steering->fgs_cache, fg);
818 }
819 
820 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
821 {
822 	int index;
823 	int ret;
824 
825 	index = ida_alloc_max(&fg->fte_allocator, fg->max_ftes - 1, GFP_KERNEL);
826 	if (index < 0)
827 		return index;
828 
829 	fte->index = index + fg->start_index;
830 retry_insert:
831 	ret = rhashtable_insert_fast(&fg->ftes_hash,
832 				     &fte->hash,
833 				     rhash_fte);
834 	if (ret) {
835 		if (ret == -EBUSY) {
836 			cond_resched();
837 			goto retry_insert;
838 		}
839 		goto err_ida_remove;
840 	}
841 
842 	tree_add_node(&fte->node, &fg->node);
843 	list_add_tail(&fte->node.list, &fg->node.children);
844 	return 0;
845 
846 err_ida_remove:
847 	ida_free(&fg->fte_allocator, index);
848 	return ret;
849 }
850 
851 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
852 				const struct mlx5_flow_spec *spec,
853 				struct mlx5_flow_act *flow_act)
854 {
855 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
856 	struct fs_fte *fte;
857 
858 	fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
859 	if (!fte)
860 		return ERR_PTR(-ENOMEM);
861 
862 	memcpy(fte->val, &spec->match_value, sizeof(fte->val));
863 	fte->node.type =  FS_TYPE_FLOW_ENTRY;
864 	fte->act_dests.action = *flow_act;
865 	fte->act_dests.flow_context = spec->flow_context;
866 
867 	tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
868 
869 	return fte;
870 }
871 
872 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
873 			       struct mlx5_flow_group *fg)
874 {
875 	rhashtable_destroy(&fg->ftes_hash);
876 	kmem_cache_free(steering->fgs_cache, fg);
877 }
878 
879 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
880 						u8 match_criteria_enable,
881 						const void *match_criteria,
882 						int start_index,
883 						int end_index)
884 {
885 	struct mlx5_flow_group *fg;
886 	int ret;
887 
888 	fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
889 	if (!fg)
890 		return ERR_PTR(-ENOMEM);
891 
892 	ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
893 	if (ret) {
894 		kmem_cache_free(steering->fgs_cache, fg);
895 		return ERR_PTR(ret);
896 	}
897 
898 	ida_init(&fg->fte_allocator);
899 	fg->mask.match_criteria_enable = match_criteria_enable;
900 	memcpy(&fg->mask.match_criteria, match_criteria,
901 	       sizeof(fg->mask.match_criteria));
902 	fg->node.type =  FS_TYPE_FLOW_GROUP;
903 	fg->start_index = start_index;
904 	fg->max_ftes = end_index - start_index + 1;
905 
906 	return fg;
907 }
908 
909 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
910 						       u8 match_criteria_enable,
911 						       const void *match_criteria,
912 						       int start_index,
913 						       int end_index,
914 						       struct list_head *prev)
915 {
916 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
917 	struct mlx5_flow_group *fg;
918 	int ret;
919 
920 	fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
921 			      start_index, end_index);
922 	if (IS_ERR(fg))
923 		return fg;
924 
925 	/* initialize refcnt, add to parent list */
926 	ret = rhltable_insert(&ft->fgs_hash,
927 			      &fg->hash,
928 			      rhash_fg);
929 	if (ret) {
930 		dealloc_flow_group(steering, fg);
931 		return ERR_PTR(ret);
932 	}
933 
934 	tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
935 	tree_add_node(&fg->node, &ft->node);
936 	/* Add node to group list */
937 	list_add(&fg->node.list, prev);
938 	atomic_inc(&ft->node.version);
939 
940 	return fg;
941 }
942 
943 static struct mlx5_flow_table *
944 alloc_flow_table(struct mlx5_flow_table_attr *ft_attr, u16 vport,
945 		 enum fs_flow_table_type table_type,
946 		 enum fs_flow_table_op_mod op_mod)
947 {
948 	struct mlx5_flow_table *ft;
949 	int ret;
950 
951 	ft = kzalloc_obj(*ft);
952 	if (!ft)
953 		return ERR_PTR(-ENOMEM);
954 
955 	ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
956 	if (ret) {
957 		kfree(ft);
958 		return ERR_PTR(ret);
959 	}
960 
961 	ft->level = ft_attr->level;
962 	ft->node.type = FS_TYPE_FLOW_TABLE;
963 	ft->op_mod = op_mod;
964 	ft->type = table_type;
965 	ft->vport = vport;
966 	ft->esw_owner_vhca_id = ft_attr->esw_owner_vhca_id;
967 	ft->flags = ft_attr->flags;
968 	INIT_LIST_HEAD(&ft->fwd_rules);
969 	mutex_init(&ft->lock);
970 
971 	return ft;
972 }
973 
974 /* If reverse is false, then we search for the first flow table in the
975  * root sub-tree from start(closest from right), else we search for the
976  * last flow table in the root sub-tree till start(closest from left).
977  */
978 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node  *root,
979 							 struct list_head *start,
980 							 bool reverse)
981 {
982 #define list_advance_entry(pos, reverse)		\
983 	((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
984 
985 #define list_for_each_advance_continue(pos, head, reverse)	\
986 	for (pos = list_advance_entry(pos, reverse);		\
987 	     &pos->list != (head);				\
988 	     pos = list_advance_entry(pos, reverse))
989 
990 	struct fs_node *iter = list_entry(start, struct fs_node, list);
991 	struct mlx5_flow_table *ft = NULL;
992 
993 	if (!root)
994 		return NULL;
995 
996 	list_for_each_advance_continue(iter, &root->children, reverse) {
997 		if (iter->type == FS_TYPE_FLOW_TABLE) {
998 			fs_get_obj(ft, iter);
999 			return ft;
1000 		}
1001 		ft = find_closest_ft_recursive(iter, &iter->children, reverse);
1002 		if (ft)
1003 			return ft;
1004 	}
1005 
1006 	return ft;
1007 }
1008 
1009 static struct fs_node *find_prio_chains_parent(struct fs_node *parent,
1010 					       struct fs_node **child)
1011 {
1012 	struct fs_node *node = NULL;
1013 
1014 	while (parent && parent->type != FS_TYPE_PRIO_CHAINS) {
1015 		node = parent;
1016 		parent = parent->parent;
1017 	}
1018 
1019 	if (child)
1020 		*child = node;
1021 
1022 	return parent;
1023 }
1024 
1025 /* If reverse is false then return the first flow table next to the passed node
1026  * in the tree, else return the last flow table before the node in the tree.
1027  * If skip is true, skip the flow tables in the same prio_chains prio.
1028  */
1029 static struct mlx5_flow_table *find_closest_ft(struct fs_node *node, bool reverse,
1030 					       bool skip)
1031 {
1032 	struct fs_node *prio_chains_parent = NULL;
1033 	struct mlx5_flow_table *ft = NULL;
1034 	struct fs_node *curr_node;
1035 	struct fs_node *parent;
1036 
1037 	if (skip)
1038 		prio_chains_parent = find_prio_chains_parent(node, NULL);
1039 	parent = node->parent;
1040 	curr_node = node;
1041 	while (!ft && parent) {
1042 		if (parent != prio_chains_parent)
1043 			ft = find_closest_ft_recursive(parent, &curr_node->list,
1044 						       reverse);
1045 		curr_node = parent;
1046 		parent = curr_node->parent;
1047 	}
1048 	return ft;
1049 }
1050 
1051 /* Assuming all the tree is locked by mutex chain lock */
1052 static struct mlx5_flow_table *find_next_chained_ft(struct fs_node *node)
1053 {
1054 	return find_closest_ft(node, false, true);
1055 }
1056 
1057 /* Assuming all the tree is locked by mutex chain lock */
1058 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_node *node)
1059 {
1060 	return find_closest_ft(node, true, true);
1061 }
1062 
1063 static struct mlx5_flow_table *find_next_fwd_ft(struct mlx5_flow_table *ft,
1064 						struct mlx5_flow_act *flow_act)
1065 {
1066 	struct fs_prio *prio;
1067 	bool next_ns;
1068 
1069 	next_ns = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS;
1070 	fs_get_obj(prio, next_ns ? ft->ns->node.parent : ft->node.parent);
1071 
1072 	return find_next_chained_ft(&prio->node);
1073 }
1074 
1075 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
1076 			       struct fs_prio *prio,
1077 			       struct mlx5_flow_table *ft)
1078 {
1079 	struct mlx5_flow_root_namespace *root = find_root(&prio->node);
1080 	struct mlx5_flow_table *iter;
1081 	int err;
1082 
1083 	fs_for_each_ft(iter, prio) {
1084 		err = root->cmds->modify_flow_table(root, iter, ft);
1085 		if (err) {
1086 			mlx5_core_err(dev,
1087 				      "Failed to modify flow table id %d, type %d, err %d\n",
1088 				      iter->id, iter->type, err);
1089 			/* The driver is out of sync with the FW */
1090 			return err;
1091 		}
1092 	}
1093 	return 0;
1094 }
1095 
1096 static struct mlx5_flow_table *find_closet_ft_prio_chains(struct fs_node *node,
1097 							  struct fs_node *parent,
1098 							  struct fs_node **child,
1099 							  bool reverse)
1100 {
1101 	struct mlx5_flow_table *ft;
1102 
1103 	ft = find_closest_ft(node, reverse, false);
1104 
1105 	if (ft && parent == find_prio_chains_parent(&ft->node, child))
1106 		return ft;
1107 
1108 	return NULL;
1109 }
1110 
1111 /* Connect flow tables from previous priority of prio to ft */
1112 static int connect_prev_fts(struct mlx5_core_dev *dev,
1113 			    struct mlx5_flow_table *ft,
1114 			    struct fs_prio *prio)
1115 {
1116 	struct fs_node *prio_parent, *parent = NULL, *child, *node;
1117 	struct mlx5_flow_table *prev_ft;
1118 	int err = 0;
1119 
1120 	prio_parent = find_prio_chains_parent(&prio->node, &child);
1121 
1122 	/* return directly if not under the first sub ns of prio_chains prio */
1123 	if (prio_parent && !list_is_first(&child->list, &prio_parent->children))
1124 		return 0;
1125 
1126 	prev_ft = find_prev_chained_ft(&prio->node);
1127 	while (prev_ft) {
1128 		struct fs_prio *prev_prio;
1129 
1130 		fs_get_obj(prev_prio, prev_ft->node.parent);
1131 		err = connect_fts_in_prio(dev, prev_prio, ft);
1132 		if (err)
1133 			break;
1134 
1135 		if (!parent) {
1136 			parent = find_prio_chains_parent(&prev_prio->node, &child);
1137 			if (!parent)
1138 				break;
1139 		}
1140 
1141 		node = child;
1142 		prev_ft = find_closet_ft_prio_chains(node, parent, &child, true);
1143 	}
1144 	return err;
1145 }
1146 
1147 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
1148 				 *prio)
1149 {
1150 	struct mlx5_flow_root_namespace *root = find_root(&prio->node);
1151 	struct mlx5_ft_underlay_qp *uqp;
1152 	int min_level = INT_MAX;
1153 	int err = 0;
1154 	u32 qpn;
1155 
1156 	if (root->root_ft)
1157 		min_level = root->root_ft->level;
1158 
1159 	if (ft->level >= min_level)
1160 		return 0;
1161 
1162 	if (list_empty(&root->underlay_qpns)) {
1163 		/* Don't set any QPN (zero) in case QPN list is empty */
1164 		qpn = 0;
1165 		err = root->cmds->update_root_ft(root, ft, qpn, false);
1166 	} else {
1167 		list_for_each_entry(uqp, &root->underlay_qpns, list) {
1168 			qpn = uqp->qpn;
1169 			err = root->cmds->update_root_ft(root, ft,
1170 							 qpn, false);
1171 			if (err)
1172 				break;
1173 		}
1174 	}
1175 
1176 	if (err)
1177 		mlx5_core_warn(root->dev,
1178 			       "Update root flow table of id(%u) qpn(%d) failed\n",
1179 			       ft->id, qpn);
1180 	else
1181 		root->root_ft = ft;
1182 
1183 	return err;
1184 }
1185 
1186 static bool rule_is_pending(struct fs_fte *fte, struct mlx5_flow_rule *rule)
1187 {
1188 	struct mlx5_flow_rule *tmp_rule;
1189 	struct fs_node *iter;
1190 
1191 	if (!fte->dup || list_empty(&fte->dup->children))
1192 		return false;
1193 
1194 	list_for_each_entry(iter, &fte->dup->children, list) {
1195 		tmp_rule = container_of(iter, struct mlx5_flow_rule, node);
1196 
1197 		if (tmp_rule == rule)
1198 			return true;
1199 	}
1200 
1201 	return false;
1202 }
1203 
1204 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
1205 					 struct mlx5_flow_destination *dest)
1206 {
1207 	struct mlx5_flow_root_namespace *root;
1208 	struct fs_fte_action *act_dests;
1209 	struct mlx5_flow_table *ft;
1210 	struct mlx5_flow_group *fg;
1211 	bool pending = false;
1212 	struct fs_fte *fte;
1213 	int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1214 	int err = 0;
1215 
1216 	fs_get_obj(fte, rule->node.parent);
1217 
1218 	pending = rule_is_pending(fte, rule);
1219 	if (pending)
1220 		act_dests = &fte->dup->act_dests;
1221 	else
1222 		act_dests = &fte->act_dests;
1223 
1224 	if (!(act_dests->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1225 		return -EINVAL;
1226 	down_write_ref_node(&fte->node, false);
1227 	fs_get_obj(fg, fte->node.parent);
1228 	fs_get_obj(ft, fg->node.parent);
1229 
1230 	memcpy(&rule->dest_attr, dest, sizeof(*dest));
1231 	root = find_root(&ft->node);
1232 	if (!pending)
1233 		err = root->cmds->update_fte(root, ft, fg,
1234 					     modify_mask, fte);
1235 	up_write_ref_node(&fte->node, false);
1236 
1237 	return err;
1238 }
1239 
1240 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
1241 				 struct mlx5_flow_destination *new_dest,
1242 				 struct mlx5_flow_destination *old_dest)
1243 {
1244 	int i;
1245 
1246 	if (!old_dest) {
1247 		if (handle->num_rules != 1)
1248 			return -EINVAL;
1249 		return _mlx5_modify_rule_destination(handle->rule[0],
1250 						     new_dest);
1251 	}
1252 
1253 	for (i = 0; i < handle->num_rules; i++) {
1254 		if (mlx5_flow_dests_cmp(old_dest, &handle->rule[i]->dest_attr))
1255 			return _mlx5_modify_rule_destination(handle->rule[i],
1256 							     new_dest);
1257 	}
1258 
1259 	return -EINVAL;
1260 }
1261 
1262 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft  */
1263 static int connect_fwd_rules(struct mlx5_core_dev *dev,
1264 			     struct mlx5_flow_table *new_next_ft,
1265 			     struct mlx5_flow_table *old_next_ft)
1266 {
1267 	struct mlx5_flow_destination dest = {};
1268 	struct mlx5_flow_rule *iter;
1269 	int err = 0;
1270 
1271 	/* new_next_ft and old_next_ft could be NULL only
1272 	 * when we create/destroy the anchor flow table.
1273 	 */
1274 	if (!new_next_ft || !old_next_ft)
1275 		return 0;
1276 
1277 	dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1278 	dest.ft = new_next_ft;
1279 
1280 	mutex_lock(&old_next_ft->lock);
1281 	list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
1282 	mutex_unlock(&old_next_ft->lock);
1283 	list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
1284 		if ((iter->sw_action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS) &&
1285 		    iter->ft->ns == new_next_ft->ns)
1286 			continue;
1287 
1288 		err = _mlx5_modify_rule_destination(iter, &dest);
1289 		if (err)
1290 			pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
1291 			       new_next_ft->id);
1292 	}
1293 	return 0;
1294 }
1295 
1296 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
1297 			      struct fs_prio *prio)
1298 {
1299 	struct mlx5_flow_table *next_ft, *first_ft;
1300 	int err = 0;
1301 
1302 	/* Connect_prev_fts and update_root_ft_create are mutually exclusive */
1303 
1304 	first_ft = list_first_entry_or_null(&prio->node.children,
1305 					    struct mlx5_flow_table, node.list);
1306 	if (!first_ft || first_ft->level > ft->level) {
1307 		err = connect_prev_fts(dev, ft, prio);
1308 		if (err)
1309 			return err;
1310 
1311 		next_ft = first_ft ? first_ft : find_next_chained_ft(&prio->node);
1312 		err = connect_fwd_rules(dev, ft, next_ft);
1313 		if (err)
1314 			return err;
1315 	}
1316 
1317 	if (MLX5_CAP_FLOWTABLE(dev,
1318 			       flow_table_properties_nic_receive.modify_root))
1319 		err = update_root_ft_create(ft, prio);
1320 	return err;
1321 }
1322 
1323 static void list_add_flow_table(struct mlx5_flow_table *ft,
1324 				struct fs_prio *prio)
1325 {
1326 	struct list_head *prev = &prio->node.children;
1327 	struct mlx5_flow_table *iter;
1328 
1329 	fs_for_each_ft(iter, prio) {
1330 		if (iter->level > ft->level)
1331 			break;
1332 		prev = &iter->node.list;
1333 	}
1334 	list_add(&ft->node.list, prev);
1335 }
1336 
1337 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1338 							struct mlx5_flow_table_attr *ft_attr,
1339 							enum fs_flow_table_op_mod op_mod,
1340 							u16 vport)
1341 {
1342 	struct mlx5_flow_root_namespace *root = find_root(&ns->node);
1343 	bool unmanaged = ft_attr->flags & MLX5_FLOW_TABLE_UNMANAGED;
1344 	struct mlx5_flow_table *next_ft;
1345 	struct fs_prio *fs_prio = NULL;
1346 	struct mlx5_flow_table *ft;
1347 	int err;
1348 
1349 	if (!root) {
1350 		pr_err("mlx5: flow steering failed to find root of namespace\n");
1351 		return ERR_PTR(-ENODEV);
1352 	}
1353 
1354 	mutex_lock(&root->chain_lock);
1355 	fs_prio = find_prio(ns, ft_attr->prio);
1356 	if (!fs_prio) {
1357 		err = -EINVAL;
1358 		goto unlock_root;
1359 	}
1360 	if (!unmanaged) {
1361 		/* The level is related to the
1362 		 * priority level range.
1363 		 */
1364 		if (ft_attr->level >= fs_prio->num_levels) {
1365 			err = -ENOSPC;
1366 			goto unlock_root;
1367 		}
1368 
1369 		ft_attr->level += fs_prio->start_level;
1370 	}
1371 
1372 	/* The level is related to the
1373 	 * priority level range.
1374 	 */
1375 	ft = alloc_flow_table(ft_attr, vport, root->table_type, op_mod);
1376 	if (IS_ERR(ft)) {
1377 		err = PTR_ERR(ft);
1378 		goto unlock_root;
1379 	}
1380 
1381 	tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1382 	next_ft = unmanaged ? ft_attr->next_ft :
1383 			      find_next_chained_ft(&fs_prio->node);
1384 	ft->def_miss_action = ns->def_miss_action;
1385 	ft->ns = ns;
1386 	err = root->cmds->create_flow_table(root, ft, ft_attr, next_ft);
1387 	if (err)
1388 		goto free_ft;
1389 
1390 	if (!unmanaged) {
1391 		err = connect_flow_table(root->dev, ft, fs_prio);
1392 		if (err)
1393 			goto destroy_ft;
1394 	}
1395 
1396 	ft->node.active = true;
1397 	down_write_ref_node(&fs_prio->node, false);
1398 	if (!unmanaged) {
1399 		tree_add_node(&ft->node, &fs_prio->node);
1400 		list_add_flow_table(ft, fs_prio);
1401 	} else {
1402 		ft->node.root = fs_prio->node.root;
1403 	}
1404 	fs_prio->num_ft++;
1405 	up_write_ref_node(&fs_prio->node, false);
1406 	mutex_unlock(&root->chain_lock);
1407 	trace_mlx5_fs_add_ft(ft);
1408 	return ft;
1409 destroy_ft:
1410 	root->cmds->destroy_flow_table(root, ft);
1411 free_ft:
1412 	rhltable_destroy(&ft->fgs_hash);
1413 	kfree(ft);
1414 unlock_root:
1415 	mutex_unlock(&root->chain_lock);
1416 	return ERR_PTR(err);
1417 }
1418 
1419 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1420 					       struct mlx5_flow_table_attr *ft_attr)
1421 {
1422 	return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1423 }
1424 EXPORT_SYMBOL(mlx5_create_flow_table);
1425 
1426 u32 mlx5_flow_table_id(struct mlx5_flow_table *ft)
1427 {
1428 	return ft->id;
1429 }
1430 EXPORT_SYMBOL(mlx5_flow_table_id);
1431 
1432 struct mlx5_flow_table *
1433 mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1434 			     struct mlx5_flow_table_attr *ft_attr, u16 vport)
1435 {
1436 	return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1437 }
1438 
1439 struct mlx5_flow_table*
1440 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1441 				 struct mlx5_flow_table_attr *ft_attr)
1442 {
1443 	return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1444 }
1445 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1446 
1447 #define MAX_FLOW_GROUP_SIZE BIT(24)
1448 struct mlx5_flow_table*
1449 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1450 				    struct mlx5_flow_table_attr *ft_attr)
1451 {
1452 	int num_reserved_entries = ft_attr->autogroup.num_reserved_entries;
1453 	int max_num_groups = ft_attr->autogroup.max_num_groups;
1454 	struct mlx5_flow_table *ft;
1455 	int autogroups_max_fte;
1456 
1457 	ft = mlx5_create_vport_flow_table(ns, ft_attr, ft_attr->vport);
1458 	if (IS_ERR(ft))
1459 		return ft;
1460 
1461 	autogroups_max_fte = ft->max_fte - num_reserved_entries;
1462 	if (max_num_groups > autogroups_max_fte)
1463 		goto err_validate;
1464 	if (num_reserved_entries > ft->max_fte)
1465 		goto err_validate;
1466 
1467 	/* Align the number of groups according to the largest group size */
1468 	if (autogroups_max_fte / (max_num_groups + 1) > MAX_FLOW_GROUP_SIZE)
1469 		max_num_groups = (autogroups_max_fte / MAX_FLOW_GROUP_SIZE) - 1;
1470 
1471 	ft->autogroup.active = true;
1472 	ft->autogroup.required_groups = max_num_groups;
1473 	ft->autogroup.max_fte = autogroups_max_fte;
1474 	/* We save place for flow groups in addition to max types */
1475 	ft->autogroup.group_size = autogroups_max_fte / (max_num_groups + 1);
1476 
1477 	return ft;
1478 
1479 err_validate:
1480 	mlx5_destroy_flow_table(ft);
1481 	return ERR_PTR(-ENOSPC);
1482 }
1483 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1484 
1485 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1486 					       u32 *fg_in)
1487 {
1488 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1489 	void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1490 					    fg_in, match_criteria);
1491 	u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1492 					    fg_in,
1493 					    match_criteria_enable);
1494 	int start_index = MLX5_GET(create_flow_group_in, fg_in,
1495 				   start_flow_index);
1496 	int end_index = MLX5_GET(create_flow_group_in, fg_in,
1497 				 end_flow_index);
1498 	struct mlx5_flow_group *fg;
1499 	int err;
1500 
1501 	if (ft->autogroup.active && start_index < ft->autogroup.max_fte)
1502 		return ERR_PTR(-EPERM);
1503 
1504 	down_write_ref_node(&ft->node, false);
1505 	fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1506 				     start_index, end_index,
1507 				     ft->node.children.prev);
1508 	up_write_ref_node(&ft->node, false);
1509 	if (IS_ERR(fg))
1510 		return fg;
1511 
1512 	err = root->cmds->create_flow_group(root, ft, fg_in, fg);
1513 	if (err) {
1514 		tree_put_node(&fg->node, false);
1515 		return ERR_PTR(err);
1516 	}
1517 	trace_mlx5_fs_add_fg(fg);
1518 	fg->node.active = true;
1519 
1520 	return fg;
1521 }
1522 EXPORT_SYMBOL(mlx5_create_flow_group);
1523 
1524 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1525 {
1526 	struct mlx5_flow_rule *rule;
1527 
1528 	rule = kzalloc_obj(*rule);
1529 	if (!rule)
1530 		return NULL;
1531 
1532 	INIT_LIST_HEAD(&rule->next_ft);
1533 	rule->node.type = FS_TYPE_FLOW_DEST;
1534 	if (dest)
1535 		memcpy(&rule->dest_attr, dest, sizeof(*dest));
1536 	else
1537 		rule->dest_attr.type = MLX5_FLOW_DESTINATION_TYPE_NONE;
1538 
1539 	return rule;
1540 }
1541 
1542 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1543 {
1544 	struct mlx5_flow_handle *handle;
1545 
1546 	handle = kzalloc_flex(*handle, rule, num_rules);
1547 	if (!handle)
1548 		return NULL;
1549 
1550 	handle->num_rules = num_rules;
1551 
1552 	return handle;
1553 }
1554 
1555 static void destroy_flow_handle_dup(struct mlx5_flow_handle *handle,
1556 				    int i)
1557 {
1558 	for (; --i >= 0;) {
1559 		list_del(&handle->rule[i]->node.list);
1560 		kfree(handle->rule[i]);
1561 	}
1562 	kfree(handle);
1563 }
1564 
1565 static void destroy_flow_handle(struct fs_fte *fte,
1566 				struct mlx5_flow_handle *handle,
1567 				struct mlx5_flow_destination *dest,
1568 				int i)
1569 {
1570 	for (; --i >= 0;) {
1571 		if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1572 			fte->act_dests.dests_size--;
1573 			list_del(&handle->rule[i]->node.list);
1574 			kfree(handle->rule[i]);
1575 		}
1576 	}
1577 	kfree(handle);
1578 }
1579 
1580 static struct mlx5_flow_handle *
1581 create_flow_handle_dup(struct list_head *children,
1582 		       struct mlx5_flow_destination *dest,
1583 		       int dest_num,
1584 		       struct fs_fte_action *act_dests)
1585 {
1586 	static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1587 	static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1588 	struct mlx5_flow_rule *rule = NULL;
1589 	struct mlx5_flow_handle *handle;
1590 	int i = 0;
1591 	int type;
1592 
1593 	handle = alloc_handle((dest_num) ? dest_num : 1);
1594 	if (!handle)
1595 		return NULL;
1596 
1597 	do {
1598 		rule = alloc_rule(dest + i);
1599 		if (!rule)
1600 			goto free_rules;
1601 
1602 		/* Add dest to dests list- we need flow tables to be in the
1603 		 * end of the list for forward to next prio rules.
1604 		 */
1605 		tree_init_node(&rule->node, NULL, del_sw_hw_dup_rule);
1606 		if (dest &&
1607 		    dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1608 			list_add(&rule->node.list, children);
1609 		else
1610 			list_add_tail(&rule->node.list, children);
1611 
1612 		if (dest) {
1613 			act_dests->dests_size++;
1614 
1615 			if (is_fwd_dest_type(dest[i].type))
1616 				act_dests->fwd_dests++;
1617 
1618 			type = dest[i].type ==
1619 				MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1620 			act_dests->modify_mask |= type ? count : dst;
1621 		}
1622 		handle->rule[i] = rule;
1623 	} while (++i < dest_num);
1624 
1625 	return handle;
1626 
1627 free_rules:
1628 	destroy_flow_handle_dup(handle, i);
1629 	act_dests->dests_size = 0;
1630 	act_dests->fwd_dests = 0;
1631 
1632 	return NULL;
1633 }
1634 
1635 static struct mlx5_flow_handle *
1636 create_flow_handle(struct fs_fte *fte,
1637 		   struct mlx5_flow_destination *dest,
1638 		   int dest_num,
1639 		   int *modify_mask,
1640 		   bool *new_rule)
1641 {
1642 	struct mlx5_flow_handle *handle;
1643 	struct mlx5_flow_rule *rule = NULL;
1644 	static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1645 	static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1646 	int type;
1647 	int i = 0;
1648 
1649 	handle = alloc_handle((dest_num) ? dest_num : 1);
1650 	if (!handle)
1651 		return ERR_PTR(-ENOMEM);
1652 
1653 	do {
1654 		if (dest) {
1655 			rule = find_flow_rule(fte, dest + i);
1656 			if (rule) {
1657 				refcount_inc(&rule->node.refcount);
1658 				goto rule_found;
1659 			}
1660 		}
1661 
1662 		*new_rule = true;
1663 		rule = alloc_rule(dest + i);
1664 		if (!rule)
1665 			goto free_rules;
1666 
1667 		/* Add dest to dests list- we need flow tables to be in the
1668 		 * end of the list for forward to next prio rules.
1669 		 */
1670 		tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1671 		if (dest &&
1672 		    dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1673 			list_add(&rule->node.list, &fte->node.children);
1674 		else
1675 			list_add_tail(&rule->node.list, &fte->node.children);
1676 		if (dest) {
1677 			fte->act_dests.dests_size++;
1678 
1679 			if (is_fwd_dest_type(dest[i].type))
1680 				fte->act_dests.fwd_dests++;
1681 
1682 			type = dest[i].type ==
1683 				MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1684 			*modify_mask |= type ? count : dst;
1685 		}
1686 rule_found:
1687 		handle->rule[i] = rule;
1688 	} while (++i < dest_num);
1689 
1690 	return handle;
1691 
1692 free_rules:
1693 	destroy_flow_handle(fte, handle, dest, i);
1694 	return ERR_PTR(-ENOMEM);
1695 }
1696 
1697 /* fte should not be deleted while calling this function */
1698 static struct mlx5_flow_handle *
1699 add_rule_fte(struct fs_fte *fte,
1700 	     struct mlx5_flow_group *fg,
1701 	     struct mlx5_flow_destination *dest,
1702 	     int dest_num,
1703 	     bool update_action)
1704 {
1705 	struct mlx5_flow_root_namespace *root;
1706 	struct mlx5_flow_handle *handle;
1707 	struct mlx5_flow_table *ft;
1708 	int modify_mask = 0;
1709 	int err;
1710 	bool new_rule = false;
1711 
1712 	handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1713 				    &new_rule);
1714 	if (IS_ERR(handle) || !new_rule)
1715 		goto out;
1716 
1717 	if (update_action)
1718 		modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1719 
1720 	fs_get_obj(ft, fg->node.parent);
1721 	root = find_root(&fg->node);
1722 	if (!(fte->status & FS_FTE_STATUS_EXISTING))
1723 		err = root->cmds->create_fte(root, ft, fg, fte);
1724 	else
1725 		err = root->cmds->update_fte(root, ft, fg, modify_mask, fte);
1726 	if (err)
1727 		goto free_handle;
1728 
1729 	fte->node.active = true;
1730 	fte->status |= FS_FTE_STATUS_EXISTING;
1731 	atomic_inc(&fg->node.version);
1732 
1733 out:
1734 	return handle;
1735 
1736 free_handle:
1737 	destroy_flow_handle(fte, handle, dest, handle->num_rules);
1738 	return ERR_PTR(err);
1739 }
1740 
1741 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table  *ft,
1742 						     const struct mlx5_flow_spec *spec)
1743 {
1744 	struct list_head *prev = &ft->node.children;
1745 	u32 max_fte = ft->autogroup.max_fte;
1746 	unsigned int candidate_index = 0;
1747 	unsigned int group_size = 0;
1748 	struct mlx5_flow_group *fg;
1749 
1750 	if (!ft->autogroup.active)
1751 		return ERR_PTR(-ENOENT);
1752 
1753 	if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1754 		group_size = ft->autogroup.group_size;
1755 
1756 	/*  max_fte == ft->autogroup.max_types */
1757 	if (group_size == 0)
1758 		group_size = 1;
1759 
1760 	/* sorted by start_index */
1761 	fs_for_each_fg(fg, ft) {
1762 		if (candidate_index + group_size > fg->start_index)
1763 			candidate_index = fg->start_index + fg->max_ftes;
1764 		else
1765 			break;
1766 		prev = &fg->node.list;
1767 	}
1768 
1769 	if (candidate_index + group_size > max_fte)
1770 		return ERR_PTR(-ENOSPC);
1771 
1772 	fg = alloc_insert_flow_group(ft,
1773 				     spec->match_criteria_enable,
1774 				     spec->match_criteria,
1775 				     candidate_index,
1776 				     candidate_index + group_size - 1,
1777 				     prev);
1778 	if (IS_ERR(fg))
1779 		goto out;
1780 
1781 	if (group_size == ft->autogroup.group_size)
1782 		ft->autogroup.num_groups++;
1783 
1784 out:
1785 	return fg;
1786 }
1787 
1788 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1789 				  struct mlx5_flow_group *fg)
1790 {
1791 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1792 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1793 	void *match_criteria_addr;
1794 	u8 src_esw_owner_mask_on;
1795 	void *misc;
1796 	int err;
1797 	u32 *in;
1798 
1799 	in = kvzalloc(inlen, GFP_KERNEL);
1800 	if (!in)
1801 		return -ENOMEM;
1802 
1803 	MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1804 		 fg->mask.match_criteria_enable);
1805 	MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1806 	MLX5_SET(create_flow_group_in, in, end_flow_index,   fg->start_index +
1807 		 fg->max_ftes - 1);
1808 
1809 	misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1810 			    misc_parameters);
1811 	src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1812 					 source_eswitch_owner_vhca_id);
1813 	MLX5_SET(create_flow_group_in, in,
1814 		 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1815 
1816 	match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1817 					   in, match_criteria);
1818 	memcpy(match_criteria_addr, fg->mask.match_criteria,
1819 	       sizeof(fg->mask.match_criteria));
1820 
1821 	err = root->cmds->create_flow_group(root, ft, in, fg);
1822 	if (!err) {
1823 		fg->node.active = true;
1824 		trace_mlx5_fs_add_fg(fg);
1825 	}
1826 
1827 	kvfree(in);
1828 	return err;
1829 }
1830 
1831 int mlx5_fs_get_packet_reformat_id(struct mlx5_pkt_reformat *pkt_reformat,
1832 				   u32 *id)
1833 {
1834 	switch (pkt_reformat->owner) {
1835 	case MLX5_FLOW_RESOURCE_OWNER_FW:
1836 		*id = pkt_reformat->id;
1837 		return 0;
1838 	case MLX5_FLOW_RESOURCE_OWNER_SW:
1839 		return mlx5_fs_dr_action_get_pkt_reformat_id(pkt_reformat, id);
1840 	case MLX5_FLOW_RESOURCE_OWNER_HWS:
1841 		return mlx5_fs_hws_action_get_pkt_reformat_id(pkt_reformat, id);
1842 	default:
1843 		return -EINVAL;
1844 	}
1845 }
1846 
1847 static bool mlx5_pkt_reformat_cmp(struct mlx5_pkt_reformat *p1,
1848 				  struct mlx5_pkt_reformat *p2)
1849 {
1850 	int err1, err2;
1851 	u32 id1, id2;
1852 
1853 	if (p1->owner != p2->owner)
1854 		return false;
1855 
1856 	err1 = mlx5_fs_get_packet_reformat_id(p1, &id1);
1857 	err2 = mlx5_fs_get_packet_reformat_id(p2, &id2);
1858 
1859 	return !err1 && !err2 && id1 == id2;
1860 }
1861 
1862 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1863 				struct mlx5_flow_destination *d2)
1864 {
1865 	if (d1->type == d2->type) {
1866 		if (((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
1867 		      d1->type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) &&
1868 		     d1->vport.num == d2->vport.num &&
1869 		     d1->vport.flags == d2->vport.flags &&
1870 		     ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ?
1871 		      (d1->vport.vhca_id == d2->vport.vhca_id) : true) &&
1872 		     ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
1873 		      mlx5_pkt_reformat_cmp(d1->vport.pkt_reformat,
1874 					    d2->vport.pkt_reformat) : true)) ||
1875 		    (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1876 		     d1->ft == d2->ft) ||
1877 		    (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1878 		     d1->tir_num == d2->tir_num) ||
1879 		    (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1880 		     d1->ft_num == d2->ft_num) ||
1881 		    (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER &&
1882 		     d1->sampler_id == d2->sampler_id) ||
1883 		    (d1->type == MLX5_FLOW_DESTINATION_TYPE_RANGE &&
1884 		     d1->range.field == d2->range.field &&
1885 		     d1->range.hit_ft == d2->range.hit_ft &&
1886 		     d1->range.miss_ft == d2->range.miss_ft &&
1887 		     d1->range.min == d2->range.min &&
1888 		     d1->range.max == d2->range.max) ||
1889 		    (d1->type == MLX5_FLOW_DESTINATION_TYPE_VHCA_RX &&
1890 		     d1->vhca.id == d2->vhca.id))
1891 			return true;
1892 	}
1893 
1894 	return false;
1895 }
1896 
1897 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1898 					     struct mlx5_flow_destination *dest)
1899 {
1900 	struct mlx5_flow_rule *rule;
1901 
1902 	list_for_each_entry(rule, &fte->node.children, node.list) {
1903 		if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1904 			return rule;
1905 	}
1906 	return NULL;
1907 }
1908 
1909 static bool check_conflicting_actions_vlan(const struct mlx5_fs_vlan *vlan0,
1910 					   const struct mlx5_fs_vlan *vlan1)
1911 {
1912 	return vlan0->ethtype != vlan1->ethtype ||
1913 	       vlan0->vid != vlan1->vid ||
1914 	       vlan0->prio != vlan1->prio;
1915 }
1916 
1917 static bool check_conflicting_actions(const struct mlx5_flow_act *act1,
1918 				      const struct mlx5_flow_act *act2)
1919 {
1920 	u32 action1 = act1->action;
1921 	u32 action2 = act2->action;
1922 	u32 xored_actions;
1923 
1924 	xored_actions = action1 ^ action2;
1925 
1926 	/* if one rule only wants to count, it's ok */
1927 	if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1928 	    action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1929 		return false;
1930 
1931 	if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP  |
1932 			     MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
1933 			     MLX5_FLOW_CONTEXT_ACTION_DECAP |
1934 			     MLX5_FLOW_CONTEXT_ACTION_MOD_HDR  |
1935 			     MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1936 			     MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1937 			     MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1938 			     MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
1939 		return true;
1940 
1941 	if (action1 & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT &&
1942 	    act1->pkt_reformat != act2->pkt_reformat)
1943 		return true;
1944 
1945 	if (action1 & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR &&
1946 	    act1->modify_hdr != act2->modify_hdr)
1947 		return true;
1948 
1949 	if (action1 & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH &&
1950 	    check_conflicting_actions_vlan(&act1->vlan[0], &act2->vlan[0]))
1951 		return true;
1952 
1953 	if (action1 & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2 &&
1954 	    check_conflicting_actions_vlan(&act1->vlan[1], &act2->vlan[1]))
1955 		return true;
1956 
1957 	return false;
1958 }
1959 
1960 static int check_conflicting_ftes(struct fs_fte *fte,
1961 				  const struct mlx5_flow_context *flow_context,
1962 				  const struct mlx5_flow_act *flow_act)
1963 {
1964 	if (check_conflicting_actions(flow_act, &fte->act_dests.action)) {
1965 		mlx5_core_warn(get_dev(&fte->node),
1966 			       "Found two FTEs with conflicting actions\n");
1967 		return -EEXIST;
1968 	}
1969 
1970 	if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) &&
1971 	    fte->act_dests.flow_context.flow_tag != flow_context->flow_tag) {
1972 		mlx5_core_warn(get_dev(&fte->node),
1973 			       "FTE flow tag %u already exists with different flow tag %u\n",
1974 			       fte->act_dests.flow_context.flow_tag,
1975 			       flow_context->flow_tag);
1976 		return -EEXIST;
1977 	}
1978 
1979 	return 0;
1980 }
1981 
1982 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1983 					    const struct mlx5_flow_spec *spec,
1984 					    struct mlx5_flow_act *flow_act,
1985 					    struct mlx5_flow_destination *dest,
1986 					    int dest_num,
1987 					    struct fs_fte *fte)
1988 {
1989 	struct mlx5_flow_handle *handle;
1990 	int old_action;
1991 	int i;
1992 	int ret;
1993 
1994 	ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act);
1995 	if (ret)
1996 		return ERR_PTR(ret);
1997 
1998 	old_action = fte->act_dests.action.action;
1999 	fte->act_dests.action.action |= flow_act->action;
2000 	handle = add_rule_fte(fte, fg, dest, dest_num,
2001 			      old_action != flow_act->action);
2002 	if (IS_ERR(handle)) {
2003 		fte->act_dests.action.action = old_action;
2004 		return handle;
2005 	}
2006 	trace_mlx5_fs_set_fte(fte, false);
2007 
2008 	/* Link newly added rules into the tree. */
2009 	for (i = 0; i < handle->num_rules; i++) {
2010 		if (!handle->rule[i]->node.parent) {
2011 			tree_add_node(&handle->rule[i]->node, &fte->node);
2012 			trace_mlx5_fs_add_rule(handle->rule[i]);
2013 		}
2014 	}
2015 	return handle;
2016 }
2017 
2018 static bool counter_is_valid(u32 action)
2019 {
2020 	return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
2021 			  MLX5_FLOW_CONTEXT_ACTION_ALLOW |
2022 			  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
2023 }
2024 
2025 static bool dest_is_valid(struct mlx5_flow_destination *dest,
2026 			  struct mlx5_flow_act *flow_act,
2027 			  struct mlx5_flow_table *ft)
2028 {
2029 	bool ignore_level = flow_act->flags & FLOW_ACT_IGNORE_FLOW_LEVEL;
2030 	u32 action = flow_act->action;
2031 
2032 	if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
2033 		return counter_is_valid(action);
2034 
2035 	if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
2036 		return true;
2037 
2038 	if (ignore_level) {
2039 		if (ft->type != FS_FT_FDB &&
2040 		    ft->type != FS_FT_NIC_RX &&
2041 		    ft->type != FS_FT_NIC_TX)
2042 			return false;
2043 
2044 		if (dest->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
2045 		    ft->type != dest->ft->type)
2046 			return false;
2047 	}
2048 
2049 	if (!dest || ((dest->type ==
2050 	    MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
2051 	    (dest->ft->level <= ft->level && !ignore_level)))
2052 		return false;
2053 	return true;
2054 }
2055 
2056 struct match_list {
2057 	struct list_head	list;
2058 	struct mlx5_flow_group *g;
2059 };
2060 
2061 static void free_match_list(struct match_list *head, bool ft_locked)
2062 {
2063 	struct match_list *iter, *match_tmp;
2064 
2065 	list_for_each_entry_safe(iter, match_tmp, &head->list,
2066 				 list) {
2067 		tree_put_node(&iter->g->node, ft_locked);
2068 		list_del(&iter->list);
2069 		kfree(iter);
2070 	}
2071 }
2072 
2073 static int build_match_list(struct match_list *match_head,
2074 			    struct mlx5_flow_table *ft,
2075 			    const struct mlx5_flow_spec *spec,
2076 			    struct mlx5_flow_group *fg,
2077 			    bool ft_locked)
2078 {
2079 	struct rhlist_head *tmp, *list;
2080 	struct mlx5_flow_group *g;
2081 
2082 	rcu_read_lock();
2083 	INIT_LIST_HEAD(&match_head->list);
2084 	/* Collect all fgs which has a matching match_criteria */
2085 	list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
2086 	/* RCU is atomic, we can't execute FW commands here */
2087 	rhl_for_each_entry_rcu(g, tmp, list, hash) {
2088 		struct match_list *curr_match;
2089 
2090 		if (fg && fg != g)
2091 			continue;
2092 
2093 		if (unlikely(!tree_get_node(&g->node)))
2094 			continue;
2095 
2096 		curr_match = kmalloc_obj(*curr_match, GFP_ATOMIC);
2097 		if (!curr_match) {
2098 			rcu_read_unlock();
2099 			free_match_list(match_head, ft_locked);
2100 			return -ENOMEM;
2101 		}
2102 		curr_match->g = g;
2103 		list_add_tail(&curr_match->list, &match_head->list);
2104 	}
2105 	rcu_read_unlock();
2106 	return 0;
2107 }
2108 
2109 static u64 matched_fgs_get_version(struct list_head *match_head)
2110 {
2111 	struct match_list *iter;
2112 	u64 version = 0;
2113 
2114 	list_for_each_entry(iter, match_head, list)
2115 		version += (u64)atomic_read(&iter->g->node.version);
2116 	return version;
2117 }
2118 
2119 static struct fs_fte *
2120 lookup_fte_locked(struct mlx5_flow_group *g,
2121 		  const u32 *match_value,
2122 		  bool take_write)
2123 {
2124 	struct fs_fte *fte_tmp;
2125 
2126 	if (take_write)
2127 		nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
2128 	else
2129 		nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
2130 	fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
2131 					 rhash_fte);
2132 	if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
2133 		fte_tmp = NULL;
2134 		goto out;
2135 	}
2136 
2137 	nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
2138 
2139 	if (!fte_tmp->node.active) {
2140 		up_write_ref_node(&fte_tmp->node, false);
2141 
2142 		if (take_write)
2143 			up_write_ref_node(&g->node, false);
2144 		else
2145 			up_read_ref_node(&g->node);
2146 
2147 		tree_put_node(&fte_tmp->node, false);
2148 
2149 		return NULL;
2150 	}
2151 
2152 out:
2153 	if (take_write)
2154 		up_write_ref_node(&g->node, false);
2155 	else
2156 		up_read_ref_node(&g->node);
2157 	return fte_tmp;
2158 }
2159 
2160 /* Native capability lacks support for adding an additional match with the same value
2161  * to the same flow group. To accommodate the NO APPEND flag in these scenarios,
2162  * we include the new rule in the existing flow table entry (fte) without immediate
2163  * hardware commitment. When a request is made to delete the corresponding hardware rule,
2164  * we then commit the pending rule to hardware.
2165  */
2166 static struct mlx5_flow_handle *
2167 add_rule_dup_match_fte(struct fs_fte *fte,
2168 		       const struct mlx5_flow_spec *spec,
2169 		       struct mlx5_flow_act *flow_act,
2170 		       struct mlx5_flow_destination *dest,
2171 		       int dest_num)
2172 {
2173 	struct mlx5_flow_handle *handle;
2174 	struct fs_fte_dup *dup;
2175 	int i = 0;
2176 
2177 	if (!fte->dup) {
2178 		dup = kvzalloc_obj(*dup);
2179 		if (!dup)
2180 			return ERR_PTR(-ENOMEM);
2181 		/* dup will be freed when the fte is freed
2182 		 * this way we don't allocate / free dup on every rule deletion
2183 		 * or creation
2184 		 */
2185 		INIT_LIST_HEAD(&dup->children);
2186 		fte->dup = dup;
2187 	}
2188 
2189 	if (!list_empty(&fte->dup->children)) {
2190 		mlx5_core_warn(get_dev(&fte->node),
2191 			       "Can have only a single duplicate rule\n");
2192 
2193 		return ERR_PTR(-EEXIST);
2194 	}
2195 
2196 	fte->dup->act_dests.action = *flow_act;
2197 	fte->dup->act_dests.flow_context = spec->flow_context;
2198 	fte->dup->act_dests.dests_size = 0;
2199 	fte->dup->act_dests.fwd_dests = 0;
2200 	fte->dup->act_dests.modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
2201 
2202 	handle = create_flow_handle_dup(&fte->dup->children,
2203 					dest, dest_num,
2204 					&fte->dup->act_dests);
2205 	if (!handle)
2206 		return ERR_PTR(-ENOMEM);
2207 
2208 	for (i = 0; i < handle->num_rules; i++) {
2209 		tree_add_node(&handle->rule[i]->node, &fte->node);
2210 		trace_mlx5_fs_add_rule(handle->rule[i]);
2211 	}
2212 
2213 	return handle;
2214 }
2215 
2216 static struct mlx5_flow_handle *
2217 try_add_to_existing_fg(struct mlx5_flow_table *ft,
2218 		       struct list_head *match_head,
2219 		       const struct mlx5_flow_spec *spec,
2220 		       struct mlx5_flow_act *flow_act,
2221 		       struct mlx5_flow_destination *dest,
2222 		       int dest_num,
2223 		       int ft_version)
2224 {
2225 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
2226 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2227 	struct mlx5_flow_group *g;
2228 	struct mlx5_flow_handle *rule;
2229 	struct match_list *iter;
2230 	bool take_write = false;
2231 	bool try_again = false;
2232 	struct fs_fte *fte;
2233 	u64  version = 0;
2234 	int err;
2235 
2236 	fte = alloc_fte(ft, spec, flow_act);
2237 	if (IS_ERR(fte))
2238 		return  ERR_PTR(-ENOMEM);
2239 
2240 search_again_locked:
2241 	if (flow_act->flags & FLOW_ACT_NO_APPEND &&
2242 	    (root->cmds->get_capabilities(root, root->table_type) &
2243 	     MLX5_FLOW_STEERING_CAP_DUPLICATE_MATCH))
2244 		goto skip_search;
2245 	version = matched_fgs_get_version(match_head);
2246 	/* Try to find an fte with identical match value and attempt update its
2247 	 * action.
2248 	 */
2249 	list_for_each_entry(iter, match_head, list) {
2250 		struct fs_fte *fte_tmp;
2251 
2252 		g = iter->g;
2253 		fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
2254 		if (!fte_tmp)
2255 			continue;
2256 		if (flow_act->flags & FLOW_ACT_NO_APPEND)
2257 			rule = add_rule_dup_match_fte(fte_tmp, spec, flow_act, dest, dest_num);
2258 		else
2259 			rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp);
2260 		/* No error check needed here, because insert_fte() is not called */
2261 		up_write_ref_node(&fte_tmp->node, false);
2262 		tree_put_node(&fte_tmp->node, false);
2263 		kmem_cache_free(steering->ftes_cache, fte);
2264 		return rule;
2265 	}
2266 
2267 skip_search:
2268 	/* No group with matching fte found, or we skipped the search.
2269 	 * Try to add a new fte to any matching fg.
2270 	 */
2271 
2272 	/* Check the ft version, for case that new flow group
2273 	 * was added while the fgs weren't locked
2274 	 */
2275 	if (atomic_read(&ft->node.version) != ft_version) {
2276 		rule = ERR_PTR(-EAGAIN);
2277 		goto out;
2278 	}
2279 
2280 	/* Check the fgs version. If version have changed it could be that an
2281 	 * FTE with the same match value was added while the fgs weren't
2282 	 * locked.
2283 	 */
2284 	if (!(flow_act->flags & FLOW_ACT_NO_APPEND) &&
2285 	    version != matched_fgs_get_version(match_head)) {
2286 		take_write = true;
2287 		goto search_again_locked;
2288 	}
2289 
2290 	list_for_each_entry(iter, match_head, list) {
2291 		g = iter->g;
2292 
2293 		nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
2294 
2295 		if (!g->node.active) {
2296 			try_again = true;
2297 			up_write_ref_node(&g->node, false);
2298 			continue;
2299 		}
2300 
2301 		err = insert_fte(g, fte);
2302 		if (err) {
2303 			up_write_ref_node(&g->node, false);
2304 			if (err == -ENOSPC)
2305 				continue;
2306 			kmem_cache_free(steering->ftes_cache, fte);
2307 			return ERR_PTR(err);
2308 		}
2309 
2310 		nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
2311 		up_write_ref_node(&g->node, false);
2312 		rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
2313 		up_write_ref_node(&fte->node, false);
2314 		if (IS_ERR(rule))
2315 			tree_put_node(&fte->node, false);
2316 		return rule;
2317 	}
2318 	err = try_again ? -EAGAIN : -ENOENT;
2319 	rule = ERR_PTR(err);
2320 out:
2321 	kmem_cache_free(steering->ftes_cache, fte);
2322 	return rule;
2323 }
2324 
2325 static struct mlx5_flow_handle *
2326 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
2327 		     const struct mlx5_flow_spec *spec,
2328 		     struct mlx5_flow_act *flow_act,
2329 		     struct mlx5_flow_destination *dest,
2330 		     int dest_num)
2331 
2332 {
2333 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
2334 	struct mlx5_flow_handle *rule;
2335 	struct match_list match_head;
2336 	struct mlx5_flow_group *g;
2337 	bool take_write = false;
2338 	struct fs_fte *fte;
2339 	int version;
2340 	int err;
2341 	int i;
2342 
2343 	if (!check_valid_spec(spec))
2344 		return ERR_PTR(-EINVAL);
2345 
2346 	if (flow_act->fg && ft->autogroup.active)
2347 		return ERR_PTR(-EINVAL);
2348 
2349 	if (dest && dest_num <= 0)
2350 		return ERR_PTR(-EINVAL);
2351 
2352 	for (i = 0; i < dest_num; i++) {
2353 		if (!dest_is_valid(&dest[i], flow_act, ft))
2354 			return ERR_PTR(-EINVAL);
2355 	}
2356 	nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
2357 search_again_locked:
2358 	version = atomic_read(&ft->node.version);
2359 
2360 	/* Collect all fgs which has a matching match_criteria */
2361 	err = build_match_list(&match_head, ft, spec, flow_act->fg, take_write);
2362 	if (err) {
2363 		if (take_write)
2364 			up_write_ref_node(&ft->node, false);
2365 		else
2366 			up_read_ref_node(&ft->node);
2367 		return ERR_PTR(err);
2368 	}
2369 
2370 	if (!take_write)
2371 		up_read_ref_node(&ft->node);
2372 
2373 	rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
2374 				      dest_num, version);
2375 	free_match_list(&match_head, take_write);
2376 	if (!IS_ERR(rule) ||
2377 	    (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
2378 		if (take_write)
2379 			up_write_ref_node(&ft->node, false);
2380 		return rule;
2381 	}
2382 
2383 	if (!take_write) {
2384 		nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
2385 		take_write = true;
2386 	}
2387 
2388 	if (PTR_ERR(rule) == -EAGAIN ||
2389 	    version != atomic_read(&ft->node.version))
2390 		goto search_again_locked;
2391 
2392 	g = alloc_auto_flow_group(ft, spec);
2393 	if (IS_ERR(g)) {
2394 		rule = ERR_CAST(g);
2395 		up_write_ref_node(&ft->node, false);
2396 		return rule;
2397 	}
2398 
2399 	fte = alloc_fte(ft, spec, flow_act);
2400 	if (IS_ERR(fte)) {
2401 		up_write_ref_node(&ft->node, false);
2402 		err = PTR_ERR(fte);
2403 		goto err_alloc_fte;
2404 	}
2405 
2406 	nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
2407 	up_write_ref_node(&ft->node, false);
2408 
2409 	err = create_auto_flow_group(ft, g);
2410 	if (err)
2411 		goto err_release_fg;
2412 
2413 	err = insert_fte(g, fte);
2414 	if (err)
2415 		goto err_release_fg;
2416 
2417 	nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
2418 	up_write_ref_node(&g->node, false);
2419 	rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
2420 	up_write_ref_node(&fte->node, false);
2421 	if (IS_ERR(rule))
2422 		tree_put_node(&fte->node, false);
2423 	tree_put_node(&g->node, false);
2424 	return rule;
2425 
2426 err_release_fg:
2427 	up_write_ref_node(&g->node, false);
2428 	kmem_cache_free(steering->ftes_cache, fte);
2429 err_alloc_fte:
2430 	tree_put_node(&g->node, false);
2431 	return ERR_PTR(err);
2432 }
2433 
2434 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
2435 {
2436 	return ((ft->type == FS_FT_NIC_RX) &&
2437 		(MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
2438 }
2439 
2440 struct mlx5_flow_handle *
2441 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
2442 		    const struct mlx5_flow_spec *spec,
2443 		    struct mlx5_flow_act *flow_act,
2444 		    struct mlx5_flow_destination *dest,
2445 		    int num_dest)
2446 {
2447 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2448 	static const struct mlx5_flow_spec zero_spec = {};
2449 	struct mlx5_flow_destination *gen_dest = NULL;
2450 	struct mlx5_flow_table *next_ft = NULL;
2451 	struct mlx5_flow_handle *handle = NULL;
2452 	u32 sw_action = flow_act->action;
2453 	int i;
2454 
2455 	if (!spec)
2456 		spec = &zero_spec;
2457 
2458 	if (!is_fwd_next_action(sw_action))
2459 		return _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
2460 
2461 	if (!fwd_next_prio_supported(ft))
2462 		return ERR_PTR(-EOPNOTSUPP);
2463 
2464 	mutex_lock(&root->chain_lock);
2465 	next_ft = find_next_fwd_ft(ft, flow_act);
2466 	if (!next_ft) {
2467 		handle = ERR_PTR(-EOPNOTSUPP);
2468 		goto unlock;
2469 	}
2470 
2471 	gen_dest = kzalloc_objs(*dest, num_dest + 1);
2472 	if (!gen_dest) {
2473 		handle = ERR_PTR(-ENOMEM);
2474 		goto unlock;
2475 	}
2476 	for (i = 0; i < num_dest; i++)
2477 		gen_dest[i] = dest[i];
2478 	gen_dest[i].type =
2479 		MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
2480 	gen_dest[i].ft = next_ft;
2481 	dest = gen_dest;
2482 	num_dest++;
2483 	flow_act->action &= ~(MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
2484 			      MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
2485 	flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2486 	handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
2487 	if (IS_ERR(handle))
2488 		goto unlock;
2489 
2490 	if (list_empty(&handle->rule[num_dest - 1]->next_ft)) {
2491 		mutex_lock(&next_ft->lock);
2492 		list_add(&handle->rule[num_dest - 1]->next_ft,
2493 			 &next_ft->fwd_rules);
2494 		mutex_unlock(&next_ft->lock);
2495 		handle->rule[num_dest - 1]->sw_action = sw_action;
2496 		handle->rule[num_dest - 1]->ft = ft;
2497 	}
2498 unlock:
2499 	mutex_unlock(&root->chain_lock);
2500 	kfree(gen_dest);
2501 	return handle;
2502 }
2503 EXPORT_SYMBOL(mlx5_add_flow_rules);
2504 
2505 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
2506 {
2507 	struct fs_fte *fte;
2508 	int i;
2509 
2510 	/* In order to consolidate the HW changes we lock the FTE for other
2511 	 * changes, and increase its refcount, in order not to perform the
2512 	 * "del" functions of the FTE. Will handle them here.
2513 	 * The removal of the rules is done under locked FTE.
2514 	 * After removing all the handle's rules, if there are remaining
2515 	 * rules, it means we just need to modify the FTE in FW, and
2516 	 * unlock/decrease the refcount we increased before.
2517 	 * Otherwise, it means the FTE should be deleted. First delete the
2518 	 * FTE in FW. Then, unlock the FTE, and proceed the tree_put_node of
2519 	 * the FTE, which will handle the last decrease of the refcount, as
2520 	 * well as required handling of its parent.
2521 	 */
2522 	fs_get_obj(fte, handle->rule[0]->node.parent);
2523 	down_write_ref_node(&fte->node, false);
2524 	for (i = handle->num_rules - 1; i >= 0; i--)
2525 		tree_remove_node(&handle->rule[i]->node, true);
2526 	if (list_empty(&fte->node.children)) {
2527 		fte->node.del_hw_func(&fte->node);
2528 		up_write_ref_node(&fte->node, false);
2529 		tree_put_node(&fte->node, false);
2530 	} else if (fte->act_dests.dests_size) {
2531 		if (fte->act_dests.modify_mask)
2532 			modify_fte(fte);
2533 		up_write_ref_node(&fte->node, false);
2534 	} else {
2535 		up_write_ref_node(&fte->node, false);
2536 	}
2537 	kfree(handle);
2538 }
2539 EXPORT_SYMBOL(mlx5_del_flow_rules);
2540 
2541 /* Assuming prio->node.children(flow tables) is sorted by level */
2542 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
2543 {
2544 	struct fs_node *prio_parent, *child;
2545 	struct fs_prio *prio;
2546 
2547 	fs_get_obj(prio, ft->node.parent);
2548 
2549 	if (!list_is_last(&ft->node.list, &prio->node.children))
2550 		return list_next_entry(ft, node.list);
2551 
2552 	prio_parent = find_prio_chains_parent(&prio->node, &child);
2553 
2554 	if (prio_parent && list_is_first(&child->list, &prio_parent->children))
2555 		return find_closest_ft(&prio->node, false, false);
2556 
2557 	return find_next_chained_ft(&prio->node);
2558 }
2559 
2560 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
2561 {
2562 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2563 	struct mlx5_ft_underlay_qp *uqp;
2564 	struct mlx5_flow_table *new_root_ft = NULL;
2565 	int err = 0;
2566 	u32 qpn;
2567 
2568 	if (root->root_ft != ft)
2569 		return 0;
2570 
2571 	new_root_ft = find_next_ft(ft);
2572 	if (!new_root_ft) {
2573 		root->root_ft = NULL;
2574 		return 0;
2575 	}
2576 
2577 	if (list_empty(&root->underlay_qpns)) {
2578 		/* Don't set any QPN (zero) in case QPN list is empty */
2579 		qpn = 0;
2580 		err = root->cmds->update_root_ft(root, new_root_ft,
2581 						 qpn, false);
2582 	} else {
2583 		list_for_each_entry(uqp, &root->underlay_qpns, list) {
2584 			qpn = uqp->qpn;
2585 			err = root->cmds->update_root_ft(root,
2586 							 new_root_ft, qpn,
2587 							 false);
2588 			if (err)
2589 				break;
2590 		}
2591 	}
2592 
2593 	if (err)
2594 		mlx5_core_warn(root->dev,
2595 			       "Update root flow table of id(%u) qpn(%d) failed\n",
2596 			       ft->id, qpn);
2597 	else
2598 		root->root_ft = new_root_ft;
2599 
2600 	return 0;
2601 }
2602 
2603 /* Connect flow table from previous priority to
2604  * the next flow table.
2605  */
2606 static int disconnect_flow_table(struct mlx5_flow_table *ft)
2607 {
2608 	struct mlx5_core_dev *dev = get_dev(&ft->node);
2609 	struct mlx5_flow_table *next_ft;
2610 	struct fs_prio *prio;
2611 	int err = 0;
2612 
2613 	err = update_root_ft_destroy(ft);
2614 	if (err)
2615 		return err;
2616 
2617 	fs_get_obj(prio, ft->node.parent);
2618 	if  (!(list_first_entry(&prio->node.children,
2619 				struct mlx5_flow_table,
2620 				node.list) == ft))
2621 		return 0;
2622 
2623 	next_ft = find_next_ft(ft);
2624 	err = connect_fwd_rules(dev, next_ft, ft);
2625 	if (err)
2626 		return err;
2627 
2628 	err = connect_prev_fts(dev, next_ft, prio);
2629 	if (err)
2630 		mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2631 			       ft->id);
2632 	return err;
2633 }
2634 
2635 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
2636 {
2637 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2638 	int err = 0;
2639 
2640 	mutex_lock(&root->chain_lock);
2641 	if (!(ft->flags & MLX5_FLOW_TABLE_UNMANAGED))
2642 		err = disconnect_flow_table(ft);
2643 	if (err) {
2644 		mutex_unlock(&root->chain_lock);
2645 		return err;
2646 	}
2647 	if (tree_remove_node(&ft->node, false))
2648 		mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2649 			       ft->id);
2650 	mutex_unlock(&root->chain_lock);
2651 
2652 	return err;
2653 }
2654 EXPORT_SYMBOL(mlx5_destroy_flow_table);
2655 
2656 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
2657 {
2658 	if (tree_remove_node(&fg->node, false))
2659 		mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2660 			       fg->id);
2661 }
2662 EXPORT_SYMBOL(mlx5_destroy_flow_group);
2663 
2664 struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
2665 						int n)
2666 {
2667 	struct mlx5_flow_steering *steering = dev->priv.steering;
2668 
2669 	if (!steering || !steering->fdb_sub_ns)
2670 		return NULL;
2671 
2672 	return steering->fdb_sub_ns[n];
2673 }
2674 EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
2675 
2676 static bool is_nic_rx_ns(enum mlx5_flow_namespace_type type)
2677 {
2678 	switch (type) {
2679 	case MLX5_FLOW_NAMESPACE_BYPASS:
2680 	case MLX5_FLOW_NAMESPACE_KERNEL_RX_MACSEC:
2681 	case MLX5_FLOW_NAMESPACE_LAG:
2682 	case MLX5_FLOW_NAMESPACE_OFFLOADS:
2683 	case MLX5_FLOW_NAMESPACE_ETHTOOL:
2684 	case MLX5_FLOW_NAMESPACE_KERNEL:
2685 	case MLX5_FLOW_NAMESPACE_LEFTOVERS:
2686 	case MLX5_FLOW_NAMESPACE_ANCHOR:
2687 		return true;
2688 	default:
2689 		return false;
2690 	}
2691 }
2692 
2693 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2694 						    enum mlx5_flow_namespace_type type)
2695 {
2696 	struct mlx5_flow_steering *steering = dev->priv.steering;
2697 	struct mlx5_flow_root_namespace *root_ns;
2698 	int prio = 0;
2699 	struct fs_prio *fs_prio;
2700 	struct mlx5_flow_namespace *ns;
2701 
2702 	if (!steering)
2703 		return NULL;
2704 
2705 	switch (type) {
2706 	case MLX5_FLOW_NAMESPACE_FDB:
2707 		if (steering->fdb_root_ns)
2708 			return &steering->fdb_root_ns->ns;
2709 		return NULL;
2710 	case MLX5_FLOW_NAMESPACE_PORT_SEL:
2711 		if (steering->port_sel_root_ns)
2712 			return &steering->port_sel_root_ns->ns;
2713 		return NULL;
2714 	case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2715 		if (steering->sniffer_rx_root_ns)
2716 			return &steering->sniffer_rx_root_ns->ns;
2717 		return NULL;
2718 	case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2719 		if (steering->sniffer_tx_root_ns)
2720 			return &steering->sniffer_tx_root_ns->ns;
2721 		return NULL;
2722 	case MLX5_FLOW_NAMESPACE_FDB_BYPASS:
2723 		root_ns = steering->fdb_root_ns;
2724 		prio =  FDB_BYPASS_PATH;
2725 		break;
2726 	case MLX5_FLOW_NAMESPACE_EGRESS:
2727 	case MLX5_FLOW_NAMESPACE_EGRESS_IPSEC:
2728 	case MLX5_FLOW_NAMESPACE_EGRESS_MACSEC:
2729 		root_ns = steering->egress_root_ns;
2730 		prio = type - MLX5_FLOW_NAMESPACE_EGRESS;
2731 		break;
2732 	case MLX5_FLOW_NAMESPACE_RDMA_RX:
2733 		root_ns = steering->rdma_rx_root_ns;
2734 		prio = RDMA_RX_BYPASS_PRIO;
2735 		break;
2736 	case MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL:
2737 		root_ns = steering->rdma_rx_root_ns;
2738 		prio = RDMA_RX_KERNEL_PRIO;
2739 		break;
2740 	case MLX5_FLOW_NAMESPACE_RDMA_TX:
2741 		root_ns = steering->rdma_tx_root_ns;
2742 		prio = RDMA_TX_BYPASS_PRIO;
2743 		break;
2744 	case MLX5_FLOW_NAMESPACE_RDMA_RX_COUNTERS:
2745 		root_ns = steering->rdma_rx_root_ns;
2746 		prio = RDMA_RX_COUNTERS_PRIO;
2747 		break;
2748 	case MLX5_FLOW_NAMESPACE_RDMA_TX_COUNTERS:
2749 		root_ns = steering->rdma_tx_root_ns;
2750 		prio = RDMA_TX_COUNTERS_PRIO;
2751 		break;
2752 	case MLX5_FLOW_NAMESPACE_RDMA_RX_IPSEC:
2753 		root_ns = steering->rdma_rx_root_ns;
2754 		prio = RDMA_RX_IPSEC_PRIO;
2755 		break;
2756 	case MLX5_FLOW_NAMESPACE_RDMA_TX_IPSEC:
2757 		root_ns = steering->rdma_tx_root_ns;
2758 		prio = RDMA_TX_IPSEC_PRIO;
2759 		break;
2760 	case MLX5_FLOW_NAMESPACE_RDMA_RX_MACSEC:
2761 		root_ns = steering->rdma_rx_root_ns;
2762 		prio = RDMA_RX_MACSEC_PRIO;
2763 		break;
2764 	case MLX5_FLOW_NAMESPACE_RDMA_TX_MACSEC:
2765 		root_ns = steering->rdma_tx_root_ns;
2766 		prio = RDMA_TX_MACSEC_PRIO;
2767 		break;
2768 	default: /* Must be NIC RX */
2769 		WARN_ON(!is_nic_rx_ns(type));
2770 		root_ns = steering->root_ns;
2771 		prio = type;
2772 		break;
2773 	}
2774 
2775 	if (!root_ns)
2776 		return NULL;
2777 
2778 	fs_prio = find_prio(&root_ns->ns, prio);
2779 	if (!fs_prio)
2780 		return NULL;
2781 
2782 	ns = list_first_entry(&fs_prio->node.children,
2783 			      typeof(*ns),
2784 			      node.list);
2785 
2786 	return ns;
2787 }
2788 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2789 
2790 struct mlx5_vport_acl_root_ns {
2791 	u16 vport_idx;
2792 	struct mlx5_flow_root_namespace *root_ns;
2793 };
2794 
2795 struct mlx5_flow_namespace *
2796 mlx5_get_flow_vport_namespace(struct mlx5_core_dev *dev,
2797 			      enum mlx5_flow_namespace_type type, int vport_idx)
2798 {
2799 	struct mlx5_flow_steering *steering = dev->priv.steering;
2800 	struct mlx5_vport_acl_root_ns *vport_ns;
2801 
2802 	if (!steering)
2803 		return NULL;
2804 
2805 	switch (type) {
2806 	case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2807 		vport_ns = xa_load(&steering->esw_egress_root_ns, vport_idx);
2808 		if (vport_ns)
2809 			return &vport_ns->root_ns->ns;
2810 		else
2811 			return NULL;
2812 	case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2813 		vport_ns = xa_load(&steering->esw_ingress_root_ns, vport_idx);
2814 		if (vport_ns)
2815 			return &vport_ns->root_ns->ns;
2816 		else
2817 			return NULL;
2818 	case MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_RX:
2819 		if (vport_idx >= steering->rdma_transport_rx_vports)
2820 			return NULL;
2821 		if (steering->rdma_transport_rx_root_ns &&
2822 		    steering->rdma_transport_rx_root_ns[vport_idx])
2823 			return &steering->rdma_transport_rx_root_ns[vport_idx]->ns;
2824 		else
2825 			return NULL;
2826 	case MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_TX:
2827 		if (vport_idx >= steering->rdma_transport_tx_vports)
2828 			return NULL;
2829 
2830 		if (steering->rdma_transport_tx_root_ns &&
2831 		    steering->rdma_transport_tx_root_ns[vport_idx])
2832 			return &steering->rdma_transport_tx_root_ns[vport_idx]->ns;
2833 		else
2834 			return NULL;
2835 	default:
2836 		return NULL;
2837 	}
2838 }
2839 EXPORT_SYMBOL(mlx5_get_flow_vport_namespace);
2840 
2841 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2842 				       unsigned int prio,
2843 				       int num_levels,
2844 				       enum fs_node_type type)
2845 {
2846 	struct fs_prio *fs_prio;
2847 
2848 	fs_prio = kzalloc_obj(*fs_prio);
2849 	if (!fs_prio)
2850 		return ERR_PTR(-ENOMEM);
2851 
2852 	fs_prio->node.type = type;
2853 	tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2854 	tree_add_node(&fs_prio->node, &ns->node);
2855 	fs_prio->num_levels = num_levels;
2856 	fs_prio->prio = prio;
2857 	list_add_tail(&fs_prio->node.list, &ns->node.children);
2858 
2859 	return fs_prio;
2860 }
2861 
2862 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2863 					      unsigned int prio,
2864 					      int num_levels)
2865 {
2866 	return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2867 }
2868 
2869 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2870 				      unsigned int prio, int num_levels)
2871 {
2872 	return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2873 }
2874 
2875 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2876 						     *ns)
2877 {
2878 	ns->node.type = FS_TYPE_NAMESPACE;
2879 
2880 	return ns;
2881 }
2882 
2883 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2884 						       int def_miss_act)
2885 {
2886 	struct mlx5_flow_namespace	*ns;
2887 
2888 	ns = kzalloc_obj(*ns);
2889 	if (!ns)
2890 		return ERR_PTR(-ENOMEM);
2891 
2892 	fs_init_namespace(ns);
2893 	ns->def_miss_action = def_miss_act;
2894 	tree_init_node(&ns->node, NULL, del_sw_ns);
2895 	tree_add_node(&ns->node, &prio->node);
2896 	list_add_tail(&ns->node.list, &prio->node.children);
2897 
2898 	return ns;
2899 }
2900 
2901 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2902 			     struct init_tree_node *prio_metadata)
2903 {
2904 	struct fs_prio *fs_prio;
2905 	int i;
2906 
2907 	for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2908 		fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2909 		if (IS_ERR(fs_prio))
2910 			return PTR_ERR(fs_prio);
2911 	}
2912 	return 0;
2913 }
2914 
2915 #define FLOW_TABLE_BIT_SZ 1
2916 #define GET_FLOW_TABLE_CAP(dev, offset) \
2917 	((be32_to_cpu(*((__be32 *)(dev->caps.hca[MLX5_CAP_FLOW_TABLE]->cur) +	\
2918 			offset / 32)) >>					\
2919 	  (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2920 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2921 {
2922 	int i;
2923 
2924 	for (i = 0; i < caps->arr_sz; i++) {
2925 		if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2926 			return false;
2927 	}
2928 	return true;
2929 }
2930 
2931 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2932 				    struct init_tree_node *init_node,
2933 				    struct fs_node *fs_parent_node,
2934 				    struct init_tree_node *init_parent_node,
2935 				    int prio)
2936 {
2937 	int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2938 					      flow_table_properties_nic_receive.
2939 					      max_ft_level);
2940 	struct mlx5_flow_namespace *fs_ns;
2941 	struct fs_prio *fs_prio;
2942 	struct fs_node *base;
2943 	int i;
2944 	int err;
2945 
2946 	if (init_node->type == FS_TYPE_PRIO) {
2947 		if ((init_node->min_ft_level > max_ft_level) ||
2948 		    !has_required_caps(steering->dev, &init_node->caps))
2949 			return 0;
2950 
2951 		fs_get_obj(fs_ns, fs_parent_node);
2952 		if (init_node->num_leaf_prios)
2953 			return create_leaf_prios(fs_ns, prio, init_node);
2954 		fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2955 		if (IS_ERR(fs_prio))
2956 			return PTR_ERR(fs_prio);
2957 		base = &fs_prio->node;
2958 	} else if (init_node->type == FS_TYPE_NAMESPACE) {
2959 		fs_get_obj(fs_prio, fs_parent_node);
2960 		fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
2961 		if (IS_ERR(fs_ns))
2962 			return PTR_ERR(fs_ns);
2963 		base = &fs_ns->node;
2964 	} else {
2965 		return -EINVAL;
2966 	}
2967 	prio = 0;
2968 	for (i = 0; i < init_node->ar_size; i++) {
2969 		err = init_root_tree_recursive(steering, &init_node->children[i],
2970 					       base, init_node, prio);
2971 		if (err)
2972 			return err;
2973 		if (init_node->children[i].type == FS_TYPE_PRIO &&
2974 		    init_node->children[i].num_leaf_prios) {
2975 			prio += init_node->children[i].num_leaf_prios;
2976 		}
2977 	}
2978 
2979 	return 0;
2980 }
2981 
2982 static int init_root_tree(struct mlx5_flow_steering *steering,
2983 			  struct init_tree_node *init_node,
2984 			  struct fs_node *fs_parent_node)
2985 {
2986 	int err;
2987 	int i;
2988 
2989 	for (i = 0; i < init_node->ar_size; i++) {
2990 		err = init_root_tree_recursive(steering, &init_node->children[i],
2991 					       fs_parent_node,
2992 					       init_node, i);
2993 		if (err)
2994 			return err;
2995 	}
2996 	return 0;
2997 }
2998 
2999 static void del_sw_root_ns(struct fs_node *node)
3000 {
3001 	struct mlx5_flow_root_namespace *root_ns;
3002 	struct mlx5_flow_namespace *ns;
3003 
3004 	fs_get_obj(ns, node);
3005 	root_ns = container_of(ns, struct mlx5_flow_root_namespace, ns);
3006 	mutex_destroy(&root_ns->chain_lock);
3007 	kfree(node);
3008 }
3009 
3010 static struct mlx5_flow_root_namespace
3011 *create_root_ns(struct mlx5_flow_steering *steering,
3012 		enum fs_flow_table_type table_type)
3013 {
3014 	const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
3015 	struct mlx5_flow_root_namespace *root_ns;
3016 	struct mlx5_flow_namespace *ns;
3017 
3018 	/* Create the root namespace */
3019 	root_ns = kzalloc_obj(*root_ns);
3020 	if (!root_ns)
3021 		return NULL;
3022 
3023 	root_ns->dev = steering->dev;
3024 	root_ns->table_type = table_type;
3025 	root_ns->cmds = cmds;
3026 
3027 	INIT_LIST_HEAD(&root_ns->underlay_qpns);
3028 
3029 	ns = &root_ns->ns;
3030 	fs_init_namespace(ns);
3031 	mutex_init(&root_ns->chain_lock);
3032 	tree_init_node(&ns->node, NULL, del_sw_root_ns);
3033 	tree_add_node(&ns->node, NULL);
3034 
3035 	return root_ns;
3036 }
3037 
3038 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
3039 
3040 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
3041 {
3042 	struct fs_prio *prio;
3043 
3044 	fs_for_each_prio(prio, ns) {
3045 		 /* This updates prio start_level and num_levels */
3046 		set_prio_attrs_in_prio(prio, acc_level);
3047 		acc_level += prio->num_levels;
3048 	}
3049 	return acc_level;
3050 }
3051 
3052 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
3053 {
3054 	struct mlx5_flow_namespace *ns;
3055 	int acc_level_ns = acc_level;
3056 
3057 	prio->start_level = acc_level;
3058 	fs_for_each_ns(ns, prio) {
3059 		/* This updates start_level and num_levels of ns's priority descendants */
3060 		acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
3061 
3062 		/* If this a prio with chains, and we can jump from one chain
3063 		 * (namespace) to another, so we accumulate the levels
3064 		 */
3065 		if (prio->node.type == FS_TYPE_PRIO_CHAINS)
3066 			acc_level = acc_level_ns;
3067 	}
3068 
3069 	if (!prio->num_levels)
3070 		prio->num_levels = acc_level_ns - prio->start_level;
3071 	WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
3072 }
3073 
3074 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
3075 {
3076 	struct mlx5_flow_namespace *ns = &root_ns->ns;
3077 	struct fs_prio *prio;
3078 	int start_level = 0;
3079 
3080 	fs_for_each_prio(prio, ns) {
3081 		set_prio_attrs_in_prio(prio, start_level);
3082 		start_level += prio->num_levels;
3083 	}
3084 }
3085 
3086 #define ANCHOR_PRIO 0
3087 #define ANCHOR_SIZE 1
3088 #define ANCHOR_LEVEL 0
3089 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
3090 {
3091 	struct mlx5_flow_namespace *ns = NULL;
3092 	struct mlx5_flow_table_attr ft_attr = {};
3093 	struct mlx5_flow_table *ft;
3094 
3095 	ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
3096 	if (WARN_ON(!ns))
3097 		return -EINVAL;
3098 
3099 	ft_attr.max_fte = ANCHOR_SIZE;
3100 	ft_attr.level   = ANCHOR_LEVEL;
3101 	ft_attr.prio    = ANCHOR_PRIO;
3102 
3103 	ft = mlx5_create_flow_table(ns, &ft_attr);
3104 	if (IS_ERR(ft)) {
3105 		mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
3106 		return PTR_ERR(ft);
3107 	}
3108 	return 0;
3109 }
3110 
3111 static int init_root_ns(struct mlx5_flow_steering *steering)
3112 {
3113 	int err;
3114 
3115 	steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
3116 	if (!steering->root_ns)
3117 		return -ENOMEM;
3118 
3119 	err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
3120 	if (err)
3121 		goto out_err;
3122 
3123 	set_prio_attrs(steering->root_ns);
3124 	err = create_anchor_flow_table(steering);
3125 	if (err)
3126 		goto out_err;
3127 
3128 	return 0;
3129 
3130 out_err:
3131 	cleanup_root_ns(steering->root_ns);
3132 	steering->root_ns = NULL;
3133 	return err;
3134 }
3135 
3136 static void clean_tree(struct fs_node *node)
3137 {
3138 	if (node) {
3139 		struct fs_node *iter;
3140 		struct fs_node *temp;
3141 
3142 		tree_get_node(node);
3143 		list_for_each_entry_safe(iter, temp, &node->children, list)
3144 			clean_tree(iter);
3145 		tree_put_node(node, false);
3146 		tree_remove_node(node, false);
3147 	}
3148 }
3149 
3150 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
3151 {
3152 	if (!root_ns)
3153 		return;
3154 
3155 	clean_tree(&root_ns->ns.node);
3156 }
3157 
3158 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
3159 {
3160 	struct fs_prio *prio;
3161 
3162 	steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
3163 	if (!steering->sniffer_tx_root_ns)
3164 		return -ENOMEM;
3165 
3166 	/* Create single prio */
3167 	prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
3168 	return PTR_ERR_OR_ZERO(prio);
3169 }
3170 
3171 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
3172 {
3173 	struct fs_prio *prio;
3174 
3175 	steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
3176 	if (!steering->sniffer_rx_root_ns)
3177 		return -ENOMEM;
3178 
3179 	/* Create single prio */
3180 	prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
3181 	return PTR_ERR_OR_ZERO(prio);
3182 }
3183 
3184 #define PORT_SEL_NUM_LEVELS 3
3185 static int init_port_sel_root_ns(struct mlx5_flow_steering *steering)
3186 {
3187 	struct fs_prio *prio;
3188 
3189 	steering->port_sel_root_ns = create_root_ns(steering, FS_FT_PORT_SEL);
3190 	if (!steering->port_sel_root_ns)
3191 		return -ENOMEM;
3192 
3193 	/* Create single prio */
3194 	prio = fs_create_prio(&steering->port_sel_root_ns->ns, 0,
3195 			      PORT_SEL_NUM_LEVELS);
3196 	return PTR_ERR_OR_ZERO(prio);
3197 }
3198 
3199 static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
3200 {
3201 	int err;
3202 
3203 	steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
3204 	if (!steering->rdma_rx_root_ns)
3205 		return -ENOMEM;
3206 
3207 	err = init_root_tree(steering, &rdma_rx_root_fs,
3208 			     &steering->rdma_rx_root_ns->ns.node);
3209 	if (err)
3210 		goto out_err;
3211 
3212 	set_prio_attrs(steering->rdma_rx_root_ns);
3213 
3214 	return 0;
3215 
3216 out_err:
3217 	cleanup_root_ns(steering->rdma_rx_root_ns);
3218 	steering->rdma_rx_root_ns = NULL;
3219 	return err;
3220 }
3221 
3222 static int init_rdma_tx_root_ns(struct mlx5_flow_steering *steering)
3223 {
3224 	int err;
3225 
3226 	steering->rdma_tx_root_ns = create_root_ns(steering, FS_FT_RDMA_TX);
3227 	if (!steering->rdma_tx_root_ns)
3228 		return -ENOMEM;
3229 
3230 	err = init_root_tree(steering, &rdma_tx_root_fs,
3231 			     &steering->rdma_tx_root_ns->ns.node);
3232 	if (err)
3233 		goto out_err;
3234 
3235 	set_prio_attrs(steering->rdma_tx_root_ns);
3236 
3237 	return 0;
3238 
3239 out_err:
3240 	cleanup_root_ns(steering->rdma_tx_root_ns);
3241 	steering->rdma_tx_root_ns = NULL;
3242 	return err;
3243 }
3244 
3245 static int
3246 init_rdma_transport_rx_root_ns_one(struct mlx5_flow_steering *steering,
3247 				   int vport_idx)
3248 {
3249 	struct mlx5_flow_root_namespace *root_ns;
3250 	struct fs_prio *prio;
3251 	int ret;
3252 	int i;
3253 
3254 	steering->rdma_transport_rx_root_ns[vport_idx] =
3255 		create_root_ns(steering, FS_FT_RDMA_TRANSPORT_RX);
3256 	if (!steering->rdma_transport_rx_root_ns[vport_idx])
3257 		return -ENOMEM;
3258 
3259 	root_ns = steering->rdma_transport_rx_root_ns[vport_idx];
3260 
3261 	for (i = 0; i < MLX5_RDMA_TRANSPORT_BYPASS_PRIO; i++) {
3262 		prio = fs_create_prio(&root_ns->ns, i, 1);
3263 		if (IS_ERR(prio)) {
3264 			ret = PTR_ERR(prio);
3265 			goto err;
3266 		}
3267 	}
3268 	set_prio_attrs(root_ns);
3269 	return 0;
3270 
3271 err:
3272 	cleanup_root_ns(root_ns);
3273 	return ret;
3274 }
3275 
3276 static int
3277 init_rdma_transport_tx_root_ns_one(struct mlx5_flow_steering *steering,
3278 				   int vport_idx)
3279 {
3280 	struct mlx5_flow_root_namespace *root_ns;
3281 	struct fs_prio *prio;
3282 	int ret;
3283 	int i;
3284 
3285 	steering->rdma_transport_tx_root_ns[vport_idx] =
3286 		create_root_ns(steering, FS_FT_RDMA_TRANSPORT_TX);
3287 	if (!steering->rdma_transport_tx_root_ns[vport_idx])
3288 		return -ENOMEM;
3289 
3290 	root_ns = steering->rdma_transport_tx_root_ns[vport_idx];
3291 
3292 	for (i = 0; i < MLX5_RDMA_TRANSPORT_BYPASS_PRIO; i++) {
3293 		prio = fs_create_prio(&root_ns->ns, i, 1);
3294 		if (IS_ERR(prio)) {
3295 			ret = PTR_ERR(prio);
3296 			goto err;
3297 		}
3298 	}
3299 	set_prio_attrs(root_ns);
3300 	return 0;
3301 
3302 err:
3303 	cleanup_root_ns(root_ns);
3304 	return ret;
3305 }
3306 
3307 static bool mlx5_fs_ns_is_empty(struct mlx5_flow_namespace *ns)
3308 {
3309 	struct fs_prio *iter_prio;
3310 
3311 	fs_for_each_prio(iter_prio, ns) {
3312 		if (iter_prio->num_ft)
3313 			return false;
3314 	}
3315 
3316 	return true;
3317 }
3318 
3319 int mlx5_fs_set_root_dev(struct mlx5_core_dev *dev,
3320 			 struct mlx5_core_dev *new_dev,
3321 			 enum fs_flow_table_type table_type)
3322 {
3323 	struct mlx5_flow_root_namespace	**root;
3324 	int total_vports;
3325 	int i;
3326 
3327 	switch (table_type) {
3328 	case FS_FT_RDMA_TRANSPORT_TX:
3329 		root = dev->priv.steering->rdma_transport_tx_root_ns;
3330 		total_vports = dev->priv.steering->rdma_transport_tx_vports;
3331 		break;
3332 	case FS_FT_RDMA_TRANSPORT_RX:
3333 		root = dev->priv.steering->rdma_transport_rx_root_ns;
3334 		total_vports = dev->priv.steering->rdma_transport_rx_vports;
3335 		break;
3336 	default:
3337 		WARN_ON_ONCE(true);
3338 		return -EINVAL;
3339 	}
3340 
3341 	for (i = 0; i < total_vports; i++) {
3342 		mutex_lock(&root[i]->chain_lock);
3343 		if (!mlx5_fs_ns_is_empty(&root[i]->ns)) {
3344 			mutex_unlock(&root[i]->chain_lock);
3345 			goto err;
3346 		}
3347 		root[i]->dev = new_dev;
3348 		mutex_unlock(&root[i]->chain_lock);
3349 	}
3350 	return 0;
3351 err:
3352 	while (i--) {
3353 		mutex_lock(&root[i]->chain_lock);
3354 		root[i]->dev = dev;
3355 		mutex_unlock(&root[i]->chain_lock);
3356 	}
3357 	/* If you hit this error try destroying all flow tables and try again */
3358 	mlx5_core_err(dev, "Failed to set root device for RDMA TRANSPORT\n");
3359 	return -EINVAL;
3360 }
3361 EXPORT_SYMBOL(mlx5_fs_set_root_dev);
3362 
3363 static int init_rdma_transport_rx_root_ns(struct mlx5_flow_steering *steering)
3364 {
3365 	struct mlx5_core_dev *dev = steering->dev;
3366 	int total_vports;
3367 	int err;
3368 	int i;
3369 
3370 	/* In case eswitch not supported and working in legacy mode */
3371 	total_vports = mlx5_eswitch_get_total_vports(dev) ?: 1;
3372 
3373 	steering->rdma_transport_rx_root_ns =
3374 			kzalloc_objs(*steering->rdma_transport_rx_root_ns,
3375 				     total_vports);
3376 	if (!steering->rdma_transport_rx_root_ns)
3377 		return -ENOMEM;
3378 
3379 	for (i = 0; i < total_vports; i++) {
3380 		err = init_rdma_transport_rx_root_ns_one(steering, i);
3381 		if (err)
3382 			goto cleanup_root_ns;
3383 	}
3384 	steering->rdma_transport_rx_vports = total_vports;
3385 	return 0;
3386 
3387 cleanup_root_ns:
3388 	while (i--)
3389 		cleanup_root_ns(steering->rdma_transport_rx_root_ns[i]);
3390 	kfree(steering->rdma_transport_rx_root_ns);
3391 	steering->rdma_transport_rx_root_ns = NULL;
3392 	return err;
3393 }
3394 
3395 static int init_rdma_transport_tx_root_ns(struct mlx5_flow_steering *steering)
3396 {
3397 	struct mlx5_core_dev *dev = steering->dev;
3398 	int total_vports;
3399 	int err;
3400 	int i;
3401 
3402 	/* In case eswitch not supported and working in legacy mode */
3403 	total_vports = mlx5_eswitch_get_total_vports(dev) ?: 1;
3404 
3405 	steering->rdma_transport_tx_root_ns =
3406 			kzalloc_objs(*steering->rdma_transport_tx_root_ns,
3407 				     total_vports);
3408 	if (!steering->rdma_transport_tx_root_ns)
3409 		return -ENOMEM;
3410 
3411 	for (i = 0; i < total_vports; i++) {
3412 		err = init_rdma_transport_tx_root_ns_one(steering, i);
3413 		if (err)
3414 			goto cleanup_root_ns;
3415 	}
3416 	steering->rdma_transport_tx_vports = total_vports;
3417 	return 0;
3418 
3419 cleanup_root_ns:
3420 	while (i--)
3421 		cleanup_root_ns(steering->rdma_transport_tx_root_ns[i]);
3422 	kfree(steering->rdma_transport_tx_root_ns);
3423 	steering->rdma_transport_tx_root_ns = NULL;
3424 	return err;
3425 }
3426 
3427 static void cleanup_rdma_transport_roots_ns(struct mlx5_flow_steering *steering)
3428 {
3429 	int i;
3430 
3431 	if (steering->rdma_transport_rx_root_ns) {
3432 		for (i = 0; i < steering->rdma_transport_rx_vports; i++)
3433 			cleanup_root_ns(steering->rdma_transport_rx_root_ns[i]);
3434 
3435 		kfree(steering->rdma_transport_rx_root_ns);
3436 		steering->rdma_transport_rx_root_ns = NULL;
3437 	}
3438 
3439 	if (steering->rdma_transport_tx_root_ns) {
3440 		for (i = 0; i < steering->rdma_transport_tx_vports; i++)
3441 			cleanup_root_ns(steering->rdma_transport_tx_root_ns[i]);
3442 
3443 		kfree(steering->rdma_transport_tx_root_ns);
3444 		steering->rdma_transport_tx_root_ns = NULL;
3445 	}
3446 }
3447 
3448 /* FT and tc chains are stored in the same array so we can re-use the
3449  * mlx5_get_fdb_sub_ns() and tc api for FT chains.
3450  * When creating a new ns for each chain store it in the first available slot.
3451  * Assume tc chains are created and stored first and only then the FT chain.
3452  */
3453 static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
3454 					struct mlx5_flow_namespace *ns)
3455 {
3456 	int chain = 0;
3457 
3458 	while (steering->fdb_sub_ns[chain])
3459 		++chain;
3460 
3461 	steering->fdb_sub_ns[chain] = ns;
3462 }
3463 
3464 static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
3465 					struct fs_prio *maj_prio)
3466 {
3467 	struct mlx5_flow_namespace *ns;
3468 	struct fs_prio *min_prio;
3469 	int prio;
3470 
3471 	ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
3472 	if (IS_ERR(ns))
3473 		return PTR_ERR(ns);
3474 
3475 	for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
3476 		min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
3477 		if (IS_ERR(min_prio))
3478 			return PTR_ERR(min_prio);
3479 	}
3480 
3481 	store_fdb_sub_ns_prio_chain(steering, ns);
3482 
3483 	return 0;
3484 }
3485 
3486 static int create_fdb_chains(struct mlx5_flow_steering *steering,
3487 			     int fs_prio,
3488 			     int chains)
3489 {
3490 	struct fs_prio *maj_prio;
3491 	int levels;
3492 	int chain;
3493 	int err;
3494 
3495 	levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
3496 	maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
3497 					  fs_prio,
3498 					  levels);
3499 	if (IS_ERR(maj_prio))
3500 		return PTR_ERR(maj_prio);
3501 
3502 	for (chain = 0; chain < chains; chain++) {
3503 		err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
3504 		if (err)
3505 			return err;
3506 	}
3507 
3508 	return 0;
3509 }
3510 
3511 static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
3512 {
3513 	int err;
3514 
3515 	steering->fdb_sub_ns = kzalloc_objs(*steering->fdb_sub_ns,
3516 					    FDB_NUM_CHAINS);
3517 	if (!steering->fdb_sub_ns)
3518 		return -ENOMEM;
3519 
3520 	err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
3521 	if (err)
3522 		return err;
3523 
3524 	err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
3525 	if (err)
3526 		return err;
3527 
3528 	return 0;
3529 }
3530 
3531 static int create_fdb_bypass(struct mlx5_flow_steering *steering)
3532 {
3533 	struct mlx5_flow_namespace *ns;
3534 	struct fs_prio *prio;
3535 	int i;
3536 
3537 	prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH, 0);
3538 	if (IS_ERR(prio))
3539 		return PTR_ERR(prio);
3540 
3541 	ns = fs_create_namespace(prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
3542 	if (IS_ERR(ns))
3543 		return PTR_ERR(ns);
3544 
3545 	for (i = 0; i < MLX5_BY_PASS_NUM_REGULAR_PRIOS; i++) {
3546 		prio = fs_create_prio(ns, i, 1);
3547 		if (IS_ERR(prio))
3548 			return PTR_ERR(prio);
3549 	}
3550 	return 0;
3551 }
3552 
3553 static void cleanup_fdb_root_ns(struct mlx5_flow_steering *steering)
3554 {
3555 	cleanup_root_ns(steering->fdb_root_ns);
3556 	steering->fdb_root_ns = NULL;
3557 	kfree(steering->fdb_sub_ns);
3558 	steering->fdb_sub_ns = NULL;
3559 }
3560 
3561 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
3562 {
3563 	struct fs_prio *maj_prio;
3564 	int err;
3565 
3566 	steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
3567 	if (!steering->fdb_root_ns)
3568 		return -ENOMEM;
3569 
3570 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_DROP_ROOT, 1);
3571 	err = PTR_ERR_OR_ZERO(maj_prio);
3572 	if (err)
3573 		goto out_err;
3574 
3575 	err = create_fdb_bypass(steering);
3576 	if (err)
3577 		goto out_err;
3578 
3579 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_INGRESS, 3);
3580 	if (IS_ERR(maj_prio)) {
3581 		err = PTR_ERR(maj_prio);
3582 		goto out_err;
3583 	}
3584 
3585 	err = create_fdb_fast_path(steering);
3586 	if (err)
3587 		goto out_err;
3588 
3589 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_TC_MISS, 1);
3590 	if (IS_ERR(maj_prio)) {
3591 		err = PTR_ERR(maj_prio);
3592 		goto out_err;
3593 	}
3594 
3595 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BR_OFFLOAD, 4);
3596 	if (IS_ERR(maj_prio)) {
3597 		err = PTR_ERR(maj_prio);
3598 		goto out_err;
3599 	}
3600 
3601 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
3602 	if (IS_ERR(maj_prio)) {
3603 		err = PTR_ERR(maj_prio);
3604 		goto out_err;
3605 	}
3606 
3607 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_EGRESS, 3);
3608 	if (IS_ERR(maj_prio)) {
3609 		err = PTR_ERR(maj_prio);
3610 		goto out_err;
3611 	}
3612 
3613 	/* We put this priority last, knowing that nothing will get here
3614 	 * unless explicitly forwarded to. This is possible because the
3615 	 * slow path tables have catch all rules and nothing gets passed
3616 	 * those tables.
3617 	 */
3618 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_PER_VPORT, 1);
3619 	if (IS_ERR(maj_prio)) {
3620 		err = PTR_ERR(maj_prio);
3621 		goto out_err;
3622 	}
3623 
3624 	set_prio_attrs(steering->fdb_root_ns);
3625 	return 0;
3626 
3627 out_err:
3628 	cleanup_fdb_root_ns(steering);
3629 	return err;
3630 }
3631 
3632 static void
3633 mlx5_fs_remove_vport_acl_root_ns(struct xarray *esw_acl_root_ns, u16 vport_idx)
3634 {
3635 	struct mlx5_vport_acl_root_ns *vport_ns;
3636 
3637 	vport_ns = xa_erase(esw_acl_root_ns, vport_idx);
3638 	if (vport_ns) {
3639 		cleanup_root_ns(vport_ns->root_ns);
3640 		kfree(vport_ns);
3641 	}
3642 }
3643 
3644 static int
3645 mlx5_fs_add_vport_acl_root_ns(struct mlx5_flow_steering *steering,
3646 			      struct xarray *esw_acl_root_ns,
3647 			      enum fs_flow_table_type table_type,
3648 			      u16 vport_idx)
3649 {
3650 	struct mlx5_vport_acl_root_ns *vport_ns;
3651 	struct fs_prio *prio;
3652 	int err;
3653 
3654 	/* sanity check, intended xarrays are used */
3655 	if (WARN_ON(esw_acl_root_ns != &steering->esw_egress_root_ns &&
3656 		    esw_acl_root_ns != &steering->esw_ingress_root_ns))
3657 		return -EINVAL;
3658 
3659 	if (table_type != FS_FT_ESW_EGRESS_ACL &&
3660 	    table_type != FS_FT_ESW_INGRESS_ACL) {
3661 		mlx5_core_err(steering->dev,
3662 			      "Invalid table type %d for egress/ingress ACLs\n",
3663 			      table_type);
3664 		return -EINVAL;
3665 	}
3666 
3667 	if (xa_load(esw_acl_root_ns, vport_idx))
3668 		return -EEXIST;
3669 
3670 	vport_ns = kzalloc_obj(*vport_ns);
3671 	if (!vport_ns)
3672 		return -ENOMEM;
3673 
3674 	vport_ns->root_ns = create_root_ns(steering, table_type);
3675 	if (!vport_ns->root_ns) {
3676 		err = -ENOMEM;
3677 		goto kfree_vport_ns;
3678 	}
3679 
3680 	/* create 1 prio*/
3681 	prio = fs_create_prio(&vport_ns->root_ns->ns, 0, 1);
3682 	if (IS_ERR(prio)) {
3683 		err = PTR_ERR(prio);
3684 		goto cleanup_root_ns;
3685 	}
3686 
3687 	vport_ns->vport_idx = vport_idx;
3688 	err = xa_insert(esw_acl_root_ns, vport_idx, vport_ns, GFP_KERNEL);
3689 	if (err)
3690 		goto cleanup_root_ns;
3691 	return 0;
3692 
3693 cleanup_root_ns:
3694 	cleanup_root_ns(vport_ns->root_ns);
3695 kfree_vport_ns:
3696 	kfree(vport_ns);
3697 	return err;
3698 }
3699 
3700 int mlx5_fs_vport_egress_acl_ns_add(struct mlx5_flow_steering *steering,
3701 				    u16 vport_idx)
3702 {
3703 	return mlx5_fs_add_vport_acl_root_ns(steering,
3704 					     &steering->esw_egress_root_ns,
3705 					     FS_FT_ESW_EGRESS_ACL, vport_idx);
3706 }
3707 
3708 int mlx5_fs_vport_ingress_acl_ns_add(struct mlx5_flow_steering *steering,
3709 				     u16 vport_idx)
3710 {
3711 	return mlx5_fs_add_vport_acl_root_ns(steering,
3712 					     &steering->esw_ingress_root_ns,
3713 					     FS_FT_ESW_INGRESS_ACL, vport_idx);
3714 }
3715 
3716 void mlx5_fs_vport_egress_acl_ns_remove(struct mlx5_flow_steering *steering,
3717 					int vport_idx)
3718 {
3719 	mlx5_fs_remove_vport_acl_root_ns(&steering->esw_egress_root_ns,
3720 					 vport_idx);
3721 }
3722 
3723 void mlx5_fs_vport_ingress_acl_ns_remove(struct mlx5_flow_steering *steering,
3724 					 int vport_idx)
3725 {
3726 	mlx5_fs_remove_vport_acl_root_ns(&steering->esw_ingress_root_ns,
3727 					 vport_idx);
3728 }
3729 
3730 u32 mlx5_fs_get_capabilities(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type type)
3731 {
3732 	struct mlx5_flow_root_namespace *root;
3733 	struct mlx5_flow_namespace *ns;
3734 
3735 	ns = mlx5_get_flow_namespace(dev, type);
3736 	if (!ns)
3737 		return 0;
3738 
3739 	root = find_root(&ns->node);
3740 	if (!root)
3741 		return 0;
3742 
3743 	return root->cmds->get_capabilities(root, root->table_type);
3744 }
3745 
3746 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
3747 {
3748 	int err;
3749 
3750 	steering->egress_root_ns = create_root_ns(steering,
3751 						  FS_FT_NIC_TX);
3752 	if (!steering->egress_root_ns)
3753 		return -ENOMEM;
3754 
3755 	err = init_root_tree(steering, &egress_root_fs,
3756 			     &steering->egress_root_ns->ns.node);
3757 	if (err)
3758 		goto cleanup;
3759 	set_prio_attrs(steering->egress_root_ns);
3760 	return 0;
3761 cleanup:
3762 	cleanup_root_ns(steering->egress_root_ns);
3763 	steering->egress_root_ns = NULL;
3764 	return err;
3765 }
3766 
3767 static int mlx5_fs_mode_validate(struct devlink *devlink, u32 id,
3768 				 union devlink_param_value val,
3769 				 struct netlink_ext_ack *extack)
3770 {
3771 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3772 	char *value = val.vstr;
3773 	u8 eswitch_mode;
3774 
3775 	eswitch_mode = mlx5_eswitch_mode(dev);
3776 	if (eswitch_mode == MLX5_ESWITCH_OFFLOADS) {
3777 		NL_SET_ERR_MSG_FMT_MOD(extack,
3778 				       "Changing fs mode is not supported when eswitch offloads enabled.");
3779 		return -EOPNOTSUPP;
3780 	}
3781 
3782 	if (!strcmp(value, "dmfs"))
3783 		return 0;
3784 
3785 	if (!strcmp(value, "smfs")) {
3786 		bool smfs_cap = mlx5_fs_dr_is_supported(dev);
3787 
3788 		if (!smfs_cap) {
3789 			NL_SET_ERR_MSG_MOD(extack,
3790 					   "Software managed steering is not supported by current device");
3791 			return -EOPNOTSUPP;
3792 		}
3793 	} else if (!strcmp(value, "hmfs")) {
3794 		bool hmfs_cap = mlx5_fs_hws_is_supported(dev);
3795 
3796 		if (!hmfs_cap) {
3797 			NL_SET_ERR_MSG_MOD(extack,
3798 					   "Hardware steering is not supported by current device");
3799 			return -EOPNOTSUPP;
3800 		}
3801 	} else {
3802 		NL_SET_ERR_MSG_MOD(extack,
3803 				   "Bad parameter: supported values are [\"dmfs\", \"smfs\", \"hmfs\"]");
3804 		return -EINVAL;
3805 	}
3806 
3807 	return 0;
3808 }
3809 
3810 static int mlx5_fs_mode_set(struct devlink *devlink, u32 id,
3811 			    struct devlink_param_gset_ctx *ctx,
3812 			    struct netlink_ext_ack *extack)
3813 {
3814 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3815 	enum mlx5_flow_steering_mode mode;
3816 
3817 	if (!strcmp(ctx->val.vstr, "smfs"))
3818 		mode = MLX5_FLOW_STEERING_MODE_SMFS;
3819 	else if (!strcmp(ctx->val.vstr, "hmfs"))
3820 		mode = MLX5_FLOW_STEERING_MODE_HMFS;
3821 	else
3822 		mode = MLX5_FLOW_STEERING_MODE_DMFS;
3823 	dev->priv.steering->mode = mode;
3824 
3825 	return 0;
3826 }
3827 
3828 static int mlx5_fs_mode_get(struct devlink *devlink, u32 id,
3829 			    struct devlink_param_gset_ctx *ctx,
3830 			    struct netlink_ext_ack *extack)
3831 {
3832 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3833 
3834 	switch (dev->priv.steering->mode) {
3835 	case MLX5_FLOW_STEERING_MODE_SMFS:
3836 		strscpy(ctx->val.vstr, "smfs", sizeof(ctx->val.vstr));
3837 		break;
3838 	case MLX5_FLOW_STEERING_MODE_HMFS:
3839 		strscpy(ctx->val.vstr, "hmfs", sizeof(ctx->val.vstr));
3840 		break;
3841 	default:
3842 		strscpy(ctx->val.vstr, "dmfs", sizeof(ctx->val.vstr));
3843 	}
3844 
3845 	return 0;
3846 }
3847 
3848 static const struct devlink_param mlx5_fs_params[] = {
3849 	DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE,
3850 			     "flow_steering_mode", DEVLINK_PARAM_TYPE_STRING,
3851 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
3852 			     mlx5_fs_mode_get, mlx5_fs_mode_set,
3853 			     mlx5_fs_mode_validate),
3854 };
3855 
3856 void mlx5_fs_core_cleanup(struct mlx5_core_dev *dev)
3857 {
3858 	struct mlx5_flow_steering *steering = dev->priv.steering;
3859 
3860 	WARN_ON(!xa_empty(&steering->esw_egress_root_ns));
3861 	WARN_ON(!xa_empty(&steering->esw_ingress_root_ns));
3862 	xa_destroy(&steering->esw_egress_root_ns);
3863 	xa_destroy(&steering->esw_ingress_root_ns);
3864 
3865 	cleanup_root_ns(steering->root_ns);
3866 	cleanup_fdb_root_ns(steering);
3867 	cleanup_root_ns(steering->port_sel_root_ns);
3868 	cleanup_root_ns(steering->sniffer_rx_root_ns);
3869 	cleanup_root_ns(steering->sniffer_tx_root_ns);
3870 	cleanup_root_ns(steering->rdma_rx_root_ns);
3871 	cleanup_root_ns(steering->rdma_tx_root_ns);
3872 	cleanup_root_ns(steering->egress_root_ns);
3873 	cleanup_rdma_transport_roots_ns(steering);
3874 
3875 	devl_params_unregister(priv_to_devlink(dev), mlx5_fs_params,
3876 			       ARRAY_SIZE(mlx5_fs_params));
3877 }
3878 
3879 int mlx5_fs_core_init(struct mlx5_core_dev *dev)
3880 {
3881 	struct mlx5_flow_steering *steering = dev->priv.steering;
3882 	int err;
3883 
3884 	err = devl_params_register(priv_to_devlink(dev), mlx5_fs_params,
3885 				   ARRAY_SIZE(mlx5_fs_params));
3886 	if (err)
3887 		return err;
3888 
3889 	if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3890 	      (MLX5_CAP_GEN(dev, nic_flow_table))) ||
3891 	     ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
3892 	      MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
3893 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
3894 		err = init_root_ns(steering);
3895 		if (err)
3896 			goto err;
3897 	}
3898 
3899 	if (MLX5_ESWITCH_MANAGER(dev)) {
3900 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
3901 			err = init_fdb_root_ns(steering);
3902 			if (err)
3903 				goto err;
3904 		}
3905 	}
3906 
3907 	if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
3908 		err = init_sniffer_rx_root_ns(steering);
3909 		if (err)
3910 			goto err;
3911 	}
3912 
3913 	if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
3914 		err = init_sniffer_tx_root_ns(steering);
3915 		if (err)
3916 			goto err;
3917 	}
3918 
3919 	if (MLX5_CAP_FLOWTABLE_PORT_SELECTION(dev, ft_support)) {
3920 		err = init_port_sel_root_ns(steering);
3921 		if (err)
3922 			goto err;
3923 	}
3924 
3925 	if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support)) {
3926 		err = init_rdma_rx_root_ns(steering);
3927 		if (err)
3928 			goto err;
3929 	}
3930 
3931 	if (MLX5_CAP_FLOWTABLE_RDMA_TX(dev, ft_support)) {
3932 		err = init_rdma_tx_root_ns(steering);
3933 		if (err)
3934 			goto err;
3935 	}
3936 
3937 	if (MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
3938 		err = init_egress_root_ns(steering);
3939 		if (err)
3940 			goto err;
3941 	}
3942 
3943 	if (MLX5_CAP_FLOWTABLE_RDMA_TRANSPORT_RX(dev, ft_support)) {
3944 		err = init_rdma_transport_rx_root_ns(steering);
3945 		if (err)
3946 			goto err;
3947 	}
3948 
3949 	if (MLX5_CAP_FLOWTABLE_RDMA_TRANSPORT_TX(dev, ft_support)) {
3950 		err = init_rdma_transport_tx_root_ns(steering);
3951 		if (err)
3952 			goto err;
3953 	}
3954 
3955 	xa_init(&steering->esw_egress_root_ns);
3956 	xa_init(&steering->esw_ingress_root_ns);
3957 	return 0;
3958 
3959 err:
3960 	mlx5_fs_core_cleanup(dev);
3961 	return err;
3962 }
3963 
3964 void mlx5_fs_core_free(struct mlx5_core_dev *dev)
3965 {
3966 	struct mlx5_flow_steering *steering = dev->priv.steering;
3967 
3968 	kmem_cache_destroy(steering->ftes_cache);
3969 	kmem_cache_destroy(steering->fgs_cache);
3970 	kfree(steering);
3971 	mlx5_ft_pool_destroy(dev);
3972 	mlx5_cleanup_fc_stats(dev);
3973 }
3974 
3975 int mlx5_fs_core_alloc(struct mlx5_core_dev *dev)
3976 {
3977 	struct mlx5_flow_steering *steering;
3978 	char name[80];
3979 	int err = 0;
3980 
3981 	err = mlx5_init_fc_stats(dev);
3982 	if (err)
3983 		return err;
3984 
3985 	err = mlx5_ft_pool_init(dev);
3986 	if (err)
3987 		goto err;
3988 
3989 	steering = kzalloc_obj(*steering);
3990 	if (!steering) {
3991 		err = -ENOMEM;
3992 		goto err;
3993 	}
3994 
3995 	steering->dev = dev;
3996 	dev->priv.steering = steering;
3997 
3998 	if (mlx5_fs_dr_is_supported(dev))
3999 		steering->mode = MLX5_FLOW_STEERING_MODE_SMFS;
4000 	else if (mlx5_fs_hws_is_supported(dev))
4001 		steering->mode = MLX5_FLOW_STEERING_MODE_HMFS;
4002 	else
4003 		steering->mode = MLX5_FLOW_STEERING_MODE_DMFS;
4004 
4005 	snprintf(name, sizeof(name), "%s-mlx5_fs_fgs", dev_name(dev->device));
4006 	steering->fgs_cache = kmem_cache_create(name,
4007 						sizeof(struct mlx5_flow_group), 0,
4008 						0, NULL);
4009 	snprintf(name, sizeof(name), "%s-mlx5_fs_ftes", dev_name(dev->device));
4010 	steering->ftes_cache = kmem_cache_create(name, sizeof(struct fs_fte), 0,
4011 						 0, NULL);
4012 	if (!steering->ftes_cache || !steering->fgs_cache) {
4013 		err = -ENOMEM;
4014 		goto err;
4015 	}
4016 
4017 	return 0;
4018 
4019 err:
4020 	mlx5_fs_core_free(dev);
4021 	return err;
4022 }
4023 
4024 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
4025 {
4026 	struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
4027 	struct mlx5_ft_underlay_qp *new_uqp;
4028 	int err = 0;
4029 
4030 	new_uqp = kzalloc_obj(*new_uqp);
4031 	if (!new_uqp)
4032 		return -ENOMEM;
4033 
4034 	mutex_lock(&root->chain_lock);
4035 
4036 	if (!root->root_ft) {
4037 		err = -EINVAL;
4038 		goto update_ft_fail;
4039 	}
4040 
4041 	err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
4042 					 false);
4043 	if (err) {
4044 		mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
4045 			       underlay_qpn, err);
4046 		goto update_ft_fail;
4047 	}
4048 
4049 	new_uqp->qpn = underlay_qpn;
4050 	list_add_tail(&new_uqp->list, &root->underlay_qpns);
4051 
4052 	mutex_unlock(&root->chain_lock);
4053 
4054 	return 0;
4055 
4056 update_ft_fail:
4057 	mutex_unlock(&root->chain_lock);
4058 	kfree(new_uqp);
4059 	return err;
4060 }
4061 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
4062 
4063 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
4064 {
4065 	struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
4066 	struct mlx5_ft_underlay_qp *uqp;
4067 	bool found = false;
4068 	int err = 0;
4069 
4070 	mutex_lock(&root->chain_lock);
4071 	list_for_each_entry(uqp, &root->underlay_qpns, list) {
4072 		if (uqp->qpn == underlay_qpn) {
4073 			found = true;
4074 			break;
4075 		}
4076 	}
4077 
4078 	if (!found) {
4079 		mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
4080 			       underlay_qpn);
4081 		err = -EINVAL;
4082 		goto out;
4083 	}
4084 
4085 	err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
4086 					 true);
4087 	if (err)
4088 		mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
4089 			       underlay_qpn, err);
4090 
4091 	list_del(&uqp->list);
4092 	mutex_unlock(&root->chain_lock);
4093 	kfree(uqp);
4094 
4095 	return 0;
4096 
4097 out:
4098 	mutex_unlock(&root->chain_lock);
4099 	return err;
4100 }
4101 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
4102 
4103 struct mlx5_flow_root_namespace *
4104 mlx5_get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
4105 {
4106 	struct mlx5_flow_namespace *ns;
4107 
4108 	if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
4109 	    ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS ||
4110 	    ns_type == MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_TX ||
4111 	    ns_type == MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_RX)
4112 		ns = mlx5_get_flow_vport_namespace(dev, ns_type, 0);
4113 	else
4114 		ns = mlx5_get_flow_namespace(dev, ns_type);
4115 	if (!ns)
4116 		return NULL;
4117 
4118 	return find_root(&ns->node);
4119 }
4120 
4121 struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
4122 						 u8 ns_type, u8 num_actions,
4123 						 void *modify_actions)
4124 {
4125 	struct mlx5_flow_root_namespace *root;
4126 	struct mlx5_modify_hdr *modify_hdr;
4127 	int err;
4128 
4129 	root = mlx5_get_root_namespace(dev, ns_type);
4130 	if (!root)
4131 		return ERR_PTR(-EOPNOTSUPP);
4132 
4133 	modify_hdr = kzalloc_obj(*modify_hdr);
4134 	if (!modify_hdr)
4135 		return ERR_PTR(-ENOMEM);
4136 
4137 	modify_hdr->ns_type = ns_type;
4138 	err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
4139 					      modify_actions, modify_hdr);
4140 	if (err) {
4141 		kfree(modify_hdr);
4142 		return ERR_PTR(err);
4143 	}
4144 
4145 	return modify_hdr;
4146 }
4147 EXPORT_SYMBOL(mlx5_modify_header_alloc);
4148 
4149 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
4150 				struct mlx5_modify_hdr *modify_hdr)
4151 {
4152 	struct mlx5_flow_root_namespace *root;
4153 
4154 	root = mlx5_get_root_namespace(dev, modify_hdr->ns_type);
4155 	if (WARN_ON(!root))
4156 		return;
4157 	root->cmds->modify_header_dealloc(root, modify_hdr);
4158 	kfree(modify_hdr);
4159 }
4160 EXPORT_SYMBOL(mlx5_modify_header_dealloc);
4161 
4162 struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
4163 						     struct mlx5_pkt_reformat_params *params,
4164 						     enum mlx5_flow_namespace_type ns_type)
4165 {
4166 	struct mlx5_pkt_reformat *pkt_reformat;
4167 	struct mlx5_flow_root_namespace *root;
4168 	int err;
4169 
4170 	root = mlx5_get_root_namespace(dev, ns_type);
4171 	if (!root)
4172 		return ERR_PTR(-EOPNOTSUPP);
4173 
4174 	pkt_reformat = kzalloc_obj(*pkt_reformat);
4175 	if (!pkt_reformat)
4176 		return ERR_PTR(-ENOMEM);
4177 
4178 	pkt_reformat->ns_type = ns_type;
4179 	pkt_reformat->reformat_type = params->type;
4180 	err = root->cmds->packet_reformat_alloc(root, params, ns_type,
4181 						pkt_reformat);
4182 	if (err) {
4183 		kfree(pkt_reformat);
4184 		return ERR_PTR(err);
4185 	}
4186 
4187 	return pkt_reformat;
4188 }
4189 EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
4190 
4191 void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
4192 				  struct mlx5_pkt_reformat *pkt_reformat)
4193 {
4194 	struct mlx5_flow_root_namespace *root;
4195 
4196 	root = mlx5_get_root_namespace(dev, pkt_reformat->ns_type);
4197 	if (WARN_ON(!root))
4198 		return;
4199 	root->cmds->packet_reformat_dealloc(root, pkt_reformat);
4200 	kfree(pkt_reformat);
4201 }
4202 EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
4203 
4204 int mlx5_get_match_definer_id(struct mlx5_flow_definer *definer)
4205 {
4206 	return definer->id;
4207 }
4208 
4209 struct mlx5_flow_definer *
4210 mlx5_create_match_definer(struct mlx5_core_dev *dev,
4211 			  enum mlx5_flow_namespace_type ns_type, u16 format_id,
4212 			  u32 *match_mask)
4213 {
4214 	struct mlx5_flow_root_namespace *root;
4215 	struct mlx5_flow_definer *definer;
4216 	int id;
4217 
4218 	root = mlx5_get_root_namespace(dev, ns_type);
4219 	if (!root)
4220 		return ERR_PTR(-EOPNOTSUPP);
4221 
4222 	definer = kzalloc_obj(*definer);
4223 	if (!definer)
4224 		return ERR_PTR(-ENOMEM);
4225 
4226 	definer->ns_type = ns_type;
4227 	id = root->cmds->create_match_definer(root, format_id, match_mask);
4228 	if (id < 0) {
4229 		mlx5_core_warn(root->dev, "Failed to create match definer (%d)\n", id);
4230 		kfree(definer);
4231 		return ERR_PTR(id);
4232 	}
4233 	definer->id = id;
4234 	return definer;
4235 }
4236 
4237 void mlx5_destroy_match_definer(struct mlx5_core_dev *dev,
4238 				struct mlx5_flow_definer *definer)
4239 {
4240 	struct mlx5_flow_root_namespace *root;
4241 
4242 	root = mlx5_get_root_namespace(dev, definer->ns_type);
4243 	if (WARN_ON(!root))
4244 		return;
4245 
4246 	root->cmds->destroy_match_definer(root, definer->id);
4247 	kfree(definer);
4248 }
4249 
4250 int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
4251 				 struct mlx5_flow_root_namespace *peer_ns,
4252 				 u16 peer_vhca_id)
4253 {
4254 	if (peer_ns && ns->mode != peer_ns->mode) {
4255 		mlx5_core_err(ns->dev,
4256 			      "Can't peer namespace of different steering mode\n");
4257 		return -EINVAL;
4258 	}
4259 
4260 	return ns->cmds->set_peer(ns, peer_ns, peer_vhca_id);
4261 }
4262 
4263 /* This function should be called only at init stage of the namespace.
4264  * It is not safe to call this function while steering operations
4265  * are executed in the namespace.
4266  */
4267 int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
4268 				 enum mlx5_flow_steering_mode mode)
4269 {
4270 	struct mlx5_flow_root_namespace *root;
4271 	const struct mlx5_flow_cmds *cmds;
4272 	int err;
4273 
4274 	root = find_root(&ns->node);
4275 	if (&root->ns != ns)
4276 	/* Can't set cmds to non root namespace */
4277 		return -EINVAL;
4278 
4279 	if (root->table_type != FS_FT_FDB)
4280 		return -EOPNOTSUPP;
4281 
4282 	if (root->mode == mode)
4283 		return 0;
4284 
4285 	if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
4286 		cmds = mlx5_fs_cmd_get_dr_cmds();
4287 	else if (mode == MLX5_FLOW_STEERING_MODE_HMFS)
4288 		cmds = mlx5_fs_cmd_get_hws_cmds();
4289 	else
4290 		cmds = mlx5_fs_cmd_get_fw_cmds();
4291 	if (!cmds)
4292 		return -EOPNOTSUPP;
4293 
4294 	err = cmds->create_ns(root);
4295 	if (err) {
4296 		mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
4297 			      err);
4298 		return err;
4299 	}
4300 
4301 	root->cmds->destroy_ns(root);
4302 	root->cmds = cmds;
4303 	root->mode = mode;
4304 
4305 	return 0;
4306 }
4307