]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/mellanox/mlx5/core/fs_core.c
f77e496f7053e026f34aae40038b5a843e3b4bb0
[linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_core.c
1 /*
2  * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32
33 #include <linux/mutex.h>
34 #include <linux/mlx5/driver.h>
35
36 #include "mlx5_core.h"
37 #include "fs_core.h"
38 #include "fs_cmd.h"
39 #include "diag/fs_tracepoint.h"
40
41 #define INIT_TREE_NODE_ARRAY_SIZE(...)  (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
42                                          sizeof(struct init_tree_node))
43
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,\
49         .caps = caps_val,\
50         .children = (struct init_tree_node[]) {__VA_ARGS__},\
51         .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
52 }
53
54 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
55         ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
56                  __VA_ARGS__)\
57
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__) \
61 }
62
63 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
64                                    sizeof(long))
65
66 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
67
68 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
69                                .caps = (long[]) {__VA_ARGS__} }
70
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))
75
76 #define LEFTOVERS_NUM_LEVELS 1
77 #define LEFTOVERS_NUM_PRIOS 1
78
79 #define BY_PASS_PRIO_NUM_LEVELS 1
80 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
81                            LEFTOVERS_NUM_PRIOS)
82
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)
91
92 #define ANCHOR_NUM_LEVELS 1
93 #define ANCHOR_NUM_PRIOS 1
94 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
95
96 #define OFFLOADS_MAX_FT 1
97 #define OFFLOADS_NUM_PRIOS 1
98 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
99
100 #define LAG_PRIO_NUM_LEVELS 1
101 #define LAG_NUM_PRIOS 1
102 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
103
104 struct node_caps {
105         size_t  arr_sz;
106         long    *caps;
107 };
108
109 static struct init_tree_node {
110         enum fs_node_type       type;
111         struct init_tree_node *children;
112         int ar_size;
113         struct node_caps caps;
114         int min_ft_level;
115         int num_leaf_prios;
116         int prio;
117         int num_levels;
118 } root_fs = {
119         .type = FS_TYPE_NAMESPACE,
120         .ar_size = 7,
121         .children = (struct init_tree_node[]) {
122                 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
123                          FS_CHAINING_CAPS,
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,
127                          FS_CHAINING_CAPS,
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,
133                          FS_CHAINING_CAPS,
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,
141                          FS_CHAINING_CAPS,
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))),
145         }
146 };
147
148 enum fs_i_lock_class {
149         FS_LOCK_GRANDPARENT,
150         FS_LOCK_PARENT,
151         FS_LOCK_CHILD
152 };
153
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,
159         .min_size = 1,
160 };
161
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,
167         .min_size = 1,
168
169 };
170
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.
179  */
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);
186
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 *))
190 {
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;
198 }
199
200 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
201 {
202         if (parent)
203                 atomic_inc(&parent->refcount);
204         node->parent = parent;
205
206         /* Parent is the root */
207         if (!parent)
208                 node->root = node;
209         else
210                 node->root = parent->root;
211 }
212
213 static int tree_get_node(struct fs_node *node)
214 {
215         return atomic_add_unless(&node->refcount, 1, 0);
216 }
217
218 static void nested_down_read_ref_node(struct fs_node *node,
219                                       enum fs_i_lock_class class)
220 {
221         if (node) {
222                 down_read_nested(&node->lock, class);
223                 atomic_inc(&node->refcount);
224         }
225 }
226
227 static void nested_down_write_ref_node(struct fs_node *node,
228                                        enum fs_i_lock_class class)
229 {
230         if (node) {
231                 down_write_nested(&node->lock, class);
232                 atomic_inc(&node->refcount);
233         }
234 }
235
236 static void down_write_ref_node(struct fs_node *node)
237 {
238         if (node) {
239                 down_write(&node->lock);
240                 atomic_inc(&node->refcount);
241         }
242 }
243
244 static void up_read_ref_node(struct fs_node *node)
245 {
246         atomic_dec(&node->refcount);
247         up_read(&node->lock);
248 }
249
250 static void up_write_ref_node(struct fs_node *node)
251 {
252         atomic_dec(&node->refcount);
253         up_write(&node->lock);
254 }
255
256 static void tree_put_node(struct fs_node *node)
257 {
258         struct fs_node *parent_node = node->parent;
259
260         if (atomic_dec_and_test(&node->refcount)) {
261                 if (node->del_hw_func)
262                         node->del_hw_func(node);
263                 if (parent_node) {
264                         /* Only root namespace doesn't have parent and we just
265                          * need to free its node.
266                          */
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);
272                 } else {
273                         kfree(node);
274                 }
275                 node = NULL;
276         }
277         if (!node && parent_node)
278                 tree_put_node(parent_node);
279 }
280
281 static int tree_remove_node(struct fs_node *node)
282 {
283         if (atomic_read(&node->refcount) > 1) {
284                 atomic_dec(&node->refcount);
285                 return -EEXIST;
286         }
287         tree_put_node(node);
288         return 0;
289 }
290
291 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
292                                  unsigned int prio)
293 {
294         struct fs_prio *iter_prio;
295
296         fs_for_each_prio(iter_prio, ns) {
297                 if (iter_prio->prio == prio)
298                         return iter_prio;
299         }
300
301         return NULL;
302 }
303
304 static bool check_last_reserved(const u32 *match_criteria)
305 {
306         char *match_criteria_reserved =
307                 MLX5_ADDR_OF(fte_match_param, match_criteria, MLX5_FTE_MATCH_PARAM_RESERVED);
308
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);
313 }
314
315 static bool check_valid_mask(u8 match_criteria_enable, const u32 *match_criteria)
316 {
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)))
321                 return false;
322
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);
327
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))
331                         return false;
332         }
333
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);
338
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))
342                         return false;
343         }
344
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);
349
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))
353                         return false;
354         }
355
356         return check_last_reserved(match_criteria);
357 }
358
359 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
360 {
361         int i;
362
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");
365                 return false;
366         }
367
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");
371                         return false;
372                 }
373
374         return check_last_reserved(spec->match_value);
375 }
376
377 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
378 {
379         struct fs_node *root;
380         struct mlx5_flow_namespace *ns;
381
382         root = node->root;
383
384         if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
385                 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
386                 return NULL;
387         }
388
389         ns = container_of(root, struct mlx5_flow_namespace, node);
390         return container_of(ns, struct mlx5_flow_root_namespace, ns);
391 }
392
393 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
394 {
395         struct mlx5_flow_root_namespace *root = find_root(node);
396
397         if (root)
398                 return root->dev->priv.steering;
399         return NULL;
400 }
401
402 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
403 {
404         struct mlx5_flow_root_namespace *root = find_root(node);
405
406         if (root)
407                 return root->dev;
408         return NULL;
409 }
410
411 static void del_hw_flow_table(struct fs_node *node)
412 {
413         struct mlx5_flow_table *ft;
414         struct mlx5_core_dev *dev;
415         int err;
416
417         fs_get_obj(ft, node);
418         dev = get_dev(&ft->node);
419
420         if (node->active) {
421                 err = mlx5_cmd_destroy_flow_table(dev, ft);
422                 if (err)
423                         mlx5_core_warn(dev, "flow steering can't destroy ft\n");
424         }
425 }
426
427 static void del_sw_flow_table(struct fs_node *node)
428 {
429         struct mlx5_flow_table *ft;
430         struct fs_prio *prio;
431
432         fs_get_obj(ft, node);
433
434         rhltable_destroy(&ft->fgs_hash);
435         fs_get_obj(prio, ft->node.parent);
436         prio->num_ft--;
437         kfree(ft);
438 }
439
440 static void del_sw_hw_rule(struct fs_node *node)
441 {
442         struct mlx5_flow_rule *rule;
443         struct mlx5_flow_table *ft;
444         struct mlx5_flow_group *fg;
445         struct fs_fte *fte;
446         int modify_mask;
447         struct mlx5_core_dev *dev = get_dev(node);
448         int err;
449         bool update_fte = false;
450
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);
460         }
461
462         if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER  &&
463             --fte->dests_size) {
464                 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
465                 fte->action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
466                 update_fte = true;
467                 goto out;
468         }
469
470         if ((fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
471             --fte->dests_size) {
472                 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
473                 update_fte = true;
474         }
475 out:
476         if (update_fte && fte->dests_size) {
477                 err = mlx5_cmd_update_fte(dev, ft, fg->id, modify_mask, fte);
478                 if (err)
479                         mlx5_core_warn(dev,
480                                        "%s can't del rule fg id=%d fte_index=%d\n",
481                                        __func__, fg->id, fte->index);
482         }
483         kfree(rule);
484 }
485
486 static void del_hw_fte(struct fs_node *node)
487 {
488         struct mlx5_flow_table *ft;
489         struct mlx5_flow_group *fg;
490         struct mlx5_core_dev *dev;
491         struct fs_fte *fte;
492         int err;
493
494         fs_get_obj(fte, node);
495         fs_get_obj(fg, fte->node.parent);
496         fs_get_obj(ft, fg->node.parent);
497
498         trace_mlx5_fs_del_fte(fte);
499         dev = get_dev(&ft->node);
500         if (node->active) {
501                 err = mlx5_cmd_delete_fte(dev, ft,
502                                           fte->index);
503                 if (err)
504                         mlx5_core_warn(dev,
505                                        "flow steering can't delete fte in index %d of flow group id %d\n",
506                                        fte->index, fg->id);
507         }
508 }
509
510 static void del_sw_fte(struct fs_node *node)
511 {
512         struct mlx5_flow_steering *steering = get_steering(node);
513         struct mlx5_flow_group *fg;
514         struct fs_fte *fte;
515         int err;
516
517         fs_get_obj(fte, node);
518         fs_get_obj(fg, fte->node.parent);
519
520         err = rhashtable_remove_fast(&fg->ftes_hash,
521                                      &fte->hash,
522                                      rhash_fte);
523         WARN_ON(err);
524         ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
525         kmem_cache_free(steering->ftes_cache, fte);
526 }
527
528 static void del_hw_flow_group(struct fs_node *node)
529 {
530         struct mlx5_flow_group *fg;
531         struct mlx5_flow_table *ft;
532         struct mlx5_core_dev *dev;
533
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);
538
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",
541                                fg->id, ft->id);
542 }
543
544 static void del_sw_flow_group(struct fs_node *node)
545 {
546         struct mlx5_flow_steering *steering = get_steering(node);
547         struct mlx5_flow_group *fg;
548         struct mlx5_flow_table *ft;
549         int err;
550
551         fs_get_obj(fg, node);
552         fs_get_obj(ft, fg->node.parent);
553
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,
559                               &fg->hash,
560                               rhash_fg);
561         WARN_ON(err);
562         kmem_cache_free(steering->fgs_cache, fg);
563 }
564
565 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
566 {
567         int index;
568         int ret;
569
570         index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
571         if (index < 0)
572                 return index;
573
574         fte->index = index + fg->start_index;
575         ret = rhashtable_insert_fast(&fg->ftes_hash,
576                                      &fte->hash,
577                                      rhash_fte);
578         if (ret)
579                 goto err_ida_remove;
580
581         tree_add_node(&fte->node, &fg->node);
582         list_add_tail(&fte->node.list, &fg->node.children);
583         return 0;
584
585 err_ida_remove:
586         ida_simple_remove(&fg->fte_allocator, index);
587         return ret;
588 }
589
590 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
591                                 u32 *match_value,
592                                 struct mlx5_flow_act *flow_act)
593 {
594         struct mlx5_flow_steering *steering = get_steering(&ft->node);
595         struct fs_fte *fte;
596
597         fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
598         if (!fte)
599                 return ERR_PTR(-ENOMEM);
600
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;
607
608         tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
609
610         return fte;
611 }
612
613 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
614                                struct mlx5_flow_group *fg)
615 {
616         rhashtable_destroy(&fg->ftes_hash);
617         kmem_cache_free(steering->fgs_cache, fg);
618 }
619
620 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
621                                                 u8 match_criteria_enable,
622                                                 void *match_criteria,
623                                                 int start_index,
624                                                 int end_index)
625 {
626         struct mlx5_flow_group *fg;
627         int ret;
628
629         fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
630         if (!fg)
631                 return ERR_PTR(-ENOMEM);
632
633         ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
634         if (ret) {
635                 kmem_cache_free(steering->fgs_cache, fg);
636                 return ERR_PTR(ret);
637 }
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;
645
646         return fg;
647 }
648
649 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
650                                                        u8 match_criteria_enable,
651                                                        void *match_criteria,
652                                                        int start_index,
653                                                        int end_index,
654                                                        struct list_head *prev)
655 {
656         struct mlx5_flow_steering *steering = get_steering(&ft->node);
657         struct mlx5_flow_group *fg;
658         int ret;
659
660         fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
661                               start_index, end_index);
662         if (IS_ERR(fg))
663                 return fg;
664
665         /* initialize refcnt, add to parent list */
666         ret = rhltable_insert(&ft->fgs_hash,
667                               &fg->hash,
668                               rhash_fg);
669         if (ret) {
670                 dealloc_flow_group(steering, fg);
671                 return ERR_PTR(ret);
672         }
673
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);
679
680         return fg;
681 }
682
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,
686                                                 u32 flags)
687 {
688         struct mlx5_flow_table *ft;
689         int ret;
690
691         ft  = kzalloc(sizeof(*ft), GFP_KERNEL);
692         if (!ft)
693                 return ERR_PTR(-ENOMEM);
694
695         ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
696         if (ret) {
697                 kfree(ft);
698                 return ERR_PTR(ret);
699         }
700
701         ft->level = level;
702         ft->node.type = FS_TYPE_FLOW_TABLE;
703         ft->op_mod = op_mod;
704         ft->type = table_type;
705         ft->vport = vport;
706         ft->max_fte = max_fte;
707         ft->flags = flags;
708         INIT_LIST_HEAD(&ft->fwd_rules);
709         mutex_init(&ft->lock);
710
711         return ft;
712 }
713
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).
717  */
718 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node  *root,
719                                                          struct list_head *start,
720                                                          bool reverse)
721 {
722 #define list_advance_entry(pos, reverse)                \
723         ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
724
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))
729
730         struct fs_node *iter = list_entry(start, struct fs_node, list);
731         struct mlx5_flow_table *ft = NULL;
732
733         if (!root)
734                 return NULL;
735
736         list_for_each_advance_continue(iter, &root->children, reverse) {
737                 if (iter->type == FS_TYPE_FLOW_TABLE) {
738                         fs_get_obj(ft, iter);
739                         return ft;
740                 }
741                 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
742                 if (ft)
743                         return ft;
744         }
745
746         return ft;
747 }
748
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.
752  */
753 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
754 {
755         struct mlx5_flow_table *ft = NULL;
756         struct fs_node *curr_node;
757         struct fs_node *parent;
758
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);
763                 curr_node = parent;
764                 parent = curr_node->parent;
765         }
766         return ft;
767 }
768
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)
771 {
772         return find_closest_ft(prio, false);
773 }
774
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)
777 {
778         return find_closest_ft(prio, true);
779 }
780
781 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
782                                struct fs_prio *prio,
783                                struct mlx5_flow_table *ft)
784 {
785         struct mlx5_flow_table *iter;
786         int i = 0;
787         int err;
788
789         fs_for_each_ft(iter, prio) {
790                 i++;
791                 err = mlx5_cmd_modify_flow_table(dev,
792                                                  iter,
793                                                  ft);
794                 if (err) {
795                         mlx5_core_warn(dev, "Failed to modify flow table %d\n",
796                                        iter->id);
797                         /* The driver is out of sync with the FW */
798                         if (i > 1)
799                                 WARN_ON(true);
800                         return err;
801                 }
802         }
803         return 0;
804 }
805
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)
810 {
811         struct mlx5_flow_table *prev_ft;
812
813         prev_ft = find_prev_chained_ft(prio);
814         if (prev_ft) {
815                 struct fs_prio *prev_prio;
816
817                 fs_get_obj(prev_prio, prev_ft->node.parent);
818                 return connect_fts_in_prio(dev, prev_prio, ft);
819         }
820         return 0;
821 }
822
823 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
824                                  *prio)
825 {
826         struct mlx5_flow_root_namespace *root = find_root(&prio->node);
827         struct mlx5_ft_underlay_qp *uqp;
828         int min_level = INT_MAX;
829         int err;
830         u32 qpn;
831
832         if (root->root_ft)
833                 min_level = root->root_ft->level;
834
835         if (ft->level >= min_level)
836                 return 0;
837
838         if (list_empty(&root->underlay_qpns)) {
839                 /* Don't set any QPN (zero) in case QPN list is empty */
840                 qpn = 0;
841                 err = mlx5_cmd_update_root_ft(root->dev, ft, qpn, false);
842         } else {
843                 list_for_each_entry(uqp, &root->underlay_qpns, list) {
844                         qpn = uqp->qpn;
845                         err = mlx5_cmd_update_root_ft(root->dev, ft, qpn,
846                                                       false);
847                         if (err)
848                                 break;
849                 }
850         }
851
852         if (err)
853                 mlx5_core_warn(root->dev,
854                                "Update root flow table of id(%u) qpn(%d) failed\n",
855                                ft->id, qpn);
856         else
857                 root->root_ft = ft;
858
859         return err;
860 }
861
862 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
863                                          struct mlx5_flow_destination *dest)
864 {
865         struct mlx5_flow_table *ft;
866         struct mlx5_flow_group *fg;
867         struct fs_fte *fte;
868         int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
869         int err = 0;
870
871         fs_get_obj(fte, rule->node.parent);
872         if (!(fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
873                 return -EINVAL;
874         down_write_ref_node(&fte->node);
875         fs_get_obj(fg, fte->node.parent);
876         fs_get_obj(ft, fg->node.parent);
877
878         memcpy(&rule->dest_attr, dest, sizeof(*dest));
879         err = mlx5_cmd_update_fte(get_dev(&ft->node),
880                                   ft, fg->id,
881                                   modify_mask,
882                                   fte);
883         up_write_ref_node(&fte->node);
884
885         return err;
886 }
887
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)
891 {
892         int i;
893
894         if (!old_dest) {
895                 if (handle->num_rules != 1)
896                         return -EINVAL;
897                 return _mlx5_modify_rule_destination(handle->rule[0],
898                                                      new_dest);
899         }
900
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],
904                                                              new_dest);
905         }
906
907         return -EINVAL;
908 }
909
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)
914 {
915         struct mlx5_flow_destination dest;
916         struct mlx5_flow_rule *iter;
917         int err = 0;
918
919         /* new_next_ft and old_next_ft could be NULL only
920          * when we create/destroy the anchor flow table.
921          */
922         if (!new_next_ft || !old_next_ft)
923                 return 0;
924
925         dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
926         dest.ft = new_next_ft;
927
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);
933                 if (err)
934                         pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
935                                new_next_ft->id);
936         }
937         return 0;
938 }
939
940 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
941                               struct fs_prio *prio)
942 {
943         struct mlx5_flow_table *next_ft;
944         int err = 0;
945
946         /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
947
948         if (list_empty(&prio->node.children)) {
949                 err = connect_prev_fts(dev, ft, prio);
950                 if (err)
951                         return err;
952
953                 next_ft = find_next_chained_ft(prio);
954                 err = connect_fwd_rules(dev, ft, next_ft);
955                 if (err)
956                         return err;
957         }
958
959         if (MLX5_CAP_FLOWTABLE(dev,
960                                flow_table_properties_nic_receive.modify_root))
961                 err = update_root_ft_create(ft, prio);
962         return err;
963 }
964
965 static void list_add_flow_table(struct mlx5_flow_table *ft,
966                                 struct fs_prio *prio)
967 {
968         struct list_head *prev = &prio->node.children;
969         struct mlx5_flow_table *iter;
970
971         fs_for_each_ft(iter, prio) {
972                 if (iter->level > ft->level)
973                         break;
974                 prev = &iter->node.list;
975         }
976         list_add(&ft->node.list, prev);
977 }
978
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,
982                                                         u16 vport)
983 {
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;
988         int log_table_sz;
989         int err;
990
991         if (!root) {
992                 pr_err("mlx5: flow steering failed to find root of namespace\n");
993                 return ERR_PTR(-ENODEV);
994         }
995
996         mutex_lock(&root->chain_lock);
997         fs_prio = find_prio(ns, ft_attr->prio);
998         if (!fs_prio) {
999                 err = -EINVAL;
1000                 goto unlock_root;
1001         }
1002         if (ft_attr->level >= fs_prio->num_levels) {
1003                 err = -ENOSPC;
1004                 goto unlock_root;
1005         }
1006         /* The level is related to the
1007          * priority level range.
1008          */
1009         ft_attr->level += fs_prio->start_level;
1010         ft = alloc_flow_table(ft_attr->level,
1011                               vport,
1012                               ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
1013                               root->table_type,
1014                               op_mod, ft_attr->flags);
1015         if (IS_ERR(ft)) {
1016                 err = PTR_ERR(ft);
1017                 goto unlock_root;
1018         }
1019
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,
1025                                          ft->flags);
1026         if (err)
1027                 goto free_ft;
1028
1029         err = connect_flow_table(root->dev, ft, fs_prio);
1030         if (err)
1031                 goto destroy_ft;
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);
1036         fs_prio->num_ft++;
1037         up_write_ref_node(&fs_prio->node);
1038         mutex_unlock(&root->chain_lock);
1039         return ft;
1040 destroy_ft:
1041         mlx5_cmd_destroy_flow_table(root->dev, ft);
1042 free_ft:
1043         kfree(ft);
1044 unlock_root:
1045         mutex_unlock(&root->chain_lock);
1046         return ERR_PTR(err);
1047 }
1048
1049 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1050                                                struct mlx5_flow_table_attr *ft_attr)
1051 {
1052         return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1053 }
1054
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)
1058 {
1059         struct mlx5_flow_table_attr ft_attr = {};
1060
1061         ft_attr.max_fte = max_fte;
1062         ft_attr.level   = level;
1063         ft_attr.prio    = prio;
1064
1065         return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1066 }
1067
1068 struct mlx5_flow_table*
1069 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1070                                  int prio, u32 level)
1071 {
1072         struct mlx5_flow_table_attr ft_attr = {};
1073
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);
1077 }
1078 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1079
1080 struct mlx5_flow_table*
1081 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1082                                     int prio,
1083                                     int num_flow_table_entries,
1084                                     int max_num_groups,
1085                                     u32 level,
1086                                     u32 flags)
1087 {
1088         struct mlx5_flow_table_attr ft_attr = {};
1089         struct mlx5_flow_table *ft;
1090
1091         if (max_num_groups > num_flow_table_entries)
1092                 return ERR_PTR(-EINVAL);
1093
1094         ft_attr.max_fte = num_flow_table_entries;
1095         ft_attr.prio    = prio;
1096         ft_attr.level   = level;
1097         ft_attr.flags   = flags;
1098
1099         ft = mlx5_create_flow_table(ns, &ft_attr);
1100         if (IS_ERR(ft))
1101                 return ft;
1102
1103         ft->autogroup.active = true;
1104         ft->autogroup.required_groups = max_num_groups;
1105
1106         return ft;
1107 }
1108 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1109
1110 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1111                                                u32 *fg_in)
1112 {
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,
1116                                             fg_in,
1117                                             match_criteria_enable);
1118         int start_index = MLX5_GET(create_flow_group_in, fg_in,
1119                                    start_flow_index);
1120         int end_index = MLX5_GET(create_flow_group_in, fg_in,
1121                                  end_flow_index);
1122         struct mlx5_core_dev *dev = get_dev(&ft->node);
1123         struct mlx5_flow_group *fg;
1124         int err;
1125
1126         if (!check_valid_mask(match_criteria_enable, match_criteria))
1127                 return ERR_PTR(-EINVAL);
1128
1129         if (ft->autogroup.active)
1130                 return ERR_PTR(-EPERM);
1131
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);
1137         if (IS_ERR(fg))
1138                 return fg;
1139
1140         err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
1141         if (err) {
1142                 tree_put_node(&fg->node);
1143                 return ERR_PTR(err);
1144         }
1145         trace_mlx5_fs_add_fg(fg);
1146         fg->node.active = true;
1147
1148         return fg;
1149 }
1150
1151 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1152 {
1153         struct mlx5_flow_rule *rule;
1154
1155         rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1156         if (!rule)
1157                 return NULL;
1158
1159         INIT_LIST_HEAD(&rule->next_ft);
1160         rule->node.type = FS_TYPE_FLOW_DEST;
1161         if (dest)
1162                 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1163
1164         return rule;
1165 }
1166
1167 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1168 {
1169         struct mlx5_flow_handle *handle;
1170
1171         handle = kzalloc(sizeof(*handle) + sizeof(handle->rule[0]) *
1172                           num_rules, GFP_KERNEL);
1173         if (!handle)
1174                 return NULL;
1175
1176         handle->num_rules = num_rules;
1177
1178         return handle;
1179 }
1180
1181 static void destroy_flow_handle(struct fs_fte *fte,
1182                                 struct mlx5_flow_handle *handle,
1183                                 struct mlx5_flow_destination *dest,
1184                                 int i)
1185 {
1186         for (; --i >= 0;) {
1187                 if (atomic_dec_and_test(&handle->rule[i]->node.refcount)) {
1188                         fte->dests_size--;
1189                         list_del(&handle->rule[i]->node.list);
1190                         kfree(handle->rule[i]);
1191                 }
1192         }
1193         kfree(handle);
1194 }
1195
1196 static struct mlx5_flow_handle *
1197 create_flow_handle(struct fs_fte *fte,
1198                    struct mlx5_flow_destination *dest,
1199                    int dest_num,
1200                    int *modify_mask,
1201                    bool *new_rule)
1202 {
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);
1207         int type;
1208         int i = 0;
1209
1210         handle = alloc_handle((dest_num) ? dest_num : 1);
1211         if (!handle)
1212                 return ERR_PTR(-ENOMEM);
1213
1214         do {
1215                 if (dest) {
1216                         rule = find_flow_rule(fte, dest + i);
1217                         if (rule) {
1218                                 atomic_inc(&rule->node.refcount);
1219                                 goto rule_found;
1220                         }
1221                 }
1222
1223                 *new_rule = true;
1224                 rule = alloc_rule(dest + i);
1225                 if (!rule)
1226                         goto free_rules;
1227
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.
1230                  */
1231                 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1232                 if (dest &&
1233                     dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1234                         list_add(&rule->node.list, &fte->node.children);
1235                 else
1236                         list_add_tail(&rule->node.list, &fte->node.children);
1237                 if (dest) {
1238                         fte->dests_size++;
1239
1240                         type = dest[i].type ==
1241                                 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1242                         *modify_mask |= type ? count : dst;
1243                 }
1244 rule_found:
1245                 handle->rule[i] = rule;
1246         } while (++i < dest_num);
1247
1248         return handle;
1249
1250 free_rules:
1251         destroy_flow_handle(fte, handle, dest, i);
1252         return ERR_PTR(-ENOMEM);
1253 }
1254
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,
1260              int dest_num,
1261              bool update_action)
1262 {
1263         struct mlx5_flow_handle *handle;
1264         struct mlx5_flow_table *ft;
1265         int modify_mask = 0;
1266         int err;
1267         bool new_rule = false;
1268
1269         handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1270                                     &new_rule);
1271         if (IS_ERR(handle) || !new_rule)
1272                 goto out;
1273
1274         if (update_action)
1275                 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1276
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),
1280                                           ft, fg->id, fte);
1281         else
1282                 err = mlx5_cmd_update_fte(get_dev(&ft->node),
1283                                           ft, fg->id, modify_mask, fte);
1284         if (err)
1285                 goto free_handle;
1286
1287         fte->node.active = true;
1288         fte->status |= FS_FTE_STATUS_EXISTING;
1289         atomic_inc(&fte->node.version);
1290
1291 out:
1292         return handle;
1293
1294 free_handle:
1295         destroy_flow_handle(fte, handle, dest, handle->num_rules);
1296         return ERR_PTR(err);
1297 }
1298
1299 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table  *ft,
1300                                                      struct mlx5_flow_spec *spec)
1301 {
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;
1306
1307         if (!ft->autogroup.active)
1308                 return ERR_PTR(-ENOENT);
1309
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);
1313
1314         /*  ft->max_fte == ft->autogroup.max_types */
1315         if (group_size == 0)
1316                 group_size = 1;
1317
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;
1322                 else
1323                         break;
1324                 prev = &fg->node.list;
1325         }
1326
1327         if (candidate_index + group_size > ft->max_fte)
1328                 return ERR_PTR(-ENOSPC);
1329
1330         fg = alloc_insert_flow_group(ft,
1331                                      spec->match_criteria_enable,
1332                                      spec->match_criteria,
1333                                      candidate_index,
1334                                      candidate_index + group_size - 1,
1335                                      prev);
1336         if (IS_ERR(fg))
1337                 goto out;
1338
1339         ft->autogroup.num_groups++;
1340
1341 out:
1342         return fg;
1343 }
1344
1345 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1346                                   struct mlx5_flow_group *fg)
1347 {
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;
1351         int err;
1352         u32 *in;
1353
1354         in = kvzalloc(inlen, GFP_KERNEL);
1355         if (!in)
1356                 return -ENOMEM;
1357
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 +
1362                  fg->max_ftes - 1);
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));
1367
1368         err = mlx5_cmd_create_flow_group(dev, ft, in, &fg->id);
1369         if (!err) {
1370                 fg->node.active = true;
1371                 trace_mlx5_fs_add_fg(fg);
1372         }
1373
1374         kvfree(in);
1375         return err;
1376 }
1377
1378 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1379                                 struct mlx5_flow_destination *d2)
1380 {
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))
1388                         return true;
1389         }
1390
1391         return false;
1392 }
1393
1394 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1395                                              struct mlx5_flow_destination *dest)
1396 {
1397         struct mlx5_flow_rule *rule;
1398
1399         list_for_each_entry(rule, &fte->node.children, node.list) {
1400                 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1401                         return rule;
1402         }
1403         return NULL;
1404 }
1405
1406 static bool check_conflicting_actions(u32 action1, u32 action2)
1407 {
1408         u32 xored_actions = action1 ^ action2;
1409
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)
1413                 return false;
1414
1415         if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP  |
1416                              MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1417                              MLX5_FLOW_CONTEXT_ACTION_DECAP))
1418                 return true;
1419
1420         return false;
1421 }
1422
1423 static int check_conflicting_ftes(struct fs_fte *fte, const struct mlx5_flow_act *flow_act)
1424 {
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");
1428                 return -EEXIST;
1429         }
1430
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",
1434                                fte->flow_tag,
1435                                flow_act->flow_tag);
1436                 return -EEXIST;
1437         }
1438
1439         return 0;
1440 }
1441
1442 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1443                                             u32 *match_value,
1444                                             struct mlx5_flow_act *flow_act,
1445                                             struct mlx5_flow_destination *dest,
1446                                             int dest_num,
1447                                             struct fs_fte *fte)
1448 {
1449         struct mlx5_flow_handle *handle;
1450         int old_action;
1451         int i;
1452         int ret;
1453
1454         ret = check_conflicting_ftes(fte, flow_act);
1455         if (ret)
1456                 return ERR_PTR(ret);
1457
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;
1464                 return handle;
1465         }
1466         trace_mlx5_fs_set_fte(fte, false);
1467
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]);
1472                 }
1473         }
1474         return handle;
1475 }
1476
1477 struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_handle *handle)
1478 {
1479         struct mlx5_flow_rule *dst;
1480         struct fs_fte *fte;
1481
1482         fs_get_obj(fte, handle->rule[0]->node.parent);
1483
1484         fs_for_each_dst(dst, fte) {
1485                 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
1486                         return dst->dest_attr.counter;
1487         }
1488
1489         return NULL;
1490 }
1491
1492 static bool counter_is_valid(struct mlx5_fc *counter, u32 action)
1493 {
1494         if (!(action & MLX5_FLOW_CONTEXT_ACTION_COUNT))
1495                 return !counter;
1496
1497         if (!counter)
1498                 return false;
1499
1500         return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1501                           MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1502 }
1503
1504 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1505                           u32 action,
1506                           struct mlx5_flow_table *ft)
1507 {
1508         if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1509                 return counter_is_valid(dest->counter, action);
1510
1511         if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1512                 return true;
1513
1514         if (!dest || ((dest->type ==
1515             MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1516             (dest->ft->level <= ft->level)))
1517                 return false;
1518         return true;
1519 }
1520
1521 struct match_list {
1522         struct list_head        list;
1523         struct mlx5_flow_group *g;
1524 };
1525
1526 struct match_list_head {
1527         struct list_head  list;
1528         struct match_list first;
1529 };
1530
1531 static void free_match_list(struct match_list_head *head)
1532 {
1533         if (!list_empty(&head->list)) {
1534                 struct match_list *iter, *match_tmp;
1535
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,
1539                                          list) {
1540                         tree_put_node(&iter->g->node);
1541                         list_del(&iter->list);
1542                         kfree(iter);
1543                 }
1544         }
1545 }
1546
1547 static int build_match_list(struct match_list_head *match_head,
1548                             struct mlx5_flow_table *ft,
1549                             struct mlx5_flow_spec *spec)
1550 {
1551         struct rhlist_head *tmp, *list;
1552         struct mlx5_flow_group *g;
1553         int err = 0;
1554
1555         rcu_read_lock();
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;
1562
1563                 if (likely(list_empty(&match_head->list))) {
1564                         if (!tree_get_node(&g->node))
1565                                 continue;
1566                         match_head->first.g = g;
1567                         list_add_tail(&match_head->first.list,
1568                                       &match_head->list);
1569                         continue;
1570                 }
1571
1572                 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1573                 if (!curr_match) {
1574                         free_match_list(match_head);
1575                         err = -ENOMEM;
1576                         goto out;
1577                 }
1578                 if (!tree_get_node(&g->node)) {
1579                         kfree(curr_match);
1580                         continue;
1581                 }
1582                 curr_match->g = g;
1583                 list_add_tail(&curr_match->list, &match_head->list);
1584         }
1585 out:
1586         rcu_read_unlock();
1587         return err;
1588 }
1589
1590 static u64 matched_fgs_get_version(struct list_head *match_head)
1591 {
1592         struct match_list *iter;
1593         u64 version = 0;
1594
1595         list_for_each_entry(iter, match_head, list)
1596                 version += (u64)atomic_read(&iter->g->node.version);
1597         return version;
1598 }
1599
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,
1606                        int dest_num,
1607                        int ft_version)
1608 {
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;
1614         struct fs_fte *fte;
1615         u64  version;
1616         int err;
1617
1618         fte = alloc_fte(ft, spec->match_value, flow_act);
1619         if (IS_ERR(fte))
1620                 return  ERR_PTR(-ENOMEM);
1621
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);
1625         }
1626
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;
1632
1633                 g = iter->g;
1634                 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, spec->match_value,
1635                                                  rhash_fte);
1636                 if (!fte_tmp || !tree_get_node(&fte_tmp->node))
1637                         continue;
1638
1639                 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1640                 if (!take_write) {
1641                         list_for_each_entry(iter, match_head, list)
1642                                 up_read_ref_node(&iter->g->node);
1643                 } else {
1644                         list_for_each_entry(iter, match_head, list)
1645                                 up_write_ref_node(&iter->g->node);
1646                 }
1647
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);
1653                 return rule;
1654         }
1655
1656         /* No group with matching fte found. Try to add a new fte to any
1657          * matching fg.
1658          */
1659
1660         if (!take_write) {
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,
1665                                                    FS_LOCK_PARENT);
1666                 take_write = true;
1667         }
1668
1669         /* Check the ft version, for case that new flow group
1670          * was added while the fgs weren't locked
1671          */
1672         if (atomic_read(&ft->node.version) != ft_version) {
1673                 rule = ERR_PTR(-EAGAIN);
1674                 goto out;
1675         }
1676
1677         /* Check the fgs version, for case the new FTE with the
1678          * same values was added while the fgs weren't locked
1679          */
1680         if (version != matched_fgs_get_version(match_head))
1681                 goto search_again_locked;
1682
1683         list_for_each_entry(iter, match_head, list) {
1684                 g = iter->g;
1685
1686                 if (!g->node.active)
1687                         continue;
1688                 err = insert_fte(g, fte);
1689                 if (err) {
1690                         if (err == -ENOSPC)
1691                                 continue;
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);
1696                 }
1697
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);
1705                 return rule;
1706         }
1707         rule = ERR_PTR(-ENOENT);
1708 out:
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);
1712         return rule;
1713 }
1714
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,
1720                      int dest_num)
1721
1722 {
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;
1728         struct fs_fte *fte;
1729         int version;
1730         int err;
1731         int i;
1732
1733         if (!check_valid_spec(spec))
1734                 return ERR_PTR(-EINVAL);
1735
1736         for (i = 0; i < dest_num; i++) {
1737                 if (!dest_is_valid(&dest[i], flow_act->action, ft))
1738                         return ERR_PTR(-EINVAL);
1739         }
1740         nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1741 search_again_locked:
1742         version = atomic_read(&ft->node.version);
1743
1744         /* Collect all fgs which has a matching match_criteria */
1745         err = build_match_list(&match_head, ft, spec);
1746         if (err)
1747                 return ERR_PTR(err);
1748
1749         if (!take_write)
1750                 up_read_ref_node(&ft->node);
1751
1752         rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1753                                       dest_num, version);
1754         free_match_list(&match_head);
1755         if (!IS_ERR(rule) ||
1756             (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN))
1757                 return rule;
1758
1759         if (!take_write) {
1760                 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1761                 take_write = true;
1762         }
1763
1764         if (PTR_ERR(rule) == -EAGAIN ||
1765             version != atomic_read(&ft->node.version))
1766                 goto search_again_locked;
1767
1768         g = alloc_auto_flow_group(ft, spec);
1769         if (IS_ERR(g)) {
1770                 rule = (void *)g;
1771                 up_write_ref_node(&ft->node);
1772                 return rule;
1773         }
1774
1775         nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1776         up_write_ref_node(&ft->node);
1777
1778         err = create_auto_flow_group(ft, g);
1779         if (err)
1780                 goto err_release_fg;
1781
1782         fte = alloc_fte(ft, spec->match_value, flow_act);
1783         if (IS_ERR(fte)) {
1784                 err = PTR_ERR(fte);
1785                 goto err_release_fg;
1786         }
1787
1788         err = insert_fte(g, fte);
1789         if (err) {
1790                 kmem_cache_free(steering->ftes_cache, fte);
1791                 goto err_release_fg;
1792         }
1793
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,
1797                            dest_num, fte);
1798         up_write_ref_node(&fte->node);
1799         tree_put_node(&fte->node);
1800         tree_put_node(&g->node);
1801         return rule;
1802
1803 err_release_fg:
1804         up_write_ref_node(&g->node);
1805         tree_put_node(&g->node);
1806         return ERR_PTR(err);
1807 }
1808
1809 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1810 {
1811         return ((ft->type == FS_FT_NIC_RX) &&
1812                 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1813 }
1814
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,
1820                     int dest_num)
1821 {
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;
1828
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);
1833                 if (dest)
1834                         return ERR_PTR(-EINVAL);
1835                 mutex_lock(&root->chain_lock);
1836                 next_ft = find_next_chained_ft(prio);
1837                 if (next_ft) {
1838                         gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1839                         gen_dest.ft = next_ft;
1840                         dest = &gen_dest;
1841                         dest_num = 1;
1842                         flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1843                 } else {
1844                         mutex_unlock(&root->chain_lock);
1845                         return ERR_PTR(-EOPNOTSUPP);
1846                 }
1847         }
1848
1849         handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, dest_num);
1850
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;
1859                 }
1860                 mutex_unlock(&root->chain_lock);
1861         }
1862         return handle;
1863 }
1864 EXPORT_SYMBOL(mlx5_add_flow_rules);
1865
1866 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1867 {
1868         int i;
1869
1870         for (i = handle->num_rules - 1; i >= 0; i--)
1871                 tree_remove_node(&handle->rule[i]->node);
1872         kfree(handle);
1873 }
1874 EXPORT_SYMBOL(mlx5_del_flow_rules);
1875
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)
1878 {
1879         struct fs_prio *prio;
1880
1881         fs_get_obj(prio, ft->node.parent);
1882
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);
1886 }
1887
1888 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1889 {
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;
1893         int err = 0;
1894         u32 qpn;
1895
1896         if (root->root_ft != ft)
1897                 return 0;
1898
1899         new_root_ft = find_next_ft(ft);
1900
1901         if (!new_root_ft) {
1902                 root->root_ft = NULL;
1903                 return 0;
1904         }
1905
1906         if (list_empty(&root->underlay_qpns)) {
1907                 /* Don't set any QPN (zero) in case QPN list is empty */
1908                 qpn = 0;
1909                 err = mlx5_cmd_update_root_ft(root->dev, new_root_ft, qpn,
1910                                               false);
1911         } else {
1912                 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1913                         qpn = uqp->qpn;
1914                         err = mlx5_cmd_update_root_ft(root->dev, new_root_ft,
1915                                                       qpn, false);
1916                         if (err)
1917                                 break;
1918                 }
1919         }
1920
1921         if (err)
1922                 mlx5_core_warn(root->dev,
1923                                "Update root flow table of id(%u) qpn(%d) failed\n",
1924                                ft->id, qpn);
1925         else
1926                 root->root_ft = new_root_ft;
1927
1928         return 0;
1929 }
1930
1931 /* Connect flow table from previous priority to
1932  * the next flow table.
1933  */
1934 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1935 {
1936         struct mlx5_core_dev *dev = get_dev(&ft->node);
1937         struct mlx5_flow_table *next_ft;
1938         struct fs_prio *prio;
1939         int err = 0;
1940
1941         err = update_root_ft_destroy(ft);
1942         if (err)
1943                 return err;
1944
1945         fs_get_obj(prio, ft->node.parent);
1946         if  (!(list_first_entry(&prio->node.children,
1947                                 struct mlx5_flow_table,
1948                                 node.list) == ft))
1949                 return 0;
1950
1951         next_ft = find_next_chained_ft(prio);
1952         err = connect_fwd_rules(dev, next_ft, ft);
1953         if (err)
1954                 return err;
1955
1956         err = connect_prev_fts(dev, next_ft, prio);
1957         if (err)
1958                 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1959                                ft->id);
1960         return err;
1961 }
1962
1963 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1964 {
1965         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1966         int err = 0;
1967
1968         mutex_lock(&root->chain_lock);
1969         err = disconnect_flow_table(ft);
1970         if (err) {
1971                 mutex_unlock(&root->chain_lock);
1972                 return err;
1973         }
1974         if (tree_remove_node(&ft->node))
1975                 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1976                                ft->id);
1977         mutex_unlock(&root->chain_lock);
1978
1979         return err;
1980 }
1981 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1982
1983 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1984 {
1985         if (tree_remove_node(&fg->node))
1986                 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1987                                fg->id);
1988 }
1989
1990 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1991                                                     enum mlx5_flow_namespace_type type)
1992 {
1993         struct mlx5_flow_steering *steering = dev->priv.steering;
1994         struct mlx5_flow_root_namespace *root_ns;
1995         int prio;
1996         struct fs_prio *fs_prio;
1997         struct mlx5_flow_namespace *ns;
1998
1999         if (!steering)
2000                 return NULL;
2001
2002         switch (type) {
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:
2010                 prio = type;
2011                 break;
2012         case MLX5_FLOW_NAMESPACE_FDB:
2013                 if (steering->fdb_root_ns)
2014                         return &steering->fdb_root_ns->ns;
2015                 else
2016                         return NULL;
2017         case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2018                 if (steering->esw_egress_root_ns)
2019                         return &steering->esw_egress_root_ns->ns;
2020                 else
2021                         return NULL;
2022         case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2023                 if (steering->esw_ingress_root_ns)
2024                         return &steering->esw_ingress_root_ns->ns;
2025                 else
2026                         return NULL;
2027         case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2028                 if (steering->sniffer_rx_root_ns)
2029                         return &steering->sniffer_rx_root_ns->ns;
2030                 else
2031                         return NULL;
2032         case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2033                 if (steering->sniffer_tx_root_ns)
2034                         return &steering->sniffer_tx_root_ns->ns;
2035                 else
2036                         return NULL;
2037         default:
2038                 return NULL;
2039         }
2040
2041         root_ns = steering->root_ns;
2042         if (!root_ns)
2043                 return NULL;
2044
2045         fs_prio = find_prio(&root_ns->ns, prio);
2046         if (!fs_prio)
2047                 return NULL;
2048
2049         ns = list_first_entry(&fs_prio->node.children,
2050                               typeof(*ns),
2051                               node.list);
2052
2053         return ns;
2054 }
2055 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2056
2057 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2058                                       unsigned int prio, int num_levels)
2059 {
2060         struct fs_prio *fs_prio;
2061
2062         fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2063         if (!fs_prio)
2064                 return ERR_PTR(-ENOMEM);
2065
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);
2072
2073         return fs_prio;
2074 }
2075
2076 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2077                                                      *ns)
2078 {
2079         ns->node.type = FS_TYPE_NAMESPACE;
2080
2081         return ns;
2082 }
2083
2084 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2085 {
2086         struct mlx5_flow_namespace      *ns;
2087
2088         ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2089         if (!ns)
2090                 return ERR_PTR(-ENOMEM);
2091
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);
2096
2097         return ns;
2098 }
2099
2100 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2101                              struct init_tree_node *prio_metadata)
2102 {
2103         struct fs_prio *fs_prio;
2104         int i;
2105
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);
2110         }
2111         return 0;
2112 }
2113
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]) +    \
2117                         offset / 32)) >>                                        \
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)
2120 {
2121         int i;
2122
2123         for (i = 0; i < caps->arr_sz; i++) {
2124                 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2125                         return false;
2126         }
2127         return true;
2128 }
2129
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,
2134                                     int prio)
2135 {
2136         int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2137                                               flow_table_properties_nic_receive.
2138                                               max_ft_level);
2139         struct mlx5_flow_namespace *fs_ns;
2140         struct fs_prio *fs_prio;
2141         struct fs_node *base;
2142         int i;
2143         int err;
2144
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))
2148                         return 0;
2149
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);
2160                 if (IS_ERR(fs_ns))
2161                         return PTR_ERR(fs_ns);
2162                 base = &fs_ns->node;
2163         } else {
2164                 return -EINVAL;
2165         }
2166         prio = 0;
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);
2170                 if (err)
2171                         return err;
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;
2175                 }
2176         }
2177
2178         return 0;
2179 }
2180
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)
2184 {
2185         int i;
2186         struct mlx5_flow_namespace *fs_ns;
2187         int err;
2188
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],
2192                                                &fs_ns->node,
2193                                                init_node, i);
2194                 if (err)
2195                         return err;
2196         }
2197         return 0;
2198 }
2199
2200 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_flow_steering *steering,
2201                                                        enum fs_flow_table_type
2202                                                        table_type)
2203 {
2204         struct mlx5_flow_root_namespace *root_ns;
2205         struct mlx5_flow_namespace *ns;
2206
2207         /* Create the root namespace */
2208         root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
2209         if (!root_ns)
2210                 return NULL;
2211
2212         root_ns->dev = steering->dev;
2213         root_ns->table_type = table_type;
2214
2215         INIT_LIST_HEAD(&root_ns->underlay_qpns);
2216
2217         ns = &root_ns->ns;
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);
2222
2223         return root_ns;
2224 }
2225
2226 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2227
2228 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2229 {
2230         struct fs_prio *prio;
2231
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;
2236         }
2237         return acc_level;
2238 }
2239
2240 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2241 {
2242         struct mlx5_flow_namespace *ns;
2243         int acc_level_ns = acc_level;
2244
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);
2252 }
2253
2254 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2255 {
2256         struct mlx5_flow_namespace *ns = &root_ns->ns;
2257         struct fs_prio *prio;
2258         int start_level = 0;
2259
2260         fs_for_each_prio(prio, ns) {
2261                 set_prio_attrs_in_prio(prio, start_level);
2262                 start_level += prio->num_levels;
2263         }
2264 }
2265
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)
2270 {
2271         struct mlx5_flow_namespace *ns = NULL;
2272         struct mlx5_flow_table_attr ft_attr = {};
2273         struct mlx5_flow_table *ft;
2274
2275         ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2276         if (WARN_ON(!ns))
2277                 return -EINVAL;
2278
2279         ft_attr.max_fte = ANCHOR_SIZE;
2280         ft_attr.level   = ANCHOR_LEVEL;
2281         ft_attr.prio    = ANCHOR_PRIO;
2282
2283         ft = mlx5_create_flow_table(ns, &ft_attr);
2284         if (IS_ERR(ft)) {
2285                 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2286                 return PTR_ERR(ft);
2287         }
2288         return 0;
2289 }
2290
2291 static int init_root_ns(struct mlx5_flow_steering *steering)
2292 {
2293         steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2294         if (!steering->root_ns)
2295                 goto cleanup;
2296
2297         if (init_root_tree(steering, &root_fs, &steering->root_ns->ns.node))
2298                 goto cleanup;
2299
2300         set_prio_attrs(steering->root_ns);
2301
2302         if (create_anchor_flow_table(steering))
2303                 goto cleanup;
2304
2305         return 0;
2306
2307 cleanup:
2308         mlx5_cleanup_fs(steering->dev);
2309         return -ENOMEM;
2310 }
2311
2312 static void clean_tree(struct fs_node *node)
2313 {
2314         if (node) {
2315                 struct fs_node *iter;
2316                 struct fs_node *temp;
2317
2318                 tree_get_node(node);
2319                 list_for_each_entry_safe(iter, temp, &node->children, list)
2320                         clean_tree(iter);
2321                 tree_put_node(node);
2322                 tree_remove_node(node);
2323         }
2324 }
2325
2326 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2327 {
2328         if (!root_ns)
2329                 return;
2330
2331         clean_tree(&root_ns->ns.node);
2332 }
2333
2334 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2335 {
2336         struct mlx5_flow_steering *steering = dev->priv.steering;
2337
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);
2347         kfree(steering);
2348 }
2349
2350 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2351 {
2352         struct fs_prio *prio;
2353
2354         steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2355         if (!steering->sniffer_tx_root_ns)
2356                 return -ENOMEM;
2357
2358         /* Create single prio */
2359         prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2360         if (IS_ERR(prio)) {
2361                 cleanup_root_ns(steering->sniffer_tx_root_ns);
2362                 return PTR_ERR(prio);
2363         }
2364         return 0;
2365 }
2366
2367 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2368 {
2369         struct fs_prio *prio;
2370
2371         steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2372         if (!steering->sniffer_rx_root_ns)
2373                 return -ENOMEM;
2374
2375         /* Create single prio */
2376         prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2377         if (IS_ERR(prio)) {
2378                 cleanup_root_ns(steering->sniffer_rx_root_ns);
2379                 return PTR_ERR(prio);
2380         }
2381         return 0;
2382 }
2383
2384 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2385 {
2386         struct fs_prio *prio;
2387
2388         steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2389         if (!steering->fdb_root_ns)
2390                 return -ENOMEM;
2391
2392         prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 1);
2393         if (IS_ERR(prio))
2394                 goto out_err;
2395
2396         prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2397         if (IS_ERR(prio))
2398                 goto out_err;
2399
2400         set_prio_attrs(steering->fdb_root_ns);
2401         return 0;
2402
2403 out_err:
2404         cleanup_root_ns(steering->fdb_root_ns);
2405         steering->fdb_root_ns = NULL;
2406         return PTR_ERR(prio);
2407 }
2408
2409 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering)
2410 {
2411         struct fs_prio *prio;
2412
2413         steering->esw_egress_root_ns = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2414         if (!steering->esw_egress_root_ns)
2415                 return -ENOMEM;
2416
2417         /* create 1 prio*/
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);
2421 }
2422
2423 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering)
2424 {
2425         struct fs_prio *prio;
2426
2427         steering->esw_ingress_root_ns = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2428         if (!steering->esw_ingress_root_ns)
2429                 return -ENOMEM;
2430
2431         /* create 1 prio*/
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);
2435 }
2436
2437 int mlx5_init_fs(struct mlx5_core_dev *dev)
2438 {
2439         struct mlx5_flow_steering *steering;
2440         int err = 0;
2441
2442         err = mlx5_init_fc_stats(dev);
2443         if (err)
2444                 return err;
2445
2446         steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2447         if (!steering)
2448                 return -ENOMEM;
2449         steering->dev = dev;
2450         dev->priv.steering = steering;
2451
2452         steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2453                                                 sizeof(struct mlx5_flow_group), 0,
2454                                                 0, NULL);
2455         steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2456                                                  0, NULL);
2457         if (!steering->ftes_cache || !steering->fgs_cache) {
2458                 err = -ENOMEM;
2459                 goto err;
2460         }
2461
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);
2468                 if (err)
2469                         goto err;
2470         }
2471
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);
2475                         if (err)
2476                                 goto err;
2477                 }
2478                 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2479                         err = init_egress_acl_root_ns(steering);
2480                         if (err)
2481                                 goto err;
2482                 }
2483                 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2484                         err = init_ingress_acl_root_ns(steering);
2485                         if (err)
2486                                 goto err;
2487                 }
2488         }
2489
2490         if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2491                 err = init_sniffer_rx_root_ns(steering);
2492                 if (err)
2493                         goto err;
2494         }
2495
2496         if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2497                 err = init_sniffer_tx_root_ns(steering);
2498                 if (err)
2499                         goto err;
2500         }
2501
2502         return 0;
2503 err:
2504         mlx5_cleanup_fs(dev);
2505         return err;
2506 }
2507
2508 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2509 {
2510         struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2511         struct mlx5_ft_underlay_qp *new_uqp;
2512         int err = 0;
2513
2514         new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2515         if (!new_uqp)
2516                 return -ENOMEM;
2517
2518         mutex_lock(&root->chain_lock);
2519
2520         if (!root->root_ft) {
2521                 err = -EINVAL;
2522                 goto update_ft_fail;
2523         }
2524
2525         err = mlx5_cmd_update_root_ft(dev, root->root_ft, underlay_qpn, false);
2526         if (err) {
2527                 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2528                                underlay_qpn, err);
2529                 goto update_ft_fail;
2530         }
2531
2532         new_uqp->qpn = underlay_qpn;
2533         list_add_tail(&new_uqp->list, &root->underlay_qpns);
2534
2535         mutex_unlock(&root->chain_lock);
2536
2537         return 0;
2538
2539 update_ft_fail:
2540         mutex_unlock(&root->chain_lock);
2541         kfree(new_uqp);
2542         return err;
2543 }
2544 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2545
2546 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2547 {
2548         struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2549         struct mlx5_ft_underlay_qp *uqp;
2550         bool found = false;
2551         int err = 0;
2552
2553         mutex_lock(&root->chain_lock);
2554         list_for_each_entry(uqp, &root->underlay_qpns, list) {
2555                 if (uqp->qpn == underlay_qpn) {
2556                         found = true;
2557                         break;
2558                 }
2559         }
2560
2561         if (!found) {
2562                 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2563                                underlay_qpn);
2564                 err = -EINVAL;
2565                 goto out;
2566         }
2567
2568         err = mlx5_cmd_update_root_ft(dev, root->root_ft, underlay_qpn, true);
2569         if (err)
2570                 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2571                                underlay_qpn, err);
2572
2573         list_del(&uqp->list);
2574         mutex_unlock(&root->chain_lock);
2575         kfree(uqp);
2576
2577         return 0;
2578
2579 out:
2580         mutex_unlock(&root->chain_lock);
2581         return err;
2582 }
2583 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);