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 INIT_PRIO(min_level_val, max_ft_val,\
44 start_level_val, ...) {.type = FS_TYPE_PRIO,\
45 .min_ft_level = min_level_val,\
46 .start_level = start_level_val,\
47 .max_ft = max_ft_val,\
48 .children = (struct init_tree_node[]) {__VA_ARGS__},\
49 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
52 #define ADD_PRIO(min_level_val, max_ft_val, start_level_val, ...)\
53 INIT_PRIO(min_level_val, max_ft_val, start_level_val,\
56 #define ADD_FT_PRIO(max_ft_val, start_level_val, ...)\
57 INIT_PRIO(0, max_ft_val, start_level_val,\
60 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
61 .children = (struct init_tree_node[]) {__VA_ARGS__},\
62 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
65 #define KERNEL_START_LEVEL 0
66 #define KERNEL_P0_START_LEVEL KERNEL_START_LEVEL
67 #define KERNEL_MAX_FT 2
68 #define KENREL_MIN_LEVEL 2
69 static struct init_tree_node {
70 enum fs_node_type type;
71 struct init_tree_node *children;
78 .type = FS_TYPE_NAMESPACE,
80 .children = (struct init_tree_node[]) {
81 ADD_PRIO(KENREL_MIN_LEVEL, KERNEL_MAX_FT,
83 ADD_NS(ADD_FT_PRIO(KERNEL_MAX_FT,
84 KERNEL_P0_START_LEVEL))),
88 enum fs_i_mutex_lock_class {
94 static void del_rule(struct fs_node *node);
95 static void del_flow_table(struct fs_node *node);
96 static void del_flow_group(struct fs_node *node);
97 static void del_fte(struct fs_node *node);
99 static void tree_init_node(struct fs_node *node,
100 unsigned int refcount,
101 void (*remove_func)(struct fs_node *))
103 atomic_set(&node->refcount, refcount);
104 INIT_LIST_HEAD(&node->list);
105 INIT_LIST_HEAD(&node->children);
106 mutex_init(&node->lock);
107 node->remove_func = remove_func;
110 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
113 atomic_inc(&parent->refcount);
114 node->parent = parent;
116 /* Parent is the root */
120 node->root = parent->root;
123 static void tree_get_node(struct fs_node *node)
125 atomic_inc(&node->refcount);
128 static void nested_lock_ref_node(struct fs_node *node,
129 enum fs_i_mutex_lock_class class)
132 mutex_lock_nested(&node->lock, class);
133 atomic_inc(&node->refcount);
137 static void lock_ref_node(struct fs_node *node)
140 mutex_lock(&node->lock);
141 atomic_inc(&node->refcount);
145 static void unlock_ref_node(struct fs_node *node)
148 atomic_dec(&node->refcount);
149 mutex_unlock(&node->lock);
153 static void tree_put_node(struct fs_node *node)
155 struct fs_node *parent_node = node->parent;
157 lock_ref_node(parent_node);
158 if (atomic_dec_and_test(&node->refcount)) {
160 list_del_init(&node->list);
161 if (node->remove_func)
162 node->remove_func(node);
166 unlock_ref_node(parent_node);
167 if (!node && parent_node)
168 tree_put_node(parent_node);
171 static int tree_remove_node(struct fs_node *node)
173 if (atomic_read(&node->refcount) > 1)
179 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
182 struct fs_prio *iter_prio;
184 fs_for_each_prio(iter_prio, ns) {
185 if (iter_prio->prio == prio)
192 static unsigned int find_next_free_level(struct fs_prio *prio)
194 if (!list_empty(&prio->node.children)) {
195 struct mlx5_flow_table *ft;
197 ft = list_last_entry(&prio->node.children,
198 struct mlx5_flow_table,
200 return ft->level + 1;
202 return prio->start_level;
205 static bool masked_memcmp(void *mask, void *val1, void *val2, size_t size)
209 for (i = 0; i < size; i++, mask++, val1++, val2++)
210 if ((*((u8 *)val1) & (*(u8 *)mask)) !=
211 ((*(u8 *)val2) & (*(u8 *)mask)))
217 static bool compare_match_value(struct mlx5_flow_group_mask *mask,
218 void *fte_param1, void *fte_param2)
220 if (mask->match_criteria_enable &
221 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) {
222 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
223 fte_param1, outer_headers);
224 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
225 fte_param2, outer_headers);
226 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
227 mask->match_criteria, outer_headers);
229 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
230 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
234 if (mask->match_criteria_enable &
235 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) {
236 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
237 fte_param1, misc_parameters);
238 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
239 fte_param2, misc_parameters);
240 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
241 mask->match_criteria, misc_parameters);
243 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
244 MLX5_ST_SZ_BYTES(fte_match_set_misc)))
248 if (mask->match_criteria_enable &
249 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS) {
250 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
251 fte_param1, inner_headers);
252 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
253 fte_param2, inner_headers);
254 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
255 mask->match_criteria, inner_headers);
257 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
258 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
264 static bool compare_match_criteria(u8 match_criteria_enable1,
265 u8 match_criteria_enable2,
266 void *mask1, void *mask2)
268 return match_criteria_enable1 == match_criteria_enable2 &&
269 !memcmp(mask1, mask2, MLX5_ST_SZ_BYTES(fte_match_param));
272 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
274 struct fs_node *root;
275 struct mlx5_flow_namespace *ns;
279 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
280 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
284 ns = container_of(root, struct mlx5_flow_namespace, node);
285 return container_of(ns, struct mlx5_flow_root_namespace, ns);
288 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
290 struct mlx5_flow_root_namespace *root = find_root(node);
297 static void del_flow_table(struct fs_node *node)
299 struct mlx5_flow_table *ft;
300 struct mlx5_core_dev *dev;
301 struct fs_prio *prio;
304 fs_get_obj(ft, node);
305 dev = get_dev(&ft->node);
307 err = mlx5_cmd_destroy_flow_table(dev, ft);
309 pr_warn("flow steering can't destroy ft\n");
310 fs_get_obj(prio, ft->node.parent);
314 static void del_rule(struct fs_node *node)
316 struct mlx5_flow_rule *rule;
317 struct mlx5_flow_table *ft;
318 struct mlx5_flow_group *fg;
321 struct mlx5_core_dev *dev = get_dev(node);
322 int match_len = MLX5_ST_SZ_BYTES(fte_match_param);
325 match_value = mlx5_vzalloc(match_len);
327 pr_warn("failed to allocate inbox\n");
331 fs_get_obj(rule, node);
332 fs_get_obj(fte, rule->node.parent);
333 fs_get_obj(fg, fte->node.parent);
334 memcpy(match_value, fte->val, sizeof(fte->val));
335 fs_get_obj(ft, fg->node.parent);
336 list_del(&rule->node.list);
338 if (fte->dests_size) {
339 err = mlx5_cmd_update_fte(dev, ft,
342 pr_warn("%s can't del rule fg id=%d fte_index=%d\n",
343 __func__, fg->id, fte->index);
348 static void del_fte(struct fs_node *node)
350 struct mlx5_flow_table *ft;
351 struct mlx5_flow_group *fg;
352 struct mlx5_core_dev *dev;
356 fs_get_obj(fte, node);
357 fs_get_obj(fg, fte->node.parent);
358 fs_get_obj(ft, fg->node.parent);
360 dev = get_dev(&ft->node);
361 err = mlx5_cmd_delete_fte(dev, ft,
364 pr_warn("flow steering can't delete fte in index %d of flow group id %d\n",
371 static void del_flow_group(struct fs_node *node)
373 struct mlx5_flow_group *fg;
374 struct mlx5_flow_table *ft;
375 struct mlx5_core_dev *dev;
377 fs_get_obj(fg, node);
378 fs_get_obj(ft, fg->node.parent);
379 dev = get_dev(&ft->node);
381 if (mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
382 pr_warn("flow steering can't destroy fg %d of ft %d\n",
386 static struct fs_fte *alloc_fte(u8 action,
393 fte = kzalloc(sizeof(*fte), GFP_KERNEL);
395 return ERR_PTR(-ENOMEM);
397 memcpy(fte->val, match_value, sizeof(fte->val));
398 fte->node.type = FS_TYPE_FLOW_ENTRY;
399 fte->flow_tag = flow_tag;
401 fte->action = action;
406 static struct mlx5_flow_group *alloc_flow_group(u32 *create_fg_in)
408 struct mlx5_flow_group *fg;
409 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
410 create_fg_in, match_criteria);
411 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
413 match_criteria_enable);
414 fg = kzalloc(sizeof(*fg), GFP_KERNEL);
416 return ERR_PTR(-ENOMEM);
418 fg->mask.match_criteria_enable = match_criteria_enable;
419 memcpy(&fg->mask.match_criteria, match_criteria,
420 sizeof(fg->mask.match_criteria));
421 fg->node.type = FS_TYPE_FLOW_GROUP;
422 fg->start_index = MLX5_GET(create_flow_group_in, create_fg_in,
424 fg->max_ftes = MLX5_GET(create_flow_group_in, create_fg_in,
425 end_flow_index) - fg->start_index + 1;
429 static struct mlx5_flow_table *alloc_flow_table(int level, int max_fte,
430 enum fs_flow_table_type table_type)
432 struct mlx5_flow_table *ft;
434 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
439 ft->node.type = FS_TYPE_FLOW_TABLE;
440 ft->type = table_type;
441 ft->max_fte = max_fte;
446 /* If reverse is false, then we search for the first flow table in the
447 * root sub-tree from start(closest from right), else we search for the
448 * last flow table in the root sub-tree till start(closest from left).
450 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
451 struct list_head *start,
454 #define list_advance_entry(pos, reverse) \
455 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
457 #define list_for_each_advance_continue(pos, head, reverse) \
458 for (pos = list_advance_entry(pos, reverse); \
459 &pos->list != (head); \
460 pos = list_advance_entry(pos, reverse))
462 struct fs_node *iter = list_entry(start, struct fs_node, list);
463 struct mlx5_flow_table *ft = NULL;
468 list_for_each_advance_continue(iter, &root->children, reverse) {
469 if (iter->type == FS_TYPE_FLOW_TABLE) {
470 fs_get_obj(ft, iter);
473 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
481 /* If reverse if false then return the first flow table in next priority of
482 * prio in the tree, else return the last flow table in the previous priority
483 * of prio in the tree.
485 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
487 struct mlx5_flow_table *ft = NULL;
488 struct fs_node *curr_node;
489 struct fs_node *parent;
491 parent = prio->node.parent;
492 curr_node = &prio->node;
493 while (!ft && parent) {
494 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
496 parent = curr_node->parent;
501 /* Assuming all the tree is locked by mutex chain lock */
502 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
504 return find_closest_ft(prio, false);
507 /* Assuming all the tree is locked by mutex chain lock */
508 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
510 return find_closest_ft(prio, true);
513 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
516 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
517 int min_level = INT_MAX;
521 min_level = root->root_ft->level;
523 if (ft->level >= min_level)
526 err = mlx5_cmd_update_root_ft(root->dev, ft);
528 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
536 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
540 struct mlx5_flow_table *ft;
543 struct mlx5_flow_root_namespace *root =
544 find_root(&ns->node);
545 struct fs_prio *fs_prio = NULL;
548 pr_err("mlx5: flow steering failed to find root of namespace\n");
549 return ERR_PTR(-ENODEV);
552 mutex_lock(&root->chain_lock);
553 fs_prio = find_prio(ns, prio);
558 if (fs_prio->num_ft == fs_prio->max_ft) {
563 ft = alloc_flow_table(find_next_free_level(fs_prio),
564 roundup_pow_of_two(max_fte),
571 tree_init_node(&ft->node, 1, del_flow_table);
572 log_table_sz = ilog2(ft->max_fte);
573 err = mlx5_cmd_create_flow_table(root->dev, ft->type, ft->level,
574 log_table_sz, &ft->id);
578 if (MLX5_CAP_FLOWTABLE(root->dev,
579 flow_table_properties_nic_receive.modify_root)) {
580 err = update_root_ft_create(ft, fs_prio);
584 lock_ref_node(&fs_prio->node);
585 tree_add_node(&ft->node, &fs_prio->node);
586 list_add_tail(&ft->node.list, &fs_prio->node.children);
588 unlock_ref_node(&fs_prio->node);
589 mutex_unlock(&root->chain_lock);
592 mlx5_cmd_destroy_flow_table(root->dev, ft);
596 mutex_unlock(&root->chain_lock);
600 struct mlx5_flow_table *mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
602 int num_flow_table_entries,
605 struct mlx5_flow_table *ft;
607 if (max_num_groups > num_flow_table_entries)
608 return ERR_PTR(-EINVAL);
610 ft = mlx5_create_flow_table(ns, prio, num_flow_table_entries);
614 ft->autogroup.active = true;
615 ft->autogroup.required_groups = max_num_groups;
620 /* Flow table should be locked */
621 static struct mlx5_flow_group *create_flow_group_common(struct mlx5_flow_table *ft,
627 struct mlx5_flow_group *fg;
628 struct mlx5_core_dev *dev = get_dev(&ft->node);
632 return ERR_PTR(-ENODEV);
634 fg = alloc_flow_group(fg_in);
638 err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
644 if (ft->autogroup.active)
645 ft->autogroup.num_groups++;
646 /* Add node to tree */
647 tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
648 tree_add_node(&fg->node, &ft->node);
649 /* Add node to group list */
650 list_add(&fg->node.list, ft->node.children.prev);
655 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
658 struct mlx5_flow_group *fg;
660 if (ft->autogroup.active)
661 return ERR_PTR(-EPERM);
663 lock_ref_node(&ft->node);
664 fg = create_flow_group_common(ft, fg_in, &ft->node.children, false);
665 unlock_ref_node(&ft->node);
670 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
672 struct mlx5_flow_rule *rule;
674 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
678 rule->node.type = FS_TYPE_FLOW_DEST;
679 memcpy(&rule->dest_attr, dest, sizeof(*dest));
684 /* fte should not be deleted while calling this function */
685 static struct mlx5_flow_rule *add_rule_fte(struct fs_fte *fte,
686 struct mlx5_flow_group *fg,
687 struct mlx5_flow_destination *dest)
689 struct mlx5_flow_table *ft;
690 struct mlx5_flow_rule *rule;
693 rule = alloc_rule(dest);
695 return ERR_PTR(-ENOMEM);
697 fs_get_obj(ft, fg->node.parent);
698 /* Add dest to dests list- added as first element after the head */
699 tree_init_node(&rule->node, 1, del_rule);
700 list_add_tail(&rule->node.list, &fte->node.children);
702 if (fte->dests_size == 1)
703 err = mlx5_cmd_create_fte(get_dev(&ft->node),
706 err = mlx5_cmd_update_fte(get_dev(&ft->node),
711 fte->status |= FS_FTE_STATUS_EXISTING;
716 list_del(&rule->node.list);
722 /* Assumed fg is locked */
723 static unsigned int get_free_fte_index(struct mlx5_flow_group *fg,
724 struct list_head **prev)
727 unsigned int start = fg->start_index;
730 *prev = &fg->node.children;
732 /* assumed list is sorted by index */
733 fs_for_each_fte(fte, fg) {
734 if (fte->index != start)
738 *prev = &fte->node.list;
744 /* prev is output, prev->next = new_fte */
745 static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
749 struct list_head **prev)
754 index = get_free_fte_index(fg, prev);
755 fte = alloc_fte(action, flow_tag, match_value, index);
762 static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
763 u8 match_criteria_enable,
766 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
767 struct list_head *prev = &ft->node.children;
768 unsigned int candidate_index = 0;
769 struct mlx5_flow_group *fg;
770 void *match_criteria_addr;
771 unsigned int group_size = 0;
774 if (!ft->autogroup.active)
775 return ERR_PTR(-ENOENT);
777 in = mlx5_vzalloc(inlen);
779 return ERR_PTR(-ENOMEM);
781 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
782 /* We save place for flow groups in addition to max types */
783 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
785 /* ft->max_fte == ft->autogroup.max_types */
789 /* sorted by start_index */
790 fs_for_each_fg(fg, ft) {
791 if (candidate_index + group_size > fg->start_index)
792 candidate_index = fg->start_index + fg->max_ftes;
795 prev = &fg->node.list;
798 if (candidate_index + group_size > ft->max_fte) {
799 fg = ERR_PTR(-ENOSPC);
803 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
804 match_criteria_enable);
805 MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
806 MLX5_SET(create_flow_group_in, in, end_flow_index, candidate_index +
808 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
810 memcpy(match_criteria_addr, match_criteria,
811 MLX5_ST_SZ_BYTES(fte_match_param));
813 fg = create_flow_group_common(ft, in, prev, true);
819 static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
823 struct mlx5_flow_destination *dest)
826 struct mlx5_flow_rule *rule;
827 struct mlx5_flow_table *ft;
828 struct list_head *prev;
830 nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
831 fs_for_each_fte(fte, fg) {
832 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
833 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
834 action == fte->action && flow_tag == fte->flow_tag) {
835 rule = add_rule_fte(fte, fg, dest);
836 unlock_ref_node(&fte->node);
842 unlock_ref_node(&fte->node);
844 fs_get_obj(ft, fg->node.parent);
845 if (fg->num_ftes >= fg->max_ftes) {
846 rule = ERR_PTR(-ENOSPC);
850 fte = create_fte(fg, match_value, action, flow_tag, &prev);
855 tree_init_node(&fte->node, 0, del_fte);
856 rule = add_rule_fte(fte, fg, dest);
864 tree_add_node(&fte->node, &fg->node);
865 list_add(&fte->node.list, prev);
867 tree_add_node(&rule->node, &fte->node);
869 unlock_ref_node(&fg->node);
873 static struct mlx5_flow_rule *add_rule_to_auto_fg(struct mlx5_flow_table *ft,
874 u8 match_criteria_enable,
879 struct mlx5_flow_destination *dest)
881 struct mlx5_flow_rule *rule;
882 struct mlx5_flow_group *g;
884 g = create_autogroup(ft, match_criteria_enable, match_criteria);
888 rule = add_rule_fg(g, match_value,
889 action, flow_tag, dest);
891 /* Remove assumes refcount > 0 and autogroup creates a group
892 * with a refcount = 0.
894 tree_get_node(&g->node);
895 tree_remove_node(&g->node);
900 struct mlx5_flow_rule *
901 mlx5_add_flow_rule(struct mlx5_flow_table *ft,
902 u8 match_criteria_enable,
907 struct mlx5_flow_destination *dest)
909 struct mlx5_flow_group *g;
910 struct mlx5_flow_rule *rule;
912 nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
913 fs_for_each_fg(g, ft)
914 if (compare_match_criteria(g->mask.match_criteria_enable,
915 match_criteria_enable,
916 g->mask.match_criteria,
918 rule = add_rule_fg(g, match_value,
919 action, flow_tag, dest);
920 if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
924 rule = add_rule_to_auto_fg(ft, match_criteria_enable, match_criteria,
925 match_value, action, flow_tag, dest);
927 unlock_ref_node(&ft->node);
931 void mlx5_del_flow_rule(struct mlx5_flow_rule *rule)
933 tree_remove_node(&rule->node);
936 /* Assuming prio->node.children(flow tables) is sorted by level */
937 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
939 struct fs_prio *prio;
941 fs_get_obj(prio, ft->node.parent);
943 if (!list_is_last(&ft->node.list, &prio->node.children))
944 return list_next_entry(ft, node.list);
945 return find_next_chained_ft(prio);
948 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
950 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
951 struct mlx5_flow_table *new_root_ft = NULL;
953 if (root->root_ft != ft)
956 new_root_ft = find_next_ft(ft);
958 int err = mlx5_cmd_update_root_ft(root->dev, new_root_ft);
961 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
965 root->root_ft = new_root_ft;
970 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
972 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
975 mutex_lock(&root->chain_lock);
976 err = update_root_ft_destroy(ft);
978 mutex_unlock(&root->chain_lock);
981 if (tree_remove_node(&ft->node))
982 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
984 mutex_unlock(&root->chain_lock);
989 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
991 if (tree_remove_node(&fg->node))
992 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
996 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
997 enum mlx5_flow_namespace_type type)
999 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1001 static struct fs_prio *fs_prio;
1002 struct mlx5_flow_namespace *ns;
1008 case MLX5_FLOW_NAMESPACE_KERNEL:
1011 case MLX5_FLOW_NAMESPACE_FDB:
1012 if (dev->priv.fdb_root_ns)
1013 return &dev->priv.fdb_root_ns->ns;
1020 fs_prio = find_prio(&root_ns->ns, prio);
1024 ns = list_first_entry(&fs_prio->node.children,
1031 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
1032 unsigned prio, int max_ft,
1035 struct fs_prio *fs_prio;
1037 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
1039 return ERR_PTR(-ENOMEM);
1041 fs_prio->node.type = FS_TYPE_PRIO;
1042 tree_init_node(&fs_prio->node, 1, NULL);
1043 tree_add_node(&fs_prio->node, &ns->node);
1044 fs_prio->max_ft = max_ft;
1045 fs_prio->prio = prio;
1046 fs_prio->start_level = start_level;
1047 list_add_tail(&fs_prio->node.list, &ns->node.children);
1052 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
1055 ns->node.type = FS_TYPE_NAMESPACE;
1060 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
1062 struct mlx5_flow_namespace *ns;
1064 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1066 return ERR_PTR(-ENOMEM);
1068 fs_init_namespace(ns);
1069 tree_init_node(&ns->node, 1, NULL);
1070 tree_add_node(&ns->node, &prio->node);
1071 list_add_tail(&ns->node.list, &prio->node.children);
1076 static int init_root_tree_recursive(int max_ft_level, struct init_tree_node *init_node,
1077 struct fs_node *fs_parent_node,
1078 struct init_tree_node *init_parent_node,
1081 struct mlx5_flow_namespace *fs_ns;
1082 struct fs_prio *fs_prio;
1083 struct fs_node *base;
1087 if (init_node->type == FS_TYPE_PRIO) {
1088 if (init_node->min_ft_level > max_ft_level)
1091 fs_get_obj(fs_ns, fs_parent_node);
1092 fs_prio = fs_create_prio(fs_ns, index, init_node->max_ft,
1093 init_node->start_level);
1094 if (IS_ERR(fs_prio))
1095 return PTR_ERR(fs_prio);
1096 base = &fs_prio->node;
1097 } else if (init_node->type == FS_TYPE_NAMESPACE) {
1098 fs_get_obj(fs_prio, fs_parent_node);
1099 fs_ns = fs_create_namespace(fs_prio);
1101 return PTR_ERR(fs_ns);
1102 base = &fs_ns->node;
1106 for (i = 0; i < init_node->ar_size; i++) {
1107 err = init_root_tree_recursive(max_ft_level,
1108 &init_node->children[i], base,
1117 static int init_root_tree(int max_ft_level, struct init_tree_node *init_node,
1118 struct fs_node *fs_parent_node)
1121 struct mlx5_flow_namespace *fs_ns;
1124 fs_get_obj(fs_ns, fs_parent_node);
1125 for (i = 0; i < init_node->ar_size; i++) {
1126 err = init_root_tree_recursive(max_ft_level,
1127 &init_node->children[i],
1136 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_core_dev *dev,
1137 enum fs_flow_table_type
1140 struct mlx5_flow_root_namespace *root_ns;
1141 struct mlx5_flow_namespace *ns;
1143 /* Create the root namespace */
1144 root_ns = mlx5_vzalloc(sizeof(*root_ns));
1149 root_ns->table_type = table_type;
1152 fs_init_namespace(ns);
1153 mutex_init(&root_ns->chain_lock);
1154 tree_init_node(&ns->node, 1, NULL);
1155 tree_add_node(&ns->node, NULL);
1160 static int init_root_ns(struct mlx5_core_dev *dev)
1162 int max_ft_level = MLX5_CAP_FLOWTABLE(dev,
1163 flow_table_properties_nic_receive.
1166 dev->priv.root_ns = create_root_ns(dev, FS_FT_NIC_RX);
1167 if (IS_ERR_OR_NULL(dev->priv.root_ns))
1170 if (init_root_tree(max_ft_level, &root_fs, &dev->priv.root_ns->ns.node))
1176 mlx5_cleanup_fs(dev);
1180 static void cleanup_single_prio_root_ns(struct mlx5_core_dev *dev,
1181 struct mlx5_flow_root_namespace *root_ns)
1183 struct fs_node *prio;
1188 if (!list_empty(&root_ns->ns.node.children)) {
1189 prio = list_first_entry(&root_ns->ns.node.children,
1192 if (tree_remove_node(prio))
1194 "Flow steering priority wasn't destroyed, refcount > 1\n");
1196 if (tree_remove_node(&root_ns->ns.node))
1198 "Flow steering namespace wasn't destroyed, refcount > 1\n");
1202 static void cleanup_root_ns(struct mlx5_core_dev *dev)
1204 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1205 struct fs_prio *iter_prio;
1207 if (!MLX5_CAP_GEN(dev, nic_flow_table))
1214 fs_for_each_prio(iter_prio, &root_ns->ns) {
1215 struct fs_node *node;
1216 struct mlx5_flow_namespace *iter_ns;
1218 fs_for_each_ns_or_ft(node, iter_prio) {
1219 if (node->type == FS_TYPE_FLOW_TABLE)
1221 fs_get_obj(iter_ns, node);
1222 while (!list_empty(&iter_ns->node.children)) {
1223 struct fs_prio *obj_iter_prio2;
1224 struct fs_node *iter_prio2 =
1225 list_first_entry(&iter_ns->node.children,
1229 fs_get_obj(obj_iter_prio2, iter_prio2);
1230 if (tree_remove_node(iter_prio2)) {
1232 "Priority %d wasn't destroyed, refcount > 1\n",
1233 obj_iter_prio2->prio);
1241 fs_for_each_prio(iter_prio, &root_ns->ns) {
1242 while (!list_empty(&iter_prio->node.children)) {
1243 struct fs_node *iter_ns =
1244 list_first_entry(&iter_prio->node.children,
1247 if (tree_remove_node(iter_ns)) {
1249 "Namespace wasn't destroyed, refcount > 1\n");
1256 while (!list_empty(&root_ns->ns.node.children)) {
1257 struct fs_prio *obj_prio_node;
1258 struct fs_node *prio_node =
1259 list_first_entry(&root_ns->ns.node.children,
1263 fs_get_obj(obj_prio_node, prio_node);
1264 if (tree_remove_node(prio_node)) {
1266 "Priority %d wasn't destroyed, refcount > 1\n",
1267 obj_prio_node->prio);
1272 if (tree_remove_node(&root_ns->ns.node)) {
1274 "root namespace wasn't destroyed, refcount > 1\n");
1278 dev->priv.root_ns = NULL;
1281 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1283 cleanup_root_ns(dev);
1284 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1287 static int init_fdb_root_ns(struct mlx5_core_dev *dev)
1289 struct fs_prio *prio;
1291 dev->priv.fdb_root_ns = create_root_ns(dev, FS_FT_FDB);
1292 if (!dev->priv.fdb_root_ns)
1295 /* Create single prio */
1296 prio = fs_create_prio(&dev->priv.fdb_root_ns->ns, 0, 1, 0);
1298 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1299 return PTR_ERR(prio);
1305 int mlx5_init_fs(struct mlx5_core_dev *dev)
1309 if (MLX5_CAP_GEN(dev, nic_flow_table)) {
1310 err = init_root_ns(dev);
1314 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
1315 err = init_fdb_root_ns(dev);
1317 cleanup_root_ns(dev);