net/mlx5: Consider encapsulation properties when comparing destinations
[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 &&
1228e912
EB
1376 d1->vport.num == d2->vport.num &&
1377 d1->vport.flags == d2->vport.flags &&
1378 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
1379 (d1->vport.reformat_id == d2->vport.reformat_id) : true)) ||
814fb875
MB
1380 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1381 d1->ft == d2->ft) ||
1382 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
664000b6
YH
1383 d1->tir_num == d2->tir_num) ||
1384 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1385 d1->ft_num == d2->ft_num))
814fb875
MB
1386 return true;
1387 }
1388
1389 return false;
1390}
1391
b3638e1a
MG
1392static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1393 struct mlx5_flow_destination *dest)
1394{
1395 struct mlx5_flow_rule *rule;
1396
1397 list_for_each_entry(rule, &fte->node.children, node.list) {
814fb875
MB
1398 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1399 return rule;
b3638e1a
MG
1400 }
1401 return NULL;
1402}
1403
0d235c3f
MB
1404static bool check_conflicting_actions(u32 action1, u32 action2)
1405{
1406 u32 xored_actions = action1 ^ action2;
1407
1408 /* if one rule only wants to count, it's ok */
1409 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1410 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1411 return false;
1412
1413 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
60786f09 1414 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
96de67a7 1415 MLX5_FLOW_CONTEXT_ACTION_DECAP |
0c06897a
OG
1416 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1417 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
8da6fe2a
JL
1418 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1419 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1420 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
0d235c3f
MB
1421 return true;
1422
1423 return false;
1424}
1425
1426static int check_conflicting_ftes(struct fs_fte *fte, const struct mlx5_flow_act *flow_act)
1427{
d2ec6a35 1428 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
0d235c3f
MB
1429 mlx5_core_warn(get_dev(&fte->node),
1430 "Found two FTEs with conflicting actions\n");
1431 return -EEXIST;
1432 }
1433
d5634fee 1434 if ((flow_act->flags & FLOW_ACT_HAS_TAG) &&
d2ec6a35 1435 fte->action.flow_tag != flow_act->flow_tag) {
0d235c3f
MB
1436 mlx5_core_warn(get_dev(&fte->node),
1437 "FTE flow tag %u already exists with different flow tag %u\n",
d2ec6a35 1438 fte->action.flow_tag,
0d235c3f
MB
1439 flow_act->flow_tag);
1440 return -EEXIST;
1441 }
1442
1443 return 0;
1444}
1445
74491de9
MB
1446static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1447 u32 *match_value,
66958ed9 1448 struct mlx5_flow_act *flow_act,
74491de9 1449 struct mlx5_flow_destination *dest,
693c6883
MB
1450 int dest_num,
1451 struct fs_fte *fte)
0c56b975 1452{
74491de9 1453 struct mlx5_flow_handle *handle;
bd71b08e 1454 int old_action;
74491de9 1455 int i;
bd71b08e 1456 int ret;
0c56b975 1457
bd71b08e
MG
1458 ret = check_conflicting_ftes(fte, flow_act);
1459 if (ret)
1460 return ERR_PTR(ret);
0c56b975 1461
d2ec6a35
MB
1462 old_action = fte->action.action;
1463 fte->action.action |= flow_act->action;
bd71b08e
MG
1464 handle = add_rule_fte(fte, fg, dest, dest_num,
1465 old_action != flow_act->action);
74491de9 1466 if (IS_ERR(handle)) {
d2ec6a35 1467 fte->action.action = old_action;
693c6883 1468 return handle;
0c56b975 1469 }
bd71b08e 1470 trace_mlx5_fs_set_fte(fte, false);
0c56b975 1471
74491de9 1472 for (i = 0; i < handle->num_rules; i++) {
dd8e1945 1473 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
74491de9 1474 tree_add_node(&handle->rule[i]->node, &fte->node);
4c03e69a
MB
1475 trace_mlx5_fs_add_rule(handle->rule[i]);
1476 }
74491de9 1477 }
74491de9 1478 return handle;
0c56b975
MG
1479}
1480
171c7625 1481static bool counter_is_valid(u32 action)
bd5251db 1482{
ae058314 1483 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
eafa6abd 1484 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
bd5251db
AV
1485}
1486
d63cd286
MG
1487static bool dest_is_valid(struct mlx5_flow_destination *dest,
1488 u32 action,
1489 struct mlx5_flow_table *ft)
1490{
bd5251db 1491 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
171c7625 1492 return counter_is_valid(action);
bd5251db 1493
d63cd286
MG
1494 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1495 return true;
1496
1497 if (!dest || ((dest->type ==
1498 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1499 (dest->ft->level <= ft->level)))
1500 return false;
1501 return true;
1502}
1503
46719d77
MG
1504struct match_list {
1505 struct list_head list;
693c6883 1506 struct mlx5_flow_group *g;
46719d77
MG
1507};
1508
1509struct match_list_head {
1510 struct list_head list;
1511 struct match_list first;
1512};
1513
1514static void free_match_list(struct match_list_head *head)
1515{
1516 if (!list_empty(&head->list)) {
1517 struct match_list *iter, *match_tmp;
1518
1519 list_del(&head->first.list);
bd71b08e 1520 tree_put_node(&head->first.g->node);
46719d77
MG
1521 list_for_each_entry_safe(iter, match_tmp, &head->list,
1522 list) {
bd71b08e 1523 tree_put_node(&iter->g->node);
46719d77
MG
1524 list_del(&iter->list);
1525 kfree(iter);
1526 }
1527 }
1528}
1529
1530static int build_match_list(struct match_list_head *match_head,
1531 struct mlx5_flow_table *ft,
1532 struct mlx5_flow_spec *spec)
1533{
693c6883 1534 struct rhlist_head *tmp, *list;
46719d77
MG
1535 struct mlx5_flow_group *g;
1536 int err = 0;
693c6883
MB
1537
1538 rcu_read_lock();
46719d77 1539 INIT_LIST_HEAD(&match_head->list);
693c6883
MB
1540 /* Collect all fgs which has a matching match_criteria */
1541 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
46719d77 1542 /* RCU is atomic, we can't execute FW commands here */
693c6883
MB
1543 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1544 struct match_list *curr_match;
1545
46719d77 1546 if (likely(list_empty(&match_head->list))) {
bd71b08e
MG
1547 if (!tree_get_node(&g->node))
1548 continue;
46719d77
MG
1549 match_head->first.g = g;
1550 list_add_tail(&match_head->first.list,
1551 &match_head->list);
693c6883
MB
1552 continue;
1553 }
693c6883 1554
46719d77 1555 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
693c6883 1556 if (!curr_match) {
46719d77
MG
1557 free_match_list(match_head);
1558 err = -ENOMEM;
1559 goto out;
693c6883 1560 }
bd71b08e
MG
1561 if (!tree_get_node(&g->node)) {
1562 kfree(curr_match);
1563 continue;
1564 }
693c6883 1565 curr_match->g = g;
46719d77 1566 list_add_tail(&curr_match->list, &match_head->list);
693c6883 1567 }
46719d77 1568out:
693c6883 1569 rcu_read_unlock();
46719d77
MG
1570 return err;
1571}
1572
bd71b08e
MG
1573static u64 matched_fgs_get_version(struct list_head *match_head)
1574{
1575 struct match_list *iter;
1576 u64 version = 0;
1577
1578 list_for_each_entry(iter, match_head, list)
1579 version += (u64)atomic_read(&iter->g->node.version);
1580 return version;
1581}
1582
ad9421e3
RD
1583static struct fs_fte *
1584lookup_fte_locked(struct mlx5_flow_group *g,
1585 u32 *match_value,
1586 bool take_write)
1587{
1588 struct fs_fte *fte_tmp;
1589
1590 if (take_write)
1591 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1592 else
1593 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
1594 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
1595 rhash_fte);
1596 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1597 fte_tmp = NULL;
1598 goto out;
1599 }
1600
1601 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1602out:
1603 if (take_write)
1604 up_write_ref_node(&g->node);
1605 else
1606 up_read_ref_node(&g->node);
1607 return fte_tmp;
1608}
1609
46719d77
MG
1610static struct mlx5_flow_handle *
1611try_add_to_existing_fg(struct mlx5_flow_table *ft,
bd71b08e 1612 struct list_head *match_head,
46719d77
MG
1613 struct mlx5_flow_spec *spec,
1614 struct mlx5_flow_act *flow_act,
1615 struct mlx5_flow_destination *dest,
bd71b08e
MG
1616 int dest_num,
1617 int ft_version)
46719d77 1618{
a369d4ac 1619 struct mlx5_flow_steering *steering = get_steering(&ft->node);
46719d77
MG
1620 struct mlx5_flow_group *g;
1621 struct mlx5_flow_handle *rule;
46719d77 1622 struct match_list *iter;
bd71b08e
MG
1623 bool take_write = false;
1624 struct fs_fte *fte;
1625 u64 version;
f5c2ff17
MG
1626 int err;
1627
a369d4ac 1628 fte = alloc_fte(ft, spec->match_value, flow_act);
f5c2ff17
MG
1629 if (IS_ERR(fte))
1630 return ERR_PTR(-ENOMEM);
46719d77 1631
bd71b08e
MG
1632search_again_locked:
1633 version = matched_fgs_get_version(match_head);
d5634fee
PB
1634 if (flow_act->flags & FLOW_ACT_NO_APPEND)
1635 goto skip_search;
693c6883 1636 /* Try to find a fg that already contains a matching fte */
bd71b08e
MG
1637 list_for_each_entry(iter, match_head, list) {
1638 struct fs_fte *fte_tmp;
693c6883
MB
1639
1640 g = iter->g;
ad9421e3
RD
1641 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1642 if (!fte_tmp)
bd71b08e 1643 continue;
bd71b08e
MG
1644 rule = add_rule_fg(g, spec->match_value,
1645 flow_act, dest, dest_num, fte_tmp);
1646 up_write_ref_node(&fte_tmp->node);
1647 tree_put_node(&fte_tmp->node);
a369d4ac 1648 kmem_cache_free(steering->ftes_cache, fte);
bd71b08e 1649 return rule;
693c6883
MB
1650 }
1651
d5634fee
PB
1652skip_search:
1653 /* No group with matching fte found, or we skipped the search.
1654 * Try to add a new fte to any matching fg.
1655 */
1656
bd71b08e
MG
1657 /* Check the ft version, for case that new flow group
1658 * was added while the fgs weren't locked
1659 */
1660 if (atomic_read(&ft->node.version) != ft_version) {
1661 rule = ERR_PTR(-EAGAIN);
1662 goto out;
1663 }
b92af5a7 1664
bd71b08e
MG
1665 /* Check the fgs version, for case the new FTE with the
1666 * same values was added while the fgs weren't locked
1667 */
ad9421e3
RD
1668 if (version != matched_fgs_get_version(match_head)) {
1669 take_write = true;
bd71b08e 1670 goto search_again_locked;
ad9421e3 1671 }
bd71b08e
MG
1672
1673 list_for_each_entry(iter, match_head, list) {
1674 g = iter->g;
1675
1676 if (!g->node.active)
1677 continue;
ad9421e3
RD
1678
1679 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1680
f5c2ff17
MG
1681 err = insert_fte(g, fte);
1682 if (err) {
ad9421e3 1683 up_write_ref_node(&g->node);
f5c2ff17 1684 if (err == -ENOSPC)
bd71b08e 1685 continue;
a369d4ac 1686 kmem_cache_free(steering->ftes_cache, fte);
f5c2ff17 1687 return ERR_PTR(err);
bd71b08e 1688 }
693c6883 1689
bd71b08e 1690 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
ad9421e3 1691 up_write_ref_node(&g->node);
bd71b08e
MG
1692 rule = add_rule_fg(g, spec->match_value,
1693 flow_act, dest, dest_num, fte);
1694 up_write_ref_node(&fte->node);
1695 tree_put_node(&fte->node);
1696 return rule;
1697 }
1698 rule = ERR_PTR(-ENOENT);
1699out:
a369d4ac 1700 kmem_cache_free(steering->ftes_cache, fte);
693c6883
MB
1701 return rule;
1702}
1703
74491de9
MB
1704static struct mlx5_flow_handle *
1705_mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1706 struct mlx5_flow_spec *spec,
66958ed9 1707 struct mlx5_flow_act *flow_act,
74491de9
MB
1708 struct mlx5_flow_destination *dest,
1709 int dest_num)
66958ed9 1710
0c56b975 1711{
a369d4ac 1712 struct mlx5_flow_steering *steering = get_steering(&ft->node);
0c56b975 1713 struct mlx5_flow_group *g;
74491de9 1714 struct mlx5_flow_handle *rule;
bd71b08e
MG
1715 struct match_list_head match_head;
1716 bool take_write = false;
1717 struct fs_fte *fte;
1718 int version;
19f100fe 1719 int err;
74491de9 1720 int i;
0c56b975 1721
693c6883 1722 if (!check_valid_spec(spec))
0d235c3f
MB
1723 return ERR_PTR(-EINVAL);
1724
74491de9 1725 for (i = 0; i < dest_num; i++) {
66958ed9 1726 if (!dest_is_valid(&dest[i], flow_act->action, ft))
74491de9
MB
1727 return ERR_PTR(-EINVAL);
1728 }
bd71b08e
MG
1729 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1730search_again_locked:
1731 version = atomic_read(&ft->node.version);
60ab4584 1732
bd71b08e
MG
1733 /* Collect all fgs which has a matching match_criteria */
1734 err = build_match_list(&match_head, ft, spec);
9238e380
VB
1735 if (err) {
1736 if (take_write)
1737 up_write_ref_node(&ft->node);
07130477
RD
1738 else
1739 up_read_ref_node(&ft->node);
bd71b08e 1740 return ERR_PTR(err);
9238e380 1741 }
bd71b08e
MG
1742
1743 if (!take_write)
1744 up_read_ref_node(&ft->node);
1745
1746 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1747 dest_num, version);
1748 free_match_list(&match_head);
1749 if (!IS_ERR(rule) ||
9238e380
VB
1750 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1751 if (take_write)
1752 up_write_ref_node(&ft->node);
bd71b08e 1753 return rule;
9238e380 1754 }
bd71b08e
MG
1755
1756 if (!take_write) {
1757 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1758 take_write = true;
1759 }
1760
1761 if (PTR_ERR(rule) == -EAGAIN ||
1762 version != atomic_read(&ft->node.version))
1763 goto search_again_locked;
f0d22d18 1764
19f100fe 1765 g = alloc_auto_flow_group(ft, spec);
c3f9bf62 1766 if (IS_ERR(g)) {
d34c6efc 1767 rule = ERR_CAST(g);
bd71b08e
MG
1768 up_write_ref_node(&ft->node);
1769 return rule;
c3f9bf62
MG
1770 }
1771
bd71b08e
MG
1772 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1773 up_write_ref_node(&ft->node);
1774
19f100fe 1775 err = create_auto_flow_group(ft, g);
bd71b08e
MG
1776 if (err)
1777 goto err_release_fg;
1778
a369d4ac 1779 fte = alloc_fte(ft, spec->match_value, flow_act);
bd71b08e
MG
1780 if (IS_ERR(fte)) {
1781 err = PTR_ERR(fte);
1782 goto err_release_fg;
19f100fe
MG
1783 }
1784
f5c2ff17
MG
1785 err = insert_fte(g, fte);
1786 if (err) {
a369d4ac 1787 kmem_cache_free(steering->ftes_cache, fte);
f5c2ff17
MG
1788 goto err_release_fg;
1789 }
1790
bd71b08e
MG
1791 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1792 up_write_ref_node(&g->node);
693c6883 1793 rule = add_rule_fg(g, spec->match_value, flow_act, dest,
bd71b08e
MG
1794 dest_num, fte);
1795 up_write_ref_node(&fte->node);
1796 tree_put_node(&fte->node);
19f100fe 1797 tree_put_node(&g->node);
0c56b975 1798 return rule;
bd71b08e
MG
1799
1800err_release_fg:
1801 up_write_ref_node(&g->node);
1802 tree_put_node(&g->node);
1803 return ERR_PTR(err);
0c56b975 1804}
b3638e1a
MG
1805
1806static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1807{
1808 return ((ft->type == FS_FT_NIC_RX) &&
1809 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1810}
1811
74491de9
MB
1812struct mlx5_flow_handle *
1813mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1814 struct mlx5_flow_spec *spec,
66958ed9 1815 struct mlx5_flow_act *flow_act,
74491de9 1816 struct mlx5_flow_destination *dest,
cf916ffb 1817 int num_dest)
b3638e1a
MG
1818{
1819 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
4c5009c5 1820 struct mlx5_flow_destination gen_dest = {};
b3638e1a 1821 struct mlx5_flow_table *next_ft = NULL;
74491de9 1822 struct mlx5_flow_handle *handle = NULL;
66958ed9 1823 u32 sw_action = flow_act->action;
b3638e1a
MG
1824 struct fs_prio *prio;
1825
1826 fs_get_obj(prio, ft->node.parent);
66958ed9 1827 if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
b3638e1a
MG
1828 if (!fwd_next_prio_supported(ft))
1829 return ERR_PTR(-EOPNOTSUPP);
cf916ffb 1830 if (num_dest)
b3638e1a
MG
1831 return ERR_PTR(-EINVAL);
1832 mutex_lock(&root->chain_lock);
1833 next_ft = find_next_chained_ft(prio);
1834 if (next_ft) {
1835 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1836 gen_dest.ft = next_ft;
1837 dest = &gen_dest;
cf916ffb 1838 num_dest = 1;
66958ed9 1839 flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
b3638e1a
MG
1840 } else {
1841 mutex_unlock(&root->chain_lock);
1842 return ERR_PTR(-EOPNOTSUPP);
1843 }
1844 }
1845
cf916ffb 1846 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
b3638e1a
MG
1847
1848 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
74491de9
MB
1849 if (!IS_ERR_OR_NULL(handle) &&
1850 (list_empty(&handle->rule[0]->next_ft))) {
b3638e1a 1851 mutex_lock(&next_ft->lock);
74491de9
MB
1852 list_add(&handle->rule[0]->next_ft,
1853 &next_ft->fwd_rules);
b3638e1a 1854 mutex_unlock(&next_ft->lock);
74491de9 1855 handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
b3638e1a
MG
1856 }
1857 mutex_unlock(&root->chain_lock);
1858 }
74491de9 1859 return handle;
b3638e1a 1860}
74491de9 1861EXPORT_SYMBOL(mlx5_add_flow_rules);
0c56b975 1862
74491de9 1863void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
0c56b975 1864{
74491de9
MB
1865 int i;
1866
1867 for (i = handle->num_rules - 1; i >= 0; i--)
1868 tree_remove_node(&handle->rule[i]->node);
1869 kfree(handle);
0c56b975 1870}
74491de9 1871EXPORT_SYMBOL(mlx5_del_flow_rules);
0c56b975 1872
2cc43b49
MG
1873/* Assuming prio->node.children(flow tables) is sorted by level */
1874static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1875{
1876 struct fs_prio *prio;
1877
1878 fs_get_obj(prio, ft->node.parent);
1879
1880 if (!list_is_last(&ft->node.list, &prio->node.children))
1881 return list_next_entry(ft, node.list);
1882 return find_next_chained_ft(prio);
1883}
1884
1885static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1886{
1887 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
dae37456 1888 struct mlx5_ft_underlay_qp *uqp;
2cc43b49 1889 struct mlx5_flow_table *new_root_ft = NULL;
dae37456
AV
1890 int err = 0;
1891 u32 qpn;
2cc43b49
MG
1892
1893 if (root->root_ft != ft)
1894 return 0;
1895
1896 new_root_ft = find_next_ft(ft);
dae37456
AV
1897 if (!new_root_ft) {
1898 root->root_ft = NULL;
1899 return 0;
1900 }
1901
1902 if (list_empty(&root->underlay_qpns)) {
1903 /* Don't set any QPN (zero) in case QPN list is empty */
1904 qpn = 0;
af76c501
MB
1905 err = root->cmds->update_root_ft(root->dev, new_root_ft,
1906 qpn, false);
dae37456
AV
1907 } else {
1908 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1909 qpn = uqp->qpn;
af76c501
MB
1910 err = root->cmds->update_root_ft(root->dev,
1911 new_root_ft, qpn,
1912 false);
dae37456
AV
1913 if (err)
1914 break;
2cc43b49 1915 }
2cc43b49 1916 }
dae37456
AV
1917
1918 if (err)
1919 mlx5_core_warn(root->dev,
1920 "Update root flow table of id(%u) qpn(%d) failed\n",
1921 ft->id, qpn);
1922 else
1923 root->root_ft = new_root_ft;
1924
2cc43b49
MG
1925 return 0;
1926}
1927
f90edfd2
MG
1928/* Connect flow table from previous priority to
1929 * the next flow table.
1930 */
1931static int disconnect_flow_table(struct mlx5_flow_table *ft)
1932{
1933 struct mlx5_core_dev *dev = get_dev(&ft->node);
1934 struct mlx5_flow_table *next_ft;
1935 struct fs_prio *prio;
1936 int err = 0;
1937
1938 err = update_root_ft_destroy(ft);
1939 if (err)
1940 return err;
1941
1942 fs_get_obj(prio, ft->node.parent);
1943 if (!(list_first_entry(&prio->node.children,
1944 struct mlx5_flow_table,
1945 node.list) == ft))
1946 return 0;
1947
1948 next_ft = find_next_chained_ft(prio);
b3638e1a
MG
1949 err = connect_fwd_rules(dev, next_ft, ft);
1950 if (err)
1951 return err;
1952
f90edfd2
MG
1953 err = connect_prev_fts(dev, next_ft, prio);
1954 if (err)
1955 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1956 ft->id);
1957 return err;
1958}
1959
86d722ad 1960int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
0c56b975 1961{
2cc43b49
MG
1962 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1963 int err = 0;
1964
1965 mutex_lock(&root->chain_lock);
f90edfd2 1966 err = disconnect_flow_table(ft);
2cc43b49
MG
1967 if (err) {
1968 mutex_unlock(&root->chain_lock);
1969 return err;
1970 }
0c56b975
MG
1971 if (tree_remove_node(&ft->node))
1972 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1973 ft->id);
2cc43b49 1974 mutex_unlock(&root->chain_lock);
0c56b975 1975
2cc43b49 1976 return err;
0c56b975 1977}
b217ea25 1978EXPORT_SYMBOL(mlx5_destroy_flow_table);
0c56b975 1979
86d722ad 1980void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
0c56b975
MG
1981{
1982 if (tree_remove_node(&fg->node))
1983 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1984 fg->id);
1985}
25302363 1986
328edb49
PB
1987struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
1988 int n)
1989{
1990 struct mlx5_flow_steering *steering = dev->priv.steering;
1991
1992 if (!steering || !steering->fdb_sub_ns)
1993 return NULL;
1994
1995 return steering->fdb_sub_ns[n];
1996}
1997EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
1998
86d722ad
MG
1999struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2000 enum mlx5_flow_namespace_type type)
25302363 2001{
fba53f7b
MG
2002 struct mlx5_flow_steering *steering = dev->priv.steering;
2003 struct mlx5_flow_root_namespace *root_ns;
8ce78257 2004 int prio = 0;
78228cbd 2005 struct fs_prio *fs_prio;
25302363
MG
2006 struct mlx5_flow_namespace *ns;
2007
fba53f7b 2008 if (!steering)
25302363
MG
2009 return NULL;
2010
2011 switch (type) {
25302363 2012 case MLX5_FLOW_NAMESPACE_FDB:
fba53f7b
MG
2013 if (steering->fdb_root_ns)
2014 return &steering->fdb_root_ns->ns;
2226dcb4 2015 return NULL;
87d22483
MG
2016 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2017 if (steering->sniffer_rx_root_ns)
2018 return &steering->sniffer_rx_root_ns->ns;
2226dcb4 2019 return NULL;
87d22483
MG
2020 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2021 if (steering->sniffer_tx_root_ns)
2022 return &steering->sniffer_tx_root_ns->ns;
25302363 2023 return NULL;
2226dcb4
MB
2024 default:
2025 break;
25302363
MG
2026 }
2027
8ce78257
MB
2028 if (type == MLX5_FLOW_NAMESPACE_EGRESS) {
2029 root_ns = steering->egress_root_ns;
2030 } else { /* Must be NIC RX */
2031 root_ns = steering->root_ns;
2032 prio = type;
25302363
MG
2033 }
2034
fba53f7b
MG
2035 if (!root_ns)
2036 return NULL;
2037
25302363
MG
2038 fs_prio = find_prio(&root_ns->ns, prio);
2039 if (!fs_prio)
2040 return NULL;
2041
2042 ns = list_first_entry(&fs_prio->node.children,
2043 typeof(*ns),
2044 node.list);
2045
2046 return ns;
2047}
b217ea25 2048EXPORT_SYMBOL(mlx5_get_flow_namespace);
25302363 2049
9b93ab98
GP
2050struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2051 enum mlx5_flow_namespace_type type,
2052 int vport)
2053{
2054 struct mlx5_flow_steering *steering = dev->priv.steering;
2055
2056 if (!steering || vport >= MLX5_TOTAL_VPORTS(dev))
2057 return NULL;
2058
2059 switch (type) {
2060 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2061 if (steering->esw_egress_root_ns &&
2062 steering->esw_egress_root_ns[vport])
2063 return &steering->esw_egress_root_ns[vport]->ns;
2064 else
2065 return NULL;
2066 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2067 if (steering->esw_ingress_root_ns &&
2068 steering->esw_ingress_root_ns[vport])
2069 return &steering->esw_ingress_root_ns[vport]->ns;
2070 else
2071 return NULL;
2072 default:
2073 return NULL;
2074 }
2075}
2076
328edb49
PB
2077static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2078 unsigned int prio,
2079 int num_levels,
2080 enum fs_node_type type)
25302363
MG
2081{
2082 struct fs_prio *fs_prio;
2083
2084 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2085 if (!fs_prio)
2086 return ERR_PTR(-ENOMEM);
2087
328edb49 2088 fs_prio->node.type = type;
139ed6c6 2089 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
25302363 2090 tree_add_node(&fs_prio->node, &ns->node);
a257b94a 2091 fs_prio->num_levels = num_levels;
25302363 2092 fs_prio->prio = prio;
25302363
MG
2093 list_add_tail(&fs_prio->node.list, &ns->node.children);
2094
2095 return fs_prio;
2096}
2097
328edb49
PB
2098static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2099 unsigned int prio,
2100 int num_levels)
2101{
2102 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2103}
2104
2105static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2106 unsigned int prio, int num_levels)
2107{
2108 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2109}
2110
25302363
MG
2111static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2112 *ns)
2113{
2114 ns->node.type = FS_TYPE_NAMESPACE;
2115
2116 return ns;
2117}
2118
2119static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
2120{
2121 struct mlx5_flow_namespace *ns;
2122
2123 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2124 if (!ns)
2125 return ERR_PTR(-ENOMEM);
2126
2127 fs_init_namespace(ns);
139ed6c6 2128 tree_init_node(&ns->node, NULL, del_sw_ns);
25302363
MG
2129 tree_add_node(&ns->node, &prio->node);
2130 list_add_tail(&ns->node.list, &prio->node.children);
2131
2132 return ns;
2133}
2134
13de6c10
MG
2135static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2136 struct init_tree_node *prio_metadata)
4cbdd30e
MG
2137{
2138 struct fs_prio *fs_prio;
2139 int i;
2140
2141 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
13de6c10 2142 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
4cbdd30e
MG
2143 if (IS_ERR(fs_prio))
2144 return PTR_ERR(fs_prio);
2145 }
2146 return 0;
2147}
2148
8d40d162
MG
2149#define FLOW_TABLE_BIT_SZ 1
2150#define GET_FLOW_TABLE_CAP(dev, offset) \
701052c5 2151 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
8d40d162
MG
2152 offset / 32)) >> \
2153 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2154static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2155{
2156 int i;
2157
2158 for (i = 0; i < caps->arr_sz; i++) {
2159 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2160 return false;
2161 }
2162 return true;
2163}
2164
fba53f7b 2165static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
8d40d162 2166 struct init_tree_node *init_node,
25302363
MG
2167 struct fs_node *fs_parent_node,
2168 struct init_tree_node *init_parent_node,
13de6c10 2169 int prio)
25302363 2170{
fba53f7b 2171 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
8d40d162
MG
2172 flow_table_properties_nic_receive.
2173 max_ft_level);
25302363
MG
2174 struct mlx5_flow_namespace *fs_ns;
2175 struct fs_prio *fs_prio;
2176 struct fs_node *base;
2177 int i;
2178 int err;
2179
2180 if (init_node->type == FS_TYPE_PRIO) {
8d40d162 2181 if ((init_node->min_ft_level > max_ft_level) ||
fba53f7b 2182 !has_required_caps(steering->dev, &init_node->caps))
8d40d162 2183 return 0;
25302363
MG
2184
2185 fs_get_obj(fs_ns, fs_parent_node);
4cbdd30e 2186 if (init_node->num_leaf_prios)
13de6c10
MG
2187 return create_leaf_prios(fs_ns, prio, init_node);
2188 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
25302363
MG
2189 if (IS_ERR(fs_prio))
2190 return PTR_ERR(fs_prio);
2191 base = &fs_prio->node;
2192 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2193 fs_get_obj(fs_prio, fs_parent_node);
2194 fs_ns = fs_create_namespace(fs_prio);
2195 if (IS_ERR(fs_ns))
2196 return PTR_ERR(fs_ns);
2197 base = &fs_ns->node;
2198 } else {
2199 return -EINVAL;
2200 }
13de6c10 2201 prio = 0;
25302363 2202 for (i = 0; i < init_node->ar_size; i++) {
fba53f7b 2203 err = init_root_tree_recursive(steering, &init_node->children[i],
13de6c10 2204 base, init_node, prio);
25302363
MG
2205 if (err)
2206 return err;
13de6c10
MG
2207 if (init_node->children[i].type == FS_TYPE_PRIO &&
2208 init_node->children[i].num_leaf_prios) {
2209 prio += init_node->children[i].num_leaf_prios;
2210 }
25302363
MG
2211 }
2212
2213 return 0;
2214}
2215
fba53f7b 2216static int init_root_tree(struct mlx5_flow_steering *steering,
8d40d162 2217 struct init_tree_node *init_node,
25302363
MG
2218 struct fs_node *fs_parent_node)
2219{
2220 int i;
2221 struct mlx5_flow_namespace *fs_ns;
2222 int err;
2223
2224 fs_get_obj(fs_ns, fs_parent_node);
2225 for (i = 0; i < init_node->ar_size; i++) {
fba53f7b 2226 err = init_root_tree_recursive(steering, &init_node->children[i],
25302363
MG
2227 &fs_ns->node,
2228 init_node, i);
2229 if (err)
2230 return err;
2231 }
2232 return 0;
2233}
2234
af76c501
MB
2235static struct mlx5_flow_root_namespace
2236*create_root_ns(struct mlx5_flow_steering *steering,
2237 enum fs_flow_table_type table_type)
25302363 2238{
af76c501 2239 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
25302363
MG
2240 struct mlx5_flow_root_namespace *root_ns;
2241 struct mlx5_flow_namespace *ns;
2242
05564d0a
AY
2243 if (mlx5_accel_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2244 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2245 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2246
86d722ad 2247 /* Create the root namespace */
1b9a07ee 2248 root_ns = kvzalloc(sizeof(*root_ns), GFP_KERNEL);
25302363
MG
2249 if (!root_ns)
2250 return NULL;
2251
fba53f7b 2252 root_ns->dev = steering->dev;
25302363 2253 root_ns->table_type = table_type;
af76c501 2254 root_ns->cmds = cmds;
25302363 2255
dae37456
AV
2256 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2257
25302363
MG
2258 ns = &root_ns->ns;
2259 fs_init_namespace(ns);
2cc43b49 2260 mutex_init(&root_ns->chain_lock);
bd71b08e 2261 tree_init_node(&ns->node, NULL, NULL);
25302363
MG
2262 tree_add_node(&ns->node, NULL);
2263
2264 return root_ns;
2265}
2266
655227ed
MG
2267static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2268
2269static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2270{
2271 struct fs_prio *prio;
2272
2273 fs_for_each_prio(prio, ns) {
a257b94a 2274 /* This updates prio start_level and num_levels */
655227ed 2275 set_prio_attrs_in_prio(prio, acc_level);
a257b94a 2276 acc_level += prio->num_levels;
655227ed
MG
2277 }
2278 return acc_level;
2279}
2280
2281static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2282{
2283 struct mlx5_flow_namespace *ns;
2284 int acc_level_ns = acc_level;
2285
2286 prio->start_level = acc_level;
2287 fs_for_each_ns(ns, prio)
a257b94a 2288 /* This updates start_level and num_levels of ns's priority descendants */
655227ed 2289 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
a257b94a
MG
2290 if (!prio->num_levels)
2291 prio->num_levels = acc_level_ns - prio->start_level;
2292 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
655227ed
MG
2293}
2294
2295static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2296{
2297 struct mlx5_flow_namespace *ns = &root_ns->ns;
2298 struct fs_prio *prio;
2299 int start_level = 0;
2300
2301 fs_for_each_prio(prio, ns) {
2302 set_prio_attrs_in_prio(prio, start_level);
a257b94a 2303 start_level += prio->num_levels;
655227ed
MG
2304 }
2305}
2306
153fefbf
MG
2307#define ANCHOR_PRIO 0
2308#define ANCHOR_SIZE 1
d63cd286 2309#define ANCHOR_LEVEL 0
fba53f7b 2310static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
153fefbf
MG
2311{
2312 struct mlx5_flow_namespace *ns = NULL;
b3ba5149 2313 struct mlx5_flow_table_attr ft_attr = {};
153fefbf
MG
2314 struct mlx5_flow_table *ft;
2315
fba53f7b 2316 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
eff596da 2317 if (WARN_ON(!ns))
153fefbf 2318 return -EINVAL;
b3ba5149
ES
2319
2320 ft_attr.max_fte = ANCHOR_SIZE;
2321 ft_attr.level = ANCHOR_LEVEL;
2322 ft_attr.prio = ANCHOR_PRIO;
2323
2324 ft = mlx5_create_flow_table(ns, &ft_attr);
153fefbf 2325 if (IS_ERR(ft)) {
fba53f7b 2326 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
153fefbf
MG
2327 return PTR_ERR(ft);
2328 }
2329 return 0;
2330}
2331
fba53f7b 2332static int init_root_ns(struct mlx5_flow_steering *steering)
25302363 2333{
9c26f5f8
TB
2334 int err;
2335
fba53f7b 2336 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
42fb18fd 2337 if (!steering->root_ns)
9c26f5f8 2338 return -ENOMEM;
25302363 2339
9c26f5f8
TB
2340 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2341 if (err)
2342 goto out_err;
25302363 2343
fba53f7b 2344 set_prio_attrs(steering->root_ns);
9c26f5f8
TB
2345 err = create_anchor_flow_table(steering);
2346 if (err)
2347 goto out_err;
153fefbf 2348
25302363
MG
2349 return 0;
2350
9c26f5f8
TB
2351out_err:
2352 cleanup_root_ns(steering->root_ns);
2353 steering->root_ns = NULL;
2354 return err;
25302363
MG
2355}
2356
0da2d666 2357static void clean_tree(struct fs_node *node)
25302363 2358{
0da2d666
MG
2359 if (node) {
2360 struct fs_node *iter;
2361 struct fs_node *temp;
25302363 2362
800350a3 2363 tree_get_node(node);
0da2d666
MG
2364 list_for_each_entry_safe(iter, temp, &node->children, list)
2365 clean_tree(iter);
800350a3 2366 tree_put_node(node);
0da2d666 2367 tree_remove_node(node);
25302363 2368 }
153fefbf
MG
2369}
2370
0da2d666 2371static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
25302363 2372{
25302363
MG
2373 if (!root_ns)
2374 return;
2375
0da2d666 2376 clean_tree(&root_ns->ns.node);
25302363
MG
2377}
2378
9b93ab98
GP
2379static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2380{
2381 struct mlx5_flow_steering *steering = dev->priv.steering;
2382 int i;
2383
2384 if (!steering->esw_egress_root_ns)
2385 return;
2386
2387 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2388 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2389
2390 kfree(steering->esw_egress_root_ns);
2391}
2392
2393static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2394{
2395 struct mlx5_flow_steering *steering = dev->priv.steering;
2396 int i;
2397
2398 if (!steering->esw_ingress_root_ns)
2399 return;
2400
2401 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++)
2402 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2403
2404 kfree(steering->esw_ingress_root_ns);
2405}
2406
25302363
MG
2407void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2408{
fba53f7b
MG
2409 struct mlx5_flow_steering *steering = dev->priv.steering;
2410
0da2d666 2411 cleanup_root_ns(steering->root_ns);
9b93ab98
GP
2412 cleanup_egress_acls_root_ns(dev);
2413 cleanup_ingress_acls_root_ns(dev);
0da2d666 2414 cleanup_root_ns(steering->fdb_root_ns);
328edb49
PB
2415 steering->fdb_root_ns = NULL;
2416 kfree(steering->fdb_sub_ns);
2417 steering->fdb_sub_ns = NULL;
87d22483
MG
2418 cleanup_root_ns(steering->sniffer_rx_root_ns);
2419 cleanup_root_ns(steering->sniffer_tx_root_ns);
5f418378 2420 cleanup_root_ns(steering->egress_root_ns);
43a335e0 2421 mlx5_cleanup_fc_stats(dev);
a369d4ac
MG
2422 kmem_cache_destroy(steering->ftes_cache);
2423 kmem_cache_destroy(steering->fgs_cache);
fba53f7b 2424 kfree(steering);
25302363
MG
2425}
2426
87d22483
MG
2427static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2428{
2429 struct fs_prio *prio;
2430
2431 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2432 if (!steering->sniffer_tx_root_ns)
2433 return -ENOMEM;
2434
2435 /* Create single prio */
2436 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2437 if (IS_ERR(prio)) {
2438 cleanup_root_ns(steering->sniffer_tx_root_ns);
2439 return PTR_ERR(prio);
2440 }
2441 return 0;
2442}
2443
2444static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2445{
2446 struct fs_prio *prio;
2447
2448 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2449 if (!steering->sniffer_rx_root_ns)
2450 return -ENOMEM;
2451
2452 /* Create single prio */
2453 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2454 if (IS_ERR(prio)) {
2455 cleanup_root_ns(steering->sniffer_rx_root_ns);
2456 return PTR_ERR(prio);
2457 }
2458 return 0;
2459}
2460
fba53f7b 2461static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
25302363 2462{
328edb49
PB
2463 struct mlx5_flow_namespace *ns;
2464 struct fs_prio *maj_prio;
2465 struct fs_prio *min_prio;
2466 int levels;
2467 int chain;
2468 int prio;
2469 int err;
25302363 2470
fba53f7b
MG
2471 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2472 if (!steering->fdb_root_ns)
25302363
MG
2473 return -ENOMEM;
2474
328edb49 2475 steering->fdb_sub_ns = kzalloc(sizeof(steering->fdb_sub_ns) *
cc3a4cd3 2476 (FDB_MAX_CHAIN + 1), GFP_KERNEL);
328edb49
PB
2477 if (!steering->fdb_sub_ns)
2478 return -ENOMEM;
2479
2480 levels = 2 * FDB_MAX_PRIO * (FDB_MAX_CHAIN + 1);
2481 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns, 0,
2482 levels);
2483 if (IS_ERR(maj_prio)) {
2484 err = PTR_ERR(maj_prio);
1033665e 2485 goto out_err;
328edb49 2486 }
1033665e 2487
328edb49
PB
2488 for (chain = 0; chain <= FDB_MAX_CHAIN; chain++) {
2489 ns = fs_create_namespace(maj_prio);
2490 if (IS_ERR(ns)) {
2491 err = PTR_ERR(ns);
2492 goto out_err;
2493 }
2494
2495 for (prio = 0; prio < FDB_MAX_PRIO * (chain + 1); prio++) {
2496 min_prio = fs_create_prio(ns, prio, 2);
2497 if (IS_ERR(min_prio)) {
2498 err = PTR_ERR(min_prio);
2499 goto out_err;
2500 }
2501 }
2502
2503 steering->fdb_sub_ns[chain] = ns;
2504 }
2505
2506 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
2507 if (IS_ERR(maj_prio)) {
2508 err = PTR_ERR(maj_prio);
1033665e 2509 goto out_err;
328edb49 2510 }
1033665e
OG
2511
2512 set_prio_attrs(steering->fdb_root_ns);
2513 return 0;
2514
2515out_err:
2516 cleanup_root_ns(steering->fdb_root_ns);
328edb49
PB
2517 kfree(steering->fdb_sub_ns);
2518 steering->fdb_sub_ns = NULL;
1033665e 2519 steering->fdb_root_ns = NULL;
328edb49 2520 return err;
25302363
MG
2521}
2522
9b93ab98 2523static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
efdc810b
MHY
2524{
2525 struct fs_prio *prio;
2526
9b93ab98
GP
2527 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2528 if (!steering->esw_egress_root_ns[vport])
efdc810b
MHY
2529 return -ENOMEM;
2530
2531 /* create 1 prio*/
9b93ab98 2532 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
44fafdaa 2533 return PTR_ERR_OR_ZERO(prio);
efdc810b
MHY
2534}
2535
9b93ab98 2536static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
efdc810b
MHY
2537{
2538 struct fs_prio *prio;
2539
9b93ab98
GP
2540 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2541 if (!steering->esw_ingress_root_ns[vport])
efdc810b
MHY
2542 return -ENOMEM;
2543
2544 /* create 1 prio*/
9b93ab98 2545 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
44fafdaa 2546 return PTR_ERR_OR_ZERO(prio);
efdc810b
MHY
2547}
2548
9b93ab98
GP
2549static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2550{
2551 struct mlx5_flow_steering *steering = dev->priv.steering;
2552 int err;
2553 int i;
2554
2555 steering->esw_egress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2556 sizeof(*steering->esw_egress_root_ns),
2557 GFP_KERNEL);
2558 if (!steering->esw_egress_root_ns)
2559 return -ENOMEM;
2560
2561 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2562 err = init_egress_acl_root_ns(steering, i);
2563 if (err)
2564 goto cleanup_root_ns;
2565 }
2566
2567 return 0;
2568
2569cleanup_root_ns:
2570 for (i--; i >= 0; i--)
2571 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2572 kfree(steering->esw_egress_root_ns);
2573 return err;
2574}
2575
2576static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2577{
2578 struct mlx5_flow_steering *steering = dev->priv.steering;
2579 int err;
2580 int i;
2581
2582 steering->esw_ingress_root_ns = kcalloc(MLX5_TOTAL_VPORTS(dev),
2583 sizeof(*steering->esw_ingress_root_ns),
2584 GFP_KERNEL);
2585 if (!steering->esw_ingress_root_ns)
2586 return -ENOMEM;
2587
2588 for (i = 0; i < MLX5_TOTAL_VPORTS(dev); i++) {
2589 err = init_ingress_acl_root_ns(steering, i);
2590 if (err)
2591 goto cleanup_root_ns;
2592 }
2593
2594 return 0;
2595
2596cleanup_root_ns:
2597 for (i--; i >= 0; i--)
2598 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2599 kfree(steering->esw_ingress_root_ns);
2600 return err;
2601}
2602
5f418378
AY
2603static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2604{
8ce78257 2605 int err;
5f418378
AY
2606
2607 steering->egress_root_ns = create_root_ns(steering,
2608 FS_FT_NIC_TX);
2609 if (!steering->egress_root_ns)
2610 return -ENOMEM;
2611
8ce78257
MB
2612 err = init_root_tree(steering, &egress_root_fs,
2613 &steering->egress_root_ns->ns.node);
2614 if (err)
2615 goto cleanup;
2616 set_prio_attrs(steering->egress_root_ns);
2617 return 0;
2618cleanup:
2619 cleanup_root_ns(steering->egress_root_ns);
2620 steering->egress_root_ns = NULL;
2621 return err;
5f418378
AY
2622}
2623
25302363
MG
2624int mlx5_init_fs(struct mlx5_core_dev *dev)
2625{
fba53f7b 2626 struct mlx5_flow_steering *steering;
25302363
MG
2627 int err = 0;
2628
43a335e0
AV
2629 err = mlx5_init_fc_stats(dev);
2630 if (err)
2631 return err;
2632
fba53f7b
MG
2633 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2634 if (!steering)
2635 return -ENOMEM;
2636 steering->dev = dev;
2637 dev->priv.steering = steering;
2638
a369d4ac
MG
2639 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2640 sizeof(struct mlx5_flow_group), 0,
2641 0, NULL);
2642 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2643 0, NULL);
2644 if (!steering->ftes_cache || !steering->fgs_cache) {
2645 err = -ENOMEM;
2646 goto err;
2647 }
2648
ffdb8827
ES
2649 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2650 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2651 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2652 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
876d634d 2653 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
fba53f7b 2654 err = init_root_ns(steering);
25302363 2655 if (err)
43a335e0 2656 goto err;
25302363 2657 }
876d634d 2658
0efc8562 2659 if (MLX5_ESWITCH_MANAGER(dev)) {
bd02ef8e 2660 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
fba53f7b 2661 err = init_fdb_root_ns(steering);
bd02ef8e
MG
2662 if (err)
2663 goto err;
2664 }
2665 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
9b93ab98 2666 err = init_egress_acls_root_ns(dev);
bd02ef8e
MG
2667 if (err)
2668 goto err;
2669 }
2670 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
9b93ab98 2671 err = init_ingress_acls_root_ns(dev);
bd02ef8e
MG
2672 if (err)
2673 goto err;
2674 }
25302363
MG
2675 }
2676
87d22483
MG
2677 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2678 err = init_sniffer_rx_root_ns(steering);
2679 if (err)
2680 goto err;
2681 }
2682
2683 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2684 err = init_sniffer_tx_root_ns(steering);
2685 if (err)
2686 goto err;
2687 }
2688
8ce78257 2689 if (MLX5_IPSEC_DEV(dev) || MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
5f418378
AY
2690 err = init_egress_root_ns(steering);
2691 if (err)
2692 goto err;
2693 }
2694
efdc810b
MHY
2695 return 0;
2696err:
2697 mlx5_cleanup_fs(dev);
25302363
MG
2698 return err;
2699}
50854114
YH
2700
2701int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2702{
2703 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
dae37456
AV
2704 struct mlx5_ft_underlay_qp *new_uqp;
2705 int err = 0;
2706
2707 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
2708 if (!new_uqp)
2709 return -ENOMEM;
2710
2711 mutex_lock(&root->chain_lock);
2712
2713 if (!root->root_ft) {
2714 err = -EINVAL;
2715 goto update_ft_fail;
2716 }
2717
af76c501
MB
2718 err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2719 false);
dae37456
AV
2720 if (err) {
2721 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
2722 underlay_qpn, err);
2723 goto update_ft_fail;
2724 }
2725
2726 new_uqp->qpn = underlay_qpn;
2727 list_add_tail(&new_uqp->list, &root->underlay_qpns);
2728
2729 mutex_unlock(&root->chain_lock);
50854114 2730
50854114 2731 return 0;
dae37456
AV
2732
2733update_ft_fail:
2734 mutex_unlock(&root->chain_lock);
2735 kfree(new_uqp);
2736 return err;
50854114
YH
2737}
2738EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2739
2740int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2741{
2742 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
dae37456
AV
2743 struct mlx5_ft_underlay_qp *uqp;
2744 bool found = false;
2745 int err = 0;
2746
2747 mutex_lock(&root->chain_lock);
2748 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2749 if (uqp->qpn == underlay_qpn) {
2750 found = true;
2751 break;
2752 }
2753 }
2754
2755 if (!found) {
2756 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
2757 underlay_qpn);
2758 err = -EINVAL;
2759 goto out;
2760 }
2761
af76c501
MB
2762 err = root->cmds->update_root_ft(dev, root->root_ft, underlay_qpn,
2763 true);
dae37456
AV
2764 if (err)
2765 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
2766 underlay_qpn, err);
2767
2768 list_del(&uqp->list);
2769 mutex_unlock(&root->chain_lock);
2770 kfree(uqp);
50854114 2771
50854114 2772 return 0;
dae37456
AV
2773
2774out:
2775 mutex_unlock(&root->chain_lock);
2776 return err;
50854114
YH
2777}
2778EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);