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