]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/net/ethernet/mellanox/mlx5/core/fs_core.c
net/mlx5_core: Add utilities to find next and prev flow-tables
[mirror_ubuntu-zesty-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
43#define INIT_PRIO(min_level_val, max_ft_val,\
44 start_level_val, ...) {.type = FS_TYPE_PRIO,\
45 .min_ft_level = min_level_val,\
46 .start_level = start_level_val,\
47 .max_ft = max_ft_val,\
48 .children = (struct init_tree_node[]) {__VA_ARGS__},\
49 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
50}
51
52#define ADD_PRIO(min_level_val, max_ft_val, start_level_val, ...)\
53 INIT_PRIO(min_level_val, max_ft_val, start_level_val,\
54 __VA_ARGS__)\
55
56#define ADD_FT_PRIO(max_ft_val, start_level_val, ...)\
57 INIT_PRIO(0, max_ft_val, start_level_val,\
58 __VA_ARGS__)\
59
60#define ADD_NS(...) {.type = FS_TYPE_NAMESPACE,\
61 .children = (struct init_tree_node[]) {__VA_ARGS__},\
62 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
63}
64
65#define KERNEL_START_LEVEL 0
66#define KERNEL_P0_START_LEVEL KERNEL_START_LEVEL
67#define KERNEL_MAX_FT 2
68#define KENREL_MIN_LEVEL 2
69static struct init_tree_node {
70 enum fs_node_type type;
71 struct init_tree_node *children;
72 int ar_size;
73 int min_ft_level;
74 int prio;
75 int max_ft;
76 int start_level;
77} root_fs = {
78 .type = FS_TYPE_NAMESPACE,
79 .ar_size = 1,
80 .children = (struct init_tree_node[]) {
81 ADD_PRIO(KENREL_MIN_LEVEL, KERNEL_MAX_FT,
82 KERNEL_START_LEVEL,
83 ADD_NS(ADD_FT_PRIO(KERNEL_MAX_FT,
84 KERNEL_P0_START_LEVEL))),
85 }
86};
87
f0d22d18
MG
88enum fs_i_mutex_lock_class {
89 FS_MUTEX_GRANDPARENT,
90 FS_MUTEX_PARENT,
91 FS_MUTEX_CHILD
92};
93
0c56b975
MG
94static void del_rule(struct fs_node *node);
95static void del_flow_table(struct fs_node *node);
96static void del_flow_group(struct fs_node *node);
97static void del_fte(struct fs_node *node);
de8575e0
MG
98
99static void tree_init_node(struct fs_node *node,
100 unsigned int refcount,
101 void (*remove_func)(struct fs_node *))
102{
103 atomic_set(&node->refcount, refcount);
104 INIT_LIST_HEAD(&node->list);
105 INIT_LIST_HEAD(&node->children);
106 mutex_init(&node->lock);
107 node->remove_func = remove_func;
108}
109
110static void tree_add_node(struct fs_node *node, struct fs_node *parent)
111{
112 if (parent)
113 atomic_inc(&parent->refcount);
114 node->parent = parent;
115
116 /* Parent is the root */
117 if (!parent)
118 node->root = node;
119 else
120 node->root = parent->root;
121}
122
123static void tree_get_node(struct fs_node *node)
124{
125 atomic_inc(&node->refcount);
126}
127
f0d22d18
MG
128static void nested_lock_ref_node(struct fs_node *node,
129 enum fs_i_mutex_lock_class class)
de8575e0
MG
130{
131 if (node) {
f0d22d18 132 mutex_lock_nested(&node->lock, class);
de8575e0
MG
133 atomic_inc(&node->refcount);
134 }
135}
136
137static void lock_ref_node(struct fs_node *node)
138{
139 if (node) {
140 mutex_lock(&node->lock);
141 atomic_inc(&node->refcount);
142 }
143}
144
145static void unlock_ref_node(struct fs_node *node)
146{
147 if (node) {
148 atomic_dec(&node->refcount);
149 mutex_unlock(&node->lock);
150 }
151}
152
153static void tree_put_node(struct fs_node *node)
154{
155 struct fs_node *parent_node = node->parent;
156
157 lock_ref_node(parent_node);
158 if (atomic_dec_and_test(&node->refcount)) {
159 if (parent_node)
160 list_del_init(&node->list);
161 if (node->remove_func)
162 node->remove_func(node);
163 kfree(node);
164 node = NULL;
165 }
166 unlock_ref_node(parent_node);
167 if (!node && parent_node)
168 tree_put_node(parent_node);
169}
170
171static int tree_remove_node(struct fs_node *node)
172{
173 if (atomic_read(&node->refcount) > 1)
174 return -EPERM;
175 tree_put_node(node);
176 return 0;
177}
5e1626c0
MG
178
179static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
180 unsigned int prio)
181{
182 struct fs_prio *iter_prio;
183
184 fs_for_each_prio(iter_prio, ns) {
185 if (iter_prio->prio == prio)
186 return iter_prio;
187 }
188
189 return NULL;
190}
191
192static unsigned int find_next_free_level(struct fs_prio *prio)
193{
194 if (!list_empty(&prio->node.children)) {
195 struct mlx5_flow_table *ft;
196
197 ft = list_last_entry(&prio->node.children,
198 struct mlx5_flow_table,
199 node.list);
200 return ft->level + 1;
201 }
202 return prio->start_level;
203}
204
205static bool masked_memcmp(void *mask, void *val1, void *val2, size_t size)
206{
207 unsigned int i;
208
209 for (i = 0; i < size; i++, mask++, val1++, val2++)
210 if ((*((u8 *)val1) & (*(u8 *)mask)) !=
211 ((*(u8 *)val2) & (*(u8 *)mask)))
212 return false;
213
214 return true;
215}
216
217static bool compare_match_value(struct mlx5_flow_group_mask *mask,
218 void *fte_param1, void *fte_param2)
219{
220 if (mask->match_criteria_enable &
221 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) {
222 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
223 fte_param1, outer_headers);
224 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
225 fte_param2, outer_headers);
226 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
227 mask->match_criteria, outer_headers);
228
229 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
230 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
231 return false;
232 }
233
234 if (mask->match_criteria_enable &
235 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) {
236 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
237 fte_param1, misc_parameters);
238 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
239 fte_param2, misc_parameters);
240 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
241 mask->match_criteria, misc_parameters);
242
243 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
244 MLX5_ST_SZ_BYTES(fte_match_set_misc)))
245 return false;
246 }
247
248 if (mask->match_criteria_enable &
249 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS) {
250 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
251 fte_param1, inner_headers);
252 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
253 fte_param2, inner_headers);
254 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
255 mask->match_criteria, inner_headers);
256
257 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
258 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
259 return false;
260 }
261 return true;
262}
263
264static bool compare_match_criteria(u8 match_criteria_enable1,
265 u8 match_criteria_enable2,
266 void *mask1, void *mask2)
267{
268 return match_criteria_enable1 == match_criteria_enable2 &&
269 !memcmp(mask1, mask2, MLX5_ST_SZ_BYTES(fte_match_param));
270}
0c56b975
MG
271
272static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
273{
274 struct fs_node *root;
275 struct mlx5_flow_namespace *ns;
276
277 root = node->root;
278
279 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
280 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
281 return NULL;
282 }
283
284 ns = container_of(root, struct mlx5_flow_namespace, node);
285 return container_of(ns, struct mlx5_flow_root_namespace, ns);
286}
287
288static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
289{
290 struct mlx5_flow_root_namespace *root = find_root(node);
291
292 if (root)
293 return root->dev;
294 return NULL;
295}
296
297static void del_flow_table(struct fs_node *node)
298{
299 struct mlx5_flow_table *ft;
300 struct mlx5_core_dev *dev;
301 struct fs_prio *prio;
302 int err;
303
304 fs_get_obj(ft, node);
305 dev = get_dev(&ft->node);
306
307 err = mlx5_cmd_destroy_flow_table(dev, ft);
308 if (err)
309 pr_warn("flow steering can't destroy ft\n");
310 fs_get_obj(prio, ft->node.parent);
311 prio->num_ft--;
312}
313
314static void del_rule(struct fs_node *node)
315{
316 struct mlx5_flow_rule *rule;
317 struct mlx5_flow_table *ft;
318 struct mlx5_flow_group *fg;
319 struct fs_fte *fte;
320 u32 *match_value;
321 struct mlx5_core_dev *dev = get_dev(node);
322 int match_len = MLX5_ST_SZ_BYTES(fte_match_param);
323 int err;
324
325 match_value = mlx5_vzalloc(match_len);
326 if (!match_value) {
327 pr_warn("failed to allocate inbox\n");
328 return;
329 }
330
331 fs_get_obj(rule, node);
332 fs_get_obj(fte, rule->node.parent);
333 fs_get_obj(fg, fte->node.parent);
334 memcpy(match_value, fte->val, sizeof(fte->val));
335 fs_get_obj(ft, fg->node.parent);
336 list_del(&rule->node.list);
337 fte->dests_size--;
338 if (fte->dests_size) {
339 err = mlx5_cmd_update_fte(dev, ft,
340 fg->id, fte);
341 if (err)
342 pr_warn("%s can't del rule fg id=%d fte_index=%d\n",
343 __func__, fg->id, fte->index);
344 }
345 kvfree(match_value);
346}
347
348static void del_fte(struct fs_node *node)
349{
350 struct mlx5_flow_table *ft;
351 struct mlx5_flow_group *fg;
352 struct mlx5_core_dev *dev;
353 struct fs_fte *fte;
354 int err;
355
356 fs_get_obj(fte, node);
357 fs_get_obj(fg, fte->node.parent);
358 fs_get_obj(ft, fg->node.parent);
359
360 dev = get_dev(&ft->node);
361 err = mlx5_cmd_delete_fte(dev, ft,
362 fte->index);
363 if (err)
364 pr_warn("flow steering can't delete fte in index %d of flow group id %d\n",
365 fte->index, fg->id);
366
367 fte->status = 0;
368 fg->num_ftes--;
369}
370
371static void del_flow_group(struct fs_node *node)
372{
373 struct mlx5_flow_group *fg;
374 struct mlx5_flow_table *ft;
375 struct mlx5_core_dev *dev;
376
377 fs_get_obj(fg, node);
378 fs_get_obj(ft, fg->node.parent);
379 dev = get_dev(&ft->node);
380
381 if (mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
382 pr_warn("flow steering can't destroy fg %d of ft %d\n",
383 fg->id, ft->id);
384}
385
386static struct fs_fte *alloc_fte(u8 action,
387 u32 flow_tag,
388 u32 *match_value,
389 unsigned int index)
390{
391 struct fs_fte *fte;
392
393 fte = kzalloc(sizeof(*fte), GFP_KERNEL);
394 if (!fte)
395 return ERR_PTR(-ENOMEM);
396
397 memcpy(fte->val, match_value, sizeof(fte->val));
398 fte->node.type = FS_TYPE_FLOW_ENTRY;
399 fte->flow_tag = flow_tag;
400 fte->index = index;
401 fte->action = action;
402
403 return fte;
404}
405
406static struct mlx5_flow_group *alloc_flow_group(u32 *create_fg_in)
407{
408 struct mlx5_flow_group *fg;
409 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
410 create_fg_in, match_criteria);
411 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
412 create_fg_in,
413 match_criteria_enable);
414 fg = kzalloc(sizeof(*fg), GFP_KERNEL);
415 if (!fg)
416 return ERR_PTR(-ENOMEM);
417
418 fg->mask.match_criteria_enable = match_criteria_enable;
419 memcpy(&fg->mask.match_criteria, match_criteria,
420 sizeof(fg->mask.match_criteria));
421 fg->node.type = FS_TYPE_FLOW_GROUP;
422 fg->start_index = MLX5_GET(create_flow_group_in, create_fg_in,
423 start_flow_index);
424 fg->max_ftes = MLX5_GET(create_flow_group_in, create_fg_in,
425 end_flow_index) - fg->start_index + 1;
426 return fg;
427}
428
429static struct mlx5_flow_table *alloc_flow_table(int level, int max_fte,
430 enum fs_flow_table_type table_type)
431{
432 struct mlx5_flow_table *ft;
433
434 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
435 if (!ft)
436 return NULL;
437
438 ft->level = level;
439 ft->node.type = FS_TYPE_FLOW_TABLE;
440 ft->type = table_type;
441 ft->max_fte = max_fte;
442
443 return ft;
444}
445
fdb6896f
MG
446/* If reverse is false, then we search for the first flow table in the
447 * root sub-tree from start(closest from right), else we search for the
448 * last flow table in the root sub-tree till start(closest from left).
449 */
450static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
451 struct list_head *start,
452 bool reverse)
453{
454#define list_advance_entry(pos, reverse) \
455 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
456
457#define list_for_each_advance_continue(pos, head, reverse) \
458 for (pos = list_advance_entry(pos, reverse); \
459 &pos->list != (head); \
460 pos = list_advance_entry(pos, reverse))
461
462 struct fs_node *iter = list_entry(start, struct fs_node, list);
463 struct mlx5_flow_table *ft = NULL;
464
465 if (!root)
466 return NULL;
467
468 list_for_each_advance_continue(iter, &root->children, reverse) {
469 if (iter->type == FS_TYPE_FLOW_TABLE) {
470 fs_get_obj(ft, iter);
471 return ft;
472 }
473 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
474 if (ft)
475 return ft;
476 }
477
478 return ft;
479}
480
481/* If reverse if false then return the first flow table in next priority of
482 * prio in the tree, else return the last flow table in the previous priority
483 * of prio in the tree.
484 */
485static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
486{
487 struct mlx5_flow_table *ft = NULL;
488 struct fs_node *curr_node;
489 struct fs_node *parent;
490
491 parent = prio->node.parent;
492 curr_node = &prio->node;
493 while (!ft && parent) {
494 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
495 curr_node = parent;
496 parent = curr_node->parent;
497 }
498 return ft;
499}
500
501/* Assuming all the tree is locked by mutex chain lock */
502static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
503{
504 return find_closest_ft(prio, false);
505}
506
507/* Assuming all the tree is locked by mutex chain lock */
508static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
509{
510 return find_closest_ft(prio, true);
511}
512
86d722ad
MG
513struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
514 int prio,
515 int max_fte)
0c56b975
MG
516{
517 struct mlx5_flow_table *ft;
518 int err;
519 int log_table_sz;
520 struct mlx5_flow_root_namespace *root =
521 find_root(&ns->node);
522 struct fs_prio *fs_prio = NULL;
523
524 if (!root) {
525 pr_err("mlx5: flow steering failed to find root of namespace\n");
526 return ERR_PTR(-ENODEV);
527 }
528
529 fs_prio = find_prio(ns, prio);
530 if (!fs_prio)
531 return ERR_PTR(-EINVAL);
532
533 lock_ref_node(&fs_prio->node);
534 if (fs_prio->num_ft == fs_prio->max_ft) {
535 err = -ENOSPC;
536 goto unlock_prio;
537 }
538
539 ft = alloc_flow_table(find_next_free_level(fs_prio),
540 roundup_pow_of_two(max_fte),
541 root->table_type);
542 if (!ft) {
543 err = -ENOMEM;
544 goto unlock_prio;
545 }
546
547 tree_init_node(&ft->node, 1, del_flow_table);
548 log_table_sz = ilog2(ft->max_fte);
549 err = mlx5_cmd_create_flow_table(root->dev, ft->type, ft->level,
550 log_table_sz, &ft->id);
551 if (err)
552 goto free_ft;
553
554 tree_add_node(&ft->node, &fs_prio->node);
555 list_add_tail(&ft->node.list, &fs_prio->node.children);
556 fs_prio->num_ft++;
557 unlock_ref_node(&fs_prio->node);
0c56b975 558 return ft;
0c56b975
MG
559free_ft:
560 kfree(ft);
561unlock_prio:
562 unlock_ref_node(&fs_prio->node);
563 return ERR_PTR(err);
564}
565
f0d22d18
MG
566struct mlx5_flow_table *mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
567 int prio,
568 int num_flow_table_entries,
569 int max_num_groups)
570{
571 struct mlx5_flow_table *ft;
572
573 if (max_num_groups > num_flow_table_entries)
574 return ERR_PTR(-EINVAL);
575
576 ft = mlx5_create_flow_table(ns, prio, num_flow_table_entries);
577 if (IS_ERR(ft))
578 return ft;
579
580 ft->autogroup.active = true;
581 ft->autogroup.required_groups = max_num_groups;
582
583 return ft;
584}
585
586/* Flow table should be locked */
587static struct mlx5_flow_group *create_flow_group_common(struct mlx5_flow_table *ft,
588 u32 *fg_in,
589 struct list_head
590 *prev_fg,
591 bool is_auto_fg)
0c56b975
MG
592{
593 struct mlx5_flow_group *fg;
594 struct mlx5_core_dev *dev = get_dev(&ft->node);
595 int err;
596
597 if (!dev)
598 return ERR_PTR(-ENODEV);
599
600 fg = alloc_flow_group(fg_in);
601 if (IS_ERR(fg))
602 return fg;
603
0c56b975
MG
604 err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
605 if (err) {
606 kfree(fg);
0c56b975
MG
607 return ERR_PTR(err);
608 }
f0d22d18
MG
609
610 if (ft->autogroup.active)
611 ft->autogroup.num_groups++;
0c56b975 612 /* Add node to tree */
f0d22d18 613 tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
0c56b975
MG
614 tree_add_node(&fg->node, &ft->node);
615 /* Add node to group list */
616 list_add(&fg->node.list, ft->node.children.prev);
f0d22d18
MG
617
618 return fg;
619}
620
621struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
622 u32 *fg_in)
623{
624 struct mlx5_flow_group *fg;
625
626 if (ft->autogroup.active)
627 return ERR_PTR(-EPERM);
628
629 lock_ref_node(&ft->node);
630 fg = create_flow_group_common(ft, fg_in, &ft->node.children, false);
0c56b975
MG
631 unlock_ref_node(&ft->node);
632
633 return fg;
634}
635
636static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
637{
638 struct mlx5_flow_rule *rule;
639
640 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
641 if (!rule)
642 return NULL;
643
644 rule->node.type = FS_TYPE_FLOW_DEST;
645 memcpy(&rule->dest_attr, dest, sizeof(*dest));
646
647 return rule;
648}
649
650/* fte should not be deleted while calling this function */
651static struct mlx5_flow_rule *add_rule_fte(struct fs_fte *fte,
652 struct mlx5_flow_group *fg,
653 struct mlx5_flow_destination *dest)
654{
655 struct mlx5_flow_table *ft;
656 struct mlx5_flow_rule *rule;
657 int err;
658
659 rule = alloc_rule(dest);
660 if (!rule)
661 return ERR_PTR(-ENOMEM);
662
663 fs_get_obj(ft, fg->node.parent);
664 /* Add dest to dests list- added as first element after the head */
665 tree_init_node(&rule->node, 1, del_rule);
666 list_add_tail(&rule->node.list, &fte->node.children);
667 fte->dests_size++;
668 if (fte->dests_size == 1)
669 err = mlx5_cmd_create_fte(get_dev(&ft->node),
670 ft, fg->id, fte);
671 else
672 err = mlx5_cmd_update_fte(get_dev(&ft->node),
673 ft, fg->id, fte);
674 if (err)
675 goto free_rule;
676
677 fte->status |= FS_FTE_STATUS_EXISTING;
678
679 return rule;
680
681free_rule:
682 list_del(&rule->node.list);
683 kfree(rule);
684 fte->dests_size--;
685 return ERR_PTR(err);
686}
687
688/* Assumed fg is locked */
689static unsigned int get_free_fte_index(struct mlx5_flow_group *fg,
690 struct list_head **prev)
691{
692 struct fs_fte *fte;
693 unsigned int start = fg->start_index;
694
695 if (prev)
696 *prev = &fg->node.children;
697
698 /* assumed list is sorted by index */
699 fs_for_each_fte(fte, fg) {
700 if (fte->index != start)
701 return start;
702 start++;
703 if (prev)
704 *prev = &fte->node.list;
705 }
706
707 return start;
708}
709
710/* prev is output, prev->next = new_fte */
711static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
712 u32 *match_value,
713 u8 action,
714 u32 flow_tag,
715 struct list_head **prev)
716{
717 struct fs_fte *fte;
718 int index;
719
720 index = get_free_fte_index(fg, prev);
721 fte = alloc_fte(action, flow_tag, match_value, index);
722 if (IS_ERR(fte))
723 return fte;
724
725 return fte;
726}
727
f0d22d18
MG
728static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
729 u8 match_criteria_enable,
730 u32 *match_criteria)
731{
732 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
733 struct list_head *prev = &ft->node.children;
734 unsigned int candidate_index = 0;
735 struct mlx5_flow_group *fg;
736 void *match_criteria_addr;
737 unsigned int group_size = 0;
738 u32 *in;
739
740 if (!ft->autogroup.active)
741 return ERR_PTR(-ENOENT);
742
743 in = mlx5_vzalloc(inlen);
744 if (!in)
745 return ERR_PTR(-ENOMEM);
746
747 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
748 /* We save place for flow groups in addition to max types */
749 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
750
751 /* ft->max_fte == ft->autogroup.max_types */
752 if (group_size == 0)
753 group_size = 1;
754
755 /* sorted by start_index */
756 fs_for_each_fg(fg, ft) {
757 if (candidate_index + group_size > fg->start_index)
758 candidate_index = fg->start_index + fg->max_ftes;
759 else
760 break;
761 prev = &fg->node.list;
762 }
763
764 if (candidate_index + group_size > ft->max_fte) {
765 fg = ERR_PTR(-ENOSPC);
766 goto out;
767 }
768
769 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
770 match_criteria_enable);
771 MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
772 MLX5_SET(create_flow_group_in, in, end_flow_index, candidate_index +
773 group_size - 1);
774 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
775 in, match_criteria);
776 memcpy(match_criteria_addr, match_criteria,
777 MLX5_ST_SZ_BYTES(fte_match_param));
778
779 fg = create_flow_group_common(ft, in, prev, true);
780out:
781 kvfree(in);
782 return fg;
783}
784
0c56b975
MG
785static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
786 u32 *match_value,
787 u8 action,
788 u32 flow_tag,
789 struct mlx5_flow_destination *dest)
790{
791 struct fs_fte *fte;
792 struct mlx5_flow_rule *rule;
793 struct mlx5_flow_table *ft;
794 struct list_head *prev;
795
f0d22d18 796 nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
0c56b975 797 fs_for_each_fte(fte, fg) {
f0d22d18 798 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
0c56b975
MG
799 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
800 action == fte->action && flow_tag == fte->flow_tag) {
801 rule = add_rule_fte(fte, fg, dest);
802 unlock_ref_node(&fte->node);
803 if (IS_ERR(rule))
804 goto unlock_fg;
805 else
806 goto add_rule;
807 }
808 unlock_ref_node(&fte->node);
809 }
810 fs_get_obj(ft, fg->node.parent);
811 if (fg->num_ftes >= fg->max_ftes) {
812 rule = ERR_PTR(-ENOSPC);
813 goto unlock_fg;
814 }
815
816 fte = create_fte(fg, match_value, action, flow_tag, &prev);
817 if (IS_ERR(fte)) {
818 rule = (void *)fte;
819 goto unlock_fg;
820 }
821 tree_init_node(&fte->node, 0, del_fte);
822 rule = add_rule_fte(fte, fg, dest);
823 if (IS_ERR(rule)) {
824 kfree(fte);
825 goto unlock_fg;
826 }
827
828 fg->num_ftes++;
829
830 tree_add_node(&fte->node, &fg->node);
831 list_add(&fte->node.list, prev);
832add_rule:
833 tree_add_node(&rule->node, &fte->node);
834unlock_fg:
835 unlock_ref_node(&fg->node);
836 return rule;
837}
838
f0d22d18
MG
839static struct mlx5_flow_rule *add_rule_to_auto_fg(struct mlx5_flow_table *ft,
840 u8 match_criteria_enable,
841 u32 *match_criteria,
842 u32 *match_value,
843 u8 action,
844 u32 flow_tag,
845 struct mlx5_flow_destination *dest)
846{
847 struct mlx5_flow_rule *rule;
848 struct mlx5_flow_group *g;
849
850 g = create_autogroup(ft, match_criteria_enable, match_criteria);
851 if (IS_ERR(g))
852 return (void *)g;
853
854 rule = add_rule_fg(g, match_value,
855 action, flow_tag, dest);
856 if (IS_ERR(rule)) {
857 /* Remove assumes refcount > 0 and autogroup creates a group
858 * with a refcount = 0.
859 */
860 tree_get_node(&g->node);
861 tree_remove_node(&g->node);
862 }
863 return rule;
864}
865
86d722ad 866struct mlx5_flow_rule *
0c56b975
MG
867mlx5_add_flow_rule(struct mlx5_flow_table *ft,
868 u8 match_criteria_enable,
869 u32 *match_criteria,
870 u32 *match_value,
871 u32 action,
872 u32 flow_tag,
873 struct mlx5_flow_destination *dest)
874{
875 struct mlx5_flow_group *g;
f0d22d18 876 struct mlx5_flow_rule *rule;
0c56b975 877
f0d22d18 878 nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
0c56b975
MG
879 fs_for_each_fg(g, ft)
880 if (compare_match_criteria(g->mask.match_criteria_enable,
881 match_criteria_enable,
882 g->mask.match_criteria,
883 match_criteria)) {
0c56b975
MG
884 rule = add_rule_fg(g, match_value,
885 action, flow_tag, dest);
f0d22d18
MG
886 if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
887 goto unlock;
0c56b975 888 }
f0d22d18
MG
889
890 rule = add_rule_to_auto_fg(ft, match_criteria_enable, match_criteria,
891 match_value, action, flow_tag, dest);
892unlock:
0c56b975 893 unlock_ref_node(&ft->node);
0c56b975
MG
894 return rule;
895}
896
86d722ad 897void mlx5_del_flow_rule(struct mlx5_flow_rule *rule)
0c56b975
MG
898{
899 tree_remove_node(&rule->node);
900}
901
86d722ad 902int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
0c56b975
MG
903{
904 if (tree_remove_node(&ft->node))
905 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
906 ft->id);
907
908 return 0;
909}
910
86d722ad 911void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
0c56b975
MG
912{
913 if (tree_remove_node(&fg->node))
914 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
915 fg->id);
916}
25302363 917
86d722ad
MG
918struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
919 enum mlx5_flow_namespace_type type)
25302363
MG
920{
921 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
922 int prio;
923 static struct fs_prio *fs_prio;
924 struct mlx5_flow_namespace *ns;
925
926 if (!root_ns)
927 return NULL;
928
929 switch (type) {
930 case MLX5_FLOW_NAMESPACE_KERNEL:
931 prio = 0;
932 break;
933 case MLX5_FLOW_NAMESPACE_FDB:
934 if (dev->priv.fdb_root_ns)
935 return &dev->priv.fdb_root_ns->ns;
936 else
937 return NULL;
938 default:
939 return NULL;
940 }
941
942 fs_prio = find_prio(&root_ns->ns, prio);
943 if (!fs_prio)
944 return NULL;
945
946 ns = list_first_entry(&fs_prio->node.children,
947 typeof(*ns),
948 node.list);
949
950 return ns;
951}
952
953static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
954 unsigned prio, int max_ft,
955 int start_level)
956{
957 struct fs_prio *fs_prio;
958
959 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
960 if (!fs_prio)
961 return ERR_PTR(-ENOMEM);
962
963 fs_prio->node.type = FS_TYPE_PRIO;
964 tree_init_node(&fs_prio->node, 1, NULL);
965 tree_add_node(&fs_prio->node, &ns->node);
966 fs_prio->max_ft = max_ft;
967 fs_prio->prio = prio;
968 fs_prio->start_level = start_level;
969 list_add_tail(&fs_prio->node.list, &ns->node.children);
970
971 return fs_prio;
972}
973
974static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
975 *ns)
976{
977 ns->node.type = FS_TYPE_NAMESPACE;
978
979 return ns;
980}
981
982static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
983{
984 struct mlx5_flow_namespace *ns;
985
986 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
987 if (!ns)
988 return ERR_PTR(-ENOMEM);
989
990 fs_init_namespace(ns);
991 tree_init_node(&ns->node, 1, NULL);
992 tree_add_node(&ns->node, &prio->node);
993 list_add_tail(&ns->node.list, &prio->node.children);
994
995 return ns;
996}
997
998static int init_root_tree_recursive(int max_ft_level, struct init_tree_node *init_node,
999 struct fs_node *fs_parent_node,
1000 struct init_tree_node *init_parent_node,
1001 int index)
1002{
1003 struct mlx5_flow_namespace *fs_ns;
1004 struct fs_prio *fs_prio;
1005 struct fs_node *base;
1006 int i;
1007 int err;
1008
1009 if (init_node->type == FS_TYPE_PRIO) {
1010 if (init_node->min_ft_level > max_ft_level)
1011 return -ENOTSUPP;
1012
1013 fs_get_obj(fs_ns, fs_parent_node);
1014 fs_prio = fs_create_prio(fs_ns, index, init_node->max_ft,
1015 init_node->start_level);
1016 if (IS_ERR(fs_prio))
1017 return PTR_ERR(fs_prio);
1018 base = &fs_prio->node;
1019 } else if (init_node->type == FS_TYPE_NAMESPACE) {
1020 fs_get_obj(fs_prio, fs_parent_node);
1021 fs_ns = fs_create_namespace(fs_prio);
1022 if (IS_ERR(fs_ns))
1023 return PTR_ERR(fs_ns);
1024 base = &fs_ns->node;
1025 } else {
1026 return -EINVAL;
1027 }
1028 for (i = 0; i < init_node->ar_size; i++) {
1029 err = init_root_tree_recursive(max_ft_level,
1030 &init_node->children[i], base,
1031 init_node, i);
1032 if (err)
1033 return err;
1034 }
1035
1036 return 0;
1037}
1038
1039static int init_root_tree(int max_ft_level, struct init_tree_node *init_node,
1040 struct fs_node *fs_parent_node)
1041{
1042 int i;
1043 struct mlx5_flow_namespace *fs_ns;
1044 int err;
1045
1046 fs_get_obj(fs_ns, fs_parent_node);
1047 for (i = 0; i < init_node->ar_size; i++) {
1048 err = init_root_tree_recursive(max_ft_level,
1049 &init_node->children[i],
1050 &fs_ns->node,
1051 init_node, i);
1052 if (err)
1053 return err;
1054 }
1055 return 0;
1056}
1057
1058static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_core_dev *dev,
1059 enum fs_flow_table_type
1060 table_type)
1061{
1062 struct mlx5_flow_root_namespace *root_ns;
1063 struct mlx5_flow_namespace *ns;
1064
86d722ad 1065 /* Create the root namespace */
25302363
MG
1066 root_ns = mlx5_vzalloc(sizeof(*root_ns));
1067 if (!root_ns)
1068 return NULL;
1069
1070 root_ns->dev = dev;
1071 root_ns->table_type = table_type;
1072
1073 ns = &root_ns->ns;
1074 fs_init_namespace(ns);
1075 tree_init_node(&ns->node, 1, NULL);
1076 tree_add_node(&ns->node, NULL);
1077
1078 return root_ns;
1079}
1080
1081static int init_root_ns(struct mlx5_core_dev *dev)
1082{
1083 int max_ft_level = MLX5_CAP_FLOWTABLE(dev,
1084 flow_table_properties_nic_receive.
1085 max_ft_level);
1086
1087 dev->priv.root_ns = create_root_ns(dev, FS_FT_NIC_RX);
1088 if (IS_ERR_OR_NULL(dev->priv.root_ns))
1089 goto cleanup;
1090
1091 if (init_root_tree(max_ft_level, &root_fs, &dev->priv.root_ns->ns.node))
1092 goto cleanup;
1093
1094 return 0;
1095
1096cleanup:
1097 mlx5_cleanup_fs(dev);
1098 return -ENOMEM;
1099}
1100
1101static void cleanup_single_prio_root_ns(struct mlx5_core_dev *dev,
1102 struct mlx5_flow_root_namespace *root_ns)
1103{
1104 struct fs_node *prio;
1105
1106 if (!root_ns)
1107 return;
1108
1109 if (!list_empty(&root_ns->ns.node.children)) {
1110 prio = list_first_entry(&root_ns->ns.node.children,
1111 struct fs_node,
1112 list);
1113 if (tree_remove_node(prio))
1114 mlx5_core_warn(dev,
1115 "Flow steering priority wasn't destroyed, refcount > 1\n");
1116 }
1117 if (tree_remove_node(&root_ns->ns.node))
1118 mlx5_core_warn(dev,
1119 "Flow steering namespace wasn't destroyed, refcount > 1\n");
1120 root_ns = NULL;
1121}
1122
1123static void cleanup_root_ns(struct mlx5_core_dev *dev)
1124{
1125 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1126 struct fs_prio *iter_prio;
1127
1128 if (!MLX5_CAP_GEN(dev, nic_flow_table))
1129 return;
1130
1131 if (!root_ns)
1132 return;
1133
1134 /* stage 1 */
1135 fs_for_each_prio(iter_prio, &root_ns->ns) {
1136 struct fs_node *node;
1137 struct mlx5_flow_namespace *iter_ns;
1138
1139 fs_for_each_ns_or_ft(node, iter_prio) {
1140 if (node->type == FS_TYPE_FLOW_TABLE)
1141 continue;
1142 fs_get_obj(iter_ns, node);
1143 while (!list_empty(&iter_ns->node.children)) {
1144 struct fs_prio *obj_iter_prio2;
1145 struct fs_node *iter_prio2 =
1146 list_first_entry(&iter_ns->node.children,
1147 struct fs_node,
1148 list);
1149
1150 fs_get_obj(obj_iter_prio2, iter_prio2);
1151 if (tree_remove_node(iter_prio2)) {
1152 mlx5_core_warn(dev,
1153 "Priority %d wasn't destroyed, refcount > 1\n",
1154 obj_iter_prio2->prio);
1155 return;
1156 }
1157 }
1158 }
1159 }
1160
1161 /* stage 2 */
1162 fs_for_each_prio(iter_prio, &root_ns->ns) {
1163 while (!list_empty(&iter_prio->node.children)) {
1164 struct fs_node *iter_ns =
1165 list_first_entry(&iter_prio->node.children,
1166 struct fs_node,
1167 list);
1168 if (tree_remove_node(iter_ns)) {
1169 mlx5_core_warn(dev,
1170 "Namespace wasn't destroyed, refcount > 1\n");
1171 return;
1172 }
1173 }
1174 }
1175
1176 /* stage 3 */
1177 while (!list_empty(&root_ns->ns.node.children)) {
1178 struct fs_prio *obj_prio_node;
1179 struct fs_node *prio_node =
1180 list_first_entry(&root_ns->ns.node.children,
1181 struct fs_node,
1182 list);
1183
1184 fs_get_obj(obj_prio_node, prio_node);
1185 if (tree_remove_node(prio_node)) {
1186 mlx5_core_warn(dev,
1187 "Priority %d wasn't destroyed, refcount > 1\n",
1188 obj_prio_node->prio);
1189 return;
1190 }
1191 }
1192
1193 if (tree_remove_node(&root_ns->ns.node)) {
1194 mlx5_core_warn(dev,
1195 "root namespace wasn't destroyed, refcount > 1\n");
1196 return;
1197 }
1198
1199 dev->priv.root_ns = NULL;
1200}
1201
1202void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1203{
1204 cleanup_root_ns(dev);
1205 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1206}
1207
1208static int init_fdb_root_ns(struct mlx5_core_dev *dev)
1209{
1210 struct fs_prio *prio;
1211
1212 dev->priv.fdb_root_ns = create_root_ns(dev, FS_FT_FDB);
1213 if (!dev->priv.fdb_root_ns)
1214 return -ENOMEM;
1215
86d722ad 1216 /* Create single prio */
25302363
MG
1217 prio = fs_create_prio(&dev->priv.fdb_root_ns->ns, 0, 1, 0);
1218 if (IS_ERR(prio)) {
1219 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1220 return PTR_ERR(prio);
1221 } else {
1222 return 0;
1223 }
1224}
1225
1226int mlx5_init_fs(struct mlx5_core_dev *dev)
1227{
1228 int err = 0;
1229
1230 if (MLX5_CAP_GEN(dev, nic_flow_table)) {
1231 err = init_root_ns(dev);
1232 if (err)
1233 return err;
1234 }
1235 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
1236 err = init_fdb_root_ns(dev);
1237 if (err)
1238 cleanup_root_ns(dev);
1239 }
1240
1241 return err;
1242}