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