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