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
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);
363 if ((fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
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;
767 memcpy(&rule->dest_attr, dest, sizeof(*dest));
772 /* fte should not be deleted while calling this function */
773 static struct mlx5_flow_rule *add_rule_fte(struct fs_fte *fte,
774 struct mlx5_flow_group *fg,
775 struct mlx5_flow_destination *dest)
777 struct mlx5_flow_table *ft;
778 struct mlx5_flow_rule *rule;
781 rule = alloc_rule(dest);
783 return ERR_PTR(-ENOMEM);
785 fs_get_obj(ft, fg->node.parent);
786 /* Add dest to dests list- added as first element after the head */
787 tree_init_node(&rule->node, 1, del_rule);
788 list_add_tail(&rule->node.list, &fte->node.children);
791 if (fte->dests_size == 1 || !dest)
792 err = mlx5_cmd_create_fte(get_dev(&ft->node),
795 err = mlx5_cmd_update_fte(get_dev(&ft->node),
800 fte->status |= FS_FTE_STATUS_EXISTING;
805 list_del(&rule->node.list);
812 /* Assumed fg is locked */
813 static unsigned int get_free_fte_index(struct mlx5_flow_group *fg,
814 struct list_head **prev)
817 unsigned int start = fg->start_index;
820 *prev = &fg->node.children;
822 /* assumed list is sorted by index */
823 fs_for_each_fte(fte, fg) {
824 if (fte->index != start)
828 *prev = &fte->node.list;
834 /* prev is output, prev->next = new_fte */
835 static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
839 struct list_head **prev)
844 index = get_free_fte_index(fg, prev);
845 fte = alloc_fte(action, flow_tag, match_value, index);
852 static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
853 u8 match_criteria_enable,
856 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
857 struct list_head *prev = &ft->node.children;
858 unsigned int candidate_index = 0;
859 struct mlx5_flow_group *fg;
860 void *match_criteria_addr;
861 unsigned int group_size = 0;
864 if (!ft->autogroup.active)
865 return ERR_PTR(-ENOENT);
867 in = mlx5_vzalloc(inlen);
869 return ERR_PTR(-ENOMEM);
871 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
872 /* We save place for flow groups in addition to max types */
873 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
875 /* ft->max_fte == ft->autogroup.max_types */
879 /* sorted by start_index */
880 fs_for_each_fg(fg, ft) {
881 if (candidate_index + group_size > fg->start_index)
882 candidate_index = fg->start_index + fg->max_ftes;
885 prev = &fg->node.list;
888 if (candidate_index + group_size > ft->max_fte) {
889 fg = ERR_PTR(-ENOSPC);
893 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
894 match_criteria_enable);
895 MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
896 MLX5_SET(create_flow_group_in, in, end_flow_index, candidate_index +
898 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
900 memcpy(match_criteria_addr, match_criteria,
901 MLX5_ST_SZ_BYTES(fte_match_param));
903 fg = create_flow_group_common(ft, in, prev, true);
909 static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
913 struct mlx5_flow_destination *dest)
916 struct mlx5_flow_rule *rule;
917 struct mlx5_flow_table *ft;
918 struct list_head *prev;
920 nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
921 fs_for_each_fte(fte, fg) {
922 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
923 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
924 action == fte->action && flow_tag == fte->flow_tag) {
925 rule = add_rule_fte(fte, fg, dest);
926 unlock_ref_node(&fte->node);
932 unlock_ref_node(&fte->node);
934 fs_get_obj(ft, fg->node.parent);
935 if (fg->num_ftes >= fg->max_ftes) {
936 rule = ERR_PTR(-ENOSPC);
940 fte = create_fte(fg, match_value, action, flow_tag, &prev);
945 tree_init_node(&fte->node, 0, del_fte);
946 rule = add_rule_fte(fte, fg, dest);
954 tree_add_node(&fte->node, &fg->node);
955 list_add(&fte->node.list, prev);
957 tree_add_node(&rule->node, &fte->node);
959 unlock_ref_node(&fg->node);
963 static struct mlx5_flow_rule *add_rule_to_auto_fg(struct mlx5_flow_table *ft,
964 u8 match_criteria_enable,
969 struct mlx5_flow_destination *dest)
971 struct mlx5_flow_rule *rule;
972 struct mlx5_flow_group *g;
974 g = create_autogroup(ft, match_criteria_enable, match_criteria);
978 rule = add_rule_fg(g, match_value,
979 action, flow_tag, dest);
981 /* Remove assumes refcount > 0 and autogroup creates a group
982 * with a refcount = 0.
984 tree_get_node(&g->node);
985 tree_remove_node(&g->node);
990 struct mlx5_flow_rule *
991 mlx5_add_flow_rule(struct mlx5_flow_table *ft,
992 u8 match_criteria_enable,
997 struct mlx5_flow_destination *dest)
999 struct mlx5_flow_group *g;
1000 struct mlx5_flow_rule *rule;
1002 if ((action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) && !dest)
1003 return ERR_PTR(-EINVAL);
1005 nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
1006 fs_for_each_fg(g, ft)
1007 if (compare_match_criteria(g->mask.match_criteria_enable,
1008 match_criteria_enable,
1009 g->mask.match_criteria,
1011 rule = add_rule_fg(g, match_value,
1012 action, flow_tag, dest);
1013 if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
1017 rule = add_rule_to_auto_fg(ft, match_criteria_enable, match_criteria,
1018 match_value, action, flow_tag, dest);
1020 unlock_ref_node(&ft->node);
1023 EXPORT_SYMBOL(mlx5_add_flow_rule);
1025 void mlx5_del_flow_rule(struct mlx5_flow_rule *rule)
1027 tree_remove_node(&rule->node);
1029 EXPORT_SYMBOL(mlx5_del_flow_rule);
1031 /* Assuming prio->node.children(flow tables) is sorted by level */
1032 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1034 struct fs_prio *prio;
1036 fs_get_obj(prio, ft->node.parent);
1038 if (!list_is_last(&ft->node.list, &prio->node.children))
1039 return list_next_entry(ft, node.list);
1040 return find_next_chained_ft(prio);
1043 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1045 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1046 struct mlx5_flow_table *new_root_ft = NULL;
1048 if (root->root_ft != ft)
1051 new_root_ft = find_next_ft(ft);
1053 int err = mlx5_cmd_update_root_ft(root->dev, new_root_ft);
1056 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
1060 root->root_ft = new_root_ft;
1065 /* Connect flow table from previous priority to
1066 * the next flow table.
1068 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1070 struct mlx5_core_dev *dev = get_dev(&ft->node);
1071 struct mlx5_flow_table *next_ft;
1072 struct fs_prio *prio;
1075 err = update_root_ft_destroy(ft);
1079 fs_get_obj(prio, ft->node.parent);
1080 if (!(list_first_entry(&prio->node.children,
1081 struct mlx5_flow_table,
1085 next_ft = find_next_chained_ft(prio);
1086 err = connect_prev_fts(dev, next_ft, prio);
1088 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1093 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1095 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1098 mutex_lock(&root->chain_lock);
1099 err = disconnect_flow_table(ft);
1101 mutex_unlock(&root->chain_lock);
1104 if (tree_remove_node(&ft->node))
1105 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1107 mutex_unlock(&root->chain_lock);
1111 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1113 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1115 if (tree_remove_node(&fg->node))
1116 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1120 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1121 enum mlx5_flow_namespace_type type)
1123 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1125 static struct fs_prio *fs_prio;
1126 struct mlx5_flow_namespace *ns;
1132 case MLX5_FLOW_NAMESPACE_BYPASS:
1133 case MLX5_FLOW_NAMESPACE_KERNEL:
1134 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
1137 case MLX5_FLOW_NAMESPACE_FDB:
1138 if (dev->priv.fdb_root_ns)
1139 return &dev->priv.fdb_root_ns->ns;
1146 fs_prio = find_prio(&root_ns->ns, prio);
1150 ns = list_first_entry(&fs_prio->node.children,
1156 EXPORT_SYMBOL(mlx5_get_flow_namespace);
1158 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
1159 unsigned prio, int max_ft)
1161 struct fs_prio *fs_prio;
1163 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
1165 return ERR_PTR(-ENOMEM);
1167 fs_prio->node.type = FS_TYPE_PRIO;
1168 tree_init_node(&fs_prio->node, 1, NULL);
1169 tree_add_node(&fs_prio->node, &ns->node);
1170 fs_prio->max_ft = max_ft;
1171 fs_prio->prio = prio;
1172 list_add_tail(&fs_prio->node.list, &ns->node.children);
1177 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
1180 ns->node.type = FS_TYPE_NAMESPACE;
1185 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
1187 struct mlx5_flow_namespace *ns;
1189 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1191 return ERR_PTR(-ENOMEM);
1193 fs_init_namespace(ns);
1194 tree_init_node(&ns->node, 1, NULL);
1195 tree_add_node(&ns->node, &prio->node);
1196 list_add_tail(&ns->node.list, &prio->node.children);
1201 static int create_leaf_prios(struct mlx5_flow_namespace *ns, struct init_tree_node
1204 struct fs_prio *fs_prio;
1207 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
1208 fs_prio = fs_create_prio(ns, i, prio_metadata->max_ft);
1209 if (IS_ERR(fs_prio))
1210 return PTR_ERR(fs_prio);
1215 #define FLOW_TABLE_BIT_SZ 1
1216 #define GET_FLOW_TABLE_CAP(dev, offset) \
1217 ((be32_to_cpu(*((__be32 *)(dev->hca_caps_cur[MLX5_CAP_FLOW_TABLE]) + \
1219 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
1220 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
1224 for (i = 0; i < caps->arr_sz; i++) {
1225 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
1231 static int init_root_tree_recursive(struct mlx5_core_dev *dev,
1232 struct init_tree_node *init_node,
1233 struct fs_node *fs_parent_node,
1234 struct init_tree_node *init_parent_node,
1237 int max_ft_level = MLX5_CAP_FLOWTABLE(dev,
1238 flow_table_properties_nic_receive.
1240 struct mlx5_flow_namespace *fs_ns;
1241 struct fs_prio *fs_prio;
1242 struct fs_node *base;
1246 if (init_node->type == FS_TYPE_PRIO) {
1247 if ((init_node->min_ft_level > max_ft_level) ||
1248 !has_required_caps(dev, &init_node->caps))
1251 fs_get_obj(fs_ns, fs_parent_node);
1252 if (init_node->num_leaf_prios)
1253 return create_leaf_prios(fs_ns, init_node);
1254 fs_prio = fs_create_prio(fs_ns, index, init_node->max_ft);
1255 if (IS_ERR(fs_prio))
1256 return PTR_ERR(fs_prio);
1257 base = &fs_prio->node;
1258 } else if (init_node->type == FS_TYPE_NAMESPACE) {
1259 fs_get_obj(fs_prio, fs_parent_node);
1260 fs_ns = fs_create_namespace(fs_prio);
1262 return PTR_ERR(fs_ns);
1263 base = &fs_ns->node;
1267 for (i = 0; i < init_node->ar_size; i++) {
1268 err = init_root_tree_recursive(dev, &init_node->children[i],
1269 base, init_node, i);
1277 static int init_root_tree(struct mlx5_core_dev *dev,
1278 struct init_tree_node *init_node,
1279 struct fs_node *fs_parent_node)
1282 struct mlx5_flow_namespace *fs_ns;
1285 fs_get_obj(fs_ns, fs_parent_node);
1286 for (i = 0; i < init_node->ar_size; i++) {
1287 err = init_root_tree_recursive(dev, &init_node->children[i],
1296 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_core_dev *dev,
1297 enum fs_flow_table_type
1300 struct mlx5_flow_root_namespace *root_ns;
1301 struct mlx5_flow_namespace *ns;
1303 /* Create the root namespace */
1304 root_ns = mlx5_vzalloc(sizeof(*root_ns));
1309 root_ns->table_type = table_type;
1312 fs_init_namespace(ns);
1313 mutex_init(&root_ns->chain_lock);
1314 tree_init_node(&ns->node, 1, NULL);
1315 tree_add_node(&ns->node, NULL);
1320 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
1322 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
1324 struct fs_prio *prio;
1326 fs_for_each_prio(prio, ns) {
1327 /* This updates prio start_level and max_ft */
1328 set_prio_attrs_in_prio(prio, acc_level);
1329 acc_level += prio->max_ft;
1334 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
1336 struct mlx5_flow_namespace *ns;
1337 int acc_level_ns = acc_level;
1339 prio->start_level = acc_level;
1340 fs_for_each_ns(ns, prio)
1341 /* This updates start_level and max_ft of ns's priority descendants */
1342 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
1344 prio->max_ft = acc_level_ns - prio->start_level;
1345 WARN_ON(prio->max_ft < acc_level_ns - prio->start_level);
1348 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
1350 struct mlx5_flow_namespace *ns = &root_ns->ns;
1351 struct fs_prio *prio;
1352 int start_level = 0;
1354 fs_for_each_prio(prio, ns) {
1355 set_prio_attrs_in_prio(prio, start_level);
1356 start_level += prio->max_ft;
1360 static int init_root_ns(struct mlx5_core_dev *dev)
1363 dev->priv.root_ns = create_root_ns(dev, FS_FT_NIC_RX);
1364 if (IS_ERR_OR_NULL(dev->priv.root_ns))
1367 if (init_root_tree(dev, &root_fs, &dev->priv.root_ns->ns.node))
1370 set_prio_attrs(dev->priv.root_ns);
1375 mlx5_cleanup_fs(dev);
1379 static void cleanup_single_prio_root_ns(struct mlx5_core_dev *dev,
1380 struct mlx5_flow_root_namespace *root_ns)
1382 struct fs_node *prio;
1387 if (!list_empty(&root_ns->ns.node.children)) {
1388 prio = list_first_entry(&root_ns->ns.node.children,
1391 if (tree_remove_node(prio))
1393 "Flow steering priority wasn't destroyed, refcount > 1\n");
1395 if (tree_remove_node(&root_ns->ns.node))
1397 "Flow steering namespace wasn't destroyed, refcount > 1\n");
1401 static void cleanup_root_ns(struct mlx5_core_dev *dev)
1403 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1404 struct fs_prio *iter_prio;
1406 if (!MLX5_CAP_GEN(dev, nic_flow_table))
1413 fs_for_each_prio(iter_prio, &root_ns->ns) {
1414 struct fs_node *node;
1415 struct mlx5_flow_namespace *iter_ns;
1417 fs_for_each_ns_or_ft(node, iter_prio) {
1418 if (node->type == FS_TYPE_FLOW_TABLE)
1420 fs_get_obj(iter_ns, node);
1421 while (!list_empty(&iter_ns->node.children)) {
1422 struct fs_prio *obj_iter_prio2;
1423 struct fs_node *iter_prio2 =
1424 list_first_entry(&iter_ns->node.children,
1428 fs_get_obj(obj_iter_prio2, iter_prio2);
1429 if (tree_remove_node(iter_prio2)) {
1431 "Priority %d wasn't destroyed, refcount > 1\n",
1432 obj_iter_prio2->prio);
1440 fs_for_each_prio(iter_prio, &root_ns->ns) {
1441 while (!list_empty(&iter_prio->node.children)) {
1442 struct fs_node *iter_ns =
1443 list_first_entry(&iter_prio->node.children,
1446 if (tree_remove_node(iter_ns)) {
1448 "Namespace wasn't destroyed, refcount > 1\n");
1455 while (!list_empty(&root_ns->ns.node.children)) {
1456 struct fs_prio *obj_prio_node;
1457 struct fs_node *prio_node =
1458 list_first_entry(&root_ns->ns.node.children,
1462 fs_get_obj(obj_prio_node, prio_node);
1463 if (tree_remove_node(prio_node)) {
1465 "Priority %d wasn't destroyed, refcount > 1\n",
1466 obj_prio_node->prio);
1471 if (tree_remove_node(&root_ns->ns.node)) {
1473 "root namespace wasn't destroyed, refcount > 1\n");
1477 dev->priv.root_ns = NULL;
1480 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1482 cleanup_root_ns(dev);
1483 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1486 static int init_fdb_root_ns(struct mlx5_core_dev *dev)
1488 struct fs_prio *prio;
1490 dev->priv.fdb_root_ns = create_root_ns(dev, FS_FT_FDB);
1491 if (!dev->priv.fdb_root_ns)
1494 /* Create single prio */
1495 prio = fs_create_prio(&dev->priv.fdb_root_ns->ns, 0, 1);
1497 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1498 return PTR_ERR(prio);
1504 int mlx5_init_fs(struct mlx5_core_dev *dev)
1508 if (MLX5_CAP_GEN(dev, nic_flow_table)) {
1509 err = init_root_ns(dev);
1513 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
1514 err = init_fdb_root_ns(dev);
1516 cleanup_root_ns(dev);