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