xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/fs_core.c (revision 72c181399b01bb4836d1fabaa9f5f6438c82178e)
1 /*
2  * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/mutex.h>
34 #include <linux/mlx5/driver.h>
35 #include <linux/mlx5/vport.h>
36 #include <linux/mlx5/eswitch.h>
37 #include <net/devlink.h>
38 
39 #include "mlx5_core.h"
40 #include "fs_core.h"
41 #include "fs_cmd.h"
42 #include "fs_ft_pool.h"
43 #include "diag/fs_tracepoint.h"
44 #include "devlink.h"
45 
46 #define INIT_TREE_NODE_ARRAY_SIZE(...)	(sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
47 					 sizeof(struct init_tree_node))
48 
49 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
50 		 ...) {.type = FS_TYPE_PRIO,\
51 	.min_ft_level = min_level_val,\
52 	.num_levels = num_levels_val,\
53 	.num_leaf_prios = num_prios_val,\
54 	.caps = caps_val,\
55 	.children = (struct init_tree_node[]) {__VA_ARGS__},\
56 	.ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
57 }
58 
59 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
60 	ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
61 		 __VA_ARGS__)\
62 
63 #define ADD_NS(def_miss_act, ...) {.type = FS_TYPE_NAMESPACE,	\
64 	.def_miss_action = def_miss_act,\
65 	.children = (struct init_tree_node[]) {__VA_ARGS__},\
66 	.ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
67 }
68 
69 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
70 				   sizeof(long))
71 
72 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
73 
74 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
75 			       .caps = (long[]) {__VA_ARGS__} }
76 
77 #define FS_CHAINING_CAPS  FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
78 					   FS_CAP(flow_table_properties_nic_receive.modify_root), \
79 					   FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
80 					   FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
81 
82 #define FS_CHAINING_CAPS_EGRESS                                                \
83 	FS_REQUIRED_CAPS(                                                      \
84 		FS_CAP(flow_table_properties_nic_transmit.flow_modify_en),     \
85 		FS_CAP(flow_table_properties_nic_transmit.modify_root),        \
86 		FS_CAP(flow_table_properties_nic_transmit                      \
87 			       .identified_miss_table_mode),                   \
88 		FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
89 
90 #define FS_CHAINING_CAPS_RDMA_TX                                                \
91 	FS_REQUIRED_CAPS(                                                       \
92 		FS_CAP(flow_table_properties_nic_transmit_rdma.flow_modify_en), \
93 		FS_CAP(flow_table_properties_nic_transmit_rdma.modify_root),    \
94 		FS_CAP(flow_table_properties_nic_transmit_rdma                  \
95 			       .identified_miss_table_mode),                    \
96 		FS_CAP(flow_table_properties_nic_transmit_rdma                  \
97 			       .flow_table_modify))
98 
99 #define LEFTOVERS_NUM_LEVELS 1
100 #define LEFTOVERS_NUM_PRIOS 1
101 
102 #define RDMA_RX_COUNTERS_PRIO_NUM_LEVELS 1
103 #define RDMA_TX_COUNTERS_PRIO_NUM_LEVELS 1
104 
105 #define BY_PASS_PRIO_NUM_LEVELS 1
106 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
107 			   LEFTOVERS_NUM_PRIOS)
108 
109 #define KERNEL_RX_MACSEC_NUM_PRIOS  1
110 #define KERNEL_RX_MACSEC_NUM_LEVELS 3
111 #define KERNEL_RX_MACSEC_MIN_LEVEL (BY_PASS_MIN_LEVEL + KERNEL_RX_MACSEC_NUM_PRIOS)
112 
113 #define ETHTOOL_PRIO_NUM_LEVELS 1
114 #define ETHTOOL_NUM_PRIOS 11
115 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
116 /* Vlan, mac, ttc, inner ttc, {UDP/ANY/aRFS/accel/{esp, esp_err}}, IPsec policy,
117  * IPsec policy miss, {IPsec RoCE MPV,Alias table},IPsec RoCE policy
118  */
119 #define KERNEL_NIC_PRIO_NUM_LEVELS 11
120 #define KERNEL_NIC_NUM_PRIOS 1
121 /* One more level for tc, and one more for promisc */
122 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 2)
123 
124 #define KERNEL_NIC_PROMISC_NUM_PRIOS 1
125 #define KERNEL_NIC_PROMISC_NUM_LEVELS 1
126 
127 #define KERNEL_NIC_TC_NUM_PRIOS  1
128 #define KERNEL_NIC_TC_NUM_LEVELS 3
129 
130 #define ANCHOR_NUM_LEVELS 1
131 #define ANCHOR_NUM_PRIOS 1
132 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
133 
134 #define OFFLOADS_MAX_FT 2
135 #define OFFLOADS_NUM_PRIOS 2
136 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + OFFLOADS_NUM_PRIOS)
137 
138 #define LAG_PRIO_NUM_LEVELS 1
139 #define LAG_NUM_PRIOS 1
140 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + KERNEL_RX_MACSEC_MIN_LEVEL + 1)
141 
142 #define KERNEL_TX_IPSEC_NUM_PRIOS  1
143 #define KERNEL_TX_IPSEC_NUM_LEVELS 4
144 #define KERNEL_TX_IPSEC_MIN_LEVEL        (KERNEL_TX_IPSEC_NUM_LEVELS)
145 
146 #define KERNEL_TX_MACSEC_NUM_PRIOS  1
147 #define KERNEL_TX_MACSEC_NUM_LEVELS 2
148 #define KERNEL_TX_MACSEC_MIN_LEVEL       (KERNEL_TX_IPSEC_MIN_LEVEL + KERNEL_TX_MACSEC_NUM_PRIOS)
149 
150 struct node_caps {
151 	size_t	arr_sz;
152 	long	*caps;
153 };
154 
155 static struct init_tree_node {
156 	enum fs_node_type	type;
157 	struct init_tree_node *children;
158 	int ar_size;
159 	struct node_caps caps;
160 	int min_ft_level;
161 	int num_leaf_prios;
162 	int prio;
163 	int num_levels;
164 	enum mlx5_flow_table_miss_action def_miss_action;
165 } root_fs = {
166 	.type = FS_TYPE_NAMESPACE,
167 	.ar_size = 8,
168 	  .children = (struct init_tree_node[]){
169 		  ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
170 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
171 				  ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
172 						    BY_PASS_PRIO_NUM_LEVELS))),
173 		  ADD_PRIO(0, KERNEL_RX_MACSEC_MIN_LEVEL, 0, FS_CHAINING_CAPS,
174 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
175 				  ADD_MULTIPLE_PRIO(KERNEL_RX_MACSEC_NUM_PRIOS,
176 						    KERNEL_RX_MACSEC_NUM_LEVELS))),
177 		  ADD_PRIO(0, LAG_MIN_LEVEL, 0, FS_CHAINING_CAPS,
178 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
179 				  ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
180 						    LAG_PRIO_NUM_LEVELS))),
181 		  ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
182 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
183 				  ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS,
184 						    OFFLOADS_MAX_FT))),
185 		  ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0, FS_CHAINING_CAPS,
186 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
187 				  ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
188 						    ETHTOOL_PRIO_NUM_LEVELS))),
189 		  ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
190 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
191 				  ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS,
192 						    KERNEL_NIC_TC_NUM_LEVELS),
193 				  ADD_MULTIPLE_PRIO(KERNEL_NIC_PROMISC_NUM_PRIOS,
194 						    KERNEL_NIC_PROMISC_NUM_LEVELS),
195 				  ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
196 						    KERNEL_NIC_PRIO_NUM_LEVELS))),
197 		  ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
198 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
199 				  ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS,
200 						    LEFTOVERS_NUM_LEVELS))),
201 		  ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
202 			   ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
203 				  ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS,
204 						    ANCHOR_NUM_LEVELS))),
205 	}
206 };
207 
208 static struct init_tree_node egress_root_fs = {
209 	.type = FS_TYPE_NAMESPACE,
210 	.ar_size = 3,
211 	.children = (struct init_tree_node[]) {
212 		ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
213 			 FS_CHAINING_CAPS_EGRESS,
214 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
215 				ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
216 						  BY_PASS_PRIO_NUM_LEVELS))),
217 		ADD_PRIO(0, KERNEL_TX_IPSEC_MIN_LEVEL, 0,
218 			 FS_CHAINING_CAPS_EGRESS,
219 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
220 				ADD_MULTIPLE_PRIO(KERNEL_TX_IPSEC_NUM_PRIOS,
221 						  KERNEL_TX_IPSEC_NUM_LEVELS))),
222 		ADD_PRIO(0, KERNEL_TX_MACSEC_MIN_LEVEL, 0,
223 			 FS_CHAINING_CAPS_EGRESS,
224 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
225 				ADD_MULTIPLE_PRIO(KERNEL_TX_MACSEC_NUM_PRIOS,
226 						  KERNEL_TX_MACSEC_NUM_LEVELS))),
227 	}
228 };
229 
230 enum {
231 	RDMA_RX_IPSEC_PRIO,
232 	RDMA_RX_MACSEC_PRIO,
233 	RDMA_RX_COUNTERS_PRIO,
234 	RDMA_RX_BYPASS_PRIO,
235 	RDMA_RX_KERNEL_PRIO,
236 };
237 
238 #define RDMA_RX_IPSEC_NUM_PRIOS 1
239 #define RDMA_RX_IPSEC_NUM_LEVELS 4
240 #define RDMA_RX_IPSEC_MIN_LEVEL  (RDMA_RX_IPSEC_NUM_LEVELS)
241 
242 #define RDMA_RX_BYPASS_MIN_LEVEL MLX5_BY_PASS_NUM_REGULAR_PRIOS
243 #define RDMA_RX_KERNEL_MIN_LEVEL (RDMA_RX_BYPASS_MIN_LEVEL + 1)
244 #define RDMA_RX_COUNTERS_MIN_LEVEL (RDMA_RX_KERNEL_MIN_LEVEL + 2)
245 
246 #define RDMA_RX_MACSEC_NUM_PRIOS 1
247 #define RDMA_RX_MACSEC_PRIO_NUM_LEVELS 2
248 #define RDMA_RX_MACSEC_MIN_LEVEL  (RDMA_RX_COUNTERS_MIN_LEVEL + RDMA_RX_MACSEC_NUM_PRIOS)
249 
250 static struct init_tree_node rdma_rx_root_fs = {
251 	.type = FS_TYPE_NAMESPACE,
252 	.ar_size = 5,
253 	.children = (struct init_tree_node[]) {
254 		[RDMA_RX_IPSEC_PRIO] =
255 		ADD_PRIO(0, RDMA_RX_IPSEC_MIN_LEVEL, 0,
256 			 FS_CHAINING_CAPS,
257 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
258 				ADD_MULTIPLE_PRIO(RDMA_RX_IPSEC_NUM_PRIOS,
259 						  RDMA_RX_IPSEC_NUM_LEVELS))),
260 		[RDMA_RX_MACSEC_PRIO] =
261 		ADD_PRIO(0, RDMA_RX_MACSEC_MIN_LEVEL, 0,
262 			 FS_CHAINING_CAPS,
263 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
264 				ADD_MULTIPLE_PRIO(RDMA_RX_MACSEC_NUM_PRIOS,
265 						  RDMA_RX_MACSEC_PRIO_NUM_LEVELS))),
266 		[RDMA_RX_COUNTERS_PRIO] =
267 		ADD_PRIO(0, RDMA_RX_COUNTERS_MIN_LEVEL, 0,
268 			 FS_CHAINING_CAPS,
269 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
270 				ADD_MULTIPLE_PRIO(MLX5_RDMA_RX_NUM_COUNTERS_PRIOS,
271 						  RDMA_RX_COUNTERS_PRIO_NUM_LEVELS))),
272 		[RDMA_RX_BYPASS_PRIO] =
273 		ADD_PRIO(0, RDMA_RX_BYPASS_MIN_LEVEL, 0,
274 			 FS_CHAINING_CAPS,
275 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
276 				ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_REGULAR_PRIOS,
277 						  BY_PASS_PRIO_NUM_LEVELS))),
278 		[RDMA_RX_KERNEL_PRIO] =
279 		ADD_PRIO(0, RDMA_RX_KERNEL_MIN_LEVEL, 0,
280 			 FS_CHAINING_CAPS,
281 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_SWITCH_DOMAIN,
282 				ADD_MULTIPLE_PRIO(1, 1))),
283 	}
284 };
285 
286 enum {
287 	RDMA_TX_COUNTERS_PRIO,
288 	RDMA_TX_IPSEC_PRIO,
289 	RDMA_TX_MACSEC_PRIO,
290 	RDMA_TX_BYPASS_PRIO,
291 };
292 
293 #define RDMA_TX_BYPASS_MIN_LEVEL MLX5_BY_PASS_NUM_PRIOS
294 #define RDMA_TX_COUNTERS_MIN_LEVEL (RDMA_TX_BYPASS_MIN_LEVEL + 1)
295 
296 #define RDMA_TX_IPSEC_NUM_PRIOS 2
297 #define RDMA_TX_IPSEC_PRIO_NUM_LEVELS 1
298 #define RDMA_TX_IPSEC_MIN_LEVEL  (RDMA_TX_COUNTERS_MIN_LEVEL + RDMA_TX_IPSEC_NUM_PRIOS)
299 
300 #define RDMA_TX_MACSEC_NUM_PRIOS 1
301 #define RDMA_TX_MACESC_PRIO_NUM_LEVELS 1
302 #define RDMA_TX_MACSEC_MIN_LEVEL  (RDMA_TX_COUNTERS_MIN_LEVEL + RDMA_TX_MACSEC_NUM_PRIOS)
303 
304 static struct init_tree_node rdma_tx_root_fs = {
305 	.type = FS_TYPE_NAMESPACE,
306 	.ar_size = 4,
307 	.children = (struct init_tree_node[]) {
308 		[RDMA_TX_COUNTERS_PRIO] =
309 		ADD_PRIO(0, RDMA_TX_COUNTERS_MIN_LEVEL, 0,
310 			 FS_CHAINING_CAPS,
311 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
312 				ADD_MULTIPLE_PRIO(MLX5_RDMA_TX_NUM_COUNTERS_PRIOS,
313 						  RDMA_TX_COUNTERS_PRIO_NUM_LEVELS))),
314 		[RDMA_TX_IPSEC_PRIO] =
315 		ADD_PRIO(0, RDMA_TX_IPSEC_MIN_LEVEL, 0,
316 			 FS_CHAINING_CAPS,
317 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
318 				ADD_MULTIPLE_PRIO(RDMA_TX_IPSEC_NUM_PRIOS,
319 						  RDMA_TX_IPSEC_PRIO_NUM_LEVELS))),
320 		[RDMA_TX_MACSEC_PRIO] =
321 		ADD_PRIO(0, RDMA_TX_MACSEC_MIN_LEVEL, 0,
322 			 FS_CHAINING_CAPS,
323 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
324 				ADD_MULTIPLE_PRIO(RDMA_TX_MACSEC_NUM_PRIOS,
325 						  RDMA_TX_MACESC_PRIO_NUM_LEVELS))),
326 		[RDMA_TX_BYPASS_PRIO] =
327 		ADD_PRIO(0, RDMA_TX_BYPASS_MIN_LEVEL, 0,
328 			 FS_CHAINING_CAPS_RDMA_TX,
329 			 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
330 				ADD_MULTIPLE_PRIO(RDMA_TX_BYPASS_MIN_LEVEL,
331 						  BY_PASS_PRIO_NUM_LEVELS))),
332 	}
333 };
334 
335 enum fs_i_lock_class {
336 	FS_LOCK_GRANDPARENT,
337 	FS_LOCK_PARENT,
338 	FS_LOCK_CHILD
339 };
340 
341 static const struct rhashtable_params rhash_fte = {
342 	.key_len = sizeof_field(struct fs_fte, val),
343 	.key_offset = offsetof(struct fs_fte, val),
344 	.head_offset = offsetof(struct fs_fte, hash),
345 	.automatic_shrinking = true,
346 	.min_size = 1,
347 };
348 
349 static const struct rhashtable_params rhash_fg = {
350 	.key_len = sizeof_field(struct mlx5_flow_group, mask),
351 	.key_offset = offsetof(struct mlx5_flow_group, mask),
352 	.head_offset = offsetof(struct mlx5_flow_group, hash),
353 	.automatic_shrinking = true,
354 	.min_size = 1,
355 
356 };
357 
358 static void del_hw_flow_table(struct fs_node *node);
359 static void del_hw_flow_group(struct fs_node *node);
360 static void del_hw_fte(struct fs_node *node);
361 static void del_sw_flow_table(struct fs_node *node);
362 static void del_sw_flow_group(struct fs_node *node);
363 static void del_sw_fte(struct fs_node *node);
364 static void del_sw_prio(struct fs_node *node);
365 static void del_sw_ns(struct fs_node *node);
366 /* Delete rule (destination) is special case that
367  * requires to lock the FTE for all the deletion process.
368  */
369 static void del_sw_hw_rule(struct fs_node *node);
370 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
371 				struct mlx5_flow_destination *d2);
372 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
373 static struct mlx5_flow_rule *
374 find_flow_rule(struct fs_fte *fte,
375 	       struct mlx5_flow_destination *dest);
376 
tree_init_node(struct fs_node * node,void (* del_hw_func)(struct fs_node *),void (* del_sw_func)(struct fs_node *))377 static void tree_init_node(struct fs_node *node,
378 			   void (*del_hw_func)(struct fs_node *),
379 			   void (*del_sw_func)(struct fs_node *))
380 {
381 	refcount_set(&node->refcount, 1);
382 	INIT_LIST_HEAD(&node->list);
383 	INIT_LIST_HEAD(&node->children);
384 	init_rwsem(&node->lock);
385 	node->del_hw_func = del_hw_func;
386 	node->del_sw_func = del_sw_func;
387 	node->active = false;
388 }
389 
tree_add_node(struct fs_node * node,struct fs_node * parent)390 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
391 {
392 	if (parent)
393 		refcount_inc(&parent->refcount);
394 	node->parent = parent;
395 
396 	/* Parent is the root */
397 	if (!parent)
398 		node->root = node;
399 	else
400 		node->root = parent->root;
401 }
402 
tree_get_node(struct fs_node * node)403 static int tree_get_node(struct fs_node *node)
404 {
405 	return refcount_inc_not_zero(&node->refcount);
406 }
407 
nested_down_read_ref_node(struct fs_node * node,enum fs_i_lock_class class)408 static void nested_down_read_ref_node(struct fs_node *node,
409 				      enum fs_i_lock_class class)
410 {
411 	if (node) {
412 		down_read_nested(&node->lock, class);
413 		refcount_inc(&node->refcount);
414 	}
415 }
416 
nested_down_write_ref_node(struct fs_node * node,enum fs_i_lock_class class)417 static void nested_down_write_ref_node(struct fs_node *node,
418 				       enum fs_i_lock_class class)
419 {
420 	if (node) {
421 		down_write_nested(&node->lock, class);
422 		refcount_inc(&node->refcount);
423 	}
424 }
425 
down_write_ref_node(struct fs_node * node,bool locked)426 static void down_write_ref_node(struct fs_node *node, bool locked)
427 {
428 	if (node) {
429 		if (!locked)
430 			down_write(&node->lock);
431 		refcount_inc(&node->refcount);
432 	}
433 }
434 
up_read_ref_node(struct fs_node * node)435 static void up_read_ref_node(struct fs_node *node)
436 {
437 	refcount_dec(&node->refcount);
438 	up_read(&node->lock);
439 }
440 
up_write_ref_node(struct fs_node * node,bool locked)441 static void up_write_ref_node(struct fs_node *node, bool locked)
442 {
443 	refcount_dec(&node->refcount);
444 	if (!locked)
445 		up_write(&node->lock);
446 }
447 
tree_put_node(struct fs_node * node,bool locked)448 static void tree_put_node(struct fs_node *node, bool locked)
449 {
450 	struct fs_node *parent_node = node->parent;
451 
452 	if (refcount_dec_and_test(&node->refcount)) {
453 		if (node->del_hw_func)
454 			node->del_hw_func(node);
455 		if (parent_node) {
456 			down_write_ref_node(parent_node, locked);
457 			list_del_init(&node->list);
458 		}
459 		node->del_sw_func(node);
460 		if (parent_node)
461 			up_write_ref_node(parent_node, locked);
462 		node = NULL;
463 	}
464 	if (!node && parent_node)
465 		tree_put_node(parent_node, locked);
466 }
467 
tree_remove_node(struct fs_node * node,bool locked)468 static int tree_remove_node(struct fs_node *node, bool locked)
469 {
470 	if (refcount_read(&node->refcount) > 1) {
471 		refcount_dec(&node->refcount);
472 		return -EEXIST;
473 	}
474 	tree_put_node(node, locked);
475 	return 0;
476 }
477 
find_prio(struct mlx5_flow_namespace * ns,unsigned int prio)478 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
479 				 unsigned int prio)
480 {
481 	struct fs_prio *iter_prio;
482 
483 	fs_for_each_prio(iter_prio, ns) {
484 		if (iter_prio->prio == prio)
485 			return iter_prio;
486 	}
487 
488 	return NULL;
489 }
490 
is_fwd_next_action(u32 action)491 static bool is_fwd_next_action(u32 action)
492 {
493 	return action & (MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
494 			 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
495 }
496 
is_fwd_dest_type(enum mlx5_flow_destination_type type)497 static bool is_fwd_dest_type(enum mlx5_flow_destination_type type)
498 {
499 	return type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM ||
500 		type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE ||
501 		type == MLX5_FLOW_DESTINATION_TYPE_UPLINK ||
502 		type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
503 		type == MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER ||
504 		type == MLX5_FLOW_DESTINATION_TYPE_TIR ||
505 		type == MLX5_FLOW_DESTINATION_TYPE_RANGE ||
506 		type == MLX5_FLOW_DESTINATION_TYPE_TABLE_TYPE;
507 }
508 
check_valid_spec(const struct mlx5_flow_spec * spec)509 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
510 {
511 	int i;
512 
513 	for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
514 		if (spec->match_value[i] & ~spec->match_criteria[i]) {
515 			pr_warn("mlx5_core: match_value differs from match_criteria\n");
516 			return false;
517 		}
518 
519 	return true;
520 }
521 
find_root(struct fs_node * node)522 struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
523 {
524 	struct fs_node *root;
525 	struct mlx5_flow_namespace *ns;
526 
527 	root = node->root;
528 
529 	if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
530 		pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
531 		return NULL;
532 	}
533 
534 	ns = container_of(root, struct mlx5_flow_namespace, node);
535 	return container_of(ns, struct mlx5_flow_root_namespace, ns);
536 }
537 
get_steering(struct fs_node * node)538 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
539 {
540 	struct mlx5_flow_root_namespace *root = find_root(node);
541 
542 	if (root)
543 		return root->dev->priv.steering;
544 	return NULL;
545 }
546 
get_dev(struct fs_node * node)547 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
548 {
549 	struct mlx5_flow_root_namespace *root = find_root(node);
550 
551 	if (root)
552 		return root->dev;
553 	return NULL;
554 }
555 
del_sw_ns(struct fs_node * node)556 static void del_sw_ns(struct fs_node *node)
557 {
558 	kfree(node);
559 }
560 
del_sw_prio(struct fs_node * node)561 static void del_sw_prio(struct fs_node *node)
562 {
563 	kfree(node);
564 }
565 
del_hw_flow_table(struct fs_node * node)566 static void del_hw_flow_table(struct fs_node *node)
567 {
568 	struct mlx5_flow_root_namespace *root;
569 	struct mlx5_flow_table *ft;
570 	struct mlx5_core_dev *dev;
571 	int err;
572 
573 	fs_get_obj(ft, node);
574 	dev = get_dev(&ft->node);
575 	root = find_root(&ft->node);
576 	trace_mlx5_fs_del_ft(ft);
577 
578 	if (node->active) {
579 		err = root->cmds->destroy_flow_table(root, ft);
580 		if (err)
581 			mlx5_core_warn(dev, "flow steering can't destroy ft\n");
582 	}
583 }
584 
del_sw_flow_table(struct fs_node * node)585 static void del_sw_flow_table(struct fs_node *node)
586 {
587 	struct mlx5_flow_table *ft;
588 	struct fs_prio *prio;
589 
590 	fs_get_obj(ft, node);
591 
592 	rhltable_destroy(&ft->fgs_hash);
593 	if (ft->node.parent) {
594 		fs_get_obj(prio, ft->node.parent);
595 		prio->num_ft--;
596 	}
597 	kfree(ft);
598 }
599 
modify_fte(struct fs_fte * fte)600 static void modify_fte(struct fs_fte *fte)
601 {
602 	struct mlx5_flow_root_namespace *root;
603 	struct mlx5_flow_table *ft;
604 	struct mlx5_flow_group *fg;
605 	struct mlx5_core_dev *dev;
606 	int err;
607 
608 	fs_get_obj(fg, fte->node.parent);
609 	fs_get_obj(ft, fg->node.parent);
610 	dev = get_dev(&fte->node);
611 
612 	root = find_root(&ft->node);
613 	err = root->cmds->update_fte(root, ft, fg, fte->act_dests.modify_mask, fte);
614 	if (err)
615 		mlx5_core_warn(dev,
616 			       "%s can't del rule fg id=%d fte_index=%d\n",
617 			       __func__, fg->id, fte->index);
618 	fte->act_dests.modify_mask = 0;
619 }
620 
del_sw_hw_dup_rule(struct fs_node * node)621 static void del_sw_hw_dup_rule(struct fs_node *node)
622 {
623 	struct mlx5_flow_rule *rule;
624 	struct fs_fte *fte;
625 
626 	fs_get_obj(rule, node);
627 	fs_get_obj(fte, rule->node.parent);
628 	trace_mlx5_fs_del_rule(rule);
629 
630 	if (is_fwd_next_action(rule->sw_action)) {
631 		mutex_lock(&rule->dest_attr.ft->lock);
632 		list_del(&rule->next_ft);
633 		mutex_unlock(&rule->dest_attr.ft->lock);
634 	}
635 
636 	/* If a pending rule is being deleted it means
637 	 * this is a NO APPEND rule, so there are no partial deletions,
638 	 * all the rules of the mlx5_flow_handle are going to be deleted
639 	 * and the rules aren't shared with any other mlx5_flow_handle instance
640 	 * so no need to do any bookkeeping like in del_sw_hw_rule().
641 	 */
642 
643 	kfree(rule);
644 }
645 
del_sw_hw_rule(struct fs_node * node)646 static void del_sw_hw_rule(struct fs_node *node)
647 {
648 	struct mlx5_flow_rule *rule;
649 	struct fs_fte *fte;
650 
651 	fs_get_obj(rule, node);
652 	fs_get_obj(fte, rule->node.parent);
653 	trace_mlx5_fs_del_rule(rule);
654 	if (is_fwd_next_action(rule->sw_action)) {
655 		mutex_lock(&rule->dest_attr.ft->lock);
656 		list_del(&rule->next_ft);
657 		mutex_unlock(&rule->dest_attr.ft->lock);
658 	}
659 
660 	if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER) {
661 		--fte->act_dests.dests_size;
662 		fte->act_dests.modify_mask |=
663 			BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
664 			BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
665 		fte->act_dests.action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
666 		mlx5_fc_local_put(rule->dest_attr.counter);
667 		goto out;
668 	}
669 
670 	if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_PORT) {
671 		--fte->act_dests.dests_size;
672 		fte->act_dests.modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
673 		fte->act_dests.action.action &= ~MLX5_FLOW_CONTEXT_ACTION_ALLOW;
674 		goto out;
675 	}
676 
677 	if (is_fwd_dest_type(rule->dest_attr.type)) {
678 		--fte->act_dests.dests_size;
679 		--fte->act_dests.fwd_dests;
680 
681 		if (!fte->act_dests.fwd_dests)
682 			fte->act_dests.action.action &=
683 				~MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
684 		fte->act_dests.modify_mask |=
685 			BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
686 		goto out;
687 	}
688 out:
689 	kfree(rule);
690 }
691 
switch_to_pending_act_dests(struct fs_fte * fte)692 static void switch_to_pending_act_dests(struct fs_fte *fte)
693 {
694 	struct fs_node *iter;
695 
696 	memcpy(&fte->act_dests, &fte->dup->act_dests, sizeof(fte->act_dests));
697 
698 	list_bulk_move_tail(&fte->node.children,
699 			    fte->dup->children.next,
700 			    fte->dup->children.prev);
701 
702 	list_for_each_entry(iter, &fte->node.children, list)
703 		iter->del_sw_func = del_sw_hw_rule;
704 
705 	/* Make sure the fte isn't deleted
706 	 * as mlx5_del_flow_rules() decreases the refcount
707 	 * of the fte to trigger deletion.
708 	 */
709 	tree_get_node(&fte->node);
710 }
711 
del_hw_fte(struct fs_node * node)712 static void del_hw_fte(struct fs_node *node)
713 {
714 	struct mlx5_flow_root_namespace *root;
715 	struct mlx5_flow_table *ft;
716 	struct mlx5_flow_group *fg;
717 	struct mlx5_core_dev *dev;
718 	bool pending_used = false;
719 	struct fs_fte *fte;
720 	int err;
721 
722 	fs_get_obj(fte, node);
723 	fs_get_obj(fg, fte->node.parent);
724 	fs_get_obj(ft, fg->node.parent);
725 
726 	trace_mlx5_fs_del_fte(fte);
727 	WARN_ON(fte->act_dests.dests_size);
728 	dev = get_dev(&ft->node);
729 	root = find_root(&ft->node);
730 
731 	if (fte->dup && !list_empty(&fte->dup->children)) {
732 		switch_to_pending_act_dests(fte);
733 		pending_used = true;
734 	} else {
735 		/* Avoid double call to del_hw_fte */
736 		node->del_hw_func = NULL;
737 	}
738 
739 	if (node->active) {
740 		if (pending_used) {
741 			err = root->cmds->update_fte(root, ft, fg,
742 						     fte->act_dests.modify_mask, fte);
743 			if (err)
744 				mlx5_core_warn(dev,
745 					       "flow steering can't update to pending rule in index %d of flow group id %d\n",
746 					       fte->index, fg->id);
747 			fte->act_dests.modify_mask = 0;
748 		} else {
749 			err = root->cmds->delete_fte(root, ft, fte);
750 			if (err)
751 				mlx5_core_warn(dev,
752 					       "flow steering can't delete fte in index %d of flow group id %d\n",
753 					       fte->index, fg->id);
754 			node->active = false;
755 		}
756 	}
757 }
758 
del_sw_fte(struct fs_node * node)759 static void del_sw_fte(struct fs_node *node)
760 {
761 	struct mlx5_flow_steering *steering = get_steering(node);
762 	struct mlx5_flow_group *fg;
763 	struct fs_fte *fte;
764 	int err;
765 
766 	fs_get_obj(fte, node);
767 	fs_get_obj(fg, fte->node.parent);
768 
769 	err = rhashtable_remove_fast(&fg->ftes_hash,
770 				     &fte->hash,
771 				     rhash_fte);
772 	WARN_ON(err);
773 	ida_free(&fg->fte_allocator, fte->index - fg->start_index);
774 	kvfree(fte->dup);
775 	kmem_cache_free(steering->ftes_cache, fte);
776 }
777 
del_hw_flow_group(struct fs_node * node)778 static void del_hw_flow_group(struct fs_node *node)
779 {
780 	struct mlx5_flow_root_namespace *root;
781 	struct mlx5_flow_group *fg;
782 	struct mlx5_flow_table *ft;
783 	struct mlx5_core_dev *dev;
784 
785 	fs_get_obj(fg, node);
786 	fs_get_obj(ft, fg->node.parent);
787 	dev = get_dev(&ft->node);
788 	trace_mlx5_fs_del_fg(fg);
789 
790 	root = find_root(&ft->node);
791 	if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
792 		mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
793 			       fg->id, ft->id);
794 }
795 
del_sw_flow_group(struct fs_node * node)796 static void del_sw_flow_group(struct fs_node *node)
797 {
798 	struct mlx5_flow_steering *steering = get_steering(node);
799 	struct mlx5_flow_group *fg;
800 	struct mlx5_flow_table *ft;
801 	int err;
802 
803 	fs_get_obj(fg, node);
804 	fs_get_obj(ft, fg->node.parent);
805 
806 	rhashtable_destroy(&fg->ftes_hash);
807 	ida_destroy(&fg->fte_allocator);
808 	if (ft->autogroup.active &&
809 	    fg->max_ftes == ft->autogroup.group_size &&
810 	    fg->start_index < ft->autogroup.max_fte)
811 		ft->autogroup.num_groups--;
812 	err = rhltable_remove(&ft->fgs_hash,
813 			      &fg->hash,
814 			      rhash_fg);
815 	WARN_ON(err);
816 	kmem_cache_free(steering->fgs_cache, fg);
817 }
818 
insert_fte(struct mlx5_flow_group * fg,struct fs_fte * fte)819 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
820 {
821 	int index;
822 	int ret;
823 
824 	index = ida_alloc_max(&fg->fte_allocator, fg->max_ftes - 1, GFP_KERNEL);
825 	if (index < 0)
826 		return index;
827 
828 	fte->index = index + fg->start_index;
829 retry_insert:
830 	ret = rhashtable_insert_fast(&fg->ftes_hash,
831 				     &fte->hash,
832 				     rhash_fte);
833 	if (ret) {
834 		if (ret == -EBUSY) {
835 			cond_resched();
836 			goto retry_insert;
837 		}
838 		goto err_ida_remove;
839 	}
840 
841 	tree_add_node(&fte->node, &fg->node);
842 	list_add_tail(&fte->node.list, &fg->node.children);
843 	return 0;
844 
845 err_ida_remove:
846 	ida_free(&fg->fte_allocator, index);
847 	return ret;
848 }
849 
alloc_fte(struct mlx5_flow_table * ft,const struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act)850 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
851 				const struct mlx5_flow_spec *spec,
852 				struct mlx5_flow_act *flow_act)
853 {
854 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
855 	struct fs_fte *fte;
856 
857 	fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
858 	if (!fte)
859 		return ERR_PTR(-ENOMEM);
860 
861 	memcpy(fte->val, &spec->match_value, sizeof(fte->val));
862 	fte->node.type =  FS_TYPE_FLOW_ENTRY;
863 	fte->act_dests.action = *flow_act;
864 	fte->act_dests.flow_context = spec->flow_context;
865 
866 	tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
867 
868 	return fte;
869 }
870 
dealloc_flow_group(struct mlx5_flow_steering * steering,struct mlx5_flow_group * fg)871 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
872 			       struct mlx5_flow_group *fg)
873 {
874 	rhashtable_destroy(&fg->ftes_hash);
875 	kmem_cache_free(steering->fgs_cache, fg);
876 }
877 
alloc_flow_group(struct mlx5_flow_steering * steering,u8 match_criteria_enable,const void * match_criteria,int start_index,int end_index)878 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
879 						u8 match_criteria_enable,
880 						const void *match_criteria,
881 						int start_index,
882 						int end_index)
883 {
884 	struct mlx5_flow_group *fg;
885 	int ret;
886 
887 	fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
888 	if (!fg)
889 		return ERR_PTR(-ENOMEM);
890 
891 	ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
892 	if (ret) {
893 		kmem_cache_free(steering->fgs_cache, fg);
894 		return ERR_PTR(ret);
895 	}
896 
897 	ida_init(&fg->fte_allocator);
898 	fg->mask.match_criteria_enable = match_criteria_enable;
899 	memcpy(&fg->mask.match_criteria, match_criteria,
900 	       sizeof(fg->mask.match_criteria));
901 	fg->node.type =  FS_TYPE_FLOW_GROUP;
902 	fg->start_index = start_index;
903 	fg->max_ftes = end_index - start_index + 1;
904 
905 	return fg;
906 }
907 
alloc_insert_flow_group(struct mlx5_flow_table * ft,u8 match_criteria_enable,const void * match_criteria,int start_index,int end_index,struct list_head * prev)908 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
909 						       u8 match_criteria_enable,
910 						       const void *match_criteria,
911 						       int start_index,
912 						       int end_index,
913 						       struct list_head *prev)
914 {
915 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
916 	struct mlx5_flow_group *fg;
917 	int ret;
918 
919 	fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
920 			      start_index, end_index);
921 	if (IS_ERR(fg))
922 		return fg;
923 
924 	/* initialize refcnt, add to parent list */
925 	ret = rhltable_insert(&ft->fgs_hash,
926 			      &fg->hash,
927 			      rhash_fg);
928 	if (ret) {
929 		dealloc_flow_group(steering, fg);
930 		return ERR_PTR(ret);
931 	}
932 
933 	tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
934 	tree_add_node(&fg->node, &ft->node);
935 	/* Add node to group list */
936 	list_add(&fg->node.list, prev);
937 	atomic_inc(&ft->node.version);
938 
939 	return fg;
940 }
941 
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_vport_acl_root_ns {
2797 	u16 vport_idx;
2798 	struct mlx5_flow_root_namespace *root_ns;
2799 };
2800 
2801 struct mlx5_flow_namespace *
mlx5_get_flow_vport_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type,int vport_idx)2802 mlx5_get_flow_vport_namespace(struct mlx5_core_dev *dev,
2803 			      enum mlx5_flow_namespace_type type, int vport_idx)
2804 {
2805 	struct mlx5_flow_steering *steering = dev->priv.steering;
2806 	struct mlx5_vport_acl_root_ns *vport_ns;
2807 
2808 	if (!steering)
2809 		return NULL;
2810 
2811 	switch (type) {
2812 	case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2813 		vport_ns = xa_load(&steering->esw_egress_root_ns, vport_idx);
2814 		if (vport_ns)
2815 			return &vport_ns->root_ns->ns;
2816 		else
2817 			return NULL;
2818 	case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2819 		vport_ns = xa_load(&steering->esw_ingress_root_ns, vport_idx);
2820 		if (vport_ns)
2821 			return &vport_ns->root_ns->ns;
2822 		else
2823 			return NULL;
2824 	case MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_RX:
2825 		if (vport_idx >= steering->rdma_transport_rx_vports)
2826 			return NULL;
2827 		if (steering->rdma_transport_rx_root_ns &&
2828 		    steering->rdma_transport_rx_root_ns[vport_idx])
2829 			return &steering->rdma_transport_rx_root_ns[vport_idx]->ns;
2830 		else
2831 			return NULL;
2832 	case MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_TX:
2833 		if (vport_idx >= steering->rdma_transport_tx_vports)
2834 			return NULL;
2835 
2836 		if (steering->rdma_transport_tx_root_ns &&
2837 		    steering->rdma_transport_tx_root_ns[vport_idx])
2838 			return &steering->rdma_transport_tx_root_ns[vport_idx]->ns;
2839 		else
2840 			return NULL;
2841 	default:
2842 		return NULL;
2843 	}
2844 }
2845 EXPORT_SYMBOL(mlx5_get_flow_vport_namespace);
2846 
_fs_create_prio(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels,enum fs_node_type type)2847 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2848 				       unsigned int prio,
2849 				       int num_levels,
2850 				       enum fs_node_type type)
2851 {
2852 	struct fs_prio *fs_prio;
2853 
2854 	fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2855 	if (!fs_prio)
2856 		return ERR_PTR(-ENOMEM);
2857 
2858 	fs_prio->node.type = type;
2859 	tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2860 	tree_add_node(&fs_prio->node, &ns->node);
2861 	fs_prio->num_levels = num_levels;
2862 	fs_prio->prio = prio;
2863 	list_add_tail(&fs_prio->node.list, &ns->node.children);
2864 
2865 	return fs_prio;
2866 }
2867 
fs_create_prio_chained(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels)2868 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2869 					      unsigned int prio,
2870 					      int num_levels)
2871 {
2872 	return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2873 }
2874 
fs_create_prio(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels)2875 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2876 				      unsigned int prio, int num_levels)
2877 {
2878 	return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2879 }
2880 
fs_init_namespace(struct mlx5_flow_namespace * ns)2881 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2882 						     *ns)
2883 {
2884 	ns->node.type = FS_TYPE_NAMESPACE;
2885 
2886 	return ns;
2887 }
2888 
fs_create_namespace(struct fs_prio * prio,int def_miss_act)2889 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2890 						       int def_miss_act)
2891 {
2892 	struct mlx5_flow_namespace	*ns;
2893 
2894 	ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2895 	if (!ns)
2896 		return ERR_PTR(-ENOMEM);
2897 
2898 	fs_init_namespace(ns);
2899 	ns->def_miss_action = def_miss_act;
2900 	tree_init_node(&ns->node, NULL, del_sw_ns);
2901 	tree_add_node(&ns->node, &prio->node);
2902 	list_add_tail(&ns->node.list, &prio->node.children);
2903 
2904 	return ns;
2905 }
2906 
create_leaf_prios(struct mlx5_flow_namespace * ns,int prio,struct init_tree_node * prio_metadata)2907 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2908 			     struct init_tree_node *prio_metadata)
2909 {
2910 	struct fs_prio *fs_prio;
2911 	int i;
2912 
2913 	for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2914 		fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2915 		if (IS_ERR(fs_prio))
2916 			return PTR_ERR(fs_prio);
2917 	}
2918 	return 0;
2919 }
2920 
2921 #define FLOW_TABLE_BIT_SZ 1
2922 #define GET_FLOW_TABLE_CAP(dev, offset) \
2923 	((be32_to_cpu(*((__be32 *)(dev->caps.hca[MLX5_CAP_FLOW_TABLE]->cur) +	\
2924 			offset / 32)) >>					\
2925 	  (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
has_required_caps(struct mlx5_core_dev * dev,struct node_caps * caps)2926 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2927 {
2928 	int i;
2929 
2930 	for (i = 0; i < caps->arr_sz; i++) {
2931 		if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2932 			return false;
2933 	}
2934 	return true;
2935 }
2936 
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)2937 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2938 				    struct init_tree_node *init_node,
2939 				    struct fs_node *fs_parent_node,
2940 				    struct init_tree_node *init_parent_node,
2941 				    int prio)
2942 {
2943 	int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2944 					      flow_table_properties_nic_receive.
2945 					      max_ft_level);
2946 	struct mlx5_flow_namespace *fs_ns;
2947 	struct fs_prio *fs_prio;
2948 	struct fs_node *base;
2949 	int i;
2950 	int err;
2951 
2952 	if (init_node->type == FS_TYPE_PRIO) {
2953 		if ((init_node->min_ft_level > max_ft_level) ||
2954 		    !has_required_caps(steering->dev, &init_node->caps))
2955 			return 0;
2956 
2957 		fs_get_obj(fs_ns, fs_parent_node);
2958 		if (init_node->num_leaf_prios)
2959 			return create_leaf_prios(fs_ns, prio, init_node);
2960 		fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2961 		if (IS_ERR(fs_prio))
2962 			return PTR_ERR(fs_prio);
2963 		base = &fs_prio->node;
2964 	} else if (init_node->type == FS_TYPE_NAMESPACE) {
2965 		fs_get_obj(fs_prio, fs_parent_node);
2966 		fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
2967 		if (IS_ERR(fs_ns))
2968 			return PTR_ERR(fs_ns);
2969 		base = &fs_ns->node;
2970 	} else {
2971 		return -EINVAL;
2972 	}
2973 	prio = 0;
2974 	for (i = 0; i < init_node->ar_size; i++) {
2975 		err = init_root_tree_recursive(steering, &init_node->children[i],
2976 					       base, init_node, prio);
2977 		if (err)
2978 			return err;
2979 		if (init_node->children[i].type == FS_TYPE_PRIO &&
2980 		    init_node->children[i].num_leaf_prios) {
2981 			prio += init_node->children[i].num_leaf_prios;
2982 		}
2983 	}
2984 
2985 	return 0;
2986 }
2987 
init_root_tree(struct mlx5_flow_steering * steering,struct init_tree_node * init_node,struct fs_node * fs_parent_node)2988 static int init_root_tree(struct mlx5_flow_steering *steering,
2989 			  struct init_tree_node *init_node,
2990 			  struct fs_node *fs_parent_node)
2991 {
2992 	int err;
2993 	int i;
2994 
2995 	for (i = 0; i < init_node->ar_size; i++) {
2996 		err = init_root_tree_recursive(steering, &init_node->children[i],
2997 					       fs_parent_node,
2998 					       init_node, i);
2999 		if (err)
3000 			return err;
3001 	}
3002 	return 0;
3003 }
3004 
del_sw_root_ns(struct fs_node * node)3005 static void del_sw_root_ns(struct fs_node *node)
3006 {
3007 	struct mlx5_flow_root_namespace *root_ns;
3008 	struct mlx5_flow_namespace *ns;
3009 
3010 	fs_get_obj(ns, node);
3011 	root_ns = container_of(ns, struct mlx5_flow_root_namespace, ns);
3012 	mutex_destroy(&root_ns->chain_lock);
3013 	kfree(node);
3014 }
3015 
3016 static struct mlx5_flow_root_namespace
create_root_ns(struct mlx5_flow_steering * steering,enum fs_flow_table_type table_type)3017 *create_root_ns(struct mlx5_flow_steering *steering,
3018 		enum fs_flow_table_type table_type)
3019 {
3020 	const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
3021 	struct mlx5_flow_root_namespace *root_ns;
3022 	struct mlx5_flow_namespace *ns;
3023 
3024 	/* Create the root namespace */
3025 	root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL);
3026 	if (!root_ns)
3027 		return NULL;
3028 
3029 	root_ns->dev = steering->dev;
3030 	root_ns->table_type = table_type;
3031 	root_ns->cmds = cmds;
3032 
3033 	INIT_LIST_HEAD(&root_ns->underlay_qpns);
3034 
3035 	ns = &root_ns->ns;
3036 	fs_init_namespace(ns);
3037 	mutex_init(&root_ns->chain_lock);
3038 	tree_init_node(&ns->node, NULL, del_sw_root_ns);
3039 	tree_add_node(&ns->node, NULL);
3040 
3041 	return root_ns;
3042 }
3043 
3044 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
3045 
set_prio_attrs_in_ns(struct mlx5_flow_namespace * ns,int acc_level)3046 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
3047 {
3048 	struct fs_prio *prio;
3049 
3050 	fs_for_each_prio(prio, ns) {
3051 		 /* This updates prio start_level and num_levels */
3052 		set_prio_attrs_in_prio(prio, acc_level);
3053 		acc_level += prio->num_levels;
3054 	}
3055 	return acc_level;
3056 }
3057 
set_prio_attrs_in_prio(struct fs_prio * prio,int acc_level)3058 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
3059 {
3060 	struct mlx5_flow_namespace *ns;
3061 	int acc_level_ns = acc_level;
3062 
3063 	prio->start_level = acc_level;
3064 	fs_for_each_ns(ns, prio) {
3065 		/* This updates start_level and num_levels of ns's priority descendants */
3066 		acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
3067 
3068 		/* If this a prio with chains, and we can jump from one chain
3069 		 * (namespace) to another, so we accumulate the levels
3070 		 */
3071 		if (prio->node.type == FS_TYPE_PRIO_CHAINS)
3072 			acc_level = acc_level_ns;
3073 	}
3074 
3075 	if (!prio->num_levels)
3076 		prio->num_levels = acc_level_ns - prio->start_level;
3077 	WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
3078 }
3079 
set_prio_attrs(struct mlx5_flow_root_namespace * root_ns)3080 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
3081 {
3082 	struct mlx5_flow_namespace *ns = &root_ns->ns;
3083 	struct fs_prio *prio;
3084 	int start_level = 0;
3085 
3086 	fs_for_each_prio(prio, ns) {
3087 		set_prio_attrs_in_prio(prio, start_level);
3088 		start_level += prio->num_levels;
3089 	}
3090 }
3091 
3092 #define ANCHOR_PRIO 0
3093 #define ANCHOR_SIZE 1
3094 #define ANCHOR_LEVEL 0
create_anchor_flow_table(struct mlx5_flow_steering * steering)3095 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
3096 {
3097 	struct mlx5_flow_namespace *ns = NULL;
3098 	struct mlx5_flow_table_attr ft_attr = {};
3099 	struct mlx5_flow_table *ft;
3100 
3101 	ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
3102 	if (WARN_ON(!ns))
3103 		return -EINVAL;
3104 
3105 	ft_attr.max_fte = ANCHOR_SIZE;
3106 	ft_attr.level   = ANCHOR_LEVEL;
3107 	ft_attr.prio    = ANCHOR_PRIO;
3108 
3109 	ft = mlx5_create_flow_table(ns, &ft_attr);
3110 	if (IS_ERR(ft)) {
3111 		mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
3112 		return PTR_ERR(ft);
3113 	}
3114 	return 0;
3115 }
3116 
init_root_ns(struct mlx5_flow_steering * steering)3117 static int init_root_ns(struct mlx5_flow_steering *steering)
3118 {
3119 	int err;
3120 
3121 	steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
3122 	if (!steering->root_ns)
3123 		return -ENOMEM;
3124 
3125 	err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
3126 	if (err)
3127 		goto out_err;
3128 
3129 	set_prio_attrs(steering->root_ns);
3130 	err = create_anchor_flow_table(steering);
3131 	if (err)
3132 		goto out_err;
3133 
3134 	return 0;
3135 
3136 out_err:
3137 	cleanup_root_ns(steering->root_ns);
3138 	steering->root_ns = NULL;
3139 	return err;
3140 }
3141 
clean_tree(struct fs_node * node)3142 static void clean_tree(struct fs_node *node)
3143 {
3144 	if (node) {
3145 		struct fs_node *iter;
3146 		struct fs_node *temp;
3147 
3148 		tree_get_node(node);
3149 		list_for_each_entry_safe(iter, temp, &node->children, list)
3150 			clean_tree(iter);
3151 		tree_put_node(node, false);
3152 		tree_remove_node(node, false);
3153 	}
3154 }
3155 
cleanup_root_ns(struct mlx5_flow_root_namespace * root_ns)3156 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
3157 {
3158 	if (!root_ns)
3159 		return;
3160 
3161 	clean_tree(&root_ns->ns.node);
3162 }
3163 
init_sniffer_tx_root_ns(struct mlx5_flow_steering * steering)3164 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
3165 {
3166 	struct fs_prio *prio;
3167 
3168 	steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
3169 	if (!steering->sniffer_tx_root_ns)
3170 		return -ENOMEM;
3171 
3172 	/* Create single prio */
3173 	prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
3174 	return PTR_ERR_OR_ZERO(prio);
3175 }
3176 
init_sniffer_rx_root_ns(struct mlx5_flow_steering * steering)3177 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
3178 {
3179 	struct fs_prio *prio;
3180 
3181 	steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
3182 	if (!steering->sniffer_rx_root_ns)
3183 		return -ENOMEM;
3184 
3185 	/* Create single prio */
3186 	prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
3187 	return PTR_ERR_OR_ZERO(prio);
3188 }
3189 
3190 #define PORT_SEL_NUM_LEVELS 3
init_port_sel_root_ns(struct mlx5_flow_steering * steering)3191 static int init_port_sel_root_ns(struct mlx5_flow_steering *steering)
3192 {
3193 	struct fs_prio *prio;
3194 
3195 	steering->port_sel_root_ns = create_root_ns(steering, FS_FT_PORT_SEL);
3196 	if (!steering->port_sel_root_ns)
3197 		return -ENOMEM;
3198 
3199 	/* Create single prio */
3200 	prio = fs_create_prio(&steering->port_sel_root_ns->ns, 0,
3201 			      PORT_SEL_NUM_LEVELS);
3202 	return PTR_ERR_OR_ZERO(prio);
3203 }
3204 
init_rdma_rx_root_ns(struct mlx5_flow_steering * steering)3205 static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
3206 {
3207 	int err;
3208 
3209 	steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
3210 	if (!steering->rdma_rx_root_ns)
3211 		return -ENOMEM;
3212 
3213 	err = init_root_tree(steering, &rdma_rx_root_fs,
3214 			     &steering->rdma_rx_root_ns->ns.node);
3215 	if (err)
3216 		goto out_err;
3217 
3218 	set_prio_attrs(steering->rdma_rx_root_ns);
3219 
3220 	return 0;
3221 
3222 out_err:
3223 	cleanup_root_ns(steering->rdma_rx_root_ns);
3224 	steering->rdma_rx_root_ns = NULL;
3225 	return err;
3226 }
3227 
init_rdma_tx_root_ns(struct mlx5_flow_steering * steering)3228 static int init_rdma_tx_root_ns(struct mlx5_flow_steering *steering)
3229 {
3230 	int err;
3231 
3232 	steering->rdma_tx_root_ns = create_root_ns(steering, FS_FT_RDMA_TX);
3233 	if (!steering->rdma_tx_root_ns)
3234 		return -ENOMEM;
3235 
3236 	err = init_root_tree(steering, &rdma_tx_root_fs,
3237 			     &steering->rdma_tx_root_ns->ns.node);
3238 	if (err)
3239 		goto out_err;
3240 
3241 	set_prio_attrs(steering->rdma_tx_root_ns);
3242 
3243 	return 0;
3244 
3245 out_err:
3246 	cleanup_root_ns(steering->rdma_tx_root_ns);
3247 	steering->rdma_tx_root_ns = NULL;
3248 	return err;
3249 }
3250 
3251 static int
init_rdma_transport_rx_root_ns_one(struct mlx5_flow_steering * steering,int vport_idx)3252 init_rdma_transport_rx_root_ns_one(struct mlx5_flow_steering *steering,
3253 				   int vport_idx)
3254 {
3255 	struct mlx5_flow_root_namespace *root_ns;
3256 	struct fs_prio *prio;
3257 	int ret;
3258 	int i;
3259 
3260 	steering->rdma_transport_rx_root_ns[vport_idx] =
3261 		create_root_ns(steering, FS_FT_RDMA_TRANSPORT_RX);
3262 	if (!steering->rdma_transport_rx_root_ns[vport_idx])
3263 		return -ENOMEM;
3264 
3265 	root_ns = steering->rdma_transport_rx_root_ns[vport_idx];
3266 
3267 	for (i = 0; i < MLX5_RDMA_TRANSPORT_BYPASS_PRIO; i++) {
3268 		prio = fs_create_prio(&root_ns->ns, i, 1);
3269 		if (IS_ERR(prio)) {
3270 			ret = PTR_ERR(prio);
3271 			goto err;
3272 		}
3273 	}
3274 	set_prio_attrs(root_ns);
3275 	return 0;
3276 
3277 err:
3278 	cleanup_root_ns(root_ns);
3279 	return ret;
3280 }
3281 
3282 static int
init_rdma_transport_tx_root_ns_one(struct mlx5_flow_steering * steering,int vport_idx)3283 init_rdma_transport_tx_root_ns_one(struct mlx5_flow_steering *steering,
3284 				   int vport_idx)
3285 {
3286 	struct mlx5_flow_root_namespace *root_ns;
3287 	struct fs_prio *prio;
3288 	int ret;
3289 	int i;
3290 
3291 	steering->rdma_transport_tx_root_ns[vport_idx] =
3292 		create_root_ns(steering, FS_FT_RDMA_TRANSPORT_TX);
3293 	if (!steering->rdma_transport_tx_root_ns[vport_idx])
3294 		return -ENOMEM;
3295 
3296 	root_ns = steering->rdma_transport_tx_root_ns[vport_idx];
3297 
3298 	for (i = 0; i < MLX5_RDMA_TRANSPORT_BYPASS_PRIO; i++) {
3299 		prio = fs_create_prio(&root_ns->ns, i, 1);
3300 		if (IS_ERR(prio)) {
3301 			ret = PTR_ERR(prio);
3302 			goto err;
3303 		}
3304 	}
3305 	set_prio_attrs(root_ns);
3306 	return 0;
3307 
3308 err:
3309 	cleanup_root_ns(root_ns);
3310 	return ret;
3311 }
3312 
init_rdma_transport_rx_root_ns(struct mlx5_flow_steering * steering)3313 static int init_rdma_transport_rx_root_ns(struct mlx5_flow_steering *steering)
3314 {
3315 	struct mlx5_core_dev *dev = steering->dev;
3316 	int total_vports;
3317 	int err;
3318 	int i;
3319 
3320 	/* In case eswitch not supported and working in legacy mode */
3321 	total_vports = mlx5_eswitch_get_total_vports(dev) ?: 1;
3322 
3323 	steering->rdma_transport_rx_root_ns =
3324 			kcalloc(total_vports,
3325 				sizeof(*steering->rdma_transport_rx_root_ns),
3326 				GFP_KERNEL);
3327 	if (!steering->rdma_transport_rx_root_ns)
3328 		return -ENOMEM;
3329 
3330 	for (i = 0; i < total_vports; i++) {
3331 		err = init_rdma_transport_rx_root_ns_one(steering, i);
3332 		if (err)
3333 			goto cleanup_root_ns;
3334 	}
3335 	steering->rdma_transport_rx_vports = total_vports;
3336 	return 0;
3337 
3338 cleanup_root_ns:
3339 	while (i--)
3340 		cleanup_root_ns(steering->rdma_transport_rx_root_ns[i]);
3341 	kfree(steering->rdma_transport_rx_root_ns);
3342 	steering->rdma_transport_rx_root_ns = NULL;
3343 	return err;
3344 }
3345 
init_rdma_transport_tx_root_ns(struct mlx5_flow_steering * steering)3346 static int init_rdma_transport_tx_root_ns(struct mlx5_flow_steering *steering)
3347 {
3348 	struct mlx5_core_dev *dev = steering->dev;
3349 	int total_vports;
3350 	int err;
3351 	int i;
3352 
3353 	/* In case eswitch not supported and working in legacy mode */
3354 	total_vports = mlx5_eswitch_get_total_vports(dev) ?: 1;
3355 
3356 	steering->rdma_transport_tx_root_ns =
3357 			kcalloc(total_vports,
3358 				sizeof(*steering->rdma_transport_tx_root_ns),
3359 				GFP_KERNEL);
3360 	if (!steering->rdma_transport_tx_root_ns)
3361 		return -ENOMEM;
3362 
3363 	for (i = 0; i < total_vports; i++) {
3364 		err = init_rdma_transport_tx_root_ns_one(steering, i);
3365 		if (err)
3366 			goto cleanup_root_ns;
3367 	}
3368 	steering->rdma_transport_tx_vports = total_vports;
3369 	return 0;
3370 
3371 cleanup_root_ns:
3372 	while (i--)
3373 		cleanup_root_ns(steering->rdma_transport_tx_root_ns[i]);
3374 	kfree(steering->rdma_transport_tx_root_ns);
3375 	steering->rdma_transport_tx_root_ns = NULL;
3376 	return err;
3377 }
3378 
cleanup_rdma_transport_roots_ns(struct mlx5_flow_steering * steering)3379 static void cleanup_rdma_transport_roots_ns(struct mlx5_flow_steering *steering)
3380 {
3381 	int i;
3382 
3383 	if (steering->rdma_transport_rx_root_ns) {
3384 		for (i = 0; i < steering->rdma_transport_rx_vports; i++)
3385 			cleanup_root_ns(steering->rdma_transport_rx_root_ns[i]);
3386 
3387 		kfree(steering->rdma_transport_rx_root_ns);
3388 		steering->rdma_transport_rx_root_ns = NULL;
3389 	}
3390 
3391 	if (steering->rdma_transport_tx_root_ns) {
3392 		for (i = 0; i < steering->rdma_transport_tx_vports; i++)
3393 			cleanup_root_ns(steering->rdma_transport_tx_root_ns[i]);
3394 
3395 		kfree(steering->rdma_transport_tx_root_ns);
3396 		steering->rdma_transport_tx_root_ns = NULL;
3397 	}
3398 }
3399 
3400 /* FT and tc chains are stored in the same array so we can re-use the
3401  * mlx5_get_fdb_sub_ns() and tc api for FT chains.
3402  * When creating a new ns for each chain store it in the first available slot.
3403  * Assume tc chains are created and stored first and only then the FT chain.
3404  */
store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering * steering,struct mlx5_flow_namespace * ns)3405 static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
3406 					struct mlx5_flow_namespace *ns)
3407 {
3408 	int chain = 0;
3409 
3410 	while (steering->fdb_sub_ns[chain])
3411 		++chain;
3412 
3413 	steering->fdb_sub_ns[chain] = ns;
3414 }
3415 
create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering * steering,struct fs_prio * maj_prio)3416 static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
3417 					struct fs_prio *maj_prio)
3418 {
3419 	struct mlx5_flow_namespace *ns;
3420 	struct fs_prio *min_prio;
3421 	int prio;
3422 
3423 	ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
3424 	if (IS_ERR(ns))
3425 		return PTR_ERR(ns);
3426 
3427 	for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
3428 		min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
3429 		if (IS_ERR(min_prio))
3430 			return PTR_ERR(min_prio);
3431 	}
3432 
3433 	store_fdb_sub_ns_prio_chain(steering, ns);
3434 
3435 	return 0;
3436 }
3437 
create_fdb_chains(struct mlx5_flow_steering * steering,int fs_prio,int chains)3438 static int create_fdb_chains(struct mlx5_flow_steering *steering,
3439 			     int fs_prio,
3440 			     int chains)
3441 {
3442 	struct fs_prio *maj_prio;
3443 	int levels;
3444 	int chain;
3445 	int err;
3446 
3447 	levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
3448 	maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
3449 					  fs_prio,
3450 					  levels);
3451 	if (IS_ERR(maj_prio))
3452 		return PTR_ERR(maj_prio);
3453 
3454 	for (chain = 0; chain < chains; chain++) {
3455 		err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
3456 		if (err)
3457 			return err;
3458 	}
3459 
3460 	return 0;
3461 }
3462 
create_fdb_fast_path(struct mlx5_flow_steering * steering)3463 static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
3464 {
3465 	int err;
3466 
3467 	steering->fdb_sub_ns = kcalloc(FDB_NUM_CHAINS,
3468 				       sizeof(*steering->fdb_sub_ns),
3469 				       GFP_KERNEL);
3470 	if (!steering->fdb_sub_ns)
3471 		return -ENOMEM;
3472 
3473 	err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
3474 	if (err)
3475 		return err;
3476 
3477 	err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
3478 	if (err)
3479 		return err;
3480 
3481 	return 0;
3482 }
3483 
create_fdb_bypass(struct mlx5_flow_steering * steering)3484 static int create_fdb_bypass(struct mlx5_flow_steering *steering)
3485 {
3486 	struct mlx5_flow_namespace *ns;
3487 	struct fs_prio *prio;
3488 	int i;
3489 
3490 	prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH, 0);
3491 	if (IS_ERR(prio))
3492 		return PTR_ERR(prio);
3493 
3494 	ns = fs_create_namespace(prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
3495 	if (IS_ERR(ns))
3496 		return PTR_ERR(ns);
3497 
3498 	for (i = 0; i < MLX5_BY_PASS_NUM_REGULAR_PRIOS; i++) {
3499 		prio = fs_create_prio(ns, i, 1);
3500 		if (IS_ERR(prio))
3501 			return PTR_ERR(prio);
3502 	}
3503 	return 0;
3504 }
3505 
cleanup_fdb_root_ns(struct mlx5_flow_steering * steering)3506 static void cleanup_fdb_root_ns(struct mlx5_flow_steering *steering)
3507 {
3508 	cleanup_root_ns(steering->fdb_root_ns);
3509 	steering->fdb_root_ns = NULL;
3510 	kfree(steering->fdb_sub_ns);
3511 	steering->fdb_sub_ns = NULL;
3512 }
3513 
init_fdb_root_ns(struct mlx5_flow_steering * steering)3514 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
3515 {
3516 	struct fs_prio *maj_prio;
3517 	int err;
3518 
3519 	steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
3520 	if (!steering->fdb_root_ns)
3521 		return -ENOMEM;
3522 
3523 	err = create_fdb_bypass(steering);
3524 	if (err)
3525 		goto out_err;
3526 
3527 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_INGRESS, 3);
3528 	if (IS_ERR(maj_prio)) {
3529 		err = PTR_ERR(maj_prio);
3530 		goto out_err;
3531 	}
3532 
3533 	err = create_fdb_fast_path(steering);
3534 	if (err)
3535 		goto out_err;
3536 
3537 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_TC_MISS, 1);
3538 	if (IS_ERR(maj_prio)) {
3539 		err = PTR_ERR(maj_prio);
3540 		goto out_err;
3541 	}
3542 
3543 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BR_OFFLOAD, 4);
3544 	if (IS_ERR(maj_prio)) {
3545 		err = PTR_ERR(maj_prio);
3546 		goto out_err;
3547 	}
3548 
3549 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
3550 	if (IS_ERR(maj_prio)) {
3551 		err = PTR_ERR(maj_prio);
3552 		goto out_err;
3553 	}
3554 
3555 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_EGRESS, 3);
3556 	if (IS_ERR(maj_prio)) {
3557 		err = PTR_ERR(maj_prio);
3558 		goto out_err;
3559 	}
3560 
3561 	/* We put this priority last, knowing that nothing will get here
3562 	 * unless explicitly forwarded to. This is possible because the
3563 	 * slow path tables have catch all rules and nothing gets passed
3564 	 * those tables.
3565 	 */
3566 	maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_PER_VPORT, 1);
3567 	if (IS_ERR(maj_prio)) {
3568 		err = PTR_ERR(maj_prio);
3569 		goto out_err;
3570 	}
3571 
3572 	set_prio_attrs(steering->fdb_root_ns);
3573 	return 0;
3574 
3575 out_err:
3576 	cleanup_fdb_root_ns(steering);
3577 	return err;
3578 }
3579 
3580 static void
mlx5_fs_remove_vport_acl_root_ns(struct xarray * esw_acl_root_ns,u16 vport_idx)3581 mlx5_fs_remove_vport_acl_root_ns(struct xarray *esw_acl_root_ns, u16 vport_idx)
3582 {
3583 	struct mlx5_vport_acl_root_ns *vport_ns;
3584 
3585 	vport_ns = xa_erase(esw_acl_root_ns, vport_idx);
3586 	if (vport_ns) {
3587 		cleanup_root_ns(vport_ns->root_ns);
3588 		kfree(vport_ns);
3589 	}
3590 }
3591 
3592 static int
mlx5_fs_add_vport_acl_root_ns(struct mlx5_flow_steering * steering,struct xarray * esw_acl_root_ns,enum fs_flow_table_type table_type,u16 vport_idx)3593 mlx5_fs_add_vport_acl_root_ns(struct mlx5_flow_steering *steering,
3594 			      struct xarray *esw_acl_root_ns,
3595 			      enum fs_flow_table_type table_type,
3596 			      u16 vport_idx)
3597 {
3598 	struct mlx5_vport_acl_root_ns *vport_ns;
3599 	struct fs_prio *prio;
3600 	int err;
3601 
3602 	/* sanity check, intended xarrays are used */
3603 	if (WARN_ON(esw_acl_root_ns != &steering->esw_egress_root_ns &&
3604 		    esw_acl_root_ns != &steering->esw_ingress_root_ns))
3605 		return -EINVAL;
3606 
3607 	if (table_type != FS_FT_ESW_EGRESS_ACL &&
3608 	    table_type != FS_FT_ESW_INGRESS_ACL) {
3609 		mlx5_core_err(steering->dev,
3610 			      "Invalid table type %d for egress/ingress ACLs\n",
3611 			      table_type);
3612 		return -EINVAL;
3613 	}
3614 
3615 	if (xa_load(esw_acl_root_ns, vport_idx))
3616 		return -EEXIST;
3617 
3618 	vport_ns = kzalloc(sizeof(*vport_ns), GFP_KERNEL);
3619 	if (!vport_ns)
3620 		return -ENOMEM;
3621 
3622 	vport_ns->root_ns = create_root_ns(steering, table_type);
3623 	if (!vport_ns->root_ns) {
3624 		err = -ENOMEM;
3625 		goto kfree_vport_ns;
3626 	}
3627 
3628 	/* create 1 prio*/
3629 	prio = fs_create_prio(&vport_ns->root_ns->ns, 0, 1);
3630 	if (IS_ERR(prio)) {
3631 		err = PTR_ERR(prio);
3632 		goto cleanup_root_ns;
3633 	}
3634 
3635 	vport_ns->vport_idx = vport_idx;
3636 	err = xa_insert(esw_acl_root_ns, vport_idx, vport_ns, GFP_KERNEL);
3637 	if (err)
3638 		goto cleanup_root_ns;
3639 	return 0;
3640 
3641 cleanup_root_ns:
3642 	cleanup_root_ns(vport_ns->root_ns);
3643 kfree_vport_ns:
3644 	kfree(vport_ns);
3645 	return err;
3646 }
3647 
mlx5_fs_vport_egress_acl_ns_add(struct mlx5_flow_steering * steering,u16 vport_idx)3648 int mlx5_fs_vport_egress_acl_ns_add(struct mlx5_flow_steering *steering,
3649 				    u16 vport_idx)
3650 {
3651 	return mlx5_fs_add_vport_acl_root_ns(steering,
3652 					     &steering->esw_egress_root_ns,
3653 					     FS_FT_ESW_EGRESS_ACL, vport_idx);
3654 }
3655 
mlx5_fs_vport_ingress_acl_ns_add(struct mlx5_flow_steering * steering,u16 vport_idx)3656 int mlx5_fs_vport_ingress_acl_ns_add(struct mlx5_flow_steering *steering,
3657 				     u16 vport_idx)
3658 {
3659 	return mlx5_fs_add_vport_acl_root_ns(steering,
3660 					     &steering->esw_ingress_root_ns,
3661 					     FS_FT_ESW_INGRESS_ACL, vport_idx);
3662 }
3663 
mlx5_fs_vport_egress_acl_ns_remove(struct mlx5_flow_steering * steering,int vport_idx)3664 void mlx5_fs_vport_egress_acl_ns_remove(struct mlx5_flow_steering *steering,
3665 					int vport_idx)
3666 {
3667 	mlx5_fs_remove_vport_acl_root_ns(&steering->esw_egress_root_ns,
3668 					 vport_idx);
3669 }
3670 
mlx5_fs_vport_ingress_acl_ns_remove(struct mlx5_flow_steering * steering,int vport_idx)3671 void mlx5_fs_vport_ingress_acl_ns_remove(struct mlx5_flow_steering *steering,
3672 					 int vport_idx)
3673 {
3674 	mlx5_fs_remove_vport_acl_root_ns(&steering->esw_ingress_root_ns,
3675 					 vport_idx);
3676 }
3677 
mlx5_fs_get_capabilities(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type)3678 u32 mlx5_fs_get_capabilities(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type type)
3679 {
3680 	struct mlx5_flow_root_namespace *root;
3681 	struct mlx5_flow_namespace *ns;
3682 
3683 	ns = mlx5_get_flow_namespace(dev, type);
3684 	if (!ns)
3685 		return 0;
3686 
3687 	root = find_root(&ns->node);
3688 	if (!root)
3689 		return 0;
3690 
3691 	return root->cmds->get_capabilities(root, root->table_type);
3692 }
3693 
init_egress_root_ns(struct mlx5_flow_steering * steering)3694 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
3695 {
3696 	int err;
3697 
3698 	steering->egress_root_ns = create_root_ns(steering,
3699 						  FS_FT_NIC_TX);
3700 	if (!steering->egress_root_ns)
3701 		return -ENOMEM;
3702 
3703 	err = init_root_tree(steering, &egress_root_fs,
3704 			     &steering->egress_root_ns->ns.node);
3705 	if (err)
3706 		goto cleanup;
3707 	set_prio_attrs(steering->egress_root_ns);
3708 	return 0;
3709 cleanup:
3710 	cleanup_root_ns(steering->egress_root_ns);
3711 	steering->egress_root_ns = NULL;
3712 	return err;
3713 }
3714 
mlx5_fs_mode_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)3715 static int mlx5_fs_mode_validate(struct devlink *devlink, u32 id,
3716 				 union devlink_param_value val,
3717 				 struct netlink_ext_ack *extack)
3718 {
3719 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3720 	char *value = val.vstr;
3721 	u8 eswitch_mode;
3722 
3723 	eswitch_mode = mlx5_eswitch_mode(dev);
3724 	if (eswitch_mode == MLX5_ESWITCH_OFFLOADS) {
3725 		NL_SET_ERR_MSG_FMT_MOD(extack,
3726 				       "Changing fs mode is not supported when eswitch offloads enabled.");
3727 		return -EOPNOTSUPP;
3728 	}
3729 
3730 	if (!strcmp(value, "dmfs"))
3731 		return 0;
3732 
3733 	if (!strcmp(value, "smfs")) {
3734 		bool smfs_cap = mlx5_fs_dr_is_supported(dev);
3735 
3736 		if (!smfs_cap) {
3737 			NL_SET_ERR_MSG_MOD(extack,
3738 					   "Software managed steering is not supported by current device");
3739 			return -EOPNOTSUPP;
3740 		}
3741 	} else if (!strcmp(value, "hmfs")) {
3742 		bool hmfs_cap = mlx5_fs_hws_is_supported(dev);
3743 
3744 		if (!hmfs_cap) {
3745 			NL_SET_ERR_MSG_MOD(extack,
3746 					   "Hardware steering is not supported by current device");
3747 			return -EOPNOTSUPP;
3748 		}
3749 	} else {
3750 		NL_SET_ERR_MSG_MOD(extack,
3751 				   "Bad parameter: supported values are [\"dmfs\", \"smfs\", \"hmfs\"]");
3752 		return -EINVAL;
3753 	}
3754 
3755 	return 0;
3756 }
3757 
mlx5_fs_mode_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx,struct netlink_ext_ack * extack)3758 static int mlx5_fs_mode_set(struct devlink *devlink, u32 id,
3759 			    struct devlink_param_gset_ctx *ctx,
3760 			    struct netlink_ext_ack *extack)
3761 {
3762 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3763 	enum mlx5_flow_steering_mode mode;
3764 
3765 	if (!strcmp(ctx->val.vstr, "smfs"))
3766 		mode = MLX5_FLOW_STEERING_MODE_SMFS;
3767 	else if (!strcmp(ctx->val.vstr, "hmfs"))
3768 		mode = MLX5_FLOW_STEERING_MODE_HMFS;
3769 	else
3770 		mode = MLX5_FLOW_STEERING_MODE_DMFS;
3771 	dev->priv.steering->mode = mode;
3772 
3773 	return 0;
3774 }
3775 
mlx5_fs_mode_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)3776 static int mlx5_fs_mode_get(struct devlink *devlink, u32 id,
3777 			    struct devlink_param_gset_ctx *ctx)
3778 {
3779 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3780 
3781 	switch (dev->priv.steering->mode) {
3782 	case MLX5_FLOW_STEERING_MODE_SMFS:
3783 		strscpy(ctx->val.vstr, "smfs", sizeof(ctx->val.vstr));
3784 		break;
3785 	case MLX5_FLOW_STEERING_MODE_HMFS:
3786 		strscpy(ctx->val.vstr, "hmfs", sizeof(ctx->val.vstr));
3787 		break;
3788 	default:
3789 		strscpy(ctx->val.vstr, "dmfs", sizeof(ctx->val.vstr));
3790 	}
3791 
3792 	return 0;
3793 }
3794 
3795 static const struct devlink_param mlx5_fs_params[] = {
3796 	DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE,
3797 			     "flow_steering_mode", DEVLINK_PARAM_TYPE_STRING,
3798 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
3799 			     mlx5_fs_mode_get, mlx5_fs_mode_set,
3800 			     mlx5_fs_mode_validate),
3801 };
3802 
mlx5_fs_core_cleanup(struct mlx5_core_dev * dev)3803 void mlx5_fs_core_cleanup(struct mlx5_core_dev *dev)
3804 {
3805 	struct mlx5_flow_steering *steering = dev->priv.steering;
3806 
3807 	WARN_ON(!xa_empty(&steering->esw_egress_root_ns));
3808 	WARN_ON(!xa_empty(&steering->esw_ingress_root_ns));
3809 	xa_destroy(&steering->esw_egress_root_ns);
3810 	xa_destroy(&steering->esw_ingress_root_ns);
3811 
3812 	cleanup_root_ns(steering->root_ns);
3813 	cleanup_fdb_root_ns(steering);
3814 	cleanup_root_ns(steering->port_sel_root_ns);
3815 	cleanup_root_ns(steering->sniffer_rx_root_ns);
3816 	cleanup_root_ns(steering->sniffer_tx_root_ns);
3817 	cleanup_root_ns(steering->rdma_rx_root_ns);
3818 	cleanup_root_ns(steering->rdma_tx_root_ns);
3819 	cleanup_root_ns(steering->egress_root_ns);
3820 	cleanup_rdma_transport_roots_ns(steering);
3821 
3822 	devl_params_unregister(priv_to_devlink(dev), mlx5_fs_params,
3823 			       ARRAY_SIZE(mlx5_fs_params));
3824 }
3825 
mlx5_fs_core_init(struct mlx5_core_dev * dev)3826 int mlx5_fs_core_init(struct mlx5_core_dev *dev)
3827 {
3828 	struct mlx5_flow_steering *steering = dev->priv.steering;
3829 	int err;
3830 
3831 	err = devl_params_register(priv_to_devlink(dev), mlx5_fs_params,
3832 				   ARRAY_SIZE(mlx5_fs_params));
3833 	if (err)
3834 		return err;
3835 
3836 	if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3837 	      (MLX5_CAP_GEN(dev, nic_flow_table))) ||
3838 	     ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
3839 	      MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
3840 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
3841 		err = init_root_ns(steering);
3842 		if (err)
3843 			goto err;
3844 	}
3845 
3846 	if (MLX5_ESWITCH_MANAGER(dev)) {
3847 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
3848 			err = init_fdb_root_ns(steering);
3849 			if (err)
3850 				goto err;
3851 		}
3852 	}
3853 
3854 	if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
3855 		err = init_sniffer_rx_root_ns(steering);
3856 		if (err)
3857 			goto err;
3858 	}
3859 
3860 	if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
3861 		err = init_sniffer_tx_root_ns(steering);
3862 		if (err)
3863 			goto err;
3864 	}
3865 
3866 	if (MLX5_CAP_FLOWTABLE_PORT_SELECTION(dev, ft_support)) {
3867 		err = init_port_sel_root_ns(steering);
3868 		if (err)
3869 			goto err;
3870 	}
3871 
3872 	if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support)) {
3873 		err = init_rdma_rx_root_ns(steering);
3874 		if (err)
3875 			goto err;
3876 	}
3877 
3878 	if (MLX5_CAP_FLOWTABLE_RDMA_TX(dev, ft_support)) {
3879 		err = init_rdma_tx_root_ns(steering);
3880 		if (err)
3881 			goto err;
3882 	}
3883 
3884 	if (MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
3885 		err = init_egress_root_ns(steering);
3886 		if (err)
3887 			goto err;
3888 	}
3889 
3890 	if (MLX5_CAP_FLOWTABLE_RDMA_TRANSPORT_RX(dev, ft_support)) {
3891 		err = init_rdma_transport_rx_root_ns(steering);
3892 		if (err)
3893 			goto err;
3894 	}
3895 
3896 	if (MLX5_CAP_FLOWTABLE_RDMA_TRANSPORT_TX(dev, ft_support)) {
3897 		err = init_rdma_transport_tx_root_ns(steering);
3898 		if (err)
3899 			goto err;
3900 	}
3901 
3902 	xa_init(&steering->esw_egress_root_ns);
3903 	xa_init(&steering->esw_ingress_root_ns);
3904 	return 0;
3905 
3906 err:
3907 	mlx5_fs_core_cleanup(dev);
3908 	return err;
3909 }
3910 
mlx5_fs_core_free(struct mlx5_core_dev * dev)3911 void mlx5_fs_core_free(struct mlx5_core_dev *dev)
3912 {
3913 	struct mlx5_flow_steering *steering = dev->priv.steering;
3914 
3915 	kmem_cache_destroy(steering->ftes_cache);
3916 	kmem_cache_destroy(steering->fgs_cache);
3917 	kfree(steering);
3918 	mlx5_ft_pool_destroy(dev);
3919 	mlx5_cleanup_fc_stats(dev);
3920 }
3921 
mlx5_fs_core_alloc(struct mlx5_core_dev * dev)3922 int mlx5_fs_core_alloc(struct mlx5_core_dev *dev)
3923 {
3924 	struct mlx5_flow_steering *steering;
3925 	char name[80];
3926 	int err = 0;
3927 
3928 	err = mlx5_init_fc_stats(dev);
3929 	if (err)
3930 		return err;
3931 
3932 	err = mlx5_ft_pool_init(dev);
3933 	if (err)
3934 		goto err;
3935 
3936 	steering = kzalloc(sizeof(*steering), GFP_KERNEL);
3937 	if (!steering) {
3938 		err = -ENOMEM;
3939 		goto err;
3940 	}
3941 
3942 	steering->dev = dev;
3943 	dev->priv.steering = steering;
3944 
3945 	if (mlx5_fs_dr_is_supported(dev))
3946 		steering->mode = MLX5_FLOW_STEERING_MODE_SMFS;
3947 	else if (mlx5_fs_hws_is_supported(dev))
3948 		steering->mode = MLX5_FLOW_STEERING_MODE_HMFS;
3949 	else
3950 		steering->mode = MLX5_FLOW_STEERING_MODE_DMFS;
3951 
3952 	snprintf(name, sizeof(name), "%s-mlx5_fs_fgs", dev_name(dev->device));
3953 	steering->fgs_cache = kmem_cache_create(name,
3954 						sizeof(struct mlx5_flow_group), 0,
3955 						0, NULL);
3956 	snprintf(name, sizeof(name), "%s-mlx5_fs_ftes", dev_name(dev->device));
3957 	steering->ftes_cache = kmem_cache_create(name, sizeof(struct fs_fte), 0,
3958 						 0, NULL);
3959 	if (!steering->ftes_cache || !steering->fgs_cache) {
3960 		err = -ENOMEM;
3961 		goto err;
3962 	}
3963 
3964 	return 0;
3965 
3966 err:
3967 	mlx5_fs_core_free(dev);
3968 	return err;
3969 }
3970 
mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)3971 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3972 {
3973 	struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3974 	struct mlx5_ft_underlay_qp *new_uqp;
3975 	int err = 0;
3976 
3977 	new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
3978 	if (!new_uqp)
3979 		return -ENOMEM;
3980 
3981 	mutex_lock(&root->chain_lock);
3982 
3983 	if (!root->root_ft) {
3984 		err = -EINVAL;
3985 		goto update_ft_fail;
3986 	}
3987 
3988 	err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3989 					 false);
3990 	if (err) {
3991 		mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
3992 			       underlay_qpn, err);
3993 		goto update_ft_fail;
3994 	}
3995 
3996 	new_uqp->qpn = underlay_qpn;
3997 	list_add_tail(&new_uqp->list, &root->underlay_qpns);
3998 
3999 	mutex_unlock(&root->chain_lock);
4000 
4001 	return 0;
4002 
4003 update_ft_fail:
4004 	mutex_unlock(&root->chain_lock);
4005 	kfree(new_uqp);
4006 	return err;
4007 }
4008 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
4009 
mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)4010 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
4011 {
4012 	struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
4013 	struct mlx5_ft_underlay_qp *uqp;
4014 	bool found = false;
4015 	int err = 0;
4016 
4017 	mutex_lock(&root->chain_lock);
4018 	list_for_each_entry(uqp, &root->underlay_qpns, list) {
4019 		if (uqp->qpn == underlay_qpn) {
4020 			found = true;
4021 			break;
4022 		}
4023 	}
4024 
4025 	if (!found) {
4026 		mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
4027 			       underlay_qpn);
4028 		err = -EINVAL;
4029 		goto out;
4030 	}
4031 
4032 	err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
4033 					 true);
4034 	if (err)
4035 		mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
4036 			       underlay_qpn, err);
4037 
4038 	list_del(&uqp->list);
4039 	mutex_unlock(&root->chain_lock);
4040 	kfree(uqp);
4041 
4042 	return 0;
4043 
4044 out:
4045 	mutex_unlock(&root->chain_lock);
4046 	return err;
4047 }
4048 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
4049 
4050 struct mlx5_flow_root_namespace *
mlx5_get_root_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type ns_type)4051 mlx5_get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
4052 {
4053 	struct mlx5_flow_namespace *ns;
4054 
4055 	if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
4056 	    ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS ||
4057 	    ns_type == MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_TX ||
4058 	    ns_type == MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_RX)
4059 		ns = mlx5_get_flow_vport_namespace(dev, ns_type, 0);
4060 	else
4061 		ns = mlx5_get_flow_namespace(dev, ns_type);
4062 	if (!ns)
4063 		return NULL;
4064 
4065 	return find_root(&ns->node);
4066 }
4067 
mlx5_modify_header_alloc(struct mlx5_core_dev * dev,u8 ns_type,u8 num_actions,void * modify_actions)4068 struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
4069 						 u8 ns_type, u8 num_actions,
4070 						 void *modify_actions)
4071 {
4072 	struct mlx5_flow_root_namespace *root;
4073 	struct mlx5_modify_hdr *modify_hdr;
4074 	int err;
4075 
4076 	root = mlx5_get_root_namespace(dev, ns_type);
4077 	if (!root)
4078 		return ERR_PTR(-EOPNOTSUPP);
4079 
4080 	modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL);
4081 	if (!modify_hdr)
4082 		return ERR_PTR(-ENOMEM);
4083 
4084 	modify_hdr->ns_type = ns_type;
4085 	err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
4086 					      modify_actions, modify_hdr);
4087 	if (err) {
4088 		kfree(modify_hdr);
4089 		return ERR_PTR(err);
4090 	}
4091 
4092 	return modify_hdr;
4093 }
4094 EXPORT_SYMBOL(mlx5_modify_header_alloc);
4095 
mlx5_modify_header_dealloc(struct mlx5_core_dev * dev,struct mlx5_modify_hdr * modify_hdr)4096 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
4097 				struct mlx5_modify_hdr *modify_hdr)
4098 {
4099 	struct mlx5_flow_root_namespace *root;
4100 
4101 	root = mlx5_get_root_namespace(dev, modify_hdr->ns_type);
4102 	if (WARN_ON(!root))
4103 		return;
4104 	root->cmds->modify_header_dealloc(root, modify_hdr);
4105 	kfree(modify_hdr);
4106 }
4107 EXPORT_SYMBOL(mlx5_modify_header_dealloc);
4108 
mlx5_packet_reformat_alloc(struct mlx5_core_dev * dev,struct mlx5_pkt_reformat_params * params,enum mlx5_flow_namespace_type ns_type)4109 struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
4110 						     struct mlx5_pkt_reformat_params *params,
4111 						     enum mlx5_flow_namespace_type ns_type)
4112 {
4113 	struct mlx5_pkt_reformat *pkt_reformat;
4114 	struct mlx5_flow_root_namespace *root;
4115 	int err;
4116 
4117 	root = mlx5_get_root_namespace(dev, ns_type);
4118 	if (!root)
4119 		return ERR_PTR(-EOPNOTSUPP);
4120 
4121 	pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL);
4122 	if (!pkt_reformat)
4123 		return ERR_PTR(-ENOMEM);
4124 
4125 	pkt_reformat->ns_type = ns_type;
4126 	pkt_reformat->reformat_type = params->type;
4127 	err = root->cmds->packet_reformat_alloc(root, params, ns_type,
4128 						pkt_reformat);
4129 	if (err) {
4130 		kfree(pkt_reformat);
4131 		return ERR_PTR(err);
4132 	}
4133 
4134 	return pkt_reformat;
4135 }
4136 EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
4137 
mlx5_packet_reformat_dealloc(struct mlx5_core_dev * dev,struct mlx5_pkt_reformat * pkt_reformat)4138 void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
4139 				  struct mlx5_pkt_reformat *pkt_reformat)
4140 {
4141 	struct mlx5_flow_root_namespace *root;
4142 
4143 	root = mlx5_get_root_namespace(dev, pkt_reformat->ns_type);
4144 	if (WARN_ON(!root))
4145 		return;
4146 	root->cmds->packet_reformat_dealloc(root, pkt_reformat);
4147 	kfree(pkt_reformat);
4148 }
4149 EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
4150 
mlx5_get_match_definer_id(struct mlx5_flow_definer * definer)4151 int mlx5_get_match_definer_id(struct mlx5_flow_definer *definer)
4152 {
4153 	return definer->id;
4154 }
4155 
4156 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)4157 mlx5_create_match_definer(struct mlx5_core_dev *dev,
4158 			  enum mlx5_flow_namespace_type ns_type, u16 format_id,
4159 			  u32 *match_mask)
4160 {
4161 	struct mlx5_flow_root_namespace *root;
4162 	struct mlx5_flow_definer *definer;
4163 	int id;
4164 
4165 	root = mlx5_get_root_namespace(dev, ns_type);
4166 	if (!root)
4167 		return ERR_PTR(-EOPNOTSUPP);
4168 
4169 	definer = kzalloc(sizeof(*definer), GFP_KERNEL);
4170 	if (!definer)
4171 		return ERR_PTR(-ENOMEM);
4172 
4173 	definer->ns_type = ns_type;
4174 	id = root->cmds->create_match_definer(root, format_id, match_mask);
4175 	if (id < 0) {
4176 		mlx5_core_warn(root->dev, "Failed to create match definer (%d)\n", id);
4177 		kfree(definer);
4178 		return ERR_PTR(id);
4179 	}
4180 	definer->id = id;
4181 	return definer;
4182 }
4183 
mlx5_destroy_match_definer(struct mlx5_core_dev * dev,struct mlx5_flow_definer * definer)4184 void mlx5_destroy_match_definer(struct mlx5_core_dev *dev,
4185 				struct mlx5_flow_definer *definer)
4186 {
4187 	struct mlx5_flow_root_namespace *root;
4188 
4189 	root = mlx5_get_root_namespace(dev, definer->ns_type);
4190 	if (WARN_ON(!root))
4191 		return;
4192 
4193 	root->cmds->destroy_match_definer(root, definer->id);
4194 	kfree(definer);
4195 }
4196 
mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_root_namespace * peer_ns,u16 peer_vhca_id)4197 int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
4198 				 struct mlx5_flow_root_namespace *peer_ns,
4199 				 u16 peer_vhca_id)
4200 {
4201 	if (peer_ns && ns->mode != peer_ns->mode) {
4202 		mlx5_core_err(ns->dev,
4203 			      "Can't peer namespace of different steering mode\n");
4204 		return -EINVAL;
4205 	}
4206 
4207 	return ns->cmds->set_peer(ns, peer_ns, peer_vhca_id);
4208 }
4209 
4210 /* This function should be called only at init stage of the namespace.
4211  * It is not safe to call this function while steering operations
4212  * are executed in the namespace.
4213  */
mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace * ns,enum mlx5_flow_steering_mode mode)4214 int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
4215 				 enum mlx5_flow_steering_mode mode)
4216 {
4217 	struct mlx5_flow_root_namespace *root;
4218 	const struct mlx5_flow_cmds *cmds;
4219 	int err;
4220 
4221 	root = find_root(&ns->node);
4222 	if (&root->ns != ns)
4223 	/* Can't set cmds to non root namespace */
4224 		return -EINVAL;
4225 
4226 	if (root->table_type != FS_FT_FDB)
4227 		return -EOPNOTSUPP;
4228 
4229 	if (root->mode == mode)
4230 		return 0;
4231 
4232 	if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
4233 		cmds = mlx5_fs_cmd_get_dr_cmds();
4234 	else if (mode == MLX5_FLOW_STEERING_MODE_HMFS)
4235 		cmds = mlx5_fs_cmd_get_hws_cmds();
4236 	else
4237 		cmds = mlx5_fs_cmd_get_fw_cmds();
4238 	if (!cmds)
4239 		return -EOPNOTSUPP;
4240 
4241 	err = cmds->create_ns(root);
4242 	if (err) {
4243 		mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
4244 			      err);
4245 		return err;
4246 	}
4247 
4248 	root->cmds->destroy_ns(root);
4249 	root->cmds = cmds;
4250 	root->mode = mode;
4251 
4252 	return 0;
4253 }
4254