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