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