net/mlx5_core: Introduce flow steering autogrouped flow 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 INIT_PRIO(min_level_val, max_ft_val,\
44                   start_level_val, ...) {.type = FS_TYPE_PRIO,\
45         .min_ft_level = min_level_val,\
46         .start_level = start_level_val,\
47         .max_ft = max_ft_val,\
48         .children = (struct init_tree_node[]) {__VA_ARGS__},\
49         .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
50 }
51
52 #define ADD_PRIO(min_level_val, max_ft_val, start_level_val, ...)\
53         INIT_PRIO(min_level_val, max_ft_val, start_level_val,\
54                   __VA_ARGS__)\
55
56 #define ADD_FT_PRIO(max_ft_val, start_level_val, ...)\
57         INIT_PRIO(0, max_ft_val, start_level_val,\
58                   __VA_ARGS__)\
59
60 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
61         .children = (struct init_tree_node[]) {__VA_ARGS__},\
62         .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
63 }
64
65 #define KERNEL_START_LEVEL 0
66 #define KERNEL_P0_START_LEVEL KERNEL_START_LEVEL
67 #define KERNEL_MAX_FT 2
68 #define KENREL_MIN_LEVEL 2
69 static struct init_tree_node {
70         enum fs_node_type       type;
71         struct init_tree_node *children;
72         int ar_size;
73         int min_ft_level;
74         int prio;
75         int max_ft;
76         int start_level;
77 } root_fs = {
78         .type = FS_TYPE_NAMESPACE,
79         .ar_size = 1,
80         .children = (struct init_tree_node[]) {
81                 ADD_PRIO(KENREL_MIN_LEVEL, KERNEL_MAX_FT,
82                          KERNEL_START_LEVEL,
83                          ADD_NS(ADD_FT_PRIO(KERNEL_MAX_FT,
84                                             KERNEL_P0_START_LEVEL))),
85         }
86 };
87
88 enum fs_i_mutex_lock_class {
89         FS_MUTEX_GRANDPARENT,
90         FS_MUTEX_PARENT,
91         FS_MUTEX_CHILD
92 };
93
94 static void del_rule(struct fs_node *node);
95 static void del_flow_table(struct fs_node *node);
96 static void del_flow_group(struct fs_node *node);
97 static void del_fte(struct fs_node *node);
98
99 static void tree_init_node(struct fs_node *node,
100                            unsigned int refcount,
101                            void (*remove_func)(struct fs_node *))
102 {
103         atomic_set(&node->refcount, refcount);
104         INIT_LIST_HEAD(&node->list);
105         INIT_LIST_HEAD(&node->children);
106         mutex_init(&node->lock);
107         node->remove_func = remove_func;
108 }
109
110 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
111 {
112         if (parent)
113                 atomic_inc(&parent->refcount);
114         node->parent = parent;
115
116         /* Parent is the root */
117         if (!parent)
118                 node->root = node;
119         else
120                 node->root = parent->root;
121 }
122
123 static void tree_get_node(struct fs_node *node)
124 {
125         atomic_inc(&node->refcount);
126 }
127
128 static void nested_lock_ref_node(struct fs_node *node,
129                                  enum fs_i_mutex_lock_class class)
130 {
131         if (node) {
132                 mutex_lock_nested(&node->lock, class);
133                 atomic_inc(&node->refcount);
134         }
135 }
136
137 static void lock_ref_node(struct fs_node *node)
138 {
139         if (node) {
140                 mutex_lock(&node->lock);
141                 atomic_inc(&node->refcount);
142         }
143 }
144
145 static void unlock_ref_node(struct fs_node *node)
146 {
147         if (node) {
148                 atomic_dec(&node->refcount);
149                 mutex_unlock(&node->lock);
150         }
151 }
152
153 static void tree_put_node(struct fs_node *node)
154 {
155         struct fs_node *parent_node = node->parent;
156
157         lock_ref_node(parent_node);
158         if (atomic_dec_and_test(&node->refcount)) {
159                 if (parent_node)
160                         list_del_init(&node->list);
161                 if (node->remove_func)
162                         node->remove_func(node);
163                 kfree(node);
164                 node = NULL;
165         }
166         unlock_ref_node(parent_node);
167         if (!node && parent_node)
168                 tree_put_node(parent_node);
169 }
170
171 static int tree_remove_node(struct fs_node *node)
172 {
173         if (atomic_read(&node->refcount) > 1)
174                 return -EPERM;
175         tree_put_node(node);
176         return 0;
177 }
178
179 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
180                                  unsigned int prio)
181 {
182         struct fs_prio *iter_prio;
183
184         fs_for_each_prio(iter_prio, ns) {
185                 if (iter_prio->prio == prio)
186                         return iter_prio;
187         }
188
189         return NULL;
190 }
191
192 static unsigned int find_next_free_level(struct fs_prio *prio)
193 {
194         if (!list_empty(&prio->node.children)) {
195                 struct mlx5_flow_table *ft;
196
197                 ft = list_last_entry(&prio->node.children,
198                                      struct mlx5_flow_table,
199                                      node.list);
200                 return ft->level + 1;
201         }
202         return prio->start_level;
203 }
204
205 static bool masked_memcmp(void *mask, void *val1, void *val2, size_t size)
206 {
207         unsigned int i;
208
209         for (i = 0; i < size; i++, mask++, val1++, val2++)
210                 if ((*((u8 *)val1) & (*(u8 *)mask)) !=
211                     ((*(u8 *)val2) & (*(u8 *)mask)))
212                         return false;
213
214         return true;
215 }
216
217 static bool compare_match_value(struct mlx5_flow_group_mask *mask,
218                                 void *fte_param1, void *fte_param2)
219 {
220         if (mask->match_criteria_enable &
221             1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) {
222                 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
223                                                 fte_param1, outer_headers);
224                 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
225                                                 fte_param2, outer_headers);
226                 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
227                                               mask->match_criteria, outer_headers);
228
229                 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
230                                    MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
231                         return false;
232         }
233
234         if (mask->match_criteria_enable &
235             1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) {
236                 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
237                                                 fte_param1, misc_parameters);
238                 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
239                                                 fte_param2, misc_parameters);
240                 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
241                                           mask->match_criteria, misc_parameters);
242
243                 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
244                                    MLX5_ST_SZ_BYTES(fte_match_set_misc)))
245                         return false;
246         }
247
248         if (mask->match_criteria_enable &
249             1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS) {
250                 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
251                                                 fte_param1, inner_headers);
252                 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
253                                                 fte_param2, inner_headers);
254                 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
255                                           mask->match_criteria, inner_headers);
256
257                 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
258                                    MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
259                         return false;
260         }
261         return true;
262 }
263
264 static bool compare_match_criteria(u8 match_criteria_enable1,
265                                    u8 match_criteria_enable2,
266                                    void *mask1, void *mask2)
267 {
268         return match_criteria_enable1 == match_criteria_enable2 &&
269                 !memcmp(mask1, mask2, MLX5_ST_SZ_BYTES(fte_match_param));
270 }
271
272 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
273 {
274         struct fs_node *root;
275         struct mlx5_flow_namespace *ns;
276
277         root = node->root;
278
279         if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
280                 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
281                 return NULL;
282         }
283
284         ns = container_of(root, struct mlx5_flow_namespace, node);
285         return container_of(ns, struct mlx5_flow_root_namespace, ns);
286 }
287
288 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
289 {
290         struct mlx5_flow_root_namespace *root = find_root(node);
291
292         if (root)
293                 return root->dev;
294         return NULL;
295 }
296
297 static void del_flow_table(struct fs_node *node)
298 {
299         struct mlx5_flow_table *ft;
300         struct mlx5_core_dev *dev;
301         struct fs_prio *prio;
302         int err;
303
304         fs_get_obj(ft, node);
305         dev = get_dev(&ft->node);
306
307         err = mlx5_cmd_destroy_flow_table(dev, ft);
308         if (err)
309                 pr_warn("flow steering can't destroy ft\n");
310         fs_get_obj(prio, ft->node.parent);
311         prio->num_ft--;
312 }
313
314 static void del_rule(struct fs_node *node)
315 {
316         struct mlx5_flow_rule *rule;
317         struct mlx5_flow_table *ft;
318         struct mlx5_flow_group *fg;
319         struct fs_fte *fte;
320         u32     *match_value;
321         struct mlx5_core_dev *dev = get_dev(node);
322         int match_len = MLX5_ST_SZ_BYTES(fte_match_param);
323         int err;
324
325         match_value = mlx5_vzalloc(match_len);
326         if (!match_value) {
327                 pr_warn("failed to allocate inbox\n");
328                 return;
329         }
330
331         fs_get_obj(rule, node);
332         fs_get_obj(fte, rule->node.parent);
333         fs_get_obj(fg, fte->node.parent);
334         memcpy(match_value, fte->val, sizeof(fte->val));
335         fs_get_obj(ft, fg->node.parent);
336         list_del(&rule->node.list);
337         fte->dests_size--;
338         if (fte->dests_size) {
339                 err = mlx5_cmd_update_fte(dev, ft,
340                                           fg->id, fte);
341                 if (err)
342                         pr_warn("%s can't del rule fg id=%d fte_index=%d\n",
343                                 __func__, fg->id, fte->index);
344         }
345         kvfree(match_value);
346 }
347
348 static void del_fte(struct fs_node *node)
349 {
350         struct mlx5_flow_table *ft;
351         struct mlx5_flow_group *fg;
352         struct mlx5_core_dev *dev;
353         struct fs_fte *fte;
354         int err;
355
356         fs_get_obj(fte, node);
357         fs_get_obj(fg, fte->node.parent);
358         fs_get_obj(ft, fg->node.parent);
359
360         dev = get_dev(&ft->node);
361         err = mlx5_cmd_delete_fte(dev, ft,
362                                   fte->index);
363         if (err)
364                 pr_warn("flow steering can't delete fte in index %d of flow group id %d\n",
365                         fte->index, fg->id);
366
367         fte->status = 0;
368         fg->num_ftes--;
369 }
370
371 static void del_flow_group(struct fs_node *node)
372 {
373         struct mlx5_flow_group *fg;
374         struct mlx5_flow_table *ft;
375         struct mlx5_core_dev *dev;
376
377         fs_get_obj(fg, node);
378         fs_get_obj(ft, fg->node.parent);
379         dev = get_dev(&ft->node);
380
381         if (mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
382                 pr_warn("flow steering can't destroy fg %d of ft %d\n",
383                         fg->id, ft->id);
384 }
385
386 static struct fs_fte *alloc_fte(u8 action,
387                                 u32 flow_tag,
388                                 u32 *match_value,
389                                 unsigned int index)
390 {
391         struct fs_fte *fte;
392
393         fte = kzalloc(sizeof(*fte), GFP_KERNEL);
394         if (!fte)
395                 return ERR_PTR(-ENOMEM);
396
397         memcpy(fte->val, match_value, sizeof(fte->val));
398         fte->node.type =  FS_TYPE_FLOW_ENTRY;
399         fte->flow_tag = flow_tag;
400         fte->index = index;
401         fte->action = action;
402
403         return fte;
404 }
405
406 static struct mlx5_flow_group *alloc_flow_group(u32 *create_fg_in)
407 {
408         struct mlx5_flow_group *fg;
409         void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
410                                             create_fg_in, match_criteria);
411         u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
412                                             create_fg_in,
413                                             match_criteria_enable);
414         fg = kzalloc(sizeof(*fg), GFP_KERNEL);
415         if (!fg)
416                 return ERR_PTR(-ENOMEM);
417
418         fg->mask.match_criteria_enable = match_criteria_enable;
419         memcpy(&fg->mask.match_criteria, match_criteria,
420                sizeof(fg->mask.match_criteria));
421         fg->node.type =  FS_TYPE_FLOW_GROUP;
422         fg->start_index = MLX5_GET(create_flow_group_in, create_fg_in,
423                                    start_flow_index);
424         fg->max_ftes = MLX5_GET(create_flow_group_in, create_fg_in,
425                                 end_flow_index) - fg->start_index + 1;
426         return fg;
427 }
428
429 static struct mlx5_flow_table *alloc_flow_table(int level, int max_fte,
430                                                 enum fs_flow_table_type table_type)
431 {
432         struct mlx5_flow_table *ft;
433
434         ft  = kzalloc(sizeof(*ft), GFP_KERNEL);
435         if (!ft)
436                 return NULL;
437
438         ft->level = level;
439         ft->node.type = FS_TYPE_FLOW_TABLE;
440         ft->type = table_type;
441         ft->max_fte = max_fte;
442
443         return ft;
444 }
445
446 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
447                                                int prio,
448                                                int max_fte)
449 {
450         struct mlx5_flow_table *ft;
451         int err;
452         int log_table_sz;
453         struct mlx5_flow_root_namespace *root =
454                 find_root(&ns->node);
455         struct fs_prio *fs_prio = NULL;
456
457         if (!root) {
458                 pr_err("mlx5: flow steering failed to find root of namespace\n");
459                 return ERR_PTR(-ENODEV);
460         }
461
462         fs_prio = find_prio(ns, prio);
463         if (!fs_prio)
464                 return ERR_PTR(-EINVAL);
465
466         lock_ref_node(&fs_prio->node);
467         if (fs_prio->num_ft == fs_prio->max_ft) {
468                 err = -ENOSPC;
469                 goto unlock_prio;
470         }
471
472         ft = alloc_flow_table(find_next_free_level(fs_prio),
473                               roundup_pow_of_two(max_fte),
474                               root->table_type);
475         if (!ft) {
476                 err = -ENOMEM;
477                 goto unlock_prio;
478         }
479
480         tree_init_node(&ft->node, 1, del_flow_table);
481         log_table_sz = ilog2(ft->max_fte);
482         err = mlx5_cmd_create_flow_table(root->dev, ft->type, ft->level,
483                                          log_table_sz, &ft->id);
484         if (err)
485                 goto free_ft;
486
487         tree_add_node(&ft->node, &fs_prio->node);
488         list_add_tail(&ft->node.list, &fs_prio->node.children);
489         fs_prio->num_ft++;
490         unlock_ref_node(&fs_prio->node);
491         return ft;
492 free_ft:
493         kfree(ft);
494 unlock_prio:
495         unlock_ref_node(&fs_prio->node);
496         return ERR_PTR(err);
497 }
498
499 struct mlx5_flow_table *mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
500                                                             int prio,
501                                                             int num_flow_table_entries,
502                                                             int max_num_groups)
503 {
504         struct mlx5_flow_table *ft;
505
506         if (max_num_groups > num_flow_table_entries)
507                 return ERR_PTR(-EINVAL);
508
509         ft = mlx5_create_flow_table(ns, prio, num_flow_table_entries);
510         if (IS_ERR(ft))
511                 return ft;
512
513         ft->autogroup.active = true;
514         ft->autogroup.required_groups = max_num_groups;
515
516         return ft;
517 }
518
519 /* Flow table should be locked */
520 static struct mlx5_flow_group *create_flow_group_common(struct mlx5_flow_table *ft,
521                                                         u32 *fg_in,
522                                                         struct list_head
523                                                         *prev_fg,
524                                                         bool is_auto_fg)
525 {
526         struct mlx5_flow_group *fg;
527         struct mlx5_core_dev *dev = get_dev(&ft->node);
528         int err;
529
530         if (!dev)
531                 return ERR_PTR(-ENODEV);
532
533         fg = alloc_flow_group(fg_in);
534         if (IS_ERR(fg))
535                 return fg;
536
537         err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
538         if (err) {
539                 kfree(fg);
540                 return ERR_PTR(err);
541         }
542
543         if (ft->autogroup.active)
544                 ft->autogroup.num_groups++;
545         /* Add node to tree */
546         tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
547         tree_add_node(&fg->node, &ft->node);
548         /* Add node to group list */
549         list_add(&fg->node.list, ft->node.children.prev);
550
551         return fg;
552 }
553
554 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
555                                                u32 *fg_in)
556 {
557         struct mlx5_flow_group *fg;
558
559         if (ft->autogroup.active)
560                 return ERR_PTR(-EPERM);
561
562         lock_ref_node(&ft->node);
563         fg = create_flow_group_common(ft, fg_in, &ft->node.children, false);
564         unlock_ref_node(&ft->node);
565
566         return fg;
567 }
568
569 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
570 {
571         struct mlx5_flow_rule *rule;
572
573         rule = kzalloc(sizeof(*rule), GFP_KERNEL);
574         if (!rule)
575                 return NULL;
576
577         rule->node.type = FS_TYPE_FLOW_DEST;
578         memcpy(&rule->dest_attr, dest, sizeof(*dest));
579
580         return rule;
581 }
582
583 /* fte should not be deleted while calling this function */
584 static struct mlx5_flow_rule *add_rule_fte(struct fs_fte *fte,
585                                            struct mlx5_flow_group *fg,
586                                            struct mlx5_flow_destination *dest)
587 {
588         struct mlx5_flow_table *ft;
589         struct mlx5_flow_rule *rule;
590         int err;
591
592         rule = alloc_rule(dest);
593         if (!rule)
594                 return ERR_PTR(-ENOMEM);
595
596         fs_get_obj(ft, fg->node.parent);
597         /* Add dest to dests list- added as first element after the head */
598         tree_init_node(&rule->node, 1, del_rule);
599         list_add_tail(&rule->node.list, &fte->node.children);
600         fte->dests_size++;
601         if (fte->dests_size == 1)
602                 err = mlx5_cmd_create_fte(get_dev(&ft->node),
603                                           ft, fg->id, fte);
604         else
605                 err = mlx5_cmd_update_fte(get_dev(&ft->node),
606                                           ft, fg->id, fte);
607         if (err)
608                 goto free_rule;
609
610         fte->status |= FS_FTE_STATUS_EXISTING;
611
612         return rule;
613
614 free_rule:
615         list_del(&rule->node.list);
616         kfree(rule);
617         fte->dests_size--;
618         return ERR_PTR(err);
619 }
620
621 /* Assumed fg is locked */
622 static unsigned int get_free_fte_index(struct mlx5_flow_group *fg,
623                                        struct list_head **prev)
624 {
625         struct fs_fte *fte;
626         unsigned int start = fg->start_index;
627
628         if (prev)
629                 *prev = &fg->node.children;
630
631         /* assumed list is sorted by index */
632         fs_for_each_fte(fte, fg) {
633                 if (fte->index != start)
634                         return start;
635                 start++;
636                 if (prev)
637                         *prev = &fte->node.list;
638         }
639
640         return start;
641 }
642
643 /* prev is output, prev->next = new_fte */
644 static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
645                                  u32 *match_value,
646                                  u8 action,
647                                  u32 flow_tag,
648                                  struct list_head **prev)
649 {
650         struct fs_fte *fte;
651         int index;
652
653         index = get_free_fte_index(fg, prev);
654         fte = alloc_fte(action, flow_tag, match_value, index);
655         if (IS_ERR(fte))
656                 return fte;
657
658         return fte;
659 }
660
661 static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
662                                                 u8 match_criteria_enable,
663                                                 u32 *match_criteria)
664 {
665         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
666         struct list_head *prev = &ft->node.children;
667         unsigned int candidate_index = 0;
668         struct mlx5_flow_group *fg;
669         void *match_criteria_addr;
670         unsigned int group_size = 0;
671         u32 *in;
672
673         if (!ft->autogroup.active)
674                 return ERR_PTR(-ENOENT);
675
676         in = mlx5_vzalloc(inlen);
677         if (!in)
678                 return ERR_PTR(-ENOMEM);
679
680         if (ft->autogroup.num_groups < ft->autogroup.required_groups)
681                 /* We save place for flow groups in addition to max types */
682                 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
683
684         /*  ft->max_fte == ft->autogroup.max_types */
685         if (group_size == 0)
686                 group_size = 1;
687
688         /* sorted by start_index */
689         fs_for_each_fg(fg, ft) {
690                 if (candidate_index + group_size > fg->start_index)
691                         candidate_index = fg->start_index + fg->max_ftes;
692                 else
693                         break;
694                 prev = &fg->node.list;
695         }
696
697         if (candidate_index + group_size > ft->max_fte) {
698                 fg = ERR_PTR(-ENOSPC);
699                 goto out;
700         }
701
702         MLX5_SET(create_flow_group_in, in, match_criteria_enable,
703                  match_criteria_enable);
704         MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
705         MLX5_SET(create_flow_group_in, in, end_flow_index,   candidate_index +
706                  group_size - 1);
707         match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
708                                            in, match_criteria);
709         memcpy(match_criteria_addr, match_criteria,
710                MLX5_ST_SZ_BYTES(fte_match_param));
711
712         fg = create_flow_group_common(ft, in, prev, true);
713 out:
714         kvfree(in);
715         return fg;
716 }
717
718 static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
719                                           u32 *match_value,
720                                           u8 action,
721                                           u32 flow_tag,
722                                           struct mlx5_flow_destination *dest)
723 {
724         struct fs_fte *fte;
725         struct mlx5_flow_rule *rule;
726         struct mlx5_flow_table *ft;
727         struct list_head *prev;
728
729         nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
730         fs_for_each_fte(fte, fg) {
731                 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
732                 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
733                     action == fte->action && flow_tag == fte->flow_tag) {
734                         rule = add_rule_fte(fte, fg, dest);
735                         unlock_ref_node(&fte->node);
736                         if (IS_ERR(rule))
737                                 goto unlock_fg;
738                         else
739                                 goto add_rule;
740                 }
741                 unlock_ref_node(&fte->node);
742         }
743         fs_get_obj(ft, fg->node.parent);
744         if (fg->num_ftes >= fg->max_ftes) {
745                 rule = ERR_PTR(-ENOSPC);
746                 goto unlock_fg;
747         }
748
749         fte = create_fte(fg, match_value, action, flow_tag, &prev);
750         if (IS_ERR(fte)) {
751                 rule = (void *)fte;
752                 goto unlock_fg;
753         }
754         tree_init_node(&fte->node, 0, del_fte);
755         rule = add_rule_fte(fte, fg, dest);
756         if (IS_ERR(rule)) {
757                 kfree(fte);
758                 goto unlock_fg;
759         }
760
761         fg->num_ftes++;
762
763         tree_add_node(&fte->node, &fg->node);
764         list_add(&fte->node.list, prev);
765 add_rule:
766         tree_add_node(&rule->node, &fte->node);
767 unlock_fg:
768         unlock_ref_node(&fg->node);
769         return rule;
770 }
771
772 static struct mlx5_flow_rule *add_rule_to_auto_fg(struct mlx5_flow_table *ft,
773                                                   u8 match_criteria_enable,
774                                                   u32 *match_criteria,
775                                                   u32 *match_value,
776                                                   u8 action,
777                                                   u32 flow_tag,
778                                                   struct mlx5_flow_destination *dest)
779 {
780         struct mlx5_flow_rule *rule;
781         struct mlx5_flow_group *g;
782
783         g = create_autogroup(ft, match_criteria_enable, match_criteria);
784         if (IS_ERR(g))
785                 return (void *)g;
786
787         rule = add_rule_fg(g, match_value,
788                            action, flow_tag, dest);
789         if (IS_ERR(rule)) {
790                 /* Remove assumes refcount > 0 and autogroup creates a group
791                  * with a refcount = 0.
792                  */
793                 tree_get_node(&g->node);
794                 tree_remove_node(&g->node);
795         }
796         return rule;
797 }
798
799 struct mlx5_flow_rule *
800 mlx5_add_flow_rule(struct mlx5_flow_table *ft,
801                    u8 match_criteria_enable,
802                    u32 *match_criteria,
803                    u32 *match_value,
804                    u32 action,
805                    u32 flow_tag,
806                    struct mlx5_flow_destination *dest)
807 {
808         struct mlx5_flow_group *g;
809         struct mlx5_flow_rule *rule;
810
811         nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
812         fs_for_each_fg(g, ft)
813                 if (compare_match_criteria(g->mask.match_criteria_enable,
814                                            match_criteria_enable,
815                                            g->mask.match_criteria,
816                                            match_criteria)) {
817                         rule = add_rule_fg(g, match_value,
818                                            action, flow_tag, dest);
819                         if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
820                                 goto unlock;
821                 }
822
823         rule = add_rule_to_auto_fg(ft, match_criteria_enable, match_criteria,
824                                    match_value, action, flow_tag, dest);
825 unlock:
826         unlock_ref_node(&ft->node);
827         return rule;
828 }
829
830 void mlx5_del_flow_rule(struct mlx5_flow_rule *rule)
831 {
832         tree_remove_node(&rule->node);
833 }
834
835 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
836 {
837         if (tree_remove_node(&ft->node))
838                 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
839                                ft->id);
840
841         return 0;
842 }
843
844 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
845 {
846         if (tree_remove_node(&fg->node))
847                 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
848                                fg->id);
849 }
850
851 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
852                                                     enum mlx5_flow_namespace_type type)
853 {
854         struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
855         int prio;
856         static struct fs_prio *fs_prio;
857         struct mlx5_flow_namespace *ns;
858
859         if (!root_ns)
860                 return NULL;
861
862         switch (type) {
863         case MLX5_FLOW_NAMESPACE_KERNEL:
864                 prio = 0;
865                 break;
866         case MLX5_FLOW_NAMESPACE_FDB:
867                 if (dev->priv.fdb_root_ns)
868                         return &dev->priv.fdb_root_ns->ns;
869                 else
870                         return NULL;
871         default:
872                 return NULL;
873         }
874
875         fs_prio = find_prio(&root_ns->ns, prio);
876         if (!fs_prio)
877                 return NULL;
878
879         ns = list_first_entry(&fs_prio->node.children,
880                               typeof(*ns),
881                               node.list);
882
883         return ns;
884 }
885
886 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
887                                       unsigned prio, int max_ft,
888                                       int start_level)
889 {
890         struct fs_prio *fs_prio;
891
892         fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
893         if (!fs_prio)
894                 return ERR_PTR(-ENOMEM);
895
896         fs_prio->node.type = FS_TYPE_PRIO;
897         tree_init_node(&fs_prio->node, 1, NULL);
898         tree_add_node(&fs_prio->node, &ns->node);
899         fs_prio->max_ft = max_ft;
900         fs_prio->prio = prio;
901         fs_prio->start_level = start_level;
902         list_add_tail(&fs_prio->node.list, &ns->node.children);
903
904         return fs_prio;
905 }
906
907 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
908                                                      *ns)
909 {
910         ns->node.type = FS_TYPE_NAMESPACE;
911
912         return ns;
913 }
914
915 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
916 {
917         struct mlx5_flow_namespace      *ns;
918
919         ns = kzalloc(sizeof(*ns), GFP_KERNEL);
920         if (!ns)
921                 return ERR_PTR(-ENOMEM);
922
923         fs_init_namespace(ns);
924         tree_init_node(&ns->node, 1, NULL);
925         tree_add_node(&ns->node, &prio->node);
926         list_add_tail(&ns->node.list, &prio->node.children);
927
928         return ns;
929 }
930
931 static int init_root_tree_recursive(int max_ft_level, struct init_tree_node *init_node,
932                                     struct fs_node *fs_parent_node,
933                                     struct init_tree_node *init_parent_node,
934                                     int index)
935 {
936         struct mlx5_flow_namespace *fs_ns;
937         struct fs_prio *fs_prio;
938         struct fs_node *base;
939         int i;
940         int err;
941
942         if (init_node->type == FS_TYPE_PRIO) {
943                 if (init_node->min_ft_level > max_ft_level)
944                         return -ENOTSUPP;
945
946                 fs_get_obj(fs_ns, fs_parent_node);
947                 fs_prio = fs_create_prio(fs_ns, index, init_node->max_ft,
948                                          init_node->start_level);
949                 if (IS_ERR(fs_prio))
950                         return PTR_ERR(fs_prio);
951                 base = &fs_prio->node;
952         } else if (init_node->type == FS_TYPE_NAMESPACE) {
953                 fs_get_obj(fs_prio, fs_parent_node);
954                 fs_ns = fs_create_namespace(fs_prio);
955                 if (IS_ERR(fs_ns))
956                         return PTR_ERR(fs_ns);
957                 base = &fs_ns->node;
958         } else {
959                 return -EINVAL;
960         }
961         for (i = 0; i < init_node->ar_size; i++) {
962                 err = init_root_tree_recursive(max_ft_level,
963                                                &init_node->children[i], base,
964                                                init_node, i);
965                 if (err)
966                         return err;
967         }
968
969         return 0;
970 }
971
972 static int init_root_tree(int max_ft_level, struct init_tree_node *init_node,
973                           struct fs_node *fs_parent_node)
974 {
975         int i;
976         struct mlx5_flow_namespace *fs_ns;
977         int err;
978
979         fs_get_obj(fs_ns, fs_parent_node);
980         for (i = 0; i < init_node->ar_size; i++) {
981                 err = init_root_tree_recursive(max_ft_level,
982                                                &init_node->children[i],
983                                                &fs_ns->node,
984                                                init_node, i);
985                 if (err)
986                         return err;
987         }
988         return 0;
989 }
990
991 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_core_dev *dev,
992                                                        enum fs_flow_table_type
993                                                        table_type)
994 {
995         struct mlx5_flow_root_namespace *root_ns;
996         struct mlx5_flow_namespace *ns;
997
998         /* Create the root namespace */
999         root_ns = mlx5_vzalloc(sizeof(*root_ns));
1000         if (!root_ns)
1001                 return NULL;
1002
1003         root_ns->dev = dev;
1004         root_ns->table_type = table_type;
1005
1006         ns = &root_ns->ns;
1007         fs_init_namespace(ns);
1008         tree_init_node(&ns->node, 1, NULL);
1009         tree_add_node(&ns->node, NULL);
1010
1011         return root_ns;
1012 }
1013
1014 static int init_root_ns(struct mlx5_core_dev *dev)
1015 {
1016         int max_ft_level = MLX5_CAP_FLOWTABLE(dev,
1017                                               flow_table_properties_nic_receive.
1018                                               max_ft_level);
1019
1020         dev->priv.root_ns = create_root_ns(dev, FS_FT_NIC_RX);
1021         if (IS_ERR_OR_NULL(dev->priv.root_ns))
1022                 goto cleanup;
1023
1024         if (init_root_tree(max_ft_level, &root_fs, &dev->priv.root_ns->ns.node))
1025                 goto cleanup;
1026
1027         return 0;
1028
1029 cleanup:
1030         mlx5_cleanup_fs(dev);
1031         return -ENOMEM;
1032 }
1033
1034 static void cleanup_single_prio_root_ns(struct mlx5_core_dev *dev,
1035                                         struct mlx5_flow_root_namespace *root_ns)
1036 {
1037         struct fs_node *prio;
1038
1039         if (!root_ns)
1040                 return;
1041
1042         if (!list_empty(&root_ns->ns.node.children)) {
1043                 prio = list_first_entry(&root_ns->ns.node.children,
1044                                         struct fs_node,
1045                                  list);
1046                 if (tree_remove_node(prio))
1047                         mlx5_core_warn(dev,
1048                                        "Flow steering priority wasn't destroyed, refcount > 1\n");
1049         }
1050         if (tree_remove_node(&root_ns->ns.node))
1051                 mlx5_core_warn(dev,
1052                                "Flow steering namespace wasn't destroyed, refcount > 1\n");
1053         root_ns = NULL;
1054 }
1055
1056 static void cleanup_root_ns(struct mlx5_core_dev *dev)
1057 {
1058         struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1059         struct fs_prio *iter_prio;
1060
1061         if (!MLX5_CAP_GEN(dev, nic_flow_table))
1062                 return;
1063
1064         if (!root_ns)
1065                 return;
1066
1067         /* stage 1 */
1068         fs_for_each_prio(iter_prio, &root_ns->ns) {
1069                 struct fs_node *node;
1070                 struct mlx5_flow_namespace *iter_ns;
1071
1072                 fs_for_each_ns_or_ft(node, iter_prio) {
1073                         if (node->type == FS_TYPE_FLOW_TABLE)
1074                                 continue;
1075                         fs_get_obj(iter_ns, node);
1076                         while (!list_empty(&iter_ns->node.children)) {
1077                                 struct fs_prio *obj_iter_prio2;
1078                                 struct fs_node *iter_prio2 =
1079                                         list_first_entry(&iter_ns->node.children,
1080                                                          struct fs_node,
1081                                                          list);
1082
1083                                 fs_get_obj(obj_iter_prio2, iter_prio2);
1084                                 if (tree_remove_node(iter_prio2)) {
1085                                         mlx5_core_warn(dev,
1086                                                        "Priority %d wasn't destroyed, refcount > 1\n",
1087                                                        obj_iter_prio2->prio);
1088                                         return;
1089                                 }
1090                         }
1091                 }
1092         }
1093
1094         /* stage 2 */
1095         fs_for_each_prio(iter_prio, &root_ns->ns) {
1096                 while (!list_empty(&iter_prio->node.children)) {
1097                         struct fs_node *iter_ns =
1098                                 list_first_entry(&iter_prio->node.children,
1099                                                  struct fs_node,
1100                                                  list);
1101                         if (tree_remove_node(iter_ns)) {
1102                                 mlx5_core_warn(dev,
1103                                                "Namespace wasn't destroyed, refcount > 1\n");
1104                                 return;
1105                         }
1106                 }
1107         }
1108
1109         /* stage 3 */
1110         while (!list_empty(&root_ns->ns.node.children)) {
1111                 struct fs_prio *obj_prio_node;
1112                 struct fs_node *prio_node =
1113                         list_first_entry(&root_ns->ns.node.children,
1114                                          struct fs_node,
1115                                          list);
1116
1117                 fs_get_obj(obj_prio_node, prio_node);
1118                 if (tree_remove_node(prio_node)) {
1119                         mlx5_core_warn(dev,
1120                                        "Priority %d wasn't destroyed, refcount > 1\n",
1121                                        obj_prio_node->prio);
1122                         return;
1123                 }
1124         }
1125
1126         if (tree_remove_node(&root_ns->ns.node)) {
1127                 mlx5_core_warn(dev,
1128                                "root namespace wasn't destroyed, refcount > 1\n");
1129                 return;
1130         }
1131
1132         dev->priv.root_ns = NULL;
1133 }
1134
1135 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1136 {
1137         cleanup_root_ns(dev);
1138         cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1139 }
1140
1141 static int init_fdb_root_ns(struct mlx5_core_dev *dev)
1142 {
1143         struct fs_prio *prio;
1144
1145         dev->priv.fdb_root_ns = create_root_ns(dev, FS_FT_FDB);
1146         if (!dev->priv.fdb_root_ns)
1147                 return -ENOMEM;
1148
1149         /* Create single prio */
1150         prio = fs_create_prio(&dev->priv.fdb_root_ns->ns, 0, 1, 0);
1151         if (IS_ERR(prio)) {
1152                 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1153                 return PTR_ERR(prio);
1154         } else {
1155                 return 0;
1156         }
1157 }
1158
1159 int mlx5_init_fs(struct mlx5_core_dev *dev)
1160 {
1161         int err = 0;
1162
1163         if (MLX5_CAP_GEN(dev, nic_flow_table)) {
1164                 err = init_root_ns(dev);
1165                 if (err)
1166                         return err;
1167         }
1168         if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
1169                 err = init_fdb_root_ns(dev);
1170                 if (err)
1171                         cleanup_root_ns(dev);
1172         }
1173
1174         return err;
1175 }