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