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"
39 #include "diag/fs_tracepoint.h"
40 #include "accel/ipsec.h"
41 #include "fpga/ipsec.h"
43 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
44 sizeof(struct init_tree_node))
46 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
47 ...) {.type = FS_TYPE_PRIO,\
48 .min_ft_level = min_level_val,\
49 .num_levels = num_levels_val,\
50 .num_leaf_prios = num_prios_val,\
52 .children = (struct init_tree_node[]) {__VA_ARGS__},\
53 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
56 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
57 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
60 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
61 .children = (struct init_tree_node[]) {__VA_ARGS__},\
62 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
65 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
68 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
70 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
71 .caps = (long[]) {__VA_ARGS__} }
73 #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
74 FS_CAP(flow_table_properties_nic_receive.modify_root), \
75 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
76 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
78 #define LEFTOVERS_NUM_LEVELS 1
79 #define LEFTOVERS_NUM_PRIOS 1
81 #define BY_PASS_PRIO_NUM_LEVELS 1
82 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
85 #define ETHTOOL_PRIO_NUM_LEVELS 1
86 #define ETHTOOL_NUM_PRIOS 11
87 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
88 /* Vlan, mac, ttc, inner ttc, aRFS */
89 #define KERNEL_NIC_PRIO_NUM_LEVELS 5
90 #define KERNEL_NIC_NUM_PRIOS 1
91 /* One more level for tc */
92 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
94 #define KERNEL_NIC_TC_NUM_PRIOS 1
95 #define KERNEL_NIC_TC_NUM_LEVELS 2
97 #define ANCHOR_NUM_LEVELS 1
98 #define ANCHOR_NUM_PRIOS 1
99 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
101 #define OFFLOADS_MAX_FT 1
102 #define OFFLOADS_NUM_PRIOS 1
103 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
105 #define LAG_PRIO_NUM_LEVELS 1
106 #define LAG_NUM_PRIOS 1
107 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
114 static struct init_tree_node {
115 enum fs_node_type type;
116 struct init_tree_node *children;
118 struct node_caps caps;
124 .type = FS_TYPE_NAMESPACE,
126 .children = (struct init_tree_node[]) {
127 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
129 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
130 BY_PASS_PRIO_NUM_LEVELS))),
131 ADD_PRIO(0, LAG_MIN_LEVEL, 0,
133 ADD_NS(ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
134 LAG_PRIO_NUM_LEVELS))),
135 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {},
136 ADD_NS(ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS, OFFLOADS_MAX_FT))),
137 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0,
139 ADD_NS(ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
140 ETHTOOL_PRIO_NUM_LEVELS))),
141 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
142 ADD_NS(ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS, KERNEL_NIC_TC_NUM_LEVELS),
143 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
144 KERNEL_NIC_PRIO_NUM_LEVELS))),
145 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
147 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_NUM_LEVELS))),
148 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
149 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_NUM_LEVELS))),
153 enum fs_i_lock_class {
159 static const struct rhashtable_params rhash_fte = {
160 .key_len = FIELD_SIZEOF(struct fs_fte, val),
161 .key_offset = offsetof(struct fs_fte, val),
162 .head_offset = offsetof(struct fs_fte, hash),
163 .automatic_shrinking = true,
167 static const struct rhashtable_params rhash_fg = {
168 .key_len = FIELD_SIZEOF(struct mlx5_flow_group, mask),
169 .key_offset = offsetof(struct mlx5_flow_group, mask),
170 .head_offset = offsetof(struct mlx5_flow_group, hash),
171 .automatic_shrinking = true,
176 static void del_hw_flow_table(struct fs_node *node);
177 static void del_hw_flow_group(struct fs_node *node);
178 static void del_hw_fte(struct fs_node *node);
179 static void del_sw_flow_table(struct fs_node *node);
180 static void del_sw_flow_group(struct fs_node *node);
181 static void del_sw_fte(struct fs_node *node);
182 static void del_sw_prio(struct fs_node *node);
183 static void del_sw_ns(struct fs_node *node);
184 /* Delete rule (destination) is special case that
185 * requires to lock the FTE for all the deletion process.
187 static void del_sw_hw_rule(struct fs_node *node);
188 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
189 struct mlx5_flow_destination *d2);
190 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
191 static struct mlx5_flow_rule *
192 find_flow_rule(struct fs_fte *fte,
193 struct mlx5_flow_destination *dest);
195 static void tree_init_node(struct fs_node *node,
196 void (*del_hw_func)(struct fs_node *),
197 void (*del_sw_func)(struct fs_node *))
199 refcount_set(&node->refcount, 1);
200 INIT_LIST_HEAD(&node->list);
201 INIT_LIST_HEAD(&node->children);
202 init_rwsem(&node->lock);
203 node->del_hw_func = del_hw_func;
204 node->del_sw_func = del_sw_func;
205 node->active = false;
208 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
211 refcount_inc(&parent->refcount);
212 node->parent = parent;
214 /* Parent is the root */
218 node->root = parent->root;
221 static int tree_get_node(struct fs_node *node)
223 return refcount_inc_not_zero(&node->refcount);
226 static void nested_down_read_ref_node(struct fs_node *node,
227 enum fs_i_lock_class class)
230 down_read_nested(&node->lock, class);
231 refcount_inc(&node->refcount);
235 static void nested_down_write_ref_node(struct fs_node *node,
236 enum fs_i_lock_class class)
239 down_write_nested(&node->lock, class);
240 refcount_inc(&node->refcount);
244 static void down_write_ref_node(struct fs_node *node)
247 down_write(&node->lock);
248 refcount_inc(&node->refcount);
252 static void up_read_ref_node(struct fs_node *node)
254 refcount_dec(&node->refcount);
255 up_read(&node->lock);
258 static void up_write_ref_node(struct fs_node *node)
260 refcount_dec(&node->refcount);
261 up_write(&node->lock);
264 static void tree_put_node(struct fs_node *node)
266 struct fs_node *parent_node = node->parent;
268 if (refcount_dec_and_test(&node->refcount)) {
269 if (node->del_hw_func)
270 node->del_hw_func(node);
272 /* Only root namespace doesn't have parent and we just
273 * need to free its node.
275 down_write_ref_node(parent_node);
276 list_del_init(&node->list);
277 if (node->del_sw_func)
278 node->del_sw_func(node);
279 up_write_ref_node(parent_node);
285 if (!node && parent_node)
286 tree_put_node(parent_node);
289 static int tree_remove_node(struct fs_node *node)
291 if (refcount_read(&node->refcount) > 1) {
292 refcount_dec(&node->refcount);
299 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
302 struct fs_prio *iter_prio;
304 fs_for_each_prio(iter_prio, ns) {
305 if (iter_prio->prio == prio)
312 static bool check_last_reserved(const u32 *match_criteria)
314 char *match_criteria_reserved =
315 MLX5_ADDR_OF(fte_match_param, match_criteria, MLX5_FTE_MATCH_PARAM_RESERVED);
317 return !match_criteria_reserved[0] &&
318 !memcmp(match_criteria_reserved, match_criteria_reserved + 1,
319 MLX5_FLD_SZ_BYTES(fte_match_param,
320 MLX5_FTE_MATCH_PARAM_RESERVED) - 1);
323 static bool check_valid_mask(u8 match_criteria_enable, const u32 *match_criteria)
325 if (match_criteria_enable & ~(
326 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) |
327 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) |
328 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS)))
331 if (!(match_criteria_enable &
332 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS)) {
333 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
334 match_criteria, outer_headers);
336 if (fg_type_mask[0] ||
337 memcmp(fg_type_mask, fg_type_mask + 1,
338 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4) - 1))
342 if (!(match_criteria_enable &
343 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS)) {
344 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
345 match_criteria, misc_parameters);
347 if (fg_type_mask[0] ||
348 memcmp(fg_type_mask, fg_type_mask + 1,
349 MLX5_ST_SZ_BYTES(fte_match_set_misc) - 1))
353 if (!(match_criteria_enable &
354 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS)) {
355 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
356 match_criteria, inner_headers);
358 if (fg_type_mask[0] ||
359 memcmp(fg_type_mask, fg_type_mask + 1,
360 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4) - 1))
364 return check_last_reserved(match_criteria);
367 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
371 if (!check_valid_mask(spec->match_criteria_enable, spec->match_criteria)) {
372 pr_warn("mlx5_core: Match criteria given mismatches match_criteria_enable\n");
376 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
377 if (spec->match_value[i] & ~spec->match_criteria[i]) {
378 pr_warn("mlx5_core: match_value differs from match_criteria\n");
382 return check_last_reserved(spec->match_value);
385 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
387 struct fs_node *root;
388 struct mlx5_flow_namespace *ns;
392 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
393 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
397 ns = container_of(root, struct mlx5_flow_namespace, node);
398 return container_of(ns, struct mlx5_flow_root_namespace, ns);
401 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
403 struct mlx5_flow_root_namespace *root = find_root(node);
406 return root->dev->priv.steering;
410 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
412 struct mlx5_flow_root_namespace *root = find_root(node);
419 static void del_sw_ns(struct fs_node *node)
424 static void del_sw_prio(struct fs_node *node)
429 static void del_hw_flow_table(struct fs_node *node)
431 struct mlx5_flow_root_namespace *root;
432 struct mlx5_flow_table *ft;
433 struct mlx5_core_dev *dev;
436 fs_get_obj(ft, node);
437 dev = get_dev(&ft->node);
438 root = find_root(&ft->node);
441 err = root->cmds->destroy_flow_table(dev, ft);
443 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
447 static void del_sw_flow_table(struct fs_node *node)
449 struct mlx5_flow_table *ft;
450 struct fs_prio *prio;
452 fs_get_obj(ft, node);
454 rhltable_destroy(&ft->fgs_hash);
455 fs_get_obj(prio, ft->node.parent);
460 static void del_sw_hw_rule(struct fs_node *node)
462 struct mlx5_flow_root_namespace *root;
463 struct mlx5_flow_rule *rule;
464 struct mlx5_flow_table *ft;
465 struct mlx5_flow_group *fg;
468 struct mlx5_core_dev *dev = get_dev(node);
470 bool update_fte = false;
472 fs_get_obj(rule, node);
473 fs_get_obj(fte, rule->node.parent);
474 fs_get_obj(fg, fte->node.parent);
475 fs_get_obj(ft, fg->node.parent);
476 trace_mlx5_fs_del_rule(rule);
477 if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
478 mutex_lock(&rule->dest_attr.ft->lock);
479 list_del(&rule->next_ft);
480 mutex_unlock(&rule->dest_attr.ft->lock);
483 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
485 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
486 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
487 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
492 if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
494 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
498 root = find_root(&ft->node);
499 if (update_fte && fte->dests_size) {
500 err = root->cmds->update_fte(dev, ft, fg->id, modify_mask, fte);
503 "%s can't del rule fg id=%d fte_index=%d\n",
504 __func__, fg->id, fte->index);
509 static void del_hw_fte(struct fs_node *node)
511 struct mlx5_flow_root_namespace *root;
512 struct mlx5_flow_table *ft;
513 struct mlx5_flow_group *fg;
514 struct mlx5_core_dev *dev;
518 fs_get_obj(fte, node);
519 fs_get_obj(fg, fte->node.parent);
520 fs_get_obj(ft, fg->node.parent);
522 trace_mlx5_fs_del_fte(fte);
523 dev = get_dev(&ft->node);
524 root = find_root(&ft->node);
526 err = root->cmds->delete_fte(dev, ft, fte);
529 "flow steering can't delete fte in index %d of flow group id %d\n",
534 static void del_sw_fte(struct fs_node *node)
536 struct mlx5_flow_steering *steering = get_steering(node);
537 struct mlx5_flow_group *fg;
541 fs_get_obj(fte, node);
542 fs_get_obj(fg, fte->node.parent);
544 err = rhashtable_remove_fast(&fg->ftes_hash,
548 ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
549 kmem_cache_free(steering->ftes_cache, fte);
552 static void del_hw_flow_group(struct fs_node *node)
554 struct mlx5_flow_root_namespace *root;
555 struct mlx5_flow_group *fg;
556 struct mlx5_flow_table *ft;
557 struct mlx5_core_dev *dev;
559 fs_get_obj(fg, node);
560 fs_get_obj(ft, fg->node.parent);
561 dev = get_dev(&ft->node);
562 trace_mlx5_fs_del_fg(fg);
564 root = find_root(&ft->node);
565 if (fg->node.active && root->cmds->destroy_flow_group(dev, ft, fg->id))
566 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
570 static void del_sw_flow_group(struct fs_node *node)
572 struct mlx5_flow_steering *steering = get_steering(node);
573 struct mlx5_flow_group *fg;
574 struct mlx5_flow_table *ft;
577 fs_get_obj(fg, node);
578 fs_get_obj(ft, fg->node.parent);
580 rhashtable_destroy(&fg->ftes_hash);
581 ida_destroy(&fg->fte_allocator);
582 if (ft->autogroup.active)
583 ft->autogroup.num_groups--;
584 err = rhltable_remove(&ft->fgs_hash,
588 kmem_cache_free(steering->fgs_cache, fg);
591 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
596 index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
600 fte->index = index + fg->start_index;
601 ret = rhashtable_insert_fast(&fg->ftes_hash,
607 tree_add_node(&fte->node, &fg->node);
608 list_add_tail(&fte->node.list, &fg->node.children);
612 ida_simple_remove(&fg->fte_allocator, index);
616 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
618 struct mlx5_flow_act *flow_act)
620 struct mlx5_flow_steering *steering = get_steering(&ft->node);
623 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
625 return ERR_PTR(-ENOMEM);
627 memcpy(fte->val, match_value, sizeof(fte->val));
628 fte->node.type = FS_TYPE_FLOW_ENTRY;
629 fte->action = *flow_act;
631 tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
636 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
637 struct mlx5_flow_group *fg)
639 rhashtable_destroy(&fg->ftes_hash);
640 kmem_cache_free(steering->fgs_cache, fg);
643 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
644 u8 match_criteria_enable,
645 void *match_criteria,
649 struct mlx5_flow_group *fg;
652 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
654 return ERR_PTR(-ENOMEM);
656 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
658 kmem_cache_free(steering->fgs_cache, fg);
661 ida_init(&fg->fte_allocator);
662 fg->mask.match_criteria_enable = match_criteria_enable;
663 memcpy(&fg->mask.match_criteria, match_criteria,
664 sizeof(fg->mask.match_criteria));
665 fg->node.type = FS_TYPE_FLOW_GROUP;
666 fg->start_index = start_index;
667 fg->max_ftes = end_index - start_index + 1;
672 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
673 u8 match_criteria_enable,
674 void *match_criteria,
677 struct list_head *prev)
679 struct mlx5_flow_steering *steering = get_steering(&ft->node);
680 struct mlx5_flow_group *fg;
683 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
684 start_index, end_index);
688 /* initialize refcnt, add to parent list */
689 ret = rhltable_insert(&ft->fgs_hash,
693 dealloc_flow_group(steering, fg);
697 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
698 tree_add_node(&fg->node, &ft->node);
699 /* Add node to group list */
700 list_add(&fg->node.list, prev);
701 atomic_inc(&ft->node.version);
706 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
707 enum fs_flow_table_type table_type,
708 enum fs_flow_table_op_mod op_mod,
711 struct mlx5_flow_table *ft;
714 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
716 return ERR_PTR(-ENOMEM);
718 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
725 ft->node.type = FS_TYPE_FLOW_TABLE;
727 ft->type = table_type;
729 ft->max_fte = max_fte;
731 INIT_LIST_HEAD(&ft->fwd_rules);
732 mutex_init(&ft->lock);
737 /* If reverse is false, then we search for the first flow table in the
738 * root sub-tree from start(closest from right), else we search for the
739 * last flow table in the root sub-tree till start(closest from left).
741 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
742 struct list_head *start,
745 #define list_advance_entry(pos, reverse) \
746 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
748 #define list_for_each_advance_continue(pos, head, reverse) \
749 for (pos = list_advance_entry(pos, reverse); \
750 &pos->list != (head); \
751 pos = list_advance_entry(pos, reverse))
753 struct fs_node *iter = list_entry(start, struct fs_node, list);
754 struct mlx5_flow_table *ft = NULL;
759 list_for_each_advance_continue(iter, &root->children, reverse) {
760 if (iter->type == FS_TYPE_FLOW_TABLE) {
761 fs_get_obj(ft, iter);
764 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
772 /* If reverse if false then return the first flow table in next priority of
773 * prio in the tree, else return the last flow table in the previous priority
774 * of prio in the tree.
776 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
778 struct mlx5_flow_table *ft = NULL;
779 struct fs_node *curr_node;
780 struct fs_node *parent;
782 parent = prio->node.parent;
783 curr_node = &prio->node;
784 while (!ft && parent) {
785 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
787 parent = curr_node->parent;
792 /* Assuming all the tree is locked by mutex chain lock */
793 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
795 return find_closest_ft(prio, false);
798 /* Assuming all the tree is locked by mutex chain lock */
799 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
801 return find_closest_ft(prio, true);
804 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
805 struct fs_prio *prio,
806 struct mlx5_flow_table *ft)
808 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
809 struct mlx5_flow_table *iter;
813 fs_for_each_ft(iter, prio) {
815 err = root->cmds->modify_flow_table(dev, iter, ft);
817 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
819 /* The driver is out of sync with the FW */
828 /* Connect flow tables from previous priority of prio to ft */
829 static int connect_prev_fts(struct mlx5_core_dev *dev,
830 struct mlx5_flow_table *ft,
831 struct fs_prio *prio)
833 struct mlx5_flow_table *prev_ft;
835 prev_ft = find_prev_chained_ft(prio);
837 struct fs_prio *prev_prio;
839 fs_get_obj(prev_prio, prev_ft->node.parent);
840 return connect_fts_in_prio(dev, prev_prio, ft);
845 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
848 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
849 struct mlx5_ft_underlay_qp *uqp;
850 int min_level = INT_MAX;
855 min_level = root->root_ft->level;
857 if (ft->level >= min_level)
860 if (list_empty(&root->underlay_qpns)) {
861 /* Don't set any QPN (zero) in case QPN list is empty */
863 err = root->cmds->update_root_ft(root->dev, ft, qpn, false);
865 list_for_each_entry(uqp, &root->underlay_qpns, list) {
867 err = root->cmds->update_root_ft(root->dev, ft,
875 mlx5_core_warn(root->dev,
876 "Update root flow table of id(%u) qpn(%d) failed\n",
884 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
885 struct mlx5_flow_destination *dest)
887 struct mlx5_flow_root_namespace *root;
888 struct mlx5_flow_table *ft;
889 struct mlx5_flow_group *fg;
891 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
894 fs_get_obj(fte, rule->node.parent);
895 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
897 down_write_ref_node(&fte->node);
898 fs_get_obj(fg, fte->node.parent);
899 fs_get_obj(ft, fg->node.parent);
901 memcpy(&rule->dest_attr, dest, sizeof(*dest));
902 root = find_root(&ft->node);
903 err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
905 up_write_ref_node(&fte->node);
910 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
911 struct mlx5_flow_destination *new_dest,
912 struct mlx5_flow_destination *old_dest)
917 if (handle->num_rules != 1)
919 return _mlx5_modify_rule_destination(handle->rule[0],
923 for (i = 0; i < handle->num_rules; i++) {
924 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
925 return _mlx5_modify_rule_destination(handle->rule[i],
932 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
933 static int connect_fwd_rules(struct mlx5_core_dev *dev,
934 struct mlx5_flow_table *new_next_ft,
935 struct mlx5_flow_table *old_next_ft)
937 struct mlx5_flow_destination dest = {};
938 struct mlx5_flow_rule *iter;
941 /* new_next_ft and old_next_ft could be NULL only
942 * when we create/destroy the anchor flow table.
944 if (!new_next_ft || !old_next_ft)
947 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
948 dest.ft = new_next_ft;
950 mutex_lock(&old_next_ft->lock);
951 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
952 mutex_unlock(&old_next_ft->lock);
953 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
954 err = _mlx5_modify_rule_destination(iter, &dest);
956 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
962 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
963 struct fs_prio *prio)
965 struct mlx5_flow_table *next_ft;
968 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
970 if (list_empty(&prio->node.children)) {
971 err = connect_prev_fts(dev, ft, prio);
975 next_ft = find_next_chained_ft(prio);
976 err = connect_fwd_rules(dev, ft, next_ft);
981 if (MLX5_CAP_FLOWTABLE(dev,
982 flow_table_properties_nic_receive.modify_root))
983 err = update_root_ft_create(ft, prio);
987 static void list_add_flow_table(struct mlx5_flow_table *ft,
988 struct fs_prio *prio)
990 struct list_head *prev = &prio->node.children;
991 struct mlx5_flow_table *iter;
993 fs_for_each_ft(iter, prio) {
994 if (iter->level > ft->level)
996 prev = &iter->node.list;
998 list_add(&ft->node.list, prev);
1001 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1002 struct mlx5_flow_table_attr *ft_attr,
1003 enum fs_flow_table_op_mod op_mod,
1006 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
1007 struct mlx5_flow_table *next_ft = NULL;
1008 struct fs_prio *fs_prio = NULL;
1009 struct mlx5_flow_table *ft;
1014 pr_err("mlx5: flow steering failed to find root of namespace\n");
1015 return ERR_PTR(-ENODEV);
1018 mutex_lock(&root->chain_lock);
1019 fs_prio = find_prio(ns, ft_attr->prio);
1024 if (ft_attr->level >= fs_prio->num_levels) {
1028 /* The level is related to the
1029 * priority level range.
1031 ft_attr->level += fs_prio->start_level;
1032 ft = alloc_flow_table(ft_attr->level,
1034 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
1036 op_mod, ft_attr->flags);
1042 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1043 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
1044 next_ft = find_next_chained_ft(fs_prio);
1045 err = root->cmds->create_flow_table(root->dev, ft->vport, ft->op_mod,
1046 ft->type, ft->level, log_table_sz,
1047 next_ft, &ft->id, ft->flags);
1051 err = connect_flow_table(root->dev, ft, fs_prio);
1054 ft->node.active = true;
1055 down_write_ref_node(&fs_prio->node);
1056 tree_add_node(&ft->node, &fs_prio->node);
1057 list_add_flow_table(ft, fs_prio);
1059 up_write_ref_node(&fs_prio->node);
1060 mutex_unlock(&root->chain_lock);
1063 root->cmds->destroy_flow_table(root->dev, ft);
1067 mutex_unlock(&root->chain_lock);
1068 return ERR_PTR(err);
1071 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1072 struct mlx5_flow_table_attr *ft_attr)
1074 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1077 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1078 int prio, int max_fte,
1079 u32 level, u16 vport)
1081 struct mlx5_flow_table_attr ft_attr = {};
1083 ft_attr.max_fte = max_fte;
1084 ft_attr.level = level;
1085 ft_attr.prio = prio;
1087 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1090 struct mlx5_flow_table*
1091 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1092 int prio, u32 level)
1094 struct mlx5_flow_table_attr ft_attr = {};
1096 ft_attr.level = level;
1097 ft_attr.prio = prio;
1098 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1100 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1102 struct mlx5_flow_table*
1103 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1105 int num_flow_table_entries,
1110 struct mlx5_flow_table_attr ft_attr = {};
1111 struct mlx5_flow_table *ft;
1113 if (max_num_groups > num_flow_table_entries)
1114 return ERR_PTR(-EINVAL);
1116 ft_attr.max_fte = num_flow_table_entries;
1117 ft_attr.prio = prio;
1118 ft_attr.level = level;
1119 ft_attr.flags = flags;
1121 ft = mlx5_create_flow_table(ns, &ft_attr);
1125 ft->autogroup.active = true;
1126 ft->autogroup.required_groups = max_num_groups;
1130 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1132 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1135 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1136 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1137 fg_in, match_criteria);
1138 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1140 match_criteria_enable);
1141 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1143 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1145 struct mlx5_core_dev *dev = get_dev(&ft->node);
1146 struct mlx5_flow_group *fg;
1149 if (!check_valid_mask(match_criteria_enable, match_criteria))
1150 return ERR_PTR(-EINVAL);
1152 if (ft->autogroup.active)
1153 return ERR_PTR(-EPERM);
1155 down_write_ref_node(&ft->node);
1156 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1157 start_index, end_index,
1158 ft->node.children.prev);
1159 up_write_ref_node(&ft->node);
1163 err = root->cmds->create_flow_group(dev, ft, fg_in, &fg->id);
1165 tree_put_node(&fg->node);
1166 return ERR_PTR(err);
1168 trace_mlx5_fs_add_fg(fg);
1169 fg->node.active = true;
1174 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1176 struct mlx5_flow_rule *rule;
1178 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1182 INIT_LIST_HEAD(&rule->next_ft);
1183 rule->node.type = FS_TYPE_FLOW_DEST;
1185 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1190 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1192 struct mlx5_flow_handle *handle;
1194 handle = kzalloc(sizeof(*handle) + sizeof(handle->rule[0]) *
1195 num_rules, GFP_KERNEL);
1199 handle->num_rules = num_rules;
1204 static void destroy_flow_handle(struct fs_fte *fte,
1205 struct mlx5_flow_handle *handle,
1206 struct mlx5_flow_destination *dest,
1210 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1212 list_del(&handle->rule[i]->node.list);
1213 kfree(handle->rule[i]);
1219 static struct mlx5_flow_handle *
1220 create_flow_handle(struct fs_fte *fte,
1221 struct mlx5_flow_destination *dest,
1226 struct mlx5_flow_handle *handle;
1227 struct mlx5_flow_rule *rule = NULL;
1228 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1229 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1233 handle = alloc_handle((dest_num) ? dest_num : 1);
1235 return ERR_PTR(-ENOMEM);
1239 rule = find_flow_rule(fte, dest + i);
1241 refcount_inc(&rule->node.refcount);
1247 rule = alloc_rule(dest + i);
1251 /* Add dest to dests list- we need flow tables to be in the
1252 * end of the list for forward to next prio rules.
1254 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1256 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1257 list_add(&rule->node.list, &fte->node.children);
1259 list_add_tail(&rule->node.list, &fte->node.children);
1263 type = dest[i].type ==
1264 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1265 *modify_mask |= type ? count : dst;
1268 handle->rule[i] = rule;
1269 } while (++i < dest_num);
1274 destroy_flow_handle(fte, handle, dest, i);
1275 return ERR_PTR(-ENOMEM);
1278 /* fte should not be deleted while calling this function */
1279 static struct mlx5_flow_handle *
1280 add_rule_fte(struct fs_fte *fte,
1281 struct mlx5_flow_group *fg,
1282 struct mlx5_flow_destination *dest,
1286 struct mlx5_flow_root_namespace *root;
1287 struct mlx5_flow_handle *handle;
1288 struct mlx5_flow_table *ft;
1289 int modify_mask = 0;
1291 bool new_rule = false;
1293 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1295 if (IS_ERR(handle) || !new_rule)
1299 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1301 fs_get_obj(ft, fg->node.parent);
1302 root = find_root(&fg->node);
1303 if (!(fte->status & FS_FTE_STATUS_EXISTING))
1304 err = root->cmds->create_fte(get_dev(&ft->node),
1307 err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
1312 fte->node.active = true;
1313 fte->status |= FS_FTE_STATUS_EXISTING;
1314 atomic_inc(&fte->node.version);
1320 destroy_flow_handle(fte, handle, dest, handle->num_rules);
1321 return ERR_PTR(err);
1324 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
1325 struct mlx5_flow_spec *spec)
1327 struct list_head *prev = &ft->node.children;
1328 struct mlx5_flow_group *fg;
1329 unsigned int candidate_index = 0;
1330 unsigned int group_size = 0;
1332 if (!ft->autogroup.active)
1333 return ERR_PTR(-ENOENT);
1335 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1336 /* We save place for flow groups in addition to max types */
1337 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
1339 /* ft->max_fte == ft->autogroup.max_types */
1340 if (group_size == 0)
1343 /* sorted by start_index */
1344 fs_for_each_fg(fg, ft) {
1345 if (candidate_index + group_size > fg->start_index)
1346 candidate_index = fg->start_index + fg->max_ftes;
1349 prev = &fg->node.list;
1352 if (candidate_index + group_size > ft->max_fte)
1353 return ERR_PTR(-ENOSPC);
1355 fg = alloc_insert_flow_group(ft,
1356 spec->match_criteria_enable,
1357 spec->match_criteria,
1359 candidate_index + group_size - 1,
1364 ft->autogroup.num_groups++;
1370 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1371 struct mlx5_flow_group *fg)
1373 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1374 struct mlx5_core_dev *dev = get_dev(&ft->node);
1375 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1376 void *match_criteria_addr;
1380 in = kvzalloc(inlen, GFP_KERNEL);
1384 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1385 fg->mask.match_criteria_enable);
1386 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1387 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1389 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1390 in, match_criteria);
1391 memcpy(match_criteria_addr, fg->mask.match_criteria,
1392 sizeof(fg->mask.match_criteria));
1394 err = root->cmds->create_flow_group(dev, ft, in, &fg->id);
1396 fg->node.active = true;
1397 trace_mlx5_fs_add_fg(fg);
1404 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1405 struct mlx5_flow_destination *d2)
1407 if (d1->type == d2->type) {
1408 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1409 d1->vport_num == d2->vport_num) ||
1410 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1411 d1->ft == d2->ft) ||
1412 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1413 d1->tir_num == d2->tir_num))
1420 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1421 struct mlx5_flow_destination *dest)
1423 struct mlx5_flow_rule *rule;
1425 list_for_each_entry(rule, &fte->node.children, node.list) {
1426 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1432 static bool check_conflicting_actions(u32 action1, u32 action2)
1434 u32 xored_actions = action1 ^ action2;
1436 /* if one rule only wants to count, it's ok */
1437 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1438 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1441 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1442 MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1443 MLX5_FLOW_CONTEXT_ACTION_DECAP |
1444 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1445 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1446 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH))
1452 static int check_conflicting_ftes(struct fs_fte *fte, const struct mlx5_flow_act *flow_act)
1454 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
1455 mlx5_core_warn(get_dev(&fte->node),
1456 "Found two FTEs with conflicting actions\n");
1460 if (flow_act->has_flow_tag &&
1461 fte->action.flow_tag != flow_act->flow_tag) {
1462 mlx5_core_warn(get_dev(&fte->node),
1463 "FTE flow tag %u already exists with different flow tag %u\n",
1464 fte->action.flow_tag,
1465 flow_act->flow_tag);
1472 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1474 struct mlx5_flow_act *flow_act,
1475 struct mlx5_flow_destination *dest,
1479 struct mlx5_flow_handle *handle;
1484 ret = check_conflicting_ftes(fte, flow_act);
1486 return ERR_PTR(ret);
1488 old_action = fte->action.action;
1489 fte->action.action |= flow_act->action;
1490 handle = add_rule_fte(fte, fg, dest, dest_num,
1491 old_action != flow_act->action);
1492 if (IS_ERR(handle)) {
1493 fte->action.action = old_action;
1496 trace_mlx5_fs_set_fte(fte, false);
1498 for (i = 0; i < handle->num_rules; i++) {
1499 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1500 tree_add_node(&handle->rule[i]->node, &fte->node);
1501 trace_mlx5_fs_add_rule(handle->rule[i]);
1507 struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_handle *handle)
1509 struct mlx5_flow_rule *dst;
1512 fs_get_obj(fte, handle->rule[0]->node.parent);
1514 fs_for_each_dst(dst, fte) {
1515 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
1516 return dst->dest_attr.counter;
1522 static bool counter_is_valid(struct mlx5_fc *counter, u32 action)
1524 if (!(action & MLX5_FLOW_CONTEXT_ACTION_COUNT))
1530 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1531 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1534 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1536 struct mlx5_flow_table *ft)
1538 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1539 return counter_is_valid(dest->counter, action);
1541 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1544 if (!dest || ((dest->type ==
1545 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1546 (dest->ft->level <= ft->level)))
1552 struct list_head list;
1553 struct mlx5_flow_group *g;
1556 struct match_list_head {
1557 struct list_head list;
1558 struct match_list first;
1561 static void free_match_list(struct match_list_head *head)
1563 if (!list_empty(&head->list)) {
1564 struct match_list *iter, *match_tmp;
1566 list_del(&head->first.list);
1567 tree_put_node(&head->first.g->node);
1568 list_for_each_entry_safe(iter, match_tmp, &head->list,
1570 tree_put_node(&iter->g->node);
1571 list_del(&iter->list);
1577 static int build_match_list(struct match_list_head *match_head,
1578 struct mlx5_flow_table *ft,
1579 struct mlx5_flow_spec *spec)
1581 struct rhlist_head *tmp, *list;
1582 struct mlx5_flow_group *g;
1586 INIT_LIST_HEAD(&match_head->list);
1587 /* Collect all fgs which has a matching match_criteria */
1588 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1589 /* RCU is atomic, we can't execute FW commands here */
1590 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1591 struct match_list *curr_match;
1593 if (likely(list_empty(&match_head->list))) {
1594 if (!tree_get_node(&g->node))
1596 match_head->first.g = g;
1597 list_add_tail(&match_head->first.list,
1602 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1604 free_match_list(match_head);
1608 if (!tree_get_node(&g->node)) {
1613 list_add_tail(&curr_match->list, &match_head->list);
1620 static u64 matched_fgs_get_version(struct list_head *match_head)
1622 struct match_list *iter;
1625 list_for_each_entry(iter, match_head, list)
1626 version += (u64)atomic_read(&iter->g->node.version);
1630 static struct mlx5_flow_handle *
1631 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1632 struct list_head *match_head,
1633 struct mlx5_flow_spec *spec,
1634 struct mlx5_flow_act *flow_act,
1635 struct mlx5_flow_destination *dest,
1639 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1640 struct mlx5_flow_group *g;
1641 struct mlx5_flow_handle *rule;
1642 struct match_list *iter;
1643 bool take_write = false;
1648 fte = alloc_fte(ft, spec->match_value, flow_act);
1650 return ERR_PTR(-ENOMEM);
1652 list_for_each_entry(iter, match_head, list) {
1653 nested_down_read_ref_node(&iter->g->node, FS_LOCK_PARENT);
1656 search_again_locked:
1657 version = matched_fgs_get_version(match_head);
1658 /* Try to find a fg that already contains a matching fte */
1659 list_for_each_entry(iter, match_head, list) {
1660 struct fs_fte *fte_tmp;
1663 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, spec->match_value,
1665 if (!fte_tmp || !tree_get_node(&fte_tmp->node))
1668 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1670 list_for_each_entry(iter, match_head, list)
1671 up_read_ref_node(&iter->g->node);
1673 list_for_each_entry(iter, match_head, list)
1674 up_write_ref_node(&iter->g->node);
1677 rule = add_rule_fg(g, spec->match_value,
1678 flow_act, dest, dest_num, fte_tmp);
1679 up_write_ref_node(&fte_tmp->node);
1680 tree_put_node(&fte_tmp->node);
1681 kmem_cache_free(steering->ftes_cache, fte);
1685 /* No group with matching fte found. Try to add a new fte to any
1690 list_for_each_entry(iter, match_head, list)
1691 up_read_ref_node(&iter->g->node);
1692 list_for_each_entry(iter, match_head, list)
1693 nested_down_write_ref_node(&iter->g->node,
1698 /* Check the ft version, for case that new flow group
1699 * was added while the fgs weren't locked
1701 if (atomic_read(&ft->node.version) != ft_version) {
1702 rule = ERR_PTR(-EAGAIN);
1706 /* Check the fgs version, for case the new FTE with the
1707 * same values was added while the fgs weren't locked
1709 if (version != matched_fgs_get_version(match_head))
1710 goto search_again_locked;
1712 list_for_each_entry(iter, match_head, list) {
1715 if (!g->node.active)
1717 err = insert_fte(g, fte);
1721 list_for_each_entry(iter, match_head, list)
1722 up_write_ref_node(&iter->g->node);
1723 kmem_cache_free(steering->ftes_cache, fte);
1724 return ERR_PTR(err);
1727 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1728 list_for_each_entry(iter, match_head, list)
1729 up_write_ref_node(&iter->g->node);
1730 rule = add_rule_fg(g, spec->match_value,
1731 flow_act, dest, dest_num, fte);
1732 up_write_ref_node(&fte->node);
1733 tree_put_node(&fte->node);
1736 rule = ERR_PTR(-ENOENT);
1738 list_for_each_entry(iter, match_head, list)
1739 up_write_ref_node(&iter->g->node);
1740 kmem_cache_free(steering->ftes_cache, fte);
1744 static struct mlx5_flow_handle *
1745 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1746 struct mlx5_flow_spec *spec,
1747 struct mlx5_flow_act *flow_act,
1748 struct mlx5_flow_destination *dest,
1752 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1753 struct mlx5_flow_group *g;
1754 struct mlx5_flow_handle *rule;
1755 struct match_list_head match_head;
1756 bool take_write = false;
1762 if (!check_valid_spec(spec))
1763 return ERR_PTR(-EINVAL);
1765 for (i = 0; i < dest_num; i++) {
1766 if (!dest_is_valid(&dest[i], flow_act->action, ft))
1767 return ERR_PTR(-EINVAL);
1769 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1770 search_again_locked:
1771 version = atomic_read(&ft->node.version);
1773 /* Collect all fgs which has a matching match_criteria */
1774 err = build_match_list(&match_head, ft, spec);
1777 up_write_ref_node(&ft->node);
1778 return ERR_PTR(err);
1782 up_read_ref_node(&ft->node);
1784 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1786 free_match_list(&match_head);
1787 if (!IS_ERR(rule) ||
1788 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1790 up_write_ref_node(&ft->node);
1795 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1799 if (PTR_ERR(rule) == -EAGAIN ||
1800 version != atomic_read(&ft->node.version))
1801 goto search_again_locked;
1803 g = alloc_auto_flow_group(ft, spec);
1806 up_write_ref_node(&ft->node);
1810 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1811 up_write_ref_node(&ft->node);
1813 err = create_auto_flow_group(ft, g);
1815 goto err_release_fg;
1817 fte = alloc_fte(ft, spec->match_value, flow_act);
1820 goto err_release_fg;
1823 err = insert_fte(g, fte);
1825 kmem_cache_free(steering->ftes_cache, fte);
1826 goto err_release_fg;
1829 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1830 up_write_ref_node(&g->node);
1831 rule = add_rule_fg(g, spec->match_value, flow_act, dest,
1833 up_write_ref_node(&fte->node);
1834 tree_put_node(&fte->node);
1835 tree_put_node(&g->node);
1839 up_write_ref_node(&g->node);
1840 tree_put_node(&g->node);
1841 return ERR_PTR(err);
1844 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1846 return ((ft->type == FS_FT_NIC_RX) &&
1847 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1850 struct mlx5_flow_handle *
1851 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1852 struct mlx5_flow_spec *spec,
1853 struct mlx5_flow_act *flow_act,
1854 struct mlx5_flow_destination *dest,
1857 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1858 struct mlx5_flow_destination gen_dest = {};
1859 struct mlx5_flow_table *next_ft = NULL;
1860 struct mlx5_flow_handle *handle = NULL;
1861 u32 sw_action = flow_act->action;
1862 struct fs_prio *prio;
1864 fs_get_obj(prio, ft->node.parent);
1865 if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1866 if (!fwd_next_prio_supported(ft))
1867 return ERR_PTR(-EOPNOTSUPP);
1869 return ERR_PTR(-EINVAL);
1870 mutex_lock(&root->chain_lock);
1871 next_ft = find_next_chained_ft(prio);
1873 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1874 gen_dest.ft = next_ft;
1877 flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1879 mutex_unlock(&root->chain_lock);
1880 return ERR_PTR(-EOPNOTSUPP);
1884 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, dest_num);
1886 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1887 if (!IS_ERR_OR_NULL(handle) &&
1888 (list_empty(&handle->rule[0]->next_ft))) {
1889 mutex_lock(&next_ft->lock);
1890 list_add(&handle->rule[0]->next_ft,
1891 &next_ft->fwd_rules);
1892 mutex_unlock(&next_ft->lock);
1893 handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1895 mutex_unlock(&root->chain_lock);
1899 EXPORT_SYMBOL(mlx5_add_flow_rules);
1901 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1905 for (i = handle->num_rules - 1; i >= 0; i--)
1906 tree_remove_node(&handle->rule[i]->node);
1909 EXPORT_SYMBOL(mlx5_del_flow_rules);
1911 /* Assuming prio->node.children(flow tables) is sorted by level */
1912 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1914 struct fs_prio *prio;
1916 fs_get_obj(prio, ft->node.parent);
1918 if (!list_is_last(&ft->node.list, &prio->node.children))
1919 return list_next_entry(ft, node.list);
1920 return find_next_chained_ft(prio);
1923 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1925 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1926 struct mlx5_ft_underlay_qp *uqp;
1927 struct mlx5_flow_table *new_root_ft = NULL;
1931 if (root->root_ft != ft)
1934 new_root_ft = find_next_ft(ft);
1936 root->root_ft = NULL;
1940 if (list_empty(&root->underlay_qpns)) {
1941 /* Don't set any QPN (zero) in case QPN list is empty */
1943 err = root->cmds->update_root_ft(root->dev, new_root_ft,
1946 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1948 err = root->cmds->update_root_ft(root->dev,
1957 mlx5_core_warn(root->dev,
1958 "Update root flow table of id(%u) qpn(%d) failed\n",
1961 root->root_ft = new_root_ft;
1966 /* Connect flow table from previous priority to
1967 * the next flow table.
1969 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1971 struct mlx5_core_dev *dev = get_dev(&ft->node);
1972 struct mlx5_flow_table *next_ft;
1973 struct fs_prio *prio;
1976 err = update_root_ft_destroy(ft);
1980 fs_get_obj(prio, ft->node.parent);
1981 if (!(list_first_entry(&prio->node.children,
1982 struct mlx5_flow_table,
1986 next_ft = find_next_chained_ft(prio);
1987 err = connect_fwd_rules(dev, next_ft, ft);
1991 err = connect_prev_fts(dev, next_ft, prio);
1993 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1998 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
2000 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2003 mutex_lock(&root->chain_lock);
2004 err = disconnect_flow_table(ft);
2006 mutex_unlock(&root->chain_lock);
2009 if (tree_remove_node(&ft->node))
2010 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2012 mutex_unlock(&root->chain_lock);
2016 EXPORT_SYMBOL(mlx5_destroy_flow_table);
2018 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
2020 if (tree_remove_node(&fg->node))
2021 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2025 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2026 enum mlx5_flow_namespace_type type)
2028 struct mlx5_flow_steering *steering = dev->priv.steering;
2029 struct mlx5_flow_root_namespace *root_ns;
2031 struct fs_prio *fs_prio;
2032 struct mlx5_flow_namespace *ns;
2038 case MLX5_FLOW_NAMESPACE_BYPASS:
2039 case MLX5_FLOW_NAMESPACE_LAG:
2040 case MLX5_FLOW_NAMESPACE_OFFLOADS:
2041 case MLX5_FLOW_NAMESPACE_ETHTOOL:
2042 case MLX5_FLOW_NAMESPACE_KERNEL:
2043 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
2044 case MLX5_FLOW_NAMESPACE_ANCHOR:
2047 case MLX5_FLOW_NAMESPACE_FDB:
2048 if (steering->fdb_root_ns)
2049 return &steering->fdb_root_ns->ns;
2052 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2053 if (steering->sniffer_rx_root_ns)
2054 return &steering->sniffer_rx_root_ns->ns;
2057 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2058 if (steering->sniffer_tx_root_ns)
2059 return &steering->sniffer_tx_root_ns->ns;
2062 case MLX5_FLOW_NAMESPACE_EGRESS:
2063 if (steering->egress_root_ns)
2064 return &steering->egress_root_ns->ns;
2071 root_ns = steering->root_ns;
2075 fs_prio = find_prio(&root_ns->ns, prio);
2079 ns = list_first_entry(&fs_prio->node.children,
2085 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2087 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2088 enum mlx5_flow_namespace_type type,
2091 struct mlx5_flow_steering *steering = dev->priv.steering;
2093 if (!steering || vport >= MLX5_TOTAL_VPORTS(dev))
2097 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2098 if (steering->esw_egress_root_ns &&
2099 steering->esw_egress_root_ns[vport])
2100 return &steering->esw_egress_root_ns[vport]->ns;
2103 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2104 if (steering->esw_ingress_root_ns &&
2105 steering->esw_ingress_root_ns[vport])
2106 return &steering->esw_ingress_root_ns[vport]->ns;
2114 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2115 unsigned int prio, int num_levels)
2117 struct fs_prio *fs_prio;
2119 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2121 return ERR_PTR(-ENOMEM);
2123 fs_prio->node.type = FS_TYPE_PRIO;
2124 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2125 tree_add_node(&fs_prio->node, &ns->node);
2126 fs_prio->num_levels = num_levels;
2127 fs_prio->prio = prio;
2128 list_add_tail(&fs_prio->node.list, &ns->node.children);
2133 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2136 ns->node.type = FS_TYPE_NAMESPACE;
2141 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2143 struct mlx5_flow_namespace *ns;
2145 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2147 return ERR_PTR(-ENOMEM);
2149 fs_init_namespace(ns);
2150 tree_init_node(&ns->node, NULL, del_sw_ns);
2151 tree_add_node(&ns->node, &prio->node);
2152 list_add_tail(&ns->node.list, &prio->node.children);
2157 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2158 struct init_tree_node *prio_metadata)
2160 struct fs_prio *fs_prio;
2163 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2164 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2165 if (IS_ERR(fs_prio))
2166 return PTR_ERR(fs_prio);
2171 #define FLOW_TABLE_BIT_SZ 1
2172 #define GET_FLOW_TABLE_CAP(dev, offset) \
2173 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
2175 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2176 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2180 for (i = 0; i < caps->arr_sz; i++) {
2181 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2187 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2188 struct init_tree_node *init_node,
2189 struct fs_node *fs_parent_node,
2190 struct init_tree_node *init_parent_node,
2193 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2194 flow_table_properties_nic_receive.
2196 struct mlx5_flow_namespace *fs_ns;
2197 struct fs_prio *fs_prio;
2198 struct fs_node *base;
2202 if (init_node->type == FS_TYPE_PRIO) {
2203 if ((init_node->min_ft_level > max_ft_level) ||
2204 !has_required_caps(steering->dev, &init_node->caps))
2207 fs_get_obj(fs_ns, fs_parent_node);
2208 if (init_node->num_leaf_prios)
2209 return create_leaf_prios(fs_ns, prio, init_node);
2210 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2211 if (IS_ERR(fs_prio))
2212 return PTR_ERR(fs_prio);
2213 base = &fs_prio->node;
2214 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2215 fs_get_obj(fs_prio, fs_parent_node);
2216 fs_ns = fs_create_namespace(fs_prio);
2218 return PTR_ERR(fs_ns);
2219 base = &fs_ns->node;
2224 for (i = 0; i < init_node->ar_size; i++) {
2225 err = init_root_tree_recursive(steering, &init_node->children[i],
2226 base, init_node, prio);
2229 if (init_node->children[i].type == FS_TYPE_PRIO &&
2230 init_node->children[i].num_leaf_prios) {
2231 prio += init_node->children[i].num_leaf_prios;
2238 static int init_root_tree(struct mlx5_flow_steering *steering,
2239 struct init_tree_node *init_node,
2240 struct fs_node *fs_parent_node)
2243 struct mlx5_flow_namespace *fs_ns;
2246 fs_get_obj(fs_ns, fs_parent_node);
2247 for (i = 0; i < init_node->ar_size; i++) {
2248 err = init_root_tree_recursive(steering, &init_node->children[i],
2257 static struct mlx5_flow_root_namespace
2258 *create_root_ns(struct mlx5_flow_steering *steering,
2259 enum fs_flow_table_type table_type)
2261 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2262 struct mlx5_flow_root_namespace *root_ns;
2263 struct mlx5_flow_namespace *ns;
2265 if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2266 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2267 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2269 /* Create the root namespace */
2270 root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
2274 root_ns->dev = steering->dev;
2275 root_ns->table_type = table_type;
2276 root_ns->cmds = cmds;
2278 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2281 fs_init_namespace(ns);
2282 mutex_init(&root_ns->chain_lock);
2283 tree_init_node(&ns->node, NULL, NULL);
2284 tree_add_node(&ns->node, NULL);
2289 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2291 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2293 struct fs_prio *prio;
2295 fs_for_each_prio(prio, ns) {
2296 /* This updates prio start_level and num_levels */
2297 set_prio_attrs_in_prio(prio, acc_level);
2298 acc_level += prio->num_levels;
2303 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2305 struct mlx5_flow_namespace *ns;
2306 int acc_level_ns = acc_level;
2308 prio->start_level = acc_level;
2309 fs_for_each_ns(ns, prio)
2310 /* This updates start_level and num_levels of ns's priority descendants */
2311 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2312 if (!prio->num_levels)
2313 prio->num_levels = acc_level_ns - prio->start_level;
2314 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2317 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2319 struct mlx5_flow_namespace *ns = &root_ns->ns;
2320 struct fs_prio *prio;
2321 int start_level = 0;
2323 fs_for_each_prio(prio, ns) {
2324 set_prio_attrs_in_prio(prio, start_level);
2325 start_level += prio->num_levels;
2329 #define ANCHOR_PRIO 0
2330 #define ANCHOR_SIZE 1
2331 #define ANCHOR_LEVEL 0
2332 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2334 struct mlx5_flow_namespace *ns = NULL;
2335 struct mlx5_flow_table_attr ft_attr = {};
2336 struct mlx5_flow_table *ft;
2338 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2342 ft_attr.max_fte = ANCHOR_SIZE;
2343 ft_attr.level = ANCHOR_LEVEL;
2344 ft_attr.prio = ANCHOR_PRIO;
2346 ft = mlx5_create_flow_table(ns, &ft_attr);
2348 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2354 static int init_root_ns(struct mlx5_flow_steering *steering)
2358 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2359 if (!steering->root_ns)
2362 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2366 set_prio_attrs(steering->root_ns);
2367 err = create_anchor_flow_table(steering);
2374 cleanup_root_ns(steering->root_ns);
2375 steering->root_ns = NULL;
2379 static void clean_tree(struct fs_node *node)
2382 struct fs_node *iter;
2383 struct fs_node *temp;
2385 tree_get_node(node);
2386 list_for_each_entry_safe(iter, temp, &node->children, list)
2388 tree_put_node(node);
2389 tree_remove_node(node);
2393 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2398 clean_tree(&root_ns->ns.node);
2401 static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2403 struct mlx5_flow_steering *steering = dev->priv.steering;
2406 if (!steering->esw_egress_root_ns)
2409 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2410 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2412 kfree(steering->esw_egress_root_ns);
2415 static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2417 struct mlx5_flow_steering *steering = dev->priv.steering;
2420 if (!steering->esw_ingress_root_ns)
2423 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2424 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2426 kfree(steering->esw_ingress_root_ns);
2429 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2431 struct mlx5_flow_steering *steering = dev->priv.steering;
2433 cleanup_root_ns(steering->root_ns);
2434 cleanup_egress_acls_root_ns(dev);
2435 cleanup_ingress_acls_root_ns(dev);
2436 cleanup_root_ns(steering->fdb_root_ns);
2437 cleanup_root_ns(steering->sniffer_rx_root_ns);
2438 cleanup_root_ns(steering->sniffer_tx_root_ns);
2439 cleanup_root_ns(steering->egress_root_ns);
2440 mlx5_cleanup_fc_stats(dev);
2441 kmem_cache_destroy(steering->ftes_cache);
2442 kmem_cache_destroy(steering->fgs_cache);
2446 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2448 struct fs_prio *prio;
2450 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2451 if (!steering->sniffer_tx_root_ns)
2454 /* Create single prio */
2455 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2457 cleanup_root_ns(steering->sniffer_tx_root_ns);
2458 return PTR_ERR(prio);
2463 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2465 struct fs_prio *prio;
2467 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2468 if (!steering->sniffer_rx_root_ns)
2471 /* Create single prio */
2472 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2474 cleanup_root_ns(steering->sniffer_rx_root_ns);
2475 return PTR_ERR(prio);
2480 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2482 struct fs_prio *prio;
2484 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2485 if (!steering->fdb_root_ns)
2488 prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 1);
2492 prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2496 set_prio_attrs(steering->fdb_root_ns);
2500 cleanup_root_ns(steering->fdb_root_ns);
2501 steering->fdb_root_ns = NULL;
2502 return PTR_ERR(prio);
2505 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2507 struct fs_prio *prio;
2509 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2510 if (!steering->esw_egress_root_ns[vport])
2514 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2515 return PTR_ERR_OR_ZERO(prio);
2518 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2520 struct fs_prio *prio;
2522 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2523 if (!steering->esw_ingress_root_ns[vport])
2527 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2528 return PTR_ERR_OR_ZERO(prio);
2531 static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2533 struct mlx5_flow_steering *steering = dev->priv.steering;
2537 steering->esw_egress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2538 sizeof(*steering->esw_egress_root_ns),
2540 if (!steering->esw_egress_root_ns)
2543 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2544 err = init_egress_acl_root_ns(steering, i);
2546 goto cleanup_root_ns;
2552 for (i--; i >= 0; i--)
2553 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2554 kfree(steering->esw_egress_root_ns);
2558 static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2560 struct mlx5_flow_steering *steering = dev->priv.steering;
2564 steering->esw_ingress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2565 sizeof(*steering->esw_ingress_root_ns),
2567 if (!steering->esw_ingress_root_ns)
2570 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2571 err = init_ingress_acl_root_ns(steering, i);
2573 goto cleanup_root_ns;
2579 for (i--; i >= 0; i--)
2580 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2581 kfree(steering->esw_ingress_root_ns);
2585 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2587 struct fs_prio *prio;
2589 steering->egress_root_ns = create_root_ns(steering,
2591 if (!steering->egress_root_ns)
2595 prio = fs_create_prio(&steering->egress_root_ns->ns, 0, 1);
2596 return PTR_ERR_OR_ZERO(prio);
2599 int mlx5_init_fs(struct mlx5_core_dev *dev)
2601 struct mlx5_flow_steering *steering;
2604 err = mlx5_init_fc_stats(dev);
2608 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2611 steering->dev = dev;
2612 dev->priv.steering = steering;
2614 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2615 sizeof(struct mlx5_flow_group), 0,
2617 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2619 if (!steering->ftes_cache || !steering->fgs_cache) {
2624 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2625 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2626 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2627 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2628 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2629 err = init_root_ns(steering);
2634 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
2635 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2636 err = init_fdb_root_ns(steering);
2640 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2641 err = init_egress_acls_root_ns(dev);
2645 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2646 err = init_ingress_acls_root_ns(dev);
2652 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2653 err = init_sniffer_rx_root_ns(steering);
2658 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2659 err = init_sniffer_tx_root_ns(steering);
2664 if (MLX5_IPSEC_DEV(dev)) {
2665 err = init_egress_root_ns(steering);
2672 mlx5_cleanup_fs(dev);
2676 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2678 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2679 struct mlx5_ft_underlay_qp *new_uqp;
2682 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2686 mutex_lock(&root->chain_lock);
2688 if (!root->root_ft) {
2690 goto update_ft_fail;
2693 err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2696 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2698 goto update_ft_fail;
2701 new_uqp->qpn = underlay_qpn;
2702 list_add_tail(&new_uqp->list, &root->underlay_qpns);
2704 mutex_unlock(&root->chain_lock);
2709 mutex_unlock(&root->chain_lock);
2713 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2715 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2717 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2718 struct mlx5_ft_underlay_qp *uqp;
2722 mutex_lock(&root->chain_lock);
2723 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2724 if (uqp->qpn == underlay_qpn) {
2731 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2737 err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2740 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2743 list_del(&uqp->list);
2744 mutex_unlock(&root->chain_lock);
2750 mutex_unlock(&root->chain_lock);
2753 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);