2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/mutex.h>
34 #include <linux/mlx5/driver.h>
36 #include "mlx5_core.h"
39 #include "diag/fs_tracepoint.h"
40 #include "accel/ipsec.h"
41 #include "fpga/ipsec.h"
43 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
44 sizeof(struct init_tree_node))
46 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
47 ...) {.type = FS_TYPE_PRIO,\
48 .min_ft_level = min_level_val,\
49 .num_levels = num_levels_val,\
50 .num_leaf_prios = num_prios_val,\
52 .children = (struct init_tree_node[]) {__VA_ARGS__},\
53 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
56 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
57 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
60 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
61 .children = (struct init_tree_node[]) {__VA_ARGS__},\
62 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
65 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
68 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
70 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
71 .caps = (long[]) {__VA_ARGS__} }
73 #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
74 FS_CAP(flow_table_properties_nic_receive.modify_root), \
75 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
76 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
78 #define LEFTOVERS_NUM_LEVELS 1
79 #define LEFTOVERS_NUM_PRIOS 1
81 #define BY_PASS_PRIO_NUM_LEVELS 1
82 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
85 #define ETHTOOL_PRIO_NUM_LEVELS 1
86 #define ETHTOOL_NUM_PRIOS 11
87 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
88 /* Vlan, mac, ttc, inner ttc, aRFS */
89 #define KERNEL_NIC_PRIO_NUM_LEVELS 5
90 #define KERNEL_NIC_NUM_PRIOS 1
91 /* One more level for tc */
92 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
94 #define KERNEL_NIC_TC_NUM_PRIOS 1
95 #define KERNEL_NIC_TC_NUM_LEVELS 2
97 #define ANCHOR_NUM_LEVELS 1
98 #define ANCHOR_NUM_PRIOS 1
99 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
101 #define OFFLOADS_MAX_FT 1
102 #define OFFLOADS_NUM_PRIOS 1
103 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
105 #define LAG_PRIO_NUM_LEVELS 1
106 #define LAG_NUM_PRIOS 1
107 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
114 static struct init_tree_node {
115 enum fs_node_type type;
116 struct init_tree_node *children;
118 struct node_caps caps;
124 .type = FS_TYPE_NAMESPACE,
126 .children = (struct init_tree_node[]) {
127 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
129 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
130 BY_PASS_PRIO_NUM_LEVELS))),
131 ADD_PRIO(0, LAG_MIN_LEVEL, 0,
133 ADD_NS(ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
134 LAG_PRIO_NUM_LEVELS))),
135 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {},
136 ADD_NS(ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS, OFFLOADS_MAX_FT))),
137 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0,
139 ADD_NS(ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
140 ETHTOOL_PRIO_NUM_LEVELS))),
141 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
142 ADD_NS(ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS, KERNEL_NIC_TC_NUM_LEVELS),
143 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
144 KERNEL_NIC_PRIO_NUM_LEVELS))),
145 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
147 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_NUM_LEVELS))),
148 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
149 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_NUM_LEVELS))),
153 enum fs_i_lock_class {
159 static const struct rhashtable_params rhash_fte = {
160 .key_len = FIELD_SIZEOF(struct fs_fte, val),
161 .key_offset = offsetof(struct fs_fte, val),
162 .head_offset = offsetof(struct fs_fte, hash),
163 .automatic_shrinking = true,
167 static const struct rhashtable_params rhash_fg = {
168 .key_len = FIELD_SIZEOF(struct mlx5_flow_group, mask),
169 .key_offset = offsetof(struct mlx5_flow_group, mask),
170 .head_offset = offsetof(struct mlx5_flow_group, hash),
171 .automatic_shrinking = true,
176 static void del_hw_flow_table(struct fs_node *node);
177 static void del_hw_flow_group(struct fs_node *node);
178 static void del_hw_fte(struct fs_node *node);
179 static void del_sw_flow_table(struct fs_node *node);
180 static void del_sw_flow_group(struct fs_node *node);
181 static void del_sw_fte(struct fs_node *node);
182 static void del_sw_prio(struct fs_node *node);
183 static void del_sw_ns(struct fs_node *node);
184 /* Delete rule (destination) is special case that
185 * requires to lock the FTE for all the deletion process.
187 static void del_sw_hw_rule(struct fs_node *node);
188 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
189 struct mlx5_flow_destination *d2);
190 static struct mlx5_flow_rule *
191 find_flow_rule(struct fs_fte *fte,
192 struct mlx5_flow_destination *dest);
194 static void tree_init_node(struct fs_node *node,
195 void (*del_hw_func)(struct fs_node *),
196 void (*del_sw_func)(struct fs_node *))
198 refcount_set(&node->refcount, 1);
199 INIT_LIST_HEAD(&node->list);
200 INIT_LIST_HEAD(&node->children);
201 init_rwsem(&node->lock);
202 node->del_hw_func = del_hw_func;
203 node->del_sw_func = del_sw_func;
204 node->active = false;
207 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
210 refcount_inc(&parent->refcount);
211 node->parent = parent;
213 /* Parent is the root */
217 node->root = parent->root;
220 static int tree_get_node(struct fs_node *node)
222 return refcount_inc_not_zero(&node->refcount);
225 static void nested_down_read_ref_node(struct fs_node *node,
226 enum fs_i_lock_class class)
229 down_read_nested(&node->lock, class);
230 refcount_inc(&node->refcount);
234 static void nested_down_write_ref_node(struct fs_node *node,
235 enum fs_i_lock_class class)
238 down_write_nested(&node->lock, class);
239 refcount_inc(&node->refcount);
243 static void down_write_ref_node(struct fs_node *node)
246 down_write(&node->lock);
247 refcount_inc(&node->refcount);
251 static void up_read_ref_node(struct fs_node *node)
253 refcount_dec(&node->refcount);
254 up_read(&node->lock);
257 static void up_write_ref_node(struct fs_node *node)
259 refcount_dec(&node->refcount);
260 up_write(&node->lock);
263 static void tree_put_node(struct fs_node *node)
265 struct fs_node *parent_node = node->parent;
267 if (refcount_dec_and_test(&node->refcount)) {
268 if (node->del_hw_func)
269 node->del_hw_func(node);
271 /* Only root namespace doesn't have parent and we just
272 * need to free its node.
274 down_write_ref_node(parent_node);
275 list_del_init(&node->list);
276 if (node->del_sw_func)
277 node->del_sw_func(node);
278 up_write_ref_node(parent_node);
284 if (!node && parent_node)
285 tree_put_node(parent_node);
288 static int tree_remove_node(struct fs_node *node)
290 if (refcount_read(&node->refcount) > 1) {
291 refcount_dec(&node->refcount);
298 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
301 struct fs_prio *iter_prio;
303 fs_for_each_prio(iter_prio, ns) {
304 if (iter_prio->prio == prio)
311 static bool check_last_reserved(const u32 *match_criteria)
313 char *match_criteria_reserved =
314 MLX5_ADDR_OF(fte_match_param, match_criteria, MLX5_FTE_MATCH_PARAM_RESERVED);
316 return !match_criteria_reserved[0] &&
317 !memcmp(match_criteria_reserved, match_criteria_reserved + 1,
318 MLX5_FLD_SZ_BYTES(fte_match_param,
319 MLX5_FTE_MATCH_PARAM_RESERVED) - 1);
322 static bool check_valid_mask(u8 match_criteria_enable, const u32 *match_criteria)
324 if (match_criteria_enable & ~(
325 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) |
326 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) |
327 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS) |
328 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS_2)))
331 if (!(match_criteria_enable &
332 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS)) {
333 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
334 match_criteria, outer_headers);
336 if (fg_type_mask[0] ||
337 memcmp(fg_type_mask, fg_type_mask + 1,
338 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4) - 1))
342 if (!(match_criteria_enable &
343 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS)) {
344 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
345 match_criteria, misc_parameters);
347 if (fg_type_mask[0] ||
348 memcmp(fg_type_mask, fg_type_mask + 1,
349 MLX5_ST_SZ_BYTES(fte_match_set_misc) - 1))
353 if (!(match_criteria_enable &
354 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS)) {
355 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
356 match_criteria, inner_headers);
358 if (fg_type_mask[0] ||
359 memcmp(fg_type_mask, fg_type_mask + 1,
360 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4) - 1))
364 if (!(match_criteria_enable &
365 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS_2)) {
366 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
367 match_criteria, misc_parameters_2);
369 if (fg_type_mask[0] ||
370 memcmp(fg_type_mask, fg_type_mask + 1,
371 MLX5_ST_SZ_BYTES(fte_match_set_misc2) - 1))
375 return check_last_reserved(match_criteria);
378 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
382 if (!check_valid_mask(spec->match_criteria_enable, spec->match_criteria)) {
383 pr_warn("mlx5_core: Match criteria given mismatches match_criteria_enable\n");
387 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
388 if (spec->match_value[i] & ~spec->match_criteria[i]) {
389 pr_warn("mlx5_core: match_value differs from match_criteria\n");
393 return check_last_reserved(spec->match_value);
396 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
398 struct fs_node *root;
399 struct mlx5_flow_namespace *ns;
403 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
404 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
408 ns = container_of(root, struct mlx5_flow_namespace, node);
409 return container_of(ns, struct mlx5_flow_root_namespace, ns);
412 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
414 struct mlx5_flow_root_namespace *root = find_root(node);
417 return root->dev->priv.steering;
421 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
423 struct mlx5_flow_root_namespace *root = find_root(node);
430 static void del_sw_ns(struct fs_node *node)
435 static void del_sw_prio(struct fs_node *node)
440 static void del_hw_flow_table(struct fs_node *node)
442 struct mlx5_flow_root_namespace *root;
443 struct mlx5_flow_table *ft;
444 struct mlx5_core_dev *dev;
447 fs_get_obj(ft, node);
448 dev = get_dev(&ft->node);
449 root = find_root(&ft->node);
452 err = root->cmds->destroy_flow_table(dev, ft);
454 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
458 static void del_sw_flow_table(struct fs_node *node)
460 struct mlx5_flow_table *ft;
461 struct fs_prio *prio;
463 fs_get_obj(ft, node);
465 rhltable_destroy(&ft->fgs_hash);
466 fs_get_obj(prio, ft->node.parent);
471 static void del_sw_hw_rule(struct fs_node *node)
473 struct mlx5_flow_root_namespace *root;
474 struct mlx5_flow_rule *rule;
475 struct mlx5_flow_table *ft;
476 struct mlx5_flow_group *fg;
479 struct mlx5_core_dev *dev = get_dev(node);
481 bool update_fte = false;
483 fs_get_obj(rule, node);
484 fs_get_obj(fte, rule->node.parent);
485 fs_get_obj(fg, fte->node.parent);
486 fs_get_obj(ft, fg->node.parent);
487 trace_mlx5_fs_del_rule(rule);
488 if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
489 mutex_lock(&rule->dest_attr.ft->lock);
490 list_del(&rule->next_ft);
491 mutex_unlock(&rule->dest_attr.ft->lock);
494 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
496 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
497 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
502 if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
504 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
508 root = find_root(&ft->node);
509 if (update_fte && fte->dests_size) {
510 err = root->cmds->update_fte(dev, ft, fg->id, modify_mask, fte);
513 "%s can't del rule fg id=%d fte_index=%d\n",
514 __func__, fg->id, fte->index);
519 static void del_hw_fte(struct fs_node *node)
521 struct mlx5_flow_root_namespace *root;
522 struct mlx5_flow_table *ft;
523 struct mlx5_flow_group *fg;
524 struct mlx5_core_dev *dev;
528 fs_get_obj(fte, node);
529 fs_get_obj(fg, fte->node.parent);
530 fs_get_obj(ft, fg->node.parent);
532 trace_mlx5_fs_del_fte(fte);
533 dev = get_dev(&ft->node);
534 root = find_root(&ft->node);
536 err = root->cmds->delete_fte(dev, ft, fte);
539 "flow steering can't delete fte in index %d of flow group id %d\n",
544 static void del_sw_fte(struct fs_node *node)
546 struct mlx5_flow_steering *steering = get_steering(node);
547 struct mlx5_flow_group *fg;
551 fs_get_obj(fte, node);
552 fs_get_obj(fg, fte->node.parent);
554 err = rhashtable_remove_fast(&fg->ftes_hash,
558 ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
559 kmem_cache_free(steering->ftes_cache, fte);
562 static void del_hw_flow_group(struct fs_node *node)
564 struct mlx5_flow_root_namespace *root;
565 struct mlx5_flow_group *fg;
566 struct mlx5_flow_table *ft;
567 struct mlx5_core_dev *dev;
569 fs_get_obj(fg, node);
570 fs_get_obj(ft, fg->node.parent);
571 dev = get_dev(&ft->node);
572 trace_mlx5_fs_del_fg(fg);
574 root = find_root(&ft->node);
575 if (fg->node.active && root->cmds->destroy_flow_group(dev, ft, fg->id))
576 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
580 static void del_sw_flow_group(struct fs_node *node)
582 struct mlx5_flow_steering *steering = get_steering(node);
583 struct mlx5_flow_group *fg;
584 struct mlx5_flow_table *ft;
587 fs_get_obj(fg, node);
588 fs_get_obj(ft, fg->node.parent);
590 rhashtable_destroy(&fg->ftes_hash);
591 ida_destroy(&fg->fte_allocator);
592 if (ft->autogroup.active)
593 ft->autogroup.num_groups--;
594 err = rhltable_remove(&ft->fgs_hash,
598 kmem_cache_free(steering->fgs_cache, fg);
601 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
606 index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
610 fte->index = index + fg->start_index;
611 ret = rhashtable_insert_fast(&fg->ftes_hash,
617 tree_add_node(&fte->node, &fg->node);
618 list_add_tail(&fte->node.list, &fg->node.children);
622 ida_simple_remove(&fg->fte_allocator, index);
626 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
628 struct mlx5_flow_act *flow_act)
630 struct mlx5_flow_steering *steering = get_steering(&ft->node);
633 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
635 return ERR_PTR(-ENOMEM);
637 memcpy(fte->val, match_value, sizeof(fte->val));
638 fte->node.type = FS_TYPE_FLOW_ENTRY;
639 fte->action = *flow_act;
641 tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
646 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
647 struct mlx5_flow_group *fg)
649 rhashtable_destroy(&fg->ftes_hash);
650 kmem_cache_free(steering->fgs_cache, fg);
653 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
654 u8 match_criteria_enable,
655 void *match_criteria,
659 struct mlx5_flow_group *fg;
662 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
664 return ERR_PTR(-ENOMEM);
666 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
668 kmem_cache_free(steering->fgs_cache, fg);
671 ida_init(&fg->fte_allocator);
672 fg->mask.match_criteria_enable = match_criteria_enable;
673 memcpy(&fg->mask.match_criteria, match_criteria,
674 sizeof(fg->mask.match_criteria));
675 fg->node.type = FS_TYPE_FLOW_GROUP;
676 fg->start_index = start_index;
677 fg->max_ftes = end_index - start_index + 1;
682 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
683 u8 match_criteria_enable,
684 void *match_criteria,
687 struct list_head *prev)
689 struct mlx5_flow_steering *steering = get_steering(&ft->node);
690 struct mlx5_flow_group *fg;
693 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
694 start_index, end_index);
698 /* initialize refcnt, add to parent list */
699 ret = rhltable_insert(&ft->fgs_hash,
703 dealloc_flow_group(steering, fg);
707 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
708 tree_add_node(&fg->node, &ft->node);
709 /* Add node to group list */
710 list_add(&fg->node.list, prev);
711 atomic_inc(&ft->node.version);
716 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
717 enum fs_flow_table_type table_type,
718 enum fs_flow_table_op_mod op_mod,
721 struct mlx5_flow_table *ft;
724 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
726 return ERR_PTR(-ENOMEM);
728 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
735 ft->node.type = FS_TYPE_FLOW_TABLE;
737 ft->type = table_type;
739 ft->max_fte = max_fte;
741 INIT_LIST_HEAD(&ft->fwd_rules);
742 mutex_init(&ft->lock);
747 /* If reverse is false, then we search for the first flow table in the
748 * root sub-tree from start(closest from right), else we search for the
749 * last flow table in the root sub-tree till start(closest from left).
751 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
752 struct list_head *start,
755 #define list_advance_entry(pos, reverse) \
756 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
758 #define list_for_each_advance_continue(pos, head, reverse) \
759 for (pos = list_advance_entry(pos, reverse); \
760 &pos->list != (head); \
761 pos = list_advance_entry(pos, reverse))
763 struct fs_node *iter = list_entry(start, struct fs_node, list);
764 struct mlx5_flow_table *ft = NULL;
769 list_for_each_advance_continue(iter, &root->children, reverse) {
770 if (iter->type == FS_TYPE_FLOW_TABLE) {
771 fs_get_obj(ft, iter);
774 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
782 /* If reverse if false then return the first flow table in next priority of
783 * prio in the tree, else return the last flow table in the previous priority
784 * of prio in the tree.
786 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
788 struct mlx5_flow_table *ft = NULL;
789 struct fs_node *curr_node;
790 struct fs_node *parent;
792 parent = prio->node.parent;
793 curr_node = &prio->node;
794 while (!ft && parent) {
795 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
797 parent = curr_node->parent;
802 /* Assuming all the tree is locked by mutex chain lock */
803 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
805 return find_closest_ft(prio, false);
808 /* Assuming all the tree is locked by mutex chain lock */
809 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
811 return find_closest_ft(prio, true);
814 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
815 struct fs_prio *prio,
816 struct mlx5_flow_table *ft)
818 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
819 struct mlx5_flow_table *iter;
823 fs_for_each_ft(iter, prio) {
825 err = root->cmds->modify_flow_table(dev, iter, ft);
827 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
829 /* The driver is out of sync with the FW */
838 /* Connect flow tables from previous priority of prio to ft */
839 static int connect_prev_fts(struct mlx5_core_dev *dev,
840 struct mlx5_flow_table *ft,
841 struct fs_prio *prio)
843 struct mlx5_flow_table *prev_ft;
845 prev_ft = find_prev_chained_ft(prio);
847 struct fs_prio *prev_prio;
849 fs_get_obj(prev_prio, prev_ft->node.parent);
850 return connect_fts_in_prio(dev, prev_prio, ft);
855 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
858 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
859 struct mlx5_ft_underlay_qp *uqp;
860 int min_level = INT_MAX;
865 min_level = root->root_ft->level;
867 if (ft->level >= min_level)
870 if (list_empty(&root->underlay_qpns)) {
871 /* Don't set any QPN (zero) in case QPN list is empty */
873 err = root->cmds->update_root_ft(root->dev, ft, qpn, false);
875 list_for_each_entry(uqp, &root->underlay_qpns, list) {
877 err = root->cmds->update_root_ft(root->dev, ft,
885 mlx5_core_warn(root->dev,
886 "Update root flow table of id(%u) qpn(%d) failed\n",
894 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
895 struct mlx5_flow_destination *dest)
897 struct mlx5_flow_root_namespace *root;
898 struct mlx5_flow_table *ft;
899 struct mlx5_flow_group *fg;
901 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
904 fs_get_obj(fte, rule->node.parent);
905 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
907 down_write_ref_node(&fte->node);
908 fs_get_obj(fg, fte->node.parent);
909 fs_get_obj(ft, fg->node.parent);
911 memcpy(&rule->dest_attr, dest, sizeof(*dest));
912 root = find_root(&ft->node);
913 err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
915 up_write_ref_node(&fte->node);
920 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
921 struct mlx5_flow_destination *new_dest,
922 struct mlx5_flow_destination *old_dest)
927 if (handle->num_rules != 1)
929 return _mlx5_modify_rule_destination(handle->rule[0],
933 for (i = 0; i < handle->num_rules; i++) {
934 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
935 return _mlx5_modify_rule_destination(handle->rule[i],
942 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
943 static int connect_fwd_rules(struct mlx5_core_dev *dev,
944 struct mlx5_flow_table *new_next_ft,
945 struct mlx5_flow_table *old_next_ft)
947 struct mlx5_flow_destination dest = {};
948 struct mlx5_flow_rule *iter;
951 /* new_next_ft and old_next_ft could be NULL only
952 * when we create/destroy the anchor flow table.
954 if (!new_next_ft || !old_next_ft)
957 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
958 dest.ft = new_next_ft;
960 mutex_lock(&old_next_ft->lock);
961 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
962 mutex_unlock(&old_next_ft->lock);
963 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
964 err = _mlx5_modify_rule_destination(iter, &dest);
966 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
972 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
973 struct fs_prio *prio)
975 struct mlx5_flow_table *next_ft;
978 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
980 if (list_empty(&prio->node.children)) {
981 err = connect_prev_fts(dev, ft, prio);
985 next_ft = find_next_chained_ft(prio);
986 err = connect_fwd_rules(dev, ft, next_ft);
991 if (MLX5_CAP_FLOWTABLE(dev,
992 flow_table_properties_nic_receive.modify_root))
993 err = update_root_ft_create(ft, prio);
997 static void list_add_flow_table(struct mlx5_flow_table *ft,
998 struct fs_prio *prio)
1000 struct list_head *prev = &prio->node.children;
1001 struct mlx5_flow_table *iter;
1003 fs_for_each_ft(iter, prio) {
1004 if (iter->level > ft->level)
1006 prev = &iter->node.list;
1008 list_add(&ft->node.list, prev);
1011 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1012 struct mlx5_flow_table_attr *ft_attr,
1013 enum fs_flow_table_op_mod op_mod,
1016 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
1017 struct mlx5_flow_table *next_ft = NULL;
1018 struct fs_prio *fs_prio = NULL;
1019 struct mlx5_flow_table *ft;
1024 pr_err("mlx5: flow steering failed to find root of namespace\n");
1025 return ERR_PTR(-ENODEV);
1028 mutex_lock(&root->chain_lock);
1029 fs_prio = find_prio(ns, ft_attr->prio);
1034 if (ft_attr->level >= fs_prio->num_levels) {
1038 /* The level is related to the
1039 * priority level range.
1041 ft_attr->level += fs_prio->start_level;
1042 ft = alloc_flow_table(ft_attr->level,
1044 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
1046 op_mod, ft_attr->flags);
1052 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1053 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
1054 next_ft = find_next_chained_ft(fs_prio);
1055 err = root->cmds->create_flow_table(root->dev, ft->vport, ft->op_mod,
1056 ft->type, ft->level, log_table_sz,
1057 next_ft, &ft->id, ft->flags);
1061 err = connect_flow_table(root->dev, ft, fs_prio);
1064 ft->node.active = true;
1065 down_write_ref_node(&fs_prio->node);
1066 tree_add_node(&ft->node, &fs_prio->node);
1067 list_add_flow_table(ft, fs_prio);
1069 up_write_ref_node(&fs_prio->node);
1070 mutex_unlock(&root->chain_lock);
1073 root->cmds->destroy_flow_table(root->dev, ft);
1077 mutex_unlock(&root->chain_lock);
1078 return ERR_PTR(err);
1081 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1082 struct mlx5_flow_table_attr *ft_attr)
1084 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1087 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1088 int prio, int max_fte,
1089 u32 level, u16 vport)
1091 struct mlx5_flow_table_attr ft_attr = {};
1093 ft_attr.max_fte = max_fte;
1094 ft_attr.level = level;
1095 ft_attr.prio = prio;
1097 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1100 struct mlx5_flow_table*
1101 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1102 int prio, u32 level)
1104 struct mlx5_flow_table_attr ft_attr = {};
1106 ft_attr.level = level;
1107 ft_attr.prio = prio;
1108 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1110 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1112 struct mlx5_flow_table*
1113 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1115 int num_flow_table_entries,
1120 struct mlx5_flow_table_attr ft_attr = {};
1121 struct mlx5_flow_table *ft;
1123 if (max_num_groups > num_flow_table_entries)
1124 return ERR_PTR(-EINVAL);
1126 ft_attr.max_fte = num_flow_table_entries;
1127 ft_attr.prio = prio;
1128 ft_attr.level = level;
1129 ft_attr.flags = flags;
1131 ft = mlx5_create_flow_table(ns, &ft_attr);
1135 ft->autogroup.active = true;
1136 ft->autogroup.required_groups = max_num_groups;
1140 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1142 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1145 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1146 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1147 fg_in, match_criteria);
1148 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1150 match_criteria_enable);
1151 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1153 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1155 struct mlx5_core_dev *dev = get_dev(&ft->node);
1156 struct mlx5_flow_group *fg;
1159 if (!check_valid_mask(match_criteria_enable, match_criteria))
1160 return ERR_PTR(-EINVAL);
1162 if (ft->autogroup.active)
1163 return ERR_PTR(-EPERM);
1165 down_write_ref_node(&ft->node);
1166 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1167 start_index, end_index,
1168 ft->node.children.prev);
1169 up_write_ref_node(&ft->node);
1173 err = root->cmds->create_flow_group(dev, ft, fg_in, &fg->id);
1175 tree_put_node(&fg->node);
1176 return ERR_PTR(err);
1178 trace_mlx5_fs_add_fg(fg);
1179 fg->node.active = true;
1184 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1186 struct mlx5_flow_rule *rule;
1188 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1192 INIT_LIST_HEAD(&rule->next_ft);
1193 rule->node.type = FS_TYPE_FLOW_DEST;
1195 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1200 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1202 struct mlx5_flow_handle *handle;
1204 handle = kzalloc(sizeof(*handle) + sizeof(handle->rule[0]) *
1205 num_rules, GFP_KERNEL);
1209 handle->num_rules = num_rules;
1214 static void destroy_flow_handle(struct fs_fte *fte,
1215 struct mlx5_flow_handle *handle,
1216 struct mlx5_flow_destination *dest,
1220 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1222 list_del(&handle->rule[i]->node.list);
1223 kfree(handle->rule[i]);
1229 static struct mlx5_flow_handle *
1230 create_flow_handle(struct fs_fte *fte,
1231 struct mlx5_flow_destination *dest,
1236 struct mlx5_flow_handle *handle;
1237 struct mlx5_flow_rule *rule = NULL;
1238 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1239 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1243 handle = alloc_handle((dest_num) ? dest_num : 1);
1245 return ERR_PTR(-ENOMEM);
1249 rule = find_flow_rule(fte, dest + i);
1251 refcount_inc(&rule->node.refcount);
1257 rule = alloc_rule(dest + i);
1261 /* Add dest to dests list- we need flow tables to be in the
1262 * end of the list for forward to next prio rules.
1264 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1266 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1267 list_add(&rule->node.list, &fte->node.children);
1269 list_add_tail(&rule->node.list, &fte->node.children);
1273 type = dest[i].type ==
1274 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1275 *modify_mask |= type ? count : dst;
1278 handle->rule[i] = rule;
1279 } while (++i < dest_num);
1284 destroy_flow_handle(fte, handle, dest, i);
1285 return ERR_PTR(-ENOMEM);
1288 /* fte should not be deleted while calling this function */
1289 static struct mlx5_flow_handle *
1290 add_rule_fte(struct fs_fte *fte,
1291 struct mlx5_flow_group *fg,
1292 struct mlx5_flow_destination *dest,
1296 struct mlx5_flow_root_namespace *root;
1297 struct mlx5_flow_handle *handle;
1298 struct mlx5_flow_table *ft;
1299 int modify_mask = 0;
1301 bool new_rule = false;
1303 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1305 if (IS_ERR(handle) || !new_rule)
1309 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1311 fs_get_obj(ft, fg->node.parent);
1312 root = find_root(&fg->node);
1313 if (!(fte->status & FS_FTE_STATUS_EXISTING))
1314 err = root->cmds->create_fte(get_dev(&ft->node),
1317 err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
1322 fte->node.active = true;
1323 fte->status |= FS_FTE_STATUS_EXISTING;
1324 atomic_inc(&fte->node.version);
1330 destroy_flow_handle(fte, handle, dest, handle->num_rules);
1331 return ERR_PTR(err);
1334 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
1335 struct mlx5_flow_spec *spec)
1337 struct list_head *prev = &ft->node.children;
1338 struct mlx5_flow_group *fg;
1339 unsigned int candidate_index = 0;
1340 unsigned int group_size = 0;
1342 if (!ft->autogroup.active)
1343 return ERR_PTR(-ENOENT);
1345 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1346 /* We save place for flow groups in addition to max types */
1347 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
1349 /* ft->max_fte == ft->autogroup.max_types */
1350 if (group_size == 0)
1353 /* sorted by start_index */
1354 fs_for_each_fg(fg, ft) {
1355 if (candidate_index + group_size > fg->start_index)
1356 candidate_index = fg->start_index + fg->max_ftes;
1359 prev = &fg->node.list;
1362 if (candidate_index + group_size > ft->max_fte)
1363 return ERR_PTR(-ENOSPC);
1365 fg = alloc_insert_flow_group(ft,
1366 spec->match_criteria_enable,
1367 spec->match_criteria,
1369 candidate_index + group_size - 1,
1374 ft->autogroup.num_groups++;
1380 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1381 struct mlx5_flow_group *fg)
1383 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1384 struct mlx5_core_dev *dev = get_dev(&ft->node);
1385 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1386 void *match_criteria_addr;
1387 u8 src_esw_owner_mask_on;
1392 in = kvzalloc(inlen, GFP_KERNEL);
1396 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1397 fg->mask.match_criteria_enable);
1398 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1399 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1402 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1404 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1405 source_eswitch_owner_vhca_id);
1406 MLX5_SET(create_flow_group_in, in,
1407 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1409 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1410 in, match_criteria);
1411 memcpy(match_criteria_addr, fg->mask.match_criteria,
1412 sizeof(fg->mask.match_criteria));
1414 err = root->cmds->create_flow_group(dev, ft, in, &fg->id);
1416 fg->node.active = true;
1417 trace_mlx5_fs_add_fg(fg);
1424 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1425 struct mlx5_flow_destination *d2)
1427 if (d1->type == d2->type) {
1428 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1429 d1->vport.num == d2->vport.num) ||
1430 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1431 d1->ft == d2->ft) ||
1432 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1433 d1->tir_num == d2->tir_num))
1440 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1441 struct mlx5_flow_destination *dest)
1443 struct mlx5_flow_rule *rule;
1445 list_for_each_entry(rule, &fte->node.children, node.list) {
1446 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1452 static bool check_conflicting_actions(u32 action1, u32 action2)
1454 u32 xored_actions = action1 ^ action2;
1456 /* if one rule only wants to count, it's ok */
1457 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1458 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1461 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1462 MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1463 MLX5_FLOW_CONTEXT_ACTION_DECAP |
1464 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1465 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1466 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH))
1472 static int check_conflicting_ftes(struct fs_fte *fte, const struct mlx5_flow_act *flow_act)
1474 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
1475 mlx5_core_warn(get_dev(&fte->node),
1476 "Found two FTEs with conflicting actions\n");
1480 if (flow_act->has_flow_tag &&
1481 fte->action.flow_tag != flow_act->flow_tag) {
1482 mlx5_core_warn(get_dev(&fte->node),
1483 "FTE flow tag %u already exists with different flow tag %u\n",
1484 fte->action.flow_tag,
1485 flow_act->flow_tag);
1492 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1494 struct mlx5_flow_act *flow_act,
1495 struct mlx5_flow_destination *dest,
1499 struct mlx5_flow_handle *handle;
1504 ret = check_conflicting_ftes(fte, flow_act);
1506 return ERR_PTR(ret);
1508 old_action = fte->action.action;
1509 fte->action.action |= flow_act->action;
1510 handle = add_rule_fte(fte, fg, dest, dest_num,
1511 old_action != flow_act->action);
1512 if (IS_ERR(handle)) {
1513 fte->action.action = old_action;
1516 trace_mlx5_fs_set_fte(fte, false);
1518 for (i = 0; i < handle->num_rules; i++) {
1519 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1520 tree_add_node(&handle->rule[i]->node, &fte->node);
1521 trace_mlx5_fs_add_rule(handle->rule[i]);
1527 struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_handle *handle)
1529 struct mlx5_flow_rule *dst;
1532 fs_get_obj(fte, handle->rule[0]->node.parent);
1534 fs_for_each_dst(dst, fte) {
1535 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
1536 return dst->dest_attr.counter;
1542 static bool counter_is_valid(struct mlx5_fc *counter, u32 action)
1544 if (!(action & MLX5_FLOW_CONTEXT_ACTION_COUNT))
1550 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1551 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1554 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1556 struct mlx5_flow_table *ft)
1558 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1559 return counter_is_valid(dest->counter, action);
1561 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1564 if (!dest || ((dest->type ==
1565 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1566 (dest->ft->level <= ft->level)))
1572 struct list_head list;
1573 struct mlx5_flow_group *g;
1576 struct match_list_head {
1577 struct list_head list;
1578 struct match_list first;
1581 static void free_match_list(struct match_list_head *head)
1583 if (!list_empty(&head->list)) {
1584 struct match_list *iter, *match_tmp;
1586 list_del(&head->first.list);
1587 tree_put_node(&head->first.g->node);
1588 list_for_each_entry_safe(iter, match_tmp, &head->list,
1590 tree_put_node(&iter->g->node);
1591 list_del(&iter->list);
1597 static int build_match_list(struct match_list_head *match_head,
1598 struct mlx5_flow_table *ft,
1599 struct mlx5_flow_spec *spec)
1601 struct rhlist_head *tmp, *list;
1602 struct mlx5_flow_group *g;
1606 INIT_LIST_HEAD(&match_head->list);
1607 /* Collect all fgs which has a matching match_criteria */
1608 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1609 /* RCU is atomic, we can't execute FW commands here */
1610 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1611 struct match_list *curr_match;
1613 if (likely(list_empty(&match_head->list))) {
1614 if (!tree_get_node(&g->node))
1616 match_head->first.g = g;
1617 list_add_tail(&match_head->first.list,
1622 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1624 free_match_list(match_head);
1628 if (!tree_get_node(&g->node)) {
1633 list_add_tail(&curr_match->list, &match_head->list);
1640 static u64 matched_fgs_get_version(struct list_head *match_head)
1642 struct match_list *iter;
1645 list_for_each_entry(iter, match_head, list)
1646 version += (u64)atomic_read(&iter->g->node.version);
1650 static struct mlx5_flow_handle *
1651 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1652 struct list_head *match_head,
1653 struct mlx5_flow_spec *spec,
1654 struct mlx5_flow_act *flow_act,
1655 struct mlx5_flow_destination *dest,
1659 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1660 struct mlx5_flow_group *g;
1661 struct mlx5_flow_handle *rule;
1662 struct match_list *iter;
1663 bool take_write = false;
1668 fte = alloc_fte(ft, spec->match_value, flow_act);
1670 return ERR_PTR(-ENOMEM);
1672 list_for_each_entry(iter, match_head, list) {
1673 nested_down_read_ref_node(&iter->g->node, FS_LOCK_PARENT);
1676 search_again_locked:
1677 version = matched_fgs_get_version(match_head);
1678 /* Try to find a fg that already contains a matching fte */
1679 list_for_each_entry(iter, match_head, list) {
1680 struct fs_fte *fte_tmp;
1683 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, spec->match_value,
1685 if (!fte_tmp || !tree_get_node(&fte_tmp->node))
1688 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1690 list_for_each_entry(iter, match_head, list)
1691 up_read_ref_node(&iter->g->node);
1693 list_for_each_entry(iter, match_head, list)
1694 up_write_ref_node(&iter->g->node);
1697 rule = add_rule_fg(g, spec->match_value,
1698 flow_act, dest, dest_num, fte_tmp);
1699 up_write_ref_node(&fte_tmp->node);
1700 tree_put_node(&fte_tmp->node);
1701 kmem_cache_free(steering->ftes_cache, fte);
1705 /* No group with matching fte found. Try to add a new fte to any
1710 list_for_each_entry(iter, match_head, list)
1711 up_read_ref_node(&iter->g->node);
1712 list_for_each_entry(iter, match_head, list)
1713 nested_down_write_ref_node(&iter->g->node,
1718 /* Check the ft version, for case that new flow group
1719 * was added while the fgs weren't locked
1721 if (atomic_read(&ft->node.version) != ft_version) {
1722 rule = ERR_PTR(-EAGAIN);
1726 /* Check the fgs version, for case the new FTE with the
1727 * same values was added while the fgs weren't locked
1729 if (version != matched_fgs_get_version(match_head))
1730 goto search_again_locked;
1732 list_for_each_entry(iter, match_head, list) {
1735 if (!g->node.active)
1737 err = insert_fte(g, fte);
1741 list_for_each_entry(iter, match_head, list)
1742 up_write_ref_node(&iter->g->node);
1743 kmem_cache_free(steering->ftes_cache, fte);
1744 return ERR_PTR(err);
1747 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1748 list_for_each_entry(iter, match_head, list)
1749 up_write_ref_node(&iter->g->node);
1750 rule = add_rule_fg(g, spec->match_value,
1751 flow_act, dest, dest_num, fte);
1752 up_write_ref_node(&fte->node);
1753 tree_put_node(&fte->node);
1756 rule = ERR_PTR(-ENOENT);
1758 list_for_each_entry(iter, match_head, list)
1759 up_write_ref_node(&iter->g->node);
1760 kmem_cache_free(steering->ftes_cache, fte);
1764 static struct mlx5_flow_handle *
1765 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1766 struct mlx5_flow_spec *spec,
1767 struct mlx5_flow_act *flow_act,
1768 struct mlx5_flow_destination *dest,
1772 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1773 struct mlx5_flow_group *g;
1774 struct mlx5_flow_handle *rule;
1775 struct match_list_head match_head;
1776 bool take_write = false;
1782 if (!check_valid_spec(spec))
1783 return ERR_PTR(-EINVAL);
1785 for (i = 0; i < dest_num; i++) {
1786 if (!dest_is_valid(&dest[i], flow_act->action, ft))
1787 return ERR_PTR(-EINVAL);
1789 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1790 search_again_locked:
1791 version = atomic_read(&ft->node.version);
1793 /* Collect all fgs which has a matching match_criteria */
1794 err = build_match_list(&match_head, ft, spec);
1797 up_write_ref_node(&ft->node);
1798 return ERR_PTR(err);
1802 up_read_ref_node(&ft->node);
1804 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1806 free_match_list(&match_head);
1807 if (!IS_ERR(rule) ||
1808 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1810 up_write_ref_node(&ft->node);
1815 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1819 if (PTR_ERR(rule) == -EAGAIN ||
1820 version != atomic_read(&ft->node.version))
1821 goto search_again_locked;
1823 g = alloc_auto_flow_group(ft, spec);
1826 up_write_ref_node(&ft->node);
1830 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1831 up_write_ref_node(&ft->node);
1833 err = create_auto_flow_group(ft, g);
1835 goto err_release_fg;
1837 fte = alloc_fte(ft, spec->match_value, flow_act);
1840 goto err_release_fg;
1843 err = insert_fte(g, fte);
1845 kmem_cache_free(steering->ftes_cache, fte);
1846 goto err_release_fg;
1849 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1850 up_write_ref_node(&g->node);
1851 rule = add_rule_fg(g, spec->match_value, flow_act, dest,
1853 up_write_ref_node(&fte->node);
1854 tree_put_node(&fte->node);
1855 tree_put_node(&g->node);
1859 up_write_ref_node(&g->node);
1860 tree_put_node(&g->node);
1861 return ERR_PTR(err);
1864 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1866 return ((ft->type == FS_FT_NIC_RX) &&
1867 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1870 struct mlx5_flow_handle *
1871 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1872 struct mlx5_flow_spec *spec,
1873 struct mlx5_flow_act *flow_act,
1874 struct mlx5_flow_destination *dest,
1877 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1878 struct mlx5_flow_destination gen_dest = {};
1879 struct mlx5_flow_table *next_ft = NULL;
1880 struct mlx5_flow_handle *handle = NULL;
1881 u32 sw_action = flow_act->action;
1882 struct fs_prio *prio;
1884 fs_get_obj(prio, ft->node.parent);
1885 if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1886 if (!fwd_next_prio_supported(ft))
1887 return ERR_PTR(-EOPNOTSUPP);
1889 return ERR_PTR(-EINVAL);
1890 mutex_lock(&root->chain_lock);
1891 next_ft = find_next_chained_ft(prio);
1893 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1894 gen_dest.ft = next_ft;
1897 flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1899 mutex_unlock(&root->chain_lock);
1900 return ERR_PTR(-EOPNOTSUPP);
1904 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, dest_num);
1906 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1907 if (!IS_ERR_OR_NULL(handle) &&
1908 (list_empty(&handle->rule[0]->next_ft))) {
1909 mutex_lock(&next_ft->lock);
1910 list_add(&handle->rule[0]->next_ft,
1911 &next_ft->fwd_rules);
1912 mutex_unlock(&next_ft->lock);
1913 handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1915 mutex_unlock(&root->chain_lock);
1919 EXPORT_SYMBOL(mlx5_add_flow_rules);
1921 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1925 for (i = handle->num_rules - 1; i >= 0; i--)
1926 tree_remove_node(&handle->rule[i]->node);
1929 EXPORT_SYMBOL(mlx5_del_flow_rules);
1931 /* Assuming prio->node.children(flow tables) is sorted by level */
1932 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1934 struct fs_prio *prio;
1936 fs_get_obj(prio, ft->node.parent);
1938 if (!list_is_last(&ft->node.list, &prio->node.children))
1939 return list_next_entry(ft, node.list);
1940 return find_next_chained_ft(prio);
1943 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1945 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1946 struct mlx5_ft_underlay_qp *uqp;
1947 struct mlx5_flow_table *new_root_ft = NULL;
1951 if (root->root_ft != ft)
1954 new_root_ft = find_next_ft(ft);
1956 root->root_ft = NULL;
1960 if (list_empty(&root->underlay_qpns)) {
1961 /* Don't set any QPN (zero) in case QPN list is empty */
1963 err = root->cmds->update_root_ft(root->dev, new_root_ft,
1966 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1968 err = root->cmds->update_root_ft(root->dev,
1977 mlx5_core_warn(root->dev,
1978 "Update root flow table of id(%u) qpn(%d) failed\n",
1981 root->root_ft = new_root_ft;
1986 /* Connect flow table from previous priority to
1987 * the next flow table.
1989 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1991 struct mlx5_core_dev *dev = get_dev(&ft->node);
1992 struct mlx5_flow_table *next_ft;
1993 struct fs_prio *prio;
1996 err = update_root_ft_destroy(ft);
2000 fs_get_obj(prio, ft->node.parent);
2001 if (!(list_first_entry(&prio->node.children,
2002 struct mlx5_flow_table,
2006 next_ft = find_next_chained_ft(prio);
2007 err = connect_fwd_rules(dev, next_ft, ft);
2011 err = connect_prev_fts(dev, next_ft, prio);
2013 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2018 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
2020 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2023 mutex_lock(&root->chain_lock);
2024 err = disconnect_flow_table(ft);
2026 mutex_unlock(&root->chain_lock);
2029 if (tree_remove_node(&ft->node))
2030 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2032 mutex_unlock(&root->chain_lock);
2036 EXPORT_SYMBOL(mlx5_destroy_flow_table);
2038 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
2040 if (tree_remove_node(&fg->node))
2041 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2045 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2046 enum mlx5_flow_namespace_type type)
2048 struct mlx5_flow_steering *steering = dev->priv.steering;
2049 struct mlx5_flow_root_namespace *root_ns;
2051 struct fs_prio *fs_prio;
2052 struct mlx5_flow_namespace *ns;
2058 case MLX5_FLOW_NAMESPACE_BYPASS:
2059 case MLX5_FLOW_NAMESPACE_LAG:
2060 case MLX5_FLOW_NAMESPACE_OFFLOADS:
2061 case MLX5_FLOW_NAMESPACE_ETHTOOL:
2062 case MLX5_FLOW_NAMESPACE_KERNEL:
2063 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
2064 case MLX5_FLOW_NAMESPACE_ANCHOR:
2067 case MLX5_FLOW_NAMESPACE_FDB:
2068 if (steering->fdb_root_ns)
2069 return &steering->fdb_root_ns->ns;
2072 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2073 if (steering->sniffer_rx_root_ns)
2074 return &steering->sniffer_rx_root_ns->ns;
2077 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2078 if (steering->sniffer_tx_root_ns)
2079 return &steering->sniffer_tx_root_ns->ns;
2082 case MLX5_FLOW_NAMESPACE_EGRESS:
2083 if (steering->egress_root_ns)
2084 return &steering->egress_root_ns->ns;
2091 root_ns = steering->root_ns;
2095 fs_prio = find_prio(&root_ns->ns, prio);
2099 ns = list_first_entry(&fs_prio->node.children,
2105 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2107 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2108 enum mlx5_flow_namespace_type type,
2111 struct mlx5_flow_steering *steering = dev->priv.steering;
2113 if (!steering || vport >= MLX5_TOTAL_VPORTS(dev))
2117 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2118 if (steering->esw_egress_root_ns &&
2119 steering->esw_egress_root_ns[vport])
2120 return &steering->esw_egress_root_ns[vport]->ns;
2123 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2124 if (steering->esw_ingress_root_ns &&
2125 steering->esw_ingress_root_ns[vport])
2126 return &steering->esw_ingress_root_ns[vport]->ns;
2134 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2135 unsigned int prio, int num_levels)
2137 struct fs_prio *fs_prio;
2139 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2141 return ERR_PTR(-ENOMEM);
2143 fs_prio->node.type = FS_TYPE_PRIO;
2144 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2145 tree_add_node(&fs_prio->node, &ns->node);
2146 fs_prio->num_levels = num_levels;
2147 fs_prio->prio = prio;
2148 list_add_tail(&fs_prio->node.list, &ns->node.children);
2153 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2156 ns->node.type = FS_TYPE_NAMESPACE;
2161 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2163 struct mlx5_flow_namespace *ns;
2165 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2167 return ERR_PTR(-ENOMEM);
2169 fs_init_namespace(ns);
2170 tree_init_node(&ns->node, NULL, del_sw_ns);
2171 tree_add_node(&ns->node, &prio->node);
2172 list_add_tail(&ns->node.list, &prio->node.children);
2177 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2178 struct init_tree_node *prio_metadata)
2180 struct fs_prio *fs_prio;
2183 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2184 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2185 if (IS_ERR(fs_prio))
2186 return PTR_ERR(fs_prio);
2191 #define FLOW_TABLE_BIT_SZ 1
2192 #define GET_FLOW_TABLE_CAP(dev, offset) \
2193 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
2195 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2196 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2200 for (i = 0; i < caps->arr_sz; i++) {
2201 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2207 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2208 struct init_tree_node *init_node,
2209 struct fs_node *fs_parent_node,
2210 struct init_tree_node *init_parent_node,
2213 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2214 flow_table_properties_nic_receive.
2216 struct mlx5_flow_namespace *fs_ns;
2217 struct fs_prio *fs_prio;
2218 struct fs_node *base;
2222 if (init_node->type == FS_TYPE_PRIO) {
2223 if ((init_node->min_ft_level > max_ft_level) ||
2224 !has_required_caps(steering->dev, &init_node->caps))
2227 fs_get_obj(fs_ns, fs_parent_node);
2228 if (init_node->num_leaf_prios)
2229 return create_leaf_prios(fs_ns, prio, init_node);
2230 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2231 if (IS_ERR(fs_prio))
2232 return PTR_ERR(fs_prio);
2233 base = &fs_prio->node;
2234 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2235 fs_get_obj(fs_prio, fs_parent_node);
2236 fs_ns = fs_create_namespace(fs_prio);
2238 return PTR_ERR(fs_ns);
2239 base = &fs_ns->node;
2244 for (i = 0; i < init_node->ar_size; i++) {
2245 err = init_root_tree_recursive(steering, &init_node->children[i],
2246 base, init_node, prio);
2249 if (init_node->children[i].type == FS_TYPE_PRIO &&
2250 init_node->children[i].num_leaf_prios) {
2251 prio += init_node->children[i].num_leaf_prios;
2258 static int init_root_tree(struct mlx5_flow_steering *steering,
2259 struct init_tree_node *init_node,
2260 struct fs_node *fs_parent_node)
2263 struct mlx5_flow_namespace *fs_ns;
2266 fs_get_obj(fs_ns, fs_parent_node);
2267 for (i = 0; i < init_node->ar_size; i++) {
2268 err = init_root_tree_recursive(steering, &init_node->children[i],
2277 static struct mlx5_flow_root_namespace
2278 *create_root_ns(struct mlx5_flow_steering *steering,
2279 enum fs_flow_table_type table_type)
2281 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2282 struct mlx5_flow_root_namespace *root_ns;
2283 struct mlx5_flow_namespace *ns;
2285 if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2286 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2287 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2289 /* Create the root namespace */
2290 root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
2294 root_ns->dev = steering->dev;
2295 root_ns->table_type = table_type;
2296 root_ns->cmds = cmds;
2298 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2301 fs_init_namespace(ns);
2302 mutex_init(&root_ns->chain_lock);
2303 tree_init_node(&ns->node, NULL, NULL);
2304 tree_add_node(&ns->node, NULL);
2309 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2311 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2313 struct fs_prio *prio;
2315 fs_for_each_prio(prio, ns) {
2316 /* This updates prio start_level and num_levels */
2317 set_prio_attrs_in_prio(prio, acc_level);
2318 acc_level += prio->num_levels;
2323 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2325 struct mlx5_flow_namespace *ns;
2326 int acc_level_ns = acc_level;
2328 prio->start_level = acc_level;
2329 fs_for_each_ns(ns, prio)
2330 /* This updates start_level and num_levels of ns's priority descendants */
2331 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2332 if (!prio->num_levels)
2333 prio->num_levels = acc_level_ns - prio->start_level;
2334 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2337 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2339 struct mlx5_flow_namespace *ns = &root_ns->ns;
2340 struct fs_prio *prio;
2341 int start_level = 0;
2343 fs_for_each_prio(prio, ns) {
2344 set_prio_attrs_in_prio(prio, start_level);
2345 start_level += prio->num_levels;
2349 #define ANCHOR_PRIO 0
2350 #define ANCHOR_SIZE 1
2351 #define ANCHOR_LEVEL 0
2352 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2354 struct mlx5_flow_namespace *ns = NULL;
2355 struct mlx5_flow_table_attr ft_attr = {};
2356 struct mlx5_flow_table *ft;
2358 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2362 ft_attr.max_fte = ANCHOR_SIZE;
2363 ft_attr.level = ANCHOR_LEVEL;
2364 ft_attr.prio = ANCHOR_PRIO;
2366 ft = mlx5_create_flow_table(ns, &ft_attr);
2368 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2374 static int init_root_ns(struct mlx5_flow_steering *steering)
2376 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2377 if (!steering->root_ns)
2380 if (init_root_tree(steering, &root_fs, &steering->root_ns->ns.node))
2383 set_prio_attrs(steering->root_ns);
2385 if (create_anchor_flow_table(steering))
2391 mlx5_cleanup_fs(steering->dev);
2395 static void clean_tree(struct fs_node *node)
2398 struct fs_node *iter;
2399 struct fs_node *temp;
2401 tree_get_node(node);
2402 list_for_each_entry_safe(iter, temp, &node->children, list)
2404 tree_put_node(node);
2405 tree_remove_node(node);
2409 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2414 clean_tree(&root_ns->ns.node);
2417 static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2419 struct mlx5_flow_steering *steering = dev->priv.steering;
2422 if (!steering->esw_egress_root_ns)
2425 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2426 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2428 kfree(steering->esw_egress_root_ns);
2431 static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2433 struct mlx5_flow_steering *steering = dev->priv.steering;
2436 if (!steering->esw_ingress_root_ns)
2439 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2440 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2442 kfree(steering->esw_ingress_root_ns);
2445 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2447 struct mlx5_flow_steering *steering = dev->priv.steering;
2449 cleanup_root_ns(steering->root_ns);
2450 cleanup_egress_acls_root_ns(dev);
2451 cleanup_ingress_acls_root_ns(dev);
2452 cleanup_root_ns(steering->fdb_root_ns);
2453 cleanup_root_ns(steering->sniffer_rx_root_ns);
2454 cleanup_root_ns(steering->sniffer_tx_root_ns);
2455 cleanup_root_ns(steering->egress_root_ns);
2456 mlx5_cleanup_fc_stats(dev);
2457 kmem_cache_destroy(steering->ftes_cache);
2458 kmem_cache_destroy(steering->fgs_cache);
2462 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2464 struct fs_prio *prio;
2466 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2467 if (!steering->sniffer_tx_root_ns)
2470 /* Create single prio */
2471 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2473 cleanup_root_ns(steering->sniffer_tx_root_ns);
2474 return PTR_ERR(prio);
2479 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2481 struct fs_prio *prio;
2483 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2484 if (!steering->sniffer_rx_root_ns)
2487 /* Create single prio */
2488 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2490 cleanup_root_ns(steering->sniffer_rx_root_ns);
2491 return PTR_ERR(prio);
2496 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2498 struct fs_prio *prio;
2500 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2501 if (!steering->fdb_root_ns)
2504 prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 1);
2508 prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2512 set_prio_attrs(steering->fdb_root_ns);
2516 cleanup_root_ns(steering->fdb_root_ns);
2517 steering->fdb_root_ns = NULL;
2518 return PTR_ERR(prio);
2521 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2523 struct fs_prio *prio;
2525 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2526 if (!steering->esw_egress_root_ns[vport])
2530 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2531 return PTR_ERR_OR_ZERO(prio);
2534 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2536 struct fs_prio *prio;
2538 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2539 if (!steering->esw_ingress_root_ns[vport])
2543 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2544 return PTR_ERR_OR_ZERO(prio);
2547 static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2549 struct mlx5_flow_steering *steering = dev->priv.steering;
2553 steering->esw_egress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2554 sizeof(*steering->esw_egress_root_ns),
2556 if (!steering->esw_egress_root_ns)
2559 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2560 err = init_egress_acl_root_ns(steering, i);
2562 goto cleanup_root_ns;
2568 for (i--; i >= 0; i--)
2569 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2570 kfree(steering->esw_egress_root_ns);
2574 static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2576 struct mlx5_flow_steering *steering = dev->priv.steering;
2580 steering->esw_ingress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2581 sizeof(*steering->esw_ingress_root_ns),
2583 if (!steering->esw_ingress_root_ns)
2586 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2587 err = init_ingress_acl_root_ns(steering, i);
2589 goto cleanup_root_ns;
2595 for (i--; i >= 0; i--)
2596 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2597 kfree(steering->esw_ingress_root_ns);
2601 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2603 struct fs_prio *prio;
2605 steering->egress_root_ns = create_root_ns(steering,
2607 if (!steering->egress_root_ns)
2611 prio = fs_create_prio(&steering->egress_root_ns->ns, 0, 1);
2612 return PTR_ERR_OR_ZERO(prio);
2615 int mlx5_init_fs(struct mlx5_core_dev *dev)
2617 struct mlx5_flow_steering *steering;
2620 err = mlx5_init_fc_stats(dev);
2624 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2627 steering->dev = dev;
2628 dev->priv.steering = steering;
2630 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2631 sizeof(struct mlx5_flow_group), 0,
2633 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2635 if (!steering->ftes_cache || !steering->fgs_cache) {
2640 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2641 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2642 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2643 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2644 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2645 err = init_root_ns(steering);
2650 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
2651 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2652 err = init_fdb_root_ns(steering);
2656 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2657 err = init_egress_acls_root_ns(dev);
2661 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2662 err = init_ingress_acls_root_ns(dev);
2668 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2669 err = init_sniffer_rx_root_ns(steering);
2674 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2675 err = init_sniffer_tx_root_ns(steering);
2680 if (MLX5_IPSEC_DEV(dev)) {
2681 err = init_egress_root_ns(steering);
2688 mlx5_cleanup_fs(dev);
2692 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2694 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2695 struct mlx5_ft_underlay_qp *new_uqp;
2698 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2702 mutex_lock(&root->chain_lock);
2704 if (!root->root_ft) {
2706 goto update_ft_fail;
2709 err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2712 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2714 goto update_ft_fail;
2717 new_uqp->qpn = underlay_qpn;
2718 list_add_tail(&new_uqp->list, &root->underlay_qpns);
2720 mutex_unlock(&root->chain_lock);
2725 mutex_unlock(&root->chain_lock);
2729 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2731 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2733 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2734 struct mlx5_ft_underlay_qp *uqp;
2738 mutex_lock(&root->chain_lock);
2739 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2740 if (uqp->qpn == underlay_qpn) {
2747 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2753 err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2756 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2759 list_del(&uqp->list);
2760 mutex_unlock(&root->chain_lock);
2766 mutex_unlock(&root->chain_lock);
2769 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);