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