]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/ethernet/mellanox/mlx5/core/fs_core.c
Merge tag 'actions-drivers-for-4.13' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_core.c
CommitLineData
de8575e0
MG
1/*
2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/mutex.h>
34#include <linux/mlx5/driver.h>
35
36#include "mlx5_core.h"
37#include "fs_core.h"
0c56b975
MG
38#include "fs_cmd.h"
39
25302363
MG
40#define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
41 sizeof(struct init_tree_node))
42
a257b94a 43#define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
8d40d162 44 ...) {.type = FS_TYPE_PRIO,\
25302363 45 .min_ft_level = min_level_val,\
a257b94a 46 .num_levels = num_levels_val,\
4cbdd30e 47 .num_leaf_prios = num_prios_val,\
8d40d162 48 .caps = caps_val,\
25302363
MG
49 .children = (struct init_tree_node[]) {__VA_ARGS__},\
50 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
51}
52
a257b94a
MG
53#define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
54 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
4cbdd30e 55 __VA_ARGS__)\
25302363
MG
56
57#define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
58 .children = (struct init_tree_node[]) {__VA_ARGS__},\
59 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
60}
61
8d40d162
MG
62#define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
63 sizeof(long))
64
65#define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
66
67#define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
68 .caps = (long[]) {__VA_ARGS__} }
69
6dc6071c
MG
70#define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
71 FS_CAP(flow_table_properties_nic_receive.modify_root), \
72 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
73 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
74
a257b94a 75#define LEFTOVERS_NUM_LEVELS 1
4cbdd30e 76#define LEFTOVERS_NUM_PRIOS 1
4cbdd30e 77
a257b94a 78#define BY_PASS_PRIO_NUM_LEVELS 1
6dc6071c 79#define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
a257b94a
MG
80 LEFTOVERS_NUM_PRIOS)
81
6dc6071c 82#define ETHTOOL_PRIO_NUM_LEVELS 1
e5835f28 83#define ETHTOOL_NUM_PRIOS 11
6dc6071c 84#define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
1cabe6b0
MG
85/* Vlan, mac, ttc, aRFS */
86#define KERNEL_NIC_PRIO_NUM_LEVELS 4
13de6c10
MG
87#define KERNEL_NIC_NUM_PRIOS 1
88/* One more level for tc */
89#define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
8d40d162 90
a257b94a 91#define ANCHOR_NUM_LEVELS 1
153fefbf
MG
92#define ANCHOR_NUM_PRIOS 1
93#define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
acbc2004
OG
94
95#define OFFLOADS_MAX_FT 1
96#define OFFLOADS_NUM_PRIOS 1
97#define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + 1)
98
3e75d4eb
AH
99#define LAG_PRIO_NUM_LEVELS 1
100#define LAG_NUM_PRIOS 1
101#define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
102
8d40d162
MG
103struct node_caps {
104 size_t arr_sz;
105 long *caps;
106};
25302363
MG
107static struct init_tree_node {
108 enum fs_node_type type;
109 struct init_tree_node *children;
110 int ar_size;
8d40d162 111 struct node_caps caps;
25302363 112 int min_ft_level;
4cbdd30e 113 int num_leaf_prios;
25302363 114 int prio;
a257b94a 115 int num_levels;
25302363
MG
116} root_fs = {
117 .type = FS_TYPE_NAMESPACE,
3e75d4eb 118 .ar_size = 7,
25302363 119 .children = (struct init_tree_node[]) {
4cbdd30e 120 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
6dc6071c 121 FS_CHAINING_CAPS,
a257b94a
MG
122 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
123 BY_PASS_PRIO_NUM_LEVELS))),
3e75d4eb
AH
124 ADD_PRIO(0, LAG_MIN_LEVEL, 0,
125 FS_CHAINING_CAPS,
126 ADD_NS(ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
127 LAG_PRIO_NUM_LEVELS))),
acbc2004
OG
128 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, {},
129 ADD_NS(ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS, OFFLOADS_MAX_FT))),
6dc6071c
MG
130 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0,
131 FS_CHAINING_CAPS,
132 ADD_NS(ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
133 ETHTOOL_PRIO_NUM_LEVELS))),
a257b94a 134 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
13de6c10
MG
135 ADD_NS(ADD_MULTIPLE_PRIO(1, 1),
136 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
137 KERNEL_NIC_PRIO_NUM_LEVELS))),
4cbdd30e 138 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
6dc6071c 139 FS_CHAINING_CAPS,
a257b94a 140 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_NUM_LEVELS))),
153fefbf 141 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
a257b94a 142 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_NUM_LEVELS))),
25302363
MG
143 }
144};
145
f0d22d18
MG
146enum fs_i_mutex_lock_class {
147 FS_MUTEX_GRANDPARENT,
148 FS_MUTEX_PARENT,
149 FS_MUTEX_CHILD
150};
151
0c56b975
MG
152static void del_rule(struct fs_node *node);
153static void del_flow_table(struct fs_node *node);
154static void del_flow_group(struct fs_node *node);
155static void del_fte(struct fs_node *node);
814fb875
MB
156static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
157 struct mlx5_flow_destination *d2);
74491de9
MB
158static struct mlx5_flow_rule *
159find_flow_rule(struct fs_fte *fte,
160 struct mlx5_flow_destination *dest);
de8575e0
MG
161
162static void tree_init_node(struct fs_node *node,
163 unsigned int refcount,
164 void (*remove_func)(struct fs_node *))
165{
166 atomic_set(&node->refcount, refcount);
167 INIT_LIST_HEAD(&node->list);
168 INIT_LIST_HEAD(&node->children);
169 mutex_init(&node->lock);
170 node->remove_func = remove_func;
171}
172
173static void tree_add_node(struct fs_node *node, struct fs_node *parent)
174{
175 if (parent)
176 atomic_inc(&parent->refcount);
177 node->parent = parent;
178
179 /* Parent is the root */
180 if (!parent)
181 node->root = node;
182 else
183 node->root = parent->root;
184}
185
186static void tree_get_node(struct fs_node *node)
187{
188 atomic_inc(&node->refcount);
189}
190
f0d22d18
MG
191static void nested_lock_ref_node(struct fs_node *node,
192 enum fs_i_mutex_lock_class class)
de8575e0
MG
193{
194 if (node) {
f0d22d18 195 mutex_lock_nested(&node->lock, class);
de8575e0
MG
196 atomic_inc(&node->refcount);
197 }
198}
199
200static void lock_ref_node(struct fs_node *node)
201{
202 if (node) {
203 mutex_lock(&node->lock);
204 atomic_inc(&node->refcount);
205 }
206}
207
208static void unlock_ref_node(struct fs_node *node)
209{
210 if (node) {
211 atomic_dec(&node->refcount);
212 mutex_unlock(&node->lock);
213 }
214}
215
216static void tree_put_node(struct fs_node *node)
217{
218 struct fs_node *parent_node = node->parent;
219
220 lock_ref_node(parent_node);
221 if (atomic_dec_and_test(&node->refcount)) {
222 if (parent_node)
223 list_del_init(&node->list);
224 if (node->remove_func)
225 node->remove_func(node);
226 kfree(node);
227 node = NULL;
228 }
229 unlock_ref_node(parent_node);
230 if (!node && parent_node)
231 tree_put_node(parent_node);
232}
233
234static int tree_remove_node(struct fs_node *node)
235{
b3638e1a
MG
236 if (atomic_read(&node->refcount) > 1) {
237 atomic_dec(&node->refcount);
238 return -EEXIST;
239 }
de8575e0
MG
240 tree_put_node(node);
241 return 0;
242}
5e1626c0
MG
243
244static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
245 unsigned int prio)
246{
247 struct fs_prio *iter_prio;
248
249 fs_for_each_prio(iter_prio, ns) {
250 if (iter_prio->prio == prio)
251 return iter_prio;
252 }
253
254 return NULL;
255}
256
5e1626c0
MG
257static bool masked_memcmp(void *mask, void *val1, void *val2, size_t size)
258{
259 unsigned int i;
260
261 for (i = 0; i < size; i++, mask++, val1++, val2++)
262 if ((*((u8 *)val1) & (*(u8 *)mask)) !=
263 ((*(u8 *)val2) & (*(u8 *)mask)))
264 return false;
265
266 return true;
267}
268
269static bool compare_match_value(struct mlx5_flow_group_mask *mask,
270 void *fte_param1, void *fte_param2)
271{
272 if (mask->match_criteria_enable &
273 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) {
274 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
275 fte_param1, outer_headers);
276 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
277 fte_param2, outer_headers);
278 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
279 mask->match_criteria, outer_headers);
280
281 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
282 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
283 return false;
284 }
285
286 if (mask->match_criteria_enable &
287 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) {
288 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
289 fte_param1, misc_parameters);
290 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
291 fte_param2, misc_parameters);
292 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
293 mask->match_criteria, misc_parameters);
294
295 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
296 MLX5_ST_SZ_BYTES(fte_match_set_misc)))
297 return false;
298 }
299
300 if (mask->match_criteria_enable &
301 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS) {
302 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
303 fte_param1, inner_headers);
304 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
305 fte_param2, inner_headers);
306 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
307 mask->match_criteria, inner_headers);
308
309 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
310 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
311 return false;
312 }
313 return true;
314}
315
316static bool compare_match_criteria(u8 match_criteria_enable1,
317 u8 match_criteria_enable2,
318 void *mask1, void *mask2)
319{
320 return match_criteria_enable1 == match_criteria_enable2 &&
321 !memcmp(mask1, mask2, MLX5_ST_SZ_BYTES(fte_match_param));
322}
0c56b975
MG
323
324static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
325{
326 struct fs_node *root;
327 struct mlx5_flow_namespace *ns;
328
329 root = node->root;
330
331 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
332 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
333 return NULL;
334 }
335
336 ns = container_of(root, struct mlx5_flow_namespace, node);
337 return container_of(ns, struct mlx5_flow_root_namespace, ns);
338}
339
340static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
341{
342 struct mlx5_flow_root_namespace *root = find_root(node);
343
344 if (root)
345 return root->dev;
346 return NULL;
347}
348
349static void del_flow_table(struct fs_node *node)
350{
351 struct mlx5_flow_table *ft;
352 struct mlx5_core_dev *dev;
353 struct fs_prio *prio;
354 int err;
355
356 fs_get_obj(ft, node);
357 dev = get_dev(&ft->node);
358
359 err = mlx5_cmd_destroy_flow_table(dev, ft);
360 if (err)
2974ab6e 361 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
0c56b975
MG
362 fs_get_obj(prio, ft->node.parent);
363 prio->num_ft--;
364}
365
366static void del_rule(struct fs_node *node)
367{
368 struct mlx5_flow_rule *rule;
369 struct mlx5_flow_table *ft;
370 struct mlx5_flow_group *fg;
371 struct fs_fte *fte;
372 u32 *match_value;
bd5251db 373 int modify_mask;
0c56b975
MG
374 struct mlx5_core_dev *dev = get_dev(node);
375 int match_len = MLX5_ST_SZ_BYTES(fte_match_param);
376 int err;
ae058314 377 bool update_fte = false;
0c56b975
MG
378
379 match_value = mlx5_vzalloc(match_len);
380 if (!match_value) {
2974ab6e 381 mlx5_core_warn(dev, "failed to allocate inbox\n");
0c56b975
MG
382 return;
383 }
384
385 fs_get_obj(rule, node);
386 fs_get_obj(fte, rule->node.parent);
387 fs_get_obj(fg, fte->node.parent);
388 memcpy(match_value, fte->val, sizeof(fte->val));
389 fs_get_obj(ft, fg->node.parent);
390 list_del(&rule->node.list);
b3638e1a
MG
391 if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
392 mutex_lock(&rule->dest_attr.ft->lock);
393 list_del(&rule->next_ft);
394 mutex_unlock(&rule->dest_attr.ft->lock);
395 }
ae058314
MB
396
397 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
398 --fte->dests_size) {
399 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
400 fte->action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
401 update_fte = true;
402 goto out;
403 }
404
60ab4584
AV
405 if ((fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
406 --fte->dests_size) {
bd5251db 407 modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST),
ae058314
MB
408 update_fte = true;
409 }
410out:
411 if (update_fte && fte->dests_size) {
412 err = mlx5_cmd_update_fte(dev, ft, fg->id, modify_mask, fte);
0c56b975 413 if (err)
2974ab6e
SM
414 mlx5_core_warn(dev,
415 "%s can't del rule fg id=%d fte_index=%d\n",
416 __func__, fg->id, fte->index);
0c56b975
MG
417 }
418 kvfree(match_value);
419}
420
421static void del_fte(struct fs_node *node)
422{
423 struct mlx5_flow_table *ft;
424 struct mlx5_flow_group *fg;
425 struct mlx5_core_dev *dev;
426 struct fs_fte *fte;
427 int err;
428
429 fs_get_obj(fte, node);
430 fs_get_obj(fg, fte->node.parent);
431 fs_get_obj(ft, fg->node.parent);
432
433 dev = get_dev(&ft->node);
434 err = mlx5_cmd_delete_fte(dev, ft,
435 fte->index);
436 if (err)
2974ab6e
SM
437 mlx5_core_warn(dev,
438 "flow steering can't delete fte in index %d of flow group id %d\n",
439 fte->index, fg->id);
0c56b975
MG
440
441 fte->status = 0;
442 fg->num_ftes--;
443}
444
445static void del_flow_group(struct fs_node *node)
446{
447 struct mlx5_flow_group *fg;
448 struct mlx5_flow_table *ft;
449 struct mlx5_core_dev *dev;
450
451 fs_get_obj(fg, node);
452 fs_get_obj(ft, fg->node.parent);
453 dev = get_dev(&ft->node);
454
32dba76a
PB
455 if (ft->autogroup.active)
456 ft->autogroup.num_groups--;
457
0c56b975 458 if (mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
2974ab6e
SM
459 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
460 fg->id, ft->id);
0c56b975
MG
461}
462
66958ed9 463static struct fs_fte *alloc_fte(struct mlx5_flow_act *flow_act,
0c56b975
MG
464 u32 *match_value,
465 unsigned int index)
466{
467 struct fs_fte *fte;
468
469 fte = kzalloc(sizeof(*fte), GFP_KERNEL);
470 if (!fte)
471 return ERR_PTR(-ENOMEM);
472
473 memcpy(fte->val, match_value, sizeof(fte->val));
474 fte->node.type = FS_TYPE_FLOW_ENTRY;
66958ed9 475 fte->flow_tag = flow_act->flow_tag;
0c56b975 476 fte->index = index;
66958ed9
HHZ
477 fte->action = flow_act->action;
478 fte->encap_id = flow_act->encap_id;
2a69cb9f 479 fte->modify_id = flow_act->modify_id;
0c56b975
MG
480
481 return fte;
482}
483
484static struct mlx5_flow_group *alloc_flow_group(u32 *create_fg_in)
485{
486 struct mlx5_flow_group *fg;
487 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
488 create_fg_in, match_criteria);
489 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
490 create_fg_in,
491 match_criteria_enable);
492 fg = kzalloc(sizeof(*fg), GFP_KERNEL);
493 if (!fg)
494 return ERR_PTR(-ENOMEM);
495
496 fg->mask.match_criteria_enable = match_criteria_enable;
497 memcpy(&fg->mask.match_criteria, match_criteria,
498 sizeof(fg->mask.match_criteria));
499 fg->node.type = FS_TYPE_FLOW_GROUP;
500 fg->start_index = MLX5_GET(create_flow_group_in, create_fg_in,
501 start_flow_index);
502 fg->max_ftes = MLX5_GET(create_flow_group_in, create_fg_in,
503 end_flow_index) - fg->start_index + 1;
504 return fg;
505}
506
efdc810b 507static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
aaff1bea 508 enum fs_flow_table_type table_type,
c9f1b073
HHZ
509 enum fs_flow_table_op_mod op_mod,
510 u32 flags)
0c56b975
MG
511{
512 struct mlx5_flow_table *ft;
513
514 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
515 if (!ft)
516 return NULL;
517
518 ft->level = level;
519 ft->node.type = FS_TYPE_FLOW_TABLE;
aaff1bea 520 ft->op_mod = op_mod;
0c56b975 521 ft->type = table_type;
efdc810b 522 ft->vport = vport;
0c56b975 523 ft->max_fte = max_fte;
c9f1b073 524 ft->flags = flags;
b3638e1a
MG
525 INIT_LIST_HEAD(&ft->fwd_rules);
526 mutex_init(&ft->lock);
0c56b975
MG
527
528 return ft;
529}
530
fdb6896f
MG
531/* If reverse is false, then we search for the first flow table in the
532 * root sub-tree from start(closest from right), else we search for the
533 * last flow table in the root sub-tree till start(closest from left).
534 */
535static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
536 struct list_head *start,
537 bool reverse)
538{
539#define list_advance_entry(pos, reverse) \
540 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
541
542#define list_for_each_advance_continue(pos, head, reverse) \
543 for (pos = list_advance_entry(pos, reverse); \
544 &pos->list != (head); \
545 pos = list_advance_entry(pos, reverse))
546
547 struct fs_node *iter = list_entry(start, struct fs_node, list);
548 struct mlx5_flow_table *ft = NULL;
549
550 if (!root)
551 return NULL;
552
553 list_for_each_advance_continue(iter, &root->children, reverse) {
554 if (iter->type == FS_TYPE_FLOW_TABLE) {
555 fs_get_obj(ft, iter);
556 return ft;
557 }
558 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
559 if (ft)
560 return ft;
561 }
562
563 return ft;
564}
565
566/* If reverse if false then return the first flow table in next priority of
567 * prio in the tree, else return the last flow table in the previous priority
568 * of prio in the tree.
569 */
570static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
571{
572 struct mlx5_flow_table *ft = NULL;
573 struct fs_node *curr_node;
574 struct fs_node *parent;
575
576 parent = prio->node.parent;
577 curr_node = &prio->node;
578 while (!ft && parent) {
579 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
580 curr_node = parent;
581 parent = curr_node->parent;
582 }
583 return ft;
584}
585
586/* Assuming all the tree is locked by mutex chain lock */
587static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
588{
589 return find_closest_ft(prio, false);
590}
591
592/* Assuming all the tree is locked by mutex chain lock */
593static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
594{
595 return find_closest_ft(prio, true);
596}
597
f90edfd2
MG
598static int connect_fts_in_prio(struct mlx5_core_dev *dev,
599 struct fs_prio *prio,
600 struct mlx5_flow_table *ft)
601{
602 struct mlx5_flow_table *iter;
603 int i = 0;
604 int err;
605
606 fs_for_each_ft(iter, prio) {
607 i++;
608 err = mlx5_cmd_modify_flow_table(dev,
609 iter,
610 ft);
611 if (err) {
612 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
613 iter->id);
614 /* The driver is out of sync with the FW */
615 if (i > 1)
616 WARN_ON(true);
617 return err;
618 }
619 }
620 return 0;
621}
622
623/* Connect flow tables from previous priority of prio to ft */
624static int connect_prev_fts(struct mlx5_core_dev *dev,
625 struct mlx5_flow_table *ft,
626 struct fs_prio *prio)
627{
628 struct mlx5_flow_table *prev_ft;
629
630 prev_ft = find_prev_chained_ft(prio);
631 if (prev_ft) {
632 struct fs_prio *prev_prio;
633
634 fs_get_obj(prev_prio, prev_ft->node.parent);
635 return connect_fts_in_prio(dev, prev_prio, ft);
636 }
637 return 0;
638}
639
2cc43b49
MG
640static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
641 *prio)
642{
643 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
644 int min_level = INT_MAX;
645 int err;
646
647 if (root->root_ft)
648 min_level = root->root_ft->level;
649
650 if (ft->level >= min_level)
651 return 0;
652
50854114 653 err = mlx5_cmd_update_root_ft(root->dev, ft, root->underlay_qpn);
2cc43b49
MG
654 if (err)
655 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
656 ft->id);
657 else
658 root->root_ft = ft;
659
660 return err;
661}
662
74491de9
MB
663static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
664 struct mlx5_flow_destination *dest)
b3638e1a
MG
665{
666 struct mlx5_flow_table *ft;
667 struct mlx5_flow_group *fg;
668 struct fs_fte *fte;
bd5251db 669 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
b3638e1a
MG
670 int err = 0;
671
672 fs_get_obj(fte, rule->node.parent);
673 if (!(fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
674 return -EINVAL;
675 lock_ref_node(&fte->node);
676 fs_get_obj(fg, fte->node.parent);
677 fs_get_obj(ft, fg->node.parent);
678
679 memcpy(&rule->dest_attr, dest, sizeof(*dest));
680 err = mlx5_cmd_update_fte(get_dev(&ft->node),
bd5251db
AV
681 ft, fg->id,
682 modify_mask,
683 fte);
b3638e1a
MG
684 unlock_ref_node(&fte->node);
685
686 return err;
687}
688
74491de9
MB
689int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
690 struct mlx5_flow_destination *new_dest,
691 struct mlx5_flow_destination *old_dest)
692{
693 int i;
694
695 if (!old_dest) {
696 if (handle->num_rules != 1)
697 return -EINVAL;
698 return _mlx5_modify_rule_destination(handle->rule[0],
699 new_dest);
700 }
701
702 for (i = 0; i < handle->num_rules; i++) {
703 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
704 return _mlx5_modify_rule_destination(handle->rule[i],
705 new_dest);
706 }
707
708 return -EINVAL;
709}
710
b3638e1a
MG
711/* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
712static int connect_fwd_rules(struct mlx5_core_dev *dev,
713 struct mlx5_flow_table *new_next_ft,
714 struct mlx5_flow_table *old_next_ft)
715{
716 struct mlx5_flow_destination dest;
717 struct mlx5_flow_rule *iter;
718 int err = 0;
719
720 /* new_next_ft and old_next_ft could be NULL only
721 * when we create/destroy the anchor flow table.
722 */
723 if (!new_next_ft || !old_next_ft)
724 return 0;
725
726 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
727 dest.ft = new_next_ft;
728
729 mutex_lock(&old_next_ft->lock);
730 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
731 mutex_unlock(&old_next_ft->lock);
732 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
74491de9 733 err = _mlx5_modify_rule_destination(iter, &dest);
b3638e1a
MG
734 if (err)
735 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
736 new_next_ft->id);
737 }
738 return 0;
739}
740
f90edfd2
MG
741static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
742 struct fs_prio *prio)
743{
b3638e1a 744 struct mlx5_flow_table *next_ft;
f90edfd2
MG
745 int err = 0;
746
747 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
748
749 if (list_empty(&prio->node.children)) {
750 err = connect_prev_fts(dev, ft, prio);
751 if (err)
752 return err;
b3638e1a
MG
753
754 next_ft = find_next_chained_ft(prio);
755 err = connect_fwd_rules(dev, ft, next_ft);
756 if (err)
757 return err;
f90edfd2
MG
758 }
759
760 if (MLX5_CAP_FLOWTABLE(dev,
761 flow_table_properties_nic_receive.modify_root))
762 err = update_root_ft_create(ft, prio);
763 return err;
764}
765
d63cd286
MG
766static void list_add_flow_table(struct mlx5_flow_table *ft,
767 struct fs_prio *prio)
768{
769 struct list_head *prev = &prio->node.children;
770 struct mlx5_flow_table *iter;
771
772 fs_for_each_ft(iter, prio) {
773 if (iter->level > ft->level)
774 break;
775 prev = &iter->node.list;
776 }
777 list_add(&ft->node.list, prev);
778}
779
efdc810b 780static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
b3ba5149 781 struct mlx5_flow_table_attr *ft_attr,
aaff1bea 782 enum fs_flow_table_op_mod op_mod,
b3ba5149 783 u16 vport)
0c56b975 784{
b3ba5149 785 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
f90edfd2 786 struct mlx5_flow_table *next_ft = NULL;
b3ba5149 787 struct fs_prio *fs_prio = NULL;
0c56b975 788 struct mlx5_flow_table *ft;
0c56b975 789 int log_table_sz;
b3ba5149 790 int err;
0c56b975
MG
791
792 if (!root) {
793 pr_err("mlx5: flow steering failed to find root of namespace\n");
794 return ERR_PTR(-ENODEV);
795 }
796
2cc43b49 797 mutex_lock(&root->chain_lock);
b3ba5149 798 fs_prio = find_prio(ns, ft_attr->prio);
2cc43b49
MG
799 if (!fs_prio) {
800 err = -EINVAL;
801 goto unlock_root;
802 }
b3ba5149 803 if (ft_attr->level >= fs_prio->num_levels) {
0c56b975 804 err = -ENOSPC;
2cc43b49 805 goto unlock_root;
0c56b975 806 }
d63cd286
MG
807 /* The level is related to the
808 * priority level range.
809 */
b3ba5149
ES
810 ft_attr->level += fs_prio->start_level;
811 ft = alloc_flow_table(ft_attr->level,
efdc810b 812 vport,
b3ba5149 813 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
aaff1bea 814 root->table_type,
b3ba5149 815 op_mod, ft_attr->flags);
0c56b975
MG
816 if (!ft) {
817 err = -ENOMEM;
2cc43b49 818 goto unlock_root;
0c56b975
MG
819 }
820
821 tree_init_node(&ft->node, 1, del_flow_table);
aaff1bea 822 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
f90edfd2 823 next_ft = find_next_chained_ft(fs_prio);
aaff1bea 824 err = mlx5_cmd_create_flow_table(root->dev, ft->vport, ft->op_mod, ft->type,
c9f1b073
HHZ
825 ft->level, log_table_sz, next_ft, &ft->id,
826 ft->flags);
0c56b975
MG
827 if (err)
828 goto free_ft;
829
f90edfd2
MG
830 err = connect_flow_table(root->dev, ft, fs_prio);
831 if (err)
832 goto destroy_ft;
2cc43b49 833 lock_ref_node(&fs_prio->node);
0c56b975 834 tree_add_node(&ft->node, &fs_prio->node);
d63cd286 835 list_add_flow_table(ft, fs_prio);
0c56b975
MG
836 fs_prio->num_ft++;
837 unlock_ref_node(&fs_prio->node);
2cc43b49 838 mutex_unlock(&root->chain_lock);
0c56b975 839 return ft;
2cc43b49
MG
840destroy_ft:
841 mlx5_cmd_destroy_flow_table(root->dev, ft);
0c56b975
MG
842free_ft:
843 kfree(ft);
2cc43b49
MG
844unlock_root:
845 mutex_unlock(&root->chain_lock);
0c56b975
MG
846 return ERR_PTR(err);
847}
848
efdc810b 849struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
b3ba5149 850 struct mlx5_flow_table_attr *ft_attr)
efdc810b 851{
b3ba5149 852 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
efdc810b
MHY
853}
854
855struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
856 int prio, int max_fte,
857 u32 level, u16 vport)
858{
b3ba5149
ES
859 struct mlx5_flow_table_attr ft_attr = {};
860
861 ft_attr.max_fte = max_fte;
862 ft_attr.level = level;
863 ft_attr.prio = prio;
864
865 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, 0);
efdc810b
MHY
866}
867
b3ba5149
ES
868struct mlx5_flow_table*
869mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
870 int prio, u32 level)
aaff1bea 871{
b3ba5149
ES
872 struct mlx5_flow_table_attr ft_attr = {};
873
874 ft_attr.level = level;
875 ft_attr.prio = prio;
876 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
aaff1bea
AH
877}
878EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
879
b3ba5149
ES
880struct mlx5_flow_table*
881mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
882 int prio,
883 int num_flow_table_entries,
884 int max_num_groups,
885 u32 level,
886 u32 flags)
f0d22d18 887{
b3ba5149 888 struct mlx5_flow_table_attr ft_attr = {};
f0d22d18
MG
889 struct mlx5_flow_table *ft;
890
891 if (max_num_groups > num_flow_table_entries)
892 return ERR_PTR(-EINVAL);
893
b3ba5149
ES
894 ft_attr.max_fte = num_flow_table_entries;
895 ft_attr.prio = prio;
896 ft_attr.level = level;
897 ft_attr.flags = flags;
898
899 ft = mlx5_create_flow_table(ns, &ft_attr);
f0d22d18
MG
900 if (IS_ERR(ft))
901 return ft;
902
903 ft->autogroup.active = true;
904 ft->autogroup.required_groups = max_num_groups;
905
906 return ft;
907}
b217ea25 908EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
f0d22d18
MG
909
910/* Flow table should be locked */
911static struct mlx5_flow_group *create_flow_group_common(struct mlx5_flow_table *ft,
912 u32 *fg_in,
913 struct list_head
914 *prev_fg,
915 bool is_auto_fg)
0c56b975
MG
916{
917 struct mlx5_flow_group *fg;
918 struct mlx5_core_dev *dev = get_dev(&ft->node);
919 int err;
920
921 if (!dev)
922 return ERR_PTR(-ENODEV);
923
924 fg = alloc_flow_group(fg_in);
925 if (IS_ERR(fg))
926 return fg;
927
0c56b975
MG
928 err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
929 if (err) {
930 kfree(fg);
0c56b975
MG
931 return ERR_PTR(err);
932 }
f0d22d18
MG
933
934 if (ft->autogroup.active)
935 ft->autogroup.num_groups++;
0c56b975 936 /* Add node to tree */
f0d22d18 937 tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
0c56b975
MG
938 tree_add_node(&fg->node, &ft->node);
939 /* Add node to group list */
eccec8da 940 list_add(&fg->node.list, prev_fg);
f0d22d18
MG
941
942 return fg;
943}
944
945struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
946 u32 *fg_in)
947{
948 struct mlx5_flow_group *fg;
949
950 if (ft->autogroup.active)
951 return ERR_PTR(-EPERM);
952
953 lock_ref_node(&ft->node);
eccec8da 954 fg = create_flow_group_common(ft, fg_in, ft->node.children.prev, false);
0c56b975
MG
955 unlock_ref_node(&ft->node);
956
957 return fg;
958}
959
960static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
961{
962 struct mlx5_flow_rule *rule;
963
964 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
965 if (!rule)
966 return NULL;
967
b3638e1a 968 INIT_LIST_HEAD(&rule->next_ft);
0c56b975 969 rule->node.type = FS_TYPE_FLOW_DEST;
60ab4584
AV
970 if (dest)
971 memcpy(&rule->dest_attr, dest, sizeof(*dest));
0c56b975
MG
972
973 return rule;
974}
975
74491de9
MB
976static struct mlx5_flow_handle *alloc_handle(int num_rules)
977{
978 struct mlx5_flow_handle *handle;
979
980 handle = kzalloc(sizeof(*handle) + sizeof(handle->rule[0]) *
981 num_rules, GFP_KERNEL);
982 if (!handle)
983 return NULL;
984
985 handle->num_rules = num_rules;
986
987 return handle;
988}
989
990static void destroy_flow_handle(struct fs_fte *fte,
991 struct mlx5_flow_handle *handle,
992 struct mlx5_flow_destination *dest,
993 int i)
994{
995 for (; --i >= 0;) {
996 if (atomic_dec_and_test(&handle->rule[i]->node.refcount)) {
997 fte->dests_size--;
998 list_del(&handle->rule[i]->node.list);
999 kfree(handle->rule[i]);
1000 }
1001 }
1002 kfree(handle);
1003}
1004
1005static struct mlx5_flow_handle *
1006create_flow_handle(struct fs_fte *fte,
1007 struct mlx5_flow_destination *dest,
1008 int dest_num,
1009 int *modify_mask,
1010 bool *new_rule)
1011{
1012 struct mlx5_flow_handle *handle;
1013 struct mlx5_flow_rule *rule = NULL;
1014 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1015 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1016 int type;
1017 int i = 0;
1018
1019 handle = alloc_handle((dest_num) ? dest_num : 1);
1020 if (!handle)
1021 return ERR_PTR(-ENOMEM);
1022
1023 do {
1024 if (dest) {
1025 rule = find_flow_rule(fte, dest + i);
1026 if (rule) {
1027 atomic_inc(&rule->node.refcount);
1028 goto rule_found;
1029 }
1030 }
1031
1032 *new_rule = true;
1033 rule = alloc_rule(dest + i);
1034 if (!rule)
1035 goto free_rules;
1036
1037 /* Add dest to dests list- we need flow tables to be in the
1038 * end of the list for forward to next prio rules.
1039 */
1040 tree_init_node(&rule->node, 1, del_rule);
1041 if (dest &&
1042 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1043 list_add(&rule->node.list, &fte->node.children);
1044 else
1045 list_add_tail(&rule->node.list, &fte->node.children);
1046 if (dest) {
1047 fte->dests_size++;
1048
1049 type = dest[i].type ==
1050 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1051 *modify_mask |= type ? count : dst;
1052 }
1053rule_found:
1054 handle->rule[i] = rule;
1055 } while (++i < dest_num);
1056
1057 return handle;
1058
1059free_rules:
1060 destroy_flow_handle(fte, handle, dest, i);
1061 return ERR_PTR(-ENOMEM);
1062}
1063
0c56b975 1064/* fte should not be deleted while calling this function */
74491de9
MB
1065static struct mlx5_flow_handle *
1066add_rule_fte(struct fs_fte *fte,
1067 struct mlx5_flow_group *fg,
1068 struct mlx5_flow_destination *dest,
1069 int dest_num,
1070 bool update_action)
0c56b975 1071{
74491de9 1072 struct mlx5_flow_handle *handle;
0c56b975 1073 struct mlx5_flow_table *ft;
bd5251db 1074 int modify_mask = 0;
0c56b975 1075 int err;
74491de9 1076 bool new_rule = false;
0c56b975 1077
74491de9
MB
1078 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1079 &new_rule);
1080 if (IS_ERR(handle) || !new_rule)
1081 goto out;
bd5251db 1082
a6224985
MB
1083 if (update_action)
1084 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
bd5251db 1085
0c56b975 1086 fs_get_obj(ft, fg->node.parent);
0501fc47 1087 if (!(fte->status & FS_FTE_STATUS_EXISTING))
0c56b975
MG
1088 err = mlx5_cmd_create_fte(get_dev(&ft->node),
1089 ft, fg->id, fte);
1090 else
1091 err = mlx5_cmd_update_fte(get_dev(&ft->node),
bd5251db 1092 ft, fg->id, modify_mask, fte);
0c56b975 1093 if (err)
74491de9 1094 goto free_handle;
0c56b975
MG
1095
1096 fte->status |= FS_FTE_STATUS_EXISTING;
1097
74491de9
MB
1098out:
1099 return handle;
0c56b975 1100
74491de9
MB
1101free_handle:
1102 destroy_flow_handle(fte, handle, dest, handle->num_rules);
0c56b975
MG
1103 return ERR_PTR(err);
1104}
1105
1106/* Assumed fg is locked */
1107static unsigned int get_free_fte_index(struct mlx5_flow_group *fg,
1108 struct list_head **prev)
1109{
1110 struct fs_fte *fte;
1111 unsigned int start = fg->start_index;
1112
1113 if (prev)
1114 *prev = &fg->node.children;
1115
1116 /* assumed list is sorted by index */
1117 fs_for_each_fte(fte, fg) {
1118 if (fte->index != start)
1119 return start;
1120 start++;
1121 if (prev)
1122 *prev = &fte->node.list;
1123 }
1124
1125 return start;
1126}
1127
1128/* prev is output, prev->next = new_fte */
1129static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
1130 u32 *match_value,
66958ed9 1131 struct mlx5_flow_act *flow_act,
0c56b975
MG
1132 struct list_head **prev)
1133{
1134 struct fs_fte *fte;
1135 int index;
1136
1137 index = get_free_fte_index(fg, prev);
66958ed9 1138 fte = alloc_fte(flow_act, match_value, index);
0c56b975
MG
1139 if (IS_ERR(fte))
1140 return fte;
1141
1142 return fte;
1143}
1144
f0d22d18
MG
1145static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
1146 u8 match_criteria_enable,
1147 u32 *match_criteria)
1148{
1149 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
af363705 1150 struct list_head *prev = &ft->node.children;
f0d22d18
MG
1151 unsigned int candidate_index = 0;
1152 struct mlx5_flow_group *fg;
1153 void *match_criteria_addr;
1154 unsigned int group_size = 0;
1155 u32 *in;
1156
1157 if (!ft->autogroup.active)
1158 return ERR_PTR(-ENOENT);
1159
1160 in = mlx5_vzalloc(inlen);
1161 if (!in)
1162 return ERR_PTR(-ENOMEM);
1163
1164 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1165 /* We save place for flow groups in addition to max types */
1166 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
1167
1168 /* ft->max_fte == ft->autogroup.max_types */
1169 if (group_size == 0)
1170 group_size = 1;
1171
1172 /* sorted by start_index */
1173 fs_for_each_fg(fg, ft) {
1174 if (candidate_index + group_size > fg->start_index)
1175 candidate_index = fg->start_index + fg->max_ftes;
1176 else
1177 break;
1178 prev = &fg->node.list;
1179 }
1180
1181 if (candidate_index + group_size > ft->max_fte) {
1182 fg = ERR_PTR(-ENOSPC);
1183 goto out;
1184 }
1185
1186 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1187 match_criteria_enable);
1188 MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
1189 MLX5_SET(create_flow_group_in, in, end_flow_index, candidate_index +
1190 group_size - 1);
1191 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1192 in, match_criteria);
1193 memcpy(match_criteria_addr, match_criteria,
1194 MLX5_ST_SZ_BYTES(fte_match_param));
1195
1196 fg = create_flow_group_common(ft, in, prev, true);
1197out:
1198 kvfree(in);
1199 return fg;
1200}
1201
814fb875
MB
1202static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1203 struct mlx5_flow_destination *d2)
1204{
1205 if (d1->type == d2->type) {
1206 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1207 d1->vport_num == d2->vport_num) ||
1208 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1209 d1->ft == d2->ft) ||
1210 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1211 d1->tir_num == d2->tir_num))
1212 return true;
1213 }
1214
1215 return false;
1216}
1217
b3638e1a
MG
1218static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1219 struct mlx5_flow_destination *dest)
1220{
1221 struct mlx5_flow_rule *rule;
1222
1223 list_for_each_entry(rule, &fte->node.children, node.list) {
814fb875
MB
1224 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1225 return rule;
b3638e1a
MG
1226 }
1227 return NULL;
1228}
1229
74491de9
MB
1230static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1231 u32 *match_value,
66958ed9 1232 struct mlx5_flow_act *flow_act,
74491de9
MB
1233 struct mlx5_flow_destination *dest,
1234 int dest_num)
0c56b975 1235{
74491de9 1236 struct mlx5_flow_handle *handle;
0c56b975
MG
1237 struct mlx5_flow_table *ft;
1238 struct list_head *prev;
74491de9
MB
1239 struct fs_fte *fte;
1240 int i;
0c56b975 1241
f0d22d18 1242 nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
0c56b975 1243 fs_for_each_fte(fte, fg) {
f0d22d18 1244 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
0c56b975 1245 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
e04a0183 1246 (flow_act->action & fte->action)) {
a6224985
MB
1247 int old_action = fte->action;
1248
e04a0183
MG
1249 if (fte->flow_tag != flow_act->flow_tag) {
1250 mlx5_core_warn(get_dev(&fte->node),
1251 "FTE flow tag %u already exists with different flow tag %u\n",
1252 fte->flow_tag,
1253 flow_act->flow_tag);
1254 handle = ERR_PTR(-EEXIST);
1255 goto unlock_fte;
1256 }
1257
66958ed9 1258 fte->action |= flow_act->action;
74491de9 1259 handle = add_rule_fte(fte, fg, dest, dest_num,
66958ed9 1260 old_action != flow_act->action);
74491de9 1261 if (IS_ERR(handle)) {
a6224985 1262 fte->action = old_action;
0fd758d6 1263 goto unlock_fte;
a6224985 1264 } else {
74491de9 1265 goto add_rules;
b3638e1a 1266 }
0c56b975
MG
1267 }
1268 unlock_ref_node(&fte->node);
1269 }
1270 fs_get_obj(ft, fg->node.parent);
1271 if (fg->num_ftes >= fg->max_ftes) {
74491de9 1272 handle = ERR_PTR(-ENOSPC);
0c56b975
MG
1273 goto unlock_fg;
1274 }
1275
66958ed9 1276 fte = create_fte(fg, match_value, flow_act, &prev);
0c56b975 1277 if (IS_ERR(fte)) {
74491de9 1278 handle = (void *)fte;
0c56b975
MG
1279 goto unlock_fg;
1280 }
1281 tree_init_node(&fte->node, 0, del_fte);
0fd758d6 1282 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
74491de9
MB
1283 handle = add_rule_fte(fte, fg, dest, dest_num, false);
1284 if (IS_ERR(handle)) {
9b8c5142 1285 unlock_ref_node(&fte->node);
0c56b975
MG
1286 kfree(fte);
1287 goto unlock_fg;
1288 }
1289
1290 fg->num_ftes++;
1291
1292 tree_add_node(&fte->node, &fg->node);
1293 list_add(&fte->node.list, prev);
74491de9
MB
1294add_rules:
1295 for (i = 0; i < handle->num_rules; i++) {
1296 if (atomic_read(&handle->rule[i]->node.refcount) == 1)
1297 tree_add_node(&handle->rule[i]->node, &fte->node);
1298 }
0fd758d6
MB
1299unlock_fte:
1300 unlock_ref_node(&fte->node);
0c56b975
MG
1301unlock_fg:
1302 unlock_ref_node(&fg->node);
74491de9 1303 return handle;
0c56b975
MG
1304}
1305
74491de9 1306struct mlx5_fc *mlx5_flow_rule_counter(struct mlx5_flow_handle *handle)
bd5251db
AV
1307{
1308 struct mlx5_flow_rule *dst;
1309 struct fs_fte *fte;
1310
74491de9 1311 fs_get_obj(fte, handle->rule[0]->node.parent);
bd5251db
AV
1312
1313 fs_for_each_dst(dst, fte) {
1314 if (dst->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER)
1315 return dst->dest_attr.counter;
1316 }
1317
1318 return NULL;
1319}
1320
1321static bool counter_is_valid(struct mlx5_fc *counter, u32 action)
1322{
1323 if (!(action & MLX5_FLOW_CONTEXT_ACTION_COUNT))
1324 return !counter;
1325
1326 if (!counter)
1327 return false;
1328
ae058314 1329 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
eafa6abd 1330 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
bd5251db
AV
1331}
1332
d63cd286
MG
1333static bool dest_is_valid(struct mlx5_flow_destination *dest,
1334 u32 action,
1335 struct mlx5_flow_table *ft)
1336{
bd5251db
AV
1337 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1338 return counter_is_valid(dest->counter, action);
1339
d63cd286
MG
1340 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1341 return true;
1342
1343 if (!dest || ((dest->type ==
1344 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1345 (dest->ft->level <= ft->level)))
1346 return false;
1347 return true;
1348}
1349
74491de9
MB
1350static struct mlx5_flow_handle *
1351_mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1352 struct mlx5_flow_spec *spec,
66958ed9 1353 struct mlx5_flow_act *flow_act,
74491de9
MB
1354 struct mlx5_flow_destination *dest,
1355 int dest_num)
66958ed9 1356
0c56b975
MG
1357{
1358 struct mlx5_flow_group *g;
74491de9
MB
1359 struct mlx5_flow_handle *rule;
1360 int i;
0c56b975 1361
74491de9 1362 for (i = 0; i < dest_num; i++) {
66958ed9 1363 if (!dest_is_valid(&dest[i], flow_act->action, ft))
74491de9
MB
1364 return ERR_PTR(-EINVAL);
1365 }
60ab4584 1366
f0d22d18 1367 nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
0c56b975
MG
1368 fs_for_each_fg(g, ft)
1369 if (compare_match_criteria(g->mask.match_criteria_enable,
c5bb1730 1370 spec->match_criteria_enable,
0c56b975 1371 g->mask.match_criteria,
c5bb1730
MG
1372 spec->match_criteria)) {
1373 rule = add_rule_fg(g, spec->match_value,
66958ed9 1374 flow_act, dest, dest_num);
f0d22d18
MG
1375 if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
1376 goto unlock;
0c56b975 1377 }
f0d22d18 1378
c5bb1730
MG
1379 g = create_autogroup(ft, spec->match_criteria_enable,
1380 spec->match_criteria);
c3f9bf62
MG
1381 if (IS_ERR(g)) {
1382 rule = (void *)g;
1383 goto unlock;
1384 }
1385
66958ed9 1386 rule = add_rule_fg(g, spec->match_value, flow_act, dest, dest_num);
c3f9bf62
MG
1387 if (IS_ERR(rule)) {
1388 /* Remove assumes refcount > 0 and autogroup creates a group
1389 * with a refcount = 0.
1390 */
1391 unlock_ref_node(&ft->node);
1392 tree_get_node(&g->node);
1393 tree_remove_node(&g->node);
1394 return rule;
1395 }
f0d22d18 1396unlock:
0c56b975 1397 unlock_ref_node(&ft->node);
0c56b975
MG
1398 return rule;
1399}
b3638e1a
MG
1400
1401static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1402{
1403 return ((ft->type == FS_FT_NIC_RX) &&
1404 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1405}
1406
74491de9
MB
1407struct mlx5_flow_handle *
1408mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1409 struct mlx5_flow_spec *spec,
66958ed9 1410 struct mlx5_flow_act *flow_act,
74491de9
MB
1411 struct mlx5_flow_destination *dest,
1412 int dest_num)
b3638e1a
MG
1413{
1414 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1415 struct mlx5_flow_destination gen_dest;
1416 struct mlx5_flow_table *next_ft = NULL;
74491de9 1417 struct mlx5_flow_handle *handle = NULL;
66958ed9 1418 u32 sw_action = flow_act->action;
b3638e1a
MG
1419 struct fs_prio *prio;
1420
1421 fs_get_obj(prio, ft->node.parent);
66958ed9 1422 if (flow_act->action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
b3638e1a
MG
1423 if (!fwd_next_prio_supported(ft))
1424 return ERR_PTR(-EOPNOTSUPP);
1425 if (dest)
1426 return ERR_PTR(-EINVAL);
1427 mutex_lock(&root->chain_lock);
1428 next_ft = find_next_chained_ft(prio);
1429 if (next_ft) {
1430 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1431 gen_dest.ft = next_ft;
1432 dest = &gen_dest;
74491de9 1433 dest_num = 1;
66958ed9 1434 flow_act->action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
b3638e1a
MG
1435 } else {
1436 mutex_unlock(&root->chain_lock);
1437 return ERR_PTR(-EOPNOTSUPP);
1438 }
1439 }
1440
66958ed9 1441 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, dest_num);
b3638e1a
MG
1442
1443 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
74491de9
MB
1444 if (!IS_ERR_OR_NULL(handle) &&
1445 (list_empty(&handle->rule[0]->next_ft))) {
b3638e1a 1446 mutex_lock(&next_ft->lock);
74491de9
MB
1447 list_add(&handle->rule[0]->next_ft,
1448 &next_ft->fwd_rules);
b3638e1a 1449 mutex_unlock(&next_ft->lock);
74491de9 1450 handle->rule[0]->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
b3638e1a
MG
1451 }
1452 mutex_unlock(&root->chain_lock);
1453 }
74491de9 1454 return handle;
b3638e1a 1455}
74491de9 1456EXPORT_SYMBOL(mlx5_add_flow_rules);
0c56b975 1457
74491de9 1458void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
0c56b975 1459{
74491de9
MB
1460 int i;
1461
1462 for (i = handle->num_rules - 1; i >= 0; i--)
1463 tree_remove_node(&handle->rule[i]->node);
1464 kfree(handle);
0c56b975 1465}
74491de9 1466EXPORT_SYMBOL(mlx5_del_flow_rules);
0c56b975 1467
2cc43b49
MG
1468/* Assuming prio->node.children(flow tables) is sorted by level */
1469static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1470{
1471 struct fs_prio *prio;
1472
1473 fs_get_obj(prio, ft->node.parent);
1474
1475 if (!list_is_last(&ft->node.list, &prio->node.children))
1476 return list_next_entry(ft, node.list);
1477 return find_next_chained_ft(prio);
1478}
1479
1480static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1481{
1482 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1483 struct mlx5_flow_table *new_root_ft = NULL;
1484
1485 if (root->root_ft != ft)
1486 return 0;
1487
1488 new_root_ft = find_next_ft(ft);
1489 if (new_root_ft) {
50854114
YH
1490 int err = mlx5_cmd_update_root_ft(root->dev, new_root_ft,
1491 root->underlay_qpn);
2cc43b49
MG
1492
1493 if (err) {
1494 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
1495 ft->id);
1496 return err;
1497 }
2cc43b49 1498 }
2fee37a4 1499 root->root_ft = new_root_ft;
2cc43b49
MG
1500 return 0;
1501}
1502
f90edfd2
MG
1503/* Connect flow table from previous priority to
1504 * the next flow table.
1505 */
1506static int disconnect_flow_table(struct mlx5_flow_table *ft)
1507{
1508 struct mlx5_core_dev *dev = get_dev(&ft->node);
1509 struct mlx5_flow_table *next_ft;
1510 struct fs_prio *prio;
1511 int err = 0;
1512
1513 err = update_root_ft_destroy(ft);
1514 if (err)
1515 return err;
1516
1517 fs_get_obj(prio, ft->node.parent);
1518 if (!(list_first_entry(&prio->node.children,
1519 struct mlx5_flow_table,
1520 node.list) == ft))
1521 return 0;
1522
1523 next_ft = find_next_chained_ft(prio);
b3638e1a
MG
1524 err = connect_fwd_rules(dev, next_ft, ft);
1525 if (err)
1526 return err;
1527
f90edfd2
MG
1528 err = connect_prev_fts(dev, next_ft, prio);
1529 if (err)
1530 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1531 ft->id);
1532 return err;
1533}
1534
86d722ad 1535int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
0c56b975 1536{
2cc43b49
MG
1537 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1538 int err = 0;
1539
1540 mutex_lock(&root->chain_lock);
f90edfd2 1541 err = disconnect_flow_table(ft);
2cc43b49
MG
1542 if (err) {
1543 mutex_unlock(&root->chain_lock);
1544 return err;
1545 }
0c56b975
MG
1546 if (tree_remove_node(&ft->node))
1547 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1548 ft->id);
2cc43b49 1549 mutex_unlock(&root->chain_lock);
0c56b975 1550
2cc43b49 1551 return err;
0c56b975 1552}
b217ea25 1553EXPORT_SYMBOL(mlx5_destroy_flow_table);
0c56b975 1554
86d722ad 1555void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
0c56b975
MG
1556{
1557 if (tree_remove_node(&fg->node))
1558 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1559 fg->id);
1560}
25302363 1561
86d722ad
MG
1562struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1563 enum mlx5_flow_namespace_type type)
25302363 1564{
fba53f7b
MG
1565 struct mlx5_flow_steering *steering = dev->priv.steering;
1566 struct mlx5_flow_root_namespace *root_ns;
25302363 1567 int prio;
78228cbd 1568 struct fs_prio *fs_prio;
25302363
MG
1569 struct mlx5_flow_namespace *ns;
1570
fba53f7b 1571 if (!steering)
25302363
MG
1572 return NULL;
1573
1574 switch (type) {
4cbdd30e 1575 case MLX5_FLOW_NAMESPACE_BYPASS:
3e75d4eb 1576 case MLX5_FLOW_NAMESPACE_LAG:
acbc2004 1577 case MLX5_FLOW_NAMESPACE_OFFLOADS:
6dc6071c 1578 case MLX5_FLOW_NAMESPACE_ETHTOOL:
25302363 1579 case MLX5_FLOW_NAMESPACE_KERNEL:
4cbdd30e 1580 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
153fefbf 1581 case MLX5_FLOW_NAMESPACE_ANCHOR:
4cbdd30e 1582 prio = type;
25302363
MG
1583 break;
1584 case MLX5_FLOW_NAMESPACE_FDB:
fba53f7b
MG
1585 if (steering->fdb_root_ns)
1586 return &steering->fdb_root_ns->ns;
25302363
MG
1587 else
1588 return NULL;
efdc810b 1589 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
fba53f7b
MG
1590 if (steering->esw_egress_root_ns)
1591 return &steering->esw_egress_root_ns->ns;
efdc810b
MHY
1592 else
1593 return NULL;
1594 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
fba53f7b
MG
1595 if (steering->esw_ingress_root_ns)
1596 return &steering->esw_ingress_root_ns->ns;
efdc810b
MHY
1597 else
1598 return NULL;
87d22483
MG
1599 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
1600 if (steering->sniffer_rx_root_ns)
1601 return &steering->sniffer_rx_root_ns->ns;
1602 else
1603 return NULL;
1604 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
1605 if (steering->sniffer_tx_root_ns)
1606 return &steering->sniffer_tx_root_ns->ns;
1607 else
1608 return NULL;
25302363
MG
1609 default:
1610 return NULL;
1611 }
1612
fba53f7b
MG
1613 root_ns = steering->root_ns;
1614 if (!root_ns)
1615 return NULL;
1616
25302363
MG
1617 fs_prio = find_prio(&root_ns->ns, prio);
1618 if (!fs_prio)
1619 return NULL;
1620
1621 ns = list_first_entry(&fs_prio->node.children,
1622 typeof(*ns),
1623 node.list);
1624
1625 return ns;
1626}
b217ea25 1627EXPORT_SYMBOL(mlx5_get_flow_namespace);
25302363
MG
1628
1629static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
a257b94a 1630 unsigned int prio, int num_levels)
25302363
MG
1631{
1632 struct fs_prio *fs_prio;
1633
1634 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
1635 if (!fs_prio)
1636 return ERR_PTR(-ENOMEM);
1637
1638 fs_prio->node.type = FS_TYPE_PRIO;
1639 tree_init_node(&fs_prio->node, 1, NULL);
1640 tree_add_node(&fs_prio->node, &ns->node);
a257b94a 1641 fs_prio->num_levels = num_levels;
25302363 1642 fs_prio->prio = prio;
25302363
MG
1643 list_add_tail(&fs_prio->node.list, &ns->node.children);
1644
1645 return fs_prio;
1646}
1647
1648static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
1649 *ns)
1650{
1651 ns->node.type = FS_TYPE_NAMESPACE;
1652
1653 return ns;
1654}
1655
1656static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
1657{
1658 struct mlx5_flow_namespace *ns;
1659
1660 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1661 if (!ns)
1662 return ERR_PTR(-ENOMEM);
1663
1664 fs_init_namespace(ns);
1665 tree_init_node(&ns->node, 1, NULL);
1666 tree_add_node(&ns->node, &prio->node);
1667 list_add_tail(&ns->node.list, &prio->node.children);
1668
1669 return ns;
1670}
1671
13de6c10
MG
1672static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
1673 struct init_tree_node *prio_metadata)
4cbdd30e
MG
1674{
1675 struct fs_prio *fs_prio;
1676 int i;
1677
1678 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
13de6c10 1679 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
4cbdd30e
MG
1680 if (IS_ERR(fs_prio))
1681 return PTR_ERR(fs_prio);
1682 }
1683 return 0;
1684}
1685
8d40d162
MG
1686#define FLOW_TABLE_BIT_SZ 1
1687#define GET_FLOW_TABLE_CAP(dev, offset) \
701052c5 1688 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
8d40d162
MG
1689 offset / 32)) >> \
1690 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
1691static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
1692{
1693 int i;
1694
1695 for (i = 0; i < caps->arr_sz; i++) {
1696 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
1697 return false;
1698 }
1699 return true;
1700}
1701
fba53f7b 1702static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
8d40d162 1703 struct init_tree_node *init_node,
25302363
MG
1704 struct fs_node *fs_parent_node,
1705 struct init_tree_node *init_parent_node,
13de6c10 1706 int prio)
25302363 1707{
fba53f7b 1708 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
8d40d162
MG
1709 flow_table_properties_nic_receive.
1710 max_ft_level);
25302363
MG
1711 struct mlx5_flow_namespace *fs_ns;
1712 struct fs_prio *fs_prio;
1713 struct fs_node *base;
1714 int i;
1715 int err;
1716
1717 if (init_node->type == FS_TYPE_PRIO) {
8d40d162 1718 if ((init_node->min_ft_level > max_ft_level) ||
fba53f7b 1719 !has_required_caps(steering->dev, &init_node->caps))
8d40d162 1720 return 0;
25302363
MG
1721
1722 fs_get_obj(fs_ns, fs_parent_node);
4cbdd30e 1723 if (init_node->num_leaf_prios)
13de6c10
MG
1724 return create_leaf_prios(fs_ns, prio, init_node);
1725 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
25302363
MG
1726 if (IS_ERR(fs_prio))
1727 return PTR_ERR(fs_prio);
1728 base = &fs_prio->node;
1729 } else if (init_node->type == FS_TYPE_NAMESPACE) {
1730 fs_get_obj(fs_prio, fs_parent_node);
1731 fs_ns = fs_create_namespace(fs_prio);
1732 if (IS_ERR(fs_ns))
1733 return PTR_ERR(fs_ns);
1734 base = &fs_ns->node;
1735 } else {
1736 return -EINVAL;
1737 }
13de6c10 1738 prio = 0;
25302363 1739 for (i = 0; i < init_node->ar_size; i++) {
fba53f7b 1740 err = init_root_tree_recursive(steering, &init_node->children[i],
13de6c10 1741 base, init_node, prio);
25302363
MG
1742 if (err)
1743 return err;
13de6c10
MG
1744 if (init_node->children[i].type == FS_TYPE_PRIO &&
1745 init_node->children[i].num_leaf_prios) {
1746 prio += init_node->children[i].num_leaf_prios;
1747 }
25302363
MG
1748 }
1749
1750 return 0;
1751}
1752
fba53f7b 1753static int init_root_tree(struct mlx5_flow_steering *steering,
8d40d162 1754 struct init_tree_node *init_node,
25302363
MG
1755 struct fs_node *fs_parent_node)
1756{
1757 int i;
1758 struct mlx5_flow_namespace *fs_ns;
1759 int err;
1760
1761 fs_get_obj(fs_ns, fs_parent_node);
1762 for (i = 0; i < init_node->ar_size; i++) {
fba53f7b 1763 err = init_root_tree_recursive(steering, &init_node->children[i],
25302363
MG
1764 &fs_ns->node,
1765 init_node, i);
1766 if (err)
1767 return err;
1768 }
1769 return 0;
1770}
1771
fba53f7b 1772static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_flow_steering *steering,
25302363
MG
1773 enum fs_flow_table_type
1774 table_type)
1775{
1776 struct mlx5_flow_root_namespace *root_ns;
1777 struct mlx5_flow_namespace *ns;
1778
86d722ad 1779 /* Create the root namespace */
25302363
MG
1780 root_ns = mlx5_vzalloc(sizeof(*root_ns));
1781 if (!root_ns)
1782 return NULL;
1783
fba53f7b 1784 root_ns->dev = steering->dev;
25302363
MG
1785 root_ns->table_type = table_type;
1786
1787 ns = &root_ns->ns;
1788 fs_init_namespace(ns);
2cc43b49 1789 mutex_init(&root_ns->chain_lock);
25302363
MG
1790 tree_init_node(&ns->node, 1, NULL);
1791 tree_add_node(&ns->node, NULL);
1792
1793 return root_ns;
1794}
1795
655227ed
MG
1796static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
1797
1798static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
1799{
1800 struct fs_prio *prio;
1801
1802 fs_for_each_prio(prio, ns) {
a257b94a 1803 /* This updates prio start_level and num_levels */
655227ed 1804 set_prio_attrs_in_prio(prio, acc_level);
a257b94a 1805 acc_level += prio->num_levels;
655227ed
MG
1806 }
1807 return acc_level;
1808}
1809
1810static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
1811{
1812 struct mlx5_flow_namespace *ns;
1813 int acc_level_ns = acc_level;
1814
1815 prio->start_level = acc_level;
1816 fs_for_each_ns(ns, prio)
a257b94a 1817 /* This updates start_level and num_levels of ns's priority descendants */
655227ed 1818 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
a257b94a
MG
1819 if (!prio->num_levels)
1820 prio->num_levels = acc_level_ns - prio->start_level;
1821 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
655227ed
MG
1822}
1823
1824static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
1825{
1826 struct mlx5_flow_namespace *ns = &root_ns->ns;
1827 struct fs_prio *prio;
1828 int start_level = 0;
1829
1830 fs_for_each_prio(prio, ns) {
1831 set_prio_attrs_in_prio(prio, start_level);
a257b94a 1832 start_level += prio->num_levels;
655227ed
MG
1833 }
1834}
1835
153fefbf
MG
1836#define ANCHOR_PRIO 0
1837#define ANCHOR_SIZE 1
d63cd286 1838#define ANCHOR_LEVEL 0
fba53f7b 1839static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
153fefbf
MG
1840{
1841 struct mlx5_flow_namespace *ns = NULL;
b3ba5149 1842 struct mlx5_flow_table_attr ft_attr = {};
153fefbf
MG
1843 struct mlx5_flow_table *ft;
1844
fba53f7b 1845 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
eff596da 1846 if (WARN_ON(!ns))
153fefbf 1847 return -EINVAL;
b3ba5149
ES
1848
1849 ft_attr.max_fte = ANCHOR_SIZE;
1850 ft_attr.level = ANCHOR_LEVEL;
1851 ft_attr.prio = ANCHOR_PRIO;
1852
1853 ft = mlx5_create_flow_table(ns, &ft_attr);
153fefbf 1854 if (IS_ERR(ft)) {
fba53f7b 1855 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
153fefbf
MG
1856 return PTR_ERR(ft);
1857 }
1858 return 0;
1859}
1860
fba53f7b 1861static int init_root_ns(struct mlx5_flow_steering *steering)
25302363 1862{
25302363 1863
fba53f7b 1864 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
42fb18fd 1865 if (!steering->root_ns)
25302363
MG
1866 goto cleanup;
1867
fba53f7b 1868 if (init_root_tree(steering, &root_fs, &steering->root_ns->ns.node))
25302363
MG
1869 goto cleanup;
1870
fba53f7b 1871 set_prio_attrs(steering->root_ns);
655227ed 1872
fba53f7b 1873 if (create_anchor_flow_table(steering))
153fefbf
MG
1874 goto cleanup;
1875
25302363
MG
1876 return 0;
1877
1878cleanup:
fba53f7b 1879 mlx5_cleanup_fs(steering->dev);
25302363
MG
1880 return -ENOMEM;
1881}
1882
0da2d666 1883static void clean_tree(struct fs_node *node)
25302363 1884{
0da2d666
MG
1885 if (node) {
1886 struct fs_node *iter;
1887 struct fs_node *temp;
25302363 1888
0da2d666
MG
1889 list_for_each_entry_safe(iter, temp, &node->children, list)
1890 clean_tree(iter);
1891 tree_remove_node(node);
25302363 1892 }
153fefbf
MG
1893}
1894
0da2d666 1895static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
25302363 1896{
25302363
MG
1897 if (!root_ns)
1898 return;
1899
0da2d666 1900 clean_tree(&root_ns->ns.node);
25302363
MG
1901}
1902
1903void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1904{
fba53f7b
MG
1905 struct mlx5_flow_steering *steering = dev->priv.steering;
1906
0da2d666
MG
1907 cleanup_root_ns(steering->root_ns);
1908 cleanup_root_ns(steering->esw_egress_root_ns);
1909 cleanup_root_ns(steering->esw_ingress_root_ns);
1910 cleanup_root_ns(steering->fdb_root_ns);
87d22483
MG
1911 cleanup_root_ns(steering->sniffer_rx_root_ns);
1912 cleanup_root_ns(steering->sniffer_tx_root_ns);
43a335e0 1913 mlx5_cleanup_fc_stats(dev);
fba53f7b 1914 kfree(steering);
25302363
MG
1915}
1916
87d22483
MG
1917static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
1918{
1919 struct fs_prio *prio;
1920
1921 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
1922 if (!steering->sniffer_tx_root_ns)
1923 return -ENOMEM;
1924
1925 /* Create single prio */
1926 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
1927 if (IS_ERR(prio)) {
1928 cleanup_root_ns(steering->sniffer_tx_root_ns);
1929 return PTR_ERR(prio);
1930 }
1931 return 0;
1932}
1933
1934static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
1935{
1936 struct fs_prio *prio;
1937
1938 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
1939 if (!steering->sniffer_rx_root_ns)
1940 return -ENOMEM;
1941
1942 /* Create single prio */
1943 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
1944 if (IS_ERR(prio)) {
1945 cleanup_root_ns(steering->sniffer_rx_root_ns);
1946 return PTR_ERR(prio);
1947 }
1948 return 0;
1949}
1950
fba53f7b 1951static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
25302363
MG
1952{
1953 struct fs_prio *prio;
1954
fba53f7b
MG
1955 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
1956 if (!steering->fdb_root_ns)
25302363
MG
1957 return -ENOMEM;
1958
fba53f7b 1959 prio = fs_create_prio(&steering->fdb_root_ns->ns, 0, 1);
1033665e
OG
1960 if (IS_ERR(prio))
1961 goto out_err;
1962
1963 prio = fs_create_prio(&steering->fdb_root_ns->ns, 1, 1);
1964 if (IS_ERR(prio))
1965 goto out_err;
1966
1967 set_prio_attrs(steering->fdb_root_ns);
1968 return 0;
1969
1970out_err:
1971 cleanup_root_ns(steering->fdb_root_ns);
1972 steering->fdb_root_ns = NULL;
1973 return PTR_ERR(prio);
25302363
MG
1974}
1975
fba53f7b 1976static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering)
efdc810b
MHY
1977{
1978 struct fs_prio *prio;
1979
fba53f7b
MG
1980 steering->esw_egress_root_ns = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
1981 if (!steering->esw_egress_root_ns)
efdc810b
MHY
1982 return -ENOMEM;
1983
1984 /* create 1 prio*/
fba53f7b
MG
1985 prio = fs_create_prio(&steering->esw_egress_root_ns->ns, 0,
1986 MLX5_TOTAL_VPORTS(steering->dev));
44fafdaa 1987 return PTR_ERR_OR_ZERO(prio);
efdc810b
MHY
1988}
1989
fba53f7b 1990static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering)
efdc810b
MHY
1991{
1992 struct fs_prio *prio;
1993
fba53f7b
MG
1994 steering->esw_ingress_root_ns = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
1995 if (!steering->esw_ingress_root_ns)
efdc810b
MHY
1996 return -ENOMEM;
1997
1998 /* create 1 prio*/
fba53f7b
MG
1999 prio = fs_create_prio(&steering->esw_ingress_root_ns->ns, 0,
2000 MLX5_TOTAL_VPORTS(steering->dev));
44fafdaa 2001 return PTR_ERR_OR_ZERO(prio);
efdc810b
MHY
2002}
2003
25302363
MG
2004int mlx5_init_fs(struct mlx5_core_dev *dev)
2005{
fba53f7b 2006 struct mlx5_flow_steering *steering;
25302363
MG
2007 int err = 0;
2008
43a335e0
AV
2009 err = mlx5_init_fc_stats(dev);
2010 if (err)
2011 return err;
2012
fba53f7b
MG
2013 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2014 if (!steering)
2015 return -ENOMEM;
2016 steering->dev = dev;
2017 dev->priv.steering = steering;
2018
ffdb8827
ES
2019 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2020 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2021 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2022 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
876d634d 2023 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
fba53f7b 2024 err = init_root_ns(steering);
25302363 2025 if (err)
43a335e0 2026 goto err;
25302363 2027 }
876d634d 2028
25302363 2029 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
bd02ef8e 2030 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
fba53f7b 2031 err = init_fdb_root_ns(steering);
bd02ef8e
MG
2032 if (err)
2033 goto err;
2034 }
2035 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
fba53f7b 2036 err = init_egress_acl_root_ns(steering);
bd02ef8e
MG
2037 if (err)
2038 goto err;
2039 }
2040 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
fba53f7b 2041 err = init_ingress_acl_root_ns(steering);
bd02ef8e
MG
2042 if (err)
2043 goto err;
2044 }
25302363
MG
2045 }
2046
87d22483
MG
2047 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2048 err = init_sniffer_rx_root_ns(steering);
2049 if (err)
2050 goto err;
2051 }
2052
2053 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2054 err = init_sniffer_tx_root_ns(steering);
2055 if (err)
2056 goto err;
2057 }
2058
efdc810b
MHY
2059 return 0;
2060err:
2061 mlx5_cleanup_fs(dev);
25302363
MG
2062 return err;
2063}
50854114
YH
2064
2065int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2066{
2067 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2068
2069 root->underlay_qpn = underlay_qpn;
2070 return 0;
2071}
2072EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
2073
2074int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
2075{
2076 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
2077
2078 root->underlay_qpn = 0;
2079 return 0;
2080}
2081EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);