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