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/vport.h>
36 #include <linux/mlx5/eswitch.h>
38 #include "mlx5_core.h"
41 #include "diag/fs_tracepoint.h"
42 #include "accel/ipsec.h"
43 #include "fpga/ipsec.h"
46 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
47 sizeof(struct init_tree_node))
49 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
50 ...) {.type = FS_TYPE_PRIO,\
51 .min_ft_level = min_level_val,\
52 .num_levels = num_levels_val,\
53 .num_leaf_prios = num_prios_val,\
55 .children = (struct init_tree_node[]) {__VA_ARGS__},\
56 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
59 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
60 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
63 #define ADD_NS(def_miss_act, ...) {.type = FS_TYPE_NAMESPACE, \
64 .def_miss_action = def_miss_act,\
65 .children = (struct init_tree_node[]) {__VA_ARGS__},\
66 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
69 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
72 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
74 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
75 .caps = (long[]) {__VA_ARGS__} }
77 #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
78 FS_CAP(flow_table_properties_nic_receive.modify_root), \
79 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
80 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
82 #define FS_CHAINING_CAPS_EGRESS \
84 FS_CAP(flow_table_properties_nic_transmit.flow_modify_en), \
85 FS_CAP(flow_table_properties_nic_transmit.modify_root), \
86 FS_CAP(flow_table_properties_nic_transmit \
87 .identified_miss_table_mode), \
88 FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
90 #define LEFTOVERS_NUM_LEVELS 1
91 #define LEFTOVERS_NUM_PRIOS 1
93 #define BY_PASS_PRIO_NUM_LEVELS 1
94 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
97 #define ETHTOOL_PRIO_NUM_LEVELS 1
98 #define ETHTOOL_NUM_PRIOS 11
99 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
100 /* Vlan, mac, ttc, inner ttc, aRFS */
101 #define KERNEL_NIC_PRIO_NUM_LEVELS 5
102 #define KERNEL_NIC_NUM_PRIOS 1
103 /* One more level for tc */
104 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
106 #define KERNEL_NIC_TC_NUM_PRIOS 1
107 #define KERNEL_NIC_TC_NUM_LEVELS 2
109 #define ANCHOR_NUM_LEVELS 1
110 #define ANCHOR_NUM_PRIOS 1
111 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
113 #define OFFLOADS_MAX_FT 1
114 #define OFFLOADS_NUM_PRIOS 1
115 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
117 #define LAG_PRIO_NUM_LEVELS 1
118 #define LAG_NUM_PRIOS 1
119 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
126 static struct init_tree_node {
127 enum fs_node_type type;
128 struct init_tree_node *children;
130 struct node_caps caps;
135 enum mlx5_flow_table_miss_action def_miss_action;
137 .type = FS_TYPE_NAMESPACE,
139 .children = (struct init_tree_node[]){
140 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
141 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
142 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
143 BY_PASS_PRIO_NUM_LEVELS))),
144 ADD_PRIO(0, LAG_MIN_LEVEL, 0, FS_CHAINING_CAPS,
145 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
146 ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
147 LAG_PRIO_NUM_LEVELS))),
148 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {},
149 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
150 ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS,
152 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0, FS_CHAINING_CAPS,
153 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
154 ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
155 ETHTOOL_PRIO_NUM_LEVELS))),
156 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
157 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
158 ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS,
159 KERNEL_NIC_TC_NUM_LEVELS),
160 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
161 KERNEL_NIC_PRIO_NUM_LEVELS))),
162 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
163 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
164 ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS,
165 LEFTOVERS_NUM_LEVELS))),
166 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
167 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
168 ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS,
169 ANCHOR_NUM_LEVELS))),
173 static struct init_tree_node egress_root_fs = {
174 .type = FS_TYPE_NAMESPACE,
176 .children = (struct init_tree_node[]) {
177 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
178 FS_CHAINING_CAPS_EGRESS,
179 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
180 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
181 BY_PASS_PRIO_NUM_LEVELS))),
185 #define RDMA_RX_BYPASS_PRIO 0
186 #define RDMA_RX_KERNEL_PRIO 1
187 static struct init_tree_node rdma_rx_root_fs = {
188 .type = FS_TYPE_NAMESPACE,
190 .children = (struct init_tree_node[]) {
191 [RDMA_RX_BYPASS_PRIO] =
192 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS, 0,
194 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
195 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_REGULAR_PRIOS,
196 BY_PASS_PRIO_NUM_LEVELS))),
197 [RDMA_RX_KERNEL_PRIO] =
198 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS + 1, 0,
200 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_SWITCH_DOMAIN,
201 ADD_MULTIPLE_PRIO(1, 1))),
205 enum fs_i_lock_class {
211 static const struct rhashtable_params rhash_fte = {
212 .key_len = FIELD_SIZEOF(struct fs_fte, val),
213 .key_offset = offsetof(struct fs_fte, val),
214 .head_offset = offsetof(struct fs_fte, hash),
215 .automatic_shrinking = true,
219 static const struct rhashtable_params rhash_fg = {
220 .key_len = FIELD_SIZEOF(struct mlx5_flow_group, mask),
221 .key_offset = offsetof(struct mlx5_flow_group, mask),
222 .head_offset = offsetof(struct mlx5_flow_group, hash),
223 .automatic_shrinking = true,
228 static void del_hw_flow_table(struct fs_node *node);
229 static void del_hw_flow_group(struct fs_node *node);
230 static void del_hw_fte(struct fs_node *node);
231 static void del_sw_flow_table(struct fs_node *node);
232 static void del_sw_flow_group(struct fs_node *node);
233 static void del_sw_fte(struct fs_node *node);
234 static void del_sw_prio(struct fs_node *node);
235 static void del_sw_ns(struct fs_node *node);
236 /* Delete rule (destination) is special case that
237 * requires to lock the FTE for all the deletion process.
239 static void del_sw_hw_rule(struct fs_node *node);
240 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
241 struct mlx5_flow_destination *d2);
242 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
243 static struct mlx5_flow_rule *
244 find_flow_rule(struct fs_fte *fte,
245 struct mlx5_flow_destination *dest);
247 static void tree_init_node(struct fs_node *node,
248 void (*del_hw_func)(struct fs_node *),
249 void (*del_sw_func)(struct fs_node *))
251 refcount_set(&node->refcount, 1);
252 INIT_LIST_HEAD(&node->list);
253 INIT_LIST_HEAD(&node->children);
254 init_rwsem(&node->lock);
255 node->del_hw_func = del_hw_func;
256 node->del_sw_func = del_sw_func;
257 node->active = false;
260 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
263 refcount_inc(&parent->refcount);
264 node->parent = parent;
266 /* Parent is the root */
270 node->root = parent->root;
273 static int tree_get_node(struct fs_node *node)
275 return refcount_inc_not_zero(&node->refcount);
278 static void nested_down_read_ref_node(struct fs_node *node,
279 enum fs_i_lock_class class)
282 down_read_nested(&node->lock, class);
283 refcount_inc(&node->refcount);
287 static void nested_down_write_ref_node(struct fs_node *node,
288 enum fs_i_lock_class class)
291 down_write_nested(&node->lock, class);
292 refcount_inc(&node->refcount);
296 static void down_write_ref_node(struct fs_node *node, bool locked)
300 down_write(&node->lock);
301 refcount_inc(&node->refcount);
305 static void up_read_ref_node(struct fs_node *node)
307 refcount_dec(&node->refcount);
308 up_read(&node->lock);
311 static void up_write_ref_node(struct fs_node *node, bool locked)
313 refcount_dec(&node->refcount);
315 up_write(&node->lock);
318 static void tree_put_node(struct fs_node *node, bool locked)
320 struct fs_node *parent_node = node->parent;
322 if (refcount_dec_and_test(&node->refcount)) {
323 if (node->del_hw_func)
324 node->del_hw_func(node);
326 /* Only root namespace doesn't have parent and we just
327 * need to free its node.
329 down_write_ref_node(parent_node, locked);
330 list_del_init(&node->list);
331 if (node->del_sw_func)
332 node->del_sw_func(node);
333 up_write_ref_node(parent_node, locked);
339 if (!node && parent_node)
340 tree_put_node(parent_node, locked);
343 static int tree_remove_node(struct fs_node *node, bool locked)
345 if (refcount_read(&node->refcount) > 1) {
346 refcount_dec(&node->refcount);
349 tree_put_node(node, locked);
353 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
356 struct fs_prio *iter_prio;
358 fs_for_each_prio(iter_prio, ns) {
359 if (iter_prio->prio == prio)
366 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
370 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
371 if (spec->match_value[i] & ~spec->match_criteria[i]) {
372 pr_warn("mlx5_core: match_value differs from match_criteria\n");
379 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
381 struct fs_node *root;
382 struct mlx5_flow_namespace *ns;
386 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
387 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
391 ns = container_of(root, struct mlx5_flow_namespace, node);
392 return container_of(ns, struct mlx5_flow_root_namespace, ns);
395 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
397 struct mlx5_flow_root_namespace *root = find_root(node);
400 return root->dev->priv.steering;
404 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
406 struct mlx5_flow_root_namespace *root = find_root(node);
413 static void del_sw_ns(struct fs_node *node)
418 static void del_sw_prio(struct fs_node *node)
423 static void del_hw_flow_table(struct fs_node *node)
425 struct mlx5_flow_root_namespace *root;
426 struct mlx5_flow_table *ft;
427 struct mlx5_core_dev *dev;
430 fs_get_obj(ft, node);
431 dev = get_dev(&ft->node);
432 root = find_root(&ft->node);
433 trace_mlx5_fs_del_ft(ft);
436 err = root->cmds->destroy_flow_table(root, ft);
438 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
442 static void del_sw_flow_table(struct fs_node *node)
444 struct mlx5_flow_table *ft;
445 struct fs_prio *prio;
447 fs_get_obj(ft, node);
449 rhltable_destroy(&ft->fgs_hash);
450 fs_get_obj(prio, ft->node.parent);
455 static void modify_fte(struct fs_fte *fte)
457 struct mlx5_flow_root_namespace *root;
458 struct mlx5_flow_table *ft;
459 struct mlx5_flow_group *fg;
460 struct mlx5_core_dev *dev;
463 fs_get_obj(fg, fte->node.parent);
464 fs_get_obj(ft, fg->node.parent);
465 dev = get_dev(&fte->node);
467 root = find_root(&ft->node);
468 err = root->cmds->update_fte(root, ft, fg, fte->modify_mask, fte);
471 "%s can't del rule fg id=%d fte_index=%d\n",
472 __func__, fg->id, fte->index);
473 fte->modify_mask = 0;
476 static void del_sw_hw_rule(struct fs_node *node)
478 struct mlx5_flow_rule *rule;
481 fs_get_obj(rule, node);
482 fs_get_obj(fte, rule->node.parent);
483 trace_mlx5_fs_del_rule(rule);
484 if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
485 mutex_lock(&rule->dest_attr.ft->lock);
486 list_del(&rule->next_ft);
487 mutex_unlock(&rule->dest_attr.ft->lock);
490 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
493 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
494 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
495 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
499 if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
502 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
508 static void del_hw_fte(struct fs_node *node)
510 struct mlx5_flow_root_namespace *root;
511 struct mlx5_flow_table *ft;
512 struct mlx5_flow_group *fg;
513 struct mlx5_core_dev *dev;
517 fs_get_obj(fte, node);
518 fs_get_obj(fg, fte->node.parent);
519 fs_get_obj(ft, fg->node.parent);
521 trace_mlx5_fs_del_fte(fte);
522 dev = get_dev(&ft->node);
523 root = find_root(&ft->node);
525 err = root->cmds->delete_fte(root, ft, fte);
528 "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(root, ft, fg))
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 && fg->max_ftes == ft->autogroup.group_size)
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,
617 const struct mlx5_flow_spec *spec,
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, &spec->match_value, sizeof(fte->val));
628 fte->node.type = FS_TYPE_FLOW_ENTRY;
629 fte->action = *flow_act;
630 fte->flow_context = spec->flow_context;
632 tree_init_node(&fte->node, NULL, del_sw_fte);
637 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
638 struct mlx5_flow_group *fg)
640 rhashtable_destroy(&fg->ftes_hash);
641 kmem_cache_free(steering->fgs_cache, fg);
644 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
645 u8 match_criteria_enable,
646 const void *match_criteria,
650 struct mlx5_flow_group *fg;
653 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
655 return ERR_PTR(-ENOMEM);
657 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
659 kmem_cache_free(steering->fgs_cache, fg);
663 ida_init(&fg->fte_allocator);
664 fg->mask.match_criteria_enable = match_criteria_enable;
665 memcpy(&fg->mask.match_criteria, match_criteria,
666 sizeof(fg->mask.match_criteria));
667 fg->node.type = FS_TYPE_FLOW_GROUP;
668 fg->start_index = start_index;
669 fg->max_ftes = end_index - start_index + 1;
674 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
675 u8 match_criteria_enable,
676 const void *match_criteria,
679 struct list_head *prev)
681 struct mlx5_flow_steering *steering = get_steering(&ft->node);
682 struct mlx5_flow_group *fg;
685 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
686 start_index, end_index);
690 /* initialize refcnt, add to parent list */
691 ret = rhltable_insert(&ft->fgs_hash,
695 dealloc_flow_group(steering, fg);
699 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
700 tree_add_node(&fg->node, &ft->node);
701 /* Add node to group list */
702 list_add(&fg->node.list, prev);
703 atomic_inc(&ft->node.version);
708 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
709 enum fs_flow_table_type table_type,
710 enum fs_flow_table_op_mod op_mod,
713 struct mlx5_flow_table *ft;
716 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
718 return ERR_PTR(-ENOMEM);
720 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
727 ft->node.type = FS_TYPE_FLOW_TABLE;
729 ft->type = table_type;
731 ft->max_fte = max_fte;
733 INIT_LIST_HEAD(&ft->fwd_rules);
734 mutex_init(&ft->lock);
739 /* If reverse is false, then we search for the first flow table in the
740 * root sub-tree from start(closest from right), else we search for the
741 * last flow table in the root sub-tree till start(closest from left).
743 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
744 struct list_head *start,
747 #define list_advance_entry(pos, reverse) \
748 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
750 #define list_for_each_advance_continue(pos, head, reverse) \
751 for (pos = list_advance_entry(pos, reverse); \
752 &pos->list != (head); \
753 pos = list_advance_entry(pos, reverse))
755 struct fs_node *iter = list_entry(start, struct fs_node, list);
756 struct mlx5_flow_table *ft = NULL;
758 if (!root || root->type == FS_TYPE_PRIO_CHAINS)
761 list_for_each_advance_continue(iter, &root->children, reverse) {
762 if (iter->type == FS_TYPE_FLOW_TABLE) {
763 fs_get_obj(ft, iter);
766 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
774 /* If reverse if false then return the first flow table in next priority of
775 * prio in the tree, else return the last flow table in the previous priority
776 * of prio in the tree.
778 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
780 struct mlx5_flow_table *ft = NULL;
781 struct fs_node *curr_node;
782 struct fs_node *parent;
784 parent = prio->node.parent;
785 curr_node = &prio->node;
786 while (!ft && parent) {
787 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
789 parent = curr_node->parent;
794 /* Assuming all the tree is locked by mutex chain lock */
795 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
797 return find_closest_ft(prio, false);
800 /* Assuming all the tree is locked by mutex chain lock */
801 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
803 return find_closest_ft(prio, true);
806 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
807 struct fs_prio *prio,
808 struct mlx5_flow_table *ft)
810 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
811 struct mlx5_flow_table *iter;
815 fs_for_each_ft(iter, prio) {
817 err = root->cmds->modify_flow_table(root, iter, ft);
819 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
821 /* The driver is out of sync with the FW */
830 /* Connect flow tables from previous priority of prio to ft */
831 static int connect_prev_fts(struct mlx5_core_dev *dev,
832 struct mlx5_flow_table *ft,
833 struct fs_prio *prio)
835 struct mlx5_flow_table *prev_ft;
837 prev_ft = find_prev_chained_ft(prio);
839 struct fs_prio *prev_prio;
841 fs_get_obj(prev_prio, prev_ft->node.parent);
842 return connect_fts_in_prio(dev, prev_prio, ft);
847 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
850 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
851 struct mlx5_ft_underlay_qp *uqp;
852 int min_level = INT_MAX;
857 min_level = root->root_ft->level;
859 if (ft->level >= min_level)
862 if (list_empty(&root->underlay_qpns)) {
863 /* Don't set any QPN (zero) in case QPN list is empty */
865 err = root->cmds->update_root_ft(root, ft, qpn, false);
867 list_for_each_entry(uqp, &root->underlay_qpns, list) {
869 err = root->cmds->update_root_ft(root, ft,
877 mlx5_core_warn(root->dev,
878 "Update root flow table of id(%u) qpn(%d) failed\n",
886 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
887 struct mlx5_flow_destination *dest)
889 struct mlx5_flow_root_namespace *root;
890 struct mlx5_flow_table *ft;
891 struct mlx5_flow_group *fg;
893 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
896 fs_get_obj(fte, rule->node.parent);
897 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
899 down_write_ref_node(&fte->node, false);
900 fs_get_obj(fg, fte->node.parent);
901 fs_get_obj(ft, fg->node.parent);
903 memcpy(&rule->dest_attr, dest, sizeof(*dest));
904 root = find_root(&ft->node);
905 err = root->cmds->update_fte(root, ft, fg,
907 up_write_ref_node(&fte->node, false);
912 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
913 struct mlx5_flow_destination *new_dest,
914 struct mlx5_flow_destination *old_dest)
919 if (handle->num_rules != 1)
921 return _mlx5_modify_rule_destination(handle->rule[0],
925 for (i = 0; i < handle->num_rules; i++) {
926 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
927 return _mlx5_modify_rule_destination(handle->rule[i],
934 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
935 static int connect_fwd_rules(struct mlx5_core_dev *dev,
936 struct mlx5_flow_table *new_next_ft,
937 struct mlx5_flow_table *old_next_ft)
939 struct mlx5_flow_destination dest = {};
940 struct mlx5_flow_rule *iter;
943 /* new_next_ft and old_next_ft could be NULL only
944 * when we create/destroy the anchor flow table.
946 if (!new_next_ft || !old_next_ft)
949 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
950 dest.ft = new_next_ft;
952 mutex_lock(&old_next_ft->lock);
953 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
954 mutex_unlock(&old_next_ft->lock);
955 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
956 err = _mlx5_modify_rule_destination(iter, &dest);
958 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
964 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
965 struct fs_prio *prio)
967 struct mlx5_flow_table *next_ft;
970 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
972 if (list_empty(&prio->node.children)) {
973 err = connect_prev_fts(dev, ft, prio);
977 next_ft = find_next_chained_ft(prio);
978 err = connect_fwd_rules(dev, ft, next_ft);
983 if (MLX5_CAP_FLOWTABLE(dev,
984 flow_table_properties_nic_receive.modify_root))
985 err = update_root_ft_create(ft, prio);
989 static void list_add_flow_table(struct mlx5_flow_table *ft,
990 struct fs_prio *prio)
992 struct list_head *prev = &prio->node.children;
993 struct mlx5_flow_table *iter;
995 fs_for_each_ft(iter, prio) {
996 if (iter->level > ft->level)
998 prev = &iter->node.list;
1000 list_add(&ft->node.list, prev);
1003 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1004 struct mlx5_flow_table_attr *ft_attr,
1005 enum fs_flow_table_op_mod op_mod,
1008 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
1009 struct mlx5_flow_table *next_ft = NULL;
1010 struct fs_prio *fs_prio = NULL;
1011 struct mlx5_flow_table *ft;
1016 pr_err("mlx5: flow steering failed to find root of namespace\n");
1017 return ERR_PTR(-ENODEV);
1020 mutex_lock(&root->chain_lock);
1021 fs_prio = find_prio(ns, ft_attr->prio);
1026 if (ft_attr->level >= fs_prio->num_levels) {
1030 /* The level is related to the
1031 * priority level range.
1033 ft_attr->level += fs_prio->start_level;
1034 ft = alloc_flow_table(ft_attr->level,
1036 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
1038 op_mod, ft_attr->flags);
1044 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1045 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
1046 next_ft = find_next_chained_ft(fs_prio);
1047 ft->def_miss_action = ns->def_miss_action;
1048 err = root->cmds->create_flow_table(root, ft, log_table_sz, next_ft);
1052 err = connect_flow_table(root->dev, ft, fs_prio);
1055 ft->node.active = true;
1056 down_write_ref_node(&fs_prio->node, false);
1057 tree_add_node(&ft->node, &fs_prio->node);
1058 list_add_flow_table(ft, fs_prio);
1060 up_write_ref_node(&fs_prio->node, false);
1061 mutex_unlock(&root->chain_lock);
1062 trace_mlx5_fs_add_ft(ft);
1065 root->cmds->destroy_flow_table(root, ft);
1069 mutex_unlock(&root->chain_lock);
1070 return ERR_PTR(err);
1073 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1074 struct mlx5_flow_table_attr *ft_attr)
1076 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1079 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1080 int prio, int max_fte,
1081 u32 level, u16 vport)
1083 struct mlx5_flow_table_attr ft_attr = {};
1085 ft_attr.max_fte = max_fte;
1086 ft_attr.level = level;
1087 ft_attr.prio = prio;
1089 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1092 struct mlx5_flow_table*
1093 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1094 int prio, u32 level)
1096 struct mlx5_flow_table_attr ft_attr = {};
1098 ft_attr.level = level;
1099 ft_attr.prio = prio;
1100 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1102 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1104 struct mlx5_flow_table*
1105 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1107 int num_flow_table_entries,
1112 struct mlx5_flow_table_attr ft_attr = {};
1113 struct mlx5_flow_table *ft;
1115 if (max_num_groups > num_flow_table_entries)
1116 return ERR_PTR(-EINVAL);
1118 ft_attr.max_fte = num_flow_table_entries;
1119 ft_attr.prio = prio;
1120 ft_attr.level = level;
1121 ft_attr.flags = flags;
1123 ft = mlx5_create_flow_table(ns, &ft_attr);
1127 ft->autogroup.active = true;
1128 ft->autogroup.required_groups = max_num_groups;
1129 /* We save place for flow groups in addition to max types */
1130 ft->autogroup.group_size = ft->max_fte / (max_num_groups + 1);
1134 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1136 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1139 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1140 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1141 fg_in, match_criteria);
1142 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1144 match_criteria_enable);
1145 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1147 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1149 struct mlx5_flow_group *fg;
1152 if (ft->autogroup.active)
1153 return ERR_PTR(-EPERM);
1155 down_write_ref_node(&ft->node, false);
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, false);
1163 err = root->cmds->create_flow_group(root, ft, fg_in, fg);
1165 tree_put_node(&fg->node, false);
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(struct_size(handle, rule, num_rules), GFP_KERNEL);
1198 handle->num_rules = num_rules;
1203 static void destroy_flow_handle(struct fs_fte *fte,
1204 struct mlx5_flow_handle *handle,
1205 struct mlx5_flow_destination *dest,
1209 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1211 list_del(&handle->rule[i]->node.list);
1212 kfree(handle->rule[i]);
1218 static struct mlx5_flow_handle *
1219 create_flow_handle(struct fs_fte *fte,
1220 struct mlx5_flow_destination *dest,
1225 struct mlx5_flow_handle *handle;
1226 struct mlx5_flow_rule *rule = NULL;
1227 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1228 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1232 handle = alloc_handle((dest_num) ? dest_num : 1);
1234 return ERR_PTR(-ENOMEM);
1238 rule = find_flow_rule(fte, dest + i);
1240 refcount_inc(&rule->node.refcount);
1246 rule = alloc_rule(dest + i);
1250 /* Add dest to dests list- we need flow tables to be in the
1251 * end of the list for forward to next prio rules.
1253 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1255 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1256 list_add(&rule->node.list, &fte->node.children);
1258 list_add_tail(&rule->node.list, &fte->node.children);
1262 type = dest[i].type ==
1263 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1264 *modify_mask |= type ? count : dst;
1267 handle->rule[i] = rule;
1268 } while (++i < dest_num);
1273 destroy_flow_handle(fte, handle, dest, i);
1274 return ERR_PTR(-ENOMEM);
1277 /* fte should not be deleted while calling this function */
1278 static struct mlx5_flow_handle *
1279 add_rule_fte(struct fs_fte *fte,
1280 struct mlx5_flow_group *fg,
1281 struct mlx5_flow_destination *dest,
1285 struct mlx5_flow_root_namespace *root;
1286 struct mlx5_flow_handle *handle;
1287 struct mlx5_flow_table *ft;
1288 int modify_mask = 0;
1290 bool new_rule = false;
1292 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1294 if (IS_ERR(handle) || !new_rule)
1298 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1300 fs_get_obj(ft, fg->node.parent);
1301 root = find_root(&fg->node);
1302 if (!(fte->status & FS_FTE_STATUS_EXISTING))
1303 err = root->cmds->create_fte(root, ft, fg, fte);
1305 err = root->cmds->update_fte(root, ft, fg, modify_mask, fte);
1309 fte->node.active = true;
1310 fte->status |= FS_FTE_STATUS_EXISTING;
1311 atomic_inc(&fte->node.version);
1317 destroy_flow_handle(fte, handle, dest, handle->num_rules);
1318 return ERR_PTR(err);
1321 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
1322 const struct mlx5_flow_spec *spec)
1324 struct list_head *prev = &ft->node.children;
1325 struct mlx5_flow_group *fg;
1326 unsigned int candidate_index = 0;
1327 unsigned int group_size = 0;
1329 if (!ft->autogroup.active)
1330 return ERR_PTR(-ENOENT);
1332 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1333 group_size = ft->autogroup.group_size;
1335 /* ft->max_fte == ft->autogroup.max_types */
1336 if (group_size == 0)
1339 /* sorted by start_index */
1340 fs_for_each_fg(fg, ft) {
1341 if (candidate_index + group_size > fg->start_index)
1342 candidate_index = fg->start_index + fg->max_ftes;
1345 prev = &fg->node.list;
1348 if (candidate_index + group_size > ft->max_fte)
1349 return ERR_PTR(-ENOSPC);
1351 fg = alloc_insert_flow_group(ft,
1352 spec->match_criteria_enable,
1353 spec->match_criteria,
1355 candidate_index + group_size - 1,
1360 if (group_size == ft->autogroup.group_size)
1361 ft->autogroup.num_groups++;
1367 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1368 struct mlx5_flow_group *fg)
1370 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1371 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1372 void *match_criteria_addr;
1373 u8 src_esw_owner_mask_on;
1378 in = kvzalloc(inlen, GFP_KERNEL);
1382 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1383 fg->mask.match_criteria_enable);
1384 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1385 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1388 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1390 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1391 source_eswitch_owner_vhca_id);
1392 MLX5_SET(create_flow_group_in, in,
1393 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1395 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1396 in, match_criteria);
1397 memcpy(match_criteria_addr, fg->mask.match_criteria,
1398 sizeof(fg->mask.match_criteria));
1400 err = root->cmds->create_flow_group(root, ft, in, fg);
1402 fg->node.active = true;
1403 trace_mlx5_fs_add_fg(fg);
1410 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1411 struct mlx5_flow_destination *d2)
1413 if (d1->type == d2->type) {
1414 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1415 d1->vport.num == d2->vport.num &&
1416 d1->vport.flags == d2->vport.flags &&
1417 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ?
1418 (d1->vport.vhca_id == d2->vport.vhca_id) : true) &&
1419 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
1420 (d1->vport.pkt_reformat->id ==
1421 d2->vport.pkt_reformat->id) : true)) ||
1422 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1423 d1->ft == d2->ft) ||
1424 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1425 d1->tir_num == d2->tir_num) ||
1426 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1427 d1->ft_num == d2->ft_num))
1434 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1435 struct mlx5_flow_destination *dest)
1437 struct mlx5_flow_rule *rule;
1439 list_for_each_entry(rule, &fte->node.children, node.list) {
1440 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1446 static bool check_conflicting_actions(u32 action1, u32 action2)
1448 u32 xored_actions = action1 ^ action2;
1450 /* if one rule only wants to count, it's ok */
1451 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1452 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1455 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1456 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
1457 MLX5_FLOW_CONTEXT_ACTION_DECAP |
1458 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1459 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1460 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1461 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1462 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
1468 static int check_conflicting_ftes(struct fs_fte *fte,
1469 const struct mlx5_flow_context *flow_context,
1470 const struct mlx5_flow_act *flow_act)
1472 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
1473 mlx5_core_warn(get_dev(&fte->node),
1474 "Found two FTEs with conflicting actions\n");
1478 if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) &&
1479 fte->flow_context.flow_tag != flow_context->flow_tag) {
1480 mlx5_core_warn(get_dev(&fte->node),
1481 "FTE flow tag %u already exists with different flow tag %u\n",
1482 fte->flow_context.flow_tag,
1483 flow_context->flow_tag);
1490 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1491 const struct mlx5_flow_spec *spec,
1492 struct mlx5_flow_act *flow_act,
1493 struct mlx5_flow_destination *dest,
1497 struct mlx5_flow_handle *handle;
1502 ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act);
1504 return ERR_PTR(ret);
1506 old_action = fte->action.action;
1507 fte->action.action |= flow_act->action;
1508 handle = add_rule_fte(fte, fg, dest, dest_num,
1509 old_action != flow_act->action);
1510 if (IS_ERR(handle)) {
1511 fte->action.action = old_action;
1514 trace_mlx5_fs_set_fte(fte, false);
1516 for (i = 0; i < handle->num_rules; i++) {
1517 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1518 tree_add_node(&handle->rule[i]->node, &fte->node);
1519 trace_mlx5_fs_add_rule(handle->rule[i]);
1525 static bool counter_is_valid(u32 action)
1527 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1528 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1531 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1533 struct mlx5_flow_table *ft)
1535 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1536 return counter_is_valid(action);
1538 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1541 if (!dest || ((dest->type ==
1542 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1543 (dest->ft->level <= ft->level)))
1549 struct list_head list;
1550 struct mlx5_flow_group *g;
1553 struct match_list_head {
1554 struct list_head list;
1555 struct match_list first;
1558 static void free_match_list(struct match_list_head *head, bool ft_locked)
1560 if (!list_empty(&head->list)) {
1561 struct match_list *iter, *match_tmp;
1563 list_del(&head->first.list);
1564 tree_put_node(&head->first.g->node, ft_locked);
1565 list_for_each_entry_safe(iter, match_tmp, &head->list,
1567 tree_put_node(&iter->g->node, ft_locked);
1568 list_del(&iter->list);
1574 static int build_match_list(struct match_list_head *match_head,
1575 struct mlx5_flow_table *ft,
1576 const struct mlx5_flow_spec *spec,
1579 struct rhlist_head *tmp, *list;
1580 struct mlx5_flow_group *g;
1584 INIT_LIST_HEAD(&match_head->list);
1585 /* Collect all fgs which has a matching match_criteria */
1586 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1587 /* RCU is atomic, we can't execute FW commands here */
1588 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1589 struct match_list *curr_match;
1591 if (likely(list_empty(&match_head->list))) {
1592 if (!tree_get_node(&g->node))
1594 match_head->first.g = g;
1595 list_add_tail(&match_head->first.list,
1600 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1602 free_match_list(match_head, ft_locked);
1606 if (!tree_get_node(&g->node)) {
1611 list_add_tail(&curr_match->list, &match_head->list);
1618 static u64 matched_fgs_get_version(struct list_head *match_head)
1620 struct match_list *iter;
1623 list_for_each_entry(iter, match_head, list)
1624 version += (u64)atomic_read(&iter->g->node.version);
1628 static struct fs_fte *
1629 lookup_fte_locked(struct mlx5_flow_group *g,
1630 const u32 *match_value,
1633 struct fs_fte *fte_tmp;
1636 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1638 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
1639 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
1641 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1645 if (!fte_tmp->node.active) {
1646 tree_put_node(&fte_tmp->node, false);
1651 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1654 up_write_ref_node(&g->node, false);
1656 up_read_ref_node(&g->node);
1660 static struct mlx5_flow_handle *
1661 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1662 struct list_head *match_head,
1663 const struct mlx5_flow_spec *spec,
1664 struct mlx5_flow_act *flow_act,
1665 struct mlx5_flow_destination *dest,
1669 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1670 struct mlx5_flow_group *g;
1671 struct mlx5_flow_handle *rule;
1672 struct match_list *iter;
1673 bool take_write = false;
1678 fte = alloc_fte(ft, spec, flow_act);
1680 return ERR_PTR(-ENOMEM);
1682 search_again_locked:
1683 version = matched_fgs_get_version(match_head);
1684 if (flow_act->flags & FLOW_ACT_NO_APPEND)
1686 /* Try to find a fg that already contains a matching fte */
1687 list_for_each_entry(iter, match_head, list) {
1688 struct fs_fte *fte_tmp;
1691 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1694 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp);
1695 up_write_ref_node(&fte_tmp->node, false);
1696 tree_put_node(&fte_tmp->node, false);
1697 kmem_cache_free(steering->ftes_cache, fte);
1702 /* No group with matching fte found, or we skipped the search.
1703 * Try to add a new fte to any matching fg.
1706 /* Check the ft version, for case that new flow group
1707 * was added while the fgs weren't locked
1709 if (atomic_read(&ft->node.version) != ft_version) {
1710 rule = ERR_PTR(-EAGAIN);
1714 /* Check the fgs version, for case the new FTE with the
1715 * same values was added while the fgs weren't locked
1717 if (version != matched_fgs_get_version(match_head)) {
1719 goto search_again_locked;
1722 list_for_each_entry(iter, match_head, list) {
1725 if (!g->node.active)
1728 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1730 err = insert_fte(g, fte);
1732 up_write_ref_node(&g->node, false);
1735 kmem_cache_free(steering->ftes_cache, fte);
1736 return ERR_PTR(err);
1739 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1740 up_write_ref_node(&g->node, false);
1741 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
1742 up_write_ref_node(&fte->node, false);
1743 tree_put_node(&fte->node, false);
1746 rule = ERR_PTR(-ENOENT);
1748 kmem_cache_free(steering->ftes_cache, fte);
1752 static struct mlx5_flow_handle *
1753 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1754 const struct mlx5_flow_spec *spec,
1755 struct mlx5_flow_act *flow_act,
1756 struct mlx5_flow_destination *dest,
1760 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1761 struct mlx5_flow_group *g;
1762 struct mlx5_flow_handle *rule;
1763 struct match_list_head match_head;
1764 bool take_write = false;
1770 if (!check_valid_spec(spec))
1771 return ERR_PTR(-EINVAL);
1773 for (i = 0; i < dest_num; i++) {
1774 if (!dest_is_valid(&dest[i], flow_act->action, ft))
1775 return ERR_PTR(-EINVAL);
1777 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1778 search_again_locked:
1779 version = atomic_read(&ft->node.version);
1781 /* Collect all fgs which has a matching match_criteria */
1782 err = build_match_list(&match_head, ft, spec, take_write);
1785 up_write_ref_node(&ft->node, false);
1787 up_read_ref_node(&ft->node);
1788 return ERR_PTR(err);
1792 up_read_ref_node(&ft->node);
1794 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1796 free_match_list(&match_head, take_write);
1797 if (!IS_ERR(rule) ||
1798 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1800 up_write_ref_node(&ft->node, false);
1805 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1809 if (PTR_ERR(rule) == -EAGAIN ||
1810 version != atomic_read(&ft->node.version))
1811 goto search_again_locked;
1813 g = alloc_auto_flow_group(ft, spec);
1816 up_write_ref_node(&ft->node, false);
1820 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1821 up_write_ref_node(&ft->node, false);
1823 err = create_auto_flow_group(ft, g);
1825 goto err_release_fg;
1827 fte = alloc_fte(ft, spec, flow_act);
1830 goto err_release_fg;
1833 err = insert_fte(g, fte);
1835 kmem_cache_free(steering->ftes_cache, fte);
1836 goto err_release_fg;
1839 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1840 up_write_ref_node(&g->node, false);
1841 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
1842 up_write_ref_node(&fte->node, false);
1843 tree_put_node(&fte->node, false);
1844 tree_put_node(&g->node, false);
1848 up_write_ref_node(&g->node, false);
1849 tree_put_node(&g->node, false);
1850 return ERR_PTR(err);
1853 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1855 return ((ft->type == FS_FT_NIC_RX) &&
1856 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1859 struct mlx5_flow_handle *
1860 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1861 const struct mlx5_flow_spec *spec,
1862 struct mlx5_flow_act *flow_act,
1863 struct mlx5_flow_destination *dest,
1866 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1867 struct mlx5_flow_destination gen_dest = {};
1868 struct mlx5_flow_table *next_ft = NULL;
1869 struct mlx5_flow_handle *handle = NULL;
1870 u32 sw_action = flow_act->action;
1871 struct fs_prio *prio;
1873 fs_get_obj(prio, ft->node.parent);
1874 if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1875 if (!fwd_next_prio_supported(ft))
1876 return ERR_PTR(-EOPNOTSUPP);
1878 return ERR_PTR(-EINVAL);
1879 mutex_lock(&root->chain_lock);
1880 next_ft = find_next_chained_ft(prio);
1882 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1883 gen_dest.ft = next_ft;
1886 flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1888 mutex_unlock(&root->chain_lock);
1889 return ERR_PTR(-EOPNOTSUPP);
1893 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1895 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1896 if (!IS_ERR_OR_NULL(handle) &&
1897 (list_empty(&handle->rule[0]->next_ft))) {
1898 mutex_lock(&next_ft->lock);
1899 list_add(&handle->rule[0]->next_ft,
1900 &next_ft->fwd_rules);
1901 mutex_unlock(&next_ft->lock);
1902 handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1904 mutex_unlock(&root->chain_lock);
1908 EXPORT_SYMBOL(mlx5_add_flow_rules);
1910 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1915 /* In order to consolidate the HW changes we lock the FTE for other
1916 * changes, and increase its refcount, in order not to perform the
1917 * "del" functions of the FTE. Will handle them here.
1918 * The removal of the rules is done under locked FTE.
1919 * After removing all the handle's rules, if there are remaining
1920 * rules, it means we just need to modify the FTE in FW, and
1921 * unlock/decrease the refcount we increased before.
1922 * Otherwise, it means the FTE should be deleted. First delete the
1923 * FTE in FW. Then, unlock the FTE, and proceed the tree_put_node of
1924 * the FTE, which will handle the last decrease of the refcount, as
1925 * well as required handling of its parent.
1927 fs_get_obj(fte, handle->rule[0]->node.parent);
1928 down_write_ref_node(&fte->node, false);
1929 for (i = handle->num_rules - 1; i >= 0; i--)
1930 tree_remove_node(&handle->rule[i]->node, true);
1931 if (fte->modify_mask && fte->dests_size) {
1933 up_write_ref_node(&fte->node, false);
1935 del_hw_fte(&fte->node);
1936 up_write(&fte->node.lock);
1937 tree_put_node(&fte->node, false);
1941 EXPORT_SYMBOL(mlx5_del_flow_rules);
1943 /* Assuming prio->node.children(flow tables) is sorted by level */
1944 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1946 struct fs_prio *prio;
1948 fs_get_obj(prio, ft->node.parent);
1950 if (!list_is_last(&ft->node.list, &prio->node.children))
1951 return list_next_entry(ft, node.list);
1952 return find_next_chained_ft(prio);
1955 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1957 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1958 struct mlx5_ft_underlay_qp *uqp;
1959 struct mlx5_flow_table *new_root_ft = NULL;
1963 if (root->root_ft != ft)
1966 new_root_ft = find_next_ft(ft);
1968 root->root_ft = NULL;
1972 if (list_empty(&root->underlay_qpns)) {
1973 /* Don't set any QPN (zero) in case QPN list is empty */
1975 err = root->cmds->update_root_ft(root, new_root_ft,
1978 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1980 err = root->cmds->update_root_ft(root,
1989 mlx5_core_warn(root->dev,
1990 "Update root flow table of id(%u) qpn(%d) failed\n",
1993 root->root_ft = new_root_ft;
1998 /* Connect flow table from previous priority to
1999 * the next flow table.
2001 static int disconnect_flow_table(struct mlx5_flow_table *ft)
2003 struct mlx5_core_dev *dev = get_dev(&ft->node);
2004 struct mlx5_flow_table *next_ft;
2005 struct fs_prio *prio;
2008 err = update_root_ft_destroy(ft);
2012 fs_get_obj(prio, ft->node.parent);
2013 if (!(list_first_entry(&prio->node.children,
2014 struct mlx5_flow_table,
2018 next_ft = find_next_chained_ft(prio);
2019 err = connect_fwd_rules(dev, next_ft, ft);
2023 err = connect_prev_fts(dev, next_ft, prio);
2025 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2030 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
2032 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2035 mutex_lock(&root->chain_lock);
2036 err = disconnect_flow_table(ft);
2038 mutex_unlock(&root->chain_lock);
2041 if (tree_remove_node(&ft->node, false))
2042 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2044 mutex_unlock(&root->chain_lock);
2048 EXPORT_SYMBOL(mlx5_destroy_flow_table);
2050 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
2052 if (tree_remove_node(&fg->node, false))
2053 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2057 struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
2060 struct mlx5_flow_steering *steering = dev->priv.steering;
2062 if (!steering || !steering->fdb_sub_ns)
2065 return steering->fdb_sub_ns[n];
2067 EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
2069 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2070 enum mlx5_flow_namespace_type type)
2072 struct mlx5_flow_steering *steering = dev->priv.steering;
2073 struct mlx5_flow_root_namespace *root_ns;
2075 struct fs_prio *fs_prio;
2076 struct mlx5_flow_namespace *ns;
2082 case MLX5_FLOW_NAMESPACE_FDB:
2083 if (steering->fdb_root_ns)
2084 return &steering->fdb_root_ns->ns;
2086 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2087 if (steering->sniffer_rx_root_ns)
2088 return &steering->sniffer_rx_root_ns->ns;
2090 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2091 if (steering->sniffer_tx_root_ns)
2092 return &steering->sniffer_tx_root_ns->ns;
2098 if (type == MLX5_FLOW_NAMESPACE_EGRESS) {
2099 root_ns = steering->egress_root_ns;
2100 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
2101 root_ns = steering->rdma_rx_root_ns;
2102 prio = RDMA_RX_BYPASS_PRIO;
2103 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL) {
2104 root_ns = steering->rdma_rx_root_ns;
2105 prio = RDMA_RX_KERNEL_PRIO;
2106 } else { /* Must be NIC RX */
2107 root_ns = steering->root_ns;
2114 fs_prio = find_prio(&root_ns->ns, prio);
2118 ns = list_first_entry(&fs_prio->node.children,
2124 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2126 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2127 enum mlx5_flow_namespace_type type,
2130 struct mlx5_flow_steering *steering = dev->priv.steering;
2132 if (!steering || vport >= mlx5_eswitch_get_total_vports(dev))
2136 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2137 if (steering->esw_egress_root_ns &&
2138 steering->esw_egress_root_ns[vport])
2139 return &steering->esw_egress_root_ns[vport]->ns;
2142 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2143 if (steering->esw_ingress_root_ns &&
2144 steering->esw_ingress_root_ns[vport])
2145 return &steering->esw_ingress_root_ns[vport]->ns;
2153 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2156 enum fs_node_type type)
2158 struct fs_prio *fs_prio;
2160 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2162 return ERR_PTR(-ENOMEM);
2164 fs_prio->node.type = type;
2165 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2166 tree_add_node(&fs_prio->node, &ns->node);
2167 fs_prio->num_levels = num_levels;
2168 fs_prio->prio = prio;
2169 list_add_tail(&fs_prio->node.list, &ns->node.children);
2174 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2178 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2181 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2182 unsigned int prio, int num_levels)
2184 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2187 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2190 ns->node.type = FS_TYPE_NAMESPACE;
2195 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2198 struct mlx5_flow_namespace *ns;
2200 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2202 return ERR_PTR(-ENOMEM);
2204 fs_init_namespace(ns);
2205 ns->def_miss_action = def_miss_act;
2206 tree_init_node(&ns->node, NULL, del_sw_ns);
2207 tree_add_node(&ns->node, &prio->node);
2208 list_add_tail(&ns->node.list, &prio->node.children);
2213 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2214 struct init_tree_node *prio_metadata)
2216 struct fs_prio *fs_prio;
2219 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2220 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2221 if (IS_ERR(fs_prio))
2222 return PTR_ERR(fs_prio);
2227 #define FLOW_TABLE_BIT_SZ 1
2228 #define GET_FLOW_TABLE_CAP(dev, offset) \
2229 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
2231 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2232 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2236 for (i = 0; i < caps->arr_sz; i++) {
2237 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2243 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2244 struct init_tree_node *init_node,
2245 struct fs_node *fs_parent_node,
2246 struct init_tree_node *init_parent_node,
2249 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2250 flow_table_properties_nic_receive.
2252 struct mlx5_flow_namespace *fs_ns;
2253 struct fs_prio *fs_prio;
2254 struct fs_node *base;
2258 if (init_node->type == FS_TYPE_PRIO) {
2259 if ((init_node->min_ft_level > max_ft_level) ||
2260 !has_required_caps(steering->dev, &init_node->caps))
2263 fs_get_obj(fs_ns, fs_parent_node);
2264 if (init_node->num_leaf_prios)
2265 return create_leaf_prios(fs_ns, prio, init_node);
2266 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2267 if (IS_ERR(fs_prio))
2268 return PTR_ERR(fs_prio);
2269 base = &fs_prio->node;
2270 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2271 fs_get_obj(fs_prio, fs_parent_node);
2272 fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
2274 return PTR_ERR(fs_ns);
2275 base = &fs_ns->node;
2280 for (i = 0; i < init_node->ar_size; i++) {
2281 err = init_root_tree_recursive(steering, &init_node->children[i],
2282 base, init_node, prio);
2285 if (init_node->children[i].type == FS_TYPE_PRIO &&
2286 init_node->children[i].num_leaf_prios) {
2287 prio += init_node->children[i].num_leaf_prios;
2294 static int init_root_tree(struct mlx5_flow_steering *steering,
2295 struct init_tree_node *init_node,
2296 struct fs_node *fs_parent_node)
2299 struct mlx5_flow_namespace *fs_ns;
2302 fs_get_obj(fs_ns, fs_parent_node);
2303 for (i = 0; i < init_node->ar_size; i++) {
2304 err = init_root_tree_recursive(steering, &init_node->children[i],
2313 static struct mlx5_flow_root_namespace
2314 *create_root_ns(struct mlx5_flow_steering *steering,
2315 enum fs_flow_table_type table_type)
2317 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2318 struct mlx5_flow_root_namespace *root_ns;
2319 struct mlx5_flow_namespace *ns;
2321 if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2322 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2323 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2325 /* Create the root namespace */
2326 root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL);
2330 root_ns->dev = steering->dev;
2331 root_ns->table_type = table_type;
2332 root_ns->cmds = cmds;
2334 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2337 fs_init_namespace(ns);
2338 mutex_init(&root_ns->chain_lock);
2339 tree_init_node(&ns->node, NULL, NULL);
2340 tree_add_node(&ns->node, NULL);
2345 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2347 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2349 struct fs_prio *prio;
2351 fs_for_each_prio(prio, ns) {
2352 /* This updates prio start_level and num_levels */
2353 set_prio_attrs_in_prio(prio, acc_level);
2354 acc_level += prio->num_levels;
2359 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2361 struct mlx5_flow_namespace *ns;
2362 int acc_level_ns = acc_level;
2364 prio->start_level = acc_level;
2365 fs_for_each_ns(ns, prio)
2366 /* This updates start_level and num_levels of ns's priority descendants */
2367 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2368 if (!prio->num_levels)
2369 prio->num_levels = acc_level_ns - prio->start_level;
2370 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2373 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2375 struct mlx5_flow_namespace *ns = &root_ns->ns;
2376 struct fs_prio *prio;
2377 int start_level = 0;
2379 fs_for_each_prio(prio, ns) {
2380 set_prio_attrs_in_prio(prio, start_level);
2381 start_level += prio->num_levels;
2385 #define ANCHOR_PRIO 0
2386 #define ANCHOR_SIZE 1
2387 #define ANCHOR_LEVEL 0
2388 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2390 struct mlx5_flow_namespace *ns = NULL;
2391 struct mlx5_flow_table_attr ft_attr = {};
2392 struct mlx5_flow_table *ft;
2394 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2398 ft_attr.max_fte = ANCHOR_SIZE;
2399 ft_attr.level = ANCHOR_LEVEL;
2400 ft_attr.prio = ANCHOR_PRIO;
2402 ft = mlx5_create_flow_table(ns, &ft_attr);
2404 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2410 static int init_root_ns(struct mlx5_flow_steering *steering)
2414 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2415 if (!steering->root_ns)
2418 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2422 set_prio_attrs(steering->root_ns);
2423 err = create_anchor_flow_table(steering);
2430 cleanup_root_ns(steering->root_ns);
2431 steering->root_ns = NULL;
2435 static void clean_tree(struct fs_node *node)
2438 struct fs_node *iter;
2439 struct fs_node *temp;
2441 tree_get_node(node);
2442 list_for_each_entry_safe(iter, temp, &node->children, list)
2444 tree_put_node(node, false);
2445 tree_remove_node(node, false);
2449 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2454 clean_tree(&root_ns->ns.node);
2457 static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2459 struct mlx5_flow_steering *steering = dev->priv.steering;
2462 if (!steering->esw_egress_root_ns)
2465 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++)
2466 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2468 kfree(steering->esw_egress_root_ns);
2469 steering->esw_egress_root_ns = NULL;
2472 static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2474 struct mlx5_flow_steering *steering = dev->priv.steering;
2477 if (!steering->esw_ingress_root_ns)
2480 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++)
2481 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2483 kfree(steering->esw_ingress_root_ns);
2484 steering->esw_ingress_root_ns = NULL;
2487 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2489 struct mlx5_flow_steering *steering = dev->priv.steering;
2491 cleanup_root_ns(steering->root_ns);
2492 cleanup_egress_acls_root_ns(dev);
2493 cleanup_ingress_acls_root_ns(dev);
2494 cleanup_root_ns(steering->fdb_root_ns);
2495 steering->fdb_root_ns = NULL;
2496 kfree(steering->fdb_sub_ns);
2497 steering->fdb_sub_ns = NULL;
2498 cleanup_root_ns(steering->sniffer_rx_root_ns);
2499 cleanup_root_ns(steering->sniffer_tx_root_ns);
2500 cleanup_root_ns(steering->rdma_rx_root_ns);
2501 cleanup_root_ns(steering->egress_root_ns);
2502 mlx5_cleanup_fc_stats(dev);
2503 kmem_cache_destroy(steering->ftes_cache);
2504 kmem_cache_destroy(steering->fgs_cache);
2508 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2510 struct fs_prio *prio;
2512 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2513 if (!steering->sniffer_tx_root_ns)
2516 /* Create single prio */
2517 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2518 return PTR_ERR_OR_ZERO(prio);
2521 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2523 struct fs_prio *prio;
2525 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2526 if (!steering->sniffer_rx_root_ns)
2529 /* Create single prio */
2530 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2531 return PTR_ERR_OR_ZERO(prio);
2534 static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
2538 steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
2539 if (!steering->rdma_rx_root_ns)
2542 err = init_root_tree(steering, &rdma_rx_root_fs,
2543 &steering->rdma_rx_root_ns->ns.node);
2547 set_prio_attrs(steering->rdma_rx_root_ns);
2552 cleanup_root_ns(steering->rdma_rx_root_ns);
2553 steering->rdma_rx_root_ns = NULL;
2556 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2558 struct mlx5_flow_namespace *ns;
2559 struct fs_prio *maj_prio;
2560 struct fs_prio *min_prio;
2566 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2567 if (!steering->fdb_root_ns)
2570 steering->fdb_sub_ns = kzalloc(sizeof(steering->fdb_sub_ns) *
2571 (FDB_MAX_CHAIN + 1), GFP_KERNEL);
2572 if (!steering->fdb_sub_ns)
2575 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH,
2577 if (IS_ERR(maj_prio)) {
2578 err = PTR_ERR(maj_prio);
2582 levels = 2 * FDB_MAX_PRIO * (FDB_MAX_CHAIN + 1);
2583 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
2586 if (IS_ERR(maj_prio)) {
2587 err = PTR_ERR(maj_prio);
2591 for (chain = 0; chain <= FDB_MAX_CHAIN; chain++) {
2592 ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
2598 for (prio = 0; prio < FDB_MAX_PRIO * (chain + 1); prio++) {
2599 min_prio = fs_create_prio(ns, prio, 2);
2600 if (IS_ERR(min_prio)) {
2601 err = PTR_ERR(min_prio);
2606 steering->fdb_sub_ns[chain] = ns;
2609 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
2610 if (IS_ERR(maj_prio)) {
2611 err = PTR_ERR(maj_prio);
2615 set_prio_attrs(steering->fdb_root_ns);
2619 cleanup_root_ns(steering->fdb_root_ns);
2620 kfree(steering->fdb_sub_ns);
2621 steering->fdb_sub_ns = NULL;
2622 steering->fdb_root_ns = NULL;
2626 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2628 struct fs_prio *prio;
2630 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2631 if (!steering->esw_egress_root_ns[vport])
2635 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2636 return PTR_ERR_OR_ZERO(prio);
2639 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2641 struct fs_prio *prio;
2643 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2644 if (!steering->esw_ingress_root_ns[vport])
2648 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2649 return PTR_ERR_OR_ZERO(prio);
2652 static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2654 struct mlx5_flow_steering *steering = dev->priv.steering;
2655 int total_vports = mlx5_eswitch_get_total_vports(dev);
2659 steering->esw_egress_root_ns =
2660 kcalloc(total_vports,
2661 sizeof(*steering->esw_egress_root_ns),
2663 if (!steering->esw_egress_root_ns)
2666 for (i = 0; i < total_vports; i++) {
2667 err = init_egress_acl_root_ns(steering, i);
2669 goto cleanup_root_ns;
2675 for (i--; i >= 0; i--)
2676 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2677 kfree(steering->esw_egress_root_ns);
2678 steering->esw_egress_root_ns = NULL;
2682 static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2684 struct mlx5_flow_steering *steering = dev->priv.steering;
2685 int total_vports = mlx5_eswitch_get_total_vports(dev);
2689 steering->esw_ingress_root_ns =
2690 kcalloc(total_vports,
2691 sizeof(*steering->esw_ingress_root_ns),
2693 if (!steering->esw_ingress_root_ns)
2696 for (i = 0; i < total_vports; i++) {
2697 err = init_ingress_acl_root_ns(steering, i);
2699 goto cleanup_root_ns;
2705 for (i--; i >= 0; i--)
2706 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2707 kfree(steering->esw_ingress_root_ns);
2708 steering->esw_ingress_root_ns = NULL;
2712 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2716 steering->egress_root_ns = create_root_ns(steering,
2718 if (!steering->egress_root_ns)
2721 err = init_root_tree(steering, &egress_root_fs,
2722 &steering->egress_root_ns->ns.node);
2725 set_prio_attrs(steering->egress_root_ns);
2728 cleanup_root_ns(steering->egress_root_ns);
2729 steering->egress_root_ns = NULL;
2733 int mlx5_init_fs(struct mlx5_core_dev *dev)
2735 struct mlx5_flow_steering *steering;
2738 err = mlx5_init_fc_stats(dev);
2742 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2745 steering->dev = dev;
2746 dev->priv.steering = steering;
2748 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2749 sizeof(struct mlx5_flow_group), 0,
2751 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2753 if (!steering->ftes_cache || !steering->fgs_cache) {
2758 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2759 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2760 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2761 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2762 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2763 err = init_root_ns(steering);
2768 if (MLX5_ESWITCH_MANAGER(dev)) {
2769 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2770 err = init_fdb_root_ns(steering);
2774 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2775 err = init_egress_acls_root_ns(dev);
2779 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2780 err = init_ingress_acls_root_ns(dev);
2786 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2787 err = init_sniffer_rx_root_ns(steering);
2792 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2793 err = init_sniffer_tx_root_ns(steering);
2798 if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) &&
2799 MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)) {
2800 err = init_rdma_rx_root_ns(steering);
2805 if (MLX5_IPSEC_DEV(dev) || MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
2806 err = init_egress_root_ns(steering);
2813 mlx5_cleanup_fs(dev);
2817 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2819 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2820 struct mlx5_ft_underlay_qp *new_uqp;
2823 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2827 mutex_lock(&root->chain_lock);
2829 if (!root->root_ft) {
2831 goto update_ft_fail;
2834 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
2837 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2839 goto update_ft_fail;
2842 new_uqp->qpn = underlay_qpn;
2843 list_add_tail(&new_uqp->list, &root->underlay_qpns);
2845 mutex_unlock(&root->chain_lock);
2850 mutex_unlock(&root->chain_lock);
2854 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2856 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2858 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2859 struct mlx5_ft_underlay_qp *uqp;
2863 mutex_lock(&root->chain_lock);
2864 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2865 if (uqp->qpn == underlay_qpn) {
2872 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2878 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
2881 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2884 list_del(&uqp->list);
2885 mutex_unlock(&root->chain_lock);
2891 mutex_unlock(&root->chain_lock);
2894 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
2896 static struct mlx5_flow_root_namespace
2897 *get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
2899 struct mlx5_flow_namespace *ns;
2901 if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
2902 ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS)
2903 ns = mlx5_get_flow_vport_acl_namespace(dev, ns_type, 0);
2905 ns = mlx5_get_flow_namespace(dev, ns_type);
2909 return find_root(&ns->node);
2912 struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
2913 u8 ns_type, u8 num_actions,
2914 void *modify_actions)
2916 struct mlx5_flow_root_namespace *root;
2917 struct mlx5_modify_hdr *modify_hdr;
2920 root = get_root_namespace(dev, ns_type);
2922 return ERR_PTR(-EOPNOTSUPP);
2924 modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL);
2926 return ERR_PTR(-ENOMEM);
2928 modify_hdr->ns_type = ns_type;
2929 err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
2930 modify_actions, modify_hdr);
2933 return ERR_PTR(err);
2938 EXPORT_SYMBOL(mlx5_modify_header_alloc);
2940 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
2941 struct mlx5_modify_hdr *modify_hdr)
2943 struct mlx5_flow_root_namespace *root;
2945 root = get_root_namespace(dev, modify_hdr->ns_type);
2948 root->cmds->modify_header_dealloc(root, modify_hdr);
2951 EXPORT_SYMBOL(mlx5_modify_header_dealloc);
2953 struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
2956 void *reformat_data,
2957 enum mlx5_flow_namespace_type ns_type)
2959 struct mlx5_pkt_reformat *pkt_reformat;
2960 struct mlx5_flow_root_namespace *root;
2963 root = get_root_namespace(dev, ns_type);
2965 return ERR_PTR(-EOPNOTSUPP);
2967 pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL);
2969 return ERR_PTR(-ENOMEM);
2971 pkt_reformat->ns_type = ns_type;
2972 pkt_reformat->reformat_type = reformat_type;
2973 err = root->cmds->packet_reformat_alloc(root, reformat_type, size,
2974 reformat_data, ns_type,
2977 kfree(pkt_reformat);
2978 return ERR_PTR(err);
2981 return pkt_reformat;
2983 EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
2985 void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
2986 struct mlx5_pkt_reformat *pkt_reformat)
2988 struct mlx5_flow_root_namespace *root;
2990 root = get_root_namespace(dev, pkt_reformat->ns_type);
2993 root->cmds->packet_reformat_dealloc(root, pkt_reformat);
2994 kfree(pkt_reformat);
2996 EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
2998 int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
2999 struct mlx5_flow_root_namespace *peer_ns)
3001 if (peer_ns && ns->mode != peer_ns->mode) {
3002 mlx5_core_err(ns->dev,
3003 "Can't peer namespace of different steering mode\n");
3007 return ns->cmds->set_peer(ns, peer_ns);
3010 /* This function should be called only at init stage of the namespace.
3011 * It is not safe to call this function while steering operations
3012 * are executed in the namespace.
3014 int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
3015 enum mlx5_flow_steering_mode mode)
3017 struct mlx5_flow_root_namespace *root;
3018 const struct mlx5_flow_cmds *cmds;
3021 root = find_root(&ns->node);
3022 if (&root->ns != ns)
3023 /* Can't set cmds to non root namespace */
3026 if (root->table_type != FS_FT_FDB)
3029 if (root->mode == mode)
3032 if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
3033 cmds = mlx5_fs_cmd_get_dr_cmds();
3035 cmds = mlx5_fs_cmd_get_fw_cmds();
3039 err = cmds->create_ns(root);
3041 mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
3046 root->cmds->destroy_ns(root);