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/eswitch.h>
36 
37 #include "mlx5_core.h"
38 #include "fs_core.h"
39 #include "fs_cmd.h"
40 #include "diag/fs_tracepoint.h"
41 #include "accel/ipsec.h"
42 #include "fpga/ipsec.h"
43 
44 #define INIT_TREE_NODE_ARRAY_SIZE(...)	(sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
45 					 sizeof(struct init_tree_node))
46 
47 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
48 		 ...) {.type = FS_TYPE_PRIO,\
49 	.min_ft_level = min_level_val,\
50 	.num_levels = num_levels_val,\
51 	.num_leaf_prios = num_prios_val,\
52 	.caps = caps_val,\
53 	.children = (struct init_tree_node[]) {__VA_ARGS__},\
54 	.ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
55 }
56 
57 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
58 	ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
59 		 __VA_ARGS__)\
60 
61 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
62 	.children = (struct init_tree_node[]) {__VA_ARGS__},\
63 	.ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
64 }
65 
66 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
67 				   sizeof(long))
68 
69 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
70 
71 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
72 			       .caps = (long[]) {__VA_ARGS__} }
73 
74 #define FS_CHAINING_CAPS  FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
75 					   FS_CAP(flow_table_properties_nic_receive.modify_root), \
76 					   FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
77 					   FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
78 
79 #define LEFTOVERS_NUM_LEVELS 1
80 #define LEFTOVERS_NUM_PRIOS 1
81 
82 #define BY_PASS_PRIO_NUM_LEVELS 1
83 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
84 			   LEFTOVERS_NUM_PRIOS)
85 
86 #define ETHTOOL_PRIO_NUM_LEVELS 1
87 #define ETHTOOL_NUM_PRIOS 11
88 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
89 /* Vlan, mac, ttc, inner ttc, aRFS */
90 #define KERNEL_NIC_PRIO_NUM_LEVELS 5
91 #define KERNEL_NIC_NUM_PRIOS 1
92 /* One more level for tc */
93 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
94 
95 #define KERNEL_NIC_TC_NUM_PRIOS  1
96 #define KERNEL_NIC_TC_NUM_LEVELS 2
97 
98 #define ANCHOR_NUM_LEVELS 1
99 #define ANCHOR_NUM_PRIOS 1
100 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
101 
102 #define OFFLOADS_MAX_FT 1
103 #define OFFLOADS_NUM_PRIOS 1
104 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
105 
106 #define LAG_PRIO_NUM_LEVELS 1
107 #define LAG_NUM_PRIOS 1
108 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
109 
110 struct node_caps {
111 	size_t	arr_sz;
112 	long	*caps;
113 };
114 
115 static struct init_tree_node {
116 	enum fs_node_type	type;
117 	struct init_tree_node *children;
118 	int ar_size;
119 	struct node_caps caps;
120 	int min_ft_level;
121 	int num_leaf_prios;
122 	int prio;
123 	int num_levels;
124 } root_fs = {
125 	.type = FS_TYPE_NAMESPACE,
126 	.ar_size = 7,
127 	.children = (struct init_tree_node[]) {
128 		ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
129 			 FS_CHAINING_CAPS,
130 			 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
131 						  BY_PASS_PRIO_NUM_LEVELS))),
132 		ADD_PRIO(0, LAG_MIN_LEVEL, 0,
133 			 FS_CHAINING_CAPS,
134 			 ADD_NS(ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
135 						  LAG_PRIO_NUM_LEVELS))),
136 		ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {},
137 			 ADD_NS(ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS, OFFLOADS_MAX_FT))),
138 		ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0,
139 			 FS_CHAINING_CAPS,
140 			 ADD_NS(ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
141 						  ETHTOOL_PRIO_NUM_LEVELS))),
142 		ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
143 			 ADD_NS(ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS, KERNEL_NIC_TC_NUM_LEVELS),
144 				ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
145 						  KERNEL_NIC_PRIO_NUM_LEVELS))),
146 		ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
147 			 FS_CHAINING_CAPS,
148 			 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_NUM_LEVELS))),
149 		ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
150 			 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_NUM_LEVELS))),
151 	}
152 };
153 
154 enum fs_i_lock_class {
155 	FS_LOCK_GRANDPARENT,
156 	FS_LOCK_PARENT,
157 	FS_LOCK_CHILD
158 };
159 
160 static const struct rhashtable_params rhash_fte = {
161 	.key_len = FIELD_SIZEOF(struct fs_fte, val),
162 	.key_offset = offsetof(struct fs_fte, val),
163 	.head_offset = offsetof(struct fs_fte, hash),
164 	.automatic_shrinking = true,
165 	.min_size = 1,
166 };
167 
168 static const struct rhashtable_params rhash_fg = {
169 	.key_len = FIELD_SIZEOF(struct mlx5_flow_group, mask),
170 	.key_offset = offsetof(struct mlx5_flow_group, mask),
171 	.head_offset = offsetof(struct mlx5_flow_group, hash),
172 	.automatic_shrinking = true,
173 	.min_size = 1,
174 
175 };
176 
177 static void del_hw_flow_table(struct fs_node *node);
178 static void del_hw_flow_group(struct fs_node *node);
179 static void del_hw_fte(struct fs_node *node);
180 static void del_sw_flow_table(struct fs_node *node);
181 static void del_sw_flow_group(struct fs_node *node);
182 static void del_sw_fte(struct fs_node *node);
183 static void del_sw_prio(struct fs_node *node);
184 static void del_sw_ns(struct fs_node *node);
185 /* Delete rule (destination) is special case that
186  * requires to lock the FTE for all the deletion process.
187  */
188 static void del_sw_hw_rule(struct fs_node *node);
189 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
190 				struct mlx5_flow_destination *d2);
191 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
192 static struct mlx5_flow_rule *
193 find_flow_rule(struct fs_fte *fte,
194 	       struct mlx5_flow_destination *dest);
195 
tree_init_node(struct fs_node * node,void (* del_hw_func)(struct fs_node *),void (* del_sw_func)(struct fs_node *))196 static void tree_init_node(struct fs_node *node,
197 			   void (*del_hw_func)(struct fs_node *),
198 			   void (*del_sw_func)(struct fs_node *))
199 {
200 	refcount_set(&node->refcount, 1);
201 	INIT_LIST_HEAD(&node->list);
202 	INIT_LIST_HEAD(&node->children);
203 	init_rwsem(&node->lock);
204 	node->del_hw_func = del_hw_func;
205 	node->del_sw_func = del_sw_func;
206 	node->active = false;
207 }
208 
tree_add_node(struct fs_node * node,struct fs_node * parent)209 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
210 {
211 	if (parent)
212 		refcount_inc(&parent->refcount);
213 	node->parent = parent;
214 
215 	/* Parent is the root */
216 	if (!parent)
217 		node->root = node;
218 	else
219 		node->root = parent->root;
220 }
221 
tree_get_node(struct fs_node * node)222 static int tree_get_node(struct fs_node *node)
223 {
224 	return refcount_inc_not_zero(&node->refcount);
225 }
226 
nested_down_read_ref_node(struct fs_node * node,enum fs_i_lock_class class)227 static void nested_down_read_ref_node(struct fs_node *node,
228 				      enum fs_i_lock_class class)
229 {
230 	if (node) {
231 		down_read_nested(&node->lock, class);
232 		refcount_inc(&node->refcount);
233 	}
234 }
235 
nested_down_write_ref_node(struct fs_node * node,enum fs_i_lock_class class)236 static void nested_down_write_ref_node(struct fs_node *node,
237 				       enum fs_i_lock_class class)
238 {
239 	if (node) {
240 		down_write_nested(&node->lock, class);
241 		refcount_inc(&node->refcount);
242 	}
243 }
244 
down_write_ref_node(struct fs_node * node)245 static void down_write_ref_node(struct fs_node *node)
246 {
247 	if (node) {
248 		down_write(&node->lock);
249 		refcount_inc(&node->refcount);
250 	}
251 }
252 
up_read_ref_node(struct fs_node * node)253 static void up_read_ref_node(struct fs_node *node)
254 {
255 	refcount_dec(&node->refcount);
256 	up_read(&node->lock);
257 }
258 
up_write_ref_node(struct fs_node * node)259 static void up_write_ref_node(struct fs_node *node)
260 {
261 	refcount_dec(&node->refcount);
262 	up_write(&node->lock);
263 }
264 
tree_put_node(struct fs_node * node)265 static void tree_put_node(struct fs_node *node)
266 {
267 	struct fs_node *parent_node = node->parent;
268 
269 	if (refcount_dec_and_test(&node->refcount)) {
270 		if (node->del_hw_func)
271 			node->del_hw_func(node);
272 		if (parent_node) {
273 			/* Only root namespace doesn't have parent and we just
274 			 * need to free its node.
275 			 */
276 			down_write_ref_node(parent_node);
277 			list_del_init(&node->list);
278 			if (node->del_sw_func)
279 				node->del_sw_func(node);
280 			up_write_ref_node(parent_node);
281 		} else {
282 			kfree(node);
283 		}
284 		node = NULL;
285 	}
286 	if (!node && parent_node)
287 		tree_put_node(parent_node);
288 }
289 
tree_remove_node(struct fs_node * node)290 static int tree_remove_node(struct fs_node *node)
291 {
292 	if (refcount_read(&node->refcount) > 1) {
293 		refcount_dec(&node->refcount);
294 		return -EEXIST;
295 	}
296 	tree_put_node(node);
297 	return 0;
298 }
299 
find_prio(struct mlx5_flow_namespace * ns,unsigned int prio)300 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
301 				 unsigned int prio)
302 {
303 	struct fs_prio *iter_prio;
304 
305 	fs_for_each_prio(iter_prio, ns) {
306 		if (iter_prio->prio == prio)
307 			return iter_prio;
308 	}
309 
310 	return NULL;
311 }
312 
check_valid_spec(const struct mlx5_flow_spec * spec)313 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
314 {
315 	int i;
316 
317 	for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
318 		if (spec->match_value[i] & ~spec->match_criteria[i]) {
319 			pr_warn("mlx5_core: match_value differs from match_criteria\n");
320 			return false;
321 		}
322 
323 	return true;
324 }
325 
find_root(struct fs_node * node)326 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
327 {
328 	struct fs_node *root;
329 	struct mlx5_flow_namespace *ns;
330 
331 	root = node->root;
332 
333 	if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
334 		pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
335 		return NULL;
336 	}
337 
338 	ns = container_of(root, struct mlx5_flow_namespace, node);
339 	return container_of(ns, struct mlx5_flow_root_namespace, ns);
340 }
341 
get_steering(struct fs_node * node)342 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
343 {
344 	struct mlx5_flow_root_namespace *root = find_root(node);
345 
346 	if (root)
347 		return root->dev->priv.steering;
348 	return NULL;
349 }
350 
get_dev(struct fs_node * node)351 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
352 {
353 	struct mlx5_flow_root_namespace *root = find_root(node);
354 
355 	if (root)
356 		return root->dev;
357 	return NULL;
358 }
359 
del_sw_ns(struct fs_node * node)360 static void del_sw_ns(struct fs_node *node)
361 {
362 	kfree(node);
363 }
364 
del_sw_prio(struct fs_node * node)365 static void del_sw_prio(struct fs_node *node)
366 {
367 	kfree(node);
368 }
369 
del_hw_flow_table(struct fs_node * node)370 static void del_hw_flow_table(struct fs_node *node)
371 {
372 	struct mlx5_flow_root_namespace *root;
373 	struct mlx5_flow_table *ft;
374 	struct mlx5_core_dev *dev;
375 	int err;
376 
377 	fs_get_obj(ft, node);
378 	dev = get_dev(&ft->node);
379 	root = find_root(&ft->node);
380 
381 	if (node->active) {
382 		err = root->cmds->destroy_flow_table(dev, ft);
383 		if (err)
384 			mlx5_core_warn(dev, "flow steering can't destroy ft\n");
385 	}
386 }
387 
del_sw_flow_table(struct fs_node * node)388 static void del_sw_flow_table(struct fs_node *node)
389 {
390 	struct mlx5_flow_table *ft;
391 	struct fs_prio *prio;
392 
393 	fs_get_obj(ft, node);
394 
395 	rhltable_destroy(&ft->fgs_hash);
396 	fs_get_obj(prio, ft->node.parent);
397 	prio->num_ft--;
398 	kfree(ft);
399 }
400 
del_sw_hw_rule(struct fs_node * node)401 static void del_sw_hw_rule(struct fs_node *node)
402 {
403 	struct mlx5_flow_root_namespace *root;
404 	struct mlx5_flow_rule *rule;
405 	struct mlx5_flow_table *ft;
406 	struct mlx5_flow_group *fg;
407 	struct fs_fte *fte;
408 	int modify_mask;
409 	struct mlx5_core_dev *dev = get_dev(node);
410 	int err;
411 	bool update_fte = false;
412 
413 	fs_get_obj(rule, node);
414 	fs_get_obj(fte, rule->node.parent);
415 	fs_get_obj(fg, fte->node.parent);
416 	fs_get_obj(ft, fg->node.parent);
417 	trace_mlx5_fs_del_rule(rule);
418 	if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
419 		mutex_lock(&rule->dest_attr.ft->lock);
420 		list_del(&rule->next_ft);
421 		mutex_unlock(&rule->dest_attr.ft->lock);
422 	}
423 
424 	if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER  &&
425 	    --fte->dests_size) {
426 		modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
427 			      BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
428 		fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
429 		update_fte = true;
430 		goto out;
431 	}
432 
433 	if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
434 	    --fte->dests_size) {
435 		modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
436 		update_fte = true;
437 	}
438 out:
439 	root = find_root(&ft->node);
440 	if (update_fte && fte->dests_size) {
441 		err = root->cmds->update_fte(dev, ft, fg->id, modify_mask, fte);
442 		if (err)
443 			mlx5_core_warn(dev,
444 				       "%s can't del rule fg id=%d fte_index=%d\n",
445 				       __func__, fg->id, fte->index);
446 	}
447 	kfree(rule);
448 }
449 
del_hw_fte(struct fs_node * node)450 static void del_hw_fte(struct fs_node *node)
451 {
452 	struct mlx5_flow_root_namespace *root;
453 	struct mlx5_flow_table *ft;
454 	struct mlx5_flow_group *fg;
455 	struct mlx5_core_dev *dev;
456 	struct fs_fte *fte;
457 	int err;
458 
459 	fs_get_obj(fte, node);
460 	fs_get_obj(fg, fte->node.parent);
461 	fs_get_obj(ft, fg->node.parent);
462 
463 	trace_mlx5_fs_del_fte(fte);
464 	dev = get_dev(&ft->node);
465 	root = find_root(&ft->node);
466 	if (node->active) {
467 		err = root->cmds->delete_fte(dev, ft, fte);
468 		if (err)
469 			mlx5_core_warn(dev,
470 				       "flow steering can't delete fte in index %d of flow group id %d\n",
471 				       fte->index, fg->id);
472 	}
473 }
474 
del_sw_fte(struct fs_node * node)475 static void del_sw_fte(struct fs_node *node)
476 {
477 	struct mlx5_flow_steering *steering = get_steering(node);
478 	struct mlx5_flow_group *fg;
479 	struct fs_fte *fte;
480 	int err;
481 
482 	fs_get_obj(fte, node);
483 	fs_get_obj(fg, fte->node.parent);
484 
485 	err = rhashtable_remove_fast(&fg->ftes_hash,
486 				     &fte->hash,
487 				     rhash_fte);
488 	WARN_ON(err);
489 	ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
490 	kmem_cache_free(steering->ftes_cache, fte);
491 }
492 
del_hw_flow_group(struct fs_node * node)493 static void del_hw_flow_group(struct fs_node *node)
494 {
495 	struct mlx5_flow_root_namespace *root;
496 	struct mlx5_flow_group *fg;
497 	struct mlx5_flow_table *ft;
498 	struct mlx5_core_dev *dev;
499 
500 	fs_get_obj(fg, node);
501 	fs_get_obj(ft, fg->node.parent);
502 	dev = get_dev(&ft->node);
503 	trace_mlx5_fs_del_fg(fg);
504 
505 	root = find_root(&ft->node);
506 	if (fg->node.active && root->cmds->destroy_flow_group(dev, ft, fg->id))
507 		mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
508 			       fg->id, ft->id);
509 }
510 
del_sw_flow_group(struct fs_node * node)511 static void del_sw_flow_group(struct fs_node *node)
512 {
513 	struct mlx5_flow_steering *steering = get_steering(node);
514 	struct mlx5_flow_group *fg;
515 	struct mlx5_flow_table *ft;
516 	int err;
517 
518 	fs_get_obj(fg, node);
519 	fs_get_obj(ft, fg->node.parent);
520 
521 	rhashtable_destroy(&fg->ftes_hash);
522 	ida_destroy(&fg->fte_allocator);
523 	if (ft->autogroup.active)
524 		ft->autogroup.num_groups--;
525 	err = rhltable_remove(&ft->fgs_hash,
526 			      &fg->hash,
527 			      rhash_fg);
528 	WARN_ON(err);
529 	kmem_cache_free(steering->fgs_cache, fg);
530 }
531 
insert_fte(struct mlx5_flow_group * fg,struct fs_fte * fte)532 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
533 {
534 	int index;
535 	int ret;
536 
537 	index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
538 	if (index < 0)
539 		return index;
540 
541 	fte->index = index + fg->start_index;
542 	ret = rhashtable_insert_fast(&fg->ftes_hash,
543 				     &fte->hash,
544 				     rhash_fte);
545 	if (ret)
546 		goto err_ida_remove;
547 
548 	tree_add_node(&fte->node, &fg->node);
549 	list_add_tail(&fte->node.list, &fg->node.children);
550 	return 0;
551 
552 err_ida_remove:
553 	ida_simple_remove(&fg->fte_allocator, index);
554 	return ret;
555 }
556 
alloc_fte(struct mlx5_flow_table * ft,u32 * match_value,struct mlx5_flow_act * flow_act)557 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
558 				u32 *match_value,
559 				struct mlx5_flow_act *flow_act)
560 {
561 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
562 	struct fs_fte *fte;
563 
564 	fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
565 	if (!fte)
566 		return ERR_PTR(-ENOMEM);
567 
568 	memcpy(fte->val, match_value, sizeof(fte->val));
569 	fte->node.type =  FS_TYPE_FLOW_ENTRY;
570 	fte->action = *flow_act;
571 
572 	tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
573 
574 	return fte;
575 }
576 
dealloc_flow_group(struct mlx5_flow_steering * steering,struct mlx5_flow_group * fg)577 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
578 			       struct mlx5_flow_group *fg)
579 {
580 	rhashtable_destroy(&fg->ftes_hash);
581 	kmem_cache_free(steering->fgs_cache, fg);
582 }
583 
alloc_flow_group(struct mlx5_flow_steering * steering,u8 match_criteria_enable,void * match_criteria,int start_index,int end_index)584 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
585 						u8 match_criteria_enable,
586 						void *match_criteria,
587 						int start_index,
588 						int end_index)
589 {
590 	struct mlx5_flow_group *fg;
591 	int ret;
592 
593 	fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
594 	if (!fg)
595 		return ERR_PTR(-ENOMEM);
596 
597 	ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
598 	if (ret) {
599 		kmem_cache_free(steering->fgs_cache, fg);
600 		return ERR_PTR(ret);
601 }
602 	ida_init(&fg->fte_allocator);
603 	fg->mask.match_criteria_enable = match_criteria_enable;
604 	memcpy(&fg->mask.match_criteria, match_criteria,
605 	       sizeof(fg->mask.match_criteria));
606 	fg->node.type =  FS_TYPE_FLOW_GROUP;
607 	fg->start_index = start_index;
608 	fg->max_ftes = end_index - start_index + 1;
609 
610 	return fg;
611 }
612 
alloc_insert_flow_group(struct mlx5_flow_table * ft,u8 match_criteria_enable,void * match_criteria,int start_index,int end_index,struct list_head * prev)613 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
614 						       u8 match_criteria_enable,
615 						       void *match_criteria,
616 						       int start_index,
617 						       int end_index,
618 						       struct list_head *prev)
619 {
620 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
621 	struct mlx5_flow_group *fg;
622 	int ret;
623 
624 	fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
625 			      start_index, end_index);
626 	if (IS_ERR(fg))
627 		return fg;
628 
629 	/* initialize refcnt, add to parent list */
630 	ret = rhltable_insert(&ft->fgs_hash,
631 			      &fg->hash,
632 			      rhash_fg);
633 	if (ret) {
634 		dealloc_flow_group(steering, fg);
635 		return ERR_PTR(ret);
636 	}
637 
638 	tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
639 	tree_add_node(&fg->node, &ft->node);
640 	/* Add node to group list */
641 	list_add(&fg->node.list, prev);
642 	atomic_inc(&ft->node.version);
643 
644 	return fg;
645 }
646 
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)647 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
648 						enum fs_flow_table_type table_type,
649 						enum fs_flow_table_op_mod op_mod,
650 						u32 flags)
651 {
652 	struct mlx5_flow_table *ft;
653 	int ret;
654 
655 	ft  = kzalloc(sizeof(*ft), GFP_KERNEL);
656 	if (!ft)
657 		return ERR_PTR(-ENOMEM);
658 
659 	ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
660 	if (ret) {
661 		kfree(ft);
662 		return ERR_PTR(ret);
663 	}
664 
665 	ft->level = level;
666 	ft->node.type = FS_TYPE_FLOW_TABLE;
667 	ft->op_mod = op_mod;
668 	ft->type = table_type;
669 	ft->vport = vport;
670 	ft->max_fte = max_fte;
671 	ft->flags = flags;
672 	INIT_LIST_HEAD(&ft->fwd_rules);
673 	mutex_init(&ft->lock);
674 
675 	return ft;
676 }
677 
678 /* If reverse is false, then we search for the first flow table in the
679  * root sub-tree from start(closest from right), else we search for the
680  * last flow table in the root sub-tree till start(closest from left).
681  */
find_closest_ft_recursive(struct fs_node * root,struct list_head * start,bool reverse)682 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node  *root,
683 							 struct list_head *start,
684 							 bool reverse)
685 {
686 #define list_advance_entry(pos, reverse)		\
687 	((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
688 
689 #define list_for_each_advance_continue(pos, head, reverse)	\
690 	for (pos = list_advance_entry(pos, reverse);		\
691 	     &pos->list != (head);				\
692 	     pos = list_advance_entry(pos, reverse))
693 
694 	struct fs_node *iter = list_entry(start, struct fs_node, list);
695 	struct mlx5_flow_table *ft = NULL;
696 
697 	if (!root)
698 		return NULL;
699 
700 	list_for_each_advance_continue(iter, &root->children, reverse) {
701 		if (iter->type == FS_TYPE_FLOW_TABLE) {
702 			fs_get_obj(ft, iter);
703 			return ft;
704 		}
705 		ft = find_closest_ft_recursive(iter, &iter->children, reverse);
706 		if (ft)
707 			return ft;
708 	}
709 
710 	return ft;
711 }
712 
713 /* If reverse if false then return the first flow table in next priority of
714  * prio in the tree, else return the last flow table in the previous priority
715  * of prio in the tree.
716  */
find_closest_ft(struct fs_prio * prio,bool reverse)717 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
718 {
719 	struct mlx5_flow_table *ft = NULL;
720 	struct fs_node *curr_node;
721 	struct fs_node *parent;
722 
723 	parent = prio->node.parent;
724 	curr_node = &prio->node;
725 	while (!ft && parent) {
726 		ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
727 		curr_node = parent;
728 		parent = curr_node->parent;
729 	}
730 	return ft;
731 }
732 
733 /* Assuming all the tree is locked by mutex chain lock */
find_next_chained_ft(struct fs_prio * prio)734 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
735 {
736 	return find_closest_ft(prio, false);
737 }
738 
739 /* Assuming all the tree is locked by mutex chain lock */
find_prev_chained_ft(struct fs_prio * prio)740 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
741 {
742 	return find_closest_ft(prio, true);
743 }
744 
connect_fts_in_prio(struct mlx5_core_dev * dev,struct fs_prio * prio,struct mlx5_flow_table * ft)745 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
746 			       struct fs_prio *prio,
747 			       struct mlx5_flow_table *ft)
748 {
749 	struct mlx5_flow_root_namespace *root = find_root(&prio->node);
750 	struct mlx5_flow_table *iter;
751 	int i = 0;
752 	int err;
753 
754 	fs_for_each_ft(iter, prio) {
755 		i++;
756 		err = root->cmds->modify_flow_table(dev, iter, ft);
757 		if (err) {
758 			mlx5_core_warn(dev, "Failed to modify flow table %d\n",
759 				       iter->id);
760 			/* The driver is out of sync with the FW */
761 			if (i > 1)
762 				WARN_ON(true);
763 			return err;
764 		}
765 	}
766 	return 0;
767 }
768 
769 /* 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)770 static int connect_prev_fts(struct mlx5_core_dev *dev,
771 			    struct mlx5_flow_table *ft,
772 			    struct fs_prio *prio)
773 {
774 	struct mlx5_flow_table *prev_ft;
775 
776 	prev_ft = find_prev_chained_ft(prio);
777 	if (prev_ft) {
778 		struct fs_prio *prev_prio;
779 
780 		fs_get_obj(prev_prio, prev_ft->node.parent);
781 		return connect_fts_in_prio(dev, prev_prio, ft);
782 	}
783 	return 0;
784 }
785 
update_root_ft_create(struct mlx5_flow_table * ft,struct fs_prio * prio)786 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
787 				 *prio)
788 {
789 	struct mlx5_flow_root_namespace *root = find_root(&prio->node);
790 	struct mlx5_ft_underlay_qp *uqp;
791 	int min_level = INT_MAX;
792 	int err;
793 	u32 qpn;
794 
795 	if (root->root_ft)
796 		min_level = root->root_ft->level;
797 
798 	if (ft->level >= min_level)
799 		return 0;
800 
801 	if (list_empty(&root->underlay_qpns)) {
802 		/* Don't set any QPN (zero) in case QPN list is empty */
803 		qpn = 0;
804 		err = root->cmds->update_root_ft(root->dev, ft, qpn, false);
805 	} else {
806 		list_for_each_entry(uqp, &root->underlay_qpns, list) {
807 			qpn = uqp->qpn;
808 			err = root->cmds->update_root_ft(root->dev, ft,
809 							 qpn, false);
810 			if (err)
811 				break;
812 		}
813 	}
814 
815 	if (err)
816 		mlx5_core_warn(root->dev,
817 			       "Update root flow table of id(%u) qpn(%d) failed\n",
818 			       ft->id, qpn);
819 	else
820 		root->root_ft = ft;
821 
822 	return err;
823 }
824 
_mlx5_modify_rule_destination(struct mlx5_flow_rule * rule,struct mlx5_flow_destination * dest)825 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
826 					 struct mlx5_flow_destination *dest)
827 {
828 	struct mlx5_flow_root_namespace *root;
829 	struct mlx5_flow_table *ft;
830 	struct mlx5_flow_group *fg;
831 	struct fs_fte *fte;
832 	int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
833 	int err = 0;
834 
835 	fs_get_obj(fte, rule->node.parent);
836 	if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
837 		return -EINVAL;
838 	down_write_ref_node(&fte->node);
839 	fs_get_obj(fg, fte->node.parent);
840 	fs_get_obj(ft, fg->node.parent);
841 
842 	memcpy(&rule->dest_attr, dest, sizeof(*dest));
843 	root = find_root(&ft->node);
844 	err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
845 				     modify_mask, fte);
846 	up_write_ref_node(&fte->node);
847 
848 	return err;
849 }
850 
mlx5_modify_rule_destination(struct mlx5_flow_handle * handle,struct mlx5_flow_destination * new_dest,struct mlx5_flow_destination * old_dest)851 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
852 				 struct mlx5_flow_destination *new_dest,
853 				 struct mlx5_flow_destination *old_dest)
854 {
855 	int i;
856 
857 	if (!old_dest) {
858 		if (handle->num_rules != 1)
859 			return -EINVAL;
860 		return _mlx5_modify_rule_destination(handle->rule[0],
861 						     new_dest);
862 	}
863 
864 	for (i = 0; i < handle->num_rules; i++) {
865 		if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
866 			return _mlx5_modify_rule_destination(handle->rule[i],
867 							     new_dest);
868 	}
869 
870 	return -EINVAL;
871 }
872 
873 /* 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)874 static int connect_fwd_rules(struct mlx5_core_dev *dev,
875 			     struct mlx5_flow_table *new_next_ft,
876 			     struct mlx5_flow_table *old_next_ft)
877 {
878 	struct mlx5_flow_destination dest = {};
879 	struct mlx5_flow_rule *iter;
880 	int err = 0;
881 
882 	/* new_next_ft and old_next_ft could be NULL only
883 	 * when we create/destroy the anchor flow table.
884 	 */
885 	if (!new_next_ft || !old_next_ft)
886 		return 0;
887 
888 	dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
889 	dest.ft = new_next_ft;
890 
891 	mutex_lock(&old_next_ft->lock);
892 	list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
893 	mutex_unlock(&old_next_ft->lock);
894 	list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
895 		err = _mlx5_modify_rule_destination(iter, &dest);
896 		if (err)
897 			pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
898 			       new_next_ft->id);
899 	}
900 	return 0;
901 }
902 
connect_flow_table(struct mlx5_core_dev * dev,struct mlx5_flow_table * ft,struct fs_prio * prio)903 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
904 			      struct fs_prio *prio)
905 {
906 	struct mlx5_flow_table *next_ft;
907 	int err = 0;
908 
909 	/* Connect_prev_fts and update_root_ft_create are mutually exclusive */
910 
911 	if (list_empty(&prio->node.children)) {
912 		err = connect_prev_fts(dev, ft, prio);
913 		if (err)
914 			return err;
915 
916 		next_ft = find_next_chained_ft(prio);
917 		err = connect_fwd_rules(dev, ft, next_ft);
918 		if (err)
919 			return err;
920 	}
921 
922 	if (MLX5_CAP_FLOWTABLE(dev,
923 			       flow_table_properties_nic_receive.modify_root))
924 		err = update_root_ft_create(ft, prio);
925 	return err;
926 }
927 
list_add_flow_table(struct mlx5_flow_table * ft,struct fs_prio * prio)928 static void list_add_flow_table(struct mlx5_flow_table *ft,
929 				struct fs_prio *prio)
930 {
931 	struct list_head *prev = &prio->node.children;
932 	struct mlx5_flow_table *iter;
933 
934 	fs_for_each_ft(iter, prio) {
935 		if (iter->level > ft->level)
936 			break;
937 		prev = &iter->node.list;
938 	}
939 	list_add(&ft->node.list, prev);
940 }
941 
__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)942 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
943 							struct mlx5_flow_table_attr *ft_attr,
944 							enum fs_flow_table_op_mod op_mod,
945 							u16 vport)
946 {
947 	struct mlx5_flow_root_namespace *root = find_root(&ns->node);
948 	struct mlx5_flow_table *next_ft = NULL;
949 	struct fs_prio *fs_prio = NULL;
950 	struct mlx5_flow_table *ft;
951 	int log_table_sz;
952 	int err;
953 
954 	if (!root) {
955 		pr_err("mlx5: flow steering failed to find root of namespace\n");
956 		return ERR_PTR(-ENODEV);
957 	}
958 
959 	mutex_lock(&root->chain_lock);
960 	fs_prio = find_prio(ns, ft_attr->prio);
961 	if (!fs_prio) {
962 		err = -EINVAL;
963 		goto unlock_root;
964 	}
965 	if (ft_attr->level >= fs_prio->num_levels) {
966 		err = -ENOSPC;
967 		goto unlock_root;
968 	}
969 	/* The level is related to the
970 	 * priority level range.
971 	 */
972 	ft_attr->level += fs_prio->start_level;
973 	ft = alloc_flow_table(ft_attr->level,
974 			      vport,
975 			      ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
976 			      root->table_type,
977 			      op_mod, ft_attr->flags);
978 	if (IS_ERR(ft)) {
979 		err = PTR_ERR(ft);
980 		goto unlock_root;
981 	}
982 
983 	tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
984 	log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
985 	next_ft = find_next_chained_ft(fs_prio);
986 	err = root->cmds->create_flow_table(root->dev, ft->vport, ft->op_mod,
987 					    ft->type, ft->level, log_table_sz,
988 					    next_ft, &ft->id, ft->flags);
989 	if (err)
990 		goto free_ft;
991 
992 	err = connect_flow_table(root->dev, ft, fs_prio);
993 	if (err)
994 		goto destroy_ft;
995 	ft->node.active = true;
996 	down_write_ref_node(&fs_prio->node);
997 	tree_add_node(&ft->node, &fs_prio->node);
998 	list_add_flow_table(ft, fs_prio);
999 	fs_prio->num_ft++;
1000 	up_write_ref_node(&fs_prio->node);
1001 	mutex_unlock(&root->chain_lock);
1002 	return ft;
1003 destroy_ft:
1004 	root->cmds->destroy_flow_table(root->dev, ft);
1005 free_ft:
1006 	kfree(ft);
1007 unlock_root:
1008 	mutex_unlock(&root->chain_lock);
1009 	return ERR_PTR(err);
1010 }
1011 
mlx5_create_flow_table(struct mlx5_flow_namespace * ns,struct mlx5_flow_table_attr * ft_attr)1012 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1013 					       struct mlx5_flow_table_attr *ft_attr)
1014 {
1015 	return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1016 }
1017 
mlx5_create_vport_flow_table(struct mlx5_flow_namespace * ns,int prio,int max_fte,u32 level,u16 vport)1018 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1019 						     int prio, int max_fte,
1020 						     u32 level, u16 vport)
1021 {
1022 	struct mlx5_flow_table_attr ft_attr = {};
1023 
1024 	ft_attr.max_fte = max_fte;
1025 	ft_attr.level   = level;
1026 	ft_attr.prio    = prio;
1027 
1028 	return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1029 }
1030 
1031 struct mlx5_flow_table*
mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace * ns,int prio,u32 level)1032 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1033 				 int prio, u32 level)
1034 {
1035 	struct mlx5_flow_table_attr ft_attr = {};
1036 
1037 	ft_attr.level = level;
1038 	ft_attr.prio  = prio;
1039 	return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1040 }
1041 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1042 
1043 struct mlx5_flow_table*
mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace * ns,int prio,int num_flow_table_entries,int max_num_groups,u32 level,u32 flags)1044 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1045 				    int prio,
1046 				    int num_flow_table_entries,
1047 				    int max_num_groups,
1048 				    u32 level,
1049 				    u32 flags)
1050 {
1051 	struct mlx5_flow_table_attr ft_attr = {};
1052 	struct mlx5_flow_table *ft;
1053 
1054 	if (max_num_groups > num_flow_table_entries)
1055 		return ERR_PTR(-EINVAL);
1056 
1057 	ft_attr.max_fte = num_flow_table_entries;
1058 	ft_attr.prio    = prio;
1059 	ft_attr.level   = level;
1060 	ft_attr.flags   = flags;
1061 
1062 	ft = mlx5_create_flow_table(ns, &ft_attr);
1063 	if (IS_ERR(ft))
1064 		return ft;
1065 
1066 	ft->autogroup.active = true;
1067 	ft->autogroup.required_groups = max_num_groups;
1068 
1069 	return ft;
1070 }
1071 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1072 
mlx5_create_flow_group(struct mlx5_flow_table * ft,u32 * fg_in)1073 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1074 					       u32 *fg_in)
1075 {
1076 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1077 	void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1078 					    fg_in, match_criteria);
1079 	u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1080 					    fg_in,
1081 					    match_criteria_enable);
1082 	int start_index = MLX5_GET(create_flow_group_in, fg_in,
1083 				   start_flow_index);
1084 	int end_index = MLX5_GET(create_flow_group_in, fg_in,
1085 				 end_flow_index);
1086 	struct mlx5_core_dev *dev = get_dev(&ft->node);
1087 	struct mlx5_flow_group *fg;
1088 	int err;
1089 
1090 	if (ft->autogroup.active)
1091 		return ERR_PTR(-EPERM);
1092 
1093 	down_write_ref_node(&ft->node);
1094 	fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1095 				     start_index, end_index,
1096 				     ft->node.children.prev);
1097 	up_write_ref_node(&ft->node);
1098 	if (IS_ERR(fg))
1099 		return fg;
1100 
1101 	err = root->cmds->create_flow_group(dev, ft, fg_in, &fg->id);
1102 	if (err) {
1103 		tree_put_node(&fg->node);
1104 		return ERR_PTR(err);
1105 	}
1106 	trace_mlx5_fs_add_fg(fg);
1107 	fg->node.active = true;
1108 
1109 	return fg;
1110 }
1111 
alloc_rule(struct mlx5_flow_destination * dest)1112 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1113 {
1114 	struct mlx5_flow_rule *rule;
1115 
1116 	rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1117 	if (!rule)
1118 		return NULL;
1119 
1120 	INIT_LIST_HEAD(&rule->next_ft);
1121 	rule->node.type = FS_TYPE_FLOW_DEST;
1122 	if (dest)
1123 		memcpy(&rule->dest_attr, dest, sizeof(*dest));
1124 
1125 	return rule;
1126 }
1127 
alloc_handle(int num_rules)1128 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1129 {
1130 	struct mlx5_flow_handle *handle;
1131 
1132 	handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
1133 	if (!handle)
1134 		return NULL;
1135 
1136 	handle->num_rules = num_rules;
1137 
1138 	return handle;
1139 }
1140 
destroy_flow_handle(struct fs_fte * fte,struct mlx5_flow_handle * handle,struct mlx5_flow_destination * dest,int i)1141 static void destroy_flow_handle(struct fs_fte *fte,
1142 				struct mlx5_flow_handle *handle,
1143 				struct mlx5_flow_destination *dest,
1144 				int i)
1145 {
1146 	for (; --i >= 0;) {
1147 		if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1148 			fte->dests_size--;
1149 			list_del(&handle->rule[i]->node.list);
1150 			kfree(handle->rule[i]);
1151 		}
1152 	}
1153 	kfree(handle);
1154 }
1155 
1156 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)1157 create_flow_handle(struct fs_fte *fte,
1158 		   struct mlx5_flow_destination *dest,
1159 		   int dest_num,
1160 		   int *modify_mask,
1161 		   bool *new_rule)
1162 {
1163 	struct mlx5_flow_handle *handle;
1164 	struct mlx5_flow_rule *rule = NULL;
1165 	static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1166 	static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1167 	int type;
1168 	int i = 0;
1169 
1170 	handle = alloc_handle((dest_num) ? dest_num : 1);
1171 	if (!handle)
1172 		return ERR_PTR(-ENOMEM);
1173 
1174 	do {
1175 		if (dest) {
1176 			rule = find_flow_rule(fte, dest + i);
1177 			if (rule) {
1178 				refcount_inc(&rule->node.refcount);
1179 				goto rule_found;
1180 			}
1181 		}
1182 
1183 		*new_rule = true;
1184 		rule = alloc_rule(dest + i);
1185 		if (!rule)
1186 			goto free_rules;
1187 
1188 		/* Add dest to dests list- we need flow tables to be in the
1189 		 * end of the list for forward to next prio rules.
1190 		 */
1191 		tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1192 		if (dest &&
1193 		    dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1194 			list_add(&rule->node.list, &fte->node.children);
1195 		else
1196 			list_add_tail(&rule->node.list, &fte->node.children);
1197 		if (dest) {
1198 			fte->dests_size++;
1199 
1200 			type = dest[i].type ==
1201 				MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1202 			*modify_mask |= type ? count : dst;
1203 		}
1204 rule_found:
1205 		handle->rule[i] = rule;
1206 	} while (++i < dest_num);
1207 
1208 	return handle;
1209 
1210 free_rules:
1211 	destroy_flow_handle(fte, handle, dest, i);
1212 	return ERR_PTR(-ENOMEM);
1213 }
1214 
1215 /* fte should not be deleted while calling this function */
1216 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)1217 add_rule_fte(struct fs_fte *fte,
1218 	     struct mlx5_flow_group *fg,
1219 	     struct mlx5_flow_destination *dest,
1220 	     int dest_num,
1221 	     bool update_action)
1222 {
1223 	struct mlx5_flow_root_namespace *root;
1224 	struct mlx5_flow_handle *handle;
1225 	struct mlx5_flow_table *ft;
1226 	int modify_mask = 0;
1227 	int err;
1228 	bool new_rule = false;
1229 
1230 	handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1231 				    &new_rule);
1232 	if (IS_ERR(handle) || !new_rule)
1233 		goto out;
1234 
1235 	if (update_action)
1236 		modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1237 
1238 	fs_get_obj(ft, fg->node.parent);
1239 	root = find_root(&fg->node);
1240 	if (!(fte->status & FS_FTE_STATUS_EXISTING))
1241 		err = root->cmds->create_fte(get_dev(&ft->node),
1242 					     ft, fg, fte);
1243 	else
1244 		err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
1245 						     modify_mask, fte);
1246 	if (err)
1247 		goto free_handle;
1248 
1249 	fte->node.active = true;
1250 	fte->status |= FS_FTE_STATUS_EXISTING;
1251 	atomic_inc(&fte->node.version);
1252 
1253 out:
1254 	return handle;
1255 
1256 free_handle:
1257 	destroy_flow_handle(fte, handle, dest, handle->num_rules);
1258 	return ERR_PTR(err);
1259 }
1260 
alloc_auto_flow_group(struct mlx5_flow_table * ft,struct mlx5_flow_spec * spec)1261 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table  *ft,
1262 						     struct mlx5_flow_spec *spec)
1263 {
1264 	struct list_head *prev = &ft->node.children;
1265 	struct mlx5_flow_group *fg;
1266 	unsigned int candidate_index = 0;
1267 	unsigned int group_size = 0;
1268 
1269 	if (!ft->autogroup.active)
1270 		return ERR_PTR(-ENOENT);
1271 
1272 	if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1273 		/* We save place for flow groups in addition to max types */
1274 		group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
1275 
1276 	/*  ft->max_fte == ft->autogroup.max_types */
1277 	if (group_size == 0)
1278 		group_size = 1;
1279 
1280 	/* sorted by start_index */
1281 	fs_for_each_fg(fg, ft) {
1282 		if (candidate_index + group_size > fg->start_index)
1283 			candidate_index = fg->start_index + fg->max_ftes;
1284 		else
1285 			break;
1286 		prev = &fg->node.list;
1287 	}
1288 
1289 	if (candidate_index + group_size > ft->max_fte)
1290 		return ERR_PTR(-ENOSPC);
1291 
1292 	fg = alloc_insert_flow_group(ft,
1293 				     spec->match_criteria_enable,
1294 				     spec->match_criteria,
1295 				     candidate_index,
1296 				     candidate_index + group_size - 1,
1297 				     prev);
1298 	if (IS_ERR(fg))
1299 		goto out;
1300 
1301 	ft->autogroup.num_groups++;
1302 
1303 out:
1304 	return fg;
1305 }
1306 
create_auto_flow_group(struct mlx5_flow_table * ft,struct mlx5_flow_group * fg)1307 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1308 				  struct mlx5_flow_group *fg)
1309 {
1310 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1311 	struct mlx5_core_dev *dev = get_dev(&ft->node);
1312 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1313 	void *match_criteria_addr;
1314 	u8 src_esw_owner_mask_on;
1315 	void *misc;
1316 	int err;
1317 	u32 *in;
1318 
1319 	in = kvzalloc(inlen, GFP_KERNEL);
1320 	if (!in)
1321 		return -ENOMEM;
1322 
1323 	MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1324 		 fg->mask.match_criteria_enable);
1325 	MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1326 	MLX5_SET(create_flow_group_in, in, end_flow_index,   fg->start_index +
1327 		 fg->max_ftes - 1);
1328 
1329 	misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1330 			    misc_parameters);
1331 	src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1332 					 source_eswitch_owner_vhca_id);
1333 	MLX5_SET(create_flow_group_in, in,
1334 		 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1335 
1336 	match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1337 					   in, match_criteria);
1338 	memcpy(match_criteria_addr, fg->mask.match_criteria,
1339 	       sizeof(fg->mask.match_criteria));
1340 
1341 	err = root->cmds->create_flow_group(dev, ft, in, &fg->id);
1342 	if (!err) {
1343 		fg->node.active = true;
1344 		trace_mlx5_fs_add_fg(fg);
1345 	}
1346 
1347 	kvfree(in);
1348 	return err;
1349 }
1350 
mlx5_flow_dests_cmp(struct mlx5_flow_destination * d1,struct mlx5_flow_destination * d2)1351 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1352 				struct mlx5_flow_destination *d2)
1353 {
1354 	if (d1->type == d2->type) {
1355 		if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1356 		     d1->vport.num == d2->vport.num) ||
1357 		    (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1358 		     d1->ft == d2->ft) ||
1359 		    (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1360 		     d1->tir_num == d2->tir_num) ||
1361 		    (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1362 		     d1->ft_num == d2->ft_num))
1363 			return true;
1364 	}
1365 
1366 	return false;
1367 }
1368 
find_flow_rule(struct fs_fte * fte,struct mlx5_flow_destination * dest)1369 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1370 					     struct mlx5_flow_destination *dest)
1371 {
1372 	struct mlx5_flow_rule *rule;
1373 
1374 	list_for_each_entry(rule, &fte->node.children, node.list) {
1375 		if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1376 			return rule;
1377 	}
1378 	return NULL;
1379 }
1380 
check_conflicting_actions(u32 action1,u32 action2)1381 static bool check_conflicting_actions(u32 action1, u32 action2)
1382 {
1383 	u32 xored_actions = action1 ^ action2;
1384 
1385 	/* if one rule only wants to count, it's ok */
1386 	if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1387 	    action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1388 		return false;
1389 
1390 	if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP  |
1391 			     MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1392 			     MLX5_FLOW_CONTEXT_ACTION_DECAP |
1393 			     MLX5_FLOW_CONTEXT_ACTION_MOD_HDR  |
1394 			     MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1395 			     MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1396 			     MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1397 			     MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
1398 		return true;
1399 
1400 	return false;
1401 }
1402 
check_conflicting_ftes(struct fs_fte * fte,const struct mlx5_flow_act * flow_act)1403 static int check_conflicting_ftes(struct fs_fte *fte, const struct mlx5_flow_act *flow_act)
1404 {
1405 	if (check_conflicting_actions(flow_act->action, fte->action.action)) {
1406 		mlx5_core_warn(get_dev(&fte->node),
1407 			       "Found two FTEs with conflicting actions\n");
1408 		return -EEXIST;
1409 	}
1410 
1411 	if (flow_act->has_flow_tag &&
1412 	    fte->action.flow_tag != flow_act->flow_tag) {
1413 		mlx5_core_warn(get_dev(&fte->node),
1414 			       "FTE flow tag %u already exists with different flow tag %u\n",
1415 			       fte->action.flow_tag,
1416 			       flow_act->flow_tag);
1417 		return -EEXIST;
1418 	}
1419 
1420 	return 0;
1421 }
1422 
add_rule_fg(struct mlx5_flow_group * fg,u32 * match_value,struct mlx5_flow_act * flow_act,struct mlx5_flow_destination * dest,int dest_num,struct fs_fte * fte)1423 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1424 					    u32 *match_value,
1425 					    struct mlx5_flow_act *flow_act,
1426 					    struct mlx5_flow_destination *dest,
1427 					    int dest_num,
1428 					    struct fs_fte *fte)
1429 {
1430 	struct mlx5_flow_handle *handle;
1431 	int old_action;
1432 	int i;
1433 	int ret;
1434 
1435 	ret = check_conflicting_ftes(fte, flow_act);
1436 	if (ret)
1437 		return ERR_PTR(ret);
1438 
1439 	old_action = fte->action.action;
1440 	fte->action.action |= flow_act->action;
1441 	handle = add_rule_fte(fte, fg, dest, dest_num,
1442 			      old_action != flow_act->action);
1443 	if (IS_ERR(handle)) {
1444 		fte->action.action = old_action;
1445 		return handle;
1446 	}
1447 	trace_mlx5_fs_set_fte(fte, false);
1448 
1449 	for (i = 0; i < handle->num_rules; i++) {
1450 		if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1451 			tree_add_node(&handle->rule[i]->node, &fte->node);
1452 			trace_mlx5_fs_add_rule(handle->rule[i]);
1453 		}
1454 	}
1455 	return handle;
1456 }
1457 
mlx5_flow_rule_counter(struct mlx5_flow_handle * handle)1458 struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_handle *handle)
1459 {
1460 	struct mlx5_flow_rule *dst;
1461 	struct fs_fte *fte;
1462 
1463 	fs_get_obj(fte, handle->rule[0]->node.parent);
1464 
1465 	fs_for_each_dst(dst, fte) {
1466 		if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
1467 			return dst->dest_attr.counter;
1468 	}
1469 
1470 	return NULL;
1471 }
1472 
counter_is_valid(struct mlx5_fc * counter,u32 action)1473 static bool counter_is_valid(struct mlx5_fc *counter, u32 action)
1474 {
1475 	if (!(action & MLX5_FLOW_CONTEXT_ACTION_COUNT))
1476 		return !counter;
1477 
1478 	if (!counter)
1479 		return false;
1480 
1481 	return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1482 			  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1483 }
1484 
dest_is_valid(struct mlx5_flow_destination * dest,u32 action,struct mlx5_flow_table * ft)1485 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1486 			  u32 action,
1487 			  struct mlx5_flow_table *ft)
1488 {
1489 	if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1490 		return counter_is_valid(dest->counter, action);
1491 
1492 	if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1493 		return true;
1494 
1495 	if (!dest || ((dest->type ==
1496 	    MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1497 	    (dest->ft->level <= ft->level)))
1498 		return false;
1499 	return true;
1500 }
1501 
1502 struct match_list {
1503 	struct list_head	list;
1504 	struct mlx5_flow_group *g;
1505 };
1506 
1507 struct match_list_head {
1508 	struct list_head  list;
1509 	struct match_list first;
1510 };
1511 
free_match_list(struct match_list_head * head)1512 static void free_match_list(struct match_list_head *head)
1513 {
1514 	if (!list_empty(&head->list)) {
1515 		struct match_list *iter, *match_tmp;
1516 
1517 		list_del(&head->first.list);
1518 		tree_put_node(&head->first.g->node);
1519 		list_for_each_entry_safe(iter, match_tmp, &head->list,
1520 					 list) {
1521 			tree_put_node(&iter->g->node);
1522 			list_del(&iter->list);
1523 			kfree(iter);
1524 		}
1525 	}
1526 }
1527 
build_match_list(struct match_list_head * match_head,struct mlx5_flow_table * ft,struct mlx5_flow_spec * spec)1528 static int build_match_list(struct match_list_head *match_head,
1529 			    struct mlx5_flow_table *ft,
1530 			    struct mlx5_flow_spec *spec)
1531 {
1532 	struct rhlist_head *tmp, *list;
1533 	struct mlx5_flow_group *g;
1534 	int err = 0;
1535 
1536 	rcu_read_lock();
1537 	INIT_LIST_HEAD(&match_head->list);
1538 	/* Collect all fgs which has a matching match_criteria */
1539 	list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1540 	/* RCU is atomic, we can't execute FW commands here */
1541 	rhl_for_each_entry_rcu(g, tmp, list, hash) {
1542 		struct match_list *curr_match;
1543 
1544 		if (likely(list_empty(&match_head->list))) {
1545 			if (!tree_get_node(&g->node))
1546 				continue;
1547 			match_head->first.g = g;
1548 			list_add_tail(&match_head->first.list,
1549 				      &match_head->list);
1550 			continue;
1551 		}
1552 
1553 		curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1554 		if (!curr_match) {
1555 			free_match_list(match_head);
1556 			err = -ENOMEM;
1557 			goto out;
1558 		}
1559 		if (!tree_get_node(&g->node)) {
1560 			kfree(curr_match);
1561 			continue;
1562 		}
1563 		curr_match->g = g;
1564 		list_add_tail(&curr_match->list, &match_head->list);
1565 	}
1566 out:
1567 	rcu_read_unlock();
1568 	return err;
1569 }
1570 
matched_fgs_get_version(struct list_head * match_head)1571 static u64 matched_fgs_get_version(struct list_head *match_head)
1572 {
1573 	struct match_list *iter;
1574 	u64 version = 0;
1575 
1576 	list_for_each_entry(iter, match_head, list)
1577 		version += (u64)atomic_read(&iter->g->node.version);
1578 	return version;
1579 }
1580 
1581 static struct fs_fte *
lookup_fte_locked(struct mlx5_flow_group * g,u32 * match_value,bool take_write)1582 lookup_fte_locked(struct mlx5_flow_group *g,
1583 		  u32 *match_value,
1584 		  bool take_write)
1585 {
1586 	struct fs_fte *fte_tmp;
1587 
1588 	if (take_write)
1589 		nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1590 	else
1591 		nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
1592 	fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
1593 					 rhash_fte);
1594 	if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1595 		fte_tmp = NULL;
1596 		goto out;
1597 	}
1598 
1599 	nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1600 out:
1601 	if (take_write)
1602 		up_write_ref_node(&g->node);
1603 	else
1604 		up_read_ref_node(&g->node);
1605 	return fte_tmp;
1606 }
1607 
1608 static struct mlx5_flow_handle *
try_add_to_existing_fg(struct mlx5_flow_table * ft,struct list_head * match_head,struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act,struct mlx5_flow_destination * dest,int dest_num,int ft_version)1609 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1610 		       struct list_head *match_head,
1611 		       struct mlx5_flow_spec *spec,
1612 		       struct mlx5_flow_act *flow_act,
1613 		       struct mlx5_flow_destination *dest,
1614 		       int dest_num,
1615 		       int ft_version)
1616 {
1617 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
1618 	struct mlx5_flow_group *g;
1619 	struct mlx5_flow_handle *rule;
1620 	struct match_list *iter;
1621 	bool take_write = false;
1622 	struct fs_fte *fte;
1623 	u64  version;
1624 	int err;
1625 
1626 	fte = alloc_fte(ft, spec->match_value, flow_act);
1627 	if (IS_ERR(fte))
1628 		return  ERR_PTR(-ENOMEM);
1629 
1630 search_again_locked:
1631 	version = matched_fgs_get_version(match_head);
1632 	/* Try to find a fg that already contains a matching fte */
1633 	list_for_each_entry(iter, match_head, list) {
1634 		struct fs_fte *fte_tmp;
1635 
1636 		g = iter->g;
1637 		fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1638 		if (!fte_tmp)
1639 			continue;
1640 		rule = add_rule_fg(g, spec->match_value,
1641 				   flow_act, dest, dest_num, fte_tmp);
1642 		up_write_ref_node(&fte_tmp->node);
1643 		tree_put_node(&fte_tmp->node);
1644 		kmem_cache_free(steering->ftes_cache, fte);
1645 		return rule;
1646 	}
1647 
1648 	/* Check the ft version, for case that new flow group
1649 	 * was added while the fgs weren't locked
1650 	 */
1651 	if (atomic_read(&ft->node.version) != ft_version) {
1652 		rule = ERR_PTR(-EAGAIN);
1653 		goto out;
1654 	}
1655 
1656 	/* Check the fgs version, for case the new FTE with the
1657 	 * same values was added while the fgs weren't locked
1658 	 */
1659 	if (version != matched_fgs_get_version(match_head)) {
1660 		take_write = true;
1661 		goto search_again_locked;
1662 	}
1663 
1664 	list_for_each_entry(iter, match_head, list) {
1665 		g = iter->g;
1666 
1667 		if (!g->node.active)
1668 			continue;
1669 
1670 		nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1671 
1672 		err = insert_fte(g, fte);
1673 		if (err) {
1674 			up_write_ref_node(&g->node);
1675 			if (err == -ENOSPC)
1676 				continue;
1677 			kmem_cache_free(steering->ftes_cache, fte);
1678 			return ERR_PTR(err);
1679 		}
1680 
1681 		nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1682 		up_write_ref_node(&g->node);
1683 		rule = add_rule_fg(g, spec->match_value,
1684 				   flow_act, dest, dest_num, fte);
1685 		up_write_ref_node(&fte->node);
1686 		tree_put_node(&fte->node);
1687 		return rule;
1688 	}
1689 	rule = ERR_PTR(-ENOENT);
1690 out:
1691 	kmem_cache_free(steering->ftes_cache, fte);
1692 	return rule;
1693 }
1694 
1695 static struct mlx5_flow_handle *
_mlx5_add_flow_rules(struct mlx5_flow_table * ft,struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act,struct mlx5_flow_destination * dest,int dest_num)1696 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1697 		     struct mlx5_flow_spec *spec,
1698 		     struct mlx5_flow_act *flow_act,
1699 		     struct mlx5_flow_destination *dest,
1700 		     int dest_num)
1701 
1702 {
1703 	struct mlx5_flow_steering *steering = get_steering(&ft->node);
1704 	struct mlx5_flow_group *g;
1705 	struct mlx5_flow_handle *rule;
1706 	struct match_list_head match_head;
1707 	bool take_write = false;
1708 	struct fs_fte *fte;
1709 	int version;
1710 	int err;
1711 	int i;
1712 
1713 	if (!check_valid_spec(spec))
1714 		return ERR_PTR(-EINVAL);
1715 
1716 	for (i = 0; i < dest_num; i++) {
1717 		if (!dest_is_valid(&dest[i], flow_act->action, ft))
1718 			return ERR_PTR(-EINVAL);
1719 	}
1720 	nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1721 search_again_locked:
1722 	version = atomic_read(&ft->node.version);
1723 
1724 	/* Collect all fgs which has a matching match_criteria */
1725 	err = build_match_list(&match_head, ft, spec);
1726 	if (err) {
1727 		if (take_write)
1728 			up_write_ref_node(&ft->node);
1729 		else
1730 			up_read_ref_node(&ft->node);
1731 		return ERR_PTR(err);
1732 	}
1733 
1734 	if (!take_write)
1735 		up_read_ref_node(&ft->node);
1736 
1737 	rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1738 				      dest_num, version);
1739 	free_match_list(&match_head);
1740 	if (!IS_ERR(rule) ||
1741 	    (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1742 		if (take_write)
1743 			up_write_ref_node(&ft->node);
1744 		return rule;
1745 	}
1746 
1747 	if (!take_write) {
1748 		nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1749 		take_write = true;
1750 	}
1751 
1752 	if (PTR_ERR(rule) == -EAGAIN ||
1753 	    version != atomic_read(&ft->node.version))
1754 		goto search_again_locked;
1755 
1756 	g = alloc_auto_flow_group(ft, spec);
1757 	if (IS_ERR(g)) {
1758 		rule = ERR_CAST(g);
1759 		up_write_ref_node(&ft->node);
1760 		return rule;
1761 	}
1762 
1763 	nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1764 	up_write_ref_node(&ft->node);
1765 
1766 	err = create_auto_flow_group(ft, g);
1767 	if (err)
1768 		goto err_release_fg;
1769 
1770 	fte = alloc_fte(ft, spec->match_value, flow_act);
1771 	if (IS_ERR(fte)) {
1772 		err = PTR_ERR(fte);
1773 		goto err_release_fg;
1774 	}
1775 
1776 	err = insert_fte(g, fte);
1777 	if (err) {
1778 		kmem_cache_free(steering->ftes_cache, fte);
1779 		goto err_release_fg;
1780 	}
1781 
1782 	nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1783 	up_write_ref_node(&g->node);
1784 	rule = add_rule_fg(g, spec->match_value, flow_act, dest,
1785 			   dest_num, fte);
1786 	up_write_ref_node(&fte->node);
1787 	tree_put_node(&fte->node);
1788 	tree_put_node(&g->node);
1789 	return rule;
1790 
1791 err_release_fg:
1792 	up_write_ref_node(&g->node);
1793 	tree_put_node(&g->node);
1794 	return ERR_PTR(err);
1795 }
1796 
fwd_next_prio_supported(struct mlx5_flow_table * ft)1797 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1798 {
1799 	return ((ft->type == FS_FT_NIC_RX) &&
1800 		(MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1801 }
1802 
1803 struct mlx5_flow_handle *
mlx5_add_flow_rules(struct mlx5_flow_table * ft,struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act,struct mlx5_flow_destination * dest,int num_dest)1804 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1805 		    struct mlx5_flow_spec *spec,
1806 		    struct mlx5_flow_act *flow_act,
1807 		    struct mlx5_flow_destination *dest,
1808 		    int num_dest)
1809 {
1810 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1811 	struct mlx5_flow_destination gen_dest = {};
1812 	struct mlx5_flow_table *next_ft = NULL;
1813 	struct mlx5_flow_handle *handle = NULL;
1814 	u32 sw_action = flow_act->action;
1815 	struct fs_prio *prio;
1816 
1817 	fs_get_obj(prio, ft->node.parent);
1818 	if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1819 		if (!fwd_next_prio_supported(ft))
1820 			return ERR_PTR(-EOPNOTSUPP);
1821 		if (num_dest)
1822 			return ERR_PTR(-EINVAL);
1823 		mutex_lock(&root->chain_lock);
1824 		next_ft = find_next_chained_ft(prio);
1825 		if (next_ft) {
1826 			gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1827 			gen_dest.ft = next_ft;
1828 			dest = &gen_dest;
1829 			num_dest = 1;
1830 			flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1831 		} else {
1832 			mutex_unlock(&root->chain_lock);
1833 			return ERR_PTR(-EOPNOTSUPP);
1834 		}
1835 	}
1836 
1837 	handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1838 
1839 	if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1840 		if (!IS_ERR_OR_NULL(handle) &&
1841 		    (list_empty(&handle->rule[0]->next_ft))) {
1842 			mutex_lock(&next_ft->lock);
1843 			list_add(&handle->rule[0]->next_ft,
1844 				 &next_ft->fwd_rules);
1845 			mutex_unlock(&next_ft->lock);
1846 			handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1847 		}
1848 		mutex_unlock(&root->chain_lock);
1849 	}
1850 	return handle;
1851 }
1852 EXPORT_SYMBOL(mlx5_add_flow_rules);
1853 
mlx5_del_flow_rules(struct mlx5_flow_handle * handle)1854 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1855 {
1856 	int i;
1857 
1858 	for (i = handle->num_rules - 1; i >= 0; i--)
1859 		tree_remove_node(&handle->rule[i]->node);
1860 	kfree(handle);
1861 }
1862 EXPORT_SYMBOL(mlx5_del_flow_rules);
1863 
1864 /* Assuming prio->node.children(flow tables) is sorted by level */
find_next_ft(struct mlx5_flow_table * ft)1865 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1866 {
1867 	struct fs_prio *prio;
1868 
1869 	fs_get_obj(prio, ft->node.parent);
1870 
1871 	if (!list_is_last(&ft->node.list, &prio->node.children))
1872 		return list_next_entry(ft, node.list);
1873 	return find_next_chained_ft(prio);
1874 }
1875 
update_root_ft_destroy(struct mlx5_flow_table * ft)1876 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1877 {
1878 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1879 	struct mlx5_ft_underlay_qp *uqp;
1880 	struct mlx5_flow_table *new_root_ft = NULL;
1881 	int err = 0;
1882 	u32 qpn;
1883 
1884 	if (root->root_ft != ft)
1885 		return 0;
1886 
1887 	new_root_ft = find_next_ft(ft);
1888 	if (!new_root_ft) {
1889 		root->root_ft = NULL;
1890 		return 0;
1891 	}
1892 
1893 	if (list_empty(&root->underlay_qpns)) {
1894 		/* Don't set any QPN (zero) in case QPN list is empty */
1895 		qpn = 0;
1896 		err = root->cmds->update_root_ft(root->dev, new_root_ft,
1897 						 qpn, false);
1898 	} else {
1899 		list_for_each_entry(uqp, &root->underlay_qpns, list) {
1900 			qpn = uqp->qpn;
1901 			err = root->cmds->update_root_ft(root->dev,
1902 							 new_root_ft, qpn,
1903 							 false);
1904 			if (err)
1905 				break;
1906 		}
1907 	}
1908 
1909 	if (err)
1910 		mlx5_core_warn(root->dev,
1911 			       "Update root flow table of id(%u) qpn(%d) failed\n",
1912 			       ft->id, qpn);
1913 	else
1914 		root->root_ft = new_root_ft;
1915 
1916 	return 0;
1917 }
1918 
1919 /* Connect flow table from previous priority to
1920  * the next flow table.
1921  */
disconnect_flow_table(struct mlx5_flow_table * ft)1922 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1923 {
1924 	struct mlx5_core_dev *dev = get_dev(&ft->node);
1925 	struct mlx5_flow_table *next_ft;
1926 	struct fs_prio *prio;
1927 	int err = 0;
1928 
1929 	err = update_root_ft_destroy(ft);
1930 	if (err)
1931 		return err;
1932 
1933 	fs_get_obj(prio, ft->node.parent);
1934 	if  (!(list_first_entry(&prio->node.children,
1935 				struct mlx5_flow_table,
1936 				node.list) == ft))
1937 		return 0;
1938 
1939 	next_ft = find_next_chained_ft(prio);
1940 	err = connect_fwd_rules(dev, next_ft, ft);
1941 	if (err)
1942 		return err;
1943 
1944 	err = connect_prev_fts(dev, next_ft, prio);
1945 	if (err)
1946 		mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1947 			       ft->id);
1948 	return err;
1949 }
1950 
mlx5_destroy_flow_table(struct mlx5_flow_table * ft)1951 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1952 {
1953 	struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1954 	int err = 0;
1955 
1956 	mutex_lock(&root->chain_lock);
1957 	err = disconnect_flow_table(ft);
1958 	if (err) {
1959 		mutex_unlock(&root->chain_lock);
1960 		return err;
1961 	}
1962 	if (tree_remove_node(&ft->node))
1963 		mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1964 			       ft->id);
1965 	mutex_unlock(&root->chain_lock);
1966 
1967 	return err;
1968 }
1969 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1970 
mlx5_destroy_flow_group(struct mlx5_flow_group * fg)1971 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1972 {
1973 	if (tree_remove_node(&fg->node))
1974 		mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1975 			       fg->id);
1976 }
1977 
mlx5_get_flow_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type)1978 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1979 						    enum mlx5_flow_namespace_type type)
1980 {
1981 	struct mlx5_flow_steering *steering = dev->priv.steering;
1982 	struct mlx5_flow_root_namespace *root_ns;
1983 	int prio;
1984 	struct fs_prio *fs_prio;
1985 	struct mlx5_flow_namespace *ns;
1986 
1987 	if (!steering)
1988 		return NULL;
1989 
1990 	switch (type) {
1991 	case MLX5_FLOW_NAMESPACE_BYPASS:
1992 	case MLX5_FLOW_NAMESPACE_LAG:
1993 	case MLX5_FLOW_NAMESPACE_OFFLOADS:
1994 	case MLX5_FLOW_NAMESPACE_ETHTOOL:
1995 	case MLX5_FLOW_NAMESPACE_KERNEL:
1996 	case MLX5_FLOW_NAMESPACE_LEFTOVERS:
1997 	case MLX5_FLOW_NAMESPACE_ANCHOR:
1998 		prio = type;
1999 		break;
2000 	case MLX5_FLOW_NAMESPACE_FDB:
2001 		if (steering->fdb_root_ns)
2002 			return &steering->fdb_root_ns->ns;
2003 		else
2004 			return NULL;
2005 	case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2006 		if (steering->sniffer_rx_root_ns)
2007 			return &steering->sniffer_rx_root_ns->ns;
2008 		else
2009 			return NULL;
2010 	case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2011 		if (steering->sniffer_tx_root_ns)
2012 			return &steering->sniffer_tx_root_ns->ns;
2013 		else
2014 			return NULL;
2015 	case MLX5_FLOW_NAMESPACE_EGRESS:
2016 		if (steering->egress_root_ns)
2017 			return &steering->egress_root_ns->ns;
2018 		else
2019 			return NULL;
2020 	default:
2021 		return NULL;
2022 	}
2023 
2024 	root_ns = steering->root_ns;
2025 	if (!root_ns)
2026 		return NULL;
2027 
2028 	fs_prio = find_prio(&root_ns->ns, prio);
2029 	if (!fs_prio)
2030 		return NULL;
2031 
2032 	ns = list_first_entry(&fs_prio->node.children,
2033 			      typeof(*ns),
2034 			      node.list);
2035 
2036 	return ns;
2037 }
2038 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2039 
mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type,int vport)2040 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2041 							      enum mlx5_flow_namespace_type type,
2042 							      int vport)
2043 {
2044 	struct mlx5_flow_steering *steering = dev->priv.steering;
2045 
2046 	if (!steering || vport >= MLX5_TOTAL_VPORTS(dev))
2047 		return NULL;
2048 
2049 	switch (type) {
2050 	case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2051 		if (steering->esw_egress_root_ns &&
2052 		    steering->esw_egress_root_ns[vport])
2053 			return &steering->esw_egress_root_ns[vport]->ns;
2054 		else
2055 			return NULL;
2056 	case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2057 		if (steering->esw_ingress_root_ns &&
2058 		    steering->esw_ingress_root_ns[vport])
2059 			return &steering->esw_ingress_root_ns[vport]->ns;
2060 		else
2061 			return NULL;
2062 	default:
2063 		return NULL;
2064 	}
2065 }
2066 
fs_create_prio(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels)2067 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2068 				      unsigned int prio, int num_levels)
2069 {
2070 	struct fs_prio *fs_prio;
2071 
2072 	fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2073 	if (!fs_prio)
2074 		return ERR_PTR(-ENOMEM);
2075 
2076 	fs_prio->node.type = FS_TYPE_PRIO;
2077 	tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2078 	tree_add_node(&fs_prio->node, &ns->node);
2079 	fs_prio->num_levels = num_levels;
2080 	fs_prio->prio = prio;
2081 	list_add_tail(&fs_prio->node.list, &ns->node.children);
2082 
2083 	return fs_prio;
2084 }
2085 
fs_init_namespace(struct mlx5_flow_namespace * ns)2086 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2087 						     *ns)
2088 {
2089 	ns->node.type = FS_TYPE_NAMESPACE;
2090 
2091 	return ns;
2092 }
2093 
fs_create_namespace(struct fs_prio * prio)2094 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2095 {
2096 	struct mlx5_flow_namespace	*ns;
2097 
2098 	ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2099 	if (!ns)
2100 		return ERR_PTR(-ENOMEM);
2101 
2102 	fs_init_namespace(ns);
2103 	tree_init_node(&ns->node, NULL, del_sw_ns);
2104 	tree_add_node(&ns->node, &prio->node);
2105 	list_add_tail(&ns->node.list, &prio->node.children);
2106 
2107 	return ns;
2108 }
2109 
create_leaf_prios(struct mlx5_flow_namespace * ns,int prio,struct init_tree_node * prio_metadata)2110 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2111 			     struct init_tree_node *prio_metadata)
2112 {
2113 	struct fs_prio *fs_prio;
2114 	int i;
2115 
2116 	for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2117 		fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2118 		if (IS_ERR(fs_prio))
2119 			return PTR_ERR(fs_prio);
2120 	}
2121 	return 0;
2122 }
2123 
2124 #define FLOW_TABLE_BIT_SZ 1
2125 #define GET_FLOW_TABLE_CAP(dev, offset) \
2126 	((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) +	\
2127 			offset / 32)) >>					\
2128 	  (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
has_required_caps(struct mlx5_core_dev * dev,struct node_caps * caps)2129 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2130 {
2131 	int i;
2132 
2133 	for (i = 0; i < caps->arr_sz; i++) {
2134 		if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2135 			return false;
2136 	}
2137 	return true;
2138 }
2139 
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)2140 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2141 				    struct init_tree_node *init_node,
2142 				    struct fs_node *fs_parent_node,
2143 				    struct init_tree_node *init_parent_node,
2144 				    int prio)
2145 {
2146 	int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2147 					      flow_table_properties_nic_receive.
2148 					      max_ft_level);
2149 	struct mlx5_flow_namespace *fs_ns;
2150 	struct fs_prio *fs_prio;
2151 	struct fs_node *base;
2152 	int i;
2153 	int err;
2154 
2155 	if (init_node->type == FS_TYPE_PRIO) {
2156 		if ((init_node->min_ft_level > max_ft_level) ||
2157 		    !has_required_caps(steering->dev, &init_node->caps))
2158 			return 0;
2159 
2160 		fs_get_obj(fs_ns, fs_parent_node);
2161 		if (init_node->num_leaf_prios)
2162 			return create_leaf_prios(fs_ns, prio, init_node);
2163 		fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2164 		if (IS_ERR(fs_prio))
2165 			return PTR_ERR(fs_prio);
2166 		base = &fs_prio->node;
2167 	} else if (init_node->type == FS_TYPE_NAMESPACE) {
2168 		fs_get_obj(fs_prio, fs_parent_node);
2169 		fs_ns = fs_create_namespace(fs_prio);
2170 		if (IS_ERR(fs_ns))
2171 			return PTR_ERR(fs_ns);
2172 		base = &fs_ns->node;
2173 	} else {
2174 		return -EINVAL;
2175 	}
2176 	prio = 0;
2177 	for (i = 0; i < init_node->ar_size; i++) {
2178 		err = init_root_tree_recursive(steering, &init_node->children[i],
2179 					       base, init_node, prio);
2180 		if (err)
2181 			return err;
2182 		if (init_node->children[i].type == FS_TYPE_PRIO &&
2183 		    init_node->children[i].num_leaf_prios) {
2184 			prio += init_node->children[i].num_leaf_prios;
2185 		}
2186 	}
2187 
2188 	return 0;
2189 }
2190 
init_root_tree(struct mlx5_flow_steering * steering,struct init_tree_node * init_node,struct fs_node * fs_parent_node)2191 static int init_root_tree(struct mlx5_flow_steering *steering,
2192 			  struct init_tree_node *init_node,
2193 			  struct fs_node *fs_parent_node)
2194 {
2195 	int i;
2196 	struct mlx5_flow_namespace *fs_ns;
2197 	int err;
2198 
2199 	fs_get_obj(fs_ns, fs_parent_node);
2200 	for (i = 0; i < init_node->ar_size; i++) {
2201 		err = init_root_tree_recursive(steering, &init_node->children[i],
2202 					       &fs_ns->node,
2203 					       init_node, i);
2204 		if (err)
2205 			return err;
2206 	}
2207 	return 0;
2208 }
2209 
2210 static struct mlx5_flow_root_namespace
create_root_ns(struct mlx5_flow_steering * steering,enum fs_flow_table_type table_type)2211 *create_root_ns(struct mlx5_flow_steering *steering,
2212 		enum fs_flow_table_type table_type)
2213 {
2214 	const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2215 	struct mlx5_flow_root_namespace *root_ns;
2216 	struct mlx5_flow_namespace *ns;
2217 
2218 	if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2219 	    (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2220 		cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2221 
2222 	/* Create the root namespace */
2223 	root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
2224 	if (!root_ns)
2225 		return NULL;
2226 
2227 	root_ns->dev = steering->dev;
2228 	root_ns->table_type = table_type;
2229 	root_ns->cmds = cmds;
2230 
2231 	INIT_LIST_HEAD(&root_ns->underlay_qpns);
2232 
2233 	ns = &root_ns->ns;
2234 	fs_init_namespace(ns);
2235 	mutex_init(&root_ns->chain_lock);
2236 	tree_init_node(&ns->node, NULL, NULL);
2237 	tree_add_node(&ns->node, NULL);
2238 
2239 	return root_ns;
2240 }
2241 
2242 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2243 
set_prio_attrs_in_ns(struct mlx5_flow_namespace * ns,int acc_level)2244 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2245 {
2246 	struct fs_prio *prio;
2247 
2248 	fs_for_each_prio(prio, ns) {
2249 		 /* This updates prio start_level and num_levels */
2250 		set_prio_attrs_in_prio(prio, acc_level);
2251 		acc_level += prio->num_levels;
2252 	}
2253 	return acc_level;
2254 }
2255 
set_prio_attrs_in_prio(struct fs_prio * prio,int acc_level)2256 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2257 {
2258 	struct mlx5_flow_namespace *ns;
2259 	int acc_level_ns = acc_level;
2260 
2261 	prio->start_level = acc_level;
2262 	fs_for_each_ns(ns, prio)
2263 		/* This updates start_level and num_levels of ns's priority descendants */
2264 		acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2265 	if (!prio->num_levels)
2266 		prio->num_levels = acc_level_ns - prio->start_level;
2267 	WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2268 }
2269 
set_prio_attrs(struct mlx5_flow_root_namespace * root_ns)2270 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2271 {
2272 	struct mlx5_flow_namespace *ns = &root_ns->ns;
2273 	struct fs_prio *prio;
2274 	int start_level = 0;
2275 
2276 	fs_for_each_prio(prio, ns) {
2277 		set_prio_attrs_in_prio(prio, start_level);
2278 		start_level += prio->num_levels;
2279 	}
2280 }
2281 
2282 #define ANCHOR_PRIO 0
2283 #define ANCHOR_SIZE 1
2284 #define ANCHOR_LEVEL 0
create_anchor_flow_table(struct mlx5_flow_steering * steering)2285 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2286 {
2287 	struct mlx5_flow_namespace *ns = NULL;
2288 	struct mlx5_flow_table_attr ft_attr = {};
2289 	struct mlx5_flow_table *ft;
2290 
2291 	ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2292 	if (WARN_ON(!ns))
2293 		return -EINVAL;
2294 
2295 	ft_attr.max_fte = ANCHOR_SIZE;
2296 	ft_attr.level   = ANCHOR_LEVEL;
2297 	ft_attr.prio    = ANCHOR_PRIO;
2298 
2299 	ft = mlx5_create_flow_table(ns, &ft_attr);
2300 	if (IS_ERR(ft)) {
2301 		mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2302 		return PTR_ERR(ft);
2303 	}
2304 	return 0;
2305 }
2306 
init_root_ns(struct mlx5_flow_steering * steering)2307 static int init_root_ns(struct mlx5_flow_steering *steering)
2308 {
2309 	int err;
2310 
2311 	steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2312 	if (!steering->root_ns)
2313 		return -ENOMEM;
2314 
2315 	err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2316 	if (err)
2317 		goto out_err;
2318 
2319 	set_prio_attrs(steering->root_ns);
2320 	err = create_anchor_flow_table(steering);
2321 	if (err)
2322 		goto out_err;
2323 
2324 	return 0;
2325 
2326 out_err:
2327 	cleanup_root_ns(steering->root_ns);
2328 	steering->root_ns = NULL;
2329 	return err;
2330 }
2331 
clean_tree(struct fs_node * node)2332 static void clean_tree(struct fs_node *node)
2333 {
2334 	if (node) {
2335 		struct fs_node *iter;
2336 		struct fs_node *temp;
2337 
2338 		tree_get_node(node);
2339 		list_for_each_entry_safe(iter, temp, &node->children, list)
2340 			clean_tree(iter);
2341 		tree_put_node(node);
2342 		tree_remove_node(node);
2343 	}
2344 }
2345 
cleanup_root_ns(struct mlx5_flow_root_namespace * root_ns)2346 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2347 {
2348 	if (!root_ns)
2349 		return;
2350 
2351 	clean_tree(&root_ns->ns.node);
2352 }
2353 
cleanup_egress_acls_root_ns(struct mlx5_core_dev * dev)2354 static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2355 {
2356 	struct mlx5_flow_steering *steering = dev->priv.steering;
2357 	int i;
2358 
2359 	if (!steering->esw_egress_root_ns)
2360 		return;
2361 
2362 	for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2363 		cleanup_root_ns(steering->esw_egress_root_ns[i]);
2364 
2365 	kfree(steering->esw_egress_root_ns);
2366 }
2367 
cleanup_ingress_acls_root_ns(struct mlx5_core_dev * dev)2368 static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2369 {
2370 	struct mlx5_flow_steering *steering = dev->priv.steering;
2371 	int i;
2372 
2373 	if (!steering->esw_ingress_root_ns)
2374 		return;
2375 
2376 	for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2377 		cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2378 
2379 	kfree(steering->esw_ingress_root_ns);
2380 }
2381 
mlx5_cleanup_fs(struct mlx5_core_dev * dev)2382 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2383 {
2384 	struct mlx5_flow_steering *steering = dev->priv.steering;
2385 
2386 	cleanup_root_ns(steering->root_ns);
2387 	cleanup_egress_acls_root_ns(dev);
2388 	cleanup_ingress_acls_root_ns(dev);
2389 	cleanup_root_ns(steering->fdb_root_ns);
2390 	cleanup_root_ns(steering->sniffer_rx_root_ns);
2391 	cleanup_root_ns(steering->sniffer_tx_root_ns);
2392 	cleanup_root_ns(steering->egress_root_ns);
2393 	mlx5_cleanup_fc_stats(dev);
2394 	kmem_cache_destroy(steering->ftes_cache);
2395 	kmem_cache_destroy(steering->fgs_cache);
2396 	kfree(steering);
2397 }
2398 
init_sniffer_tx_root_ns(struct mlx5_flow_steering * steering)2399 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2400 {
2401 	struct fs_prio *prio;
2402 
2403 	steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2404 	if (!steering->sniffer_tx_root_ns)
2405 		return -ENOMEM;
2406 
2407 	/* Create single prio */
2408 	prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2409 	if (IS_ERR(prio)) {
2410 		cleanup_root_ns(steering->sniffer_tx_root_ns);
2411 		return PTR_ERR(prio);
2412 	}
2413 	return 0;
2414 }
2415 
init_sniffer_rx_root_ns(struct mlx5_flow_steering * steering)2416 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2417 {
2418 	struct fs_prio *prio;
2419 
2420 	steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2421 	if (!steering->sniffer_rx_root_ns)
2422 		return -ENOMEM;
2423 
2424 	/* Create single prio */
2425 	prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2426 	if (IS_ERR(prio)) {
2427 		cleanup_root_ns(steering->sniffer_rx_root_ns);
2428 		return PTR_ERR(prio);
2429 	}
2430 	return 0;
2431 }
2432 
init_fdb_root_ns(struct mlx5_flow_steering * steering)2433 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2434 {
2435 	struct fs_prio *prio;
2436 
2437 	steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2438 	if (!steering->fdb_root_ns)
2439 		return -ENOMEM;
2440 
2441 	prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 2);
2442 	if (IS_ERR(prio))
2443 		goto out_err;
2444 
2445 	prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2446 	if (IS_ERR(prio))
2447 		goto out_err;
2448 
2449 	set_prio_attrs(steering->fdb_root_ns);
2450 	return 0;
2451 
2452 out_err:
2453 	cleanup_root_ns(steering->fdb_root_ns);
2454 	steering->fdb_root_ns = NULL;
2455 	return PTR_ERR(prio);
2456 }
2457 
init_egress_acl_root_ns(struct mlx5_flow_steering * steering,int vport)2458 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2459 {
2460 	struct fs_prio *prio;
2461 
2462 	steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2463 	if (!steering->esw_egress_root_ns[vport])
2464 		return -ENOMEM;
2465 
2466 	/* create 1 prio*/
2467 	prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2468 	return PTR_ERR_OR_ZERO(prio);
2469 }
2470 
init_ingress_acl_root_ns(struct mlx5_flow_steering * steering,int vport)2471 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2472 {
2473 	struct fs_prio *prio;
2474 
2475 	steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2476 	if (!steering->esw_ingress_root_ns[vport])
2477 		return -ENOMEM;
2478 
2479 	/* create 1 prio*/
2480 	prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2481 	return PTR_ERR_OR_ZERO(prio);
2482 }
2483 
init_egress_acls_root_ns(struct mlx5_core_dev * dev)2484 static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2485 {
2486 	struct mlx5_flow_steering *steering = dev->priv.steering;
2487 	int err;
2488 	int i;
2489 
2490 	steering->esw_egress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2491 					       sizeof(*steering->esw_egress_root_ns),
2492 					       GFP_KERNEL);
2493 	if (!steering->esw_egress_root_ns)
2494 		return -ENOMEM;
2495 
2496 	for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2497 		err = init_egress_acl_root_ns(steering, i);
2498 		if (err)
2499 			goto cleanup_root_ns;
2500 	}
2501 
2502 	return 0;
2503 
2504 cleanup_root_ns:
2505 	for (i--; i >= 0; i--)
2506 		cleanup_root_ns(steering->esw_egress_root_ns[i]);
2507 	kfree(steering->esw_egress_root_ns);
2508 	return err;
2509 }
2510 
init_ingress_acls_root_ns(struct mlx5_core_dev * dev)2511 static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2512 {
2513 	struct mlx5_flow_steering *steering = dev->priv.steering;
2514 	int err;
2515 	int i;
2516 
2517 	steering->esw_ingress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2518 						sizeof(*steering->esw_ingress_root_ns),
2519 						GFP_KERNEL);
2520 	if (!steering->esw_ingress_root_ns)
2521 		return -ENOMEM;
2522 
2523 	for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2524 		err = init_ingress_acl_root_ns(steering, i);
2525 		if (err)
2526 			goto cleanup_root_ns;
2527 	}
2528 
2529 	return 0;
2530 
2531 cleanup_root_ns:
2532 	for (i--; i >= 0; i--)
2533 		cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2534 	kfree(steering->esw_ingress_root_ns);
2535 	return err;
2536 }
2537 
init_egress_root_ns(struct mlx5_flow_steering * steering)2538 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2539 {
2540 	struct fs_prio *prio;
2541 
2542 	steering->egress_root_ns = create_root_ns(steering,
2543 						  FS_FT_NIC_TX);
2544 	if (!steering->egress_root_ns)
2545 		return -ENOMEM;
2546 
2547 	/* create 1 prio*/
2548 	prio = fs_create_prio(&steering->egress_root_ns->ns, 0, 1);
2549 	return PTR_ERR_OR_ZERO(prio);
2550 }
2551 
mlx5_init_fs(struct mlx5_core_dev * dev)2552 int mlx5_init_fs(struct mlx5_core_dev *dev)
2553 {
2554 	struct mlx5_flow_steering *steering;
2555 	int err = 0;
2556 
2557 	err = mlx5_init_fc_stats(dev);
2558 	if (err)
2559 		return err;
2560 
2561 	steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2562 	if (!steering)
2563 		return -ENOMEM;
2564 	steering->dev = dev;
2565 	dev->priv.steering = steering;
2566 
2567 	steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2568 						sizeof(struct mlx5_flow_group), 0,
2569 						0, NULL);
2570 	steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2571 						 0, NULL);
2572 	if (!steering->ftes_cache || !steering->fgs_cache) {
2573 		err = -ENOMEM;
2574 		goto err;
2575 	}
2576 
2577 	if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2578 	      (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2579 	     ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2580 	      MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2581 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2582 		err = init_root_ns(steering);
2583 		if (err)
2584 			goto err;
2585 	}
2586 
2587 	if (MLX5_ESWITCH_MANAGER(dev)) {
2588 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2589 			err = init_fdb_root_ns(steering);
2590 			if (err)
2591 				goto err;
2592 		}
2593 		if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2594 			err = init_egress_acls_root_ns(dev);
2595 			if (err)
2596 				goto err;
2597 		}
2598 		if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2599 			err = init_ingress_acls_root_ns(dev);
2600 			if (err)
2601 				goto err;
2602 		}
2603 	}
2604 
2605 	if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2606 		err = init_sniffer_rx_root_ns(steering);
2607 		if (err)
2608 			goto err;
2609 	}
2610 
2611 	if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2612 		err = init_sniffer_tx_root_ns(steering);
2613 		if (err)
2614 			goto err;
2615 	}
2616 
2617 	if (MLX5_IPSEC_DEV(dev)) {
2618 		err = init_egress_root_ns(steering);
2619 		if (err)
2620 			goto err;
2621 	}
2622 
2623 	return 0;
2624 err:
2625 	mlx5_cleanup_fs(dev);
2626 	return err;
2627 }
2628 
mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)2629 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2630 {
2631 	struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2632 	struct mlx5_ft_underlay_qp *new_uqp;
2633 	int err = 0;
2634 
2635 	new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2636 	if (!new_uqp)
2637 		return -ENOMEM;
2638 
2639 	mutex_lock(&root->chain_lock);
2640 
2641 	if (!root->root_ft) {
2642 		err = -EINVAL;
2643 		goto update_ft_fail;
2644 	}
2645 
2646 	err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2647 					 false);
2648 	if (err) {
2649 		mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2650 			       underlay_qpn, err);
2651 		goto update_ft_fail;
2652 	}
2653 
2654 	new_uqp->qpn = underlay_qpn;
2655 	list_add_tail(&new_uqp->list, &root->underlay_qpns);
2656 
2657 	mutex_unlock(&root->chain_lock);
2658 
2659 	return 0;
2660 
2661 update_ft_fail:
2662 	mutex_unlock(&root->chain_lock);
2663 	kfree(new_uqp);
2664 	return err;
2665 }
2666 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2667 
mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)2668 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2669 {
2670 	struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2671 	struct mlx5_ft_underlay_qp *uqp;
2672 	bool found = false;
2673 	int err = 0;
2674 
2675 	mutex_lock(&root->chain_lock);
2676 	list_for_each_entry(uqp, &root->underlay_qpns, list) {
2677 		if (uqp->qpn == underlay_qpn) {
2678 			found = true;
2679 			break;
2680 		}
2681 	}
2682 
2683 	if (!found) {
2684 		mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2685 			       underlay_qpn);
2686 		err = -EINVAL;
2687 		goto out;
2688 	}
2689 
2690 	err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2691 					 true);
2692 	if (err)
2693 		mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2694 			       underlay_qpn, err);
2695 
2696 	list_del(&uqp->list);
2697 	mutex_unlock(&root->chain_lock);
2698 	kfree(uqp);
2699 
2700 	return 0;
2701 
2702 out:
2703 	mutex_unlock(&root->chain_lock);
2704 	return err;
2705 }
2706 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
2707