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