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