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