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, num_levels_val, caps_val,\
44 ...) {.type = FS_TYPE_PRIO,\
45 .min_ft_level = min_level_val,\
46 .num_levels = num_levels_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, num_levels_val, ...)\
54 ADD_PRIO(num_prios_val, 0, num_levels_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_NUM_LEVELS 1
71 #define LEFTOVERS_NUM_PRIOS 1
73 #define BY_PASS_PRIO_NUM_LEVELS 1
74 #define BY_PASS_MIN_LEVEL (KERNEL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
77 /* Vlan, mac, ttc, aRFS */
78 #define KERNEL_NIC_PRIO_NUM_LEVELS 4
79 #define KERNEL_NIC_NUM_PRIOS 1
80 /* One more level for tc */
81 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
83 #define ANCHOR_NUM_LEVELS 1
84 #define ANCHOR_NUM_PRIOS 1
85 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
87 #define OFFLOADS_MAX_FT 1
88 #define OFFLOADS_NUM_PRIOS 1
89 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
95 static struct init_tree_node {
96 enum fs_node_type type;
97 struct init_tree_node *children;
99 struct node_caps caps;
105 .type = FS_TYPE_NAMESPACE,
107 .children = (struct init_tree_node[]) {
108 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
109 FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en),
110 FS_CAP(flow_table_properties_nic_receive.modify_root),
111 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode),
112 FS_CAP(flow_table_properties_nic_receive.flow_table_modify)),
113 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
114 BY_PASS_PRIO_NUM_LEVELS))),
115 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {},
116 ADD_NS(ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS, OFFLOADS_MAX_FT))),
118 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
119 ADD_NS(ADD_MULTIPLE_PRIO(1, 1),
120 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
121 KERNEL_NIC_PRIO_NUM_LEVELS))),
122 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
123 FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en),
124 FS_CAP(flow_table_properties_nic_receive.modify_root),
125 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode),
126 FS_CAP(flow_table_properties_nic_receive.flow_table_modify)),
127 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_NUM_LEVELS))),
128 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
129 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_NUM_LEVELS))),
133 enum fs_i_mutex_lock_class {
134 FS_MUTEX_GRANDPARENT,
139 static void del_rule(struct fs_node *node);
140 static void del_flow_table(struct fs_node *node);
141 static void del_flow_group(struct fs_node *node);
142 static void del_fte(struct fs_node *node);
144 static void tree_init_node(struct fs_node *node,
145 unsigned int refcount,
146 void (*remove_func)(struct fs_node *))
148 atomic_set(&node->refcount, refcount);
149 INIT_LIST_HEAD(&node->list);
150 INIT_LIST_HEAD(&node->children);
151 mutex_init(&node->lock);
152 node->remove_func = remove_func;
155 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
158 atomic_inc(&parent->refcount);
159 node->parent = parent;
161 /* Parent is the root */
165 node->root = parent->root;
168 static void tree_get_node(struct fs_node *node)
170 atomic_inc(&node->refcount);
173 static void nested_lock_ref_node(struct fs_node *node,
174 enum fs_i_mutex_lock_class class)
177 mutex_lock_nested(&node->lock, class);
178 atomic_inc(&node->refcount);
182 static void lock_ref_node(struct fs_node *node)
185 mutex_lock(&node->lock);
186 atomic_inc(&node->refcount);
190 static void unlock_ref_node(struct fs_node *node)
193 atomic_dec(&node->refcount);
194 mutex_unlock(&node->lock);
198 static void tree_put_node(struct fs_node *node)
200 struct fs_node *parent_node = node->parent;
202 lock_ref_node(parent_node);
203 if (atomic_dec_and_test(&node->refcount)) {
205 list_del_init(&node->list);
206 if (node->remove_func)
207 node->remove_func(node);
211 unlock_ref_node(parent_node);
212 if (!node && parent_node)
213 tree_put_node(parent_node);
216 static int tree_remove_node(struct fs_node *node)
218 if (atomic_read(&node->refcount) > 1) {
219 atomic_dec(&node->refcount);
226 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
229 struct fs_prio *iter_prio;
231 fs_for_each_prio(iter_prio, ns) {
232 if (iter_prio->prio == prio)
239 static bool masked_memcmp(void *mask, void *val1, void *val2, size_t size)
243 for (i = 0; i < size; i++, mask++, val1++, val2++)
244 if ((*((u8 *)val1) & (*(u8 *)mask)) !=
245 ((*(u8 *)val2) & (*(u8 *)mask)))
251 static bool compare_match_value(struct mlx5_flow_group_mask *mask,
252 void *fte_param1, void *fte_param2)
254 if (mask->match_criteria_enable &
255 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) {
256 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
257 fte_param1, outer_headers);
258 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
259 fte_param2, outer_headers);
260 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
261 mask->match_criteria, outer_headers);
263 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
264 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
268 if (mask->match_criteria_enable &
269 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) {
270 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
271 fte_param1, misc_parameters);
272 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
273 fte_param2, misc_parameters);
274 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
275 mask->match_criteria, misc_parameters);
277 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
278 MLX5_ST_SZ_BYTES(fte_match_set_misc)))
282 if (mask->match_criteria_enable &
283 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS) {
284 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
285 fte_param1, inner_headers);
286 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
287 fte_param2, inner_headers);
288 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
289 mask->match_criteria, inner_headers);
291 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
292 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
298 static bool compare_match_criteria(u8 match_criteria_enable1,
299 u8 match_criteria_enable2,
300 void *mask1, void *mask2)
302 return match_criteria_enable1 == match_criteria_enable2 &&
303 !memcmp(mask1, mask2, MLX5_ST_SZ_BYTES(fte_match_param));
306 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
308 struct fs_node *root;
309 struct mlx5_flow_namespace *ns;
313 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
314 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
318 ns = container_of(root, struct mlx5_flow_namespace, node);
319 return container_of(ns, struct mlx5_flow_root_namespace, ns);
322 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
324 struct mlx5_flow_root_namespace *root = find_root(node);
331 static void del_flow_table(struct fs_node *node)
333 struct mlx5_flow_table *ft;
334 struct mlx5_core_dev *dev;
335 struct fs_prio *prio;
338 fs_get_obj(ft, node);
339 dev = get_dev(&ft->node);
341 err = mlx5_cmd_destroy_flow_table(dev, ft);
343 pr_warn("flow steering can't destroy ft\n");
344 fs_get_obj(prio, ft->node.parent);
348 static void del_rule(struct fs_node *node)
350 struct mlx5_flow_rule *rule;
351 struct mlx5_flow_table *ft;
352 struct mlx5_flow_group *fg;
356 struct mlx5_core_dev *dev = get_dev(node);
357 int match_len = MLX5_ST_SZ_BYTES(fte_match_param);
360 match_value = mlx5_vzalloc(match_len);
362 pr_warn("failed to allocate inbox\n");
366 fs_get_obj(rule, node);
367 fs_get_obj(fte, rule->node.parent);
368 fs_get_obj(fg, fte->node.parent);
369 memcpy(match_value, fte->val, sizeof(fte->val));
370 fs_get_obj(ft, fg->node.parent);
371 list_del(&rule->node.list);
372 if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
373 mutex_lock(&rule->dest_attr.ft->lock);
374 list_del(&rule->next_ft);
375 mutex_unlock(&rule->dest_attr.ft->lock);
377 if ((fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
379 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
380 err = mlx5_cmd_update_fte(dev, ft,
385 pr_warn("%s can't del rule fg id=%d fte_index=%d\n",
386 __func__, fg->id, fte->index);
391 static void del_fte(struct fs_node *node)
393 struct mlx5_flow_table *ft;
394 struct mlx5_flow_group *fg;
395 struct mlx5_core_dev *dev;
399 fs_get_obj(fte, node);
400 fs_get_obj(fg, fte->node.parent);
401 fs_get_obj(ft, fg->node.parent);
403 dev = get_dev(&ft->node);
404 err = mlx5_cmd_delete_fte(dev, ft,
407 pr_warn("flow steering can't delete fte in index %d of flow group id %d\n",
414 static void del_flow_group(struct fs_node *node)
416 struct mlx5_flow_group *fg;
417 struct mlx5_flow_table *ft;
418 struct mlx5_core_dev *dev;
420 fs_get_obj(fg, node);
421 fs_get_obj(ft, fg->node.parent);
422 dev = get_dev(&ft->node);
424 if (mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
425 pr_warn("flow steering can't destroy fg %d of ft %d\n",
429 static struct fs_fte *alloc_fte(u8 action,
436 fte = kzalloc(sizeof(*fte), GFP_KERNEL);
438 return ERR_PTR(-ENOMEM);
440 memcpy(fte->val, match_value, sizeof(fte->val));
441 fte->node.type = FS_TYPE_FLOW_ENTRY;
442 fte->flow_tag = flow_tag;
444 fte->action = action;
449 static struct mlx5_flow_group *alloc_flow_group(u32 *create_fg_in)
451 struct mlx5_flow_group *fg;
452 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
453 create_fg_in, match_criteria);
454 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
456 match_criteria_enable);
457 fg = kzalloc(sizeof(*fg), GFP_KERNEL);
459 return ERR_PTR(-ENOMEM);
461 fg->mask.match_criteria_enable = match_criteria_enable;
462 memcpy(&fg->mask.match_criteria, match_criteria,
463 sizeof(fg->mask.match_criteria));
464 fg->node.type = FS_TYPE_FLOW_GROUP;
465 fg->start_index = MLX5_GET(create_flow_group_in, create_fg_in,
467 fg->max_ftes = MLX5_GET(create_flow_group_in, create_fg_in,
468 end_flow_index) - fg->start_index + 1;
472 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
473 enum fs_flow_table_type table_type)
475 struct mlx5_flow_table *ft;
477 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
482 ft->node.type = FS_TYPE_FLOW_TABLE;
483 ft->type = table_type;
485 ft->max_fte = max_fte;
486 INIT_LIST_HEAD(&ft->fwd_rules);
487 mutex_init(&ft->lock);
492 /* If reverse is false, then we search for the first flow table in the
493 * root sub-tree from start(closest from right), else we search for the
494 * last flow table in the root sub-tree till start(closest from left).
496 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
497 struct list_head *start,
500 #define list_advance_entry(pos, reverse) \
501 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
503 #define list_for_each_advance_continue(pos, head, reverse) \
504 for (pos = list_advance_entry(pos, reverse); \
505 &pos->list != (head); \
506 pos = list_advance_entry(pos, reverse))
508 struct fs_node *iter = list_entry(start, struct fs_node, list);
509 struct mlx5_flow_table *ft = NULL;
514 list_for_each_advance_continue(iter, &root->children, reverse) {
515 if (iter->type == FS_TYPE_FLOW_TABLE) {
516 fs_get_obj(ft, iter);
519 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
527 /* If reverse if false then return the first flow table in next priority of
528 * prio in the tree, else return the last flow table in the previous priority
529 * of prio in the tree.
531 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
533 struct mlx5_flow_table *ft = NULL;
534 struct fs_node *curr_node;
535 struct fs_node *parent;
537 parent = prio->node.parent;
538 curr_node = &prio->node;
539 while (!ft && parent) {
540 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
542 parent = curr_node->parent;
547 /* Assuming all the tree is locked by mutex chain lock */
548 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
550 return find_closest_ft(prio, false);
553 /* Assuming all the tree is locked by mutex chain lock */
554 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
556 return find_closest_ft(prio, true);
559 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
560 struct fs_prio *prio,
561 struct mlx5_flow_table *ft)
563 struct mlx5_flow_table *iter;
567 fs_for_each_ft(iter, prio) {
569 err = mlx5_cmd_modify_flow_table(dev,
573 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
575 /* The driver is out of sync with the FW */
584 /* Connect flow tables from previous priority of prio to ft */
585 static int connect_prev_fts(struct mlx5_core_dev *dev,
586 struct mlx5_flow_table *ft,
587 struct fs_prio *prio)
589 struct mlx5_flow_table *prev_ft;
591 prev_ft = find_prev_chained_ft(prio);
593 struct fs_prio *prev_prio;
595 fs_get_obj(prev_prio, prev_ft->node.parent);
596 return connect_fts_in_prio(dev, prev_prio, ft);
601 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
604 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
605 int min_level = INT_MAX;
609 min_level = root->root_ft->level;
611 if (ft->level >= min_level)
614 err = mlx5_cmd_update_root_ft(root->dev, ft);
616 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
624 int mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
625 struct mlx5_flow_destination *dest)
627 struct mlx5_flow_table *ft;
628 struct mlx5_flow_group *fg;
630 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
633 fs_get_obj(fte, rule->node.parent);
634 if (!(fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
636 lock_ref_node(&fte->node);
637 fs_get_obj(fg, fte->node.parent);
638 fs_get_obj(ft, fg->node.parent);
640 memcpy(&rule->dest_attr, dest, sizeof(*dest));
641 err = mlx5_cmd_update_fte(get_dev(&ft->node),
645 unlock_ref_node(&fte->node);
650 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
651 static int connect_fwd_rules(struct mlx5_core_dev *dev,
652 struct mlx5_flow_table *new_next_ft,
653 struct mlx5_flow_table *old_next_ft)
655 struct mlx5_flow_destination dest;
656 struct mlx5_flow_rule *iter;
659 /* new_next_ft and old_next_ft could be NULL only
660 * when we create/destroy the anchor flow table.
662 if (!new_next_ft || !old_next_ft)
665 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
666 dest.ft = new_next_ft;
668 mutex_lock(&old_next_ft->lock);
669 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
670 mutex_unlock(&old_next_ft->lock);
671 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
672 err = mlx5_modify_rule_destination(iter, &dest);
674 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
680 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
681 struct fs_prio *prio)
683 struct mlx5_flow_table *next_ft;
686 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
688 if (list_empty(&prio->node.children)) {
689 err = connect_prev_fts(dev, ft, prio);
693 next_ft = find_next_chained_ft(prio);
694 err = connect_fwd_rules(dev, ft, next_ft);
699 if (MLX5_CAP_FLOWTABLE(dev,
700 flow_table_properties_nic_receive.modify_root))
701 err = update_root_ft_create(ft, prio);
705 static void list_add_flow_table(struct mlx5_flow_table *ft,
706 struct fs_prio *prio)
708 struct list_head *prev = &prio->node.children;
709 struct mlx5_flow_table *iter;
711 fs_for_each_ft(iter, prio) {
712 if (iter->level > ft->level)
714 prev = &iter->node.list;
716 list_add(&ft->node.list, prev);
719 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
721 int max_fte, u32 level)
723 struct mlx5_flow_table *next_ft = NULL;
724 struct mlx5_flow_table *ft;
727 struct mlx5_flow_root_namespace *root =
728 find_root(&ns->node);
729 struct fs_prio *fs_prio = NULL;
732 pr_err("mlx5: flow steering failed to find root of namespace\n");
733 return ERR_PTR(-ENODEV);
736 mutex_lock(&root->chain_lock);
737 fs_prio = find_prio(ns, prio);
742 if (level >= fs_prio->num_levels) {
746 /* The level is related to the
747 * priority level range.
749 level += fs_prio->start_level;
750 ft = alloc_flow_table(level,
752 roundup_pow_of_two(max_fte),
759 tree_init_node(&ft->node, 1, del_flow_table);
760 log_table_sz = ilog2(ft->max_fte);
761 next_ft = find_next_chained_ft(fs_prio);
762 err = mlx5_cmd_create_flow_table(root->dev, ft->vport, ft->type, ft->level,
763 log_table_sz, next_ft, &ft->id);
767 err = connect_flow_table(root->dev, ft, fs_prio);
770 lock_ref_node(&fs_prio->node);
771 tree_add_node(&ft->node, &fs_prio->node);
772 list_add_flow_table(ft, fs_prio);
774 unlock_ref_node(&fs_prio->node);
775 mutex_unlock(&root->chain_lock);
778 mlx5_cmd_destroy_flow_table(root->dev, ft);
782 mutex_unlock(&root->chain_lock);
786 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
787 int prio, int max_fte,
790 return __mlx5_create_flow_table(ns, 0, prio, max_fte, level);
793 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
794 int prio, int max_fte,
795 u32 level, u16 vport)
797 return __mlx5_create_flow_table(ns, vport, prio, max_fte, level);
800 struct mlx5_flow_table *mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
802 int num_flow_table_entries,
806 struct mlx5_flow_table *ft;
808 if (max_num_groups > num_flow_table_entries)
809 return ERR_PTR(-EINVAL);
811 ft = mlx5_create_flow_table(ns, prio, num_flow_table_entries, level);
815 ft->autogroup.active = true;
816 ft->autogroup.required_groups = max_num_groups;
820 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
822 /* Flow table should be locked */
823 static struct mlx5_flow_group *create_flow_group_common(struct mlx5_flow_table *ft,
829 struct mlx5_flow_group *fg;
830 struct mlx5_core_dev *dev = get_dev(&ft->node);
834 return ERR_PTR(-ENODEV);
836 fg = alloc_flow_group(fg_in);
840 err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
846 if (ft->autogroup.active)
847 ft->autogroup.num_groups++;
848 /* Add node to tree */
849 tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
850 tree_add_node(&fg->node, &ft->node);
851 /* Add node to group list */
852 list_add(&fg->node.list, ft->node.children.prev);
857 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
860 struct mlx5_flow_group *fg;
862 if (ft->autogroup.active)
863 return ERR_PTR(-EPERM);
865 lock_ref_node(&ft->node);
866 fg = create_flow_group_common(ft, fg_in, &ft->node.children, false);
867 unlock_ref_node(&ft->node);
872 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
874 struct mlx5_flow_rule *rule;
876 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
880 INIT_LIST_HEAD(&rule->next_ft);
881 rule->node.type = FS_TYPE_FLOW_DEST;
883 memcpy(&rule->dest_attr, dest, sizeof(*dest));
888 /* fte should not be deleted while calling this function */
889 static struct mlx5_flow_rule *add_rule_fte(struct fs_fte *fte,
890 struct mlx5_flow_group *fg,
891 struct mlx5_flow_destination *dest)
893 struct mlx5_flow_table *ft;
894 struct mlx5_flow_rule *rule;
898 rule = alloc_rule(dest);
900 return ERR_PTR(-ENOMEM);
902 fs_get_obj(ft, fg->node.parent);
903 /* Add dest to dests list- we need flow tables to be in the
904 * end of the list for forward to next prio rules.
906 tree_init_node(&rule->node, 1, del_rule);
907 if (dest && dest->type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
908 list_add(&rule->node.list, &fte->node.children);
910 list_add_tail(&rule->node.list, &fte->node.children);
914 modify_mask |= dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER ?
915 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS) :
916 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
919 if (fte->dests_size == 1 || !dest)
920 err = mlx5_cmd_create_fte(get_dev(&ft->node),
923 err = mlx5_cmd_update_fte(get_dev(&ft->node),
924 ft, fg->id, modify_mask, fte);
928 fte->status |= FS_FTE_STATUS_EXISTING;
933 list_del(&rule->node.list);
940 /* Assumed fg is locked */
941 static unsigned int get_free_fte_index(struct mlx5_flow_group *fg,
942 struct list_head **prev)
945 unsigned int start = fg->start_index;
948 *prev = &fg->node.children;
950 /* assumed list is sorted by index */
951 fs_for_each_fte(fte, fg) {
952 if (fte->index != start)
956 *prev = &fte->node.list;
962 /* prev is output, prev->next = new_fte */
963 static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
967 struct list_head **prev)
972 index = get_free_fte_index(fg, prev);
973 fte = alloc_fte(action, flow_tag, match_value, index);
980 static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
981 u8 match_criteria_enable,
984 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
985 struct list_head *prev = &ft->node.children;
986 unsigned int candidate_index = 0;
987 struct mlx5_flow_group *fg;
988 void *match_criteria_addr;
989 unsigned int group_size = 0;
992 if (!ft->autogroup.active)
993 return ERR_PTR(-ENOENT);
995 in = mlx5_vzalloc(inlen);
997 return ERR_PTR(-ENOMEM);
999 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1000 /* We save place for flow groups in addition to max types */
1001 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
1003 /* ft->max_fte == ft->autogroup.max_types */
1004 if (group_size == 0)
1007 /* sorted by start_index */
1008 fs_for_each_fg(fg, ft) {
1009 if (candidate_index + group_size > fg->start_index)
1010 candidate_index = fg->start_index + fg->max_ftes;
1013 prev = &fg->node.list;
1016 if (candidate_index + group_size > ft->max_fte) {
1017 fg = ERR_PTR(-ENOSPC);
1021 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1022 match_criteria_enable);
1023 MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
1024 MLX5_SET(create_flow_group_in, in, end_flow_index, candidate_index +
1026 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1027 in, match_criteria);
1028 memcpy(match_criteria_addr, match_criteria,
1029 MLX5_ST_SZ_BYTES(fte_match_param));
1031 fg = create_flow_group_common(ft, in, prev, true);
1037 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1038 struct mlx5_flow_destination *dest)
1040 struct mlx5_flow_rule *rule;
1042 list_for_each_entry(rule, &fte->node.children, node.list) {
1043 if (rule->dest_attr.type == dest->type) {
1044 if ((dest->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1045 dest->vport_num == rule->dest_attr.vport_num) ||
1046 (dest->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1047 dest->ft == rule->dest_attr.ft) ||
1048 (dest->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1049 dest->tir_num == rule->dest_attr.tir_num))
1056 static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
1060 struct mlx5_flow_destination *dest)
1063 struct mlx5_flow_rule *rule;
1064 struct mlx5_flow_table *ft;
1065 struct list_head *prev;
1067 nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
1068 fs_for_each_fte(fte, fg) {
1069 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
1070 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
1071 action == fte->action && flow_tag == fte->flow_tag) {
1072 rule = find_flow_rule(fte, dest);
1074 atomic_inc(&rule->node.refcount);
1075 unlock_ref_node(&fte->node);
1076 unlock_ref_node(&fg->node);
1079 rule = add_rule_fte(fte, fg, dest);
1080 unlock_ref_node(&fte->node);
1086 unlock_ref_node(&fte->node);
1088 fs_get_obj(ft, fg->node.parent);
1089 if (fg->num_ftes >= fg->max_ftes) {
1090 rule = ERR_PTR(-ENOSPC);
1094 fte = create_fte(fg, match_value, action, flow_tag, &prev);
1099 tree_init_node(&fte->node, 0, del_fte);
1100 rule = add_rule_fte(fte, fg, dest);
1108 tree_add_node(&fte->node, &fg->node);
1109 list_add(&fte->node.list, prev);
1111 tree_add_node(&rule->node, &fte->node);
1113 unlock_ref_node(&fg->node);
1117 struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_rule *rule)
1119 struct mlx5_flow_rule *dst;
1122 fs_get_obj(fte, rule->node.parent);
1124 fs_for_each_dst(dst, fte) {
1125 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
1126 return dst->dest_attr.counter;
1132 static bool counter_is_valid(struct mlx5_fc *counter, u32 action)
1134 if (!(action & MLX5_FLOW_CONTEXT_ACTION_COUNT))
1140 /* Hardware support counter for a drop action only */
1141 return action == (MLX5_FLOW_CONTEXT_ACTION_DROP | MLX5_FLOW_CONTEXT_ACTION_COUNT);
1144 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1146 struct mlx5_flow_table *ft)
1148 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1149 return counter_is_valid(dest->counter, action);
1151 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1154 if (!dest || ((dest->type ==
1155 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1156 (dest->ft->level <= ft->level)))
1161 static struct mlx5_flow_rule *
1162 _mlx5_add_flow_rule(struct mlx5_flow_table *ft,
1163 struct mlx5_flow_spec *spec,
1166 struct mlx5_flow_destination *dest)
1168 struct mlx5_flow_group *g;
1169 struct mlx5_flow_rule *rule;
1171 if (!dest_is_valid(dest, action, ft))
1172 return ERR_PTR(-EINVAL);
1174 nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
1175 fs_for_each_fg(g, ft)
1176 if (compare_match_criteria(g->mask.match_criteria_enable,
1177 spec->match_criteria_enable,
1178 g->mask.match_criteria,
1179 spec->match_criteria)) {
1180 rule = add_rule_fg(g, spec->match_value,
1181 action, flow_tag, dest);
1182 if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
1186 g = create_autogroup(ft, spec->match_criteria_enable,
1187 spec->match_criteria);
1193 rule = add_rule_fg(g, spec->match_value,
1194 action, flow_tag, dest);
1196 /* Remove assumes refcount > 0 and autogroup creates a group
1197 * with a refcount = 0.
1199 unlock_ref_node(&ft->node);
1200 tree_get_node(&g->node);
1201 tree_remove_node(&g->node);
1205 unlock_ref_node(&ft->node);
1209 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1211 return ((ft->type == FS_FT_NIC_RX) &&
1212 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1215 struct mlx5_flow_rule *
1216 mlx5_add_flow_rule(struct mlx5_flow_table *ft,
1217 struct mlx5_flow_spec *spec,
1220 struct mlx5_flow_destination *dest)
1222 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1223 struct mlx5_flow_destination gen_dest;
1224 struct mlx5_flow_table *next_ft = NULL;
1225 struct mlx5_flow_rule *rule = NULL;
1226 u32 sw_action = action;
1227 struct fs_prio *prio;
1229 fs_get_obj(prio, ft->node.parent);
1230 if (action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1231 if (!fwd_next_prio_supported(ft))
1232 return ERR_PTR(-EOPNOTSUPP);
1234 return ERR_PTR(-EINVAL);
1235 mutex_lock(&root->chain_lock);
1236 next_ft = find_next_chained_ft(prio);
1238 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1239 gen_dest.ft = next_ft;
1241 action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1243 mutex_unlock(&root->chain_lock);
1244 return ERR_PTR(-EOPNOTSUPP);
1248 rule = _mlx5_add_flow_rule(ft, spec, action, flow_tag, dest);
1250 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1251 if (!IS_ERR_OR_NULL(rule) &&
1252 (list_empty(&rule->next_ft))) {
1253 mutex_lock(&next_ft->lock);
1254 list_add(&rule->next_ft, &next_ft->fwd_rules);
1255 mutex_unlock(&next_ft->lock);
1256 rule->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1258 mutex_unlock(&root->chain_lock);
1262 EXPORT_SYMBOL(mlx5_add_flow_rule);
1264 void mlx5_del_flow_rule(struct mlx5_flow_rule *rule)
1266 tree_remove_node(&rule->node);
1268 EXPORT_SYMBOL(mlx5_del_flow_rule);
1270 /* Assuming prio->node.children(flow tables) is sorted by level */
1271 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1273 struct fs_prio *prio;
1275 fs_get_obj(prio, ft->node.parent);
1277 if (!list_is_last(&ft->node.list, &prio->node.children))
1278 return list_next_entry(ft, node.list);
1279 return find_next_chained_ft(prio);
1282 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1284 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1285 struct mlx5_flow_table *new_root_ft = NULL;
1287 if (root->root_ft != ft)
1290 new_root_ft = find_next_ft(ft);
1292 int err = mlx5_cmd_update_root_ft(root->dev, new_root_ft);
1295 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
1300 root->root_ft = new_root_ft;
1304 /* Connect flow table from previous priority to
1305 * the next flow table.
1307 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1309 struct mlx5_core_dev *dev = get_dev(&ft->node);
1310 struct mlx5_flow_table *next_ft;
1311 struct fs_prio *prio;
1314 err = update_root_ft_destroy(ft);
1318 fs_get_obj(prio, ft->node.parent);
1319 if (!(list_first_entry(&prio->node.children,
1320 struct mlx5_flow_table,
1324 next_ft = find_next_chained_ft(prio);
1325 err = connect_fwd_rules(dev, next_ft, ft);
1329 err = connect_prev_fts(dev, next_ft, prio);
1331 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1336 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1338 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1341 mutex_lock(&root->chain_lock);
1342 err = disconnect_flow_table(ft);
1344 mutex_unlock(&root->chain_lock);
1347 if (tree_remove_node(&ft->node))
1348 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1350 mutex_unlock(&root->chain_lock);
1354 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1356 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1358 if (tree_remove_node(&fg->node))
1359 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1363 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1364 enum mlx5_flow_namespace_type type)
1366 struct mlx5_flow_steering *steering = dev->priv.steering;
1367 struct mlx5_flow_root_namespace *root_ns;
1369 struct fs_prio *fs_prio;
1370 struct mlx5_flow_namespace *ns;
1376 case MLX5_FLOW_NAMESPACE_BYPASS:
1377 case MLX5_FLOW_NAMESPACE_OFFLOADS:
1378 case MLX5_FLOW_NAMESPACE_KERNEL:
1379 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
1380 case MLX5_FLOW_NAMESPACE_ANCHOR:
1383 case MLX5_FLOW_NAMESPACE_FDB:
1384 if (steering->fdb_root_ns)
1385 return &steering->fdb_root_ns->ns;
1388 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
1389 if (steering->esw_egress_root_ns)
1390 return &steering->esw_egress_root_ns->ns;
1393 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
1394 if (steering->esw_ingress_root_ns)
1395 return &steering->esw_ingress_root_ns->ns;
1402 root_ns = steering->root_ns;
1406 fs_prio = find_prio(&root_ns->ns, prio);
1410 ns = list_first_entry(&fs_prio->node.children,
1416 EXPORT_SYMBOL(mlx5_get_flow_namespace);
1418 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
1419 unsigned int prio, int num_levels)
1421 struct fs_prio *fs_prio;
1423 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
1425 return ERR_PTR(-ENOMEM);
1427 fs_prio->node.type = FS_TYPE_PRIO;
1428 tree_init_node(&fs_prio->node, 1, NULL);
1429 tree_add_node(&fs_prio->node, &ns->node);
1430 fs_prio->num_levels = num_levels;
1431 fs_prio->prio = prio;
1432 list_add_tail(&fs_prio->node.list, &ns->node.children);
1437 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
1440 ns->node.type = FS_TYPE_NAMESPACE;
1445 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
1447 struct mlx5_flow_namespace *ns;
1449 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1451 return ERR_PTR(-ENOMEM);
1453 fs_init_namespace(ns);
1454 tree_init_node(&ns->node, 1, NULL);
1455 tree_add_node(&ns->node, &prio->node);
1456 list_add_tail(&ns->node.list, &prio->node.children);
1461 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
1462 struct init_tree_node *prio_metadata)
1464 struct fs_prio *fs_prio;
1467 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
1468 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
1469 if (IS_ERR(fs_prio))
1470 return PTR_ERR(fs_prio);
1475 #define FLOW_TABLE_BIT_SZ 1
1476 #define GET_FLOW_TABLE_CAP(dev, offset) \
1477 ((be32_to_cpu(*((__be32 *)(dev->hca_caps_cur[MLX5_CAP_FLOW_TABLE]) + \
1479 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
1480 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
1484 for (i = 0; i < caps->arr_sz; i++) {
1485 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
1491 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
1492 struct init_tree_node *init_node,
1493 struct fs_node *fs_parent_node,
1494 struct init_tree_node *init_parent_node,
1497 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
1498 flow_table_properties_nic_receive.
1500 struct mlx5_flow_namespace *fs_ns;
1501 struct fs_prio *fs_prio;
1502 struct fs_node *base;
1506 if (init_node->type == FS_TYPE_PRIO) {
1507 if ((init_node->min_ft_level > max_ft_level) ||
1508 !has_required_caps(steering->dev, &init_node->caps))
1511 fs_get_obj(fs_ns, fs_parent_node);
1512 if (init_node->num_leaf_prios)
1513 return create_leaf_prios(fs_ns, prio, init_node);
1514 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
1515 if (IS_ERR(fs_prio))
1516 return PTR_ERR(fs_prio);
1517 base = &fs_prio->node;
1518 } else if (init_node->type == FS_TYPE_NAMESPACE) {
1519 fs_get_obj(fs_prio, fs_parent_node);
1520 fs_ns = fs_create_namespace(fs_prio);
1522 return PTR_ERR(fs_ns);
1523 base = &fs_ns->node;
1528 for (i = 0; i < init_node->ar_size; i++) {
1529 err = init_root_tree_recursive(steering, &init_node->children[i],
1530 base, init_node, prio);
1533 if (init_node->children[i].type == FS_TYPE_PRIO &&
1534 init_node->children[i].num_leaf_prios) {
1535 prio += init_node->children[i].num_leaf_prios;
1542 static int init_root_tree(struct mlx5_flow_steering *steering,
1543 struct init_tree_node *init_node,
1544 struct fs_node *fs_parent_node)
1547 struct mlx5_flow_namespace *fs_ns;
1550 fs_get_obj(fs_ns, fs_parent_node);
1551 for (i = 0; i < init_node->ar_size; i++) {
1552 err = init_root_tree_recursive(steering, &init_node->children[i],
1561 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_flow_steering *steering,
1562 enum fs_flow_table_type
1565 struct mlx5_flow_root_namespace *root_ns;
1566 struct mlx5_flow_namespace *ns;
1568 /* Create the root namespace */
1569 root_ns = mlx5_vzalloc(sizeof(*root_ns));
1573 root_ns->dev = steering->dev;
1574 root_ns->table_type = table_type;
1577 fs_init_namespace(ns);
1578 mutex_init(&root_ns->chain_lock);
1579 tree_init_node(&ns->node, 1, NULL);
1580 tree_add_node(&ns->node, NULL);
1585 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
1587 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
1589 struct fs_prio *prio;
1591 fs_for_each_prio(prio, ns) {
1592 /* This updates prio start_level and num_levels */
1593 set_prio_attrs_in_prio(prio, acc_level);
1594 acc_level += prio->num_levels;
1599 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
1601 struct mlx5_flow_namespace *ns;
1602 int acc_level_ns = acc_level;
1604 prio->start_level = acc_level;
1605 fs_for_each_ns(ns, prio)
1606 /* This updates start_level and num_levels of ns's priority descendants */
1607 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
1608 if (!prio->num_levels)
1609 prio->num_levels = acc_level_ns - prio->start_level;
1610 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
1613 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
1615 struct mlx5_flow_namespace *ns = &root_ns->ns;
1616 struct fs_prio *prio;
1617 int start_level = 0;
1619 fs_for_each_prio(prio, ns) {
1620 set_prio_attrs_in_prio(prio, start_level);
1621 start_level += prio->num_levels;
1625 #define ANCHOR_PRIO 0
1626 #define ANCHOR_SIZE 1
1627 #define ANCHOR_LEVEL 0
1628 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
1630 struct mlx5_flow_namespace *ns = NULL;
1631 struct mlx5_flow_table *ft;
1633 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
1636 ft = mlx5_create_flow_table(ns, ANCHOR_PRIO, ANCHOR_SIZE, ANCHOR_LEVEL);
1638 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
1644 static int init_root_ns(struct mlx5_flow_steering *steering)
1647 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
1648 if (IS_ERR_OR_NULL(steering->root_ns))
1651 if (init_root_tree(steering, &root_fs, &steering->root_ns->ns.node))
1654 set_prio_attrs(steering->root_ns);
1656 if (create_anchor_flow_table(steering))
1662 mlx5_cleanup_fs(steering->dev);
1666 static void cleanup_single_prio_root_ns(struct mlx5_flow_steering *steering,
1667 struct mlx5_flow_root_namespace *root_ns)
1669 struct fs_node *prio;
1674 if (!list_empty(&root_ns->ns.node.children)) {
1675 prio = list_first_entry(&root_ns->ns.node.children,
1678 if (tree_remove_node(prio))
1679 mlx5_core_warn(steering->dev,
1680 "Flow steering priority wasn't destroyed, refcount > 1\n");
1682 if (tree_remove_node(&root_ns->ns.node))
1683 mlx5_core_warn(steering->dev,
1684 "Flow steering namespace wasn't destroyed, refcount > 1\n");
1688 static void destroy_flow_tables(struct fs_prio *prio)
1690 struct mlx5_flow_table *iter;
1691 struct mlx5_flow_table *tmp;
1693 fs_for_each_ft_safe(iter, tmp, prio)
1694 mlx5_destroy_flow_table(iter);
1697 static void cleanup_root_ns(struct mlx5_flow_steering *steering)
1699 struct mlx5_flow_root_namespace *root_ns = steering->root_ns;
1700 struct fs_prio *iter_prio;
1702 if (!MLX5_CAP_GEN(steering->dev, nic_flow_table))
1709 fs_for_each_prio(iter_prio, &root_ns->ns) {
1710 struct fs_node *node;
1711 struct mlx5_flow_namespace *iter_ns;
1713 fs_for_each_ns_or_ft(node, iter_prio) {
1714 if (node->type == FS_TYPE_FLOW_TABLE)
1716 fs_get_obj(iter_ns, node);
1717 while (!list_empty(&iter_ns->node.children)) {
1718 struct fs_prio *obj_iter_prio2;
1719 struct fs_node *iter_prio2 =
1720 list_first_entry(&iter_ns->node.children,
1724 fs_get_obj(obj_iter_prio2, iter_prio2);
1725 destroy_flow_tables(obj_iter_prio2);
1726 if (tree_remove_node(iter_prio2)) {
1727 mlx5_core_warn(steering->dev,
1728 "Priority %d wasn't destroyed, refcount > 1\n",
1729 obj_iter_prio2->prio);
1737 fs_for_each_prio(iter_prio, &root_ns->ns) {
1738 while (!list_empty(&iter_prio->node.children)) {
1739 struct fs_node *iter_ns =
1740 list_first_entry(&iter_prio->node.children,
1743 if (tree_remove_node(iter_ns)) {
1744 mlx5_core_warn(steering->dev,
1745 "Namespace wasn't destroyed, refcount > 1\n");
1752 while (!list_empty(&root_ns->ns.node.children)) {
1753 struct fs_prio *obj_prio_node;
1754 struct fs_node *prio_node =
1755 list_first_entry(&root_ns->ns.node.children,
1759 fs_get_obj(obj_prio_node, prio_node);
1760 if (tree_remove_node(prio_node)) {
1761 mlx5_core_warn(steering->dev,
1762 "Priority %d wasn't destroyed, refcount > 1\n",
1763 obj_prio_node->prio);
1768 if (tree_remove_node(&root_ns->ns.node)) {
1769 mlx5_core_warn(steering->dev,
1770 "root namespace wasn't destroyed, refcount > 1\n");
1774 steering->root_ns = NULL;
1777 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1779 struct mlx5_flow_steering *steering = dev->priv.steering;
1781 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1784 cleanup_root_ns(steering);
1785 cleanup_single_prio_root_ns(steering, steering->esw_egress_root_ns);
1786 cleanup_single_prio_root_ns(steering, steering->esw_ingress_root_ns);
1787 cleanup_single_prio_root_ns(steering, steering->fdb_root_ns);
1788 mlx5_cleanup_fc_stats(dev);
1792 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
1794 struct fs_prio *prio;
1796 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
1797 if (!steering->fdb_root_ns)
1800 /* Create single prio */
1801 prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 1);
1803 cleanup_single_prio_root_ns(steering, steering->fdb_root_ns);
1804 return PTR_ERR(prio);
1810 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering)
1812 struct fs_prio *prio;
1814 steering->esw_egress_root_ns = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
1815 if (!steering->esw_egress_root_ns)
1819 prio = fs_create_prio(&steering->esw_egress_root_ns->ns, 0,
1820 MLX5_TOTAL_VPORTS(steering->dev));
1822 return PTR_ERR(prio);
1827 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering)
1829 struct fs_prio *prio;
1831 steering->esw_ingress_root_ns = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
1832 if (!steering->esw_ingress_root_ns)
1836 prio = fs_create_prio(&steering->esw_ingress_root_ns->ns, 0,
1837 MLX5_TOTAL_VPORTS(steering->dev));
1839 return PTR_ERR(prio);
1844 int mlx5_init_fs(struct mlx5_core_dev *dev)
1846 struct mlx5_flow_steering *steering;
1849 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1852 err = mlx5_init_fc_stats(dev);
1856 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
1859 steering->dev = dev;
1860 dev->priv.steering = steering;
1862 if (MLX5_CAP_GEN(dev, nic_flow_table) &&
1863 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
1864 err = init_root_ns(steering);
1869 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
1870 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
1871 err = init_fdb_root_ns(steering);
1875 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
1876 err = init_egress_acl_root_ns(steering);
1880 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
1881 err = init_ingress_acl_root_ns(steering);
1889 mlx5_cleanup_fs(dev);