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