xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads.c (revision cfaaa7d010d1fc58f9717fcc8591201e741d2d49)
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_offloads_start(struct mlx5_eswitch * esw,struct netlink_ext_ack * extack)2335 static int esw_offloads_start(struct mlx5_eswitch *esw,
2336 			      struct netlink_ext_ack *extack)
2337 {
2338 	int err;
2339 
2340 	esw->mode = MLX5_ESWITCH_OFFLOADS;
2341 	err = mlx5_eswitch_enable_locked(esw, esw->dev->priv.sriov.num_vfs);
2342 	if (err) {
2343 		NL_SET_ERR_MSG_MOD(extack,
2344 				   "Failed setting eswitch to offloads");
2345 		esw->mode = MLX5_ESWITCH_LEGACY;
2346 		mlx5_rescan_drivers(esw->dev);
2347 		return err;
2348 	}
2349 	if (esw->offloads.inline_mode == MLX5_INLINE_MODE_NONE) {
2350 		if (mlx5_eswitch_inline_mode_get(esw,
2351 						 &esw->offloads.inline_mode)) {
2352 			esw->offloads.inline_mode = MLX5_INLINE_MODE_L2;
2353 			NL_SET_ERR_MSG_MOD(extack,
2354 					   "Inline mode is different between vports");
2355 		}
2356 	}
2357 	return 0;
2358 }
2359 
mlx5_esw_offloads_rep_init(struct mlx5_eswitch * esw,const struct mlx5_vport * vport)2360 static int mlx5_esw_offloads_rep_init(struct mlx5_eswitch *esw, const struct mlx5_vport *vport)
2361 {
2362 	struct mlx5_eswitch_rep *rep;
2363 	int rep_type;
2364 	int err;
2365 
2366 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
2367 	if (!rep)
2368 		return -ENOMEM;
2369 
2370 	rep->vport = vport->vport;
2371 	rep->vport_index = vport->index;
2372 	for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++)
2373 		atomic_set(&rep->rep_data[rep_type].state, REP_UNREGISTERED);
2374 
2375 	err = xa_insert(&esw->offloads.vport_reps, rep->vport, rep, GFP_KERNEL);
2376 	if (err)
2377 		goto insert_err;
2378 
2379 	return 0;
2380 
2381 insert_err:
2382 	kfree(rep);
2383 	return err;
2384 }
2385 
mlx5_esw_offloads_rep_cleanup(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep)2386 static void mlx5_esw_offloads_rep_cleanup(struct mlx5_eswitch *esw,
2387 					  struct mlx5_eswitch_rep *rep)
2388 {
2389 	xa_erase(&esw->offloads.vport_reps, rep->vport);
2390 	kfree(rep);
2391 }
2392 
esw_offloads_cleanup_reps(struct mlx5_eswitch * esw)2393 static void esw_offloads_cleanup_reps(struct mlx5_eswitch *esw)
2394 {
2395 	struct mlx5_eswitch_rep *rep;
2396 	unsigned long i;
2397 
2398 	mlx5_esw_for_each_rep(esw, i, rep)
2399 		mlx5_esw_offloads_rep_cleanup(esw, rep);
2400 	xa_destroy(&esw->offloads.vport_reps);
2401 }
2402 
esw_offloads_init_reps(struct mlx5_eswitch * esw)2403 static int esw_offloads_init_reps(struct mlx5_eswitch *esw)
2404 {
2405 	struct mlx5_vport *vport;
2406 	unsigned long i;
2407 	int err;
2408 
2409 	xa_init(&esw->offloads.vport_reps);
2410 
2411 	mlx5_esw_for_each_vport(esw, i, vport) {
2412 		err = mlx5_esw_offloads_rep_init(esw, vport);
2413 		if (err)
2414 			goto err;
2415 	}
2416 	return 0;
2417 
2418 err:
2419 	esw_offloads_cleanup_reps(esw);
2420 	return err;
2421 }
2422 
esw_port_metadata_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx,struct netlink_ext_ack * extack)2423 static int esw_port_metadata_set(struct devlink *devlink, u32 id,
2424 				 struct devlink_param_gset_ctx *ctx,
2425 				 struct netlink_ext_ack *extack)
2426 {
2427 	struct mlx5_core_dev *dev = devlink_priv(devlink);
2428 	struct mlx5_eswitch *esw = dev->priv.eswitch;
2429 	int err = 0;
2430 
2431 	down_write(&esw->mode_lock);
2432 	if (mlx5_esw_is_fdb_created(esw)) {
2433 		err = -EBUSY;
2434 		goto done;
2435 	}
2436 	if (!mlx5_esw_vport_match_metadata_supported(esw)) {
2437 		err = -EOPNOTSUPP;
2438 		goto done;
2439 	}
2440 	if (ctx->val.vbool)
2441 		esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
2442 	else
2443 		esw->flags &= ~MLX5_ESWITCH_VPORT_MATCH_METADATA;
2444 done:
2445 	up_write(&esw->mode_lock);
2446 	return err;
2447 }
2448 
esw_port_metadata_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)2449 static int esw_port_metadata_get(struct devlink *devlink, u32 id,
2450 				 struct devlink_param_gset_ctx *ctx)
2451 {
2452 	struct mlx5_core_dev *dev = devlink_priv(devlink);
2453 
2454 	ctx->val.vbool = mlx5_eswitch_vport_match_metadata_enabled(dev->priv.eswitch);
2455 	return 0;
2456 }
2457 
esw_port_metadata_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)2458 static int esw_port_metadata_validate(struct devlink *devlink, u32 id,
2459 				      union devlink_param_value val,
2460 				      struct netlink_ext_ack *extack)
2461 {
2462 	struct mlx5_core_dev *dev = devlink_priv(devlink);
2463 	u8 esw_mode;
2464 
2465 	esw_mode = mlx5_eswitch_mode(dev);
2466 	if (esw_mode == MLX5_ESWITCH_OFFLOADS) {
2467 		NL_SET_ERR_MSG_MOD(extack,
2468 				   "E-Switch must either disabled or non switchdev mode");
2469 		return -EBUSY;
2470 	}
2471 	return 0;
2472 }
2473 
2474 static const struct devlink_param esw_devlink_params[] = {
2475 	DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_ESW_PORT_METADATA,
2476 			     "esw_port_metadata", DEVLINK_PARAM_TYPE_BOOL,
2477 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
2478 			     esw_port_metadata_get,
2479 			     esw_port_metadata_set,
2480 			     esw_port_metadata_validate),
2481 };
2482 
esw_offloads_init(struct mlx5_eswitch * esw)2483 int esw_offloads_init(struct mlx5_eswitch *esw)
2484 {
2485 	int err;
2486 
2487 	err = esw_offloads_init_reps(esw);
2488 	if (err)
2489 		return err;
2490 
2491 	if (MLX5_ESWITCH_MANAGER(esw->dev) &&
2492 	    mlx5_esw_vport_match_metadata_supported(esw))
2493 		esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
2494 
2495 	err = devl_params_register(priv_to_devlink(esw->dev),
2496 				   esw_devlink_params,
2497 				   ARRAY_SIZE(esw_devlink_params));
2498 	if (err)
2499 		goto err_params;
2500 
2501 	return 0;
2502 
2503 err_params:
2504 	esw_offloads_cleanup_reps(esw);
2505 	return err;
2506 }
2507 
esw_offloads_cleanup(struct mlx5_eswitch * esw)2508 void esw_offloads_cleanup(struct mlx5_eswitch *esw)
2509 {
2510 	devl_params_unregister(priv_to_devlink(esw->dev),
2511 			       esw_devlink_params,
2512 			       ARRAY_SIZE(esw_devlink_params));
2513 	esw_offloads_cleanup_reps(esw);
2514 }
2515 
__esw_offloads_load_rep(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep,u8 rep_type)2516 static int __esw_offloads_load_rep(struct mlx5_eswitch *esw,
2517 				   struct mlx5_eswitch_rep *rep, u8 rep_type)
2518 {
2519 	if (atomic_cmpxchg(&rep->rep_data[rep_type].state,
2520 			   REP_REGISTERED, REP_LOADED) == REP_REGISTERED)
2521 		return esw->offloads.rep_ops[rep_type]->load(esw->dev, rep);
2522 
2523 	return 0;
2524 }
2525 
__esw_offloads_unload_rep(struct mlx5_eswitch * esw,struct mlx5_eswitch_rep * rep,u8 rep_type)2526 static void __esw_offloads_unload_rep(struct mlx5_eswitch *esw,
2527 				      struct mlx5_eswitch_rep *rep, u8 rep_type)
2528 {
2529 	if (atomic_cmpxchg(&rep->rep_data[rep_type].state,
2530 			   REP_LOADED, REP_REGISTERED) == REP_LOADED) {
2531 		if (rep_type == REP_ETH)
2532 			__esw_offloads_unload_rep(esw, rep, REP_IB);
2533 		esw->offloads.rep_ops[rep_type]->unload(rep);
2534 	}
2535 }
2536 
__unload_reps_all_vport(struct mlx5_eswitch * esw,u8 rep_type)2537 static void __unload_reps_all_vport(struct mlx5_eswitch *esw, u8 rep_type)
2538 {
2539 	struct mlx5_eswitch_rep *rep;
2540 	unsigned long i;
2541 
2542 	mlx5_esw_for_each_rep(esw, i, rep)
2543 		__esw_offloads_unload_rep(esw, rep, rep_type);
2544 }
2545 
mlx5_esw_offloads_rep_load(struct mlx5_eswitch * esw,u16 vport_num)2546 static int mlx5_esw_offloads_rep_load(struct mlx5_eswitch *esw, u16 vport_num)
2547 {
2548 	struct mlx5_eswitch_rep *rep;
2549 	int rep_type;
2550 	int err;
2551 
2552 	rep = mlx5_eswitch_get_rep(esw, vport_num);
2553 	for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++) {
2554 		err = __esw_offloads_load_rep(esw, rep, rep_type);
2555 		if (err)
2556 			goto err_reps;
2557 	}
2558 
2559 	return 0;
2560 
2561 err_reps:
2562 	atomic_set(&rep->rep_data[rep_type].state, REP_REGISTERED);
2563 	for (--rep_type; rep_type >= 0; rep_type--)
2564 		__esw_offloads_unload_rep(esw, rep, rep_type);
2565 	return err;
2566 }
2567 
mlx5_esw_offloads_rep_unload(struct mlx5_eswitch * esw,u16 vport_num)2568 static void mlx5_esw_offloads_rep_unload(struct mlx5_eswitch *esw, u16 vport_num)
2569 {
2570 	struct mlx5_eswitch_rep *rep;
2571 	int rep_type;
2572 
2573 	rep = mlx5_eswitch_get_rep(esw, vport_num);
2574 	for (rep_type = NUM_REP_TYPES - 1; rep_type >= 0; rep_type--)
2575 		__esw_offloads_unload_rep(esw, rep, rep_type);
2576 }
2577 
mlx5_esw_offloads_init_pf_vf_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2578 int mlx5_esw_offloads_init_pf_vf_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2579 {
2580 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2581 		return 0;
2582 
2583 	return mlx5_esw_offloads_pf_vf_devlink_port_init(esw, vport);
2584 }
2585 
mlx5_esw_offloads_cleanup_pf_vf_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2586 void mlx5_esw_offloads_cleanup_pf_vf_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2587 {
2588 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2589 		return;
2590 
2591 	mlx5_esw_offloads_pf_vf_devlink_port_cleanup(esw, vport);
2592 }
2593 
mlx5_esw_offloads_init_sf_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport,struct mlx5_devlink_port * dl_port,u32 controller,u32 sfnum)2594 int mlx5_esw_offloads_init_sf_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport,
2595 				  struct mlx5_devlink_port *dl_port,
2596 				  u32 controller, u32 sfnum)
2597 {
2598 	return mlx5_esw_offloads_sf_devlink_port_init(esw, vport, dl_port, controller, sfnum);
2599 }
2600 
mlx5_esw_offloads_cleanup_sf_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2601 void mlx5_esw_offloads_cleanup_sf_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2602 {
2603 	mlx5_esw_offloads_sf_devlink_port_cleanup(esw, vport);
2604 }
2605 
mlx5_esw_offloads_load_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2606 int mlx5_esw_offloads_load_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2607 {
2608 	int err;
2609 
2610 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2611 		return 0;
2612 
2613 	err = mlx5_esw_offloads_devlink_port_register(esw, vport);
2614 	if (err)
2615 		return err;
2616 
2617 	err = mlx5_esw_offloads_rep_load(esw, vport->vport);
2618 	if (err)
2619 		goto load_err;
2620 	return err;
2621 
2622 load_err:
2623 	mlx5_esw_offloads_devlink_port_unregister(esw, vport);
2624 	return err;
2625 }
2626 
mlx5_esw_offloads_unload_rep(struct mlx5_eswitch * esw,struct mlx5_vport * vport)2627 void mlx5_esw_offloads_unload_rep(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
2628 {
2629 	if (esw->mode != MLX5_ESWITCH_OFFLOADS)
2630 		return;
2631 
2632 	mlx5_esw_offloads_rep_unload(esw, vport->vport);
2633 
2634 	mlx5_esw_offloads_devlink_port_unregister(esw, vport);
2635 }
2636 
esw_set_slave_root_fdb(struct mlx5_core_dev * master,struct mlx5_core_dev * slave)2637 static int esw_set_slave_root_fdb(struct mlx5_core_dev *master,
2638 				  struct mlx5_core_dev *slave)
2639 {
2640 	u32 in[MLX5_ST_SZ_DW(set_flow_table_root_in)]   = {};
2641 	u32 out[MLX5_ST_SZ_DW(set_flow_table_root_out)] = {};
2642 	struct mlx5_flow_root_namespace *root;
2643 	struct mlx5_flow_namespace *ns;
2644 	int err;
2645 
2646 	MLX5_SET(set_flow_table_root_in, in, opcode,
2647 		 MLX5_CMD_OP_SET_FLOW_TABLE_ROOT);
2648 	MLX5_SET(set_flow_table_root_in, in, table_type,
2649 		 FS_FT_FDB);
2650 
2651 	if (master) {
2652 		ns = mlx5_get_flow_namespace(master,
2653 					     MLX5_FLOW_NAMESPACE_FDB);
2654 		root = find_root(&ns->node);
2655 		mutex_lock(&root->chain_lock);
2656 		MLX5_SET(set_flow_table_root_in, in,
2657 			 table_eswitch_owner_vhca_id_valid, 1);
2658 		MLX5_SET(set_flow_table_root_in, in,
2659 			 table_eswitch_owner_vhca_id,
2660 			 MLX5_CAP_GEN(master, vhca_id));
2661 		MLX5_SET(set_flow_table_root_in, in, table_id,
2662 			 root->root_ft->id);
2663 	} else {
2664 		ns = mlx5_get_flow_namespace(slave,
2665 					     MLX5_FLOW_NAMESPACE_FDB);
2666 		root = find_root(&ns->node);
2667 		mutex_lock(&root->chain_lock);
2668 		MLX5_SET(set_flow_table_root_in, in, table_id,
2669 			 root->root_ft->id);
2670 	}
2671 
2672 	err = mlx5_cmd_exec(slave, in, sizeof(in), out, sizeof(out));
2673 	mutex_unlock(&root->chain_lock);
2674 
2675 	return err;
2676 }
2677 
__esw_set_master_egress_rule(struct mlx5_core_dev * master,struct mlx5_core_dev * slave,struct mlx5_vport * vport,struct mlx5_flow_table * acl)2678 static int __esw_set_master_egress_rule(struct mlx5_core_dev *master,
2679 					struct mlx5_core_dev *slave,
2680 					struct mlx5_vport *vport,
2681 					struct mlx5_flow_table *acl)
2682 {
2683 	u16 slave_index = MLX5_CAP_GEN(slave, vhca_id);
2684 	struct mlx5_flow_handle *flow_rule = NULL;
2685 	struct mlx5_flow_destination dest = {};
2686 	struct mlx5_flow_act flow_act = {};
2687 	struct mlx5_flow_spec *spec;
2688 	int err = 0;
2689 	void *misc;
2690 
2691 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2692 	if (!spec)
2693 		return -ENOMEM;
2694 
2695 	spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS;
2696 	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2697 			    misc_parameters);
2698 	MLX5_SET(fte_match_set_misc, misc, source_port, MLX5_VPORT_UPLINK);
2699 	MLX5_SET(fte_match_set_misc, misc, source_eswitch_owner_vhca_id, slave_index);
2700 
2701 	misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters);
2702 	MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
2703 	MLX5_SET_TO_ONES(fte_match_set_misc, misc,
2704 			 source_eswitch_owner_vhca_id);
2705 
2706 	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2707 	dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
2708 	dest.vport.num = slave->priv.eswitch->manager_vport;
2709 	dest.vport.vhca_id = MLX5_CAP_GEN(slave, vhca_id);
2710 	dest.vport.flags |= MLX5_FLOW_DEST_VPORT_VHCA_ID;
2711 
2712 	flow_rule = mlx5_add_flow_rules(acl, spec, &flow_act,
2713 					&dest, 1);
2714 	if (IS_ERR(flow_rule)) {
2715 		err = PTR_ERR(flow_rule);
2716 	} else {
2717 		err = xa_insert(&vport->egress.offloads.bounce_rules,
2718 				slave_index, flow_rule, GFP_KERNEL);
2719 		if (err)
2720 			mlx5_del_flow_rules(flow_rule);
2721 	}
2722 
2723 	kvfree(spec);
2724 	return err;
2725 }
2726 
esw_master_egress_create_resources(struct mlx5_eswitch * esw,struct mlx5_flow_namespace * egress_ns,struct mlx5_vport * vport,size_t count)2727 static int esw_master_egress_create_resources(struct mlx5_eswitch *esw,
2728 					      struct mlx5_flow_namespace *egress_ns,
2729 					      struct mlx5_vport *vport, size_t count)
2730 {
2731 	int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
2732 	struct mlx5_flow_table_attr ft_attr = {
2733 		.max_fte = count, .prio = 0, .level = 0,
2734 	};
2735 	struct mlx5_flow_table *acl;
2736 	struct mlx5_flow_group *g;
2737 	void *match_criteria;
2738 	u32 *flow_group_in;
2739 	int err;
2740 
2741 	if (vport->egress.acl)
2742 		return 0;
2743 
2744 	flow_group_in = kvzalloc(inlen, GFP_KERNEL);
2745 	if (!flow_group_in)
2746 		return -ENOMEM;
2747 
2748 	if (vport->vport || mlx5_core_is_ecpf(esw->dev))
2749 		ft_attr.flags = MLX5_FLOW_TABLE_OTHER_VPORT;
2750 
2751 	acl = mlx5_create_vport_flow_table(egress_ns, &ft_attr, vport->vport);
2752 	if (IS_ERR(acl)) {
2753 		err = PTR_ERR(acl);
2754 		goto out;
2755 	}
2756 
2757 	match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in,
2758 				      match_criteria);
2759 	MLX5_SET_TO_ONES(fte_match_param, match_criteria,
2760 			 misc_parameters.source_port);
2761 	MLX5_SET_TO_ONES(fte_match_param, match_criteria,
2762 			 misc_parameters.source_eswitch_owner_vhca_id);
2763 	MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
2764 		 MLX5_MATCH_MISC_PARAMETERS);
2765 
2766 	MLX5_SET(create_flow_group_in, flow_group_in,
2767 		 source_eswitch_owner_vhca_id_valid, 1);
2768 	MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
2769 	MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, count);
2770 
2771 	g = mlx5_create_flow_group(acl, flow_group_in);
2772 	if (IS_ERR(g)) {
2773 		err = PTR_ERR(g);
2774 		goto err_group;
2775 	}
2776 
2777 	vport->egress.acl = acl;
2778 	vport->egress.offloads.bounce_grp = g;
2779 	vport->egress.type = VPORT_EGRESS_ACL_TYPE_SHARED_FDB;
2780 	xa_init_flags(&vport->egress.offloads.bounce_rules, XA_FLAGS_ALLOC);
2781 
2782 	kvfree(flow_group_in);
2783 
2784 	return 0;
2785 
2786 err_group:
2787 	mlx5_destroy_flow_table(acl);
2788 out:
2789 	kvfree(flow_group_in);
2790 	return err;
2791 }
2792 
esw_master_egress_destroy_resources(struct mlx5_vport * vport)2793 static void esw_master_egress_destroy_resources(struct mlx5_vport *vport)
2794 {
2795 	if (!xa_empty(&vport->egress.offloads.bounce_rules))
2796 		return;
2797 	mlx5_destroy_flow_group(vport->egress.offloads.bounce_grp);
2798 	vport->egress.offloads.bounce_grp = NULL;
2799 	mlx5_destroy_flow_table(vport->egress.acl);
2800 	vport->egress.acl = NULL;
2801 }
2802 
esw_set_master_egress_rule(struct mlx5_core_dev * master,struct mlx5_core_dev * slave,size_t count)2803 static int esw_set_master_egress_rule(struct mlx5_core_dev *master,
2804 				      struct mlx5_core_dev *slave, size_t count)
2805 {
2806 	struct mlx5_eswitch *esw = master->priv.eswitch;
2807 	u16 slave_index = MLX5_CAP_GEN(slave, vhca_id);
2808 	struct mlx5_flow_namespace *egress_ns;
2809 	struct mlx5_vport *vport;
2810 	int err;
2811 
2812 	vport = mlx5_eswitch_get_vport(esw, esw->manager_vport);
2813 	if (IS_ERR(vport))
2814 		return PTR_ERR(vport);
2815 
2816 	egress_ns = mlx5_get_flow_vport_acl_namespace(master,
2817 						      MLX5_FLOW_NAMESPACE_ESW_EGRESS,
2818 						      vport->index);
2819 	if (!egress_ns)
2820 		return -EINVAL;
2821 
2822 	if (vport->egress.acl && vport->egress.type != VPORT_EGRESS_ACL_TYPE_SHARED_FDB)
2823 		return 0;
2824 
2825 	err = esw_master_egress_create_resources(esw, egress_ns, vport, count);
2826 	if (err)
2827 		return err;
2828 
2829 	if (xa_load(&vport->egress.offloads.bounce_rules, slave_index))
2830 		return -EINVAL;
2831 
2832 	err = __esw_set_master_egress_rule(master, slave, vport, vport->egress.acl);
2833 	if (err)
2834 		goto err_rule;
2835 
2836 	return 0;
2837 
2838 err_rule:
2839 	esw_master_egress_destroy_resources(vport);
2840 	return err;
2841 }
2842 
esw_unset_master_egress_rule(struct mlx5_core_dev * dev,struct mlx5_core_dev * slave_dev)2843 static void esw_unset_master_egress_rule(struct mlx5_core_dev *dev,
2844 					 struct mlx5_core_dev *slave_dev)
2845 {
2846 	struct mlx5_vport *vport;
2847 
2848 	vport = mlx5_eswitch_get_vport(dev->priv.eswitch,
2849 				       dev->priv.eswitch->manager_vport);
2850 
2851 	esw_acl_egress_ofld_bounce_rule_destroy(vport, MLX5_CAP_GEN(slave_dev, vhca_id));
2852 
2853 	if (xa_empty(&vport->egress.offloads.bounce_rules)) {
2854 		esw_acl_egress_ofld_cleanup(vport);
2855 		xa_destroy(&vport->egress.offloads.bounce_rules);
2856 	}
2857 }
2858 
mlx5_eswitch_offloads_single_fdb_add_one(struct mlx5_eswitch * master_esw,struct mlx5_eswitch * slave_esw,int max_slaves)2859 int mlx5_eswitch_offloads_single_fdb_add_one(struct mlx5_eswitch *master_esw,
2860 					     struct mlx5_eswitch *slave_esw, int max_slaves)
2861 {
2862 	int err;
2863 
2864 	err = esw_set_slave_root_fdb(master_esw->dev,
2865 				     slave_esw->dev);
2866 	if (err)
2867 		return err;
2868 
2869 	err = esw_set_master_egress_rule(master_esw->dev,
2870 					 slave_esw->dev, max_slaves);
2871 	if (err)
2872 		goto err_acl;
2873 
2874 	return err;
2875 
2876 err_acl:
2877 	esw_set_slave_root_fdb(NULL, slave_esw->dev);
2878 	return err;
2879 }
2880 
mlx5_eswitch_offloads_single_fdb_del_one(struct mlx5_eswitch * master_esw,struct mlx5_eswitch * slave_esw)2881 void mlx5_eswitch_offloads_single_fdb_del_one(struct mlx5_eswitch *master_esw,
2882 					      struct mlx5_eswitch *slave_esw)
2883 {
2884 	esw_set_slave_root_fdb(NULL, slave_esw->dev);
2885 	esw_unset_master_egress_rule(master_esw->dev, slave_esw->dev);
2886 }
2887 
2888 #define ESW_OFFLOADS_DEVCOM_PAIR	(0)
2889 #define ESW_OFFLOADS_DEVCOM_UNPAIR	(1)
2890 
mlx5_esw_offloads_rep_event_unpair(struct mlx5_eswitch * esw,struct mlx5_eswitch * peer_esw)2891 static void mlx5_esw_offloads_rep_event_unpair(struct mlx5_eswitch *esw,
2892 					       struct mlx5_eswitch *peer_esw)
2893 {
2894 	const struct mlx5_eswitch_rep_ops *ops;
2895 	struct mlx5_eswitch_rep *rep;
2896 	unsigned long i;
2897 	u8 rep_type;
2898 
2899 	mlx5_esw_for_each_rep(esw, i, rep) {
2900 		rep_type = NUM_REP_TYPES;
2901 		while (rep_type--) {
2902 			ops = esw->offloads.rep_ops[rep_type];
2903 			if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
2904 			    ops->event)
2905 				ops->event(esw, rep, MLX5_SWITCHDEV_EVENT_UNPAIR, peer_esw);
2906 		}
2907 	}
2908 }
2909 
mlx5_esw_offloads_unpair(struct mlx5_eswitch * esw,struct mlx5_eswitch * peer_esw)2910 static void mlx5_esw_offloads_unpair(struct mlx5_eswitch *esw,
2911 				     struct mlx5_eswitch *peer_esw)
2912 {
2913 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT)
2914 	mlx5e_tc_clean_fdb_peer_flows(esw);
2915 #endif
2916 	mlx5_esw_offloads_rep_event_unpair(esw, peer_esw);
2917 	esw_del_fdb_peer_miss_rules(esw, peer_esw->dev);
2918 }
2919 
mlx5_esw_offloads_pair(struct mlx5_eswitch * esw,struct mlx5_eswitch * peer_esw)2920 static int mlx5_esw_offloads_pair(struct mlx5_eswitch *esw,
2921 				  struct mlx5_eswitch *peer_esw)
2922 {
2923 	const struct mlx5_eswitch_rep_ops *ops;
2924 	struct mlx5_eswitch_rep *rep;
2925 	unsigned long i;
2926 	u8 rep_type;
2927 	int err;
2928 
2929 	err = esw_add_fdb_peer_miss_rules(esw, peer_esw->dev);
2930 	if (err)
2931 		return err;
2932 
2933 	mlx5_esw_for_each_rep(esw, i, rep) {
2934 		for (rep_type = 0; rep_type < NUM_REP_TYPES; rep_type++) {
2935 			ops = esw->offloads.rep_ops[rep_type];
2936 			if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
2937 			    ops->event) {
2938 				err = ops->event(esw, rep, MLX5_SWITCHDEV_EVENT_PAIR, peer_esw);
2939 				if (err)
2940 					goto err_out;
2941 			}
2942 		}
2943 	}
2944 
2945 	return 0;
2946 
2947 err_out:
2948 	mlx5_esw_offloads_unpair(esw, peer_esw);
2949 	return err;
2950 }
2951 
mlx5_esw_offloads_set_ns_peer(struct mlx5_eswitch * esw,struct mlx5_eswitch * peer_esw,bool pair)2952 static int mlx5_esw_offloads_set_ns_peer(struct mlx5_eswitch *esw,
2953 					 struct mlx5_eswitch *peer_esw,
2954 					 bool pair)
2955 {
2956 	u16 peer_vhca_id = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
2957 	u16 vhca_id = MLX5_CAP_GEN(esw->dev, vhca_id);
2958 	struct mlx5_flow_root_namespace *peer_ns;
2959 	struct mlx5_flow_root_namespace *ns;
2960 	int err;
2961 
2962 	peer_ns = peer_esw->dev->priv.steering->fdb_root_ns;
2963 	ns = esw->dev->priv.steering->fdb_root_ns;
2964 
2965 	if (pair) {
2966 		err = mlx5_flow_namespace_set_peer(ns, peer_ns, peer_vhca_id);
2967 		if (err)
2968 			return err;
2969 
2970 		err = mlx5_flow_namespace_set_peer(peer_ns, ns, vhca_id);
2971 		if (err) {
2972 			mlx5_flow_namespace_set_peer(ns, NULL, peer_vhca_id);
2973 			return err;
2974 		}
2975 	} else {
2976 		mlx5_flow_namespace_set_peer(ns, NULL, peer_vhca_id);
2977 		mlx5_flow_namespace_set_peer(peer_ns, NULL, vhca_id);
2978 	}
2979 
2980 	return 0;
2981 }
2982 
mlx5_esw_offloads_devcom_event(int event,void * my_data,void * event_data)2983 static int mlx5_esw_offloads_devcom_event(int event,
2984 					  void *my_data,
2985 					  void *event_data)
2986 {
2987 	struct mlx5_eswitch *esw = my_data;
2988 	struct mlx5_eswitch *peer_esw = event_data;
2989 	u16 esw_i, peer_esw_i;
2990 	bool esw_paired;
2991 	int err;
2992 
2993 	peer_esw_i = MLX5_CAP_GEN(peer_esw->dev, vhca_id);
2994 	esw_i = MLX5_CAP_GEN(esw->dev, vhca_id);
2995 	esw_paired = !!xa_load(&esw->paired, peer_esw_i);
2996 
2997 	switch (event) {
2998 	case ESW_OFFLOADS_DEVCOM_PAIR:
2999 		if (mlx5_eswitch_vport_match_metadata_enabled(esw) !=
3000 		    mlx5_eswitch_vport_match_metadata_enabled(peer_esw))
3001 			break;
3002 
3003 		if (esw_paired)
3004 			break;
3005 
3006 		err = mlx5_esw_offloads_set_ns_peer(esw, peer_esw, true);
3007 		if (err)
3008 			goto err_out;
3009 
3010 		err = mlx5_esw_offloads_pair(esw, peer_esw);
3011 		if (err)
3012 			goto err_peer;
3013 
3014 		err = mlx5_esw_offloads_pair(peer_esw, esw);
3015 		if (err)
3016 			goto err_pair;
3017 
3018 		err = xa_insert(&esw->paired, peer_esw_i, peer_esw, GFP_KERNEL);
3019 		if (err)
3020 			goto err_xa;
3021 
3022 		err = xa_insert(&peer_esw->paired, esw_i, esw, GFP_KERNEL);
3023 		if (err)
3024 			goto err_peer_xa;
3025 
3026 		esw->num_peers++;
3027 		peer_esw->num_peers++;
3028 		mlx5_devcom_comp_set_ready(esw->devcom, true);
3029 		break;
3030 
3031 	case ESW_OFFLOADS_DEVCOM_UNPAIR:
3032 		if (!esw_paired)
3033 			break;
3034 
3035 		peer_esw->num_peers--;
3036 		esw->num_peers--;
3037 		if (!esw->num_peers && !peer_esw->num_peers)
3038 			mlx5_devcom_comp_set_ready(esw->devcom, false);
3039 		xa_erase(&peer_esw->paired, esw_i);
3040 		xa_erase(&esw->paired, peer_esw_i);
3041 		mlx5_esw_offloads_unpair(peer_esw, esw);
3042 		mlx5_esw_offloads_unpair(esw, peer_esw);
3043 		mlx5_esw_offloads_set_ns_peer(esw, peer_esw, false);
3044 		break;
3045 	}
3046 
3047 	return 0;
3048 
3049 err_peer_xa:
3050 	xa_erase(&esw->paired, peer_esw_i);
3051 err_xa:
3052 	mlx5_esw_offloads_unpair(peer_esw, esw);
3053 err_pair:
3054 	mlx5_esw_offloads_unpair(esw, peer_esw);
3055 err_peer:
3056 	mlx5_esw_offloads_set_ns_peer(esw, peer_esw, false);
3057 err_out:
3058 	mlx5_core_err(esw->dev, "esw offloads devcom event failure, event %u err %d",
3059 		      event, err);
3060 	return err;
3061 }
3062 
mlx5_esw_offloads_devcom_init(struct mlx5_eswitch * esw,u64 key)3063 void mlx5_esw_offloads_devcom_init(struct mlx5_eswitch *esw, u64 key)
3064 {
3065 	int i;
3066 
3067 	for (i = 0; i < MLX5_MAX_PORTS; i++)
3068 		INIT_LIST_HEAD(&esw->offloads.peer_flows[i]);
3069 	mutex_init(&esw->offloads.peer_mutex);
3070 
3071 	if (!MLX5_CAP_ESW(esw->dev, merged_eswitch))
3072 		return;
3073 
3074 	if ((MLX5_VPORT_MANAGER(esw->dev) || mlx5_core_is_ecpf_esw_manager(esw->dev)) &&
3075 	    !mlx5_lag_is_supported(esw->dev))
3076 		return;
3077 
3078 	xa_init(&esw->paired);
3079 	esw->num_peers = 0;
3080 	esw->devcom = mlx5_devcom_register_component(esw->dev->priv.devc,
3081 						     MLX5_DEVCOM_ESW_OFFLOADS,
3082 						     key,
3083 						     mlx5_esw_offloads_devcom_event,
3084 						     esw);
3085 	if (IS_ERR(esw->devcom))
3086 		return;
3087 
3088 	mlx5_devcom_send_event(esw->devcom,
3089 			       ESW_OFFLOADS_DEVCOM_PAIR,
3090 			       ESW_OFFLOADS_DEVCOM_UNPAIR,
3091 			       esw);
3092 }
3093 
mlx5_esw_offloads_devcom_cleanup(struct mlx5_eswitch * esw)3094 void mlx5_esw_offloads_devcom_cleanup(struct mlx5_eswitch *esw)
3095 {
3096 	if (IS_ERR_OR_NULL(esw->devcom))
3097 		return;
3098 
3099 	mlx5_devcom_send_event(esw->devcom,
3100 			       ESW_OFFLOADS_DEVCOM_UNPAIR,
3101 			       ESW_OFFLOADS_DEVCOM_UNPAIR,
3102 			       esw);
3103 
3104 	mlx5_devcom_unregister_component(esw->devcom);
3105 	xa_destroy(&esw->paired);
3106 	esw->devcom = NULL;
3107 }
3108 
mlx5_esw_offloads_devcom_is_ready(struct mlx5_eswitch * esw)3109 bool mlx5_esw_offloads_devcom_is_ready(struct mlx5_eswitch *esw)
3110 {
3111 	return mlx5_devcom_comp_is_ready(esw->devcom);
3112 }
3113 
mlx5_esw_vport_match_metadata_supported(const struct mlx5_eswitch * esw)3114 bool mlx5_esw_vport_match_metadata_supported(const struct mlx5_eswitch *esw)
3115 {
3116 	if (!MLX5_CAP_ESW(esw->dev, esw_uplink_ingress_acl))
3117 		return false;
3118 
3119 	if (!(MLX5_CAP_ESW_FLOWTABLE(esw->dev, fdb_to_vport_reg_c_id) &
3120 	      MLX5_FDB_TO_VPORT_REG_C_0))
3121 		return false;
3122 
3123 	return true;
3124 }
3125 
3126 #define MLX5_ESW_METADATA_RSVD_UPLINK 1
3127 
3128 /* Share the same metadata for uplink's. This is fine because:
3129  * (a) In shared FDB mode (LAG) both uplink's are treated the
3130  *     same and tagged with the same metadata.
3131  * (b) In non shared FDB mode, packets from physical port0
3132  *     cannot hit eswitch of PF1 and vice versa.
3133  */
mlx5_esw_match_metadata_reserved(struct mlx5_eswitch * esw)3134 static u32 mlx5_esw_match_metadata_reserved(struct mlx5_eswitch *esw)
3135 {
3136 	return MLX5_ESW_METADATA_RSVD_UPLINK;
3137 }
3138 
mlx5_esw_match_metadata_alloc(struct mlx5_eswitch * esw)3139 u32 mlx5_esw_match_metadata_alloc(struct mlx5_eswitch *esw)
3140 {
3141 	u32 vport_end_ida = (1 << ESW_VPORT_BITS) - 1;
3142 	/* Reserve 0xf for internal port offload */
3143 	u32 max_pf_num = (1 << ESW_PFNUM_BITS) - 2;
3144 	u32 pf_num;
3145 	int id;
3146 
3147 	/* Only 4 bits of pf_num */
3148 	pf_num = mlx5_get_dev_index(esw->dev);
3149 	if (pf_num > max_pf_num)
3150 		return 0;
3151 
3152 	/* Metadata is 4 bits of PFNUM and 12 bits of unique id */
3153 	/* Use only non-zero vport_id (2-4095) for all PF's */
3154 	id = ida_alloc_range(&esw->offloads.vport_metadata_ida,
3155 			     MLX5_ESW_METADATA_RSVD_UPLINK + 1,
3156 			     vport_end_ida, GFP_KERNEL);
3157 	if (id < 0)
3158 		return 0;
3159 	id = (pf_num << ESW_VPORT_BITS) | id;
3160 	return id;
3161 }
3162 
mlx5_esw_match_metadata_free(struct mlx5_eswitch * esw,u32 metadata)3163 void mlx5_esw_match_metadata_free(struct mlx5_eswitch *esw, u32 metadata)
3164 {
3165 	u32 vport_bit_mask = (1 << ESW_VPORT_BITS) - 1;
3166 
3167 	/* Metadata contains only 12 bits of actual ida id */
3168 	ida_free(&esw->offloads.vport_metadata_ida, metadata & vport_bit_mask);
3169 }
3170 
esw_offloads_vport_metadata_setup(struct mlx5_eswitch * esw,struct mlx5_vport * vport)3171 static int esw_offloads_vport_metadata_setup(struct mlx5_eswitch *esw,
3172 					     struct mlx5_vport *vport)
3173 {
3174 	if (vport->vport == MLX5_VPORT_UPLINK)
3175 		vport->default_metadata = mlx5_esw_match_metadata_reserved(esw);
3176 	else
3177 		vport->default_metadata = mlx5_esw_match_metadata_alloc(esw);
3178 
3179 	vport->metadata = vport->default_metadata;
3180 	return vport->metadata ? 0 : -ENOSPC;
3181 }
3182 
esw_offloads_vport_metadata_cleanup(struct mlx5_eswitch * esw,struct mlx5_vport * vport)3183 static void esw_offloads_vport_metadata_cleanup(struct mlx5_eswitch *esw,
3184 						struct mlx5_vport *vport)
3185 {
3186 	if (!vport->default_metadata)
3187 		return;
3188 
3189 	if (vport->vport == MLX5_VPORT_UPLINK)
3190 		return;
3191 
3192 	WARN_ON(vport->metadata != vport->default_metadata);
3193 	mlx5_esw_match_metadata_free(esw, vport->default_metadata);
3194 }
3195 
esw_offloads_metadata_uninit(struct mlx5_eswitch * esw)3196 static void esw_offloads_metadata_uninit(struct mlx5_eswitch *esw)
3197 {
3198 	struct mlx5_vport *vport;
3199 	unsigned long i;
3200 
3201 	if (!mlx5_eswitch_vport_match_metadata_enabled(esw))
3202 		return;
3203 
3204 	mlx5_esw_for_each_vport(esw, i, vport)
3205 		esw_offloads_vport_metadata_cleanup(esw, vport);
3206 }
3207 
esw_offloads_metadata_init(struct mlx5_eswitch * esw)3208 static int esw_offloads_metadata_init(struct mlx5_eswitch *esw)
3209 {
3210 	struct mlx5_vport *vport;
3211 	unsigned long i;
3212 	int err;
3213 
3214 	if (!mlx5_eswitch_vport_match_metadata_enabled(esw))
3215 		return 0;
3216 
3217 	mlx5_esw_for_each_vport(esw, i, vport) {
3218 		err = esw_offloads_vport_metadata_setup(esw, vport);
3219 		if (err)
3220 			goto metadata_err;
3221 	}
3222 
3223 	return 0;
3224 
3225 metadata_err:
3226 	esw_offloads_metadata_uninit(esw);
3227 	return err;
3228 }
3229 
3230 int
esw_vport_create_offloads_acl_tables(struct mlx5_eswitch * esw,struct mlx5_vport * vport)3231 esw_vport_create_offloads_acl_tables(struct mlx5_eswitch *esw,
3232 				     struct mlx5_vport *vport)
3233 {
3234 	int err;
3235 
3236 	err = esw_acl_ingress_ofld_setup(esw, vport);
3237 	if (err)
3238 		return err;
3239 
3240 	err = esw_acl_egress_ofld_setup(esw, vport);
3241 	if (err)
3242 		goto egress_err;
3243 
3244 	return 0;
3245 
3246 egress_err:
3247 	esw_acl_ingress_ofld_cleanup(esw, vport);
3248 	return err;
3249 }
3250 
3251 void
esw_vport_destroy_offloads_acl_tables(struct mlx5_eswitch * esw,struct mlx5_vport * vport)3252 esw_vport_destroy_offloads_acl_tables(struct mlx5_eswitch *esw,
3253 				      struct mlx5_vport *vport)
3254 {
3255 	esw_acl_egress_ofld_cleanup(vport);
3256 	esw_acl_ingress_ofld_cleanup(esw, vport);
3257 }
3258 
esw_create_offloads_acl_tables(struct mlx5_eswitch * esw)3259 static int esw_create_offloads_acl_tables(struct mlx5_eswitch *esw)
3260 {
3261 	struct mlx5_vport *uplink, *manager;
3262 	int ret;
3263 
3264 	uplink = mlx5_eswitch_get_vport(esw, MLX5_VPORT_UPLINK);
3265 	if (IS_ERR(uplink))
3266 		return PTR_ERR(uplink);
3267 
3268 	ret = esw_vport_create_offloads_acl_tables(esw, uplink);
3269 	if (ret)
3270 		return ret;
3271 
3272 	manager = mlx5_eswitch_get_vport(esw, esw->manager_vport);
3273 	if (IS_ERR(manager)) {
3274 		ret = PTR_ERR(manager);
3275 		goto err_manager;
3276 	}
3277 
3278 	ret = esw_vport_create_offloads_acl_tables(esw, manager);
3279 	if (ret)
3280 		goto err_manager;
3281 
3282 	return 0;
3283 
3284 err_manager:
3285 	esw_vport_destroy_offloads_acl_tables(esw, uplink);
3286 	return ret;
3287 }
3288 
esw_destroy_offloads_acl_tables(struct mlx5_eswitch * esw)3289 static void esw_destroy_offloads_acl_tables(struct mlx5_eswitch *esw)
3290 {
3291 	struct mlx5_vport *vport;
3292 
3293 	vport = mlx5_eswitch_get_vport(esw, esw->manager_vport);
3294 	if (!IS_ERR(vport))
3295 		esw_vport_destroy_offloads_acl_tables(esw, vport);
3296 
3297 	vport = mlx5_eswitch_get_vport(esw, MLX5_VPORT_UPLINK);
3298 	if (!IS_ERR(vport))
3299 		esw_vport_destroy_offloads_acl_tables(esw, vport);
3300 }
3301 
mlx5_eswitch_reload_ib_reps(struct mlx5_eswitch * esw)3302 int mlx5_eswitch_reload_ib_reps(struct mlx5_eswitch *esw)
3303 {
3304 	struct mlx5_eswitch_rep *rep;
3305 	unsigned long i;
3306 	int ret;
3307 
3308 	if (!esw || esw->mode != MLX5_ESWITCH_OFFLOADS)
3309 		return 0;
3310 
3311 	rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_UPLINK);
3312 	if (atomic_read(&rep->rep_data[REP_ETH].state) != REP_LOADED)
3313 		return 0;
3314 
3315 	ret = __esw_offloads_load_rep(esw, rep, REP_IB);
3316 	if (ret)
3317 		return ret;
3318 
3319 	mlx5_esw_for_each_rep(esw, i, rep) {
3320 		if (atomic_read(&rep->rep_data[REP_ETH].state) == REP_LOADED)
3321 			__esw_offloads_load_rep(esw, rep, REP_IB);
3322 	}
3323 
3324 	return 0;
3325 }
3326 
esw_offloads_steering_init(struct mlx5_eswitch * esw)3327 static int esw_offloads_steering_init(struct mlx5_eswitch *esw)
3328 {
3329 	struct mlx5_esw_indir_table *indir;
3330 	int err;
3331 
3332 	memset(&esw->fdb_table.offloads, 0, sizeof(struct offloads_fdb));
3333 	mutex_init(&esw->fdb_table.offloads.vports.lock);
3334 	hash_init(esw->fdb_table.offloads.vports.table);
3335 	atomic64_set(&esw->user_count, 0);
3336 
3337 	indir = mlx5_esw_indir_table_init();
3338 	if (IS_ERR(indir)) {
3339 		err = PTR_ERR(indir);
3340 		goto create_indir_err;
3341 	}
3342 	esw->fdb_table.offloads.indir = indir;
3343 
3344 	err = esw_create_offloads_acl_tables(esw);
3345 	if (err)
3346 		goto create_acl_err;
3347 
3348 	err = esw_create_offloads_table(esw);
3349 	if (err)
3350 		goto create_offloads_err;
3351 
3352 	err = esw_create_restore_table(esw);
3353 	if (err)
3354 		goto create_restore_err;
3355 
3356 	err = esw_create_offloads_fdb_tables(esw);
3357 	if (err)
3358 		goto create_fdb_err;
3359 
3360 	err = esw_create_vport_rx_group(esw);
3361 	if (err)
3362 		goto create_fg_err;
3363 
3364 	err = esw_create_vport_rx_drop_group(esw);
3365 	if (err)
3366 		goto create_rx_drop_fg_err;
3367 
3368 	err = esw_create_vport_rx_drop_rule(esw);
3369 	if (err)
3370 		goto create_rx_drop_rule_err;
3371 
3372 	return 0;
3373 
3374 create_rx_drop_rule_err:
3375 	esw_destroy_vport_rx_drop_group(esw);
3376 create_rx_drop_fg_err:
3377 	esw_destroy_vport_rx_group(esw);
3378 create_fg_err:
3379 	esw_destroy_offloads_fdb_tables(esw);
3380 create_fdb_err:
3381 	esw_destroy_restore_table(esw);
3382 create_restore_err:
3383 	esw_destroy_offloads_table(esw);
3384 create_offloads_err:
3385 	esw_destroy_offloads_acl_tables(esw);
3386 create_acl_err:
3387 	mlx5_esw_indir_table_destroy(esw->fdb_table.offloads.indir);
3388 create_indir_err:
3389 	mutex_destroy(&esw->fdb_table.offloads.vports.lock);
3390 	return err;
3391 }
3392 
esw_offloads_steering_cleanup(struct mlx5_eswitch * esw)3393 static void esw_offloads_steering_cleanup(struct mlx5_eswitch *esw)
3394 {
3395 	esw_destroy_vport_rx_drop_rule(esw);
3396 	esw_destroy_vport_rx_drop_group(esw);
3397 	esw_destroy_vport_rx_group(esw);
3398 	esw_destroy_offloads_fdb_tables(esw);
3399 	esw_destroy_restore_table(esw);
3400 	esw_destroy_offloads_table(esw);
3401 	esw_destroy_offloads_acl_tables(esw);
3402 	mlx5_esw_indir_table_destroy(esw->fdb_table.offloads.indir);
3403 	mutex_destroy(&esw->fdb_table.offloads.vports.lock);
3404 }
3405 
3406 static void
esw_vfs_changed_event_handler(struct mlx5_eswitch * esw,const u32 * out)3407 esw_vfs_changed_event_handler(struct mlx5_eswitch *esw, const u32 *out)
3408 {
3409 	struct devlink *devlink;
3410 	bool host_pf_disabled;
3411 	u16 new_num_vfs;
3412 
3413 	new_num_vfs = MLX5_GET(query_esw_functions_out, out,
3414 			       host_params_context.host_num_of_vfs);
3415 	host_pf_disabled = MLX5_GET(query_esw_functions_out, out,
3416 				    host_params_context.host_pf_disabled);
3417 
3418 	if (new_num_vfs == esw->esw_funcs.num_vfs || host_pf_disabled)
3419 		return;
3420 
3421 	devlink = priv_to_devlink(esw->dev);
3422 	devl_lock(devlink);
3423 	/* Number of VFs can only change from "0 to x" or "x to 0". */
3424 	if (esw->esw_funcs.num_vfs > 0) {
3425 		mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
3426 	} else {
3427 		int err;
3428 
3429 		err = mlx5_eswitch_load_vf_vports(esw, new_num_vfs,
3430 						  MLX5_VPORT_UC_ADDR_CHANGE);
3431 		if (err) {
3432 			devl_unlock(devlink);
3433 			return;
3434 		}
3435 	}
3436 	esw->esw_funcs.num_vfs = new_num_vfs;
3437 	devl_unlock(devlink);
3438 }
3439 
esw_functions_changed_event_handler(struct work_struct * work)3440 static void esw_functions_changed_event_handler(struct work_struct *work)
3441 {
3442 	struct mlx5_host_work *host_work;
3443 	struct mlx5_eswitch *esw;
3444 	const u32 *out;
3445 
3446 	host_work = container_of(work, struct mlx5_host_work, work);
3447 	esw = host_work->esw;
3448 
3449 	out = mlx5_esw_query_functions(esw->dev);
3450 	if (IS_ERR(out))
3451 		goto out;
3452 
3453 	esw_vfs_changed_event_handler(esw, out);
3454 	kvfree(out);
3455 out:
3456 	kfree(host_work);
3457 }
3458 
mlx5_esw_funcs_changed_handler(struct notifier_block * nb,unsigned long type,void * data)3459 int mlx5_esw_funcs_changed_handler(struct notifier_block *nb, unsigned long type, void *data)
3460 {
3461 	struct mlx5_esw_functions *esw_funcs;
3462 	struct mlx5_host_work *host_work;
3463 	struct mlx5_eswitch *esw;
3464 
3465 	host_work = kzalloc(sizeof(*host_work), GFP_ATOMIC);
3466 	if (!host_work)
3467 		return NOTIFY_DONE;
3468 
3469 	esw_funcs = mlx5_nb_cof(nb, struct mlx5_esw_functions, nb);
3470 	esw = container_of(esw_funcs, struct mlx5_eswitch, esw_funcs);
3471 
3472 	host_work->esw = esw;
3473 
3474 	INIT_WORK(&host_work->work, esw_functions_changed_event_handler);
3475 	queue_work(esw->work_queue, &host_work->work);
3476 
3477 	return NOTIFY_OK;
3478 }
3479 
mlx5_esw_host_number_init(struct mlx5_eswitch * esw)3480 static int mlx5_esw_host_number_init(struct mlx5_eswitch *esw)
3481 {
3482 	const u32 *query_host_out;
3483 
3484 	if (!mlx5_core_is_ecpf_esw_manager(esw->dev))
3485 		return 0;
3486 
3487 	query_host_out = mlx5_esw_query_functions(esw->dev);
3488 	if (IS_ERR(query_host_out))
3489 		return PTR_ERR(query_host_out);
3490 
3491 	/* Mark non local controller with non zero controller number. */
3492 	esw->offloads.host_number = MLX5_GET(query_esw_functions_out, query_host_out,
3493 					     host_params_context.host_number);
3494 	kvfree(query_host_out);
3495 	return 0;
3496 }
3497 
mlx5_esw_offloads_controller_valid(const struct mlx5_eswitch * esw,u32 controller)3498 bool mlx5_esw_offloads_controller_valid(const struct mlx5_eswitch *esw, u32 controller)
3499 {
3500 	/* Local controller is always valid */
3501 	if (controller == 0)
3502 		return true;
3503 
3504 	if (!mlx5_core_is_ecpf_esw_manager(esw->dev))
3505 		return false;
3506 
3507 	/* External host number starts with zero in device */
3508 	return (controller == esw->offloads.host_number + 1);
3509 }
3510 
esw_offloads_enable(struct mlx5_eswitch * esw)3511 int esw_offloads_enable(struct mlx5_eswitch *esw)
3512 {
3513 	struct mapping_ctx *reg_c0_obj_pool;
3514 	struct mlx5_vport *vport;
3515 	unsigned long i;
3516 	u64 mapping_id;
3517 	int err;
3518 
3519 	mutex_init(&esw->offloads.termtbl_mutex);
3520 	mlx5_rdma_enable_roce(esw->dev);
3521 
3522 	err = mlx5_esw_host_number_init(esw);
3523 	if (err)
3524 		goto err_metadata;
3525 
3526 	err = esw_offloads_metadata_init(esw);
3527 	if (err)
3528 		goto err_metadata;
3529 
3530 	err = esw_set_passing_vport_metadata(esw, true);
3531 	if (err)
3532 		goto err_vport_metadata;
3533 
3534 	mapping_id = mlx5_query_nic_system_image_guid(esw->dev);
3535 
3536 	reg_c0_obj_pool = mapping_create_for_id(mapping_id, MAPPING_TYPE_CHAIN,
3537 						sizeof(struct mlx5_mapped_obj),
3538 						ESW_REG_C0_USER_DATA_METADATA_MASK,
3539 						true);
3540 
3541 	if (IS_ERR(reg_c0_obj_pool)) {
3542 		err = PTR_ERR(reg_c0_obj_pool);
3543 		goto err_pool;
3544 	}
3545 	esw->offloads.reg_c0_obj_pool = reg_c0_obj_pool;
3546 
3547 	err = esw_offloads_steering_init(esw);
3548 	if (err)
3549 		goto err_steering_init;
3550 
3551 	/* Representor will control the vport link state */
3552 	mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs)
3553 		vport->info.link_state = MLX5_VPORT_ADMIN_STATE_DOWN;
3554 	if (mlx5_core_ec_sriov_enabled(esw->dev))
3555 		mlx5_esw_for_each_ec_vf_vport(esw, i, vport, esw->esw_funcs.num_ec_vfs)
3556 			vport->info.link_state = MLX5_VPORT_ADMIN_STATE_DOWN;
3557 
3558 	/* Uplink vport rep must load first. */
3559 	err = mlx5_esw_offloads_rep_load(esw, MLX5_VPORT_UPLINK);
3560 	if (err)
3561 		goto err_uplink;
3562 
3563 	err = mlx5_eswitch_enable_pf_vf_vports(esw, MLX5_VPORT_UC_ADDR_CHANGE);
3564 	if (err)
3565 		goto err_vports;
3566 
3567 	return 0;
3568 
3569 err_vports:
3570 	mlx5_esw_offloads_rep_unload(esw, MLX5_VPORT_UPLINK);
3571 err_uplink:
3572 	esw_offloads_steering_cleanup(esw);
3573 err_steering_init:
3574 	mapping_destroy(reg_c0_obj_pool);
3575 err_pool:
3576 	esw_set_passing_vport_metadata(esw, false);
3577 err_vport_metadata:
3578 	esw_offloads_metadata_uninit(esw);
3579 err_metadata:
3580 	mlx5_rdma_disable_roce(esw->dev);
3581 	mutex_destroy(&esw->offloads.termtbl_mutex);
3582 	return err;
3583 }
3584 
esw_offloads_stop(struct mlx5_eswitch * esw,struct netlink_ext_ack * extack)3585 static int esw_offloads_stop(struct mlx5_eswitch *esw,
3586 			     struct netlink_ext_ack *extack)
3587 {
3588 	int err;
3589 
3590 	esw->mode = MLX5_ESWITCH_LEGACY;
3591 
3592 	/* If changing from switchdev to legacy mode without sriov enabled,
3593 	 * no need to create legacy fdb.
3594 	 */
3595 	if (!mlx5_core_is_pf(esw->dev) || !mlx5_sriov_is_enabled(esw->dev))
3596 		return 0;
3597 
3598 	err = mlx5_eswitch_enable_locked(esw, MLX5_ESWITCH_IGNORE_NUM_VFS);
3599 	if (err)
3600 		NL_SET_ERR_MSG_MOD(extack, "Failed setting eswitch to legacy");
3601 
3602 	return err;
3603 }
3604 
esw_offloads_disable(struct mlx5_eswitch * esw)3605 void esw_offloads_disable(struct mlx5_eswitch *esw)
3606 {
3607 	mlx5_eswitch_disable_pf_vf_vports(esw);
3608 	mlx5_esw_offloads_rep_unload(esw, MLX5_VPORT_UPLINK);
3609 	esw_set_passing_vport_metadata(esw, false);
3610 	esw_offloads_steering_cleanup(esw);
3611 	mapping_destroy(esw->offloads.reg_c0_obj_pool);
3612 	esw_offloads_metadata_uninit(esw);
3613 	mlx5_rdma_disable_roce(esw->dev);
3614 	mutex_destroy(&esw->offloads.termtbl_mutex);
3615 }
3616 
esw_mode_from_devlink(u16 mode,u16 * mlx5_mode)3617 static int esw_mode_from_devlink(u16 mode, u16 *mlx5_mode)
3618 {
3619 	switch (mode) {
3620 	case DEVLINK_ESWITCH_MODE_LEGACY:
3621 		*mlx5_mode = MLX5_ESWITCH_LEGACY;
3622 		break;
3623 	case DEVLINK_ESWITCH_MODE_SWITCHDEV:
3624 		*mlx5_mode = MLX5_ESWITCH_OFFLOADS;
3625 		break;
3626 	default:
3627 		return -EINVAL;
3628 	}
3629 
3630 	return 0;
3631 }
3632 
esw_mode_to_devlink(u16 mlx5_mode,u16 * mode)3633 static int esw_mode_to_devlink(u16 mlx5_mode, u16 *mode)
3634 {
3635 	switch (mlx5_mode) {
3636 	case MLX5_ESWITCH_LEGACY:
3637 		*mode = DEVLINK_ESWITCH_MODE_LEGACY;
3638 		break;
3639 	case MLX5_ESWITCH_OFFLOADS:
3640 		*mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
3641 		break;
3642 	default:
3643 		return -EINVAL;
3644 	}
3645 
3646 	return 0;
3647 }
3648 
esw_inline_mode_from_devlink(u8 mode,u8 * mlx5_mode)3649 static int esw_inline_mode_from_devlink(u8 mode, u8 *mlx5_mode)
3650 {
3651 	switch (mode) {
3652 	case DEVLINK_ESWITCH_INLINE_MODE_NONE:
3653 		*mlx5_mode = MLX5_INLINE_MODE_NONE;
3654 		break;
3655 	case DEVLINK_ESWITCH_INLINE_MODE_LINK:
3656 		*mlx5_mode = MLX5_INLINE_MODE_L2;
3657 		break;
3658 	case DEVLINK_ESWITCH_INLINE_MODE_NETWORK:
3659 		*mlx5_mode = MLX5_INLINE_MODE_IP;
3660 		break;
3661 	case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT:
3662 		*mlx5_mode = MLX5_INLINE_MODE_TCP_UDP;
3663 		break;
3664 	default:
3665 		return -EINVAL;
3666 	}
3667 
3668 	return 0;
3669 }
3670 
esw_inline_mode_to_devlink(u8 mlx5_mode,u8 * mode)3671 static int esw_inline_mode_to_devlink(u8 mlx5_mode, u8 *mode)
3672 {
3673 	switch (mlx5_mode) {
3674 	case MLX5_INLINE_MODE_NONE:
3675 		*mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
3676 		break;
3677 	case MLX5_INLINE_MODE_L2:
3678 		*mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
3679 		break;
3680 	case MLX5_INLINE_MODE_IP:
3681 		*mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
3682 		break;
3683 	case MLX5_INLINE_MODE_TCP_UDP:
3684 		*mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
3685 		break;
3686 	default:
3687 		return -EINVAL;
3688 	}
3689 
3690 	return 0;
3691 }
3692 
mlx5_eswitch_block_mode(struct mlx5_core_dev * dev)3693 int mlx5_eswitch_block_mode(struct mlx5_core_dev *dev)
3694 {
3695 	struct mlx5_eswitch *esw = dev->priv.eswitch;
3696 	int err;
3697 
3698 	if (!mlx5_esw_allowed(esw))
3699 		return 0;
3700 
3701 	/* Take TC into account */
3702 	err = mlx5_esw_try_lock(esw);
3703 	if (err < 0)
3704 		return err;
3705 
3706 	esw->offloads.num_block_mode++;
3707 	mlx5_esw_unlock(esw);
3708 	return 0;
3709 }
3710 
mlx5_eswitch_unblock_mode(struct mlx5_core_dev * dev)3711 void mlx5_eswitch_unblock_mode(struct mlx5_core_dev *dev)
3712 {
3713 	struct mlx5_eswitch *esw = dev->priv.eswitch;
3714 
3715 	if (!mlx5_esw_allowed(esw))
3716 		return;
3717 
3718 	down_write(&esw->mode_lock);
3719 	esw->offloads.num_block_mode--;
3720 	up_write(&esw->mode_lock);
3721 }
3722 
mlx5_devlink_eswitch_mode_set(struct devlink * devlink,u16 mode,struct netlink_ext_ack * extack)3723 int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode,
3724 				  struct netlink_ext_ack *extack)
3725 {
3726 	u16 cur_mlx5_mode, mlx5_mode = 0;
3727 	struct mlx5_eswitch *esw;
3728 	int err = 0;
3729 
3730 	esw = mlx5_devlink_eswitch_get(devlink);
3731 	if (IS_ERR(esw))
3732 		return PTR_ERR(esw);
3733 
3734 	if (esw_mode_from_devlink(mode, &mlx5_mode))
3735 		return -EINVAL;
3736 
3737 	if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV && mlx5_get_sd(esw->dev)) {
3738 		NL_SET_ERR_MSG_MOD(extack,
3739 				   "Can't change E-Switch mode to switchdev when multi-PF netdev (Socket Direct) is configured.");
3740 		return -EPERM;
3741 	}
3742 
3743 	mlx5_lag_disable_change(esw->dev);
3744 	err = mlx5_esw_try_lock(esw);
3745 	if (err < 0) {
3746 		NL_SET_ERR_MSG_MOD(extack, "Can't change mode, E-Switch is busy");
3747 		goto enable_lag;
3748 	}
3749 	cur_mlx5_mode = err;
3750 	err = 0;
3751 
3752 	if (cur_mlx5_mode == mlx5_mode)
3753 		goto unlock;
3754 
3755 	if (esw->offloads.num_block_mode) {
3756 		NL_SET_ERR_MSG_MOD(extack,
3757 				   "Can't change eswitch mode when IPsec SA and/or policies are configured");
3758 		err = -EOPNOTSUPP;
3759 		goto unlock;
3760 	}
3761 
3762 	esw->eswitch_operation_in_progress = true;
3763 	up_write(&esw->mode_lock);
3764 
3765 	mlx5_eswitch_disable_locked(esw);
3766 	if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV) {
3767 		if (mlx5_devlink_trap_get_num_active(esw->dev)) {
3768 			NL_SET_ERR_MSG_MOD(extack,
3769 					   "Can't change mode while devlink traps are active");
3770 			err = -EOPNOTSUPP;
3771 			goto skip;
3772 		}
3773 		err = esw_offloads_start(esw, extack);
3774 	} else if (mode == DEVLINK_ESWITCH_MODE_LEGACY) {
3775 		err = esw_offloads_stop(esw, extack);
3776 		mlx5_rescan_drivers(esw->dev);
3777 	} else {
3778 		err = -EINVAL;
3779 	}
3780 
3781 skip:
3782 	down_write(&esw->mode_lock);
3783 	esw->eswitch_operation_in_progress = false;
3784 unlock:
3785 	mlx5_esw_unlock(esw);
3786 enable_lag:
3787 	mlx5_lag_enable_change(esw->dev);
3788 	return err;
3789 }
3790 
mlx5_devlink_eswitch_mode_get(struct devlink * devlink,u16 * mode)3791 int mlx5_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
3792 {
3793 	struct mlx5_eswitch *esw;
3794 
3795 	esw = mlx5_devlink_eswitch_get(devlink);
3796 	if (IS_ERR(esw))
3797 		return PTR_ERR(esw);
3798 
3799 	return esw_mode_to_devlink(esw->mode, mode);
3800 }
3801 
mlx5_esw_vports_inline_set(struct mlx5_eswitch * esw,u8 mlx5_mode,struct netlink_ext_ack * extack)3802 static int mlx5_esw_vports_inline_set(struct mlx5_eswitch *esw, u8 mlx5_mode,
3803 				      struct netlink_ext_ack *extack)
3804 {
3805 	struct mlx5_core_dev *dev = esw->dev;
3806 	struct mlx5_vport *vport;
3807 	u16 err_vport_num = 0;
3808 	unsigned long i;
3809 	int err = 0;
3810 
3811 	mlx5_esw_for_each_host_func_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
3812 		err = mlx5_modify_nic_vport_min_inline(dev, vport->vport, mlx5_mode);
3813 		if (err) {
3814 			err_vport_num = vport->vport;
3815 			NL_SET_ERR_MSG_MOD(extack,
3816 					   "Failed to set min inline on vport");
3817 			goto revert_inline_mode;
3818 		}
3819 	}
3820 	if (mlx5_core_ec_sriov_enabled(esw->dev)) {
3821 		mlx5_esw_for_each_ec_vf_vport(esw, i, vport, esw->esw_funcs.num_ec_vfs) {
3822 			err = mlx5_modify_nic_vport_min_inline(dev, vport->vport, mlx5_mode);
3823 			if (err) {
3824 				err_vport_num = vport->vport;
3825 				NL_SET_ERR_MSG_MOD(extack,
3826 						   "Failed to set min inline on vport");
3827 				goto revert_ec_vf_inline_mode;
3828 			}
3829 		}
3830 	}
3831 	return 0;
3832 
3833 revert_ec_vf_inline_mode:
3834 	mlx5_esw_for_each_ec_vf_vport(esw, i, vport, esw->esw_funcs.num_ec_vfs) {
3835 		if (vport->vport == err_vport_num)
3836 			break;
3837 		mlx5_modify_nic_vport_min_inline(dev,
3838 						 vport->vport,
3839 						 esw->offloads.inline_mode);
3840 	}
3841 revert_inline_mode:
3842 	mlx5_esw_for_each_host_func_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
3843 		if (vport->vport == err_vport_num)
3844 			break;
3845 		mlx5_modify_nic_vport_min_inline(dev,
3846 						 vport->vport,
3847 						 esw->offloads.inline_mode);
3848 	}
3849 	return err;
3850 }
3851 
mlx5_devlink_eswitch_inline_mode_set(struct devlink * devlink,u8 mode,struct netlink_ext_ack * extack)3852 int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode,
3853 					 struct netlink_ext_ack *extack)
3854 {
3855 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3856 	struct mlx5_eswitch *esw;
3857 	u8 mlx5_mode;
3858 	int err;
3859 
3860 	esw = mlx5_devlink_eswitch_get(devlink);
3861 	if (IS_ERR(esw))
3862 		return PTR_ERR(esw);
3863 
3864 	down_write(&esw->mode_lock);
3865 
3866 	switch (MLX5_CAP_ETH(dev, wqe_inline_mode)) {
3867 	case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
3868 		if (mode == DEVLINK_ESWITCH_INLINE_MODE_NONE) {
3869 			err = 0;
3870 			goto out;
3871 		}
3872 
3873 		fallthrough;
3874 	case MLX5_CAP_INLINE_MODE_L2:
3875 		NL_SET_ERR_MSG_MOD(extack, "Inline mode can't be set");
3876 		err = -EOPNOTSUPP;
3877 		goto out;
3878 	case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
3879 		break;
3880 	}
3881 
3882 	if (atomic64_read(&esw->offloads.num_flows) > 0) {
3883 		NL_SET_ERR_MSG_MOD(extack,
3884 				   "Can't set inline mode when flows are configured");
3885 		err = -EOPNOTSUPP;
3886 		goto out;
3887 	}
3888 
3889 	err = esw_inline_mode_from_devlink(mode, &mlx5_mode);
3890 	if (err)
3891 		goto out;
3892 
3893 	esw->eswitch_operation_in_progress = true;
3894 	up_write(&esw->mode_lock);
3895 
3896 	err = mlx5_esw_vports_inline_set(esw, mlx5_mode, extack);
3897 	if (!err)
3898 		esw->offloads.inline_mode = mlx5_mode;
3899 
3900 	down_write(&esw->mode_lock);
3901 	esw->eswitch_operation_in_progress = false;
3902 	up_write(&esw->mode_lock);
3903 	return 0;
3904 
3905 out:
3906 	up_write(&esw->mode_lock);
3907 	return err;
3908 }
3909 
mlx5_devlink_eswitch_inline_mode_get(struct devlink * devlink,u8 * mode)3910 int mlx5_devlink_eswitch_inline_mode_get(struct devlink *devlink, u8 *mode)
3911 {
3912 	struct mlx5_eswitch *esw;
3913 
3914 	esw = mlx5_devlink_eswitch_get(devlink);
3915 	if (IS_ERR(esw))
3916 		return PTR_ERR(esw);
3917 
3918 	return esw_inline_mode_to_devlink(esw->offloads.inline_mode, mode);
3919 }
3920 
mlx5_eswitch_block_encap(struct mlx5_core_dev * dev)3921 bool mlx5_eswitch_block_encap(struct mlx5_core_dev *dev)
3922 {
3923 	struct mlx5_eswitch *esw = dev->priv.eswitch;
3924 
3925 	if (!mlx5_esw_allowed(esw))
3926 		return true;
3927 
3928 	down_write(&esw->mode_lock);
3929 	if (esw->mode != MLX5_ESWITCH_LEGACY &&
3930 	    esw->offloads.encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE) {
3931 		up_write(&esw->mode_lock);
3932 		return false;
3933 	}
3934 
3935 	esw->offloads.num_block_encap++;
3936 	up_write(&esw->mode_lock);
3937 	return true;
3938 }
3939 
mlx5_eswitch_unblock_encap(struct mlx5_core_dev * dev)3940 void mlx5_eswitch_unblock_encap(struct mlx5_core_dev *dev)
3941 {
3942 	struct mlx5_eswitch *esw = dev->priv.eswitch;
3943 
3944 	if (!mlx5_esw_allowed(esw))
3945 		return;
3946 
3947 	down_write(&esw->mode_lock);
3948 	esw->offloads.num_block_encap--;
3949 	up_write(&esw->mode_lock);
3950 }
3951 
mlx5_devlink_eswitch_encap_mode_set(struct devlink * devlink,enum devlink_eswitch_encap_mode encap,struct netlink_ext_ack * extack)3952 int mlx5_devlink_eswitch_encap_mode_set(struct devlink *devlink,
3953 					enum devlink_eswitch_encap_mode encap,
3954 					struct netlink_ext_ack *extack)
3955 {
3956 	struct mlx5_core_dev *dev = devlink_priv(devlink);
3957 	struct mlx5_eswitch *esw;
3958 	int err = 0;
3959 
3960 	esw = mlx5_devlink_eswitch_get(devlink);
3961 	if (IS_ERR(esw))
3962 		return PTR_ERR(esw);
3963 
3964 	down_write(&esw->mode_lock);
3965 
3966 	if (encap != DEVLINK_ESWITCH_ENCAP_MODE_NONE &&
3967 	    (!MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) ||
3968 	     !MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))) {
3969 		err = -EOPNOTSUPP;
3970 		goto unlock;
3971 	}
3972 
3973 	if (encap && encap != DEVLINK_ESWITCH_ENCAP_MODE_BASIC) {
3974 		err = -EOPNOTSUPP;
3975 		goto unlock;
3976 	}
3977 
3978 	if (esw->mode == MLX5_ESWITCH_LEGACY) {
3979 		esw->offloads.encap = encap;
3980 		goto unlock;
3981 	}
3982 
3983 	if (esw->offloads.encap == encap)
3984 		goto unlock;
3985 
3986 	if (atomic64_read(&esw->offloads.num_flows) > 0) {
3987 		NL_SET_ERR_MSG_MOD(extack,
3988 				   "Can't set encapsulation when flows are configured");
3989 		err = -EOPNOTSUPP;
3990 		goto unlock;
3991 	}
3992 
3993 	if (esw->offloads.num_block_encap) {
3994 		NL_SET_ERR_MSG_MOD(extack,
3995 				   "Can't set encapsulation when IPsec SA and/or policies are configured");
3996 		err = -EOPNOTSUPP;
3997 		goto unlock;
3998 	}
3999 
4000 	esw->eswitch_operation_in_progress = true;
4001 	up_write(&esw->mode_lock);
4002 
4003 	esw_destroy_offloads_fdb_tables(esw);
4004 
4005 	esw->offloads.encap = encap;
4006 
4007 	err = esw_create_offloads_fdb_tables(esw);
4008 
4009 	if (err) {
4010 		NL_SET_ERR_MSG_MOD(extack,
4011 				   "Failed re-creating fast FDB table");
4012 		esw->offloads.encap = !encap;
4013 		(void)esw_create_offloads_fdb_tables(esw);
4014 	}
4015 
4016 	down_write(&esw->mode_lock);
4017 	esw->eswitch_operation_in_progress = false;
4018 
4019 unlock:
4020 	up_write(&esw->mode_lock);
4021 	return err;
4022 }
4023 
mlx5_devlink_eswitch_encap_mode_get(struct devlink * devlink,enum devlink_eswitch_encap_mode * encap)4024 int mlx5_devlink_eswitch_encap_mode_get(struct devlink *devlink,
4025 					enum devlink_eswitch_encap_mode *encap)
4026 {
4027 	struct mlx5_eswitch *esw;
4028 
4029 	esw = mlx5_devlink_eswitch_get(devlink);
4030 	if (IS_ERR(esw))
4031 		return PTR_ERR(esw);
4032 
4033 	*encap = esw->offloads.encap;
4034 	return 0;
4035 }
4036 
4037 static bool
mlx5_eswitch_vport_has_rep(const struct mlx5_eswitch * esw,u16 vport_num)4038 mlx5_eswitch_vport_has_rep(const struct mlx5_eswitch *esw, u16 vport_num)
4039 {
4040 	/* Currently, only ECPF based device has representor for host PF. */
4041 	if (vport_num == MLX5_VPORT_PF &&
4042 	    !mlx5_core_is_ecpf_esw_manager(esw->dev))
4043 		return false;
4044 
4045 	if (vport_num == MLX5_VPORT_ECPF &&
4046 	    !mlx5_ecpf_vport_exists(esw->dev))
4047 		return false;
4048 
4049 	return true;
4050 }
4051 
mlx5_eswitch_register_vport_reps(struct mlx5_eswitch * esw,const struct mlx5_eswitch_rep_ops * ops,u8 rep_type)4052 void mlx5_eswitch_register_vport_reps(struct mlx5_eswitch *esw,
4053 				      const struct mlx5_eswitch_rep_ops *ops,
4054 				      u8 rep_type)
4055 {
4056 	struct mlx5_eswitch_rep_data *rep_data;
4057 	struct mlx5_eswitch_rep *rep;
4058 	unsigned long i;
4059 
4060 	esw->offloads.rep_ops[rep_type] = ops;
4061 	mlx5_esw_for_each_rep(esw, i, rep) {
4062 		if (likely(mlx5_eswitch_vport_has_rep(esw, rep->vport))) {
4063 			rep->esw = esw;
4064 			rep_data = &rep->rep_data[rep_type];
4065 			atomic_set(&rep_data->state, REP_REGISTERED);
4066 		}
4067 	}
4068 }
4069 EXPORT_SYMBOL(mlx5_eswitch_register_vport_reps);
4070 
mlx5_eswitch_unregister_vport_reps(struct mlx5_eswitch * esw,u8 rep_type)4071 void mlx5_eswitch_unregister_vport_reps(struct mlx5_eswitch *esw, u8 rep_type)
4072 {
4073 	struct mlx5_eswitch_rep *rep;
4074 	unsigned long i;
4075 
4076 	if (esw->mode == MLX5_ESWITCH_OFFLOADS)
4077 		__unload_reps_all_vport(esw, rep_type);
4078 
4079 	mlx5_esw_for_each_rep(esw, i, rep)
4080 		atomic_set(&rep->rep_data[rep_type].state, REP_UNREGISTERED);
4081 }
4082 EXPORT_SYMBOL(mlx5_eswitch_unregister_vport_reps);
4083 
mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch * esw,u8 rep_type)4084 void *mlx5_eswitch_get_uplink_priv(struct mlx5_eswitch *esw, u8 rep_type)
4085 {
4086 	struct mlx5_eswitch_rep *rep;
4087 
4088 	rep = mlx5_eswitch_get_rep(esw, MLX5_VPORT_UPLINK);
4089 	return rep->rep_data[rep_type].priv;
4090 }
4091 
mlx5_eswitch_get_proto_dev(struct mlx5_eswitch * esw,u16 vport,u8 rep_type)4092 void *mlx5_eswitch_get_proto_dev(struct mlx5_eswitch *esw,
4093 				 u16 vport,
4094 				 u8 rep_type)
4095 {
4096 	struct mlx5_eswitch_rep *rep;
4097 
4098 	rep = mlx5_eswitch_get_rep(esw, vport);
4099 
4100 	if (atomic_read(&rep->rep_data[rep_type].state) == REP_LOADED &&
4101 	    esw->offloads.rep_ops[rep_type]->get_proto_dev)
4102 		return esw->offloads.rep_ops[rep_type]->get_proto_dev(rep);
4103 	return NULL;
4104 }
4105 EXPORT_SYMBOL(mlx5_eswitch_get_proto_dev);
4106 
mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch * esw,u8 rep_type)4107 void *mlx5_eswitch_uplink_get_proto_dev(struct mlx5_eswitch *esw, u8 rep_type)
4108 {
4109 	return mlx5_eswitch_get_proto_dev(esw, MLX5_VPORT_UPLINK, rep_type);
4110 }
4111 EXPORT_SYMBOL(mlx5_eswitch_uplink_get_proto_dev);
4112 
mlx5_eswitch_vport_rep(struct mlx5_eswitch * esw,u16 vport)4113 struct mlx5_eswitch_rep *mlx5_eswitch_vport_rep(struct mlx5_eswitch *esw,
4114 						u16 vport)
4115 {
4116 	return mlx5_eswitch_get_rep(esw, vport);
4117 }
4118 EXPORT_SYMBOL(mlx5_eswitch_vport_rep);
4119 
mlx5_eswitch_reg_c1_loopback_enabled(const struct mlx5_eswitch * esw)4120 bool mlx5_eswitch_reg_c1_loopback_enabled(const struct mlx5_eswitch *esw)
4121 {
4122 	return !!(esw->flags & MLX5_ESWITCH_REG_C1_LOOPBACK_ENABLED);
4123 }
4124 EXPORT_SYMBOL(mlx5_eswitch_reg_c1_loopback_enabled);
4125 
mlx5_eswitch_vport_match_metadata_enabled(const struct mlx5_eswitch * esw)4126 bool mlx5_eswitch_vport_match_metadata_enabled(const struct mlx5_eswitch *esw)
4127 {
4128 	return !!(esw->flags & MLX5_ESWITCH_VPORT_MATCH_METADATA);
4129 }
4130 EXPORT_SYMBOL(mlx5_eswitch_vport_match_metadata_enabled);
4131 
mlx5_eswitch_get_vport_metadata_for_match(struct mlx5_eswitch * esw,u16 vport_num)4132 u32 mlx5_eswitch_get_vport_metadata_for_match(struct mlx5_eswitch *esw,
4133 					      u16 vport_num)
4134 {
4135 	struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
4136 
4137 	if (WARN_ON_ONCE(IS_ERR(vport)))
4138 		return 0;
4139 
4140 	return vport->metadata << (32 - ESW_SOURCE_PORT_METADATA_BITS);
4141 }
4142 EXPORT_SYMBOL(mlx5_eswitch_get_vport_metadata_for_match);
4143 
mlx5_esw_query_vport_vhca_id(struct mlx5_eswitch * esw,u16 vport_num,u16 * vhca_id)4144 static int mlx5_esw_query_vport_vhca_id(struct mlx5_eswitch *esw, u16 vport_num, u16 *vhca_id)
4145 {
4146 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4147 	void *query_ctx;
4148 	void *hca_caps;
4149 	int err;
4150 
4151 	*vhca_id = 0;
4152 
4153 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4154 	if (!query_ctx)
4155 		return -ENOMEM;
4156 
4157 	err = mlx5_vport_get_other_func_general_cap(esw->dev, vport_num, query_ctx);
4158 	if (err)
4159 		goto out_free;
4160 
4161 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4162 	*vhca_id = MLX5_GET(cmd_hca_cap, hca_caps, vhca_id);
4163 
4164 out_free:
4165 	kfree(query_ctx);
4166 	return err;
4167 }
4168 
mlx5_esw_vport_vhca_id_set(struct mlx5_eswitch * esw,u16 vport_num)4169 int mlx5_esw_vport_vhca_id_set(struct mlx5_eswitch *esw, u16 vport_num)
4170 {
4171 	u16 *old_entry, *vhca_map_entry, vhca_id;
4172 	int err;
4173 
4174 	err = mlx5_esw_query_vport_vhca_id(esw, vport_num, &vhca_id);
4175 	if (err) {
4176 		esw_warn(esw->dev, "Getting vhca_id for vport failed (vport=%u,err=%d)\n",
4177 			 vport_num, err);
4178 		return err;
4179 	}
4180 
4181 	vhca_map_entry = kmalloc(sizeof(*vhca_map_entry), GFP_KERNEL);
4182 	if (!vhca_map_entry)
4183 		return -ENOMEM;
4184 
4185 	*vhca_map_entry = vport_num;
4186 	old_entry = xa_store(&esw->offloads.vhca_map, vhca_id, vhca_map_entry, GFP_KERNEL);
4187 	if (xa_is_err(old_entry)) {
4188 		kfree(vhca_map_entry);
4189 		return xa_err(old_entry);
4190 	}
4191 	kfree(old_entry);
4192 	return 0;
4193 }
4194 
mlx5_esw_vport_vhca_id_clear(struct mlx5_eswitch * esw,u16 vport_num)4195 void mlx5_esw_vport_vhca_id_clear(struct mlx5_eswitch *esw, u16 vport_num)
4196 {
4197 	u16 *vhca_map_entry, vhca_id;
4198 	int err;
4199 
4200 	err = mlx5_esw_query_vport_vhca_id(esw, vport_num, &vhca_id);
4201 	if (err)
4202 		esw_warn(esw->dev, "Getting vhca_id for vport failed (vport=%hu,err=%d)\n",
4203 			 vport_num, err);
4204 
4205 	vhca_map_entry = xa_erase(&esw->offloads.vhca_map, vhca_id);
4206 	kfree(vhca_map_entry);
4207 }
4208 
mlx5_eswitch_vhca_id_to_vport(struct mlx5_eswitch * esw,u16 vhca_id,u16 * vport_num)4209 int mlx5_eswitch_vhca_id_to_vport(struct mlx5_eswitch *esw, u16 vhca_id, u16 *vport_num)
4210 {
4211 	u16 *res = xa_load(&esw->offloads.vhca_map, vhca_id);
4212 
4213 	if (!res)
4214 		return -ENOENT;
4215 
4216 	*vport_num = *res;
4217 	return 0;
4218 }
4219 
mlx5_eswitch_get_vport_metadata_for_set(struct mlx5_eswitch * esw,u16 vport_num)4220 u32 mlx5_eswitch_get_vport_metadata_for_set(struct mlx5_eswitch *esw,
4221 					    u16 vport_num)
4222 {
4223 	struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
4224 
4225 	if (WARN_ON_ONCE(IS_ERR(vport)))
4226 		return 0;
4227 
4228 	return vport->metadata;
4229 }
4230 EXPORT_SYMBOL(mlx5_eswitch_get_vport_metadata_for_set);
4231 
mlx5_devlink_port_fn_hw_addr_get(struct devlink_port * port,u8 * hw_addr,int * hw_addr_len,struct netlink_ext_ack * extack)4232 int mlx5_devlink_port_fn_hw_addr_get(struct devlink_port *port,
4233 				     u8 *hw_addr, int *hw_addr_len,
4234 				     struct netlink_ext_ack *extack)
4235 {
4236 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4237 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4238 
4239 	mutex_lock(&esw->state_lock);
4240 	ether_addr_copy(hw_addr, vport->info.mac);
4241 	*hw_addr_len = ETH_ALEN;
4242 	mutex_unlock(&esw->state_lock);
4243 	return 0;
4244 }
4245 
mlx5_devlink_port_fn_hw_addr_set(struct devlink_port * port,const u8 * hw_addr,int hw_addr_len,struct netlink_ext_ack * extack)4246 int mlx5_devlink_port_fn_hw_addr_set(struct devlink_port *port,
4247 				     const u8 *hw_addr, int hw_addr_len,
4248 				     struct netlink_ext_ack *extack)
4249 {
4250 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4251 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4252 
4253 	return mlx5_eswitch_set_vport_mac(esw, vport->vport, hw_addr);
4254 }
4255 
mlx5_devlink_port_fn_migratable_get(struct devlink_port * port,bool * is_enabled,struct netlink_ext_ack * extack)4256 int mlx5_devlink_port_fn_migratable_get(struct devlink_port *port, bool *is_enabled,
4257 					struct netlink_ext_ack *extack)
4258 {
4259 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4260 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4261 
4262 	if (!MLX5_CAP_GEN(esw->dev, migration)) {
4263 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support migration");
4264 		return -EOPNOTSUPP;
4265 	}
4266 
4267 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4268 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support VHCA management");
4269 		return -EOPNOTSUPP;
4270 	}
4271 
4272 	mutex_lock(&esw->state_lock);
4273 	*is_enabled = vport->info.mig_enabled;
4274 	mutex_unlock(&esw->state_lock);
4275 	return 0;
4276 }
4277 
mlx5_devlink_port_fn_migratable_set(struct devlink_port * port,bool enable,struct netlink_ext_ack * extack)4278 int mlx5_devlink_port_fn_migratable_set(struct devlink_port *port, bool enable,
4279 					struct netlink_ext_ack *extack)
4280 {
4281 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4282 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4283 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4284 	void *query_ctx;
4285 	void *hca_caps;
4286 	int err;
4287 
4288 	if (!MLX5_CAP_GEN(esw->dev, migration)) {
4289 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support migration");
4290 		return -EOPNOTSUPP;
4291 	}
4292 
4293 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4294 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support VHCA management");
4295 		return -EOPNOTSUPP;
4296 	}
4297 
4298 	mutex_lock(&esw->state_lock);
4299 
4300 	if (vport->info.mig_enabled == enable) {
4301 		err = 0;
4302 		goto out;
4303 	}
4304 
4305 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4306 	if (!query_ctx) {
4307 		err = -ENOMEM;
4308 		goto out;
4309 	}
4310 
4311 	err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
4312 					    MLX5_CAP_GENERAL_2);
4313 	if (err) {
4314 		NL_SET_ERR_MSG_MOD(extack, "Failed getting HCA caps");
4315 		goto out_free;
4316 	}
4317 
4318 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4319 	MLX5_SET(cmd_hca_cap_2, hca_caps, migratable, enable);
4320 
4321 	err = mlx5_vport_set_other_func_cap(esw->dev, hca_caps, vport->vport,
4322 					    MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE2);
4323 	if (err) {
4324 		NL_SET_ERR_MSG_MOD(extack, "Failed setting HCA migratable cap");
4325 		goto out_free;
4326 	}
4327 
4328 	vport->info.mig_enabled = enable;
4329 
4330 out_free:
4331 	kfree(query_ctx);
4332 out:
4333 	mutex_unlock(&esw->state_lock);
4334 	return err;
4335 }
4336 
mlx5_devlink_port_fn_roce_get(struct devlink_port * port,bool * is_enabled,struct netlink_ext_ack * extack)4337 int mlx5_devlink_port_fn_roce_get(struct devlink_port *port, bool *is_enabled,
4338 				  struct netlink_ext_ack *extack)
4339 {
4340 	struct mlx5_eswitch *esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4341 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4342 
4343 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4344 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support VHCA management");
4345 		return -EOPNOTSUPP;
4346 	}
4347 
4348 	mutex_lock(&esw->state_lock);
4349 	*is_enabled = vport->info.roce_enabled;
4350 	mutex_unlock(&esw->state_lock);
4351 	return 0;
4352 }
4353 
mlx5_devlink_port_fn_roce_set(struct devlink_port * port,bool enable,struct netlink_ext_ack * extack)4354 int mlx5_devlink_port_fn_roce_set(struct devlink_port *port, bool enable,
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 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4360 	u16 vport_num = vport->vport;
4361 	void *query_ctx;
4362 	void *hca_caps;
4363 	int err;
4364 
4365 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4366 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support VHCA management");
4367 		return -EOPNOTSUPP;
4368 	}
4369 
4370 	mutex_lock(&esw->state_lock);
4371 
4372 	if (vport->info.roce_enabled == enable) {
4373 		err = 0;
4374 		goto out;
4375 	}
4376 
4377 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4378 	if (!query_ctx) {
4379 		err = -ENOMEM;
4380 		goto out;
4381 	}
4382 
4383 	err = mlx5_vport_get_other_func_cap(esw->dev, vport_num, query_ctx,
4384 					    MLX5_CAP_GENERAL);
4385 	if (err) {
4386 		NL_SET_ERR_MSG_MOD(extack, "Failed getting HCA caps");
4387 		goto out_free;
4388 	}
4389 
4390 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4391 	MLX5_SET(cmd_hca_cap, hca_caps, roce, enable);
4392 
4393 	err = mlx5_vport_set_other_func_cap(esw->dev, hca_caps, vport_num,
4394 					    MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE);
4395 	if (err) {
4396 		NL_SET_ERR_MSG_MOD(extack, "Failed setting HCA roce cap");
4397 		goto out_free;
4398 	}
4399 
4400 	vport->info.roce_enabled = enable;
4401 
4402 out_free:
4403 	kfree(query_ctx);
4404 out:
4405 	mutex_unlock(&esw->state_lock);
4406 	return err;
4407 }
4408 
4409 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)4410 mlx5_eswitch_restore_ipsec_rule(struct mlx5_eswitch *esw, struct mlx5_flow_handle *rule,
4411 				struct mlx5_esw_flow_attr *esw_attr, int attr_idx)
4412 {
4413 	struct mlx5_flow_destination new_dest = {};
4414 	struct mlx5_flow_destination old_dest = {};
4415 
4416 	if (!esw_setup_uplink_fwd_ipsec_needed(esw, esw_attr, attr_idx))
4417 		return 0;
4418 
4419 	esw_setup_dest_fwd_ipsec(&old_dest, NULL, esw, esw_attr, attr_idx, 0, false);
4420 	esw_setup_dest_fwd_vport(&new_dest, NULL, esw, esw_attr, attr_idx, 0, false);
4421 
4422 	return mlx5_modify_rule_destination(rule, &new_dest, &old_dest);
4423 }
4424 
4425 #ifdef CONFIG_XFRM_OFFLOAD
mlx5_devlink_port_fn_ipsec_crypto_get(struct devlink_port * port,bool * is_enabled,struct netlink_ext_ack * extack)4426 int mlx5_devlink_port_fn_ipsec_crypto_get(struct devlink_port *port, bool *is_enabled,
4427 					  struct netlink_ext_ack *extack)
4428 {
4429 	struct mlx5_eswitch *esw;
4430 	struct mlx5_vport *vport;
4431 	int err = 0;
4432 
4433 	esw = mlx5_devlink_eswitch_get(port->devlink);
4434 	if (IS_ERR(esw))
4435 		return PTR_ERR(esw);
4436 
4437 	if (!mlx5_esw_ipsec_vf_offload_supported(esw->dev)) {
4438 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support IPSec crypto");
4439 		return -EOPNOTSUPP;
4440 	}
4441 
4442 	vport = mlx5_devlink_port_vport_get(port);
4443 
4444 	mutex_lock(&esw->state_lock);
4445 	if (!vport->enabled) {
4446 		err = -EOPNOTSUPP;
4447 		goto unlock;
4448 	}
4449 
4450 	*is_enabled = vport->info.ipsec_crypto_enabled;
4451 unlock:
4452 	mutex_unlock(&esw->state_lock);
4453 	return err;
4454 }
4455 
mlx5_devlink_port_fn_ipsec_crypto_set(struct devlink_port * port,bool enable,struct netlink_ext_ack * extack)4456 int mlx5_devlink_port_fn_ipsec_crypto_set(struct devlink_port *port, bool enable,
4457 					  struct netlink_ext_ack *extack)
4458 {
4459 	struct mlx5_eswitch *esw;
4460 	struct mlx5_vport *vport;
4461 	u16 vport_num;
4462 	int err;
4463 
4464 	esw = mlx5_devlink_eswitch_get(port->devlink);
4465 	if (IS_ERR(esw))
4466 		return PTR_ERR(esw);
4467 
4468 	vport_num = mlx5_esw_devlink_port_index_to_vport_num(port->index);
4469 	err = mlx5_esw_ipsec_vf_crypto_offload_supported(esw->dev, vport_num);
4470 	if (err) {
4471 		NL_SET_ERR_MSG_MOD(extack,
4472 				   "Device doesn't support IPsec crypto");
4473 		return err;
4474 	}
4475 
4476 	vport = mlx5_devlink_port_vport_get(port);
4477 
4478 	mutex_lock(&esw->state_lock);
4479 	if (!vport->enabled) {
4480 		err = -EOPNOTSUPP;
4481 		NL_SET_ERR_MSG_MOD(extack, "Eswitch vport is disabled");
4482 		goto unlock;
4483 	}
4484 
4485 	if (vport->info.ipsec_crypto_enabled == enable)
4486 		goto unlock;
4487 
4488 	if (!esw->enabled_ipsec_vf_count && esw->dev->num_ipsec_offloads) {
4489 		err = -EBUSY;
4490 		goto unlock;
4491 	}
4492 
4493 	err = mlx5_esw_ipsec_vf_crypto_offload_set(esw, vport, enable);
4494 	if (err) {
4495 		NL_SET_ERR_MSG_MOD(extack, "Failed to set IPsec crypto");
4496 		goto unlock;
4497 	}
4498 
4499 	vport->info.ipsec_crypto_enabled = enable;
4500 	if (enable)
4501 		esw->enabled_ipsec_vf_count++;
4502 	else
4503 		esw->enabled_ipsec_vf_count--;
4504 unlock:
4505 	mutex_unlock(&esw->state_lock);
4506 	return err;
4507 }
4508 
mlx5_devlink_port_fn_ipsec_packet_get(struct devlink_port * port,bool * is_enabled,struct netlink_ext_ack * extack)4509 int mlx5_devlink_port_fn_ipsec_packet_get(struct devlink_port *port, bool *is_enabled,
4510 					  struct netlink_ext_ack *extack)
4511 {
4512 	struct mlx5_eswitch *esw;
4513 	struct mlx5_vport *vport;
4514 	int err = 0;
4515 
4516 	esw = mlx5_devlink_eswitch_get(port->devlink);
4517 	if (IS_ERR(esw))
4518 		return PTR_ERR(esw);
4519 
4520 	if (!mlx5_esw_ipsec_vf_offload_supported(esw->dev)) {
4521 		NL_SET_ERR_MSG_MOD(extack, "Device doesn't support IPsec packet");
4522 		return -EOPNOTSUPP;
4523 	}
4524 
4525 	vport = mlx5_devlink_port_vport_get(port);
4526 
4527 	mutex_lock(&esw->state_lock);
4528 	if (!vport->enabled) {
4529 		err = -EOPNOTSUPP;
4530 		goto unlock;
4531 	}
4532 
4533 	*is_enabled = vport->info.ipsec_packet_enabled;
4534 unlock:
4535 	mutex_unlock(&esw->state_lock);
4536 	return err;
4537 }
4538 
mlx5_devlink_port_fn_ipsec_packet_set(struct devlink_port * port,bool enable,struct netlink_ext_ack * extack)4539 int mlx5_devlink_port_fn_ipsec_packet_set(struct devlink_port *port,
4540 					  bool enable,
4541 					  struct netlink_ext_ack *extack)
4542 {
4543 	struct mlx5_eswitch *esw;
4544 	struct mlx5_vport *vport;
4545 	u16 vport_num;
4546 	int err;
4547 
4548 	esw = mlx5_devlink_eswitch_get(port->devlink);
4549 	if (IS_ERR(esw))
4550 		return PTR_ERR(esw);
4551 
4552 	vport_num = mlx5_esw_devlink_port_index_to_vport_num(port->index);
4553 	err = mlx5_esw_ipsec_vf_packet_offload_supported(esw->dev, vport_num);
4554 	if (err) {
4555 		NL_SET_ERR_MSG_MOD(extack,
4556 				   "Device doesn't support IPsec packet mode");
4557 		return err;
4558 	}
4559 
4560 	vport = mlx5_devlink_port_vport_get(port);
4561 	mutex_lock(&esw->state_lock);
4562 	if (!vport->enabled) {
4563 		err = -EOPNOTSUPP;
4564 		NL_SET_ERR_MSG_MOD(extack, "Eswitch vport is disabled");
4565 		goto unlock;
4566 	}
4567 
4568 	if (vport->info.ipsec_packet_enabled == enable)
4569 		goto unlock;
4570 
4571 	if (!esw->enabled_ipsec_vf_count && esw->dev->num_ipsec_offloads) {
4572 		err = -EBUSY;
4573 		goto unlock;
4574 	}
4575 
4576 	err = mlx5_esw_ipsec_vf_packet_offload_set(esw, vport, enable);
4577 	if (err) {
4578 		NL_SET_ERR_MSG_MOD(extack,
4579 				   "Failed to set IPsec packet mode");
4580 		goto unlock;
4581 	}
4582 
4583 	vport->info.ipsec_packet_enabled = enable;
4584 	if (enable)
4585 		esw->enabled_ipsec_vf_count++;
4586 	else
4587 		esw->enabled_ipsec_vf_count--;
4588 unlock:
4589 	mutex_unlock(&esw->state_lock);
4590 	return err;
4591 }
4592 #endif /* CONFIG_XFRM_OFFLOAD */
4593 
4594 int
mlx5_devlink_port_fn_max_io_eqs_get(struct devlink_port * port,u32 * max_io_eqs,struct netlink_ext_ack * extack)4595 mlx5_devlink_port_fn_max_io_eqs_get(struct devlink_port *port, u32 *max_io_eqs,
4596 				    struct netlink_ext_ack *extack)
4597 {
4598 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4599 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4600 	u16 vport_num = vport->vport;
4601 	struct mlx5_eswitch *esw;
4602 	void *query_ctx;
4603 	void *hca_caps;
4604 	u32 max_eqs;
4605 	int err;
4606 
4607 	esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4608 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4609 		NL_SET_ERR_MSG_MOD(extack,
4610 				   "Device doesn't support VHCA management");
4611 		return -EOPNOTSUPP;
4612 	}
4613 
4614 	if (!MLX5_CAP_GEN_2(esw->dev, max_num_eqs_24b)) {
4615 		NL_SET_ERR_MSG_MOD(extack,
4616 				   "Device doesn't support getting the max number of EQs");
4617 		return -EOPNOTSUPP;
4618 	}
4619 
4620 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4621 	if (!query_ctx)
4622 		return -ENOMEM;
4623 
4624 	mutex_lock(&esw->state_lock);
4625 	err = mlx5_vport_get_other_func_cap(esw->dev, vport_num, query_ctx,
4626 					    MLX5_CAP_GENERAL_2);
4627 	if (err) {
4628 		NL_SET_ERR_MSG_MOD(extack, "Failed getting HCA caps");
4629 		goto out;
4630 	}
4631 
4632 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4633 	max_eqs = MLX5_GET(cmd_hca_cap_2, hca_caps, max_num_eqs_24b);
4634 	if (max_eqs < MLX5_ESW_MAX_CTRL_EQS)
4635 		*max_io_eqs = 0;
4636 	else
4637 		*max_io_eqs = max_eqs - MLX5_ESW_MAX_CTRL_EQS;
4638 out:
4639 	mutex_unlock(&esw->state_lock);
4640 	kfree(query_ctx);
4641 	return err;
4642 }
4643 
4644 int
mlx5_devlink_port_fn_max_io_eqs_set(struct devlink_port * port,u32 max_io_eqs,struct netlink_ext_ack * extack)4645 mlx5_devlink_port_fn_max_io_eqs_set(struct devlink_port *port, u32 max_io_eqs,
4646 				    struct netlink_ext_ack *extack)
4647 {
4648 	struct mlx5_vport *vport = mlx5_devlink_port_vport_get(port);
4649 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
4650 	u16 vport_num = vport->vport;
4651 	struct mlx5_eswitch *esw;
4652 	void *query_ctx;
4653 	void *hca_caps;
4654 	u16 max_eqs;
4655 	int err;
4656 
4657 	esw = mlx5_devlink_eswitch_nocheck_get(port->devlink);
4658 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
4659 		NL_SET_ERR_MSG_MOD(extack,
4660 				   "Device doesn't support VHCA management");
4661 		return -EOPNOTSUPP;
4662 	}
4663 
4664 	if (!MLX5_CAP_GEN_2(esw->dev, max_num_eqs_24b)) {
4665 		NL_SET_ERR_MSG_MOD(extack,
4666 				   "Device doesn't support changing the max number of EQs");
4667 		return -EOPNOTSUPP;
4668 	}
4669 
4670 	if (check_add_overflow(max_io_eqs, MLX5_ESW_MAX_CTRL_EQS, &max_eqs)) {
4671 		NL_SET_ERR_MSG_MOD(extack, "Supplied value out of range");
4672 		return -EINVAL;
4673 	}
4674 
4675 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
4676 	if (!query_ctx)
4677 		return -ENOMEM;
4678 
4679 	mutex_lock(&esw->state_lock);
4680 	err = mlx5_vport_get_other_func_cap(esw->dev, vport_num, query_ctx,
4681 					    MLX5_CAP_GENERAL_2);
4682 	if (err) {
4683 		NL_SET_ERR_MSG_MOD(extack, "Failed getting HCA caps");
4684 		goto out;
4685 	}
4686 
4687 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
4688 	MLX5_SET(cmd_hca_cap_2, hca_caps, max_num_eqs_24b, max_eqs);
4689 
4690 	if (mlx5_esw_is_sf_vport(esw, vport_num))
4691 		MLX5_SET(cmd_hca_cap_2, hca_caps, sf_eq_usage, 1);
4692 
4693 	err = mlx5_vport_set_other_func_cap(esw->dev, hca_caps, vport_num,
4694 					    MLX5_SET_HCA_CAP_OP_MOD_GENERAL_DEVICE2);
4695 	if (err)
4696 		NL_SET_ERR_MSG_MOD(extack, "Failed setting HCA caps");
4697 	vport->max_eqs_set = true;
4698 out:
4699 	mutex_unlock(&esw->state_lock);
4700 	kfree(query_ctx);
4701 	return err;
4702 }
4703 
4704 int
mlx5_devlink_port_fn_max_io_eqs_set_sf_default(struct devlink_port * port,struct netlink_ext_ack * extack)4705 mlx5_devlink_port_fn_max_io_eqs_set_sf_default(struct devlink_port *port,
4706 					       struct netlink_ext_ack *extack)
4707 {
4708 	return mlx5_devlink_port_fn_max_io_eqs_set(port,
4709 						   MLX5_ESW_DEFAULT_SF_COMP_EQS,
4710 						   extack);
4711 }
4712