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