Merge branches 'pm-core' and 'pm-domains'
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_core.c
CommitLineData
de8575e0
MG
1/*
2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/mutex.h>
34#include <linux/mlx5/driver.h>
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
4cbdd30e 43#define ADD_PRIO(num_prios_val, min_level_val, max_ft_val, caps_val,\
8d40d162 44 ...) {.type = FS_TYPE_PRIO,\
25302363 45 .min_ft_level = min_level_val,\
25302363 46 .max_ft = max_ft_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
4cbdd30e
MG
53#define ADD_MULTIPLE_PRIO(num_prios_val, max_ft_val, ...)\
54 ADD_PRIO(num_prios_val, 0, max_ft_val, {},\
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
4cbdd30e
MG
70#define LEFTOVERS_MAX_FT 1
71#define LEFTOVERS_NUM_PRIOS 1
72#define BY_PASS_PRIO_MAX_FT 1
73#define BY_PASS_MIN_LEVEL (KENREL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
74 LEFTOVERS_MAX_FT)
75
b6172aac
AV
76#define KERNEL_MAX_FT 3
77#define KERNEL_NUM_PRIOS 2
25302363 78#define KENREL_MIN_LEVEL 2
8d40d162 79
153fefbf
MG
80#define ANCHOR_MAX_FT 1
81#define ANCHOR_NUM_PRIOS 1
82#define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
8d40d162
MG
83struct node_caps {
84 size_t arr_sz;
85 long *caps;
86};
25302363
MG
87static struct init_tree_node {
88 enum fs_node_type type;
89 struct init_tree_node *children;
90 int ar_size;
8d40d162 91 struct node_caps caps;
25302363 92 int min_ft_level;
4cbdd30e 93 int num_leaf_prios;
25302363
MG
94 int prio;
95 int max_ft;
25302363
MG
96} root_fs = {
97 .type = FS_TYPE_NAMESPACE,
153fefbf 98 .ar_size = 4,
25302363 99 .children = (struct init_tree_node[]) {
4cbdd30e
MG
100 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
101 FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en),
102 FS_CAP(flow_table_properties_nic_receive.modify_root),
103 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode),
104 FS_CAP(flow_table_properties_nic_receive.flow_table_modify)),
105 ADD_NS(ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS, BY_PASS_PRIO_MAX_FT))),
106 ADD_PRIO(0, KENREL_MIN_LEVEL, 0, {},
107 ADD_NS(ADD_MULTIPLE_PRIO(KERNEL_NUM_PRIOS, KERNEL_MAX_FT))),
108 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0,
109 FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en),
110 FS_CAP(flow_table_properties_nic_receive.modify_root),
111 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode),
112 FS_CAP(flow_table_properties_nic_receive.flow_table_modify)),
113 ADD_NS(ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS, LEFTOVERS_MAX_FT))),
153fefbf
MG
114 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
115 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_MAX_FT))),
25302363
MG
116 }
117};
118
f0d22d18
MG
119enum fs_i_mutex_lock_class {
120 FS_MUTEX_GRANDPARENT,
121 FS_MUTEX_PARENT,
122 FS_MUTEX_CHILD
123};
124
0c56b975
MG
125static void del_rule(struct fs_node *node);
126static void del_flow_table(struct fs_node *node);
127static void del_flow_group(struct fs_node *node);
128static void del_fte(struct fs_node *node);
de8575e0
MG
129
130static void tree_init_node(struct fs_node *node,
131 unsigned int refcount,
132 void (*remove_func)(struct fs_node *))
133{
134 atomic_set(&node->refcount, refcount);
135 INIT_LIST_HEAD(&node->list);
136 INIT_LIST_HEAD(&node->children);
137 mutex_init(&node->lock);
138 node->remove_func = remove_func;
139}
140
141static void tree_add_node(struct fs_node *node, struct fs_node *parent)
142{
143 if (parent)
144 atomic_inc(&parent->refcount);
145 node->parent = parent;
146
147 /* Parent is the root */
148 if (!parent)
149 node->root = node;
150 else
151 node->root = parent->root;
152}
153
154static void tree_get_node(struct fs_node *node)
155{
156 atomic_inc(&node->refcount);
157}
158
f0d22d18
MG
159static void nested_lock_ref_node(struct fs_node *node,
160 enum fs_i_mutex_lock_class class)
de8575e0
MG
161{
162 if (node) {
f0d22d18 163 mutex_lock_nested(&node->lock, class);
de8575e0
MG
164 atomic_inc(&node->refcount);
165 }
166}
167
168static void lock_ref_node(struct fs_node *node)
169{
170 if (node) {
171 mutex_lock(&node->lock);
172 atomic_inc(&node->refcount);
173 }
174}
175
176static void unlock_ref_node(struct fs_node *node)
177{
178 if (node) {
179 atomic_dec(&node->refcount);
180 mutex_unlock(&node->lock);
181 }
182}
183
184static void tree_put_node(struct fs_node *node)
185{
186 struct fs_node *parent_node = node->parent;
187
188 lock_ref_node(parent_node);
189 if (atomic_dec_and_test(&node->refcount)) {
190 if (parent_node)
191 list_del_init(&node->list);
192 if (node->remove_func)
193 node->remove_func(node);
194 kfree(node);
195 node = NULL;
196 }
197 unlock_ref_node(parent_node);
198 if (!node && parent_node)
199 tree_put_node(parent_node);
200}
201
202static int tree_remove_node(struct fs_node *node)
203{
b3638e1a
MG
204 if (atomic_read(&node->refcount) > 1) {
205 atomic_dec(&node->refcount);
206 return -EEXIST;
207 }
de8575e0
MG
208 tree_put_node(node);
209 return 0;
210}
5e1626c0
MG
211
212static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
213 unsigned int prio)
214{
215 struct fs_prio *iter_prio;
216
217 fs_for_each_prio(iter_prio, ns) {
218 if (iter_prio->prio == prio)
219 return iter_prio;
220 }
221
222 return NULL;
223}
224
225static unsigned int find_next_free_level(struct fs_prio *prio)
226{
227 if (!list_empty(&prio->node.children)) {
228 struct mlx5_flow_table *ft;
229
230 ft = list_last_entry(&prio->node.children,
231 struct mlx5_flow_table,
232 node.list);
233 return ft->level + 1;
234 }
235 return prio->start_level;
236}
237
238static bool masked_memcmp(void *mask, void *val1, void *val2, size_t size)
239{
240 unsigned int i;
241
242 for (i = 0; i < size; i++, mask++, val1++, val2++)
243 if ((*((u8 *)val1) & (*(u8 *)mask)) !=
244 ((*(u8 *)val2) & (*(u8 *)mask)))
245 return false;
246
247 return true;
248}
249
250static bool compare_match_value(struct mlx5_flow_group_mask *mask,
251 void *fte_param1, void *fte_param2)
252{
253 if (mask->match_criteria_enable &
254 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_OUTER_HEADERS) {
255 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
256 fte_param1, outer_headers);
257 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
258 fte_param2, outer_headers);
259 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
260 mask->match_criteria, outer_headers);
261
262 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
263 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
264 return false;
265 }
266
267 if (mask->match_criteria_enable &
268 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_MISC_PARAMETERS) {
269 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
270 fte_param1, misc_parameters);
271 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
272 fte_param2, misc_parameters);
273 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
274 mask->match_criteria, misc_parameters);
275
276 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
277 MLX5_ST_SZ_BYTES(fte_match_set_misc)))
278 return false;
279 }
280
281 if (mask->match_criteria_enable &
282 1 << MLX5_CREATE_FLOW_GROUP_IN_MATCH_CRITERIA_ENABLE_INNER_HEADERS) {
283 void *fte_match1 = MLX5_ADDR_OF(fte_match_param,
284 fte_param1, inner_headers);
285 void *fte_match2 = MLX5_ADDR_OF(fte_match_param,
286 fte_param2, inner_headers);
287 void *fte_mask = MLX5_ADDR_OF(fte_match_param,
288 mask->match_criteria, inner_headers);
289
290 if (!masked_memcmp(fte_mask, fte_match1, fte_match2,
291 MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4)))
292 return false;
293 }
294 return true;
295}
296
297static bool compare_match_criteria(u8 match_criteria_enable1,
298 u8 match_criteria_enable2,
299 void *mask1, void *mask2)
300{
301 return match_criteria_enable1 == match_criteria_enable2 &&
302 !memcmp(mask1, mask2, MLX5_ST_SZ_BYTES(fte_match_param));
303}
0c56b975
MG
304
305static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
306{
307 struct fs_node *root;
308 struct mlx5_flow_namespace *ns;
309
310 root = node->root;
311
312 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
313 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
314 return NULL;
315 }
316
317 ns = container_of(root, struct mlx5_flow_namespace, node);
318 return container_of(ns, struct mlx5_flow_root_namespace, ns);
319}
320
321static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
322{
323 struct mlx5_flow_root_namespace *root = find_root(node);
324
325 if (root)
326 return root->dev;
327 return NULL;
328}
329
330static void del_flow_table(struct fs_node *node)
331{
332 struct mlx5_flow_table *ft;
333 struct mlx5_core_dev *dev;
334 struct fs_prio *prio;
335 int err;
336
337 fs_get_obj(ft, node);
338 dev = get_dev(&ft->node);
339
340 err = mlx5_cmd_destroy_flow_table(dev, ft);
341 if (err)
342 pr_warn("flow steering can't destroy ft\n");
343 fs_get_obj(prio, ft->node.parent);
344 prio->num_ft--;
345}
346
347static void del_rule(struct fs_node *node)
348{
349 struct mlx5_flow_rule *rule;
350 struct mlx5_flow_table *ft;
351 struct mlx5_flow_group *fg;
352 struct fs_fte *fte;
353 u32 *match_value;
354 struct mlx5_core_dev *dev = get_dev(node);
355 int match_len = MLX5_ST_SZ_BYTES(fte_match_param);
356 int err;
357
358 match_value = mlx5_vzalloc(match_len);
359 if (!match_value) {
360 pr_warn("failed to allocate inbox\n");
361 return;
362 }
363
364 fs_get_obj(rule, node);
365 fs_get_obj(fte, rule->node.parent);
366 fs_get_obj(fg, fte->node.parent);
367 memcpy(match_value, fte->val, sizeof(fte->val));
368 fs_get_obj(ft, fg->node.parent);
369 list_del(&rule->node.list);
b3638e1a
MG
370 if (rule->sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
371 mutex_lock(&rule->dest_attr.ft->lock);
372 list_del(&rule->next_ft);
373 mutex_unlock(&rule->dest_attr.ft->lock);
374 }
60ab4584
AV
375 if ((fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
376 --fte->dests_size) {
0c56b975
MG
377 err = mlx5_cmd_update_fte(dev, ft,
378 fg->id, fte);
379 if (err)
380 pr_warn("%s can't del rule fg id=%d fte_index=%d\n",
381 __func__, fg->id, fte->index);
382 }
383 kvfree(match_value);
384}
385
386static void del_fte(struct fs_node *node)
387{
388 struct mlx5_flow_table *ft;
389 struct mlx5_flow_group *fg;
390 struct mlx5_core_dev *dev;
391 struct fs_fte *fte;
392 int err;
393
394 fs_get_obj(fte, node);
395 fs_get_obj(fg, fte->node.parent);
396 fs_get_obj(ft, fg->node.parent);
397
398 dev = get_dev(&ft->node);
399 err = mlx5_cmd_delete_fte(dev, ft,
400 fte->index);
401 if (err)
402 pr_warn("flow steering can't delete fte in index %d of flow group id %d\n",
403 fte->index, fg->id);
404
405 fte->status = 0;
406 fg->num_ftes--;
407}
408
409static void del_flow_group(struct fs_node *node)
410{
411 struct mlx5_flow_group *fg;
412 struct mlx5_flow_table *ft;
413 struct mlx5_core_dev *dev;
414
415 fs_get_obj(fg, node);
416 fs_get_obj(ft, fg->node.parent);
417 dev = get_dev(&ft->node);
418
419 if (mlx5_cmd_destroy_flow_group(dev, ft, fg->id))
420 pr_warn("flow steering can't destroy fg %d of ft %d\n",
421 fg->id, ft->id);
422}
423
424static struct fs_fte *alloc_fte(u8 action,
425 u32 flow_tag,
426 u32 *match_value,
427 unsigned int index)
428{
429 struct fs_fte *fte;
430
431 fte = kzalloc(sizeof(*fte), GFP_KERNEL);
432 if (!fte)
433 return ERR_PTR(-ENOMEM);
434
435 memcpy(fte->val, match_value, sizeof(fte->val));
436 fte->node.type = FS_TYPE_FLOW_ENTRY;
437 fte->flow_tag = flow_tag;
438 fte->index = index;
439 fte->action = action;
440
441 return fte;
442}
443
444static struct mlx5_flow_group *alloc_flow_group(u32 *create_fg_in)
445{
446 struct mlx5_flow_group *fg;
447 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
448 create_fg_in, match_criteria);
449 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
450 create_fg_in,
451 match_criteria_enable);
452 fg = kzalloc(sizeof(*fg), GFP_KERNEL);
453 if (!fg)
454 return ERR_PTR(-ENOMEM);
455
456 fg->mask.match_criteria_enable = match_criteria_enable;
457 memcpy(&fg->mask.match_criteria, match_criteria,
458 sizeof(fg->mask.match_criteria));
459 fg->node.type = FS_TYPE_FLOW_GROUP;
460 fg->start_index = MLX5_GET(create_flow_group_in, create_fg_in,
461 start_flow_index);
462 fg->max_ftes = MLX5_GET(create_flow_group_in, create_fg_in,
463 end_flow_index) - fg->start_index + 1;
464 return fg;
465}
466
467static struct mlx5_flow_table *alloc_flow_table(int level, int max_fte,
468 enum fs_flow_table_type table_type)
469{
470 struct mlx5_flow_table *ft;
471
472 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
473 if (!ft)
474 return NULL;
475
476 ft->level = level;
477 ft->node.type = FS_TYPE_FLOW_TABLE;
478 ft->type = table_type;
479 ft->max_fte = max_fte;
b3638e1a
MG
480 INIT_LIST_HEAD(&ft->fwd_rules);
481 mutex_init(&ft->lock);
0c56b975
MG
482
483 return ft;
484}
485
fdb6896f
MG
486/* If reverse is false, then we search for the first flow table in the
487 * root sub-tree from start(closest from right), else we search for the
488 * last flow table in the root sub-tree till start(closest from left).
489 */
490static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
491 struct list_head *start,
492 bool reverse)
493{
494#define list_advance_entry(pos, reverse) \
495 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
496
497#define list_for_each_advance_continue(pos, head, reverse) \
498 for (pos = list_advance_entry(pos, reverse); \
499 &pos->list != (head); \
500 pos = list_advance_entry(pos, reverse))
501
502 struct fs_node *iter = list_entry(start, struct fs_node, list);
503 struct mlx5_flow_table *ft = NULL;
504
505 if (!root)
506 return NULL;
507
508 list_for_each_advance_continue(iter, &root->children, reverse) {
509 if (iter->type == FS_TYPE_FLOW_TABLE) {
510 fs_get_obj(ft, iter);
511 return ft;
512 }
513 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
514 if (ft)
515 return ft;
516 }
517
518 return ft;
519}
520
521/* If reverse if false then return the first flow table in next priority of
522 * prio in the tree, else return the last flow table in the previous priority
523 * of prio in the tree.
524 */
525static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
526{
527 struct mlx5_flow_table *ft = NULL;
528 struct fs_node *curr_node;
529 struct fs_node *parent;
530
531 parent = prio->node.parent;
532 curr_node = &prio->node;
533 while (!ft && parent) {
534 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
535 curr_node = parent;
536 parent = curr_node->parent;
537 }
538 return ft;
539}
540
541/* Assuming all the tree is locked by mutex chain lock */
542static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
543{
544 return find_closest_ft(prio, false);
545}
546
547/* Assuming all the tree is locked by mutex chain lock */
548static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
549{
550 return find_closest_ft(prio, true);
551}
552
f90edfd2
MG
553static int connect_fts_in_prio(struct mlx5_core_dev *dev,
554 struct fs_prio *prio,
555 struct mlx5_flow_table *ft)
556{
557 struct mlx5_flow_table *iter;
558 int i = 0;
559 int err;
560
561 fs_for_each_ft(iter, prio) {
562 i++;
563 err = mlx5_cmd_modify_flow_table(dev,
564 iter,
565 ft);
566 if (err) {
567 mlx5_core_warn(dev, "Failed to modify flow table %d\n",
568 iter->id);
569 /* The driver is out of sync with the FW */
570 if (i > 1)
571 WARN_ON(true);
572 return err;
573 }
574 }
575 return 0;
576}
577
578/* Connect flow tables from previous priority of prio to ft */
579static int connect_prev_fts(struct mlx5_core_dev *dev,
580 struct mlx5_flow_table *ft,
581 struct fs_prio *prio)
582{
583 struct mlx5_flow_table *prev_ft;
584
585 prev_ft = find_prev_chained_ft(prio);
586 if (prev_ft) {
587 struct fs_prio *prev_prio;
588
589 fs_get_obj(prev_prio, prev_ft->node.parent);
590 return connect_fts_in_prio(dev, prev_prio, ft);
591 }
592 return 0;
593}
594
2cc43b49
MG
595static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
596 *prio)
597{
598 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
599 int min_level = INT_MAX;
600 int err;
601
602 if (root->root_ft)
603 min_level = root->root_ft->level;
604
605 if (ft->level >= min_level)
606 return 0;
607
608 err = mlx5_cmd_update_root_ft(root->dev, ft);
609 if (err)
610 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
611 ft->id);
612 else
613 root->root_ft = ft;
614
615 return err;
616}
617
b3638e1a
MG
618static int mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
619 struct mlx5_flow_destination *dest)
620{
621 struct mlx5_flow_table *ft;
622 struct mlx5_flow_group *fg;
623 struct fs_fte *fte;
624 int err = 0;
625
626 fs_get_obj(fte, rule->node.parent);
627 if (!(fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
628 return -EINVAL;
629 lock_ref_node(&fte->node);
630 fs_get_obj(fg, fte->node.parent);
631 fs_get_obj(ft, fg->node.parent);
632
633 memcpy(&rule->dest_attr, dest, sizeof(*dest));
634 err = mlx5_cmd_update_fte(get_dev(&ft->node),
635 ft, fg->id, fte);
636 unlock_ref_node(&fte->node);
637
638 return err;
639}
640
641/* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
642static int connect_fwd_rules(struct mlx5_core_dev *dev,
643 struct mlx5_flow_table *new_next_ft,
644 struct mlx5_flow_table *old_next_ft)
645{
646 struct mlx5_flow_destination dest;
647 struct mlx5_flow_rule *iter;
648 int err = 0;
649
650 /* new_next_ft and old_next_ft could be NULL only
651 * when we create/destroy the anchor flow table.
652 */
653 if (!new_next_ft || !old_next_ft)
654 return 0;
655
656 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
657 dest.ft = new_next_ft;
658
659 mutex_lock(&old_next_ft->lock);
660 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
661 mutex_unlock(&old_next_ft->lock);
662 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
663 err = mlx5_modify_rule_destination(iter, &dest);
664 if (err)
665 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
666 new_next_ft->id);
667 }
668 return 0;
669}
670
f90edfd2
MG
671static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
672 struct fs_prio *prio)
673{
b3638e1a 674 struct mlx5_flow_table *next_ft;
f90edfd2
MG
675 int err = 0;
676
677 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
678
679 if (list_empty(&prio->node.children)) {
680 err = connect_prev_fts(dev, ft, prio);
681 if (err)
682 return err;
b3638e1a
MG
683
684 next_ft = find_next_chained_ft(prio);
685 err = connect_fwd_rules(dev, ft, next_ft);
686 if (err)
687 return err;
f90edfd2
MG
688 }
689
690 if (MLX5_CAP_FLOWTABLE(dev,
691 flow_table_properties_nic_receive.modify_root))
692 err = update_root_ft_create(ft, prio);
693 return err;
694}
695
86d722ad
MG
696struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
697 int prio,
698 int max_fte)
0c56b975 699{
f90edfd2 700 struct mlx5_flow_table *next_ft = NULL;
0c56b975
MG
701 struct mlx5_flow_table *ft;
702 int err;
703 int log_table_sz;
704 struct mlx5_flow_root_namespace *root =
705 find_root(&ns->node);
706 struct fs_prio *fs_prio = NULL;
707
708 if (!root) {
709 pr_err("mlx5: flow steering failed to find root of namespace\n");
710 return ERR_PTR(-ENODEV);
711 }
712
2cc43b49 713 mutex_lock(&root->chain_lock);
0c56b975 714 fs_prio = find_prio(ns, prio);
2cc43b49
MG
715 if (!fs_prio) {
716 err = -EINVAL;
717 goto unlock_root;
718 }
0c56b975
MG
719 if (fs_prio->num_ft == fs_prio->max_ft) {
720 err = -ENOSPC;
2cc43b49 721 goto unlock_root;
0c56b975
MG
722 }
723
724 ft = alloc_flow_table(find_next_free_level(fs_prio),
725 roundup_pow_of_two(max_fte),
726 root->table_type);
727 if (!ft) {
728 err = -ENOMEM;
2cc43b49 729 goto unlock_root;
0c56b975
MG
730 }
731
732 tree_init_node(&ft->node, 1, del_flow_table);
733 log_table_sz = ilog2(ft->max_fte);
f90edfd2 734 next_ft = find_next_chained_ft(fs_prio);
0c56b975 735 err = mlx5_cmd_create_flow_table(root->dev, ft->type, ft->level,
f90edfd2 736 log_table_sz, next_ft, &ft->id);
0c56b975
MG
737 if (err)
738 goto free_ft;
739
f90edfd2
MG
740 err = connect_flow_table(root->dev, ft, fs_prio);
741 if (err)
742 goto destroy_ft;
2cc43b49 743 lock_ref_node(&fs_prio->node);
0c56b975
MG
744 tree_add_node(&ft->node, &fs_prio->node);
745 list_add_tail(&ft->node.list, &fs_prio->node.children);
746 fs_prio->num_ft++;
747 unlock_ref_node(&fs_prio->node);
2cc43b49 748 mutex_unlock(&root->chain_lock);
0c56b975 749 return ft;
2cc43b49
MG
750destroy_ft:
751 mlx5_cmd_destroy_flow_table(root->dev, ft);
0c56b975
MG
752free_ft:
753 kfree(ft);
2cc43b49
MG
754unlock_root:
755 mutex_unlock(&root->chain_lock);
0c56b975
MG
756 return ERR_PTR(err);
757}
758
f0d22d18
MG
759struct mlx5_flow_table *mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
760 int prio,
761 int num_flow_table_entries,
762 int max_num_groups)
763{
764 struct mlx5_flow_table *ft;
765
766 if (max_num_groups > num_flow_table_entries)
767 return ERR_PTR(-EINVAL);
768
769 ft = mlx5_create_flow_table(ns, prio, num_flow_table_entries);
770 if (IS_ERR(ft))
771 return ft;
772
773 ft->autogroup.active = true;
774 ft->autogroup.required_groups = max_num_groups;
775
776 return ft;
777}
b217ea25 778EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
f0d22d18
MG
779
780/* Flow table should be locked */
781static struct mlx5_flow_group *create_flow_group_common(struct mlx5_flow_table *ft,
782 u32 *fg_in,
783 struct list_head
784 *prev_fg,
785 bool is_auto_fg)
0c56b975
MG
786{
787 struct mlx5_flow_group *fg;
788 struct mlx5_core_dev *dev = get_dev(&ft->node);
789 int err;
790
791 if (!dev)
792 return ERR_PTR(-ENODEV);
793
794 fg = alloc_flow_group(fg_in);
795 if (IS_ERR(fg))
796 return fg;
797
0c56b975
MG
798 err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
799 if (err) {
800 kfree(fg);
0c56b975
MG
801 return ERR_PTR(err);
802 }
f0d22d18
MG
803
804 if (ft->autogroup.active)
805 ft->autogroup.num_groups++;
0c56b975 806 /* Add node to tree */
f0d22d18 807 tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
0c56b975
MG
808 tree_add_node(&fg->node, &ft->node);
809 /* Add node to group list */
810 list_add(&fg->node.list, ft->node.children.prev);
f0d22d18
MG
811
812 return fg;
813}
814
815struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
816 u32 *fg_in)
817{
818 struct mlx5_flow_group *fg;
819
820 if (ft->autogroup.active)
821 return ERR_PTR(-EPERM);
822
823 lock_ref_node(&ft->node);
824 fg = create_flow_group_common(ft, fg_in, &ft->node.children, false);
0c56b975
MG
825 unlock_ref_node(&ft->node);
826
827 return fg;
828}
829
830static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
831{
832 struct mlx5_flow_rule *rule;
833
834 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
835 if (!rule)
836 return NULL;
837
b3638e1a 838 INIT_LIST_HEAD(&rule->next_ft);
0c56b975 839 rule->node.type = FS_TYPE_FLOW_DEST;
60ab4584
AV
840 if (dest)
841 memcpy(&rule->dest_attr, dest, sizeof(*dest));
0c56b975
MG
842
843 return rule;
844}
845
846/* fte should not be deleted while calling this function */
847static struct mlx5_flow_rule *add_rule_fte(struct fs_fte *fte,
848 struct mlx5_flow_group *fg,
849 struct mlx5_flow_destination *dest)
850{
851 struct mlx5_flow_table *ft;
852 struct mlx5_flow_rule *rule;
853 int err;
854
855 rule = alloc_rule(dest);
856 if (!rule)
857 return ERR_PTR(-ENOMEM);
858
859 fs_get_obj(ft, fg->node.parent);
b3638e1a
MG
860 /* Add dest to dests list- we need flow tables to be in the
861 * end of the list for forward to next prio rules.
862 */
0c56b975 863 tree_init_node(&rule->node, 1, del_rule);
b3638e1a
MG
864 if (dest && dest->type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
865 list_add(&rule->node.list, &fte->node.children);
866 else
867 list_add_tail(&rule->node.list, &fte->node.children);
60ab4584
AV
868 if (dest)
869 fte->dests_size++;
870 if (fte->dests_size == 1 || !dest)
0c56b975
MG
871 err = mlx5_cmd_create_fte(get_dev(&ft->node),
872 ft, fg->id, fte);
873 else
874 err = mlx5_cmd_update_fte(get_dev(&ft->node),
875 ft, fg->id, fte);
876 if (err)
877 goto free_rule;
878
879 fte->status |= FS_FTE_STATUS_EXISTING;
880
881 return rule;
882
883free_rule:
884 list_del(&rule->node.list);
885 kfree(rule);
60ab4584
AV
886 if (dest)
887 fte->dests_size--;
0c56b975
MG
888 return ERR_PTR(err);
889}
890
891/* Assumed fg is locked */
892static unsigned int get_free_fte_index(struct mlx5_flow_group *fg,
893 struct list_head **prev)
894{
895 struct fs_fte *fte;
896 unsigned int start = fg->start_index;
897
898 if (prev)
899 *prev = &fg->node.children;
900
901 /* assumed list is sorted by index */
902 fs_for_each_fte(fte, fg) {
903 if (fte->index != start)
904 return start;
905 start++;
906 if (prev)
907 *prev = &fte->node.list;
908 }
909
910 return start;
911}
912
913/* prev is output, prev->next = new_fte */
914static struct fs_fte *create_fte(struct mlx5_flow_group *fg,
915 u32 *match_value,
916 u8 action,
917 u32 flow_tag,
918 struct list_head **prev)
919{
920 struct fs_fte *fte;
921 int index;
922
923 index = get_free_fte_index(fg, prev);
924 fte = alloc_fte(action, flow_tag, match_value, index);
925 if (IS_ERR(fte))
926 return fte;
927
928 return fte;
929}
930
f0d22d18
MG
931static struct mlx5_flow_group *create_autogroup(struct mlx5_flow_table *ft,
932 u8 match_criteria_enable,
933 u32 *match_criteria)
934{
935 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
936 struct list_head *prev = &ft->node.children;
937 unsigned int candidate_index = 0;
938 struct mlx5_flow_group *fg;
939 void *match_criteria_addr;
940 unsigned int group_size = 0;
941 u32 *in;
942
943 if (!ft->autogroup.active)
944 return ERR_PTR(-ENOENT);
945
946 in = mlx5_vzalloc(inlen);
947 if (!in)
948 return ERR_PTR(-ENOMEM);
949
950 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
951 /* We save place for flow groups in addition to max types */
952 group_size = ft->max_fte / (ft->autogroup.required_groups + 1);
953
954 /* ft->max_fte == ft->autogroup.max_types */
955 if (group_size == 0)
956 group_size = 1;
957
958 /* sorted by start_index */
959 fs_for_each_fg(fg, ft) {
960 if (candidate_index + group_size > fg->start_index)
961 candidate_index = fg->start_index + fg->max_ftes;
962 else
963 break;
964 prev = &fg->node.list;
965 }
966
967 if (candidate_index + group_size > ft->max_fte) {
968 fg = ERR_PTR(-ENOSPC);
969 goto out;
970 }
971
972 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
973 match_criteria_enable);
974 MLX5_SET(create_flow_group_in, in, start_flow_index, candidate_index);
975 MLX5_SET(create_flow_group_in, in, end_flow_index, candidate_index +
976 group_size - 1);
977 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
978 in, match_criteria);
979 memcpy(match_criteria_addr, match_criteria,
980 MLX5_ST_SZ_BYTES(fte_match_param));
981
982 fg = create_flow_group_common(ft, in, prev, true);
983out:
984 kvfree(in);
985 return fg;
986}
987
b3638e1a
MG
988static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
989 struct mlx5_flow_destination *dest)
990{
991 struct mlx5_flow_rule *rule;
992
993 list_for_each_entry(rule, &fte->node.children, node.list) {
994 if (rule->dest_attr.type == dest->type) {
995 if ((dest->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
996 dest->vport_num == rule->dest_attr.vport_num) ||
997 (dest->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
998 dest->ft == rule->dest_attr.ft) ||
999 (dest->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1000 dest->tir_num == rule->dest_attr.tir_num))
1001 return rule;
1002 }
1003 }
1004 return NULL;
1005}
1006
0c56b975
MG
1007static struct mlx5_flow_rule *add_rule_fg(struct mlx5_flow_group *fg,
1008 u32 *match_value,
1009 u8 action,
1010 u32 flow_tag,
1011 struct mlx5_flow_destination *dest)
1012{
1013 struct fs_fte *fte;
1014 struct mlx5_flow_rule *rule;
1015 struct mlx5_flow_table *ft;
1016 struct list_head *prev;
1017
f0d22d18 1018 nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
0c56b975 1019 fs_for_each_fte(fte, fg) {
f0d22d18 1020 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
0c56b975
MG
1021 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
1022 action == fte->action && flow_tag == fte->flow_tag) {
b3638e1a
MG
1023 rule = find_flow_rule(fte, dest);
1024 if (rule) {
1025 atomic_inc(&rule->node.refcount);
1026 unlock_ref_node(&fte->node);
1027 unlock_ref_node(&fg->node);
1028 return rule;
1029 }
0c56b975
MG
1030 rule = add_rule_fte(fte, fg, dest);
1031 unlock_ref_node(&fte->node);
1032 if (IS_ERR(rule))
1033 goto unlock_fg;
1034 else
1035 goto add_rule;
1036 }
1037 unlock_ref_node(&fte->node);
1038 }
1039 fs_get_obj(ft, fg->node.parent);
1040 if (fg->num_ftes >= fg->max_ftes) {
1041 rule = ERR_PTR(-ENOSPC);
1042 goto unlock_fg;
1043 }
1044
1045 fte = create_fte(fg, match_value, action, flow_tag, &prev);
1046 if (IS_ERR(fte)) {
1047 rule = (void *)fte;
1048 goto unlock_fg;
1049 }
1050 tree_init_node(&fte->node, 0, del_fte);
1051 rule = add_rule_fte(fte, fg, dest);
1052 if (IS_ERR(rule)) {
1053 kfree(fte);
1054 goto unlock_fg;
1055 }
1056
1057 fg->num_ftes++;
1058
1059 tree_add_node(&fte->node, &fg->node);
1060 list_add(&fte->node.list, prev);
1061add_rule:
1062 tree_add_node(&rule->node, &fte->node);
1063unlock_fg:
1064 unlock_ref_node(&fg->node);
1065 return rule;
1066}
1067
b3638e1a
MG
1068static struct mlx5_flow_rule *
1069_mlx5_add_flow_rule(struct mlx5_flow_table *ft,
1070 u8 match_criteria_enable,
1071 u32 *match_criteria,
1072 u32 *match_value,
1073 u32 action,
1074 u32 flow_tag,
1075 struct mlx5_flow_destination *dest)
0c56b975
MG
1076{
1077 struct mlx5_flow_group *g;
f0d22d18 1078 struct mlx5_flow_rule *rule;
0c56b975 1079
60ab4584
AV
1080 if ((action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) && !dest)
1081 return ERR_PTR(-EINVAL);
1082
f0d22d18 1083 nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
0c56b975
MG
1084 fs_for_each_fg(g, ft)
1085 if (compare_match_criteria(g->mask.match_criteria_enable,
1086 match_criteria_enable,
1087 g->mask.match_criteria,
1088 match_criteria)) {
0c56b975
MG
1089 rule = add_rule_fg(g, match_value,
1090 action, flow_tag, dest);
f0d22d18
MG
1091 if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
1092 goto unlock;
0c56b975 1093 }
f0d22d18 1094
c3f9bf62
MG
1095 g = create_autogroup(ft, match_criteria_enable, match_criteria);
1096 if (IS_ERR(g)) {
1097 rule = (void *)g;
1098 goto unlock;
1099 }
1100
1101 rule = add_rule_fg(g, match_value,
1102 action, flow_tag, dest);
1103 if (IS_ERR(rule)) {
1104 /* Remove assumes refcount > 0 and autogroup creates a group
1105 * with a refcount = 0.
1106 */
1107 unlock_ref_node(&ft->node);
1108 tree_get_node(&g->node);
1109 tree_remove_node(&g->node);
1110 return rule;
1111 }
f0d22d18 1112unlock:
0c56b975 1113 unlock_ref_node(&ft->node);
0c56b975
MG
1114 return rule;
1115}
b3638e1a
MG
1116
1117static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1118{
1119 return ((ft->type == FS_FT_NIC_RX) &&
1120 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1121}
1122
1123struct mlx5_flow_rule *
1124mlx5_add_flow_rule(struct mlx5_flow_table *ft,
1125 u8 match_criteria_enable,
1126 u32 *match_criteria,
1127 u32 *match_value,
1128 u32 action,
1129 u32 flow_tag,
1130 struct mlx5_flow_destination *dest)
1131{
1132 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1133 struct mlx5_flow_destination gen_dest;
1134 struct mlx5_flow_table *next_ft = NULL;
1135 struct mlx5_flow_rule *rule = NULL;
1136 u32 sw_action = action;
1137 struct fs_prio *prio;
1138
1139 fs_get_obj(prio, ft->node.parent);
1140 if (action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1141 if (!fwd_next_prio_supported(ft))
1142 return ERR_PTR(-EOPNOTSUPP);
1143 if (dest)
1144 return ERR_PTR(-EINVAL);
1145 mutex_lock(&root->chain_lock);
1146 next_ft = find_next_chained_ft(prio);
1147 if (next_ft) {
1148 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1149 gen_dest.ft = next_ft;
1150 dest = &gen_dest;
1151 action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1152 } else {
1153 mutex_unlock(&root->chain_lock);
1154 return ERR_PTR(-EOPNOTSUPP);
1155 }
1156 }
1157
1158 rule = _mlx5_add_flow_rule(ft, match_criteria_enable, match_criteria,
1159 match_value, action, flow_tag, dest);
1160
1161 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1162 if (!IS_ERR_OR_NULL(rule) &&
1163 (list_empty(&rule->next_ft))) {
1164 mutex_lock(&next_ft->lock);
1165 list_add(&rule->next_ft, &next_ft->fwd_rules);
1166 mutex_unlock(&next_ft->lock);
1167 rule->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1168 }
1169 mutex_unlock(&root->chain_lock);
1170 }
1171 return rule;
1172}
b217ea25 1173EXPORT_SYMBOL(mlx5_add_flow_rule);
0c56b975 1174
86d722ad 1175void mlx5_del_flow_rule(struct mlx5_flow_rule *rule)
0c56b975
MG
1176{
1177 tree_remove_node(&rule->node);
1178}
b217ea25 1179EXPORT_SYMBOL(mlx5_del_flow_rule);
0c56b975 1180
2cc43b49
MG
1181/* Assuming prio->node.children(flow tables) is sorted by level */
1182static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1183{
1184 struct fs_prio *prio;
1185
1186 fs_get_obj(prio, ft->node.parent);
1187
1188 if (!list_is_last(&ft->node.list, &prio->node.children))
1189 return list_next_entry(ft, node.list);
1190 return find_next_chained_ft(prio);
1191}
1192
1193static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1194{
1195 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1196 struct mlx5_flow_table *new_root_ft = NULL;
1197
1198 if (root->root_ft != ft)
1199 return 0;
1200
1201 new_root_ft = find_next_ft(ft);
1202 if (new_root_ft) {
1203 int err = mlx5_cmd_update_root_ft(root->dev, new_root_ft);
1204
1205 if (err) {
1206 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
1207 ft->id);
1208 return err;
1209 }
1210 root->root_ft = new_root_ft;
1211 }
1212 return 0;
1213}
1214
f90edfd2
MG
1215/* Connect flow table from previous priority to
1216 * the next flow table.
1217 */
1218static int disconnect_flow_table(struct mlx5_flow_table *ft)
1219{
1220 struct mlx5_core_dev *dev = get_dev(&ft->node);
1221 struct mlx5_flow_table *next_ft;
1222 struct fs_prio *prio;
1223 int err = 0;
1224
1225 err = update_root_ft_destroy(ft);
1226 if (err)
1227 return err;
1228
1229 fs_get_obj(prio, ft->node.parent);
1230 if (!(list_first_entry(&prio->node.children,
1231 struct mlx5_flow_table,
1232 node.list) == ft))
1233 return 0;
1234
1235 next_ft = find_next_chained_ft(prio);
b3638e1a
MG
1236 err = connect_fwd_rules(dev, next_ft, ft);
1237 if (err)
1238 return err;
1239
f90edfd2
MG
1240 err = connect_prev_fts(dev, next_ft, prio);
1241 if (err)
1242 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1243 ft->id);
1244 return err;
1245}
1246
86d722ad 1247int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
0c56b975 1248{
2cc43b49
MG
1249 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1250 int err = 0;
1251
1252 mutex_lock(&root->chain_lock);
f90edfd2 1253 err = disconnect_flow_table(ft);
2cc43b49
MG
1254 if (err) {
1255 mutex_unlock(&root->chain_lock);
1256 return err;
1257 }
0c56b975
MG
1258 if (tree_remove_node(&ft->node))
1259 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1260 ft->id);
2cc43b49 1261 mutex_unlock(&root->chain_lock);
0c56b975 1262
2cc43b49 1263 return err;
0c56b975 1264}
b217ea25 1265EXPORT_SYMBOL(mlx5_destroy_flow_table);
0c56b975 1266
86d722ad 1267void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
0c56b975
MG
1268{
1269 if (tree_remove_node(&fg->node))
1270 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1271 fg->id);
1272}
25302363 1273
86d722ad
MG
1274struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1275 enum mlx5_flow_namespace_type type)
25302363
MG
1276{
1277 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1278 int prio;
78228cbd 1279 struct fs_prio *fs_prio;
25302363
MG
1280 struct mlx5_flow_namespace *ns;
1281
1282 if (!root_ns)
1283 return NULL;
1284
1285 switch (type) {
4cbdd30e 1286 case MLX5_FLOW_NAMESPACE_BYPASS:
25302363 1287 case MLX5_FLOW_NAMESPACE_KERNEL:
4cbdd30e 1288 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
153fefbf 1289 case MLX5_FLOW_NAMESPACE_ANCHOR:
4cbdd30e 1290 prio = type;
25302363
MG
1291 break;
1292 case MLX5_FLOW_NAMESPACE_FDB:
1293 if (dev->priv.fdb_root_ns)
1294 return &dev->priv.fdb_root_ns->ns;
1295 else
1296 return NULL;
1297 default:
1298 return NULL;
1299 }
1300
1301 fs_prio = find_prio(&root_ns->ns, prio);
1302 if (!fs_prio)
1303 return NULL;
1304
1305 ns = list_first_entry(&fs_prio->node.children,
1306 typeof(*ns),
1307 node.list);
1308
1309 return ns;
1310}
b217ea25 1311EXPORT_SYMBOL(mlx5_get_flow_namespace);
25302363
MG
1312
1313static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
655227ed 1314 unsigned prio, int max_ft)
25302363
MG
1315{
1316 struct fs_prio *fs_prio;
1317
1318 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
1319 if (!fs_prio)
1320 return ERR_PTR(-ENOMEM);
1321
1322 fs_prio->node.type = FS_TYPE_PRIO;
1323 tree_init_node(&fs_prio->node, 1, NULL);
1324 tree_add_node(&fs_prio->node, &ns->node);
1325 fs_prio->max_ft = max_ft;
1326 fs_prio->prio = prio;
25302363
MG
1327 list_add_tail(&fs_prio->node.list, &ns->node.children);
1328
1329 return fs_prio;
1330}
1331
1332static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
1333 *ns)
1334{
1335 ns->node.type = FS_TYPE_NAMESPACE;
1336
1337 return ns;
1338}
1339
1340static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
1341{
1342 struct mlx5_flow_namespace *ns;
1343
1344 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1345 if (!ns)
1346 return ERR_PTR(-ENOMEM);
1347
1348 fs_init_namespace(ns);
1349 tree_init_node(&ns->node, 1, NULL);
1350 tree_add_node(&ns->node, &prio->node);
1351 list_add_tail(&ns->node.list, &prio->node.children);
1352
1353 return ns;
1354}
1355
4cbdd30e
MG
1356static int create_leaf_prios(struct mlx5_flow_namespace *ns, struct init_tree_node
1357 *prio_metadata)
1358{
1359 struct fs_prio *fs_prio;
1360 int i;
1361
1362 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
1363 fs_prio = fs_create_prio(ns, i, prio_metadata->max_ft);
1364 if (IS_ERR(fs_prio))
1365 return PTR_ERR(fs_prio);
1366 }
1367 return 0;
1368}
1369
8d40d162
MG
1370#define FLOW_TABLE_BIT_SZ 1
1371#define GET_FLOW_TABLE_CAP(dev, offset) \
1372 ((be32_to_cpu(*((__be32 *)(dev->hca_caps_cur[MLX5_CAP_FLOW_TABLE]) + \
1373 offset / 32)) >> \
1374 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
1375static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
1376{
1377 int i;
1378
1379 for (i = 0; i < caps->arr_sz; i++) {
1380 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
1381 return false;
1382 }
1383 return true;
1384}
1385
1386static int init_root_tree_recursive(struct mlx5_core_dev *dev,
1387 struct init_tree_node *init_node,
25302363
MG
1388 struct fs_node *fs_parent_node,
1389 struct init_tree_node *init_parent_node,
1390 int index)
1391{
8d40d162
MG
1392 int max_ft_level = MLX5_CAP_FLOWTABLE(dev,
1393 flow_table_properties_nic_receive.
1394 max_ft_level);
25302363
MG
1395 struct mlx5_flow_namespace *fs_ns;
1396 struct fs_prio *fs_prio;
1397 struct fs_node *base;
1398 int i;
1399 int err;
1400
1401 if (init_node->type == FS_TYPE_PRIO) {
8d40d162
MG
1402 if ((init_node->min_ft_level > max_ft_level) ||
1403 !has_required_caps(dev, &init_node->caps))
1404 return 0;
25302363
MG
1405
1406 fs_get_obj(fs_ns, fs_parent_node);
4cbdd30e
MG
1407 if (init_node->num_leaf_prios)
1408 return create_leaf_prios(fs_ns, init_node);
655227ed 1409 fs_prio = fs_create_prio(fs_ns, index, init_node->max_ft);
25302363
MG
1410 if (IS_ERR(fs_prio))
1411 return PTR_ERR(fs_prio);
1412 base = &fs_prio->node;
1413 } else if (init_node->type == FS_TYPE_NAMESPACE) {
1414 fs_get_obj(fs_prio, fs_parent_node);
1415 fs_ns = fs_create_namespace(fs_prio);
1416 if (IS_ERR(fs_ns))
1417 return PTR_ERR(fs_ns);
1418 base = &fs_ns->node;
1419 } else {
1420 return -EINVAL;
1421 }
1422 for (i = 0; i < init_node->ar_size; i++) {
8d40d162
MG
1423 err = init_root_tree_recursive(dev, &init_node->children[i],
1424 base, init_node, i);
25302363
MG
1425 if (err)
1426 return err;
1427 }
1428
1429 return 0;
1430}
1431
8d40d162
MG
1432static int init_root_tree(struct mlx5_core_dev *dev,
1433 struct init_tree_node *init_node,
25302363
MG
1434 struct fs_node *fs_parent_node)
1435{
1436 int i;
1437 struct mlx5_flow_namespace *fs_ns;
1438 int err;
1439
1440 fs_get_obj(fs_ns, fs_parent_node);
1441 for (i = 0; i < init_node->ar_size; i++) {
8d40d162 1442 err = init_root_tree_recursive(dev, &init_node->children[i],
25302363
MG
1443 &fs_ns->node,
1444 init_node, i);
1445 if (err)
1446 return err;
1447 }
1448 return 0;
1449}
1450
1451static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_core_dev *dev,
1452 enum fs_flow_table_type
1453 table_type)
1454{
1455 struct mlx5_flow_root_namespace *root_ns;
1456 struct mlx5_flow_namespace *ns;
1457
86d722ad 1458 /* Create the root namespace */
25302363
MG
1459 root_ns = mlx5_vzalloc(sizeof(*root_ns));
1460 if (!root_ns)
1461 return NULL;
1462
1463 root_ns->dev = dev;
1464 root_ns->table_type = table_type;
1465
1466 ns = &root_ns->ns;
1467 fs_init_namespace(ns);
2cc43b49 1468 mutex_init(&root_ns->chain_lock);
25302363
MG
1469 tree_init_node(&ns->node, 1, NULL);
1470 tree_add_node(&ns->node, NULL);
1471
1472 return root_ns;
1473}
1474
655227ed
MG
1475static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
1476
1477static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
1478{
1479 struct fs_prio *prio;
1480
1481 fs_for_each_prio(prio, ns) {
1482 /* This updates prio start_level and max_ft */
1483 set_prio_attrs_in_prio(prio, acc_level);
1484 acc_level += prio->max_ft;
1485 }
1486 return acc_level;
1487}
1488
1489static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
1490{
1491 struct mlx5_flow_namespace *ns;
1492 int acc_level_ns = acc_level;
1493
1494 prio->start_level = acc_level;
1495 fs_for_each_ns(ns, prio)
1496 /* This updates start_level and max_ft of ns's priority descendants */
1497 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
1498 if (!prio->max_ft)
1499 prio->max_ft = acc_level_ns - prio->start_level;
1500 WARN_ON(prio->max_ft < acc_level_ns - prio->start_level);
1501}
1502
1503static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
1504{
1505 struct mlx5_flow_namespace *ns = &root_ns->ns;
1506 struct fs_prio *prio;
1507 int start_level = 0;
1508
1509 fs_for_each_prio(prio, ns) {
1510 set_prio_attrs_in_prio(prio, start_level);
1511 start_level += prio->max_ft;
1512 }
1513}
1514
153fefbf
MG
1515#define ANCHOR_PRIO 0
1516#define ANCHOR_SIZE 1
1517static int create_anchor_flow_table(struct mlx5_core_dev
1518 *dev)
1519{
1520 struct mlx5_flow_namespace *ns = NULL;
1521 struct mlx5_flow_table *ft;
1522
1523 ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_ANCHOR);
1524 if (!ns)
1525 return -EINVAL;
1526 ft = mlx5_create_flow_table(ns, ANCHOR_PRIO, ANCHOR_SIZE);
1527 if (IS_ERR(ft)) {
1528 mlx5_core_err(dev, "Failed to create last anchor flow table");
1529 return PTR_ERR(ft);
1530 }
1531 return 0;
1532}
1533
25302363
MG
1534static int init_root_ns(struct mlx5_core_dev *dev)
1535{
25302363
MG
1536
1537 dev->priv.root_ns = create_root_ns(dev, FS_FT_NIC_RX);
1538 if (IS_ERR_OR_NULL(dev->priv.root_ns))
1539 goto cleanup;
1540
8d40d162 1541 if (init_root_tree(dev, &root_fs, &dev->priv.root_ns->ns.node))
25302363
MG
1542 goto cleanup;
1543
655227ed
MG
1544 set_prio_attrs(dev->priv.root_ns);
1545
153fefbf
MG
1546 if (create_anchor_flow_table(dev))
1547 goto cleanup;
1548
25302363
MG
1549 return 0;
1550
1551cleanup:
1552 mlx5_cleanup_fs(dev);
1553 return -ENOMEM;
1554}
1555
1556static void cleanup_single_prio_root_ns(struct mlx5_core_dev *dev,
1557 struct mlx5_flow_root_namespace *root_ns)
1558{
1559 struct fs_node *prio;
1560
1561 if (!root_ns)
1562 return;
1563
1564 if (!list_empty(&root_ns->ns.node.children)) {
1565 prio = list_first_entry(&root_ns->ns.node.children,
1566 struct fs_node,
1567 list);
1568 if (tree_remove_node(prio))
1569 mlx5_core_warn(dev,
1570 "Flow steering priority wasn't destroyed, refcount > 1\n");
1571 }
1572 if (tree_remove_node(&root_ns->ns.node))
1573 mlx5_core_warn(dev,
1574 "Flow steering namespace wasn't destroyed, refcount > 1\n");
1575 root_ns = NULL;
1576}
1577
153fefbf
MG
1578static void destroy_flow_tables(struct fs_prio *prio)
1579{
1580 struct mlx5_flow_table *iter;
1581 struct mlx5_flow_table *tmp;
1582
1583 fs_for_each_ft_safe(iter, tmp, prio)
1584 mlx5_destroy_flow_table(iter);
1585}
1586
25302363
MG
1587static void cleanup_root_ns(struct mlx5_core_dev *dev)
1588{
1589 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1590 struct fs_prio *iter_prio;
1591
1592 if (!MLX5_CAP_GEN(dev, nic_flow_table))
1593 return;
1594
1595 if (!root_ns)
1596 return;
1597
1598 /* stage 1 */
1599 fs_for_each_prio(iter_prio, &root_ns->ns) {
1600 struct fs_node *node;
1601 struct mlx5_flow_namespace *iter_ns;
1602
1603 fs_for_each_ns_or_ft(node, iter_prio) {
1604 if (node->type == FS_TYPE_FLOW_TABLE)
1605 continue;
1606 fs_get_obj(iter_ns, node);
1607 while (!list_empty(&iter_ns->node.children)) {
1608 struct fs_prio *obj_iter_prio2;
1609 struct fs_node *iter_prio2 =
1610 list_first_entry(&iter_ns->node.children,
1611 struct fs_node,
1612 list);
1613
1614 fs_get_obj(obj_iter_prio2, iter_prio2);
153fefbf 1615 destroy_flow_tables(obj_iter_prio2);
25302363
MG
1616 if (tree_remove_node(iter_prio2)) {
1617 mlx5_core_warn(dev,
1618 "Priority %d wasn't destroyed, refcount > 1\n",
1619 obj_iter_prio2->prio);
1620 return;
1621 }
1622 }
1623 }
1624 }
1625
1626 /* stage 2 */
1627 fs_for_each_prio(iter_prio, &root_ns->ns) {
1628 while (!list_empty(&iter_prio->node.children)) {
1629 struct fs_node *iter_ns =
1630 list_first_entry(&iter_prio->node.children,
1631 struct fs_node,
1632 list);
1633 if (tree_remove_node(iter_ns)) {
1634 mlx5_core_warn(dev,
1635 "Namespace wasn't destroyed, refcount > 1\n");
1636 return;
1637 }
1638 }
1639 }
1640
1641 /* stage 3 */
1642 while (!list_empty(&root_ns->ns.node.children)) {
1643 struct fs_prio *obj_prio_node;
1644 struct fs_node *prio_node =
1645 list_first_entry(&root_ns->ns.node.children,
1646 struct fs_node,
1647 list);
1648
1649 fs_get_obj(obj_prio_node, prio_node);
1650 if (tree_remove_node(prio_node)) {
1651 mlx5_core_warn(dev,
1652 "Priority %d wasn't destroyed, refcount > 1\n",
1653 obj_prio_node->prio);
1654 return;
1655 }
1656 }
1657
1658 if (tree_remove_node(&root_ns->ns.node)) {
1659 mlx5_core_warn(dev,
1660 "root namespace wasn't destroyed, refcount > 1\n");
1661 return;
1662 }
1663
1664 dev->priv.root_ns = NULL;
1665}
1666
1667void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1668{
1669 cleanup_root_ns(dev);
1670 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1671}
1672
1673static int init_fdb_root_ns(struct mlx5_core_dev *dev)
1674{
1675 struct fs_prio *prio;
1676
1677 dev->priv.fdb_root_ns = create_root_ns(dev, FS_FT_FDB);
1678 if (!dev->priv.fdb_root_ns)
1679 return -ENOMEM;
1680
86d722ad 1681 /* Create single prio */
655227ed 1682 prio = fs_create_prio(&dev->priv.fdb_root_ns->ns, 0, 1);
25302363
MG
1683 if (IS_ERR(prio)) {
1684 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1685 return PTR_ERR(prio);
1686 } else {
1687 return 0;
1688 }
1689}
1690
1691int mlx5_init_fs(struct mlx5_core_dev *dev)
1692{
1693 int err = 0;
1694
1695 if (MLX5_CAP_GEN(dev, nic_flow_table)) {
1696 err = init_root_ns(dev);
1697 if (err)
1698 return err;
1699 }
1700 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
1701 err = init_fdb_root_ns(dev);
1702 if (err)
1703 cleanup_root_ns(dev);
1704 }
1705
1706 return err;
1707}