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