2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/mutex.h>
34 #include <linux/mlx5/driver.h>
36 #include "mlx5_core.h"
39 #include "diag/fs_tracepoint.h"
41 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
42 sizeof(struct init_tree_node))
44 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
45 ...) {.type = FS_TYPE_PRIO,\
46 .min_ft_level = min_level_val,\
47 .num_levels = num_levels_val,\
48 .num_leaf_prios = num_prios_val,\
50 .children = (struct init_tree_node[]) {__VA_ARGS__},\
51 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
54 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
55 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
58 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
59 .children = (struct init_tree_node[]) {__VA_ARGS__},\
60 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
63 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
66 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
68 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
69 .caps = (long[]) {__VA_ARGS__} }
71 #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
72 FS_CAP(flow_table_properties_nic_receive.modify_root), \
73 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
74 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
76 #define LEFTOVERS_NUM_LEVELS 1
77 #define LEFTOVERS_NUM_PRIOS 1
79 #define BY_PASS_PRIO_NUM_LEVELS 1
80 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
83 #define ETHTOOL_PRIO_NUM_LEVELS 1
84 #define ETHTOOL_NUM_PRIOS 11
85 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
86 /* Vlan, mac, ttc, inner ttc, aRFS */
87 #define KERNEL_NIC_PRIO_NUM_LEVELS 5
88 #define KERNEL_NIC_NUM_PRIOS 1
89 /* One more level for tc */
90 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
92 #define ANCHOR_NUM_LEVELS 1
93 #define ANCHOR_NUM_PRIOS 1
94 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
96 #define OFFLOADS_MAX_FT 1
97 #define OFFLOADS_NUM_PRIOS 1
98 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
100 #define LAG_PRIO_NUM_LEVELS 1
101 #define LAG_NUM_PRIOS 1
102 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
109 static struct init_tree_node {
110 enum fs_node_type type;
111 struct init_tree_node *children;
113 struct node_caps caps;
119 .type = FS_TYPE_NAMESPACE,
121 .children = (struct init_tree_node[]) {
122 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
124 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
125 BY_PASS_PRIO_NUM_LEVELS))),
126 ADD_PRIO(0, LAG_MIN_LEVEL, 0,
128 ADD_NS(ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
129 LAG_PRIO_NUM_LEVELS))),
130 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {},
131 ADD_NS(ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS, OFFLOADS_MAX_FT))),
132 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0,
134 ADD_NS(ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
135 ETHTOOL_PRIO_NUM_LEVELS))),
136 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
137 ADD_NS(ADD_MULTIPLE_PRIO(1, 1),
138 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
139 KERNEL_NIC_PRIO_NUM_LEVELS))),
140 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
142 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_NUM_LEVELS))),
143 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
144 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_NUM_LEVELS))),
148 enum fs_i_lock_class {
154 static const struct rhashtable_params rhash_fte = {
155 .key_len = FIELD_SIZEOF(struct fs_fte, val),
156 .key_offset = offsetof(struct fs_fte, val),
157 .head_offset = offsetof(struct fs_fte, hash),
158 .automatic_shrinking = true,
162 static const struct rhashtable_params rhash_fg = {
163 .key_len = FIELD_SIZEOF(struct mlx5_flow_group, mask),
164 .key_offset = offsetof(struct mlx5_flow_group, mask),
165 .head_offset = offsetof(struct mlx5_flow_group, hash),
166 .automatic_shrinking = true,
171 static void del_hw_flow_table(struct fs_node *node);
172 static void del_hw_flow_group(struct fs_node *node);
173 static void del_hw_fte(struct fs_node *node);
174 static void del_sw_flow_table(struct fs_node *node);
175 static void del_sw_flow_group(struct fs_node *node);
176 static void del_sw_fte(struct fs_node *node);
177 /* Delete rule (destination) is special case that
178 * requires to lock the FTE for all the deletion process.
180 static void del_sw_hw_rule(struct fs_node *node);
181 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
182 struct mlx5_flow_destination *d2);
183 static struct mlx5_flow_rule *
184 find_flow_rule(struct fs_fte *fte,
185 struct mlx5_flow_destination *dest);
187 static void tree_init_node(struct fs_node *node,
188 void (*del_hw_func)(struct fs_node *),
189 void (*del_sw_func)(struct fs_node *))
191 atomic_set(&node->refcount, 1);
192 INIT_LIST_HEAD(&node->list);
193 INIT_LIST_HEAD(&node->children);
194 init_rwsem(&node->lock);
195 node->del_hw_func = del_hw_func;
196 node->del_sw_func = del_sw_func;
197 node->active = false;
200 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
203 atomic_inc(&parent->refcount);
204 node->parent = parent;
206 /* Parent is the root */
210 node->root = parent->root;
213 static int tree_get_node(struct fs_node *node)
215 return atomic_add_unless(&node->refcount, 1, 0);
218 static void nested_down_read_ref_node(struct fs_node *node,
219 enum fs_i_lock_class class)
222 down_read_nested(&node->lock, class);
223 atomic_inc(&node->refcount);
227 static void nested_down_write_ref_node(struct fs_node *node,
228 enum fs_i_lock_class class)
231 down_write_nested(&node->lock, class);
232 atomic_inc(&node->refcount);
236 static void down_write_ref_node(struct fs_node *node)
239 down_write(&node->lock);
240 atomic_inc(&node->refcount);
244 static void up_read_ref_node(struct fs_node *node)
246 atomic_dec(&node->refcount);
247 up_read(&node->lock);
250 static void up_write_ref_node(struct fs_node *node)
252 atomic_dec(&node->refcount);
253 up_write(&node->lock);
256 static void tree_put_node(struct fs_node *node)
258 struct fs_node *parent_node = node->parent;
260 if (atomic_dec_and_test(&node->refcount)) {
261 if (node->del_hw_func)
262 node->del_hw_func(node);
264 /* Only root namespace doesn't have parent and we just
265 * need to free its node.
267 down_write_ref_node(parent_node);
268 list_del_init(&node->list);
269 if (node->del_sw_func)
270 node->del_sw_func(node);
271 up_write_ref_node(parent_node);
277 if (!node && parent_node)
278 tree_put_node(parent_node);
281 static int tree_remove_node(struct fs_node *node)
283 if (atomic_read(&node->refcount) > 1) {
284 atomic_dec(&node->refcount);
291 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
294 struct fs_prio *iter_prio;
296 fs_for_each_prio(iter_prio, ns) {
297 if (iter_prio->prio == prio)
304 static bool check_last_reserved(const u32 *match_criteria)
306 char *match_criteria_reserved =
307 MLX5_ADDR_OF(fte_match_param, match_criteria, MLX5_FTE_MATCH_PARAM_RESERVED);
309 return !match_criteria_reserved[0] &&
310 !memcmp(match_criteria_reserved, match_criteria_reserved + 1,
311 MLX5_FLD_SZ_BYTES(fte_match_param,
312 MLX5_FTE_MATCH_PARAM_RESERVED) - 1);
315 static bool check_valid_mask(u8 match_criteria_enable, const u32 *match_criteria)
317 if (match_criteria_enable & ~(
318 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) |
319 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) |
320 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS)))
323 if (!(match_criteria_enable &
324 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS)) {
325 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
326 match_criteria, outer_headers);
328 if (fg_type_mask[0] ||
329 memcmp(fg_type_mask, fg_type_mask + 1,
330 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4) - 1))
334 if (!(match_criteria_enable &
335 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS)) {
336 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
337 match_criteria, misc_parameters);
339 if (fg_type_mask[0] ||
340 memcmp(fg_type_mask, fg_type_mask + 1,
341 MLX5_ST_SZ_BYTES(fte_match_set_misc) - 1))
345 if (!(match_criteria_enable &
346 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS)) {
347 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
348 match_criteria, inner_headers);
350 if (fg_type_mask[0] ||
351 memcmp(fg_type_mask, fg_type_mask + 1,
352 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4) - 1))
356 return check_last_reserved(match_criteria);
359 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
363 if (!check_valid_mask(spec->match_criteria_enable, spec->match_criteria)) {
364 pr_warn("mlx5_core: Match criteria given mismatches match_criteria_enable\n");
368 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
369 if (spec->match_value[i] & ~spec->match_criteria[i]) {
370 pr_warn("mlx5_core: match_value differs from match_criteria\n");
374 return check_last_reserved(spec->match_value);
377 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
379 struct fs_node *root;
380 struct mlx5_flow_namespace *ns;
384 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
385 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
389 ns = container_of(root, struct mlx5_flow_namespace, node);
390 return container_of(ns, struct mlx5_flow_root_namespace, ns);
393 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
395 struct mlx5_flow_root_namespace *root = find_root(node);
398 return root->dev->priv.steering;
402 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
404 struct mlx5_flow_root_namespace *root = find_root(node);
411 static void del_hw_flow_table(struct fs_node *node)
413 struct mlx5_flow_table *ft;
414 struct mlx5_core_dev *dev;
417 fs_get_obj(ft, node);
418 dev = get_dev(&ft->node);
421 err = mlx5_cmd_destroy_flow_table(dev, ft);
423 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
427 static void del_sw_flow_table(struct fs_node *node)
429 struct mlx5_flow_table *ft;
430 struct fs_prio *prio;
432 fs_get_obj(ft, node);
434 rhltable_destroy(&ft->fgs_hash);
435 fs_get_obj(prio, ft->node.parent);
440 static void del_sw_hw_rule(struct fs_node *node)
442 struct mlx5_flow_rule *rule;
443 struct mlx5_flow_table *ft;
444 struct mlx5_flow_group *fg;
447 struct mlx5_core_dev *dev = get_dev(node);
449 bool update_fte = false;
451 fs_get_obj(rule, node);
452 fs_get_obj(fte, rule->node.parent);
453 fs_get_obj(fg, fte->node.parent);
454 fs_get_obj(ft, fg->node.parent);
455 trace_mlx5_fs_del_rule(rule);
456 if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
457 mutex_lock(&rule->dest_attr.ft->lock);
458 list_del(&rule->next_ft);
459 mutex_unlock(&rule->dest_attr.ft->lock);
462 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
464 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
465 fte->action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
470 if ((fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
472 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
476 if (update_fte && fte->dests_size) {
477 err = mlx5_cmd_update_fte(dev, ft, fg->id, modify_mask, fte);
480 "%s can't del rule fg id=%d fte_index=%d\n",
481 __func__, fg->id, fte->index);
486 static void del_hw_fte(struct fs_node *node)
488 struct mlx5_flow_table *ft;
489 struct mlx5_flow_group *fg;
490 struct mlx5_core_dev *dev;
494 fs_get_obj(fte, node);
495 fs_get_obj(fg, fte->node.parent);
496 fs_get_obj(ft, fg->node.parent);
498 trace_mlx5_fs_del_fte(fte);
499 dev = get_dev(&ft->node);
501 err = mlx5_cmd_delete_fte(dev, ft,
505 "flow steering can't delete fte in index %d of flow group id %d\n",
510 static void del_sw_fte(struct fs_node *node)
512 struct mlx5_flow_steering *steering = get_steering(node);
513 struct mlx5_flow_group *fg;
517 fs_get_obj(fte, node);
518 fs_get_obj(fg, fte->node.parent);
520 err = rhashtable_remove_fast(&fg->ftes_hash,
524 ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
525 kmem_cache_free(steering->ftes_cache, fte);
528 static void del_hw_flow_group(struct fs_node *node)
530 struct mlx5_flow_group *fg;
531 struct mlx5_flow_table *ft;
532 struct mlx5_core_dev *dev;
534 fs_get_obj(fg, node);
535 fs_get_obj(ft, fg->node.parent);
536 dev = get_dev(&ft->node);
537 trace_mlx5_fs_del_fg(fg);
539 if (fg->node.active && mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
540 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
544 static void del_sw_flow_group(struct fs_node *node)
546 struct mlx5_flow_steering *steering = get_steering(node);
547 struct mlx5_flow_group *fg;
548 struct mlx5_flow_table *ft;
551 fs_get_obj(fg, node);
552 fs_get_obj(ft, fg->node.parent);
554 rhashtable_destroy(&fg->ftes_hash);
555 ida_destroy(&fg->fte_allocator);
556 if (ft->autogroup.active)
557 ft->autogroup.num_groups--;
558 err = rhltable_remove(&ft->fgs_hash,
562 kmem_cache_free(steering->fgs_cache, fg);
565 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
570 index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
574 fte->index = index + fg->start_index;
575 ret = rhashtable_insert_fast(&fg->ftes_hash,
581 tree_add_node(&fte->node, &fg->node);
582 list_add_tail(&fte->node.list, &fg->node.children);
586 ida_simple_remove(&fg->fte_allocator, index);
590 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
592 struct mlx5_flow_act *flow_act)
594 struct mlx5_flow_steering *steering = get_steering(&ft->node);
597 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
599 return ERR_PTR(-ENOMEM);
601 memcpy(fte->val, match_value, sizeof(fte->val));
602 fte->node.type = FS_TYPE_FLOW_ENTRY;
603 fte->flow_tag = flow_act->flow_tag;
604 fte->action = flow_act->action;
605 fte->encap_id = flow_act->encap_id;
606 fte->modify_id = flow_act->modify_id;
608 tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
613 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
614 struct mlx5_flow_group *fg)
616 rhashtable_destroy(&fg->ftes_hash);
617 kmem_cache_free(steering->fgs_cache, fg);
620 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
621 u8 match_criteria_enable,
622 void *match_criteria,
626 struct mlx5_flow_group *fg;
629 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
631 return ERR_PTR(-ENOMEM);
633 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
635 kmem_cache_free(steering->fgs_cache, fg);
638 ida_init(&fg->fte_allocator);
639 fg->mask.match_criteria_enable = match_criteria_enable;
640 memcpy(&fg->mask.match_criteria, match_criteria,
641 sizeof(fg->mask.match_criteria));
642 fg->node.type = FS_TYPE_FLOW_GROUP;
643 fg->start_index = start_index;
644 fg->max_ftes = end_index - start_index + 1;
649 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
650 u8 match_criteria_enable,
651 void *match_criteria,
654 struct list_head *prev)
656 struct mlx5_flow_steering *steering = get_steering(&ft->node);
657 struct mlx5_flow_group *fg;
660 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
661 start_index, end_index);
665 /* initialize refcnt, add to parent list */
666 ret = rhltable_insert(&ft->fgs_hash,
670 dealloc_flow_group(steering, fg);
674 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
675 tree_add_node(&fg->node, &ft->node);
676 /* Add node to group list */
677 list_add(&fg->node.list, prev);
678 atomic_inc(&ft->node.version);
683 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
684 enum fs_flow_table_type table_type,
685 enum fs_flow_table_op_mod op_mod,
688 struct mlx5_flow_table *ft;
691 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
693 return ERR_PTR(-ENOMEM);
695 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
702 ft->node.type = FS_TYPE_FLOW_TABLE;
704 ft->type = table_type;
706 ft->max_fte = max_fte;
708 INIT_LIST_HEAD(&ft->fwd_rules);
709 mutex_init(&ft->lock);
714 /* If reverse is false, then we search for the first flow table in the
715 * root sub-tree from start(closest from right), else we search for the
716 * last flow table in the root sub-tree till start(closest from left).
718 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
719 struct list_head *start,
722 #define list_advance_entry(pos, reverse) \
723 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
725 #define list_for_each_advance_continue(pos, head, reverse) \
726 for (pos = list_advance_entry(pos, reverse); \
727 &pos->list != (head); \
728 pos = list_advance_entry(pos, reverse))
730 struct fs_node *iter = list_entry(start, struct fs_node, list);
731 struct mlx5_flow_table *ft = NULL;
736 list_for_each_advance_continue(iter, &root->children, reverse) {
737 if (iter->type == FS_TYPE_FLOW_TABLE) {
738 fs_get_obj(ft, iter);
741 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
749 /* If reverse if false then return the first flow table in next priority of
750 * prio in the tree, else return the last flow table in the previous priority
751 * of prio in the tree.
753 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
755 struct mlx5_flow_table *ft = NULL;
756 struct fs_node *curr_node;
757 struct fs_node *parent;
759 parent = prio->node.parent;
760 curr_node = &prio->node;
761 while (!ft && parent) {
762 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
764 parent = curr_node->parent;
769 /* Assuming all the tree is locked by mutex chain lock */
770 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
772 return find_closest_ft(prio, false);
775 /* Assuming all the tree is locked by mutex chain lock */
776 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
778 return find_closest_ft(prio, true);
781 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
782 struct fs_prio *prio,
783 struct mlx5_flow_table *ft)
785 struct mlx5_flow_table *iter;
789 fs_for_each_ft(iter, prio) {
791 err = mlx5_cmd_modify_flow_table(dev,
795 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
797 /* The driver is out of sync with the FW */
806 /* Connect flow tables from previous priority of prio to ft */
807 static int connect_prev_fts(struct mlx5_core_dev *dev,
808 struct mlx5_flow_table *ft,
809 struct fs_prio *prio)
811 struct mlx5_flow_table *prev_ft;
813 prev_ft = find_prev_chained_ft(prio);
815 struct fs_prio *prev_prio;
817 fs_get_obj(prev_prio, prev_ft->node.parent);
818 return connect_fts_in_prio(dev, prev_prio, ft);
823 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
826 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
827 struct mlx5_ft_underlay_qp *uqp;
828 int min_level = INT_MAX;
833 min_level = root->root_ft->level;
835 if (ft->level >= min_level)
838 if (list_empty(&root->underlay_qpns)) {
839 /* Don't set any QPN (zero) in case QPN list is empty */
841 err = mlx5_cmd_update_root_ft(root->dev, ft, qpn, false);
843 list_for_each_entry(uqp, &root->underlay_qpns, list) {
845 err = mlx5_cmd_update_root_ft(root->dev, ft, qpn,
853 mlx5_core_warn(root->dev,
854 "Update root flow table of id(%u) qpn(%d) failed\n",
862 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
863 struct mlx5_flow_destination *dest)
865 struct mlx5_flow_table *ft;
866 struct mlx5_flow_group *fg;
868 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
871 fs_get_obj(fte, rule->node.parent);
872 if (!(fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
874 down_write_ref_node(&fte->node);
875 fs_get_obj(fg, fte->node.parent);
876 fs_get_obj(ft, fg->node.parent);
878 memcpy(&rule->dest_attr, dest, sizeof(*dest));
879 err = mlx5_cmd_update_fte(get_dev(&ft->node),
883 up_write_ref_node(&fte->node);
888 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
889 struct mlx5_flow_destination *new_dest,
890 struct mlx5_flow_destination *old_dest)
895 if (handle->num_rules != 1)
897 return _mlx5_modify_rule_destination(handle->rule[0],
901 for (i = 0; i < handle->num_rules; i++) {
902 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
903 return _mlx5_modify_rule_destination(handle->rule[i],
910 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
911 static int connect_fwd_rules(struct mlx5_core_dev *dev,
912 struct mlx5_flow_table *new_next_ft,
913 struct mlx5_flow_table *old_next_ft)
915 struct mlx5_flow_destination dest;
916 struct mlx5_flow_rule *iter;
919 /* new_next_ft and old_next_ft could be NULL only
920 * when we create/destroy the anchor flow table.
922 if (!new_next_ft || !old_next_ft)
925 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
926 dest.ft = new_next_ft;
928 mutex_lock(&old_next_ft->lock);
929 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
930 mutex_unlock(&old_next_ft->lock);
931 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
932 err = _mlx5_modify_rule_destination(iter, &dest);
934 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
940 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
941 struct fs_prio *prio)
943 struct mlx5_flow_table *next_ft;
946 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
948 if (list_empty(&prio->node.children)) {
949 err = connect_prev_fts(dev, ft, prio);
953 next_ft = find_next_chained_ft(prio);
954 err = connect_fwd_rules(dev, ft, next_ft);
959 if (MLX5_CAP_FLOWTABLE(dev,
960 flow_table_properties_nic_receive.modify_root))
961 err = update_root_ft_create(ft, prio);
965 static void list_add_flow_table(struct mlx5_flow_table *ft,
966 struct fs_prio *prio)
968 struct list_head *prev = &prio->node.children;
969 struct mlx5_flow_table *iter;
971 fs_for_each_ft(iter, prio) {
972 if (iter->level > ft->level)
974 prev = &iter->node.list;
976 list_add(&ft->node.list, prev);
979 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
980 struct mlx5_flow_table_attr *ft_attr,
981 enum fs_flow_table_op_mod op_mod,
984 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
985 struct mlx5_flow_table *next_ft = NULL;
986 struct fs_prio *fs_prio = NULL;
987 struct mlx5_flow_table *ft;
992 pr_err("mlx5: flow steering failed to find root of namespace\n");
993 return ERR_PTR(-ENODEV);
996 mutex_lock(&root->chain_lock);
997 fs_prio = find_prio(ns, ft_attr->prio);
1002 if (ft_attr->level >= fs_prio->num_levels) {
1006 /* The level is related to the
1007 * priority level range.
1009 ft_attr->level += fs_prio->start_level;
1010 ft = alloc_flow_table(ft_attr->level,
1012 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
1014 op_mod, ft_attr->flags);
1020 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1021 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
1022 next_ft = find_next_chained_ft(fs_prio);
1023 err = mlx5_cmd_create_flow_table(root->dev, ft->vport, ft->op_mod, ft->type,
1024 ft->level, log_table_sz, next_ft, &ft->id,
1029 err = connect_flow_table(root->dev, ft, fs_prio);
1032 ft->node.active = true;
1033 down_write_ref_node(&fs_prio->node);
1034 tree_add_node(&ft->node, &fs_prio->node);
1035 list_add_flow_table(ft, fs_prio);
1037 up_write_ref_node(&fs_prio->node);
1038 mutex_unlock(&root->chain_lock);
1041 mlx5_cmd_destroy_flow_table(root->dev, ft);
1045 mutex_unlock(&root->chain_lock);
1046 return ERR_PTR(err);
1049 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1050 struct mlx5_flow_table_attr *ft_attr)
1052 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1055 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1056 int prio, int max_fte,
1057 u32 level, u16 vport)
1059 struct mlx5_flow_table_attr ft_attr = {};
1061 ft_attr.max_fte = max_fte;
1062 ft_attr.level = level;
1063 ft_attr.prio = prio;
1065 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1068 struct mlx5_flow_table*
1069 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1070 int prio, u32 level)
1072 struct mlx5_flow_table_attr ft_attr = {};
1074 ft_attr.level = level;
1075 ft_attr.prio = prio;
1076 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1078 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1080 struct mlx5_flow_table*
1081 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1083 int num_flow_table_entries,
1088 struct mlx5_flow_table_attr ft_attr = {};
1089 struct mlx5_flow_table *ft;
1091 if (max_num_groups > num_flow_table_entries)
1092 return ERR_PTR(-EINVAL);
1094 ft_attr.max_fte = num_flow_table_entries;
1095 ft_attr.prio = prio;
1096 ft_attr.level = level;
1097 ft_attr.flags = flags;
1099 ft = mlx5_create_flow_table(ns, &ft_attr);
1103 ft->autogroup.active = true;
1104 ft->autogroup.required_groups = max_num_groups;
1108 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1110 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1113 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1114 fg_in, match_criteria);
1115 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1117 match_criteria_enable);
1118 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1120 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1122 struct mlx5_core_dev *dev = get_dev(&ft->node);
1123 struct mlx5_flow_group *fg;
1126 if (!check_valid_mask(match_criteria_enable, match_criteria))
1127 return ERR_PTR(-EINVAL);
1129 if (ft->autogroup.active)
1130 return ERR_PTR(-EPERM);
1132 down_write_ref_node(&ft->node);
1133 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1134 start_index, end_index,
1135 ft->node.children.prev);
1136 up_write_ref_node(&ft->node);
1140 err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
1142 tree_put_node(&fg->node);
1143 return ERR_PTR(err);
1145 trace_mlx5_fs_add_fg(fg);
1146 fg->node.active = true;
1151 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1153 struct mlx5_flow_rule *rule;
1155 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1159 INIT_LIST_HEAD(&rule->next_ft);
1160 rule->node.type = FS_TYPE_FLOW_DEST;
1162 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1167 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1169 struct mlx5_flow_handle *handle;
1171 handle = kzalloc(sizeof(*handle) + sizeof(handle->rule[0]) *
1172 num_rules, GFP_KERNEL);
1176 handle->num_rules = num_rules;
1181 static void destroy_flow_handle(struct fs_fte *fte,
1182 struct mlx5_flow_handle *handle,
1183 struct mlx5_flow_destination *dest,
1187 if (atomic_dec_and_test(&handle->rule[i]->node.refcount)) {
1189 list_del(&handle->rule[i]->node.list);
1190 kfree(handle->rule[i]);
1196 static struct mlx5_flow_handle *
1197 create_flow_handle(struct fs_fte *fte,
1198 struct mlx5_flow_destination *dest,
1203 struct mlx5_flow_handle *handle;
1204 struct mlx5_flow_rule *rule = NULL;
1205 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1206 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1210 handle = alloc_handle((dest_num) ? dest_num : 1);
1212 return ERR_PTR(-ENOMEM);
1216 rule = find_flow_rule(fte, dest + i);
1218 atomic_inc(&rule->node.refcount);
1224 rule = alloc_rule(dest + i);
1228 /* Add dest to dests list- we need flow tables to be in the
1229 * end of the list for forward to next prio rules.
1231 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1233 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1234 list_add(&rule->node.list, &fte->node.children);
1236 list_add_tail(&rule->node.list, &fte->node.children);
1240 type = dest[i].type ==
1241 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1242 *modify_mask |= type ? count : dst;
1245 handle->rule[i] = rule;
1246 } while (++i < dest_num);
1251 destroy_flow_handle(fte, handle, dest, i);
1252 return ERR_PTR(-ENOMEM);
1255 /* fte should not be deleted while calling this function */
1256 static struct mlx5_flow_handle *
1257 add_rule_fte(struct fs_fte *fte,
1258 struct mlx5_flow_group *fg,
1259 struct mlx5_flow_destination *dest,
1263 struct mlx5_flow_handle *handle;
1264 struct mlx5_flow_table *ft;
1265 int modify_mask = 0;
1267 bool new_rule = false;
1269 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1271 if (IS_ERR(handle) || !new_rule)
1275 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1277 fs_get_obj(ft, fg->node.parent);
1278 if (!(fte->status & FS_FTE_STATUS_EXISTING))
1279 err = mlx5_cmd_create_fte(get_dev(&ft->node),
1282 err = mlx5_cmd_update_fte(get_dev(&ft->node),
1283 ft, fg->id, modify_mask, fte);
1287 fte->node.active = true;
1288 fte->status |= FS_FTE_STATUS_EXISTING;
1289 atomic_inc(&fte->node.version);
1295 destroy_flow_handle(fte, handle, dest, handle->num_rules);
1296 return ERR_PTR(err);
1299 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
1300 struct mlx5_flow_spec *spec)
1302 struct list_head *prev = &ft->node.children;
1303 struct mlx5_flow_group *fg;
1304 unsigned int candidate_index = 0;
1305 unsigned int group_size = 0;
1307 if (!ft->autogroup.active)
1308 return ERR_PTR(-ENOENT);
1310 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1311 /* We save place for flow groups in addition to max types */
1312 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
1314 /* ft->max_fte == ft->autogroup.max_types */
1315 if (group_size == 0)
1318 /* sorted by start_index */
1319 fs_for_each_fg(fg, ft) {
1320 if (candidate_index + group_size > fg->start_index)
1321 candidate_index = fg->start_index + fg->max_ftes;
1324 prev = &fg->node.list;
1327 if (candidate_index + group_size > ft->max_fte)
1328 return ERR_PTR(-ENOSPC);
1330 fg = alloc_insert_flow_group(ft,
1331 spec->match_criteria_enable,
1332 spec->match_criteria,
1334 candidate_index + group_size - 1,
1339 ft->autogroup.num_groups++;
1345 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1346 struct mlx5_flow_group *fg)
1348 struct mlx5_core_dev *dev = get_dev(&ft->node);
1349 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1350 void *match_criteria_addr;
1354 in = kvzalloc(inlen, GFP_KERNEL);
1358 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1359 fg->mask.match_criteria_enable);
1360 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1361 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1363 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1364 in, match_criteria);
1365 memcpy(match_criteria_addr, fg->mask.match_criteria,
1366 sizeof(fg->mask.match_criteria));
1368 err = mlx5_cmd_create_flow_group(dev, ft, in, &fg->id);
1370 fg->node.active = true;
1371 trace_mlx5_fs_add_fg(fg);
1378 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1379 struct mlx5_flow_destination *d2)
1381 if (d1->type == d2->type) {
1382 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1383 d1->vport_num == d2->vport_num) ||
1384 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1385 d1->ft == d2->ft) ||
1386 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1387 d1->tir_num == d2->tir_num))
1394 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1395 struct mlx5_flow_destination *dest)
1397 struct mlx5_flow_rule *rule;
1399 list_for_each_entry(rule, &fte->node.children, node.list) {
1400 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1406 static bool check_conflicting_actions(u32 action1, u32 action2)
1408 u32 xored_actions = action1 ^ action2;
1410 /* if one rule only wants to count, it's ok */
1411 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1412 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1415 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1416 MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1417 MLX5_FLOW_CONTEXT_ACTION_DECAP))
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)) {
1426 mlx5_core_warn(get_dev(&fte->node),
1427 "Found two FTEs with conflicting actions\n");
1431 if (fte->flow_tag != flow_act->flow_tag) {
1432 mlx5_core_warn(get_dev(&fte->node),
1433 "FTE flow tag %u already exists with different flow tag %u\n",
1435 flow_act->flow_tag);
1442 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1444 struct mlx5_flow_act *flow_act,
1445 struct mlx5_flow_destination *dest,
1449 struct mlx5_flow_handle *handle;
1454 ret = check_conflicting_ftes(fte, flow_act);
1456 return ERR_PTR(ret);
1458 old_action = fte->action;
1459 fte->action |= flow_act->action;
1460 handle = add_rule_fte(fte, fg, dest, dest_num,
1461 old_action != flow_act->action);
1462 if (IS_ERR(handle)) {
1463 fte->action = old_action;
1466 trace_mlx5_fs_set_fte(fte, false);
1468 for (i = 0; i < handle->num_rules; i++) {
1469 if (atomic_read(&handle->rule[i]->node.refcount) == 1) {
1470 tree_add_node(&handle->rule[i]->node, &fte->node);
1471 trace_mlx5_fs_add_rule(handle->rule[i]);
1477 struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_handle *handle)
1479 struct mlx5_flow_rule *dst;
1482 fs_get_obj(fte, handle->rule[0]->node.parent);
1484 fs_for_each_dst(dst, fte) {
1485 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
1486 return dst->dest_attr.counter;
1492 static bool counter_is_valid(struct mlx5_fc *counter, u32 action)
1494 if (!(action & MLX5_FLOW_CONTEXT_ACTION_COUNT))
1500 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1501 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1504 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1506 struct mlx5_flow_table *ft)
1508 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1509 return counter_is_valid(dest->counter, action);
1511 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1514 if (!dest || ((dest->type ==
1515 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1516 (dest->ft->level <= ft->level)))
1522 struct list_head list;
1523 struct mlx5_flow_group *g;
1526 struct match_list_head {
1527 struct list_head list;
1528 struct match_list first;
1531 static void free_match_list(struct match_list_head *head)
1533 if (!list_empty(&head->list)) {
1534 struct match_list *iter, *match_tmp;
1536 list_del(&head->first.list);
1537 tree_put_node(&head->first.g->node);
1538 list_for_each_entry_safe(iter, match_tmp, &head->list,
1540 tree_put_node(&iter->g->node);
1541 list_del(&iter->list);
1547 static int build_match_list(struct match_list_head *match_head,
1548 struct mlx5_flow_table *ft,
1549 struct mlx5_flow_spec *spec)
1551 struct rhlist_head *tmp, *list;
1552 struct mlx5_flow_group *g;
1556 INIT_LIST_HEAD(&match_head->list);
1557 /* Collect all fgs which has a matching match_criteria */
1558 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1559 /* RCU is atomic, we can't execute FW commands here */
1560 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1561 struct match_list *curr_match;
1563 if (likely(list_empty(&match_head->list))) {
1564 if (!tree_get_node(&g->node))
1566 match_head->first.g = g;
1567 list_add_tail(&match_head->first.list,
1572 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1574 free_match_list(match_head);
1578 if (!tree_get_node(&g->node)) {
1583 list_add_tail(&curr_match->list, &match_head->list);
1590 static u64 matched_fgs_get_version(struct list_head *match_head)
1592 struct match_list *iter;
1595 list_for_each_entry(iter, match_head, list)
1596 version += (u64)atomic_read(&iter->g->node.version);
1600 static struct mlx5_flow_handle *
1601 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1602 struct list_head *match_head,
1603 struct mlx5_flow_spec *spec,
1604 struct mlx5_flow_act *flow_act,
1605 struct mlx5_flow_destination *dest,
1609 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1610 struct mlx5_flow_group *g;
1611 struct mlx5_flow_handle *rule;
1612 struct match_list *iter;
1613 bool take_write = false;
1618 fte = alloc_fte(ft, spec->match_value, flow_act);
1620 return ERR_PTR(-ENOMEM);
1622 list_for_each_entry(iter, match_head, list) {
1623 nested_down_read_ref_node(&iter->g->node, FS_LOCK_PARENT);
1624 ida_pre_get(&iter->g->fte_allocator, GFP_KERNEL);
1627 search_again_locked:
1628 version = matched_fgs_get_version(match_head);
1629 /* Try to find a fg that already contains a matching fte */
1630 list_for_each_entry(iter, match_head, list) {
1631 struct fs_fte *fte_tmp;
1634 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, spec->match_value,
1636 if (!fte_tmp || !tree_get_node(&fte_tmp->node))
1639 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1641 list_for_each_entry(iter, match_head, list)
1642 up_read_ref_node(&iter->g->node);
1644 list_for_each_entry(iter, match_head, list)
1645 up_write_ref_node(&iter->g->node);
1648 rule = add_rule_fg(g, spec->match_value,
1649 flow_act, dest, dest_num, fte_tmp);
1650 up_write_ref_node(&fte_tmp->node);
1651 tree_put_node(&fte_tmp->node);
1652 kmem_cache_free(steering->ftes_cache, fte);
1656 /* No group with matching fte found. Try to add a new fte to any
1661 list_for_each_entry(iter, match_head, list)
1662 up_read_ref_node(&iter->g->node);
1663 list_for_each_entry(iter, match_head, list)
1664 nested_down_write_ref_node(&iter->g->node,
1669 /* Check the ft version, for case that new flow group
1670 * was added while the fgs weren't locked
1672 if (atomic_read(&ft->node.version) != ft_version) {
1673 rule = ERR_PTR(-EAGAIN);
1677 /* Check the fgs version, for case the new FTE with the
1678 * same values was added while the fgs weren't locked
1680 if (version != matched_fgs_get_version(match_head))
1681 goto search_again_locked;
1683 list_for_each_entry(iter, match_head, list) {
1686 if (!g->node.active)
1688 err = insert_fte(g, fte);
1692 list_for_each_entry(iter, match_head, list)
1693 up_write_ref_node(&iter->g->node);
1694 kmem_cache_free(steering->ftes_cache, fte);
1695 return ERR_PTR(err);
1698 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1699 list_for_each_entry(iter, match_head, list)
1700 up_write_ref_node(&iter->g->node);
1701 rule = add_rule_fg(g, spec->match_value,
1702 flow_act, dest, dest_num, fte);
1703 up_write_ref_node(&fte->node);
1704 tree_put_node(&fte->node);
1707 rule = ERR_PTR(-ENOENT);
1709 list_for_each_entry(iter, match_head, list)
1710 up_write_ref_node(&iter->g->node);
1711 kmem_cache_free(steering->ftes_cache, fte);
1715 static struct mlx5_flow_handle *
1716 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1717 struct mlx5_flow_spec *spec,
1718 struct mlx5_flow_act *flow_act,
1719 struct mlx5_flow_destination *dest,
1723 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1724 struct mlx5_flow_group *g;
1725 struct mlx5_flow_handle *rule;
1726 struct match_list_head match_head;
1727 bool take_write = false;
1733 if (!check_valid_spec(spec))
1734 return ERR_PTR(-EINVAL);
1736 for (i = 0; i < dest_num; i++) {
1737 if (!dest_is_valid(&dest[i], flow_act->action, ft))
1738 return ERR_PTR(-EINVAL);
1740 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1741 search_again_locked:
1742 version = atomic_read(&ft->node.version);
1744 /* Collect all fgs which has a matching match_criteria */
1745 err = build_match_list(&match_head, ft, spec);
1747 return ERR_PTR(err);
1750 up_read_ref_node(&ft->node);
1752 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1754 free_match_list(&match_head);
1755 if (!IS_ERR(rule) ||
1756 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN))
1760 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1764 if (PTR_ERR(rule) == -EAGAIN ||
1765 version != atomic_read(&ft->node.version))
1766 goto search_again_locked;
1768 g = alloc_auto_flow_group(ft, spec);
1771 up_write_ref_node(&ft->node);
1775 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1776 up_write_ref_node(&ft->node);
1778 err = create_auto_flow_group(ft, g);
1780 goto err_release_fg;
1782 fte = alloc_fte(ft, spec->match_value, flow_act);
1785 goto err_release_fg;
1788 err = insert_fte(g, fte);
1790 kmem_cache_free(steering->ftes_cache, fte);
1791 goto err_release_fg;
1794 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1795 up_write_ref_node(&g->node);
1796 rule = add_rule_fg(g, spec->match_value, flow_act, dest,
1798 up_write_ref_node(&fte->node);
1799 tree_put_node(&fte->node);
1800 tree_put_node(&g->node);
1804 up_write_ref_node(&g->node);
1805 tree_put_node(&g->node);
1806 return ERR_PTR(err);
1809 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1811 return ((ft->type == FS_FT_NIC_RX) &&
1812 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1815 struct mlx5_flow_handle *
1816 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1817 struct mlx5_flow_spec *spec,
1818 struct mlx5_flow_act *flow_act,
1819 struct mlx5_flow_destination *dest,
1822 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1823 struct mlx5_flow_destination gen_dest;
1824 struct mlx5_flow_table *next_ft = NULL;
1825 struct mlx5_flow_handle *handle = NULL;
1826 u32 sw_action = flow_act->action;
1827 struct fs_prio *prio;
1829 fs_get_obj(prio, ft->node.parent);
1830 if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1831 if (!fwd_next_prio_supported(ft))
1832 return ERR_PTR(-EOPNOTSUPP);
1834 return ERR_PTR(-EINVAL);
1835 mutex_lock(&root->chain_lock);
1836 next_ft = find_next_chained_ft(prio);
1838 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1839 gen_dest.ft = next_ft;
1842 flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1844 mutex_unlock(&root->chain_lock);
1845 return ERR_PTR(-EOPNOTSUPP);
1849 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, dest_num);
1851 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1852 if (!IS_ERR_OR_NULL(handle) &&
1853 (list_empty(&handle->rule[0]->next_ft))) {
1854 mutex_lock(&next_ft->lock);
1855 list_add(&handle->rule[0]->next_ft,
1856 &next_ft->fwd_rules);
1857 mutex_unlock(&next_ft->lock);
1858 handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1860 mutex_unlock(&root->chain_lock);
1864 EXPORT_SYMBOL(mlx5_add_flow_rules);
1866 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1870 for (i = handle->num_rules - 1; i >= 0; i--)
1871 tree_remove_node(&handle->rule[i]->node);
1874 EXPORT_SYMBOL(mlx5_del_flow_rules);
1876 /* Assuming prio->node.children(flow tables) is sorted by level */
1877 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1879 struct fs_prio *prio;
1881 fs_get_obj(prio, ft->node.parent);
1883 if (!list_is_last(&ft->node.list, &prio->node.children))
1884 return list_next_entry(ft, node.list);
1885 return find_next_chained_ft(prio);
1888 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1890 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1891 struct mlx5_ft_underlay_qp *uqp;
1892 struct mlx5_flow_table *new_root_ft = NULL;
1896 if (root->root_ft != ft)
1899 new_root_ft = find_next_ft(ft);
1902 root->root_ft = NULL;
1906 if (list_empty(&root->underlay_qpns)) {
1907 /* Don't set any QPN (zero) in case QPN list is empty */
1909 err = mlx5_cmd_update_root_ft(root->dev, new_root_ft, qpn,
1912 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1914 err = mlx5_cmd_update_root_ft(root->dev, new_root_ft,
1922 mlx5_core_warn(root->dev,
1923 "Update root flow table of id(%u) qpn(%d) failed\n",
1926 root->root_ft = new_root_ft;
1931 /* Connect flow table from previous priority to
1932 * the next flow table.
1934 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1936 struct mlx5_core_dev *dev = get_dev(&ft->node);
1937 struct mlx5_flow_table *next_ft;
1938 struct fs_prio *prio;
1941 err = update_root_ft_destroy(ft);
1945 fs_get_obj(prio, ft->node.parent);
1946 if (!(list_first_entry(&prio->node.children,
1947 struct mlx5_flow_table,
1951 next_ft = find_next_chained_ft(prio);
1952 err = connect_fwd_rules(dev, next_ft, ft);
1956 err = connect_prev_fts(dev, next_ft, prio);
1958 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1963 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1965 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1968 mutex_lock(&root->chain_lock);
1969 err = disconnect_flow_table(ft);
1971 mutex_unlock(&root->chain_lock);
1974 if (tree_remove_node(&ft->node))
1975 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1977 mutex_unlock(&root->chain_lock);
1981 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1983 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1985 if (tree_remove_node(&fg->node))
1986 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1990 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1991 enum mlx5_flow_namespace_type type)
1993 struct mlx5_flow_steering *steering = dev->priv.steering;
1994 struct mlx5_flow_root_namespace *root_ns;
1996 struct fs_prio *fs_prio;
1997 struct mlx5_flow_namespace *ns;
2003 case MLX5_FLOW_NAMESPACE_BYPASS:
2004 case MLX5_FLOW_NAMESPACE_LAG:
2005 case MLX5_FLOW_NAMESPACE_OFFLOADS:
2006 case MLX5_FLOW_NAMESPACE_ETHTOOL:
2007 case MLX5_FLOW_NAMESPACE_KERNEL:
2008 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
2009 case MLX5_FLOW_NAMESPACE_ANCHOR:
2012 case MLX5_FLOW_NAMESPACE_FDB:
2013 if (steering->fdb_root_ns)
2014 return &steering->fdb_root_ns->ns;
2017 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2018 if (steering->esw_egress_root_ns)
2019 return &steering->esw_egress_root_ns->ns;
2022 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2023 if (steering->esw_ingress_root_ns)
2024 return &steering->esw_ingress_root_ns->ns;
2027 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2028 if (steering->sniffer_rx_root_ns)
2029 return &steering->sniffer_rx_root_ns->ns;
2032 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2033 if (steering->sniffer_tx_root_ns)
2034 return &steering->sniffer_tx_root_ns->ns;
2041 root_ns = steering->root_ns;
2045 fs_prio = find_prio(&root_ns->ns, prio);
2049 ns = list_first_entry(&fs_prio->node.children,
2055 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2057 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2058 unsigned int prio, int num_levels)
2060 struct fs_prio *fs_prio;
2062 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2064 return ERR_PTR(-ENOMEM);
2066 fs_prio->node.type = FS_TYPE_PRIO;
2067 tree_init_node(&fs_prio->node, NULL, NULL);
2068 tree_add_node(&fs_prio->node, &ns->node);
2069 fs_prio->num_levels = num_levels;
2070 fs_prio->prio = prio;
2071 list_add_tail(&fs_prio->node.list, &ns->node.children);
2076 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2079 ns->node.type = FS_TYPE_NAMESPACE;
2084 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2086 struct mlx5_flow_namespace *ns;
2088 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2090 return ERR_PTR(-ENOMEM);
2092 fs_init_namespace(ns);
2093 tree_init_node(&ns->node, NULL, NULL);
2094 tree_add_node(&ns->node, &prio->node);
2095 list_add_tail(&ns->node.list, &prio->node.children);
2100 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2101 struct init_tree_node *prio_metadata)
2103 struct fs_prio *fs_prio;
2106 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2107 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2108 if (IS_ERR(fs_prio))
2109 return PTR_ERR(fs_prio);
2114 #define FLOW_TABLE_BIT_SZ 1
2115 #define GET_FLOW_TABLE_CAP(dev, offset) \
2116 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
2118 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2119 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2123 for (i = 0; i < caps->arr_sz; i++) {
2124 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2130 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2131 struct init_tree_node *init_node,
2132 struct fs_node *fs_parent_node,
2133 struct init_tree_node *init_parent_node,
2136 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2137 flow_table_properties_nic_receive.
2139 struct mlx5_flow_namespace *fs_ns;
2140 struct fs_prio *fs_prio;
2141 struct fs_node *base;
2145 if (init_node->type == FS_TYPE_PRIO) {
2146 if ((init_node->min_ft_level > max_ft_level) ||
2147 !has_required_caps(steering->dev, &init_node->caps))
2150 fs_get_obj(fs_ns, fs_parent_node);
2151 if (init_node->num_leaf_prios)
2152 return create_leaf_prios(fs_ns, prio, init_node);
2153 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2154 if (IS_ERR(fs_prio))
2155 return PTR_ERR(fs_prio);
2156 base = &fs_prio->node;
2157 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2158 fs_get_obj(fs_prio, fs_parent_node);
2159 fs_ns = fs_create_namespace(fs_prio);
2161 return PTR_ERR(fs_ns);
2162 base = &fs_ns->node;
2167 for (i = 0; i < init_node->ar_size; i++) {
2168 err = init_root_tree_recursive(steering, &init_node->children[i],
2169 base, init_node, prio);
2172 if (init_node->children[i].type == FS_TYPE_PRIO &&
2173 init_node->children[i].num_leaf_prios) {
2174 prio += init_node->children[i].num_leaf_prios;
2181 static int init_root_tree(struct mlx5_flow_steering *steering,
2182 struct init_tree_node *init_node,
2183 struct fs_node *fs_parent_node)
2186 struct mlx5_flow_namespace *fs_ns;
2189 fs_get_obj(fs_ns, fs_parent_node);
2190 for (i = 0; i < init_node->ar_size; i++) {
2191 err = init_root_tree_recursive(steering, &init_node->children[i],
2200 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_flow_steering *steering,
2201 enum fs_flow_table_type
2204 struct mlx5_flow_root_namespace *root_ns;
2205 struct mlx5_flow_namespace *ns;
2207 /* Create the root namespace */
2208 root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
2212 root_ns->dev = steering->dev;
2213 root_ns->table_type = table_type;
2215 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2218 fs_init_namespace(ns);
2219 mutex_init(&root_ns->chain_lock);
2220 tree_init_node(&ns->node, NULL, NULL);
2221 tree_add_node(&ns->node, NULL);
2226 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2228 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2230 struct fs_prio *prio;
2232 fs_for_each_prio(prio, ns) {
2233 /* This updates prio start_level and num_levels */
2234 set_prio_attrs_in_prio(prio, acc_level);
2235 acc_level += prio->num_levels;
2240 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2242 struct mlx5_flow_namespace *ns;
2243 int acc_level_ns = acc_level;
2245 prio->start_level = acc_level;
2246 fs_for_each_ns(ns, prio)
2247 /* This updates start_level and num_levels of ns's priority descendants */
2248 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2249 if (!prio->num_levels)
2250 prio->num_levels = acc_level_ns - prio->start_level;
2251 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2254 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2256 struct mlx5_flow_namespace *ns = &root_ns->ns;
2257 struct fs_prio *prio;
2258 int start_level = 0;
2260 fs_for_each_prio(prio, ns) {
2261 set_prio_attrs_in_prio(prio, start_level);
2262 start_level += prio->num_levels;
2266 #define ANCHOR_PRIO 0
2267 #define ANCHOR_SIZE 1
2268 #define ANCHOR_LEVEL 0
2269 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2271 struct mlx5_flow_namespace *ns = NULL;
2272 struct mlx5_flow_table_attr ft_attr = {};
2273 struct mlx5_flow_table *ft;
2275 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2279 ft_attr.max_fte = ANCHOR_SIZE;
2280 ft_attr.level = ANCHOR_LEVEL;
2281 ft_attr.prio = ANCHOR_PRIO;
2283 ft = mlx5_create_flow_table(ns, &ft_attr);
2285 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2291 static int init_root_ns(struct mlx5_flow_steering *steering)
2293 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2294 if (!steering->root_ns)
2297 if (init_root_tree(steering, &root_fs, &steering->root_ns->ns.node))
2300 set_prio_attrs(steering->root_ns);
2302 if (create_anchor_flow_table(steering))
2308 mlx5_cleanup_fs(steering->dev);
2312 static void clean_tree(struct fs_node *node)
2315 struct fs_node *iter;
2316 struct fs_node *temp;
2318 tree_get_node(node);
2319 list_for_each_entry_safe(iter, temp, &node->children, list)
2321 tree_put_node(node);
2322 tree_remove_node(node);
2326 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2331 clean_tree(&root_ns->ns.node);
2334 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2336 struct mlx5_flow_steering *steering = dev->priv.steering;
2338 cleanup_root_ns(steering->root_ns);
2339 cleanup_root_ns(steering->esw_egress_root_ns);
2340 cleanup_root_ns(steering->esw_ingress_root_ns);
2341 cleanup_root_ns(steering->fdb_root_ns);
2342 cleanup_root_ns(steering->sniffer_rx_root_ns);
2343 cleanup_root_ns(steering->sniffer_tx_root_ns);
2344 mlx5_cleanup_fc_stats(dev);
2345 kmem_cache_destroy(steering->ftes_cache);
2346 kmem_cache_destroy(steering->fgs_cache);
2350 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2352 struct fs_prio *prio;
2354 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2355 if (!steering->sniffer_tx_root_ns)
2358 /* Create single prio */
2359 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2361 cleanup_root_ns(steering->sniffer_tx_root_ns);
2362 return PTR_ERR(prio);
2367 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2369 struct fs_prio *prio;
2371 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2372 if (!steering->sniffer_rx_root_ns)
2375 /* Create single prio */
2376 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2378 cleanup_root_ns(steering->sniffer_rx_root_ns);
2379 return PTR_ERR(prio);
2384 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2386 struct fs_prio *prio;
2388 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2389 if (!steering->fdb_root_ns)
2392 prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 1);
2396 prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2400 set_prio_attrs(steering->fdb_root_ns);
2404 cleanup_root_ns(steering->fdb_root_ns);
2405 steering->fdb_root_ns = NULL;
2406 return PTR_ERR(prio);
2409 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering)
2411 struct fs_prio *prio;
2413 steering->esw_egress_root_ns = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2414 if (!steering->esw_egress_root_ns)
2418 prio = fs_create_prio(&steering->esw_egress_root_ns->ns, 0,
2419 MLX5_TOTAL_VPORTS(steering->dev));
2420 return PTR_ERR_OR_ZERO(prio);
2423 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering)
2425 struct fs_prio *prio;
2427 steering->esw_ingress_root_ns = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2428 if (!steering->esw_ingress_root_ns)
2432 prio = fs_create_prio(&steering->esw_ingress_root_ns->ns, 0,
2433 MLX5_TOTAL_VPORTS(steering->dev));
2434 return PTR_ERR_OR_ZERO(prio);
2437 int mlx5_init_fs(struct mlx5_core_dev *dev)
2439 struct mlx5_flow_steering *steering;
2442 err = mlx5_init_fc_stats(dev);
2446 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2449 steering->dev = dev;
2450 dev->priv.steering = steering;
2452 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2453 sizeof(struct mlx5_flow_group), 0,
2455 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2457 if (!steering->ftes_cache || !steering->fgs_cache) {
2462 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2463 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2464 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2465 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2466 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2467 err = init_root_ns(steering);
2472 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
2473 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2474 err = init_fdb_root_ns(steering);
2478 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2479 err = init_egress_acl_root_ns(steering);
2483 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2484 err = init_ingress_acl_root_ns(steering);
2490 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2491 err = init_sniffer_rx_root_ns(steering);
2496 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2497 err = init_sniffer_tx_root_ns(steering);
2504 mlx5_cleanup_fs(dev);
2508 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2510 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2511 struct mlx5_ft_underlay_qp *new_uqp;
2514 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2518 mutex_lock(&root->chain_lock);
2520 if (!root->root_ft) {
2522 goto update_ft_fail;
2525 err = mlx5_cmd_update_root_ft(dev, root->root_ft, underlay_qpn, false);
2527 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2529 goto update_ft_fail;
2532 new_uqp->qpn = underlay_qpn;
2533 list_add_tail(&new_uqp->list, &root->underlay_qpns);
2535 mutex_unlock(&root->chain_lock);
2540 mutex_unlock(&root->chain_lock);
2544 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2546 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2548 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2549 struct mlx5_ft_underlay_qp *uqp;
2553 mutex_lock(&root->chain_lock);
2554 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2555 if (uqp->qpn == underlay_qpn) {
2562 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2568 err = mlx5_cmd_update_root_ft(dev, root->root_ft, underlay_qpn, true);
2570 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2573 list_del(&uqp->list);
2574 mutex_unlock(&root->chain_lock);
2580 mutex_unlock(&root->chain_lock);
2583 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);