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 break;
2494 case MLX5_FLOW_NAMESPACE_RDMA_RX_COUNTERS:
2495 root_ns = steering->rdma_rx_root_ns;
2496 prio = RDMA_RX_COUNTERS_PRIO;
2497 break;
2498 case MLX5_FLOW_NAMESPACE_RDMA_TX_COUNTERS:
2499 root_ns = steering->rdma_tx_root_ns;
2500 prio = RDMA_TX_COUNTERS_PRIO;
2501 break;
2502 case MLX5_FLOW_NAMESPACE_RDMA_RX_IPSEC:
2503 root_ns = steering->rdma_rx_root_ns;
2504 prio = RDMA_RX_IPSEC_PRIO;
2505 break;
2506 case MLX5_FLOW_NAMESPACE_RDMA_TX_IPSEC:
2507 root_ns = steering->rdma_tx_root_ns;
2508 prio = RDMA_TX_IPSEC_PRIO;
2509 break;
2510 case MLX5_FLOW_NAMESPACE_RDMA_RX_MACSEC:
2511 root_ns = steering->rdma_rx_root_ns;
2512 prio = RDMA_RX_MACSEC_PRIO;
2513 break;
2514 case MLX5_FLOW_NAMESPACE_RDMA_TX_MACSEC:
2515 root_ns = steering->rdma_tx_root_ns;
2516 prio = RDMA_TX_MACSEC_PRIO;
2517 break;
2518 default: /* Must be NIC RX */
2519 WARN_ON(!is_nic_rx_ns(type));
2520 root_ns = steering->root_ns;
2521 prio = type;
2522 break;
2523 }
2524
2525 if (!root_ns)
2526 return NULL;
2527
2528 fs_prio = find_prio(&root_ns->ns, prio);
2529 if (!fs_prio)
2530 return NULL;
2531
2532 ns = list_first_entry(&fs_prio->node.children,
2533 typeof(*ns),
2534 node.list);
2535
2536 return ns;
2537 }
2538 EXPORT_SYMBOL(mlx5_get_flow_namespace);
2539
mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type,int vport)2540 struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2541 enum mlx5_flow_namespace_type type,
2542 int vport)
2543 {
2544 struct mlx5_flow_steering *steering = dev->priv.steering;
2545
2546 if (!steering)
2547 return NULL;
2548
2549 switch (type) {
2550 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2551 if (vport >= steering->esw_egress_acl_vports)
2552 return NULL;
2553 if (steering->esw_egress_root_ns &&
2554 steering->esw_egress_root_ns[vport])
2555 return &steering->esw_egress_root_ns[vport]->ns;
2556 else
2557 return NULL;
2558 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2559 if (vport >= steering->esw_ingress_acl_vports)
2560 return NULL;
2561 if (steering->esw_ingress_root_ns &&
2562 steering->esw_ingress_root_ns[vport])
2563 return &steering->esw_ingress_root_ns[vport]->ns;
2564 else
2565 return NULL;
2566 default:
2567 return NULL;
2568 }
2569 }
2570
_fs_create_prio(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels,enum fs_node_type type)2571 static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2572 unsigned int prio,
2573 int num_levels,
2574 enum fs_node_type type)
2575 {
2576 struct fs_prio *fs_prio;
2577
2578 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2579 if (!fs_prio)
2580 return ERR_PTR(-ENOMEM);
2581
2582 fs_prio->node.type = type;
2583 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
2584 tree_add_node(&fs_prio->node, &ns->node);
2585 fs_prio->num_levels = num_levels;
2586 fs_prio->prio = prio;
2587 list_add_tail(&fs_prio->node.list, &ns->node.children);
2588
2589 return fs_prio;
2590 }
2591
fs_create_prio_chained(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels)2592 static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2593 unsigned int prio,
2594 int num_levels)
2595 {
2596 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2597 }
2598
fs_create_prio(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels)2599 static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2600 unsigned int prio, int num_levels)
2601 {
2602 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2603 }
2604
fs_init_namespace(struct mlx5_flow_namespace * ns)2605 static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2606 *ns)
2607 {
2608 ns->node.type = FS_TYPE_NAMESPACE;
2609
2610 return ns;
2611 }
2612
fs_create_namespace(struct fs_prio * prio,int def_miss_act)2613 static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2614 int def_miss_act)
2615 {
2616 struct mlx5_flow_namespace *ns;
2617
2618 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2619 if (!ns)
2620 return ERR_PTR(-ENOMEM);
2621
2622 fs_init_namespace(ns);
2623 ns->def_miss_action = def_miss_act;
2624 tree_init_node(&ns->node, NULL, del_sw_ns);
2625 tree_add_node(&ns->node, &prio->node);
2626 list_add_tail(&ns->node.list, &prio->node.children);
2627
2628 return ns;
2629 }
2630
create_leaf_prios(struct mlx5_flow_namespace * ns,int prio,struct init_tree_node * prio_metadata)2631 static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2632 struct init_tree_node *prio_metadata)
2633 {
2634 struct fs_prio *fs_prio;
2635 int i;
2636
2637 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
2638 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
2639 if (IS_ERR(fs_prio))
2640 return PTR_ERR(fs_prio);
2641 }
2642 return 0;
2643 }
2644
2645 #define FLOW_TABLE_BIT_SZ 1
2646 #define GET_FLOW_TABLE_CAP(dev, offset) \
2647 ((be32_to_cpu(*((__be32 *)(dev->caps.hca[MLX5_CAP_FLOW_TABLE]->cur) + \
2648 offset / 32)) >> \
2649 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
has_required_caps(struct mlx5_core_dev * dev,struct node_caps * caps)2650 static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2651 {
2652 int i;
2653
2654 for (i = 0; i < caps->arr_sz; i++) {
2655 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2656 return false;
2657 }
2658 return true;
2659 }
2660
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)2661 static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
2662 struct init_tree_node *init_node,
2663 struct fs_node *fs_parent_node,
2664 struct init_tree_node *init_parent_node,
2665 int prio)
2666 {
2667 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
2668 flow_table_properties_nic_receive.
2669 max_ft_level);
2670 struct mlx5_flow_namespace *fs_ns;
2671 struct fs_prio *fs_prio;
2672 struct fs_node *base;
2673 int i;
2674 int err;
2675
2676 if (init_node->type == FS_TYPE_PRIO) {
2677 if ((init_node->min_ft_level > max_ft_level) ||
2678 !has_required_caps(steering->dev, &init_node->caps))
2679 return 0;
2680
2681 fs_get_obj(fs_ns, fs_parent_node);
2682 if (init_node->num_leaf_prios)
2683 return create_leaf_prios(fs_ns, prio, init_node);
2684 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
2685 if (IS_ERR(fs_prio))
2686 return PTR_ERR(fs_prio);
2687 base = &fs_prio->node;
2688 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2689 fs_get_obj(fs_prio, fs_parent_node);
2690 fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
2691 if (IS_ERR(fs_ns))
2692 return PTR_ERR(fs_ns);
2693 base = &fs_ns->node;
2694 } else {
2695 return -EINVAL;
2696 }
2697 prio = 0;
2698 for (i = 0; i < init_node->ar_size; i++) {
2699 err = init_root_tree_recursive(steering, &init_node->children[i],
2700 base, init_node, prio);
2701 if (err)
2702 return err;
2703 if (init_node->children[i].type == FS_TYPE_PRIO &&
2704 init_node->children[i].num_leaf_prios) {
2705 prio += init_node->children[i].num_leaf_prios;
2706 }
2707 }
2708
2709 return 0;
2710 }
2711
init_root_tree(struct mlx5_flow_steering * steering,struct init_tree_node * init_node,struct fs_node * fs_parent_node)2712 static int init_root_tree(struct mlx5_flow_steering *steering,
2713 struct init_tree_node *init_node,
2714 struct fs_node *fs_parent_node)
2715 {
2716 int err;
2717 int i;
2718
2719 for (i = 0; i < init_node->ar_size; i++) {
2720 err = init_root_tree_recursive(steering, &init_node->children[i],
2721 fs_parent_node,
2722 init_node, i);
2723 if (err)
2724 return err;
2725 }
2726 return 0;
2727 }
2728
del_sw_root_ns(struct fs_node * node)2729 static void del_sw_root_ns(struct fs_node *node)
2730 {
2731 struct mlx5_flow_root_namespace *root_ns;
2732 struct mlx5_flow_namespace *ns;
2733
2734 fs_get_obj(ns, node);
2735 root_ns = container_of(ns, struct mlx5_flow_root_namespace, ns);
2736 mutex_destroy(&root_ns->chain_lock);
2737 kfree(node);
2738 }
2739
2740 static struct mlx5_flow_root_namespace
create_root_ns(struct mlx5_flow_steering * steering,enum fs_flow_table_type table_type)2741 *create_root_ns(struct mlx5_flow_steering *steering,
2742 enum fs_flow_table_type table_type)
2743 {
2744 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
2745 struct mlx5_flow_root_namespace *root_ns;
2746 struct mlx5_flow_namespace *ns;
2747
2748 /* Create the root namespace */
2749 root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL);
2750 if (!root_ns)
2751 return NULL;
2752
2753 root_ns->dev = steering->dev;
2754 root_ns->table_type = table_type;
2755 root_ns->cmds = cmds;
2756
2757 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2758
2759 ns = &root_ns->ns;
2760 fs_init_namespace(ns);
2761 mutex_init(&root_ns->chain_lock);
2762 tree_init_node(&ns->node, NULL, del_sw_root_ns);
2763 tree_add_node(&ns->node, NULL);
2764
2765 return root_ns;
2766 }
2767
2768 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2769
set_prio_attrs_in_ns(struct mlx5_flow_namespace * ns,int acc_level)2770 static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2771 {
2772 struct fs_prio *prio;
2773
2774 fs_for_each_prio(prio, ns) {
2775 /* This updates prio start_level and num_levels */
2776 set_prio_attrs_in_prio(prio, acc_level);
2777 acc_level += prio->num_levels;
2778 }
2779 return acc_level;
2780 }
2781
set_prio_attrs_in_prio(struct fs_prio * prio,int acc_level)2782 static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2783 {
2784 struct mlx5_flow_namespace *ns;
2785 int acc_level_ns = acc_level;
2786
2787 prio->start_level = acc_level;
2788 fs_for_each_ns(ns, prio) {
2789 /* This updates start_level and num_levels of ns's priority descendants */
2790 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
2791
2792 /* If this a prio with chains, and we can jump from one chain
2793 * (namespace) to another, so we accumulate the levels
2794 */
2795 if (prio->node.type == FS_TYPE_PRIO_CHAINS)
2796 acc_level = acc_level_ns;
2797 }
2798
2799 if (!prio->num_levels)
2800 prio->num_levels = acc_level_ns - prio->start_level;
2801 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2802 }
2803
set_prio_attrs(struct mlx5_flow_root_namespace * root_ns)2804 static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2805 {
2806 struct mlx5_flow_namespace *ns = &root_ns->ns;
2807 struct fs_prio *prio;
2808 int start_level = 0;
2809
2810 fs_for_each_prio(prio, ns) {
2811 set_prio_attrs_in_prio(prio, start_level);
2812 start_level += prio->num_levels;
2813 }
2814 }
2815
2816 #define ANCHOR_PRIO 0
2817 #define ANCHOR_SIZE 1
2818 #define ANCHOR_LEVEL 0
create_anchor_flow_table(struct mlx5_flow_steering * steering)2819 static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2820 {
2821 struct mlx5_flow_namespace *ns = NULL;
2822 struct mlx5_flow_table_attr ft_attr = {};
2823 struct mlx5_flow_table *ft;
2824
2825 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2826 if (WARN_ON(!ns))
2827 return -EINVAL;
2828
2829 ft_attr.max_fte = ANCHOR_SIZE;
2830 ft_attr.level = ANCHOR_LEVEL;
2831 ft_attr.prio = ANCHOR_PRIO;
2832
2833 ft = mlx5_create_flow_table(ns, &ft_attr);
2834 if (IS_ERR(ft)) {
2835 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2836 return PTR_ERR(ft);
2837 }
2838 return 0;
2839 }
2840
init_root_ns(struct mlx5_flow_steering * steering)2841 static int init_root_ns(struct mlx5_flow_steering *steering)
2842 {
2843 int err;
2844
2845 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
2846 if (!steering->root_ns)
2847 return -ENOMEM;
2848
2849 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2850 if (err)
2851 goto out_err;
2852
2853 set_prio_attrs(steering->root_ns);
2854 err = create_anchor_flow_table(steering);
2855 if (err)
2856 goto out_err;
2857
2858 return 0;
2859
2860 out_err:
2861 cleanup_root_ns(steering->root_ns);
2862 steering->root_ns = NULL;
2863 return err;
2864 }
2865
clean_tree(struct fs_node * node)2866 static void clean_tree(struct fs_node *node)
2867 {
2868 if (node) {
2869 struct fs_node *iter;
2870 struct fs_node *temp;
2871
2872 tree_get_node(node);
2873 list_for_each_entry_safe(iter, temp, &node->children, list)
2874 clean_tree(iter);
2875 tree_put_node(node, false);
2876 tree_remove_node(node, false);
2877 }
2878 }
2879
cleanup_root_ns(struct mlx5_flow_root_namespace * root_ns)2880 static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
2881 {
2882 if (!root_ns)
2883 return;
2884
2885 clean_tree(&root_ns->ns.node);
2886 }
2887
init_sniffer_tx_root_ns(struct mlx5_flow_steering * steering)2888 static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2889 {
2890 struct fs_prio *prio;
2891
2892 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2893 if (!steering->sniffer_tx_root_ns)
2894 return -ENOMEM;
2895
2896 /* Create single prio */
2897 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2898 return PTR_ERR_OR_ZERO(prio);
2899 }
2900
init_sniffer_rx_root_ns(struct mlx5_flow_steering * steering)2901 static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2902 {
2903 struct fs_prio *prio;
2904
2905 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2906 if (!steering->sniffer_rx_root_ns)
2907 return -ENOMEM;
2908
2909 /* Create single prio */
2910 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2911 return PTR_ERR_OR_ZERO(prio);
2912 }
2913
2914 #define PORT_SEL_NUM_LEVELS 3
init_port_sel_root_ns(struct mlx5_flow_steering * steering)2915 static int init_port_sel_root_ns(struct mlx5_flow_steering *steering)
2916 {
2917 struct fs_prio *prio;
2918
2919 steering->port_sel_root_ns = create_root_ns(steering, FS_FT_PORT_SEL);
2920 if (!steering->port_sel_root_ns)
2921 return -ENOMEM;
2922
2923 /* Create single prio */
2924 prio = fs_create_prio(&steering->port_sel_root_ns->ns, 0,
2925 PORT_SEL_NUM_LEVELS);
2926 return PTR_ERR_OR_ZERO(prio);
2927 }
2928
init_rdma_rx_root_ns(struct mlx5_flow_steering * steering)2929 static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
2930 {
2931 int err;
2932
2933 steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
2934 if (!steering->rdma_rx_root_ns)
2935 return -ENOMEM;
2936
2937 err = init_root_tree(steering, &rdma_rx_root_fs,
2938 &steering->rdma_rx_root_ns->ns.node);
2939 if (err)
2940 goto out_err;
2941
2942 set_prio_attrs(steering->rdma_rx_root_ns);
2943
2944 return 0;
2945
2946 out_err:
2947 cleanup_root_ns(steering->rdma_rx_root_ns);
2948 steering->rdma_rx_root_ns = NULL;
2949 return err;
2950 }
2951
init_rdma_tx_root_ns(struct mlx5_flow_steering * steering)2952 static int init_rdma_tx_root_ns(struct mlx5_flow_steering *steering)
2953 {
2954 int err;
2955
2956 steering->rdma_tx_root_ns = create_root_ns(steering, FS_FT_RDMA_TX);
2957 if (!steering->rdma_tx_root_ns)
2958 return -ENOMEM;
2959
2960 err = init_root_tree(steering, &rdma_tx_root_fs,
2961 &steering->rdma_tx_root_ns->ns.node);
2962 if (err)
2963 goto out_err;
2964
2965 set_prio_attrs(steering->rdma_tx_root_ns);
2966
2967 return 0;
2968
2969 out_err:
2970 cleanup_root_ns(steering->rdma_tx_root_ns);
2971 steering->rdma_tx_root_ns = NULL;
2972 return err;
2973 }
2974
2975 /* FT and tc chains are stored in the same array so we can re-use the
2976 * mlx5_get_fdb_sub_ns() and tc api for FT chains.
2977 * When creating a new ns for each chain store it in the first available slot.
2978 * Assume tc chains are created and stored first and only then the FT chain.
2979 */
store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering * steering,struct mlx5_flow_namespace * ns)2980 static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2981 struct mlx5_flow_namespace *ns)
2982 {
2983 int chain = 0;
2984
2985 while (steering->fdb_sub_ns[chain])
2986 ++chain;
2987
2988 steering->fdb_sub_ns[chain] = ns;
2989 }
2990
create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering * steering,struct fs_prio * maj_prio)2991 static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2992 struct fs_prio *maj_prio)
2993 {
2994 struct mlx5_flow_namespace *ns;
2995 struct fs_prio *min_prio;
2996 int prio;
2997
2998 ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
2999 if (IS_ERR(ns))
3000 return PTR_ERR(ns);
3001
3002 for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
3003 min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
3004 if (IS_ERR(min_prio))
3005 return PTR_ERR(min_prio);
3006 }
3007
3008 store_fdb_sub_ns_prio_chain(steering, ns);
3009
3010 return 0;
3011 }
3012
create_fdb_chains(struct mlx5_flow_steering * steering,int fs_prio,int chains)3013 static int create_fdb_chains(struct mlx5_flow_steering *steering,
3014 int fs_prio,
3015 int chains)
3016 {
3017 struct fs_prio *maj_prio;
3018 int levels;
3019 int chain;
3020 int err;
3021
3022 levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
3023 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
3024 fs_prio,
3025 levels);
3026 if (IS_ERR(maj_prio))
3027 return PTR_ERR(maj_prio);
3028
3029 for (chain = 0; chain < chains; chain++) {
3030 err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
3031 if (err)
3032 return err;
3033 }
3034
3035 return 0;
3036 }
3037
create_fdb_fast_path(struct mlx5_flow_steering * steering)3038 static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
3039 {
3040 int err;
3041
3042 steering->fdb_sub_ns = kcalloc(FDB_NUM_CHAINS,
3043 sizeof(*steering->fdb_sub_ns),
3044 GFP_KERNEL);
3045 if (!steering->fdb_sub_ns)
3046 return -ENOMEM;
3047
3048 err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
3049 if (err)
3050 return err;
3051
3052 err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
3053 if (err)
3054 return err;
3055
3056 return 0;
3057 }
3058
create_fdb_bypass(struct mlx5_flow_steering * steering)3059 static int create_fdb_bypass(struct mlx5_flow_steering *steering)
3060 {
3061 struct mlx5_flow_namespace *ns;
3062 struct fs_prio *prio;
3063 int i;
3064
3065 prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH, 0);
3066 if (IS_ERR(prio))
3067 return PTR_ERR(prio);
3068
3069 ns = fs_create_namespace(prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
3070 if (IS_ERR(ns))
3071 return PTR_ERR(ns);
3072
3073 for (i = 0; i < MLX5_BY_PASS_NUM_REGULAR_PRIOS; i++) {
3074 prio = fs_create_prio(ns, i, 1);
3075 if (IS_ERR(prio))
3076 return PTR_ERR(prio);
3077 }
3078 return 0;
3079 }
3080
cleanup_fdb_root_ns(struct mlx5_flow_steering * steering)3081 static void cleanup_fdb_root_ns(struct mlx5_flow_steering *steering)
3082 {
3083 cleanup_root_ns(steering->fdb_root_ns);
3084 steering->fdb_root_ns = NULL;
3085 kfree(steering->fdb_sub_ns);
3086 steering->fdb_sub_ns = NULL;
3087 }
3088
init_fdb_root_ns(struct mlx5_flow_steering * steering)3089 static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
3090 {
3091 struct fs_prio *maj_prio;
3092 int err;
3093
3094 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
3095 if (!steering->fdb_root_ns)
3096 return -ENOMEM;
3097
3098 err = create_fdb_bypass(steering);
3099 if (err)
3100 goto out_err;
3101
3102 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_INGRESS, 3);
3103 if (IS_ERR(maj_prio)) {
3104 err = PTR_ERR(maj_prio);
3105 goto out_err;
3106 }
3107
3108 err = create_fdb_fast_path(steering);
3109 if (err)
3110 goto out_err;
3111
3112 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_TC_MISS, 1);
3113 if (IS_ERR(maj_prio)) {
3114 err = PTR_ERR(maj_prio);
3115 goto out_err;
3116 }
3117
3118 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BR_OFFLOAD, 4);
3119 if (IS_ERR(maj_prio)) {
3120 err = PTR_ERR(maj_prio);
3121 goto out_err;
3122 }
3123
3124 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
3125 if (IS_ERR(maj_prio)) {
3126 err = PTR_ERR(maj_prio);
3127 goto out_err;
3128 }
3129
3130 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_EGRESS, 3);
3131 if (IS_ERR(maj_prio)) {
3132 err = PTR_ERR(maj_prio);
3133 goto out_err;
3134 }
3135
3136 /* We put this priority last, knowing that nothing will get here
3137 * unless explicitly forwarded to. This is possible because the
3138 * slow path tables have catch all rules and nothing gets passed
3139 * those tables.
3140 */
3141 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_PER_VPORT, 1);
3142 if (IS_ERR(maj_prio)) {
3143 err = PTR_ERR(maj_prio);
3144 goto out_err;
3145 }
3146
3147 set_prio_attrs(steering->fdb_root_ns);
3148 return 0;
3149
3150 out_err:
3151 cleanup_fdb_root_ns(steering);
3152 return err;
3153 }
3154
init_egress_acl_root_ns(struct mlx5_flow_steering * steering,int vport)3155 static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
3156 {
3157 struct fs_prio *prio;
3158
3159 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
3160 if (!steering->esw_egress_root_ns[vport])
3161 return -ENOMEM;
3162
3163 /* create 1 prio*/
3164 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
3165 return PTR_ERR_OR_ZERO(prio);
3166 }
3167
init_ingress_acl_root_ns(struct mlx5_flow_steering * steering,int vport)3168 static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
3169 {
3170 struct fs_prio *prio;
3171
3172 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
3173 if (!steering->esw_ingress_root_ns[vport])
3174 return -ENOMEM;
3175
3176 /* create 1 prio*/
3177 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
3178 return PTR_ERR_OR_ZERO(prio);
3179 }
3180
mlx5_fs_egress_acls_init(struct mlx5_core_dev * dev,int total_vports)3181 int mlx5_fs_egress_acls_init(struct mlx5_core_dev *dev, int total_vports)
3182 {
3183 struct mlx5_flow_steering *steering = dev->priv.steering;
3184 int err;
3185 int i;
3186
3187 steering->esw_egress_root_ns =
3188 kcalloc(total_vports,
3189 sizeof(*steering->esw_egress_root_ns),
3190 GFP_KERNEL);
3191 if (!steering->esw_egress_root_ns)
3192 return -ENOMEM;
3193
3194 for (i = 0; i < total_vports; i++) {
3195 err = init_egress_acl_root_ns(steering, i);
3196 if (err)
3197 goto cleanup_root_ns;
3198 }
3199 steering->esw_egress_acl_vports = total_vports;
3200 return 0;
3201
3202 cleanup_root_ns:
3203 for (i--; i >= 0; i--)
3204 cleanup_root_ns(steering->esw_egress_root_ns[i]);
3205 kfree(steering->esw_egress_root_ns);
3206 steering->esw_egress_root_ns = NULL;
3207 return err;
3208 }
3209
mlx5_fs_egress_acls_cleanup(struct mlx5_core_dev * dev)3210 void mlx5_fs_egress_acls_cleanup(struct mlx5_core_dev *dev)
3211 {
3212 struct mlx5_flow_steering *steering = dev->priv.steering;
3213 int i;
3214
3215 if (!steering->esw_egress_root_ns)
3216 return;
3217
3218 for (i = 0; i < steering->esw_egress_acl_vports; i++)
3219 cleanup_root_ns(steering->esw_egress_root_ns[i]);
3220
3221 kfree(steering->esw_egress_root_ns);
3222 steering->esw_egress_root_ns = NULL;
3223 }
3224
mlx5_fs_ingress_acls_init(struct mlx5_core_dev * dev,int total_vports)3225 int mlx5_fs_ingress_acls_init(struct mlx5_core_dev *dev, int total_vports)
3226 {
3227 struct mlx5_flow_steering *steering = dev->priv.steering;
3228 int err;
3229 int i;
3230
3231 steering->esw_ingress_root_ns =
3232 kcalloc(total_vports,
3233 sizeof(*steering->esw_ingress_root_ns),
3234 GFP_KERNEL);
3235 if (!steering->esw_ingress_root_ns)
3236 return -ENOMEM;
3237
3238 for (i = 0; i < total_vports; i++) {
3239 err = init_ingress_acl_root_ns(steering, i);
3240 if (err)
3241 goto cleanup_root_ns;
3242 }
3243 steering->esw_ingress_acl_vports = total_vports;
3244 return 0;
3245
3246 cleanup_root_ns:
3247 for (i--; i >= 0; i--)
3248 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
3249 kfree(steering->esw_ingress_root_ns);
3250 steering->esw_ingress_root_ns = NULL;
3251 return err;
3252 }
3253
mlx5_fs_ingress_acls_cleanup(struct mlx5_core_dev * dev)3254 void mlx5_fs_ingress_acls_cleanup(struct mlx5_core_dev *dev)
3255 {
3256 struct mlx5_flow_steering *steering = dev->priv.steering;
3257 int i;
3258
3259 if (!steering->esw_ingress_root_ns)
3260 return;
3261
3262 for (i = 0; i < steering->esw_ingress_acl_vports; i++)
3263 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
3264
3265 kfree(steering->esw_ingress_root_ns);
3266 steering->esw_ingress_root_ns = NULL;
3267 }
3268
mlx5_fs_get_capabilities(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type)3269 u32 mlx5_fs_get_capabilities(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type type)
3270 {
3271 struct mlx5_flow_root_namespace *root;
3272 struct mlx5_flow_namespace *ns;
3273
3274 ns = mlx5_get_flow_namespace(dev, type);
3275 if (!ns)
3276 return 0;
3277
3278 root = find_root(&ns->node);
3279 if (!root)
3280 return 0;
3281
3282 return root->cmds->get_capabilities(root, root->table_type);
3283 }
3284
init_egress_root_ns(struct mlx5_flow_steering * steering)3285 static int init_egress_root_ns(struct mlx5_flow_steering *steering)
3286 {
3287 int err;
3288
3289 steering->egress_root_ns = create_root_ns(steering,
3290 FS_FT_NIC_TX);
3291 if (!steering->egress_root_ns)
3292 return -ENOMEM;
3293
3294 err = init_root_tree(steering, &egress_root_fs,
3295 &steering->egress_root_ns->ns.node);
3296 if (err)
3297 goto cleanup;
3298 set_prio_attrs(steering->egress_root_ns);
3299 return 0;
3300 cleanup:
3301 cleanup_root_ns(steering->egress_root_ns);
3302 steering->egress_root_ns = NULL;
3303 return err;
3304 }
3305
mlx5_fs_mode_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)3306 static int mlx5_fs_mode_validate(struct devlink *devlink, u32 id,
3307 union devlink_param_value val,
3308 struct netlink_ext_ack *extack)
3309 {
3310 struct mlx5_core_dev *dev = devlink_priv(devlink);
3311 char *value = val.vstr;
3312 int err = 0;
3313
3314 if (!strcmp(value, "dmfs")) {
3315 return 0;
3316 } else if (!strcmp(value, "smfs")) {
3317 u8 eswitch_mode;
3318 bool smfs_cap;
3319
3320 eswitch_mode = mlx5_eswitch_mode(dev);
3321 smfs_cap = mlx5_fs_dr_is_supported(dev);
3322
3323 if (!smfs_cap) {
3324 err = -EOPNOTSUPP;
3325 NL_SET_ERR_MSG_MOD(extack,
3326 "Software managed steering is not supported by current device");
3327 }
3328
3329 else if (eswitch_mode == MLX5_ESWITCH_OFFLOADS) {
3330 NL_SET_ERR_MSG_MOD(extack,
3331 "Software managed steering is not supported when eswitch offloads enabled.");
3332 err = -EOPNOTSUPP;
3333 }
3334 } else {
3335 NL_SET_ERR_MSG_MOD(extack,
3336 "Bad parameter: supported values are [\"dmfs\", \"smfs\"]");
3337 err = -EINVAL;
3338 }
3339
3340 return err;
3341 }
3342
mlx5_fs_mode_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)3343 static int mlx5_fs_mode_set(struct devlink *devlink, u32 id,
3344 struct devlink_param_gset_ctx *ctx)
3345 {
3346 struct mlx5_core_dev *dev = devlink_priv(devlink);
3347 enum mlx5_flow_steering_mode mode;
3348
3349 if (!strcmp(ctx->val.vstr, "smfs"))
3350 mode = MLX5_FLOW_STEERING_MODE_SMFS;
3351 else
3352 mode = MLX5_FLOW_STEERING_MODE_DMFS;
3353 dev->priv.steering->mode = mode;
3354
3355 return 0;
3356 }
3357
mlx5_fs_mode_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)3358 static int mlx5_fs_mode_get(struct devlink *devlink, u32 id,
3359 struct devlink_param_gset_ctx *ctx)
3360 {
3361 struct mlx5_core_dev *dev = devlink_priv(devlink);
3362
3363 if (dev->priv.steering->mode == MLX5_FLOW_STEERING_MODE_SMFS)
3364 strcpy(ctx->val.vstr, "smfs");
3365 else
3366 strcpy(ctx->val.vstr, "dmfs");
3367 return 0;
3368 }
3369
3370 static const struct devlink_param mlx5_fs_params[] = {
3371 DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE,
3372 "flow_steering_mode", DEVLINK_PARAM_TYPE_STRING,
3373 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
3374 mlx5_fs_mode_get, mlx5_fs_mode_set,
3375 mlx5_fs_mode_validate),
3376 };
3377
mlx5_fs_core_cleanup(struct mlx5_core_dev * dev)3378 void mlx5_fs_core_cleanup(struct mlx5_core_dev *dev)
3379 {
3380 struct mlx5_flow_steering *steering = dev->priv.steering;
3381
3382 cleanup_root_ns(steering->root_ns);
3383 cleanup_fdb_root_ns(steering);
3384 cleanup_root_ns(steering->port_sel_root_ns);
3385 cleanup_root_ns(steering->sniffer_rx_root_ns);
3386 cleanup_root_ns(steering->sniffer_tx_root_ns);
3387 cleanup_root_ns(steering->rdma_rx_root_ns);
3388 cleanup_root_ns(steering->rdma_tx_root_ns);
3389 cleanup_root_ns(steering->egress_root_ns);
3390
3391 devl_params_unregister(priv_to_devlink(dev), mlx5_fs_params,
3392 ARRAY_SIZE(mlx5_fs_params));
3393 }
3394
mlx5_fs_core_init(struct mlx5_core_dev * dev)3395 int mlx5_fs_core_init(struct mlx5_core_dev *dev)
3396 {
3397 struct mlx5_flow_steering *steering = dev->priv.steering;
3398 int err;
3399
3400 err = devl_params_register(priv_to_devlink(dev), mlx5_fs_params,
3401 ARRAY_SIZE(mlx5_fs_params));
3402 if (err)
3403 return err;
3404
3405 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3406 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
3407 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
3408 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
3409 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
3410 err = init_root_ns(steering);
3411 if (err)
3412 goto err;
3413 }
3414
3415 if (MLX5_ESWITCH_MANAGER(dev)) {
3416 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
3417 err = init_fdb_root_ns(steering);
3418 if (err)
3419 goto err;
3420 }
3421 }
3422
3423 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
3424 err = init_sniffer_rx_root_ns(steering);
3425 if (err)
3426 goto err;
3427 }
3428
3429 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
3430 err = init_sniffer_tx_root_ns(steering);
3431 if (err)
3432 goto err;
3433 }
3434
3435 if (MLX5_CAP_FLOWTABLE_PORT_SELECTION(dev, ft_support)) {
3436 err = init_port_sel_root_ns(steering);
3437 if (err)
3438 goto err;
3439 }
3440
3441 if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) &&
3442 MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)) {
3443 err = init_rdma_rx_root_ns(steering);
3444 if (err)
3445 goto err;
3446 }
3447
3448 if (MLX5_CAP_FLOWTABLE_RDMA_TX(dev, ft_support)) {
3449 err = init_rdma_tx_root_ns(steering);
3450 if (err)
3451 goto err;
3452 }
3453
3454 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
3455 err = init_egress_root_ns(steering);
3456 if (err)
3457 goto err;
3458 }
3459
3460 return 0;
3461
3462 err:
3463 mlx5_fs_core_cleanup(dev);
3464 return err;
3465 }
3466
mlx5_fs_core_free(struct mlx5_core_dev * dev)3467 void mlx5_fs_core_free(struct mlx5_core_dev *dev)
3468 {
3469 struct mlx5_flow_steering *steering = dev->priv.steering;
3470
3471 kmem_cache_destroy(steering->ftes_cache);
3472 kmem_cache_destroy(steering->fgs_cache);
3473 kfree(steering);
3474 mlx5_ft_pool_destroy(dev);
3475 mlx5_cleanup_fc_stats(dev);
3476 }
3477
mlx5_fs_core_alloc(struct mlx5_core_dev * dev)3478 int mlx5_fs_core_alloc(struct mlx5_core_dev *dev)
3479 {
3480 struct mlx5_flow_steering *steering;
3481 int err = 0;
3482
3483 err = mlx5_init_fc_stats(dev);
3484 if (err)
3485 return err;
3486
3487 err = mlx5_ft_pool_init(dev);
3488 if (err)
3489 goto err;
3490
3491 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
3492 if (!steering) {
3493 err = -ENOMEM;
3494 goto err;
3495 }
3496
3497 steering->dev = dev;
3498 dev->priv.steering = steering;
3499
3500 if (mlx5_fs_dr_is_supported(dev))
3501 steering->mode = MLX5_FLOW_STEERING_MODE_SMFS;
3502 else
3503 steering->mode = MLX5_FLOW_STEERING_MODE_DMFS;
3504
3505 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
3506 sizeof(struct mlx5_flow_group), 0,
3507 0, NULL);
3508 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
3509 0, NULL);
3510 if (!steering->ftes_cache || !steering->fgs_cache) {
3511 err = -ENOMEM;
3512 goto err;
3513 }
3514
3515 return 0;
3516
3517 err:
3518 mlx5_fs_core_free(dev);
3519 return err;
3520 }
3521
mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)3522 int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3523 {
3524 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3525 struct mlx5_ft_underlay_qp *new_uqp;
3526 int err = 0;
3527
3528 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
3529 if (!new_uqp)
3530 return -ENOMEM;
3531
3532 mutex_lock(&root->chain_lock);
3533
3534 if (!root->root_ft) {
3535 err = -EINVAL;
3536 goto update_ft_fail;
3537 }
3538
3539 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3540 false);
3541 if (err) {
3542 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
3543 underlay_qpn, err);
3544 goto update_ft_fail;
3545 }
3546
3547 new_uqp->qpn = underlay_qpn;
3548 list_add_tail(&new_uqp->list, &root->underlay_qpns);
3549
3550 mutex_unlock(&root->chain_lock);
3551
3552 return 0;
3553
3554 update_ft_fail:
3555 mutex_unlock(&root->chain_lock);
3556 kfree(new_uqp);
3557 return err;
3558 }
3559 EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
3560
mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)3561 int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3562 {
3563 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3564 struct mlx5_ft_underlay_qp *uqp;
3565 bool found = false;
3566 int err = 0;
3567
3568 mutex_lock(&root->chain_lock);
3569 list_for_each_entry(uqp, &root->underlay_qpns, list) {
3570 if (uqp->qpn == underlay_qpn) {
3571 found = true;
3572 break;
3573 }
3574 }
3575
3576 if (!found) {
3577 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
3578 underlay_qpn);
3579 err = -EINVAL;
3580 goto out;
3581 }
3582
3583 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3584 true);
3585 if (err)
3586 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
3587 underlay_qpn, err);
3588
3589 list_del(&uqp->list);
3590 mutex_unlock(&root->chain_lock);
3591 kfree(uqp);
3592
3593 return 0;
3594
3595 out:
3596 mutex_unlock(&root->chain_lock);
3597 return err;
3598 }
3599 EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
3600
3601 static struct mlx5_flow_root_namespace
get_root_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type ns_type)3602 *get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
3603 {
3604 struct mlx5_flow_namespace *ns;
3605
3606 if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
3607 ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS)
3608 ns = mlx5_get_flow_vport_acl_namespace(dev, ns_type, 0);
3609 else
3610 ns = mlx5_get_flow_namespace(dev, ns_type);
3611 if (!ns)
3612 return NULL;
3613
3614 return find_root(&ns->node);
3615 }
3616
mlx5_modify_header_alloc(struct mlx5_core_dev * dev,u8 ns_type,u8 num_actions,void * modify_actions)3617 struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
3618 u8 ns_type, u8 num_actions,
3619 void *modify_actions)
3620 {
3621 struct mlx5_flow_root_namespace *root;
3622 struct mlx5_modify_hdr *modify_hdr;
3623 int err;
3624
3625 root = get_root_namespace(dev, ns_type);
3626 if (!root)
3627 return ERR_PTR(-EOPNOTSUPP);
3628
3629 modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL);
3630 if (!modify_hdr)
3631 return ERR_PTR(-ENOMEM);
3632
3633 modify_hdr->ns_type = ns_type;
3634 err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
3635 modify_actions, modify_hdr);
3636 if (err) {
3637 kfree(modify_hdr);
3638 return ERR_PTR(err);
3639 }
3640
3641 return modify_hdr;
3642 }
3643 EXPORT_SYMBOL(mlx5_modify_header_alloc);
3644
mlx5_modify_header_dealloc(struct mlx5_core_dev * dev,struct mlx5_modify_hdr * modify_hdr)3645 void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
3646 struct mlx5_modify_hdr *modify_hdr)
3647 {
3648 struct mlx5_flow_root_namespace *root;
3649
3650 root = get_root_namespace(dev, modify_hdr->ns_type);
3651 if (WARN_ON(!root))
3652 return;
3653 root->cmds->modify_header_dealloc(root, modify_hdr);
3654 kfree(modify_hdr);
3655 }
3656 EXPORT_SYMBOL(mlx5_modify_header_dealloc);
3657
mlx5_packet_reformat_alloc(struct mlx5_core_dev * dev,struct mlx5_pkt_reformat_params * params,enum mlx5_flow_namespace_type ns_type)3658 struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
3659 struct mlx5_pkt_reformat_params *params,
3660 enum mlx5_flow_namespace_type ns_type)
3661 {
3662 struct mlx5_pkt_reformat *pkt_reformat;
3663 struct mlx5_flow_root_namespace *root;
3664 int err;
3665
3666 root = get_root_namespace(dev, ns_type);
3667 if (!root)
3668 return ERR_PTR(-EOPNOTSUPP);
3669
3670 pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL);
3671 if (!pkt_reformat)
3672 return ERR_PTR(-ENOMEM);
3673
3674 pkt_reformat->ns_type = ns_type;
3675 pkt_reformat->reformat_type = params->type;
3676 err = root->cmds->packet_reformat_alloc(root, params, ns_type,
3677 pkt_reformat);
3678 if (err) {
3679 kfree(pkt_reformat);
3680 return ERR_PTR(err);
3681 }
3682
3683 return pkt_reformat;
3684 }
3685 EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
3686
mlx5_packet_reformat_dealloc(struct mlx5_core_dev * dev,struct mlx5_pkt_reformat * pkt_reformat)3687 void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
3688 struct mlx5_pkt_reformat *pkt_reformat)
3689 {
3690 struct mlx5_flow_root_namespace *root;
3691
3692 root = get_root_namespace(dev, pkt_reformat->ns_type);
3693 if (WARN_ON(!root))
3694 return;
3695 root->cmds->packet_reformat_dealloc(root, pkt_reformat);
3696 kfree(pkt_reformat);
3697 }
3698 EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
3699
mlx5_get_match_definer_id(struct mlx5_flow_definer * definer)3700 int mlx5_get_match_definer_id(struct mlx5_flow_definer *definer)
3701 {
3702 return definer->id;
3703 }
3704
3705 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)3706 mlx5_create_match_definer(struct mlx5_core_dev *dev,
3707 enum mlx5_flow_namespace_type ns_type, u16 format_id,
3708 u32 *match_mask)
3709 {
3710 struct mlx5_flow_root_namespace *root;
3711 struct mlx5_flow_definer *definer;
3712 int id;
3713
3714 root = get_root_namespace(dev, ns_type);
3715 if (!root)
3716 return ERR_PTR(-EOPNOTSUPP);
3717
3718 definer = kzalloc(sizeof(*definer), GFP_KERNEL);
3719 if (!definer)
3720 return ERR_PTR(-ENOMEM);
3721
3722 definer->ns_type = ns_type;
3723 id = root->cmds->create_match_definer(root, format_id, match_mask);
3724 if (id < 0) {
3725 mlx5_core_warn(root->dev, "Failed to create match definer (%d)\n", id);
3726 kfree(definer);
3727 return ERR_PTR(id);
3728 }
3729 definer->id = id;
3730 return definer;
3731 }
3732
mlx5_destroy_match_definer(struct mlx5_core_dev * dev,struct mlx5_flow_definer * definer)3733 void mlx5_destroy_match_definer(struct mlx5_core_dev *dev,
3734 struct mlx5_flow_definer *definer)
3735 {
3736 struct mlx5_flow_root_namespace *root;
3737
3738 root = get_root_namespace(dev, definer->ns_type);
3739 if (WARN_ON(!root))
3740 return;
3741
3742 root->cmds->destroy_match_definer(root, definer->id);
3743 kfree(definer);
3744 }
3745
mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_root_namespace * peer_ns,u16 peer_vhca_id)3746 int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
3747 struct mlx5_flow_root_namespace *peer_ns,
3748 u16 peer_vhca_id)
3749 {
3750 if (peer_ns && ns->mode != peer_ns->mode) {
3751 mlx5_core_err(ns->dev,
3752 "Can't peer namespace of different steering mode\n");
3753 return -EINVAL;
3754 }
3755
3756 return ns->cmds->set_peer(ns, peer_ns, peer_vhca_id);
3757 }
3758
3759 /* This function should be called only at init stage of the namespace.
3760 * It is not safe to call this function while steering operations
3761 * are executed in the namespace.
3762 */
mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace * ns,enum mlx5_flow_steering_mode mode)3763 int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
3764 enum mlx5_flow_steering_mode mode)
3765 {
3766 struct mlx5_flow_root_namespace *root;
3767 const struct mlx5_flow_cmds *cmds;
3768 int err;
3769
3770 root = find_root(&ns->node);
3771 if (&root->ns != ns)
3772 /* Can't set cmds to non root namespace */
3773 return -EINVAL;
3774
3775 if (root->table_type != FS_FT_FDB)
3776 return -EOPNOTSUPP;
3777
3778 if (root->mode == mode)
3779 return 0;
3780
3781 if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
3782 cmds = mlx5_fs_cmd_get_dr_cmds();
3783 else
3784 cmds = mlx5_fs_cmd_get_fw_cmds();
3785 if (!cmds)
3786 return -EOPNOTSUPP;
3787
3788 err = cmds->create_ns(root);
3789 if (err) {
3790 mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
3791 err);
3792 return err;
3793 }
3794
3795 root->cmds->destroy_ns(root);
3796 root->cmds = cmds;
3797 root->mode = mode;
3798
3799 return 0;
3800 }
3801