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