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