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