net/mlx5: Expose other function ifc bits
[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>
de8575e0
MG
37
38#include "mlx5_core.h"
39#include "fs_core.h"
0c56b975 40#include "fs_cmd.h"
4c03e69a 41#include "diag/fs_tracepoint.h"
5f418378 42#include "accel/ipsec.h"
05564d0a 43#include "fpga/ipsec.h"
0c56b975 44
25302363
MG
45#define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
46 sizeof(struct init_tree_node))
47
a257b94a 48#define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
8d40d162 49 ...) {.type = FS_TYPE_PRIO,\
25302363 50 .min_ft_level = min_level_val,\
a257b94a 51 .num_levels = num_levels_val,\
4cbdd30e 52 .num_leaf_prios = num_prios_val,\
8d40d162 53 .caps = caps_val,\
25302363
MG
54 .children = (struct init_tree_node[]) {__VA_ARGS__},\
55 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
56}
57
a257b94a
MG
58#define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
59 ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
4cbdd30e 60 __VA_ARGS__)\
25302363 61
f66ad830
MZ
62#define ADD_NS(def_miss_act, ...) {.type = FS_TYPE_NAMESPACE, \
63 .def_miss_action = def_miss_act,\
25302363
MG
64 .children = (struct init_tree_node[]) {__VA_ARGS__},\
65 .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
66}
67
8d40d162
MG
68#define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
69 sizeof(long))
70
71#define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
72
73#define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
74 .caps = (long[]) {__VA_ARGS__} }
75
6dc6071c
MG
76#define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
77 FS_CAP(flow_table_properties_nic_receive.modify_root), \
78 FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
79 FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
80
8ce78257
MB
81#define FS_CHAINING_CAPS_EGRESS \
82 FS_REQUIRED_CAPS( \
83 FS_CAP(flow_table_properties_nic_transmit.flow_modify_en), \
84 FS_CAP(flow_table_properties_nic_transmit.modify_root), \
85 FS_CAP(flow_table_properties_nic_transmit \
86 .identified_miss_table_mode), \
87 FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
88
24670b1a
MG
89#define FS_CHAINING_CAPS_RDMA_TX \
90 FS_REQUIRED_CAPS( \
91 FS_CAP(flow_table_properties_nic_transmit_rdma.flow_modify_en), \
92 FS_CAP(flow_table_properties_nic_transmit_rdma.modify_root), \
93 FS_CAP(flow_table_properties_nic_transmit_rdma \
94 .identified_miss_table_mode), \
95 FS_CAP(flow_table_properties_nic_transmit_rdma \
96 .flow_table_modify))
97
a257b94a 98#define LEFTOVERS_NUM_LEVELS 1
4cbdd30e 99#define LEFTOVERS_NUM_PRIOS 1
4cbdd30e 100
a257b94a 101#define BY_PASS_PRIO_NUM_LEVELS 1
6dc6071c 102#define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
a257b94a
MG
103 LEFTOVERS_NUM_PRIOS)
104
6dc6071c 105#define ETHTOOL_PRIO_NUM_LEVELS 1
e5835f28 106#define ETHTOOL_NUM_PRIOS 11
6dc6071c 107#define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
5e466345 108/* Vlan, mac, ttc, inner ttc, {aRFS/accel and esp/esp_err} */
c062d52a 109#define KERNEL_NIC_PRIO_NUM_LEVELS 6
13de6c10
MG
110#define KERNEL_NIC_NUM_PRIOS 1
111/* One more level for tc */
112#define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
8d40d162 113
479f074c
OG
114#define KERNEL_NIC_TC_NUM_PRIOS 1
115#define KERNEL_NIC_TC_NUM_LEVELS 2
116
a257b94a 117#define ANCHOR_NUM_LEVELS 1
153fefbf
MG
118#define ANCHOR_NUM_PRIOS 1
119#define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
acbc2004 120
20f7b37f 121#define OFFLOADS_MAX_FT 2
11b717d6
PB
122#define OFFLOADS_NUM_PRIOS 2
123#define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + OFFLOADS_NUM_PRIOS)
acbc2004 124
3e75d4eb
AH
125#define LAG_PRIO_NUM_LEVELS 1
126#define LAG_NUM_PRIOS 1
127#define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + 1)
128
ee92e4f1
HN
129#define KERNEL_TX_IPSEC_NUM_PRIOS 1
130#define KERNEL_TX_IPSEC_NUM_LEVELS 1
131#define KERNEL_TX_MIN_LEVEL (KERNEL_TX_IPSEC_NUM_LEVELS)
132
8d40d162
MG
133struct node_caps {
134 size_t arr_sz;
135 long *caps;
136};
8963ca45 137
25302363
MG
138static struct init_tree_node {
139 enum fs_node_type type;
140 struct init_tree_node *children;
141 int ar_size;
8d40d162 142 struct node_caps caps;
25302363 143 int min_ft_level;
4cbdd30e 144 int num_leaf_prios;
25302363 145 int prio;
a257b94a 146 int num_levels;
f66ad830 147 enum mlx5_flow_table_miss_action def_miss_action;
25302363
MG
148} root_fs = {
149 .type = FS_TYPE_NAMESPACE,
3e75d4eb 150 .ar_size = 7,
f66ad830
MZ
151 .children = (struct init_tree_node[]){
152 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
153 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
154 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
155 BY_PASS_PRIO_NUM_LEVELS))),
156 ADD_PRIO(0, LAG_MIN_LEVEL, 0, FS_CHAINING_CAPS,
157 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
158 ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
159 LAG_PRIO_NUM_LEVELS))),
20f7b37f 160 ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
f66ad830
MZ
161 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
162 ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS,
163 OFFLOADS_MAX_FT))),
164 ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0, FS_CHAINING_CAPS,
165 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
166 ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
167 ETHTOOL_PRIO_NUM_LEVELS))),
168 ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
169 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
170 ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS,
171 KERNEL_NIC_TC_NUM_LEVELS),
172 ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
173 KERNEL_NIC_PRIO_NUM_LEVELS))),
174 ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
175 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
176 ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS,
177 LEFTOVERS_NUM_LEVELS))),
178 ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
179 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
180 ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS,
181 ANCHOR_NUM_LEVELS))),
25302363
MG
182 }
183};
184
8ce78257
MB
185static struct init_tree_node egress_root_fs = {
186 .type = FS_TYPE_NAMESPACE,
ee92e4f1
HN
187#ifdef CONFIG_MLX5_IPSEC
188 .ar_size = 2,
189#else
8ce78257 190 .ar_size = 1,
ee92e4f1 191#endif
8ce78257
MB
192 .children = (struct init_tree_node[]) {
193 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
194 FS_CHAINING_CAPS_EGRESS,
f66ad830
MZ
195 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
196 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
8ce78257 197 BY_PASS_PRIO_NUM_LEVELS))),
ee92e4f1
HN
198#ifdef CONFIG_MLX5_IPSEC
199 ADD_PRIO(0, KERNEL_TX_MIN_LEVEL, 0,
200 FS_CHAINING_CAPS_EGRESS,
201 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
202 ADD_MULTIPLE_PRIO(KERNEL_TX_IPSEC_NUM_PRIOS,
203 KERNEL_TX_IPSEC_NUM_LEVELS))),
204#endif
8ce78257
MB
205 }
206};
207
e6806e9a
MZ
208#define RDMA_RX_BYPASS_PRIO 0
209#define RDMA_RX_KERNEL_PRIO 1
210static struct init_tree_node rdma_rx_root_fs = {
211 .type = FS_TYPE_NAMESPACE,
212 .ar_size = 2,
213 .children = (struct init_tree_node[]) {
214 [RDMA_RX_BYPASS_PRIO] =
215 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS, 0,
216 FS_CHAINING_CAPS,
217 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
218 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_REGULAR_PRIOS,
219 BY_PASS_PRIO_NUM_LEVELS))),
220 [RDMA_RX_KERNEL_PRIO] =
221 ADD_PRIO(0, MLX5_BY_PASS_NUM_REGULAR_PRIOS + 1, 0,
222 FS_CHAINING_CAPS,
223 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_SWITCH_DOMAIN,
224 ADD_MULTIPLE_PRIO(1, 1))),
225 }
226};
227
24670b1a
MG
228static struct init_tree_node rdma_tx_root_fs = {
229 .type = FS_TYPE_NAMESPACE,
230 .ar_size = 1,
231 .children = (struct init_tree_node[]) {
232 ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
233 FS_CHAINING_CAPS_RDMA_TX,
234 ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
235 ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
236 BY_PASS_PRIO_NUM_LEVELS))),
237 }
238};
239
c7784b1c
MG
240enum fs_i_lock_class {
241 FS_LOCK_GRANDPARENT,
242 FS_LOCK_PARENT,
243 FS_LOCK_CHILD
f0d22d18
MG
244};
245
0d235c3f 246static const struct rhashtable_params rhash_fte = {
c593642c 247 .key_len = sizeof_field(struct fs_fte, val),
0d235c3f
MB
248 .key_offset = offsetof(struct fs_fte, val),
249 .head_offset = offsetof(struct fs_fte, hash),
250 .automatic_shrinking = true,
251 .min_size = 1,
252};
253
693c6883 254static const struct rhashtable_params rhash_fg = {
c593642c 255 .key_len = sizeof_field(struct mlx5_flow_group, mask),
693c6883
MB
256 .key_offset = offsetof(struct mlx5_flow_group, mask),
257 .head_offset = offsetof(struct mlx5_flow_group, hash),
258 .automatic_shrinking = true,
259 .min_size = 1,
260
261};
262
bd71b08e
MG
263static void del_hw_flow_table(struct fs_node *node);
264static void del_hw_flow_group(struct fs_node *node);
265static void del_hw_fte(struct fs_node *node);
266static void del_sw_flow_table(struct fs_node *node);
267static void del_sw_flow_group(struct fs_node *node);
268static void del_sw_fte(struct fs_node *node);
139ed6c6
MG
269static void del_sw_prio(struct fs_node *node);
270static void del_sw_ns(struct fs_node *node);
14c129e3 271/* Delete rule (destination) is special case that
bd71b08e
MG
272 * requires to lock the FTE for all the deletion process.
273 */
274static void del_sw_hw_rule(struct fs_node *node);
814fb875
MB
275static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
276 struct mlx5_flow_destination *d2);
9c26f5f8 277static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
74491de9
MB
278static struct mlx5_flow_rule *
279find_flow_rule(struct fs_fte *fte,
280 struct mlx5_flow_destination *dest);
de8575e0
MG
281
282static void tree_init_node(struct fs_node *node,
bd71b08e
MG
283 void (*del_hw_func)(struct fs_node *),
284 void (*del_sw_func)(struct fs_node *))
de8575e0 285{
dd8e1945 286 refcount_set(&node->refcount, 1);
de8575e0
MG
287 INIT_LIST_HEAD(&node->list);
288 INIT_LIST_HEAD(&node->children);
c7784b1c 289 init_rwsem(&node->lock);
bd71b08e
MG
290 node->del_hw_func = del_hw_func;
291 node->del_sw_func = del_sw_func;
19f100fe 292 node->active = false;
de8575e0
MG
293}
294
295static void tree_add_node(struct fs_node *node, struct fs_node *parent)
296{
297 if (parent)
dd8e1945 298 refcount_inc(&parent->refcount);
de8575e0
MG
299 node->parent = parent;
300
301 /* Parent is the root */
302 if (!parent)
303 node->root = node;
304 else
305 node->root = parent->root;
306}
307
bd71b08e 308static int tree_get_node(struct fs_node *node)
de8575e0 309{
dd8e1945 310 return refcount_inc_not_zero(&node->refcount);
de8575e0
MG
311}
312
bd71b08e
MG
313static void nested_down_read_ref_node(struct fs_node *node,
314 enum fs_i_lock_class class)
de8575e0
MG
315{
316 if (node) {
bd71b08e 317 down_read_nested(&node->lock, class);
dd8e1945 318 refcount_inc(&node->refcount);
de8575e0
MG
319 }
320}
321
bd71b08e
MG
322static void nested_down_write_ref_node(struct fs_node *node,
323 enum fs_i_lock_class class)
de8575e0
MG
324{
325 if (node) {
bd71b08e 326 down_write_nested(&node->lock, class);
dd8e1945 327 refcount_inc(&node->refcount);
de8575e0
MG
328 }
329}
330
476d61b7 331static void down_write_ref_node(struct fs_node *node, bool locked)
de8575e0
MG
332{
333 if (node) {
476d61b7
EB
334 if (!locked)
335 down_write(&node->lock);
dd8e1945 336 refcount_inc(&node->refcount);
de8575e0
MG
337 }
338}
339
bd71b08e
MG
340static void up_read_ref_node(struct fs_node *node)
341{
dd8e1945 342 refcount_dec(&node->refcount);
bd71b08e
MG
343 up_read(&node->lock);
344}
345
476d61b7 346static void up_write_ref_node(struct fs_node *node, bool locked)
bd71b08e 347{
dd8e1945 348 refcount_dec(&node->refcount);
476d61b7
EB
349 if (!locked)
350 up_write(&node->lock);
bd71b08e
MG
351}
352
476d61b7 353static void tree_put_node(struct fs_node *node, bool locked)
de8575e0
MG
354{
355 struct fs_node *parent_node = node->parent;
356
dd8e1945 357 if (refcount_dec_and_test(&node->refcount)) {
bd71b08e
MG
358 if (node->del_hw_func)
359 node->del_hw_func(node);
360 if (parent_node) {
476d61b7 361 down_write_ref_node(parent_node, locked);
de8575e0 362 list_del_init(&node->list);
bd71b08e 363 }
6eb7a268
RD
364 node->del_sw_func(node);
365 if (parent_node)
366 up_write_ref_node(parent_node, locked);
de8575e0
MG
367 node = NULL;
368 }
de8575e0 369 if (!node && parent_node)
476d61b7 370 tree_put_node(parent_node, locked);
de8575e0
MG
371}
372
476d61b7 373static int tree_remove_node(struct fs_node *node, bool locked)
de8575e0 374{
dd8e1945
ER
375 if (refcount_read(&node->refcount) > 1) {
376 refcount_dec(&node->refcount);
b3638e1a
MG
377 return -EEXIST;
378 }
476d61b7 379 tree_put_node(node, locked);
de8575e0
MG
380 return 0;
381}
5e1626c0
MG
382
383static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
384 unsigned int prio)
385{
386 struct fs_prio *iter_prio;
387
388 fs_for_each_prio(iter_prio, ns) {
389 if (iter_prio->prio == prio)
390 return iter_prio;
391 }
392
393 return NULL;
394}
395
9254f8ed
MG
396static bool is_fwd_next_action(u32 action)
397{
398 return action & (MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
399 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
400}
401
693c6883 402static bool check_valid_spec(const struct mlx5_flow_spec *spec)
5e1626c0 403{
693c6883
MB
404 int i;
405
693c6883
MB
406 for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
407 if (spec->match_value[i] & ~spec->match_criteria[i]) {
408 pr_warn("mlx5_core: match_value differs from match_criteria\n");
409 return false;
410 }
411
2aada6c0 412 return true;
5e1626c0 413}
0c56b975
MG
414
415static struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
416{
417 struct fs_node *root;
418 struct mlx5_flow_namespace *ns;
419
420 root = node->root;
421
422 if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
423 pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
424 return NULL;
425 }
426
427 ns = container_of(root, struct mlx5_flow_namespace, node);
428 return container_of(ns, struct mlx5_flow_root_namespace, ns);
429}
430
a369d4ac
MG
431static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
432{
433 struct mlx5_flow_root_namespace *root = find_root(node);
434
435 if (root)
436 return root->dev->priv.steering;
437 return NULL;
438}
439
0c56b975
MG
440static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
441{
442 struct mlx5_flow_root_namespace *root = find_root(node);
443
444 if (root)
445 return root->dev;
446 return NULL;
447}
448
139ed6c6
MG
449static void del_sw_ns(struct fs_node *node)
450{
451 kfree(node);
452}
453
454static void del_sw_prio(struct fs_node *node)
455{
456 kfree(node);
457}
458
bd71b08e 459static void del_hw_flow_table(struct fs_node *node)
0c56b975 460{
af76c501 461 struct mlx5_flow_root_namespace *root;
0c56b975
MG
462 struct mlx5_flow_table *ft;
463 struct mlx5_core_dev *dev;
0c56b975
MG
464 int err;
465
466 fs_get_obj(ft, node);
467 dev = get_dev(&ft->node);
af76c501 468 root = find_root(&ft->node);
8e4ca986 469 trace_mlx5_fs_del_ft(ft);
0c56b975 470
19f100fe 471 if (node->active) {
ae288a48 472 err = root->cmds->destroy_flow_table(root, ft);
19f100fe
MG
473 if (err)
474 mlx5_core_warn(dev, "flow steering can't destroy ft\n");
475 }
bd71b08e
MG
476}
477
478static void del_sw_flow_table(struct fs_node *node)
479{
480 struct mlx5_flow_table *ft;
481 struct fs_prio *prio;
482
483 fs_get_obj(ft, node);
484
693c6883 485 rhltable_destroy(&ft->fgs_hash);
aee37f3d
RD
486 if (ft->node.parent) {
487 fs_get_obj(prio, ft->node.parent);
488 prio->num_ft--;
489 }
a369d4ac 490 kfree(ft);
0c56b975
MG
491}
492
e7aafc8f 493static void modify_fte(struct fs_fte *fte)
0c56b975 494{
af76c501 495 struct mlx5_flow_root_namespace *root;
0c56b975
MG
496 struct mlx5_flow_table *ft;
497 struct mlx5_flow_group *fg;
e7aafc8f 498 struct mlx5_core_dev *dev;
0c56b975
MG
499 int err;
500
0c56b975 501 fs_get_obj(fg, fte->node.parent);
0c56b975 502 fs_get_obj(ft, fg->node.parent);
e7aafc8f
EB
503 dev = get_dev(&fte->node);
504
505 root = find_root(&ft->node);
ae288a48 506 err = root->cmds->update_fte(root, ft, fg, fte->modify_mask, fte);
e7aafc8f
EB
507 if (err)
508 mlx5_core_warn(dev,
509 "%s can't del rule fg id=%d fte_index=%d\n",
510 __func__, fg->id, fte->index);
511 fte->modify_mask = 0;
512}
513
514static void del_sw_hw_rule(struct fs_node *node)
515{
516 struct mlx5_flow_rule *rule;
517 struct fs_fte *fte;
518
519 fs_get_obj(rule, node);
520 fs_get_obj(fte, rule->node.parent);
4c03e69a 521 trace_mlx5_fs_del_rule(rule);
9254f8ed 522 if (is_fwd_next_action(rule->sw_action)) {
b3638e1a
MG
523 mutex_lock(&rule->dest_attr.ft->lock);
524 list_del(&rule->next_ft);
525 mutex_unlock(&rule->dest_attr.ft->lock);
526 }
ae058314
MB
527
528 if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER &&
529 --fte->dests_size) {
e7aafc8f
EB
530 fte->modify_mask |=
531 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
532 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
d2ec6a35 533 fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
ae058314
MB
534 goto out;
535 }
536
d2ec6a35 537 if ((fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) &&
60ab4584 538 --fte->dests_size) {
e7aafc8f
EB
539 fte->modify_mask |=
540 BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
ae058314
MB
541 }
542out:
a369d4ac 543 kfree(rule);
0c56b975
MG
544}
545
bd71b08e 546static void del_hw_fte(struct fs_node *node)
0c56b975 547{
af76c501 548 struct mlx5_flow_root_namespace *root;
0c56b975
MG
549 struct mlx5_flow_table *ft;
550 struct mlx5_flow_group *fg;
551 struct mlx5_core_dev *dev;
552 struct fs_fte *fte;
553 int err;
554
555 fs_get_obj(fte, node);
556 fs_get_obj(fg, fte->node.parent);
557 fs_get_obj(ft, fg->node.parent);
558
bd71b08e 559 trace_mlx5_fs_del_fte(fte);
0c56b975 560 dev = get_dev(&ft->node);
af76c501 561 root = find_root(&ft->node);
19f100fe 562 if (node->active) {
ae288a48 563 err = root->cmds->delete_fte(root, ft, fte);
19f100fe
MG
564 if (err)
565 mlx5_core_warn(dev,
566 "flow steering can't delete fte in index %d of flow group id %d\n",
567 fte->index, fg->id);
6237634d 568 node->active = 0;
19f100fe 569 }
bd71b08e
MG
570}
571
572static void del_sw_fte(struct fs_node *node)
573{
1f0593e7 574 struct mlx5_flow_steering *steering = get_steering(node);
bd71b08e
MG
575 struct mlx5_flow_group *fg;
576 struct fs_fte *fte;
577 int err;
578
579 fs_get_obj(fte, node);
580 fs_get_obj(fg, fte->node.parent);
0c56b975 581
19f100fe
MG
582 err = rhashtable_remove_fast(&fg->ftes_hash,
583 &fte->hash,
584 rhash_fte);
585 WARN_ON(err);
586 ida_simple_remove(&fg->fte_allocator, fte->index - fg->start_index);
1f0593e7 587 kmem_cache_free(steering->ftes_cache, fte);
0c56b975
MG
588}
589
bd71b08e 590static void del_hw_flow_group(struct fs_node *node)
0c56b975 591{
af76c501 592 struct mlx5_flow_root_namespace *root;
0c56b975
MG
593 struct mlx5_flow_group *fg;
594 struct mlx5_flow_table *ft;
595 struct mlx5_core_dev *dev;
596
597 fs_get_obj(fg, node);
598 fs_get_obj(ft, fg->node.parent);
599 dev = get_dev(&ft->node);
4c03e69a 600 trace_mlx5_fs_del_fg(fg);
0c56b975 601
af76c501 602 root = find_root(&ft->node);
ae288a48 603 if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
bd71b08e
MG
604 mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
605 fg->id, ft->id);
606}
607
608static void del_sw_flow_group(struct fs_node *node)
609{
a369d4ac 610 struct mlx5_flow_steering *steering = get_steering(node);
bd71b08e
MG
611 struct mlx5_flow_group *fg;
612 struct mlx5_flow_table *ft;
613 int err;
614
615 fs_get_obj(fg, node);
616 fs_get_obj(ft, fg->node.parent);
32dba76a 617
0d235c3f 618 rhashtable_destroy(&fg->ftes_hash);
75d1d187 619 ida_destroy(&fg->fte_allocator);
79cdb0aa
PB
620 if (ft->autogroup.active &&
621 fg->max_ftes == ft->autogroup.group_size &&
622 fg->start_index < ft->autogroup.max_fte)
bd71b08e 623 ft->autogroup.num_groups--;
693c6883
MB
624 err = rhltable_remove(&ft->fgs_hash,
625 &fg->hash,
626 rhash_fg);
627 WARN_ON(err);
a369d4ac 628 kmem_cache_free(steering->fgs_cache, fg);
0c56b975
MG
629}
630
f5c2ff17
MG
631static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
632{
633 int index;
634 int ret;
635
636 index = ida_simple_get(&fg->fte_allocator, 0, fg->max_ftes, GFP_KERNEL);
637 if (index < 0)
638 return index;
639
640 fte->index = index + fg->start_index;
641 ret = rhashtable_insert_fast(&fg->ftes_hash,
642 &fte->hash,
643 rhash_fte);
644 if (ret)
645 goto err_ida_remove;
646
647 tree_add_node(&fte->node, &fg->node);
648 list_add_tail(&fte->node.list, &fg->node.children);
649 return 0;
650
651err_ida_remove:
652 ida_simple_remove(&fg->fte_allocator, index);
653 return ret;
654}
655
a369d4ac 656static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
5233794b 657 const struct mlx5_flow_spec *spec,
f5c2ff17 658 struct mlx5_flow_act *flow_act)
0c56b975 659{
a369d4ac 660 struct mlx5_flow_steering *steering = get_steering(&ft->node);
0c56b975
MG
661 struct fs_fte *fte;
662
a369d4ac 663 fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
0c56b975
MG
664 if (!fte)
665 return ERR_PTR(-ENOMEM);
666
bb0ee7dc 667 memcpy(fte->val, &spec->match_value, sizeof(fte->val));
0c56b975 668 fte->node.type = FS_TYPE_FLOW_ENTRY;
d2ec6a35 669 fte->action = *flow_act;
bb0ee7dc 670 fte->flow_context = spec->flow_context;
0c56b975 671
cefc2355 672 tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
19f100fe
MG
673
674 return fte;
19f100fe
MG
675}
676
a369d4ac
MG
677static void dealloc_flow_group(struct mlx5_flow_steering *steering,
678 struct mlx5_flow_group *fg)
19f100fe
MG
679{
680 rhashtable_destroy(&fg->ftes_hash);
a369d4ac 681 kmem_cache_free(steering->fgs_cache, fg);
19f100fe
MG
682}
683
a369d4ac
MG
684static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
685 u8 match_criteria_enable,
5233794b 686 const void *match_criteria,
19f100fe
MG
687 int start_index,
688 int end_index)
0c56b975
MG
689{
690 struct mlx5_flow_group *fg;
0d235c3f
MB
691 int ret;
692
a369d4ac 693 fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
0c56b975
MG
694 if (!fg)
695 return ERR_PTR(-ENOMEM);
696
0d235c3f
MB
697 ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
698 if (ret) {
a369d4ac 699 kmem_cache_free(steering->fgs_cache, fg);
0d235c3f 700 return ERR_PTR(ret);
fc9c5a4a
TZ
701 }
702
75d1d187 703 ida_init(&fg->fte_allocator);
0c56b975
MG
704 fg->mask.match_criteria_enable = match_criteria_enable;
705 memcpy(&fg->mask.match_criteria, match_criteria,
706 sizeof(fg->mask.match_criteria));
707 fg->node.type = FS_TYPE_FLOW_GROUP;
19f100fe
MG
708 fg->start_index = start_index;
709 fg->max_ftes = end_index - start_index + 1;
710
711 return fg;
712}
713
714static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
715 u8 match_criteria_enable,
5233794b 716 const void *match_criteria,
19f100fe
MG
717 int start_index,
718 int end_index,
719 struct list_head *prev)
720{
a369d4ac 721 struct mlx5_flow_steering *steering = get_steering(&ft->node);
19f100fe
MG
722 struct mlx5_flow_group *fg;
723 int ret;
724
a369d4ac 725 fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
19f100fe
MG
726 start_index, end_index);
727 if (IS_ERR(fg))
728 return fg;
729
730 /* initialize refcnt, add to parent list */
731 ret = rhltable_insert(&ft->fgs_hash,
732 &fg->hash,
733 rhash_fg);
734 if (ret) {
a369d4ac 735 dealloc_flow_group(steering, fg);
19f100fe
MG
736 return ERR_PTR(ret);
737 }
738
bd71b08e 739 tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
19f100fe
MG
740 tree_add_node(&fg->node, &ft->node);
741 /* Add node to group list */
742 list_add(&fg->node.list, prev);
bd71b08e 743 atomic_inc(&ft->node.version);
19f100fe 744
0c56b975
MG
745 return fg;
746}
747
efdc810b 748static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport, int max_fte,
aaff1bea 749 enum fs_flow_table_type table_type,
c9f1b073
HHZ
750 enum fs_flow_table_op_mod op_mod,
751 u32 flags)
0c56b975
MG
752{
753 struct mlx5_flow_table *ft;
693c6883 754 int ret;
0c56b975
MG
755
756 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
757 if (!ft)
693c6883
MB
758 return ERR_PTR(-ENOMEM);
759
760 ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
761 if (ret) {
762 kfree(ft);
763 return ERR_PTR(ret);
764 }
0c56b975
MG
765
766 ft->level = level;
767 ft->node.type = FS_TYPE_FLOW_TABLE;
aaff1bea 768 ft->op_mod = op_mod;
0c56b975 769 ft->type = table_type;
efdc810b 770 ft->vport = vport;
0c56b975 771 ft->max_fte = max_fte;
c9f1b073 772 ft->flags = flags;
b3638e1a
MG
773 INIT_LIST_HEAD(&ft->fwd_rules);
774 mutex_init(&ft->lock);
0c56b975
MG
775
776 return ft;
777}
778
fdb6896f
MG
779/* If reverse is false, then we search for the first flow table in the
780 * root sub-tree from start(closest from right), else we search for the
781 * last flow table in the root sub-tree till start(closest from left).
782 */
783static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
784 struct list_head *start,
785 bool reverse)
786{
787#define list_advance_entry(pos, reverse) \
788 ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
789
790#define list_for_each_advance_continue(pos, head, reverse) \
791 for (pos = list_advance_entry(pos, reverse); \
792 &pos->list != (head); \
793 pos = list_advance_entry(pos, reverse))
794
795 struct fs_node *iter = list_entry(start, struct fs_node, list);
796 struct mlx5_flow_table *ft = NULL;
797
328edb49 798 if (!root || root->type == FS_TYPE_PRIO_CHAINS)
fdb6896f
MG
799 return NULL;
800
801 list_for_each_advance_continue(iter, &root->children, reverse) {
802 if (iter->type == FS_TYPE_FLOW_TABLE) {
803 fs_get_obj(ft, iter);
804 return ft;
805 }
806 ft = find_closest_ft_recursive(iter, &iter->children, reverse);
807 if (ft)
808 return ft;
809 }
810
811 return ft;
812}
813
59f8f7c8 814/* If reverse is false then return the first flow table in next priority of
fdb6896f
MG
815 * prio in the tree, else return the last flow table in the previous priority
816 * of prio in the tree.
817 */
818static struct mlx5_flow_table *find_closest_ft(struct fs_prio *prio, bool reverse)
819{
820 struct mlx5_flow_table *ft = NULL;
821 struct fs_node *curr_node;
822 struct fs_node *parent;
823
824 parent = prio->node.parent;
825 curr_node = &prio->node;
826 while (!ft && parent) {
827 ft = find_closest_ft_recursive(parent, &curr_node->list, reverse);
828 curr_node = parent;
829 parent = curr_node->parent;
830 }
831 return ft;
832}
833
834/* Assuming all the tree is locked by mutex chain lock */
835static struct mlx5_flow_table *find_next_chained_ft(struct fs_prio *prio)
836{
837 return find_closest_ft(prio, false);
838}
839
840/* Assuming all the tree is locked by mutex chain lock */
841static struct mlx5_flow_table *find_prev_chained_ft(struct fs_prio *prio)
842{
843 return find_closest_ft(prio, true);
844}
845
9254f8ed
MG
846static struct mlx5_flow_table *find_next_fwd_ft(struct mlx5_flow_table *ft,
847 struct mlx5_flow_act *flow_act)
848{
9254f8ed 849 struct fs_prio *prio;
59f8f7c8 850 bool next_ns;
9254f8ed 851
59f8f7c8
MG
852 next_ns = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS;
853 fs_get_obj(prio, next_ns ? ft->ns->node.parent : ft->node.parent);
9254f8ed 854
59f8f7c8 855 return find_next_chained_ft(prio);
9254f8ed
MG
856}
857
f90edfd2
MG
858static int connect_fts_in_prio(struct mlx5_core_dev *dev,
859 struct fs_prio *prio,
860 struct mlx5_flow_table *ft)
861{
af76c501 862 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
f90edfd2 863 struct mlx5_flow_table *iter;
f90edfd2
MG
864 int err;
865
866 fs_for_each_ft(iter, prio) {
ae288a48 867 err = root->cmds->modify_flow_table(root, iter, ft);
f90edfd2 868 if (err) {
6c4e9bcf
LR
869 mlx5_core_err(dev,
870 "Failed to modify flow table id %d, type %d, err %d\n",
871 iter->id, iter->type, err);
f90edfd2 872 /* The driver is out of sync with the FW */
f90edfd2
MG
873 return err;
874 }
875 }
876 return 0;
877}
878
879/* Connect flow tables from previous priority of prio to ft */
880static int connect_prev_fts(struct mlx5_core_dev *dev,
881 struct mlx5_flow_table *ft,
882 struct fs_prio *prio)
883{
884 struct mlx5_flow_table *prev_ft;
885
886 prev_ft = find_prev_chained_ft(prio);
887 if (prev_ft) {
888 struct fs_prio *prev_prio;
889
890 fs_get_obj(prev_prio, prev_ft->node.parent);
891 return connect_fts_in_prio(dev, prev_prio, ft);
892 }
893 return 0;
894}
895
2cc43b49
MG
896static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
897 *prio)
898{
899 struct mlx5_flow_root_namespace *root = find_root(&prio->node);
dae37456 900 struct mlx5_ft_underlay_qp *uqp;
2cc43b49 901 int min_level = INT_MAX;
aef6c443 902 int err = 0;
dae37456 903 u32 qpn;
2cc43b49
MG
904
905 if (root->root_ft)
906 min_level = root->root_ft->level;
907
908 if (ft->level >= min_level)
909 return 0;
910
dae37456
AV
911 if (list_empty(&root->underlay_qpns)) {
912 /* Don't set any QPN (zero) in case QPN list is empty */
913 qpn = 0;
ae288a48 914 err = root->cmds->update_root_ft(root, ft, qpn, false);
dae37456
AV
915 } else {
916 list_for_each_entry(uqp, &root->underlay_qpns, list) {
917 qpn = uqp->qpn;
ae288a48 918 err = root->cmds->update_root_ft(root, ft,
af76c501 919 qpn, false);
dae37456
AV
920 if (err)
921 break;
922 }
923 }
924
2cc43b49 925 if (err)
dae37456
AV
926 mlx5_core_warn(root->dev,
927 "Update root flow table of id(%u) qpn(%d) failed\n",
928 ft->id, qpn);
2cc43b49
MG
929 else
930 root->root_ft = ft;
931
932 return err;
933}
934
74491de9
MB
935static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
936 struct mlx5_flow_destination *dest)
b3638e1a 937{
af76c501 938 struct mlx5_flow_root_namespace *root;
b3638e1a
MG
939 struct mlx5_flow_table *ft;
940 struct mlx5_flow_group *fg;
941 struct fs_fte *fte;
bd5251db 942 int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
b3638e1a
MG
943 int err = 0;
944
945 fs_get_obj(fte, rule->node.parent);
d2ec6a35 946 if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
b3638e1a 947 return -EINVAL;
476d61b7 948 down_write_ref_node(&fte->node, false);
b3638e1a
MG
949 fs_get_obj(fg, fte->node.parent);
950 fs_get_obj(ft, fg->node.parent);
951
952 memcpy(&rule->dest_attr, dest, sizeof(*dest));
af76c501 953 root = find_root(&ft->node);
ae288a48 954 err = root->cmds->update_fte(root, ft, fg,
af76c501 955 modify_mask, fte);
476d61b7 956 up_write_ref_node(&fte->node, false);
b3638e1a
MG
957
958 return err;
959}
960
74491de9
MB
961int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
962 struct mlx5_flow_destination *new_dest,
963 struct mlx5_flow_destination *old_dest)
964{
965 int i;
966
967 if (!old_dest) {
968 if (handle->num_rules != 1)
969 return -EINVAL;
970 return _mlx5_modify_rule_destination(handle->rule[0],
971 new_dest);
972 }
973
974 for (i = 0; i < handle->num_rules; i++) {
975 if (mlx5_flow_dests_cmp(new_dest, &handle->rule[i]->dest_attr))
976 return _mlx5_modify_rule_destination(handle->rule[i],
977 new_dest);
978 }
979
980 return -EINVAL;
981}
982
b3638e1a
MG
983/* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
984static int connect_fwd_rules(struct mlx5_core_dev *dev,
985 struct mlx5_flow_table *new_next_ft,
986 struct mlx5_flow_table *old_next_ft)
987{
4c5009c5 988 struct mlx5_flow_destination dest = {};
b3638e1a
MG
989 struct mlx5_flow_rule *iter;
990 int err = 0;
991
992 /* new_next_ft and old_next_ft could be NULL only
993 * when we create/destroy the anchor flow table.
994 */
995 if (!new_next_ft || !old_next_ft)
996 return 0;
997
998 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
999 dest.ft = new_next_ft;
1000
1001 mutex_lock(&old_next_ft->lock);
1002 list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
1003 mutex_unlock(&old_next_ft->lock);
1004 list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
9254f8ed
MG
1005 if ((iter->sw_action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS) &&
1006 iter->ft->ns == new_next_ft->ns)
1007 continue;
1008
74491de9 1009 err = _mlx5_modify_rule_destination(iter, &dest);
b3638e1a
MG
1010 if (err)
1011 pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
1012 new_next_ft->id);
1013 }
1014 return 0;
1015}
1016
f90edfd2
MG
1017static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
1018 struct fs_prio *prio)
1019{
b3638e1a 1020 struct mlx5_flow_table *next_ft;
f90edfd2
MG
1021 int err = 0;
1022
1023 /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
1024
1025 if (list_empty(&prio->node.children)) {
1026 err = connect_prev_fts(dev, ft, prio);
1027 if (err)
1028 return err;
b3638e1a
MG
1029
1030 next_ft = find_next_chained_ft(prio);
1031 err = connect_fwd_rules(dev, ft, next_ft);
1032 if (err)
1033 return err;
f90edfd2
MG
1034 }
1035
1036 if (MLX5_CAP_FLOWTABLE(dev,
1037 flow_table_properties_nic_receive.modify_root))
1038 err = update_root_ft_create(ft, prio);
1039 return err;
1040}
1041
d63cd286
MG
1042static void list_add_flow_table(struct mlx5_flow_table *ft,
1043 struct fs_prio *prio)
1044{
1045 struct list_head *prev = &prio->node.children;
1046 struct mlx5_flow_table *iter;
1047
1048 fs_for_each_ft(iter, prio) {
1049 if (iter->level > ft->level)
1050 break;
1051 prev = &iter->node.list;
1052 }
1053 list_add(&ft->node.list, prev);
1054}
1055
efdc810b 1056static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
b3ba5149 1057 struct mlx5_flow_table_attr *ft_attr,
aaff1bea 1058 enum fs_flow_table_op_mod op_mod,
b3ba5149 1059 u16 vport)
0c56b975 1060{
b3ba5149 1061 struct mlx5_flow_root_namespace *root = find_root(&ns->node);
5281a0c9
PB
1062 bool unmanaged = ft_attr->flags & MLX5_FLOW_TABLE_UNMANAGED;
1063 struct mlx5_flow_table *next_ft;
b3ba5149 1064 struct fs_prio *fs_prio = NULL;
0c56b975 1065 struct mlx5_flow_table *ft;
0c56b975 1066 int log_table_sz;
b3ba5149 1067 int err;
0c56b975
MG
1068
1069 if (!root) {
1070 pr_err("mlx5: flow steering failed to find root of namespace\n");
1071 return ERR_PTR(-ENODEV);
1072 }
1073
2cc43b49 1074 mutex_lock(&root->chain_lock);
b3ba5149 1075 fs_prio = find_prio(ns, ft_attr->prio);
2cc43b49
MG
1076 if (!fs_prio) {
1077 err = -EINVAL;
1078 goto unlock_root;
1079 }
5281a0c9
PB
1080 if (!unmanaged) {
1081 /* The level is related to the
1082 * priority level range.
1083 */
1084 if (ft_attr->level >= fs_prio->num_levels) {
1085 err = -ENOSPC;
1086 goto unlock_root;
1087 }
1088
1089 ft_attr->level += fs_prio->start_level;
0c56b975 1090 }
5281a0c9 1091
d63cd286
MG
1092 /* The level is related to the
1093 * priority level range.
1094 */
b3ba5149 1095 ft = alloc_flow_table(ft_attr->level,
efdc810b 1096 vport,
b3ba5149 1097 ft_attr->max_fte ? roundup_pow_of_two(ft_attr->max_fte) : 0,
aaff1bea 1098 root->table_type,
b3ba5149 1099 op_mod, ft_attr->flags);
693c6883
MB
1100 if (IS_ERR(ft)) {
1101 err = PTR_ERR(ft);
2cc43b49 1102 goto unlock_root;
0c56b975
MG
1103 }
1104
bd71b08e 1105 tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
aaff1bea 1106 log_table_sz = ft->max_fte ? ilog2(ft->max_fte) : 0;
5281a0c9
PB
1107 next_ft = unmanaged ? ft_attr->next_ft :
1108 find_next_chained_ft(fs_prio);
f66ad830 1109 ft->def_miss_action = ns->def_miss_action;
9254f8ed 1110 ft->ns = ns;
ae288a48 1111 err = root->cmds->create_flow_table(root, ft, log_table_sz, next_ft);
0c56b975
MG
1112 if (err)
1113 goto free_ft;
1114
5281a0c9
PB
1115 if (!unmanaged) {
1116 err = connect_flow_table(root->dev, ft, fs_prio);
1117 if (err)
1118 goto destroy_ft;
1119 }
1120
19f100fe 1121 ft->node.active = true;
476d61b7 1122 down_write_ref_node(&fs_prio->node, false);
5281a0c9
PB
1123 if (!unmanaged) {
1124 tree_add_node(&ft->node, &fs_prio->node);
1125 list_add_flow_table(ft, fs_prio);
1126 } else {
1127 ft->node.root = fs_prio->node.root;
1128 }
0c56b975 1129 fs_prio->num_ft++;
476d61b7 1130 up_write_ref_node(&fs_prio->node, false);
2cc43b49 1131 mutex_unlock(&root->chain_lock);
8e4ca986 1132 trace_mlx5_fs_add_ft(ft);
0c56b975 1133 return ft;
2cc43b49 1134destroy_ft:
ae288a48 1135 root->cmds->destroy_flow_table(root, ft);
0c56b975
MG
1136free_ft:
1137 kfree(ft);
2cc43b49
MG
1138unlock_root:
1139 mutex_unlock(&root->chain_lock);
0c56b975
MG
1140 return ERR_PTR(err);
1141}
1142
efdc810b 1143struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
b3ba5149 1144 struct mlx5_flow_table_attr *ft_attr)
efdc810b 1145{
b3ba5149 1146 return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
efdc810b
MHY
1147}
1148
1149struct mlx5_flow_table *mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1150 int prio, int max_fte,
1151 u32 level, u16 vport)
1152{
b3ba5149
ES
1153 struct mlx5_flow_table_attr ft_attr = {};
1154
1155 ft_attr.max_fte = max_fte;
1156 ft_attr.level = level;
1157 ft_attr.prio = prio;
1158
57f35c93 1159 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_NORMAL, vport);
efdc810b
MHY
1160}
1161
b3ba5149
ES
1162struct mlx5_flow_table*
1163mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1164 int prio, u32 level)
aaff1bea 1165{
b3ba5149
ES
1166 struct mlx5_flow_table_attr ft_attr = {};
1167
1168 ft_attr.level = level;
1169 ft_attr.prio = prio;
1170 return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
aaff1bea
AH
1171}
1172EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1173
b3ba5149
ES
1174struct mlx5_flow_table*
1175mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
61dc7b01 1176 struct mlx5_flow_table_attr *ft_attr)
f0d22d18 1177{
79cdb0aa
PB
1178 int num_reserved_entries = ft_attr->autogroup.num_reserved_entries;
1179 int autogroups_max_fte = ft_attr->max_fte - num_reserved_entries;
1180 int max_num_groups = ft_attr->autogroup.max_num_groups;
f0d22d18
MG
1181 struct mlx5_flow_table *ft;
1182
79cdb0aa
PB
1183 if (max_num_groups > autogroups_max_fte)
1184 return ERR_PTR(-EINVAL);
1185 if (num_reserved_entries > ft_attr->max_fte)
f0d22d18
MG
1186 return ERR_PTR(-EINVAL);
1187
61dc7b01 1188 ft = mlx5_create_flow_table(ns, ft_attr);
f0d22d18
MG
1189 if (IS_ERR(ft))
1190 return ft;
1191
1192 ft->autogroup.active = true;
79cdb0aa
PB
1193 ft->autogroup.required_groups = max_num_groups;
1194 ft->autogroup.max_fte = autogroups_max_fte;
97fd8da2 1195 /* We save place for flow groups in addition to max types */
79cdb0aa 1196 ft->autogroup.group_size = autogroups_max_fte / (max_num_groups + 1);
f0d22d18
MG
1197
1198 return ft;
1199}
b217ea25 1200EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
f0d22d18 1201
f0d22d18
MG
1202struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1203 u32 *fg_in)
1204{
af76c501 1205 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
0d235c3f
MB
1206 void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1207 fg_in, match_criteria);
1208 u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
1209 fg_in,
1210 match_criteria_enable);
19f100fe
MG
1211 int start_index = MLX5_GET(create_flow_group_in, fg_in,
1212 start_flow_index);
1213 int end_index = MLX5_GET(create_flow_group_in, fg_in,
1214 end_flow_index);
f0d22d18 1215 struct mlx5_flow_group *fg;
19f100fe 1216 int err;
f0d22d18 1217
79cdb0aa 1218 if (ft->autogroup.active && start_index < ft->autogroup.max_fte)
f0d22d18
MG
1219 return ERR_PTR(-EPERM);
1220
476d61b7 1221 down_write_ref_node(&ft->node, false);
19f100fe
MG
1222 fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
1223 start_index, end_index,
1224 ft->node.children.prev);
476d61b7 1225 up_write_ref_node(&ft->node, false);
19f100fe
MG
1226 if (IS_ERR(fg))
1227 return fg;
1228
ae288a48 1229 err = root->cmds->create_flow_group(root, ft, fg_in, fg);
19f100fe 1230 if (err) {
476d61b7 1231 tree_put_node(&fg->node, false);
19f100fe
MG
1232 return ERR_PTR(err);
1233 }
1234 trace_mlx5_fs_add_fg(fg);
1235 fg->node.active = true;
0c56b975
MG
1236
1237 return fg;
1238}
1239
1240static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
1241{
1242 struct mlx5_flow_rule *rule;
1243
1244 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1245 if (!rule)
1246 return NULL;
1247
b3638e1a 1248 INIT_LIST_HEAD(&rule->next_ft);
0c56b975 1249 rule->node.type = FS_TYPE_FLOW_DEST;
60ab4584
AV
1250 if (dest)
1251 memcpy(&rule->dest_attr, dest, sizeof(*dest));
0c56b975
MG
1252
1253 return rule;
1254}
1255
74491de9
MB
1256static struct mlx5_flow_handle *alloc_handle(int num_rules)
1257{
1258 struct mlx5_flow_handle *handle;
1259
acafe7e3 1260 handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
74491de9
MB
1261 if (!handle)
1262 return NULL;
1263
1264 handle->num_rules = num_rules;
1265
1266 return handle;
1267}
1268
1269static void destroy_flow_handle(struct fs_fte *fte,
1270 struct mlx5_flow_handle *handle,
1271 struct mlx5_flow_destination *dest,
1272 int i)
1273{
1274 for (; --i >= 0;) {
dd8e1945 1275 if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
74491de9
MB
1276 fte->dests_size--;
1277 list_del(&handle->rule[i]->node.list);
1278 kfree(handle->rule[i]);
1279 }
1280 }
1281 kfree(handle);
1282}
1283
1284static struct mlx5_flow_handle *
1285create_flow_handle(struct fs_fte *fte,
1286 struct mlx5_flow_destination *dest,
1287 int dest_num,
1288 int *modify_mask,
1289 bool *new_rule)
1290{
1291 struct mlx5_flow_handle *handle;
1292 struct mlx5_flow_rule *rule = NULL;
1293 static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
1294 static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1295 int type;
1296 int i = 0;
1297
1298 handle = alloc_handle((dest_num) ? dest_num : 1);
1299 if (!handle)
1300 return ERR_PTR(-ENOMEM);
1301
1302 do {
1303 if (dest) {
1304 rule = find_flow_rule(fte, dest + i);
1305 if (rule) {
dd8e1945 1306 refcount_inc(&rule->node.refcount);
74491de9
MB
1307 goto rule_found;
1308 }
1309 }
1310
1311 *new_rule = true;
1312 rule = alloc_rule(dest + i);
1313 if (!rule)
1314 goto free_rules;
1315
1316 /* Add dest to dests list- we need flow tables to be in the
1317 * end of the list for forward to next prio rules.
1318 */
bd71b08e 1319 tree_init_node(&rule->node, NULL, del_sw_hw_rule);
74491de9
MB
1320 if (dest &&
1321 dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1322 list_add(&rule->node.list, &fte->node.children);
1323 else
1324 list_add_tail(&rule->node.list, &fte->node.children);
1325 if (dest) {
1326 fte->dests_size++;
1327
1328 type = dest[i].type ==
1329 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
1330 *modify_mask |= type ? count : dst;
1331 }
1332rule_found:
1333 handle->rule[i] = rule;
1334 } while (++i < dest_num);
1335
1336 return handle;
1337
1338free_rules:
1339 destroy_flow_handle(fte, handle, dest, i);
1340 return ERR_PTR(-ENOMEM);
1341}
1342
0c56b975 1343/* fte should not be deleted while calling this function */
74491de9
MB
1344static struct mlx5_flow_handle *
1345add_rule_fte(struct fs_fte *fte,
1346 struct mlx5_flow_group *fg,
1347 struct mlx5_flow_destination *dest,
1348 int dest_num,
1349 bool update_action)
0c56b975 1350{
af76c501 1351 struct mlx5_flow_root_namespace *root;
74491de9 1352 struct mlx5_flow_handle *handle;
0c56b975 1353 struct mlx5_flow_table *ft;
bd5251db 1354 int modify_mask = 0;
0c56b975 1355 int err;
74491de9 1356 bool new_rule = false;
0c56b975 1357
74491de9
MB
1358 handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
1359 &new_rule);
1360 if (IS_ERR(handle) || !new_rule)
1361 goto out;
bd5251db 1362
a6224985
MB
1363 if (update_action)
1364 modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
bd5251db 1365
0c56b975 1366 fs_get_obj(ft, fg->node.parent);
af76c501 1367 root = find_root(&fg->node);
0501fc47 1368 if (!(fte->status & FS_FTE_STATUS_EXISTING))
ae288a48 1369 err = root->cmds->create_fte(root, ft, fg, fte);
0c56b975 1370 else
ae288a48 1371 err = root->cmds->update_fte(root, ft, fg, modify_mask, fte);
0c56b975 1372 if (err)
74491de9 1373 goto free_handle;
0c56b975 1374
19f100fe 1375 fte->node.active = true;
0c56b975 1376 fte->status |= FS_FTE_STATUS_EXISTING;
454401ae 1377 atomic_inc(&fg->node.version);
0c56b975 1378
74491de9
MB
1379out:
1380 return handle;
0c56b975 1381
74491de9
MB
1382free_handle:
1383 destroy_flow_handle(fte, handle, dest, handle->num_rules);
0c56b975
MG
1384 return ERR_PTR(err);
1385}
1386
19f100fe 1387static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
5233794b 1388 const struct mlx5_flow_spec *spec)
0c56b975 1389{
af363705 1390 struct list_head *prev = &ft->node.children;
79cdb0aa 1391 u32 max_fte = ft->autogroup.max_fte;
19f100fe 1392 unsigned int candidate_index = 0;
f0d22d18 1393 unsigned int group_size = 0;
79cdb0aa 1394 struct mlx5_flow_group *fg;
f0d22d18
MG
1395
1396 if (!ft->autogroup.active)
1397 return ERR_PTR(-ENOENT);
1398
f0d22d18 1399 if (ft->autogroup.num_groups < ft->autogroup.required_groups)
97fd8da2 1400 group_size = ft->autogroup.group_size;
f0d22d18 1401
79cdb0aa 1402 /* max_fte == ft->autogroup.max_types */
f0d22d18
MG
1403 if (group_size == 0)
1404 group_size = 1;
1405
1406 /* sorted by start_index */
1407 fs_for_each_fg(fg, ft) {
1408 if (candidate_index + group_size > fg->start_index)
1409 candidate_index = fg->start_index + fg->max_ftes;
1410 else
1411 break;
1412 prev = &fg->node.list;
1413 }
1414
79cdb0aa 1415 if (candidate_index + group_size > max_fte)
19f100fe
MG
1416 return ERR_PTR(-ENOSPC);
1417
1418 fg = alloc_insert_flow_group(ft,
1419 spec->match_criteria_enable,
1420 spec->match_criteria,
1421 candidate_index,
1422 candidate_index + group_size - 1,
1423 prev);
1424 if (IS_ERR(fg))
f0d22d18 1425 goto out;
19f100fe 1426
97fd8da2
MG
1427 if (group_size == ft->autogroup.group_size)
1428 ft->autogroup.num_groups++;
19f100fe
MG
1429
1430out:
1431 return fg;
1432}
1433
1434static int create_auto_flow_group(struct mlx5_flow_table *ft,
1435 struct mlx5_flow_group *fg)
1436{
af76c501 1437 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
19f100fe
MG
1438 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1439 void *match_criteria_addr;
3e99df87
SK
1440 u8 src_esw_owner_mask_on;
1441 void *misc;
19f100fe
MG
1442 int err;
1443 u32 *in;
1444
1445 in = kvzalloc(inlen, GFP_KERNEL);
1446 if (!in)
1447 return -ENOMEM;
f0d22d18
MG
1448
1449 MLX5_SET(create_flow_group_in, in, match_criteria_enable,
19f100fe
MG
1450 fg->mask.match_criteria_enable);
1451 MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
1452 MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
1453 fg->max_ftes - 1);
3e99df87
SK
1454
1455 misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
1456 misc_parameters);
1457 src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
1458 source_eswitch_owner_vhca_id);
1459 MLX5_SET(create_flow_group_in, in,
1460 source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
1461
f0d22d18
MG
1462 match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1463 in, match_criteria);
19f100fe
MG
1464 memcpy(match_criteria_addr, fg->mask.match_criteria,
1465 sizeof(fg->mask.match_criteria));
1466
ae288a48 1467 err = root->cmds->create_flow_group(root, ft, in, fg);
19f100fe
MG
1468 if (!err) {
1469 fg->node.active = true;
1470 trace_mlx5_fs_add_fg(fg);
1471 }
f0d22d18 1472
f0d22d18 1473 kvfree(in);
19f100fe 1474 return err;
f0d22d18
MG
1475}
1476
814fb875
MB
1477static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1478 struct mlx5_flow_destination *d2)
1479{
1480 if (d1->type == d2->type) {
1481 if ((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT &&
1228e912
EB
1482 d1->vport.num == d2->vport.num &&
1483 d1->vport.flags == d2->vport.flags &&
c979c445
DL
1484 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ?
1485 (d1->vport.vhca_id == d2->vport.vhca_id) : true) &&
1228e912 1486 ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
2b688ea5
MG
1487 (d1->vport.pkt_reformat->id ==
1488 d2->vport.pkt_reformat->id) : true)) ||
814fb875
MB
1489 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1490 d1->ft == d2->ft) ||
1491 (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
664000b6
YH
1492 d1->tir_num == d2->tir_num) ||
1493 (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
1494 d1->ft_num == d2->ft_num))
814fb875
MB
1495 return true;
1496 }
1497
1498 return false;
1499}
1500
b3638e1a
MG
1501static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1502 struct mlx5_flow_destination *dest)
1503{
1504 struct mlx5_flow_rule *rule;
1505
1506 list_for_each_entry(rule, &fte->node.children, node.list) {
814fb875
MB
1507 if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1508 return rule;
b3638e1a
MG
1509 }
1510 return NULL;
1511}
1512
0d235c3f
MB
1513static bool check_conflicting_actions(u32 action1, u32 action2)
1514{
1515 u32 xored_actions = action1 ^ action2;
1516
1517 /* if one rule only wants to count, it's ok */
1518 if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
1519 action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
1520 return false;
1521
1522 if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
60786f09 1523 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
96de67a7 1524 MLX5_FLOW_CONTEXT_ACTION_DECAP |
0c06897a
OG
1525 MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1526 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
8da6fe2a
JL
1527 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
1528 MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
1529 MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
0d235c3f
MB
1530 return true;
1531
1532 return false;
1533}
1534
bb0ee7dc
JL
1535static int check_conflicting_ftes(struct fs_fte *fte,
1536 const struct mlx5_flow_context *flow_context,
1537 const struct mlx5_flow_act *flow_act)
0d235c3f 1538{
d2ec6a35 1539 if (check_conflicting_actions(flow_act->action, fte->action.action)) {
0d235c3f
MB
1540 mlx5_core_warn(get_dev(&fte->node),
1541 "Found two FTEs with conflicting actions\n");
1542 return -EEXIST;
1543 }
1544
bb0ee7dc
JL
1545 if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) &&
1546 fte->flow_context.flow_tag != flow_context->flow_tag) {
0d235c3f
MB
1547 mlx5_core_warn(get_dev(&fte->node),
1548 "FTE flow tag %u already exists with different flow tag %u\n",
bb0ee7dc
JL
1549 fte->flow_context.flow_tag,
1550 flow_context->flow_tag);
0d235c3f
MB
1551 return -EEXIST;
1552 }
1553
1554 return 0;
1555}
1556
74491de9 1557static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
5233794b 1558 const struct mlx5_flow_spec *spec,
66958ed9 1559 struct mlx5_flow_act *flow_act,
74491de9 1560 struct mlx5_flow_destination *dest,
693c6883
MB
1561 int dest_num,
1562 struct fs_fte *fte)
0c56b975 1563{
74491de9 1564 struct mlx5_flow_handle *handle;
bd71b08e 1565 int old_action;
74491de9 1566 int i;
bd71b08e 1567 int ret;
0c56b975 1568
bb0ee7dc 1569 ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act);
bd71b08e
MG
1570 if (ret)
1571 return ERR_PTR(ret);
0c56b975 1572
d2ec6a35
MB
1573 old_action = fte->action.action;
1574 fte->action.action |= flow_act->action;
bd71b08e
MG
1575 handle = add_rule_fte(fte, fg, dest, dest_num,
1576 old_action != flow_act->action);
74491de9 1577 if (IS_ERR(handle)) {
d2ec6a35 1578 fte->action.action = old_action;
693c6883 1579 return handle;
0c56b975 1580 }
bd71b08e 1581 trace_mlx5_fs_set_fte(fte, false);
0c56b975 1582
74491de9 1583 for (i = 0; i < handle->num_rules; i++) {
dd8e1945 1584 if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
74491de9 1585 tree_add_node(&handle->rule[i]->node, &fte->node);
4c03e69a
MB
1586 trace_mlx5_fs_add_rule(handle->rule[i]);
1587 }
74491de9 1588 }
74491de9 1589 return handle;
0c56b975
MG
1590}
1591
171c7625 1592static bool counter_is_valid(u32 action)
bd5251db 1593{
ae058314 1594 return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
4c2573e1 1595 MLX5_FLOW_CONTEXT_ACTION_ALLOW |
eafa6abd 1596 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
bd5251db
AV
1597}
1598
d63cd286 1599static bool dest_is_valid(struct mlx5_flow_destination *dest,
ff189b43 1600 struct mlx5_flow_act *flow_act,
d63cd286
MG
1601 struct mlx5_flow_table *ft)
1602{
ff189b43
PB
1603 bool ignore_level = flow_act->flags & FLOW_ACT_IGNORE_FLOW_LEVEL;
1604 u32 action = flow_act->action;
1605
bd5251db 1606 if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
171c7625 1607 return counter_is_valid(action);
bd5251db 1608
d63cd286
MG
1609 if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1610 return true;
1611
ff189b43 1612 if (ignore_level) {
006f623e
AL
1613 if (ft->type != FS_FT_FDB &&
1614 ft->type != FS_FT_NIC_RX)
ff189b43
PB
1615 return false;
1616
1617 if (dest->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
006f623e 1618 ft->type != dest->ft->type)
ff189b43
PB
1619 return false;
1620 }
1621
d63cd286
MG
1622 if (!dest || ((dest->type ==
1623 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
ff189b43 1624 (dest->ft->level <= ft->level && !ignore_level)))
d63cd286
MG
1625 return false;
1626 return true;
1627}
1628
46719d77
MG
1629struct match_list {
1630 struct list_head list;
693c6883 1631 struct mlx5_flow_group *g;
46719d77
MG
1632};
1633
b820ce00 1634static void free_match_list(struct match_list *head, bool ft_locked)
46719d77 1635{
b820ce00 1636 struct match_list *iter, *match_tmp;
46719d77 1637
b820ce00
EC
1638 list_for_each_entry_safe(iter, match_tmp, &head->list,
1639 list) {
1640 tree_put_node(&iter->g->node, ft_locked);
1641 list_del(&iter->list);
1642 kfree(iter);
46719d77
MG
1643 }
1644}
1645
b820ce00 1646static int build_match_list(struct match_list *match_head,
46719d77 1647 struct mlx5_flow_table *ft,
c1948390
MG
1648 const struct mlx5_flow_spec *spec,
1649 bool ft_locked)
46719d77 1650{
693c6883 1651 struct rhlist_head *tmp, *list;
46719d77
MG
1652 struct mlx5_flow_group *g;
1653 int err = 0;
693c6883
MB
1654
1655 rcu_read_lock();
46719d77 1656 INIT_LIST_HEAD(&match_head->list);
693c6883
MB
1657 /* Collect all fgs which has a matching match_criteria */
1658 list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
46719d77 1659 /* RCU is atomic, we can't execute FW commands here */
693c6883
MB
1660 rhl_for_each_entry_rcu(g, tmp, list, hash) {
1661 struct match_list *curr_match;
1662
b820ce00 1663 if (unlikely(!tree_get_node(&g->node)))
693c6883 1664 continue;
693c6883 1665
46719d77 1666 curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
693c6883 1667 if (!curr_match) {
c1948390 1668 free_match_list(match_head, ft_locked);
46719d77
MG
1669 err = -ENOMEM;
1670 goto out;
693c6883
MB
1671 }
1672 curr_match->g = g;
46719d77 1673 list_add_tail(&curr_match->list, &match_head->list);
693c6883 1674 }
46719d77 1675out:
693c6883 1676 rcu_read_unlock();
46719d77
MG
1677 return err;
1678}
1679
bd71b08e
MG
1680static u64 matched_fgs_get_version(struct list_head *match_head)
1681{
1682 struct match_list *iter;
1683 u64 version = 0;
1684
1685 list_for_each_entry(iter, match_head, list)
1686 version += (u64)atomic_read(&iter->g->node.version);
1687 return version;
1688}
1689
ad9421e3 1690static struct fs_fte *
1f0593e7
PP
1691lookup_fte_locked(struct mlx5_flow_group *g,
1692 const u32 *match_value,
1693 bool take_write)
7dee607e
PP
1694{
1695 struct fs_fte *fte_tmp;
1696
1f0593e7
PP
1697 if (take_write)
1698 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1699 else
1700 nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
1701 fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
1702 rhash_fte);
ad9421e3
RD
1703 if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
1704 fte_tmp = NULL;
1705 goto out;
1706 }
6237634d 1707 if (!fte_tmp->node.active) {
476d61b7 1708 tree_put_node(&fte_tmp->node, false);
6237634d
EB
1709 fte_tmp = NULL;
1710 goto out;
1711 }
ad9421e3
RD
1712
1713 nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
1714out:
1f0593e7
PP
1715 if (take_write)
1716 up_write_ref_node(&g->node, false);
7dee607e 1717 else
1f0593e7
PP
1718 up_read_ref_node(&g->node);
1719 return fte_tmp;
7dee607e
PP
1720}
1721
46719d77
MG
1722static struct mlx5_flow_handle *
1723try_add_to_existing_fg(struct mlx5_flow_table *ft,
bd71b08e 1724 struct list_head *match_head,
5233794b 1725 const struct mlx5_flow_spec *spec,
46719d77
MG
1726 struct mlx5_flow_act *flow_act,
1727 struct mlx5_flow_destination *dest,
bd71b08e
MG
1728 int dest_num,
1729 int ft_version)
46719d77 1730{
a369d4ac 1731 struct mlx5_flow_steering *steering = get_steering(&ft->node);
46719d77
MG
1732 struct mlx5_flow_group *g;
1733 struct mlx5_flow_handle *rule;
46719d77 1734 struct match_list *iter;
bd71b08e
MG
1735 bool take_write = false;
1736 struct fs_fte *fte;
dc638d11 1737 u64 version = 0;
f5c2ff17
MG
1738 int err;
1739
bb0ee7dc 1740 fte = alloc_fte(ft, spec, flow_act);
f5c2ff17
MG
1741 if (IS_ERR(fte))
1742 return ERR_PTR(-ENOMEM);
46719d77 1743
bd71b08e 1744search_again_locked:
d5634fee
PB
1745 if (flow_act->flags & FLOW_ACT_NO_APPEND)
1746 goto skip_search;
dc638d11
EC
1747 version = matched_fgs_get_version(match_head);
1748 /* Try to find an fte with identical match value and attempt update its
1749 * action.
1750 */
bd71b08e
MG
1751 list_for_each_entry(iter, match_head, list) {
1752 struct fs_fte *fte_tmp;
693c6883
MB
1753
1754 g = iter->g;
ad9421e3
RD
1755 fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1756 if (!fte_tmp)
bd71b08e 1757 continue;
bb0ee7dc 1758 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp);
476d61b7
EB
1759 up_write_ref_node(&fte_tmp->node, false);
1760 tree_put_node(&fte_tmp->node, false);
a369d4ac 1761 kmem_cache_free(steering->ftes_cache, fte);
bd71b08e 1762 return rule;
693c6883
MB
1763 }
1764
d5634fee
PB
1765skip_search:
1766 /* No group with matching fte found, or we skipped the search.
1767 * Try to add a new fte to any matching fg.
1768 */
1769
bd71b08e
MG
1770 /* Check the ft version, for case that new flow group
1771 * was added while the fgs weren't locked
1772 */
1773 if (atomic_read(&ft->node.version) != ft_version) {
1774 rule = ERR_PTR(-EAGAIN);
1775 goto out;
1776 }
b92af5a7 1777
dc638d11
EC
1778 /* Check the fgs version. If version have changed it could be that an
1779 * FTE with the same match value was added while the fgs weren't
1780 * locked.
bd71b08e 1781 */
dc638d11
EC
1782 if (!(flow_act->flags & FLOW_ACT_NO_APPEND) &&
1783 version != matched_fgs_get_version(match_head)) {
ad9421e3 1784 take_write = true;
bd71b08e 1785 goto search_again_locked;
ad9421e3 1786 }
bd71b08e
MG
1787
1788 list_for_each_entry(iter, match_head, list) {
1789 g = iter->g;
1790
ad9421e3
RD
1791 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
1792
49c0355d
PB
1793 if (!g->node.active) {
1794 up_write_ref_node(&g->node, false);
1795 continue;
1796 }
1797
f5c2ff17
MG
1798 err = insert_fte(g, fte);
1799 if (err) {
476d61b7 1800 up_write_ref_node(&g->node, false);
f5c2ff17 1801 if (err == -ENOSPC)
bd71b08e 1802 continue;
a369d4ac 1803 kmem_cache_free(steering->ftes_cache, fte);
f5c2ff17 1804 return ERR_PTR(err);
bd71b08e 1805 }
693c6883 1806
bd71b08e 1807 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
476d61b7 1808 up_write_ref_node(&g->node, false);
bb0ee7dc 1809 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
476d61b7 1810 up_write_ref_node(&fte->node, false);
bd71b08e
MG
1811 return rule;
1812 }
1813 rule = ERR_PTR(-ENOENT);
1814out:
a369d4ac 1815 kmem_cache_free(steering->ftes_cache, fte);
693c6883
MB
1816 return rule;
1817}
1818
74491de9
MB
1819static struct mlx5_flow_handle *
1820_mlx5_add_flow_rules(struct mlx5_flow_table *ft,
5233794b 1821 const struct mlx5_flow_spec *spec,
66958ed9 1822 struct mlx5_flow_act *flow_act,
74491de9
MB
1823 struct mlx5_flow_destination *dest,
1824 int dest_num)
66958ed9 1825
0c56b975 1826{
a369d4ac 1827 struct mlx5_flow_steering *steering = get_steering(&ft->node);
74491de9 1828 struct mlx5_flow_handle *rule;
b820ce00
EC
1829 struct match_list match_head;
1830 struct mlx5_flow_group *g;
bd71b08e
MG
1831 bool take_write = false;
1832 struct fs_fte *fte;
1833 int version;
19f100fe 1834 int err;
74491de9 1835 int i;
0c56b975 1836
693c6883 1837 if (!check_valid_spec(spec))
0d235c3f
MB
1838 return ERR_PTR(-EINVAL);
1839
74491de9 1840 for (i = 0; i < dest_num; i++) {
ff189b43 1841 if (!dest_is_valid(&dest[i], flow_act, ft))
74491de9
MB
1842 return ERR_PTR(-EINVAL);
1843 }
bd71b08e
MG
1844 nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1845search_again_locked:
1846 version = atomic_read(&ft->node.version);
60ab4584 1847
bd71b08e 1848 /* Collect all fgs which has a matching match_criteria */
c1948390 1849 err = build_match_list(&match_head, ft, spec, take_write);
9238e380
VB
1850 if (err) {
1851 if (take_write)
476d61b7 1852 up_write_ref_node(&ft->node, false);
07130477
RD
1853 else
1854 up_read_ref_node(&ft->node);
bd71b08e 1855 return ERR_PTR(err);
9238e380 1856 }
bd71b08e
MG
1857
1858 if (!take_write)
1859 up_read_ref_node(&ft->node);
1860
1861 rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
1862 dest_num, version);
c1948390 1863 free_match_list(&match_head, take_write);
bd71b08e 1864 if (!IS_ERR(rule) ||
9238e380
VB
1865 (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
1866 if (take_write)
476d61b7 1867 up_write_ref_node(&ft->node, false);
bd71b08e 1868 return rule;
9238e380 1869 }
bd71b08e
MG
1870
1871 if (!take_write) {
1872 nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
1873 take_write = true;
1874 }
1875
1876 if (PTR_ERR(rule) == -EAGAIN ||
1877 version != atomic_read(&ft->node.version))
1878 goto search_again_locked;
f0d22d18 1879
19f100fe 1880 g = alloc_auto_flow_group(ft, spec);
c3f9bf62 1881 if (IS_ERR(g)) {
d34c6efc 1882 rule = ERR_CAST(g);
476d61b7 1883 up_write_ref_node(&ft->node, false);
bd71b08e 1884 return rule;
c3f9bf62
MG
1885 }
1886
84c7af63
PP
1887 fte = alloc_fte(ft, spec, flow_act);
1888 if (IS_ERR(fte)) {
1889 up_write_ref_node(&ft->node, false);
1890 err = PTR_ERR(fte);
1891 goto err_alloc_fte;
1892 }
1893
bd71b08e 1894 nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
476d61b7 1895 up_write_ref_node(&ft->node, false);
bd71b08e 1896
19f100fe 1897 err = create_auto_flow_group(ft, g);
bd71b08e
MG
1898 if (err)
1899 goto err_release_fg;
1900
f5c2ff17 1901 err = insert_fte(g, fte);
84c7af63 1902 if (err)
f5c2ff17 1903 goto err_release_fg;
f5c2ff17 1904
bd71b08e 1905 nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
476d61b7 1906 up_write_ref_node(&g->node, false);
bb0ee7dc 1907 rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
476d61b7 1908 up_write_ref_node(&fte->node, false);
476d61b7 1909 tree_put_node(&g->node, false);
0c56b975 1910 return rule;
bd71b08e
MG
1911
1912err_release_fg:
476d61b7 1913 up_write_ref_node(&g->node, false);
84c7af63
PP
1914 kmem_cache_free(steering->ftes_cache, fte);
1915err_alloc_fte:
476d61b7 1916 tree_put_node(&g->node, false);
bd71b08e 1917 return ERR_PTR(err);
0c56b975 1918}
b3638e1a
MG
1919
1920static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
1921{
1922 return ((ft->type == FS_FT_NIC_RX) &&
1923 (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
1924}
1925
74491de9
MB
1926struct mlx5_flow_handle *
1927mlx5_add_flow_rules(struct mlx5_flow_table *ft,
5233794b 1928 const struct mlx5_flow_spec *spec,
66958ed9 1929 struct mlx5_flow_act *flow_act,
74491de9 1930 struct mlx5_flow_destination *dest,
cf916ffb 1931 int num_dest)
b3638e1a
MG
1932{
1933 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
5c2aa8ae 1934 static const struct mlx5_flow_spec zero_spec = {};
14c129e3 1935 struct mlx5_flow_destination *gen_dest = NULL;
b3638e1a 1936 struct mlx5_flow_table *next_ft = NULL;
74491de9 1937 struct mlx5_flow_handle *handle = NULL;
66958ed9 1938 u32 sw_action = flow_act->action;
14c129e3 1939 int i;
b3638e1a 1940
5c2aa8ae
MB
1941 if (!spec)
1942 spec = &zero_spec;
1943
9254f8ed 1944 if (!is_fwd_next_action(sw_action))
14c129e3
MG
1945 return _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
1946
1947 if (!fwd_next_prio_supported(ft))
1948 return ERR_PTR(-EOPNOTSUPP);
1949
1950 mutex_lock(&root->chain_lock);
9254f8ed 1951 next_ft = find_next_fwd_ft(ft, flow_act);
14c129e3
MG
1952 if (!next_ft) {
1953 handle = ERR_PTR(-EOPNOTSUPP);
1954 goto unlock;
b3638e1a
MG
1955 }
1956
14c129e3
MG
1957 gen_dest = kcalloc(num_dest + 1, sizeof(*dest),
1958 GFP_KERNEL);
1959 if (!gen_dest) {
1960 handle = ERR_PTR(-ENOMEM);
1961 goto unlock;
1962 }
1963 for (i = 0; i < num_dest; i++)
1964 gen_dest[i] = dest[i];
1965 gen_dest[i].type =
1966 MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1967 gen_dest[i].ft = next_ft;
1968 dest = gen_dest;
1969 num_dest++;
9254f8ed
MG
1970 flow_act->action &= ~(MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
1971 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
14c129e3 1972 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
cf916ffb 1973 handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
14c129e3
MG
1974 if (IS_ERR(handle))
1975 goto unlock;
1976
1977 if (list_empty(&handle->rule[num_dest - 1]->next_ft)) {
1978 mutex_lock(&next_ft->lock);
1979 list_add(&handle->rule[num_dest - 1]->next_ft,
1980 &next_ft->fwd_rules);
1981 mutex_unlock(&next_ft->lock);
9254f8ed
MG
1982 handle->rule[num_dest - 1]->sw_action = sw_action;
1983 handle->rule[num_dest - 1]->ft = ft;
b3638e1a 1984 }
14c129e3
MG
1985unlock:
1986 mutex_unlock(&root->chain_lock);
1987 kfree(gen_dest);
74491de9 1988 return handle;
b3638e1a 1989}
74491de9 1990EXPORT_SYMBOL(mlx5_add_flow_rules);
0c56b975 1991
74491de9 1992void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
0c56b975 1993{
718ce4d6 1994 struct fs_fte *fte;
74491de9
MB
1995 int i;
1996
718ce4d6
EB
1997 /* In order to consolidate the HW changes we lock the FTE for other
1998 * changes, and increase its refcount, in order not to perform the
1999 * "del" functions of the FTE. Will handle them here.
2000 * The removal of the rules is done under locked FTE.
2001 * After removing all the handle's rules, if there are remaining
2002 * rules, it means we just need to modify the FTE in FW, and
2003 * unlock/decrease the refcount we increased before.
2004 * Otherwise, it means the FTE should be deleted. First delete the
2005 * FTE in FW. Then, unlock the FTE, and proceed the tree_put_node of
2006 * the FTE, which will handle the last decrease of the refcount, as
2007 * well as required handling of its parent.
2008 */
2009 fs_get_obj(fte, handle->rule[0]->node.parent);
2010 down_write_ref_node(&fte->node, false);
74491de9 2011 for (i = handle->num_rules - 1; i >= 0; i--)
718ce4d6
EB
2012 tree_remove_node(&handle->rule[i]->node, true);
2013 if (fte->modify_mask && fte->dests_size) {
2014 modify_fte(fte);
2015 up_write_ref_node(&fte->node, false);
2016 } else {
2017 del_hw_fte(&fte->node);
cefc2355
MG
2018 /* Avoid double call to del_hw_fte */
2019 fte->node.del_hw_func = NULL;
2020 up_write_ref_node(&fte->node, false);
718ce4d6
EB
2021 tree_put_node(&fte->node, false);
2022 }
74491de9 2023 kfree(handle);
0c56b975 2024}
74491de9 2025EXPORT_SYMBOL(mlx5_del_flow_rules);
0c56b975 2026
2cc43b49
MG
2027/* Assuming prio->node.children(flow tables) is sorted by level */
2028static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
2029{
2030 struct fs_prio *prio;
2031
2032 fs_get_obj(prio, ft->node.parent);
2033
2034 if (!list_is_last(&ft->node.list, &prio->node.children))
2035 return list_next_entry(ft, node.list);
2036 return find_next_chained_ft(prio);
2037}
2038
2039static int update_root_ft_destroy(struct mlx5_flow_table *ft)
2040{
2041 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
dae37456 2042 struct mlx5_ft_underlay_qp *uqp;
2cc43b49 2043 struct mlx5_flow_table *new_root_ft = NULL;
dae37456
AV
2044 int err = 0;
2045 u32 qpn;
2cc43b49
MG
2046
2047 if (root->root_ft != ft)
2048 return 0;
2049
2050 new_root_ft = find_next_ft(ft);
dae37456
AV
2051 if (!new_root_ft) {
2052 root->root_ft = NULL;
2053 return 0;
2054 }
2055
2056 if (list_empty(&root->underlay_qpns)) {
2057 /* Don't set any QPN (zero) in case QPN list is empty */
2058 qpn = 0;
ae288a48 2059 err = root->cmds->update_root_ft(root, new_root_ft,
af76c501 2060 qpn, false);
dae37456
AV
2061 } else {
2062 list_for_each_entry(uqp, &root->underlay_qpns, list) {
2063 qpn = uqp->qpn;
ae288a48 2064 err = root->cmds->update_root_ft(root,
af76c501
MB
2065 new_root_ft, qpn,
2066 false);
dae37456
AV
2067 if (err)
2068 break;
2cc43b49 2069 }
2cc43b49 2070 }
dae37456
AV
2071
2072 if (err)
2073 mlx5_core_warn(root->dev,
2074 "Update root flow table of id(%u) qpn(%d) failed\n",
2075 ft->id, qpn);
2076 else
2077 root->root_ft = new_root_ft;
2078
2cc43b49
MG
2079 return 0;
2080}
2081
f90edfd2
MG
2082/* Connect flow table from previous priority to
2083 * the next flow table.
2084 */
2085static int disconnect_flow_table(struct mlx5_flow_table *ft)
2086{
2087 struct mlx5_core_dev *dev = get_dev(&ft->node);
2088 struct mlx5_flow_table *next_ft;
2089 struct fs_prio *prio;
2090 int err = 0;
2091
2092 err = update_root_ft_destroy(ft);
2093 if (err)
2094 return err;
2095
2096 fs_get_obj(prio, ft->node.parent);
2097 if (!(list_first_entry(&prio->node.children,
2098 struct mlx5_flow_table,
2099 node.list) == ft))
2100 return 0;
2101
2102 next_ft = find_next_chained_ft(prio);
b3638e1a
MG
2103 err = connect_fwd_rules(dev, next_ft, ft);
2104 if (err)
2105 return err;
2106
f90edfd2
MG
2107 err = connect_prev_fts(dev, next_ft, prio);
2108 if (err)
2109 mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2110 ft->id);
2111 return err;
2112}
2113
86d722ad 2114int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
0c56b975 2115{
2cc43b49
MG
2116 struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2117 int err = 0;
2118
2119 mutex_lock(&root->chain_lock);
5281a0c9
PB
2120 if (!(ft->flags & MLX5_FLOW_TABLE_UNMANAGED))
2121 err = disconnect_flow_table(ft);
2cc43b49
MG
2122 if (err) {
2123 mutex_unlock(&root->chain_lock);
2124 return err;
2125 }
476d61b7 2126 if (tree_remove_node(&ft->node, false))
0c56b975
MG
2127 mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
2128 ft->id);
2cc43b49 2129 mutex_unlock(&root->chain_lock);
0c56b975 2130
2cc43b49 2131 return err;
0c56b975 2132}
b217ea25 2133EXPORT_SYMBOL(mlx5_destroy_flow_table);
0c56b975 2134
86d722ad 2135void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
0c56b975 2136{
476d61b7 2137 if (tree_remove_node(&fg->node, false))
0c56b975
MG
2138 mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
2139 fg->id);
2140}
25302363 2141
328edb49
PB
2142struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
2143 int n)
2144{
2145 struct mlx5_flow_steering *steering = dev->priv.steering;
2146
2147 if (!steering || !steering->fdb_sub_ns)
2148 return NULL;
2149
2150 return steering->fdb_sub_ns[n];
2151}
2152EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
2153
86d722ad
MG
2154struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
2155 enum mlx5_flow_namespace_type type)
25302363 2156{
fba53f7b
MG
2157 struct mlx5_flow_steering *steering = dev->priv.steering;
2158 struct mlx5_flow_root_namespace *root_ns;
8ce78257 2159 int prio = 0;
78228cbd 2160 struct fs_prio *fs_prio;
25302363
MG
2161 struct mlx5_flow_namespace *ns;
2162
fba53f7b 2163 if (!steering)
25302363
MG
2164 return NULL;
2165
2166 switch (type) {
25302363 2167 case MLX5_FLOW_NAMESPACE_FDB:
fba53f7b
MG
2168 if (steering->fdb_root_ns)
2169 return &steering->fdb_root_ns->ns;
2226dcb4 2170 return NULL;
87d22483
MG
2171 case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
2172 if (steering->sniffer_rx_root_ns)
2173 return &steering->sniffer_rx_root_ns->ns;
2226dcb4 2174 return NULL;
87d22483
MG
2175 case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
2176 if (steering->sniffer_tx_root_ns)
2177 return &steering->sniffer_tx_root_ns->ns;
25302363 2178 return NULL;
2226dcb4
MB
2179 default:
2180 break;
25302363
MG
2181 }
2182
ee92e4f1
HN
2183 if (type == MLX5_FLOW_NAMESPACE_EGRESS ||
2184 type == MLX5_FLOW_NAMESPACE_EGRESS_KERNEL) {
8ce78257 2185 root_ns = steering->egress_root_ns;
ee92e4f1 2186 prio = type - MLX5_FLOW_NAMESPACE_EGRESS;
e6806e9a
MZ
2187 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
2188 root_ns = steering->rdma_rx_root_ns;
2189 prio = RDMA_RX_BYPASS_PRIO;
2190 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL) {
2191 root_ns = steering->rdma_rx_root_ns;
2192 prio = RDMA_RX_KERNEL_PRIO;
24670b1a
MG
2193 } else if (type == MLX5_FLOW_NAMESPACE_RDMA_TX) {
2194 root_ns = steering->rdma_tx_root_ns;
8ce78257
MB
2195 } else { /* Must be NIC RX */
2196 root_ns = steering->root_ns;
2197 prio = type;
25302363
MG
2198 }
2199
fba53f7b
MG
2200 if (!root_ns)
2201 return NULL;
2202
25302363
MG
2203 fs_prio = find_prio(&root_ns->ns, prio);
2204 if (!fs_prio)
2205 return NULL;
2206
2207 ns = list_first_entry(&fs_prio->node.children,
2208 typeof(*ns),
2209 node.list);
2210
2211 return ns;
2212}
b217ea25 2213EXPORT_SYMBOL(mlx5_get_flow_namespace);
25302363 2214
9b93ab98
GP
2215struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
2216 enum mlx5_flow_namespace_type type,
2217 int vport)
2218{
2219 struct mlx5_flow_steering *steering = dev->priv.steering;
2220
2752b823 2221 if (!steering || vport >= mlx5_eswitch_get_total_vports(dev))
9b93ab98
GP
2222 return NULL;
2223
2224 switch (type) {
2225 case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
2226 if (steering->esw_egress_root_ns &&
2227 steering->esw_egress_root_ns[vport])
2228 return &steering->esw_egress_root_ns[vport]->ns;
2229 else
2230 return NULL;
2231 case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
2232 if (steering->esw_ingress_root_ns &&
2233 steering->esw_ingress_root_ns[vport])
2234 return &steering->esw_ingress_root_ns[vport]->ns;
2235 else
2236 return NULL;
2237 default:
2238 return NULL;
2239 }
2240}
2241
328edb49
PB
2242static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2243 unsigned int prio,
2244 int num_levels,
2245 enum fs_node_type type)
25302363
MG
2246{
2247 struct fs_prio *fs_prio;
2248
2249 fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
2250 if (!fs_prio)
2251 return ERR_PTR(-ENOMEM);
2252
328edb49 2253 fs_prio->node.type = type;
139ed6c6 2254 tree_init_node(&fs_prio->node, NULL, del_sw_prio);
25302363 2255 tree_add_node(&fs_prio->node, &ns->node);
a257b94a 2256 fs_prio->num_levels = num_levels;
25302363 2257 fs_prio->prio = prio;
25302363
MG
2258 list_add_tail(&fs_prio->node.list, &ns->node.children);
2259
2260 return fs_prio;
2261}
2262
328edb49
PB
2263static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2264 unsigned int prio,
2265 int num_levels)
2266{
2267 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2268}
2269
2270static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2271 unsigned int prio, int num_levels)
2272{
2273 return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2274}
2275
25302363
MG
2276static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
2277 *ns)
2278{
2279 ns->node.type = FS_TYPE_NAMESPACE;
2280
2281 return ns;
2282}
2283
f66ad830
MZ
2284static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2285 int def_miss_act)
25302363
MG
2286{
2287 struct mlx5_flow_namespace *ns;
2288
2289 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
2290 if (!ns)
2291 return ERR_PTR(-ENOMEM);
2292
2293 fs_init_namespace(ns);
f66ad830 2294 ns->def_miss_action = def_miss_act;
139ed6c6 2295 tree_init_node(&ns->node, NULL, del_sw_ns);
25302363
MG
2296 tree_add_node(&ns->node, &prio->node);
2297 list_add_tail(&ns->node.list, &prio->node.children);
2298
2299 return ns;
2300}
2301
13de6c10
MG
2302static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
2303 struct init_tree_node *prio_metadata)
4cbdd30e
MG
2304{
2305 struct fs_prio *fs_prio;
2306 int i;
2307
2308 for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
13de6c10 2309 fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
4cbdd30e
MG
2310 if (IS_ERR(fs_prio))
2311 return PTR_ERR(fs_prio);
2312 }
2313 return 0;
2314}
2315
8d40d162
MG
2316#define FLOW_TABLE_BIT_SZ 1
2317#define GET_FLOW_TABLE_CAP(dev, offset) \
701052c5 2318 ((be32_to_cpu(*((__be32 *)(dev->caps.hca_cur[MLX5_CAP_FLOW_TABLE]) + \
8d40d162
MG
2319 offset / 32)) >> \
2320 (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
2321static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
2322{
2323 int i;
2324
2325 for (i = 0; i < caps->arr_sz; i++) {
2326 if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
2327 return false;
2328 }
2329 return true;
2330}
2331
fba53f7b 2332static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
8d40d162 2333 struct init_tree_node *init_node,
25302363
MG
2334 struct fs_node *fs_parent_node,
2335 struct init_tree_node *init_parent_node,
13de6c10 2336 int prio)
25302363 2337{
fba53f7b 2338 int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
8d40d162
MG
2339 flow_table_properties_nic_receive.
2340 max_ft_level);
25302363
MG
2341 struct mlx5_flow_namespace *fs_ns;
2342 struct fs_prio *fs_prio;
2343 struct fs_node *base;
2344 int i;
2345 int err;
2346
2347 if (init_node->type == FS_TYPE_PRIO) {
8d40d162 2348 if ((init_node->min_ft_level > max_ft_level) ||
fba53f7b 2349 !has_required_caps(steering->dev, &init_node->caps))
8d40d162 2350 return 0;
25302363
MG
2351
2352 fs_get_obj(fs_ns, fs_parent_node);
4cbdd30e 2353 if (init_node->num_leaf_prios)
13de6c10
MG
2354 return create_leaf_prios(fs_ns, prio, init_node);
2355 fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
25302363
MG
2356 if (IS_ERR(fs_prio))
2357 return PTR_ERR(fs_prio);
2358 base = &fs_prio->node;
2359 } else if (init_node->type == FS_TYPE_NAMESPACE) {
2360 fs_get_obj(fs_prio, fs_parent_node);
f66ad830 2361 fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
25302363
MG
2362 if (IS_ERR(fs_ns))
2363 return PTR_ERR(fs_ns);
2364 base = &fs_ns->node;
2365 } else {
2366 return -EINVAL;
2367 }
13de6c10 2368 prio = 0;
25302363 2369 for (i = 0; i < init_node->ar_size; i++) {
fba53f7b 2370 err = init_root_tree_recursive(steering, &init_node->children[i],
13de6c10 2371 base, init_node, prio);
25302363
MG
2372 if (err)
2373 return err;
13de6c10
MG
2374 if (init_node->children[i].type == FS_TYPE_PRIO &&
2375 init_node->children[i].num_leaf_prios) {
2376 prio += init_node->children[i].num_leaf_prios;
2377 }
25302363
MG
2378 }
2379
2380 return 0;
2381}
2382
fba53f7b 2383static int init_root_tree(struct mlx5_flow_steering *steering,
8d40d162 2384 struct init_tree_node *init_node,
25302363
MG
2385 struct fs_node *fs_parent_node)
2386{
2387 int i;
2388 struct mlx5_flow_namespace *fs_ns;
2389 int err;
2390
2391 fs_get_obj(fs_ns, fs_parent_node);
2392 for (i = 0; i < init_node->ar_size; i++) {
fba53f7b 2393 err = init_root_tree_recursive(steering, &init_node->children[i],
25302363
MG
2394 &fs_ns->node,
2395 init_node, i);
2396 if (err)
2397 return err;
2398 }
2399 return 0;
2400}
2401
6eb7a268
RD
2402static void del_sw_root_ns(struct fs_node *node)
2403{
9ca41539
RD
2404 struct mlx5_flow_root_namespace *root_ns;
2405 struct mlx5_flow_namespace *ns;
2406
2407 fs_get_obj(ns, node);
2408 root_ns = container_of(ns, struct mlx5_flow_root_namespace, ns);
2409 mutex_destroy(&root_ns->chain_lock);
6eb7a268
RD
2410 kfree(node);
2411}
2412
af76c501
MB
2413static struct mlx5_flow_root_namespace
2414*create_root_ns(struct mlx5_flow_steering *steering,
2415 enum fs_flow_table_type table_type)
25302363 2416{
af76c501 2417 const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
25302363
MG
2418 struct mlx5_flow_root_namespace *root_ns;
2419 struct mlx5_flow_namespace *ns;
2420
8c8eea07 2421 if (mlx5_fpga_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
05564d0a
AY
2422 (table_type == FS_FT_NIC_RX || table_type == FS_FT_NIC_TX))
2423 cmds = mlx5_fs_cmd_get_default_ipsec_fpga_cmds(table_type);
2424
86d722ad 2425 /* Create the root namespace */
25fa506b 2426 root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL);
25302363
MG
2427 if (!root_ns)
2428 return NULL;
2429
fba53f7b 2430 root_ns->dev = steering->dev;
25302363 2431 root_ns->table_type = table_type;
af76c501 2432 root_ns->cmds = cmds;
25302363 2433
dae37456
AV
2434 INIT_LIST_HEAD(&root_ns->underlay_qpns);
2435
25302363
MG
2436 ns = &root_ns->ns;
2437 fs_init_namespace(ns);
2cc43b49 2438 mutex_init(&root_ns->chain_lock);
6eb7a268 2439 tree_init_node(&ns->node, NULL, del_sw_root_ns);
25302363
MG
2440 tree_add_node(&ns->node, NULL);
2441
2442 return root_ns;
2443}
2444
655227ed
MG
2445static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2446
2447static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2448{
2449 struct fs_prio *prio;
2450
2451 fs_for_each_prio(prio, ns) {
a257b94a 2452 /* This updates prio start_level and num_levels */
655227ed 2453 set_prio_attrs_in_prio(prio, acc_level);
a257b94a 2454 acc_level += prio->num_levels;
655227ed
MG
2455 }
2456 return acc_level;
2457}
2458
2459static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2460{
2461 struct mlx5_flow_namespace *ns;
2462 int acc_level_ns = acc_level;
2463
2464 prio->start_level = acc_level;
34b13cb3 2465 fs_for_each_ns(ns, prio) {
a257b94a 2466 /* This updates start_level and num_levels of ns's priority descendants */
655227ed 2467 acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
34b13cb3
PB
2468
2469 /* If this a prio with chains, and we can jump from one chain
2470 * (namepsace) to another, so we accumulate the levels
2471 */
2472 if (prio->node.type == FS_TYPE_PRIO_CHAINS)
2473 acc_level = acc_level_ns;
2474 }
2475
a257b94a
MG
2476 if (!prio->num_levels)
2477 prio->num_levels = acc_level_ns - prio->start_level;
2478 WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
655227ed
MG
2479}
2480
2481static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2482{
2483 struct mlx5_flow_namespace *ns = &root_ns->ns;
2484 struct fs_prio *prio;
2485 int start_level = 0;
2486
2487 fs_for_each_prio(prio, ns) {
2488 set_prio_attrs_in_prio(prio, start_level);
a257b94a 2489 start_level += prio->num_levels;
655227ed
MG
2490 }
2491}
2492
153fefbf
MG
2493#define ANCHOR_PRIO 0
2494#define ANCHOR_SIZE 1
d63cd286 2495#define ANCHOR_LEVEL 0
fba53f7b 2496static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
153fefbf
MG
2497{
2498 struct mlx5_flow_namespace *ns = NULL;
b3ba5149 2499 struct mlx5_flow_table_attr ft_attr = {};
153fefbf
MG
2500 struct mlx5_flow_table *ft;
2501
fba53f7b 2502 ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
eff596da 2503 if (WARN_ON(!ns))
153fefbf 2504 return -EINVAL;
b3ba5149
ES
2505
2506 ft_attr.max_fte = ANCHOR_SIZE;
2507 ft_attr.level = ANCHOR_LEVEL;
2508 ft_attr.prio = ANCHOR_PRIO;
2509
2510 ft = mlx5_create_flow_table(ns, &ft_attr);
153fefbf 2511 if (IS_ERR(ft)) {
fba53f7b 2512 mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
153fefbf
MG
2513 return PTR_ERR(ft);
2514 }
2515 return 0;
2516}
2517
fba53f7b 2518static int init_root_ns(struct mlx5_flow_steering *steering)
25302363 2519{
9c26f5f8
TB
2520 int err;
2521
fba53f7b 2522 steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
42fb18fd 2523 if (!steering->root_ns)
9c26f5f8 2524 return -ENOMEM;
25302363 2525
9c26f5f8
TB
2526 err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
2527 if (err)
2528 goto out_err;
25302363 2529
fba53f7b 2530 set_prio_attrs(steering->root_ns);
9c26f5f8
TB
2531 err = create_anchor_flow_table(steering);
2532 if (err)
2533 goto out_err;
153fefbf 2534
25302363
MG
2535 return 0;
2536
9c26f5f8
TB
2537out_err:
2538 cleanup_root_ns(steering->root_ns);
2539 steering->root_ns = NULL;
2540 return err;
25302363
MG
2541}
2542
0da2d666 2543static void clean_tree(struct fs_node *node)
25302363 2544{
0da2d666
MG
2545 if (node) {
2546 struct fs_node *iter;
2547 struct fs_node *temp;
25302363 2548
800350a3 2549 tree_get_node(node);
0da2d666
MG
2550 list_for_each_entry_safe(iter, temp, &node->children, list)
2551 clean_tree(iter);
476d61b7
EB
2552 tree_put_node(node, false);
2553 tree_remove_node(node, false);
25302363 2554 }
153fefbf
MG
2555}
2556
0da2d666 2557static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
25302363 2558{
25302363
MG
2559 if (!root_ns)
2560 return;
2561
0da2d666 2562 clean_tree(&root_ns->ns.node);
25302363
MG
2563}
2564
9b93ab98
GP
2565static void cleanup_egress_acls_root_ns(struct mlx5_core_dev *dev)
2566{
2567 struct mlx5_flow_steering *steering = dev->priv.steering;
2568 int i;
2569
2570 if (!steering->esw_egress_root_ns)
2571 return;
2572
2752b823 2573 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++)
9b93ab98
GP
2574 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2575
2576 kfree(steering->esw_egress_root_ns);
9414277a 2577 steering->esw_egress_root_ns = NULL;
9b93ab98
GP
2578}
2579
2580static void cleanup_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2581{
2582 struct mlx5_flow_steering *steering = dev->priv.steering;
2583 int i;
2584
2585 if (!steering->esw_ingress_root_ns)
2586 return;
2587
2752b823 2588 for (i = 0; i < mlx5_eswitch_get_total_vports(dev); i++)
9b93ab98
GP
2589 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2590
2591 kfree(steering->esw_ingress_root_ns);
9414277a 2592 steering->esw_ingress_root_ns = NULL;
9b93ab98
GP
2593}
2594
25302363
MG
2595void mlx5_cleanup_fs(struct mlx5_core_dev *dev)
2596{
fba53f7b
MG
2597 struct mlx5_flow_steering *steering = dev->priv.steering;
2598
0da2d666 2599 cleanup_root_ns(steering->root_ns);
9b93ab98
GP
2600 cleanup_egress_acls_root_ns(dev);
2601 cleanup_ingress_acls_root_ns(dev);
0da2d666 2602 cleanup_root_ns(steering->fdb_root_ns);
328edb49
PB
2603 steering->fdb_root_ns = NULL;
2604 kfree(steering->fdb_sub_ns);
2605 steering->fdb_sub_ns = NULL;
87d22483
MG
2606 cleanup_root_ns(steering->sniffer_rx_root_ns);
2607 cleanup_root_ns(steering->sniffer_tx_root_ns);
d83eb50e 2608 cleanup_root_ns(steering->rdma_rx_root_ns);
24670b1a 2609 cleanup_root_ns(steering->rdma_tx_root_ns);
5f418378 2610 cleanup_root_ns(steering->egress_root_ns);
43a335e0 2611 mlx5_cleanup_fc_stats(dev);
a369d4ac
MG
2612 kmem_cache_destroy(steering->ftes_cache);
2613 kmem_cache_destroy(steering->fgs_cache);
fba53f7b 2614 kfree(steering);
25302363
MG
2615}
2616
87d22483
MG
2617static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
2618{
2619 struct fs_prio *prio;
2620
2621 steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
2622 if (!steering->sniffer_tx_root_ns)
2623 return -ENOMEM;
2624
2625 /* Create single prio */
2626 prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
905f6bd3 2627 return PTR_ERR_OR_ZERO(prio);
87d22483
MG
2628}
2629
2630static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
2631{
2632 struct fs_prio *prio;
2633
2634 steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
2635 if (!steering->sniffer_rx_root_ns)
2636 return -ENOMEM;
2637
2638 /* Create single prio */
2639 prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
905f6bd3 2640 return PTR_ERR_OR_ZERO(prio);
87d22483
MG
2641}
2642
d83eb50e
MG
2643static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
2644{
e6806e9a 2645 int err;
d83eb50e
MG
2646
2647 steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
2648 if (!steering->rdma_rx_root_ns)
2649 return -ENOMEM;
2650
e6806e9a
MZ
2651 err = init_root_tree(steering, &rdma_rx_root_fs,
2652 &steering->rdma_rx_root_ns->ns.node);
2653 if (err)
2654 goto out_err;
f6f7d6b5 2655
e6806e9a
MZ
2656 set_prio_attrs(steering->rdma_rx_root_ns);
2657
2658 return 0;
2659
2660out_err:
2661 cleanup_root_ns(steering->rdma_rx_root_ns);
2662 steering->rdma_rx_root_ns = NULL;
2663 return err;
d83eb50e 2664}
439e843f 2665
24670b1a
MG
2666static int init_rdma_tx_root_ns(struct mlx5_flow_steering *steering)
2667{
2668 int err;
2669
2670 steering->rdma_tx_root_ns = create_root_ns(steering, FS_FT_RDMA_TX);
2671 if (!steering->rdma_tx_root_ns)
2672 return -ENOMEM;
2673
2674 err = init_root_tree(steering, &rdma_tx_root_fs,
2675 &steering->rdma_tx_root_ns->ns.node);
2676 if (err)
2677 goto out_err;
2678
2679 set_prio_attrs(steering->rdma_tx_root_ns);
2680
2681 return 0;
2682
2683out_err:
2684 cleanup_root_ns(steering->rdma_tx_root_ns);
2685 steering->rdma_tx_root_ns = NULL;
2686 return err;
2687}
2688
439e843f
PB
2689/* FT and tc chains are stored in the same array so we can re-use the
2690 * mlx5_get_fdb_sub_ns() and tc api for FT chains.
2691 * When creating a new ns for each chain store it in the first available slot.
2692 * Assume tc chains are created and stored first and only then the FT chain.
2693 */
2694static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2695 struct mlx5_flow_namespace *ns)
2696{
2697 int chain = 0;
2698
2699 while (steering->fdb_sub_ns[chain])
2700 ++chain;
2701
2702 steering->fdb_sub_ns[chain] = ns;
2703}
2704
2705static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2706 struct fs_prio *maj_prio)
25302363 2707{
328edb49 2708 struct mlx5_flow_namespace *ns;
328edb49 2709 struct fs_prio *min_prio;
439e843f
PB
2710 int prio;
2711
2712 ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
2713 if (IS_ERR(ns))
2714 return PTR_ERR(ns);
2715
2716 for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
2717 min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
2718 if (IS_ERR(min_prio))
2719 return PTR_ERR(min_prio);
2720 }
2721
2722 store_fdb_sub_ns_prio_chain(steering, ns);
2723
2724 return 0;
2725}
2726
2727static int create_fdb_chains(struct mlx5_flow_steering *steering,
2728 int fs_prio,
2729 int chains)
2730{
2731 struct fs_prio *maj_prio;
328edb49
PB
2732 int levels;
2733 int chain;
328edb49 2734 int err;
25302363 2735
439e843f
PB
2736 levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
2737 maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
2738 fs_prio,
2739 levels);
2740 if (IS_ERR(maj_prio))
2741 return PTR_ERR(maj_prio);
2742
2743 for (chain = 0; chain < chains; chain++) {
2744 err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
2745 if (err)
2746 return err;
2747 }
2748
2749 return 0;
2750}
25302363 2751
439e843f
PB
2752static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
2753{
439e843f
PB
2754 int err;
2755
975b992f 2756 steering->fdb_sub_ns = kcalloc(FDB_NUM_CHAINS,
439e843f
PB
2757 sizeof(*steering->fdb_sub_ns),
2758 GFP_KERNEL);
328edb49
PB
2759 if (!steering->fdb_sub_ns)
2760 return -ENOMEM;
2761
975b992f
PB
2762 err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
2763 if (err)
2764 return err;
2765
2766 err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
439e843f
PB
2767 if (err)
2768 return err;
2769
2770 return 0;
2771}
2772
2773static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
2774{
2775 struct fs_prio *maj_prio;
2776 int err;
2777
2778 steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
2779 if (!steering->fdb_root_ns)
2780 return -ENOMEM;
2781
d9cb0675
MB
2782 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH,
2783 1);
2784 if (IS_ERR(maj_prio)) {
2785 err = PTR_ERR(maj_prio);
2786 goto out_err;
2787 }
439e843f
PB
2788 err = create_fdb_fast_path(steering);
2789 if (err)
1033665e 2790 goto out_err;
328edb49 2791
b6d9ccb1 2792 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
328edb49
PB
2793 if (IS_ERR(maj_prio)) {
2794 err = PTR_ERR(maj_prio);
1033665e 2795 goto out_err;
328edb49 2796 }
1033665e 2797
96e32687
EC
2798 /* We put this priority last, knowing that nothing will get here
2799 * unless explicitly forwarded to. This is possible because the
2800 * slow path tables have catch all rules and nothing gets passed
2801 * those tables.
2802 */
2803 maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_PER_VPORT, 1);
2804 if (IS_ERR(maj_prio)) {
2805 err = PTR_ERR(maj_prio);
2806 goto out_err;
2807 }
2808
1033665e
OG
2809 set_prio_attrs(steering->fdb_root_ns);
2810 return 0;
2811
2812out_err:
2813 cleanup_root_ns(steering->fdb_root_ns);
328edb49
PB
2814 kfree(steering->fdb_sub_ns);
2815 steering->fdb_sub_ns = NULL;
1033665e 2816 steering->fdb_root_ns = NULL;
328edb49 2817 return err;
25302363
MG
2818}
2819
9b93ab98 2820static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
efdc810b
MHY
2821{
2822 struct fs_prio *prio;
2823
9b93ab98
GP
2824 steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
2825 if (!steering->esw_egress_root_ns[vport])
efdc810b
MHY
2826 return -ENOMEM;
2827
2828 /* create 1 prio*/
9b93ab98 2829 prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
44fafdaa 2830 return PTR_ERR_OR_ZERO(prio);
efdc810b
MHY
2831}
2832
9b93ab98 2833static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
efdc810b
MHY
2834{
2835 struct fs_prio *prio;
2836
9b93ab98
GP
2837 steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
2838 if (!steering->esw_ingress_root_ns[vport])
efdc810b
MHY
2839 return -ENOMEM;
2840
2841 /* create 1 prio*/
9b93ab98 2842 prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
44fafdaa 2843 return PTR_ERR_OR_ZERO(prio);
efdc810b
MHY
2844}
2845
9b93ab98
GP
2846static int init_egress_acls_root_ns(struct mlx5_core_dev *dev)
2847{
2848 struct mlx5_flow_steering *steering = dev->priv.steering;
2752b823 2849 int total_vports = mlx5_eswitch_get_total_vports(dev);
9b93ab98
GP
2850 int err;
2851 int i;
2852
2752b823
PP
2853 steering->esw_egress_root_ns =
2854 kcalloc(total_vports,
2855 sizeof(*steering->esw_egress_root_ns),
2856 GFP_KERNEL);
9b93ab98
GP
2857 if (!steering->esw_egress_root_ns)
2858 return -ENOMEM;
2859
2752b823 2860 for (i = 0; i < total_vports; i++) {
9b93ab98
GP
2861 err = init_egress_acl_root_ns(steering, i);
2862 if (err)
2863 goto cleanup_root_ns;
2864 }
2865
2866 return 0;
2867
2868cleanup_root_ns:
2869 for (i--; i >= 0; i--)
2870 cleanup_root_ns(steering->esw_egress_root_ns[i]);
2871 kfree(steering->esw_egress_root_ns);
9414277a 2872 steering->esw_egress_root_ns = NULL;
9b93ab98
GP
2873 return err;
2874}
2875
2876static int init_ingress_acls_root_ns(struct mlx5_core_dev *dev)
2877{
2878 struct mlx5_flow_steering *steering = dev->priv.steering;
2752b823 2879 int total_vports = mlx5_eswitch_get_total_vports(dev);
9b93ab98
GP
2880 int err;
2881 int i;
2882
2752b823
PP
2883 steering->esw_ingress_root_ns =
2884 kcalloc(total_vports,
2885 sizeof(*steering->esw_ingress_root_ns),
2886 GFP_KERNEL);
9b93ab98
GP
2887 if (!steering->esw_ingress_root_ns)
2888 return -ENOMEM;
2889
2752b823 2890 for (i = 0; i < total_vports; i++) {
9b93ab98
GP
2891 err = init_ingress_acl_root_ns(steering, i);
2892 if (err)
2893 goto cleanup_root_ns;
2894 }
2895
2896 return 0;
2897
2898cleanup_root_ns:
2899 for (i--; i >= 0; i--)
2900 cleanup_root_ns(steering->esw_ingress_root_ns[i]);
2901 kfree(steering->esw_ingress_root_ns);
9414277a 2902 steering->esw_ingress_root_ns = NULL;
9b93ab98
GP
2903 return err;
2904}
2905
5f418378
AY
2906static int init_egress_root_ns(struct mlx5_flow_steering *steering)
2907{
8ce78257 2908 int err;
5f418378
AY
2909
2910 steering->egress_root_ns = create_root_ns(steering,
2911 FS_FT_NIC_TX);
2912 if (!steering->egress_root_ns)
2913 return -ENOMEM;
2914
8ce78257
MB
2915 err = init_root_tree(steering, &egress_root_fs,
2916 &steering->egress_root_ns->ns.node);
2917 if (err)
2918 goto cleanup;
2919 set_prio_attrs(steering->egress_root_ns);
2920 return 0;
2921cleanup:
2922 cleanup_root_ns(steering->egress_root_ns);
2923 steering->egress_root_ns = NULL;
2924 return err;
5f418378
AY
2925}
2926
25302363
MG
2927int mlx5_init_fs(struct mlx5_core_dev *dev)
2928{
fba53f7b 2929 struct mlx5_flow_steering *steering;
25302363
MG
2930 int err = 0;
2931
43a335e0
AV
2932 err = mlx5_init_fc_stats(dev);
2933 if (err)
2934 return err;
2935
fba53f7b
MG
2936 steering = kzalloc(sizeof(*steering), GFP_KERNEL);
2937 if (!steering)
2938 return -ENOMEM;
2939 steering->dev = dev;
2940 dev->priv.steering = steering;
2941
a369d4ac
MG
2942 steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
2943 sizeof(struct mlx5_flow_group), 0,
2944 0, NULL);
2945 steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
2946 0, NULL);
2947 if (!steering->ftes_cache || !steering->fgs_cache) {
2948 err = -ENOMEM;
2949 goto err;
2950 }
2951
ffdb8827
ES
2952 if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
2953 (MLX5_CAP_GEN(dev, nic_flow_table))) ||
2954 ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
2955 MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
876d634d 2956 MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
fba53f7b 2957 err = init_root_ns(steering);
25302363 2958 if (err)
43a335e0 2959 goto err;
25302363 2960 }
876d634d 2961
0efc8562 2962 if (MLX5_ESWITCH_MANAGER(dev)) {
bd02ef8e 2963 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
fba53f7b 2964 err = init_fdb_root_ns(steering);
bd02ef8e
MG
2965 if (err)
2966 goto err;
2967 }
2968 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
9b93ab98 2969 err = init_egress_acls_root_ns(dev);
bd02ef8e
MG
2970 if (err)
2971 goto err;
2972 }
2973 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
9b93ab98 2974 err = init_ingress_acls_root_ns(dev);
bd02ef8e
MG
2975 if (err)
2976 goto err;
2977 }
25302363
MG
2978 }
2979
87d22483
MG
2980 if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
2981 err = init_sniffer_rx_root_ns(steering);
2982 if (err)
2983 goto err;
2984 }
2985
2986 if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
2987 err = init_sniffer_tx_root_ns(steering);
2988 if (err)
2989 goto err;
2990 }
2991
f6f7d6b5
MG
2992 if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) &&
2993 MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)) {
d83eb50e
MG
2994 err = init_rdma_rx_root_ns(steering);
2995 if (err)
2996 goto err;
2997 }
2998
24670b1a
MG
2999 if (MLX5_CAP_FLOWTABLE_RDMA_TX(dev, ft_support)) {
3000 err = init_rdma_tx_root_ns(steering);
3001 if (err)
3002 goto err;
3003 }
3004
8c8eea07
RS
3005 if (mlx5_fpga_ipsec_device_caps(steering->dev) & MLX5_ACCEL_IPSEC_CAP_DEVICE ||
3006 MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
5f418378
AY
3007 err = init_egress_root_ns(steering);
3008 if (err)
3009 goto err;
3010 }
3011
efdc810b
MHY
3012 return 0;
3013err:
3014 mlx5_cleanup_fs(dev);
25302363
MG
3015 return err;
3016}
50854114
YH
3017
3018int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3019{
3020 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
dae37456
AV
3021 struct mlx5_ft_underlay_qp *new_uqp;
3022 int err = 0;
3023
3024 new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
3025 if (!new_uqp)
3026 return -ENOMEM;
3027
3028 mutex_lock(&root->chain_lock);
3029
3030 if (!root->root_ft) {
3031 err = -EINVAL;
3032 goto update_ft_fail;
3033 }
3034
ae288a48 3035 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
af76c501 3036 false);
dae37456
AV
3037 if (err) {
3038 mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
3039 underlay_qpn, err);
3040 goto update_ft_fail;
3041 }
3042
3043 new_uqp->qpn = underlay_qpn;
3044 list_add_tail(&new_uqp->list, &root->underlay_qpns);
3045
3046 mutex_unlock(&root->chain_lock);
50854114 3047
50854114 3048 return 0;
dae37456
AV
3049
3050update_ft_fail:
3051 mutex_unlock(&root->chain_lock);
3052 kfree(new_uqp);
3053 return err;
50854114
YH
3054}
3055EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
3056
3057int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
3058{
3059 struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
dae37456
AV
3060 struct mlx5_ft_underlay_qp *uqp;
3061 bool found = false;
3062 int err = 0;
3063
3064 mutex_lock(&root->chain_lock);
3065 list_for_each_entry(uqp, &root->underlay_qpns, list) {
3066 if (uqp->qpn == underlay_qpn) {
3067 found = true;
3068 break;
3069 }
3070 }
3071
3072 if (!found) {
3073 mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
3074 underlay_qpn);
3075 err = -EINVAL;
3076 goto out;
3077 }
3078
ae288a48 3079 err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
af76c501 3080 true);
dae37456
AV
3081 if (err)
3082 mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
3083 underlay_qpn, err);
3084
3085 list_del(&uqp->list);
3086 mutex_unlock(&root->chain_lock);
3087 kfree(uqp);
50854114 3088
50854114 3089 return 0;
dae37456
AV
3090
3091out:
3092 mutex_unlock(&root->chain_lock);
3093 return err;
50854114
YH
3094}
3095EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
2b688ea5
MG
3096
3097static struct mlx5_flow_root_namespace
3098*get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
3099{
3100 struct mlx5_flow_namespace *ns;
3101
3102 if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
3103 ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS)
3104 ns = mlx5_get_flow_vport_acl_namespace(dev, ns_type, 0);
3105 else
3106 ns = mlx5_get_flow_namespace(dev, ns_type);
3107 if (!ns)
3108 return NULL;
3109
3110 return find_root(&ns->node);
3111}
3112
3113struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
3114 u8 ns_type, u8 num_actions,
3115 void *modify_actions)
3116{
3117 struct mlx5_flow_root_namespace *root;
3118 struct mlx5_modify_hdr *modify_hdr;
3119 int err;
3120
3121 root = get_root_namespace(dev, ns_type);
3122 if (!root)
3123 return ERR_PTR(-EOPNOTSUPP);
3124
3125 modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL);
3126 if (!modify_hdr)
3127 return ERR_PTR(-ENOMEM);
3128
3129 modify_hdr->ns_type = ns_type;
3130 err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
3131 modify_actions, modify_hdr);
3132 if (err) {
3133 kfree(modify_hdr);
3134 return ERR_PTR(err);
3135 }
3136
3137 return modify_hdr;
3138}
3139EXPORT_SYMBOL(mlx5_modify_header_alloc);
3140
3141void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
3142 struct mlx5_modify_hdr *modify_hdr)
3143{
3144 struct mlx5_flow_root_namespace *root;
3145
3146 root = get_root_namespace(dev, modify_hdr->ns_type);
3147 if (WARN_ON(!root))
3148 return;
3149 root->cmds->modify_header_dealloc(root, modify_hdr);
3150 kfree(modify_hdr);
3151}
3152EXPORT_SYMBOL(mlx5_modify_header_dealloc);
3153
3154struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
3155 int reformat_type,
3156 size_t size,
3157 void *reformat_data,
3158 enum mlx5_flow_namespace_type ns_type)
3159{
3160 struct mlx5_pkt_reformat *pkt_reformat;
3161 struct mlx5_flow_root_namespace *root;
3162 int err;
3163
3164 root = get_root_namespace(dev, ns_type);
3165 if (!root)
3166 return ERR_PTR(-EOPNOTSUPP);
3167
3168 pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL);
3169 if (!pkt_reformat)
3170 return ERR_PTR(-ENOMEM);
3171
3172 pkt_reformat->ns_type = ns_type;
3173 pkt_reformat->reformat_type = reformat_type;
3174 err = root->cmds->packet_reformat_alloc(root, reformat_type, size,
3175 reformat_data, ns_type,
3176 pkt_reformat);
3177 if (err) {
3178 kfree(pkt_reformat);
3179 return ERR_PTR(err);
3180 }
3181
3182 return pkt_reformat;
3183}
3184EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
3185
3186void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
3187 struct mlx5_pkt_reformat *pkt_reformat)
3188{
3189 struct mlx5_flow_root_namespace *root;
3190
3191 root = get_root_namespace(dev, pkt_reformat->ns_type);
3192 if (WARN_ON(!root))
3193 return;
3194 root->cmds->packet_reformat_dealloc(root, pkt_reformat);
3195 kfree(pkt_reformat);
3196}
3197EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
6a48faee
MG
3198
3199int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
3200 struct mlx5_flow_root_namespace *peer_ns)
3201{
38b9d1c6
MG
3202 if (peer_ns && ns->mode != peer_ns->mode) {
3203 mlx5_core_err(ns->dev,
3204 "Can't peer namespace of different steering mode\n");
3205 return -EINVAL;
3206 }
3207
6a48faee
MG
3208 return ns->cmds->set_peer(ns, peer_ns);
3209}
38b9d1c6
MG
3210
3211/* This function should be called only at init stage of the namespace.
3212 * It is not safe to call this function while steering operations
3213 * are executed in the namespace.
3214 */
3215int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
3216 enum mlx5_flow_steering_mode mode)
3217{
3218 struct mlx5_flow_root_namespace *root;
3219 const struct mlx5_flow_cmds *cmds;
3220 int err;
3221
3222 root = find_root(&ns->node);
3223 if (&root->ns != ns)
3224 /* Can't set cmds to non root namespace */
3225 return -EINVAL;
3226
3227 if (root->table_type != FS_FT_FDB)
3228 return -EOPNOTSUPP;
3229
3230 if (root->mode == mode)
3231 return 0;
3232
3233 if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
3234 cmds = mlx5_fs_cmd_get_dr_cmds();
3235 else
3236 cmds = mlx5_fs_cmd_get_fw_cmds();
3237 if (!cmds)
3238 return -EOPNOTSUPP;
3239
3240 err = cmds->create_ns(root);
3241 if (err) {
3242 mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
3243 err);
3244 return err;
3245 }
3246
3247 root->cmds->destroy_ns(root);
3248 root->cmds = cmds;
3249 root->mode = mode;
3250
3251 return 0;
3252}