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 policy miss, {IPsec RoCE MPV,Alias table},IPsec RoCE policy
118 */
119 #define KERNEL_NIC_PRIO_NUM_LEVELS 11
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 mlx5_fc_local_put(rule->dest_attr.counter);
667 goto out;
668 }
669
670 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_PORT) {
671 --fte->act_dests.dests_size;
672 fte->act_dests.modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
673 fte->act_dests.action.action &= ~MLX5_FLOW_CONTEXT_ACTION_ALLOW;
674 goto out;
675 }
676
677 if (is_fwd_dest_type(rule->dest_attr.type)) {
678 --fte->act_dests.dests_size;
679 --fte->act_dests.fwd_dests;
680
681 if (!fte->act_dests.fwd_dests)
682 fte->act_dests.action.action &=
683 ~MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
684 fte->act_dests.modify_mask |=
685 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
686 goto out;
687 }
688 out:
689 kfree(rule);
690 }
691
switch_to_pending_act_dests(struct fs_fte * fte)692 static void switch_to_pending_act_dests(struct fs_fte *fte)
693 {
694 struct fs_node *iter;
695
696 memcpy(&fte->act_dests, &fte->dup->act_dests, sizeof(fte->act_dests));
697
698 list_bulk_move_tail(&fte->node.children,
699 fte->dup->children.next,
700 fte->dup->children.prev);
701
702 list_for_each_entry(iter, &fte->node.children, list)
703 iter->del_sw_func = del_sw_hw_rule;
704
705 /* Make sure the fte isn't deleted
706 * as mlx5_del_flow_rules() decreases the refcount
707 * of the fte to trigger deletion.
708 */
709 tree_get_node(&fte->node);
710 }
711
del_hw_fte(struct fs_node * node)712 static void del_hw_fte(struct fs_node *node)
713 {
714 struct mlx5_flow_root_namespace *root;
715 struct mlx5_flow_table *ft;
716 struct mlx5_flow_group *fg;
717 struct mlx5_core_dev *dev;
718 bool pending_used = false;
719 struct fs_fte *fte;
720 int err;
721
722 fs_get_obj(fte, node);
723 fs_get_obj(fg, fte->node.parent);
724 fs_get_obj(ft, fg->node.parent);
725
726 trace_mlx5_fs_del_fte(fte);
727 WARN_ON(fte->act_dests.dests_size);
728 dev = get_dev(&ft->node);
729 root = find_root(&ft->node);
730
731 if (fte->dup && !list_empty(&fte->dup->children)) {
732 switch_to_pending_act_dests(fte);
733 pending_used = true;
734 } else {
735 /* Avoid double call to del_hw_fte */
736 node->del_hw_func = NULL;
737 }
738
739 if (node->active) {
740 if (pending_used) {
741 err = root->cmds->update_fte(root, ft, fg,
742 fte->act_dests.modify_mask, fte);
743 if (err)
744 mlx5_core_warn(dev,
745 "flow steering can't update to pending rule in index %d of flow group id %d\n",
746 fte->index, fg->id);
747 fte->act_dests.modify_mask = 0;
748 } else {
749 err = root->cmds->delete_fte(root, ft, fte);
750 if (err)
751 mlx5_core_warn(dev,
752 "flow steering can't delete fte in index %d of flow group id %d\n",
753 fte->index, fg->id);
754 node->active = false;
755 }
756 }
757 }
758
del_sw_fte(struct fs_node * node)759 static void del_sw_fte(struct fs_node *node)
760 {
761 struct mlx5_flow_steering *steering = get_steering(node);
762 struct mlx5_flow_group *fg;
763 struct fs_fte *fte;
764 int err;
765
766 fs_get_obj(fte, node);
767 fs_get_obj(fg, fte->node.parent);
768
769 err = rhashtable_remove_fast(&fg->ftes_hash,
770 &fte->hash,
771 rhash_fte);
772 WARN_ON(err);
773 ida_free(&fg->fte_allocator, fte->index - fg->start_index);
774 kvfree(fte->dup);
775 kmem_cache_free(steering->ftes_cache, fte);
776 }
777
del_hw_flow_group(struct fs_node * node)778 static void del_hw_flow_group(struct fs_node *node)
779 {
780 struct mlx5_flow_root_namespace *root;
781 struct mlx5_flow_group *fg;
782 struct mlx5_flow_table *ft;
783 struct mlx5_core_dev *dev;
784
785 fs_get_obj(fg, node);
786 fs_get_obj(ft, fg->node.parent);
787 dev = get_dev(&ft->node);
788 trace_mlx5_fs_del_fg(fg);
789
790 root = find_root(&ft->node);
791 if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
792 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
793 fg->id, ft->id);
794 }
795
del_sw_flow_group(struct fs_node * node)796 static void del_sw_flow_group(struct fs_node *node)
797 {
798 struct mlx5_flow_steering *steering = get_steering(node);
799 struct mlx5_flow_group *fg;
800 struct mlx5_flow_table *ft;
801 int err;
802
803 fs_get_obj(fg, node);
804 fs_get_obj(ft, fg->node.parent);
805
806 rhashtable_destroy(&fg->ftes_hash);
807 ida_destroy(&fg->fte_allocator);
808 if (ft->autogroup.active &&
809 fg->max_ftes == ft->autogroup.group_size &&
810 fg->start_index < ft->autogroup.max_fte)
811 ft->autogroup.num_groups--;
812 err = rhltable_remove(&ft->fgs_hash,
813 &fg->hash,
814 rhash_fg);
815 WARN_ON(err);
816 kmem_cache_free(steering->fgs_cache, fg);
817 }
818
insert_fte(struct mlx5_flow_group * fg,struct fs_fte * fte)819 static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
820 {
821 int index;
822 int ret;
823
824 index = ida_alloc_max(&fg->fte_allocator, fg->max_ftes - 1, GFP_KERNEL);
825 if (index < 0)
826 return index;
827
828 fte->index = index + fg->start_index;
829 retry_insert:
830 ret = rhashtable_insert_fast(&fg->ftes_hash,
831 &fte->hash,
832 rhash_fte);
833 if (ret) {
834 if (ret == -EBUSY) {
835 cond_resched();
836 goto retry_insert;
837 }
838 goto err_ida_remove;
839 }
840
841 tree_add_node(&fte->node, &fg->node);
842 list_add_tail(&fte->node.list, &fg->node.children);
843 return 0;
844
845 err_ida_remove:
846 ida_free(&fg->fte_allocator, index);
847 return ret;
848 }
849
alloc_fte(struct mlx5_flow_table * ft,const struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act)850 static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
851 const struct mlx5_flow_spec *spec,
852 struct mlx5_flow_act *flow_act)
853 {
854 struct mlx5_flow_steering *steering = get_steering(&ft->node);
855 struct fs_fte *fte;
856
857 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
858 if (!fte)
859 return ERR_PTR(-ENOMEM);
860
861 memcpy(fte->val, &spec->match_value, sizeof(fte->val));
862 fte->node.type = FS_TYPE_FLOW_ENTRY;
863 fte->act_dests.action = *flow_act;
864 fte->act_dests.flow_context = spec->flow_context;
865
866 tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
867
868 return fte;
869 }
870
dealloc_flow_group(struct mlx5_flow_steering * steering,struct mlx5_flow_group * fg)871 static void dealloc_flow_group(struct mlx5_flow_steering *steering,
872 struct mlx5_flow_group *fg)
873 {
874 rhashtable_destroy(&fg->ftes_hash);
875 kmem_cache_free(steering->fgs_cache, fg);
876 }
877
alloc_flow_group(struct mlx5_flow_steering * steering,u8 match_criteria_enable,const void * match_criteria,int start_index,int end_index)878 static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
879 u8 match_criteria_enable,
880 const void *match_criteria,
881 int start_index,
882 int end_index)
883 {
884 struct mlx5_flow_group *fg;
885 int ret;
886
887 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
888 if (!fg)
889 return ERR_PTR(-ENOMEM);
890
891 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
892 if (ret) {
893 kmem_cache_free(steering->fgs_cache, fg);
894 return ERR_PTR(ret);
895 }
896
897 ida_init(&fg->fte_allocator);
898 fg->mask.match_criteria_enable = match_criteria_enable;
899 memcpy(&fg->mask.match_criteria, match_criteria,
900 sizeof(fg->mask.match_criteria));
901 fg->node.type = FS_TYPE_FLOW_GROUP;
902 fg->start_index = start_index;
903 fg->max_ftes = end_index - start_index + 1;
904
905 return fg;
906 }
907
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)908 static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
909 u8 match_criteria_enable,
910 const void *match_criteria,
911 int start_index,
912 int end_index,
913 struct list_head *prev)
914 {
915 struct mlx5_flow_steering *steering = get_steering(&ft->node);
916 struct mlx5_flow_group *fg;
917 int ret;
918
919 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
920 start_index, end_index);
921 if (IS_ERR(fg))
922 return fg;
923
924 /* initialize refcnt, add to parent list */
925 ret = rhltable_insert(&ft->fgs_hash,
926 &fg->hash,
927 rhash_fg);
928 if (ret) {
929 dealloc_flow_group(steering, fg);
930 return ERR_PTR(ret);
931 }
932
933 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
934 tree_add_node(&fg->node, &ft->node);
935 /* Add node to group list */
936 list_add(&fg->node.list, prev);
937 atomic_inc(&ft->node.version);
938
939 return fg;
940 }
941
942 static struct mlx5_flow_table *
alloc_flow_table(struct mlx5_flow_table_attr * ft_attr,u16 vport,enum fs_flow_table_type table_type,enum fs_flow_table_op_mod op_mod)943 alloc_flow_table(struct mlx5_flow_table_attr *ft_attr, u16 vport,
944 enum fs_flow_table_type table_type,
945 enum fs_flow_table_op_mod op_mod)
946 {
947 struct mlx5_flow_table *ft;
948 int ret;
949
950 ft = kzalloc_obj(*ft);
951 if (!ft)
952 return ERR_PTR(-ENOMEM);
953
954 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
955 if (ret) {
956 kfree(ft);
957 return ERR_PTR(ret);
958 }
959
960 ft->level = ft_attr->level;
961 ft->node.type = FS_TYPE_FLOW_TABLE;
962 ft->op_mod = op_mod;
963 ft->type = table_type;
964 ft->vport = vport;
965 ft->esw_owner_vhca_id = ft_attr->esw_owner_vhca_id;
966 ft->flags = ft_attr->flags;
967 INIT_LIST_HEAD(&ft->fwd_rules);
968 mutex_init(&ft->lock);
969
970 return ft;
971 }
972
973 /* If reverse is false, then we search for the first flow table in the
974 * root sub-tree from start(closest from right), else we search for the
975 * last flow table in the root sub-tree till start(closest from left).
976 */
find_closest_ft_recursive(struct fs_node * root,struct list_head * start,bool reverse)977 static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
978 struct list_head *start,
979 bool reverse)
980 {
981 #define list_advance_entry(pos, reverse) \
982 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
983
984 #define list_for_each_advance_continue(pos, head, reverse) \
985 for (pos = list_advance_entry(pos, reverse); \
986 &pos->list != (head); \
987 pos = list_advance_entry(pos, reverse))
988
989 struct fs_node *iter = list_entry(start, struct fs_node, list);
990 struct mlx5_flow_table *ft = NULL;
991
992 if (!root)
993 return NULL;
994
995 list_for_each_advance_continue(iter, &root->children, reverse) {
996 if (iter->type == FS_TYPE_FLOW_TABLE) {
997 fs_get_obj(ft, iter);
998 return ft;
999 }
1000 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
1001 if (ft)
1002 return ft;
1003 }
1004
1005 return ft;
1006 }
1007
find_prio_chains_parent(struct fs_node * parent,struct fs_node ** child)1008 static struct fs_node *find_prio_chains_parent(struct fs_node *parent,
1009 struct fs_node **child)
1010 {
1011 struct fs_node *node = NULL;
1012
1013 while (parent && parent->type != FS_TYPE_PRIO_CHAINS) {
1014 node = parent;
1015 parent = parent->parent;
1016 }
1017
1018 if (child)
1019 *child = node;
1020
1021 return parent;
1022 }
1023
1024 /* If reverse is false then return the first flow table next to the passed node
1025 * in the tree, else return the last flow table before the node in the tree.
1026 * If skip is true, skip the flow tables in the same prio_chains prio.
1027 */
find_closest_ft(struct fs_node * node,bool reverse,bool skip)1028 static struct mlx5_flow_table *find_closest_ft(struct fs_node *node, bool reverse,
1029 bool skip)
1030 {
1031 struct fs_node *prio_chains_parent = NULL;
1032 struct mlx5_flow_table *ft = NULL;
1033 struct fs_node *curr_node;
1034 struct fs_node *parent;
1035
1036 if (skip)
1037 prio_chains_parent = find_prio_chains_parent(node, NULL);
1038 parent = node->parent;
1039 curr_node = node;
1040 while (!ft && parent) {
1041 if (parent != prio_chains_parent)
1042 ft = find_closest_ft_recursive(parent, &curr_node->list,
1043 reverse);
1044 curr_node = parent;
1045 parent = curr_node->parent;
1046 }
1047 return ft;
1048 }
1049
1050 /* Assuming all the tree is locked by mutex chain lock */
find_next_chained_ft(struct fs_node * node)1051 static struct mlx5_flow_table *find_next_chained_ft(struct fs_node *node)
1052 {
1053 return find_closest_ft(node, false, true);
1054 }
1055
1056 /* Assuming all the tree is locked by mutex chain lock */
find_prev_chained_ft(struct fs_node * node)1057 static struct mlx5_flow_table *find_prev_chained_ft(struct fs_node *node)
1058 {
1059 return find_closest_ft(node, true, true);
1060 }
1061
find_next_fwd_ft(struct mlx5_flow_table * ft,struct mlx5_flow_act * flow_act)1062 static struct mlx5_flow_table *find_next_fwd_ft(struct mlx5_flow_table *ft,
1063 struct mlx5_flow_act *flow_act)
1064 {
1065 struct fs_prio *prio;
1066 bool next_ns;
1067
1068 next_ns = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS;
1069 fs_get_obj(prio, next_ns ? ft->ns->node.parent : ft->node.parent);
1070
1071 return find_next_chained_ft(&prio->node);
1072 }
1073
connect_fts_in_prio(struct mlx5_core_dev * dev,struct fs_prio * prio,struct mlx5_flow_table * ft)1074 static int connect_fts_in_prio(struct mlx5_core_dev *dev,
1075 struct fs_prio *prio,
1076 struct mlx5_flow_table *ft)
1077 {
1078 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
1079 struct mlx5_flow_table *iter;
1080 int err;
1081
1082 fs_for_each_ft(iter, prio) {
1083 err = root->cmds->modify_flow_table(root, iter, ft);
1084 if (err) {
1085 mlx5_core_err(dev,
1086 "Failed to modify flow table id %d, type %d, err %d\n",
1087 iter->id, iter->type, err);
1088 /* The driver is out of sync with the FW */
1089 return err;
1090 }
1091 }
1092 return 0;
1093 }
1094
find_closet_ft_prio_chains(struct fs_node * node,struct fs_node * parent,struct fs_node ** child,bool reverse)1095 static struct mlx5_flow_table *find_closet_ft_prio_chains(struct fs_node *node,
1096 struct fs_node *parent,
1097 struct fs_node **child,
1098 bool reverse)
1099 {
1100 struct mlx5_flow_table *ft;
1101
1102 ft = find_closest_ft(node, reverse, false);
1103
1104 if (ft && parent == find_prio_chains_parent(&ft->node, child))
1105 return ft;
1106
1107 return NULL;
1108 }
1109
1110 /* 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)1111 static int connect_prev_fts(struct mlx5_core_dev *dev,
1112 struct mlx5_flow_table *ft,
1113 struct fs_prio *prio)
1114 {
1115 struct fs_node *prio_parent, *parent = NULL, *child, *node;
1116 struct mlx5_flow_table *prev_ft;
1117 int err = 0;
1118
1119 prio_parent = find_prio_chains_parent(&prio->node, &child);
1120
1121 /* return directly if not under the first sub ns of prio_chains prio */
1122 if (prio_parent && !list_is_first(&child->list, &prio_parent->children))
1123 return 0;
1124
1125 prev_ft = find_prev_chained_ft(&prio->node);
1126 while (prev_ft) {
1127 struct fs_prio *prev_prio;
1128
1129 fs_get_obj(prev_prio, prev_ft->node.parent);
1130 err = connect_fts_in_prio(dev, prev_prio, ft);
1131 if (err)
1132 break;
1133
1134 if (!parent) {
1135 parent = find_prio_chains_parent(&prev_prio->node, &child);
1136 if (!parent)
1137 break;
1138 }
1139
1140 node = child;
1141 prev_ft = find_closet_ft_prio_chains(node, parent, &child, true);
1142 }
1143 return err;
1144 }
1145
update_root_ft_create(struct mlx5_flow_table * ft,struct fs_prio * prio)1146 static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
1147 *prio)
1148 {
1149 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
1150 struct mlx5_ft_underlay_qp *uqp;
1151 int min_level = INT_MAX;
1152 int err = 0;
1153 u32 qpn;
1154
1155 if (root->root_ft)
1156 min_level = root->root_ft->level;
1157
1158 if (ft->level >= min_level)
1159 return 0;
1160
1161 if (list_empty(&root->underlay_qpns)) {
1162 /* Don't set any QPN (zero) in case QPN list is empty */
1163 qpn = 0;
1164 err = root->cmds->update_root_ft(root, ft, qpn, false);
1165 } else {
1166 list_for_each_entry(uqp, &root->underlay_qpns, list) {
1167 qpn = uqp->qpn;
1168 err = root->cmds->update_root_ft(root, ft,
1169 qpn, false);
1170 if (err)
1171 break;
1172 }
1173 }
1174
1175 if (err)
1176 mlx5_core_warn(root->dev,
1177 "Update root flow table of id(%u) qpn(%d) failed\n",
1178 ft->id, qpn);
1179 else
1180 root->root_ft = ft;
1181
1182 return err;
1183 }
1184
rule_is_pending(struct fs_fte * fte,struct mlx5_flow_rule * rule)1185 static bool rule_is_pending(struct fs_fte *fte, struct mlx5_flow_rule *rule)
1186 {
1187 struct mlx5_flow_rule *tmp_rule;
1188 struct fs_node *iter;
1189
1190 if (!fte->dup || list_empty(&fte->dup->children))
1191 return false;
1192
1193 list_for_each_entry(iter, &fte->dup->children, list) {
1194 tmp_rule = container_of(iter, struct mlx5_flow_rule, node);
1195
1196 if (tmp_rule == rule)
1197 return true;
1198 }
1199
1200 return false;
1201 }
1202
_mlx5_modify_rule_destination(struct mlx5_flow_rule * rule,struct mlx5_flow_destination * dest)1203 static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
1204 struct mlx5_flow_destination *dest)
1205 {
1206 struct mlx5_flow_root_namespace *root;
1207 struct fs_fte_action *act_dests;
1208 struct mlx5_flow_table *ft;
1209 struct mlx5_flow_group *fg;
1210 bool pending = false;
1211 struct fs_fte *fte;
1212 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1213 int err = 0;
1214
1215 fs_get_obj(fte, rule->node.parent);
1216
1217 pending = rule_is_pending(fte, rule);
1218 if (pending)
1219 act_dests = &fte->dup->act_dests;
1220 else
1221 act_dests = &fte->act_dests;
1222
1223 if (!(act_dests->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1224 return -EINVAL;
1225 down_write_ref_node(&fte->node, false);
1226 fs_get_obj(fg, fte->node.parent);
1227 fs_get_obj(ft, fg->node.parent);
1228
1229 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1230 root = find_root(&ft->node);
1231 if (!pending)
1232 err = root->cmds->update_fte(root, ft, fg,
1233 modify_mask, fte);
1234 up_write_ref_node(&fte->node, false);
1235
1236 return err;
1237 }
1238
mlx5_modify_rule_destination(struct mlx5_flow_handle * handle,struct mlx5_flow_destination * new_dest,struct mlx5_flow_destination * old_dest)1239 int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
1240 struct mlx5_flow_destination *new_dest,
1241 struct mlx5_flow_destination *old_dest)
1242 {
1243 int i;
1244
1245 if (!old_dest) {
1246 if (handle->num_rules != 1)
1247 return -EINVAL;
1248 return _mlx5_modify_rule_destination(handle->rule[0],
1249 new_dest);
1250 }
1251
1252 for (i = 0; i < handle->num_rules; i++) {
1253 if (mlx5_flow_dests_cmp(old_dest, &handle->rule[i]->dest_attr))
1254 return _mlx5_modify_rule_destination(handle->rule[i],
1255 new_dest);
1256 }
1257
1258 return -EINVAL;
1259 }
1260
1261 /* 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)1262 static int connect_fwd_rules(struct mlx5_core_dev *dev,
1263 struct mlx5_flow_table *new_next_ft,
1264 struct mlx5_flow_table *old_next_ft)
1265 {
1266 struct mlx5_flow_destination dest = {};
1267 struct mlx5_flow_rule *iter;
1268 int err = 0;
1269
1270 /* new_next_ft and old_next_ft could be NULL only
1271 * when we create/destroy the anchor flow table.
1272 */
1273 if (!new_next_ft || !old_next_ft)
1274 return 0;
1275
1276 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1277 dest.ft = new_next_ft;
1278
1279 mutex_lock(&old_next_ft->lock);
1280 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
1281 mutex_unlock(&old_next_ft->lock);
1282 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
1283 if ((iter->sw_action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS) &&
1284 iter->ft->ns == new_next_ft->ns)
1285 continue;
1286
1287 err = _mlx5_modify_rule_destination(iter, &dest);
1288 if (err)
1289 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
1290 new_next_ft->id);
1291 }
1292 return 0;
1293 }
1294
connect_flow_table(struct mlx5_core_dev * dev,struct mlx5_flow_table * ft,struct fs_prio * prio)1295 static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
1296 struct fs_prio *prio)
1297 {
1298 struct mlx5_flow_table *next_ft, *first_ft;
1299 int err = 0;
1300
1301 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
1302
1303 first_ft = list_first_entry_or_null(&prio->node.children,
1304 struct mlx5_flow_table, node.list);
1305 if (!first_ft || first_ft->level > ft->level) {
1306 err = connect_prev_fts(dev, ft, prio);
1307 if (err)
1308 return err;
1309
1310 next_ft = first_ft ? first_ft : find_next_chained_ft(&prio->node);
1311 err = connect_fwd_rules(dev, ft, next_ft);
1312 if (err)
1313 return err;
1314 }
1315
1316 if (MLX5_CAP_FLOWTABLE(dev,
1317 flow_table_properties_nic_receive.modify_root))
1318 err = update_root_ft_create(ft, prio);
1319 return err;
1320 }
1321
list_add_flow_table(struct mlx5_flow_table * ft,struct fs_prio * prio)1322 static void list_add_flow_table(struct mlx5_flow_table *ft,
1323 struct fs_prio *prio)
1324 {
1325 struct list_head *prev = &prio->node.children;
1326 struct mlx5_flow_table *iter;
1327
1328 fs_for_each_ft(iter, prio) {
1329 if (iter->level > ft->level)
1330 break;
1331 prev = &iter->node.list;
1332 }
1333 list_add(&ft->node.list, prev);
1334 }
1335
__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)1336 static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1337 struct mlx5_flow_table_attr *ft_attr,
1338 enum fs_flow_table_op_mod op_mod,
1339 u16 vport)
1340 {
1341 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
1342 bool unmanaged = ft_attr->flags & MLX5_FLOW_TABLE_UNMANAGED;
1343 struct mlx5_flow_table *next_ft;
1344 struct fs_prio *fs_prio = NULL;
1345 struct mlx5_flow_table *ft;
1346 int err;
1347
1348 if (!root) {
1349 pr_err("mlx5: flow steering failed to find root of namespace\n");
1350 return ERR_PTR(-ENODEV);
1351 }
1352
1353 mutex_lock(&root->chain_lock);
1354 fs_prio = find_prio(ns, ft_attr->prio);
1355 if (!fs_prio) {
1356 err = -EINVAL;
1357 goto unlock_root;
1358 }
1359 if (!unmanaged) {
1360 /* The level is related to the
1361 * priority level range.
1362 */
1363 if (ft_attr->level >= fs_prio->num_levels) {
1364 err = -ENOSPC;
1365 goto unlock_root;
1366 }
1367
1368 ft_attr->level += fs_prio->start_level;
1369 }
1370
1371 /* The level is related to the
1372 * priority level range.
1373 */
1374 ft = alloc_flow_table(ft_attr, vport, root->table_type, op_mod);
1375 if (IS_ERR(ft)) {
1376 err = PTR_ERR(ft);
1377 goto unlock_root;
1378 }
1379
1380 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
1381 next_ft = unmanaged ? ft_attr->next_ft :
1382 find_next_chained_ft(&fs_prio->node);
1383 ft->def_miss_action = ns->def_miss_action;
1384 ft->ns = ns;
1385 err = root->cmds->create_flow_table(root, ft, ft_attr, next_ft);
1386 if (err)
1387 goto free_ft;
1388
1389 if (!unmanaged) {
1390 err = connect_flow_table(root->dev, ft, fs_prio);
1391 if (err)
1392 goto destroy_ft;
1393 }
1394
1395 ft->node.active = true;
1396 down_write_ref_node(&fs_prio->node, false);
1397 if (!unmanaged) {
1398 tree_add_node(&ft->node, &fs_prio->node);
1399 list_add_flow_table(ft, fs_prio);
1400 } else {
1401 ft->node.root = fs_prio->node.root;
1402 }
1403 fs_prio->num_ft++;
1404 up_write_ref_node(&fs_prio->node, false);
1405 mutex_unlock(&root->chain_lock);
1406 trace_mlx5_fs_add_ft(ft);
1407 return ft;
1408 destroy_ft:
1409 root->cmds->destroy_flow_table(root, ft);
1410 free_ft:
1411 rhltable_destroy(&ft->fgs_hash);
1412 kfree(ft);
1413 unlock_root:
1414 mutex_unlock(&root->chain_lock);
1415 return ERR_PTR(err);
1416 }
1417
mlx5_create_flow_table(struct mlx5_flow_namespace * ns,struct mlx5_flow_table_attr * ft_attr)1418 struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1419 struct mlx5_flow_table_attr *ft_attr)
1420 {
1421 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1422 }
1423 EXPORT_SYMBOL(mlx5_create_flow_table);
1424
mlx5_flow_table_id(struct mlx5_flow_table * ft)1425 u32 mlx5_flow_table_id(struct mlx5_flow_table *ft)
1426 {
1427 return ft->id;
1428 }
1429 EXPORT_SYMBOL(mlx5_flow_table_id);
1430
1431 struct mlx5_flow_table *
mlx5_create_vport_flow_table(struct mlx5_flow_namespace * ns,struct mlx5_flow_table_attr * ft_attr,u16 vport)1432 mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1433 struct mlx5_flow_table_attr *ft_attr, u16 vport)
1434 {
1435 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1436 }
1437
1438 struct mlx5_flow_table*
mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace * ns,int prio,u32 level)1439 mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1440 int prio, u32 level)
1441 {
1442 struct mlx5_flow_table_attr ft_attr = {};
1443
1444 ft_attr.level = level;
1445 ft_attr.prio = prio;
1446 ft_attr.max_fte = 1;
1447
1448 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1449 }
1450 EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1451
1452 #define MAX_FLOW_GROUP_SIZE BIT(24)
1453 struct mlx5_flow_table*
mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace * ns,struct mlx5_flow_table_attr * ft_attr)1454 mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
1455 struct mlx5_flow_table_attr *ft_attr)
1456 {
1457 int num_reserved_entries = ft_attr->autogroup.num_reserved_entries;
1458 int max_num_groups = ft_attr->autogroup.max_num_groups;
1459 struct mlx5_flow_table *ft;
1460 int autogroups_max_fte;
1461
1462 ft = mlx5_create_vport_flow_table(ns, ft_attr, ft_attr->vport);
1463 if (IS_ERR(ft))
1464 return ft;
1465
1466 autogroups_max_fte = ft->max_fte - num_reserved_entries;
1467 if (max_num_groups > autogroups_max_fte)
1468 goto err_validate;
1469 if (num_reserved_entries > ft->max_fte)
1470 goto err_validate;
1471
1472 /* Align the number of groups according to the largest group size */
1473 if (autogroups_max_fte / (max_num_groups + 1) > MAX_FLOW_GROUP_SIZE)
1474 max_num_groups = (autogroups_max_fte / MAX_FLOW_GROUP_SIZE) - 1;
1475
1476 ft->autogroup.active = true;
1477 ft->autogroup.required_groups = max_num_groups;
1478 ft->autogroup.max_fte = autogroups_max_fte;
1479 /* We save place for flow groups in addition to max types */
1480 ft->autogroup.group_size = autogroups_max_fte / (max_num_groups + 1);
1481
1482 return ft;
1483
1484 err_validate:
1485 mlx5_destroy_flow_table(ft);
1486 return ERR_PTR(-ENOSPC);
1487 }
1488 EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1489
mlx5_create_flow_group(struct mlx5_flow_table * ft,u32 * fg_in)1490 struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1491 u32 *fg_in)
1492 {
1493 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1494 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1495 fg_in, match_criteria);
1496 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1497 fg_in,
1498 match_criteria_enable);
1499 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1500 start_flow_index);
1501 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1502 end_flow_index);
1503 struct mlx5_flow_group *fg;
1504 int err;
1505
1506 if (ft->autogroup.active && start_index < ft->autogroup.max_fte)
1507 return ERR_PTR(-EPERM);
1508
1509 down_write_ref_node(&ft->node, false);
1510 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1511 start_index, end_index,
1512 ft->node.children.prev);
1513 up_write_ref_node(&ft->node, false);
1514 if (IS_ERR(fg))
1515 return fg;
1516
1517 err = root->cmds->create_flow_group(root, ft, fg_in, fg);
1518 if (err) {
1519 tree_put_node(&fg->node, false);
1520 return ERR_PTR(err);
1521 }
1522 trace_mlx5_fs_add_fg(fg);
1523 fg->node.active = true;
1524
1525 return fg;
1526 }
1527 EXPORT_SYMBOL(mlx5_create_flow_group);
1528
alloc_rule(struct mlx5_flow_destination * dest)1529 static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1530 {
1531 struct mlx5_flow_rule *rule;
1532
1533 rule = kzalloc_obj(*rule);
1534 if (!rule)
1535 return NULL;
1536
1537 INIT_LIST_HEAD(&rule->next_ft);
1538 rule->node.type = FS_TYPE_FLOW_DEST;
1539 if (dest)
1540 memcpy(&rule->dest_attr, dest, sizeof(*dest));
1541 else
1542 rule->dest_attr.type = MLX5_FLOW_DESTINATION_TYPE_NONE;
1543
1544 return rule;
1545 }
1546
alloc_handle(int num_rules)1547 static struct mlx5_flow_handle *alloc_handle(int num_rules)
1548 {
1549 struct mlx5_flow_handle *handle;
1550
1551 handle = kzalloc_flex(*handle, rule, num_rules);
1552 if (!handle)
1553 return NULL;
1554
1555 handle->num_rules = num_rules;
1556
1557 return handle;
1558 }
1559
destroy_flow_handle_dup(struct mlx5_flow_handle * handle,int i)1560 static void destroy_flow_handle_dup(struct mlx5_flow_handle *handle,
1561 int i)
1562 {
1563 for (; --i >= 0;) {
1564 list_del(&handle->rule[i]->node.list);
1565 kfree(handle->rule[i]);
1566 }
1567 kfree(handle);
1568 }
1569
destroy_flow_handle(struct fs_fte * fte,struct mlx5_flow_handle * handle,struct mlx5_flow_destination * dest,int i)1570 static void destroy_flow_handle(struct fs_fte *fte,
1571 struct mlx5_flow_handle *handle,
1572 struct mlx5_flow_destination *dest,
1573 int i)
1574 {
1575 for (; --i >= 0;) {
1576 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
1577 fte->act_dests.dests_size--;
1578 list_del(&handle->rule[i]->node.list);
1579 kfree(handle->rule[i]);
1580 }
1581 }
1582 kfree(handle);
1583 }
1584
1585 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)1586 create_flow_handle_dup(struct list_head *children,
1587 struct mlx5_flow_destination *dest,
1588 int dest_num,
1589 struct fs_fte_action *act_dests)
1590 {
1591 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1592 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1593 struct mlx5_flow_rule *rule = NULL;
1594 struct mlx5_flow_handle *handle;
1595 int i = 0;
1596 int type;
1597
1598 handle = alloc_handle((dest_num) ? dest_num : 1);
1599 if (!handle)
1600 return NULL;
1601
1602 do {
1603 rule = alloc_rule(dest + i);
1604 if (!rule)
1605 goto free_rules;
1606
1607 /* Add dest to dests list- we need flow tables to be in the
1608 * end of the list for forward to next prio rules.
1609 */
1610 tree_init_node(&rule->node, NULL, del_sw_hw_dup_rule);
1611 if (dest &&
1612 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1613 list_add(&rule->node.list, children);
1614 else
1615 list_add_tail(&rule->node.list, children);
1616
1617 if (dest) {
1618 act_dests->dests_size++;
1619
1620 if (is_fwd_dest_type(dest[i].type))
1621 act_dests->fwd_dests++;
1622
1623 type = dest[i].type ==
1624 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1625 act_dests->modify_mask |= type ? count : dst;
1626 }
1627 handle->rule[i] = rule;
1628 } while (++i < dest_num);
1629
1630 return handle;
1631
1632 free_rules:
1633 destroy_flow_handle_dup(handle, i);
1634 act_dests->dests_size = 0;
1635 act_dests->fwd_dests = 0;
1636
1637 return NULL;
1638 }
1639
1640 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)1641 create_flow_handle(struct fs_fte *fte,
1642 struct mlx5_flow_destination *dest,
1643 int dest_num,
1644 int *modify_mask,
1645 bool *new_rule)
1646 {
1647 struct mlx5_flow_handle *handle;
1648 struct mlx5_flow_rule *rule = NULL;
1649 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1650 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1651 int type;
1652 int i = 0;
1653
1654 handle = alloc_handle((dest_num) ? dest_num : 1);
1655 if (!handle)
1656 return ERR_PTR(-ENOMEM);
1657
1658 do {
1659 if (dest) {
1660 rule = find_flow_rule(fte, dest + i);
1661 if (rule) {
1662 refcount_inc(&rule->node.refcount);
1663 goto rule_found;
1664 }
1665 }
1666
1667 *new_rule = true;
1668 rule = alloc_rule(dest + i);
1669 if (!rule)
1670 goto free_rules;
1671
1672 /* Add dest to dests list- we need flow tables to be in the
1673 * end of the list for forward to next prio rules.
1674 */
1675 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
1676 if (dest &&
1677 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1678 list_add(&rule->node.list, &fte->node.children);
1679 else
1680 list_add_tail(&rule->node.list, &fte->node.children);
1681 if (dest) {
1682 fte->act_dests.dests_size++;
1683
1684 if (is_fwd_dest_type(dest[i].type))
1685 fte->act_dests.fwd_dests++;
1686
1687 type = dest[i].type ==
1688 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1689 *modify_mask |= type ? count : dst;
1690 }
1691 rule_found:
1692 handle->rule[i] = rule;
1693 } while (++i < dest_num);
1694
1695 return handle;
1696
1697 free_rules:
1698 destroy_flow_handle(fte, handle, dest, i);
1699 return ERR_PTR(-ENOMEM);
1700 }
1701
1702 /* fte should not be deleted while calling this function */
1703 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)1704 add_rule_fte(struct fs_fte *fte,
1705 struct mlx5_flow_group *fg,
1706 struct mlx5_flow_destination *dest,
1707 int dest_num,
1708 bool update_action)
1709 {
1710 struct mlx5_flow_root_namespace *root;
1711 struct mlx5_flow_handle *handle;
1712 struct mlx5_flow_table *ft;
1713 int modify_mask = 0;
1714 int err;
1715 bool new_rule = false;
1716
1717 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1718 &new_rule);
1719 if (IS_ERR(handle) || !new_rule)
1720 goto out;
1721
1722 if (update_action)
1723 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
1724
1725 fs_get_obj(ft, fg->node.parent);
1726 root = find_root(&fg->node);
1727 if (!(fte->status & FS_FTE_STATUS_EXISTING))
1728 err = root->cmds->create_fte(root, ft, fg, fte);
1729 else
1730 err = root->cmds->update_fte(root, ft, fg, modify_mask, fte);
1731 if (err)
1732 goto free_handle;
1733
1734 fte->node.active = true;
1735 fte->status |= FS_FTE_STATUS_EXISTING;
1736 atomic_inc(&fg->node.version);
1737
1738 out:
1739 return handle;
1740
1741 free_handle:
1742 destroy_flow_handle(fte, handle, dest, handle->num_rules);
1743 return ERR_PTR(err);
1744 }
1745
alloc_auto_flow_group(struct mlx5_flow_table * ft,const struct mlx5_flow_spec * spec)1746 static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
1747 const struct mlx5_flow_spec *spec)
1748 {
1749 struct list_head *prev = &ft->node.children;
1750 u32 max_fte = ft->autogroup.max_fte;
1751 unsigned int candidate_index = 0;
1752 unsigned int group_size = 0;
1753 struct mlx5_flow_group *fg;
1754
1755 if (!ft->autogroup.active)
1756 return ERR_PTR(-ENOENT);
1757
1758 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
1759 group_size = ft->autogroup.group_size;
1760
1761 /* max_fte == ft->autogroup.max_types */
1762 if (group_size == 0)
1763 group_size = 1;
1764
1765 /* sorted by start_index */
1766 fs_for_each_fg(fg, ft) {
1767 if (candidate_index + group_size > fg->start_index)
1768 candidate_index = fg->start_index + fg->max_ftes;
1769 else
1770 break;
1771 prev = &fg->node.list;
1772 }
1773
1774 if (candidate_index + group_size > max_fte)
1775 return ERR_PTR(-ENOSPC);
1776
1777 fg = alloc_insert_flow_group(ft,
1778 spec->match_criteria_enable,
1779 spec->match_criteria,
1780 candidate_index,
1781 candidate_index + group_size - 1,
1782 prev);
1783 if (IS_ERR(fg))
1784 goto out;
1785
1786 if (group_size == ft->autogroup.group_size)
1787 ft->autogroup.num_groups++;
1788
1789 out:
1790 return fg;
1791 }
1792
create_auto_flow_group(struct mlx5_flow_table * ft,struct mlx5_flow_group * fg)1793 static int create_auto_flow_group(struct mlx5_flow_table *ft,
1794 struct mlx5_flow_group *fg)
1795 {
1796 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
1797 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1798 void *match_criteria_addr;
1799 u8 src_esw_owner_mask_on;
1800 void *misc;
1801 int err;
1802 u32 *in;
1803
1804 in = kvzalloc(inlen, GFP_KERNEL);
1805 if (!in)
1806 return -ENOMEM;
1807
1808 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
1809 fg->mask.match_criteria_enable);
1810 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1811 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1812 fg->max_ftes - 1);
1813
1814 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1815 misc_parameters);
1816 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1817 source_eswitch_owner_vhca_id);
1818 MLX5_SET(create_flow_group_in, in,
1819 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1820
1821 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1822 in, match_criteria);
1823 memcpy(match_criteria_addr, fg->mask.match_criteria,
1824 sizeof(fg->mask.match_criteria));
1825
1826 err = root->cmds->create_flow_group(root, ft, in, fg);
1827 if (!err) {
1828 fg->node.active = true;
1829 trace_mlx5_fs_add_fg(fg);
1830 }
1831
1832 kvfree(in);
1833 return err;
1834 }
1835
mlx5_fs_get_packet_reformat_id(struct mlx5_pkt_reformat * pkt_reformat,u32 * id)1836 int mlx5_fs_get_packet_reformat_id(struct mlx5_pkt_reformat *pkt_reformat,
1837 u32 *id)
1838 {
1839 switch (pkt_reformat->owner) {
1840 case MLX5_FLOW_RESOURCE_OWNER_FW:
1841 *id = pkt_reformat->id;
1842 return 0;
1843 case MLX5_FLOW_RESOURCE_OWNER_SW:
1844 return mlx5_fs_dr_action_get_pkt_reformat_id(pkt_reformat, id);
1845 case MLX5_FLOW_RESOURCE_OWNER_HWS:
1846 return mlx5_fs_hws_action_get_pkt_reformat_id(pkt_reformat, id);
1847 default:
1848 return -EINVAL;
1849 }
1850 }
1851
mlx5_pkt_reformat_cmp(struct mlx5_pkt_reformat * p1,struct mlx5_pkt_reformat * p2)1852 static bool mlx5_pkt_reformat_cmp(struct mlx5_pkt_reformat *p1,
1853 struct mlx5_pkt_reformat *p2)
1854 {
1855 int err1, err2;
1856 u32 id1, id2;
1857
1858 if (p1->owner != p2->owner)
1859 return false;
1860
1861 err1 = mlx5_fs_get_packet_reformat_id(p1, &id1);
1862 err2 = mlx5_fs_get_packet_reformat_id(p2, &id2);
1863
1864 return !err1 && !err2 && id1 == id2;
1865 }
1866
mlx5_flow_dests_cmp(struct mlx5_flow_destination * d1,struct mlx5_flow_destination * d2)1867 static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1868 struct mlx5_flow_destination *d2)
1869 {
1870 if (d1->type == d2->type) {
1871 if (((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
1872 d1->type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) &&
1873 d1->vport.num == d2->vport.num &&
1874 d1->vport.flags == d2->vport.flags &&
1875 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ?
1876 (d1->vport.vhca_id == d2->vport.vhca_id) : true) &&
1877 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
1878 mlx5_pkt_reformat_cmp(d1->vport.pkt_reformat,
1879 d2->vport.pkt_reformat) : true)) ||
1880 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1881 d1->ft == d2->ft) ||
1882 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1883 d1->tir_num == d2->tir_num) ||
1884 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1885 d1->ft_num == d2->ft_num) ||
1886 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER &&
1887 d1->sampler_id == d2->sampler_id) ||
1888 (d1->type == MLX5_FLOW_DESTINATION_TYPE_RANGE &&
1889 d1->range.field == d2->range.field &&
1890 d1->range.hit_ft == d2->range.hit_ft &&
1891 d1->range.miss_ft == d2->range.miss_ft &&
1892 d1->range.min == d2->range.min &&
1893 d1->range.max == d2->range.max))
1894 return true;
1895 }
1896
1897 return false;
1898 }
1899
find_flow_rule(struct fs_fte * fte,struct mlx5_flow_destination * dest)1900 static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1901 struct mlx5_flow_destination *dest)
1902 {
1903 struct mlx5_flow_rule *rule;
1904
1905 list_for_each_entry(rule, &fte->node.children, node.list) {
1906 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1907 return rule;
1908 }
1909 return NULL;
1910 }
1911
check_conflicting_actions_vlan(const struct mlx5_fs_vlan * vlan0,const struct mlx5_fs_vlan * vlan1)1912 static bool check_conflicting_actions_vlan(const struct mlx5_fs_vlan *vlan0,
1913 const struct mlx5_fs_vlan *vlan1)
1914 {
1915 return vlan0->ethtype != vlan1->ethtype ||
1916 vlan0->vid != vlan1->vid ||
1917 vlan0->prio != vlan1->prio;
1918 }
1919
check_conflicting_actions(const struct mlx5_flow_act * act1,const struct mlx5_flow_act * act2)1920 static bool check_conflicting_actions(const struct mlx5_flow_act *act1,
1921 const struct mlx5_flow_act *act2)
1922 {
1923 u32 action1 = act1->action;
1924 u32 action2 = act2->action;
1925 u32 xored_actions;
1926
1927 xored_actions = action1 ^ action2;
1928
1929 /* if one rule only wants to count, it's ok */
1930 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1931 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1932 return false;
1933
1934 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
1935 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
1936 MLX5_FLOW_CONTEXT_ACTION_DECAP |
1937 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1938 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
1939 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1940 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1941 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
1942 return true;
1943
1944 if (action1 & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT &&
1945 act1->pkt_reformat != act2->pkt_reformat)
1946 return true;
1947
1948 if (action1 & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR &&
1949 act1->modify_hdr != act2->modify_hdr)
1950 return true;
1951
1952 if (action1 & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH &&
1953 check_conflicting_actions_vlan(&act1->vlan[0], &act2->vlan[0]))
1954 return true;
1955
1956 if (action1 & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2 &&
1957 check_conflicting_actions_vlan(&act1->vlan[1], &act2->vlan[1]))
1958 return true;
1959
1960 return false;
1961 }
1962
check_conflicting_ftes(struct fs_fte * fte,const struct mlx5_flow_context * flow_context,const struct mlx5_flow_act * flow_act)1963 static int check_conflicting_ftes(struct fs_fte *fte,
1964 const struct mlx5_flow_context *flow_context,
1965 const struct mlx5_flow_act *flow_act)
1966 {
1967 if (check_conflicting_actions(flow_act, &fte->act_dests.action)) {
1968 mlx5_core_warn(get_dev(&fte->node),
1969 "Found two FTEs with conflicting actions\n");
1970 return -EEXIST;
1971 }
1972
1973 if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) &&
1974 fte->act_dests.flow_context.flow_tag != flow_context->flow_tag) {
1975 mlx5_core_warn(get_dev(&fte->node),
1976 "FTE flow tag %u already exists with different flow tag %u\n",
1977 fte->act_dests.flow_context.flow_tag,
1978 flow_context->flow_tag);
1979 return -EEXIST;
1980 }
1981
1982 return 0;
1983 }
1984
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)1985 static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
1986 const struct mlx5_flow_spec *spec,
1987 struct mlx5_flow_act *flow_act,
1988 struct mlx5_flow_destination *dest,
1989 int dest_num,
1990 struct fs_fte *fte)
1991 {
1992 struct mlx5_flow_handle *handle;
1993 int old_action;
1994 int i;
1995 int ret;
1996
1997 ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act);
1998 if (ret)
1999 return ERR_PTR(ret);
2000
2001 old_action = fte->act_dests.action.action;
2002 fte->act_dests.action.action |= flow_act->action;
2003 handle = add_rule_fte(fte, fg, dest, dest_num,
2004 old_action != flow_act->action);
2005 if (IS_ERR(handle)) {
2006 fte->act_dests.action.action = old_action;
2007 return handle;
2008 }
2009 trace_mlx5_fs_set_fte(fte, false);
2010
2011 /* Link newly added rules into the tree. */
2012 for (i = 0; i < handle->num_rules; i++) {
2013 if (!handle->rule[i]->node.parent) {
2014 tree_add_node(&handle->rule[i]->node, &fte->node);
2015 trace_mlx5_fs_add_rule(handle->rule[i]);
2016 }
2017 }
2018 return handle;
2019 }
2020
counter_is_valid(u32 action)2021 static bool counter_is_valid(u32 action)
2022 {
2023 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
2024 MLX5_FLOW_CONTEXT_ACTION_ALLOW |
2025 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
2026 }
2027
dest_is_valid(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_flow_table * ft)2028 static bool dest_is_valid(struct mlx5_flow_destination *dest,
2029 struct mlx5_flow_act *flow_act,
2030 struct mlx5_flow_table *ft)
2031 {
2032 bool ignore_level = flow_act->flags & FLOW_ACT_IGNORE_FLOW_LEVEL;
2033 u32 action = flow_act->action;
2034
2035 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
2036 return counter_is_valid(action);
2037
2038 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
2039 return true;
2040
2041 if (ignore_level) {
2042 if (ft->type != FS_FT_FDB &&
2043 ft->type != FS_FT_NIC_RX &&
2044 ft->type != FS_FT_NIC_TX)
2045 return false;
2046
2047 if (dest->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
2048 ft->type != dest->ft->type)
2049 return false;
2050 }
2051
2052 if (!dest || ((dest->type ==
2053 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
2054 (dest->ft->level <= ft->level && !ignore_level)))
2055 return false;
2056 return true;
2057 }
2058
2059 struct match_list {
2060 struct list_head list;
2061 struct mlx5_flow_group *g;
2062 };
2063
free_match_list(struct match_list * head,bool ft_locked)2064 static void free_match_list(struct match_list *head, bool ft_locked)
2065 {
2066 struct match_list *iter, *match_tmp;
2067
2068 list_for_each_entry_safe(iter, match_tmp, &head->list,
2069 list) {
2070 tree_put_node(&iter->g->node, ft_locked);
2071 list_del(&iter->list);
2072 kfree(iter);
2073 }
2074 }
2075
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)2076 static int build_match_list(struct match_list *match_head,
2077 struct mlx5_flow_table *ft,
2078 const struct mlx5_flow_spec *spec,
2079 struct mlx5_flow_group *fg,
2080 bool ft_locked)
2081 {
2082 struct rhlist_head *tmp, *list;
2083 struct mlx5_flow_group *g;
2084
2085 rcu_read_lock();
2086 INIT_LIST_HEAD(&match_head->list);
2087 /* Collect all fgs which has a matching match_criteria */
2088 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
2089 /* RCU is atomic, we can't execute FW commands here */
2090 rhl_for_each_entry_rcu(g, tmp, list, hash) {
2091 struct match_list *curr_match;
2092
2093 if (fg && fg != g)
2094 continue;
2095
2096 if (unlikely(!tree_get_node(&g->node)))
2097 continue;
2098
2099 curr_match = kmalloc_obj(*curr_match, GFP_ATOMIC);
2100 if (!curr_match) {
2101 rcu_read_unlock();
2102 free_match_list(match_head, ft_locked);
2103 return -ENOMEM;
2104 }
2105 curr_match->g = g;
2106 list_add_tail(&curr_match->list, &match_head->list);
2107 }
2108 rcu_read_unlock();
2109 return 0;
2110 }
2111
matched_fgs_get_version(struct list_head * match_head)2112 static u64 matched_fgs_get_version(struct list_head *match_head)
2113 {
2114 struct match_list *iter;
2115 u64 version = 0;
2116
2117 list_for_each_entry(iter, match_head, list)
2118 version += (u64)atomic_read(&iter->g->node.version);
2119 return version;
2120 }
2121
2122 static struct fs_fte *
lookup_fte_locked(struct mlx5_flow_group * g,const u32 * match_value,bool take_write)2123 lookup_fte_locked(struct mlx5_flow_group *g,
2124 const u32 *match_value,
2125 bool take_write)
2126 {
2127 struct fs_fte *fte_tmp;
2128
2129 if (take_write)
2130 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
2131 else
2132 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
2133 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
2134 rhash_fte);
2135 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
2136 fte_tmp = NULL;
2137 goto out;
2138 }
2139
2140 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
2141
2142 if (!fte_tmp->node.active) {
2143 up_write_ref_node(&fte_tmp->node, false);
2144
2145 if (take_write)
2146 up_write_ref_node(&g->node, false);
2147 else
2148 up_read_ref_node(&g->node);
2149
2150 tree_put_node(&fte_tmp->node, false);
2151
2152 return NULL;
2153 }
2154
2155 out:
2156 if (take_write)
2157 up_write_ref_node(&g->node, false);
2158 else
2159 up_read_ref_node(&g->node);
2160 return fte_tmp;
2161 }
2162
2163 /* Native capability lacks support for adding an additional match with the same value
2164 * to the same flow group. To accommodate the NO APPEND flag in these scenarios,
2165 * we include the new rule in the existing flow table entry (fte) without immediate
2166 * hardware commitment. When a request is made to delete the corresponding hardware rule,
2167 * we then commit the pending rule to hardware.
2168 */
2169 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)2170 add_rule_dup_match_fte(struct fs_fte *fte,
2171 const struct mlx5_flow_spec *spec,
2172 struct mlx5_flow_act *flow_act,
2173 struct mlx5_flow_destination *dest,
2174 int dest_num)
2175 {
2176 struct mlx5_flow_handle *handle;
2177 struct fs_fte_dup *dup;
2178 int i = 0;
2179
2180 if (!fte->dup) {
2181 dup = kvzalloc_obj(*dup);
2182 if (!dup)
2183 return ERR_PTR(-ENOMEM);
2184 /* dup will be freed when the fte is freed
2185 * this way we don't allocate / free dup on every rule deletion
2186 * or creation
2187 */
2188 INIT_LIST_HEAD(&dup->children);
2189 fte->dup = dup;
2190 }
2191
2192 if (!list_empty(&fte->dup->children)) {
2193 mlx5_core_warn(get_dev(&fte->node),
2194 "Can have only a single duplicate rule\n");
2195
2196 return ERR_PTR(-EEXIST);
2197 }
2198
2199 fte->dup->act_dests.action = *flow_act;
2200 fte->dup->act_dests.flow_context = spec->flow_context;
2201 fte->dup->act_dests.dests_size = 0;
2202 fte->dup->act_dests.fwd_dests = 0;
2203 fte->dup->act_dests.modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
2204
2205 handle = create_flow_handle_dup(&fte->dup->children,
2206 dest, dest_num,
2207 &fte->dup->act_dests);
2208 if (!handle)
2209 return ERR_PTR(-ENOMEM);
2210
2211 for (i = 0; i < handle->num_rules; i++) {
2212 tree_add_node(&handle->rule[i]->node, &fte->node);
2213 trace_mlx5_fs_add_rule(handle->rule[i]);
2214 }
2215
2216 return handle;
2217 }
2218
2219 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)2220 try_add_to_existing_fg(struct mlx5_flow_table *ft,
2221 struct list_head *match_head,
2222 const struct mlx5_flow_spec *spec,
2223 struct mlx5_flow_act *flow_act,
2224 struct mlx5_flow_destination *dest,
2225 int dest_num,
2226 int ft_version)
2227 {
2228 struct mlx5_flow_steering *steering = get_steering(&ft->node);
2229 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2230 struct mlx5_flow_group *g;
2231 struct mlx5_flow_handle *rule;
2232 struct match_list *iter;
2233 bool take_write = false;
2234 bool try_again = false;
2235 struct fs_fte *fte;
2236 u64 version = 0;
2237 int err;
2238
2239 fte = alloc_fte(ft, spec, flow_act);
2240 if (IS_ERR(fte))
2241 return ERR_PTR(-ENOMEM);
2242
2243 search_again_locked:
2244 if (flow_act->flags & FLOW_ACT_NO_APPEND &&
2245 (root->cmds->get_capabilities(root, root->table_type) &
2246 MLX5_FLOW_STEERING_CAP_DUPLICATE_MATCH))
2247 goto skip_search;
2248 version = matched_fgs_get_version(match_head);
2249 /* Try to find an fte with identical match value and attempt update its
2250 * action.
2251 */
2252 list_for_each_entry(iter, match_head, list) {
2253 struct fs_fte *fte_tmp;
2254
2255 g = iter->g;
2256 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
2257 if (!fte_tmp)
2258 continue;
2259 if (flow_act->flags & FLOW_ACT_NO_APPEND)
2260 rule = add_rule_dup_match_fte(fte_tmp, spec, flow_act, dest, dest_num);
2261 else
2262 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp);
2263 /* No error check needed here, because insert_fte() is not called */
2264 up_write_ref_node(&fte_tmp->node, false);
2265 tree_put_node(&fte_tmp->node, false);
2266 kmem_cache_free(steering->ftes_cache, fte);
2267 return rule;
2268 }
2269
2270 skip_search:
2271 /* No group with matching fte found, or we skipped the search.
2272 * Try to add a new fte to any matching fg.
2273 */
2274
2275 /* Check the ft version, for case that new flow group
2276 * was added while the fgs weren't locked
2277 */
2278 if (atomic_read(&ft->node.version) != ft_version) {
2279 rule = ERR_PTR(-EAGAIN);
2280 goto out;
2281 }
2282
2283 /* Check the fgs version. If version have changed it could be that an
2284 * FTE with the same match value was added while the fgs weren't
2285 * locked.
2286 */
2287 if (!(flow_act->flags & FLOW_ACT_NO_APPEND) &&
2288 version != matched_fgs_get_version(match_head)) {
2289 take_write = true;
2290 goto search_again_locked;
2291 }
2292
2293 list_for_each_entry(iter, match_head, list) {
2294 g = iter->g;
2295
2296 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
2297
2298 if (!g->node.active) {
2299 try_again = true;
2300 up_write_ref_node(&g->node, false);
2301 continue;
2302 }
2303
2304 err = insert_fte(g, fte);
2305 if (err) {
2306 up_write_ref_node(&g->node, false);
2307 if (err == -ENOSPC)
2308 continue;
2309 kmem_cache_free(steering->ftes_cache, fte);
2310 return ERR_PTR(err);
2311 }
2312
2313 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
2314 up_write_ref_node(&g->node, false);
2315 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
2316 up_write_ref_node(&fte->node, false);
2317 if (IS_ERR(rule))
2318 tree_put_node(&fte->node, false);
2319 return rule;
2320 }
2321 err = try_again ? -EAGAIN : -ENOENT;
2322 rule = ERR_PTR(err);
2323 out:
2324 kmem_cache_free(steering->ftes_cache, fte);
2325 return rule;
2326 }
2327
2328 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)2329 _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
2330 const struct mlx5_flow_spec *spec,
2331 struct mlx5_flow_act *flow_act,
2332 struct mlx5_flow_destination *dest,
2333 int dest_num)
2334
2335 {
2336 struct mlx5_flow_steering *steering = get_steering(&ft->node);
2337 struct mlx5_flow_handle *rule;
2338 struct match_list match_head;
2339 struct mlx5_flow_group *g;
2340 bool take_write = false;
2341 struct fs_fte *fte;
2342 int version;
2343 int err;
2344 int i;
2345
2346 if (!check_valid_spec(spec))
2347 return ERR_PTR(-EINVAL);
2348
2349 if (flow_act->fg && ft->autogroup.active)
2350 return ERR_PTR(-EINVAL);
2351
2352 if (dest && dest_num <= 0)
2353 return ERR_PTR(-EINVAL);
2354
2355 for (i = 0; i < dest_num; i++) {
2356 if (!dest_is_valid(&dest[i], flow_act, ft))
2357 return ERR_PTR(-EINVAL);
2358 }
2359 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
2360 search_again_locked:
2361 version = atomic_read(&ft->node.version);
2362
2363 /* Collect all fgs which has a matching match_criteria */
2364 err = build_match_list(&match_head, ft, spec, flow_act->fg, take_write);
2365 if (err) {
2366 if (take_write)
2367 up_write_ref_node(&ft->node, false);
2368 else
2369 up_read_ref_node(&ft->node);
2370 return ERR_PTR(err);
2371 }
2372
2373 if (!take_write)
2374 up_read_ref_node(&ft->node);
2375
2376 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
2377 dest_num, version);
2378 free_match_list(&match_head, take_write);
2379 if (!IS_ERR(rule) ||
2380 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
2381 if (take_write)
2382 up_write_ref_node(&ft->node, false);
2383 return rule;
2384 }
2385
2386 if (!take_write) {
2387 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
2388 take_write = true;
2389 }
2390
2391 if (PTR_ERR(rule) == -EAGAIN ||
2392 version != atomic_read(&ft->node.version))
2393 goto search_again_locked;
2394
2395 g = alloc_auto_flow_group(ft, spec);
2396 if (IS_ERR(g)) {
2397 rule = ERR_CAST(g);
2398 up_write_ref_node(&ft->node, false);
2399 return rule;
2400 }
2401
2402 fte = alloc_fte(ft, spec, flow_act);
2403 if (IS_ERR(fte)) {
2404 up_write_ref_node(&ft->node, false);
2405 err = PTR_ERR(fte);
2406 goto err_alloc_fte;
2407 }
2408
2409 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
2410 up_write_ref_node(&ft->node, false);
2411
2412 err = create_auto_flow_group(ft, g);
2413 if (err)
2414 goto err_release_fg;
2415
2416 err = insert_fte(g, fte);
2417 if (err)
2418 goto err_release_fg;
2419
2420 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
2421 up_write_ref_node(&g->node, false);
2422 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
2423 up_write_ref_node(&fte->node, false);
2424 if (IS_ERR(rule))
2425 tree_put_node(&fte->node, false);
2426 tree_put_node(&g->node, false);
2427 return rule;
2428
2429 err_release_fg:
2430 up_write_ref_node(&g->node, false);
2431 kmem_cache_free(steering->ftes_cache, fte);
2432 err_alloc_fte:
2433 tree_put_node(&g->node, false);
2434 return ERR_PTR(err);
2435 }
2436
fwd_next_prio_supported(struct mlx5_flow_table * ft)2437 static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
2438 {
2439 return ((ft->type == FS_FT_NIC_RX) &&
2440 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
2441 }
2442
2443 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)2444 mlx5_add_flow_rules(struct mlx5_flow_table *ft,
2445 const struct mlx5_flow_spec *spec,
2446 struct mlx5_flow_act *flow_act,
2447 struct mlx5_flow_destination *dest,
2448 int num_dest)
2449 {
2450 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2451 static const struct mlx5_flow_spec zero_spec = {};
2452 struct mlx5_flow_destination *gen_dest = NULL;
2453 struct mlx5_flow_table *next_ft = NULL;
2454 struct mlx5_flow_handle *handle = NULL;
2455 u32 sw_action = flow_act->action;
2456 int i;
2457
2458 if (!spec)
2459 spec = &zero_spec;
2460
2461 if (!is_fwd_next_action(sw_action))
2462 return _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
2463
2464 if (!fwd_next_prio_supported(ft))
2465 return ERR_PTR(-EOPNOTSUPP);
2466
2467 mutex_lock(&root->chain_lock);
2468 next_ft = find_next_fwd_ft(ft, flow_act);
2469 if (!next_ft) {
2470 handle = ERR_PTR(-EOPNOTSUPP);
2471 goto unlock;
2472 }
2473
2474 gen_dest = kzalloc_objs(*dest, num_dest + 1);
2475 if (!gen_dest) {
2476 handle = ERR_PTR(-ENOMEM);
2477 goto unlock;
2478 }
2479 for (i = 0; i < num_dest; i++)
2480 gen_dest[i] = dest[i];
2481 gen_dest[i].type =
2482 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
2483 gen_dest[i].ft = next_ft;
2484 dest = gen_dest;
2485 num_dest++;
2486 flow_act->action &= ~(MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
2487 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
2488 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2489 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
2490 if (IS_ERR(handle))
2491 goto unlock;
2492
2493 if (list_empty(&handle->rule[num_dest - 1]->next_ft)) {
2494 mutex_lock(&next_ft->lock);
2495 list_add(&handle->rule[num_dest - 1]->next_ft,
2496 &next_ft->fwd_rules);
2497 mutex_unlock(&next_ft->lock);
2498 handle->rule[num_dest - 1]->sw_action = sw_action;
2499 handle->rule[num_dest - 1]->ft = ft;
2500 }
2501 unlock:
2502 mutex_unlock(&root->chain_lock);
2503 kfree(gen_dest);
2504 return handle;
2505 }
2506 EXPORT_SYMBOL(mlx5_add_flow_rules);
2507
mlx5_del_flow_rules(struct mlx5_flow_handle * handle)2508 void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
2509 {
2510 struct fs_fte *fte;
2511 int i;
2512
2513 /* In order to consolidate the HW changes we lock the FTE for other
2514 * changes, and increase its refcount, in order not to perform the
2515 * "del" functions of the FTE. Will handle them here.
2516 * The removal of the rules is done under locked FTE.
2517 * After removing all the handle's rules, if there are remaining
2518 * rules, it means we just need to modify the FTE in FW, and
2519 * unlock/decrease the refcount we increased before.
2520 * Otherwise, it means the FTE should be deleted. First delete the
2521 * FTE in FW. Then, unlock the FTE, and proceed the tree_put_node of
2522 * the FTE, which will handle the last decrease of the refcount, as
2523 * well as required handling of its parent.
2524 */
2525 fs_get_obj(fte, handle->rule[0]->node.parent);
2526 down_write_ref_node(&fte->node, false);
2527 for (i = handle->num_rules - 1; i >= 0; i--)
2528 tree_remove_node(&handle->rule[i]->node, true);
2529 if (list_empty(&fte->node.children)) {
2530 fte->node.del_hw_func(&fte->node);
2531 up_write_ref_node(&fte->node, false);
2532 tree_put_node(&fte->node, false);
2533 } else if (fte->act_dests.dests_size) {
2534 if (fte->act_dests.modify_mask)
2535 modify_fte(fte);
2536 up_write_ref_node(&fte->node, false);
2537 } else {
2538 up_write_ref_node(&fte->node, false);
2539 }
2540 kfree(handle);
2541 }
2542 EXPORT_SYMBOL(mlx5_del_flow_rules);
2543
2544 /* Assuming prio->node.children(flow tables) is sorted by level */
find_next_ft(struct mlx5_flow_table * ft)2545 static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
2546 {
2547 struct fs_node *prio_parent, *child;
2548 struct fs_prio *prio;
2549
2550 fs_get_obj(prio, ft->node.parent);
2551
2552 if (!list_is_last(&ft->node.list, &prio->node.children))
2553 return list_next_entry(ft, node.list);
2554
2555 prio_parent = find_prio_chains_parent(&prio->node, &child);
2556
2557 if (prio_parent && list_is_first(&child->list, &prio_parent->children))
2558 return find_closest_ft(&prio->node, false, false);
2559
2560 return find_next_chained_ft(&prio->node);
2561 }
2562
update_root_ft_destroy(struct mlx5_flow_table * ft)2563 static int update_root_ft_destroy(struct mlx5_flow_table *ft)
2564 {
2565 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2566 struct mlx5_ft_underlay_qp *uqp;
2567 struct mlx5_flow_table *new_root_ft = NULL;
2568 int err = 0;
2569 u32 qpn;
2570
2571 if (root->root_ft != ft)
2572 return 0;
2573
2574 new_root_ft = find_next_ft(ft);
2575 if (!new_root_ft) {
2576 root->root_ft = NULL;
2577 return 0;
2578 }
2579
2580 if (list_empty(&root->underlay_qpns)) {
2581 /* Don't set any QPN (zero) in case QPN list is empty */
2582 qpn = 0;
2583 err = root->cmds->update_root_ft(root, new_root_ft,
2584 qpn, false);
2585 } else {
2586 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2587 qpn = uqp->qpn;
2588 err = root->cmds->update_root_ft(root,
2589 new_root_ft, qpn,
2590 false);
2591 if (err)
2592 break;
2593 }
2594 }
2595
2596 if (err)
2597 mlx5_core_warn(root->dev,
2598 "Update root flow table of id(%u) qpn(%d) failed\n",
2599 ft->id, qpn);
2600 else
2601 root->root_ft = new_root_ft;
2602
2603 return 0;
2604 }
2605
2606 /* Connect flow table from previous priority to
2607 * the next flow table.
2608 */
disconnect_flow_table(struct mlx5_flow_table * ft)2609 static int disconnect_flow_table(struct mlx5_flow_table *ft)
2610 {
2611 struct mlx5_core_dev *dev = get_dev(&ft->node);
2612 struct mlx5_flow_table *next_ft;
2613 struct fs_prio *prio;
2614 int err = 0;
2615
2616 err = update_root_ft_destroy(ft);
2617 if (err)
2618 return err;
2619
2620 fs_get_obj(prio, ft->node.parent);
2621 if (!(list_first_entry(&prio->node.children,
2622 struct mlx5_flow_table,
2623 node.list) == ft))
2624 return 0;
2625
2626 next_ft = find_next_ft(ft);
2627 err = connect_fwd_rules(dev, next_ft, ft);
2628 if (err)
2629 return err;
2630
2631 err = connect_prev_fts(dev, next_ft, prio);
2632 if (err)
2633 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2634 ft->id);
2635 return err;
2636 }
2637
mlx5_destroy_flow_table(struct mlx5_flow_table * ft)2638 int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
2639 {
2640 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2641 int err = 0;
2642
2643 mutex_lock(&root->chain_lock);
2644 if (!(ft->flags & MLX5_FLOW_TABLE_UNMANAGED))
2645 err = disconnect_flow_table(ft);
2646 if (err) {
2647 mutex_unlock(&root->chain_lock);
2648 return err;
2649 }
2650 if (tree_remove_node(&ft->node, false))
2651 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2652 ft->id);
2653 mutex_unlock(&root->chain_lock);
2654
2655 return err;
2656 }
2657 EXPORT_SYMBOL(mlx5_destroy_flow_table);
2658
mlx5_destroy_flow_group(struct mlx5_flow_group * fg)2659 void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
2660 {
2661 if (tree_remove_node(&fg->node, false))
2662 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2663 fg->id);
2664 }
2665 EXPORT_SYMBOL(mlx5_destroy_flow_group);
2666
mlx5_get_fdb_sub_ns(struct mlx5_core_dev * dev,int n)2667 struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
2668 int n)
2669 {
2670 struct mlx5_flow_steering *steering = dev->priv.steering;
2671
2672 if (!steering || !steering->fdb_sub_ns)
2673 return NULL;
2674
2675 return steering->fdb_sub_ns[n];
2676 }
2677 EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
2678
is_nic_rx_ns(enum mlx5_flow_namespace_type type)2679 static bool is_nic_rx_ns(enum mlx5_flow_namespace_type type)
2680 {
2681 switch (type) {
2682 case MLX5_FLOW_NAMESPACE_BYPASS:
2683 case MLX5_FLOW_NAMESPACE_KERNEL_RX_MACSEC:
2684 case MLX5_FLOW_NAMESPACE_LAG:
2685 case MLX5_FLOW_NAMESPACE_OFFLOADS:
2686 case MLX5_FLOW_NAMESPACE_ETHTOOL:
2687 case MLX5_FLOW_NAMESPACE_KERNEL:
2688 case MLX5_FLOW_NAMESPACE_LEFTOVERS:
2689 case MLX5_FLOW_NAMESPACE_ANCHOR:
2690 return true;
2691 default:
2692 return false;
2693 }
2694 }
2695
mlx5_get_flow_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type)2696 struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2697 enum mlx5_flow_namespace_type type)
2698 {
2699 struct mlx5_flow_steering *steering = dev->priv.steering;
2700 struct mlx5_flow_root_namespace *root_ns;
2701 int prio = 0;
2702 struct fs_prio *fs_prio;
2703 struct mlx5_flow_namespace *ns;
2704
2705 if (!steering)
2706 return NULL;
2707
2708 switch (type) {
2709 case MLX5_FLOW_NAMESPACE_FDB:
2710 if (steering->fdb_root_ns)
2711 return &steering->fdb_root_ns->ns;
2712 return NULL;
2713 case MLX5_FLOW_NAMESPACE_PORT_SEL:
2714 if (steering->port_sel_root_ns)
2715 return &steering->port_sel_root_ns->ns;
2716 return NULL;
2717 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2718 if (steering->sniffer_rx_root_ns)
2719 return &steering->sniffer_rx_root_ns->ns;
2720 return NULL;
2721 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2722 if (steering->sniffer_tx_root_ns)
2723 return &steering->sniffer_tx_root_ns->ns;
2724 return NULL;
2725 case MLX5_FLOW_NAMESPACE_FDB_BYPASS:
2726 root_ns = steering->fdb_root_ns;
2727 prio = FDB_BYPASS_PATH;
2728 break;
2729 case MLX5_FLOW_NAMESPACE_EGRESS:
2730 case MLX5_FLOW_NAMESPACE_EGRESS_IPSEC:
2731 case MLX5_FLOW_NAMESPACE_EGRESS_MACSEC:
2732 root_ns = steering->egress_root_ns;
2733 prio = type - MLX5_FLOW_NAMESPACE_EGRESS;
2734 break;
2735 case MLX5_FLOW_NAMESPACE_RDMA_RX:
2736 root_ns = steering->rdma_rx_root_ns;
2737 prio = RDMA_RX_BYPASS_PRIO;
2738 break;
2739 case MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL:
2740 root_ns = steering->rdma_rx_root_ns;
2741 prio = RDMA_RX_KERNEL_PRIO;
2742 break;
2743 case MLX5_FLOW_NAMESPACE_RDMA_TX:
2744 root_ns = steering->rdma_tx_root_ns;
2745 prio = RDMA_TX_BYPASS_PRIO;
2746 break;
2747 case MLX5_FLOW_NAMESPACE_RDMA_RX_COUNTERS:
2748 root_ns = steering->rdma_rx_root_ns;
2749 prio = RDMA_RX_COUNTERS_PRIO;
2750 break;
2751 case MLX5_FLOW_NAMESPACE_RDMA_TX_COUNTERS:
2752 root_ns = steering->rdma_tx_root_ns;
2753 prio = RDMA_TX_COUNTERS_PRIO;
2754 break;
2755 case MLX5_FLOW_NAMESPACE_RDMA_RX_IPSEC:
2756 root_ns = steering->rdma_rx_root_ns;
2757 prio = RDMA_RX_IPSEC_PRIO;
2758 break;
2759 case MLX5_FLOW_NAMESPACE_RDMA_TX_IPSEC:
2760 root_ns = steering->rdma_tx_root_ns;
2761 prio = RDMA_TX_IPSEC_PRIO;
2762 break;
2763 case MLX5_FLOW_NAMESPACE_RDMA_RX_MACSEC:
2764 root_ns = steering->rdma_rx_root_ns;
2765 prio = RDMA_RX_MACSEC_PRIO;
2766 break;
2767 case MLX5_FLOW_NAMESPACE_RDMA_TX_MACSEC:
2768 root_ns = steering->rdma_tx_root_ns;
2769 prio = RDMA_TX_MACSEC_PRIO;
2770 break;
2771 default: /* Must be NIC RX */
2772 WARN_ON(!is_nic_rx_ns(type));
2773 root_ns = steering->root_ns;
2774 prio = type;
2775 break;
2776 }
2777
2778 if (!root_ns)
2779 return NULL;
2780
2781 fs_prio = find_prio(&root_ns->ns, prio);
2782 if (!fs_prio)
2783 return NULL;
2784
2785 ns = list_first_entry(&fs_prio->node.children,
2786 typeof(*ns),
2787 node.list);
2788
2789 return ns;
2790 }
2791 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2792
2793 struct mlx5_vport_acl_root_ns {
2794 u16 vport_idx;
2795 struct mlx5_flow_root_namespace *root_ns;
2796 };
2797
2798 struct mlx5_flow_namespace *
mlx5_get_flow_vport_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type,int vport_idx)2799 mlx5_get_flow_vport_namespace(struct mlx5_core_dev *dev,
2800 enum mlx5_flow_namespace_type type, int vport_idx)
2801 {
2802 struct mlx5_flow_steering *steering = dev->priv.steering;
2803 struct mlx5_vport_acl_root_ns *vport_ns;
2804
2805 if (!steering)
2806 return NULL;
2807
2808 switch (type) {
2809 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2810 vport_ns = xa_load(&steering->esw_egress_root_ns, vport_idx);
2811 if (vport_ns)
2812 return &vport_ns->root_ns->ns;
2813 else
2814 return NULL;
2815 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2816 vport_ns = xa_load(&steering->esw_ingress_root_ns, vport_idx);
2817 if (vport_ns)
2818 return &vport_ns->root_ns->ns;
2819 else
2820 return NULL;
2821 case MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_RX:
2822 if (vport_idx >= steering->rdma_transport_rx_vports)
2823 return NULL;
2824 if (steering->rdma_transport_rx_root_ns &&
2825 steering->rdma_transport_rx_root_ns[vport_idx])
2826 return &steering->rdma_transport_rx_root_ns[vport_idx]->ns;
2827 else
2828 return NULL;
2829 case MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_TX:
2830 if (vport_idx >= steering->rdma_transport_tx_vports)
2831 return NULL;
2832
2833 if (steering->rdma_transport_tx_root_ns &&
2834 steering->rdma_transport_tx_root_ns[vport_idx])
2835 return &steering->rdma_transport_tx_root_ns[vport_idx]->ns;
2836 else
2837 return NULL;
2838 default:
2839 return NULL;
2840 }
2841 }
2842 EXPORT_SYMBOL(mlx5_get_flow_vport_namespace);
2843
_fs_create_prio(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels,enum fs_node_type type)2844 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2845 unsigned int prio,
2846 int num_levels,
2847 enum fs_node_type type)
2848 {
2849 struct fs_prio *fs_prio;
2850
2851 fs_prio = kzalloc_obj(*fs_prio);
2852 if (!fs_prio)
2853 return ERR_PTR(-ENOMEM);
2854
2855 fs_prio->node.type = type;
2856 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2857 tree_add_node(&fs_prio->node, &ns->node);
2858 fs_prio->num_levels = num_levels;
2859 fs_prio->prio = prio;
2860 list_add_tail(&fs_prio->node.list, &ns->node.children);
2861
2862 return fs_prio;
2863 }
2864
fs_create_prio_chained(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels)2865 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2866 unsigned int prio,
2867 int num_levels)
2868 {
2869 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2870 }
2871
fs_create_prio(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels)2872 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2873 unsigned int prio, int num_levels)
2874 {
2875 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2876 }
2877
fs_init_namespace(struct mlx5_flow_namespace * ns)2878 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2879 *ns)
2880 {
2881 ns->node.type = FS_TYPE_NAMESPACE;
2882
2883 return ns;
2884 }
2885
fs_create_namespace(struct fs_prio * prio,int def_miss_act)2886 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2887 int def_miss_act)
2888 {
2889 struct mlx5_flow_namespace *ns;
2890
2891 ns = kzalloc_obj(*ns);
2892 if (!ns)
2893 return ERR_PTR(-ENOMEM);
2894
2895 fs_init_namespace(ns);
2896 ns->def_miss_action = def_miss_act;
2897 tree_init_node(&ns->node, NULL, del_sw_ns);
2898 tree_add_node(&ns->node, &prio->node);
2899 list_add_tail(&ns->node.list, &prio->node.children);
2900
2901 return ns;
2902 }
2903
create_leaf_prios(struct mlx5_flow_namespace * ns,int prio,struct init_tree_node * prio_metadata)2904 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2905 struct init_tree_node *prio_metadata)
2906 {
2907 struct fs_prio *fs_prio;
2908 int i;
2909
2910 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2911 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2912 if (IS_ERR(fs_prio))
2913 return PTR_ERR(fs_prio);
2914 }
2915 return 0;
2916 }
2917
2918 #define FLOW_TABLE_BIT_SZ 1
2919 #define GET_FLOW_TABLE_CAP(dev, offset) \
2920 ((be32_to_cpu(*((__be32 *)(dev->caps.hca[MLX5_CAP_FLOW_TABLE]->cur) + \
2921 offset / 32)) >> \
2922 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
has_required_caps(struct mlx5_core_dev * dev,struct node_caps * caps)2923 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2924 {
2925 int i;
2926
2927 for (i = 0; i < caps->arr_sz; i++) {
2928 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2929 return false;
2930 }
2931 return true;
2932 }
2933
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)2934 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2935 struct init_tree_node *init_node,
2936 struct fs_node *fs_parent_node,
2937 struct init_tree_node *init_parent_node,
2938 int prio)
2939 {
2940 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2941 flow_table_properties_nic_receive.
2942 max_ft_level);
2943 struct mlx5_flow_namespace *fs_ns;
2944 struct fs_prio *fs_prio;
2945 struct fs_node *base;
2946 int i;
2947 int err;
2948
2949 if (init_node->type == FS_TYPE_PRIO) {
2950 if ((init_node->min_ft_level > max_ft_level) ||
2951 !has_required_caps(steering->dev, &init_node->caps))
2952 return 0;
2953
2954 fs_get_obj(fs_ns, fs_parent_node);
2955 if (init_node->num_leaf_prios)
2956 return create_leaf_prios(fs_ns, prio, init_node);
2957 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2958 if (IS_ERR(fs_prio))
2959 return PTR_ERR(fs_prio);
2960 base = &fs_prio->node;
2961 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2962 fs_get_obj(fs_prio, fs_parent_node);
2963 fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
2964 if (IS_ERR(fs_ns))
2965 return PTR_ERR(fs_ns);
2966 base = &fs_ns->node;
2967 } else {
2968 return -EINVAL;
2969 }
2970 prio = 0;
2971 for (i = 0; i < init_node->ar_size; i++) {
2972 err = init_root_tree_recursive(steering, &init_node->children[i],
2973 base, init_node, prio);
2974 if (err)
2975 return err;
2976 if (init_node->children[i].type == FS_TYPE_PRIO &&
2977 init_node->children[i].num_leaf_prios) {
2978 prio += init_node->children[i].num_leaf_prios;
2979 }
2980 }
2981
2982 return 0;
2983 }
2984
init_root_tree(struct mlx5_flow_steering * steering,struct init_tree_node * init_node,struct fs_node * fs_parent_node)2985 static int init_root_tree(struct mlx5_flow_steering *steering,
2986 struct init_tree_node *init_node,
2987 struct fs_node *fs_parent_node)
2988 {
2989 int err;
2990 int i;
2991
2992 for (i = 0; i < init_node->ar_size; i++) {
2993 err = init_root_tree_recursive(steering, &init_node->children[i],
2994 fs_parent_node,
2995 init_node, i);
2996 if (err)
2997 return err;
2998 }
2999 return 0;
3000 }
3001
del_sw_root_ns(struct fs_node * node)3002 static void del_sw_root_ns(struct fs_node *node)
3003 {
3004 struct mlx5_flow_root_namespace *root_ns;
3005 struct mlx5_flow_namespace *ns;
3006
3007 fs_get_obj(ns, node);
3008 root_ns = container_of(ns, struct mlx5_flow_root_namespace, ns);
3009 mutex_destroy(&root_ns->chain_lock);
3010 kfree(node);
3011 }
3012
3013 static struct mlx5_flow_root_namespace
create_root_ns(struct mlx5_flow_steering * steering,enum fs_flow_table_type table_type)3014 *create_root_ns(struct mlx5_flow_steering *steering,
3015 enum fs_flow_table_type table_type)
3016 {
3017 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
3018 struct mlx5_flow_root_namespace *root_ns;
3019 struct mlx5_flow_namespace *ns;
3020
3021 /* Create the root namespace */
3022 root_ns = kzalloc_obj(*root_ns);
3023 if (!root_ns)
3024 return NULL;
3025
3026 root_ns->dev = steering->dev;
3027 root_ns->table_type = table_type;
3028 root_ns->cmds = cmds;
3029
3030 INIT_LIST_HEAD(&root_ns->underlay_qpns);
3031
3032 ns = &root_ns->ns;
3033 fs_init_namespace(ns);
3034 mutex_init(&root_ns->chain_lock);
3035 tree_init_node(&ns->node, NULL, del_sw_root_ns);
3036 tree_add_node(&ns->node, NULL);
3037
3038 return root_ns;
3039 }
3040
3041 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
3042
set_prio_attrs_in_ns(struct mlx5_flow_namespace * ns,int acc_level)3043 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
3044 {
3045 struct fs_prio *prio;
3046
3047 fs_for_each_prio(prio, ns) {
3048 /* This updates prio start_level and num_levels */
3049 set_prio_attrs_in_prio(prio, acc_level);
3050 acc_level += prio->num_levels;
3051 }
3052 return acc_level;
3053 }
3054
set_prio_attrs_in_prio(struct fs_prio * prio,int acc_level)3055 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
3056 {
3057 struct mlx5_flow_namespace *ns;
3058 int acc_level_ns = acc_level;
3059
3060 prio->start_level = acc_level;
3061 fs_for_each_ns(ns, prio) {
3062 /* This updates start_level and num_levels of ns's priority descendants */
3063 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
3064
3065 /* If this a prio with chains, and we can jump from one chain
3066 * (namespace) to another, so we accumulate the levels
3067 */
3068 if (prio->node.type == FS_TYPE_PRIO_CHAINS)
3069 acc_level = acc_level_ns;
3070 }
3071
3072 if (!prio->num_levels)
3073 prio->num_levels = acc_level_ns - prio->start_level;
3074 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
3075 }
3076
set_prio_attrs(struct mlx5_flow_root_namespace * root_ns)3077 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
3078 {
3079 struct mlx5_flow_namespace *ns = &root_ns->ns;
3080 struct fs_prio *prio;
3081 int start_level = 0;
3082
3083 fs_for_each_prio(prio, ns) {
3084 set_prio_attrs_in_prio(prio, start_level);
3085 start_level += prio->num_levels;
3086 }
3087 }
3088
3089 #define ANCHOR_PRIO 0
3090 #define ANCHOR_SIZE 1
3091 #define ANCHOR_LEVEL 0
create_anchor_flow_table(struct mlx5_flow_steering * steering)3092 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
3093 {
3094 struct mlx5_flow_namespace *ns = NULL;
3095 struct mlx5_flow_table_attr ft_attr = {};
3096 struct mlx5_flow_table *ft;
3097
3098 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
3099 if (WARN_ON(!ns))
3100 return -EINVAL;
3101
3102 ft_attr.max_fte = ANCHOR_SIZE;
3103 ft_attr.level = ANCHOR_LEVEL;
3104 ft_attr.prio = ANCHOR_PRIO;
3105
3106 ft = mlx5_create_flow_table(ns, &ft_attr);
3107 if (IS_ERR(ft)) {
3108 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
3109 return PTR_ERR(ft);
3110 }
3111 return 0;
3112 }
3113
init_root_ns(struct mlx5_flow_steering * steering)3114 static int init_root_ns(struct mlx5_flow_steering *steering)
3115 {
3116 int err;
3117
3118 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
3119 if (!steering->root_ns)
3120 return -ENOMEM;
3121
3122 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
3123 if (err)
3124 goto out_err;
3125
3126 set_prio_attrs(steering->root_ns);
3127 err = create_anchor_flow_table(steering);
3128 if (err)
3129 goto out_err;
3130
3131 return 0;
3132
3133 out_err:
3134 cleanup_root_ns(steering->root_ns);
3135 steering->root_ns = NULL;
3136 return err;
3137 }
3138
clean_tree(struct fs_node * node)3139 static void clean_tree(struct fs_node *node)
3140 {
3141 if (node) {
3142 struct fs_node *iter;
3143 struct fs_node *temp;
3144
3145 tree_get_node(node);
3146 list_for_each_entry_safe(iter, temp, &node->children, list)
3147 clean_tree(iter);
3148 tree_put_node(node, false);
3149 tree_remove_node(node, false);
3150 }
3151 }
3152
cleanup_root_ns(struct mlx5_flow_root_namespace * root_ns)3153 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
3154 {
3155 if (!root_ns)
3156 return;
3157
3158 clean_tree(&root_ns->ns.node);
3159 }
3160
init_sniffer_tx_root_ns(struct mlx5_flow_steering * steering)3161 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
3162 {
3163 struct fs_prio *prio;
3164
3165 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
3166 if (!steering->sniffer_tx_root_ns)
3167 return -ENOMEM;
3168
3169 /* Create single prio */
3170 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
3171 return PTR_ERR_OR_ZERO(prio);
3172 }
3173
init_sniffer_rx_root_ns(struct mlx5_flow_steering * steering)3174 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
3175 {
3176 struct fs_prio *prio;
3177
3178 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
3179 if (!steering->sniffer_rx_root_ns)
3180 return -ENOMEM;
3181
3182 /* Create single prio */
3183 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
3184 return PTR_ERR_OR_ZERO(prio);
3185 }
3186
3187 #define PORT_SEL_NUM_LEVELS 3
init_port_sel_root_ns(struct mlx5_flow_steering * steering)3188 static int init_port_sel_root_ns(struct mlx5_flow_steering *steering)
3189 {
3190 struct fs_prio *prio;
3191
3192 steering->port_sel_root_ns = create_root_ns(steering, FS_FT_PORT_SEL);
3193 if (!steering->port_sel_root_ns)
3194 return -ENOMEM;
3195
3196 /* Create single prio */
3197 prio = fs_create_prio(&steering->port_sel_root_ns->ns, 0,
3198 PORT_SEL_NUM_LEVELS);
3199 return PTR_ERR_OR_ZERO(prio);
3200 }
3201
init_rdma_rx_root_ns(struct mlx5_flow_steering * steering)3202 static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
3203 {
3204 int err;
3205
3206 steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
3207 if (!steering->rdma_rx_root_ns)
3208 return -ENOMEM;
3209
3210 err = init_root_tree(steering, &rdma_rx_root_fs,
3211 &steering->rdma_rx_root_ns->ns.node);
3212 if (err)
3213 goto out_err;
3214
3215 set_prio_attrs(steering->rdma_rx_root_ns);
3216
3217 return 0;
3218
3219 out_err:
3220 cleanup_root_ns(steering->rdma_rx_root_ns);
3221 steering->rdma_rx_root_ns = NULL;
3222 return err;
3223 }
3224
init_rdma_tx_root_ns(struct mlx5_flow_steering * steering)3225 static int init_rdma_tx_root_ns(struct mlx5_flow_steering *steering)
3226 {
3227 int err;
3228
3229 steering->rdma_tx_root_ns = create_root_ns(steering, FS_FT_RDMA_TX);
3230 if (!steering->rdma_tx_root_ns)
3231 return -ENOMEM;
3232
3233 err = init_root_tree(steering, &rdma_tx_root_fs,
3234 &steering->rdma_tx_root_ns->ns.node);
3235 if (err)
3236 goto out_err;
3237
3238 set_prio_attrs(steering->rdma_tx_root_ns);
3239
3240 return 0;
3241
3242 out_err:
3243 cleanup_root_ns(steering->rdma_tx_root_ns);
3244 steering->rdma_tx_root_ns = NULL;
3245 return err;
3246 }
3247
3248 static int
init_rdma_transport_rx_root_ns_one(struct mlx5_flow_steering * steering,int vport_idx)3249 init_rdma_transport_rx_root_ns_one(struct mlx5_flow_steering *steering,
3250 int vport_idx)
3251 {
3252 struct mlx5_flow_root_namespace *root_ns;
3253 struct fs_prio *prio;
3254 int ret;
3255 int i;
3256
3257 steering->rdma_transport_rx_root_ns[vport_idx] =
3258 create_root_ns(steering, FS_FT_RDMA_TRANSPORT_RX);
3259 if (!steering->rdma_transport_rx_root_ns[vport_idx])
3260 return -ENOMEM;
3261
3262 root_ns = steering->rdma_transport_rx_root_ns[vport_idx];
3263
3264 for (i = 0; i < MLX5_RDMA_TRANSPORT_BYPASS_PRIO; i++) {
3265 prio = fs_create_prio(&root_ns->ns, i, 1);
3266 if (IS_ERR(prio)) {
3267 ret = PTR_ERR(prio);
3268 goto err;
3269 }
3270 }
3271 set_prio_attrs(root_ns);
3272 return 0;
3273
3274 err:
3275 cleanup_root_ns(root_ns);
3276 return ret;
3277 }
3278
3279 static int
init_rdma_transport_tx_root_ns_one(struct mlx5_flow_steering * steering,int vport_idx)3280 init_rdma_transport_tx_root_ns_one(struct mlx5_flow_steering *steering,
3281 int vport_idx)
3282 {
3283 struct mlx5_flow_root_namespace *root_ns;
3284 struct fs_prio *prio;
3285 int ret;
3286 int i;
3287
3288 steering->rdma_transport_tx_root_ns[vport_idx] =
3289 create_root_ns(steering, FS_FT_RDMA_TRANSPORT_TX);
3290 if (!steering->rdma_transport_tx_root_ns[vport_idx])
3291 return -ENOMEM;
3292
3293 root_ns = steering->rdma_transport_tx_root_ns[vport_idx];
3294
3295 for (i = 0; i < MLX5_RDMA_TRANSPORT_BYPASS_PRIO; i++) {
3296 prio = fs_create_prio(&root_ns->ns, i, 1);
3297 if (IS_ERR(prio)) {
3298 ret = PTR_ERR(prio);
3299 goto err;
3300 }
3301 }
3302 set_prio_attrs(root_ns);
3303 return 0;
3304
3305 err:
3306 cleanup_root_ns(root_ns);
3307 return ret;
3308 }
3309
mlx5_fs_ns_is_empty(struct mlx5_flow_namespace * ns)3310 static bool mlx5_fs_ns_is_empty(struct mlx5_flow_namespace *ns)
3311 {
3312 struct fs_prio *iter_prio;
3313
3314 fs_for_each_prio(iter_prio, ns) {
3315 if (iter_prio->num_ft)
3316 return false;
3317 }
3318
3319 return true;
3320 }
3321
mlx5_fs_set_root_dev(struct mlx5_core_dev * dev,struct mlx5_core_dev * new_dev,enum fs_flow_table_type table_type)3322 int mlx5_fs_set_root_dev(struct mlx5_core_dev *dev,
3323 struct mlx5_core_dev *new_dev,
3324 enum fs_flow_table_type table_type)
3325 {
3326 struct mlx5_flow_root_namespace **root;
3327 int total_vports;
3328 int i;
3329
3330 switch (table_type) {
3331 case FS_FT_RDMA_TRANSPORT_TX:
3332 root = dev->priv.steering->rdma_transport_tx_root_ns;
3333 total_vports = dev->priv.steering->rdma_transport_tx_vports;
3334 break;
3335 case FS_FT_RDMA_TRANSPORT_RX:
3336 root = dev->priv.steering->rdma_transport_rx_root_ns;
3337 total_vports = dev->priv.steering->rdma_transport_rx_vports;
3338 break;
3339 default:
3340 WARN_ON_ONCE(true);
3341 return -EINVAL;
3342 }
3343
3344 for (i = 0; i < total_vports; i++) {
3345 mutex_lock(&root[i]->chain_lock);
3346 if (!mlx5_fs_ns_is_empty(&root[i]->ns)) {
3347 mutex_unlock(&root[i]->chain_lock);
3348 goto err;
3349 }
3350 root[i]->dev = new_dev;
3351 mutex_unlock(&root[i]->chain_lock);
3352 }
3353 return 0;
3354 err:
3355 while (i--) {
3356 mutex_lock(&root[i]->chain_lock);
3357 root[i]->dev = dev;
3358 mutex_unlock(&root[i]->chain_lock);
3359 }
3360 /* If you hit this error try destroying all flow tables and try again */
3361 mlx5_core_err(dev, "Failed to set root device for RDMA TRANSPORT\n");
3362 return -EINVAL;
3363 }
3364 EXPORT_SYMBOL(mlx5_fs_set_root_dev);
3365
init_rdma_transport_rx_root_ns(struct mlx5_flow_steering * steering)3366 static int init_rdma_transport_rx_root_ns(struct mlx5_flow_steering *steering)
3367 {
3368 struct mlx5_core_dev *dev = steering->dev;
3369 int total_vports;
3370 int err;
3371 int i;
3372
3373 /* In case eswitch not supported and working in legacy mode */
3374 total_vports = mlx5_eswitch_get_total_vports(dev) ?: 1;
3375
3376 steering->rdma_transport_rx_root_ns =
3377 kzalloc_objs(*steering->rdma_transport_rx_root_ns,
3378 total_vports);
3379 if (!steering->rdma_transport_rx_root_ns)
3380 return -ENOMEM;
3381
3382 for (i = 0; i < total_vports; i++) {
3383 err = init_rdma_transport_rx_root_ns_one(steering, i);
3384 if (err)
3385 goto cleanup_root_ns;
3386 }
3387 steering->rdma_transport_rx_vports = total_vports;
3388 return 0;
3389
3390 cleanup_root_ns:
3391 while (i--)
3392 cleanup_root_ns(steering->rdma_transport_rx_root_ns[i]);
3393 kfree(steering->rdma_transport_rx_root_ns);
3394 steering->rdma_transport_rx_root_ns = NULL;
3395 return err;
3396 }
3397
init_rdma_transport_tx_root_ns(struct mlx5_flow_steering * steering)3398 static int init_rdma_transport_tx_root_ns(struct mlx5_flow_steering *steering)
3399 {
3400 struct mlx5_core_dev *dev = steering->dev;
3401 int total_vports;
3402 int err;
3403 int i;
3404
3405 /* In case eswitch not supported and working in legacy mode */
3406 total_vports = mlx5_eswitch_get_total_vports(dev) ?: 1;
3407
3408 steering->rdma_transport_tx_root_ns =
3409 kzalloc_objs(*steering->rdma_transport_tx_root_ns,
3410 total_vports);
3411 if (!steering->rdma_transport_tx_root_ns)
3412 return -ENOMEM;
3413
3414 for (i = 0; i < total_vports; i++) {
3415 err = init_rdma_transport_tx_root_ns_one(steering, i);
3416 if (err)
3417 goto cleanup_root_ns;
3418 }
3419 steering->rdma_transport_tx_vports = total_vports;
3420 return 0;
3421
3422 cleanup_root_ns:
3423 while (i--)
3424 cleanup_root_ns(steering->rdma_transport_tx_root_ns[i]);
3425 kfree(steering->rdma_transport_tx_root_ns);
3426 steering->rdma_transport_tx_root_ns = NULL;
3427 return err;
3428 }
3429
cleanup_rdma_transport_roots_ns(struct mlx5_flow_steering * steering)3430 static void cleanup_rdma_transport_roots_ns(struct mlx5_flow_steering *steering)
3431 {
3432 int i;
3433
3434 if (steering->rdma_transport_rx_root_ns) {
3435 for (i = 0; i < steering->rdma_transport_rx_vports; i++)
3436 cleanup_root_ns(steering->rdma_transport_rx_root_ns[i]);
3437
3438 kfree(steering->rdma_transport_rx_root_ns);
3439 steering->rdma_transport_rx_root_ns = NULL;
3440 }
3441
3442 if (steering->rdma_transport_tx_root_ns) {
3443 for (i = 0; i < steering->rdma_transport_tx_vports; i++)
3444 cleanup_root_ns(steering->rdma_transport_tx_root_ns[i]);
3445
3446 kfree(steering->rdma_transport_tx_root_ns);
3447 steering->rdma_transport_tx_root_ns = NULL;
3448 }
3449 }
3450
3451 /* FT and tc chains are stored in the same array so we can re-use the
3452 * mlx5_get_fdb_sub_ns() and tc api for FT chains.
3453 * When creating a new ns for each chain store it in the first available slot.
3454 * Assume tc chains are created and stored first and only then the FT chain.
3455 */
store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering * steering,struct mlx5_flow_namespace * ns)3456 static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
3457 struct mlx5_flow_namespace *ns)
3458 {
3459 int chain = 0;
3460
3461 while (steering->fdb_sub_ns[chain])
3462 ++chain;
3463
3464 steering->fdb_sub_ns[chain] = ns;
3465 }
3466
create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering * steering,struct fs_prio * maj_prio)3467 static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
3468 struct fs_prio *maj_prio)
3469 {
3470 struct mlx5_flow_namespace *ns;
3471 struct fs_prio *min_prio;
3472 int prio;
3473
3474 ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
3475 if (IS_ERR(ns))
3476 return PTR_ERR(ns);
3477
3478 for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
3479 min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
3480 if (IS_ERR(min_prio))
3481 return PTR_ERR(min_prio);
3482 }
3483
3484 store_fdb_sub_ns_prio_chain(steering, ns);
3485
3486 return 0;
3487 }
3488
create_fdb_chains(struct mlx5_flow_steering * steering,int fs_prio,int chains)3489 static int create_fdb_chains(struct mlx5_flow_steering *steering,
3490 int fs_prio,
3491 int chains)
3492 {
3493 struct fs_prio *maj_prio;
3494 int levels;
3495 int chain;
3496 int err;
3497
3498 levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
3499 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
3500 fs_prio,
3501 levels);
3502 if (IS_ERR(maj_prio))
3503 return PTR_ERR(maj_prio);
3504
3505 for (chain = 0; chain < chains; chain++) {
3506 err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
3507 if (err)
3508 return err;
3509 }
3510
3511 return 0;
3512 }
3513
create_fdb_fast_path(struct mlx5_flow_steering * steering)3514 static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
3515 {
3516 int err;
3517
3518 steering->fdb_sub_ns = kzalloc_objs(*steering->fdb_sub_ns,
3519 FDB_NUM_CHAINS);
3520 if (!steering->fdb_sub_ns)
3521 return -ENOMEM;
3522
3523 err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
3524 if (err)
3525 return err;
3526
3527 err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
3528 if (err)
3529 return err;
3530
3531 return 0;
3532 }
3533
create_fdb_bypass(struct mlx5_flow_steering * steering)3534 static int create_fdb_bypass(struct mlx5_flow_steering *steering)
3535 {
3536 struct mlx5_flow_namespace *ns;
3537 struct fs_prio *prio;
3538 int i;
3539
3540 prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH, 0);
3541 if (IS_ERR(prio))
3542 return PTR_ERR(prio);
3543
3544 ns = fs_create_namespace(prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
3545 if (IS_ERR(ns))
3546 return PTR_ERR(ns);
3547
3548 for (i = 0; i < MLX5_BY_PASS_NUM_REGULAR_PRIOS; i++) {
3549 prio = fs_create_prio(ns, i, 1);
3550 if (IS_ERR(prio))
3551 return PTR_ERR(prio);
3552 }
3553 return 0;
3554 }
3555
cleanup_fdb_root_ns(struct mlx5_flow_steering * steering)3556 static void cleanup_fdb_root_ns(struct mlx5_flow_steering *steering)
3557 {
3558 cleanup_root_ns(steering->fdb_root_ns);
3559 steering->fdb_root_ns = NULL;
3560 kfree(steering->fdb_sub_ns);
3561 steering->fdb_sub_ns = NULL;
3562 }
3563
init_fdb_root_ns(struct mlx5_flow_steering * steering)3564 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
3565 {
3566 struct fs_prio *maj_prio;
3567 int err;
3568
3569 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
3570 if (!steering->fdb_root_ns)
3571 return -ENOMEM;
3572
3573 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_DROP_ROOT, 1);
3574 err = PTR_ERR_OR_ZERO(maj_prio);
3575 if (err)
3576 goto out_err;
3577
3578 err = create_fdb_bypass(steering);
3579 if (err)
3580 goto out_err;
3581
3582 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_INGRESS, 3);
3583 if (IS_ERR(maj_prio)) {
3584 err = PTR_ERR(maj_prio);
3585 goto out_err;
3586 }
3587
3588 err = create_fdb_fast_path(steering);
3589 if (err)
3590 goto out_err;
3591
3592 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_TC_MISS, 1);
3593 if (IS_ERR(maj_prio)) {
3594 err = PTR_ERR(maj_prio);
3595 goto out_err;
3596 }
3597
3598 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BR_OFFLOAD, 4);
3599 if (IS_ERR(maj_prio)) {
3600 err = PTR_ERR(maj_prio);
3601 goto out_err;
3602 }
3603
3604 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
3605 if (IS_ERR(maj_prio)) {
3606 err = PTR_ERR(maj_prio);
3607 goto out_err;
3608 }
3609
3610 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_EGRESS, 3);
3611 if (IS_ERR(maj_prio)) {
3612 err = PTR_ERR(maj_prio);
3613 goto out_err;
3614 }
3615
3616 /* We put this priority last, knowing that nothing will get here
3617 * unless explicitly forwarded to. This is possible because the
3618 * slow path tables have catch all rules and nothing gets passed
3619 * those tables.
3620 */
3621 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_PER_VPORT, 1);
3622 if (IS_ERR(maj_prio)) {
3623 err = PTR_ERR(maj_prio);
3624 goto out_err;
3625 }
3626
3627 set_prio_attrs(steering->fdb_root_ns);
3628 return 0;
3629
3630 out_err:
3631 cleanup_fdb_root_ns(steering);
3632 return err;
3633 }
3634
3635 static void
mlx5_fs_remove_vport_acl_root_ns(struct xarray * esw_acl_root_ns,u16 vport_idx)3636 mlx5_fs_remove_vport_acl_root_ns(struct xarray *esw_acl_root_ns, u16 vport_idx)
3637 {
3638 struct mlx5_vport_acl_root_ns *vport_ns;
3639
3640 vport_ns = xa_erase(esw_acl_root_ns, vport_idx);
3641 if (vport_ns) {
3642 cleanup_root_ns(vport_ns->root_ns);
3643 kfree(vport_ns);
3644 }
3645 }
3646
3647 static int
mlx5_fs_add_vport_acl_root_ns(struct mlx5_flow_steering * steering,struct xarray * esw_acl_root_ns,enum fs_flow_table_type table_type,u16 vport_idx)3648 mlx5_fs_add_vport_acl_root_ns(struct mlx5_flow_steering *steering,
3649 struct xarray *esw_acl_root_ns,
3650 enum fs_flow_table_type table_type,
3651 u16 vport_idx)
3652 {
3653 struct mlx5_vport_acl_root_ns *vport_ns;
3654 struct fs_prio *prio;
3655 int err;
3656
3657 /* sanity check, intended xarrays are used */
3658 if (WARN_ON(esw_acl_root_ns != &steering->esw_egress_root_ns &&
3659 esw_acl_root_ns != &steering->esw_ingress_root_ns))
3660 return -EINVAL;
3661
3662 if (table_type != FS_FT_ESW_EGRESS_ACL &&
3663 table_type != FS_FT_ESW_INGRESS_ACL) {
3664 mlx5_core_err(steering->dev,
3665 "Invalid table type %d for egress/ingress ACLs\n",
3666 table_type);
3667 return -EINVAL;
3668 }
3669
3670 if (xa_load(esw_acl_root_ns, vport_idx))
3671 return -EEXIST;
3672
3673 vport_ns = kzalloc_obj(*vport_ns);
3674 if (!vport_ns)
3675 return -ENOMEM;
3676
3677 vport_ns->root_ns = create_root_ns(steering, table_type);
3678 if (!vport_ns->root_ns) {
3679 err = -ENOMEM;
3680 goto kfree_vport_ns;
3681 }
3682
3683 /* create 1 prio*/
3684 prio = fs_create_prio(&vport_ns->root_ns->ns, 0, 1);
3685 if (IS_ERR(prio)) {
3686 err = PTR_ERR(prio);
3687 goto cleanup_root_ns;
3688 }
3689
3690 vport_ns->vport_idx = vport_idx;
3691 err = xa_insert(esw_acl_root_ns, vport_idx, vport_ns, GFP_KERNEL);
3692 if (err)
3693 goto cleanup_root_ns;
3694 return 0;
3695
3696 cleanup_root_ns:
3697 cleanup_root_ns(vport_ns->root_ns);
3698 kfree_vport_ns:
3699 kfree(vport_ns);
3700 return err;
3701 }
3702
mlx5_fs_vport_egress_acl_ns_add(struct mlx5_flow_steering * steering,u16 vport_idx)3703 int mlx5_fs_vport_egress_acl_ns_add(struct mlx5_flow_steering *steering,
3704 u16 vport_idx)
3705 {
3706 return mlx5_fs_add_vport_acl_root_ns(steering,
3707 &steering->esw_egress_root_ns,
3708 FS_FT_ESW_EGRESS_ACL, vport_idx);
3709 }
3710
mlx5_fs_vport_ingress_acl_ns_add(struct mlx5_flow_steering * steering,u16 vport_idx)3711 int mlx5_fs_vport_ingress_acl_ns_add(struct mlx5_flow_steering *steering,
3712 u16 vport_idx)
3713 {
3714 return mlx5_fs_add_vport_acl_root_ns(steering,
3715 &steering->esw_ingress_root_ns,
3716 FS_FT_ESW_INGRESS_ACL, vport_idx);
3717 }
3718
mlx5_fs_vport_egress_acl_ns_remove(struct mlx5_flow_steering * steering,int vport_idx)3719 void mlx5_fs_vport_egress_acl_ns_remove(struct mlx5_flow_steering *steering,
3720 int vport_idx)
3721 {
3722 mlx5_fs_remove_vport_acl_root_ns(&steering->esw_egress_root_ns,
3723 vport_idx);
3724 }
3725
mlx5_fs_vport_ingress_acl_ns_remove(struct mlx5_flow_steering * steering,int vport_idx)3726 void mlx5_fs_vport_ingress_acl_ns_remove(struct mlx5_flow_steering *steering,
3727 int vport_idx)
3728 {
3729 mlx5_fs_remove_vport_acl_root_ns(&steering->esw_ingress_root_ns,
3730 vport_idx);
3731 }
3732
mlx5_fs_get_capabilities(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type)3733 u32 mlx5_fs_get_capabilities(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type type)
3734 {
3735 struct mlx5_flow_root_namespace *root;
3736 struct mlx5_flow_namespace *ns;
3737
3738 ns = mlx5_get_flow_namespace(dev, type);
3739 if (!ns)
3740 return 0;
3741
3742 root = find_root(&ns->node);
3743 if (!root)
3744 return 0;
3745
3746 return root->cmds->get_capabilities(root, root->table_type);
3747 }
3748
init_egress_root_ns(struct mlx5_flow_steering * steering)3749 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
3750 {
3751 int err;
3752
3753 steering->egress_root_ns = create_root_ns(steering,
3754 FS_FT_NIC_TX);
3755 if (!steering->egress_root_ns)
3756 return -ENOMEM;
3757
3758 err = init_root_tree(steering, &egress_root_fs,
3759 &steering->egress_root_ns->ns.node);
3760 if (err)
3761 goto cleanup;
3762 set_prio_attrs(steering->egress_root_ns);
3763 return 0;
3764 cleanup:
3765 cleanup_root_ns(steering->egress_root_ns);
3766 steering->egress_root_ns = NULL;
3767 return err;
3768 }
3769
mlx5_fs_mode_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)3770 static int mlx5_fs_mode_validate(struct devlink *devlink, u32 id,
3771 union devlink_param_value val,
3772 struct netlink_ext_ack *extack)
3773 {
3774 struct mlx5_core_dev *dev = devlink_priv(devlink);
3775 char *value = val.vstr;
3776 u8 eswitch_mode;
3777
3778 eswitch_mode = mlx5_eswitch_mode(dev);
3779 if (eswitch_mode == MLX5_ESWITCH_OFFLOADS) {
3780 NL_SET_ERR_MSG_FMT_MOD(extack,
3781 "Changing fs mode is not supported when eswitch offloads enabled.");
3782 return -EOPNOTSUPP;
3783 }
3784
3785 if (!strcmp(value, "dmfs"))
3786 return 0;
3787
3788 if (!strcmp(value, "smfs")) {
3789 bool smfs_cap = mlx5_fs_dr_is_supported(dev);
3790
3791 if (!smfs_cap) {
3792 NL_SET_ERR_MSG_MOD(extack,
3793 "Software managed steering is not supported by current device");
3794 return -EOPNOTSUPP;
3795 }
3796 } else if (!strcmp(value, "hmfs")) {
3797 bool hmfs_cap = mlx5_fs_hws_is_supported(dev);
3798
3799 if (!hmfs_cap) {
3800 NL_SET_ERR_MSG_MOD(extack,
3801 "Hardware steering is not supported by current device");
3802 return -EOPNOTSUPP;
3803 }
3804 } else {
3805 NL_SET_ERR_MSG_MOD(extack,
3806 "Bad parameter: supported values are [\"dmfs\", \"smfs\", \"hmfs\"]");
3807 return -EINVAL;
3808 }
3809
3810 return 0;
3811 }
3812
mlx5_fs_mode_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx,struct netlink_ext_ack * extack)3813 static int mlx5_fs_mode_set(struct devlink *devlink, u32 id,
3814 struct devlink_param_gset_ctx *ctx,
3815 struct netlink_ext_ack *extack)
3816 {
3817 struct mlx5_core_dev *dev = devlink_priv(devlink);
3818 enum mlx5_flow_steering_mode mode;
3819
3820 if (!strcmp(ctx->val.vstr, "smfs"))
3821 mode = MLX5_FLOW_STEERING_MODE_SMFS;
3822 else if (!strcmp(ctx->val.vstr, "hmfs"))
3823 mode = MLX5_FLOW_STEERING_MODE_HMFS;
3824 else
3825 mode = MLX5_FLOW_STEERING_MODE_DMFS;
3826 dev->priv.steering->mode = mode;
3827
3828 return 0;
3829 }
3830
mlx5_fs_mode_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx,struct netlink_ext_ack * extack)3831 static int mlx5_fs_mode_get(struct devlink *devlink, u32 id,
3832 struct devlink_param_gset_ctx *ctx,
3833 struct netlink_ext_ack *extack)
3834 {
3835 struct mlx5_core_dev *dev = devlink_priv(devlink);
3836
3837 switch (dev->priv.steering->mode) {
3838 case MLX5_FLOW_STEERING_MODE_SMFS:
3839 strscpy(ctx->val.vstr, "smfs", sizeof(ctx->val.vstr));
3840 break;
3841 case MLX5_FLOW_STEERING_MODE_HMFS:
3842 strscpy(ctx->val.vstr, "hmfs", sizeof(ctx->val.vstr));
3843 break;
3844 default:
3845 strscpy(ctx->val.vstr, "dmfs", sizeof(ctx->val.vstr));
3846 }
3847
3848 return 0;
3849 }
3850
3851 static const struct devlink_param mlx5_fs_params[] = {
3852 DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE,
3853 "flow_steering_mode", DEVLINK_PARAM_TYPE_STRING,
3854 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
3855 mlx5_fs_mode_get, mlx5_fs_mode_set,
3856 mlx5_fs_mode_validate),
3857 };
3858
mlx5_fs_core_cleanup(struct mlx5_core_dev * dev)3859 void mlx5_fs_core_cleanup(struct mlx5_core_dev *dev)
3860 {
3861 struct mlx5_flow_steering *steering = dev->priv.steering;
3862
3863 WARN_ON(!xa_empty(&steering->esw_egress_root_ns));
3864 WARN_ON(!xa_empty(&steering->esw_ingress_root_ns));
3865 xa_destroy(&steering->esw_egress_root_ns);
3866 xa_destroy(&steering->esw_ingress_root_ns);
3867
3868 cleanup_root_ns(steering->root_ns);
3869 cleanup_fdb_root_ns(steering);
3870 cleanup_root_ns(steering->port_sel_root_ns);
3871 cleanup_root_ns(steering->sniffer_rx_root_ns);
3872 cleanup_root_ns(steering->sniffer_tx_root_ns);
3873 cleanup_root_ns(steering->rdma_rx_root_ns);
3874 cleanup_root_ns(steering->rdma_tx_root_ns);
3875 cleanup_root_ns(steering->egress_root_ns);
3876 cleanup_rdma_transport_roots_ns(steering);
3877
3878 devl_params_unregister(priv_to_devlink(dev), mlx5_fs_params,
3879 ARRAY_SIZE(mlx5_fs_params));
3880 }
3881
mlx5_fs_core_init(struct mlx5_core_dev * dev)3882 int mlx5_fs_core_init(struct mlx5_core_dev *dev)
3883 {
3884 struct mlx5_flow_steering *steering = dev->priv.steering;
3885 int err;
3886
3887 err = devl_params_register(priv_to_devlink(dev), mlx5_fs_params,
3888 ARRAY_SIZE(mlx5_fs_params));
3889 if (err)
3890 return err;
3891
3892 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3893 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
3894 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
3895 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
3896 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
3897 err = init_root_ns(steering);
3898 if (err)
3899 goto err;
3900 }
3901
3902 if (MLX5_ESWITCH_MANAGER(dev)) {
3903 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
3904 err = init_fdb_root_ns(steering);
3905 if (err)
3906 goto err;
3907 }
3908 }
3909
3910 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
3911 err = init_sniffer_rx_root_ns(steering);
3912 if (err)
3913 goto err;
3914 }
3915
3916 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
3917 err = init_sniffer_tx_root_ns(steering);
3918 if (err)
3919 goto err;
3920 }
3921
3922 if (MLX5_CAP_FLOWTABLE_PORT_SELECTION(dev, ft_support)) {
3923 err = init_port_sel_root_ns(steering);
3924 if (err)
3925 goto err;
3926 }
3927
3928 if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support)) {
3929 err = init_rdma_rx_root_ns(steering);
3930 if (err)
3931 goto err;
3932 }
3933
3934 if (MLX5_CAP_FLOWTABLE_RDMA_TX(dev, ft_support)) {
3935 err = init_rdma_tx_root_ns(steering);
3936 if (err)
3937 goto err;
3938 }
3939
3940 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
3941 err = init_egress_root_ns(steering);
3942 if (err)
3943 goto err;
3944 }
3945
3946 if (MLX5_CAP_FLOWTABLE_RDMA_TRANSPORT_RX(dev, ft_support)) {
3947 err = init_rdma_transport_rx_root_ns(steering);
3948 if (err)
3949 goto err;
3950 }
3951
3952 if (MLX5_CAP_FLOWTABLE_RDMA_TRANSPORT_TX(dev, ft_support)) {
3953 err = init_rdma_transport_tx_root_ns(steering);
3954 if (err)
3955 goto err;
3956 }
3957
3958 xa_init(&steering->esw_egress_root_ns);
3959 xa_init(&steering->esw_ingress_root_ns);
3960 return 0;
3961
3962 err:
3963 mlx5_fs_core_cleanup(dev);
3964 return err;
3965 }
3966
mlx5_fs_core_free(struct mlx5_core_dev * dev)3967 void mlx5_fs_core_free(struct mlx5_core_dev *dev)
3968 {
3969 struct mlx5_flow_steering *steering = dev->priv.steering;
3970
3971 kmem_cache_destroy(steering->ftes_cache);
3972 kmem_cache_destroy(steering->fgs_cache);
3973 kfree(steering);
3974 mlx5_ft_pool_destroy(dev);
3975 mlx5_cleanup_fc_stats(dev);
3976 }
3977
mlx5_fs_core_alloc(struct mlx5_core_dev * dev)3978 int mlx5_fs_core_alloc(struct mlx5_core_dev *dev)
3979 {
3980 struct mlx5_flow_steering *steering;
3981 char name[80];
3982 int err = 0;
3983
3984 err = mlx5_init_fc_stats(dev);
3985 if (err)
3986 return err;
3987
3988 err = mlx5_ft_pool_init(dev);
3989 if (err)
3990 goto err;
3991
3992 steering = kzalloc_obj(*steering);
3993 if (!steering) {
3994 err = -ENOMEM;
3995 goto err;
3996 }
3997
3998 steering->dev = dev;
3999 dev->priv.steering = steering;
4000
4001 if (mlx5_fs_dr_is_supported(dev))
4002 steering->mode = MLX5_FLOW_STEERING_MODE_SMFS;
4003 else if (mlx5_fs_hws_is_supported(dev))
4004 steering->mode = MLX5_FLOW_STEERING_MODE_HMFS;
4005 else
4006 steering->mode = MLX5_FLOW_STEERING_MODE_DMFS;
4007
4008 snprintf(name, sizeof(name), "%s-mlx5_fs_fgs", dev_name(dev->device));
4009 steering->fgs_cache = kmem_cache_create(name,
4010 sizeof(struct mlx5_flow_group), 0,
4011 0, NULL);
4012 snprintf(name, sizeof(name), "%s-mlx5_fs_ftes", dev_name(dev->device));
4013 steering->ftes_cache = kmem_cache_create(name, sizeof(struct fs_fte), 0,
4014 0, NULL);
4015 if (!steering->ftes_cache || !steering->fgs_cache) {
4016 err = -ENOMEM;
4017 goto err;
4018 }
4019
4020 return 0;
4021
4022 err:
4023 mlx5_fs_core_free(dev);
4024 return err;
4025 }
4026
mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)4027 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
4028 {
4029 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
4030 struct mlx5_ft_underlay_qp *new_uqp;
4031 int err = 0;
4032
4033 new_uqp = kzalloc_obj(*new_uqp);
4034 if (!new_uqp)
4035 return -ENOMEM;
4036
4037 mutex_lock(&root->chain_lock);
4038
4039 if (!root->root_ft) {
4040 err = -EINVAL;
4041 goto update_ft_fail;
4042 }
4043
4044 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
4045 false);
4046 if (err) {
4047 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
4048 underlay_qpn, err);
4049 goto update_ft_fail;
4050 }
4051
4052 new_uqp->qpn = underlay_qpn;
4053 list_add_tail(&new_uqp->list, &root->underlay_qpns);
4054
4055 mutex_unlock(&root->chain_lock);
4056
4057 return 0;
4058
4059 update_ft_fail:
4060 mutex_unlock(&root->chain_lock);
4061 kfree(new_uqp);
4062 return err;
4063 }
4064 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
4065
mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)4066 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
4067 {
4068 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
4069 struct mlx5_ft_underlay_qp *uqp;
4070 bool found = false;
4071 int err = 0;
4072
4073 mutex_lock(&root->chain_lock);
4074 list_for_each_entry(uqp, &root->underlay_qpns, list) {
4075 if (uqp->qpn == underlay_qpn) {
4076 found = true;
4077 break;
4078 }
4079 }
4080
4081 if (!found) {
4082 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
4083 underlay_qpn);
4084 err = -EINVAL;
4085 goto out;
4086 }
4087
4088 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
4089 true);
4090 if (err)
4091 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
4092 underlay_qpn, err);
4093
4094 list_del(&uqp->list);
4095 mutex_unlock(&root->chain_lock);
4096 kfree(uqp);
4097
4098 return 0;
4099
4100 out:
4101 mutex_unlock(&root->chain_lock);
4102 return err;
4103 }
4104 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
4105
4106 struct mlx5_flow_root_namespace *
mlx5_get_root_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type ns_type)4107 mlx5_get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
4108 {
4109 struct mlx5_flow_namespace *ns;
4110
4111 if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
4112 ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS ||
4113 ns_type == MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_TX ||
4114 ns_type == MLX5_FLOW_NAMESPACE_RDMA_TRANSPORT_RX)
4115 ns = mlx5_get_flow_vport_namespace(dev, ns_type, 0);
4116 else
4117 ns = mlx5_get_flow_namespace(dev, ns_type);
4118 if (!ns)
4119 return NULL;
4120
4121 return find_root(&ns->node);
4122 }
4123
mlx5_modify_header_alloc(struct mlx5_core_dev * dev,u8 ns_type,u8 num_actions,void * modify_actions)4124 struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
4125 u8 ns_type, u8 num_actions,
4126 void *modify_actions)
4127 {
4128 struct mlx5_flow_root_namespace *root;
4129 struct mlx5_modify_hdr *modify_hdr;
4130 int err;
4131
4132 root = mlx5_get_root_namespace(dev, ns_type);
4133 if (!root)
4134 return ERR_PTR(-EOPNOTSUPP);
4135
4136 modify_hdr = kzalloc_obj(*modify_hdr);
4137 if (!modify_hdr)
4138 return ERR_PTR(-ENOMEM);
4139
4140 modify_hdr->ns_type = ns_type;
4141 err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
4142 modify_actions, modify_hdr);
4143 if (err) {
4144 kfree(modify_hdr);
4145 return ERR_PTR(err);
4146 }
4147
4148 return modify_hdr;
4149 }
4150 EXPORT_SYMBOL(mlx5_modify_header_alloc);
4151
mlx5_modify_header_dealloc(struct mlx5_core_dev * dev,struct mlx5_modify_hdr * modify_hdr)4152 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
4153 struct mlx5_modify_hdr *modify_hdr)
4154 {
4155 struct mlx5_flow_root_namespace *root;
4156
4157 root = mlx5_get_root_namespace(dev, modify_hdr->ns_type);
4158 if (WARN_ON(!root))
4159 return;
4160 root->cmds->modify_header_dealloc(root, modify_hdr);
4161 kfree(modify_hdr);
4162 }
4163 EXPORT_SYMBOL(mlx5_modify_header_dealloc);
4164
mlx5_packet_reformat_alloc(struct mlx5_core_dev * dev,struct mlx5_pkt_reformat_params * params,enum mlx5_flow_namespace_type ns_type)4165 struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
4166 struct mlx5_pkt_reformat_params *params,
4167 enum mlx5_flow_namespace_type ns_type)
4168 {
4169 struct mlx5_pkt_reformat *pkt_reformat;
4170 struct mlx5_flow_root_namespace *root;
4171 int err;
4172
4173 root = mlx5_get_root_namespace(dev, ns_type);
4174 if (!root)
4175 return ERR_PTR(-EOPNOTSUPP);
4176
4177 pkt_reformat = kzalloc_obj(*pkt_reformat);
4178 if (!pkt_reformat)
4179 return ERR_PTR(-ENOMEM);
4180
4181 pkt_reformat->ns_type = ns_type;
4182 pkt_reformat->reformat_type = params->type;
4183 err = root->cmds->packet_reformat_alloc(root, params, ns_type,
4184 pkt_reformat);
4185 if (err) {
4186 kfree(pkt_reformat);
4187 return ERR_PTR(err);
4188 }
4189
4190 return pkt_reformat;
4191 }
4192 EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
4193
mlx5_packet_reformat_dealloc(struct mlx5_core_dev * dev,struct mlx5_pkt_reformat * pkt_reformat)4194 void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
4195 struct mlx5_pkt_reformat *pkt_reformat)
4196 {
4197 struct mlx5_flow_root_namespace *root;
4198
4199 root = mlx5_get_root_namespace(dev, pkt_reformat->ns_type);
4200 if (WARN_ON(!root))
4201 return;
4202 root->cmds->packet_reformat_dealloc(root, pkt_reformat);
4203 kfree(pkt_reformat);
4204 }
4205 EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
4206
mlx5_get_match_definer_id(struct mlx5_flow_definer * definer)4207 int mlx5_get_match_definer_id(struct mlx5_flow_definer *definer)
4208 {
4209 return definer->id;
4210 }
4211
4212 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)4213 mlx5_create_match_definer(struct mlx5_core_dev *dev,
4214 enum mlx5_flow_namespace_type ns_type, u16 format_id,
4215 u32 *match_mask)
4216 {
4217 struct mlx5_flow_root_namespace *root;
4218 struct mlx5_flow_definer *definer;
4219 int id;
4220
4221 root = mlx5_get_root_namespace(dev, ns_type);
4222 if (!root)
4223 return ERR_PTR(-EOPNOTSUPP);
4224
4225 definer = kzalloc_obj(*definer);
4226 if (!definer)
4227 return ERR_PTR(-ENOMEM);
4228
4229 definer->ns_type = ns_type;
4230 id = root->cmds->create_match_definer(root, format_id, match_mask);
4231 if (id < 0) {
4232 mlx5_core_warn(root->dev, "Failed to create match definer (%d)\n", id);
4233 kfree(definer);
4234 return ERR_PTR(id);
4235 }
4236 definer->id = id;
4237 return definer;
4238 }
4239
mlx5_destroy_match_definer(struct mlx5_core_dev * dev,struct mlx5_flow_definer * definer)4240 void mlx5_destroy_match_definer(struct mlx5_core_dev *dev,
4241 struct mlx5_flow_definer *definer)
4242 {
4243 struct mlx5_flow_root_namespace *root;
4244
4245 root = mlx5_get_root_namespace(dev, definer->ns_type);
4246 if (WARN_ON(!root))
4247 return;
4248
4249 root->cmds->destroy_match_definer(root, definer->id);
4250 kfree(definer);
4251 }
4252
mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_root_namespace * peer_ns,u16 peer_vhca_id)4253 int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
4254 struct mlx5_flow_root_namespace *peer_ns,
4255 u16 peer_vhca_id)
4256 {
4257 if (peer_ns && ns->mode != peer_ns->mode) {
4258 mlx5_core_err(ns->dev,
4259 "Can't peer namespace of different steering mode\n");
4260 return -EINVAL;
4261 }
4262
4263 return ns->cmds->set_peer(ns, peer_ns, peer_vhca_id);
4264 }
4265
4266 /* This function should be called only at init stage of the namespace.
4267 * It is not safe to call this function while steering operations
4268 * are executed in the namespace.
4269 */
mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace * ns,enum mlx5_flow_steering_mode mode)4270 int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
4271 enum mlx5_flow_steering_mode mode)
4272 {
4273 struct mlx5_flow_root_namespace *root;
4274 const struct mlx5_flow_cmds *cmds;
4275 int err;
4276
4277 root = find_root(&ns->node);
4278 if (&root->ns != ns)
4279 /* Can't set cmds to non root namespace */
4280 return -EINVAL;
4281
4282 if (root->table_type != FS_FT_FDB)
4283 return -EOPNOTSUPP;
4284
4285 if (root->mode == mode)
4286 return 0;
4287
4288 if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
4289 cmds = mlx5_fs_cmd_get_dr_cmds();
4290 else if (mode == MLX5_FLOW_STEERING_MODE_HMFS)
4291 cmds = mlx5_fs_cmd_get_hws_cmds();
4292 else
4293 cmds = mlx5_fs_cmd_get_fw_cmds();
4294 if (!cmds)
4295 return -EOPNOTSUPP;
4296
4297 err = cmds->create_ns(root);
4298 if (err) {
4299 mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
4300 err);
4301 return err;
4302 }
4303
4304 root->cmds->destroy_ns(root);
4305 root->cmds = cmds;
4306 root->mode = mode;
4307
4308 return 0;
4309 }
4310