1de8575e0SMaor Gottlieb /*
2de8575e0SMaor Gottlieb * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3de8575e0SMaor Gottlieb *
4de8575e0SMaor Gottlieb * This software is available to you under a choice of one of two
5de8575e0SMaor Gottlieb * licenses. You may choose to be licensed under the terms of the GNU
6de8575e0SMaor Gottlieb * General Public License (GPL) Version 2, available from the file
7de8575e0SMaor Gottlieb * COPYING in the main directory of this source tree, or the
8de8575e0SMaor Gottlieb * OpenIB.org BSD license below:
9de8575e0SMaor Gottlieb *
10de8575e0SMaor Gottlieb * Redistribution and use in source and binary forms, with or
11de8575e0SMaor Gottlieb * without modification, are permitted provided that the following
12de8575e0SMaor Gottlieb * conditions are met:
13de8575e0SMaor Gottlieb *
14de8575e0SMaor Gottlieb * - Redistributions of source code must retain the above
15de8575e0SMaor Gottlieb * copyright notice, this list of conditions and the following
16de8575e0SMaor Gottlieb * disclaimer.
17de8575e0SMaor Gottlieb *
18de8575e0SMaor Gottlieb * - Redistributions in binary form must reproduce the above
19de8575e0SMaor Gottlieb * copyright notice, this list of conditions and the following
20de8575e0SMaor Gottlieb * disclaimer in the documentation and/or other materials
21de8575e0SMaor Gottlieb * provided with the distribution.
22de8575e0SMaor Gottlieb *
23de8575e0SMaor Gottlieb * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24de8575e0SMaor Gottlieb * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25de8575e0SMaor Gottlieb * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26de8575e0SMaor Gottlieb * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27de8575e0SMaor Gottlieb * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28de8575e0SMaor Gottlieb * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29de8575e0SMaor Gottlieb * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30de8575e0SMaor Gottlieb * SOFTWARE.
31de8575e0SMaor Gottlieb */
32de8575e0SMaor Gottlieb
33de8575e0SMaor Gottlieb #include <linux/mutex.h>
34de8575e0SMaor Gottlieb #include <linux/mlx5/driver.h>
35bf3e4d38SBodong Wang #include <linux/mlx5/vport.h>
360efc8562SOr Gerlitz #include <linux/mlx5/eswitch.h>
37db492c1eSJiri Pirko #include <net/devlink.h>
38de8575e0SMaor Gottlieb
39de8575e0SMaor Gottlieb #include "mlx5_core.h"
40de8575e0SMaor Gottlieb #include "fs_core.h"
410c56b975SMaor Gottlieb #include "fs_cmd.h"
424a98544dSPaul Blakey #include "fs_ft_pool.h"
434c03e69aSMatan Barak #include "diag/fs_tracepoint.h"
44db492c1eSJiri Pirko #include "devlink.h"
450c56b975SMaor Gottlieb
4625302363SMaor Gottlieb #define INIT_TREE_NODE_ARRAY_SIZE(...) (sizeof((struct init_tree_node[]){__VA_ARGS__}) /\
4725302363SMaor Gottlieb sizeof(struct init_tree_node))
4825302363SMaor Gottlieb
49a257b94aSMaor Gottlieb #define ADD_PRIO(num_prios_val, min_level_val, num_levels_val, caps_val,\
50655227edSMaor Gottlieb ...) {.type = FS_TYPE_PRIO,\
5125302363SMaor Gottlieb .min_ft_level = min_level_val,\
52a257b94aSMaor Gottlieb .num_levels = num_levels_val,\
534cbdd30eSMaor Gottlieb .num_leaf_prios = num_prios_val,\
548d40d162SMaor Gottlieb .caps = caps_val,\
5525302363SMaor Gottlieb .children = (struct init_tree_node[]) {__VA_ARGS__},\
5625302363SMaor Gottlieb .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
5725302363SMaor Gottlieb }
5825302363SMaor Gottlieb
59a257b94aSMaor Gottlieb #define ADD_MULTIPLE_PRIO(num_prios_val, num_levels_val, ...)\
60a257b94aSMaor Gottlieb ADD_PRIO(num_prios_val, 0, num_levels_val, {},\
6125302363SMaor Gottlieb __VA_ARGS__)\
6225302363SMaor Gottlieb
63f66ad830SMark Zhang #define ADD_NS(def_miss_act, ...) {.type = FS_TYPE_NAMESPACE, \
64f66ad830SMark Zhang .def_miss_action = def_miss_act,\
6525302363SMaor Gottlieb .children = (struct init_tree_node[]) {__VA_ARGS__},\
6625302363SMaor Gottlieb .ar_size = INIT_TREE_NODE_ARRAY_SIZE(__VA_ARGS__) \
6725302363SMaor Gottlieb }
6825302363SMaor Gottlieb
698d40d162SMaor Gottlieb #define INIT_CAPS_ARRAY_SIZE(...) (sizeof((long[]){__VA_ARGS__}) /\
708d40d162SMaor Gottlieb sizeof(long))
718d40d162SMaor Gottlieb
728d40d162SMaor Gottlieb #define FS_CAP(cap) (__mlx5_bit_off(flow_table_nic_cap, cap))
738d40d162SMaor Gottlieb
748d40d162SMaor Gottlieb #define FS_REQUIRED_CAPS(...) {.arr_sz = INIT_CAPS_ARRAY_SIZE(__VA_ARGS__), \
758d40d162SMaor Gottlieb .caps = (long[]) {__VA_ARGS__} }
768d40d162SMaor Gottlieb
776dc6071cSMaor Gottlieb #define FS_CHAINING_CAPS FS_REQUIRED_CAPS(FS_CAP(flow_table_properties_nic_receive.flow_modify_en), \
786dc6071cSMaor Gottlieb FS_CAP(flow_table_properties_nic_receive.modify_root), \
796dc6071cSMaor Gottlieb FS_CAP(flow_table_properties_nic_receive.identified_miss_table_mode), \
806dc6071cSMaor Gottlieb FS_CAP(flow_table_properties_nic_receive.flow_table_modify))
816dc6071cSMaor Gottlieb
828ce78257SMark Bloch #define FS_CHAINING_CAPS_EGRESS \
838ce78257SMark Bloch FS_REQUIRED_CAPS( \
848ce78257SMark Bloch FS_CAP(flow_table_properties_nic_transmit.flow_modify_en), \
858ce78257SMark Bloch FS_CAP(flow_table_properties_nic_transmit.modify_root), \
868ce78257SMark Bloch FS_CAP(flow_table_properties_nic_transmit \
878ce78257SMark Bloch .identified_miss_table_mode), \
888ce78257SMark Bloch FS_CAP(flow_table_properties_nic_transmit.flow_table_modify))
898ce78257SMark Bloch
9024670b1aSMichael Guralnik #define FS_CHAINING_CAPS_RDMA_TX \
9124670b1aSMichael Guralnik FS_REQUIRED_CAPS( \
9224670b1aSMichael Guralnik FS_CAP(flow_table_properties_nic_transmit_rdma.flow_modify_en), \
9324670b1aSMichael Guralnik FS_CAP(flow_table_properties_nic_transmit_rdma.modify_root), \
9424670b1aSMichael Guralnik FS_CAP(flow_table_properties_nic_transmit_rdma \
9524670b1aSMichael Guralnik .identified_miss_table_mode), \
9624670b1aSMichael Guralnik FS_CAP(flow_table_properties_nic_transmit_rdma \
9724670b1aSMichael Guralnik .flow_table_modify))
9824670b1aSMichael Guralnik
99a257b94aSMaor Gottlieb #define LEFTOVERS_NUM_LEVELS 1
1004cbdd30eSMaor Gottlieb #define LEFTOVERS_NUM_PRIOS 1
1014cbdd30eSMaor Gottlieb
102b8dfed63SAharon Landau #define RDMA_RX_COUNTERS_PRIO_NUM_LEVELS 1
103b8dfed63SAharon Landau #define RDMA_TX_COUNTERS_PRIO_NUM_LEVELS 1
104b8dfed63SAharon Landau
105a257b94aSMaor Gottlieb #define BY_PASS_PRIO_NUM_LEVELS 1
1066dc6071cSMaor Gottlieb #define BY_PASS_MIN_LEVEL (ETHTOOL_MIN_LEVEL + MLX5_BY_PASS_NUM_PRIOS +\
107a257b94aSMaor Gottlieb LEFTOVERS_NUM_PRIOS)
108a257b94aSMaor Gottlieb
10915d187e2SLior Nahmanson #define KERNEL_RX_MACSEC_NUM_PRIOS 1
1108c14a2c7SPatrisious Haddad #define KERNEL_RX_MACSEC_NUM_LEVELS 3
11115d187e2SLior Nahmanson #define KERNEL_RX_MACSEC_MIN_LEVEL (BY_PASS_MIN_LEVEL + KERNEL_RX_MACSEC_NUM_PRIOS)
11215d187e2SLior Nahmanson
1136dc6071cSMaor Gottlieb #define ETHTOOL_PRIO_NUM_LEVELS 1
114e5835f28SMaor Gottlieb #define ETHTOOL_NUM_PRIOS 11
1156dc6071cSMaor Gottlieb #define ETHTOOL_MIN_LEVEL (KERNEL_MIN_LEVEL + ETHTOOL_NUM_PRIOS)
11689957760SMark Zhang /* Promiscuous, Vlan, mac, ttc, inner ttc, {UDP/ANY/aRFS/accel/{esp, esp_err}}, IPsec policy,
11789957760SMark Zhang * IPsec RoCE policy
11889957760SMark Zhang */
11989957760SMark Zhang #define KERNEL_NIC_PRIO_NUM_LEVELS 9
12013de6c10SMaor Gottlieb #define KERNEL_NIC_NUM_PRIOS 1
12113de6c10SMaor Gottlieb /* One more level for tc */
12213de6c10SMaor Gottlieb #define KERNEL_MIN_LEVEL (KERNEL_NIC_PRIO_NUM_LEVELS + 1)
1238d40d162SMaor Gottlieb
124479f074cSOr Gerlitz #define KERNEL_NIC_TC_NUM_PRIOS 1
12566cb64e2SMaor Dickman #define KERNEL_NIC_TC_NUM_LEVELS 3
126479f074cSOr Gerlitz
127a257b94aSMaor Gottlieb #define ANCHOR_NUM_LEVELS 1
128153fefbfSMaor Gottlieb #define ANCHOR_NUM_PRIOS 1
129153fefbfSMaor Gottlieb #define ANCHOR_MIN_LEVEL (BY_PASS_MIN_LEVEL + 1)
130acbc2004SOr Gerlitz
13120f7b37fSSaeed Mahameed #define OFFLOADS_MAX_FT 2
13211b717d6SPaul Blakey #define OFFLOADS_NUM_PRIOS 2
13311b717d6SPaul Blakey #define OFFLOADS_MIN_LEVEL (ANCHOR_MIN_LEVEL + OFFLOADS_NUM_PRIOS)
134acbc2004SOr Gerlitz
1353e75d4ebSAviv Heller #define LAG_PRIO_NUM_LEVELS 1
1363e75d4ebSAviv Heller #define LAG_NUM_PRIOS 1
13715d187e2SLior Nahmanson #define LAG_MIN_LEVEL (OFFLOADS_MIN_LEVEL + KERNEL_RX_MACSEC_MIN_LEVEL + 1)
1383e75d4ebSAviv Heller
139ee92e4f1SHuy Nguyen #define KERNEL_TX_IPSEC_NUM_PRIOS 1
140d0c19a31SRaed Salem #define KERNEL_TX_IPSEC_NUM_LEVELS 3
141ee534d7fSLior Nahmanson #define KERNEL_TX_IPSEC_MIN_LEVEL (KERNEL_TX_IPSEC_NUM_LEVELS)
142ee534d7fSLior Nahmanson
143ee534d7fSLior Nahmanson #define KERNEL_TX_MACSEC_NUM_PRIOS 1
144ee534d7fSLior Nahmanson #define KERNEL_TX_MACSEC_NUM_LEVELS 2
145ee534d7fSLior Nahmanson #define KERNEL_TX_MACSEC_MIN_LEVEL (KERNEL_TX_IPSEC_MIN_LEVEL + KERNEL_TX_MACSEC_NUM_PRIOS)
146ee92e4f1SHuy Nguyen
1478d40d162SMaor Gottlieb struct node_caps {
1488d40d162SMaor Gottlieb size_t arr_sz;
1498d40d162SMaor Gottlieb long *caps;
1508d40d162SMaor Gottlieb };
1518963ca45SOr Gerlitz
15225302363SMaor Gottlieb static struct init_tree_node {
15325302363SMaor Gottlieb enum fs_node_type type;
15425302363SMaor Gottlieb struct init_tree_node *children;
15525302363SMaor Gottlieb int ar_size;
1568d40d162SMaor Gottlieb struct node_caps caps;
15725302363SMaor Gottlieb int min_ft_level;
1584cbdd30eSMaor Gottlieb int num_leaf_prios;
15925302363SMaor Gottlieb int prio;
160a257b94aSMaor Gottlieb int num_levels;
161f66ad830SMark Zhang enum mlx5_flow_table_miss_action def_miss_action;
16225302363SMaor Gottlieb } root_fs = {
16325302363SMaor Gottlieb .type = FS_TYPE_NAMESPACE,
16415d187e2SLior Nahmanson .ar_size = 8,
16525302363SMaor Gottlieb .children = (struct init_tree_node[]){
166f66ad830SMark Zhang ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
167f66ad830SMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
168f66ad830SMark Zhang ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
169a257b94aSMaor Gottlieb BY_PASS_PRIO_NUM_LEVELS))),
17015d187e2SLior Nahmanson ADD_PRIO(0, KERNEL_RX_MACSEC_MIN_LEVEL, 0, FS_CHAINING_CAPS,
17115d187e2SLior Nahmanson ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
17215d187e2SLior Nahmanson ADD_MULTIPLE_PRIO(KERNEL_RX_MACSEC_NUM_PRIOS,
17315d187e2SLior Nahmanson KERNEL_RX_MACSEC_NUM_LEVELS))),
174f66ad830SMark Zhang ADD_PRIO(0, LAG_MIN_LEVEL, 0, FS_CHAINING_CAPS,
175f66ad830SMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
176f66ad830SMark Zhang ADD_MULTIPLE_PRIO(LAG_NUM_PRIOS,
1773e75d4ebSAviv Heller LAG_PRIO_NUM_LEVELS))),
17820f7b37fSSaeed Mahameed ADD_PRIO(0, OFFLOADS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
179f66ad830SMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
180f66ad830SMark Zhang ADD_MULTIPLE_PRIO(OFFLOADS_NUM_PRIOS,
181f66ad830SMark Zhang OFFLOADS_MAX_FT))),
182f66ad830SMark Zhang ADD_PRIO(0, ETHTOOL_MIN_LEVEL, 0, FS_CHAINING_CAPS,
183f66ad830SMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
184f66ad830SMark Zhang ADD_MULTIPLE_PRIO(ETHTOOL_NUM_PRIOS,
1856dc6071cSMaor Gottlieb ETHTOOL_PRIO_NUM_LEVELS))),
186a257b94aSMaor Gottlieb ADD_PRIO(0, KERNEL_MIN_LEVEL, 0, {},
187f66ad830SMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
188f66ad830SMark Zhang ADD_MULTIPLE_PRIO(KERNEL_NIC_TC_NUM_PRIOS,
189f66ad830SMark Zhang KERNEL_NIC_TC_NUM_LEVELS),
19013de6c10SMaor Gottlieb ADD_MULTIPLE_PRIO(KERNEL_NIC_NUM_PRIOS,
19113de6c10SMaor Gottlieb KERNEL_NIC_PRIO_NUM_LEVELS))),
192f66ad830SMark Zhang ADD_PRIO(0, BY_PASS_MIN_LEVEL, 0, FS_CHAINING_CAPS,
193f66ad830SMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
194f66ad830SMark Zhang ADD_MULTIPLE_PRIO(LEFTOVERS_NUM_PRIOS,
195f66ad830SMark Zhang LEFTOVERS_NUM_LEVELS))),
196153fefbfSMaor Gottlieb ADD_PRIO(0, ANCHOR_MIN_LEVEL, 0, {},
197f66ad830SMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
198f66ad830SMark Zhang ADD_MULTIPLE_PRIO(ANCHOR_NUM_PRIOS,
199f66ad830SMark Zhang ANCHOR_NUM_LEVELS))),
20025302363SMaor Gottlieb }
20125302363SMaor Gottlieb };
20225302363SMaor Gottlieb
2038ce78257SMark Bloch static struct init_tree_node egress_root_fs = {
2048ce78257SMark Bloch .type = FS_TYPE_NAMESPACE,
205ee534d7fSLior Nahmanson .ar_size = 3,
2068ce78257SMark Bloch .children = (struct init_tree_node[]) {
2078ce78257SMark Bloch ADD_PRIO(0, MLX5_BY_PASS_NUM_PRIOS, 0,
2088ce78257SMark Bloch FS_CHAINING_CAPS_EGRESS,
209f66ad830SMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
210f66ad830SMark Zhang ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_PRIOS,
2118ce78257SMark Bloch BY_PASS_PRIO_NUM_LEVELS))),
212ee534d7fSLior Nahmanson ADD_PRIO(0, KERNEL_TX_IPSEC_MIN_LEVEL, 0,
213ee92e4f1SHuy Nguyen FS_CHAINING_CAPS_EGRESS,
214ee92e4f1SHuy Nguyen ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
215ee92e4f1SHuy Nguyen ADD_MULTIPLE_PRIO(KERNEL_TX_IPSEC_NUM_PRIOS,
216ee92e4f1SHuy Nguyen KERNEL_TX_IPSEC_NUM_LEVELS))),
217ee534d7fSLior Nahmanson ADD_PRIO(0, KERNEL_TX_MACSEC_MIN_LEVEL, 0,
218ee534d7fSLior Nahmanson FS_CHAINING_CAPS_EGRESS,
219ee534d7fSLior Nahmanson ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
220ee534d7fSLior Nahmanson ADD_MULTIPLE_PRIO(KERNEL_TX_MACSEC_NUM_PRIOS,
221ee534d7fSLior Nahmanson KERNEL_TX_MACSEC_NUM_LEVELS))),
2228ce78257SMark Bloch }
2238ce78257SMark Bloch };
2248ce78257SMark Bloch
225b8dfed63SAharon Landau enum {
226f91ddd3aSMark Zhang RDMA_RX_IPSEC_PRIO,
227afcb21d5SPatrisious Haddad RDMA_RX_MACSEC_PRIO,
228b8dfed63SAharon Landau RDMA_RX_COUNTERS_PRIO,
229b8dfed63SAharon Landau RDMA_RX_BYPASS_PRIO,
230b8dfed63SAharon Landau RDMA_RX_KERNEL_PRIO,
231b8dfed63SAharon Landau };
232b8dfed63SAharon Landau
233f91ddd3aSMark Zhang #define RDMA_RX_IPSEC_NUM_PRIOS 1
234f91ddd3aSMark Zhang #define RDMA_RX_IPSEC_NUM_LEVELS 2
235f91ddd3aSMark Zhang #define RDMA_RX_IPSEC_MIN_LEVEL (RDMA_RX_IPSEC_NUM_LEVELS)
236f91ddd3aSMark Zhang
237b8dfed63SAharon Landau #define RDMA_RX_BYPASS_MIN_LEVEL MLX5_BY_PASS_NUM_REGULAR_PRIOS
238b8dfed63SAharon Landau #define RDMA_RX_KERNEL_MIN_LEVEL (RDMA_RX_BYPASS_MIN_LEVEL + 1)
239b8dfed63SAharon Landau #define RDMA_RX_COUNTERS_MIN_LEVEL (RDMA_RX_KERNEL_MIN_LEVEL + 2)
240b8dfed63SAharon Landau
241afcb21d5SPatrisious Haddad #define RDMA_RX_MACSEC_NUM_PRIOS 1
242afcb21d5SPatrisious Haddad #define RDMA_RX_MACSEC_PRIO_NUM_LEVELS 2
243afcb21d5SPatrisious Haddad #define RDMA_RX_MACSEC_MIN_LEVEL (RDMA_RX_COUNTERS_MIN_LEVEL + RDMA_RX_MACSEC_NUM_PRIOS)
244afcb21d5SPatrisious Haddad
245e6806e9aSMark Zhang static struct init_tree_node rdma_rx_root_fs = {
246e6806e9aSMark Zhang .type = FS_TYPE_NAMESPACE,
247afcb21d5SPatrisious Haddad .ar_size = 5,
248e6806e9aSMark Zhang .children = (struct init_tree_node[]) {
249f91ddd3aSMark Zhang [RDMA_RX_IPSEC_PRIO] =
250f91ddd3aSMark Zhang ADD_PRIO(0, RDMA_RX_IPSEC_MIN_LEVEL, 0,
251f91ddd3aSMark Zhang FS_CHAINING_CAPS,
252f91ddd3aSMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
253f91ddd3aSMark Zhang ADD_MULTIPLE_PRIO(RDMA_RX_IPSEC_NUM_PRIOS,
254f91ddd3aSMark Zhang RDMA_RX_IPSEC_NUM_LEVELS))),
255afcb21d5SPatrisious Haddad [RDMA_RX_MACSEC_PRIO] =
256afcb21d5SPatrisious Haddad ADD_PRIO(0, RDMA_RX_MACSEC_MIN_LEVEL, 0,
257afcb21d5SPatrisious Haddad FS_CHAINING_CAPS,
258afcb21d5SPatrisious Haddad ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
259afcb21d5SPatrisious Haddad ADD_MULTIPLE_PRIO(RDMA_RX_MACSEC_NUM_PRIOS,
260afcb21d5SPatrisious Haddad RDMA_RX_MACSEC_PRIO_NUM_LEVELS))),
261b8dfed63SAharon Landau [RDMA_RX_COUNTERS_PRIO] =
262b8dfed63SAharon Landau ADD_PRIO(0, RDMA_RX_COUNTERS_MIN_LEVEL, 0,
263b8dfed63SAharon Landau FS_CHAINING_CAPS,
264b8dfed63SAharon Landau ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
265b8dfed63SAharon Landau ADD_MULTIPLE_PRIO(MLX5_RDMA_RX_NUM_COUNTERS_PRIOS,
266b8dfed63SAharon Landau RDMA_RX_COUNTERS_PRIO_NUM_LEVELS))),
267e6806e9aSMark Zhang [RDMA_RX_BYPASS_PRIO] =
268b8dfed63SAharon Landau ADD_PRIO(0, RDMA_RX_BYPASS_MIN_LEVEL, 0,
269e6806e9aSMark Zhang FS_CHAINING_CAPS,
270e6806e9aSMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
271e6806e9aSMark Zhang ADD_MULTIPLE_PRIO(MLX5_BY_PASS_NUM_REGULAR_PRIOS,
272e6806e9aSMark Zhang BY_PASS_PRIO_NUM_LEVELS))),
273e6806e9aSMark Zhang [RDMA_RX_KERNEL_PRIO] =
274b8dfed63SAharon Landau ADD_PRIO(0, RDMA_RX_KERNEL_MIN_LEVEL, 0,
275e6806e9aSMark Zhang FS_CHAINING_CAPS,
276e6806e9aSMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_SWITCH_DOMAIN,
277e6806e9aSMark Zhang ADD_MULTIPLE_PRIO(1, 1))),
278e6806e9aSMark Zhang }
279e6806e9aSMark Zhang };
280e6806e9aSMark Zhang
281b8dfed63SAharon Landau enum {
282b8dfed63SAharon Landau RDMA_TX_COUNTERS_PRIO,
283f91ddd3aSMark Zhang RDMA_TX_IPSEC_PRIO,
284afcb21d5SPatrisious Haddad RDMA_TX_MACSEC_PRIO,
285b8dfed63SAharon Landau RDMA_TX_BYPASS_PRIO,
286b8dfed63SAharon Landau };
287b8dfed63SAharon Landau
288b8dfed63SAharon Landau #define RDMA_TX_BYPASS_MIN_LEVEL MLX5_BY_PASS_NUM_PRIOS
289b8dfed63SAharon Landau #define RDMA_TX_COUNTERS_MIN_LEVEL (RDMA_TX_BYPASS_MIN_LEVEL + 1)
290b8dfed63SAharon Landau
291f91ddd3aSMark Zhang #define RDMA_TX_IPSEC_NUM_PRIOS 1
292f91ddd3aSMark Zhang #define RDMA_TX_IPSEC_PRIO_NUM_LEVELS 1
293f91ddd3aSMark Zhang #define RDMA_TX_IPSEC_MIN_LEVEL (RDMA_TX_COUNTERS_MIN_LEVEL + RDMA_TX_IPSEC_NUM_PRIOS)
294f91ddd3aSMark Zhang
295afcb21d5SPatrisious Haddad #define RDMA_TX_MACSEC_NUM_PRIOS 1
296afcb21d5SPatrisious Haddad #define RDMA_TX_MACESC_PRIO_NUM_LEVELS 1
297afcb21d5SPatrisious Haddad #define RDMA_TX_MACSEC_MIN_LEVEL (RDMA_TX_COUNTERS_MIN_LEVEL + RDMA_TX_MACSEC_NUM_PRIOS)
298afcb21d5SPatrisious Haddad
29924670b1aSMichael Guralnik static struct init_tree_node rdma_tx_root_fs = {
30024670b1aSMichael Guralnik .type = FS_TYPE_NAMESPACE,
301afcb21d5SPatrisious Haddad .ar_size = 4,
30224670b1aSMichael Guralnik .children = (struct init_tree_node[]) {
303b8dfed63SAharon Landau [RDMA_TX_COUNTERS_PRIO] =
304b8dfed63SAharon Landau ADD_PRIO(0, RDMA_TX_COUNTERS_MIN_LEVEL, 0,
305b8dfed63SAharon Landau FS_CHAINING_CAPS,
306b8dfed63SAharon Landau ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
307b8dfed63SAharon Landau ADD_MULTIPLE_PRIO(MLX5_RDMA_TX_NUM_COUNTERS_PRIOS,
308b8dfed63SAharon Landau RDMA_TX_COUNTERS_PRIO_NUM_LEVELS))),
309f91ddd3aSMark Zhang [RDMA_TX_IPSEC_PRIO] =
310f91ddd3aSMark Zhang ADD_PRIO(0, RDMA_TX_IPSEC_MIN_LEVEL, 0,
311f91ddd3aSMark Zhang FS_CHAINING_CAPS,
312f91ddd3aSMark Zhang ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
313f91ddd3aSMark Zhang ADD_MULTIPLE_PRIO(RDMA_TX_IPSEC_NUM_PRIOS,
314f91ddd3aSMark Zhang RDMA_TX_IPSEC_PRIO_NUM_LEVELS))),
315afcb21d5SPatrisious Haddad [RDMA_TX_MACSEC_PRIO] =
316afcb21d5SPatrisious Haddad ADD_PRIO(0, RDMA_TX_MACSEC_MIN_LEVEL, 0,
317afcb21d5SPatrisious Haddad FS_CHAINING_CAPS,
318afcb21d5SPatrisious Haddad ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
319afcb21d5SPatrisious Haddad ADD_MULTIPLE_PRIO(RDMA_TX_MACSEC_NUM_PRIOS,
320afcb21d5SPatrisious Haddad RDMA_TX_MACESC_PRIO_NUM_LEVELS))),
321b8dfed63SAharon Landau [RDMA_TX_BYPASS_PRIO] =
322b8dfed63SAharon Landau ADD_PRIO(0, RDMA_TX_BYPASS_MIN_LEVEL, 0,
32324670b1aSMichael Guralnik FS_CHAINING_CAPS_RDMA_TX,
32424670b1aSMichael Guralnik ADD_NS(MLX5_FLOW_TABLE_MISS_ACTION_DEF,
325b8dfed63SAharon Landau ADD_MULTIPLE_PRIO(RDMA_TX_BYPASS_MIN_LEVEL,
32624670b1aSMichael Guralnik BY_PASS_PRIO_NUM_LEVELS))),
32724670b1aSMichael Guralnik }
32824670b1aSMichael Guralnik };
32924670b1aSMichael Guralnik
330c7784b1cSMaor Gottlieb enum fs_i_lock_class {
331c7784b1cSMaor Gottlieb FS_LOCK_GRANDPARENT,
332c7784b1cSMaor Gottlieb FS_LOCK_PARENT,
333c7784b1cSMaor Gottlieb FS_LOCK_CHILD
334f0d22d18SMaor Gottlieb };
335f0d22d18SMaor Gottlieb
3360d235c3fSMatan Barak static const struct rhashtable_params rhash_fte = {
337c593642cSPankaj Bharadiya .key_len = sizeof_field(struct fs_fte, val),
3380d235c3fSMatan Barak .key_offset = offsetof(struct fs_fte, val),
3390d235c3fSMatan Barak .head_offset = offsetof(struct fs_fte, hash),
3400d235c3fSMatan Barak .automatic_shrinking = true,
3410d235c3fSMatan Barak .min_size = 1,
3420d235c3fSMatan Barak };
3430d235c3fSMatan Barak
344693c6883SMatan Barak static const struct rhashtable_params rhash_fg = {
345c593642cSPankaj Bharadiya .key_len = sizeof_field(struct mlx5_flow_group, mask),
346693c6883SMatan Barak .key_offset = offsetof(struct mlx5_flow_group, mask),
347693c6883SMatan Barak .head_offset = offsetof(struct mlx5_flow_group, hash),
348693c6883SMatan Barak .automatic_shrinking = true,
349693c6883SMatan Barak .min_size = 1,
350693c6883SMatan Barak
351693c6883SMatan Barak };
352693c6883SMatan Barak
353bd71b08eSMaor Gottlieb static void del_hw_flow_table(struct fs_node *node);
354bd71b08eSMaor Gottlieb static void del_hw_flow_group(struct fs_node *node);
355bd71b08eSMaor Gottlieb static void del_hw_fte(struct fs_node *node);
356bd71b08eSMaor Gottlieb static void del_sw_flow_table(struct fs_node *node);
357bd71b08eSMaor Gottlieb static void del_sw_flow_group(struct fs_node *node);
358bd71b08eSMaor Gottlieb static void del_sw_fte(struct fs_node *node);
359139ed6c6SMaor Gottlieb static void del_sw_prio(struct fs_node *node);
360139ed6c6SMaor Gottlieb static void del_sw_ns(struct fs_node *node);
361bd71b08eSMaor Gottlieb /* Delete rule (destination) is special case that
362bd71b08eSMaor Gottlieb * requires to lock the FTE for all the deletion process.
363bd71b08eSMaor Gottlieb */
364bd71b08eSMaor Gottlieb static void del_sw_hw_rule(struct fs_node *node);
365814fb875SMark Bloch static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
366814fb875SMark Bloch struct mlx5_flow_destination *d2);
3679c26f5f8STalat Batheesh static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns);
36874491de9SMark Bloch static struct mlx5_flow_rule *
36974491de9SMark Bloch find_flow_rule(struct fs_fte *fte,
37074491de9SMark Bloch struct mlx5_flow_destination *dest);
371de8575e0SMaor Gottlieb
tree_init_node(struct fs_node * node,void (* del_hw_func)(struct fs_node *),void (* del_sw_func)(struct fs_node *))372de8575e0SMaor Gottlieb static void tree_init_node(struct fs_node *node,
373bd71b08eSMaor Gottlieb void (*del_hw_func)(struct fs_node *),
374bd71b08eSMaor Gottlieb void (*del_sw_func)(struct fs_node *))
375de8575e0SMaor Gottlieb {
376dd8e1945SElena Reshetova refcount_set(&node->refcount, 1);
377de8575e0SMaor Gottlieb INIT_LIST_HEAD(&node->list);
378de8575e0SMaor Gottlieb INIT_LIST_HEAD(&node->children);
379c7784b1cSMaor Gottlieb init_rwsem(&node->lock);
380bd71b08eSMaor Gottlieb node->del_hw_func = del_hw_func;
381bd71b08eSMaor Gottlieb node->del_sw_func = del_sw_func;
38219f100feSMaor Gottlieb node->active = false;
383de8575e0SMaor Gottlieb }
384de8575e0SMaor Gottlieb
tree_add_node(struct fs_node * node,struct fs_node * parent)385de8575e0SMaor Gottlieb static void tree_add_node(struct fs_node *node, struct fs_node *parent)
386de8575e0SMaor Gottlieb {
387de8575e0SMaor Gottlieb if (parent)
388dd8e1945SElena Reshetova refcount_inc(&parent->refcount);
389de8575e0SMaor Gottlieb node->parent = parent;
390de8575e0SMaor Gottlieb
391de8575e0SMaor Gottlieb /* Parent is the root */
392de8575e0SMaor Gottlieb if (!parent)
393de8575e0SMaor Gottlieb node->root = node;
394de8575e0SMaor Gottlieb else
395de8575e0SMaor Gottlieb node->root = parent->root;
396de8575e0SMaor Gottlieb }
397de8575e0SMaor Gottlieb
tree_get_node(struct fs_node * node)398bd71b08eSMaor Gottlieb static int tree_get_node(struct fs_node *node)
399de8575e0SMaor Gottlieb {
400dd8e1945SElena Reshetova return refcount_inc_not_zero(&node->refcount);
401de8575e0SMaor Gottlieb }
402de8575e0SMaor Gottlieb
nested_down_read_ref_node(struct fs_node * node,enum fs_i_lock_class class)403bd71b08eSMaor Gottlieb static void nested_down_read_ref_node(struct fs_node *node,
404bd71b08eSMaor Gottlieb enum fs_i_lock_class class)
405bd71b08eSMaor Gottlieb {
406bd71b08eSMaor Gottlieb if (node) {
407bd71b08eSMaor Gottlieb down_read_nested(&node->lock, class);
408dd8e1945SElena Reshetova refcount_inc(&node->refcount);
409bd71b08eSMaor Gottlieb }
410bd71b08eSMaor Gottlieb }
411bd71b08eSMaor Gottlieb
nested_down_write_ref_node(struct fs_node * node,enum fs_i_lock_class class)412bd71b08eSMaor Gottlieb static void nested_down_write_ref_node(struct fs_node *node,
413c7784b1cSMaor Gottlieb enum fs_i_lock_class class)
414de8575e0SMaor Gottlieb {
415de8575e0SMaor Gottlieb if (node) {
416c7784b1cSMaor Gottlieb down_write_nested(&node->lock, class);
417dd8e1945SElena Reshetova refcount_inc(&node->refcount);
418de8575e0SMaor Gottlieb }
419de8575e0SMaor Gottlieb }
420de8575e0SMaor Gottlieb
down_write_ref_node(struct fs_node * node,bool locked)421476d61b7SEli Britstein static void down_write_ref_node(struct fs_node *node, bool locked)
422de8575e0SMaor Gottlieb {
423de8575e0SMaor Gottlieb if (node) {
424476d61b7SEli Britstein if (!locked)
425c7784b1cSMaor Gottlieb down_write(&node->lock);
426dd8e1945SElena Reshetova refcount_inc(&node->refcount);
427de8575e0SMaor Gottlieb }
428de8575e0SMaor Gottlieb }
429de8575e0SMaor Gottlieb
up_read_ref_node(struct fs_node * node)430bd71b08eSMaor Gottlieb static void up_read_ref_node(struct fs_node *node)
431de8575e0SMaor Gottlieb {
432dd8e1945SElena Reshetova refcount_dec(&node->refcount);
433bd71b08eSMaor Gottlieb up_read(&node->lock);
434bd71b08eSMaor Gottlieb }
435bd71b08eSMaor Gottlieb
up_write_ref_node(struct fs_node * node,bool locked)436476d61b7SEli Britstein static void up_write_ref_node(struct fs_node *node, bool locked)
437bd71b08eSMaor Gottlieb {
438dd8e1945SElena Reshetova refcount_dec(&node->refcount);
439476d61b7SEli Britstein if (!locked)
440c7784b1cSMaor Gottlieb up_write(&node->lock);
441de8575e0SMaor Gottlieb }
442de8575e0SMaor Gottlieb
tree_put_node(struct fs_node * node,bool locked)443476d61b7SEli Britstein static void tree_put_node(struct fs_node *node, bool locked)
444de8575e0SMaor Gottlieb {
445de8575e0SMaor Gottlieb struct fs_node *parent_node = node->parent;
446de8575e0SMaor Gottlieb
447dd8e1945SElena Reshetova if (refcount_dec_and_test(&node->refcount)) {
448bd71b08eSMaor Gottlieb if (node->del_hw_func)
449bd71b08eSMaor Gottlieb node->del_hw_func(node);
450bd71b08eSMaor Gottlieb if (parent_node) {
451476d61b7SEli Britstein down_write_ref_node(parent_node, locked);
452de8575e0SMaor Gottlieb list_del_init(&node->list);
453a369d4acSMaor Gottlieb }
4546eb7a268SRoi Dayan node->del_sw_func(node);
4556eb7a268SRoi Dayan if (parent_node)
4566eb7a268SRoi Dayan up_write_ref_node(parent_node, locked);
457de8575e0SMaor Gottlieb node = NULL;
458de8575e0SMaor Gottlieb }
459de8575e0SMaor Gottlieb if (!node && parent_node)
460476d61b7SEli Britstein tree_put_node(parent_node, locked);
461de8575e0SMaor Gottlieb }
462de8575e0SMaor Gottlieb
tree_remove_node(struct fs_node * node,bool locked)463476d61b7SEli Britstein static int tree_remove_node(struct fs_node *node, bool locked)
464de8575e0SMaor Gottlieb {
465dd8e1945SElena Reshetova if (refcount_read(&node->refcount) > 1) {
466dd8e1945SElena Reshetova refcount_dec(&node->refcount);
467b3638e1aSMaor Gottlieb return -EEXIST;
468b3638e1aSMaor Gottlieb }
469476d61b7SEli Britstein tree_put_node(node, locked);
470de8575e0SMaor Gottlieb return 0;
471de8575e0SMaor Gottlieb }
4725e1626c0SMaor Gottlieb
find_prio(struct mlx5_flow_namespace * ns,unsigned int prio)4735e1626c0SMaor Gottlieb static struct fs_prio *find_prio(struct mlx5_flow_namespace *ns,
4745e1626c0SMaor Gottlieb unsigned int prio)
4755e1626c0SMaor Gottlieb {
4765e1626c0SMaor Gottlieb struct fs_prio *iter_prio;
4775e1626c0SMaor Gottlieb
4785e1626c0SMaor Gottlieb fs_for_each_prio(iter_prio, ns) {
4795e1626c0SMaor Gottlieb if (iter_prio->prio == prio)
4805e1626c0SMaor Gottlieb return iter_prio;
4815e1626c0SMaor Gottlieb }
4825e1626c0SMaor Gottlieb
4835e1626c0SMaor Gottlieb return NULL;
4845e1626c0SMaor Gottlieb }
4855e1626c0SMaor Gottlieb
is_fwd_next_action(u32 action)4869254f8edSMaor Gottlieb static bool is_fwd_next_action(u32 action)
4879254f8edSMaor Gottlieb {
4889254f8edSMaor Gottlieb return action & (MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
4899254f8edSMaor Gottlieb MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
4909254f8edSMaor Gottlieb }
4919254f8edSMaor Gottlieb
is_fwd_dest_type(enum mlx5_flow_destination_type type)492a30c8b90SMark Bloch static bool is_fwd_dest_type(enum mlx5_flow_destination_type type)
493a30c8b90SMark Bloch {
494a30c8b90SMark Bloch return type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM ||
495a30c8b90SMark Bloch type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE ||
496a30c8b90SMark Bloch type == MLX5_FLOW_DESTINATION_TYPE_UPLINK ||
497a30c8b90SMark Bloch type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
498a30c8b90SMark Bloch type == MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER ||
49938bf24c3SYevgeny Kliteynik type == MLX5_FLOW_DESTINATION_TYPE_TIR ||
5004f226b71SMark Zhang type == MLX5_FLOW_DESTINATION_TYPE_RANGE ||
5014f226b71SMark Zhang type == MLX5_FLOW_DESTINATION_TYPE_TABLE_TYPE;
502a30c8b90SMark Bloch }
503a30c8b90SMark Bloch
check_valid_spec(const struct mlx5_flow_spec * spec)504693c6883SMatan Barak static bool check_valid_spec(const struct mlx5_flow_spec *spec)
5055e1626c0SMaor Gottlieb {
506693c6883SMatan Barak int i;
507693c6883SMatan Barak
508693c6883SMatan Barak for (i = 0; i < MLX5_ST_SZ_DW_MATCH_PARAM; i++)
509693c6883SMatan Barak if (spec->match_value[i] & ~spec->match_criteria[i]) {
510693c6883SMatan Barak pr_warn("mlx5_core: match_value differs from match_criteria\n");
511693c6883SMatan Barak return false;
512693c6883SMatan Barak }
513693c6883SMatan Barak
5142aada6c0SYishai Hadas return true;
5155e1626c0SMaor Gottlieb }
5160c56b975SMaor Gottlieb
find_root(struct fs_node * node)517db202995SMark Bloch struct mlx5_flow_root_namespace *find_root(struct fs_node *node)
5180c56b975SMaor Gottlieb {
5190c56b975SMaor Gottlieb struct fs_node *root;
5200c56b975SMaor Gottlieb struct mlx5_flow_namespace *ns;
5210c56b975SMaor Gottlieb
5220c56b975SMaor Gottlieb root = node->root;
5230c56b975SMaor Gottlieb
5240c56b975SMaor Gottlieb if (WARN_ON(root->type != FS_TYPE_NAMESPACE)) {
5250c56b975SMaor Gottlieb pr_warn("mlx5: flow steering node is not in tree or garbaged\n");
5260c56b975SMaor Gottlieb return NULL;
5270c56b975SMaor Gottlieb }
5280c56b975SMaor Gottlieb
5290c56b975SMaor Gottlieb ns = container_of(root, struct mlx5_flow_namespace, node);
5300c56b975SMaor Gottlieb return container_of(ns, struct mlx5_flow_root_namespace, ns);
5310c56b975SMaor Gottlieb }
5320c56b975SMaor Gottlieb
get_steering(struct fs_node * node)533a369d4acSMaor Gottlieb static inline struct mlx5_flow_steering *get_steering(struct fs_node *node)
534a369d4acSMaor Gottlieb {
535a369d4acSMaor Gottlieb struct mlx5_flow_root_namespace *root = find_root(node);
536a369d4acSMaor Gottlieb
537a369d4acSMaor Gottlieb if (root)
538a369d4acSMaor Gottlieb return root->dev->priv.steering;
539a369d4acSMaor Gottlieb return NULL;
540a369d4acSMaor Gottlieb }
541a369d4acSMaor Gottlieb
get_dev(struct fs_node * node)5420c56b975SMaor Gottlieb static inline struct mlx5_core_dev *get_dev(struct fs_node *node)
5430c56b975SMaor Gottlieb {
5440c56b975SMaor Gottlieb struct mlx5_flow_root_namespace *root = find_root(node);
5450c56b975SMaor Gottlieb
5460c56b975SMaor Gottlieb if (root)
5470c56b975SMaor Gottlieb return root->dev;
5480c56b975SMaor Gottlieb return NULL;
5490c56b975SMaor Gottlieb }
5500c56b975SMaor Gottlieb
del_sw_ns(struct fs_node * node)551139ed6c6SMaor Gottlieb static void del_sw_ns(struct fs_node *node)
552139ed6c6SMaor Gottlieb {
553139ed6c6SMaor Gottlieb kfree(node);
554139ed6c6SMaor Gottlieb }
555139ed6c6SMaor Gottlieb
del_sw_prio(struct fs_node * node)556139ed6c6SMaor Gottlieb static void del_sw_prio(struct fs_node *node)
557139ed6c6SMaor Gottlieb {
558139ed6c6SMaor Gottlieb kfree(node);
559139ed6c6SMaor Gottlieb }
560139ed6c6SMaor Gottlieb
del_hw_flow_table(struct fs_node * node)561bd71b08eSMaor Gottlieb static void del_hw_flow_table(struct fs_node *node)
5620c56b975SMaor Gottlieb {
563af76c501SMatan Barak struct mlx5_flow_root_namespace *root;
5640c56b975SMaor Gottlieb struct mlx5_flow_table *ft;
5650c56b975SMaor Gottlieb struct mlx5_core_dev *dev;
5660c56b975SMaor Gottlieb int err;
5670c56b975SMaor Gottlieb
5680c56b975SMaor Gottlieb fs_get_obj(ft, node);
5690c56b975SMaor Gottlieb dev = get_dev(&ft->node);
570af76c501SMatan Barak root = find_root(&ft->node);
5718e4ca986SOr Gerlitz trace_mlx5_fs_del_ft(ft);
5720c56b975SMaor Gottlieb
57319f100feSMaor Gottlieb if (node->active) {
574ae288a48SMaor Gottlieb err = root->cmds->destroy_flow_table(root, ft);
5750c56b975SMaor Gottlieb if (err)
5762974ab6eSSaeed Mahameed mlx5_core_warn(dev, "flow steering can't destroy ft\n");
57719f100feSMaor Gottlieb }
578bd71b08eSMaor Gottlieb }
579bd71b08eSMaor Gottlieb
del_sw_flow_table(struct fs_node * node)580bd71b08eSMaor Gottlieb static void del_sw_flow_table(struct fs_node *node)
581bd71b08eSMaor Gottlieb {
582bd71b08eSMaor Gottlieb struct mlx5_flow_table *ft;
583bd71b08eSMaor Gottlieb struct fs_prio *prio;
584bd71b08eSMaor Gottlieb
585bd71b08eSMaor Gottlieb fs_get_obj(ft, node);
586bd71b08eSMaor Gottlieb
587693c6883SMatan Barak rhltable_destroy(&ft->fgs_hash);
588aee37f3dSRoi Dayan if (ft->node.parent) {
5890c56b975SMaor Gottlieb fs_get_obj(prio, ft->node.parent);
5900c56b975SMaor Gottlieb prio->num_ft--;
591aee37f3dSRoi Dayan }
592a369d4acSMaor Gottlieb kfree(ft);
5930c56b975SMaor Gottlieb }
5940c56b975SMaor Gottlieb
modify_fte(struct fs_fte * fte)595e7aafc8fSEli Britstein static void modify_fte(struct fs_fte *fte)
5960c56b975SMaor Gottlieb {
597af76c501SMatan Barak struct mlx5_flow_root_namespace *root;
5980c56b975SMaor Gottlieb struct mlx5_flow_table *ft;
5990c56b975SMaor Gottlieb struct mlx5_flow_group *fg;
600e7aafc8fSEli Britstein struct mlx5_core_dev *dev;
6010c56b975SMaor Gottlieb int err;
602e7aafc8fSEli Britstein
603e7aafc8fSEli Britstein fs_get_obj(fg, fte->node.parent);
604e7aafc8fSEli Britstein fs_get_obj(ft, fg->node.parent);
605e7aafc8fSEli Britstein dev = get_dev(&fte->node);
606e7aafc8fSEli Britstein
607e7aafc8fSEli Britstein root = find_root(&ft->node);
608ae288a48SMaor Gottlieb err = root->cmds->update_fte(root, ft, fg, fte->modify_mask, fte);
609e7aafc8fSEli Britstein if (err)
610e7aafc8fSEli Britstein mlx5_core_warn(dev,
611e7aafc8fSEli Britstein "%s can't del rule fg id=%d fte_index=%d\n",
612e7aafc8fSEli Britstein __func__, fg->id, fte->index);
613e7aafc8fSEli Britstein fte->modify_mask = 0;
614e7aafc8fSEli Britstein }
615e7aafc8fSEli Britstein
del_sw_hw_rule(struct fs_node * node)616e7aafc8fSEli Britstein static void del_sw_hw_rule(struct fs_node *node)
617e7aafc8fSEli Britstein {
618e7aafc8fSEli Britstein struct mlx5_flow_rule *rule;
619e7aafc8fSEli Britstein struct fs_fte *fte;
6200c56b975SMaor Gottlieb
6210c56b975SMaor Gottlieb fs_get_obj(rule, node);
6220c56b975SMaor Gottlieb fs_get_obj(fte, rule->node.parent);
6234c03e69aSMatan Barak trace_mlx5_fs_del_rule(rule);
6249254f8edSMaor Gottlieb if (is_fwd_next_action(rule->sw_action)) {
625b3638e1aSMaor Gottlieb mutex_lock(&rule->dest_attr.ft->lock);
626b3638e1aSMaor Gottlieb list_del(&rule->next_ft);
627b3638e1aSMaor Gottlieb mutex_unlock(&rule->dest_attr.ft->lock);
628b3638e1aSMaor Gottlieb }
629ae058314SMark Bloch
630d49d6307SMark Bloch if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_COUNTER) {
631d49d6307SMark Bloch --fte->dests_size;
632e7aafc8fSEli Britstein fte->modify_mask |=
633e7aafc8fSEli Britstein BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION) |
634202854e9SChris Mi BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
635d2ec6a35SMatan Barak fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_COUNT;
636ae058314SMark Bloch goto out;
637ae058314SMark Bloch }
638ae058314SMark Bloch
639d49d6307SMark Bloch if (rule->dest_attr.type == MLX5_FLOW_DESTINATION_TYPE_PORT) {
640d49d6307SMark Bloch --fte->dests_size;
6418cbcc5efSMichael Guralnik fte->modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
6428cbcc5efSMichael Guralnik fte->action.action &= ~MLX5_FLOW_CONTEXT_ACTION_ALLOW;
6438cbcc5efSMichael Guralnik goto out;
6448cbcc5efSMichael Guralnik }
6458cbcc5efSMichael Guralnik
646a30c8b90SMark Bloch if (is_fwd_dest_type(rule->dest_attr.type)) {
647d49d6307SMark Bloch --fte->dests_size;
648a30c8b90SMark Bloch --fte->fwd_dests;
649a30c8b90SMark Bloch
650a30c8b90SMark Bloch if (!fte->fwd_dests)
651a30c8b90SMark Bloch fte->action.action &=
652a30c8b90SMark Bloch ~MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
653e7aafc8fSEli Britstein fte->modify_mask |=
654e7aafc8fSEli Britstein BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
655c3ae3a9cSMark Bloch goto out;
656ae058314SMark Bloch }
657ae058314SMark Bloch out:
658a369d4acSMaor Gottlieb kfree(rule);
6590c56b975SMaor Gottlieb }
6600c56b975SMaor Gottlieb
del_hw_fte(struct fs_node * node)661bd71b08eSMaor Gottlieb static void del_hw_fte(struct fs_node *node)
6620c56b975SMaor Gottlieb {
663af76c501SMatan Barak struct mlx5_flow_root_namespace *root;
6640c56b975SMaor Gottlieb struct mlx5_flow_table *ft;
6650c56b975SMaor Gottlieb struct mlx5_flow_group *fg;
6660c56b975SMaor Gottlieb struct mlx5_core_dev *dev;
6670c56b975SMaor Gottlieb struct fs_fte *fte;
6680c56b975SMaor Gottlieb int err;
6690c56b975SMaor Gottlieb
6700c56b975SMaor Gottlieb fs_get_obj(fte, node);
6710c56b975SMaor Gottlieb fs_get_obj(fg, fte->node.parent);
6720c56b975SMaor Gottlieb fs_get_obj(ft, fg->node.parent);
6730c56b975SMaor Gottlieb
674bd71b08eSMaor Gottlieb trace_mlx5_fs_del_fte(fte);
6753a09fae0SMark Bloch WARN_ON(fte->dests_size);
6760c56b975SMaor Gottlieb dev = get_dev(&ft->node);
677af76c501SMatan Barak root = find_root(&ft->node);
67819f100feSMaor Gottlieb if (node->active) {
679ae288a48SMaor Gottlieb err = root->cmds->delete_fte(root, ft, fte);
6800c56b975SMaor Gottlieb if (err)
6812974ab6eSSaeed Mahameed mlx5_core_warn(dev,
6822974ab6eSSaeed Mahameed "flow steering can't delete fte in index %d of flow group id %d\n",
6830c56b975SMaor Gottlieb fte->index, fg->id);
68479398543SJiapeng Zhong node->active = false;
68519f100feSMaor Gottlieb }
686bd71b08eSMaor Gottlieb }
687bd71b08eSMaor Gottlieb
del_sw_fte(struct fs_node * node)688bd71b08eSMaor Gottlieb static void del_sw_fte(struct fs_node *node)
689bd71b08eSMaor Gottlieb {
6901f0593e7SParav Pandit struct mlx5_flow_steering *steering = get_steering(node);
691bd71b08eSMaor Gottlieb struct mlx5_flow_group *fg;
692bd71b08eSMaor Gottlieb struct fs_fte *fte;
693bd71b08eSMaor Gottlieb int err;
694bd71b08eSMaor Gottlieb
695bd71b08eSMaor Gottlieb fs_get_obj(fte, node);
696bd71b08eSMaor Gottlieb fs_get_obj(fg, fte->node.parent);
6970c56b975SMaor Gottlieb
69819f100feSMaor Gottlieb err = rhashtable_remove_fast(&fg->ftes_hash,
69919f100feSMaor Gottlieb &fte->hash,
70019f100feSMaor Gottlieb rhash_fte);
70119f100feSMaor Gottlieb WARN_ON(err);
7028802b8a4SRoi Dayan ida_free(&fg->fte_allocator, fte->index - fg->start_index);
7031f0593e7SParav Pandit kmem_cache_free(steering->ftes_cache, fte);
7040c56b975SMaor Gottlieb }
7050c56b975SMaor Gottlieb
del_hw_flow_group(struct fs_node * node)706bd71b08eSMaor Gottlieb static void del_hw_flow_group(struct fs_node *node)
7070c56b975SMaor Gottlieb {
708af76c501SMatan Barak struct mlx5_flow_root_namespace *root;
7090c56b975SMaor Gottlieb struct mlx5_flow_group *fg;
7100c56b975SMaor Gottlieb struct mlx5_flow_table *ft;
7110c56b975SMaor Gottlieb struct mlx5_core_dev *dev;
7120c56b975SMaor Gottlieb
7130c56b975SMaor Gottlieb fs_get_obj(fg, node);
7140c56b975SMaor Gottlieb fs_get_obj(ft, fg->node.parent);
7150c56b975SMaor Gottlieb dev = get_dev(&ft->node);
7164c03e69aSMatan Barak trace_mlx5_fs_del_fg(fg);
7170c56b975SMaor Gottlieb
718af76c501SMatan Barak root = find_root(&ft->node);
719ae288a48SMaor Gottlieb if (fg->node.active && root->cmds->destroy_flow_group(root, ft, fg))
720bd71b08eSMaor Gottlieb mlx5_core_warn(dev, "flow steering can't destroy fg %d of ft %d\n",
721bd71b08eSMaor Gottlieb fg->id, ft->id);
722bd71b08eSMaor Gottlieb }
723bd71b08eSMaor Gottlieb
del_sw_flow_group(struct fs_node * node)724bd71b08eSMaor Gottlieb static void del_sw_flow_group(struct fs_node *node)
725bd71b08eSMaor Gottlieb {
726a369d4acSMaor Gottlieb struct mlx5_flow_steering *steering = get_steering(node);
727bd71b08eSMaor Gottlieb struct mlx5_flow_group *fg;
728bd71b08eSMaor Gottlieb struct mlx5_flow_table *ft;
729bd71b08eSMaor Gottlieb int err;
730bd71b08eSMaor Gottlieb
731bd71b08eSMaor Gottlieb fs_get_obj(fg, node);
732bd71b08eSMaor Gottlieb fs_get_obj(ft, fg->node.parent);
73332dba76aSPaul Blakey
7340d235c3fSMatan Barak rhashtable_destroy(&fg->ftes_hash);
73575d1d187SMaor Gottlieb ida_destroy(&fg->fte_allocator);
73679cdb0aaSPaul Blakey if (ft->autogroup.active &&
73779cdb0aaSPaul Blakey fg->max_ftes == ft->autogroup.group_size &&
73879cdb0aaSPaul Blakey fg->start_index < ft->autogroup.max_fte)
739bd71b08eSMaor Gottlieb ft->autogroup.num_groups--;
740693c6883SMatan Barak err = rhltable_remove(&ft->fgs_hash,
741693c6883SMatan Barak &fg->hash,
742693c6883SMatan Barak rhash_fg);
743693c6883SMatan Barak WARN_ON(err);
744a369d4acSMaor Gottlieb kmem_cache_free(steering->fgs_cache, fg);
7450c56b975SMaor Gottlieb }
7460c56b975SMaor Gottlieb
insert_fte(struct mlx5_flow_group * fg,struct fs_fte * fte)747f5c2ff17SMaor Gottlieb static int insert_fte(struct mlx5_flow_group *fg, struct fs_fte *fte)
748f5c2ff17SMaor Gottlieb {
749f5c2ff17SMaor Gottlieb int index;
750f5c2ff17SMaor Gottlieb int ret;
751f5c2ff17SMaor Gottlieb
7528802b8a4SRoi Dayan index = ida_alloc_max(&fg->fte_allocator, fg->max_ftes - 1, GFP_KERNEL);
753f5c2ff17SMaor Gottlieb if (index < 0)
754f5c2ff17SMaor Gottlieb return index;
755f5c2ff17SMaor Gottlieb
756f5c2ff17SMaor Gottlieb fte->index = index + fg->start_index;
757f5c2ff17SMaor Gottlieb ret = rhashtable_insert_fast(&fg->ftes_hash,
758f5c2ff17SMaor Gottlieb &fte->hash,
759f5c2ff17SMaor Gottlieb rhash_fte);
760f5c2ff17SMaor Gottlieb if (ret)
761f5c2ff17SMaor Gottlieb goto err_ida_remove;
762f5c2ff17SMaor Gottlieb
763f5c2ff17SMaor Gottlieb tree_add_node(&fte->node, &fg->node);
764f5c2ff17SMaor Gottlieb list_add_tail(&fte->node.list, &fg->node.children);
765f5c2ff17SMaor Gottlieb return 0;
766f5c2ff17SMaor Gottlieb
767f5c2ff17SMaor Gottlieb err_ida_remove:
7688802b8a4SRoi Dayan ida_free(&fg->fte_allocator, index);
769f5c2ff17SMaor Gottlieb return ret;
770f5c2ff17SMaor Gottlieb }
771f5c2ff17SMaor Gottlieb
alloc_fte(struct mlx5_flow_table * ft,const struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act)772a369d4acSMaor Gottlieb static struct fs_fte *alloc_fte(struct mlx5_flow_table *ft,
7735233794bSArnd Bergmann const struct mlx5_flow_spec *spec,
774f5c2ff17SMaor Gottlieb struct mlx5_flow_act *flow_act)
7750c56b975SMaor Gottlieb {
776a369d4acSMaor Gottlieb struct mlx5_flow_steering *steering = get_steering(&ft->node);
7770c56b975SMaor Gottlieb struct fs_fte *fte;
7780c56b975SMaor Gottlieb
779a369d4acSMaor Gottlieb fte = kmem_cache_zalloc(steering->ftes_cache, GFP_KERNEL);
7800c56b975SMaor Gottlieb if (!fte)
7810c56b975SMaor Gottlieb return ERR_PTR(-ENOMEM);
7820c56b975SMaor Gottlieb
783bb0ee7dcSJianbo Liu memcpy(fte->val, &spec->match_value, sizeof(fte->val));
7840c56b975SMaor Gottlieb fte->node.type = FS_TYPE_FLOW_ENTRY;
785d2ec6a35SMatan Barak fte->action = *flow_act;
786bb0ee7dcSJianbo Liu fte->flow_context = spec->flow_context;
7870c56b975SMaor Gottlieb
788cefc2355SMaor Gottlieb tree_init_node(&fte->node, del_hw_fte, del_sw_fte);
78919f100feSMaor Gottlieb
79019f100feSMaor Gottlieb return fte;
79119f100feSMaor Gottlieb }
79219f100feSMaor Gottlieb
dealloc_flow_group(struct mlx5_flow_steering * steering,struct mlx5_flow_group * fg)793a369d4acSMaor Gottlieb static void dealloc_flow_group(struct mlx5_flow_steering *steering,
794a369d4acSMaor Gottlieb struct mlx5_flow_group *fg)
79519f100feSMaor Gottlieb {
79619f100feSMaor Gottlieb rhashtable_destroy(&fg->ftes_hash);
797a369d4acSMaor Gottlieb kmem_cache_free(steering->fgs_cache, fg);
79819f100feSMaor Gottlieb }
79919f100feSMaor Gottlieb
alloc_flow_group(struct mlx5_flow_steering * steering,u8 match_criteria_enable,const void * match_criteria,int start_index,int end_index)800a369d4acSMaor Gottlieb static struct mlx5_flow_group *alloc_flow_group(struct mlx5_flow_steering *steering,
801a369d4acSMaor Gottlieb u8 match_criteria_enable,
8025233794bSArnd Bergmann const void *match_criteria,
80319f100feSMaor Gottlieb int start_index,
80419f100feSMaor Gottlieb int end_index)
8050c56b975SMaor Gottlieb {
8060c56b975SMaor Gottlieb struct mlx5_flow_group *fg;
8070d235c3fSMatan Barak int ret;
8080d235c3fSMatan Barak
809a369d4acSMaor Gottlieb fg = kmem_cache_zalloc(steering->fgs_cache, GFP_KERNEL);
8100c56b975SMaor Gottlieb if (!fg)
8110c56b975SMaor Gottlieb return ERR_PTR(-ENOMEM);
8120c56b975SMaor Gottlieb
8130d235c3fSMatan Barak ret = rhashtable_init(&fg->ftes_hash, &rhash_fte);
8140d235c3fSMatan Barak if (ret) {
815a369d4acSMaor Gottlieb kmem_cache_free(steering->fgs_cache, fg);
8160d235c3fSMatan Barak return ERR_PTR(ret);
8170d235c3fSMatan Barak }
818fc9c5a4aSTonghao Zhang
81975d1d187SMaor Gottlieb ida_init(&fg->fte_allocator);
8200c56b975SMaor Gottlieb fg->mask.match_criteria_enable = match_criteria_enable;
8210c56b975SMaor Gottlieb memcpy(&fg->mask.match_criteria, match_criteria,
8220c56b975SMaor Gottlieb sizeof(fg->mask.match_criteria));
8230c56b975SMaor Gottlieb fg->node.type = FS_TYPE_FLOW_GROUP;
82419f100feSMaor Gottlieb fg->start_index = start_index;
82519f100feSMaor Gottlieb fg->max_ftes = end_index - start_index + 1;
82619f100feSMaor Gottlieb
82719f100feSMaor Gottlieb return fg;
82819f100feSMaor Gottlieb }
82919f100feSMaor Gottlieb
alloc_insert_flow_group(struct mlx5_flow_table * ft,u8 match_criteria_enable,const void * match_criteria,int start_index,int end_index,struct list_head * prev)83019f100feSMaor Gottlieb static struct mlx5_flow_group *alloc_insert_flow_group(struct mlx5_flow_table *ft,
83119f100feSMaor Gottlieb u8 match_criteria_enable,
8325233794bSArnd Bergmann const void *match_criteria,
83319f100feSMaor Gottlieb int start_index,
83419f100feSMaor Gottlieb int end_index,
83519f100feSMaor Gottlieb struct list_head *prev)
83619f100feSMaor Gottlieb {
837a369d4acSMaor Gottlieb struct mlx5_flow_steering *steering = get_steering(&ft->node);
83819f100feSMaor Gottlieb struct mlx5_flow_group *fg;
83919f100feSMaor Gottlieb int ret;
84019f100feSMaor Gottlieb
841a369d4acSMaor Gottlieb fg = alloc_flow_group(steering, match_criteria_enable, match_criteria,
84219f100feSMaor Gottlieb start_index, end_index);
84319f100feSMaor Gottlieb if (IS_ERR(fg))
84419f100feSMaor Gottlieb return fg;
84519f100feSMaor Gottlieb
84619f100feSMaor Gottlieb /* initialize refcnt, add to parent list */
84719f100feSMaor Gottlieb ret = rhltable_insert(&ft->fgs_hash,
84819f100feSMaor Gottlieb &fg->hash,
84919f100feSMaor Gottlieb rhash_fg);
85019f100feSMaor Gottlieb if (ret) {
851a369d4acSMaor Gottlieb dealloc_flow_group(steering, fg);
85219f100feSMaor Gottlieb return ERR_PTR(ret);
85319f100feSMaor Gottlieb }
85419f100feSMaor Gottlieb
855bd71b08eSMaor Gottlieb tree_init_node(&fg->node, del_hw_flow_group, del_sw_flow_group);
85619f100feSMaor Gottlieb tree_add_node(&fg->node, &ft->node);
85719f100feSMaor Gottlieb /* Add node to group list */
85819f100feSMaor Gottlieb list_add(&fg->node.list, prev);
859bd71b08eSMaor Gottlieb atomic_inc(&ft->node.version);
86019f100feSMaor Gottlieb
8610c56b975SMaor Gottlieb return fg;
8620c56b975SMaor Gottlieb }
8630c56b975SMaor Gottlieb
alloc_flow_table(int level,u16 vport,enum fs_flow_table_type table_type,enum fs_flow_table_op_mod op_mod,u32 flags)86404745afbSPaul Blakey static struct mlx5_flow_table *alloc_flow_table(int level, u16 vport,
865aaff1beaSAviv Heller enum fs_flow_table_type table_type,
866c9f1b073SHadar Hen Zion enum fs_flow_table_op_mod op_mod,
867c9f1b073SHadar Hen Zion u32 flags)
8680c56b975SMaor Gottlieb {
8690c56b975SMaor Gottlieb struct mlx5_flow_table *ft;
870693c6883SMatan Barak int ret;
8710c56b975SMaor Gottlieb
8720c56b975SMaor Gottlieb ft = kzalloc(sizeof(*ft), GFP_KERNEL);
8730c56b975SMaor Gottlieb if (!ft)
874693c6883SMatan Barak return ERR_PTR(-ENOMEM);
875693c6883SMatan Barak
876693c6883SMatan Barak ret = rhltable_init(&ft->fgs_hash, &rhash_fg);
877693c6883SMatan Barak if (ret) {
878693c6883SMatan Barak kfree(ft);
879693c6883SMatan Barak return ERR_PTR(ret);
880693c6883SMatan Barak }
8810c56b975SMaor Gottlieb
8820c56b975SMaor Gottlieb ft->level = level;
8830c56b975SMaor Gottlieb ft->node.type = FS_TYPE_FLOW_TABLE;
884aaff1beaSAviv Heller ft->op_mod = op_mod;
8850c56b975SMaor Gottlieb ft->type = table_type;
886efdc810bSMohamad Haj Yahia ft->vport = vport;
887c9f1b073SHadar Hen Zion ft->flags = flags;
888b3638e1aSMaor Gottlieb INIT_LIST_HEAD(&ft->fwd_rules);
889b3638e1aSMaor Gottlieb mutex_init(&ft->lock);
8900c56b975SMaor Gottlieb
8910c56b975SMaor Gottlieb return ft;
8920c56b975SMaor Gottlieb }
8930c56b975SMaor Gottlieb
894fdb6896fSMaor Gottlieb /* If reverse is false, then we search for the first flow table in the
895fdb6896fSMaor Gottlieb * root sub-tree from start(closest from right), else we search for the
896fdb6896fSMaor Gottlieb * last flow table in the root sub-tree till start(closest from left).
897fdb6896fSMaor Gottlieb */
find_closest_ft_recursive(struct fs_node * root,struct list_head * start,bool reverse)898fdb6896fSMaor Gottlieb static struct mlx5_flow_table *find_closest_ft_recursive(struct fs_node *root,
899fdb6896fSMaor Gottlieb struct list_head *start,
900fdb6896fSMaor Gottlieb bool reverse)
901fdb6896fSMaor Gottlieb {
902fdb6896fSMaor Gottlieb #define list_advance_entry(pos, reverse) \
903fdb6896fSMaor Gottlieb ((reverse) ? list_prev_entry(pos, list) : list_next_entry(pos, list))
904fdb6896fSMaor Gottlieb
905fdb6896fSMaor Gottlieb #define list_for_each_advance_continue(pos, head, reverse) \
906fdb6896fSMaor Gottlieb for (pos = list_advance_entry(pos, reverse); \
907fdb6896fSMaor Gottlieb &pos->list != (head); \
908fdb6896fSMaor Gottlieb pos = list_advance_entry(pos, reverse))
909fdb6896fSMaor Gottlieb
910fdb6896fSMaor Gottlieb struct fs_node *iter = list_entry(start, struct fs_node, list);
911fdb6896fSMaor Gottlieb struct mlx5_flow_table *ft = NULL;
912fdb6896fSMaor Gottlieb
913c635ca45SJianbo Liu if (!root)
914fdb6896fSMaor Gottlieb return NULL;
915fdb6896fSMaor Gottlieb
916fdb6896fSMaor Gottlieb list_for_each_advance_continue(iter, &root->children, reverse) {
917fdb6896fSMaor Gottlieb if (iter->type == FS_TYPE_FLOW_TABLE) {
918fdb6896fSMaor Gottlieb fs_get_obj(ft, iter);
919fdb6896fSMaor Gottlieb return ft;
920fdb6896fSMaor Gottlieb }
921fdb6896fSMaor Gottlieb ft = find_closest_ft_recursive(iter, &iter->children, reverse);
922fdb6896fSMaor Gottlieb if (ft)
923fdb6896fSMaor Gottlieb return ft;
924fdb6896fSMaor Gottlieb }
925fdb6896fSMaor Gottlieb
926fdb6896fSMaor Gottlieb return ft;
927fdb6896fSMaor Gottlieb }
928fdb6896fSMaor Gottlieb
find_prio_chains_parent(struct fs_node * parent,struct fs_node ** child)929c635ca45SJianbo Liu static struct fs_node *find_prio_chains_parent(struct fs_node *parent,
930c635ca45SJianbo Liu struct fs_node **child)
931fdb6896fSMaor Gottlieb {
932c635ca45SJianbo Liu struct fs_node *node = NULL;
933c635ca45SJianbo Liu
934c635ca45SJianbo Liu while (parent && parent->type != FS_TYPE_PRIO_CHAINS) {
935c635ca45SJianbo Liu node = parent;
936c635ca45SJianbo Liu parent = parent->parent;
937c635ca45SJianbo Liu }
938c635ca45SJianbo Liu
939c635ca45SJianbo Liu if (child)
940c635ca45SJianbo Liu *child = node;
941c635ca45SJianbo Liu
942c635ca45SJianbo Liu return parent;
943c635ca45SJianbo Liu }
944c635ca45SJianbo Liu
945618d28a5SJianbo Liu /* If reverse is false then return the first flow table next to the passed node
946618d28a5SJianbo Liu * in the tree, else return the last flow table before the node in the tree.
947c635ca45SJianbo Liu * If skip is true, skip the flow tables in the same prio_chains prio.
948fdb6896fSMaor Gottlieb */
find_closest_ft(struct fs_node * node,bool reverse,bool skip)949c635ca45SJianbo Liu static struct mlx5_flow_table *find_closest_ft(struct fs_node *node, bool reverse,
950c635ca45SJianbo Liu bool skip)
951fdb6896fSMaor Gottlieb {
952c635ca45SJianbo Liu struct fs_node *prio_chains_parent = NULL;
953fdb6896fSMaor Gottlieb struct mlx5_flow_table *ft = NULL;
954fdb6896fSMaor Gottlieb struct fs_node *curr_node;
955fdb6896fSMaor Gottlieb struct fs_node *parent;
956fdb6896fSMaor Gottlieb
957c635ca45SJianbo Liu if (skip)
958c635ca45SJianbo Liu prio_chains_parent = find_prio_chains_parent(node, NULL);
959618d28a5SJianbo Liu parent = node->parent;
960618d28a5SJianbo Liu curr_node = node;
961fdb6896fSMaor Gottlieb while (!ft && parent) {
962c635ca45SJianbo Liu if (parent != prio_chains_parent)
963c635ca45SJianbo Liu ft = find_closest_ft_recursive(parent, &curr_node->list,
964c635ca45SJianbo Liu reverse);
965fdb6896fSMaor Gottlieb curr_node = parent;
966fdb6896fSMaor Gottlieb parent = curr_node->parent;
967fdb6896fSMaor Gottlieb }
968fdb6896fSMaor Gottlieb return ft;
969fdb6896fSMaor Gottlieb }
970fdb6896fSMaor Gottlieb
971fdb6896fSMaor Gottlieb /* Assuming all the tree is locked by mutex chain lock */
find_next_chained_ft(struct fs_node * node)972618d28a5SJianbo Liu static struct mlx5_flow_table *find_next_chained_ft(struct fs_node *node)
973fdb6896fSMaor Gottlieb {
974c635ca45SJianbo Liu return find_closest_ft(node, false, true);
975fdb6896fSMaor Gottlieb }
976fdb6896fSMaor Gottlieb
977fdb6896fSMaor Gottlieb /* Assuming all the tree is locked by mutex chain lock */
find_prev_chained_ft(struct fs_node * node)978618d28a5SJianbo Liu static struct mlx5_flow_table *find_prev_chained_ft(struct fs_node *node)
979fdb6896fSMaor Gottlieb {
980c635ca45SJianbo Liu return find_closest_ft(node, true, true);
981fdb6896fSMaor Gottlieb }
982fdb6896fSMaor Gottlieb
find_next_fwd_ft(struct mlx5_flow_table * ft,struct mlx5_flow_act * flow_act)9839254f8edSMaor Gottlieb static struct mlx5_flow_table *find_next_fwd_ft(struct mlx5_flow_table *ft,
9849254f8edSMaor Gottlieb struct mlx5_flow_act *flow_act)
9859254f8edSMaor Gottlieb {
9869254f8edSMaor Gottlieb struct fs_prio *prio;
98759f8f7c8SMaor Gottlieb bool next_ns;
9889254f8edSMaor Gottlieb
98959f8f7c8SMaor Gottlieb next_ns = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS;
99059f8f7c8SMaor Gottlieb fs_get_obj(prio, next_ns ? ft->ns->node.parent : ft->node.parent);
9919254f8edSMaor Gottlieb
992618d28a5SJianbo Liu return find_next_chained_ft(&prio->node);
9939254f8edSMaor Gottlieb }
9949254f8edSMaor Gottlieb
connect_fts_in_prio(struct mlx5_core_dev * dev,struct fs_prio * prio,struct mlx5_flow_table * ft)995f90edfd2SMaor Gottlieb static int connect_fts_in_prio(struct mlx5_core_dev *dev,
996f90edfd2SMaor Gottlieb struct fs_prio *prio,
997f90edfd2SMaor Gottlieb struct mlx5_flow_table *ft)
998f90edfd2SMaor Gottlieb {
999af76c501SMatan Barak struct mlx5_flow_root_namespace *root = find_root(&prio->node);
1000f90edfd2SMaor Gottlieb struct mlx5_flow_table *iter;
1001f90edfd2SMaor Gottlieb int err;
1002f90edfd2SMaor Gottlieb
1003f90edfd2SMaor Gottlieb fs_for_each_ft(iter, prio) {
1004ae288a48SMaor Gottlieb err = root->cmds->modify_flow_table(root, iter, ft);
1005f90edfd2SMaor Gottlieb if (err) {
10066c4e9bcfSLeon Romanovsky mlx5_core_err(dev,
10076c4e9bcfSLeon Romanovsky "Failed to modify flow table id %d, type %d, err %d\n",
10086c4e9bcfSLeon Romanovsky iter->id, iter->type, err);
1009f90edfd2SMaor Gottlieb /* The driver is out of sync with the FW */
1010f90edfd2SMaor Gottlieb return err;
1011f90edfd2SMaor Gottlieb }
1012f90edfd2SMaor Gottlieb }
1013f90edfd2SMaor Gottlieb return 0;
1014f90edfd2SMaor Gottlieb }
1015f90edfd2SMaor Gottlieb
find_closet_ft_prio_chains(struct fs_node * node,struct fs_node * parent,struct fs_node ** child,bool reverse)1016c635ca45SJianbo Liu static struct mlx5_flow_table *find_closet_ft_prio_chains(struct fs_node *node,
1017c635ca45SJianbo Liu struct fs_node *parent,
1018c635ca45SJianbo Liu struct fs_node **child,
1019c635ca45SJianbo Liu bool reverse)
1020c635ca45SJianbo Liu {
1021c635ca45SJianbo Liu struct mlx5_flow_table *ft;
1022c635ca45SJianbo Liu
1023c635ca45SJianbo Liu ft = find_closest_ft(node, reverse, false);
1024c635ca45SJianbo Liu
1025c635ca45SJianbo Liu if (ft && parent == find_prio_chains_parent(&ft->node, child))
1026c635ca45SJianbo Liu return ft;
1027c635ca45SJianbo Liu
1028c635ca45SJianbo Liu return NULL;
1029c635ca45SJianbo Liu }
1030c635ca45SJianbo Liu
1031f90edfd2SMaor Gottlieb /* Connect flow tables from previous priority of prio to ft */
connect_prev_fts(struct mlx5_core_dev * dev,struct mlx5_flow_table * ft,struct fs_prio * prio)1032f90edfd2SMaor Gottlieb static int connect_prev_fts(struct mlx5_core_dev *dev,
1033f90edfd2SMaor Gottlieb struct mlx5_flow_table *ft,
1034f90edfd2SMaor Gottlieb struct fs_prio *prio)
1035f90edfd2SMaor Gottlieb {
1036c635ca45SJianbo Liu struct fs_node *prio_parent, *parent = NULL, *child, *node;
1037f90edfd2SMaor Gottlieb struct mlx5_flow_table *prev_ft;
1038c635ca45SJianbo Liu int err = 0;
1039f90edfd2SMaor Gottlieb
1040c635ca45SJianbo Liu prio_parent = find_prio_chains_parent(&prio->node, &child);
1041c635ca45SJianbo Liu
1042c635ca45SJianbo Liu /* return directly if not under the first sub ns of prio_chains prio */
1043c635ca45SJianbo Liu if (prio_parent && !list_is_first(&child->list, &prio_parent->children))
1044c635ca45SJianbo Liu return 0;
1045f90edfd2SMaor Gottlieb
1046618d28a5SJianbo Liu prev_ft = find_prev_chained_ft(&prio->node);
1047c635ca45SJianbo Liu while (prev_ft) {
1048f90edfd2SMaor Gottlieb struct fs_prio *prev_prio;
1049f90edfd2SMaor Gottlieb
1050f90edfd2SMaor Gottlieb fs_get_obj(prev_prio, prev_ft->node.parent);
1051c635ca45SJianbo Liu err = connect_fts_in_prio(dev, prev_prio, ft);
1052c635ca45SJianbo Liu if (err)
1053c635ca45SJianbo Liu break;
1054c635ca45SJianbo Liu
1055c635ca45SJianbo Liu if (!parent) {
1056c635ca45SJianbo Liu parent = find_prio_chains_parent(&prev_prio->node, &child);
1057c635ca45SJianbo Liu if (!parent)
1058c635ca45SJianbo Liu break;
1059f90edfd2SMaor Gottlieb }
1060c635ca45SJianbo Liu
1061c635ca45SJianbo Liu node = child;
1062c635ca45SJianbo Liu prev_ft = find_closet_ft_prio_chains(node, parent, &child, true);
1063c635ca45SJianbo Liu }
1064c635ca45SJianbo Liu return err;
1065f90edfd2SMaor Gottlieb }
1066f90edfd2SMaor Gottlieb
update_root_ft_create(struct mlx5_flow_table * ft,struct fs_prio * prio)10672cc43b49SMaor Gottlieb static int update_root_ft_create(struct mlx5_flow_table *ft, struct fs_prio
10682cc43b49SMaor Gottlieb *prio)
10692cc43b49SMaor Gottlieb {
10702cc43b49SMaor Gottlieb struct mlx5_flow_root_namespace *root = find_root(&prio->node);
1071dae37456SAlex Vesker struct mlx5_ft_underlay_qp *uqp;
10722cc43b49SMaor Gottlieb int min_level = INT_MAX;
1073aef6c443STariq Toukan int err = 0;
1074dae37456SAlex Vesker u32 qpn;
10752cc43b49SMaor Gottlieb
10762cc43b49SMaor Gottlieb if (root->root_ft)
10772cc43b49SMaor Gottlieb min_level = root->root_ft->level;
10782cc43b49SMaor Gottlieb
10792cc43b49SMaor Gottlieb if (ft->level >= min_level)
10802cc43b49SMaor Gottlieb return 0;
10812cc43b49SMaor Gottlieb
1082dae37456SAlex Vesker if (list_empty(&root->underlay_qpns)) {
1083dae37456SAlex Vesker /* Don't set any QPN (zero) in case QPN list is empty */
1084dae37456SAlex Vesker qpn = 0;
1085ae288a48SMaor Gottlieb err = root->cmds->update_root_ft(root, ft, qpn, false);
1086dae37456SAlex Vesker } else {
1087dae37456SAlex Vesker list_for_each_entry(uqp, &root->underlay_qpns, list) {
1088dae37456SAlex Vesker qpn = uqp->qpn;
1089ae288a48SMaor Gottlieb err = root->cmds->update_root_ft(root, ft,
1090af76c501SMatan Barak qpn, false);
10912cc43b49SMaor Gottlieb if (err)
1092dae37456SAlex Vesker break;
1093dae37456SAlex Vesker }
1094dae37456SAlex Vesker }
1095dae37456SAlex Vesker
1096dae37456SAlex Vesker if (err)
1097dae37456SAlex Vesker mlx5_core_warn(root->dev,
1098dae37456SAlex Vesker "Update root flow table of id(%u) qpn(%d) failed\n",
1099dae37456SAlex Vesker ft->id, qpn);
11002cc43b49SMaor Gottlieb else
11012cc43b49SMaor Gottlieb root->root_ft = ft;
11022cc43b49SMaor Gottlieb
11032cc43b49SMaor Gottlieb return err;
11042cc43b49SMaor Gottlieb }
11052cc43b49SMaor Gottlieb
_mlx5_modify_rule_destination(struct mlx5_flow_rule * rule,struct mlx5_flow_destination * dest)110674491de9SMark Bloch static int _mlx5_modify_rule_destination(struct mlx5_flow_rule *rule,
1107b3638e1aSMaor Gottlieb struct mlx5_flow_destination *dest)
1108b3638e1aSMaor Gottlieb {
1109af76c501SMatan Barak struct mlx5_flow_root_namespace *root;
1110b3638e1aSMaor Gottlieb struct mlx5_flow_table *ft;
1111b3638e1aSMaor Gottlieb struct mlx5_flow_group *fg;
1112b3638e1aSMaor Gottlieb struct fs_fte *fte;
1113bd5251dbSAmir Vadai int modify_mask = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
1114b3638e1aSMaor Gottlieb int err = 0;
1115b3638e1aSMaor Gottlieb
1116b3638e1aSMaor Gottlieb fs_get_obj(fte, rule->node.parent);
1117d2ec6a35SMatan Barak if (!(fte->action.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1118b3638e1aSMaor Gottlieb return -EINVAL;
1119476d61b7SEli Britstein down_write_ref_node(&fte->node, false);
1120b3638e1aSMaor Gottlieb fs_get_obj(fg, fte->node.parent);
1121b3638e1aSMaor Gottlieb fs_get_obj(ft, fg->node.parent);
1122b3638e1aSMaor Gottlieb
1123b3638e1aSMaor Gottlieb memcpy(&rule->dest_attr, dest, sizeof(*dest));
1124af76c501SMatan Barak root = find_root(&ft->node);
1125ae288a48SMaor Gottlieb err = root->cmds->update_fte(root, ft, fg,
1126af76c501SMatan Barak modify_mask, fte);
1127476d61b7SEli Britstein up_write_ref_node(&fte->node, false);
1128b3638e1aSMaor Gottlieb
1129b3638e1aSMaor Gottlieb return err;
1130b3638e1aSMaor Gottlieb }
1131b3638e1aSMaor Gottlieb
mlx5_modify_rule_destination(struct mlx5_flow_handle * handle,struct mlx5_flow_destination * new_dest,struct mlx5_flow_destination * old_dest)113274491de9SMark Bloch int mlx5_modify_rule_destination(struct mlx5_flow_handle *handle,
113374491de9SMark Bloch struct mlx5_flow_destination *new_dest,
113474491de9SMark Bloch struct mlx5_flow_destination *old_dest)
113574491de9SMark Bloch {
113674491de9SMark Bloch int i;
113774491de9SMark Bloch
113874491de9SMark Bloch if (!old_dest) {
113974491de9SMark Bloch if (handle->num_rules != 1)
114074491de9SMark Bloch return -EINVAL;
114174491de9SMark Bloch return _mlx5_modify_rule_destination(handle->rule[0],
114274491de9SMark Bloch new_dest);
114374491de9SMark Bloch }
114474491de9SMark Bloch
114574491de9SMark Bloch for (i = 0; i < handle->num_rules; i++) {
11461632649dSJianbo Liu if (mlx5_flow_dests_cmp(old_dest, &handle->rule[i]->dest_attr))
114774491de9SMark Bloch return _mlx5_modify_rule_destination(handle->rule[i],
114874491de9SMark Bloch new_dest);
114974491de9SMark Bloch }
115074491de9SMark Bloch
115174491de9SMark Bloch return -EINVAL;
115274491de9SMark Bloch }
115374491de9SMark Bloch
1154b3638e1aSMaor Gottlieb /* Modify/set FWD rules that point on old_next_ft to point on new_next_ft */
connect_fwd_rules(struct mlx5_core_dev * dev,struct mlx5_flow_table * new_next_ft,struct mlx5_flow_table * old_next_ft)1155b3638e1aSMaor Gottlieb static int connect_fwd_rules(struct mlx5_core_dev *dev,
1156b3638e1aSMaor Gottlieb struct mlx5_flow_table *new_next_ft,
1157b3638e1aSMaor Gottlieb struct mlx5_flow_table *old_next_ft)
1158b3638e1aSMaor Gottlieb {
11594c5009c5SRabie Loulou struct mlx5_flow_destination dest = {};
1160b3638e1aSMaor Gottlieb struct mlx5_flow_rule *iter;
1161b3638e1aSMaor Gottlieb int err = 0;
1162b3638e1aSMaor Gottlieb
1163b3638e1aSMaor Gottlieb /* new_next_ft and old_next_ft could be NULL only
1164b3638e1aSMaor Gottlieb * when we create/destroy the anchor flow table.
1165b3638e1aSMaor Gottlieb */
1166b3638e1aSMaor Gottlieb if (!new_next_ft || !old_next_ft)
1167b3638e1aSMaor Gottlieb return 0;
1168b3638e1aSMaor Gottlieb
1169b3638e1aSMaor Gottlieb dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1170b3638e1aSMaor Gottlieb dest.ft = new_next_ft;
1171b3638e1aSMaor Gottlieb
1172b3638e1aSMaor Gottlieb mutex_lock(&old_next_ft->lock);
1173b3638e1aSMaor Gottlieb list_splice_init(&old_next_ft->fwd_rules, &new_next_ft->fwd_rules);
1174b3638e1aSMaor Gottlieb mutex_unlock(&old_next_ft->lock);
1175b3638e1aSMaor Gottlieb list_for_each_entry(iter, &new_next_ft->fwd_rules, next_ft) {
11769254f8edSMaor Gottlieb if ((iter->sw_action & MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS) &&
11779254f8edSMaor Gottlieb iter->ft->ns == new_next_ft->ns)
11789254f8edSMaor Gottlieb continue;
11799254f8edSMaor Gottlieb
118074491de9SMark Bloch err = _mlx5_modify_rule_destination(iter, &dest);
1181b3638e1aSMaor Gottlieb if (err)
1182b3638e1aSMaor Gottlieb pr_err("mlx5_core: failed to modify rule to point on flow table %d\n",
1183b3638e1aSMaor Gottlieb new_next_ft->id);
1184b3638e1aSMaor Gottlieb }
1185b3638e1aSMaor Gottlieb return 0;
1186b3638e1aSMaor Gottlieb }
1187b3638e1aSMaor Gottlieb
connect_flow_table(struct mlx5_core_dev * dev,struct mlx5_flow_table * ft,struct fs_prio * prio)1188f90edfd2SMaor Gottlieb static int connect_flow_table(struct mlx5_core_dev *dev, struct mlx5_flow_table *ft,
1189f90edfd2SMaor Gottlieb struct fs_prio *prio)
1190f90edfd2SMaor Gottlieb {
11918b54874eSMaor Gottlieb struct mlx5_flow_table *next_ft, *first_ft;
1192f90edfd2SMaor Gottlieb int err = 0;
1193f90edfd2SMaor Gottlieb
1194f90edfd2SMaor Gottlieb /* Connect_prev_fts and update_root_ft_create are mutually exclusive */
1195f90edfd2SMaor Gottlieb
11968b54874eSMaor Gottlieb first_ft = list_first_entry_or_null(&prio->node.children,
11978b54874eSMaor Gottlieb struct mlx5_flow_table, node.list);
11988b54874eSMaor Gottlieb if (!first_ft || first_ft->level > ft->level) {
1199f90edfd2SMaor Gottlieb err = connect_prev_fts(dev, ft, prio);
1200f90edfd2SMaor Gottlieb if (err)
1201f90edfd2SMaor Gottlieb return err;
1202b3638e1aSMaor Gottlieb
1203618d28a5SJianbo Liu next_ft = first_ft ? first_ft : find_next_chained_ft(&prio->node);
1204b3638e1aSMaor Gottlieb err = connect_fwd_rules(dev, ft, next_ft);
1205b3638e1aSMaor Gottlieb if (err)
1206b3638e1aSMaor Gottlieb return err;
1207f90edfd2SMaor Gottlieb }
1208f90edfd2SMaor Gottlieb
1209f90edfd2SMaor Gottlieb if (MLX5_CAP_FLOWTABLE(dev,
1210f90edfd2SMaor Gottlieb flow_table_properties_nic_receive.modify_root))
1211f90edfd2SMaor Gottlieb err = update_root_ft_create(ft, prio);
1212f90edfd2SMaor Gottlieb return err;
1213f90edfd2SMaor Gottlieb }
1214f90edfd2SMaor Gottlieb
list_add_flow_table(struct mlx5_flow_table * ft,struct fs_prio * prio)1215d63cd286SMaor Gottlieb static void list_add_flow_table(struct mlx5_flow_table *ft,
1216d63cd286SMaor Gottlieb struct fs_prio *prio)
1217d63cd286SMaor Gottlieb {
1218d63cd286SMaor Gottlieb struct list_head *prev = &prio->node.children;
1219d63cd286SMaor Gottlieb struct mlx5_flow_table *iter;
1220d63cd286SMaor Gottlieb
1221d63cd286SMaor Gottlieb fs_for_each_ft(iter, prio) {
1222d63cd286SMaor Gottlieb if (iter->level > ft->level)
1223d63cd286SMaor Gottlieb break;
1224d63cd286SMaor Gottlieb prev = &iter->node.list;
1225d63cd286SMaor Gottlieb }
1226d63cd286SMaor Gottlieb list_add(&ft->node.list, prev);
1227d63cd286SMaor Gottlieb }
1228d63cd286SMaor Gottlieb
__mlx5_create_flow_table(struct mlx5_flow_namespace * ns,struct mlx5_flow_table_attr * ft_attr,enum fs_flow_table_op_mod op_mod,u16 vport)1229efdc810bSMohamad Haj Yahia static struct mlx5_flow_table *__mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1230b3ba5149SErez Shitrit struct mlx5_flow_table_attr *ft_attr,
1231aaff1beaSAviv Heller enum fs_flow_table_op_mod op_mod,
1232b3ba5149SErez Shitrit u16 vport)
12330c56b975SMaor Gottlieb {
1234b3ba5149SErez Shitrit struct mlx5_flow_root_namespace *root = find_root(&ns->node);
12355281a0c9SPaul Blakey bool unmanaged = ft_attr->flags & MLX5_FLOW_TABLE_UNMANAGED;
12365281a0c9SPaul Blakey struct mlx5_flow_table *next_ft;
12370c56b975SMaor Gottlieb struct fs_prio *fs_prio = NULL;
1238b3ba5149SErez Shitrit struct mlx5_flow_table *ft;
1239b3ba5149SErez Shitrit int err;
12400c56b975SMaor Gottlieb
12410c56b975SMaor Gottlieb if (!root) {
12420c56b975SMaor Gottlieb pr_err("mlx5: flow steering failed to find root of namespace\n");
12430c56b975SMaor Gottlieb return ERR_PTR(-ENODEV);
12440c56b975SMaor Gottlieb }
12450c56b975SMaor Gottlieb
12462cc43b49SMaor Gottlieb mutex_lock(&root->chain_lock);
1247b3ba5149SErez Shitrit fs_prio = find_prio(ns, ft_attr->prio);
12482cc43b49SMaor Gottlieb if (!fs_prio) {
12492cc43b49SMaor Gottlieb err = -EINVAL;
12502cc43b49SMaor Gottlieb goto unlock_root;
12512cc43b49SMaor Gottlieb }
12525281a0c9SPaul Blakey if (!unmanaged) {
12535281a0c9SPaul Blakey /* The level is related to the
12545281a0c9SPaul Blakey * priority level range.
12555281a0c9SPaul Blakey */
1256b3ba5149SErez Shitrit if (ft_attr->level >= fs_prio->num_levels) {
12570c56b975SMaor Gottlieb err = -ENOSPC;
12582cc43b49SMaor Gottlieb goto unlock_root;
12590c56b975SMaor Gottlieb }
12605281a0c9SPaul Blakey
12615281a0c9SPaul Blakey ft_attr->level += fs_prio->start_level;
12625281a0c9SPaul Blakey }
12635281a0c9SPaul Blakey
1264d63cd286SMaor Gottlieb /* The level is related to the
1265d63cd286SMaor Gottlieb * priority level range.
1266d63cd286SMaor Gottlieb */
1267b3ba5149SErez Shitrit ft = alloc_flow_table(ft_attr->level,
1268efdc810bSMohamad Haj Yahia vport,
1269aaff1beaSAviv Heller root->table_type,
1270b3ba5149SErez Shitrit op_mod, ft_attr->flags);
1271693c6883SMatan Barak if (IS_ERR(ft)) {
1272693c6883SMatan Barak err = PTR_ERR(ft);
12732cc43b49SMaor Gottlieb goto unlock_root;
12740c56b975SMaor Gottlieb }
12750c56b975SMaor Gottlieb
1276bd71b08eSMaor Gottlieb tree_init_node(&ft->node, del_hw_flow_table, del_sw_flow_table);
12775281a0c9SPaul Blakey next_ft = unmanaged ? ft_attr->next_ft :
1278618d28a5SJianbo Liu find_next_chained_ft(&fs_prio->node);
1279f66ad830SMark Zhang ft->def_miss_action = ns->def_miss_action;
12809254f8edSMaor Gottlieb ft->ns = ns;
1281b0bb369eSMark Bloch err = root->cmds->create_flow_table(root, ft, ft_attr, next_ft);
12820c56b975SMaor Gottlieb if (err)
12830c56b975SMaor Gottlieb goto free_ft;
12840c56b975SMaor Gottlieb
12855281a0c9SPaul Blakey if (!unmanaged) {
1286f90edfd2SMaor Gottlieb err = connect_flow_table(root->dev, ft, fs_prio);
12872cc43b49SMaor Gottlieb if (err)
12882cc43b49SMaor Gottlieb goto destroy_ft;
12895281a0c9SPaul Blakey }
12905281a0c9SPaul Blakey
129119f100feSMaor Gottlieb ft->node.active = true;
1292476d61b7SEli Britstein down_write_ref_node(&fs_prio->node, false);
12935281a0c9SPaul Blakey if (!unmanaged) {
12940c56b975SMaor Gottlieb tree_add_node(&ft->node, &fs_prio->node);
1295d63cd286SMaor Gottlieb list_add_flow_table(ft, fs_prio);
12965281a0c9SPaul Blakey } else {
12975281a0c9SPaul Blakey ft->node.root = fs_prio->node.root;
12985281a0c9SPaul Blakey }
12990c56b975SMaor Gottlieb fs_prio->num_ft++;
1300476d61b7SEli Britstein up_write_ref_node(&fs_prio->node, false);
13012cc43b49SMaor Gottlieb mutex_unlock(&root->chain_lock);
13028e4ca986SOr Gerlitz trace_mlx5_fs_add_ft(ft);
13030c56b975SMaor Gottlieb return ft;
13042cc43b49SMaor Gottlieb destroy_ft:
1305ae288a48SMaor Gottlieb root->cmds->destroy_flow_table(root, ft);
13060c56b975SMaor Gottlieb free_ft:
1307487c6ef8SRoi Dayan rhltable_destroy(&ft->fgs_hash);
13080c56b975SMaor Gottlieb kfree(ft);
13092cc43b49SMaor Gottlieb unlock_root:
13102cc43b49SMaor Gottlieb mutex_unlock(&root->chain_lock);
13110c56b975SMaor Gottlieb return ERR_PTR(err);
13120c56b975SMaor Gottlieb }
13130c56b975SMaor Gottlieb
mlx5_create_flow_table(struct mlx5_flow_namespace * ns,struct mlx5_flow_table_attr * ft_attr)1314efdc810bSMohamad Haj Yahia struct mlx5_flow_table *mlx5_create_flow_table(struct mlx5_flow_namespace *ns,
1315b3ba5149SErez Shitrit struct mlx5_flow_table_attr *ft_attr)
1316efdc810bSMohamad Haj Yahia {
1317b3ba5149SErez Shitrit return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, 0);
1318efdc810bSMohamad Haj Yahia }
13198d2a9d8dSEli Cohen EXPORT_SYMBOL(mlx5_create_flow_table);
1320efdc810bSMohamad Haj Yahia
mlx5_flow_table_id(struct mlx5_flow_table * ft)13216c27c56cSMark Bloch u32 mlx5_flow_table_id(struct mlx5_flow_table *ft)
13226c27c56cSMark Bloch {
13236c27c56cSMark Bloch return ft->id;
13246c27c56cSMark Bloch }
13256c27c56cSMark Bloch EXPORT_SYMBOL(mlx5_flow_table_id);
13266c27c56cSMark Bloch
1327617b860cSParav Pandit struct mlx5_flow_table *
mlx5_create_vport_flow_table(struct mlx5_flow_namespace * ns,struct mlx5_flow_table_attr * ft_attr,u16 vport)1328617b860cSParav Pandit mlx5_create_vport_flow_table(struct mlx5_flow_namespace *ns,
1329617b860cSParav Pandit struct mlx5_flow_table_attr *ft_attr, u16 vport)
1330efdc810bSMohamad Haj Yahia {
1331617b860cSParav Pandit return __mlx5_create_flow_table(ns, ft_attr, FS_FT_OP_MOD_NORMAL, vport);
1332efdc810bSMohamad Haj Yahia }
1333efdc810bSMohamad Haj Yahia
1334b3ba5149SErez Shitrit struct mlx5_flow_table*
mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace * ns,int prio,u32 level)1335b3ba5149SErez Shitrit mlx5_create_lag_demux_flow_table(struct mlx5_flow_namespace *ns,
1336aaff1beaSAviv Heller int prio, u32 level)
1337aaff1beaSAviv Heller {
1338b3ba5149SErez Shitrit struct mlx5_flow_table_attr ft_attr = {};
1339b3ba5149SErez Shitrit
1340b3ba5149SErez Shitrit ft_attr.level = level;
1341b3ba5149SErez Shitrit ft_attr.prio = prio;
13424a98544dSPaul Blakey ft_attr.max_fte = 1;
13434a98544dSPaul Blakey
1344b3ba5149SErez Shitrit return __mlx5_create_flow_table(ns, &ft_attr, FS_FT_OP_MOD_LAG_DEMUX, 0);
1345aaff1beaSAviv Heller }
1346aaff1beaSAviv Heller EXPORT_SYMBOL(mlx5_create_lag_demux_flow_table);
1347aaff1beaSAviv Heller
134871513c05SPaul Blakey #define MAX_FLOW_GROUP_SIZE BIT(24)
1349b3ba5149SErez Shitrit struct mlx5_flow_table*
mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace * ns,struct mlx5_flow_table_attr * ft_attr)1350b3ba5149SErez Shitrit mlx5_create_auto_grouped_flow_table(struct mlx5_flow_namespace *ns,
135161dc7b01SPaul Blakey struct mlx5_flow_table_attr *ft_attr)
1352f0d22d18SMaor Gottlieb {
135379cdb0aaSPaul Blakey int num_reserved_entries = ft_attr->autogroup.num_reserved_entries;
135479cdb0aaSPaul Blakey int max_num_groups = ft_attr->autogroup.max_num_groups;
1355f0d22d18SMaor Gottlieb struct mlx5_flow_table *ft;
13564a98544dSPaul Blakey int autogroups_max_fte;
1357f0d22d18SMaor Gottlieb
135861dc7b01SPaul Blakey ft = mlx5_create_flow_table(ns, ft_attr);
1359f0d22d18SMaor Gottlieb if (IS_ERR(ft))
1360f0d22d18SMaor Gottlieb return ft;
1361f0d22d18SMaor Gottlieb
13624a98544dSPaul Blakey autogroups_max_fte = ft->max_fte - num_reserved_entries;
13634a98544dSPaul Blakey if (max_num_groups > autogroups_max_fte)
13644a98544dSPaul Blakey goto err_validate;
13654a98544dSPaul Blakey if (num_reserved_entries > ft->max_fte)
13664a98544dSPaul Blakey goto err_validate;
13674a98544dSPaul Blakey
136871513c05SPaul Blakey /* Align the number of groups according to the largest group size */
136971513c05SPaul Blakey if (autogroups_max_fte / (max_num_groups + 1) > MAX_FLOW_GROUP_SIZE)
137071513c05SPaul Blakey max_num_groups = (autogroups_max_fte / MAX_FLOW_GROUP_SIZE) - 1;
137171513c05SPaul Blakey
1372f0d22d18SMaor Gottlieb ft->autogroup.active = true;
137379cdb0aaSPaul Blakey ft->autogroup.required_groups = max_num_groups;
137479cdb0aaSPaul Blakey ft->autogroup.max_fte = autogroups_max_fte;
137597fd8da2SMaor Gottlieb /* We save place for flow groups in addition to max types */
137679cdb0aaSPaul Blakey ft->autogroup.group_size = autogroups_max_fte / (max_num_groups + 1);
1377f0d22d18SMaor Gottlieb
1378f0d22d18SMaor Gottlieb return ft;
13794a98544dSPaul Blakey
13804a98544dSPaul Blakey err_validate:
13814a98544dSPaul Blakey mlx5_destroy_flow_table(ft);
13824a98544dSPaul Blakey return ERR_PTR(-ENOSPC);
1383f0d22d18SMaor Gottlieb }
1384b217ea25SMaor Gottlieb EXPORT_SYMBOL(mlx5_create_auto_grouped_flow_table);
1385f0d22d18SMaor Gottlieb
mlx5_create_flow_group(struct mlx5_flow_table * ft,u32 * fg_in)1386f0d22d18SMaor Gottlieb struct mlx5_flow_group *mlx5_create_flow_group(struct mlx5_flow_table *ft,
1387f0d22d18SMaor Gottlieb u32 *fg_in)
1388f0d22d18SMaor Gottlieb {
1389af76c501SMatan Barak struct mlx5_flow_root_namespace *root = find_root(&ft->node);
13900d235c3fSMatan Barak void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
13910d235c3fSMatan Barak fg_in, match_criteria);
13920d235c3fSMatan Barak u8 match_criteria_enable = MLX5_GET(create_flow_group_in,
13930d235c3fSMatan Barak fg_in,
13940d235c3fSMatan Barak match_criteria_enable);
139519f100feSMaor Gottlieb int start_index = MLX5_GET(create_flow_group_in, fg_in,
139619f100feSMaor Gottlieb start_flow_index);
139719f100feSMaor Gottlieb int end_index = MLX5_GET(create_flow_group_in, fg_in,
139819f100feSMaor Gottlieb end_flow_index);
1399f0d22d18SMaor Gottlieb struct mlx5_flow_group *fg;
140019f100feSMaor Gottlieb int err;
1401f0d22d18SMaor Gottlieb
140279cdb0aaSPaul Blakey if (ft->autogroup.active && start_index < ft->autogroup.max_fte)
1403f0d22d18SMaor Gottlieb return ERR_PTR(-EPERM);
1404f0d22d18SMaor Gottlieb
1405476d61b7SEli Britstein down_write_ref_node(&ft->node, false);
140619f100feSMaor Gottlieb fg = alloc_insert_flow_group(ft, match_criteria_enable, match_criteria,
140719f100feSMaor Gottlieb start_index, end_index,
140819f100feSMaor Gottlieb ft->node.children.prev);
1409476d61b7SEli Britstein up_write_ref_node(&ft->node, false);
141019f100feSMaor Gottlieb if (IS_ERR(fg))
141119f100feSMaor Gottlieb return fg;
141219f100feSMaor Gottlieb
1413ae288a48SMaor Gottlieb err = root->cmds->create_flow_group(root, ft, fg_in, fg);
141419f100feSMaor Gottlieb if (err) {
1415476d61b7SEli Britstein tree_put_node(&fg->node, false);
141619f100feSMaor Gottlieb return ERR_PTR(err);
141719f100feSMaor Gottlieb }
141819f100feSMaor Gottlieb trace_mlx5_fs_add_fg(fg);
141919f100feSMaor Gottlieb fg->node.active = true;
14200c56b975SMaor Gottlieb
14210c56b975SMaor Gottlieb return fg;
14220c56b975SMaor Gottlieb }
14238d2a9d8dSEli Cohen EXPORT_SYMBOL(mlx5_create_flow_group);
14240c56b975SMaor Gottlieb
alloc_rule(struct mlx5_flow_destination * dest)14250c56b975SMaor Gottlieb static struct mlx5_flow_rule *alloc_rule(struct mlx5_flow_destination *dest)
14260c56b975SMaor Gottlieb {
14270c56b975SMaor Gottlieb struct mlx5_flow_rule *rule;
14280c56b975SMaor Gottlieb
14290c56b975SMaor Gottlieb rule = kzalloc(sizeof(*rule), GFP_KERNEL);
14300c56b975SMaor Gottlieb if (!rule)
14310c56b975SMaor Gottlieb return NULL;
14320c56b975SMaor Gottlieb
1433b3638e1aSMaor Gottlieb INIT_LIST_HEAD(&rule->next_ft);
14340c56b975SMaor Gottlieb rule->node.type = FS_TYPE_FLOW_DEST;
143560ab4584SAmir Vadai if (dest)
14360c56b975SMaor Gottlieb memcpy(&rule->dest_attr, dest, sizeof(*dest));
14376510bc0dSMark Bloch else
14386510bc0dSMark Bloch rule->dest_attr.type = MLX5_FLOW_DESTINATION_TYPE_NONE;
14390c56b975SMaor Gottlieb
14400c56b975SMaor Gottlieb return rule;
14410c56b975SMaor Gottlieb }
14420c56b975SMaor Gottlieb
alloc_handle(int num_rules)144374491de9SMark Bloch static struct mlx5_flow_handle *alloc_handle(int num_rules)
14440c56b975SMaor Gottlieb {
144574491de9SMark Bloch struct mlx5_flow_handle *handle;
14460c56b975SMaor Gottlieb
1447acafe7e3SKees Cook handle = kzalloc(struct_size(handle, rule, num_rules), GFP_KERNEL);
144874491de9SMark Bloch if (!handle)
144974491de9SMark Bloch return NULL;
145074491de9SMark Bloch
145174491de9SMark Bloch handle->num_rules = num_rules;
145274491de9SMark Bloch
145374491de9SMark Bloch return handle;
145474491de9SMark Bloch }
145574491de9SMark Bloch
destroy_flow_handle(struct fs_fte * fte,struct mlx5_flow_handle * handle,struct mlx5_flow_destination * dest,int i)145674491de9SMark Bloch static void destroy_flow_handle(struct fs_fte *fte,
145774491de9SMark Bloch struct mlx5_flow_handle *handle,
145874491de9SMark Bloch struct mlx5_flow_destination *dest,
145974491de9SMark Bloch int i)
146074491de9SMark Bloch {
146174491de9SMark Bloch for (; --i >= 0;) {
1462dd8e1945SElena Reshetova if (refcount_dec_and_test(&handle->rule[i]->node.refcount)) {
146374491de9SMark Bloch fte->dests_size--;
146474491de9SMark Bloch list_del(&handle->rule[i]->node.list);
146574491de9SMark Bloch kfree(handle->rule[i]);
146674491de9SMark Bloch }
146774491de9SMark Bloch }
146874491de9SMark Bloch kfree(handle);
146974491de9SMark Bloch }
147074491de9SMark Bloch
147174491de9SMark Bloch static struct mlx5_flow_handle *
create_flow_handle(struct fs_fte * fte,struct mlx5_flow_destination * dest,int dest_num,int * modify_mask,bool * new_rule)147274491de9SMark Bloch create_flow_handle(struct fs_fte *fte,
147374491de9SMark Bloch struct mlx5_flow_destination *dest,
147474491de9SMark Bloch int dest_num,
147574491de9SMark Bloch int *modify_mask,
147674491de9SMark Bloch bool *new_rule)
147774491de9SMark Bloch {
147874491de9SMark Bloch struct mlx5_flow_handle *handle;
147974491de9SMark Bloch struct mlx5_flow_rule *rule = NULL;
148074491de9SMark Bloch static int count = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_FLOW_COUNTERS);
148174491de9SMark Bloch static int dst = BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_DESTINATION_LIST);
148274491de9SMark Bloch int type;
148374491de9SMark Bloch int i = 0;
148474491de9SMark Bloch
148574491de9SMark Bloch handle = alloc_handle((dest_num) ? dest_num : 1);
148674491de9SMark Bloch if (!handle)
14870c56b975SMaor Gottlieb return ERR_PTR(-ENOMEM);
14880c56b975SMaor Gottlieb
148974491de9SMark Bloch do {
149074491de9SMark Bloch if (dest) {
149174491de9SMark Bloch rule = find_flow_rule(fte, dest + i);
149274491de9SMark Bloch if (rule) {
1493dd8e1945SElena Reshetova refcount_inc(&rule->node.refcount);
149474491de9SMark Bloch goto rule_found;
149574491de9SMark Bloch }
149674491de9SMark Bloch }
1497a6224985SMark Bloch
149874491de9SMark Bloch *new_rule = true;
149974491de9SMark Bloch rule = alloc_rule(dest + i);
150074491de9SMark Bloch if (!rule)
150174491de9SMark Bloch goto free_rules;
150274491de9SMark Bloch
1503b3638e1aSMaor Gottlieb /* Add dest to dests list- we need flow tables to be in the
1504b3638e1aSMaor Gottlieb * end of the list for forward to next prio rules.
1505b3638e1aSMaor Gottlieb */
1506bd71b08eSMaor Gottlieb tree_init_node(&rule->node, NULL, del_sw_hw_rule);
150774491de9SMark Bloch if (dest &&
150874491de9SMark Bloch dest[i].type != MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE)
1509b3638e1aSMaor Gottlieb list_add(&rule->node.list, &fte->node.children);
1510b3638e1aSMaor Gottlieb else
15110c56b975SMaor Gottlieb list_add_tail(&rule->node.list, &fte->node.children);
1512bd5251dbSAmir Vadai if (dest) {
15130c56b975SMaor Gottlieb fte->dests_size++;
1514bd5251dbSAmir Vadai
1515a30c8b90SMark Bloch if (is_fwd_dest_type(dest[i].type))
1516a30c8b90SMark Bloch fte->fwd_dests++;
1517a30c8b90SMark Bloch
151874491de9SMark Bloch type = dest[i].type ==
151974491de9SMark Bloch MLX5_FLOW_DESTINATION_TYPE_COUNTER;
152074491de9SMark Bloch *modify_mask |= type ? count : dst;
152174491de9SMark Bloch }
152274491de9SMark Bloch rule_found:
152374491de9SMark Bloch handle->rule[i] = rule;
152474491de9SMark Bloch } while (++i < dest_num);
152574491de9SMark Bloch
152674491de9SMark Bloch return handle;
152774491de9SMark Bloch
152874491de9SMark Bloch free_rules:
152974491de9SMark Bloch destroy_flow_handle(fte, handle, dest, i);
153074491de9SMark Bloch return ERR_PTR(-ENOMEM);
1531bd5251dbSAmir Vadai }
1532bd5251dbSAmir Vadai
153374491de9SMark Bloch /* fte should not be deleted while calling this function */
153474491de9SMark Bloch static struct mlx5_flow_handle *
add_rule_fte(struct fs_fte * fte,struct mlx5_flow_group * fg,struct mlx5_flow_destination * dest,int dest_num,bool update_action)153574491de9SMark Bloch add_rule_fte(struct fs_fte *fte,
153674491de9SMark Bloch struct mlx5_flow_group *fg,
153774491de9SMark Bloch struct mlx5_flow_destination *dest,
153874491de9SMark Bloch int dest_num,
153974491de9SMark Bloch bool update_action)
154074491de9SMark Bloch {
1541af76c501SMatan Barak struct mlx5_flow_root_namespace *root;
154274491de9SMark Bloch struct mlx5_flow_handle *handle;
154374491de9SMark Bloch struct mlx5_flow_table *ft;
154474491de9SMark Bloch int modify_mask = 0;
154574491de9SMark Bloch int err;
154674491de9SMark Bloch bool new_rule = false;
154774491de9SMark Bloch
154874491de9SMark Bloch handle = create_flow_handle(fte, dest, dest_num, &modify_mask,
154974491de9SMark Bloch &new_rule);
155074491de9SMark Bloch if (IS_ERR(handle) || !new_rule)
155174491de9SMark Bloch goto out;
155274491de9SMark Bloch
155374491de9SMark Bloch if (update_action)
155474491de9SMark Bloch modify_mask |= BIT(MLX5_SET_FTE_MODIFY_ENABLE_MASK_ACTION);
155574491de9SMark Bloch
155674491de9SMark Bloch fs_get_obj(ft, fg->node.parent);
1557af76c501SMatan Barak root = find_root(&fg->node);
15580501fc47SMark Bloch if (!(fte->status & FS_FTE_STATUS_EXISTING))
1559ae288a48SMaor Gottlieb err = root->cmds->create_fte(root, ft, fg, fte);
15600c56b975SMaor Gottlieb else
1561ae288a48SMaor Gottlieb err = root->cmds->update_fte(root, ft, fg, modify_mask, fte);
15620c56b975SMaor Gottlieb if (err)
156374491de9SMark Bloch goto free_handle;
15640c56b975SMaor Gottlieb
156519f100feSMaor Gottlieb fte->node.active = true;
15660c56b975SMaor Gottlieb fte->status |= FS_FTE_STATUS_EXISTING;
1567454401aeSEli Cohen atomic_inc(&fg->node.version);
15680c56b975SMaor Gottlieb
156974491de9SMark Bloch out:
157074491de9SMark Bloch return handle;
15710c56b975SMaor Gottlieb
157274491de9SMark Bloch free_handle:
157374491de9SMark Bloch destroy_flow_handle(fte, handle, dest, handle->num_rules);
15740c56b975SMaor Gottlieb return ERR_PTR(err);
15750c56b975SMaor Gottlieb }
15760c56b975SMaor Gottlieb
alloc_auto_flow_group(struct mlx5_flow_table * ft,const struct mlx5_flow_spec * spec)157719f100feSMaor Gottlieb static struct mlx5_flow_group *alloc_auto_flow_group(struct mlx5_flow_table *ft,
15785233794bSArnd Bergmann const struct mlx5_flow_spec *spec)
15790c56b975SMaor Gottlieb {
1580af363705SPaul Blakey struct list_head *prev = &ft->node.children;
158179cdb0aaSPaul Blakey u32 max_fte = ft->autogroup.max_fte;
158219f100feSMaor Gottlieb unsigned int candidate_index = 0;
1583f0d22d18SMaor Gottlieb unsigned int group_size = 0;
158479cdb0aaSPaul Blakey struct mlx5_flow_group *fg;
1585f0d22d18SMaor Gottlieb
1586f0d22d18SMaor Gottlieb if (!ft->autogroup.active)
1587f0d22d18SMaor Gottlieb return ERR_PTR(-ENOENT);
1588f0d22d18SMaor Gottlieb
1589f0d22d18SMaor Gottlieb if (ft->autogroup.num_groups < ft->autogroup.required_groups)
159097fd8da2SMaor Gottlieb group_size = ft->autogroup.group_size;
1591f0d22d18SMaor Gottlieb
159279cdb0aaSPaul Blakey /* max_fte == ft->autogroup.max_types */
1593f0d22d18SMaor Gottlieb if (group_size == 0)
1594f0d22d18SMaor Gottlieb group_size = 1;
1595f0d22d18SMaor Gottlieb
1596f0d22d18SMaor Gottlieb /* sorted by start_index */
1597f0d22d18SMaor Gottlieb fs_for_each_fg(fg, ft) {
1598f0d22d18SMaor Gottlieb if (candidate_index + group_size > fg->start_index)
1599f0d22d18SMaor Gottlieb candidate_index = fg->start_index + fg->max_ftes;
1600f0d22d18SMaor Gottlieb else
1601f0d22d18SMaor Gottlieb break;
1602f0d22d18SMaor Gottlieb prev = &fg->node.list;
1603f0d22d18SMaor Gottlieb }
1604f0d22d18SMaor Gottlieb
160579cdb0aaSPaul Blakey if (candidate_index + group_size > max_fte)
160619f100feSMaor Gottlieb return ERR_PTR(-ENOSPC);
160719f100feSMaor Gottlieb
160819f100feSMaor Gottlieb fg = alloc_insert_flow_group(ft,
160919f100feSMaor Gottlieb spec->match_criteria_enable,
161019f100feSMaor Gottlieb spec->match_criteria,
161119f100feSMaor Gottlieb candidate_index,
161219f100feSMaor Gottlieb candidate_index + group_size - 1,
161319f100feSMaor Gottlieb prev);
161419f100feSMaor Gottlieb if (IS_ERR(fg))
1615f0d22d18SMaor Gottlieb goto out;
161619f100feSMaor Gottlieb
161797fd8da2SMaor Gottlieb if (group_size == ft->autogroup.group_size)
161819f100feSMaor Gottlieb ft->autogroup.num_groups++;
161919f100feSMaor Gottlieb
162019f100feSMaor Gottlieb out:
162119f100feSMaor Gottlieb return fg;
1622f0d22d18SMaor Gottlieb }
1623f0d22d18SMaor Gottlieb
create_auto_flow_group(struct mlx5_flow_table * ft,struct mlx5_flow_group * fg)162419f100feSMaor Gottlieb static int create_auto_flow_group(struct mlx5_flow_table *ft,
162519f100feSMaor Gottlieb struct mlx5_flow_group *fg)
162619f100feSMaor Gottlieb {
1627af76c501SMatan Barak struct mlx5_flow_root_namespace *root = find_root(&ft->node);
162819f100feSMaor Gottlieb int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
162919f100feSMaor Gottlieb void *match_criteria_addr;
16303e99df87SShahar Klein u8 src_esw_owner_mask_on;
16313e99df87SShahar Klein void *misc;
163219f100feSMaor Gottlieb int err;
163319f100feSMaor Gottlieb u32 *in;
163419f100feSMaor Gottlieb
163519f100feSMaor Gottlieb in = kvzalloc(inlen, GFP_KERNEL);
163619f100feSMaor Gottlieb if (!in)
163719f100feSMaor Gottlieb return -ENOMEM;
163819f100feSMaor Gottlieb
1639f0d22d18SMaor Gottlieb MLX5_SET(create_flow_group_in, in, match_criteria_enable,
164019f100feSMaor Gottlieb fg->mask.match_criteria_enable);
164119f100feSMaor Gottlieb MLX5_SET(create_flow_group_in, in, start_flow_index, fg->start_index);
164219f100feSMaor Gottlieb MLX5_SET(create_flow_group_in, in, end_flow_index, fg->start_index +
164319f100feSMaor Gottlieb fg->max_ftes - 1);
16443e99df87SShahar Klein
16453e99df87SShahar Klein misc = MLX5_ADDR_OF(fte_match_param, fg->mask.match_criteria,
16463e99df87SShahar Klein misc_parameters);
16473e99df87SShahar Klein src_esw_owner_mask_on = !!MLX5_GET(fte_match_set_misc, misc,
16483e99df87SShahar Klein source_eswitch_owner_vhca_id);
16493e99df87SShahar Klein MLX5_SET(create_flow_group_in, in,
16503e99df87SShahar Klein source_eswitch_owner_vhca_id_valid, src_esw_owner_mask_on);
16513e99df87SShahar Klein
1652f0d22d18SMaor Gottlieb match_criteria_addr = MLX5_ADDR_OF(create_flow_group_in,
1653f0d22d18SMaor Gottlieb in, match_criteria);
165419f100feSMaor Gottlieb memcpy(match_criteria_addr, fg->mask.match_criteria,
165519f100feSMaor Gottlieb sizeof(fg->mask.match_criteria));
1656f0d22d18SMaor Gottlieb
1657ae288a48SMaor Gottlieb err = root->cmds->create_flow_group(root, ft, in, fg);
165819f100feSMaor Gottlieb if (!err) {
165919f100feSMaor Gottlieb fg->node.active = true;
166019f100feSMaor Gottlieb trace_mlx5_fs_add_fg(fg);
166119f100feSMaor Gottlieb }
166219f100feSMaor Gottlieb
1663f0d22d18SMaor Gottlieb kvfree(in);
166419f100feSMaor Gottlieb return err;
1665f0d22d18SMaor Gottlieb }
1666f0d22d18SMaor Gottlieb
mlx5_pkt_reformat_cmp(struct mlx5_pkt_reformat * p1,struct mlx5_pkt_reformat * p2)1667690e3d91SCosmin Ratiu static bool mlx5_pkt_reformat_cmp(struct mlx5_pkt_reformat *p1,
1668690e3d91SCosmin Ratiu struct mlx5_pkt_reformat *p2)
1669690e3d91SCosmin Ratiu {
1670690e3d91SCosmin Ratiu return p1->owner == p2->owner &&
1671690e3d91SCosmin Ratiu (p1->owner == MLX5_FLOW_RESOURCE_OWNER_FW ?
1672690e3d91SCosmin Ratiu p1->id == p2->id :
1673690e3d91SCosmin Ratiu mlx5_fs_dr_action_get_pkt_reformat_id(p1) ==
1674690e3d91SCosmin Ratiu mlx5_fs_dr_action_get_pkt_reformat_id(p2));
1675690e3d91SCosmin Ratiu }
1676690e3d91SCosmin Ratiu
mlx5_flow_dests_cmp(struct mlx5_flow_destination * d1,struct mlx5_flow_destination * d2)1677814fb875SMark Bloch static bool mlx5_flow_dests_cmp(struct mlx5_flow_destination *d1,
1678814fb875SMark Bloch struct mlx5_flow_destination *d2)
1679814fb875SMark Bloch {
1680814fb875SMark Bloch if (d1->type == d2->type) {
1681e3a0f40bSYevgeny Kliteynik if (((d1->type == MLX5_FLOW_DESTINATION_TYPE_VPORT ||
1682e3a0f40bSYevgeny Kliteynik d1->type == MLX5_FLOW_DESTINATION_TYPE_UPLINK) &&
16831228e912SEli Britstein d1->vport.num == d2->vport.num &&
16841228e912SEli Britstein d1->vport.flags == d2->vport.flags &&
1685c979c445SDmytro Linkin ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_VHCA_ID) ?
1686c979c445SDmytro Linkin (d1->vport.vhca_id == d2->vport.vhca_id) : true) &&
16871228e912SEli Britstein ((d1->vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID) ?
1688690e3d91SCosmin Ratiu mlx5_pkt_reformat_cmp(d1->vport.pkt_reformat,
1689690e3d91SCosmin Ratiu d2->vport.pkt_reformat) : true)) ||
1690814fb875SMark Bloch (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1691814fb875SMark Bloch d1->ft == d2->ft) ||
1692814fb875SMark Bloch (d1->type == MLX5_FLOW_DESTINATION_TYPE_TIR &&
1693664000b6SYishai Hadas d1->tir_num == d2->tir_num) ||
1694664000b6SYishai Hadas (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM &&
16956f851556SYevgeny Kliteynik d1->ft_num == d2->ft_num) ||
16966f851556SYevgeny Kliteynik (d1->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER &&
169738bf24c3SYevgeny Kliteynik d1->sampler_id == d2->sampler_id) ||
169838bf24c3SYevgeny Kliteynik (d1->type == MLX5_FLOW_DESTINATION_TYPE_RANGE &&
169938bf24c3SYevgeny Kliteynik d1->range.field == d2->range.field &&
170038bf24c3SYevgeny Kliteynik d1->range.hit_ft == d2->range.hit_ft &&
170138bf24c3SYevgeny Kliteynik d1->range.miss_ft == d2->range.miss_ft &&
170238bf24c3SYevgeny Kliteynik d1->range.min == d2->range.min &&
170338bf24c3SYevgeny Kliteynik d1->range.max == d2->range.max))
1704814fb875SMark Bloch return true;
1705814fb875SMark Bloch }
1706814fb875SMark Bloch
1707814fb875SMark Bloch return false;
1708814fb875SMark Bloch }
1709814fb875SMark Bloch
find_flow_rule(struct fs_fte * fte,struct mlx5_flow_destination * dest)1710b3638e1aSMaor Gottlieb static struct mlx5_flow_rule *find_flow_rule(struct fs_fte *fte,
1711b3638e1aSMaor Gottlieb struct mlx5_flow_destination *dest)
1712b3638e1aSMaor Gottlieb {
1713b3638e1aSMaor Gottlieb struct mlx5_flow_rule *rule;
1714b3638e1aSMaor Gottlieb
1715b3638e1aSMaor Gottlieb list_for_each_entry(rule, &fte->node.children, node.list) {
1716814fb875SMark Bloch if (mlx5_flow_dests_cmp(&rule->dest_attr, dest))
1717b3638e1aSMaor Gottlieb return rule;
1718b3638e1aSMaor Gottlieb }
1719b3638e1aSMaor Gottlieb return NULL;
1720b3638e1aSMaor Gottlieb }
1721b3638e1aSMaor Gottlieb
check_conflicting_actions_vlan(const struct mlx5_fs_vlan * vlan0,const struct mlx5_fs_vlan * vlan1)17228fa5e7b2SMark Bloch static bool check_conflicting_actions_vlan(const struct mlx5_fs_vlan *vlan0,
17238fa5e7b2SMark Bloch const struct mlx5_fs_vlan *vlan1)
17240c56b975SMaor Gottlieb {
17258fa5e7b2SMark Bloch return vlan0->ethtype != vlan1->ethtype ||
17268fa5e7b2SMark Bloch vlan0->vid != vlan1->vid ||
17278fa5e7b2SMark Bloch vlan0->prio != vlan1->prio;
17288fa5e7b2SMark Bloch }
17298fa5e7b2SMark Bloch
check_conflicting_actions(const struct mlx5_flow_act * act1,const struct mlx5_flow_act * act2)17308fa5e7b2SMark Bloch static bool check_conflicting_actions(const struct mlx5_flow_act *act1,
17318fa5e7b2SMark Bloch const struct mlx5_flow_act *act2)
17328fa5e7b2SMark Bloch {
17338fa5e7b2SMark Bloch u32 action1 = act1->action;
17348fa5e7b2SMark Bloch u32 action2 = act2->action;
17358fa5e7b2SMark Bloch u32 xored_actions;
17368fa5e7b2SMark Bloch
17378fa5e7b2SMark Bloch xored_actions = action1 ^ action2;
17380c56b975SMaor Gottlieb
17390d235c3fSMatan Barak /* if one rule only wants to count, it's ok */
17400d235c3fSMatan Barak if (action1 == MLX5_FLOW_CONTEXT_ACTION_COUNT ||
17410d235c3fSMatan Barak action2 == MLX5_FLOW_CONTEXT_ACTION_COUNT)
17420d235c3fSMatan Barak return false;
17430d235c3fSMatan Barak
17440d235c3fSMatan Barak if (xored_actions & (MLX5_FLOW_CONTEXT_ACTION_DROP |
174560786f09SMark Bloch MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT |
174696de67a7SOr Gerlitz MLX5_FLOW_CONTEXT_ACTION_DECAP |
17470c06897aSOr Gerlitz MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
17480c06897aSOr Gerlitz MLX5_FLOW_CONTEXT_ACTION_VLAN_POP |
17498da6fe2aSJianbo Liu MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH |
17508da6fe2aSJianbo Liu MLX5_FLOW_CONTEXT_ACTION_VLAN_POP_2 |
17518da6fe2aSJianbo Liu MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2))
17520d235c3fSMatan Barak return true;
17530d235c3fSMatan Barak
17548fa5e7b2SMark Bloch if (action1 & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT &&
17558fa5e7b2SMark Bloch act1->pkt_reformat != act2->pkt_reformat)
17568fa5e7b2SMark Bloch return true;
17578fa5e7b2SMark Bloch
17588fa5e7b2SMark Bloch if (action1 & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR &&
17598fa5e7b2SMark Bloch act1->modify_hdr != act2->modify_hdr)
17608fa5e7b2SMark Bloch return true;
17618fa5e7b2SMark Bloch
17628fa5e7b2SMark Bloch if (action1 & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH &&
17638fa5e7b2SMark Bloch check_conflicting_actions_vlan(&act1->vlan[0], &act2->vlan[0]))
17648fa5e7b2SMark Bloch return true;
17658fa5e7b2SMark Bloch
17668fa5e7b2SMark Bloch if (action1 & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2 &&
17678fa5e7b2SMark Bloch check_conflicting_actions_vlan(&act1->vlan[1], &act2->vlan[1]))
17688fa5e7b2SMark Bloch return true;
17698fa5e7b2SMark Bloch
17700d235c3fSMatan Barak return false;
17710d235c3fSMatan Barak }
17720d235c3fSMatan Barak
check_conflicting_ftes(struct fs_fte * fte,const struct mlx5_flow_context * flow_context,const struct mlx5_flow_act * flow_act)1773bb0ee7dcSJianbo Liu static int check_conflicting_ftes(struct fs_fte *fte,
1774bb0ee7dcSJianbo Liu const struct mlx5_flow_context *flow_context,
1775bb0ee7dcSJianbo Liu const struct mlx5_flow_act *flow_act)
17760d235c3fSMatan Barak {
17778fa5e7b2SMark Bloch if (check_conflicting_actions(flow_act, &fte->action)) {
17780d235c3fSMatan Barak mlx5_core_warn(get_dev(&fte->node),
17790d235c3fSMatan Barak "Found two FTEs with conflicting actions\n");
17800d235c3fSMatan Barak return -EEXIST;
17810d235c3fSMatan Barak }
1782a6224985SMark Bloch
1783bb0ee7dcSJianbo Liu if ((flow_context->flags & FLOW_CONTEXT_HAS_TAG) &&
1784bb0ee7dcSJianbo Liu fte->flow_context.flow_tag != flow_context->flow_tag) {
1785e04a0183SMaor Gottlieb mlx5_core_warn(get_dev(&fte->node),
1786e04a0183SMaor Gottlieb "FTE flow tag %u already exists with different flow tag %u\n",
1787bb0ee7dcSJianbo Liu fte->flow_context.flow_tag,
1788bb0ee7dcSJianbo Liu flow_context->flow_tag);
17890d235c3fSMatan Barak return -EEXIST;
17900d235c3fSMatan Barak }
17910d235c3fSMatan Barak
17920d235c3fSMatan Barak return 0;
17930d235c3fSMatan Barak }
17940d235c3fSMatan Barak
add_rule_fg(struct mlx5_flow_group * fg,const struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act,struct mlx5_flow_destination * dest,int dest_num,struct fs_fte * fte)17950d235c3fSMatan Barak static struct mlx5_flow_handle *add_rule_fg(struct mlx5_flow_group *fg,
17965233794bSArnd Bergmann const struct mlx5_flow_spec *spec,
17970d235c3fSMatan Barak struct mlx5_flow_act *flow_act,
17980d235c3fSMatan Barak struct mlx5_flow_destination *dest,
1799693c6883SMatan Barak int dest_num,
1800693c6883SMatan Barak struct fs_fte *fte)
18010d235c3fSMatan Barak {
18020d235c3fSMatan Barak struct mlx5_flow_handle *handle;
18030d235c3fSMatan Barak int old_action;
1804bd71b08eSMaor Gottlieb int i;
18050d235c3fSMatan Barak int ret;
18060d235c3fSMatan Barak
1807bb0ee7dcSJianbo Liu ret = check_conflicting_ftes(fte, &spec->flow_context, flow_act);
1808bd71b08eSMaor Gottlieb if (ret)
1809bd71b08eSMaor Gottlieb return ERR_PTR(ret);
1810e04a0183SMaor Gottlieb
1811d2ec6a35SMatan Barak old_action = fte->action.action;
1812d2ec6a35SMatan Barak fte->action.action |= flow_act->action;
181374491de9SMark Bloch handle = add_rule_fte(fte, fg, dest, dest_num,
181466958ed9SHadar Hen Zion old_action != flow_act->action);
181574491de9SMark Bloch if (IS_ERR(handle)) {
1816d2ec6a35SMatan Barak fte->action.action = old_action;
1817693c6883SMatan Barak return handle;
18180c56b975SMaor Gottlieb }
1819bd71b08eSMaor Gottlieb trace_mlx5_fs_set_fte(fte, false);
18200c56b975SMaor Gottlieb
18215cf5337eSCosmin Ratiu /* Link newly added rules into the tree. */
182274491de9SMark Bloch for (i = 0; i < handle->num_rules; i++) {
18235cf5337eSCosmin Ratiu if (!handle->rule[i]->node.parent) {
182474491de9SMark Bloch tree_add_node(&handle->rule[i]->node, &fte->node);
18254c03e69aSMatan Barak trace_mlx5_fs_add_rule(handle->rule[i]);
18264c03e69aSMatan Barak }
182774491de9SMark Bloch }
182874491de9SMark Bloch return handle;
18290c56b975SMaor Gottlieb }
18300c56b975SMaor Gottlieb
counter_is_valid(u32 action)1831171c7625SMark Bloch static bool counter_is_valid(u32 action)
1832bd5251dbSAmir Vadai {
1833ae058314SMark Bloch return (action & (MLX5_FLOW_CONTEXT_ACTION_DROP |
18344c2573e1SMichael Guralnik MLX5_FLOW_CONTEXT_ACTION_ALLOW |
1835eafa6abdSDan Carpenter MLX5_FLOW_CONTEXT_ACTION_FWD_DEST));
1836bd5251dbSAmir Vadai }
1837bd5251dbSAmir Vadai
dest_is_valid(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_flow_table * ft)1838d63cd286SMaor Gottlieb static bool dest_is_valid(struct mlx5_flow_destination *dest,
1839ff189b43SPaul Blakey struct mlx5_flow_act *flow_act,
1840d63cd286SMaor Gottlieb struct mlx5_flow_table *ft)
1841d63cd286SMaor Gottlieb {
1842ff189b43SPaul Blakey bool ignore_level = flow_act->flags & FLOW_ACT_IGNORE_FLOW_LEVEL;
1843ff189b43SPaul Blakey u32 action = flow_act->action;
1844ff189b43SPaul Blakey
1845bd5251dbSAmir Vadai if (dest && (dest->type == MLX5_FLOW_DESTINATION_TYPE_COUNTER))
1846171c7625SMark Bloch return counter_is_valid(action);
1847bd5251dbSAmir Vadai
1848d63cd286SMaor Gottlieb if (!(action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST))
1849d63cd286SMaor Gottlieb return true;
1850d63cd286SMaor Gottlieb
1851ff189b43SPaul Blakey if (ignore_level) {
1852006f623eSAriel Levkovich if (ft->type != FS_FT_FDB &&
1853664eab8aSPaul Blakey ft->type != FS_FT_NIC_RX &&
1854664eab8aSPaul Blakey ft->type != FS_FT_NIC_TX)
1855ff189b43SPaul Blakey return false;
1856ff189b43SPaul Blakey
1857ff189b43SPaul Blakey if (dest->type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE &&
1858006f623eSAriel Levkovich ft->type != dest->ft->type)
1859ff189b43SPaul Blakey return false;
1860ff189b43SPaul Blakey }
1861ff189b43SPaul Blakey
1862d63cd286SMaor Gottlieb if (!dest || ((dest->type ==
1863d63cd286SMaor Gottlieb MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) &&
1864ff189b43SPaul Blakey (dest->ft->level <= ft->level && !ignore_level)))
1865d63cd286SMaor Gottlieb return false;
1866d63cd286SMaor Gottlieb return true;
1867d63cd286SMaor Gottlieb }
1868d63cd286SMaor Gottlieb
186946719d77SMaor Gottlieb struct match_list {
187046719d77SMaor Gottlieb struct list_head list;
187146719d77SMaor Gottlieb struct mlx5_flow_group *g;
187246719d77SMaor Gottlieb };
187346719d77SMaor Gottlieb
free_match_list(struct match_list * head,bool ft_locked)1874b820ce00SEli Cohen static void free_match_list(struct match_list *head, bool ft_locked)
187546719d77SMaor Gottlieb {
187646719d77SMaor Gottlieb struct match_list *iter, *match_tmp;
187746719d77SMaor Gottlieb
187846719d77SMaor Gottlieb list_for_each_entry_safe(iter, match_tmp, &head->list,
187946719d77SMaor Gottlieb list) {
1880c1948390SMaor Gottlieb tree_put_node(&iter->g->node, ft_locked);
188146719d77SMaor Gottlieb list_del(&iter->list);
188246719d77SMaor Gottlieb kfree(iter);
188346719d77SMaor Gottlieb }
188446719d77SMaor Gottlieb }
188546719d77SMaor Gottlieb
build_match_list(struct match_list * match_head,struct mlx5_flow_table * ft,const struct mlx5_flow_spec * spec,struct mlx5_flow_group * fg,bool ft_locked)1886b820ce00SEli Cohen static int build_match_list(struct match_list *match_head,
188746719d77SMaor Gottlieb struct mlx5_flow_table *ft,
1888c1948390SMaor Gottlieb const struct mlx5_flow_spec *spec,
1889c2c922daSMark Bloch struct mlx5_flow_group *fg,
1890c1948390SMaor Gottlieb bool ft_locked)
189146719d77SMaor Gottlieb {
189246719d77SMaor Gottlieb struct rhlist_head *tmp, *list;
189346719d77SMaor Gottlieb struct mlx5_flow_group *g;
189446719d77SMaor Gottlieb
189546719d77SMaor Gottlieb rcu_read_lock();
189646719d77SMaor Gottlieb INIT_LIST_HEAD(&match_head->list);
189746719d77SMaor Gottlieb /* Collect all fgs which has a matching match_criteria */
189846719d77SMaor Gottlieb list = rhltable_lookup(&ft->fgs_hash, spec, rhash_fg);
189946719d77SMaor Gottlieb /* RCU is atomic, we can't execute FW commands here */
190046719d77SMaor Gottlieb rhl_for_each_entry_rcu(g, tmp, list, hash) {
190146719d77SMaor Gottlieb struct match_list *curr_match;
190246719d77SMaor Gottlieb
1903c2c922daSMark Bloch if (fg && fg != g)
1904c2c922daSMark Bloch continue;
1905c2c922daSMark Bloch
1906b820ce00SEli Cohen if (unlikely(!tree_get_node(&g->node)))
1907bd71b08eSMaor Gottlieb continue;
190846719d77SMaor Gottlieb
190946719d77SMaor Gottlieb curr_match = kmalloc(sizeof(*curr_match), GFP_ATOMIC);
191046719d77SMaor Gottlieb if (!curr_match) {
1911ee27e330SMaor Gottlieb rcu_read_unlock();
1912c1948390SMaor Gottlieb free_match_list(match_head, ft_locked);
1913ee27e330SMaor Gottlieb return -ENOMEM;
191446719d77SMaor Gottlieb }
191546719d77SMaor Gottlieb curr_match->g = g;
191646719d77SMaor Gottlieb list_add_tail(&curr_match->list, &match_head->list);
191746719d77SMaor Gottlieb }
191846719d77SMaor Gottlieb rcu_read_unlock();
191908929f32SMaor Dickman return 0;
192046719d77SMaor Gottlieb }
192146719d77SMaor Gottlieb
matched_fgs_get_version(struct list_head * match_head)1922bd71b08eSMaor Gottlieb static u64 matched_fgs_get_version(struct list_head *match_head)
1923bd71b08eSMaor Gottlieb {
1924bd71b08eSMaor Gottlieb struct match_list *iter;
1925bd71b08eSMaor Gottlieb u64 version = 0;
1926bd71b08eSMaor Gottlieb
1927bd71b08eSMaor Gottlieb list_for_each_entry(iter, match_head, list)
1928bd71b08eSMaor Gottlieb version += (u64)atomic_read(&iter->g->node.version);
1929bd71b08eSMaor Gottlieb return version;
1930bd71b08eSMaor Gottlieb }
1931bd71b08eSMaor Gottlieb
1932ad9421e3SRoi Dayan static struct fs_fte *
lookup_fte_locked(struct mlx5_flow_group * g,const u32 * match_value,bool take_write)19331f0593e7SParav Pandit lookup_fte_locked(struct mlx5_flow_group *g,
19341f0593e7SParav Pandit const u32 *match_value,
19351f0593e7SParav Pandit bool take_write)
1936ad9421e3SRoi Dayan {
1937ad9421e3SRoi Dayan struct fs_fte *fte_tmp;
1938ad9421e3SRoi Dayan
19391f0593e7SParav Pandit if (take_write)
1940ad9421e3SRoi Dayan nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
19417dee607eSParav Pandit else
19421f0593e7SParav Pandit nested_down_read_ref_node(&g->node, FS_LOCK_PARENT);
19431f0593e7SParav Pandit fte_tmp = rhashtable_lookup_fast(&g->ftes_hash, match_value,
19441f0593e7SParav Pandit rhash_fte);
19451f0593e7SParav Pandit if (!fte_tmp || !tree_get_node(&fte_tmp->node)) {
19461f0593e7SParav Pandit fte_tmp = NULL;
19471f0593e7SParav Pandit goto out;
19481f0593e7SParav Pandit }
19491f0593e7SParav Pandit if (!fte_tmp->node.active) {
19501f0593e7SParav Pandit tree_put_node(&fte_tmp->node, false);
19511f0593e7SParav Pandit fte_tmp = NULL;
19521f0593e7SParav Pandit goto out;
19531f0593e7SParav Pandit }
19541f0593e7SParav Pandit
19551f0593e7SParav Pandit nested_down_write_ref_node(&fte_tmp->node, FS_LOCK_CHILD);
19561f0593e7SParav Pandit out:
19571f0593e7SParav Pandit if (take_write)
19581f0593e7SParav Pandit up_write_ref_node(&g->node, false);
19591f0593e7SParav Pandit else
19601f0593e7SParav Pandit up_read_ref_node(&g->node);
19611f0593e7SParav Pandit return fte_tmp;
19627dee607eSParav Pandit }
19637dee607eSParav Pandit
196474491de9SMark Bloch static struct mlx5_flow_handle *
try_add_to_existing_fg(struct mlx5_flow_table * ft,struct list_head * match_head,const struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act,struct mlx5_flow_destination * dest,int dest_num,int ft_version)1965693c6883SMatan Barak try_add_to_existing_fg(struct mlx5_flow_table *ft,
1966bd71b08eSMaor Gottlieb struct list_head *match_head,
19675233794bSArnd Bergmann const struct mlx5_flow_spec *spec,
1968693c6883SMatan Barak struct mlx5_flow_act *flow_act,
1969693c6883SMatan Barak struct mlx5_flow_destination *dest,
1970bd71b08eSMaor Gottlieb int dest_num,
1971bd71b08eSMaor Gottlieb int ft_version)
1972693c6883SMatan Barak {
1973a369d4acSMaor Gottlieb struct mlx5_flow_steering *steering = get_steering(&ft->node);
1974693c6883SMatan Barak struct mlx5_flow_group *g;
1975b92af5a7SMatan Barak struct mlx5_flow_handle *rule;
197646719d77SMaor Gottlieb struct match_list *iter;
1977bd71b08eSMaor Gottlieb bool take_write = false;
1978693c6883SMatan Barak struct fs_fte *fte;
1979dc638d11SEli Cohen u64 version = 0;
1980f5c2ff17SMaor Gottlieb int err;
1981f5c2ff17SMaor Gottlieb
1982bb0ee7dcSJianbo Liu fte = alloc_fte(ft, spec, flow_act);
1983f5c2ff17SMaor Gottlieb if (IS_ERR(fte))
1984f5c2ff17SMaor Gottlieb return ERR_PTR(-ENOMEM);
1985bd71b08eSMaor Gottlieb
1986bd71b08eSMaor Gottlieb search_again_locked:
1987d5634feeSPaul Blakey if (flow_act->flags & FLOW_ACT_NO_APPEND)
1988d5634feeSPaul Blakey goto skip_search;
1989dc638d11SEli Cohen version = matched_fgs_get_version(match_head);
1990dc638d11SEli Cohen /* Try to find an fte with identical match value and attempt update its
1991dc638d11SEli Cohen * action.
1992dc638d11SEli Cohen */
1993bd71b08eSMaor Gottlieb list_for_each_entry(iter, match_head, list) {
1994bd71b08eSMaor Gottlieb struct fs_fte *fte_tmp;
1995693c6883SMatan Barak
1996693c6883SMatan Barak g = iter->g;
1997ad9421e3SRoi Dayan fte_tmp = lookup_fte_locked(g, spec->match_value, take_write);
1998ad9421e3SRoi Dayan if (!fte_tmp)
1999bd71b08eSMaor Gottlieb continue;
2000bb0ee7dcSJianbo Liu rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte_tmp);
2001a5bfe6b4SMaor Gottlieb /* No error check needed here, because insert_fte() is not called */
2002476d61b7SEli Britstein up_write_ref_node(&fte_tmp->node, false);
2003476d61b7SEli Britstein tree_put_node(&fte_tmp->node, false);
2004a369d4acSMaor Gottlieb kmem_cache_free(steering->ftes_cache, fte);
2005bd71b08eSMaor Gottlieb return rule;
2006693c6883SMatan Barak }
2007693c6883SMatan Barak
2008d5634feeSPaul Blakey skip_search:
2009d5634feeSPaul Blakey /* No group with matching fte found, or we skipped the search.
2010d5634feeSPaul Blakey * Try to add a new fte to any matching fg.
2011d5634feeSPaul Blakey */
2012d5634feeSPaul Blakey
2013bd71b08eSMaor Gottlieb /* Check the ft version, for case that new flow group
2014bd71b08eSMaor Gottlieb * was added while the fgs weren't locked
2015bd71b08eSMaor Gottlieb */
2016bd71b08eSMaor Gottlieb if (atomic_read(&ft->node.version) != ft_version) {
2017bd71b08eSMaor Gottlieb rule = ERR_PTR(-EAGAIN);
2018bd71b08eSMaor Gottlieb goto out;
2019bd71b08eSMaor Gottlieb }
2020bd71b08eSMaor Gottlieb
2021dc638d11SEli Cohen /* Check the fgs version. If version have changed it could be that an
2022dc638d11SEli Cohen * FTE with the same match value was added while the fgs weren't
2023dc638d11SEli Cohen * locked.
2024bd71b08eSMaor Gottlieb */
2025dc638d11SEli Cohen if (!(flow_act->flags & FLOW_ACT_NO_APPEND) &&
2026dc638d11SEli Cohen version != matched_fgs_get_version(match_head)) {
2027ad9421e3SRoi Dayan take_write = true;
2028bd71b08eSMaor Gottlieb goto search_again_locked;
2029ad9421e3SRoi Dayan }
2030bd71b08eSMaor Gottlieb
2031bd71b08eSMaor Gottlieb list_for_each_entry(iter, match_head, list) {
2032693c6883SMatan Barak g = iter->g;
2033693c6883SMatan Barak
2034ad9421e3SRoi Dayan nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
2035ad9421e3SRoi Dayan
203649c0355dSPaul Blakey if (!g->node.active) {
203749c0355dSPaul Blakey up_write_ref_node(&g->node, false);
203849c0355dSPaul Blakey continue;
203949c0355dSPaul Blakey }
204049c0355dSPaul Blakey
2041f5c2ff17SMaor Gottlieb err = insert_fte(g, fte);
2042f5c2ff17SMaor Gottlieb if (err) {
2043476d61b7SEli Britstein up_write_ref_node(&g->node, false);
2044f5c2ff17SMaor Gottlieb if (err == -ENOSPC)
2045bd71b08eSMaor Gottlieb continue;
2046a369d4acSMaor Gottlieb kmem_cache_free(steering->ftes_cache, fte);
2047f5c2ff17SMaor Gottlieb return ERR_PTR(err);
2048bd71b08eSMaor Gottlieb }
2049bd71b08eSMaor Gottlieb
2050bd71b08eSMaor Gottlieb nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
2051476d61b7SEli Britstein up_write_ref_node(&g->node, false);
2052bb0ee7dcSJianbo Liu rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
2053476d61b7SEli Britstein up_write_ref_node(&fte->node, false);
2054a5bfe6b4SMaor Gottlieb if (IS_ERR(rule))
2055a5bfe6b4SMaor Gottlieb tree_put_node(&fte->node, false);
2056bd71b08eSMaor Gottlieb return rule;
2057693c6883SMatan Barak }
2058b92af5a7SMatan Barak rule = ERR_PTR(-ENOENT);
2059bd71b08eSMaor Gottlieb out:
2060a369d4acSMaor Gottlieb kmem_cache_free(steering->ftes_cache, fte);
2061693c6883SMatan Barak return rule;
2062693c6883SMatan Barak }
2063693c6883SMatan Barak
2064693c6883SMatan Barak static struct mlx5_flow_handle *
_mlx5_add_flow_rules(struct mlx5_flow_table * ft,const struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act,struct mlx5_flow_destination * dest,int dest_num)206574491de9SMark Bloch _mlx5_add_flow_rules(struct mlx5_flow_table *ft,
20665233794bSArnd Bergmann const struct mlx5_flow_spec *spec,
206766958ed9SHadar Hen Zion struct mlx5_flow_act *flow_act,
206874491de9SMark Bloch struct mlx5_flow_destination *dest,
206974491de9SMark Bloch int dest_num)
207066958ed9SHadar Hen Zion
20710c56b975SMaor Gottlieb {
2072a369d4acSMaor Gottlieb struct mlx5_flow_steering *steering = get_steering(&ft->node);
207374491de9SMark Bloch struct mlx5_flow_handle *rule;
2074b820ce00SEli Cohen struct match_list match_head;
2075b820ce00SEli Cohen struct mlx5_flow_group *g;
2076bd71b08eSMaor Gottlieb bool take_write = false;
2077bd71b08eSMaor Gottlieb struct fs_fte *fte;
2078bd71b08eSMaor Gottlieb int version;
207919f100feSMaor Gottlieb int err;
208074491de9SMark Bloch int i;
20810c56b975SMaor Gottlieb
2082693c6883SMatan Barak if (!check_valid_spec(spec))
20830d235c3fSMatan Barak return ERR_PTR(-EINVAL);
20840d235c3fSMatan Barak
2085c2c922daSMark Bloch if (flow_act->fg && ft->autogroup.active)
2086c2c922daSMark Bloch return ERR_PTR(-EINVAL);
2087c2c922daSMark Bloch
2088f07d8afbSOz Shlomo if (dest && dest_num <= 0)
2089f07d8afbSOz Shlomo return ERR_PTR(-EINVAL);
2090f07d8afbSOz Shlomo
209174491de9SMark Bloch for (i = 0; i < dest_num; i++) {
2092ff189b43SPaul Blakey if (!dest_is_valid(&dest[i], flow_act, ft))
209360ab4584SAmir Vadai return ERR_PTR(-EINVAL);
209474491de9SMark Bloch }
2095bd71b08eSMaor Gottlieb nested_down_read_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
2096bd71b08eSMaor Gottlieb search_again_locked:
2097bd71b08eSMaor Gottlieb version = atomic_read(&ft->node.version);
209860ab4584SAmir Vadai
2099bd71b08eSMaor Gottlieb /* Collect all fgs which has a matching match_criteria */
2100c2c922daSMark Bloch err = build_match_list(&match_head, ft, spec, flow_act->fg, take_write);
21019238e380SVlad Buslov if (err) {
21029238e380SVlad Buslov if (take_write)
2103476d61b7SEli Britstein up_write_ref_node(&ft->node, false);
210407130477SRoi Dayan else
210507130477SRoi Dayan up_read_ref_node(&ft->node);
2106bd71b08eSMaor Gottlieb return ERR_PTR(err);
21079238e380SVlad Buslov }
2108bd71b08eSMaor Gottlieb
2109bd71b08eSMaor Gottlieb if (!take_write)
2110bd71b08eSMaor Gottlieb up_read_ref_node(&ft->node);
2111bd71b08eSMaor Gottlieb
2112bd71b08eSMaor Gottlieb rule = try_add_to_existing_fg(ft, &match_head.list, spec, flow_act, dest,
2113bd71b08eSMaor Gottlieb dest_num, version);
2114c1948390SMaor Gottlieb free_match_list(&match_head, take_write);
2115bd71b08eSMaor Gottlieb if (!IS_ERR(rule) ||
21169238e380SVlad Buslov (PTR_ERR(rule) != -ENOENT && PTR_ERR(rule) != -EAGAIN)) {
21179238e380SVlad Buslov if (take_write)
2118476d61b7SEli Britstein up_write_ref_node(&ft->node, false);
2119bd71b08eSMaor Gottlieb return rule;
21209238e380SVlad Buslov }
2121bd71b08eSMaor Gottlieb
2122bd71b08eSMaor Gottlieb if (!take_write) {
2123bd71b08eSMaor Gottlieb nested_down_write_ref_node(&ft->node, FS_LOCK_GRANDPARENT);
2124bd71b08eSMaor Gottlieb take_write = true;
2125bd71b08eSMaor Gottlieb }
2126bd71b08eSMaor Gottlieb
2127bd71b08eSMaor Gottlieb if (PTR_ERR(rule) == -EAGAIN ||
2128bd71b08eSMaor Gottlieb version != atomic_read(&ft->node.version))
2129bd71b08eSMaor Gottlieb goto search_again_locked;
2130f0d22d18SMaor Gottlieb
213119f100feSMaor Gottlieb g = alloc_auto_flow_group(ft, spec);
2132c3f9bf62SMaor Gottlieb if (IS_ERR(g)) {
2133d34c6efcSRoi Dayan rule = ERR_CAST(g);
2134476d61b7SEli Britstein up_write_ref_node(&ft->node, false);
2135bd71b08eSMaor Gottlieb return rule;
2136c3f9bf62SMaor Gottlieb }
2137c3f9bf62SMaor Gottlieb
213884c7af63SParav Pandit fte = alloc_fte(ft, spec, flow_act);
213984c7af63SParav Pandit if (IS_ERR(fte)) {
214084c7af63SParav Pandit up_write_ref_node(&ft->node, false);
214184c7af63SParav Pandit err = PTR_ERR(fte);
214284c7af63SParav Pandit goto err_alloc_fte;
214384c7af63SParav Pandit }
214484c7af63SParav Pandit
2145bd71b08eSMaor Gottlieb nested_down_write_ref_node(&g->node, FS_LOCK_PARENT);
2146476d61b7SEli Britstein up_write_ref_node(&ft->node, false);
2147bd71b08eSMaor Gottlieb
214819f100feSMaor Gottlieb err = create_auto_flow_group(ft, g);
2149bd71b08eSMaor Gottlieb if (err)
2150bd71b08eSMaor Gottlieb goto err_release_fg;
2151bd71b08eSMaor Gottlieb
2152f5c2ff17SMaor Gottlieb err = insert_fte(g, fte);
215384c7af63SParav Pandit if (err)
2154f5c2ff17SMaor Gottlieb goto err_release_fg;
2155f5c2ff17SMaor Gottlieb
2156bd71b08eSMaor Gottlieb nested_down_write_ref_node(&fte->node, FS_LOCK_CHILD);
2157476d61b7SEli Britstein up_write_ref_node(&g->node, false);
2158bb0ee7dcSJianbo Liu rule = add_rule_fg(g, spec, flow_act, dest, dest_num, fte);
2159476d61b7SEli Britstein up_write_ref_node(&fte->node, false);
2160a5bfe6b4SMaor Gottlieb if (IS_ERR(rule))
2161a5bfe6b4SMaor Gottlieb tree_put_node(&fte->node, false);
2162476d61b7SEli Britstein tree_put_node(&g->node, false);
21630c56b975SMaor Gottlieb return rule;
2164bd71b08eSMaor Gottlieb
2165bd71b08eSMaor Gottlieb err_release_fg:
2166476d61b7SEli Britstein up_write_ref_node(&g->node, false);
216784c7af63SParav Pandit kmem_cache_free(steering->ftes_cache, fte);
216884c7af63SParav Pandit err_alloc_fte:
2169476d61b7SEli Britstein tree_put_node(&g->node, false);
2170bd71b08eSMaor Gottlieb return ERR_PTR(err);
21710c56b975SMaor Gottlieb }
2172b3638e1aSMaor Gottlieb
fwd_next_prio_supported(struct mlx5_flow_table * ft)2173b3638e1aSMaor Gottlieb static bool fwd_next_prio_supported(struct mlx5_flow_table *ft)
2174b3638e1aSMaor Gottlieb {
2175b3638e1aSMaor Gottlieb return ((ft->type == FS_FT_NIC_RX) &&
2176b3638e1aSMaor Gottlieb (MLX5_CAP_FLOWTABLE(get_dev(&ft->node), nic_rx_multi_path_tirs)));
2177b3638e1aSMaor Gottlieb }
2178b3638e1aSMaor Gottlieb
217974491de9SMark Bloch struct mlx5_flow_handle *
mlx5_add_flow_rules(struct mlx5_flow_table * ft,const struct mlx5_flow_spec * spec,struct mlx5_flow_act * flow_act,struct mlx5_flow_destination * dest,int num_dest)218074491de9SMark Bloch mlx5_add_flow_rules(struct mlx5_flow_table *ft,
21815233794bSArnd Bergmann const struct mlx5_flow_spec *spec,
218266958ed9SHadar Hen Zion struct mlx5_flow_act *flow_act,
218374491de9SMark Bloch struct mlx5_flow_destination *dest,
2184cf916ffbSEli Cohen int num_dest)
2185b3638e1aSMaor Gottlieb {
2186b3638e1aSMaor Gottlieb struct mlx5_flow_root_namespace *root = find_root(&ft->node);
21875c2aa8aeSMark Bloch static const struct mlx5_flow_spec zero_spec = {};
218814c129e3SMaor Gottlieb struct mlx5_flow_destination *gen_dest = NULL;
2189b3638e1aSMaor Gottlieb struct mlx5_flow_table *next_ft = NULL;
219074491de9SMark Bloch struct mlx5_flow_handle *handle = NULL;
219166958ed9SHadar Hen Zion u32 sw_action = flow_act->action;
219214c129e3SMaor Gottlieb int i;
2193b3638e1aSMaor Gottlieb
21945c2aa8aeSMark Bloch if (!spec)
21955c2aa8aeSMark Bloch spec = &zero_spec;
21965c2aa8aeSMark Bloch
21979254f8edSMaor Gottlieb if (!is_fwd_next_action(sw_action))
219814c129e3SMaor Gottlieb return _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
219914c129e3SMaor Gottlieb
2200b3638e1aSMaor Gottlieb if (!fwd_next_prio_supported(ft))
2201b3638e1aSMaor Gottlieb return ERR_PTR(-EOPNOTSUPP);
220214c129e3SMaor Gottlieb
2203b3638e1aSMaor Gottlieb mutex_lock(&root->chain_lock);
22049254f8edSMaor Gottlieb next_ft = find_next_fwd_ft(ft, flow_act);
220514c129e3SMaor Gottlieb if (!next_ft) {
220614c129e3SMaor Gottlieb handle = ERR_PTR(-EOPNOTSUPP);
220714c129e3SMaor Gottlieb goto unlock;
2208b3638e1aSMaor Gottlieb }
2209b3638e1aSMaor Gottlieb
221014c129e3SMaor Gottlieb gen_dest = kcalloc(num_dest + 1, sizeof(*dest),
221114c129e3SMaor Gottlieb GFP_KERNEL);
221214c129e3SMaor Gottlieb if (!gen_dest) {
221314c129e3SMaor Gottlieb handle = ERR_PTR(-ENOMEM);
221414c129e3SMaor Gottlieb goto unlock;
221514c129e3SMaor Gottlieb }
221614c129e3SMaor Gottlieb for (i = 0; i < num_dest; i++)
221714c129e3SMaor Gottlieb gen_dest[i] = dest[i];
221814c129e3SMaor Gottlieb gen_dest[i].type =
221914c129e3SMaor Gottlieb MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
222014c129e3SMaor Gottlieb gen_dest[i].ft = next_ft;
222114c129e3SMaor Gottlieb dest = gen_dest;
222214c129e3SMaor Gottlieb num_dest++;
22239254f8edSMaor Gottlieb flow_act->action &= ~(MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO |
22249254f8edSMaor Gottlieb MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_NS);
222514c129e3SMaor Gottlieb flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2226cf916ffbSEli Cohen handle = _mlx5_add_flow_rules(ft, spec, flow_act, dest, num_dest);
222714c129e3SMaor Gottlieb if (IS_ERR(handle))
222814c129e3SMaor Gottlieb goto unlock;
2229b3638e1aSMaor Gottlieb
223014c129e3SMaor Gottlieb if (list_empty(&handle->rule[num_dest - 1]->next_ft)) {
2231b3638e1aSMaor Gottlieb mutex_lock(&next_ft->lock);
223214c129e3SMaor Gottlieb list_add(&handle->rule[num_dest - 1]->next_ft,
223374491de9SMark Bloch &next_ft->fwd_rules);
2234b3638e1aSMaor Gottlieb mutex_unlock(&next_ft->lock);
22359254f8edSMaor Gottlieb handle->rule[num_dest - 1]->sw_action = sw_action;
22369254f8edSMaor Gottlieb handle->rule[num_dest - 1]->ft = ft;
2237b3638e1aSMaor Gottlieb }
223814c129e3SMaor Gottlieb unlock:
2239b3638e1aSMaor Gottlieb mutex_unlock(&root->chain_lock);
224014c129e3SMaor Gottlieb kfree(gen_dest);
224174491de9SMark Bloch return handle;
2242b3638e1aSMaor Gottlieb }
224374491de9SMark Bloch EXPORT_SYMBOL(mlx5_add_flow_rules);
22440c56b975SMaor Gottlieb
mlx5_del_flow_rules(struct mlx5_flow_handle * handle)224574491de9SMark Bloch void mlx5_del_flow_rules(struct mlx5_flow_handle *handle)
22460c56b975SMaor Gottlieb {
2247718ce4d6SEli Britstein struct fs_fte *fte;
224874491de9SMark Bloch int i;
224974491de9SMark Bloch
2250718ce4d6SEli Britstein /* In order to consolidate the HW changes we lock the FTE for other
2251718ce4d6SEli Britstein * changes, and increase its refcount, in order not to perform the
2252718ce4d6SEli Britstein * "del" functions of the FTE. Will handle them here.
2253718ce4d6SEli Britstein * The removal of the rules is done under locked FTE.
2254718ce4d6SEli Britstein * After removing all the handle's rules, if there are remaining
2255718ce4d6SEli Britstein * rules, it means we just need to modify the FTE in FW, and
2256718ce4d6SEli Britstein * unlock/decrease the refcount we increased before.
2257718ce4d6SEli Britstein * Otherwise, it means the FTE should be deleted. First delete the
2258718ce4d6SEli Britstein * FTE in FW. Then, unlock the FTE, and proceed the tree_put_node of
2259718ce4d6SEli Britstein * the FTE, which will handle the last decrease of the refcount, as
2260718ce4d6SEli Britstein * well as required handling of its parent.
2261718ce4d6SEli Britstein */
2262718ce4d6SEli Britstein fs_get_obj(fte, handle->rule[0]->node.parent);
2263718ce4d6SEli Britstein down_write_ref_node(&fte->node, false);
226474491de9SMark Bloch for (i = handle->num_rules - 1; i >= 0; i--)
2265718ce4d6SEli Britstein tree_remove_node(&handle->rule[i]->node, true);
22667b0c6338SMark Bloch if (list_empty(&fte->node.children)) {
226772191a4cSMark Bloch fte->node.del_hw_func(&fte->node);
2268cefc2355SMaor Gottlieb /* Avoid double call to del_hw_fte */
2269cefc2355SMaor Gottlieb fte->node.del_hw_func = NULL;
2270cefc2355SMaor Gottlieb up_write_ref_node(&fte->node, false);
2271718ce4d6SEli Britstein tree_put_node(&fte->node, false);
22727b0c6338SMark Bloch } else if (fte->dests_size) {
22737b0c6338SMark Bloch if (fte->modify_mask)
22747b0c6338SMark Bloch modify_fte(fte);
22757b0c6338SMark Bloch up_write_ref_node(&fte->node, false);
2276b645e57dSMaor Gottlieb } else {
2277b645e57dSMaor Gottlieb up_write_ref_node(&fte->node, false);
2278718ce4d6SEli Britstein }
227974491de9SMark Bloch kfree(handle);
22800c56b975SMaor Gottlieb }
228174491de9SMark Bloch EXPORT_SYMBOL(mlx5_del_flow_rules);
22820c56b975SMaor Gottlieb
22832cc43b49SMaor Gottlieb /* Assuming prio->node.children(flow tables) is sorted by level */
find_next_ft(struct mlx5_flow_table * ft)22842cc43b49SMaor Gottlieb static struct mlx5_flow_table *find_next_ft(struct mlx5_flow_table *ft)
22852cc43b49SMaor Gottlieb {
2286c635ca45SJianbo Liu struct fs_node *prio_parent, *child;
22872cc43b49SMaor Gottlieb struct fs_prio *prio;
22882cc43b49SMaor Gottlieb
22892cc43b49SMaor Gottlieb fs_get_obj(prio, ft->node.parent);
22902cc43b49SMaor Gottlieb
22912cc43b49SMaor Gottlieb if (!list_is_last(&ft->node.list, &prio->node.children))
22922cc43b49SMaor Gottlieb return list_next_entry(ft, node.list);
2293c635ca45SJianbo Liu
2294c635ca45SJianbo Liu prio_parent = find_prio_chains_parent(&prio->node, &child);
2295c635ca45SJianbo Liu
2296c635ca45SJianbo Liu if (prio_parent && list_is_first(&child->list, &prio_parent->children))
2297c635ca45SJianbo Liu return find_closest_ft(&prio->node, false, false);
2298c635ca45SJianbo Liu
2299618d28a5SJianbo Liu return find_next_chained_ft(&prio->node);
23002cc43b49SMaor Gottlieb }
23012cc43b49SMaor Gottlieb
update_root_ft_destroy(struct mlx5_flow_table * ft)23022cc43b49SMaor Gottlieb static int update_root_ft_destroy(struct mlx5_flow_table *ft)
23032cc43b49SMaor Gottlieb {
23042cc43b49SMaor Gottlieb struct mlx5_flow_root_namespace *root = find_root(&ft->node);
2305dae37456SAlex Vesker struct mlx5_ft_underlay_qp *uqp;
23062cc43b49SMaor Gottlieb struct mlx5_flow_table *new_root_ft = NULL;
2307dae37456SAlex Vesker int err = 0;
2308dae37456SAlex Vesker u32 qpn;
23092cc43b49SMaor Gottlieb
23102cc43b49SMaor Gottlieb if (root->root_ft != ft)
23112cc43b49SMaor Gottlieb return 0;
23122cc43b49SMaor Gottlieb
23132cc43b49SMaor Gottlieb new_root_ft = find_next_ft(ft);
2314dae37456SAlex Vesker if (!new_root_ft) {
2315dae37456SAlex Vesker root->root_ft = NULL;
2316dae37456SAlex Vesker return 0;
2317dae37456SAlex Vesker }
2318dae37456SAlex Vesker
2319dae37456SAlex Vesker if (list_empty(&root->underlay_qpns)) {
2320dae37456SAlex Vesker /* Don't set any QPN (zero) in case QPN list is empty */
2321dae37456SAlex Vesker qpn = 0;
2322ae288a48SMaor Gottlieb err = root->cmds->update_root_ft(root, new_root_ft,
2323af76c501SMatan Barak qpn, false);
2324dae37456SAlex Vesker } else {
2325dae37456SAlex Vesker list_for_each_entry(uqp, &root->underlay_qpns, list) {
2326dae37456SAlex Vesker qpn = uqp->qpn;
2327ae288a48SMaor Gottlieb err = root->cmds->update_root_ft(root,
2328af76c501SMatan Barak new_root_ft, qpn,
2329af76c501SMatan Barak false);
2330dae37456SAlex Vesker if (err)
2331dae37456SAlex Vesker break;
23322cc43b49SMaor Gottlieb }
23332cc43b49SMaor Gottlieb }
2334dae37456SAlex Vesker
2335dae37456SAlex Vesker if (err)
2336dae37456SAlex Vesker mlx5_core_warn(root->dev,
2337dae37456SAlex Vesker "Update root flow table of id(%u) qpn(%d) failed\n",
2338dae37456SAlex Vesker ft->id, qpn);
2339dae37456SAlex Vesker else
23402fee37a4SMaor Gottlieb root->root_ft = new_root_ft;
2341dae37456SAlex Vesker
23422cc43b49SMaor Gottlieb return 0;
23432cc43b49SMaor Gottlieb }
23442cc43b49SMaor Gottlieb
2345f90edfd2SMaor Gottlieb /* Connect flow table from previous priority to
2346f90edfd2SMaor Gottlieb * the next flow table.
2347f90edfd2SMaor Gottlieb */
disconnect_flow_table(struct mlx5_flow_table * ft)2348f90edfd2SMaor Gottlieb static int disconnect_flow_table(struct mlx5_flow_table *ft)
2349f90edfd2SMaor Gottlieb {
2350f90edfd2SMaor Gottlieb struct mlx5_core_dev *dev = get_dev(&ft->node);
2351f90edfd2SMaor Gottlieb struct mlx5_flow_table *next_ft;
2352f90edfd2SMaor Gottlieb struct fs_prio *prio;
2353f90edfd2SMaor Gottlieb int err = 0;
2354f90edfd2SMaor Gottlieb
2355f90edfd2SMaor Gottlieb err = update_root_ft_destroy(ft);
2356f90edfd2SMaor Gottlieb if (err)
2357f90edfd2SMaor Gottlieb return err;
2358f90edfd2SMaor Gottlieb
2359f90edfd2SMaor Gottlieb fs_get_obj(prio, ft->node.parent);
2360f90edfd2SMaor Gottlieb if (!(list_first_entry(&prio->node.children,
2361f90edfd2SMaor Gottlieb struct mlx5_flow_table,
2362f90edfd2SMaor Gottlieb node.list) == ft))
2363f90edfd2SMaor Gottlieb return 0;
2364f90edfd2SMaor Gottlieb
23658b54874eSMaor Gottlieb next_ft = find_next_ft(ft);
2366b3638e1aSMaor Gottlieb err = connect_fwd_rules(dev, next_ft, ft);
2367b3638e1aSMaor Gottlieb if (err)
2368b3638e1aSMaor Gottlieb return err;
2369b3638e1aSMaor Gottlieb
2370f90edfd2SMaor Gottlieb err = connect_prev_fts(dev, next_ft, prio);
2371f90edfd2SMaor Gottlieb if (err)
2372f90edfd2SMaor Gottlieb mlx5_core_warn(dev, "Failed to disconnect flow table %d\n",
2373f90edfd2SMaor Gottlieb ft->id);
2374f90edfd2SMaor Gottlieb return err;
2375f90edfd2SMaor Gottlieb }
2376f90edfd2SMaor Gottlieb
mlx5_destroy_flow_table(struct mlx5_flow_table * ft)237786d722adSMaor Gottlieb int mlx5_destroy_flow_table(struct mlx5_flow_table *ft)
23780c56b975SMaor Gottlieb {
23792cc43b49SMaor Gottlieb struct mlx5_flow_root_namespace *root = find_root(&ft->node);
23802cc43b49SMaor Gottlieb int err = 0;
23812cc43b49SMaor Gottlieb
23822cc43b49SMaor Gottlieb mutex_lock(&root->chain_lock);
23835281a0c9SPaul Blakey if (!(ft->flags & MLX5_FLOW_TABLE_UNMANAGED))
2384f90edfd2SMaor Gottlieb err = disconnect_flow_table(ft);
23852cc43b49SMaor Gottlieb if (err) {
23862cc43b49SMaor Gottlieb mutex_unlock(&root->chain_lock);
23872cc43b49SMaor Gottlieb return err;
23882cc43b49SMaor Gottlieb }
2389476d61b7SEli Britstein if (tree_remove_node(&ft->node, false))
23900c56b975SMaor Gottlieb mlx5_core_warn(get_dev(&ft->node), "Flow table %d wasn't destroyed, refcount > 1\n",
23910c56b975SMaor Gottlieb ft->id);
23922cc43b49SMaor Gottlieb mutex_unlock(&root->chain_lock);
23930c56b975SMaor Gottlieb
23942cc43b49SMaor Gottlieb return err;
23950c56b975SMaor Gottlieb }
2396b217ea25SMaor Gottlieb EXPORT_SYMBOL(mlx5_destroy_flow_table);
23970c56b975SMaor Gottlieb
mlx5_destroy_flow_group(struct mlx5_flow_group * fg)239886d722adSMaor Gottlieb void mlx5_destroy_flow_group(struct mlx5_flow_group *fg)
23990c56b975SMaor Gottlieb {
2400476d61b7SEli Britstein if (tree_remove_node(&fg->node, false))
24010c56b975SMaor Gottlieb mlx5_core_warn(get_dev(&fg->node), "Flow group %d wasn't destroyed, refcount > 1\n",
24020c56b975SMaor Gottlieb fg->id);
24030c56b975SMaor Gottlieb }
24048d2a9d8dSEli Cohen EXPORT_SYMBOL(mlx5_destroy_flow_group);
240525302363SMaor Gottlieb
mlx5_get_fdb_sub_ns(struct mlx5_core_dev * dev,int n)2406328edb49SPaul Blakey struct mlx5_flow_namespace *mlx5_get_fdb_sub_ns(struct mlx5_core_dev *dev,
2407328edb49SPaul Blakey int n)
2408328edb49SPaul Blakey {
2409328edb49SPaul Blakey struct mlx5_flow_steering *steering = dev->priv.steering;
2410328edb49SPaul Blakey
2411328edb49SPaul Blakey if (!steering || !steering->fdb_sub_ns)
2412328edb49SPaul Blakey return NULL;
2413328edb49SPaul Blakey
2414328edb49SPaul Blakey return steering->fdb_sub_ns[n];
2415328edb49SPaul Blakey }
2416328edb49SPaul Blakey EXPORT_SYMBOL(mlx5_get_fdb_sub_ns);
2417328edb49SPaul Blakey
is_nic_rx_ns(enum mlx5_flow_namespace_type type)24184588fed7SMaor Gottlieb static bool is_nic_rx_ns(enum mlx5_flow_namespace_type type)
24194588fed7SMaor Gottlieb {
24204588fed7SMaor Gottlieb switch (type) {
24214588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_BYPASS:
242215d187e2SLior Nahmanson case MLX5_FLOW_NAMESPACE_KERNEL_RX_MACSEC:
24234588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_LAG:
24244588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_OFFLOADS:
24254588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_ETHTOOL:
24264588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_KERNEL:
24274588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_LEFTOVERS:
24284588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_ANCHOR:
24294588fed7SMaor Gottlieb return true;
24304588fed7SMaor Gottlieb default:
24314588fed7SMaor Gottlieb return false;
24324588fed7SMaor Gottlieb }
24334588fed7SMaor Gottlieb }
24344588fed7SMaor Gottlieb
mlx5_get_flow_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type)243586d722adSMaor Gottlieb struct mlx5_flow_namespace *mlx5_get_flow_namespace(struct mlx5_core_dev *dev,
243625302363SMaor Gottlieb enum mlx5_flow_namespace_type type)
243725302363SMaor Gottlieb {
2438fba53f7bSMaor Gottlieb struct mlx5_flow_steering *steering = dev->priv.steering;
2439fba53f7bSMaor Gottlieb struct mlx5_flow_root_namespace *root_ns;
24408ce78257SMark Bloch int prio = 0;
244178228cbdSEli Cohen struct fs_prio *fs_prio;
244225302363SMaor Gottlieb struct mlx5_flow_namespace *ns;
244325302363SMaor Gottlieb
2444fba53f7bSMaor Gottlieb if (!steering)
244525302363SMaor Gottlieb return NULL;
244625302363SMaor Gottlieb
244725302363SMaor Gottlieb switch (type) {
244825302363SMaor Gottlieb case MLX5_FLOW_NAMESPACE_FDB:
2449fba53f7bSMaor Gottlieb if (steering->fdb_root_ns)
2450fba53f7bSMaor Gottlieb return &steering->fdb_root_ns->ns;
245125302363SMaor Gottlieb return NULL;
2452425a563aSMaor Gottlieb case MLX5_FLOW_NAMESPACE_PORT_SEL:
2453425a563aSMaor Gottlieb if (steering->port_sel_root_ns)
2454425a563aSMaor Gottlieb return &steering->port_sel_root_ns->ns;
2455425a563aSMaor Gottlieb return NULL;
245687d22483SMaor Gottlieb case MLX5_FLOW_NAMESPACE_SNIFFER_RX:
245787d22483SMaor Gottlieb if (steering->sniffer_rx_root_ns)
245887d22483SMaor Gottlieb return &steering->sniffer_rx_root_ns->ns;
245987d22483SMaor Gottlieb return NULL;
246087d22483SMaor Gottlieb case MLX5_FLOW_NAMESPACE_SNIFFER_TX:
246187d22483SMaor Gottlieb if (steering->sniffer_tx_root_ns)
246287d22483SMaor Gottlieb return &steering->sniffer_tx_root_ns->ns;
24635f418378SAviad Yehezkel return NULL;
2464c7d5fa10SMaor Gottlieb case MLX5_FLOW_NAMESPACE_FDB_BYPASS:
2465c7d5fa10SMaor Gottlieb root_ns = steering->fdb_root_ns;
2466c7d5fa10SMaor Gottlieb prio = FDB_BYPASS_PATH;
2467c7d5fa10SMaor Gottlieb break;
24684588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_EGRESS:
2469ee534d7fSLior Nahmanson case MLX5_FLOW_NAMESPACE_EGRESS_IPSEC:
2470ee534d7fSLior Nahmanson case MLX5_FLOW_NAMESPACE_EGRESS_MACSEC:
24718ce78257SMark Bloch root_ns = steering->egress_root_ns;
2472ee92e4f1SHuy Nguyen prio = type - MLX5_FLOW_NAMESPACE_EGRESS;
24734588fed7SMaor Gottlieb break;
24744588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_RDMA_RX:
2475e6806e9aSMark Zhang root_ns = steering->rdma_rx_root_ns;
2476e6806e9aSMark Zhang prio = RDMA_RX_BYPASS_PRIO;
24774588fed7SMaor Gottlieb break;
24784588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_RDMA_RX_KERNEL:
2479e6806e9aSMark Zhang root_ns = steering->rdma_rx_root_ns;
2480e6806e9aSMark Zhang prio = RDMA_RX_KERNEL_PRIO;
24814588fed7SMaor Gottlieb break;
24824588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_RDMA_TX:
248324670b1aSMichael Guralnik root_ns = steering->rdma_tx_root_ns;
24844588fed7SMaor Gottlieb break;
24854588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_RDMA_RX_COUNTERS:
2486b8dfed63SAharon Landau root_ns = steering->rdma_rx_root_ns;
2487b8dfed63SAharon Landau prio = RDMA_RX_COUNTERS_PRIO;
24884588fed7SMaor Gottlieb break;
24894588fed7SMaor Gottlieb case MLX5_FLOW_NAMESPACE_RDMA_TX_COUNTERS:
2490b8dfed63SAharon Landau root_ns = steering->rdma_tx_root_ns;
2491b8dfed63SAharon Landau prio = RDMA_TX_COUNTERS_PRIO;
24924588fed7SMaor Gottlieb break;
2493f91ddd3aSMark Zhang case MLX5_FLOW_NAMESPACE_RDMA_RX_IPSEC:
2494f91ddd3aSMark Zhang root_ns = steering->rdma_rx_root_ns;
2495f91ddd3aSMark Zhang prio = RDMA_RX_IPSEC_PRIO;
2496f91ddd3aSMark Zhang break;
2497f91ddd3aSMark Zhang case MLX5_FLOW_NAMESPACE_RDMA_TX_IPSEC:
2498f91ddd3aSMark Zhang root_ns = steering->rdma_tx_root_ns;
2499f91ddd3aSMark Zhang prio = RDMA_TX_IPSEC_PRIO;
2500f91ddd3aSMark Zhang break;
2501afcb21d5SPatrisious Haddad case MLX5_FLOW_NAMESPACE_RDMA_RX_MACSEC:
2502afcb21d5SPatrisious Haddad root_ns = steering->rdma_rx_root_ns;
2503afcb21d5SPatrisious Haddad prio = RDMA_RX_MACSEC_PRIO;
2504afcb21d5SPatrisious Haddad break;
2505afcb21d5SPatrisious Haddad case MLX5_FLOW_NAMESPACE_RDMA_TX_MACSEC:
2506afcb21d5SPatrisious Haddad root_ns = steering->rdma_tx_root_ns;
2507afcb21d5SPatrisious Haddad prio = RDMA_TX_MACSEC_PRIO;
2508afcb21d5SPatrisious Haddad break;
25094588fed7SMaor Gottlieb default: /* Must be NIC RX */
25104588fed7SMaor Gottlieb WARN_ON(!is_nic_rx_ns(type));
2511fba53f7bSMaor Gottlieb root_ns = steering->root_ns;
25122226dcb4SMark Bloch prio = type;
25134588fed7SMaor Gottlieb break;
25148ce78257SMark Bloch }
25158ce78257SMark Bloch
2516fba53f7bSMaor Gottlieb if (!root_ns)
2517fba53f7bSMaor Gottlieb return NULL;
2518fba53f7bSMaor Gottlieb
251925302363SMaor Gottlieb fs_prio = find_prio(&root_ns->ns, prio);
252025302363SMaor Gottlieb if (!fs_prio)
252125302363SMaor Gottlieb return NULL;
252225302363SMaor Gottlieb
252325302363SMaor Gottlieb ns = list_first_entry(&fs_prio->node.children,
252425302363SMaor Gottlieb typeof(*ns),
252525302363SMaor Gottlieb node.list);
252625302363SMaor Gottlieb
252725302363SMaor Gottlieb return ns;
252825302363SMaor Gottlieb }
2529b217ea25SMaor Gottlieb EXPORT_SYMBOL(mlx5_get_flow_namespace);
253025302363SMaor Gottlieb
mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type,int vport)25319b93ab98SGal Pressman struct mlx5_flow_namespace *mlx5_get_flow_vport_acl_namespace(struct mlx5_core_dev *dev,
25329b93ab98SGal Pressman enum mlx5_flow_namespace_type type,
25339b93ab98SGal Pressman int vport)
25349b93ab98SGal Pressman {
25359b93ab98SGal Pressman struct mlx5_flow_steering *steering = dev->priv.steering;
25369b93ab98SGal Pressman
253757b92bddSParav Pandit if (!steering)
25389b93ab98SGal Pressman return NULL;
25399b93ab98SGal Pressman
25409b93ab98SGal Pressman switch (type) {
25419b93ab98SGal Pressman case MLX5_FLOW_NAMESPACE_ESW_EGRESS:
254257b92bddSParav Pandit if (vport >= steering->esw_egress_acl_vports)
254357b92bddSParav Pandit return NULL;
25449b93ab98SGal Pressman if (steering->esw_egress_root_ns &&
25459b93ab98SGal Pressman steering->esw_egress_root_ns[vport])
25469b93ab98SGal Pressman return &steering->esw_egress_root_ns[vport]->ns;
25479b93ab98SGal Pressman else
25489b93ab98SGal Pressman return NULL;
25499b93ab98SGal Pressman case MLX5_FLOW_NAMESPACE_ESW_INGRESS:
255057b92bddSParav Pandit if (vport >= steering->esw_ingress_acl_vports)
255157b92bddSParav Pandit return NULL;
25529b93ab98SGal Pressman if (steering->esw_ingress_root_ns &&
25539b93ab98SGal Pressman steering->esw_ingress_root_ns[vport])
25549b93ab98SGal Pressman return &steering->esw_ingress_root_ns[vport]->ns;
25559b93ab98SGal Pressman else
25569b93ab98SGal Pressman return NULL;
25579b93ab98SGal Pressman default:
25589b93ab98SGal Pressman return NULL;
25599b93ab98SGal Pressman }
25609b93ab98SGal Pressman }
25619b93ab98SGal Pressman
_fs_create_prio(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels,enum fs_node_type type)2562328edb49SPaul Blakey static struct fs_prio *_fs_create_prio(struct mlx5_flow_namespace *ns,
2563328edb49SPaul Blakey unsigned int prio,
2564328edb49SPaul Blakey int num_levels,
2565328edb49SPaul Blakey enum fs_node_type type)
256625302363SMaor Gottlieb {
256725302363SMaor Gottlieb struct fs_prio *fs_prio;
256825302363SMaor Gottlieb
256925302363SMaor Gottlieb fs_prio = kzalloc(sizeof(*fs_prio), GFP_KERNEL);
257025302363SMaor Gottlieb if (!fs_prio)
257125302363SMaor Gottlieb return ERR_PTR(-ENOMEM);
257225302363SMaor Gottlieb
2573328edb49SPaul Blakey fs_prio->node.type = type;
2574139ed6c6SMaor Gottlieb tree_init_node(&fs_prio->node, NULL, del_sw_prio);
257525302363SMaor Gottlieb tree_add_node(&fs_prio->node, &ns->node);
2576a257b94aSMaor Gottlieb fs_prio->num_levels = num_levels;
257725302363SMaor Gottlieb fs_prio->prio = prio;
257825302363SMaor Gottlieb list_add_tail(&fs_prio->node.list, &ns->node.children);
257925302363SMaor Gottlieb
258025302363SMaor Gottlieb return fs_prio;
258125302363SMaor Gottlieb }
258225302363SMaor Gottlieb
fs_create_prio_chained(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels)2583328edb49SPaul Blakey static struct fs_prio *fs_create_prio_chained(struct mlx5_flow_namespace *ns,
2584328edb49SPaul Blakey unsigned int prio,
2585328edb49SPaul Blakey int num_levels)
2586328edb49SPaul Blakey {
2587328edb49SPaul Blakey return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO_CHAINS);
2588328edb49SPaul Blakey }
2589328edb49SPaul Blakey
fs_create_prio(struct mlx5_flow_namespace * ns,unsigned int prio,int num_levels)2590328edb49SPaul Blakey static struct fs_prio *fs_create_prio(struct mlx5_flow_namespace *ns,
2591328edb49SPaul Blakey unsigned int prio, int num_levels)
2592328edb49SPaul Blakey {
2593328edb49SPaul Blakey return _fs_create_prio(ns, prio, num_levels, FS_TYPE_PRIO);
2594328edb49SPaul Blakey }
2595328edb49SPaul Blakey
fs_init_namespace(struct mlx5_flow_namespace * ns)259625302363SMaor Gottlieb static struct mlx5_flow_namespace *fs_init_namespace(struct mlx5_flow_namespace
259725302363SMaor Gottlieb *ns)
259825302363SMaor Gottlieb {
259925302363SMaor Gottlieb ns->node.type = FS_TYPE_NAMESPACE;
260025302363SMaor Gottlieb
260125302363SMaor Gottlieb return ns;
260225302363SMaor Gottlieb }
260325302363SMaor Gottlieb
fs_create_namespace(struct fs_prio * prio,int def_miss_act)2604f66ad830SMark Zhang static struct mlx5_flow_namespace *fs_create_namespace(struct fs_prio *prio,
2605f66ad830SMark Zhang int def_miss_act)
260625302363SMaor Gottlieb {
260725302363SMaor Gottlieb struct mlx5_flow_namespace *ns;
260825302363SMaor Gottlieb
260925302363SMaor Gottlieb ns = kzalloc(sizeof(*ns), GFP_KERNEL);
261025302363SMaor Gottlieb if (!ns)
261125302363SMaor Gottlieb return ERR_PTR(-ENOMEM);
261225302363SMaor Gottlieb
261325302363SMaor Gottlieb fs_init_namespace(ns);
2614f66ad830SMark Zhang ns->def_miss_action = def_miss_act;
2615139ed6c6SMaor Gottlieb tree_init_node(&ns->node, NULL, del_sw_ns);
261625302363SMaor Gottlieb tree_add_node(&ns->node, &prio->node);
261725302363SMaor Gottlieb list_add_tail(&ns->node.list, &prio->node.children);
261825302363SMaor Gottlieb
261925302363SMaor Gottlieb return ns;
262025302363SMaor Gottlieb }
262125302363SMaor Gottlieb
create_leaf_prios(struct mlx5_flow_namespace * ns,int prio,struct init_tree_node * prio_metadata)262213de6c10SMaor Gottlieb static int create_leaf_prios(struct mlx5_flow_namespace *ns, int prio,
262313de6c10SMaor Gottlieb struct init_tree_node *prio_metadata)
26244cbdd30eSMaor Gottlieb {
26254cbdd30eSMaor Gottlieb struct fs_prio *fs_prio;
26264cbdd30eSMaor Gottlieb int i;
26274cbdd30eSMaor Gottlieb
26284cbdd30eSMaor Gottlieb for (i = 0; i < prio_metadata->num_leaf_prios; i++) {
262913de6c10SMaor Gottlieb fs_prio = fs_create_prio(ns, prio++, prio_metadata->num_levels);
26304cbdd30eSMaor Gottlieb if (IS_ERR(fs_prio))
26314cbdd30eSMaor Gottlieb return PTR_ERR(fs_prio);
26324cbdd30eSMaor Gottlieb }
26334cbdd30eSMaor Gottlieb return 0;
26344cbdd30eSMaor Gottlieb }
26354cbdd30eSMaor Gottlieb
26368d40d162SMaor Gottlieb #define FLOW_TABLE_BIT_SZ 1
26378d40d162SMaor Gottlieb #define GET_FLOW_TABLE_CAP(dev, offset) \
263848f02eefSParav Pandit ((be32_to_cpu(*((__be32 *)(dev->caps.hca[MLX5_CAP_FLOW_TABLE]->cur) + \
26398d40d162SMaor Gottlieb offset / 32)) >> \
26408d40d162SMaor Gottlieb (32 - FLOW_TABLE_BIT_SZ - (offset & 0x1f))) & FLOW_TABLE_BIT_SZ)
has_required_caps(struct mlx5_core_dev * dev,struct node_caps * caps)26418d40d162SMaor Gottlieb static bool has_required_caps(struct mlx5_core_dev *dev, struct node_caps *caps)
26428d40d162SMaor Gottlieb {
26438d40d162SMaor Gottlieb int i;
26448d40d162SMaor Gottlieb
26458d40d162SMaor Gottlieb for (i = 0; i < caps->arr_sz; i++) {
26468d40d162SMaor Gottlieb if (!GET_FLOW_TABLE_CAP(dev, caps->caps[i]))
26478d40d162SMaor Gottlieb return false;
26488d40d162SMaor Gottlieb }
26498d40d162SMaor Gottlieb return true;
26508d40d162SMaor Gottlieb }
26518d40d162SMaor Gottlieb
init_root_tree_recursive(struct mlx5_flow_steering * steering,struct init_tree_node * init_node,struct fs_node * fs_parent_node,struct init_tree_node * init_parent_node,int prio)2652fba53f7bSMaor Gottlieb static int init_root_tree_recursive(struct mlx5_flow_steering *steering,
26538d40d162SMaor Gottlieb struct init_tree_node *init_node,
265425302363SMaor Gottlieb struct fs_node *fs_parent_node,
265525302363SMaor Gottlieb struct init_tree_node *init_parent_node,
265613de6c10SMaor Gottlieb int prio)
265725302363SMaor Gottlieb {
2658fba53f7bSMaor Gottlieb int max_ft_level = MLX5_CAP_FLOWTABLE(steering->dev,
26598d40d162SMaor Gottlieb flow_table_properties_nic_receive.
26608d40d162SMaor Gottlieb max_ft_level);
266125302363SMaor Gottlieb struct mlx5_flow_namespace *fs_ns;
266225302363SMaor Gottlieb struct fs_prio *fs_prio;
266325302363SMaor Gottlieb struct fs_node *base;
266425302363SMaor Gottlieb int i;
266525302363SMaor Gottlieb int err;
266625302363SMaor Gottlieb
266725302363SMaor Gottlieb if (init_node->type == FS_TYPE_PRIO) {
26688d40d162SMaor Gottlieb if ((init_node->min_ft_level > max_ft_level) ||
2669fba53f7bSMaor Gottlieb !has_required_caps(steering->dev, &init_node->caps))
26708d40d162SMaor Gottlieb return 0;
267125302363SMaor Gottlieb
267225302363SMaor Gottlieb fs_get_obj(fs_ns, fs_parent_node);
26734cbdd30eSMaor Gottlieb if (init_node->num_leaf_prios)
267413de6c10SMaor Gottlieb return create_leaf_prios(fs_ns, prio, init_node);
267513de6c10SMaor Gottlieb fs_prio = fs_create_prio(fs_ns, prio, init_node->num_levels);
267625302363SMaor Gottlieb if (IS_ERR(fs_prio))
267725302363SMaor Gottlieb return PTR_ERR(fs_prio);
267825302363SMaor Gottlieb base = &fs_prio->node;
267925302363SMaor Gottlieb } else if (init_node->type == FS_TYPE_NAMESPACE) {
268025302363SMaor Gottlieb fs_get_obj(fs_prio, fs_parent_node);
2681f66ad830SMark Zhang fs_ns = fs_create_namespace(fs_prio, init_node->def_miss_action);
268225302363SMaor Gottlieb if (IS_ERR(fs_ns))
268325302363SMaor Gottlieb return PTR_ERR(fs_ns);
268425302363SMaor Gottlieb base = &fs_ns->node;
268525302363SMaor Gottlieb } else {
268625302363SMaor Gottlieb return -EINVAL;
268725302363SMaor Gottlieb }
268813de6c10SMaor Gottlieb prio = 0;
268925302363SMaor Gottlieb for (i = 0; i < init_node->ar_size; i++) {
2690fba53f7bSMaor Gottlieb err = init_root_tree_recursive(steering, &init_node->children[i],
269113de6c10SMaor Gottlieb base, init_node, prio);
269225302363SMaor Gottlieb if (err)
269325302363SMaor Gottlieb return err;
269413de6c10SMaor Gottlieb if (init_node->children[i].type == FS_TYPE_PRIO &&
269513de6c10SMaor Gottlieb init_node->children[i].num_leaf_prios) {
269613de6c10SMaor Gottlieb prio += init_node->children[i].num_leaf_prios;
269713de6c10SMaor Gottlieb }
269825302363SMaor Gottlieb }
269925302363SMaor Gottlieb
270025302363SMaor Gottlieb return 0;
270125302363SMaor Gottlieb }
270225302363SMaor Gottlieb
init_root_tree(struct mlx5_flow_steering * steering,struct init_tree_node * init_node,struct fs_node * fs_parent_node)2703fba53f7bSMaor Gottlieb static int init_root_tree(struct mlx5_flow_steering *steering,
27048d40d162SMaor Gottlieb struct init_tree_node *init_node,
270525302363SMaor Gottlieb struct fs_node *fs_parent_node)
270625302363SMaor Gottlieb {
270725302363SMaor Gottlieb int err;
270861e9508fSEli Cohen int i;
270925302363SMaor Gottlieb
271025302363SMaor Gottlieb for (i = 0; i < init_node->ar_size; i++) {
2711fba53f7bSMaor Gottlieb err = init_root_tree_recursive(steering, &init_node->children[i],
271261e9508fSEli Cohen fs_parent_node,
271325302363SMaor Gottlieb init_node, i);
271425302363SMaor Gottlieb if (err)
271525302363SMaor Gottlieb return err;
271625302363SMaor Gottlieb }
271725302363SMaor Gottlieb return 0;
271825302363SMaor Gottlieb }
271925302363SMaor Gottlieb
del_sw_root_ns(struct fs_node * node)27206eb7a268SRoi Dayan static void del_sw_root_ns(struct fs_node *node)
27216eb7a268SRoi Dayan {
27229ca41539SRoi Dayan struct mlx5_flow_root_namespace *root_ns;
27239ca41539SRoi Dayan struct mlx5_flow_namespace *ns;
27249ca41539SRoi Dayan
27259ca41539SRoi Dayan fs_get_obj(ns, node);
27269ca41539SRoi Dayan root_ns = container_of(ns, struct mlx5_flow_root_namespace, ns);
27279ca41539SRoi Dayan mutex_destroy(&root_ns->chain_lock);
27286eb7a268SRoi Dayan kfree(node);
27296eb7a268SRoi Dayan }
27306eb7a268SRoi Dayan
2731af76c501SMatan Barak static struct mlx5_flow_root_namespace
create_root_ns(struct mlx5_flow_steering * steering,enum fs_flow_table_type table_type)2732af76c501SMatan Barak *create_root_ns(struct mlx5_flow_steering *steering,
2733af76c501SMatan Barak enum fs_flow_table_type table_type)
273425302363SMaor Gottlieb {
2735af76c501SMatan Barak const struct mlx5_flow_cmds *cmds = mlx5_fs_cmd_get_default(table_type);
273625302363SMaor Gottlieb struct mlx5_flow_root_namespace *root_ns;
273725302363SMaor Gottlieb struct mlx5_flow_namespace *ns;
273825302363SMaor Gottlieb
273986d722adSMaor Gottlieb /* Create the root namespace */
274025fa506bSParav Pandit root_ns = kzalloc(sizeof(*root_ns), GFP_KERNEL);
274125302363SMaor Gottlieb if (!root_ns)
274225302363SMaor Gottlieb return NULL;
274325302363SMaor Gottlieb
2744fba53f7bSMaor Gottlieb root_ns->dev = steering->dev;
274525302363SMaor Gottlieb root_ns->table_type = table_type;
2746af76c501SMatan Barak root_ns->cmds = cmds;
274725302363SMaor Gottlieb
2748dae37456SAlex Vesker INIT_LIST_HEAD(&root_ns->underlay_qpns);
2749dae37456SAlex Vesker
275025302363SMaor Gottlieb ns = &root_ns->ns;
275125302363SMaor Gottlieb fs_init_namespace(ns);
27522cc43b49SMaor Gottlieb mutex_init(&root_ns->chain_lock);
27536eb7a268SRoi Dayan tree_init_node(&ns->node, NULL, del_sw_root_ns);
275425302363SMaor Gottlieb tree_add_node(&ns->node, NULL);
275525302363SMaor Gottlieb
275625302363SMaor Gottlieb return root_ns;
275725302363SMaor Gottlieb }
275825302363SMaor Gottlieb
2759655227edSMaor Gottlieb static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level);
2760655227edSMaor Gottlieb
set_prio_attrs_in_ns(struct mlx5_flow_namespace * ns,int acc_level)2761655227edSMaor Gottlieb static int set_prio_attrs_in_ns(struct mlx5_flow_namespace *ns, int acc_level)
2762655227edSMaor Gottlieb {
2763655227edSMaor Gottlieb struct fs_prio *prio;
2764655227edSMaor Gottlieb
2765655227edSMaor Gottlieb fs_for_each_prio(prio, ns) {
2766a257b94aSMaor Gottlieb /* This updates prio start_level and num_levels */
2767655227edSMaor Gottlieb set_prio_attrs_in_prio(prio, acc_level);
2768a257b94aSMaor Gottlieb acc_level += prio->num_levels;
2769655227edSMaor Gottlieb }
2770655227edSMaor Gottlieb return acc_level;
2771655227edSMaor Gottlieb }
2772655227edSMaor Gottlieb
set_prio_attrs_in_prio(struct fs_prio * prio,int acc_level)2773655227edSMaor Gottlieb static void set_prio_attrs_in_prio(struct fs_prio *prio, int acc_level)
2774655227edSMaor Gottlieb {
2775655227edSMaor Gottlieb struct mlx5_flow_namespace *ns;
2776655227edSMaor Gottlieb int acc_level_ns = acc_level;
2777655227edSMaor Gottlieb
2778655227edSMaor Gottlieb prio->start_level = acc_level;
277934b13cb3SPaul Blakey fs_for_each_ns(ns, prio) {
2780a257b94aSMaor Gottlieb /* This updates start_level and num_levels of ns's priority descendants */
2781655227edSMaor Gottlieb acc_level_ns = set_prio_attrs_in_ns(ns, acc_level);
278234b13cb3SPaul Blakey
278334b13cb3SPaul Blakey /* If this a prio with chains, and we can jump from one chain
278439c538d6SCai Huoqing * (namespace) to another, so we accumulate the levels
278534b13cb3SPaul Blakey */
278634b13cb3SPaul Blakey if (prio->node.type == FS_TYPE_PRIO_CHAINS)
278734b13cb3SPaul Blakey acc_level = acc_level_ns;
278834b13cb3SPaul Blakey }
278934b13cb3SPaul Blakey
2790a257b94aSMaor Gottlieb if (!prio->num_levels)
2791a257b94aSMaor Gottlieb prio->num_levels = acc_level_ns - prio->start_level;
2792a257b94aSMaor Gottlieb WARN_ON(prio->num_levels < acc_level_ns - prio->start_level);
2793655227edSMaor Gottlieb }
2794655227edSMaor Gottlieb
set_prio_attrs(struct mlx5_flow_root_namespace * root_ns)2795655227edSMaor Gottlieb static void set_prio_attrs(struct mlx5_flow_root_namespace *root_ns)
2796655227edSMaor Gottlieb {
2797655227edSMaor Gottlieb struct mlx5_flow_namespace *ns = &root_ns->ns;
2798655227edSMaor Gottlieb struct fs_prio *prio;
2799655227edSMaor Gottlieb int start_level = 0;
2800655227edSMaor Gottlieb
2801655227edSMaor Gottlieb fs_for_each_prio(prio, ns) {
2802655227edSMaor Gottlieb set_prio_attrs_in_prio(prio, start_level);
2803a257b94aSMaor Gottlieb start_level += prio->num_levels;
2804655227edSMaor Gottlieb }
2805655227edSMaor Gottlieb }
2806655227edSMaor Gottlieb
2807153fefbfSMaor Gottlieb #define ANCHOR_PRIO 0
2808153fefbfSMaor Gottlieb #define ANCHOR_SIZE 1
2809d63cd286SMaor Gottlieb #define ANCHOR_LEVEL 0
create_anchor_flow_table(struct mlx5_flow_steering * steering)2810fba53f7bSMaor Gottlieb static int create_anchor_flow_table(struct mlx5_flow_steering *steering)
2811153fefbfSMaor Gottlieb {
2812153fefbfSMaor Gottlieb struct mlx5_flow_namespace *ns = NULL;
2813b3ba5149SErez Shitrit struct mlx5_flow_table_attr ft_attr = {};
2814153fefbfSMaor Gottlieb struct mlx5_flow_table *ft;
2815153fefbfSMaor Gottlieb
2816fba53f7bSMaor Gottlieb ns = mlx5_get_flow_namespace(steering->dev, MLX5_FLOW_NAMESPACE_ANCHOR);
2817eff596daSOr Gerlitz if (WARN_ON(!ns))
2818153fefbfSMaor Gottlieb return -EINVAL;
2819b3ba5149SErez Shitrit
2820b3ba5149SErez Shitrit ft_attr.max_fte = ANCHOR_SIZE;
2821b3ba5149SErez Shitrit ft_attr.level = ANCHOR_LEVEL;
2822b3ba5149SErez Shitrit ft_attr.prio = ANCHOR_PRIO;
2823b3ba5149SErez Shitrit
2824b3ba5149SErez Shitrit ft = mlx5_create_flow_table(ns, &ft_attr);
2825153fefbfSMaor Gottlieb if (IS_ERR(ft)) {
2826fba53f7bSMaor Gottlieb mlx5_core_err(steering->dev, "Failed to create last anchor flow table");
2827153fefbfSMaor Gottlieb return PTR_ERR(ft);
2828153fefbfSMaor Gottlieb }
2829153fefbfSMaor Gottlieb return 0;
2830153fefbfSMaor Gottlieb }
2831153fefbfSMaor Gottlieb
init_root_ns(struct mlx5_flow_steering * steering)2832fba53f7bSMaor Gottlieb static int init_root_ns(struct mlx5_flow_steering *steering)
283325302363SMaor Gottlieb {
28349c26f5f8STalat Batheesh int err;
28359c26f5f8STalat Batheesh
2836fba53f7bSMaor Gottlieb steering->root_ns = create_root_ns(steering, FS_FT_NIC_RX);
283742fb18fdSChristophe Jaillet if (!steering->root_ns)
28389c26f5f8STalat Batheesh return -ENOMEM;
283925302363SMaor Gottlieb
28409c26f5f8STalat Batheesh err = init_root_tree(steering, &root_fs, &steering->root_ns->ns.node);
28419c26f5f8STalat Batheesh if (err)
28429c26f5f8STalat Batheesh goto out_err;
284325302363SMaor Gottlieb
2844fba53f7bSMaor Gottlieb set_prio_attrs(steering->root_ns);
28459c26f5f8STalat Batheesh err = create_anchor_flow_table(steering);
28469c26f5f8STalat Batheesh if (err)
28479c26f5f8STalat Batheesh goto out_err;
2848153fefbfSMaor Gottlieb
284925302363SMaor Gottlieb return 0;
285025302363SMaor Gottlieb
28519c26f5f8STalat Batheesh out_err:
28529c26f5f8STalat Batheesh cleanup_root_ns(steering->root_ns);
28539c26f5f8STalat Batheesh steering->root_ns = NULL;
28549c26f5f8STalat Batheesh return err;
285525302363SMaor Gottlieb }
285625302363SMaor Gottlieb
clean_tree(struct fs_node * node)28570da2d666SMaor Gottlieb static void clean_tree(struct fs_node *node)
285825302363SMaor Gottlieb {
28590da2d666SMaor Gottlieb if (node) {
28600da2d666SMaor Gottlieb struct fs_node *iter;
28610da2d666SMaor Gottlieb struct fs_node *temp;
286225302363SMaor Gottlieb
2863800350a3SMaor Gottlieb tree_get_node(node);
28640da2d666SMaor Gottlieb list_for_each_entry_safe(iter, temp, &node->children, list)
28650da2d666SMaor Gottlieb clean_tree(iter);
2866476d61b7SEli Britstein tree_put_node(node, false);
2867476d61b7SEli Britstein tree_remove_node(node, false);
28680da2d666SMaor Gottlieb }
28690da2d666SMaor Gottlieb }
28700da2d666SMaor Gottlieb
cleanup_root_ns(struct mlx5_flow_root_namespace * root_ns)28710da2d666SMaor Gottlieb static void cleanup_root_ns(struct mlx5_flow_root_namespace *root_ns)
28720da2d666SMaor Gottlieb {
287325302363SMaor Gottlieb if (!root_ns)
287425302363SMaor Gottlieb return;
287525302363SMaor Gottlieb
28760da2d666SMaor Gottlieb clean_tree(&root_ns->ns.node);
287725302363SMaor Gottlieb }
287825302363SMaor Gottlieb
init_sniffer_tx_root_ns(struct mlx5_flow_steering * steering)287987d22483SMaor Gottlieb static int init_sniffer_tx_root_ns(struct mlx5_flow_steering *steering)
288087d22483SMaor Gottlieb {
288187d22483SMaor Gottlieb struct fs_prio *prio;
288287d22483SMaor Gottlieb
288387d22483SMaor Gottlieb steering->sniffer_tx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_TX);
288487d22483SMaor Gottlieb if (!steering->sniffer_tx_root_ns)
288587d22483SMaor Gottlieb return -ENOMEM;
288687d22483SMaor Gottlieb
288787d22483SMaor Gottlieb /* Create single prio */
288887d22483SMaor Gottlieb prio = fs_create_prio(&steering->sniffer_tx_root_ns->ns, 0, 1);
2889905f6bd3SParav Pandit return PTR_ERR_OR_ZERO(prio);
289087d22483SMaor Gottlieb }
289187d22483SMaor Gottlieb
init_sniffer_rx_root_ns(struct mlx5_flow_steering * steering)289287d22483SMaor Gottlieb static int init_sniffer_rx_root_ns(struct mlx5_flow_steering *steering)
289387d22483SMaor Gottlieb {
289487d22483SMaor Gottlieb struct fs_prio *prio;
289587d22483SMaor Gottlieb
289687d22483SMaor Gottlieb steering->sniffer_rx_root_ns = create_root_ns(steering, FS_FT_SNIFFER_RX);
289787d22483SMaor Gottlieb if (!steering->sniffer_rx_root_ns)
289887d22483SMaor Gottlieb return -ENOMEM;
289987d22483SMaor Gottlieb
290087d22483SMaor Gottlieb /* Create single prio */
290187d22483SMaor Gottlieb prio = fs_create_prio(&steering->sniffer_rx_root_ns->ns, 0, 1);
2902905f6bd3SParav Pandit return PTR_ERR_OR_ZERO(prio);
290387d22483SMaor Gottlieb }
290487d22483SMaor Gottlieb
2905425a563aSMaor Gottlieb #define PORT_SEL_NUM_LEVELS 3
init_port_sel_root_ns(struct mlx5_flow_steering * steering)2906425a563aSMaor Gottlieb static int init_port_sel_root_ns(struct mlx5_flow_steering *steering)
2907425a563aSMaor Gottlieb {
2908425a563aSMaor Gottlieb struct fs_prio *prio;
2909425a563aSMaor Gottlieb
2910425a563aSMaor Gottlieb steering->port_sel_root_ns = create_root_ns(steering, FS_FT_PORT_SEL);
2911425a563aSMaor Gottlieb if (!steering->port_sel_root_ns)
2912425a563aSMaor Gottlieb return -ENOMEM;
2913425a563aSMaor Gottlieb
2914425a563aSMaor Gottlieb /* Create single prio */
2915425a563aSMaor Gottlieb prio = fs_create_prio(&steering->port_sel_root_ns->ns, 0,
2916425a563aSMaor Gottlieb PORT_SEL_NUM_LEVELS);
2917425a563aSMaor Gottlieb return PTR_ERR_OR_ZERO(prio);
2918425a563aSMaor Gottlieb }
2919425a563aSMaor Gottlieb
init_rdma_rx_root_ns(struct mlx5_flow_steering * steering)2920d83eb50eSMaor Gottlieb static int init_rdma_rx_root_ns(struct mlx5_flow_steering *steering)
2921d83eb50eSMaor Gottlieb {
2922e6806e9aSMark Zhang int err;
2923d83eb50eSMaor Gottlieb
2924d83eb50eSMaor Gottlieb steering->rdma_rx_root_ns = create_root_ns(steering, FS_FT_RDMA_RX);
2925d83eb50eSMaor Gottlieb if (!steering->rdma_rx_root_ns)
2926d83eb50eSMaor Gottlieb return -ENOMEM;
2927d83eb50eSMaor Gottlieb
2928e6806e9aSMark Zhang err = init_root_tree(steering, &rdma_rx_root_fs,
2929e6806e9aSMark Zhang &steering->rdma_rx_root_ns->ns.node);
2930e6806e9aSMark Zhang if (err)
2931e6806e9aSMark Zhang goto out_err;
2932f6f7d6b5SMaor Gottlieb
2933e6806e9aSMark Zhang set_prio_attrs(steering->rdma_rx_root_ns);
2934e6806e9aSMark Zhang
2935e6806e9aSMark Zhang return 0;
2936e6806e9aSMark Zhang
2937e6806e9aSMark Zhang out_err:
2938e6806e9aSMark Zhang cleanup_root_ns(steering->rdma_rx_root_ns);
2939e6806e9aSMark Zhang steering->rdma_rx_root_ns = NULL;
2940e6806e9aSMark Zhang return err;
2941d83eb50eSMaor Gottlieb }
2942439e843fSPaul Blakey
init_rdma_tx_root_ns(struct mlx5_flow_steering * steering)294324670b1aSMichael Guralnik static int init_rdma_tx_root_ns(struct mlx5_flow_steering *steering)
294424670b1aSMichael Guralnik {
294524670b1aSMichael Guralnik int err;
294624670b1aSMichael Guralnik
294724670b1aSMichael Guralnik steering->rdma_tx_root_ns = create_root_ns(steering, FS_FT_RDMA_TX);
294824670b1aSMichael Guralnik if (!steering->rdma_tx_root_ns)
294924670b1aSMichael Guralnik return -ENOMEM;
295024670b1aSMichael Guralnik
295124670b1aSMichael Guralnik err = init_root_tree(steering, &rdma_tx_root_fs,
295224670b1aSMichael Guralnik &steering->rdma_tx_root_ns->ns.node);
295324670b1aSMichael Guralnik if (err)
295424670b1aSMichael Guralnik goto out_err;
295524670b1aSMichael Guralnik
295624670b1aSMichael Guralnik set_prio_attrs(steering->rdma_tx_root_ns);
295724670b1aSMichael Guralnik
295824670b1aSMichael Guralnik return 0;
295924670b1aSMichael Guralnik
296024670b1aSMichael Guralnik out_err:
296124670b1aSMichael Guralnik cleanup_root_ns(steering->rdma_tx_root_ns);
296224670b1aSMichael Guralnik steering->rdma_tx_root_ns = NULL;
296324670b1aSMichael Guralnik return err;
296424670b1aSMichael Guralnik }
296524670b1aSMichael Guralnik
2966439e843fSPaul Blakey /* FT and tc chains are stored in the same array so we can re-use the
2967439e843fSPaul Blakey * mlx5_get_fdb_sub_ns() and tc api for FT chains.
2968439e843fSPaul Blakey * When creating a new ns for each chain store it in the first available slot.
2969439e843fSPaul Blakey * Assume tc chains are created and stored first and only then the FT chain.
2970439e843fSPaul Blakey */
store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering * steering,struct mlx5_flow_namespace * ns)2971439e843fSPaul Blakey static void store_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2972439e843fSPaul Blakey struct mlx5_flow_namespace *ns)
2973439e843fSPaul Blakey {
2974439e843fSPaul Blakey int chain = 0;
2975439e843fSPaul Blakey
2976439e843fSPaul Blakey while (steering->fdb_sub_ns[chain])
2977439e843fSPaul Blakey ++chain;
2978439e843fSPaul Blakey
2979439e843fSPaul Blakey steering->fdb_sub_ns[chain] = ns;
2980439e843fSPaul Blakey }
2981439e843fSPaul Blakey
create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering * steering,struct fs_prio * maj_prio)2982439e843fSPaul Blakey static int create_fdb_sub_ns_prio_chain(struct mlx5_flow_steering *steering,
2983439e843fSPaul Blakey struct fs_prio *maj_prio)
298425302363SMaor Gottlieb {
2985328edb49SPaul Blakey struct mlx5_flow_namespace *ns;
2986328edb49SPaul Blakey struct fs_prio *min_prio;
2987439e843fSPaul Blakey int prio;
2988439e843fSPaul Blakey
2989439e843fSPaul Blakey ns = fs_create_namespace(maj_prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
2990439e843fSPaul Blakey if (IS_ERR(ns))
2991439e843fSPaul Blakey return PTR_ERR(ns);
2992439e843fSPaul Blakey
2993439e843fSPaul Blakey for (prio = 0; prio < FDB_TC_MAX_PRIO; prio++) {
2994439e843fSPaul Blakey min_prio = fs_create_prio(ns, prio, FDB_TC_LEVELS_PER_PRIO);
2995439e843fSPaul Blakey if (IS_ERR(min_prio))
2996439e843fSPaul Blakey return PTR_ERR(min_prio);
2997439e843fSPaul Blakey }
2998439e843fSPaul Blakey
2999439e843fSPaul Blakey store_fdb_sub_ns_prio_chain(steering, ns);
3000439e843fSPaul Blakey
3001439e843fSPaul Blakey return 0;
3002439e843fSPaul Blakey }
3003439e843fSPaul Blakey
create_fdb_chains(struct mlx5_flow_steering * steering,int fs_prio,int chains)3004439e843fSPaul Blakey static int create_fdb_chains(struct mlx5_flow_steering *steering,
3005439e843fSPaul Blakey int fs_prio,
3006439e843fSPaul Blakey int chains)
3007439e843fSPaul Blakey {
3008439e843fSPaul Blakey struct fs_prio *maj_prio;
3009328edb49SPaul Blakey int levels;
3010328edb49SPaul Blakey int chain;
3011439e843fSPaul Blakey int err;
3012439e843fSPaul Blakey
3013439e843fSPaul Blakey levels = FDB_TC_LEVELS_PER_PRIO * FDB_TC_MAX_PRIO * chains;
3014439e843fSPaul Blakey maj_prio = fs_create_prio_chained(&steering->fdb_root_ns->ns,
3015439e843fSPaul Blakey fs_prio,
3016439e843fSPaul Blakey levels);
3017439e843fSPaul Blakey if (IS_ERR(maj_prio))
3018439e843fSPaul Blakey return PTR_ERR(maj_prio);
3019439e843fSPaul Blakey
3020439e843fSPaul Blakey for (chain = 0; chain < chains; chain++) {
3021439e843fSPaul Blakey err = create_fdb_sub_ns_prio_chain(steering, maj_prio);
3022439e843fSPaul Blakey if (err)
3023439e843fSPaul Blakey return err;
3024439e843fSPaul Blakey }
3025439e843fSPaul Blakey
3026439e843fSPaul Blakey return 0;
3027439e843fSPaul Blakey }
3028439e843fSPaul Blakey
create_fdb_fast_path(struct mlx5_flow_steering * steering)3029439e843fSPaul Blakey static int create_fdb_fast_path(struct mlx5_flow_steering *steering)
3030439e843fSPaul Blakey {
3031439e843fSPaul Blakey int err;
3032439e843fSPaul Blakey
3033975b992fSPaul Blakey steering->fdb_sub_ns = kcalloc(FDB_NUM_CHAINS,
3034439e843fSPaul Blakey sizeof(*steering->fdb_sub_ns),
3035439e843fSPaul Blakey GFP_KERNEL);
3036439e843fSPaul Blakey if (!steering->fdb_sub_ns)
3037439e843fSPaul Blakey return -ENOMEM;
3038439e843fSPaul Blakey
3039975b992fSPaul Blakey err = create_fdb_chains(steering, FDB_TC_OFFLOAD, FDB_TC_MAX_CHAIN + 1);
3040975b992fSPaul Blakey if (err)
3041975b992fSPaul Blakey return err;
3042975b992fSPaul Blakey
3043975b992fSPaul Blakey err = create_fdb_chains(steering, FDB_FT_OFFLOAD, 1);
3044439e843fSPaul Blakey if (err)
3045439e843fSPaul Blakey return err;
3046439e843fSPaul Blakey
3047439e843fSPaul Blakey return 0;
3048439e843fSPaul Blakey }
3049439e843fSPaul Blakey
create_fdb_bypass(struct mlx5_flow_steering * steering)3050c7d5fa10SMaor Gottlieb static int create_fdb_bypass(struct mlx5_flow_steering *steering)
3051c7d5fa10SMaor Gottlieb {
3052c7d5fa10SMaor Gottlieb struct mlx5_flow_namespace *ns;
3053c7d5fa10SMaor Gottlieb struct fs_prio *prio;
3054c7d5fa10SMaor Gottlieb int i;
3055c7d5fa10SMaor Gottlieb
3056c7d5fa10SMaor Gottlieb prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BYPASS_PATH, 0);
3057c7d5fa10SMaor Gottlieb if (IS_ERR(prio))
3058c7d5fa10SMaor Gottlieb return PTR_ERR(prio);
3059c7d5fa10SMaor Gottlieb
3060c7d5fa10SMaor Gottlieb ns = fs_create_namespace(prio, MLX5_FLOW_TABLE_MISS_ACTION_DEF);
3061c7d5fa10SMaor Gottlieb if (IS_ERR(ns))
3062c7d5fa10SMaor Gottlieb return PTR_ERR(ns);
3063c7d5fa10SMaor Gottlieb
3064c7d5fa10SMaor Gottlieb for (i = 0; i < MLX5_BY_PASS_NUM_REGULAR_PRIOS; i++) {
3065c7d5fa10SMaor Gottlieb prio = fs_create_prio(ns, i, 1);
3066c7d5fa10SMaor Gottlieb if (IS_ERR(prio))
3067c7d5fa10SMaor Gottlieb return PTR_ERR(prio);
3068c7d5fa10SMaor Gottlieb }
3069c7d5fa10SMaor Gottlieb return 0;
3070c7d5fa10SMaor Gottlieb }
3071c7d5fa10SMaor Gottlieb
cleanup_fdb_root_ns(struct mlx5_flow_steering * steering)3072684f062cSShay Drory static void cleanup_fdb_root_ns(struct mlx5_flow_steering *steering)
3073684f062cSShay Drory {
3074684f062cSShay Drory cleanup_root_ns(steering->fdb_root_ns);
3075684f062cSShay Drory steering->fdb_root_ns = NULL;
3076684f062cSShay Drory kfree(steering->fdb_sub_ns);
3077684f062cSShay Drory steering->fdb_sub_ns = NULL;
3078684f062cSShay Drory }
3079684f062cSShay Drory
init_fdb_root_ns(struct mlx5_flow_steering * steering)3080439e843fSPaul Blakey static int init_fdb_root_ns(struct mlx5_flow_steering *steering)
3081439e843fSPaul Blakey {
3082439e843fSPaul Blakey struct fs_prio *maj_prio;
3083328edb49SPaul Blakey int err;
308425302363SMaor Gottlieb
3085fba53f7bSMaor Gottlieb steering->fdb_root_ns = create_root_ns(steering, FS_FT_FDB);
3086fba53f7bSMaor Gottlieb if (!steering->fdb_root_ns)
308725302363SMaor Gottlieb return -ENOMEM;
308825302363SMaor Gottlieb
3089c7d5fa10SMaor Gottlieb err = create_fdb_bypass(steering);
3090c7d5fa10SMaor Gottlieb if (err)
3091d9cb0675SMark Bloch goto out_err;
3092c7d5fa10SMaor Gottlieb
30931762f132SJianbo Liu maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_INGRESS, 3);
30941762f132SJianbo Liu if (IS_ERR(maj_prio)) {
30951762f132SJianbo Liu err = PTR_ERR(maj_prio);
30961762f132SJianbo Liu goto out_err;
30971762f132SJianbo Liu }
30981762f132SJianbo Liu
3099439e843fSPaul Blakey err = create_fdb_fast_path(steering);
3100439e843fSPaul Blakey if (err)
31011033665eSOr Gerlitz goto out_err;
3102328edb49SPaul Blakey
3103ec3be887SVlad Buslov maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_TC_MISS, 1);
3104ec3be887SVlad Buslov if (IS_ERR(maj_prio)) {
3105ec3be887SVlad Buslov err = PTR_ERR(maj_prio);
3106ec3be887SVlad Buslov goto out_err;
3107ec3be887SVlad Buslov }
3108ec3be887SVlad Buslov
3109272ecfc9SVlad Buslov maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_BR_OFFLOAD, 4);
311019e9bfa0SVlad Buslov if (IS_ERR(maj_prio)) {
311119e9bfa0SVlad Buslov err = PTR_ERR(maj_prio);
311219e9bfa0SVlad Buslov goto out_err;
311319e9bfa0SVlad Buslov }
311419e9bfa0SVlad Buslov
3115b6d9ccb1SMark Bloch maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_SLOW_PATH, 1);
3116328edb49SPaul Blakey if (IS_ERR(maj_prio)) {
3117328edb49SPaul Blakey err = PTR_ERR(maj_prio);
3118328edb49SPaul Blakey goto out_err;
3119328edb49SPaul Blakey }
31201033665eSOr Gerlitz
3121c6c2bf5dSJianbo Liu maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_CRYPTO_EGRESS, 3);
3122c6c2bf5dSJianbo Liu if (IS_ERR(maj_prio)) {
3123c6c2bf5dSJianbo Liu err = PTR_ERR(maj_prio);
3124c6c2bf5dSJianbo Liu goto out_err;
3125c6c2bf5dSJianbo Liu }
3126c6c2bf5dSJianbo Liu
312796e32687SEli Cohen /* We put this priority last, knowing that nothing will get here
312896e32687SEli Cohen * unless explicitly forwarded to. This is possible because the
312996e32687SEli Cohen * slow path tables have catch all rules and nothing gets passed
313096e32687SEli Cohen * those tables.
313196e32687SEli Cohen */
313296e32687SEli Cohen maj_prio = fs_create_prio(&steering->fdb_root_ns->ns, FDB_PER_VPORT, 1);
313396e32687SEli Cohen if (IS_ERR(maj_prio)) {
313496e32687SEli Cohen err = PTR_ERR(maj_prio);
313596e32687SEli Cohen goto out_err;
313696e32687SEli Cohen }
313796e32687SEli Cohen
31381033665eSOr Gerlitz set_prio_attrs(steering->fdb_root_ns);
31391033665eSOr Gerlitz return 0;
31401033665eSOr Gerlitz
31411033665eSOr Gerlitz out_err:
3142684f062cSShay Drory cleanup_fdb_root_ns(steering);
3143328edb49SPaul Blakey return err;
314425302363SMaor Gottlieb }
314525302363SMaor Gottlieb
init_egress_acl_root_ns(struct mlx5_flow_steering * steering,int vport)31469b93ab98SGal Pressman static int init_egress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
3147efdc810bSMohamad Haj Yahia {
3148efdc810bSMohamad Haj Yahia struct fs_prio *prio;
3149efdc810bSMohamad Haj Yahia
31509b93ab98SGal Pressman steering->esw_egress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_EGRESS_ACL);
31519b93ab98SGal Pressman if (!steering->esw_egress_root_ns[vport])
3152efdc810bSMohamad Haj Yahia return -ENOMEM;
3153efdc810bSMohamad Haj Yahia
3154efdc810bSMohamad Haj Yahia /* create 1 prio*/
31559b93ab98SGal Pressman prio = fs_create_prio(&steering->esw_egress_root_ns[vport]->ns, 0, 1);
315644fafdaaSWei Yongjun return PTR_ERR_OR_ZERO(prio);
3157efdc810bSMohamad Haj Yahia }
3158efdc810bSMohamad Haj Yahia
init_ingress_acl_root_ns(struct mlx5_flow_steering * steering,int vport)31599b93ab98SGal Pressman static int init_ingress_acl_root_ns(struct mlx5_flow_steering *steering, int vport)
3160efdc810bSMohamad Haj Yahia {
3161efdc810bSMohamad Haj Yahia struct fs_prio *prio;
3162efdc810bSMohamad Haj Yahia
31639b93ab98SGal Pressman steering->esw_ingress_root_ns[vport] = create_root_ns(steering, FS_FT_ESW_INGRESS_ACL);
31649b93ab98SGal Pressman if (!steering->esw_ingress_root_ns[vport])
3165efdc810bSMohamad Haj Yahia return -ENOMEM;
3166efdc810bSMohamad Haj Yahia
3167efdc810bSMohamad Haj Yahia /* create 1 prio*/
31689b93ab98SGal Pressman prio = fs_create_prio(&steering->esw_ingress_root_ns[vport]->ns, 0, 1);
316944fafdaaSWei Yongjun return PTR_ERR_OR_ZERO(prio);
3170efdc810bSMohamad Haj Yahia }
3171efdc810bSMohamad Haj Yahia
mlx5_fs_egress_acls_init(struct mlx5_core_dev * dev,int total_vports)317257b92bddSParav Pandit int mlx5_fs_egress_acls_init(struct mlx5_core_dev *dev, int total_vports)
31739b93ab98SGal Pressman {
31749b93ab98SGal Pressman struct mlx5_flow_steering *steering = dev->priv.steering;
31759b93ab98SGal Pressman int err;
31769b93ab98SGal Pressman int i;
31779b93ab98SGal Pressman
31782752b823SParav Pandit steering->esw_egress_root_ns =
31792752b823SParav Pandit kcalloc(total_vports,
31809b93ab98SGal Pressman sizeof(*steering->esw_egress_root_ns),
31819b93ab98SGal Pressman GFP_KERNEL);
31829b93ab98SGal Pressman if (!steering->esw_egress_root_ns)
31839b93ab98SGal Pressman return -ENOMEM;
31849b93ab98SGal Pressman
31852752b823SParav Pandit for (i = 0; i < total_vports; i++) {
31869b93ab98SGal Pressman err = init_egress_acl_root_ns(steering, i);
31879b93ab98SGal Pressman if (err)
31889b93ab98SGal Pressman goto cleanup_root_ns;
31899b93ab98SGal Pressman }
319057b92bddSParav Pandit steering->esw_egress_acl_vports = total_vports;
31919b93ab98SGal Pressman return 0;
31929b93ab98SGal Pressman
31939b93ab98SGal Pressman cleanup_root_ns:
31949b93ab98SGal Pressman for (i--; i >= 0; i--)
31959b93ab98SGal Pressman cleanup_root_ns(steering->esw_egress_root_ns[i]);
31969b93ab98SGal Pressman kfree(steering->esw_egress_root_ns);
31979414277aSParav Pandit steering->esw_egress_root_ns = NULL;
31989b93ab98SGal Pressman return err;
31999b93ab98SGal Pressman }
32009b93ab98SGal Pressman
mlx5_fs_egress_acls_cleanup(struct mlx5_core_dev * dev)320157b92bddSParav Pandit void mlx5_fs_egress_acls_cleanup(struct mlx5_core_dev *dev)
32029b93ab98SGal Pressman {
32039b93ab98SGal Pressman struct mlx5_flow_steering *steering = dev->priv.steering;
320457b92bddSParav Pandit int i;
320557b92bddSParav Pandit
320657b92bddSParav Pandit if (!steering->esw_egress_root_ns)
320757b92bddSParav Pandit return;
320857b92bddSParav Pandit
320957b92bddSParav Pandit for (i = 0; i < steering->esw_egress_acl_vports; i++)
321057b92bddSParav Pandit cleanup_root_ns(steering->esw_egress_root_ns[i]);
321157b92bddSParav Pandit
321257b92bddSParav Pandit kfree(steering->esw_egress_root_ns);
321357b92bddSParav Pandit steering->esw_egress_root_ns = NULL;
321457b92bddSParav Pandit }
321557b92bddSParav Pandit
mlx5_fs_ingress_acls_init(struct mlx5_core_dev * dev,int total_vports)321657b92bddSParav Pandit int mlx5_fs_ingress_acls_init(struct mlx5_core_dev *dev, int total_vports)
321757b92bddSParav Pandit {
321857b92bddSParav Pandit struct mlx5_flow_steering *steering = dev->priv.steering;
32199b93ab98SGal Pressman int err;
32209b93ab98SGal Pressman int i;
32219b93ab98SGal Pressman
32222752b823SParav Pandit steering->esw_ingress_root_ns =
32232752b823SParav Pandit kcalloc(total_vports,
32249b93ab98SGal Pressman sizeof(*steering->esw_ingress_root_ns),
32259b93ab98SGal Pressman GFP_KERNEL);
32269b93ab98SGal Pressman if (!steering->esw_ingress_root_ns)
32279b93ab98SGal Pressman return -ENOMEM;
32289b93ab98SGal Pressman
32292752b823SParav Pandit for (i = 0; i < total_vports; i++) {
32309b93ab98SGal Pressman err = init_ingress_acl_root_ns(steering, i);
32319b93ab98SGal Pressman if (err)
32329b93ab98SGal Pressman goto cleanup_root_ns;
32339b93ab98SGal Pressman }
323457b92bddSParav Pandit steering->esw_ingress_acl_vports = total_vports;
32359b93ab98SGal Pressman return 0;
32369b93ab98SGal Pressman
32379b93ab98SGal Pressman cleanup_root_ns:
32389b93ab98SGal Pressman for (i--; i >= 0; i--)
32399b93ab98SGal Pressman cleanup_root_ns(steering->esw_ingress_root_ns[i]);
32409b93ab98SGal Pressman kfree(steering->esw_ingress_root_ns);
32419414277aSParav Pandit steering->esw_ingress_root_ns = NULL;
32429b93ab98SGal Pressman return err;
32439b93ab98SGal Pressman }
32449b93ab98SGal Pressman
mlx5_fs_ingress_acls_cleanup(struct mlx5_core_dev * dev)324557b92bddSParav Pandit void mlx5_fs_ingress_acls_cleanup(struct mlx5_core_dev *dev)
324657b92bddSParav Pandit {
324757b92bddSParav Pandit struct mlx5_flow_steering *steering = dev->priv.steering;
324857b92bddSParav Pandit int i;
324957b92bddSParav Pandit
325057b92bddSParav Pandit if (!steering->esw_ingress_root_ns)
325157b92bddSParav Pandit return;
325257b92bddSParav Pandit
325357b92bddSParav Pandit for (i = 0; i < steering->esw_ingress_acl_vports; i++)
325457b92bddSParav Pandit cleanup_root_ns(steering->esw_ingress_root_ns[i]);
325557b92bddSParav Pandit
325657b92bddSParav Pandit kfree(steering->esw_ingress_root_ns);
325757b92bddSParav Pandit steering->esw_ingress_root_ns = NULL;
325857b92bddSParav Pandit }
325957b92bddSParav Pandit
mlx5_fs_get_capabilities(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type type)32608348b71cSDima Chumak u32 mlx5_fs_get_capabilities(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type type)
32618348b71cSDima Chumak {
32628348b71cSDima Chumak struct mlx5_flow_root_namespace *root;
32638348b71cSDima Chumak struct mlx5_flow_namespace *ns;
32648348b71cSDima Chumak
32658348b71cSDima Chumak ns = mlx5_get_flow_namespace(dev, type);
32668348b71cSDima Chumak if (!ns)
32678348b71cSDima Chumak return 0;
32688348b71cSDima Chumak
32698348b71cSDima Chumak root = find_root(&ns->node);
32708348b71cSDima Chumak if (!root)
32718348b71cSDima Chumak return 0;
32728348b71cSDima Chumak
32738348b71cSDima Chumak return root->cmds->get_capabilities(root, root->table_type);
32748348b71cSDima Chumak }
32758348b71cSDima Chumak
init_egress_root_ns(struct mlx5_flow_steering * steering)32765f418378SAviad Yehezkel static int init_egress_root_ns(struct mlx5_flow_steering *steering)
32775f418378SAviad Yehezkel {
32788ce78257SMark Bloch int err;
32795f418378SAviad Yehezkel
32805f418378SAviad Yehezkel steering->egress_root_ns = create_root_ns(steering,
32815f418378SAviad Yehezkel FS_FT_NIC_TX);
32825f418378SAviad Yehezkel if (!steering->egress_root_ns)
32835f418378SAviad Yehezkel return -ENOMEM;
32845f418378SAviad Yehezkel
32858ce78257SMark Bloch err = init_root_tree(steering, &egress_root_fs,
32868ce78257SMark Bloch &steering->egress_root_ns->ns.node);
32878ce78257SMark Bloch if (err)
32888ce78257SMark Bloch goto cleanup;
32898ce78257SMark Bloch set_prio_attrs(steering->egress_root_ns);
32908ce78257SMark Bloch return 0;
32918ce78257SMark Bloch cleanup:
32928ce78257SMark Bloch cleanup_root_ns(steering->egress_root_ns);
32938ce78257SMark Bloch steering->egress_root_ns = NULL;
32948ce78257SMark Bloch return err;
32955f418378SAviad Yehezkel }
32965f418378SAviad Yehezkel
mlx5_fs_mode_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)3297db492c1eSJiri Pirko static int mlx5_fs_mode_validate(struct devlink *devlink, u32 id,
3298db492c1eSJiri Pirko union devlink_param_value val,
3299db492c1eSJiri Pirko struct netlink_ext_ack *extack)
3300db492c1eSJiri Pirko {
3301db492c1eSJiri Pirko struct mlx5_core_dev *dev = devlink_priv(devlink);
3302db492c1eSJiri Pirko char *value = val.vstr;
3303db492c1eSJiri Pirko int err = 0;
3304db492c1eSJiri Pirko
3305db492c1eSJiri Pirko if (!strcmp(value, "dmfs")) {
3306db492c1eSJiri Pirko return 0;
3307db492c1eSJiri Pirko } else if (!strcmp(value, "smfs")) {
3308db492c1eSJiri Pirko u8 eswitch_mode;
3309db492c1eSJiri Pirko bool smfs_cap;
3310db492c1eSJiri Pirko
3311db492c1eSJiri Pirko eswitch_mode = mlx5_eswitch_mode(dev);
3312db492c1eSJiri Pirko smfs_cap = mlx5_fs_dr_is_supported(dev);
3313db492c1eSJiri Pirko
3314db492c1eSJiri Pirko if (!smfs_cap) {
3315db492c1eSJiri Pirko err = -EOPNOTSUPP;
3316db492c1eSJiri Pirko NL_SET_ERR_MSG_MOD(extack,
3317db492c1eSJiri Pirko "Software managed steering is not supported by current device");
3318db492c1eSJiri Pirko }
3319db492c1eSJiri Pirko
3320db492c1eSJiri Pirko else if (eswitch_mode == MLX5_ESWITCH_OFFLOADS) {
3321db492c1eSJiri Pirko NL_SET_ERR_MSG_MOD(extack,
3322db492c1eSJiri Pirko "Software managed steering is not supported when eswitch offloads enabled.");
3323db492c1eSJiri Pirko err = -EOPNOTSUPP;
3324db492c1eSJiri Pirko }
3325db492c1eSJiri Pirko } else {
3326db492c1eSJiri Pirko NL_SET_ERR_MSG_MOD(extack,
3327db492c1eSJiri Pirko "Bad parameter: supported values are [\"dmfs\", \"smfs\"]");
3328db492c1eSJiri Pirko err = -EINVAL;
3329db492c1eSJiri Pirko }
3330db492c1eSJiri Pirko
3331db492c1eSJiri Pirko return err;
3332db492c1eSJiri Pirko }
3333db492c1eSJiri Pirko
mlx5_fs_mode_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)3334db492c1eSJiri Pirko static int mlx5_fs_mode_set(struct devlink *devlink, u32 id,
3335db492c1eSJiri Pirko struct devlink_param_gset_ctx *ctx)
3336db492c1eSJiri Pirko {
3337db492c1eSJiri Pirko struct mlx5_core_dev *dev = devlink_priv(devlink);
3338db492c1eSJiri Pirko enum mlx5_flow_steering_mode mode;
3339db492c1eSJiri Pirko
3340db492c1eSJiri Pirko if (!strcmp(ctx->val.vstr, "smfs"))
3341db492c1eSJiri Pirko mode = MLX5_FLOW_STEERING_MODE_SMFS;
3342db492c1eSJiri Pirko else
3343db492c1eSJiri Pirko mode = MLX5_FLOW_STEERING_MODE_DMFS;
3344db492c1eSJiri Pirko dev->priv.steering->mode = mode;
3345db492c1eSJiri Pirko
3346db492c1eSJiri Pirko return 0;
3347db492c1eSJiri Pirko }
3348db492c1eSJiri Pirko
mlx5_fs_mode_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)3349db492c1eSJiri Pirko static int mlx5_fs_mode_get(struct devlink *devlink, u32 id,
3350db492c1eSJiri Pirko struct devlink_param_gset_ctx *ctx)
3351db492c1eSJiri Pirko {
3352db492c1eSJiri Pirko struct mlx5_core_dev *dev = devlink_priv(devlink);
3353db492c1eSJiri Pirko
3354db492c1eSJiri Pirko if (dev->priv.steering->mode == MLX5_FLOW_STEERING_MODE_SMFS)
3355db492c1eSJiri Pirko strcpy(ctx->val.vstr, "smfs");
3356db492c1eSJiri Pirko else
3357db492c1eSJiri Pirko strcpy(ctx->val.vstr, "dmfs");
3358db492c1eSJiri Pirko return 0;
3359db492c1eSJiri Pirko }
3360db492c1eSJiri Pirko
3361db492c1eSJiri Pirko static const struct devlink_param mlx5_fs_params[] = {
3362db492c1eSJiri Pirko DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE,
3363db492c1eSJiri Pirko "flow_steering_mode", DEVLINK_PARAM_TYPE_STRING,
3364db492c1eSJiri Pirko BIT(DEVLINK_PARAM_CMODE_RUNTIME),
3365db492c1eSJiri Pirko mlx5_fs_mode_get, mlx5_fs_mode_set,
3366db492c1eSJiri Pirko mlx5_fs_mode_validate),
3367db492c1eSJiri Pirko };
3368db492c1eSJiri Pirko
mlx5_fs_core_cleanup(struct mlx5_core_dev * dev)3369b3388697SShay Drory void mlx5_fs_core_cleanup(struct mlx5_core_dev *dev)
337025302363SMaor Gottlieb {
3371b3388697SShay Drory struct mlx5_flow_steering *steering = dev->priv.steering;
3372b3388697SShay Drory
3373b3388697SShay Drory cleanup_root_ns(steering->root_ns);
3374684f062cSShay Drory cleanup_fdb_root_ns(steering);
3375b3388697SShay Drory cleanup_root_ns(steering->port_sel_root_ns);
3376b3388697SShay Drory cleanup_root_ns(steering->sniffer_rx_root_ns);
3377b3388697SShay Drory cleanup_root_ns(steering->sniffer_tx_root_ns);
3378b3388697SShay Drory cleanup_root_ns(steering->rdma_rx_root_ns);
3379b3388697SShay Drory cleanup_root_ns(steering->rdma_tx_root_ns);
3380b3388697SShay Drory cleanup_root_ns(steering->egress_root_ns);
3381db492c1eSJiri Pirko
3382db492c1eSJiri Pirko devl_params_unregister(priv_to_devlink(dev), mlx5_fs_params,
3383db492c1eSJiri Pirko ARRAY_SIZE(mlx5_fs_params));
3384b3388697SShay Drory }
3385b3388697SShay Drory
mlx5_fs_core_init(struct mlx5_core_dev * dev)3386b3388697SShay Drory int mlx5_fs_core_init(struct mlx5_core_dev *dev)
3387b3388697SShay Drory {
3388b3388697SShay Drory struct mlx5_flow_steering *steering = dev->priv.steering;
3389db492c1eSJiri Pirko int err;
3390db492c1eSJiri Pirko
3391db492c1eSJiri Pirko err = devl_params_register(priv_to_devlink(dev), mlx5_fs_params,
3392db492c1eSJiri Pirko ARRAY_SIZE(mlx5_fs_params));
3393db492c1eSJiri Pirko if (err)
3394db492c1eSJiri Pirko return err;
339525302363SMaor Gottlieb
3396ffdb8827SErez Shitrit if ((((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3397ffdb8827SErez Shitrit (MLX5_CAP_GEN(dev, nic_flow_table))) ||
3398ffdb8827SErez Shitrit ((MLX5_CAP_GEN(dev, port_type) == MLX5_CAP_PORT_TYPE_IB) &&
3399ffdb8827SErez Shitrit MLX5_CAP_GEN(dev, ipoib_enhanced_offloads))) &&
3400876d634dSMaor Gottlieb MLX5_CAP_FLOWTABLE_NIC_RX(dev, ft_support)) {
3401fba53f7bSMaor Gottlieb err = init_root_ns(steering);
340225302363SMaor Gottlieb if (err)
340343a335e0SAmir Vadai goto err;
340425302363SMaor Gottlieb }
3405876d634dSMaor Gottlieb
34060efc8562SOr Gerlitz if (MLX5_ESWITCH_MANAGER(dev)) {
3407bd02ef8eSMaor Gottlieb if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ft_support)) {
3408fba53f7bSMaor Gottlieb err = init_fdb_root_ns(steering);
340925302363SMaor Gottlieb if (err)
3410efdc810bSMohamad Haj Yahia goto err;
3411efdc810bSMohamad Haj Yahia }
3412bd02ef8eSMaor Gottlieb }
341325302363SMaor Gottlieb
341487d22483SMaor Gottlieb if (MLX5_CAP_FLOWTABLE_SNIFFER_RX(dev, ft_support)) {
341587d22483SMaor Gottlieb err = init_sniffer_rx_root_ns(steering);
341687d22483SMaor Gottlieb if (err)
341787d22483SMaor Gottlieb goto err;
341887d22483SMaor Gottlieb }
341987d22483SMaor Gottlieb
342087d22483SMaor Gottlieb if (MLX5_CAP_FLOWTABLE_SNIFFER_TX(dev, ft_support)) {
342187d22483SMaor Gottlieb err = init_sniffer_tx_root_ns(steering);
342287d22483SMaor Gottlieb if (err)
342387d22483SMaor Gottlieb goto err;
342487d22483SMaor Gottlieb }
342587d22483SMaor Gottlieb
3426425a563aSMaor Gottlieb if (MLX5_CAP_FLOWTABLE_PORT_SELECTION(dev, ft_support)) {
3427425a563aSMaor Gottlieb err = init_port_sel_root_ns(steering);
3428425a563aSMaor Gottlieb if (err)
3429425a563aSMaor Gottlieb goto err;
3430425a563aSMaor Gottlieb }
3431425a563aSMaor Gottlieb
3432f6f7d6b5SMaor Gottlieb if (MLX5_CAP_FLOWTABLE_RDMA_RX(dev, ft_support) &&
3433f6f7d6b5SMaor Gottlieb MLX5_CAP_FLOWTABLE_RDMA_RX(dev, table_miss_action_domain)) {
3434d83eb50eSMaor Gottlieb err = init_rdma_rx_root_ns(steering);
3435d83eb50eSMaor Gottlieb if (err)
3436d83eb50eSMaor Gottlieb goto err;
3437d83eb50eSMaor Gottlieb }
3438d83eb50eSMaor Gottlieb
343924670b1aSMichael Guralnik if (MLX5_CAP_FLOWTABLE_RDMA_TX(dev, ft_support)) {
344024670b1aSMichael Guralnik err = init_rdma_tx_root_ns(steering);
344124670b1aSMichael Guralnik if (err)
344224670b1aSMichael Guralnik goto err;
344324670b1aSMichael Guralnik }
344424670b1aSMichael Guralnik
34452fa33b35SLeon Romanovsky if (MLX5_CAP_FLOWTABLE_NIC_TX(dev, ft_support)) {
34465f418378SAviad Yehezkel err = init_egress_root_ns(steering);
34475f418378SAviad Yehezkel if (err)
34485f418378SAviad Yehezkel goto err;
34495f418378SAviad Yehezkel }
34505f418378SAviad Yehezkel
3451efdc810bSMohamad Haj Yahia return 0;
3452b3388697SShay Drory
3453efdc810bSMohamad Haj Yahia err:
3454b3388697SShay Drory mlx5_fs_core_cleanup(dev);
3455b3388697SShay Drory return err;
3456b3388697SShay Drory }
3457b3388697SShay Drory
mlx5_fs_core_free(struct mlx5_core_dev * dev)3458b3388697SShay Drory void mlx5_fs_core_free(struct mlx5_core_dev *dev)
3459b3388697SShay Drory {
3460b3388697SShay Drory struct mlx5_flow_steering *steering = dev->priv.steering;
3461b3388697SShay Drory
3462b3388697SShay Drory kmem_cache_destroy(steering->ftes_cache);
3463b3388697SShay Drory kmem_cache_destroy(steering->fgs_cache);
3464b3388697SShay Drory kfree(steering);
3465b3388697SShay Drory mlx5_ft_pool_destroy(dev);
3466b3388697SShay Drory mlx5_cleanup_fc_stats(dev);
3467b3388697SShay Drory }
3468b3388697SShay Drory
mlx5_fs_core_alloc(struct mlx5_core_dev * dev)3469b3388697SShay Drory int mlx5_fs_core_alloc(struct mlx5_core_dev *dev)
3470b3388697SShay Drory {
3471b3388697SShay Drory struct mlx5_flow_steering *steering;
3472b3388697SShay Drory int err = 0;
3473b3388697SShay Drory
3474b3388697SShay Drory err = mlx5_init_fc_stats(dev);
3475b3388697SShay Drory if (err)
3476b3388697SShay Drory return err;
3477b3388697SShay Drory
3478b3388697SShay Drory err = mlx5_ft_pool_init(dev);
3479b3388697SShay Drory if (err)
3480b3388697SShay Drory goto err;
3481b3388697SShay Drory
3482b3388697SShay Drory steering = kzalloc(sizeof(*steering), GFP_KERNEL);
3483b3388697SShay Drory if (!steering) {
3484b3388697SShay Drory err = -ENOMEM;
3485b3388697SShay Drory goto err;
3486b3388697SShay Drory }
3487b3388697SShay Drory
3488b3388697SShay Drory steering->dev = dev;
3489b3388697SShay Drory dev->priv.steering = steering;
3490b3388697SShay Drory
3491b3388697SShay Drory if (mlx5_fs_dr_is_supported(dev))
3492b3388697SShay Drory steering->mode = MLX5_FLOW_STEERING_MODE_SMFS;
3493b3388697SShay Drory else
3494b3388697SShay Drory steering->mode = MLX5_FLOW_STEERING_MODE_DMFS;
3495b3388697SShay Drory
3496b3388697SShay Drory steering->fgs_cache = kmem_cache_create("mlx5_fs_fgs",
3497b3388697SShay Drory sizeof(struct mlx5_flow_group), 0,
3498b3388697SShay Drory 0, NULL);
3499b3388697SShay Drory steering->ftes_cache = kmem_cache_create("mlx5_fs_ftes", sizeof(struct fs_fte), 0,
3500b3388697SShay Drory 0, NULL);
3501b3388697SShay Drory if (!steering->ftes_cache || !steering->fgs_cache) {
3502b3388697SShay Drory err = -ENOMEM;
3503b3388697SShay Drory goto err;
3504b3388697SShay Drory }
3505b3388697SShay Drory
3506b3388697SShay Drory return 0;
3507b3388697SShay Drory
3508b3388697SShay Drory err:
3509b3388697SShay Drory mlx5_fs_core_free(dev);
351025302363SMaor Gottlieb return err;
351125302363SMaor Gottlieb }
351250854114SYishai Hadas
mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)351350854114SYishai Hadas int mlx5_fs_add_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
351450854114SYishai Hadas {
351550854114SYishai Hadas struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3516dae37456SAlex Vesker struct mlx5_ft_underlay_qp *new_uqp;
3517dae37456SAlex Vesker int err = 0;
351850854114SYishai Hadas
3519dae37456SAlex Vesker new_uqp = kzalloc(sizeof(*new_uqp), GFP_KERNEL);
3520dae37456SAlex Vesker if (!new_uqp)
3521dae37456SAlex Vesker return -ENOMEM;
3522dae37456SAlex Vesker
3523dae37456SAlex Vesker mutex_lock(&root->chain_lock);
3524dae37456SAlex Vesker
3525dae37456SAlex Vesker if (!root->root_ft) {
3526dae37456SAlex Vesker err = -EINVAL;
3527dae37456SAlex Vesker goto update_ft_fail;
3528dae37456SAlex Vesker }
3529dae37456SAlex Vesker
3530ae288a48SMaor Gottlieb err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3531af76c501SMatan Barak false);
3532dae37456SAlex Vesker if (err) {
3533dae37456SAlex Vesker mlx5_core_warn(dev, "Failed adding underlay QPN (%u) to root FT err(%d)\n",
3534dae37456SAlex Vesker underlay_qpn, err);
3535dae37456SAlex Vesker goto update_ft_fail;
3536dae37456SAlex Vesker }
3537dae37456SAlex Vesker
3538dae37456SAlex Vesker new_uqp->qpn = underlay_qpn;
3539dae37456SAlex Vesker list_add_tail(&new_uqp->list, &root->underlay_qpns);
3540dae37456SAlex Vesker
3541dae37456SAlex Vesker mutex_unlock(&root->chain_lock);
3542dae37456SAlex Vesker
354350854114SYishai Hadas return 0;
3544dae37456SAlex Vesker
3545dae37456SAlex Vesker update_ft_fail:
3546dae37456SAlex Vesker mutex_unlock(&root->chain_lock);
3547dae37456SAlex Vesker kfree(new_uqp);
3548dae37456SAlex Vesker return err;
354950854114SYishai Hadas }
355050854114SYishai Hadas EXPORT_SYMBOL(mlx5_fs_add_rx_underlay_qpn);
355150854114SYishai Hadas
mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev * dev,u32 underlay_qpn)355250854114SYishai Hadas int mlx5_fs_remove_rx_underlay_qpn(struct mlx5_core_dev *dev, u32 underlay_qpn)
355350854114SYishai Hadas {
355450854114SYishai Hadas struct mlx5_flow_root_namespace *root = dev->priv.steering->root_ns;
3555dae37456SAlex Vesker struct mlx5_ft_underlay_qp *uqp;
3556dae37456SAlex Vesker bool found = false;
3557dae37456SAlex Vesker int err = 0;
355850854114SYishai Hadas
3559dae37456SAlex Vesker mutex_lock(&root->chain_lock);
3560dae37456SAlex Vesker list_for_each_entry(uqp, &root->underlay_qpns, list) {
3561dae37456SAlex Vesker if (uqp->qpn == underlay_qpn) {
3562dae37456SAlex Vesker found = true;
3563dae37456SAlex Vesker break;
3564dae37456SAlex Vesker }
3565dae37456SAlex Vesker }
3566dae37456SAlex Vesker
3567dae37456SAlex Vesker if (!found) {
3568dae37456SAlex Vesker mlx5_core_warn(dev, "Failed finding underlay qp (%u) in qpn list\n",
3569dae37456SAlex Vesker underlay_qpn);
3570dae37456SAlex Vesker err = -EINVAL;
3571dae37456SAlex Vesker goto out;
3572dae37456SAlex Vesker }
3573dae37456SAlex Vesker
3574ae288a48SMaor Gottlieb err = root->cmds->update_root_ft(root, root->root_ft, underlay_qpn,
3575af76c501SMatan Barak true);
3576dae37456SAlex Vesker if (err)
3577dae37456SAlex Vesker mlx5_core_warn(dev, "Failed removing underlay QPN (%u) from root FT err(%d)\n",
3578dae37456SAlex Vesker underlay_qpn, err);
3579dae37456SAlex Vesker
3580dae37456SAlex Vesker list_del(&uqp->list);
3581dae37456SAlex Vesker mutex_unlock(&root->chain_lock);
3582dae37456SAlex Vesker kfree(uqp);
3583dae37456SAlex Vesker
358450854114SYishai Hadas return 0;
3585dae37456SAlex Vesker
3586dae37456SAlex Vesker out:
3587dae37456SAlex Vesker mutex_unlock(&root->chain_lock);
3588dae37456SAlex Vesker return err;
358950854114SYishai Hadas }
359050854114SYishai Hadas EXPORT_SYMBOL(mlx5_fs_remove_rx_underlay_qpn);
35912b688ea5SMaor Gottlieb
35922b688ea5SMaor Gottlieb static struct mlx5_flow_root_namespace
get_root_namespace(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type ns_type)35932b688ea5SMaor Gottlieb *get_root_namespace(struct mlx5_core_dev *dev, enum mlx5_flow_namespace_type ns_type)
35942b688ea5SMaor Gottlieb {
35952b688ea5SMaor Gottlieb struct mlx5_flow_namespace *ns;
35962b688ea5SMaor Gottlieb
35972b688ea5SMaor Gottlieb if (ns_type == MLX5_FLOW_NAMESPACE_ESW_EGRESS ||
35982b688ea5SMaor Gottlieb ns_type == MLX5_FLOW_NAMESPACE_ESW_INGRESS)
35992b688ea5SMaor Gottlieb ns = mlx5_get_flow_vport_acl_namespace(dev, ns_type, 0);
36002b688ea5SMaor Gottlieb else
36012b688ea5SMaor Gottlieb ns = mlx5_get_flow_namespace(dev, ns_type);
36022b688ea5SMaor Gottlieb if (!ns)
36032b688ea5SMaor Gottlieb return NULL;
36042b688ea5SMaor Gottlieb
36052b688ea5SMaor Gottlieb return find_root(&ns->node);
36062b688ea5SMaor Gottlieb }
36072b688ea5SMaor Gottlieb
mlx5_modify_header_alloc(struct mlx5_core_dev * dev,u8 ns_type,u8 num_actions,void * modify_actions)36082b688ea5SMaor Gottlieb struct mlx5_modify_hdr *mlx5_modify_header_alloc(struct mlx5_core_dev *dev,
36092b688ea5SMaor Gottlieb u8 ns_type, u8 num_actions,
36102b688ea5SMaor Gottlieb void *modify_actions)
36112b688ea5SMaor Gottlieb {
36122b688ea5SMaor Gottlieb struct mlx5_flow_root_namespace *root;
36132b688ea5SMaor Gottlieb struct mlx5_modify_hdr *modify_hdr;
36142b688ea5SMaor Gottlieb int err;
36152b688ea5SMaor Gottlieb
36162b688ea5SMaor Gottlieb root = get_root_namespace(dev, ns_type);
36172b688ea5SMaor Gottlieb if (!root)
36182b688ea5SMaor Gottlieb return ERR_PTR(-EOPNOTSUPP);
36192b688ea5SMaor Gottlieb
36202b688ea5SMaor Gottlieb modify_hdr = kzalloc(sizeof(*modify_hdr), GFP_KERNEL);
36212b688ea5SMaor Gottlieb if (!modify_hdr)
36222b688ea5SMaor Gottlieb return ERR_PTR(-ENOMEM);
36232b688ea5SMaor Gottlieb
36242b688ea5SMaor Gottlieb modify_hdr->ns_type = ns_type;
36252b688ea5SMaor Gottlieb err = root->cmds->modify_header_alloc(root, ns_type, num_actions,
36262b688ea5SMaor Gottlieb modify_actions, modify_hdr);
36272b688ea5SMaor Gottlieb if (err) {
36282b688ea5SMaor Gottlieb kfree(modify_hdr);
36292b688ea5SMaor Gottlieb return ERR_PTR(err);
36302b688ea5SMaor Gottlieb }
36312b688ea5SMaor Gottlieb
36322b688ea5SMaor Gottlieb return modify_hdr;
36332b688ea5SMaor Gottlieb }
36342b688ea5SMaor Gottlieb EXPORT_SYMBOL(mlx5_modify_header_alloc);
36352b688ea5SMaor Gottlieb
mlx5_modify_header_dealloc(struct mlx5_core_dev * dev,struct mlx5_modify_hdr * modify_hdr)36362b688ea5SMaor Gottlieb void mlx5_modify_header_dealloc(struct mlx5_core_dev *dev,
36372b688ea5SMaor Gottlieb struct mlx5_modify_hdr *modify_hdr)
36382b688ea5SMaor Gottlieb {
36392b688ea5SMaor Gottlieb struct mlx5_flow_root_namespace *root;
36402b688ea5SMaor Gottlieb
36412b688ea5SMaor Gottlieb root = get_root_namespace(dev, modify_hdr->ns_type);
36422b688ea5SMaor Gottlieb if (WARN_ON(!root))
36432b688ea5SMaor Gottlieb return;
36442b688ea5SMaor Gottlieb root->cmds->modify_header_dealloc(root, modify_hdr);
36452b688ea5SMaor Gottlieb kfree(modify_hdr);
36462b688ea5SMaor Gottlieb }
36472b688ea5SMaor Gottlieb EXPORT_SYMBOL(mlx5_modify_header_dealloc);
36482b688ea5SMaor Gottlieb
mlx5_packet_reformat_alloc(struct mlx5_core_dev * dev,struct mlx5_pkt_reformat_params * params,enum mlx5_flow_namespace_type ns_type)36492b688ea5SMaor Gottlieb struct mlx5_pkt_reformat *mlx5_packet_reformat_alloc(struct mlx5_core_dev *dev,
36503f3f05abSYevgeny Kliteynik struct mlx5_pkt_reformat_params *params,
36512b688ea5SMaor Gottlieb enum mlx5_flow_namespace_type ns_type)
36522b688ea5SMaor Gottlieb {
36532b688ea5SMaor Gottlieb struct mlx5_pkt_reformat *pkt_reformat;
36542b688ea5SMaor Gottlieb struct mlx5_flow_root_namespace *root;
36552b688ea5SMaor Gottlieb int err;
36562b688ea5SMaor Gottlieb
36572b688ea5SMaor Gottlieb root = get_root_namespace(dev, ns_type);
36582b688ea5SMaor Gottlieb if (!root)
36592b688ea5SMaor Gottlieb return ERR_PTR(-EOPNOTSUPP);
36602b688ea5SMaor Gottlieb
36612b688ea5SMaor Gottlieb pkt_reformat = kzalloc(sizeof(*pkt_reformat), GFP_KERNEL);
36622b688ea5SMaor Gottlieb if (!pkt_reformat)
36632b688ea5SMaor Gottlieb return ERR_PTR(-ENOMEM);
36642b688ea5SMaor Gottlieb
36652b688ea5SMaor Gottlieb pkt_reformat->ns_type = ns_type;
36663f3f05abSYevgeny Kliteynik pkt_reformat->reformat_type = params->type;
36673f3f05abSYevgeny Kliteynik err = root->cmds->packet_reformat_alloc(root, params, ns_type,
36682b688ea5SMaor Gottlieb pkt_reformat);
36692b688ea5SMaor Gottlieb if (err) {
36702b688ea5SMaor Gottlieb kfree(pkt_reformat);
36712b688ea5SMaor Gottlieb return ERR_PTR(err);
36722b688ea5SMaor Gottlieb }
36732b688ea5SMaor Gottlieb
36742b688ea5SMaor Gottlieb return pkt_reformat;
36752b688ea5SMaor Gottlieb }
36762b688ea5SMaor Gottlieb EXPORT_SYMBOL(mlx5_packet_reformat_alloc);
36772b688ea5SMaor Gottlieb
mlx5_packet_reformat_dealloc(struct mlx5_core_dev * dev,struct mlx5_pkt_reformat * pkt_reformat)36782b688ea5SMaor Gottlieb void mlx5_packet_reformat_dealloc(struct mlx5_core_dev *dev,
36792b688ea5SMaor Gottlieb struct mlx5_pkt_reformat *pkt_reformat)
36802b688ea5SMaor Gottlieb {
36812b688ea5SMaor Gottlieb struct mlx5_flow_root_namespace *root;
36822b688ea5SMaor Gottlieb
36832b688ea5SMaor Gottlieb root = get_root_namespace(dev, pkt_reformat->ns_type);
36842b688ea5SMaor Gottlieb if (WARN_ON(!root))
36852b688ea5SMaor Gottlieb return;
36862b688ea5SMaor Gottlieb root->cmds->packet_reformat_dealloc(root, pkt_reformat);
36872b688ea5SMaor Gottlieb kfree(pkt_reformat);
36882b688ea5SMaor Gottlieb }
36892b688ea5SMaor Gottlieb EXPORT_SYMBOL(mlx5_packet_reformat_dealloc);
36906a48faeeSMaor Gottlieb
mlx5_get_match_definer_id(struct mlx5_flow_definer * definer)3691e7e2519eSMaor Gottlieb int mlx5_get_match_definer_id(struct mlx5_flow_definer *definer)
3692e7e2519eSMaor Gottlieb {
3693e7e2519eSMaor Gottlieb return definer->id;
3694e7e2519eSMaor Gottlieb }
3695e7e2519eSMaor Gottlieb
3696e7e2519eSMaor Gottlieb struct mlx5_flow_definer *
mlx5_create_match_definer(struct mlx5_core_dev * dev,enum mlx5_flow_namespace_type ns_type,u16 format_id,u32 * match_mask)3697e7e2519eSMaor Gottlieb mlx5_create_match_definer(struct mlx5_core_dev *dev,
3698e7e2519eSMaor Gottlieb enum mlx5_flow_namespace_type ns_type, u16 format_id,
3699e7e2519eSMaor Gottlieb u32 *match_mask)
3700e7e2519eSMaor Gottlieb {
3701e7e2519eSMaor Gottlieb struct mlx5_flow_root_namespace *root;
3702e7e2519eSMaor Gottlieb struct mlx5_flow_definer *definer;
3703e7e2519eSMaor Gottlieb int id;
3704e7e2519eSMaor Gottlieb
3705e7e2519eSMaor Gottlieb root = get_root_namespace(dev, ns_type);
3706e7e2519eSMaor Gottlieb if (!root)
3707e7e2519eSMaor Gottlieb return ERR_PTR(-EOPNOTSUPP);
3708e7e2519eSMaor Gottlieb
3709e7e2519eSMaor Gottlieb definer = kzalloc(sizeof(*definer), GFP_KERNEL);
3710e7e2519eSMaor Gottlieb if (!definer)
3711e7e2519eSMaor Gottlieb return ERR_PTR(-ENOMEM);
3712e7e2519eSMaor Gottlieb
3713e7e2519eSMaor Gottlieb definer->ns_type = ns_type;
3714e7e2519eSMaor Gottlieb id = root->cmds->create_match_definer(root, format_id, match_mask);
3715e7e2519eSMaor Gottlieb if (id < 0) {
3716e7e2519eSMaor Gottlieb mlx5_core_warn(root->dev, "Failed to create match definer (%d)\n", id);
3717e7e2519eSMaor Gottlieb kfree(definer);
3718e7e2519eSMaor Gottlieb return ERR_PTR(id);
3719e7e2519eSMaor Gottlieb }
3720e7e2519eSMaor Gottlieb definer->id = id;
3721e7e2519eSMaor Gottlieb return definer;
3722e7e2519eSMaor Gottlieb }
3723e7e2519eSMaor Gottlieb
mlx5_destroy_match_definer(struct mlx5_core_dev * dev,struct mlx5_flow_definer * definer)3724e7e2519eSMaor Gottlieb void mlx5_destroy_match_definer(struct mlx5_core_dev *dev,
3725e7e2519eSMaor Gottlieb struct mlx5_flow_definer *definer)
3726e7e2519eSMaor Gottlieb {
3727e7e2519eSMaor Gottlieb struct mlx5_flow_root_namespace *root;
3728e7e2519eSMaor Gottlieb
3729e7e2519eSMaor Gottlieb root = get_root_namespace(dev, definer->ns_type);
3730e7e2519eSMaor Gottlieb if (WARN_ON(!root))
3731e7e2519eSMaor Gottlieb return;
3732e7e2519eSMaor Gottlieb
3733e7e2519eSMaor Gottlieb root->cmds->destroy_match_definer(root, definer->id);
3734e7e2519eSMaor Gottlieb kfree(definer);
3735e7e2519eSMaor Gottlieb }
3736e7e2519eSMaor Gottlieb
mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace * ns,struct mlx5_flow_root_namespace * peer_ns,u16 peer_vhca_id)37376a48faeeSMaor Gottlieb int mlx5_flow_namespace_set_peer(struct mlx5_flow_root_namespace *ns,
37386d5b7321SShay Drory struct mlx5_flow_root_namespace *peer_ns,
373962752c0bSShay Drory u16 peer_vhca_id)
37406a48faeeSMaor Gottlieb {
374138b9d1c6SMaor Gottlieb if (peer_ns && ns->mode != peer_ns->mode) {
374238b9d1c6SMaor Gottlieb mlx5_core_err(ns->dev,
374338b9d1c6SMaor Gottlieb "Can't peer namespace of different steering mode\n");
374438b9d1c6SMaor Gottlieb return -EINVAL;
374538b9d1c6SMaor Gottlieb }
374638b9d1c6SMaor Gottlieb
374762752c0bSShay Drory return ns->cmds->set_peer(ns, peer_ns, peer_vhca_id);
37486a48faeeSMaor Gottlieb }
374938b9d1c6SMaor Gottlieb
375038b9d1c6SMaor Gottlieb /* This function should be called only at init stage of the namespace.
375138b9d1c6SMaor Gottlieb * It is not safe to call this function while steering operations
375238b9d1c6SMaor Gottlieb * are executed in the namespace.
375338b9d1c6SMaor Gottlieb */
mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace * ns,enum mlx5_flow_steering_mode mode)375438b9d1c6SMaor Gottlieb int mlx5_flow_namespace_set_mode(struct mlx5_flow_namespace *ns,
375538b9d1c6SMaor Gottlieb enum mlx5_flow_steering_mode mode)
375638b9d1c6SMaor Gottlieb {
375738b9d1c6SMaor Gottlieb struct mlx5_flow_root_namespace *root;
375838b9d1c6SMaor Gottlieb const struct mlx5_flow_cmds *cmds;
375938b9d1c6SMaor Gottlieb int err;
376038b9d1c6SMaor Gottlieb
376138b9d1c6SMaor Gottlieb root = find_root(&ns->node);
376238b9d1c6SMaor Gottlieb if (&root->ns != ns)
376338b9d1c6SMaor Gottlieb /* Can't set cmds to non root namespace */
376438b9d1c6SMaor Gottlieb return -EINVAL;
376538b9d1c6SMaor Gottlieb
376638b9d1c6SMaor Gottlieb if (root->table_type != FS_FT_FDB)
376738b9d1c6SMaor Gottlieb return -EOPNOTSUPP;
376838b9d1c6SMaor Gottlieb
376938b9d1c6SMaor Gottlieb if (root->mode == mode)
377038b9d1c6SMaor Gottlieb return 0;
377138b9d1c6SMaor Gottlieb
377238b9d1c6SMaor Gottlieb if (mode == MLX5_FLOW_STEERING_MODE_SMFS)
377338b9d1c6SMaor Gottlieb cmds = mlx5_fs_cmd_get_dr_cmds();
377438b9d1c6SMaor Gottlieb else
377538b9d1c6SMaor Gottlieb cmds = mlx5_fs_cmd_get_fw_cmds();
377638b9d1c6SMaor Gottlieb if (!cmds)
377738b9d1c6SMaor Gottlieb return -EOPNOTSUPP;
377838b9d1c6SMaor Gottlieb
377938b9d1c6SMaor Gottlieb err = cmds->create_ns(root);
378038b9d1c6SMaor Gottlieb if (err) {
378138b9d1c6SMaor Gottlieb mlx5_core_err(root->dev, "Failed to create flow namespace (%d)\n",
378238b9d1c6SMaor Gottlieb err);
378338b9d1c6SMaor Gottlieb return err;
378438b9d1c6SMaor Gottlieb }
378538b9d1c6SMaor Gottlieb
378638b9d1c6SMaor Gottlieb root->cmds->destroy_ns(root);
378738b9d1c6SMaor Gottlieb root->cmds = cmds;
378838b9d1c6SMaor Gottlieb root->mode = mode;
378938b9d1c6SMaor Gottlieb
379038b9d1c6SMaor Gottlieb return 0;
379138b9d1c6SMaor Gottlieb }
3792