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