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