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