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