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