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