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