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