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