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