MIPS: OCTEON: Replace SIZEOF_FIELD() macro
[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
7dee607e
PP
534static void del_sw_fte_rcu(struct rcu_head *head)
535{
536 struct fs_fte *fte = container_of(head, struct fs_fte, rcu);
537 struct mlx5_flow_steering *steering = get_steering(&fte->node);
538
539 kmem_cache_free(steering->ftes_cache, fte);
540}
541
bd71b08e
MG
542static void del_sw_fte(struct fs_node *node)
543{
544 struct mlx5_flow_group *fg;
545 struct fs_fte *fte;
546 int err;
547
548 fs_get_obj(fte, node);
549 fs_get_obj(fg, fte->node.parent);
0c56b975 550
19f100fe
MG
551 err = rhashtable_remove_fast(&fg->ftes_hash,
552 &fte->hash,
553 rhash_fte);
554 WARN_ON(err);
555 ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
7dee607e
PP
556
557 call_rcu(&fte->rcu, del_sw_fte_rcu);
0c56b975
MG
558}
559
bd71b08e 560static void del_hw_flow_group(struct fs_node *node)
0c56b975 561{
af76c501 562 struct mlx5_flow_root_namespace *root;
0c56b975
MG
563 struct mlx5_flow_group *fg;
564 struct mlx5_flow_table *ft;
565 struct mlx5_core_dev *dev;
566
567 fs_get_obj(fg, node);
568 fs_get_obj(ft, fg->node.parent);
569 dev = get_dev(&ft->node);
4c03e69a 570 trace_mlx5_fs_del_fg(fg);
0c56b975 571
af76c501 572 root = find_root(&ft->node);
ae288a48 573 if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
bd71b08e
MG
574 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
575 fg->id, ft->id);
576}
577
578static void del_sw_flow_group(struct fs_node *node)
579{
a369d4ac 580 struct mlx5_flow_steering *steering = get_steering(node);
bd71b08e
MG
581 struct mlx5_flow_group *fg;
582 struct mlx5_flow_table *ft;
583 int err;
584
585 fs_get_obj(fg, node);
586 fs_get_obj(ft, fg->node.parent);
32dba76a 587
0d235c3f 588 rhashtable_destroy(&fg->ftes_hash);
75d1d187 589 ida_destroy(&fg->fte_allocator);
97fd8da2 590 if (ft->autogroup.active && fg->max_ftes == ft->autogroup.group_size)
bd71b08e 591 ft->autogroup.num_groups--;
693c6883
MB
592 err = rhltable_remove(&ft->fgs_hash,
593 &fg->hash,
594 rhash_fg);
595 WARN_ON(err);
a369d4ac 596 kmem_cache_free(steering->fgs_cache, fg);
0c56b975
MG
597}
598
f5c2ff17
MG
599static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
600{
601 int index;
602 int ret;
603
604 index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
605 if (index < 0)
606 return index;
607
608 fte->index = index + fg->start_index;
609 ret = rhashtable_insert_fast(&fg->ftes_hash,
610 &fte->hash,
611 rhash_fte);
612 if (ret)
613 goto err_ida_remove;
614
615 tree_add_node(&fte->node, &fg->node);
616 list_add_tail(&fte->node.list, &fg->node.children);
617 return 0;
618
619err_ida_remove:
620 ida_simple_remove(&fg->fte_allocator, index);
621 return ret;
622}
623
a369d4ac 624static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
5233794b 625 const struct mlx5_flow_spec *spec,
f5c2ff17 626 struct mlx5_flow_act *flow_act)
0c56b975 627{
a369d4ac 628 struct mlx5_flow_steering *steering = get_steering(&ft->node);
0c56b975
MG
629 struct fs_fte *fte;
630
a369d4ac 631 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
0c56b975
MG
632 if (!fte)
633 return ERR_PTR(-ENOMEM);
634
bb0ee7dc 635 memcpy(fte->val, &spec->match_value, sizeof(fte->val));
0c56b975 636 fte->node.type = FS_TYPE_FLOW_ENTRY;
d2ec6a35 637 fte->action = *flow_act;
bb0ee7dc 638 fte->flow_context = spec->flow_context;
0c56b975 639
718ce4d6 640 tree_init_node(&fte->node, NULL, del_sw_fte);
19f100fe
MG
641
642 return fte;
19f100fe
MG
643}
644
a369d4ac
MG
645static void dealloc_flow_group(struct mlx5_flow_steering *steering,
646 struct mlx5_flow_group *fg)
19f100fe
MG
647{
648 rhashtable_destroy(&fg->ftes_hash);
a369d4ac 649 kmem_cache_free(steering->fgs_cache, fg);
19f100fe
MG
650}
651
a369d4ac
MG
652static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
653 u8 match_criteria_enable,
5233794b 654 const void *match_criteria,
19f100fe
MG
655 int start_index,
656 int end_index)
0c56b975
MG
657{
658 struct mlx5_flow_group *fg;
0d235c3f
MB
659 int ret;
660
a369d4ac 661 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
0c56b975
MG
662 if (!fg)
663 return ERR_PTR(-ENOMEM);
664
0d235c3f
MB
665 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
666 if (ret) {
a369d4ac 667 kmem_cache_free(steering->fgs_cache, fg);
0d235c3f 668 return ERR_PTR(ret);
fc9c5a4a
TZ
669 }
670
75d1d187 671 ida_init(&fg->fte_allocator);
0c56b975
MG
672 fg->mask.match_criteria_enable = match_criteria_enable;
673 memcpy(&fg->mask.match_criteria, match_criteria,
674 sizeof(fg->mask.match_criteria));
675 fg->node.type = FS_TYPE_FLOW_GROUP;
19f100fe
MG
676 fg->start_index = start_index;
677 fg->max_ftes = end_index - start_index + 1;
678
679 return fg;
680}
681
682static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
683 u8 match_criteria_enable,
5233794b 684 const void *match_criteria,
19f100fe
MG
685 int start_index,
686 int end_index,
687 struct list_head *prev)
688{
a369d4ac 689 struct mlx5_flow_steering *steering = get_steering(&ft->node);
19f100fe
MG
690 struct mlx5_flow_group *fg;
691 int ret;
692
a369d4ac 693 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
19f100fe
MG
694 start_index, end_index);
695 if (IS_ERR(fg))
696 return fg;
697
698 /* initialize refcnt, add to parent list */
699 ret = rhltable_insert(&ft->fgs_hash,
700 &fg->hash,
701 rhash_fg);
702 if (ret) {
a369d4ac 703 dealloc_flow_group(steering, fg);
19f100fe
MG
704 return ERR_PTR(ret);
705 }
706
bd71b08e 707 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
19f100fe
MG
708 tree_add_node(&fg->node, &ft->node);
709 /* Add node to group list */
710 list_add(&fg->node.list, prev);
bd71b08e 711 atomic_inc(&ft->node.version);
19f100fe 712
0c56b975
MG
713 return fg;
714}
715
efdc810b 716static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
aaff1bea 717 enum fs_flow_table_type table_type,
c9f1b073
HHZ
718 enum fs_flow_table_op_mod op_mod,
719 u32 flags)
0c56b975
MG
720{
721 struct mlx5_flow_table *ft;
693c6883 722 int ret;
0c56b975
MG
723
724 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
725 if (!ft)
693c6883
MB
726 return ERR_PTR(-ENOMEM);
727
728 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
729 if (ret) {
730 kfree(ft);
731 return ERR_PTR(ret);
732 }
0c56b975
MG
733
734 ft->level = level;
735 ft->node.type = FS_TYPE_FLOW_TABLE;
aaff1bea 736 ft->op_mod = op_mod;
0c56b975 737 ft->type = table_type;
efdc810b 738 ft->vport = vport;
0c56b975 739 ft->max_fte = max_fte;
c9f1b073 740 ft->flags = flags;
b3638e1a
MG
741 INIT_LIST_HEAD(&ft->fwd_rules);
742 mutex_init(&ft->lock);
0c56b975
MG
743
744 return ft;
745}
746
fdb6896f
MG
747/* If reverse is false, then we search for the first flow table in the
748 * root sub-tree from start(closest from right), else we search for the
749 * last flow table in the root sub-tree till start(closest from left).
750 */
751static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
752 struct list_head *start,
753 bool reverse)
754{
755#define list_advance_entry(pos, reverse) \
756 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
757
758#define list_for_each_advance_continue(pos, head, reverse) \
759 for (pos = list_advance_entry(pos, reverse); \
760 &pos->list != (head); \
761 pos = list_advance_entry(pos, reverse))
762
763 struct fs_node *iter = list_entry(start, struct fs_node, list);
764 struct mlx5_flow_table *ft = NULL;
765
328edb49 766 if (!root || root->type == FS_TYPE_PRIO_CHAINS)
fdb6896f
MG
767 return NULL;
768
769 list_for_each_advance_continue(iter, &root->children, reverse) {
770 if (iter->type == FS_TYPE_FLOW_TABLE) {
771 fs_get_obj(ft, iter);
772 return ft;
773 }
774 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
775 if (ft)
776 return ft;
777 }
778
779 return ft;
780}
781
782/* If reverse if false then return the first flow table in next priority of
783 * prio in the tree, else return the last flow table in the previous priority
784 * of prio in the tree.
785 */
786static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
787{
788 struct mlx5_flow_table *ft = NULL;
789 struct fs_node *curr_node;
790 struct fs_node *parent;
791
792 parent = prio->node.parent;
793 curr_node = &prio->node;
794 while (!ft && parent) {
795 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
796 curr_node = parent;
797 parent = curr_node->parent;
798 }
799 return ft;
800}
801
802/* Assuming all the tree is locked by mutex chain lock */
803static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
804{
805 return find_closest_ft(prio, false);
806}
807
808/* Assuming all the tree is locked by mutex chain lock */
809static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
810{
811 return find_closest_ft(prio, true);
812}
813
f90edfd2
MG
814static int connect_fts_in_prio(struct mlx5_core_dev *dev,
815 struct fs_prio *prio,
816 struct mlx5_flow_table *ft)
817{
af76c501 818 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
f90edfd2
MG
819 struct mlx5_flow_table *iter;
820 int i = 0;
821 int err;
822
823 fs_for_each_ft(iter, prio) {
824 i++;
ae288a48 825 err = root->cmds->modify_flow_table(root, iter, ft);
f90edfd2
MG
826 if (err) {
827 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
828 iter->id);
829 /* The driver is out of sync with the FW */
830 if (i > 1)
831 WARN_ON(true);
832 return err;
833 }
834 }
835 return 0;
836}
837
838/* Connect flow tables from previous priority of prio to ft */
839static int connect_prev_fts(struct mlx5_core_dev *dev,
840 struct mlx5_flow_table *ft,
841 struct fs_prio *prio)
842{
843 struct mlx5_flow_table *prev_ft;
844
845 prev_ft = find_prev_chained_ft(prio);
846 if (prev_ft) {
847 struct fs_prio *prev_prio;
848
849 fs_get_obj(prev_prio, prev_ft->node.parent);
850 return connect_fts_in_prio(dev, prev_prio, ft);
851 }
852 return 0;
853}
854
2cc43b49
MG
855static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
856 *prio)
857{
858 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
dae37456 859 struct mlx5_ft_underlay_qp *uqp;
2cc43b49 860 int min_level = INT_MAX;
aef6c443 861 int err = 0;
dae37456 862 u32 qpn;
2cc43b49
MG
863
864 if (root->root_ft)
865 min_level = root->root_ft->level;
866
867 if (ft->level >= min_level)
868 return 0;
869
dae37456
AV
870 if (list_empty(&root->underlay_qpns)) {
871 /* Don't set any QPN (zero) in case QPN list is empty */
872 qpn = 0;
ae288a48 873 err = root->cmds->update_root_ft(root, ft, qpn, false);
dae37456
AV
874 } else {
875 list_for_each_entry(uqp, &root->underlay_qpns, list) {
876 qpn = uqp->qpn;
ae288a48 877 err = root->cmds->update_root_ft(root, ft,
af76c501 878 qpn, false);
dae37456
AV
879 if (err)
880 break;
881 }
882 }
883
2cc43b49 884 if (err)
dae37456
AV
885 mlx5_core_warn(root->dev,
886 "Update root flow table of id(%u) qpn(%d) failed\n",
887 ft->id, qpn);
2cc43b49
MG
888 else
889 root->root_ft = ft;
890
891 return err;
892}
893
74491de9
MB
894static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
895 struct mlx5_flow_destination *dest)
b3638e1a 896{
af76c501 897 struct mlx5_flow_root_namespace *root;
b3638e1a
MG
898 struct mlx5_flow_table *ft;
899 struct mlx5_flow_group *fg;
900 struct fs_fte *fte;
bd5251db 901 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
b3638e1a
MG
902 int err = 0;
903
904 fs_get_obj(fte, rule->node.parent);
d2ec6a35 905 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
b3638e1a 906 return -EINVAL;
476d61b7 907 down_write_ref_node(&fte->node, false);
b3638e1a
MG
908 fs_get_obj(fg, fte->node.parent);
909 fs_get_obj(ft, fg->node.parent);
910
911 memcpy(&rule->dest_attr, dest, sizeof(*dest));
af76c501 912 root = find_root(&ft->node);
ae288a48 913 err = root->cmds->update_fte(root, ft, fg,
af76c501 914 modify_mask, fte);
476d61b7 915 up_write_ref_node(&fte->node, false);
b3638e1a
MG
916
917 return err;
918}
919
74491de9
MB
920int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
921 struct mlx5_flow_destination *new_dest,
922 struct mlx5_flow_destination *old_dest)
923{
924 int i;
925
926 if (!old_dest) {
927 if (handle->num_rules != 1)
928 return -EINVAL;
929 return _mlx5_modify_rule_destination(handle->rule[0],
930 new_dest);
931 }
932
933 for (i = 0; i < handle->num_rules; i++) {
934 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
935 return _mlx5_modify_rule_destination(handle->rule[i],
936 new_dest);
937 }
938
939 return -EINVAL;
940}
941
b3638e1a
MG
942/* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
943static int connect_fwd_rules(struct mlx5_core_dev *dev,
944 struct mlx5_flow_table *new_next_ft,
945 struct mlx5_flow_table *old_next_ft)
946{
4c5009c5 947 struct mlx5_flow_destination dest = {};
b3638e1a
MG
948 struct mlx5_flow_rule *iter;
949 int err = 0;
950
951 /* new_next_ft and old_next_ft could be NULL only
952 * when we create/destroy the anchor flow table.
953 */
954 if (!new_next_ft || !old_next_ft)
955 return 0;
956
957 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
958 dest.ft = new_next_ft;
959
960 mutex_lock(&old_next_ft->lock);
961 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
962 mutex_unlock(&old_next_ft->lock);
963 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
74491de9 964 err = _mlx5_modify_rule_destination(iter, &dest);
b3638e1a
MG
965 if (err)
966 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
967 new_next_ft->id);
968 }
969 return 0;
970}
971
f90edfd2
MG
972static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
973 struct fs_prio *prio)
974{
b3638e1a 975 struct mlx5_flow_table *next_ft;
f90edfd2
MG
976 int err = 0;
977
978 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
979
980 if (list_empty(&prio->node.children)) {
981 err = connect_prev_fts(dev, ft, prio);
982 if (err)
983 return err;
b3638e1a
MG
984
985 next_ft = find_next_chained_ft(prio);
986 err = connect_fwd_rules(dev, ft, next_ft);
987 if (err)
988 return err;
f90edfd2
MG
989 }
990
991 if (MLX5_CAP_FLOWTABLE(dev,
992 flow_table_properties_nic_receive.modify_root))
993 err = update_root_ft_create(ft, prio);
994 return err;
995}
996
d63cd286
MG
997static void list_add_flow_table(struct mlx5_flow_table *ft,
998 struct fs_prio *prio)
999{
1000 struct list_head *prev = &prio->node.children;
1001 struct mlx5_flow_table *iter;
1002
1003 fs_for_each_ft(iter, prio) {
1004 if (iter->level > ft->level)
1005 break;
1006 prev = &iter->node.list;
1007 }
1008 list_add(&ft->node.list, prev);
1009}
1010
efdc810b 1011static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
b3ba5149 1012 struct mlx5_flow_table_attr *ft_attr,
aaff1bea 1013 enum fs_flow_table_op_mod op_mod,
b3ba5149 1014 u16 vport)
0c56b975 1015{
b3ba5149 1016 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
f90edfd2 1017 struct mlx5_flow_table *next_ft = NULL;
b3ba5149 1018 struct fs_prio *fs_prio = NULL;
0c56b975 1019 struct mlx5_flow_table *ft;
0c56b975 1020 int log_table_sz;
b3ba5149 1021 int err;
0c56b975
MG
1022
1023 if (!root) {
1024 pr_err("mlx5: flow steering failed to find root of namespace\n");
1025 return ERR_PTR(-ENODEV);
1026 }
1027
2cc43b49 1028 mutex_lock(&root->chain_lock);
b3ba5149 1029 fs_prio = find_prio(ns, ft_attr->prio);
2cc43b49
MG
1030 if (!fs_prio) {
1031 err = -EINVAL;
1032 goto unlock_root;
1033 }
b3ba5149 1034 if (ft_attr->level >= fs_prio->num_levels) {
0c56b975 1035 err = -ENOSPC;
2cc43b49 1036 goto unlock_root;
0c56b975 1037 }
d63cd286
MG
1038 /* The level is related to the
1039 * priority level range.
1040 */
b3ba5149
ES
1041 ft_attr->level += fs_prio->start_level;
1042 ft = alloc_flow_table(ft_attr->level,
efdc810b 1043 vport,
b3ba5149 1044 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
aaff1bea 1045 root->table_type,
b3ba5149 1046 op_mod, ft_attr->flags);
693c6883
MB
1047 if (IS_ERR(ft)) {
1048 err = PTR_ERR(ft);
2cc43b49 1049 goto unlock_root;
0c56b975
MG
1050 }
1051
bd71b08e 1052 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
aaff1bea 1053 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
f90edfd2 1054 next_ft = find_next_chained_ft(fs_prio);
f66ad830 1055 ft->def_miss_action = ns->def_miss_action;
ae288a48 1056 err = root->cmds->create_flow_table(root, ft, log_table_sz, next_ft);
0c56b975
MG
1057 if (err)
1058 goto free_ft;
1059
f90edfd2
MG
1060 err = connect_flow_table(root->dev, ft, fs_prio);
1061 if (err)
1062 goto destroy_ft;
19f100fe 1063 ft->node.active = true;
476d61b7 1064 down_write_ref_node(&fs_prio->node, false);
0c56b975 1065 tree_add_node(&ft->node, &fs_prio->node);
d63cd286 1066 list_add_flow_table(ft, fs_prio);
0c56b975 1067 fs_prio->num_ft++;
476d61b7 1068 up_write_ref_node(&fs_prio->node, false);
2cc43b49 1069 mutex_unlock(&root->chain_lock);
8e4ca986 1070 trace_mlx5_fs_add_ft(ft);
0c56b975 1071 return ft;
2cc43b49 1072destroy_ft:
ae288a48 1073 root->cmds->destroy_flow_table(root, ft);
0c56b975
MG
1074free_ft:
1075 kfree(ft);
2cc43b49
MG
1076unlock_root:
1077 mutex_unlock(&root->chain_lock);
0c56b975
MG
1078 return ERR_PTR(err);
1079}
1080
efdc810b 1081struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
b3ba5149 1082 struct mlx5_flow_table_attr *ft_attr)
efdc810b 1083{
b3ba5149 1084 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
efdc810b
MHY
1085}
1086
1087struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1088 int prio, int max_fte,
1089 u32 level, u16 vport)
1090{
b3ba5149
ES
1091 struct mlx5_flow_table_attr ft_attr = {};
1092
1093 ft_attr.max_fte = max_fte;
1094 ft_attr.level = level;
1095 ft_attr.prio = prio;
1096
57f35c93 1097 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
efdc810b
MHY
1098}
1099
b3ba5149
ES
1100struct mlx5_flow_table*
1101mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1102 int prio, u32 level)
aaff1bea 1103{
b3ba5149
ES
1104 struct mlx5_flow_table_attr ft_attr = {};
1105
1106 ft_attr.level = level;
1107 ft_attr.prio = prio;
1108 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
aaff1bea
AH
1109}
1110EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1111
b3ba5149
ES
1112struct mlx5_flow_table*
1113mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1114 int prio,
1115 int num_flow_table_entries,
1116 int max_num_groups,
1117 u32 level,
1118 u32 flags)
f0d22d18 1119{
b3ba5149 1120 struct mlx5_flow_table_attr ft_attr = {};
f0d22d18
MG
1121 struct mlx5_flow_table *ft;
1122
1123 if (max_num_groups > num_flow_table_entries)
1124 return ERR_PTR(-EINVAL);
1125
b3ba5149
ES
1126 ft_attr.max_fte = num_flow_table_entries;
1127 ft_attr.prio = prio;
1128 ft_attr.level = level;
1129 ft_attr.flags = flags;
1130
1131 ft = mlx5_create_flow_table(ns, &ft_attr);
f0d22d18
MG
1132 if (IS_ERR(ft))
1133 return ft;
1134
1135 ft->autogroup.active = true;
1136 ft->autogroup.required_groups = max_num_groups;
97fd8da2
MG
1137 /* We save place for flow groups in addition to max types */
1138 ft->autogroup.group_size = ft->max_fte / (max_num_groups + 1);
f0d22d18
MG
1139
1140 return ft;
1141}
b217ea25 1142EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
f0d22d18 1143
f0d22d18
MG
1144struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1145 u32 *fg_in)
1146{
af76c501 1147 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
0d235c3f
MB
1148 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1149 fg_in, match_criteria);
1150 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1151 fg_in,
1152 match_criteria_enable);
19f100fe
MG
1153 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1154 start_flow_index);
1155 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1156 end_flow_index);
f0d22d18 1157 struct mlx5_flow_group *fg;
19f100fe 1158 int err;
f0d22d18
MG
1159
1160 if (ft->autogroup.active)
1161 return ERR_PTR(-EPERM);
1162
476d61b7 1163 down_write_ref_node(&ft->node, false);
19f100fe
MG
1164 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1165 start_index, end_index,
1166 ft->node.children.prev);
476d61b7 1167 up_write_ref_node(&ft->node, false);
19f100fe
MG
1168 if (IS_ERR(fg))
1169 return fg;
1170
ae288a48 1171 err = root->cmds->create_flow_group(root, ft, fg_in, fg);
19f100fe 1172 if (err) {
476d61b7 1173 tree_put_node(&fg->node, false);
19f100fe
MG
1174 return ERR_PTR(err);
1175 }
1176 trace_mlx5_fs_add_fg(fg);
1177 fg->node.active = true;
0c56b975
MG
1178
1179 return fg;
1180}
1181
1182static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1183{
1184 struct mlx5_flow_rule *rule;
1185
1186 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1187 if (!rule)
1188 return NULL;
1189
b3638e1a 1190 INIT_LIST_HEAD(&rule->next_ft);
0c56b975 1191 rule->node.type = FS_TYPE_FLOW_DEST;
60ab4584
AV
1192 if (dest)
1193 memcpy(&rule->dest_attr, dest, sizeof(*dest));
0c56b975
MG
1194
1195 return rule;
1196}
1197
74491de9
MB
1198static struct mlx5_flow_handle *alloc_handle(int num_rules)
1199{
1200 struct mlx5_flow_handle *handle;
1201
acafe7e3 1202 handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
74491de9
MB
1203 if (!handle)
1204 return NULL;
1205
1206 handle->num_rules = num_rules;
1207
1208 return handle;
1209}
1210
1211static void destroy_flow_handle(struct fs_fte *fte,
1212 struct mlx5_flow_handle *handle,
1213 struct mlx5_flow_destination *dest,
1214 int i)
1215{
1216 for (; --i >= 0;) {
dd8e1945 1217 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
74491de9
MB
1218 fte->dests_size--;
1219 list_del(&handle->rule[i]->node.list);
1220 kfree(handle->rule[i]);
1221 }
1222 }
1223 kfree(handle);
1224}
1225
1226static struct mlx5_flow_handle *
1227create_flow_handle(struct fs_fte *fte,
1228 struct mlx5_flow_destination *dest,
1229 int dest_num,
1230 int *modify_mask,
1231 bool *new_rule)
1232{
1233 struct mlx5_flow_handle *handle;
1234 struct mlx5_flow_rule *rule = NULL;
1235 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1236 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1237 int type;
1238 int i = 0;
1239
1240 handle = alloc_handle((dest_num) ? dest_num : 1);
1241 if (!handle)
1242 return ERR_PTR(-ENOMEM);
1243
1244 do {
1245 if (dest) {
1246 rule = find_flow_rule(fte, dest + i);
1247 if (rule) {
dd8e1945 1248 refcount_inc(&rule->node.refcount);
74491de9
MB
1249 goto rule_found;
1250 }
1251 }
1252
1253 *new_rule = true;
1254 rule = alloc_rule(dest + i);
1255 if (!rule)
1256 goto free_rules;
1257
1258 /* Add dest to dests list- we need flow tables to be in the
1259 * end of the list for forward to next prio rules.
1260 */
bd71b08e 1261 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
74491de9
MB
1262 if (dest &&
1263 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1264 list_add(&rule->node.list, &fte->node.children);
1265 else
1266 list_add_tail(&rule->node.list, &fte->node.children);
1267 if (dest) {
1268 fte->dests_size++;
1269
1270 type = dest[i].type ==
1271 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1272 *modify_mask |= type ? count : dst;
1273 }
1274rule_found:
1275 handle->rule[i] = rule;
1276 } while (++i < dest_num);
1277
1278 return handle;
1279
1280free_rules:
1281 destroy_flow_handle(fte, handle, dest, i);
1282 return ERR_PTR(-ENOMEM);
1283}
1284
0c56b975 1285/* fte should not be deleted while calling this function */
74491de9
MB
1286static struct mlx5_flow_handle *
1287add_rule_fte(struct fs_fte *fte,
1288 struct mlx5_flow_group *fg,
1289 struct mlx5_flow_destination *dest,
1290 int dest_num,
1291 bool update_action)
0c56b975 1292{
af76c501 1293 struct mlx5_flow_root_namespace *root;
74491de9 1294 struct mlx5_flow_handle *handle;
0c56b975 1295 struct mlx5_flow_table *ft;
bd5251db 1296 int modify_mask = 0;
0c56b975 1297 int err;
74491de9 1298 bool new_rule = false;
0c56b975 1299
74491de9
MB
1300 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1301 &new_rule);
1302 if (IS_ERR(handle) || !new_rule)
1303 goto out;
bd5251db 1304
a6224985
MB
1305 if (update_action)
1306 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
bd5251db 1307
0c56b975 1308 fs_get_obj(ft, fg->node.parent);
af76c501 1309 root = find_root(&fg->node);
0501fc47 1310 if (!(fte->status & FS_FTE_STATUS_EXISTING))
ae288a48 1311 err = root->cmds->create_fte(root, ft, fg, fte);
0c56b975 1312 else
ae288a48 1313 err = root->cmds->update_fte(root, ft, fg, modify_mask, fte);
0c56b975 1314 if (err)
74491de9 1315 goto free_handle;
0c56b975 1316
19f100fe 1317 fte->node.active = true;
0c56b975 1318 fte->status |= FS_FTE_STATUS_EXISTING;
bd71b08e 1319 atomic_inc(&fte->node.version);
0c56b975 1320
74491de9
MB
1321out:
1322 return handle;
0c56b975 1323
74491de9
MB
1324free_handle:
1325 destroy_flow_handle(fte, handle, dest, handle->num_rules);
0c56b975
MG
1326 return ERR_PTR(err);
1327}
1328
19f100fe 1329static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
5233794b 1330 const struct mlx5_flow_spec *spec)
0c56b975 1331{
af363705 1332 struct list_head *prev = &ft->node.children;
f0d22d18 1333 struct mlx5_flow_group *fg;
19f100fe 1334 unsigned int candidate_index = 0;
f0d22d18 1335 unsigned int group_size = 0;
f0d22d18
MG
1336
1337 if (!ft->autogroup.active)
1338 return ERR_PTR(-ENOENT);
1339
f0d22d18 1340 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
97fd8da2 1341 group_size = ft->autogroup.group_size;
f0d22d18
MG
1342
1343 /* ft->max_fte == ft->autogroup.max_types */
1344 if (group_size == 0)
1345 group_size = 1;
1346
1347 /* sorted by start_index */
1348 fs_for_each_fg(fg, ft) {
1349 if (candidate_index + group_size > fg->start_index)
1350 candidate_index = fg->start_index + fg->max_ftes;
1351 else
1352 break;
1353 prev = &fg->node.list;
1354 }
1355
19f100fe
MG
1356 if (candidate_index + group_size > ft->max_fte)
1357 return ERR_PTR(-ENOSPC);
1358
1359 fg = alloc_insert_flow_group(ft,
1360 spec->match_criteria_enable,
1361 spec->match_criteria,
1362 candidate_index,
1363 candidate_index + group_size - 1,
1364 prev);
1365 if (IS_ERR(fg))
f0d22d18 1366 goto out;
19f100fe 1367
97fd8da2
MG
1368 if (group_size == ft->autogroup.group_size)
1369 ft->autogroup.num_groups++;
19f100fe
MG
1370
1371out:
1372 return fg;
1373}
1374
1375static int create_auto_flow_group(struct mlx5_flow_table *ft,
1376 struct mlx5_flow_group *fg)
1377{
af76c501 1378 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
19f100fe
MG
1379 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1380 void *match_criteria_addr;
3e99df87
SK
1381 u8 src_esw_owner_mask_on;
1382 void *misc;
19f100fe
MG
1383 int err;
1384 u32 *in;
1385
1386 in = kvzalloc(inlen, GFP_KERNEL);
1387 if (!in)
1388 return -ENOMEM;
f0d22d18
MG
1389
1390 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
19f100fe
MG
1391 fg->mask.match_criteria_enable);
1392 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1393 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1394 fg->max_ftes - 1);
3e99df87
SK
1395
1396 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1397 misc_parameters);
1398 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1399 source_eswitch_owner_vhca_id);
1400 MLX5_SET(create_flow_group_in, in,
1401 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1402
f0d22d18
MG
1403 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1404 in, match_criteria);
19f100fe
MG
1405 memcpy(match_criteria_addr, fg->mask.match_criteria,
1406 sizeof(fg->mask.match_criteria));
1407
ae288a48 1408 err = root->cmds->create_flow_group(root, ft, in, fg);
19f100fe
MG
1409 if (!err) {
1410 fg->node.active = true;
1411 trace_mlx5_fs_add_fg(fg);
1412 }
f0d22d18 1413
f0d22d18 1414 kvfree(in);
19f100fe 1415 return err;
f0d22d18
MG
1416}
1417
814fb875
MB
1418static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1419 struct mlx5_flow_destination *d2)
1420{
1421 if (d1->type == d2->type) {
1422 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1228e912
EB
1423 d1->vport.num == d2->vport.num &&
1424 d1->vport.flags == d2->vport.flags &&
c979c445
DL
1425 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ?
1426 (d1->vport.vhca_id == d2->vport.vhca_id) : true) &&
1228e912 1427 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
2b688ea5
MG
1428 (d1->vport.pkt_reformat->id ==
1429 d2->vport.pkt_reformat->id) : true)) ||
814fb875
MB
1430 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1431 d1->ft == d2->ft) ||
1432 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
664000b6
YH
1433 d1->tir_num == d2->tir_num) ||
1434 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1435 d1->ft_num == d2->ft_num))
814fb875
MB
1436 return true;
1437 }
1438
1439 return false;
1440}
1441
b3638e1a
MG
1442static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1443 struct mlx5_flow_destination *dest)
1444{
1445 struct mlx5_flow_rule *rule;
1446
1447 list_for_each_entry(rule, &fte->node.children, node.list) {
814fb875
MB
1448 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1449 return rule;
b3638e1a
MG
1450 }
1451 return NULL;
1452}
1453
0d235c3f
MB
1454static bool check_conflicting_actions(u32 action1, u32 action2)
1455{
1456 u32 xored_actions = action1 ^ action2;
1457
1458 /* if one rule only wants to count, it's ok */
1459 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1460 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1461 return false;
1462
1463 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
60786f09 1464 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
96de67a7 1465 MLX5_FLOW_CONTEXT_ACTION_DECAP |
0c06897a
OG
1466 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1467 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
8da6fe2a
JL
1468 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1469 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1470 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
0d235c3f
MB
1471 return true;
1472
1473 return false;
1474}
1475
bb0ee7dc
JL
1476static int check_conflicting_ftes(struct fs_fte *fte,
1477 const struct mlx5_flow_context *flow_context,
1478 const struct mlx5_flow_act *flow_act)
0d235c3f 1479{
d2ec6a35 1480 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
0d235c3f
MB
1481 mlx5_core_warn(get_dev(&fte->node),
1482 "Found two FTEs with conflicting actions\n");
1483 return -EEXIST;
1484 }
1485
bb0ee7dc
JL
1486 if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) &&
1487 fte->flow_context.flow_tag != flow_context->flow_tag) {
0d235c3f
MB
1488 mlx5_core_warn(get_dev(&fte->node),
1489 "FTE flow tag %u already exists with different flow tag %u\n",
bb0ee7dc
JL
1490 fte->flow_context.flow_tag,
1491 flow_context->flow_tag);
0d235c3f
MB
1492 return -EEXIST;
1493 }
1494
1495 return 0;
1496}
1497
74491de9 1498static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
5233794b 1499 const struct mlx5_flow_spec *spec,
66958ed9 1500 struct mlx5_flow_act *flow_act,
74491de9 1501 struct mlx5_flow_destination *dest,
693c6883
MB
1502 int dest_num,
1503 struct fs_fte *fte)
0c56b975 1504{
74491de9 1505 struct mlx5_flow_handle *handle;
bd71b08e 1506 int old_action;
74491de9 1507 int i;
bd71b08e 1508 int ret;
0c56b975 1509
bb0ee7dc 1510 ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act);
bd71b08e
MG
1511 if (ret)
1512 return ERR_PTR(ret);
0c56b975 1513
d2ec6a35
MB
1514 old_action = fte->action.action;
1515 fte->action.action |= flow_act->action;
bd71b08e
MG
1516 handle = add_rule_fte(fte, fg, dest, dest_num,
1517 old_action != flow_act->action);
74491de9 1518 if (IS_ERR(handle)) {
d2ec6a35 1519 fte->action.action = old_action;
693c6883 1520 return handle;
0c56b975 1521 }
bd71b08e 1522 trace_mlx5_fs_set_fte(fte, false);
0c56b975 1523
74491de9 1524 for (i = 0; i < handle->num_rules; i++) {
dd8e1945 1525 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
74491de9 1526 tree_add_node(&handle->rule[i]->node, &fte->node);
4c03e69a
MB
1527 trace_mlx5_fs_add_rule(handle->rule[i]);
1528 }
74491de9 1529 }
74491de9 1530 return handle;
0c56b975
MG
1531}
1532
171c7625 1533static bool counter_is_valid(u32 action)
bd5251db 1534{
ae058314 1535 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
eafa6abd 1536 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
bd5251db
AV
1537}
1538
d63cd286
MG
1539static bool dest_is_valid(struct mlx5_flow_destination *dest,
1540 u32 action,
1541 struct mlx5_flow_table *ft)
1542{
bd5251db 1543 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
171c7625 1544 return counter_is_valid(action);
bd5251db 1545
d63cd286
MG
1546 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1547 return true;
1548
1549 if (!dest || ((dest->type ==
1550 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1551 (dest->ft->level <= ft->level)))
1552 return false;
1553 return true;
1554}
1555
46719d77
MG
1556struct match_list {
1557 struct list_head list;
693c6883 1558 struct mlx5_flow_group *g;
46719d77
MG
1559};
1560
1561struct match_list_head {
1562 struct list_head list;
1563 struct match_list first;
1564};
1565
1566static void free_match_list(struct match_list_head *head)
1567{
1568 if (!list_empty(&head->list)) {
1569 struct match_list *iter, *match_tmp;
1570
1571 list_del(&head->first.list);
476d61b7 1572 tree_put_node(&head->first.g->node, false);
46719d77
MG
1573 list_for_each_entry_safe(iter, match_tmp, &head->list,
1574 list) {
476d61b7 1575 tree_put_node(&iter->g->node, false);
46719d77
MG
1576 list_del(&iter->list);
1577 kfree(iter);
1578 }
1579 }
1580}
1581
1582static int build_match_list(struct match_list_head *match_head,
1583 struct mlx5_flow_table *ft,
5233794b 1584 const struct mlx5_flow_spec *spec)
46719d77 1585{
693c6883 1586 struct rhlist_head *tmp, *list;
46719d77
MG
1587 struct mlx5_flow_group *g;
1588 int err = 0;
693c6883
MB
1589
1590 rcu_read_lock();
46719d77 1591 INIT_LIST_HEAD(&match_head->list);
693c6883
MB
1592 /* Collect all fgs which has a matching match_criteria */
1593 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
46719d77 1594 /* RCU is atomic, we can't execute FW commands here */
693c6883
MB
1595 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1596 struct match_list *curr_match;
1597
46719d77 1598 if (likely(list_empty(&match_head->list))) {
bd71b08e
MG
1599 if (!tree_get_node(&g->node))
1600 continue;
46719d77
MG
1601 match_head->first.g = g;
1602 list_add_tail(&match_head->first.list,
1603 &match_head->list);
693c6883
MB
1604 continue;
1605 }
693c6883 1606
46719d77 1607 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
693c6883 1608 if (!curr_match) {
46719d77
MG
1609 free_match_list(match_head);
1610 err = -ENOMEM;
1611 goto out;
693c6883 1612 }
bd71b08e
MG
1613 if (!tree_get_node(&g->node)) {
1614 kfree(curr_match);
1615 continue;
1616 }
693c6883 1617 curr_match->g = g;
46719d77 1618 list_add_tail(&curr_match->list, &match_head->list);
693c6883 1619 }
46719d77 1620out:
693c6883 1621 rcu_read_unlock();
46719d77
MG
1622 return err;
1623}
1624
bd71b08e
MG
1625static u64 matched_fgs_get_version(struct list_head *match_head)
1626{
1627 struct match_list *iter;
1628 u64 version = 0;
1629
1630 list_for_each_entry(iter, match_head, list)
1631 version += (u64)atomic_read(&iter->g->node.version);
1632 return version;
1633}
1634
ad9421e3 1635static struct fs_fte *
7dee607e 1636lookup_fte_for_write_locked(struct mlx5_flow_group *g, const u32 *match_value)
ad9421e3
RD
1637{
1638 struct fs_fte *fte_tmp;
1639
7dee607e
PP
1640 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1641
1642 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value, rhash_fte);
1643 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1644 fte_tmp = NULL;
1645 goto out;
1646 }
1647
1648 if (!fte_tmp->node.active) {
1649 tree_put_node(&fte_tmp->node, false);
1650 fte_tmp = NULL;
1651 goto out;
1652 }
1653 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1654
1655out:
1656 up_write_ref_node(&g->node, false);
1657 return fte_tmp;
1658}
1659
1660static struct fs_fte *
1661lookup_fte_for_read_locked(struct mlx5_flow_group *g, const u32 *match_value)
1662{
1663 struct fs_fte *fte_tmp;
1664
1665 if (!tree_get_node(&g->node))
1666 return NULL;
1667
1668 rcu_read_lock();
1669 fte_tmp = rhashtable_lookup(&g->ftes_hash, match_value, rhash_fte);
ad9421e3 1670 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
7dee607e 1671 rcu_read_unlock();
ad9421e3
RD
1672 fte_tmp = NULL;
1673 goto out;
1674 }
7dee607e
PP
1675 rcu_read_unlock();
1676
6237634d 1677 if (!fte_tmp->node.active) {
476d61b7 1678 tree_put_node(&fte_tmp->node, false);
6237634d
EB
1679 fte_tmp = NULL;
1680 goto out;
1681 }
ad9421e3
RD
1682
1683 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
7dee607e 1684
ad9421e3 1685out:
7dee607e 1686 tree_put_node(&g->node, false);
ad9421e3
RD
1687 return fte_tmp;
1688}
1689
7dee607e
PP
1690static struct fs_fte *
1691lookup_fte_locked(struct mlx5_flow_group *g, const u32 *match_value, bool write)
1692{
1693 if (write)
1694 return lookup_fte_for_write_locked(g, match_value);
1695 else
1696 return lookup_fte_for_read_locked(g, match_value);
1697}
1698
46719d77
MG
1699static struct mlx5_flow_handle *
1700try_add_to_existing_fg(struct mlx5_flow_table *ft,
bd71b08e 1701 struct list_head *match_head,
5233794b 1702 const struct mlx5_flow_spec *spec,
46719d77
MG
1703 struct mlx5_flow_act *flow_act,
1704 struct mlx5_flow_destination *dest,
bd71b08e
MG
1705 int dest_num,
1706 int ft_version)
46719d77 1707{
a369d4ac 1708 struct mlx5_flow_steering *steering = get_steering(&ft->node);
46719d77
MG
1709 struct mlx5_flow_group *g;
1710 struct mlx5_flow_handle *rule;
46719d77 1711 struct match_list *iter;
bd71b08e
MG
1712 bool take_write = false;
1713 struct fs_fte *fte;
1714 u64 version;
f5c2ff17
MG
1715 int err;
1716
bb0ee7dc 1717 fte = alloc_fte(ft, spec, flow_act);
f5c2ff17
MG
1718 if (IS_ERR(fte))
1719 return ERR_PTR(-ENOMEM);
46719d77 1720
bd71b08e
MG
1721search_again_locked:
1722 version = matched_fgs_get_version(match_head);
d5634fee
PB
1723 if (flow_act->flags & FLOW_ACT_NO_APPEND)
1724 goto skip_search;
693c6883 1725 /* Try to find a fg that already contains a matching fte */
bd71b08e
MG
1726 list_for_each_entry(iter, match_head, list) {
1727 struct fs_fte *fte_tmp;
693c6883
MB
1728
1729 g = iter->g;
ad9421e3
RD
1730 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1731 if (!fte_tmp)
bd71b08e 1732 continue;
bb0ee7dc 1733 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp);
476d61b7
EB
1734 up_write_ref_node(&fte_tmp->node, false);
1735 tree_put_node(&fte_tmp->node, false);
a369d4ac 1736 kmem_cache_free(steering->ftes_cache, fte);
bd71b08e 1737 return rule;
693c6883
MB
1738 }
1739
d5634fee
PB
1740skip_search:
1741 /* No group with matching fte found, or we skipped the search.
1742 * Try to add a new fte to any matching fg.
1743 */
1744
bd71b08e
MG
1745 /* Check the ft version, for case that new flow group
1746 * was added while the fgs weren't locked
1747 */
1748 if (atomic_read(&ft->node.version) != ft_version) {
1749 rule = ERR_PTR(-EAGAIN);
1750 goto out;
1751 }
b92af5a7 1752
bd71b08e
MG
1753 /* Check the fgs version, for case the new FTE with the
1754 * same values was added while the fgs weren't locked
1755 */
ad9421e3
RD
1756 if (version != matched_fgs_get_version(match_head)) {
1757 take_write = true;
bd71b08e 1758 goto search_again_locked;
ad9421e3 1759 }
bd71b08e
MG
1760
1761 list_for_each_entry(iter, match_head, list) {
1762 g = iter->g;
1763
1764 if (!g->node.active)
1765 continue;
ad9421e3
RD
1766
1767 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1768
f5c2ff17
MG
1769 err = insert_fte(g, fte);
1770 if (err) {
476d61b7 1771 up_write_ref_node(&g->node, false);
f5c2ff17 1772 if (err == -ENOSPC)
bd71b08e 1773 continue;
a369d4ac 1774 kmem_cache_free(steering->ftes_cache, fte);
f5c2ff17 1775 return ERR_PTR(err);
bd71b08e 1776 }
693c6883 1777
bd71b08e 1778 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
476d61b7 1779 up_write_ref_node(&g->node, false);
bb0ee7dc 1780 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
476d61b7
EB
1781 up_write_ref_node(&fte->node, false);
1782 tree_put_node(&fte->node, false);
bd71b08e
MG
1783 return rule;
1784 }
1785 rule = ERR_PTR(-ENOENT);
1786out:
a369d4ac 1787 kmem_cache_free(steering->ftes_cache, fte);
693c6883
MB
1788 return rule;
1789}
1790
74491de9
MB
1791static struct mlx5_flow_handle *
1792_mlx5_add_flow_rules(struct mlx5_flow_table *ft,
5233794b 1793 const struct mlx5_flow_spec *spec,
66958ed9 1794 struct mlx5_flow_act *flow_act,
74491de9
MB
1795 struct mlx5_flow_destination *dest,
1796 int dest_num)
66958ed9 1797
0c56b975 1798{
a369d4ac 1799 struct mlx5_flow_steering *steering = get_steering(&ft->node);
0c56b975 1800 struct mlx5_flow_group *g;
74491de9 1801 struct mlx5_flow_handle *rule;
bd71b08e
MG
1802 struct match_list_head match_head;
1803 bool take_write = false;
1804 struct fs_fte *fte;
1805 int version;
19f100fe 1806 int err;
74491de9 1807 int i;
0c56b975 1808
693c6883 1809 if (!check_valid_spec(spec))
0d235c3f
MB
1810 return ERR_PTR(-EINVAL);
1811
74491de9 1812 for (i = 0; i < dest_num; i++) {
66958ed9 1813 if (!dest_is_valid(&dest[i], flow_act->action, ft))
74491de9
MB
1814 return ERR_PTR(-EINVAL);
1815 }
bd71b08e
MG
1816 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1817search_again_locked:
1818 version = atomic_read(&ft->node.version);
60ab4584 1819
bd71b08e
MG
1820 /* Collect all fgs which has a matching match_criteria */
1821 err = build_match_list(&match_head, ft, spec);
9238e380
VB
1822 if (err) {
1823 if (take_write)
476d61b7 1824 up_write_ref_node(&ft->node, false);
07130477
RD
1825 else
1826 up_read_ref_node(&ft->node);
bd71b08e 1827 return ERR_PTR(err);
9238e380 1828 }
bd71b08e
MG
1829
1830 if (!take_write)
1831 up_read_ref_node(&ft->node);
1832
1833 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1834 dest_num, version);
1835 free_match_list(&match_head);
1836 if (!IS_ERR(rule) ||
9238e380
VB
1837 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1838 if (take_write)
476d61b7 1839 up_write_ref_node(&ft->node, false);
bd71b08e 1840 return rule;
9238e380 1841 }
bd71b08e
MG
1842
1843 if (!take_write) {
1844 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1845 take_write = true;
1846 }
1847
1848 if (PTR_ERR(rule) == -EAGAIN ||
1849 version != atomic_read(&ft->node.version))
1850 goto search_again_locked;
f0d22d18 1851
19f100fe 1852 g = alloc_auto_flow_group(ft, spec);
c3f9bf62 1853 if (IS_ERR(g)) {
d34c6efc 1854 rule = ERR_CAST(g);
476d61b7 1855 up_write_ref_node(&ft->node, false);
bd71b08e 1856 return rule;
c3f9bf62
MG
1857 }
1858
84c7af63
PP
1859 fte = alloc_fte(ft, spec, flow_act);
1860 if (IS_ERR(fte)) {
1861 up_write_ref_node(&ft->node, false);
1862 err = PTR_ERR(fte);
1863 goto err_alloc_fte;
1864 }
1865
bd71b08e 1866 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
476d61b7 1867 up_write_ref_node(&ft->node, false);
bd71b08e 1868
19f100fe 1869 err = create_auto_flow_group(ft, g);
bd71b08e
MG
1870 if (err)
1871 goto err_release_fg;
1872
f5c2ff17 1873 err = insert_fte(g, fte);
84c7af63 1874 if (err)
f5c2ff17 1875 goto err_release_fg;
f5c2ff17 1876
bd71b08e 1877 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
476d61b7 1878 up_write_ref_node(&g->node, false);
bb0ee7dc 1879 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
476d61b7
EB
1880 up_write_ref_node(&fte->node, false);
1881 tree_put_node(&fte->node, false);
1882 tree_put_node(&g->node, false);
0c56b975 1883 return rule;
bd71b08e
MG
1884
1885err_release_fg:
476d61b7 1886 up_write_ref_node(&g->node, false);
84c7af63
PP
1887 kmem_cache_free(steering->ftes_cache, fte);
1888err_alloc_fte:
476d61b7 1889 tree_put_node(&g->node, false);
bd71b08e 1890 return ERR_PTR(err);
0c56b975 1891}
b3638e1a
MG
1892
1893static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1894{
1895 return ((ft->type == FS_FT_NIC_RX) &&
1896 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1897}
1898
74491de9
MB
1899struct mlx5_flow_handle *
1900mlx5_add_flow_rules(struct mlx5_flow_table *ft,
5233794b 1901 const struct mlx5_flow_spec *spec,
66958ed9 1902 struct mlx5_flow_act *flow_act,
74491de9 1903 struct mlx5_flow_destination *dest,
cf916ffb 1904 int num_dest)
b3638e1a
MG
1905{
1906 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
4c5009c5 1907 struct mlx5_flow_destination gen_dest = {};
b3638e1a 1908 struct mlx5_flow_table *next_ft = NULL;
74491de9 1909 struct mlx5_flow_handle *handle = NULL;
66958ed9 1910 u32 sw_action = flow_act->action;
b3638e1a
MG
1911 struct fs_prio *prio;
1912
1913 fs_get_obj(prio, ft->node.parent);
66958ed9 1914 if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
b3638e1a
MG
1915 if (!fwd_next_prio_supported(ft))
1916 return ERR_PTR(-EOPNOTSUPP);
cf916ffb 1917 if (num_dest)
b3638e1a
MG
1918 return ERR_PTR(-EINVAL);
1919 mutex_lock(&root->chain_lock);
1920 next_ft = find_next_chained_ft(prio);
1921 if (next_ft) {
1922 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1923 gen_dest.ft = next_ft;
1924 dest = &gen_dest;
cf916ffb 1925 num_dest = 1;
66958ed9 1926 flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
b3638e1a
MG
1927 } else {
1928 mutex_unlock(&root->chain_lock);
1929 return ERR_PTR(-EOPNOTSUPP);
1930 }
1931 }
1932
cf916ffb 1933 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
b3638e1a
MG
1934
1935 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
74491de9
MB
1936 if (!IS_ERR_OR_NULL(handle) &&
1937 (list_empty(&handle->rule[0]->next_ft))) {
b3638e1a 1938 mutex_lock(&next_ft->lock);
74491de9
MB
1939 list_add(&handle->rule[0]->next_ft,
1940 &next_ft->fwd_rules);
b3638e1a 1941 mutex_unlock(&next_ft->lock);
74491de9 1942 handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
b3638e1a
MG
1943 }
1944 mutex_unlock(&root->chain_lock);
1945 }
74491de9 1946 return handle;
b3638e1a 1947}
74491de9 1948EXPORT_SYMBOL(mlx5_add_flow_rules);
0c56b975 1949
74491de9 1950void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
0c56b975 1951{
718ce4d6 1952 struct fs_fte *fte;
74491de9
MB
1953 int i;
1954
718ce4d6
EB
1955 /* In order to consolidate the HW changes we lock the FTE for other
1956 * changes, and increase its refcount, in order not to perform the
1957 * "del" functions of the FTE. Will handle them here.
1958 * The removal of the rules is done under locked FTE.
1959 * After removing all the handle's rules, if there are remaining
1960 * rules, it means we just need to modify the FTE in FW, and
1961 * unlock/decrease the refcount we increased before.
1962 * Otherwise, it means the FTE should be deleted. First delete the
1963 * FTE in FW. Then, unlock the FTE, and proceed the tree_put_node of
1964 * the FTE, which will handle the last decrease of the refcount, as
1965 * well as required handling of its parent.
1966 */
1967 fs_get_obj(fte, handle->rule[0]->node.parent);
1968 down_write_ref_node(&fte->node, false);
74491de9 1969 for (i = handle->num_rules - 1; i >= 0; i--)
718ce4d6
EB
1970 tree_remove_node(&handle->rule[i]->node, true);
1971 if (fte->modify_mask && fte->dests_size) {
1972 modify_fte(fte);
1973 up_write_ref_node(&fte->node, false);
1974 } else {
1975 del_hw_fte(&fte->node);
1976 up_write(&fte->node.lock);
1977 tree_put_node(&fte->node, false);
1978 }
74491de9 1979 kfree(handle);
0c56b975 1980}
74491de9 1981EXPORT_SYMBOL(mlx5_del_flow_rules);
0c56b975 1982
2cc43b49
MG
1983/* Assuming prio->node.children(flow tables) is sorted by level */
1984static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1985{
1986 struct fs_prio *prio;
1987
1988 fs_get_obj(prio, ft->node.parent);
1989
1990 if (!list_is_last(&ft->node.list, &prio->node.children))
1991 return list_next_entry(ft, node.list);
1992 return find_next_chained_ft(prio);
1993}
1994
1995static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1996{
1997 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
dae37456 1998 struct mlx5_ft_underlay_qp *uqp;
2cc43b49 1999 struct mlx5_flow_table *new_root_ft = NULL;
dae37456
AV
2000 int err = 0;
2001 u32 qpn;
2cc43b49
MG
2002
2003 if (root->root_ft != ft)
2004 return 0;
2005
2006 new_root_ft = find_next_ft(ft);
dae37456
AV
2007 if (!new_root_ft) {
2008 root->root_ft = NULL;
2009 return 0;
2010 }
2011
2012 if (list_empty(&root->underlay_qpns)) {
2013 /* Don't set any QPN (zero) in case QPN list is empty */
2014 qpn = 0;
ae288a48 2015 err = root->cmds->update_root_ft(root, new_root_ft,
af76c501 2016 qpn, false);
dae37456
AV
2017 } else {
2018 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2019 qpn = uqp->qpn;
ae288a48 2020 err = root->cmds->update_root_ft(root,
af76c501
MB
2021 new_root_ft, qpn,
2022 false);
dae37456
AV
2023 if (err)
2024 break;
2cc43b49 2025 }
2cc43b49 2026 }
dae37456
AV
2027
2028 if (err)
2029 mlx5_core_warn(root->dev,
2030 "Update root flow table of id(%u) qpn(%d) failed\n",
2031 ft->id, qpn);
2032 else
2033 root->root_ft = new_root_ft;
2034
2cc43b49
MG
2035 return 0;
2036}
2037
f90edfd2
MG
2038/* Connect flow table from previous priority to
2039 * the next flow table.
2040 */
2041static int disconnect_flow_table(struct mlx5_flow_table *ft)
2042{
2043 struct mlx5_core_dev *dev = get_dev(&ft->node);
2044 struct mlx5_flow_table *next_ft;
2045 struct fs_prio *prio;
2046 int err = 0;
2047
2048 err = update_root_ft_destroy(ft);
2049 if (err)
2050 return err;
2051
2052 fs_get_obj(prio, ft->node.parent);
2053 if (!(list_first_entry(&prio->node.children,
2054 struct mlx5_flow_table,
2055 node.list) == ft))
2056 return 0;
2057
2058 next_ft = find_next_chained_ft(prio);
b3638e1a
MG
2059 err = connect_fwd_rules(dev, next_ft, ft);
2060 if (err)
2061 return err;
2062
f90edfd2
MG
2063 err = connect_prev_fts(dev, next_ft, prio);
2064 if (err)
2065 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2066 ft->id);
2067 return err;
2068}
2069
86d722ad 2070int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
0c56b975 2071{
2cc43b49
MG
2072 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2073 int err = 0;
2074
2075 mutex_lock(&root->chain_lock);
f90edfd2 2076 err = disconnect_flow_table(ft);
2cc43b49
MG
2077 if (err) {
2078 mutex_unlock(&root->chain_lock);
2079 return err;
2080 }
476d61b7 2081 if (tree_remove_node(&ft->node, false))
0c56b975
MG
2082 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2083 ft->id);
2cc43b49 2084 mutex_unlock(&root->chain_lock);
0c56b975 2085
2cc43b49 2086 return err;
0c56b975 2087}
b217ea25 2088EXPORT_SYMBOL(mlx5_destroy_flow_table);
0c56b975 2089
86d722ad 2090void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
0c56b975 2091{
476d61b7 2092 if (tree_remove_node(&fg->node, false))
0c56b975
MG
2093 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2094 fg->id);
2095}
25302363 2096
328edb49
PB
2097struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
2098 int n)
2099{
2100 struct mlx5_flow_steering *steering = dev->priv.steering;
2101
2102 if (!steering || !steering->fdb_sub_ns)
2103 return NULL;
2104
2105 return steering->fdb_sub_ns[n];
2106}
2107EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
2108
86d722ad
MG
2109struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2110 enum mlx5_flow_namespace_type type)
25302363 2111{
fba53f7b
MG
2112 struct mlx5_flow_steering *steering = dev->priv.steering;
2113 struct mlx5_flow_root_namespace *root_ns;
8ce78257 2114 int prio = 0;
78228cbd 2115 struct fs_prio *fs_prio;
25302363
MG
2116 struct mlx5_flow_namespace *ns;
2117
fba53f7b 2118 if (!steering)
25302363
MG
2119 return NULL;
2120
2121 switch (type) {
25302363 2122 case MLX5_FLOW_NAMESPACE_FDB:
fba53f7b
MG
2123 if (steering->fdb_root_ns)
2124 return &steering->fdb_root_ns->ns;
2226dcb4 2125 return NULL;
87d22483
MG
2126 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2127 if (steering->sniffer_rx_root_ns)
2128 return &steering->sniffer_rx_root_ns->ns;
2226dcb4 2129 return NULL;
87d22483
MG
2130 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2131 if (steering->sniffer_tx_root_ns)
2132 return &steering->sniffer_tx_root_ns->ns;
25302363 2133 return NULL;
2226dcb4
MB
2134 default:
2135 break;
25302363
MG
2136 }
2137
8ce78257
MB
2138 if (type == MLX5_FLOW_NAMESPACE_EGRESS) {
2139 root_ns = steering->egress_root_ns;
e6806e9a
MZ
2140 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
2141 root_ns = steering->rdma_rx_root_ns;
2142 prio = RDMA_RX_BYPASS_PRIO;
2143 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL) {
2144 root_ns = steering->rdma_rx_root_ns;
2145 prio = RDMA_RX_KERNEL_PRIO;
8ce78257
MB
2146 } else { /* Must be NIC RX */
2147 root_ns = steering->root_ns;
2148 prio = type;
25302363
MG
2149 }
2150
fba53f7b
MG
2151 if (!root_ns)
2152 return NULL;
2153
25302363
MG
2154 fs_prio = find_prio(&root_ns->ns, prio);
2155 if (!fs_prio)
2156 return NULL;
2157
2158 ns = list_first_entry(&fs_prio->node.children,
2159 typeof(*ns),
2160 node.list);
2161
2162 return ns;
2163}
b217ea25 2164EXPORT_SYMBOL(mlx5_get_flow_namespace);
25302363 2165
9b93ab98
GP
2166struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2167 enum mlx5_flow_namespace_type type,
2168 int vport)
2169{
2170 struct mlx5_flow_steering *steering = dev->priv.steering;
2171
2752b823 2172 if (!steering || vport >= mlx5_eswitch_get_total_vports(dev))
9b93ab98
GP
2173 return NULL;
2174
2175 switch (type) {
2176 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2177 if (steering->esw_egress_root_ns &&
2178 steering->esw_egress_root_ns[vport])
2179 return &steering->esw_egress_root_ns[vport]->ns;
2180 else
2181 return NULL;
2182 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2183 if (steering->esw_ingress_root_ns &&
2184 steering->esw_ingress_root_ns[vport])
2185 return &steering->esw_ingress_root_ns[vport]->ns;
2186 else
2187 return NULL;
2188 default:
2189 return NULL;
2190 }
2191}
2192
328edb49
PB
2193static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2194 unsigned int prio,
2195 int num_levels,
2196 enum fs_node_type type)
25302363
MG
2197{
2198 struct fs_prio *fs_prio;
2199
2200 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2201 if (!fs_prio)
2202 return ERR_PTR(-ENOMEM);
2203
328edb49 2204 fs_prio->node.type = type;
139ed6c6 2205 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
25302363 2206 tree_add_node(&fs_prio->node, &ns->node);
a257b94a 2207 fs_prio->num_levels = num_levels;
25302363 2208 fs_prio->prio = prio;
25302363
MG
2209 list_add_tail(&fs_prio->node.list, &ns->node.children);
2210
2211 return fs_prio;
2212}
2213
328edb49
PB
2214static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2215 unsigned int prio,
2216 int num_levels)
2217{
2218 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2219}
2220
2221static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2222 unsigned int prio, int num_levels)
2223{
2224 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2225}
2226
25302363
MG
2227static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2228 *ns)
2229{
2230 ns->node.type = FS_TYPE_NAMESPACE;
2231
2232 return ns;
2233}
2234
f66ad830
MZ
2235static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2236 int def_miss_act)
25302363
MG
2237{
2238 struct mlx5_flow_namespace *ns;
2239
2240 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2241 if (!ns)
2242 return ERR_PTR(-ENOMEM);
2243
2244 fs_init_namespace(ns);
f66ad830 2245 ns->def_miss_action = def_miss_act;
139ed6c6 2246 tree_init_node(&ns->node, NULL, del_sw_ns);
25302363
MG
2247 tree_add_node(&ns->node, &prio->node);
2248 list_add_tail(&ns->node.list, &prio->node.children);
2249
2250 return ns;
2251}
2252
13de6c10
MG
2253static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2254 struct init_tree_node *prio_metadata)
4cbdd30e
MG
2255{
2256 struct fs_prio *fs_prio;
2257 int i;
2258
2259 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
13de6c10 2260 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
4cbdd30e
MG
2261 if (IS_ERR(fs_prio))
2262 return PTR_ERR(fs_prio);
2263 }
2264 return 0;
2265}
2266
8d40d162
MG
2267#define FLOW_TABLE_BIT_SZ 1
2268#define GET_FLOW_TABLE_CAP(dev, offset) \
701052c5 2269 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
8d40d162
MG
2270 offset / 32)) >> \
2271 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2272static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2273{
2274 int i;
2275
2276 for (i = 0; i < caps->arr_sz; i++) {
2277 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2278 return false;
2279 }
2280 return true;
2281}
2282
fba53f7b 2283static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
8d40d162 2284 struct init_tree_node *init_node,
25302363
MG
2285 struct fs_node *fs_parent_node,
2286 struct init_tree_node *init_parent_node,
13de6c10 2287 int prio)
25302363 2288{
fba53f7b 2289 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
8d40d162
MG
2290 flow_table_properties_nic_receive.
2291 max_ft_level);
25302363
MG
2292 struct mlx5_flow_namespace *fs_ns;
2293 struct fs_prio *fs_prio;
2294 struct fs_node *base;
2295 int i;
2296 int err;
2297
2298 if (init_node->type == FS_TYPE_PRIO) {
8d40d162 2299 if ((init_node->min_ft_level > max_ft_level) ||
fba53f7b 2300 !has_required_caps(steering->dev, &init_node->caps))
8d40d162 2301 return 0;
25302363
MG
2302
2303 fs_get_obj(fs_ns, fs_parent_node);
4cbdd30e 2304 if (init_node->num_leaf_prios)
13de6c10
MG
2305 return create_leaf_prios(fs_ns, prio, init_node);
2306 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
25302363
MG
2307 if (IS_ERR(fs_prio))
2308 return PTR_ERR(fs_prio);
2309 base = &fs_prio->node;
2310 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2311 fs_get_obj(fs_prio, fs_parent_node);
f66ad830 2312 fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
25302363
MG
2313 if (IS_ERR(fs_ns))
2314 return PTR_ERR(fs_ns);
2315 base = &fs_ns->node;
2316 } else {
2317 return -EINVAL;
2318 }
13de6c10 2319 prio = 0;
25302363 2320 for (i = 0; i < init_node->ar_size; i++) {
fba53f7b 2321 err = init_root_tree_recursive(steering, &init_node->children[i],
13de6c10 2322 base, init_node, prio);
25302363
MG
2323 if (err)
2324 return err;
13de6c10
MG
2325 if (init_node->children[i].type == FS_TYPE_PRIO &&
2326 init_node->children[i].num_leaf_prios) {
2327 prio += init_node->children[i].num_leaf_prios;
2328 }
25302363
MG
2329 }
2330
2331 return 0;
2332}
2333
fba53f7b 2334static int init_root_tree(struct mlx5_flow_steering *steering,
8d40d162 2335 struct init_tree_node *init_node,
25302363
MG
2336 struct fs_node *fs_parent_node)
2337{
2338 int i;
2339 struct mlx5_flow_namespace *fs_ns;
2340 int err;
2341
2342 fs_get_obj(fs_ns, fs_parent_node);
2343 for (i = 0; i < init_node->ar_size; i++) {
fba53f7b 2344 err = init_root_tree_recursive(steering, &init_node->children[i],
25302363
MG
2345 &fs_ns->node,
2346 init_node, i);
2347 if (err)
2348 return err;
2349 }
2350 return 0;
2351}
2352
af76c501
MB
2353static struct mlx5_flow_root_namespace
2354*create_root_ns(struct mlx5_flow_steering *steering,
2355 enum fs_flow_table_type table_type)
25302363 2356{
af76c501 2357 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
25302363
MG
2358 struct mlx5_flow_root_namespace *root_ns;
2359 struct mlx5_flow_namespace *ns;
2360
05564d0a
AY
2361 if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2362 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2363 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2364
86d722ad 2365 /* Create the root namespace */
25fa506b 2366 root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL);
25302363
MG
2367 if (!root_ns)
2368 return NULL;
2369
fba53f7b 2370 root_ns->dev = steering->dev;
25302363 2371 root_ns->table_type = table_type;
af76c501 2372 root_ns->cmds = cmds;
25302363 2373
dae37456
AV
2374 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2375
25302363
MG
2376 ns = &root_ns->ns;
2377 fs_init_namespace(ns);
2cc43b49 2378 mutex_init(&root_ns->chain_lock);
bd71b08e 2379 tree_init_node(&ns->node, NULL, NULL);
25302363
MG
2380 tree_add_node(&ns->node, NULL);
2381
2382 return root_ns;
2383}
2384
655227ed
MG
2385static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2386
2387static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2388{
2389 struct fs_prio *prio;
2390
2391 fs_for_each_prio(prio, ns) {
a257b94a 2392 /* This updates prio start_level and num_levels */
655227ed 2393 set_prio_attrs_in_prio(prio, acc_level);
a257b94a 2394 acc_level += prio->num_levels;
655227ed
MG
2395 }
2396 return acc_level;
2397}
2398
2399static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2400{
2401 struct mlx5_flow_namespace *ns;
2402 int acc_level_ns = acc_level;
2403
2404 prio->start_level = acc_level;
34b13cb3 2405 fs_for_each_ns(ns, prio) {
a257b94a 2406 /* This updates start_level and num_levels of ns's priority descendants */
655227ed 2407 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
34b13cb3
PB
2408
2409 /* If this a prio with chains, and we can jump from one chain
2410 * (namepsace) to another, so we accumulate the levels
2411 */
2412 if (prio->node.type == FS_TYPE_PRIO_CHAINS)
2413 acc_level = acc_level_ns;
2414 }
2415
a257b94a
MG
2416 if (!prio->num_levels)
2417 prio->num_levels = acc_level_ns - prio->start_level;
2418 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
655227ed
MG
2419}
2420
2421static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2422{
2423 struct mlx5_flow_namespace *ns = &root_ns->ns;
2424 struct fs_prio *prio;
2425 int start_level = 0;
2426
2427 fs_for_each_prio(prio, ns) {
2428 set_prio_attrs_in_prio(prio, start_level);
a257b94a 2429 start_level += prio->num_levels;
655227ed
MG
2430 }
2431}
2432
153fefbf
MG
2433#define ANCHOR_PRIO 0
2434#define ANCHOR_SIZE 1
d63cd286 2435#define ANCHOR_LEVEL 0
fba53f7b 2436static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
153fefbf
MG
2437{
2438 struct mlx5_flow_namespace *ns = NULL;
b3ba5149 2439 struct mlx5_flow_table_attr ft_attr = {};
153fefbf
MG
2440 struct mlx5_flow_table *ft;
2441
fba53f7b 2442 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
eff596da 2443 if (WARN_ON(!ns))
153fefbf 2444 return -EINVAL;
b3ba5149
ES
2445
2446 ft_attr.max_fte = ANCHOR_SIZE;
2447 ft_attr.level = ANCHOR_LEVEL;
2448 ft_attr.prio = ANCHOR_PRIO;
2449
2450 ft = mlx5_create_flow_table(ns, &ft_attr);
153fefbf 2451 if (IS_ERR(ft)) {
fba53f7b 2452 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
153fefbf
MG
2453 return PTR_ERR(ft);
2454 }
2455 return 0;
2456}
2457
fba53f7b 2458static int init_root_ns(struct mlx5_flow_steering *steering)
25302363 2459{
9c26f5f8
TB
2460 int err;
2461
fba53f7b 2462 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
42fb18fd 2463 if (!steering->root_ns)
9c26f5f8 2464 return -ENOMEM;
25302363 2465
9c26f5f8
TB
2466 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2467 if (err)
2468 goto out_err;
25302363 2469
fba53f7b 2470 set_prio_attrs(steering->root_ns);
9c26f5f8
TB
2471 err = create_anchor_flow_table(steering);
2472 if (err)
2473 goto out_err;
153fefbf 2474
25302363
MG
2475 return 0;
2476
9c26f5f8
TB
2477out_err:
2478 cleanup_root_ns(steering->root_ns);
2479 steering->root_ns = NULL;
2480 return err;
25302363
MG
2481}
2482
0da2d666 2483static void clean_tree(struct fs_node *node)
25302363 2484{
0da2d666
MG
2485 if (node) {
2486 struct fs_node *iter;
2487 struct fs_node *temp;
25302363 2488
800350a3 2489 tree_get_node(node);
0da2d666
MG
2490 list_for_each_entry_safe(iter, temp, &node->children, list)
2491 clean_tree(iter);
476d61b7
EB
2492 tree_put_node(node, false);
2493 tree_remove_node(node, false);
25302363 2494 }
153fefbf
MG
2495}
2496
0da2d666 2497static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
25302363 2498{
25302363
MG
2499 if (!root_ns)
2500 return;
2501
0da2d666 2502 clean_tree(&root_ns->ns.node);
25302363
MG
2503}
2504
9b93ab98
GP
2505static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2506{
2507 struct mlx5_flow_steering *steering = dev->priv.steering;
2508 int i;
2509
2510 if (!steering->esw_egress_root_ns)
2511 return;
2512
2752b823 2513 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++)
9b93ab98
GP
2514 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2515
2516 kfree(steering->esw_egress_root_ns);
9414277a 2517 steering->esw_egress_root_ns = NULL;
9b93ab98
GP
2518}
2519
2520static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2521{
2522 struct mlx5_flow_steering *steering = dev->priv.steering;
2523 int i;
2524
2525 if (!steering->esw_ingress_root_ns)
2526 return;
2527
2752b823 2528 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++)
9b93ab98
GP
2529 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2530
2531 kfree(steering->esw_ingress_root_ns);
9414277a 2532 steering->esw_ingress_root_ns = NULL;
9b93ab98
GP
2533}
2534
25302363
MG
2535void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2536{
fba53f7b
MG
2537 struct mlx5_flow_steering *steering = dev->priv.steering;
2538
0da2d666 2539 cleanup_root_ns(steering->root_ns);
9b93ab98
GP
2540 cleanup_egress_acls_root_ns(dev);
2541 cleanup_ingress_acls_root_ns(dev);
0da2d666 2542 cleanup_root_ns(steering->fdb_root_ns);
328edb49
PB
2543 steering->fdb_root_ns = NULL;
2544 kfree(steering->fdb_sub_ns);
2545 steering->fdb_sub_ns = NULL;
87d22483
MG
2546 cleanup_root_ns(steering->sniffer_rx_root_ns);
2547 cleanup_root_ns(steering->sniffer_tx_root_ns);
d83eb50e 2548 cleanup_root_ns(steering->rdma_rx_root_ns);
5f418378 2549 cleanup_root_ns(steering->egress_root_ns);
43a335e0 2550 mlx5_cleanup_fc_stats(dev);
a369d4ac
MG
2551 kmem_cache_destroy(steering->ftes_cache);
2552 kmem_cache_destroy(steering->fgs_cache);
fba53f7b 2553 kfree(steering);
25302363
MG
2554}
2555
87d22483
MG
2556static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2557{
2558 struct fs_prio *prio;
2559
2560 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2561 if (!steering->sniffer_tx_root_ns)
2562 return -ENOMEM;
2563
2564 /* Create single prio */
2565 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
905f6bd3 2566 return PTR_ERR_OR_ZERO(prio);
87d22483
MG
2567}
2568
2569static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2570{
2571 struct fs_prio *prio;
2572
2573 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2574 if (!steering->sniffer_rx_root_ns)
2575 return -ENOMEM;
2576
2577 /* Create single prio */
2578 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
905f6bd3 2579 return PTR_ERR_OR_ZERO(prio);
87d22483
MG
2580}
2581
d83eb50e
MG
2582static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
2583{
e6806e9a 2584 int err;
d83eb50e
MG
2585
2586 steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
2587 if (!steering->rdma_rx_root_ns)
2588 return -ENOMEM;
2589
e6806e9a
MZ
2590 err = init_root_tree(steering, &rdma_rx_root_fs,
2591 &steering->rdma_rx_root_ns->ns.node);
2592 if (err)
2593 goto out_err;
f6f7d6b5 2594
e6806e9a
MZ
2595 set_prio_attrs(steering->rdma_rx_root_ns);
2596
2597 return 0;
2598
2599out_err:
2600 cleanup_root_ns(steering->rdma_rx_root_ns);
2601 steering->rdma_rx_root_ns = NULL;
2602 return err;
d83eb50e 2603}
439e843f
PB
2604
2605/* FT and tc chains are stored in the same array so we can re-use the
2606 * mlx5_get_fdb_sub_ns() and tc api for FT chains.
2607 * When creating a new ns for each chain store it in the first available slot.
2608 * Assume tc chains are created and stored first and only then the FT chain.
2609 */
2610static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2611 struct mlx5_flow_namespace *ns)
2612{
2613 int chain = 0;
2614
2615 while (steering->fdb_sub_ns[chain])
2616 ++chain;
2617
2618 steering->fdb_sub_ns[chain] = ns;
2619}
2620
2621static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2622 struct fs_prio *maj_prio)
25302363 2623{
328edb49 2624 struct mlx5_flow_namespace *ns;
328edb49 2625 struct fs_prio *min_prio;
439e843f
PB
2626 int prio;
2627
2628 ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
2629 if (IS_ERR(ns))
2630 return PTR_ERR(ns);
2631
2632 for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
2633 min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
2634 if (IS_ERR(min_prio))
2635 return PTR_ERR(min_prio);
2636 }
2637
2638 store_fdb_sub_ns_prio_chain(steering, ns);
2639
2640 return 0;
2641}
2642
2643static int create_fdb_chains(struct mlx5_flow_steering *steering,
2644 int fs_prio,
2645 int chains)
2646{
2647 struct fs_prio *maj_prio;
328edb49
PB
2648 int levels;
2649 int chain;
328edb49 2650 int err;
25302363 2651
439e843f
PB
2652 levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
2653 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
2654 fs_prio,
2655 levels);
2656 if (IS_ERR(maj_prio))
2657 return PTR_ERR(maj_prio);
2658
2659 for (chain = 0; chain < chains; chain++) {
2660 err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
2661 if (err)
2662 return err;
2663 }
2664
2665 return 0;
2666}
25302363 2667
439e843f
PB
2668static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
2669{
439e843f
PB
2670 int err;
2671
975b992f 2672 steering->fdb_sub_ns = kcalloc(FDB_NUM_CHAINS,
439e843f
PB
2673 sizeof(*steering->fdb_sub_ns),
2674 GFP_KERNEL);
328edb49
PB
2675 if (!steering->fdb_sub_ns)
2676 return -ENOMEM;
2677
975b992f
PB
2678 err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
2679 if (err)
2680 return err;
2681
2682 err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
439e843f
PB
2683 if (err)
2684 return err;
2685
2686 return 0;
2687}
2688
2689static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2690{
2691 struct fs_prio *maj_prio;
2692 int err;
2693
2694 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2695 if (!steering->fdb_root_ns)
2696 return -ENOMEM;
2697
d9cb0675
MB
2698 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH,
2699 1);
2700 if (IS_ERR(maj_prio)) {
2701 err = PTR_ERR(maj_prio);
2702 goto out_err;
2703 }
439e843f
PB
2704 err = create_fdb_fast_path(steering);
2705 if (err)
1033665e 2706 goto out_err;
328edb49 2707
b6d9ccb1 2708 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
328edb49
PB
2709 if (IS_ERR(maj_prio)) {
2710 err = PTR_ERR(maj_prio);
1033665e 2711 goto out_err;
328edb49 2712 }
1033665e
OG
2713
2714 set_prio_attrs(steering->fdb_root_ns);
2715 return 0;
2716
2717out_err:
2718 cleanup_root_ns(steering->fdb_root_ns);
328edb49
PB
2719 kfree(steering->fdb_sub_ns);
2720 steering->fdb_sub_ns = NULL;
1033665e 2721 steering->fdb_root_ns = NULL;
328edb49 2722 return err;
25302363
MG
2723}
2724
9b93ab98 2725static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
efdc810b
MHY
2726{
2727 struct fs_prio *prio;
2728
9b93ab98
GP
2729 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2730 if (!steering->esw_egress_root_ns[vport])
efdc810b
MHY
2731 return -ENOMEM;
2732
2733 /* create 1 prio*/
9b93ab98 2734 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
44fafdaa 2735 return PTR_ERR_OR_ZERO(prio);
efdc810b
MHY
2736}
2737
9b93ab98 2738static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
efdc810b
MHY
2739{
2740 struct fs_prio *prio;
2741
9b93ab98
GP
2742 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2743 if (!steering->esw_ingress_root_ns[vport])
efdc810b
MHY
2744 return -ENOMEM;
2745
2746 /* create 1 prio*/
9b93ab98 2747 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
44fafdaa 2748 return PTR_ERR_OR_ZERO(prio);
efdc810b
MHY
2749}
2750
9b93ab98
GP
2751static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2752{
2753 struct mlx5_flow_steering *steering = dev->priv.steering;
2752b823 2754 int total_vports = mlx5_eswitch_get_total_vports(dev);
9b93ab98
GP
2755 int err;
2756 int i;
2757
2752b823
PP
2758 steering->esw_egress_root_ns =
2759 kcalloc(total_vports,
2760 sizeof(*steering->esw_egress_root_ns),
2761 GFP_KERNEL);
9b93ab98
GP
2762 if (!steering->esw_egress_root_ns)
2763 return -ENOMEM;
2764
2752b823 2765 for (i = 0; i < total_vports; i++) {
9b93ab98
GP
2766 err = init_egress_acl_root_ns(steering, i);
2767 if (err)
2768 goto cleanup_root_ns;
2769 }
2770
2771 return 0;
2772
2773cleanup_root_ns:
2774 for (i--; i >= 0; i--)
2775 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2776 kfree(steering->esw_egress_root_ns);
9414277a 2777 steering->esw_egress_root_ns = NULL;
9b93ab98
GP
2778 return err;
2779}
2780
2781static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2782{
2783 struct mlx5_flow_steering *steering = dev->priv.steering;
2752b823 2784 int total_vports = mlx5_eswitch_get_total_vports(dev);
9b93ab98
GP
2785 int err;
2786 int i;
2787
2752b823
PP
2788 steering->esw_ingress_root_ns =
2789 kcalloc(total_vports,
2790 sizeof(*steering->esw_ingress_root_ns),
2791 GFP_KERNEL);
9b93ab98
GP
2792 if (!steering->esw_ingress_root_ns)
2793 return -ENOMEM;
2794
2752b823 2795 for (i = 0; i < total_vports; i++) {
9b93ab98
GP
2796 err = init_ingress_acl_root_ns(steering, i);
2797 if (err)
2798 goto cleanup_root_ns;
2799 }
2800
2801 return 0;
2802
2803cleanup_root_ns:
2804 for (i--; i >= 0; i--)
2805 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2806 kfree(steering->esw_ingress_root_ns);
9414277a 2807 steering->esw_ingress_root_ns = NULL;
9b93ab98
GP
2808 return err;
2809}
2810
5f418378
AY
2811static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2812{
8ce78257 2813 int err;
5f418378
AY
2814
2815 steering->egress_root_ns = create_root_ns(steering,
2816 FS_FT_NIC_TX);
2817 if (!steering->egress_root_ns)
2818 return -ENOMEM;
2819
8ce78257
MB
2820 err = init_root_tree(steering, &egress_root_fs,
2821 &steering->egress_root_ns->ns.node);
2822 if (err)
2823 goto cleanup;
2824 set_prio_attrs(steering->egress_root_ns);
2825 return 0;
2826cleanup:
2827 cleanup_root_ns(steering->egress_root_ns);
2828 steering->egress_root_ns = NULL;
2829 return err;
5f418378
AY
2830}
2831
25302363
MG
2832int mlx5_init_fs(struct mlx5_core_dev *dev)
2833{
fba53f7b 2834 struct mlx5_flow_steering *steering;
25302363
MG
2835 int err = 0;
2836
43a335e0
AV
2837 err = mlx5_init_fc_stats(dev);
2838 if (err)
2839 return err;
2840
fba53f7b
MG
2841 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2842 if (!steering)
2843 return -ENOMEM;
2844 steering->dev = dev;
2845 dev->priv.steering = steering;
2846
a369d4ac
MG
2847 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2848 sizeof(struct mlx5_flow_group), 0,
2849 0, NULL);
2850 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2851 0, NULL);
2852 if (!steering->ftes_cache || !steering->fgs_cache) {
2853 err = -ENOMEM;
2854 goto err;
2855 }
2856
ffdb8827
ES
2857 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2858 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2859 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2860 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
876d634d 2861 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
fba53f7b 2862 err = init_root_ns(steering);
25302363 2863 if (err)
43a335e0 2864 goto err;
25302363 2865 }
876d634d 2866
0efc8562 2867 if (MLX5_ESWITCH_MANAGER(dev)) {
bd02ef8e 2868 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
fba53f7b 2869 err = init_fdb_root_ns(steering);
bd02ef8e
MG
2870 if (err)
2871 goto err;
2872 }
2873 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
9b93ab98 2874 err = init_egress_acls_root_ns(dev);
bd02ef8e
MG
2875 if (err)
2876 goto err;
2877 }
2878 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
9b93ab98 2879 err = init_ingress_acls_root_ns(dev);
bd02ef8e
MG
2880 if (err)
2881 goto err;
2882 }
25302363
MG
2883 }
2884
87d22483
MG
2885 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2886 err = init_sniffer_rx_root_ns(steering);
2887 if (err)
2888 goto err;
2889 }
2890
2891 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2892 err = init_sniffer_tx_root_ns(steering);
2893 if (err)
2894 goto err;
2895 }
2896
f6f7d6b5
MG
2897 if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) &&
2898 MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)) {
d83eb50e
MG
2899 err = init_rdma_rx_root_ns(steering);
2900 if (err)
2901 goto err;
2902 }
2903
8ce78257 2904 if (MLX5_IPSEC_DEV(dev) || MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
5f418378
AY
2905 err = init_egress_root_ns(steering);
2906 if (err)
2907 goto err;
2908 }
2909
efdc810b
MHY
2910 return 0;
2911err:
2912 mlx5_cleanup_fs(dev);
25302363
MG
2913 return err;
2914}
50854114
YH
2915
2916int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2917{
2918 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
dae37456
AV
2919 struct mlx5_ft_underlay_qp *new_uqp;
2920 int err = 0;
2921
2922 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2923 if (!new_uqp)
2924 return -ENOMEM;
2925
2926 mutex_lock(&root->chain_lock);
2927
2928 if (!root->root_ft) {
2929 err = -EINVAL;
2930 goto update_ft_fail;
2931 }
2932
ae288a48 2933 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
af76c501 2934 false);
dae37456
AV
2935 if (err) {
2936 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2937 underlay_qpn, err);
2938 goto update_ft_fail;
2939 }
2940
2941 new_uqp->qpn = underlay_qpn;
2942 list_add_tail(&new_uqp->list, &root->underlay_qpns);
2943
2944 mutex_unlock(&root->chain_lock);
50854114 2945
50854114 2946 return 0;
dae37456
AV
2947
2948update_ft_fail:
2949 mutex_unlock(&root->chain_lock);
2950 kfree(new_uqp);
2951 return err;
50854114
YH
2952}
2953EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2954
2955int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2956{
2957 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
dae37456
AV
2958 struct mlx5_ft_underlay_qp *uqp;
2959 bool found = false;
2960 int err = 0;
2961
2962 mutex_lock(&root->chain_lock);
2963 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2964 if (uqp->qpn == underlay_qpn) {
2965 found = true;
2966 break;
2967 }
2968 }
2969
2970 if (!found) {
2971 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2972 underlay_qpn);
2973 err = -EINVAL;
2974 goto out;
2975 }
2976
ae288a48 2977 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
af76c501 2978 true);
dae37456
AV
2979 if (err)
2980 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2981 underlay_qpn, err);
2982
2983 list_del(&uqp->list);
2984 mutex_unlock(&root->chain_lock);
2985 kfree(uqp);
50854114 2986
50854114 2987 return 0;
dae37456
AV
2988
2989out:
2990 mutex_unlock(&root->chain_lock);
2991 return err;
50854114
YH
2992}
2993EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
2b688ea5
MG
2994
2995static struct mlx5_flow_root_namespace
2996*get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
2997{
2998 struct mlx5_flow_namespace *ns;
2999
3000 if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
3001 ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS)
3002 ns = mlx5_get_flow_vport_acl_namespace(dev, ns_type, 0);
3003 else
3004 ns = mlx5_get_flow_namespace(dev, ns_type);
3005 if (!ns)
3006 return NULL;
3007
3008 return find_root(&ns->node);
3009}
3010
3011struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
3012 u8 ns_type, u8 num_actions,
3013 void *modify_actions)
3014{
3015 struct mlx5_flow_root_namespace *root;
3016 struct mlx5_modify_hdr *modify_hdr;
3017 int err;
3018
3019 root = get_root_namespace(dev, ns_type);
3020 if (!root)
3021 return ERR_PTR(-EOPNOTSUPP);
3022
3023 modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL);
3024 if (!modify_hdr)
3025 return ERR_PTR(-ENOMEM);
3026
3027 modify_hdr->ns_type = ns_type;
3028 err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
3029 modify_actions, modify_hdr);
3030 if (err) {
3031 kfree(modify_hdr);
3032 return ERR_PTR(err);
3033 }
3034
3035 return modify_hdr;
3036}
3037EXPORT_SYMBOL(mlx5_modify_header_alloc);
3038
3039void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
3040 struct mlx5_modify_hdr *modify_hdr)
3041{
3042 struct mlx5_flow_root_namespace *root;
3043
3044 root = get_root_namespace(dev, modify_hdr->ns_type);
3045 if (WARN_ON(!root))
3046 return;
3047 root->cmds->modify_header_dealloc(root, modify_hdr);
3048 kfree(modify_hdr);
3049}
3050EXPORT_SYMBOL(mlx5_modify_header_dealloc);
3051
3052struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
3053 int reformat_type,
3054 size_t size,
3055 void *reformat_data,
3056 enum mlx5_flow_namespace_type ns_type)
3057{
3058 struct mlx5_pkt_reformat *pkt_reformat;
3059 struct mlx5_flow_root_namespace *root;
3060 int err;
3061
3062 root = get_root_namespace(dev, ns_type);
3063 if (!root)
3064 return ERR_PTR(-EOPNOTSUPP);
3065
3066 pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL);
3067 if (!pkt_reformat)
3068 return ERR_PTR(-ENOMEM);
3069
3070 pkt_reformat->ns_type = ns_type;
3071 pkt_reformat->reformat_type = reformat_type;
3072 err = root->cmds->packet_reformat_alloc(root, reformat_type, size,
3073 reformat_data, ns_type,
3074 pkt_reformat);
3075 if (err) {
3076 kfree(pkt_reformat);
3077 return ERR_PTR(err);
3078 }
3079
3080 return pkt_reformat;
3081}
3082EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
3083
3084void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
3085 struct mlx5_pkt_reformat *pkt_reformat)
3086{
3087 struct mlx5_flow_root_namespace *root;
3088
3089 root = get_root_namespace(dev, pkt_reformat->ns_type);
3090 if (WARN_ON(!root))
3091 return;
3092 root->cmds->packet_reformat_dealloc(root, pkt_reformat);
3093 kfree(pkt_reformat);
3094}
3095EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
6a48faee
MG
3096
3097int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
3098 struct mlx5_flow_root_namespace *peer_ns)
3099{
38b9d1c6
MG
3100 if (peer_ns && ns->mode != peer_ns->mode) {
3101 mlx5_core_err(ns->dev,
3102 "Can't peer namespace of different steering mode\n");
3103 return -EINVAL;
3104 }
3105
6a48faee
MG
3106 return ns->cmds->set_peer(ns, peer_ns);
3107}
38b9d1c6
MG
3108
3109/* This function should be called only at init stage of the namespace.
3110 * It is not safe to call this function while steering operations
3111 * are executed in the namespace.
3112 */
3113int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
3114 enum mlx5_flow_steering_mode mode)
3115{
3116 struct mlx5_flow_root_namespace *root;
3117 const struct mlx5_flow_cmds *cmds;
3118 int err;
3119
3120 root = find_root(&ns->node);
3121 if (&root->ns != ns)
3122 /* Can't set cmds to non root namespace */
3123 return -EINVAL;
3124
3125 if (root->table_type != FS_FT_FDB)
3126 return -EOPNOTSUPP;
3127
3128 if (root->mode == mode)
3129 return 0;
3130
3131 if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
3132 cmds = mlx5_fs_cmd_get_dr_cmds();
3133 else
3134 cmds = mlx5_fs_cmd_get_fw_cmds();
3135 if (!cmds)
3136 return -EOPNOTSUPP;
3137
3138 err = cmds->create_ns(root);
3139 if (err) {
3140 mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
3141 err);
3142 return err;
3143 }
3144
3145 root->cmds->destroy_ns(root);
3146 root->cmds = cmds;
3147 root->mode = mode;
3148
3149 return 0;
3150}