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