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