net/mlx5: Allocate enough space for the FDB sub-namespaces
[muen/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_core.c
1 /*
2  * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/mutex.h>
34 #include <linux/mlx5/driver.h>
35 #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->flags & FLOW_ACT_HAS_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         if (flow_act->flags & FLOW_ACT_NO_APPEND)
1632                 goto skip_search;
1633         /* Try to find a fg that already contains a matching fte */
1634         list_for_each_entry(iter, match_head, list) {
1635                 struct fs_fte *fte_tmp;
1636
1637                 g = iter->g;
1638                 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1639                 if (!fte_tmp)
1640                         continue;
1641                 rule = add_rule_fg(g, spec->match_value,
1642                                    flow_act, dest, dest_num, fte_tmp);
1643                 up_write_ref_node(&fte_tmp->node);
1644                 tree_put_node(&fte_tmp->node);
1645                 kmem_cache_free(steering->ftes_cache, fte);
1646                 return rule;
1647         }
1648
1649 skip_search:
1650         /* No group with matching fte found, or we skipped the search.
1651          * Try to add a new fte to any matching fg.
1652          */
1653
1654         /* Check the ft version, for case that new flow group
1655          * was added while the fgs weren't locked
1656          */
1657         if (atomic_read(&ft->node.version) != ft_version) {
1658                 rule = ERR_PTR(-EAGAIN);
1659                 goto out;
1660         }
1661
1662         /* Check the fgs version, for case the new FTE with the
1663          * same values was added while the fgs weren't locked
1664          */
1665         if (version != matched_fgs_get_version(match_head)) {
1666                 take_write = true;
1667                 goto search_again_locked;
1668         }
1669
1670         list_for_each_entry(iter, match_head, list) {
1671                 g = iter->g;
1672
1673                 if (!g->node.active)
1674                         continue;
1675
1676                 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1677
1678                 err = insert_fte(g, fte);
1679                 if (err) {
1680                         up_write_ref_node(&g->node);
1681                         if (err == -ENOSPC)
1682                                 continue;
1683                         kmem_cache_free(steering->ftes_cache, fte);
1684                         return ERR_PTR(err);
1685                 }
1686
1687                 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1688                 up_write_ref_node(&g->node);
1689                 rule = add_rule_fg(g, spec->match_value,
1690                                    flow_act, dest, dest_num, fte);
1691                 up_write_ref_node(&fte->node);
1692                 tree_put_node(&fte->node);
1693                 return rule;
1694         }
1695         rule = ERR_PTR(-ENOENT);
1696 out:
1697         kmem_cache_free(steering->ftes_cache, fte);
1698         return rule;
1699 }
1700
1701 static struct mlx5_flow_handle *
1702 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1703                      struct mlx5_flow_spec *spec,
1704                      struct mlx5_flow_act *flow_act,
1705                      struct mlx5_flow_destination *dest,
1706                      int dest_num)
1707
1708 {
1709         struct mlx5_flow_steering *steering = get_steering(&ft->node);
1710         struct mlx5_flow_group *g;
1711         struct mlx5_flow_handle *rule;
1712         struct match_list_head match_head;
1713         bool take_write = false;
1714         struct fs_fte *fte;
1715         int version;
1716         int err;
1717         int i;
1718
1719         if (!check_valid_spec(spec))
1720                 return ERR_PTR(-EINVAL);
1721
1722         for (i = 0; i < dest_num; i++) {
1723                 if (!dest_is_valid(&dest[i], flow_act->action, ft))
1724                         return ERR_PTR(-EINVAL);
1725         }
1726         nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1727 search_again_locked:
1728         version = atomic_read(&ft->node.version);
1729
1730         /* Collect all fgs which has a matching match_criteria */
1731         err = build_match_list(&match_head, ft, spec);
1732         if (err) {
1733                 if (take_write)
1734                         up_write_ref_node(&ft->node);
1735                 else
1736                         up_read_ref_node(&ft->node);
1737                 return ERR_PTR(err);
1738         }
1739
1740         if (!take_write)
1741                 up_read_ref_node(&ft->node);
1742
1743         rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1744                                       dest_num, version);
1745         free_match_list(&match_head);
1746         if (!IS_ERR(rule) ||
1747             (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1748                 if (take_write)
1749                         up_write_ref_node(&ft->node);
1750                 return rule;
1751         }
1752
1753         if (!take_write) {
1754                 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1755                 take_write = true;
1756         }
1757
1758         if (PTR_ERR(rule) == -EAGAIN ||
1759             version != atomic_read(&ft->node.version))
1760                 goto search_again_locked;
1761
1762         g = alloc_auto_flow_group(ft, spec);
1763         if (IS_ERR(g)) {
1764                 rule = ERR_CAST(g);
1765                 up_write_ref_node(&ft->node);
1766                 return rule;
1767         }
1768
1769         nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1770         up_write_ref_node(&ft->node);
1771
1772         err = create_auto_flow_group(ft, g);
1773         if (err)
1774                 goto err_release_fg;
1775
1776         fte = alloc_fte(ft, spec->match_value, flow_act);
1777         if (IS_ERR(fte)) {
1778                 err = PTR_ERR(fte);
1779                 goto err_release_fg;
1780         }
1781
1782         err = insert_fte(g, fte);
1783         if (err) {
1784                 kmem_cache_free(steering->ftes_cache, fte);
1785                 goto err_release_fg;
1786         }
1787
1788         nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1789         up_write_ref_node(&g->node);
1790         rule = add_rule_fg(g, spec->match_value, flow_act, dest,
1791                            dest_num, fte);
1792         up_write_ref_node(&fte->node);
1793         tree_put_node(&fte->node);
1794         tree_put_node(&g->node);
1795         return rule;
1796
1797 err_release_fg:
1798         up_write_ref_node(&g->node);
1799         tree_put_node(&g->node);
1800         return ERR_PTR(err);
1801 }
1802
1803 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1804 {
1805         return ((ft->type == FS_FT_NIC_RX) &&
1806                 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1807 }
1808
1809 struct mlx5_flow_handle *
1810 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1811                     struct mlx5_flow_spec *spec,
1812                     struct mlx5_flow_act *flow_act,
1813                     struct mlx5_flow_destination *dest,
1814                     int num_dest)
1815 {
1816         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1817         struct mlx5_flow_destination gen_dest = {};
1818         struct mlx5_flow_table *next_ft = NULL;
1819         struct mlx5_flow_handle *handle = NULL;
1820         u32 sw_action = flow_act->action;
1821         struct fs_prio *prio;
1822
1823         fs_get_obj(prio, ft->node.parent);
1824         if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1825                 if (!fwd_next_prio_supported(ft))
1826                         return ERR_PTR(-EOPNOTSUPP);
1827                 if (num_dest)
1828                         return ERR_PTR(-EINVAL);
1829                 mutex_lock(&root->chain_lock);
1830                 next_ft = find_next_chained_ft(prio);
1831                 if (next_ft) {
1832                         gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1833                         gen_dest.ft = next_ft;
1834                         dest = &gen_dest;
1835                         num_dest = 1;
1836                         flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1837                 } else {
1838                         mutex_unlock(&root->chain_lock);
1839                         return ERR_PTR(-EOPNOTSUPP);
1840                 }
1841         }
1842
1843         handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1844
1845         if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1846                 if (!IS_ERR_OR_NULL(handle) &&
1847                     (list_empty(&handle->rule[0]->next_ft))) {
1848                         mutex_lock(&next_ft->lock);
1849                         list_add(&handle->rule[0]->next_ft,
1850                                  &next_ft->fwd_rules);
1851                         mutex_unlock(&next_ft->lock);
1852                         handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1853                 }
1854                 mutex_unlock(&root->chain_lock);
1855         }
1856         return handle;
1857 }
1858 EXPORT_SYMBOL(mlx5_add_flow_rules);
1859
1860 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
1861 {
1862         int i;
1863
1864         for (i = handle->num_rules - 1; i >= 0; i--)
1865                 tree_remove_node(&handle->rule[i]->node);
1866         kfree(handle);
1867 }
1868 EXPORT_SYMBOL(mlx5_del_flow_rules);
1869
1870 /* Assuming prio->node.children(flow tables) is sorted by level */
1871 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1872 {
1873         struct fs_prio *prio;
1874
1875         fs_get_obj(prio, ft->node.parent);
1876
1877         if (!list_is_last(&ft->node.list, &prio->node.children))
1878                 return list_next_entry(ft, node.list);
1879         return find_next_chained_ft(prio);
1880 }
1881
1882 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1883 {
1884         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1885         struct mlx5_ft_underlay_qp *uqp;
1886         struct mlx5_flow_table *new_root_ft = NULL;
1887         int err = 0;
1888         u32 qpn;
1889
1890         if (root->root_ft != ft)
1891                 return 0;
1892
1893         new_root_ft = find_next_ft(ft);
1894         if (!new_root_ft) {
1895                 root->root_ft = NULL;
1896                 return 0;
1897         }
1898
1899         if (list_empty(&root->underlay_qpns)) {
1900                 /* Don't set any QPN (zero) in case QPN list is empty */
1901                 qpn = 0;
1902                 err = root->cmds->update_root_ft(root->dev, new_root_ft,
1903                                                  qpn, false);
1904         } else {
1905                 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1906                         qpn = uqp->qpn;
1907                         err = root->cmds->update_root_ft(root->dev,
1908                                                          new_root_ft, qpn,
1909                                                          false);
1910                         if (err)
1911                                 break;
1912                 }
1913         }
1914
1915         if (err)
1916                 mlx5_core_warn(root->dev,
1917                                "Update root flow table of id(%u) qpn(%d) failed\n",
1918                                ft->id, qpn);
1919         else
1920                 root->root_ft = new_root_ft;
1921
1922         return 0;
1923 }
1924
1925 /* Connect flow table from previous priority to
1926  * the next flow table.
1927  */
1928 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1929 {
1930         struct mlx5_core_dev *dev = get_dev(&ft->node);
1931         struct mlx5_flow_table *next_ft;
1932         struct fs_prio *prio;
1933         int err = 0;
1934
1935         err = update_root_ft_destroy(ft);
1936         if (err)
1937                 return err;
1938
1939         fs_get_obj(prio, ft->node.parent);
1940         if  (!(list_first_entry(&prio->node.children,
1941                                 struct mlx5_flow_table,
1942                                 node.list) == ft))
1943                 return 0;
1944
1945         next_ft = find_next_chained_ft(prio);
1946         err = connect_fwd_rules(dev, next_ft, ft);
1947         if (err)
1948                 return err;
1949
1950         err = connect_prev_fts(dev, next_ft, prio);
1951         if (err)
1952                 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1953                                ft->id);
1954         return err;
1955 }
1956
1957 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1958 {
1959         struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1960         int err = 0;
1961
1962         mutex_lock(&root->chain_lock);
1963         err = disconnect_flow_table(ft);
1964         if (err) {
1965                 mutex_unlock(&root->chain_lock);
1966                 return err;
1967         }
1968         if (tree_remove_node(&ft->node))
1969                 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1970                                ft->id);
1971         mutex_unlock(&root->chain_lock);
1972
1973         return err;
1974 }
1975 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1976
1977 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1978 {
1979         if (tree_remove_node(&fg->node))
1980                 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1981                                fg->id);
1982 }
1983
1984 struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
1985                                                 int n)
1986 {
1987         struct mlx5_flow_steering *steering = dev->priv.steering;
1988
1989         if (!steering || !steering->fdb_sub_ns)
1990                 return NULL;
1991
1992         return steering->fdb_sub_ns[n];
1993 }
1994 EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
1995
1996 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1997                                                     enum mlx5_flow_namespace_type type)
1998 {
1999         struct mlx5_flow_steering *steering = dev->priv.steering;
2000         struct mlx5_flow_root_namespace *root_ns;
2001         int prio = 0;
2002         struct fs_prio *fs_prio;
2003         struct mlx5_flow_namespace *ns;
2004
2005         if (!steering)
2006                 return NULL;
2007
2008         switch (type) {
2009         case MLX5_FLOW_NAMESPACE_FDB:
2010                 if (steering->fdb_root_ns)
2011                         return &steering->fdb_root_ns->ns;
2012                 return NULL;
2013         case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2014                 if (steering->sniffer_rx_root_ns)
2015                         return &steering->sniffer_rx_root_ns->ns;
2016                 return NULL;
2017         case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2018                 if (steering->sniffer_tx_root_ns)
2019                         return &steering->sniffer_tx_root_ns->ns;
2020                 return NULL;
2021         default:
2022                 break;
2023         }
2024
2025         if (type == MLX5_FLOW_NAMESPACE_EGRESS) {
2026                 root_ns = steering->egress_root_ns;
2027         } else { /* Must be NIC RX */
2028                 root_ns = steering->root_ns;
2029                 prio = type;
2030         }
2031
2032         if (!root_ns)
2033                 return NULL;
2034
2035         fs_prio = find_prio(&root_ns->ns, prio);
2036         if (!fs_prio)
2037                 return NULL;
2038
2039         ns = list_first_entry(&fs_prio->node.children,
2040                               typeof(*ns),
2041                               node.list);
2042
2043         return ns;
2044 }
2045 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2046
2047 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2048                                                               enum mlx5_flow_namespace_type type,
2049                                                               int vport)
2050 {
2051         struct mlx5_flow_steering *steering = dev->priv.steering;
2052
2053         if (!steering || vport >= MLX5_TOTAL_VPORTS(dev))
2054                 return NULL;
2055
2056         switch (type) {
2057         case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2058                 if (steering->esw_egress_root_ns &&
2059                     steering->esw_egress_root_ns[vport])
2060                         return &steering->esw_egress_root_ns[vport]->ns;
2061                 else
2062                         return NULL;
2063         case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2064                 if (steering->esw_ingress_root_ns &&
2065                     steering->esw_ingress_root_ns[vport])
2066                         return &steering->esw_ingress_root_ns[vport]->ns;
2067                 else
2068                         return NULL;
2069         default:
2070                 return NULL;
2071         }
2072 }
2073
2074 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2075                                        unsigned int prio,
2076                                        int num_levels,
2077                                        enum fs_node_type type)
2078 {
2079         struct fs_prio *fs_prio;
2080
2081         fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2082         if (!fs_prio)
2083                 return ERR_PTR(-ENOMEM);
2084
2085         fs_prio->node.type = type;
2086         tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2087         tree_add_node(&fs_prio->node, &ns->node);
2088         fs_prio->num_levels = num_levels;
2089         fs_prio->prio = prio;
2090         list_add_tail(&fs_prio->node.list, &ns->node.children);
2091
2092         return fs_prio;
2093 }
2094
2095 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2096                                               unsigned int prio,
2097                                               int num_levels)
2098 {
2099         return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2100 }
2101
2102 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2103                                       unsigned int prio, int num_levels)
2104 {
2105         return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2106 }
2107
2108 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2109                                                      *ns)
2110 {
2111         ns->node.type = FS_TYPE_NAMESPACE;
2112
2113         return ns;
2114 }
2115
2116 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2117 {
2118         struct mlx5_flow_namespace      *ns;
2119
2120         ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2121         if (!ns)
2122                 return ERR_PTR(-ENOMEM);
2123
2124         fs_init_namespace(ns);
2125         tree_init_node(&ns->node, NULL, del_sw_ns);
2126         tree_add_node(&ns->node, &prio->node);
2127         list_add_tail(&ns->node.list, &prio->node.children);
2128
2129         return ns;
2130 }
2131
2132 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2133                              struct init_tree_node *prio_metadata)
2134 {
2135         struct fs_prio *fs_prio;
2136         int i;
2137
2138         for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2139                 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2140                 if (IS_ERR(fs_prio))
2141                         return PTR_ERR(fs_prio);
2142         }
2143         return 0;
2144 }
2145
2146 #define FLOW_TABLE_BIT_SZ 1
2147 #define GET_FLOW_TABLE_CAP(dev, offset) \
2148         ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) +    \
2149                         offset / 32)) >>                                        \
2150           (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2151 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2152 {
2153         int i;
2154
2155         for (i = 0; i < caps->arr_sz; i++) {
2156                 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2157                         return false;
2158         }
2159         return true;
2160 }
2161
2162 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2163                                     struct init_tree_node *init_node,
2164                                     struct fs_node *fs_parent_node,
2165                                     struct init_tree_node *init_parent_node,
2166                                     int prio)
2167 {
2168         int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2169                                               flow_table_properties_nic_receive.
2170                                               max_ft_level);
2171         struct mlx5_flow_namespace *fs_ns;
2172         struct fs_prio *fs_prio;
2173         struct fs_node *base;
2174         int i;
2175         int err;
2176
2177         if (init_node->type == FS_TYPE_PRIO) {
2178                 if ((init_node->min_ft_level > max_ft_level) ||
2179                     !has_required_caps(steering->dev, &init_node->caps))
2180                         return 0;
2181
2182                 fs_get_obj(fs_ns, fs_parent_node);
2183                 if (init_node->num_leaf_prios)
2184                         return create_leaf_prios(fs_ns, prio, init_node);
2185                 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2186                 if (IS_ERR(fs_prio))
2187                         return PTR_ERR(fs_prio);
2188                 base = &fs_prio->node;
2189         } else if (init_node->type == FS_TYPE_NAMESPACE) {
2190                 fs_get_obj(fs_prio, fs_parent_node);
2191                 fs_ns = fs_create_namespace(fs_prio);
2192                 if (IS_ERR(fs_ns))
2193                         return PTR_ERR(fs_ns);
2194                 base = &fs_ns->node;
2195         } else {
2196                 return -EINVAL;
2197         }
2198         prio = 0;
2199         for (i = 0; i < init_node->ar_size; i++) {
2200                 err = init_root_tree_recursive(steering, &init_node->children[i],
2201                                                base, init_node, prio);
2202                 if (err)
2203                         return err;
2204                 if (init_node->children[i].type == FS_TYPE_PRIO &&
2205                     init_node->children[i].num_leaf_prios) {
2206                         prio += init_node->children[i].num_leaf_prios;
2207                 }
2208         }
2209
2210         return 0;
2211 }
2212
2213 static int init_root_tree(struct mlx5_flow_steering *steering,
2214                           struct init_tree_node *init_node,
2215                           struct fs_node *fs_parent_node)
2216 {
2217         int i;
2218         struct mlx5_flow_namespace *fs_ns;
2219         int err;
2220
2221         fs_get_obj(fs_ns, fs_parent_node);
2222         for (i = 0; i < init_node->ar_size; i++) {
2223                 err = init_root_tree_recursive(steering, &init_node->children[i],
2224                                                &fs_ns->node,
2225                                                init_node, i);
2226                 if (err)
2227                         return err;
2228         }
2229         return 0;
2230 }
2231
2232 static struct mlx5_flow_root_namespace
2233 *create_root_ns(struct mlx5_flow_steering *steering,
2234                 enum fs_flow_table_type table_type)
2235 {
2236         const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2237         struct mlx5_flow_root_namespace *root_ns;
2238         struct mlx5_flow_namespace *ns;
2239
2240         if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2241             (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2242                 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2243
2244         /* Create the root namespace */
2245         root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
2246         if (!root_ns)
2247                 return NULL;
2248
2249         root_ns->dev = steering->dev;
2250         root_ns->table_type = table_type;
2251         root_ns->cmds = cmds;
2252
2253         INIT_LIST_HEAD(&root_ns->underlay_qpns);
2254
2255         ns = &root_ns->ns;
2256         fs_init_namespace(ns);
2257         mutex_init(&root_ns->chain_lock);
2258         tree_init_node(&ns->node, NULL, NULL);
2259         tree_add_node(&ns->node, NULL);
2260
2261         return root_ns;
2262 }
2263
2264 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2265
2266 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2267 {
2268         struct fs_prio *prio;
2269
2270         fs_for_each_prio(prio, ns) {
2271                  /* This updates prio start_level and num_levels */
2272                 set_prio_attrs_in_prio(prio, acc_level);
2273                 acc_level += prio->num_levels;
2274         }
2275         return acc_level;
2276 }
2277
2278 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2279 {
2280         struct mlx5_flow_namespace *ns;
2281         int acc_level_ns = acc_level;
2282
2283         prio->start_level = acc_level;
2284         fs_for_each_ns(ns, prio)
2285                 /* This updates start_level and num_levels of ns's priority descendants */
2286                 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2287         if (!prio->num_levels)
2288                 prio->num_levels = acc_level_ns - prio->start_level;
2289         WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2290 }
2291
2292 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2293 {
2294         struct mlx5_flow_namespace *ns = &root_ns->ns;
2295         struct fs_prio *prio;
2296         int start_level = 0;
2297
2298         fs_for_each_prio(prio, ns) {
2299                 set_prio_attrs_in_prio(prio, start_level);
2300                 start_level += prio->num_levels;
2301         }
2302 }
2303
2304 #define ANCHOR_PRIO 0
2305 #define ANCHOR_SIZE 1
2306 #define ANCHOR_LEVEL 0
2307 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2308 {
2309         struct mlx5_flow_namespace *ns = NULL;
2310         struct mlx5_flow_table_attr ft_attr = {};
2311         struct mlx5_flow_table *ft;
2312
2313         ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2314         if (WARN_ON(!ns))
2315                 return -EINVAL;
2316
2317         ft_attr.max_fte = ANCHOR_SIZE;
2318         ft_attr.level   = ANCHOR_LEVEL;
2319         ft_attr.prio    = ANCHOR_PRIO;
2320
2321         ft = mlx5_create_flow_table(ns, &ft_attr);
2322         if (IS_ERR(ft)) {
2323                 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2324                 return PTR_ERR(ft);
2325         }
2326         return 0;
2327 }
2328
2329 static int init_root_ns(struct mlx5_flow_steering *steering)
2330 {
2331         int err;
2332
2333         steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2334         if (!steering->root_ns)
2335                 return -ENOMEM;
2336
2337         err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2338         if (err)
2339                 goto out_err;
2340
2341         set_prio_attrs(steering->root_ns);
2342         err = create_anchor_flow_table(steering);
2343         if (err)
2344                 goto out_err;
2345
2346         return 0;
2347
2348 out_err:
2349         cleanup_root_ns(steering->root_ns);
2350         steering->root_ns = NULL;
2351         return err;
2352 }
2353
2354 static void clean_tree(struct fs_node *node)
2355 {
2356         if (node) {
2357                 struct fs_node *iter;
2358                 struct fs_node *temp;
2359
2360                 tree_get_node(node);
2361                 list_for_each_entry_safe(iter, temp, &node->children, list)
2362                         clean_tree(iter);
2363                 tree_put_node(node);
2364                 tree_remove_node(node);
2365         }
2366 }
2367
2368 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2369 {
2370         if (!root_ns)
2371                 return;
2372
2373         clean_tree(&root_ns->ns.node);
2374 }
2375
2376 static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2377 {
2378         struct mlx5_flow_steering *steering = dev->priv.steering;
2379         int i;
2380
2381         if (!steering->esw_egress_root_ns)
2382                 return;
2383
2384         for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2385                 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2386
2387         kfree(steering->esw_egress_root_ns);
2388 }
2389
2390 static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2391 {
2392         struct mlx5_flow_steering *steering = dev->priv.steering;
2393         int i;
2394
2395         if (!steering->esw_ingress_root_ns)
2396                 return;
2397
2398         for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2399                 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2400
2401         kfree(steering->esw_ingress_root_ns);
2402 }
2403
2404 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2405 {
2406         struct mlx5_flow_steering *steering = dev->priv.steering;
2407
2408         cleanup_root_ns(steering->root_ns);
2409         cleanup_egress_acls_root_ns(dev);
2410         cleanup_ingress_acls_root_ns(dev);
2411         cleanup_root_ns(steering->fdb_root_ns);
2412         steering->fdb_root_ns = NULL;
2413         kfree(steering->fdb_sub_ns);
2414         steering->fdb_sub_ns = NULL;
2415         cleanup_root_ns(steering->sniffer_rx_root_ns);
2416         cleanup_root_ns(steering->sniffer_tx_root_ns);
2417         cleanup_root_ns(steering->egress_root_ns);
2418         mlx5_cleanup_fc_stats(dev);
2419         kmem_cache_destroy(steering->ftes_cache);
2420         kmem_cache_destroy(steering->fgs_cache);
2421         kfree(steering);
2422 }
2423
2424 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2425 {
2426         struct fs_prio *prio;
2427
2428         steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2429         if (!steering->sniffer_tx_root_ns)
2430                 return -ENOMEM;
2431
2432         /* Create single prio */
2433         prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2434         if (IS_ERR(prio)) {
2435                 cleanup_root_ns(steering->sniffer_tx_root_ns);
2436                 return PTR_ERR(prio);
2437         }
2438         return 0;
2439 }
2440
2441 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2442 {
2443         struct fs_prio *prio;
2444
2445         steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2446         if (!steering->sniffer_rx_root_ns)
2447                 return -ENOMEM;
2448
2449         /* Create single prio */
2450         prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2451         if (IS_ERR(prio)) {
2452                 cleanup_root_ns(steering->sniffer_rx_root_ns);
2453                 return PTR_ERR(prio);
2454         }
2455         return 0;
2456 }
2457
2458 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2459 {
2460         struct mlx5_flow_namespace *ns;
2461         struct fs_prio *maj_prio;
2462         struct fs_prio *min_prio;
2463         int levels;
2464         int chain;
2465         int prio;
2466         int err;
2467
2468         steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2469         if (!steering->fdb_root_ns)
2470                 return -ENOMEM;
2471
2472         steering->fdb_sub_ns = kzalloc(sizeof(steering->fdb_sub_ns) *
2473                                        (FDB_MAX_CHAIN + 1), GFP_KERNEL);
2474         if (!steering->fdb_sub_ns)
2475                 return -ENOMEM;
2476
2477         levels = 2 * FDB_MAX_PRIO * (FDB_MAX_CHAIN + 1);
2478         maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns, 0,
2479                                           levels);
2480         if (IS_ERR(maj_prio)) {
2481                 err = PTR_ERR(maj_prio);
2482                 goto out_err;
2483         }
2484
2485         for (chain = 0; chain <= FDB_MAX_CHAIN; chain++) {
2486                 ns = fs_create_namespace(maj_prio);
2487                 if (IS_ERR(ns)) {
2488                         err = PTR_ERR(ns);
2489                         goto out_err;
2490                 }
2491
2492                 for (prio = 0; prio < FDB_MAX_PRIO * (chain + 1); prio++) {
2493                         min_prio = fs_create_prio(ns, prio, 2);
2494                         if (IS_ERR(min_prio)) {
2495                                 err = PTR_ERR(min_prio);
2496                                 goto out_err;
2497                         }
2498                 }
2499
2500                 steering->fdb_sub_ns[chain] = ns;
2501         }
2502
2503         maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2504         if (IS_ERR(maj_prio)) {
2505                 err = PTR_ERR(maj_prio);
2506                 goto out_err;
2507         }
2508
2509         set_prio_attrs(steering->fdb_root_ns);
2510         return 0;
2511
2512 out_err:
2513         cleanup_root_ns(steering->fdb_root_ns);
2514         kfree(steering->fdb_sub_ns);
2515         steering->fdb_sub_ns = NULL;
2516         steering->fdb_root_ns = NULL;
2517         return err;
2518 }
2519
2520 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2521 {
2522         struct fs_prio *prio;
2523
2524         steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2525         if (!steering->esw_egress_root_ns[vport])
2526                 return -ENOMEM;
2527
2528         /* create 1 prio*/
2529         prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2530         return PTR_ERR_OR_ZERO(prio);
2531 }
2532
2533 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2534 {
2535         struct fs_prio *prio;
2536
2537         steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2538         if (!steering->esw_ingress_root_ns[vport])
2539                 return -ENOMEM;
2540
2541         /* create 1 prio*/
2542         prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2543         return PTR_ERR_OR_ZERO(prio);
2544 }
2545
2546 static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2547 {
2548         struct mlx5_flow_steering *steering = dev->priv.steering;
2549         int err;
2550         int i;
2551
2552         steering->esw_egress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2553                                                sizeof(*steering->esw_egress_root_ns),
2554                                                GFP_KERNEL);
2555         if (!steering->esw_egress_root_ns)
2556                 return -ENOMEM;
2557
2558         for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2559                 err = init_egress_acl_root_ns(steering, i);
2560                 if (err)
2561                         goto cleanup_root_ns;
2562         }
2563
2564         return 0;
2565
2566 cleanup_root_ns:
2567         for (i--; i >= 0; i--)
2568                 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2569         kfree(steering->esw_egress_root_ns);
2570         return err;
2571 }
2572
2573 static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2574 {
2575         struct mlx5_flow_steering *steering = dev->priv.steering;
2576         int err;
2577         int i;
2578
2579         steering->esw_ingress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2580                                                 sizeof(*steering->esw_ingress_root_ns),
2581                                                 GFP_KERNEL);
2582         if (!steering->esw_ingress_root_ns)
2583                 return -ENOMEM;
2584
2585         for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2586                 err = init_ingress_acl_root_ns(steering, i);
2587                 if (err)
2588                         goto cleanup_root_ns;
2589         }
2590
2591         return 0;
2592
2593 cleanup_root_ns:
2594         for (i--; i >= 0; i--)
2595                 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2596         kfree(steering->esw_ingress_root_ns);
2597         return err;
2598 }
2599
2600 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2601 {
2602         int err;
2603
2604         steering->egress_root_ns = create_root_ns(steering,
2605                                                   FS_FT_NIC_TX);
2606         if (!steering->egress_root_ns)
2607                 return -ENOMEM;
2608
2609         err = init_root_tree(steering, &egress_root_fs,
2610                              &steering->egress_root_ns->ns.node);
2611         if (err)
2612                 goto cleanup;
2613         set_prio_attrs(steering->egress_root_ns);
2614         return 0;
2615 cleanup:
2616         cleanup_root_ns(steering->egress_root_ns);
2617         steering->egress_root_ns = NULL;
2618         return err;
2619 }
2620
2621 int mlx5_init_fs(struct mlx5_core_dev *dev)
2622 {
2623         struct mlx5_flow_steering *steering;
2624         int err = 0;
2625
2626         err = mlx5_init_fc_stats(dev);
2627         if (err)
2628                 return err;
2629
2630         steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2631         if (!steering)
2632                 return -ENOMEM;
2633         steering->dev = dev;
2634         dev->priv.steering = steering;
2635
2636         steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2637                                                 sizeof(struct mlx5_flow_group), 0,
2638                                                 0, NULL);
2639         steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2640                                                  0, NULL);
2641         if (!steering->ftes_cache || !steering->fgs_cache) {
2642                 err = -ENOMEM;
2643                 goto err;
2644         }
2645
2646         if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2647               (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2648              ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2649               MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2650             MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2651                 err = init_root_ns(steering);
2652                 if (err)
2653                         goto err;
2654         }
2655
2656         if (MLX5_ESWITCH_MANAGER(dev)) {
2657                 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
2658                         err = init_fdb_root_ns(steering);
2659                         if (err)
2660                                 goto err;
2661                 }
2662                 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
2663                         err = init_egress_acls_root_ns(dev);
2664                         if (err)
2665                                 goto err;
2666                 }
2667                 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
2668                         err = init_ingress_acls_root_ns(dev);
2669                         if (err)
2670                                 goto err;
2671                 }
2672         }
2673
2674         if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2675                 err = init_sniffer_rx_root_ns(steering);
2676                 if (err)
2677                         goto err;
2678         }
2679
2680         if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2681                 err = init_sniffer_tx_root_ns(steering);
2682                 if (err)
2683                         goto err;
2684         }
2685
2686         if (MLX5_IPSEC_DEV(dev) || MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
2687                 err = init_egress_root_ns(steering);
2688                 if (err)
2689                         goto err;
2690         }
2691
2692         return 0;
2693 err:
2694         mlx5_cleanup_fs(dev);
2695         return err;
2696 }
2697
2698 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2699 {
2700         struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2701         struct mlx5_ft_underlay_qp *new_uqp;
2702         int err = 0;
2703
2704         new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2705         if (!new_uqp)
2706                 return -ENOMEM;
2707
2708         mutex_lock(&root->chain_lock);
2709
2710         if (!root->root_ft) {
2711                 err = -EINVAL;
2712                 goto update_ft_fail;
2713         }
2714
2715         err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2716                                          false);
2717         if (err) {
2718                 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2719                                underlay_qpn, err);
2720                 goto update_ft_fail;
2721         }
2722
2723         new_uqp->qpn = underlay_qpn;
2724         list_add_tail(&new_uqp->list, &root->underlay_qpns);
2725
2726         mutex_unlock(&root->chain_lock);
2727
2728         return 0;
2729
2730 update_ft_fail:
2731         mutex_unlock(&root->chain_lock);
2732         kfree(new_uqp);
2733         return err;
2734 }
2735 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2736
2737 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2738 {
2739         struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2740         struct mlx5_ft_underlay_qp *uqp;
2741         bool found = false;
2742         int err = 0;
2743
2744         mutex_lock(&root->chain_lock);
2745         list_for_each_entry(uqp, &root->underlay_qpns, list) {
2746                 if (uqp->qpn == underlay_qpn) {
2747                         found = true;
2748                         break;
2749                 }
2750         }
2751
2752         if (!found) {
2753                 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2754                                underlay_qpn);
2755                 err = -EINVAL;
2756                 goto out;
2757         }
2758
2759         err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2760                                          true);
2761         if (err)
2762                 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2763                                underlay_qpn, err);
2764
2765         list_del(&uqp->list);
2766         mutex_unlock(&root->chain_lock);
2767         kfree(uqp);
2768
2769         return 0;
2770
2771 out:
2772         mutex_unlock(&root->chain_lock);
2773         return err;
2774 }
2775 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);