1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/mutex.h>
34#include <linux/mlx5/driver.h>
35#include <linux/mlx5/vport.h>
36#include <linux/mlx5/eswitch.h>
37
38#include "mlx5_core.h"
39#include "fs_core.h"
40#include "fs_cmd.h"
41#include "fs_ft_pool.h"
42#include "diag/fs_tracepoint.h"
43#include "accel/ipsec.h"
44#include "fpga/ipsec.h"
45
46#define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
47 sizeof(struct init_tree_node))
48
49#define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
50 ...) {.type = FS_TYPE_PRIO,\
51 .min_ft_level = min_level_val,\
52 .num_levels = num_levels_val,\
53 .num_leaf_prios = num_prios_val,\
54 .caps = caps_val,\
55 .children = (struct init_tree_node[]) {__VA_ARGS__},\
56 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
57}
58
59#define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
60 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
61 __VA_ARGS__)\
62
63#define ADD_NS(def_miss_act, ...) {.type = FS_TYPE_NAMESPACE, \
64 .def_miss_action = def_miss_act,\
65 .children = (struct init_tree_node[]) {__VA_ARGS__},\
66 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
67}
68
69#define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
70 sizeof(long))
71
72#define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
73
74#define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
75 .caps = (long[]) {__VA_ARGS__} }
76
77#define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
78 FS_CAP(flow_table_properties_nic_receive.modify_root), \
79 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
80 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
81
82#define FS_CHAINING_CAPS_EGRESS \
83 FS_REQUIRED_CAPS( \
84 FS_CAP(flow_table_properties_nic_transmit.flow_modify_en), \
85 FS_CAP(flow_table_properties_nic_transmit.modify_root), \
86 FS_CAP(flow_table_properties_nic_transmit \
87 .identified_miss_table_mode), \
88 FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
89
90#define FS_CHAINING_CAPS_RDMA_TX \
91 FS_REQUIRED_CAPS( \
92 FS_CAP(flow_table_properties_nic_transmit_rdma.flow_modify_en), \
93 FS_CAP(flow_table_properties_nic_transmit_rdma.modify_root), \
94 FS_CAP(flow_table_properties_nic_transmit_rdma \
95 .identified_miss_table_mode), \
96 FS_CAP(flow_table_properties_nic_transmit_rdma \
97 .flow_table_modify))
98
99#define LEFTOVERS_NUM_LEVELS 1
100#define LEFTOVERS_NUM_PRIOS 1
101
102#define BY_PASS_PRIO_NUM_LEVELS 1
103#define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
104 LEFTOVERS_NUM_PRIOS)
105
106#define ETHTOOL_PRIO_NUM_LEVELS 1
107#define ETHTOOL_NUM_PRIOS 11
108#define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
109
110#define KERNEL_NIC_PRIO_NUM_LEVELS 7
111#define KERNEL_NIC_NUM_PRIOS 1
112
113#define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
114
115#define KERNEL_NIC_TC_NUM_PRIOS 1
116#define KERNEL_NIC_TC_NUM_LEVELS 2
117
118#define ANCHOR_NUM_LEVELS 1
119#define ANCHOR_NUM_PRIOS 1
120#define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
121
122#define OFFLOADS_MAX_FT 2
123#define OFFLOADS_NUM_PRIOS 2
124#define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + OFFLOADS_NUM_PRIOS)
125
126#define LAG_PRIO_NUM_LEVELS 1
127#define LAG_NUM_PRIOS 1
128#define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
129
130#define KERNEL_TX_IPSEC_NUM_PRIOS 1
131#define KERNEL_TX_IPSEC_NUM_LEVELS 1
132#define KERNEL_TX_MIN_LEVEL (KERNEL_TX_IPSEC_NUM_LEVELS)
133
134struct node_caps {
135 size_t arr_sz;
136 long *caps;
137};
138
139static struct init_tree_node {
140 enum fs_node_type type;
141 struct init_tree_node *children;
142 int ar_size;
143 struct node_caps caps;
144 int min_ft_level;
145 int num_leaf_prios;
146 int prio;
147 int num_levels;
148 enum mlx5_flow_table_miss_action def_miss_action;
149} root_fs = {
150 .type = FS_TYPE_NAMESPACE,
151 .ar_size = 7,
152 .children = (struct init_tree_node[]){
153 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
154 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
155 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
156 BY_PASS_PRIO_NUM_LEVELS))),
157 ADD_PRIO(0, LAG_MIN_LEVEL, 0, FS_CHAINING_CAPS,
158 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
159 ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
160 LAG_PRIO_NUM_LEVELS))),
161 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
162 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
163 ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS,
164 OFFLOADS_MAX_FT))),
165 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0, FS_CHAINING_CAPS,
166 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
167 ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
168 ETHTOOL_PRIO_NUM_LEVELS))),
169 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
170 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
171 ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS,
172 KERNEL_NIC_TC_NUM_LEVELS),
173 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
174 KERNEL_NIC_PRIO_NUM_LEVELS))),
175 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
176 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
177 ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS,
178 LEFTOVERS_NUM_LEVELS))),
179 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
180 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
181 ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS,
182 ANCHOR_NUM_LEVELS))),
183 }
184};
185
186static struct init_tree_node egress_root_fs = {
187 .type = FS_TYPE_NAMESPACE,
188#ifdef CONFIG_MLX5_IPSEC
189 .ar_size = 2,
190#else
191 .ar_size = 1,
192#endif
193 .children = (struct init_tree_node[]) {
194 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
195 FS_CHAINING_CAPS_EGRESS,
196 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
197 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
198 BY_PASS_PRIO_NUM_LEVELS))),
199#ifdef CONFIG_MLX5_IPSEC
200 ADD_PRIO(0, KERNEL_TX_MIN_LEVEL, 0,
201 FS_CHAINING_CAPS_EGRESS,
202 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
203 ADD_MULTIPLE_PRIO(KERNEL_TX_IPSEC_NUM_PRIOS,
204 KERNEL_TX_IPSEC_NUM_LEVELS))),
205#endif
206 }
207};
208
209#define RDMA_RX_BYPASS_PRIO 0
210#define RDMA_RX_KERNEL_PRIO 1
211static struct init_tree_node rdma_rx_root_fs = {
212 .type = FS_TYPE_NAMESPACE,
213 .ar_size = 2,
214 .children = (struct init_tree_node[]) {
215 [RDMA_RX_BYPASS_PRIO] =
216 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS, 0,
217 FS_CHAINING_CAPS,
218 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
219 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_REGULAR_PRIOS,
220 BY_PASS_PRIO_NUM_LEVELS))),
221 [RDMA_RX_KERNEL_PRIO] =
222 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS + 1, 0,
223 FS_CHAINING_CAPS,
224 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_SWITCH_DOMAIN,
225 ADD_MULTIPLE_PRIO(1, 1))),
226 }
227};
228
229static struct init_tree_node rdma_tx_root_fs = {
230 .type = FS_TYPE_NAMESPACE,
231 .ar_size = 1,
232 .children = (struct init_tree_node[]) {
233 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
234 FS_CHAINING_CAPS_RDMA_TX,
235 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
236 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
237 BY_PASS_PRIO_NUM_LEVELS))),
238 }
239};
240
241enum fs_i_lock_class {
242 FS_LOCK_GRANDPARENT,
243 FS_LOCK_PARENT,
244 FS_LOCK_CHILD
245};
246
247static const struct rhashtable_params rhash_fte = {
248 .key_len = sizeof_field(struct fs_fte, val),
249 .key_offset = offsetof(struct fs_fte, val),
250 .head_offset = offsetof(struct fs_fte, hash),
251 .automatic_shrinking = true,
252 .min_size = 1,
253};
254
255static const struct rhashtable_params rhash_fg = {
256 .key_len = sizeof_field(struct mlx5_flow_group, mask),
257 .key_offset = offsetof(struct mlx5_flow_group, mask),
258 .head_offset = offsetof(struct mlx5_flow_group, hash),
259 .automatic_shrinking = true,
260 .min_size = 1,
261
262};
263
264static void del_hw_flow_table(struct fs_node *node);
265static void del_hw_flow_group(struct fs_node *node);
266static void del_hw_fte(struct fs_node *node);
267static void del_sw_flow_table(struct fs_node *node);
268static void del_sw_flow_group(struct fs_node *node);
269static void del_sw_fte(struct fs_node *node);
270static void del_sw_prio(struct fs_node *node);
271static void del_sw_ns(struct fs_node *node);
272
273
274
275static void del_sw_hw_rule(struct fs_node *node);
276static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
277 struct mlx5_flow_destination *d2);
278static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
279static struct mlx5_flow_rule *
280find_flow_rule(struct fs_fte *fte,
281 struct mlx5_flow_destination *dest);
282
283static void tree_init_node(struct fs_node *node,
284 void (*del_hw_func)(struct fs_node *),
285 void (*del_sw_func)(struct fs_node *))
286{
287 refcount_set(&node->refcount, 1);
288 INIT_LIST_HEAD(&node->list);
289 INIT_LIST_HEAD(&node->children);
290 init_rwsem(&node->lock);
291 node->del_hw_func = del_hw_func;
292 node->del_sw_func = del_sw_func;
293 node->active = false;
294}
295
296static void tree_add_node(struct fs_node *node, struct fs_node *parent)
297{
298 if (parent)
299 refcount_inc(&parent->refcount);
300 node->parent = parent;
301
302
303 if (!parent)
304 node->root = node;
305 else
306 node->root = parent->root;
307}
308
309static int tree_get_node(struct fs_node *node)
310{
311 return refcount_inc_not_zero(&node->refcount);
312}
313
314static void nested_down_read_ref_node(struct fs_node *node,
315 enum fs_i_lock_class class)
316{
317 if (node) {
318 down_read_nested(&node->lock, class);
319 refcount_inc(&node->refcount);
320 }
321}
322
323static void nested_down_write_ref_node(struct fs_node *node,
324 enum fs_i_lock_class class)
325{
326 if (node) {
327 down_write_nested(&node->lock, class);
328 refcount_inc(&node->refcount);
329 }
330}
331
332static void down_write_ref_node(struct fs_node *node, bool locked)
333{
334 if (node) {
335 if (!locked)
336 down_write(&node->lock);
337 refcount_inc(&node->refcount);
338 }
339}
340
341static void up_read_ref_node(struct fs_node *node)
342{
343 refcount_dec(&node->refcount);
344 up_read(&node->lock);
345}
346
347static void up_write_ref_node(struct fs_node *node, bool locked)
348{
349 refcount_dec(&node->refcount);
350 if (!locked)
351 up_write(&node->lock);
352}
353
354static void tree_put_node(struct fs_node *node, bool locked)
355{
356 struct fs_node *parent_node = node->parent;
357
358 if (refcount_dec_and_test(&node->refcount)) {
359 if (node->del_hw_func)
360 node->del_hw_func(node);
361 if (parent_node) {
362 down_write_ref_node(parent_node, locked);
363 list_del_init(&node->list);
364 }
365 node->del_sw_func(node);
366 if (parent_node)
367 up_write_ref_node(parent_node, locked);
368 node = NULL;
369 }
370 if (!node && parent_node)
371 tree_put_node(parent_node, locked);
372}
373
374static int tree_remove_node(struct fs_node *node, bool locked)
375{
376 if (refcount_read(&node->refcount) > 1) {
377 refcount_dec(&node->refcount);
378 return -EEXIST;
379 }
380 tree_put_node(node, locked);
381 return 0;
382}
383
384static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
385 unsigned int prio)
386{
387 struct fs_prio *iter_prio;
388
389 fs_for_each_prio(iter_prio, ns) {
390 if (iter_prio->prio == prio)
391 return iter_prio;
392 }
393
394 return NULL;
395}
396
397static bool is_fwd_next_action(u32 action)
398{
399 return action & (MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
400 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
401}
402
403static bool check_valid_spec(const struct mlx5_flow_spec *spec)
404{
405 int i;
406
407 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
408 if (spec->match_value[i] & ~spec->match_criteria[i]) {
409 pr_warn("mlx5_core: match_value differs from match_criteria\n");
410 return false;
411 }
412
413 return true;
414}
415
416static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
417{
418 struct fs_node *root;
419 struct mlx5_flow_namespace *ns;
420
421 root = node->root;
422
423 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
424 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
425 return NULL;
426 }
427
428 ns = container_of(root, struct mlx5_flow_namespace, node);
429 return container_of(ns, struct mlx5_flow_root_namespace, ns);
430}
431
432static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
433{
434 struct mlx5_flow_root_namespace *root = find_root(node);
435
436 if (root)
437 return root->dev->priv.steering;
438 return NULL;
439}
440
441static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
442{
443 struct mlx5_flow_root_namespace *root = find_root(node);
444
445 if (root)
446 return root->dev;
447 return NULL;
448}
449
450static void del_sw_ns(struct fs_node *node)
451{
452 kfree(node);
453}
454
455static void del_sw_prio(struct fs_node *node)
456{
457 kfree(node);
458}
459
460static void del_hw_flow_table(struct fs_node *node)
461{
462 struct mlx5_flow_root_namespace *root;
463 struct mlx5_flow_table *ft;
464 struct mlx5_core_dev *dev;
465 int err;
466
467 fs_get_obj(ft, node);
468 dev = get_dev(&ft->node);
469 root = find_root(&ft->node);
470 trace_mlx5_fs_del_ft(ft);
471
472 if (node->active) {
473 err = root->cmds->destroy_flow_table(root, ft);
474 if (err)
475 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
476 }
477}
478
479static void del_sw_flow_table(struct fs_node *node)
480{
481 struct mlx5_flow_table *ft;
482 struct fs_prio *prio;
483
484 fs_get_obj(ft, node);
485
486 rhltable_destroy(&ft->fgs_hash);
487 if (ft->node.parent) {
488 fs_get_obj(prio, ft->node.parent);
489 prio->num_ft--;
490 }
491 kfree(ft);
492}
493
494static void modify_fte(struct fs_fte *fte)
495{
496 struct mlx5_flow_root_namespace *root;
497 struct mlx5_flow_table *ft;
498 struct mlx5_flow_group *fg;
499 struct mlx5_core_dev *dev;
500 int err;
501
502 fs_get_obj(fg, fte->node.parent);
503 fs_get_obj(ft, fg->node.parent);
504 dev = get_dev(&fte->node);
505
506 root = find_root(&ft->node);
507 err = root->cmds->update_fte(root, ft, fg, fte->modify_mask, fte);
508 if (err)
509 mlx5_core_warn(dev,
510 "%s can't del rule fg id=%d fte_index=%d\n",
511 __func__, fg->id, fte->index);
512 fte->modify_mask = 0;
513}
514
515static void del_sw_hw_rule(struct fs_node *node)
516{
517 struct mlx5_flow_rule *rule;
518 struct fs_fte *fte;
519
520 fs_get_obj(rule, node);
521 fs_get_obj(fte, rule->node.parent);
522 trace_mlx5_fs_del_rule(rule);
523 if (is_fwd_next_action(rule->sw_action)) {
524 mutex_lock(&rule->dest_attr.ft->lock);
525 list_del(&rule->next_ft);
526 mutex_unlock(&rule->dest_attr.ft->lock);
527 }
528
529 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
530 --fte->dests_size) {
531 fte->modify_mask |=
532 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
533 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
534 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
535 goto out;
536 }
537
538 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_PORT &&
539 --fte->dests_size) {
540 fte->modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
541 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_ALLOW;
542 goto out;
543 }
544
545 if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
546 --fte->dests_size) {
547 fte->modify_mask |=
548 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
549 }
550out:
551 kfree(rule);
552}
553
554static void del_hw_fte(struct fs_node *node)
555{
556 struct mlx5_flow_root_namespace *root;
557 struct mlx5_flow_table *ft;
558 struct mlx5_flow_group *fg;
559 struct mlx5_core_dev *dev;
560 struct fs_fte *fte;
561 int err;
562
563 fs_get_obj(fte, node);
564 fs_get_obj(fg, fte->node.parent);
565 fs_get_obj(ft, fg->node.parent);
566
567 trace_mlx5_fs_del_fte(fte);
568 dev = get_dev(&ft->node);
569 root = find_root(&ft->node);
570 if (node->active) {
571 err = root->cmds->delete_fte(root, ft, fte);
572 if (err)
573 mlx5_core_warn(dev,
574 "flow steering can't delete fte in index %d of flow group id %d\n",
575 fte->index, fg->id);
576 node->active = false;
577 }
578}
579
580static void del_sw_fte(struct fs_node *node)
581{
582 struct mlx5_flow_steering *steering = get_steering(node);
583 struct mlx5_flow_group *fg;
584 struct fs_fte *fte;
585 int err;
586
587 fs_get_obj(fte, node);
588 fs_get_obj(fg, fte->node.parent);
589
590 err = rhashtable_remove_fast(&fg->ftes_hash,
591 &fte->hash,
592 rhash_fte);
593 WARN_ON(err);
594 ida_free(&fg->fte_allocator, fte->index - fg->start_index);
595 kmem_cache_free(steering->ftes_cache, fte);
596}
597
598static void del_hw_flow_group(struct fs_node *node)
599{
600 struct mlx5_flow_root_namespace *root;
601 struct mlx5_flow_group *fg;
602 struct mlx5_flow_table *ft;
603 struct mlx5_core_dev *dev;
604
605 fs_get_obj(fg, node);
606 fs_get_obj(ft, fg->node.parent);
607 dev = get_dev(&ft->node);
608 trace_mlx5_fs_del_fg(fg);
609
610 root = find_root(&ft->node);
611 if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
612 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
613 fg->id, ft->id);
614}
615
616static void del_sw_flow_group(struct fs_node *node)
617{
618 struct mlx5_flow_steering *steering = get_steering(node);
619 struct mlx5_flow_group *fg;
620 struct mlx5_flow_table *ft;
621 int err;
622
623 fs_get_obj(fg, node);
624 fs_get_obj(ft, fg->node.parent);
625
626 rhashtable_destroy(&fg->ftes_hash);
627 ida_destroy(&fg->fte_allocator);
628 if (ft->autogroup.active &&
629 fg->max_ftes == ft->autogroup.group_size &&
630 fg->start_index < ft->autogroup.max_fte)
631 ft->autogroup.num_groups--;
632 err = rhltable_remove(&ft->fgs_hash,
633 &fg->hash,
634 rhash_fg);
635 WARN_ON(err);
636 kmem_cache_free(steering->fgs_cache, fg);
637}
638
639static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
640{
641 int index;
642 int ret;
643
644 index = ida_alloc_max(&fg->fte_allocator, fg->max_ftes - 1, GFP_KERNEL);
645 if (index < 0)
646 return index;
647
648 fte->index = index + fg->start_index;
649 ret = rhashtable_insert_fast(&fg->ftes_hash,
650 &fte->hash,
651 rhash_fte);
652 if (ret)
653 goto err_ida_remove;
654
655 tree_add_node(&fte->node, &fg->node);
656 list_add_tail(&fte->node.list, &fg->node.children);
657 return 0;
658
659err_ida_remove:
660 ida_free(&fg->fte_allocator, index);
661 return ret;
662}
663
664static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
665 const struct mlx5_flow_spec *spec,
666 struct mlx5_flow_act *flow_act)
667{
668 struct mlx5_flow_steering *steering = get_steering(&ft->node);
669 struct fs_fte *fte;
670
671 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
672 if (!fte)
673 return ERR_PTR(-ENOMEM);
674
675 memcpy(fte->val, &spec->match_value, sizeof(fte->val));
676 fte->node.type = FS_TYPE_FLOW_ENTRY;
677 fte->action = *flow_act;
678 fte->flow_context = spec->flow_context;
679
680 tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
681
682 return fte;
683}
684
685static void dealloc_flow_group(struct mlx5_flow_steering *steering,
686 struct mlx5_flow_group *fg)
687{
688 rhashtable_destroy(&fg->ftes_hash);
689 kmem_cache_free(steering->fgs_cache, fg);
690}
691
692static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
693 u8 match_criteria_enable,
694 const void *match_criteria,
695 int start_index,
696 int end_index)
697{
698 struct mlx5_flow_group *fg;
699 int ret;
700
701 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
702 if (!fg)
703 return ERR_PTR(-ENOMEM);
704
705 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
706 if (ret) {
707 kmem_cache_free(steering->fgs_cache, fg);
708 return ERR_PTR(ret);
709 }
710
711 ida_init(&fg->fte_allocator);
712 fg->mask.match_criteria_enable = match_criteria_enable;
713 memcpy(&fg->mask.match_criteria, match_criteria,
714 sizeof(fg->mask.match_criteria));
715 fg->node.type = FS_TYPE_FLOW_GROUP;
716 fg->start_index = start_index;
717 fg->max_ftes = end_index - start_index + 1;
718
719 return fg;
720}
721
722static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
723 u8 match_criteria_enable,
724 const void *match_criteria,
725 int start_index,
726 int end_index,
727 struct list_head *prev)
728{
729 struct mlx5_flow_steering *steering = get_steering(&ft->node);
730 struct mlx5_flow_group *fg;
731 int ret;
732
733 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
734 start_index, end_index);
735 if (IS_ERR(fg))
736 return fg;
737
738
739 ret = rhltable_insert(&ft->fgs_hash,
740 &fg->hash,
741 rhash_fg);
742 if (ret) {
743 dealloc_flow_group(steering, fg);
744 return ERR_PTR(ret);
745 }
746
747 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
748 tree_add_node(&fg->node, &ft->node);
749
750 list_add(&fg->node.list, prev);
751 atomic_inc(&ft->node.version);
752
753 return fg;
754}
755
756static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport,
757 enum fs_flow_table_type table_type,
758 enum fs_flow_table_op_mod op_mod,
759 u32 flags)
760{
761 struct mlx5_flow_table *ft;
762 int ret;
763
764 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
765 if (!ft)
766 return ERR_PTR(-ENOMEM);
767
768 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
769 if (ret) {
770 kfree(ft);
771 return ERR_PTR(ret);
772 }
773
774 ft->level = level;
775 ft->node.type = FS_TYPE_FLOW_TABLE;
776 ft->op_mod = op_mod;
777 ft->type = table_type;
778 ft->vport = vport;
779 ft->flags = flags;
780 INIT_LIST_HEAD(&ft->fwd_rules);
781 mutex_init(&ft->lock);
782
783 return ft;
784}
785
786
787
788
789
790static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
791 struct list_head *start,
792 bool reverse)
793{
794#define list_advance_entry(pos, reverse) \
795 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
796
797#define list_for_each_advance_continue(pos, head, reverse) \
798 for (pos = list_advance_entry(pos, reverse); \
799 &pos->list != (head); \
800 pos = list_advance_entry(pos, reverse))
801
802 struct fs_node *iter = list_entry(start, struct fs_node, list);
803 struct mlx5_flow_table *ft = NULL;
804
805 if (!root || root->type == FS_TYPE_PRIO_CHAINS)
806 return NULL;
807
808 list_for_each_advance_continue(iter, &root->children, reverse) {
809 if (iter->type == FS_TYPE_FLOW_TABLE) {
810 fs_get_obj(ft, iter);
811 return ft;
812 }
813 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
814 if (ft)
815 return ft;
816 }
817
818 return ft;
819}
820
821
822
823
824
825static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
826{
827 struct mlx5_flow_table *ft = NULL;
828 struct fs_node *curr_node;
829 struct fs_node *parent;
830
831 parent = prio->node.parent;
832 curr_node = &prio->node;
833 while (!ft && parent) {
834 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
835 curr_node = parent;
836 parent = curr_node->parent;
837 }
838 return ft;
839}
840
841
842static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
843{
844 return find_closest_ft(prio, false);
845}
846
847
848static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
849{
850 return find_closest_ft(prio, true);
851}
852
853static struct mlx5_flow_table *find_next_fwd_ft(struct mlx5_flow_table *ft,
854 struct mlx5_flow_act *flow_act)
855{
856 struct fs_prio *prio;
857 bool next_ns;
858
859 next_ns = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS;
860 fs_get_obj(prio, next_ns ? ft->ns->node.parent : ft->node.parent);
861
862 return find_next_chained_ft(prio);
863}
864
865static int connect_fts_in_prio(struct mlx5_core_dev *dev,
866 struct fs_prio *prio,
867 struct mlx5_flow_table *ft)
868{
869 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
870 struct mlx5_flow_table *iter;
871 int err;
872
873 fs_for_each_ft(iter, prio) {
874 err = root->cmds->modify_flow_table(root, iter, ft);
875 if (err) {
876 mlx5_core_err(dev,
877 "Failed to modify flow table id %d, type %d, err %d\n",
878 iter->id, iter->type, err);
879
880 return err;
881 }
882 }
883 return 0;
884}
885
886
887static int connect_prev_fts(struct mlx5_core_dev *dev,
888 struct mlx5_flow_table *ft,
889 struct fs_prio *prio)
890{
891 struct mlx5_flow_table *prev_ft;
892
893 prev_ft = find_prev_chained_ft(prio);
894 if (prev_ft) {
895 struct fs_prio *prev_prio;
896
897 fs_get_obj(prev_prio, prev_ft->node.parent);
898 return connect_fts_in_prio(dev, prev_prio, ft);
899 }
900 return 0;
901}
902
903static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
904 *prio)
905{
906 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
907 struct mlx5_ft_underlay_qp *uqp;
908 int min_level = INT_MAX;
909 int err = 0;
910 u32 qpn;
911
912 if (root->root_ft)
913 min_level = root->root_ft->level;
914
915 if (ft->level >= min_level)
916 return 0;
917
918 if (list_empty(&root->underlay_qpns)) {
919
920 qpn = 0;
921 err = root->cmds->update_root_ft(root, ft, qpn, false);
922 } else {
923 list_for_each_entry(uqp, &root->underlay_qpns, list) {
924 qpn = uqp->qpn;
925 err = root->cmds->update_root_ft(root, ft,
926 qpn, false);
927 if (err)
928 break;
929 }
930 }
931
932 if (err)
933 mlx5_core_warn(root->dev,
934 "Update root flow table of id(%u) qpn(%d) failed\n",
935 ft->id, qpn);
936 else
937 root->root_ft = ft;
938
939 return err;
940}
941
942static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
943 struct mlx5_flow_destination *dest)
944{
945 struct mlx5_flow_root_namespace *root;
946 struct mlx5_flow_table *ft;
947 struct mlx5_flow_group *fg;
948 struct fs_fte *fte;
949 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
950 int err = 0;
951
952 fs_get_obj(fte, rule->node.parent);
953 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
954 return -EINVAL;
955 down_write_ref_node(&fte->node, false);
956 fs_get_obj(fg, fte->node.parent);
957 fs_get_obj(ft, fg->node.parent);
958
959 memcpy(&rule->dest_attr, dest, sizeof(*dest));
960 root = find_root(&ft->node);
961 err = root->cmds->update_fte(root, ft, fg,
962 modify_mask, fte);
963 up_write_ref_node(&fte->node, false);
964
965 return err;
966}
967
968int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
969 struct mlx5_flow_destination *new_dest,
970 struct mlx5_flow_destination *old_dest)
971{
972 int i;
973
974 if (!old_dest) {
975 if (handle->num_rules != 1)
976 return -EINVAL;
977 return _mlx5_modify_rule_destination(handle->rule[0],
978 new_dest);
979 }
980
981 for (i = 0; i < handle->num_rules; i++) {
982 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
983 return _mlx5_modify_rule_destination(handle->rule[i],
984 new_dest);
985 }
986
987 return -EINVAL;
988}
989
990
991static int connect_fwd_rules(struct mlx5_core_dev *dev,
992 struct mlx5_flow_table *new_next_ft,
993 struct mlx5_flow_table *old_next_ft)
994{
995 struct mlx5_flow_destination dest = {};
996 struct mlx5_flow_rule *iter;
997 int err = 0;
998
999
1000
1001
1002 if (!new_next_ft || !old_next_ft)
1003 return 0;
1004
1005 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1006 dest.ft = new_next_ft;
1007
1008 mutex_lock(&old_next_ft->lock);
1009 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
1010 mutex_unlock(&old_next_ft->lock);
1011 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
1012 if ((iter->sw_action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS) &&
1013 iter->ft->ns == new_next_ft->ns)
1014 continue;
1015
1016 err = _mlx5_modify_rule_destination(iter, &dest);
1017 if (err)
1018 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
1019 new_next_ft->id);
1020 }
1021 return 0;
1022}
1023
1024static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
1025 struct fs_prio *prio)
1026{
1027 struct mlx5_flow_table *next_ft, *first_ft;
1028 int err = 0;
1029
1030
1031
1032 first_ft = list_first_entry_or_null(&prio->node.children,
1033 struct mlx5_flow_table, node.list);
1034 if (!first_ft || first_ft->level > ft->level) {
1035 err = connect_prev_fts(dev, ft, prio);
1036 if (err)
1037 return err;
1038
1039 next_ft = first_ft ? first_ft : find_next_chained_ft(prio);
1040 err = connect_fwd_rules(dev, ft, next_ft);
1041 if (err)
1042 return err;
1043 }
1044
1045 if (MLX5_CAP_FLOWTABLE(dev,
1046 flow_table_properties_nic_receive.modify_root))
1047 err = update_root_ft_create(ft, prio);
1048 return err;
1049}
1050
1051static void list_add_flow_table(struct mlx5_flow_table *ft,
1052 struct fs_prio *prio)
1053{
1054 struct list_head *prev = &prio->node.children;
1055 struct mlx5_flow_table *iter;
1056
1057 fs_for_each_ft(iter, prio) {
1058 if (iter->level > ft->level)
1059 break;
1060 prev = &iter->node.list;
1061 }
1062 list_add(&ft->node.list, prev);
1063}
1064
1065static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1066 struct mlx5_flow_table_attr *ft_attr,
1067 enum fs_flow_table_op_mod op_mod,
1068 u16 vport)
1069{
1070 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
1071 bool unmanaged = ft_attr->flags & MLX5_FLOW_TABLE_UNMANAGED;
1072 struct mlx5_flow_table *next_ft;
1073 struct fs_prio *fs_prio = NULL;
1074 struct mlx5_flow_table *ft;
1075 int err;
1076
1077 if (!root) {
1078 pr_err("mlx5: flow steering failed to find root of namespace\n");
1079 return ERR_PTR(-ENODEV);
1080 }
1081
1082 mutex_lock(&root->chain_lock);
1083 fs_prio = find_prio(ns, ft_attr->prio);
1084 if (!fs_prio) {
1085 err = -EINVAL;
1086 goto unlock_root;
1087 }
1088 if (!unmanaged) {
1089
1090
1091
1092 if (ft_attr->level >= fs_prio->num_levels) {
1093 err = -ENOSPC;
1094 goto unlock_root;
1095 }
1096
1097 ft_attr->level += fs_prio->start_level;
1098 }
1099
1100
1101
1102
1103 ft = alloc_flow_table(ft_attr->level,
1104 vport,
1105 root->table_type,
1106 op_mod, ft_attr->flags);
1107 if (IS_ERR(ft)) {
1108 err = PTR_ERR(ft);
1109 goto unlock_root;
1110 }
1111
1112 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1113 next_ft = unmanaged ? ft_attr->next_ft :
1114 find_next_chained_ft(fs_prio);
1115 ft->def_miss_action = ns->def_miss_action;
1116 ft->ns = ns;
1117 err = root->cmds->create_flow_table(root, ft, ft_attr->max_fte, next_ft);
1118 if (err)
1119 goto free_ft;
1120
1121 if (!unmanaged) {
1122 err = connect_flow_table(root->dev, ft, fs_prio);
1123 if (err)
1124 goto destroy_ft;
1125 }
1126
1127 ft->node.active = true;
1128 down_write_ref_node(&fs_prio->node, false);
1129 if (!unmanaged) {
1130 tree_add_node(&ft->node, &fs_prio->node);
1131 list_add_flow_table(ft, fs_prio);
1132 } else {
1133 ft->node.root = fs_prio->node.root;
1134 }
1135 fs_prio->num_ft++;
1136 up_write_ref_node(&fs_prio->node, false);
1137 mutex_unlock(&root->chain_lock);
1138 trace_mlx5_fs_add_ft(ft);
1139 return ft;
1140destroy_ft:
1141 root->cmds->destroy_flow_table(root, ft);
1142free_ft:
1143 rhltable_destroy(&ft->fgs_hash);
1144 kfree(ft);
1145unlock_root:
1146 mutex_unlock(&root->chain_lock);
1147 return ERR_PTR(err);
1148}
1149
1150struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1151 struct mlx5_flow_table_attr *ft_attr)
1152{
1153 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1154}
1155EXPORT_SYMBOL(mlx5_create_flow_table);
1156
1157struct mlx5_flow_table *
1158mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1159 struct mlx5_flow_table_attr *ft_attr, u16 vport)
1160{
1161 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1162}
1163
1164struct mlx5_flow_table*
1165mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1166 int prio, u32 level)
1167{
1168 struct mlx5_flow_table_attr ft_attr = {};
1169
1170 ft_attr.level = level;
1171 ft_attr.prio = prio;
1172 ft_attr.max_fte = 1;
1173
1174 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1175}
1176EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1177
1178#define MAX_FLOW_GROUP_SIZE BIT(24)
1179struct mlx5_flow_table*
1180mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1181 struct mlx5_flow_table_attr *ft_attr)
1182{
1183 int num_reserved_entries = ft_attr->autogroup.num_reserved_entries;
1184 int max_num_groups = ft_attr->autogroup.max_num_groups;
1185 struct mlx5_flow_table *ft;
1186 int autogroups_max_fte;
1187
1188 ft = mlx5_create_flow_table(ns, ft_attr);
1189 if (IS_ERR(ft))
1190 return ft;
1191
1192 autogroups_max_fte = ft->max_fte - num_reserved_entries;
1193 if (max_num_groups > autogroups_max_fte)
1194 goto err_validate;
1195 if (num_reserved_entries > ft->max_fte)
1196 goto err_validate;
1197
1198
1199 if (autogroups_max_fte / (max_num_groups + 1) > MAX_FLOW_GROUP_SIZE)
1200 max_num_groups = (autogroups_max_fte / MAX_FLOW_GROUP_SIZE) - 1;
1201
1202 ft->autogroup.active = true;
1203 ft->autogroup.required_groups = max_num_groups;
1204 ft->autogroup.max_fte = autogroups_max_fte;
1205
1206 ft->autogroup.group_size = autogroups_max_fte / (max_num_groups + 1);
1207
1208 return ft;
1209
1210err_validate:
1211 mlx5_destroy_flow_table(ft);
1212 return ERR_PTR(-ENOSPC);
1213}
1214EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1215
1216struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1217 u32 *fg_in)
1218{
1219 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1220 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1221 fg_in, match_criteria);
1222 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1223 fg_in,
1224 match_criteria_enable);
1225 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1226 start_flow_index);
1227 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1228 end_flow_index);
1229 struct mlx5_flow_group *fg;
1230 int err;
1231
1232 if (ft->autogroup.active && start_index < ft->autogroup.max_fte)
1233 return ERR_PTR(-EPERM);
1234
1235 down_write_ref_node(&ft->node, false);
1236 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1237 start_index, end_index,
1238 ft->node.children.prev);
1239 up_write_ref_node(&ft->node, false);
1240 if (IS_ERR(fg))
1241 return fg;
1242
1243 err = root->cmds->create_flow_group(root, ft, fg_in, fg);
1244 if (err) {
1245 tree_put_node(&fg->node, false);
1246 return ERR_PTR(err);
1247 }
1248 trace_mlx5_fs_add_fg(fg);
1249 fg->node.active = true;
1250
1251 return fg;
1252}
1253EXPORT_SYMBOL(mlx5_create_flow_group);
1254
1255static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1256{
1257 struct mlx5_flow_rule *rule;
1258
1259 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1260 if (!rule)
1261 return NULL;
1262
1263 INIT_LIST_HEAD(&rule->next_ft);
1264 rule->node.type = FS_TYPE_FLOW_DEST;
1265 if (dest)
1266 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1267
1268 return rule;
1269}
1270
1271static struct mlx5_flow_handle *alloc_handle(int num_rules)
1272{
1273 struct mlx5_flow_handle *handle;
1274
1275 handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
1276 if (!handle)
1277 return NULL;
1278
1279 handle->num_rules = num_rules;
1280
1281 return handle;
1282}
1283
1284static void destroy_flow_handle(struct fs_fte *fte,
1285 struct mlx5_flow_handle *handle,
1286 struct mlx5_flow_destination *dest,
1287 int i)
1288{
1289 for (; --i >= 0;) {
1290 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1291 fte->dests_size--;
1292 list_del(&handle->rule[i]->node.list);
1293 kfree(handle->rule[i]);
1294 }
1295 }
1296 kfree(handle);
1297}
1298
1299static struct mlx5_flow_handle *
1300create_flow_handle(struct fs_fte *fte,
1301 struct mlx5_flow_destination *dest,
1302 int dest_num,
1303 int *modify_mask,
1304 bool *new_rule)
1305{
1306 struct mlx5_flow_handle *handle;
1307 struct mlx5_flow_rule *rule = NULL;
1308 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1309 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1310 int type;
1311 int i = 0;
1312
1313 handle = alloc_handle((dest_num) ? dest_num : 1);
1314 if (!handle)
1315 return ERR_PTR(-ENOMEM);
1316
1317 do {
1318 if (dest) {
1319 rule = find_flow_rule(fte, dest + i);
1320 if (rule) {
1321 refcount_inc(&rule->node.refcount);
1322 goto rule_found;
1323 }
1324 }
1325
1326 *new_rule = true;
1327 rule = alloc_rule(dest + i);
1328 if (!rule)
1329 goto free_rules;
1330
1331
1332
1333
1334 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1335 if (dest &&
1336 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1337 list_add(&rule->node.list, &fte->node.children);
1338 else
1339 list_add_tail(&rule->node.list, &fte->node.children);
1340 if (dest) {
1341 fte->dests_size++;
1342
1343 type = dest[i].type ==
1344 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1345 *modify_mask |= type ? count : dst;
1346 }
1347rule_found:
1348 handle->rule[i] = rule;
1349 } while (++i < dest_num);
1350
1351 return handle;
1352
1353free_rules:
1354 destroy_flow_handle(fte, handle, dest, i);
1355 return ERR_PTR(-ENOMEM);
1356}
1357
1358
1359static struct mlx5_flow_handle *
1360add_rule_fte(struct fs_fte *fte,
1361 struct mlx5_flow_group *fg,
1362 struct mlx5_flow_destination *dest,
1363 int dest_num,
1364 bool update_action)
1365{
1366 struct mlx5_flow_root_namespace *root;
1367 struct mlx5_flow_handle *handle;
1368 struct mlx5_flow_table *ft;
1369 int modify_mask = 0;
1370 int err;
1371 bool new_rule = false;
1372
1373 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1374 &new_rule);
1375 if (IS_ERR(handle) || !new_rule)
1376 goto out;
1377
1378 if (update_action)
1379 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1380
1381 fs_get_obj(ft, fg->node.parent);
1382 root = find_root(&fg->node);
1383 if (!(fte->status & FS_FTE_STATUS_EXISTING))
1384 err = root->cmds->create_fte(root, ft, fg, fte);
1385 else
1386 err = root->cmds->update_fte(root, ft, fg, modify_mask, fte);
1387 if (err)
1388 goto free_handle;
1389
1390 fte->node.active = true;
1391 fte->status |= FS_FTE_STATUS_EXISTING;
1392 atomic_inc(&fg->node.version);
1393
1394out:
1395 return handle;
1396
1397free_handle:
1398 destroy_flow_handle(fte, handle, dest, handle->num_rules);
1399 return ERR_PTR(err);
1400}
1401
1402static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
1403 const struct mlx5_flow_spec *spec)
1404{
1405 struct list_head *prev = &ft->node.children;
1406 u32 max_fte = ft->autogroup.max_fte;
1407 unsigned int candidate_index = 0;
1408 unsigned int group_size = 0;
1409 struct mlx5_flow_group *fg;
1410
1411 if (!ft->autogroup.active)
1412 return ERR_PTR(-ENOENT);
1413
1414 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1415 group_size = ft->autogroup.group_size;
1416
1417
1418 if (group_size == 0)
1419 group_size = 1;
1420
1421
1422 fs_for_each_fg(fg, ft) {
1423 if (candidate_index + group_size > fg->start_index)
1424 candidate_index = fg->start_index + fg->max_ftes;
1425 else
1426 break;
1427 prev = &fg->node.list;
1428 }
1429
1430 if (candidate_index + group_size > max_fte)
1431 return ERR_PTR(-ENOSPC);
1432
1433 fg = alloc_insert_flow_group(ft,
1434 spec->match_criteria_enable,
1435 spec->match_criteria,
1436 candidate_index,
1437 candidate_index + group_size - 1,
1438 prev);
1439 if (IS_ERR(fg))
1440 goto out;
1441
1442 if (group_size == ft->autogroup.group_size)
1443 ft->autogroup.num_groups++;
1444
1445out:
1446 return fg;
1447}
1448
1449static int create_auto_flow_group(struct mlx5_flow_table *ft,
1450 struct mlx5_flow_group *fg)
1451{
1452 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1453 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1454 void *match_criteria_addr;
1455 u8 src_esw_owner_mask_on;
1456 void *misc;
1457 int err;
1458 u32 *in;
1459
1460 in = kvzalloc(inlen, GFP_KERNEL);
1461 if (!in)
1462 return -ENOMEM;
1463
1464 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1465 fg->mask.match_criteria_enable);
1466 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1467 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1468 fg->max_ftes - 1);
1469
1470 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1471 misc_parameters);
1472 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1473 source_eswitch_owner_vhca_id);
1474 MLX5_SET(create_flow_group_in, in,
1475 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1476
1477 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1478 in, match_criteria);
1479 memcpy(match_criteria_addr, fg->mask.match_criteria,
1480 sizeof(fg->mask.match_criteria));
1481
1482 err = root->cmds->create_flow_group(root, ft, in, fg);
1483 if (!err) {
1484 fg->node.active = true;
1485 trace_mlx5_fs_add_fg(fg);
1486 }
1487
1488 kvfree(in);
1489 return err;
1490}
1491
1492static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1493 struct mlx5_flow_destination *d2)
1494{
1495 if (d1->type == d2->type) {
1496 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1497 d1->vport.num == d2->vport.num &&
1498 d1->vport.flags == d2->vport.flags &&
1499 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ?
1500 (d1->vport.vhca_id == d2->vport.vhca_id) : true) &&
1501 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
1502 (d1->vport.pkt_reformat->id ==
1503 d2->vport.pkt_reformat->id) : true)) ||
1504 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1505 d1->ft == d2->ft) ||
1506 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1507 d1->tir_num == d2->tir_num) ||
1508 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1509 d1->ft_num == d2->ft_num) ||
1510 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER &&
1511 d1->sampler_id == d2->sampler_id))
1512 return true;
1513 }
1514
1515 return false;
1516}
1517
1518static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1519 struct mlx5_flow_destination *dest)
1520{
1521 struct mlx5_flow_rule *rule;
1522
1523 list_for_each_entry(rule, &fte->node.children, node.list) {
1524 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1525 return rule;
1526 }
1527 return NULL;
1528}
1529
1530static bool check_conflicting_actions(u32 action1, u32 action2)
1531{
1532 u32 xored_actions = action1 ^ action2;
1533
1534
1535 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1536 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1537 return false;
1538
1539 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1540 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
1541 MLX5_FLOW_CONTEXT_ACTION_DECAP |
1542 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1543 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1544 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1545 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1546 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
1547 return true;
1548
1549 return false;
1550}
1551
1552static int check_conflicting_ftes(struct fs_fte *fte,
1553 const struct mlx5_flow_context *flow_context,
1554 const struct mlx5_flow_act *flow_act)
1555{
1556 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
1557 mlx5_core_warn(get_dev(&fte->node),
1558 "Found two FTEs with conflicting actions\n");
1559 return -EEXIST;
1560 }
1561
1562 if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) &&
1563 fte->flow_context.flow_tag != flow_context->flow_tag) {
1564 mlx5_core_warn(get_dev(&fte->node),
1565 "FTE flow tag %u already exists with different flow tag %u\n",
1566 fte->flow_context.flow_tag,
1567 flow_context->flow_tag);
1568 return -EEXIST;
1569 }
1570
1571 return 0;
1572}
1573
1574static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1575 const struct mlx5_flow_spec *spec,
1576 struct mlx5_flow_act *flow_act,
1577 struct mlx5_flow_destination *dest,
1578 int dest_num,
1579 struct fs_fte *fte)
1580{
1581 struct mlx5_flow_handle *handle;
1582 int old_action;
1583 int i;
1584 int ret;
1585
1586 ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act);
1587 if (ret)
1588 return ERR_PTR(ret);
1589
1590 old_action = fte->action.action;
1591 fte->action.action |= flow_act->action;
1592 handle = add_rule_fte(fte, fg, dest, dest_num,
1593 old_action != flow_act->action);
1594 if (IS_ERR(handle)) {
1595 fte->action.action = old_action;
1596 return handle;
1597 }
1598 trace_mlx5_fs_set_fte(fte, false);
1599
1600 for (i = 0; i < handle->num_rules; i++) {
1601 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
1602 tree_add_node(&handle->rule[i]->node, &fte->node);
1603 trace_mlx5_fs_add_rule(handle->rule[i]);
1604 }
1605 }
1606 return handle;
1607}
1608
1609static bool counter_is_valid(u32 action)
1610{
1611 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1612 MLX5_FLOW_CONTEXT_ACTION_ALLOW |
1613 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1614}
1615
1616static bool dest_is_valid(struct mlx5_flow_destination *dest,
1617 struct mlx5_flow_act *flow_act,
1618 struct mlx5_flow_table *ft)
1619{
1620 bool ignore_level = flow_act->flags & FLOW_ACT_IGNORE_FLOW_LEVEL;
1621 u32 action = flow_act->action;
1622
1623 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1624 return counter_is_valid(action);
1625
1626 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1627 return true;
1628
1629 if (ignore_level) {
1630 if (ft->type != FS_FT_FDB &&
1631 ft->type != FS_FT_NIC_RX)
1632 return false;
1633
1634 if (dest->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1635 ft->type != dest->ft->type)
1636 return false;
1637 }
1638
1639 if (!dest || ((dest->type ==
1640 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1641 (dest->ft->level <= ft->level && !ignore_level)))
1642 return false;
1643 return true;
1644}
1645
1646struct match_list {
1647 struct list_head list;
1648 struct mlx5_flow_group *g;
1649};
1650
1651static void free_match_list(struct match_list *head, bool ft_locked)
1652{
1653 struct match_list *iter, *match_tmp;
1654
1655 list_for_each_entry_safe(iter, match_tmp, &head->list,
1656 list) {
1657 tree_put_node(&iter->g->node, ft_locked);
1658 list_del(&iter->list);
1659 kfree(iter);
1660 }
1661}
1662
1663static int build_match_list(struct match_list *match_head,
1664 struct mlx5_flow_table *ft,
1665 const struct mlx5_flow_spec *spec,
1666 bool ft_locked)
1667{
1668 struct rhlist_head *tmp, *list;
1669 struct mlx5_flow_group *g;
1670 int err = 0;
1671
1672 rcu_read_lock();
1673 INIT_LIST_HEAD(&match_head->list);
1674
1675 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
1676
1677 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1678 struct match_list *curr_match;
1679
1680 if (unlikely(!tree_get_node(&g->node)))
1681 continue;
1682
1683 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
1684 if (!curr_match) {
1685 free_match_list(match_head, ft_locked);
1686 err = -ENOMEM;
1687 goto out;
1688 }
1689 curr_match->g = g;
1690 list_add_tail(&curr_match->list, &match_head->list);
1691 }
1692out:
1693 rcu_read_unlock();
1694 return err;
1695}
1696
1697static u64 matched_fgs_get_version(struct list_head *match_head)
1698{
1699 struct match_list *iter;
1700 u64 version = 0;
1701
1702 list_for_each_entry(iter, match_head, list)
1703 version += (u64)atomic_read(&iter->g->node.version);
1704 return version;
1705}
1706
1707static struct fs_fte *
1708lookup_fte_locked(struct mlx5_flow_group *g,
1709 const u32 *match_value,
1710 bool take_write)
1711{
1712 struct fs_fte *fte_tmp;
1713
1714 if (take_write)
1715 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1716 else
1717 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
1718 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
1719 rhash_fte);
1720 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1721 fte_tmp = NULL;
1722 goto out;
1723 }
1724 if (!fte_tmp->node.active) {
1725 tree_put_node(&fte_tmp->node, false);
1726 fte_tmp = NULL;
1727 goto out;
1728 }
1729
1730 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1731out:
1732 if (take_write)
1733 up_write_ref_node(&g->node, false);
1734 else
1735 up_read_ref_node(&g->node);
1736 return fte_tmp;
1737}
1738
1739static struct mlx5_flow_handle *
1740try_add_to_existing_fg(struct mlx5_flow_table *ft,
1741 struct list_head *match_head,
1742 const struct mlx5_flow_spec *spec,
1743 struct mlx5_flow_act *flow_act,
1744 struct mlx5_flow_destination *dest,
1745 int dest_num,
1746 int ft_version)
1747{
1748 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1749 struct mlx5_flow_group *g;
1750 struct mlx5_flow_handle *rule;
1751 struct match_list *iter;
1752 bool take_write = false;
1753 struct fs_fte *fte;
1754 u64 version = 0;
1755 int err;
1756
1757 fte = alloc_fte(ft, spec, flow_act);
1758 if (IS_ERR(fte))
1759 return ERR_PTR(-ENOMEM);
1760
1761search_again_locked:
1762 if (flow_act->flags & FLOW_ACT_NO_APPEND)
1763 goto skip_search;
1764 version = matched_fgs_get_version(match_head);
1765
1766
1767
1768 list_for_each_entry(iter, match_head, list) {
1769 struct fs_fte *fte_tmp;
1770
1771 g = iter->g;
1772 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1773 if (!fte_tmp)
1774 continue;
1775 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp);
1776
1777 up_write_ref_node(&fte_tmp->node, false);
1778 tree_put_node(&fte_tmp->node, false);
1779 kmem_cache_free(steering->ftes_cache, fte);
1780 return rule;
1781 }
1782
1783skip_search:
1784
1785
1786
1787
1788
1789
1790
1791 if (atomic_read(&ft->node.version) != ft_version) {
1792 rule = ERR_PTR(-EAGAIN);
1793 goto out;
1794 }
1795
1796
1797
1798
1799
1800 if (!(flow_act->flags & FLOW_ACT_NO_APPEND) &&
1801 version != matched_fgs_get_version(match_head)) {
1802 take_write = true;
1803 goto search_again_locked;
1804 }
1805
1806 list_for_each_entry(iter, match_head, list) {
1807 g = iter->g;
1808
1809 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1810
1811 if (!g->node.active) {
1812 up_write_ref_node(&g->node, false);
1813 continue;
1814 }
1815
1816 err = insert_fte(g, fte);
1817 if (err) {
1818 up_write_ref_node(&g->node, false);
1819 if (err == -ENOSPC)
1820 continue;
1821 kmem_cache_free(steering->ftes_cache, fte);
1822 return ERR_PTR(err);
1823 }
1824
1825 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1826 up_write_ref_node(&g->node, false);
1827 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
1828 up_write_ref_node(&fte->node, false);
1829 if (IS_ERR(rule))
1830 tree_put_node(&fte->node, false);
1831 return rule;
1832 }
1833 rule = ERR_PTR(-ENOENT);
1834out:
1835 kmem_cache_free(steering->ftes_cache, fte);
1836 return rule;
1837}
1838
1839static struct mlx5_flow_handle *
1840_mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1841 const struct mlx5_flow_spec *spec,
1842 struct mlx5_flow_act *flow_act,
1843 struct mlx5_flow_destination *dest,
1844 int dest_num)
1845
1846{
1847 struct mlx5_flow_steering *steering = get_steering(&ft->node);
1848 struct mlx5_flow_handle *rule;
1849 struct match_list match_head;
1850 struct mlx5_flow_group *g;
1851 bool take_write = false;
1852 struct fs_fte *fte;
1853 int version;
1854 int err;
1855 int i;
1856
1857 if (!check_valid_spec(spec))
1858 return ERR_PTR(-EINVAL);
1859
1860 for (i = 0; i < dest_num; i++) {
1861 if (!dest_is_valid(&dest[i], flow_act, ft))
1862 return ERR_PTR(-EINVAL);
1863 }
1864 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1865search_again_locked:
1866 version = atomic_read(&ft->node.version);
1867
1868
1869 err = build_match_list(&match_head, ft, spec, take_write);
1870 if (err) {
1871 if (take_write)
1872 up_write_ref_node(&ft->node, false);
1873 else
1874 up_read_ref_node(&ft->node);
1875 return ERR_PTR(err);
1876 }
1877
1878 if (!take_write)
1879 up_read_ref_node(&ft->node);
1880
1881 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1882 dest_num, version);
1883 free_match_list(&match_head, take_write);
1884 if (!IS_ERR(rule) ||
1885 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1886 if (take_write)
1887 up_write_ref_node(&ft->node, false);
1888 return rule;
1889 }
1890
1891 if (!take_write) {
1892 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1893 take_write = true;
1894 }
1895
1896 if (PTR_ERR(rule) == -EAGAIN ||
1897 version != atomic_read(&ft->node.version))
1898 goto search_again_locked;
1899
1900 g = alloc_auto_flow_group(ft, spec);
1901 if (IS_ERR(g)) {
1902 rule = ERR_CAST(g);
1903 up_write_ref_node(&ft->node, false);
1904 return rule;
1905 }
1906
1907 fte = alloc_fte(ft, spec, flow_act);
1908 if (IS_ERR(fte)) {
1909 up_write_ref_node(&ft->node, false);
1910 err = PTR_ERR(fte);
1911 goto err_alloc_fte;
1912 }
1913
1914 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1915 up_write_ref_node(&ft->node, false);
1916
1917 err = create_auto_flow_group(ft, g);
1918 if (err)
1919 goto err_release_fg;
1920
1921 err = insert_fte(g, fte);
1922 if (err)
1923 goto err_release_fg;
1924
1925 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
1926 up_write_ref_node(&g->node, false);
1927 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
1928 up_write_ref_node(&fte->node, false);
1929 if (IS_ERR(rule))
1930 tree_put_node(&fte->node, false);
1931 tree_put_node(&g->node, false);
1932 return rule;
1933
1934err_release_fg:
1935 up_write_ref_node(&g->node, false);
1936 kmem_cache_free(steering->ftes_cache, fte);
1937err_alloc_fte:
1938 tree_put_node(&g->node, false);
1939 return ERR_PTR(err);
1940}
1941
1942static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1943{
1944 return ((ft->type == FS_FT_NIC_RX) &&
1945 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1946}
1947
1948struct mlx5_flow_handle *
1949mlx5_add_flow_rules(struct mlx5_flow_table *ft,
1950 const struct mlx5_flow_spec *spec,
1951 struct mlx5_flow_act *flow_act,
1952 struct mlx5_flow_destination *dest,
1953 int num_dest)
1954{
1955 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1956 static const struct mlx5_flow_spec zero_spec = {};
1957 struct mlx5_flow_destination *gen_dest = NULL;
1958 struct mlx5_flow_table *next_ft = NULL;
1959 struct mlx5_flow_handle *handle = NULL;
1960 u32 sw_action = flow_act->action;
1961 int i;
1962
1963 if (!spec)
1964 spec = &zero_spec;
1965
1966 if (!is_fwd_next_action(sw_action))
1967 return _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1968
1969 if (!fwd_next_prio_supported(ft))
1970 return ERR_PTR(-EOPNOTSUPP);
1971
1972 mutex_lock(&root->chain_lock);
1973 next_ft = find_next_fwd_ft(ft, flow_act);
1974 if (!next_ft) {
1975 handle = ERR_PTR(-EOPNOTSUPP);
1976 goto unlock;
1977 }
1978
1979 gen_dest = kcalloc(num_dest + 1, sizeof(*dest),
1980 GFP_KERNEL);
1981 if (!gen_dest) {
1982 handle = ERR_PTR(-ENOMEM);
1983 goto unlock;
1984 }
1985 for (i = 0; i < num_dest; i++)
1986 gen_dest[i] = dest[i];
1987 gen_dest[i].type =
1988 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1989 gen_dest[i].ft = next_ft;
1990 dest = gen_dest;
1991 num_dest++;
1992 flow_act->action &= ~(MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
1993 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
1994 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1995 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1996 if (IS_ERR(handle))
1997 goto unlock;
1998
1999 if (list_empty(&handle->rule[num_dest - 1]->next_ft)) {
2000 mutex_lock(&next_ft->lock);
2001 list_add(&handle->rule[num_dest - 1]->next_ft,
2002 &next_ft->fwd_rules);
2003 mutex_unlock(&next_ft->lock);
2004 handle->rule[num_dest - 1]->sw_action = sw_action;
2005 handle->rule[num_dest - 1]->ft = ft;
2006 }
2007unlock:
2008 mutex_unlock(&root->chain_lock);
2009 kfree(gen_dest);
2010 return handle;
2011}
2012EXPORT_SYMBOL(mlx5_add_flow_rules);
2013
2014void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
2015{
2016 struct fs_fte *fte;
2017 int i;
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031 fs_get_obj(fte, handle->rule[0]->node.parent);
2032 down_write_ref_node(&fte->node, false);
2033 for (i = handle->num_rules - 1; i >= 0; i--)
2034 tree_remove_node(&handle->rule[i]->node, true);
2035 if (fte->dests_size) {
2036 if (fte->modify_mask)
2037 modify_fte(fte);
2038 up_write_ref_node(&fte->node, false);
2039 } else if (list_empty(&fte->node.children)) {
2040 del_hw_fte(&fte->node);
2041
2042 fte->node.del_hw_func = NULL;
2043 up_write_ref_node(&fte->node, false);
2044 tree_put_node(&fte->node, false);
2045 }
2046 kfree(handle);
2047}
2048EXPORT_SYMBOL(mlx5_del_flow_rules);
2049
2050
2051static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
2052{
2053 struct fs_prio *prio;
2054
2055 fs_get_obj(prio, ft->node.parent);
2056
2057 if (!list_is_last(&ft->node.list, &prio->node.children))
2058 return list_next_entry(ft, node.list);
2059 return find_next_chained_ft(prio);
2060}
2061
2062static int update_root_ft_destroy(struct mlx5_flow_table *ft)
2063{
2064 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2065 struct mlx5_ft_underlay_qp *uqp;
2066 struct mlx5_flow_table *new_root_ft = NULL;
2067 int err = 0;
2068 u32 qpn;
2069
2070 if (root->root_ft != ft)
2071 return 0;
2072
2073 new_root_ft = find_next_ft(ft);
2074 if (!new_root_ft) {
2075 root->root_ft = NULL;
2076 return 0;
2077 }
2078
2079 if (list_empty(&root->underlay_qpns)) {
2080
2081 qpn = 0;
2082 err = root->cmds->update_root_ft(root, new_root_ft,
2083 qpn, false);
2084 } else {
2085 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2086 qpn = uqp->qpn;
2087 err = root->cmds->update_root_ft(root,
2088 new_root_ft, qpn,
2089 false);
2090 if (err)
2091 break;
2092 }
2093 }
2094
2095 if (err)
2096 mlx5_core_warn(root->dev,
2097 "Update root flow table of id(%u) qpn(%d) failed\n",
2098 ft->id, qpn);
2099 else
2100 root->root_ft = new_root_ft;
2101
2102 return 0;
2103}
2104
2105
2106
2107
2108static int disconnect_flow_table(struct mlx5_flow_table *ft)
2109{
2110 struct mlx5_core_dev *dev = get_dev(&ft->node);
2111 struct mlx5_flow_table *next_ft;
2112 struct fs_prio *prio;
2113 int err = 0;
2114
2115 err = update_root_ft_destroy(ft);
2116 if (err)
2117 return err;
2118
2119 fs_get_obj(prio, ft->node.parent);
2120 if (!(list_first_entry(&prio->node.children,
2121 struct mlx5_flow_table,
2122 node.list) == ft))
2123 return 0;
2124
2125 next_ft = find_next_ft(ft);
2126 err = connect_fwd_rules(dev, next_ft, ft);
2127 if (err)
2128 return err;
2129
2130 err = connect_prev_fts(dev, next_ft, prio);
2131 if (err)
2132 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2133 ft->id);
2134 return err;
2135}
2136
2137int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
2138{
2139 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2140 int err = 0;
2141
2142 mutex_lock(&root->chain_lock);
2143 if (!(ft->flags & MLX5_FLOW_TABLE_UNMANAGED))
2144 err = disconnect_flow_table(ft);
2145 if (err) {
2146 mutex_unlock(&root->chain_lock);
2147 return err;
2148 }
2149 if (tree_remove_node(&ft->node, false))
2150 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2151 ft->id);
2152 mutex_unlock(&root->chain_lock);
2153
2154 return err;
2155}
2156EXPORT_SYMBOL(mlx5_destroy_flow_table);
2157
2158void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
2159{
2160 if (tree_remove_node(&fg->node, false))
2161 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2162 fg->id);
2163}
2164EXPORT_SYMBOL(mlx5_destroy_flow_group);
2165
2166struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
2167 int n)
2168{
2169 struct mlx5_flow_steering *steering = dev->priv.steering;
2170
2171 if (!steering || !steering->fdb_sub_ns)
2172 return NULL;
2173
2174 return steering->fdb_sub_ns[n];
2175}
2176EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
2177
2178struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2179 enum mlx5_flow_namespace_type type)
2180{
2181 struct mlx5_flow_steering *steering = dev->priv.steering;
2182 struct mlx5_flow_root_namespace *root_ns;
2183 int prio = 0;
2184 struct fs_prio *fs_prio;
2185 struct mlx5_flow_namespace *ns;
2186
2187 if (!steering)
2188 return NULL;
2189
2190 switch (type) {
2191 case MLX5_FLOW_NAMESPACE_FDB:
2192 if (steering->fdb_root_ns)
2193 return &steering->fdb_root_ns->ns;
2194 return NULL;
2195 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2196 if (steering->sniffer_rx_root_ns)
2197 return &steering->sniffer_rx_root_ns->ns;
2198 return NULL;
2199 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2200 if (steering->sniffer_tx_root_ns)
2201 return &steering->sniffer_tx_root_ns->ns;
2202 return NULL;
2203 default:
2204 break;
2205 }
2206
2207 if (type == MLX5_FLOW_NAMESPACE_EGRESS ||
2208 type == MLX5_FLOW_NAMESPACE_EGRESS_KERNEL) {
2209 root_ns = steering->egress_root_ns;
2210 prio = type - MLX5_FLOW_NAMESPACE_EGRESS;
2211 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
2212 root_ns = steering->rdma_rx_root_ns;
2213 prio = RDMA_RX_BYPASS_PRIO;
2214 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL) {
2215 root_ns = steering->rdma_rx_root_ns;
2216 prio = RDMA_RX_KERNEL_PRIO;
2217 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_TX) {
2218 root_ns = steering->rdma_tx_root_ns;
2219 } else {
2220 root_ns = steering->root_ns;
2221 prio = type;
2222 }
2223
2224 if (!root_ns)
2225 return NULL;
2226
2227 fs_prio = find_prio(&root_ns->ns, prio);
2228 if (!fs_prio)
2229 return NULL;
2230
2231 ns = list_first_entry(&fs_prio->node.children,
2232 typeof(*ns),
2233 node.list);
2234
2235 return ns;
2236}
2237EXPORT_SYMBOL(mlx5_get_flow_namespace);
2238
2239struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2240 enum mlx5_flow_namespace_type type,
2241 int vport)
2242{
2243 struct mlx5_flow_steering *steering = dev->priv.steering;
2244
2245 if (!steering)
2246 return NULL;
2247
2248 switch (type) {
2249 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2250 if (vport >= steering->esw_egress_acl_vports)
2251 return NULL;
2252 if (steering->esw_egress_root_ns &&
2253 steering->esw_egress_root_ns[vport])
2254 return &steering->esw_egress_root_ns[vport]->ns;
2255 else
2256 return NULL;
2257 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2258 if (vport >= steering->esw_ingress_acl_vports)
2259 return NULL;
2260 if (steering->esw_ingress_root_ns &&
2261 steering->esw_ingress_root_ns[vport])
2262 return &steering->esw_ingress_root_ns[vport]->ns;
2263 else
2264 return NULL;
2265 default:
2266 return NULL;
2267 }
2268}
2269
2270static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2271 unsigned int prio,
2272 int num_levels,
2273 enum fs_node_type type)
2274{
2275 struct fs_prio *fs_prio;
2276
2277 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2278 if (!fs_prio)
2279 return ERR_PTR(-ENOMEM);
2280
2281 fs_prio->node.type = type;
2282 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2283 tree_add_node(&fs_prio->node, &ns->node);
2284 fs_prio->num_levels = num_levels;
2285 fs_prio->prio = prio;
2286 list_add_tail(&fs_prio->node.list, &ns->node.children);
2287
2288 return fs_prio;
2289}
2290
2291static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2292 unsigned int prio,
2293 int num_levels)
2294{
2295 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2296}
2297
2298static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2299 unsigned int prio, int num_levels)
2300{
2301 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2302}
2303
2304static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2305 *ns)
2306{
2307 ns->node.type = FS_TYPE_NAMESPACE;
2308
2309 return ns;
2310}
2311
2312static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2313 int def_miss_act)
2314{
2315 struct mlx5_flow_namespace *ns;
2316
2317 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2318 if (!ns)
2319 return ERR_PTR(-ENOMEM);
2320
2321 fs_init_namespace(ns);
2322 ns->def_miss_action = def_miss_act;
2323 tree_init_node(&ns->node, NULL, del_sw_ns);
2324 tree_add_node(&ns->node, &prio->node);
2325 list_add_tail(&ns->node.list, &prio->node.children);
2326
2327 return ns;
2328}
2329
2330static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2331 struct init_tree_node *prio_metadata)
2332{
2333 struct fs_prio *fs_prio;
2334 int i;
2335
2336 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2337 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2338 if (IS_ERR(fs_prio))
2339 return PTR_ERR(fs_prio);
2340 }
2341 return 0;
2342}
2343
2344#define FLOW_TABLE_BIT_SZ 1
2345#define GET_FLOW_TABLE_CAP(dev, offset) \
2346 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
2347 offset / 32)) >> \
2348 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2349static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2350{
2351 int i;
2352
2353 for (i = 0; i < caps->arr_sz; i++) {
2354 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2355 return false;
2356 }
2357 return true;
2358}
2359
2360static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2361 struct init_tree_node *init_node,
2362 struct fs_node *fs_parent_node,
2363 struct init_tree_node *init_parent_node,
2364 int prio)
2365{
2366 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2367 flow_table_properties_nic_receive.
2368 max_ft_level);
2369 struct mlx5_flow_namespace *fs_ns;
2370 struct fs_prio *fs_prio;
2371 struct fs_node *base;
2372 int i;
2373 int err;
2374
2375 if (init_node->type == FS_TYPE_PRIO) {
2376 if ((init_node->min_ft_level > max_ft_level) ||
2377 !has_required_caps(steering->dev, &init_node->caps))
2378 return 0;
2379
2380 fs_get_obj(fs_ns, fs_parent_node);
2381 if (init_node->num_leaf_prios)
2382 return create_leaf_prios(fs_ns, prio, init_node);
2383 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2384 if (IS_ERR(fs_prio))
2385 return PTR_ERR(fs_prio);
2386 base = &fs_prio->node;
2387 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2388 fs_get_obj(fs_prio, fs_parent_node);
2389 fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
2390 if (IS_ERR(fs_ns))
2391 return PTR_ERR(fs_ns);
2392 base = &fs_ns->node;
2393 } else {
2394 return -EINVAL;
2395 }
2396 prio = 0;
2397 for (i = 0; i < init_node->ar_size; i++) {
2398 err = init_root_tree_recursive(steering, &init_node->children[i],
2399 base, init_node, prio);
2400 if (err)
2401 return err;
2402 if (init_node->children[i].type == FS_TYPE_PRIO &&
2403 init_node->children[i].num_leaf_prios) {
2404 prio += init_node->children[i].num_leaf_prios;
2405 }
2406 }
2407
2408 return 0;
2409}
2410
2411static int init_root_tree(struct mlx5_flow_steering *steering,
2412 struct init_tree_node *init_node,
2413 struct fs_node *fs_parent_node)
2414{
2415 int err;
2416 int i;
2417
2418 for (i = 0; i < init_node->ar_size; i++) {
2419 err = init_root_tree_recursive(steering, &init_node->children[i],
2420 fs_parent_node,
2421 init_node, i);
2422 if (err)
2423 return err;
2424 }
2425 return 0;
2426}
2427
2428static void del_sw_root_ns(struct fs_node *node)
2429{
2430 struct mlx5_flow_root_namespace *root_ns;
2431 struct mlx5_flow_namespace *ns;
2432
2433 fs_get_obj(ns, node);
2434 root_ns = container_of(ns, struct mlx5_flow_root_namespace, ns);
2435 mutex_destroy(&root_ns->chain_lock);
2436 kfree(node);
2437}
2438
2439static struct mlx5_flow_root_namespace
2440*create_root_ns(struct mlx5_flow_steering *steering,
2441 enum fs_flow_table_type table_type)
2442{
2443 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2444 struct mlx5_flow_root_namespace *root_ns;
2445 struct mlx5_flow_namespace *ns;
2446
2447 if (mlx5_fpga_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
2448 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2449 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2450
2451
2452 root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL);
2453 if (!root_ns)
2454 return NULL;
2455
2456 root_ns->dev = steering->dev;
2457 root_ns->table_type = table_type;
2458 root_ns->cmds = cmds;
2459
2460 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2461
2462 ns = &root_ns->ns;
2463 fs_init_namespace(ns);
2464 mutex_init(&root_ns->chain_lock);
2465 tree_init_node(&ns->node, NULL, del_sw_root_ns);
2466 tree_add_node(&ns->node, NULL);
2467
2468 return root_ns;
2469}
2470
2471static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2472
2473static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2474{
2475 struct fs_prio *prio;
2476
2477 fs_for_each_prio(prio, ns) {
2478
2479 set_prio_attrs_in_prio(prio, acc_level);
2480 acc_level += prio->num_levels;
2481 }
2482 return acc_level;
2483}
2484
2485static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2486{
2487 struct mlx5_flow_namespace *ns;
2488 int acc_level_ns = acc_level;
2489
2490 prio->start_level = acc_level;
2491 fs_for_each_ns(ns, prio) {
2492
2493 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2494
2495
2496
2497
2498 if (prio->node.type == FS_TYPE_PRIO_CHAINS)
2499 acc_level = acc_level_ns;
2500 }
2501
2502 if (!prio->num_levels)
2503 prio->num_levels = acc_level_ns - prio->start_level;
2504 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2505}
2506
2507static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2508{
2509 struct mlx5_flow_namespace *ns = &root_ns->ns;
2510 struct fs_prio *prio;
2511 int start_level = 0;
2512
2513 fs_for_each_prio(prio, ns) {
2514 set_prio_attrs_in_prio(prio, start_level);
2515 start_level += prio->num_levels;
2516 }
2517}
2518
2519#define ANCHOR_PRIO 0
2520#define ANCHOR_SIZE 1
2521#define ANCHOR_LEVEL 0
2522static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2523{
2524 struct mlx5_flow_namespace *ns = NULL;
2525 struct mlx5_flow_table_attr ft_attr = {};
2526 struct mlx5_flow_table *ft;
2527
2528 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2529 if (WARN_ON(!ns))
2530 return -EINVAL;
2531
2532 ft_attr.max_fte = ANCHOR_SIZE;
2533 ft_attr.level = ANCHOR_LEVEL;
2534 ft_attr.prio = ANCHOR_PRIO;
2535
2536 ft = mlx5_create_flow_table(ns, &ft_attr);
2537 if (IS_ERR(ft)) {
2538 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2539 return PTR_ERR(ft);
2540 }
2541 return 0;
2542}
2543
2544static int init_root_ns(struct mlx5_flow_steering *steering)
2545{
2546 int err;
2547
2548 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2549 if (!steering->root_ns)
2550 return -ENOMEM;
2551
2552 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2553 if (err)
2554 goto out_err;
2555
2556 set_prio_attrs(steering->root_ns);
2557 err = create_anchor_flow_table(steering);
2558 if (err)
2559 goto out_err;
2560
2561 return 0;
2562
2563out_err:
2564 cleanup_root_ns(steering->root_ns);
2565 steering->root_ns = NULL;
2566 return err;
2567}
2568
2569static void clean_tree(struct fs_node *node)
2570{
2571 if (node) {
2572 struct fs_node *iter;
2573 struct fs_node *temp;
2574
2575 tree_get_node(node);
2576 list_for_each_entry_safe(iter, temp, &node->children, list)
2577 clean_tree(iter);
2578 tree_put_node(node, false);
2579 tree_remove_node(node, false);
2580 }
2581}
2582
2583static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2584{
2585 if (!root_ns)
2586 return;
2587
2588 clean_tree(&root_ns->ns.node);
2589}
2590
2591void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2592{
2593 struct mlx5_flow_steering *steering = dev->priv.steering;
2594
2595 cleanup_root_ns(steering->root_ns);
2596 cleanup_root_ns(steering->fdb_root_ns);
2597 steering->fdb_root_ns = NULL;
2598 kfree(steering->fdb_sub_ns);
2599 steering->fdb_sub_ns = NULL;
2600 cleanup_root_ns(steering->sniffer_rx_root_ns);
2601 cleanup_root_ns(steering->sniffer_tx_root_ns);
2602 cleanup_root_ns(steering->rdma_rx_root_ns);
2603 cleanup_root_ns(steering->rdma_tx_root_ns);
2604 cleanup_root_ns(steering->egress_root_ns);
2605 mlx5_cleanup_fc_stats(dev);
2606 kmem_cache_destroy(steering->ftes_cache);
2607 kmem_cache_destroy(steering->fgs_cache);
2608 mlx5_ft_pool_destroy(dev);
2609 kfree(steering);
2610}
2611
2612static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2613{
2614 struct fs_prio *prio;
2615
2616 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2617 if (!steering->sniffer_tx_root_ns)
2618 return -ENOMEM;
2619
2620
2621 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2622 return PTR_ERR_OR_ZERO(prio);
2623}
2624
2625static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2626{
2627 struct fs_prio *prio;
2628
2629 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2630 if (!steering->sniffer_rx_root_ns)
2631 return -ENOMEM;
2632
2633
2634 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2635 return PTR_ERR_OR_ZERO(prio);
2636}
2637
2638static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
2639{
2640 int err;
2641
2642 steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
2643 if (!steering->rdma_rx_root_ns)
2644 return -ENOMEM;
2645
2646 err = init_root_tree(steering, &rdma_rx_root_fs,
2647 &steering->rdma_rx_root_ns->ns.node);
2648 if (err)
2649 goto out_err;
2650
2651 set_prio_attrs(steering->rdma_rx_root_ns);
2652
2653 return 0;
2654
2655out_err:
2656 cleanup_root_ns(steering->rdma_rx_root_ns);
2657 steering->rdma_rx_root_ns = NULL;
2658 return err;
2659}
2660
2661static int init_rdma_tx_root_ns(struct mlx5_flow_steering *steering)
2662{
2663 int err;
2664
2665 steering->rdma_tx_root_ns = create_root_ns(steering, FS_FT_RDMA_TX);
2666 if (!steering->rdma_tx_root_ns)
2667 return -ENOMEM;
2668
2669 err = init_root_tree(steering, &rdma_tx_root_fs,
2670 &steering->rdma_tx_root_ns->ns.node);
2671 if (err)
2672 goto out_err;
2673
2674 set_prio_attrs(steering->rdma_tx_root_ns);
2675
2676 return 0;
2677
2678out_err:
2679 cleanup_root_ns(steering->rdma_tx_root_ns);
2680 steering->rdma_tx_root_ns = NULL;
2681 return err;
2682}
2683
2684
2685
2686
2687
2688
2689static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2690 struct mlx5_flow_namespace *ns)
2691{
2692 int chain = 0;
2693
2694 while (steering->fdb_sub_ns[chain])
2695 ++chain;
2696
2697 steering->fdb_sub_ns[chain] = ns;
2698}
2699
2700static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2701 struct fs_prio *maj_prio)
2702{
2703 struct mlx5_flow_namespace *ns;
2704 struct fs_prio *min_prio;
2705 int prio;
2706
2707 ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
2708 if (IS_ERR(ns))
2709 return PTR_ERR(ns);
2710
2711 for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
2712 min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
2713 if (IS_ERR(min_prio))
2714 return PTR_ERR(min_prio);
2715 }
2716
2717 store_fdb_sub_ns_prio_chain(steering, ns);
2718
2719 return 0;
2720}
2721
2722static int create_fdb_chains(struct mlx5_flow_steering *steering,
2723 int fs_prio,
2724 int chains)
2725{
2726 struct fs_prio *maj_prio;
2727 int levels;
2728 int chain;
2729 int err;
2730
2731 levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
2732 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
2733 fs_prio,
2734 levels);
2735 if (IS_ERR(maj_prio))
2736 return PTR_ERR(maj_prio);
2737
2738 for (chain = 0; chain < chains; chain++) {
2739 err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
2740 if (err)
2741 return err;
2742 }
2743
2744 return 0;
2745}
2746
2747static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
2748{
2749 int err;
2750
2751 steering->fdb_sub_ns = kcalloc(FDB_NUM_CHAINS,
2752 sizeof(*steering->fdb_sub_ns),
2753 GFP_KERNEL);
2754 if (!steering->fdb_sub_ns)
2755 return -ENOMEM;
2756
2757 err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
2758 if (err)
2759 return err;
2760
2761 err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
2762 if (err)
2763 return err;
2764
2765 return 0;
2766}
2767
2768static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2769{
2770 struct fs_prio *maj_prio;
2771 int err;
2772
2773 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2774 if (!steering->fdb_root_ns)
2775 return -ENOMEM;
2776
2777 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH,
2778 1);
2779 if (IS_ERR(maj_prio)) {
2780 err = PTR_ERR(maj_prio);
2781 goto out_err;
2782 }
2783 err = create_fdb_fast_path(steering);
2784 if (err)
2785 goto out_err;
2786
2787 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_TC_MISS, 1);
2788 if (IS_ERR(maj_prio)) {
2789 err = PTR_ERR(maj_prio);
2790 goto out_err;
2791 }
2792
2793 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BR_OFFLOAD, 3);
2794 if (IS_ERR(maj_prio)) {
2795 err = PTR_ERR(maj_prio);
2796 goto out_err;
2797 }
2798
2799 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
2800 if (IS_ERR(maj_prio)) {
2801 err = PTR_ERR(maj_prio);
2802 goto out_err;
2803 }
2804
2805
2806
2807
2808
2809
2810 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_PER_VPORT, 1);
2811 if (IS_ERR(maj_prio)) {
2812 err = PTR_ERR(maj_prio);
2813 goto out_err;
2814 }
2815
2816 set_prio_attrs(steering->fdb_root_ns);
2817 return 0;
2818
2819out_err:
2820 cleanup_root_ns(steering->fdb_root_ns);
2821 kfree(steering->fdb_sub_ns);
2822 steering->fdb_sub_ns = NULL;
2823 steering->fdb_root_ns = NULL;
2824 return err;
2825}
2826
2827static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2828{
2829 struct fs_prio *prio;
2830
2831 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2832 if (!steering->esw_egress_root_ns[vport])
2833 return -ENOMEM;
2834
2835
2836 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
2837 return PTR_ERR_OR_ZERO(prio);
2838}
2839
2840static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
2841{
2842 struct fs_prio *prio;
2843
2844 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2845 if (!steering->esw_ingress_root_ns[vport])
2846 return -ENOMEM;
2847
2848
2849 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
2850 return PTR_ERR_OR_ZERO(prio);
2851}
2852
2853int mlx5_fs_egress_acls_init(struct mlx5_core_dev *dev, int total_vports)
2854{
2855 struct mlx5_flow_steering *steering = dev->priv.steering;
2856 int err;
2857 int i;
2858
2859 steering->esw_egress_root_ns =
2860 kcalloc(total_vports,
2861 sizeof(*steering->esw_egress_root_ns),
2862 GFP_KERNEL);
2863 if (!steering->esw_egress_root_ns)
2864 return -ENOMEM;
2865
2866 for (i = 0; i < total_vports; i++) {
2867 err = init_egress_acl_root_ns(steering, i);
2868 if (err)
2869 goto cleanup_root_ns;
2870 }
2871 steering->esw_egress_acl_vports = total_vports;
2872 return 0;
2873
2874cleanup_root_ns:
2875 for (i--; i >= 0; i--)
2876 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2877 kfree(steering->esw_egress_root_ns);
2878 steering->esw_egress_root_ns = NULL;
2879 return err;
2880}
2881
2882void mlx5_fs_egress_acls_cleanup(struct mlx5_core_dev *dev)
2883{
2884 struct mlx5_flow_steering *steering = dev->priv.steering;
2885 int i;
2886
2887 if (!steering->esw_egress_root_ns)
2888 return;
2889
2890 for (i = 0; i < steering->esw_egress_acl_vports; i++)
2891 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2892
2893 kfree(steering->esw_egress_root_ns);
2894 steering->esw_egress_root_ns = NULL;
2895}
2896
2897int mlx5_fs_ingress_acls_init(struct mlx5_core_dev *dev, int total_vports)
2898{
2899 struct mlx5_flow_steering *steering = dev->priv.steering;
2900 int err;
2901 int i;
2902
2903 steering->esw_ingress_root_ns =
2904 kcalloc(total_vports,
2905 sizeof(*steering->esw_ingress_root_ns),
2906 GFP_KERNEL);
2907 if (!steering->esw_ingress_root_ns)
2908 return -ENOMEM;
2909
2910 for (i = 0; i < total_vports; i++) {
2911 err = init_ingress_acl_root_ns(steering, i);
2912 if (err)
2913 goto cleanup_root_ns;
2914 }
2915 steering->esw_ingress_acl_vports = total_vports;
2916 return 0;
2917
2918cleanup_root_ns:
2919 for (i--; i >= 0; i--)
2920 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2921 kfree(steering->esw_ingress_root_ns);
2922 steering->esw_ingress_root_ns = NULL;
2923 return err;
2924}
2925
2926void mlx5_fs_ingress_acls_cleanup(struct mlx5_core_dev *dev)
2927{
2928 struct mlx5_flow_steering *steering = dev->priv.steering;
2929 int i;
2930
2931 if (!steering->esw_ingress_root_ns)
2932 return;
2933
2934 for (i = 0; i < steering->esw_ingress_acl_vports; i++)
2935 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2936
2937 kfree(steering->esw_ingress_root_ns);
2938 steering->esw_ingress_root_ns = NULL;
2939}
2940
2941static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2942{
2943 int err;
2944
2945 steering->egress_root_ns = create_root_ns(steering,
2946 FS_FT_NIC_TX);
2947 if (!steering->egress_root_ns)
2948 return -ENOMEM;
2949
2950 err = init_root_tree(steering, &egress_root_fs,
2951 &steering->egress_root_ns->ns.node);
2952 if (err)
2953 goto cleanup;
2954 set_prio_attrs(steering->egress_root_ns);
2955 return 0;
2956cleanup:
2957 cleanup_root_ns(steering->egress_root_ns);
2958 steering->egress_root_ns = NULL;
2959 return err;
2960}
2961
2962int mlx5_init_fs(struct mlx5_core_dev *dev)
2963{
2964 struct mlx5_flow_steering *steering;
2965 int err = 0;
2966
2967 err = mlx5_init_fc_stats(dev);
2968 if (err)
2969 return err;
2970
2971 err = mlx5_ft_pool_init(dev);
2972 if (err)
2973 return err;
2974
2975 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2976 if (!steering) {
2977 err = -ENOMEM;
2978 goto err;
2979 }
2980
2981 steering->dev = dev;
2982 dev->priv.steering = steering;
2983
2984 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2985 sizeof(struct mlx5_flow_group), 0,
2986 0, NULL);
2987 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2988 0, NULL);
2989 if (!steering->ftes_cache || !steering->fgs_cache) {
2990 err = -ENOMEM;
2991 goto err;
2992 }
2993
2994 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2995 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2996 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2997 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
2998 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
2999 err = init_root_ns(steering);
3000 if (err)
3001 goto err;
3002 }
3003
3004 if (MLX5_ESWITCH_MANAGER(dev)) {
3005 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
3006 err = init_fdb_root_ns(steering);
3007 if (err)
3008 goto err;
3009 }
3010 }
3011
3012 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
3013 err = init_sniffer_rx_root_ns(steering);
3014 if (err)
3015 goto err;
3016 }
3017
3018 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
3019 err = init_sniffer_tx_root_ns(steering);
3020 if (err)
3021 goto err;
3022 }
3023
3024 if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) &&
3025 MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)) {
3026 err = init_rdma_rx_root_ns(steering);
3027 if (err)
3028 goto err;
3029 }
3030
3031 if (MLX5_CAP_FLOWTABLE_RDMA_TX(dev, ft_support)) {
3032 err = init_rdma_tx_root_ns(steering);
3033 if (err)
3034 goto err;
3035 }
3036
3037 if (mlx5_fpga_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE ||
3038 MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
3039 err = init_egress_root_ns(steering);
3040 if (err)
3041 goto err;
3042 }
3043
3044 return 0;
3045err:
3046 mlx5_cleanup_fs(dev);
3047 return err;
3048}
3049
3050int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3051{
3052 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3053 struct mlx5_ft_underlay_qp *new_uqp;
3054 int err = 0;
3055
3056 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
3057 if (!new_uqp)
3058 return -ENOMEM;
3059
3060 mutex_lock(&root->chain_lock);
3061
3062 if (!root->root_ft) {
3063 err = -EINVAL;
3064 goto update_ft_fail;
3065 }
3066
3067 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3068 false);
3069 if (err) {
3070 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
3071 underlay_qpn, err);
3072 goto update_ft_fail;
3073 }
3074
3075 new_uqp->qpn = underlay_qpn;
3076 list_add_tail(&new_uqp->list, &root->underlay_qpns);
3077
3078 mutex_unlock(&root->chain_lock);
3079
3080 return 0;
3081
3082update_ft_fail:
3083 mutex_unlock(&root->chain_lock);
3084 kfree(new_uqp);
3085 return err;
3086}
3087EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
3088
3089int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3090{
3091 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3092 struct mlx5_ft_underlay_qp *uqp;
3093 bool found = false;
3094 int err = 0;
3095
3096 mutex_lock(&root->chain_lock);
3097 list_for_each_entry(uqp, &root->underlay_qpns, list) {
3098 if (uqp->qpn == underlay_qpn) {
3099 found = true;
3100 break;
3101 }
3102 }
3103
3104 if (!found) {
3105 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
3106 underlay_qpn);
3107 err = -EINVAL;
3108 goto out;
3109 }
3110
3111 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3112 true);
3113 if (err)
3114 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
3115 underlay_qpn, err);
3116
3117 list_del(&uqp->list);
3118 mutex_unlock(&root->chain_lock);
3119 kfree(uqp);
3120
3121 return 0;
3122
3123out:
3124 mutex_unlock(&root->chain_lock);
3125 return err;
3126}
3127EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
3128
3129static struct mlx5_flow_root_namespace
3130*get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
3131{
3132 struct mlx5_flow_namespace *ns;
3133
3134 if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
3135 ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS)
3136 ns = mlx5_get_flow_vport_acl_namespace(dev, ns_type, 0);
3137 else
3138 ns = mlx5_get_flow_namespace(dev, ns_type);
3139 if (!ns)
3140 return NULL;
3141
3142 return find_root(&ns->node);
3143}
3144
3145struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
3146 u8 ns_type, u8 num_actions,
3147 void *modify_actions)
3148{
3149 struct mlx5_flow_root_namespace *root;
3150 struct mlx5_modify_hdr *modify_hdr;
3151 int err;
3152
3153 root = get_root_namespace(dev, ns_type);
3154 if (!root)
3155 return ERR_PTR(-EOPNOTSUPP);
3156
3157 modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL);
3158 if (!modify_hdr)
3159 return ERR_PTR(-ENOMEM);
3160
3161 modify_hdr->ns_type = ns_type;
3162 err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
3163 modify_actions, modify_hdr);
3164 if (err) {
3165 kfree(modify_hdr);
3166 return ERR_PTR(err);
3167 }
3168
3169 return modify_hdr;
3170}
3171EXPORT_SYMBOL(mlx5_modify_header_alloc);
3172
3173void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
3174 struct mlx5_modify_hdr *modify_hdr)
3175{
3176 struct mlx5_flow_root_namespace *root;
3177
3178 root = get_root_namespace(dev, modify_hdr->ns_type);
3179 if (WARN_ON(!root))
3180 return;
3181 root->cmds->modify_header_dealloc(root, modify_hdr);
3182 kfree(modify_hdr);
3183}
3184EXPORT_SYMBOL(mlx5_modify_header_dealloc);
3185
3186struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
3187 struct mlx5_pkt_reformat_params *params,
3188 enum mlx5_flow_namespace_type ns_type)
3189{
3190 struct mlx5_pkt_reformat *pkt_reformat;
3191 struct mlx5_flow_root_namespace *root;
3192 int err;
3193
3194 root = get_root_namespace(dev, ns_type);
3195 if (!root)
3196 return ERR_PTR(-EOPNOTSUPP);
3197
3198 pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL);
3199 if (!pkt_reformat)
3200 return ERR_PTR(-ENOMEM);
3201
3202 pkt_reformat->ns_type = ns_type;
3203 pkt_reformat->reformat_type = params->type;
3204 err = root->cmds->packet_reformat_alloc(root, params, ns_type,
3205 pkt_reformat);
3206 if (err) {
3207 kfree(pkt_reformat);
3208 return ERR_PTR(err);
3209 }
3210
3211 return pkt_reformat;
3212}
3213EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
3214
3215void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
3216 struct mlx5_pkt_reformat *pkt_reformat)
3217{
3218 struct mlx5_flow_root_namespace *root;
3219
3220 root = get_root_namespace(dev, pkt_reformat->ns_type);
3221 if (WARN_ON(!root))
3222 return;
3223 root->cmds->packet_reformat_dealloc(root, pkt_reformat);
3224 kfree(pkt_reformat);
3225}
3226EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
3227
3228int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
3229 struct mlx5_flow_root_namespace *peer_ns)
3230{
3231 if (peer_ns && ns->mode != peer_ns->mode) {
3232 mlx5_core_err(ns->dev,
3233 "Can't peer namespace of different steering mode\n");
3234 return -EINVAL;
3235 }
3236
3237 return ns->cmds->set_peer(ns, peer_ns);
3238}
3239
3240
3241
3242
3243
3244int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
3245 enum mlx5_flow_steering_mode mode)
3246{
3247 struct mlx5_flow_root_namespace *root;
3248 const struct mlx5_flow_cmds *cmds;
3249 int err;
3250
3251 root = find_root(&ns->node);
3252 if (&root->ns != ns)
3253
3254 return -EINVAL;
3255
3256 if (root->table_type != FS_FT_FDB)
3257 return -EOPNOTSUPP;
3258
3259 if (root->mode == mode)
3260 return 0;
3261
3262 if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
3263 cmds = mlx5_fs_cmd_get_dr_cmds();
3264 else
3265 cmds = mlx5_fs_cmd_get_fw_cmds();
3266 if (!cmds)
3267 return -EOPNOTSUPP;
3268
3269 err = cmds->create_ns(root);
3270 if (err) {
3271 mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
3272 err);
3273 return err;
3274 }
3275
3276 root->cmds->destroy_ns(root);
3277 root->cmds = cmds;
3278 root->mode = mode;
3279
3280 return 0;
3281}
3282