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