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