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