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