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