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>
35 #include <linux/mlx5/eswitch.h>
37 #include "mlx5_core.h"
40 #include "diag/fs_tracepoint.h"
41 #include "accel/ipsec.h"
42 #include "fpga/ipsec.h"
45 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
46 sizeof(struct init_tree_node))
48 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
49 ...) {.type = FS_TYPE_PRIO,\
50 .min_ft_level = min_level_val,\
51 .num_levels = num_levels_val,\
52 .num_leaf_prios = num_prios_val,\
54 .children = (struct init_tree_node[]) {__VA_ARGS__},\
55 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
58 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
59 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
62 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
63 .children = (struct init_tree_node[]) {__VA_ARGS__},\
64 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
67 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
70 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
72 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
73 .caps = (long[]) {__VA_ARGS__} }
75 #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
76 FS_CAP(flow_table_properties_nic_receive.modify_root), \
77 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
78 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
80 #define FS_CHAINING_CAPS_EGRESS \
82 FS_CAP(flow_table_properties_nic_transmit.flow_modify_en), \
83 FS_CAP(flow_table_properties_nic_transmit.modify_root), \
84 FS_CAP(flow_table_properties_nic_transmit \
85 .identified_miss_table_mode), \
86 FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
88 #define LEFTOVERS_NUM_LEVELS 1
89 #define LEFTOVERS_NUM_PRIOS 1
91 #define BY_PASS_PRIO_NUM_LEVELS 1
92 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
95 #define ETHTOOL_PRIO_NUM_LEVELS 1
96 #define ETHTOOL_NUM_PRIOS 11
97 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
98 /* Vlan, mac, ttc, inner ttc, aRFS */
99 #define KERNEL_NIC_PRIO_NUM_LEVELS 5
100 #define KERNEL_NIC_NUM_PRIOS 1
101 /* One more level for tc */
102 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
104 #define KERNEL_NIC_TC_NUM_PRIOS 1
105 #define KERNEL_NIC_TC_NUM_LEVELS 2
107 #define ANCHOR_NUM_LEVELS 1
108 #define ANCHOR_NUM_PRIOS 1
109 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
111 #define OFFLOADS_MAX_FT 1
112 #define OFFLOADS_NUM_PRIOS 1
113 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
115 #define LAG_PRIO_NUM_LEVELS 1
116 #define LAG_NUM_PRIOS 1
117 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
124 static struct init_tree_node {
125 enum fs_node_type type;
126 struct init_tree_node *children;
128 struct node_caps caps;
134 .type = FS_TYPE_NAMESPACE,
136 .children = (struct init_tree_node[]) {
137 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
139 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
140 BY_PASS_PRIO_NUM_LEVELS))),
141 ADD_PRIO(0, LAG_MIN_LEVEL, 0,
143 ADD_NS(ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
144 LAG_PRIO_NUM_LEVELS))),
145 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {},
146 ADD_NS(ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS, OFFLOADS_MAX_FT))),
147 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0,
149 ADD_NS(ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
150 ETHTOOL_PRIO_NUM_LEVELS))),
151 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
152 ADD_NS(ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS, KERNEL_NIC_TC_NUM_LEVELS),
153 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
154 KERNEL_NIC_PRIO_NUM_LEVELS))),
155 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
157 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_NUM_LEVELS))),
158 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
159 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_NUM_LEVELS))),
163 static struct init_tree_node egress_root_fs = {
164 .type = FS_TYPE_NAMESPACE,
166 .children = (struct init_tree_node[]) {
167 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
168 FS_CHAINING_CAPS_EGRESS,
169 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
170 BY_PASS_PRIO_NUM_LEVELS))),
174 enum fs_i_lock_class {
180 static const struct rhashtable_params rhash_fte = {
181 .key_len = FIELD_SIZEOF(struct fs_fte, val),
182 .key_offset = offsetof(struct fs_fte, val),
183 .head_offset = offsetof(struct fs_fte, hash),
184 .automatic_shrinking = true,
188 static const struct rhashtable_params rhash_fg = {
189 .key_len = FIELD_SIZEOF(struct mlx5_flow_group, mask),
190 .key_offset = offsetof(struct mlx5_flow_group, mask),
191 .head_offset = offsetof(struct mlx5_flow_group, hash),
192 .automatic_shrinking = true,
197 static void del_hw_flow_table(struct fs_node *node);
198 static void del_hw_flow_group(struct fs_node *node);
199 static void del_hw_fte(struct fs_node *node);
200 static void del_sw_flow_table(struct fs_node *node);
201 static void del_sw_flow_group(struct fs_node *node);
202 static void del_sw_fte(struct fs_node *node);
203 static void del_sw_prio(struct fs_node *node);
204 static void del_sw_ns(struct fs_node *node);
205 /* Delete rule (destination) is special case that
206 * requires to lock the FTE for all the deletion process.
208 static void del_sw_hw_rule(struct fs_node *node);
209 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
210 struct mlx5_flow_destination *d2);
211 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
212 static struct mlx5_flow_rule *
213 find_flow_rule(struct fs_fte *fte,
214 struct mlx5_flow_destination *dest);
216 static void tree_init_node(struct fs_node *node,
217 void (*del_hw_func)(struct fs_node *),
218 void (*del_sw_func)(struct fs_node *))
220 refcount_set(&node->refcount, 1);
221 INIT_LIST_HEAD(&node->list);
222 INIT_LIST_HEAD(&node->children);
223 init_rwsem(&node->lock);
224 node->del_hw_func = del_hw_func;
225 node->del_sw_func = del_sw_func;
226 node->active = false;
229 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
232 refcount_inc(&parent->refcount);
233 node->parent = parent;
235 /* Parent is the root */
239 node->root = parent->root;
242 static int tree_get_node(struct fs_node *node)
244 return refcount_inc_not_zero(&node->refcount);
247 static void nested_down_read_ref_node(struct fs_node *node,
248 enum fs_i_lock_class class)
251 down_read_nested(&node->lock, class);
252 refcount_inc(&node->refcount);
256 static void nested_down_write_ref_node(struct fs_node *node,
257 enum fs_i_lock_class class)
260 down_write_nested(&node->lock, class);
261 refcount_inc(&node->refcount);
265 static void down_write_ref_node(struct fs_node *node)
268 down_write(&node->lock);
269 refcount_inc(&node->refcount);
273 static void up_read_ref_node(struct fs_node *node)
275 refcount_dec(&node->refcount);
276 up_read(&node->lock);
279 static void up_write_ref_node(struct fs_node *node)
281 refcount_dec(&node->refcount);
282 up_write(&node->lock);
285 static void tree_put_node(struct fs_node *node)
287 struct fs_node *parent_node = node->parent;
289 if (refcount_dec_and_test(&node->refcount)) {
290 if (node->del_hw_func)
291 node->del_hw_func(node);
293 /* Only root namespace doesn't have parent and we just
294 * need to free its node.
296 down_write_ref_node(parent_node);
297 list_del_init(&node->list);
298 if (node->del_sw_func)
299 node->del_sw_func(node);
300 up_write_ref_node(parent_node);
306 if (!node && parent_node)
307 tree_put_node(parent_node);
310 static int tree_remove_node(struct fs_node *node)
312 if (refcount_read(&node->refcount) > 1) {
313 refcount_dec(&node->refcount);
320 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
323 struct fs_prio *iter_prio;
325 fs_for_each_prio(iter_prio, ns) {
326 if (iter_prio->prio == prio)
333 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
337 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
338 if (spec->match_value[i] & ~spec->match_criteria[i]) {
339 pr_warn("mlx5_core: match_value differs from match_criteria\n");
346 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
348 struct fs_node *root;
349 struct mlx5_flow_namespace *ns;
353 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
354 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
358 ns = container_of(root, struct mlx5_flow_namespace, node);
359 return container_of(ns, struct mlx5_flow_root_namespace, ns);
362 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
364 struct mlx5_flow_root_namespace *root = find_root(node);
367 return root->dev->priv.steering;
371 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
373 struct mlx5_flow_root_namespace *root = find_root(node);
380 static void del_sw_ns(struct fs_node *node)
385 static void del_sw_prio(struct fs_node *node)
390 static void del_hw_flow_table(struct fs_node *node)
392 struct mlx5_flow_root_namespace *root;
393 struct mlx5_flow_table *ft;
394 struct mlx5_core_dev *dev;
397 fs_get_obj(ft, node);
398 dev = get_dev(&ft->node);
399 root = find_root(&ft->node);
402 err = root->cmds->destroy_flow_table(dev, ft);
404 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
408 static void del_sw_flow_table(struct fs_node *node)
410 struct mlx5_flow_table *ft;
411 struct fs_prio *prio;
413 fs_get_obj(ft, node);
415 rhltable_destroy(&ft->fgs_hash);
416 fs_get_obj(prio, ft->node.parent);
421 static void del_sw_hw_rule(struct fs_node *node)
423 struct mlx5_flow_root_namespace *root;
424 struct mlx5_flow_rule *rule;
425 struct mlx5_flow_table *ft;
426 struct mlx5_flow_group *fg;
429 struct mlx5_core_dev *dev = get_dev(node);
431 bool update_fte = false;
433 fs_get_obj(rule, node);
434 fs_get_obj(fte, rule->node.parent);
435 fs_get_obj(fg, fte->node.parent);
436 fs_get_obj(ft, fg->node.parent);
437 trace_mlx5_fs_del_rule(rule);
438 if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
439 mutex_lock(&rule->dest_attr.ft->lock);
440 list_del(&rule->next_ft);
441 mutex_unlock(&rule->dest_attr.ft->lock);
444 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
446 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
447 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
448 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
453 if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
455 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
459 root = find_root(&ft->node);
460 if (update_fte && fte->dests_size) {
461 err = root->cmds->update_fte(dev, ft, fg->id, modify_mask, fte);
464 "%s can't del rule fg id=%d fte_index=%d\n",
465 __func__, fg->id, fte->index);
470 static void del_hw_fte(struct fs_node *node)
472 struct mlx5_flow_root_namespace *root;
473 struct mlx5_flow_table *ft;
474 struct mlx5_flow_group *fg;
475 struct mlx5_core_dev *dev;
479 fs_get_obj(fte, node);
480 fs_get_obj(fg, fte->node.parent);
481 fs_get_obj(ft, fg->node.parent);
483 trace_mlx5_fs_del_fte(fte);
484 dev = get_dev(&ft->node);
485 root = find_root(&ft->node);
487 err = root->cmds->delete_fte(dev, ft, fte);
490 "flow steering can't delete fte in index %d of flow group id %d\n",
495 static void del_sw_fte(struct fs_node *node)
497 struct mlx5_flow_steering *steering = get_steering(node);
498 struct mlx5_flow_group *fg;
502 fs_get_obj(fte, node);
503 fs_get_obj(fg, fte->node.parent);
505 err = rhashtable_remove_fast(&fg->ftes_hash,
509 ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
510 kmem_cache_free(steering->ftes_cache, fte);
513 static void del_hw_flow_group(struct fs_node *node)
515 struct mlx5_flow_root_namespace *root;
516 struct mlx5_flow_group *fg;
517 struct mlx5_flow_table *ft;
518 struct mlx5_core_dev *dev;
520 fs_get_obj(fg, node);
521 fs_get_obj(ft, fg->node.parent);
522 dev = get_dev(&ft->node);
523 trace_mlx5_fs_del_fg(fg);
525 root = find_root(&ft->node);
526 if (fg->node.active && root->cmds->destroy_flow_group(dev, ft, fg->id))
527 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
531 static void del_sw_flow_group(struct fs_node *node)
533 struct mlx5_flow_steering *steering = get_steering(node);
534 struct mlx5_flow_group *fg;
535 struct mlx5_flow_table *ft;
538 fs_get_obj(fg, node);
539 fs_get_obj(ft, fg->node.parent);
541 rhashtable_destroy(&fg->ftes_hash);
542 ida_destroy(&fg->fte_allocator);
543 if (ft->autogroup.active)
544 ft->autogroup.num_groups--;
545 err = rhltable_remove(&ft->fgs_hash,
549 kmem_cache_free(steering->fgs_cache, fg);
552 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
557 index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
561 fte->index = index + fg->start_index;
562 ret = rhashtable_insert_fast(&fg->ftes_hash,
568 tree_add_node(&fte->node, &fg->node);
569 list_add_tail(&fte->node.list, &fg->node.children);
573 ida_simple_remove(&fg->fte_allocator, index);
577 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
579 struct mlx5_flow_act *flow_act)
581 struct mlx5_flow_steering *steering = get_steering(&ft->node);
584 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
586 return ERR_PTR(-ENOMEM);
588 memcpy(fte->val, match_value, sizeof(fte->val));
589 fte->node.type = FS_TYPE_FLOW_ENTRY;
590 fte->action = *flow_act;
592 tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
597 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
598 struct mlx5_flow_group *fg)
600 rhashtable_destroy(&fg->ftes_hash);
601 kmem_cache_free(steering->fgs_cache, fg);
604 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
605 u8 match_criteria_enable,
606 void *match_criteria,
610 struct mlx5_flow_group *fg;
613 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
615 return ERR_PTR(-ENOMEM);
617 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
619 kmem_cache_free(steering->fgs_cache, fg);
622 ida_init(&fg->fte_allocator);
623 fg->mask.match_criteria_enable = match_criteria_enable;
624 memcpy(&fg->mask.match_criteria, match_criteria,
625 sizeof(fg->mask.match_criteria));
626 fg->node.type = FS_TYPE_FLOW_GROUP;
627 fg->start_index = start_index;
628 fg->max_ftes = end_index - start_index + 1;
633 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
634 u8 match_criteria_enable,
635 void *match_criteria,
638 struct list_head *prev)
640 struct mlx5_flow_steering *steering = get_steering(&ft->node);
641 struct mlx5_flow_group *fg;
644 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
645 start_index, end_index);
649 /* initialize refcnt, add to parent list */
650 ret = rhltable_insert(&ft->fgs_hash,
654 dealloc_flow_group(steering, fg);
658 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
659 tree_add_node(&fg->node, &ft->node);
660 /* Add node to group list */
661 list_add(&fg->node.list, prev);
662 atomic_inc(&ft->node.version);
667 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
668 enum fs_flow_table_type table_type,
669 enum fs_flow_table_op_mod op_mod,
672 struct mlx5_flow_table *ft;
675 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
677 return ERR_PTR(-ENOMEM);
679 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
686 ft->node.type = FS_TYPE_FLOW_TABLE;
688 ft->type = table_type;
690 ft->max_fte = max_fte;
692 INIT_LIST_HEAD(&ft->fwd_rules);
693 mutex_init(&ft->lock);
698 /* If reverse is false, then we search for the first flow table in the
699 * root sub-tree from start(closest from right), else we search for the
700 * last flow table in the root sub-tree till start(closest from left).
702 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
703 struct list_head *start,
706 #define list_advance_entry(pos, reverse) \
707 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
709 #define list_for_each_advance_continue(pos, head, reverse) \
710 for (pos = list_advance_entry(pos, reverse); \
711 &pos->list != (head); \
712 pos = list_advance_entry(pos, reverse))
714 struct fs_node *iter = list_entry(start, struct fs_node, list);
715 struct mlx5_flow_table *ft = NULL;
717 if (!root || root->type == FS_TYPE_PRIO_CHAINS)
720 list_for_each_advance_continue(iter, &root->children, reverse) {
721 if (iter->type == FS_TYPE_FLOW_TABLE) {
722 fs_get_obj(ft, iter);
725 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
733 /* If reverse if false then return the first flow table in next priority of
734 * prio in the tree, else return the last flow table in the previous priority
735 * of prio in the tree.
737 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
739 struct mlx5_flow_table *ft = NULL;
740 struct fs_node *curr_node;
741 struct fs_node *parent;
743 parent = prio->node.parent;
744 curr_node = &prio->node;
745 while (!ft && parent) {
746 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
748 parent = curr_node->parent;
753 /* Assuming all the tree is locked by mutex chain lock */
754 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
756 return find_closest_ft(prio, false);
759 /* Assuming all the tree is locked by mutex chain lock */
760 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
762 return find_closest_ft(prio, true);
765 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
766 struct fs_prio *prio,
767 struct mlx5_flow_table *ft)
769 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
770 struct mlx5_flow_table *iter;
774 fs_for_each_ft(iter, prio) {
776 err = root->cmds->modify_flow_table(dev, iter, ft);
778 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
780 /* The driver is out of sync with the FW */
789 /* Connect flow tables from previous priority of prio to ft */
790 static int connect_prev_fts(struct mlx5_core_dev *dev,
791 struct mlx5_flow_table *ft,
792 struct fs_prio *prio)
794 struct mlx5_flow_table *prev_ft;
796 prev_ft = find_prev_chained_ft(prio);
798 struct fs_prio *prev_prio;
800 fs_get_obj(prev_prio, prev_ft->node.parent);
801 return connect_fts_in_prio(dev, prev_prio, ft);
806 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
809 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
810 struct mlx5_ft_underlay_qp *uqp;
811 int min_level = INT_MAX;
816 min_level = root->root_ft->level;
818 if (ft->level >= min_level)
821 if (list_empty(&root->underlay_qpns)) {
822 /* Don't set any QPN (zero) in case QPN list is empty */
824 err = root->cmds->update_root_ft(root->dev, ft, qpn, false);
826 list_for_each_entry(uqp, &root->underlay_qpns, list) {
828 err = root->cmds->update_root_ft(root->dev, ft,
836 mlx5_core_warn(root->dev,
837 "Update root flow table of id(%u) qpn(%d) failed\n",
845 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
846 struct mlx5_flow_destination *dest)
848 struct mlx5_flow_root_namespace *root;
849 struct mlx5_flow_table *ft;
850 struct mlx5_flow_group *fg;
852 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
855 fs_get_obj(fte, rule->node.parent);
856 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
858 down_write_ref_node(&fte->node);
859 fs_get_obj(fg, fte->node.parent);
860 fs_get_obj(ft, fg->node.parent);
862 memcpy(&rule->dest_attr, dest, sizeof(*dest));
863 root = find_root(&ft->node);
864 err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
866 up_write_ref_node(&fte->node);
871 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
872 struct mlx5_flow_destination *new_dest,
873 struct mlx5_flow_destination *old_dest)
878 if (handle->num_rules != 1)
880 return _mlx5_modify_rule_destination(handle->rule[0],
884 for (i = 0; i < handle->num_rules; i++) {
885 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
886 return _mlx5_modify_rule_destination(handle->rule[i],
893 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
894 static int connect_fwd_rules(struct mlx5_core_dev *dev,
895 struct mlx5_flow_table *new_next_ft,
896 struct mlx5_flow_table *old_next_ft)
898 struct mlx5_flow_destination dest = {};
899 struct mlx5_flow_rule *iter;
902 /* new_next_ft and old_next_ft could be NULL only
903 * when we create/destroy the anchor flow table.
905 if (!new_next_ft || !old_next_ft)
908 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
909 dest.ft = new_next_ft;
911 mutex_lock(&old_next_ft->lock);
912 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
913 mutex_unlock(&old_next_ft->lock);
914 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
915 err = _mlx5_modify_rule_destination(iter, &dest);
917 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
923 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
924 struct fs_prio *prio)
926 struct mlx5_flow_table *next_ft;
929 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
931 if (list_empty(&prio->node.children)) {
932 err = connect_prev_fts(dev, ft, prio);
936 next_ft = find_next_chained_ft(prio);
937 err = connect_fwd_rules(dev, ft, next_ft);
942 if (MLX5_CAP_FLOWTABLE(dev,
943 flow_table_properties_nic_receive.modify_root))
944 err = update_root_ft_create(ft, prio);
948 static void list_add_flow_table(struct mlx5_flow_table *ft,
949 struct fs_prio *prio)
951 struct list_head *prev = &prio->node.children;
952 struct mlx5_flow_table *iter;
954 fs_for_each_ft(iter, prio) {
955 if (iter->level > ft->level)
957 prev = &iter->node.list;
959 list_add(&ft->node.list, prev);
962 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
963 struct mlx5_flow_table_attr *ft_attr,
964 enum fs_flow_table_op_mod op_mod,
967 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
968 struct mlx5_flow_table *next_ft = NULL;
969 struct fs_prio *fs_prio = NULL;
970 struct mlx5_flow_table *ft;
975 pr_err("mlx5: flow steering failed to find root of namespace\n");
976 return ERR_PTR(-ENODEV);
979 mutex_lock(&root->chain_lock);
980 fs_prio = find_prio(ns, ft_attr->prio);
985 if (ft_attr->level >= fs_prio->num_levels) {
989 /* The level is related to the
990 * priority level range.
992 ft_attr->level += fs_prio->start_level;
993 ft = alloc_flow_table(ft_attr->level,
995 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
997 op_mod, ft_attr->flags);
1003 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1004 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
1005 next_ft = find_next_chained_ft(fs_prio);
1006 err = root->cmds->create_flow_table(root->dev, ft->vport, ft->op_mod,
1007 ft->type, ft->level, log_table_sz,
1008 next_ft, &ft->id, ft->flags);
1012 err = connect_flow_table(root->dev, ft, fs_prio);
1015 ft->node.active = true;
1016 down_write_ref_node(&fs_prio->node);
1017 tree_add_node(&ft->node, &fs_prio->node);
1018 list_add_flow_table(ft, fs_prio);
1020 up_write_ref_node(&fs_prio->node);
1021 mutex_unlock(&root->chain_lock);
1024 root->cmds->destroy_flow_table(root->dev, ft);
1028 mutex_unlock(&root->chain_lock);
1029 return ERR_PTR(err);
1032 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1033 struct mlx5_flow_table_attr *ft_attr)
1035 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1038 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1039 int prio, int max_fte,
1040 u32 level, u16 vport)
1042 struct mlx5_flow_table_attr ft_attr = {};
1044 ft_attr.max_fte = max_fte;
1045 ft_attr.level = level;
1046 ft_attr.prio = prio;
1048 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1051 struct mlx5_flow_table*
1052 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1053 int prio, u32 level)
1055 struct mlx5_flow_table_attr ft_attr = {};
1057 ft_attr.level = level;
1058 ft_attr.prio = prio;
1059 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1061 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1063 struct mlx5_flow_table*
1064 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1066 int num_flow_table_entries,
1071 struct mlx5_flow_table_attr ft_attr = {};
1072 struct mlx5_flow_table *ft;
1074 if (max_num_groups > num_flow_table_entries)
1075 return ERR_PTR(-EINVAL);
1077 ft_attr.max_fte = num_flow_table_entries;
1078 ft_attr.prio = prio;
1079 ft_attr.level = level;
1080 ft_attr.flags = flags;
1082 ft = mlx5_create_flow_table(ns, &ft_attr);
1086 ft->autogroup.active = true;
1087 ft->autogroup.required_groups = max_num_groups;
1091 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1093 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1096 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1097 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1098 fg_in, match_criteria);
1099 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1101 match_criteria_enable);
1102 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1104 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1106 struct mlx5_core_dev *dev = get_dev(&ft->node);
1107 struct mlx5_flow_group *fg;
1110 if (ft->autogroup.active)
1111 return ERR_PTR(-EPERM);
1113 down_write_ref_node(&ft->node);
1114 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1115 start_index, end_index,
1116 ft->node.children.prev);
1117 up_write_ref_node(&ft->node);
1121 err = root->cmds->create_flow_group(dev, ft, fg_in, &fg->id);
1123 tree_put_node(&fg->node);
1124 return ERR_PTR(err);
1126 trace_mlx5_fs_add_fg(fg);
1127 fg->node.active = true;
1132 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1134 struct mlx5_flow_rule *rule;
1136 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1140 INIT_LIST_HEAD(&rule->next_ft);
1141 rule->node.type = FS_TYPE_FLOW_DEST;
1143 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1148 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1150 struct mlx5_flow_handle *handle;
1152 handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
1156 handle->num_rules = num_rules;
1161 static void destroy_flow_handle(struct fs_fte *fte,
1162 struct mlx5_flow_handle *handle,
1163 struct mlx5_flow_destination *dest,
1167 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1169 list_del(&handle->rule[i]->node.list);
1170 kfree(handle->rule[i]);
1176 static struct mlx5_flow_handle *
1177 create_flow_handle(struct fs_fte *fte,
1178 struct mlx5_flow_destination *dest,
1183 struct mlx5_flow_handle *handle;
1184 struct mlx5_flow_rule *rule = NULL;
1185 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1186 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1190 handle = alloc_handle((dest_num) ? dest_num : 1);
1192 return ERR_PTR(-ENOMEM);
1196 rule = find_flow_rule(fte, dest + i);
1198 refcount_inc(&rule->node.refcount);
1204 rule = alloc_rule(dest + i);
1208 /* Add dest to dests list- we need flow tables to be in the
1209 * end of the list for forward to next prio rules.
1211 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1213 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1214 list_add(&rule->node.list, &fte->node.children);
1216 list_add_tail(&rule->node.list, &fte->node.children);
1220 type = dest[i].type ==
1221 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1222 *modify_mask |= type ? count : dst;
1225 handle->rule[i] = rule;
1226 } while (++i < dest_num);
1231 destroy_flow_handle(fte, handle, dest, i);
1232 return ERR_PTR(-ENOMEM);
1235 /* fte should not be deleted while calling this function */
1236 static struct mlx5_flow_handle *
1237 add_rule_fte(struct fs_fte *fte,
1238 struct mlx5_flow_group *fg,
1239 struct mlx5_flow_destination *dest,
1243 struct mlx5_flow_root_namespace *root;
1244 struct mlx5_flow_handle *handle;
1245 struct mlx5_flow_table *ft;
1246 int modify_mask = 0;
1248 bool new_rule = false;
1250 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1252 if (IS_ERR(handle) || !new_rule)
1256 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1258 fs_get_obj(ft, fg->node.parent);
1259 root = find_root(&fg->node);
1260 if (!(fte->status & FS_FTE_STATUS_EXISTING))
1261 err = root->cmds->create_fte(get_dev(&ft->node),
1264 err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
1269 fte->node.active = true;
1270 fte->status |= FS_FTE_STATUS_EXISTING;
1271 atomic_inc(&fte->node.version);
1277 destroy_flow_handle(fte, handle, dest, handle->num_rules);
1278 return ERR_PTR(err);
1281 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
1282 struct mlx5_flow_spec *spec)
1284 struct list_head *prev = &ft->node.children;
1285 struct mlx5_flow_group *fg;
1286 unsigned int candidate_index = 0;
1287 unsigned int group_size = 0;
1289 if (!ft->autogroup.active)
1290 return ERR_PTR(-ENOENT);
1292 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1293 /* We save place for flow groups in addition to max types */
1294 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
1296 /* ft->max_fte == ft->autogroup.max_types */
1297 if (group_size == 0)
1300 /* sorted by start_index */
1301 fs_for_each_fg(fg, ft) {
1302 if (candidate_index + group_size > fg->start_index)
1303 candidate_index = fg->start_index + fg->max_ftes;
1306 prev = &fg->node.list;
1309 if (candidate_index + group_size > ft->max_fte)
1310 return ERR_PTR(-ENOSPC);
1312 fg = alloc_insert_flow_group(ft,
1313 spec->match_criteria_enable,
1314 spec->match_criteria,
1316 candidate_index + group_size - 1,
1321 ft->autogroup.num_groups++;
1327 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1328 struct mlx5_flow_group *fg)
1330 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1331 struct mlx5_core_dev *dev = get_dev(&ft->node);
1332 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1333 void *match_criteria_addr;
1334 u8 src_esw_owner_mask_on;
1339 in = kvzalloc(inlen, GFP_KERNEL);
1343 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1344 fg->mask.match_criteria_enable);
1345 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1346 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1349 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1351 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1352 source_eswitch_owner_vhca_id);
1353 MLX5_SET(create_flow_group_in, in,
1354 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1356 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1357 in, match_criteria);
1358 memcpy(match_criteria_addr, fg->mask.match_criteria,
1359 sizeof(fg->mask.match_criteria));
1361 err = root->cmds->create_flow_group(dev, ft, in, &fg->id);
1363 fg->node.active = true;
1364 trace_mlx5_fs_add_fg(fg);
1371 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1372 struct mlx5_flow_destination *d2)
1374 if (d1->type == d2->type) {
1375 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1376 d1->vport.num == d2->vport.num) ||
1377 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1378 d1->ft == d2->ft) ||
1379 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1380 d1->tir_num == d2->tir_num) ||
1381 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1382 d1->ft_num == d2->ft_num))
1389 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1390 struct mlx5_flow_destination *dest)
1392 struct mlx5_flow_rule *rule;
1394 list_for_each_entry(rule, &fte->node.children, node.list) {
1395 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1401 static bool check_conflicting_actions(u32 action1, u32 action2)
1403 u32 xored_actions = action1 ^ action2;
1405 /* if one rule only wants to count, it's ok */
1406 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1407 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1410 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1411 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
1412 MLX5_FLOW_CONTEXT_ACTION_DECAP |
1413 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1414 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1415 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1416 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1417 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
1423 static int check_conflicting_ftes(struct fs_fte *fte, const struct mlx5_flow_act *flow_act)
1425 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
1426 mlx5_core_warn(get_dev(&fte->node),
1427 "Found two FTEs with conflicting actions\n");
1431 if ((flow_act->flags & FLOW_ACT_HAS_TAG) &&
1432 fte->action.flow_tag != flow_act->flow_tag) {
1433 mlx5_core_warn(get_dev(&fte->node),
1434 "FTE flow tag %u already exists with different flow tag %u\n",
1435 fte->action.flow_tag,
1436 flow_act->flow_tag);
1443 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1445 struct mlx5_flow_act *flow_act,
1446 struct mlx5_flow_destination *dest,
1450 struct mlx5_flow_handle *handle;
1455 ret = check_conflicting_ftes(fte, flow_act);
1457 return ERR_PTR(ret);
1459 old_action = fte->action.action;
1460 fte->action.action |= flow_act->action;
1461 handle = add_rule_fte(fte, fg, dest, dest_num,
1462 old_action != flow_act->action);
1463 if (IS_ERR(handle)) {
1464 fte->action.action = old_action;
1467 trace_mlx5_fs_set_fte(fte, false);
1469 for (i = 0; i < handle->num_rules; i++) {
1470 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1471 tree_add_node(&handle->rule[i]->node, &fte->node);
1472 trace_mlx5_fs_add_rule(handle->rule[i]);
1478 static bool counter_is_valid(u32 action)
1480 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1481 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1484 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1486 struct mlx5_flow_table *ft)
1488 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1489 return counter_is_valid(action);
1491 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1494 if (!dest || ((dest->type ==
1495 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1496 (dest->ft->level <= ft->level)))
1502 struct list_head list;
1503 struct mlx5_flow_group *g;
1506 struct match_list_head {
1507 struct list_head list;
1508 struct match_list first;
1511 static void free_match_list(struct match_list_head *head)
1513 if (!list_empty(&head->list)) {
1514 struct match_list *iter, *match_tmp;
1516 list_del(&head->first.list);
1517 tree_put_node(&head->first.g->node);
1518 list_for_each_entry_safe(iter, match_tmp, &head->list,
1520 tree_put_node(&iter->g->node);
1521 list_del(&iter->list);
1527 static int build_match_list(struct match_list_head *match_head,
1528 struct mlx5_flow_table *ft,
1529 struct mlx5_flow_spec *spec)
1531 struct rhlist_head *tmp, *list;
1532 struct mlx5_flow_group *g;
1536 INIT_LIST_HEAD(&match_head->list);
1537 /* Collect all fgs which has a matching match_criteria */
1538 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1539 /* RCU is atomic, we can't execute FW commands here */
1540 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1541 struct match_list *curr_match;
1543 if (likely(list_empty(&match_head->list))) {
1544 if (!tree_get_node(&g->node))
1546 match_head->first.g = g;
1547 list_add_tail(&match_head->first.list,
1552 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1554 free_match_list(match_head);
1558 if (!tree_get_node(&g->node)) {
1563 list_add_tail(&curr_match->list, &match_head->list);
1570 static u64 matched_fgs_get_version(struct list_head *match_head)
1572 struct match_list *iter;
1575 list_for_each_entry(iter, match_head, list)
1576 version += (u64)atomic_read(&iter->g->node.version);
1580 static struct fs_fte *
1581 lookup_fte_locked(struct mlx5_flow_group *g,
1585 struct fs_fte *fte_tmp;
1588 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1590 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
1591 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
1593 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1598 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1601 up_write_ref_node(&g->node);
1603 up_read_ref_node(&g->node);
1607 static struct mlx5_flow_handle *
1608 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1609 struct list_head *match_head,
1610 struct mlx5_flow_spec *spec,
1611 struct mlx5_flow_act *flow_act,
1612 struct mlx5_flow_destination *dest,
1616 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1617 struct mlx5_flow_group *g;
1618 struct mlx5_flow_handle *rule;
1619 struct match_list *iter;
1620 bool take_write = false;
1625 fte = alloc_fte(ft, spec->match_value, flow_act);
1627 return ERR_PTR(-ENOMEM);
1629 search_again_locked:
1630 version = matched_fgs_get_version(match_head);
1631 if (flow_act->flags & FLOW_ACT_NO_APPEND)
1633 /* Try to find a fg that already contains a matching fte */
1634 list_for_each_entry(iter, match_head, list) {
1635 struct fs_fte *fte_tmp;
1638 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1641 rule = add_rule_fg(g, spec->match_value,
1642 flow_act, dest, dest_num, fte_tmp);
1643 up_write_ref_node(&fte_tmp->node);
1644 tree_put_node(&fte_tmp->node);
1645 kmem_cache_free(steering->ftes_cache, fte);
1650 /* No group with matching fte found, or we skipped the search.
1651 * Try to add a new fte to any matching fg.
1654 /* Check the ft version, for case that new flow group
1655 * was added while the fgs weren't locked
1657 if (atomic_read(&ft->node.version) != ft_version) {
1658 rule = ERR_PTR(-EAGAIN);
1662 /* Check the fgs version, for case the new FTE with the
1663 * same values was added while the fgs weren't locked
1665 if (version != matched_fgs_get_version(match_head)) {
1667 goto search_again_locked;
1670 list_for_each_entry(iter, match_head, list) {
1673 if (!g->node.active)
1676 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1678 err = insert_fte(g, fte);
1680 up_write_ref_node(&g->node);
1683 kmem_cache_free(steering->ftes_cache, fte);
1684 return ERR_PTR(err);
1687 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1688 up_write_ref_node(&g->node);
1689 rule = add_rule_fg(g, spec->match_value,
1690 flow_act, dest, dest_num, fte);
1691 up_write_ref_node(&fte->node);
1692 tree_put_node(&fte->node);
1695 rule = ERR_PTR(-ENOENT);
1697 kmem_cache_free(steering->ftes_cache, fte);
1701 static struct mlx5_flow_handle *
1702 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1703 struct mlx5_flow_spec *spec,
1704 struct mlx5_flow_act *flow_act,
1705 struct mlx5_flow_destination *dest,
1709 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1710 struct mlx5_flow_group *g;
1711 struct mlx5_flow_handle *rule;
1712 struct match_list_head match_head;
1713 bool take_write = false;
1719 if (!check_valid_spec(spec))
1720 return ERR_PTR(-EINVAL);
1722 for (i = 0; i < dest_num; i++) {
1723 if (!dest_is_valid(&dest[i], flow_act->action, ft))
1724 return ERR_PTR(-EINVAL);
1726 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1727 search_again_locked:
1728 version = atomic_read(&ft->node.version);
1730 /* Collect all fgs which has a matching match_criteria */
1731 err = build_match_list(&match_head, ft, spec);
1734 up_write_ref_node(&ft->node);
1736 up_read_ref_node(&ft->node);
1737 return ERR_PTR(err);
1741 up_read_ref_node(&ft->node);
1743 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1745 free_match_list(&match_head);
1746 if (!IS_ERR(rule) ||
1747 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1749 up_write_ref_node(&ft->node);
1754 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1758 if (PTR_ERR(rule) == -EAGAIN ||
1759 version != atomic_read(&ft->node.version))
1760 goto search_again_locked;
1762 g = alloc_auto_flow_group(ft, spec);
1765 up_write_ref_node(&ft->node);
1769 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1770 up_write_ref_node(&ft->node);
1772 err = create_auto_flow_group(ft, g);
1774 goto err_release_fg;
1776 fte = alloc_fte(ft, spec->match_value, flow_act);
1779 goto err_release_fg;
1782 err = insert_fte(g, fte);
1784 kmem_cache_free(steering->ftes_cache, fte);
1785 goto err_release_fg;
1788 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1789 up_write_ref_node(&g->node);
1790 rule = add_rule_fg(g, spec->match_value, flow_act, dest,
1792 up_write_ref_node(&fte->node);
1793 tree_put_node(&fte->node);
1794 tree_put_node(&g->node);
1798 up_write_ref_node(&g->node);
1799 tree_put_node(&g->node);
1800 return ERR_PTR(err);
1803 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1805 return ((ft->type == FS_FT_NIC_RX) &&
1806 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1809 struct mlx5_flow_handle *
1810 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1811 struct mlx5_flow_spec *spec,
1812 struct mlx5_flow_act *flow_act,
1813 struct mlx5_flow_destination *dest,
1816 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1817 struct mlx5_flow_destination gen_dest = {};
1818 struct mlx5_flow_table *next_ft = NULL;
1819 struct mlx5_flow_handle *handle = NULL;
1820 u32 sw_action = flow_act->action;
1821 struct fs_prio *prio;
1823 fs_get_obj(prio, ft->node.parent);
1824 if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1825 if (!fwd_next_prio_supported(ft))
1826 return ERR_PTR(-EOPNOTSUPP);
1828 return ERR_PTR(-EINVAL);
1829 mutex_lock(&root->chain_lock);
1830 next_ft = find_next_chained_ft(prio);
1832 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1833 gen_dest.ft = next_ft;
1836 flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1838 mutex_unlock(&root->chain_lock);
1839 return ERR_PTR(-EOPNOTSUPP);
1843 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1845 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1846 if (!IS_ERR_OR_NULL(handle) &&
1847 (list_empty(&handle->rule[0]->next_ft))) {
1848 mutex_lock(&next_ft->lock);
1849 list_add(&handle->rule[0]->next_ft,
1850 &next_ft->fwd_rules);
1851 mutex_unlock(&next_ft->lock);
1852 handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1854 mutex_unlock(&root->chain_lock);
1858 EXPORT_SYMBOL(mlx5_add_flow_rules);
1860 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1864 for (i = handle->num_rules - 1; i >= 0; i--)
1865 tree_remove_node(&handle->rule[i]->node);
1868 EXPORT_SYMBOL(mlx5_del_flow_rules);
1870 /* Assuming prio->node.children(flow tables) is sorted by level */
1871 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1873 struct fs_prio *prio;
1875 fs_get_obj(prio, ft->node.parent);
1877 if (!list_is_last(&ft->node.list, &prio->node.children))
1878 return list_next_entry(ft, node.list);
1879 return find_next_chained_ft(prio);
1882 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1884 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1885 struct mlx5_ft_underlay_qp *uqp;
1886 struct mlx5_flow_table *new_root_ft = NULL;
1890 if (root->root_ft != ft)
1893 new_root_ft = find_next_ft(ft);
1895 root->root_ft = NULL;
1899 if (list_empty(&root->underlay_qpns)) {
1900 /* Don't set any QPN (zero) in case QPN list is empty */
1902 err = root->cmds->update_root_ft(root->dev, new_root_ft,
1905 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1907 err = root->cmds->update_root_ft(root->dev,
1916 mlx5_core_warn(root->dev,
1917 "Update root flow table of id(%u) qpn(%d) failed\n",
1920 root->root_ft = new_root_ft;
1925 /* Connect flow table from previous priority to
1926 * the next flow table.
1928 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1930 struct mlx5_core_dev *dev = get_dev(&ft->node);
1931 struct mlx5_flow_table *next_ft;
1932 struct fs_prio *prio;
1935 err = update_root_ft_destroy(ft);
1939 fs_get_obj(prio, ft->node.parent);
1940 if (!(list_first_entry(&prio->node.children,
1941 struct mlx5_flow_table,
1945 next_ft = find_next_chained_ft(prio);
1946 err = connect_fwd_rules(dev, next_ft, ft);
1950 err = connect_prev_fts(dev, next_ft, prio);
1952 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1957 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1959 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1962 mutex_lock(&root->chain_lock);
1963 err = disconnect_flow_table(ft);
1965 mutex_unlock(&root->chain_lock);
1968 if (tree_remove_node(&ft->node))
1969 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1971 mutex_unlock(&root->chain_lock);
1975 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1977 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1979 if (tree_remove_node(&fg->node))
1980 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1984 struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
1987 struct mlx5_flow_steering *steering = dev->priv.steering;
1989 if (!steering || !steering->fdb_sub_ns)
1992 return steering->fdb_sub_ns[n];
1994 EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
1996 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1997 enum mlx5_flow_namespace_type type)
1999 struct mlx5_flow_steering *steering = dev->priv.steering;
2000 struct mlx5_flow_root_namespace *root_ns;
2002 struct fs_prio *fs_prio;
2003 struct mlx5_flow_namespace *ns;
2009 case MLX5_FLOW_NAMESPACE_FDB:
2010 if (steering->fdb_root_ns)
2011 return &steering->fdb_root_ns->ns;
2013 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2014 if (steering->sniffer_rx_root_ns)
2015 return &steering->sniffer_rx_root_ns->ns;
2017 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2018 if (steering->sniffer_tx_root_ns)
2019 return &steering->sniffer_tx_root_ns->ns;
2025 if (type == MLX5_FLOW_NAMESPACE_EGRESS) {
2026 root_ns = steering->egress_root_ns;
2027 } else { /* Must be NIC RX */
2028 root_ns = steering->root_ns;
2035 fs_prio = find_prio(&root_ns->ns, prio);
2039 ns = list_first_entry(&fs_prio->node.children,
2045 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2047 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2048 enum mlx5_flow_namespace_type type,
2051 struct mlx5_flow_steering *steering = dev->priv.steering;
2053 if (!steering || vport >= MLX5_TOTAL_VPORTS(dev))
2057 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2058 if (steering->esw_egress_root_ns &&
2059 steering->esw_egress_root_ns[vport])
2060 return &steering->esw_egress_root_ns[vport]->ns;
2063 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2064 if (steering->esw_ingress_root_ns &&
2065 steering->esw_ingress_root_ns[vport])
2066 return &steering->esw_ingress_root_ns[vport]->ns;
2074 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2077 enum fs_node_type type)
2079 struct fs_prio *fs_prio;
2081 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2083 return ERR_PTR(-ENOMEM);
2085 fs_prio->node.type = type;
2086 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2087 tree_add_node(&fs_prio->node, &ns->node);
2088 fs_prio->num_levels = num_levels;
2089 fs_prio->prio = prio;
2090 list_add_tail(&fs_prio->node.list, &ns->node.children);
2095 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2099 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2102 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2103 unsigned int prio, int num_levels)
2105 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2108 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2111 ns->node.type = FS_TYPE_NAMESPACE;
2116 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2118 struct mlx5_flow_namespace *ns;
2120 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2122 return ERR_PTR(-ENOMEM);
2124 fs_init_namespace(ns);
2125 tree_init_node(&ns->node, NULL, del_sw_ns);
2126 tree_add_node(&ns->node, &prio->node);
2127 list_add_tail(&ns->node.list, &prio->node.children);
2132 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2133 struct init_tree_node *prio_metadata)
2135 struct fs_prio *fs_prio;
2138 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2139 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2140 if (IS_ERR(fs_prio))
2141 return PTR_ERR(fs_prio);
2146 #define FLOW_TABLE_BIT_SZ 1
2147 #define GET_FLOW_TABLE_CAP(dev, offset) \
2148 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
2150 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2151 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2155 for (i = 0; i < caps->arr_sz; i++) {
2156 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2162 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2163 struct init_tree_node *init_node,
2164 struct fs_node *fs_parent_node,
2165 struct init_tree_node *init_parent_node,
2168 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2169 flow_table_properties_nic_receive.
2171 struct mlx5_flow_namespace *fs_ns;
2172 struct fs_prio *fs_prio;
2173 struct fs_node *base;
2177 if (init_node->type == FS_TYPE_PRIO) {
2178 if ((init_node->min_ft_level > max_ft_level) ||
2179 !has_required_caps(steering->dev, &init_node->caps))
2182 fs_get_obj(fs_ns, fs_parent_node);
2183 if (init_node->num_leaf_prios)
2184 return create_leaf_prios(fs_ns, prio, init_node);
2185 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2186 if (IS_ERR(fs_prio))
2187 return PTR_ERR(fs_prio);
2188 base = &fs_prio->node;
2189 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2190 fs_get_obj(fs_prio, fs_parent_node);
2191 fs_ns = fs_create_namespace(fs_prio);
2193 return PTR_ERR(fs_ns);
2194 base = &fs_ns->node;
2199 for (i = 0; i < init_node->ar_size; i++) {
2200 err = init_root_tree_recursive(steering, &init_node->children[i],
2201 base, init_node, prio);
2204 if (init_node->children[i].type == FS_TYPE_PRIO &&
2205 init_node->children[i].num_leaf_prios) {
2206 prio += init_node->children[i].num_leaf_prios;
2213 static int init_root_tree(struct mlx5_flow_steering *steering,
2214 struct init_tree_node *init_node,
2215 struct fs_node *fs_parent_node)
2218 struct mlx5_flow_namespace *fs_ns;
2221 fs_get_obj(fs_ns, fs_parent_node);
2222 for (i = 0; i < init_node->ar_size; i++) {
2223 err = init_root_tree_recursive(steering, &init_node->children[i],
2232 static struct mlx5_flow_root_namespace
2233 *create_root_ns(struct mlx5_flow_steering *steering,
2234 enum fs_flow_table_type table_type)
2236 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2237 struct mlx5_flow_root_namespace *root_ns;
2238 struct mlx5_flow_namespace *ns;
2240 if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2241 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2242 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2244 /* Create the root namespace */
2245 root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
2249 root_ns->dev = steering->dev;
2250 root_ns->table_type = table_type;
2251 root_ns->cmds = cmds;
2253 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2256 fs_init_namespace(ns);
2257 mutex_init(&root_ns->chain_lock);
2258 tree_init_node(&ns->node, NULL, NULL);
2259 tree_add_node(&ns->node, NULL);
2264 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2266 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2268 struct fs_prio *prio;
2270 fs_for_each_prio(prio, ns) {
2271 /* This updates prio start_level and num_levels */
2272 set_prio_attrs_in_prio(prio, acc_level);
2273 acc_level += prio->num_levels;
2278 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2280 struct mlx5_flow_namespace *ns;
2281 int acc_level_ns = acc_level;
2283 prio->start_level = acc_level;
2284 fs_for_each_ns(ns, prio)
2285 /* This updates start_level and num_levels of ns's priority descendants */
2286 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2287 if (!prio->num_levels)
2288 prio->num_levels = acc_level_ns - prio->start_level;
2289 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2292 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2294 struct mlx5_flow_namespace *ns = &root_ns->ns;
2295 struct fs_prio *prio;
2296 int start_level = 0;
2298 fs_for_each_prio(prio, ns) {
2299 set_prio_attrs_in_prio(prio, start_level);
2300 start_level += prio->num_levels;
2304 #define ANCHOR_PRIO 0
2305 #define ANCHOR_SIZE 1
2306 #define ANCHOR_LEVEL 0
2307 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2309 struct mlx5_flow_namespace *ns = NULL;
2310 struct mlx5_flow_table_attr ft_attr = {};
2311 struct mlx5_flow_table *ft;
2313 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2317 ft_attr.max_fte = ANCHOR_SIZE;
2318 ft_attr.level = ANCHOR_LEVEL;
2319 ft_attr.prio = ANCHOR_PRIO;
2321 ft = mlx5_create_flow_table(ns, &ft_attr);
2323 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2329 static int init_root_ns(struct mlx5_flow_steering *steering)
2333 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2334 if (!steering->root_ns)
2337 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2341 set_prio_attrs(steering->root_ns);
2342 err = create_anchor_flow_table(steering);
2349 cleanup_root_ns(steering->root_ns);
2350 steering->root_ns = NULL;
2354 static void clean_tree(struct fs_node *node)
2357 struct fs_node *iter;
2358 struct fs_node *temp;
2360 tree_get_node(node);
2361 list_for_each_entry_safe(iter, temp, &node->children, list)
2363 tree_put_node(node);
2364 tree_remove_node(node);
2368 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2373 clean_tree(&root_ns->ns.node);
2376 static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2378 struct mlx5_flow_steering *steering = dev->priv.steering;
2381 if (!steering->esw_egress_root_ns)
2384 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2385 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2387 kfree(steering->esw_egress_root_ns);
2390 static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2392 struct mlx5_flow_steering *steering = dev->priv.steering;
2395 if (!steering->esw_ingress_root_ns)
2398 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2399 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2401 kfree(steering->esw_ingress_root_ns);
2404 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2406 struct mlx5_flow_steering *steering = dev->priv.steering;
2408 cleanup_root_ns(steering->root_ns);
2409 cleanup_egress_acls_root_ns(dev);
2410 cleanup_ingress_acls_root_ns(dev);
2411 cleanup_root_ns(steering->fdb_root_ns);
2412 steering->fdb_root_ns = NULL;
2413 kfree(steering->fdb_sub_ns);
2414 steering->fdb_sub_ns = NULL;
2415 cleanup_root_ns(steering->sniffer_rx_root_ns);
2416 cleanup_root_ns(steering->sniffer_tx_root_ns);
2417 cleanup_root_ns(steering->egress_root_ns);
2418 mlx5_cleanup_fc_stats(dev);
2419 kmem_cache_destroy(steering->ftes_cache);
2420 kmem_cache_destroy(steering->fgs_cache);
2424 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2426 struct fs_prio *prio;
2428 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2429 if (!steering->sniffer_tx_root_ns)
2432 /* Create single prio */
2433 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2435 cleanup_root_ns(steering->sniffer_tx_root_ns);
2436 return PTR_ERR(prio);
2441 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2443 struct fs_prio *prio;
2445 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2446 if (!steering->sniffer_rx_root_ns)
2449 /* Create single prio */
2450 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2452 cleanup_root_ns(steering->sniffer_rx_root_ns);
2453 return PTR_ERR(prio);
2458 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2460 struct mlx5_flow_namespace *ns;
2461 struct fs_prio *maj_prio;
2462 struct fs_prio *min_prio;
2468 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2469 if (!steering->fdb_root_ns)
2472 steering->fdb_sub_ns = kzalloc(sizeof(steering->fdb_sub_ns) *
2473 (FDB_MAX_CHAIN + 1), GFP_KERNEL);
2474 if (!steering->fdb_sub_ns)
2477 levels = 2 * FDB_MAX_PRIO * (FDB_MAX_CHAIN + 1);
2478 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns, 0,
2480 if (IS_ERR(maj_prio)) {
2481 err = PTR_ERR(maj_prio);
2485 for (chain = 0; chain <= FDB_MAX_CHAIN; chain++) {
2486 ns = fs_create_namespace(maj_prio);
2492 for (prio = 0; prio < FDB_MAX_PRIO * (chain + 1); prio++) {
2493 min_prio = fs_create_prio(ns, prio, 2);
2494 if (IS_ERR(min_prio)) {
2495 err = PTR_ERR(min_prio);
2500 steering->fdb_sub_ns[chain] = ns;
2503 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2504 if (IS_ERR(maj_prio)) {
2505 err = PTR_ERR(maj_prio);
2509 set_prio_attrs(steering->fdb_root_ns);
2513 cleanup_root_ns(steering->fdb_root_ns);
2514 kfree(steering->fdb_sub_ns);
2515 steering->fdb_sub_ns = NULL;
2516 steering->fdb_root_ns = NULL;
2520 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2522 struct fs_prio *prio;
2524 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2525 if (!steering->esw_egress_root_ns[vport])
2529 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2530 return PTR_ERR_OR_ZERO(prio);
2533 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2535 struct fs_prio *prio;
2537 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2538 if (!steering->esw_ingress_root_ns[vport])
2542 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2543 return PTR_ERR_OR_ZERO(prio);
2546 static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2548 struct mlx5_flow_steering *steering = dev->priv.steering;
2552 steering->esw_egress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2553 sizeof(*steering->esw_egress_root_ns),
2555 if (!steering->esw_egress_root_ns)
2558 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2559 err = init_egress_acl_root_ns(steering, i);
2561 goto cleanup_root_ns;
2567 for (i--; i >= 0; i--)
2568 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2569 kfree(steering->esw_egress_root_ns);
2573 static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2575 struct mlx5_flow_steering *steering = dev->priv.steering;
2579 steering->esw_ingress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2580 sizeof(*steering->esw_ingress_root_ns),
2582 if (!steering->esw_ingress_root_ns)
2585 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2586 err = init_ingress_acl_root_ns(steering, i);
2588 goto cleanup_root_ns;
2594 for (i--; i >= 0; i--)
2595 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2596 kfree(steering->esw_ingress_root_ns);
2600 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2604 steering->egress_root_ns = create_root_ns(steering,
2606 if (!steering->egress_root_ns)
2609 err = init_root_tree(steering, &egress_root_fs,
2610 &steering->egress_root_ns->ns.node);
2613 set_prio_attrs(steering->egress_root_ns);
2616 cleanup_root_ns(steering->egress_root_ns);
2617 steering->egress_root_ns = NULL;
2621 int mlx5_init_fs(struct mlx5_core_dev *dev)
2623 struct mlx5_flow_steering *steering;
2626 err = mlx5_init_fc_stats(dev);
2630 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2633 steering->dev = dev;
2634 dev->priv.steering = steering;
2636 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2637 sizeof(struct mlx5_flow_group), 0,
2639 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2641 if (!steering->ftes_cache || !steering->fgs_cache) {
2646 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2647 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2648 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2649 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2650 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2651 err = init_root_ns(steering);
2656 if (MLX5_ESWITCH_MANAGER(dev)) {
2657 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2658 err = init_fdb_root_ns(steering);
2662 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2663 err = init_egress_acls_root_ns(dev);
2667 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2668 err = init_ingress_acls_root_ns(dev);
2674 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2675 err = init_sniffer_rx_root_ns(steering);
2680 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2681 err = init_sniffer_tx_root_ns(steering);
2686 if (MLX5_IPSEC_DEV(dev) || MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
2687 err = init_egress_root_ns(steering);
2694 mlx5_cleanup_fs(dev);
2698 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2700 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2701 struct mlx5_ft_underlay_qp *new_uqp;
2704 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2708 mutex_lock(&root->chain_lock);
2710 if (!root->root_ft) {
2712 goto update_ft_fail;
2715 err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2718 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2720 goto update_ft_fail;
2723 new_uqp->qpn = underlay_qpn;
2724 list_add_tail(&new_uqp->list, &root->underlay_qpns);
2726 mutex_unlock(&root->chain_lock);
2731 mutex_unlock(&root->chain_lock);
2735 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2737 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2739 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2740 struct mlx5_ft_underlay_qp *uqp;
2744 mutex_lock(&root->chain_lock);
2745 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2746 if (uqp->qpn == underlay_qpn) {
2753 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2759 err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2762 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2765 list_del(&uqp->list);
2766 mutex_unlock(&root->chain_lock);
2772 mutex_unlock(&root->chain_lock);
2775 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);