xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c (revision 896d8946da97332d4dc80fa1937d8dd6b1c35ad4)
1 /*
2  * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/etherdevice.h>
34 #include <linux/idr.h>
35 #include <linux/mlx5/driver.h>
36 #include <linux/mlx5/mlx5_ifc.h>
37 #include <linux/mlx5/vport.h>
38 #include <linux/mlx5/fs.h>
39 #include "mlx5_core.h"
40 #include "eswitch.h"
41 #include "esw/indir_table.h"
42 #include "esw/acl/ofld.h"
43 #include "rdma.h"
44 #include "en.h"
45 #include "fs_core.h"
46 #include "lib/mlx5.h"
47 #include "lib/devcom.h"
48 #include "lib/eq.h"
49 #include "lib/fs_chains.h"
50 #include "en_tc.h"
51 #include "en/mapping.h"
52 #include "devlink.h"
53 #include "lag/lag.h"
54 #include "en/tc/post_meter.h"
55 
56 #define mlx5_esw_for_each_rep(esw, i, rep) \
57 	xa_for_each(&((esw)->offloads.vport_reps), i, rep)
58 
59 /* There are two match-all miss flows, one for unicast dst mac and
60  * one for multicast.
61  */
62 #define MLX5_ESW_MISS_FLOWS (2)
63 #define UPLINK_REP_INDEX 0
64 
65 #define MLX5_ESW_VPORT_TBL_SIZE 128
66 #define MLX5_ESW_VPORT_TBL_NUM_GROUPS  4
67 
68 #define MLX5_ESW_FT_OFFLOADS_DROP_RULE (1)
69 
70 #define MLX5_ESW_MAX_CTRL_EQS 4
71 #define MLX5_ESW_DEFAULT_SF_COMP_EQS 8
72 
73 static struct esw_vport_tbl_namespace mlx5_esw_vport_tbl_mirror_ns = {
74 	.max_fte = MLX5_ESW_VPORT_TBL_SIZE,
75 	.max_num_groups = MLX5_ESW_VPORT_TBL_NUM_GROUPS,
76 	.flags = 0,
77 };
78 
mlx5_eswitch_get_rep(struct mlx5_eswitch * esw,u16 vport_num)79 static struct mlx5_eswitch_rep *mlx5_eswitch_get_rep(struct mlx5_eswitch *esw,
80 						     u16 vport_num)
81 {
82 	return xa_load(&esw->offloads.vport_reps, vport_num);
83 }
84 
85 static void
mlx5_eswitch_set_rule_flow_source(struct mlx5_eswitch * esw,struct mlx5_flow_spec * spec,struct mlx5_esw_flow_attr * attr)86 mlx5_eswitch_set_rule_flow_source(struct mlx5_eswitch *esw,
87 				  struct mlx5_flow_spec *spec,
88 				  struct mlx5_esw_flow_attr *attr)
89 {
90 	if (!MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source) || !attr || !attr->in_rep)
91 		return;
92 
93 	if (attr->int_port) {
94 		spec->flow_context.flow_source = mlx5e_tc_int_port_get_flow_source(attr->int_port);
95 
96 		return;
97 	}
98 
99 	spec->flow_context.flow_source = (attr->in_rep->vport == MLX5_VPORT_UPLINK) ?
100 					 MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK :
101 					 MLX5_FLOW_CONTEXT_FLOW_SOURCE_LOCAL_VPORT;
102 }
103 
104 /* Actually only the upper 16 bits of reg c0 need to be cleared, but the lower 16 bits
105  * are not needed as well in the following process. So clear them all for simplicity.
106  */
107 void
mlx5_eswitch_clear_rule_source_port(struct mlx5_eswitch * esw,struct mlx5_flow_spec * spec)108 mlx5_eswitch_clear_rule_source_port(struct mlx5_eswitch *esw, struct mlx5_flow_spec *spec)
109 {
110 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
111 		void *misc2;
112 
113 		misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
114 		MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0, 0);
115 
116 		misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
117 		MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0, 0);
118 
119 		if (!memchr_inv(misc2, 0, MLX5_ST_SZ_BYTES(fte_match_set_misc2)))
120 			spec->match_criteria_enable &= ~MLX5_MATCH_MISC_PARAMETERS_2;
121 	}
122 }
123 
124 static void
mlx5_eswitch_set_rule_source_port(struct mlx5_eswitch * esw,struct mlx5_flow_spec * spec,struct mlx5_flow_attr * attr,struct mlx5_eswitch * src_esw,u16 vport)125 mlx5_eswitch_set_rule_source_port(struct mlx5_eswitch *esw,
126 				  struct mlx5_flow_spec *spec,
127 				  struct mlx5_flow_attr *attr,
128 				  struct mlx5_eswitch *src_esw,
129 				  u16 vport)
130 {
131 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
132 	u32 metadata;
133 	void *misc2;
134 	void *misc;
135 
136 	/* Use metadata matching because vport is not represented by single
137 	 * VHCA in dual-port RoCE mode, and matching on source vport may fail.
138 	 */
139 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
140 		if (mlx5_esw_indir_table_decap_vport(attr))
141 			vport = mlx5_esw_indir_table_decap_vport(attr);
142 
143 		if (!attr->chain && esw_attr && esw_attr->int_port)
144 			metadata =
145 				mlx5e_tc_int_port_get_metadata_for_match(esw_attr->int_port);
146 		else
147 			metadata =
148 				mlx5_eswitch_get_vport_metadata_for_match(src_esw, vport);
149 
150 		misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
151 		MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0, metadata);
152 
153 		misc2 = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
154 		MLX5_SET(fte_match_set_misc2, misc2, metadata_reg_c_0,
155 			 mlx5_eswitch_get_vport_metadata_mask());
156 
157 		spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS_2;
158 	} else {
159 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
160 		MLX5_SET(fte_match_set_misc, misc, source_port, vport);
161 
162 		if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
163 			MLX5_SET(fte_match_set_misc, misc,
164 				 source_eswitch_owner_vhca_id,
165 				 MLX5_CAP_GEN(src_esw->dev, vhca_id));
166 
167 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
168 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
169 		if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
170 			MLX5_SET_TO_ONES(fte_match_set_misc, misc,
171 					 source_eswitch_owner_vhca_id);
172 
173 		spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS;
174 	}
175 }
176 
177 static int
esw_setup_decap_indir(struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr)178 esw_setup_decap_indir(struct mlx5_eswitch *esw,
179 		      struct mlx5_flow_attr *attr)
180 {
181 	struct mlx5_flow_table *ft;
182 
183 	if (!(attr->flags & MLX5_ATTR_FLAG_SRC_REWRITE))
184 		return -EOPNOTSUPP;
185 
186 	ft = mlx5_esw_indir_table_get(esw, attr,
187 				      mlx5_esw_indir_table_decap_vport(attr), true);
188 	return PTR_ERR_OR_ZERO(ft);
189 }
190 
191 static void
esw_cleanup_decap_indir(struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr)192 esw_cleanup_decap_indir(struct mlx5_eswitch *esw,
193 			struct mlx5_flow_attr *attr)
194 {
195 	if (mlx5_esw_indir_table_decap_vport(attr))
196 		mlx5_esw_indir_table_put(esw,
197 					 mlx5_esw_indir_table_decap_vport(attr),
198 					 true);
199 }
200 
201 static int
esw_setup_mtu_dest(struct mlx5_flow_destination * dest,struct mlx5e_meter_attr * meter,int i)202 esw_setup_mtu_dest(struct mlx5_flow_destination *dest,
203 		   struct mlx5e_meter_attr *meter,
204 		   int i)
205 {
206 	dest[i].type = MLX5_FLOW_DESTINATION_TYPE_RANGE;
207 	dest[i].range.field = MLX5_FLOW_DEST_RANGE_FIELD_PKT_LEN;
208 	dest[i].range.min = 0;
209 	dest[i].range.max = meter->params.mtu;
210 	dest[i].range.hit_ft = mlx5e_post_meter_get_mtu_true_ft(meter->post_meter);
211 	dest[i].range.miss_ft = mlx5e_post_meter_get_mtu_false_ft(meter->post_meter);
212 
213 	return 0;
214 }
215 
216 static int
esw_setup_sampler_dest(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,u32 sampler_id,int i)217 esw_setup_sampler_dest(struct mlx5_flow_destination *dest,
218 		       struct mlx5_flow_act *flow_act,
219 		       u32 sampler_id,
220 		       int i)
221 {
222 	flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
223 	dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER;
224 	dest[i].sampler_id = sampler_id;
225 
226 	return 0;
227 }
228 
229 static int
esw_setup_ft_dest(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr,int i)230 esw_setup_ft_dest(struct mlx5_flow_destination *dest,
231 		  struct mlx5_flow_act *flow_act,
232 		  struct mlx5_eswitch *esw,
233 		  struct mlx5_flow_attr *attr,
234 		  int i)
235 {
236 	flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
237 	dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
238 	dest[i].ft = attr->dest_ft;
239 
240 	if (mlx5_esw_indir_table_decap_vport(attr))
241 		return esw_setup_decap_indir(esw, attr);
242 	return 0;
243 }
244 
245 static void
esw_setup_accept_dest(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_fs_chains * chains,int i)246 esw_setup_accept_dest(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
247 		      struct mlx5_fs_chains *chains, int i)
248 {
249 	if (mlx5_chains_ignore_flow_level_supported(chains))
250 		flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
251 	dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
252 	dest[i].ft = mlx5_chains_get_tc_end_ft(chains);
253 }
254 
255 static void
esw_setup_slow_path_dest(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_eswitch * esw,int i)256 esw_setup_slow_path_dest(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
257 			 struct mlx5_eswitch *esw, int i)
258 {
259 	if (MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ignore_flow_level))
260 		flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
261 	dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
262 	dest[i].ft = mlx5_eswitch_get_slow_fdb(esw);
263 }
264 
265 static int
esw_setup_chain_dest(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_fs_chains * chains,u32 chain,u32 prio,u32 level,int i)266 esw_setup_chain_dest(struct mlx5_flow_destination *dest,
267 		     struct mlx5_flow_act *flow_act,
268 		     struct mlx5_fs_chains *chains,
269 		     u32 chain, u32 prio, u32 level,
270 		     int i)
271 {
272 	struct mlx5_flow_table *ft;
273 
274 	flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
275 	ft = mlx5_chains_get_table(chains, chain, prio, level);
276 	if (IS_ERR(ft))
277 		return PTR_ERR(ft);
278 
279 	dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
280 	dest[i].ft = ft;
281 	return  0;
282 }
283 
esw_put_dest_tables_loop(struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr,int from,int to)284 static void esw_put_dest_tables_loop(struct mlx5_eswitch *esw, struct mlx5_flow_attr *attr,
285 				     int from, int to)
286 {
287 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
288 	struct mlx5_fs_chains *chains = esw_chains(esw);
289 	int i;
290 
291 	for (i = from; i < to; i++)
292 		if (esw_attr->dests[i].flags & MLX5_ESW_DEST_CHAIN_WITH_SRC_PORT_CHANGE)
293 			mlx5_chains_put_table(chains, 0, 1, 0);
294 		else if (mlx5_esw_indir_table_needed(esw, attr, esw_attr->dests[i].vport,
295 						     esw_attr->dests[i].mdev))
296 			mlx5_esw_indir_table_put(esw, esw_attr->dests[i].vport, false);
297 }
298 
299 static bool
esw_is_chain_src_port_rewrite(struct mlx5_eswitch * esw,struct mlx5_esw_flow_attr * esw_attr)300 esw_is_chain_src_port_rewrite(struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *esw_attr)
301 {
302 	int i;
303 
304 	for (i = esw_attr->split_count; i < esw_attr->out_count; i++)
305 		if (esw_attr->dests[i].flags & MLX5_ESW_DEST_CHAIN_WITH_SRC_PORT_CHANGE)
306 			return true;
307 	return false;
308 }
309 
310 static int
esw_setup_chain_src_port_rewrite(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_eswitch * esw,struct mlx5_fs_chains * chains,struct mlx5_flow_attr * attr,int * i)311 esw_setup_chain_src_port_rewrite(struct mlx5_flow_destination *dest,
312 				 struct mlx5_flow_act *flow_act,
313 				 struct mlx5_eswitch *esw,
314 				 struct mlx5_fs_chains *chains,
315 				 struct mlx5_flow_attr *attr,
316 				 int *i)
317 {
318 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
319 	int err;
320 
321 	if (!(attr->flags & MLX5_ATTR_FLAG_SRC_REWRITE))
322 		return -EOPNOTSUPP;
323 
324 	/* flow steering cannot handle more than one dest with the same ft
325 	 * in a single flow
326 	 */
327 	if (esw_attr->out_count - esw_attr->split_count > 1)
328 		return -EOPNOTSUPP;
329 
330 	err = esw_setup_chain_dest(dest, flow_act, chains, attr->dest_chain, 1, 0, *i);
331 	if (err)
332 		return err;
333 
334 	if (esw_attr->dests[esw_attr->split_count].pkt_reformat) {
335 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
336 		flow_act->pkt_reformat = esw_attr->dests[esw_attr->split_count].pkt_reformat;
337 	}
338 	(*i)++;
339 
340 	return 0;
341 }
342 
esw_cleanup_chain_src_port_rewrite(struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr)343 static void esw_cleanup_chain_src_port_rewrite(struct mlx5_eswitch *esw,
344 					       struct mlx5_flow_attr *attr)
345 {
346 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
347 
348 	esw_put_dest_tables_loop(esw, attr, esw_attr->split_count, esw_attr->out_count);
349 }
350 
351 static bool
esw_is_indir_table(struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr)352 esw_is_indir_table(struct mlx5_eswitch *esw, struct mlx5_flow_attr *attr)
353 {
354 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
355 	bool result = false;
356 	int i;
357 
358 	/* Indirect table is supported only for flows with in_port uplink
359 	 * and the destination is vport on the same eswitch as the uplink,
360 	 * return false in case at least one of destinations doesn't meet
361 	 * this criteria.
362 	 */
363 	for (i = esw_attr->split_count; i < esw_attr->out_count; i++) {
364 		if (esw_attr->dests[i].vport_valid &&
365 		    mlx5_esw_indir_table_needed(esw, attr, esw_attr->dests[i].vport,
366 						esw_attr->dests[i].mdev)) {
367 			result = true;
368 		} else {
369 			result = false;
370 			break;
371 		}
372 	}
373 	return result;
374 }
375 
376 static int
esw_setup_indir_table(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr,int * i)377 esw_setup_indir_table(struct mlx5_flow_destination *dest,
378 		      struct mlx5_flow_act *flow_act,
379 		      struct mlx5_eswitch *esw,
380 		      struct mlx5_flow_attr *attr,
381 		      int *i)
382 {
383 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
384 	int j, err;
385 
386 	if (!(attr->flags & MLX5_ATTR_FLAG_SRC_REWRITE))
387 		return -EOPNOTSUPP;
388 
389 	for (j = esw_attr->split_count; j < esw_attr->out_count; j++, (*i)++) {
390 		flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
391 		dest[*i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
392 
393 		dest[*i].ft = mlx5_esw_indir_table_get(esw, attr,
394 						       esw_attr->dests[j].vport, false);
395 		if (IS_ERR(dest[*i].ft)) {
396 			err = PTR_ERR(dest[*i].ft);
397 			goto err_indir_tbl_get;
398 		}
399 	}
400 
401 	if (mlx5_esw_indir_table_decap_vport(attr)) {
402 		err = esw_setup_decap_indir(esw, attr);
403 		if (err)
404 			goto err_indir_tbl_get;
405 	}
406 
407 	return 0;
408 
409 err_indir_tbl_get:
410 	esw_put_dest_tables_loop(esw, attr, esw_attr->split_count, j);
411 	return err;
412 }
413 
esw_cleanup_indir_table(struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr)414 static void esw_cleanup_indir_table(struct mlx5_eswitch *esw, struct mlx5_flow_attr *attr)
415 {
416 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
417 
418 	esw_put_dest_tables_loop(esw, attr, esw_attr->split_count, esw_attr->out_count);
419 	esw_cleanup_decap_indir(esw, attr);
420 }
421 
422 static void
esw_cleanup_chain_dest(struct mlx5_fs_chains * chains,u32 chain,u32 prio,u32 level)423 esw_cleanup_chain_dest(struct mlx5_fs_chains *chains, u32 chain, u32 prio, u32 level)
424 {
425 	mlx5_chains_put_table(chains, chain, prio, level);
426 }
427 
esw_same_vhca_id(struct mlx5_core_dev * mdev1,struct mlx5_core_dev * mdev2)428 static bool esw_same_vhca_id(struct mlx5_core_dev *mdev1, struct mlx5_core_dev *mdev2)
429 {
430 	return MLX5_CAP_GEN(mdev1, vhca_id) == MLX5_CAP_GEN(mdev2, vhca_id);
431 }
432 
esw_setup_uplink_fwd_ipsec_needed(struct mlx5_eswitch * esw,struct mlx5_esw_flow_attr * esw_attr,int attr_idx)433 static bool esw_setup_uplink_fwd_ipsec_needed(struct mlx5_eswitch *esw,
434 					      struct mlx5_esw_flow_attr *esw_attr,
435 					      int attr_idx)
436 {
437 	if (esw->offloads.ft_ipsec_tx_pol &&
438 	    esw_attr->dests[attr_idx].vport_valid &&
439 	    esw_attr->dests[attr_idx].vport == MLX5_VPORT_UPLINK &&
440 	    /* To be aligned with software, encryption is needed only for tunnel device */
441 	    (esw_attr->dests[attr_idx].flags & MLX5_ESW_DEST_ENCAP_VALID) &&
442 	    esw_attr->dests[attr_idx].vport != esw_attr->in_rep->vport &&
443 	    esw_same_vhca_id(esw_attr->dests[attr_idx].mdev, esw->dev))
444 		return true;
445 
446 	return false;
447 }
448 
esw_flow_dests_fwd_ipsec_check(struct mlx5_eswitch * esw,struct mlx5_esw_flow_attr * esw_attr)449 static bool esw_flow_dests_fwd_ipsec_check(struct mlx5_eswitch *esw,
450 					   struct mlx5_esw_flow_attr *esw_attr)
451 {
452 	int i;
453 
454 	if (!esw->offloads.ft_ipsec_tx_pol)
455 		return true;
456 
457 	for (i = 0; i < esw_attr->split_count; i++)
458 		if (esw_setup_uplink_fwd_ipsec_needed(esw, esw_attr, i))
459 			return false;
460 
461 	for (i = esw_attr->split_count; i < esw_attr->out_count; i++)
462 		if (esw_setup_uplink_fwd_ipsec_needed(esw, esw_attr, i) &&
463 		    (esw_attr->out_count - esw_attr->split_count > 1))
464 			return false;
465 
466 	return true;
467 }
468 
469 static void
esw_setup_dest_fwd_vport(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_eswitch * esw,struct mlx5_esw_flow_attr * esw_attr,int attr_idx,int dest_idx,bool pkt_reformat)470 esw_setup_dest_fwd_vport(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
471 			 struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *esw_attr,
472 			 int attr_idx, int dest_idx, bool pkt_reformat)
473 {
474 	dest[dest_idx].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
475 	dest[dest_idx].vport.num = esw_attr->dests[attr_idx].vport;
476 	if (MLX5_CAP_ESW(esw->dev, merged_eswitch)) {
477 		dest[dest_idx].vport.vhca_id =
478 			MLX5_CAP_GEN(esw_attr->dests[attr_idx].mdev, vhca_id);
479 		dest[dest_idx].vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
480 		if (dest[dest_idx].vport.num == MLX5_VPORT_UPLINK &&
481 		    mlx5_lag_is_mpesw(esw->dev))
482 			dest[dest_idx].type = MLX5_FLOW_DESTINATION_TYPE_UPLINK;
483 	}
484 	if (esw_attr->dests[attr_idx].flags & MLX5_ESW_DEST_ENCAP_VALID) {
485 		if (pkt_reformat) {
486 			flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
487 			flow_act->pkt_reformat = esw_attr->dests[attr_idx].pkt_reformat;
488 		}
489 		dest[dest_idx].vport.flags |= MLX5_FLOW_DEST_VPORT_REFORMAT_ID;
490 		dest[dest_idx].vport.pkt_reformat = esw_attr->dests[attr_idx].pkt_reformat;
491 	}
492 }
493 
494 static void
esw_setup_dest_fwd_ipsec(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_eswitch * esw,struct mlx5_esw_flow_attr * esw_attr,int attr_idx,int dest_idx,bool pkt_reformat)495 esw_setup_dest_fwd_ipsec(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
496 			 struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *esw_attr,
497 			 int attr_idx, int dest_idx, bool pkt_reformat)
498 {
499 	dest[dest_idx].ft = esw->offloads.ft_ipsec_tx_pol;
500 	dest[dest_idx].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
501 	if (pkt_reformat &&
502 	    esw_attr->dests[attr_idx].flags & MLX5_ESW_DEST_ENCAP_VALID) {
503 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
504 		flow_act->pkt_reformat = esw_attr->dests[attr_idx].pkt_reformat;
505 	}
506 }
507 
508 static void
esw_setup_vport_dest(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_eswitch * esw,struct mlx5_esw_flow_attr * esw_attr,int attr_idx,int dest_idx,bool pkt_reformat)509 esw_setup_vport_dest(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
510 		     struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *esw_attr,
511 		     int attr_idx, int dest_idx, bool pkt_reformat)
512 {
513 	if (esw_setup_uplink_fwd_ipsec_needed(esw, esw_attr, attr_idx))
514 		esw_setup_dest_fwd_ipsec(dest, flow_act, esw, esw_attr,
515 					 attr_idx, dest_idx, pkt_reformat);
516 	else
517 		esw_setup_dest_fwd_vport(dest, flow_act, esw, esw_attr,
518 					 attr_idx, dest_idx, pkt_reformat);
519 }
520 
521 static int
esw_setup_vport_dests(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_eswitch * esw,struct mlx5_esw_flow_attr * esw_attr,int i)522 esw_setup_vport_dests(struct mlx5_flow_destination *dest, struct mlx5_flow_act *flow_act,
523 		      struct mlx5_eswitch *esw, struct mlx5_esw_flow_attr *esw_attr,
524 		      int i)
525 {
526 	int j;
527 
528 	for (j = esw_attr->split_count; j < esw_attr->out_count; j++, i++)
529 		esw_setup_vport_dest(dest, flow_act, esw, esw_attr, j, i, true);
530 	return i;
531 }
532 
533 static bool
esw_src_port_rewrite_supported(struct mlx5_eswitch * esw)534 esw_src_port_rewrite_supported(struct mlx5_eswitch *esw)
535 {
536 	return MLX5_CAP_GEN(esw->dev, reg_c_preserve) &&
537 	       mlx5_eswitch_vport_match_metadata_enabled(esw) &&
538 	       MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ignore_flow_level);
539 }
540 
541 static bool
esw_dests_to_int_external(struct mlx5_flow_destination * dests,int max_dest)542 esw_dests_to_int_external(struct mlx5_flow_destination *dests, int max_dest)
543 {
544 	bool internal_dest = false, external_dest = false;
545 	int i;
546 
547 	for (i = 0; i < max_dest; i++) {
548 		if (dests[i].type != MLX5_FLOW_DESTINATION_TYPE_VPORT &&
549 		    dests[i].type != MLX5_FLOW_DESTINATION_TYPE_UPLINK)
550 			continue;
551 
552 		/* Uplink dest is external, but considered as internal
553 		 * if there is reformat because firmware uses LB+hairpin to support it.
554 		 */
555 		if (dests[i].vport.num == MLX5_VPORT_UPLINK &&
556 		    !(dests[i].vport.flags & MLX5_FLOW_DEST_VPORT_REFORMAT_ID))
557 			external_dest = true;
558 		else
559 			internal_dest = true;
560 
561 		if (internal_dest && external_dest)
562 			return true;
563 	}
564 
565 	return false;
566 }
567 
568 static int
esw_setup_dests(struct mlx5_flow_destination * dest,struct mlx5_flow_act * flow_act,struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr,struct mlx5_flow_spec * spec,int * i)569 esw_setup_dests(struct mlx5_flow_destination *dest,
570 		struct mlx5_flow_act *flow_act,
571 		struct mlx5_eswitch *esw,
572 		struct mlx5_flow_attr *attr,
573 		struct mlx5_flow_spec *spec,
574 		int *i)
575 {
576 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
577 	struct mlx5_fs_chains *chains = esw_chains(esw);
578 	int err = 0;
579 
580 	if (!mlx5_eswitch_termtbl_required(esw, attr, flow_act, spec) &&
581 	    esw_src_port_rewrite_supported(esw))
582 		attr->flags |= MLX5_ATTR_FLAG_SRC_REWRITE;
583 
584 	if (attr->flags & MLX5_ATTR_FLAG_SLOW_PATH) {
585 		esw_setup_slow_path_dest(dest, flow_act, esw, *i);
586 		(*i)++;
587 		goto out;
588 	}
589 
590 	if (attr->flags & MLX5_ATTR_FLAG_SAMPLE) {
591 		esw_setup_sampler_dest(dest, flow_act, attr->sample_attr.sampler_id, *i);
592 		(*i)++;
593 	} else if (attr->flags & MLX5_ATTR_FLAG_ACCEPT) {
594 		esw_setup_accept_dest(dest, flow_act, chains, *i);
595 		(*i)++;
596 	} else if (attr->flags & MLX5_ATTR_FLAG_MTU) {
597 		err = esw_setup_mtu_dest(dest, &attr->meter_attr, *i);
598 		(*i)++;
599 	} else if (esw_is_indir_table(esw, attr)) {
600 		err = esw_setup_indir_table(dest, flow_act, esw, attr, i);
601 	} else if (esw_is_chain_src_port_rewrite(esw, esw_attr)) {
602 		err = esw_setup_chain_src_port_rewrite(dest, flow_act, esw, chains, attr, i);
603 	} else {
604 		*i = esw_setup_vport_dests(dest, flow_act, esw, esw_attr, *i);
605 
606 		if (attr->dest_ft) {
607 			err = esw_setup_ft_dest(dest, flow_act, esw, attr, *i);
608 			(*i)++;
609 		} else if (attr->dest_chain) {
610 			err = esw_setup_chain_dest(dest, flow_act, chains, attr->dest_chain,
611 						   1, 0, *i);
612 			(*i)++;
613 		}
614 	}
615 
616 	if (attr->extra_split_ft) {
617 		flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
618 		dest[*i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
619 		dest[*i].ft = attr->extra_split_ft;
620 		(*i)++;
621 	}
622 
623 out:
624 	return err;
625 }
626 
627 static void
esw_cleanup_dests(struct mlx5_eswitch * esw,struct mlx5_flow_attr * attr)628 esw_cleanup_dests(struct mlx5_eswitch *esw,
629 		  struct mlx5_flow_attr *attr)
630 {
631 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
632 	struct mlx5_fs_chains *chains = esw_chains(esw);
633 
634 	if (attr->dest_ft) {
635 		esw_cleanup_decap_indir(esw, attr);
636 	} else if (!mlx5e_tc_attr_flags_skip(attr->flags)) {
637 		if (attr->dest_chain)
638 			esw_cleanup_chain_dest(chains, attr->dest_chain, 1, 0);
639 		else if (esw_is_indir_table(esw, attr))
640 			esw_cleanup_indir_table(esw, attr);
641 		else if (esw_is_chain_src_port_rewrite(esw, esw_attr))
642 			esw_cleanup_chain_src_port_rewrite(esw, attr);
643 	}
644 }
645 
646 static void
esw_setup_meter(struct mlx5_flow_attr * attr,struct mlx5_flow_act * flow_act)647 esw_setup_meter(struct mlx5_flow_attr *attr, struct mlx5_flow_act *flow_act)
648 {
649 	struct mlx5e_flow_meter_handle *meter;
650 
651 	meter = attr->meter_attr.meter;
652 	flow_act->exe_aso.type = attr->exe_aso_type;
653 	flow_act->exe_aso.object_id = meter->obj_id;
654 	flow_act->exe_aso.flow_meter.meter_idx = meter->idx;
655 	flow_act->exe_aso.flow_meter.init_color = MLX5_FLOW_METER_COLOR_GREEN;
656 	/* use metadata reg 5 for packet color */
657 	flow_act->exe_aso.return_reg_id = 5;
658 }
659 
660 struct mlx5_flow_handle *
mlx5_eswitch_add_offloaded_rule(struct mlx5_eswitch * esw,struct mlx5_flow_spec * spec,struct mlx5_flow_attr * attr)661 mlx5_eswitch_add_offloaded_rule(struct mlx5_eswitch *esw,
662 				struct mlx5_flow_spec *spec,
663 				struct mlx5_flow_attr *attr)
664 {
665 	struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND, };
666 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
667 	struct mlx5_fs_chains *chains = esw_chains(esw);
668 	bool split = !!(esw_attr->split_count);
669 	struct mlx5_vport_tbl_attr fwd_attr;
670 	struct mlx5_flow_destination *dest;
671 	struct mlx5_flow_handle *rule;
672 	struct mlx5_flow_table *fdb;
673 	int i = 0;
674 
675 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
676 		return ERR_PTR(-EOPNOTSUPP);
677 
678 	if (!mlx5_eswitch_vlan_actions_supported(esw->dev, 1))
679 		return ERR_PTR(-EOPNOTSUPP);
680 
681 	if (!esw_flow_dests_fwd_ipsec_check(esw, esw_attr))
682 		return ERR_PTR(-EOPNOTSUPP);
683 
684 	dest = kcalloc(MLX5_MAX_FLOW_FWD_VPORTS + 1, sizeof(*dest), GFP_KERNEL);
685 	if (!dest)
686 		return ERR_PTR(-ENOMEM);
687 
688 	flow_act.action = attr->action;
689 
690 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
691 		flow_act.vlan[0].ethtype = ntohs(esw_attr->vlan_proto[0]);
692 		flow_act.vlan[0].vid = esw_attr->vlan_vid[0];
693 		flow_act.vlan[0].prio = esw_attr->vlan_prio[0];
694 		if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
695 			flow_act.vlan[1].ethtype = ntohs(esw_attr->vlan_proto[1]);
696 			flow_act.vlan[1].vid = esw_attr->vlan_vid[1];
697 			flow_act.vlan[1].prio = esw_attr->vlan_prio[1];
698 		}
699 	}
700 
701 	mlx5_eswitch_set_rule_flow_source(esw, spec, esw_attr);
702 
703 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
704 		int err;
705 
706 		err = esw_setup_dests(dest, &flow_act, esw, attr, spec, &i);
707 		if (err) {
708 			rule = ERR_PTR(err);
709 			goto err_create_goto_table;
710 		}
711 
712 		/* Header rewrite with combined wire+loopback in FDB is not allowed */
713 		if ((flow_act.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) &&
714 		    esw_dests_to_int_external(dest, i)) {
715 			esw_warn(esw->dev,
716 				 "FDB: Header rewrite with forwarding to both internal and external dests is not allowed\n");
717 			rule = ERR_PTR(-EINVAL);
718 			goto err_esw_get;
719 		}
720 	}
721 
722 	if (esw_attr->decap_pkt_reformat)
723 		flow_act.pkt_reformat = esw_attr->decap_pkt_reformat;
724 
725 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
726 		dest[i].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
727 		dest[i].counter_id = mlx5_fc_id(attr->counter);
728 		i++;
729 	}
730 
731 	if (attr->outer_match_level != MLX5_MATCH_NONE)
732 		spec->match_criteria_enable |= MLX5_MATCH_OUTER_HEADERS;
733 	if (attr->inner_match_level != MLX5_MATCH_NONE)
734 		spec->match_criteria_enable |= MLX5_MATCH_INNER_HEADERS;
735 
736 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
737 		flow_act.modify_hdr = attr->modify_hdr;
738 
739 	if ((flow_act.action & MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO) &&
740 	    attr->exe_aso_type == MLX5_EXE_ASO_FLOW_METER)
741 		esw_setup_meter(attr, &flow_act);
742 
743 	if (split) {
744 		fwd_attr.chain = attr->chain;
745 		fwd_attr.prio = attr->prio;
746 		fwd_attr.vport = esw_attr->in_rep->vport;
747 		fwd_attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
748 
749 		fdb = mlx5_esw_vporttbl_get(esw, &fwd_attr);
750 	} else {
751 		if (attr->chain || attr->prio)
752 			fdb = mlx5_chains_get_table(chains, attr->chain,
753 						    attr->prio, 0);
754 		else
755 			fdb = attr->ft;
756 
757 		if (!(attr->flags & MLX5_ATTR_FLAG_NO_IN_PORT))
758 			mlx5_eswitch_set_rule_source_port(esw, spec, attr,
759 							  esw_attr->in_mdev->priv.eswitch,
760 							  esw_attr->in_rep->vport);
761 	}
762 	if (IS_ERR(fdb)) {
763 		rule = ERR_CAST(fdb);
764 		goto err_esw_get;
765 	}
766 
767 	if (!i) {
768 		kfree(dest);
769 		dest = NULL;
770 	}
771 
772 	if (mlx5_eswitch_termtbl_required(esw, attr, &flow_act, spec))
773 		rule = mlx5_eswitch_add_termtbl_rule(esw, fdb, spec, esw_attr,
774 						     &flow_act, dest, i);
775 	else
776 		rule = mlx5_add_flow_rules(fdb, spec, &flow_act, dest, i);
777 	if (IS_ERR(rule))
778 		goto err_add_rule;
779 	else
780 		atomic64_inc(&esw->offloads.num_flows);
781 
782 	kfree(dest);
783 	return rule;
784 
785 err_add_rule:
786 	if (split)
787 		mlx5_esw_vporttbl_put(esw, &fwd_attr);
788 	else if (attr->chain || attr->prio)
789 		mlx5_chains_put_table(chains, attr->chain, attr->prio, 0);
790 err_esw_get:
791 	esw_cleanup_dests(esw, attr);
792 err_create_goto_table:
793 	kfree(dest);
794 	return rule;
795 }
796 
797 struct mlx5_flow_handle *
mlx5_eswitch_add_fwd_rule(struct mlx5_eswitch * esw,struct mlx5_flow_spec * spec,struct mlx5_flow_attr * attr)798 mlx5_eswitch_add_fwd_rule(struct mlx5_eswitch *esw,
799 			  struct mlx5_flow_spec *spec,
800 			  struct mlx5_flow_attr *attr)
801 {
802 	struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND, };
803 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
804 	struct mlx5_fs_chains *chains = esw_chains(esw);
805 	struct mlx5_vport_tbl_attr fwd_attr;
806 	struct mlx5_flow_destination *dest;
807 	struct mlx5_flow_table *fast_fdb;
808 	struct mlx5_flow_table *fwd_fdb;
809 	struct mlx5_flow_handle *rule;
810 	int i, err = 0;
811 
812 	dest = kcalloc(MLX5_MAX_FLOW_FWD_VPORTS + 1, sizeof(*dest), GFP_KERNEL);
813 	if (!dest)
814 		return ERR_PTR(-ENOMEM);
815 
816 	fast_fdb = mlx5_chains_get_table(chains, attr->chain, attr->prio, 0);
817 	if (IS_ERR(fast_fdb)) {
818 		rule = ERR_CAST(fast_fdb);
819 		goto err_get_fast;
820 	}
821 
822 	fwd_attr.chain = attr->chain;
823 	fwd_attr.prio = attr->prio;
824 	fwd_attr.vport = esw_attr->in_rep->vport;
825 	fwd_attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
826 	fwd_fdb = mlx5_esw_vporttbl_get(esw, &fwd_attr);
827 	if (IS_ERR(fwd_fdb)) {
828 		rule = ERR_CAST(fwd_fdb);
829 		goto err_get_fwd;
830 	}
831 
832 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
833 	for (i = 0; i < esw_attr->split_count; i++) {
834 		if (esw_attr->dests[i].flags & MLX5_ESW_DEST_CHAIN_WITH_SRC_PORT_CHANGE)
835 			/* Source port rewrite (forward to ovs internal port or statck device) isn't
836 			 * supported in the rule of split action.
837 			 */
838 			err = -EOPNOTSUPP;
839 		else
840 			esw_setup_vport_dest(dest, &flow_act, esw, esw_attr, i, i, false);
841 
842 		if (err) {
843 			rule = ERR_PTR(err);
844 			goto err_chain_src_rewrite;
845 		}
846 	}
847 	dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
848 	dest[i].ft = fwd_fdb;
849 	i++;
850 
851 	mlx5_eswitch_set_rule_source_port(esw, spec, attr,
852 					  esw_attr->in_mdev->priv.eswitch,
853 					  esw_attr->in_rep->vport);
854 
855 	if (attr->outer_match_level != MLX5_MATCH_NONE)
856 		spec->match_criteria_enable |= MLX5_MATCH_OUTER_HEADERS;
857 
858 	flow_act.flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
859 	rule = mlx5_add_flow_rules(fast_fdb, spec, &flow_act, dest, i);
860 
861 	if (IS_ERR(rule)) {
862 		i = esw_attr->split_count;
863 		goto err_chain_src_rewrite;
864 	}
865 
866 	atomic64_inc(&esw->offloads.num_flows);
867 
868 	kfree(dest);
869 	return rule;
870 err_chain_src_rewrite:
871 	mlx5_esw_vporttbl_put(esw, &fwd_attr);
872 err_get_fwd:
873 	mlx5_chains_put_table(chains, attr->chain, attr->prio, 0);
874 err_get_fast:
875 	kfree(dest);
876 	return rule;
877 }
878 
879 static void
__mlx5_eswitch_del_rule(struct mlx5_eswitch * esw,struct mlx5_flow_handle * rule,struct mlx5_flow_attr * attr,bool fwd_rule)880 __mlx5_eswitch_del_rule(struct mlx5_eswitch *esw,
881 			struct mlx5_flow_handle *rule,
882 			struct mlx5_flow_attr *attr,
883 			bool fwd_rule)
884 {
885 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
886 	struct mlx5_fs_chains *chains = esw_chains(esw);
887 	bool split = (esw_attr->split_count > 0);
888 	struct mlx5_vport_tbl_attr fwd_attr;
889 	int i;
890 
891 	mlx5_del_flow_rules(rule);
892 
893 	if (!mlx5e_tc_attr_flags_skip(attr->flags)) {
894 		/* unref the term table */
895 		for (i = 0; i < MLX5_MAX_FLOW_FWD_VPORTS; i++) {
896 			if (esw_attr->dests[i].termtbl)
897 				mlx5_eswitch_termtbl_put(esw, esw_attr->dests[i].termtbl);
898 		}
899 	}
900 
901 	atomic64_dec(&esw->offloads.num_flows);
902 
903 	if (fwd_rule || split) {
904 		fwd_attr.chain = attr->chain;
905 		fwd_attr.prio = attr->prio;
906 		fwd_attr.vport = esw_attr->in_rep->vport;
907 		fwd_attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
908 	}
909 
910 	if (fwd_rule)  {
911 		mlx5_esw_vporttbl_put(esw, &fwd_attr);
912 		mlx5_chains_put_table(chains, attr->chain, attr->prio, 0);
913 	} else {
914 		if (split)
915 			mlx5_esw_vporttbl_put(esw, &fwd_attr);
916 		else if (attr->chain || attr->prio)
917 			mlx5_chains_put_table(chains, attr->chain, attr->prio, 0);
918 		esw_cleanup_dests(esw, attr);
919 	}
920 }
921 
922 void
mlx5_eswitch_del_offloaded_rule(struct mlx5_eswitch * esw,struct mlx5_flow_handle * rule,struct mlx5_flow_attr * attr)923 mlx5_eswitch_del_offloaded_rule(struct mlx5_eswitch *esw,
924 				struct mlx5_flow_handle *rule,
925 				struct mlx5_flow_attr *attr)
926 {
927 	__mlx5_eswitch_del_rule(esw, rule, attr, false);
928 }
929 
930 void
mlx5_eswitch_del_fwd_rule(struct mlx5_eswitch * esw,struct mlx5_flow_handle * rule,struct mlx5_flow_attr * attr)931 mlx5_eswitch_del_fwd_rule(struct mlx5_eswitch *esw,
932 			  struct mlx5_flow_handle *rule,
933 			  struct mlx5_flow_attr *attr)
934 {
935 	__mlx5_eswitch_del_rule(esw, rule, attr, true);
936 }
937 
938 struct mlx5_flow_handle *
mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch * on_esw,struct mlx5_eswitch * from_esw,struct mlx5_eswitch_rep * rep,u32 sqn)939 mlx5_eswitch_add_send_to_vport_rule(struct mlx5_eswitch *on_esw,
940 				    struct mlx5_eswitch *from_esw,
941 				    struct mlx5_eswitch_rep *rep,
942 				    u32 sqn)
943 {
944 	struct mlx5_flow_act flow_act = {0};
945 	struct mlx5_flow_destination dest = {};
946 	struct mlx5_flow_handle *flow_rule;
947 	struct mlx5_flow_spec *spec;
948 	void *misc;
949 	u16 vport;
950 
951 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
952 	if (!spec) {
953 		flow_rule = ERR_PTR(-ENOMEM);
954 		goto out;
955 	}
956 
957 	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
958 	MLX5_SET(fte_match_set_misc, misc, source_sqn, sqn);
959 
960 	misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
961 	MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_sqn);
962 
963 	spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
964 
965 	/* source vport is the esw manager */
966 	vport = from_esw->manager_vport;
967 
968 	if (mlx5_eswitch_vport_match_metadata_enabled(on_esw)) {
969 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
970 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
971 			 mlx5_eswitch_get_vport_metadata_for_match(from_esw, vport));
972 
973 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
974 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
975 			 mlx5_eswitch_get_vport_metadata_mask());
976 
977 		spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS_2;
978 	} else {
979 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
980 		MLX5_SET(fte_match_set_misc, misc, source_port, vport);
981 
982 		if (MLX5_CAP_ESW(on_esw->dev, merged_eswitch))
983 			MLX5_SET(fte_match_set_misc, misc, source_eswitch_owner_vhca_id,
984 				 MLX5_CAP_GEN(from_esw->dev, vhca_id));
985 
986 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
987 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
988 
989 		if (MLX5_CAP_ESW(on_esw->dev, merged_eswitch))
990 			MLX5_SET_TO_ONES(fte_match_set_misc, misc,
991 					 source_eswitch_owner_vhca_id);
992 
993 		spec->match_criteria_enable |= MLX5_MATCH_MISC_PARAMETERS;
994 	}
995 
996 	dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
997 	dest.vport.num = rep->vport;
998 	dest.vport.vhca_id = MLX5_CAP_GEN(rep->esw->dev, vhca_id);
999 	dest.vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
1000 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1001 
1002 	if (rep->vport == MLX5_VPORT_UPLINK &&
1003 	    on_esw == from_esw && on_esw->offloads.ft_ipsec_tx_pol) {
1004 		dest.ft = on_esw->offloads.ft_ipsec_tx_pol;
1005 		flow_act.flags = FLOW_ACT_IGNORE_FLOW_LEVEL;
1006 		dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1007 	} else {
1008 		dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1009 		dest.vport.num = rep->vport;
1010 		dest.vport.vhca_id = MLX5_CAP_GEN(rep->esw->dev, vhca_id);
1011 		dest.vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
1012 	}
1013 
1014 	if (MLX5_CAP_ESW_FLOWTABLE(on_esw->dev, flow_source) &&
1015 	    rep->vport == MLX5_VPORT_UPLINK)
1016 		spec->flow_context.flow_source = MLX5_FLOW_CONTEXT_FLOW_SOURCE_LOCAL_VPORT;
1017 
1018 	flow_rule = mlx5_add_flow_rules(mlx5_eswitch_get_slow_fdb(on_esw),
1019 					spec, &flow_act, &dest, 1);
1020 	if (IS_ERR(flow_rule))
1021 		esw_warn(on_esw->dev, "FDB: Failed to add send to vport rule err %ld\n",
1022 			 PTR_ERR(flow_rule));
1023 out:
1024 	kvfree(spec);
1025 	return flow_rule;
1026 }
1027 EXPORT_SYMBOL(mlx5_eswitch_add_send_to_vport_rule);
1028 
mlx5_eswitch_del_send_to_vport_rule(struct mlx5_flow_handle * rule)1029 void mlx5_eswitch_del_send_to_vport_rule(struct mlx5_flow_handle *rule)
1030 {
1031 	mlx5_del_flow_rules(rule);
1032 }
1033 
mlx5_eswitch_del_send_to_vport_meta_rule(struct mlx5_flow_handle * rule)1034 void mlx5_eswitch_del_send_to_vport_meta_rule(struct mlx5_flow_handle *rule)
1035 {
1036 	if (rule)
1037 		mlx5_del_flow_rules(rule);
1038 }
1039 
1040 struct mlx5_flow_handle *
mlx5_eswitch_add_send_to_vport_meta_rule(struct mlx5_eswitch * esw,u16 vport_num)1041 mlx5_eswitch_add_send_to_vport_meta_rule(struct mlx5_eswitch *esw, u16 vport_num)
1042 {
1043 	struct mlx5_flow_destination dest = {};
1044 	struct mlx5_flow_act flow_act = {0};
1045 	struct mlx5_flow_handle *flow_rule;
1046 	struct mlx5_flow_spec *spec;
1047 
1048 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1049 	if (!spec)
1050 		return ERR_PTR(-ENOMEM);
1051 
1052 	MLX5_SET(fte_match_param, spec->match_criteria,
1053 		 misc_parameters_2.metadata_reg_c_0, mlx5_eswitch_get_vport_metadata_mask());
1054 	MLX5_SET(fte_match_param, spec->match_criteria,
1055 		 misc_parameters_2.metadata_reg_c_1, ESW_TUN_MASK);
1056 	MLX5_SET(fte_match_param, spec->match_value, misc_parameters_2.metadata_reg_c_1,
1057 		 ESW_TUN_SLOW_TABLE_GOTO_VPORT_MARK);
1058 
1059 	spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
1060 	dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1061 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1062 
1063 	MLX5_SET(fte_match_param, spec->match_value, misc_parameters_2.metadata_reg_c_0,
1064 		 mlx5_eswitch_get_vport_metadata_for_match(esw, vport_num));
1065 	dest.vport.num = vport_num;
1066 
1067 	flow_rule = mlx5_add_flow_rules(mlx5_eswitch_get_slow_fdb(esw),
1068 					spec, &flow_act, &dest, 1);
1069 	if (IS_ERR(flow_rule))
1070 		esw_warn(esw->dev, "FDB: Failed to add send to vport meta rule vport %d, err %ld\n",
1071 			 vport_num, PTR_ERR(flow_rule));
1072 
1073 	kvfree(spec);
1074 	return flow_rule;
1075 }
1076 
mlx5_eswitch_reg_c1_loopback_supported(struct mlx5_eswitch * esw)1077 static bool mlx5_eswitch_reg_c1_loopback_supported(struct mlx5_eswitch *esw)
1078 {
1079 	return MLX5_CAP_ESW_FLOWTABLE(esw->dev, fdb_to_vport_reg_c_id) &
1080 	       MLX5_FDB_TO_VPORT_REG_C_1;
1081 }
1082 
esw_set_passing_vport_metadata(struct mlx5_eswitch * esw,bool enable)1083 static int esw_set_passing_vport_metadata(struct mlx5_eswitch *esw, bool enable)
1084 {
1085 	u32 out[MLX5_ST_SZ_DW(query_esw_vport_context_out)] = {};
1086 	u32 min[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {};
1087 	u32 in[MLX5_ST_SZ_DW(query_esw_vport_context_in)] = {};
1088 	u8 curr, wanted;
1089 	int err;
1090 
1091 	if (!mlx5_eswitch_reg_c1_loopback_supported(esw) &&
1092 	    !mlx5_eswitch_vport_match_metadata_enabled(esw))
1093 		return 0;
1094 
1095 	MLX5_SET(query_esw_vport_context_in, in, opcode,
1096 		 MLX5_CMD_OP_QUERY_ESW_VPORT_CONTEXT);
1097 	err = mlx5_cmd_exec_inout(esw->dev, query_esw_vport_context, in, out);
1098 	if (err)
1099 		return err;
1100 
1101 	curr = MLX5_GET(query_esw_vport_context_out, out,
1102 			esw_vport_context.fdb_to_vport_reg_c_id);
1103 	wanted = MLX5_FDB_TO_VPORT_REG_C_0;
1104 	if (mlx5_eswitch_reg_c1_loopback_supported(esw))
1105 		wanted |= MLX5_FDB_TO_VPORT_REG_C_1;
1106 
1107 	if (enable)
1108 		curr |= wanted;
1109 	else
1110 		curr &= ~wanted;
1111 
1112 	MLX5_SET(modify_esw_vport_context_in, min,
1113 		 esw_vport_context.fdb_to_vport_reg_c_id, curr);
1114 	MLX5_SET(modify_esw_vport_context_in, min,
1115 		 field_select.fdb_to_vport_reg_c_id, 1);
1116 
1117 	err = mlx5_eswitch_modify_esw_vport_context(esw->dev, 0, false, min);
1118 	if (!err) {
1119 		if (enable && (curr & MLX5_FDB_TO_VPORT_REG_C_1))
1120 			esw->flags |= MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED;
1121 		else
1122 			esw->flags &= ~MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED;
1123 	}
1124 
1125 	return err;
1126 }
1127 
peer_miss_rules_setup(struct mlx5_eswitch * esw,struct mlx5_core_dev * peer_dev,struct mlx5_flow_spec * spec,struct mlx5_flow_destination * dest)1128 static void peer_miss_rules_setup(struct mlx5_eswitch *esw,
1129 				  struct mlx5_core_dev *peer_dev,
1130 				  struct mlx5_flow_spec *spec,
1131 				  struct mlx5_flow_destination *dest)
1132 {
1133 	void *misc;
1134 
1135 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1136 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1137 				    misc_parameters_2);
1138 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1139 			 mlx5_eswitch_get_vport_metadata_mask());
1140 
1141 		spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
1142 	} else {
1143 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1144 				    misc_parameters);
1145 
1146 		MLX5_SET(fte_match_set_misc, misc, source_eswitch_owner_vhca_id,
1147 			 MLX5_CAP_GEN(peer_dev, vhca_id));
1148 
1149 		spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
1150 
1151 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1152 				    misc_parameters);
1153 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
1154 		MLX5_SET_TO_ONES(fte_match_set_misc, misc,
1155 				 source_eswitch_owner_vhca_id);
1156 	}
1157 
1158 	dest->type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1159 	dest->vport.num = peer_dev->priv.eswitch->manager_vport;
1160 	dest->vport.vhca_id = MLX5_CAP_GEN(peer_dev, vhca_id);
1161 	dest->vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
1162 }
1163 
esw_set_peer_miss_rule_source_port(struct mlx5_eswitch * esw,struct mlx5_eswitch * peer_esw,struct mlx5_flow_spec * spec,u16 vport)1164 static void esw_set_peer_miss_rule_source_port(struct mlx5_eswitch *esw,
1165 					       struct mlx5_eswitch *peer_esw,
1166 					       struct mlx5_flow_spec *spec,
1167 					       u16 vport)
1168 {
1169 	void *misc;
1170 
1171 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1172 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1173 				    misc_parameters_2);
1174 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1175 			 mlx5_eswitch_get_vport_metadata_for_match(peer_esw,
1176 								   vport));
1177 	} else {
1178 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1179 				    misc_parameters);
1180 		MLX5_SET(fte_match_set_misc, misc, source_port, vport);
1181 	}
1182 }
1183 
esw_add_fdb_peer_miss_rules(struct mlx5_eswitch * esw,struct mlx5_core_dev * peer_dev)1184 static int esw_add_fdb_peer_miss_rules(struct mlx5_eswitch *esw,
1185 				       struct mlx5_core_dev *peer_dev)
1186 {
1187 	struct mlx5_flow_destination dest = {};
1188 	struct mlx5_flow_act flow_act = {0};
1189 	struct mlx5_flow_handle **flows;
1190 	/* total vports is the same for both e-switches */
1191 	int nvports = esw->total_vports;
1192 	struct mlx5_flow_handle *flow;
1193 	struct mlx5_flow_spec *spec;
1194 	struct mlx5_vport *vport;
1195 	int err, pfindex;
1196 	unsigned long i;
1197 	void *misc;
1198 
1199 	if (!MLX5_VPORT_MANAGER(esw->dev) && !mlx5_core_is_ecpf_esw_manager(esw->dev))
1200 		return 0;
1201 
1202 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1203 	if (!spec)
1204 		return -ENOMEM;
1205 
1206 	peer_miss_rules_setup(esw, peer_dev, spec, &dest);
1207 
1208 	flows = kvcalloc(nvports, sizeof(*flows), GFP_KERNEL);
1209 	if (!flows) {
1210 		err = -ENOMEM;
1211 		goto alloc_flows_err;
1212 	}
1213 
1214 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1215 	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1216 			    misc_parameters);
1217 
1218 	if (mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1219 		vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_PF);
1220 		esw_set_peer_miss_rule_source_port(esw, peer_dev->priv.eswitch,
1221 						   spec, MLX5_VPORT_PF);
1222 
1223 		flow = mlx5_add_flow_rules(mlx5_eswitch_get_slow_fdb(esw),
1224 					   spec, &flow_act, &dest, 1);
1225 		if (IS_ERR(flow)) {
1226 			err = PTR_ERR(flow);
1227 			goto add_pf_flow_err;
1228 		}
1229 		flows[vport->index] = flow;
1230 	}
1231 
1232 	if (mlx5_ecpf_vport_exists(esw->dev)) {
1233 		vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_ECPF);
1234 		MLX5_SET(fte_match_set_misc, misc, source_port, MLX5_VPORT_ECPF);
1235 		flow = mlx5_add_flow_rules(mlx5_eswitch_get_slow_fdb(esw),
1236 					   spec, &flow_act, &dest, 1);
1237 		if (IS_ERR(flow)) {
1238 			err = PTR_ERR(flow);
1239 			goto add_ecpf_flow_err;
1240 		}
1241 		flows[vport->index] = flow;
1242 	}
1243 
1244 	mlx5_esw_for_each_vf_vport(esw, i, vport, mlx5_core_max_vfs(esw->dev)) {
1245 		esw_set_peer_miss_rule_source_port(esw,
1246 						   peer_dev->priv.eswitch,
1247 						   spec, vport->vport);
1248 
1249 		flow = mlx5_add_flow_rules(mlx5_eswitch_get_slow_fdb(esw),
1250 					   spec, &flow_act, &dest, 1);
1251 		if (IS_ERR(flow)) {
1252 			err = PTR_ERR(flow);
1253 			goto add_vf_flow_err;
1254 		}
1255 		flows[vport->index] = flow;
1256 	}
1257 
1258 	if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1259 		mlx5_esw_for_each_ec_vf_vport(esw, i, vport, mlx5_core_max_ec_vfs(esw->dev)) {
1260 			if (i >= mlx5_core_max_ec_vfs(peer_dev))
1261 				break;
1262 			esw_set_peer_miss_rule_source_port(esw, peer_dev->priv.eswitch,
1263 							   spec, vport->vport);
1264 			flow = mlx5_add_flow_rules(esw->fdb_table.offloads.slow_fdb,
1265 						   spec, &flow_act, &dest, 1);
1266 			if (IS_ERR(flow)) {
1267 				err = PTR_ERR(flow);
1268 				goto add_ec_vf_flow_err;
1269 			}
1270 			flows[vport->index] = flow;
1271 		}
1272 	}
1273 
1274 	pfindex = mlx5_get_dev_index(peer_dev);
1275 	if (pfindex >= MLX5_MAX_PORTS) {
1276 		esw_warn(esw->dev, "Peer dev index(%d) is over the max num defined(%d)\n",
1277 			 pfindex, MLX5_MAX_PORTS);
1278 		err = -EINVAL;
1279 		goto add_ec_vf_flow_err;
1280 	}
1281 	esw->fdb_table.offloads.peer_miss_rules[pfindex] = flows;
1282 
1283 	kvfree(spec);
1284 	return 0;
1285 
1286 add_ec_vf_flow_err:
1287 	mlx5_esw_for_each_ec_vf_vport(esw, i, vport, mlx5_core_max_ec_vfs(esw->dev)) {
1288 		if (!flows[vport->index])
1289 			continue;
1290 		mlx5_del_flow_rules(flows[vport->index]);
1291 	}
1292 add_vf_flow_err:
1293 	mlx5_esw_for_each_vf_vport(esw, i, vport, mlx5_core_max_vfs(esw->dev)) {
1294 		if (!flows[vport->index])
1295 			continue;
1296 		mlx5_del_flow_rules(flows[vport->index]);
1297 	}
1298 	if (mlx5_ecpf_vport_exists(esw->dev)) {
1299 		vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_ECPF);
1300 		mlx5_del_flow_rules(flows[vport->index]);
1301 	}
1302 add_ecpf_flow_err:
1303 	if (mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1304 		vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_PF);
1305 		mlx5_del_flow_rules(flows[vport->index]);
1306 	}
1307 add_pf_flow_err:
1308 	esw_warn(esw->dev, "FDB: Failed to add peer miss flow rule err %d\n", err);
1309 	kvfree(flows);
1310 alloc_flows_err:
1311 	kvfree(spec);
1312 	return err;
1313 }
1314 
esw_del_fdb_peer_miss_rules(struct mlx5_eswitch * esw,struct mlx5_core_dev * peer_dev)1315 static void esw_del_fdb_peer_miss_rules(struct mlx5_eswitch *esw,
1316 					struct mlx5_core_dev *peer_dev)
1317 {
1318 	u16 peer_index = mlx5_get_dev_index(peer_dev);
1319 	struct mlx5_flow_handle **flows;
1320 	struct mlx5_vport *vport;
1321 	unsigned long i;
1322 
1323 	flows = esw->fdb_table.offloads.peer_miss_rules[peer_index];
1324 	if (!flows)
1325 		return;
1326 
1327 	if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1328 		mlx5_esw_for_each_ec_vf_vport(esw, i, vport, mlx5_core_max_ec_vfs(esw->dev)) {
1329 			/* The flow for a particular vport could be NULL if the other ECPF
1330 			 * has fewer or no VFs enabled
1331 			 */
1332 			if (!flows[vport->index])
1333 				continue;
1334 			mlx5_del_flow_rules(flows[vport->index]);
1335 		}
1336 	}
1337 
1338 	mlx5_esw_for_each_vf_vport(esw, i, vport, mlx5_core_max_vfs(esw->dev))
1339 		mlx5_del_flow_rules(flows[vport->index]);
1340 
1341 	if (mlx5_ecpf_vport_exists(esw->dev)) {
1342 		vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_ECPF);
1343 		mlx5_del_flow_rules(flows[vport->index]);
1344 	}
1345 
1346 	if (mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1347 		vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_PF);
1348 		mlx5_del_flow_rules(flows[vport->index]);
1349 	}
1350 
1351 	kvfree(flows);
1352 	esw->fdb_table.offloads.peer_miss_rules[peer_index] = NULL;
1353 }
1354 
esw_add_fdb_miss_rule(struct mlx5_eswitch * esw)1355 static int esw_add_fdb_miss_rule(struct mlx5_eswitch *esw)
1356 {
1357 	struct mlx5_flow_act flow_act = {0};
1358 	struct mlx5_flow_destination dest = {};
1359 	struct mlx5_flow_handle *flow_rule = NULL;
1360 	struct mlx5_flow_spec *spec;
1361 	void *headers_c;
1362 	void *headers_v;
1363 	int err = 0;
1364 	u8 *dmac_c;
1365 	u8 *dmac_v;
1366 
1367 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1368 	if (!spec) {
1369 		err = -ENOMEM;
1370 		goto out;
1371 	}
1372 
1373 	spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
1374 	headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1375 				 outer_headers);
1376 	dmac_c = MLX5_ADDR_OF(fte_match_param, headers_c,
1377 			      outer_headers.dmac_47_16);
1378 	dmac_c[0] = 0x01;
1379 
1380 	dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1381 	dest.vport.num = esw->manager_vport;
1382 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1383 
1384 	flow_rule = mlx5_add_flow_rules(mlx5_eswitch_get_slow_fdb(esw),
1385 					spec, &flow_act, &dest, 1);
1386 	if (IS_ERR(flow_rule)) {
1387 		err = PTR_ERR(flow_rule);
1388 		esw_warn(esw->dev,  "FDB: Failed to add unicast miss flow rule err %d\n", err);
1389 		goto out;
1390 	}
1391 
1392 	esw->fdb_table.offloads.miss_rule_uni = flow_rule;
1393 
1394 	headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1395 				 outer_headers);
1396 	dmac_v = MLX5_ADDR_OF(fte_match_param, headers_v,
1397 			      outer_headers.dmac_47_16);
1398 	dmac_v[0] = 0x01;
1399 	flow_rule = mlx5_add_flow_rules(mlx5_eswitch_get_slow_fdb(esw),
1400 					spec, &flow_act, &dest, 1);
1401 	if (IS_ERR(flow_rule)) {
1402 		err = PTR_ERR(flow_rule);
1403 		esw_warn(esw->dev, "FDB: Failed to add multicast miss flow rule err %d\n", err);
1404 		mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
1405 		goto out;
1406 	}
1407 
1408 	esw->fdb_table.offloads.miss_rule_multi = flow_rule;
1409 
1410 out:
1411 	kvfree(spec);
1412 	return err;
1413 }
1414 
1415 struct mlx5_flow_handle *
esw_add_restore_rule(struct mlx5_eswitch * esw,u32 tag)1416 esw_add_restore_rule(struct mlx5_eswitch *esw, u32 tag)
1417 {
1418 	struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND, };
1419 	struct mlx5_flow_table *ft = esw->offloads.ft_offloads_restore;
1420 	struct mlx5_flow_context *flow_context;
1421 	struct mlx5_flow_handle *flow_rule;
1422 	struct mlx5_flow_destination dest;
1423 	struct mlx5_flow_spec *spec;
1424 	void *misc;
1425 
1426 	if (!mlx5_eswitch_reg_c1_loopback_supported(esw))
1427 		return ERR_PTR(-EOPNOTSUPP);
1428 
1429 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
1430 	if (!spec)
1431 		return ERR_PTR(-ENOMEM);
1432 
1433 	misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1434 			    misc_parameters_2);
1435 	MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
1436 		 ESW_REG_C0_USER_DATA_METADATA_MASK);
1437 	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1438 			    misc_parameters_2);
1439 	MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0, tag);
1440 	spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
1441 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1442 			  MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
1443 	flow_act.modify_hdr = esw->offloads.restore_copy_hdr_id;
1444 
1445 	flow_context = &spec->flow_context;
1446 	flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
1447 	flow_context->flow_tag = tag;
1448 	dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1449 	dest.ft = esw->offloads.ft_offloads;
1450 
1451 	flow_rule = mlx5_add_flow_rules(ft, spec, &flow_act, &dest, 1);
1452 	kvfree(spec);
1453 
1454 	if (IS_ERR(flow_rule))
1455 		esw_warn(esw->dev,
1456 			 "Failed to create restore rule for tag: %d, err(%d)\n",
1457 			 tag, (int)PTR_ERR(flow_rule));
1458 
1459 	return flow_rule;
1460 }
1461 
1462 #define MAX_PF_SQ 256
1463 #define MAX_SQ_NVPORTS 32
1464 
1465 void
mlx5_esw_set_flow_group_source_port(struct mlx5_eswitch * esw,u32 * flow_group_in,int match_params)1466 mlx5_esw_set_flow_group_source_port(struct mlx5_eswitch *esw,
1467 				    u32 *flow_group_in,
1468 				    int match_params)
1469 {
1470 	void *match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1471 					    flow_group_in,
1472 					    match_criteria);
1473 
1474 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1475 		MLX5_SET(create_flow_group_in, flow_group_in,
1476 			 match_criteria_enable,
1477 			 MLX5_MATCH_MISC_PARAMETERS_2 | match_params);
1478 
1479 		MLX5_SET(fte_match_param, match_criteria,
1480 			 misc_parameters_2.metadata_reg_c_0,
1481 			 mlx5_eswitch_get_vport_metadata_mask());
1482 	} else {
1483 		MLX5_SET(create_flow_group_in, flow_group_in,
1484 			 match_criteria_enable,
1485 			 MLX5_MATCH_MISC_PARAMETERS | match_params);
1486 
1487 		MLX5_SET_TO_ONES(fte_match_param, match_criteria,
1488 				 misc_parameters.source_port);
1489 	}
1490 }
1491 
1492 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
esw_vport_tbl_put(struct mlx5_eswitch * esw)1493 static void esw_vport_tbl_put(struct mlx5_eswitch *esw)
1494 {
1495 	struct mlx5_vport_tbl_attr attr;
1496 	struct mlx5_vport *vport;
1497 	unsigned long i;
1498 
1499 	attr.chain = 0;
1500 	attr.prio = 1;
1501 	mlx5_esw_for_each_vport(esw, i, vport) {
1502 		attr.vport = vport->vport;
1503 		attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
1504 		mlx5_esw_vporttbl_put(esw, &attr);
1505 	}
1506 }
1507 
esw_vport_tbl_get(struct mlx5_eswitch * esw)1508 static int esw_vport_tbl_get(struct mlx5_eswitch *esw)
1509 {
1510 	struct mlx5_vport_tbl_attr attr;
1511 	struct mlx5_flow_table *fdb;
1512 	struct mlx5_vport *vport;
1513 	unsigned long i;
1514 
1515 	attr.chain = 0;
1516 	attr.prio = 1;
1517 	mlx5_esw_for_each_vport(esw, i, vport) {
1518 		attr.vport = vport->vport;
1519 		attr.vport_ns = &mlx5_esw_vport_tbl_mirror_ns;
1520 		fdb = mlx5_esw_vporttbl_get(esw, &attr);
1521 		if (IS_ERR(fdb))
1522 			goto out;
1523 	}
1524 	return 0;
1525 
1526 out:
1527 	esw_vport_tbl_put(esw);
1528 	return PTR_ERR(fdb);
1529 }
1530 
1531 #define fdb_modify_header_fwd_to_table_supported(esw) \
1532 	(MLX5_CAP_ESW_FLOWTABLE((esw)->dev, fdb_modify_header_fwd_to_table))
esw_init_chains_offload_flags(struct mlx5_eswitch * esw,u32 * flags)1533 static void esw_init_chains_offload_flags(struct mlx5_eswitch *esw, u32 *flags)
1534 {
1535 	struct mlx5_core_dev *dev = esw->dev;
1536 
1537 	if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, ignore_flow_level))
1538 		*flags |= MLX5_CHAINS_IGNORE_FLOW_LEVEL_SUPPORTED;
1539 
1540 	if (!MLX5_CAP_ESW_FLOWTABLE(dev, multi_fdb_encap) &&
1541 	    esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE) {
1542 		*flags &= ~MLX5_CHAINS_AND_PRIOS_SUPPORTED;
1543 		esw_warn(dev, "Tc chains and priorities offload aren't supported, update firmware if needed\n");
1544 	} else if (!mlx5_eswitch_reg_c1_loopback_enabled(esw)) {
1545 		*flags &= ~MLX5_CHAINS_AND_PRIOS_SUPPORTED;
1546 		esw_warn(dev, "Tc chains and priorities offload aren't supported\n");
1547 	} else if (!fdb_modify_header_fwd_to_table_supported(esw)) {
1548 		/* Disabled when ttl workaround is needed, e.g
1549 		 * when ESWITCH_IPV4_TTL_MODIFY_ENABLE = true in mlxconfig
1550 		 */
1551 		esw_warn(dev,
1552 			 "Tc chains and priorities offload aren't supported, check firmware version, or mlxconfig settings\n");
1553 		*flags &= ~MLX5_CHAINS_AND_PRIOS_SUPPORTED;
1554 	} else {
1555 		*flags |= MLX5_CHAINS_AND_PRIOS_SUPPORTED;
1556 		esw_info(dev, "Supported tc chains and prios offload\n");
1557 	}
1558 
1559 	if (esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE)
1560 		*flags |= MLX5_CHAINS_FT_TUNNEL_SUPPORTED;
1561 }
1562 
1563 static int
esw_chains_create(struct mlx5_eswitch * esw,struct mlx5_flow_table * miss_fdb)1564 esw_chains_create(struct mlx5_eswitch *esw, struct mlx5_flow_table *miss_fdb)
1565 {
1566 	struct mlx5_core_dev *dev = esw->dev;
1567 	struct mlx5_flow_table *nf_ft, *ft;
1568 	struct mlx5_chains_attr attr = {};
1569 	struct mlx5_fs_chains *chains;
1570 	int err;
1571 
1572 	esw_init_chains_offload_flags(esw, &attr.flags);
1573 	attr.ns = MLX5_FLOW_NAMESPACE_FDB;
1574 	attr.max_grp_num = esw->params.large_group_num;
1575 	attr.default_ft = miss_fdb;
1576 	attr.mapping = esw->offloads.reg_c0_obj_pool;
1577 
1578 	chains = mlx5_chains_create(dev, &attr);
1579 	if (IS_ERR(chains)) {
1580 		err = PTR_ERR(chains);
1581 		esw_warn(dev, "Failed to create fdb chains err(%d)\n", err);
1582 		return err;
1583 	}
1584 	mlx5_chains_print_info(chains);
1585 
1586 	esw->fdb_table.offloads.esw_chains_priv = chains;
1587 
1588 	/* Create tc_end_ft which is the always created ft chain */
1589 	nf_ft = mlx5_chains_get_table(chains, mlx5_chains_get_nf_ft_chain(chains),
1590 				      1, 0);
1591 	if (IS_ERR(nf_ft)) {
1592 		err = PTR_ERR(nf_ft);
1593 		goto nf_ft_err;
1594 	}
1595 
1596 	/* Always open the root for fast path */
1597 	ft = mlx5_chains_get_table(chains, 0, 1, 0);
1598 	if (IS_ERR(ft)) {
1599 		err = PTR_ERR(ft);
1600 		goto level_0_err;
1601 	}
1602 
1603 	/* Open level 1 for split fdb rules now if prios isn't supported  */
1604 	if (!mlx5_chains_prios_supported(chains)) {
1605 		err = esw_vport_tbl_get(esw);
1606 		if (err)
1607 			goto level_1_err;
1608 	}
1609 
1610 	mlx5_chains_set_end_ft(chains, nf_ft);
1611 
1612 	return 0;
1613 
1614 level_1_err:
1615 	mlx5_chains_put_table(chains, 0, 1, 0);
1616 level_0_err:
1617 	mlx5_chains_put_table(chains, mlx5_chains_get_nf_ft_chain(chains), 1, 0);
1618 nf_ft_err:
1619 	mlx5_chains_destroy(chains);
1620 	esw->fdb_table.offloads.esw_chains_priv = NULL;
1621 
1622 	return err;
1623 }
1624 
1625 static void
esw_chains_destroy(struct mlx5_eswitch * esw,struct mlx5_fs_chains * chains)1626 esw_chains_destroy(struct mlx5_eswitch *esw, struct mlx5_fs_chains *chains)
1627 {
1628 	if (!mlx5_chains_prios_supported(chains))
1629 		esw_vport_tbl_put(esw);
1630 	mlx5_chains_put_table(chains, 0, 1, 0);
1631 	mlx5_chains_put_table(chains, mlx5_chains_get_nf_ft_chain(chains), 1, 0);
1632 	mlx5_chains_destroy(chains);
1633 }
1634 
1635 #else /* CONFIG_MLX5_CLS_ACT */
1636 
1637 static int
esw_chains_create(struct mlx5_eswitch * esw,struct mlx5_flow_table * miss_fdb)1638 esw_chains_create(struct mlx5_eswitch *esw, struct mlx5_flow_table *miss_fdb)
1639 { return 0; }
1640 
1641 static void
esw_chains_destroy(struct mlx5_eswitch * esw,struct mlx5_fs_chains * chains)1642 esw_chains_destroy(struct mlx5_eswitch *esw, struct mlx5_fs_chains *chains)
1643 {}
1644 
1645 #endif
1646 
1647 static int
esw_create_send_to_vport_group(struct mlx5_eswitch * esw,struct mlx5_flow_table * fdb,u32 * flow_group_in,int * ix)1648 esw_create_send_to_vport_group(struct mlx5_eswitch *esw,
1649 			       struct mlx5_flow_table *fdb,
1650 			       u32 *flow_group_in,
1651 			       int *ix)
1652 {
1653 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1654 	struct mlx5_flow_group *g;
1655 	void *match_criteria;
1656 	int count, err = 0;
1657 
1658 	memset(flow_group_in, 0, inlen);
1659 
1660 	mlx5_esw_set_flow_group_source_port(esw, flow_group_in, MLX5_MATCH_MISC_PARAMETERS);
1661 
1662 	match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
1663 	MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_sqn);
1664 
1665 	if (!mlx5_eswitch_vport_match_metadata_enabled(esw) &&
1666 	    MLX5_CAP_ESW(esw->dev, merged_eswitch)) {
1667 		MLX5_SET_TO_ONES(fte_match_param, match_criteria,
1668 				 misc_parameters.source_eswitch_owner_vhca_id);
1669 		MLX5_SET(create_flow_group_in, flow_group_in,
1670 			 source_eswitch_owner_vhca_id_valid, 1);
1671 	}
1672 
1673 	/* See comment at table_size calculation */
1674 	count = MLX5_MAX_PORTS * (esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ);
1675 	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
1676 	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, *ix + count - 1);
1677 	*ix += count;
1678 
1679 	g = mlx5_create_flow_group(fdb, flow_group_in);
1680 	if (IS_ERR(g)) {
1681 		err = PTR_ERR(g);
1682 		esw_warn(esw->dev, "Failed to create send-to-vport flow group err(%d)\n", err);
1683 		goto out;
1684 	}
1685 	esw->fdb_table.offloads.send_to_vport_grp = g;
1686 
1687 out:
1688 	return err;
1689 }
1690 
1691 static int
esw_create_meta_send_to_vport_group(struct mlx5_eswitch * esw,struct mlx5_flow_table * fdb,u32 * flow_group_in,int * ix)1692 esw_create_meta_send_to_vport_group(struct mlx5_eswitch *esw,
1693 				    struct mlx5_flow_table *fdb,
1694 				    u32 *flow_group_in,
1695 				    int *ix)
1696 {
1697 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1698 	struct mlx5_flow_group *g;
1699 	void *match_criteria;
1700 	int err = 0;
1701 
1702 	if (!esw_src_port_rewrite_supported(esw))
1703 		return 0;
1704 
1705 	memset(flow_group_in, 0, inlen);
1706 
1707 	MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
1708 		 MLX5_MATCH_MISC_PARAMETERS_2);
1709 
1710 	match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
1711 
1712 	MLX5_SET(fte_match_param, match_criteria,
1713 		 misc_parameters_2.metadata_reg_c_0,
1714 		 mlx5_eswitch_get_vport_metadata_mask());
1715 	MLX5_SET(fte_match_param, match_criteria,
1716 		 misc_parameters_2.metadata_reg_c_1, ESW_TUN_MASK);
1717 
1718 	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, *ix);
1719 	MLX5_SET(create_flow_group_in, flow_group_in,
1720 		 end_flow_index, *ix + esw->total_vports - 1);
1721 	*ix += esw->total_vports;
1722 
1723 	g = mlx5_create_flow_group(fdb, flow_group_in);
1724 	if (IS_ERR(g)) {
1725 		err = PTR_ERR(g);
1726 		esw_warn(esw->dev,
1727 			 "Failed to create send-to-vport meta flow group err(%d)\n", err);
1728 		goto send_vport_meta_err;
1729 	}
1730 	esw->fdb_table.offloads.send_to_vport_meta_grp = g;
1731 
1732 	return 0;
1733 
1734 send_vport_meta_err:
1735 	return err;
1736 }
1737 
1738 static int
esw_create_peer_esw_miss_group(struct mlx5_eswitch * esw,struct mlx5_flow_table * fdb,u32 * flow_group_in,int * ix)1739 esw_create_peer_esw_miss_group(struct mlx5_eswitch *esw,
1740 			       struct mlx5_flow_table *fdb,
1741 			       u32 *flow_group_in,
1742 			       int *ix)
1743 {
1744 	int max_peer_ports = (esw->total_vports - 1) * (MLX5_MAX_PORTS - 1);
1745 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1746 	struct mlx5_flow_group *g;
1747 	void *match_criteria;
1748 	int err = 0;
1749 
1750 	if (!MLX5_CAP_ESW(esw->dev, merged_eswitch))
1751 		return 0;
1752 
1753 	memset(flow_group_in, 0, inlen);
1754 
1755 	mlx5_esw_set_flow_group_source_port(esw, flow_group_in, 0);
1756 
1757 	if (!mlx5_eswitch_vport_match_metadata_enabled(esw)) {
1758 		match_criteria = MLX5_ADDR_OF(create_flow_group_in,
1759 					      flow_group_in,
1760 					      match_criteria);
1761 
1762 		MLX5_SET_TO_ONES(fte_match_param, match_criteria,
1763 				 misc_parameters.source_eswitch_owner_vhca_id);
1764 
1765 		MLX5_SET(create_flow_group_in, flow_group_in,
1766 			 source_eswitch_owner_vhca_id_valid, 1);
1767 	}
1768 
1769 	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, *ix);
1770 	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
1771 		 *ix + max_peer_ports);
1772 	*ix += max_peer_ports + 1;
1773 
1774 	g = mlx5_create_flow_group(fdb, flow_group_in);
1775 	if (IS_ERR(g)) {
1776 		err = PTR_ERR(g);
1777 		esw_warn(esw->dev, "Failed to create peer miss flow group err(%d)\n", err);
1778 		goto out;
1779 	}
1780 	esw->fdb_table.offloads.peer_miss_grp = g;
1781 
1782 out:
1783 	return err;
1784 }
1785 
1786 static int
esw_create_miss_group(struct mlx5_eswitch * esw,struct mlx5_flow_table * fdb,u32 * flow_group_in,int * ix)1787 esw_create_miss_group(struct mlx5_eswitch *esw,
1788 		      struct mlx5_flow_table *fdb,
1789 		      u32 *flow_group_in,
1790 		      int *ix)
1791 {
1792 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1793 	struct mlx5_flow_group *g;
1794 	void *match_criteria;
1795 	int err = 0;
1796 	u8 *dmac;
1797 
1798 	memset(flow_group_in, 0, inlen);
1799 
1800 	MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
1801 		 MLX5_MATCH_OUTER_HEADERS);
1802 	match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
1803 				      match_criteria);
1804 	dmac = MLX5_ADDR_OF(fte_match_param, match_criteria,
1805 			    outer_headers.dmac_47_16);
1806 	dmac[0] = 0x01;
1807 
1808 	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, *ix);
1809 	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
1810 		 *ix + MLX5_ESW_MISS_FLOWS);
1811 
1812 	g = mlx5_create_flow_group(fdb, flow_group_in);
1813 	if (IS_ERR(g)) {
1814 		err = PTR_ERR(g);
1815 		esw_warn(esw->dev, "Failed to create miss flow group err(%d)\n", err);
1816 		goto miss_err;
1817 	}
1818 	esw->fdb_table.offloads.miss_grp = g;
1819 
1820 	err = esw_add_fdb_miss_rule(esw);
1821 	if (err)
1822 		goto miss_rule_err;
1823 
1824 	return 0;
1825 
1826 miss_rule_err:
1827 	mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
1828 miss_err:
1829 	return err;
1830 }
1831 
esw_create_offloads_fdb_tables(struct mlx5_eswitch * esw)1832 static int esw_create_offloads_fdb_tables(struct mlx5_eswitch *esw)
1833 {
1834 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1835 	struct mlx5_flow_table_attr ft_attr = {};
1836 	struct mlx5_core_dev *dev = esw->dev;
1837 	struct mlx5_flow_namespace *root_ns;
1838 	struct mlx5_flow_table *fdb = NULL;
1839 	int table_size, ix = 0, err = 0;
1840 	u32 flags = 0, *flow_group_in;
1841 
1842 	esw_debug(esw->dev, "Create offloads FDB Tables\n");
1843 
1844 	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
1845 	if (!flow_group_in)
1846 		return -ENOMEM;
1847 
1848 	root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
1849 	if (!root_ns) {
1850 		esw_warn(dev, "Failed to get FDB flow namespace\n");
1851 		err = -EOPNOTSUPP;
1852 		goto ns_err;
1853 	}
1854 	esw->fdb_table.offloads.ns = root_ns;
1855 	err = mlx5_flow_namespace_set_mode(root_ns,
1856 					   esw->dev->priv.steering->mode);
1857 	if (err) {
1858 		esw_warn(dev, "Failed to set FDB namespace steering mode\n");
1859 		goto ns_err;
1860 	}
1861 
1862 	/* To be strictly correct:
1863 	 *	MLX5_MAX_PORTS * (esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ)
1864 	 * should be:
1865 	 *	esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ +
1866 	 *	peer_esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ
1867 	 * but as the peer device might not be in switchdev mode it's not
1868 	 * possible. We use the fact that by default FW sets max vfs and max sfs
1869 	 * to the same value on both devices. If it needs to be changed in the future note
1870 	 * the peer miss group should also be created based on the number of
1871 	 * total vports of the peer (currently is also uses esw->total_vports).
1872 	 */
1873 	table_size = MLX5_MAX_PORTS * (esw->total_vports * MAX_SQ_NVPORTS + MAX_PF_SQ) +
1874 		     esw->total_vports * MLX5_MAX_PORTS + MLX5_ESW_MISS_FLOWS;
1875 
1876 	/* create the slow path fdb with encap set, so further table instances
1877 	 * can be created at run time while VFs are probed if the FW allows that.
1878 	 */
1879 	if (esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE)
1880 		flags |= (MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT |
1881 			  MLX5_FLOW_TABLE_TUNNEL_EN_DECAP);
1882 
1883 	ft_attr.flags = flags;
1884 	ft_attr.max_fte = table_size;
1885 	ft_attr.prio = FDB_SLOW_PATH;
1886 
1887 	fdb = mlx5_create_flow_table(root_ns, &ft_attr);
1888 	if (IS_ERR(fdb)) {
1889 		err = PTR_ERR(fdb);
1890 		esw_warn(dev, "Failed to create slow path FDB Table err %d\n", err);
1891 		goto slow_fdb_err;
1892 	}
1893 	esw->fdb_table.offloads.slow_fdb = fdb;
1894 
1895 	/* Create empty TC-miss managed table. This allows plugging in following
1896 	 * priorities without directly exposing their level 0 table to
1897 	 * eswitch_offloads and passing it as miss_fdb to following call to
1898 	 * esw_chains_create().
1899 	 */
1900 	memset(&ft_attr, 0, sizeof(ft_attr));
1901 	ft_attr.prio = FDB_TC_MISS;
1902 	esw->fdb_table.offloads.tc_miss_table = mlx5_create_flow_table(root_ns, &ft_attr);
1903 	if (IS_ERR(esw->fdb_table.offloads.tc_miss_table)) {
1904 		err = PTR_ERR(esw->fdb_table.offloads.tc_miss_table);
1905 		esw_warn(dev, "Failed to create TC miss FDB Table err %d\n", err);
1906 		goto tc_miss_table_err;
1907 	}
1908 
1909 	err = esw_chains_create(esw, esw->fdb_table.offloads.tc_miss_table);
1910 	if (err) {
1911 		esw_warn(dev, "Failed to open fdb chains err(%d)\n", err);
1912 		goto fdb_chains_err;
1913 	}
1914 
1915 	err = esw_create_send_to_vport_group(esw, fdb, flow_group_in, &ix);
1916 	if (err)
1917 		goto send_vport_err;
1918 
1919 	err = esw_create_meta_send_to_vport_group(esw, fdb, flow_group_in, &ix);
1920 	if (err)
1921 		goto send_vport_meta_err;
1922 
1923 	err = esw_create_peer_esw_miss_group(esw, fdb, flow_group_in, &ix);
1924 	if (err)
1925 		goto peer_miss_err;
1926 
1927 	err = esw_create_miss_group(esw, fdb, flow_group_in, &ix);
1928 	if (err)
1929 		goto miss_err;
1930 
1931 	kvfree(flow_group_in);
1932 	return 0;
1933 
1934 miss_err:
1935 	if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
1936 		mlx5_destroy_flow_group(esw->fdb_table.offloads.peer_miss_grp);
1937 peer_miss_err:
1938 	if (esw->fdb_table.offloads.send_to_vport_meta_grp)
1939 		mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_meta_grp);
1940 send_vport_meta_err:
1941 	mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
1942 send_vport_err:
1943 	esw_chains_destroy(esw, esw_chains(esw));
1944 fdb_chains_err:
1945 	mlx5_destroy_flow_table(esw->fdb_table.offloads.tc_miss_table);
1946 tc_miss_table_err:
1947 	mlx5_destroy_flow_table(mlx5_eswitch_get_slow_fdb(esw));
1948 slow_fdb_err:
1949 	/* Holds true only as long as DMFS is the default */
1950 	mlx5_flow_namespace_set_mode(root_ns, MLX5_FLOW_STEERING_MODE_DMFS);
1951 ns_err:
1952 	kvfree(flow_group_in);
1953 	return err;
1954 }
1955 
esw_destroy_offloads_fdb_tables(struct mlx5_eswitch * esw)1956 static void esw_destroy_offloads_fdb_tables(struct mlx5_eswitch *esw)
1957 {
1958 	if (!mlx5_eswitch_get_slow_fdb(esw))
1959 		return;
1960 
1961 	esw_debug(esw->dev, "Destroy offloads FDB Tables\n");
1962 	mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_multi);
1963 	mlx5_del_flow_rules(esw->fdb_table.offloads.miss_rule_uni);
1964 	mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_grp);
1965 	if (esw->fdb_table.offloads.send_to_vport_meta_grp)
1966 		mlx5_destroy_flow_group(esw->fdb_table.offloads.send_to_vport_meta_grp);
1967 	if (MLX5_CAP_ESW(esw->dev, merged_eswitch))
1968 		mlx5_destroy_flow_group(esw->fdb_table.offloads.peer_miss_grp);
1969 	mlx5_destroy_flow_group(esw->fdb_table.offloads.miss_grp);
1970 
1971 	esw_chains_destroy(esw, esw_chains(esw));
1972 
1973 	mlx5_destroy_flow_table(esw->fdb_table.offloads.tc_miss_table);
1974 	mlx5_destroy_flow_table(mlx5_eswitch_get_slow_fdb(esw));
1975 	/* Holds true only as long as DMFS is the default */
1976 	mlx5_flow_namespace_set_mode(esw->fdb_table.offloads.ns,
1977 				     MLX5_FLOW_STEERING_MODE_DMFS);
1978 	atomic64_set(&esw->user_count, 0);
1979 }
1980 
esw_get_nr_ft_offloads_steering_src_ports(struct mlx5_eswitch * esw)1981 static int esw_get_nr_ft_offloads_steering_src_ports(struct mlx5_eswitch *esw)
1982 {
1983 	int nvports;
1984 
1985 	nvports = esw->total_vports + MLX5_ESW_MISS_FLOWS;
1986 	if (mlx5e_tc_int_port_supported(esw))
1987 		nvports += MLX5E_TC_MAX_INT_PORT_NUM;
1988 
1989 	return nvports;
1990 }
1991 
esw_create_offloads_table(struct mlx5_eswitch * esw)1992 static int esw_create_offloads_table(struct mlx5_eswitch *esw)
1993 {
1994 	struct mlx5_flow_table_attr ft_attr = {};
1995 	struct mlx5_core_dev *dev = esw->dev;
1996 	struct mlx5_flow_table *ft_offloads;
1997 	struct mlx5_flow_namespace *ns;
1998 	int err = 0;
1999 
2000 	ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
2001 	if (!ns) {
2002 		esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
2003 		return -EOPNOTSUPP;
2004 	}
2005 
2006 	ft_attr.max_fte = esw_get_nr_ft_offloads_steering_src_ports(esw) +
2007 			  MLX5_ESW_FT_OFFLOADS_DROP_RULE;
2008 	ft_attr.prio = 1;
2009 
2010 	ft_offloads = mlx5_create_flow_table(ns, &ft_attr);
2011 	if (IS_ERR(ft_offloads)) {
2012 		err = PTR_ERR(ft_offloads);
2013 		esw_warn(esw->dev, "Failed to create offloads table, err %d\n", err);
2014 		return err;
2015 	}
2016 
2017 	esw->offloads.ft_offloads = ft_offloads;
2018 	return 0;
2019 }
2020 
esw_destroy_offloads_table(struct mlx5_eswitch * esw)2021 static void esw_destroy_offloads_table(struct mlx5_eswitch *esw)
2022 {
2023 	struct mlx5_esw_offload *offloads = &esw->offloads;
2024 
2025 	mlx5_destroy_flow_table(offloads->ft_offloads);
2026 }
2027 
esw_create_vport_rx_group(struct mlx5_eswitch * esw)2028 static int esw_create_vport_rx_group(struct mlx5_eswitch *esw)
2029 {
2030 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2031 	struct mlx5_flow_group *g;
2032 	u32 *flow_group_in;
2033 	int nvports;
2034 	int err = 0;
2035 
2036 	nvports = esw_get_nr_ft_offloads_steering_src_ports(esw);
2037 	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2038 	if (!flow_group_in)
2039 		return -ENOMEM;
2040 
2041 	mlx5_esw_set_flow_group_source_port(esw, flow_group_in, 0);
2042 
2043 	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
2044 	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, nvports - 1);
2045 
2046 	g = mlx5_create_flow_group(esw->offloads.ft_offloads, flow_group_in);
2047 
2048 	if (IS_ERR(g)) {
2049 		err = PTR_ERR(g);
2050 		mlx5_core_warn(esw->dev, "Failed to create vport rx group err %d\n", err);
2051 		goto out;
2052 	}
2053 
2054 	esw->offloads.vport_rx_group = g;
2055 out:
2056 	kvfree(flow_group_in);
2057 	return err;
2058 }
2059 
esw_destroy_vport_rx_group(struct mlx5_eswitch * esw)2060 static void esw_destroy_vport_rx_group(struct mlx5_eswitch *esw)
2061 {
2062 	mlx5_destroy_flow_group(esw->offloads.vport_rx_group);
2063 }
2064 
esw_create_vport_rx_drop_rule_index(struct mlx5_eswitch * esw)2065 static int esw_create_vport_rx_drop_rule_index(struct mlx5_eswitch *esw)
2066 {
2067 	/* ft_offloads table is enlarged by MLX5_ESW_FT_OFFLOADS_DROP_RULE (1)
2068 	 * for the drop rule, which is placed at the end of the table.
2069 	 * So return the total of vport and int_port as rule index.
2070 	 */
2071 	return esw_get_nr_ft_offloads_steering_src_ports(esw);
2072 }
2073 
esw_create_vport_rx_drop_group(struct mlx5_eswitch * esw)2074 static int esw_create_vport_rx_drop_group(struct mlx5_eswitch *esw)
2075 {
2076 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2077 	struct mlx5_flow_group *g;
2078 	u32 *flow_group_in;
2079 	int flow_index;
2080 	int err = 0;
2081 
2082 	flow_index = esw_create_vport_rx_drop_rule_index(esw);
2083 
2084 	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2085 	if (!flow_group_in)
2086 		return -ENOMEM;
2087 
2088 	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, flow_index);
2089 	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, flow_index);
2090 
2091 	g = mlx5_create_flow_group(esw->offloads.ft_offloads, flow_group_in);
2092 
2093 	if (IS_ERR(g)) {
2094 		err = PTR_ERR(g);
2095 		mlx5_core_warn(esw->dev, "Failed to create vport rx drop group err %d\n", err);
2096 		goto out;
2097 	}
2098 
2099 	esw->offloads.vport_rx_drop_group = g;
2100 out:
2101 	kvfree(flow_group_in);
2102 	return err;
2103 }
2104 
esw_destroy_vport_rx_drop_group(struct mlx5_eswitch * esw)2105 static void esw_destroy_vport_rx_drop_group(struct mlx5_eswitch *esw)
2106 {
2107 	if (esw->offloads.vport_rx_drop_group)
2108 		mlx5_destroy_flow_group(esw->offloads.vport_rx_drop_group);
2109 }
2110 
2111 void
mlx5_esw_set_spec_source_port(struct mlx5_eswitch * esw,u16 vport,struct mlx5_flow_spec * spec)2112 mlx5_esw_set_spec_source_port(struct mlx5_eswitch *esw,
2113 			      u16 vport,
2114 			      struct mlx5_flow_spec *spec)
2115 {
2116 	void *misc;
2117 
2118 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
2119 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
2120 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
2121 			 mlx5_eswitch_get_vport_metadata_for_match(esw, vport));
2122 
2123 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
2124 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
2125 			 mlx5_eswitch_get_vport_metadata_mask());
2126 
2127 		spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;
2128 	} else {
2129 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters);
2130 		MLX5_SET(fte_match_set_misc, misc, source_port, vport);
2131 
2132 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
2133 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
2134 
2135 		spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
2136 	}
2137 }
2138 
2139 struct mlx5_flow_handle *
mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch * esw,u16 vport,struct mlx5_flow_destination * dest)2140 mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, u16 vport,
2141 				  struct mlx5_flow_destination *dest)
2142 {
2143 	struct mlx5_flow_act flow_act = {0};
2144 	struct mlx5_flow_handle *flow_rule;
2145 	struct mlx5_flow_spec *spec;
2146 
2147 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2148 	if (!spec) {
2149 		flow_rule = ERR_PTR(-ENOMEM);
2150 		goto out;
2151 	}
2152 
2153 	mlx5_esw_set_spec_source_port(esw, vport, spec);
2154 
2155 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2156 	flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, spec,
2157 					&flow_act, dest, 1);
2158 	if (IS_ERR(flow_rule)) {
2159 		esw_warn(esw->dev, "fs offloads: Failed to add vport rx rule err %ld\n", PTR_ERR(flow_rule));
2160 		goto out;
2161 	}
2162 
2163 out:
2164 	kvfree(spec);
2165 	return flow_rule;
2166 }
2167 
esw_create_vport_rx_drop_rule(struct mlx5_eswitch * esw)2168 static int esw_create_vport_rx_drop_rule(struct mlx5_eswitch *esw)
2169 {
2170 	struct mlx5_flow_act flow_act = {};
2171 	struct mlx5_flow_handle *flow_rule;
2172 
2173 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2174 	flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, NULL,
2175 					&flow_act, NULL, 0);
2176 	if (IS_ERR(flow_rule)) {
2177 		esw_warn(esw->dev,
2178 			 "fs offloads: Failed to add vport rx drop rule err %ld\n",
2179 			 PTR_ERR(flow_rule));
2180 		return PTR_ERR(flow_rule);
2181 	}
2182 
2183 	esw->offloads.vport_rx_drop_rule = flow_rule;
2184 
2185 	return 0;
2186 }
2187 
esw_destroy_vport_rx_drop_rule(struct mlx5_eswitch * esw)2188 static void esw_destroy_vport_rx_drop_rule(struct mlx5_eswitch *esw)
2189 {
2190 	if (esw->offloads.vport_rx_drop_rule)
2191 		mlx5_del_flow_rules(esw->offloads.vport_rx_drop_rule);
2192 }
2193 
mlx5_eswitch_inline_mode_get(struct mlx5_eswitch * esw,u8 * mode)2194 static int mlx5_eswitch_inline_mode_get(struct mlx5_eswitch *esw, u8 *mode)
2195 {
2196 	u8 prev_mlx5_mode, mlx5_mode = MLX5_INLINE_MODE_L2;
2197 	struct mlx5_core_dev *dev = esw->dev;
2198 	struct mlx5_vport *vport;
2199 	unsigned long i;
2200 
2201 	if (!MLX5_CAP_GEN(dev, vport_group_manager))
2202 		return -EOPNOTSUPP;
2203 
2204 	if (!mlx5_esw_is_fdb_created(esw))
2205 		return -EOPNOTSUPP;
2206 
2207 	switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
2208 	case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
2209 		mlx5_mode = MLX5_INLINE_MODE_NONE;
2210 		goto out;
2211 	case MLX5_CAP_INLINE_MODE_L2:
2212 		mlx5_mode = MLX5_INLINE_MODE_L2;
2213 		goto out;
2214 	case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
2215 		goto query_vports;
2216 	}
2217 
2218 query_vports:
2219 	mlx5_query_nic_vport_min_inline(dev, esw->first_host_vport, &prev_mlx5_mode);
2220 	mlx5_esw_for_each_host_func_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
2221 		mlx5_query_nic_vport_min_inline(dev, vport->vport, &mlx5_mode);
2222 		if (prev_mlx5_mode != mlx5_mode)
2223 			return -EINVAL;
2224 		prev_mlx5_mode = mlx5_mode;
2225 	}
2226 
2227 out:
2228 	*mode = mlx5_mode;
2229 	return 0;
2230 }
2231 
esw_destroy_restore_table(struct mlx5_eswitch * esw)2232 static void esw_destroy_restore_table(struct mlx5_eswitch *esw)
2233 {
2234 	struct mlx5_esw_offload *offloads = &esw->offloads;
2235 
2236 	if (!mlx5_eswitch_reg_c1_loopback_supported(esw))
2237 		return;
2238 
2239 	mlx5_modify_header_dealloc(esw->dev, offloads->restore_copy_hdr_id);
2240 	mlx5_destroy_flow_group(offloads->restore_group);
2241 	mlx5_destroy_flow_table(offloads->ft_offloads_restore);
2242 }
2243 
esw_create_restore_table(struct mlx5_eswitch * esw)2244 static int esw_create_restore_table(struct mlx5_eswitch *esw)
2245 {
2246 	u8 modact[MLX5_UN_SZ_BYTES(set_add_copy_action_in_auto)] = {};
2247 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2248 	struct mlx5_flow_table_attr ft_attr = {};
2249 	struct mlx5_core_dev *dev = esw->dev;
2250 	struct mlx5_flow_namespace *ns;
2251 	struct mlx5_modify_hdr *mod_hdr;
2252 	void *match_criteria, *misc;
2253 	struct mlx5_flow_table *ft;
2254 	struct mlx5_flow_group *g;
2255 	u32 *flow_group_in;
2256 	int err = 0;
2257 
2258 	if (!mlx5_eswitch_reg_c1_loopback_supported(esw))
2259 		return 0;
2260 
2261 	ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_OFFLOADS);
2262 	if (!ns) {
2263 		esw_warn(esw->dev, "Failed to get offloads flow namespace\n");
2264 		return -EOPNOTSUPP;
2265 	}
2266 
2267 	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2268 	if (!flow_group_in) {
2269 		err = -ENOMEM;
2270 		goto out_free;
2271 	}
2272 
2273 	ft_attr.max_fte = 1 << ESW_REG_C0_USER_DATA_METADATA_BITS;
2274 	ft = mlx5_create_flow_table(ns, &ft_attr);
2275 	if (IS_ERR(ft)) {
2276 		err = PTR_ERR(ft);
2277 		esw_warn(esw->dev, "Failed to create restore table, err %d\n",
2278 			 err);
2279 		goto out_free;
2280 	}
2281 
2282 	match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
2283 				      match_criteria);
2284 	misc = MLX5_ADDR_OF(fte_match_param, match_criteria,
2285 			    misc_parameters_2);
2286 
2287 	MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
2288 		 ESW_REG_C0_USER_DATA_METADATA_MASK);
2289 	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
2290 	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index,
2291 		 ft_attr.max_fte - 1);
2292 	MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
2293 		 MLX5_MATCH_MISC_PARAMETERS_2);
2294 	g = mlx5_create_flow_group(ft, flow_group_in);
2295 	if (IS_ERR(g)) {
2296 		err = PTR_ERR(g);
2297 		esw_warn(dev, "Failed to create restore flow group, err: %d\n",
2298 			 err);
2299 		goto err_group;
2300 	}
2301 
2302 	MLX5_SET(copy_action_in, modact, action_type, MLX5_ACTION_TYPE_COPY);
2303 	MLX5_SET(copy_action_in, modact, src_field,
2304 		 MLX5_ACTION_IN_FIELD_METADATA_REG_C_1);
2305 	MLX5_SET(copy_action_in, modact, dst_field,
2306 		 MLX5_ACTION_IN_FIELD_METADATA_REG_B);
2307 	mod_hdr = mlx5_modify_header_alloc(esw->dev,
2308 					   MLX5_FLOW_NAMESPACE_KERNEL, 1,
2309 					   modact);
2310 	if (IS_ERR(mod_hdr)) {
2311 		err = PTR_ERR(mod_hdr);
2312 		esw_warn(dev, "Failed to create restore mod header, err: %d\n",
2313 			 err);
2314 		goto err_mod_hdr;
2315 	}
2316 
2317 	esw->offloads.ft_offloads_restore = ft;
2318 	esw->offloads.restore_group = g;
2319 	esw->offloads.restore_copy_hdr_id = mod_hdr;
2320 
2321 	kvfree(flow_group_in);
2322 
2323 	return 0;
2324 
2325 err_mod_hdr:
2326 	mlx5_destroy_flow_group(g);
2327 err_group:
2328 	mlx5_destroy_flow_table(ft);
2329 out_free:
2330 	kvfree(flow_group_in);
2331 
2332 	return err;
2333 }
2334 
esw_mode_change(struct mlx5_eswitch * esw,u16 mode)2335 static void esw_mode_change(struct mlx5_eswitch *esw, u16 mode)
2336 {
2337 	mlx5_devcom_comp_lock(esw->dev->priv.hca_devcom_comp);
2338 	if (esw->dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV ||
2339 	    mlx5_core_mp_enabled(esw->dev)) {
2340 		esw->mode = mode;
2341 		mlx5_rescan_drivers_locked(esw->dev);
2342 		mlx5_devcom_comp_unlock(esw->dev->priv.hca_devcom_comp);
2343 		return;
2344 	}
2345 
2346 	esw->dev->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
2347 	mlx5_rescan_drivers_locked(esw->dev);
2348 	esw->mode = mode;
2349 	esw->dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
2350 	mlx5_rescan_drivers_locked(esw->dev);
2351 	mlx5_devcom_comp_unlock(esw->dev->priv.hca_devcom_comp);
2352 }
2353 
esw_offloads_start(struct mlx5_eswitch * esw,struct netlink_ext_ack * extack)2354 static int esw_offloads_start(struct mlx5_eswitch *esw,
2355 			      struct netlink_ext_ack *extack)
2356 {
2357 	int err;
2358 
2359 	esw_mode_change(esw, MLX5_ESWITCH_OFFLOADS);
2360 	err = mlx5_eswitch_enable_locked(esw, esw->dev->priv.sriov.num_vfs);
2361 	if (err) {
2362 		NL_SET_ERR_MSG_MOD(extack,
2363 				   "Failed setting eswitch to offloads");
2364 		esw_mode_change(esw, MLX5_ESWITCH_LEGACY);
2365 		return err;
2366 	}
2367 	if (esw->offloads.inline_mode == MLX5_INLINE_MODE_NONE) {
2368 		if (mlx5_eswitch_inline_mode_get(esw,
2369 						 &esw->offloads.inline_mode)) {
2370 			esw->offloads.inline_mode = MLX5_INLINE_MODE_L2;
2371 			NL_SET_ERR_MSG_MOD(extack,
2372 					   "Inline mode is different between vports");
2373 		}
2374 	}
2375 	return 0;
2376 }
2377 
mlx5_esw_offloads_rep_init(struct mlx5_eswitch * esw,const struct mlx5_vport * vport)2378 static int mlx5_esw_offloads_rep_init(struct mlx5_eswitch *esw, const struct mlx5_vport *vport)
2379 {
2380 	struct mlx5_eswitch_rep *rep;
2381 	int rep_type;
2382 	int err;
2383 
2384 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
2385 	if (!rep)
2386 		return -ENOMEM;
2387 
2388 	rep->vport = vport->vport;
2389 	rep->vport_index = vport->index;
2390 	for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++)
2391 		atomic_set(&rep->rep_data[rep_type].state, REP_UNREGISTERED);
2392 
2393 	err = xa_insert(&esw->offloads.vport_reps, rep->vport, rep, GFP_KERNEL);
2394 	if (err)
2395 		goto insert_err;
2396 
2397 	return 0;
2398 
2399 insert_err:
2400 	kfree(rep);
2401 	return err;
2402 }
2403 
mlx5_esw_offloads_rep_cleanup(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep)2404 static void mlx5_esw_offloads_rep_cleanup(struct mlx5_eswitch *esw,
2405 					  struct mlx5_eswitch_rep *rep)
2406 {
2407 	xa_erase(&esw->offloads.vport_reps, rep->vport);
2408 	kfree(rep);
2409 }
2410 
esw_offloads_cleanup_reps(struct mlx5_eswitch * esw)2411 static void esw_offloads_cleanup_reps(struct mlx5_eswitch *esw)
2412 {
2413 	struct mlx5_eswitch_rep *rep;
2414 	unsigned long i;
2415 
2416 	mlx5_esw_for_each_rep(esw, i, rep)
2417 		mlx5_esw_offloads_rep_cleanup(esw, rep);
2418 	xa_destroy(&esw->offloads.vport_reps);
2419 }
2420 
esw_offloads_init_reps(struct mlx5_eswitch * esw)2421 static int esw_offloads_init_reps(struct mlx5_eswitch *esw)
2422 {
2423 	struct mlx5_vport *vport;
2424 	unsigned long i;
2425 	int err;
2426 
2427 	xa_init(&esw->offloads.vport_reps);
2428 
2429 	mlx5_esw_for_each_vport(esw, i, vport) {
2430 		err = mlx5_esw_offloads_rep_init(esw, vport);
2431 		if (err)
2432 			goto err;
2433 	}
2434 	return 0;
2435 
2436 err:
2437 	esw_offloads_cleanup_reps(esw);
2438 	return err;
2439 }
2440 
esw_port_metadata_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx,struct netlink_ext_ack * extack)2441 static int esw_port_metadata_set(struct devlink *devlink, u32 id,
2442 				 struct devlink_param_gset_ctx *ctx,
2443 				 struct netlink_ext_ack *extack)
2444 {
2445 	struct mlx5_core_dev *dev = devlink_priv(devlink);
2446 	struct mlx5_eswitch *esw = dev->priv.eswitch;
2447 	int err = 0;
2448 
2449 	down_write(&esw->mode_lock);
2450 	if (mlx5_esw_is_fdb_created(esw)) {
2451 		err = -EBUSY;
2452 		goto done;
2453 	}
2454 	if (!mlx5_esw_vport_match_metadata_supported(esw)) {
2455 		err = -EOPNOTSUPP;
2456 		goto done;
2457 	}
2458 	if (ctx->val.vbool)
2459 		esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
2460 	else
2461 		esw->flags &= ~MLX5_ESWITCH_VPORT_MATCH_METADATA;
2462 done:
2463 	up_write(&esw->mode_lock);
2464 	return err;
2465 }
2466 
esw_port_metadata_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)2467 static int esw_port_metadata_get(struct devlink *devlink, u32 id,
2468 				 struct devlink_param_gset_ctx *ctx)
2469 {
2470 	struct mlx5_core_dev *dev = devlink_priv(devlink);
2471 
2472 	ctx->val.vbool = mlx5_eswitch_vport_match_metadata_enabled(dev->priv.eswitch);
2473 	return 0;
2474 }
2475 
esw_port_metadata_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)2476 static int esw_port_metadata_validate(struct devlink *devlink, u32 id,
2477 				      union devlink_param_value val,
2478 				      struct netlink_ext_ack *extack)
2479 {
2480 	struct mlx5_core_dev *dev = devlink_priv(devlink);
2481 	u8 esw_mode;
2482 
2483 	esw_mode = mlx5_eswitch_mode(dev);
2484 	if (esw_mode == MLX5_ESWITCH_OFFLOADS) {
2485 		NL_SET_ERR_MSG_MOD(extack,
2486 				   "E-Switch must either disabled or non switchdev mode");
2487 		return -EBUSY;
2488 	}
2489 	return 0;
2490 }
2491 
2492 static const struct devlink_param esw_devlink_params[] = {
2493 	DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_ESW_PORT_METADATA,
2494 			     "esw_port_metadata", DEVLINK_PARAM_TYPE_BOOL,
2495 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
2496 			     esw_port_metadata_get,
2497 			     esw_port_metadata_set,
2498 			     esw_port_metadata_validate),
2499 };
2500 
esw_offloads_init(struct mlx5_eswitch * esw)2501 int esw_offloads_init(struct mlx5_eswitch *esw)
2502 {
2503 	int err;
2504 
2505 	err = esw_offloads_init_reps(esw);
2506 	if (err)
2507 		return err;
2508 
2509 	if (MLX5_ESWITCH_MANAGER(esw->dev) &&
2510 	    mlx5_esw_vport_match_metadata_supported(esw))
2511 		esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
2512 
2513 	err = devl_params_register(priv_to_devlink(esw->dev),
2514 				   esw_devlink_params,
2515 				   ARRAY_SIZE(esw_devlink_params));
2516 	if (err)
2517 		goto err_params;
2518 
2519 	return 0;
2520 
2521 err_params:
2522 	esw_offloads_cleanup_reps(esw);
2523 	return err;
2524 }
2525 
esw_offloads_cleanup(struct mlx5_eswitch * esw)2526 void esw_offloads_cleanup(struct mlx5_eswitch *esw)
2527 {
2528 	devl_params_unregister(priv_to_devlink(esw->dev),
2529 			       esw_devlink_params,
2530 			       ARRAY_SIZE(esw_devlink_params));
2531 	esw_offloads_cleanup_reps(esw);
2532 }
2533 
__esw_offloads_load_rep(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep,u8 rep_type)2534 static int __esw_offloads_load_rep(struct mlx5_eswitch *esw,
2535 				   struct mlx5_eswitch_rep *rep, u8 rep_type)
2536 {
2537 	if (atomic_cmpxchg(&rep->rep_data[rep_type].state,
2538 			   REP_REGISTERED, REP_LOADED) == REP_REGISTERED)
2539 		return esw->offloads.rep_ops[rep_type]->load(esw->dev, rep);
2540 
2541 	return 0;
2542 }
2543 
__esw_offloads_unload_rep(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep,u8 rep_type)2544 static void __esw_offloads_unload_rep(struct mlx5_eswitch *esw,
2545 				      struct mlx5_eswitch_rep *rep, u8 rep_type)
2546 {
2547 	if (atomic_cmpxchg(&rep->rep_data[rep_type].state,
2548 			   REP_LOADED, REP_REGISTERED) == REP_LOADED) {
2549 		if (rep_type == REP_ETH)
2550 			__esw_offloads_unload_rep(esw, rep, REP_IB);
2551 		esw->offloads.rep_ops[rep_type]->unload(rep);
2552 	}
2553 }
2554 
__unload_reps_all_vport(struct mlx5_eswitch * esw,u8 rep_type)2555 static void __unload_reps_all_vport(struct mlx5_eswitch *esw, u8 rep_type)
2556 {
2557 	struct mlx5_eswitch_rep *rep;
2558 	unsigned long i;
2559 
2560 	mlx5_esw_for_each_rep(esw, i, rep)
2561 		__esw_offloads_unload_rep(esw, rep, rep_type);
2562 }
2563 
mlx5_esw_offloads_rep_load(struct mlx5_eswitch * esw,u16 vport_num)2564 static int mlx5_esw_offloads_rep_load(struct mlx5_eswitch *esw, u16 vport_num)
2565 {
2566 	struct mlx5_eswitch_rep *rep;
2567 	int rep_type;
2568 	int err;
2569 
2570 	rep = mlx5_eswitch_get_rep(esw, vport_num);
2571 	for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++) {
2572 		err = __esw_offloads_load_rep(esw, rep, rep_type);
2573 		if (err)
2574 			goto err_reps;
2575 	}
2576 
2577 	return 0;
2578 
2579 err_reps:
2580 	atomic_set(&rep->rep_data[rep_type].state, REP_REGISTERED);
2581 	for (--rep_type; rep_type >= 0; rep_type--)
2582 		__esw_offloads_unload_rep(esw, rep, rep_type);
2583 	return err;
2584 }
2585 
mlx5_esw_offloads_rep_unload(struct mlx5_eswitch * esw,u16 vport_num)2586 static void mlx5_esw_offloads_rep_unload(struct mlx5_eswitch *esw, u16 vport_num)
2587 {
2588 	struct mlx5_eswitch_rep *rep;
2589 	int rep_type;
2590 
2591 	rep = mlx5_eswitch_get_rep(esw, vport_num);
2592 	for (rep_type = NUM_REP_TYPES - 1; rep_type >= 0; rep_type--)
2593 		__esw_offloads_unload_rep(esw, rep, rep_type);
2594 }
2595 
mlx5_esw_offloads_init_pf_vf_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2596 int mlx5_esw_offloads_init_pf_vf_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2597 {
2598 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2599 		return 0;
2600 
2601 	return mlx5_esw_offloads_pf_vf_devlink_port_init(esw, vport);
2602 }
2603 
mlx5_esw_offloads_cleanup_pf_vf_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2604 void mlx5_esw_offloads_cleanup_pf_vf_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2605 {
2606 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2607 		return;
2608 
2609 	mlx5_esw_offloads_pf_vf_devlink_port_cleanup(esw, vport);
2610 }
2611 
mlx5_esw_offloads_init_sf_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport,struct mlx5_devlink_port * dl_port,u32 controller,u32 sfnum)2612 int mlx5_esw_offloads_init_sf_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport,
2613 				  struct mlx5_devlink_port *dl_port,
2614 				  u32 controller, u32 sfnum)
2615 {
2616 	return mlx5_esw_offloads_sf_devlink_port_init(esw, vport, dl_port, controller, sfnum);
2617 }
2618 
mlx5_esw_offloads_cleanup_sf_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2619 void mlx5_esw_offloads_cleanup_sf_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2620 {
2621 	mlx5_esw_offloads_sf_devlink_port_cleanup(esw, vport);
2622 }
2623 
mlx5_esw_offloads_load_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2624 int mlx5_esw_offloads_load_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2625 {
2626 	int err;
2627 
2628 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2629 		return 0;
2630 
2631 	err = mlx5_esw_offloads_devlink_port_register(esw, vport);
2632 	if (err)
2633 		return err;
2634 
2635 	err = mlx5_esw_offloads_rep_load(esw, vport->vport);
2636 	if (err)
2637 		goto load_err;
2638 	return err;
2639 
2640 load_err:
2641 	mlx5_esw_offloads_devlink_port_unregister(vport);
2642 	return err;
2643 }
2644 
mlx5_esw_offloads_unload_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2645 void mlx5_esw_offloads_unload_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2646 {
2647 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2648 		return;
2649 
2650 	mlx5_esw_offloads_rep_unload(esw, vport->vport);
2651 
2652 	mlx5_esw_offloads_devlink_port_unregister(vport);
2653 }
2654 
esw_set_slave_root_fdb(struct mlx5_core_dev * master,struct mlx5_core_dev * slave)2655 static int esw_set_slave_root_fdb(struct mlx5_core_dev *master,
2656 				  struct mlx5_core_dev *slave)
2657 {
2658 	u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)]   = {};
2659 	u32 out[MLX5_ST_SZ_DW(set_flow_table_root_out)] = {};
2660 	struct mlx5_flow_root_namespace *root;
2661 	struct mlx5_flow_namespace *ns;
2662 	int err;
2663 
2664 	MLX5_SET(set_flow_table_root_in, in, opcode,
2665 		 MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
2666 	MLX5_SET(set_flow_table_root_in, in, table_type,
2667 		 FS_FT_FDB);
2668 
2669 	if (master) {
2670 		ns = mlx5_get_flow_namespace(master,
2671 					     MLX5_FLOW_NAMESPACE_FDB);
2672 		root = find_root(&ns->node);
2673 		mutex_lock(&root->chain_lock);
2674 		MLX5_SET(set_flow_table_root_in, in,
2675 			 table_eswitch_owner_vhca_id_valid, 1);
2676 		MLX5_SET(set_flow_table_root_in, in,
2677 			 table_eswitch_owner_vhca_id,
2678 			 MLX5_CAP_GEN(master, vhca_id));
2679 		MLX5_SET(set_flow_table_root_in, in, table_id,
2680 			 root->root_ft->id);
2681 	} else {
2682 		ns = mlx5_get_flow_namespace(slave,
2683 					     MLX5_FLOW_NAMESPACE_FDB);
2684 		root = find_root(&ns->node);
2685 		mutex_lock(&root->chain_lock);
2686 		MLX5_SET(set_flow_table_root_in, in, table_id,
2687 			 root->root_ft->id);
2688 	}
2689 
2690 	err = mlx5_cmd_exec(slave, in, sizeof(in), out, sizeof(out));
2691 	mutex_unlock(&root->chain_lock);
2692 
2693 	return err;
2694 }
2695 
__esw_set_master_egress_rule(struct mlx5_core_dev * master,struct mlx5_core_dev * slave,struct mlx5_vport * vport,struct mlx5_flow_table * acl)2696 static int __esw_set_master_egress_rule(struct mlx5_core_dev *master,
2697 					struct mlx5_core_dev *slave,
2698 					struct mlx5_vport *vport,
2699 					struct mlx5_flow_table *acl)
2700 {
2701 	u16 slave_index = MLX5_CAP_GEN(slave, vhca_id);
2702 	struct mlx5_flow_handle *flow_rule = NULL;
2703 	struct mlx5_flow_destination dest = {};
2704 	struct mlx5_flow_act flow_act = {};
2705 	struct mlx5_flow_spec *spec;
2706 	int err = 0;
2707 	void *misc;
2708 
2709 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2710 	if (!spec)
2711 		return -ENOMEM;
2712 
2713 	spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
2714 	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2715 			    misc_parameters);
2716 	MLX5_SET(fte_match_set_misc, misc, source_port, MLX5_VPORT_UPLINK);
2717 	MLX5_SET(fte_match_set_misc, misc, source_eswitch_owner_vhca_id, slave_index);
2718 
2719 	misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
2720 	MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
2721 	MLX5_SET_TO_ONES(fte_match_set_misc, misc,
2722 			 source_eswitch_owner_vhca_id);
2723 
2724 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2725 	dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
2726 	dest.vport.num = slave->priv.eswitch->manager_vport;
2727 	dest.vport.vhca_id = MLX5_CAP_GEN(slave, vhca_id);
2728 	dest.vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
2729 
2730 	flow_rule = mlx5_add_flow_rules(acl, spec, &flow_act,
2731 					&dest, 1);
2732 	if (IS_ERR(flow_rule)) {
2733 		err = PTR_ERR(flow_rule);
2734 	} else {
2735 		err = xa_insert(&vport->egress.offloads.bounce_rules,
2736 				slave_index, flow_rule, GFP_KERNEL);
2737 		if (err)
2738 			mlx5_del_flow_rules(flow_rule);
2739 	}
2740 
2741 	kvfree(spec);
2742 	return err;
2743 }
2744 
esw_master_egress_create_resources(struct mlx5_eswitch * esw,struct mlx5_flow_namespace * egress_ns,struct mlx5_vport * vport,size_t count)2745 static int esw_master_egress_create_resources(struct mlx5_eswitch *esw,
2746 					      struct mlx5_flow_namespace *egress_ns,
2747 					      struct mlx5_vport *vport, size_t count)
2748 {
2749 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2750 	struct mlx5_flow_table_attr ft_attr = {
2751 		.max_fte = count, .prio = 0, .level = 0,
2752 	};
2753 	struct mlx5_flow_table *acl;
2754 	struct mlx5_flow_group *g;
2755 	void *match_criteria;
2756 	u32 *flow_group_in;
2757 	int err;
2758 
2759 	if (vport->egress.acl)
2760 		return 0;
2761 
2762 	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2763 	if (!flow_group_in)
2764 		return -ENOMEM;
2765 
2766 	if (vport->vport || mlx5_core_is_ecpf(esw->dev))
2767 		ft_attr.flags = MLX5_FLOW_TABLE_OTHER_VPORT;
2768 
2769 	acl = mlx5_create_vport_flow_table(egress_ns, &ft_attr, vport->vport);
2770 	if (IS_ERR(acl)) {
2771 		err = PTR_ERR(acl);
2772 		goto out;
2773 	}
2774 
2775 	match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
2776 				      match_criteria);
2777 	MLX5_SET_TO_ONES(fte_match_param, match_criteria,
2778 			 misc_parameters.source_port);
2779 	MLX5_SET_TO_ONES(fte_match_param, match_criteria,
2780 			 misc_parameters.source_eswitch_owner_vhca_id);
2781 	MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
2782 		 MLX5_MATCH_MISC_PARAMETERS);
2783 
2784 	MLX5_SET(create_flow_group_in, flow_group_in,
2785 		 source_eswitch_owner_vhca_id_valid, 1);
2786 	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
2787 	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, count);
2788 
2789 	g = mlx5_create_flow_group(acl, flow_group_in);
2790 	if (IS_ERR(g)) {
2791 		err = PTR_ERR(g);
2792 		goto err_group;
2793 	}
2794 
2795 	vport->egress.acl = acl;
2796 	vport->egress.offloads.bounce_grp = g;
2797 	vport->egress.type = VPORT_EGRESS_ACL_TYPE_SHARED_FDB;
2798 	xa_init_flags(&vport->egress.offloads.bounce_rules, XA_FLAGS_ALLOC);
2799 
2800 	kvfree(flow_group_in);
2801 
2802 	return 0;
2803 
2804 err_group:
2805 	mlx5_destroy_flow_table(acl);
2806 out:
2807 	kvfree(flow_group_in);
2808 	return err;
2809 }
2810 
esw_master_egress_destroy_resources(struct mlx5_vport * vport)2811 static void esw_master_egress_destroy_resources(struct mlx5_vport *vport)
2812 {
2813 	if (!xa_empty(&vport->egress.offloads.bounce_rules))
2814 		return;
2815 	mlx5_destroy_flow_group(vport->egress.offloads.bounce_grp);
2816 	vport->egress.offloads.bounce_grp = NULL;
2817 	mlx5_destroy_flow_table(vport->egress.acl);
2818 	vport->egress.acl = NULL;
2819 }
2820 
esw_set_master_egress_rule(struct mlx5_core_dev * master,struct mlx5_core_dev * slave,size_t count)2821 static int esw_set_master_egress_rule(struct mlx5_core_dev *master,
2822 				      struct mlx5_core_dev *slave, size_t count)
2823 {
2824 	struct mlx5_eswitch *esw = master->priv.eswitch;
2825 	u16 slave_index = MLX5_CAP_GEN(slave, vhca_id);
2826 	struct mlx5_flow_namespace *egress_ns;
2827 	struct mlx5_vport *vport;
2828 	int err;
2829 
2830 	vport = mlx5_eswitch_get_vport(esw, esw->manager_vport);
2831 	if (IS_ERR(vport))
2832 		return PTR_ERR(vport);
2833 
2834 	egress_ns = mlx5_get_flow_vport_acl_namespace(master,
2835 						      MLX5_FLOW_NAMESPACE_ESW_EGRESS,
2836 						      vport->index);
2837 	if (!egress_ns)
2838 		return -EINVAL;
2839 
2840 	if (vport->egress.acl && vport->egress.type != VPORT_EGRESS_ACL_TYPE_SHARED_FDB)
2841 		return 0;
2842 
2843 	err = esw_master_egress_create_resources(esw, egress_ns, vport, count);
2844 	if (err)
2845 		return err;
2846 
2847 	if (xa_load(&vport->egress.offloads.bounce_rules, slave_index))
2848 		return -EINVAL;
2849 
2850 	err = __esw_set_master_egress_rule(master, slave, vport, vport->egress.acl);
2851 	if (err)
2852 		goto err_rule;
2853 
2854 	return 0;
2855 
2856 err_rule:
2857 	esw_master_egress_destroy_resources(vport);
2858 	return err;
2859 }
2860 
esw_unset_master_egress_rule(struct mlx5_core_dev * dev,struct mlx5_core_dev * slave_dev)2861 static void esw_unset_master_egress_rule(struct mlx5_core_dev *dev,
2862 					 struct mlx5_core_dev *slave_dev)
2863 {
2864 	struct mlx5_vport *vport;
2865 
2866 	vport = mlx5_eswitch_get_vport(dev->priv.eswitch,
2867 				       dev->priv.eswitch->manager_vport);
2868 
2869 	esw_acl_egress_ofld_bounce_rule_destroy(vport, MLX5_CAP_GEN(slave_dev, vhca_id));
2870 
2871 	if (xa_empty(&vport->egress.offloads.bounce_rules)) {
2872 		esw_acl_egress_ofld_cleanup(vport);
2873 		xa_destroy(&vport->egress.offloads.bounce_rules);
2874 	}
2875 }
2876 
mlx5_eswitch_offloads_single_fdb_add_one(struct mlx5_eswitch * master_esw,struct mlx5_eswitch * slave_esw,int max_slaves)2877 int mlx5_eswitch_offloads_single_fdb_add_one(struct mlx5_eswitch *master_esw,
2878 					     struct mlx5_eswitch *slave_esw, int max_slaves)
2879 {
2880 	int err;
2881 
2882 	err = esw_set_slave_root_fdb(master_esw->dev,
2883 				     slave_esw->dev);
2884 	if (err)
2885 		return err;
2886 
2887 	err = esw_set_master_egress_rule(master_esw->dev,
2888 					 slave_esw->dev, max_slaves);
2889 	if (err)
2890 		goto err_acl;
2891 
2892 	return err;
2893 
2894 err_acl:
2895 	esw_set_slave_root_fdb(NULL, slave_esw->dev);
2896 	return err;
2897 }
2898 
mlx5_eswitch_offloads_single_fdb_del_one(struct mlx5_eswitch * master_esw,struct mlx5_eswitch * slave_esw)2899 void mlx5_eswitch_offloads_single_fdb_del_one(struct mlx5_eswitch *master_esw,
2900 					      struct mlx5_eswitch *slave_esw)
2901 {
2902 	esw_set_slave_root_fdb(NULL, slave_esw->dev);
2903 	esw_unset_master_egress_rule(master_esw->dev, slave_esw->dev);
2904 }
2905 
2906 #define ESW_OFFLOADS_DEVCOM_PAIR	(0)
2907 #define ESW_OFFLOADS_DEVCOM_UNPAIR	(1)
2908 
mlx5_esw_offloads_rep_event_unpair(struct mlx5_eswitch * esw,struct mlx5_eswitch * peer_esw)2909 static void mlx5_esw_offloads_rep_event_unpair(struct mlx5_eswitch *esw,
2910 					       struct mlx5_eswitch *peer_esw)
2911 {
2912 	const struct mlx5_eswitch_rep_ops *ops;
2913 	struct mlx5_eswitch_rep *rep;
2914 	unsigned long i;
2915 	u8 rep_type;
2916 
2917 	mlx5_esw_for_each_rep(esw, i, rep) {
2918 		rep_type = NUM_REP_TYPES;
2919 		while (rep_type--) {
2920 			ops = esw->offloads.rep_ops[rep_type];
2921 			if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
2922 			    ops->event)
2923 				ops->event(esw, rep, MLX5_SWITCHDEV_EVENT_UNPAIR, peer_esw);
2924 		}
2925 	}
2926 }
2927 
mlx5_esw_offloads_unpair(struct mlx5_eswitch * esw,struct mlx5_eswitch * peer_esw)2928 static void mlx5_esw_offloads_unpair(struct mlx5_eswitch *esw,
2929 				     struct mlx5_eswitch *peer_esw)
2930 {
2931 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
2932 	mlx5e_tc_clean_fdb_peer_flows(esw);
2933 #endif
2934 	mlx5_esw_offloads_rep_event_unpair(esw, peer_esw);
2935 	esw_del_fdb_peer_miss_rules(esw, peer_esw->dev);
2936 }
2937 
mlx5_esw_offloads_pair(struct mlx5_eswitch * esw,struct mlx5_eswitch * peer_esw)2938 static int mlx5_esw_offloads_pair(struct mlx5_eswitch *esw,
2939 				  struct mlx5_eswitch *peer_esw)
2940 {
2941 	const struct mlx5_eswitch_rep_ops *ops;
2942 	struct mlx5_eswitch_rep *rep;
2943 	unsigned long i;
2944 	u8 rep_type;
2945 	int err;
2946 
2947 	err = esw_add_fdb_peer_miss_rules(esw, peer_esw->dev);
2948 	if (err)
2949 		return err;
2950 
2951 	mlx5_esw_for_each_rep(esw, i, rep) {
2952 		for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++) {
2953 			ops = esw->offloads.rep_ops[rep_type];
2954 			if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
2955 			    ops->event) {
2956 				err = ops->event(esw, rep, MLX5_SWITCHDEV_EVENT_PAIR, peer_esw);
2957 				if (err)
2958 					goto err_out;
2959 			}
2960 		}
2961 	}
2962 
2963 	return 0;
2964 
2965 err_out:
2966 	mlx5_esw_offloads_unpair(esw, peer_esw);
2967 	return err;
2968 }
2969 
mlx5_esw_offloads_set_ns_peer(struct mlx5_eswitch * esw,struct mlx5_eswitch * peer_esw,bool pair)2970 static int mlx5_esw_offloads_set_ns_peer(struct mlx5_eswitch *esw,
2971 					 struct mlx5_eswitch *peer_esw,
2972 					 bool pair)
2973 {
2974 	u16 peer_vhca_id = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
2975 	u16 vhca_id = MLX5_CAP_GEN(esw->dev, vhca_id);
2976 	struct mlx5_flow_root_namespace *peer_ns;
2977 	struct mlx5_flow_root_namespace *ns;
2978 	int err;
2979 
2980 	peer_ns = peer_esw->dev->priv.steering->fdb_root_ns;
2981 	ns = esw->dev->priv.steering->fdb_root_ns;
2982 
2983 	if (pair) {
2984 		err = mlx5_flow_namespace_set_peer(ns, peer_ns, peer_vhca_id);
2985 		if (err)
2986 			return err;
2987 
2988 		err = mlx5_flow_namespace_set_peer(peer_ns, ns, vhca_id);
2989 		if (err) {
2990 			mlx5_flow_namespace_set_peer(ns, NULL, peer_vhca_id);
2991 			return err;
2992 		}
2993 	} else {
2994 		mlx5_flow_namespace_set_peer(ns, NULL, peer_vhca_id);
2995 		mlx5_flow_namespace_set_peer(peer_ns, NULL, vhca_id);
2996 	}
2997 
2998 	return 0;
2999 }
3000 
mlx5_esw_offloads_devcom_event(int event,void * my_data,void * event_data)3001 static int mlx5_esw_offloads_devcom_event(int event,
3002 					  void *my_data,
3003 					  void *event_data)
3004 {
3005 	struct mlx5_eswitch *esw = my_data;
3006 	struct mlx5_eswitch *peer_esw = event_data;
3007 	u16 esw_i, peer_esw_i;
3008 	bool esw_paired;
3009 	int err;
3010 
3011 	peer_esw_i = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
3012 	esw_i = MLX5_CAP_GEN(esw->dev, vhca_id);
3013 	esw_paired = !!xa_load(&esw->paired, peer_esw_i);
3014 
3015 	switch (event) {
3016 	case ESW_OFFLOADS_DEVCOM_PAIR:
3017 		if (mlx5_eswitch_vport_match_metadata_enabled(esw) !=
3018 		    mlx5_eswitch_vport_match_metadata_enabled(peer_esw))
3019 			break;
3020 
3021 		if (esw_paired)
3022 			break;
3023 
3024 		err = mlx5_esw_offloads_set_ns_peer(esw, peer_esw, true);
3025 		if (err)
3026 			goto err_out;
3027 
3028 		err = mlx5_esw_offloads_pair(esw, peer_esw);
3029 		if (err)
3030 			goto err_peer;
3031 
3032 		err = mlx5_esw_offloads_pair(peer_esw, esw);
3033 		if (err)
3034 			goto err_pair;
3035 
3036 		err = xa_insert(&esw->paired, peer_esw_i, peer_esw, GFP_KERNEL);
3037 		if (err)
3038 			goto err_xa;
3039 
3040 		err = xa_insert(&peer_esw->paired, esw_i, esw, GFP_KERNEL);
3041 		if (err)
3042 			goto err_peer_xa;
3043 
3044 		esw->num_peers++;
3045 		peer_esw->num_peers++;
3046 		mlx5_devcom_comp_set_ready(esw->devcom, true);
3047 		break;
3048 
3049 	case ESW_OFFLOADS_DEVCOM_UNPAIR:
3050 		if (!esw_paired)
3051 			break;
3052 
3053 		peer_esw->num_peers--;
3054 		esw->num_peers--;
3055 		if (!esw->num_peers && !peer_esw->num_peers)
3056 			mlx5_devcom_comp_set_ready(esw->devcom, false);
3057 		xa_erase(&peer_esw->paired, esw_i);
3058 		xa_erase(&esw->paired, peer_esw_i);
3059 		mlx5_esw_offloads_unpair(peer_esw, esw);
3060 		mlx5_esw_offloads_unpair(esw, peer_esw);
3061 		mlx5_esw_offloads_set_ns_peer(esw, peer_esw, false);
3062 		break;
3063 	}
3064 
3065 	return 0;
3066 
3067 err_peer_xa:
3068 	xa_erase(&esw->paired, peer_esw_i);
3069 err_xa:
3070 	mlx5_esw_offloads_unpair(peer_esw, esw);
3071 err_pair:
3072 	mlx5_esw_offloads_unpair(esw, peer_esw);
3073 err_peer:
3074 	mlx5_esw_offloads_set_ns_peer(esw, peer_esw, false);
3075 err_out:
3076 	mlx5_core_err(esw->dev, "esw offloads devcom event failure, event %u err %d",
3077 		      event, err);
3078 	return err;
3079 }
3080 
mlx5_esw_offloads_devcom_init(struct mlx5_eswitch * esw,u64 key)3081 void mlx5_esw_offloads_devcom_init(struct mlx5_eswitch *esw, u64 key)
3082 {
3083 	int i;
3084 
3085 	for (i = 0; i < MLX5_MAX_PORTS; i++)
3086 		INIT_LIST_HEAD(&esw->offloads.peer_flows[i]);
3087 	mutex_init(&esw->offloads.peer_mutex);
3088 
3089 	if (!MLX5_CAP_ESW(esw->dev, merged_eswitch))
3090 		return;
3091 
3092 	if ((MLX5_VPORT_MANAGER(esw->dev) || mlx5_core_is_ecpf_esw_manager(esw->dev)) &&
3093 	    !mlx5_lag_is_supported(esw->dev))
3094 		return;
3095 
3096 	xa_init(&esw->paired);
3097 	esw->num_peers = 0;
3098 	esw->devcom = mlx5_devcom_register_component(esw->dev->priv.devc,
3099 						     MLX5_DEVCOM_ESW_OFFLOADS,
3100 						     key,
3101 						     mlx5_esw_offloads_devcom_event,
3102 						     esw);
3103 	if (IS_ERR(esw->devcom))
3104 		return;
3105 
3106 	mlx5_devcom_send_event(esw->devcom,
3107 			       ESW_OFFLOADS_DEVCOM_PAIR,
3108 			       ESW_OFFLOADS_DEVCOM_UNPAIR,
3109 			       esw);
3110 }
3111 
mlx5_esw_offloads_devcom_cleanup(struct mlx5_eswitch * esw)3112 void mlx5_esw_offloads_devcom_cleanup(struct mlx5_eswitch *esw)
3113 {
3114 	if (IS_ERR_OR_NULL(esw->devcom))
3115 		return;
3116 
3117 	mlx5_devcom_send_event(esw->devcom,
3118 			       ESW_OFFLOADS_DEVCOM_UNPAIR,
3119 			       ESW_OFFLOADS_DEVCOM_UNPAIR,
3120 			       esw);
3121 
3122 	mlx5_devcom_unregister_component(esw->devcom);
3123 	xa_destroy(&esw->paired);
3124 	esw->devcom = NULL;
3125 }
3126 
mlx5_esw_offloads_devcom_is_ready(struct mlx5_eswitch * esw)3127 bool mlx5_esw_offloads_devcom_is_ready(struct mlx5_eswitch *esw)
3128 {
3129 	return mlx5_devcom_comp_is_ready(esw->devcom);
3130 }
3131 
mlx5_esw_vport_match_metadata_supported(const struct mlx5_eswitch * esw)3132 bool mlx5_esw_vport_match_metadata_supported(const struct mlx5_eswitch *esw)
3133 {
3134 	if (!MLX5_CAP_ESW(esw->dev, esw_uplink_ingress_acl))
3135 		return false;
3136 
3137 	if (!(MLX5_CAP_ESW_FLOWTABLE(esw->dev, fdb_to_vport_reg_c_id) &
3138 	      MLX5_FDB_TO_VPORT_REG_C_0))
3139 		return false;
3140 
3141 	return true;
3142 }
3143 
3144 #define MLX5_ESW_METADATA_RSVD_UPLINK 1
3145 
3146 /* Share the same metadata for uplink's. This is fine because:
3147  * (a) In shared FDB mode (LAG) both uplink's are treated the
3148  *     same and tagged with the same metadata.
3149  * (b) In non shared FDB mode, packets from physical port0
3150  *     cannot hit eswitch of PF1 and vice versa.
3151  */
mlx5_esw_match_metadata_reserved(struct mlx5_eswitch * esw)3152 static u32 mlx5_esw_match_metadata_reserved(struct mlx5_eswitch *esw)
3153 {
3154 	return MLX5_ESW_METADATA_RSVD_UPLINK;
3155 }
3156 
mlx5_esw_match_metadata_alloc(struct mlx5_eswitch * esw)3157 u32 mlx5_esw_match_metadata_alloc(struct mlx5_eswitch *esw)
3158 {
3159 	u32 vport_end_ida = (1 << ESW_VPORT_BITS) - 1;
3160 	/* Reserve 0xf for internal port offload */
3161 	u32 max_pf_num = (1 << ESW_PFNUM_BITS) - 2;
3162 	u32 pf_num;
3163 	int id;
3164 
3165 	/* Only 4 bits of pf_num */
3166 	pf_num = mlx5_get_dev_index(esw->dev);
3167 	if (pf_num > max_pf_num)
3168 		return 0;
3169 
3170 	/* Metadata is 4 bits of PFNUM and 12 bits of unique id */
3171 	/* Use only non-zero vport_id (2-4095) for all PF's */
3172 	id = ida_alloc_range(&esw->offloads.vport_metadata_ida,
3173 			     MLX5_ESW_METADATA_RSVD_UPLINK + 1,
3174 			     vport_end_ida, GFP_KERNEL);
3175 	if (id < 0)
3176 		return 0;
3177 	id = (pf_num << ESW_VPORT_BITS) | id;
3178 	return id;
3179 }
3180 
mlx5_esw_match_metadata_free(struct mlx5_eswitch * esw,u32 metadata)3181 void mlx5_esw_match_metadata_free(struct mlx5_eswitch *esw, u32 metadata)
3182 {
3183 	u32 vport_bit_mask = (1 << ESW_VPORT_BITS) - 1;
3184 
3185 	/* Metadata contains only 12 bits of actual ida id */
3186 	ida_free(&esw->offloads.vport_metadata_ida, metadata & vport_bit_mask);
3187 }
3188 
esw_offloads_vport_metadata_setup(struct mlx5_eswitch * esw,struct mlx5_vport * vport)3189 static int esw_offloads_vport_metadata_setup(struct mlx5_eswitch *esw,
3190 					     struct mlx5_vport *vport)
3191 {
3192 	if (vport->vport == MLX5_VPORT_UPLINK)
3193 		vport->default_metadata = mlx5_esw_match_metadata_reserved(esw);
3194 	else
3195 		vport->default_metadata = mlx5_esw_match_metadata_alloc(esw);
3196 
3197 	vport->metadata = vport->default_metadata;
3198 	return vport->metadata ? 0 : -ENOSPC;
3199 }
3200 
esw_offloads_vport_metadata_cleanup(struct mlx5_eswitch * esw,struct mlx5_vport * vport)3201 static void esw_offloads_vport_metadata_cleanup(struct mlx5_eswitch *esw,
3202 						struct mlx5_vport *vport)
3203 {
3204 	if (!vport->default_metadata)
3205 		return;
3206 
3207 	if (vport->vport == MLX5_VPORT_UPLINK)
3208 		return;
3209 
3210 	WARN_ON(vport->metadata != vport->default_metadata);
3211 	mlx5_esw_match_metadata_free(esw, vport->default_metadata);
3212 }
3213 
esw_offloads_metadata_uninit(struct mlx5_eswitch * esw)3214 static void esw_offloads_metadata_uninit(struct mlx5_eswitch *esw)
3215 {
3216 	struct mlx5_vport *vport;
3217 	unsigned long i;
3218 
3219 	if (!mlx5_eswitch_vport_match_metadata_enabled(esw))
3220 		return;
3221 
3222 	mlx5_esw_for_each_vport(esw, i, vport)
3223 		esw_offloads_vport_metadata_cleanup(esw, vport);
3224 }
3225 
esw_offloads_metadata_init(struct mlx5_eswitch * esw)3226 static int esw_offloads_metadata_init(struct mlx5_eswitch *esw)
3227 {
3228 	struct mlx5_vport *vport;
3229 	unsigned long i;
3230 	int err;
3231 
3232 	if (!mlx5_eswitch_vport_match_metadata_enabled(esw))
3233 		return 0;
3234 
3235 	mlx5_esw_for_each_vport(esw, i, vport) {
3236 		err = esw_offloads_vport_metadata_setup(esw, vport);
3237 		if (err)
3238 			goto metadata_err;
3239 	}
3240 
3241 	return 0;
3242 
3243 metadata_err:
3244 	esw_offloads_metadata_uninit(esw);
3245 	return err;
3246 }
3247 
3248 int
esw_vport_create_offloads_acl_tables(struct mlx5_eswitch * esw,struct mlx5_vport * vport)3249 esw_vport_create_offloads_acl_tables(struct mlx5_eswitch *esw,
3250 				     struct mlx5_vport *vport)
3251 {
3252 	int err;
3253 
3254 	err = esw_acl_ingress_ofld_setup(esw, vport);
3255 	if (err)
3256 		return err;
3257 
3258 	err = esw_acl_egress_ofld_setup(esw, vport);
3259 	if (err)
3260 		goto egress_err;
3261 
3262 	return 0;
3263 
3264 egress_err:
3265 	esw_acl_ingress_ofld_cleanup(esw, vport);
3266 	return err;
3267 }
3268 
3269 void
esw_vport_destroy_offloads_acl_tables(struct mlx5_eswitch * esw,struct mlx5_vport * vport)3270 esw_vport_destroy_offloads_acl_tables(struct mlx5_eswitch *esw,
3271 				      struct mlx5_vport *vport)
3272 {
3273 	esw_acl_egress_ofld_cleanup(vport);
3274 	esw_acl_ingress_ofld_cleanup(esw, vport);
3275 }
3276 
esw_create_offloads_acl_tables(struct mlx5_eswitch * esw)3277 static int esw_create_offloads_acl_tables(struct mlx5_eswitch *esw)
3278 {
3279 	struct mlx5_vport *uplink, *manager;
3280 	int ret;
3281 
3282 	uplink = mlx5_eswitch_get_vport(esw, MLX5_VPORT_UPLINK);
3283 	if (IS_ERR(uplink))
3284 		return PTR_ERR(uplink);
3285 
3286 	ret = esw_vport_create_offloads_acl_tables(esw, uplink);
3287 	if (ret)
3288 		return ret;
3289 
3290 	manager = mlx5_eswitch_get_vport(esw, esw->manager_vport);
3291 	if (IS_ERR(manager)) {
3292 		ret = PTR_ERR(manager);
3293 		goto err_manager;
3294 	}
3295 
3296 	ret = esw_vport_create_offloads_acl_tables(esw, manager);
3297 	if (ret)
3298 		goto err_manager;
3299 
3300 	return 0;
3301 
3302 err_manager:
3303 	esw_vport_destroy_offloads_acl_tables(esw, uplink);
3304 	return ret;
3305 }
3306 
esw_destroy_offloads_acl_tables(struct mlx5_eswitch * esw)3307 static void esw_destroy_offloads_acl_tables(struct mlx5_eswitch *esw)
3308 {
3309 	struct mlx5_vport *vport;
3310 
3311 	vport = mlx5_eswitch_get_vport(esw, esw->manager_vport);
3312 	if (!IS_ERR(vport))
3313 		esw_vport_destroy_offloads_acl_tables(esw, vport);
3314 
3315 	vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_UPLINK);
3316 	if (!IS_ERR(vport))
3317 		esw_vport_destroy_offloads_acl_tables(esw, vport);
3318 }
3319 
mlx5_eswitch_reload_ib_reps(struct mlx5_eswitch * esw)3320 int mlx5_eswitch_reload_ib_reps(struct mlx5_eswitch *esw)
3321 {
3322 	struct mlx5_eswitch_rep *rep;
3323 	unsigned long i;
3324 	int ret;
3325 
3326 	if (!esw || esw->mode != MLX5_ESWITCH_OFFLOADS)
3327 		return 0;
3328 
3329 	rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_UPLINK);
3330 	if (atomic_read(&rep->rep_data[REP_ETH].state) != REP_LOADED)
3331 		return 0;
3332 
3333 	ret = __esw_offloads_load_rep(esw, rep, REP_IB);
3334 	if (ret)
3335 		return ret;
3336 
3337 	mlx5_esw_for_each_rep(esw, i, rep) {
3338 		if (atomic_read(&rep->rep_data[REP_ETH].state) == REP_LOADED)
3339 			__esw_offloads_load_rep(esw, rep, REP_IB);
3340 	}
3341 
3342 	return 0;
3343 }
3344 
esw_offloads_steering_init(struct mlx5_eswitch * esw)3345 static int esw_offloads_steering_init(struct mlx5_eswitch *esw)
3346 {
3347 	struct mlx5_esw_indir_table *indir;
3348 	int err;
3349 
3350 	memset(&esw->fdb_table.offloads, 0, sizeof(struct offloads_fdb));
3351 	mutex_init(&esw->fdb_table.offloads.vports.lock);
3352 	hash_init(esw->fdb_table.offloads.vports.table);
3353 	atomic64_set(&esw->user_count, 0);
3354 
3355 	indir = mlx5_esw_indir_table_init();
3356 	if (IS_ERR(indir)) {
3357 		err = PTR_ERR(indir);
3358 		goto create_indir_err;
3359 	}
3360 	esw->fdb_table.offloads.indir = indir;
3361 
3362 	err = esw_create_offloads_acl_tables(esw);
3363 	if (err)
3364 		goto create_acl_err;
3365 
3366 	err = esw_create_offloads_table(esw);
3367 	if (err)
3368 		goto create_offloads_err;
3369 
3370 	err = esw_create_restore_table(esw);
3371 	if (err)
3372 		goto create_restore_err;
3373 
3374 	err = esw_create_offloads_fdb_tables(esw);
3375 	if (err)
3376 		goto create_fdb_err;
3377 
3378 	err = esw_create_vport_rx_group(esw);
3379 	if (err)
3380 		goto create_fg_err;
3381 
3382 	err = esw_create_vport_rx_drop_group(esw);
3383 	if (err)
3384 		goto create_rx_drop_fg_err;
3385 
3386 	err = esw_create_vport_rx_drop_rule(esw);
3387 	if (err)
3388 		goto create_rx_drop_rule_err;
3389 
3390 	return 0;
3391 
3392 create_rx_drop_rule_err:
3393 	esw_destroy_vport_rx_drop_group(esw);
3394 create_rx_drop_fg_err:
3395 	esw_destroy_vport_rx_group(esw);
3396 create_fg_err:
3397 	esw_destroy_offloads_fdb_tables(esw);
3398 create_fdb_err:
3399 	esw_destroy_restore_table(esw);
3400 create_restore_err:
3401 	esw_destroy_offloads_table(esw);
3402 create_offloads_err:
3403 	esw_destroy_offloads_acl_tables(esw);
3404 create_acl_err:
3405 	mlx5_esw_indir_table_destroy(esw->fdb_table.offloads.indir);
3406 create_indir_err:
3407 	mutex_destroy(&esw->fdb_table.offloads.vports.lock);
3408 	return err;
3409 }
3410 
esw_offloads_steering_cleanup(struct mlx5_eswitch * esw)3411 static void esw_offloads_steering_cleanup(struct mlx5_eswitch *esw)
3412 {
3413 	esw_destroy_vport_rx_drop_rule(esw);
3414 	esw_destroy_vport_rx_drop_group(esw);
3415 	esw_destroy_vport_rx_group(esw);
3416 	esw_destroy_offloads_fdb_tables(esw);
3417 	esw_destroy_restore_table(esw);
3418 	esw_destroy_offloads_table(esw);
3419 	esw_destroy_offloads_acl_tables(esw);
3420 	mlx5_esw_indir_table_destroy(esw->fdb_table.offloads.indir);
3421 	mutex_destroy(&esw->fdb_table.offloads.vports.lock);
3422 }
3423 
3424 static void
esw_vfs_changed_event_handler(struct mlx5_eswitch * esw,const u32 * out)3425 esw_vfs_changed_event_handler(struct mlx5_eswitch *esw, const u32 *out)
3426 {
3427 	struct devlink *devlink;
3428 	bool host_pf_disabled;
3429 	u16 new_num_vfs;
3430 
3431 	new_num_vfs = MLX5_GET(query_esw_functions_out, out,
3432 			       host_params_context.host_num_of_vfs);
3433 	host_pf_disabled = MLX5_GET(query_esw_functions_out, out,
3434 				    host_params_context.host_pf_disabled);
3435 
3436 	if (new_num_vfs == esw->esw_funcs.num_vfs || host_pf_disabled)
3437 		return;
3438 
3439 	devlink = priv_to_devlink(esw->dev);
3440 	devl_lock(devlink);
3441 	/* Number of VFs can only change from "0 to x" or "x to 0". */
3442 	if (esw->esw_funcs.num_vfs > 0) {
3443 		mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
3444 	} else {
3445 		int err;
3446 
3447 		err = mlx5_eswitch_load_vf_vports(esw, new_num_vfs,
3448 						  MLX5_VPORT_UC_ADDR_CHANGE);
3449 		if (err) {
3450 			devl_unlock(devlink);
3451 			return;
3452 		}
3453 	}
3454 	esw->esw_funcs.num_vfs = new_num_vfs;
3455 	devl_unlock(devlink);
3456 }
3457 
esw_functions_changed_event_handler(struct work_struct * work)3458 static void esw_functions_changed_event_handler(struct work_struct *work)
3459 {
3460 	struct mlx5_host_work *host_work;
3461 	struct mlx5_eswitch *esw;
3462 	const u32 *out;
3463 
3464 	host_work = container_of(work, struct mlx5_host_work, work);
3465 	esw = host_work->esw;
3466 
3467 	out = mlx5_esw_query_functions(esw->dev);
3468 	if (IS_ERR(out))
3469 		goto out;
3470 
3471 	esw_vfs_changed_event_handler(esw, out);
3472 	kvfree(out);
3473 out:
3474 	kfree(host_work);
3475 }
3476 
mlx5_esw_funcs_changed_handler(struct notifier_block * nb,unsigned long type,void * data)3477 int mlx5_esw_funcs_changed_handler(struct notifier_block *nb, unsigned long type, void *data)
3478 {
3479 	struct mlx5_esw_functions *esw_funcs;
3480 	struct mlx5_host_work *host_work;
3481 	struct mlx5_eswitch *esw;
3482 
3483 	host_work = kzalloc(sizeof(*host_work), GFP_ATOMIC);
3484 	if (!host_work)
3485 		return NOTIFY_DONE;
3486 
3487 	esw_funcs = mlx5_nb_cof(nb, struct mlx5_esw_functions, nb);
3488 	esw = container_of(esw_funcs, struct mlx5_eswitch, esw_funcs);
3489 
3490 	host_work->esw = esw;
3491 
3492 	INIT_WORK(&host_work->work, esw_functions_changed_event_handler);
3493 	queue_work(esw->work_queue, &host_work->work);
3494 
3495 	return NOTIFY_OK;
3496 }
3497 
mlx5_esw_host_number_init(struct mlx5_eswitch * esw)3498 static int mlx5_esw_host_number_init(struct mlx5_eswitch *esw)
3499 {
3500 	const u32 *query_host_out;
3501 
3502 	if (!mlx5_core_is_ecpf_esw_manager(esw->dev))
3503 		return 0;
3504 
3505 	query_host_out = mlx5_esw_query_functions(esw->dev);
3506 	if (IS_ERR(query_host_out))
3507 		return PTR_ERR(query_host_out);
3508 
3509 	/* Mark non local controller with non zero controller number. */
3510 	esw->offloads.host_number = MLX5_GET(query_esw_functions_out, query_host_out,
3511 					     host_params_context.host_number);
3512 	kvfree(query_host_out);
3513 	return 0;
3514 }
3515 
mlx5_esw_offloads_controller_valid(const struct mlx5_eswitch * esw,u32 controller)3516 bool mlx5_esw_offloads_controller_valid(const struct mlx5_eswitch *esw, u32 controller)
3517 {
3518 	/* Local controller is always valid */
3519 	if (controller == 0)
3520 		return true;
3521 
3522 	if (!mlx5_core_is_ecpf_esw_manager(esw->dev))
3523 		return false;
3524 
3525 	/* External host number starts with zero in device */
3526 	return (controller == esw->offloads.host_number + 1);
3527 }
3528 
esw_offloads_enable(struct mlx5_eswitch * esw)3529 int esw_offloads_enable(struct mlx5_eswitch *esw)
3530 {
3531 	struct mapping_ctx *reg_c0_obj_pool;
3532 	struct mlx5_vport *vport;
3533 	unsigned long i;
3534 	u64 mapping_id;
3535 	int err;
3536 
3537 	mutex_init(&esw->offloads.termtbl_mutex);
3538 	mlx5_rdma_enable_roce(esw->dev);
3539 
3540 	err = mlx5_esw_host_number_init(esw);
3541 	if (err)
3542 		goto err_metadata;
3543 
3544 	err = esw_offloads_metadata_init(esw);
3545 	if (err)
3546 		goto err_metadata;
3547 
3548 	err = esw_set_passing_vport_metadata(esw, true);
3549 	if (err)
3550 		goto err_vport_metadata;
3551 
3552 	mapping_id = mlx5_query_nic_system_image_guid(esw->dev);
3553 
3554 	reg_c0_obj_pool = mapping_create_for_id(mapping_id, MAPPING_TYPE_CHAIN,
3555 						sizeof(struct mlx5_mapped_obj),
3556 						ESW_REG_C0_USER_DATA_METADATA_MASK,
3557 						true);
3558 
3559 	if (IS_ERR(reg_c0_obj_pool)) {
3560 		err = PTR_ERR(reg_c0_obj_pool);
3561 		goto err_pool;
3562 	}
3563 	esw->offloads.reg_c0_obj_pool = reg_c0_obj_pool;
3564 
3565 	err = esw_offloads_steering_init(esw);
3566 	if (err)
3567 		goto err_steering_init;
3568 
3569 	/* Representor will control the vport link state */
3570 	mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs)
3571 		vport->info.link_state = MLX5_VPORT_ADMIN_STATE_DOWN;
3572 	if (mlx5_core_ec_sriov_enabled(esw->dev))
3573 		mlx5_esw_for_each_ec_vf_vport(esw, i, vport, esw->esw_funcs.num_ec_vfs)
3574 			vport->info.link_state = MLX5_VPORT_ADMIN_STATE_DOWN;
3575 
3576 	/* Uplink vport rep must load first. */
3577 	err = mlx5_esw_offloads_rep_load(esw, MLX5_VPORT_UPLINK);
3578 	if (err)
3579 		goto err_uplink;
3580 
3581 	err = mlx5_eswitch_enable_pf_vf_vports(esw, MLX5_VPORT_UC_ADDR_CHANGE);
3582 	if (err)
3583 		goto err_vports;
3584 
3585 	return 0;
3586 
3587 err_vports:
3588 	mlx5_esw_offloads_rep_unload(esw, MLX5_VPORT_UPLINK);
3589 err_uplink:
3590 	esw_offloads_steering_cleanup(esw);
3591 err_steering_init:
3592 	mapping_destroy(reg_c0_obj_pool);
3593 err_pool:
3594 	esw_set_passing_vport_metadata(esw, false);
3595 err_vport_metadata:
3596 	esw_offloads_metadata_uninit(esw);
3597 err_metadata:
3598 	mlx5_rdma_disable_roce(esw->dev);
3599 	mutex_destroy(&esw->offloads.termtbl_mutex);
3600 	return err;
3601 }
3602 
esw_offloads_stop(struct mlx5_eswitch * esw,struct netlink_ext_ack * extack)3603 static int esw_offloads_stop(struct mlx5_eswitch *esw,
3604 			     struct netlink_ext_ack *extack)
3605 {
3606 	int err;
3607 
3608 	esw_mode_change(esw, MLX5_ESWITCH_LEGACY);
3609 
3610 	/* If changing from switchdev to legacy mode without sriov enabled,
3611 	 * no need to create legacy fdb.
3612 	 */
3613 	if (!mlx5_core_is_pf(esw->dev) || !mlx5_sriov_is_enabled(esw->dev))
3614 		return 0;
3615 
3616 	err = mlx5_eswitch_enable_locked(esw, MLX5_ESWITCH_IGNORE_NUM_VFS);
3617 	if (err)
3618 		NL_SET_ERR_MSG_MOD(extack, "Failed setting eswitch to legacy");
3619 
3620 	return err;
3621 }
3622 
esw_offloads_disable(struct mlx5_eswitch * esw)3623 void esw_offloads_disable(struct mlx5_eswitch *esw)
3624 {
3625 	mlx5_eswitch_disable_pf_vf_vports(esw);
3626 	mlx5_esw_offloads_rep_unload(esw, MLX5_VPORT_UPLINK);
3627 	esw_set_passing_vport_metadata(esw, false);
3628 	esw_offloads_steering_cleanup(esw);
3629 	mapping_destroy(esw->offloads.reg_c0_obj_pool);
3630 	esw_offloads_metadata_uninit(esw);
3631 	mlx5_rdma_disable_roce(esw->dev);
3632 	mutex_destroy(&esw->offloads.termtbl_mutex);
3633 }
3634 
esw_mode_from_devlink(u16 mode,u16 * mlx5_mode)3635 static int esw_mode_from_devlink(u16 mode, u16 *mlx5_mode)
3636 {
3637 	switch (mode) {
3638 	case DEVLINK_ESWITCH_MODE_LEGACY:
3639 		*mlx5_mode = MLX5_ESWITCH_LEGACY;
3640 		break;
3641 	case DEVLINK_ESWITCH_MODE_SWITCHDEV:
3642 		*mlx5_mode = MLX5_ESWITCH_OFFLOADS;
3643 		break;
3644 	default:
3645 		return -EINVAL;
3646 	}
3647 
3648 	return 0;
3649 }
3650 
esw_mode_to_devlink(u16 mlx5_mode,u16 * mode)3651 static int esw_mode_to_devlink(u16 mlx5_mode, u16 *mode)
3652 {
3653 	switch (mlx5_mode) {
3654 	case MLX5_ESWITCH_LEGACY:
3655 		*mode = DEVLINK_ESWITCH_MODE_LEGACY;
3656 		break;
3657 	case MLX5_ESWITCH_OFFLOADS:
3658 		*mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
3659 		break;
3660 	default:
3661 		return -EINVAL;
3662 	}
3663 
3664 	return 0;
3665 }
3666 
esw_inline_mode_from_devlink(u8 mode,u8 * mlx5_mode)3667 static int esw_inline_mode_from_devlink(u8 mode, u8 *mlx5_mode)
3668 {
3669 	switch (mode) {
3670 	case DEVLINK_ESWITCH_INLINE_MODE_NONE:
3671 		*mlx5_mode = MLX5_INLINE_MODE_NONE;
3672 		break;
3673 	case DEVLINK_ESWITCH_INLINE_MODE_LINK:
3674 		*mlx5_mode = MLX5_INLINE_MODE_L2;
3675 		break;
3676 	case DEVLINK_ESWITCH_INLINE_MODE_NETWORK:
3677 		*mlx5_mode = MLX5_INLINE_MODE_IP;
3678 		break;
3679 	case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT:
3680 		*mlx5_mode = MLX5_INLINE_MODE_TCP_UDP;
3681 		break;
3682 	default:
3683 		return -EINVAL;
3684 	}
3685 
3686 	return 0;
3687 }
3688 
esw_inline_mode_to_devlink(u8 mlx5_mode,u8 * mode)3689 static int esw_inline_mode_to_devlink(u8 mlx5_mode, u8 *mode)
3690 {
3691 	switch (mlx5_mode) {
3692 	case MLX5_INLINE_MODE_NONE:
3693 		*mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
3694 		break;
3695 	case MLX5_INLINE_MODE_L2:
3696 		*mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
3697 		break;
3698 	case MLX5_INLINE_MODE_IP:
3699 		*mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
3700 		break;
3701 	case MLX5_INLINE_MODE_TCP_UDP:
3702 		*mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
3703 		break;
3704 	default:
3705 		return -EINVAL;
3706 	}
3707 
3708 	return 0;
3709 }
3710 
mlx5_eswitch_block_mode(struct mlx5_core_dev * dev)3711 int mlx5_eswitch_block_mode(struct mlx5_core_dev *dev)
3712 {
3713 	struct mlx5_eswitch *esw = dev->priv.eswitch;
3714 	int err;
3715 
3716 	if (!mlx5_esw_allowed(esw))
3717 		return 0;
3718 
3719 	/* Take TC into account */
3720 	err = mlx5_esw_try_lock(esw);
3721 	if (err < 0)
3722 		return err;
3723 
3724 	esw->offloads.num_block_mode++;
3725 	mlx5_esw_unlock(esw);
3726 	return 0;
3727 }
3728 
mlx5_eswitch_unblock_mode(struct mlx5_core_dev * dev)3729 void mlx5_eswitch_unblock_mode(struct mlx5_core_dev *dev)
3730 {
3731 	struct mlx5_eswitch *esw = dev->priv.eswitch;
3732 
3733 	if (!mlx5_esw_allowed(esw))
3734 		return;
3735 
3736 	down_write(&esw->mode_lock);
3737 	esw->offloads.num_block_mode--;
3738 	up_write(&esw->mode_lock);
3739 }
3740 
mlx5_devlink_eswitch_mode_set(struct devlink * devlink,u16 mode,struct netlink_ext_ack * extack)3741 int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
3742 				  struct netlink_ext_ack *extack)
3743 {
3744 	u16 cur_mlx5_mode, mlx5_mode = 0;
3745 	struct mlx5_eswitch *esw;
3746 	int err = 0;
3747 
3748 	esw = mlx5_devlink_eswitch_get(devlink);
3749 	if (IS_ERR(esw))
3750 		return PTR_ERR(esw);
3751 
3752 	if (esw_mode_from_devlink(mode, &mlx5_mode))
3753 		return -EINVAL;
3754 
3755 	if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV && mlx5_get_sd(esw->dev)) {
3756 		NL_SET_ERR_MSG_MOD(extack,
3757 				   "Can't change E-Switch mode to switchdev when multi-PF netdev (Socket Direct) is configured.");
3758 		return -EPERM;
3759 	}
3760 
3761 	mlx5_lag_disable_change(esw->dev);
3762 	err = mlx5_esw_try_lock(esw);
3763 	if (err < 0) {
3764 		NL_SET_ERR_MSG_MOD(extack, "Can't change mode, E-Switch is busy");
3765 		goto enable_lag;
3766 	}
3767 	cur_mlx5_mode = err;
3768 	err = 0;
3769 
3770 	if (cur_mlx5_mode == mlx5_mode)
3771 		goto unlock;
3772 
3773 	if (esw->offloads.num_block_mode) {
3774 		NL_SET_ERR_MSG_MOD(extack,
3775 				   "Can't change eswitch mode when IPsec SA and/or policies are configured");
3776 		err = -EOPNOTSUPP;
3777 		goto unlock;
3778 	}
3779 
3780 	esw->eswitch_operation_in_progress = true;
3781 	up_write(&esw->mode_lock);
3782 
3783 	mlx5_eswitch_disable_locked(esw);
3784 	if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV) {
3785 		if (mlx5_devlink_trap_get_num_active(esw->dev)) {
3786 			NL_SET_ERR_MSG_MOD(extack,
3787 					   "Can't change mode while devlink traps are active");
3788 			err = -EOPNOTSUPP;
3789 			goto skip;
3790 		}
3791 		err = esw_offloads_start(esw, extack);
3792 	} else if (mode == DEVLINK_ESWITCH_MODE_LEGACY) {
3793 		err = esw_offloads_stop(esw, extack);
3794 	} else {
3795 		err = -EINVAL;
3796 	}
3797 
3798 skip:
3799 	down_write(&esw->mode_lock);
3800 	esw->eswitch_operation_in_progress = false;
3801 unlock:
3802 	mlx5_esw_unlock(esw);
3803 enable_lag:
3804 	mlx5_lag_enable_change(esw->dev);
3805 	return err;
3806 }
3807 
mlx5_devlink_eswitch_mode_get(struct devlink * devlink,u16 * mode)3808 int mlx5_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
3809 {
3810 	struct mlx5_eswitch *esw;
3811 
3812 	esw = mlx5_devlink_eswitch_get(devlink);
3813 	if (IS_ERR(esw))
3814 		return PTR_ERR(esw);
3815 
3816 	return esw_mode_to_devlink(esw->mode, mode);
3817 }
3818 
mlx5_esw_vports_inline_set(struct mlx5_eswitch * esw,u8 mlx5_mode,struct netlink_ext_ack * extack)3819 static int mlx5_esw_vports_inline_set(struct mlx5_eswitch *esw, u8 mlx5_mode,
3820 				      struct netlink_ext_ack *extack)
3821 {
3822 	struct mlx5_core_dev *dev = esw->dev;
3823 	struct mlx5_vport *vport;
3824 	u16 err_vport_num = 0;
3825 	unsigned long i;
3826 	int err = 0;
3827 
3828 	mlx5_esw_for_each_host_func_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
3829 		err = mlx5_modify_nic_vport_min_inline(dev, vport->vport, mlx5_mode);
3830 		if (err) {
3831 			err_vport_num = vport->vport;
3832 			NL_SET_ERR_MSG_MOD(extack,
3833 					   "Failed to set min inline on vport");
3834 			goto revert_inline_mode;
3835 		}
3836 	}
3837 	if (mlx5_core_ec_sriov_enabled(esw->dev)) {
3838 		mlx5_esw_for_each_ec_vf_vport(esw, i, vport, esw->esw_funcs.num_ec_vfs) {
3839 			err = mlx5_modify_nic_vport_min_inline(dev, vport->vport, mlx5_mode);
3840 			if (err) {
3841 				err_vport_num = vport->vport;
3842 				NL_SET_ERR_MSG_MOD(extack,
3843 						   "Failed to set min inline on vport");
3844 				goto revert_ec_vf_inline_mode;
3845 			}
3846 		}
3847 	}
3848 	return 0;
3849 
3850 revert_ec_vf_inline_mode:
3851 	mlx5_esw_for_each_ec_vf_vport(esw, i, vport, esw->esw_funcs.num_ec_vfs) {
3852 		if (vport->vport == err_vport_num)
3853 			break;
3854 		mlx5_modify_nic_vport_min_inline(dev,
3855 						 vport->vport,
3856 						 esw->offloads.inline_mode);
3857 	}
3858 revert_inline_mode:
3859 	mlx5_esw_for_each_host_func_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
3860 		if (vport->vport == err_vport_num)
3861 			break;
3862 		mlx5_modify_nic_vport_min_inline(dev,
3863 						 vport->vport,
3864 						 esw->offloads.inline_mode);
3865 	}
3866 	return err;
3867 }
3868 
mlx5_devlink_eswitch_inline_mode_set(struct devlink * devlink,u8 mode,struct netlink_ext_ack * extack)3869 int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode,
3870 					 struct netlink_ext_ack *extack)
3871 {
3872 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3873 	struct mlx5_eswitch *esw;
3874 	u8 mlx5_mode;
3875 	int err;
3876 
3877 	esw = mlx5_devlink_eswitch_get(devlink);
3878 	if (IS_ERR(esw))
3879 		return PTR_ERR(esw);
3880 
3881 	down_write(&esw->mode_lock);
3882 
3883 	switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
3884 	case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
3885 		if (mode == DEVLINK_ESWITCH_INLINE_MODE_NONE) {
3886 			err = 0;
3887 			goto out;
3888 		}
3889 
3890 		fallthrough;
3891 	case MLX5_CAP_INLINE_MODE_L2:
3892 		NL_SET_ERR_MSG_MOD(extack, "Inline mode can't be set");
3893 		err = -EOPNOTSUPP;
3894 		goto out;
3895 	case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
3896 		break;
3897 	}
3898 
3899 	if (atomic64_read(&esw->offloads.num_flows) > 0) {
3900 		NL_SET_ERR_MSG_MOD(extack,
3901 				   "Can't set inline mode when flows are configured");
3902 		err = -EOPNOTSUPP;
3903 		goto out;
3904 	}
3905 
3906 	err = esw_inline_mode_from_devlink(mode, &mlx5_mode);
3907 	if (err)
3908 		goto out;
3909 
3910 	esw->eswitch_operation_in_progress = true;
3911 	up_write(&esw->mode_lock);
3912 
3913 	err = mlx5_esw_vports_inline_set(esw, mlx5_mode, extack);
3914 	if (!err)
3915 		esw->offloads.inline_mode = mlx5_mode;
3916 
3917 	down_write(&esw->mode_lock);
3918 	esw->eswitch_operation_in_progress = false;
3919 	up_write(&esw->mode_lock);
3920 	return 0;
3921 
3922 out:
3923 	up_write(&esw->mode_lock);
3924 	return err;
3925 }
3926 
mlx5_devlink_eswitch_inline_mode_get(struct devlink * devlink,u8 * mode)3927 int mlx5_devlink_eswitch_inline_mode_get(struct devlink *devlink, u8 *mode)
3928 {
3929 	struct mlx5_eswitch *esw;
3930 
3931 	esw = mlx5_devlink_eswitch_get(devlink);
3932 	if (IS_ERR(esw))
3933 		return PTR_ERR(esw);
3934 
3935 	return esw_inline_mode_to_devlink(esw->offloads.inline_mode, mode);
3936 }
3937 
mlx5_eswitch_block_encap(struct mlx5_core_dev * dev)3938 bool mlx5_eswitch_block_encap(struct mlx5_core_dev *dev)
3939 {
3940 	struct mlx5_eswitch *esw = dev->priv.eswitch;
3941 
3942 	if (!mlx5_esw_allowed(esw))
3943 		return true;
3944 
3945 	down_write(&esw->mode_lock);
3946 	if (esw->mode != MLX5_ESWITCH_LEGACY &&
3947 	    esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE) {
3948 		up_write(&esw->mode_lock);
3949 		return false;
3950 	}
3951 
3952 	esw->offloads.num_block_encap++;
3953 	up_write(&esw->mode_lock);
3954 	return true;
3955 }
3956 
mlx5_eswitch_unblock_encap(struct mlx5_core_dev * dev)3957 void mlx5_eswitch_unblock_encap(struct mlx5_core_dev *dev)
3958 {
3959 	struct mlx5_eswitch *esw = dev->priv.eswitch;
3960 
3961 	if (!mlx5_esw_allowed(esw))
3962 		return;
3963 
3964 	down_write(&esw->mode_lock);
3965 	esw->offloads.num_block_encap--;
3966 	up_write(&esw->mode_lock);
3967 }
3968 
mlx5_devlink_eswitch_encap_mode_set(struct devlink * devlink,enum devlink_eswitch_encap_mode encap,struct netlink_ext_ack * extack)3969 int mlx5_devlink_eswitch_encap_mode_set(struct devlink *devlink,
3970 					enum devlink_eswitch_encap_mode encap,
3971 					struct netlink_ext_ack *extack)
3972 {
3973 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3974 	struct mlx5_eswitch *esw;
3975 	int err = 0;
3976 
3977 	esw = mlx5_devlink_eswitch_get(devlink);
3978 	if (IS_ERR(esw))
3979 		return PTR_ERR(esw);
3980 
3981 	down_write(&esw->mode_lock);
3982 
3983 	if (encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE &&
3984 	    (!MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) ||
3985 	     !MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))) {
3986 		err = -EOPNOTSUPP;
3987 		goto unlock;
3988 	}
3989 
3990 	if (encap && encap != DEVLINK_ESWITCH_ENCAP_MODE_BASIC) {
3991 		err = -EOPNOTSUPP;
3992 		goto unlock;
3993 	}
3994 
3995 	if (esw->mode == MLX5_ESWITCH_LEGACY) {
3996 		esw->offloads.encap = encap;
3997 		goto unlock;
3998 	}
3999 
4000 	if (esw->offloads.encap == encap)
4001 		goto unlock;
4002 
4003 	if (atomic64_read(&esw->offloads.num_flows) > 0) {
4004 		NL_SET_ERR_MSG_MOD(extack,
4005 				   "Can't set encapsulation when flows are configured");
4006 		err = -EOPNOTSUPP;
4007 		goto unlock;
4008 	}
4009 
4010 	if (esw->offloads.num_block_encap) {
4011 		NL_SET_ERR_MSG_MOD(extack,
4012 				   "Can't set encapsulation when IPsec SA and/or policies are configured");
4013 		err = -EOPNOTSUPP;
4014 		goto unlock;
4015 	}
4016 
4017 	esw->eswitch_operation_in_progress = true;
4018 	up_write(&esw->mode_lock);
4019 
4020 	esw_destroy_offloads_fdb_tables(esw);
4021 
4022 	esw->offloads.encap = encap;
4023 
4024 	err = esw_create_offloads_fdb_tables(esw);
4025 
4026 	if (err) {
4027 		NL_SET_ERR_MSG_MOD(extack,
4028 				   "Failed re-creating fast FDB table");
4029 		esw->offloads.encap = !encap;
4030 		(void)esw_create_offloads_fdb_tables(esw);
4031 	}
4032 
4033 	down_write(&esw->mode_lock);
4034 	esw->eswitch_operation_in_progress = false;
4035 
4036 unlock:
4037 	up_write(&esw->mode_lock);
4038 	return err;
4039 }
4040 
mlx5_devlink_eswitch_encap_mode_get(struct devlink * devlink,enum devlink_eswitch_encap_mode * encap)4041 int mlx5_devlink_eswitch_encap_mode_get(struct devlink *devlink,
4042 					enum devlink_eswitch_encap_mode *encap)
4043 {
4044 	struct mlx5_eswitch *esw;
4045 
4046 	esw = mlx5_devlink_eswitch_get(devlink);
4047 	if (IS_ERR(esw))
4048 		return PTR_ERR(esw);
4049 
4050 	*encap = esw->offloads.encap;
4051 	return 0;
4052 }
4053 
4054 static bool
mlx5_eswitch_vport_has_rep(const struct mlx5_eswitch * esw,u16 vport_num)4055 mlx5_eswitch_vport_has_rep(const struct mlx5_eswitch *esw, u16 vport_num)
4056 {
4057 	/* Currently, only ECPF based device has representor for host PF. */
4058 	if (vport_num == MLX5_VPORT_PF &&
4059 	    !mlx5_core_is_ecpf_esw_manager(esw->dev))
4060 		return false;
4061 
4062 	if (vport_num == MLX5_VPORT_ECPF &&
4063 	    !mlx5_ecpf_vport_exists(esw->dev))
4064 		return false;
4065 
4066 	return true;
4067 }
4068 
mlx5_eswitch_register_vport_reps(struct mlx5_eswitch * esw,const struct mlx5_eswitch_rep_ops * ops,u8 rep_type)4069 void mlx5_eswitch_register_vport_reps(struct mlx5_eswitch *esw,
4070 				      const struct mlx5_eswitch_rep_ops *ops,
4071 				      u8 rep_type)
4072 {
4073 	struct mlx5_eswitch_rep_data *rep_data;
4074 	struct mlx5_eswitch_rep *rep;
4075 	unsigned long i;
4076 
4077 	esw->offloads.rep_ops[rep_type] = ops;
4078 	mlx5_esw_for_each_rep(esw, i, rep) {
4079 		if (likely(mlx5_eswitch_vport_has_rep(esw, rep->vport))) {
4080 			rep->esw = esw;
4081 			rep_data = &rep->rep_data[rep_type];
4082 			atomic_set(&rep_data->state, REP_REGISTERED);
4083 		}
4084 	}
4085 }
4086 EXPORT_SYMBOL(mlx5_eswitch_register_vport_reps);
4087 
mlx5_eswitch_unregister_vport_reps(struct mlx5_eswitch * esw,u8 rep_type)4088 void mlx5_eswitch_unregister_vport_reps(struct mlx5_eswitch *esw, u8 rep_type)
4089 {
4090 	struct mlx5_eswitch_rep *rep;
4091 	unsigned long i;
4092 
4093 	if (esw->mode == MLX5_ESWITCH_OFFLOADS)
4094 		__unload_reps_all_vport(esw, rep_type);
4095 
4096 	mlx5_esw_for_each_rep(esw, i, rep)
4097 		atomic_set(&rep->rep_data[rep_type].state, REP_UNREGISTERED);
4098 }
4099 EXPORT_SYMBOL(mlx5_eswitch_unregister_vport_reps);
4100 
mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch * esw,u8 rep_type)4101 void *mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch *esw, u8 rep_type)
4102 {
4103 	struct mlx5_eswitch_rep *rep;
4104 
4105 	rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_UPLINK);
4106 	return rep->rep_data[rep_type].priv;
4107 }
4108 
mlx5_eswitch_get_proto_dev(struct mlx5_eswitch * esw,u16 vport,u8 rep_type)4109 void *mlx5_eswitch_get_proto_dev(struct mlx5_eswitch *esw,
4110 				 u16 vport,
4111 				 u8 rep_type)
4112 {
4113 	struct mlx5_eswitch_rep *rep;
4114 
4115 	rep = mlx5_eswitch_get_rep(esw, vport);
4116 
4117 	if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
4118 	    esw->offloads.rep_ops[rep_type]->get_proto_dev)
4119 		return esw->offloads.rep_ops[rep_type]->get_proto_dev(rep);
4120 	return NULL;
4121 }
4122 EXPORT_SYMBOL(mlx5_eswitch_get_proto_dev);
4123 
mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch * esw,u8 rep_type)4124 void *mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch *esw, u8 rep_type)
4125 {
4126 	return mlx5_eswitch_get_proto_dev(esw, MLX5_VPORT_UPLINK, rep_type);
4127 }
4128 EXPORT_SYMBOL(mlx5_eswitch_uplink_get_proto_dev);
4129 
mlx5_eswitch_vport_rep(struct mlx5_eswitch * esw,u16 vport)4130 struct mlx5_eswitch_rep *mlx5_eswitch_vport_rep(struct mlx5_eswitch *esw,
4131 						u16 vport)
4132 {
4133 	return mlx5_eswitch_get_rep(esw, vport);
4134 }
4135 EXPORT_SYMBOL(mlx5_eswitch_vport_rep);
4136 
mlx5_eswitch_reg_c1_loopback_enabled(const struct mlx5_eswitch * esw)4137 bool mlx5_eswitch_reg_c1_loopback_enabled(const struct mlx5_eswitch *esw)
4138 {
4139 	return !!(esw->flags & MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED);
4140 }
4141 EXPORT_SYMBOL(mlx5_eswitch_reg_c1_loopback_enabled);
4142 
mlx5_eswitch_vport_match_metadata_enabled(const struct mlx5_eswitch * esw)4143 bool mlx5_eswitch_vport_match_metadata_enabled(const struct mlx5_eswitch *esw)
4144 {
4145 	return !!(esw->flags & MLX5_ESWITCH_VPORT_MATCH_METADATA);
4146 }
4147 EXPORT_SYMBOL(mlx5_eswitch_vport_match_metadata_enabled);
4148 
mlx5_eswitch_get_vport_metadata_for_match(struct mlx5_eswitch * esw,u16 vport_num)4149 u32 mlx5_eswitch_get_vport_metadata_for_match(struct mlx5_eswitch *esw,
4150 					      u16 vport_num)
4151 {
4152 	struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
4153 
4154 	if (WARN_ON_ONCE(IS_ERR(vport)))
4155 		return 0;
4156 
4157 	return vport->metadata << (32 - ESW_SOURCE_PORT_METADATA_BITS);
4158 }
4159 EXPORT_SYMBOL(mlx5_eswitch_get_vport_metadata_for_match);
4160 
mlx5_esw_query_vport_vhca_id(struct mlx5_eswitch * esw,u16 vport_num,u16 * vhca_id)4161 static int mlx5_esw_query_vport_vhca_id(struct mlx5_eswitch *esw, u16 vport_num, u16 *vhca_id)
4162 {
4163 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4164 	void *query_ctx;
4165 	void *hca_caps;
4166 	int err;
4167 
4168 	*vhca_id = 0;
4169 
4170 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4171 	if (!query_ctx)
4172 		return -ENOMEM;
4173 
4174 	err = mlx5_vport_get_other_func_general_cap(esw->dev, vport_num, query_ctx);
4175 	if (err)
4176 		goto out_free;
4177 
4178 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4179 	*vhca_id = MLX5_GET(cmd_hca_cap, hca_caps, vhca_id);
4180 
4181 out_free:
4182 	kfree(query_ctx);
4183 	return err;
4184 }
4185 
mlx5_esw_vport_vhca_id_set(struct mlx5_eswitch * esw,u16 vport_num)4186 int mlx5_esw_vport_vhca_id_set(struct mlx5_eswitch *esw, u16 vport_num)
4187 {
4188 	u16 *old_entry, *vhca_map_entry, vhca_id;
4189 	int err;
4190 
4191 	err = mlx5_esw_query_vport_vhca_id(esw, vport_num, &vhca_id);
4192 	if (err) {
4193 		esw_warn(esw->dev, "Getting vhca_id for vport failed (vport=%u,err=%d)\n",
4194 			 vport_num, err);
4195 		return err;
4196 	}
4197 
4198 	vhca_map_entry = kmalloc(sizeof(*vhca_map_entry), GFP_KERNEL);
4199 	if (!vhca_map_entry)
4200 		return -ENOMEM;
4201 
4202 	*vhca_map_entry = vport_num;
4203 	old_entry = xa_store(&esw->offloads.vhca_map, vhca_id, vhca_map_entry, GFP_KERNEL);
4204 	if (xa_is_err(old_entry)) {
4205 		kfree(vhca_map_entry);
4206 		return xa_err(old_entry);
4207 	}
4208 	kfree(old_entry);
4209 	return 0;
4210 }
4211 
mlx5_esw_vport_vhca_id_clear(struct mlx5_eswitch * esw,u16 vport_num)4212 void mlx5_esw_vport_vhca_id_clear(struct mlx5_eswitch *esw, u16 vport_num)
4213 {
4214 	u16 *vhca_map_entry, vhca_id;
4215 	int err;
4216 
4217 	err = mlx5_esw_query_vport_vhca_id(esw, vport_num, &vhca_id);
4218 	if (err)
4219 		esw_warn(esw->dev, "Getting vhca_id for vport failed (vport=%hu,err=%d)\n",
4220 			 vport_num, err);
4221 
4222 	vhca_map_entry = xa_erase(&esw->offloads.vhca_map, vhca_id);
4223 	kfree(vhca_map_entry);
4224 }
4225 
mlx5_eswitch_vhca_id_to_vport(struct mlx5_eswitch * esw,u16 vhca_id,u16 * vport_num)4226 int mlx5_eswitch_vhca_id_to_vport(struct mlx5_eswitch *esw, u16 vhca_id, u16 *vport_num)
4227 {
4228 	u16 *res = xa_load(&esw->offloads.vhca_map, vhca_id);
4229 
4230 	if (!res)
4231 		return -ENOENT;
4232 
4233 	*vport_num = *res;
4234 	return 0;
4235 }
4236 
mlx5_eswitch_get_vport_metadata_for_set(struct mlx5_eswitch * esw,u16 vport_num)4237 u32 mlx5_eswitch_get_vport_metadata_for_set(struct mlx5_eswitch *esw,
4238 					    u16 vport_num)
4239 {
4240 	struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
4241 
4242 	if (WARN_ON_ONCE(IS_ERR(vport)))
4243 		return 0;
4244 
4245 	return vport->metadata;
4246 }
4247 EXPORT_SYMBOL(mlx5_eswitch_get_vport_metadata_for_set);
4248 
mlx5_devlink_port_fn_hw_addr_get(struct devlink_port * port,u8 * hw_addr,int * hw_addr_len,struct netlink_ext_ack * extack)4249 int mlx5_devlink_port_fn_hw_addr_get(struct devlink_port *port,
4250 				     u8 *hw_addr, int *hw_addr_len,
4251 				     struct netlink_ext_ack *extack)
4252 {
4253 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4254 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4255 
4256 	mutex_lock(&esw->state_lock);
4257 	ether_addr_copy(hw_addr, vport->info.mac);
4258 	*hw_addr_len = ETH_ALEN;
4259 	mutex_unlock(&esw->state_lock);
4260 	return 0;
4261 }
4262 
mlx5_devlink_port_fn_hw_addr_set(struct devlink_port * port,const u8 * hw_addr,int hw_addr_len,struct netlink_ext_ack * extack)4263 int mlx5_devlink_port_fn_hw_addr_set(struct devlink_port *port,
4264 				     const u8 *hw_addr, int hw_addr_len,
4265 				     struct netlink_ext_ack *extack)
4266 {
4267 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4268 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4269 
4270 	return mlx5_eswitch_set_vport_mac(esw, vport->vport, hw_addr);
4271 }
4272 
mlx5_devlink_port_fn_migratable_get(struct devlink_port * port,bool * is_enabled,struct netlink_ext_ack * extack)4273 int mlx5_devlink_port_fn_migratable_get(struct devlink_port *port, bool *is_enabled,
4274 					struct netlink_ext_ack *extack)
4275 {
4276 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4277 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4278 
4279 	if (!MLX5_CAP_GEN(esw->dev, migration)) {
4280 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support migration");
4281 		return -EOPNOTSUPP;
4282 	}
4283 
4284 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4285 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support VHCA management");
4286 		return -EOPNOTSUPP;
4287 	}
4288 
4289 	mutex_lock(&esw->state_lock);
4290 	*is_enabled = vport->info.mig_enabled;
4291 	mutex_unlock(&esw->state_lock);
4292 	return 0;
4293 }
4294 
mlx5_devlink_port_fn_migratable_set(struct devlink_port * port,bool enable,struct netlink_ext_ack * extack)4295 int mlx5_devlink_port_fn_migratable_set(struct devlink_port *port, bool enable,
4296 					struct netlink_ext_ack *extack)
4297 {
4298 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4299 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4300 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4301 	void *query_ctx;
4302 	void *hca_caps;
4303 	int err;
4304 
4305 	if (!MLX5_CAP_GEN(esw->dev, migration)) {
4306 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support migration");
4307 		return -EOPNOTSUPP;
4308 	}
4309 
4310 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4311 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support VHCA management");
4312 		return -EOPNOTSUPP;
4313 	}
4314 
4315 	mutex_lock(&esw->state_lock);
4316 
4317 	if (vport->info.mig_enabled == enable) {
4318 		err = 0;
4319 		goto out;
4320 	}
4321 
4322 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4323 	if (!query_ctx) {
4324 		err = -ENOMEM;
4325 		goto out;
4326 	}
4327 
4328 	err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
4329 					    MLX5_CAP_GENERAL_2);
4330 	if (err) {
4331 		NL_SET_ERR_MSG_MOD(extack, "Failed getting HCA caps");
4332 		goto out_free;
4333 	}
4334 
4335 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4336 	MLX5_SET(cmd_hca_cap_2, hca_caps, migratable, enable);
4337 
4338 	err = mlx5_vport_set_other_func_cap(esw->dev, hca_caps, vport->vport,
4339 					    MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE2);
4340 	if (err) {
4341 		NL_SET_ERR_MSG_MOD(extack, "Failed setting HCA migratable cap");
4342 		goto out_free;
4343 	}
4344 
4345 	vport->info.mig_enabled = enable;
4346 
4347 out_free:
4348 	kfree(query_ctx);
4349 out:
4350 	mutex_unlock(&esw->state_lock);
4351 	return err;
4352 }
4353 
mlx5_devlink_port_fn_roce_get(struct devlink_port * port,bool * is_enabled,struct netlink_ext_ack * extack)4354 int mlx5_devlink_port_fn_roce_get(struct devlink_port *port, bool *is_enabled,
4355 				  struct netlink_ext_ack *extack)
4356 {
4357 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4358 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4359 
4360 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4361 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support VHCA management");
4362 		return -EOPNOTSUPP;
4363 	}
4364 
4365 	mutex_lock(&esw->state_lock);
4366 	*is_enabled = vport->info.roce_enabled;
4367 	mutex_unlock(&esw->state_lock);
4368 	return 0;
4369 }
4370 
mlx5_devlink_port_fn_roce_set(struct devlink_port * port,bool enable,struct netlink_ext_ack * extack)4371 int mlx5_devlink_port_fn_roce_set(struct devlink_port *port, bool enable,
4372 				  struct netlink_ext_ack *extack)
4373 {
4374 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4375 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4376 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4377 	u16 vport_num = vport->vport;
4378 	void *query_ctx;
4379 	void *hca_caps;
4380 	int err;
4381 
4382 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4383 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support VHCA management");
4384 		return -EOPNOTSUPP;
4385 	}
4386 
4387 	mutex_lock(&esw->state_lock);
4388 
4389 	if (vport->info.roce_enabled == enable) {
4390 		err = 0;
4391 		goto out;
4392 	}
4393 
4394 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4395 	if (!query_ctx) {
4396 		err = -ENOMEM;
4397 		goto out;
4398 	}
4399 
4400 	err = mlx5_vport_get_other_func_cap(esw->dev, vport_num, query_ctx,
4401 					    MLX5_CAP_GENERAL);
4402 	if (err) {
4403 		NL_SET_ERR_MSG_MOD(extack, "Failed getting HCA caps");
4404 		goto out_free;
4405 	}
4406 
4407 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4408 	MLX5_SET(cmd_hca_cap, hca_caps, roce, enable);
4409 
4410 	err = mlx5_vport_set_other_func_cap(esw->dev, hca_caps, vport_num,
4411 					    MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
4412 	if (err) {
4413 		NL_SET_ERR_MSG_MOD(extack, "Failed setting HCA roce cap");
4414 		goto out_free;
4415 	}
4416 
4417 	vport->info.roce_enabled = enable;
4418 
4419 out_free:
4420 	kfree(query_ctx);
4421 out:
4422 	mutex_unlock(&esw->state_lock);
4423 	return err;
4424 }
4425 
4426 int
mlx5_eswitch_restore_ipsec_rule(struct mlx5_eswitch * esw,struct mlx5_flow_handle * rule,struct mlx5_esw_flow_attr * esw_attr,int attr_idx)4427 mlx5_eswitch_restore_ipsec_rule(struct mlx5_eswitch *esw, struct mlx5_flow_handle *rule,
4428 				struct mlx5_esw_flow_attr *esw_attr, int attr_idx)
4429 {
4430 	struct mlx5_flow_destination new_dest = {};
4431 	struct mlx5_flow_destination old_dest = {};
4432 
4433 	if (!esw_setup_uplink_fwd_ipsec_needed(esw, esw_attr, attr_idx))
4434 		return 0;
4435 
4436 	esw_setup_dest_fwd_ipsec(&old_dest, NULL, esw, esw_attr, attr_idx, 0, false);
4437 	esw_setup_dest_fwd_vport(&new_dest, NULL, esw, esw_attr, attr_idx, 0, false);
4438 
4439 	return mlx5_modify_rule_destination(rule, &new_dest, &old_dest);
4440 }
4441 
4442 #ifdef CONFIG_XFRM_OFFLOAD
mlx5_devlink_port_fn_ipsec_crypto_get(struct devlink_port * port,bool * is_enabled,struct netlink_ext_ack * extack)4443 int mlx5_devlink_port_fn_ipsec_crypto_get(struct devlink_port *port, bool *is_enabled,
4444 					  struct netlink_ext_ack *extack)
4445 {
4446 	struct mlx5_eswitch *esw;
4447 	struct mlx5_vport *vport;
4448 	int err = 0;
4449 
4450 	esw = mlx5_devlink_eswitch_get(port->devlink);
4451 	if (IS_ERR(esw))
4452 		return PTR_ERR(esw);
4453 
4454 	if (!mlx5_esw_ipsec_vf_offload_supported(esw->dev)) {
4455 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support IPSec crypto");
4456 		return -EOPNOTSUPP;
4457 	}
4458 
4459 	vport = mlx5_devlink_port_vport_get(port);
4460 
4461 	mutex_lock(&esw->state_lock);
4462 	if (!vport->enabled) {
4463 		err = -EOPNOTSUPP;
4464 		goto unlock;
4465 	}
4466 
4467 	*is_enabled = vport->info.ipsec_crypto_enabled;
4468 unlock:
4469 	mutex_unlock(&esw->state_lock);
4470 	return err;
4471 }
4472 
mlx5_devlink_port_fn_ipsec_crypto_set(struct devlink_port * port,bool enable,struct netlink_ext_ack * extack)4473 int mlx5_devlink_port_fn_ipsec_crypto_set(struct devlink_port *port, bool enable,
4474 					  struct netlink_ext_ack *extack)
4475 {
4476 	struct mlx5_eswitch *esw;
4477 	struct mlx5_vport *vport;
4478 	u16 vport_num;
4479 	int err;
4480 
4481 	esw = mlx5_devlink_eswitch_get(port->devlink);
4482 	if (IS_ERR(esw))
4483 		return PTR_ERR(esw);
4484 
4485 	vport_num = mlx5_esw_devlink_port_index_to_vport_num(port->index);
4486 	err = mlx5_esw_ipsec_vf_crypto_offload_supported(esw->dev, vport_num);
4487 	if (err) {
4488 		NL_SET_ERR_MSG_MOD(extack,
4489 				   "Device doesn't support IPsec crypto");
4490 		return err;
4491 	}
4492 
4493 	vport = mlx5_devlink_port_vport_get(port);
4494 
4495 	mutex_lock(&esw->state_lock);
4496 	if (!vport->enabled) {
4497 		err = -EOPNOTSUPP;
4498 		NL_SET_ERR_MSG_MOD(extack, "Eswitch vport is disabled");
4499 		goto unlock;
4500 	}
4501 
4502 	if (vport->info.ipsec_crypto_enabled == enable)
4503 		goto unlock;
4504 
4505 	if (!esw->enabled_ipsec_vf_count && esw->dev->num_ipsec_offloads) {
4506 		err = -EBUSY;
4507 		goto unlock;
4508 	}
4509 
4510 	err = mlx5_esw_ipsec_vf_crypto_offload_set(esw, vport, enable);
4511 	if (err) {
4512 		NL_SET_ERR_MSG_MOD(extack, "Failed to set IPsec crypto");
4513 		goto unlock;
4514 	}
4515 
4516 	vport->info.ipsec_crypto_enabled = enable;
4517 	if (enable)
4518 		esw->enabled_ipsec_vf_count++;
4519 	else
4520 		esw->enabled_ipsec_vf_count--;
4521 unlock:
4522 	mutex_unlock(&esw->state_lock);
4523 	return err;
4524 }
4525 
mlx5_devlink_port_fn_ipsec_packet_get(struct devlink_port * port,bool * is_enabled,struct netlink_ext_ack * extack)4526 int mlx5_devlink_port_fn_ipsec_packet_get(struct devlink_port *port, bool *is_enabled,
4527 					  struct netlink_ext_ack *extack)
4528 {
4529 	struct mlx5_eswitch *esw;
4530 	struct mlx5_vport *vport;
4531 	int err = 0;
4532 
4533 	esw = mlx5_devlink_eswitch_get(port->devlink);
4534 	if (IS_ERR(esw))
4535 		return PTR_ERR(esw);
4536 
4537 	if (!mlx5_esw_ipsec_vf_offload_supported(esw->dev)) {
4538 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support IPsec packet");
4539 		return -EOPNOTSUPP;
4540 	}
4541 
4542 	vport = mlx5_devlink_port_vport_get(port);
4543 
4544 	mutex_lock(&esw->state_lock);
4545 	if (!vport->enabled) {
4546 		err = -EOPNOTSUPP;
4547 		goto unlock;
4548 	}
4549 
4550 	*is_enabled = vport->info.ipsec_packet_enabled;
4551 unlock:
4552 	mutex_unlock(&esw->state_lock);
4553 	return err;
4554 }
4555 
mlx5_devlink_port_fn_ipsec_packet_set(struct devlink_port * port,bool enable,struct netlink_ext_ack * extack)4556 int mlx5_devlink_port_fn_ipsec_packet_set(struct devlink_port *port,
4557 					  bool enable,
4558 					  struct netlink_ext_ack *extack)
4559 {
4560 	struct mlx5_eswitch *esw;
4561 	struct mlx5_vport *vport;
4562 	u16 vport_num;
4563 	int err;
4564 
4565 	esw = mlx5_devlink_eswitch_get(port->devlink);
4566 	if (IS_ERR(esw))
4567 		return PTR_ERR(esw);
4568 
4569 	vport_num = mlx5_esw_devlink_port_index_to_vport_num(port->index);
4570 	err = mlx5_esw_ipsec_vf_packet_offload_supported(esw->dev, vport_num);
4571 	if (err) {
4572 		NL_SET_ERR_MSG_MOD(extack,
4573 				   "Device doesn't support IPsec packet mode");
4574 		return err;
4575 	}
4576 
4577 	vport = mlx5_devlink_port_vport_get(port);
4578 	mutex_lock(&esw->state_lock);
4579 	if (!vport->enabled) {
4580 		err = -EOPNOTSUPP;
4581 		NL_SET_ERR_MSG_MOD(extack, "Eswitch vport is disabled");
4582 		goto unlock;
4583 	}
4584 
4585 	if (vport->info.ipsec_packet_enabled == enable)
4586 		goto unlock;
4587 
4588 	if (!esw->enabled_ipsec_vf_count && esw->dev->num_ipsec_offloads) {
4589 		err = -EBUSY;
4590 		goto unlock;
4591 	}
4592 
4593 	err = mlx5_esw_ipsec_vf_packet_offload_set(esw, vport, enable);
4594 	if (err) {
4595 		NL_SET_ERR_MSG_MOD(extack,
4596 				   "Failed to set IPsec packet mode");
4597 		goto unlock;
4598 	}
4599 
4600 	vport->info.ipsec_packet_enabled = enable;
4601 	if (enable)
4602 		esw->enabled_ipsec_vf_count++;
4603 	else
4604 		esw->enabled_ipsec_vf_count--;
4605 unlock:
4606 	mutex_unlock(&esw->state_lock);
4607 	return err;
4608 }
4609 #endif /* CONFIG_XFRM_OFFLOAD */
4610 
4611 int
mlx5_devlink_port_fn_max_io_eqs_get(struct devlink_port * port,u32 * max_io_eqs,struct netlink_ext_ack * extack)4612 mlx5_devlink_port_fn_max_io_eqs_get(struct devlink_port *port, u32 *max_io_eqs,
4613 				    struct netlink_ext_ack *extack)
4614 {
4615 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4616 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4617 	u16 vport_num = vport->vport;
4618 	struct mlx5_eswitch *esw;
4619 	void *query_ctx;
4620 	void *hca_caps;
4621 	u32 max_eqs;
4622 	int err;
4623 
4624 	esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4625 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4626 		NL_SET_ERR_MSG_MOD(extack,
4627 				   "Device doesn't support VHCA management");
4628 		return -EOPNOTSUPP;
4629 	}
4630 
4631 	if (!MLX5_CAP_GEN_2(esw->dev, max_num_eqs_24b)) {
4632 		NL_SET_ERR_MSG_MOD(extack,
4633 				   "Device doesn't support getting the max number of EQs");
4634 		return -EOPNOTSUPP;
4635 	}
4636 
4637 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4638 	if (!query_ctx)
4639 		return -ENOMEM;
4640 
4641 	mutex_lock(&esw->state_lock);
4642 	err = mlx5_vport_get_other_func_cap(esw->dev, vport_num, query_ctx,
4643 					    MLX5_CAP_GENERAL_2);
4644 	if (err) {
4645 		NL_SET_ERR_MSG_MOD(extack, "Failed getting HCA caps");
4646 		goto out;
4647 	}
4648 
4649 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4650 	max_eqs = MLX5_GET(cmd_hca_cap_2, hca_caps, max_num_eqs_24b);
4651 	if (max_eqs < MLX5_ESW_MAX_CTRL_EQS)
4652 		*max_io_eqs = 0;
4653 	else
4654 		*max_io_eqs = max_eqs - MLX5_ESW_MAX_CTRL_EQS;
4655 out:
4656 	mutex_unlock(&esw->state_lock);
4657 	kfree(query_ctx);
4658 	return err;
4659 }
4660 
4661 int
mlx5_devlink_port_fn_max_io_eqs_set(struct devlink_port * port,u32 max_io_eqs,struct netlink_ext_ack * extack)4662 mlx5_devlink_port_fn_max_io_eqs_set(struct devlink_port *port, u32 max_io_eqs,
4663 				    struct netlink_ext_ack *extack)
4664 {
4665 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4666 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4667 	u16 vport_num = vport->vport;
4668 	struct mlx5_eswitch *esw;
4669 	void *query_ctx;
4670 	void *hca_caps;
4671 	u16 max_eqs;
4672 	int err;
4673 
4674 	esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4675 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4676 		NL_SET_ERR_MSG_MOD(extack,
4677 				   "Device doesn't support VHCA management");
4678 		return -EOPNOTSUPP;
4679 	}
4680 
4681 	if (!MLX5_CAP_GEN_2(esw->dev, max_num_eqs_24b)) {
4682 		NL_SET_ERR_MSG_MOD(extack,
4683 				   "Device doesn't support changing the max number of EQs");
4684 		return -EOPNOTSUPP;
4685 	}
4686 
4687 	if (check_add_overflow(max_io_eqs, MLX5_ESW_MAX_CTRL_EQS, &max_eqs)) {
4688 		NL_SET_ERR_MSG_MOD(extack, "Supplied value out of range");
4689 		return -EINVAL;
4690 	}
4691 
4692 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4693 	if (!query_ctx)
4694 		return -ENOMEM;
4695 
4696 	mutex_lock(&esw->state_lock);
4697 	err = mlx5_vport_get_other_func_cap(esw->dev, vport_num, query_ctx,
4698 					    MLX5_CAP_GENERAL_2);
4699 	if (err) {
4700 		NL_SET_ERR_MSG_MOD(extack, "Failed getting HCA caps");
4701 		goto out;
4702 	}
4703 
4704 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4705 	MLX5_SET(cmd_hca_cap_2, hca_caps, max_num_eqs_24b, max_eqs);
4706 
4707 	if (mlx5_esw_is_sf_vport(esw, vport_num))
4708 		MLX5_SET(cmd_hca_cap_2, hca_caps, sf_eq_usage, 1);
4709 
4710 	err = mlx5_vport_set_other_func_cap(esw->dev, hca_caps, vport_num,
4711 					    MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE2);
4712 	if (err)
4713 		NL_SET_ERR_MSG_MOD(extack, "Failed setting HCA caps");
4714 	vport->max_eqs_set = true;
4715 out:
4716 	mutex_unlock(&esw->state_lock);
4717 	kfree(query_ctx);
4718 	return err;
4719 }
4720 
4721 int
mlx5_devlink_port_fn_max_io_eqs_set_sf_default(struct devlink_port * port,struct netlink_ext_ack * extack)4722 mlx5_devlink_port_fn_max_io_eqs_set_sf_default(struct devlink_port *port,
4723 					       struct netlink_ext_ack *extack)
4724 {
4725 	return mlx5_devlink_port_fn_max_io_eqs_set(port,
4726 						   MLX5_ESW_DEFAULT_SF_COMP_EQS,
4727 						   extack);
4728 }
4729