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