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