2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/mutex.h>
34 #include <linux/mlx5/driver.h>
36 #include "mlx5_core.h"
40 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
41 sizeof(struct init_tree_node))
43 #define ADD_PRIO(num_prios_val, min_level_val, max_ft_val, caps_val,\
44 ...) {.type = FS_TYPE_PRIO,\
45 .min_ft_level = min_level_val,\
46 .max_ft = max_ft_val,\
47 .num_leaf_prios = num_prios_val,\
49 .children = (struct init_tree_node[]) {__VA_ARGS__},\
50 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
53 #define ADD_MULTIPLE_PRIO(num_prios_val, max_ft_val, ...)\
54 ADD_PRIO(num_prios_val, 0, max_ft_val, {},\
57 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
58 .children = (struct init_tree_node[]) {__VA_ARGS__},\
59 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
62 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
65 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
67 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
68 .caps = (long[]) {__VA_ARGS__} }
70 #define LEFTOVERS_MAX_FT 1
71 #define LEFTOVERS_NUM_PRIOS 1
72 #define BY_PASS_PRIO_MAX_FT 1
73 #define BY_PASS_MIN_LEVEL (KENREL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
76 #define KERNEL_MAX_FT 3
77 #define KERNEL_NUM_PRIOS 2
78 #define KENREL_MIN_LEVEL 2
80 #define ANCHOR_MAX_FT 1
81 #define ANCHOR_NUM_PRIOS 1
82 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
87 static struct init_tree_node
{
88 enum fs_node_type type
;
89 struct init_tree_node
*children
;
91 struct node_caps caps
;
97 .type
= FS_TYPE_NAMESPACE
,
99 .children
= (struct init_tree_node
[]) {
100 ADD_PRIO(0, BY_PASS_MIN_LEVEL
, 0,
101 FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive
.flow_modify_en
),
102 FS_CAP(flow_table_properties_nic_receive
.modify_root
),
103 FS_CAP(flow_table_properties_nic_receive
.identified_miss_table_mode
),
104 FS_CAP(flow_table_properties_nic_receive
.flow_table_modify
)),
105 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS
, BY_PASS_PRIO_MAX_FT
))),
106 ADD_PRIO(0, KENREL_MIN_LEVEL
, 0, {},
107 ADD_NS(ADD_MULTIPLE_PRIO(KERNEL_NUM_PRIOS
, KERNEL_MAX_FT
))),
108 ADD_PRIO(0, BY_PASS_MIN_LEVEL
, 0,
109 FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive
.flow_modify_en
),
110 FS_CAP(flow_table_properties_nic_receive
.modify_root
),
111 FS_CAP(flow_table_properties_nic_receive
.identified_miss_table_mode
),
112 FS_CAP(flow_table_properties_nic_receive
.flow_table_modify
)),
113 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS
, LEFTOVERS_MAX_FT
))),
114 ADD_PRIO(0, ANCHOR_MIN_LEVEL
, 0, {},
115 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS
, ANCHOR_MAX_FT
))),
119 enum fs_i_mutex_lock_class
{
120 FS_MUTEX_GRANDPARENT
,
125 static void del_rule(struct fs_node
*node
);
126 static void del_flow_table(struct fs_node
*node
);
127 static void del_flow_group(struct fs_node
*node
);
128 static void del_fte(struct fs_node
*node
);
130 static void tree_init_node(struct fs_node
*node
,
131 unsigned int refcount
,
132 void (*remove_func
)(struct fs_node
*))
134 atomic_set(&node
->refcount
, refcount
);
135 INIT_LIST_HEAD(&node
->list
);
136 INIT_LIST_HEAD(&node
->children
);
137 mutex_init(&node
->lock
);
138 node
->remove_func
= remove_func
;
141 static void tree_add_node(struct fs_node
*node
, struct fs_node
*parent
)
144 atomic_inc(&parent
->refcount
);
145 node
->parent
= parent
;
147 /* Parent is the root */
151 node
->root
= parent
->root
;
154 static void tree_get_node(struct fs_node
*node
)
156 atomic_inc(&node
->refcount
);
159 static void nested_lock_ref_node(struct fs_node
*node
,
160 enum fs_i_mutex_lock_class
class)
163 mutex_lock_nested(&node
->lock
, class);
164 atomic_inc(&node
->refcount
);
168 static void lock_ref_node(struct fs_node
*node
)
171 mutex_lock(&node
->lock
);
172 atomic_inc(&node
->refcount
);
176 static void unlock_ref_node(struct fs_node
*node
)
179 atomic_dec(&node
->refcount
);
180 mutex_unlock(&node
->lock
);
184 static void tree_put_node(struct fs_node
*node
)
186 struct fs_node
*parent_node
= node
->parent
;
188 lock_ref_node(parent_node
);
189 if (atomic_dec_and_test(&node
->refcount
)) {
191 list_del_init(&node
->list
);
192 if (node
->remove_func
)
193 node
->remove_func(node
);
197 unlock_ref_node(parent_node
);
198 if (!node
&& parent_node
)
199 tree_put_node(parent_node
);
202 static int tree_remove_node(struct fs_node
*node
)
204 if (atomic_read(&node
->refcount
) > 1) {
205 atomic_dec(&node
->refcount
);
212 static struct fs_prio
*find_prio(struct mlx5_flow_namespace
*ns
,
215 struct fs_prio
*iter_prio
;
217 fs_for_each_prio(iter_prio
, ns
) {
218 if (iter_prio
->prio
== prio
)
225 static unsigned int find_next_free_level(struct fs_prio
*prio
)
227 if (!list_empty(&prio
->node
.children
)) {
228 struct mlx5_flow_table
*ft
;
230 ft
= list_last_entry(&prio
->node
.children
,
231 struct mlx5_flow_table
,
233 return ft
->level
+ 1;
235 return prio
->start_level
;
238 static bool masked_memcmp(void *mask
, void *val1
, void *val2
, size_t size
)
242 for (i
= 0; i
< size
; i
++, mask
++, val1
++, val2
++)
243 if ((*((u8
*)val1
) & (*(u8
*)mask
)) !=
244 ((*(u8
*)val2
) & (*(u8
*)mask
)))
250 static bool compare_match_value(struct mlx5_flow_group_mask
*mask
,
251 void *fte_param1
, void *fte_param2
)
253 if (mask
->match_criteria_enable
&
254 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS
) {
255 void *fte_match1
= MLX5_ADDR_OF(fte_match_param
,
256 fte_param1
, outer_headers
);
257 void *fte_match2
= MLX5_ADDR_OF(fte_match_param
,
258 fte_param2
, outer_headers
);
259 void *fte_mask
= MLX5_ADDR_OF(fte_match_param
,
260 mask
->match_criteria
, outer_headers
);
262 if (!masked_memcmp(fte_mask
, fte_match1
, fte_match2
,
263 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4
)))
267 if (mask
->match_criteria_enable
&
268 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS
) {
269 void *fte_match1
= MLX5_ADDR_OF(fte_match_param
,
270 fte_param1
, misc_parameters
);
271 void *fte_match2
= MLX5_ADDR_OF(fte_match_param
,
272 fte_param2
, misc_parameters
);
273 void *fte_mask
= MLX5_ADDR_OF(fte_match_param
,
274 mask
->match_criteria
, misc_parameters
);
276 if (!masked_memcmp(fte_mask
, fte_match1
, fte_match2
,
277 MLX5_ST_SZ_BYTES(fte_match_set_misc
)))
281 if (mask
->match_criteria_enable
&
282 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS
) {
283 void *fte_match1
= MLX5_ADDR_OF(fte_match_param
,
284 fte_param1
, inner_headers
);
285 void *fte_match2
= MLX5_ADDR_OF(fte_match_param
,
286 fte_param2
, inner_headers
);
287 void *fte_mask
= MLX5_ADDR_OF(fte_match_param
,
288 mask
->match_criteria
, inner_headers
);
290 if (!masked_memcmp(fte_mask
, fte_match1
, fte_match2
,
291 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4
)))
297 static bool compare_match_criteria(u8 match_criteria_enable1
,
298 u8 match_criteria_enable2
,
299 void *mask1
, void *mask2
)
301 return match_criteria_enable1
== match_criteria_enable2
&&
302 !memcmp(mask1
, mask2
, MLX5_ST_SZ_BYTES(fte_match_param
));
305 static struct mlx5_flow_root_namespace
*find_root(struct fs_node
*node
)
307 struct fs_node
*root
;
308 struct mlx5_flow_namespace
*ns
;
312 if (WARN_ON(root
->type
!= FS_TYPE_NAMESPACE
)) {
313 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
317 ns
= container_of(root
, struct mlx5_flow_namespace
, node
);
318 return container_of(ns
, struct mlx5_flow_root_namespace
, ns
);
321 static inline struct mlx5_core_dev
*get_dev(struct fs_node
*node
)
323 struct mlx5_flow_root_namespace
*root
= find_root(node
);
330 static void del_flow_table(struct fs_node
*node
)
332 struct mlx5_flow_table
*ft
;
333 struct mlx5_core_dev
*dev
;
334 struct fs_prio
*prio
;
337 fs_get_obj(ft
, node
);
338 dev
= get_dev(&ft
->node
);
340 err
= mlx5_cmd_destroy_flow_table(dev
, ft
);
342 pr_warn("flow steering can't destroy ft\n");
343 fs_get_obj(prio
, ft
->node
.parent
);
347 static void del_rule(struct fs_node
*node
)
349 struct mlx5_flow_rule
*rule
;
350 struct mlx5_flow_table
*ft
;
351 struct mlx5_flow_group
*fg
;
354 struct mlx5_core_dev
*dev
= get_dev(node
);
355 int match_len
= MLX5_ST_SZ_BYTES(fte_match_param
);
358 match_value
= mlx5_vzalloc(match_len
);
360 pr_warn("failed to allocate inbox\n");
364 fs_get_obj(rule
, node
);
365 fs_get_obj(fte
, rule
->node
.parent
);
366 fs_get_obj(fg
, fte
->node
.parent
);
367 memcpy(match_value
, fte
->val
, sizeof(fte
->val
));
368 fs_get_obj(ft
, fg
->node
.parent
);
369 list_del(&rule
->node
.list
);
370 if (rule
->sw_action
== MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
) {
371 mutex_lock(&rule
->dest_attr
.ft
->lock
);
372 list_del(&rule
->next_ft
);
373 mutex_unlock(&rule
->dest_attr
.ft
->lock
);
375 if ((fte
->action
& MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
) &&
377 err
= mlx5_cmd_update_fte(dev
, ft
,
380 pr_warn("%s can't del rule fg id=%d fte_index=%d\n",
381 __func__
, fg
->id
, fte
->index
);
386 static void del_fte(struct fs_node
*node
)
388 struct mlx5_flow_table
*ft
;
389 struct mlx5_flow_group
*fg
;
390 struct mlx5_core_dev
*dev
;
394 fs_get_obj(fte
, node
);
395 fs_get_obj(fg
, fte
->node
.parent
);
396 fs_get_obj(ft
, fg
->node
.parent
);
398 dev
= get_dev(&ft
->node
);
399 err
= mlx5_cmd_delete_fte(dev
, ft
,
402 pr_warn("flow steering can't delete fte in index %d of flow group id %d\n",
409 static void del_flow_group(struct fs_node
*node
)
411 struct mlx5_flow_group
*fg
;
412 struct mlx5_flow_table
*ft
;
413 struct mlx5_core_dev
*dev
;
415 fs_get_obj(fg
, node
);
416 fs_get_obj(ft
, fg
->node
.parent
);
417 dev
= get_dev(&ft
->node
);
419 if (mlx5_cmd_destroy_flow_group(dev
, ft
, fg
->id
))
420 pr_warn("flow steering can't destroy fg %d of ft %d\n",
424 static struct fs_fte
*alloc_fte(u8 action
,
431 fte
= kzalloc(sizeof(*fte
), GFP_KERNEL
);
433 return ERR_PTR(-ENOMEM
);
435 memcpy(fte
->val
, match_value
, sizeof(fte
->val
));
436 fte
->node
.type
= FS_TYPE_FLOW_ENTRY
;
437 fte
->flow_tag
= flow_tag
;
439 fte
->action
= action
;
444 static struct mlx5_flow_group
*alloc_flow_group(u32
*create_fg_in
)
446 struct mlx5_flow_group
*fg
;
447 void *match_criteria
= MLX5_ADDR_OF(create_flow_group_in
,
448 create_fg_in
, match_criteria
);
449 u8 match_criteria_enable
= MLX5_GET(create_flow_group_in
,
451 match_criteria_enable
);
452 fg
= kzalloc(sizeof(*fg
), GFP_KERNEL
);
454 return ERR_PTR(-ENOMEM
);
456 fg
->mask
.match_criteria_enable
= match_criteria_enable
;
457 memcpy(&fg
->mask
.match_criteria
, match_criteria
,
458 sizeof(fg
->mask
.match_criteria
));
459 fg
->node
.type
= FS_TYPE_FLOW_GROUP
;
460 fg
->start_index
= MLX5_GET(create_flow_group_in
, create_fg_in
,
462 fg
->max_ftes
= MLX5_GET(create_flow_group_in
, create_fg_in
,
463 end_flow_index
) - fg
->start_index
+ 1;
467 static struct mlx5_flow_table
*alloc_flow_table(int level
, int max_fte
,
468 enum fs_flow_table_type table_type
)
470 struct mlx5_flow_table
*ft
;
472 ft
= kzalloc(sizeof(*ft
), GFP_KERNEL
);
477 ft
->node
.type
= FS_TYPE_FLOW_TABLE
;
478 ft
->type
= table_type
;
479 ft
->max_fte
= max_fte
;
480 INIT_LIST_HEAD(&ft
->fwd_rules
);
481 mutex_init(&ft
->lock
);
486 /* If reverse is false, then we search for the first flow table in the
487 * root sub-tree from start(closest from right), else we search for the
488 * last flow table in the root sub-tree till start(closest from left).
490 static struct mlx5_flow_table
*find_closest_ft_recursive(struct fs_node
*root
,
491 struct list_head
*start
,
494 #define list_advance_entry(pos, reverse) \
495 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
497 #define list_for_each_advance_continue(pos, head, reverse) \
498 for (pos = list_advance_entry(pos, reverse); \
499 &pos->list != (head); \
500 pos = list_advance_entry(pos, reverse))
502 struct fs_node
*iter
= list_entry(start
, struct fs_node
, list
);
503 struct mlx5_flow_table
*ft
= NULL
;
508 list_for_each_advance_continue(iter
, &root
->children
, reverse
) {
509 if (iter
->type
== FS_TYPE_FLOW_TABLE
) {
510 fs_get_obj(ft
, iter
);
513 ft
= find_closest_ft_recursive(iter
, &iter
->children
, reverse
);
521 /* If reverse if false then return the first flow table in next priority of
522 * prio in the tree, else return the last flow table in the previous priority
523 * of prio in the tree.
525 static struct mlx5_flow_table
*find_closest_ft(struct fs_prio
*prio
, bool reverse
)
527 struct mlx5_flow_table
*ft
= NULL
;
528 struct fs_node
*curr_node
;
529 struct fs_node
*parent
;
531 parent
= prio
->node
.parent
;
532 curr_node
= &prio
->node
;
533 while (!ft
&& parent
) {
534 ft
= find_closest_ft_recursive(parent
, &curr_node
->list
, reverse
);
536 parent
= curr_node
->parent
;
541 /* Assuming all the tree is locked by mutex chain lock */
542 static struct mlx5_flow_table
*find_next_chained_ft(struct fs_prio
*prio
)
544 return find_closest_ft(prio
, false);
547 /* Assuming all the tree is locked by mutex chain lock */
548 static struct mlx5_flow_table
*find_prev_chained_ft(struct fs_prio
*prio
)
550 return find_closest_ft(prio
, true);
553 static int connect_fts_in_prio(struct mlx5_core_dev
*dev
,
554 struct fs_prio
*prio
,
555 struct mlx5_flow_table
*ft
)
557 struct mlx5_flow_table
*iter
;
561 fs_for_each_ft(iter
, prio
) {
563 err
= mlx5_cmd_modify_flow_table(dev
,
567 mlx5_core_warn(dev
, "Failed to modify flow table %d\n",
569 /* The driver is out of sync with the FW */
578 /* Connect flow tables from previous priority of prio to ft */
579 static int connect_prev_fts(struct mlx5_core_dev
*dev
,
580 struct mlx5_flow_table
*ft
,
581 struct fs_prio
*prio
)
583 struct mlx5_flow_table
*prev_ft
;
585 prev_ft
= find_prev_chained_ft(prio
);
587 struct fs_prio
*prev_prio
;
589 fs_get_obj(prev_prio
, prev_ft
->node
.parent
);
590 return connect_fts_in_prio(dev
, prev_prio
, ft
);
595 static int update_root_ft_create(struct mlx5_flow_table
*ft
, struct fs_prio
598 struct mlx5_flow_root_namespace
*root
= find_root(&prio
->node
);
599 int min_level
= INT_MAX
;
603 min_level
= root
->root_ft
->level
;
605 if (ft
->level
>= min_level
)
608 err
= mlx5_cmd_update_root_ft(root
->dev
, ft
);
610 mlx5_core_warn(root
->dev
, "Update root flow table of id=%u failed\n",
618 static int mlx5_modify_rule_destination(struct mlx5_flow_rule
*rule
,
619 struct mlx5_flow_destination
*dest
)
621 struct mlx5_flow_table
*ft
;
622 struct mlx5_flow_group
*fg
;
626 fs_get_obj(fte
, rule
->node
.parent
);
627 if (!(fte
->action
& MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
))
629 lock_ref_node(&fte
->node
);
630 fs_get_obj(fg
, fte
->node
.parent
);
631 fs_get_obj(ft
, fg
->node
.parent
);
633 memcpy(&rule
->dest_attr
, dest
, sizeof(*dest
));
634 err
= mlx5_cmd_update_fte(get_dev(&ft
->node
),
636 unlock_ref_node(&fte
->node
);
641 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
642 static int connect_fwd_rules(struct mlx5_core_dev
*dev
,
643 struct mlx5_flow_table
*new_next_ft
,
644 struct mlx5_flow_table
*old_next_ft
)
646 struct mlx5_flow_destination dest
;
647 struct mlx5_flow_rule
*iter
;
650 /* new_next_ft and old_next_ft could be NULL only
651 * when we create/destroy the anchor flow table.
653 if (!new_next_ft
|| !old_next_ft
)
656 dest
.type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
657 dest
.ft
= new_next_ft
;
659 mutex_lock(&old_next_ft
->lock
);
660 list_splice_init(&old_next_ft
->fwd_rules
, &new_next_ft
->fwd_rules
);
661 mutex_unlock(&old_next_ft
->lock
);
662 list_for_each_entry(iter
, &new_next_ft
->fwd_rules
, next_ft
) {
663 err
= mlx5_modify_rule_destination(iter
, &dest
);
665 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
671 static int connect_flow_table(struct mlx5_core_dev
*dev
, struct mlx5_flow_table
*ft
,
672 struct fs_prio
*prio
)
674 struct mlx5_flow_table
*next_ft
;
677 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
679 if (list_empty(&prio
->node
.children
)) {
680 err
= connect_prev_fts(dev
, ft
, prio
);
684 next_ft
= find_next_chained_ft(prio
);
685 err
= connect_fwd_rules(dev
, ft
, next_ft
);
690 if (MLX5_CAP_FLOWTABLE(dev
,
691 flow_table_properties_nic_receive
.modify_root
))
692 err
= update_root_ft_create(ft
, prio
);
696 struct mlx5_flow_table
*mlx5_create_flow_table(struct mlx5_flow_namespace
*ns
,
700 struct mlx5_flow_table
*next_ft
= NULL
;
701 struct mlx5_flow_table
*ft
;
704 struct mlx5_flow_root_namespace
*root
=
705 find_root(&ns
->node
);
706 struct fs_prio
*fs_prio
= NULL
;
709 pr_err("mlx5: flow steering failed to find root of namespace\n");
710 return ERR_PTR(-ENODEV
);
713 mutex_lock(&root
->chain_lock
);
714 fs_prio
= find_prio(ns
, prio
);
719 if (fs_prio
->num_ft
== fs_prio
->max_ft
) {
724 ft
= alloc_flow_table(find_next_free_level(fs_prio
),
725 roundup_pow_of_two(max_fte
),
732 tree_init_node(&ft
->node
, 1, del_flow_table
);
733 log_table_sz
= ilog2(ft
->max_fte
);
734 next_ft
= find_next_chained_ft(fs_prio
);
735 err
= mlx5_cmd_create_flow_table(root
->dev
, ft
->type
, ft
->level
,
736 log_table_sz
, next_ft
, &ft
->id
);
740 err
= connect_flow_table(root
->dev
, ft
, fs_prio
);
743 lock_ref_node(&fs_prio
->node
);
744 tree_add_node(&ft
->node
, &fs_prio
->node
);
745 list_add_tail(&ft
->node
.list
, &fs_prio
->node
.children
);
747 unlock_ref_node(&fs_prio
->node
);
748 mutex_unlock(&root
->chain_lock
);
751 mlx5_cmd_destroy_flow_table(root
->dev
, ft
);
755 mutex_unlock(&root
->chain_lock
);
759 struct mlx5_flow_table
*mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace
*ns
,
761 int num_flow_table_entries
,
764 struct mlx5_flow_table
*ft
;
766 if (max_num_groups
> num_flow_table_entries
)
767 return ERR_PTR(-EINVAL
);
769 ft
= mlx5_create_flow_table(ns
, prio
, num_flow_table_entries
);
773 ft
->autogroup
.active
= true;
774 ft
->autogroup
.required_groups
= max_num_groups
;
778 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table
);
780 /* Flow table should be locked */
781 static struct mlx5_flow_group
*create_flow_group_common(struct mlx5_flow_table
*ft
,
787 struct mlx5_flow_group
*fg
;
788 struct mlx5_core_dev
*dev
= get_dev(&ft
->node
);
792 return ERR_PTR(-ENODEV
);
794 fg
= alloc_flow_group(fg_in
);
798 err
= mlx5_cmd_create_flow_group(dev
, ft
, fg_in
, &fg
->id
);
804 if (ft
->autogroup
.active
)
805 ft
->autogroup
.num_groups
++;
806 /* Add node to tree */
807 tree_init_node(&fg
->node
, !is_auto_fg
, del_flow_group
);
808 tree_add_node(&fg
->node
, &ft
->node
);
809 /* Add node to group list */
810 list_add(&fg
->node
.list
, ft
->node
.children
.prev
);
815 struct mlx5_flow_group
*mlx5_create_flow_group(struct mlx5_flow_table
*ft
,
818 struct mlx5_flow_group
*fg
;
820 if (ft
->autogroup
.active
)
821 return ERR_PTR(-EPERM
);
823 lock_ref_node(&ft
->node
);
824 fg
= create_flow_group_common(ft
, fg_in
, &ft
->node
.children
, false);
825 unlock_ref_node(&ft
->node
);
830 static struct mlx5_flow_rule
*alloc_rule(struct mlx5_flow_destination
*dest
)
832 struct mlx5_flow_rule
*rule
;
834 rule
= kzalloc(sizeof(*rule
), GFP_KERNEL
);
838 INIT_LIST_HEAD(&rule
->next_ft
);
839 rule
->node
.type
= FS_TYPE_FLOW_DEST
;
841 memcpy(&rule
->dest_attr
, dest
, sizeof(*dest
));
846 /* fte should not be deleted while calling this function */
847 static struct mlx5_flow_rule
*add_rule_fte(struct fs_fte
*fte
,
848 struct mlx5_flow_group
*fg
,
849 struct mlx5_flow_destination
*dest
)
851 struct mlx5_flow_table
*ft
;
852 struct mlx5_flow_rule
*rule
;
855 rule
= alloc_rule(dest
);
857 return ERR_PTR(-ENOMEM
);
859 fs_get_obj(ft
, fg
->node
.parent
);
860 /* Add dest to dests list- we need flow tables to be in the
861 * end of the list for forward to next prio rules.
863 tree_init_node(&rule
->node
, 1, del_rule
);
864 if (dest
&& dest
->type
!= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
)
865 list_add(&rule
->node
.list
, &fte
->node
.children
);
867 list_add_tail(&rule
->node
.list
, &fte
->node
.children
);
870 if (fte
->dests_size
== 1 || !dest
)
871 err
= mlx5_cmd_create_fte(get_dev(&ft
->node
),
874 err
= mlx5_cmd_update_fte(get_dev(&ft
->node
),
879 fte
->status
|= FS_FTE_STATUS_EXISTING
;
884 list_del(&rule
->node
.list
);
891 /* Assumed fg is locked */
892 static unsigned int get_free_fte_index(struct mlx5_flow_group
*fg
,
893 struct list_head
**prev
)
896 unsigned int start
= fg
->start_index
;
899 *prev
= &fg
->node
.children
;
901 /* assumed list is sorted by index */
902 fs_for_each_fte(fte
, fg
) {
903 if (fte
->index
!= start
)
907 *prev
= &fte
->node
.list
;
913 /* prev is output, prev->next = new_fte */
914 static struct fs_fte
*create_fte(struct mlx5_flow_group
*fg
,
918 struct list_head
**prev
)
923 index
= get_free_fte_index(fg
, prev
);
924 fte
= alloc_fte(action
, flow_tag
, match_value
, index
);
931 static struct mlx5_flow_group
*create_autogroup(struct mlx5_flow_table
*ft
,
932 u8 match_criteria_enable
,
935 int inlen
= MLX5_ST_SZ_BYTES(create_flow_group_in
);
936 struct list_head
*prev
= &ft
->node
.children
;
937 unsigned int candidate_index
= 0;
938 struct mlx5_flow_group
*fg
;
939 void *match_criteria_addr
;
940 unsigned int group_size
= 0;
943 if (!ft
->autogroup
.active
)
944 return ERR_PTR(-ENOENT
);
946 in
= mlx5_vzalloc(inlen
);
948 return ERR_PTR(-ENOMEM
);
950 if (ft
->autogroup
.num_groups
< ft
->autogroup
.required_groups
)
951 /* We save place for flow groups in addition to max types */
952 group_size
= ft
->max_fte
/ (ft
->autogroup
.required_groups
+ 1);
954 /* ft->max_fte == ft->autogroup.max_types */
958 /* sorted by start_index */
959 fs_for_each_fg(fg
, ft
) {
960 if (candidate_index
+ group_size
> fg
->start_index
)
961 candidate_index
= fg
->start_index
+ fg
->max_ftes
;
964 prev
= &fg
->node
.list
;
967 if (candidate_index
+ group_size
> ft
->max_fte
) {
968 fg
= ERR_PTR(-ENOSPC
);
972 MLX5_SET(create_flow_group_in
, in
, match_criteria_enable
,
973 match_criteria_enable
);
974 MLX5_SET(create_flow_group_in
, in
, start_flow_index
, candidate_index
);
975 MLX5_SET(create_flow_group_in
, in
, end_flow_index
, candidate_index
+
977 match_criteria_addr
= MLX5_ADDR_OF(create_flow_group_in
,
979 memcpy(match_criteria_addr
, match_criteria
,
980 MLX5_ST_SZ_BYTES(fte_match_param
));
982 fg
= create_flow_group_common(ft
, in
, prev
, true);
988 static struct mlx5_flow_rule
*find_flow_rule(struct fs_fte
*fte
,
989 struct mlx5_flow_destination
*dest
)
991 struct mlx5_flow_rule
*rule
;
993 list_for_each_entry(rule
, &fte
->node
.children
, node
.list
) {
994 if (rule
->dest_attr
.type
== dest
->type
) {
995 if ((dest
->type
== MLX5_FLOW_DESTINATION_TYPE_VPORT
&&
996 dest
->vport_num
== rule
->dest_attr
.vport_num
) ||
997 (dest
->type
== MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
&&
998 dest
->ft
== rule
->dest_attr
.ft
) ||
999 (dest
->type
== MLX5_FLOW_DESTINATION_TYPE_TIR
&&
1000 dest
->tir_num
== rule
->dest_attr
.tir_num
))
1007 static struct mlx5_flow_rule
*add_rule_fg(struct mlx5_flow_group
*fg
,
1011 struct mlx5_flow_destination
*dest
)
1014 struct mlx5_flow_rule
*rule
;
1015 struct mlx5_flow_table
*ft
;
1016 struct list_head
*prev
;
1018 nested_lock_ref_node(&fg
->node
, FS_MUTEX_PARENT
);
1019 fs_for_each_fte(fte
, fg
) {
1020 nested_lock_ref_node(&fte
->node
, FS_MUTEX_CHILD
);
1021 if (compare_match_value(&fg
->mask
, match_value
, &fte
->val
) &&
1022 action
== fte
->action
&& flow_tag
== fte
->flow_tag
) {
1023 rule
= find_flow_rule(fte
, dest
);
1025 atomic_inc(&rule
->node
.refcount
);
1026 unlock_ref_node(&fte
->node
);
1027 unlock_ref_node(&fg
->node
);
1030 rule
= add_rule_fte(fte
, fg
, dest
);
1031 unlock_ref_node(&fte
->node
);
1037 unlock_ref_node(&fte
->node
);
1039 fs_get_obj(ft
, fg
->node
.parent
);
1040 if (fg
->num_ftes
>= fg
->max_ftes
) {
1041 rule
= ERR_PTR(-ENOSPC
);
1045 fte
= create_fte(fg
, match_value
, action
, flow_tag
, &prev
);
1050 tree_init_node(&fte
->node
, 0, del_fte
);
1051 rule
= add_rule_fte(fte
, fg
, dest
);
1059 tree_add_node(&fte
->node
, &fg
->node
);
1060 list_add(&fte
->node
.list
, prev
);
1062 tree_add_node(&rule
->node
, &fte
->node
);
1064 unlock_ref_node(&fg
->node
);
1068 static struct mlx5_flow_rule
*add_rule_to_auto_fg(struct mlx5_flow_table
*ft
,
1069 u8 match_criteria_enable
,
1070 u32
*match_criteria
,
1074 struct mlx5_flow_destination
*dest
)
1076 struct mlx5_flow_rule
*rule
;
1077 struct mlx5_flow_group
*g
;
1079 g
= create_autogroup(ft
, match_criteria_enable
, match_criteria
);
1083 rule
= add_rule_fg(g
, match_value
,
1084 action
, flow_tag
, dest
);
1086 /* Remove assumes refcount > 0 and autogroup creates a group
1087 * with a refcount = 0.
1089 tree_get_node(&g
->node
);
1090 tree_remove_node(&g
->node
);
1095 static struct mlx5_flow_rule
*
1096 _mlx5_add_flow_rule(struct mlx5_flow_table
*ft
,
1097 u8 match_criteria_enable
,
1098 u32
*match_criteria
,
1102 struct mlx5_flow_destination
*dest
)
1104 struct mlx5_flow_group
*g
;
1105 struct mlx5_flow_rule
*rule
;
1107 if ((action
& MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
) && !dest
)
1108 return ERR_PTR(-EINVAL
);
1110 nested_lock_ref_node(&ft
->node
, FS_MUTEX_GRANDPARENT
);
1111 fs_for_each_fg(g
, ft
)
1112 if (compare_match_criteria(g
->mask
.match_criteria_enable
,
1113 match_criteria_enable
,
1114 g
->mask
.match_criteria
,
1116 rule
= add_rule_fg(g
, match_value
,
1117 action
, flow_tag
, dest
);
1118 if (!IS_ERR(rule
) || PTR_ERR(rule
) != -ENOSPC
)
1122 rule
= add_rule_to_auto_fg(ft
, match_criteria_enable
, match_criteria
,
1123 match_value
, action
, flow_tag
, dest
);
1125 unlock_ref_node(&ft
->node
);
1129 static bool fwd_next_prio_supported(struct mlx5_flow_table
*ft
)
1131 return ((ft
->type
== FS_FT_NIC_RX
) &&
1132 (MLX5_CAP_FLOWTABLE(get_dev(&ft
->node
), nic_rx_multi_path_tirs
)));
1135 struct mlx5_flow_rule
*
1136 mlx5_add_flow_rule(struct mlx5_flow_table
*ft
,
1137 u8 match_criteria_enable
,
1138 u32
*match_criteria
,
1142 struct mlx5_flow_destination
*dest
)
1144 struct mlx5_flow_root_namespace
*root
= find_root(&ft
->node
);
1145 struct mlx5_flow_destination gen_dest
;
1146 struct mlx5_flow_table
*next_ft
= NULL
;
1147 struct mlx5_flow_rule
*rule
= NULL
;
1148 u32 sw_action
= action
;
1149 struct fs_prio
*prio
;
1151 fs_get_obj(prio
, ft
->node
.parent
);
1152 if (action
== MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
) {
1153 if (!fwd_next_prio_supported(ft
))
1154 return ERR_PTR(-EOPNOTSUPP
);
1156 return ERR_PTR(-EINVAL
);
1157 mutex_lock(&root
->chain_lock
);
1158 next_ft
= find_next_chained_ft(prio
);
1160 gen_dest
.type
= MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE
;
1161 gen_dest
.ft
= next_ft
;
1163 action
= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
1165 mutex_unlock(&root
->chain_lock
);
1166 return ERR_PTR(-EOPNOTSUPP
);
1170 rule
= _mlx5_add_flow_rule(ft
, match_criteria_enable
, match_criteria
,
1171 match_value
, action
, flow_tag
, dest
);
1173 if (sw_action
== MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
) {
1174 if (!IS_ERR_OR_NULL(rule
) &&
1175 (list_empty(&rule
->next_ft
))) {
1176 mutex_lock(&next_ft
->lock
);
1177 list_add(&rule
->next_ft
, &next_ft
->fwd_rules
);
1178 mutex_unlock(&next_ft
->lock
);
1179 rule
->sw_action
= MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO
;
1181 mutex_unlock(&root
->chain_lock
);
1185 EXPORT_SYMBOL(mlx5_add_flow_rule
);
1187 void mlx5_del_flow_rule(struct mlx5_flow_rule
*rule
)
1189 tree_remove_node(&rule
->node
);
1191 EXPORT_SYMBOL(mlx5_del_flow_rule
);
1193 /* Assuming prio->node.children(flow tables) is sorted by level */
1194 static struct mlx5_flow_table
*find_next_ft(struct mlx5_flow_table
*ft
)
1196 struct fs_prio
*prio
;
1198 fs_get_obj(prio
, ft
->node
.parent
);
1200 if (!list_is_last(&ft
->node
.list
, &prio
->node
.children
))
1201 return list_next_entry(ft
, node
.list
);
1202 return find_next_chained_ft(prio
);
1205 static int update_root_ft_destroy(struct mlx5_flow_table
*ft
)
1207 struct mlx5_flow_root_namespace
*root
= find_root(&ft
->node
);
1208 struct mlx5_flow_table
*new_root_ft
= NULL
;
1210 if (root
->root_ft
!= ft
)
1213 new_root_ft
= find_next_ft(ft
);
1215 int err
= mlx5_cmd_update_root_ft(root
->dev
, new_root_ft
);
1218 mlx5_core_warn(root
->dev
, "Update root flow table of id=%u failed\n",
1222 root
->root_ft
= new_root_ft
;
1227 /* Connect flow table from previous priority to
1228 * the next flow table.
1230 static int disconnect_flow_table(struct mlx5_flow_table
*ft
)
1232 struct mlx5_core_dev
*dev
= get_dev(&ft
->node
);
1233 struct mlx5_flow_table
*next_ft
;
1234 struct fs_prio
*prio
;
1237 err
= update_root_ft_destroy(ft
);
1241 fs_get_obj(prio
, ft
->node
.parent
);
1242 if (!(list_first_entry(&prio
->node
.children
,
1243 struct mlx5_flow_table
,
1247 next_ft
= find_next_chained_ft(prio
);
1248 err
= connect_fwd_rules(dev
, next_ft
, ft
);
1252 err
= connect_prev_fts(dev
, next_ft
, prio
);
1254 mlx5_core_warn(dev
, "Failed to disconnect flow table %d\n",
1259 int mlx5_destroy_flow_table(struct mlx5_flow_table
*ft
)
1261 struct mlx5_flow_root_namespace
*root
= find_root(&ft
->node
);
1264 mutex_lock(&root
->chain_lock
);
1265 err
= disconnect_flow_table(ft
);
1267 mutex_unlock(&root
->chain_lock
);
1270 if (tree_remove_node(&ft
->node
))
1271 mlx5_core_warn(get_dev(&ft
->node
), "Flow table %d wasn't destroyed, refcount > 1\n",
1273 mutex_unlock(&root
->chain_lock
);
1277 EXPORT_SYMBOL(mlx5_destroy_flow_table
);
1279 void mlx5_destroy_flow_group(struct mlx5_flow_group
*fg
)
1281 if (tree_remove_node(&fg
->node
))
1282 mlx5_core_warn(get_dev(&fg
->node
), "Flow group %d wasn't destroyed, refcount > 1\n",
1286 struct mlx5_flow_namespace
*mlx5_get_flow_namespace(struct mlx5_core_dev
*dev
,
1287 enum mlx5_flow_namespace_type type
)
1289 struct mlx5_flow_root_namespace
*root_ns
= dev
->priv
.root_ns
;
1291 static struct fs_prio
*fs_prio
;
1292 struct mlx5_flow_namespace
*ns
;
1298 case MLX5_FLOW_NAMESPACE_BYPASS
:
1299 case MLX5_FLOW_NAMESPACE_KERNEL
:
1300 case MLX5_FLOW_NAMESPACE_LEFTOVERS
:
1301 case MLX5_FLOW_NAMESPACE_ANCHOR
:
1304 case MLX5_FLOW_NAMESPACE_FDB
:
1305 if (dev
->priv
.fdb_root_ns
)
1306 return &dev
->priv
.fdb_root_ns
->ns
;
1313 fs_prio
= find_prio(&root_ns
->ns
, prio
);
1317 ns
= list_first_entry(&fs_prio
->node
.children
,
1323 EXPORT_SYMBOL(mlx5_get_flow_namespace
);
1325 static struct fs_prio
*fs_create_prio(struct mlx5_flow_namespace
*ns
,
1326 unsigned prio
, int max_ft
)
1328 struct fs_prio
*fs_prio
;
1330 fs_prio
= kzalloc(sizeof(*fs_prio
), GFP_KERNEL
);
1332 return ERR_PTR(-ENOMEM
);
1334 fs_prio
->node
.type
= FS_TYPE_PRIO
;
1335 tree_init_node(&fs_prio
->node
, 1, NULL
);
1336 tree_add_node(&fs_prio
->node
, &ns
->node
);
1337 fs_prio
->max_ft
= max_ft
;
1338 fs_prio
->prio
= prio
;
1339 list_add_tail(&fs_prio
->node
.list
, &ns
->node
.children
);
1344 static struct mlx5_flow_namespace
*fs_init_namespace(struct mlx5_flow_namespace
1347 ns
->node
.type
= FS_TYPE_NAMESPACE
;
1352 static struct mlx5_flow_namespace
*fs_create_namespace(struct fs_prio
*prio
)
1354 struct mlx5_flow_namespace
*ns
;
1356 ns
= kzalloc(sizeof(*ns
), GFP_KERNEL
);
1358 return ERR_PTR(-ENOMEM
);
1360 fs_init_namespace(ns
);
1361 tree_init_node(&ns
->node
, 1, NULL
);
1362 tree_add_node(&ns
->node
, &prio
->node
);
1363 list_add_tail(&ns
->node
.list
, &prio
->node
.children
);
1368 static int create_leaf_prios(struct mlx5_flow_namespace
*ns
, struct init_tree_node
1371 struct fs_prio
*fs_prio
;
1374 for (i
= 0; i
< prio_metadata
->num_leaf_prios
; i
++) {
1375 fs_prio
= fs_create_prio(ns
, i
, prio_metadata
->max_ft
);
1376 if (IS_ERR(fs_prio
))
1377 return PTR_ERR(fs_prio
);
1382 #define FLOW_TABLE_BIT_SZ 1
1383 #define GET_FLOW_TABLE_CAP(dev, offset) \
1384 ((be32_to_cpu(*((__be32 *)(dev->hca_caps_cur[MLX5_CAP_FLOW_TABLE]) + \
1386 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
1387 static bool has_required_caps(struct mlx5_core_dev
*dev
, struct node_caps
*caps
)
1391 for (i
= 0; i
< caps
->arr_sz
; i
++) {
1392 if (!GET_FLOW_TABLE_CAP(dev
, caps
->caps
[i
]))
1398 static int init_root_tree_recursive(struct mlx5_core_dev
*dev
,
1399 struct init_tree_node
*init_node
,
1400 struct fs_node
*fs_parent_node
,
1401 struct init_tree_node
*init_parent_node
,
1404 int max_ft_level
= MLX5_CAP_FLOWTABLE(dev
,
1405 flow_table_properties_nic_receive
.
1407 struct mlx5_flow_namespace
*fs_ns
;
1408 struct fs_prio
*fs_prio
;
1409 struct fs_node
*base
;
1413 if (init_node
->type
== FS_TYPE_PRIO
) {
1414 if ((init_node
->min_ft_level
> max_ft_level
) ||
1415 !has_required_caps(dev
, &init_node
->caps
))
1418 fs_get_obj(fs_ns
, fs_parent_node
);
1419 if (init_node
->num_leaf_prios
)
1420 return create_leaf_prios(fs_ns
, init_node
);
1421 fs_prio
= fs_create_prio(fs_ns
, index
, init_node
->max_ft
);
1422 if (IS_ERR(fs_prio
))
1423 return PTR_ERR(fs_prio
);
1424 base
= &fs_prio
->node
;
1425 } else if (init_node
->type
== FS_TYPE_NAMESPACE
) {
1426 fs_get_obj(fs_prio
, fs_parent_node
);
1427 fs_ns
= fs_create_namespace(fs_prio
);
1429 return PTR_ERR(fs_ns
);
1430 base
= &fs_ns
->node
;
1434 for (i
= 0; i
< init_node
->ar_size
; i
++) {
1435 err
= init_root_tree_recursive(dev
, &init_node
->children
[i
],
1436 base
, init_node
, i
);
1444 static int init_root_tree(struct mlx5_core_dev
*dev
,
1445 struct init_tree_node
*init_node
,
1446 struct fs_node
*fs_parent_node
)
1449 struct mlx5_flow_namespace
*fs_ns
;
1452 fs_get_obj(fs_ns
, fs_parent_node
);
1453 for (i
= 0; i
< init_node
->ar_size
; i
++) {
1454 err
= init_root_tree_recursive(dev
, &init_node
->children
[i
],
1463 static struct mlx5_flow_root_namespace
*create_root_ns(struct mlx5_core_dev
*dev
,
1464 enum fs_flow_table_type
1467 struct mlx5_flow_root_namespace
*root_ns
;
1468 struct mlx5_flow_namespace
*ns
;
1470 /* Create the root namespace */
1471 root_ns
= mlx5_vzalloc(sizeof(*root_ns
));
1476 root_ns
->table_type
= table_type
;
1479 fs_init_namespace(ns
);
1480 mutex_init(&root_ns
->chain_lock
);
1481 tree_init_node(&ns
->node
, 1, NULL
);
1482 tree_add_node(&ns
->node
, NULL
);
1487 static void set_prio_attrs_in_prio(struct fs_prio
*prio
, int acc_level
);
1489 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace
*ns
, int acc_level
)
1491 struct fs_prio
*prio
;
1493 fs_for_each_prio(prio
, ns
) {
1494 /* This updates prio start_level and max_ft */
1495 set_prio_attrs_in_prio(prio
, acc_level
);
1496 acc_level
+= prio
->max_ft
;
1501 static void set_prio_attrs_in_prio(struct fs_prio
*prio
, int acc_level
)
1503 struct mlx5_flow_namespace
*ns
;
1504 int acc_level_ns
= acc_level
;
1506 prio
->start_level
= acc_level
;
1507 fs_for_each_ns(ns
, prio
)
1508 /* This updates start_level and max_ft of ns's priority descendants */
1509 acc_level_ns
= set_prio_attrs_in_ns(ns
, acc_level
);
1511 prio
->max_ft
= acc_level_ns
- prio
->start_level
;
1512 WARN_ON(prio
->max_ft
< acc_level_ns
- prio
->start_level
);
1515 static void set_prio_attrs(struct mlx5_flow_root_namespace
*root_ns
)
1517 struct mlx5_flow_namespace
*ns
= &root_ns
->ns
;
1518 struct fs_prio
*prio
;
1519 int start_level
= 0;
1521 fs_for_each_prio(prio
, ns
) {
1522 set_prio_attrs_in_prio(prio
, start_level
);
1523 start_level
+= prio
->max_ft
;
1527 #define ANCHOR_PRIO 0
1528 #define ANCHOR_SIZE 1
1529 static int create_anchor_flow_table(struct mlx5_core_dev
1532 struct mlx5_flow_namespace
*ns
= NULL
;
1533 struct mlx5_flow_table
*ft
;
1535 ns
= mlx5_get_flow_namespace(dev
, MLX5_FLOW_NAMESPACE_ANCHOR
);
1538 ft
= mlx5_create_flow_table(ns
, ANCHOR_PRIO
, ANCHOR_SIZE
);
1540 mlx5_core_err(dev
, "Failed to create last anchor flow table");
1546 static int init_root_ns(struct mlx5_core_dev
*dev
)
1549 dev
->priv
.root_ns
= create_root_ns(dev
, FS_FT_NIC_RX
);
1550 if (IS_ERR_OR_NULL(dev
->priv
.root_ns
))
1553 if (init_root_tree(dev
, &root_fs
, &dev
->priv
.root_ns
->ns
.node
))
1556 set_prio_attrs(dev
->priv
.root_ns
);
1558 if (create_anchor_flow_table(dev
))
1564 mlx5_cleanup_fs(dev
);
1568 static void cleanup_single_prio_root_ns(struct mlx5_core_dev
*dev
,
1569 struct mlx5_flow_root_namespace
*root_ns
)
1571 struct fs_node
*prio
;
1576 if (!list_empty(&root_ns
->ns
.node
.children
)) {
1577 prio
= list_first_entry(&root_ns
->ns
.node
.children
,
1580 if (tree_remove_node(prio
))
1582 "Flow steering priority wasn't destroyed, refcount > 1\n");
1584 if (tree_remove_node(&root_ns
->ns
.node
))
1586 "Flow steering namespace wasn't destroyed, refcount > 1\n");
1590 static void destroy_flow_tables(struct fs_prio
*prio
)
1592 struct mlx5_flow_table
*iter
;
1593 struct mlx5_flow_table
*tmp
;
1595 fs_for_each_ft_safe(iter
, tmp
, prio
)
1596 mlx5_destroy_flow_table(iter
);
1599 static void cleanup_root_ns(struct mlx5_core_dev
*dev
)
1601 struct mlx5_flow_root_namespace
*root_ns
= dev
->priv
.root_ns
;
1602 struct fs_prio
*iter_prio
;
1604 if (!MLX5_CAP_GEN(dev
, nic_flow_table
))
1611 fs_for_each_prio(iter_prio
, &root_ns
->ns
) {
1612 struct fs_node
*node
;
1613 struct mlx5_flow_namespace
*iter_ns
;
1615 fs_for_each_ns_or_ft(node
, iter_prio
) {
1616 if (node
->type
== FS_TYPE_FLOW_TABLE
)
1618 fs_get_obj(iter_ns
, node
);
1619 while (!list_empty(&iter_ns
->node
.children
)) {
1620 struct fs_prio
*obj_iter_prio2
;
1621 struct fs_node
*iter_prio2
=
1622 list_first_entry(&iter_ns
->node
.children
,
1626 fs_get_obj(obj_iter_prio2
, iter_prio2
);
1627 destroy_flow_tables(obj_iter_prio2
);
1628 if (tree_remove_node(iter_prio2
)) {
1630 "Priority %d wasn't destroyed, refcount > 1\n",
1631 obj_iter_prio2
->prio
);
1639 fs_for_each_prio(iter_prio
, &root_ns
->ns
) {
1640 while (!list_empty(&iter_prio
->node
.children
)) {
1641 struct fs_node
*iter_ns
=
1642 list_first_entry(&iter_prio
->node
.children
,
1645 if (tree_remove_node(iter_ns
)) {
1647 "Namespace wasn't destroyed, refcount > 1\n");
1654 while (!list_empty(&root_ns
->ns
.node
.children
)) {
1655 struct fs_prio
*obj_prio_node
;
1656 struct fs_node
*prio_node
=
1657 list_first_entry(&root_ns
->ns
.node
.children
,
1661 fs_get_obj(obj_prio_node
, prio_node
);
1662 if (tree_remove_node(prio_node
)) {
1664 "Priority %d wasn't destroyed, refcount > 1\n",
1665 obj_prio_node
->prio
);
1670 if (tree_remove_node(&root_ns
->ns
.node
)) {
1672 "root namespace wasn't destroyed, refcount > 1\n");
1676 dev
->priv
.root_ns
= NULL
;
1679 void mlx5_cleanup_fs(struct mlx5_core_dev
*dev
)
1681 cleanup_root_ns(dev
);
1682 cleanup_single_prio_root_ns(dev
, dev
->priv
.fdb_root_ns
);
1685 static int init_fdb_root_ns(struct mlx5_core_dev
*dev
)
1687 struct fs_prio
*prio
;
1689 dev
->priv
.fdb_root_ns
= create_root_ns(dev
, FS_FT_FDB
);
1690 if (!dev
->priv
.fdb_root_ns
)
1693 /* Create single prio */
1694 prio
= fs_create_prio(&dev
->priv
.fdb_root_ns
->ns
, 0, 1);
1696 cleanup_single_prio_root_ns(dev
, dev
->priv
.fdb_root_ns
);
1697 return PTR_ERR(prio
);
1703 int mlx5_init_fs(struct mlx5_core_dev
*dev
)
1707 if (MLX5_CAP_GEN(dev
, nic_flow_table
)) {
1708 err
= init_root_ns(dev
);
1712 if (MLX5_CAP_GEN(dev
, eswitch_flow_table
)) {
1713 err
= init_fdb_root_ns(dev
);
1715 cleanup_root_ns(dev
);