Merge branch 'mlx5-next' of git://git.kernel.org/pub/scm/linux/kernel/git/mellanox...
[sfrench/cifs-2.6.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 #include <linux/mlx5/eswitch.h>
36
37 #include "mlx5_core.h"
38 #include "fs_core.h"
39 #include "fs_cmd.h"
40 #include "diag/fs_tracepoint.h"
41 #include "accel/ipsec.h"
42 #include "fpga/ipsec.h"
43
44 #define INIT_TREE_NODE_ARRAY_SIZE(...)  (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
45                                          sizeof(struct init_tree_node))
46
47 #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
48                  ...) {.type = FS_TYPE_PRIO,\
49         .min_ft_level = min_level_val,\
50         .num_levels = num_levels_val,\
51         .num_leaf_prios = num_prios_val,\
52         .caps = caps_val,\
53         .children = (struct init_tree_node[]) {__VA_ARGS__},\
54         .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
55 }
56
57 #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
58         ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
59                  __VA_ARGS__)\
60
61 #define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
62         .children = (struct init_tree_node[]) {__VA_ARGS__},\
63         .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
64 }
65
66 #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
67                                    sizeof(long))
68
69 #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
70
71 #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
72                                .caps = (long[]) {__VA_ARGS__} }
73
74 #define FS_CHAINING_CAPS  FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
75                                            FS_CAP(flow_table_properties_nic_receive.modify_root), \
76                                            FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
77                                            FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
78
79 #define FS_CHAINING_CAPS_EGRESS                                                \
80         FS_REQUIRED_CAPS(                                                      \
81                 FS_CAP(flow_table_properties_nic_transmit.flow_modify_en),     \
82                 FS_CAP(flow_table_properties_nic_transmit.modify_root),        \
83                 FS_CAP(flow_table_properties_nic_transmit                      \
84                                .identified_miss_table_mode),                   \
85                 FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
86
87 #define LEFTOVERS_NUM_LEVELS 1
88 #define LEFTOVERS_NUM_PRIOS 1
89
90 #define BY_PASS_PRIO_NUM_LEVELS 1
91 #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
92                            LEFTOVERS_NUM_PRIOS)
93
94 #define ETHTOOL_PRIO_NUM_LEVELS 1
95 #define ETHTOOL_NUM_PRIOS 11
96 #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
97 /* Vlan, mac, ttc, inner ttc, aRFS */
98 #define KERNEL_NIC_PRIO_NUM_LEVELS 5
99 #define KERNEL_NIC_NUM_PRIOS 1
100 /* One more level for tc */
101 #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
102
103 #define KERNEL_NIC_TC_NUM_PRIOS  1
104 #define KERNEL_NIC_TC_NUM_LEVELS 2
105
106 #define ANCHOR_NUM_LEVELS 1
107 #define ANCHOR_NUM_PRIOS 1
108 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
109
110 #define OFFLOADS_MAX_FT 1
111 #define OFFLOADS_NUM_PRIOS 1
112 #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
113
114 #define LAG_PRIO_NUM_LEVELS 1
115 #define LAG_NUM_PRIOS 1
116 #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
117
118 struct node_caps {
119         size_t  arr_sz;
120         long    *caps;
121 };
122
123 static struct init_tree_node {
124         enum fs_node_type       type;
125         struct init_tree_node *children;
126         int ar_size;
127         struct node_caps caps;
128         int min_ft_level;
129         int num_leaf_prios;
130         int prio;
131         int num_levels;
132 } root_fs = {
133         .type = FS_TYPE_NAMESPACE,
134         .ar_size = 7,
135         .children = (struct init_tree_node[]) {
136                 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
137                          FS_CHAINING_CAPS,
138                          ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
139                                                   BY_PASS_PRIO_NUM_LEVELS))),
140                 ADD_PRIO(0, LAG_MIN_LEVEL, 0,
141                          FS_CHAINING_CAPS,
142                          ADD_NS(ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
143                                                   LAG_PRIO_NUM_LEVELS))),
144                 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {},
145                          ADD_NS(ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS, OFFLOADS_MAX_FT))),
146                 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0,
147                          FS_CHAINING_CAPS,
148                          ADD_NS(ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
149                                                   ETHTOOL_PRIO_NUM_LEVELS))),
150                 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
151                          ADD_NS(ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS, KERNEL_NIC_TC_NUM_LEVELS),
152                                 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
153                                                   KERNEL_NIC_PRIO_NUM_LEVELS))),
154                 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
155                          FS_CHAINING_CAPS,
156                          ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_NUM_LEVELS))),
157                 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
158                          ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_NUM_LEVELS))),
159         }
160 };
161
162 static struct init_tree_node egress_root_fs = {
163         .type = FS_TYPE_NAMESPACE,
164         .ar_size = 1,
165         .children = (struct init_tree_node[]) {
166                 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
167                          FS_CHAINING_CAPS_EGRESS,
168                          ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
169                                                   BY_PASS_PRIO_NUM_LEVELS))),
170         }
171 };
172
173 enum fs_i_lock_class {
174         FS_LOCK_GRANDPARENT,
175         FS_LOCK_PARENT,
176         FS_LOCK_CHILD
177 };
178
179 static const struct rhashtable_params rhash_fte = {
180         .key_len = FIELD_SIZEOF(struct fs_fte, val),
181         .key_offset = offsetof(struct fs_fte, val),
182         .head_offset = offsetof(struct fs_fte, hash),
183         .automatic_shrinking = true,
184         .min_size = 1,
185 };
186
187 static const struct rhashtable_params rhash_fg = {
188         .key_len = FIELD_SIZEOF(struct mlx5_flow_group, mask),
189         .key_offset = offsetof(struct mlx5_flow_group, mask),
190         .head_offset = offsetof(struct mlx5_flow_group, hash),
191         .automatic_shrinking = true,
192         .min_size = 1,
193
194 };
195
196 static void del_hw_flow_table(struct fs_node *node);
197 static void del_hw_flow_group(struct fs_node *node);
198 static void del_hw_fte(struct fs_node *node);
199 static void del_sw_flow_table(struct fs_node *node);
200 static void del_sw_flow_group(struct fs_node *node);
201 static void del_sw_fte(struct fs_node *node);
202 static void del_sw_prio(struct fs_node *node);
203 static void del_sw_ns(struct fs_node *node);
204 /* Delete rule (destination) is special case that 
205  * requires to lock the FTE for all the deletion process.
206  */
207 static void del_sw_hw_rule(struct fs_node *node);
208 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
209                                 struct mlx5_flow_destination *d2);
210 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
211 static struct mlx5_flow_rule *
212 find_flow_rule(struct fs_fte *fte,
213                struct mlx5_flow_destination *dest);
214
215 static void tree_init_node(struct fs_node *node,
216                            void (*del_hw_func)(struct fs_node *),
217                            void (*del_sw_func)(struct fs_node *))
218 {
219         refcount_set(&node->refcount, 1);
220         INIT_LIST_HEAD(&node->list);
221         INIT_LIST_HEAD(&node->children);
222         init_rwsem(&node->lock);
223         node->del_hw_func = del_hw_func;
224         node->del_sw_func = del_sw_func;
225         node->active = false;
226 }
227
228 static void tree_add_node(struct fs_node *node, struct fs_node *parent)
229 {
230         if (parent)
231                 refcount_inc(&parent->refcount);
232         node->parent = parent;
233
234         /* Parent is the root */
235         if (!parent)
236                 node->root = node;
237         else
238                 node->root = parent->root;
239 }
240
241 static int tree_get_node(struct fs_node *node)
242 {
243         return refcount_inc_not_zero(&node->refcount);
244 }
245
246 static void nested_down_read_ref_node(struct fs_node *node,
247                                       enum fs_i_lock_class class)
248 {
249         if (node) {
250                 down_read_nested(&node->lock, class);
251                 refcount_inc(&node->refcount);
252         }
253 }
254
255 static void nested_down_write_ref_node(struct fs_node *node,
256                                        enum fs_i_lock_class class)
257 {
258         if (node) {
259                 down_write_nested(&node->lock, class);
260                 refcount_inc(&node->refcount);
261         }
262 }
263
264 static void down_write_ref_node(struct fs_node *node)
265 {
266         if (node) {
267                 down_write(&node->lock);
268                 refcount_inc(&node->refcount);
269         }
270 }
271
272 static void up_read_ref_node(struct fs_node *node)
273 {
274         refcount_dec(&node->refcount);
275         up_read(&node->lock);
276 }
277
278 static void up_write_ref_node(struct fs_node *node)
279 {
280         refcount_dec(&node->refcount);
281         up_write(&node->lock);
282 }
283
284 static void tree_put_node(struct fs_node *node)
285 {
286         struct fs_node *parent_node = node->parent;
287
288         if (refcount_dec_and_test(&node->refcount)) {
289                 if (node->del_hw_func)
290                         node->del_hw_func(node);
291                 if (parent_node) {
292                         /* Only root namespace doesn't have parent and we just
293                          * need to free its node.
294                          */
295                         down_write_ref_node(parent_node);
296                         list_del_init(&node->list);
297                         if (node->del_sw_func)
298                                 node->del_sw_func(node);
299                         up_write_ref_node(parent_node);
300                 } else {
301                         kfree(node);
302                 }
303                 node = NULL;
304         }
305         if (!node && parent_node)
306                 tree_put_node(parent_node);
307 }
308
309 static int tree_remove_node(struct fs_node *node)
310 {
311         if (refcount_read(&node->refcount) > 1) {
312                 refcount_dec(&node->refcount);
313                 return -EEXIST;
314         }
315         tree_put_node(node);
316         return 0;
317 }
318
319 static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
320                                  unsigned int prio)
321 {
322         struct fs_prio *iter_prio;
323
324         fs_for_each_prio(iter_prio, ns) {
325                 if (iter_prio->prio == prio)
326                         return iter_prio;
327         }
328
329         return NULL;
330 }
331
332 static bool check_valid_spec(const struct mlx5_flow_spec *spec)
333 {
334         int i;
335
336         for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
337                 if (spec->match_value[i] & ~spec->match_criteria[i]) {
338                         pr_warn("mlx5_core: match_value differs from match_criteria\n");
339                         return false;
340                 }
341
342         return true;
343 }
344
345 static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
346 {
347         struct fs_node *root;
348         struct mlx5_flow_namespace *ns;
349
350         root = node->root;
351
352         if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
353                 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
354                 return NULL;
355         }
356
357         ns = container_of(root, struct mlx5_flow_namespace, node);
358         return container_of(ns, struct mlx5_flow_root_namespace, ns);
359 }
360
361 static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
362 {
363         struct mlx5_flow_root_namespace *root = find_root(node);
364
365         if (root)
366                 return root->dev->priv.steering;
367         return NULL;
368 }
369
370 static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
371 {
372         struct mlx5_flow_root_namespace *root = find_root(node);
373
374         if (root)
375                 return root->dev;
376         return NULL;
377 }
378
379 static void del_sw_ns(struct fs_node *node)
380 {
381         kfree(node);
382 }
383
384 static void del_sw_prio(struct fs_node *node)
385 {
386         kfree(node);
387 }
388
389 static void del_hw_flow_table(struct fs_node *node)
390 {
391         struct mlx5_flow_root_namespace *root;
392         struct mlx5_flow_table *ft;
393         struct mlx5_core_dev *dev;
394         int err;
395
396         fs_get_obj(ft, node);
397         dev = get_dev(&ft->node);
398         root = find_root(&ft->node);
399
400         if (node->active) {
401                 err = root->cmds->destroy_flow_table(dev, ft);
402                 if (err)
403                         mlx5_core_warn(dev, "flow steering can't destroy ft\n");
404         }
405 }
406
407 static void del_sw_flow_table(struct fs_node *node)
408 {
409         struct mlx5_flow_table *ft;
410         struct fs_prio *prio;
411
412         fs_get_obj(ft, node);
413
414         rhltable_destroy(&ft->fgs_hash);
415         fs_get_obj(prio, ft->node.parent);
416         prio->num_ft--;
417         kfree(ft);
418 }
419
420 static void del_sw_hw_rule(struct fs_node *node)
421 {
422         struct mlx5_flow_root_namespace *root;
423         struct mlx5_flow_rule *rule;
424         struct mlx5_flow_table *ft;
425         struct mlx5_flow_group *fg;
426         struct fs_fte *fte;
427         int modify_mask;
428         struct mlx5_core_dev *dev = get_dev(node);
429         int err;
430         bool update_fte = false;
431
432         fs_get_obj(rule, node);
433         fs_get_obj(fte, rule->node.parent);
434         fs_get_obj(fg, fte->node.parent);
435         fs_get_obj(ft, fg->node.parent);
436         trace_mlx5_fs_del_rule(rule);
437         if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
438                 mutex_lock(&rule->dest_attr.ft->lock);
439                 list_del(&rule->next_ft);
440                 mutex_unlock(&rule->dest_attr.ft->lock);
441         }
442
443         if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER  &&
444             --fte->dests_size) {
445                 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
446                               BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
447                 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
448                 update_fte = true;
449                 goto out;
450         }
451
452         if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
453             --fte->dests_size) {
454                 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
455                 update_fte = true;
456         }
457 out:
458         root = find_root(&ft->node);
459         if (update_fte && fte->dests_size) {
460                 err = root->cmds->update_fte(dev, ft, fg->id, modify_mask, fte);
461                 if (err)
462                         mlx5_core_warn(dev,
463                                        "%s can't del rule fg id=%d fte_index=%d\n",
464                                        __func__, fg->id, fte->index);
465         }
466         kfree(rule);
467 }
468
469 static void del_hw_fte(struct fs_node *node)
470 {
471         struct mlx5_flow_root_namespace *root;
472         struct mlx5_flow_table *ft;
473         struct mlx5_flow_group *fg;
474         struct mlx5_core_dev *dev;
475         struct fs_fte *fte;
476         int err;
477
478         fs_get_obj(fte, node);
479         fs_get_obj(fg, fte->node.parent);
480         fs_get_obj(ft, fg->node.parent);
481
482         trace_mlx5_fs_del_fte(fte);
483         dev = get_dev(&ft->node);
484         root = find_root(&ft->node);
485         if (node->active) {
486                 err = root->cmds->delete_fte(dev, ft, fte);
487                 if (err)
488                         mlx5_core_warn(dev,
489                                        "flow steering can't delete fte in index %d of flow group id %d\n",
490                                        fte->index, fg->id);
491         }
492 }
493
494 static void del_sw_fte(struct fs_node *node)
495 {
496         struct mlx5_flow_steering *steering = get_steering(node);
497         struct mlx5_flow_group *fg;
498         struct fs_fte *fte;
499         int err;
500
501         fs_get_obj(fte, node);
502         fs_get_obj(fg, fte->node.parent);
503
504         err = rhashtable_remove_fast(&fg->ftes_hash,
505                                      &fte->hash,
506                                      rhash_fte);
507         WARN_ON(err);
508         ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
509         kmem_cache_free(steering->ftes_cache, fte);
510 }
511
512 static void del_hw_flow_group(struct fs_node *node)
513 {
514         struct mlx5_flow_root_namespace *root;
515         struct mlx5_flow_group *fg;
516         struct mlx5_flow_table *ft;
517         struct mlx5_core_dev *dev;
518
519         fs_get_obj(fg, node);
520         fs_get_obj(ft, fg->node.parent);
521         dev = get_dev(&ft->node);
522         trace_mlx5_fs_del_fg(fg);
523
524         root = find_root(&ft->node);
525         if (fg->node.active && root->cmds->destroy_flow_group(dev, ft, fg->id))
526                 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
527                                fg->id, ft->id);
528 }
529
530 static void del_sw_flow_group(struct fs_node *node)
531 {
532         struct mlx5_flow_steering *steering = get_steering(node);
533         struct mlx5_flow_group *fg;
534         struct mlx5_flow_table *ft;
535         int err;
536
537         fs_get_obj(fg, node);
538         fs_get_obj(ft, fg->node.parent);
539
540         rhashtable_destroy(&fg->ftes_hash);
541         ida_destroy(&fg->fte_allocator);
542         if (ft->autogroup.active)
543                 ft->autogroup.num_groups--;
544         err = rhltable_remove(&ft->fgs_hash,
545                               &fg->hash,
546                               rhash_fg);
547         WARN_ON(err);
548         kmem_cache_free(steering->fgs_cache, fg);
549 }
550
551 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
552 {
553         int index;
554         int ret;
555
556         index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
557         if (index < 0)
558                 return index;
559
560         fte->index = index + fg->start_index;
561         ret = rhashtable_insert_fast(&fg->ftes_hash,
562                                      &fte->hash,
563                                      rhash_fte);
564         if (ret)
565                 goto err_ida_remove;
566
567         tree_add_node(&fte->node, &fg->node);
568         list_add_tail(&fte->node.list, &fg->node.children);
569         return 0;
570
571 err_ida_remove:
572         ida_simple_remove(&fg->fte_allocator, index);
573         return ret;
574 }
575
576 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
577                                 u32 *match_value,
578                                 struct mlx5_flow_act *flow_act)
579 {
580         struct mlx5_flow_steering *steering = get_steering(&ft->node);
581         struct fs_fte *fte;
582
583         fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
584         if (!fte)
585                 return ERR_PTR(-ENOMEM);
586
587         memcpy(fte->val, match_value, sizeof(fte->val));
588         fte->node.type =  FS_TYPE_FLOW_ENTRY;
589         fte->action = *flow_act;
590
591         tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
592
593         return fte;
594 }
595
596 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
597                                struct mlx5_flow_group *fg)
598 {
599         rhashtable_destroy(&fg->ftes_hash);
600         kmem_cache_free(steering->fgs_cache, fg);
601 }
602
603 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
604                                                 u8 match_criteria_enable,
605                                                 void *match_criteria,
606                                                 int start_index,
607                                                 int end_index)
608 {
609         struct mlx5_flow_group *fg;
610         int ret;
611
612         fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
613         if (!fg)
614                 return ERR_PTR(-ENOMEM);
615
616         ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
617         if (ret) {
618                 kmem_cache_free(steering->fgs_cache, fg);
619                 return ERR_PTR(ret);
620 }
621         ida_init(&fg->fte_allocator);
622         fg->mask.match_criteria_enable = match_criteria_enable;
623         memcpy(&fg->mask.match_criteria, match_criteria,
624                sizeof(fg->mask.match_criteria));
625         fg->node.type =  FS_TYPE_FLOW_GROUP;
626         fg->start_index = start_index;
627         fg->max_ftes = end_index - start_index + 1;
628
629         return fg;
630 }
631
632 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
633                                                        u8 match_criteria_enable,
634                                                        void *match_criteria,
635                                                        int start_index,
636                                                        int end_index,
637                                                        struct list_head *prev)
638 {
639         struct mlx5_flow_steering *steering = get_steering(&ft->node);
640         struct mlx5_flow_group *fg;
641         int ret;
642
643         fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
644                               start_index, end_index);
645         if (IS_ERR(fg))
646                 return fg;
647
648         /* initialize refcnt, add to parent list */
649         ret = rhltable_insert(&ft->fgs_hash,
650                               &fg->hash,
651                               rhash_fg);
652         if (ret) {
653                 dealloc_flow_group(steering, fg);
654                 return ERR_PTR(ret);
655         }
656
657         tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
658         tree_add_node(&fg->node, &ft->node);
659         /* Add node to group list */
660         list_add(&fg->node.list, prev);
661         atomic_inc(&ft->node.version);
662
663         return fg;
664 }
665
666 static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
667                                                 enum fs_flow_table_type table_type,
668                                                 enum fs_flow_table_op_mod op_mod,
669                                                 u32 flags)
670 {
671         struct mlx5_flow_table *ft;
672         int ret;
673
674         ft  = kzalloc(sizeof(*ft), GFP_KERNEL);
675         if (!ft)
676                 return ERR_PTR(-ENOMEM);
677
678         ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
679         if (ret) {
680                 kfree(ft);
681                 return ERR_PTR(ret);
682         }
683
684         ft->level = level;
685         ft->node.type = FS_TYPE_FLOW_TABLE;
686         ft->op_mod = op_mod;
687         ft->type = table_type;
688         ft->vport = vport;
689         ft->max_fte = max_fte;
690         ft->flags = flags;
691         INIT_LIST_HEAD(&ft->fwd_rules);
692         mutex_init(&ft->lock);
693
694         return ft;
695 }
696
697 /* If reverse is false, then we search for the first flow table in the
698  * root sub-tree from start(closest from right), else we search for the
699  * last flow table in the root sub-tree till start(closest from left).
700  */
701 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node  *root,
702                                                          struct list_head *start,
703                                                          bool reverse)
704 {
705 #define list_advance_entry(pos, reverse)                \
706         ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
707
708 #define list_for_each_advance_continue(pos, head, reverse)      \
709         for (pos = list_advance_entry(pos, reverse);            \
710              &pos->list != (head);                              \
711              pos = list_advance_entry(pos, reverse))
712
713         struct fs_node *iter = list_entry(start, struct fs_node, list);
714         struct mlx5_flow_table *ft = NULL;
715
716         if (!root)
717                 return NULL;
718
719         list_for_each_advance_continue(iter, &root->children, reverse) {
720                 if (iter->type == FS_TYPE_FLOW_TABLE) {
721                         fs_get_obj(ft, iter);
722                         return ft;
723                 }
724                 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
725                 if (ft)
726                         return ft;
727         }
728
729         return ft;
730 }
731
732 /* If reverse if false then return the first flow table in next priority of
733  * prio in the tree, else return the last flow table in the previous priority
734  * of prio in the tree.
735  */
736 static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
737 {
738         struct mlx5_flow_table *ft = NULL;
739         struct fs_node *curr_node;
740         struct fs_node *parent;
741
742         parent = prio->node.parent;
743         curr_node = &prio->node;
744         while (!ft && parent) {
745                 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
746                 curr_node = parent;
747                 parent = curr_node->parent;
748         }
749         return ft;
750 }
751
752 /* Assuming all the tree is locked by mutex chain lock */
753 static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
754 {
755         return find_closest_ft(prio, false);
756 }
757
758 /* Assuming all the tree is locked by mutex chain lock */
759 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
760 {
761         return find_closest_ft(prio, true);
762 }
763
764 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
765                                struct fs_prio *prio,
766                                struct mlx5_flow_table *ft)
767 {
768         struct mlx5_flow_root_namespace *root = find_root(&prio->node);
769         struct mlx5_flow_table *iter;
770         int i = 0;
771         int err;
772
773         fs_for_each_ft(iter, prio) {
774                 i++;
775                 err = root->cmds->modify_flow_table(dev, iter, ft);
776                 if (err) {
777                         mlx5_core_warn(dev, "Failed to modify flow table %d\n",
778                                        iter->id);
779                         /* The driver is out of sync with the FW */
780                         if (i > 1)
781                                 WARN_ON(true);
782                         return err;
783                 }
784         }
785         return 0;
786 }
787
788 /* Connect flow tables from previous priority of prio to ft */
789 static int connect_prev_fts(struct mlx5_core_dev *dev,
790                             struct mlx5_flow_table *ft,
791                             struct fs_prio *prio)
792 {
793         struct mlx5_flow_table *prev_ft;
794
795         prev_ft = find_prev_chained_ft(prio);
796         if (prev_ft) {
797                 struct fs_prio *prev_prio;
798
799                 fs_get_obj(prev_prio, prev_ft->node.parent);
800                 return connect_fts_in_prio(dev, prev_prio, ft);
801         }
802         return 0;
803 }
804
805 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
806                                  *prio)
807 {
808         struct mlx5_flow_root_namespace *root = find_root(&prio->node);
809         struct mlx5_ft_underlay_qp *uqp;
810         int min_level = INT_MAX;
811         int err;
812         u32 qpn;
813
814         if (root->root_ft)
815                 min_level = root->root_ft->level;
816
817         if (ft->level >= min_level)
818                 return 0;
819
820         if (list_empty(&root->underlay_qpns)) {
821                 /* Don't set any QPN (zero) in case QPN list is empty */
822                 qpn = 0;
823                 err = root->cmds->update_root_ft(root->dev, ft, qpn, false);
824         } else {
825                 list_for_each_entry(uqp, &root->underlay_qpns, list) {
826                         qpn = uqp->qpn;
827                         err = root->cmds->update_root_ft(root->dev, ft,
828                                                          qpn, false);
829                         if (err)
830                                 break;
831                 }
832         }
833
834         if (err)
835                 mlx5_core_warn(root->dev,
836                                "Update root flow table of id(%u) qpn(%d) failed\n",
837                                ft->id, qpn);
838         else
839                 root->root_ft = ft;
840
841         return err;
842 }
843
844 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
845                                          struct mlx5_flow_destination *dest)
846 {
847         struct mlx5_flow_root_namespace *root;
848         struct mlx5_flow_table *ft;
849         struct mlx5_flow_group *fg;
850         struct fs_fte *fte;
851         int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
852         int err = 0;
853
854         fs_get_obj(fte, rule->node.parent);
855         if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
856                 return -EINVAL;
857         down_write_ref_node(&fte->node);
858         fs_get_obj(fg, fte->node.parent);
859         fs_get_obj(ft, fg->node.parent);
860
861         memcpy(&rule->dest_attr, dest, sizeof(*dest));
862         root = find_root(&ft->node);
863         err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
864                                      modify_mask, fte);
865         up_write_ref_node(&fte->node);
866
867         return err;
868 }
869
870 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
871                                  struct mlx5_flow_destination *new_dest,
872                                  struct mlx5_flow_destination *old_dest)
873 {
874         int i;
875
876         if (!old_dest) {
877                 if (handle->num_rules != 1)
878                         return -EINVAL;
879                 return _mlx5_modify_rule_destination(handle->rule[0],
880                                                      new_dest);
881         }
882
883         for (i = 0; i < handle->num_rules; i++) {
884                 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
885                         return _mlx5_modify_rule_destination(handle->rule[i],
886                                                              new_dest);
887         }
888
889         return -EINVAL;
890 }
891
892 /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft  */
893 static int connect_fwd_rules(struct mlx5_core_dev *dev,
894                              struct mlx5_flow_table *new_next_ft,
895                              struct mlx5_flow_table *old_next_ft)
896 {
897         struct mlx5_flow_destination dest = {};
898         struct mlx5_flow_rule *iter;
899         int err = 0;
900
901         /* new_next_ft and old_next_ft could be NULL only
902          * when we create/destroy the anchor flow table.
903          */
904         if (!new_next_ft || !old_next_ft)
905                 return 0;
906
907         dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
908         dest.ft = new_next_ft;
909
910         mutex_lock(&old_next_ft->lock);
911         list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
912         mutex_unlock(&old_next_ft->lock);
913         list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
914                 err = _mlx5_modify_rule_destination(iter, &dest);
915                 if (err)
916                         pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
917                                new_next_ft->id);
918         }
919         return 0;
920 }
921
922 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
923                               struct fs_prio *prio)
924 {
925         struct mlx5_flow_table *next_ft;
926         int err = 0;
927
928         /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
929
930         if (list_empty(&prio->node.children)) {
931                 err = connect_prev_fts(dev, ft, prio);
932                 if (err)
933                         return err;
934
935                 next_ft = find_next_chained_ft(prio);
936                 err = connect_fwd_rules(dev, ft, next_ft);
937                 if (err)
938                         return err;
939         }
940
941         if (MLX5_CAP_FLOWTABLE(dev,
942                                flow_table_properties_nic_receive.modify_root))
943                 err = update_root_ft_create(ft, prio);
944         return err;
945 }
946
947 static void list_add_flow_table(struct mlx5_flow_table *ft,
948                                 struct fs_prio *prio)
949 {
950         struct list_head *prev = &prio->node.children;
951         struct mlx5_flow_table *iter;
952
953         fs_for_each_ft(iter, prio) {
954                 if (iter->level > ft->level)
955                         break;
956                 prev = &iter->node.list;
957         }
958         list_add(&ft->node.list, prev);
959 }
960
961 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
962                                                         struct mlx5_flow_table_attr *ft_attr,
963                                                         enum fs_flow_table_op_mod op_mod,
964                                                         u16 vport)
965 {
966         struct mlx5_flow_root_namespace *root = find_root(&ns->node);
967         struct mlx5_flow_table *next_ft = NULL;
968         struct fs_prio *fs_prio = NULL;
969         struct mlx5_flow_table *ft;
970         int log_table_sz;
971         int err;
972
973         if (!root) {
974                 pr_err("mlx5: flow steering failed to find root of namespace\n");
975                 return ERR_PTR(-ENODEV);
976         }
977
978         mutex_lock(&root->chain_lock);
979         fs_prio = find_prio(ns, ft_attr->prio);
980         if (!fs_prio) {
981                 err = -EINVAL;
982                 goto unlock_root;
983         }
984         if (ft_attr->level >= fs_prio->num_levels) {
985                 err = -ENOSPC;
986                 goto unlock_root;
987         }
988         /* The level is related to the
989          * priority level range.
990          */
991         ft_attr->level += fs_prio->start_level;
992         ft = alloc_flow_table(ft_attr->level,
993                               vport,
994                               ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
995                               root->table_type,
996                               op_mod, ft_attr->flags);
997         if (IS_ERR(ft)) {
998                 err = PTR_ERR(ft);
999                 goto unlock_root;
1000         }
1001
1002         tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1003         log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
1004         next_ft = find_next_chained_ft(fs_prio);
1005         err = root->cmds->create_flow_table(root->dev, ft->vport, ft->op_mod,
1006                                             ft->type, ft->level, log_table_sz,
1007                                             next_ft, &ft->id, ft->flags);
1008         if (err)
1009                 goto free_ft;
1010
1011         err = connect_flow_table(root->dev, ft, fs_prio);
1012         if (err)
1013                 goto destroy_ft;
1014         ft->node.active = true;
1015         down_write_ref_node(&fs_prio->node);
1016         tree_add_node(&ft->node, &fs_prio->node);
1017         list_add_flow_table(ft, fs_prio);
1018         fs_prio->num_ft++;
1019         up_write_ref_node(&fs_prio->node);
1020         mutex_unlock(&root->chain_lock);
1021         return ft;
1022 destroy_ft:
1023         root->cmds->destroy_flow_table(root->dev, ft);
1024 free_ft:
1025         kfree(ft);
1026 unlock_root:
1027         mutex_unlock(&root->chain_lock);
1028         return ERR_PTR(err);
1029 }
1030
1031 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1032                                                struct mlx5_flow_table_attr *ft_attr)
1033 {
1034         return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1035 }
1036
1037 struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1038                                                      int prio, int max_fte,
1039                                                      u32 level, u16 vport)
1040 {
1041         struct mlx5_flow_table_attr ft_attr = {};
1042
1043         ft_attr.max_fte = max_fte;
1044         ft_attr.level   = level;
1045         ft_attr.prio    = prio;
1046
1047         return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1048 }
1049
1050 struct mlx5_flow_table*
1051 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1052                                  int prio, u32 level)
1053 {
1054         struct mlx5_flow_table_attr ft_attr = {};
1055
1056         ft_attr.level = level;
1057         ft_attr.prio  = prio;
1058         return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1059 }
1060 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1061
1062 struct mlx5_flow_table*
1063 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1064                                     int prio,
1065                                     int num_flow_table_entries,
1066                                     int max_num_groups,
1067                                     u32 level,
1068                                     u32 flags)
1069 {
1070         struct mlx5_flow_table_attr ft_attr = {};
1071         struct mlx5_flow_table *ft;
1072
1073         if (max_num_groups > num_flow_table_entries)
1074                 return ERR_PTR(-EINVAL);
1075
1076         ft_attr.max_fte = num_flow_table_entries;
1077         ft_attr.prio    = prio;
1078         ft_attr.level   = level;
1079         ft_attr.flags   = flags;
1080
1081         ft = mlx5_create_flow_table(ns, &ft_attr);
1082         if (IS_ERR(ft))
1083                 return ft;
1084
1085         ft->autogroup.active = true;
1086         ft->autogroup.required_groups = max_num_groups;
1087
1088         return ft;
1089 }
1090 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1091
1092 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1093                                                u32 *fg_in)
1094 {
1095         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1096         void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1097                                             fg_in, match_criteria);
1098         u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1099                                             fg_in,
1100                                             match_criteria_enable);
1101         int start_index = MLX5_GET(create_flow_group_in, fg_in,
1102                                    start_flow_index);
1103         int end_index = MLX5_GET(create_flow_group_in, fg_in,
1104                                  end_flow_index);
1105         struct mlx5_core_dev *dev = get_dev(&ft->node);
1106         struct mlx5_flow_group *fg;
1107         int err;
1108
1109         if (ft->autogroup.active)
1110                 return ERR_PTR(-EPERM);
1111
1112         down_write_ref_node(&ft->node);
1113         fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1114                                      start_index, end_index,
1115                                      ft->node.children.prev);
1116         up_write_ref_node(&ft->node);
1117         if (IS_ERR(fg))
1118                 return fg;
1119
1120         err = root->cmds->create_flow_group(dev, ft, fg_in, &fg->id);
1121         if (err) {
1122                 tree_put_node(&fg->node);
1123                 return ERR_PTR(err);
1124         }
1125         trace_mlx5_fs_add_fg(fg);
1126         fg->node.active = true;
1127
1128         return fg;
1129 }
1130
1131 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1132 {
1133         struct mlx5_flow_rule *rule;
1134
1135         rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1136         if (!rule)
1137                 return NULL;
1138
1139         INIT_LIST_HEAD(&rule->next_ft);
1140         rule->node.type = FS_TYPE_FLOW_DEST;
1141         if (dest)
1142                 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1143
1144         return rule;
1145 }
1146
1147 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1148 {
1149         struct mlx5_flow_handle *handle;
1150
1151         handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
1152         if (!handle)
1153                 return NULL;
1154
1155         handle->num_rules = num_rules;
1156
1157         return handle;
1158 }
1159
1160 static void destroy_flow_handle(struct fs_fte *fte,
1161                                 struct mlx5_flow_handle *handle,
1162                                 struct mlx5_flow_destination *dest,
1163                                 int i)
1164 {
1165         for (; --i >= 0;) {
1166                 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1167                         fte->dests_size--;
1168                         list_del(&handle->rule[i]->node.list);
1169                         kfree(handle->rule[i]);
1170                 }
1171         }
1172         kfree(handle);
1173 }
1174
1175 static struct mlx5_flow_handle *
1176 create_flow_handle(struct fs_fte *fte,
1177                    struct mlx5_flow_destination *dest,
1178                    int dest_num,
1179                    int *modify_mask,
1180                    bool *new_rule)
1181 {
1182         struct mlx5_flow_handle *handle;
1183         struct mlx5_flow_rule *rule = NULL;
1184         static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1185         static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1186         int type;
1187         int i = 0;
1188
1189         handle = alloc_handle((dest_num) ? dest_num : 1);
1190         if (!handle)
1191                 return ERR_PTR(-ENOMEM);
1192
1193         do {
1194                 if (dest) {
1195                         rule = find_flow_rule(fte, dest + i);
1196                         if (rule) {
1197                                 refcount_inc(&rule->node.refcount);
1198                                 goto rule_found;
1199                         }
1200                 }
1201
1202                 *new_rule = true;
1203                 rule = alloc_rule(dest + i);
1204                 if (!rule)
1205                         goto free_rules;
1206
1207                 /* Add dest to dests list- we need flow tables to be in the
1208                  * end of the list for forward to next prio rules.
1209                  */
1210                 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1211                 if (dest &&
1212                     dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1213                         list_add(&rule->node.list, &fte->node.children);
1214                 else
1215                         list_add_tail(&rule->node.list, &fte->node.children);
1216                 if (dest) {
1217                         fte->dests_size++;
1218
1219                         type = dest[i].type ==
1220                                 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1221                         *modify_mask |= type ? count : dst;
1222                 }
1223 rule_found:
1224                 handle->rule[i] = rule;
1225         } while (++i < dest_num);
1226
1227         return handle;
1228
1229 free_rules:
1230         destroy_flow_handle(fte, handle, dest, i);
1231         return ERR_PTR(-ENOMEM);
1232 }
1233
1234 /* fte should not be deleted while calling this function */
1235 static struct mlx5_flow_handle *
1236 add_rule_fte(struct fs_fte *fte,
1237              struct mlx5_flow_group *fg,
1238              struct mlx5_flow_destination *dest,
1239              int dest_num,
1240              bool update_action)
1241 {
1242         struct mlx5_flow_root_namespace *root;
1243         struct mlx5_flow_handle *handle;
1244         struct mlx5_flow_table *ft;
1245         int modify_mask = 0;
1246         int err;
1247         bool new_rule = false;
1248
1249         handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1250                                     &new_rule);
1251         if (IS_ERR(handle) || !new_rule)
1252                 goto out;
1253
1254         if (update_action)
1255                 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1256
1257         fs_get_obj(ft, fg->node.parent);
1258         root = find_root(&fg->node);
1259         if (!(fte->status & FS_FTE_STATUS_EXISTING))
1260                 err = root->cmds->create_fte(get_dev(&ft->node),
1261                                              ft, fg, fte);
1262         else
1263                 err = root->cmds->update_fte(get_dev(&ft->node), ft, fg->id,
1264                                                      modify_mask, fte);
1265         if (err)
1266                 goto free_handle;
1267
1268         fte->node.active = true;
1269         fte->status |= FS_FTE_STATUS_EXISTING;
1270         atomic_inc(&fte->node.version);
1271
1272 out:
1273         return handle;
1274
1275 free_handle:
1276         destroy_flow_handle(fte, handle, dest, handle->num_rules);
1277         return ERR_PTR(err);
1278 }
1279
1280 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table  *ft,
1281                                                      struct mlx5_flow_spec *spec)
1282 {
1283         struct list_head *prev = &ft->node.children;
1284         struct mlx5_flow_group *fg;
1285         unsigned int candidate_index = 0;
1286         unsigned int group_size = 0;
1287
1288         if (!ft->autogroup.active)
1289                 return ERR_PTR(-ENOENT);
1290
1291         if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1292                 /* We save place for flow groups in addition to max types */
1293                 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
1294
1295         /*  ft->max_fte == ft->autogroup.max_types */
1296         if (group_size == 0)
1297                 group_size = 1;
1298
1299         /* sorted by start_index */
1300         fs_for_each_fg(fg, ft) {
1301                 if (candidate_index + group_size > fg->start_index)
1302                         candidate_index = fg->start_index + fg->max_ftes;
1303                 else
1304                         break;
1305                 prev = &fg->node.list;
1306         }
1307
1308         if (candidate_index + group_size > ft->max_fte)
1309                 return ERR_PTR(-ENOSPC);
1310
1311         fg = alloc_insert_flow_group(ft,
1312                                      spec->match_criteria_enable,
1313                                      spec->match_criteria,
1314                                      candidate_index,
1315                                      candidate_index + group_size - 1,
1316                                      prev);
1317         if (IS_ERR(fg))
1318                 goto out;
1319
1320         ft->autogroup.num_groups++;
1321
1322 out:
1323         return fg;
1324 }
1325
1326 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1327                                   struct mlx5_flow_group *fg)
1328 {
1329         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1330         struct mlx5_core_dev *dev = get_dev(&ft->node);
1331         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1332         void *match_criteria_addr;
1333         u8 src_esw_owner_mask_on;
1334         void *misc;
1335         int err;
1336         u32 *in;
1337
1338         in = kvzalloc(inlen, GFP_KERNEL);
1339         if (!in)
1340                 return -ENOMEM;
1341
1342         MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1343                  fg->mask.match_criteria_enable);
1344         MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1345         MLX5_SET(create_flow_group_in, in, end_flow_index,   fg->start_index +
1346                  fg->max_ftes - 1);
1347
1348         misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1349                             misc_parameters);
1350         src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1351                                          source_eswitch_owner_vhca_id);
1352         MLX5_SET(create_flow_group_in, in,
1353                  source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1354
1355         match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1356                                            in, match_criteria);
1357         memcpy(match_criteria_addr, fg->mask.match_criteria,
1358                sizeof(fg->mask.match_criteria));
1359
1360         err = root->cmds->create_flow_group(dev, ft, in, &fg->id);
1361         if (!err) {
1362                 fg->node.active = true;
1363                 trace_mlx5_fs_add_fg(fg);
1364         }
1365
1366         kvfree(in);
1367         return err;
1368 }
1369
1370 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1371                                 struct mlx5_flow_destination *d2)
1372 {
1373         if (d1->type == d2->type) {
1374                 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1375                      d1->vport.num == d2->vport.num) ||
1376                     (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1377                      d1->ft == d2->ft) ||
1378                     (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1379                      d1->tir_num == d2->tir_num) ||
1380                     (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1381                      d1->ft_num == d2->ft_num))
1382                         return true;
1383         }
1384
1385         return false;
1386 }
1387
1388 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1389                                              struct mlx5_flow_destination *dest)
1390 {
1391         struct mlx5_flow_rule *rule;
1392
1393         list_for_each_entry(rule, &fte->node.children, node.list) {
1394                 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1395                         return rule;
1396         }
1397         return NULL;
1398 }
1399
1400 static bool check_conflicting_actions(u32 action1, u32 action2)
1401 {
1402         u32 xored_actions = action1 ^ action2;
1403
1404         /* if one rule only wants to count, it's ok */
1405         if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1406             action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1407                 return false;
1408
1409         if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP  |
1410                              MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
1411                              MLX5_FLOW_CONTEXT_ACTION_DECAP |
1412                              MLX5_FLOW_CONTEXT_ACTION_MOD_HDR  |
1413                              MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1414                              MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1415                              MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1416                              MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
1417                 return true;
1418
1419         return false;
1420 }
1421
1422 static int check_conflicting_ftes(struct fs_fte *fte, const struct mlx5_flow_act *flow_act)
1423 {
1424         if (check_conflicting_actions(flow_act->action, fte->action.action)) {
1425                 mlx5_core_warn(get_dev(&fte->node),
1426                                "Found two FTEs with conflicting actions\n");
1427                 return -EEXIST;
1428         }
1429
1430         if (flow_act->has_flow_tag &&
1431             fte->action.flow_tag != flow_act->flow_tag) {
1432                 mlx5_core_warn(get_dev(&fte->node),
1433                                "FTE flow tag %u already exists with different flow tag %u\n",
1434                                fte->action.flow_tag,
1435                                flow_act->flow_tag);
1436                 return -EEXIST;
1437         }
1438
1439         return 0;
1440 }
1441
1442 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1443                                             u32 *match_value,
1444                                             struct mlx5_flow_act *flow_act,
1445                                             struct mlx5_flow_destination *dest,
1446                                             int dest_num,
1447                                             struct fs_fte *fte)
1448 {
1449         struct mlx5_flow_handle *handle;
1450         int old_action;
1451         int i;
1452         int ret;
1453
1454         ret = check_conflicting_ftes(fte, flow_act);
1455         if (ret)
1456                 return ERR_PTR(ret);
1457
1458         old_action = fte->action.action;
1459         fte->action.action |= flow_act->action;
1460         handle = add_rule_fte(fte, fg, dest, dest_num,
1461                               old_action != flow_act->action);
1462         if (IS_ERR(handle)) {
1463                 fte->action.action = old_action;
1464                 return handle;
1465         }
1466         trace_mlx5_fs_set_fte(fte, false);
1467
1468         for (i = 0; i < handle->num_rules; i++) {
1469                 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1470                         tree_add_node(&handle->rule[i]->node, &fte->node);
1471                         trace_mlx5_fs_add_rule(handle->rule[i]);
1472                 }
1473         }
1474         return handle;
1475 }
1476
1477 struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_handle *handle)
1478 {
1479         struct mlx5_flow_rule *dst;
1480         struct fs_fte *fte;
1481
1482         fs_get_obj(fte, handle->rule[0]->node.parent);
1483
1484         fs_for_each_dst(dst, fte) {
1485                 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
1486                         return dst->dest_attr.counter;
1487         }
1488
1489         return NULL;
1490 }
1491
1492 static bool counter_is_valid(struct mlx5_fc *counter, u32 action)
1493 {
1494         if (!(action & MLX5_FLOW_CONTEXT_ACTION_COUNT))
1495                 return !counter;
1496
1497         if (!counter)
1498                 return false;
1499
1500         return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1501                           MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1502 }
1503
1504 static bool dest_is_valid(struct mlx5_flow_destination *dest,
1505                           u32 action,
1506                           struct mlx5_flow_table *ft)
1507 {
1508         if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1509                 return counter_is_valid(dest->counter, action);
1510
1511         if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1512                 return true;
1513
1514         if (!dest || ((dest->type ==
1515             MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1516             (dest->ft->level <= ft->level)))
1517                 return false;
1518         return true;
1519 }
1520
1521 struct match_list {
1522         struct list_head        list;
1523         struct mlx5_flow_group *g;
1524 };
1525
1526 struct match_list_head {
1527         struct list_head  list;
1528         struct match_list first;
1529 };
1530
1531 static void free_match_list(struct match_list_head *head)
1532 {
1533         if (!list_empty(&head->list)) {
1534                 struct match_list *iter, *match_tmp;
1535
1536                 list_del(&head->first.list);
1537                 tree_put_node(&head->first.g->node);
1538                 list_for_each_entry_safe(iter, match_tmp, &head->list,
1539                                          list) {
1540                         tree_put_node(&iter->g->node);
1541                         list_del(&iter->list);
1542                         kfree(iter);
1543                 }
1544         }
1545 }
1546
1547 static int build_match_list(struct match_list_head *match_head,
1548                             struct mlx5_flow_table *ft,
1549                             struct mlx5_flow_spec *spec)
1550 {
1551         struct rhlist_head *tmp, *list;
1552         struct mlx5_flow_group *g;
1553         int err = 0;
1554
1555         rcu_read_lock();
1556         INIT_LIST_HEAD(&match_head->list);
1557         /* Collect all fgs which has a matching match_criteria */
1558         list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1559         /* RCU is atomic, we can't execute FW commands here */
1560         rhl_for_each_entry_rcu(g, tmp, list, hash) {
1561                 struct match_list *curr_match;
1562
1563                 if (likely(list_empty(&match_head->list))) {
1564                         if (!tree_get_node(&g->node))
1565                                 continue;
1566                         match_head->first.g = g;
1567                         list_add_tail(&match_head->first.list,
1568                                       &match_head->list);
1569                         continue;
1570                 }
1571
1572                 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1573                 if (!curr_match) {
1574                         free_match_list(match_head);
1575                         err = -ENOMEM;
1576                         goto out;
1577                 }
1578                 if (!tree_get_node(&g->node)) {
1579                         kfree(curr_match);
1580                         continue;
1581                 }
1582                 curr_match->g = g;
1583                 list_add_tail(&curr_match->list, &match_head->list);
1584         }
1585 out:
1586         rcu_read_unlock();
1587         return err;
1588 }
1589
1590 static u64 matched_fgs_get_version(struct list_head *match_head)
1591 {
1592         struct match_list *iter;
1593         u64 version = 0;
1594
1595         list_for_each_entry(iter, match_head, list)
1596                 version += (u64)atomic_read(&iter->g->node.version);
1597         return version;
1598 }
1599
1600 static struct fs_fte *
1601 lookup_fte_locked(struct mlx5_flow_group *g,
1602                   u32 *match_value,
1603                   bool take_write)
1604 {
1605         struct fs_fte *fte_tmp;
1606
1607         if (take_write)
1608                 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1609         else
1610                 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
1611         fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
1612                                          rhash_fte);
1613         if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1614                 fte_tmp = NULL;
1615                 goto out;
1616         }
1617
1618         nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1619 out:
1620         if (take_write)
1621                 up_write_ref_node(&g->node);
1622         else
1623                 up_read_ref_node(&g->node);
1624         return fte_tmp;
1625 }
1626
1627 static struct mlx5_flow_handle *
1628 try_add_to_existing_fg(struct mlx5_flow_table *ft,
1629                        struct list_head *match_head,
1630                        struct mlx5_flow_spec *spec,
1631                        struct mlx5_flow_act *flow_act,
1632                        struct mlx5_flow_destination *dest,
1633                        int dest_num,
1634                        int ft_version)
1635 {
1636         struct mlx5_flow_steering *steering = get_steering(&ft->node);
1637         struct mlx5_flow_group *g;
1638         struct mlx5_flow_handle *rule;
1639         struct match_list *iter;
1640         bool take_write = false;
1641         struct fs_fte *fte;
1642         u64  version;
1643         int err;
1644
1645         fte = alloc_fte(ft, spec->match_value, flow_act);
1646         if (IS_ERR(fte))
1647                 return  ERR_PTR(-ENOMEM);
1648
1649 search_again_locked:
1650         version = matched_fgs_get_version(match_head);
1651         /* Try to find a fg that already contains a matching fte */
1652         list_for_each_entry(iter, match_head, list) {
1653                 struct fs_fte *fte_tmp;
1654
1655                 g = iter->g;
1656                 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1657                 if (!fte_tmp)
1658                         continue;
1659                 rule = add_rule_fg(g, spec->match_value,
1660                                    flow_act, dest, dest_num, fte_tmp);
1661                 up_write_ref_node(&fte_tmp->node);
1662                 tree_put_node(&fte_tmp->node);
1663                 kmem_cache_free(steering->ftes_cache, fte);
1664                 return rule;
1665         }
1666
1667         /* Check the ft version, for case that new flow group
1668          * was added while the fgs weren't locked
1669          */
1670         if (atomic_read(&ft->node.version) != ft_version) {
1671                 rule = ERR_PTR(-EAGAIN);
1672                 goto out;
1673         }
1674
1675         /* Check the fgs version, for case the new FTE with the
1676          * same values was added while the fgs weren't locked
1677          */
1678         if (version != matched_fgs_get_version(match_head)) {
1679                 take_write = true;
1680                 goto search_again_locked;
1681         }
1682
1683         list_for_each_entry(iter, match_head, list) {
1684                 g = iter->g;
1685
1686                 if (!g->node.active)
1687                         continue;
1688
1689                 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1690
1691                 err = insert_fte(g, fte);
1692                 if (err) {
1693                         up_write_ref_node(&g->node);
1694                         if (err == -ENOSPC)
1695                                 continue;
1696                         kmem_cache_free(steering->ftes_cache, fte);
1697                         return ERR_PTR(err);
1698                 }
1699
1700                 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1701                 up_write_ref_node(&g->node);
1702                 rule = add_rule_fg(g, spec->match_value,
1703                                    flow_act, dest, dest_num, fte);
1704                 up_write_ref_node(&fte->node);
1705                 tree_put_node(&fte->node);
1706                 return rule;
1707         }
1708         rule = ERR_PTR(-ENOENT);
1709 out:
1710         kmem_cache_free(steering->ftes_cache, fte);
1711         return rule;
1712 }
1713
1714 static struct mlx5_flow_handle *
1715 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1716                      struct mlx5_flow_spec *spec,
1717                      struct mlx5_flow_act *flow_act,
1718                      struct mlx5_flow_destination *dest,
1719                      int dest_num)
1720
1721 {
1722         struct mlx5_flow_steering *steering = get_steering(&ft->node);
1723         struct mlx5_flow_group *g;
1724         struct mlx5_flow_handle *rule;
1725         struct match_list_head match_head;
1726         bool take_write = false;
1727         struct fs_fte *fte;
1728         int version;
1729         int err;
1730         int i;
1731
1732         if (!check_valid_spec(spec))
1733                 return ERR_PTR(-EINVAL);
1734
1735         for (i = 0; i < dest_num; i++) {
1736                 if (!dest_is_valid(&dest[i], flow_act->action, ft))
1737                         return ERR_PTR(-EINVAL);
1738         }
1739         nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1740 search_again_locked:
1741         version = atomic_read(&ft->node.version);
1742
1743         /* Collect all fgs which has a matching match_criteria */
1744         err = build_match_list(&match_head, ft, spec);
1745         if (err) {
1746                 if (take_write)
1747                         up_write_ref_node(&ft->node);
1748                 else
1749                         up_read_ref_node(&ft->node);
1750                 return ERR_PTR(err);
1751         }
1752
1753         if (!take_write)
1754                 up_read_ref_node(&ft->node);
1755
1756         rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1757                                       dest_num, version);
1758         free_match_list(&match_head);
1759         if (!IS_ERR(rule) ||
1760             (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1761                 if (take_write)
1762                         up_write_ref_node(&ft->node);
1763                 return rule;
1764         }
1765
1766         if (!take_write) {
1767                 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1768                 take_write = true;
1769         }
1770
1771         if (PTR_ERR(rule) == -EAGAIN ||
1772             version != atomic_read(&ft->node.version))
1773                 goto search_again_locked;
1774
1775         g = alloc_auto_flow_group(ft, spec);
1776         if (IS_ERR(g)) {
1777                 rule = ERR_CAST(g);
1778                 up_write_ref_node(&ft->node);
1779                 return rule;
1780         }
1781
1782         nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1783         up_write_ref_node(&ft->node);
1784
1785         err = create_auto_flow_group(ft, g);
1786         if (err)
1787                 goto err_release_fg;
1788
1789         fte = alloc_fte(ft, spec->match_value, flow_act);
1790         if (IS_ERR(fte)) {
1791                 err = PTR_ERR(fte);
1792                 goto err_release_fg;
1793         }
1794
1795         err = insert_fte(g, fte);
1796         if (err) {
1797                 kmem_cache_free(steering->ftes_cache, fte);
1798                 goto err_release_fg;
1799         }
1800
1801         nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1802         up_write_ref_node(&g->node);
1803         rule = add_rule_fg(g, spec->match_value, flow_act, dest,
1804                            dest_num, fte);
1805         up_write_ref_node(&fte->node);
1806         tree_put_node(&fte->node);
1807         tree_put_node(&g->node);
1808         return rule;
1809
1810 err_release_fg:
1811         up_write_ref_node(&g->node);
1812         tree_put_node(&g->node);
1813         return ERR_PTR(err);
1814 }
1815
1816 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1817 {
1818         return ((ft->type == FS_FT_NIC_RX) &&
1819                 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1820 }
1821
1822 struct mlx5_flow_handle *
1823 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1824                     struct mlx5_flow_spec *spec,
1825                     struct mlx5_flow_act *flow_act,
1826                     struct mlx5_flow_destination *dest,
1827                     int num_dest)
1828 {
1829         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1830         struct mlx5_flow_destination gen_dest = {};
1831         struct mlx5_flow_table *next_ft = NULL;
1832         struct mlx5_flow_handle *handle = NULL;
1833         u32 sw_action = flow_act->action;
1834         struct fs_prio *prio;
1835
1836         fs_get_obj(prio, ft->node.parent);
1837         if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1838                 if (!fwd_next_prio_supported(ft))
1839                         return ERR_PTR(-EOPNOTSUPP);
1840                 if (num_dest)
1841                         return ERR_PTR(-EINVAL);
1842                 mutex_lock(&root->chain_lock);
1843                 next_ft = find_next_chained_ft(prio);
1844                 if (next_ft) {
1845                         gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1846                         gen_dest.ft = next_ft;
1847                         dest = &gen_dest;
1848                         num_dest = 1;
1849                         flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1850                 } else {
1851                         mutex_unlock(&root->chain_lock);
1852                         return ERR_PTR(-EOPNOTSUPP);
1853                 }
1854         }
1855
1856         handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1857
1858         if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1859                 if (!IS_ERR_OR_NULL(handle) &&
1860                     (list_empty(&handle->rule[0]->next_ft))) {
1861                         mutex_lock(&next_ft->lock);
1862                         list_add(&handle->rule[0]->next_ft,
1863                                  &next_ft->fwd_rules);
1864                         mutex_unlock(&next_ft->lock);
1865                         handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1866                 }
1867                 mutex_unlock(&root->chain_lock);
1868         }
1869         return handle;
1870 }
1871 EXPORT_SYMBOL(mlx5_add_flow_rules);
1872
1873 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1874 {
1875         int i;
1876
1877         for (i = handle->num_rules - 1; i >= 0; i--)
1878                 tree_remove_node(&handle->rule[i]->node);
1879         kfree(handle);
1880 }
1881 EXPORT_SYMBOL(mlx5_del_flow_rules);
1882
1883 /* Assuming prio->node.children(flow tables) is sorted by level */
1884 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1885 {
1886         struct fs_prio *prio;
1887
1888         fs_get_obj(prio, ft->node.parent);
1889
1890         if (!list_is_last(&ft->node.list, &prio->node.children))
1891                 return list_next_entry(ft, node.list);
1892         return find_next_chained_ft(prio);
1893 }
1894
1895 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1896 {
1897         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1898         struct mlx5_ft_underlay_qp *uqp;
1899         struct mlx5_flow_table *new_root_ft = NULL;
1900         int err = 0;
1901         u32 qpn;
1902
1903         if (root->root_ft != ft)
1904                 return 0;
1905
1906         new_root_ft = find_next_ft(ft);
1907         if (!new_root_ft) {
1908                 root->root_ft = NULL;
1909                 return 0;
1910         }
1911
1912         if (list_empty(&root->underlay_qpns)) {
1913                 /* Don't set any QPN (zero) in case QPN list is empty */
1914                 qpn = 0;
1915                 err = root->cmds->update_root_ft(root->dev, new_root_ft,
1916                                                  qpn, false);
1917         } else {
1918                 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1919                         qpn = uqp->qpn;
1920                         err = root->cmds->update_root_ft(root->dev,
1921                                                          new_root_ft, qpn,
1922                                                          false);
1923                         if (err)
1924                                 break;
1925                 }
1926         }
1927
1928         if (err)
1929                 mlx5_core_warn(root->dev,
1930                                "Update root flow table of id(%u) qpn(%d) failed\n",
1931                                ft->id, qpn);
1932         else
1933                 root->root_ft = new_root_ft;
1934
1935         return 0;
1936 }
1937
1938 /* Connect flow table from previous priority to
1939  * the next flow table.
1940  */
1941 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1942 {
1943         struct mlx5_core_dev *dev = get_dev(&ft->node);
1944         struct mlx5_flow_table *next_ft;
1945         struct fs_prio *prio;
1946         int err = 0;
1947
1948         err = update_root_ft_destroy(ft);
1949         if (err)
1950                 return err;
1951
1952         fs_get_obj(prio, ft->node.parent);
1953         if  (!(list_first_entry(&prio->node.children,
1954                                 struct mlx5_flow_table,
1955                                 node.list) == ft))
1956                 return 0;
1957
1958         next_ft = find_next_chained_ft(prio);
1959         err = connect_fwd_rules(dev, next_ft, ft);
1960         if (err)
1961                 return err;
1962
1963         err = connect_prev_fts(dev, next_ft, prio);
1964         if (err)
1965                 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1966                                ft->id);
1967         return err;
1968 }
1969
1970 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1971 {
1972         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1973         int err = 0;
1974
1975         mutex_lock(&root->chain_lock);
1976         err = disconnect_flow_table(ft);
1977         if (err) {
1978                 mutex_unlock(&root->chain_lock);
1979                 return err;
1980         }
1981         if (tree_remove_node(&ft->node))
1982                 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1983                                ft->id);
1984         mutex_unlock(&root->chain_lock);
1985
1986         return err;
1987 }
1988 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1989
1990 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1991 {
1992         if (tree_remove_node(&fg->node))
1993                 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1994                                fg->id);
1995 }
1996
1997 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1998                                                     enum mlx5_flow_namespace_type type)
1999 {
2000         struct mlx5_flow_steering *steering = dev->priv.steering;
2001         struct mlx5_flow_root_namespace *root_ns;
2002         int prio = 0;
2003         struct fs_prio *fs_prio;
2004         struct mlx5_flow_namespace *ns;
2005
2006         if (!steering)
2007                 return NULL;
2008
2009         switch (type) {
2010         case MLX5_FLOW_NAMESPACE_FDB:
2011                 if (steering->fdb_root_ns)
2012                         return &steering->fdb_root_ns->ns;
2013                 return NULL;
2014         case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2015                 if (steering->sniffer_rx_root_ns)
2016                         return &steering->sniffer_rx_root_ns->ns;
2017                 return NULL;
2018         case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2019                 if (steering->sniffer_tx_root_ns)
2020                         return &steering->sniffer_tx_root_ns->ns;
2021                 return NULL;
2022         default:
2023                 break;
2024         }
2025
2026         if (type == MLX5_FLOW_NAMESPACE_EGRESS) {
2027                 root_ns = steering->egress_root_ns;
2028         } else { /* Must be NIC RX */
2029                 root_ns = steering->root_ns;
2030                 prio = type;
2031         }
2032
2033         if (!root_ns)
2034                 return NULL;
2035
2036         fs_prio = find_prio(&root_ns->ns, prio);
2037         if (!fs_prio)
2038                 return NULL;
2039
2040         ns = list_first_entry(&fs_prio->node.children,
2041                               typeof(*ns),
2042                               node.list);
2043
2044         return ns;
2045 }
2046 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2047
2048 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2049                                                               enum mlx5_flow_namespace_type type,
2050                                                               int vport)
2051 {
2052         struct mlx5_flow_steering *steering = dev->priv.steering;
2053
2054         if (!steering || vport >= MLX5_TOTAL_VPORTS(dev))
2055                 return NULL;
2056
2057         switch (type) {
2058         case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2059                 if (steering->esw_egress_root_ns &&
2060                     steering->esw_egress_root_ns[vport])
2061                         return &steering->esw_egress_root_ns[vport]->ns;
2062                 else
2063                         return NULL;
2064         case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2065                 if (steering->esw_ingress_root_ns &&
2066                     steering->esw_ingress_root_ns[vport])
2067                         return &steering->esw_ingress_root_ns[vport]->ns;
2068                 else
2069                         return NULL;
2070         default:
2071                 return NULL;
2072         }
2073 }
2074
2075 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2076                                       unsigned int prio, int num_levels)
2077 {
2078         struct fs_prio *fs_prio;
2079
2080         fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2081         if (!fs_prio)
2082                 return ERR_PTR(-ENOMEM);
2083
2084         fs_prio->node.type = FS_TYPE_PRIO;
2085         tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2086         tree_add_node(&fs_prio->node, &ns->node);
2087         fs_prio->num_levels = num_levels;
2088         fs_prio->prio = prio;
2089         list_add_tail(&fs_prio->node.list, &ns->node.children);
2090
2091         return fs_prio;
2092 }
2093
2094 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2095                                                      *ns)
2096 {
2097         ns->node.type = FS_TYPE_NAMESPACE;
2098
2099         return ns;
2100 }
2101
2102 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2103 {
2104         struct mlx5_flow_namespace      *ns;
2105
2106         ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2107         if (!ns)
2108                 return ERR_PTR(-ENOMEM);
2109
2110         fs_init_namespace(ns);
2111         tree_init_node(&ns->node, NULL, del_sw_ns);
2112         tree_add_node(&ns->node, &prio->node);
2113         list_add_tail(&ns->node.list, &prio->node.children);
2114
2115         return ns;
2116 }
2117
2118 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2119                              struct init_tree_node *prio_metadata)
2120 {
2121         struct fs_prio *fs_prio;
2122         int i;
2123
2124         for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2125                 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2126                 if (IS_ERR(fs_prio))
2127                         return PTR_ERR(fs_prio);
2128         }
2129         return 0;
2130 }
2131
2132 #define FLOW_TABLE_BIT_SZ 1
2133 #define GET_FLOW_TABLE_CAP(dev, offset) \
2134         ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) +    \
2135                         offset / 32)) >>                                        \
2136           (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2137 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2138 {
2139         int i;
2140
2141         for (i = 0; i < caps->arr_sz; i++) {
2142                 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2143                         return false;
2144         }
2145         return true;
2146 }
2147
2148 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2149                                     struct init_tree_node *init_node,
2150                                     struct fs_node *fs_parent_node,
2151                                     struct init_tree_node *init_parent_node,
2152                                     int prio)
2153 {
2154         int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2155                                               flow_table_properties_nic_receive.
2156                                               max_ft_level);
2157         struct mlx5_flow_namespace *fs_ns;
2158         struct fs_prio *fs_prio;
2159         struct fs_node *base;
2160         int i;
2161         int err;
2162
2163         if (init_node->type == FS_TYPE_PRIO) {
2164                 if ((init_node->min_ft_level > max_ft_level) ||
2165                     !has_required_caps(steering->dev, &init_node->caps))
2166                         return 0;
2167
2168                 fs_get_obj(fs_ns, fs_parent_node);
2169                 if (init_node->num_leaf_prios)
2170                         return create_leaf_prios(fs_ns, prio, init_node);
2171                 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2172                 if (IS_ERR(fs_prio))
2173                         return PTR_ERR(fs_prio);
2174                 base = &fs_prio->node;
2175         } else if (init_node->type == FS_TYPE_NAMESPACE) {
2176                 fs_get_obj(fs_prio, fs_parent_node);
2177                 fs_ns = fs_create_namespace(fs_prio);
2178                 if (IS_ERR(fs_ns))
2179                         return PTR_ERR(fs_ns);
2180                 base = &fs_ns->node;
2181         } else {
2182                 return -EINVAL;
2183         }
2184         prio = 0;
2185         for (i = 0; i < init_node->ar_size; i++) {
2186                 err = init_root_tree_recursive(steering, &init_node->children[i],
2187                                                base, init_node, prio);
2188                 if (err)
2189                         return err;
2190                 if (init_node->children[i].type == FS_TYPE_PRIO &&
2191                     init_node->children[i].num_leaf_prios) {
2192                         prio += init_node->children[i].num_leaf_prios;
2193                 }
2194         }
2195
2196         return 0;
2197 }
2198
2199 static int init_root_tree(struct mlx5_flow_steering *steering,
2200                           struct init_tree_node *init_node,
2201                           struct fs_node *fs_parent_node)
2202 {
2203         int i;
2204         struct mlx5_flow_namespace *fs_ns;
2205         int err;
2206
2207         fs_get_obj(fs_ns, fs_parent_node);
2208         for (i = 0; i < init_node->ar_size; i++) {
2209                 err = init_root_tree_recursive(steering, &init_node->children[i],
2210                                                &fs_ns->node,
2211                                                init_node, i);
2212                 if (err)
2213                         return err;
2214         }
2215         return 0;
2216 }
2217
2218 static struct mlx5_flow_root_namespace
2219 *create_root_ns(struct mlx5_flow_steering *steering,
2220                 enum fs_flow_table_type table_type)
2221 {
2222         const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2223         struct mlx5_flow_root_namespace *root_ns;
2224         struct mlx5_flow_namespace *ns;
2225
2226         if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2227             (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2228                 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2229
2230         /* Create the root namespace */
2231         root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
2232         if (!root_ns)
2233                 return NULL;
2234
2235         root_ns->dev = steering->dev;
2236         root_ns->table_type = table_type;
2237         root_ns->cmds = cmds;
2238
2239         INIT_LIST_HEAD(&root_ns->underlay_qpns);
2240
2241         ns = &root_ns->ns;
2242         fs_init_namespace(ns);
2243         mutex_init(&root_ns->chain_lock);
2244         tree_init_node(&ns->node, NULL, NULL);
2245         tree_add_node(&ns->node, NULL);
2246
2247         return root_ns;
2248 }
2249
2250 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2251
2252 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2253 {
2254         struct fs_prio *prio;
2255
2256         fs_for_each_prio(prio, ns) {
2257                  /* This updates prio start_level and num_levels */
2258                 set_prio_attrs_in_prio(prio, acc_level);
2259                 acc_level += prio->num_levels;
2260         }
2261         return acc_level;
2262 }
2263
2264 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2265 {
2266         struct mlx5_flow_namespace *ns;
2267         int acc_level_ns = acc_level;
2268
2269         prio->start_level = acc_level;
2270         fs_for_each_ns(ns, prio)
2271                 /* This updates start_level and num_levels of ns's priority descendants */
2272                 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2273         if (!prio->num_levels)
2274                 prio->num_levels = acc_level_ns - prio->start_level;
2275         WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2276 }
2277
2278 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2279 {
2280         struct mlx5_flow_namespace *ns = &root_ns->ns;
2281         struct fs_prio *prio;
2282         int start_level = 0;
2283
2284         fs_for_each_prio(prio, ns) {
2285                 set_prio_attrs_in_prio(prio, start_level);
2286                 start_level += prio->num_levels;
2287         }
2288 }
2289
2290 #define ANCHOR_PRIO 0
2291 #define ANCHOR_SIZE 1
2292 #define ANCHOR_LEVEL 0
2293 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2294 {
2295         struct mlx5_flow_namespace *ns = NULL;
2296         struct mlx5_flow_table_attr ft_attr = {};
2297         struct mlx5_flow_table *ft;
2298
2299         ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2300         if (WARN_ON(!ns))
2301                 return -EINVAL;
2302
2303         ft_attr.max_fte = ANCHOR_SIZE;
2304         ft_attr.level   = ANCHOR_LEVEL;
2305         ft_attr.prio    = ANCHOR_PRIO;
2306
2307         ft = mlx5_create_flow_table(ns, &ft_attr);
2308         if (IS_ERR(ft)) {
2309                 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2310                 return PTR_ERR(ft);
2311         }
2312         return 0;
2313 }
2314
2315 static int init_root_ns(struct mlx5_flow_steering *steering)
2316 {
2317         int err;
2318
2319         steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2320         if (!steering->root_ns)
2321                 return -ENOMEM;
2322
2323         err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2324         if (err)
2325                 goto out_err;
2326
2327         set_prio_attrs(steering->root_ns);
2328         err = create_anchor_flow_table(steering);
2329         if (err)
2330                 goto out_err;
2331
2332         return 0;
2333
2334 out_err:
2335         cleanup_root_ns(steering->root_ns);
2336         steering->root_ns = NULL;
2337         return err;
2338 }
2339
2340 static void clean_tree(struct fs_node *node)
2341 {
2342         if (node) {
2343                 struct fs_node *iter;
2344                 struct fs_node *temp;
2345
2346                 tree_get_node(node);
2347                 list_for_each_entry_safe(iter, temp, &node->children, list)
2348                         clean_tree(iter);
2349                 tree_put_node(node);
2350                 tree_remove_node(node);
2351         }
2352 }
2353
2354 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2355 {
2356         if (!root_ns)
2357                 return;
2358
2359         clean_tree(&root_ns->ns.node);
2360 }
2361
2362 static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2363 {
2364         struct mlx5_flow_steering *steering = dev->priv.steering;
2365         int i;
2366
2367         if (!steering->esw_egress_root_ns)
2368                 return;
2369
2370         for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2371                 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2372
2373         kfree(steering->esw_egress_root_ns);
2374 }
2375
2376 static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2377 {
2378         struct mlx5_flow_steering *steering = dev->priv.steering;
2379         int i;
2380
2381         if (!steering->esw_ingress_root_ns)
2382                 return;
2383
2384         for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2385                 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2386
2387         kfree(steering->esw_ingress_root_ns);
2388 }
2389
2390 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2391 {
2392         struct mlx5_flow_steering *steering = dev->priv.steering;
2393
2394         cleanup_root_ns(steering->root_ns);
2395         cleanup_egress_acls_root_ns(dev);
2396         cleanup_ingress_acls_root_ns(dev);
2397         cleanup_root_ns(steering->fdb_root_ns);
2398         cleanup_root_ns(steering->sniffer_rx_root_ns);
2399         cleanup_root_ns(steering->sniffer_tx_root_ns);
2400         cleanup_root_ns(steering->egress_root_ns);
2401         mlx5_cleanup_fc_stats(dev);
2402         kmem_cache_destroy(steering->ftes_cache);
2403         kmem_cache_destroy(steering->fgs_cache);
2404         kfree(steering);
2405 }
2406
2407 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2408 {
2409         struct fs_prio *prio;
2410
2411         steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2412         if (!steering->sniffer_tx_root_ns)
2413                 return -ENOMEM;
2414
2415         /* Create single prio */
2416         prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2417         if (IS_ERR(prio)) {
2418                 cleanup_root_ns(steering->sniffer_tx_root_ns);
2419                 return PTR_ERR(prio);
2420         }
2421         return 0;
2422 }
2423
2424 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2425 {
2426         struct fs_prio *prio;
2427
2428         steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2429         if (!steering->sniffer_rx_root_ns)
2430                 return -ENOMEM;
2431
2432         /* Create single prio */
2433         prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2434         if (IS_ERR(prio)) {
2435                 cleanup_root_ns(steering->sniffer_rx_root_ns);
2436                 return PTR_ERR(prio);
2437         }
2438         return 0;
2439 }
2440
2441 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2442 {
2443         struct fs_prio *prio;
2444
2445         steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2446         if (!steering->fdb_root_ns)
2447                 return -ENOMEM;
2448
2449         prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 2);
2450         if (IS_ERR(prio))
2451                 goto out_err;
2452
2453         prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2454         if (IS_ERR(prio))
2455                 goto out_err;
2456
2457         set_prio_attrs(steering->fdb_root_ns);
2458         return 0;
2459
2460 out_err:
2461         cleanup_root_ns(steering->fdb_root_ns);
2462         steering->fdb_root_ns = NULL;
2463         return PTR_ERR(prio);
2464 }
2465
2466 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2467 {
2468         struct fs_prio *prio;
2469
2470         steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2471         if (!steering->esw_egress_root_ns[vport])
2472                 return -ENOMEM;
2473
2474         /* create 1 prio*/
2475         prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2476         return PTR_ERR_OR_ZERO(prio);
2477 }
2478
2479 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2480 {
2481         struct fs_prio *prio;
2482
2483         steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2484         if (!steering->esw_ingress_root_ns[vport])
2485                 return -ENOMEM;
2486
2487         /* create 1 prio*/
2488         prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2489         return PTR_ERR_OR_ZERO(prio);
2490 }
2491
2492 static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2493 {
2494         struct mlx5_flow_steering *steering = dev->priv.steering;
2495         int err;
2496         int i;
2497
2498         steering->esw_egress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2499                                                sizeof(*steering->esw_egress_root_ns),
2500                                                GFP_KERNEL);
2501         if (!steering->esw_egress_root_ns)
2502                 return -ENOMEM;
2503
2504         for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2505                 err = init_egress_acl_root_ns(steering, i);
2506                 if (err)
2507                         goto cleanup_root_ns;
2508         }
2509
2510         return 0;
2511
2512 cleanup_root_ns:
2513         for (i--; i >= 0; i--)
2514                 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2515         kfree(steering->esw_egress_root_ns);
2516         return err;
2517 }
2518
2519 static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2520 {
2521         struct mlx5_flow_steering *steering = dev->priv.steering;
2522         int err;
2523         int i;
2524
2525         steering->esw_ingress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2526                                                 sizeof(*steering->esw_ingress_root_ns),
2527                                                 GFP_KERNEL);
2528         if (!steering->esw_ingress_root_ns)
2529                 return -ENOMEM;
2530
2531         for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2532                 err = init_ingress_acl_root_ns(steering, i);
2533                 if (err)
2534                         goto cleanup_root_ns;
2535         }
2536
2537         return 0;
2538
2539 cleanup_root_ns:
2540         for (i--; i >= 0; i--)
2541                 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2542         kfree(steering->esw_ingress_root_ns);
2543         return err;
2544 }
2545
2546 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2547 {
2548         int err;
2549
2550         steering->egress_root_ns = create_root_ns(steering,
2551                                                   FS_FT_NIC_TX);
2552         if (!steering->egress_root_ns)
2553                 return -ENOMEM;
2554
2555         err = init_root_tree(steering, &egress_root_fs,
2556                              &steering->egress_root_ns->ns.node);
2557         if (err)
2558                 goto cleanup;
2559         set_prio_attrs(steering->egress_root_ns);
2560         return 0;
2561 cleanup:
2562         cleanup_root_ns(steering->egress_root_ns);
2563         steering->egress_root_ns = NULL;
2564         return err;
2565 }
2566
2567 int mlx5_init_fs(struct mlx5_core_dev *dev)
2568 {
2569         struct mlx5_flow_steering *steering;
2570         int err = 0;
2571
2572         err = mlx5_init_fc_stats(dev);
2573         if (err)
2574                 return err;
2575
2576         steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2577         if (!steering)
2578                 return -ENOMEM;
2579         steering->dev = dev;
2580         dev->priv.steering = steering;
2581
2582         steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2583                                                 sizeof(struct mlx5_flow_group), 0,
2584                                                 0, NULL);
2585         steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2586                                                  0, NULL);
2587         if (!steering->ftes_cache || !steering->fgs_cache) {
2588                 err = -ENOMEM;
2589                 goto err;
2590         }
2591
2592         if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2593               (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2594              ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2595               MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2596             MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2597                 err = init_root_ns(steering);
2598                 if (err)
2599                         goto err;
2600         }
2601
2602         if (MLX5_ESWITCH_MANAGER(dev)) {
2603                 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2604                         err = init_fdb_root_ns(steering);
2605                         if (err)
2606                                 goto err;
2607                 }
2608                 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2609                         err = init_egress_acls_root_ns(dev);
2610                         if (err)
2611                                 goto err;
2612                 }
2613                 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2614                         err = init_ingress_acls_root_ns(dev);
2615                         if (err)
2616                                 goto err;
2617                 }
2618         }
2619
2620         if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2621                 err = init_sniffer_rx_root_ns(steering);
2622                 if (err)
2623                         goto err;
2624         }
2625
2626         if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2627                 err = init_sniffer_tx_root_ns(steering);
2628                 if (err)
2629                         goto err;
2630         }
2631
2632         if (MLX5_IPSEC_DEV(dev) || MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
2633                 err = init_egress_root_ns(steering);
2634                 if (err)
2635                         goto err;
2636         }
2637
2638         return 0;
2639 err:
2640         mlx5_cleanup_fs(dev);
2641         return err;
2642 }
2643
2644 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2645 {
2646         struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2647         struct mlx5_ft_underlay_qp *new_uqp;
2648         int err = 0;
2649
2650         new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2651         if (!new_uqp)
2652                 return -ENOMEM;
2653
2654         mutex_lock(&root->chain_lock);
2655
2656         if (!root->root_ft) {
2657                 err = -EINVAL;
2658                 goto update_ft_fail;
2659         }
2660
2661         err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2662                                          false);
2663         if (err) {
2664                 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2665                                underlay_qpn, err);
2666                 goto update_ft_fail;
2667         }
2668
2669         new_uqp->qpn = underlay_qpn;
2670         list_add_tail(&new_uqp->list, &root->underlay_qpns);
2671
2672         mutex_unlock(&root->chain_lock);
2673
2674         return 0;
2675
2676 update_ft_fail:
2677         mutex_unlock(&root->chain_lock);
2678         kfree(new_uqp);
2679         return err;
2680 }
2681 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2682
2683 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2684 {
2685         struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2686         struct mlx5_ft_underlay_qp *uqp;
2687         bool found = false;
2688         int err = 0;
2689
2690         mutex_lock(&root->chain_lock);
2691         list_for_each_entry(uqp, &root->underlay_qpns, list) {
2692                 if (uqp->qpn == underlay_qpn) {
2693                         found = true;
2694                         break;
2695                 }
2696         }
2697
2698         if (!found) {
2699                 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2700                                underlay_qpn);
2701                 err = -EINVAL;
2702                 goto out;
2703         }
2704
2705         err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2706                                          true);
2707         if (err)
2708                 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2709                                underlay_qpn, err);
2710
2711         list_del(&uqp->list);
2712         mutex_unlock(&root->chain_lock);
2713         kfree(uqp);
2714
2715         return 0;
2716
2717 out:
2718         mutex_unlock(&root->chain_lock);
2719         return err;
2720 }
2721 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);