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