bonding: fix bond_get_stats()
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_core.c
1 /*
2  * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/mutex.h>
34 #include <linux/mlx5/driver.h>
35
36 #include "mlx5_core.h"
37 #include "fs_core.h"
38 #include "fs_cmd.h"
39
40 #define INIT_TREE_NODE_ARRAY_SIZE(...)  (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
41                                          sizeof(struct init_tree_node))
42
43 #define ADD_PRIO(num_prios_val, min_level_val, max_ft_val, caps_val,\
44                  ...) {.type = FS_TYPE_PRIO,\
45         .min_ft_level = min_level_val,\
46         .max_ft = max_ft_val,\
47         .num_leaf_prios = num_prios_val,\
48         .caps = caps_val,\
49         .children = (struct init_tree_node[]) {__VA_ARGS__},\
50         .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
51 }
52
53 #define ADD_MULTIPLE_PRIO(num_prios_val, max_ft_val, ...)\
54         ADD_PRIO(num_prios_val, 0, max_ft_val, {},\
55                  __VA_ARGS__)\
56
57 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
58         .children = (struct init_tree_node[]) {__VA_ARGS__},\
59         .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
60 }
61
62 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
63                                    sizeof(long))
64
65 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
66
67 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
68                                .caps = (long[]) {__VA_ARGS__} }
69
70 #define LEFTOVERS_MAX_FT 1
71 #define LEFTOVERS_NUM_PRIOS 1
72 #define BY_PASS_PRIO_MAX_FT 1
73 #define BY_PASS_MIN_LEVEL (KENREL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
74                            LEFTOVERS_MAX_FT)
75
76 #define KERNEL_MAX_FT 3
77 #define KERNEL_NUM_PRIOS 2
78 #define KENREL_MIN_LEVEL 2
79
80 struct node_caps {
81         size_t  arr_sz;
82         long    *caps;
83 };
84 static struct init_tree_node {
85         enum fs_node_type       type;
86         struct init_tree_node *children;
87         int ar_size;
88         struct node_caps caps;
89         int min_ft_level;
90         int num_leaf_prios;
91         int prio;
92         int max_ft;
93 } root_fs = {
94         .type = FS_TYPE_NAMESPACE,
95         .ar_size = 3,
96         .children = (struct init_tree_node[]) {
97                 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
98                          FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en),
99                                           FS_CAP(flow_table_properties_nic_receive.modify_root),
100                                           FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode),
101                                           FS_CAP(flow_table_properties_nic_receive.flow_table_modify)),
102                          ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS, BY_PASS_PRIO_MAX_FT))),
103                 ADD_PRIO(0, KENREL_MIN_LEVEL, 0, {},
104                          ADD_NS(ADD_MULTIPLE_PRIO(KERNEL_NUM_PRIOS, KERNEL_MAX_FT))),
105                 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
106                          FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en),
107                                           FS_CAP(flow_table_properties_nic_receive.modify_root),
108                                           FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode),
109                                           FS_CAP(flow_table_properties_nic_receive.flow_table_modify)),
110                          ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_MAX_FT))),
111         }
112 };
113
114 enum fs_i_mutex_lock_class {
115         FS_MUTEX_GRANDPARENT,
116         FS_MUTEX_PARENT,
117         FS_MUTEX_CHILD
118 };
119
120 static void del_rule(struct fs_node *node);
121 static void del_flow_table(struct fs_node *node);
122 static void del_flow_group(struct fs_node *node);
123 static void del_fte(struct fs_node *node);
124
125 static void tree_init_node(struct fs_node *node,
126                            unsigned int refcount,
127                            void (*remove_func)(struct fs_node *))
128 {
129         atomic_set(&node->refcount, refcount);
130         INIT_LIST_HEAD(&node->list);
131         INIT_LIST_HEAD(&node->children);
132         mutex_init(&node->lock);
133         node->remove_func = remove_func;
134 }
135
136 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
137 {
138         if (parent)
139                 atomic_inc(&parent->refcount);
140         node->parent = parent;
141
142         /* Parent is the root */
143         if (!parent)
144                 node->root = node;
145         else
146                 node->root = parent->root;
147 }
148
149 static void tree_get_node(struct fs_node *node)
150 {
151         atomic_inc(&node->refcount);
152 }
153
154 static void nested_lock_ref_node(struct fs_node *node,
155                                  enum fs_i_mutex_lock_class class)
156 {
157         if (node) {
158                 mutex_lock_nested(&node->lock, class);
159                 atomic_inc(&node->refcount);
160         }
161 }
162
163 static void lock_ref_node(struct fs_node *node)
164 {
165         if (node) {
166                 mutex_lock(&node->lock);
167                 atomic_inc(&node->refcount);
168         }
169 }
170
171 static void unlock_ref_node(struct fs_node *node)
172 {
173         if (node) {
174                 atomic_dec(&node->refcount);
175                 mutex_unlock(&node->lock);
176         }
177 }
178
179 static void tree_put_node(struct fs_node *node)
180 {
181         struct fs_node *parent_node = node->parent;
182
183         lock_ref_node(parent_node);
184         if (atomic_dec_and_test(&node->refcount)) {
185                 if (parent_node)
186                         list_del_init(&node->list);
187                 if (node->remove_func)
188                         node->remove_func(node);
189                 kfree(node);
190                 node = NULL;
191         }
192         unlock_ref_node(parent_node);
193         if (!node && parent_node)
194                 tree_put_node(parent_node);
195 }
196
197 static int tree_remove_node(struct fs_node *node)
198 {
199         if (atomic_read(&node->refcount) > 1)
200                 return -EPERM;
201         tree_put_node(node);
202         return 0;
203 }
204
205 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
206                                  unsigned int prio)
207 {
208         struct fs_prio *iter_prio;
209
210         fs_for_each_prio(iter_prio, ns) {
211                 if (iter_prio->prio == prio)
212                         return iter_prio;
213         }
214
215         return NULL;
216 }
217
218 static unsigned int find_next_free_level(struct fs_prio *prio)
219 {
220         if (!list_empty(&prio->node.children)) {
221                 struct mlx5_flow_table *ft;
222
223                 ft = list_last_entry(&prio->node.children,
224                                      struct mlx5_flow_table,
225                                      node.list);
226                 return ft->level + 1;
227         }
228         return prio->start_level;
229 }
230
231 static bool masked_memcmp(void *mask, void *val1, void *val2, size_t size)
232 {
233         unsigned int i;
234
235         for (i = 0; i < size; i++, mask++, val1++, val2++)
236                 if ((*((u8 *)val1) & (*(u8 *)mask)) !=
237                     ((*(u8 *)val2) & (*(u8 *)mask)))
238                         return false;
239
240         return true;
241 }
242
243 static bool compare_match_value(struct mlx5_flow_group_mask *mask,
244                                 void *fte_param1, void *fte_param2)
245 {
246         if (mask->match_criteria_enable &
247             1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) {
248                 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
249                                                 fte_param1, outer_headers);
250                 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
251                                                 fte_param2, outer_headers);
252                 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
253                                               mask->match_criteria, outer_headers);
254
255                 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
256                                    MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
257                         return false;
258         }
259
260         if (mask->match_criteria_enable &
261             1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) {
262                 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
263                                                 fte_param1, misc_parameters);
264                 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
265                                                 fte_param2, misc_parameters);
266                 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
267                                           mask->match_criteria, misc_parameters);
268
269                 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
270                                    MLX5_ST_SZ_BYTES(fte_match_set_misc)))
271                         return false;
272         }
273
274         if (mask->match_criteria_enable &
275             1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS) {
276                 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
277                                                 fte_param1, inner_headers);
278                 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
279                                                 fte_param2, inner_headers);
280                 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
281                                           mask->match_criteria, inner_headers);
282
283                 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
284                                    MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
285                         return false;
286         }
287         return true;
288 }
289
290 static bool compare_match_criteria(u8 match_criteria_enable1,
291                                    u8 match_criteria_enable2,
292                                    void *mask1, void *mask2)
293 {
294         return match_criteria_enable1 == match_criteria_enable2 &&
295                 !memcmp(mask1, mask2, MLX5_ST_SZ_BYTES(fte_match_param));
296 }
297
298 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
299 {
300         struct fs_node *root;
301         struct mlx5_flow_namespace *ns;
302
303         root = node->root;
304
305         if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
306                 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
307                 return NULL;
308         }
309
310         ns = container_of(root, struct mlx5_flow_namespace, node);
311         return container_of(ns, struct mlx5_flow_root_namespace, ns);
312 }
313
314 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
315 {
316         struct mlx5_flow_root_namespace *root = find_root(node);
317
318         if (root)
319                 return root->dev;
320         return NULL;
321 }
322
323 static void del_flow_table(struct fs_node *node)
324 {
325         struct mlx5_flow_table *ft;
326         struct mlx5_core_dev *dev;
327         struct fs_prio *prio;
328         int err;
329
330         fs_get_obj(ft, node);
331         dev = get_dev(&ft->node);
332
333         err = mlx5_cmd_destroy_flow_table(dev, ft);
334         if (err)
335                 pr_warn("flow steering can't destroy ft\n");
336         fs_get_obj(prio, ft->node.parent);
337         prio->num_ft--;
338 }
339
340 static void del_rule(struct fs_node *node)
341 {
342         struct mlx5_flow_rule *rule;
343         struct mlx5_flow_table *ft;
344         struct mlx5_flow_group *fg;
345         struct fs_fte *fte;
346         u32     *match_value;
347         struct mlx5_core_dev *dev = get_dev(node);
348         int match_len = MLX5_ST_SZ_BYTES(fte_match_param);
349         int err;
350
351         match_value = mlx5_vzalloc(match_len);
352         if (!match_value) {
353                 pr_warn("failed to allocate inbox\n");
354                 return;
355         }
356
357         fs_get_obj(rule, node);
358         fs_get_obj(fte, rule->node.parent);
359         fs_get_obj(fg, fte->node.parent);
360         memcpy(match_value, fte->val, sizeof(fte->val));
361         fs_get_obj(ft, fg->node.parent);
362         list_del(&rule->node.list);
363         if ((fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
364             --fte->dests_size) {
365                 err = mlx5_cmd_update_fte(dev, ft,
366                                           fg->id, fte);
367                 if (err)
368                         pr_warn("%s can't del rule fg id=%d fte_index=%d\n",
369                                 __func__, fg->id, fte->index);
370         }
371         kvfree(match_value);
372 }
373
374 static void del_fte(struct fs_node *node)
375 {
376         struct mlx5_flow_table *ft;
377         struct mlx5_flow_group *fg;
378         struct mlx5_core_dev *dev;
379         struct fs_fte *fte;
380         int err;
381
382         fs_get_obj(fte, node);
383         fs_get_obj(fg, fte->node.parent);
384         fs_get_obj(ft, fg->node.parent);
385
386         dev = get_dev(&ft->node);
387         err = mlx5_cmd_delete_fte(dev, ft,
388                                   fte->index);
389         if (err)
390                 pr_warn("flow steering can't delete fte in index %d of flow group id %d\n",
391                         fte->index, fg->id);
392
393         fte->status = 0;
394         fg->num_ftes--;
395 }
396
397 static void del_flow_group(struct fs_node *node)
398 {
399         struct mlx5_flow_group *fg;
400         struct mlx5_flow_table *ft;
401         struct mlx5_core_dev *dev;
402
403         fs_get_obj(fg, node);
404         fs_get_obj(ft, fg->node.parent);
405         dev = get_dev(&ft->node);
406
407         if (mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
408                 pr_warn("flow steering can't destroy fg %d of ft %d\n",
409                         fg->id, ft->id);
410 }
411
412 static struct fs_fte *alloc_fte(u8 action,
413                                 u32 flow_tag,
414                                 u32 *match_value,
415                                 unsigned int index)
416 {
417         struct fs_fte *fte;
418
419         fte = kzalloc(sizeof(*fte), GFP_KERNEL);
420         if (!fte)
421                 return ERR_PTR(-ENOMEM);
422
423         memcpy(fte->val, match_value, sizeof(fte->val));
424         fte->node.type =  FS_TYPE_FLOW_ENTRY;
425         fte->flow_tag = flow_tag;
426         fte->index = index;
427         fte->action = action;
428
429         return fte;
430 }
431
432 static struct mlx5_flow_group *alloc_flow_group(u32 *create_fg_in)
433 {
434         struct mlx5_flow_group *fg;
435         void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
436                                             create_fg_in, match_criteria);
437         u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
438                                             create_fg_in,
439                                             match_criteria_enable);
440         fg = kzalloc(sizeof(*fg), GFP_KERNEL);
441         if (!fg)
442                 return ERR_PTR(-ENOMEM);
443
444         fg->mask.match_criteria_enable = match_criteria_enable;
445         memcpy(&fg->mask.match_criteria, match_criteria,
446                sizeof(fg->mask.match_criteria));
447         fg->node.type =  FS_TYPE_FLOW_GROUP;
448         fg->start_index = MLX5_GET(create_flow_group_in, create_fg_in,
449                                    start_flow_index);
450         fg->max_ftes = MLX5_GET(create_flow_group_in, create_fg_in,
451                                 end_flow_index) - fg->start_index + 1;
452         return fg;
453 }
454
455 static struct mlx5_flow_table *alloc_flow_table(int level, int max_fte,
456                                                 enum fs_flow_table_type table_type)
457 {
458         struct mlx5_flow_table *ft;
459
460         ft  = kzalloc(sizeof(*ft), GFP_KERNEL);
461         if (!ft)
462                 return NULL;
463
464         ft->level = level;
465         ft->node.type = FS_TYPE_FLOW_TABLE;
466         ft->type = table_type;
467         ft->max_fte = max_fte;
468
469         return ft;
470 }
471
472 /* If reverse is false, then we search for the first flow table in the
473  * root sub-tree from start(closest from right), else we search for the
474  * last flow table in the root sub-tree till start(closest from left).
475  */
476 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node  *root,
477                                                          struct list_head *start,
478                                                          bool reverse)
479 {
480 #define list_advance_entry(pos, reverse)                \
481         ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
482
483 #define list_for_each_advance_continue(pos, head, reverse)      \
484         for (pos = list_advance_entry(pos, reverse);            \
485              &pos->list != (head);                              \
486              pos = list_advance_entry(pos, reverse))
487
488         struct fs_node *iter = list_entry(start, struct fs_node, list);
489         struct mlx5_flow_table *ft = NULL;
490
491         if (!root)
492                 return NULL;
493
494         list_for_each_advance_continue(iter, &root->children, reverse) {
495                 if (iter->type == FS_TYPE_FLOW_TABLE) {
496                         fs_get_obj(ft, iter);
497                         return ft;
498                 }
499                 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
500                 if (ft)
501                         return ft;
502         }
503
504         return ft;
505 }
506
507 /* If reverse if false then return the first flow table in next priority of
508  * prio in the tree, else return the last flow table in the previous priority
509  * of prio in the tree.
510  */
511 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
512 {
513         struct mlx5_flow_table *ft = NULL;
514         struct fs_node *curr_node;
515         struct fs_node *parent;
516
517         parent = prio->node.parent;
518         curr_node = &prio->node;
519         while (!ft && parent) {
520                 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
521                 curr_node = parent;
522                 parent = curr_node->parent;
523         }
524         return ft;
525 }
526
527 /* Assuming all the tree is locked by mutex chain lock */
528 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
529 {
530         return find_closest_ft(prio, false);
531 }
532
533 /* Assuming all the tree is locked by mutex chain lock */
534 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
535 {
536         return find_closest_ft(prio, true);
537 }
538
539 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
540                                struct fs_prio *prio,
541                                struct mlx5_flow_table *ft)
542 {
543         struct mlx5_flow_table *iter;
544         int i = 0;
545         int err;
546
547         fs_for_each_ft(iter, prio) {
548                 i++;
549                 err = mlx5_cmd_modify_flow_table(dev,
550                                                  iter,
551                                                  ft);
552                 if (err) {
553                         mlx5_core_warn(dev, "Failed to modify flow table %d\n",
554                                        iter->id);
555                         /* The driver is out of sync with the FW */
556                         if (i > 1)
557                                 WARN_ON(true);
558                         return err;
559                 }
560         }
561         return 0;
562 }
563
564 /* Connect flow tables from previous priority of prio to ft */
565 static int connect_prev_fts(struct mlx5_core_dev *dev,
566                             struct mlx5_flow_table *ft,
567                             struct fs_prio *prio)
568 {
569         struct mlx5_flow_table *prev_ft;
570
571         prev_ft = find_prev_chained_ft(prio);
572         if (prev_ft) {
573                 struct fs_prio *prev_prio;
574
575                 fs_get_obj(prev_prio, prev_ft->node.parent);
576                 return connect_fts_in_prio(dev, prev_prio, ft);
577         }
578         return 0;
579 }
580
581 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
582                                  *prio)
583 {
584         struct mlx5_flow_root_namespace *root = find_root(&prio->node);
585         int min_level = INT_MAX;
586         int err;
587
588         if (root->root_ft)
589                 min_level = root->root_ft->level;
590
591         if (ft->level >= min_level)
592                 return 0;
593
594         err = mlx5_cmd_update_root_ft(root->dev, ft);
595         if (err)
596                 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
597                                ft->id);
598         else
599                 root->root_ft = ft;
600
601         return err;
602 }
603
604 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
605                               struct fs_prio *prio)
606 {
607         int err = 0;
608
609         /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
610
611         if (list_empty(&prio->node.children)) {
612                 err = connect_prev_fts(dev, ft, prio);
613                 if (err)
614                         return err;
615         }
616
617         if (MLX5_CAP_FLOWTABLE(dev,
618                                flow_table_properties_nic_receive.modify_root))
619                 err = update_root_ft_create(ft, prio);
620         return err;
621 }
622
623 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
624                                                int prio,
625                                                int max_fte)
626 {
627         struct mlx5_flow_table *next_ft = NULL;
628         struct mlx5_flow_table *ft;
629         int err;
630         int log_table_sz;
631         struct mlx5_flow_root_namespace *root =
632                 find_root(&ns->node);
633         struct fs_prio *fs_prio = NULL;
634
635         if (!root) {
636                 pr_err("mlx5: flow steering failed to find root of namespace\n");
637                 return ERR_PTR(-ENODEV);
638         }
639
640         mutex_lock(&root->chain_lock);
641         fs_prio = find_prio(ns, prio);
642         if (!fs_prio) {
643                 err = -EINVAL;
644                 goto unlock_root;
645         }
646         if (fs_prio->num_ft == fs_prio->max_ft) {
647                 err = -ENOSPC;
648                 goto unlock_root;
649         }
650
651         ft = alloc_flow_table(find_next_free_level(fs_prio),
652                               roundup_pow_of_two(max_fte),
653                               root->table_type);
654         if (!ft) {
655                 err = -ENOMEM;
656                 goto unlock_root;
657         }
658
659         tree_init_node(&ft->node, 1, del_flow_table);
660         log_table_sz = ilog2(ft->max_fte);
661         next_ft = find_next_chained_ft(fs_prio);
662         err = mlx5_cmd_create_flow_table(root->dev, ft->type, ft->level,
663                                          log_table_sz, next_ft, &ft->id);
664         if (err)
665                 goto free_ft;
666
667         err = connect_flow_table(root->dev, ft, fs_prio);
668         if (err)
669                 goto destroy_ft;
670         lock_ref_node(&fs_prio->node);
671         tree_add_node(&ft->node, &fs_prio->node);
672         list_add_tail(&ft->node.list, &fs_prio->node.children);
673         fs_prio->num_ft++;
674         unlock_ref_node(&fs_prio->node);
675         mutex_unlock(&root->chain_lock);
676         return ft;
677 destroy_ft:
678         mlx5_cmd_destroy_flow_table(root->dev, ft);
679 free_ft:
680         kfree(ft);
681 unlock_root:
682         mutex_unlock(&root->chain_lock);
683         return ERR_PTR(err);
684 }
685
686 struct mlx5_flow_table *mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
687                                                             int prio,
688                                                             int num_flow_table_entries,
689                                                             int max_num_groups)
690 {
691         struct mlx5_flow_table *ft;
692
693         if (max_num_groups > num_flow_table_entries)
694                 return ERR_PTR(-EINVAL);
695
696         ft = mlx5_create_flow_table(ns, prio, num_flow_table_entries);
697         if (IS_ERR(ft))
698                 return ft;
699
700         ft->autogroup.active = true;
701         ft->autogroup.required_groups = max_num_groups;
702
703         return ft;
704 }
705 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
706
707 /* Flow table should be locked */
708 static struct mlx5_flow_group *create_flow_group_common(struct mlx5_flow_table *ft,
709                                                         u32 *fg_in,
710                                                         struct list_head
711                                                         *prev_fg,
712                                                         bool is_auto_fg)
713 {
714         struct mlx5_flow_group *fg;
715         struct mlx5_core_dev *dev = get_dev(&ft->node);
716         int err;
717
718         if (!dev)
719                 return ERR_PTR(-ENODEV);
720
721         fg = alloc_flow_group(fg_in);
722         if (IS_ERR(fg))
723                 return fg;
724
725         err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
726         if (err) {
727                 kfree(fg);
728                 return ERR_PTR(err);
729         }
730
731         if (ft->autogroup.active)
732                 ft->autogroup.num_groups++;
733         /* Add node to tree */
734         tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
735         tree_add_node(&fg->node, &ft->node);
736         /* Add node to group list */
737         list_add(&fg->node.list, ft->node.children.prev);
738
739         return fg;
740 }
741
742 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
743                                                u32 *fg_in)
744 {
745         struct mlx5_flow_group *fg;
746
747         if (ft->autogroup.active)
748                 return ERR_PTR(-EPERM);
749
750         lock_ref_node(&ft->node);
751         fg = create_flow_group_common(ft, fg_in, &ft->node.children, false);
752         unlock_ref_node(&ft->node);
753
754         return fg;
755 }
756
757 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
758 {
759         struct mlx5_flow_rule *rule;
760
761         rule = kzalloc(sizeof(*rule), GFP_KERNEL);
762         if (!rule)
763                 return NULL;
764
765         rule->node.type = FS_TYPE_FLOW_DEST;
766         if (dest)
767                 memcpy(&rule->dest_attr, dest, sizeof(*dest));
768
769         return rule;
770 }
771
772 /* fte should not be deleted while calling this function */
773 static struct mlx5_flow_rule *add_rule_fte(struct fs_fte *fte,
774                                            struct mlx5_flow_group *fg,
775                                            struct mlx5_flow_destination *dest)
776 {
777         struct mlx5_flow_table *ft;
778         struct mlx5_flow_rule *rule;
779         int err;
780
781         rule = alloc_rule(dest);
782         if (!rule)
783                 return ERR_PTR(-ENOMEM);
784
785         fs_get_obj(ft, fg->node.parent);
786         /* Add dest to dests list- added as first element after the head */
787         tree_init_node(&rule->node, 1, del_rule);
788         list_add_tail(&rule->node.list, &fte->node.children);
789         if (dest)
790                 fte->dests_size++;
791         if (fte->dests_size == 1 || !dest)
792                 err = mlx5_cmd_create_fte(get_dev(&ft->node),
793                                           ft, fg->id, fte);
794         else
795                 err = mlx5_cmd_update_fte(get_dev(&ft->node),
796                                           ft, fg->id, fte);
797         if (err)
798                 goto free_rule;
799
800         fte->status |= FS_FTE_STATUS_EXISTING;
801
802         return rule;
803
804 free_rule:
805         list_del(&rule->node.list);
806         kfree(rule);
807         if (dest)
808                 fte->dests_size--;
809         return ERR_PTR(err);
810 }
811
812 /* Assumed fg is locked */
813 static unsigned int get_free_fte_index(struct mlx5_flow_group *fg,
814                                        struct list_head **prev)
815 {
816         struct fs_fte *fte;
817         unsigned int start = fg->start_index;
818
819         if (prev)
820                 *prev = &fg->node.children;
821
822         /* assumed list is sorted by index */
823         fs_for_each_fte(fte, fg) {
824                 if (fte->index != start)
825                         return start;
826                 start++;
827                 if (prev)
828                         *prev = &fte->node.list;
829         }
830
831         return start;
832 }
833
834 /* prev is output, prev->next = new_fte */
835 static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
836                                  u32 *match_value,
837                                  u8 action,
838                                  u32 flow_tag,
839                                  struct list_head **prev)
840 {
841         struct fs_fte *fte;
842         int index;
843
844         index = get_free_fte_index(fg, prev);
845         fte = alloc_fte(action, flow_tag, match_value, index);
846         if (IS_ERR(fte))
847                 return fte;
848
849         return fte;
850 }
851
852 static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
853                                                 u8 match_criteria_enable,
854                                                 u32 *match_criteria)
855 {
856         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
857         struct list_head *prev = &ft->node.children;
858         unsigned int candidate_index = 0;
859         struct mlx5_flow_group *fg;
860         void *match_criteria_addr;
861         unsigned int group_size = 0;
862         u32 *in;
863
864         if (!ft->autogroup.active)
865                 return ERR_PTR(-ENOENT);
866
867         in = mlx5_vzalloc(inlen);
868         if (!in)
869                 return ERR_PTR(-ENOMEM);
870
871         if (ft->autogroup.num_groups < ft->autogroup.required_groups)
872                 /* We save place for flow groups in addition to max types */
873                 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
874
875         /*  ft->max_fte == ft->autogroup.max_types */
876         if (group_size == 0)
877                 group_size = 1;
878
879         /* sorted by start_index */
880         fs_for_each_fg(fg, ft) {
881                 if (candidate_index + group_size > fg->start_index)
882                         candidate_index = fg->start_index + fg->max_ftes;
883                 else
884                         break;
885                 prev = &fg->node.list;
886         }
887
888         if (candidate_index + group_size > ft->max_fte) {
889                 fg = ERR_PTR(-ENOSPC);
890                 goto out;
891         }
892
893         MLX5_SET(create_flow_group_in, in, match_criteria_enable,
894                  match_criteria_enable);
895         MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
896         MLX5_SET(create_flow_group_in, in, end_flow_index,   candidate_index +
897                  group_size - 1);
898         match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
899                                            in, match_criteria);
900         memcpy(match_criteria_addr, match_criteria,
901                MLX5_ST_SZ_BYTES(fte_match_param));
902
903         fg = create_flow_group_common(ft, in, prev, true);
904 out:
905         kvfree(in);
906         return fg;
907 }
908
909 static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
910                                           u32 *match_value,
911                                           u8 action,
912                                           u32 flow_tag,
913                                           struct mlx5_flow_destination *dest)
914 {
915         struct fs_fte *fte;
916         struct mlx5_flow_rule *rule;
917         struct mlx5_flow_table *ft;
918         struct list_head *prev;
919
920         nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
921         fs_for_each_fte(fte, fg) {
922                 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
923                 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
924                     action == fte->action && flow_tag == fte->flow_tag) {
925                         rule = add_rule_fte(fte, fg, dest);
926                         unlock_ref_node(&fte->node);
927                         if (IS_ERR(rule))
928                                 goto unlock_fg;
929                         else
930                                 goto add_rule;
931                 }
932                 unlock_ref_node(&fte->node);
933         }
934         fs_get_obj(ft, fg->node.parent);
935         if (fg->num_ftes >= fg->max_ftes) {
936                 rule = ERR_PTR(-ENOSPC);
937                 goto unlock_fg;
938         }
939
940         fte = create_fte(fg, match_value, action, flow_tag, &prev);
941         if (IS_ERR(fte)) {
942                 rule = (void *)fte;
943                 goto unlock_fg;
944         }
945         tree_init_node(&fte->node, 0, del_fte);
946         rule = add_rule_fte(fte, fg, dest);
947         if (IS_ERR(rule)) {
948                 kfree(fte);
949                 goto unlock_fg;
950         }
951
952         fg->num_ftes++;
953
954         tree_add_node(&fte->node, &fg->node);
955         list_add(&fte->node.list, prev);
956 add_rule:
957         tree_add_node(&rule->node, &fte->node);
958 unlock_fg:
959         unlock_ref_node(&fg->node);
960         return rule;
961 }
962
963 static struct mlx5_flow_rule *add_rule_to_auto_fg(struct mlx5_flow_table *ft,
964                                                   u8 match_criteria_enable,
965                                                   u32 *match_criteria,
966                                                   u32 *match_value,
967                                                   u8 action,
968                                                   u32 flow_tag,
969                                                   struct mlx5_flow_destination *dest)
970 {
971         struct mlx5_flow_rule *rule;
972         struct mlx5_flow_group *g;
973
974         g = create_autogroup(ft, match_criteria_enable, match_criteria);
975         if (IS_ERR(g))
976                 return (void *)g;
977
978         rule = add_rule_fg(g, match_value,
979                            action, flow_tag, dest);
980         if (IS_ERR(rule)) {
981                 /* Remove assumes refcount > 0 and autogroup creates a group
982                  * with a refcount = 0.
983                  */
984                 tree_get_node(&g->node);
985                 tree_remove_node(&g->node);
986         }
987         return rule;
988 }
989
990 struct mlx5_flow_rule *
991 mlx5_add_flow_rule(struct mlx5_flow_table *ft,
992                    u8 match_criteria_enable,
993                    u32 *match_criteria,
994                    u32 *match_value,
995                    u32 action,
996                    u32 flow_tag,
997                    struct mlx5_flow_destination *dest)
998 {
999         struct mlx5_flow_group *g;
1000         struct mlx5_flow_rule *rule;
1001
1002         if ((action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) && !dest)
1003                 return ERR_PTR(-EINVAL);
1004
1005         nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
1006         fs_for_each_fg(g, ft)
1007                 if (compare_match_criteria(g->mask.match_criteria_enable,
1008                                            match_criteria_enable,
1009                                            g->mask.match_criteria,
1010                                            match_criteria)) {
1011                         rule = add_rule_fg(g, match_value,
1012                                            action, flow_tag, dest);
1013                         if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
1014                                 goto unlock;
1015                 }
1016
1017         rule = add_rule_to_auto_fg(ft, match_criteria_enable, match_criteria,
1018                                    match_value, action, flow_tag, dest);
1019 unlock:
1020         unlock_ref_node(&ft->node);
1021         return rule;
1022 }
1023 EXPORT_SYMBOL(mlx5_add_flow_rule);
1024
1025 void mlx5_del_flow_rule(struct mlx5_flow_rule *rule)
1026 {
1027         tree_remove_node(&rule->node);
1028 }
1029 EXPORT_SYMBOL(mlx5_del_flow_rule);
1030
1031 /* Assuming prio->node.children(flow tables) is sorted by level */
1032 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1033 {
1034         struct fs_prio *prio;
1035
1036         fs_get_obj(prio, ft->node.parent);
1037
1038         if (!list_is_last(&ft->node.list, &prio->node.children))
1039                 return list_next_entry(ft, node.list);
1040         return find_next_chained_ft(prio);
1041 }
1042
1043 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1044 {
1045         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1046         struct mlx5_flow_table *new_root_ft = NULL;
1047
1048         if (root->root_ft != ft)
1049                 return 0;
1050
1051         new_root_ft = find_next_ft(ft);
1052         if (new_root_ft) {
1053                 int err = mlx5_cmd_update_root_ft(root->dev, new_root_ft);
1054
1055                 if (err) {
1056                         mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
1057                                        ft->id);
1058                         return err;
1059                 }
1060                 root->root_ft = new_root_ft;
1061         }
1062         return 0;
1063 }
1064
1065 /* Connect flow table from previous priority to
1066  * the next flow table.
1067  */
1068 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1069 {
1070         struct mlx5_core_dev *dev = get_dev(&ft->node);
1071         struct mlx5_flow_table *next_ft;
1072         struct fs_prio *prio;
1073         int err = 0;
1074
1075         err = update_root_ft_destroy(ft);
1076         if (err)
1077                 return err;
1078
1079         fs_get_obj(prio, ft->node.parent);
1080         if  (!(list_first_entry(&prio->node.children,
1081                                 struct mlx5_flow_table,
1082                                 node.list) == ft))
1083                 return 0;
1084
1085         next_ft = find_next_chained_ft(prio);
1086         err = connect_prev_fts(dev, next_ft, prio);
1087         if (err)
1088                 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1089                                ft->id);
1090         return err;
1091 }
1092
1093 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1094 {
1095         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1096         int err = 0;
1097
1098         mutex_lock(&root->chain_lock);
1099         err = disconnect_flow_table(ft);
1100         if (err) {
1101                 mutex_unlock(&root->chain_lock);
1102                 return err;
1103         }
1104         if (tree_remove_node(&ft->node))
1105                 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1106                                ft->id);
1107         mutex_unlock(&root->chain_lock);
1108
1109         return err;
1110 }
1111 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1112
1113 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1114 {
1115         if (tree_remove_node(&fg->node))
1116                 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1117                                fg->id);
1118 }
1119
1120 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1121                                                     enum mlx5_flow_namespace_type type)
1122 {
1123         struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1124         int prio;
1125         static struct fs_prio *fs_prio;
1126         struct mlx5_flow_namespace *ns;
1127
1128         if (!root_ns)
1129                 return NULL;
1130
1131         switch (type) {
1132         case MLX5_FLOW_NAMESPACE_BYPASS:
1133         case MLX5_FLOW_NAMESPACE_KERNEL:
1134         case MLX5_FLOW_NAMESPACE_LEFTOVERS:
1135                 prio = type;
1136                 break;
1137         case MLX5_FLOW_NAMESPACE_FDB:
1138                 if (dev->priv.fdb_root_ns)
1139                         return &dev->priv.fdb_root_ns->ns;
1140                 else
1141                         return NULL;
1142         default:
1143                 return NULL;
1144         }
1145
1146         fs_prio = find_prio(&root_ns->ns, prio);
1147         if (!fs_prio)
1148                 return NULL;
1149
1150         ns = list_first_entry(&fs_prio->node.children,
1151                               typeof(*ns),
1152                               node.list);
1153
1154         return ns;
1155 }
1156 EXPORT_SYMBOL(mlx5_get_flow_namespace);
1157
1158 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
1159                                       unsigned prio, int max_ft)
1160 {
1161         struct fs_prio *fs_prio;
1162
1163         fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
1164         if (!fs_prio)
1165                 return ERR_PTR(-ENOMEM);
1166
1167         fs_prio->node.type = FS_TYPE_PRIO;
1168         tree_init_node(&fs_prio->node, 1, NULL);
1169         tree_add_node(&fs_prio->node, &ns->node);
1170         fs_prio->max_ft = max_ft;
1171         fs_prio->prio = prio;
1172         list_add_tail(&fs_prio->node.list, &ns->node.children);
1173
1174         return fs_prio;
1175 }
1176
1177 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
1178                                                      *ns)
1179 {
1180         ns->node.type = FS_TYPE_NAMESPACE;
1181
1182         return ns;
1183 }
1184
1185 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
1186 {
1187         struct mlx5_flow_namespace      *ns;
1188
1189         ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1190         if (!ns)
1191                 return ERR_PTR(-ENOMEM);
1192
1193         fs_init_namespace(ns);
1194         tree_init_node(&ns->node, 1, NULL);
1195         tree_add_node(&ns->node, &prio->node);
1196         list_add_tail(&ns->node.list, &prio->node.children);
1197
1198         return ns;
1199 }
1200
1201 static int create_leaf_prios(struct mlx5_flow_namespace *ns, struct init_tree_node
1202                              *prio_metadata)
1203 {
1204         struct fs_prio *fs_prio;
1205         int i;
1206
1207         for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
1208                 fs_prio = fs_create_prio(ns, i, prio_metadata->max_ft);
1209                 if (IS_ERR(fs_prio))
1210                         return PTR_ERR(fs_prio);
1211         }
1212         return 0;
1213 }
1214
1215 #define FLOW_TABLE_BIT_SZ 1
1216 #define GET_FLOW_TABLE_CAP(dev, offset) \
1217         ((be32_to_cpu(*((__be32 *)(dev->hca_caps_cur[MLX5_CAP_FLOW_TABLE]) +    \
1218                         offset / 32)) >>                                        \
1219           (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
1220 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
1221 {
1222         int i;
1223
1224         for (i = 0; i < caps->arr_sz; i++) {
1225                 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
1226                         return false;
1227         }
1228         return true;
1229 }
1230
1231 static int init_root_tree_recursive(struct mlx5_core_dev *dev,
1232                                     struct init_tree_node *init_node,
1233                                     struct fs_node *fs_parent_node,
1234                                     struct init_tree_node *init_parent_node,
1235                                     int index)
1236 {
1237         int max_ft_level = MLX5_CAP_FLOWTABLE(dev,
1238                                               flow_table_properties_nic_receive.
1239                                               max_ft_level);
1240         struct mlx5_flow_namespace *fs_ns;
1241         struct fs_prio *fs_prio;
1242         struct fs_node *base;
1243         int i;
1244         int err;
1245
1246         if (init_node->type == FS_TYPE_PRIO) {
1247                 if ((init_node->min_ft_level > max_ft_level) ||
1248                     !has_required_caps(dev, &init_node->caps))
1249                         return 0;
1250
1251                 fs_get_obj(fs_ns, fs_parent_node);
1252                 if (init_node->num_leaf_prios)
1253                         return create_leaf_prios(fs_ns, init_node);
1254                 fs_prio = fs_create_prio(fs_ns, index, init_node->max_ft);
1255                 if (IS_ERR(fs_prio))
1256                         return PTR_ERR(fs_prio);
1257                 base = &fs_prio->node;
1258         } else if (init_node->type == FS_TYPE_NAMESPACE) {
1259                 fs_get_obj(fs_prio, fs_parent_node);
1260                 fs_ns = fs_create_namespace(fs_prio);
1261                 if (IS_ERR(fs_ns))
1262                         return PTR_ERR(fs_ns);
1263                 base = &fs_ns->node;
1264         } else {
1265                 return -EINVAL;
1266         }
1267         for (i = 0; i < init_node->ar_size; i++) {
1268                 err = init_root_tree_recursive(dev, &init_node->children[i],
1269                                                base, init_node, i);
1270                 if (err)
1271                         return err;
1272         }
1273
1274         return 0;
1275 }
1276
1277 static int init_root_tree(struct mlx5_core_dev *dev,
1278                           struct init_tree_node *init_node,
1279                           struct fs_node *fs_parent_node)
1280 {
1281         int i;
1282         struct mlx5_flow_namespace *fs_ns;
1283         int err;
1284
1285         fs_get_obj(fs_ns, fs_parent_node);
1286         for (i = 0; i < init_node->ar_size; i++) {
1287                 err = init_root_tree_recursive(dev, &init_node->children[i],
1288                                                &fs_ns->node,
1289                                                init_node, i);
1290                 if (err)
1291                         return err;
1292         }
1293         return 0;
1294 }
1295
1296 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_core_dev *dev,
1297                                                        enum fs_flow_table_type
1298                                                        table_type)
1299 {
1300         struct mlx5_flow_root_namespace *root_ns;
1301         struct mlx5_flow_namespace *ns;
1302
1303         /* Create the root namespace */
1304         root_ns = mlx5_vzalloc(sizeof(*root_ns));
1305         if (!root_ns)
1306                 return NULL;
1307
1308         root_ns->dev = dev;
1309         root_ns->table_type = table_type;
1310
1311         ns = &root_ns->ns;
1312         fs_init_namespace(ns);
1313         mutex_init(&root_ns->chain_lock);
1314         tree_init_node(&ns->node, 1, NULL);
1315         tree_add_node(&ns->node, NULL);
1316
1317         return root_ns;
1318 }
1319
1320 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
1321
1322 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
1323 {
1324         struct fs_prio *prio;
1325
1326         fs_for_each_prio(prio, ns) {
1327                  /* This updates prio start_level and max_ft */
1328                 set_prio_attrs_in_prio(prio, acc_level);
1329                 acc_level += prio->max_ft;
1330         }
1331         return acc_level;
1332 }
1333
1334 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
1335 {
1336         struct mlx5_flow_namespace *ns;
1337         int acc_level_ns = acc_level;
1338
1339         prio->start_level = acc_level;
1340         fs_for_each_ns(ns, prio)
1341                 /* This updates start_level and max_ft of ns's priority descendants */
1342                 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
1343         if (!prio->max_ft)
1344                 prio->max_ft = acc_level_ns - prio->start_level;
1345         WARN_ON(prio->max_ft < acc_level_ns - prio->start_level);
1346 }
1347
1348 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
1349 {
1350         struct mlx5_flow_namespace *ns = &root_ns->ns;
1351         struct fs_prio *prio;
1352         int start_level = 0;
1353
1354         fs_for_each_prio(prio, ns) {
1355                 set_prio_attrs_in_prio(prio, start_level);
1356                 start_level += prio->max_ft;
1357         }
1358 }
1359
1360 static int init_root_ns(struct mlx5_core_dev *dev)
1361 {
1362
1363         dev->priv.root_ns = create_root_ns(dev, FS_FT_NIC_RX);
1364         if (IS_ERR_OR_NULL(dev->priv.root_ns))
1365                 goto cleanup;
1366
1367         if (init_root_tree(dev, &root_fs, &dev->priv.root_ns->ns.node))
1368                 goto cleanup;
1369
1370         set_prio_attrs(dev->priv.root_ns);
1371
1372         return 0;
1373
1374 cleanup:
1375         mlx5_cleanup_fs(dev);
1376         return -ENOMEM;
1377 }
1378
1379 static void cleanup_single_prio_root_ns(struct mlx5_core_dev *dev,
1380                                         struct mlx5_flow_root_namespace *root_ns)
1381 {
1382         struct fs_node *prio;
1383
1384         if (!root_ns)
1385                 return;
1386
1387         if (!list_empty(&root_ns->ns.node.children)) {
1388                 prio = list_first_entry(&root_ns->ns.node.children,
1389                                         struct fs_node,
1390                                  list);
1391                 if (tree_remove_node(prio))
1392                         mlx5_core_warn(dev,
1393                                        "Flow steering priority wasn't destroyed, refcount > 1\n");
1394         }
1395         if (tree_remove_node(&root_ns->ns.node))
1396                 mlx5_core_warn(dev,
1397                                "Flow steering namespace wasn't destroyed, refcount > 1\n");
1398         root_ns = NULL;
1399 }
1400
1401 static void cleanup_root_ns(struct mlx5_core_dev *dev)
1402 {
1403         struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1404         struct fs_prio *iter_prio;
1405
1406         if (!MLX5_CAP_GEN(dev, nic_flow_table))
1407                 return;
1408
1409         if (!root_ns)
1410                 return;
1411
1412         /* stage 1 */
1413         fs_for_each_prio(iter_prio, &root_ns->ns) {
1414                 struct fs_node *node;
1415                 struct mlx5_flow_namespace *iter_ns;
1416
1417                 fs_for_each_ns_or_ft(node, iter_prio) {
1418                         if (node->type == FS_TYPE_FLOW_TABLE)
1419                                 continue;
1420                         fs_get_obj(iter_ns, node);
1421                         while (!list_empty(&iter_ns->node.children)) {
1422                                 struct fs_prio *obj_iter_prio2;
1423                                 struct fs_node *iter_prio2 =
1424                                         list_first_entry(&iter_ns->node.children,
1425                                                          struct fs_node,
1426                                                          list);
1427
1428                                 fs_get_obj(obj_iter_prio2, iter_prio2);
1429                                 if (tree_remove_node(iter_prio2)) {
1430                                         mlx5_core_warn(dev,
1431                                                        "Priority %d wasn't destroyed, refcount > 1\n",
1432                                                        obj_iter_prio2->prio);
1433                                         return;
1434                                 }
1435                         }
1436                 }
1437         }
1438
1439         /* stage 2 */
1440         fs_for_each_prio(iter_prio, &root_ns->ns) {
1441                 while (!list_empty(&iter_prio->node.children)) {
1442                         struct fs_node *iter_ns =
1443                                 list_first_entry(&iter_prio->node.children,
1444                                                  struct fs_node,
1445                                                  list);
1446                         if (tree_remove_node(iter_ns)) {
1447                                 mlx5_core_warn(dev,
1448                                                "Namespace wasn't destroyed, refcount > 1\n");
1449                                 return;
1450                         }
1451                 }
1452         }
1453
1454         /* stage 3 */
1455         while (!list_empty(&root_ns->ns.node.children)) {
1456                 struct fs_prio *obj_prio_node;
1457                 struct fs_node *prio_node =
1458                         list_first_entry(&root_ns->ns.node.children,
1459                                          struct fs_node,
1460                                          list);
1461
1462                 fs_get_obj(obj_prio_node, prio_node);
1463                 if (tree_remove_node(prio_node)) {
1464                         mlx5_core_warn(dev,
1465                                        "Priority %d wasn't destroyed, refcount > 1\n",
1466                                        obj_prio_node->prio);
1467                         return;
1468                 }
1469         }
1470
1471         if (tree_remove_node(&root_ns->ns.node)) {
1472                 mlx5_core_warn(dev,
1473                                "root namespace wasn't destroyed, refcount > 1\n");
1474                 return;
1475         }
1476
1477         dev->priv.root_ns = NULL;
1478 }
1479
1480 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1481 {
1482         cleanup_root_ns(dev);
1483         cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1484 }
1485
1486 static int init_fdb_root_ns(struct mlx5_core_dev *dev)
1487 {
1488         struct fs_prio *prio;
1489
1490         dev->priv.fdb_root_ns = create_root_ns(dev, FS_FT_FDB);
1491         if (!dev->priv.fdb_root_ns)
1492                 return -ENOMEM;
1493
1494         /* Create single prio */
1495         prio = fs_create_prio(&dev->priv.fdb_root_ns->ns, 0, 1);
1496         if (IS_ERR(prio)) {
1497                 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1498                 return PTR_ERR(prio);
1499         } else {
1500                 return 0;
1501         }
1502 }
1503
1504 int mlx5_init_fs(struct mlx5_core_dev *dev)
1505 {
1506         int err = 0;
1507
1508         if (MLX5_CAP_GEN(dev, nic_flow_table)) {
1509                 err = init_root_ns(dev);
1510                 if (err)
1511                         return err;
1512         }
1513         if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
1514                 err = init_fdb_root_ns(dev);
1515                 if (err)
1516                         cleanup_root_ns(dev);
1517         }
1518
1519         return err;
1520 }