Merge tag 'mlx5-fixes-2017-12-19' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.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 static void del_sw_prio(struct fs_node *node);
178 static void del_sw_ns(struct fs_node *node);
179 /* Delete rule (destination) is special case that 
180  * requires to lock the FTE for all the deletion process.
181  */
182 static void del_sw_hw_rule(struct fs_node *node);
183 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
184                                 struct mlx5_flow_destination *d2);
185 static struct mlx5_flow_rule *
186 find_flow_rule(struct fs_fte *fte,
187                struct mlx5_flow_destination *dest);
188
189 static void tree_init_node(struct fs_node *node,
190                            void (*del_hw_func)(struct fs_node *),
191                            void (*del_sw_func)(struct fs_node *))
192 {
193         refcount_set(&node->refcount, 1);
194         INIT_LIST_HEAD(&node->list);
195         INIT_LIST_HEAD(&node->children);
196         init_rwsem(&node->lock);
197         node->del_hw_func = del_hw_func;
198         node->del_sw_func = del_sw_func;
199         node->active = false;
200 }
201
202 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
203 {
204         if (parent)
205                 refcount_inc(&parent->refcount);
206         node->parent = parent;
207
208         /* Parent is the root */
209         if (!parent)
210                 node->root = node;
211         else
212                 node->root = parent->root;
213 }
214
215 static int tree_get_node(struct fs_node *node)
216 {
217         return refcount_inc_not_zero(&node->refcount);
218 }
219
220 static void nested_down_read_ref_node(struct fs_node *node,
221                                       enum fs_i_lock_class class)
222 {
223         if (node) {
224                 down_read_nested(&node->lock, class);
225                 refcount_inc(&node->refcount);
226         }
227 }
228
229 static void nested_down_write_ref_node(struct fs_node *node,
230                                        enum fs_i_lock_class class)
231 {
232         if (node) {
233                 down_write_nested(&node->lock, class);
234                 refcount_inc(&node->refcount);
235         }
236 }
237
238 static void down_write_ref_node(struct fs_node *node)
239 {
240         if (node) {
241                 down_write(&node->lock);
242                 refcount_inc(&node->refcount);
243         }
244 }
245
246 static void up_read_ref_node(struct fs_node *node)
247 {
248         refcount_dec(&node->refcount);
249         up_read(&node->lock);
250 }
251
252 static void up_write_ref_node(struct fs_node *node)
253 {
254         refcount_dec(&node->refcount);
255         up_write(&node->lock);
256 }
257
258 static void tree_put_node(struct fs_node *node)
259 {
260         struct fs_node *parent_node = node->parent;
261
262         if (refcount_dec_and_test(&node->refcount)) {
263                 if (node->del_hw_func)
264                         node->del_hw_func(node);
265                 if (parent_node) {
266                         /* Only root namespace doesn't have parent and we just
267                          * need to free its node.
268                          */
269                         down_write_ref_node(parent_node);
270                         list_del_init(&node->list);
271                         if (node->del_sw_func)
272                                 node->del_sw_func(node);
273                         up_write_ref_node(parent_node);
274                 } else {
275                         kfree(node);
276                 }
277                 node = NULL;
278         }
279         if (!node && parent_node)
280                 tree_put_node(parent_node);
281 }
282
283 static int tree_remove_node(struct fs_node *node)
284 {
285         if (refcount_read(&node->refcount) > 1) {
286                 refcount_dec(&node->refcount);
287                 return -EEXIST;
288         }
289         tree_put_node(node);
290         return 0;
291 }
292
293 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
294                                  unsigned int prio)
295 {
296         struct fs_prio *iter_prio;
297
298         fs_for_each_prio(iter_prio, ns) {
299                 if (iter_prio->prio == prio)
300                         return iter_prio;
301         }
302
303         return NULL;
304 }
305
306 static bool check_last_reserved(const u32 *match_criteria)
307 {
308         char *match_criteria_reserved =
309                 MLX5_ADDR_OF(fte_match_param, match_criteria, MLX5_FTE_MATCH_PARAM_RESERVED);
310
311         return  !match_criteria_reserved[0] &&
312                 !memcmp(match_criteria_reserved, match_criteria_reserved + 1,
313                         MLX5_FLD_SZ_BYTES(fte_match_param,
314                                           MLX5_FTE_MATCH_PARAM_RESERVED) - 1);
315 }
316
317 static bool check_valid_mask(u8 match_criteria_enable, const u32 *match_criteria)
318 {
319         if (match_criteria_enable & ~(
320                 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS)   |
321                 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) |
322                 (1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS)))
323                 return false;
324
325         if (!(match_criteria_enable &
326               1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS)) {
327                 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
328                                                   match_criteria, outer_headers);
329
330                 if (fg_type_mask[0] ||
331                     memcmp(fg_type_mask, fg_type_mask + 1,
332                            MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4) - 1))
333                         return false;
334         }
335
336         if (!(match_criteria_enable &
337               1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS)) {
338                 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
339                                                   match_criteria, misc_parameters);
340
341                 if (fg_type_mask[0] ||
342                     memcmp(fg_type_mask, fg_type_mask + 1,
343                            MLX5_ST_SZ_BYTES(fte_match_set_misc) - 1))
344                         return false;
345         }
346
347         if (!(match_criteria_enable &
348               1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS)) {
349                 char *fg_type_mask = MLX5_ADDR_OF(fte_match_param,
350                                                   match_criteria, inner_headers);
351
352                 if (fg_type_mask[0] ||
353                     memcmp(fg_type_mask, fg_type_mask + 1,
354                            MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4) - 1))
355                         return false;
356         }
357
358         return check_last_reserved(match_criteria);
359 }
360
361 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
362 {
363         int i;
364
365         if (!check_valid_mask(spec->match_criteria_enable, spec->match_criteria)) {
366                 pr_warn("mlx5_core: Match criteria given mismatches match_criteria_enable\n");
367                 return false;
368         }
369
370         for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
371                 if (spec->match_value[i] & ~spec->match_criteria[i]) {
372                         pr_warn("mlx5_core: match_value differs from match_criteria\n");
373                         return false;
374                 }
375
376         return check_last_reserved(spec->match_value);
377 }
378
379 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
380 {
381         struct fs_node *root;
382         struct mlx5_flow_namespace *ns;
383
384         root = node->root;
385
386         if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
387                 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
388                 return NULL;
389         }
390
391         ns = container_of(root, struct mlx5_flow_namespace, node);
392         return container_of(ns, struct mlx5_flow_root_namespace, ns);
393 }
394
395 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
396 {
397         struct mlx5_flow_root_namespace *root = find_root(node);
398
399         if (root)
400                 return root->dev->priv.steering;
401         return NULL;
402 }
403
404 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
405 {
406         struct mlx5_flow_root_namespace *root = find_root(node);
407
408         if (root)
409                 return root->dev;
410         return NULL;
411 }
412
413 static void del_sw_ns(struct fs_node *node)
414 {
415         kfree(node);
416 }
417
418 static void del_sw_prio(struct fs_node *node)
419 {
420         kfree(node);
421 }
422
423 static void del_hw_flow_table(struct fs_node *node)
424 {
425         struct mlx5_flow_table *ft;
426         struct mlx5_core_dev *dev;
427         int err;
428
429         fs_get_obj(ft, node);
430         dev = get_dev(&ft->node);
431
432         if (node->active) {
433                 err = mlx5_cmd_destroy_flow_table(dev, ft);
434                 if (err)
435                         mlx5_core_warn(dev, "flow steering can't destroy ft\n");
436         }
437 }
438
439 static void del_sw_flow_table(struct fs_node *node)
440 {
441         struct mlx5_flow_table *ft;
442         struct fs_prio *prio;
443
444         fs_get_obj(ft, node);
445
446         rhltable_destroy(&ft->fgs_hash);
447         fs_get_obj(prio, ft->node.parent);
448         prio->num_ft--;
449         kfree(ft);
450 }
451
452 static void del_sw_hw_rule(struct fs_node *node)
453 {
454         struct mlx5_flow_rule *rule;
455         struct mlx5_flow_table *ft;
456         struct mlx5_flow_group *fg;
457         struct fs_fte *fte;
458         int modify_mask;
459         struct mlx5_core_dev *dev = get_dev(node);
460         int err;
461         bool update_fte = false;
462
463         fs_get_obj(rule, node);
464         fs_get_obj(fte, rule->node.parent);
465         fs_get_obj(fg, fte->node.parent);
466         fs_get_obj(ft, fg->node.parent);
467         trace_mlx5_fs_del_rule(rule);
468         if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
469                 mutex_lock(&rule->dest_attr.ft->lock);
470                 list_del(&rule->next_ft);
471                 mutex_unlock(&rule->dest_attr.ft->lock);
472         }
473
474         if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER  &&
475             --fte->dests_size) {
476                 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
477                 fte->action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
478                 update_fte = true;
479                 goto out;
480         }
481
482         if ((fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
483             --fte->dests_size) {
484                 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
485                 update_fte = true;
486         }
487 out:
488         if (update_fte && fte->dests_size) {
489                 err = mlx5_cmd_update_fte(dev, ft, fg->id, modify_mask, fte);
490                 if (err)
491                         mlx5_core_warn(dev,
492                                        "%s can't del rule fg id=%d fte_index=%d\n",
493                                        __func__, fg->id, fte->index);
494         }
495         kfree(rule);
496 }
497
498 static void del_hw_fte(struct fs_node *node)
499 {
500         struct mlx5_flow_table *ft;
501         struct mlx5_flow_group *fg;
502         struct mlx5_core_dev *dev;
503         struct fs_fte *fte;
504         int err;
505
506         fs_get_obj(fte, node);
507         fs_get_obj(fg, fte->node.parent);
508         fs_get_obj(ft, fg->node.parent);
509
510         trace_mlx5_fs_del_fte(fte);
511         dev = get_dev(&ft->node);
512         if (node->active) {
513                 err = mlx5_cmd_delete_fte(dev, ft,
514                                           fte->index);
515                 if (err)
516                         mlx5_core_warn(dev,
517                                        "flow steering can't delete fte in index %d of flow group id %d\n",
518                                        fte->index, fg->id);
519         }
520 }
521
522 static void del_sw_fte(struct fs_node *node)
523 {
524         struct mlx5_flow_steering *steering = get_steering(node);
525         struct mlx5_flow_group *fg;
526         struct fs_fte *fte;
527         int err;
528
529         fs_get_obj(fte, node);
530         fs_get_obj(fg, fte->node.parent);
531
532         err = rhashtable_remove_fast(&fg->ftes_hash,
533                                      &fte->hash,
534                                      rhash_fte);
535         WARN_ON(err);
536         ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
537         kmem_cache_free(steering->ftes_cache, fte);
538 }
539
540 static void del_hw_flow_group(struct fs_node *node)
541 {
542         struct mlx5_flow_group *fg;
543         struct mlx5_flow_table *ft;
544         struct mlx5_core_dev *dev;
545
546         fs_get_obj(fg, node);
547         fs_get_obj(ft, fg->node.parent);
548         dev = get_dev(&ft->node);
549         trace_mlx5_fs_del_fg(fg);
550
551         if (fg->node.active && mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
552                 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
553                                fg->id, ft->id);
554 }
555
556 static void del_sw_flow_group(struct fs_node *node)
557 {
558         struct mlx5_flow_steering *steering = get_steering(node);
559         struct mlx5_flow_group *fg;
560         struct mlx5_flow_table *ft;
561         int err;
562
563         fs_get_obj(fg, node);
564         fs_get_obj(ft, fg->node.parent);
565
566         rhashtable_destroy(&fg->ftes_hash);
567         ida_destroy(&fg->fte_allocator);
568         if (ft->autogroup.active)
569                 ft->autogroup.num_groups--;
570         err = rhltable_remove(&ft->fgs_hash,
571                               &fg->hash,
572                               rhash_fg);
573         WARN_ON(err);
574         kmem_cache_free(steering->fgs_cache, fg);
575 }
576
577 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
578 {
579         int index;
580         int ret;
581
582         index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
583         if (index < 0)
584                 return index;
585
586         fte->index = index + fg->start_index;
587         ret = rhashtable_insert_fast(&fg->ftes_hash,
588                                      &fte->hash,
589                                      rhash_fte);
590         if (ret)
591                 goto err_ida_remove;
592
593         tree_add_node(&fte->node, &fg->node);
594         list_add_tail(&fte->node.list, &fg->node.children);
595         return 0;
596
597 err_ida_remove:
598         ida_simple_remove(&fg->fte_allocator, index);
599         return ret;
600 }
601
602 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
603                                 u32 *match_value,
604                                 struct mlx5_flow_act *flow_act)
605 {
606         struct mlx5_flow_steering *steering = get_steering(&ft->node);
607         struct fs_fte *fte;
608
609         fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
610         if (!fte)
611                 return ERR_PTR(-ENOMEM);
612
613         memcpy(fte->val, match_value, sizeof(fte->val));
614         fte->node.type =  FS_TYPE_FLOW_ENTRY;
615         fte->flow_tag = flow_act->flow_tag;
616         fte->action = flow_act->action;
617         fte->encap_id = flow_act->encap_id;
618         fte->modify_id = flow_act->modify_id;
619
620         tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
621
622         return fte;
623 }
624
625 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
626                                struct mlx5_flow_group *fg)
627 {
628         rhashtable_destroy(&fg->ftes_hash);
629         kmem_cache_free(steering->fgs_cache, fg);
630 }
631
632 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
633                                                 u8 match_criteria_enable,
634                                                 void *match_criteria,
635                                                 int start_index,
636                                                 int end_index)
637 {
638         struct mlx5_flow_group *fg;
639         int ret;
640
641         fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
642         if (!fg)
643                 return ERR_PTR(-ENOMEM);
644
645         ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
646         if (ret) {
647                 kmem_cache_free(steering->fgs_cache, fg);
648                 return ERR_PTR(ret);
649 }
650         ida_init(&fg->fte_allocator);
651         fg->mask.match_criteria_enable = match_criteria_enable;
652         memcpy(&fg->mask.match_criteria, match_criteria,
653                sizeof(fg->mask.match_criteria));
654         fg->node.type =  FS_TYPE_FLOW_GROUP;
655         fg->start_index = start_index;
656         fg->max_ftes = end_index - start_index + 1;
657
658         return fg;
659 }
660
661 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
662                                                        u8 match_criteria_enable,
663                                                        void *match_criteria,
664                                                        int start_index,
665                                                        int end_index,
666                                                        struct list_head *prev)
667 {
668         struct mlx5_flow_steering *steering = get_steering(&ft->node);
669         struct mlx5_flow_group *fg;
670         int ret;
671
672         fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
673                               start_index, end_index);
674         if (IS_ERR(fg))
675                 return fg;
676
677         /* initialize refcnt, add to parent list */
678         ret = rhltable_insert(&ft->fgs_hash,
679                               &fg->hash,
680                               rhash_fg);
681         if (ret) {
682                 dealloc_flow_group(steering, fg);
683                 return ERR_PTR(ret);
684         }
685
686         tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
687         tree_add_node(&fg->node, &ft->node);
688         /* Add node to group list */
689         list_add(&fg->node.list, prev);
690         atomic_inc(&ft->node.version);
691
692         return fg;
693 }
694
695 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
696                                                 enum fs_flow_table_type table_type,
697                                                 enum fs_flow_table_op_mod op_mod,
698                                                 u32 flags)
699 {
700         struct mlx5_flow_table *ft;
701         int ret;
702
703         ft  = kzalloc(sizeof(*ft), GFP_KERNEL);
704         if (!ft)
705                 return ERR_PTR(-ENOMEM);
706
707         ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
708         if (ret) {
709                 kfree(ft);
710                 return ERR_PTR(ret);
711         }
712
713         ft->level = level;
714         ft->node.type = FS_TYPE_FLOW_TABLE;
715         ft->op_mod = op_mod;
716         ft->type = table_type;
717         ft->vport = vport;
718         ft->max_fte = max_fte;
719         ft->flags = flags;
720         INIT_LIST_HEAD(&ft->fwd_rules);
721         mutex_init(&ft->lock);
722
723         return ft;
724 }
725
726 /* If reverse is false, then we search for the first flow table in the
727  * root sub-tree from start(closest from right), else we search for the
728  * last flow table in the root sub-tree till start(closest from left).
729  */
730 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node  *root,
731                                                          struct list_head *start,
732                                                          bool reverse)
733 {
734 #define list_advance_entry(pos, reverse)                \
735         ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
736
737 #define list_for_each_advance_continue(pos, head, reverse)      \
738         for (pos = list_advance_entry(pos, reverse);            \
739              &pos->list != (head);                              \
740              pos = list_advance_entry(pos, reverse))
741
742         struct fs_node *iter = list_entry(start, struct fs_node, list);
743         struct mlx5_flow_table *ft = NULL;
744
745         if (!root)
746                 return NULL;
747
748         list_for_each_advance_continue(iter, &root->children, reverse) {
749                 if (iter->type == FS_TYPE_FLOW_TABLE) {
750                         fs_get_obj(ft, iter);
751                         return ft;
752                 }
753                 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
754                 if (ft)
755                         return ft;
756         }
757
758         return ft;
759 }
760
761 /* If reverse if false then return the first flow table in next priority of
762  * prio in the tree, else return the last flow table in the previous priority
763  * of prio in the tree.
764  */
765 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
766 {
767         struct mlx5_flow_table *ft = NULL;
768         struct fs_node *curr_node;
769         struct fs_node *parent;
770
771         parent = prio->node.parent;
772         curr_node = &prio->node;
773         while (!ft && parent) {
774                 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
775                 curr_node = parent;
776                 parent = curr_node->parent;
777         }
778         return ft;
779 }
780
781 /* Assuming all the tree is locked by mutex chain lock */
782 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
783 {
784         return find_closest_ft(prio, false);
785 }
786
787 /* Assuming all the tree is locked by mutex chain lock */
788 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
789 {
790         return find_closest_ft(prio, true);
791 }
792
793 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
794                                struct fs_prio *prio,
795                                struct mlx5_flow_table *ft)
796 {
797         struct mlx5_flow_table *iter;
798         int i = 0;
799         int err;
800
801         fs_for_each_ft(iter, prio) {
802                 i++;
803                 err = mlx5_cmd_modify_flow_table(dev,
804                                                  iter,
805                                                  ft);
806                 if (err) {
807                         mlx5_core_warn(dev, "Failed to modify flow table %d\n",
808                                        iter->id);
809                         /* The driver is out of sync with the FW */
810                         if (i > 1)
811                                 WARN_ON(true);
812                         return err;
813                 }
814         }
815         return 0;
816 }
817
818 /* Connect flow tables from previous priority of prio to ft */
819 static int connect_prev_fts(struct mlx5_core_dev *dev,
820                             struct mlx5_flow_table *ft,
821                             struct fs_prio *prio)
822 {
823         struct mlx5_flow_table *prev_ft;
824
825         prev_ft = find_prev_chained_ft(prio);
826         if (prev_ft) {
827                 struct fs_prio *prev_prio;
828
829                 fs_get_obj(prev_prio, prev_ft->node.parent);
830                 return connect_fts_in_prio(dev, prev_prio, ft);
831         }
832         return 0;
833 }
834
835 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
836                                  *prio)
837 {
838         struct mlx5_flow_root_namespace *root = find_root(&prio->node);
839         struct mlx5_ft_underlay_qp *uqp;
840         int min_level = INT_MAX;
841         int err;
842         u32 qpn;
843
844         if (root->root_ft)
845                 min_level = root->root_ft->level;
846
847         if (ft->level >= min_level)
848                 return 0;
849
850         if (list_empty(&root->underlay_qpns)) {
851                 /* Don't set any QPN (zero) in case QPN list is empty */
852                 qpn = 0;
853                 err = mlx5_cmd_update_root_ft(root->dev, ft, qpn, false);
854         } else {
855                 list_for_each_entry(uqp, &root->underlay_qpns, list) {
856                         qpn = uqp->qpn;
857                         err = mlx5_cmd_update_root_ft(root->dev, ft, qpn,
858                                                       false);
859                         if (err)
860                                 break;
861                 }
862         }
863
864         if (err)
865                 mlx5_core_warn(root->dev,
866                                "Update root flow table of id(%u) qpn(%d) failed\n",
867                                ft->id, qpn);
868         else
869                 root->root_ft = ft;
870
871         return err;
872 }
873
874 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
875                                          struct mlx5_flow_destination *dest)
876 {
877         struct mlx5_flow_table *ft;
878         struct mlx5_flow_group *fg;
879         struct fs_fte *fte;
880         int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
881         int err = 0;
882
883         fs_get_obj(fte, rule->node.parent);
884         if (!(fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
885                 return -EINVAL;
886         down_write_ref_node(&fte->node);
887         fs_get_obj(fg, fte->node.parent);
888         fs_get_obj(ft, fg->node.parent);
889
890         memcpy(&rule->dest_attr, dest, sizeof(*dest));
891         err = mlx5_cmd_update_fte(get_dev(&ft->node),
892                                   ft, fg->id,
893                                   modify_mask,
894                                   fte);
895         up_write_ref_node(&fte->node);
896
897         return err;
898 }
899
900 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
901                                  struct mlx5_flow_destination *new_dest,
902                                  struct mlx5_flow_destination *old_dest)
903 {
904         int i;
905
906         if (!old_dest) {
907                 if (handle->num_rules != 1)
908                         return -EINVAL;
909                 return _mlx5_modify_rule_destination(handle->rule[0],
910                                                      new_dest);
911         }
912
913         for (i = 0; i < handle->num_rules; i++) {
914                 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
915                         return _mlx5_modify_rule_destination(handle->rule[i],
916                                                              new_dest);
917         }
918
919         return -EINVAL;
920 }
921
922 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft  */
923 static int connect_fwd_rules(struct mlx5_core_dev *dev,
924                              struct mlx5_flow_table *new_next_ft,
925                              struct mlx5_flow_table *old_next_ft)
926 {
927         struct mlx5_flow_destination dest = {};
928         struct mlx5_flow_rule *iter;
929         int err = 0;
930
931         /* new_next_ft and old_next_ft could be NULL only
932          * when we create/destroy the anchor flow table.
933          */
934         if (!new_next_ft || !old_next_ft)
935                 return 0;
936
937         dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
938         dest.ft = new_next_ft;
939
940         mutex_lock(&old_next_ft->lock);
941         list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
942         mutex_unlock(&old_next_ft->lock);
943         list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
944                 err = _mlx5_modify_rule_destination(iter, &dest);
945                 if (err)
946                         pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
947                                new_next_ft->id);
948         }
949         return 0;
950 }
951
952 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
953                               struct fs_prio *prio)
954 {
955         struct mlx5_flow_table *next_ft;
956         int err = 0;
957
958         /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
959
960         if (list_empty(&prio->node.children)) {
961                 err = connect_prev_fts(dev, ft, prio);
962                 if (err)
963                         return err;
964
965                 next_ft = find_next_chained_ft(prio);
966                 err = connect_fwd_rules(dev, ft, next_ft);
967                 if (err)
968                         return err;
969         }
970
971         if (MLX5_CAP_FLOWTABLE(dev,
972                                flow_table_properties_nic_receive.modify_root))
973                 err = update_root_ft_create(ft, prio);
974         return err;
975 }
976
977 static void list_add_flow_table(struct mlx5_flow_table *ft,
978                                 struct fs_prio *prio)
979 {
980         struct list_head *prev = &prio->node.children;
981         struct mlx5_flow_table *iter;
982
983         fs_for_each_ft(iter, prio) {
984                 if (iter->level > ft->level)
985                         break;
986                 prev = &iter->node.list;
987         }
988         list_add(&ft->node.list, prev);
989 }
990
991 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
992                                                         struct mlx5_flow_table_attr *ft_attr,
993                                                         enum fs_flow_table_op_mod op_mod,
994                                                         u16 vport)
995 {
996         struct mlx5_flow_root_namespace *root = find_root(&ns->node);
997         struct mlx5_flow_table *next_ft = NULL;
998         struct fs_prio *fs_prio = NULL;
999         struct mlx5_flow_table *ft;
1000         int log_table_sz;
1001         int err;
1002
1003         if (!root) {
1004                 pr_err("mlx5: flow steering failed to find root of namespace\n");
1005                 return ERR_PTR(-ENODEV);
1006         }
1007
1008         mutex_lock(&root->chain_lock);
1009         fs_prio = find_prio(ns, ft_attr->prio);
1010         if (!fs_prio) {
1011                 err = -EINVAL;
1012                 goto unlock_root;
1013         }
1014         if (ft_attr->level >= fs_prio->num_levels) {
1015                 err = -ENOSPC;
1016                 goto unlock_root;
1017         }
1018         /* The level is related to the
1019          * priority level range.
1020          */
1021         ft_attr->level += fs_prio->start_level;
1022         ft = alloc_flow_table(ft_attr->level,
1023                               vport,
1024                               ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
1025                               root->table_type,
1026                               op_mod, ft_attr->flags);
1027         if (IS_ERR(ft)) {
1028                 err = PTR_ERR(ft);
1029                 goto unlock_root;
1030         }
1031
1032         tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1033         log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
1034         next_ft = find_next_chained_ft(fs_prio);
1035         err = mlx5_cmd_create_flow_table(root->dev, ft->vport, ft->op_mod, ft->type,
1036                                          ft->level, log_table_sz, next_ft, &ft->id,
1037                                          ft->flags);
1038         if (err)
1039                 goto free_ft;
1040
1041         err = connect_flow_table(root->dev, ft, fs_prio);
1042         if (err)
1043                 goto destroy_ft;
1044         ft->node.active = true;
1045         down_write_ref_node(&fs_prio->node);
1046         tree_add_node(&ft->node, &fs_prio->node);
1047         list_add_flow_table(ft, fs_prio);
1048         fs_prio->num_ft++;
1049         up_write_ref_node(&fs_prio->node);
1050         mutex_unlock(&root->chain_lock);
1051         return ft;
1052 destroy_ft:
1053         mlx5_cmd_destroy_flow_table(root->dev, ft);
1054 free_ft:
1055         kfree(ft);
1056 unlock_root:
1057         mutex_unlock(&root->chain_lock);
1058         return ERR_PTR(err);
1059 }
1060
1061 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1062                                                struct mlx5_flow_table_attr *ft_attr)
1063 {
1064         return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1065 }
1066
1067 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1068                                                      int prio, int max_fte,
1069                                                      u32 level, u16 vport)
1070 {
1071         struct mlx5_flow_table_attr ft_attr = {};
1072
1073         ft_attr.max_fte = max_fte;
1074         ft_attr.level   = level;
1075         ft_attr.prio    = prio;
1076
1077         return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1078 }
1079
1080 struct mlx5_flow_table*
1081 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1082                                  int prio, u32 level)
1083 {
1084         struct mlx5_flow_table_attr ft_attr = {};
1085
1086         ft_attr.level = level;
1087         ft_attr.prio  = prio;
1088         return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1089 }
1090 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1091
1092 struct mlx5_flow_table*
1093 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1094                                     int prio,
1095                                     int num_flow_table_entries,
1096                                     int max_num_groups,
1097                                     u32 level,
1098                                     u32 flags)
1099 {
1100         struct mlx5_flow_table_attr ft_attr = {};
1101         struct mlx5_flow_table *ft;
1102
1103         if (max_num_groups > num_flow_table_entries)
1104                 return ERR_PTR(-EINVAL);
1105
1106         ft_attr.max_fte = num_flow_table_entries;
1107         ft_attr.prio    = prio;
1108         ft_attr.level   = level;
1109         ft_attr.flags   = flags;
1110
1111         ft = mlx5_create_flow_table(ns, &ft_attr);
1112         if (IS_ERR(ft))
1113                 return ft;
1114
1115         ft->autogroup.active = true;
1116         ft->autogroup.required_groups = max_num_groups;
1117
1118         return ft;
1119 }
1120 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1121
1122 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1123                                                u32 *fg_in)
1124 {
1125         void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1126                                             fg_in, match_criteria);
1127         u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1128                                             fg_in,
1129                                             match_criteria_enable);
1130         int start_index = MLX5_GET(create_flow_group_in, fg_in,
1131                                    start_flow_index);
1132         int end_index = MLX5_GET(create_flow_group_in, fg_in,
1133                                  end_flow_index);
1134         struct mlx5_core_dev *dev = get_dev(&ft->node);
1135         struct mlx5_flow_group *fg;
1136         int err;
1137
1138         if (!check_valid_mask(match_criteria_enable, match_criteria))
1139                 return ERR_PTR(-EINVAL);
1140
1141         if (ft->autogroup.active)
1142                 return ERR_PTR(-EPERM);
1143
1144         down_write_ref_node(&ft->node);
1145         fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1146                                      start_index, end_index,
1147                                      ft->node.children.prev);
1148         up_write_ref_node(&ft->node);
1149         if (IS_ERR(fg))
1150                 return fg;
1151
1152         err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
1153         if (err) {
1154                 tree_put_node(&fg->node);
1155                 return ERR_PTR(err);
1156         }
1157         trace_mlx5_fs_add_fg(fg);
1158         fg->node.active = true;
1159
1160         return fg;
1161 }
1162
1163 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1164 {
1165         struct mlx5_flow_rule *rule;
1166
1167         rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1168         if (!rule)
1169                 return NULL;
1170
1171         INIT_LIST_HEAD(&rule->next_ft);
1172         rule->node.type = FS_TYPE_FLOW_DEST;
1173         if (dest)
1174                 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1175
1176         return rule;
1177 }
1178
1179 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1180 {
1181         struct mlx5_flow_handle *handle;
1182
1183         handle = kzalloc(sizeof(*handle) + sizeof(handle->rule[0]) *
1184                           num_rules, GFP_KERNEL);
1185         if (!handle)
1186                 return NULL;
1187
1188         handle->num_rules = num_rules;
1189
1190         return handle;
1191 }
1192
1193 static void destroy_flow_handle(struct fs_fte *fte,
1194                                 struct mlx5_flow_handle *handle,
1195                                 struct mlx5_flow_destination *dest,
1196                                 int i)
1197 {
1198         for (; --i >= 0;) {
1199                 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1200                         fte->dests_size--;
1201                         list_del(&handle->rule[i]->node.list);
1202                         kfree(handle->rule[i]);
1203                 }
1204         }
1205         kfree(handle);
1206 }
1207
1208 static struct mlx5_flow_handle *
1209 create_flow_handle(struct fs_fte *fte,
1210                    struct mlx5_flow_destination *dest,
1211                    int dest_num,
1212                    int *modify_mask,
1213                    bool *new_rule)
1214 {
1215         struct mlx5_flow_handle *handle;
1216         struct mlx5_flow_rule *rule = NULL;
1217         static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1218         static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1219         int type;
1220         int i = 0;
1221
1222         handle = alloc_handle((dest_num) ? dest_num : 1);
1223         if (!handle)
1224                 return ERR_PTR(-ENOMEM);
1225
1226         do {
1227                 if (dest) {
1228                         rule = find_flow_rule(fte, dest + i);
1229                         if (rule) {
1230                                 refcount_inc(&rule->node.refcount);
1231                                 goto rule_found;
1232                         }
1233                 }
1234
1235                 *new_rule = true;
1236                 rule = alloc_rule(dest + i);
1237                 if (!rule)
1238                         goto free_rules;
1239
1240                 /* Add dest to dests list- we need flow tables to be in the
1241                  * end of the list for forward to next prio rules.
1242                  */
1243                 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1244                 if (dest &&
1245                     dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1246                         list_add(&rule->node.list, &fte->node.children);
1247                 else
1248                         list_add_tail(&rule->node.list, &fte->node.children);
1249                 if (dest) {
1250                         fte->dests_size++;
1251
1252                         type = dest[i].type ==
1253                                 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1254                         *modify_mask |= type ? count : dst;
1255                 }
1256 rule_found:
1257                 handle->rule[i] = rule;
1258         } while (++i < dest_num);
1259
1260         return handle;
1261
1262 free_rules:
1263         destroy_flow_handle(fte, handle, dest, i);
1264         return ERR_PTR(-ENOMEM);
1265 }
1266
1267 /* fte should not be deleted while calling this function */
1268 static struct mlx5_flow_handle *
1269 add_rule_fte(struct fs_fte *fte,
1270              struct mlx5_flow_group *fg,
1271              struct mlx5_flow_destination *dest,
1272              int dest_num,
1273              bool update_action)
1274 {
1275         struct mlx5_flow_handle *handle;
1276         struct mlx5_flow_table *ft;
1277         int modify_mask = 0;
1278         int err;
1279         bool new_rule = false;
1280
1281         handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1282                                     &new_rule);
1283         if (IS_ERR(handle) || !new_rule)
1284                 goto out;
1285
1286         if (update_action)
1287                 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1288
1289         fs_get_obj(ft, fg->node.parent);
1290         if (!(fte->status & FS_FTE_STATUS_EXISTING))
1291                 err = mlx5_cmd_create_fte(get_dev(&ft->node),
1292                                           ft, fg->id, fte);
1293         else
1294                 err = mlx5_cmd_update_fte(get_dev(&ft->node),
1295                                           ft, fg->id, modify_mask, fte);
1296         if (err)
1297                 goto free_handle;
1298
1299         fte->node.active = true;
1300         fte->status |= FS_FTE_STATUS_EXISTING;
1301         atomic_inc(&fte->node.version);
1302
1303 out:
1304         return handle;
1305
1306 free_handle:
1307         destroy_flow_handle(fte, handle, dest, handle->num_rules);
1308         return ERR_PTR(err);
1309 }
1310
1311 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table  *ft,
1312                                                      struct mlx5_flow_spec *spec)
1313 {
1314         struct list_head *prev = &ft->node.children;
1315         struct mlx5_flow_group *fg;
1316         unsigned int candidate_index = 0;
1317         unsigned int group_size = 0;
1318
1319         if (!ft->autogroup.active)
1320                 return ERR_PTR(-ENOENT);
1321
1322         if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1323                 /* We save place for flow groups in addition to max types */
1324                 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
1325
1326         /*  ft->max_fte == ft->autogroup.max_types */
1327         if (group_size == 0)
1328                 group_size = 1;
1329
1330         /* sorted by start_index */
1331         fs_for_each_fg(fg, ft) {
1332                 if (candidate_index + group_size > fg->start_index)
1333                         candidate_index = fg->start_index + fg->max_ftes;
1334                 else
1335                         break;
1336                 prev = &fg->node.list;
1337         }
1338
1339         if (candidate_index + group_size > ft->max_fte)
1340                 return ERR_PTR(-ENOSPC);
1341
1342         fg = alloc_insert_flow_group(ft,
1343                                      spec->match_criteria_enable,
1344                                      spec->match_criteria,
1345                                      candidate_index,
1346                                      candidate_index + group_size - 1,
1347                                      prev);
1348         if (IS_ERR(fg))
1349                 goto out;
1350
1351         ft->autogroup.num_groups++;
1352
1353 out:
1354         return fg;
1355 }
1356
1357 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1358                                   struct mlx5_flow_group *fg)
1359 {
1360         struct mlx5_core_dev *dev = get_dev(&ft->node);
1361         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1362         void *match_criteria_addr;
1363         int err;
1364         u32 *in;
1365
1366         in = kvzalloc(inlen, GFP_KERNEL);
1367         if (!in)
1368                 return -ENOMEM;
1369
1370         MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1371                  fg->mask.match_criteria_enable);
1372         MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1373         MLX5_SET(create_flow_group_in, in, end_flow_index,   fg->start_index +
1374                  fg->max_ftes - 1);
1375         match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1376                                            in, match_criteria);
1377         memcpy(match_criteria_addr, fg->mask.match_criteria,
1378                sizeof(fg->mask.match_criteria));
1379
1380         err = mlx5_cmd_create_flow_group(dev, ft, in, &fg->id);
1381         if (!err) {
1382                 fg->node.active = true;
1383                 trace_mlx5_fs_add_fg(fg);
1384         }
1385
1386         kvfree(in);
1387         return err;
1388 }
1389
1390 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1391                                 struct mlx5_flow_destination *d2)
1392 {
1393         if (d1->type == d2->type) {
1394                 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1395                      d1->vport_num == d2->vport_num) ||
1396                     (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1397                      d1->ft == d2->ft) ||
1398                     (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1399                      d1->tir_num == d2->tir_num))
1400                         return true;
1401         }
1402
1403         return false;
1404 }
1405
1406 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1407                                              struct mlx5_flow_destination *dest)
1408 {
1409         struct mlx5_flow_rule *rule;
1410
1411         list_for_each_entry(rule, &fte->node.children, node.list) {
1412                 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1413                         return rule;
1414         }
1415         return NULL;
1416 }
1417
1418 static bool check_conflicting_actions(u32 action1, u32 action2)
1419 {
1420         u32 xored_actions = action1 ^ action2;
1421
1422         /* if one rule only wants to count, it's ok */
1423         if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1424             action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1425                 return false;
1426
1427         if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP  |
1428                              MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1429                              MLX5_FLOW_CONTEXT_ACTION_DECAP))
1430                 return true;
1431
1432         return false;
1433 }
1434
1435 static int check_conflicting_ftes(struct fs_fte *fte, const struct mlx5_flow_act *flow_act)
1436 {
1437         if (check_conflicting_actions(flow_act->action, fte->action)) {
1438                 mlx5_core_warn(get_dev(&fte->node),
1439                                "Found two FTEs with conflicting actions\n");
1440                 return -EEXIST;
1441         }
1442
1443         if (fte->flow_tag != flow_act->flow_tag) {
1444                 mlx5_core_warn(get_dev(&fte->node),
1445                                "FTE flow tag %u already exists with different flow tag %u\n",
1446                                fte->flow_tag,
1447                                flow_act->flow_tag);
1448                 return -EEXIST;
1449         }
1450
1451         return 0;
1452 }
1453
1454 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1455                                             u32 *match_value,
1456                                             struct mlx5_flow_act *flow_act,
1457                                             struct mlx5_flow_destination *dest,
1458                                             int dest_num,
1459                                             struct fs_fte *fte)
1460 {
1461         struct mlx5_flow_handle *handle;
1462         int old_action;
1463         int i;
1464         int ret;
1465
1466         ret = check_conflicting_ftes(fte, flow_act);
1467         if (ret)
1468                 return ERR_PTR(ret);
1469
1470         old_action = fte->action;
1471         fte->action |= flow_act->action;
1472         handle = add_rule_fte(fte, fg, dest, dest_num,
1473                               old_action != flow_act->action);
1474         if (IS_ERR(handle)) {
1475                 fte->action = old_action;
1476                 return handle;
1477         }
1478         trace_mlx5_fs_set_fte(fte, false);
1479
1480         for (i = 0; i < handle->num_rules; i++) {
1481                 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1482                         tree_add_node(&handle->rule[i]->node, &fte->node);
1483                         trace_mlx5_fs_add_rule(handle->rule[i]);
1484                 }
1485         }
1486         return handle;
1487 }
1488
1489 struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_handle *handle)
1490 {
1491         struct mlx5_flow_rule *dst;
1492         struct fs_fte *fte;
1493
1494         fs_get_obj(fte, handle->rule[0]->node.parent);
1495
1496         fs_for_each_dst(dst, fte) {
1497                 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
1498                         return dst->dest_attr.counter;
1499         }
1500
1501         return NULL;
1502 }
1503
1504 static bool counter_is_valid(struct mlx5_fc *counter, u32 action)
1505 {
1506         if (!(action & MLX5_FLOW_CONTEXT_ACTION_COUNT))
1507                 return !counter;
1508
1509         if (!counter)
1510                 return false;
1511
1512         return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1513                           MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1514 }
1515
1516 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1517                           u32 action,
1518                           struct mlx5_flow_table *ft)
1519 {
1520         if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1521                 return counter_is_valid(dest->counter, action);
1522
1523         if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1524                 return true;
1525
1526         if (!dest || ((dest->type ==
1527             MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1528             (dest->ft->level <= ft->level)))
1529                 return false;
1530         return true;
1531 }
1532
1533 struct match_list {
1534         struct list_head        list;
1535         struct mlx5_flow_group *g;
1536 };
1537
1538 struct match_list_head {
1539         struct list_head  list;
1540         struct match_list first;
1541 };
1542
1543 static void free_match_list(struct match_list_head *head)
1544 {
1545         if (!list_empty(&head->list)) {
1546                 struct match_list *iter, *match_tmp;
1547
1548                 list_del(&head->first.list);
1549                 tree_put_node(&head->first.g->node);
1550                 list_for_each_entry_safe(iter, match_tmp, &head->list,
1551                                          list) {
1552                         tree_put_node(&iter->g->node);
1553                         list_del(&iter->list);
1554                         kfree(iter);
1555                 }
1556         }
1557 }
1558
1559 static int build_match_list(struct match_list_head *match_head,
1560                             struct mlx5_flow_table *ft,
1561                             struct mlx5_flow_spec *spec)
1562 {
1563         struct rhlist_head *tmp, *list;
1564         struct mlx5_flow_group *g;
1565         int err = 0;
1566
1567         rcu_read_lock();
1568         INIT_LIST_HEAD(&match_head->list);
1569         /* Collect all fgs which has a matching match_criteria */
1570         list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1571         /* RCU is atomic, we can't execute FW commands here */
1572         rhl_for_each_entry_rcu(g, tmp, list, hash) {
1573                 struct match_list *curr_match;
1574
1575                 if (likely(list_empty(&match_head->list))) {
1576                         if (!tree_get_node(&g->node))
1577                                 continue;
1578                         match_head->first.g = g;
1579                         list_add_tail(&match_head->first.list,
1580                                       &match_head->list);
1581                         continue;
1582                 }
1583
1584                 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1585                 if (!curr_match) {
1586                         free_match_list(match_head);
1587                         err = -ENOMEM;
1588                         goto out;
1589                 }
1590                 if (!tree_get_node(&g->node)) {
1591                         kfree(curr_match);
1592                         continue;
1593                 }
1594                 curr_match->g = g;
1595                 list_add_tail(&curr_match->list, &match_head->list);
1596         }
1597 out:
1598         rcu_read_unlock();
1599         return err;
1600 }
1601
1602 static u64 matched_fgs_get_version(struct list_head *match_head)
1603 {
1604         struct match_list *iter;
1605         u64 version = 0;
1606
1607         list_for_each_entry(iter, match_head, list)
1608                 version += (u64)atomic_read(&iter->g->node.version);
1609         return version;
1610 }
1611
1612 static struct mlx5_flow_handle *
1613 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1614                        struct list_head *match_head,
1615                        struct mlx5_flow_spec *spec,
1616                        struct mlx5_flow_act *flow_act,
1617                        struct mlx5_flow_destination *dest,
1618                        int dest_num,
1619                        int ft_version)
1620 {
1621         struct mlx5_flow_steering *steering = get_steering(&ft->node);
1622         struct mlx5_flow_group *g;
1623         struct mlx5_flow_handle *rule;
1624         struct match_list *iter;
1625         bool take_write = false;
1626         struct fs_fte *fte;
1627         u64  version;
1628         int err;
1629
1630         fte = alloc_fte(ft, spec->match_value, flow_act);
1631         if (IS_ERR(fte))
1632                 return  ERR_PTR(-ENOMEM);
1633
1634         list_for_each_entry(iter, match_head, list) {
1635                 nested_down_read_ref_node(&iter->g->node, FS_LOCK_PARENT);
1636                 ida_pre_get(&iter->g->fte_allocator, GFP_KERNEL);
1637         }
1638
1639 search_again_locked:
1640         version = matched_fgs_get_version(match_head);
1641         /* Try to find a fg that already contains a matching fte */
1642         list_for_each_entry(iter, match_head, list) {
1643                 struct fs_fte *fte_tmp;
1644
1645                 g = iter->g;
1646                 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, spec->match_value,
1647                                                  rhash_fte);
1648                 if (!fte_tmp || !tree_get_node(&fte_tmp->node))
1649                         continue;
1650
1651                 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1652                 if (!take_write) {
1653                         list_for_each_entry(iter, match_head, list)
1654                                 up_read_ref_node(&iter->g->node);
1655                 } else {
1656                         list_for_each_entry(iter, match_head, list)
1657                                 up_write_ref_node(&iter->g->node);
1658                 }
1659
1660                 rule = add_rule_fg(g, spec->match_value,
1661                                    flow_act, dest, dest_num, fte_tmp);
1662                 up_write_ref_node(&fte_tmp->node);
1663                 tree_put_node(&fte_tmp->node);
1664                 kmem_cache_free(steering->ftes_cache, fte);
1665                 return rule;
1666         }
1667
1668         /* No group with matching fte found. Try to add a new fte to any
1669          * matching fg.
1670          */
1671
1672         if (!take_write) {
1673                 list_for_each_entry(iter, match_head, list)
1674                         up_read_ref_node(&iter->g->node);
1675                 list_for_each_entry(iter, match_head, list)
1676                         nested_down_write_ref_node(&iter->g->node,
1677                                                    FS_LOCK_PARENT);
1678                 take_write = true;
1679         }
1680
1681         /* Check the ft version, for case that new flow group
1682          * was added while the fgs weren't locked
1683          */
1684         if (atomic_read(&ft->node.version) != ft_version) {
1685                 rule = ERR_PTR(-EAGAIN);
1686                 goto out;
1687         }
1688
1689         /* Check the fgs version, for case the new FTE with the
1690          * same values was added while the fgs weren't locked
1691          */
1692         if (version != matched_fgs_get_version(match_head))
1693                 goto search_again_locked;
1694
1695         list_for_each_entry(iter, match_head, list) {
1696                 g = iter->g;
1697
1698                 if (!g->node.active)
1699                         continue;
1700                 err = insert_fte(g, fte);
1701                 if (err) {
1702                         if (err == -ENOSPC)
1703                                 continue;
1704                         list_for_each_entry(iter, match_head, list)
1705                                 up_write_ref_node(&iter->g->node);
1706                         kmem_cache_free(steering->ftes_cache, fte);
1707                         return ERR_PTR(err);
1708                 }
1709
1710                 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1711                 list_for_each_entry(iter, match_head, list)
1712                         up_write_ref_node(&iter->g->node);
1713                 rule = add_rule_fg(g, spec->match_value,
1714                                    flow_act, dest, dest_num, fte);
1715                 up_write_ref_node(&fte->node);
1716                 tree_put_node(&fte->node);
1717                 return rule;
1718         }
1719         rule = ERR_PTR(-ENOENT);
1720 out:
1721         list_for_each_entry(iter, match_head, list)
1722                 up_write_ref_node(&iter->g->node);
1723         kmem_cache_free(steering->ftes_cache, fte);
1724         return rule;
1725 }
1726
1727 static struct mlx5_flow_handle *
1728 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1729                      struct mlx5_flow_spec *spec,
1730                      struct mlx5_flow_act *flow_act,
1731                      struct mlx5_flow_destination *dest,
1732                      int dest_num)
1733
1734 {
1735         struct mlx5_flow_steering *steering = get_steering(&ft->node);
1736         struct mlx5_flow_group *g;
1737         struct mlx5_flow_handle *rule;
1738         struct match_list_head match_head;
1739         bool take_write = false;
1740         struct fs_fte *fte;
1741         int version;
1742         int err;
1743         int i;
1744
1745         if (!check_valid_spec(spec))
1746                 return ERR_PTR(-EINVAL);
1747
1748         for (i = 0; i < dest_num; i++) {
1749                 if (!dest_is_valid(&dest[i], flow_act->action, ft))
1750                         return ERR_PTR(-EINVAL);
1751         }
1752         nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1753 search_again_locked:
1754         version = atomic_read(&ft->node.version);
1755
1756         /* Collect all fgs which has a matching match_criteria */
1757         err = build_match_list(&match_head, ft, spec);
1758         if (err)
1759                 return ERR_PTR(err);
1760
1761         if (!take_write)
1762                 up_read_ref_node(&ft->node);
1763
1764         rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1765                                       dest_num, version);
1766         free_match_list(&match_head);
1767         if (!IS_ERR(rule) ||
1768             (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN))
1769                 return rule;
1770
1771         if (!take_write) {
1772                 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1773                 take_write = true;
1774         }
1775
1776         if (PTR_ERR(rule) == -EAGAIN ||
1777             version != atomic_read(&ft->node.version))
1778                 goto search_again_locked;
1779
1780         g = alloc_auto_flow_group(ft, spec);
1781         if (IS_ERR(g)) {
1782                 rule = (void *)g;
1783                 up_write_ref_node(&ft->node);
1784                 return rule;
1785         }
1786
1787         nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1788         up_write_ref_node(&ft->node);
1789
1790         err = create_auto_flow_group(ft, g);
1791         if (err)
1792                 goto err_release_fg;
1793
1794         fte = alloc_fte(ft, spec->match_value, flow_act);
1795         if (IS_ERR(fte)) {
1796                 err = PTR_ERR(fte);
1797                 goto err_release_fg;
1798         }
1799
1800         err = insert_fte(g, fte);
1801         if (err) {
1802                 kmem_cache_free(steering->ftes_cache, fte);
1803                 goto err_release_fg;
1804         }
1805
1806         nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1807         up_write_ref_node(&g->node);
1808         rule = add_rule_fg(g, spec->match_value, flow_act, dest,
1809                            dest_num, fte);
1810         up_write_ref_node(&fte->node);
1811         tree_put_node(&fte->node);
1812         tree_put_node(&g->node);
1813         return rule;
1814
1815 err_release_fg:
1816         up_write_ref_node(&g->node);
1817         tree_put_node(&g->node);
1818         return ERR_PTR(err);
1819 }
1820
1821 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1822 {
1823         return ((ft->type == FS_FT_NIC_RX) &&
1824                 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1825 }
1826
1827 struct mlx5_flow_handle *
1828 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1829                     struct mlx5_flow_spec *spec,
1830                     struct mlx5_flow_act *flow_act,
1831                     struct mlx5_flow_destination *dest,
1832                     int dest_num)
1833 {
1834         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1835         struct mlx5_flow_destination gen_dest = {};
1836         struct mlx5_flow_table *next_ft = NULL;
1837         struct mlx5_flow_handle *handle = NULL;
1838         u32 sw_action = flow_act->action;
1839         struct fs_prio *prio;
1840
1841         fs_get_obj(prio, ft->node.parent);
1842         if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1843                 if (!fwd_next_prio_supported(ft))
1844                         return ERR_PTR(-EOPNOTSUPP);
1845                 if (dest)
1846                         return ERR_PTR(-EINVAL);
1847                 mutex_lock(&root->chain_lock);
1848                 next_ft = find_next_chained_ft(prio);
1849                 if (next_ft) {
1850                         gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1851                         gen_dest.ft = next_ft;
1852                         dest = &gen_dest;
1853                         dest_num = 1;
1854                         flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1855                 } else {
1856                         mutex_unlock(&root->chain_lock);
1857                         return ERR_PTR(-EOPNOTSUPP);
1858                 }
1859         }
1860
1861         handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, dest_num);
1862
1863         if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1864                 if (!IS_ERR_OR_NULL(handle) &&
1865                     (list_empty(&handle->rule[0]->next_ft))) {
1866                         mutex_lock(&next_ft->lock);
1867                         list_add(&handle->rule[0]->next_ft,
1868                                  &next_ft->fwd_rules);
1869                         mutex_unlock(&next_ft->lock);
1870                         handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1871                 }
1872                 mutex_unlock(&root->chain_lock);
1873         }
1874         return handle;
1875 }
1876 EXPORT_SYMBOL(mlx5_add_flow_rules);
1877
1878 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1879 {
1880         int i;
1881
1882         for (i = handle->num_rules - 1; i >= 0; i--)
1883                 tree_remove_node(&handle->rule[i]->node);
1884         kfree(handle);
1885 }
1886 EXPORT_SYMBOL(mlx5_del_flow_rules);
1887
1888 /* Assuming prio->node.children(flow tables) is sorted by level */
1889 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1890 {
1891         struct fs_prio *prio;
1892
1893         fs_get_obj(prio, ft->node.parent);
1894
1895         if (!list_is_last(&ft->node.list, &prio->node.children))
1896                 return list_next_entry(ft, node.list);
1897         return find_next_chained_ft(prio);
1898 }
1899
1900 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1901 {
1902         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1903         struct mlx5_ft_underlay_qp *uqp;
1904         struct mlx5_flow_table *new_root_ft = NULL;
1905         int err = 0;
1906         u32 qpn;
1907
1908         if (root->root_ft != ft)
1909                 return 0;
1910
1911         new_root_ft = find_next_ft(ft);
1912
1913         if (!new_root_ft) {
1914                 root->root_ft = NULL;
1915                 return 0;
1916         }
1917
1918         if (list_empty(&root->underlay_qpns)) {
1919                 /* Don't set any QPN (zero) in case QPN list is empty */
1920                 qpn = 0;
1921                 err = mlx5_cmd_update_root_ft(root->dev, new_root_ft, qpn,
1922                                               false);
1923         } else {
1924                 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1925                         qpn = uqp->qpn;
1926                         err = mlx5_cmd_update_root_ft(root->dev, new_root_ft,
1927                                                       qpn, false);
1928                         if (err)
1929                                 break;
1930                 }
1931         }
1932
1933         if (err)
1934                 mlx5_core_warn(root->dev,
1935                                "Update root flow table of id(%u) qpn(%d) failed\n",
1936                                ft->id, qpn);
1937         else
1938                 root->root_ft = new_root_ft;
1939
1940         return 0;
1941 }
1942
1943 /* Connect flow table from previous priority to
1944  * the next flow table.
1945  */
1946 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1947 {
1948         struct mlx5_core_dev *dev = get_dev(&ft->node);
1949         struct mlx5_flow_table *next_ft;
1950         struct fs_prio *prio;
1951         int err = 0;
1952
1953         err = update_root_ft_destroy(ft);
1954         if (err)
1955                 return err;
1956
1957         fs_get_obj(prio, ft->node.parent);
1958         if  (!(list_first_entry(&prio->node.children,
1959                                 struct mlx5_flow_table,
1960                                 node.list) == ft))
1961                 return 0;
1962
1963         next_ft = find_next_chained_ft(prio);
1964         err = connect_fwd_rules(dev, next_ft, ft);
1965         if (err)
1966                 return err;
1967
1968         err = connect_prev_fts(dev, next_ft, prio);
1969         if (err)
1970                 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1971                                ft->id);
1972         return err;
1973 }
1974
1975 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1976 {
1977         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1978         int err = 0;
1979
1980         mutex_lock(&root->chain_lock);
1981         err = disconnect_flow_table(ft);
1982         if (err) {
1983                 mutex_unlock(&root->chain_lock);
1984                 return err;
1985         }
1986         if (tree_remove_node(&ft->node))
1987                 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1988                                ft->id);
1989         mutex_unlock(&root->chain_lock);
1990
1991         return err;
1992 }
1993 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1994
1995 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1996 {
1997         if (tree_remove_node(&fg->node))
1998                 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1999                                fg->id);
2000 }
2001
2002 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2003                                                     enum mlx5_flow_namespace_type type)
2004 {
2005         struct mlx5_flow_steering *steering = dev->priv.steering;
2006         struct mlx5_flow_root_namespace *root_ns;
2007         int prio;
2008         struct fs_prio *fs_prio;
2009         struct mlx5_flow_namespace *ns;
2010
2011         if (!steering)
2012                 return NULL;
2013
2014         switch (type) {
2015         case MLX5_FLOW_NAMESPACE_BYPASS:
2016         case MLX5_FLOW_NAMESPACE_LAG:
2017         case MLX5_FLOW_NAMESPACE_OFFLOADS:
2018         case MLX5_FLOW_NAMESPACE_ETHTOOL:
2019         case MLX5_FLOW_NAMESPACE_KERNEL:
2020         case MLX5_FLOW_NAMESPACE_LEFTOVERS:
2021         case MLX5_FLOW_NAMESPACE_ANCHOR:
2022                 prio = type;
2023                 break;
2024         case MLX5_FLOW_NAMESPACE_FDB:
2025                 if (steering->fdb_root_ns)
2026                         return &steering->fdb_root_ns->ns;
2027                 else
2028                         return NULL;
2029         case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2030                 if (steering->esw_egress_root_ns)
2031                         return &steering->esw_egress_root_ns->ns;
2032                 else
2033                         return NULL;
2034         case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2035                 if (steering->esw_ingress_root_ns)
2036                         return &steering->esw_ingress_root_ns->ns;
2037                 else
2038                         return NULL;
2039         case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2040                 if (steering->sniffer_rx_root_ns)
2041                         return &steering->sniffer_rx_root_ns->ns;
2042                 else
2043                         return NULL;
2044         case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2045                 if (steering->sniffer_tx_root_ns)
2046                         return &steering->sniffer_tx_root_ns->ns;
2047                 else
2048                         return NULL;
2049         default:
2050                 return NULL;
2051         }
2052
2053         root_ns = steering->root_ns;
2054         if (!root_ns)
2055                 return NULL;
2056
2057         fs_prio = find_prio(&root_ns->ns, prio);
2058         if (!fs_prio)
2059                 return NULL;
2060
2061         ns = list_first_entry(&fs_prio->node.children,
2062                               typeof(*ns),
2063                               node.list);
2064
2065         return ns;
2066 }
2067 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2068
2069 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2070                                       unsigned int prio, int num_levels)
2071 {
2072         struct fs_prio *fs_prio;
2073
2074         fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2075         if (!fs_prio)
2076                 return ERR_PTR(-ENOMEM);
2077
2078         fs_prio->node.type = FS_TYPE_PRIO;
2079         tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2080         tree_add_node(&fs_prio->node, &ns->node);
2081         fs_prio->num_levels = num_levels;
2082         fs_prio->prio = prio;
2083         list_add_tail(&fs_prio->node.list, &ns->node.children);
2084
2085         return fs_prio;
2086 }
2087
2088 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2089                                                      *ns)
2090 {
2091         ns->node.type = FS_TYPE_NAMESPACE;
2092
2093         return ns;
2094 }
2095
2096 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2097 {
2098         struct mlx5_flow_namespace      *ns;
2099
2100         ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2101         if (!ns)
2102                 return ERR_PTR(-ENOMEM);
2103
2104         fs_init_namespace(ns);
2105         tree_init_node(&ns->node, NULL, del_sw_ns);
2106         tree_add_node(&ns->node, &prio->node);
2107         list_add_tail(&ns->node.list, &prio->node.children);
2108
2109         return ns;
2110 }
2111
2112 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2113                              struct init_tree_node *prio_metadata)
2114 {
2115         struct fs_prio *fs_prio;
2116         int i;
2117
2118         for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2119                 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2120                 if (IS_ERR(fs_prio))
2121                         return PTR_ERR(fs_prio);
2122         }
2123         return 0;
2124 }
2125
2126 #define FLOW_TABLE_BIT_SZ 1
2127 #define GET_FLOW_TABLE_CAP(dev, offset) \
2128         ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) +    \
2129                         offset / 32)) >>                                        \
2130           (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2131 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2132 {
2133         int i;
2134
2135         for (i = 0; i < caps->arr_sz; i++) {
2136                 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2137                         return false;
2138         }
2139         return true;
2140 }
2141
2142 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2143                                     struct init_tree_node *init_node,
2144                                     struct fs_node *fs_parent_node,
2145                                     struct init_tree_node *init_parent_node,
2146                                     int prio)
2147 {
2148         int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2149                                               flow_table_properties_nic_receive.
2150                                               max_ft_level);
2151         struct mlx5_flow_namespace *fs_ns;
2152         struct fs_prio *fs_prio;
2153         struct fs_node *base;
2154         int i;
2155         int err;
2156
2157         if (init_node->type == FS_TYPE_PRIO) {
2158                 if ((init_node->min_ft_level > max_ft_level) ||
2159                     !has_required_caps(steering->dev, &init_node->caps))
2160                         return 0;
2161
2162                 fs_get_obj(fs_ns, fs_parent_node);
2163                 if (init_node->num_leaf_prios)
2164                         return create_leaf_prios(fs_ns, prio, init_node);
2165                 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2166                 if (IS_ERR(fs_prio))
2167                         return PTR_ERR(fs_prio);
2168                 base = &fs_prio->node;
2169         } else if (init_node->type == FS_TYPE_NAMESPACE) {
2170                 fs_get_obj(fs_prio, fs_parent_node);
2171                 fs_ns = fs_create_namespace(fs_prio);
2172                 if (IS_ERR(fs_ns))
2173                         return PTR_ERR(fs_ns);
2174                 base = &fs_ns->node;
2175         } else {
2176                 return -EINVAL;
2177         }
2178         prio = 0;
2179         for (i = 0; i < init_node->ar_size; i++) {
2180                 err = init_root_tree_recursive(steering, &init_node->children[i],
2181                                                base, init_node, prio);
2182                 if (err)
2183                         return err;
2184                 if (init_node->children[i].type == FS_TYPE_PRIO &&
2185                     init_node->children[i].num_leaf_prios) {
2186                         prio += init_node->children[i].num_leaf_prios;
2187                 }
2188         }
2189
2190         return 0;
2191 }
2192
2193 static int init_root_tree(struct mlx5_flow_steering *steering,
2194                           struct init_tree_node *init_node,
2195                           struct fs_node *fs_parent_node)
2196 {
2197         int i;
2198         struct mlx5_flow_namespace *fs_ns;
2199         int err;
2200
2201         fs_get_obj(fs_ns, fs_parent_node);
2202         for (i = 0; i < init_node->ar_size; i++) {
2203                 err = init_root_tree_recursive(steering, &init_node->children[i],
2204                                                &fs_ns->node,
2205                                                init_node, i);
2206                 if (err)
2207                         return err;
2208         }
2209         return 0;
2210 }
2211
2212 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_flow_steering *steering,
2213                                                        enum fs_flow_table_type
2214                                                        table_type)
2215 {
2216         struct mlx5_flow_root_namespace *root_ns;
2217         struct mlx5_flow_namespace *ns;
2218
2219         /* Create the root namespace */
2220         root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
2221         if (!root_ns)
2222                 return NULL;
2223
2224         root_ns->dev = steering->dev;
2225         root_ns->table_type = table_type;
2226
2227         INIT_LIST_HEAD(&root_ns->underlay_qpns);
2228
2229         ns = &root_ns->ns;
2230         fs_init_namespace(ns);
2231         mutex_init(&root_ns->chain_lock);
2232         tree_init_node(&ns->node, NULL, NULL);
2233         tree_add_node(&ns->node, NULL);
2234
2235         return root_ns;
2236 }
2237
2238 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2239
2240 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2241 {
2242         struct fs_prio *prio;
2243
2244         fs_for_each_prio(prio, ns) {
2245                  /* This updates prio start_level and num_levels */
2246                 set_prio_attrs_in_prio(prio, acc_level);
2247                 acc_level += prio->num_levels;
2248         }
2249         return acc_level;
2250 }
2251
2252 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2253 {
2254         struct mlx5_flow_namespace *ns;
2255         int acc_level_ns = acc_level;
2256
2257         prio->start_level = acc_level;
2258         fs_for_each_ns(ns, prio)
2259                 /* This updates start_level and num_levels of ns's priority descendants */
2260                 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2261         if (!prio->num_levels)
2262                 prio->num_levels = acc_level_ns - prio->start_level;
2263         WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2264 }
2265
2266 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2267 {
2268         struct mlx5_flow_namespace *ns = &root_ns->ns;
2269         struct fs_prio *prio;
2270         int start_level = 0;
2271
2272         fs_for_each_prio(prio, ns) {
2273                 set_prio_attrs_in_prio(prio, start_level);
2274                 start_level += prio->num_levels;
2275         }
2276 }
2277
2278 #define ANCHOR_PRIO 0
2279 #define ANCHOR_SIZE 1
2280 #define ANCHOR_LEVEL 0
2281 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2282 {
2283         struct mlx5_flow_namespace *ns = NULL;
2284         struct mlx5_flow_table_attr ft_attr = {};
2285         struct mlx5_flow_table *ft;
2286
2287         ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2288         if (WARN_ON(!ns))
2289                 return -EINVAL;
2290
2291         ft_attr.max_fte = ANCHOR_SIZE;
2292         ft_attr.level   = ANCHOR_LEVEL;
2293         ft_attr.prio    = ANCHOR_PRIO;
2294
2295         ft = mlx5_create_flow_table(ns, &ft_attr);
2296         if (IS_ERR(ft)) {
2297                 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2298                 return PTR_ERR(ft);
2299         }
2300         return 0;
2301 }
2302
2303 static int init_root_ns(struct mlx5_flow_steering *steering)
2304 {
2305         steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2306         if (!steering->root_ns)
2307                 goto cleanup;
2308
2309         if (init_root_tree(steering, &root_fs, &steering->root_ns->ns.node))
2310                 goto cleanup;
2311
2312         set_prio_attrs(steering->root_ns);
2313
2314         if (create_anchor_flow_table(steering))
2315                 goto cleanup;
2316
2317         return 0;
2318
2319 cleanup:
2320         mlx5_cleanup_fs(steering->dev);
2321         return -ENOMEM;
2322 }
2323
2324 static void clean_tree(struct fs_node *node)
2325 {
2326         if (node) {
2327                 struct fs_node *iter;
2328                 struct fs_node *temp;
2329
2330                 tree_get_node(node);
2331                 list_for_each_entry_safe(iter, temp, &node->children, list)
2332                         clean_tree(iter);
2333                 tree_put_node(node);
2334                 tree_remove_node(node);
2335         }
2336 }
2337
2338 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2339 {
2340         if (!root_ns)
2341                 return;
2342
2343         clean_tree(&root_ns->ns.node);
2344 }
2345
2346 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2347 {
2348         struct mlx5_flow_steering *steering = dev->priv.steering;
2349
2350         cleanup_root_ns(steering->root_ns);
2351         cleanup_root_ns(steering->esw_egress_root_ns);
2352         cleanup_root_ns(steering->esw_ingress_root_ns);
2353         cleanup_root_ns(steering->fdb_root_ns);
2354         cleanup_root_ns(steering->sniffer_rx_root_ns);
2355         cleanup_root_ns(steering->sniffer_tx_root_ns);
2356         mlx5_cleanup_fc_stats(dev);
2357         kmem_cache_destroy(steering->ftes_cache);
2358         kmem_cache_destroy(steering->fgs_cache);
2359         kfree(steering);
2360 }
2361
2362 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2363 {
2364         struct fs_prio *prio;
2365
2366         steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2367         if (!steering->sniffer_tx_root_ns)
2368                 return -ENOMEM;
2369
2370         /* Create single prio */
2371         prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2372         if (IS_ERR(prio)) {
2373                 cleanup_root_ns(steering->sniffer_tx_root_ns);
2374                 return PTR_ERR(prio);
2375         }
2376         return 0;
2377 }
2378
2379 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2380 {
2381         struct fs_prio *prio;
2382
2383         steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2384         if (!steering->sniffer_rx_root_ns)
2385                 return -ENOMEM;
2386
2387         /* Create single prio */
2388         prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2389         if (IS_ERR(prio)) {
2390                 cleanup_root_ns(steering->sniffer_rx_root_ns);
2391                 return PTR_ERR(prio);
2392         }
2393         return 0;
2394 }
2395
2396 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2397 {
2398         struct fs_prio *prio;
2399
2400         steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2401         if (!steering->fdb_root_ns)
2402                 return -ENOMEM;
2403
2404         prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 1);
2405         if (IS_ERR(prio))
2406                 goto out_err;
2407
2408         prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2409         if (IS_ERR(prio))
2410                 goto out_err;
2411
2412         set_prio_attrs(steering->fdb_root_ns);
2413         return 0;
2414
2415 out_err:
2416         cleanup_root_ns(steering->fdb_root_ns);
2417         steering->fdb_root_ns = NULL;
2418         return PTR_ERR(prio);
2419 }
2420
2421 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering)
2422 {
2423         struct fs_prio *prio;
2424
2425         steering->esw_egress_root_ns = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2426         if (!steering->esw_egress_root_ns)
2427                 return -ENOMEM;
2428
2429         /* create 1 prio*/
2430         prio = fs_create_prio(&steering->esw_egress_root_ns->ns, 0,
2431                               MLX5_TOTAL_VPORTS(steering->dev));
2432         return PTR_ERR_OR_ZERO(prio);
2433 }
2434
2435 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering)
2436 {
2437         struct fs_prio *prio;
2438
2439         steering->esw_ingress_root_ns = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2440         if (!steering->esw_ingress_root_ns)
2441                 return -ENOMEM;
2442
2443         /* create 1 prio*/
2444         prio = fs_create_prio(&steering->esw_ingress_root_ns->ns, 0,
2445                               MLX5_TOTAL_VPORTS(steering->dev));
2446         return PTR_ERR_OR_ZERO(prio);
2447 }
2448
2449 int mlx5_init_fs(struct mlx5_core_dev *dev)
2450 {
2451         struct mlx5_flow_steering *steering;
2452         int err = 0;
2453
2454         err = mlx5_init_fc_stats(dev);
2455         if (err)
2456                 return err;
2457
2458         steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2459         if (!steering)
2460                 return -ENOMEM;
2461         steering->dev = dev;
2462         dev->priv.steering = steering;
2463
2464         steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2465                                                 sizeof(struct mlx5_flow_group), 0,
2466                                                 0, NULL);
2467         steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2468                                                  0, NULL);
2469         if (!steering->ftes_cache || !steering->fgs_cache) {
2470                 err = -ENOMEM;
2471                 goto err;
2472         }
2473
2474         if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2475               (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2476              ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2477               MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2478             MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2479                 err = init_root_ns(steering);
2480                 if (err)
2481                         goto err;
2482         }
2483
2484         if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
2485                 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2486                         err = init_fdb_root_ns(steering);
2487                         if (err)
2488                                 goto err;
2489                 }
2490                 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2491                         err = init_egress_acl_root_ns(steering);
2492                         if (err)
2493                                 goto err;
2494                 }
2495                 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2496                         err = init_ingress_acl_root_ns(steering);
2497                         if (err)
2498                                 goto err;
2499                 }
2500         }
2501
2502         if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2503                 err = init_sniffer_rx_root_ns(steering);
2504                 if (err)
2505                         goto err;
2506         }
2507
2508         if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2509                 err = init_sniffer_tx_root_ns(steering);
2510                 if (err)
2511                         goto err;
2512         }
2513
2514         return 0;
2515 err:
2516         mlx5_cleanup_fs(dev);
2517         return err;
2518 }
2519
2520 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2521 {
2522         struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2523         struct mlx5_ft_underlay_qp *new_uqp;
2524         int err = 0;
2525
2526         new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2527         if (!new_uqp)
2528                 return -ENOMEM;
2529
2530         mutex_lock(&root->chain_lock);
2531
2532         if (!root->root_ft) {
2533                 err = -EINVAL;
2534                 goto update_ft_fail;
2535         }
2536
2537         err = mlx5_cmd_update_root_ft(dev, root->root_ft, underlay_qpn, false);
2538         if (err) {
2539                 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2540                                underlay_qpn, err);
2541                 goto update_ft_fail;
2542         }
2543
2544         new_uqp->qpn = underlay_qpn;
2545         list_add_tail(&new_uqp->list, &root->underlay_qpns);
2546
2547         mutex_unlock(&root->chain_lock);
2548
2549         return 0;
2550
2551 update_ft_fail:
2552         mutex_unlock(&root->chain_lock);
2553         kfree(new_uqp);
2554         return err;
2555 }
2556 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2557
2558 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2559 {
2560         struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2561         struct mlx5_ft_underlay_qp *uqp;
2562         bool found = false;
2563         int err = 0;
2564
2565         mutex_lock(&root->chain_lock);
2566         list_for_each_entry(uqp, &root->underlay_qpns, list) {
2567                 if (uqp->qpn == underlay_qpn) {
2568                         found = true;
2569                         break;
2570                 }
2571         }
2572
2573         if (!found) {
2574                 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2575                                underlay_qpn);
2576                 err = -EINVAL;
2577                 goto out;
2578         }
2579
2580         err = mlx5_cmd_update_root_ft(dev, root->root_ft, underlay_qpn, true);
2581         if (err)
2582                 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2583                                underlay_qpn, err);
2584
2585         list_del(&uqp->list);
2586         mutex_unlock(&root->chain_lock);
2587         kfree(uqp);
2588
2589         return 0;
2590
2591 out:
2592         mutex_unlock(&root->chain_lock);
2593         return err;
2594 }
2595 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);