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 2
77 #define KERNEL_NUM_PRIOS 1
78 #define KENREL_MIN_LEVEL 2
84 static struct init_tree_node {
85 enum fs_node_type type;
86 struct init_tree_node *children;
88 struct node_caps caps;
94 .type = FS_TYPE_NAMESPACE,
96 .children = (struct init_tree_node[]) {
97 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
98 FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en),
99 FS_CAP(flow_table_properties_nic_receive.modify_root),
100 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode),
101 FS_CAP(flow_table_properties_nic_receive.flow_table_modify)),
102 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS, BY_PASS_PRIO_MAX_FT))),
103 ADD_PRIO(0, KENREL_MIN_LEVEL, 0, {},
104 ADD_NS(ADD_MULTIPLE_PRIO(KERNEL_NUM_PRIOS, KERNEL_MAX_FT))),
105 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
106 FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en),
107 FS_CAP(flow_table_properties_nic_receive.modify_root),
108 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode),
109 FS_CAP(flow_table_properties_nic_receive.flow_table_modify)),
110 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_MAX_FT))),
114 enum fs_i_mutex_lock_class {
115 FS_MUTEX_GRANDPARENT,
120 static void del_rule(struct fs_node *node);
121 static void del_flow_table(struct fs_node *node);
122 static void del_flow_group(struct fs_node *node);
123 static void del_fte(struct fs_node *node);
125 static void tree_init_node(struct fs_node *node,
126 unsigned int refcount,
127 void (*remove_func)(struct fs_node *))
129 atomic_set(&node->refcount, refcount);
130 INIT_LIST_HEAD(&node->list);
131 INIT_LIST_HEAD(&node->children);
132 mutex_init(&node->lock);
133 node->remove_func = remove_func;
136 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
139 atomic_inc(&parent->refcount);
140 node->parent = parent;
142 /* Parent is the root */
146 node->root = parent->root;
149 static void tree_get_node(struct fs_node *node)
151 atomic_inc(&node->refcount);
154 static void nested_lock_ref_node(struct fs_node *node,
155 enum fs_i_mutex_lock_class class)
158 mutex_lock_nested(&node->lock, class);
159 atomic_inc(&node->refcount);
163 static void lock_ref_node(struct fs_node *node)
166 mutex_lock(&node->lock);
167 atomic_inc(&node->refcount);
171 static void unlock_ref_node(struct fs_node *node)
174 atomic_dec(&node->refcount);
175 mutex_unlock(&node->lock);
179 static void tree_put_node(struct fs_node *node)
181 struct fs_node *parent_node = node->parent;
183 lock_ref_node(parent_node);
184 if (atomic_dec_and_test(&node->refcount)) {
186 list_del_init(&node->list);
187 if (node->remove_func)
188 node->remove_func(node);
192 unlock_ref_node(parent_node);
193 if (!node && parent_node)
194 tree_put_node(parent_node);
197 static int tree_remove_node(struct fs_node *node)
199 if (atomic_read(&node->refcount) > 1)
205 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
208 struct fs_prio *iter_prio;
210 fs_for_each_prio(iter_prio, ns) {
211 if (iter_prio->prio == prio)
218 static unsigned int find_next_free_level(struct fs_prio *prio)
220 if (!list_empty(&prio->node.children)) {
221 struct mlx5_flow_table *ft;
223 ft = list_last_entry(&prio->node.children,
224 struct mlx5_flow_table,
226 return ft->level + 1;
228 return prio->start_level;
231 static bool masked_memcmp(void *mask, void *val1, void *val2, size_t size)
235 for (i = 0; i < size; i++, mask++, val1++, val2++)
236 if ((*((u8 *)val1) & (*(u8 *)mask)) !=
237 ((*(u8 *)val2) & (*(u8 *)mask)))
243 static bool compare_match_value(struct mlx5_flow_group_mask *mask,
244 void *fte_param1, void *fte_param2)
246 if (mask->match_criteria_enable &
247 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) {
248 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
249 fte_param1, outer_headers);
250 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
251 fte_param2, outer_headers);
252 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
253 mask->match_criteria, outer_headers);
255 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
256 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
260 if (mask->match_criteria_enable &
261 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) {
262 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
263 fte_param1, misc_parameters);
264 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
265 fte_param2, misc_parameters);
266 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
267 mask->match_criteria, misc_parameters);
269 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
270 MLX5_ST_SZ_BYTES(fte_match_set_misc)))
274 if (mask->match_criteria_enable &
275 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS) {
276 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
277 fte_param1, inner_headers);
278 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
279 fte_param2, inner_headers);
280 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
281 mask->match_criteria, inner_headers);
283 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
284 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
290 static bool compare_match_criteria(u8 match_criteria_enable1,
291 u8 match_criteria_enable2,
292 void *mask1, void *mask2)
294 return match_criteria_enable1 == match_criteria_enable2 &&
295 !memcmp(mask1, mask2, MLX5_ST_SZ_BYTES(fte_match_param));
298 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
300 struct fs_node *root;
301 struct mlx5_flow_namespace *ns;
305 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
306 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
310 ns = container_of(root, struct mlx5_flow_namespace, node);
311 return container_of(ns, struct mlx5_flow_root_namespace, ns);
314 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
316 struct mlx5_flow_root_namespace *root = find_root(node);
323 static void del_flow_table(struct fs_node *node)
325 struct mlx5_flow_table *ft;
326 struct mlx5_core_dev *dev;
327 struct fs_prio *prio;
330 fs_get_obj(ft, node);
331 dev = get_dev(&ft->node);
333 err = mlx5_cmd_destroy_flow_table(dev, ft);
335 pr_warn("flow steering can't destroy ft\n");
336 fs_get_obj(prio, ft->node.parent);
340 static void del_rule(struct fs_node *node)
342 struct mlx5_flow_rule *rule;
343 struct mlx5_flow_table *ft;
344 struct mlx5_flow_group *fg;
347 struct mlx5_core_dev *dev = get_dev(node);
348 int match_len = MLX5_ST_SZ_BYTES(fte_match_param);
351 match_value = mlx5_vzalloc(match_len);
353 pr_warn("failed to allocate inbox\n");
357 fs_get_obj(rule, node);
358 fs_get_obj(fte, rule->node.parent);
359 fs_get_obj(fg, fte->node.parent);
360 memcpy(match_value, fte->val, sizeof(fte->val));
361 fs_get_obj(ft, fg->node.parent);
362 list_del(&rule->node.list);
364 if (fte->dests_size) {
365 err = mlx5_cmd_update_fte(dev, ft,
368 pr_warn("%s can't del rule fg id=%d fte_index=%d\n",
369 __func__, fg->id, fte->index);
374 static void del_fte(struct fs_node *node)
376 struct mlx5_flow_table *ft;
377 struct mlx5_flow_group *fg;
378 struct mlx5_core_dev *dev;
382 fs_get_obj(fte, node);
383 fs_get_obj(fg, fte->node.parent);
384 fs_get_obj(ft, fg->node.parent);
386 dev = get_dev(&ft->node);
387 err = mlx5_cmd_delete_fte(dev, ft,
390 pr_warn("flow steering can't delete fte in index %d of flow group id %d\n",
397 static void del_flow_group(struct fs_node *node)
399 struct mlx5_flow_group *fg;
400 struct mlx5_flow_table *ft;
401 struct mlx5_core_dev *dev;
403 fs_get_obj(fg, node);
404 fs_get_obj(ft, fg->node.parent);
405 dev = get_dev(&ft->node);
407 if (mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
408 pr_warn("flow steering can't destroy fg %d of ft %d\n",
412 static struct fs_fte *alloc_fte(u8 action,
419 fte = kzalloc(sizeof(*fte), GFP_KERNEL);
421 return ERR_PTR(-ENOMEM);
423 memcpy(fte->val, match_value, sizeof(fte->val));
424 fte->node.type = FS_TYPE_FLOW_ENTRY;
425 fte->flow_tag = flow_tag;
427 fte->action = action;
432 static struct mlx5_flow_group *alloc_flow_group(u32 *create_fg_in)
434 struct mlx5_flow_group *fg;
435 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
436 create_fg_in, match_criteria);
437 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
439 match_criteria_enable);
440 fg = kzalloc(sizeof(*fg), GFP_KERNEL);
442 return ERR_PTR(-ENOMEM);
444 fg->mask.match_criteria_enable = match_criteria_enable;
445 memcpy(&fg->mask.match_criteria, match_criteria,
446 sizeof(fg->mask.match_criteria));
447 fg->node.type = FS_TYPE_FLOW_GROUP;
448 fg->start_index = MLX5_GET(create_flow_group_in, create_fg_in,
450 fg->max_ftes = MLX5_GET(create_flow_group_in, create_fg_in,
451 end_flow_index) - fg->start_index + 1;
455 static struct mlx5_flow_table *alloc_flow_table(int level, int max_fte,
456 enum fs_flow_table_type table_type)
458 struct mlx5_flow_table *ft;
460 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
465 ft->node.type = FS_TYPE_FLOW_TABLE;
466 ft->type = table_type;
467 ft->max_fte = max_fte;
472 /* If reverse is false, then we search for the first flow table in the
473 * root sub-tree from start(closest from right), else we search for the
474 * last flow table in the root sub-tree till start(closest from left).
476 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
477 struct list_head *start,
480 #define list_advance_entry(pos, reverse) \
481 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
483 #define list_for_each_advance_continue(pos, head, reverse) \
484 for (pos = list_advance_entry(pos, reverse); \
485 &pos->list != (head); \
486 pos = list_advance_entry(pos, reverse))
488 struct fs_node *iter = list_entry(start, struct fs_node, list);
489 struct mlx5_flow_table *ft = NULL;
494 list_for_each_advance_continue(iter, &root->children, reverse) {
495 if (iter->type == FS_TYPE_FLOW_TABLE) {
496 fs_get_obj(ft, iter);
499 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
507 /* If reverse if false then return the first flow table in next priority of
508 * prio in the tree, else return the last flow table in the previous priority
509 * of prio in the tree.
511 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
513 struct mlx5_flow_table *ft = NULL;
514 struct fs_node *curr_node;
515 struct fs_node *parent;
517 parent = prio->node.parent;
518 curr_node = &prio->node;
519 while (!ft && parent) {
520 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
522 parent = curr_node->parent;
527 /* Assuming all the tree is locked by mutex chain lock */
528 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
530 return find_closest_ft(prio, false);
533 /* Assuming all the tree is locked by mutex chain lock */
534 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
536 return find_closest_ft(prio, true);
539 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
540 struct fs_prio *prio,
541 struct mlx5_flow_table *ft)
543 struct mlx5_flow_table *iter;
547 fs_for_each_ft(iter, prio) {
549 err = mlx5_cmd_modify_flow_table(dev,
553 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
555 /* The driver is out of sync with the FW */
564 /* Connect flow tables from previous priority of prio to ft */
565 static int connect_prev_fts(struct mlx5_core_dev *dev,
566 struct mlx5_flow_table *ft,
567 struct fs_prio *prio)
569 struct mlx5_flow_table *prev_ft;
571 prev_ft = find_prev_chained_ft(prio);
573 struct fs_prio *prev_prio;
575 fs_get_obj(prev_prio, prev_ft->node.parent);
576 return connect_fts_in_prio(dev, prev_prio, ft);
581 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
584 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
585 int min_level = INT_MAX;
589 min_level = root->root_ft->level;
591 if (ft->level >= min_level)
594 err = mlx5_cmd_update_root_ft(root->dev, ft);
596 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
604 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
605 struct fs_prio *prio)
609 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
611 if (list_empty(&prio->node.children)) {
612 err = connect_prev_fts(dev, ft, prio);
617 if (MLX5_CAP_FLOWTABLE(dev,
618 flow_table_properties_nic_receive.modify_root))
619 err = update_root_ft_create(ft, prio);
623 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
627 struct mlx5_flow_table *next_ft = NULL;
628 struct mlx5_flow_table *ft;
631 struct mlx5_flow_root_namespace *root =
632 find_root(&ns->node);
633 struct fs_prio *fs_prio = NULL;
636 pr_err("mlx5: flow steering failed to find root of namespace\n");
637 return ERR_PTR(-ENODEV);
640 mutex_lock(&root->chain_lock);
641 fs_prio = find_prio(ns, prio);
646 if (fs_prio->num_ft == fs_prio->max_ft) {
651 ft = alloc_flow_table(find_next_free_level(fs_prio),
652 roundup_pow_of_two(max_fte),
659 tree_init_node(&ft->node, 1, del_flow_table);
660 log_table_sz = ilog2(ft->max_fte);
661 next_ft = find_next_chained_ft(fs_prio);
662 err = mlx5_cmd_create_flow_table(root->dev, ft->type, ft->level,
663 log_table_sz, next_ft, &ft->id);
667 err = connect_flow_table(root->dev, ft, fs_prio);
670 lock_ref_node(&fs_prio->node);
671 tree_add_node(&ft->node, &fs_prio->node);
672 list_add_tail(&ft->node.list, &fs_prio->node.children);
674 unlock_ref_node(&fs_prio->node);
675 mutex_unlock(&root->chain_lock);
678 mlx5_cmd_destroy_flow_table(root->dev, ft);
682 mutex_unlock(&root->chain_lock);
686 struct mlx5_flow_table *mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
688 int num_flow_table_entries,
691 struct mlx5_flow_table *ft;
693 if (max_num_groups > num_flow_table_entries)
694 return ERR_PTR(-EINVAL);
696 ft = mlx5_create_flow_table(ns, prio, num_flow_table_entries);
700 ft->autogroup.active = true;
701 ft->autogroup.required_groups = max_num_groups;
705 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
707 /* Flow table should be locked */
708 static struct mlx5_flow_group *create_flow_group_common(struct mlx5_flow_table *ft,
714 struct mlx5_flow_group *fg;
715 struct mlx5_core_dev *dev = get_dev(&ft->node);
719 return ERR_PTR(-ENODEV);
721 fg = alloc_flow_group(fg_in);
725 err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
731 if (ft->autogroup.active)
732 ft->autogroup.num_groups++;
733 /* Add node to tree */
734 tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
735 tree_add_node(&fg->node, &ft->node);
736 /* Add node to group list */
737 list_add(&fg->node.list, ft->node.children.prev);
742 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
745 struct mlx5_flow_group *fg;
747 if (ft->autogroup.active)
748 return ERR_PTR(-EPERM);
750 lock_ref_node(&ft->node);
751 fg = create_flow_group_common(ft, fg_in, &ft->node.children, false);
752 unlock_ref_node(&ft->node);
757 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
759 struct mlx5_flow_rule *rule;
761 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
765 rule->node.type = FS_TYPE_FLOW_DEST;
766 memcpy(&rule->dest_attr, dest, sizeof(*dest));
771 /* fte should not be deleted while calling this function */
772 static struct mlx5_flow_rule *add_rule_fte(struct fs_fte *fte,
773 struct mlx5_flow_group *fg,
774 struct mlx5_flow_destination *dest)
776 struct mlx5_flow_table *ft;
777 struct mlx5_flow_rule *rule;
780 rule = alloc_rule(dest);
782 return ERR_PTR(-ENOMEM);
784 fs_get_obj(ft, fg->node.parent);
785 /* Add dest to dests list- added as first element after the head */
786 tree_init_node(&rule->node, 1, del_rule);
787 list_add_tail(&rule->node.list, &fte->node.children);
789 if (fte->dests_size == 1)
790 err = mlx5_cmd_create_fte(get_dev(&ft->node),
793 err = mlx5_cmd_update_fte(get_dev(&ft->node),
798 fte->status |= FS_FTE_STATUS_EXISTING;
803 list_del(&rule->node.list);
809 /* Assumed fg is locked */
810 static unsigned int get_free_fte_index(struct mlx5_flow_group *fg,
811 struct list_head **prev)
814 unsigned int start = fg->start_index;
817 *prev = &fg->node.children;
819 /* assumed list is sorted by index */
820 fs_for_each_fte(fte, fg) {
821 if (fte->index != start)
825 *prev = &fte->node.list;
831 /* prev is output, prev->next = new_fte */
832 static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
836 struct list_head **prev)
841 index = get_free_fte_index(fg, prev);
842 fte = alloc_fte(action, flow_tag, match_value, index);
849 static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
850 u8 match_criteria_enable,
853 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
854 struct list_head *prev = &ft->node.children;
855 unsigned int candidate_index = 0;
856 struct mlx5_flow_group *fg;
857 void *match_criteria_addr;
858 unsigned int group_size = 0;
861 if (!ft->autogroup.active)
862 return ERR_PTR(-ENOENT);
864 in = mlx5_vzalloc(inlen);
866 return ERR_PTR(-ENOMEM);
868 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
869 /* We save place for flow groups in addition to max types */
870 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
872 /* ft->max_fte == ft->autogroup.max_types */
876 /* sorted by start_index */
877 fs_for_each_fg(fg, ft) {
878 if (candidate_index + group_size > fg->start_index)
879 candidate_index = fg->start_index + fg->max_ftes;
882 prev = &fg->node.list;
885 if (candidate_index + group_size > ft->max_fte) {
886 fg = ERR_PTR(-ENOSPC);
890 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
891 match_criteria_enable);
892 MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
893 MLX5_SET(create_flow_group_in, in, end_flow_index, candidate_index +
895 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
897 memcpy(match_criteria_addr, match_criteria,
898 MLX5_ST_SZ_BYTES(fte_match_param));
900 fg = create_flow_group_common(ft, in, prev, true);
906 static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
910 struct mlx5_flow_destination *dest)
913 struct mlx5_flow_rule *rule;
914 struct mlx5_flow_table *ft;
915 struct list_head *prev;
917 nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
918 fs_for_each_fte(fte, fg) {
919 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
920 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
921 action == fte->action && flow_tag == fte->flow_tag) {
922 rule = add_rule_fte(fte, fg, dest);
923 unlock_ref_node(&fte->node);
929 unlock_ref_node(&fte->node);
931 fs_get_obj(ft, fg->node.parent);
932 if (fg->num_ftes >= fg->max_ftes) {
933 rule = ERR_PTR(-ENOSPC);
937 fte = create_fte(fg, match_value, action, flow_tag, &prev);
942 tree_init_node(&fte->node, 0, del_fte);
943 rule = add_rule_fte(fte, fg, dest);
951 tree_add_node(&fte->node, &fg->node);
952 list_add(&fte->node.list, prev);
954 tree_add_node(&rule->node, &fte->node);
956 unlock_ref_node(&fg->node);
960 static struct mlx5_flow_rule *add_rule_to_auto_fg(struct mlx5_flow_table *ft,
961 u8 match_criteria_enable,
966 struct mlx5_flow_destination *dest)
968 struct mlx5_flow_rule *rule;
969 struct mlx5_flow_group *g;
971 g = create_autogroup(ft, match_criteria_enable, match_criteria);
975 rule = add_rule_fg(g, match_value,
976 action, flow_tag, dest);
978 /* Remove assumes refcount > 0 and autogroup creates a group
979 * with a refcount = 0.
981 tree_get_node(&g->node);
982 tree_remove_node(&g->node);
987 struct mlx5_flow_rule *
988 mlx5_add_flow_rule(struct mlx5_flow_table *ft,
989 u8 match_criteria_enable,
994 struct mlx5_flow_destination *dest)
996 struct mlx5_flow_group *g;
997 struct mlx5_flow_rule *rule;
999 nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
1000 fs_for_each_fg(g, ft)
1001 if (compare_match_criteria(g->mask.match_criteria_enable,
1002 match_criteria_enable,
1003 g->mask.match_criteria,
1005 rule = add_rule_fg(g, match_value,
1006 action, flow_tag, dest);
1007 if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
1011 rule = add_rule_to_auto_fg(ft, match_criteria_enable, match_criteria,
1012 match_value, action, flow_tag, dest);
1014 unlock_ref_node(&ft->node);
1017 EXPORT_SYMBOL(mlx5_add_flow_rule);
1019 void mlx5_del_flow_rule(struct mlx5_flow_rule *rule)
1021 tree_remove_node(&rule->node);
1023 EXPORT_SYMBOL(mlx5_del_flow_rule);
1025 /* Assuming prio->node.children(flow tables) is sorted by level */
1026 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1028 struct fs_prio *prio;
1030 fs_get_obj(prio, ft->node.parent);
1032 if (!list_is_last(&ft->node.list, &prio->node.children))
1033 return list_next_entry(ft, node.list);
1034 return find_next_chained_ft(prio);
1037 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1039 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1040 struct mlx5_flow_table *new_root_ft = NULL;
1042 if (root->root_ft != ft)
1045 new_root_ft = find_next_ft(ft);
1047 int err = mlx5_cmd_update_root_ft(root->dev, new_root_ft);
1050 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
1054 root->root_ft = new_root_ft;
1059 /* Connect flow table from previous priority to
1060 * the next flow table.
1062 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1064 struct mlx5_core_dev *dev = get_dev(&ft->node);
1065 struct mlx5_flow_table *next_ft;
1066 struct fs_prio *prio;
1069 err = update_root_ft_destroy(ft);
1073 fs_get_obj(prio, ft->node.parent);
1074 if (!(list_first_entry(&prio->node.children,
1075 struct mlx5_flow_table,
1079 next_ft = find_next_chained_ft(prio);
1080 err = connect_prev_fts(dev, next_ft, prio);
1082 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1087 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1089 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1092 mutex_lock(&root->chain_lock);
1093 err = disconnect_flow_table(ft);
1095 mutex_unlock(&root->chain_lock);
1098 if (tree_remove_node(&ft->node))
1099 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1101 mutex_unlock(&root->chain_lock);
1105 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1107 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1109 if (tree_remove_node(&fg->node))
1110 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1114 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1115 enum mlx5_flow_namespace_type type)
1117 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1119 static struct fs_prio *fs_prio;
1120 struct mlx5_flow_namespace *ns;
1126 case MLX5_FLOW_NAMESPACE_BYPASS:
1127 case MLX5_FLOW_NAMESPACE_KERNEL:
1128 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
1131 case MLX5_FLOW_NAMESPACE_FDB:
1132 if (dev->priv.fdb_root_ns)
1133 return &dev->priv.fdb_root_ns->ns;
1140 fs_prio = find_prio(&root_ns->ns, prio);
1144 ns = list_first_entry(&fs_prio->node.children,
1150 EXPORT_SYMBOL(mlx5_get_flow_namespace);
1152 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
1153 unsigned prio, int max_ft)
1155 struct fs_prio *fs_prio;
1157 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
1159 return ERR_PTR(-ENOMEM);
1161 fs_prio->node.type = FS_TYPE_PRIO;
1162 tree_init_node(&fs_prio->node, 1, NULL);
1163 tree_add_node(&fs_prio->node, &ns->node);
1164 fs_prio->max_ft = max_ft;
1165 fs_prio->prio = prio;
1166 list_add_tail(&fs_prio->node.list, &ns->node.children);
1171 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
1174 ns->node.type = FS_TYPE_NAMESPACE;
1179 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
1181 struct mlx5_flow_namespace *ns;
1183 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1185 return ERR_PTR(-ENOMEM);
1187 fs_init_namespace(ns);
1188 tree_init_node(&ns->node, 1, NULL);
1189 tree_add_node(&ns->node, &prio->node);
1190 list_add_tail(&ns->node.list, &prio->node.children);
1195 static int create_leaf_prios(struct mlx5_flow_namespace *ns, struct init_tree_node
1198 struct fs_prio *fs_prio;
1201 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
1202 fs_prio = fs_create_prio(ns, i, prio_metadata->max_ft);
1203 if (IS_ERR(fs_prio))
1204 return PTR_ERR(fs_prio);
1209 #define FLOW_TABLE_BIT_SZ 1
1210 #define GET_FLOW_TABLE_CAP(dev, offset) \
1211 ((be32_to_cpu(*((__be32 *)(dev->hca_caps_cur[MLX5_CAP_FLOW_TABLE]) + \
1213 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
1214 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
1218 for (i = 0; i < caps->arr_sz; i++) {
1219 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
1225 static int init_root_tree_recursive(struct mlx5_core_dev *dev,
1226 struct init_tree_node *init_node,
1227 struct fs_node *fs_parent_node,
1228 struct init_tree_node *init_parent_node,
1231 int max_ft_level = MLX5_CAP_FLOWTABLE(dev,
1232 flow_table_properties_nic_receive.
1234 struct mlx5_flow_namespace *fs_ns;
1235 struct fs_prio *fs_prio;
1236 struct fs_node *base;
1240 if (init_node->type == FS_TYPE_PRIO) {
1241 if ((init_node->min_ft_level > max_ft_level) ||
1242 !has_required_caps(dev, &init_node->caps))
1245 fs_get_obj(fs_ns, fs_parent_node);
1246 if (init_node->num_leaf_prios)
1247 return create_leaf_prios(fs_ns, init_node);
1248 fs_prio = fs_create_prio(fs_ns, index, init_node->max_ft);
1249 if (IS_ERR(fs_prio))
1250 return PTR_ERR(fs_prio);
1251 base = &fs_prio->node;
1252 } else if (init_node->type == FS_TYPE_NAMESPACE) {
1253 fs_get_obj(fs_prio, fs_parent_node);
1254 fs_ns = fs_create_namespace(fs_prio);
1256 return PTR_ERR(fs_ns);
1257 base = &fs_ns->node;
1261 for (i = 0; i < init_node->ar_size; i++) {
1262 err = init_root_tree_recursive(dev, &init_node->children[i],
1263 base, init_node, i);
1271 static int init_root_tree(struct mlx5_core_dev *dev,
1272 struct init_tree_node *init_node,
1273 struct fs_node *fs_parent_node)
1276 struct mlx5_flow_namespace *fs_ns;
1279 fs_get_obj(fs_ns, fs_parent_node);
1280 for (i = 0; i < init_node->ar_size; i++) {
1281 err = init_root_tree_recursive(dev, &init_node->children[i],
1290 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_core_dev *dev,
1291 enum fs_flow_table_type
1294 struct mlx5_flow_root_namespace *root_ns;
1295 struct mlx5_flow_namespace *ns;
1297 /* Create the root namespace */
1298 root_ns = mlx5_vzalloc(sizeof(*root_ns));
1303 root_ns->table_type = table_type;
1306 fs_init_namespace(ns);
1307 mutex_init(&root_ns->chain_lock);
1308 tree_init_node(&ns->node, 1, NULL);
1309 tree_add_node(&ns->node, NULL);
1314 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
1316 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
1318 struct fs_prio *prio;
1320 fs_for_each_prio(prio, ns) {
1321 /* This updates prio start_level and max_ft */
1322 set_prio_attrs_in_prio(prio, acc_level);
1323 acc_level += prio->max_ft;
1328 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
1330 struct mlx5_flow_namespace *ns;
1331 int acc_level_ns = acc_level;
1333 prio->start_level = acc_level;
1334 fs_for_each_ns(ns, prio)
1335 /* This updates start_level and max_ft of ns's priority descendants */
1336 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
1338 prio->max_ft = acc_level_ns - prio->start_level;
1339 WARN_ON(prio->max_ft < acc_level_ns - prio->start_level);
1342 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
1344 struct mlx5_flow_namespace *ns = &root_ns->ns;
1345 struct fs_prio *prio;
1346 int start_level = 0;
1348 fs_for_each_prio(prio, ns) {
1349 set_prio_attrs_in_prio(prio, start_level);
1350 start_level += prio->max_ft;
1354 static int init_root_ns(struct mlx5_core_dev *dev)
1357 dev->priv.root_ns = create_root_ns(dev, FS_FT_NIC_RX);
1358 if (IS_ERR_OR_NULL(dev->priv.root_ns))
1361 if (init_root_tree(dev, &root_fs, &dev->priv.root_ns->ns.node))
1364 set_prio_attrs(dev->priv.root_ns);
1369 mlx5_cleanup_fs(dev);
1373 static void cleanup_single_prio_root_ns(struct mlx5_core_dev *dev,
1374 struct mlx5_flow_root_namespace *root_ns)
1376 struct fs_node *prio;
1381 if (!list_empty(&root_ns->ns.node.children)) {
1382 prio = list_first_entry(&root_ns->ns.node.children,
1385 if (tree_remove_node(prio))
1387 "Flow steering priority wasn't destroyed, refcount > 1\n");
1389 if (tree_remove_node(&root_ns->ns.node))
1391 "Flow steering namespace wasn't destroyed, refcount > 1\n");
1395 static void cleanup_root_ns(struct mlx5_core_dev *dev)
1397 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1398 struct fs_prio *iter_prio;
1400 if (!MLX5_CAP_GEN(dev, nic_flow_table))
1407 fs_for_each_prio(iter_prio, &root_ns->ns) {
1408 struct fs_node *node;
1409 struct mlx5_flow_namespace *iter_ns;
1411 fs_for_each_ns_or_ft(node, iter_prio) {
1412 if (node->type == FS_TYPE_FLOW_TABLE)
1414 fs_get_obj(iter_ns, node);
1415 while (!list_empty(&iter_ns->node.children)) {
1416 struct fs_prio *obj_iter_prio2;
1417 struct fs_node *iter_prio2 =
1418 list_first_entry(&iter_ns->node.children,
1422 fs_get_obj(obj_iter_prio2, iter_prio2);
1423 if (tree_remove_node(iter_prio2)) {
1425 "Priority %d wasn't destroyed, refcount > 1\n",
1426 obj_iter_prio2->prio);
1434 fs_for_each_prio(iter_prio, &root_ns->ns) {
1435 while (!list_empty(&iter_prio->node.children)) {
1436 struct fs_node *iter_ns =
1437 list_first_entry(&iter_prio->node.children,
1440 if (tree_remove_node(iter_ns)) {
1442 "Namespace wasn't destroyed, refcount > 1\n");
1449 while (!list_empty(&root_ns->ns.node.children)) {
1450 struct fs_prio *obj_prio_node;
1451 struct fs_node *prio_node =
1452 list_first_entry(&root_ns->ns.node.children,
1456 fs_get_obj(obj_prio_node, prio_node);
1457 if (tree_remove_node(prio_node)) {
1459 "Priority %d wasn't destroyed, refcount > 1\n",
1460 obj_prio_node->prio);
1465 if (tree_remove_node(&root_ns->ns.node)) {
1467 "root namespace wasn't destroyed, refcount > 1\n");
1471 dev->priv.root_ns = NULL;
1474 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1476 cleanup_root_ns(dev);
1477 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1480 static int init_fdb_root_ns(struct mlx5_core_dev *dev)
1482 struct fs_prio *prio;
1484 dev->priv.fdb_root_ns = create_root_ns(dev, FS_FT_FDB);
1485 if (!dev->priv.fdb_root_ns)
1488 /* Create single prio */
1489 prio = fs_create_prio(&dev->priv.fdb_root_ns->ns, 0, 1);
1491 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1492 return PTR_ERR(prio);
1498 int mlx5_init_fs(struct mlx5_core_dev *dev)
1502 if (MLX5_CAP_GEN(dev, nic_flow_table)) {
1503 err = init_root_ns(dev);
1507 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
1508 err = init_fdb_root_ns(dev);
1510 cleanup_root_ns(dev);