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