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