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