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