fuse: update mailing list in MAINTAINERS
[deliverable/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_core.c
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"
38 #include "fs_cmd.h"
39
40 #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
41 sizeof(struct init_tree_node))
42
43 #define ADD_PRIO(num_prios_val, min_level_val, max_ft_val, caps_val,\
44 ...) {.type = FS_TYPE_PRIO,\
45 .min_ft_level = min_level_val,\
46 .max_ft = max_ft_val,\
47 .num_leaf_prios = num_prios_val,\
48 .caps = caps_val,\
49 .children = (struct init_tree_node[]) {__VA_ARGS__},\
50 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
51 }
52
53 #define ADD_MULTIPLE_PRIO(num_prios_val, max_ft_val, ...)\
54 ADD_PRIO(num_prios_val, 0, max_ft_val, {},\
55 __VA_ARGS__)\
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
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
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
76 #define KERNEL_MAX_FT 3
77 #define KERNEL_NUM_PRIOS 2
78 #define KENREL_MIN_LEVEL 2
79
80 #define ANCHOR_MAX_FT 1
81 #define ANCHOR_NUM_PRIOS 1
82 #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
83 struct node_caps {
84 size_t arr_sz;
85 long *caps;
86 };
87 static struct init_tree_node {
88 enum fs_node_type type;
89 struct init_tree_node *children;
90 int ar_size;
91 struct node_caps caps;
92 int min_ft_level;
93 int num_leaf_prios;
94 int prio;
95 int max_ft;
96 } root_fs = {
97 .type = FS_TYPE_NAMESPACE,
98 .ar_size = 4,
99 .children = (struct init_tree_node[]) {
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))),
114 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
115 ADD_NS(ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS, ANCHOR_MAX_FT))),
116 }
117 };
118
119 enum fs_i_mutex_lock_class {
120 FS_MUTEX_GRANDPARENT,
121 FS_MUTEX_PARENT,
122 FS_MUTEX_CHILD
123 };
124
125 static void del_rule(struct fs_node *node);
126 static void del_flow_table(struct fs_node *node);
127 static void del_flow_group(struct fs_node *node);
128 static void del_fte(struct fs_node *node);
129
130 static 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
141 static 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
154 static void tree_get_node(struct fs_node *node)
155 {
156 atomic_inc(&node->refcount);
157 }
158
159 static void nested_lock_ref_node(struct fs_node *node,
160 enum fs_i_mutex_lock_class class)
161 {
162 if (node) {
163 mutex_lock_nested(&node->lock, class);
164 atomic_inc(&node->refcount);
165 }
166 }
167
168 static 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
176 static 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
184 static 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
202 static int tree_remove_node(struct fs_node *node)
203 {
204 if (atomic_read(&node->refcount) > 1) {
205 atomic_dec(&node->refcount);
206 return -EEXIST;
207 }
208 tree_put_node(node);
209 return 0;
210 }
211
212 static 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
225 static 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
238 static 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
250 static 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
297 static 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 }
304
305 static 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
321 static 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
330 static 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
347 static 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);
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 }
375 if ((fte->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
376 --fte->dests_size) {
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
386 static 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
409 static 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
424 static 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
444 static 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
467 static 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;
480 INIT_LIST_HEAD(&ft->fwd_rules);
481 mutex_init(&ft->lock);
482
483 return ft;
484 }
485
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 */
490 static 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 */
525 static 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 */
542 static 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 */
548 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
549 {
550 return find_closest_ft(prio, true);
551 }
552
553 static 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 */
579 static 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
595 static 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
618 static 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 */
642 static 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
671 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
672 struct fs_prio *prio)
673 {
674 struct mlx5_flow_table *next_ft;
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;
683
684 next_ft = find_next_chained_ft(prio);
685 err = connect_fwd_rules(dev, ft, next_ft);
686 if (err)
687 return err;
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
696 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
697 int prio,
698 int max_fte)
699 {
700 struct mlx5_flow_table *next_ft = NULL;
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
713 mutex_lock(&root->chain_lock);
714 fs_prio = find_prio(ns, prio);
715 if (!fs_prio) {
716 err = -EINVAL;
717 goto unlock_root;
718 }
719 if (fs_prio->num_ft == fs_prio->max_ft) {
720 err = -ENOSPC;
721 goto unlock_root;
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;
729 goto unlock_root;
730 }
731
732 tree_init_node(&ft->node, 1, del_flow_table);
733 log_table_sz = ilog2(ft->max_fte);
734 next_ft = find_next_chained_ft(fs_prio);
735 err = mlx5_cmd_create_flow_table(root->dev, ft->type, ft->level,
736 log_table_sz, next_ft, &ft->id);
737 if (err)
738 goto free_ft;
739
740 err = connect_flow_table(root->dev, ft, fs_prio);
741 if (err)
742 goto destroy_ft;
743 lock_ref_node(&fs_prio->node);
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);
748 mutex_unlock(&root->chain_lock);
749 return ft;
750 destroy_ft:
751 mlx5_cmd_destroy_flow_table(root->dev, ft);
752 free_ft:
753 kfree(ft);
754 unlock_root:
755 mutex_unlock(&root->chain_lock);
756 return ERR_PTR(err);
757 }
758
759 struct 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 }
778 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
779
780 /* Flow table should be locked */
781 static 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)
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
798 err = mlx5_cmd_create_flow_group(dev, ft, fg_in, &fg->id);
799 if (err) {
800 kfree(fg);
801 return ERR_PTR(err);
802 }
803
804 if (ft->autogroup.active)
805 ft->autogroup.num_groups++;
806 /* Add node to tree */
807 tree_init_node(&fg->node, !is_auto_fg, del_flow_group);
808 tree_add_node(&fg->node, &ft->node);
809 /* Add node to group list */
810 list_add(&fg->node.list, ft->node.children.prev);
811
812 return fg;
813 }
814
815 struct 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);
825 unlock_ref_node(&ft->node);
826
827 return fg;
828 }
829
830 static 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
838 INIT_LIST_HEAD(&rule->next_ft);
839 rule->node.type = FS_TYPE_FLOW_DEST;
840 if (dest)
841 memcpy(&rule->dest_attr, dest, sizeof(*dest));
842
843 return rule;
844 }
845
846 /* fte should not be deleted while calling this function */
847 static 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);
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 */
863 tree_init_node(&rule->node, 1, del_rule);
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);
868 if (dest)
869 fte->dests_size++;
870 if (fte->dests_size == 1 || !dest)
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
883 free_rule:
884 list_del(&rule->node.list);
885 kfree(rule);
886 if (dest)
887 fte->dests_size--;
888 return ERR_PTR(err);
889 }
890
891 /* Assumed fg is locked */
892 static 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 */
914 static 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
931 static 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);
983 out:
984 kvfree(in);
985 return fg;
986 }
987
988 static 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
1007 static 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
1018 nested_lock_ref_node(&fg->node, FS_MUTEX_PARENT);
1019 fs_for_each_fte(fte, fg) {
1020 nested_lock_ref_node(&fte->node, FS_MUTEX_CHILD);
1021 if (compare_match_value(&fg->mask, match_value, &fte->val) &&
1022 action == fte->action && flow_tag == fte->flow_tag) {
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 }
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);
1061 add_rule:
1062 tree_add_node(&rule->node, &fte->node);
1063 unlock_fg:
1064 unlock_ref_node(&fg->node);
1065 return rule;
1066 }
1067
1068 static struct mlx5_flow_rule *add_rule_to_auto_fg(struct mlx5_flow_table *ft,
1069 u8 match_criteria_enable,
1070 u32 *match_criteria,
1071 u32 *match_value,
1072 u8 action,
1073 u32 flow_tag,
1074 struct mlx5_flow_destination *dest)
1075 {
1076 struct mlx5_flow_rule *rule;
1077 struct mlx5_flow_group *g;
1078
1079 g = create_autogroup(ft, match_criteria_enable, match_criteria);
1080 if (IS_ERR(g))
1081 return (void *)g;
1082
1083 rule = add_rule_fg(g, match_value,
1084 action, flow_tag, dest);
1085 if (IS_ERR(rule)) {
1086 /* Remove assumes refcount > 0 and autogroup creates a group
1087 * with a refcount = 0.
1088 */
1089 tree_get_node(&g->node);
1090 tree_remove_node(&g->node);
1091 }
1092 return rule;
1093 }
1094
1095 static struct mlx5_flow_rule *
1096 _mlx5_add_flow_rule(struct mlx5_flow_table *ft,
1097 u8 match_criteria_enable,
1098 u32 *match_criteria,
1099 u32 *match_value,
1100 u32 action,
1101 u32 flow_tag,
1102 struct mlx5_flow_destination *dest)
1103 {
1104 struct mlx5_flow_group *g;
1105 struct mlx5_flow_rule *rule;
1106
1107 if ((action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) && !dest)
1108 return ERR_PTR(-EINVAL);
1109
1110 nested_lock_ref_node(&ft->node, FS_MUTEX_GRANDPARENT);
1111 fs_for_each_fg(g, ft)
1112 if (compare_match_criteria(g->mask.match_criteria_enable,
1113 match_criteria_enable,
1114 g->mask.match_criteria,
1115 match_criteria)) {
1116 rule = add_rule_fg(g, match_value,
1117 action, flow_tag, dest);
1118 if (!IS_ERR(rule) || PTR_ERR(rule) != -ENOSPC)
1119 goto unlock;
1120 }
1121
1122 rule = add_rule_to_auto_fg(ft, match_criteria_enable, match_criteria,
1123 match_value, action, flow_tag, dest);
1124 unlock:
1125 unlock_ref_node(&ft->node);
1126 return rule;
1127 }
1128
1129 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1130 {
1131 return ((ft->type == FS_FT_NIC_RX) &&
1132 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1133 }
1134
1135 struct mlx5_flow_rule *
1136 mlx5_add_flow_rule(struct mlx5_flow_table *ft,
1137 u8 match_criteria_enable,
1138 u32 *match_criteria,
1139 u32 *match_value,
1140 u32 action,
1141 u32 flow_tag,
1142 struct mlx5_flow_destination *dest)
1143 {
1144 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1145 struct mlx5_flow_destination gen_dest;
1146 struct mlx5_flow_table *next_ft = NULL;
1147 struct mlx5_flow_rule *rule = NULL;
1148 u32 sw_action = action;
1149 struct fs_prio *prio;
1150
1151 fs_get_obj(prio, ft->node.parent);
1152 if (action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1153 if (!fwd_next_prio_supported(ft))
1154 return ERR_PTR(-EOPNOTSUPP);
1155 if (dest)
1156 return ERR_PTR(-EINVAL);
1157 mutex_lock(&root->chain_lock);
1158 next_ft = find_next_chained_ft(prio);
1159 if (next_ft) {
1160 gen_dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1161 gen_dest.ft = next_ft;
1162 dest = &gen_dest;
1163 action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1164 } else {
1165 mutex_unlock(&root->chain_lock);
1166 return ERR_PTR(-EOPNOTSUPP);
1167 }
1168 }
1169
1170 rule = _mlx5_add_flow_rule(ft, match_criteria_enable, match_criteria,
1171 match_value, action, flow_tag, dest);
1172
1173 if (sw_action == MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO) {
1174 if (!IS_ERR_OR_NULL(rule) &&
1175 (list_empty(&rule->next_ft))) {
1176 mutex_lock(&next_ft->lock);
1177 list_add(&rule->next_ft, &next_ft->fwd_rules);
1178 mutex_unlock(&next_ft->lock);
1179 rule->sw_action = MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1180 }
1181 mutex_unlock(&root->chain_lock);
1182 }
1183 return rule;
1184 }
1185 EXPORT_SYMBOL(mlx5_add_flow_rule);
1186
1187 void mlx5_del_flow_rule(struct mlx5_flow_rule *rule)
1188 {
1189 tree_remove_node(&rule->node);
1190 }
1191 EXPORT_SYMBOL(mlx5_del_flow_rule);
1192
1193 /* Assuming prio->node.children(flow tables) is sorted by level */
1194 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
1195 {
1196 struct fs_prio *prio;
1197
1198 fs_get_obj(prio, ft->node.parent);
1199
1200 if (!list_is_last(&ft->node.list, &prio->node.children))
1201 return list_next_entry(ft, node.list);
1202 return find_next_chained_ft(prio);
1203 }
1204
1205 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
1206 {
1207 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1208 struct mlx5_flow_table *new_root_ft = NULL;
1209
1210 if (root->root_ft != ft)
1211 return 0;
1212
1213 new_root_ft = find_next_ft(ft);
1214 if (new_root_ft) {
1215 int err = mlx5_cmd_update_root_ft(root->dev, new_root_ft);
1216
1217 if (err) {
1218 mlx5_core_warn(root->dev, "Update root flow table of id=%u failed\n",
1219 ft->id);
1220 return err;
1221 }
1222 root->root_ft = new_root_ft;
1223 }
1224 return 0;
1225 }
1226
1227 /* Connect flow table from previous priority to
1228 * the next flow table.
1229 */
1230 static int disconnect_flow_table(struct mlx5_flow_table *ft)
1231 {
1232 struct mlx5_core_dev *dev = get_dev(&ft->node);
1233 struct mlx5_flow_table *next_ft;
1234 struct fs_prio *prio;
1235 int err = 0;
1236
1237 err = update_root_ft_destroy(ft);
1238 if (err)
1239 return err;
1240
1241 fs_get_obj(prio, ft->node.parent);
1242 if (!(list_first_entry(&prio->node.children,
1243 struct mlx5_flow_table,
1244 node.list) == ft))
1245 return 0;
1246
1247 next_ft = find_next_chained_ft(prio);
1248 err = connect_fwd_rules(dev, next_ft, ft);
1249 if (err)
1250 return err;
1251
1252 err = connect_prev_fts(dev, next_ft, prio);
1253 if (err)
1254 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
1255 ft->id);
1256 return err;
1257 }
1258
1259 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
1260 {
1261 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1262 int err = 0;
1263
1264 mutex_lock(&root->chain_lock);
1265 err = disconnect_flow_table(ft);
1266 if (err) {
1267 mutex_unlock(&root->chain_lock);
1268 return err;
1269 }
1270 if (tree_remove_node(&ft->node))
1271 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
1272 ft->id);
1273 mutex_unlock(&root->chain_lock);
1274
1275 return err;
1276 }
1277 EXPORT_SYMBOL(mlx5_destroy_flow_table);
1278
1279 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
1280 {
1281 if (tree_remove_node(&fg->node))
1282 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
1283 fg->id);
1284 }
1285
1286 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
1287 enum mlx5_flow_namespace_type type)
1288 {
1289 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1290 int prio;
1291 static struct fs_prio *fs_prio;
1292 struct mlx5_flow_namespace *ns;
1293
1294 if (!root_ns)
1295 return NULL;
1296
1297 switch (type) {
1298 case MLX5_FLOW_NAMESPACE_BYPASS:
1299 case MLX5_FLOW_NAMESPACE_KERNEL:
1300 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
1301 case MLX5_FLOW_NAMESPACE_ANCHOR:
1302 prio = type;
1303 break;
1304 case MLX5_FLOW_NAMESPACE_FDB:
1305 if (dev->priv.fdb_root_ns)
1306 return &dev->priv.fdb_root_ns->ns;
1307 else
1308 return NULL;
1309 default:
1310 return NULL;
1311 }
1312
1313 fs_prio = find_prio(&root_ns->ns, prio);
1314 if (!fs_prio)
1315 return NULL;
1316
1317 ns = list_first_entry(&fs_prio->node.children,
1318 typeof(*ns),
1319 node.list);
1320
1321 return ns;
1322 }
1323 EXPORT_SYMBOL(mlx5_get_flow_namespace);
1324
1325 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
1326 unsigned prio, int max_ft)
1327 {
1328 struct fs_prio *fs_prio;
1329
1330 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
1331 if (!fs_prio)
1332 return ERR_PTR(-ENOMEM);
1333
1334 fs_prio->node.type = FS_TYPE_PRIO;
1335 tree_init_node(&fs_prio->node, 1, NULL);
1336 tree_add_node(&fs_prio->node, &ns->node);
1337 fs_prio->max_ft = max_ft;
1338 fs_prio->prio = prio;
1339 list_add_tail(&fs_prio->node.list, &ns->node.children);
1340
1341 return fs_prio;
1342 }
1343
1344 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
1345 *ns)
1346 {
1347 ns->node.type = FS_TYPE_NAMESPACE;
1348
1349 return ns;
1350 }
1351
1352 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio)
1353 {
1354 struct mlx5_flow_namespace *ns;
1355
1356 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
1357 if (!ns)
1358 return ERR_PTR(-ENOMEM);
1359
1360 fs_init_namespace(ns);
1361 tree_init_node(&ns->node, 1, NULL);
1362 tree_add_node(&ns->node, &prio->node);
1363 list_add_tail(&ns->node.list, &prio->node.children);
1364
1365 return ns;
1366 }
1367
1368 static int create_leaf_prios(struct mlx5_flow_namespace *ns, struct init_tree_node
1369 *prio_metadata)
1370 {
1371 struct fs_prio *fs_prio;
1372 int i;
1373
1374 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
1375 fs_prio = fs_create_prio(ns, i, prio_metadata->max_ft);
1376 if (IS_ERR(fs_prio))
1377 return PTR_ERR(fs_prio);
1378 }
1379 return 0;
1380 }
1381
1382 #define FLOW_TABLE_BIT_SZ 1
1383 #define GET_FLOW_TABLE_CAP(dev, offset) \
1384 ((be32_to_cpu(*((__be32 *)(dev->hca_caps_cur[MLX5_CAP_FLOW_TABLE]) + \
1385 offset / 32)) >> \
1386 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
1387 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
1388 {
1389 int i;
1390
1391 for (i = 0; i < caps->arr_sz; i++) {
1392 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
1393 return false;
1394 }
1395 return true;
1396 }
1397
1398 static int init_root_tree_recursive(struct mlx5_core_dev *dev,
1399 struct init_tree_node *init_node,
1400 struct fs_node *fs_parent_node,
1401 struct init_tree_node *init_parent_node,
1402 int index)
1403 {
1404 int max_ft_level = MLX5_CAP_FLOWTABLE(dev,
1405 flow_table_properties_nic_receive.
1406 max_ft_level);
1407 struct mlx5_flow_namespace *fs_ns;
1408 struct fs_prio *fs_prio;
1409 struct fs_node *base;
1410 int i;
1411 int err;
1412
1413 if (init_node->type == FS_TYPE_PRIO) {
1414 if ((init_node->min_ft_level > max_ft_level) ||
1415 !has_required_caps(dev, &init_node->caps))
1416 return 0;
1417
1418 fs_get_obj(fs_ns, fs_parent_node);
1419 if (init_node->num_leaf_prios)
1420 return create_leaf_prios(fs_ns, init_node);
1421 fs_prio = fs_create_prio(fs_ns, index, init_node->max_ft);
1422 if (IS_ERR(fs_prio))
1423 return PTR_ERR(fs_prio);
1424 base = &fs_prio->node;
1425 } else if (init_node->type == FS_TYPE_NAMESPACE) {
1426 fs_get_obj(fs_prio, fs_parent_node);
1427 fs_ns = fs_create_namespace(fs_prio);
1428 if (IS_ERR(fs_ns))
1429 return PTR_ERR(fs_ns);
1430 base = &fs_ns->node;
1431 } else {
1432 return -EINVAL;
1433 }
1434 for (i = 0; i < init_node->ar_size; i++) {
1435 err = init_root_tree_recursive(dev, &init_node->children[i],
1436 base, init_node, i);
1437 if (err)
1438 return err;
1439 }
1440
1441 return 0;
1442 }
1443
1444 static int init_root_tree(struct mlx5_core_dev *dev,
1445 struct init_tree_node *init_node,
1446 struct fs_node *fs_parent_node)
1447 {
1448 int i;
1449 struct mlx5_flow_namespace *fs_ns;
1450 int err;
1451
1452 fs_get_obj(fs_ns, fs_parent_node);
1453 for (i = 0; i < init_node->ar_size; i++) {
1454 err = init_root_tree_recursive(dev, &init_node->children[i],
1455 &fs_ns->node,
1456 init_node, i);
1457 if (err)
1458 return err;
1459 }
1460 return 0;
1461 }
1462
1463 static struct mlx5_flow_root_namespace *create_root_ns(struct mlx5_core_dev *dev,
1464 enum fs_flow_table_type
1465 table_type)
1466 {
1467 struct mlx5_flow_root_namespace *root_ns;
1468 struct mlx5_flow_namespace *ns;
1469
1470 /* Create the root namespace */
1471 root_ns = mlx5_vzalloc(sizeof(*root_ns));
1472 if (!root_ns)
1473 return NULL;
1474
1475 root_ns->dev = dev;
1476 root_ns->table_type = table_type;
1477
1478 ns = &root_ns->ns;
1479 fs_init_namespace(ns);
1480 mutex_init(&root_ns->chain_lock);
1481 tree_init_node(&ns->node, 1, NULL);
1482 tree_add_node(&ns->node, NULL);
1483
1484 return root_ns;
1485 }
1486
1487 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
1488
1489 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
1490 {
1491 struct fs_prio *prio;
1492
1493 fs_for_each_prio(prio, ns) {
1494 /* This updates prio start_level and max_ft */
1495 set_prio_attrs_in_prio(prio, acc_level);
1496 acc_level += prio->max_ft;
1497 }
1498 return acc_level;
1499 }
1500
1501 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
1502 {
1503 struct mlx5_flow_namespace *ns;
1504 int acc_level_ns = acc_level;
1505
1506 prio->start_level = acc_level;
1507 fs_for_each_ns(ns, prio)
1508 /* This updates start_level and max_ft of ns's priority descendants */
1509 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
1510 if (!prio->max_ft)
1511 prio->max_ft = acc_level_ns - prio->start_level;
1512 WARN_ON(prio->max_ft < acc_level_ns - prio->start_level);
1513 }
1514
1515 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
1516 {
1517 struct mlx5_flow_namespace *ns = &root_ns->ns;
1518 struct fs_prio *prio;
1519 int start_level = 0;
1520
1521 fs_for_each_prio(prio, ns) {
1522 set_prio_attrs_in_prio(prio, start_level);
1523 start_level += prio->max_ft;
1524 }
1525 }
1526
1527 #define ANCHOR_PRIO 0
1528 #define ANCHOR_SIZE 1
1529 static int create_anchor_flow_table(struct mlx5_core_dev
1530 *dev)
1531 {
1532 struct mlx5_flow_namespace *ns = NULL;
1533 struct mlx5_flow_table *ft;
1534
1535 ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_ANCHOR);
1536 if (!ns)
1537 return -EINVAL;
1538 ft = mlx5_create_flow_table(ns, ANCHOR_PRIO, ANCHOR_SIZE);
1539 if (IS_ERR(ft)) {
1540 mlx5_core_err(dev, "Failed to create last anchor flow table");
1541 return PTR_ERR(ft);
1542 }
1543 return 0;
1544 }
1545
1546 static int init_root_ns(struct mlx5_core_dev *dev)
1547 {
1548
1549 dev->priv.root_ns = create_root_ns(dev, FS_FT_NIC_RX);
1550 if (IS_ERR_OR_NULL(dev->priv.root_ns))
1551 goto cleanup;
1552
1553 if (init_root_tree(dev, &root_fs, &dev->priv.root_ns->ns.node))
1554 goto cleanup;
1555
1556 set_prio_attrs(dev->priv.root_ns);
1557
1558 if (create_anchor_flow_table(dev))
1559 goto cleanup;
1560
1561 return 0;
1562
1563 cleanup:
1564 mlx5_cleanup_fs(dev);
1565 return -ENOMEM;
1566 }
1567
1568 static void cleanup_single_prio_root_ns(struct mlx5_core_dev *dev,
1569 struct mlx5_flow_root_namespace *root_ns)
1570 {
1571 struct fs_node *prio;
1572
1573 if (!root_ns)
1574 return;
1575
1576 if (!list_empty(&root_ns->ns.node.children)) {
1577 prio = list_first_entry(&root_ns->ns.node.children,
1578 struct fs_node,
1579 list);
1580 if (tree_remove_node(prio))
1581 mlx5_core_warn(dev,
1582 "Flow steering priority wasn't destroyed, refcount > 1\n");
1583 }
1584 if (tree_remove_node(&root_ns->ns.node))
1585 mlx5_core_warn(dev,
1586 "Flow steering namespace wasn't destroyed, refcount > 1\n");
1587 root_ns = NULL;
1588 }
1589
1590 static void destroy_flow_tables(struct fs_prio *prio)
1591 {
1592 struct mlx5_flow_table *iter;
1593 struct mlx5_flow_table *tmp;
1594
1595 fs_for_each_ft_safe(iter, tmp, prio)
1596 mlx5_destroy_flow_table(iter);
1597 }
1598
1599 static void cleanup_root_ns(struct mlx5_core_dev *dev)
1600 {
1601 struct mlx5_flow_root_namespace *root_ns = dev->priv.root_ns;
1602 struct fs_prio *iter_prio;
1603
1604 if (!MLX5_CAP_GEN(dev, nic_flow_table))
1605 return;
1606
1607 if (!root_ns)
1608 return;
1609
1610 /* stage 1 */
1611 fs_for_each_prio(iter_prio, &root_ns->ns) {
1612 struct fs_node *node;
1613 struct mlx5_flow_namespace *iter_ns;
1614
1615 fs_for_each_ns_or_ft(node, iter_prio) {
1616 if (node->type == FS_TYPE_FLOW_TABLE)
1617 continue;
1618 fs_get_obj(iter_ns, node);
1619 while (!list_empty(&iter_ns->node.children)) {
1620 struct fs_prio *obj_iter_prio2;
1621 struct fs_node *iter_prio2 =
1622 list_first_entry(&iter_ns->node.children,
1623 struct fs_node,
1624 list);
1625
1626 fs_get_obj(obj_iter_prio2, iter_prio2);
1627 destroy_flow_tables(obj_iter_prio2);
1628 if (tree_remove_node(iter_prio2)) {
1629 mlx5_core_warn(dev,
1630 "Priority %d wasn't destroyed, refcount > 1\n",
1631 obj_iter_prio2->prio);
1632 return;
1633 }
1634 }
1635 }
1636 }
1637
1638 /* stage 2 */
1639 fs_for_each_prio(iter_prio, &root_ns->ns) {
1640 while (!list_empty(&iter_prio->node.children)) {
1641 struct fs_node *iter_ns =
1642 list_first_entry(&iter_prio->node.children,
1643 struct fs_node,
1644 list);
1645 if (tree_remove_node(iter_ns)) {
1646 mlx5_core_warn(dev,
1647 "Namespace wasn't destroyed, refcount > 1\n");
1648 return;
1649 }
1650 }
1651 }
1652
1653 /* stage 3 */
1654 while (!list_empty(&root_ns->ns.node.children)) {
1655 struct fs_prio *obj_prio_node;
1656 struct fs_node *prio_node =
1657 list_first_entry(&root_ns->ns.node.children,
1658 struct fs_node,
1659 list);
1660
1661 fs_get_obj(obj_prio_node, prio_node);
1662 if (tree_remove_node(prio_node)) {
1663 mlx5_core_warn(dev,
1664 "Priority %d wasn't destroyed, refcount > 1\n",
1665 obj_prio_node->prio);
1666 return;
1667 }
1668 }
1669
1670 if (tree_remove_node(&root_ns->ns.node)) {
1671 mlx5_core_warn(dev,
1672 "root namespace wasn't destroyed, refcount > 1\n");
1673 return;
1674 }
1675
1676 dev->priv.root_ns = NULL;
1677 }
1678
1679 void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
1680 {
1681 cleanup_root_ns(dev);
1682 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1683 }
1684
1685 static int init_fdb_root_ns(struct mlx5_core_dev *dev)
1686 {
1687 struct fs_prio *prio;
1688
1689 dev->priv.fdb_root_ns = create_root_ns(dev, FS_FT_FDB);
1690 if (!dev->priv.fdb_root_ns)
1691 return -ENOMEM;
1692
1693 /* Create single prio */
1694 prio = fs_create_prio(&dev->priv.fdb_root_ns->ns, 0, 1);
1695 if (IS_ERR(prio)) {
1696 cleanup_single_prio_root_ns(dev, dev->priv.fdb_root_ns);
1697 return PTR_ERR(prio);
1698 } else {
1699 return 0;
1700 }
1701 }
1702
1703 int mlx5_init_fs(struct mlx5_core_dev *dev)
1704 {
1705 int err = 0;
1706
1707 if (MLX5_CAP_GEN(dev, nic_flow_table)) {
1708 err = init_root_ns(dev);
1709 if (err)
1710 return err;
1711 }
1712 if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
1713 err = init_fdb_root_ns(dev);
1714 if (err)
1715 cleanup_root_ns(dev);
1716 }
1717
1718 return err;
1719 }
This page took 0.066074 seconds and 5 git commands to generate.