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