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