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