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